label.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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. dev_dbg(ndd->dev, "failed to init config data area: (%u:%u)\n",
  382. ndd->nsarea.max_xfer, ndd->nsarea.config_size);
  383. return -ENXIO;
  384. }
  385. /*
  386. * We need to determine the maximum index area as this is the section
  387. * we must read and validate before we can start processing labels.
  388. *
  389. * If the area is too small to contain the two indexes and 2 labels
  390. * then we abort.
  391. *
  392. * Start at a label size of 128 as this should result in the largest
  393. * possible namespace index size.
  394. */
  395. ndd->nslabel_size = 128;
  396. read_size = sizeof_namespace_index(ndd) * 2;
  397. if (!read_size)
  398. return -ENXIO;
  399. /* Allocate config data */
  400. config_size = ndd->nsarea.config_size;
  401. ndd->data = kvzalloc(config_size, GFP_KERNEL);
  402. if (!ndd->data)
  403. return -ENOMEM;
  404. /*
  405. * We want to guarantee as few reads as possible while conserving
  406. * memory. To do that we figure out how much unused space will be left
  407. * in the last read, divide that by the total number of reads it is
  408. * going to take given our maximum transfer size, and then reduce our
  409. * maximum transfer size based on that result.
  410. */
  411. max_xfer = min_t(size_t, ndd->nsarea.max_xfer, config_size);
  412. if (read_size < max_xfer) {
  413. /* trim waste */
  414. max_xfer -= ((max_xfer - 1) - (config_size - 1) % max_xfer) /
  415. DIV_ROUND_UP(config_size, max_xfer);
  416. /* make certain we read indexes in exactly 1 read */
  417. if (max_xfer < read_size)
  418. max_xfer = read_size;
  419. }
  420. /* Make our initial read size a multiple of max_xfer size */
  421. read_size = min(DIV_ROUND_UP(read_size, max_xfer) * max_xfer,
  422. config_size);
  423. /* Read the index data */
  424. rc = nvdimm_get_config_data(ndd, ndd->data, 0, read_size);
  425. if (rc)
  426. goto out_err;
  427. /* Validate index data, if not valid assume all labels are invalid */
  428. ndd->ns_current = nd_label_validate(ndd);
  429. if (ndd->ns_current < 0)
  430. return 0;
  431. /* Record our index values */
  432. ndd->ns_next = nd_label_next_nsindex(ndd->ns_current);
  433. /* Copy "current" index on top of the "next" index */
  434. nsindex = to_current_namespace_index(ndd);
  435. nd_label_copy(ndd, to_next_namespace_index(ndd), nsindex);
  436. /* Determine starting offset for label data */
  437. offset = __le64_to_cpu(nsindex->labeloff);
  438. nslot = __le32_to_cpu(nsindex->nslot);
  439. /* Loop through the free list pulling in any active labels */
  440. for (i = 0; i < nslot; i++, offset += ndd->nslabel_size) {
  441. size_t label_read_size;
  442. /* zero out the unused labels */
  443. if (test_bit_le(i, nsindex->free)) {
  444. memset(ndd->data + offset, 0, ndd->nslabel_size);
  445. continue;
  446. }
  447. /* if we already read past here then just continue */
  448. if (offset + ndd->nslabel_size <= read_size)
  449. continue;
  450. /* if we haven't read in a while reset our read_size offset */
  451. if (read_size < offset)
  452. read_size = offset;
  453. /* determine how much more will be read after this next call. */
  454. label_read_size = offset + ndd->nslabel_size - read_size;
  455. label_read_size = DIV_ROUND_UP(label_read_size, max_xfer) *
  456. max_xfer;
  457. /* truncate last read if needed */
  458. if (read_size + label_read_size > config_size)
  459. label_read_size = config_size - read_size;
  460. /* Read the label data */
  461. rc = nvdimm_get_config_data(ndd, ndd->data + read_size,
  462. read_size, label_read_size);
  463. if (rc)
  464. goto out_err;
  465. /* push read_size to next read offset */
  466. read_size += label_read_size;
  467. }
  468. dev_dbg(ndd->dev, "len: %zu rc: %d\n", offset, rc);
  469. out_err:
  470. return rc;
  471. }
  472. int nd_label_active_count(struct nvdimm_drvdata *ndd)
  473. {
  474. struct nd_namespace_index *nsindex;
  475. unsigned long *free;
  476. u32 nslot, slot;
  477. int count = 0;
  478. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  479. return 0;
  480. for_each_clear_bit_le(slot, free, nslot) {
  481. struct nd_namespace_label *nd_label;
  482. nd_label = to_label(ndd, slot);
  483. if (!slot_valid(ndd, nd_label, slot)) {
  484. u32 label_slot = nsl_get_slot(ndd, nd_label);
  485. u64 size = nsl_get_rawsize(ndd, nd_label);
  486. u64 dpa = nsl_get_dpa(ndd, nd_label);
  487. dev_dbg(ndd->dev,
  488. "slot%d invalid slot: %d dpa: %llx size: %llx\n",
  489. slot, label_slot, dpa, size);
  490. continue;
  491. }
  492. count++;
  493. }
  494. return count;
  495. }
  496. struct nd_namespace_label *nd_label_active(struct nvdimm_drvdata *ndd, int n)
  497. {
  498. struct nd_namespace_index *nsindex;
  499. unsigned long *free;
  500. u32 nslot, slot;
  501. if (!preamble_current(ndd, &nsindex, &free, &nslot))
  502. return NULL;
  503. for_each_clear_bit_le(slot, free, nslot) {
  504. struct nd_namespace_label *nd_label;
  505. nd_label = to_label(ndd, slot);
  506. if (!slot_valid(ndd, nd_label, slot))
  507. continue;
  508. if (n-- == 0)
  509. return to_label(ndd, slot);
  510. }
  511. return NULL;
  512. }
  513. u32 nd_label_alloc_slot(struct nvdimm_drvdata *ndd)
  514. {
  515. struct nd_namespace_index *nsindex;
  516. unsigned long *free;
  517. u32 nslot, slot;
  518. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  519. return UINT_MAX;
  520. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  521. slot = find_next_bit_le(free, nslot, 0);
  522. if (slot == nslot)
  523. return UINT_MAX;
  524. clear_bit_le(slot, free);
  525. return slot;
  526. }
  527. bool nd_label_free_slot(struct nvdimm_drvdata *ndd, u32 slot)
  528. {
  529. struct nd_namespace_index *nsindex;
  530. unsigned long *free;
  531. u32 nslot;
  532. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  533. return false;
  534. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  535. if (slot < nslot)
  536. return !test_and_set_bit_le(slot, free);
  537. return false;
  538. }
  539. u32 nd_label_nfree(struct nvdimm_drvdata *ndd)
  540. {
  541. struct nd_namespace_index *nsindex;
  542. unsigned long *free;
  543. u32 nslot;
  544. WARN_ON(!is_nvdimm_bus_locked(ndd->dev));
  545. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  546. return nvdimm_num_label_slots(ndd);
  547. return bitmap_weight(free, nslot);
  548. }
  549. static int nd_label_write_index(struct nvdimm_drvdata *ndd, int index, u32 seq,
  550. unsigned long flags)
  551. {
  552. struct nd_namespace_index *nsindex;
  553. unsigned long offset;
  554. u64 checksum;
  555. u32 nslot;
  556. int rc;
  557. nsindex = to_namespace_index(ndd, index);
  558. if (flags & ND_NSINDEX_INIT)
  559. nslot = nvdimm_num_label_slots(ndd);
  560. else
  561. nslot = __le32_to_cpu(nsindex->nslot);
  562. memcpy(nsindex->sig, NSINDEX_SIGNATURE, NSINDEX_SIG_LEN);
  563. memset(&nsindex->flags, 0, 3);
  564. nsindex->labelsize = sizeof_namespace_label(ndd) >> 8;
  565. nsindex->seq = __cpu_to_le32(seq);
  566. offset = (unsigned long) nsindex
  567. - (unsigned long) to_namespace_index(ndd, 0);
  568. nsindex->myoff = __cpu_to_le64(offset);
  569. nsindex->mysize = __cpu_to_le64(sizeof_namespace_index(ndd));
  570. offset = (unsigned long) to_namespace_index(ndd,
  571. nd_label_next_nsindex(index))
  572. - (unsigned long) to_namespace_index(ndd, 0);
  573. nsindex->otheroff = __cpu_to_le64(offset);
  574. offset = (unsigned long) nd_label_base(ndd)
  575. - (unsigned long) to_namespace_index(ndd, 0);
  576. nsindex->labeloff = __cpu_to_le64(offset);
  577. nsindex->nslot = __cpu_to_le32(nslot);
  578. nsindex->major = __cpu_to_le16(1);
  579. if (sizeof_namespace_label(ndd) < 256)
  580. nsindex->minor = __cpu_to_le16(1);
  581. else
  582. nsindex->minor = __cpu_to_le16(2);
  583. nsindex->checksum = __cpu_to_le64(0);
  584. if (flags & ND_NSINDEX_INIT) {
  585. unsigned long *free = (unsigned long *) nsindex->free;
  586. u32 nfree = ALIGN(nslot, BITS_PER_LONG);
  587. int last_bits, i;
  588. memset(nsindex->free, 0xff, nfree / 8);
  589. for (i = 0, last_bits = nfree - nslot; i < last_bits; i++)
  590. clear_bit_le(nslot + i, free);
  591. }
  592. checksum = nd_fletcher64(nsindex, sizeof_namespace_index(ndd), 1);
  593. nsindex->checksum = __cpu_to_le64(checksum);
  594. rc = nvdimm_set_config_data(ndd, __le64_to_cpu(nsindex->myoff),
  595. nsindex, sizeof_namespace_index(ndd));
  596. if (rc < 0)
  597. return rc;
  598. if (flags & ND_NSINDEX_INIT)
  599. return 0;
  600. /* copy the index we just wrote to the new 'next' */
  601. WARN_ON(index != ndd->ns_next);
  602. nd_label_copy(ndd, to_current_namespace_index(ndd), nsindex);
  603. ndd->ns_current = nd_label_next_nsindex(ndd->ns_current);
  604. ndd->ns_next = nd_label_next_nsindex(ndd->ns_next);
  605. WARN_ON(ndd->ns_current == ndd->ns_next);
  606. return 0;
  607. }
  608. static unsigned long nd_label_offset(struct nvdimm_drvdata *ndd,
  609. struct nd_namespace_label *nd_label)
  610. {
  611. return (unsigned long) nd_label
  612. - (unsigned long) to_namespace_index(ndd, 0);
  613. }
  614. static enum nvdimm_claim_class guid_to_nvdimm_cclass(guid_t *guid)
  615. {
  616. if (guid_equal(guid, &nvdimm_btt_guid))
  617. return NVDIMM_CCLASS_BTT;
  618. else if (guid_equal(guid, &nvdimm_btt2_guid))
  619. return NVDIMM_CCLASS_BTT2;
  620. else if (guid_equal(guid, &nvdimm_pfn_guid))
  621. return NVDIMM_CCLASS_PFN;
  622. else if (guid_equal(guid, &nvdimm_dax_guid))
  623. return NVDIMM_CCLASS_DAX;
  624. else if (guid_equal(guid, &guid_null))
  625. return NVDIMM_CCLASS_NONE;
  626. return NVDIMM_CCLASS_UNKNOWN;
  627. }
  628. /* CXL labels store UUIDs instead of GUIDs for the same data */
  629. static enum nvdimm_claim_class uuid_to_nvdimm_cclass(uuid_t *uuid)
  630. {
  631. if (uuid_equal(uuid, &nvdimm_btt_uuid))
  632. return NVDIMM_CCLASS_BTT;
  633. else if (uuid_equal(uuid, &nvdimm_btt2_uuid))
  634. return NVDIMM_CCLASS_BTT2;
  635. else if (uuid_equal(uuid, &nvdimm_pfn_uuid))
  636. return NVDIMM_CCLASS_PFN;
  637. else if (uuid_equal(uuid, &nvdimm_dax_uuid))
  638. return NVDIMM_CCLASS_DAX;
  639. else if (uuid_equal(uuid, &uuid_null))
  640. return NVDIMM_CCLASS_NONE;
  641. return NVDIMM_CCLASS_UNKNOWN;
  642. }
  643. static const guid_t *to_abstraction_guid(enum nvdimm_claim_class claim_class,
  644. guid_t *target)
  645. {
  646. if (claim_class == NVDIMM_CCLASS_BTT)
  647. return &nvdimm_btt_guid;
  648. else if (claim_class == NVDIMM_CCLASS_BTT2)
  649. return &nvdimm_btt2_guid;
  650. else if (claim_class == NVDIMM_CCLASS_PFN)
  651. return &nvdimm_pfn_guid;
  652. else if (claim_class == NVDIMM_CCLASS_DAX)
  653. return &nvdimm_dax_guid;
  654. else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
  655. /*
  656. * If we're modifying a namespace for which we don't
  657. * know the claim_class, don't touch the existing guid.
  658. */
  659. return target;
  660. } else
  661. return &guid_null;
  662. }
  663. /* CXL labels store UUIDs instead of GUIDs for the same data */
  664. static const uuid_t *to_abstraction_uuid(enum nvdimm_claim_class claim_class,
  665. uuid_t *target)
  666. {
  667. if (claim_class == NVDIMM_CCLASS_BTT)
  668. return &nvdimm_btt_uuid;
  669. else if (claim_class == NVDIMM_CCLASS_BTT2)
  670. return &nvdimm_btt2_uuid;
  671. else if (claim_class == NVDIMM_CCLASS_PFN)
  672. return &nvdimm_pfn_uuid;
  673. else if (claim_class == NVDIMM_CCLASS_DAX)
  674. return &nvdimm_dax_uuid;
  675. else if (claim_class == NVDIMM_CCLASS_UNKNOWN) {
  676. /*
  677. * If we're modifying a namespace for which we don't
  678. * know the claim_class, don't touch the existing uuid.
  679. */
  680. return target;
  681. } else
  682. return &uuid_null;
  683. }
  684. static void reap_victim(struct nd_mapping *nd_mapping,
  685. struct nd_label_ent *victim)
  686. {
  687. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  688. u32 slot = to_slot(ndd, victim->label);
  689. dev_dbg(ndd->dev, "free: %d\n", slot);
  690. nd_label_free_slot(ndd, slot);
  691. victim->label = NULL;
  692. }
  693. static void nsl_set_type_guid(struct nvdimm_drvdata *ndd,
  694. struct nd_namespace_label *nd_label, guid_t *guid)
  695. {
  696. if (efi_namespace_label_has(ndd, type_guid))
  697. guid_copy(&nd_label->efi.type_guid, guid);
  698. }
  699. bool nsl_validate_type_guid(struct nvdimm_drvdata *ndd,
  700. struct nd_namespace_label *nd_label, guid_t *guid)
  701. {
  702. if (ndd->cxl || !efi_namespace_label_has(ndd, type_guid))
  703. return true;
  704. if (!guid_equal(&nd_label->efi.type_guid, guid)) {
  705. dev_dbg(ndd->dev, "expect type_guid %pUb got %pUb\n", guid,
  706. &nd_label->efi.type_guid);
  707. return false;
  708. }
  709. return true;
  710. }
  711. static void nsl_set_claim_class(struct nvdimm_drvdata *ndd,
  712. struct nd_namespace_label *nd_label,
  713. enum nvdimm_claim_class claim_class)
  714. {
  715. if (ndd->cxl) {
  716. uuid_t uuid;
  717. import_uuid(&uuid, nd_label->cxl.abstraction_uuid);
  718. export_uuid(nd_label->cxl.abstraction_uuid,
  719. to_abstraction_uuid(claim_class, &uuid));
  720. return;
  721. }
  722. if (!efi_namespace_label_has(ndd, abstraction_guid))
  723. return;
  724. guid_copy(&nd_label->efi.abstraction_guid,
  725. to_abstraction_guid(claim_class,
  726. &nd_label->efi.abstraction_guid));
  727. }
  728. enum nvdimm_claim_class nsl_get_claim_class(struct nvdimm_drvdata *ndd,
  729. struct nd_namespace_label *nd_label)
  730. {
  731. if (ndd->cxl) {
  732. uuid_t uuid;
  733. import_uuid(&uuid, nd_label->cxl.abstraction_uuid);
  734. return uuid_to_nvdimm_cclass(&uuid);
  735. }
  736. if (!efi_namespace_label_has(ndd, abstraction_guid))
  737. return NVDIMM_CCLASS_NONE;
  738. return guid_to_nvdimm_cclass(&nd_label->efi.abstraction_guid);
  739. }
  740. static int __pmem_label_update(struct nd_region *nd_region,
  741. struct nd_mapping *nd_mapping, struct nd_namespace_pmem *nspm,
  742. int pos, unsigned long flags)
  743. {
  744. struct nd_namespace_common *ndns = &nspm->nsio.common;
  745. struct nd_interleave_set *nd_set = nd_region->nd_set;
  746. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  747. struct nd_namespace_label *nd_label;
  748. struct nd_namespace_index *nsindex;
  749. struct nd_label_ent *label_ent;
  750. struct nd_label_id label_id;
  751. struct resource *res;
  752. unsigned long *free;
  753. u32 nslot, slot;
  754. size_t offset;
  755. u64 cookie;
  756. int rc;
  757. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  758. return -ENXIO;
  759. cookie = nd_region_interleave_set_cookie(nd_region, nsindex);
  760. nd_label_gen_id(&label_id, nspm->uuid, 0);
  761. for_each_dpa_resource(ndd, res)
  762. if (strcmp(res->name, label_id.id) == 0)
  763. break;
  764. if (!res) {
  765. WARN_ON_ONCE(1);
  766. return -ENXIO;
  767. }
  768. /* allocate and write the label to the staging (next) index */
  769. slot = nd_label_alloc_slot(ndd);
  770. if (slot == UINT_MAX)
  771. return -ENXIO;
  772. dev_dbg(ndd->dev, "allocated: %d\n", slot);
  773. nd_label = to_label(ndd, slot);
  774. memset(nd_label, 0, sizeof_namespace_label(ndd));
  775. nsl_set_uuid(ndd, nd_label, nspm->uuid);
  776. nsl_set_name(ndd, nd_label, nspm->alt_name);
  777. nsl_set_flags(ndd, nd_label, flags);
  778. nsl_set_nlabel(ndd, nd_label, nd_region->ndr_mappings);
  779. nsl_set_nrange(ndd, nd_label, 1);
  780. nsl_set_position(ndd, nd_label, pos);
  781. nsl_set_isetcookie(ndd, nd_label, cookie);
  782. nsl_set_rawsize(ndd, nd_label, resource_size(res));
  783. nsl_set_lbasize(ndd, nd_label, nspm->lbasize);
  784. nsl_set_dpa(ndd, nd_label, res->start);
  785. nsl_set_slot(ndd, nd_label, slot);
  786. nsl_set_type_guid(ndd, nd_label, &nd_set->type_guid);
  787. nsl_set_claim_class(ndd, nd_label, ndns->claim_class);
  788. nsl_calculate_checksum(ndd, nd_label);
  789. nd_dbg_dpa(nd_region, ndd, res, "\n");
  790. /* update label */
  791. offset = nd_label_offset(ndd, nd_label);
  792. rc = nvdimm_set_config_data(ndd, offset, nd_label,
  793. sizeof_namespace_label(ndd));
  794. if (rc < 0)
  795. return rc;
  796. /* Garbage collect the previous label */
  797. mutex_lock(&nd_mapping->lock);
  798. list_for_each_entry(label_ent, &nd_mapping->labels, list) {
  799. if (!label_ent->label)
  800. continue;
  801. if (test_and_clear_bit(ND_LABEL_REAP, &label_ent->flags) ||
  802. nsl_uuid_equal(ndd, label_ent->label, nspm->uuid))
  803. reap_victim(nd_mapping, label_ent);
  804. }
  805. /* update index */
  806. rc = nd_label_write_index(ndd, ndd->ns_next,
  807. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  808. if (rc == 0) {
  809. list_for_each_entry(label_ent, &nd_mapping->labels, list)
  810. if (!label_ent->label) {
  811. label_ent->label = nd_label;
  812. nd_label = NULL;
  813. break;
  814. }
  815. dev_WARN_ONCE(&nspm->nsio.common.dev, nd_label,
  816. "failed to track label: %d\n",
  817. to_slot(ndd, nd_label));
  818. if (nd_label)
  819. rc = -ENXIO;
  820. }
  821. mutex_unlock(&nd_mapping->lock);
  822. return rc;
  823. }
  824. static int init_labels(struct nd_mapping *nd_mapping, int num_labels)
  825. {
  826. int i, old_num_labels = 0;
  827. struct nd_label_ent *label_ent;
  828. struct nd_namespace_index *nsindex;
  829. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  830. mutex_lock(&nd_mapping->lock);
  831. list_for_each_entry(label_ent, &nd_mapping->labels, list)
  832. old_num_labels++;
  833. mutex_unlock(&nd_mapping->lock);
  834. /*
  835. * We need to preserve all the old labels for the mapping so
  836. * they can be garbage collected after writing the new labels.
  837. */
  838. for (i = old_num_labels; i < num_labels; i++) {
  839. label_ent = kzalloc(sizeof(*label_ent), GFP_KERNEL);
  840. if (!label_ent)
  841. return -ENOMEM;
  842. mutex_lock(&nd_mapping->lock);
  843. list_add_tail(&label_ent->list, &nd_mapping->labels);
  844. mutex_unlock(&nd_mapping->lock);
  845. }
  846. if (ndd->ns_current == -1 || ndd->ns_next == -1)
  847. /* pass */;
  848. else
  849. return max(num_labels, old_num_labels);
  850. nsindex = to_namespace_index(ndd, 0);
  851. memset(nsindex, 0, ndd->nsarea.config_size);
  852. for (i = 0; i < 2; i++) {
  853. int rc = nd_label_write_index(ndd, i, 3 - i, ND_NSINDEX_INIT);
  854. if (rc)
  855. return rc;
  856. }
  857. ndd->ns_next = 1;
  858. ndd->ns_current = 0;
  859. return max(num_labels, old_num_labels);
  860. }
  861. static int del_labels(struct nd_mapping *nd_mapping, uuid_t *uuid)
  862. {
  863. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  864. struct nd_label_ent *label_ent, *e;
  865. struct nd_namespace_index *nsindex;
  866. unsigned long *free;
  867. LIST_HEAD(list);
  868. u32 nslot, slot;
  869. int active = 0;
  870. if (!uuid)
  871. return 0;
  872. /* no index || no labels == nothing to delete */
  873. if (!preamble_next(ndd, &nsindex, &free, &nslot))
  874. return 0;
  875. mutex_lock(&nd_mapping->lock);
  876. list_for_each_entry_safe(label_ent, e, &nd_mapping->labels, list) {
  877. struct nd_namespace_label *nd_label = label_ent->label;
  878. if (!nd_label)
  879. continue;
  880. active++;
  881. if (!nsl_uuid_equal(ndd, nd_label, uuid))
  882. continue;
  883. active--;
  884. slot = to_slot(ndd, nd_label);
  885. nd_label_free_slot(ndd, slot);
  886. dev_dbg(ndd->dev, "free: %d\n", slot);
  887. list_move_tail(&label_ent->list, &list);
  888. label_ent->label = NULL;
  889. }
  890. list_splice_tail_init(&list, &nd_mapping->labels);
  891. if (active == 0) {
  892. nd_mapping_free_labels(nd_mapping);
  893. dev_dbg(ndd->dev, "no more active labels\n");
  894. }
  895. mutex_unlock(&nd_mapping->lock);
  896. return nd_label_write_index(ndd, ndd->ns_next,
  897. nd_inc_seq(__le32_to_cpu(nsindex->seq)), 0);
  898. }
  899. int nd_pmem_namespace_label_update(struct nd_region *nd_region,
  900. struct nd_namespace_pmem *nspm, resource_size_t size)
  901. {
  902. int i, rc;
  903. for (i = 0; i < nd_region->ndr_mappings; i++) {
  904. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  905. struct nvdimm_drvdata *ndd = to_ndd(nd_mapping);
  906. struct resource *res;
  907. int count = 0;
  908. if (size == 0) {
  909. rc = del_labels(nd_mapping, nspm->uuid);
  910. if (rc)
  911. return rc;
  912. continue;
  913. }
  914. for_each_dpa_resource(ndd, res)
  915. if (strncmp(res->name, "pmem", 4) == 0)
  916. count++;
  917. WARN_ON_ONCE(!count);
  918. rc = init_labels(nd_mapping, count);
  919. if (rc < 0)
  920. return rc;
  921. rc = __pmem_label_update(nd_region, nd_mapping, nspm, i,
  922. NSLABEL_FLAG_UPDATING);
  923. if (rc)
  924. return rc;
  925. }
  926. if (size == 0)
  927. return 0;
  928. /* Clear the UPDATING flag per UEFI 2.7 expectations */
  929. for (i = 0; i < nd_region->ndr_mappings; i++) {
  930. struct nd_mapping *nd_mapping = &nd_region->mapping[i];
  931. rc = __pmem_label_update(nd_region, nd_mapping, nspm, i, 0);
  932. if (rc)
  933. return rc;
  934. }
  935. return 0;
  936. }
  937. int __init nd_label_init(void)
  938. {
  939. WARN_ON(guid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_guid));
  940. WARN_ON(guid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_guid));
  941. WARN_ON(guid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_guid));
  942. WARN_ON(guid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_guid));
  943. WARN_ON(uuid_parse(NVDIMM_BTT_GUID, &nvdimm_btt_uuid));
  944. WARN_ON(uuid_parse(NVDIMM_BTT2_GUID, &nvdimm_btt2_uuid));
  945. WARN_ON(uuid_parse(NVDIMM_PFN_GUID, &nvdimm_pfn_uuid));
  946. WARN_ON(uuid_parse(NVDIMM_DAX_GUID, &nvdimm_dax_uuid));
  947. WARN_ON(uuid_parse(CXL_REGION_UUID, &cxl_region_uuid));
  948. WARN_ON(uuid_parse(CXL_NAMESPACE_UUID, &cxl_namespace_uuid));
  949. return 0;
  950. }