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