vlun.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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/syscalls.h>
  15. #include <misc/cxl.h>
  16. #include <asm/unaligned.h>
  17. #include <asm/bitsperlong.h>
  18. #include <scsi/scsi_cmnd.h>
  19. #include <scsi/scsi_host.h>
  20. #include <uapi/scsi/cxlflash_ioctl.h>
  21. #include "sislite.h"
  22. #include "common.h"
  23. #include "vlun.h"
  24. #include "superpipe.h"
  25. /**
  26. * marshal_virt_to_resize() - translate uvirtual to resize structure
  27. * @virt: Source structure from which to translate/copy.
  28. * @resize: Destination structure for the translate/copy.
  29. */
  30. static void marshal_virt_to_resize(struct dk_cxlflash_uvirtual *virt,
  31. struct dk_cxlflash_resize *resize)
  32. {
  33. resize->hdr = virt->hdr;
  34. resize->context_id = virt->context_id;
  35. resize->rsrc_handle = virt->rsrc_handle;
  36. resize->req_size = virt->lun_size;
  37. resize->last_lba = virt->last_lba;
  38. }
  39. /**
  40. * marshal_clone_to_rele() - translate clone to release structure
  41. * @clone: Source structure from which to translate/copy.
  42. * @rele: Destination structure for the translate/copy.
  43. */
  44. static void marshal_clone_to_rele(struct dk_cxlflash_clone *clone,
  45. struct dk_cxlflash_release *release)
  46. {
  47. release->hdr = clone->hdr;
  48. release->context_id = clone->context_id_dst;
  49. }
  50. /**
  51. * ba_init() - initializes a block allocator
  52. * @ba_lun: Block allocator to initialize.
  53. *
  54. * Return: 0 on success, -errno on failure
  55. */
  56. static int ba_init(struct ba_lun *ba_lun)
  57. {
  58. struct ba_lun_info *bali = NULL;
  59. int lun_size_au = 0, i = 0;
  60. int last_word_underflow = 0;
  61. u64 *lam;
  62. pr_debug("%s: Initializing LUN: lun_id = %llX, "
  63. "ba_lun->lsize = %lX, ba_lun->au_size = %lX\n",
  64. __func__, ba_lun->lun_id, ba_lun->lsize, ba_lun->au_size);
  65. /* Calculate bit map size */
  66. lun_size_au = ba_lun->lsize / ba_lun->au_size;
  67. if (lun_size_au == 0) {
  68. pr_debug("%s: Requested LUN size of 0!\n", __func__);
  69. return -EINVAL;
  70. }
  71. /* Allocate lun information container */
  72. bali = kzalloc(sizeof(struct ba_lun_info), GFP_KERNEL);
  73. if (unlikely(!bali)) {
  74. pr_err("%s: Failed to allocate lun_info for lun_id %llX\n",
  75. __func__, ba_lun->lun_id);
  76. return -ENOMEM;
  77. }
  78. bali->total_aus = lun_size_au;
  79. bali->lun_bmap_size = lun_size_au / BITS_PER_LONG;
  80. if (lun_size_au % BITS_PER_LONG)
  81. bali->lun_bmap_size++;
  82. /* Allocate bitmap space */
  83. bali->lun_alloc_map = kzalloc((bali->lun_bmap_size * sizeof(u64)),
  84. GFP_KERNEL);
  85. if (unlikely(!bali->lun_alloc_map)) {
  86. pr_err("%s: Failed to allocate lun allocation map: "
  87. "lun_id = %llX\n", __func__, ba_lun->lun_id);
  88. kfree(bali);
  89. return -ENOMEM;
  90. }
  91. /* Initialize the bit map size and set all bits to '1' */
  92. bali->free_aun_cnt = lun_size_au;
  93. for (i = 0; i < bali->lun_bmap_size; i++)
  94. bali->lun_alloc_map[i] = 0xFFFFFFFFFFFFFFFFULL;
  95. /* If the last word not fully utilized, mark extra bits as allocated */
  96. last_word_underflow = (bali->lun_bmap_size * BITS_PER_LONG);
  97. last_word_underflow -= bali->free_aun_cnt;
  98. if (last_word_underflow > 0) {
  99. lam = &bali->lun_alloc_map[bali->lun_bmap_size - 1];
  100. for (i = (HIBIT - last_word_underflow + 1);
  101. i < BITS_PER_LONG;
  102. i++)
  103. clear_bit(i, (ulong *)lam);
  104. }
  105. /* Initialize high elevator index, low/curr already at 0 from kzalloc */
  106. bali->free_high_idx = bali->lun_bmap_size;
  107. /* Allocate clone map */
  108. bali->aun_clone_map = kzalloc((bali->total_aus * sizeof(u8)),
  109. GFP_KERNEL);
  110. if (unlikely(!bali->aun_clone_map)) {
  111. pr_err("%s: Failed to allocate clone map: lun_id = %llX\n",
  112. __func__, ba_lun->lun_id);
  113. kfree(bali->lun_alloc_map);
  114. kfree(bali);
  115. return -ENOMEM;
  116. }
  117. /* Pass the allocated LUN info as a handle to the user */
  118. ba_lun->ba_lun_handle = bali;
  119. pr_debug("%s: Successfully initialized the LUN: "
  120. "lun_id = %llX, bitmap size = %X, free_aun_cnt = %llX\n",
  121. __func__, ba_lun->lun_id, bali->lun_bmap_size,
  122. bali->free_aun_cnt);
  123. return 0;
  124. }
  125. /**
  126. * find_free_range() - locates a free bit within the block allocator
  127. * @low: First word in block allocator to start search.
  128. * @high: Last word in block allocator to search.
  129. * @bali: LUN information structure owning the block allocator to search.
  130. * @bit_word: Passes back the word in the block allocator owning the free bit.
  131. *
  132. * Return: The bit position within the passed back word, -1 on failure
  133. */
  134. static int find_free_range(u32 low,
  135. u32 high,
  136. struct ba_lun_info *bali, int *bit_word)
  137. {
  138. int i;
  139. u64 bit_pos = -1;
  140. ulong *lam, num_bits;
  141. for (i = low; i < high; i++)
  142. if (bali->lun_alloc_map[i] != 0) {
  143. lam = (ulong *)&bali->lun_alloc_map[i];
  144. num_bits = (sizeof(*lam) * BITS_PER_BYTE);
  145. bit_pos = find_first_bit(lam, num_bits);
  146. pr_devel("%s: Found free bit %llX in LUN "
  147. "map entry %llX at bitmap index = %X\n",
  148. __func__, bit_pos, bali->lun_alloc_map[i],
  149. 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 = %llX, 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 = %llX\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 = %llX\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, on lun_id %llX, "
  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: The AUN %llX is not allocated on lun_id %llX\n",
  231. __func__, to_free, ba_lun->lun_id);
  232. return -1;
  233. }
  234. pr_debug("%s: Received a request to free AU %llX on lun_id %llX, "
  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 on lun_id %llX has been cloned. Clone "
  239. "count = %X\n", __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 at bit_pos %X, bit map index %X on "
  253. "lun_id %llX, 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 is not allocated on lun_id %llX\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 %llX\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 %llX 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. u8 *sense_buf = NULL;
  367. int rc = 0;
  368. int result = 0;
  369. int ws_limit = SISLITE_MAX_WS_BLOCKS;
  370. u64 offset = lba;
  371. int left = nblks;
  372. u32 to = sdev->request_queue->rq_timeout;
  373. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
  374. struct device *dev = &cfg->dev->dev;
  375. cmd_buf = kzalloc(CMD_BUFSIZE, GFP_KERNEL);
  376. scsi_cmd = kzalloc(MAX_COMMAND_SIZE, GFP_KERNEL);
  377. sense_buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
  378. if (unlikely(!cmd_buf || !scsi_cmd || !sense_buf)) {
  379. rc = -ENOMEM;
  380. goto out;
  381. }
  382. while (left > 0) {
  383. scsi_cmd[0] = WRITE_SAME_16;
  384. put_unaligned_be64(offset, &scsi_cmd[2]);
  385. put_unaligned_be32(ws_limit < left ? ws_limit : left,
  386. &scsi_cmd[10]);
  387. /* Drop the ioctl read semahpore across lengthy call */
  388. up_read(&cfg->ioctl_rwsem);
  389. result = scsi_execute(sdev, scsi_cmd, DMA_TO_DEVICE, cmd_buf,
  390. CMD_BUFSIZE, sense_buf, to, CMD_RETRIES,
  391. 0, NULL);
  392. down_read(&cfg->ioctl_rwsem);
  393. rc = check_state(cfg);
  394. if (rc) {
  395. dev_err(dev, "%s: Failed state! result=0x08%X\n",
  396. __func__, result);
  397. rc = -ENODEV;
  398. goto out;
  399. }
  400. if (result) {
  401. dev_err_ratelimited(dev, "%s: command failed for "
  402. "offset %lld result=0x%x\n",
  403. __func__, offset, result);
  404. rc = -EIO;
  405. goto out;
  406. }
  407. left -= ws_limit;
  408. offset += ws_limit;
  409. }
  410. out:
  411. kfree(cmd_buf);
  412. kfree(scsi_cmd);
  413. kfree(sense_buf);
  414. pr_debug("%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 sisl_lxt_entry *lxt = NULL, *lxt_old = NULL;
  442. struct llun_info *lli = sdev->hostdata;
  443. struct glun_info *gli = lli->parent;
  444. struct blka *blka = &gli->blka;
  445. u32 av_size;
  446. u32 ngrps, ngrps_old;
  447. u64 aun; /* chunk# allocated by block allocator */
  448. u64 delta = *new_size - rhte->lxt_cnt;
  449. u64 my_new_size;
  450. int i, rc = 0;
  451. /*
  452. * Check what is available in the block allocator before re-allocating
  453. * LXT array. This is done up front under the mutex which must not be
  454. * released until after allocation is complete.
  455. */
  456. mutex_lock(&blka->mutex);
  457. av_size = ba_space(&blka->ba_lun);
  458. if (unlikely(av_size <= 0)) {
  459. pr_debug("%s: ba_space error: av_size %d\n", __func__, av_size);
  460. mutex_unlock(&blka->mutex);
  461. rc = -ENOSPC;
  462. goto out;
  463. }
  464. if (av_size < delta)
  465. delta = av_size;
  466. lxt_old = rhte->lxt_start;
  467. ngrps_old = LXT_NUM_GROUPS(rhte->lxt_cnt);
  468. ngrps = LXT_NUM_GROUPS(rhte->lxt_cnt + delta);
  469. if (ngrps != ngrps_old) {
  470. /* reallocate to fit new size */
  471. lxt = kzalloc((sizeof(*lxt) * LXT_GROUP_SIZE * ngrps),
  472. GFP_KERNEL);
  473. if (unlikely(!lxt)) {
  474. mutex_unlock(&blka->mutex);
  475. rc = -ENOMEM;
  476. goto out;
  477. }
  478. /* copy over all old entries */
  479. memcpy(lxt, lxt_old, (sizeof(*lxt) * rhte->lxt_cnt));
  480. } else
  481. lxt = lxt_old;
  482. /* nothing can fail from now on */
  483. my_new_size = rhte->lxt_cnt + delta;
  484. /* add new entries to the end */
  485. for (i = rhte->lxt_cnt; i < my_new_size; i++) {
  486. /*
  487. * Due to the earlier check of available space, ba_alloc
  488. * cannot fail here. If it did due to internal error,
  489. * leave a rlba_base of -1u which will likely be a
  490. * invalid LUN (too large).
  491. */
  492. aun = ba_alloc(&blka->ba_lun);
  493. if ((aun == -1ULL) || (aun >= blka->nchunk))
  494. pr_debug("%s: ba_alloc error: allocated chunk# %llX, "
  495. "max %llX\n", __func__, aun, blka->nchunk - 1);
  496. /* select both ports, use r/w perms from RHT */
  497. lxt[i].rlba_base = ((aun << MC_CHUNK_SHIFT) |
  498. (lli->lun_index << LXT_LUNIDX_SHIFT) |
  499. (RHT_PERM_RW << LXT_PERM_SHIFT |
  500. lli->port_sel));
  501. }
  502. mutex_unlock(&blka->mutex);
  503. /*
  504. * The following sequence is prescribed in the SISlite spec
  505. * for syncing up with the AFU when adding LXT entries.
  506. */
  507. dma_wmb(); /* Make LXT updates are visible */
  508. rhte->lxt_start = lxt;
  509. dma_wmb(); /* Make RHT entry's LXT table update visible */
  510. rhte->lxt_cnt = my_new_size;
  511. dma_wmb(); /* Make RHT entry's LXT table size update visible */
  512. cxlflash_afu_sync(afu, ctxid, rhndl, AFU_LW_SYNC);
  513. /* free old lxt if reallocated */
  514. if (lxt != lxt_old)
  515. kfree(lxt_old);
  516. *new_size = my_new_size;
  517. out:
  518. pr_debug("%s: returning rc=%d\n", __func__, rc);
  519. return rc;
  520. }
  521. /**
  522. * shrink_lxt() - reduces translation table associated with the specified RHTE
  523. * @afu: AFU associated with the host.
  524. * @sdev: SCSI device associated with LUN.
  525. * @rhndl: Resource handle associated with the RHTE.
  526. * @rhte: Resource handle entry (RHTE).
  527. * @ctxi: Context owning resources.
  528. * @new_size: Number of translation entries associated with RHTE.
  529. *
  530. * Return: 0 on success, -errno on failure
  531. */
  532. static int shrink_lxt(struct afu *afu,
  533. struct scsi_device *sdev,
  534. res_hndl_t rhndl,
  535. struct sisl_rht_entry *rhte,
  536. struct ctx_info *ctxi,
  537. u64 *new_size)
  538. {
  539. struct sisl_lxt_entry *lxt, *lxt_old;
  540. struct llun_info *lli = sdev->hostdata;
  541. struct glun_info *gli = lli->parent;
  542. struct blka *blka = &gli->blka;
  543. ctx_hndl_t ctxid = DECODE_CTXID(ctxi->ctxid);
  544. bool needs_ws = ctxi->rht_needs_ws[rhndl];
  545. bool needs_sync = !ctxi->err_recovery_active;
  546. u32 ngrps, ngrps_old;
  547. u64 aun; /* chunk# allocated by block allocator */
  548. u64 delta = rhte->lxt_cnt - *new_size;
  549. u64 my_new_size;
  550. int i, rc = 0;
  551. lxt_old = rhte->lxt_start;
  552. ngrps_old = LXT_NUM_GROUPS(rhte->lxt_cnt);
  553. ngrps = LXT_NUM_GROUPS(rhte->lxt_cnt - delta);
  554. if (ngrps != ngrps_old) {
  555. /* Reallocate to fit new size unless new size is 0 */
  556. if (ngrps) {
  557. lxt = kzalloc((sizeof(*lxt) * LXT_GROUP_SIZE * ngrps),
  558. GFP_KERNEL);
  559. if (unlikely(!lxt)) {
  560. rc = -ENOMEM;
  561. goto out;
  562. }
  563. /* Copy over old entries that will remain */
  564. memcpy(lxt, lxt_old,
  565. (sizeof(*lxt) * (rhte->lxt_cnt - delta)));
  566. } else
  567. lxt = NULL;
  568. } else
  569. lxt = lxt_old;
  570. /* Nothing can fail from now on */
  571. my_new_size = rhte->lxt_cnt - delta;
  572. /*
  573. * The following sequence is prescribed in the SISlite spec
  574. * for syncing up with the AFU when removing LXT entries.
  575. */
  576. rhte->lxt_cnt = my_new_size;
  577. dma_wmb(); /* Make RHT entry's LXT table size update visible */
  578. rhte->lxt_start = lxt;
  579. dma_wmb(); /* Make RHT entry's LXT table update visible */
  580. if (needs_sync)
  581. cxlflash_afu_sync(afu, ctxid, rhndl, AFU_HW_SYNC);
  582. if (needs_ws) {
  583. /*
  584. * Mark the context as unavailable, so that we can release
  585. * the mutex safely.
  586. */
  587. ctxi->unavail = true;
  588. mutex_unlock(&ctxi->mutex);
  589. }
  590. /* Free LBAs allocated to freed chunks */
  591. mutex_lock(&blka->mutex);
  592. for (i = delta - 1; i >= 0; i--) {
  593. /* Mask the higher 48 bits before shifting, even though
  594. * it is a noop
  595. */
  596. aun = (lxt_old[my_new_size + i].rlba_base & SISL_ASTATUS_MASK);
  597. aun = (aun >> MC_CHUNK_SHIFT);
  598. if (needs_ws)
  599. write_same16(sdev, aun, MC_CHUNK_SIZE);
  600. ba_free(&blka->ba_lun, aun);
  601. }
  602. mutex_unlock(&blka->mutex);
  603. if (needs_ws) {
  604. /* Make the context visible again */
  605. mutex_lock(&ctxi->mutex);
  606. ctxi->unavail = false;
  607. }
  608. /* Free old lxt if reallocated */
  609. if (lxt != lxt_old)
  610. kfree(lxt_old);
  611. *new_size = my_new_size;
  612. out:
  613. pr_debug("%s: returning rc=%d\n", __func__, rc);
  614. return rc;
  615. }
  616. /**
  617. * _cxlflash_vlun_resize() - changes the size of a virtual LUN
  618. * @sdev: SCSI device associated with LUN owning virtual LUN.
  619. * @ctxi: Context owning resources.
  620. * @resize: Resize ioctl data structure.
  621. *
  622. * On successful return, the user is informed of the new size (in blocks)
  623. * of the virtual LUN in last LBA format. When the size of the virtual
  624. * LUN is zero, the last LBA is reflected as -1. See comment in the
  625. * prologue for _cxlflash_disk_release() regarding AFU syncs and contexts
  626. * on the error recovery list.
  627. *
  628. * Return: 0 on success, -errno on failure
  629. */
  630. int _cxlflash_vlun_resize(struct scsi_device *sdev,
  631. struct ctx_info *ctxi,
  632. struct dk_cxlflash_resize *resize)
  633. {
  634. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
  635. struct llun_info *lli = sdev->hostdata;
  636. struct glun_info *gli = lli->parent;
  637. struct afu *afu = cfg->afu;
  638. bool put_ctx = false;
  639. res_hndl_t rhndl = resize->rsrc_handle;
  640. u64 new_size;
  641. u64 nsectors;
  642. u64 ctxid = DECODE_CTXID(resize->context_id),
  643. rctxid = resize->context_id;
  644. struct sisl_rht_entry *rhte;
  645. int rc = 0;
  646. /*
  647. * The requested size (req_size) is always assumed to be in 4k blocks,
  648. * so we have to convert it here from 4k to chunk size.
  649. */
  650. nsectors = (resize->req_size * CXLFLASH_BLOCK_SIZE) / gli->blk_len;
  651. new_size = DIV_ROUND_UP(nsectors, MC_CHUNK_SIZE);
  652. pr_debug("%s: ctxid=%llu rhndl=0x%llx, req_size=0x%llx,"
  653. "new_size=%llx\n", __func__, ctxid, resize->rsrc_handle,
  654. resize->req_size, new_size);
  655. if (unlikely(gli->mode != MODE_VIRTUAL)) {
  656. pr_debug("%s: LUN mode does not support resize! (%d)\n",
  657. __func__, gli->mode);
  658. rc = -EINVAL;
  659. goto out;
  660. }
  661. if (!ctxi) {
  662. ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
  663. if (unlikely(!ctxi)) {
  664. pr_debug("%s: Bad context! (%llu)\n", __func__, ctxid);
  665. rc = -EINVAL;
  666. goto out;
  667. }
  668. put_ctx = true;
  669. }
  670. rhte = get_rhte(ctxi, rhndl, lli);
  671. if (unlikely(!rhte)) {
  672. pr_debug("%s: Bad resource handle! (%u)\n", __func__, rhndl);
  673. rc = -EINVAL;
  674. goto out;
  675. }
  676. if (new_size > rhte->lxt_cnt)
  677. rc = grow_lxt(afu, sdev, ctxid, rhndl, rhte, &new_size);
  678. else if (new_size < rhte->lxt_cnt)
  679. rc = shrink_lxt(afu, sdev, rhndl, rhte, ctxi, &new_size);
  680. resize->hdr.return_flags = 0;
  681. resize->last_lba = (new_size * MC_CHUNK_SIZE * gli->blk_len);
  682. resize->last_lba /= CXLFLASH_BLOCK_SIZE;
  683. resize->last_lba--;
  684. out:
  685. if (put_ctx)
  686. put_context(ctxi);
  687. pr_debug("%s: resized to %lld returning rc=%d\n",
  688. __func__, resize->last_lba, rc);
  689. return rc;
  690. }
  691. int cxlflash_vlun_resize(struct scsi_device *sdev,
  692. struct dk_cxlflash_resize *resize)
  693. {
  694. return _cxlflash_vlun_resize(sdev, NULL, resize);
  695. }
  696. /**
  697. * cxlflash_restore_luntable() - Restore LUN table to prior state
  698. * @cfg: Internal structure associated with the host.
  699. */
  700. void cxlflash_restore_luntable(struct cxlflash_cfg *cfg)
  701. {
  702. struct llun_info *lli, *temp;
  703. u32 chan;
  704. u32 lind;
  705. struct afu *afu = cfg->afu;
  706. struct sisl_global_map __iomem *agm = &afu->afu_map->global;
  707. mutex_lock(&global.mutex);
  708. list_for_each_entry_safe(lli, temp, &cfg->lluns, list) {
  709. if (!lli->in_table)
  710. continue;
  711. lind = lli->lun_index;
  712. if (lli->port_sel == BOTH_PORTS) {
  713. writeq_be(lli->lun_id[0], &agm->fc_port[0][lind]);
  714. writeq_be(lli->lun_id[1], &agm->fc_port[1][lind]);
  715. pr_debug("%s: Virtual LUN on slot %d id0=%llx, "
  716. "id1=%llx\n", __func__, lind,
  717. lli->lun_id[0], lli->lun_id[1]);
  718. } else {
  719. chan = PORT2CHAN(lli->port_sel);
  720. writeq_be(lli->lun_id[chan], &agm->fc_port[chan][lind]);
  721. pr_debug("%s: Virtual LUN on slot %d chan=%d, "
  722. "id=%llx\n", __func__, lind, chan,
  723. lli->lun_id[chan]);
  724. }
  725. }
  726. mutex_unlock(&global.mutex);
  727. }
  728. /**
  729. * init_luntable() - write an entry in the LUN table
  730. * @cfg: Internal structure associated with the host.
  731. * @lli: Per adapter LUN information structure.
  732. *
  733. * On successful return, a LUN table entry is created.
  734. * At the top for LUNs visible on both ports.
  735. * At the bottom for LUNs visible only on one port.
  736. *
  737. * Return: 0 on success, -errno on failure
  738. */
  739. static int init_luntable(struct cxlflash_cfg *cfg, struct llun_info *lli)
  740. {
  741. u32 chan;
  742. u32 lind;
  743. int rc = 0;
  744. struct afu *afu = cfg->afu;
  745. struct sisl_global_map __iomem *agm = &afu->afu_map->global;
  746. mutex_lock(&global.mutex);
  747. if (lli->in_table)
  748. goto out;
  749. if (lli->port_sel == BOTH_PORTS) {
  750. /*
  751. * If this LUN is visible from both ports, we will put
  752. * it in the top half of the LUN table.
  753. */
  754. if ((cfg->promote_lun_index == cfg->last_lun_index[0]) ||
  755. (cfg->promote_lun_index == cfg->last_lun_index[1])) {
  756. rc = -ENOSPC;
  757. goto out;
  758. }
  759. lind = lli->lun_index = cfg->promote_lun_index;
  760. writeq_be(lli->lun_id[0], &agm->fc_port[0][lind]);
  761. writeq_be(lli->lun_id[1], &agm->fc_port[1][lind]);
  762. cfg->promote_lun_index++;
  763. pr_debug("%s: Virtual LUN on slot %d id0=%llx, id1=%llx\n",
  764. __func__, lind, lli->lun_id[0], lli->lun_id[1]);
  765. } else {
  766. /*
  767. * If this LUN is visible only from one port, we will put
  768. * it in the bottom half of the LUN table.
  769. */
  770. chan = PORT2CHAN(lli->port_sel);
  771. if (cfg->promote_lun_index == cfg->last_lun_index[chan]) {
  772. rc = -ENOSPC;
  773. goto out;
  774. }
  775. lind = lli->lun_index = cfg->last_lun_index[chan];
  776. writeq_be(lli->lun_id[chan], &agm->fc_port[chan][lind]);
  777. cfg->last_lun_index[chan]--;
  778. pr_debug("%s: Virtual LUN on slot %d chan=%d, id=%llx\n",
  779. __func__, lind, chan, lli->lun_id[chan]);
  780. }
  781. lli->in_table = true;
  782. out:
  783. mutex_unlock(&global.mutex);
  784. pr_debug("%s: returning rc=%d\n", __func__, rc);
  785. return rc;
  786. }
  787. /**
  788. * cxlflash_disk_virtual_open() - open a virtual disk of specified size
  789. * @sdev: SCSI device associated with LUN owning virtual LUN.
  790. * @arg: UVirtual ioctl data structure.
  791. *
  792. * On successful return, the user is informed of the resource handle
  793. * to be used to identify the virtual LUN and the size (in blocks) of
  794. * the virtual LUN in last LBA format. When the size of the virtual LUN
  795. * is zero, the last LBA is reflected as -1.
  796. *
  797. * Return: 0 on success, -errno on failure
  798. */
  799. int cxlflash_disk_virtual_open(struct scsi_device *sdev, void *arg)
  800. {
  801. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
  802. struct device *dev = &cfg->dev->dev;
  803. struct llun_info *lli = sdev->hostdata;
  804. struct glun_info *gli = lli->parent;
  805. struct dk_cxlflash_uvirtual *virt = (struct dk_cxlflash_uvirtual *)arg;
  806. struct dk_cxlflash_resize resize;
  807. u64 ctxid = DECODE_CTXID(virt->context_id),
  808. rctxid = virt->context_id;
  809. u64 lun_size = virt->lun_size;
  810. u64 last_lba = 0;
  811. u64 rsrc_handle = -1;
  812. int rc = 0;
  813. struct ctx_info *ctxi = NULL;
  814. struct sisl_rht_entry *rhte = NULL;
  815. pr_debug("%s: ctxid=%llu ls=0x%llx\n", __func__, ctxid, lun_size);
  816. /* Setup the LUNs block allocator on first call */
  817. mutex_lock(&gli->mutex);
  818. if (gli->mode == MODE_NONE) {
  819. rc = init_vlun(lli);
  820. if (rc) {
  821. dev_err(dev, "%s: call to init_vlun failed rc=%d!\n",
  822. __func__, rc);
  823. rc = -ENOMEM;
  824. goto err0;
  825. }
  826. }
  827. rc = cxlflash_lun_attach(gli, MODE_VIRTUAL, true);
  828. if (unlikely(rc)) {
  829. dev_err(dev, "%s: Failed to attach to LUN! (VIRTUAL)\n",
  830. __func__);
  831. goto err0;
  832. }
  833. mutex_unlock(&gli->mutex);
  834. rc = init_luntable(cfg, lli);
  835. if (rc) {
  836. dev_err(dev, "%s: call to init_luntable failed rc=%d!\n",
  837. __func__, rc);
  838. goto err1;
  839. }
  840. ctxi = get_context(cfg, rctxid, lli, 0);
  841. if (unlikely(!ctxi)) {
  842. dev_err(dev, "%s: Bad context! (%llu)\n", __func__, ctxid);
  843. rc = -EINVAL;
  844. goto err1;
  845. }
  846. rhte = rhte_checkout(ctxi, lli);
  847. if (unlikely(!rhte)) {
  848. dev_err(dev, "%s: too many opens for this context\n", __func__);
  849. rc = -EMFILE; /* too many opens */
  850. goto err1;
  851. }
  852. rsrc_handle = (rhte - ctxi->rht_start);
  853. /* Populate RHT format 0 */
  854. rhte->nmask = MC_RHT_NMASK;
  855. rhte->fp = SISL_RHT_FP(0U, ctxi->rht_perms);
  856. /* Resize even if requested size is 0 */
  857. marshal_virt_to_resize(virt, &resize);
  858. resize.rsrc_handle = rsrc_handle;
  859. rc = _cxlflash_vlun_resize(sdev, ctxi, &resize);
  860. if (rc) {
  861. dev_err(dev, "%s: resize failed rc %d\n", __func__, rc);
  862. goto err2;
  863. }
  864. last_lba = resize.last_lba;
  865. if (virt->hdr.flags & DK_CXLFLASH_UVIRTUAL_NEED_WRITE_SAME)
  866. ctxi->rht_needs_ws[rsrc_handle] = true;
  867. virt->hdr.return_flags = 0;
  868. virt->last_lba = last_lba;
  869. virt->rsrc_handle = rsrc_handle;
  870. if (lli->port_sel == BOTH_PORTS)
  871. virt->hdr.return_flags |= DK_CXLFLASH_ALL_PORTS_ACTIVE;
  872. out:
  873. if (likely(ctxi))
  874. put_context(ctxi);
  875. pr_debug("%s: returning handle 0x%llx rc=%d llba %lld\n",
  876. __func__, rsrc_handle, rc, last_lba);
  877. return rc;
  878. err2:
  879. rhte_checkin(ctxi, rhte);
  880. err1:
  881. cxlflash_lun_detach(gli);
  882. goto out;
  883. err0:
  884. /* Special common cleanup prior to successful LUN attach */
  885. cxlflash_ba_terminate(&gli->blka.ba_lun);
  886. mutex_unlock(&gli->mutex);
  887. goto out;
  888. }
  889. /**
  890. * clone_lxt() - copies translation tables from source to destination RHTE
  891. * @afu: AFU associated with the host.
  892. * @blka: Block allocator associated with LUN.
  893. * @ctxid: Context ID of context owning the RHTE.
  894. * @rhndl: Resource handle associated with the RHTE.
  895. * @rhte: Destination resource handle entry (RHTE).
  896. * @rhte_src: Source resource handle entry (RHTE).
  897. *
  898. * Return: 0 on success, -errno on failure
  899. */
  900. static int clone_lxt(struct afu *afu,
  901. struct blka *blka,
  902. ctx_hndl_t ctxid,
  903. res_hndl_t rhndl,
  904. struct sisl_rht_entry *rhte,
  905. struct sisl_rht_entry *rhte_src)
  906. {
  907. struct sisl_lxt_entry *lxt;
  908. u32 ngrps;
  909. u64 aun; /* chunk# allocated by block allocator */
  910. int i, j;
  911. ngrps = LXT_NUM_GROUPS(rhte_src->lxt_cnt);
  912. if (ngrps) {
  913. /* allocate new LXTs for clone */
  914. lxt = kzalloc((sizeof(*lxt) * LXT_GROUP_SIZE * ngrps),
  915. GFP_KERNEL);
  916. if (unlikely(!lxt))
  917. return -ENOMEM;
  918. /* copy over */
  919. memcpy(lxt, rhte_src->lxt_start,
  920. (sizeof(*lxt) * rhte_src->lxt_cnt));
  921. /* clone the LBAs in block allocator via ref_cnt */
  922. mutex_lock(&blka->mutex);
  923. for (i = 0; i < rhte_src->lxt_cnt; i++) {
  924. aun = (lxt[i].rlba_base >> MC_CHUNK_SHIFT);
  925. if (ba_clone(&blka->ba_lun, aun) == -1ULL) {
  926. /* free the clones already made */
  927. for (j = 0; j < i; j++) {
  928. aun = (lxt[j].rlba_base >>
  929. MC_CHUNK_SHIFT);
  930. ba_free(&blka->ba_lun, aun);
  931. }
  932. mutex_unlock(&blka->mutex);
  933. kfree(lxt);
  934. return -EIO;
  935. }
  936. }
  937. mutex_unlock(&blka->mutex);
  938. } else {
  939. lxt = NULL;
  940. }
  941. /*
  942. * The following sequence is prescribed in the SISlite spec
  943. * for syncing up with the AFU when adding LXT entries.
  944. */
  945. dma_wmb(); /* Make LXT updates are visible */
  946. rhte->lxt_start = lxt;
  947. dma_wmb(); /* Make RHT entry's LXT table update visible */
  948. rhte->lxt_cnt = rhte_src->lxt_cnt;
  949. dma_wmb(); /* Make RHT entry's LXT table size update visible */
  950. cxlflash_afu_sync(afu, ctxid, rhndl, AFU_LW_SYNC);
  951. pr_debug("%s: returning\n", __func__);
  952. return 0;
  953. }
  954. /**
  955. * cxlflash_disk_clone() - clone a context by making snapshot of another
  956. * @sdev: SCSI device associated with LUN owning virtual LUN.
  957. * @clone: Clone ioctl data structure.
  958. *
  959. * This routine effectively performs cxlflash_disk_open operation for each
  960. * in-use virtual resource in the source context. Note that the destination
  961. * context must be in pristine state and cannot have any resource handles
  962. * open at the time of the clone.
  963. *
  964. * Return: 0 on success, -errno on failure
  965. */
  966. int cxlflash_disk_clone(struct scsi_device *sdev,
  967. struct dk_cxlflash_clone *clone)
  968. {
  969. struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
  970. struct llun_info *lli = sdev->hostdata;
  971. struct glun_info *gli = lli->parent;
  972. struct blka *blka = &gli->blka;
  973. struct afu *afu = cfg->afu;
  974. struct dk_cxlflash_release release = { { 0 }, 0 };
  975. struct ctx_info *ctxi_src = NULL,
  976. *ctxi_dst = NULL;
  977. struct lun_access *lun_access_src, *lun_access_dst;
  978. u32 perms;
  979. u64 ctxid_src = DECODE_CTXID(clone->context_id_src),
  980. ctxid_dst = DECODE_CTXID(clone->context_id_dst),
  981. rctxid_src = clone->context_id_src,
  982. rctxid_dst = clone->context_id_dst;
  983. int i, j;
  984. int rc = 0;
  985. bool found;
  986. LIST_HEAD(sidecar);
  987. pr_debug("%s: ctxid_src=%llu ctxid_dst=%llu\n",
  988. __func__, ctxid_src, ctxid_dst);
  989. /* Do not clone yourself */
  990. if (unlikely(rctxid_src == rctxid_dst)) {
  991. rc = -EINVAL;
  992. goto out;
  993. }
  994. if (unlikely(gli->mode != MODE_VIRTUAL)) {
  995. rc = -EINVAL;
  996. pr_debug("%s: Clone not supported on physical LUNs! (%d)\n",
  997. __func__, gli->mode);
  998. goto out;
  999. }
  1000. ctxi_src = get_context(cfg, rctxid_src, lli, CTX_CTRL_CLONE);
  1001. ctxi_dst = get_context(cfg, rctxid_dst, lli, 0);
  1002. if (unlikely(!ctxi_src || !ctxi_dst)) {
  1003. pr_debug("%s: Bad context! (%llu,%llu)\n", __func__,
  1004. ctxid_src, ctxid_dst);
  1005. rc = -EINVAL;
  1006. goto out;
  1007. }
  1008. /* Verify there is no open resource handle in the destination context */
  1009. for (i = 0; i < MAX_RHT_PER_CONTEXT; i++)
  1010. if (ctxi_dst->rht_start[i].nmask != 0) {
  1011. rc = -EINVAL;
  1012. goto out;
  1013. }
  1014. /* Clone LUN access list */
  1015. list_for_each_entry(lun_access_src, &ctxi_src->luns, list) {
  1016. found = false;
  1017. list_for_each_entry(lun_access_dst, &ctxi_dst->luns, list)
  1018. if (lun_access_dst->sdev == lun_access_src->sdev) {
  1019. found = true;
  1020. break;
  1021. }
  1022. if (!found) {
  1023. lun_access_dst = kzalloc(sizeof(*lun_access_dst),
  1024. GFP_KERNEL);
  1025. if (unlikely(!lun_access_dst)) {
  1026. pr_err("%s: Unable to allocate lun_access!\n",
  1027. __func__);
  1028. rc = -ENOMEM;
  1029. goto out;
  1030. }
  1031. *lun_access_dst = *lun_access_src;
  1032. list_add(&lun_access_dst->list, &sidecar);
  1033. }
  1034. }
  1035. if (unlikely(!ctxi_src->rht_out)) {
  1036. pr_debug("%s: Nothing to clone!\n", __func__);
  1037. goto out_success;
  1038. }
  1039. /* User specified permission on attach */
  1040. perms = ctxi_dst->rht_perms;
  1041. /*
  1042. * Copy over checked-out RHT (and their associated LXT) entries by
  1043. * hand, stopping after we've copied all outstanding entries and
  1044. * cleaning up if the clone fails.
  1045. *
  1046. * Note: This loop is equivalent to performing cxlflash_disk_open and
  1047. * cxlflash_vlun_resize. As such, LUN accounting needs to be taken into
  1048. * account by attaching after each successful RHT entry clone. In the
  1049. * event that a clone failure is experienced, the LUN detach is handled
  1050. * via the cleanup performed by _cxlflash_disk_release.
  1051. */
  1052. for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) {
  1053. if (ctxi_src->rht_out == ctxi_dst->rht_out)
  1054. break;
  1055. if (ctxi_src->rht_start[i].nmask == 0)
  1056. continue;
  1057. /* Consume a destination RHT entry */
  1058. ctxi_dst->rht_out++;
  1059. ctxi_dst->rht_start[i].nmask = ctxi_src->rht_start[i].nmask;
  1060. ctxi_dst->rht_start[i].fp =
  1061. SISL_RHT_FP_CLONE(ctxi_src->rht_start[i].fp, perms);
  1062. ctxi_dst->rht_lun[i] = ctxi_src->rht_lun[i];
  1063. rc = clone_lxt(afu, blka, ctxid_dst, i,
  1064. &ctxi_dst->rht_start[i],
  1065. &ctxi_src->rht_start[i]);
  1066. if (rc) {
  1067. marshal_clone_to_rele(clone, &release);
  1068. for (j = 0; j < i; j++) {
  1069. release.rsrc_handle = j;
  1070. _cxlflash_disk_release(sdev, ctxi_dst,
  1071. &release);
  1072. }
  1073. /* Put back the one we failed on */
  1074. rhte_checkin(ctxi_dst, &ctxi_dst->rht_start[i]);
  1075. goto err;
  1076. }
  1077. cxlflash_lun_attach(gli, gli->mode, false);
  1078. }
  1079. out_success:
  1080. list_splice(&sidecar, &ctxi_dst->luns);
  1081. /* fall through */
  1082. out:
  1083. if (ctxi_src)
  1084. put_context(ctxi_src);
  1085. if (ctxi_dst)
  1086. put_context(ctxi_dst);
  1087. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1088. return rc;
  1089. err:
  1090. list_for_each_entry_safe(lun_access_src, lun_access_dst, &sidecar, list)
  1091. kfree(lun_access_src);
  1092. goto out;
  1093. }