staging: fsl-mc: remove dpmng API files
[linux-2.6/btrfs-unstable.git] / drivers / scsi / cxlflash / lunmgt.c
blob4d232e271af60edad6316f15e8cc1689b2e807ed
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 cxlflash_cfg *cfg = shost_priv(sdev->host);
36 struct device *dev = &cfg->dev->dev;
37 struct llun_info *lli = NULL;
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;
45 lli->sdev = sdev;
46 lli->host_no = sdev->host->host_no;
47 lli->in_table = false;
49 memcpy(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
50 out:
51 return lli;
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.
59 * Return: Allocated global glun_info structure on success, NULL on failure
61 static struct glun_info *create_global(struct scsi_device *sdev, u8 *wwid)
63 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
64 struct device *dev = &cfg->dev->dev;
65 struct glun_info *gli = NULL;
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;
73 mutex_init(&gli->mutex);
74 memcpy(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
75 out:
76 return gli;
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.
84 * Return: Found local lun_info structure on success, NULL on failure
86 static struct llun_info *lookup_local(struct cxlflash_cfg *cfg, u8 *wwid)
88 struct llun_info *lli, *temp;
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;
94 return NULL;
97 /**
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))
109 return gli;
111 return NULL;
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.).
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).
128 * Keep a pointer back from the local to the global entry.
130 * This routine assumes the caller holds the global mutex.
132 * Return: Found/Allocated local lun_info structure on success, NULL on failure
134 static struct llun_info *find_and_create_lun(struct scsi_device *sdev, u8 *wwid)
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;
141 if (unlikely(!wwid))
142 goto out;
144 lli = lookup_local(cfg, wwid);
145 if (lli)
146 goto out;
148 lli = create_local(sdev, wwid);
149 if (unlikely(!lli))
150 goto out;
152 gli = lookup_global(wwid);
153 if (gli) {
154 lli->parent = gli;
155 list_add(&lli->list, &cfg->lluns);
156 goto out;
159 gli = create_global(sdev, wwid);
160 if (unlikely(!gli)) {
161 kfree(lli);
162 lli = NULL;
163 goto out;
166 lli->parent = gli;
167 list_add(&lli->list, &cfg->lluns);
169 list_add(&gli->list, &global.gluns);
171 out:
172 dev_dbg(dev, "%s: returning lli=%p, gli=%p\n", __func__, lli, gli);
173 return lli;
177 * cxlflash_term_local_luns() - Delete all entries from local LUN list, free.
178 * @cfg: Internal structure associated with the host.
180 void cxlflash_term_local_luns(struct cxlflash_cfg *cfg)
182 struct llun_info *lli, *temp;
184 mutex_lock(&global.mutex);
185 list_for_each_entry_safe(lli, temp, &cfg->lluns, list) {
186 list_del(&lli->list);
187 kfree(lli);
189 mutex_unlock(&global.mutex);
193 * cxlflash_list_init() - initializes the global LUN list
195 void cxlflash_list_init(void)
197 INIT_LIST_HEAD(&global.gluns);
198 mutex_init(&global.mutex);
199 global.err_page = NULL;
203 * cxlflash_term_global_luns() - frees resources associated with global LUN list
205 void cxlflash_term_global_luns(void)
207 struct glun_info *gli, *temp;
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);
215 mutex_unlock(&global.mutex);
219 * cxlflash_manage_lun() - handles LUN management activities
220 * @sdev: SCSI device associated with LUN.
221 * @manage: Manage ioctl data structure.
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.
227 * Return: 0 on success, -errno on failure
229 int cxlflash_manage_lun(struct scsi_device *sdev,
230 struct dk_cxlflash_manage_lun *manage)
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;
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;
249 if (flags & DK_CXLFLASH_MANAGE_LUN_ENABLE_SUPERPIPE) {
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.
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 {
263 * Clean up local LUN for this port and reset table
264 * tracking when no more references exist.
266 sdev->hostdata = NULL;
267 lli->port_sel &= ~CHAN2PORTMASK(chan);
268 if (lli->port_sel == 0U)
269 lli->in_table = false;
273 dev_dbg(dev, "%s: port_sel=%08x chan=%u lun_id=%016llx\n",
274 __func__, lli->port_sel, chan, lli->lun_id[chan]);
276 out:
277 mutex_unlock(&global.mutex);
278 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
279 return rc;