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