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