]>
Commit | Line | Data |
---|---|---|
65be2c79 MO |
1 | /* |
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 <linux/delay.h> | |
16 | #include <linux/file.h> | |
17 | #include <linux/syscalls.h> | |
18 | #include <misc/cxl.h> | |
19 | #include <asm/unaligned.h> | |
20 | ||
21 | #include <scsi/scsi.h> | |
22 | #include <scsi/scsi_host.h> | |
23 | #include <scsi/scsi_cmnd.h> | |
24 | #include <scsi/scsi_eh.h> | |
25 | #include <uapi/scsi/cxlflash_ioctl.h> | |
26 | ||
27 | #include "sislite.h" | |
28 | #include "common.h" | |
2cb79266 | 29 | #include "vlun.h" |
65be2c79 MO |
30 | #include "superpipe.h" |
31 | ||
32 | struct cxlflash_global global; | |
33 | ||
2cb79266 MO |
34 | /** |
35 | * marshal_rele_to_resize() - translate release to resize structure | |
36 | * @rele: Source structure from which to translate/copy. | |
37 | * @resize: Destination structure for the translate/copy. | |
38 | */ | |
39 | static void marshal_rele_to_resize(struct dk_cxlflash_release *release, | |
40 | struct dk_cxlflash_resize *resize) | |
41 | { | |
42 | resize->hdr = release->hdr; | |
43 | resize->context_id = release->context_id; | |
44 | resize->rsrc_handle = release->rsrc_handle; | |
45 | } | |
46 | ||
65be2c79 MO |
47 | /** |
48 | * marshal_det_to_rele() - translate detach to release structure | |
49 | * @detach: Destination structure for the translate/copy. | |
50 | * @rele: Source structure from which to translate/copy. | |
51 | */ | |
52 | static void marshal_det_to_rele(struct dk_cxlflash_detach *detach, | |
53 | struct dk_cxlflash_release *release) | |
54 | { | |
55 | release->hdr = detach->hdr; | |
56 | release->context_id = detach->context_id; | |
57 | } | |
58 | ||
c2c292f4 UK |
59 | /** |
60 | * marshal_udir_to_rele() - translate udirect to release structure | |
61 | * @udirect: Source structure from which to translate/copy. | |
62 | * @release: Destination structure for the translate/copy. | |
63 | */ | |
64 | static void marshal_udir_to_rele(struct dk_cxlflash_udirect *udirect, | |
65 | struct dk_cxlflash_release *release) | |
66 | { | |
67 | release->hdr = udirect->hdr; | |
68 | release->context_id = udirect->context_id; | |
69 | release->rsrc_handle = udirect->rsrc_handle; | |
70 | } | |
71 | ||
65be2c79 MO |
72 | /** |
73 | * cxlflash_free_errpage() - frees resources associated with global error page | |
74 | */ | |
75 | void cxlflash_free_errpage(void) | |
76 | { | |
77 | ||
78 | mutex_lock(&global.mutex); | |
79 | if (global.err_page) { | |
80 | __free_page(global.err_page); | |
81 | global.err_page = NULL; | |
82 | } | |
83 | mutex_unlock(&global.mutex); | |
84 | } | |
85 | ||
86 | /** | |
87 | * cxlflash_stop_term_user_contexts() - stops/terminates known user contexts | |
88 | * @cfg: Internal structure associated with the host. | |
89 | * | |
90 | * When the host needs to go down, all users must be quiesced and their | |
91 | * memory freed. This is accomplished by putting the contexts in error | |
f15fbf8d | 92 | * state which will notify the user and let them 'drive' the tear down. |
65be2c79 | 93 | * Meanwhile, this routine camps until all user contexts have been removed. |
323e3342 MO |
94 | * |
95 | * Note that the main loop in this routine will always execute at least once | |
96 | * to flush the reset_waitq. | |
65be2c79 MO |
97 | */ |
98 | void cxlflash_stop_term_user_contexts(struct cxlflash_cfg *cfg) | |
99 | { | |
100 | struct device *dev = &cfg->dev->dev; | |
323e3342 | 101 | int i, found = true; |
65be2c79 MO |
102 | |
103 | cxlflash_mark_contexts_error(cfg); | |
104 | ||
105 | while (true) { | |
65be2c79 MO |
106 | for (i = 0; i < MAX_CONTEXT; i++) |
107 | if (cfg->ctx_tbl[i]) { | |
108 | found = true; | |
109 | break; | |
110 | } | |
111 | ||
112 | if (!found && list_empty(&cfg->ctx_err_recovery)) | |
113 | return; | |
114 | ||
115 | dev_dbg(dev, "%s: Wait for user contexts to quiesce...\n", | |
116 | __func__); | |
439e85c1 | 117 | wake_up_all(&cfg->reset_waitq); |
65be2c79 | 118 | ssleep(1); |
323e3342 | 119 | found = false; |
65be2c79 MO |
120 | } |
121 | } | |
122 | ||
123 | /** | |
124 | * find_error_context() - locates a context by cookie on the error recovery list | |
125 | * @cfg: Internal structure associated with the host. | |
126 | * @rctxid: Desired context by id. | |
127 | * @file: Desired context by file. | |
128 | * | |
129 | * Return: Found context on success, NULL on failure | |
130 | */ | |
131 | static struct ctx_info *find_error_context(struct cxlflash_cfg *cfg, u64 rctxid, | |
132 | struct file *file) | |
133 | { | |
134 | struct ctx_info *ctxi; | |
135 | ||
136 | list_for_each_entry(ctxi, &cfg->ctx_err_recovery, list) | |
137 | if ((ctxi->ctxid == rctxid) || (ctxi->file == file)) | |
138 | return ctxi; | |
139 | ||
140 | return NULL; | |
141 | } | |
142 | ||
143 | /** | |
144 | * get_context() - obtains a validated and locked context reference | |
145 | * @cfg: Internal structure associated with the host. | |
146 | * @rctxid: Desired context (raw, un-decoded format). | |
147 | * @arg: LUN information or file associated with request. | |
148 | * @ctx_ctrl: Control information to 'steer' desired lookup. | |
149 | * | |
150 | * NOTE: despite the name pid, in linux, current->pid actually refers | |
151 | * to the lightweight process id (tid) and can change if the process is | |
152 | * multi threaded. The tgid remains constant for the process and only changes | |
153 | * when the process of fork. For all intents and purposes, think of tgid | |
154 | * as a pid in the traditional sense. | |
155 | * | |
156 | * Return: Validated context on success, NULL on failure | |
157 | */ | |
158 | struct ctx_info *get_context(struct cxlflash_cfg *cfg, u64 rctxid, | |
159 | void *arg, enum ctx_ctrl ctx_ctrl) | |
160 | { | |
161 | struct device *dev = &cfg->dev->dev; | |
162 | struct ctx_info *ctxi = NULL; | |
163 | struct lun_access *lun_access = NULL; | |
164 | struct file *file = NULL; | |
165 | struct llun_info *lli = arg; | |
166 | u64 ctxid = DECODE_CTXID(rctxid); | |
167 | int rc; | |
d84c198f | 168 | pid_t pid = task_tgid_nr(current), ctxpid = 0; |
65be2c79 MO |
169 | |
170 | if (ctx_ctrl & CTX_CTRL_FILE) { | |
171 | lli = NULL; | |
172 | file = (struct file *)arg; | |
173 | } | |
174 | ||
175 | if (ctx_ctrl & CTX_CTRL_CLONE) | |
d84c198f | 176 | pid = task_ppid_nr(current); |
65be2c79 MO |
177 | |
178 | if (likely(ctxid < MAX_CONTEXT)) { | |
179 | while (true) { | |
a82544c7 | 180 | mutex_lock(&cfg->ctx_tbl_list_mutex); |
65be2c79 MO |
181 | ctxi = cfg->ctx_tbl[ctxid]; |
182 | if (ctxi) | |
183 | if ((file && (ctxi->file != file)) || | |
184 | (!file && (ctxi->ctxid != rctxid))) | |
185 | ctxi = NULL; | |
186 | ||
187 | if ((ctx_ctrl & CTX_CTRL_ERR) || | |
188 | (!ctxi && (ctx_ctrl & CTX_CTRL_ERR_FALLBACK))) | |
189 | ctxi = find_error_context(cfg, rctxid, file); | |
190 | if (!ctxi) { | |
191 | mutex_unlock(&cfg->ctx_tbl_list_mutex); | |
192 | goto out; | |
193 | } | |
194 | ||
195 | /* | |
196 | * Need to acquire ownership of the context while still | |
197 | * under the table/list lock to serialize with a remove | |
198 | * thread. Use the 'try' to avoid stalling the | |
199 | * table/list lock for a single context. | |
200 | * | |
201 | * Note that the lock order is: | |
202 | * | |
203 | * cfg->ctx_tbl_list_mutex -> ctxi->mutex | |
204 | * | |
205 | * Therefore release ctx_tbl_list_mutex before retrying. | |
206 | */ | |
207 | rc = mutex_trylock(&ctxi->mutex); | |
208 | mutex_unlock(&cfg->ctx_tbl_list_mutex); | |
209 | if (rc) | |
210 | break; /* got the context's lock! */ | |
211 | } | |
212 | ||
213 | if (ctxi->unavail) | |
214 | goto denied; | |
215 | ||
216 | ctxpid = ctxi->pid; | |
217 | if (likely(!(ctx_ctrl & CTX_CTRL_NOPID))) | |
218 | if (pid != ctxpid) | |
219 | goto denied; | |
220 | ||
221 | if (lli) { | |
222 | list_for_each_entry(lun_access, &ctxi->luns, list) | |
223 | if (lun_access->lli == lli) | |
224 | goto out; | |
225 | goto denied; | |
226 | } | |
227 | } | |
228 | ||
229 | out: | |
fb67d44d | 230 | dev_dbg(dev, "%s: rctxid=%016llx ctxinfo=%p ctxpid=%u pid=%u " |
65be2c79 MO |
231 | "ctx_ctrl=%u\n", __func__, rctxid, ctxi, ctxpid, pid, |
232 | ctx_ctrl); | |
233 | ||
234 | return ctxi; | |
235 | ||
236 | denied: | |
237 | mutex_unlock(&ctxi->mutex); | |
238 | ctxi = NULL; | |
239 | goto out; | |
240 | } | |
241 | ||
242 | /** | |
243 | * put_context() - release a context that was retrieved from get_context() | |
244 | * @ctxi: Context to release. | |
245 | * | |
246 | * For now, releasing the context equates to unlocking it's mutex. | |
247 | */ | |
248 | void put_context(struct ctx_info *ctxi) | |
249 | { | |
250 | mutex_unlock(&ctxi->mutex); | |
251 | } | |
252 | ||
253 | /** | |
254 | * afu_attach() - attach a context to the AFU | |
255 | * @cfg: Internal structure associated with the host. | |
256 | * @ctxi: Context to attach. | |
257 | * | |
258 | * Upon setting the context capabilities, they must be confirmed with | |
259 | * a read back operation as the context might have been closed since | |
260 | * the mailbox was unlocked. When this occurs, registration is failed. | |
261 | * | |
262 | * Return: 0 on success, -errno on failure | |
263 | */ | |
264 | static int afu_attach(struct cxlflash_cfg *cfg, struct ctx_info *ctxi) | |
265 | { | |
266 | struct device *dev = &cfg->dev->dev; | |
267 | struct afu *afu = cfg->afu; | |
1786f4a0 | 268 | struct sisl_ctrl_map __iomem *ctrl_map = ctxi->ctrl_map; |
65be2c79 | 269 | int rc = 0; |
bfc0bab1 | 270 | struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ); |
65be2c79 MO |
271 | u64 val; |
272 | ||
273 | /* Unlock cap and restrict user to read/write cmds in translated mode */ | |
274 | readq_be(&ctrl_map->mbox_r); | |
275 | val = (SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD); | |
276 | writeq_be(val, &ctrl_map->ctx_cap); | |
277 | val = readq_be(&ctrl_map->ctx_cap); | |
278 | if (val != (SISL_CTX_CAP_READ_CMD | SISL_CTX_CAP_WRITE_CMD)) { | |
fb67d44d | 279 | dev_err(dev, "%s: ctx may be closed val=%016llx\n", |
65be2c79 MO |
280 | __func__, val); |
281 | rc = -EAGAIN; | |
282 | goto out; | |
283 | } | |
284 | ||
285 | /* Set up MMIO registers pointing to the RHT */ | |
286 | writeq_be((u64)ctxi->rht_start, &ctrl_map->rht_start); | |
bfc0bab1 | 287 | val = SISL_RHT_CNT_ID((u64)MAX_RHT_PER_CONTEXT, (u64)(hwq->ctx_hndl)); |
65be2c79 MO |
288 | writeq_be(val, &ctrl_map->rht_cnt_id); |
289 | out: | |
290 | dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc); | |
291 | return rc; | |
292 | } | |
293 | ||
294 | /** | |
295 | * read_cap16() - issues a SCSI READ_CAP16 command | |
296 | * @sdev: SCSI device associated with LUN. | |
297 | * @lli: LUN destined for capacity request. | |
298 | * | |
aacb4ff6 MO |
299 | * The READ_CAP16 can take quite a while to complete. Should an EEH occur while |
300 | * in scsi_execute(), the EEH handler will attempt to recover. As part of the | |
301 | * recovery, the handler drains all currently running ioctls, waiting until they | |
302 | * have completed before proceeding with a reset. As this routine is used on the | |
303 | * ioctl path, this can create a condition where the EEH handler becomes stuck, | |
304 | * infinitely waiting for this ioctl thread. To avoid this behavior, temporarily | |
305 | * unmark this thread as an ioctl thread by releasing the ioctl read semaphore. | |
306 | * This will allow the EEH handler to proceed with a recovery while this thread | |
307 | * is still running. Once the scsi_execute() returns, reacquire the ioctl read | |
308 | * semaphore and check the adapter state in case it changed while inside of | |
309 | * scsi_execute(). The state check will wait if the adapter is still being | |
310 | * recovered or return a failure if the recovery failed. In the event that the | |
311 | * adapter reset failed, simply return the failure as the ioctl would be unable | |
312 | * to continue. | |
313 | * | |
314 | * Note that the above puts a requirement on this routine to only be called on | |
315 | * an ioctl thread. | |
316 | * | |
65be2c79 MO |
317 | * Return: 0 on success, -errno on failure |
318 | */ | |
319 | static int read_cap16(struct scsi_device *sdev, struct llun_info *lli) | |
320 | { | |
fb67d44d | 321 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
65be2c79 MO |
322 | struct device *dev = &cfg->dev->dev; |
323 | struct glun_info *gli = lli->parent; | |
76aaf87b | 324 | struct scsi_sense_hdr sshdr; |
65be2c79 MO |
325 | u8 *cmd_buf = NULL; |
326 | u8 *scsi_cmd = NULL; | |
327 | u8 *sense_buf = NULL; | |
328 | int rc = 0; | |
329 | int result = 0; | |
330 | int retry_cnt = 0; | |
471a5a60 | 331 | u32 to = CMD_TIMEOUT * HZ; |
65be2c79 MO |
332 | |
333 | retry: | |
334 | cmd_buf = kzalloc(CMD_BUFSIZE, GFP_KERNEL); | |
335 | scsi_cmd = kzalloc(MAX_COMMAND_SIZE, GFP_KERNEL); | |
336 | sense_buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL); | |
337 | if (unlikely(!cmd_buf || !scsi_cmd || !sense_buf)) { | |
338 | rc = -ENOMEM; | |
339 | goto out; | |
340 | } | |
341 | ||
342 | scsi_cmd[0] = SERVICE_ACTION_IN_16; /* read cap(16) */ | |
343 | scsi_cmd[1] = SAI_READ_CAPACITY_16; /* service action */ | |
344 | put_unaligned_be32(CMD_BUFSIZE, &scsi_cmd[10]); | |
345 | ||
fb67d44d | 346 | dev_dbg(dev, "%s: %ssending cmd(%02x)\n", __func__, |
65be2c79 MO |
347 | retry_cnt ? "re" : "", scsi_cmd[0]); |
348 | ||
aacb4ff6 MO |
349 | /* Drop the ioctl read semahpore across lengthy call */ |
350 | up_read(&cfg->ioctl_rwsem); | |
65be2c79 | 351 | result = scsi_execute(sdev, scsi_cmd, DMA_FROM_DEVICE, cmd_buf, |
76aaf87b CH |
352 | CMD_BUFSIZE, sense_buf, &sshdr, to, CMD_RETRIES, |
353 | 0, 0, NULL); | |
aacb4ff6 MO |
354 | down_read(&cfg->ioctl_rwsem); |
355 | rc = check_state(cfg); | |
356 | if (rc) { | |
fb67d44d | 357 | dev_err(dev, "%s: Failed state result=%08x\n", |
aacb4ff6 MO |
358 | __func__, result); |
359 | rc = -ENODEV; | |
360 | goto out; | |
361 | } | |
65be2c79 MO |
362 | |
363 | if (driver_byte(result) == DRIVER_SENSE) { | |
364 | result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */ | |
365 | if (result & SAM_STAT_CHECK_CONDITION) { | |
65be2c79 MO |
366 | switch (sshdr.sense_key) { |
367 | case NO_SENSE: | |
368 | case RECOVERED_ERROR: | |
369 | /* fall through */ | |
370 | case NOT_READY: | |
371 | result &= ~SAM_STAT_CHECK_CONDITION; | |
372 | break; | |
373 | case UNIT_ATTENTION: | |
374 | switch (sshdr.asc) { | |
375 | case 0x29: /* Power on Reset or Device Reset */ | |
376 | /* fall through */ | |
377 | case 0x2A: /* Device capacity changed */ | |
378 | case 0x3F: /* Report LUNs changed */ | |
379 | /* Retry the command once more */ | |
380 | if (retry_cnt++ < 1) { | |
381 | kfree(cmd_buf); | |
382 | kfree(scsi_cmd); | |
383 | kfree(sense_buf); | |
384 | goto retry; | |
385 | } | |
386 | } | |
387 | break; | |
388 | default: | |
389 | break; | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
394 | if (result) { | |
fb67d44d | 395 | dev_err(dev, "%s: command failed, result=%08x\n", |
65be2c79 MO |
396 | __func__, result); |
397 | rc = -EIO; | |
398 | goto out; | |
399 | } | |
400 | ||
401 | /* | |
402 | * Read cap was successful, grab values from the buffer; | |
403 | * note that we don't need to worry about unaligned access | |
404 | * as the buffer is allocated on an aligned boundary. | |
405 | */ | |
406 | mutex_lock(&gli->mutex); | |
1786f4a0 MO |
407 | gli->max_lba = be64_to_cpu(*((__be64 *)&cmd_buf[0])); |
408 | gli->blk_len = be32_to_cpu(*((__be32 *)&cmd_buf[8])); | |
65be2c79 MO |
409 | mutex_unlock(&gli->mutex); |
410 | ||
411 | out: | |
412 | kfree(cmd_buf); | |
413 | kfree(scsi_cmd); | |
414 | kfree(sense_buf); | |
415 | ||
416 | dev_dbg(dev, "%s: maxlba=%lld blklen=%d rc=%d\n", | |
417 | __func__, gli->max_lba, gli->blk_len, rc); | |
418 | return rc; | |
419 | } | |
420 | ||
421 | /** | |
422 | * get_rhte() - obtains validated resource handle table entry reference | |
423 | * @ctxi: Context owning the resource handle. | |
424 | * @rhndl: Resource handle associated with entry. | |
425 | * @lli: LUN associated with request. | |
426 | * | |
427 | * Return: Validated RHTE on success, NULL on failure | |
428 | */ | |
429 | struct sisl_rht_entry *get_rhte(struct ctx_info *ctxi, res_hndl_t rhndl, | |
430 | struct llun_info *lli) | |
431 | { | |
fb67d44d MO |
432 | struct cxlflash_cfg *cfg = ctxi->cfg; |
433 | struct device *dev = &cfg->dev->dev; | |
65be2c79 MO |
434 | struct sisl_rht_entry *rhte = NULL; |
435 | ||
436 | if (unlikely(!ctxi->rht_start)) { | |
fb67d44d | 437 | dev_dbg(dev, "%s: Context does not have allocated RHT\n", |
65be2c79 MO |
438 | __func__); |
439 | goto out; | |
440 | } | |
441 | ||
442 | if (unlikely(rhndl >= MAX_RHT_PER_CONTEXT)) { | |
fb67d44d MO |
443 | dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n", |
444 | __func__, rhndl); | |
65be2c79 MO |
445 | goto out; |
446 | } | |
447 | ||
448 | if (unlikely(ctxi->rht_lun[rhndl] != lli)) { | |
fb67d44d MO |
449 | dev_dbg(dev, "%s: Bad resource handle LUN rhndl=%d\n", |
450 | __func__, rhndl); | |
65be2c79 MO |
451 | goto out; |
452 | } | |
453 | ||
454 | rhte = &ctxi->rht_start[rhndl]; | |
455 | if (unlikely(rhte->nmask == 0)) { | |
fb67d44d MO |
456 | dev_dbg(dev, "%s: Unopened resource handle rhndl=%d\n", |
457 | __func__, rhndl); | |
65be2c79 MO |
458 | rhte = NULL; |
459 | goto out; | |
460 | } | |
461 | ||
462 | out: | |
463 | return rhte; | |
464 | } | |
465 | ||
466 | /** | |
467 | * rhte_checkout() - obtains free/empty resource handle table entry | |
468 | * @ctxi: Context owning the resource handle. | |
469 | * @lli: LUN associated with request. | |
470 | * | |
471 | * Return: Free RHTE on success, NULL on failure | |
472 | */ | |
473 | struct sisl_rht_entry *rhte_checkout(struct ctx_info *ctxi, | |
474 | struct llun_info *lli) | |
475 | { | |
fb67d44d MO |
476 | struct cxlflash_cfg *cfg = ctxi->cfg; |
477 | struct device *dev = &cfg->dev->dev; | |
65be2c79 MO |
478 | struct sisl_rht_entry *rhte = NULL; |
479 | int i; | |
480 | ||
481 | /* Find a free RHT entry */ | |
482 | for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) | |
483 | if (ctxi->rht_start[i].nmask == 0) { | |
484 | rhte = &ctxi->rht_start[i]; | |
485 | ctxi->rht_out++; | |
486 | break; | |
487 | } | |
488 | ||
489 | if (likely(rhte)) | |
490 | ctxi->rht_lun[i] = lli; | |
491 | ||
fb67d44d | 492 | dev_dbg(dev, "%s: returning rhte=%p index=%d\n", __func__, rhte, i); |
65be2c79 MO |
493 | return rhte; |
494 | } | |
495 | ||
496 | /** | |
497 | * rhte_checkin() - releases a resource handle table entry | |
498 | * @ctxi: Context owning the resource handle. | |
499 | * @rhte: RHTE to release. | |
500 | */ | |
501 | void rhte_checkin(struct ctx_info *ctxi, | |
502 | struct sisl_rht_entry *rhte) | |
503 | { | |
504 | u32 rsrc_handle = rhte - ctxi->rht_start; | |
505 | ||
506 | rhte->nmask = 0; | |
507 | rhte->fp = 0; | |
508 | ctxi->rht_out--; | |
509 | ctxi->rht_lun[rsrc_handle] = NULL; | |
2cb79266 | 510 | ctxi->rht_needs_ws[rsrc_handle] = false; |
65be2c79 MO |
511 | } |
512 | ||
513 | /** | |
514 | * rhte_format1() - populates a RHTE for format 1 | |
515 | * @rhte: RHTE to populate. | |
516 | * @lun_id: LUN ID of LUN associated with RHTE. | |
517 | * @perm: Desired permissions for RHTE. | |
518 | * @port_sel: Port selection mask | |
519 | */ | |
520 | static void rht_format1(struct sisl_rht_entry *rhte, u64 lun_id, u32 perm, | |
521 | u32 port_sel) | |
522 | { | |
523 | /* | |
524 | * Populate the Format 1 RHT entry for direct access (physical | |
525 | * LUN) using the synchronization sequence defined in the | |
526 | * SISLite specification. | |
527 | */ | |
528 | struct sisl_rht_entry_f1 dummy = { 0 }; | |
529 | struct sisl_rht_entry_f1 *rhte_f1 = (struct sisl_rht_entry_f1 *)rhte; | |
530 | ||
531 | memset(rhte_f1, 0, sizeof(*rhte_f1)); | |
532 | rhte_f1->fp = SISL_RHT_FP(1U, 0); | |
533 | dma_wmb(); /* Make setting of format bit visible */ | |
534 | ||
535 | rhte_f1->lun_id = lun_id; | |
536 | dma_wmb(); /* Make setting of LUN id visible */ | |
537 | ||
538 | /* | |
539 | * Use a dummy RHT Format 1 entry to build the second dword | |
540 | * of the entry that must be populated in a single write when | |
541 | * enabled (valid bit set to TRUE). | |
542 | */ | |
543 | dummy.valid = 0x80; | |
544 | dummy.fp = SISL_RHT_FP(1U, perm); | |
545 | dummy.port_sel = port_sel; | |
546 | rhte_f1->dw = dummy.dw; | |
547 | ||
548 | dma_wmb(); /* Make remaining RHT entry fields visible */ | |
549 | } | |
550 | ||
551 | /** | |
552 | * cxlflash_lun_attach() - attaches a user to a LUN and manages the LUN's mode | |
553 | * @gli: LUN to attach. | |
554 | * @mode: Desired mode of the LUN. | |
555 | * @locked: Mutex status on current thread. | |
556 | * | |
557 | * Return: 0 on success, -errno on failure | |
558 | */ | |
559 | int cxlflash_lun_attach(struct glun_info *gli, enum lun_mode mode, bool locked) | |
560 | { | |
561 | int rc = 0; | |
562 | ||
563 | if (!locked) | |
564 | mutex_lock(&gli->mutex); | |
565 | ||
566 | if (gli->mode == MODE_NONE) | |
567 | gli->mode = mode; | |
568 | else if (gli->mode != mode) { | |
fb67d44d | 569 | pr_debug("%s: gli_mode=%d requested_mode=%d\n", |
65be2c79 MO |
570 | __func__, gli->mode, mode); |
571 | rc = -EINVAL; | |
572 | goto out; | |
573 | } | |
574 | ||
575 | gli->users++; | |
576 | WARN_ON(gli->users <= 0); | |
577 | out: | |
578 | pr_debug("%s: Returning rc=%d gli->mode=%u gli->users=%u\n", | |
579 | __func__, rc, gli->mode, gli->users); | |
580 | if (!locked) | |
581 | mutex_unlock(&gli->mutex); | |
582 | return rc; | |
583 | } | |
584 | ||
585 | /** | |
586 | * cxlflash_lun_detach() - detaches a user from a LUN and resets the LUN's mode | |
587 | * @gli: LUN to detach. | |
2cb79266 MO |
588 | * |
589 | * When resetting the mode, terminate block allocation resources as they | |
590 | * are no longer required (service is safe to call even when block allocation | |
591 | * resources were not present - such as when transitioning from physical mode). | |
592 | * These resources will be reallocated when needed (subsequent transition to | |
593 | * virtual mode). | |
65be2c79 MO |
594 | */ |
595 | void cxlflash_lun_detach(struct glun_info *gli) | |
596 | { | |
597 | mutex_lock(&gli->mutex); | |
598 | WARN_ON(gli->mode == MODE_NONE); | |
2cb79266 | 599 | if (--gli->users == 0) { |
65be2c79 | 600 | gli->mode = MODE_NONE; |
2cb79266 MO |
601 | cxlflash_ba_terminate(&gli->blka.ba_lun); |
602 | } | |
65be2c79 MO |
603 | pr_debug("%s: gli->users=%u\n", __func__, gli->users); |
604 | WARN_ON(gli->users < 0); | |
605 | mutex_unlock(&gli->mutex); | |
606 | } | |
607 | ||
608 | /** | |
609 | * _cxlflash_disk_release() - releases the specified resource entry | |
610 | * @sdev: SCSI device associated with LUN. | |
611 | * @ctxi: Context owning resources. | |
612 | * @release: Release ioctl data structure. | |
613 | * | |
2cb79266 MO |
614 | * For LUNs in virtual mode, the virtual LUN associated with the specified |
615 | * resource handle is resized to 0 prior to releasing the RHTE. Note that the | |
616 | * AFU sync should _not_ be performed when the context is sitting on the error | |
617 | * recovery list. A context on the error recovery list is not known to the AFU | |
618 | * due to reset. When the context is recovered, it will be reattached and made | |
619 | * known again to the AFU. | |
65be2c79 MO |
620 | * |
621 | * Return: 0 on success, -errno on failure | |
622 | */ | |
623 | int _cxlflash_disk_release(struct scsi_device *sdev, | |
624 | struct ctx_info *ctxi, | |
625 | struct dk_cxlflash_release *release) | |
626 | { | |
fb67d44d | 627 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
65be2c79 MO |
628 | struct device *dev = &cfg->dev->dev; |
629 | struct llun_info *lli = sdev->hostdata; | |
630 | struct glun_info *gli = lli->parent; | |
631 | struct afu *afu = cfg->afu; | |
632 | bool put_ctx = false; | |
633 | ||
2cb79266 | 634 | struct dk_cxlflash_resize size; |
65be2c79 MO |
635 | res_hndl_t rhndl = release->rsrc_handle; |
636 | ||
637 | int rc = 0; | |
c2c292f4 | 638 | int rcr = 0; |
65be2c79 MO |
639 | u64 ctxid = DECODE_CTXID(release->context_id), |
640 | rctxid = release->context_id; | |
641 | ||
642 | struct sisl_rht_entry *rhte; | |
643 | struct sisl_rht_entry_f1 *rhte_f1; | |
644 | ||
fb67d44d | 645 | dev_dbg(dev, "%s: ctxid=%llu rhndl=%llu gli->mode=%u gli->users=%u\n", |
65be2c79 MO |
646 | __func__, ctxid, release->rsrc_handle, gli->mode, gli->users); |
647 | ||
648 | if (!ctxi) { | |
649 | ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK); | |
650 | if (unlikely(!ctxi)) { | |
fb67d44d | 651 | dev_dbg(dev, "%s: Bad context ctxid=%llu\n", |
65be2c79 MO |
652 | __func__, ctxid); |
653 | rc = -EINVAL; | |
654 | goto out; | |
655 | } | |
656 | ||
657 | put_ctx = true; | |
658 | } | |
659 | ||
660 | rhte = get_rhte(ctxi, rhndl, lli); | |
661 | if (unlikely(!rhte)) { | |
fb67d44d | 662 | dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n", |
65be2c79 MO |
663 | __func__, rhndl); |
664 | rc = -EINVAL; | |
665 | goto out; | |
666 | } | |
667 | ||
2cb79266 MO |
668 | /* |
669 | * Resize to 0 for virtual LUNS by setting the size | |
670 | * to 0. This will clear LXT_START and LXT_CNT fields | |
671 | * in the RHT entry and properly sync with the AFU. | |
672 | * | |
673 | * Afterwards we clear the remaining fields. | |
674 | */ | |
65be2c79 | 675 | switch (gli->mode) { |
2cb79266 MO |
676 | case MODE_VIRTUAL: |
677 | marshal_rele_to_resize(release, &size); | |
678 | size.req_size = 0; | |
679 | rc = _cxlflash_vlun_resize(sdev, ctxi, &size); | |
680 | if (rc) { | |
681 | dev_dbg(dev, "%s: resize failed rc %d\n", __func__, rc); | |
682 | goto out; | |
683 | } | |
684 | ||
685 | break; | |
65be2c79 MO |
686 | case MODE_PHYSICAL: |
687 | /* | |
688 | * Clear the Format 1 RHT entry for direct access | |
689 | * (physical LUN) using the synchronization sequence | |
690 | * defined in the SISLite specification. | |
691 | */ | |
692 | rhte_f1 = (struct sisl_rht_entry_f1 *)rhte; | |
693 | ||
694 | rhte_f1->valid = 0; | |
695 | dma_wmb(); /* Make revocation of RHT entry visible */ | |
696 | ||
697 | rhte_f1->lun_id = 0; | |
698 | dma_wmb(); /* Make clearing of LUN id visible */ | |
699 | ||
700 | rhte_f1->dw = 0; | |
701 | dma_wmb(); /* Make RHT entry bottom-half clearing visible */ | |
702 | ||
c2c292f4 UK |
703 | if (!ctxi->err_recovery_active) { |
704 | rcr = cxlflash_afu_sync(afu, ctxid, rhndl, AFU_HW_SYNC); | |
705 | if (unlikely(rcr)) | |
706 | dev_dbg(dev, "%s: AFU sync failed rc=%d\n", | |
707 | __func__, rcr); | |
708 | } | |
65be2c79 MO |
709 | break; |
710 | default: | |
711 | WARN(1, "Unsupported LUN mode!"); | |
712 | goto out; | |
713 | } | |
714 | ||
715 | rhte_checkin(ctxi, rhte); | |
716 | cxlflash_lun_detach(gli); | |
717 | ||
718 | out: | |
719 | if (put_ctx) | |
720 | put_context(ctxi); | |
721 | dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc); | |
722 | return rc; | |
723 | } | |
724 | ||
725 | int cxlflash_disk_release(struct scsi_device *sdev, | |
726 | struct dk_cxlflash_release *release) | |
727 | { | |
728 | return _cxlflash_disk_release(sdev, NULL, release); | |
729 | } | |
730 | ||
731 | /** | |
732 | * destroy_context() - releases a context | |
733 | * @cfg: Internal structure associated with the host. | |
734 | * @ctxi: Context to release. | |
735 | * | |
41b99e1a MO |
736 | * This routine is safe to be called with a a non-initialized context. |
737 | * Also note that the routine conditionally checks for the existence | |
738 | * of the context control map before clearing the RHT registers and | |
739 | * context capabilities because it is possible to destroy a context | |
740 | * while the context is in the error state (previous mapping was | |
741 | * removed [so there is no need to worry about clearing] and context | |
742 | * is waiting for a new mapping). | |
65be2c79 MO |
743 | */ |
744 | static void destroy_context(struct cxlflash_cfg *cfg, | |
745 | struct ctx_info *ctxi) | |
746 | { | |
747 | struct afu *afu = cfg->afu; | |
748 | ||
5e6632d1 MO |
749 | if (ctxi->initialized) { |
750 | WARN_ON(!list_empty(&ctxi->luns)); | |
65be2c79 | 751 | |
5e6632d1 MO |
752 | /* Clear RHT registers and drop all capabilities for context */ |
753 | if (afu->afu_map && ctxi->ctrl_map) { | |
754 | writeq_be(0, &ctxi->ctrl_map->rht_start); | |
755 | writeq_be(0, &ctxi->ctrl_map->rht_cnt_id); | |
756 | writeq_be(0, &ctxi->ctrl_map->ctx_cap); | |
757 | } | |
65be2c79 MO |
758 | } |
759 | ||
760 | /* Free memory associated with context */ | |
761 | free_page((ulong)ctxi->rht_start); | |
2cb79266 | 762 | kfree(ctxi->rht_needs_ws); |
65be2c79 MO |
763 | kfree(ctxi->rht_lun); |
764 | kfree(ctxi); | |
65be2c79 MO |
765 | } |
766 | ||
767 | /** | |
768 | * create_context() - allocates and initializes a context | |
769 | * @cfg: Internal structure associated with the host. | |
65be2c79 MO |
770 | * |
771 | * Return: Allocated context on success, NULL on failure | |
772 | */ | |
5e6632d1 | 773 | static struct ctx_info *create_context(struct cxlflash_cfg *cfg) |
65be2c79 MO |
774 | { |
775 | struct device *dev = &cfg->dev->dev; | |
65be2c79 MO |
776 | struct ctx_info *ctxi = NULL; |
777 | struct llun_info **lli = NULL; | |
e568e23f | 778 | u8 *ws = NULL; |
65be2c79 MO |
779 | struct sisl_rht_entry *rhte; |
780 | ||
781 | ctxi = kzalloc(sizeof(*ctxi), GFP_KERNEL); | |
782 | lli = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*lli)), GFP_KERNEL); | |
2cb79266 MO |
783 | ws = kzalloc((MAX_RHT_PER_CONTEXT * sizeof(*ws)), GFP_KERNEL); |
784 | if (unlikely(!ctxi || !lli || !ws)) { | |
fb67d44d | 785 | dev_err(dev, "%s: Unable to allocate context\n", __func__); |
65be2c79 MO |
786 | goto err; |
787 | } | |
788 | ||
789 | rhte = (struct sisl_rht_entry *)get_zeroed_page(GFP_KERNEL); | |
790 | if (unlikely(!rhte)) { | |
fb67d44d | 791 | dev_err(dev, "%s: Unable to allocate RHT\n", __func__); |
65be2c79 MO |
792 | goto err; |
793 | } | |
794 | ||
795 | ctxi->rht_lun = lli; | |
2cb79266 | 796 | ctxi->rht_needs_ws = ws; |
65be2c79 | 797 | ctxi->rht_start = rhte; |
5e6632d1 MO |
798 | out: |
799 | return ctxi; | |
800 | ||
801 | err: | |
802 | kfree(ws); | |
803 | kfree(lli); | |
804 | kfree(ctxi); | |
805 | ctxi = NULL; | |
806 | goto out; | |
807 | } | |
808 | ||
809 | /** | |
810 | * init_context() - initializes a previously allocated context | |
811 | * @ctxi: Previously allocated context | |
812 | * @cfg: Internal structure associated with the host. | |
b070545d | 813 | * @ctx: Previously obtained context cookie. |
5e6632d1 | 814 | * @ctxid: Previously obtained process element associated with CXL context. |
5e6632d1 MO |
815 | * @file: Previously obtained file associated with CXL context. |
816 | * @perms: User-specified permissions. | |
af2047ec | 817 | * @irqs: User-specified number of interrupts. |
5e6632d1 MO |
818 | */ |
819 | static void init_context(struct ctx_info *ctxi, struct cxlflash_cfg *cfg, | |
af2047ec MO |
820 | void *ctx, int ctxid, struct file *file, u32 perms, |
821 | u64 irqs) | |
5e6632d1 MO |
822 | { |
823 | struct afu *afu = cfg->afu; | |
65be2c79 | 824 | |
5e6632d1 | 825 | ctxi->rht_perms = perms; |
65be2c79 MO |
826 | ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl; |
827 | ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid); | |
af2047ec | 828 | ctxi->irqs = irqs; |
d84c198f | 829 | ctxi->pid = task_tgid_nr(current); /* tgid = pid */ |
65be2c79 | 830 | ctxi->ctx = ctx; |
44ef38f9 | 831 | ctxi->cfg = cfg; |
65be2c79 | 832 | ctxi->file = file; |
5e6632d1 | 833 | ctxi->initialized = true; |
65be2c79 | 834 | mutex_init(&ctxi->mutex); |
888baf06 | 835 | kref_init(&ctxi->kref); |
65be2c79 MO |
836 | INIT_LIST_HEAD(&ctxi->luns); |
837 | INIT_LIST_HEAD(&ctxi->list); /* initialize for list_empty() */ | |
65be2c79 MO |
838 | } |
839 | ||
888baf06 MO |
840 | /** |
841 | * remove_context() - context kref release handler | |
842 | * @kref: Kernel reference associated with context to be removed. | |
843 | * | |
844 | * When a context no longer has any references it can safely be removed | |
845 | * from global access and destroyed. Note that it is assumed the thread | |
846 | * relinquishing access to the context holds its mutex. | |
847 | */ | |
848 | static void remove_context(struct kref *kref) | |
849 | { | |
850 | struct ctx_info *ctxi = container_of(kref, struct ctx_info, kref); | |
851 | struct cxlflash_cfg *cfg = ctxi->cfg; | |
888baf06 MO |
852 | u64 ctxid = DECODE_CTXID(ctxi->ctxid); |
853 | ||
854 | /* Remove context from table/error list */ | |
855 | WARN_ON(!mutex_is_locked(&ctxi->mutex)); | |
856 | ctxi->unavail = true; | |
857 | mutex_unlock(&ctxi->mutex); | |
858 | mutex_lock(&cfg->ctx_tbl_list_mutex); | |
859 | mutex_lock(&ctxi->mutex); | |
860 | ||
861 | if (!list_empty(&ctxi->list)) | |
862 | list_del(&ctxi->list); | |
863 | cfg->ctx_tbl[ctxid] = NULL; | |
864 | mutex_unlock(&cfg->ctx_tbl_list_mutex); | |
865 | mutex_unlock(&ctxi->mutex); | |
866 | ||
867 | /* Context now completely uncoupled/unreachable */ | |
888baf06 | 868 | destroy_context(cfg, ctxi); |
888baf06 MO |
869 | } |
870 | ||
65be2c79 MO |
871 | /** |
872 | * _cxlflash_disk_detach() - detaches a LUN from a context | |
873 | * @sdev: SCSI device associated with LUN. | |
874 | * @ctxi: Context owning resources. | |
875 | * @detach: Detach ioctl data structure. | |
876 | * | |
877 | * As part of the detach, all per-context resources associated with the LUN | |
878 | * are cleaned up. When detaching the last LUN for a context, the context | |
879 | * itself is cleaned up and released. | |
880 | * | |
881 | * Return: 0 on success, -errno on failure | |
882 | */ | |
883 | static int _cxlflash_disk_detach(struct scsi_device *sdev, | |
884 | struct ctx_info *ctxi, | |
885 | struct dk_cxlflash_detach *detach) | |
886 | { | |
fb67d44d | 887 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
65be2c79 MO |
888 | struct device *dev = &cfg->dev->dev; |
889 | struct llun_info *lli = sdev->hostdata; | |
890 | struct lun_access *lun_access, *t; | |
891 | struct dk_cxlflash_release rel; | |
892 | bool put_ctx = false; | |
893 | ||
894 | int i; | |
895 | int rc = 0; | |
65be2c79 MO |
896 | u64 ctxid = DECODE_CTXID(detach->context_id), |
897 | rctxid = detach->context_id; | |
898 | ||
899 | dev_dbg(dev, "%s: ctxid=%llu\n", __func__, ctxid); | |
900 | ||
901 | if (!ctxi) { | |
902 | ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK); | |
903 | if (unlikely(!ctxi)) { | |
fb67d44d | 904 | dev_dbg(dev, "%s: Bad context ctxid=%llu\n", |
65be2c79 MO |
905 | __func__, ctxid); |
906 | rc = -EINVAL; | |
907 | goto out; | |
908 | } | |
909 | ||
910 | put_ctx = true; | |
911 | } | |
912 | ||
913 | /* Cleanup outstanding resources tied to this LUN */ | |
914 | if (ctxi->rht_out) { | |
915 | marshal_det_to_rele(detach, &rel); | |
916 | for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) { | |
917 | if (ctxi->rht_lun[i] == lli) { | |
918 | rel.rsrc_handle = i; | |
919 | _cxlflash_disk_release(sdev, ctxi, &rel); | |
920 | } | |
921 | ||
922 | /* No need to loop further if we're done */ | |
923 | if (ctxi->rht_out == 0) | |
924 | break; | |
925 | } | |
926 | } | |
927 | ||
928 | /* Take our LUN out of context, free the node */ | |
929 | list_for_each_entry_safe(lun_access, t, &ctxi->luns, list) | |
930 | if (lun_access->lli == lli) { | |
931 | list_del(&lun_access->list); | |
932 | kfree(lun_access); | |
933 | lun_access = NULL; | |
934 | break; | |
935 | } | |
936 | ||
888baf06 MO |
937 | /* |
938 | * Release the context reference and the sdev reference that | |
939 | * bound this LUN to the context. | |
940 | */ | |
c4a11827 MO |
941 | if (kref_put(&ctxi->kref, remove_context)) |
942 | put_ctx = false; | |
22fe1ae8 | 943 | scsi_device_put(sdev); |
65be2c79 MO |
944 | out: |
945 | if (put_ctx) | |
946 | put_context(ctxi); | |
947 | dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc); | |
948 | return rc; | |
949 | } | |
950 | ||
951 | static int cxlflash_disk_detach(struct scsi_device *sdev, | |
952 | struct dk_cxlflash_detach *detach) | |
953 | { | |
954 | return _cxlflash_disk_detach(sdev, NULL, detach); | |
955 | } | |
956 | ||
957 | /** | |
958 | * cxlflash_cxl_release() - release handler for adapter file descriptor | |
959 | * @inode: File-system inode associated with fd. | |
960 | * @file: File installed with adapter file descriptor. | |
961 | * | |
962 | * This routine is the release handler for the fops registered with | |
963 | * the CXL services on an initial attach for a context. It is called | |
cd34af40 MO |
964 | * when a close (explicity by the user or as part of a process tear |
965 | * down) is performed on the adapter file descriptor returned to the | |
966 | * user. The user should be aware that explicitly performing a close | |
967 | * considered catastrophic and subsequent usage of the superpipe API | |
968 | * with previously saved off tokens will fail. | |
65be2c79 | 969 | * |
cd34af40 MO |
970 | * This routine derives the context reference and calls detach for |
971 | * each LUN associated with the context.The final detach operation | |
972 | * causes the context itself to be freed. With exception to when the | |
973 | * CXL process element (context id) lookup fails (a case that should | |
974 | * theoretically never occur), every call into this routine results | |
975 | * in a complete freeing of a context. | |
65be2c79 MO |
976 | * |
977 | * Return: 0 on success | |
978 | */ | |
979 | static int cxlflash_cxl_release(struct inode *inode, struct file *file) | |
980 | { | |
b070545d | 981 | void *ctx = cxl_fops_get_context(file); |
65be2c79 MO |
982 | struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg, |
983 | cxl_fops); | |
984 | struct device *dev = &cfg->dev->dev; | |
985 | struct ctx_info *ctxi = NULL; | |
986 | struct dk_cxlflash_detach detach = { { 0 }, 0 }; | |
987 | struct lun_access *lun_access, *t; | |
988 | enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE; | |
989 | int ctxid; | |
990 | ||
991 | ctxid = cxl_process_element(ctx); | |
992 | if (unlikely(ctxid < 0)) { | |
fb67d44d | 993 | dev_err(dev, "%s: Context %p was closed ctxid=%d\n", |
65be2c79 MO |
994 | __func__, ctx, ctxid); |
995 | goto out; | |
996 | } | |
997 | ||
998 | ctxi = get_context(cfg, ctxid, file, ctrl); | |
999 | if (unlikely(!ctxi)) { | |
1000 | ctxi = get_context(cfg, ctxid, file, ctrl | CTX_CTRL_CLONE); | |
1001 | if (!ctxi) { | |
fb67d44d | 1002 | dev_dbg(dev, "%s: ctxid=%d already free\n", |
65be2c79 MO |
1003 | __func__, ctxid); |
1004 | goto out_release; | |
1005 | } | |
1006 | ||
fb67d44d | 1007 | dev_dbg(dev, "%s: Another process owns ctxid=%d\n", |
65be2c79 MO |
1008 | __func__, ctxid); |
1009 | put_context(ctxi); | |
1010 | goto out; | |
1011 | } | |
1012 | ||
fb67d44d | 1013 | dev_dbg(dev, "%s: close for ctxid=%d\n", __func__, ctxid); |
65be2c79 | 1014 | |
65be2c79 MO |
1015 | detach.context_id = ctxi->ctxid; |
1016 | list_for_each_entry_safe(lun_access, t, &ctxi->luns, list) | |
1017 | _cxlflash_disk_detach(lun_access->sdev, ctxi, &detach); | |
1018 | out_release: | |
1019 | cxl_fd_release(inode, file); | |
1020 | out: | |
1021 | dev_dbg(dev, "%s: returning\n", __func__); | |
1022 | return 0; | |
1023 | } | |
1024 | ||
1025 | /** | |
1026 | * unmap_context() - clears a previously established mapping | |
1027 | * @ctxi: Context owning the mapping. | |
1028 | * | |
1029 | * This routine is used to switch between the error notification page | |
1030 | * (dummy page of all 1's) and the real mapping (established by the CXL | |
1031 | * fault handler). | |
1032 | */ | |
1033 | static void unmap_context(struct ctx_info *ctxi) | |
1034 | { | |
1035 | unmap_mapping_range(ctxi->file->f_mapping, 0, 0, 1); | |
1036 | } | |
1037 | ||
1038 | /** | |
1039 | * get_err_page() - obtains and allocates the error notification page | |
fb67d44d | 1040 | * @cfg: Internal structure associated with the host. |
65be2c79 MO |
1041 | * |
1042 | * Return: error notification page on success, NULL on failure | |
1043 | */ | |
fb67d44d | 1044 | static struct page *get_err_page(struct cxlflash_cfg *cfg) |
65be2c79 MO |
1045 | { |
1046 | struct page *err_page = global.err_page; | |
fb67d44d | 1047 | struct device *dev = &cfg->dev->dev; |
65be2c79 MO |
1048 | |
1049 | if (unlikely(!err_page)) { | |
1050 | err_page = alloc_page(GFP_KERNEL); | |
1051 | if (unlikely(!err_page)) { | |
fb67d44d MO |
1052 | dev_err(dev, "%s: Unable to allocate err_page\n", |
1053 | __func__); | |
65be2c79 MO |
1054 | goto out; |
1055 | } | |
1056 | ||
1057 | memset(page_address(err_page), -1, PAGE_SIZE); | |
1058 | ||
1059 | /* Serialize update w/ other threads to avoid a leak */ | |
1060 | mutex_lock(&global.mutex); | |
1061 | if (likely(!global.err_page)) | |
1062 | global.err_page = err_page; | |
1063 | else { | |
1064 | __free_page(err_page); | |
1065 | err_page = global.err_page; | |
1066 | } | |
1067 | mutex_unlock(&global.mutex); | |
1068 | } | |
1069 | ||
1070 | out: | |
fb67d44d | 1071 | dev_dbg(dev, "%s: returning err_page=%p\n", __func__, err_page); |
65be2c79 MO |
1072 | return err_page; |
1073 | } | |
1074 | ||
1075 | /** | |
1076 | * cxlflash_mmap_fault() - mmap fault handler for adapter file descriptor | |
65be2c79 MO |
1077 | * @vmf: VM fault associated with current fault. |
1078 | * | |
1079 | * To support error notification via MMIO, faults are 'caught' by this routine | |
1080 | * that was inserted before passing back the adapter file descriptor on attach. | |
1081 | * When a fault occurs, this routine evaluates if error recovery is active and | |
1082 | * if so, installs the error page to 'notify' the user about the error state. | |
1083 | * During normal operation, the fault is simply handled by the original fault | |
1084 | * handler that was installed by CXL services as part of initializing the | |
1085 | * adapter file descriptor. The VMA's page protection bits are toggled to | |
1086 | * indicate cached/not-cached depending on the memory backing the fault. | |
1087 | * | |
1088 | * Return: 0 on success, VM_FAULT_SIGBUS on failure | |
1089 | */ | |
11bac800 | 1090 | static int cxlflash_mmap_fault(struct vm_fault *vmf) |
65be2c79 | 1091 | { |
11bac800 | 1092 | struct vm_area_struct *vma = vmf->vma; |
65be2c79 | 1093 | struct file *file = vma->vm_file; |
b070545d | 1094 | void *ctx = cxl_fops_get_context(file); |
65be2c79 MO |
1095 | struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg, |
1096 | cxl_fops); | |
1097 | struct device *dev = &cfg->dev->dev; | |
1098 | struct ctx_info *ctxi = NULL; | |
1099 | struct page *err_page = NULL; | |
1100 | enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE; | |
1101 | int rc = 0; | |
1102 | int ctxid; | |
1103 | ||
1104 | ctxid = cxl_process_element(ctx); | |
1105 | if (unlikely(ctxid < 0)) { | |
fb67d44d | 1106 | dev_err(dev, "%s: Context %p was closed ctxid=%d\n", |
65be2c79 MO |
1107 | __func__, ctx, ctxid); |
1108 | goto err; | |
1109 | } | |
1110 | ||
1111 | ctxi = get_context(cfg, ctxid, file, ctrl); | |
1112 | if (unlikely(!ctxi)) { | |
fb67d44d | 1113 | dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid); |
65be2c79 MO |
1114 | goto err; |
1115 | } | |
1116 | ||
de9f0b0c | 1117 | dev_dbg(dev, "%s: fault for context %d\n", __func__, ctxid); |
65be2c79 MO |
1118 | |
1119 | if (likely(!ctxi->err_recovery_active)) { | |
1120 | vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot); | |
11bac800 | 1121 | rc = ctxi->cxl_mmap_vmops->fault(vmf); |
65be2c79 | 1122 | } else { |
fb67d44d | 1123 | dev_dbg(dev, "%s: err recovery active, use err_page\n", |
65be2c79 MO |
1124 | __func__); |
1125 | ||
fb67d44d | 1126 | err_page = get_err_page(cfg); |
65be2c79 | 1127 | if (unlikely(!err_page)) { |
fb67d44d | 1128 | dev_err(dev, "%s: Could not get err_page\n", __func__); |
65be2c79 MO |
1129 | rc = VM_FAULT_RETRY; |
1130 | goto out; | |
1131 | } | |
1132 | ||
1133 | get_page(err_page); | |
1134 | vmf->page = err_page; | |
1135 | vma->vm_page_prot = pgprot_cached(vma->vm_page_prot); | |
1136 | } | |
1137 | ||
1138 | out: | |
1139 | if (likely(ctxi)) | |
1140 | put_context(ctxi); | |
1141 | dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc); | |
1142 | return rc; | |
1143 | ||
1144 | err: | |
1145 | rc = VM_FAULT_SIGBUS; | |
1146 | goto out; | |
1147 | } | |
1148 | ||
1149 | /* | |
1150 | * Local MMAP vmops to 'catch' faults | |
1151 | */ | |
1152 | static const struct vm_operations_struct cxlflash_mmap_vmops = { | |
1153 | .fault = cxlflash_mmap_fault, | |
1154 | }; | |
1155 | ||
1156 | /** | |
1157 | * cxlflash_cxl_mmap() - mmap handler for adapter file descriptor | |
1158 | * @file: File installed with adapter file descriptor. | |
1159 | * @vma: VM area associated with mapping. | |
1160 | * | |
1161 | * Installs local mmap vmops to 'catch' faults for error notification support. | |
1162 | * | |
1163 | * Return: 0 on success, -errno on failure | |
1164 | */ | |
1165 | static int cxlflash_cxl_mmap(struct file *file, struct vm_area_struct *vma) | |
1166 | { | |
b070545d | 1167 | void *ctx = cxl_fops_get_context(file); |
65be2c79 MO |
1168 | struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg, |
1169 | cxl_fops); | |
1170 | struct device *dev = &cfg->dev->dev; | |
1171 | struct ctx_info *ctxi = NULL; | |
1172 | enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE; | |
1173 | int ctxid; | |
1174 | int rc = 0; | |
1175 | ||
1176 | ctxid = cxl_process_element(ctx); | |
1177 | if (unlikely(ctxid < 0)) { | |
fb67d44d | 1178 | dev_err(dev, "%s: Context %p was closed ctxid=%d\n", |
65be2c79 MO |
1179 | __func__, ctx, ctxid); |
1180 | rc = -EIO; | |
1181 | goto out; | |
1182 | } | |
1183 | ||
1184 | ctxi = get_context(cfg, ctxid, file, ctrl); | |
1185 | if (unlikely(!ctxi)) { | |
fb67d44d | 1186 | dev_dbg(dev, "%s: Bad context ctxid=%d\n", __func__, ctxid); |
65be2c79 MO |
1187 | rc = -EIO; |
1188 | goto out; | |
1189 | } | |
1190 | ||
de9f0b0c | 1191 | dev_dbg(dev, "%s: mmap for context %d\n", __func__, ctxid); |
65be2c79 MO |
1192 | |
1193 | rc = cxl_fd_mmap(file, vma); | |
1194 | if (likely(!rc)) { | |
1195 | /* Insert ourself in the mmap fault handler path */ | |
1196 | ctxi->cxl_mmap_vmops = vma->vm_ops; | |
1197 | vma->vm_ops = &cxlflash_mmap_vmops; | |
1198 | } | |
1199 | ||
1200 | out: | |
1201 | if (likely(ctxi)) | |
1202 | put_context(ctxi); | |
1203 | return rc; | |
1204 | } | |
1205 | ||
17ead26f | 1206 | const struct file_operations cxlflash_cxl_fops = { |
65be2c79 MO |
1207 | .owner = THIS_MODULE, |
1208 | .mmap = cxlflash_cxl_mmap, | |
1209 | .release = cxlflash_cxl_release, | |
1210 | }; | |
1211 | ||
1212 | /** | |
1213 | * cxlflash_mark_contexts_error() - move contexts to error state and list | |
1214 | * @cfg: Internal structure associated with the host. | |
1215 | * | |
1216 | * A context is only moved over to the error list when there are no outstanding | |
1217 | * references to it. This ensures that a running operation has completed. | |
1218 | * | |
1219 | * Return: 0 on success, -errno on failure | |
1220 | */ | |
1221 | int cxlflash_mark_contexts_error(struct cxlflash_cfg *cfg) | |
1222 | { | |
1223 | int i, rc = 0; | |
1224 | struct ctx_info *ctxi = NULL; | |
1225 | ||
1226 | mutex_lock(&cfg->ctx_tbl_list_mutex); | |
1227 | ||
1228 | for (i = 0; i < MAX_CONTEXT; i++) { | |
1229 | ctxi = cfg->ctx_tbl[i]; | |
1230 | if (ctxi) { | |
1231 | mutex_lock(&ctxi->mutex); | |
1232 | cfg->ctx_tbl[i] = NULL; | |
1233 | list_add(&ctxi->list, &cfg->ctx_err_recovery); | |
1234 | ctxi->err_recovery_active = true; | |
1235 | ctxi->ctrl_map = NULL; | |
1236 | unmap_context(ctxi); | |
1237 | mutex_unlock(&ctxi->mutex); | |
1238 | } | |
1239 | } | |
1240 | ||
1241 | mutex_unlock(&cfg->ctx_tbl_list_mutex); | |
1242 | return rc; | |
1243 | } | |
1244 | ||
1245 | /* | |
1246 | * Dummy NULL fops | |
1247 | */ | |
1248 | static const struct file_operations null_fops = { | |
1249 | .owner = THIS_MODULE, | |
1250 | }; | |
1251 | ||
0a27ae51 MO |
1252 | /** |
1253 | * check_state() - checks and responds to the current adapter state | |
1254 | * @cfg: Internal structure associated with the host. | |
1255 | * | |
1256 | * This routine can block and should only be used on process context. | |
1257 | * It assumes that the caller is an ioctl thread and holding the ioctl | |
1258 | * read semaphore. This is temporarily let up across the wait to allow | |
1259 | * for draining actively running ioctls. Also note that when waking up | |
1260 | * from waiting in reset, the state is unknown and must be checked again | |
1261 | * before proceeding. | |
1262 | * | |
1263 | * Return: 0 on success, -errno on failure | |
1264 | */ | |
aacb4ff6 | 1265 | int check_state(struct cxlflash_cfg *cfg) |
0a27ae51 MO |
1266 | { |
1267 | struct device *dev = &cfg->dev->dev; | |
1268 | int rc = 0; | |
1269 | ||
1270 | retry: | |
1271 | switch (cfg->state) { | |
439e85c1 MO |
1272 | case STATE_RESET: |
1273 | dev_dbg(dev, "%s: Reset state, going to wait...\n", __func__); | |
0a27ae51 | 1274 | up_read(&cfg->ioctl_rwsem); |
439e85c1 MO |
1275 | rc = wait_event_interruptible(cfg->reset_waitq, |
1276 | cfg->state != STATE_RESET); | |
0a27ae51 MO |
1277 | down_read(&cfg->ioctl_rwsem); |
1278 | if (unlikely(rc)) | |
1279 | break; | |
1280 | goto retry; | |
1281 | case STATE_FAILTERM: | |
fb67d44d | 1282 | dev_dbg(dev, "%s: Failed/Terminating\n", __func__); |
0a27ae51 MO |
1283 | rc = -ENODEV; |
1284 | break; | |
1285 | default: | |
1286 | break; | |
1287 | } | |
1288 | ||
1289 | return rc; | |
1290 | } | |
1291 | ||
65be2c79 MO |
1292 | /** |
1293 | * cxlflash_disk_attach() - attach a LUN to a context | |
1294 | * @sdev: SCSI device associated with LUN. | |
1295 | * @attach: Attach ioctl data structure. | |
1296 | * | |
1297 | * Creates a context and attaches LUN to it. A LUN can only be attached | |
1298 | * one time to a context (subsequent attaches for the same context/LUN pair | |
1299 | * are not supported). Additional LUNs can be attached to a context by | |
1300 | * specifying the 'reuse' flag defined in the cxlflash_ioctl.h header. | |
1301 | * | |
1302 | * Return: 0 on success, -errno on failure | |
1303 | */ | |
1304 | static int cxlflash_disk_attach(struct scsi_device *sdev, | |
1305 | struct dk_cxlflash_attach *attach) | |
1306 | { | |
fb67d44d | 1307 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
65be2c79 MO |
1308 | struct device *dev = &cfg->dev->dev; |
1309 | struct afu *afu = cfg->afu; | |
1310 | struct llun_info *lli = sdev->hostdata; | |
1311 | struct glun_info *gli = lli->parent; | |
87623531 | 1312 | struct cxl_ioctl_start_work work = { 0 }; |
65be2c79 MO |
1313 | struct ctx_info *ctxi = NULL; |
1314 | struct lun_access *lun_access = NULL; | |
1315 | int rc = 0; | |
1316 | u32 perms; | |
1317 | int ctxid = -1; | |
af2047ec | 1318 | u64 irqs = attach->num_interrupts; |
696d0b0c | 1319 | u64 flags = 0UL; |
65be2c79 | 1320 | u64 rctxid = 0UL; |
8a96b52a | 1321 | struct file *file = NULL; |
65be2c79 | 1322 | |
b070545d | 1323 | void *ctx = NULL; |
65be2c79 MO |
1324 | |
1325 | int fd = -1; | |
1326 | ||
af2047ec | 1327 | if (irqs > 4) { |
65be2c79 | 1328 | dev_dbg(dev, "%s: Cannot support this many interrupts %llu\n", |
af2047ec | 1329 | __func__, irqs); |
65be2c79 MO |
1330 | rc = -EINVAL; |
1331 | goto out; | |
1332 | } | |
1333 | ||
1334 | if (gli->max_lba == 0) { | |
fb67d44d | 1335 | dev_dbg(dev, "%s: No capacity info for LUN=%016llx\n", |
65be2c79 MO |
1336 | __func__, lli->lun_id[sdev->channel]); |
1337 | rc = read_cap16(sdev, lli); | |
1338 | if (rc) { | |
fb67d44d | 1339 | dev_err(dev, "%s: Invalid device rc=%d\n", |
65be2c79 MO |
1340 | __func__, rc); |
1341 | rc = -ENODEV; | |
1342 | goto out; | |
1343 | } | |
fb67d44d MO |
1344 | dev_dbg(dev, "%s: LBA = %016llx\n", __func__, gli->max_lba); |
1345 | dev_dbg(dev, "%s: BLK_LEN = %08x\n", __func__, gli->blk_len); | |
65be2c79 MO |
1346 | } |
1347 | ||
1348 | if (attach->hdr.flags & DK_CXLFLASH_ATTACH_REUSE_CONTEXT) { | |
1349 | rctxid = attach->context_id; | |
1350 | ctxi = get_context(cfg, rctxid, NULL, 0); | |
1351 | if (!ctxi) { | |
fb67d44d | 1352 | dev_dbg(dev, "%s: Bad context rctxid=%016llx\n", |
65be2c79 MO |
1353 | __func__, rctxid); |
1354 | rc = -EINVAL; | |
1355 | goto out; | |
1356 | } | |
1357 | ||
1358 | list_for_each_entry(lun_access, &ctxi->luns, list) | |
1359 | if (lun_access->lli == lli) { | |
fb67d44d | 1360 | dev_dbg(dev, "%s: Already attached\n", |
65be2c79 MO |
1361 | __func__); |
1362 | rc = -EINVAL; | |
1363 | goto out; | |
1364 | } | |
1365 | } | |
1366 | ||
22fe1ae8 MO |
1367 | rc = scsi_device_get(sdev); |
1368 | if (unlikely(rc)) { | |
fb67d44d | 1369 | dev_err(dev, "%s: Unable to get sdev reference\n", __func__); |
22fe1ae8 MO |
1370 | goto out; |
1371 | } | |
1372 | ||
65be2c79 MO |
1373 | lun_access = kzalloc(sizeof(*lun_access), GFP_KERNEL); |
1374 | if (unlikely(!lun_access)) { | |
fb67d44d | 1375 | dev_err(dev, "%s: Unable to allocate lun_access\n", __func__); |
65be2c79 | 1376 | rc = -ENOMEM; |
8a96b52a | 1377 | goto err; |
65be2c79 MO |
1378 | } |
1379 | ||
1380 | lun_access->lli = lli; | |
1381 | lun_access->sdev = sdev; | |
1382 | ||
888baf06 | 1383 | /* Non-NULL context indicates reuse (another context reference) */ |
65be2c79 | 1384 | if (ctxi) { |
fb67d44d | 1385 | dev_dbg(dev, "%s: Reusing context for LUN rctxid=%016llx\n", |
65be2c79 | 1386 | __func__, rctxid); |
888baf06 | 1387 | kref_get(&ctxi->kref); |
65be2c79 | 1388 | list_add(&lun_access->list, &ctxi->luns); |
65be2c79 MO |
1389 | goto out_attach; |
1390 | } | |
1391 | ||
5d1952ac UK |
1392 | ctxi = create_context(cfg); |
1393 | if (unlikely(!ctxi)) { | |
fb67d44d | 1394 | dev_err(dev, "%s: Failed to create context ctxid=%d\n", |
5d1952ac | 1395 | __func__, ctxid); |
9ff87041 | 1396 | rc = -ENOMEM; |
5d1952ac UK |
1397 | goto err; |
1398 | } | |
1399 | ||
65be2c79 | 1400 | ctx = cxl_dev_context_init(cfg->dev); |
21891a45 | 1401 | if (IS_ERR_OR_NULL(ctx)) { |
65be2c79 MO |
1402 | dev_err(dev, "%s: Could not initialize context %p\n", |
1403 | __func__, ctx); | |
1404 | rc = -ENODEV; | |
8a96b52a | 1405 | goto err; |
65be2c79 MO |
1406 | } |
1407 | ||
87623531 MO |
1408 | work.num_interrupts = irqs; |
1409 | work.flags = CXL_START_WORK_NUM_IRQS; | |
5d1952ac | 1410 | |
87623531 | 1411 | rc = cxl_start_work(ctx, &work); |
5d1952ac UK |
1412 | if (unlikely(rc)) { |
1413 | dev_dbg(dev, "%s: Could not start context rc=%d\n", | |
1414 | __func__, rc); | |
1415 | goto err; | |
1416 | } | |
1417 | ||
65be2c79 | 1418 | ctxid = cxl_process_element(ctx); |
e37390be | 1419 | if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) { |
fb67d44d | 1420 | dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid); |
65be2c79 | 1421 | rc = -EPERM; |
8a96b52a | 1422 | goto err; |
65be2c79 MO |
1423 | } |
1424 | ||
1425 | file = cxl_get_fd(ctx, &cfg->cxl_fops, &fd); | |
1426 | if (unlikely(fd < 0)) { | |
1427 | rc = -ENODEV; | |
1428 | dev_err(dev, "%s: Could not get file descriptor\n", __func__); | |
8a96b52a | 1429 | goto err; |
65be2c79 MO |
1430 | } |
1431 | ||
1432 | /* Translate read/write O_* flags from fcntl.h to AFU permission bits */ | |
1433 | perms = SISL_RHT_PERM(attach->hdr.flags + 1); | |
1434 | ||
5e6632d1 | 1435 | /* Context mutex is locked upon return */ |
af2047ec | 1436 | init_context(ctxi, cfg, ctx, ctxid, file, perms, irqs); |
5e6632d1 | 1437 | |
65be2c79 MO |
1438 | rc = afu_attach(cfg, ctxi); |
1439 | if (unlikely(rc)) { | |
1440 | dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc); | |
8a96b52a | 1441 | goto err; |
65be2c79 MO |
1442 | } |
1443 | ||
1444 | /* | |
1445 | * No error paths after this point. Once the fd is installed it's | |
1446 | * visible to user space and can't be undone safely on this thread. | |
1447 | * There is no need to worry about a deadlock here because no one | |
1448 | * knows about us yet; we can be the only one holding our mutex. | |
1449 | */ | |
1450 | list_add(&lun_access->list, &ctxi->luns); | |
65be2c79 MO |
1451 | mutex_lock(&cfg->ctx_tbl_list_mutex); |
1452 | mutex_lock(&ctxi->mutex); | |
1453 | cfg->ctx_tbl[ctxid] = ctxi; | |
1454 | mutex_unlock(&cfg->ctx_tbl_list_mutex); | |
1455 | fd_install(fd, file); | |
1456 | ||
1457 | out_attach: | |
cd34af40 | 1458 | if (fd != -1) |
696d0b0c MO |
1459 | flags |= DK_CXLFLASH_APP_CLOSE_ADAP_FD; |
1460 | if (afu_is_sq_cmd_mode(afu)) | |
1461 | flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE; | |
cd34af40 | 1462 | |
696d0b0c | 1463 | attach->hdr.return_flags = flags; |
65be2c79 MO |
1464 | attach->context_id = ctxi->ctxid; |
1465 | attach->block_size = gli->blk_len; | |
1466 | attach->mmio_size = sizeof(afu->afu_map->hosts[0].harea); | |
1467 | attach->last_lba = gli->max_lba; | |
471a5a60 MK |
1468 | attach->max_xfer = sdev->host->max_sectors * MAX_SECTOR_UNIT; |
1469 | attach->max_xfer /= gli->blk_len; | |
65be2c79 MO |
1470 | |
1471 | out: | |
1472 | attach->adap_fd = fd; | |
1473 | ||
1474 | if (ctxi) | |
1475 | put_context(ctxi); | |
1476 | ||
1477 | dev_dbg(dev, "%s: returning ctxid=%d fd=%d bs=%lld rc=%d llba=%lld\n", | |
1478 | __func__, ctxid, fd, attach->block_size, rc, attach->last_lba); | |
1479 | return rc; | |
1480 | ||
8a96b52a MO |
1481 | err: |
1482 | /* Cleanup CXL context; okay to 'stop' even if it was not started */ | |
1483 | if (!IS_ERR_OR_NULL(ctx)) { | |
1484 | cxl_stop_context(ctx); | |
1485 | cxl_release_context(ctx); | |
1486 | ctx = NULL; | |
1487 | } | |
1488 | ||
65be2c79 MO |
1489 | /* |
1490 | * Here, we're overriding the fops with a dummy all-NULL fops because | |
1491 | * fput() calls the release fop, which will cause us to mistakenly | |
1492 | * call into the CXL code. Rather than try to add yet more complexity | |
1493 | * to that routine (cxlflash_cxl_release) we should try to fix the | |
1494 | * issue here. | |
1495 | */ | |
8a96b52a MO |
1496 | if (fd > 0) { |
1497 | file->f_op = &null_fops; | |
1498 | fput(file); | |
1499 | put_unused_fd(fd); | |
1500 | fd = -1; | |
1501 | file = NULL; | |
1502 | } | |
1503 | ||
41b99e1a | 1504 | /* Cleanup our context */ |
8a96b52a MO |
1505 | if (ctxi) { |
1506 | destroy_context(cfg, ctxi); | |
1507 | ctxi = NULL; | |
1508 | } | |
1509 | ||
65be2c79 | 1510 | kfree(lun_access); |
22fe1ae8 | 1511 | scsi_device_put(sdev); |
65be2c79 MO |
1512 | goto out; |
1513 | } | |
1514 | ||
1515 | /** | |
1516 | * recover_context() - recovers a context in error | |
1517 | * @cfg: Internal structure associated with the host. | |
1518 | * @ctxi: Context to release. | |
de9f0b0c | 1519 | * @adap_fd: Adapter file descriptor associated with new/recovered context. |
65be2c79 MO |
1520 | * |
1521 | * Restablishes the state for a context-in-error. | |
1522 | * | |
1523 | * Return: 0 on success, -errno on failure | |
1524 | */ | |
de9f0b0c MO |
1525 | static int recover_context(struct cxlflash_cfg *cfg, |
1526 | struct ctx_info *ctxi, | |
1527 | int *adap_fd) | |
65be2c79 MO |
1528 | { |
1529 | struct device *dev = &cfg->dev->dev; | |
1530 | int rc = 0; | |
cd34af40 | 1531 | int fd = -1; |
65be2c79 MO |
1532 | int ctxid = -1; |
1533 | struct file *file; | |
b070545d | 1534 | void *ctx; |
65be2c79 | 1535 | struct afu *afu = cfg->afu; |
87623531 | 1536 | struct cxl_ioctl_start_work work = { 0 }; |
65be2c79 MO |
1537 | |
1538 | ctx = cxl_dev_context_init(cfg->dev); | |
21891a45 | 1539 | if (IS_ERR_OR_NULL(ctx)) { |
65be2c79 MO |
1540 | dev_err(dev, "%s: Could not initialize context %p\n", |
1541 | __func__, ctx); | |
1542 | rc = -ENODEV; | |
1543 | goto out; | |
1544 | } | |
1545 | ||
87623531 MO |
1546 | work.num_interrupts = ctxi->irqs; |
1547 | work.flags = CXL_START_WORK_NUM_IRQS; | |
1548 | ||
1549 | rc = cxl_start_work(ctx, &work); | |
5d1952ac UK |
1550 | if (unlikely(rc)) { |
1551 | dev_dbg(dev, "%s: Could not start context rc=%d\n", | |
1552 | __func__, rc); | |
1553 | goto err1; | |
1554 | } | |
1555 | ||
65be2c79 | 1556 | ctxid = cxl_process_element(ctx); |
e37390be | 1557 | if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) { |
fb67d44d | 1558 | dev_err(dev, "%s: ctxid=%d invalid\n", __func__, ctxid); |
65be2c79 | 1559 | rc = -EPERM; |
5d1952ac | 1560 | goto err2; |
65be2c79 MO |
1561 | } |
1562 | ||
1563 | file = cxl_get_fd(ctx, &cfg->cxl_fops, &fd); | |
1564 | if (unlikely(fd < 0)) { | |
1565 | rc = -ENODEV; | |
1566 | dev_err(dev, "%s: Could not get file descriptor\n", __func__); | |
65be2c79 MO |
1567 | goto err2; |
1568 | } | |
1569 | ||
1570 | /* Update with new MMIO area based on updated context id */ | |
1571 | ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl; | |
1572 | ||
1573 | rc = afu_attach(cfg, ctxi); | |
1574 | if (rc) { | |
1575 | dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc); | |
1576 | goto err3; | |
1577 | } | |
1578 | ||
1579 | /* | |
1580 | * No error paths after this point. Once the fd is installed it's | |
1581 | * visible to user space and can't be undone safely on this thread. | |
1582 | */ | |
65be2c79 | 1583 | ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid); |
65be2c79 MO |
1584 | ctxi->ctx = ctx; |
1585 | ctxi->file = file; | |
1586 | ||
1587 | /* | |
1588 | * Put context back in table (note the reinit of the context list); | |
1589 | * we must first drop the context's mutex and then acquire it in | |
1590 | * order with the table/list mutex to avoid a deadlock - safe to do | |
1591 | * here because no one can find us at this moment in time. | |
1592 | */ | |
1593 | mutex_unlock(&ctxi->mutex); | |
1594 | mutex_lock(&cfg->ctx_tbl_list_mutex); | |
1595 | mutex_lock(&ctxi->mutex); | |
1596 | list_del_init(&ctxi->list); | |
1597 | cfg->ctx_tbl[ctxid] = ctxi; | |
1598 | mutex_unlock(&cfg->ctx_tbl_list_mutex); | |
1599 | fd_install(fd, file); | |
de9f0b0c | 1600 | *adap_fd = fd; |
65be2c79 MO |
1601 | out: |
1602 | dev_dbg(dev, "%s: returning ctxid=%d fd=%d rc=%d\n", | |
1603 | __func__, ctxid, fd, rc); | |
1604 | return rc; | |
1605 | ||
1606 | err3: | |
65be2c79 MO |
1607 | fput(file); |
1608 | put_unused_fd(fd); | |
5d1952ac UK |
1609 | err2: |
1610 | cxl_stop_context(ctx); | |
65be2c79 MO |
1611 | err1: |
1612 | cxl_release_context(ctx); | |
1613 | goto out; | |
1614 | } | |
1615 | ||
65be2c79 MO |
1616 | /** |
1617 | * cxlflash_afu_recover() - initiates AFU recovery | |
1618 | * @sdev: SCSI device associated with LUN. | |
1619 | * @recover: Recover ioctl data structure. | |
1620 | * | |
1621 | * Only a single recovery is allowed at a time to avoid exhausting CXL | |
1622 | * resources (leading to recovery failure) in the event that we're up | |
1623 | * against the maximum number of contexts limit. For similar reasons, | |
1624 | * a context recovery is retried if there are multiple recoveries taking | |
1625 | * place at the same time and the failure was due to CXL services being | |
1626 | * unable to keep up. | |
1627 | * | |
635f6b08 MK |
1628 | * As this routine is called on ioctl context, it holds the ioctl r/w |
1629 | * semaphore that is used to drain ioctls in recovery scenarios. The | |
1630 | * implementation to achieve the pacing described above (a local mutex) | |
1631 | * requires that the ioctl r/w semaphore be dropped and reacquired to | |
1632 | * avoid a 3-way deadlock when multiple process recoveries operate in | |
1633 | * parallel. | |
1634 | * | |
65be2c79 MO |
1635 | * Because a user can detect an error condition before the kernel, it is |
1636 | * quite possible for this routine to act as the kernel's EEH detection | |
1637 | * source (MMIO read of mbox_r). Because of this, there is a window of | |
1638 | * time where an EEH might have been detected but not yet 'serviced' | |
439e85c1 | 1639 | * (callback invoked, causing the device to enter reset state). To avoid |
65be2c79 MO |
1640 | * looping in this routine during that window, a 1 second sleep is in place |
1641 | * between the time the MMIO failure is detected and the time a wait on the | |
439e85c1 | 1642 | * reset wait queue is attempted via check_state(). |
65be2c79 MO |
1643 | * |
1644 | * Return: 0 on success, -errno on failure | |
1645 | */ | |
1646 | static int cxlflash_afu_recover(struct scsi_device *sdev, | |
1647 | struct dk_cxlflash_recover_afu *recover) | |
1648 | { | |
fb67d44d | 1649 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
65be2c79 MO |
1650 | struct device *dev = &cfg->dev->dev; |
1651 | struct llun_info *lli = sdev->hostdata; | |
1652 | struct afu *afu = cfg->afu; | |
1653 | struct ctx_info *ctxi = NULL; | |
1654 | struct mutex *mutex = &cfg->ctx_recovery_mutex; | |
bfc0bab1 | 1655 | struct hwq *hwq = get_hwq(afu, PRIMARY_HWQ); |
696d0b0c | 1656 | u64 flags; |
65be2c79 MO |
1657 | u64 ctxid = DECODE_CTXID(recover->context_id), |
1658 | rctxid = recover->context_id; | |
1659 | long reg; | |
1a9e3941 | 1660 | bool locked = true; |
65be2c79 | 1661 | int lretry = 20; /* up to 2 seconds */ |
de9f0b0c | 1662 | int new_adap_fd = -1; |
65be2c79 MO |
1663 | int rc = 0; |
1664 | ||
1665 | atomic_inc(&cfg->recovery_threads); | |
635f6b08 | 1666 | up_read(&cfg->ioctl_rwsem); |
65be2c79 | 1667 | rc = mutex_lock_interruptible(mutex); |
635f6b08 | 1668 | down_read(&cfg->ioctl_rwsem); |
1a9e3941 MO |
1669 | if (rc) { |
1670 | locked = false; | |
65be2c79 | 1671 | goto out; |
1a9e3941 MO |
1672 | } |
1673 | ||
635f6b08 MK |
1674 | rc = check_state(cfg); |
1675 | if (rc) { | |
fb67d44d | 1676 | dev_err(dev, "%s: Failed state rc=%d\n", __func__, rc); |
635f6b08 MK |
1677 | rc = -ENODEV; |
1678 | goto out; | |
1679 | } | |
65be2c79 | 1680 | |
fb67d44d | 1681 | dev_dbg(dev, "%s: reason=%016llx rctxid=%016llx\n", |
65be2c79 MO |
1682 | __func__, recover->reason, rctxid); |
1683 | ||
1684 | retry: | |
1685 | /* Ensure that this process is attached to the context */ | |
1686 | ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK); | |
1687 | if (unlikely(!ctxi)) { | |
fb67d44d | 1688 | dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid); |
65be2c79 MO |
1689 | rc = -EINVAL; |
1690 | goto out; | |
1691 | } | |
1692 | ||
1693 | if (ctxi->err_recovery_active) { | |
1694 | retry_recover: | |
de9f0b0c | 1695 | rc = recover_context(cfg, ctxi, &new_adap_fd); |
65be2c79 | 1696 | if (unlikely(rc)) { |
fb67d44d | 1697 | dev_err(dev, "%s: Recovery failed ctxid=%llu rc=%d\n", |
65be2c79 MO |
1698 | __func__, ctxid, rc); |
1699 | if ((rc == -ENODEV) && | |
1700 | ((atomic_read(&cfg->recovery_threads) > 1) || | |
1701 | (lretry--))) { | |
fb67d44d | 1702 | dev_dbg(dev, "%s: Going to try again\n", |
65be2c79 MO |
1703 | __func__); |
1704 | mutex_unlock(mutex); | |
1705 | msleep(100); | |
1706 | rc = mutex_lock_interruptible(mutex); | |
1a9e3941 MO |
1707 | if (rc) { |
1708 | locked = false; | |
65be2c79 | 1709 | goto out; |
1a9e3941 | 1710 | } |
65be2c79 MO |
1711 | goto retry_recover; |
1712 | } | |
1713 | ||
1714 | goto out; | |
1715 | } | |
1716 | ||
1717 | ctxi->err_recovery_active = false; | |
696d0b0c MO |
1718 | |
1719 | flags = DK_CXLFLASH_APP_CLOSE_ADAP_FD | | |
1720 | DK_CXLFLASH_RECOVER_AFU_CONTEXT_RESET; | |
1721 | if (afu_is_sq_cmd_mode(afu)) | |
1722 | flags |= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE; | |
1723 | ||
1724 | recover->hdr.return_flags = flags; | |
65be2c79 | 1725 | recover->context_id = ctxi->ctxid; |
de9f0b0c | 1726 | recover->adap_fd = new_adap_fd; |
65be2c79 | 1727 | recover->mmio_size = sizeof(afu->afu_map->hosts[0].harea); |
65be2c79 MO |
1728 | goto out; |
1729 | } | |
1730 | ||
1731 | /* Test if in error state */ | |
bfc0bab1 | 1732 | reg = readq_be(&hwq->ctrl_map->mbox_r); |
65be2c79 | 1733 | if (reg == -1) { |
0a27ae51 MO |
1734 | dev_dbg(dev, "%s: MMIO fail, wait for recovery.\n", __func__); |
1735 | ||
1736 | /* | |
1737 | * Before checking the state, put back the context obtained with | |
1738 | * get_context() as it is no longer needed and sleep for a short | |
1739 | * period of time (see prolog notes). | |
1740 | */ | |
1741 | put_context(ctxi); | |
65be2c79 MO |
1742 | ctxi = NULL; |
1743 | ssleep(1); | |
1744 | rc = check_state(cfg); | |
1745 | if (unlikely(rc)) | |
1746 | goto out; | |
1747 | goto retry; | |
1748 | } | |
1749 | ||
fb67d44d | 1750 | dev_dbg(dev, "%s: MMIO working, no recovery required\n", __func__); |
65be2c79 MO |
1751 | out: |
1752 | if (likely(ctxi)) | |
1753 | put_context(ctxi); | |
1a9e3941 MO |
1754 | if (locked) |
1755 | mutex_unlock(mutex); | |
65be2c79 MO |
1756 | atomic_dec_if_positive(&cfg->recovery_threads); |
1757 | return rc; | |
1758 | } | |
1759 | ||
1760 | /** | |
1761 | * process_sense() - evaluates and processes sense data | |
1762 | * @sdev: SCSI device associated with LUN. | |
1763 | * @verify: Verify ioctl data structure. | |
1764 | * | |
1765 | * Return: 0 on success, -errno on failure | |
1766 | */ | |
1767 | static int process_sense(struct scsi_device *sdev, | |
1768 | struct dk_cxlflash_verify *verify) | |
1769 | { | |
fb67d44d | 1770 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
65be2c79 MO |
1771 | struct device *dev = &cfg->dev->dev; |
1772 | struct llun_info *lli = sdev->hostdata; | |
1773 | struct glun_info *gli = lli->parent; | |
1774 | u64 prev_lba = gli->max_lba; | |
1775 | struct scsi_sense_hdr sshdr = { 0 }; | |
1776 | int rc = 0; | |
1777 | ||
1778 | rc = scsi_normalize_sense((const u8 *)&verify->sense_data, | |
1779 | DK_CXLFLASH_VERIFY_SENSE_LEN, &sshdr); | |
1780 | if (!rc) { | |
fb67d44d | 1781 | dev_err(dev, "%s: Failed to normalize sense data\n", __func__); |
65be2c79 MO |
1782 | rc = -EINVAL; |
1783 | goto out; | |
1784 | } | |
1785 | ||
1786 | switch (sshdr.sense_key) { | |
1787 | case NO_SENSE: | |
1788 | case RECOVERED_ERROR: | |
1789 | /* fall through */ | |
1790 | case NOT_READY: | |
1791 | break; | |
1792 | case UNIT_ATTENTION: | |
1793 | switch (sshdr.asc) { | |
1794 | case 0x29: /* Power on Reset or Device Reset */ | |
1795 | /* fall through */ | |
1796 | case 0x2A: /* Device settings/capacity changed */ | |
1797 | rc = read_cap16(sdev, lli); | |
1798 | if (rc) { | |
1799 | rc = -ENODEV; | |
1800 | break; | |
1801 | } | |
1802 | if (prev_lba != gli->max_lba) | |
1803 | dev_dbg(dev, "%s: Capacity changed old=%lld " | |
1804 | "new=%lld\n", __func__, prev_lba, | |
1805 | gli->max_lba); | |
1806 | break; | |
1807 | case 0x3F: /* Report LUNs changed, Rescan. */ | |
1808 | scsi_scan_host(cfg->host); | |
1809 | break; | |
1810 | default: | |
1811 | rc = -EIO; | |
1812 | break; | |
1813 | } | |
1814 | break; | |
1815 | default: | |
1816 | rc = -EIO; | |
1817 | break; | |
1818 | } | |
1819 | out: | |
1820 | dev_dbg(dev, "%s: sense_key %x asc %x ascq %x rc %d\n", __func__, | |
1821 | sshdr.sense_key, sshdr.asc, sshdr.ascq, rc); | |
1822 | return rc; | |
1823 | } | |
1824 | ||
1825 | /** | |
1826 | * cxlflash_disk_verify() - verifies a LUN is the same and handle size changes | |
1827 | * @sdev: SCSI device associated with LUN. | |
1828 | * @verify: Verify ioctl data structure. | |
1829 | * | |
1830 | * Return: 0 on success, -errno on failure | |
1831 | */ | |
1832 | static int cxlflash_disk_verify(struct scsi_device *sdev, | |
1833 | struct dk_cxlflash_verify *verify) | |
1834 | { | |
1835 | int rc = 0; | |
1836 | struct ctx_info *ctxi = NULL; | |
fb67d44d | 1837 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
65be2c79 MO |
1838 | struct device *dev = &cfg->dev->dev; |
1839 | struct llun_info *lli = sdev->hostdata; | |
1840 | struct glun_info *gli = lli->parent; | |
1841 | struct sisl_rht_entry *rhte = NULL; | |
1842 | res_hndl_t rhndl = verify->rsrc_handle; | |
1843 | u64 ctxid = DECODE_CTXID(verify->context_id), | |
1844 | rctxid = verify->context_id; | |
1845 | u64 last_lba = 0; | |
1846 | ||
fb67d44d MO |
1847 | dev_dbg(dev, "%s: ctxid=%llu rhndl=%016llx, hint=%016llx, " |
1848 | "flags=%016llx\n", __func__, ctxid, verify->rsrc_handle, | |
65be2c79 MO |
1849 | verify->hint, verify->hdr.flags); |
1850 | ||
1851 | ctxi = get_context(cfg, rctxid, lli, 0); | |
1852 | if (unlikely(!ctxi)) { | |
fb67d44d | 1853 | dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid); |
65be2c79 MO |
1854 | rc = -EINVAL; |
1855 | goto out; | |
1856 | } | |
1857 | ||
1858 | rhte = get_rhte(ctxi, rhndl, lli); | |
1859 | if (unlikely(!rhte)) { | |
fb67d44d | 1860 | dev_dbg(dev, "%s: Bad resource handle rhndl=%d\n", |
65be2c79 MO |
1861 | __func__, rhndl); |
1862 | rc = -EINVAL; | |
1863 | goto out; | |
1864 | } | |
1865 | ||
1866 | /* | |
1867 | * Look at the hint/sense to see if it requires us to redrive | |
1868 | * inquiry (i.e. the Unit attention is due to the WWN changing). | |
1869 | */ | |
1870 | if (verify->hint & DK_CXLFLASH_VERIFY_HINT_SENSE) { | |
8e782623 MO |
1871 | /* Can't hold mutex across process_sense/read_cap16, |
1872 | * since we could have an intervening EEH event. | |
1873 | */ | |
1874 | ctxi->unavail = true; | |
1875 | mutex_unlock(&ctxi->mutex); | |
65be2c79 MO |
1876 | rc = process_sense(sdev, verify); |
1877 | if (unlikely(rc)) { | |
1878 | dev_err(dev, "%s: Failed to validate sense data (%d)\n", | |
1879 | __func__, rc); | |
8e782623 MO |
1880 | mutex_lock(&ctxi->mutex); |
1881 | ctxi->unavail = false; | |
65be2c79 MO |
1882 | goto out; |
1883 | } | |
8e782623 MO |
1884 | mutex_lock(&ctxi->mutex); |
1885 | ctxi->unavail = false; | |
65be2c79 MO |
1886 | } |
1887 | ||
1888 | switch (gli->mode) { | |
1889 | case MODE_PHYSICAL: | |
1890 | last_lba = gli->max_lba; | |
1891 | break; | |
2cb79266 MO |
1892 | case MODE_VIRTUAL: |
1893 | /* Cast lxt_cnt to u64 for multiply to be treated as 64bit op */ | |
1894 | last_lba = ((u64)rhte->lxt_cnt * MC_CHUNK_SIZE * gli->blk_len); | |
1895 | last_lba /= CXLFLASH_BLOCK_SIZE; | |
1896 | last_lba--; | |
1897 | break; | |
65be2c79 MO |
1898 | default: |
1899 | WARN(1, "Unsupported LUN mode!"); | |
1900 | } | |
1901 | ||
1902 | verify->last_lba = last_lba; | |
1903 | ||
1904 | out: | |
1905 | if (likely(ctxi)) | |
1906 | put_context(ctxi); | |
fb67d44d | 1907 | dev_dbg(dev, "%s: returning rc=%d llba=%llx\n", |
65be2c79 MO |
1908 | __func__, rc, verify->last_lba); |
1909 | return rc; | |
1910 | } | |
1911 | ||
1912 | /** | |
1913 | * decode_ioctl() - translates an encoded ioctl to an easily identifiable string | |
1914 | * @cmd: The ioctl command to decode. | |
1915 | * | |
1916 | * Return: A string identifying the decoded ioctl. | |
1917 | */ | |
1918 | static char *decode_ioctl(int cmd) | |
1919 | { | |
1920 | switch (cmd) { | |
1921 | case DK_CXLFLASH_ATTACH: | |
1922 | return __stringify_1(DK_CXLFLASH_ATTACH); | |
1923 | case DK_CXLFLASH_USER_DIRECT: | |
1924 | return __stringify_1(DK_CXLFLASH_USER_DIRECT); | |
2cb79266 MO |
1925 | case DK_CXLFLASH_USER_VIRTUAL: |
1926 | return __stringify_1(DK_CXLFLASH_USER_VIRTUAL); | |
1927 | case DK_CXLFLASH_VLUN_RESIZE: | |
1928 | return __stringify_1(DK_CXLFLASH_VLUN_RESIZE); | |
65be2c79 MO |
1929 | case DK_CXLFLASH_RELEASE: |
1930 | return __stringify_1(DK_CXLFLASH_RELEASE); | |
1931 | case DK_CXLFLASH_DETACH: | |
1932 | return __stringify_1(DK_CXLFLASH_DETACH); | |
1933 | case DK_CXLFLASH_VERIFY: | |
1934 | return __stringify_1(DK_CXLFLASH_VERIFY); | |
2cb79266 MO |
1935 | case DK_CXLFLASH_VLUN_CLONE: |
1936 | return __stringify_1(DK_CXLFLASH_VLUN_CLONE); | |
65be2c79 MO |
1937 | case DK_CXLFLASH_RECOVER_AFU: |
1938 | return __stringify_1(DK_CXLFLASH_RECOVER_AFU); | |
1939 | case DK_CXLFLASH_MANAGE_LUN: | |
1940 | return __stringify_1(DK_CXLFLASH_MANAGE_LUN); | |
1941 | } | |
1942 | ||
1943 | return "UNKNOWN"; | |
1944 | } | |
1945 | ||
1946 | /** | |
1947 | * cxlflash_disk_direct_open() - opens a direct (physical) disk | |
1948 | * @sdev: SCSI device associated with LUN. | |
1949 | * @arg: UDirect ioctl data structure. | |
1950 | * | |
1951 | * On successful return, the user is informed of the resource handle | |
1952 | * to be used to identify the direct lun and the size (in blocks) of | |
1953 | * the direct lun in last LBA format. | |
1954 | * | |
1955 | * Return: 0 on success, -errno on failure | |
1956 | */ | |
1957 | static int cxlflash_disk_direct_open(struct scsi_device *sdev, void *arg) | |
1958 | { | |
fb67d44d | 1959 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
65be2c79 MO |
1960 | struct device *dev = &cfg->dev->dev; |
1961 | struct afu *afu = cfg->afu; | |
1962 | struct llun_info *lli = sdev->hostdata; | |
1963 | struct glun_info *gli = lli->parent; | |
c2c292f4 | 1964 | struct dk_cxlflash_release rel = { { 0 }, 0 }; |
65be2c79 MO |
1965 | |
1966 | struct dk_cxlflash_udirect *pphys = (struct dk_cxlflash_udirect *)arg; | |
1967 | ||
1968 | u64 ctxid = DECODE_CTXID(pphys->context_id), | |
1969 | rctxid = pphys->context_id; | |
1970 | u64 lun_size = 0; | |
1971 | u64 last_lba = 0; | |
1972 | u64 rsrc_handle = -1; | |
8fa4f177 | 1973 | u32 port = CHAN2PORTMASK(sdev->channel); |
65be2c79 MO |
1974 | |
1975 | int rc = 0; | |
1976 | ||
1977 | struct ctx_info *ctxi = NULL; | |
1978 | struct sisl_rht_entry *rhte = NULL; | |
1979 | ||
fb67d44d | 1980 | dev_dbg(dev, "%s: ctxid=%llu ls=%llu\n", __func__, ctxid, lun_size); |
65be2c79 MO |
1981 | |
1982 | rc = cxlflash_lun_attach(gli, MODE_PHYSICAL, false); | |
1983 | if (unlikely(rc)) { | |
fb67d44d | 1984 | dev_dbg(dev, "%s: Failed attach to LUN (PHYSICAL)\n", __func__); |
65be2c79 MO |
1985 | goto out; |
1986 | } | |
1987 | ||
1988 | ctxi = get_context(cfg, rctxid, lli, 0); | |
1989 | if (unlikely(!ctxi)) { | |
fb67d44d | 1990 | dev_dbg(dev, "%s: Bad context ctxid=%llu\n", __func__, ctxid); |
65be2c79 MO |
1991 | rc = -EINVAL; |
1992 | goto err1; | |
1993 | } | |
1994 | ||
1995 | rhte = rhte_checkout(ctxi, lli); | |
1996 | if (unlikely(!rhte)) { | |
fb67d44d MO |
1997 | dev_dbg(dev, "%s: Too many opens ctxid=%lld\n", |
1998 | __func__, ctxid); | |
65be2c79 MO |
1999 | rc = -EMFILE; /* too many opens */ |
2000 | goto err1; | |
2001 | } | |
2002 | ||
2003 | rsrc_handle = (rhte - ctxi->rht_start); | |
2004 | ||
2005 | rht_format1(rhte, lli->lun_id[sdev->channel], ctxi->rht_perms, port); | |
65be2c79 MO |
2006 | |
2007 | last_lba = gli->max_lba; | |
2008 | pphys->hdr.return_flags = 0; | |
2009 | pphys->last_lba = last_lba; | |
2010 | pphys->rsrc_handle = rsrc_handle; | |
2011 | ||
c2c292f4 UK |
2012 | rc = cxlflash_afu_sync(afu, ctxid, rsrc_handle, AFU_LW_SYNC); |
2013 | if (unlikely(rc)) { | |
2014 | dev_dbg(dev, "%s: AFU sync failed rc=%d\n", __func__, rc); | |
2015 | goto err2; | |
2016 | } | |
2017 | ||
65be2c79 MO |
2018 | out: |
2019 | if (likely(ctxi)) | |
2020 | put_context(ctxi); | |
fb67d44d | 2021 | dev_dbg(dev, "%s: returning handle=%llu rc=%d llba=%llu\n", |
65be2c79 MO |
2022 | __func__, rsrc_handle, rc, last_lba); |
2023 | return rc; | |
2024 | ||
c2c292f4 UK |
2025 | err2: |
2026 | marshal_udir_to_rele(pphys, &rel); | |
2027 | _cxlflash_disk_release(sdev, ctxi, &rel); | |
2028 | goto out; | |
65be2c79 MO |
2029 | err1: |
2030 | cxlflash_lun_detach(gli); | |
2031 | goto out; | |
2032 | } | |
2033 | ||
2034 | /** | |
2035 | * ioctl_common() - common IOCTL handler for driver | |
2036 | * @sdev: SCSI device associated with LUN. | |
2037 | * @cmd: IOCTL command. | |
2038 | * | |
2039 | * Handles common fencing operations that are valid for multiple ioctls. Always | |
2040 | * allow through ioctls that are cleanup oriented in nature, even when operating | |
2041 | * in a failed/terminating state. | |
2042 | * | |
2043 | * Return: 0 on success, -errno on failure | |
2044 | */ | |
2045 | static int ioctl_common(struct scsi_device *sdev, int cmd) | |
2046 | { | |
fb67d44d | 2047 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
65be2c79 MO |
2048 | struct device *dev = &cfg->dev->dev; |
2049 | struct llun_info *lli = sdev->hostdata; | |
2050 | int rc = 0; | |
2051 | ||
2052 | if (unlikely(!lli)) { | |
2053 | dev_dbg(dev, "%s: Unknown LUN\n", __func__); | |
2054 | rc = -EINVAL; | |
2055 | goto out; | |
2056 | } | |
2057 | ||
2058 | rc = check_state(cfg); | |
2059 | if (unlikely(rc) && (cfg->state == STATE_FAILTERM)) { | |
2060 | switch (cmd) { | |
2cb79266 | 2061 | case DK_CXLFLASH_VLUN_RESIZE: |
65be2c79 MO |
2062 | case DK_CXLFLASH_RELEASE: |
2063 | case DK_CXLFLASH_DETACH: | |
fb67d44d | 2064 | dev_dbg(dev, "%s: Command override rc=%d\n", |
65be2c79 MO |
2065 | __func__, rc); |
2066 | rc = 0; | |
2067 | break; | |
2068 | } | |
2069 | } | |
2070 | out: | |
2071 | return rc; | |
2072 | } | |
2073 | ||
2074 | /** | |
2075 | * cxlflash_ioctl() - IOCTL handler for driver | |
2076 | * @sdev: SCSI device associated with LUN. | |
2077 | * @cmd: IOCTL command. | |
2078 | * @arg: Userspace ioctl data structure. | |
2079 | * | |
0a27ae51 MO |
2080 | * A read/write semaphore is used to implement a 'drain' of currently |
2081 | * running ioctls. The read semaphore is taken at the beginning of each | |
2082 | * ioctl thread and released upon concluding execution. Additionally the | |
2083 | * semaphore should be released and then reacquired in any ioctl execution | |
2084 | * path which will wait for an event to occur that is outside the scope of | |
2085 | * the ioctl (i.e. an adapter reset). To drain the ioctls currently running, | |
2086 | * a thread simply needs to acquire the write semaphore. | |
2087 | * | |
65be2c79 MO |
2088 | * Return: 0 on success, -errno on failure |
2089 | */ | |
2090 | int cxlflash_ioctl(struct scsi_device *sdev, int cmd, void __user *arg) | |
2091 | { | |
2092 | typedef int (*sioctl) (struct scsi_device *, void *); | |
2093 | ||
fb67d44d | 2094 | struct cxlflash_cfg *cfg = shost_priv(sdev->host); |
65be2c79 MO |
2095 | struct device *dev = &cfg->dev->dev; |
2096 | struct afu *afu = cfg->afu; | |
2097 | struct dk_cxlflash_hdr *hdr; | |
2098 | char buf[sizeof(union cxlflash_ioctls)]; | |
2099 | size_t size = 0; | |
2100 | bool known_ioctl = false; | |
2101 | int idx; | |
2102 | int rc = 0; | |
2103 | struct Scsi_Host *shost = sdev->host; | |
2104 | sioctl do_ioctl = NULL; | |
2105 | ||
2106 | static const struct { | |
2107 | size_t size; | |
2108 | sioctl ioctl; | |
2109 | } ioctl_tbl[] = { /* NOTE: order matters here */ | |
2110 | {sizeof(struct dk_cxlflash_attach), (sioctl)cxlflash_disk_attach}, | |
2111 | {sizeof(struct dk_cxlflash_udirect), cxlflash_disk_direct_open}, | |
2112 | {sizeof(struct dk_cxlflash_release), (sioctl)cxlflash_disk_release}, | |
2113 | {sizeof(struct dk_cxlflash_detach), (sioctl)cxlflash_disk_detach}, | |
2114 | {sizeof(struct dk_cxlflash_verify), (sioctl)cxlflash_disk_verify}, | |
2115 | {sizeof(struct dk_cxlflash_recover_afu), (sioctl)cxlflash_afu_recover}, | |
2116 | {sizeof(struct dk_cxlflash_manage_lun), (sioctl)cxlflash_manage_lun}, | |
2cb79266 MO |
2117 | {sizeof(struct dk_cxlflash_uvirtual), cxlflash_disk_virtual_open}, |
2118 | {sizeof(struct dk_cxlflash_resize), (sioctl)cxlflash_vlun_resize}, | |
2119 | {sizeof(struct dk_cxlflash_clone), (sioctl)cxlflash_disk_clone}, | |
65be2c79 MO |
2120 | }; |
2121 | ||
0a27ae51 MO |
2122 | /* Hold read semaphore so we can drain if needed */ |
2123 | down_read(&cfg->ioctl_rwsem); | |
2124 | ||
65be2c79 MO |
2125 | /* Restrict command set to physical support only for internal LUN */ |
2126 | if (afu->internal_lun) | |
2127 | switch (cmd) { | |
2128 | case DK_CXLFLASH_RELEASE: | |
2cb79266 MO |
2129 | case DK_CXLFLASH_USER_VIRTUAL: |
2130 | case DK_CXLFLASH_VLUN_RESIZE: | |
2131 | case DK_CXLFLASH_VLUN_CLONE: | |
65be2c79 MO |
2132 | dev_dbg(dev, "%s: %s not supported for lun_mode=%d\n", |
2133 | __func__, decode_ioctl(cmd), afu->internal_lun); | |
2134 | rc = -EINVAL; | |
2135 | goto cxlflash_ioctl_exit; | |
2136 | } | |
2137 | ||
2138 | switch (cmd) { | |
2139 | case DK_CXLFLASH_ATTACH: | |
2140 | case DK_CXLFLASH_USER_DIRECT: | |
2141 | case DK_CXLFLASH_RELEASE: | |
2142 | case DK_CXLFLASH_DETACH: | |
2143 | case DK_CXLFLASH_VERIFY: | |
2144 | case DK_CXLFLASH_RECOVER_AFU: | |
2cb79266 MO |
2145 | case DK_CXLFLASH_USER_VIRTUAL: |
2146 | case DK_CXLFLASH_VLUN_RESIZE: | |
2147 | case DK_CXLFLASH_VLUN_CLONE: | |
65be2c79 MO |
2148 | dev_dbg(dev, "%s: %s (%08X) on dev(%d/%d/%d/%llu)\n", |
2149 | __func__, decode_ioctl(cmd), cmd, shost->host_no, | |
2150 | sdev->channel, sdev->id, sdev->lun); | |
2151 | rc = ioctl_common(sdev, cmd); | |
2152 | if (unlikely(rc)) | |
2153 | goto cxlflash_ioctl_exit; | |
2154 | ||
2155 | /* fall through */ | |
2156 | ||
2157 | case DK_CXLFLASH_MANAGE_LUN: | |
2158 | known_ioctl = true; | |
2159 | idx = _IOC_NR(cmd) - _IOC_NR(DK_CXLFLASH_ATTACH); | |
2160 | size = ioctl_tbl[idx].size; | |
2161 | do_ioctl = ioctl_tbl[idx].ioctl; | |
2162 | ||
2163 | if (likely(do_ioctl)) | |
2164 | break; | |
2165 | ||
2166 | /* fall through */ | |
2167 | default: | |
2168 | rc = -EINVAL; | |
2169 | goto cxlflash_ioctl_exit; | |
2170 | } | |
2171 | ||
2172 | if (unlikely(copy_from_user(&buf, arg, size))) { | |
fb67d44d | 2173 | dev_err(dev, "%s: copy_from_user() fail " |
65be2c79 MO |
2174 | "size=%lu cmd=%d (%s) arg=%p\n", |
2175 | __func__, size, cmd, decode_ioctl(cmd), arg); | |
2176 | rc = -EFAULT; | |
2177 | goto cxlflash_ioctl_exit; | |
2178 | } | |
2179 | ||
2180 | hdr = (struct dk_cxlflash_hdr *)&buf; | |
2181 | if (hdr->version != DK_CXLFLASH_VERSION_0) { | |
2182 | dev_dbg(dev, "%s: Version %u not supported for %s\n", | |
2183 | __func__, hdr->version, decode_ioctl(cmd)); | |
2184 | rc = -EINVAL; | |
2185 | goto cxlflash_ioctl_exit; | |
2186 | } | |
2187 | ||
2188 | if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->rsvd[2] || hdr->return_flags) { | |
fb67d44d | 2189 | dev_dbg(dev, "%s: Reserved/rflags populated\n", __func__); |
65be2c79 MO |
2190 | rc = -EINVAL; |
2191 | goto cxlflash_ioctl_exit; | |
2192 | } | |
2193 | ||
2194 | rc = do_ioctl(sdev, (void *)&buf); | |
2195 | if (likely(!rc)) | |
2196 | if (unlikely(copy_to_user(arg, &buf, size))) { | |
fb67d44d | 2197 | dev_err(dev, "%s: copy_to_user() fail " |
65be2c79 MO |
2198 | "size=%lu cmd=%d (%s) arg=%p\n", |
2199 | __func__, size, cmd, decode_ioctl(cmd), arg); | |
2200 | rc = -EFAULT; | |
2201 | } | |
2202 | ||
2203 | /* fall through to exit */ | |
2204 | ||
2205 | cxlflash_ioctl_exit: | |
0a27ae51 | 2206 | up_read(&cfg->ioctl_rwsem); |
65be2c79 MO |
2207 | if (unlikely(rc && known_ioctl)) |
2208 | dev_err(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) " | |
2209 | "returned rc %d\n", __func__, | |
2210 | decode_ioctl(cmd), cmd, shost->host_no, | |
2211 | sdev->channel, sdev->id, sdev->lun, rc); | |
2212 | else | |
2213 | dev_dbg(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) " | |
2214 | "returned rc %d\n", __func__, decode_ioctl(cmd), | |
2215 | cmd, shost->host_no, sdev->channel, sdev->id, | |
2216 | sdev->lun, rc); | |
2217 | return rc; | |
2218 | } |