vlun.c 36 KB

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