label.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/ndctl.h>
  7. #include <linux/uuid.h>
  8. #include <linux/slab.h>
  9. #include <linux/io.h>
  10. #include <linux/nd.h>
  11. #include "nd-core.h"
  12. #include "label.h"
  13. #include "nd.h"
  14. static guid_t nvdimm_btt_guid;
  15. static guid_t nvdimm_btt2_guid;
  16. static guid_t nvdimm_pfn_guid;
  17. static guid_t nvdimm_dax_guid;
  18. static uuid_t nvdimm_btt_uuid;
  19. static uuid_t nvdimm_btt2_uuid;
  20. static uuid_t nvdimm_pfn_uuid;
  21. static uuid_t nvdimm_dax_uuid;
  22. static uuid_t cxl_region_uuid;
  23. static uuid_t cxl_namespace_uuid;
  24. static const char NSINDEX_SIGNATURE[] = "NAMESPACE_INDEX\0";
  25. static u32 best_seq(u32 a, u32 b)
  26. {
  27. a &= NSINDEX_SEQ_MASK;
  28. b &= NSINDEX_SEQ_MASK;
  29. if (a == 0 || a == b)
  30. return b;
  31. else if (b == 0)
  32. return a;
  33. else if (nd_inc_seq(a) == b)
  34. return b;
  35. else
  36. return a;
  37. }
  38. unsigned sizeof_namespace_label(struct nvdimm_drvdata *ndd)
  39. {
  40. return ndd->nslabel_size;
  41. }
  42. static size_t __sizeof_namespace_index(u32 nslot)
  43. {
  44. return ALIGN(sizeof(struct nd_namespace_index) + DIV_ROUND_UP(nslot, 8),
  45. NSINDEX_ALIGN);
  46. }
  47. static int __nvdimm_num_label_slots(struct nvdimm_drvdata *ndd,
  48. size_t index_size)
  49. {
  50. return (ndd->nsarea.config_size - index_size * 2) /
  51. sizeof_namespace_label(ndd);
  52. }
  53. int nvdimm_num_label_slots(struct nvdimm_drvdata *ndd)
  54. {
  55. u32 tmp_nslot, n;
  56. tmp_nslot = ndd->nsarea.config_size / sizeof_namespace_label(ndd);
  57. n = __sizeof_namespace_index(tmp_nslot) / NSINDEX_ALIGN;
  58. return __nvdimm_num_label_slots(ndd, NSINDEX_ALIGN * n);
  59. }
  60. size_t sizeof_namespace_index(struct nvdimm_drvdata *ndd)
  61. {
  62. u32 nslot, space, size;
  63. /*
  64. * Per UEFI 2.7, the minimum size of the Label Storage Area is large
  65. * enough to hold 2 index blocks and 2 labels. The minimum index
  66. * block size is 256 bytes. The label size is 128 for namespaces
  67. * prior to version 1.2 and at minimum 256 for version 1.2 and later.
  68. */
  69. nslot = nvdimm_num_label_slots(ndd);
  70. space = ndd->nsarea.config_size - nslot * sizeof_namespace_label(ndd);
  71. size = __sizeof_namespace_index(nslot) * 2;
  72. if (size <= space && nslot >= 2)
  73. return size / 2;
  74. dev_err(ndd->dev, "label area (%d) too small to host (%d byte) labels\n",
  75. ndd->nsarea.config_size, sizeof_namespace_label(ndd));
  76. return 0;
  77. }
  78. static int __nd_label_validate(struct nvdimm_drvdata *ndd)
  79. {
  80. /*
  81. * On media label format consists of two index blocks followed
  82. * by an array of labels. None of these structures are ever
  83. * updated in place. A sequence number tracks the current
  84. * active index and the next one to write, while labels are
  85. * written to free slots.
  86. *
  87. * +------------+
  88. * | |
  89. * | nsindex0 |
  90. * | |
  91. * +------------+
  92. * | |
  93. * | nsindex1 |
  94. * | |
  95. * +------------+
  96. * | label0 |
  97. * +------------+
  98. * | label1 |
  99. * +------------+
  100. * | |
  101. * ....nslot...
  102. * | |
  103. * +------------+
  104. * | labelN |
  105. * +------------+
  106. */
  107. struct nd_namespace_index *nsindex[] = {
  108. to_namespace_index(ndd, 0),
  109. to_namespace_index(ndd, 1),
  110. };
  111. const int num_index = ARRAY_SIZE(nsindex);
  112. struct device *dev = ndd->dev;
  113. bool valid[2] = { 0 };
  114. int i, num_valid = 0;
  115. u32 seq;
  116. for (i = 0; i < num_index; i++) {
  117. u32 nslot;
  118. u8 sig[NSINDEX_SIG_LEN];
  119. u64 sum_save, sum, size;
  120. unsigned int version, labelsize;
  121. memcpy(sig, nsindex[i]->sig, NSINDEX_SIG_LEN);
  122. if (memcmp(sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN) != 0) {
  123. dev_dbg(dev, "nsindex%d signature invalid\n", i);
  124. continue;
  125. }
  126. /* label sizes larger than 128 arrived with v1.2 */
  127. version = __le16_to_cpu(nsindex[i]->major) * 100
  128. + __le16_to_cpu(nsindex[i]->minor);
  129. if (version >= 102)
  130. labelsize = 1 << (7 + nsindex[i]->labelsize);
  131. else
  132. labelsize = 128;
  133. if (labelsize != sizeof_namespace_label(ndd)) {
  134. dev_dbg(dev, "nsindex%d labelsize %d invalid\n",
  135. i, nsindex[i]->labelsize);
  136. continue;
  137. }
  138. sum_save = __le64_to_cpu(nsindex[i]->checksum);
  139. nsindex[i]->checksum = __cpu_to_le64(0);
  140. sum = nd_fletcher64(nsindex[i], sizeof_namespace_index(ndd), 1);
  141. nsindex[i]->checksum = __cpu_to_le64(sum_save);
  142. if (sum != sum_save) {
  143. dev_dbg(dev, "nsindex%d checksum invalid\n", i);
  144. continue;
  145. }
  146. seq = __le32_to_cpu(nsindex[i]->seq);
  147. if ((seq & NSINDEX_SEQ_MASK) == 0) {
  148. dev_dbg(dev, "nsindex%d sequence: %#x invalid\n", i, seq);
  149. continue;
  150. }
  151. /* sanity check the index against expected values */
  152. if (__le64_to_cpu(nsindex[i]->myoff)
  153. != i * sizeof_namespace_index(ndd)) {
  154. dev_dbg(dev, "nsindex%d myoff: %#llx invalid\n",
  155. i, (unsigned long long)
  156. __le64_to_cpu(nsindex[i]->myoff));
  157. continue;
  158. }
  159. if (__le64_to_cpu(nsindex[i]->otheroff)
  160. != (!i) * sizeof_namespace_index(ndd)) {
  161. dev_dbg(dev, "nsindex%d otheroff: %#llx invalid\n",
  162. i, (unsigned long long)
  163. __le64_to_cpu(nsindex[i]->otheroff));
  164. continue;
  165. }
  166. if (__le64_to_cpu(nsindex[i]->labeloff)
  167. != 2 * sizeof_namespace_index(ndd)) {
  168. dev_dbg(dev, "nsindex%d labeloff: %#llx invalid\n",
  169. i, (unsigned long long)
  170. __le64_to_cpu(nsindex[i]->labeloff));
  171. continue;
  172. }
  173. size = __le64_to_cpu(nsindex[i]->mysize);
  174. if (size > sizeof_namespace_index(ndd)
  175. || size < sizeof(struct nd_namespace_index)) {
  176. dev_dbg(dev, "nsindex%d mysize: %#llx invalid\n", i, size);
  177. continue;
  178. }
  179. nslot = __le32_to_cpu(nsindex[i]->nslot);
  180. if (nslot * sizeof_namespace_label(ndd)
  181. + 2 * sizeof_namespace_index(ndd)
  182. > ndd->nsarea.config_size) {
  183. dev_dbg(dev, "nsindex%d nslot: %u invalid, config_size: %#x\n",
  184. i, nslot, ndd->nsarea.config_size);
  185. continue;
  186. }
  187. valid[i] = true;
  188. num_valid++;
  189. }
  190. switch (num_valid) {
  191. case 0:
  192. break;
  193. case 1:
  194. for (i = 0; i < num_index; i++)
  195. if (valid[i])
  196. return i;
  197. /* can't have num_valid > 0 but valid[] = { false, false } */
  198. WARN_ON(1);
  199. break;
  200. default:
  201. /* pick the best index... */
  202. seq = best_seq(__le32_to_cpu(nsindex[0]->seq),
  203. __le32_to_cpu(nsindex[1]->seq));
  204. if (seq == (__le32_to_cpu(nsindex[1]->seq) & NSINDEX_SEQ_MASK))
  205. return 1;
  206. else
  207. return 0;
  208. break;
  209. }
  210. return -1;
  211. }
  212. static int nd_label_validate(struct nvdimm_drvdata *ndd)
  213. {
  214. /*
  215. * In order to probe for and validate namespace index blocks we
  216. * need to know the size of the labels, and we can't trust the
  217. * size of the labels until we validate the index blocks.
  218. * Resolve this dependency loop by probing for known label
  219. * sizes, but default to v1.2 256-byte namespace labels if
  220. * discovery fails.
  221. */
  222. int label_size[] = { 128, 256 };
  223. int i, rc;
  224. for (i = 0; i < ARRAY_SIZE(label_size); i++) {
  225. ndd->nslabel_size = label_size[i];
  226. rc = __nd_label_validate(ndd);
  227. if (rc >= 0)
  228. return rc;
  229. }
  230. return -1;
  231. }
  232. static void nd_label_copy(struct nvdimm_drvdata *ndd,
  233. struct nd_namespace_index *dst,
  234. struct nd_namespace_index *src)
  235. {
  236. /* just exit if either destination or source is NULL */
  237. if (!dst || !src)
  238. return;
  239. memcpy(dst, src, sizeof_namespace_index(ndd));
  240. }
  241. static struct nd_namespace_label *nd_label_base(struct nvdimm_drvdata *ndd)
  242. {
  243. void *base = to_namespace_index(ndd, 0);
  244. return base + 2 * sizeof_namespace_index(ndd);
  245. }
  246. static int to_slot(struct nvdimm_drvdata *ndd,
  247. struct nd_namespace_label *nd_label)
  248. {
  249. unsigned long label, base;
  250. label = (unsigned long) nd_label;
  251. base = (unsigned long) nd_label_base(ndd);
  252. return (label - base) / sizeof_namespace_label(ndd);
  253. }
  254. static struct nd_namespace_label *to_label(struct nvdimm_drvdata *ndd, int slot)
  255. {
  256. unsigned long label, base;
  257. base = (unsigned long) nd_label_base(ndd);
  258. label = base + sizeof_namespace_label(ndd) * slot;
  259. return (struct nd_namespace_label *) label;
  260. }
  261. #define for_each_clear_bit_le(bit, addr, size) \
  262. for ((bit) = find_next_zero_bit_le((addr), (size), 0); \
  263. (bit) < (size); \
  264. (bit) = find_next_zero_bit_le((addr), (size), (bit) + 1))
  265. /**
  266. * preamble_index - common variable initialization for nd_label_* routines
  267. * @ndd: dimm container for the relevant label set
  268. * @idx: namespace_index index
  269. * @nsindex_out: on return set to the currently active namespace index
  270. * @free: on return set to the free label bitmap in the index
  271. * @nslot: on return set to the number of slots in the label space
  272. */
  273. static bool preamble_index(struct nvdimm_drvdata *ndd, int idx,
  274. struct nd_namespace_index **nsindex_out,
  275. unsigned long **free, u32 *nslot)
  276. {
  277. struct nd_namespace_index *nsindex;
  278. nsindex = to_namespace_index(ndd, idx);
  279. if (nsindex == NULL)
  280. return false;
  281. *free = (unsigned long *) nsindex->free;
  282. *nslot = __le32_to_cpu(nsindex->nslot);
  283. *nsindex_out = nsindex;
  284. return true;
  285. }
  286. char *nd_label_gen_id(struct nd_label_id *label_id, const uuid_t *uuid,
  287. u32 flags)
  288. {
  289. if (!label_id || !uuid)
  290. return NULL;
  291. snprintf(label_id->id, ND_LABEL_ID_SIZE, "pmem-%pUb", uuid);
  292. return label_id->id;
  293. }
  294. static bool preamble_current(struct nvdimm_drvdata *ndd,
  295. struct nd_namespace_index **nsindex,
  296. unsigned long **free, u32 *nslot)
  297. {
  298. return preamble_index(ndd, ndd->ns_current, nsindex,
  299. free, nslot);
  300. }
  301. static bool preamble_next(struct nvdimm_drvdata *ndd,
  302. struct nd_namespace_index **nsindex,
  303. unsigned long **free, u32 *nslot)
  304. {
  305. return preamble_index(ndd, ndd->ns_next, nsindex,
  306. free, nslot);
  307. }
  308. static bool nsl_validate_checksum(struct nvdimm_drvdata *ndd,
  309. struct nd_namespace_label *nd_label)
  310. {
  311. u64 sum, sum_save;
  312. if (!ndd->cxl && !efi_namespace_label_has(ndd, checksum))
  313. return true;
  314. sum_save = nsl_get_checksum(ndd, nd_label);
  315. nsl_set_checksum(ndd, nd_label, 0);
  316. sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
  317. nsl_set_checksum(ndd, nd_label, sum_save);
  318. return sum == sum_save;
  319. }
  320. static void nsl_calculate_checksum(struct nvdimm_drvdata *ndd,
  321. struct nd_namespace_label *nd_label)
  322. {
  323. u64 sum;
  324. if (!ndd->cxl && !efi_namespace_label_has(ndd, checksum))
  325. return;
  326. nsl_set_checksum(ndd, nd_label, 0);
  327. sum = nd_fletcher64(nd_label, sizeof_namespace_label(ndd), 1);
  328. nsl_set_checksum(ndd, nd_label, sum);
  329. }
  330. static bool slot_valid(struct nvdimm_drvdata *ndd,
  331. struct nd_namespace_label *nd_label, u32 slot)
  332. {
  333. bool valid;
  334. /* check that we are written where we expect to be written */
  335. if (slot != nsl_get_slot(ndd, nd_label))
  336. return false;
  337. valid = nsl_validate_checksum(ndd, nd_label);
  338. if (!valid)
  339. dev_dbg(ndd->dev, "fail checksum. slot: %d\n", slot);
  340. return valid;
  341. }
  342. int nd_label_reserve_dpa(struct nvdimm_drvdata *ndd)
  343. {
  344. struct nd_namespace_index *nsindex;
  345. unsigned long *free;
  346. u32 nslot, slot;
  347. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  348. return 0; /* no label, nothing to reserve */
  349. for_each_clear_bit_le(slot, free, nslot) {
  350. struct nd_namespace_label *nd_label;
  351. struct nd_region *nd_region = NULL;
  352. struct nd_label_id label_id;
  353. struct resource *res;
  354. uuid_t label_uuid;
  355. u32 flags;
  356. nd_label = to_label(ndd, slot);
  357. if (!slot_valid(ndd, nd_label, slot))
  358. continue;
  359. nsl_get_uuid(ndd, nd_label, &label_uuid);
  360. flags = nsl_get_flags(ndd, nd_label);
  361. nd_label_gen_id(&label_id, &label_uuid, flags);
  362. res = nvdimm_allocate_dpa(ndd, &label_id,
  363. nsl_get_dpa(ndd, nd_label),
  364. nsl_get_rawsize(ndd, nd_label));
  365. nd_dbg_dpa(nd_region, ndd, res, "reserve\n");
  366. if (!res)
  367. return -EBUSY;
  368. }
  369. return 0;
  370. }
  371. int nd_label_data_init(struct nvdimm_drvdata *ndd)
  372. {
  373. size_t config_size, read_size, max_xfer, offset;
  374. struct nd_namespace_index *nsindex;
  375. unsigned int i;
  376. int rc = 0;
  377. u32 nslot;
  378. if (ndd->data)
  379. return 0;
  380. if (ndd->nsarea.status || ndd->nsarea.max_xfer == 0 ||
  381. ndd->nsarea.config_size == 0) {
  382. dev_dbg(ndd->dev, "failed to init config data area: (%u:%u)\n",
  383. ndd->nsarea.max_xfer, ndd->nsarea.config_size);
  384. return -ENXIO;
  385. }
  386. /*
  387. * We need to determine the maximum index area as this is the section
  388. * we must read and validate before we can start processing labels.
  389. *
  390. * If the area is too small to contain the two indexes and 2 labels
  391. * then we abort.
  392. *
  393. * Start at a label size of 128 as this should result in the largest
  394. * possible namespace index size.
  395. */
  396. ndd->nslabel_size = 128;
  397. read_size = sizeof_namespace_index(ndd) * 2;
  398. if (!read_size)
  399. return -ENXIO;
  400. /* Allocate config data */
  401. config_size = ndd->nsarea.config_size;
  402. ndd->data = kvzalloc(config_size, GFP_KERNEL);
  403. if (!ndd->data)
  404. return -ENOMEM;
  405. /*
  406. * We want to guarantee as few reads as possible while conserving
  407. * memory. To do that we figure out how much unused space will be left
  408. * in the last read, divide that by the total number of reads it is
  409. * going to take given our maximum transfer size, and then reduce our
  410. * maximum transfer size based on that result.
  411. */
  412. max_xfer = min_t(size_t, ndd->nsarea.max_xfer, config_size);
  413. if (read_size < max_xfer) {
  414. /* trim waste */
  415. max_xfer -= ((max_xfer - 1) - (config_size - 1) % max_xfer) /
  416. DIV_ROUND_UP(config_size, max_xfer);
  417. /* make certain we read indexes in exactly 1 read */
  418. if (max_xfer < read_size)
  419. max_xfer = read_size;
  420. }
  421. /* Make our initial read size a multiple of max_xfer size */
  422. read_size = min(DIV_ROUND_UP(read_size, max_xfer) * max_xfer,
  423. config_size);
  424. /* Read the index data */
  425. rc = nvdimm_get_config_data(ndd, ndd->data, 0, read_size);
  426. if (rc)
  427. goto out_err;
  428. /* Validate index data, if not valid assume all labels are invalid */
  429. ndd->ns_current = nd_label_validate(ndd);
  430. if (ndd->ns_current < 0)
  431. return 0;
  432. /* Record our index values */
  433. ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
  434. /* Copy "current" index on top of the "next" index */
  435. nsindex = to_current_namespace_index(ndd);
  436. nd_label_copy(ndd, to_next_namespace_index(ndd), nsindex);
  437. /* Determine starting offset for label data */
  438. offset = __le64_to_cpu(nsindex->labeloff);
  439. nslot = __le32_to_cpu(nsindex->nslot);
  440. /* Loop through the free list pulling in any active labels */
  441. for (i = 0; i < nslot; i++, offset += ndd->nslabel_size) {
  442. size_t label_read_size;
  443. /* zero out the unused labels */
  444. if (test_bit_le(i, nsindex->free)) {
  445. memset(ndd->data + offset, 0, ndd->nslabel_size);
  446. continue;
  447. }
  448. /* if we already read past here then just continue */
  449. if (offset + ndd->nslabel_size <= read_size)
  450. continue;
  451. /* if we haven't read in a while reset our read_size offset */
  452. if (read_size < offset)
  453. read_size = offset;
  454. /* determine how much more will be read after this next call. */
  455. label_read_size = offset + ndd->nslabel_size - read_size;
  456. label_read_size = DIV_ROUND_UP(label_read_size, max_xfer) *
  457. max_xfer;
  458. /* truncate last read if needed */
  459. if (read_size + label_read_size > config_size)
  460. label_read_size = config_size - read_size;
  461. /* Read the label data */
  462. rc = nvdimm_get_config_data(ndd, ndd->data + read_size,
  463. read_size, label_read_size);
  464. if (rc)
  465. goto out_err;
  466. /* push read_size to next read offset */
  467. read_size += label_read_size;
  468. }
  469. dev_dbg(ndd->dev, "len: %zu rc: %d\n", offset, rc);
  470. out_err:
  471. return rc;
  472. }
  473. int nd_label_active_count(struct nvdimm_drvdata *ndd)
  474. {
  475. struct nd_namespace_index *nsindex;
  476. unsigned long *free;
  477. u32 nslot, slot;
  478. int count = 0;
  479. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  480. return 0;
  481. for_each_clear_bit_le(slot, free, nslot) {
  482. struct nd_namespace_label *nd_label;
  483. nd_label = to_label(ndd, slot);
  484. if (!slot_valid(ndd, nd_label, slot)) {
  485. u32 label_slot = nsl_get_slot(ndd, nd_label);
  486. u64 size = nsl_get_rawsize(ndd, nd_label);
  487. u64 dpa = nsl_get_dpa(ndd, nd_label);
  488. dev_dbg(ndd->dev,
  489. "slot%d invalid slot: %d dpa: %llx size: %llx\n",
  490. slot, label_slot, dpa, size);
  491. continue;
  492. }
  493. count++;
  494. }
  495. return count;
  496. }
  497. struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
  498. {
  499. struct nd_namespace_index *nsindex;
  500. unsigned long *free;
  501. u32 nslot, slot;
  502. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  503. return NULL;
  504. for_each_clear_bit_le(slot, free, nslot) {
  505. struct nd_namespace_label *nd_label;
  506. nd_label = to_label(ndd, slot);
  507. if (!slot_valid(ndd, nd_label, slot))
  508. continue;
  509. if (n-- == 0)
  510. return to_label(ndd, slot);
  511. }
  512. return NULL;
  513. }
  514. u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
  515. {
  516. struct nd_namespace_index *nsindex;
  517. unsigned long *free;
  518. u32 nslot, slot;
  519. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  520. return UINT_MAX;
  521. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  522. slot = find_next_bit_le(free, nslot, 0);
  523. if (slot == nslot)
  524. return UINT_MAX;
  525. clear_bit_le(slot, free);
  526. return slot;
  527. }
  528. bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
  529. {
  530. struct nd_namespace_index *nsindex;
  531. unsigned long *free;
  532. u32 nslot;
  533. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  534. return false;
  535. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  536. if (slot < nslot)
  537. return !test_and_set_bit_le(slot, free);
  538. return false;
  539. }
  540. u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
  541. {
  542. struct nd_namespace_index *nsindex;
  543. unsigned long *free;
  544. u32 nslot;
  545. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  546. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  547. return nvdimm_num_label_slots(ndd);
  548. return bitmap_weight(free, nslot);
  549. }
  550. static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
  551. unsigned long flags)
  552. {
  553. struct nd_namespace_index *nsindex;
  554. unsigned long offset;
  555. u64 checksum;
  556. u32 nslot;
  557. int rc;
  558. nsindex = to_namespace_index(ndd, index);
  559. if (flags & ND_NSINDEX_INIT)
  560. nslot = nvdimm_num_label_slots(ndd);
  561. else
  562. nslot = __le32_to_cpu(nsindex->nslot);
  563. memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
  564. memset(&nsindex->flags, 0, 3);
  565. nsindex->labelsize = sizeof_namespace_label(ndd) >> 8;
  566. nsindex->seq = __cpu_to_le32(seq);
  567. offset = (unsigned long) nsindex
  568. - (unsigned long) to_namespace_index(ndd, 0);
  569. nsindex->myoff = __cpu_to_le64(offset);
  570. nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
  571. offset = (unsigned long) to_namespace_index(ndd,
  572. nd_label_next_nsindex(index))
  573. - (unsigned long) to_namespace_index(ndd, 0);
  574. nsindex->otheroff = __cpu_to_le64(offset);
  575. offset = (unsigned long) nd_label_base(ndd)
  576. - (unsigned long) to_namespace_index(ndd, 0);
  577. nsindex->labeloff = __cpu_to_le64(offset);
  578. nsindex->nslot = __cpu_to_le32(nslot);
  579. nsindex->major = __cpu_to_le16(1);
  580. if (sizeof_namespace_label(ndd) < 256)
  581. nsindex->minor = __cpu_to_le16(1);
  582. else
  583. nsindex->minor = __cpu_to_le16(2);
  584. nsindex->checksum = __cpu_to_le64(0);
  585. if (flags & ND_NSINDEX_INIT) {
  586. unsigned long *free = (unsigned long *) nsindex->free;
  587. u32 nfree = ALIGN(nslot, BITS_PER_LONG);
  588. int last_bits, i;
  589. memset(nsindex->free, 0xff, nfree / 8);
  590. for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
  591. clear_bit_le(nslot + i, free);
  592. }
  593. checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
  594. nsindex->checksum = __cpu_to_le64(checksum);
  595. rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
  596. nsindex, sizeof_namespace_index(ndd));
  597. if (rc < 0)
  598. return rc;
  599. if (flags & ND_NSINDEX_INIT)
  600. return 0;
  601. /* copy the index we just wrote to the new 'next' */
  602. WARN_ON(index != ndd->ns_next);
  603. nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
  604. ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
  605. ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
  606. WARN_ON(ndd->ns_current == ndd->ns_next);
  607. return 0;
  608. }
  609. static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
  610. struct nd_namespace_label *nd_label)
  611. {
  612. return (unsigned long) nd_label
  613. - (unsigned long) to_namespace_index(ndd, 0);
  614. }
  615. static enum nvdimm_claim_class guid_to_nvdimm_cclass(guid_t *guid)
  616. {
  617. if (guid_equal(guid, &nvdimm_btt_guid))
  618. return NVDIMM_CCLASS_BTT;
  619. else if (guid_equal(guid, &nvdimm_btt2_guid))
  620. return NVDIMM_CCLASS_BTT2;
  621. else if (guid_equal(guid, &nvdimm_pfn_guid))
  622. return NVDIMM_CCLASS_PFN;
  623. else if (guid_equal(guid, &nvdimm_dax_guid))
  624. return NVDIMM_CCLASS_DAX;
  625. else if (guid_equal(guid, &guid_null))
  626. return NVDIMM_CCLASS_NONE;
  627. return NVDIMM_CCLASS_UNKNOWN;
  628. }
  629. /* CXL labels store UUIDs instead of GUIDs for the same data */
  630. static enum nvdimm_claim_class uuid_to_nvdimm_cclass(uuid_t *uuid)
  631. {
  632. if (uuid_equal(uuid, &nvdimm_btt_uuid))
  633. return NVDIMM_CCLASS_BTT;
  634. else if (uuid_equal(uuid, &nvdimm_btt2_uuid))
  635. return NVDIMM_CCLASS_BTT2;
  636. else if (uuid_equal(uuid, &nvdimm_pfn_uuid))
  637. return NVDIMM_CCLASS_PFN;
  638. else if (uuid_equal(uuid, &nvdimm_dax_uuid))
  639. return NVDIMM_CCLASS_DAX;
  640. else if (uuid_equal(uuid, &uuid_null))
  641. return NVDIMM_CCLASS_NONE;
  642. return NVDIMM_CCLASS_UNKNOWN;
  643. }
  644. static const guid_t *to_abstraction_guid(enum nvdimm_claim_class claim_class,
  645. guid_t *target)
  646. {
  647. if (claim_class == NVDIMM_CCLASS_BTT)
  648. return &nvdimm_btt_guid;
  649. else if (claim_class == NVDIMM_CCLASS_BTT2)
  650. return &nvdimm_btt2_guid;
  651. else if (claim_class == NVDIMM_CCLASS_PFN)
  652. return &nvdimm_pfn_guid;
  653. else if (claim_class == NVDIMM_CCLASS_DAX)
  654. return &nvdimm_dax_guid;
  655. else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
  656. /*
  657. * If we're modifying a namespace for which we don't
  658. * know the claim_class, don't touch the existing guid.
  659. */
  660. return target;
  661. } else
  662. return &guid_null;
  663. }
  664. /* CXL labels store UUIDs instead of GUIDs for the same data */
  665. static const uuid_t *to_abstraction_uuid(enum nvdimm_claim_class claim_class,
  666. uuid_t *target)
  667. {
  668. if (claim_class == NVDIMM_CCLASS_BTT)
  669. return &nvdimm_btt_uuid;
  670. else if (claim_class == NVDIMM_CCLASS_BTT2)
  671. return &nvdimm_btt2_uuid;
  672. else if (claim_class == NVDIMM_CCLASS_PFN)
  673. return &nvdimm_pfn_uuid;
  674. else if (claim_class == NVDIMM_CCLASS_DAX)
  675. return &nvdimm_dax_uuid;
  676. else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
  677. /*
  678. * If we're modifying a namespace for which we don't
  679. * know the claim_class, don't touch the existing uuid.
  680. */
  681. return target;
  682. } else
  683. return &uuid_null;
  684. }
  685. static void reap_victim(struct nd_mapping *nd_mapping,
  686. struct nd_label_ent *victim)
  687. {
  688. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  689. u32 slot = to_slot(ndd, victim->label);
  690. dev_dbg(ndd->dev, "free: %d\n", slot);
  691. nd_label_free_slot(ndd, slot);
  692. victim->label = NULL;
  693. }
  694. static void nsl_set_type_guid(struct nvdimm_drvdata *ndd,
  695. struct nd_namespace_label *nd_label, guid_t *guid)
  696. {
  697. if (efi_namespace_label_has(ndd, type_guid))
  698. guid_copy(&nd_label->efi.type_guid, guid);
  699. }
  700. bool nsl_validate_type_guid(struct nvdimm_drvdata *ndd,
  701. struct nd_namespace_label *nd_label, guid_t *guid)
  702. {
  703. if (ndd->cxl || !efi_namespace_label_has(ndd, type_guid))
  704. return true;
  705. if (!guid_equal(&nd_label->efi.type_guid, guid)) {
  706. dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n", guid,
  707. &nd_label->efi.type_guid);
  708. return false;
  709. }
  710. return true;
  711. }
  712. static void nsl_set_claim_class(struct nvdimm_drvdata *ndd,
  713. struct nd_namespace_label *nd_label,
  714. enum nvdimm_claim_class claim_class)
  715. {
  716. if (ndd->cxl) {
  717. uuid_t uuid;
  718. import_uuid(&uuid, nd_label->cxl.abstraction_uuid);
  719. export_uuid(nd_label->cxl.abstraction_uuid,
  720. to_abstraction_uuid(claim_class, &uuid));
  721. return;
  722. }
  723. if (!efi_namespace_label_has(ndd, abstraction_guid))
  724. return;
  725. guid_copy(&nd_label->efi.abstraction_guid,
  726. to_abstraction_guid(claim_class,
  727. &nd_label->efi.abstraction_guid));
  728. }
  729. enum nvdimm_claim_class nsl_get_claim_class(struct nvdimm_drvdata *ndd,
  730. struct nd_namespace_label *nd_label)
  731. {
  732. if (ndd->cxl) {
  733. uuid_t uuid;
  734. import_uuid(&uuid, nd_label->cxl.abstraction_uuid);
  735. return uuid_to_nvdimm_cclass(&uuid);
  736. }
  737. if (!efi_namespace_label_has(ndd, abstraction_guid))
  738. return NVDIMM_CCLASS_NONE;
  739. return guid_to_nvdimm_cclass(&nd_label->efi.abstraction_guid);
  740. }
  741. static int __pmem_label_update(struct nd_region *nd_region,
  742. struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
  743. int pos, unsigned long flags)
  744. {
  745. struct nd_namespace_common *ndns = &nspm->nsio.common;
  746. struct nd_interleave_set *nd_set = nd_region->nd_set;
  747. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  748. struct nd_namespace_label *nd_label;
  749. struct nd_namespace_index *nsindex;
  750. struct nd_label_ent *label_ent;
  751. struct nd_label_id label_id;
  752. struct resource *res;
  753. unsigned long *free;
  754. u32 nslot, slot;
  755. size_t offset;
  756. u64 cookie;
  757. int rc;
  758. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  759. return -ENXIO;
  760. cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
  761. nd_label_gen_id(&label_id, nspm->uuid, 0);
  762. for_each_dpa_resource(ndd, res)
  763. if (strcmp(res->name, label_id.id) == 0)
  764. break;
  765. if (!res) {
  766. WARN_ON_ONCE(1);
  767. return -ENXIO;
  768. }
  769. /* allocate and write the label to the staging (next) index */
  770. slot = nd_label_alloc_slot(ndd);
  771. if (slot == UINT_MAX)
  772. return -ENXIO;
  773. dev_dbg(ndd->dev, "allocated: %d\n", slot);
  774. nd_label = to_label(ndd, slot);
  775. memset(nd_label, 0, sizeof_namespace_label(ndd));
  776. nsl_set_uuid(ndd, nd_label, nspm->uuid);
  777. nsl_set_name(ndd, nd_label, nspm->alt_name);
  778. nsl_set_flags(ndd, nd_label, flags);
  779. nsl_set_nlabel(ndd, nd_label, nd_region->ndr_mappings);
  780. nsl_set_nrange(ndd, nd_label, 1);
  781. nsl_set_position(ndd, nd_label, pos);
  782. nsl_set_isetcookie(ndd, nd_label, cookie);
  783. nsl_set_rawsize(ndd, nd_label, resource_size(res));
  784. nsl_set_lbasize(ndd, nd_label, nspm->lbasize);
  785. nsl_set_dpa(ndd, nd_label, res->start);
  786. nsl_set_slot(ndd, nd_label, slot);
  787. nsl_set_type_guid(ndd, nd_label, &nd_set->type_guid);
  788. nsl_set_claim_class(ndd, nd_label, ndns->claim_class);
  789. nsl_calculate_checksum(ndd, nd_label);
  790. nd_dbg_dpa(nd_region, ndd, res, "\n");
  791. /* update label */
  792. offset = nd_label_offset(ndd, nd_label);
  793. rc = nvdimm_set_config_data(ndd, offset, nd_label,
  794. sizeof_namespace_label(ndd));
  795. if (rc < 0)
  796. return rc;
  797. /* Garbage collect the previous label */
  798. mutex_lock(&nd_mapping->lock);
  799. list_for_each_entry(label_ent, &nd_mapping->labels, list) {
  800. if (!label_ent->label)
  801. continue;
  802. if (test_and_clear_bit(ND_LABEL_REAP, &label_ent->flags) ||
  803. nsl_uuid_equal(ndd, label_ent->label, nspm->uuid))
  804. reap_victim(nd_mapping, label_ent);
  805. }
  806. /* update index */
  807. rc = nd_label_write_index(ndd, ndd->ns_next,
  808. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  809. if (rc == 0) {
  810. list_for_each_entry(label_ent, &nd_mapping->labels, list)
  811. if (!label_ent->label) {
  812. label_ent->label = nd_label;
  813. nd_label = NULL;
  814. break;
  815. }
  816. dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
  817. "failed to track label: %d\n",
  818. to_slot(ndd, nd_label));
  819. if (nd_label)
  820. rc = -ENXIO;
  821. }
  822. mutex_unlock(&nd_mapping->lock);
  823. return rc;
  824. }
  825. static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
  826. {
  827. int i, old_num_labels = 0;
  828. struct nd_label_ent *label_ent;
  829. struct nd_namespace_index *nsindex;
  830. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  831. mutex_lock(&nd_mapping->lock);
  832. list_for_each_entry(label_ent, &nd_mapping->labels, list)
  833. old_num_labels++;
  834. mutex_unlock(&nd_mapping->lock);
  835. /*
  836. * We need to preserve all the old labels for the mapping so
  837. * they can be garbage collected after writing the new labels.
  838. */
  839. for (i = old_num_labels; i < num_labels; i++) {
  840. label_ent = kzalloc_obj(*label_ent);
  841. if (!label_ent)
  842. return -ENOMEM;
  843. mutex_lock(&nd_mapping->lock);
  844. list_add_tail(&label_ent->list, &nd_mapping->labels);
  845. mutex_unlock(&nd_mapping->lock);
  846. }
  847. if (ndd->ns_current == -1 || ndd->ns_next == -1)
  848. /* pass */;
  849. else
  850. return max(num_labels, old_num_labels);
  851. nsindex = to_namespace_index(ndd, 0);
  852. memset(nsindex, 0, ndd->nsarea.config_size);
  853. for (i = 0; i < 2; i++) {
  854. int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
  855. if (rc)
  856. return rc;
  857. }
  858. ndd->ns_next = 1;
  859. ndd->ns_current = 0;
  860. return max(num_labels, old_num_labels);
  861. }
  862. static int del_labels(struct nd_mapping *nd_mapping, uuid_t *uuid)
  863. {
  864. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  865. struct nd_label_ent *label_ent, *e;
  866. struct nd_namespace_index *nsindex;
  867. unsigned long *free;
  868. LIST_HEAD(list);
  869. u32 nslot, slot;
  870. int active = 0;
  871. if (!uuid)
  872. return 0;
  873. /* no index || no labels == nothing to delete */
  874. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  875. return 0;
  876. mutex_lock(&nd_mapping->lock);
  877. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  878. struct nd_namespace_label *nd_label = label_ent->label;
  879. if (!nd_label)
  880. continue;
  881. active++;
  882. if (!nsl_uuid_equal(ndd, nd_label, uuid))
  883. continue;
  884. active--;
  885. slot = to_slot(ndd, nd_label);
  886. nd_label_free_slot(ndd, slot);
  887. dev_dbg(ndd->dev, "free: %d\n", slot);
  888. list_move_tail(&label_ent->list, &list);
  889. label_ent->label = NULL;
  890. }
  891. list_splice_tail_init(&list, &nd_mapping->labels);
  892. if (active == 0) {
  893. nd_mapping_free_labels(nd_mapping);
  894. dev_dbg(ndd->dev, "no more active labels\n");
  895. }
  896. mutex_unlock(&nd_mapping->lock);
  897. return nd_label_write_index(ndd, ndd->ns_next,
  898. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  899. }
  900. int nd_pmem_namespace_label_update(struct nd_region *nd_region,
  901. struct nd_namespace_pmem *nspm, resource_size_t size)
  902. {
  903. int i, rc;
  904. for (i = 0; i < nd_region->ndr_mappings; i++) {
  905. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  906. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  907. struct resource *res;
  908. int count = 0;
  909. if (size == 0) {
  910. rc = del_labels(nd_mapping, nspm->uuid);
  911. if (rc)
  912. return rc;
  913. continue;
  914. }
  915. for_each_dpa_resource(ndd, res)
  916. if (strncmp(res->name, "pmem", 4) == 0)
  917. count++;
  918. WARN_ON_ONCE(!count);
  919. rc = init_labels(nd_mapping, count);
  920. if (rc < 0)
  921. return rc;
  922. rc = __pmem_label_update(nd_region, nd_mapping, nspm, i,
  923. NSLABEL_FLAG_UPDATING);
  924. if (rc)
  925. return rc;
  926. }
  927. if (size == 0)
  928. return 0;
  929. /* Clear the UPDATING flag per UEFI 2.7 expectations */
  930. for (i = 0; i < nd_region->ndr_mappings; i++) {
  931. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  932. rc = __pmem_label_update(nd_region, nd_mapping, nspm, i, 0);
  933. if (rc)
  934. return rc;
  935. }
  936. return 0;
  937. }
  938. int __init nd_label_init(void)
  939. {
  940. WARN_ON(guid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_guid));
  941. WARN_ON(guid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_guid));
  942. WARN_ON(guid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_guid));
  943. WARN_ON(guid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_guid));
  944. WARN_ON(uuid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_uuid));
  945. WARN_ON(uuid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_uuid));
  946. WARN_ON(uuid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_uuid));
  947. WARN_ON(uuid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_uuid));
  948. WARN_ON(uuid_parse(CXL_REGION_UUID, &cxl_region_uuid));
  949. WARN_ON(uuid_parse(CXL_NAMESPACE_UUID, &cxl_namespace_uuid));
  950. return 0;
  951. }