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.
15 #include <asm/unaligned.h>
17 #include <linux/interrupt.h>
18 #include <linux/pci.h>
20 #include <scsi/scsi_host.h>
21 #include <uapi/scsi/cxlflash_ioctl.h>
26 #include "superpipe.h"
29 * create_local() - allocate and initialize a local LUN information structure
30 * @sdev: SCSI device associated with LUN.
31 * @wwid: World Wide Node Name for LUN.
33 * Return: Allocated local llun_info structure on success, NULL on failure
35 static struct llun_info
*create_local(struct scsi_device
*sdev
, u8
*wwid
)
37 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
38 struct device
*dev
= &cfg
->dev
->dev
;
39 struct llun_info
*lli
= NULL
;
41 lli
= kzalloc(sizeof(*lli
), GFP_KERNEL
);
43 dev_err(dev
, "%s: could not allocate lli\n", __func__
);
48 lli
->host_no
= sdev
->host
->host_no
;
49 lli
->in_table
= false;
51 memcpy(lli
->wwid
, wwid
, DK_CXLFLASH_MANAGE_LUN_WWID_LEN
);
57 * create_global() - allocate and initialize a global LUN information structure
58 * @sdev: SCSI device associated with LUN.
59 * @wwid: World Wide Node Name for LUN.
61 * Return: Allocated global glun_info structure on success, NULL on failure
63 static struct glun_info
*create_global(struct scsi_device
*sdev
, u8
*wwid
)
65 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
66 struct device
*dev
= &cfg
->dev
->dev
;
67 struct glun_info
*gli
= NULL
;
69 gli
= kzalloc(sizeof(*gli
), GFP_KERNEL
);
71 dev_err(dev
, "%s: could not allocate gli\n", __func__
);
75 mutex_init(&gli
->mutex
);
76 memcpy(gli
->wwid
, wwid
, DK_CXLFLASH_MANAGE_LUN_WWID_LEN
);
82 * lookup_local() - find a local LUN information structure by WWID
83 * @cfg: Internal structure associated with the host.
84 * @wwid: WWID associated with LUN.
86 * Return: Found local lun_info structure on success, NULL on failure
88 static struct llun_info
*lookup_local(struct cxlflash_cfg
*cfg
, u8
*wwid
)
90 struct llun_info
*lli
, *temp
;
92 list_for_each_entry_safe(lli
, temp
, &cfg
->lluns
, list
)
93 if (!memcmp(lli
->wwid
, wwid
, DK_CXLFLASH_MANAGE_LUN_WWID_LEN
))
100 * lookup_global() - find a global LUN information structure by WWID
101 * @wwid: WWID associated with LUN.
103 * Return: Found global lun_info structure on success, NULL on failure
105 static struct glun_info
*lookup_global(u8
*wwid
)
107 struct glun_info
*gli
, *temp
;
109 list_for_each_entry_safe(gli
, temp
, &global
.gluns
, list
)
110 if (!memcmp(gli
->wwid
, wwid
, DK_CXLFLASH_MANAGE_LUN_WWID_LEN
))
117 * find_and_create_lun() - find or create a local LUN information structure
118 * @sdev: SCSI device associated with LUN.
119 * @wwid: WWID associated with LUN.
121 * The LUN is kept both in a local list (per adapter) and in a global list
122 * (across all adapters). Certain attributes of the LUN are local to the
123 * adapter (such as index, port selection mask, etc.).
125 * The block allocation map is shared across all adapters (i.e. associated
126 * wih the global list). Since different attributes are associated with
127 * the per adapter and global entries, allocate two separate structures for each
128 * LUN (one local, one global).
130 * Keep a pointer back from the local to the global entry.
132 * This routine assumes the caller holds the global mutex.
134 * Return: Found/Allocated local lun_info structure on success, NULL on failure
136 static struct llun_info
*find_and_create_lun(struct scsi_device
*sdev
, u8
*wwid
)
138 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
139 struct device
*dev
= &cfg
->dev
->dev
;
140 struct llun_info
*lli
= NULL
;
141 struct glun_info
*gli
= NULL
;
146 lli
= lookup_local(cfg
, wwid
);
150 lli
= create_local(sdev
, wwid
);
154 gli
= lookup_global(wwid
);
157 list_add(&lli
->list
, &cfg
->lluns
);
161 gli
= create_global(sdev
, wwid
);
162 if (unlikely(!gli
)) {
169 list_add(&lli
->list
, &cfg
->lluns
);
171 list_add(&gli
->list
, &global
.gluns
);
174 dev_dbg(dev
, "%s: returning lli=%p, gli=%p\n", __func__
, lli
, gli
);
179 * cxlflash_term_local_luns() - Delete all entries from local LUN list, free.
180 * @cfg: Internal structure associated with the host.
182 void cxlflash_term_local_luns(struct cxlflash_cfg
*cfg
)
184 struct llun_info
*lli
, *temp
;
186 mutex_lock(&global
.mutex
);
187 list_for_each_entry_safe(lli
, temp
, &cfg
->lluns
, list
) {
188 list_del(&lli
->list
);
191 mutex_unlock(&global
.mutex
);
195 * cxlflash_list_init() - initializes the global LUN list
197 void cxlflash_list_init(void)
199 INIT_LIST_HEAD(&global
.gluns
);
200 mutex_init(&global
.mutex
);
201 global
.err_page
= NULL
;
205 * cxlflash_term_global_luns() - frees resources associated with global LUN list
207 void cxlflash_term_global_luns(void)
209 struct glun_info
*gli
, *temp
;
211 mutex_lock(&global
.mutex
);
212 list_for_each_entry_safe(gli
, temp
, &global
.gluns
, list
) {
213 list_del(&gli
->list
);
214 cxlflash_ba_terminate(&gli
->blka
.ba_lun
);
217 mutex_unlock(&global
.mutex
);
221 * cxlflash_manage_lun() - handles LUN management activities
222 * @sdev: SCSI device associated with LUN.
223 * @manage: Manage ioctl data structure.
225 * This routine is used to notify the driver about a LUN's WWID and associate
226 * SCSI devices (sdev) with a global LUN instance. Additionally it serves to
227 * change a LUN's operating mode: legacy or superpipe.
229 * Return: 0 on success, -errno on failure
231 int cxlflash_manage_lun(struct scsi_device
*sdev
,
232 struct dk_cxlflash_manage_lun
*manage
)
234 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
235 struct device
*dev
= &cfg
->dev
->dev
;
236 struct llun_info
*lli
= NULL
;
238 u64 flags
= manage
->hdr
.flags
;
239 u32 chan
= sdev
->channel
;
241 mutex_lock(&global
.mutex
);
242 lli
= find_and_create_lun(sdev
, manage
->wwid
);
243 dev_dbg(dev
, "%s: WWID=%016llx%016llx, flags=%016llx lli=%p\n",
244 __func__
, get_unaligned_be64(&manage
->wwid
[0]),
245 get_unaligned_be64(&manage
->wwid
[8]), manage
->hdr
.flags
, lli
);
246 if (unlikely(!lli
)) {
251 if (flags
& DK_CXLFLASH_MANAGE_LUN_ENABLE_SUPERPIPE
) {
253 * Update port selection mask based upon channel, store off LUN
254 * in unpacked, AFU-friendly format, and hang LUN reference in
257 lli
->port_sel
|= CHAN2PORTMASK(chan
);
258 lli
->lun_id
[chan
] = lun_to_lunid(sdev
->lun
);
259 sdev
->hostdata
= lli
;
260 } else if (flags
& DK_CXLFLASH_MANAGE_LUN_DISABLE_SUPERPIPE
) {
261 if (lli
->parent
->mode
!= MODE_NONE
)
265 * Clean up local LUN for this port and reset table
266 * tracking when no more references exist.
268 sdev
->hostdata
= NULL
;
269 lli
->port_sel
&= ~CHAN2PORTMASK(chan
);
270 if (lli
->port_sel
== 0U)
271 lli
->in_table
= false;
275 dev_dbg(dev
, "%s: port_sel=%08x chan=%u lun_id=%016llx\n",
276 __func__
, lli
->port_sel
, chan
, lli
->lun_id
[chan
]);
279 mutex_unlock(&global
.mutex
);
280 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);