1
2
3
4
5
6
7
8
9
10
11
12
13
14
15#include <asm/unaligned.h>
16
17#include <linux/interrupt.h>
18#include <linux/pci.h>
19
20#include <scsi/scsi_host.h>
21#include <uapi/scsi/cxlflash_ioctl.h>
22
23#include "sislite.h"
24#include "common.h"
25#include "vlun.h"
26#include "superpipe.h"
27
28
29
30
31
32
33
34
35static struct llun_info *create_local(struct scsi_device *sdev, u8 *wwid)
36{
37 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
38 struct device *dev = &cfg->dev->dev;
39 struct llun_info *lli = NULL;
40
41 lli = kzalloc(sizeof(*lli), GFP_KERNEL);
42 if (unlikely(!lli)) {
43 dev_err(dev, "%s: could not allocate lli\n", __func__);
44 goto out;
45 }
46
47 lli->sdev = sdev;
48 lli->host_no = sdev->host->host_no;
49 lli->in_table = false;
50
51 memcpy(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
52out:
53 return lli;
54}
55
56
57
58
59
60
61
62
63static struct glun_info *create_global(struct scsi_device *sdev, u8 *wwid)
64{
65 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
66 struct device *dev = &cfg->dev->dev;
67 struct glun_info *gli = NULL;
68
69 gli = kzalloc(sizeof(*gli), GFP_KERNEL);
70 if (unlikely(!gli)) {
71 dev_err(dev, "%s: could not allocate gli\n", __func__);
72 goto out;
73 }
74
75 mutex_init(&gli->mutex);
76 memcpy(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN);
77out:
78 return gli;
79}
80
81
82
83
84
85
86
87
88static struct llun_info *lookup_local(struct cxlflash_cfg *cfg, u8 *wwid)
89{
90 struct llun_info *lli, *temp;
91
92 list_for_each_entry_safe(lli, temp, &cfg->lluns, list)
93 if (!memcmp(lli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN))
94 return lli;
95
96 return NULL;
97}
98
99
100
101
102
103
104
105static struct glun_info *lookup_global(u8 *wwid)
106{
107 struct glun_info *gli, *temp;
108
109 list_for_each_entry_safe(gli, temp, &global.gluns, list)
110 if (!memcmp(gli->wwid, wwid, DK_CXLFLASH_MANAGE_LUN_WWID_LEN))
111 return gli;
112
113 return NULL;
114}
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136static struct llun_info *find_and_create_lun(struct scsi_device *sdev, u8 *wwid)
137{
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;
142
143 if (unlikely(!wwid))
144 goto out;
145
146 lli = lookup_local(cfg, wwid);
147 if (lli)
148 goto out;
149
150 lli = create_local(sdev, wwid);
151 if (unlikely(!lli))
152 goto out;
153
154 gli = lookup_global(wwid);
155 if (gli) {
156 lli->parent = gli;
157 list_add(&lli->list, &cfg->lluns);
158 goto out;
159 }
160
161 gli = create_global(sdev, wwid);
162 if (unlikely(!gli)) {
163 kfree(lli);
164 lli = NULL;
165 goto out;
166 }
167
168 lli->parent = gli;
169 list_add(&lli->list, &cfg->lluns);
170
171 list_add(&gli->list, &global.gluns);
172
173out:
174 dev_dbg(dev, "%s: returning lli=%p, gli=%p\n", __func__, lli, gli);
175 return lli;
176}
177
178
179
180
181
182void cxlflash_term_local_luns(struct cxlflash_cfg *cfg)
183{
184 struct llun_info *lli, *temp;
185
186 mutex_lock(&global.mutex);
187 list_for_each_entry_safe(lli, temp, &cfg->lluns, list) {
188 list_del(&lli->list);
189 kfree(lli);
190 }
191 mutex_unlock(&global.mutex);
192}
193
194
195
196
197void cxlflash_list_init(void)
198{
199 INIT_LIST_HEAD(&global.gluns);
200 mutex_init(&global.mutex);
201 global.err_page = NULL;
202}
203
204
205
206
207void cxlflash_term_global_luns(void)
208{
209 struct glun_info *gli, *temp;
210
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);
215 kfree(gli);
216 }
217 mutex_unlock(&global.mutex);
218}
219
220
221
222
223
224
225
226
227
228
229
230
231int cxlflash_manage_lun(struct scsi_device *sdev,
232 struct dk_cxlflash_manage_lun *manage)
233{
234 struct cxlflash_cfg *cfg = shost_priv(sdev->host);
235 struct device *dev = &cfg->dev->dev;
236 struct llun_info *lli = NULL;
237 int rc = 0;
238 u64 flags = manage->hdr.flags;
239 u32 chan = sdev->channel;
240
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)) {
247 rc = -ENOMEM;
248 goto out;
249 }
250
251 if (flags & DK_CXLFLASH_MANAGE_LUN_ENABLE_SUPERPIPE) {
252
253
254
255
256
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)
262 rc = -EBUSY;
263 else {
264
265
266
267
268 sdev->hostdata = NULL;
269 lli->port_sel &= ~CHAN2PORTMASK(chan);
270 if (lli->port_sel == 0U)
271 lli->in_table = false;
272 }
273 }
274
275 dev_dbg(dev, "%s: port_sel=%08x chan=%u lun_id=%016llx\n",
276 __func__, lli->port_sel, chan, lli->lun_id[chan]);
277
278out:
279 mutex_unlock(&global.mutex);
280 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
281 return rc;
282}
283