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
= (struct cxlflash_cfg
*)sdev
->host
->hostdata
;
306 struct device
*dev
= &cfg
->dev
->dev
;
307 struct glun_info
*gli
= lli
->parent
;
310 u8
*sense_buf
= NULL
;
314 u32 to
= CMD_TIMEOUT
* HZ
;
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
)) {
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]);
329 dev_dbg(dev
, "%s: %ssending cmd(0x%x)\n", __func__
,
330 retry_cnt
? "re" : "", scsi_cmd
[0]);
332 /* Drop the ioctl read semahpore across lengthy call */
333 up_read(&cfg
->ioctl_rwsem
);
334 result
= scsi_execute(sdev
, scsi_cmd
, DMA_FROM_DEVICE
, cmd_buf
,
335 CMD_BUFSIZE
, sense_buf
, to
, CMD_RETRIES
, 0, NULL
);
336 down_read(&cfg
->ioctl_rwsem
);
337 rc
= check_state(cfg
);
339 dev_err(dev
, "%s: Failed state! result=0x08%X\n",
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
;
350 scsi_normalize_sense(sense_buf
, SCSI_SENSE_BUFFERSIZE
,
352 switch (sshdr
.sense_key
) {
354 case RECOVERED_ERROR
:
357 result
&= ~SAM_STAT_CHECK_CONDITION
;
361 case 0x29: /* Power on Reset or Device Reset */
363 case 0x2A: /* Device capacity changed */
364 case 0x3F: /* Report LUNs changed */
365 /* Retry the command once more */
366 if (retry_cnt
++ < 1) {
381 dev_err(dev
, "%s: command failed, result=0x%x\n",
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.
392 mutex_lock(&gli
->mutex
);
393 gli
->max_lba
= be64_to_cpu(*((__be64
*)&cmd_buf
[0]));
394 gli
->blk_len
= be32_to_cpu(*((__be32
*)&cmd_buf
[8]));
395 mutex_unlock(&gli
->mutex
);
402 dev_dbg(dev
, "%s: maxlba=%lld blklen=%d rc=%d\n",
403 __func__
, gli
->max_lba
, gli
->blk_len
, rc
);
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.
413 * Return: Validated RHTE on success, NULL on failure
415 struct sisl_rht_entry
*get_rhte(struct ctx_info
*ctxi
, res_hndl_t rhndl
,
416 struct llun_info
*lli
)
418 struct sisl_rht_entry
*rhte
= NULL
;
420 if (unlikely(!ctxi
->rht_start
)) {
421 pr_debug("%s: Context does not have allocated RHT!\n",
426 if (unlikely(rhndl
>= MAX_RHT_PER_CONTEXT
)) {
427 pr_debug("%s: Bad resource handle! (%d)\n", __func__
, rhndl
);
431 if (unlikely(ctxi
->rht_lun
[rhndl
] != lli
)) {
432 pr_debug("%s: Bad resource handle LUN! (%d)\n",
437 rhte
= &ctxi
->rht_start
[rhndl
];
438 if (unlikely(rhte
->nmask
== 0)) {
439 pr_debug("%s: Unopened resource handle! (%d)\n",
450 * rhte_checkout() - obtains free/empty resource handle table entry
451 * @ctxi: Context owning the resource handle.
452 * @lli: LUN associated with request.
454 * Return: Free RHTE on success, NULL on failure
456 struct sisl_rht_entry
*rhte_checkout(struct ctx_info
*ctxi
,
457 struct llun_info
*lli
)
459 struct sisl_rht_entry
*rhte
= NULL
;
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
];
471 ctxi
->rht_lun
[i
] = lli
;
473 pr_debug("%s: returning rhte=%p (%d)\n", __func__
, rhte
, i
);
478 * rhte_checkin() - releases a resource handle table entry
479 * @ctxi: Context owning the resource handle.
480 * @rhte: RHTE to release.
482 void rhte_checkin(struct ctx_info
*ctxi
,
483 struct sisl_rht_entry
*rhte
)
485 u32 rsrc_handle
= rhte
- ctxi
->rht_start
;
490 ctxi
->rht_lun
[rsrc_handle
] = NULL
;
491 ctxi
->rht_needs_ws
[rsrc_handle
] = false;
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
501 static void rht_format1(struct sisl_rht_entry
*rhte
, u64 lun_id
, u32 perm
,
505 * Populate the Format 1 RHT entry for direct access (physical
506 * LUN) using the synchronization sequence defined in the
507 * SISLite specification.
509 struct sisl_rht_entry_f1 dummy
= { 0 };
510 struct sisl_rht_entry_f1
*rhte_f1
= (struct sisl_rht_entry_f1
*)rhte
;
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 */
516 rhte_f1
->lun_id
= lun_id
;
517 dma_wmb(); /* Make setting of LUN id visible */
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).
525 dummy
.fp
= SISL_RHT_FP(1U, perm
);
526 dummy
.port_sel
= port_sel
;
527 rhte_f1
->dw
= dummy
.dw
;
529 dma_wmb(); /* Make remaining RHT entry fields visible */
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.
538 * Return: 0 on success, -errno on failure
540 int cxlflash_lun_attach(struct glun_info
*gli
, enum lun_mode mode
, bool locked
)
545 mutex_lock(&gli
->mutex
);
547 if (gli
->mode
== MODE_NONE
)
549 else if (gli
->mode
!= mode
) {
550 pr_debug("%s: LUN operating in mode %d, requested mode %d\n",
551 __func__
, gli
->mode
, mode
);
557 WARN_ON(gli
->users
<= 0);
559 pr_debug("%s: Returning rc=%d gli->mode=%u gli->users=%u\n",
560 __func__
, rc
, gli
->mode
, gli
->users
);
562 mutex_unlock(&gli
->mutex
);
567 * cxlflash_lun_detach() - detaches a user from a LUN and resets the LUN's mode
568 * @gli: LUN to detach.
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
576 void cxlflash_lun_detach(struct glun_info
*gli
)
578 mutex_lock(&gli
->mutex
);
579 WARN_ON(gli
->mode
== MODE_NONE
);
580 if (--gli
->users
== 0) {
581 gli
->mode
= MODE_NONE
;
582 cxlflash_ba_terminate(&gli
->blka
.ba_lun
);
584 pr_debug("%s: gli->users=%u\n", __func__
, gli
->users
);
585 WARN_ON(gli
->users
< 0);
586 mutex_unlock(&gli
->mutex
);
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.
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.
602 * Return: 0 on success, -errno on failure
604 int _cxlflash_disk_release(struct scsi_device
*sdev
,
605 struct ctx_info
*ctxi
,
606 struct dk_cxlflash_release
*release
)
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;
615 struct dk_cxlflash_resize size
;
616 res_hndl_t rhndl
= release
->rsrc_handle
;
619 u64 ctxid
= DECODE_CTXID(release
->context_id
),
620 rctxid
= release
->context_id
;
622 struct sisl_rht_entry
*rhte
;
623 struct sisl_rht_entry_f1
*rhte_f1
;
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
);
629 ctxi
= get_context(cfg
, rctxid
, lli
, CTX_CTRL_ERR_FALLBACK
);
630 if (unlikely(!ctxi
)) {
631 dev_dbg(dev
, "%s: Bad context! (%llu)\n",
640 rhte
= get_rhte(ctxi
, rhndl
, lli
);
641 if (unlikely(!rhte
)) {
642 dev_dbg(dev
, "%s: Bad resource handle! (%d)\n",
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.
653 * Afterwards we clear the remaining fields.
657 marshal_rele_to_resize(release
, &size
);
659 rc
= _cxlflash_vlun_resize(sdev
, ctxi
, &size
);
661 dev_dbg(dev
, "%s: resize failed rc %d\n", __func__
, rc
);
668 * Clear the Format 1 RHT entry for direct access
669 * (physical LUN) using the synchronization sequence
670 * defined in the SISLite specification.
672 rhte_f1
= (struct sisl_rht_entry_f1
*)rhte
;
675 dma_wmb(); /* Make revocation of RHT entry visible */
678 dma_wmb(); /* Make clearing of LUN id visible */
681 dma_wmb(); /* Make RHT entry bottom-half clearing visible */
683 if (!ctxi
->err_recovery_active
)
684 cxlflash_afu_sync(afu
, ctxid
, rhndl
, AFU_HW_SYNC
);
687 WARN(1, "Unsupported LUN mode!");
691 rhte_checkin(ctxi
, rhte
);
692 cxlflash_lun_detach(gli
);
697 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
701 int cxlflash_disk_release(struct scsi_device
*sdev
,
702 struct dk_cxlflash_release
*release
)
704 return _cxlflash_disk_release(sdev
, NULL
, release
);
708 * destroy_context() - releases a context
709 * @cfg: Internal structure associated with the host.
710 * @ctxi: Context to release.
712 * Note that the rht_lun member of the context was cut from a single
713 * allocation when the context was created and therefore does not need
714 * to be explicitly freed. Also note that we conditionally check for the
715 * existence of the context control map before clearing the RHT registers
716 * and context capabilities because it is possible to destroy a context
717 * while the context is in the error state (previous mapping was removed
718 * [so we don't have to worry about clearing] and context is waiting for
721 static void destroy_context(struct cxlflash_cfg
*cfg
,
722 struct ctx_info
*ctxi
)
724 struct afu
*afu
= cfg
->afu
;
726 WARN_ON(!list_empty(&ctxi
->luns
));
728 /* Clear RHT registers and drop all capabilities for this 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
);
735 /* Free memory associated with context */
736 free_page((ulong
)ctxi
->rht_start
);
737 kfree(ctxi
->rht_needs_ws
);
738 kfree(ctxi
->rht_lun
);
743 * create_context() - allocates and initializes a context
744 * @cfg: Internal structure associated with the host.
745 * @ctx: Previously obtained CXL context reference.
746 * @ctxid: Previously obtained process element associated with CXL context.
747 * @adap_fd: Previously obtained adapter fd associated with CXL context.
748 * @file: Previously obtained file associated with CXL context.
749 * @perms: User-specified permissions.
751 * The context's mutex is locked when an allocated context is returned.
753 * Return: Allocated context on success, NULL on failure
755 static struct ctx_info
*create_context(struct cxlflash_cfg
*cfg
,
756 struct cxl_context
*ctx
, int ctxid
,
757 int adap_fd
, struct file
*file
,
760 struct device
*dev
= &cfg
->dev
->dev
;
761 struct afu
*afu
= cfg
->afu
;
762 struct ctx_info
*ctxi
= NULL
;
763 struct llun_info
**lli
= NULL
;
765 struct sisl_rht_entry
*rhte
;
767 ctxi
= kzalloc(sizeof(*ctxi
), GFP_KERNEL
);
768 lli
= kzalloc((MAX_RHT_PER_CONTEXT
* sizeof(*lli
)), GFP_KERNEL
);
769 ws
= kzalloc((MAX_RHT_PER_CONTEXT
* sizeof(*ws
)), GFP_KERNEL
);
770 if (unlikely(!ctxi
|| !lli
|| !ws
)) {
771 dev_err(dev
, "%s: Unable to allocate context!\n", __func__
);
775 rhte
= (struct sisl_rht_entry
*)get_zeroed_page(GFP_KERNEL
);
776 if (unlikely(!rhte
)) {
777 dev_err(dev
, "%s: Unable to allocate RHT!\n", __func__
);
782 ctxi
->rht_needs_ws
= ws
;
783 ctxi
->rht_start
= rhte
;
784 ctxi
->rht_perms
= perms
;
786 ctxi
->ctrl_map
= &afu
->afu_map
->ctrls
[ctxid
].ctrl
;
787 ctxi
->ctxid
= ENCODE_CTXID(ctxi
, ctxid
);
789 ctxi
->pid
= current
->tgid
; /* tgid = pid */
792 mutex_init(&ctxi
->mutex
);
793 INIT_LIST_HEAD(&ctxi
->luns
);
794 INIT_LIST_HEAD(&ctxi
->list
); /* initialize for list_empty() */
796 mutex_lock(&ctxi
->mutex
);
809 * _cxlflash_disk_detach() - detaches a LUN from a context
810 * @sdev: SCSI device associated with LUN.
811 * @ctxi: Context owning resources.
812 * @detach: Detach ioctl data structure.
814 * As part of the detach, all per-context resources associated with the LUN
815 * are cleaned up. When detaching the last LUN for a context, the context
816 * itself is cleaned up and released.
818 * Return: 0 on success, -errno on failure
820 static int _cxlflash_disk_detach(struct scsi_device
*sdev
,
821 struct ctx_info
*ctxi
,
822 struct dk_cxlflash_detach
*detach
)
824 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)sdev
->host
->hostdata
;
825 struct device
*dev
= &cfg
->dev
->dev
;
826 struct llun_info
*lli
= sdev
->hostdata
;
827 struct lun_access
*lun_access
, *t
;
828 struct dk_cxlflash_release rel
;
829 bool put_ctx
= false;
834 u64 ctxid
= DECODE_CTXID(detach
->context_id
),
835 rctxid
= detach
->context_id
;
837 dev_dbg(dev
, "%s: ctxid=%llu\n", __func__
, ctxid
);
840 ctxi
= get_context(cfg
, rctxid
, lli
, CTX_CTRL_ERR_FALLBACK
);
841 if (unlikely(!ctxi
)) {
842 dev_dbg(dev
, "%s: Bad context! (%llu)\n",
851 /* Cleanup outstanding resources tied to this LUN */
853 marshal_det_to_rele(detach
, &rel
);
854 for (i
= 0; i
< MAX_RHT_PER_CONTEXT
; i
++) {
855 if (ctxi
->rht_lun
[i
] == lli
) {
857 _cxlflash_disk_release(sdev
, ctxi
, &rel
);
860 /* No need to loop further if we're done */
861 if (ctxi
->rht_out
== 0)
866 /* Take our LUN out of context, free the node */
867 list_for_each_entry_safe(lun_access
, t
, &ctxi
->luns
, list
)
868 if (lun_access
->lli
== lli
) {
869 list_del(&lun_access
->list
);
875 /* Tear down context following last LUN cleanup */
876 if (list_empty(&ctxi
->luns
)) {
877 ctxi
->unavail
= true;
878 mutex_unlock(&ctxi
->mutex
);
879 mutex_lock(&cfg
->ctx_tbl_list_mutex
);
880 mutex_lock(&ctxi
->mutex
);
882 /* Might not have been in error list so conditionally remove */
883 if (!list_empty(&ctxi
->list
))
884 list_del(&ctxi
->list
);
885 cfg
->ctx_tbl
[ctxid
] = NULL
;
886 mutex_unlock(&cfg
->ctx_tbl_list_mutex
);
887 mutex_unlock(&ctxi
->mutex
);
890 destroy_context(cfg
, ctxi
);
895 * As a last step, clean up external resources when not
896 * already on an external cleanup thread, i.e.: close(adap_fd).
898 * NOTE: this will free up the context from the CXL services,
899 * allowing it to dole out the same context_id on a future
900 * (or even currently in-flight) disk_attach operation.
906 /* Release the sdev reference that bound this LUN to the context */
907 scsi_device_put(sdev
);
912 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
916 static int cxlflash_disk_detach(struct scsi_device
*sdev
,
917 struct dk_cxlflash_detach
*detach
)
919 return _cxlflash_disk_detach(sdev
, NULL
, detach
);
923 * cxlflash_cxl_release() - release handler for adapter file descriptor
924 * @inode: File-system inode associated with fd.
925 * @file: File installed with adapter file descriptor.
927 * This routine is the release handler for the fops registered with
928 * the CXL services on an initial attach for a context. It is called
929 * when a close is performed on the adapter file descriptor returned
930 * to the user. Programmatically, the user is not required to perform
931 * the close, as it is handled internally via the detach ioctl when
932 * a context is being removed. Note that nothing prevents the user
933 * from performing a close, but the user should be aware that doing
934 * so is considered catastrophic and subsequent usage of the superpipe
935 * API with previously saved off tokens will fail.
937 * When initiated from an external close (either by the user or via
938 * a process tear down), the routine derives the context reference
939 * and calls detach for each LUN associated with the context. The
940 * final detach operation will cause the context itself to be freed.
941 * Note that the saved off lfd is reset prior to calling detach to
942 * signify that the final detach should not perform a close.
944 * When initiated from a detach operation as part of the tear down
945 * of a context, the context is first completely freed and then the
946 * close is performed. This routine will fail to derive the context
947 * reference (due to the context having already been freed) and then
948 * call into the CXL release entry point.
950 * Thus, with exception to when the CXL process element (context id)
951 * lookup fails (a case that should theoretically never occur), every
952 * call into this routine results in a complete freeing of a context.
954 * As part of the detach, all per-context resources associated with the LUN
955 * are cleaned up. When detaching the last LUN for a context, the context
956 * itself is cleaned up and released.
958 * Return: 0 on success
960 static int cxlflash_cxl_release(struct inode
*inode
, struct file
*file
)
962 struct cxl_context
*ctx
= cxl_fops_get_context(file
);
963 struct cxlflash_cfg
*cfg
= container_of(file
->f_op
, struct cxlflash_cfg
,
965 struct device
*dev
= &cfg
->dev
->dev
;
966 struct ctx_info
*ctxi
= NULL
;
967 struct dk_cxlflash_detach detach
= { { 0 }, 0 };
968 struct lun_access
*lun_access
, *t
;
969 enum ctx_ctrl ctrl
= CTX_CTRL_ERR_FALLBACK
| CTX_CTRL_FILE
;
972 ctxid
= cxl_process_element(ctx
);
973 if (unlikely(ctxid
< 0)) {
974 dev_err(dev
, "%s: Context %p was closed! (%d)\n",
975 __func__
, ctx
, ctxid
);
979 ctxi
= get_context(cfg
, ctxid
, file
, ctrl
);
980 if (unlikely(!ctxi
)) {
981 ctxi
= get_context(cfg
, ctxid
, file
, ctrl
| CTX_CTRL_CLONE
);
983 dev_dbg(dev
, "%s: Context %d already free!\n",
988 dev_dbg(dev
, "%s: Another process owns context %d!\n",
994 dev_dbg(dev
, "%s: close(%d) for context %d\n",
995 __func__
, ctxi
->lfd
, ctxid
);
997 /* Reset the file descriptor to indicate we're on a close() thread */
999 detach
.context_id
= ctxi
->ctxid
;
1000 list_for_each_entry_safe(lun_access
, t
, &ctxi
->luns
, list
)
1001 _cxlflash_disk_detach(lun_access
->sdev
, ctxi
, &detach
);
1003 cxl_fd_release(inode
, file
);
1005 dev_dbg(dev
, "%s: returning\n", __func__
);
1010 * unmap_context() - clears a previously established mapping
1011 * @ctxi: Context owning the mapping.
1013 * This routine is used to switch between the error notification page
1014 * (dummy page of all 1's) and the real mapping (established by the CXL
1017 static void unmap_context(struct ctx_info
*ctxi
)
1019 unmap_mapping_range(ctxi
->file
->f_mapping
, 0, 0, 1);
1023 * get_err_page() - obtains and allocates the error notification page
1025 * Return: error notification page on success, NULL on failure
1027 static struct page
*get_err_page(void)
1029 struct page
*err_page
= global
.err_page
;
1031 if (unlikely(!err_page
)) {
1032 err_page
= alloc_page(GFP_KERNEL
);
1033 if (unlikely(!err_page
)) {
1034 pr_err("%s: Unable to allocate err_page!\n", __func__
);
1038 memset(page_address(err_page
), -1, PAGE_SIZE
);
1040 /* Serialize update w/ other threads to avoid a leak */
1041 mutex_lock(&global
.mutex
);
1042 if (likely(!global
.err_page
))
1043 global
.err_page
= err_page
;
1045 __free_page(err_page
);
1046 err_page
= global
.err_page
;
1048 mutex_unlock(&global
.mutex
);
1052 pr_debug("%s: returning err_page=%p\n", __func__
, err_page
);
1057 * cxlflash_mmap_fault() - mmap fault handler for adapter file descriptor
1058 * @vma: VM area associated with mapping.
1059 * @vmf: VM fault associated with current fault.
1061 * To support error notification via MMIO, faults are 'caught' by this routine
1062 * that was inserted before passing back the adapter file descriptor on attach.
1063 * When a fault occurs, this routine evaluates if error recovery is active and
1064 * if so, installs the error page to 'notify' the user about the error state.
1065 * During normal operation, the fault is simply handled by the original fault
1066 * handler that was installed by CXL services as part of initializing the
1067 * adapter file descriptor. The VMA's page protection bits are toggled to
1068 * indicate cached/not-cached depending on the memory backing the fault.
1070 * Return: 0 on success, VM_FAULT_SIGBUS on failure
1072 static int cxlflash_mmap_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1074 struct file
*file
= vma
->vm_file
;
1075 struct cxl_context
*ctx
= cxl_fops_get_context(file
);
1076 struct cxlflash_cfg
*cfg
= container_of(file
->f_op
, struct cxlflash_cfg
,
1078 struct device
*dev
= &cfg
->dev
->dev
;
1079 struct ctx_info
*ctxi
= NULL
;
1080 struct page
*err_page
= NULL
;
1081 enum ctx_ctrl ctrl
= CTX_CTRL_ERR_FALLBACK
| CTX_CTRL_FILE
;
1085 ctxid
= cxl_process_element(ctx
);
1086 if (unlikely(ctxid
< 0)) {
1087 dev_err(dev
, "%s: Context %p was closed! (%d)\n",
1088 __func__
, ctx
, ctxid
);
1092 ctxi
= get_context(cfg
, ctxid
, file
, ctrl
);
1093 if (unlikely(!ctxi
)) {
1094 dev_dbg(dev
, "%s: Bad context! (%d)\n", __func__
, ctxid
);
1098 dev_dbg(dev
, "%s: fault(%d) for context %d\n",
1099 __func__
, ctxi
->lfd
, ctxid
);
1101 if (likely(!ctxi
->err_recovery_active
)) {
1102 vma
->vm_page_prot
= pgprot_noncached(vma
->vm_page_prot
);
1103 rc
= ctxi
->cxl_mmap_vmops
->fault(vma
, vmf
);
1105 dev_dbg(dev
, "%s: err recovery active, use err_page!\n",
1108 err_page
= get_err_page();
1109 if (unlikely(!err_page
)) {
1110 dev_err(dev
, "%s: Could not obtain error page!\n",
1112 rc
= VM_FAULT_RETRY
;
1117 vmf
->page
= err_page
;
1118 vma
->vm_page_prot
= pgprot_cached(vma
->vm_page_prot
);
1124 dev_dbg(dev
, "%s: returning rc=%d\n", __func__
, rc
);
1128 rc
= VM_FAULT_SIGBUS
;
1133 * Local MMAP vmops to 'catch' faults
1135 static const struct vm_operations_struct cxlflash_mmap_vmops
= {
1136 .fault
= cxlflash_mmap_fault
,
1140 * cxlflash_cxl_mmap() - mmap handler for adapter file descriptor
1141 * @file: File installed with adapter file descriptor.
1142 * @vma: VM area associated with mapping.
1144 * Installs local mmap vmops to 'catch' faults for error notification support.
1146 * Return: 0 on success, -errno on failure
1148 static int cxlflash_cxl_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1150 struct cxl_context
*ctx
= cxl_fops_get_context(file
);
1151 struct cxlflash_cfg
*cfg
= container_of(file
->f_op
, struct cxlflash_cfg
,
1153 struct device
*dev
= &cfg
->dev
->dev
;
1154 struct ctx_info
*ctxi
= NULL
;
1155 enum ctx_ctrl ctrl
= CTX_CTRL_ERR_FALLBACK
| CTX_CTRL_FILE
;
1159 ctxid
= cxl_process_element(ctx
);
1160 if (unlikely(ctxid
< 0)) {
1161 dev_err(dev
, "%s: Context %p was closed! (%d)\n",
1162 __func__
, ctx
, ctxid
);
1167 ctxi
= get_context(cfg
, ctxid
, file
, ctrl
);
1168 if (unlikely(!ctxi
)) {
1169 dev_dbg(dev
, "%s: Bad context! (%d)\n", __func__
, ctxid
);
1174 dev_dbg(dev
, "%s: mmap(%d) for context %d\n",
1175 __func__
, ctxi
->lfd
, ctxid
);
1177 rc
= cxl_fd_mmap(file
, vma
);
1179 /* Insert ourself in the mmap fault handler path */
1180 ctxi
->cxl_mmap_vmops
= vma
->vm_ops
;
1181 vma
->vm_ops
= &cxlflash_mmap_vmops
;
1190 const struct file_operations cxlflash_cxl_fops
= {
1191 .owner
= THIS_MODULE
,
1192 .mmap
= cxlflash_cxl_mmap
,
1193 .release
= cxlflash_cxl_release
,
1197 * cxlflash_mark_contexts_error() - move contexts to error state and list
1198 * @cfg: Internal structure associated with the host.
1200 * A context is only moved over to the error list when there are no outstanding
1201 * references to it. This ensures that a running operation has completed.
1203 * Return: 0 on success, -errno on failure
1205 int cxlflash_mark_contexts_error(struct cxlflash_cfg
*cfg
)
1208 struct ctx_info
*ctxi
= NULL
;
1210 mutex_lock(&cfg
->ctx_tbl_list_mutex
);
1212 for (i
= 0; i
< MAX_CONTEXT
; i
++) {
1213 ctxi
= cfg
->ctx_tbl
[i
];
1215 mutex_lock(&ctxi
->mutex
);
1216 cfg
->ctx_tbl
[i
] = NULL
;
1217 list_add(&ctxi
->list
, &cfg
->ctx_err_recovery
);
1218 ctxi
->err_recovery_active
= true;
1219 ctxi
->ctrl_map
= NULL
;
1220 unmap_context(ctxi
);
1221 mutex_unlock(&ctxi
->mutex
);
1225 mutex_unlock(&cfg
->ctx_tbl_list_mutex
);
1232 static const struct file_operations null_fops
= {
1233 .owner
= THIS_MODULE
,
1237 * check_state() - checks and responds to the current adapter state
1238 * @cfg: Internal structure associated with the host.
1240 * This routine can block and should only be used on process context.
1241 * It assumes that the caller is an ioctl thread and holding the ioctl
1242 * read semaphore. This is temporarily let up across the wait to allow
1243 * for draining actively running ioctls. Also note that when waking up
1244 * from waiting in reset, the state is unknown and must be checked again
1245 * before proceeding.
1247 * Return: 0 on success, -errno on failure
1249 int check_state(struct cxlflash_cfg
*cfg
)
1251 struct device
*dev
= &cfg
->dev
->dev
;
1255 switch (cfg
->state
) {
1257 dev_dbg(dev
, "%s: Reset state, going to wait...\n", __func__
);
1258 up_read(&cfg
->ioctl_rwsem
);
1259 rc
= wait_event_interruptible(cfg
->reset_waitq
,
1260 cfg
->state
!= STATE_RESET
);
1261 down_read(&cfg
->ioctl_rwsem
);
1265 case STATE_FAILTERM
:
1266 dev_dbg(dev
, "%s: Failed/Terminating!\n", __func__
);
1277 * cxlflash_disk_attach() - attach a LUN to a context
1278 * @sdev: SCSI device associated with LUN.
1279 * @attach: Attach ioctl data structure.
1281 * Creates a context and attaches LUN to it. A LUN can only be attached
1282 * one time to a context (subsequent attaches for the same context/LUN pair
1283 * are not supported). Additional LUNs can be attached to a context by
1284 * specifying the 'reuse' flag defined in the cxlflash_ioctl.h header.
1286 * Return: 0 on success, -errno on failure
1288 static int cxlflash_disk_attach(struct scsi_device
*sdev
,
1289 struct dk_cxlflash_attach
*attach
)
1291 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)sdev
->host
->hostdata
;
1292 struct device
*dev
= &cfg
->dev
->dev
;
1293 struct afu
*afu
= cfg
->afu
;
1294 struct llun_info
*lli
= sdev
->hostdata
;
1295 struct glun_info
*gli
= lli
->parent
;
1296 struct cxl_ioctl_start_work
*work
;
1297 struct ctx_info
*ctxi
= NULL
;
1298 struct lun_access
*lun_access
= NULL
;
1305 struct cxl_context
*ctx
;
1309 if (attach
->num_interrupts
> 4) {
1310 dev_dbg(dev
, "%s: Cannot support this many interrupts %llu\n",
1311 __func__
, attach
->num_interrupts
);
1316 if (gli
->max_lba
== 0) {
1317 dev_dbg(dev
, "%s: No capacity info for this LUN (%016llX)\n",
1318 __func__
, lli
->lun_id
[sdev
->channel
]);
1319 rc
= read_cap16(sdev
, lli
);
1321 dev_err(dev
, "%s: Invalid device! (%d)\n",
1326 dev_dbg(dev
, "%s: LBA = %016llX\n", __func__
, gli
->max_lba
);
1327 dev_dbg(dev
, "%s: BLK_LEN = %08X\n", __func__
, gli
->blk_len
);
1330 if (attach
->hdr
.flags
& DK_CXLFLASH_ATTACH_REUSE_CONTEXT
) {
1331 rctxid
= attach
->context_id
;
1332 ctxi
= get_context(cfg
, rctxid
, NULL
, 0);
1334 dev_dbg(dev
, "%s: Bad context! (%016llX)\n",
1340 list_for_each_entry(lun_access
, &ctxi
->luns
, list
)
1341 if (lun_access
->lli
== lli
) {
1342 dev_dbg(dev
, "%s: Already attached!\n",
1349 rc
= scsi_device_get(sdev
);
1351 dev_err(dev
, "%s: Unable to get sdev reference!\n", __func__
);
1355 lun_access
= kzalloc(sizeof(*lun_access
), GFP_KERNEL
);
1356 if (unlikely(!lun_access
)) {
1357 dev_err(dev
, "%s: Unable to allocate lun_access!\n", __func__
);
1362 lun_access
->lli
= lli
;
1363 lun_access
->sdev
= sdev
;
1365 /* Non-NULL context indicates reuse */
1367 dev_dbg(dev
, "%s: Reusing context for LUN! (%016llX)\n",
1369 list_add(&lun_access
->list
, &ctxi
->luns
);
1374 ctx
= cxl_dev_context_init(cfg
->dev
);
1375 if (unlikely(IS_ERR_OR_NULL(ctx
))) {
1376 dev_err(dev
, "%s: Could not initialize context %p\n",
1382 ctxid
= cxl_process_element(ctx
);
1383 if (unlikely((ctxid
>= MAX_CONTEXT
) || (ctxid
< 0))) {
1384 dev_err(dev
, "%s: ctxid (%d) invalid!\n", __func__
, ctxid
);
1389 file
= cxl_get_fd(ctx
, &cfg
->cxl_fops
, &fd
);
1390 if (unlikely(fd
< 0)) {
1392 dev_err(dev
, "%s: Could not get file descriptor\n", __func__
);
1396 /* Translate read/write O_* flags from fcntl.h to AFU permission bits */
1397 perms
= SISL_RHT_PERM(attach
->hdr
.flags
+ 1);
1399 ctxi
= create_context(cfg
, ctx
, ctxid
, fd
, file
, perms
);
1400 if (unlikely(!ctxi
)) {
1401 dev_err(dev
, "%s: Failed to create context! (%d)\n",
1407 work
->num_interrupts
= attach
->num_interrupts
;
1408 work
->flags
= CXL_START_WORK_NUM_IRQS
;
1410 rc
= cxl_start_work(ctx
, work
);
1412 dev_dbg(dev
, "%s: Could not start context rc=%d\n",
1417 rc
= afu_attach(cfg
, ctxi
);
1419 dev_err(dev
, "%s: Could not attach AFU rc %d\n", __func__
, rc
);
1424 * No error paths after this point. Once the fd is installed it's
1425 * visible to user space and can't be undone safely on this thread.
1426 * There is no need to worry about a deadlock here because no one
1427 * knows about us yet; we can be the only one holding our mutex.
1429 list_add(&lun_access
->list
, &ctxi
->luns
);
1430 mutex_unlock(&ctxi
->mutex
);
1431 mutex_lock(&cfg
->ctx_tbl_list_mutex
);
1432 mutex_lock(&ctxi
->mutex
);
1433 cfg
->ctx_tbl
[ctxid
] = ctxi
;
1434 mutex_unlock(&cfg
->ctx_tbl_list_mutex
);
1435 fd_install(fd
, file
);
1438 attach
->hdr
.return_flags
= 0;
1439 attach
->context_id
= ctxi
->ctxid
;
1440 attach
->block_size
= gli
->blk_len
;
1441 attach
->mmio_size
= sizeof(afu
->afu_map
->hosts
[0].harea
);
1442 attach
->last_lba
= gli
->max_lba
;
1443 attach
->max_xfer
= sdev
->host
->max_sectors
* MAX_SECTOR_UNIT
;
1444 attach
->max_xfer
/= gli
->blk_len
;
1447 attach
->adap_fd
= fd
;
1452 dev_dbg(dev
, "%s: returning ctxid=%d fd=%d bs=%lld rc=%d llba=%lld\n",
1453 __func__
, ctxid
, fd
, attach
->block_size
, rc
, attach
->last_lba
);
1457 cxl_stop_context(ctx
);
1460 destroy_context(cfg
, ctxi
);
1464 * Here, we're overriding the fops with a dummy all-NULL fops because
1465 * fput() calls the release fop, which will cause us to mistakenly
1466 * call into the CXL code. Rather than try to add yet more complexity
1467 * to that routine (cxlflash_cxl_release) we should try to fix the
1470 file
->f_op
= &null_fops
;
1475 cxl_release_context(ctx
);
1479 scsi_device_put(sdev
);
1484 * recover_context() - recovers a context in error
1485 * @cfg: Internal structure associated with the host.
1486 * @ctxi: Context to release.
1488 * Restablishes the state for a context-in-error.
1490 * Return: 0 on success, -errno on failure
1492 static int recover_context(struct cxlflash_cfg
*cfg
, struct ctx_info
*ctxi
)
1494 struct device
*dev
= &cfg
->dev
->dev
;
1496 int old_fd
, fd
= -1;
1499 struct cxl_context
*ctx
;
1500 struct afu
*afu
= cfg
->afu
;
1502 ctx
= cxl_dev_context_init(cfg
->dev
);
1503 if (unlikely(IS_ERR_OR_NULL(ctx
))) {
1504 dev_err(dev
, "%s: Could not initialize context %p\n",
1510 ctxid
= cxl_process_element(ctx
);
1511 if (unlikely((ctxid
>= MAX_CONTEXT
) || (ctxid
< 0))) {
1512 dev_err(dev
, "%s: ctxid (%d) invalid!\n", __func__
, ctxid
);
1517 file
= cxl_get_fd(ctx
, &cfg
->cxl_fops
, &fd
);
1518 if (unlikely(fd
< 0)) {
1520 dev_err(dev
, "%s: Could not get file descriptor\n", __func__
);
1524 rc
= cxl_start_work(ctx
, &ctxi
->work
);
1526 dev_dbg(dev
, "%s: Could not start context rc=%d\n",
1531 /* Update with new MMIO area based on updated context id */
1532 ctxi
->ctrl_map
= &afu
->afu_map
->ctrls
[ctxid
].ctrl
;
1534 rc
= afu_attach(cfg
, ctxi
);
1536 dev_err(dev
, "%s: Could not attach AFU rc %d\n", __func__
, rc
);
1541 * No error paths after this point. Once the fd is installed it's
1542 * visible to user space and can't be undone safely on this thread.
1545 ctxi
->ctxid
= ENCODE_CTXID(ctxi
, ctxid
);
1551 * Put context back in table (note the reinit of the context list);
1552 * we must first drop the context's mutex and then acquire it in
1553 * order with the table/list mutex to avoid a deadlock - safe to do
1554 * here because no one can find us at this moment in time.
1556 mutex_unlock(&ctxi
->mutex
);
1557 mutex_lock(&cfg
->ctx_tbl_list_mutex
);
1558 mutex_lock(&ctxi
->mutex
);
1559 list_del_init(&ctxi
->list
);
1560 cfg
->ctx_tbl
[ctxid
] = ctxi
;
1561 mutex_unlock(&cfg
->ctx_tbl_list_mutex
);
1562 fd_install(fd
, file
);
1564 /* Release the original adapter fd and associated CXL resources */
1567 dev_dbg(dev
, "%s: returning ctxid=%d fd=%d rc=%d\n",
1568 __func__
, ctxid
, fd
, rc
);
1572 cxl_stop_context(ctx
);
1577 cxl_release_context(ctx
);
1582 * cxlflash_afu_recover() - initiates AFU recovery
1583 * @sdev: SCSI device associated with LUN.
1584 * @recover: Recover ioctl data structure.
1586 * Only a single recovery is allowed at a time to avoid exhausting CXL
1587 * resources (leading to recovery failure) in the event that we're up
1588 * against the maximum number of contexts limit. For similar reasons,
1589 * a context recovery is retried if there are multiple recoveries taking
1590 * place at the same time and the failure was due to CXL services being
1591 * unable to keep up.
1593 * As this routine is called on ioctl context, it holds the ioctl r/w
1594 * semaphore that is used to drain ioctls in recovery scenarios. The
1595 * implementation to achieve the pacing described above (a local mutex)
1596 * requires that the ioctl r/w semaphore be dropped and reacquired to
1597 * avoid a 3-way deadlock when multiple process recoveries operate in
1600 * Because a user can detect an error condition before the kernel, it is
1601 * quite possible for this routine to act as the kernel's EEH detection
1602 * source (MMIO read of mbox_r). Because of this, there is a window of
1603 * time where an EEH might have been detected but not yet 'serviced'
1604 * (callback invoked, causing the device to enter reset state). To avoid
1605 * looping in this routine during that window, a 1 second sleep is in place
1606 * between the time the MMIO failure is detected and the time a wait on the
1607 * reset wait queue is attempted via check_state().
1609 * Return: 0 on success, -errno on failure
1611 static int cxlflash_afu_recover(struct scsi_device
*sdev
,
1612 struct dk_cxlflash_recover_afu
*recover
)
1614 struct cxlflash_cfg
*cfg
= (struct cxlflash_cfg
*)sdev
->host
->hostdata
;
1615 struct device
*dev
= &cfg
->dev
->dev
;
1616 struct llun_info
*lli
= sdev
->hostdata
;
1617 struct afu
*afu
= cfg
->afu
;
1618 struct ctx_info
*ctxi
= NULL
;
1619 struct mutex
*mutex
= &cfg
->ctx_recovery_mutex
;
1620 u64 ctxid
= DECODE_CTXID(recover
->context_id
),
1621 rctxid
= recover
->context_id
;
1623 int lretry
= 20; /* up to 2 seconds */
1626 atomic_inc(&cfg
->recovery_threads
);
1627 up_read(&cfg
->ioctl_rwsem
);
1628 rc
= mutex_lock_interruptible(mutex
);
1629 down_read(&cfg
->ioctl_rwsem
);
1632 rc
= check_state(cfg
);
1634 dev_err(dev
, "%s: Failed state! rc=%d\n", __func__
, rc
);
1639 dev_dbg(dev
, "%s: reason 0x%016llX rctxid=%016llX\n",
1640 __func__
, recover
->reason
, rctxid
);
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
);
1651 if (ctxi
->err_recovery_active
) {
1653 rc
= recover_context(cfg
, ctxi
);
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) ||
1660 dev_dbg(dev
, "%s: Going to try again!\n",
1662 mutex_unlock(mutex
);
1664 rc
= mutex_lock_interruptible(mutex
);
1673 ctxi
->err_recovery_active
= false;
1674 recover
->context_id
= ctxi
->ctxid
;
1675 recover
->adap_fd
= ctxi
->lfd
;
1676 recover
->mmio_size
= sizeof(afu
->afu_map
->hosts
[0].harea
);
1677 recover
->hdr
.return_flags
|=
1678 DK_CXLFLASH_RECOVER_AFU_CONTEXT_RESET
;
1682 /* Test if in error state */
1683 reg
= readq_be(&afu
->ctrl_map
->mbox_r
);
1685 dev_dbg(dev
, "%s: MMIO fail, wait for recovery.\n", __func__
);
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).
1695 rc
= check_state(cfg
);
1701 dev_dbg(dev
, "%s: MMIO working, no recovery required!\n", __func__
);
1705 mutex_unlock(mutex
);
1706 atomic_dec_if_positive(&cfg
->recovery_threads
);
1711 * process_sense() - evaluates and processes sense data
1712 * @sdev: SCSI device associated with LUN.
1713 * @verify: Verify ioctl data structure.
1715 * Return: 0 on success, -errno on failure
1717 static int process_sense(struct scsi_device
*sdev
,
1718 struct dk_cxlflash_verify
*verify
)
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 };
1728 rc
= scsi_normalize_sense((const u8
*)&verify
->sense_data
,
1729 DK_CXLFLASH_VERIFY_SENSE_LEN
, &sshdr
);
1731 dev_err(dev
, "%s: Failed to normalize sense data!\n", __func__
);
1736 switch (sshdr
.sense_key
) {
1738 case RECOVERED_ERROR
:
1742 case UNIT_ATTENTION
:
1743 switch (sshdr
.asc
) {
1744 case 0x29: /* Power on Reset or Device Reset */
1746 case 0x2A: /* Device settings/capacity changed */
1747 rc
= read_cap16(sdev
, lli
);
1752 if (prev_lba
!= gli
->max_lba
)
1753 dev_dbg(dev
, "%s: Capacity changed old=%lld "
1754 "new=%lld\n", __func__
, prev_lba
,
1757 case 0x3F: /* Report LUNs changed, Rescan. */
1758 scsi_scan_host(cfg
->host
);
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
);
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.
1780 * Return: 0 on success, -errno on failure
1782 static int cxlflash_disk_verify(struct scsi_device
*sdev
,
1783 struct dk_cxlflash_verify
*verify
)
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
;
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
);
1801 ctxi
= get_context(cfg
, rctxid
, lli
, 0);
1802 if (unlikely(!ctxi
)) {
1803 dev_dbg(dev
, "%s: Bad context! (%llu)\n", __func__
, ctxid
);
1808 rhte
= get_rhte(ctxi
, rhndl
, lli
);
1809 if (unlikely(!rhte
)) {
1810 dev_dbg(dev
, "%s: Bad resource handle! (%d)\n",
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).
1820 if (verify
->hint
& DK_CXLFLASH_VERIFY_HINT_SENSE
) {
1821 /* Can't hold mutex across process_sense/read_cap16,
1822 * since we could have an intervening EEH event.
1824 ctxi
->unavail
= true;
1825 mutex_unlock(&ctxi
->mutex
);
1826 rc
= process_sense(sdev
, verify
);
1828 dev_err(dev
, "%s: Failed to validate sense data (%d)\n",
1830 mutex_lock(&ctxi
->mutex
);
1831 ctxi
->unavail
= false;
1834 mutex_lock(&ctxi
->mutex
);
1835 ctxi
->unavail
= false;
1838 switch (gli
->mode
) {
1840 last_lba
= gli
->max_lba
;
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
;
1849 WARN(1, "Unsupported LUN mode!");
1852 verify
->last_lba
= last_lba
;
1857 dev_dbg(dev
, "%s: returning rc=%d llba=%llX\n",
1858 __func__
, rc
, verify
->last_lba
);
1863 * decode_ioctl() - translates an encoded ioctl to an easily identifiable string
1864 * @cmd: The ioctl command to decode.
1866 * Return: A string identifying the decoded ioctl.
1868 static char *decode_ioctl(int 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
);
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
);
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
);
1885 case DK_CXLFLASH_VLUN_CLONE
:
1886 return __stringify_1(DK_CXLFLASH_VLUN_CLONE
);
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
);
1897 * cxlflash_disk_direct_open() - opens a direct (physical) disk
1898 * @sdev: SCSI device associated with LUN.
1899 * @arg: UDirect ioctl data structure.
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.
1905 * Return: 0 on success, -errno on failure
1907 static int cxlflash_disk_direct_open(struct scsi_device
*sdev
, void *arg
)
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
;
1915 struct dk_cxlflash_udirect
*pphys
= (struct dk_cxlflash_udirect
*)arg
;
1917 u64 ctxid
= DECODE_CTXID(pphys
->context_id
),
1918 rctxid
= pphys
->context_id
;
1921 u64 rsrc_handle
= -1;
1922 u32 port
= CHAN2PORT(sdev
->channel
);
1926 struct ctx_info
*ctxi
= NULL
;
1927 struct sisl_rht_entry
*rhte
= NULL
;
1929 pr_debug("%s: ctxid=%llu ls=0x%llx\n", __func__
, ctxid
, lun_size
);
1931 rc
= cxlflash_lun_attach(gli
, MODE_PHYSICAL
, false);
1933 dev_dbg(dev
, "%s: Failed to attach to LUN! (PHYSICAL)\n",
1938 ctxi
= get_context(cfg
, rctxid
, lli
, 0);
1939 if (unlikely(!ctxi
)) {
1940 dev_dbg(dev
, "%s: Bad context! (%llu)\n", __func__
, ctxid
);
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 */
1952 rsrc_handle
= (rhte
- ctxi
->rht_start
);
1954 rht_format1(rhte
, lli
->lun_id
[sdev
->channel
], ctxi
->rht_perms
, port
);
1955 cxlflash_afu_sync(afu
, ctxid
, rsrc_handle
, AFU_LW_SYNC
);
1957 last_lba
= gli
->max_lba
;
1958 pphys
->hdr
.return_flags
= 0;
1959 pphys
->last_lba
= last_lba
;
1960 pphys
->rsrc_handle
= rsrc_handle
;
1965 dev_dbg(dev
, "%s: returning handle 0x%llx rc=%d llba %lld\n",
1966 __func__
, rsrc_handle
, rc
, last_lba
);
1970 cxlflash_lun_detach(gli
);
1975 * ioctl_common() - common IOCTL handler for driver
1976 * @sdev: SCSI device associated with LUN.
1977 * @cmd: IOCTL command.
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.
1983 * Return: 0 on success, -errno on failure
1985 static int ioctl_common(struct scsi_device
*sdev
, int cmd
)
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
;
1992 if (unlikely(!lli
)) {
1993 dev_dbg(dev
, "%s: Unknown LUN\n", __func__
);
1998 rc
= check_state(cfg
);
1999 if (unlikely(rc
) && (cfg
->state
== STATE_FAILTERM
)) {
2001 case DK_CXLFLASH_VLUN_RESIZE
:
2002 case DK_CXLFLASH_RELEASE
:
2003 case DK_CXLFLASH_DETACH
:
2004 dev_dbg(dev
, "%s: Command override! (%d)\n",
2015 * cxlflash_ioctl() - IOCTL handler for driver
2016 * @sdev: SCSI device associated with LUN.
2017 * @cmd: IOCTL command.
2018 * @arg: Userspace ioctl data structure.
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.
2028 * Return: 0 on success, -errno on failure
2030 int cxlflash_ioctl(struct scsi_device
*sdev
, int cmd
, void __user
*arg
)
2032 typedef int (*sioctl
) (struct scsi_device
*, void *);
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
)];
2040 bool known_ioctl
= false;
2043 struct Scsi_Host
*shost
= sdev
->host
;
2044 sioctl do_ioctl
= NULL
;
2046 static const struct {
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
},
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
},
2062 /* Hold read semaphore so we can drain if needed */
2063 down_read(&cfg
->ioctl_rwsem
);
2065 /* Restrict command set to physical support only for internal LUN */
2066 if (afu
->internal_lun
)
2068 case DK_CXLFLASH_RELEASE
:
2069 case DK_CXLFLASH_USER_VIRTUAL
:
2070 case DK_CXLFLASH_VLUN_RESIZE
:
2071 case DK_CXLFLASH_VLUN_CLONE
:
2072 dev_dbg(dev
, "%s: %s not supported for lun_mode=%d\n",
2073 __func__
, decode_ioctl(cmd
), afu
->internal_lun
);
2075 goto cxlflash_ioctl_exit
;
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
:
2085 case DK_CXLFLASH_USER_VIRTUAL
:
2086 case DK_CXLFLASH_VLUN_RESIZE
:
2087 case DK_CXLFLASH_VLUN_CLONE
:
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
);
2093 goto cxlflash_ioctl_exit
;
2097 case DK_CXLFLASH_MANAGE_LUN
:
2099 idx
= _IOC_NR(cmd
) - _IOC_NR(DK_CXLFLASH_ATTACH
);
2100 size
= ioctl_tbl
[idx
].size
;
2101 do_ioctl
= ioctl_tbl
[idx
].ioctl
;
2103 if (likely(do_ioctl
))
2109 goto cxlflash_ioctl_exit
;
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
);
2117 goto cxlflash_ioctl_exit
;
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
));
2125 goto cxlflash_ioctl_exit
;
2128 if (hdr
->rsvd
[0] || hdr
->rsvd
[1] || hdr
->rsvd
[2] || hdr
->return_flags
) {
2129 dev_dbg(dev
, "%s: Reserved/rflags populated!\n", __func__
);
2131 goto cxlflash_ioctl_exit
;
2134 rc
= do_ioctl(sdev
, (void *)&buf
);
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
);
2143 /* fall through to exit */
2145 cxlflash_ioctl_exit
:
2146 up_read(&cfg
->ioctl_rwsem
);
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
);
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
,