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