Merge remote-tracking branch 'scsi/for-next'
[deliverable/linux.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.
5e6632d1
MO
791 * @file: Previously obtained file associated with CXL context.
792 * @perms: User-specified permissions.
5e6632d1
MO
793 */
794static void init_context(struct ctx_info *ctxi, struct cxlflash_cfg *cfg,
de9f0b0c
MO
795 struct cxl_context *ctx, int ctxid, struct file *file,
796 u32 perms)
5e6632d1
MO
797{
798 struct afu *afu = cfg->afu;
65be2c79 799
5e6632d1 800 ctxi->rht_perms = perms;
65be2c79
MO
801 ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
802 ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
65be2c79
MO
803 ctxi->pid = current->tgid; /* tgid = pid */
804 ctxi->ctx = ctx;
44ef38f9 805 ctxi->cfg = cfg;
65be2c79 806 ctxi->file = file;
5e6632d1 807 ctxi->initialized = true;
65be2c79 808 mutex_init(&ctxi->mutex);
888baf06 809 kref_init(&ctxi->kref);
65be2c79
MO
810 INIT_LIST_HEAD(&ctxi->luns);
811 INIT_LIST_HEAD(&ctxi->list); /* initialize for list_empty() */
65be2c79
MO
812}
813
888baf06
MO
814/**
815 * remove_context() - context kref release handler
816 * @kref: Kernel reference associated with context to be removed.
817 *
818 * When a context no longer has any references it can safely be removed
819 * from global access and destroyed. Note that it is assumed the thread
820 * relinquishing access to the context holds its mutex.
821 */
822static void remove_context(struct kref *kref)
823{
824 struct ctx_info *ctxi = container_of(kref, struct ctx_info, kref);
825 struct cxlflash_cfg *cfg = ctxi->cfg;
888baf06
MO
826 u64 ctxid = DECODE_CTXID(ctxi->ctxid);
827
828 /* Remove context from table/error list */
829 WARN_ON(!mutex_is_locked(&ctxi->mutex));
830 ctxi->unavail = true;
831 mutex_unlock(&ctxi->mutex);
832 mutex_lock(&cfg->ctx_tbl_list_mutex);
833 mutex_lock(&ctxi->mutex);
834
835 if (!list_empty(&ctxi->list))
836 list_del(&ctxi->list);
837 cfg->ctx_tbl[ctxid] = NULL;
838 mutex_unlock(&cfg->ctx_tbl_list_mutex);
839 mutex_unlock(&ctxi->mutex);
840
841 /* Context now completely uncoupled/unreachable */
888baf06 842 destroy_context(cfg, ctxi);
888baf06
MO
843}
844
65be2c79
MO
845/**
846 * _cxlflash_disk_detach() - detaches a LUN from a context
847 * @sdev: SCSI device associated with LUN.
848 * @ctxi: Context owning resources.
849 * @detach: Detach ioctl data structure.
850 *
851 * As part of the detach, all per-context resources associated with the LUN
852 * are cleaned up. When detaching the last LUN for a context, the context
853 * itself is cleaned up and released.
854 *
855 * Return: 0 on success, -errno on failure
856 */
857static int _cxlflash_disk_detach(struct scsi_device *sdev,
858 struct ctx_info *ctxi,
859 struct dk_cxlflash_detach *detach)
860{
861 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
862 struct device *dev = &cfg->dev->dev;
863 struct llun_info *lli = sdev->hostdata;
864 struct lun_access *lun_access, *t;
865 struct dk_cxlflash_release rel;
866 bool put_ctx = false;
867
868 int i;
869 int rc = 0;
65be2c79
MO
870 u64 ctxid = DECODE_CTXID(detach->context_id),
871 rctxid = detach->context_id;
872
873 dev_dbg(dev, "%s: ctxid=%llu\n", __func__, ctxid);
874
875 if (!ctxi) {
876 ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
877 if (unlikely(!ctxi)) {
878 dev_dbg(dev, "%s: Bad context! (%llu)\n",
879 __func__, ctxid);
880 rc = -EINVAL;
881 goto out;
882 }
883
884 put_ctx = true;
885 }
886
887 /* Cleanup outstanding resources tied to this LUN */
888 if (ctxi->rht_out) {
889 marshal_det_to_rele(detach, &rel);
890 for (i = 0; i < MAX_RHT_PER_CONTEXT; i++) {
891 if (ctxi->rht_lun[i] == lli) {
892 rel.rsrc_handle = i;
893 _cxlflash_disk_release(sdev, ctxi, &rel);
894 }
895
896 /* No need to loop further if we're done */
897 if (ctxi->rht_out == 0)
898 break;
899 }
900 }
901
902 /* Take our LUN out of context, free the node */
903 list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
904 if (lun_access->lli == lli) {
905 list_del(&lun_access->list);
906 kfree(lun_access);
907 lun_access = NULL;
908 break;
909 }
910
888baf06
MO
911 /*
912 * Release the context reference and the sdev reference that
913 * bound this LUN to the context.
914 */
915 put_ctx = !kref_put(&ctxi->kref, remove_context);
22fe1ae8 916 scsi_device_put(sdev);
65be2c79
MO
917out:
918 if (put_ctx)
919 put_context(ctxi);
920 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
921 return rc;
922}
923
924static int cxlflash_disk_detach(struct scsi_device *sdev,
925 struct dk_cxlflash_detach *detach)
926{
927 return _cxlflash_disk_detach(sdev, NULL, detach);
928}
929
930/**
931 * cxlflash_cxl_release() - release handler for adapter file descriptor
932 * @inode: File-system inode associated with fd.
933 * @file: File installed with adapter file descriptor.
934 *
935 * This routine is the release handler for the fops registered with
936 * the CXL services on an initial attach for a context. It is called
cd34af40
MO
937 * when a close (explicity by the user or as part of a process tear
938 * down) is performed on the adapter file descriptor returned to the
939 * user. The user should be aware that explicitly performing a close
940 * considered catastrophic and subsequent usage of the superpipe API
941 * with previously saved off tokens will fail.
65be2c79 942 *
cd34af40
MO
943 * This routine derives the context reference and calls detach for
944 * each LUN associated with the context.The final detach operation
945 * causes the context itself to be freed. With exception to when the
946 * CXL process element (context id) lookup fails (a case that should
947 * theoretically never occur), every call into this routine results
948 * in a complete freeing of a context.
65be2c79
MO
949 *
950 * Return: 0 on success
951 */
952static int cxlflash_cxl_release(struct inode *inode, struct file *file)
953{
954 struct cxl_context *ctx = cxl_fops_get_context(file);
955 struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
956 cxl_fops);
957 struct device *dev = &cfg->dev->dev;
958 struct ctx_info *ctxi = NULL;
959 struct dk_cxlflash_detach detach = { { 0 }, 0 };
960 struct lun_access *lun_access, *t;
961 enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
962 int ctxid;
963
964 ctxid = cxl_process_element(ctx);
965 if (unlikely(ctxid < 0)) {
966 dev_err(dev, "%s: Context %p was closed! (%d)\n",
967 __func__, ctx, ctxid);
968 goto out;
969 }
970
971 ctxi = get_context(cfg, ctxid, file, ctrl);
972 if (unlikely(!ctxi)) {
973 ctxi = get_context(cfg, ctxid, file, ctrl | CTX_CTRL_CLONE);
974 if (!ctxi) {
975 dev_dbg(dev, "%s: Context %d already free!\n",
976 __func__, ctxid);
977 goto out_release;
978 }
979
980 dev_dbg(dev, "%s: Another process owns context %d!\n",
981 __func__, ctxid);
982 put_context(ctxi);
983 goto out;
984 }
985
cd34af40 986 dev_dbg(dev, "%s: close for context %d\n", __func__, ctxid);
65be2c79 987
65be2c79
MO
988 detach.context_id = ctxi->ctxid;
989 list_for_each_entry_safe(lun_access, t, &ctxi->luns, list)
990 _cxlflash_disk_detach(lun_access->sdev, ctxi, &detach);
991out_release:
992 cxl_fd_release(inode, file);
993out:
994 dev_dbg(dev, "%s: returning\n", __func__);
995 return 0;
996}
997
998/**
999 * unmap_context() - clears a previously established mapping
1000 * @ctxi: Context owning the mapping.
1001 *
1002 * This routine is used to switch between the error notification page
1003 * (dummy page of all 1's) and the real mapping (established by the CXL
1004 * fault handler).
1005 */
1006static void unmap_context(struct ctx_info *ctxi)
1007{
1008 unmap_mapping_range(ctxi->file->f_mapping, 0, 0, 1);
1009}
1010
1011/**
1012 * get_err_page() - obtains and allocates the error notification page
1013 *
1014 * Return: error notification page on success, NULL on failure
1015 */
1016static struct page *get_err_page(void)
1017{
1018 struct page *err_page = global.err_page;
1019
1020 if (unlikely(!err_page)) {
1021 err_page = alloc_page(GFP_KERNEL);
1022 if (unlikely(!err_page)) {
1023 pr_err("%s: Unable to allocate err_page!\n", __func__);
1024 goto out;
1025 }
1026
1027 memset(page_address(err_page), -1, PAGE_SIZE);
1028
1029 /* Serialize update w/ other threads to avoid a leak */
1030 mutex_lock(&global.mutex);
1031 if (likely(!global.err_page))
1032 global.err_page = err_page;
1033 else {
1034 __free_page(err_page);
1035 err_page = global.err_page;
1036 }
1037 mutex_unlock(&global.mutex);
1038 }
1039
1040out:
1041 pr_debug("%s: returning err_page=%p\n", __func__, err_page);
1042 return err_page;
1043}
1044
1045/**
1046 * cxlflash_mmap_fault() - mmap fault handler for adapter file descriptor
1047 * @vma: VM area associated with mapping.
1048 * @vmf: VM fault associated with current fault.
1049 *
1050 * To support error notification via MMIO, faults are 'caught' by this routine
1051 * that was inserted before passing back the adapter file descriptor on attach.
1052 * When a fault occurs, this routine evaluates if error recovery is active and
1053 * if so, installs the error page to 'notify' the user about the error state.
1054 * During normal operation, the fault is simply handled by the original fault
1055 * handler that was installed by CXL services as part of initializing the
1056 * adapter file descriptor. The VMA's page protection bits are toggled to
1057 * indicate cached/not-cached depending on the memory backing the fault.
1058 *
1059 * Return: 0 on success, VM_FAULT_SIGBUS on failure
1060 */
1061static int cxlflash_mmap_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1062{
1063 struct file *file = vma->vm_file;
1064 struct cxl_context *ctx = cxl_fops_get_context(file);
1065 struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
1066 cxl_fops);
1067 struct device *dev = &cfg->dev->dev;
1068 struct ctx_info *ctxi = NULL;
1069 struct page *err_page = NULL;
1070 enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
1071 int rc = 0;
1072 int ctxid;
1073
1074 ctxid = cxl_process_element(ctx);
1075 if (unlikely(ctxid < 0)) {
1076 dev_err(dev, "%s: Context %p was closed! (%d)\n",
1077 __func__, ctx, ctxid);
1078 goto err;
1079 }
1080
1081 ctxi = get_context(cfg, ctxid, file, ctrl);
1082 if (unlikely(!ctxi)) {
1083 dev_dbg(dev, "%s: Bad context! (%d)\n", __func__, ctxid);
1084 goto err;
1085 }
1086
de9f0b0c 1087 dev_dbg(dev, "%s: fault for context %d\n", __func__, ctxid);
65be2c79
MO
1088
1089 if (likely(!ctxi->err_recovery_active)) {
1090 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1091 rc = ctxi->cxl_mmap_vmops->fault(vma, vmf);
1092 } else {
1093 dev_dbg(dev, "%s: err recovery active, use err_page!\n",
1094 __func__);
1095
1096 err_page = get_err_page();
1097 if (unlikely(!err_page)) {
1098 dev_err(dev, "%s: Could not obtain error page!\n",
1099 __func__);
1100 rc = VM_FAULT_RETRY;
1101 goto out;
1102 }
1103
1104 get_page(err_page);
1105 vmf->page = err_page;
1106 vma->vm_page_prot = pgprot_cached(vma->vm_page_prot);
1107 }
1108
1109out:
1110 if (likely(ctxi))
1111 put_context(ctxi);
1112 dev_dbg(dev, "%s: returning rc=%d\n", __func__, rc);
1113 return rc;
1114
1115err:
1116 rc = VM_FAULT_SIGBUS;
1117 goto out;
1118}
1119
1120/*
1121 * Local MMAP vmops to 'catch' faults
1122 */
1123static const struct vm_operations_struct cxlflash_mmap_vmops = {
1124 .fault = cxlflash_mmap_fault,
1125};
1126
1127/**
1128 * cxlflash_cxl_mmap() - mmap handler for adapter file descriptor
1129 * @file: File installed with adapter file descriptor.
1130 * @vma: VM area associated with mapping.
1131 *
1132 * Installs local mmap vmops to 'catch' faults for error notification support.
1133 *
1134 * Return: 0 on success, -errno on failure
1135 */
1136static int cxlflash_cxl_mmap(struct file *file, struct vm_area_struct *vma)
1137{
1138 struct cxl_context *ctx = cxl_fops_get_context(file);
1139 struct cxlflash_cfg *cfg = container_of(file->f_op, struct cxlflash_cfg,
1140 cxl_fops);
1141 struct device *dev = &cfg->dev->dev;
1142 struct ctx_info *ctxi = NULL;
1143 enum ctx_ctrl ctrl = CTX_CTRL_ERR_FALLBACK | CTX_CTRL_FILE;
1144 int ctxid;
1145 int rc = 0;
1146
1147 ctxid = cxl_process_element(ctx);
1148 if (unlikely(ctxid < 0)) {
1149 dev_err(dev, "%s: Context %p was closed! (%d)\n",
1150 __func__, ctx, ctxid);
1151 rc = -EIO;
1152 goto out;
1153 }
1154
1155 ctxi = get_context(cfg, ctxid, file, ctrl);
1156 if (unlikely(!ctxi)) {
1157 dev_dbg(dev, "%s: Bad context! (%d)\n", __func__, ctxid);
1158 rc = -EIO;
1159 goto out;
1160 }
1161
de9f0b0c 1162 dev_dbg(dev, "%s: mmap for context %d\n", __func__, ctxid);
65be2c79
MO
1163
1164 rc = cxl_fd_mmap(file, vma);
1165 if (likely(!rc)) {
1166 /* Insert ourself in the mmap fault handler path */
1167 ctxi->cxl_mmap_vmops = vma->vm_ops;
1168 vma->vm_ops = &cxlflash_mmap_vmops;
1169 }
1170
1171out:
1172 if (likely(ctxi))
1173 put_context(ctxi);
1174 return rc;
1175}
1176
17ead26f 1177const struct file_operations cxlflash_cxl_fops = {
65be2c79
MO
1178 .owner = THIS_MODULE,
1179 .mmap = cxlflash_cxl_mmap,
1180 .release = cxlflash_cxl_release,
1181};
1182
1183/**
1184 * cxlflash_mark_contexts_error() - move contexts to error state and list
1185 * @cfg: Internal structure associated with the host.
1186 *
1187 * A context is only moved over to the error list when there are no outstanding
1188 * references to it. This ensures that a running operation has completed.
1189 *
1190 * Return: 0 on success, -errno on failure
1191 */
1192int cxlflash_mark_contexts_error(struct cxlflash_cfg *cfg)
1193{
1194 int i, rc = 0;
1195 struct ctx_info *ctxi = NULL;
1196
1197 mutex_lock(&cfg->ctx_tbl_list_mutex);
1198
1199 for (i = 0; i < MAX_CONTEXT; i++) {
1200 ctxi = cfg->ctx_tbl[i];
1201 if (ctxi) {
1202 mutex_lock(&ctxi->mutex);
1203 cfg->ctx_tbl[i] = NULL;
1204 list_add(&ctxi->list, &cfg->ctx_err_recovery);
1205 ctxi->err_recovery_active = true;
1206 ctxi->ctrl_map = NULL;
1207 unmap_context(ctxi);
1208 mutex_unlock(&ctxi->mutex);
1209 }
1210 }
1211
1212 mutex_unlock(&cfg->ctx_tbl_list_mutex);
1213 return rc;
1214}
1215
1216/*
1217 * Dummy NULL fops
1218 */
1219static const struct file_operations null_fops = {
1220 .owner = THIS_MODULE,
1221};
1222
0a27ae51
MO
1223/**
1224 * check_state() - checks and responds to the current adapter state
1225 * @cfg: Internal structure associated with the host.
1226 *
1227 * This routine can block and should only be used on process context.
1228 * It assumes that the caller is an ioctl thread and holding the ioctl
1229 * read semaphore. This is temporarily let up across the wait to allow
1230 * for draining actively running ioctls. Also note that when waking up
1231 * from waiting in reset, the state is unknown and must be checked again
1232 * before proceeding.
1233 *
1234 * Return: 0 on success, -errno on failure
1235 */
aacb4ff6 1236int check_state(struct cxlflash_cfg *cfg)
0a27ae51
MO
1237{
1238 struct device *dev = &cfg->dev->dev;
1239 int rc = 0;
1240
1241retry:
1242 switch (cfg->state) {
439e85c1
MO
1243 case STATE_RESET:
1244 dev_dbg(dev, "%s: Reset state, going to wait...\n", __func__);
0a27ae51 1245 up_read(&cfg->ioctl_rwsem);
439e85c1
MO
1246 rc = wait_event_interruptible(cfg->reset_waitq,
1247 cfg->state != STATE_RESET);
0a27ae51
MO
1248 down_read(&cfg->ioctl_rwsem);
1249 if (unlikely(rc))
1250 break;
1251 goto retry;
1252 case STATE_FAILTERM:
1253 dev_dbg(dev, "%s: Failed/Terminating!\n", __func__);
1254 rc = -ENODEV;
1255 break;
1256 default:
1257 break;
1258 }
1259
1260 return rc;
1261}
1262
65be2c79
MO
1263/**
1264 * cxlflash_disk_attach() - attach a LUN to a context
1265 * @sdev: SCSI device associated with LUN.
1266 * @attach: Attach ioctl data structure.
1267 *
1268 * Creates a context and attaches LUN to it. A LUN can only be attached
1269 * one time to a context (subsequent attaches for the same context/LUN pair
1270 * are not supported). Additional LUNs can be attached to a context by
1271 * specifying the 'reuse' flag defined in the cxlflash_ioctl.h header.
1272 *
1273 * Return: 0 on success, -errno on failure
1274 */
1275static int cxlflash_disk_attach(struct scsi_device *sdev,
1276 struct dk_cxlflash_attach *attach)
1277{
1278 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
1279 struct device *dev = &cfg->dev->dev;
1280 struct afu *afu = cfg->afu;
1281 struct llun_info *lli = sdev->hostdata;
1282 struct glun_info *gli = lli->parent;
1283 struct cxl_ioctl_start_work *work;
1284 struct ctx_info *ctxi = NULL;
1285 struct lun_access *lun_access = NULL;
1286 int rc = 0;
1287 u32 perms;
1288 int ctxid = -1;
1289 u64 rctxid = 0UL;
8a96b52a 1290 struct file *file = NULL;
65be2c79 1291
8a96b52a 1292 struct cxl_context *ctx = NULL;
65be2c79
MO
1293
1294 int fd = -1;
1295
65be2c79
MO
1296 if (attach->num_interrupts > 4) {
1297 dev_dbg(dev, "%s: Cannot support this many interrupts %llu\n",
1298 __func__, attach->num_interrupts);
1299 rc = -EINVAL;
1300 goto out;
1301 }
1302
1303 if (gli->max_lba == 0) {
1304 dev_dbg(dev, "%s: No capacity info for this LUN (%016llX)\n",
1305 __func__, lli->lun_id[sdev->channel]);
1306 rc = read_cap16(sdev, lli);
1307 if (rc) {
1308 dev_err(dev, "%s: Invalid device! (%d)\n",
1309 __func__, rc);
1310 rc = -ENODEV;
1311 goto out;
1312 }
1313 dev_dbg(dev, "%s: LBA = %016llX\n", __func__, gli->max_lba);
1314 dev_dbg(dev, "%s: BLK_LEN = %08X\n", __func__, gli->blk_len);
1315 }
1316
1317 if (attach->hdr.flags & DK_CXLFLASH_ATTACH_REUSE_CONTEXT) {
1318 rctxid = attach->context_id;
1319 ctxi = get_context(cfg, rctxid, NULL, 0);
1320 if (!ctxi) {
1321 dev_dbg(dev, "%s: Bad context! (%016llX)\n",
1322 __func__, rctxid);
1323 rc = -EINVAL;
1324 goto out;
1325 }
1326
1327 list_for_each_entry(lun_access, &ctxi->luns, list)
1328 if (lun_access->lli == lli) {
1329 dev_dbg(dev, "%s: Already attached!\n",
1330 __func__);
1331 rc = -EINVAL;
1332 goto out;
1333 }
1334 }
1335
22fe1ae8
MO
1336 rc = scsi_device_get(sdev);
1337 if (unlikely(rc)) {
1338 dev_err(dev, "%s: Unable to get sdev reference!\n", __func__);
1339 goto out;
1340 }
1341
65be2c79
MO
1342 lun_access = kzalloc(sizeof(*lun_access), GFP_KERNEL);
1343 if (unlikely(!lun_access)) {
1344 dev_err(dev, "%s: Unable to allocate lun_access!\n", __func__);
1345 rc = -ENOMEM;
8a96b52a 1346 goto err;
65be2c79
MO
1347 }
1348
1349 lun_access->lli = lli;
1350 lun_access->sdev = sdev;
1351
888baf06 1352 /* Non-NULL context indicates reuse (another context reference) */
65be2c79
MO
1353 if (ctxi) {
1354 dev_dbg(dev, "%s: Reusing context for LUN! (%016llX)\n",
1355 __func__, rctxid);
888baf06 1356 kref_get(&ctxi->kref);
65be2c79 1357 list_add(&lun_access->list, &ctxi->luns);
65be2c79
MO
1358 goto out_attach;
1359 }
1360
5d1952ac
UK
1361 ctxi = create_context(cfg);
1362 if (unlikely(!ctxi)) {
1363 dev_err(dev, "%s: Failed to create context! (%d)\n",
1364 __func__, ctxid);
1365 goto err;
1366 }
1367
65be2c79 1368 ctx = cxl_dev_context_init(cfg->dev);
21891a45 1369 if (IS_ERR_OR_NULL(ctx)) {
65be2c79
MO
1370 dev_err(dev, "%s: Could not initialize context %p\n",
1371 __func__, ctx);
1372 rc = -ENODEV;
8a96b52a 1373 goto err;
65be2c79
MO
1374 }
1375
5d1952ac
UK
1376 work = &ctxi->work;
1377 work->num_interrupts = attach->num_interrupts;
1378 work->flags = CXL_START_WORK_NUM_IRQS;
1379
1380 rc = cxl_start_work(ctx, work);
1381 if (unlikely(rc)) {
1382 dev_dbg(dev, "%s: Could not start context rc=%d\n",
1383 __func__, rc);
1384 goto err;
1385 }
1386
65be2c79 1387 ctxid = cxl_process_element(ctx);
e37390be 1388 if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
65be2c79
MO
1389 dev_err(dev, "%s: ctxid (%d) invalid!\n", __func__, ctxid);
1390 rc = -EPERM;
8a96b52a 1391 goto err;
65be2c79
MO
1392 }
1393
1394 file = cxl_get_fd(ctx, &cfg->cxl_fops, &fd);
1395 if (unlikely(fd < 0)) {
1396 rc = -ENODEV;
1397 dev_err(dev, "%s: Could not get file descriptor\n", __func__);
8a96b52a 1398 goto err;
65be2c79
MO
1399 }
1400
1401 /* Translate read/write O_* flags from fcntl.h to AFU permission bits */
1402 perms = SISL_RHT_PERM(attach->hdr.flags + 1);
1403
5e6632d1 1404 /* Context mutex is locked upon return */
de9f0b0c 1405 init_context(ctxi, cfg, ctx, ctxid, file, perms);
5e6632d1 1406
65be2c79
MO
1407 rc = afu_attach(cfg, ctxi);
1408 if (unlikely(rc)) {
1409 dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
8a96b52a 1410 goto err;
65be2c79
MO
1411 }
1412
1413 /*
1414 * No error paths after this point. Once the fd is installed it's
1415 * visible to user space and can't be undone safely on this thread.
1416 * There is no need to worry about a deadlock here because no one
1417 * knows about us yet; we can be the only one holding our mutex.
1418 */
1419 list_add(&lun_access->list, &ctxi->luns);
65be2c79
MO
1420 mutex_lock(&cfg->ctx_tbl_list_mutex);
1421 mutex_lock(&ctxi->mutex);
1422 cfg->ctx_tbl[ctxid] = ctxi;
1423 mutex_unlock(&cfg->ctx_tbl_list_mutex);
1424 fd_install(fd, file);
1425
1426out_attach:
cd34af40
MO
1427 if (fd != -1)
1428 attach->hdr.return_flags = DK_CXLFLASH_APP_CLOSE_ADAP_FD;
1429 else
1430 attach->hdr.return_flags = 0;
1431
65be2c79
MO
1432 attach->context_id = ctxi->ctxid;
1433 attach->block_size = gli->blk_len;
1434 attach->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
1435 attach->last_lba = gli->max_lba;
471a5a60
MK
1436 attach->max_xfer = sdev->host->max_sectors * MAX_SECTOR_UNIT;
1437 attach->max_xfer /= gli->blk_len;
65be2c79
MO
1438
1439out:
1440 attach->adap_fd = fd;
1441
1442 if (ctxi)
1443 put_context(ctxi);
1444
1445 dev_dbg(dev, "%s: returning ctxid=%d fd=%d bs=%lld rc=%d llba=%lld\n",
1446 __func__, ctxid, fd, attach->block_size, rc, attach->last_lba);
1447 return rc;
1448
8a96b52a
MO
1449err:
1450 /* Cleanup CXL context; okay to 'stop' even if it was not started */
1451 if (!IS_ERR_OR_NULL(ctx)) {
1452 cxl_stop_context(ctx);
1453 cxl_release_context(ctx);
1454 ctx = NULL;
1455 }
1456
65be2c79
MO
1457 /*
1458 * Here, we're overriding the fops with a dummy all-NULL fops because
1459 * fput() calls the release fop, which will cause us to mistakenly
1460 * call into the CXL code. Rather than try to add yet more complexity
1461 * to that routine (cxlflash_cxl_release) we should try to fix the
1462 * issue here.
1463 */
8a96b52a
MO
1464 if (fd > 0) {
1465 file->f_op = &null_fops;
1466 fput(file);
1467 put_unused_fd(fd);
1468 fd = -1;
1469 file = NULL;
1470 }
1471
41b99e1a 1472 /* Cleanup our context */
8a96b52a
MO
1473 if (ctxi) {
1474 destroy_context(cfg, ctxi);
1475 ctxi = NULL;
1476 }
1477
65be2c79 1478 kfree(lun_access);
22fe1ae8 1479 scsi_device_put(sdev);
65be2c79
MO
1480 goto out;
1481}
1482
1483/**
1484 * recover_context() - recovers a context in error
1485 * @cfg: Internal structure associated with the host.
1486 * @ctxi: Context to release.
de9f0b0c 1487 * @adap_fd: Adapter file descriptor associated with new/recovered context.
65be2c79
MO
1488 *
1489 * Restablishes the state for a context-in-error.
1490 *
1491 * Return: 0 on success, -errno on failure
1492 */
de9f0b0c
MO
1493static int recover_context(struct cxlflash_cfg *cfg,
1494 struct ctx_info *ctxi,
1495 int *adap_fd)
65be2c79
MO
1496{
1497 struct device *dev = &cfg->dev->dev;
1498 int rc = 0;
cd34af40 1499 int fd = -1;
65be2c79
MO
1500 int ctxid = -1;
1501 struct file *file;
1502 struct cxl_context *ctx;
1503 struct afu *afu = cfg->afu;
1504
1505 ctx = cxl_dev_context_init(cfg->dev);
21891a45 1506 if (IS_ERR_OR_NULL(ctx)) {
65be2c79
MO
1507 dev_err(dev, "%s: Could not initialize context %p\n",
1508 __func__, ctx);
1509 rc = -ENODEV;
1510 goto out;
1511 }
1512
5d1952ac
UK
1513 rc = cxl_start_work(ctx, &ctxi->work);
1514 if (unlikely(rc)) {
1515 dev_dbg(dev, "%s: Could not start context rc=%d\n",
1516 __func__, rc);
1517 goto err1;
1518 }
1519
65be2c79 1520 ctxid = cxl_process_element(ctx);
e37390be 1521 if (unlikely((ctxid >= MAX_CONTEXT) || (ctxid < 0))) {
65be2c79
MO
1522 dev_err(dev, "%s: ctxid (%d) invalid!\n", __func__, ctxid);
1523 rc = -EPERM;
5d1952ac 1524 goto err2;
65be2c79
MO
1525 }
1526
1527 file = cxl_get_fd(ctx, &cfg->cxl_fops, &fd);
1528 if (unlikely(fd < 0)) {
1529 rc = -ENODEV;
1530 dev_err(dev, "%s: Could not get file descriptor\n", __func__);
65be2c79
MO
1531 goto err2;
1532 }
1533
1534 /* Update with new MMIO area based on updated context id */
1535 ctxi->ctrl_map = &afu->afu_map->ctrls[ctxid].ctrl;
1536
1537 rc = afu_attach(cfg, ctxi);
1538 if (rc) {
1539 dev_err(dev, "%s: Could not attach AFU rc %d\n", __func__, rc);
1540 goto err3;
1541 }
1542
1543 /*
1544 * No error paths after this point. Once the fd is installed it's
1545 * visible to user space and can't be undone safely on this thread.
1546 */
65be2c79 1547 ctxi->ctxid = ENCODE_CTXID(ctxi, ctxid);
65be2c79
MO
1548 ctxi->ctx = ctx;
1549 ctxi->file = file;
1550
1551 /*
1552 * Put context back in table (note the reinit of the context list);
1553 * we must first drop the context's mutex and then acquire it in
1554 * order with the table/list mutex to avoid a deadlock - safe to do
1555 * here because no one can find us at this moment in time.
1556 */
1557 mutex_unlock(&ctxi->mutex);
1558 mutex_lock(&cfg->ctx_tbl_list_mutex);
1559 mutex_lock(&ctxi->mutex);
1560 list_del_init(&ctxi->list);
1561 cfg->ctx_tbl[ctxid] = ctxi;
1562 mutex_unlock(&cfg->ctx_tbl_list_mutex);
1563 fd_install(fd, file);
de9f0b0c 1564 *adap_fd = fd;
65be2c79
MO
1565out:
1566 dev_dbg(dev, "%s: returning ctxid=%d fd=%d rc=%d\n",
1567 __func__, ctxid, fd, rc);
1568 return rc;
1569
1570err3:
65be2c79
MO
1571 fput(file);
1572 put_unused_fd(fd);
5d1952ac
UK
1573err2:
1574 cxl_stop_context(ctx);
65be2c79
MO
1575err1:
1576 cxl_release_context(ctx);
1577 goto out;
1578}
1579
65be2c79
MO
1580/**
1581 * cxlflash_afu_recover() - initiates AFU recovery
1582 * @sdev: SCSI device associated with LUN.
1583 * @recover: Recover ioctl data structure.
1584 *
1585 * Only a single recovery is allowed at a time to avoid exhausting CXL
1586 * resources (leading to recovery failure) in the event that we're up
1587 * against the maximum number of contexts limit. For similar reasons,
1588 * a context recovery is retried if there are multiple recoveries taking
1589 * place at the same time and the failure was due to CXL services being
1590 * unable to keep up.
1591 *
635f6b08
MK
1592 * As this routine is called on ioctl context, it holds the ioctl r/w
1593 * semaphore that is used to drain ioctls in recovery scenarios. The
1594 * implementation to achieve the pacing described above (a local mutex)
1595 * requires that the ioctl r/w semaphore be dropped and reacquired to
1596 * avoid a 3-way deadlock when multiple process recoveries operate in
1597 * parallel.
1598 *
65be2c79
MO
1599 * Because a user can detect an error condition before the kernel, it is
1600 * quite possible for this routine to act as the kernel's EEH detection
1601 * source (MMIO read of mbox_r). Because of this, there is a window of
1602 * time where an EEH might have been detected but not yet 'serviced'
439e85c1 1603 * (callback invoked, causing the device to enter reset state). To avoid
65be2c79
MO
1604 * looping in this routine during that window, a 1 second sleep is in place
1605 * between the time the MMIO failure is detected and the time a wait on the
439e85c1 1606 * reset wait queue is attempted via check_state().
65be2c79
MO
1607 *
1608 * Return: 0 on success, -errno on failure
1609 */
1610static int cxlflash_afu_recover(struct scsi_device *sdev,
1611 struct dk_cxlflash_recover_afu *recover)
1612{
1613 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
1614 struct device *dev = &cfg->dev->dev;
1615 struct llun_info *lli = sdev->hostdata;
1616 struct afu *afu = cfg->afu;
1617 struct ctx_info *ctxi = NULL;
1618 struct mutex *mutex = &cfg->ctx_recovery_mutex;
1619 u64 ctxid = DECODE_CTXID(recover->context_id),
1620 rctxid = recover->context_id;
1621 long reg;
1622 int lretry = 20; /* up to 2 seconds */
de9f0b0c 1623 int new_adap_fd = -1;
65be2c79
MO
1624 int rc = 0;
1625
1626 atomic_inc(&cfg->recovery_threads);
635f6b08 1627 up_read(&cfg->ioctl_rwsem);
65be2c79 1628 rc = mutex_lock_interruptible(mutex);
635f6b08 1629 down_read(&cfg->ioctl_rwsem);
65be2c79
MO
1630 if (rc)
1631 goto out;
635f6b08
MK
1632 rc = check_state(cfg);
1633 if (rc) {
1634 dev_err(dev, "%s: Failed state! rc=%d\n", __func__, rc);
1635 rc = -ENODEV;
1636 goto out;
1637 }
65be2c79
MO
1638
1639 dev_dbg(dev, "%s: reason 0x%016llX rctxid=%016llX\n",
1640 __func__, recover->reason, rctxid);
1641
1642retry:
1643 /* Ensure that this process is attached to the context */
1644 ctxi = get_context(cfg, rctxid, lli, CTX_CTRL_ERR_FALLBACK);
1645 if (unlikely(!ctxi)) {
1646 dev_dbg(dev, "%s: Bad context! (%llu)\n", __func__, ctxid);
1647 rc = -EINVAL;
1648 goto out;
1649 }
1650
1651 if (ctxi->err_recovery_active) {
1652retry_recover:
de9f0b0c 1653 rc = recover_context(cfg, ctxi, &new_adap_fd);
65be2c79
MO
1654 if (unlikely(rc)) {
1655 dev_err(dev, "%s: Recovery failed for context %llu (rc=%d)\n",
1656 __func__, ctxid, rc);
1657 if ((rc == -ENODEV) &&
1658 ((atomic_read(&cfg->recovery_threads) > 1) ||
1659 (lretry--))) {
1660 dev_dbg(dev, "%s: Going to try again!\n",
1661 __func__);
1662 mutex_unlock(mutex);
1663 msleep(100);
1664 rc = mutex_lock_interruptible(mutex);
1665 if (rc)
1666 goto out;
1667 goto retry_recover;
1668 }
1669
1670 goto out;
1671 }
1672
1673 ctxi->err_recovery_active = false;
1674 recover->context_id = ctxi->ctxid;
de9f0b0c 1675 recover->adap_fd = new_adap_fd;
65be2c79 1676 recover->mmio_size = sizeof(afu->afu_map->hosts[0].harea);
cd34af40 1677 recover->hdr.return_flags = DK_CXLFLASH_APP_CLOSE_ADAP_FD |
65be2c79
MO
1678 DK_CXLFLASH_RECOVER_AFU_CONTEXT_RESET;
1679 goto out;
1680 }
1681
1682 /* Test if in error state */
1683 reg = readq_be(&afu->ctrl_map->mbox_r);
1684 if (reg == -1) {
0a27ae51
MO
1685 dev_dbg(dev, "%s: MMIO fail, wait for recovery.\n", __func__);
1686
1687 /*
1688 * Before checking the state, put back the context obtained with
1689 * get_context() as it is no longer needed and sleep for a short
1690 * period of time (see prolog notes).
1691 */
1692 put_context(ctxi);
65be2c79
MO
1693 ctxi = NULL;
1694 ssleep(1);
1695 rc = check_state(cfg);
1696 if (unlikely(rc))
1697 goto out;
1698 goto retry;
1699 }
1700
1701 dev_dbg(dev, "%s: MMIO working, no recovery required!\n", __func__);
1702out:
1703 if (likely(ctxi))
1704 put_context(ctxi);
1705 mutex_unlock(mutex);
1706 atomic_dec_if_positive(&cfg->recovery_threads);
1707 return rc;
1708}
1709
1710/**
1711 * process_sense() - evaluates and processes sense data
1712 * @sdev: SCSI device associated with LUN.
1713 * @verify: Verify ioctl data structure.
1714 *
1715 * Return: 0 on success, -errno on failure
1716 */
1717static int process_sense(struct scsi_device *sdev,
1718 struct dk_cxlflash_verify *verify)
1719{
1720 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
1721 struct device *dev = &cfg->dev->dev;
1722 struct llun_info *lli = sdev->hostdata;
1723 struct glun_info *gli = lli->parent;
1724 u64 prev_lba = gli->max_lba;
1725 struct scsi_sense_hdr sshdr = { 0 };
1726 int rc = 0;
1727
1728 rc = scsi_normalize_sense((const u8 *)&verify->sense_data,
1729 DK_CXLFLASH_VERIFY_SENSE_LEN, &sshdr);
1730 if (!rc) {
1731 dev_err(dev, "%s: Failed to normalize sense data!\n", __func__);
1732 rc = -EINVAL;
1733 goto out;
1734 }
1735
1736 switch (sshdr.sense_key) {
1737 case NO_SENSE:
1738 case RECOVERED_ERROR:
1739 /* fall through */
1740 case NOT_READY:
1741 break;
1742 case UNIT_ATTENTION:
1743 switch (sshdr.asc) {
1744 case 0x29: /* Power on Reset or Device Reset */
1745 /* fall through */
1746 case 0x2A: /* Device settings/capacity changed */
1747 rc = read_cap16(sdev, lli);
1748 if (rc) {
1749 rc = -ENODEV;
1750 break;
1751 }
1752 if (prev_lba != gli->max_lba)
1753 dev_dbg(dev, "%s: Capacity changed old=%lld "
1754 "new=%lld\n", __func__, prev_lba,
1755 gli->max_lba);
1756 break;
1757 case 0x3F: /* Report LUNs changed, Rescan. */
1758 scsi_scan_host(cfg->host);
1759 break;
1760 default:
1761 rc = -EIO;
1762 break;
1763 }
1764 break;
1765 default:
1766 rc = -EIO;
1767 break;
1768 }
1769out:
1770 dev_dbg(dev, "%s: sense_key %x asc %x ascq %x rc %d\n", __func__,
1771 sshdr.sense_key, sshdr.asc, sshdr.ascq, rc);
1772 return rc;
1773}
1774
1775/**
1776 * cxlflash_disk_verify() - verifies a LUN is the same and handle size changes
1777 * @sdev: SCSI device associated with LUN.
1778 * @verify: Verify ioctl data structure.
1779 *
1780 * Return: 0 on success, -errno on failure
1781 */
1782static int cxlflash_disk_verify(struct scsi_device *sdev,
1783 struct dk_cxlflash_verify *verify)
1784{
1785 int rc = 0;
1786 struct ctx_info *ctxi = NULL;
1787 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
1788 struct device *dev = &cfg->dev->dev;
1789 struct llun_info *lli = sdev->hostdata;
1790 struct glun_info *gli = lli->parent;
1791 struct sisl_rht_entry *rhte = NULL;
1792 res_hndl_t rhndl = verify->rsrc_handle;
1793 u64 ctxid = DECODE_CTXID(verify->context_id),
1794 rctxid = verify->context_id;
1795 u64 last_lba = 0;
1796
1797 dev_dbg(dev, "%s: ctxid=%llu rhndl=%016llX, hint=%016llX, "
1798 "flags=%016llX\n", __func__, ctxid, verify->rsrc_handle,
1799 verify->hint, verify->hdr.flags);
1800
1801 ctxi = get_context(cfg, rctxid, lli, 0);
1802 if (unlikely(!ctxi)) {
1803 dev_dbg(dev, "%s: Bad context! (%llu)\n", __func__, ctxid);
1804 rc = -EINVAL;
1805 goto out;
1806 }
1807
1808 rhte = get_rhte(ctxi, rhndl, lli);
1809 if (unlikely(!rhte)) {
1810 dev_dbg(dev, "%s: Bad resource handle! (%d)\n",
1811 __func__, rhndl);
1812 rc = -EINVAL;
1813 goto out;
1814 }
1815
1816 /*
1817 * Look at the hint/sense to see if it requires us to redrive
1818 * inquiry (i.e. the Unit attention is due to the WWN changing).
1819 */
1820 if (verify->hint & DK_CXLFLASH_VERIFY_HINT_SENSE) {
8e782623
MO
1821 /* Can't hold mutex across process_sense/read_cap16,
1822 * since we could have an intervening EEH event.
1823 */
1824 ctxi->unavail = true;
1825 mutex_unlock(&ctxi->mutex);
65be2c79
MO
1826 rc = process_sense(sdev, verify);
1827 if (unlikely(rc)) {
1828 dev_err(dev, "%s: Failed to validate sense data (%d)\n",
1829 __func__, rc);
8e782623
MO
1830 mutex_lock(&ctxi->mutex);
1831 ctxi->unavail = false;
65be2c79
MO
1832 goto out;
1833 }
8e782623
MO
1834 mutex_lock(&ctxi->mutex);
1835 ctxi->unavail = false;
65be2c79
MO
1836 }
1837
1838 switch (gli->mode) {
1839 case MODE_PHYSICAL:
1840 last_lba = gli->max_lba;
1841 break;
2cb79266
MO
1842 case MODE_VIRTUAL:
1843 /* Cast lxt_cnt to u64 for multiply to be treated as 64bit op */
1844 last_lba = ((u64)rhte->lxt_cnt * MC_CHUNK_SIZE * gli->blk_len);
1845 last_lba /= CXLFLASH_BLOCK_SIZE;
1846 last_lba--;
1847 break;
65be2c79
MO
1848 default:
1849 WARN(1, "Unsupported LUN mode!");
1850 }
1851
1852 verify->last_lba = last_lba;
1853
1854out:
1855 if (likely(ctxi))
1856 put_context(ctxi);
1857 dev_dbg(dev, "%s: returning rc=%d llba=%llX\n",
1858 __func__, rc, verify->last_lba);
1859 return rc;
1860}
1861
1862/**
1863 * decode_ioctl() - translates an encoded ioctl to an easily identifiable string
1864 * @cmd: The ioctl command to decode.
1865 *
1866 * Return: A string identifying the decoded ioctl.
1867 */
1868static char *decode_ioctl(int cmd)
1869{
1870 switch (cmd) {
1871 case DK_CXLFLASH_ATTACH:
1872 return __stringify_1(DK_CXLFLASH_ATTACH);
1873 case DK_CXLFLASH_USER_DIRECT:
1874 return __stringify_1(DK_CXLFLASH_USER_DIRECT);
2cb79266
MO
1875 case DK_CXLFLASH_USER_VIRTUAL:
1876 return __stringify_1(DK_CXLFLASH_USER_VIRTUAL);
1877 case DK_CXLFLASH_VLUN_RESIZE:
1878 return __stringify_1(DK_CXLFLASH_VLUN_RESIZE);
65be2c79
MO
1879 case DK_CXLFLASH_RELEASE:
1880 return __stringify_1(DK_CXLFLASH_RELEASE);
1881 case DK_CXLFLASH_DETACH:
1882 return __stringify_1(DK_CXLFLASH_DETACH);
1883 case DK_CXLFLASH_VERIFY:
1884 return __stringify_1(DK_CXLFLASH_VERIFY);
2cb79266
MO
1885 case DK_CXLFLASH_VLUN_CLONE:
1886 return __stringify_1(DK_CXLFLASH_VLUN_CLONE);
65be2c79
MO
1887 case DK_CXLFLASH_RECOVER_AFU:
1888 return __stringify_1(DK_CXLFLASH_RECOVER_AFU);
1889 case DK_CXLFLASH_MANAGE_LUN:
1890 return __stringify_1(DK_CXLFLASH_MANAGE_LUN);
1891 }
1892
1893 return "UNKNOWN";
1894}
1895
1896/**
1897 * cxlflash_disk_direct_open() - opens a direct (physical) disk
1898 * @sdev: SCSI device associated with LUN.
1899 * @arg: UDirect ioctl data structure.
1900 *
1901 * On successful return, the user is informed of the resource handle
1902 * to be used to identify the direct lun and the size (in blocks) of
1903 * the direct lun in last LBA format.
1904 *
1905 * Return: 0 on success, -errno on failure
1906 */
1907static int cxlflash_disk_direct_open(struct scsi_device *sdev, void *arg)
1908{
1909 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
1910 struct device *dev = &cfg->dev->dev;
1911 struct afu *afu = cfg->afu;
1912 struct llun_info *lli = sdev->hostdata;
1913 struct glun_info *gli = lli->parent;
1914
1915 struct dk_cxlflash_udirect *pphys = (struct dk_cxlflash_udirect *)arg;
1916
1917 u64 ctxid = DECODE_CTXID(pphys->context_id),
1918 rctxid = pphys->context_id;
1919 u64 lun_size = 0;
1920 u64 last_lba = 0;
1921 u64 rsrc_handle = -1;
1922 u32 port = CHAN2PORT(sdev->channel);
1923
1924 int rc = 0;
1925
1926 struct ctx_info *ctxi = NULL;
1927 struct sisl_rht_entry *rhte = NULL;
1928
1929 pr_debug("%s: ctxid=%llu ls=0x%llx\n", __func__, ctxid, lun_size);
1930
1931 rc = cxlflash_lun_attach(gli, MODE_PHYSICAL, false);
1932 if (unlikely(rc)) {
1933 dev_dbg(dev, "%s: Failed to attach to LUN! (PHYSICAL)\n",
1934 __func__);
1935 goto out;
1936 }
1937
1938 ctxi = get_context(cfg, rctxid, lli, 0);
1939 if (unlikely(!ctxi)) {
1940 dev_dbg(dev, "%s: Bad context! (%llu)\n", __func__, ctxid);
1941 rc = -EINVAL;
1942 goto err1;
1943 }
1944
1945 rhte = rhte_checkout(ctxi, lli);
1946 if (unlikely(!rhte)) {
1947 dev_dbg(dev, "%s: too many opens for this context\n", __func__);
1948 rc = -EMFILE; /* too many opens */
1949 goto err1;
1950 }
1951
1952 rsrc_handle = (rhte - ctxi->rht_start);
1953
1954 rht_format1(rhte, lli->lun_id[sdev->channel], ctxi->rht_perms, port);
1955 cxlflash_afu_sync(afu, ctxid, rsrc_handle, AFU_LW_SYNC);
1956
1957 last_lba = gli->max_lba;
1958 pphys->hdr.return_flags = 0;
1959 pphys->last_lba = last_lba;
1960 pphys->rsrc_handle = rsrc_handle;
1961
1962out:
1963 if (likely(ctxi))
1964 put_context(ctxi);
1965 dev_dbg(dev, "%s: returning handle 0x%llx rc=%d llba %lld\n",
1966 __func__, rsrc_handle, rc, last_lba);
1967 return rc;
1968
1969err1:
1970 cxlflash_lun_detach(gli);
1971 goto out;
1972}
1973
1974/**
1975 * ioctl_common() - common IOCTL handler for driver
1976 * @sdev: SCSI device associated with LUN.
1977 * @cmd: IOCTL command.
1978 *
1979 * Handles common fencing operations that are valid for multiple ioctls. Always
1980 * allow through ioctls that are cleanup oriented in nature, even when operating
1981 * in a failed/terminating state.
1982 *
1983 * Return: 0 on success, -errno on failure
1984 */
1985static int ioctl_common(struct scsi_device *sdev, int cmd)
1986{
1987 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
1988 struct device *dev = &cfg->dev->dev;
1989 struct llun_info *lli = sdev->hostdata;
1990 int rc = 0;
1991
1992 if (unlikely(!lli)) {
1993 dev_dbg(dev, "%s: Unknown LUN\n", __func__);
1994 rc = -EINVAL;
1995 goto out;
1996 }
1997
1998 rc = check_state(cfg);
1999 if (unlikely(rc) && (cfg->state == STATE_FAILTERM)) {
2000 switch (cmd) {
2cb79266 2001 case DK_CXLFLASH_VLUN_RESIZE:
65be2c79
MO
2002 case DK_CXLFLASH_RELEASE:
2003 case DK_CXLFLASH_DETACH:
2004 dev_dbg(dev, "%s: Command override! (%d)\n",
2005 __func__, rc);
2006 rc = 0;
2007 break;
2008 }
2009 }
2010out:
2011 return rc;
2012}
2013
2014/**
2015 * cxlflash_ioctl() - IOCTL handler for driver
2016 * @sdev: SCSI device associated with LUN.
2017 * @cmd: IOCTL command.
2018 * @arg: Userspace ioctl data structure.
2019 *
0a27ae51
MO
2020 * A read/write semaphore is used to implement a 'drain' of currently
2021 * running ioctls. The read semaphore is taken at the beginning of each
2022 * ioctl thread and released upon concluding execution. Additionally the
2023 * semaphore should be released and then reacquired in any ioctl execution
2024 * path which will wait for an event to occur that is outside the scope of
2025 * the ioctl (i.e. an adapter reset). To drain the ioctls currently running,
2026 * a thread simply needs to acquire the write semaphore.
2027 *
65be2c79
MO
2028 * Return: 0 on success, -errno on failure
2029 */
2030int cxlflash_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
2031{
2032 typedef int (*sioctl) (struct scsi_device *, void *);
2033
2034 struct cxlflash_cfg *cfg = (struct cxlflash_cfg *)sdev->host->hostdata;
2035 struct device *dev = &cfg->dev->dev;
2036 struct afu *afu = cfg->afu;
2037 struct dk_cxlflash_hdr *hdr;
2038 char buf[sizeof(union cxlflash_ioctls)];
2039 size_t size = 0;
2040 bool known_ioctl = false;
2041 int idx;
2042 int rc = 0;
2043 struct Scsi_Host *shost = sdev->host;
2044 sioctl do_ioctl = NULL;
2045
2046 static const struct {
2047 size_t size;
2048 sioctl ioctl;
2049 } ioctl_tbl[] = { /* NOTE: order matters here */
2050 {sizeof(struct dk_cxlflash_attach), (sioctl)cxlflash_disk_attach},
2051 {sizeof(struct dk_cxlflash_udirect), cxlflash_disk_direct_open},
2052 {sizeof(struct dk_cxlflash_release), (sioctl)cxlflash_disk_release},
2053 {sizeof(struct dk_cxlflash_detach), (sioctl)cxlflash_disk_detach},
2054 {sizeof(struct dk_cxlflash_verify), (sioctl)cxlflash_disk_verify},
2055 {sizeof(struct dk_cxlflash_recover_afu), (sioctl)cxlflash_afu_recover},
2056 {sizeof(struct dk_cxlflash_manage_lun), (sioctl)cxlflash_manage_lun},
2cb79266
MO
2057 {sizeof(struct dk_cxlflash_uvirtual), cxlflash_disk_virtual_open},
2058 {sizeof(struct dk_cxlflash_resize), (sioctl)cxlflash_vlun_resize},
2059 {sizeof(struct dk_cxlflash_clone), (sioctl)cxlflash_disk_clone},
65be2c79
MO
2060 };
2061
0a27ae51
MO
2062 /* Hold read semaphore so we can drain if needed */
2063 down_read(&cfg->ioctl_rwsem);
2064
65be2c79
MO
2065 /* Restrict command set to physical support only for internal LUN */
2066 if (afu->internal_lun)
2067 switch (cmd) {
2068 case DK_CXLFLASH_RELEASE:
2cb79266
MO
2069 case DK_CXLFLASH_USER_VIRTUAL:
2070 case DK_CXLFLASH_VLUN_RESIZE:
2071 case DK_CXLFLASH_VLUN_CLONE:
65be2c79
MO
2072 dev_dbg(dev, "%s: %s not supported for lun_mode=%d\n",
2073 __func__, decode_ioctl(cmd), afu->internal_lun);
2074 rc = -EINVAL;
2075 goto cxlflash_ioctl_exit;
2076 }
2077
2078 switch (cmd) {
2079 case DK_CXLFLASH_ATTACH:
2080 case DK_CXLFLASH_USER_DIRECT:
2081 case DK_CXLFLASH_RELEASE:
2082 case DK_CXLFLASH_DETACH:
2083 case DK_CXLFLASH_VERIFY:
2084 case DK_CXLFLASH_RECOVER_AFU:
2cb79266
MO
2085 case DK_CXLFLASH_USER_VIRTUAL:
2086 case DK_CXLFLASH_VLUN_RESIZE:
2087 case DK_CXLFLASH_VLUN_CLONE:
65be2c79
MO
2088 dev_dbg(dev, "%s: %s (%08X) on dev(%d/%d/%d/%llu)\n",
2089 __func__, decode_ioctl(cmd), cmd, shost->host_no,
2090 sdev->channel, sdev->id, sdev->lun);
2091 rc = ioctl_common(sdev, cmd);
2092 if (unlikely(rc))
2093 goto cxlflash_ioctl_exit;
2094
2095 /* fall through */
2096
2097 case DK_CXLFLASH_MANAGE_LUN:
2098 known_ioctl = true;
2099 idx = _IOC_NR(cmd) - _IOC_NR(DK_CXLFLASH_ATTACH);
2100 size = ioctl_tbl[idx].size;
2101 do_ioctl = ioctl_tbl[idx].ioctl;
2102
2103 if (likely(do_ioctl))
2104 break;
2105
2106 /* fall through */
2107 default:
2108 rc = -EINVAL;
2109 goto cxlflash_ioctl_exit;
2110 }
2111
2112 if (unlikely(copy_from_user(&buf, arg, size))) {
2113 dev_err(dev, "%s: copy_from_user() fail! "
2114 "size=%lu cmd=%d (%s) arg=%p\n",
2115 __func__, size, cmd, decode_ioctl(cmd), arg);
2116 rc = -EFAULT;
2117 goto cxlflash_ioctl_exit;
2118 }
2119
2120 hdr = (struct dk_cxlflash_hdr *)&buf;
2121 if (hdr->version != DK_CXLFLASH_VERSION_0) {
2122 dev_dbg(dev, "%s: Version %u not supported for %s\n",
2123 __func__, hdr->version, decode_ioctl(cmd));
2124 rc = -EINVAL;
2125 goto cxlflash_ioctl_exit;
2126 }
2127
2128 if (hdr->rsvd[0] || hdr->rsvd[1] || hdr->rsvd[2] || hdr->return_flags) {
2129 dev_dbg(dev, "%s: Reserved/rflags populated!\n", __func__);
2130 rc = -EINVAL;
2131 goto cxlflash_ioctl_exit;
2132 }
2133
2134 rc = do_ioctl(sdev, (void *)&buf);
2135 if (likely(!rc))
2136 if (unlikely(copy_to_user(arg, &buf, size))) {
2137 dev_err(dev, "%s: copy_to_user() fail! "
2138 "size=%lu cmd=%d (%s) arg=%p\n",
2139 __func__, size, cmd, decode_ioctl(cmd), arg);
2140 rc = -EFAULT;
2141 }
2142
2143 /* fall through to exit */
2144
2145cxlflash_ioctl_exit:
0a27ae51 2146 up_read(&cfg->ioctl_rwsem);
65be2c79
MO
2147 if (unlikely(rc && known_ioctl))
2148 dev_err(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
2149 "returned rc %d\n", __func__,
2150 decode_ioctl(cmd), cmd, shost->host_no,
2151 sdev->channel, sdev->id, sdev->lun, rc);
2152 else
2153 dev_dbg(dev, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
2154 "returned rc %d\n", __func__, decode_ioctl(cmd),
2155 cmd, shost->host_no, sdev->channel, sdev->id,
2156 sdev->lun, rc);
2157 return rc;
2158}
This page took 0.162123 seconds and 5 git commands to generate.