2 * CXL Flash Device Driver
4 * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
5 * Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
7 * Copyright (C) 2015 IBM Corporation
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.
16 #include <asm/unaligned.h>
18 #include <scsi/scsi_host.h>
19 #include <uapi/scsi/cxlflash_ioctl.h>
23 #include "superpipe.h"
26 * create_local() - allocate and initialize a local LUN information structure
27 * @sdev: SCSI device associated with LUN.
28 * @wwid: World Wide Node Name for LUN.
30 * Return: Allocated local llun_info structure on success, NULL on failure
32 static struct llun_info *create_local(struct scsi_device *sdev, u8 *wwid)
34 struct llun_info *lli = NULL;
36 lli = kzalloc(sizeof(*lli), GFP_KERNEL);
38 pr_err("%s: could not allocate lli\n", __func__);
43 lli->newly_created = true;
44 lli->host_no = sdev->host->host_no;
46 memcpy(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
52 * create_global() - allocate and initialize a global LUN information structure
53 * @sdev: SCSI device associated with LUN.
54 * @wwid: World Wide Node Name for LUN.
56 * Return: Allocated global glun_info structure on success, NULL on failure
58 static struct glun_info *create_global(struct scsi_device *sdev, u8 *wwid)
60 struct glun_info *gli = NULL;
62 gli = kzalloc(sizeof(*gli), GFP_KERNEL);
64 pr_err("%s: could not allocate gli\n", __func__);
68 mutex_init(&gli->mutex);
69 memcpy(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
75 * refresh_local() - find and update local LUN information structure by WWID
76 * @cfg: Internal structure associated with the host.
77 * @wwid: WWID associated with LUN.
79 * When the LUN is found, mark it by updating it's newly_created field.
81 * Return: Found local lun_info structure on success, NULL on failure
82 * If a LUN with the WWID is found in the list, refresh it's state.
84 static struct llun_info *refresh_local(struct cxlflash_cfg *cfg, u8 *wwid)
86 struct llun_info *lli, *temp;
88 list_for_each_entry_safe(lli, temp, &cfg->lluns, list)
89 if (!memcmp(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN)) {
90 lli->newly_created = false;
98 * lookup_global() - find a global LUN information structure by WWID
99 * @wwid: WWID associated with LUN.
101 * Return: Found global lun_info structure on success, NULL on failure
103 static struct glun_info *lookup_global(u8 *wwid)
105 struct glun_info *gli, *temp;
107 list_for_each_entry_safe(gli, temp, &global.gluns, list)
108 if (!memcmp(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN))
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.
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 * The block allocation map is shared across all adapters (i.e. associated
123 * wih the global list). Since different attributes are associated with
124 * the per adapter and global entries, allocate two separate structures for each
125 * LUN (one local, one global).
127 * Keep a pointer back from the local to the global entry.
129 * Return: Found/Allocated local lun_info structure on success, NULL on failure
131 static struct llun_info *find_and_create_lun(struct scsi_device *sdev, u8 *wwid)
133 struct llun_info *lli = NULL;
134 struct glun_info *gli = NULL;
135 struct Scsi_Host *shost = sdev->host;
136 struct cxlflash_cfg *cfg = shost_priv(shost);
138 mutex_lock(&global.mutex);
142 lli = refresh_local(cfg, wwid);
146 lli = create_local(sdev, wwid);
150 gli = lookup_global(wwid);
153 list_add(&lli->list, &cfg->lluns);
157 gli = create_global(sdev, wwid);
158 if (unlikely(!gli)) {
165 list_add(&lli->list, &cfg->lluns);
167 list_add(&gli->list, &global.gluns);
170 mutex_unlock(&global.mutex);
171 pr_debug("%s: returning %p\n", __func__, lli);
176 * cxlflash_term_local_luns() - Delete all entries from local LUN list, free.
177 * @cfg: Internal structure associated with the host.
179 void cxlflash_term_local_luns(struct cxlflash_cfg *cfg)
181 struct llun_info *lli, *temp;
183 mutex_lock(&global.mutex);
184 list_for_each_entry_safe(lli, temp, &cfg->lluns, list) {
185 list_del(&lli->list);
188 mutex_unlock(&global.mutex);
192 * cxlflash_list_init() - initializes the global LUN list
194 void cxlflash_list_init(void)
196 INIT_LIST_HEAD(&global.gluns);
197 mutex_init(&global.mutex);
198 global.err_page = NULL;
202 * cxlflash_term_global_luns() - frees resources associated with global LUN list
204 void cxlflash_term_global_luns(void)
206 struct glun_info *gli, *temp;
208 mutex_lock(&global.mutex);
209 list_for_each_entry_safe(gli, temp, &global.gluns, list) {
210 list_del(&gli->list);
213 mutex_unlock(&global.mutex);
217 * cxlflash_manage_lun() - handles LUN management activities
218 * @sdev: SCSI device associated with LUN.
219 * @manage: Manage ioctl data structure.
221 * This routine is used to notify the driver about a LUN's WWID and associate
222 * SCSI devices (sdev) with a global LUN instance. Additionally it serves to
223 * change a LUN's operating mode: legacy or superpipe.
225 * Return: 0 on success, -errno on failure
227 int cxlflash_manage_lun(struct scsi_device *sdev,
228 struct dk_cxlflash_manage_lun *manage)
231 struct llun_info *lli = NULL;
232 u64 flags = manage->hdr.flags;
233 u32 chan = sdev->channel;
235 lli = find_and_create_lun(sdev, manage->wwid);
236 pr_debug("%s: ENTER: WWID = %016llX%016llX, flags = %016llX li = %p\n",
237 __func__, get_unaligned_le64(&manage->wwid[0]),
238 get_unaligned_le64(&manage->wwid[8]),
239 manage->hdr.flags, lli);
240 if (unlikely(!lli)) {
245 if (flags & DK_CXLFLASH_MANAGE_LUN_ENABLE_SUPERPIPE) {
246 if (lli->newly_created)
247 lli->port_sel = CHAN2PORT(chan);
249 lli->port_sel = BOTH_PORTS;
250 /* Store off lun in unpacked, AFU-friendly format */
251 lli->lun_id[chan] = lun_to_lunid(sdev->lun);
252 sdev->hostdata = lli;
253 } else if (flags & DK_CXLFLASH_MANAGE_LUN_DISABLE_SUPERPIPE) {
254 if (lli->parent->mode != MODE_NONE)
257 sdev->hostdata = NULL;
261 pr_debug("%s: returning rc=%d\n", __func__, rc);