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