2 * CXL Flash Device Driver
4 * Written by: Manoj N. Kumar <manoj@linux.vnet.ibm.com>, IBM Corporation
5 * Matthew R. Ochs <mrochs@linux.vnet.ibm.com>, IBM Corporation
7 * Copyright (C) 2015 IBM Corporation
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.
15 #include <linux/delay.h>
16 #include <linux/file.h>
17 #include <linux/syscalls.h>
19 #include <asm/unaligned.h>
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>
30 #include "superpipe.h"
32 struct cxlflash_global global
;
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.
39 static void marshal_rele_to_resize(struct dk_cxlflash_release
*release
,
40 struct dk_cxlflash_resize
*resize
)
42 resize
->hdr
= release
->hdr
;
43 resize
->context_id
= release
->context_id
;
44 resize
->rsrc_handle
= release
->rsrc_handle
;
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.
52 static void marshal_det_to_rele(struct dk_cxlflash_detach
*detach
,
53 struct dk_cxlflash_release
*release
)
55 release
->hdr
= detach
->hdr
;
56 release
->context_id
= detach
->context_id
;
60 * cxlflash_free_errpage() - frees resources associated with global error page
62 void cxlflash_free_errpage(void)
65 mutex_lock(&global
.mutex
);
66 if (global
.err_page
) {
67 __free_page(global
.err_page
);
68 global
.err_page
= NULL
;
70 mutex_unlock(&global
.mutex
);
74 * cxlflash_stop_term_user_contexts() - stops/terminates known user contexts
75 * @cfg: Internal structure associated with the host.
77 * When the host needs to go down, all users must be quiesced and their
78 * memory freed. This is accomplished by putting the contexts in error
79 * state which will notify the user and let them 'drive' the tear down.
80 * Meanwhile, this routine camps until all user contexts have been removed.
82 void cxlflash_stop_term_user_contexts(struct cxlflash_cfg
*cfg
)
84 struct device
*dev
= &cfg
->dev
->dev
;
87 cxlflash_mark_contexts_error(cfg
);
92 for (i
= 0; i
< MAX_CONTEXT
; i
++)
93 if (cfg
->ctx_tbl
[i
]) {
98 if (!found
&& list_empty(&cfg
->ctx_err_recovery
))
101 dev_dbg(dev
, "%s: Wait for user contexts to quiesce...\n",
103 wake_up_all(&cfg
->reset_waitq
);
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.
114 * Return: Found context on success, NULL on failure
116 static struct ctx_info
*find_error_context(struct cxlflash_cfg
*cfg
, u64 rctxid
,
119 struct ctx_info
*ctxi
;
121 list_for_each_entry(ctxi
, &cfg
->ctx_err_recovery
, list
)
122 if ((ctxi
->ctxid
== rctxid
) || (ctxi
->file
== file
))
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.
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.
141 * Return: Validated context on success, NULL on failure
143 struct ctx_info
*get_context(struct cxlflash_cfg
*cfg
, u64 rctxid
,
144 void *arg
, enum ctx_ctrl ctx_ctrl
)
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
);
153 pid_t pid
= current
->tgid
, ctxpid
= 0;
155 if (ctx_ctrl
& CTX_CTRL_FILE
) {
157 file
= (struct file
*)arg
;
160 if (ctx_ctrl
& CTX_CTRL_CLONE
)
161 pid
= current
->parent
->tgid
;
163 if (likely(ctxid
< MAX_CONTEXT
)) {
165 mutex_lock(&cfg
->ctx_tbl_list_mutex
);
166 ctxi
= cfg
->ctx_tbl
[ctxid
];
168 if ((file
&& (ctxi
->file
!= file
)) ||
169 (!file
&& (ctxi
->ctxid
!= rctxid
)))
172 if ((ctx_ctrl
& CTX_CTRL_ERR
) ||
173 (!ctxi
&& (ctx_ctrl
& CTX_CTRL_ERR_FALLBACK
)))
174 ctxi
= find_error_context(cfg
, rctxid
, file
);
176 mutex_unlock(&cfg
->ctx_tbl_list_mutex
);
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.
186 * Note that the lock order is:
188 * cfg->ctx_tbl_list_mutex -> ctxi->mutex
190 * Therefore release ctx_tbl_list_mutex before retrying.
192 rc
= mutex_trylock(&ctxi
->mutex
);
193 mutex_unlock(&cfg
->ctx_tbl_list_mutex
);
195 break; /* got the context's lock! */
202 if (likely(!(ctx_ctrl
& CTX_CTRL_NOPID
)))
207 list_for_each_entry(lun_access
, &ctxi
->luns
, list
)
208 if (lun_access
->lli
== lli
)
215 dev_dbg(dev
, "%s: rctxid=%016llx ctxinfo=%p ctxpid=%u pid=%u "
216 "ctx_ctrl=%u\n", __func__
, rctxid
, ctxi
, ctxpid
, pid
,
222 mutex_unlock(&ctxi
->mutex
);
228 * put_context() - release a context that was retrieved from get_context()
229 * @ctxi: Context to release.
231 * For now, releasing the context equates to unlocking it's mutex.
233 void put_context(struct ctx_info
*ctxi
)
235 mutex_unlock(&ctxi
->mutex
);
239 * afu_attach() - attach a context to the AFU
240 * @cfg: Internal structure associated with the host.
241 * @ctxi: Context to attach.
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.
247 * Return: 0 on success, -errno on failure
249 static int afu_attach(struct cxlflash_cfg
*cfg
, struct ctx_info
*ctxi
)
251 struct device
*dev
= &cfg
->dev
->dev
;
252 struct afu
*afu
= cfg
->afu
;
253 struct sisl_ctrl_map __iomem
*ctrl_map
= ctxi
->ctrl_map
;
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",
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
);
274 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
279 * read_cap16() - issues a SCSI READ_CAP16 command
280 * @sdev: SCSI device associated with LUN.
281 * @lli: LUN destined for capacity request.
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
298 * Note that the above puts a requirement on this routine to only be called on
301 * Return: 0 on success, -errno on failure
303 static int read_cap16(struct scsi_device
*sdev
, struct llun_info
*lli
)
305 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
306 struct device
*dev
= &cfg
->dev
->dev
;
307 struct glun_info
*gli
= lli
->parent
;
308 struct scsi_sense_hdr sshdr
;
311 u8
*sense_buf
= NULL
;
315 u32 to
= CMD_TIMEOUT
* HZ
;
318 cmd_buf
= kzalloc(CMD_BUFSIZE
, GFP_KERNEL
);
319 scsi_cmd
= kzalloc(MAX_COMMAND_SIZE
, GFP_KERNEL
);
320 sense_buf
= kzalloc(SCSI_SENSE_BUFFERSIZE
, GFP_KERNEL
);
321 if (unlikely(!cmd_buf
|| !scsi_cmd
|| !sense_buf
)) {
326 scsi_cmd
[0] = SERVICE_ACTION_IN_16
; /* read cap(16) */
327 scsi_cmd
[1] = SAI_READ_CAPACITY_16
; /* service action */
328 put_unaligned_be32(CMD_BUFSIZE
, &scsi_cmd
[10]);
330 dev_dbg(dev
, "%s: %ssending cmd(%02x)\n", __func__
,
331 retry_cnt
? "re" : "", scsi_cmd
[0]);
333 /* Drop the ioctl read semahpore across lengthy call */
334 up_read(&cfg
->ioctl_rwsem
);
335 result
= scsi_execute(sdev
, scsi_cmd
, DMA_FROM_DEVICE
, cmd_buf
,
336 CMD_BUFSIZE
, sense_buf
, &sshdr
, to
, CMD_RETRIES
,
338 down_read(&cfg
->ioctl_rwsem
);
339 rc
= check_state(cfg
);
341 dev_err(dev
, "%s: Failed state result=%08x\n",
347 if (driver_byte(result
) == DRIVER_SENSE
) {
348 result
&= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
349 if (result
& SAM_STAT_CHECK_CONDITION
) {
350 switch (sshdr
.sense_key
) {
352 case RECOVERED_ERROR
:
355 result
&= ~SAM_STAT_CHECK_CONDITION
;
359 case 0x29: /* Power on Reset or Device Reset */
361 case 0x2A: /* Device capacity changed */
362 case 0x3F: /* Report LUNs changed */
363 /* Retry the command once more */
364 if (retry_cnt
++ < 1) {
379 dev_err(dev
, "%s: command failed, result=%08x\n",
386 * Read cap was successful, grab values from the buffer;
387 * note that we don't need to worry about unaligned access
388 * as the buffer is allocated on an aligned boundary.
390 mutex_lock(&gli
->mutex
);
391 gli
->max_lba
= be64_to_cpu(*((__be64
*)&cmd_buf
[0]));
392 gli
->blk_len
= be32_to_cpu(*((__be32
*)&cmd_buf
[8]));
393 mutex_unlock(&gli
->mutex
);
400 dev_dbg(dev
, "%s: maxlba=%lld blklen=%d rc=%d\n",
401 __func__
, gli
->max_lba
, gli
->blk_len
, rc
);
406 * get_rhte() - obtains validated resource handle table entry reference
407 * @ctxi: Context owning the resource handle.
408 * @rhndl: Resource handle associated with entry.
409 * @lli: LUN associated with request.
411 * Return: Validated RHTE on success, NULL on failure
413 struct sisl_rht_entry
*get_rhte(struct ctx_info
*ctxi
, res_hndl_t rhndl
,
414 struct llun_info
*lli
)
416 struct cxlflash_cfg
*cfg
= ctxi
->cfg
;
417 struct device
*dev
= &cfg
->dev
->dev
;
418 struct sisl_rht_entry
*rhte
= NULL
;
420 if (unlikely(!ctxi
->rht_start
)) {
421 dev_dbg(dev
, "%s: Context does not have allocated RHT\n",
426 if (unlikely(rhndl
>= MAX_RHT_PER_CONTEXT
)) {
427 dev_dbg(dev
, "%s: Bad resource handle rhndl=%d\n",
432 if (unlikely(ctxi
->rht_lun
[rhndl
] != lli
)) {
433 dev_dbg(dev
, "%s: Bad resource handle LUN rhndl=%d\n",
438 rhte
= &ctxi
->rht_start
[rhndl
];
439 if (unlikely(rhte
->nmask
== 0)) {
440 dev_dbg(dev
, "%s: Unopened resource handle rhndl=%d\n",
451 * rhte_checkout() - obtains free/empty resource handle table entry
452 * @ctxi: Context owning the resource handle.
453 * @lli: LUN associated with request.
455 * Return: Free RHTE on success, NULL on failure
457 struct sisl_rht_entry
*rhte_checkout(struct ctx_info
*ctxi
,
458 struct llun_info
*lli
)
460 struct cxlflash_cfg
*cfg
= ctxi
->cfg
;
461 struct device
*dev
= &cfg
->dev
->dev
;
462 struct sisl_rht_entry
*rhte
= NULL
;
465 /* Find a free RHT entry */
466 for (i
= 0; i
< MAX_RHT_PER_CONTEXT
; i
++)
467 if (ctxi
->rht_start
[i
].nmask
== 0) {
468 rhte
= &ctxi
->rht_start
[i
];
474 ctxi
->rht_lun
[i
] = lli
;
476 dev_dbg(dev
, "%s: returning rhte=%p index=%d\n", __func__
, rhte
, i
);
481 * rhte_checkin() - releases a resource handle table entry
482 * @ctxi: Context owning the resource handle.
483 * @rhte: RHTE to release.
485 void rhte_checkin(struct ctx_info
*ctxi
,
486 struct sisl_rht_entry
*rhte
)
488 u32 rsrc_handle
= rhte
- ctxi
->rht_start
;
493 ctxi
->rht_lun
[rsrc_handle
] = NULL
;
494 ctxi
->rht_needs_ws
[rsrc_handle
] = false;
498 * rhte_format1() - populates a RHTE for format 1
499 * @rhte: RHTE to populate.
500 * @lun_id: LUN ID of LUN associated with RHTE.
501 * @perm: Desired permissions for RHTE.
502 * @port_sel: Port selection mask
504 static void rht_format1(struct sisl_rht_entry
*rhte
, u64 lun_id
, u32 perm
,
508 * Populate the Format 1 RHT entry for direct access (physical
509 * LUN) using the synchronization sequence defined in the
510 * SISLite specification.
512 struct sisl_rht_entry_f1 dummy
= { 0 };
513 struct sisl_rht_entry_f1
*rhte_f1
= (struct sisl_rht_entry_f1
*)rhte
;
515 memset(rhte_f1
, 0, sizeof(*rhte_f1
));
516 rhte_f1
->fp
= SISL_RHT_FP(1U, 0);
517 dma_wmb(); /* Make setting of format bit visible */
519 rhte_f1
->lun_id
= lun_id
;
520 dma_wmb(); /* Make setting of LUN id visible */
523 * Use a dummy RHT Format 1 entry to build the second dword
524 * of the entry that must be populated in a single write when
525 * enabled (valid bit set to TRUE).
528 dummy
.fp
= SISL_RHT_FP(1U, perm
);
529 dummy
.port_sel
= port_sel
;
530 rhte_f1
->dw
= dummy
.dw
;
532 dma_wmb(); /* Make remaining RHT entry fields visible */
536 * cxlflash_lun_attach() - attaches a user to a LUN and manages the LUN's mode
537 * @gli: LUN to attach.
538 * @mode: Desired mode of the LUN.
539 * @locked: Mutex status on current thread.
541 * Return: 0 on success, -errno on failure
543 int cxlflash_lun_attach(struct glun_info
*gli
, enum lun_mode mode
, bool locked
)
548 mutex_lock(&gli
->mutex
);
550 if (gli
->mode
== MODE_NONE
)
552 else if (gli
->mode
!= mode
) {
553 pr_debug("%s: gli_mode=%d requested_mode=%d\n",
554 __func__
, gli
->mode
, mode
);
560 WARN_ON(gli
->users
<= 0);
562 pr_debug("%s: Returning rc=%d gli->mode=%u gli->users=%u\n",
563 __func__
, rc
, gli
->mode
, gli
->users
);
565 mutex_unlock(&gli
->mutex
);
570 * cxlflash_lun_detach() - detaches a user from a LUN and resets the LUN's mode
571 * @gli: LUN to detach.
573 * When resetting the mode, terminate block allocation resources as they
574 * are no longer required (service is safe to call even when block allocation
575 * resources were not present - such as when transitioning from physical mode).
576 * These resources will be reallocated when needed (subsequent transition to
579 void cxlflash_lun_detach(struct glun_info
*gli
)
581 mutex_lock(&gli
->mutex
);
582 WARN_ON(gli
->mode
== MODE_NONE
);
583 if (--gli
->users
== 0) {
584 gli
->mode
= MODE_NONE
;
585 cxlflash_ba_terminate(&gli
->blka
.ba_lun
);
587 pr_debug("%s: gli->users=%u\n", __func__
, gli
->users
);
588 WARN_ON(gli
->users
< 0);
589 mutex_unlock(&gli
->mutex
);
593 * _cxlflash_disk_release() - releases the specified resource entry
594 * @sdev: SCSI device associated with LUN.
595 * @ctxi: Context owning resources.
596 * @release: Release ioctl data structure.
598 * For LUNs in virtual mode, the virtual LUN associated with the specified
599 * resource handle is resized to 0 prior to releasing the RHTE. Note that the
600 * AFU sync should _not_ be performed when the context is sitting on the error
601 * recovery list. A context on the error recovery list is not known to the AFU
602 * due to reset. When the context is recovered, it will be reattached and made
603 * known again to the AFU.
605 * Return: 0 on success, -errno on failure
607 int _cxlflash_disk_release(struct scsi_device
*sdev
,
608 struct ctx_info
*ctxi
,
609 struct dk_cxlflash_release
*release
)
611 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
612 struct device
*dev
= &cfg
->dev
->dev
;
613 struct llun_info
*lli
= sdev
->hostdata
;
614 struct glun_info
*gli
= lli
->parent
;
615 struct afu
*afu
= cfg
->afu
;
616 bool put_ctx
= false;
618 struct dk_cxlflash_resize size
;
619 res_hndl_t rhndl
= release
->rsrc_handle
;
622 u64 ctxid
= DECODE_CTXID(release
->context_id
),
623 rctxid
= release
->context_id
;
625 struct sisl_rht_entry
*rhte
;
626 struct sisl_rht_entry_f1
*rhte_f1
;
628 dev_dbg(dev
, "%s: ctxid=%llu rhndl=%llu gli->mode=%u gli->users=%u\n",
629 __func__
, ctxid
, release
->rsrc_handle
, gli
->mode
, gli
->users
);
632 ctxi
= get_context(cfg
, rctxid
, lli
, CTX_CTRL_ERR_FALLBACK
);
633 if (unlikely(!ctxi
)) {
634 dev_dbg(dev
, "%s: Bad context ctxid=%llu\n",
643 rhte
= get_rhte(ctxi
, rhndl
, lli
);
644 if (unlikely(!rhte
)) {
645 dev_dbg(dev
, "%s: Bad resource handle rhndl=%d\n",
652 * Resize to 0 for virtual LUNS by setting the size
653 * to 0. This will clear LXT_START and LXT_CNT fields
654 * in the RHT entry and properly sync with the AFU.
656 * Afterwards we clear the remaining fields.
660 marshal_rele_to_resize(release
, &size
);
662 rc
= _cxlflash_vlun_resize(sdev
, ctxi
, &size
);
664 dev_dbg(dev
, "%s: resize failed rc %d\n", __func__
, rc
);
671 * Clear the Format 1 RHT entry for direct access
672 * (physical LUN) using the synchronization sequence
673 * defined in the SISLite specification.
675 rhte_f1
= (struct sisl_rht_entry_f1
*)rhte
;
678 dma_wmb(); /* Make revocation of RHT entry visible */
681 dma_wmb(); /* Make clearing of LUN id visible */
684 dma_wmb(); /* Make RHT entry bottom-half clearing visible */
686 if (!ctxi
->err_recovery_active
)
687 cxlflash_afu_sync(afu
, ctxid
, rhndl
, AFU_HW_SYNC
);
690 WARN(1, "Unsupported LUN mode!");
694 rhte_checkin(ctxi
, rhte
);
695 cxlflash_lun_detach(gli
);
700 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
704 int cxlflash_disk_release(struct scsi_device
*sdev
,
705 struct dk_cxlflash_release
*release
)
707 return _cxlflash_disk_release(sdev
, NULL
, release
);
711 * destroy_context() - releases a context
712 * @cfg: Internal structure associated with the host.
713 * @ctxi: Context to release.
715 * This routine is safe to be called with a a non-initialized context.
716 * Also note that the routine conditionally checks for the existence
717 * of the context control map before clearing the RHT registers and
718 * context capabilities because it is possible to destroy a context
719 * while the context is in the error state (previous mapping was
720 * removed [so there is no need to worry about clearing] and context
721 * is waiting for a new mapping).
723 static void destroy_context(struct cxlflash_cfg
*cfg
,
724 struct ctx_info
*ctxi
)
726 struct afu
*afu
= cfg
->afu
;
728 if (ctxi
->initialized
) {
729 WARN_ON(!list_empty(&ctxi
->luns
));
731 /* Clear RHT registers and drop all capabilities for context */
732 if (afu
->afu_map
&& ctxi
->ctrl_map
) {
733 writeq_be(0, &ctxi
->ctrl_map
->rht_start
);
734 writeq_be(0, &ctxi
->ctrl_map
->rht_cnt_id
);
735 writeq_be(0, &ctxi
->ctrl_map
->ctx_cap
);
739 /* Free memory associated with context */
740 free_page((ulong
)ctxi
->rht_start
);
741 kfree(ctxi
->rht_needs_ws
);
742 kfree(ctxi
->rht_lun
);
747 * create_context() - allocates and initializes a context
748 * @cfg: Internal structure associated with the host.
750 * Return: Allocated context on success, NULL on failure
752 static struct ctx_info
*create_context(struct cxlflash_cfg
*cfg
)
754 struct device
*dev
= &cfg
->dev
->dev
;
755 struct ctx_info
*ctxi
= NULL
;
756 struct llun_info
**lli
= NULL
;
758 struct sisl_rht_entry
*rhte
;
760 ctxi
= kzalloc(sizeof(*ctxi
), GFP_KERNEL
);
761 lli
= kzalloc((MAX_RHT_PER_CONTEXT
* sizeof(*lli
)), GFP_KERNEL
);
762 ws
= kzalloc((MAX_RHT_PER_CONTEXT
* sizeof(*ws
)), GFP_KERNEL
);
763 if (unlikely(!ctxi
|| !lli
|| !ws
)) {
764 dev_err(dev
, "%s: Unable to allocate context\n", __func__
);
768 rhte
= (struct sisl_rht_entry
*)get_zeroed_page(GFP_KERNEL
);
769 if (unlikely(!rhte
)) {
770 dev_err(dev
, "%s: Unable to allocate RHT\n", __func__
);
775 ctxi
->rht_needs_ws
= ws
;
776 ctxi
->rht_start
= rhte
;
789 * init_context() - initializes a previously allocated context
790 * @ctxi: Previously allocated context
791 * @cfg: Internal structure associated with the host.
792 * @ctx: Previously obtained CXL context reference.
793 * @ctxid: Previously obtained process element associated with CXL context.
794 * @file: Previously obtained file associated with CXL context.
795 * @perms: User-specified permissions.
797 static void init_context(struct ctx_info
*ctxi
, struct cxlflash_cfg
*cfg
,
798 struct cxl_context
*ctx
, int ctxid
, struct file
*file
,
801 struct afu
*afu
= cfg
->afu
;
803 ctxi
->rht_perms
= perms
;
804 ctxi
->ctrl_map
= &afu
->afu_map
->ctrls
[ctxid
].ctrl
;
805 ctxi
->ctxid
= ENCODE_CTXID(ctxi
, ctxid
);
806 ctxi
->pid
= current
->tgid
; /* tgid = pid */
810 ctxi
->initialized
= true;
811 mutex_init(&ctxi
->mutex
);
812 kref_init(&ctxi
->kref
);
813 INIT_LIST_HEAD(&ctxi
->luns
);
814 INIT_LIST_HEAD(&ctxi
->list
); /* initialize for list_empty() */
818 * remove_context() - context kref release handler
819 * @kref: Kernel reference associated with context to be removed.
821 * When a context no longer has any references it can safely be removed
822 * from global access and destroyed. Note that it is assumed the thread
823 * relinquishing access to the context holds its mutex.
825 static void remove_context(struct kref
*kref
)
827 struct ctx_info
*ctxi
= container_of(kref
, struct ctx_info
, kref
);
828 struct cxlflash_cfg
*cfg
= ctxi
->cfg
;
829 u64 ctxid
= DECODE_CTXID(ctxi
->ctxid
);
831 /* Remove context from table/error list */
832 WARN_ON(!mutex_is_locked(&ctxi
->mutex
));
833 ctxi
->unavail
= true;
834 mutex_unlock(&ctxi
->mutex
);
835 mutex_lock(&cfg
->ctx_tbl_list_mutex
);
836 mutex_lock(&ctxi
->mutex
);
838 if (!list_empty(&ctxi
->list
))
839 list_del(&ctxi
->list
);
840 cfg
->ctx_tbl
[ctxid
] = NULL
;
841 mutex_unlock(&cfg
->ctx_tbl_list_mutex
);
842 mutex_unlock(&ctxi
->mutex
);
844 /* Context now completely uncoupled/unreachable */
845 destroy_context(cfg
, ctxi
);
849 * _cxlflash_disk_detach() - detaches a LUN from a context
850 * @sdev: SCSI device associated with LUN.
851 * @ctxi: Context owning resources.
852 * @detach: Detach ioctl data structure.
854 * As part of the detach, all per-context resources associated with the LUN
855 * are cleaned up. When detaching the last LUN for a context, the context
856 * itself is cleaned up and released.
858 * Return: 0 on success, -errno on failure
860 static int _cxlflash_disk_detach(struct scsi_device
*sdev
,
861 struct ctx_info
*ctxi
,
862 struct dk_cxlflash_detach
*detach
)
864 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
865 struct device
*dev
= &cfg
->dev
->dev
;
866 struct llun_info
*lli
= sdev
->hostdata
;
867 struct lun_access
*lun_access
, *t
;
868 struct dk_cxlflash_release rel
;
869 bool put_ctx
= false;
873 u64 ctxid
= DECODE_CTXID(detach
->context_id
),
874 rctxid
= detach
->context_id
;
876 dev_dbg(dev
, "%s: ctxid=%llu\n", __func__
, ctxid
);
879 ctxi
= get_context(cfg
, rctxid
, lli
, CTX_CTRL_ERR_FALLBACK
);
880 if (unlikely(!ctxi
)) {
881 dev_dbg(dev
, "%s: Bad context ctxid=%llu\n",
890 /* Cleanup outstanding resources tied to this LUN */
892 marshal_det_to_rele(detach
, &rel
);
893 for (i
= 0; i
< MAX_RHT_PER_CONTEXT
; i
++) {
894 if (ctxi
->rht_lun
[i
] == lli
) {
896 _cxlflash_disk_release(sdev
, ctxi
, &rel
);
899 /* No need to loop further if we're done */
900 if (ctxi
->rht_out
== 0)
905 /* Take our LUN out of context, free the node */
906 list_for_each_entry_safe(lun_access
, t
, &ctxi
->luns
, list
)
907 if (lun_access
->lli
== lli
) {
908 list_del(&lun_access
->list
);
915 * Release the context reference and the sdev reference that
916 * bound this LUN to the context.
918 if (kref_put(&ctxi
->kref
, remove_context
))
920 scsi_device_put(sdev
);
924 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
928 static int cxlflash_disk_detach(struct scsi_device
*sdev
,
929 struct dk_cxlflash_detach
*detach
)
931 return _cxlflash_disk_detach(sdev
, NULL
, detach
);
935 * cxlflash_cxl_release() - release handler for adapter file descriptor
936 * @inode: File-system inode associated with fd.
937 * @file: File installed with adapter file descriptor.
939 * This routine is the release handler for the fops registered with
940 * the CXL services on an initial attach for a context. It is called
941 * when a close (explicity by the user or as part of a process tear
942 * down) is performed on the adapter file descriptor returned to the
943 * user. The user should be aware that explicitly performing a close
944 * considered catastrophic and subsequent usage of the superpipe API
945 * with previously saved off tokens will fail.
947 * This routine derives the context reference and calls detach for
948 * each LUN associated with the context.The final detach operation
949 * causes the context itself to be freed. With exception to when the
950 * CXL process element (context id) lookup fails (a case that should
951 * theoretically never occur), every call into this routine results
952 * in a complete freeing of a context.
954 * Return: 0 on success
956 static int cxlflash_cxl_release(struct inode
*inode
, struct file
*file
)
958 struct cxl_context
*ctx
= cxl_fops_get_context(file
);
959 struct cxlflash_cfg
*cfg
= container_of(file
->f_op
, struct cxlflash_cfg
,
961 struct device
*dev
= &cfg
->dev
->dev
;
962 struct ctx_info
*ctxi
= NULL
;
963 struct dk_cxlflash_detach detach
= { { 0 }, 0 };
964 struct lun_access
*lun_access
, *t
;
965 enum ctx_ctrl ctrl
= CTX_CTRL_ERR_FALLBACK
| CTX_CTRL_FILE
;
968 ctxid
= cxl_process_element(ctx
);
969 if (unlikely(ctxid
< 0)) {
970 dev_err(dev
, "%s: Context %p was closed ctxid=%d\n",
971 __func__
, ctx
, ctxid
);
975 ctxi
= get_context(cfg
, ctxid
, file
, ctrl
);
976 if (unlikely(!ctxi
)) {
977 ctxi
= get_context(cfg
, ctxid
, file
, ctrl
| CTX_CTRL_CLONE
);
979 dev_dbg(dev
, "%s: ctxid=%d already free\n",
984 dev_dbg(dev
, "%s: Another process owns ctxid=%d\n",
990 dev_dbg(dev
, "%s: close for ctxid=%d\n", __func__
, ctxid
);
992 detach
.context_id
= ctxi
->ctxid
;
993 list_for_each_entry_safe(lun_access
, t
, &ctxi
->luns
, list
)
994 _cxlflash_disk_detach(lun_access
->sdev
, ctxi
, &detach
);
996 cxl_fd_release(inode
, file
);
998 dev_dbg(dev
, "%s: returning\n", __func__
);
1003 * unmap_context() - clears a previously established mapping
1004 * @ctxi: Context owning the mapping.
1006 * This routine is used to switch between the error notification page
1007 * (dummy page of all 1's) and the real mapping (established by the CXL
1010 static void unmap_context(struct ctx_info
*ctxi
)
1012 unmap_mapping_range(ctxi
->file
->f_mapping
, 0, 0, 1);
1016 * get_err_page() - obtains and allocates the error notification page
1017 * @cfg: Internal structure associated with the host.
1019 * Return: error notification page on success, NULL on failure
1021 static struct page
*get_err_page(struct cxlflash_cfg
*cfg
)
1023 struct page
*err_page
= global
.err_page
;
1024 struct device
*dev
= &cfg
->dev
->dev
;
1026 if (unlikely(!err_page
)) {
1027 err_page
= alloc_page(GFP_KERNEL
);
1028 if (unlikely(!err_page
)) {
1029 dev_err(dev
, "%s: Unable to allocate err_page\n",
1034 memset(page_address(err_page
), -1, PAGE_SIZE
);
1036 /* Serialize update w/ other threads to avoid a leak */
1037 mutex_lock(&global
.mutex
);
1038 if (likely(!global
.err_page
))
1039 global
.err_page
= err_page
;
1041 __free_page(err_page
);
1042 err_page
= global
.err_page
;
1044 mutex_unlock(&global
.mutex
);
1048 dev_dbg(dev
, "%s: returning err_page=%p\n", __func__
, err_page
);
1053 * cxlflash_mmap_fault() - mmap fault handler for adapter file descriptor
1054 * @vmf: VM fault associated with current fault.
1056 * To support error notification via MMIO, faults are 'caught' by this routine
1057 * that was inserted before passing back the adapter file descriptor on attach.
1058 * When a fault occurs, this routine evaluates if error recovery is active and
1059 * if so, installs the error page to 'notify' the user about the error state.
1060 * During normal operation, the fault is simply handled by the original fault
1061 * handler that was installed by CXL services as part of initializing the
1062 * adapter file descriptor. The VMA's page protection bits are toggled to
1063 * indicate cached/not-cached depending on the memory backing the fault.
1065 * Return: 0 on success, VM_FAULT_SIGBUS on failure
1067 static int cxlflash_mmap_fault(struct vm_fault
*vmf
)
1069 struct vm_area_struct
*vma
= vmf
->vma
;
1070 struct file
*file
= vma
->vm_file
;
1071 struct cxl_context
*ctx
= cxl_fops_get_context(file
);
1072 struct cxlflash_cfg
*cfg
= container_of(file
->f_op
, struct cxlflash_cfg
,
1074 struct device
*dev
= &cfg
->dev
->dev
;
1075 struct ctx_info
*ctxi
= NULL
;
1076 struct page
*err_page
= NULL
;
1077 enum ctx_ctrl ctrl
= CTX_CTRL_ERR_FALLBACK
| CTX_CTRL_FILE
;
1081 ctxid
= cxl_process_element(ctx
);
1082 if (unlikely(ctxid
< 0)) {
1083 dev_err(dev
, "%s: Context %p was closed ctxid=%d\n",
1084 __func__
, ctx
, ctxid
);
1088 ctxi
= get_context(cfg
, ctxid
, file
, ctrl
);
1089 if (unlikely(!ctxi
)) {
1090 dev_dbg(dev
, "%s: Bad context ctxid=%d\n", __func__
, ctxid
);
1094 dev_dbg(dev
, "%s: fault for context %d\n", __func__
, ctxid
);
1096 if (likely(!ctxi
->err_recovery_active
)) {
1097 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1098 rc
= ctxi
->cxl_mmap_vmops
->fault(vmf
);
1100 dev_dbg(dev
, "%s: err recovery active, use err_page\n",
1103 err_page
= get_err_page(cfg
);
1104 if (unlikely(!err_page
)) {
1105 dev_err(dev
, "%s: Could not get err_page\n", __func__
);
1106 rc
= VM_FAULT_RETRY
;
1111 vmf
->page
= err_page
;
1112 vma
->vm_page_prot
= pgprot_cached(vma
->vm_page_prot
);
1118 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1122 rc
= VM_FAULT_SIGBUS
;
1127 * Local MMAP vmops to 'catch' faults
1129 static const struct vm_operations_struct cxlflash_mmap_vmops
= {
1130 .fault
= cxlflash_mmap_fault
,
1134 * cxlflash_cxl_mmap() - mmap handler for adapter file descriptor
1135 * @file: File installed with adapter file descriptor.
1136 * @vma: VM area associated with mapping.
1138 * Installs local mmap vmops to 'catch' faults for error notification support.
1140 * Return: 0 on success, -errno on failure
1142 static int cxlflash_cxl_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1144 struct cxl_context
*ctx
= cxl_fops_get_context(file
);
1145 struct cxlflash_cfg
*cfg
= container_of(file
->f_op
, struct cxlflash_cfg
,
1147 struct device
*dev
= &cfg
->dev
->dev
;
1148 struct ctx_info
*ctxi
= NULL
;
1149 enum ctx_ctrl ctrl
= CTX_CTRL_ERR_FALLBACK
| CTX_CTRL_FILE
;
1153 ctxid
= cxl_process_element(ctx
);
1154 if (unlikely(ctxid
< 0)) {
1155 dev_err(dev
, "%s: Context %p was closed ctxid=%d\n",
1156 __func__
, ctx
, ctxid
);
1161 ctxi
= get_context(cfg
, ctxid
, file
, ctrl
);
1162 if (unlikely(!ctxi
)) {
1163 dev_dbg(dev
, "%s: Bad context ctxid=%d\n", __func__
, ctxid
);
1168 dev_dbg(dev
, "%s: mmap for context %d\n", __func__
, ctxid
);
1170 rc
= cxl_fd_mmap(file
, vma
);
1172 /* Insert ourself in the mmap fault handler path */
1173 ctxi
->cxl_mmap_vmops
= vma
->vm_ops
;
1174 vma
->vm_ops
= &cxlflash_mmap_vmops
;
1183 const struct file_operations cxlflash_cxl_fops
= {
1184 .owner
= THIS_MODULE
,
1185 .mmap
= cxlflash_cxl_mmap
,
1186 .release
= cxlflash_cxl_release
,
1190 * cxlflash_mark_contexts_error() - move contexts to error state and list
1191 * @cfg: Internal structure associated with the host.
1193 * A context is only moved over to the error list when there are no outstanding
1194 * references to it. This ensures that a running operation has completed.
1196 * Return: 0 on success, -errno on failure
1198 int cxlflash_mark_contexts_error(struct cxlflash_cfg
*cfg
)
1201 struct ctx_info
*ctxi
= NULL
;
1203 mutex_lock(&cfg
->ctx_tbl_list_mutex
);
1205 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
1206 ctxi
= cfg
->ctx_tbl
[i
];
1208 mutex_lock(&ctxi
->mutex
);
1209 cfg
->ctx_tbl
[i
] = NULL
;
1210 list_add(&ctxi
->list
, &cfg
->ctx_err_recovery
);
1211 ctxi
->err_recovery_active
= true;
1212 ctxi
->ctrl_map
= NULL
;
1213 unmap_context(ctxi
);
1214 mutex_unlock(&ctxi
->mutex
);
1218 mutex_unlock(&cfg
->ctx_tbl_list_mutex
);
1225 static const struct file_operations null_fops
= {
1226 .owner
= THIS_MODULE
,
1230 * check_state() - checks and responds to the current adapter state
1231 * @cfg: Internal structure associated with the host.
1233 * This routine can block and should only be used on process context.
1234 * It assumes that the caller is an ioctl thread and holding the ioctl
1235 * read semaphore. This is temporarily let up across the wait to allow
1236 * for draining actively running ioctls. Also note that when waking up
1237 * from waiting in reset, the state is unknown and must be checked again
1238 * before proceeding.
1240 * Return: 0 on success, -errno on failure
1242 int check_state(struct cxlflash_cfg
*cfg
)
1244 struct device
*dev
= &cfg
->dev
->dev
;
1248 switch (cfg
->state
) {
1250 dev_dbg(dev
, "%s: Reset state, going to wait...\n", __func__
);
1251 up_read(&cfg
->ioctl_rwsem
);
1252 rc
= wait_event_interruptible(cfg
->reset_waitq
,
1253 cfg
->state
!= STATE_RESET
);
1254 down_read(&cfg
->ioctl_rwsem
);
1258 case STATE_FAILTERM
:
1259 dev_dbg(dev
, "%s: Failed/Terminating\n", __func__
);
1270 * cxlflash_disk_attach() - attach a LUN to a context
1271 * @sdev: SCSI device associated with LUN.
1272 * @attach: Attach ioctl data structure.
1274 * Creates a context and attaches LUN to it. A LUN can only be attached
1275 * one time to a context (subsequent attaches for the same context/LUN pair
1276 * are not supported). Additional LUNs can be attached to a context by
1277 * specifying the 'reuse' flag defined in the cxlflash_ioctl.h header.
1279 * Return: 0 on success, -errno on failure
1281 static int cxlflash_disk_attach(struct scsi_device
*sdev
,
1282 struct dk_cxlflash_attach
*attach
)
1284 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
1285 struct device
*dev
= &cfg
->dev
->dev
;
1286 struct afu
*afu
= cfg
->afu
;
1287 struct llun_info
*lli
= sdev
->hostdata
;
1288 struct glun_info
*gli
= lli
->parent
;
1289 struct cxl_ioctl_start_work
*work
;
1290 struct ctx_info
*ctxi
= NULL
;
1291 struct lun_access
*lun_access
= NULL
;
1297 struct file
*file
= NULL
;
1299 struct cxl_context
*ctx
= NULL
;
1303 if (attach
->num_interrupts
> 4) {
1304 dev_dbg(dev
, "%s: Cannot support this many interrupts %llu\n",
1305 __func__
, attach
->num_interrupts
);
1310 if (gli
->max_lba
== 0) {
1311 dev_dbg(dev
, "%s: No capacity info for LUN=%016llx\n",
1312 __func__
, lli
->lun_id
[sdev
->channel
]);
1313 rc
= read_cap16(sdev
, lli
);
1315 dev_err(dev
, "%s: Invalid device rc=%d\n",
1320 dev_dbg(dev
, "%s: LBA = %016llx\n", __func__
, gli
->max_lba
);
1321 dev_dbg(dev
, "%s: BLK_LEN = %08x\n", __func__
, gli
->blk_len
);
1324 if (attach
->hdr
.flags
& DK_CXLFLASH_ATTACH_REUSE_CONTEXT
) {
1325 rctxid
= attach
->context_id
;
1326 ctxi
= get_context(cfg
, rctxid
, NULL
, 0);
1328 dev_dbg(dev
, "%s: Bad context rctxid=%016llx\n",
1334 list_for_each_entry(lun_access
, &ctxi
->luns
, list
)
1335 if (lun_access
->lli
== lli
) {
1336 dev_dbg(dev
, "%s: Already attached\n",
1343 rc
= scsi_device_get(sdev
);
1345 dev_err(dev
, "%s: Unable to get sdev reference\n", __func__
);
1349 lun_access
= kzalloc(sizeof(*lun_access
), GFP_KERNEL
);
1350 if (unlikely(!lun_access
)) {
1351 dev_err(dev
, "%s: Unable to allocate lun_access\n", __func__
);
1356 lun_access
->lli
= lli
;
1357 lun_access
->sdev
= sdev
;
1359 /* Non-NULL context indicates reuse (another context reference) */
1361 dev_dbg(dev
, "%s: Reusing context for LUN rctxid=%016llx\n",
1363 kref_get(&ctxi
->kref
);
1364 list_add(&lun_access
->list
, &ctxi
->luns
);
1368 ctxi
= create_context(cfg
);
1369 if (unlikely(!ctxi
)) {
1370 dev_err(dev
, "%s: Failed to create context ctxid=%d\n",
1375 ctx
= cxl_dev_context_init(cfg
->dev
);
1376 if (IS_ERR_OR_NULL(ctx
)) {
1377 dev_err(dev
, "%s: Could not initialize context %p\n",
1384 work
->num_interrupts
= attach
->num_interrupts
;
1385 work
->flags
= CXL_START_WORK_NUM_IRQS
;
1387 rc
= cxl_start_work(ctx
, work
);
1389 dev_dbg(dev
, "%s: Could not start context rc=%d\n",
1394 ctxid
= cxl_process_element(ctx
);
1395 if (unlikely((ctxid
>= MAX_CONTEXT
) || (ctxid
< 0))) {
1396 dev_err(dev
, "%s: ctxid=%d invalid\n", __func__
, ctxid
);
1401 file
= cxl_get_fd(ctx
, &cfg
->cxl_fops
, &fd
);
1402 if (unlikely(fd
< 0)) {
1404 dev_err(dev
, "%s: Could not get file descriptor\n", __func__
);
1408 /* Translate read/write O_* flags from fcntl.h to AFU permission bits */
1409 perms
= SISL_RHT_PERM(attach
->hdr
.flags
+ 1);
1411 /* Context mutex is locked upon return */
1412 init_context(ctxi
, cfg
, ctx
, ctxid
, file
, perms
);
1414 rc
= afu_attach(cfg
, ctxi
);
1416 dev_err(dev
, "%s: Could not attach AFU rc %d\n", __func__
, rc
);
1421 * No error paths after this point. Once the fd is installed it's
1422 * visible to user space and can't be undone safely on this thread.
1423 * There is no need to worry about a deadlock here because no one
1424 * knows about us yet; we can be the only one holding our mutex.
1426 list_add(&lun_access
->list
, &ctxi
->luns
);
1427 mutex_lock(&cfg
->ctx_tbl_list_mutex
);
1428 mutex_lock(&ctxi
->mutex
);
1429 cfg
->ctx_tbl
[ctxid
] = ctxi
;
1430 mutex_unlock(&cfg
->ctx_tbl_list_mutex
);
1431 fd_install(fd
, file
);
1435 flags
|= DK_CXLFLASH_APP_CLOSE_ADAP_FD
;
1436 if (afu_is_sq_cmd_mode(afu
))
1437 flags
|= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE
;
1439 attach
->hdr
.return_flags
= flags
;
1440 attach
->context_id
= ctxi
->ctxid
;
1441 attach
->block_size
= gli
->blk_len
;
1442 attach
->mmio_size
= sizeof(afu
->afu_map
->hosts
[0].harea
);
1443 attach
->last_lba
= gli
->max_lba
;
1444 attach
->max_xfer
= sdev
->host
->max_sectors
* MAX_SECTOR_UNIT
;
1445 attach
->max_xfer
/= gli
->blk_len
;
1448 attach
->adap_fd
= fd
;
1453 dev_dbg(dev
, "%s: returning ctxid=%d fd=%d bs=%lld rc=%d llba=%lld\n",
1454 __func__
, ctxid
, fd
, attach
->block_size
, rc
, attach
->last_lba
);
1458 /* Cleanup CXL context; okay to 'stop' even if it was not started */
1459 if (!IS_ERR_OR_NULL(ctx
)) {
1460 cxl_stop_context(ctx
);
1461 cxl_release_context(ctx
);
1466 * Here, we're overriding the fops with a dummy all-NULL fops because
1467 * fput() calls the release fop, which will cause us to mistakenly
1468 * call into the CXL code. Rather than try to add yet more complexity
1469 * to that routine (cxlflash_cxl_release) we should try to fix the
1473 file
->f_op
= &null_fops
;
1480 /* Cleanup our context */
1482 destroy_context(cfg
, ctxi
);
1487 scsi_device_put(sdev
);
1492 * recover_context() - recovers a context in error
1493 * @cfg: Internal structure associated with the host.
1494 * @ctxi: Context to release.
1495 * @adap_fd: Adapter file descriptor associated with new/recovered context.
1497 * Restablishes the state for a context-in-error.
1499 * Return: 0 on success, -errno on failure
1501 static int recover_context(struct cxlflash_cfg
*cfg
,
1502 struct ctx_info
*ctxi
,
1505 struct device
*dev
= &cfg
->dev
->dev
;
1510 struct cxl_context
*ctx
;
1511 struct afu
*afu
= cfg
->afu
;
1513 ctx
= cxl_dev_context_init(cfg
->dev
);
1514 if (IS_ERR_OR_NULL(ctx
)) {
1515 dev_err(dev
, "%s: Could not initialize context %p\n",
1521 rc
= cxl_start_work(ctx
, &ctxi
->work
);
1523 dev_dbg(dev
, "%s: Could not start context rc=%d\n",
1528 ctxid
= cxl_process_element(ctx
);
1529 if (unlikely((ctxid
>= MAX_CONTEXT
) || (ctxid
< 0))) {
1530 dev_err(dev
, "%s: ctxid=%d invalid\n", __func__
, ctxid
);
1535 file
= cxl_get_fd(ctx
, &cfg
->cxl_fops
, &fd
);
1536 if (unlikely(fd
< 0)) {
1538 dev_err(dev
, "%s: Could not get file descriptor\n", __func__
);
1542 /* Update with new MMIO area based on updated context id */
1543 ctxi
->ctrl_map
= &afu
->afu_map
->ctrls
[ctxid
].ctrl
;
1545 rc
= afu_attach(cfg
, ctxi
);
1547 dev_err(dev
, "%s: Could not attach AFU rc %d\n", __func__
, rc
);
1552 * No error paths after this point. Once the fd is installed it's
1553 * visible to user space and can't be undone safely on this thread.
1555 ctxi
->ctxid
= ENCODE_CTXID(ctxi
, ctxid
);
1560 * Put context back in table (note the reinit of the context list);
1561 * we must first drop the context's mutex and then acquire it in
1562 * order with the table/list mutex to avoid a deadlock - safe to do
1563 * here because no one can find us at this moment in time.
1565 mutex_unlock(&ctxi
->mutex
);
1566 mutex_lock(&cfg
->ctx_tbl_list_mutex
);
1567 mutex_lock(&ctxi
->mutex
);
1568 list_del_init(&ctxi
->list
);
1569 cfg
->ctx_tbl
[ctxid
] = ctxi
;
1570 mutex_unlock(&cfg
->ctx_tbl_list_mutex
);
1571 fd_install(fd
, file
);
1574 dev_dbg(dev
, "%s: returning ctxid=%d fd=%d rc=%d\n",
1575 __func__
, ctxid
, fd
, rc
);
1582 cxl_stop_context(ctx
);
1584 cxl_release_context(ctx
);
1589 * cxlflash_afu_recover() - initiates AFU recovery
1590 * @sdev: SCSI device associated with LUN.
1591 * @recover: Recover ioctl data structure.
1593 * Only a single recovery is allowed at a time to avoid exhausting CXL
1594 * resources (leading to recovery failure) in the event that we're up
1595 * against the maximum number of contexts limit. For similar reasons,
1596 * a context recovery is retried if there are multiple recoveries taking
1597 * place at the same time and the failure was due to CXL services being
1598 * unable to keep up.
1600 * As this routine is called on ioctl context, it holds the ioctl r/w
1601 * semaphore that is used to drain ioctls in recovery scenarios. The
1602 * implementation to achieve the pacing described above (a local mutex)
1603 * requires that the ioctl r/w semaphore be dropped and reacquired to
1604 * avoid a 3-way deadlock when multiple process recoveries operate in
1607 * Because a user can detect an error condition before the kernel, it is
1608 * quite possible for this routine to act as the kernel's EEH detection
1609 * source (MMIO read of mbox_r). Because of this, there is a window of
1610 * time where an EEH might have been detected but not yet 'serviced'
1611 * (callback invoked, causing the device to enter reset state). To avoid
1612 * looping in this routine during that window, a 1 second sleep is in place
1613 * between the time the MMIO failure is detected and the time a wait on the
1614 * reset wait queue is attempted via check_state().
1616 * Return: 0 on success, -errno on failure
1618 static int cxlflash_afu_recover(struct scsi_device
*sdev
,
1619 struct dk_cxlflash_recover_afu
*recover
)
1621 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
1622 struct device
*dev
= &cfg
->dev
->dev
;
1623 struct llun_info
*lli
= sdev
->hostdata
;
1624 struct afu
*afu
= cfg
->afu
;
1625 struct ctx_info
*ctxi
= NULL
;
1626 struct mutex
*mutex
= &cfg
->ctx_recovery_mutex
;
1628 u64 ctxid
= DECODE_CTXID(recover
->context_id
),
1629 rctxid
= recover
->context_id
;
1631 int lretry
= 20; /* up to 2 seconds */
1632 int new_adap_fd
= -1;
1635 atomic_inc(&cfg
->recovery_threads
);
1636 up_read(&cfg
->ioctl_rwsem
);
1637 rc
= mutex_lock_interruptible(mutex
);
1638 down_read(&cfg
->ioctl_rwsem
);
1641 rc
= check_state(cfg
);
1643 dev_err(dev
, "%s: Failed state rc=%d\n", __func__
, rc
);
1648 dev_dbg(dev
, "%s: reason=%016llx rctxid=%016llx\n",
1649 __func__
, recover
->reason
, rctxid
);
1652 /* Ensure that this process is attached to the context */
1653 ctxi
= get_context(cfg
, rctxid
, lli
, CTX_CTRL_ERR_FALLBACK
);
1654 if (unlikely(!ctxi
)) {
1655 dev_dbg(dev
, "%s: Bad context ctxid=%llu\n", __func__
, ctxid
);
1660 if (ctxi
->err_recovery_active
) {
1662 rc
= recover_context(cfg
, ctxi
, &new_adap_fd
);
1664 dev_err(dev
, "%s: Recovery failed ctxid=%llu rc=%d\n",
1665 __func__
, ctxid
, rc
);
1666 if ((rc
== -ENODEV
) &&
1667 ((atomic_read(&cfg
->recovery_threads
) > 1) ||
1669 dev_dbg(dev
, "%s: Going to try again\n",
1671 mutex_unlock(mutex
);
1673 rc
= mutex_lock_interruptible(mutex
);
1682 ctxi
->err_recovery_active
= false;
1684 flags
= DK_CXLFLASH_APP_CLOSE_ADAP_FD
|
1685 DK_CXLFLASH_RECOVER_AFU_CONTEXT_RESET
;
1686 if (afu_is_sq_cmd_mode(afu
))
1687 flags
|= DK_CXLFLASH_CONTEXT_SQ_CMD_MODE
;
1689 recover
->hdr
.return_flags
= flags
;
1690 recover
->context_id
= ctxi
->ctxid
;
1691 recover
->adap_fd
= new_adap_fd
;
1692 recover
->mmio_size
= sizeof(afu
->afu_map
->hosts
[0].harea
);
1696 /* Test if in error state */
1697 reg
= readq_be(&afu
->ctrl_map
->mbox_r
);
1699 dev_dbg(dev
, "%s: MMIO fail, wait for recovery.\n", __func__
);
1702 * Before checking the state, put back the context obtained with
1703 * get_context() as it is no longer needed and sleep for a short
1704 * period of time (see prolog notes).
1709 rc
= check_state(cfg
);
1715 dev_dbg(dev
, "%s: MMIO working, no recovery required\n", __func__
);
1719 mutex_unlock(mutex
);
1720 atomic_dec_if_positive(&cfg
->recovery_threads
);
1725 * process_sense() - evaluates and processes sense data
1726 * @sdev: SCSI device associated with LUN.
1727 * @verify: Verify ioctl data structure.
1729 * Return: 0 on success, -errno on failure
1731 static int process_sense(struct scsi_device
*sdev
,
1732 struct dk_cxlflash_verify
*verify
)
1734 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
1735 struct device
*dev
= &cfg
->dev
->dev
;
1736 struct llun_info
*lli
= sdev
->hostdata
;
1737 struct glun_info
*gli
= lli
->parent
;
1738 u64 prev_lba
= gli
->max_lba
;
1739 struct scsi_sense_hdr sshdr
= { 0 };
1742 rc
= scsi_normalize_sense((const u8
*)&verify
->sense_data
,
1743 DK_CXLFLASH_VERIFY_SENSE_LEN
, &sshdr
);
1745 dev_err(dev
, "%s: Failed to normalize sense data\n", __func__
);
1750 switch (sshdr
.sense_key
) {
1752 case RECOVERED_ERROR
:
1756 case UNIT_ATTENTION
:
1757 switch (sshdr
.asc
) {
1758 case 0x29: /* Power on Reset or Device Reset */
1760 case 0x2A: /* Device settings/capacity changed */
1761 rc
= read_cap16(sdev
, lli
);
1766 if (prev_lba
!= gli
->max_lba
)
1767 dev_dbg(dev
, "%s: Capacity changed old=%lld "
1768 "new=%lld\n", __func__
, prev_lba
,
1771 case 0x3F: /* Report LUNs changed, Rescan. */
1772 scsi_scan_host(cfg
->host
);
1784 dev_dbg(dev
, "%s: sense_key %x asc %x ascq %x rc %d\n", __func__
,
1785 sshdr
.sense_key
, sshdr
.asc
, sshdr
.ascq
, rc
);
1790 * cxlflash_disk_verify() - verifies a LUN is the same and handle size changes
1791 * @sdev: SCSI device associated with LUN.
1792 * @verify: Verify ioctl data structure.
1794 * Return: 0 on success, -errno on failure
1796 static int cxlflash_disk_verify(struct scsi_device
*sdev
,
1797 struct dk_cxlflash_verify
*verify
)
1800 struct ctx_info
*ctxi
= NULL
;
1801 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
1802 struct device
*dev
= &cfg
->dev
->dev
;
1803 struct llun_info
*lli
= sdev
->hostdata
;
1804 struct glun_info
*gli
= lli
->parent
;
1805 struct sisl_rht_entry
*rhte
= NULL
;
1806 res_hndl_t rhndl
= verify
->rsrc_handle
;
1807 u64 ctxid
= DECODE_CTXID(verify
->context_id
),
1808 rctxid
= verify
->context_id
;
1811 dev_dbg(dev
, "%s: ctxid=%llu rhndl=%016llx, hint=%016llx, "
1812 "flags=%016llx\n", __func__
, ctxid
, verify
->rsrc_handle
,
1813 verify
->hint
, verify
->hdr
.flags
);
1815 ctxi
= get_context(cfg
, rctxid
, lli
, 0);
1816 if (unlikely(!ctxi
)) {
1817 dev_dbg(dev
, "%s: Bad context ctxid=%llu\n", __func__
, ctxid
);
1822 rhte
= get_rhte(ctxi
, rhndl
, lli
);
1823 if (unlikely(!rhte
)) {
1824 dev_dbg(dev
, "%s: Bad resource handle rhndl=%d\n",
1831 * Look at the hint/sense to see if it requires us to redrive
1832 * inquiry (i.e. the Unit attention is due to the WWN changing).
1834 if (verify
->hint
& DK_CXLFLASH_VERIFY_HINT_SENSE
) {
1835 /* Can't hold mutex across process_sense/read_cap16,
1836 * since we could have an intervening EEH event.
1838 ctxi
->unavail
= true;
1839 mutex_unlock(&ctxi
->mutex
);
1840 rc
= process_sense(sdev
, verify
);
1842 dev_err(dev
, "%s: Failed to validate sense data (%d)\n",
1844 mutex_lock(&ctxi
->mutex
);
1845 ctxi
->unavail
= false;
1848 mutex_lock(&ctxi
->mutex
);
1849 ctxi
->unavail
= false;
1852 switch (gli
->mode
) {
1854 last_lba
= gli
->max_lba
;
1857 /* Cast lxt_cnt to u64 for multiply to be treated as 64bit op */
1858 last_lba
= ((u64
)rhte
->lxt_cnt
* MC_CHUNK_SIZE
* gli
->blk_len
);
1859 last_lba
/= CXLFLASH_BLOCK_SIZE
;
1863 WARN(1, "Unsupported LUN mode!");
1866 verify
->last_lba
= last_lba
;
1871 dev_dbg(dev
, "%s: returning rc=%d llba=%llx\n",
1872 __func__
, rc
, verify
->last_lba
);
1877 * decode_ioctl() - translates an encoded ioctl to an easily identifiable string
1878 * @cmd: The ioctl command to decode.
1880 * Return: A string identifying the decoded ioctl.
1882 static char *decode_ioctl(int cmd
)
1885 case DK_CXLFLASH_ATTACH
:
1886 return __stringify_1(DK_CXLFLASH_ATTACH
);
1887 case DK_CXLFLASH_USER_DIRECT
:
1888 return __stringify_1(DK_CXLFLASH_USER_DIRECT
);
1889 case DK_CXLFLASH_USER_VIRTUAL
:
1890 return __stringify_1(DK_CXLFLASH_USER_VIRTUAL
);
1891 case DK_CXLFLASH_VLUN_RESIZE
:
1892 return __stringify_1(DK_CXLFLASH_VLUN_RESIZE
);
1893 case DK_CXLFLASH_RELEASE
:
1894 return __stringify_1(DK_CXLFLASH_RELEASE
);
1895 case DK_CXLFLASH_DETACH
:
1896 return __stringify_1(DK_CXLFLASH_DETACH
);
1897 case DK_CXLFLASH_VERIFY
:
1898 return __stringify_1(DK_CXLFLASH_VERIFY
);
1899 case DK_CXLFLASH_VLUN_CLONE
:
1900 return __stringify_1(DK_CXLFLASH_VLUN_CLONE
);
1901 case DK_CXLFLASH_RECOVER_AFU
:
1902 return __stringify_1(DK_CXLFLASH_RECOVER_AFU
);
1903 case DK_CXLFLASH_MANAGE_LUN
:
1904 return __stringify_1(DK_CXLFLASH_MANAGE_LUN
);
1911 * cxlflash_disk_direct_open() - opens a direct (physical) disk
1912 * @sdev: SCSI device associated with LUN.
1913 * @arg: UDirect ioctl data structure.
1915 * On successful return, the user is informed of the resource handle
1916 * to be used to identify the direct lun and the size (in blocks) of
1917 * the direct lun in last LBA format.
1919 * Return: 0 on success, -errno on failure
1921 static int cxlflash_disk_direct_open(struct scsi_device
*sdev
, void *arg
)
1923 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
1924 struct device
*dev
= &cfg
->dev
->dev
;
1925 struct afu
*afu
= cfg
->afu
;
1926 struct llun_info
*lli
= sdev
->hostdata
;
1927 struct glun_info
*gli
= lli
->parent
;
1929 struct dk_cxlflash_udirect
*pphys
= (struct dk_cxlflash_udirect
*)arg
;
1931 u64 ctxid
= DECODE_CTXID(pphys
->context_id
),
1932 rctxid
= pphys
->context_id
;
1935 u64 rsrc_handle
= -1;
1936 u32 port
= CHAN2PORT(sdev
->channel
);
1940 struct ctx_info
*ctxi
= NULL
;
1941 struct sisl_rht_entry
*rhte
= NULL
;
1943 dev_dbg(dev
, "%s: ctxid=%llu ls=%llu\n", __func__
, ctxid
, lun_size
);
1945 rc
= cxlflash_lun_attach(gli
, MODE_PHYSICAL
, false);
1947 dev_dbg(dev
, "%s: Failed attach to LUN (PHYSICAL)\n", __func__
);
1951 ctxi
= get_context(cfg
, rctxid
, lli
, 0);
1952 if (unlikely(!ctxi
)) {
1953 dev_dbg(dev
, "%s: Bad context ctxid=%llu\n", __func__
, ctxid
);
1958 rhte
= rhte_checkout(ctxi
, lli
);
1959 if (unlikely(!rhte
)) {
1960 dev_dbg(dev
, "%s: Too many opens ctxid=%lld\n",
1962 rc
= -EMFILE
; /* too many opens */
1966 rsrc_handle
= (rhte
- ctxi
->rht_start
);
1968 rht_format1(rhte
, lli
->lun_id
[sdev
->channel
], ctxi
->rht_perms
, port
);
1969 cxlflash_afu_sync(afu
, ctxid
, rsrc_handle
, AFU_LW_SYNC
);
1971 last_lba
= gli
->max_lba
;
1972 pphys
->hdr
.return_flags
= 0;
1973 pphys
->last_lba
= last_lba
;
1974 pphys
->rsrc_handle
= rsrc_handle
;
1979 dev_dbg(dev
, "%s: returning handle=%llu rc=%d llba=%llu\n",
1980 __func__
, rsrc_handle
, rc
, last_lba
);
1984 cxlflash_lun_detach(gli
);
1989 * ioctl_common() - common IOCTL handler for driver
1990 * @sdev: SCSI device associated with LUN.
1991 * @cmd: IOCTL command.
1993 * Handles common fencing operations that are valid for multiple ioctls. Always
1994 * allow through ioctls that are cleanup oriented in nature, even when operating
1995 * in a failed/terminating state.
1997 * Return: 0 on success, -errno on failure
1999 static int ioctl_common(struct scsi_device
*sdev
, int cmd
)
2001 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
2002 struct device
*dev
= &cfg
->dev
->dev
;
2003 struct llun_info
*lli
= sdev
->hostdata
;
2006 if (unlikely(!lli
)) {
2007 dev_dbg(dev
, "%s: Unknown LUN\n", __func__
);
2012 rc
= check_state(cfg
);
2013 if (unlikely(rc
) && (cfg
->state
== STATE_FAILTERM
)) {
2015 case DK_CXLFLASH_VLUN_RESIZE
:
2016 case DK_CXLFLASH_RELEASE
:
2017 case DK_CXLFLASH_DETACH
:
2018 dev_dbg(dev
, "%s: Command override rc=%d\n",
2029 * cxlflash_ioctl() - IOCTL handler for driver
2030 * @sdev: SCSI device associated with LUN.
2031 * @cmd: IOCTL command.
2032 * @arg: Userspace ioctl data structure.
2034 * A read/write semaphore is used to implement a 'drain' of currently
2035 * running ioctls. The read semaphore is taken at the beginning of each
2036 * ioctl thread and released upon concluding execution. Additionally the
2037 * semaphore should be released and then reacquired in any ioctl execution
2038 * path which will wait for an event to occur that is outside the scope of
2039 * the ioctl (i.e. an adapter reset). To drain the ioctls currently running,
2040 * a thread simply needs to acquire the write semaphore.
2042 * Return: 0 on success, -errno on failure
2044 int cxlflash_ioctl(struct scsi_device
*sdev
, int cmd
, void __user
*arg
)
2046 typedef int (*sioctl
) (struct scsi_device
*, void *);
2048 struct cxlflash_cfg
*cfg
= shost_priv(sdev
->host
);
2049 struct device
*dev
= &cfg
->dev
->dev
;
2050 struct afu
*afu
= cfg
->afu
;
2051 struct dk_cxlflash_hdr
*hdr
;
2052 char buf
[sizeof(union cxlflash_ioctls
)];
2054 bool known_ioctl
= false;
2057 struct Scsi_Host
*shost
= sdev
->host
;
2058 sioctl do_ioctl
= NULL
;
2060 static const struct {
2063 } ioctl_tbl
[] = { /* NOTE: order matters here */
2064 {sizeof(struct dk_cxlflash_attach
), (sioctl
)cxlflash_disk_attach
},
2065 {sizeof(struct dk_cxlflash_udirect
), cxlflash_disk_direct_open
},
2066 {sizeof(struct dk_cxlflash_release
), (sioctl
)cxlflash_disk_release
},
2067 {sizeof(struct dk_cxlflash_detach
), (sioctl
)cxlflash_disk_detach
},
2068 {sizeof(struct dk_cxlflash_verify
), (sioctl
)cxlflash_disk_verify
},
2069 {sizeof(struct dk_cxlflash_recover_afu
), (sioctl
)cxlflash_afu_recover
},
2070 {sizeof(struct dk_cxlflash_manage_lun
), (sioctl
)cxlflash_manage_lun
},
2071 {sizeof(struct dk_cxlflash_uvirtual
), cxlflash_disk_virtual_open
},
2072 {sizeof(struct dk_cxlflash_resize
), (sioctl
)cxlflash_vlun_resize
},
2073 {sizeof(struct dk_cxlflash_clone
), (sioctl
)cxlflash_disk_clone
},
2076 /* Hold read semaphore so we can drain if needed */
2077 down_read(&cfg
->ioctl_rwsem
);
2079 /* Restrict command set to physical support only for internal LUN */
2080 if (afu
->internal_lun
)
2082 case DK_CXLFLASH_RELEASE
:
2083 case DK_CXLFLASH_USER_VIRTUAL
:
2084 case DK_CXLFLASH_VLUN_RESIZE
:
2085 case DK_CXLFLASH_VLUN_CLONE
:
2086 dev_dbg(dev
, "%s: %s not supported for lun_mode=%d\n",
2087 __func__
, decode_ioctl(cmd
), afu
->internal_lun
);
2089 goto cxlflash_ioctl_exit
;
2093 case DK_CXLFLASH_ATTACH
:
2094 case DK_CXLFLASH_USER_DIRECT
:
2095 case DK_CXLFLASH_RELEASE
:
2096 case DK_CXLFLASH_DETACH
:
2097 case DK_CXLFLASH_VERIFY
:
2098 case DK_CXLFLASH_RECOVER_AFU
:
2099 case DK_CXLFLASH_USER_VIRTUAL
:
2100 case DK_CXLFLASH_VLUN_RESIZE
:
2101 case DK_CXLFLASH_VLUN_CLONE
:
2102 dev_dbg(dev
, "%s: %s (%08X) on dev(%d/%d/%d/%llu)\n",
2103 __func__
, decode_ioctl(cmd
), cmd
, shost
->host_no
,
2104 sdev
->channel
, sdev
->id
, sdev
->lun
);
2105 rc
= ioctl_common(sdev
, cmd
);
2107 goto cxlflash_ioctl_exit
;
2111 case DK_CXLFLASH_MANAGE_LUN
:
2113 idx
= _IOC_NR(cmd
) - _IOC_NR(DK_CXLFLASH_ATTACH
);
2114 size
= ioctl_tbl
[idx
].size
;
2115 do_ioctl
= ioctl_tbl
[idx
].ioctl
;
2117 if (likely(do_ioctl
))
2123 goto cxlflash_ioctl_exit
;
2126 if (unlikely(copy_from_user(&buf
, arg
, size
))) {
2127 dev_err(dev
, "%s: copy_from_user() fail "
2128 "size=%lu cmd=%d (%s) arg=%p\n",
2129 __func__
, size
, cmd
, decode_ioctl(cmd
), arg
);
2131 goto cxlflash_ioctl_exit
;
2134 hdr
= (struct dk_cxlflash_hdr
*)&buf
;
2135 if (hdr
->version
!= DK_CXLFLASH_VERSION_0
) {
2136 dev_dbg(dev
, "%s: Version %u not supported for %s\n",
2137 __func__
, hdr
->version
, decode_ioctl(cmd
));
2139 goto cxlflash_ioctl_exit
;
2142 if (hdr
->rsvd
[0] || hdr
->rsvd
[1] || hdr
->rsvd
[2] || hdr
->return_flags
) {
2143 dev_dbg(dev
, "%s: Reserved/rflags populated\n", __func__
);
2145 goto cxlflash_ioctl_exit
;
2148 rc
= do_ioctl(sdev
, (void *)&buf
);
2150 if (unlikely(copy_to_user(arg
, &buf
, size
))) {
2151 dev_err(dev
, "%s: copy_to_user() fail "
2152 "size=%lu cmd=%d (%s) arg=%p\n",
2153 __func__
, size
, cmd
, decode_ioctl(cmd
), arg
);
2157 /* fall through to exit */
2159 cxlflash_ioctl_exit
:
2160 up_read(&cfg
->ioctl_rwsem
);
2161 if (unlikely(rc
&& known_ioctl
))
2162 dev_err(dev
, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
2163 "returned rc %d\n", __func__
,
2164 decode_ioctl(cmd
), cmd
, shost
->host_no
,
2165 sdev
->channel
, sdev
->id
, sdev
->lun
, rc
);
2167 dev_dbg(dev
, "%s: ioctl %s (%08X) on dev(%d/%d/%d/%llu) "
2168 "returned rc %d\n", __func__
, decode_ioctl(cmd
),
2169 cmd
, shost
->host_no
, sdev
->channel
, sdev
->id
,