xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision 51f8e43e)
1d613b6a7SJames Smart /*******************************************************************
2d613b6a7SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
3d613b6a7SJames Smart  * Fibre Channsel Host Bus Adapters.                               *
40d041215SJames Smart  * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
54ae2ebdeSJames Smart  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6d613b6a7SJames Smart  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7d613b6a7SJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
8d080abe0SJames Smart  * www.broadcom.com                                                *
9d613b6a7SJames Smart  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10d613b6a7SJames Smart  *                                                                 *
11d613b6a7SJames Smart  * This program is free software; you can redistribute it and/or   *
12d613b6a7SJames Smart  * modify it under the terms of version 2 of the GNU General       *
13d613b6a7SJames Smart  * Public License as published by the Free Software Foundation.    *
14d613b6a7SJames Smart  * This program is distributed in the hope that it will be useful. *
15d613b6a7SJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16d613b6a7SJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17d613b6a7SJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18d613b6a7SJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19d613b6a7SJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20d613b6a7SJames Smart  * more details, a copy of which can be found in the file COPYING  *
21d613b6a7SJames Smart  * included with this package.                                     *
22d613b6a7SJames Smart  ********************************************************************/
23d613b6a7SJames Smart #include <linux/pci.h>
24d613b6a7SJames Smart #include <linux/slab.h>
25d613b6a7SJames Smart #include <linux/interrupt.h>
26d613b6a7SJames Smart #include <linux/delay.h>
27d613b6a7SJames Smart #include <asm/unaligned.h>
28d613b6a7SJames Smart #include <linux/crc-t10dif.h>
29d613b6a7SJames Smart #include <net/checksum.h>
30d613b6a7SJames Smart 
31d613b6a7SJames Smart #include <scsi/scsi.h>
32d613b6a7SJames Smart #include <scsi/scsi_device.h>
33d613b6a7SJames Smart #include <scsi/scsi_eh.h>
34d613b6a7SJames Smart #include <scsi/scsi_host.h>
35d613b6a7SJames Smart #include <scsi/scsi_tcq.h>
36d613b6a7SJames Smart #include <scsi/scsi_transport_fc.h>
37d613b6a7SJames Smart #include <scsi/fc/fc_fs.h>
38d613b6a7SJames Smart 
398d731d1aSJames Smart #include <linux/nvme.h>
40d613b6a7SJames Smart #include <linux/nvme-fc-driver.h>
41cbc5de1bSJames Smart #include <linux/nvme-fc.h>
42d613b6a7SJames Smart 
43d613b6a7SJames Smart #include "lpfc_version.h"
44d613b6a7SJames Smart #include "lpfc_hw4.h"
45d613b6a7SJames Smart #include "lpfc_hw.h"
46d613b6a7SJames Smart #include "lpfc_sli.h"
47d613b6a7SJames Smart #include "lpfc_sli4.h"
48d613b6a7SJames Smart #include "lpfc_nl.h"
49d613b6a7SJames Smart #include "lpfc_disc.h"
50d613b6a7SJames Smart #include "lpfc.h"
51d613b6a7SJames Smart #include "lpfc_scsi.h"
52d613b6a7SJames Smart #include "lpfc_nvme.h"
53d613b6a7SJames Smart #include "lpfc_nvmet.h"
54d613b6a7SJames Smart #include "lpfc_logmsg.h"
55d613b6a7SJames Smart #include "lpfc_crtn.h"
56d613b6a7SJames Smart #include "lpfc_vport.h"
572b65e182SJames Smart #include "lpfc_debugfs.h"
58d613b6a7SJames Smart 
59d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *,
60d613b6a7SJames Smart 						 struct lpfc_nvmet_rcv_ctx *,
61d613b6a7SJames Smart 						 dma_addr_t rspbuf,
62d613b6a7SJames Smart 						 uint16_t rspsize);
63d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *,
64d613b6a7SJames Smart 						  struct lpfc_nvmet_rcv_ctx *);
65d613b6a7SJames Smart static int lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *,
66d613b6a7SJames Smart 					  struct lpfc_nvmet_rcv_ctx *,
67d613b6a7SJames Smart 					  uint32_t, uint16_t);
68d613b6a7SJames Smart static int lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *,
69d613b6a7SJames Smart 					    struct lpfc_nvmet_rcv_ctx *,
70d613b6a7SJames Smart 					    uint32_t, uint16_t);
71d613b6a7SJames Smart static int lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *,
72d613b6a7SJames Smart 					   struct lpfc_nvmet_rcv_ctx *,
73d613b6a7SJames Smart 					   uint32_t, uint16_t);
746e8e1c14SJames Smart static void lpfc_nvmet_wqfull_flush(struct lpfc_hba *, struct lpfc_queue *,
756e8e1c14SJames Smart 				    struct lpfc_nvmet_rcv_ctx *);
76472e146dSJames Smart static void lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *);
77472e146dSJames Smart 
78472e146dSJames Smart static void lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf);
79d613b6a7SJames Smart 
80fab2e466SColin Ian King static union lpfc_wqe128 lpfc_tsend_cmd_template;
81fab2e466SColin Ian King static union lpfc_wqe128 lpfc_treceive_cmd_template;
82fab2e466SColin Ian King static union lpfc_wqe128 lpfc_trsp_cmd_template;
83bd3061baSJames Smart 
84bd3061baSJames Smart /* Setup WQE templates for NVME IOs */
85bd3061baSJames Smart void
86fab2e466SColin Ian King lpfc_nvmet_cmd_template(void)
87bd3061baSJames Smart {
88bd3061baSJames Smart 	union lpfc_wqe128 *wqe;
89bd3061baSJames Smart 
90bd3061baSJames Smart 	/* TSEND template */
91bd3061baSJames Smart 	wqe = &lpfc_tsend_cmd_template;
92bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
93bd3061baSJames Smart 
94bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
95bd3061baSJames Smart 
96bd3061baSJames Smart 	/* Word 3 - payload_offset_len is zero */
97bd3061baSJames Smart 
98bd3061baSJames Smart 	/* Word 4 - relative_offset is variable */
99bd3061baSJames Smart 
100bd3061baSJames Smart 	/* Word 5 - is zero */
101bd3061baSJames Smart 
102bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
103bd3061baSJames Smart 
104bd3061baSJames Smart 	/* Word 7 - wqe_ar is variable */
105bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_tsend.wqe_com, CMD_FCP_TSEND64_WQE);
106bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_tsend.wqe_com, PARM_REL_OFF);
107bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_tsend.wqe_com, CLASS3);
108bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_tsend.wqe_com, SLI4_CT_RPI);
109bd3061baSJames Smart 	bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 1);
110bd3061baSJames Smart 
111bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
112bd3061baSJames Smart 
113bd3061baSJames Smart 	/* Word 9  - reqtag, rcvoxid is variable */
114bd3061baSJames Smart 
115bd3061baSJames Smart 	/* Word 10 - wqes, xc is variable */
116bd3061baSJames Smart 	bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
117bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_tsend.wqe_com, 1);
118bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
119bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 1);
120bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_tsend.wqe_com, LPFC_WQE_IOD_WRITE);
121bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_tsend.wqe_com, LPFC_WQE_LENLOC_WORD12);
122bd3061baSJames Smart 
123bd3061baSJames Smart 	/* Word 11 - sup, irsp, irsplen is variable */
124bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_tsend.wqe_com, FCP_COMMAND_TSEND);
125bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_tsend.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
126bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
127bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
128bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
129bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_tsend.wqe_com, 0);
130bd3061baSJames Smart 
131bd3061baSJames Smart 	/* Word 12 - fcp_data_len is variable */
132bd3061baSJames Smart 
133bd3061baSJames Smart 	/* Word 13, 14, 15 - PBDE is zero */
134bd3061baSJames Smart 
135bd3061baSJames Smart 	/* TRECEIVE template */
136bd3061baSJames Smart 	wqe = &lpfc_treceive_cmd_template;
137bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
138bd3061baSJames Smart 
139bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
140bd3061baSJames Smart 
141bd3061baSJames Smart 	/* Word 3 */
142bd3061baSJames Smart 	wqe->fcp_treceive.payload_offset_len = TXRDY_PAYLOAD_LEN;
143bd3061baSJames Smart 
144bd3061baSJames Smart 	/* Word 4 - relative_offset is variable */
145bd3061baSJames Smart 
146bd3061baSJames Smart 	/* Word 5 - is zero */
147bd3061baSJames Smart 
148bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
149bd3061baSJames Smart 
150bd3061baSJames Smart 	/* Word 7 */
151bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_treceive.wqe_com, CMD_FCP_TRECEIVE64_WQE);
152bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_treceive.wqe_com, PARM_REL_OFF);
153bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_treceive.wqe_com, CLASS3);
154bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_treceive.wqe_com, SLI4_CT_RPI);
155bd3061baSJames Smart 	bf_set(wqe_ar, &wqe->fcp_treceive.wqe_com, 0);
156bd3061baSJames Smart 
157bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
158bd3061baSJames Smart 
159bd3061baSJames Smart 	/* Word 9  - reqtag, rcvoxid is variable */
160bd3061baSJames Smart 
161bd3061baSJames Smart 	/* Word 10 - xc is variable */
162bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_treceive.wqe_com, 1);
163bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_treceive.wqe_com, 0);
164bd3061baSJames Smart 	bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
165bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_treceive.wqe_com, LPFC_WQE_IOD_READ);
166bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_treceive.wqe_com, LPFC_WQE_LENLOC_WORD12);
167bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 1);
168bd3061baSJames Smart 
169bd3061baSJames Smart 	/* Word 11 - pbde is variable */
170bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_treceive.wqe_com, FCP_COMMAND_TRECEIVE);
171bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_treceive.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
172bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_treceive.wqe_com, 0);
173bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_treceive.wqe_com, 0);
174bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_treceive.wqe_com, 0);
175bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 1);
176bd3061baSJames Smart 
177bd3061baSJames Smart 	/* Word 12 - fcp_data_len is variable */
178bd3061baSJames Smart 
179bd3061baSJames Smart 	/* Word 13, 14, 15 - PBDE is variable */
180bd3061baSJames Smart 
181bd3061baSJames Smart 	/* TRSP template */
182bd3061baSJames Smart 	wqe = &lpfc_trsp_cmd_template;
183bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
184bd3061baSJames Smart 
185bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
186bd3061baSJames Smart 
187bd3061baSJames Smart 	/* Word 3 - response_len is variable */
188bd3061baSJames Smart 
189bd3061baSJames Smart 	/* Word 4, 5 - is zero */
190bd3061baSJames Smart 
191bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
192bd3061baSJames Smart 
193bd3061baSJames Smart 	/* Word 7 */
194bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_trsp.wqe_com, CMD_FCP_TRSP64_WQE);
195bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_trsp.wqe_com, PARM_UNUSED);
196bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_trsp.wqe_com, CLASS3);
197bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_trsp.wqe_com, SLI4_CT_RPI);
198bd3061baSJames Smart 	bf_set(wqe_ag, &wqe->fcp_trsp.wqe_com, 1); /* wqe_ar */
199bd3061baSJames Smart 
200bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
201bd3061baSJames Smart 
202bd3061baSJames Smart 	/* Word 9  - reqtag is variable */
203bd3061baSJames Smart 
204bd3061baSJames Smart 	/* Word 10 wqes, xc is variable */
205bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_trsp.wqe_com, 1);
206bd3061baSJames Smart 	bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
207bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 0);
208bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 0);
209bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_trsp.wqe_com, LPFC_WQE_IOD_NONE);
210bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_trsp.wqe_com, LPFC_WQE_LENLOC_WORD3);
211bd3061baSJames Smart 
212bd3061baSJames Smart 	/* Word 11 irsp, irsplen is variable */
213bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_trsp.wqe_com, FCP_COMMAND_TRSP);
214bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_trsp.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
215bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_trsp.wqe_com, 0);
216bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 0);
217bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com, 0);
218bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_trsp.wqe_com, 0);
219bd3061baSJames Smart 
220bd3061baSJames Smart 	/* Word 12, 13, 14, 15 - is zero */
221bd3061baSJames Smart }
222bd3061baSJames Smart 
223336df6ebSNathan Chancellor #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
224d7b761b0SYueHaibing static struct lpfc_nvmet_rcv_ctx *
22579d8c4ceSJames Smart lpfc_nvmet_get_ctx_for_xri(struct lpfc_hba *phba, u16 xri)
22679d8c4ceSJames Smart {
22779d8c4ceSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
22879d8c4ceSJames Smart 	unsigned long iflag;
22979d8c4ceSJames Smart 	bool found = false;
23079d8c4ceSJames Smart 
23179d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
23279d8c4ceSJames Smart 	list_for_each_entry(ctxp, &phba->sli4_hba.t_active_ctx_list, list) {
23379d8c4ceSJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
23479d8c4ceSJames Smart 			continue;
23579d8c4ceSJames Smart 
23679d8c4ceSJames Smart 		found = true;
23779d8c4ceSJames Smart 		break;
23879d8c4ceSJames Smart 	}
23979d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
24079d8c4ceSJames Smart 	if (found)
24179d8c4ceSJames Smart 		return ctxp;
24279d8c4ceSJames Smart 
24379d8c4ceSJames Smart 	return NULL;
24479d8c4ceSJames Smart }
24579d8c4ceSJames Smart 
246d7b761b0SYueHaibing static struct lpfc_nvmet_rcv_ctx *
24779d8c4ceSJames Smart lpfc_nvmet_get_ctx_for_oxid(struct lpfc_hba *phba, u16 oxid, u32 sid)
24879d8c4ceSJames Smart {
24979d8c4ceSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
25079d8c4ceSJames Smart 	unsigned long iflag;
25179d8c4ceSJames Smart 	bool found = false;
25279d8c4ceSJames Smart 
25379d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
25479d8c4ceSJames Smart 	list_for_each_entry(ctxp, &phba->sli4_hba.t_active_ctx_list, list) {
25579d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
25679d8c4ceSJames Smart 			continue;
25779d8c4ceSJames Smart 
25879d8c4ceSJames Smart 		found = true;
25979d8c4ceSJames Smart 		break;
26079d8c4ceSJames Smart 	}
26179d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
26279d8c4ceSJames Smart 	if (found)
26379d8c4ceSJames Smart 		return ctxp;
26479d8c4ceSJames Smart 
26579d8c4ceSJames Smart 	return NULL;
26679d8c4ceSJames Smart }
267336df6ebSNathan Chancellor #endif
26879d8c4ceSJames Smart 
2693999df75SBart Van Assche static void
27086c67379SJames Smart lpfc_nvmet_defer_release(struct lpfc_hba *phba, struct lpfc_nvmet_rcv_ctx *ctxp)
27186c67379SJames Smart {
272472e146dSJames Smart 	lockdep_assert_held(&ctxp->ctxlock);
27386c67379SJames Smart 
274e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
27579d8c4ceSJames Smart 			"6313 NVMET Defer ctx release oxid x%x flg x%x\n",
27686c67379SJames Smart 			ctxp->oxid, ctxp->flag);
27786c67379SJames Smart 
278472e146dSJames Smart 	if (ctxp->flag & LPFC_NVMET_CTX_RLS)
27986c67379SJames Smart 		return;
280472e146dSJames Smart 
28186c67379SJames Smart 	ctxp->flag |= LPFC_NVMET_CTX_RLS;
28279d8c4ceSJames Smart 	spin_lock(&phba->sli4_hba.t_active_list_lock);
28379d8c4ceSJames Smart 	list_del(&ctxp->list);
28479d8c4ceSJames Smart 	spin_unlock(&phba->sli4_hba.t_active_list_lock);
285472e146dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
28686c67379SJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
287472e146dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
28886c67379SJames Smart }
28986c67379SJames Smart 
290d613b6a7SJames Smart /**
291d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
292d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
293d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
294d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
295d613b6a7SJames Smart  *
296d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
297d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME LS commands
298d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
299d613b6a7SJames Smart  **/
300d613b6a7SJames Smart static void
301d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
302d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
303d613b6a7SJames Smart {
304d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
305d613b6a7SJames Smart 	struct nvmefc_tgt_ls_req *rsp;
306d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
307d613b6a7SJames Smart 	uint32_t status, result;
308d613b6a7SJames Smart 
309d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
310d613b6a7SJames Smart 	result = wcqe->parameter;
311ce1b591cSJames Smart 	ctxp = cmdwqe->context2;
312ce1b591cSJames Smart 
313ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_LS_RSP || ctxp->entry_cnt != 2) {
314ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
315ce1b591cSJames Smart 				"6410 NVMET LS cmpl state mismatch IO x%x: "
316ce1b591cSJames Smart 				"%d %d\n",
317ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
318ce1b591cSJames Smart 	}
319ce1b591cSJames Smart 
320d613b6a7SJames Smart 	if (!phba->targetport)
321d613b6a7SJames Smart 		goto out;
322d613b6a7SJames Smart 
323d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
324d613b6a7SJames Smart 
3254b056682SJames Smart 	if (tgtp) {
3264b056682SJames Smart 		if (status) {
327d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_error);
3288ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
3294b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_aborted);
3304b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
3314b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_xb_set);
3324b056682SJames Smart 		} else {
333d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
3344b056682SJames Smart 		}
3354b056682SJames Smart 	}
336d613b6a7SJames Smart 
337d613b6a7SJames Smart out:
338d613b6a7SJames Smart 	rsp = &ctxp->ctx.ls_req;
339d613b6a7SJames Smart 
3402b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  CMPL: xri x%x stat x%x result x%x\n",
3412b65e182SJames Smart 			 ctxp->oxid, status, result);
3422b65e182SJames Smart 
343d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
344ce1b591cSJames Smart 			"6038 NVMET LS rsp cmpl: %d %d oxid x%x\n",
345ce1b591cSJames Smart 			status, result, ctxp->oxid);
346d613b6a7SJames Smart 
347d613b6a7SJames Smart 	lpfc_nlp_put(cmdwqe->context1);
348d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
349d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
350d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
351d613b6a7SJames Smart 	rsp->done(rsp);
352d613b6a7SJames Smart 	kfree(ctxp);
353d613b6a7SJames Smart }
354d613b6a7SJames Smart 
355d613b6a7SJames Smart /**
3566c621a22SJames Smart  * lpfc_nvmet_ctxbuf_post - Repost a NVMET RQ DMA buffer and clean up context
357d613b6a7SJames Smart  * @phba: HBA buffer is associated with
358d613b6a7SJames Smart  * @ctxp: context to clean up
359d613b6a7SJames Smart  * @mp: Buffer to free
360d613b6a7SJames Smart  *
361d613b6a7SJames Smart  * Description: Frees the given DMA buffer in the appropriate way given by
362d613b6a7SJames Smart  * reposting it to its associated RQ so it can be reused.
363d613b6a7SJames Smart  *
364d613b6a7SJames Smart  * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
365d613b6a7SJames Smart  *
366d613b6a7SJames Smart  * Returns: None
367d613b6a7SJames Smart  **/
368d613b6a7SJames Smart void
3696c621a22SJames Smart lpfc_nvmet_ctxbuf_post(struct lpfc_hba *phba, struct lpfc_nvmet_ctxbuf *ctx_buf)
370d613b6a7SJames Smart {
371eeeb51d8SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
3726c621a22SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
373a8cf5dfeSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
374a8cf5dfeSJames Smart 	struct fc_frame_header *fc_hdr;
375a8cf5dfeSJames Smart 	struct rqb_dmabuf *nvmebuf;
37666d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
377472e146dSJames Smart 	uint32_t size, oxid, sid;
37866d7ce93SDick Kennedy 	int cpu;
3796c621a22SJames Smart 	unsigned long iflag;
38086c67379SJames Smart 
381d613b6a7SJames Smart 	if (ctxp->txrdy) {
382771db5c0SRomain Perier 		dma_pool_free(phba->txrdy_payload_pool, ctxp->txrdy,
383d613b6a7SJames Smart 			      ctxp->txrdy_phys);
384d613b6a7SJames Smart 		ctxp->txrdy = NULL;
385d613b6a7SJames Smart 		ctxp->txrdy_phys = 0;
386d613b6a7SJames Smart 	}
387ce1b591cSJames Smart 
388ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_FREE) {
389ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
390ce1b591cSJames Smart 				"6411 NVMET free, already free IO x%x: %d %d\n",
391ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
392ce1b591cSJames Smart 	}
393472e146dSJames Smart 
394472e146dSJames Smart 	if (ctxp->rqb_buffer) {
395472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
3964767c58aSJames Smart 		nvmebuf = ctxp->rqb_buffer;
3974767c58aSJames Smart 		/* check if freed in another path whilst acquiring lock */
3984767c58aSJames Smart 		if (nvmebuf) {
399472e146dSJames Smart 			ctxp->rqb_buffer = NULL;
400472e146dSJames Smart 			if (ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) {
401472e146dSJames Smart 				ctxp->flag &= ~LPFC_NVMET_CTX_REUSE_WQ;
402472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4034767c58aSJames Smart 				nvmebuf->hrq->rqbp->rqb_free_buffer(phba,
4044767c58aSJames Smart 								    nvmebuf);
405472e146dSJames Smart 			} else {
406472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4074767c58aSJames Smart 				/* repost */
4084767c58aSJames Smart 				lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
4094767c58aSJames Smart 			}
4104767c58aSJames Smart 		} else {
4114767c58aSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
412472e146dSJames Smart 		}
413472e146dSJames Smart 	}
414d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_FREE;
4156c621a22SJames Smart 
416a8cf5dfeSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
417a8cf5dfeSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
418a8cf5dfeSJames Smart 		list_remove_head(&phba->sli4_hba.lpfc_nvmet_io_wait_list,
419a8cf5dfeSJames Smart 				 nvmebuf, struct rqb_dmabuf,
420a8cf5dfeSJames Smart 				 hbuf.list);
421a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt--;
422a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
423a8cf5dfeSJames Smart 				       iflag);
424a8cf5dfeSJames Smart 
425a8cf5dfeSJames Smart 		fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
426a8cf5dfeSJames Smart 		oxid = be16_to_cpu(fc_hdr->fh_ox_id);
427a8cf5dfeSJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
428a8cf5dfeSJames Smart 		size = nvmebuf->bytes_recv;
429a8cf5dfeSJames Smart 		sid = sli4_sid_from_fc_hdr(fc_hdr);
430a8cf5dfeSJames Smart 
431a8cf5dfeSJames Smart 		ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
432a8cf5dfeSJames Smart 		ctxp->wqeq = NULL;
433a8cf5dfeSJames Smart 		ctxp->txrdy = NULL;
434a8cf5dfeSJames Smart 		ctxp->offset = 0;
435a8cf5dfeSJames Smart 		ctxp->phba = phba;
436a8cf5dfeSJames Smart 		ctxp->size = size;
437a8cf5dfeSJames Smart 		ctxp->oxid = oxid;
438a8cf5dfeSJames Smart 		ctxp->sid = sid;
439a8cf5dfeSJames Smart 		ctxp->state = LPFC_NVMET_STE_RCV;
440a8cf5dfeSJames Smart 		ctxp->entry_cnt = 1;
441a8cf5dfeSJames Smart 		ctxp->flag = 0;
442a8cf5dfeSJames Smart 		ctxp->ctxbuf = ctx_buf;
443cbc5de1bSJames Smart 		ctxp->rqb_buffer = (void *)nvmebuf;
444a8cf5dfeSJames Smart 		spin_lock_init(&ctxp->ctxlock);
445a8cf5dfeSJames Smart 
446a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
447d74a89aaSJames Smart 		/* NOTE: isr time stamp is stale when context is re-assigned*/
448d74a89aaSJames Smart 		if (ctxp->ts_isr_cmd) {
449d74a89aaSJames Smart 			ctxp->ts_cmd_nvme = 0;
450a8cf5dfeSJames Smart 			ctxp->ts_nvme_data = 0;
451a8cf5dfeSJames Smart 			ctxp->ts_data_wqput = 0;
452a8cf5dfeSJames Smart 			ctxp->ts_isr_data = 0;
453a8cf5dfeSJames Smart 			ctxp->ts_data_nvme = 0;
454a8cf5dfeSJames Smart 			ctxp->ts_nvme_status = 0;
455a8cf5dfeSJames Smart 			ctxp->ts_status_wqput = 0;
456a8cf5dfeSJames Smart 			ctxp->ts_isr_status = 0;
457a8cf5dfeSJames Smart 			ctxp->ts_status_nvme = 0;
458d613b6a7SJames Smart 		}
459a8cf5dfeSJames Smart #endif
460a8cf5dfeSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_in);
461472e146dSJames Smart 
46279d8c4ceSJames Smart 		/* Indicate that a replacement buffer has been posted */
463472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
464472e146dSJames Smart 		ctxp->flag |= LPFC_NVMET_CTX_REUSE_WQ;
465472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
466a8cf5dfeSJames Smart 
467472e146dSJames Smart 		if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
468a8cf5dfeSJames Smart 			atomic_inc(&tgtp->rcv_fcp_cmd_drop);
469472e146dSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
470472e146dSJames Smart 					"6181 Unable to queue deferred work "
471472e146dSJames Smart 					"for oxid x%x. "
472472e146dSJames Smart 					"FCP Drop IO [x%x x%x x%x]\n",
473472e146dSJames Smart 					ctxp->oxid,
474a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_in),
475a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_out),
476a8cf5dfeSJames Smart 					atomic_read(&tgtp->xmt_fcp_release));
477a8cf5dfeSJames Smart 
478472e146dSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
479a8cf5dfeSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
480472e146dSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
481a8cf5dfeSJames Smart 			lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
482472e146dSJames Smart 		}
483a8cf5dfeSJames Smart 		return;
484a8cf5dfeSJames Smart 	}
485a8cf5dfeSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
486a8cf5dfeSJames Smart 
48766d7ce93SDick Kennedy 	/*
48866d7ce93SDick Kennedy 	 * Use the CPU context list, from the MRQ the IO was received on
48966d7ce93SDick Kennedy 	 * (ctxp->idx), to save context structure.
49066d7ce93SDick Kennedy 	 */
49179d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
49279d8c4ceSJames Smart 	list_del_init(&ctxp->list);
49379d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
494d6d189ceSBart Van Assche 	cpu = raw_smp_processor_id();
49566d7ce93SDick Kennedy 	infop = lpfc_get_ctx_list(phba, cpu, ctxp->idx);
49666d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, iflag);
49766d7ce93SDick Kennedy 	list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
49866d7ce93SDick Kennedy 	infop->nvmet_ctx_list_cnt++;
49966d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, iflag);
500eeeb51d8SJames Smart #endif
501d613b6a7SJames Smart }
502d613b6a7SJames Smart 
5032b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5042b65e182SJames Smart static void
5052b65e182SJames Smart lpfc_nvmet_ktime(struct lpfc_hba *phba,
5062b65e182SJames Smart 		 struct lpfc_nvmet_rcv_ctx *ctxp)
5072b65e182SJames Smart {
5082b65e182SJames Smart 	uint64_t seg1, seg2, seg3, seg4, seg5;
5092b65e182SJames Smart 	uint64_t seg6, seg7, seg8, seg9, seg10;
510c8a4ce0bSDick Kennedy 	uint64_t segsum;
5112b65e182SJames Smart 
5122b65e182SJames Smart 	if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
5132b65e182SJames Smart 	    !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
5142b65e182SJames Smart 	    !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
5152b65e182SJames Smart 	    !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
5162b65e182SJames Smart 	    !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
5172b65e182SJames Smart 		return;
5182b65e182SJames Smart 
519c8a4ce0bSDick Kennedy 	if (ctxp->ts_status_nvme < ctxp->ts_isr_cmd)
520c8a4ce0bSDick Kennedy 		return;
5212b65e182SJames Smart 	if (ctxp->ts_isr_cmd  > ctxp->ts_cmd_nvme)
5222b65e182SJames Smart 		return;
5232b65e182SJames Smart 	if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
5242b65e182SJames Smart 		return;
5252b65e182SJames Smart 	if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
5262b65e182SJames Smart 		return;
5272b65e182SJames Smart 	if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
5282b65e182SJames Smart 		return;
5292b65e182SJames Smart 	if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
5302b65e182SJames Smart 		return;
5312b65e182SJames Smart 	if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
5322b65e182SJames Smart 		return;
5332b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
5342b65e182SJames Smart 		return;
5352b65e182SJames Smart 	if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
5362b65e182SJames Smart 		return;
5372b65e182SJames Smart 	if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
5382b65e182SJames Smart 		return;
5392b65e182SJames Smart 	/*
5402b65e182SJames Smart 	 * Segment 1 - Time from FCP command received by MSI-X ISR
5412b65e182SJames Smart 	 * to FCP command is passed to NVME Layer.
5422b65e182SJames Smart 	 * Segment 2 - Time from FCP command payload handed
5432b65e182SJames Smart 	 * off to NVME Layer to Driver receives a Command op
5442b65e182SJames Smart 	 * from NVME Layer.
5452b65e182SJames Smart 	 * Segment 3 - Time from Driver receives a Command op
5462b65e182SJames Smart 	 * from NVME Layer to Command is put on WQ.
5472b65e182SJames Smart 	 * Segment 4 - Time from Driver WQ put is done
5482b65e182SJames Smart 	 * to MSI-X ISR for Command cmpl.
5492b65e182SJames Smart 	 * Segment 5 - Time from MSI-X ISR for Command cmpl to
5502b65e182SJames Smart 	 * Command cmpl is passed to NVME Layer.
5512b65e182SJames Smart 	 * Segment 6 - Time from Command cmpl is passed to NVME
5522b65e182SJames Smart 	 * Layer to Driver receives a RSP op from NVME Layer.
5532b65e182SJames Smart 	 * Segment 7 - Time from Driver receives a RSP op from
5542b65e182SJames Smart 	 * NVME Layer to WQ put is done on TRSP FCP Status.
5552b65e182SJames Smart 	 * Segment 8 - Time from Driver WQ put is done on TRSP
5562b65e182SJames Smart 	 * FCP Status to MSI-X ISR for TRSP cmpl.
5572b65e182SJames Smart 	 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
5582b65e182SJames Smart 	 * TRSP cmpl is passed to NVME Layer.
5592b65e182SJames Smart 	 * Segment 10 - Time from FCP command received by
5602b65e182SJames Smart 	 * MSI-X ISR to command is completed on wire.
5612b65e182SJames Smart 	 * (Segments 1 thru 8) for READDATA / WRITEDATA
5622b65e182SJames Smart 	 * (Segments 1 thru 4) for READDATA_RSP
5632b65e182SJames Smart 	 */
5642b65e182SJames Smart 	seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
565c8a4ce0bSDick Kennedy 	segsum = seg1;
566c8a4ce0bSDick Kennedy 
567c8a4ce0bSDick Kennedy 	seg2 = ctxp->ts_nvme_data - ctxp->ts_isr_cmd;
568c8a4ce0bSDick Kennedy 	if (segsum > seg2)
569c8a4ce0bSDick Kennedy 		return;
570c8a4ce0bSDick Kennedy 	seg2 -= segsum;
571c8a4ce0bSDick Kennedy 	segsum += seg2;
572c8a4ce0bSDick Kennedy 
573c8a4ce0bSDick Kennedy 	seg3 = ctxp->ts_data_wqput - ctxp->ts_isr_cmd;
574c8a4ce0bSDick Kennedy 	if (segsum > seg3)
575c8a4ce0bSDick Kennedy 		return;
576c8a4ce0bSDick Kennedy 	seg3 -= segsum;
577c8a4ce0bSDick Kennedy 	segsum += seg3;
578c8a4ce0bSDick Kennedy 
579c8a4ce0bSDick Kennedy 	seg4 = ctxp->ts_isr_data - ctxp->ts_isr_cmd;
580c8a4ce0bSDick Kennedy 	if (segsum > seg4)
581c8a4ce0bSDick Kennedy 		return;
582c8a4ce0bSDick Kennedy 	seg4 -= segsum;
583c8a4ce0bSDick Kennedy 	segsum += seg4;
584c8a4ce0bSDick Kennedy 
585c8a4ce0bSDick Kennedy 	seg5 = ctxp->ts_data_nvme - ctxp->ts_isr_cmd;
586c8a4ce0bSDick Kennedy 	if (segsum > seg5)
587c8a4ce0bSDick Kennedy 		return;
588c8a4ce0bSDick Kennedy 	seg5 -= segsum;
589c8a4ce0bSDick Kennedy 	segsum += seg5;
590c8a4ce0bSDick Kennedy 
5912b65e182SJames Smart 
5922b65e182SJames Smart 	/* For auto rsp commands seg6 thru seg10 will be 0 */
5932b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
594c8a4ce0bSDick Kennedy 		seg6 = ctxp->ts_nvme_status - ctxp->ts_isr_cmd;
595c8a4ce0bSDick Kennedy 		if (segsum > seg6)
596c8a4ce0bSDick Kennedy 			return;
597c8a4ce0bSDick Kennedy 		seg6 -= segsum;
598c8a4ce0bSDick Kennedy 		segsum += seg6;
599c8a4ce0bSDick Kennedy 
600c8a4ce0bSDick Kennedy 		seg7 = ctxp->ts_status_wqput - ctxp->ts_isr_cmd;
601c8a4ce0bSDick Kennedy 		if (segsum > seg7)
602c8a4ce0bSDick Kennedy 			return;
603c8a4ce0bSDick Kennedy 		seg7 -= segsum;
604c8a4ce0bSDick Kennedy 		segsum += seg7;
605c8a4ce0bSDick Kennedy 
606c8a4ce0bSDick Kennedy 		seg8 = ctxp->ts_isr_status - ctxp->ts_isr_cmd;
607c8a4ce0bSDick Kennedy 		if (segsum > seg8)
608c8a4ce0bSDick Kennedy 			return;
609c8a4ce0bSDick Kennedy 		seg8 -= segsum;
610c8a4ce0bSDick Kennedy 		segsum += seg8;
611c8a4ce0bSDick Kennedy 
612c8a4ce0bSDick Kennedy 		seg9 = ctxp->ts_status_nvme - ctxp->ts_isr_cmd;
613c8a4ce0bSDick Kennedy 		if (segsum > seg9)
614c8a4ce0bSDick Kennedy 			return;
615c8a4ce0bSDick Kennedy 		seg9 -= segsum;
616c8a4ce0bSDick Kennedy 		segsum += seg9;
617c8a4ce0bSDick Kennedy 
618c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_status < ctxp->ts_isr_cmd)
619c8a4ce0bSDick Kennedy 			return;
6202b65e182SJames Smart 		seg10 = (ctxp->ts_isr_status -
6212b65e182SJames Smart 			ctxp->ts_isr_cmd);
6222b65e182SJames Smart 	} else {
623c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_data < ctxp->ts_isr_cmd)
624c8a4ce0bSDick Kennedy 			return;
6252b65e182SJames Smart 		seg6 =  0;
6262b65e182SJames Smart 		seg7 =  0;
6272b65e182SJames Smart 		seg8 =  0;
6282b65e182SJames Smart 		seg9 =  0;
6292b65e182SJames Smart 		seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
6302b65e182SJames Smart 	}
6312b65e182SJames Smart 
6322b65e182SJames Smart 	phba->ktime_seg1_total += seg1;
6332b65e182SJames Smart 	if (seg1 < phba->ktime_seg1_min)
6342b65e182SJames Smart 		phba->ktime_seg1_min = seg1;
6352b65e182SJames Smart 	else if (seg1 > phba->ktime_seg1_max)
6362b65e182SJames Smart 		phba->ktime_seg1_max = seg1;
6372b65e182SJames Smart 
6382b65e182SJames Smart 	phba->ktime_seg2_total += seg2;
6392b65e182SJames Smart 	if (seg2 < phba->ktime_seg2_min)
6402b65e182SJames Smart 		phba->ktime_seg2_min = seg2;
6412b65e182SJames Smart 	else if (seg2 > phba->ktime_seg2_max)
6422b65e182SJames Smart 		phba->ktime_seg2_max = seg2;
6432b65e182SJames Smart 
6442b65e182SJames Smart 	phba->ktime_seg3_total += seg3;
6452b65e182SJames Smart 	if (seg3 < phba->ktime_seg3_min)
6462b65e182SJames Smart 		phba->ktime_seg3_min = seg3;
6472b65e182SJames Smart 	else if (seg3 > phba->ktime_seg3_max)
6482b65e182SJames Smart 		phba->ktime_seg3_max = seg3;
6492b65e182SJames Smart 
6502b65e182SJames Smart 	phba->ktime_seg4_total += seg4;
6512b65e182SJames Smart 	if (seg4 < phba->ktime_seg4_min)
6522b65e182SJames Smart 		phba->ktime_seg4_min = seg4;
6532b65e182SJames Smart 	else if (seg4 > phba->ktime_seg4_max)
6542b65e182SJames Smart 		phba->ktime_seg4_max = seg4;
6552b65e182SJames Smart 
6562b65e182SJames Smart 	phba->ktime_seg5_total += seg5;
6572b65e182SJames Smart 	if (seg5 < phba->ktime_seg5_min)
6582b65e182SJames Smart 		phba->ktime_seg5_min = seg5;
6592b65e182SJames Smart 	else if (seg5 > phba->ktime_seg5_max)
6602b65e182SJames Smart 		phba->ktime_seg5_max = seg5;
6612b65e182SJames Smart 
6622b65e182SJames Smart 	phba->ktime_data_samples++;
6632b65e182SJames Smart 	if (!seg6)
6642b65e182SJames Smart 		goto out;
6652b65e182SJames Smart 
6662b65e182SJames Smart 	phba->ktime_seg6_total += seg6;
6672b65e182SJames Smart 	if (seg6 < phba->ktime_seg6_min)
6682b65e182SJames Smart 		phba->ktime_seg6_min = seg6;
6692b65e182SJames Smart 	else if (seg6 > phba->ktime_seg6_max)
6702b65e182SJames Smart 		phba->ktime_seg6_max = seg6;
6712b65e182SJames Smart 
6722b65e182SJames Smart 	phba->ktime_seg7_total += seg7;
6732b65e182SJames Smart 	if (seg7 < phba->ktime_seg7_min)
6742b65e182SJames Smart 		phba->ktime_seg7_min = seg7;
6752b65e182SJames Smart 	else if (seg7 > phba->ktime_seg7_max)
6762b65e182SJames Smart 		phba->ktime_seg7_max = seg7;
6772b65e182SJames Smart 
6782b65e182SJames Smart 	phba->ktime_seg8_total += seg8;
6792b65e182SJames Smart 	if (seg8 < phba->ktime_seg8_min)
6802b65e182SJames Smart 		phba->ktime_seg8_min = seg8;
6812b65e182SJames Smart 	else if (seg8 > phba->ktime_seg8_max)
6822b65e182SJames Smart 		phba->ktime_seg8_max = seg8;
6832b65e182SJames Smart 
6842b65e182SJames Smart 	phba->ktime_seg9_total += seg9;
6852b65e182SJames Smart 	if (seg9 < phba->ktime_seg9_min)
6862b65e182SJames Smart 		phba->ktime_seg9_min = seg9;
6872b65e182SJames Smart 	else if (seg9 > phba->ktime_seg9_max)
6882b65e182SJames Smart 		phba->ktime_seg9_max = seg9;
6892b65e182SJames Smart out:
6902b65e182SJames Smart 	phba->ktime_seg10_total += seg10;
6912b65e182SJames Smart 	if (seg10 < phba->ktime_seg10_min)
6922b65e182SJames Smart 		phba->ktime_seg10_min = seg10;
6932b65e182SJames Smart 	else if (seg10 > phba->ktime_seg10_max)
6942b65e182SJames Smart 		phba->ktime_seg10_max = seg10;
6952b65e182SJames Smart 	phba->ktime_status_samples++;
6962b65e182SJames Smart }
6972b65e182SJames Smart #endif
6982b65e182SJames Smart 
699d613b6a7SJames Smart /**
700d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
701d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
702d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
703d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
704d613b6a7SJames Smart  *
705d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
706d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME FCP commands
707d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
708d613b6a7SJames Smart  **/
709d613b6a7SJames Smart static void
710d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
711d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
712d613b6a7SJames Smart {
713d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
714d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
715d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
716e3246a12SJames Smart 	uint32_t status, result, op, start_clean, logerr;
7172b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
7182b65e182SJames Smart 	uint32_t id;
7192b65e182SJames Smart #endif
720d613b6a7SJames Smart 
721d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
72286c67379SJames Smart 	ctxp->flag &= ~LPFC_NVMET_IO_INP;
72386c67379SJames Smart 
724d613b6a7SJames Smart 	rsp = &ctxp->ctx.fcp_req;
725d613b6a7SJames Smart 	op = rsp->op;
726d613b6a7SJames Smart 
727d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
728d613b6a7SJames Smart 	result = wcqe->parameter;
729d613b6a7SJames Smart 
73086c67379SJames Smart 	if (phba->targetport)
73186c67379SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
73286c67379SJames Smart 	else
73386c67379SJames Smart 		tgtp = NULL;
734d613b6a7SJames Smart 
7352b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
7362b65e182SJames Smart 			 ctxp->oxid, op, status);
7372b65e182SJames Smart 
738d613b6a7SJames Smart 	if (status) {
739d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
740d613b6a7SJames Smart 		rsp->transferred_length = 0;
7414b056682SJames Smart 		if (tgtp) {
742d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_error);
7438ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
7444b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_aborted);
7454b056682SJames Smart 		}
74686c67379SJames Smart 
747e3246a12SJames Smart 		logerr = LOG_NVME_IOERR;
748e3246a12SJames Smart 
74986c67379SJames Smart 		/* pick up SLI4 exhange busy condition */
75086c67379SJames Smart 		if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
75186c67379SJames Smart 			ctxp->flag |= LPFC_NVMET_XBUSY;
752e3246a12SJames Smart 			logerr |= LOG_NVME_ABTS;
7534b056682SJames Smart 			if (tgtp)
7544b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_xb_set);
75586c67379SJames Smart 
75686c67379SJames Smart 		} else {
75786c67379SJames Smart 			ctxp->flag &= ~LPFC_NVMET_XBUSY;
75886c67379SJames Smart 		}
75986c67379SJames Smart 
760e3246a12SJames Smart 		lpfc_printf_log(phba, KERN_INFO, logerr,
76179d8c4ceSJames Smart 				"6315 IO Error Cmpl oxid: x%x xri: x%x %x/%x "
76279d8c4ceSJames Smart 				"XBUSY:x%x\n",
76379d8c4ceSJames Smart 				ctxp->oxid, ctxp->ctxbuf->sglq->sli4_xritag,
76479d8c4ceSJames Smart 				status, result, ctxp->flag);
765e3246a12SJames Smart 
766d613b6a7SJames Smart 	} else {
767d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_SUCCESS;
768d613b6a7SJames Smart 		if (op == NVMET_FCOP_RSP)
769d613b6a7SJames Smart 			rsp->transferred_length = rsp->rsplen;
770d613b6a7SJames Smart 		else
771d613b6a7SJames Smart 			rsp->transferred_length = rsp->transfer_length;
77286c67379SJames Smart 		if (tgtp)
773d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
774d613b6a7SJames Smart 	}
775d613b6a7SJames Smart 
776d613b6a7SJames Smart 	if ((op == NVMET_FCOP_READDATA_RSP) ||
777d613b6a7SJames Smart 	    (op == NVMET_FCOP_RSP)) {
778d613b6a7SJames Smart 		/* Sanity check */
779d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DONE;
780d613b6a7SJames Smart 		ctxp->entry_cnt++;
78186c67379SJames Smart 
7822b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
783c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
7842b65e182SJames Smart 			if (rsp->op == NVMET_FCOP_READDATA_RSP) {
7852b65e182SJames Smart 				ctxp->ts_isr_data =
7862b65e182SJames Smart 					cmdwqe->isr_timestamp;
7872b65e182SJames Smart 				ctxp->ts_data_nvme =
7882b65e182SJames Smart 					ktime_get_ns();
7892b65e182SJames Smart 				ctxp->ts_nvme_status =
7902b65e182SJames Smart 					ctxp->ts_data_nvme;
7912b65e182SJames Smart 				ctxp->ts_status_wqput =
7922b65e182SJames Smart 					ctxp->ts_data_nvme;
7932b65e182SJames Smart 				ctxp->ts_isr_status =
7942b65e182SJames Smart 					ctxp->ts_data_nvme;
7952b65e182SJames Smart 				ctxp->ts_status_nvme =
7962b65e182SJames Smart 					ctxp->ts_data_nvme;
7972b65e182SJames Smart 			} else {
7982b65e182SJames Smart 				ctxp->ts_isr_status =
7992b65e182SJames Smart 					cmdwqe->isr_timestamp;
8002b65e182SJames Smart 				ctxp->ts_status_nvme =
8012b65e182SJames Smart 					ktime_get_ns();
8022b65e182SJames Smart 			}
8032b65e182SJames Smart 		}
8042b65e182SJames Smart #endif
805d613b6a7SJames Smart 		rsp->done(rsp);
8062b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
807c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme)
8082b65e182SJames Smart 			lpfc_nvmet_ktime(phba, ctxp);
8092b65e182SJames Smart #endif
81019b58d94SJames Smart 		/* lpfc_nvmet_xmt_fcp_release() will recycle the context */
811d613b6a7SJames Smart 	} else {
812d613b6a7SJames Smart 		ctxp->entry_cnt++;
813c6e0c925SDick Kennedy 		start_clean = offsetof(struct lpfc_iocbq, iocb_flag);
814d613b6a7SJames Smart 		memset(((char *)cmdwqe) + start_clean, 0,
815d613b6a7SJames Smart 		       (sizeof(struct lpfc_iocbq) - start_clean));
8162b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
817c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
8182b65e182SJames Smart 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
8192b65e182SJames Smart 			ctxp->ts_data_nvme = ktime_get_ns();
8202b65e182SJames Smart 		}
8212b65e182SJames Smart #endif
822d613b6a7SJames Smart 		rsp->done(rsp);
823d613b6a7SJames Smart 	}
82463df6d63SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
82563df6d63SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
826d6d189ceSBart Van Assche 		id = raw_smp_processor_id();
82763df6d63SJames Smart 		if (id < LPFC_CHECK_CPU_CNT) {
82863df6d63SJames Smart 			if (ctxp->cpu != id)
82963df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
83063df6d63SJames Smart 						"6704 CPU Check cmdcmpl: "
83163df6d63SJames Smart 						"cpu %d expect %d\n",
83263df6d63SJames Smart 						id, ctxp->cpu);
83363df6d63SJames Smart 			phba->sli4_hba.hdwq[rsp->hwqid].cpucheck_cmpl_io[id]++;
83463df6d63SJames Smart 		}
83563df6d63SJames Smart 	}
83663df6d63SJames Smart #endif
837d613b6a7SJames Smart }
838d613b6a7SJames Smart 
839d613b6a7SJames Smart static int
840d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
841d613b6a7SJames Smart 		      struct nvmefc_tgt_ls_req *rsp)
842d613b6a7SJames Smart {
843d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
844d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.ls_req);
845d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
846d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf =
847d613b6a7SJames Smart 		(struct hbq_dmabuf *)ctxp->rqb_buffer;
848d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
849d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
850d613b6a7SJames Smart 	struct lpfc_dmabuf dmabuf;
851d613b6a7SJames Smart 	struct ulp_bde64 bpl;
852d613b6a7SJames Smart 	int rc;
853d613b6a7SJames Smart 
8541abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
8551abcb371SDick Kennedy 		return -ENODEV;
8561abcb371SDick Kennedy 
8573386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
8583386f4bdSJames Smart 		return -ENODEV;
8593386f4bdSJames Smart 
860d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
861ce1b591cSJames Smart 			"6023 NVMET LS rsp oxid x%x\n", ctxp->oxid);
862ce1b591cSJames Smart 
863ce1b591cSJames Smart 	if ((ctxp->state != LPFC_NVMET_STE_LS_RCV) ||
864ce1b591cSJames Smart 	    (ctxp->entry_cnt != 1)) {
865ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
866ce1b591cSJames Smart 				"6412 NVMET LS rsp state mismatch "
867ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
868ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
869ce1b591cSJames Smart 	}
870ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_LS_RSP;
871ce1b591cSJames Smart 	ctxp->entry_cnt++;
872d613b6a7SJames Smart 
873d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, ctxp, rsp->rspdma,
874d613b6a7SJames Smart 				      rsp->rsplen);
875d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
876d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_drop);
877d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
878d613b6a7SJames Smart 				"6150 LS Drop IO x%x: Prep\n",
879d613b6a7SJames Smart 				ctxp->oxid);
880d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
881547077a4SJames Smart 		atomic_inc(&nvmep->xmt_ls_abort);
882d613b6a7SJames Smart 		lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp,
883d613b6a7SJames Smart 						ctxp->sid, ctxp->oxid);
884d613b6a7SJames Smart 		return -ENOMEM;
885d613b6a7SJames Smart 	}
886d613b6a7SJames Smart 
887d613b6a7SJames Smart 	/* Save numBdes for bpl2sgl */
888d613b6a7SJames Smart 	nvmewqeq->rsvd2 = 1;
889d613b6a7SJames Smart 	nvmewqeq->hba_wqidx = 0;
890d613b6a7SJames Smart 	nvmewqeq->context3 = &dmabuf;
891d613b6a7SJames Smart 	dmabuf.virt = &bpl;
892d613b6a7SJames Smart 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
893d613b6a7SJames Smart 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
894d613b6a7SJames Smart 	bpl.tus.f.bdeSize = rsp->rsplen;
895d613b6a7SJames Smart 	bpl.tus.f.bdeFlags = 0;
896d613b6a7SJames Smart 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
897d613b6a7SJames Smart 
898d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_rsp_cmp;
899d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
900d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
901d613b6a7SJames Smart 
9022b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  RESP: xri x%x wqidx x%x len x%x\n",
9032b65e182SJames Smart 			 ctxp->oxid, nvmewqeq->hba_wqidx, rsp->rsplen);
9042b65e182SJames Smart 
9051fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
906d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
907d613b6a7SJames Smart 		/*
908d613b6a7SJames Smart 		 * Okay to repost buffer here, but wait till cmpl
909d613b6a7SJames Smart 		 * before freeing ctxp and iocbq.
910d613b6a7SJames Smart 		 */
911d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
912d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_rsp);
913d613b6a7SJames Smart 		return 0;
914d613b6a7SJames Smart 	}
915d613b6a7SJames Smart 	/* Give back resources */
916d613b6a7SJames Smart 	atomic_inc(&nvmep->xmt_ls_drop);
917d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
918d613b6a7SJames Smart 			"6151 LS Drop IO x%x: Issue %d\n",
919d613b6a7SJames Smart 			ctxp->oxid, rc);
920d613b6a7SJames Smart 
921d613b6a7SJames Smart 	lpfc_nlp_put(nvmewqeq->context1);
922d613b6a7SJames Smart 
923d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
924547077a4SJames Smart 	atomic_inc(&nvmep->xmt_ls_abort);
925d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
926d613b6a7SJames Smart 	return -ENXIO;
927d613b6a7SJames Smart }
928d613b6a7SJames Smart 
929d613b6a7SJames Smart static int
930d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
931d613b6a7SJames Smart 		      struct nvmefc_tgt_fcp_req *rsp)
932d613b6a7SJames Smart {
933d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
934d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
935d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
936d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
9376e8e1c14SJames Smart 	struct lpfc_queue *wq;
938d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
9396e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
9406e8e1c14SJames Smart 	unsigned long iflags;
941223b78eaSArnd Bergmann 	int rc;
9422b65e182SJames Smart 
9431abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING) {
9441abcb371SDick Kennedy 		rc = -ENODEV;
9451abcb371SDick Kennedy 		goto aerr;
9461abcb371SDick Kennedy 	}
9471abcb371SDick Kennedy 
9483386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING) {
9493386f4bdSJames Smart 		rc = -ENODEV;
9503386f4bdSJames Smart 		goto aerr;
9513386f4bdSJames Smart 	}
9523386f4bdSJames Smart 
9532b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
954c8a4ce0bSDick Kennedy 	if (ctxp->ts_cmd_nvme) {
9552b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
9562b65e182SJames Smart 			ctxp->ts_nvme_status = ktime_get_ns();
9572b65e182SJames Smart 		else
9582b65e182SJames Smart 			ctxp->ts_nvme_data = ktime_get_ns();
9592b65e182SJames Smart 	}
9601fbf9742SJames Smart 
9611fbf9742SJames Smart 	/* Setup the hdw queue if not already set */
9621fbf9742SJames Smart 	if (!ctxp->hdwq)
9631fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[rsp->hwqid];
9641fbf9742SJames Smart 
9652b65e182SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
966d6d189ceSBart Van Assche 		int id = raw_smp_processor_id();
96763df6d63SJames Smart 		if (id < LPFC_CHECK_CPU_CNT) {
96863df6d63SJames Smart 			if (rsp->hwqid != id)
96963df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
9702b65e182SJames Smart 						"6705 CPU Check OP: "
9712b65e182SJames Smart 						"cpu %d expect %d\n",
9722b65e182SJames Smart 						id, rsp->hwqid);
97363df6d63SJames Smart 			phba->sli4_hba.hdwq[rsp->hwqid].cpucheck_xmt_io[id]++;
9742b65e182SJames Smart 		}
97563df6d63SJames Smart 		ctxp->cpu = id; /* Setup cpu for cmpl check */
9762b65e182SJames Smart 	}
9772b65e182SJames Smart #endif
978d613b6a7SJames Smart 
979d613b6a7SJames Smart 	/* Sanity check */
98086c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_ABTS_RCV) ||
98186c67379SJames Smart 	    (ctxp->state == LPFC_NVMET_STE_ABORT)) {
982d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
983d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
98479d8c4ceSJames Smart 				"6102 IO oxid x%x aborted\n",
985d613b6a7SJames Smart 				ctxp->oxid);
986a5068b46SJames Smart 		rc = -ENXIO;
987d613b6a7SJames Smart 		goto aerr;
988d613b6a7SJames Smart 	}
989d613b6a7SJames Smart 
990d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
991d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
992d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
993d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
994d613b6a7SJames Smart 				"6152 FCP Drop IO x%x: Prep\n",
995d613b6a7SJames Smart 				ctxp->oxid);
996a5068b46SJames Smart 		rc = -ENXIO;
997d613b6a7SJames Smart 		goto aerr;
998d613b6a7SJames Smart 	}
999d613b6a7SJames Smart 
1000d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
1001d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
1002d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
1003d613b6a7SJames Smart 	nvmewqeq->iocb_flag |=  LPFC_IO_NVMET;
1004d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
1005d613b6a7SJames Smart 
10062b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
10072b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
10082b65e182SJames Smart 
100961f3d4bfSJames Smart 	ctxp->flag |= LPFC_NVMET_IO_INP;
10101fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
1011d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
10122b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1013c8a4ce0bSDick Kennedy 		if (!ctxp->ts_cmd_nvme)
10142b65e182SJames Smart 			return 0;
10152b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
10162b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
10172b65e182SJames Smart 		else
10182b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
10192b65e182SJames Smart #endif
1020d613b6a7SJames Smart 		return 0;
1021d613b6a7SJames Smart 	}
1022d613b6a7SJames Smart 
10236e8e1c14SJames Smart 	if (rc == -EBUSY) {
10246e8e1c14SJames Smart 		/*
10256e8e1c14SJames Smart 		 * WQ was full, so queue nvmewqeq to be sent after
10266e8e1c14SJames Smart 		 * WQE release CQE
10276e8e1c14SJames Smart 		 */
10286e8e1c14SJames Smart 		ctxp->flag |= LPFC_NVMET_DEFER_WQFULL;
1029c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
10306e8e1c14SJames Smart 		pring = wq->pring;
10316e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
10326e8e1c14SJames Smart 		list_add_tail(&nvmewqeq->list, &wq->wqfull_list);
10336e8e1c14SJames Smart 		wq->q_flag |= HBA_NVMET_WQFULL;
10346e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
1035411de511SJames Smart 		atomic_inc(&lpfc_nvmep->defer_wqfull);
10366e8e1c14SJames Smart 		return 0;
10376e8e1c14SJames Smart 	}
10386e8e1c14SJames Smart 
1039d613b6a7SJames Smart 	/* Give back resources */
1040d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1041d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1042d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
1043d613b6a7SJames Smart 			ctxp->oxid, rc);
1044d613b6a7SJames Smart 
1045d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
1046d613b6a7SJames Smart 	nvmewqeq->context2 = NULL;
1047d613b6a7SJames Smart 	nvmewqeq->context3 = NULL;
1048a5068b46SJames Smart 	rc = -EBUSY;
1049d613b6a7SJames Smart aerr:
1050a5068b46SJames Smart 	return rc;
1051d613b6a7SJames Smart }
1052d613b6a7SJames Smart 
1053d613b6a7SJames Smart static void
1054d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
1055d613b6a7SJames Smart {
1056d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
1057d613b6a7SJames Smart 
1058d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
1059c41f5988SEwan D. Milne 	if (tport->phba->targetport)
1060c41f5988SEwan D. Milne 		complete(tport->tport_unreg_cmp);
1061d613b6a7SJames Smart }
1062d613b6a7SJames Smart 
106319b58d94SJames Smart static void
1064a97ec51bSJames Smart lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
1065a97ec51bSJames Smart 			 struct nvmefc_tgt_fcp_req *req)
1066a97ec51bSJames Smart {
1067a97ec51bSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
1068a97ec51bSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
1069a97ec51bSJames Smart 		container_of(req, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
1070a97ec51bSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
10716e8e1c14SJames Smart 	struct lpfc_queue *wq;
107286c67379SJames Smart 	unsigned long flags;
1073a97ec51bSJames Smart 
10741abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
10751abcb371SDick Kennedy 		return;
10761abcb371SDick Kennedy 
10773386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
10783386f4bdSJames Smart 		return;
10793386f4bdSJames Smart 
10801fbf9742SJames Smart 	if (!ctxp->hdwq)
10811fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[0];
10821fbf9742SJames Smart 
1083a97ec51bSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
108479d8c4ceSJames Smart 			"6103 NVMET Abort op: oxid x%x flg x%x ste %d\n",
1085ce1b591cSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
1086a97ec51bSJames Smart 
1087ce1b591cSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x flg x%x ste x%x\n",
1088ce1b591cSJames Smart 			 ctxp->oxid, ctxp->flag, ctxp->state);
1089a97ec51bSJames Smart 
1090a97ec51bSJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
1091ce1b591cSJames Smart 
109286c67379SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
109386c67379SJames Smart 
109486c67379SJames Smart 	/* Since iaab/iaar are NOT set, we need to check
109586c67379SJames Smart 	 * if the firmware is in process of aborting IO
109686c67379SJames Smart 	 */
109779d8c4ceSJames Smart 	if (ctxp->flag & (LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP)) {
109886c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
109986c67379SJames Smart 		return;
110086c67379SJames Smart 	}
1101a97ec51bSJames Smart 	ctxp->flag |= LPFC_NVMET_ABORT_OP;
1102ce1b591cSJames Smart 
11036e8e1c14SJames Smart 	if (ctxp->flag & LPFC_NVMET_DEFER_WQFULL) {
1104c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11056e8e1c14SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
11066e8e1c14SJames Smart 						 ctxp->oxid);
1107c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
11086e8e1c14SJames Smart 		lpfc_nvmet_wqfull_flush(phba, wq, ctxp);
11096e8e1c14SJames Smart 		return;
11106e8e1c14SJames Smart 	}
1111c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11126e8e1c14SJames Smart 
1113ce1b591cSJames Smart 	/* An state of LPFC_NVMET_STE_RCV means we have just received
1114ce1b591cSJames Smart 	 * the NVME command and have not started processing it.
1115ce1b591cSJames Smart 	 * (by issuing any IO WQEs on this exchange yet)
1116ce1b591cSJames Smart 	 */
1117ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_RCV)
1118ce1b591cSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1119a97ec51bSJames Smart 						 ctxp->oxid);
1120a97ec51bSJames Smart 	else
1121ce1b591cSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1122a97ec51bSJames Smart 					       ctxp->oxid);
1123a97ec51bSJames Smart }
1124a97ec51bSJames Smart 
1125a97ec51bSJames Smart static void
112619b58d94SJames Smart lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
112719b58d94SJames Smart 			   struct nvmefc_tgt_fcp_req *rsp)
112819b58d94SJames Smart {
1129547077a4SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
113019b58d94SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
113119b58d94SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
113219b58d94SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
113319b58d94SJames Smart 	unsigned long flags;
113419b58d94SJames Smart 	bool aborting = false;
113519b58d94SJames Smart 
1136c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
1137c160c0f8SJames Smart 	if (ctxp->flag & LPFC_NVMET_XBUSY)
1138c160c0f8SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1139c160c0f8SJames Smart 				"6027 NVMET release with XBUSY flag x%x"
1140c160c0f8SJames Smart 				" oxid x%x\n",
1141c160c0f8SJames Smart 				ctxp->flag, ctxp->oxid);
1142c160c0f8SJames Smart 	else if (ctxp->state != LPFC_NVMET_STE_DONE &&
1143c160c0f8SJames Smart 		 ctxp->state != LPFC_NVMET_STE_ABORT)
1144ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1145ce1b591cSJames Smart 				"6413 NVMET release bad state %d %d oxid x%x\n",
1146ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
1147ce1b591cSJames Smart 
114886c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_ABORT_OP) ||
114986c67379SJames Smart 	    (ctxp->flag & LPFC_NVMET_XBUSY)) {
115019b58d94SJames Smart 		aborting = true;
115186c67379SJames Smart 		/* let the abort path do the real release */
115286c67379SJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
115319b58d94SJames Smart 	}
115419b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
115519b58d94SJames Smart 
11569094367aSArnd Bergmann 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d abt %d\n", ctxp->oxid,
11579094367aSArnd Bergmann 			 ctxp->state, aborting);
115819b58d94SJames Smart 
1159547077a4SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_release);
116079d8c4ceSJames Smart 	ctxp->flag &= ~LPFC_NVMET_TNOTIFY;
1161547077a4SJames Smart 
116286c67379SJames Smart 	if (aborting)
116386c67379SJames Smart 		return;
116486c67379SJames Smart 
11656c621a22SJames Smart 	lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
116619b58d94SJames Smart }
116719b58d94SJames Smart 
116850738420SJames Smart static void
116950738420SJames Smart lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port *tgtport,
117050738420SJames Smart 		     struct nvmefc_tgt_fcp_req *rsp)
117150738420SJames Smart {
117250738420SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
117350738420SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
117450738420SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
117550738420SJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
117650738420SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
1177472e146dSJames Smart 	unsigned long iflag;
1178472e146dSJames Smart 
117950738420SJames Smart 
118050738420SJames Smart 	lpfc_nvmeio_data(phba, "NVMET DEFERRCV: xri x%x sz %d CPU %02x\n",
1181d6d189ceSBart Van Assche 			 ctxp->oxid, ctxp->size, raw_smp_processor_id());
118250738420SJames Smart 
11836871e814SJames Smart 	if (!nvmebuf) {
11846871e814SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
118579d8c4ceSJames Smart 				"6425 Defer rcv: no buffer oxid x%x: "
11866871e814SJames Smart 				"flg %x ste %x\n",
11876871e814SJames Smart 				ctxp->oxid, ctxp->flag, ctxp->state);
11886871e814SJames Smart 		return;
11896871e814SJames Smart 	}
11906871e814SJames Smart 
119150738420SJames Smart 	tgtp = phba->targetport->private;
11926871e814SJames Smart 	if (tgtp)
119350738420SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_defer);
1194411de511SJames Smart 
1195411de511SJames Smart 	/* Free the nvmebuf since a new buffer already replaced it */
1196cbc5de1bSJames Smart 	nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
1197472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflag);
1198472e146dSJames Smart 	ctxp->rqb_buffer = NULL;
1199472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
120050738420SJames Smart }
120150738420SJames Smart 
1202ab723121SJames Smart static void
1203ab723121SJames Smart lpfc_nvmet_discovery_event(struct nvmet_fc_target_port *tgtport)
1204ab723121SJames Smart {
1205ab723121SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1206ab723121SJames Smart 	struct lpfc_hba *phba;
1207ab723121SJames Smart 	uint32_t rc;
1208ab723121SJames Smart 
1209ab723121SJames Smart 	tgtp = tgtport->private;
1210ab723121SJames Smart 	phba = tgtp->phba;
1211ab723121SJames Smart 
1212ab723121SJames Smart 	rc = lpfc_issue_els_rscn(phba->pport, 0);
1213ab723121SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1214ab723121SJames Smart 			"6420 NVMET subsystem change: Notification %s\n",
1215ab723121SJames Smart 			(rc) ? "Failed" : "Sent");
1216ab723121SJames Smart }
1217ab723121SJames Smart 
1218d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
1219d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
1220d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
1221d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
1222a97ec51bSJames Smart 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
122319b58d94SJames Smart 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
122450738420SJames Smart 	.defer_rcv	= lpfc_nvmet_defer_rcv,
1225ab723121SJames Smart 	.discovery_event = lpfc_nvmet_discovery_event,
1226d613b6a7SJames Smart 
1227d613b6a7SJames Smart 	.max_hw_queues  = 1,
1228d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1229d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1230d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
1231d613b6a7SJames Smart 
1232d613b6a7SJames Smart 	/* optional features */
1233d613b6a7SJames Smart 	.target_features = 0,
1234d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
1235d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
1236d613b6a7SJames Smart };
1237d613b6a7SJames Smart 
123839aa23f9SColin Ian King static void
123966d7ce93SDick Kennedy __lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba *phba,
124066d7ce93SDick Kennedy 		struct lpfc_nvmet_ctx_info *infop)
12416c621a22SJames Smart {
12426c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf, *next_ctx_buf;
12436c621a22SJames Smart 	unsigned long flags;
12446c621a22SJames Smart 
124566d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, flags);
1246966bb5b7SJames Smart 	list_for_each_entry_safe(ctx_buf, next_ctx_buf,
124766d7ce93SDick Kennedy 				&infop->nvmet_ctx_list, list) {
12485e5b511dSJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
12496c621a22SJames Smart 		list_del_init(&ctx_buf->list);
12505e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
125166d7ce93SDick Kennedy 
125266d7ce93SDick Kennedy 		__lpfc_clear_active_sglq(phba, ctx_buf->sglq->sli4_lxritag);
12536c621a22SJames Smart 		ctx_buf->sglq->state = SGL_FREED;
12546c621a22SJames Smart 		ctx_buf->sglq->ndlp = NULL;
12556c621a22SJames Smart 
1256c4031db7SDan Carpenter 		spin_lock(&phba->sli4_hba.sgl_list_lock);
12576c621a22SJames Smart 		list_add_tail(&ctx_buf->sglq->list,
12586c621a22SJames Smart 				&phba->sli4_hba.lpfc_nvmet_sgl_list);
1259c4031db7SDan Carpenter 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
12606c621a22SJames Smart 
12616c621a22SJames Smart 		lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
12626c621a22SJames Smart 		kfree(ctx_buf->context);
12636c621a22SJames Smart 	}
126466d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, flags);
1265966bb5b7SJames Smart }
126666d7ce93SDick Kennedy 
126766d7ce93SDick Kennedy static void
126866d7ce93SDick Kennedy lpfc_nvmet_cleanup_io_context(struct lpfc_hba *phba)
126966d7ce93SDick Kennedy {
127066d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
127166d7ce93SDick Kennedy 	int i, j;
127266d7ce93SDick Kennedy 
127366d7ce93SDick Kennedy 	/* The first context list, MRQ 0 CPU 0 */
127466d7ce93SDick Kennedy 	infop = phba->sli4_hba.nvmet_ctx_info;
127566d7ce93SDick Kennedy 	if (!infop)
127666d7ce93SDick Kennedy 		return;
127766d7ce93SDick Kennedy 
127866d7ce93SDick Kennedy 	/* Cycle the the entire CPU context list for every MRQ */
127966d7ce93SDick Kennedy 	for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
1280222e9239SJames Smart 		for_each_present_cpu(j) {
1281222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, j, i);
128266d7ce93SDick Kennedy 			__lpfc_nvmet_clean_io_for_cpu(phba, infop);
128366d7ce93SDick Kennedy 		}
128466d7ce93SDick Kennedy 	}
128566d7ce93SDick Kennedy 	kfree(phba->sli4_hba.nvmet_ctx_info);
128666d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = NULL;
12876c621a22SJames Smart }
12886c621a22SJames Smart 
128939aa23f9SColin Ian King static int
12906c621a22SJames Smart lpfc_nvmet_setup_io_context(struct lpfc_hba *phba)
12916c621a22SJames Smart {
12926c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
12936c621a22SJames Smart 	struct lpfc_iocbq *nvmewqe;
12946c621a22SJames Smart 	union lpfc_wqe128 *wqe;
129566d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *last_infop;
129666d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
1297222e9239SJames Smart 	int i, j, idx, cpu;
12986c621a22SJames Smart 
12996c621a22SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
13006c621a22SJames Smart 			"6403 Allocate NVMET resources for %d XRIs\n",
13016c621a22SJames Smart 			phba->sli4_hba.nvmet_xri_cnt);
13026c621a22SJames Smart 
130366d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = kcalloc(
1304222e9239SJames Smart 		phba->sli4_hba.num_possible_cpu * phba->cfg_nvmet_mrq,
130566d7ce93SDick Kennedy 		sizeof(struct lpfc_nvmet_ctx_info), GFP_KERNEL);
130666d7ce93SDick Kennedy 	if (!phba->sli4_hba.nvmet_ctx_info) {
130766d7ce93SDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
130866d7ce93SDick Kennedy 				"6419 Failed allocate memory for "
130966d7ce93SDick Kennedy 				"nvmet context lists\n");
131066d7ce93SDick Kennedy 		return -ENOMEM;
131166d7ce93SDick Kennedy 	}
131266d7ce93SDick Kennedy 
131366d7ce93SDick Kennedy 	/*
131466d7ce93SDick Kennedy 	 * Assuming X CPUs in the system, and Y MRQs, allocate some
131566d7ce93SDick Kennedy 	 * lpfc_nvmet_ctx_info structures as follows:
131666d7ce93SDick Kennedy 	 *
131766d7ce93SDick Kennedy 	 * cpu0/mrq0 cpu1/mrq0 ... cpuX/mrq0
131866d7ce93SDick Kennedy 	 * cpu0/mrq1 cpu1/mrq1 ... cpuX/mrq1
131966d7ce93SDick Kennedy 	 * ...
132066d7ce93SDick Kennedy 	 * cpuX/mrqY cpuX/mrqY ... cpuX/mrqY
132166d7ce93SDick Kennedy 	 *
132266d7ce93SDick Kennedy 	 * Each line represents a MRQ "silo" containing an entry for
132366d7ce93SDick Kennedy 	 * every CPU.
132466d7ce93SDick Kennedy 	 *
132566d7ce93SDick Kennedy 	 * MRQ X is initially assumed to be associated with CPU X, thus
132666d7ce93SDick Kennedy 	 * contexts are initially distributed across all MRQs using
132766d7ce93SDick Kennedy 	 * the MRQ index (N) as follows cpuN/mrqN. When contexts are
132866d7ce93SDick Kennedy 	 * freed, the are freed to the MRQ silo based on the CPU number
132966d7ce93SDick Kennedy 	 * of the IO completion. Thus a context that was allocated for MRQ A
133066d7ce93SDick Kennedy 	 * whose IO completed on CPU B will be freed to cpuB/mrqA.
133166d7ce93SDick Kennedy 	 */
1332222e9239SJames Smart 	for_each_possible_cpu(i) {
133366d7ce93SDick Kennedy 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1334222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
133566d7ce93SDick Kennedy 			INIT_LIST_HEAD(&infop->nvmet_ctx_list);
133666d7ce93SDick Kennedy 			spin_lock_init(&infop->nvmet_ctx_list_lock);
133766d7ce93SDick Kennedy 			infop->nvmet_ctx_list_cnt = 0;
133866d7ce93SDick Kennedy 		}
133966d7ce93SDick Kennedy 	}
134066d7ce93SDick Kennedy 
134166d7ce93SDick Kennedy 	/*
134266d7ce93SDick Kennedy 	 * Setup the next CPU context info ptr for each MRQ.
134366d7ce93SDick Kennedy 	 * MRQ 0 will cycle thru CPUs 0 - X separately from
134466d7ce93SDick Kennedy 	 * MRQ 1 cycling thru CPUs 0 - X, and so on.
134566d7ce93SDick Kennedy 	 */
134666d7ce93SDick Kennedy 	for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1347222e9239SJames Smart 		last_infop = lpfc_get_ctx_list(phba,
1348222e9239SJames Smart 					       cpumask_first(cpu_present_mask),
1349222e9239SJames Smart 					       j);
1350222e9239SJames Smart 		for (i = phba->sli4_hba.num_possible_cpu - 1;  i >= 0; i--) {
135166d7ce93SDick Kennedy 			infop = lpfc_get_ctx_list(phba, i, j);
135266d7ce93SDick Kennedy 			infop->nvmet_ctx_next_cpu = last_infop;
135366d7ce93SDick Kennedy 			last_infop = infop;
135466d7ce93SDick Kennedy 		}
135566d7ce93SDick Kennedy 	}
135666d7ce93SDick Kennedy 
13576c621a22SJames Smart 	/* For all nvmet xris, allocate resources needed to process a
13586c621a22SJames Smart 	 * received command on a per xri basis.
13596c621a22SJames Smart 	 */
136066d7ce93SDick Kennedy 	idx = 0;
1361222e9239SJames Smart 	cpu = cpumask_first(cpu_present_mask);
13626c621a22SJames Smart 	for (i = 0; i < phba->sli4_hba.nvmet_xri_cnt; i++) {
13636c621a22SJames Smart 		ctx_buf = kzalloc(sizeof(*ctx_buf), GFP_KERNEL);
13646c621a22SJames Smart 		if (!ctx_buf) {
13656c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13666c621a22SJames Smart 					"6404 Ran out of memory for NVMET\n");
13676c621a22SJames Smart 			return -ENOMEM;
13686c621a22SJames Smart 		}
13696c621a22SJames Smart 
13706c621a22SJames Smart 		ctx_buf->context = kzalloc(sizeof(*ctx_buf->context),
13716c621a22SJames Smart 					   GFP_KERNEL);
13726c621a22SJames Smart 		if (!ctx_buf->context) {
13736c621a22SJames Smart 			kfree(ctx_buf);
13746c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13756c621a22SJames Smart 					"6405 Ran out of NVMET "
13766c621a22SJames Smart 					"context memory\n");
13776c621a22SJames Smart 			return -ENOMEM;
13786c621a22SJames Smart 		}
13796c621a22SJames Smart 		ctx_buf->context->ctxbuf = ctx_buf;
1380ce1b591cSJames Smart 		ctx_buf->context->state = LPFC_NVMET_STE_FREE;
13816c621a22SJames Smart 
13826c621a22SJames Smart 		ctx_buf->iocbq = lpfc_sli_get_iocbq(phba);
13836c621a22SJames Smart 		if (!ctx_buf->iocbq) {
13846c621a22SJames Smart 			kfree(ctx_buf->context);
13856c621a22SJames Smart 			kfree(ctx_buf);
13866c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13876c621a22SJames Smart 					"6406 Ran out of NVMET iocb/WQEs\n");
13886c621a22SJames Smart 			return -ENOMEM;
13896c621a22SJames Smart 		}
13906c621a22SJames Smart 		ctx_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
13916c621a22SJames Smart 		nvmewqe = ctx_buf->iocbq;
1392205e8240SJames Smart 		wqe = &nvmewqe->wqe;
1393bd3061baSJames Smart 
13946c621a22SJames Smart 		/* Initialize WQE */
13956c621a22SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
13966c621a22SJames Smart 
13976c621a22SJames Smart 		ctx_buf->iocbq->context1 = NULL;
13986c621a22SJames Smart 		spin_lock(&phba->sli4_hba.sgl_list_lock);
13996c621a22SJames Smart 		ctx_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, ctx_buf->iocbq);
14006c621a22SJames Smart 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
14016c621a22SJames Smart 		if (!ctx_buf->sglq) {
14026c621a22SJames Smart 			lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
14036c621a22SJames Smart 			kfree(ctx_buf->context);
14046c621a22SJames Smart 			kfree(ctx_buf);
14056c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
14066c621a22SJames Smart 					"6407 Ran out of NVMET XRIs\n");
14076c621a22SJames Smart 			return -ENOMEM;
14086c621a22SJames Smart 		}
1409472e146dSJames Smart 		INIT_WORK(&ctx_buf->defer_work, lpfc_nvmet_fcp_rqst_defer_work);
141066d7ce93SDick Kennedy 
141166d7ce93SDick Kennedy 		/*
141266d7ce93SDick Kennedy 		 * Add ctx to MRQidx context list. Our initial assumption
141366d7ce93SDick Kennedy 		 * is MRQidx will be associated with CPUidx. This association
141466d7ce93SDick Kennedy 		 * can change on the fly.
141566d7ce93SDick Kennedy 		 */
1416222e9239SJames Smart 		infop = lpfc_get_ctx_list(phba, cpu, idx);
141766d7ce93SDick Kennedy 		spin_lock(&infop->nvmet_ctx_list_lock);
141866d7ce93SDick Kennedy 		list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
141966d7ce93SDick Kennedy 		infop->nvmet_ctx_list_cnt++;
142066d7ce93SDick Kennedy 		spin_unlock(&infop->nvmet_ctx_list_lock);
142166d7ce93SDick Kennedy 
142266d7ce93SDick Kennedy 		/* Spread ctx structures evenly across all MRQs */
142366d7ce93SDick Kennedy 		idx++;
1424222e9239SJames Smart 		if (idx >= phba->cfg_nvmet_mrq) {
142566d7ce93SDick Kennedy 			idx = 0;
1426222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1427222e9239SJames Smart 			continue;
1428222e9239SJames Smart 		}
1429222e9239SJames Smart 		cpu = cpumask_next(cpu, cpu_present_mask);
1430222e9239SJames Smart 		if (cpu == nr_cpu_ids)
1431222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1432222e9239SJames Smart 
14336c621a22SJames Smart 	}
143466d7ce93SDick Kennedy 
1435222e9239SJames Smart 	for_each_present_cpu(i) {
14362879265fSJames Smart 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
14372879265fSJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
143866d7ce93SDick Kennedy 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
143966d7ce93SDick Kennedy 					"6408 TOTAL NVMET ctx for CPU %d "
144032350664SJames Smart 					"MRQ %d: cnt %d nextcpu x%px\n",
144166d7ce93SDick Kennedy 					i, j, infop->nvmet_ctx_list_cnt,
144266d7ce93SDick Kennedy 					infop->nvmet_ctx_next_cpu);
144366d7ce93SDick Kennedy 		}
144466d7ce93SDick Kennedy 	}
14456c621a22SJames Smart 	return 0;
14466c621a22SJames Smart }
14476c621a22SJames Smart 
1448d613b6a7SJames Smart int
1449d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1450d613b6a7SJames Smart {
1451d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1452d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1453d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
14546c621a22SJames Smart 	int error;
1455d613b6a7SJames Smart 
1456d613b6a7SJames Smart 	if (phba->targetport)
1457d613b6a7SJames Smart 		return 0;
1458d613b6a7SJames Smart 
14596c621a22SJames Smart 	error = lpfc_nvmet_setup_io_context(phba);
14606c621a22SJames Smart 	if (error)
14616c621a22SJames Smart 		return error;
14626c621a22SJames Smart 
1463d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1464d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1465d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1466d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
1467d613b6a7SJames Smart 
14685b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
14695b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
14705b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
14714d4c4a4aSJames Smart 	 */
14724d4c4a4aSJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1473cdb42becSJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_hdw_queue;
1474f485c18dSDick Kennedy 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP;
1475d613b6a7SJames Smart 
14767d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1477d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1478d613b6a7SJames Smart 					     &phba->pcidev->dev,
1479d613b6a7SJames Smart 					     &phba->targetport);
1480166d7211SJames Smart #else
14816c621a22SJames Smart 	error = -ENOENT;
1482166d7211SJames Smart #endif
1483d613b6a7SJames Smart 	if (error) {
1484d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1485e7981a2cSDick Kennedy 				"6025 Cannot register NVME targetport x%x: "
1486e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1487e7981a2cSDick Kennedy 				error,
1488e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1489e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1490e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1491d613b6a7SJames Smart 		phba->targetport = NULL;
1492e7981a2cSDick Kennedy 		phba->nvmet_support = 0;
14936c621a22SJames Smart 
14946c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
14956c621a22SJames Smart 
1496d613b6a7SJames Smart 	} else {
1497d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
1498d613b6a7SJames Smart 			phba->targetport->private;
1499d613b6a7SJames Smart 		tgtp->phba = phba;
1500d613b6a7SJames Smart 
1501d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1502d613b6a7SJames Smart 				"6026 Registered NVME "
150332350664SJames Smart 				"targetport: x%px, private x%px "
1504e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1505d613b6a7SJames Smart 				phba->targetport, tgtp,
1506e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1507e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1508e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1509d613b6a7SJames Smart 
1510d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
1511d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
1512d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
1513d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
1514547077a4SJames Smart 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1515d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
1516d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
1517d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
15184b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_xb_set, 0);
15194b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_aborted, 0);
1520d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1521d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1522d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1523d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1524d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
1525d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1526d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
1527d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
1528d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
1529547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_release, 0);
1530d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1531d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
15324b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_xb_set, 0);
15334b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_aborted, 0);
1534d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
15354b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_xri_abort_cqe, 0);
1536547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
1537547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1538547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_unsol, 0);
1539547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_sol, 0);
1540d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
1541d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1542411de511SJames Smart 		atomic_set(&tgtp->defer_ctx, 0);
1543411de511SJames Smart 		atomic_set(&tgtp->defer_fod, 0);
1544411de511SJames Smart 		atomic_set(&tgtp->defer_wqfull, 0);
1545d613b6a7SJames Smart 	}
1546d613b6a7SJames Smart 	return error;
1547d613b6a7SJames Smart }
1548d613b6a7SJames Smart 
1549d613b6a7SJames Smart int
1550d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1551d613b6a7SJames Smart {
1552d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1553d613b6a7SJames Smart 
1554d613b6a7SJames Smart 	if (!phba->targetport)
1555d613b6a7SJames Smart 		return 0;
1556d613b6a7SJames Smart 
1557d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
155832350664SJames Smart 			 "6007 Update NVMET port x%px did x%x\n",
1559d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
1560d613b6a7SJames Smart 
1561d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
1562d613b6a7SJames Smart 	return 0;
1563d613b6a7SJames Smart }
1564d613b6a7SJames Smart 
1565318083adSJames Smart /**
1566318083adSJames Smart  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1567318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
1568318083adSJames Smart  * @axri: pointer to the nvmet xri abort wcqe structure.
1569318083adSJames Smart  *
1570318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
1571318083adSJames Smart  * NVMET aborted xri.
1572318083adSJames Smart  **/
1573318083adSJames Smart void
1574318083adSJames Smart lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
1575318083adSJames Smart 			    struct sli4_wcqe_xri_aborted *axri)
1576318083adSJames Smart {
157732b93865SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
157886c67379SJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
157986c67379SJames Smart 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
158086c67379SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
15814b056682SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
158279d8c4ceSJames Smart 	struct nvmefc_tgt_fcp_req *req = NULL;
158386c67379SJames Smart 	struct lpfc_nodelist *ndlp;
158486c67379SJames Smart 	unsigned long iflag = 0;
158586c67379SJames Smart 	int rrq_empty = 0;
158686c67379SJames Smart 	bool released = false;
158786c67379SJames Smart 
158886c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
158986c67379SJames Smart 			"6317 XB aborted xri x%x rxid x%x\n", xri, rxid);
159086c67379SJames Smart 
159186c67379SJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
159286c67379SJames Smart 		return;
15934b056682SJames Smart 
15944b056682SJames Smart 	if (phba->targetport) {
15954b056682SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
15964b056682SJames Smart 		atomic_inc(&tgtp->xmt_fcp_xri_abort_cqe);
15974b056682SJames Smart 	}
15984b056682SJames Smart 
159986c67379SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
16005e5b511dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
160186c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
160286c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
160386c67379SJames Smart 				 list) {
16046c621a22SJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
160586c67379SJames Smart 			continue;
160686c67379SJames Smart 
1607c160c0f8SJames Smart 		spin_lock(&ctxp->ctxlock);
160886c67379SJames Smart 		/* Check if we already received a free context call
160986c67379SJames Smart 		 * and we have completed processing an abort situation.
161086c67379SJames Smart 		 */
161186c67379SJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_RLS &&
161286c67379SJames Smart 		    !(ctxp->flag & LPFC_NVMET_ABORT_OP)) {
161379d8c4ceSJames Smart 			list_del_init(&ctxp->list);
161486c67379SJames Smart 			released = true;
161586c67379SJames Smart 		}
161686c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_XBUSY;
1617c160c0f8SJames Smart 		spin_unlock(&ctxp->ctxlock);
16185e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
161986c67379SJames Smart 
162086c67379SJames Smart 		rrq_empty = list_empty(&phba->active_rrq_list);
162186c67379SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, iflag);
162286c67379SJames Smart 		ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
162386c67379SJames Smart 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
162486c67379SJames Smart 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
162586c67379SJames Smart 		     ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
162686c67379SJames Smart 			lpfc_set_rrq_active(phba, ndlp,
16276c621a22SJames Smart 				ctxp->ctxbuf->sglq->sli4_lxritag,
162886c67379SJames Smart 				rxid, 1);
162986c67379SJames Smart 			lpfc_sli4_abts_err_handler(phba, ndlp, axri);
163086c67379SJames Smart 		}
163186c67379SJames Smart 
163286c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
163379d8c4ceSJames Smart 				"6318 XB aborted oxid x%x flg x%x (%x)\n",
163486c67379SJames Smart 				ctxp->oxid, ctxp->flag, released);
163586c67379SJames Smart 		if (released)
16366c621a22SJames Smart 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
16376c621a22SJames Smart 
163886c67379SJames Smart 		if (rrq_empty)
163986c67379SJames Smart 			lpfc_worker_wake_up(phba);
164086c67379SJames Smart 		return;
164186c67379SJames Smart 	}
16425e5b511dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
164386c67379SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
164479d8c4ceSJames Smart 
164579d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_xri(phba, xri);
164679d8c4ceSJames Smart 	if (ctxp) {
164779d8c4ceSJames Smart 		/*
164879d8c4ceSJames Smart 		 *  Abort already done by FW, so BA_ACC sent.
164979d8c4ceSJames Smart 		 *  However, the transport may be unaware.
165079d8c4ceSJames Smart 		 */
165179d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
165279d8c4ceSJames Smart 				"6323 NVMET Rcv ABTS xri x%x ctxp state x%x "
165379d8c4ceSJames Smart 				"flag x%x oxid x%x rxid x%x\n",
165479d8c4ceSJames Smart 				xri, ctxp->state, ctxp->flag, ctxp->oxid,
165579d8c4ceSJames Smart 				rxid);
165679d8c4ceSJames Smart 
165779d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
165879d8c4ceSJames Smart 		ctxp->flag |= LPFC_NVMET_ABTS_RCV;
165979d8c4ceSJames Smart 		ctxp->state = LPFC_NVMET_STE_ABORT;
166079d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
166179d8c4ceSJames Smart 
166279d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
166379d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
166401d53c04SJames Smart 				 xri, raw_smp_processor_id(), 0);
166579d8c4ceSJames Smart 
166679d8c4ceSJames Smart 		req = &ctxp->ctx.fcp_req;
166779d8c4ceSJames Smart 		if (req)
166879d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport, req);
166979d8c4ceSJames Smart 	}
167032b93865SJames Smart #endif
167186c67379SJames Smart }
167286c67379SJames Smart 
167386c67379SJames Smart int
167486c67379SJames Smart lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport *vport,
167586c67379SJames Smart 			   struct fc_frame_header *fc_hdr)
167686c67379SJames Smart {
167786c67379SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
167886c67379SJames Smart 	struct lpfc_hba *phba = vport->phba;
167986c67379SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
168086c67379SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
168132b93865SJames Smart 	uint32_t sid;
168279d8c4ceSJames Smart 	uint16_t oxid, xri;
168386c67379SJames Smart 	unsigned long iflag = 0;
168486c67379SJames Smart 
168532b93865SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
168679d8c4ceSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
168786c67379SJames Smart 
168886c67379SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
16895e5b511dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
169086c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
169186c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
169286c67379SJames Smart 				 list) {
169379d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
169486c67379SJames Smart 			continue;
169586c67379SJames Smart 
169632b93865SJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
169732b93865SJames Smart 
16985e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
169986c67379SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, iflag);
170086c67379SJames Smart 
170186c67379SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
170286c67379SJames Smart 		ctxp->flag |= LPFC_NVMET_ABTS_RCV;
170386c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
170486c67379SJames Smart 
170586c67379SJames Smart 		lpfc_nvmeio_data(phba,
170686c67379SJames Smart 			"NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1707d6d189ceSBart Van Assche 			xri, raw_smp_processor_id(), 0);
170886c67379SJames Smart 
170986c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
171086c67379SJames Smart 				"6319 NVMET Rcv ABTS:acc xri x%x\n", xri);
171186c67379SJames Smart 
171286c67379SJames Smart 		rsp = &ctxp->ctx.fcp_req;
171386c67379SJames Smart 		nvmet_fc_rcv_fcp_abort(phba->targetport, rsp);
171486c67379SJames Smart 
171586c67379SJames Smart 		/* Respond with BA_ACC accordingly */
171686c67379SJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
171786c67379SJames Smart 		return 0;
171886c67379SJames Smart 	}
17195e5b511dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
172086c67379SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
172186c67379SJames Smart 
172279d8c4ceSJames Smart 	/* check the wait list */
172379d8c4ceSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
172479d8c4ceSJames Smart 		struct rqb_dmabuf *nvmebuf;
172579d8c4ceSJames Smart 		struct fc_frame_header *fc_hdr_tmp;
172679d8c4ceSJames Smart 		u32 sid_tmp;
172779d8c4ceSJames Smart 		u16 oxid_tmp;
172879d8c4ceSJames Smart 		bool found = false;
172979d8c4ceSJames Smart 
173079d8c4ceSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
173179d8c4ceSJames Smart 
173279d8c4ceSJames Smart 		/* match by oxid and s_id */
173379d8c4ceSJames Smart 		list_for_each_entry(nvmebuf,
173479d8c4ceSJames Smart 				    &phba->sli4_hba.lpfc_nvmet_io_wait_list,
173579d8c4ceSJames Smart 				    hbuf.list) {
173679d8c4ceSJames Smart 			fc_hdr_tmp = (struct fc_frame_header *)
173779d8c4ceSJames Smart 					(nvmebuf->hbuf.virt);
173879d8c4ceSJames Smart 			oxid_tmp = be16_to_cpu(fc_hdr_tmp->fh_ox_id);
173979d8c4ceSJames Smart 			sid_tmp = sli4_sid_from_fc_hdr(fc_hdr_tmp);
174079d8c4ceSJames Smart 			if (oxid_tmp != oxid || sid_tmp != sid)
174179d8c4ceSJames Smart 				continue;
174286c67379SJames Smart 
174386c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
174479d8c4ceSJames Smart 					"6321 NVMET Rcv ABTS oxid x%x from x%x "
174579d8c4ceSJames Smart 					"is waiting for a ctxp\n",
174679d8c4ceSJames Smart 					oxid, sid);
174779d8c4ceSJames Smart 
174879d8c4ceSJames Smart 			list_del_init(&nvmebuf->hbuf.list);
174979d8c4ceSJames Smart 			phba->sli4_hba.nvmet_io_wait_cnt--;
175079d8c4ceSJames Smart 			found = true;
175179d8c4ceSJames Smart 			break;
175279d8c4ceSJames Smart 		}
175379d8c4ceSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
175479d8c4ceSJames Smart 				       iflag);
175579d8c4ceSJames Smart 
175679d8c4ceSJames Smart 		/* free buffer since already posted a new DMA buffer to RQ */
175779d8c4ceSJames Smart 		if (found) {
175879d8c4ceSJames Smart 			nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
175979d8c4ceSJames Smart 			/* Respond with BA_ACC accordingly */
176079d8c4ceSJames Smart 			lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
176179d8c4ceSJames Smart 			return 0;
176279d8c4ceSJames Smart 		}
176379d8c4ceSJames Smart 	}
176479d8c4ceSJames Smart 
176579d8c4ceSJames Smart 	/* check active list */
176679d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_oxid(phba, oxid, sid);
176779d8c4ceSJames Smart 	if (ctxp) {
176879d8c4ceSJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
176979d8c4ceSJames Smart 
177079d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
177179d8c4ceSJames Smart 		ctxp->flag |= (LPFC_NVMET_ABTS_RCV | LPFC_NVMET_ABORT_OP);
177279d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
177379d8c4ceSJames Smart 
177479d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
177579d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
177679d8c4ceSJames Smart 				 xri, raw_smp_processor_id(), 0);
177779d8c4ceSJames Smart 
177879d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
177979d8c4ceSJames Smart 				"6322 NVMET Rcv ABTS:acc oxid x%x xri x%x "
178079d8c4ceSJames Smart 				"flag x%x state x%x\n",
178179d8c4ceSJames Smart 				ctxp->oxid, xri, ctxp->flag, ctxp->state);
178279d8c4ceSJames Smart 
178379d8c4ceSJames Smart 		if (ctxp->flag & LPFC_NVMET_TNOTIFY) {
178479d8c4ceSJames Smart 			/* Notify the transport */
178579d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport,
178679d8c4ceSJames Smart 					       &ctxp->ctx.fcp_req);
178779d8c4ceSJames Smart 		} else {
17886594d31bSJames Smart 			cancel_work_sync(&ctxp->ctxbuf->defer_work);
178979d8c4ceSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
179079d8c4ceSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
179179d8c4ceSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
179279d8c4ceSJames Smart 		}
179379d8c4ceSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
179479d8c4ceSJames Smart 					       ctxp->oxid);
179579d8c4ceSJames Smart 
17966594d31bSJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
179779d8c4ceSJames Smart 		return 0;
179879d8c4ceSJames Smart 	}
179979d8c4ceSJames Smart 
180079d8c4ceSJames Smart 	lpfc_nvmeio_data(phba, "NVMET ABTS RCV: oxid x%x CPU %02x rjt %d\n",
180179d8c4ceSJames Smart 			 oxid, raw_smp_processor_id(), 1);
180279d8c4ceSJames Smart 
180379d8c4ceSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
180479d8c4ceSJames Smart 			"6320 NVMET Rcv ABTS:rjt oxid x%x\n", oxid);
180586c67379SJames Smart 
180686c67379SJames Smart 	/* Respond with BA_RJT accordingly */
180786c67379SJames Smart 	lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 0);
180886c67379SJames Smart #endif
1809b06e13c3SJens Axboe 	return 0;
1810318083adSJames Smart }
1811318083adSJames Smart 
18126e8e1c14SJames Smart static void
18136e8e1c14SJames Smart lpfc_nvmet_wqfull_flush(struct lpfc_hba *phba, struct lpfc_queue *wq,
18146e8e1c14SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
18156e8e1c14SJames Smart {
18166e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
18176e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
18186e8e1c14SJames Smart 	struct lpfc_iocbq *next_nvmewqeq;
18196e8e1c14SJames Smart 	unsigned long iflags;
18206e8e1c14SJames Smart 	struct lpfc_wcqe_complete wcqe;
18216e8e1c14SJames Smart 	struct lpfc_wcqe_complete *wcqep;
18226e8e1c14SJames Smart 
18236e8e1c14SJames Smart 	pring = wq->pring;
18246e8e1c14SJames Smart 	wcqep = &wcqe;
18256e8e1c14SJames Smart 
18266e8e1c14SJames Smart 	/* Fake an ABORT error code back to cmpl routine */
18276e8e1c14SJames Smart 	memset(wcqep, 0, sizeof(struct lpfc_wcqe_complete));
18286e8e1c14SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, IOSTAT_LOCAL_REJECT);
18296e8e1c14SJames Smart 	wcqep->parameter = IOERR_ABORT_REQUESTED;
18306e8e1c14SJames Smart 
18316e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
18326e8e1c14SJames Smart 	list_for_each_entry_safe(nvmewqeq, next_nvmewqeq,
18336e8e1c14SJames Smart 				 &wq->wqfull_list, list) {
18346e8e1c14SJames Smart 		if (ctxp) {
18356e8e1c14SJames Smart 			/* Checking for a specific IO to flush */
18366e8e1c14SJames Smart 			if (nvmewqeq->context2 == ctxp) {
18376e8e1c14SJames Smart 				list_del(&nvmewqeq->list);
18386e8e1c14SJames Smart 				spin_unlock_irqrestore(&pring->ring_lock,
18396e8e1c14SJames Smart 						       iflags);
18406e8e1c14SJames Smart 				lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq,
18416e8e1c14SJames Smart 							  wcqep);
18426e8e1c14SJames Smart 				return;
18436e8e1c14SJames Smart 			}
18446e8e1c14SJames Smart 			continue;
18456e8e1c14SJames Smart 		} else {
18466e8e1c14SJames Smart 			/* Flush all IOs */
18476e8e1c14SJames Smart 			list_del(&nvmewqeq->list);
18486e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
18496e8e1c14SJames Smart 			lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq, wcqep);
18506e8e1c14SJames Smart 			spin_lock_irqsave(&pring->ring_lock, iflags);
18516e8e1c14SJames Smart 		}
18526e8e1c14SJames Smart 	}
18536e8e1c14SJames Smart 	if (!ctxp)
18546e8e1c14SJames Smart 		wq->q_flag &= ~HBA_NVMET_WQFULL;
18556e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
18566e8e1c14SJames Smart }
18576e8e1c14SJames Smart 
18586e8e1c14SJames Smart void
18596e8e1c14SJames Smart lpfc_nvmet_wqfull_process(struct lpfc_hba *phba,
18606e8e1c14SJames Smart 			  struct lpfc_queue *wq)
18616e8e1c14SJames Smart {
18626e8e1c14SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
18636e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
18646e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
18651fbf9742SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
18666e8e1c14SJames Smart 	unsigned long iflags;
18676e8e1c14SJames Smart 	int rc;
18686e8e1c14SJames Smart 
18696e8e1c14SJames Smart 	/*
18706e8e1c14SJames Smart 	 * Some WQE slots are available, so try to re-issue anything
18716e8e1c14SJames Smart 	 * on the WQ wqfull_list.
18726e8e1c14SJames Smart 	 */
18736e8e1c14SJames Smart 	pring = wq->pring;
18746e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
18756e8e1c14SJames Smart 	while (!list_empty(&wq->wqfull_list)) {
18766e8e1c14SJames Smart 		list_remove_head(&wq->wqfull_list, nvmewqeq, struct lpfc_iocbq,
18776e8e1c14SJames Smart 				 list);
18786e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
18791fbf9742SJames Smart 		ctxp = (struct lpfc_nvmet_rcv_ctx *)nvmewqeq->context2;
18801fbf9742SJames Smart 		rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
18816e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
18826e8e1c14SJames Smart 		if (rc == -EBUSY) {
18836e8e1c14SJames Smart 			/* WQ was full again, so put it back on the list */
18846e8e1c14SJames Smart 			list_add(&nvmewqeq->list, &wq->wqfull_list);
18856e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
18866e8e1c14SJames Smart 			return;
18876e8e1c14SJames Smart 		}
188879d8c4ceSJames Smart 		if (rc == WQE_SUCCESS) {
188979d8c4ceSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
189079d8c4ceSJames Smart 			if (ctxp->ts_cmd_nvme) {
189179d8c4ceSJames Smart 				if (ctxp->ctx.fcp_req.op == NVMET_FCOP_RSP)
189279d8c4ceSJames Smart 					ctxp->ts_status_wqput = ktime_get_ns();
189379d8c4ceSJames Smart 				else
189479d8c4ceSJames Smart 					ctxp->ts_data_wqput = ktime_get_ns();
189579d8c4ceSJames Smart 			}
189679d8c4ceSJames Smart #endif
189779d8c4ceSJames Smart 		} else {
189879d8c4ceSJames Smart 			WARN_ON(rc);
189979d8c4ceSJames Smart 		}
19006e8e1c14SJames Smart 	}
19016e8e1c14SJames Smart 	wq->q_flag &= ~HBA_NVMET_WQFULL;
19026e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
19036e8e1c14SJames Smart 
19046e8e1c14SJames Smart #endif
19056e8e1c14SJames Smart }
19066e8e1c14SJames Smart 
1907d613b6a7SJames Smart void
1908d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
1909d613b6a7SJames Smart {
19107d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1911d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
19126e8e1c14SJames Smart 	struct lpfc_queue *wq;
19136e8e1c14SJames Smart 	uint32_t qidx;
1914c41f5988SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(tport_unreg_cmp);
1915d613b6a7SJames Smart 
1916d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
1917d613b6a7SJames Smart 		return;
1918d613b6a7SJames Smart 	if (phba->targetport) {
1919d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1920cdb42becSJames Smart 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
1921c00f62e6SJames Smart 			wq = phba->sli4_hba.hdwq[qidx].io_wq;
19226e8e1c14SJames Smart 			lpfc_nvmet_wqfull_flush(phba, wq, NULL);
19236e8e1c14SJames Smart 		}
1924c41f5988SEwan D. Milne 		tgtp->tport_unreg_cmp = &tport_unreg_cmp;
1925d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
19262a0fb340SJames Smart 		if (!wait_for_completion_timeout(tgtp->tport_unreg_cmp,
19272a0fb340SJames Smart 					msecs_to_jiffies(LPFC_NVMET_WAIT_TMO)))
19282a0fb340SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
192932350664SJames Smart 					"6179 Unreg targetport x%px timeout "
19302a0fb340SJames Smart 					"reached.\n", phba->targetport);
19316c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
1932d613b6a7SJames Smart 	}
1933d613b6a7SJames Smart 	phba->targetport = NULL;
1934166d7211SJames Smart #endif
1935d613b6a7SJames Smart }
1936d613b6a7SJames Smart 
1937d613b6a7SJames Smart /**
1938d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer - Process an unsolicited event data buffer
1939d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
1940d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
1941d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
1942d613b6a7SJames Smart  *
1943d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
1944d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
1945d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
1946d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
1947d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
1948d613b6a7SJames Smart  * of the discovery state machine.
1949d613b6a7SJames Smart  **/
1950d613b6a7SJames Smart static void
1951d613b6a7SJames Smart lpfc_nvmet_unsol_ls_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1952d613b6a7SJames Smart 			   struct hbq_dmabuf *nvmebuf)
1953d613b6a7SJames Smart {
19547d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1955d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1956d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
1957d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1958d613b6a7SJames Smart 	uint32_t *payload;
1959d613b6a7SJames Smart 	uint32_t size, oxid, sid, rc;
1960d613b6a7SJames Smart 
196168c9b55dSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
196268c9b55dSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
196368c9b55dSJames Smart 
1964cc74e31dSColin Ian King 	if (!phba->targetport) {
1965d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
196668c9b55dSJames Smart 				"6154 LS Drop IO x%x\n", oxid);
19672b65e182SJames Smart 		oxid = 0;
19682b65e182SJames Smart 		size = 0;
19692b65e182SJames Smart 		sid = 0;
1970547077a4SJames Smart 		ctxp = NULL;
1971d613b6a7SJames Smart 		goto dropit;
1972d613b6a7SJames Smart 	}
1973d613b6a7SJames Smart 
1974d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1975d613b6a7SJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
1976d613b6a7SJames Smart 	size = bf_get(lpfc_rcqe_length,  &nvmebuf->cq_event.cqe.rcqe_cmpl);
1977d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
1978d613b6a7SJames Smart 
1979d613b6a7SJames Smart 	ctxp = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx), GFP_ATOMIC);
1980d613b6a7SJames Smart 	if (ctxp == NULL) {
1981d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_drop);
1982d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1983d613b6a7SJames Smart 				"6155 LS Drop IO x%x: Alloc\n",
1984d613b6a7SJames Smart 				oxid);
1985d613b6a7SJames Smart dropit:
19862b65e182SJames Smart 		lpfc_nvmeio_data(phba, "NVMET LS  DROP: "
19872b65e182SJames Smart 				 "xri x%x sz %d from %06x\n",
19882b65e182SJames Smart 				 oxid, size, sid);
1989d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1990d613b6a7SJames Smart 		return;
1991d613b6a7SJames Smart 	}
1992d613b6a7SJames Smart 	ctxp->phba = phba;
1993d613b6a7SJames Smart 	ctxp->size = size;
1994d613b6a7SJames Smart 	ctxp->oxid = oxid;
1995d613b6a7SJames Smart 	ctxp->sid = sid;
1996d613b6a7SJames Smart 	ctxp->wqeq = NULL;
1997ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_LS_RCV;
1998ce1b591cSJames Smart 	ctxp->entry_cnt = 1;
1999d613b6a7SJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
20001fbf9742SJames Smart 	ctxp->hdwq = &phba->sli4_hba.hdwq[0];
20012b65e182SJames Smart 
20022b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS   RCV: xri x%x sz %d from %06x\n",
20032b65e182SJames Smart 			 oxid, size, sid);
2004d613b6a7SJames Smart 	/*
2005d613b6a7SJames Smart 	 * The calling sequence should be:
2006d613b6a7SJames Smart 	 * nvmet_fc_rcv_ls_req -> lpfc_nvmet_xmt_ls_rsp/cmp ->_req->done
2007d613b6a7SJames Smart 	 * lpfc_nvmet_xmt_ls_rsp_cmp should free the allocated ctxp.
2008d613b6a7SJames Smart 	 */
2009d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
2010d613b6a7SJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, &ctxp->ctx.ls_req,
2011d613b6a7SJames Smart 				 payload, size);
2012d613b6a7SJames Smart 
2013d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2014ce1b591cSJames Smart 			"6037 NVMET Unsol rcv: sz %d rc %d: %08x %08x %08x "
2015ce1b591cSJames Smart 			"%08x %08x %08x\n", size, rc,
2016d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
2017d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
20182b65e182SJames Smart 
2019d613b6a7SJames Smart 	if (rc == 0) {
2020d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
2021d613b6a7SJames Smart 		return;
2022d613b6a7SJames Smart 	}
20232b65e182SJames Smart 
20242b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  DROP: xri x%x sz %d from %06x\n",
20252b65e182SJames Smart 			 oxid, size, sid);
20262b65e182SJames Smart 
2027d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
2028d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2029d613b6a7SJames Smart 			"6156 LS Drop IO x%x: nvmet_fc_rcv_ls_req %d\n",
2030d613b6a7SJames Smart 			ctxp->oxid, rc);
2031d613b6a7SJames Smart 
2032d613b6a7SJames Smart 	/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
2033d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
2034d613b6a7SJames Smart 
2035d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_ls_abort);
2036d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, sid, oxid);
2037166d7211SJames Smart #endif
2038d613b6a7SJames Smart }
2039d613b6a7SJames Smart 
2040472e146dSJames Smart static void
2041472e146dSJames Smart lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf)
2042472e146dSJames Smart {
2043472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2044472e146dSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
2045472e146dSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
2046472e146dSJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
2047472e146dSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
20484552e0f6SJames Smart 	uint32_t *payload, qno;
2049472e146dSJames Smart 	uint32_t rc;
2050472e146dSJames Smart 	unsigned long iflags;
2051472e146dSJames Smart 
2052472e146dSJames Smart 	if (!nvmebuf) {
2053472e146dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2054472e146dSJames Smart 			"6159 process_rcv_fcp_req, nvmebuf is NULL, "
2055472e146dSJames Smart 			"oxid: x%x flg: x%x state: x%x\n",
2056472e146dSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
2057472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
2058472e146dSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2059472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2060472e146dSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
2061472e146dSJames Smart 						 ctxp->oxid);
2062472e146dSJames Smart 		return;
2063472e146dSJames Smart 	}
2064472e146dSJames Smart 
206579d8c4ceSJames Smart 	if (ctxp->flag & LPFC_NVMET_ABTS_RCV) {
206679d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
206779d8c4ceSJames Smart 				"6324 IO oxid x%x aborted\n",
206879d8c4ceSJames Smart 				ctxp->oxid);
206979d8c4ceSJames Smart 		return;
207079d8c4ceSJames Smart 	}
207179d8c4ceSJames Smart 
2072472e146dSJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
2073472e146dSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
207479d8c4ceSJames Smart 	ctxp->flag |= LPFC_NVMET_TNOTIFY;
2075d74a89aaSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2076d74a89aaSJames Smart 	if (ctxp->ts_isr_cmd)
2077d74a89aaSJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
2078d74a89aaSJames Smart #endif
2079472e146dSJames Smart 	/*
2080472e146dSJames Smart 	 * The calling sequence should be:
2081472e146dSJames Smart 	 * nvmet_fc_rcv_fcp_req->lpfc_nvmet_xmt_fcp_op/cmp- req->done
2082472e146dSJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
2083472e146dSJames Smart 	 * When we return from nvmet_fc_rcv_fcp_req, all relevant info
2084472e146dSJames Smart 	 * the NVME command / FC header is stored.
2085472e146dSJames Smart 	 * A buffer has already been reposted for this IO, so just free
2086472e146dSJames Smart 	 * the nvmebuf.
2087472e146dSJames Smart 	 */
2088472e146dSJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->ctx.fcp_req,
2089472e146dSJames Smart 				  payload, ctxp->size);
2090472e146dSJames Smart 	/* Process FCP command */
2091472e146dSJames Smart 	if (rc == 0) {
2092472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
20934552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
20944552e0f6SJames Smart 		if ((ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) ||
20954552e0f6SJames Smart 		    (nvmebuf != ctxp->rqb_buffer)) {
20964552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
20974552e0f6SJames Smart 			return;
20984552e0f6SJames Smart 		}
20994552e0f6SJames Smart 		ctxp->rqb_buffer = NULL;
21004552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
21014552e0f6SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
2102472e146dSJames Smart 		return;
2103472e146dSJames Smart 	}
2104472e146dSJames Smart 
2105472e146dSJames Smart 	/* Processing of FCP command is deferred */
2106472e146dSJames Smart 	if (rc == -EOVERFLOW) {
2107472e146dSJames Smart 		lpfc_nvmeio_data(phba, "NVMET RCV BUSY: xri x%x sz %d "
2108472e146dSJames Smart 				 "from %06x\n",
2109472e146dSJames Smart 				 ctxp->oxid, ctxp->size, ctxp->sid);
2110472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
2111472e146dSJames Smart 		atomic_inc(&tgtp->defer_fod);
21124552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
21134552e0f6SJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) {
21144552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
21154552e0f6SJames Smart 			return;
21164552e0f6SJames Smart 		}
21174552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
21184552e0f6SJames Smart 		/*
21194552e0f6SJames Smart 		 * Post a replacement DMA buffer to RQ and defer
21204552e0f6SJames Smart 		 * freeing rcv buffer till .defer_rcv callback
21214552e0f6SJames Smart 		 */
21224552e0f6SJames Smart 		qno = nvmebuf->idx;
21234552e0f6SJames Smart 		lpfc_post_rq_buffer(
21244552e0f6SJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
21254552e0f6SJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2126472e146dSJames Smart 		return;
2127472e146dSJames Smart 	}
212879d8c4ceSJames Smart 	ctxp->flag &= ~LPFC_NVMET_TNOTIFY;
2129472e146dSJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2130472e146dSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2131472e146dSJames Smart 			"2582 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
2132472e146dSJames Smart 			ctxp->oxid, rc,
2133472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_in),
2134472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_out),
2135472e146dSJames Smart 			atomic_read(&tgtp->xmt_fcp_release));
2136472e146dSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
2137472e146dSJames Smart 			 ctxp->oxid, ctxp->size, ctxp->sid);
2138472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflags);
2139472e146dSJames Smart 	lpfc_nvmet_defer_release(phba, ctxp);
2140472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2141472e146dSJames Smart 	lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
2142472e146dSJames Smart #endif
2143472e146dSJames Smart }
2144472e146dSJames Smart 
2145472e146dSJames Smart static void
2146472e146dSJames Smart lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *work)
2147472e146dSJames Smart {
2148472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2149472e146dSJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf =
2150472e146dSJames Smart 		container_of(work, struct lpfc_nvmet_ctxbuf, defer_work);
2151472e146dSJames Smart 
2152472e146dSJames Smart 	lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2153472e146dSJames Smart #endif
2154472e146dSJames Smart }
2155472e146dSJames Smart 
215666d7ce93SDick Kennedy static struct lpfc_nvmet_ctxbuf *
215766d7ce93SDick Kennedy lpfc_nvmet_replenish_context(struct lpfc_hba *phba,
215866d7ce93SDick Kennedy 			     struct lpfc_nvmet_ctx_info *current_infop)
215966d7ce93SDick Kennedy {
21602299e432SDick Kennedy #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
216166d7ce93SDick Kennedy 	struct lpfc_nvmet_ctxbuf *ctx_buf = NULL;
216266d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *get_infop;
216366d7ce93SDick Kennedy 	int i;
216466d7ce93SDick Kennedy 
216566d7ce93SDick Kennedy 	/*
216666d7ce93SDick Kennedy 	 * The current_infop for the MRQ a NVME command IU was received
216766d7ce93SDick Kennedy 	 * on is empty. Our goal is to replenish this MRQs context
216866d7ce93SDick Kennedy 	 * list from a another CPUs.
216966d7ce93SDick Kennedy 	 *
217066d7ce93SDick Kennedy 	 * First we need to pick a context list to start looking on.
217166d7ce93SDick Kennedy 	 * nvmet_ctx_start_cpu has available context the last time
217266d7ce93SDick Kennedy 	 * we needed to replenish this CPU where nvmet_ctx_next_cpu
217366d7ce93SDick Kennedy 	 * is just the next sequential CPU for this MRQ.
217466d7ce93SDick Kennedy 	 */
217566d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_start_cpu)
217666d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_start_cpu;
217766d7ce93SDick Kennedy 	else
217866d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_next_cpu;
217966d7ce93SDick Kennedy 
2180222e9239SJames Smart 	for (i = 0; i < phba->sli4_hba.num_possible_cpu; i++) {
218166d7ce93SDick Kennedy 		if (get_infop == current_infop) {
218266d7ce93SDick Kennedy 			get_infop = get_infop->nvmet_ctx_next_cpu;
218366d7ce93SDick Kennedy 			continue;
218466d7ce93SDick Kennedy 		}
218566d7ce93SDick Kennedy 		spin_lock(&get_infop->nvmet_ctx_list_lock);
218666d7ce93SDick Kennedy 
218766d7ce93SDick Kennedy 		/* Just take the entire context list, if there are any */
218866d7ce93SDick Kennedy 		if (get_infop->nvmet_ctx_list_cnt) {
218966d7ce93SDick Kennedy 			list_splice_init(&get_infop->nvmet_ctx_list,
219066d7ce93SDick Kennedy 				    &current_infop->nvmet_ctx_list);
219166d7ce93SDick Kennedy 			current_infop->nvmet_ctx_list_cnt =
219266d7ce93SDick Kennedy 				get_infop->nvmet_ctx_list_cnt - 1;
219366d7ce93SDick Kennedy 			get_infop->nvmet_ctx_list_cnt = 0;
219466d7ce93SDick Kennedy 			spin_unlock(&get_infop->nvmet_ctx_list_lock);
219566d7ce93SDick Kennedy 
219666d7ce93SDick Kennedy 			current_infop->nvmet_ctx_start_cpu = get_infop;
219766d7ce93SDick Kennedy 			list_remove_head(&current_infop->nvmet_ctx_list,
219866d7ce93SDick Kennedy 					 ctx_buf, struct lpfc_nvmet_ctxbuf,
219966d7ce93SDick Kennedy 					 list);
220066d7ce93SDick Kennedy 			return ctx_buf;
220166d7ce93SDick Kennedy 		}
220266d7ce93SDick Kennedy 
220366d7ce93SDick Kennedy 		/* Otherwise, move on to the next CPU for this MRQ */
220466d7ce93SDick Kennedy 		spin_unlock(&get_infop->nvmet_ctx_list_lock);
220566d7ce93SDick Kennedy 		get_infop = get_infop->nvmet_ctx_next_cpu;
220666d7ce93SDick Kennedy 	}
220766d7ce93SDick Kennedy 
22082299e432SDick Kennedy #endif
220966d7ce93SDick Kennedy 	/* Nothing found, all contexts for the MRQ are in-flight */
221066d7ce93SDick Kennedy 	return NULL;
221166d7ce93SDick Kennedy }
221266d7ce93SDick Kennedy 
2213d613b6a7SJames Smart /**
2214d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
2215d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
221666d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2217d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
2218d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2219d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2220d613b6a7SJames Smart  *
2221d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
2222d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
2223d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
2224d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
2225d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
2226d613b6a7SJames Smart  * of the discovery state machine.
2227d613b6a7SJames Smart  **/
2228d613b6a7SJames Smart static void
2229d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
223066d7ce93SDick Kennedy 			    uint32_t idx,
2231d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
2232d74a89aaSJames Smart 			    uint64_t isr_timestamp,
2233d74a89aaSJames Smart 			    uint8_t cqflag)
2234d613b6a7SJames Smart {
2235d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
2236d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2237d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
22386c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
223966d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *current_infop;
2240472e146dSJames Smart 	uint32_t size, oxid, sid, qno;
22416c621a22SJames Smart 	unsigned long iflag;
224266d7ce93SDick Kennedy 	int current_cpu;
2243d613b6a7SJames Smart 
224473626173SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_TARGET_FC))
224573626173SArnd Bergmann 		return;
224673626173SArnd Bergmann 
22476c621a22SJames Smart 	ctx_buf = NULL;
2248d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
2249d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
22506c621a22SJames Smart 				"6157 NVMET FCP Drop IO\n");
2251472e146dSJames Smart 		if (nvmebuf)
2252472e146dSJames Smart 			lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2253472e146dSJames Smart 		return;
2254d613b6a7SJames Smart 	}
2255d613b6a7SJames Smart 
225666d7ce93SDick Kennedy 	/*
225766d7ce93SDick Kennedy 	 * Get a pointer to the context list for this MRQ based on
225866d7ce93SDick Kennedy 	 * the CPU this MRQ IRQ is associated with. If the CPU association
225966d7ce93SDick Kennedy 	 * changes from our initial assumption, the context list could
226066d7ce93SDick Kennedy 	 * be empty, thus it would need to be replenished with the
226166d7ce93SDick Kennedy 	 * context list from another CPU for this MRQ.
226266d7ce93SDick Kennedy 	 */
2263d6d189ceSBart Van Assche 	current_cpu = raw_smp_processor_id();
226466d7ce93SDick Kennedy 	current_infop = lpfc_get_ctx_list(phba, current_cpu, idx);
226566d7ce93SDick Kennedy 	spin_lock_irqsave(&current_infop->nvmet_ctx_list_lock, iflag);
226666d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_list_cnt) {
226766d7ce93SDick Kennedy 		list_remove_head(&current_infop->nvmet_ctx_list,
22686c621a22SJames Smart 				 ctx_buf, struct lpfc_nvmet_ctxbuf, list);
226966d7ce93SDick Kennedy 		current_infop->nvmet_ctx_list_cnt--;
2270966bb5b7SJames Smart 	} else {
227166d7ce93SDick Kennedy 		ctx_buf = lpfc_nvmet_replenish_context(phba, current_infop);
22726c621a22SJames Smart 	}
227366d7ce93SDick Kennedy 	spin_unlock_irqrestore(&current_infop->nvmet_ctx_list_lock, iflag);
22746c621a22SJames Smart 
2275a8cf5dfeSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
2276a8cf5dfeSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2277a8cf5dfeSJames Smart 	size = nvmebuf->bytes_recv;
2278a8cf5dfeSJames Smart 
2279a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2280a8cf5dfeSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_RCV) {
228163df6d63SJames Smart 		if (current_cpu < LPFC_CHECK_CPU_CNT) {
228263df6d63SJames Smart 			if (idx != current_cpu)
228363df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
228463df6d63SJames Smart 						"6703 CPU Check rcv: "
228563df6d63SJames Smart 						"cpu %d expect %d\n",
228663df6d63SJames Smart 						current_cpu, idx);
228763df6d63SJames Smart 			phba->sli4_hba.hdwq[idx].cpucheck_rcv_io[current_cpu]++;
228863df6d63SJames Smart 		}
2289a8cf5dfeSJames Smart 	}
2290a8cf5dfeSJames Smart #endif
2291a8cf5dfeSJames Smart 
2292a8cf5dfeSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d CPU %02x\n",
2293d6d189ceSBart Van Assche 			 oxid, size, raw_smp_processor_id());
2294a8cf5dfeSJames Smart 
2295411de511SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2296411de511SJames Smart 
22976c621a22SJames Smart 	if (!ctx_buf) {
2298a8cf5dfeSJames Smart 		/* Queue this NVME IO to process later */
2299a8cf5dfeSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
2300a8cf5dfeSJames Smart 		list_add_tail(&nvmebuf->hbuf.list,
2301a8cf5dfeSJames Smart 			      &phba->sli4_hba.lpfc_nvmet_io_wait_list);
2302a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt++;
2303a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_total++;
2304a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
2305a8cf5dfeSJames Smart 				       iflag);
2306a8cf5dfeSJames Smart 
2307a8cf5dfeSJames Smart 		/* Post a brand new DMA buffer to RQ */
2308a8cf5dfeSJames Smart 		qno = nvmebuf->idx;
2309a8cf5dfeSJames Smart 		lpfc_post_rq_buffer(
2310a8cf5dfeSJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
2311a8cf5dfeSJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2312411de511SJames Smart 
2313411de511SJames Smart 		atomic_inc(&tgtp->defer_ctx);
2314a8cf5dfeSJames Smart 		return;
23156c621a22SJames Smart 	}
2316d613b6a7SJames Smart 
2317d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
2318d613b6a7SJames Smart 
23196c621a22SJames Smart 	ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
232079d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
232179d8c4ceSJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.t_active_ctx_list);
232279d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
2323ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_FREE) {
2324ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2325ce1b591cSJames Smart 				"6414 NVMET Context corrupt %d %d oxid x%x\n",
2326ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
2327ce1b591cSJames Smart 	}
2328d613b6a7SJames Smart 	ctxp->wqeq = NULL;
2329d613b6a7SJames Smart 	ctxp->txrdy = NULL;
2330d613b6a7SJames Smart 	ctxp->offset = 0;
2331d613b6a7SJames Smart 	ctxp->phba = phba;
2332d613b6a7SJames Smart 	ctxp->size = size;
2333d613b6a7SJames Smart 	ctxp->oxid = oxid;
2334d613b6a7SJames Smart 	ctxp->sid = sid;
233566d7ce93SDick Kennedy 	ctxp->idx = idx;
2336d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_RCV;
2337d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
2338d613b6a7SJames Smart 	ctxp->flag = 0;
23396c621a22SJames Smart 	ctxp->ctxbuf = ctx_buf;
2340cbc5de1bSJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
23411fbf9742SJames Smart 	ctxp->hdwq = NULL;
23422b7824d0SJames Smart 	spin_lock_init(&ctxp->ctxlock);
2343d613b6a7SJames Smart 
23442b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2345d74a89aaSJames Smart 	if (isr_timestamp)
23462b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
2347d74a89aaSJames Smart 	ctxp->ts_cmd_nvme = 0;
23482b65e182SJames Smart 	ctxp->ts_nvme_data = 0;
23492b65e182SJames Smart 	ctxp->ts_data_wqput = 0;
23502b65e182SJames Smart 	ctxp->ts_isr_data = 0;
23512b65e182SJames Smart 	ctxp->ts_data_nvme = 0;
23522b65e182SJames Smart 	ctxp->ts_nvme_status = 0;
23532b65e182SJames Smart 	ctxp->ts_status_wqput = 0;
23542b65e182SJames Smart 	ctxp->ts_isr_status = 0;
23552b65e182SJames Smart 	ctxp->ts_status_nvme = 0;
23562b65e182SJames Smart #endif
23572b65e182SJames Smart 
2358d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
2359d74a89aaSJames Smart 	/* check for cq processing load */
2360d74a89aaSJames Smart 	if (!cqflag) {
2361472e146dSJames Smart 		lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2362d74a89aaSJames Smart 		return;
2363d74a89aaSJames Smart 	}
2364d74a89aaSJames Smart 
2365d74a89aaSJames Smart 	if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
2366d74a89aaSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2367d74a89aaSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
2368d74a89aaSJames Smart 				"6325 Unable to queue work for oxid x%x. "
2369d74a89aaSJames Smart 				"FCP Drop IO [x%x x%x x%x]\n",
2370d74a89aaSJames Smart 				ctxp->oxid,
2371d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_in),
2372d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_out),
2373d74a89aaSJames Smart 				atomic_read(&tgtp->xmt_fcp_release));
2374d74a89aaSJames Smart 
2375d74a89aaSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
2376d74a89aaSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2377d74a89aaSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
2378d74a89aaSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
2379d74a89aaSJames Smart 	}
2380d613b6a7SJames Smart }
2381d613b6a7SJames Smart 
2382d613b6a7SJames Smart /**
2383d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_event - Process an unsolicited event from an nvme nport
2384d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
2385d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
2386d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2387d613b6a7SJames Smart  *
2388d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2389d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2390d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2391d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer() after properly set up the buffer from the
2392d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2393d613b6a7SJames Smart  **/
2394d613b6a7SJames Smart void
2395d613b6a7SJames Smart lpfc_nvmet_unsol_ls_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2396d613b6a7SJames Smart 			  struct lpfc_iocbq *piocb)
2397d613b6a7SJames Smart {
2398d613b6a7SJames Smart 	struct lpfc_dmabuf *d_buf;
2399d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf;
2400d613b6a7SJames Smart 
2401d613b6a7SJames Smart 	d_buf = piocb->context2;
2402d613b6a7SJames Smart 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2403d613b6a7SJames Smart 
2404d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
2405d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
2406d613b6a7SJames Smart 		return;
2407d613b6a7SJames Smart 	}
2408d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_buffer(phba, pring, nvmebuf);
2409d613b6a7SJames Smart }
2410d613b6a7SJames Smart 
2411d613b6a7SJames Smart /**
2412d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
2413d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
241466d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2415d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2416d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2417d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2418d613b6a7SJames Smart  *
2419d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2420d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2421d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2422d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
2423d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2424d613b6a7SJames Smart  **/
2425d613b6a7SJames Smart void
2426d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
242766d7ce93SDick Kennedy 			   uint32_t idx,
2428d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
2429d74a89aaSJames Smart 			   uint64_t isr_timestamp,
2430d74a89aaSJames Smart 			   uint8_t cqflag)
2431d613b6a7SJames Smart {
2432d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
24336c621a22SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2434d613b6a7SJames Smart 		return;
2435d613b6a7SJames Smart 	}
2436d74a89aaSJames Smart 	lpfc_nvmet_unsol_fcp_buffer(phba, idx, nvmebuf, isr_timestamp, cqflag);
2437d613b6a7SJames Smart }
2438d613b6a7SJames Smart 
2439d613b6a7SJames Smart /**
2440d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
2441d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
2442d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
2443d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
2444d613b6a7SJames Smart  * @rspsize: size of the NVME command.
2445d613b6a7SJames Smart  *
2446d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
2447d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
2448d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
2449d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
2450d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
2451d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
2452d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
2453d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
2454d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
2455d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
2456d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
2457d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
2458d613b6a7SJames Smart  * reference for the command's callback function to access later.
2459d613b6a7SJames Smart  *
2460d613b6a7SJames Smart  * Return code
2461d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
2462d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
2463d613b6a7SJames Smart  **/
2464d613b6a7SJames Smart static struct lpfc_iocbq *
2465d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
2466d613b6a7SJames Smart 		       struct lpfc_nvmet_rcv_ctx *ctxp,
2467d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
2468d613b6a7SJames Smart {
2469d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2470d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2471205e8240SJames Smart 	union lpfc_wqe128 *wqe;
2472d613b6a7SJames Smart 
2473d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2474d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2475ce1b591cSJames Smart 				"6104 NVMET prep LS wqe: link err: "
2476ce1b591cSJames Smart 				"NPORT x%x oxid:x%x ste %d\n",
2477ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2478d613b6a7SJames Smart 		return NULL;
2479d613b6a7SJames Smart 	}
2480d613b6a7SJames Smart 
2481d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
2482d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
2483d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2484d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2485ce1b591cSJames Smart 				"6105 NVMET prep LS wqe: No WQE: "
2486ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2487ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2488d613b6a7SJames Smart 		return NULL;
2489d613b6a7SJames Smart 	}
2490d613b6a7SJames Smart 
2491d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2492d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2493d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2494d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2495d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2496ce1b591cSJames Smart 				"6106 NVMET prep LS wqe: No ndlp: "
2497ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2498ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2499d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2500d613b6a7SJames Smart 	}
2501d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
2502d613b6a7SJames Smart 
2503d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
2504d613b6a7SJames Smart 	nvmewqe->context1 = lpfc_nlp_get(ndlp);
2505d613b6a7SJames Smart 	if (nvmewqe->context1 == NULL)
2506d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2507d613b6a7SJames Smart 	nvmewqe->context2 = ctxp;
2508d613b6a7SJames Smart 
2509d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
2510d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
2511d613b6a7SJames Smart 
2512d613b6a7SJames Smart 	/* Words 0 - 2 */
2513d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2514d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
2515d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
2516d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
2517d613b6a7SJames Smart 
2518d613b6a7SJames Smart 	/* Word 3 */
2519d613b6a7SJames Smart 
2520d613b6a7SJames Smart 	/* Word 4 */
2521d613b6a7SJames Smart 
2522d613b6a7SJames Smart 	/* Word 5 */
2523d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
2524d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
2525d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
25268b361639SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
2527d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
2528d613b6a7SJames Smart 
2529d613b6a7SJames Smart 	/* Word 6 */
2530d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
2531d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2532d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
2533d613b6a7SJames Smart 
2534d613b6a7SJames Smart 	/* Word 7 */
2535d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
2536d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
2537d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
2538d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
2539d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
2540d613b6a7SJames Smart 
2541d613b6a7SJames Smart 	/* Word 8 */
2542d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
2543d613b6a7SJames Smart 
2544d613b6a7SJames Smart 	/* Word 9 */
2545d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
2546d613b6a7SJames Smart 	/* Needs to be set by caller */
2547d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
2548d613b6a7SJames Smart 
2549d613b6a7SJames Smart 	/* Word 10 */
2550d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
2551d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2552d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
2553d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
2554d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
2555d613b6a7SJames Smart 
2556d613b6a7SJames Smart 	/* Word 11 */
2557d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
2558d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
2559d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
2560d613b6a7SJames Smart 	       OTHER_COMMAND);
2561d613b6a7SJames Smart 
2562d613b6a7SJames Smart 	/* Word 12 */
2563d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
2564d613b6a7SJames Smart 
2565d613b6a7SJames Smart 	nvmewqe->retry = 1;
2566d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2567d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2568d613b6a7SJames Smart 	nvmewqe->iocb_flag |= LPFC_IO_NVME_LS;
2569d613b6a7SJames Smart 
2570ce1b591cSJames Smart 	/* Xmit NVMET response to remote NPORT <did> */
2571d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2572ce1b591cSJames Smart 			"6039 Xmit NVMET LS response to remote "
2573d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
2574d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
2575d613b6a7SJames Smart 			rspsize);
2576d613b6a7SJames Smart 	return nvmewqe;
2577d613b6a7SJames Smart 
2578d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
2579d613b6a7SJames Smart 	nvmewqe->context2 = NULL;
2580d613b6a7SJames Smart 	nvmewqe->context3 = NULL;
2581d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
2582d613b6a7SJames Smart 	return NULL;
2583d613b6a7SJames Smart }
2584d613b6a7SJames Smart 
2585d613b6a7SJames Smart 
2586d613b6a7SJames Smart static struct lpfc_iocbq *
2587d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
2588d613b6a7SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
2589d613b6a7SJames Smart {
2590d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->ctx.fcp_req;
2591d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2592d613b6a7SJames Smart 	struct sli4_sge *sgl;
2593d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2594d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2595d613b6a7SJames Smart 	struct scatterlist *sgel;
2596d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
25970bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
2598d613b6a7SJames Smart 	uint32_t *txrdy;
2599d613b6a7SJames Smart 	dma_addr_t physaddr;
2600d613b6a7SJames Smart 	int i, cnt;
26010bc2b7c5SJames Smart 	int do_pbde;
2602d613b6a7SJames Smart 	int xc = 1;
2603d613b6a7SJames Smart 
2604d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2605d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2606ce1b591cSJames Smart 				"6107 NVMET prep FCP wqe: link err:"
2607ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2608ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2609d613b6a7SJames Smart 		return NULL;
2610d613b6a7SJames Smart 	}
2611d613b6a7SJames Smart 
2612d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2613d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2614d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2615d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2616d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2617ce1b591cSJames Smart 				"6108 NVMET prep FCP wqe: no ndlp: "
2618ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2619ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2620d613b6a7SJames Smart 		return NULL;
2621d613b6a7SJames Smart 	}
2622d613b6a7SJames Smart 
262381e6a637SJames Smart 	if (rsp->sg_cnt > lpfc_tgttemplate.max_sgl_segments) {
2624d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2625ce1b591cSJames Smart 				"6109 NVMET prep FCP wqe: seg cnt err: "
2626ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d cnt %d\n",
2627ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state,
2628ce1b591cSJames Smart 				phba->cfg_nvme_seg_cnt);
2629d613b6a7SJames Smart 		return NULL;
2630d613b6a7SJames Smart 	}
2631d613b6a7SJames Smart 
2632d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2633d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
2634d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2635d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
26366c621a22SJames Smart 		nvmewqe = ctxp->ctxbuf->iocbq;
2637d613b6a7SJames Smart 		if (nvmewqe == NULL) {
2638d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2639ce1b591cSJames Smart 					"6110 NVMET prep FCP wqe: No "
2640ce1b591cSJames Smart 					"WQE: NPORT x%x oxid x%x ste %d\n",
2641ce1b591cSJames Smart 					ctxp->sid, ctxp->oxid, ctxp->state);
2642d613b6a7SJames Smart 			return NULL;
2643d613b6a7SJames Smart 		}
2644d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
2645d613b6a7SJames Smart 		xc = 0; /* create new XRI */
2646d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
2647d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
2648d613b6a7SJames Smart 	}
2649d613b6a7SJames Smart 
2650d613b6a7SJames Smart 	/* Sanity check */
2651d613b6a7SJames Smart 	if (((ctxp->state == LPFC_NVMET_STE_RCV) &&
2652d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
2653ce1b591cSJames Smart 	    (ctxp->state == LPFC_NVMET_STE_DATA)) {
2654205e8240SJames Smart 		wqe = &nvmewqe->wqe;
2655d613b6a7SJames Smart 	} else {
2656d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2657ce1b591cSJames Smart 				"6111 Wrong state NVMET FCP: %d  cnt %d\n",
2658ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt);
2659d613b6a7SJames Smart 		return NULL;
2660d613b6a7SJames Smart 	}
2661d613b6a7SJames Smart 
26626c621a22SJames Smart 	sgl  = (struct sli4_sge *)ctxp->ctxbuf->sglq->sgl;
2663d613b6a7SJames Smart 	switch (rsp->op) {
2664d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
2665d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
2666bd3061baSJames Smart 		/* From the tsend template, initialize words 7 - 11 */
2667bd3061baSJames Smart 		memcpy(&wqe->words[7],
2668bd3061baSJames Smart 		       &lpfc_tsend_cmd_template.words[7],
2669bd3061baSJames Smart 		       sizeof(uint32_t) * 5);
2670bd3061baSJames Smart 
2671d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2672d613b6a7SJames Smart 		sgel = &rsp->sg[0];
2673d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2674d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2675d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
2676d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
2677d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
2678d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2679d613b6a7SJames Smart 
2680d613b6a7SJames Smart 		/* Word 3 */
2681d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
2682d613b6a7SJames Smart 
2683d613b6a7SJames Smart 		/* Word 4 */
2684d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
2685d613b6a7SJames Smart 
2686d613b6a7SJames Smart 		/* Word 5 */
2687bd3061baSJames Smart 		wqe->fcp_tsend.reserved = 0;
2688d613b6a7SJames Smart 
2689d613b6a7SJames Smart 		/* Word 6 */
2690d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
2691d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2692d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
2693d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2694d613b6a7SJames Smart 
2695bd3061baSJames Smart 		/* Word 7 - set ar later */
2696d613b6a7SJames Smart 
2697d613b6a7SJames Smart 		/* Word 8 */
2698d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
2699d613b6a7SJames Smart 
2700d613b6a7SJames Smart 		/* Word 9 */
2701d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
2702d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
2703d613b6a7SJames Smart 
2704bd3061baSJames Smart 		/* Word 10 - set wqes later, in template xc=1 */
2705bd3061baSJames Smart 		if (!xc)
2706bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 0);
2707d613b6a7SJames Smart 
2708bd3061baSJames Smart 		/* Word 11 - set sup, irsp, irsplen later */
2709bd3061baSJames Smart 		do_pbde = 0;
2710d613b6a7SJames Smart 
2711d613b6a7SJames Smart 		/* Word 12 */
2712d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2713d613b6a7SJames Smart 
2714d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
2715d613b6a7SJames Smart 		sgl->addr_hi = 0;
2716d613b6a7SJames Smart 		sgl->addr_lo = 0;
2717d613b6a7SJames Smart 		sgl->word2 = 0;
2718d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2719d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2720d613b6a7SJames Smart 		sgl->sge_len = 0;
2721d613b6a7SJames Smart 		sgl++;
2722d613b6a7SJames Smart 		sgl->addr_hi = 0;
2723d613b6a7SJames Smart 		sgl->addr_lo = 0;
2724d613b6a7SJames Smart 		sgl->word2 = 0;
2725d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2726d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2727d613b6a7SJames Smart 		sgl->sge_len = 0;
2728d613b6a7SJames Smart 		sgl++;
2729d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
2730d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
2731bd3061baSJames Smart 
2732bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2733bd3061baSJames Smart 
273420aefac3SJames Smart 			if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
273520aefac3SJames Smart 				if (ndlp->nlp_flag & NLP_SUPPRESS_RSP)
273620aefac3SJames Smart 					bf_set(wqe_sup,
273720aefac3SJames Smart 					       &wqe->fcp_tsend.wqe_com, 1);
2738d613b6a7SJames Smart 			} else {
2739d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
2740d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
2741d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
2742d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
2743d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
2744d613b6a7SJames Smart 				       rsp->rsplen);
2745d613b6a7SJames Smart 			}
2746d613b6a7SJames Smart 		} else {
2747d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
2748d613b6a7SJames Smart 
2749bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2750d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
2751d613b6a7SJames Smart 		}
2752d613b6a7SJames Smart 		break;
2753d613b6a7SJames Smart 
2754d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
2755bd3061baSJames Smart 		/* From the treceive template, initialize words 3 - 11 */
2756bd3061baSJames Smart 		memcpy(&wqe->words[3],
2757bd3061baSJames Smart 		       &lpfc_treceive_cmd_template.words[3],
2758bd3061baSJames Smart 		       sizeof(uint32_t) * 9);
2759bd3061baSJames Smart 
2760d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2761771db5c0SRomain Perier 		txrdy = dma_pool_alloc(phba->txrdy_payload_pool,
2762d613b6a7SJames Smart 				       GFP_KERNEL, &physaddr);
2763d613b6a7SJames Smart 		if (!txrdy) {
2764d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2765d613b6a7SJames Smart 					"6041 Bad txrdy buffer: oxid x%x\n",
2766d613b6a7SJames Smart 					ctxp->oxid);
2767d613b6a7SJames Smart 			return NULL;
2768d613b6a7SJames Smart 		}
2769d613b6a7SJames Smart 		ctxp->txrdy = txrdy;
2770d613b6a7SJames Smart 		ctxp->txrdy_phys = physaddr;
2771d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2772d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = TXRDY_PAYLOAD_LEN;
2773d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrLow =
2774d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2775d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrHigh =
2776d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2777d613b6a7SJames Smart 
2778d613b6a7SJames Smart 		/* Word 4 */
2779d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
2780d613b6a7SJames Smart 
2781d613b6a7SJames Smart 		/* Word 6 */
2782d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
2783d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2784d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
2785d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2786d613b6a7SJames Smart 
2787d613b6a7SJames Smart 		/* Word 7 */
2788d613b6a7SJames Smart 
2789d613b6a7SJames Smart 		/* Word 8 */
2790d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
2791d613b6a7SJames Smart 
2792d613b6a7SJames Smart 		/* Word 9 */
2793d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
2794d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
2795d613b6a7SJames Smart 
2796bd3061baSJames Smart 		/* Word 10 - in template xc=1 */
2797bd3061baSJames Smart 		if (!xc)
2798bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, 0);
2799d613b6a7SJames Smart 
2800bd3061baSJames Smart 		/* Word 11 - set pbde later */
2801414abe0aSJames Smart 		if (phba->cfg_enable_pbde) {
2802bd3061baSJames Smart 			do_pbde = 1;
2803bd3061baSJames Smart 		} else {
2804bd3061baSJames Smart 			bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 0);
2805bd3061baSJames Smart 			do_pbde = 0;
2806bd3061baSJames Smart 		}
2807d613b6a7SJames Smart 
2808d613b6a7SJames Smart 		/* Word 12 */
2809d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2810d613b6a7SJames Smart 
2811d613b6a7SJames Smart 		/* Setup 1 TXRDY and 1 SKIP SGE */
2812d613b6a7SJames Smart 		txrdy[0] = 0;
2813d613b6a7SJames Smart 		txrdy[1] = cpu_to_be32(rsp->transfer_length);
2814d613b6a7SJames Smart 		txrdy[2] = 0;
2815d613b6a7SJames Smart 
2816d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
2817d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
2818d613b6a7SJames Smart 		sgl->word2 = 0;
2819d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2820d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2821d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(TXRDY_PAYLOAD_LEN);
2822d613b6a7SJames Smart 		sgl++;
2823d613b6a7SJames Smart 		sgl->addr_hi = 0;
2824d613b6a7SJames Smart 		sgl->addr_lo = 0;
2825d613b6a7SJames Smart 		sgl->word2 = 0;
2826d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2827d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2828d613b6a7SJames Smart 		sgl->sge_len = 0;
2829d613b6a7SJames Smart 		sgl++;
2830d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
2831d613b6a7SJames Smart 		break;
2832d613b6a7SJames Smart 
2833d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
2834bd3061baSJames Smart 		/* From the treceive template, initialize words 4 - 11 */
2835bd3061baSJames Smart 		memcpy(&wqe->words[4],
2836bd3061baSJames Smart 		       &lpfc_trsp_cmd_template.words[4],
2837bd3061baSJames Smart 		       sizeof(uint32_t) * 8);
2838bd3061baSJames Smart 
2839d613b6a7SJames Smart 		/* Words 0 - 2 */
2840d613b6a7SJames Smart 		physaddr = rsp->rspdma;
2841d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2842d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
2843d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
2844d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2845d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
2846d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2847d613b6a7SJames Smart 
2848d613b6a7SJames Smart 		/* Word 3 */
2849d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
2850d613b6a7SJames Smart 
2851d613b6a7SJames Smart 		/* Word 6 */
2852d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
2853d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2854d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
2855d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2856d613b6a7SJames Smart 
2857d613b6a7SJames Smart 		/* Word 7 */
2858d613b6a7SJames Smart 
2859d613b6a7SJames Smart 		/* Word 8 */
2860d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
2861d613b6a7SJames Smart 
2862d613b6a7SJames Smart 		/* Word 9 */
2863d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
2864d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
2865d613b6a7SJames Smart 
2866d613b6a7SJames Smart 		/* Word 10 */
2867bd3061baSJames Smart 		if (xc)
2868bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 1);
2869d613b6a7SJames Smart 
2870d613b6a7SJames Smart 		/* Word 11 */
2871bd3061baSJames Smart 		/* In template wqes=0 irsp=0 irsplen=0 - good response */
2872bd3061baSJames Smart 		if (rsp->rsplen != LPFC_NVMET_SUCCESS_LEN) {
2873bd3061baSJames Smart 			/* Bad response - embed it */
2874d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
2875d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
2876d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
2877d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
2878d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
2879d613b6a7SJames Smart 		}
2880bd3061baSJames Smart 		do_pbde = 0;
2881bd3061baSJames Smart 
2882bd3061baSJames Smart 		/* Word 12 */
2883bd3061baSJames Smart 		wqe->fcp_trsp.rsvd_12_15[0] = 0;
2884d613b6a7SJames Smart 
2885d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
2886d613b6a7SJames Smart 		rsp->sg_cnt = 0;
2887d613b6a7SJames Smart 		sgl->word2 = 0;
2888d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
2889d613b6a7SJames Smart 		break;
2890d613b6a7SJames Smart 
2891d613b6a7SJames Smart 	default:
2892d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2893d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
2894d613b6a7SJames Smart 				rsp->op);
2895d613b6a7SJames Smart 		return NULL;
2896d613b6a7SJames Smart 	}
2897d613b6a7SJames Smart 
2898d613b6a7SJames Smart 	nvmewqe->retry = 1;
2899d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2900d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2901d613b6a7SJames Smart 	nvmewqe->context1 = ndlp;
2902d613b6a7SJames Smart 
290346e8e475SMing Lei 	for_each_sg(rsp->sg, sgel, rsp->sg_cnt, i) {
2904d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2905d613b6a7SJames Smart 		cnt = sg_dma_len(sgel);
2906d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
2907d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
2908d613b6a7SJames Smart 		sgl->word2 = 0;
2909d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2910d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
2911d613b6a7SJames Smart 		if ((i+1) == rsp->sg_cnt)
2912d613b6a7SJames Smart 			bf_set(lpfc_sli4_sge_last, sgl, 1);
2913d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2914d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(cnt);
2915414abe0aSJames Smart 		if (i == 0) {
29160bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)&wqe->words[13];
2917414abe0aSJames Smart 			if (do_pbde) {
29180bc2b7c5SJames Smart 				/* Words 13-15  (PBDE) */
29190bc2b7c5SJames Smart 				bde->addrLow = sgl->addr_lo;
29200bc2b7c5SJames Smart 				bde->addrHigh = sgl->addr_hi;
29210bc2b7c5SJames Smart 				bde->tus.f.bdeSize =
29220bc2b7c5SJames Smart 					le32_to_cpu(sgl->sge_len);
29230bc2b7c5SJames Smart 				bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
29240bc2b7c5SJames Smart 				bde->tus.w = cpu_to_le32(bde->tus.w);
2925414abe0aSJames Smart 			} else {
2926414abe0aSJames Smart 				memset(bde, 0, sizeof(struct ulp_bde64));
2927414abe0aSJames Smart 			}
29280bc2b7c5SJames Smart 		}
2929d613b6a7SJames Smart 		sgl++;
2930d613b6a7SJames Smart 		ctxp->offset += cnt;
2931d613b6a7SJames Smart 	}
2932ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_DATA;
2933ce1b591cSJames Smart 	ctxp->entry_cnt++;
2934d613b6a7SJames Smart 	return nvmewqe;
2935d613b6a7SJames Smart }
2936d613b6a7SJames Smart 
2937d613b6a7SJames Smart /**
2938d613b6a7SJames Smart  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
2939d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
2940d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
2941d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
2942d613b6a7SJames Smart  *
2943d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
2944d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
2945d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
2946d613b6a7SJames Smart  **/
2947d613b6a7SJames Smart static void
2948d613b6a7SJames Smart lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2949d613b6a7SJames Smart 			     struct lpfc_wcqe_complete *wcqe)
2950d613b6a7SJames Smart {
2951d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
2952d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2953b27cbd55SBart Van Assche 	uint32_t result;
295419b58d94SJames Smart 	unsigned long flags;
295519b58d94SJames Smart 	bool released = false;
2956d613b6a7SJames Smart 
2957d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
2958d613b6a7SJames Smart 	result = wcqe->parameter;
2959d613b6a7SJames Smart 
2960d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2961547077a4SJames Smart 	if (ctxp->flag & LPFC_NVMET_ABORT_OP)
2962547077a4SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
2963d613b6a7SJames Smart 
2964c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
2965d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_DONE;
296686c67379SJames Smart 
296786c67379SJames Smart 	/* Check if we already received a free context call
296886c67379SJames Smart 	 * and we have completed processing an abort situation.
296986c67379SJames Smart 	 */
297086c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_CTX_RLS) &&
297186c67379SJames Smart 	    !(ctxp->flag & LPFC_NVMET_XBUSY)) {
2972c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
297379d8c4ceSJames Smart 		list_del_init(&ctxp->list);
2974c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
297519b58d94SJames Smart 		released = true;
297686c67379SJames Smart 	}
297719b58d94SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
297819b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
2979547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
298019b58d94SJames Smart 
2981e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
298279d8c4ceSJames Smart 			"6165 ABORT cmpl: oxid x%x flg x%x (%d) "
298386c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
298486c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
298586c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
298686c67379SJames Smart 			result, wcqe->word3);
298786c67379SJames Smart 
29886c621a22SJames Smart 	cmdwqe->context2 = NULL;
29896c621a22SJames Smart 	cmdwqe->context3 = NULL;
299019b58d94SJames Smart 	/*
299119b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
299219b58d94SJames Smart 	 * will be recycled by transport release call.
299319b58d94SJames Smart 	 */
299419b58d94SJames Smart 	if (released)
29956c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
2996d613b6a7SJames Smart 
29976c621a22SJames Smart 	/* This is the iocbq for the abort, not the command */
2998d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
299986c67379SJames Smart 
300086c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
300186c67379SJames Smart 	 * For LPFC_NVMET_XBUSY, lpfc_sli4_nvmet_xri_aborted
300286c67379SJames Smart 	 * should have been called already.
300386c67379SJames Smart 	 */
3004d613b6a7SJames Smart }
3005d613b6a7SJames Smart 
3006d613b6a7SJames Smart /**
300786c67379SJames Smart  * lpfc_nvmet_unsol_fcp_abort_cmp - Completion handler for ABTS
3008d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3009d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3010d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
3011d613b6a7SJames Smart  *
3012d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3013d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
3014d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3015d613b6a7SJames Smart  **/
3016d613b6a7SJames Smart static void
301786c67379SJames Smart lpfc_nvmet_unsol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3018d613b6a7SJames Smart 			       struct lpfc_wcqe_complete *wcqe)
3019d613b6a7SJames Smart {
3020d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
3021d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
302219b58d94SJames Smart 	unsigned long flags;
3023b27cbd55SBart Van Assche 	uint32_t result;
302419b58d94SJames Smart 	bool released = false;
3025d613b6a7SJames Smart 
3026d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
3027d613b6a7SJames Smart 	result = wcqe->parameter;
3028d613b6a7SJames Smart 
302986c67379SJames Smart 	if (!ctxp) {
303086c67379SJames Smart 		/* if context is clear, related io alrady complete */
3031d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
303286c67379SJames Smart 				"6070 ABTS cmpl: WCQE: %08x %08x %08x %08x\n",
303386c67379SJames Smart 				wcqe->word0, wcqe->total_data_placed,
3034d613b6a7SJames Smart 				result, wcqe->word3);
303586c67379SJames Smart 		return;
303686c67379SJames Smart 	}
3037d613b6a7SJames Smart 
303878e1d200SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3039c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
304078e1d200SJames Smart 	if (ctxp->flag & LPFC_NVMET_ABORT_OP)
304178e1d200SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
304278e1d200SJames Smart 
3043d613b6a7SJames Smart 	/* Sanity check */
3044d613b6a7SJames Smart 	if (ctxp->state != LPFC_NVMET_STE_ABORT) {
3045d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
304686c67379SJames Smart 				"6112 ABTS Wrong state:%d oxid x%x\n",
3047d613b6a7SJames Smart 				ctxp->state, ctxp->oxid);
3048d613b6a7SJames Smart 	}
304986c67379SJames Smart 
305086c67379SJames Smart 	/* Check if we already received a free context call
305186c67379SJames Smart 	 * and we have completed processing an abort situation.
305286c67379SJames Smart 	 */
3053d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_DONE;
305486c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_CTX_RLS) &&
305586c67379SJames Smart 	    !(ctxp->flag & LPFC_NVMET_XBUSY)) {
3056c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
305779d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3058c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
305919b58d94SJames Smart 		released = true;
306086c67379SJames Smart 	}
306119b58d94SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
306219b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3063547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
306419b58d94SJames Smart 
306586c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
306679d8c4ceSJames Smart 			"6316 ABTS cmpl oxid x%x flg x%x (%x) "
306786c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
306886c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
306986c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
307086c67379SJames Smart 			result, wcqe->word3);
30716c621a22SJames Smart 
30726c621a22SJames Smart 	cmdwqe->context2 = NULL;
30736c621a22SJames Smart 	cmdwqe->context3 = NULL;
307419b58d94SJames Smart 	/*
307519b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
307619b58d94SJames Smart 	 * will be recycled by transport release call.
307719b58d94SJames Smart 	 */
307819b58d94SJames Smart 	if (released)
30796c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
308086c67379SJames Smart 
308186c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
308286c67379SJames Smart 	 * For LPFC_NVMET_XBUSY, lpfc_sli4_nvmet_xri_aborted
308386c67379SJames Smart 	 * should have been called already.
308486c67379SJames Smart 	 */
3085d613b6a7SJames Smart }
3086d613b6a7SJames Smart 
3087d613b6a7SJames Smart /**
3088d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
3089d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3090d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3091d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
3092d613b6a7SJames Smart  *
3093d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3094d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for LS cmds
3095d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3096d613b6a7SJames Smart  **/
3097d613b6a7SJames Smart static void
3098d613b6a7SJames Smart lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3099d613b6a7SJames Smart 			    struct lpfc_wcqe_complete *wcqe)
3100d613b6a7SJames Smart {
3101d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
3102d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3103b27cbd55SBart Van Assche 	uint32_t result;
3104d613b6a7SJames Smart 
3105d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
3106d613b6a7SJames Smart 	result = wcqe->parameter;
3107d613b6a7SJames Smart 
3108d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3109547077a4SJames Smart 	atomic_inc(&tgtp->xmt_ls_abort_cmpl);
3110d613b6a7SJames Smart 
3111d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
311232350664SJames Smart 			"6083 Abort cmpl: ctx x%px WCQE:%08x %08x %08x %08x\n",
3113d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
3114d613b6a7SJames Smart 			result, wcqe->word3);
3115d613b6a7SJames Smart 
3116ce1b591cSJames Smart 	if (!ctxp) {
3117ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3118ce1b591cSJames Smart 				"6415 NVMET LS Abort No ctx: WCQE: "
3119ce1b591cSJames Smart 				 "%08x %08x %08x %08x\n",
3120ce1b591cSJames Smart 				wcqe->word0, wcqe->total_data_placed,
3121ce1b591cSJames Smart 				result, wcqe->word3);
3122ce1b591cSJames Smart 
3123ce1b591cSJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
3124ce1b591cSJames Smart 		return;
3125ce1b591cSJames Smart 	}
3126ce1b591cSJames Smart 
3127ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_LS_ABORT) {
3128ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3129ce1b591cSJames Smart 				"6416 NVMET LS abort cmpl state mismatch: "
3130ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
3131ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3132ce1b591cSJames Smart 	}
3133ce1b591cSJames Smart 
3134d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
3135d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
3136d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3137d613b6a7SJames Smart 	kfree(ctxp);
3138d613b6a7SJames Smart }
3139d613b6a7SJames Smart 
3140d613b6a7SJames Smart static int
3141d613b6a7SJames Smart lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
3142d613b6a7SJames Smart 			     struct lpfc_nvmet_rcv_ctx *ctxp,
3143d613b6a7SJames Smart 			     uint32_t sid, uint16_t xri)
3144d613b6a7SJames Smart {
3145d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3146d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3147205e8240SJames Smart 	union lpfc_wqe128 *wqe_abts;
3148d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3149d613b6a7SJames Smart 
3150d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
315186c67379SJames Smart 			"6067 ABTS: sid %x xri x%x/x%x\n",
3152318083adSJames Smart 			sid, xri, ctxp->wqeq->sli4_xritag);
3153d613b6a7SJames Smart 
3154d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3155d613b6a7SJames Smart 
3156d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3157d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3158d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3159d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3160d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3161ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3162d613b6a7SJames Smart 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
3163b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3164d613b6a7SJames Smart 
3165d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3166d613b6a7SJames Smart 		return 0;
3167d613b6a7SJames Smart 	}
3168d613b6a7SJames Smart 
3169d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
3170d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
3171d613b6a7SJames Smart 
3172d613b6a7SJames Smart 	/*
3173d613b6a7SJames Smart 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
3174d613b6a7SJames Smart 	 * that were initialized in lpfc_sli4_nvmet_alloc.
3175d613b6a7SJames Smart 	 */
3176d613b6a7SJames Smart 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
3177d613b6a7SJames Smart 
3178d613b6a7SJames Smart 	/* Word 5 */
3179d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
3180d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
3181d613b6a7SJames Smart 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
3182d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
3183d613b6a7SJames Smart 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
3184d613b6a7SJames Smart 
3185d613b6a7SJames Smart 	/* Word 6 */
3186d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
3187d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
3188d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
3189d613b6a7SJames Smart 	       abts_wqeq->sli4_xritag);
3190d613b6a7SJames Smart 
3191d613b6a7SJames Smart 	/* Word 7 */
3192d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
3193d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
3194d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
3195d613b6a7SJames Smart 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
3196d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
3197d613b6a7SJames Smart 
3198d613b6a7SJames Smart 	/* Word 8 */
3199d613b6a7SJames Smart 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
3200d613b6a7SJames Smart 
3201d613b6a7SJames Smart 	/* Word 9 */
3202d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
3203d613b6a7SJames Smart 	/* Needs to be set by caller */
3204d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
3205d613b6a7SJames Smart 
3206d613b6a7SJames Smart 	/* Word 10 */
3207d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe_abts->xmit_sequence.wqe_com, 1);
3208d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
3209d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
3210d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
3211d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
3212d613b6a7SJames Smart 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
3213d613b6a7SJames Smart 
3214d613b6a7SJames Smart 	/* Word 11 */
3215d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
3216d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
3217d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
3218d613b6a7SJames Smart 	       OTHER_COMMAND);
3219d613b6a7SJames Smart 
3220d613b6a7SJames Smart 	abts_wqeq->vport = phba->pport;
3221d613b6a7SJames Smart 	abts_wqeq->context1 = ndlp;
3222d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
3223d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
3224d613b6a7SJames Smart 	abts_wqeq->rsvd2 = 0;
3225d613b6a7SJames Smart 	/* hba_wqidx should already be setup from command we are aborting */
3226d613b6a7SJames Smart 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
3227d613b6a7SJames Smart 	abts_wqeq->iocb.ulpLe = 1;
3228d613b6a7SJames Smart 
3229d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
3230d613b6a7SJames Smart 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
3231d613b6a7SJames Smart 			xri, abts_wqeq->iotag);
3232d613b6a7SJames Smart 	return 1;
3233d613b6a7SJames Smart }
3234d613b6a7SJames Smart 
3235d613b6a7SJames Smart static int
3236d613b6a7SJames Smart lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
3237d613b6a7SJames Smart 			       struct lpfc_nvmet_rcv_ctx *ctxp,
3238d613b6a7SJames Smart 			       uint32_t sid, uint16_t xri)
3239d613b6a7SJames Smart {
3240d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3241d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3242d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3243d613b6a7SJames Smart 	unsigned long flags;
324451f8e43eSJames Smart 	u8 opt;
3245d613b6a7SJames Smart 	int rc;
3246d613b6a7SJames Smart 
3247d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3248d613b6a7SJames Smart 	if (!ctxp->wqeq) {
32496c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3250d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3251d613b6a7SJames Smart 	}
3252d613b6a7SJames Smart 
3253d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3254d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3255d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3256d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3257d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3258ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
325986c67379SJames Smart 				"6160 Drop ABORT - wrong NDLP state x%x.\n",
3260b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3261d613b6a7SJames Smart 
3262d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3263c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
326486c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3265c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3266d613b6a7SJames Smart 		return 0;
3267d613b6a7SJames Smart 	}
3268d613b6a7SJames Smart 
3269d613b6a7SJames Smart 	/* Issue ABTS for this WQE based on iotag */
3270d613b6a7SJames Smart 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
3271c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3272d613b6a7SJames Smart 	if (!ctxp->abort_wqeq) {
3273547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3274ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
327586c67379SJames Smart 				"6161 ABORT failed: No wqeqs: "
3276d613b6a7SJames Smart 				"xri: x%x\n", ctxp->oxid);
3277d613b6a7SJames Smart 		/* No failure to an ABTS request. */
327886c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3279c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3280d613b6a7SJames Smart 		return 0;
3281d613b6a7SJames Smart 	}
3282d613b6a7SJames Smart 	abts_wqeq = ctxp->abort_wqeq;
3283d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
328451f8e43eSJames Smart 	opt = (ctxp->flag & LPFC_NVMET_ABTS_RCV) ? INHIBIT_ABORT : 0;
3285c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3286d613b6a7SJames Smart 
3287d613b6a7SJames Smart 	/* Announce entry to new IO submit field. */
328886c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
328986c67379SJames Smart 			"6162 ABORT Request to rport DID x%06x "
3290d613b6a7SJames Smart 			"for xri x%x x%x\n",
3291d613b6a7SJames Smart 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
3292d613b6a7SJames Smart 
3293d613b6a7SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
3294d613b6a7SJames Smart 	 * cleared when the reset is complete and rings reestablished.
3295d613b6a7SJames Smart 	 */
3296d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3297d613b6a7SJames Smart 	/* driver queued commands are in process of being flushed */
3298c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
3299d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3300547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3301d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
3302d613b6a7SJames Smart 				"6163 Driver in reset cleanup - flushing "
3303d613b6a7SJames Smart 				"NVME Req now. hba_flag x%x oxid x%x\n",
3304d613b6a7SJames Smart 				phba->hba_flag, ctxp->oxid);
3305d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3306c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
330786c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3308c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3309d613b6a7SJames Smart 		return 0;
3310d613b6a7SJames Smart 	}
3311d613b6a7SJames Smart 
3312d613b6a7SJames Smart 	/* Outstanding abort is in progress */
3313d613b6a7SJames Smart 	if (abts_wqeq->iocb_flag & LPFC_DRIVER_ABORTED) {
3314d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3315547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3316d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
3317d613b6a7SJames Smart 				"6164 Outstanding NVME I/O Abort Request "
3318d613b6a7SJames Smart 				"still pending on oxid x%x\n",
3319d613b6a7SJames Smart 				ctxp->oxid);
3320d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3321c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
332286c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3323c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3324d613b6a7SJames Smart 		return 0;
3325d613b6a7SJames Smart 	}
3326d613b6a7SJames Smart 
3327d613b6a7SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
3328d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_DRIVER_ABORTED;
3329d613b6a7SJames Smart 
333051f8e43eSJames Smart 	lpfc_nvme_prep_abort_wqe(abts_wqeq, ctxp->wqeq->sli4_xritag, opt);
3331d613b6a7SJames Smart 
3332d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
3333d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
3334d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
3335d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
3336d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVME;
3337d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
33384550f9c7SJames Smart 	abts_wqeq->vport = phba->pport;
33391fbf9742SJames Smart 	if (!ctxp->hdwq)
33401fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
33411fbf9742SJames Smart 
33421fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3343d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3344547077a4SJames Smart 	if (rc == WQE_SUCCESS) {
3345547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_sol);
3346d613b6a7SJames Smart 		return 0;
3347547077a4SJames Smart 	}
3348d613b6a7SJames Smart 
3349547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3350c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
335186c67379SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3352c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3353d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
335486c67379SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
335586c67379SJames Smart 			"6166 Failed ABORT issue_wqe with status x%x "
3356d613b6a7SJames Smart 			"for oxid x%x.\n",
3357d613b6a7SJames Smart 			rc, ctxp->oxid);
3358d613b6a7SJames Smart 	return 1;
3359d613b6a7SJames Smart }
3360d613b6a7SJames Smart 
3361d613b6a7SJames Smart static int
3362d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
3363d613b6a7SJames Smart 				 struct lpfc_nvmet_rcv_ctx *ctxp,
3364d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
3365d613b6a7SJames Smart {
3366d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3367d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3368d613b6a7SJames Smart 	unsigned long flags;
3369c160c0f8SJames Smart 	bool released = false;
3370d613b6a7SJames Smart 	int rc;
3371d613b6a7SJames Smart 
3372d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3373d613b6a7SJames Smart 	if (!ctxp->wqeq) {
33746c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3375d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3376d613b6a7SJames Smart 	}
3377d613b6a7SJames Smart 
3378ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_FREE) {
3379ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3380ce1b591cSJames Smart 				"6417 NVMET ABORT ctx freed %d %d oxid x%x\n",
3381ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
3382ce1b591cSJames Smart 		rc = WQE_BUSY;
3383ce1b591cSJames Smart 		goto aerr;
3384ce1b591cSJames Smart 	}
3385ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
3386ce1b591cSJames Smart 	ctxp->entry_cnt++;
3387d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
3388d613b6a7SJames Smart 	if (rc == 0)
3389d613b6a7SJames Smart 		goto aerr;
3390d613b6a7SJames Smart 
3391d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3392d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
339386c67379SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_unsol_fcp_abort_cmp;
339486c67379SJames Smart 	abts_wqeq->iocb_cmpl = NULL;
3395d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVMET;
33961fbf9742SJames Smart 	if (!ctxp->hdwq)
33971fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
33981fbf9742SJames Smart 
33991fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3400d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3401d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3402d613b6a7SJames Smart 		return 0;
3403d613b6a7SJames Smart 	}
3404d613b6a7SJames Smart 
3405d613b6a7SJames Smart aerr:
340668c9b55dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3407c160c0f8SJames Smart 	if (ctxp->flag & LPFC_NVMET_CTX_RLS) {
3408c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
340979d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3410c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
3411c160c0f8SJames Smart 		released = true;
3412c160c0f8SJames Smart 	}
341368c9b55dSJames Smart 	ctxp->flag &= ~(LPFC_NVMET_ABORT_OP | LPFC_NVMET_CTX_RLS);
341468c9b55dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
341568c9b55dSJames Smart 
3416d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3417ce1b591cSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
341879d8c4ceSJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x "
341979d8c4ceSJames Smart 			"(%x)\n",
342079d8c4ceSJames Smart 			ctxp->oxid, rc, released);
3421c160c0f8SJames Smart 	if (released)
342268c9b55dSJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3423d613b6a7SJames Smart 	return 1;
3424d613b6a7SJames Smart }
3425d613b6a7SJames Smart 
3426d613b6a7SJames Smart static int
3427d613b6a7SJames Smart lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *phba,
3428d613b6a7SJames Smart 				struct lpfc_nvmet_rcv_ctx *ctxp,
3429d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
3430d613b6a7SJames Smart {
3431d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3432d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3433d613b6a7SJames Smart 	unsigned long flags;
3434d613b6a7SJames Smart 	int rc;
3435d613b6a7SJames Smart 
3436ce1b591cSJames Smart 	if ((ctxp->state == LPFC_NVMET_STE_LS_RCV && ctxp->entry_cnt == 1) ||
3437ce1b591cSJames Smart 	    (ctxp->state == LPFC_NVMET_STE_LS_RSP && ctxp->entry_cnt == 2)) {
3438ce1b591cSJames Smart 		ctxp->state = LPFC_NVMET_STE_LS_ABORT;
3439ce1b591cSJames Smart 		ctxp->entry_cnt++;
3440ce1b591cSJames Smart 	} else {
3441ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3442ce1b591cSJames Smart 				"6418 NVMET LS abort state mismatch "
3443ce1b591cSJames Smart 				"IO x%x: %d %d\n",
3444ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3445ce1b591cSJames Smart 		ctxp->state = LPFC_NVMET_STE_LS_ABORT;
3446ce1b591cSJames Smart 	}
3447ce1b591cSJames Smart 
3448d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3449d613b6a7SJames Smart 	if (!ctxp->wqeq) {
3450d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
3451d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
3452d613b6a7SJames Smart 		if (!ctxp->wqeq) {
3453ce1b591cSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3454d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
3455d613b6a7SJames Smart 					"xri: x%x\n", xri);
3456d613b6a7SJames Smart 			/* No failure to an ABTS request. */
3457d613b6a7SJames Smart 			kfree(ctxp);
3458d613b6a7SJames Smart 			return 0;
3459d613b6a7SJames Smart 		}
3460d613b6a7SJames Smart 	}
3461d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
34626c621a22SJames Smart 
3463ce1b591cSJames Smart 	if (lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri) == 0) {
3464ce1b591cSJames Smart 		rc = WQE_BUSY;
3465ce1b591cSJames Smart 		goto out;
3466ce1b591cSJames Smart 	}
3467d613b6a7SJames Smart 
3468d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3469d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
3470d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
3471d613b6a7SJames Smart 	abts_wqeq->iocb_flag |=  LPFC_IO_NVME_LS;
34721fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3473d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3474d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3475547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_unsol);
3476d613b6a7SJames Smart 		return 0;
3477d613b6a7SJames Smart 	}
3478ce1b591cSJames Smart out:
3479d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3480d613b6a7SJames Smart 	abts_wqeq->context2 = NULL;
3481d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
3482d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3483d613b6a7SJames Smart 	kfree(ctxp);
3484ce1b591cSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3485d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
3486d613b6a7SJames Smart 	return 0;
3487d613b6a7SJames Smart }
3488