Matthew R. Ochs | 2cb7926 | 2015-08-13 21:47:53 -0500 | [diff] [blame] | 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 | * Return: 0 on success, -errno on failure |
| 404 | */ |
| 405 | static int write_same16(struct scsi_device *sdev, |
| 406 | u64 lba, |
| 407 | u32 nblks) |
| 408 | { |
| 409 | u8 *cmd_buf = NULL; |
| 410 | u8 *scsi_cmd = NULL; |
| 411 | u8 *sense_buf = NULL; |
| 412 | int rc = 0; |
| 413 | int result = 0; |
| 414 | int ws_limit = SISLITE_MAX_WS_BLOCKS; |
| 415 | u64 offset = lba; |
| 416 | int left = nblks; |
| 417 | u32 tout = sdev->request_queue->rq_timeout; |
| 418 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata; |
| 419 | struct device *dev = &cfg->dev->dev; |
| 420 | |
| 421 | cmd_buf = kzalloc(CMD_BUFSIZE, GFP_KERNEL); |
| 422 | scsi_cmd = kzalloc(MAX_COMMAND_SIZE, GFP_KERNEL); |
| 423 | sense_buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL); |
| 424 | if (unlikely(!cmd_buf || !scsi_cmd || !sense_buf)) { |
| 425 | rc = -ENOMEM; |
| 426 | goto out; |
| 427 | } |
| 428 | |
| 429 | while (left > 0) { |
| 430 | |
| 431 | scsi_cmd[0] = WRITE_SAME_16; |
| 432 | put_unaligned_be64(offset, &scsi_cmd[2]); |
| 433 | put_unaligned_be32(ws_limit < left ? ws_limit : left, |
| 434 | &scsi_cmd[10]); |
| 435 | |
| 436 | result = scsi_execute(sdev, scsi_cmd, DMA_TO_DEVICE, cmd_buf, |
| 437 | CMD_BUFSIZE, sense_buf, tout, 5, 0, NULL); |
| 438 | if (result) { |
| 439 | dev_err_ratelimited(dev, "%s: command failed for " |
| 440 | "offset %lld result=0x%x\n", |
| 441 | __func__, offset, result); |
| 442 | rc = -EIO; |
| 443 | goto out; |
| 444 | } |
| 445 | left -= ws_limit; |
| 446 | offset += ws_limit; |
| 447 | } |
| 448 | |
| 449 | out: |
| 450 | kfree(cmd_buf); |
| 451 | kfree(scsi_cmd); |
| 452 | kfree(sense_buf); |
| 453 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 454 | return rc; |
| 455 | } |
| 456 | |
| 457 | /** |
| 458 | * grow_lxt() - expands the translation table associated with the specified RHTE |
| 459 | * @afu: AFU associated with the host. |
| 460 | * @sdev: SCSI device associated with LUN. |
| 461 | * @ctxid: Context ID of context owning the RHTE. |
| 462 | * @rhndl: Resource handle associated with the RHTE. |
| 463 | * @rhte: Resource handle entry (RHTE). |
| 464 | * @new_size: Number of translation entries associated with RHTE. |
| 465 | * |
| 466 | * By design, this routine employs a 'best attempt' allocation and will |
| 467 | * truncate the requested size down if there is not sufficient space in |
| 468 | * the block allocator to satisfy the request but there does exist some |
| 469 | * amount of space. The user is made aware of this by returning the size |
| 470 | * allocated. |
| 471 | * |
| 472 | * Return: 0 on success, -errno on failure |
| 473 | */ |
| 474 | static int grow_lxt(struct afu *afu, |
| 475 | struct scsi_device *sdev, |
| 476 | ctx_hndl_t ctxid, |
| 477 | res_hndl_t rhndl, |
| 478 | struct sisl_rht_entry *rhte, |
| 479 | u64 *new_size) |
| 480 | { |
| 481 | struct sisl_lxt_entry *lxt = NULL, *lxt_old = NULL; |
| 482 | struct llun_info *lli = sdev->hostdata; |
| 483 | struct glun_info *gli = lli->parent; |
| 484 | struct blka *blka = &gli->blka; |
| 485 | u32 av_size; |
| 486 | u32 ngrps, ngrps_old; |
| 487 | u64 aun; /* chunk# allocated by block allocator */ |
| 488 | u64 delta = *new_size - rhte->lxt_cnt; |
| 489 | u64 my_new_size; |
| 490 | int i, rc = 0; |
| 491 | |
| 492 | /* |
| 493 | * Check what is available in the block allocator before re-allocating |
| 494 | * LXT array. This is done up front under the mutex which must not be |
| 495 | * released until after allocation is complete. |
| 496 | */ |
| 497 | mutex_lock(&blka->mutex); |
| 498 | av_size = ba_space(&blka->ba_lun); |
| 499 | if (unlikely(av_size <= 0)) { |
| 500 | pr_debug("%s: ba_space error: av_size %d\n", __func__, av_size); |
| 501 | mutex_unlock(&blka->mutex); |
| 502 | rc = -ENOSPC; |
| 503 | goto out; |
| 504 | } |
| 505 | |
| 506 | if (av_size < delta) |
| 507 | delta = av_size; |
| 508 | |
| 509 | lxt_old = rhte->lxt_start; |
| 510 | ngrps_old = LXT_NUM_GROUPS(rhte->lxt_cnt); |
| 511 | ngrps = LXT_NUM_GROUPS(rhte->lxt_cnt + delta); |
| 512 | |
| 513 | if (ngrps != ngrps_old) { |
| 514 | /* reallocate to fit new size */ |
| 515 | lxt = kzalloc((sizeof(*lxt) * LXT_GROUP_SIZE * ngrps), |
| 516 | GFP_KERNEL); |
| 517 | if (unlikely(!lxt)) { |
| 518 | mutex_unlock(&blka->mutex); |
| 519 | rc = -ENOMEM; |
| 520 | goto out; |
| 521 | } |
| 522 | |
| 523 | /* copy over all old entries */ |
| 524 | memcpy(lxt, lxt_old, (sizeof(*lxt) * rhte->lxt_cnt)); |
| 525 | } else |
| 526 | lxt = lxt_old; |
| 527 | |
| 528 | /* nothing can fail from now on */ |
| 529 | my_new_size = rhte->lxt_cnt + delta; |
| 530 | |
| 531 | /* add new entries to the end */ |
| 532 | for (i = rhte->lxt_cnt; i < my_new_size; i++) { |
| 533 | /* |
| 534 | * Due to the earlier check of available space, ba_alloc |
| 535 | * cannot fail here. If it did due to internal error, |
| 536 | * leave a rlba_base of -1u which will likely be a |
| 537 | * invalid LUN (too large). |
| 538 | */ |
| 539 | aun = ba_alloc(&blka->ba_lun); |
| 540 | if ((aun == -1ULL) || (aun >= blka->nchunk)) |
| 541 | pr_debug("%s: ba_alloc error: allocated chunk# %llX, " |
| 542 | "max %llX\n", __func__, aun, blka->nchunk - 1); |
| 543 | |
| 544 | /* select both ports, use r/w perms from RHT */ |
| 545 | lxt[i].rlba_base = ((aun << MC_CHUNK_SHIFT) | |
| 546 | (lli->lun_index << LXT_LUNIDX_SHIFT) | |
| 547 | (RHT_PERM_RW << LXT_PERM_SHIFT | |
| 548 | lli->port_sel)); |
| 549 | } |
| 550 | |
| 551 | mutex_unlock(&blka->mutex); |
| 552 | |
| 553 | /* |
| 554 | * The following sequence is prescribed in the SISlite spec |
| 555 | * for syncing up with the AFU when adding LXT entries. |
| 556 | */ |
| 557 | dma_wmb(); /* Make LXT updates are visible */ |
| 558 | |
| 559 | rhte->lxt_start = lxt; |
| 560 | dma_wmb(); /* Make RHT entry's LXT table update visible */ |
| 561 | |
| 562 | rhte->lxt_cnt = my_new_size; |
| 563 | dma_wmb(); /* Make RHT entry's LXT table size update visible */ |
| 564 | |
| 565 | cxlflash_afu_sync(afu, ctxid, rhndl, AFU_LW_SYNC); |
| 566 | |
| 567 | /* free old lxt if reallocated */ |
| 568 | if (lxt != lxt_old) |
| 569 | kfree(lxt_old); |
| 570 | *new_size = my_new_size; |
| 571 | out: |
| 572 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 573 | return rc; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * shrink_lxt() - reduces translation table associated with the specified RHTE |
| 578 | * @afu: AFU associated with the host. |
| 579 | * @sdev: SCSI device associated with LUN. |
| 580 | * @rhndl: Resource handle associated with the RHTE. |
| 581 | * @rhte: Resource handle entry (RHTE). |
| 582 | * @ctxi: Context owning resources. |
| 583 | * @new_size: Number of translation entries associated with RHTE. |
| 584 | * |
| 585 | * Return: 0 on success, -errno on failure |
| 586 | */ |
| 587 | static int shrink_lxt(struct afu *afu, |
| 588 | struct scsi_device *sdev, |
| 589 | res_hndl_t rhndl, |
| 590 | struct sisl_rht_entry *rhte, |
| 591 | struct ctx_info *ctxi, |
| 592 | u64 *new_size) |
| 593 | { |
| 594 | struct sisl_lxt_entry *lxt, *lxt_old; |
| 595 | struct llun_info *lli = sdev->hostdata; |
| 596 | struct glun_info *gli = lli->parent; |
| 597 | struct blka *blka = &gli->blka; |
| 598 | ctx_hndl_t ctxid = DECODE_CTXID(ctxi->ctxid); |
| 599 | bool needs_ws = ctxi->rht_needs_ws[rhndl]; |
| 600 | bool needs_sync = !ctxi->err_recovery_active; |
| 601 | u32 ngrps, ngrps_old; |
| 602 | u64 aun; /* chunk# allocated by block allocator */ |
| 603 | u64 delta = rhte->lxt_cnt - *new_size; |
| 604 | u64 my_new_size; |
| 605 | int i, rc = 0; |
| 606 | |
| 607 | lxt_old = rhte->lxt_start; |
| 608 | ngrps_old = LXT_NUM_GROUPS(rhte->lxt_cnt); |
| 609 | ngrps = LXT_NUM_GROUPS(rhte->lxt_cnt - delta); |
| 610 | |
| 611 | if (ngrps != ngrps_old) { |
| 612 | /* Reallocate to fit new size unless new size is 0 */ |
| 613 | if (ngrps) { |
| 614 | lxt = kzalloc((sizeof(*lxt) * LXT_GROUP_SIZE * ngrps), |
| 615 | GFP_KERNEL); |
| 616 | if (unlikely(!lxt)) { |
| 617 | rc = -ENOMEM; |
| 618 | goto out; |
| 619 | } |
| 620 | |
| 621 | /* Copy over old entries that will remain */ |
| 622 | memcpy(lxt, lxt_old, |
| 623 | (sizeof(*lxt) * (rhte->lxt_cnt - delta))); |
| 624 | } else |
| 625 | lxt = NULL; |
| 626 | } else |
| 627 | lxt = lxt_old; |
| 628 | |
| 629 | /* Nothing can fail from now on */ |
| 630 | my_new_size = rhte->lxt_cnt - delta; |
| 631 | |
| 632 | /* |
| 633 | * The following sequence is prescribed in the SISlite spec |
| 634 | * for syncing up with the AFU when removing LXT entries. |
| 635 | */ |
| 636 | rhte->lxt_cnt = my_new_size; |
| 637 | dma_wmb(); /* Make RHT entry's LXT table size update visible */ |
| 638 | |
| 639 | rhte->lxt_start = lxt; |
| 640 | dma_wmb(); /* Make RHT entry's LXT table update visible */ |
| 641 | |
| 642 | if (needs_sync) |
| 643 | cxlflash_afu_sync(afu, ctxid, rhndl, AFU_HW_SYNC); |
| 644 | |
| 645 | if (needs_ws) { |
| 646 | /* |
| 647 | * Mark the context as unavailable, so that we can release |
| 648 | * the mutex safely. |
| 649 | */ |
| 650 | ctxi->unavail = true; |
| 651 | mutex_unlock(&ctxi->mutex); |
| 652 | } |
| 653 | |
| 654 | /* Free LBAs allocated to freed chunks */ |
| 655 | mutex_lock(&blka->mutex); |
| 656 | for (i = delta - 1; i >= 0; i--) { |
| 657 | /* Mask the higher 48 bits before shifting, even though |
| 658 | * it is a noop |
| 659 | */ |
| 660 | aun = (lxt_old[my_new_size + i].rlba_base & SISL_ASTATUS_MASK); |
| 661 | aun = (aun >> MC_CHUNK_SHIFT); |
| 662 | if (needs_ws) |
| 663 | write_same16(sdev, aun, MC_CHUNK_SIZE); |
| 664 | ba_free(&blka->ba_lun, aun); |
| 665 | } |
| 666 | mutex_unlock(&blka->mutex); |
| 667 | |
| 668 | if (needs_ws) { |
| 669 | /* Make the context visible again */ |
| 670 | mutex_lock(&ctxi->mutex); |
| 671 | ctxi->unavail = false; |
| 672 | } |
| 673 | |
| 674 | /* Free old lxt if reallocated */ |
| 675 | if (lxt != lxt_old) |
| 676 | kfree(lxt_old); |
| 677 | *new_size = my_new_size; |
| 678 | out: |
| 679 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 680 | return rc; |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * _cxlflash_vlun_resize() - changes the size of a virtual lun |
| 685 | * @sdev: SCSI device associated with LUN owning virtual LUN. |
| 686 | * @ctxi: Context owning resources. |
| 687 | * @resize: Resize ioctl data structure. |
| 688 | * |
| 689 | * On successful return, the user is informed of the new size (in blocks) |
| 690 | * of the virtual lun in last LBA format. When the size of the virtual |
| 691 | * lun is zero, the last LBA is reflected as -1. See comment in the |
| 692 | * prologue for _cxlflash_disk_release() regarding AFU syncs and contexts |
| 693 | * on the error recovery list. |
| 694 | * |
| 695 | * Return: 0 on success, -errno on failure |
| 696 | */ |
| 697 | int _cxlflash_vlun_resize(struct scsi_device *sdev, |
| 698 | struct ctx_info *ctxi, |
| 699 | struct dk_cxlflash_resize *resize) |
| 700 | { |
| 701 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata; |
| 702 | struct llun_info *lli = sdev->hostdata; |
| 703 | struct glun_info *gli = lli->parent; |
| 704 | struct afu *afu = cfg->afu; |
| 705 | bool put_ctx = false; |
| 706 | |
| 707 | res_hndl_t rhndl = resize->rsrc_handle; |
| 708 | u64 new_size; |
| 709 | u64 nsectors; |
| 710 | u64 ctxid = DECODE_CTXID(resize->context_id), |
| 711 | rctxid = resize->context_id; |
| 712 | |
| 713 | struct sisl_rht_entry *rhte; |
| 714 | |
| 715 | int rc = 0; |
| 716 | |
| 717 | /* |
| 718 | * The requested size (req_size) is always assumed to be in 4k blocks, |
| 719 | * so we have to convert it here from 4k to chunk size. |
| 720 | */ |
| 721 | nsectors = (resize->req_size * CXLFLASH_BLOCK_SIZE) / gli->blk_len; |
| 722 | new_size = DIV_ROUND_UP(nsectors, MC_CHUNK_SIZE); |
| 723 | |
| 724 | pr_debug("%s: ctxid=%llu rhndl=0x%llx, req_size=0x%llx," |
| 725 | "new_size=%llx\n", __func__, ctxid, resize->rsrc_handle, |
| 726 | resize->req_size, new_size); |
| 727 | |
| 728 | if (unlikely(gli->mode != MODE_VIRTUAL)) { |
| 729 | pr_debug("%s: LUN mode does not support resize! (%d)\n", |
| 730 | __func__, gli->mode); |
| 731 | rc = -EINVAL; |
| 732 | goto out; |
| 733 | |
| 734 | } |
| 735 | |
| 736 | if (!ctxi) { |
| 737 | ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK); |
| 738 | if (unlikely(!ctxi)) { |
| 739 | pr_debug("%s: Bad context! (%llu)\n", __func__, ctxid); |
| 740 | rc = -EINVAL; |
| 741 | goto out; |
| 742 | } |
| 743 | |
| 744 | put_ctx = true; |
| 745 | } |
| 746 | |
| 747 | rhte = get_rhte(ctxi, rhndl, lli); |
| 748 | if (unlikely(!rhte)) { |
| 749 | pr_debug("%s: Bad resource handle! (%u)\n", __func__, rhndl); |
| 750 | rc = -EINVAL; |
| 751 | goto out; |
| 752 | } |
| 753 | |
| 754 | if (new_size > rhte->lxt_cnt) |
| 755 | rc = grow_lxt(afu, sdev, ctxid, rhndl, rhte, &new_size); |
| 756 | else if (new_size < rhte->lxt_cnt) |
| 757 | rc = shrink_lxt(afu, sdev, rhndl, rhte, ctxi, &new_size); |
| 758 | |
| 759 | resize->hdr.return_flags = 0; |
| 760 | resize->last_lba = (new_size * MC_CHUNK_SIZE * gli->blk_len); |
| 761 | resize->last_lba /= CXLFLASH_BLOCK_SIZE; |
| 762 | resize->last_lba--; |
| 763 | |
| 764 | out: |
| 765 | if (put_ctx) |
| 766 | put_context(ctxi); |
| 767 | pr_debug("%s: resized to %lld returning rc=%d\n", |
| 768 | __func__, resize->last_lba, rc); |
| 769 | return rc; |
| 770 | } |
| 771 | |
| 772 | int cxlflash_vlun_resize(struct scsi_device *sdev, |
| 773 | struct dk_cxlflash_resize *resize) |
| 774 | { |
| 775 | return _cxlflash_vlun_resize(sdev, NULL, resize); |
| 776 | } |
| 777 | |
| 778 | /** |
| 779 | * cxlflash_restore_luntable() - Restore LUN table to prior state |
| 780 | * @cfg: Internal structure associated with the host. |
| 781 | */ |
| 782 | void cxlflash_restore_luntable(struct cxlflash_cfg *cfg) |
| 783 | { |
| 784 | struct llun_info *lli, *temp; |
| 785 | u32 chan; |
| 786 | u32 lind; |
| 787 | struct afu *afu = cfg->afu; |
| 788 | struct sisl_global_map *agm = &afu->afu_map->global; |
| 789 | |
| 790 | mutex_lock(&global.mutex); |
| 791 | |
| 792 | list_for_each_entry_safe(lli, temp, &cfg->lluns, list) { |
| 793 | if (!lli->in_table) |
| 794 | continue; |
| 795 | |
| 796 | lind = lli->lun_index; |
| 797 | |
| 798 | if (lli->port_sel == BOTH_PORTS) { |
| 799 | writeq_be(lli->lun_id[0], &agm->fc_port[0][lind]); |
| 800 | writeq_be(lli->lun_id[1], &agm->fc_port[1][lind]); |
| 801 | pr_debug("%s: Virtual LUN on slot %d id0=%llx, " |
| 802 | "id1=%llx\n", __func__, lind, |
| 803 | lli->lun_id[0], lli->lun_id[1]); |
| 804 | } else { |
| 805 | chan = PORT2CHAN(lli->port_sel); |
| 806 | writeq_be(lli->lun_id[chan], &agm->fc_port[chan][lind]); |
| 807 | pr_debug("%s: Virtual LUN on slot %d chan=%d, " |
| 808 | "id=%llx\n", __func__, lind, chan, |
| 809 | lli->lun_id[chan]); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | mutex_unlock(&global.mutex); |
| 814 | } |
| 815 | |
| 816 | /** |
| 817 | * init_luntable() - write an entry in the LUN table |
| 818 | * @cfg: Internal structure associated with the host. |
| 819 | * @lli: Per adapter LUN information structure. |
| 820 | * |
| 821 | * On successful return, a LUN table entry is created. |
| 822 | * At the top for LUNs visible on both ports. |
| 823 | * At the bottom for LUNs visible only on one port. |
| 824 | * |
| 825 | * Return: 0 on success, -errno on failure |
| 826 | */ |
| 827 | static int init_luntable(struct cxlflash_cfg *cfg, struct llun_info *lli) |
| 828 | { |
| 829 | u32 chan; |
| 830 | u32 lind; |
| 831 | int rc = 0; |
| 832 | struct afu *afu = cfg->afu; |
| 833 | struct sisl_global_map *agm = &afu->afu_map->global; |
| 834 | |
| 835 | mutex_lock(&global.mutex); |
| 836 | |
| 837 | if (lli->in_table) |
| 838 | goto out; |
| 839 | |
| 840 | if (lli->port_sel == BOTH_PORTS) { |
| 841 | /* |
| 842 | * If this LUN is visible from both ports, we will put |
| 843 | * it in the top half of the LUN table. |
| 844 | */ |
| 845 | if ((cfg->promote_lun_index == cfg->last_lun_index[0]) || |
| 846 | (cfg->promote_lun_index == cfg->last_lun_index[1])) { |
| 847 | rc = -ENOSPC; |
| 848 | goto out; |
| 849 | } |
| 850 | |
| 851 | lind = lli->lun_index = cfg->promote_lun_index; |
| 852 | writeq_be(lli->lun_id[0], &agm->fc_port[0][lind]); |
| 853 | writeq_be(lli->lun_id[1], &agm->fc_port[1][lind]); |
| 854 | cfg->promote_lun_index++; |
| 855 | pr_debug("%s: Virtual LUN on slot %d id0=%llx, id1=%llx\n", |
| 856 | __func__, lind, lli->lun_id[0], lli->lun_id[1]); |
| 857 | } else { |
| 858 | /* |
| 859 | * If this LUN is visible only from one port, we will put |
| 860 | * it in the bottom half of the LUN table. |
| 861 | */ |
| 862 | chan = PORT2CHAN(lli->port_sel); |
| 863 | if (cfg->promote_lun_index == cfg->last_lun_index[chan]) { |
| 864 | rc = -ENOSPC; |
| 865 | goto out; |
| 866 | } |
| 867 | |
| 868 | lind = lli->lun_index = cfg->last_lun_index[chan]; |
| 869 | writeq_be(lli->lun_id[chan], &agm->fc_port[chan][lind]); |
| 870 | cfg->last_lun_index[chan]--; |
| 871 | pr_debug("%s: Virtual LUN on slot %d chan=%d, id=%llx\n", |
| 872 | __func__, lind, chan, lli->lun_id[chan]); |
| 873 | } |
| 874 | |
| 875 | lli->in_table = true; |
| 876 | out: |
| 877 | mutex_unlock(&global.mutex); |
| 878 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 879 | return rc; |
| 880 | } |
| 881 | |
| 882 | /** |
| 883 | * cxlflash_disk_virtual_open() - open a virtual disk of specified size |
| 884 | * @sdev: SCSI device associated with LUN owning virtual LUN. |
| 885 | * @arg: UVirtual ioctl data structure. |
| 886 | * |
| 887 | * On successful return, the user is informed of the resource handle |
| 888 | * to be used to identify the virtual lun and the size (in blocks) of |
| 889 | * the virtual lun in last LBA format. When the size of the virtual lun |
| 890 | * is zero, the last LBA is reflected as -1. |
| 891 | * |
| 892 | * Return: 0 on success, -errno on failure |
| 893 | */ |
| 894 | int cxlflash_disk_virtual_open(struct scsi_device *sdev, void *arg) |
| 895 | { |
| 896 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata; |
| 897 | struct device *dev = &cfg->dev->dev; |
| 898 | struct llun_info *lli = sdev->hostdata; |
| 899 | struct glun_info *gli = lli->parent; |
| 900 | |
| 901 | struct dk_cxlflash_uvirtual *virt = (struct dk_cxlflash_uvirtual *)arg; |
| 902 | struct dk_cxlflash_resize resize; |
| 903 | |
| 904 | u64 ctxid = DECODE_CTXID(virt->context_id), |
| 905 | rctxid = virt->context_id; |
| 906 | u64 lun_size = virt->lun_size; |
| 907 | u64 last_lba = 0; |
| 908 | u64 rsrc_handle = -1; |
| 909 | |
| 910 | int rc = 0; |
| 911 | |
| 912 | struct ctx_info *ctxi = NULL; |
| 913 | struct sisl_rht_entry *rhte = NULL; |
| 914 | |
| 915 | pr_debug("%s: ctxid=%llu ls=0x%llx\n", __func__, ctxid, lun_size); |
| 916 | |
| 917 | mutex_lock(&gli->mutex); |
| 918 | if (gli->mode == MODE_NONE) { |
| 919 | /* Setup the LUN table and block allocator on first call */ |
| 920 | rc = init_luntable(cfg, lli); |
| 921 | if (rc) { |
| 922 | dev_err(dev, "%s: call to init_luntable failed " |
| 923 | "rc=%d!\n", __func__, rc); |
| 924 | goto err0; |
| 925 | } |
| 926 | |
| 927 | rc = init_vlun(lli); |
| 928 | if (rc) { |
| 929 | dev_err(dev, "%s: call to init_vlun failed rc=%d!\n", |
| 930 | __func__, rc); |
| 931 | rc = -ENOMEM; |
| 932 | goto err0; |
| 933 | } |
| 934 | } |
| 935 | |
| 936 | rc = cxlflash_lun_attach(gli, MODE_VIRTUAL, true); |
| 937 | if (unlikely(rc)) { |
| 938 | dev_err(dev, "%s: Failed to attach to LUN! (VIRTUAL)\n", |
| 939 | __func__); |
| 940 | goto err0; |
| 941 | } |
| 942 | mutex_unlock(&gli->mutex); |
| 943 | |
| 944 | ctxi = get_context(cfg, rctxid, lli, 0); |
| 945 | if (unlikely(!ctxi)) { |
| 946 | dev_err(dev, "%s: Bad context! (%llu)\n", __func__, ctxid); |
| 947 | rc = -EINVAL; |
| 948 | goto err1; |
| 949 | } |
| 950 | |
| 951 | rhte = rhte_checkout(ctxi, lli); |
| 952 | if (unlikely(!rhte)) { |
| 953 | dev_err(dev, "%s: too many opens for this context\n", __func__); |
| 954 | rc = -EMFILE; /* too many opens */ |
| 955 | goto err1; |
| 956 | } |
| 957 | |
| 958 | rsrc_handle = (rhte - ctxi->rht_start); |
| 959 | |
| 960 | /* Populate RHT format 0 */ |
| 961 | rhte->nmask = MC_RHT_NMASK; |
| 962 | rhte->fp = SISL_RHT_FP(0U, ctxi->rht_perms); |
| 963 | |
| 964 | /* Resize even if requested size is 0 */ |
| 965 | marshal_virt_to_resize(virt, &resize); |
| 966 | resize.rsrc_handle = rsrc_handle; |
| 967 | rc = _cxlflash_vlun_resize(sdev, ctxi, &resize); |
| 968 | if (rc) { |
| 969 | dev_err(dev, "%s: resize failed rc %d\n", __func__, rc); |
| 970 | goto err2; |
| 971 | } |
| 972 | last_lba = resize.last_lba; |
| 973 | |
| 974 | if (virt->hdr.flags & DK_CXLFLASH_UVIRTUAL_NEED_WRITE_SAME) |
| 975 | ctxi->rht_needs_ws[rsrc_handle] = true; |
| 976 | |
| 977 | virt->hdr.return_flags = 0; |
| 978 | virt->last_lba = last_lba; |
| 979 | virt->rsrc_handle = rsrc_handle; |
| 980 | |
| 981 | out: |
| 982 | if (likely(ctxi)) |
| 983 | put_context(ctxi); |
| 984 | pr_debug("%s: returning handle 0x%llx rc=%d llba %lld\n", |
| 985 | __func__, rsrc_handle, rc, last_lba); |
| 986 | return rc; |
| 987 | |
| 988 | err2: |
| 989 | rhte_checkin(ctxi, rhte); |
| 990 | err1: |
| 991 | cxlflash_lun_detach(gli); |
| 992 | goto out; |
| 993 | err0: |
| 994 | /* Special common cleanup prior to successful LUN attach */ |
| 995 | cxlflash_ba_terminate(&gli->blka.ba_lun); |
| 996 | mutex_unlock(&gli->mutex); |
| 997 | goto out; |
| 998 | } |
| 999 | |
| 1000 | /** |
| 1001 | * clone_lxt() - copies translation tables from source to destination RHTE |
| 1002 | * @afu: AFU associated with the host. |
| 1003 | * @blka: Block allocator associated with LUN. |
| 1004 | * @ctxid: Context ID of context owning the RHTE. |
| 1005 | * @rhndl: Resource handle associated with the RHTE. |
| 1006 | * @rhte: Destination resource handle entry (RHTE). |
| 1007 | * @rhte_src: Source resource handle entry (RHTE). |
| 1008 | * |
| 1009 | * Return: 0 on success, -errno on failure |
| 1010 | */ |
| 1011 | static int clone_lxt(struct afu *afu, |
| 1012 | struct blka *blka, |
| 1013 | ctx_hndl_t ctxid, |
| 1014 | res_hndl_t rhndl, |
| 1015 | struct sisl_rht_entry *rhte, |
| 1016 | struct sisl_rht_entry *rhte_src) |
| 1017 | { |
| 1018 | struct sisl_lxt_entry *lxt; |
| 1019 | u32 ngrps; |
| 1020 | u64 aun; /* chunk# allocated by block allocator */ |
| 1021 | int i, j; |
| 1022 | |
| 1023 | ngrps = LXT_NUM_GROUPS(rhte_src->lxt_cnt); |
| 1024 | |
| 1025 | if (ngrps) { |
| 1026 | /* allocate new LXTs for clone */ |
| 1027 | lxt = kzalloc((sizeof(*lxt) * LXT_GROUP_SIZE * ngrps), |
| 1028 | GFP_KERNEL); |
| 1029 | if (unlikely(!lxt)) |
| 1030 | return -ENOMEM; |
| 1031 | |
| 1032 | /* copy over */ |
| 1033 | memcpy(lxt, rhte_src->lxt_start, |
| 1034 | (sizeof(*lxt) * rhte_src->lxt_cnt)); |
| 1035 | |
| 1036 | /* clone the LBAs in block allocator via ref_cnt */ |
| 1037 | mutex_lock(&blka->mutex); |
| 1038 | for (i = 0; i < rhte_src->lxt_cnt; i++) { |
| 1039 | aun = (lxt[i].rlba_base >> MC_CHUNK_SHIFT); |
| 1040 | if (ba_clone(&blka->ba_lun, aun) == -1ULL) { |
| 1041 | /* free the clones already made */ |
| 1042 | for (j = 0; j < i; j++) { |
| 1043 | aun = (lxt[j].rlba_base >> |
| 1044 | MC_CHUNK_SHIFT); |
| 1045 | ba_free(&blka->ba_lun, aun); |
| 1046 | } |
| 1047 | |
| 1048 | mutex_unlock(&blka->mutex); |
| 1049 | kfree(lxt); |
| 1050 | return -EIO; |
| 1051 | } |
| 1052 | } |
| 1053 | mutex_unlock(&blka->mutex); |
| 1054 | } else { |
| 1055 | lxt = NULL; |
| 1056 | } |
| 1057 | |
| 1058 | /* |
| 1059 | * The following sequence is prescribed in the SISlite spec |
| 1060 | * for syncing up with the AFU when adding LXT entries. |
| 1061 | */ |
| 1062 | dma_wmb(); /* Make LXT updates are visible */ |
| 1063 | |
| 1064 | rhte->lxt_start = lxt; |
| 1065 | dma_wmb(); /* Make RHT entry's LXT table update visible */ |
| 1066 | |
| 1067 | rhte->lxt_cnt = rhte_src->lxt_cnt; |
| 1068 | dma_wmb(); /* Make RHT entry's LXT table size update visible */ |
| 1069 | |
| 1070 | cxlflash_afu_sync(afu, ctxid, rhndl, AFU_LW_SYNC); |
| 1071 | |
| 1072 | pr_debug("%s: returning\n", __func__); |
| 1073 | return 0; |
| 1074 | } |
| 1075 | |
| 1076 | /** |
| 1077 | * cxlflash_disk_clone() - clone a context by making snapshot of another |
| 1078 | * @sdev: SCSI device associated with LUN owning virtual LUN. |
| 1079 | * @clone: Clone ioctl data structure. |
| 1080 | * |
| 1081 | * This routine effectively performs cxlflash_disk_open operation for each |
| 1082 | * in-use virtual resource in the source context. Note that the destination |
| 1083 | * context must be in pristine state and cannot have any resource handles |
| 1084 | * open at the time of the clone. |
| 1085 | * |
| 1086 | * Return: 0 on success, -errno on failure |
| 1087 | */ |
| 1088 | int cxlflash_disk_clone(struct scsi_device *sdev, |
| 1089 | struct dk_cxlflash_clone *clone) |
| 1090 | { |
| 1091 | struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata; |
| 1092 | struct llun_info *lli = sdev->hostdata; |
| 1093 | struct glun_info *gli = lli->parent; |
| 1094 | struct blka *blka = &gli->blka; |
| 1095 | struct afu *afu = cfg->afu; |
| 1096 | struct dk_cxlflash_release release = { { 0 }, 0 }; |
| 1097 | |
| 1098 | struct ctx_info *ctxi_src = NULL, |
| 1099 | *ctxi_dst = NULL; |
| 1100 | struct lun_access *lun_access_src, *lun_access_dst; |
| 1101 | u32 perms; |
| 1102 | u64 ctxid_src = DECODE_CTXID(clone->context_id_src), |
| 1103 | ctxid_dst = DECODE_CTXID(clone->context_id_dst), |
| 1104 | rctxid_src = clone->context_id_src, |
| 1105 | rctxid_dst = clone->context_id_dst; |
| 1106 | int adap_fd_src = clone->adap_fd_src; |
| 1107 | int i, j; |
| 1108 | int rc = 0; |
| 1109 | bool found; |
| 1110 | LIST_HEAD(sidecar); |
| 1111 | |
| 1112 | pr_debug("%s: ctxid_src=%llu ctxid_dst=%llu adap_fd_src=%d\n", |
| 1113 | __func__, ctxid_src, ctxid_dst, adap_fd_src); |
| 1114 | |
| 1115 | /* Do not clone yourself */ |
| 1116 | if (unlikely(rctxid_src == rctxid_dst)) { |
| 1117 | rc = -EINVAL; |
| 1118 | goto out; |
| 1119 | } |
| 1120 | |
| 1121 | if (unlikely(gli->mode != MODE_VIRTUAL)) { |
| 1122 | rc = -EINVAL; |
| 1123 | pr_debug("%s: Clone not supported on physical LUNs! (%d)\n", |
| 1124 | __func__, gli->mode); |
| 1125 | goto out; |
| 1126 | } |
| 1127 | |
| 1128 | ctxi_src = get_context(cfg, rctxid_src, lli, CTX_CTRL_CLONE); |
| 1129 | ctxi_dst = get_context(cfg, rctxid_dst, lli, 0); |
| 1130 | if (unlikely(!ctxi_src || !ctxi_dst)) { |
| 1131 | pr_debug("%s: Bad context! (%llu,%llu)\n", __func__, |
| 1132 | ctxid_src, ctxid_dst); |
| 1133 | rc = -EINVAL; |
| 1134 | goto out; |
| 1135 | } |
| 1136 | |
| 1137 | if (unlikely(adap_fd_src != ctxi_src->lfd)) { |
| 1138 | pr_debug("%s: Invalid source adapter fd! (%d)\n", |
| 1139 | __func__, adap_fd_src); |
| 1140 | rc = -EINVAL; |
| 1141 | goto out; |
| 1142 | } |
| 1143 | |
| 1144 | /* Verify there is no open resource handle in the destination context */ |
| 1145 | for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) |
| 1146 | if (ctxi_dst->rht_start[i].nmask != 0) { |
| 1147 | rc = -EINVAL; |
| 1148 | goto out; |
| 1149 | } |
| 1150 | |
| 1151 | /* Clone LUN access list */ |
| 1152 | list_for_each_entry(lun_access_src, &ctxi_src->luns, list) { |
| 1153 | found = false; |
| 1154 | list_for_each_entry(lun_access_dst, &ctxi_dst->luns, list) |
| 1155 | if (lun_access_dst->sdev == lun_access_src->sdev) { |
| 1156 | found = true; |
| 1157 | break; |
| 1158 | } |
| 1159 | |
| 1160 | if (!found) { |
| 1161 | lun_access_dst = kzalloc(sizeof(*lun_access_dst), |
| 1162 | GFP_KERNEL); |
| 1163 | if (unlikely(!lun_access_dst)) { |
| 1164 | pr_err("%s: Unable to allocate lun_access!\n", |
| 1165 | __func__); |
| 1166 | rc = -ENOMEM; |
| 1167 | goto out; |
| 1168 | } |
| 1169 | |
| 1170 | *lun_access_dst = *lun_access_src; |
| 1171 | list_add(&lun_access_dst->list, &sidecar); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | if (unlikely(!ctxi_src->rht_out)) { |
| 1176 | pr_debug("%s: Nothing to clone!\n", __func__); |
| 1177 | goto out_success; |
| 1178 | } |
| 1179 | |
| 1180 | /* User specified permission on attach */ |
| 1181 | perms = ctxi_dst->rht_perms; |
| 1182 | |
| 1183 | /* |
| 1184 | * Copy over checked-out RHT (and their associated LXT) entries by |
| 1185 | * hand, stopping after we've copied all outstanding entries and |
| 1186 | * cleaning up if the clone fails. |
| 1187 | * |
| 1188 | * Note: This loop is equivalent to performing cxlflash_disk_open and |
| 1189 | * cxlflash_vlun_resize. As such, LUN accounting needs to be taken into |
| 1190 | * account by attaching after each successful RHT entry clone. In the |
| 1191 | * event that a clone failure is experienced, the LUN detach is handled |
| 1192 | * via the cleanup performed by _cxlflash_disk_release. |
| 1193 | */ |
| 1194 | for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) { |
| 1195 | if (ctxi_src->rht_out == ctxi_dst->rht_out) |
| 1196 | break; |
| 1197 | if (ctxi_src->rht_start[i].nmask == 0) |
| 1198 | continue; |
| 1199 | |
| 1200 | /* Consume a destination RHT entry */ |
| 1201 | ctxi_dst->rht_out++; |
| 1202 | ctxi_dst->rht_start[i].nmask = ctxi_src->rht_start[i].nmask; |
| 1203 | ctxi_dst->rht_start[i].fp = |
| 1204 | SISL_RHT_FP_CLONE(ctxi_src->rht_start[i].fp, perms); |
| 1205 | ctxi_dst->rht_lun[i] = ctxi_src->rht_lun[i]; |
| 1206 | |
| 1207 | rc = clone_lxt(afu, blka, ctxid_dst, i, |
| 1208 | &ctxi_dst->rht_start[i], |
| 1209 | &ctxi_src->rht_start[i]); |
| 1210 | if (rc) { |
| 1211 | marshal_clone_to_rele(clone, &release); |
| 1212 | for (j = 0; j < i; j++) { |
| 1213 | release.rsrc_handle = j; |
| 1214 | _cxlflash_disk_release(sdev, ctxi_dst, |
| 1215 | &release); |
| 1216 | } |
| 1217 | |
| 1218 | /* Put back the one we failed on */ |
| 1219 | rhte_checkin(ctxi_dst, &ctxi_dst->rht_start[i]); |
| 1220 | goto err; |
| 1221 | } |
| 1222 | |
| 1223 | cxlflash_lun_attach(gli, gli->mode, false); |
| 1224 | } |
| 1225 | |
| 1226 | out_success: |
| 1227 | list_splice(&sidecar, &ctxi_dst->luns); |
| 1228 | sys_close(adap_fd_src); |
| 1229 | |
| 1230 | /* fall through */ |
| 1231 | out: |
| 1232 | if (ctxi_src) |
| 1233 | put_context(ctxi_src); |
| 1234 | if (ctxi_dst) |
| 1235 | put_context(ctxi_dst); |
| 1236 | pr_debug("%s: returning rc=%d\n", __func__, rc); |
| 1237 | return rc; |
| 1238 | |
| 1239 | err: |
| 1240 | list_for_each_entry_safe(lun_access_src, lun_access_dst, &sidecar, list) |
| 1241 | kfree(lun_access_src); |
| 1242 | goto out; |
| 1243 | } |