blob: 4d232e271af60edad6316f15e8cc1689b2e807ed [file] [log] [blame]
Matthew R. Ochs65be2c72015-08-13 21:47:43 -05001/*
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"
Matthew R. Ochs2cb79262015-08-13 21:47:53 -050023#include "vlun.h"
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050024#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 */
33static struct llun_info *create_local(struct scsi_device *sdev, u8 *wwid)
34{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060035 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
36 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050037 struct llun_info *lli = NULL;
38
39 lli = kzalloc(sizeof(*lli), GFP_KERNEL);
40 if (unlikely(!lli)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060041 dev_err(dev, "%s: could not allocate lli\n", __func__);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050042 goto out;
43 }
44
45 lli->sdev = sdev;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050046 lli->host_no = sdev->host->host_no;
Matthew R. Ochs2cb79262015-08-13 21:47:53 -050047 lli->in_table = false;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050048
49 memcpy(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
50out:
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 */
61static struct glun_info *create_global(struct scsi_device *sdev, u8 *wwid)
62{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060063 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
64 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050065 struct glun_info *gli = NULL;
66
67 gli = kzalloc(sizeof(*gli), GFP_KERNEL);
68 if (unlikely(!gli)) {
Matthew R. Ochsfb67d442017-01-11 19:19:47 -060069 dev_err(dev, "%s: could not allocate gli\n", __func__);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050070 goto out;
71 }
72
73 mutex_init(&gli->mutex);
74 memcpy(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
75out:
76 return gli;
77}
78
79/**
Matthew R. Ochs1a474012015-10-21 15:16:15 -050080 * lookup_local() - find a local LUN information structure by WWID
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050081 * @cfg: Internal structure associated with the host.
82 * @wwid: WWID associated with LUN.
83 *
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050084 * Return: Found local lun_info structure on success, NULL on failure
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050085 */
Matthew R. Ochs1a474012015-10-21 15:16:15 -050086static struct llun_info *lookup_local(struct cxlflash_cfg *cfg, u8 *wwid)
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050087{
88 struct llun_info *lli, *temp;
89
90 list_for_each_entry_safe(lli, temp, &cfg->lluns, list)
Matthew R. Ochs1a474012015-10-21 15:16:15 -050091 if (!memcmp(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN))
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050092 return lli;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -050093
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 */
103static 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
Manoj Kumarfa4aa632015-10-21 15:10:31 -0500121 * adapter (such as index, port selection mask, etc.).
122 *
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500123 * 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 *
Manoj Kumarfa4aa632015-10-21 15:10:31 -0500130 * This routine assumes the caller holds the global mutex.
131 *
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500132 * Return: Found/Allocated local lun_info structure on success, NULL on failure
133 */
134static struct llun_info *find_and_create_lun(struct scsi_device *sdev, u8 *wwid)
135{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600136 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
137 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500138 struct llun_info *lli = NULL;
139 struct glun_info *gli = NULL;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500140
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500141 if (unlikely(!wwid))
142 goto out;
143
Matthew R. Ochs1a474012015-10-21 15:16:15 -0500144 lli = lookup_local(cfg, wwid);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500145 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
171out:
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600172 dev_dbg(dev, "%s: returning lli=%p, gli=%p\n", __func__, lli, gli);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500173 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 */
180void 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 */
195void 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 */
205void 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);
Matthew R. Ochs2cb79262015-08-13 21:47:53 -0500212 cxlflash_ba_terminate(&gli->blka.ba_lun);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500213 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 */
229int cxlflash_manage_lun(struct scsi_device *sdev,
230 struct dk_cxlflash_manage_lun *manage)
231{
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600232 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
233 struct device *dev = &cfg->dev->dev;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500234 struct llun_info *lli = NULL;
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600235 int rc = 0;
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500236 u64 flags = manage->hdr.flags;
237 u32 chan = sdev->channel;
238
Manoj Kumarfa4aa632015-10-21 15:10:31 -0500239 mutex_lock(&global.mutex);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500240 lli = find_and_create_lun(sdev, manage->wwid);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600241 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);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500244 if (unlikely(!lli)) {
245 rc = -ENOMEM;
246 goto out;
247 }
248
249 if (flags & DK_CXLFLASH_MANAGE_LUN_ENABLE_SUPERPIPE) {
Matthew R. Ochs1a474012015-10-21 15:16:15 -0500250 /*
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 */
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -0500255 lli->port_sel |= CHAN2PORTMASK(chan);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500256 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;
Matthew R. Ochs1a474012015-10-21 15:16:15 -0500261 else {
Uma Krishnan8a260542016-11-28 18:41:19 -0600262 /*
263 * Clean up local LUN for this port and reset table
264 * tracking when no more references exist.
265 */
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500266 sdev->hostdata = NULL;
Matthew R. Ochs8fa4f172017-04-12 14:14:05 -0500267 lli->port_sel &= ~CHAN2PORTMASK(chan);
Uma Krishnan8a260542016-11-28 18:41:19 -0600268 if (lli->port_sel == 0U)
269 lli->in_table = false;
Matthew R. Ochs1a474012015-10-21 15:16:15 -0500270 }
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500271 }
272
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600273 dev_dbg(dev, "%s: port_sel=%08x chan=%u lun_id=%016llx\n",
274 __func__, lli->port_sel, chan, lli->lun_id[chan]);
Matthew R. Ochs1a474012015-10-21 15:16:15 -0500275
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500276out:
Manoj Kumarfa4aa632015-10-21 15:10:31 -0500277 mutex_unlock(&global.mutex);
Matthew R. Ochsfb67d442017-01-11 19:19:47 -0600278 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
Matthew R. Ochs65be2c72015-08-13 21:47:43 -0500279 return rc;
280}