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" 23 #include "vlun.h" 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 { 35 struct cxlflash_cfg *cfg = shost_priv(sdev->host); 36 struct device *dev = &cfg->dev->dev; 37 struct llun_info *lli = NULL; 38 39 lli = kzalloc(sizeof(*lli), GFP_KERNEL); 40 if (unlikely(!lli)) { 41 dev_err(dev, "%s: could not allocate lli\n", __func__); 42 goto out; 43 } 44 45 lli->sdev = sdev; 46 lli->host_no = sdev->host->host_no; 47 lli->in_table = false; 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 { 63 struct cxlflash_cfg *cfg = shost_priv(sdev->host); 64 struct device *dev = &cfg->dev->dev; 65 struct glun_info *gli = NULL; 66 67 gli = kzalloc(sizeof(*gli), GFP_KERNEL); 68 if (unlikely(!gli)) { 69 dev_err(dev, "%s: could not allocate gli\n", __func__); 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 /** 80 * lookup_local() - find a local LUN information structure by WWID 81 * @cfg: Internal structure associated with the host. 82 * @wwid: WWID associated with LUN. 83 * 84 * Return: Found local lun_info structure on success, NULL on failure 85 */ 86 static struct llun_info *lookup_local(struct cxlflash_cfg *cfg, u8 *wwid) 87 { 88 struct llun_info *lli, *temp; 89 90 list_for_each_entry_safe(lli, temp, &cfg->lluns, list) 91 if (!memcmp(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN)) 92 return lli; 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 121 * adapter (such as index, port selection mask, etc.). 122 * 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 * 130 * This routine assumes the caller holds the global mutex. 131 * 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 { 136 struct cxlflash_cfg *cfg = shost_priv(sdev->host); 137 struct device *dev = &cfg->dev->dev; 138 struct llun_info *lli = NULL; 139 struct glun_info *gli = NULL; 140 141 if (unlikely(!wwid)) 142 goto out; 143 144 lli = lookup_local(cfg, wwid); 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: 172 dev_dbg(dev, "%s: returning lli=%p, gli=%p\n", __func__, lli, gli); 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); 212 cxlflash_ba_terminate(&gli->blka.ba_lun); 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 { 232 struct cxlflash_cfg *cfg = shost_priv(sdev->host); 233 struct device *dev = &cfg->dev->dev; 234 struct llun_info *lli = NULL; 235 int rc = 0; 236 u64 flags = manage->hdr.flags; 237 u32 chan = sdev->channel; 238 239 mutex_lock(&global.mutex); 240 lli = find_and_create_lun(sdev, manage->wwid); 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); 244 if (unlikely(!lli)) { 245 rc = -ENOMEM; 246 goto out; 247 } 248 249 if (flags & DK_CXLFLASH_MANAGE_LUN_ENABLE_SUPERPIPE) { 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 */ 255 lli->port_sel |= CHAN2PORTMASK(chan); 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; 261 else { 262 /* 263 * Clean up local LUN for this port and reset table 264 * tracking when no more references exist. 265 */ 266 sdev->hostdata = NULL; 267 lli->port_sel &= ~CHAN2PORTMASK(chan); 268 if (lli->port_sel == 0U) 269 lli->in_table = false; 270 } 271 } 272 273 dev_dbg(dev, "%s: port_sel=%08x chan=%u lun_id=%016llx\n", 274 __func__, lli->port_sel, chan, lli->lun_id[chan]); 275 276 out: 277 mutex_unlock(&global.mutex); 278 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc); 279 return rc; 280 } 281