vlun.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275
  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 adap_fd_src = clone->adap_fd_src;
  984. int i, j;
  985. int rc = 0;
  986. bool found;
  987. LIST_HEAD(sidecar);
  988. pr_debug("%s: ctxid_src=%llu ctxid_dst=%llu adap_fd_src=%d\n",
  989. __func__, ctxid_src, ctxid_dst, adap_fd_src);
  990. /* Do not clone yourself */
  991. if (unlikely(rctxid_src == rctxid_dst)) {
  992. rc = -EINVAL;
  993. goto out;
  994. }
  995. if (unlikely(gli->mode != MODE_VIRTUAL)) {
  996. rc = -EINVAL;
  997. pr_debug("%s: Clone not supported on physical LUNs! (%d)\n",
  998. __func__, gli->mode);
  999. goto out;
  1000. }
  1001. ctxi_src = get_context(cfg, rctxid_src, lli, CTX_CTRL_CLONE);
  1002. ctxi_dst = get_context(cfg, rctxid_dst, lli, 0);
  1003. if (unlikely(!ctxi_src || !ctxi_dst)) {
  1004. pr_debug("%s: Bad context! (%llu,%llu)\n", __func__,
  1005. ctxid_src, ctxid_dst);
  1006. rc = -EINVAL;
  1007. goto out;
  1008. }
  1009. if (unlikely(adap_fd_src != ctxi_src->lfd)) {
  1010. pr_debug("%s: Invalid source adapter fd! (%d)\n",
  1011. __func__, adap_fd_src);
  1012. rc = -EINVAL;
  1013. goto out;
  1014. }
  1015. /* Verify there is no open resource handle in the destination context */
  1016. for (i = 0; i < MAX_RHT_PER_CONTEXT; i++)
  1017. if (ctxi_dst->rht_start[i].nmask != 0) {
  1018. rc = -EINVAL;
  1019. goto out;
  1020. }
  1021. /* Clone LUN access list */
  1022. list_for_each_entry(lun_access_src, &ctxi_src->luns, list) {
  1023. found = false;
  1024. list_for_each_entry(lun_access_dst, &ctxi_dst->luns, list)
  1025. if (lun_access_dst->sdev == lun_access_src->sdev) {
  1026. found = true;
  1027. break;
  1028. }
  1029. if (!found) {
  1030. lun_access_dst = kzalloc(sizeof(*lun_access_dst),
  1031. GFP_KERNEL);
  1032. if (unlikely(!lun_access_dst)) {
  1033. pr_err("%s: Unable to allocate lun_access!\n",
  1034. __func__);
  1035. rc = -ENOMEM;
  1036. goto out;
  1037. }
  1038. *lun_access_dst = *lun_access_src;
  1039. list_add(&lun_access_dst->list, &sidecar);
  1040. }
  1041. }
  1042. if (unlikely(!ctxi_src->rht_out)) {
  1043. pr_debug("%s: Nothing to clone!\n", __func__);
  1044. goto out_success;
  1045. }
  1046. /* User specified permission on attach */
  1047. perms = ctxi_dst->rht_perms;
  1048. /*
  1049. * Copy over checked-out RHT (and their associated LXT) entries by
  1050. * hand, stopping after we've copied all outstanding entries and
  1051. * cleaning up if the clone fails.
  1052. *
  1053. * Note: This loop is equivalent to performing cxlflash_disk_open and
  1054. * cxlflash_vlun_resize. As such, LUN accounting needs to be taken into
  1055. * account by attaching after each successful RHT entry clone. In the
  1056. * event that a clone failure is experienced, the LUN detach is handled
  1057. * via the cleanup performed by _cxlflash_disk_release.
  1058. */
  1059. for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) {
  1060. if (ctxi_src->rht_out == ctxi_dst->rht_out)
  1061. break;
  1062. if (ctxi_src->rht_start[i].nmask == 0)
  1063. continue;
  1064. /* Consume a destination RHT entry */
  1065. ctxi_dst->rht_out++;
  1066. ctxi_dst->rht_start[i].nmask = ctxi_src->rht_start[i].nmask;
  1067. ctxi_dst->rht_start[i].fp =
  1068. SISL_RHT_FP_CLONE(ctxi_src->rht_start[i].fp, perms);
  1069. ctxi_dst->rht_lun[i] = ctxi_src->rht_lun[i];
  1070. rc = clone_lxt(afu, blka, ctxid_dst, i,
  1071. &ctxi_dst->rht_start[i],
  1072. &ctxi_src->rht_start[i]);
  1073. if (rc) {
  1074. marshal_clone_to_rele(clone, &release);
  1075. for (j = 0; j < i; j++) {
  1076. release.rsrc_handle = j;
  1077. _cxlflash_disk_release(sdev, ctxi_dst,
  1078. &release);
  1079. }
  1080. /* Put back the one we failed on */
  1081. rhte_checkin(ctxi_dst, &ctxi_dst->rht_start[i]);
  1082. goto err;
  1083. }
  1084. cxlflash_lun_attach(gli, gli->mode, false);
  1085. }
  1086. out_success:
  1087. list_splice(&sidecar, &ctxi_dst->luns);
  1088. sys_close(adap_fd_src);
  1089. /* fall through */
  1090. out:
  1091. if (ctxi_src)
  1092. put_context(ctxi_src);
  1093. if (ctxi_dst)
  1094. put_context(ctxi_dst);
  1095. pr_debug("%s: returning rc=%d\n", __func__, rc);
  1096. return rc;
  1097. err:
  1098. list_for_each_entry_safe(lun_access_src, lun_access_dst, &sidecar, list)
  1099. kfree(lun_access_src);
  1100. goto out;
  1101. }