Merge branch 'r6040-next'
[linux/fpc-iii.git] / drivers / scsi / cxlflash / lunmgt.c
bloba0923cade6f3ead93b53dbb868e492df08635c51
1 /*
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 <misc/cxl.h>
16 #include <asm/unaligned.h>
18 #include <scsi/scsi_host.h>
19 #include <uapi/scsi/cxlflash_ioctl.h>
21 #include "sislite.h"
22 #include "common.h"
23 #include "vlun.h"
24 #include "superpipe.h"
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.
31 * Return: Allocated local llun_info structure on success, NULL on failure
33 static struct llun_info *create_local(struct scsi_device *sdev, u8 *wwid)
35 struct llun_info *lli = NULL;
37 lli = kzalloc(sizeof(*lli), GFP_KERNEL);
38 if (unlikely(!lli)) {
39 pr_err("%s: could not allocate lli\n", __func__);
40 goto out;
43 lli->sdev = sdev;
44 lli->host_no = sdev->host->host_no;
45 lli->in_table = false;
47 memcpy(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
48 out:
49 return lli;
52 /**
53 * create_global() - allocate and initialize a global LUN information structure
54 * @sdev: SCSI device associated with LUN.
55 * @wwid: World Wide Node Name for LUN.
57 * Return: Allocated global glun_info structure on success, NULL on failure
59 static struct glun_info *create_global(struct scsi_device *sdev, u8 *wwid)
61 struct glun_info *gli = NULL;
63 gli = kzalloc(sizeof(*gli), GFP_KERNEL);
64 if (unlikely(!gli)) {
65 pr_err("%s: could not allocate gli\n", __func__);
66 goto out;
69 mutex_init(&gli->mutex);
70 memcpy(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
71 out:
72 return gli;
75 /**
76 * lookup_local() - find a local LUN information structure by WWID
77 * @cfg: Internal structure associated with the host.
78 * @wwid: WWID associated with LUN.
80 * Return: Found local lun_info structure on success, NULL on failure
82 static struct llun_info *lookup_local(struct cxlflash_cfg *cfg, u8 *wwid)
84 struct llun_info *lli, *temp;
86 list_for_each_entry_safe(lli, temp, &cfg->lluns, list)
87 if (!memcmp(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN))
88 return lli;
90 return NULL;
93 /**
94 * lookup_global() - find a global LUN information structure by WWID
95 * @wwid: WWID associated with LUN.
97 * Return: Found global lun_info structure on success, NULL on failure
99 static struct glun_info *lookup_global(u8 *wwid)
101 struct glun_info *gli, *temp;
103 list_for_each_entry_safe(gli, temp, &global.gluns, list)
104 if (!memcmp(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN))
105 return gli;
107 return NULL;
111 * find_and_create_lun() - find or create a local LUN information structure
112 * @sdev: SCSI device associated with LUN.
113 * @wwid: WWID associated with LUN.
115 * The LUN is kept both in a local list (per adapter) and in a global list
116 * (across all adapters). Certain attributes of the LUN are local to the
117 * adapter (such as index, port selection mask, etc.).
119 * The block allocation map is shared across all adapters (i.e. associated
120 * wih the global list). Since different attributes are associated with
121 * the per adapter and global entries, allocate two separate structures for each
122 * LUN (one local, one global).
124 * Keep a pointer back from the local to the global entry.
126 * This routine assumes the caller holds the global mutex.
128 * Return: Found/Allocated local lun_info structure on success, NULL on failure
130 static struct llun_info *find_and_create_lun(struct scsi_device *sdev, u8 *wwid)
132 struct llun_info *lli = NULL;
133 struct glun_info *gli = NULL;
134 struct Scsi_Host *shost = sdev->host;
135 struct cxlflash_cfg *cfg = shost_priv(shost);
137 if (unlikely(!wwid))
138 goto out;
140 lli = lookup_local(cfg, wwid);
141 if (lli)
142 goto out;
144 lli = create_local(sdev, wwid);
145 if (unlikely(!lli))
146 goto out;
148 gli = lookup_global(wwid);
149 if (gli) {
150 lli->parent = gli;
151 list_add(&lli->list, &cfg->lluns);
152 goto out;
155 gli = create_global(sdev, wwid);
156 if (unlikely(!gli)) {
157 kfree(lli);
158 lli = NULL;
159 goto out;
162 lli->parent = gli;
163 list_add(&lli->list, &cfg->lluns);
165 list_add(&gli->list, &global.gluns);
167 out:
168 pr_debug("%s: returning %p\n", __func__, lli);
169 return lli;
173 * cxlflash_term_local_luns() - Delete all entries from local LUN list, free.
174 * @cfg: Internal structure associated with the host.
176 void cxlflash_term_local_luns(struct cxlflash_cfg *cfg)
178 struct llun_info *lli, *temp;
180 mutex_lock(&global.mutex);
181 list_for_each_entry_safe(lli, temp, &cfg->lluns, list) {
182 list_del(&lli->list);
183 kfree(lli);
185 mutex_unlock(&global.mutex);
189 * cxlflash_list_init() - initializes the global LUN list
191 void cxlflash_list_init(void)
193 INIT_LIST_HEAD(&global.gluns);
194 mutex_init(&global.mutex);
195 global.err_page = NULL;
199 * cxlflash_term_global_luns() - frees resources associated with global LUN list
201 void cxlflash_term_global_luns(void)
203 struct glun_info *gli, *temp;
205 mutex_lock(&global.mutex);
206 list_for_each_entry_safe(gli, temp, &global.gluns, list) {
207 list_del(&gli->list);
208 cxlflash_ba_terminate(&gli->blka.ba_lun);
209 kfree(gli);
211 mutex_unlock(&global.mutex);
215 * cxlflash_manage_lun() - handles LUN management activities
216 * @sdev: SCSI device associated with LUN.
217 * @manage: Manage ioctl data structure.
219 * This routine is used to notify the driver about a LUN's WWID and associate
220 * SCSI devices (sdev) with a global LUN instance. Additionally it serves to
221 * change a LUN's operating mode: legacy or superpipe.
223 * Return: 0 on success, -errno on failure
225 int cxlflash_manage_lun(struct scsi_device *sdev,
226 struct dk_cxlflash_manage_lun *manage)
228 int rc = 0;
229 struct llun_info *lli = NULL;
230 u64 flags = manage->hdr.flags;
231 u32 chan = sdev->channel;
233 mutex_lock(&global.mutex);
234 lli = find_and_create_lun(sdev, manage->wwid);
235 pr_debug("%s: ENTER: WWID = %016llX%016llX, flags = %016llX li = %p\n",
236 __func__, get_unaligned_be64(&manage->wwid[0]),
237 get_unaligned_be64(&manage->wwid[8]),
238 manage->hdr.flags, lli);
239 if (unlikely(!lli)) {
240 rc = -ENOMEM;
241 goto out;
244 if (flags & DK_CXLFLASH_MANAGE_LUN_ENABLE_SUPERPIPE) {
246 * Update port selection mask based upon channel, store off LUN
247 * in unpacked, AFU-friendly format, and hang LUN reference in
248 * the sdev.
250 lli->port_sel |= CHAN2PORT(chan);
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)
255 rc = -EBUSY;
256 else {
257 sdev->hostdata = NULL;
258 lli->port_sel &= ~CHAN2PORT(chan);
262 pr_debug("%s: port_sel = %08X chan = %u lun_id = %016llX\n", __func__,
263 lli->port_sel, chan, lli->lun_id[chan]);
265 out:
266 mutex_unlock(&global.mutex);
267 pr_debug("%s: returning rc=%d\n", __func__, rc);
268 return rc;