Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -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 <misc/cxl.h> |
| 16 | #include <asm/unaligned.h> |
| 17 | |
| 18 | #include <scsi/scsi_host.h> |
| 19 | #include <uapi/scsi/cxlflash_ioctl.h> |
| 20 | |
| 21 | #include "sislite.h" |
| 22 | #include "common.h" |
Matthew R. Ochs | 2cb7926 | 2015-08-13 21:47:53 -0500 | [diff] [blame] | 23 | #include "vlun.h" |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 24 | #include "superpipe.h" |
| 25 | |
| 26 | /** |
| 27 | * create_local() - allocate and initialize a local LUN information structure |
| 28 | * @sdev: SCSI device associated with LUN. |
| 29 | * @wwid: World Wide Node Name for LUN. |
| 30 | * |
| 31 | * Return: Allocated local llun_info structure on success, NULL on failure |
| 32 | */ |
| 33 | static struct llun_info *create_local(struct scsi_device *sdev, u8 *wwid) |
| 34 | { |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 35 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
| 36 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 37 | struct llun_info *lli = NULL; |
| 38 | |
| 39 | lli = kzalloc(sizeof(*lli), GFP_KERNEL); |
| 40 | if (unlikely(!lli)) { |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 41 | dev_err(dev, "%s: could not allocate lli\n", __func__); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 42 | goto out; |
| 43 | } |
| 44 | |
| 45 | lli->sdev = sdev; |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 46 | lli->host_no = sdev->host->host_no; |
Matthew R. Ochs | 2cb7926 | 2015-08-13 21:47:53 -0500 | [diff] [blame] | 47 | lli->in_table = false; |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 48 | |
| 49 | memcpy(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN); |
| 50 | out: |
| 51 | return lli; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * create_global() - allocate and initialize a global LUN information structure |
| 56 | * @sdev: SCSI device associated with LUN. |
| 57 | * @wwid: World Wide Node Name for LUN. |
| 58 | * |
| 59 | * Return: Allocated global glun_info structure on success, NULL on failure |
| 60 | */ |
| 61 | static struct glun_info *create_global(struct scsi_device *sdev, u8 *wwid) |
| 62 | { |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 63 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
| 64 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 65 | struct glun_info *gli = NULL; |
| 66 | |
| 67 | gli = kzalloc(sizeof(*gli), GFP_KERNEL); |
| 68 | if (unlikely(!gli)) { |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 69 | dev_err(dev, "%s: could not allocate gli\n", __func__); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 70 | goto out; |
| 71 | } |
| 72 | |
| 73 | mutex_init(&gli->mutex); |
| 74 | memcpy(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN); |
| 75 | out: |
| 76 | return gli; |
| 77 | } |
| 78 | |
| 79 | /** |
Matthew R. Ochs | 1a47401 | 2015-10-21 15:16:15 -0500 | [diff] [blame] | 80 | * lookup_local() - find a local LUN information structure by WWID |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 81 | * @cfg: Internal structure associated with the host. |
| 82 | * @wwid: WWID associated with LUN. |
| 83 | * |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 84 | * Return: Found local lun_info structure on success, NULL on failure |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 85 | */ |
Matthew R. Ochs | 1a47401 | 2015-10-21 15:16:15 -0500 | [diff] [blame] | 86 | static struct llun_info *lookup_local(struct cxlflash_cfg *cfg, u8 *wwid) |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 87 | { |
| 88 | struct llun_info *lli, *temp; |
| 89 | |
| 90 | list_for_each_entry_safe(lli, temp, &cfg->lluns, list) |
Matthew R. Ochs | 1a47401 | 2015-10-21 15:16:15 -0500 | [diff] [blame] | 91 | if (!memcmp(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN)) |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 92 | return lli; |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 93 | |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * lookup_global() - find a global LUN information structure by WWID |
| 99 | * @wwid: WWID associated with LUN. |
| 100 | * |
| 101 | * Return: Found global lun_info structure on success, NULL on failure |
| 102 | */ |
| 103 | static struct glun_info *lookup_global(u8 *wwid) |
| 104 | { |
| 105 | struct glun_info *gli, *temp; |
| 106 | |
| 107 | list_for_each_entry_safe(gli, temp, &global.gluns, list) |
| 108 | if (!memcmp(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN)) |
| 109 | return gli; |
| 110 | |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * find_and_create_lun() - find or create a local LUN information structure |
| 116 | * @sdev: SCSI device associated with LUN. |
| 117 | * @wwid: WWID associated with LUN. |
| 118 | * |
| 119 | * The LUN is kept both in a local list (per adapter) and in a global list |
| 120 | * (across all adapters). Certain attributes of the LUN are local to the |
Manoj Kumar | fa4aa63 | 2015-10-21 15:10:31 -0500 | [diff] [blame] | 121 | * adapter (such as index, port selection mask, etc.). |
| 122 | * |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 123 | * The block allocation map is shared across all adapters (i.e. associated |
| 124 | * wih the global list). Since different attributes are associated with |
| 125 | * the per adapter and global entries, allocate two separate structures for each |
| 126 | * LUN (one local, one global). |
| 127 | * |
| 128 | * Keep a pointer back from the local to the global entry. |
| 129 | * |
Manoj Kumar | fa4aa63 | 2015-10-21 15:10:31 -0500 | [diff] [blame] | 130 | * This routine assumes the caller holds the global mutex. |
| 131 | * |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 132 | * Return: Found/Allocated local lun_info structure on success, NULL on failure |
| 133 | */ |
| 134 | static struct llun_info *find_and_create_lun(struct scsi_device *sdev, u8 *wwid) |
| 135 | { |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 136 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
| 137 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 138 | struct llun_info *lli = NULL; |
| 139 | struct glun_info *gli = NULL; |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 140 | |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 141 | if (unlikely(!wwid)) |
| 142 | goto out; |
| 143 | |
Matthew R. Ochs | 1a47401 | 2015-10-21 15:16:15 -0500 | [diff] [blame] | 144 | lli = lookup_local(cfg, wwid); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 145 | if (lli) |
| 146 | goto out; |
| 147 | |
| 148 | lli = create_local(sdev, wwid); |
| 149 | if (unlikely(!lli)) |
| 150 | goto out; |
| 151 | |
| 152 | gli = lookup_global(wwid); |
| 153 | if (gli) { |
| 154 | lli->parent = gli; |
| 155 | list_add(&lli->list, &cfg->lluns); |
| 156 | goto out; |
| 157 | } |
| 158 | |
| 159 | gli = create_global(sdev, wwid); |
| 160 | if (unlikely(!gli)) { |
| 161 | kfree(lli); |
| 162 | lli = NULL; |
| 163 | goto out; |
| 164 | } |
| 165 | |
| 166 | lli->parent = gli; |
| 167 | list_add(&lli->list, &cfg->lluns); |
| 168 | |
| 169 | list_add(&gli->list, &global.gluns); |
| 170 | |
| 171 | out: |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 172 | dev_dbg(dev, "%s: returning lli=%p, gli=%p\n", __func__, lli, gli); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 173 | return lli; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * cxlflash_term_local_luns() - Delete all entries from local LUN list, free. |
| 178 | * @cfg: Internal structure associated with the host. |
| 179 | */ |
| 180 | void cxlflash_term_local_luns(struct cxlflash_cfg *cfg) |
| 181 | { |
| 182 | struct llun_info *lli, *temp; |
| 183 | |
| 184 | mutex_lock(&global.mutex); |
| 185 | list_for_each_entry_safe(lli, temp, &cfg->lluns, list) { |
| 186 | list_del(&lli->list); |
| 187 | kfree(lli); |
| 188 | } |
| 189 | mutex_unlock(&global.mutex); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * cxlflash_list_init() - initializes the global LUN list |
| 194 | */ |
| 195 | void cxlflash_list_init(void) |
| 196 | { |
| 197 | INIT_LIST_HEAD(&global.gluns); |
| 198 | mutex_init(&global.mutex); |
| 199 | global.err_page = NULL; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * cxlflash_term_global_luns() - frees resources associated with global LUN list |
| 204 | */ |
| 205 | void cxlflash_term_global_luns(void) |
| 206 | { |
| 207 | struct glun_info *gli, *temp; |
| 208 | |
| 209 | mutex_lock(&global.mutex); |
| 210 | list_for_each_entry_safe(gli, temp, &global.gluns, list) { |
| 211 | list_del(&gli->list); |
Matthew R. Ochs | 2cb7926 | 2015-08-13 21:47:53 -0500 | [diff] [blame] | 212 | cxlflash_ba_terminate(&gli->blka.ba_lun); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 213 | kfree(gli); |
| 214 | } |
| 215 | mutex_unlock(&global.mutex); |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * cxlflash_manage_lun() - handles LUN management activities |
| 220 | * @sdev: SCSI device associated with LUN. |
| 221 | * @manage: Manage ioctl data structure. |
| 222 | * |
| 223 | * This routine is used to notify the driver about a LUN's WWID and associate |
| 224 | * SCSI devices (sdev) with a global LUN instance. Additionally it serves to |
| 225 | * change a LUN's operating mode: legacy or superpipe. |
| 226 | * |
| 227 | * Return: 0 on success, -errno on failure |
| 228 | */ |
| 229 | int cxlflash_manage_lun(struct scsi_device *sdev, |
| 230 | struct dk_cxlflash_manage_lun *manage) |
| 231 | { |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 232 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
| 233 | struct device *dev = &cfg->dev->dev; |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 234 | struct llun_info *lli = NULL; |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 235 | int rc = 0; |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 236 | u64 flags = manage->hdr.flags; |
| 237 | u32 chan = sdev->channel; |
| 238 | |
Manoj Kumar | fa4aa63 | 2015-10-21 15:10:31 -0500 | [diff] [blame] | 239 | mutex_lock(&global.mutex); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 240 | lli = find_and_create_lun(sdev, manage->wwid); |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 241 | dev_dbg(dev, "%s: WWID=%016llx%016llx, flags=%016llx lli=%p\n", |
| 242 | __func__, get_unaligned_be64(&manage->wwid[0]), |
| 243 | get_unaligned_be64(&manage->wwid[8]), manage->hdr.flags, lli); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 244 | if (unlikely(!lli)) { |
| 245 | rc = -ENOMEM; |
| 246 | goto out; |
| 247 | } |
| 248 | |
| 249 | if (flags & DK_CXLFLASH_MANAGE_LUN_ENABLE_SUPERPIPE) { |
Matthew R. Ochs | 1a47401 | 2015-10-21 15:16:15 -0500 | [diff] [blame] | 250 | /* |
| 251 | * Update port selection mask based upon channel, store off LUN |
| 252 | * in unpacked, AFU-friendly format, and hang LUN reference in |
| 253 | * the sdev. |
| 254 | */ |
Matthew R. Ochs | 8fa4f17 | 2017-04-12 14:14:05 -0500 | [diff] [blame] | 255 | lli->port_sel |= CHAN2PORTMASK(chan); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 256 | lli->lun_id[chan] = lun_to_lunid(sdev->lun); |
| 257 | sdev->hostdata = lli; |
| 258 | } else if (flags & DK_CXLFLASH_MANAGE_LUN_DISABLE_SUPERPIPE) { |
| 259 | if (lli->parent->mode != MODE_NONE) |
| 260 | rc = -EBUSY; |
Matthew R. Ochs | 1a47401 | 2015-10-21 15:16:15 -0500 | [diff] [blame] | 261 | else { |
Uma Krishnan | 8a26054 | 2016-11-28 18:41:19 -0600 | [diff] [blame] | 262 | /* |
| 263 | * Clean up local LUN for this port and reset table |
| 264 | * tracking when no more references exist. |
| 265 | */ |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 266 | sdev->hostdata = NULL; |
Matthew R. Ochs | 8fa4f17 | 2017-04-12 14:14:05 -0500 | [diff] [blame] | 267 | lli->port_sel &= ~CHAN2PORTMASK(chan); |
Uma Krishnan | 8a26054 | 2016-11-28 18:41:19 -0600 | [diff] [blame] | 268 | if (lli->port_sel == 0U) |
| 269 | lli->in_table = false; |
Matthew R. Ochs | 1a47401 | 2015-10-21 15:16:15 -0500 | [diff] [blame] | 270 | } |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 273 | dev_dbg(dev, "%s: port_sel=%08x chan=%u lun_id=%016llx\n", |
| 274 | __func__, lli->port_sel, chan, lli->lun_id[chan]); |
Matthew R. Ochs | 1a47401 | 2015-10-21 15:16:15 -0500 | [diff] [blame] | 275 | |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 276 | out: |
Manoj Kumar | fa4aa63 | 2015-10-21 15:10:31 -0500 | [diff] [blame] | 277 | mutex_unlock(&global.mutex); |
Matthew R. Ochs | fb67d44 | 2017-01-11 19:19:47 -0600 | [diff] [blame] | 278 | dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc); |
Matthew R. Ochs | 65be2c7 | 2015-08-13 21:47:43 -0500 | [diff] [blame] | 279 | return rc; |
| 280 | } |