vlun.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /*
  2. * CXL Flash Device Driver
  3. *
  4. * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
  5. * Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
  6. *
  7. * Copyright (C) 2015 IBM Corporation
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; either version
  12. * 2 of the License, or (at your option) any later version.
  13. */
  14. #include <linux/interrupt.h>
  15. #include <linux/pci.h>
  16. #include <linux/syscalls.h>
  17. #include <asm/unaligned.h>
  18. #include <asm/bitsperlong.h>
  19. #include <scsi/scsi_cmnd.h>
  20. #include <scsi/scsi_host.h>
  21. #include <uapi/scsi/cxlflash_ioctl.h>
  22. #include "sislite.h"
  23. #include "common.h"
  24. #include "vlun.h"
  25. #include "superpipe.h"
  26. /**
  27. * marshal_virt_to_resize() - translate uvirtual to resize structure
  28. * @virt: Source structure from which to translate/copy.
  29. * @resize: Destination structure for the translate/copy.
  30. */
  31. static void marshal_virt_to_resize(struct dk_cxlflash_uvirtual *virt,
  32. struct dk_cxlflash_resize *resize)
  33. {
  34. resize->hdr = virt->hdr;
  35. resize->context_id = virt->context_id;
  36. resize->rsrc_handle = virt->rsrc_handle;
  37. resize->req_size = virt->lun_size;
  38. resize->last_lba = virt->last_lba;
  39. }
  40. /**
  41. * marshal_clone_to_rele() - translate clone to release structure
  42. * @clone: Source structure from which to translate/copy.
  43. * @rele: Destination structure for the translate/copy.
  44. */
  45. static void marshal_clone_to_rele(struct dk_cxlflash_clone *clone,
  46. struct dk_cxlflash_release *release)
  47. {
  48. release->hdr = clone->hdr;
  49. release->context_id = clone->context_id_dst;
  50. }
  51. /**
  52. * ba_init() - initializes a block allocator
  53. * @ba_lun: Block allocator to initialize.
  54. *
  55. * Return: 0 on success, -errno on failure
  56. */
  57. static int ba_init(struct ba_lun *ba_lun)
  58. {
  59. struct ba_lun_info *bali = NULL;
  60. int lun_size_au = 0, i = 0;
  61. int last_word_underflow = 0;
  62. u64 *lam;
  63. pr_debug("%s: Initializing LUN: lun_id=%016llx "
  64. "ba_lun->lsize=%lx ba_lun->au_size=%lX\n",
  65. __func__, ba_lun->lun_id, ba_lun->lsize, ba_lun->au_size);
  66. /* Calculate bit map size */
  67. lun_size_au = ba_lun->lsize / ba_lun->au_size;
  68. if (lun_size_au == 0) {
  69. pr_debug("%s: Requested LUN size of 0!\n", __func__);
  70. return -EINVAL;
  71. }
  72. /* Allocate lun information container */
  73. bali = kzalloc(sizeof(struct ba_lun_info), GFP_KERNEL);
  74. if (unlikely(!bali)) {
  75. pr_err("%s: Failed to allocate lun_info lun_id=%016llx\n",
  76. __func__, ba_lun->lun_id);
  77. return -ENOMEM;
  78. }
  79. bali->total_aus = lun_size_au;
  80. bali->lun_bmap_size = lun_size_au / BITS_PER_LONG;
  81. if (lun_size_au % BITS_PER_LONG)
  82. bali->lun_bmap_size++;
  83. /* Allocate bitmap space */
  84. bali->lun_alloc_map = kzalloc((bali->lun_bmap_size * sizeof(u64)),
  85. GFP_KERNEL);
  86. if (unlikely(!bali->lun_alloc_map)) {
  87. pr_err("%s: Failed to allocate lun allocation map: "
  88. "lun_id=%016llx\n", __func__, ba_lun->lun_id);
  89. kfree(bali);
  90. return -ENOMEM;
  91. }
  92. /* Initialize the bit map size and set all bits to '1' */
  93. bali->free_aun_cnt = lun_size_au;
  94. for (i = 0; i < bali->lun_bmap_size; i++)
  95. bali->lun_alloc_map[i] = 0xFFFFFFFFFFFFFFFFULL;
  96. /* If the last word not fully utilized, mark extra bits as allocated */
  97. last_word_underflow = (bali->lun_bmap_size * BITS_PER_LONG);
  98. last_word_underflow -= bali->free_aun_cnt;
  99. if (last_word_underflow > 0) {
  100. lam = &bali->lun_alloc_map[bali->lun_bmap_size - 1];
  101. for (i = (HIBIT - last_word_underflow + 1);
  102. i < BITS_PER_LONG;
  103. i++)
  104. clear_bit(i, (ulong *)lam);
  105. }
  106. /* Initialize high elevator index, low/curr already at 0 from kzalloc */
  107. bali->free_high_idx = bali->lun_bmap_size;
  108. /* Allocate clone map */
  109. bali->aun_clone_map = kzalloc((bali->total_aus * sizeof(u8)),
  110. GFP_KERNEL);
  111. if (unlikely(!bali->aun_clone_map)) {
  112. pr_err("%s: Failed to allocate clone map: lun_id=%016llx\n",
  113. __func__, ba_lun->lun_id);
  114. kfree(bali->lun_alloc_map);
  115. kfree(bali);
  116. return -ENOMEM;
  117. }
  118. /* Pass the allocated LUN info as a handle to the user */
  119. ba_lun->ba_lun_handle = bali;
  120. pr_debug("%s: Successfully initialized the LUN: "
  121. "lun_id=%016llx bitmap size=%x, free_aun_cnt=%llx\n",
  122. __func__, ba_lun->lun_id, bali->lun_bmap_size,
  123. bali->free_aun_cnt);
  124. return 0;
  125. }
  126. /**
  127. * find_free_range() - locates a free bit within the block allocator
  128. * @low: First word in block allocator to start search.
  129. * @high: Last word in block allocator to search.
  130. * @bali: LUN information structure owning the block allocator to search.
  131. * @bit_word: Passes back the word in the block allocator owning the free bit.
  132. *
  133. * Return: The bit position within the passed back word, -1 on failure
  134. */
  135. static int find_free_range(u32 low,
  136. u32 high,
  137. struct ba_lun_info *bali, int *bit_word)
  138. {
  139. int i;
  140. u64 bit_pos = -1;
  141. ulong *lam, num_bits;
  142. for (i = low; i < high; i++)
  143. if (bali->lun_alloc_map[i] != 0) {
  144. lam = (ulong *)&bali->lun_alloc_map[i];
  145. num_bits = (sizeof(*lam) * BITS_PER_BYTE);
  146. bit_pos = find_first_bit(lam, num_bits);
  147. pr_devel("%s: Found free bit %llu in LUN "
  148. "map entry %016llx at bitmap index = %d\n",
  149. __func__, bit_pos, bali->lun_alloc_map[i], i);
  150. *bit_word = i;
  151. bali->free_aun_cnt--;
  152. clear_bit(bit_pos, lam);
  153. break;
  154. }
  155. return bit_pos;
  156. }
  157. /**
  158. * ba_alloc() - allocates a block from the block allocator
  159. * @ba_lun: Block allocator from which to allocate a block.
  160. *
  161. * Return: The allocated block, -1 on failure
  162. */
  163. static u64 ba_alloc(struct ba_lun *ba_lun)
  164. {
  165. u64 bit_pos = -1;
  166. int bit_word = 0;
  167. struct ba_lun_info *bali = NULL;
  168. bali = ba_lun->ba_lun_handle;
  169. pr_debug("%s: Received block allocation request: "
  170. "lun_id=%016llx free_aun_cnt=%llx\n",
  171. __func__, ba_lun->lun_id, bali->free_aun_cnt);
  172. if (bali->free_aun_cnt == 0) {
  173. pr_debug("%s: No space left on LUN: lun_id=%016llx\n",
  174. __func__, ba_lun->lun_id);
  175. return -1ULL;
  176. }
  177. /* Search to find a free entry, curr->high then low->curr */
  178. bit_pos = find_free_range(bali->free_curr_idx,
  179. bali->free_high_idx, bali, &bit_word);
  180. if (bit_pos == -1) {
  181. bit_pos = find_free_range(bali->free_low_idx,
  182. bali->free_curr_idx,
  183. bali, &bit_word);
  184. if (bit_pos == -1) {
  185. pr_debug("%s: Could not find an allocation unit on LUN:"
  186. " lun_id=%016llx\n", __func__, ba_lun->lun_id);
  187. return -1ULL;
  188. }
  189. }
  190. /* Update the free_curr_idx */
  191. if (bit_pos == HIBIT)
  192. bali->free_curr_idx = bit_word + 1;
  193. else
  194. bali->free_curr_idx = bit_word;
  195. pr_debug("%s: Allocating AU number=%llx lun_id=%016llx "
  196. "free_aun_cnt=%llx\n", __func__,
  197. ((bit_word * BITS_PER_LONG) + bit_pos), ba_lun->lun_id,
  198. bali->free_aun_cnt);
  199. return (u64) ((bit_word * BITS_PER_LONG) + bit_pos);
  200. }
  201. /**
  202. * validate_alloc() - validates the specified block has been allocated
  203. * @ba_lun_info: LUN info owning the block allocator.
  204. * @aun: Block to validate.
  205. *
  206. * Return: 0 on success, -1 on failure
  207. */
  208. static int validate_alloc(struct ba_lun_info *bali, u64 aun)
  209. {
  210. int idx = 0, bit_pos = 0;
  211. idx = aun / BITS_PER_LONG;
  212. bit_pos = aun % BITS_PER_LONG;
  213. if (test_bit(bit_pos, (ulong *)&bali->lun_alloc_map[idx]))
  214. return -1;
  215. return 0;
  216. }
  217. /**
  218. * ba_free() - frees a block from the block allocator
  219. * @ba_lun: Block allocator from which to allocate a block.
  220. * @to_free: Block to free.
  221. *
  222. * Return: 0 on success, -1 on failure
  223. */
  224. static int ba_free(struct ba_lun *ba_lun, u64 to_free)
  225. {
  226. int idx = 0, bit_pos = 0;
  227. struct ba_lun_info *bali = NULL;
  228. bali = ba_lun->ba_lun_handle;
  229. if (validate_alloc(bali, to_free)) {
  230. pr_debug("%s: AUN %llx is not allocated on lun_id=%016llx\n",
  231. __func__, to_free, ba_lun->lun_id);
  232. return -1;
  233. }
  234. pr_debug("%s: Received a request to free AU=%llx lun_id=%016llx "
  235. "free_aun_cnt=%llx\n", __func__, to_free, ba_lun->lun_id,
  236. bali->free_aun_cnt);
  237. if (bali->aun_clone_map[to_free] > 0) {
  238. pr_debug("%s: AUN %llx lun_id=%016llx cloned. Clone count=%x\n",
  239. __func__, to_free, ba_lun->lun_id,
  240. bali->aun_clone_map[to_free]);
  241. bali->aun_clone_map[to_free]--;
  242. return 0;
  243. }
  244. idx = to_free / BITS_PER_LONG;
  245. bit_pos = to_free % BITS_PER_LONG;
  246. set_bit(bit_pos, (ulong *)&bali->lun_alloc_map[idx]);
  247. bali->free_aun_cnt++;
  248. if (idx < bali->free_low_idx)
  249. bali->free_low_idx = idx;
  250. else if (idx > bali->free_high_idx)
  251. bali->free_high_idx = idx;
  252. pr_debug("%s: Successfully freed AU bit_pos=%x bit map index=%x "
  253. "lun_id=%016llx free_aun_cnt=%llx\n", __func__, bit_pos, idx,
  254. ba_lun->lun_id, bali->free_aun_cnt);
  255. return 0;
  256. }
  257. /**
  258. * ba_clone() - Clone a chunk of the block allocation table
  259. * @ba_lun: Block allocator from which to allocate a block.
  260. * @to_free: Block to free.
  261. *
  262. * Return: 0 on success, -1 on failure
  263. */
  264. static int ba_clone(struct ba_lun *ba_lun, u64 to_clone)
  265. {
  266. struct ba_lun_info *bali = ba_lun->ba_lun_handle;
  267. if (validate_alloc(bali, to_clone)) {
  268. pr_debug("%s: AUN=%llx not allocated on lun_id=%016llx\n",
  269. __func__, to_clone, ba_lun->lun_id);
  270. return -1;
  271. }
  272. pr_debug("%s: Received a request to clone AUN %llx on lun_id=%016llx\n",
  273. __func__, to_clone, ba_lun->lun_id);
  274. if (bali->aun_clone_map[to_clone] == MAX_AUN_CLONE_CNT) {
  275. pr_debug("%s: AUN %llx on lun_id=%016llx hit max clones already\n",
  276. __func__, to_clone, ba_lun->lun_id);
  277. return -1;
  278. }
  279. bali->aun_clone_map[to_clone]++;
  280. return 0;
  281. }
  282. /**
  283. * ba_space() - returns the amount of free space left in the block allocator
  284. * @ba_lun: Block allocator.
  285. *
  286. * Return: Amount of free space in block allocator
  287. */
  288. static u64 ba_space(struct ba_lun *ba_lun)
  289. {
  290. struct ba_lun_info *bali = ba_lun->ba_lun_handle;
  291. return bali->free_aun_cnt;
  292. }
  293. /**
  294. * cxlflash_ba_terminate() - frees resources associated with the block allocator
  295. * @ba_lun: Block allocator.
  296. *
  297. * Safe to call in a partially allocated state.
  298. */
  299. void cxlflash_ba_terminate(struct ba_lun *ba_lun)
  300. {
  301. struct ba_lun_info *bali = ba_lun->ba_lun_handle;
  302. if (bali) {
  303. kfree(bali->aun_clone_map);
  304. kfree(bali->lun_alloc_map);
  305. kfree(bali);
  306. ba_lun->ba_lun_handle = NULL;
  307. }
  308. }
  309. /**
  310. * init_vlun() - initializes a LUN for virtual use
  311. * @lun_info: LUN information structure that owns the block allocator.
  312. *
  313. * Return: 0 on success, -errno on failure
  314. */
  315. static int init_vlun(struct llun_info *lli)
  316. {
  317. int rc = 0;
  318. struct glun_info *gli = lli->parent;
  319. struct blka *blka = &gli->blka;
  320. memset(blka, 0, sizeof(*blka));
  321. mutex_init(&blka->mutex);
  322. /* LUN IDs are unique per port, save the index instead */
  323. blka->ba_lun.lun_id = lli->lun_index;
  324. blka->ba_lun.lsize = gli->max_lba + 1;
  325. blka->ba_lun.lba_size = gli->blk_len;
  326. blka->ba_lun.au_size = MC_CHUNK_SIZE;
  327. blka->nchunk = blka->ba_lun.lsize / MC_CHUNK_SIZE;
  328. rc = ba_init(&blka->ba_lun);
  329. if (unlikely(rc))
  330. pr_debug("%s: cannot init block_alloc, rc=%d\n", __func__, rc);
  331. pr_debug("%s: returning rc=%d lli=%p\n", __func__, rc, lli);
  332. return rc;
  333. }
  334. /**
  335. * write_same16() - sends a SCSI WRITE_SAME16 (0) command to specified LUN
  336. * @sdev: SCSI device associated with LUN.
  337. * @lba: Logical block address to start write same.
  338. * @nblks: Number of logical blocks to write same.
  339. *
  340. * The SCSI WRITE_SAME16 can take quite a while to complete. Should an EEH occur
  341. * while in scsi_execute(), the EEH handler will attempt to recover. As part of
  342. * the recovery, the handler drains all currently running ioctls, waiting until
  343. * they have completed before proceeding with a reset. As this routine is used
  344. * on the ioctl path, this can create a condition where the EEH handler becomes
  345. * stuck, infinitely waiting for this ioctl thread. To avoid this behavior,
  346. * temporarily unmark this thread as an ioctl thread by releasing the ioctl read
  347. * semaphore. This will allow the EEH handler to proceed with a recovery while
  348. * this thread is still running. Once the scsi_execute() returns, reacquire the
  349. * ioctl read semaphore and check the adapter state in case it changed while
  350. * inside of scsi_execute(). The state check will wait if the adapter is still
  351. * being recovered or return a failure if the recovery failed. In the event that
  352. * the adapter reset failed, simply return the failure as the ioctl would be
  353. * unable to continue.
  354. *
  355. * Note that the above puts a requirement on this routine to only be called on
  356. * an ioctl thread.
  357. *
  358. * Return: 0 on success, -errno on failure
  359. */
  360. static int write_same16(struct scsi_device *sdev,
  361. u64 lba,
  362. u32 nblks)
  363. {
  364. u8 *cmd_buf = NULL;
  365. u8 *scsi_cmd = NULL;
  366. int rc = 0;
  367. int result = 0;
  368. u64 offset = lba;
  369. int left = nblks;
  370. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  371. struct device *dev = &cfg->dev->dev;
  372. const u32 s = ilog2(sdev->sector_size) - 9;
  373. const u32 to = sdev->request_queue->rq_timeout;
  374. const u32 ws_limit = blk_queue_get_max_sectors(sdev->request_queue,
  375. REQ_OP_WRITE_SAME) >> s;
  376. cmd_buf = kzalloc(CMD_BUFSIZE, GFP_KERNEL);
  377. scsi_cmd = kzalloc(MAX_COMMAND_SIZE, GFP_KERNEL);
  378. if (unlikely(!cmd_buf || !scsi_cmd)) {
  379. rc = -ENOMEM;
  380. goto out;
  381. }
  382. while (left > 0) {
  383. scsi_cmd[0] = WRITE_SAME_16;
  384. scsi_cmd[1] = cfg->ws_unmap ? 0x8 : 0;
  385. put_unaligned_be64(offset, &scsi_cmd[2]);
  386. put_unaligned_be32(ws_limit < left ? ws_limit : left,
  387. &scsi_cmd[10]);
  388. /* Drop the ioctl read semahpore across lengthy call */
  389. up_read(&cfg->ioctl_rwsem);
  390. result = scsi_execute(sdev, scsi_cmd, DMA_TO_DEVICE, cmd_buf,
  391. CMD_BUFSIZE, NULL, NULL, to,
  392. CMD_RETRIES, 0, 0, NULL);
  393. down_read(&cfg->ioctl_rwsem);
  394. rc = check_state(cfg);
  395. if (rc) {
  396. dev_err(dev, "%s: Failed state result=%08x\n",
  397. __func__, result);
  398. rc = -ENODEV;
  399. goto out;
  400. }
  401. if (result) {
  402. dev_err_ratelimited(dev, "%s: command failed for "
  403. "offset=%lld result=%08x\n",
  404. __func__, offset, result);
  405. rc = -EIO;
  406. goto out;
  407. }
  408. left -= ws_limit;
  409. offset += ws_limit;
  410. }
  411. out:
  412. kfree(cmd_buf);
  413. kfree(scsi_cmd);
  414. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  415. return rc;
  416. }
  417. /**
  418. * grow_lxt() - expands the translation table associated with the specified RHTE
  419. * @afu: AFU associated with the host.
  420. * @sdev: SCSI device associated with LUN.
  421. * @ctxid: Context ID of context owning the RHTE.
  422. * @rhndl: Resource handle associated with the RHTE.
  423. * @rhte: Resource handle entry (RHTE).
  424. * @new_size: Number of translation entries associated with RHTE.
  425. *
  426. * By design, this routine employs a 'best attempt' allocation and will
  427. * truncate the requested size down if there is not sufficient space in
  428. * the block allocator to satisfy the request but there does exist some
  429. * amount of space. The user is made aware of this by returning the size
  430. * allocated.
  431. *
  432. * Return: 0 on success, -errno on failure
  433. */
  434. static int grow_lxt(struct afu *afu,
  435. struct scsi_device *sdev,
  436. ctx_hndl_t ctxid,
  437. res_hndl_t rhndl,
  438. struct sisl_rht_entry *rhte,
  439. u64 *new_size)
  440. {
  441. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  442. struct device *dev = &cfg->dev->dev;
  443. struct sisl_lxt_entry *lxt = NULL, *lxt_old = NULL;
  444. struct llun_info *lli = sdev->hostdata;
  445. struct glun_info *gli = lli->parent;
  446. struct blka *blka = &gli->blka;
  447. u32 av_size;
  448. u32 ngrps, ngrps_old;
  449. u64 aun; /* chunk# allocated by block allocator */
  450. u64 delta = *new_size - rhte->lxt_cnt;
  451. u64 my_new_size;
  452. int i, rc = 0;
  453. /*
  454. * Check what is available in the block allocator before re-allocating
  455. * LXT array. This is done up front under the mutex which must not be
  456. * released until after allocation is complete.
  457. */
  458. mutex_lock(&blka->mutex);
  459. av_size = ba_space(&blka->ba_lun);
  460. if (unlikely(av_size <= 0)) {
  461. dev_dbg(dev, "%s: ba_space error av_size=%d\n",
  462. __func__, av_size);
  463. mutex_unlock(&blka->mutex);
  464. rc = -ENOSPC;
  465. goto out;
  466. }
  467. if (av_size < delta)
  468. delta = av_size;
  469. lxt_old = rhte->lxt_start;
  470. ngrps_old = LXT_NUM_GROUPS(rhte->lxt_cnt);
  471. ngrps = LXT_NUM_GROUPS(rhte->lxt_cnt + delta);
  472. if (ngrps != ngrps_old) {
  473. /* reallocate to fit new size */
  474. lxt = kzalloc((sizeof(*lxt) * LXT_GROUP_SIZE * ngrps),
  475. GFP_KERNEL);
  476. if (unlikely(!lxt)) {
  477. mutex_unlock(&blka->mutex);
  478. rc = -ENOMEM;
  479. goto out;
  480. }
  481. /* copy over all old entries */
  482. memcpy(lxt, lxt_old, (sizeof(*lxt) * rhte->lxt_cnt));
  483. } else
  484. lxt = lxt_old;
  485. /* nothing can fail from now on */
  486. my_new_size = rhte->lxt_cnt + delta;
  487. /* add new entries to the end */
  488. for (i = rhte->lxt_cnt; i < my_new_size; i++) {
  489. /*
  490. * Due to the earlier check of available space, ba_alloc
  491. * cannot fail here. If it did due to internal error,
  492. * leave a rlba_base of -1u which will likely be a
  493. * invalid LUN (too large).
  494. */
  495. aun = ba_alloc(&blka->ba_lun);
  496. if ((aun == -1ULL) || (aun >= blka->nchunk))
  497. dev_dbg(dev, "%s: ba_alloc error allocated chunk=%llu "
  498. "max=%llu\n", __func__, aun, blka->nchunk - 1);
  499. /* select both ports, use r/w perms from RHT */
  500. lxt[i].rlba_base = ((aun << MC_CHUNK_SHIFT) |
  501. (lli->lun_index << LXT_LUNIDX_SHIFT) |
  502. (RHT_PERM_RW << LXT_PERM_SHIFT |
  503. lli->port_sel));
  504. }
  505. mutex_unlock(&blka->mutex);
  506. /*
  507. * The following sequence is prescribed in the SISlite spec
  508. * for syncing up with the AFU when adding LXT entries.
  509. */
  510. dma_wmb(); /* Make LXT updates are visible */
  511. rhte->lxt_start = lxt;
  512. dma_wmb(); /* Make RHT entry's LXT table update visible */
  513. rhte->lxt_cnt = my_new_size;
  514. dma_wmb(); /* Make RHT entry's LXT table size update visible */
  515. rc = cxlflash_afu_sync(afu, ctxid, rhndl, AFU_LW_SYNC);
  516. if (unlikely(rc))
  517. rc = -EAGAIN;
  518. /* free old lxt if reallocated */
  519. if (lxt != lxt_old)
  520. kfree(lxt_old);
  521. *new_size = my_new_size;
  522. out:
  523. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  524. return rc;
  525. }
  526. /**
  527. * shrink_lxt() - reduces translation table associated with the specified RHTE
  528. * @afu: AFU associated with the host.
  529. * @sdev: SCSI device associated with LUN.
  530. * @rhndl: Resource handle associated with the RHTE.
  531. * @rhte: Resource handle entry (RHTE).
  532. * @ctxi: Context owning resources.
  533. * @new_size: Number of translation entries associated with RHTE.
  534. *
  535. * Return: 0 on success, -errno on failure
  536. */
  537. static int shrink_lxt(struct afu *afu,
  538. struct scsi_device *sdev,
  539. res_hndl_t rhndl,
  540. struct sisl_rht_entry *rhte,
  541. struct ctx_info *ctxi,
  542. u64 *new_size)
  543. {
  544. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  545. struct device *dev = &cfg->dev->dev;
  546. struct sisl_lxt_entry *lxt, *lxt_old;
  547. struct llun_info *lli = sdev->hostdata;
  548. struct glun_info *gli = lli->parent;
  549. struct blka *blka = &gli->blka;
  550. ctx_hndl_t ctxid = DECODE_CTXID(ctxi->ctxid);
  551. bool needs_ws = ctxi->rht_needs_ws[rhndl];
  552. bool needs_sync = !ctxi->err_recovery_active;
  553. u32 ngrps, ngrps_old;
  554. u64 aun; /* chunk# allocated by block allocator */
  555. u64 delta = rhte->lxt_cnt - *new_size;
  556. u64 my_new_size;
  557. int i, rc = 0;
  558. lxt_old = rhte->lxt_start;
  559. ngrps_old = LXT_NUM_GROUPS(rhte->lxt_cnt);
  560. ngrps = LXT_NUM_GROUPS(rhte->lxt_cnt - delta);
  561. if (ngrps != ngrps_old) {
  562. /* Reallocate to fit new size unless new size is 0 */
  563. if (ngrps) {
  564. lxt = kzalloc((sizeof(*lxt) * LXT_GROUP_SIZE * ngrps),
  565. GFP_KERNEL);
  566. if (unlikely(!lxt)) {
  567. rc = -ENOMEM;
  568. goto out;
  569. }
  570. /* Copy over old entries that will remain */
  571. memcpy(lxt, lxt_old,
  572. (sizeof(*lxt) * (rhte->lxt_cnt - delta)));
  573. } else
  574. lxt = NULL;
  575. } else
  576. lxt = lxt_old;
  577. /* Nothing can fail from now on */
  578. my_new_size = rhte->lxt_cnt - delta;
  579. /*
  580. * The following sequence is prescribed in the SISlite spec
  581. * for syncing up with the AFU when removing LXT entries.
  582. */
  583. rhte->lxt_cnt = my_new_size;
  584. dma_wmb(); /* Make RHT entry's LXT table size update visible */
  585. rhte->lxt_start = lxt;
  586. dma_wmb(); /* Make RHT entry's LXT table update visible */
  587. if (needs_sync) {
  588. rc = cxlflash_afu_sync(afu, ctxid, rhndl, AFU_HW_SYNC);
  589. if (unlikely(rc))
  590. rc = -EAGAIN;
  591. }
  592. if (needs_ws) {
  593. /*
  594. * Mark the context as unavailable, so that we can release
  595. * the mutex safely.
  596. */
  597. ctxi->unavail = true;
  598. mutex_unlock(&ctxi->mutex);
  599. }
  600. /* Free LBAs allocated to freed chunks */
  601. mutex_lock(&blka->mutex);
  602. for (i = delta - 1; i >= 0; i--) {
  603. aun = lxt_old[my_new_size + i].rlba_base >> MC_CHUNK_SHIFT;
  604. if (needs_ws)
  605. write_same16(sdev, aun, MC_CHUNK_SIZE);
  606. ba_free(&blka->ba_lun, aun);
  607. }
  608. mutex_unlock(&blka->mutex);
  609. if (needs_ws) {
  610. /* Make the context visible again */
  611. mutex_lock(&ctxi->mutex);
  612. ctxi->unavail = false;
  613. }
  614. /* Free old lxt if reallocated */
  615. if (lxt != lxt_old)
  616. kfree(lxt_old);
  617. *new_size = my_new_size;
  618. out:
  619. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  620. return rc;
  621. }
  622. /**
  623. * _cxlflash_vlun_resize() - changes the size of a virtual LUN
  624. * @sdev: SCSI device associated with LUN owning virtual LUN.
  625. * @ctxi: Context owning resources.
  626. * @resize: Resize ioctl data structure.
  627. *
  628. * On successful return, the user is informed of the new size (in blocks)
  629. * of the virtual LUN in last LBA format. When the size of the virtual
  630. * LUN is zero, the last LBA is reflected as -1. See comment in the
  631. * prologue for _cxlflash_disk_release() regarding AFU syncs and contexts
  632. * on the error recovery list.
  633. *
  634. * Return: 0 on success, -errno on failure
  635. */
  636. int _cxlflash_vlun_resize(struct scsi_device *sdev,
  637. struct ctx_info *ctxi,
  638. struct dk_cxlflash_resize *resize)
  639. {
  640. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  641. struct device *dev = &cfg->dev->dev;
  642. struct llun_info *lli = sdev->hostdata;
  643. struct glun_info *gli = lli->parent;
  644. struct afu *afu = cfg->afu;
  645. bool put_ctx = false;
  646. res_hndl_t rhndl = resize->rsrc_handle;
  647. u64 new_size;
  648. u64 nsectors;
  649. u64 ctxid = DECODE_CTXID(resize->context_id),
  650. rctxid = resize->context_id;
  651. struct sisl_rht_entry *rhte;
  652. int rc = 0;
  653. /*
  654. * The requested size (req_size) is always assumed to be in 4k blocks,
  655. * so we have to convert it here from 4k to chunk size.
  656. */
  657. nsectors = (resize->req_size * CXLFLASH_BLOCK_SIZE) / gli->blk_len;
  658. new_size = DIV_ROUND_UP(nsectors, MC_CHUNK_SIZE);
  659. dev_dbg(dev, "%s: ctxid=%llu rhndl=%llu req_size=%llu new_size=%llu\n",
  660. __func__, ctxid, resize->rsrc_handle, resize->req_size,
  661. new_size);
  662. if (unlikely(gli->mode != MODE_VIRTUAL)) {
  663. dev_dbg(dev, "%s: LUN mode does not support resize mode=%d\n",
  664. __func__, gli->mode);
  665. rc = -EINVAL;
  666. goto out;
  667. }
  668. if (!ctxi) {
  669. ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
  670. if (unlikely(!ctxi)) {
  671. dev_dbg(dev, "%s: Bad context ctxid=%llu\n",
  672. __func__, ctxid);
  673. rc = -EINVAL;
  674. goto out;
  675. }
  676. put_ctx = true;
  677. }
  678. rhte = get_rhte(ctxi, rhndl, lli);
  679. if (unlikely(!rhte)) {
  680. dev_dbg(dev, "%s: Bad resource handle rhndl=%u\n",
  681. __func__, rhndl);
  682. rc = -EINVAL;
  683. goto out;
  684. }
  685. if (new_size > rhte->lxt_cnt)
  686. rc = grow_lxt(afu, sdev, ctxid, rhndl, rhte, &new_size);
  687. else if (new_size < rhte->lxt_cnt)
  688. rc = shrink_lxt(afu, sdev, rhndl, rhte, ctxi, &new_size);
  689. else {
  690. /*
  691. * Rare case where there is already sufficient space, just
  692. * need to perform a translation sync with the AFU. This
  693. * scenario likely follows a previous sync failure during
  694. * a resize operation. Accordingly, perform the heavyweight
  695. * form of translation sync as it is unknown which type of
  696. * resize failed previously.
  697. */
  698. rc = cxlflash_afu_sync(afu, ctxid, rhndl, AFU_HW_SYNC);
  699. if (unlikely(rc)) {
  700. rc = -EAGAIN;
  701. goto out;
  702. }
  703. }
  704. resize->hdr.return_flags = 0;
  705. resize->last_lba = (new_size * MC_CHUNK_SIZE * gli->blk_len);
  706. resize->last_lba /= CXLFLASH_BLOCK_SIZE;
  707. resize->last_lba--;
  708. out:
  709. if (put_ctx)
  710. put_context(ctxi);
  711. dev_dbg(dev, "%s: resized to %llu returning rc=%d\n",
  712. __func__, resize->last_lba, rc);
  713. return rc;
  714. }
  715. int cxlflash_vlun_resize(struct scsi_device *sdev,
  716. struct dk_cxlflash_resize *resize)
  717. {
  718. return _cxlflash_vlun_resize(sdev, NULL, resize);
  719. }
  720. /**
  721. * cxlflash_restore_luntable() - Restore LUN table to prior state
  722. * @cfg: Internal structure associated with the host.
  723. */
  724. void cxlflash_restore_luntable(struct cxlflash_cfg *cfg)
  725. {
  726. struct llun_info *lli, *temp;
  727. u32 lind;
  728. int k;
  729. struct device *dev = &cfg->dev->dev;
  730. __be64 __iomem *fc_port_luns;
  731. mutex_lock(&global.mutex);
  732. list_for_each_entry_safe(lli, temp, &cfg->lluns, list) {
  733. if (!lli->in_table)
  734. continue;
  735. lind = lli->lun_index;
  736. dev_dbg(dev, "%s: Virtual LUNs on slot %d:\n", __func__, lind);
  737. for (k = 0; k < cfg->num_fc_ports; k++)
  738. if (lli->port_sel & (1 << k)) {
  739. fc_port_luns = get_fc_port_luns(cfg, k);
  740. writeq_be(lli->lun_id[k], &fc_port_luns[lind]);
  741. dev_dbg(dev, "\t%d=%llx\n", k, lli->lun_id[k]);
  742. }
  743. }
  744. mutex_unlock(&global.mutex);
  745. }
  746. /**
  747. * get_num_ports() - compute number of ports from port selection mask
  748. * @psm: Port selection mask.
  749. *
  750. * Return: Population count of port selection mask
  751. */
  752. static inline u8 get_num_ports(u32 psm)
  753. {
  754. static const u8 bits[16] = { 0, 1, 1, 2, 1, 2, 2, 3,
  755. 1, 2, 2, 3, 2, 3, 3, 4 };
  756. return bits[psm & 0xf];
  757. }
  758. /**
  759. * init_luntable() - write an entry in the LUN table
  760. * @cfg: Internal structure associated with the host.
  761. * @lli: Per adapter LUN information structure.
  762. *
  763. * On successful return, a LUN table entry is created:
  764. * - at the top for LUNs visible on multiple ports.
  765. * - at the bottom for LUNs visible only on one port.
  766. *
  767. * Return: 0 on success, -errno on failure
  768. */
  769. static int init_luntable(struct cxlflash_cfg *cfg, struct llun_info *lli)
  770. {
  771. u32 chan;
  772. u32 lind;
  773. u32 nports;
  774. int rc = 0;
  775. int k;
  776. struct device *dev = &cfg->dev->dev;
  777. __be64 __iomem *fc_port_luns;
  778. mutex_lock(&global.mutex);
  779. if (lli->in_table)
  780. goto out;
  781. nports = get_num_ports(lli->port_sel);
  782. if (nports == 0 || nports > cfg->num_fc_ports) {
  783. WARN(1, "Unsupported port configuration nports=%u", nports);
  784. rc = -EIO;
  785. goto out;
  786. }
  787. if (nports > 1) {
  788. /*
  789. * When LUN is visible from multiple ports, we will put
  790. * it in the top half of the LUN table.
  791. */
  792. for (k = 0; k < cfg->num_fc_ports; k++) {
  793. if (!(lli->port_sel & (1 << k)))
  794. continue;
  795. if (cfg->promote_lun_index == cfg->last_lun_index[k]) {
  796. rc = -ENOSPC;
  797. goto out;
  798. }
  799. }
  800. lind = lli->lun_index = cfg->promote_lun_index;
  801. dev_dbg(dev, "%s: Virtual LUNs on slot %d:\n", __func__, lind);
  802. for (k = 0; k < cfg->num_fc_ports; k++) {
  803. if (!(lli->port_sel & (1 << k)))
  804. continue;
  805. fc_port_luns = get_fc_port_luns(cfg, k);
  806. writeq_be(lli->lun_id[k], &fc_port_luns[lind]);
  807. dev_dbg(dev, "\t%d=%llx\n", k, lli->lun_id[k]);
  808. }
  809. cfg->promote_lun_index++;
  810. } else {
  811. /*
  812. * When LUN is visible only from one port, we will put
  813. * it in the bottom half of the LUN table.
  814. */
  815. chan = PORTMASK2CHAN(lli->port_sel);
  816. if (cfg->promote_lun_index == cfg->last_lun_index[chan]) {
  817. rc = -ENOSPC;
  818. goto out;
  819. }
  820. lind = lli->lun_index = cfg->last_lun_index[chan];
  821. fc_port_luns = get_fc_port_luns(cfg, chan);
  822. writeq_be(lli->lun_id[chan], &fc_port_luns[lind]);
  823. cfg->last_lun_index[chan]--;
  824. dev_dbg(dev, "%s: Virtual LUNs on slot %d:\n\t%d=%llx\n",
  825. __func__, lind, chan, lli->lun_id[chan]);
  826. }
  827. lli->in_table = true;
  828. out:
  829. mutex_unlock(&global.mutex);
  830. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  831. return rc;
  832. }
  833. /**
  834. * cxlflash_disk_virtual_open() - open a virtual disk of specified size
  835. * @sdev: SCSI device associated with LUN owning virtual LUN.
  836. * @arg: UVirtual ioctl data structure.
  837. *
  838. * On successful return, the user is informed of the resource handle
  839. * to be used to identify the virtual LUN and the size (in blocks) of
  840. * the virtual LUN in last LBA format. When the size of the virtual LUN
  841. * is zero, the last LBA is reflected as -1.
  842. *
  843. * Return: 0 on success, -errno on failure
  844. */
  845. int cxlflash_disk_virtual_open(struct scsi_device *sdev, void *arg)
  846. {
  847. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  848. struct device *dev = &cfg->dev->dev;
  849. struct llun_info *lli = sdev->hostdata;
  850. struct glun_info *gli = lli->parent;
  851. struct dk_cxlflash_uvirtual *virt = (struct dk_cxlflash_uvirtual *)arg;
  852. struct dk_cxlflash_resize resize;
  853. u64 ctxid = DECODE_CTXID(virt->context_id),
  854. rctxid = virt->context_id;
  855. u64 lun_size = virt->lun_size;
  856. u64 last_lba = 0;
  857. u64 rsrc_handle = -1;
  858. int rc = 0;
  859. struct ctx_info *ctxi = NULL;
  860. struct sisl_rht_entry *rhte = NULL;
  861. dev_dbg(dev, "%s: ctxid=%llu ls=%llu\n", __func__, ctxid, lun_size);
  862. /* Setup the LUNs block allocator on first call */
  863. mutex_lock(&gli->mutex);
  864. if (gli->mode == MODE_NONE) {
  865. rc = init_vlun(lli);
  866. if (rc) {
  867. dev_err(dev, "%s: init_vlun failed rc=%d\n",
  868. __func__, rc);
  869. rc = -ENOMEM;
  870. goto err0;
  871. }
  872. }
  873. rc = cxlflash_lun_attach(gli, MODE_VIRTUAL, true);
  874. if (unlikely(rc)) {
  875. dev_err(dev, "%s: Failed attach to LUN (VIRTUAL)\n", __func__);
  876. goto err0;
  877. }
  878. mutex_unlock(&gli->mutex);
  879. rc = init_luntable(cfg, lli);
  880. if (rc) {
  881. dev_err(dev, "%s: init_luntable failed rc=%d\n", __func__, rc);
  882. goto err1;
  883. }
  884. ctxi = get_context(cfg, rctxid, lli, 0);
  885. if (unlikely(!ctxi)) {
  886. dev_err(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid);
  887. rc = -EINVAL;
  888. goto err1;
  889. }
  890. rhte = rhte_checkout(ctxi, lli);
  891. if (unlikely(!rhte)) {
  892. dev_err(dev, "%s: too many opens ctxid=%llu\n",
  893. __func__, ctxid);
  894. rc = -EMFILE; /* too many opens */
  895. goto err1;
  896. }
  897. rsrc_handle = (rhte - ctxi->rht_start);
  898. /* Populate RHT format 0 */
  899. rhte->nmask = MC_RHT_NMASK;
  900. rhte->fp = SISL_RHT_FP(0U, ctxi->rht_perms);
  901. /* Resize even if requested size is 0 */
  902. marshal_virt_to_resize(virt, &resize);
  903. resize.rsrc_handle = rsrc_handle;
  904. rc = _cxlflash_vlun_resize(sdev, ctxi, &resize);
  905. if (rc) {
  906. dev_err(dev, "%s: resize failed rc=%d\n", __func__, rc);
  907. goto err2;
  908. }
  909. last_lba = resize.last_lba;
  910. if (virt->hdr.flags & DK_CXLFLASH_UVIRTUAL_NEED_WRITE_SAME)
  911. ctxi->rht_needs_ws[rsrc_handle] = true;
  912. virt->hdr.return_flags = 0;
  913. virt->last_lba = last_lba;
  914. virt->rsrc_handle = rsrc_handle;
  915. if (get_num_ports(lli->port_sel) > 1)
  916. virt->hdr.return_flags |= DK_CXLFLASH_ALL_PORTS_ACTIVE;
  917. out:
  918. if (likely(ctxi))
  919. put_context(ctxi);
  920. dev_dbg(dev, "%s: returning handle=%llu rc=%d llba=%llu\n",
  921. __func__, rsrc_handle, rc, last_lba);
  922. return rc;
  923. err2:
  924. rhte_checkin(ctxi, rhte);
  925. err1:
  926. cxlflash_lun_detach(gli);
  927. goto out;
  928. err0:
  929. /* Special common cleanup prior to successful LUN attach */
  930. cxlflash_ba_terminate(&gli->blka.ba_lun);
  931. mutex_unlock(&gli->mutex);
  932. goto out;
  933. }
  934. /**
  935. * clone_lxt() - copies translation tables from source to destination RHTE
  936. * @afu: AFU associated with the host.
  937. * @blka: Block allocator associated with LUN.
  938. * @ctxid: Context ID of context owning the RHTE.
  939. * @rhndl: Resource handle associated with the RHTE.
  940. * @rhte: Destination resource handle entry (RHTE).
  941. * @rhte_src: Source resource handle entry (RHTE).
  942. *
  943. * Return: 0 on success, -errno on failure
  944. */
  945. static int clone_lxt(struct afu *afu,
  946. struct blka *blka,
  947. ctx_hndl_t ctxid,
  948. res_hndl_t rhndl,
  949. struct sisl_rht_entry *rhte,
  950. struct sisl_rht_entry *rhte_src)
  951. {
  952. struct cxlflash_cfg *cfg = afu->parent;
  953. struct device *dev = &cfg->dev->dev;
  954. struct sisl_lxt_entry *lxt = NULL;
  955. bool locked = false;
  956. u32 ngrps;
  957. u64 aun; /* chunk# allocated by block allocator */
  958. int j;
  959. int i = 0;
  960. int rc = 0;
  961. ngrps = LXT_NUM_GROUPS(rhte_src->lxt_cnt);
  962. if (ngrps) {
  963. /* allocate new LXTs for clone */
  964. lxt = kzalloc((sizeof(*lxt) * LXT_GROUP_SIZE * ngrps),
  965. GFP_KERNEL);
  966. if (unlikely(!lxt)) {
  967. rc = -ENOMEM;
  968. goto out;
  969. }
  970. /* copy over */
  971. memcpy(lxt, rhte_src->lxt_start,
  972. (sizeof(*lxt) * rhte_src->lxt_cnt));
  973. /* clone the LBAs in block allocator via ref_cnt, note that the
  974. * block allocator mutex must be held until it is established
  975. * that this routine will complete without the need for a
  976. * cleanup.
  977. */
  978. mutex_lock(&blka->mutex);
  979. locked = true;
  980. for (i = 0; i < rhte_src->lxt_cnt; i++) {
  981. aun = (lxt[i].rlba_base >> MC_CHUNK_SHIFT);
  982. if (ba_clone(&blka->ba_lun, aun) == -1ULL) {
  983. rc = -EIO;
  984. goto err;
  985. }
  986. }
  987. }
  988. /*
  989. * The following sequence is prescribed in the SISlite spec
  990. * for syncing up with the AFU when adding LXT entries.
  991. */
  992. dma_wmb(); /* Make LXT updates are visible */
  993. rhte->lxt_start = lxt;
  994. dma_wmb(); /* Make RHT entry's LXT table update visible */
  995. rhte->lxt_cnt = rhte_src->lxt_cnt;
  996. dma_wmb(); /* Make RHT entry's LXT table size update visible */
  997. rc = cxlflash_afu_sync(afu, ctxid, rhndl, AFU_LW_SYNC);
  998. if (unlikely(rc)) {
  999. rc = -EAGAIN;
  1000. goto err2;
  1001. }
  1002. out:
  1003. if (locked)
  1004. mutex_unlock(&blka->mutex);
  1005. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  1006. return rc;
  1007. err2:
  1008. /* Reset the RHTE */
  1009. rhte->lxt_cnt = 0;
  1010. dma_wmb();
  1011. rhte->lxt_start = NULL;
  1012. dma_wmb();
  1013. err:
  1014. /* free the clones already made */
  1015. for (j = 0; j < i; j++) {
  1016. aun = (lxt[j].rlba_base >> MC_CHUNK_SHIFT);
  1017. ba_free(&blka->ba_lun, aun);
  1018. }
  1019. kfree(lxt);
  1020. goto out;
  1021. }
  1022. /**
  1023. * cxlflash_disk_clone() - clone a context by making snapshot of another
  1024. * @sdev: SCSI device associated with LUN owning virtual LUN.
  1025. * @clone: Clone ioctl data structure.
  1026. *
  1027. * This routine effectively performs cxlflash_disk_open operation for each
  1028. * in-use virtual resource in the source context. Note that the destination
  1029. * context must be in pristine state and cannot have any resource handles
  1030. * open at the time of the clone.
  1031. *
  1032. * Return: 0 on success, -errno on failure
  1033. */
  1034. int cxlflash_disk_clone(struct scsi_device *sdev,
  1035. struct dk_cxlflash_clone *clone)
  1036. {
  1037. struct cxlflash_cfg *cfg = shost_priv(sdev->host);
  1038. struct device *dev = &cfg->dev->dev;
  1039. struct llun_info *lli = sdev->hostdata;
  1040. struct glun_info *gli = lli->parent;
  1041. struct blka *blka = &gli->blka;
  1042. struct afu *afu = cfg->afu;
  1043. struct dk_cxlflash_release release = { { 0 }, 0 };
  1044. struct ctx_info *ctxi_src = NULL,
  1045. *ctxi_dst = NULL;
  1046. struct lun_access *lun_access_src, *lun_access_dst;
  1047. u32 perms;
  1048. u64 ctxid_src = DECODE_CTXID(clone->context_id_src),
  1049. ctxid_dst = DECODE_CTXID(clone->context_id_dst),
  1050. rctxid_src = clone->context_id_src,
  1051. rctxid_dst = clone->context_id_dst;
  1052. int i, j;
  1053. int rc = 0;
  1054. bool found;
  1055. LIST_HEAD(sidecar);
  1056. dev_dbg(dev, "%s: ctxid_src=%llu ctxid_dst=%llu\n",
  1057. __func__, ctxid_src, ctxid_dst);
  1058. /* Do not clone yourself */
  1059. if (unlikely(rctxid_src == rctxid_dst)) {
  1060. rc = -EINVAL;
  1061. goto out;
  1062. }
  1063. if (unlikely(gli->mode != MODE_VIRTUAL)) {
  1064. rc = -EINVAL;
  1065. dev_dbg(dev, "%s: Only supported on virtual LUNs mode=%u\n",
  1066. __func__, gli->mode);
  1067. goto out;
  1068. }
  1069. ctxi_src = get_context(cfg, rctxid_src, lli, CTX_CTRL_CLONE);
  1070. ctxi_dst = get_context(cfg, rctxid_dst, lli, 0);
  1071. if (unlikely(!ctxi_src || !ctxi_dst)) {
  1072. dev_dbg(dev, "%s: Bad context ctxid_src=%llu ctxid_dst=%llu\n",
  1073. __func__, ctxid_src, ctxid_dst);
  1074. rc = -EINVAL;
  1075. goto out;
  1076. }
  1077. /* Verify there is no open resource handle in the destination context */
  1078. for (i = 0; i < MAX_RHT_PER_CONTEXT; i++)
  1079. if (ctxi_dst->rht_start[i].nmask != 0) {
  1080. rc = -EINVAL;
  1081. goto out;
  1082. }
  1083. /* Clone LUN access list */
  1084. list_for_each_entry(lun_access_src, &ctxi_src->luns, list) {
  1085. found = false;
  1086. list_for_each_entry(lun_access_dst, &ctxi_dst->luns, list)
  1087. if (lun_access_dst->sdev == lun_access_src->sdev) {
  1088. found = true;
  1089. break;
  1090. }
  1091. if (!found) {
  1092. lun_access_dst = kzalloc(sizeof(*lun_access_dst),
  1093. GFP_KERNEL);
  1094. if (unlikely(!lun_access_dst)) {
  1095. dev_err(dev, "%s: lun_access allocation fail\n",
  1096. __func__);
  1097. rc = -ENOMEM;
  1098. goto out;
  1099. }
  1100. *lun_access_dst = *lun_access_src;
  1101. list_add(&lun_access_dst->list, &sidecar);
  1102. }
  1103. }
  1104. if (unlikely(!ctxi_src->rht_out)) {
  1105. dev_dbg(dev, "%s: Nothing to clone\n", __func__);
  1106. goto out_success;
  1107. }
  1108. /* User specified permission on attach */
  1109. perms = ctxi_dst->rht_perms;
  1110. /*
  1111. * Copy over checked-out RHT (and their associated LXT) entries by
  1112. * hand, stopping after we've copied all outstanding entries and
  1113. * cleaning up if the clone fails.
  1114. *
  1115. * Note: This loop is equivalent to performing cxlflash_disk_open and
  1116. * cxlflash_vlun_resize. As such, LUN accounting needs to be taken into
  1117. * account by attaching after each successful RHT entry clone. In the
  1118. * event that a clone failure is experienced, the LUN detach is handled
  1119. * via the cleanup performed by _cxlflash_disk_release.
  1120. */
  1121. for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) {
  1122. if (ctxi_src->rht_out == ctxi_dst->rht_out)
  1123. break;
  1124. if (ctxi_src->rht_start[i].nmask == 0)
  1125. continue;
  1126. /* Consume a destination RHT entry */
  1127. ctxi_dst->rht_out++;
  1128. ctxi_dst->rht_start[i].nmask = ctxi_src->rht_start[i].nmask;
  1129. ctxi_dst->rht_start[i].fp =
  1130. SISL_RHT_FP_CLONE(ctxi_src->rht_start[i].fp, perms);
  1131. ctxi_dst->rht_lun[i] = ctxi_src->rht_lun[i];
  1132. rc = clone_lxt(afu, blka, ctxid_dst, i,
  1133. &ctxi_dst->rht_start[i],
  1134. &ctxi_src->rht_start[i]);
  1135. if (rc) {
  1136. marshal_clone_to_rele(clone, &release);
  1137. for (j = 0; j < i; j++) {
  1138. release.rsrc_handle = j;
  1139. _cxlflash_disk_release(sdev, ctxi_dst,
  1140. &release);
  1141. }
  1142. /* Put back the one we failed on */
  1143. rhte_checkin(ctxi_dst, &ctxi_dst->rht_start[i]);
  1144. goto err;
  1145. }
  1146. cxlflash_lun_attach(gli, gli->mode, false);
  1147. }
  1148. out_success:
  1149. list_splice(&sidecar, &ctxi_dst->luns);
  1150. /* fall through */
  1151. out:
  1152. if (ctxi_src)
  1153. put_context(ctxi_src);
  1154. if (ctxi_dst)
  1155. put_context(ctxi_dst);
  1156. dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
  1157. return rc;
  1158. err:
  1159. list_for_each_entry_safe(lun_access_src, lun_access_dst, &sidecar, list)
  1160. kfree(lun_access_src);
  1161. goto out;
  1162. }