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