xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision db7531d2)
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 
39d613b6a7SJames Smart #include "lpfc_version.h"
40d613b6a7SJames Smart #include "lpfc_hw4.h"
41d613b6a7SJames Smart #include "lpfc_hw.h"
42d613b6a7SJames Smart #include "lpfc_sli.h"
43d613b6a7SJames Smart #include "lpfc_sli4.h"
44d613b6a7SJames Smart #include "lpfc_nl.h"
45d613b6a7SJames Smart #include "lpfc_disc.h"
46d613b6a7SJames Smart #include "lpfc.h"
47d613b6a7SJames Smart #include "lpfc_scsi.h"
48d613b6a7SJames Smart #include "lpfc_nvme.h"
49d613b6a7SJames Smart #include "lpfc_logmsg.h"
50d613b6a7SJames Smart #include "lpfc_crtn.h"
51d613b6a7SJames Smart #include "lpfc_vport.h"
522b65e182SJames Smart #include "lpfc_debugfs.h"
53d613b6a7SJames Smart 
54d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *,
557cacae2aSJames Smart 						 struct lpfc_async_xchg_ctx *,
56d613b6a7SJames Smart 						 dma_addr_t rspbuf,
57d613b6a7SJames Smart 						 uint16_t rspsize);
58d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *,
597cacae2aSJames Smart 						  struct lpfc_async_xchg_ctx *);
60d613b6a7SJames Smart static int lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *,
617cacae2aSJames Smart 					  struct lpfc_async_xchg_ctx *,
62d613b6a7SJames Smart 					  uint32_t, uint16_t);
63d613b6a7SJames Smart static int lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *,
647cacae2aSJames Smart 					    struct lpfc_async_xchg_ctx *,
65d613b6a7SJames Smart 					    uint32_t, uint16_t);
666e8e1c14SJames Smart static void lpfc_nvmet_wqfull_flush(struct lpfc_hba *, struct lpfc_queue *,
677cacae2aSJames Smart 				    struct lpfc_async_xchg_ctx *);
68472e146dSJames Smart static void lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *);
69472e146dSJames Smart 
70472e146dSJames Smart static void lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf);
71d613b6a7SJames Smart 
72fab2e466SColin Ian King static union lpfc_wqe128 lpfc_tsend_cmd_template;
73fab2e466SColin Ian King static union lpfc_wqe128 lpfc_treceive_cmd_template;
74fab2e466SColin Ian King static union lpfc_wqe128 lpfc_trsp_cmd_template;
75bd3061baSJames Smart 
76bd3061baSJames Smart /* Setup WQE templates for NVME IOs */
77bd3061baSJames Smart void
78fab2e466SColin Ian King lpfc_nvmet_cmd_template(void)
79bd3061baSJames Smart {
80bd3061baSJames Smart 	union lpfc_wqe128 *wqe;
81bd3061baSJames Smart 
82bd3061baSJames Smart 	/* TSEND template */
83bd3061baSJames Smart 	wqe = &lpfc_tsend_cmd_template;
84bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
85bd3061baSJames Smart 
86bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
87bd3061baSJames Smart 
88bd3061baSJames Smart 	/* Word 3 - payload_offset_len is zero */
89bd3061baSJames Smart 
90bd3061baSJames Smart 	/* Word 4 - relative_offset is variable */
91bd3061baSJames Smart 
92bd3061baSJames Smart 	/* Word 5 - is zero */
93bd3061baSJames Smart 
94bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
95bd3061baSJames Smart 
96bd3061baSJames Smart 	/* Word 7 - wqe_ar is variable */
97bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_tsend.wqe_com, CMD_FCP_TSEND64_WQE);
98bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_tsend.wqe_com, PARM_REL_OFF);
99bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_tsend.wqe_com, CLASS3);
100bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_tsend.wqe_com, SLI4_CT_RPI);
101bd3061baSJames Smart 	bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 1);
102bd3061baSJames Smart 
103bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
104bd3061baSJames Smart 
105bd3061baSJames Smart 	/* Word 9  - reqtag, rcvoxid is variable */
106bd3061baSJames Smart 
107bd3061baSJames Smart 	/* Word 10 - wqes, xc is variable */
108b101eb27SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_tsend.wqe_com, LPFC_NVME_XCHG);
109bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_tsend.wqe_com, 1);
110bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
111bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 1);
112bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_tsend.wqe_com, LPFC_WQE_IOD_WRITE);
113bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_tsend.wqe_com, LPFC_WQE_LENLOC_WORD12);
114bd3061baSJames Smart 
115bd3061baSJames Smart 	/* Word 11 - sup, irsp, irsplen is variable */
116bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_tsend.wqe_com, FCP_COMMAND_TSEND);
117bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_tsend.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
118bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
119bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
120bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
121bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_tsend.wqe_com, 0);
122bd3061baSJames Smart 
123bd3061baSJames Smart 	/* Word 12 - fcp_data_len is variable */
124bd3061baSJames Smart 
125bd3061baSJames Smart 	/* Word 13, 14, 15 - PBDE is zero */
126bd3061baSJames Smart 
127bd3061baSJames Smart 	/* TRECEIVE template */
128bd3061baSJames Smart 	wqe = &lpfc_treceive_cmd_template;
129bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
130bd3061baSJames Smart 
131bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
132bd3061baSJames Smart 
133bd3061baSJames Smart 	/* Word 3 */
134bd3061baSJames Smart 	wqe->fcp_treceive.payload_offset_len = TXRDY_PAYLOAD_LEN;
135bd3061baSJames Smart 
136bd3061baSJames Smart 	/* Word 4 - relative_offset is variable */
137bd3061baSJames Smart 
138bd3061baSJames Smart 	/* Word 5 - is zero */
139bd3061baSJames Smart 
140bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
141bd3061baSJames Smart 
142bd3061baSJames Smart 	/* Word 7 */
143bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_treceive.wqe_com, CMD_FCP_TRECEIVE64_WQE);
144bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_treceive.wqe_com, PARM_REL_OFF);
145bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_treceive.wqe_com, CLASS3);
146bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_treceive.wqe_com, SLI4_CT_RPI);
147bd3061baSJames Smart 	bf_set(wqe_ar, &wqe->fcp_treceive.wqe_com, 0);
148bd3061baSJames Smart 
149bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
150bd3061baSJames Smart 
151bd3061baSJames Smart 	/* Word 9  - reqtag, rcvoxid is variable */
152bd3061baSJames Smart 
153bd3061baSJames Smart 	/* Word 10 - xc is variable */
154bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_treceive.wqe_com, 1);
155bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_treceive.wqe_com, 0);
156b101eb27SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_treceive.wqe_com, LPFC_NVME_XCHG);
157bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_treceive.wqe_com, LPFC_WQE_IOD_READ);
158bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_treceive.wqe_com, LPFC_WQE_LENLOC_WORD12);
159bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 1);
160bd3061baSJames Smart 
161bd3061baSJames Smart 	/* Word 11 - pbde is variable */
162bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_treceive.wqe_com, FCP_COMMAND_TRECEIVE);
163bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_treceive.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
164bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_treceive.wqe_com, 0);
165bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_treceive.wqe_com, 0);
166bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_treceive.wqe_com, 0);
167bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 1);
168bd3061baSJames Smart 
169bd3061baSJames Smart 	/* Word 12 - fcp_data_len is variable */
170bd3061baSJames Smart 
171bd3061baSJames Smart 	/* Word 13, 14, 15 - PBDE is variable */
172bd3061baSJames Smart 
173bd3061baSJames Smart 	/* TRSP template */
174bd3061baSJames Smart 	wqe = &lpfc_trsp_cmd_template;
175bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
176bd3061baSJames Smart 
177bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
178bd3061baSJames Smart 
179bd3061baSJames Smart 	/* Word 3 - response_len is variable */
180bd3061baSJames Smart 
181bd3061baSJames Smart 	/* Word 4, 5 - is zero */
182bd3061baSJames Smart 
183bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
184bd3061baSJames Smart 
185bd3061baSJames Smart 	/* Word 7 */
186bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_trsp.wqe_com, CMD_FCP_TRSP64_WQE);
187bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_trsp.wqe_com, PARM_UNUSED);
188bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_trsp.wqe_com, CLASS3);
189bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_trsp.wqe_com, SLI4_CT_RPI);
190bd3061baSJames Smart 	bf_set(wqe_ag, &wqe->fcp_trsp.wqe_com, 1); /* wqe_ar */
191bd3061baSJames Smart 
192bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
193bd3061baSJames Smart 
194bd3061baSJames Smart 	/* Word 9  - reqtag is variable */
195bd3061baSJames Smart 
196bd3061baSJames Smart 	/* Word 10 wqes, xc is variable */
197bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_trsp.wqe_com, 1);
198b101eb27SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_trsp.wqe_com, LPFC_NVME_XCHG);
199bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 0);
200bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 0);
201bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_trsp.wqe_com, LPFC_WQE_IOD_NONE);
202bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_trsp.wqe_com, LPFC_WQE_LENLOC_WORD3);
203bd3061baSJames Smart 
204bd3061baSJames Smart 	/* Word 11 irsp, irsplen is variable */
205bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_trsp.wqe_com, FCP_COMMAND_TRSP);
206bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_trsp.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
207bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_trsp.wqe_com, 0);
208bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 0);
209bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com, 0);
210bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_trsp.wqe_com, 0);
211bd3061baSJames Smart 
212bd3061baSJames Smart 	/* Word 12, 13, 14, 15 - is zero */
213bd3061baSJames Smart }
214bd3061baSJames Smart 
215336df6ebSNathan Chancellor #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2167cacae2aSJames Smart static struct lpfc_async_xchg_ctx *
21779d8c4ceSJames Smart lpfc_nvmet_get_ctx_for_xri(struct lpfc_hba *phba, u16 xri)
21879d8c4ceSJames Smart {
2197cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
22079d8c4ceSJames Smart 	unsigned long iflag;
22179d8c4ceSJames Smart 	bool found = false;
22279d8c4ceSJames Smart 
22379d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
22479d8c4ceSJames Smart 	list_for_each_entry(ctxp, &phba->sli4_hba.t_active_ctx_list, list) {
22579d8c4ceSJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
22679d8c4ceSJames Smart 			continue;
22779d8c4ceSJames Smart 
22879d8c4ceSJames Smart 		found = true;
22979d8c4ceSJames Smart 		break;
23079d8c4ceSJames Smart 	}
23179d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
23279d8c4ceSJames Smart 	if (found)
23379d8c4ceSJames Smart 		return ctxp;
23479d8c4ceSJames Smart 
23579d8c4ceSJames Smart 	return NULL;
23679d8c4ceSJames Smart }
23779d8c4ceSJames Smart 
2387cacae2aSJames Smart static struct lpfc_async_xchg_ctx *
23979d8c4ceSJames Smart lpfc_nvmet_get_ctx_for_oxid(struct lpfc_hba *phba, u16 oxid, u32 sid)
24079d8c4ceSJames Smart {
2417cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
24279d8c4ceSJames Smart 	unsigned long iflag;
24379d8c4ceSJames Smart 	bool found = false;
24479d8c4ceSJames Smart 
24579d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
24679d8c4ceSJames Smart 	list_for_each_entry(ctxp, &phba->sli4_hba.t_active_ctx_list, list) {
24779d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
24879d8c4ceSJames Smart 			continue;
24979d8c4ceSJames Smart 
25079d8c4ceSJames Smart 		found = true;
25179d8c4ceSJames Smart 		break;
25279d8c4ceSJames Smart 	}
25379d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
25479d8c4ceSJames Smart 	if (found)
25579d8c4ceSJames Smart 		return ctxp;
25679d8c4ceSJames Smart 
25779d8c4ceSJames Smart 	return NULL;
25879d8c4ceSJames Smart }
259336df6ebSNathan Chancellor #endif
26079d8c4ceSJames Smart 
2613999df75SBart Van Assche static void
2627cacae2aSJames Smart lpfc_nvmet_defer_release(struct lpfc_hba *phba,
2637cacae2aSJames Smart 			struct lpfc_async_xchg_ctx *ctxp)
26486c67379SJames Smart {
265472e146dSJames Smart 	lockdep_assert_held(&ctxp->ctxlock);
26686c67379SJames Smart 
267e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
26879d8c4ceSJames Smart 			"6313 NVMET Defer ctx release oxid x%x flg x%x\n",
26986c67379SJames Smart 			ctxp->oxid, ctxp->flag);
27086c67379SJames Smart 
2717b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_CTX_RLS)
27286c67379SJames Smart 		return;
273472e146dSJames Smart 
2747b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_CTX_RLS;
27579d8c4ceSJames Smart 	spin_lock(&phba->sli4_hba.t_active_list_lock);
27679d8c4ceSJames Smart 	list_del(&ctxp->list);
27779d8c4ceSJames Smart 	spin_unlock(&phba->sli4_hba.t_active_list_lock);
278472e146dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
27986c67379SJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
280472e146dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
28186c67379SJames Smart }
28286c67379SJames Smart 
283d613b6a7SJames Smart /**
284fe1bedecSJames Smart  * __lpfc_nvme_xmt_ls_rsp_cmp - Generic completion handler for the
285fe1bedecSJames Smart  *         transmission of an NVME LS response.
286fe1bedecSJames Smart  * @phba: Pointer to HBA context object.
287fe1bedecSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
288fe1bedecSJames Smart  * @wcqe: Pointer to driver response CQE object.
289fe1bedecSJames Smart  *
290fe1bedecSJames Smart  * The function is called from SLI ring event handler with no
291fe1bedecSJames Smart  * lock held. The function frees memory resources used for the command
292fe1bedecSJames Smart  * used to send the NVME LS RSP.
293fe1bedecSJames Smart  **/
294fe1bedecSJames Smart void
295fe1bedecSJames Smart __lpfc_nvme_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
296fe1bedecSJames Smart 			   struct lpfc_wcqe_complete *wcqe)
297fe1bedecSJames Smart {
298fe1bedecSJames Smart 	struct lpfc_async_xchg_ctx *axchg = cmdwqe->context2;
299fe1bedecSJames Smart 	struct nvmefc_ls_rsp *ls_rsp = &axchg->ls_rsp;
300fe1bedecSJames Smart 	uint32_t status, result;
301fe1bedecSJames Smart 
302fe1bedecSJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
303fe1bedecSJames Smart 	result = wcqe->parameter;
304fe1bedecSJames Smart 
305fe1bedecSJames Smart 	if (axchg->state != LPFC_NVME_STE_LS_RSP || axchg->entry_cnt != 2) {
306372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
307fe1bedecSJames Smart 				"6410 NVMEx LS cmpl state mismatch IO x%x: "
308fe1bedecSJames Smart 				"%d %d\n",
309fe1bedecSJames Smart 				axchg->oxid, axchg->state, axchg->entry_cnt);
310fe1bedecSJames Smart 	}
311fe1bedecSJames Smart 
312fe1bedecSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS  CMPL: xri x%x stat x%x result x%x\n",
313fe1bedecSJames Smart 			 axchg->oxid, status, result);
314fe1bedecSJames Smart 
315fe1bedecSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
316fe1bedecSJames Smart 			"6038 NVMEx LS rsp cmpl: %d %d oxid x%x\n",
317fe1bedecSJames Smart 			status, result, axchg->oxid);
318fe1bedecSJames Smart 
319fe1bedecSJames Smart 	lpfc_nlp_put(cmdwqe->context1);
320fe1bedecSJames Smart 	cmdwqe->context2 = NULL;
321fe1bedecSJames Smart 	cmdwqe->context3 = NULL;
322fe1bedecSJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
323fe1bedecSJames Smart 	ls_rsp->done(ls_rsp);
324fe1bedecSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
325fe1bedecSJames Smart 			"6200 NVMEx LS rsp cmpl done status %d oxid x%x\n",
326fe1bedecSJames Smart 			status, axchg->oxid);
327fe1bedecSJames Smart 	kfree(axchg);
328fe1bedecSJames Smart }
329fe1bedecSJames Smart 
330fe1bedecSJames Smart /**
331d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
332d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
333d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
334d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
335d613b6a7SJames Smart  *
336d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
337d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME LS commands
338fe1bedecSJames Smart  * The function updates any states and statistics, then calls the
339fe1bedecSJames Smart  * generic completion handler to free resources.
340d613b6a7SJames Smart  **/
341d613b6a7SJames Smart static void
342d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
343d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
344d613b6a7SJames Smart {
345d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
346d613b6a7SJames Smart 	uint32_t status, result;
347d613b6a7SJames Smart 
348d613b6a7SJames Smart 	if (!phba->targetport)
349fe1bedecSJames Smart 		goto finish;
350fe1bedecSJames Smart 
351fe1bedecSJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
352fe1bedecSJames Smart 	result = wcqe->parameter;
353d613b6a7SJames Smart 
354d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3554b056682SJames Smart 	if (tgtp) {
3564b056682SJames Smart 		if (status) {
357d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_error);
3588ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
3594b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_aborted);
3604b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
3614b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_xb_set);
3624b056682SJames Smart 		} else {
363d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
3644b056682SJames Smart 		}
3654b056682SJames Smart 	}
366d613b6a7SJames Smart 
367fe1bedecSJames Smart finish:
368fe1bedecSJames Smart 	__lpfc_nvme_xmt_ls_rsp_cmp(phba, cmdwqe, wcqe);
369d613b6a7SJames Smart }
370d613b6a7SJames Smart 
371d613b6a7SJames Smart /**
3726c621a22SJames Smart  * lpfc_nvmet_ctxbuf_post - Repost a NVMET RQ DMA buffer and clean up context
373d613b6a7SJames Smart  * @phba: HBA buffer is associated with
374d3cb79fcSLee Jones  * @ctx_buf: ctx buffer context
375d613b6a7SJames Smart  *
376d613b6a7SJames Smart  * Description: Frees the given DMA buffer in the appropriate way given by
377d613b6a7SJames Smart  * reposting it to its associated RQ so it can be reused.
378d613b6a7SJames Smart  *
379d613b6a7SJames Smart  * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
380d613b6a7SJames Smart  *
381d613b6a7SJames Smart  * Returns: None
382d613b6a7SJames Smart  **/
383d613b6a7SJames Smart void
3846c621a22SJames Smart lpfc_nvmet_ctxbuf_post(struct lpfc_hba *phba, struct lpfc_nvmet_ctxbuf *ctx_buf)
385d613b6a7SJames Smart {
386eeeb51d8SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
3877cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp = ctx_buf->context;
388a8cf5dfeSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
389a8cf5dfeSJames Smart 	struct fc_frame_header *fc_hdr;
390a8cf5dfeSJames Smart 	struct rqb_dmabuf *nvmebuf;
39166d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
392472e146dSJames Smart 	uint32_t size, oxid, sid;
39366d7ce93SDick Kennedy 	int cpu;
3946c621a22SJames Smart 	unsigned long iflag;
39586c67379SJames Smart 
3967b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_FREE) {
397372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
398ce1b591cSJames Smart 				"6411 NVMET free, already free IO x%x: %d %d\n",
399ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
400ce1b591cSJames Smart 	}
401472e146dSJames Smart 
402472e146dSJames Smart 	if (ctxp->rqb_buffer) {
403472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
4044767c58aSJames Smart 		nvmebuf = ctxp->rqb_buffer;
4054767c58aSJames Smart 		/* check if freed in another path whilst acquiring lock */
4064767c58aSJames Smart 		if (nvmebuf) {
407472e146dSJames Smart 			ctxp->rqb_buffer = NULL;
4087b7f551bSJames Smart 			if (ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) {
4097b7f551bSJames Smart 				ctxp->flag &= ~LPFC_NVME_CTX_REUSE_WQ;
410472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4114767c58aSJames Smart 				nvmebuf->hrq->rqbp->rqb_free_buffer(phba,
4124767c58aSJames Smart 								    nvmebuf);
413472e146dSJames Smart 			} else {
414472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4154767c58aSJames Smart 				/* repost */
4164767c58aSJames Smart 				lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
4174767c58aSJames Smart 			}
4184767c58aSJames Smart 		} else {
4194767c58aSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
420472e146dSJames Smart 		}
421472e146dSJames Smart 	}
4227b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_FREE;
4236c621a22SJames Smart 
424a8cf5dfeSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
425a8cf5dfeSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
426a8cf5dfeSJames Smart 		list_remove_head(&phba->sli4_hba.lpfc_nvmet_io_wait_list,
427a8cf5dfeSJames Smart 				 nvmebuf, struct rqb_dmabuf,
428a8cf5dfeSJames Smart 				 hbuf.list);
429a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt--;
430a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
431a8cf5dfeSJames Smart 				       iflag);
432a8cf5dfeSJames Smart 
433a8cf5dfeSJames Smart 		fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
434a8cf5dfeSJames Smart 		oxid = be16_to_cpu(fc_hdr->fh_ox_id);
435a8cf5dfeSJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
436a8cf5dfeSJames Smart 		size = nvmebuf->bytes_recv;
437a8cf5dfeSJames Smart 		sid = sli4_sid_from_fc_hdr(fc_hdr);
438a8cf5dfeSJames Smart 
4397cacae2aSJames Smart 		ctxp = (struct lpfc_async_xchg_ctx *)ctx_buf->context;
440a8cf5dfeSJames Smart 		ctxp->wqeq = NULL;
441a8cf5dfeSJames Smart 		ctxp->offset = 0;
442a8cf5dfeSJames Smart 		ctxp->phba = phba;
443a8cf5dfeSJames Smart 		ctxp->size = size;
444a8cf5dfeSJames Smart 		ctxp->oxid = oxid;
445a8cf5dfeSJames Smart 		ctxp->sid = sid;
4467b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_RCV;
447a8cf5dfeSJames Smart 		ctxp->entry_cnt = 1;
448a8cf5dfeSJames Smart 		ctxp->flag = 0;
449a8cf5dfeSJames Smart 		ctxp->ctxbuf = ctx_buf;
450cbc5de1bSJames Smart 		ctxp->rqb_buffer = (void *)nvmebuf;
451a8cf5dfeSJames Smart 		spin_lock_init(&ctxp->ctxlock);
452a8cf5dfeSJames Smart 
453a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
454d74a89aaSJames Smart 		/* NOTE: isr time stamp is stale when context is re-assigned*/
455d74a89aaSJames Smart 		if (ctxp->ts_isr_cmd) {
456d74a89aaSJames Smart 			ctxp->ts_cmd_nvme = 0;
457a8cf5dfeSJames Smart 			ctxp->ts_nvme_data = 0;
458a8cf5dfeSJames Smart 			ctxp->ts_data_wqput = 0;
459a8cf5dfeSJames Smart 			ctxp->ts_isr_data = 0;
460a8cf5dfeSJames Smart 			ctxp->ts_data_nvme = 0;
461a8cf5dfeSJames Smart 			ctxp->ts_nvme_status = 0;
462a8cf5dfeSJames Smart 			ctxp->ts_status_wqput = 0;
463a8cf5dfeSJames Smart 			ctxp->ts_isr_status = 0;
464a8cf5dfeSJames Smart 			ctxp->ts_status_nvme = 0;
465d613b6a7SJames Smart 		}
466a8cf5dfeSJames Smart #endif
467a8cf5dfeSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_in);
468472e146dSJames Smart 
46979d8c4ceSJames Smart 		/* Indicate that a replacement buffer has been posted */
470472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
4717b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_CTX_REUSE_WQ;
472472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
473a8cf5dfeSJames Smart 
474472e146dSJames Smart 		if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
475a8cf5dfeSJames Smart 			atomic_inc(&tgtp->rcv_fcp_cmd_drop);
476372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
477472e146dSJames Smart 					"6181 Unable to queue deferred work "
478472e146dSJames Smart 					"for oxid x%x. "
479472e146dSJames Smart 					"FCP Drop IO [x%x x%x x%x]\n",
480472e146dSJames Smart 					ctxp->oxid,
481a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_in),
482a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_out),
483a8cf5dfeSJames Smart 					atomic_read(&tgtp->xmt_fcp_release));
484a8cf5dfeSJames Smart 
485472e146dSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
486a8cf5dfeSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
487472e146dSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
488a8cf5dfeSJames Smart 			lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
489472e146dSJames Smart 		}
490a8cf5dfeSJames Smart 		return;
491a8cf5dfeSJames Smart 	}
492a8cf5dfeSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
493a8cf5dfeSJames Smart 
49466d7ce93SDick Kennedy 	/*
49566d7ce93SDick Kennedy 	 * Use the CPU context list, from the MRQ the IO was received on
49666d7ce93SDick Kennedy 	 * (ctxp->idx), to save context structure.
49766d7ce93SDick Kennedy 	 */
49879d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
49979d8c4ceSJames Smart 	list_del_init(&ctxp->list);
50079d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
501d6d189ceSBart Van Assche 	cpu = raw_smp_processor_id();
50266d7ce93SDick Kennedy 	infop = lpfc_get_ctx_list(phba, cpu, ctxp->idx);
50366d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, iflag);
50466d7ce93SDick Kennedy 	list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
50566d7ce93SDick Kennedy 	infop->nvmet_ctx_list_cnt++;
50666d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, iflag);
507eeeb51d8SJames Smart #endif
508d613b6a7SJames Smart }
509d613b6a7SJames Smart 
5102b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5112b65e182SJames Smart static void
5122b65e182SJames Smart lpfc_nvmet_ktime(struct lpfc_hba *phba,
5137cacae2aSJames Smart 		 struct lpfc_async_xchg_ctx *ctxp)
5142b65e182SJames Smart {
5152b65e182SJames Smart 	uint64_t seg1, seg2, seg3, seg4, seg5;
5162b65e182SJames Smart 	uint64_t seg6, seg7, seg8, seg9, seg10;
517c8a4ce0bSDick Kennedy 	uint64_t segsum;
5182b65e182SJames Smart 
5192b65e182SJames Smart 	if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
5202b65e182SJames Smart 	    !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
5212b65e182SJames Smart 	    !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
5222b65e182SJames Smart 	    !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
5232b65e182SJames Smart 	    !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
5242b65e182SJames Smart 		return;
5252b65e182SJames Smart 
526c8a4ce0bSDick Kennedy 	if (ctxp->ts_status_nvme < ctxp->ts_isr_cmd)
527c8a4ce0bSDick Kennedy 		return;
5282b65e182SJames Smart 	if (ctxp->ts_isr_cmd  > ctxp->ts_cmd_nvme)
5292b65e182SJames Smart 		return;
5302b65e182SJames Smart 	if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
5312b65e182SJames Smart 		return;
5322b65e182SJames Smart 	if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
5332b65e182SJames Smart 		return;
5342b65e182SJames Smart 	if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
5352b65e182SJames Smart 		return;
5362b65e182SJames Smart 	if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
5372b65e182SJames Smart 		return;
5382b65e182SJames Smart 	if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
5392b65e182SJames Smart 		return;
5402b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
5412b65e182SJames Smart 		return;
5422b65e182SJames Smart 	if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
5432b65e182SJames Smart 		return;
5442b65e182SJames Smart 	if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
5452b65e182SJames Smart 		return;
5462b65e182SJames Smart 	/*
5472b65e182SJames Smart 	 * Segment 1 - Time from FCP command received by MSI-X ISR
5482b65e182SJames Smart 	 * to FCP command is passed to NVME Layer.
5492b65e182SJames Smart 	 * Segment 2 - Time from FCP command payload handed
5502b65e182SJames Smart 	 * off to NVME Layer to Driver receives a Command op
5512b65e182SJames Smart 	 * from NVME Layer.
5522b65e182SJames Smart 	 * Segment 3 - Time from Driver receives a Command op
5532b65e182SJames Smart 	 * from NVME Layer to Command is put on WQ.
5542b65e182SJames Smart 	 * Segment 4 - Time from Driver WQ put is done
5552b65e182SJames Smart 	 * to MSI-X ISR for Command cmpl.
5562b65e182SJames Smart 	 * Segment 5 - Time from MSI-X ISR for Command cmpl to
5572b65e182SJames Smart 	 * Command cmpl is passed to NVME Layer.
5582b65e182SJames Smart 	 * Segment 6 - Time from Command cmpl is passed to NVME
5592b65e182SJames Smart 	 * Layer to Driver receives a RSP op from NVME Layer.
5602b65e182SJames Smart 	 * Segment 7 - Time from Driver receives a RSP op from
5612b65e182SJames Smart 	 * NVME Layer to WQ put is done on TRSP FCP Status.
5622b65e182SJames Smart 	 * Segment 8 - Time from Driver WQ put is done on TRSP
5632b65e182SJames Smart 	 * FCP Status to MSI-X ISR for TRSP cmpl.
5642b65e182SJames Smart 	 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
5652b65e182SJames Smart 	 * TRSP cmpl is passed to NVME Layer.
5662b65e182SJames Smart 	 * Segment 10 - Time from FCP command received by
5672b65e182SJames Smart 	 * MSI-X ISR to command is completed on wire.
5682b65e182SJames Smart 	 * (Segments 1 thru 8) for READDATA / WRITEDATA
5692b65e182SJames Smart 	 * (Segments 1 thru 4) for READDATA_RSP
5702b65e182SJames Smart 	 */
5712b65e182SJames Smart 	seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
572c8a4ce0bSDick Kennedy 	segsum = seg1;
573c8a4ce0bSDick Kennedy 
574c8a4ce0bSDick Kennedy 	seg2 = ctxp->ts_nvme_data - ctxp->ts_isr_cmd;
575c8a4ce0bSDick Kennedy 	if (segsum > seg2)
576c8a4ce0bSDick Kennedy 		return;
577c8a4ce0bSDick Kennedy 	seg2 -= segsum;
578c8a4ce0bSDick Kennedy 	segsum += seg2;
579c8a4ce0bSDick Kennedy 
580c8a4ce0bSDick Kennedy 	seg3 = ctxp->ts_data_wqput - ctxp->ts_isr_cmd;
581c8a4ce0bSDick Kennedy 	if (segsum > seg3)
582c8a4ce0bSDick Kennedy 		return;
583c8a4ce0bSDick Kennedy 	seg3 -= segsum;
584c8a4ce0bSDick Kennedy 	segsum += seg3;
585c8a4ce0bSDick Kennedy 
586c8a4ce0bSDick Kennedy 	seg4 = ctxp->ts_isr_data - ctxp->ts_isr_cmd;
587c8a4ce0bSDick Kennedy 	if (segsum > seg4)
588c8a4ce0bSDick Kennedy 		return;
589c8a4ce0bSDick Kennedy 	seg4 -= segsum;
590c8a4ce0bSDick Kennedy 	segsum += seg4;
591c8a4ce0bSDick Kennedy 
592c8a4ce0bSDick Kennedy 	seg5 = ctxp->ts_data_nvme - ctxp->ts_isr_cmd;
593c8a4ce0bSDick Kennedy 	if (segsum > seg5)
594c8a4ce0bSDick Kennedy 		return;
595c8a4ce0bSDick Kennedy 	seg5 -= segsum;
596c8a4ce0bSDick Kennedy 	segsum += seg5;
597c8a4ce0bSDick Kennedy 
5982b65e182SJames Smart 
5992b65e182SJames Smart 	/* For auto rsp commands seg6 thru seg10 will be 0 */
6002b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
601c8a4ce0bSDick Kennedy 		seg6 = ctxp->ts_nvme_status - ctxp->ts_isr_cmd;
602c8a4ce0bSDick Kennedy 		if (segsum > seg6)
603c8a4ce0bSDick Kennedy 			return;
604c8a4ce0bSDick Kennedy 		seg6 -= segsum;
605c8a4ce0bSDick Kennedy 		segsum += seg6;
606c8a4ce0bSDick Kennedy 
607c8a4ce0bSDick Kennedy 		seg7 = ctxp->ts_status_wqput - ctxp->ts_isr_cmd;
608c8a4ce0bSDick Kennedy 		if (segsum > seg7)
609c8a4ce0bSDick Kennedy 			return;
610c8a4ce0bSDick Kennedy 		seg7 -= segsum;
611c8a4ce0bSDick Kennedy 		segsum += seg7;
612c8a4ce0bSDick Kennedy 
613c8a4ce0bSDick Kennedy 		seg8 = ctxp->ts_isr_status - ctxp->ts_isr_cmd;
614c8a4ce0bSDick Kennedy 		if (segsum > seg8)
615c8a4ce0bSDick Kennedy 			return;
616c8a4ce0bSDick Kennedy 		seg8 -= segsum;
617c8a4ce0bSDick Kennedy 		segsum += seg8;
618c8a4ce0bSDick Kennedy 
619c8a4ce0bSDick Kennedy 		seg9 = ctxp->ts_status_nvme - ctxp->ts_isr_cmd;
620c8a4ce0bSDick Kennedy 		if (segsum > seg9)
621c8a4ce0bSDick Kennedy 			return;
622c8a4ce0bSDick Kennedy 		seg9 -= segsum;
623c8a4ce0bSDick Kennedy 		segsum += seg9;
624c8a4ce0bSDick Kennedy 
625c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_status < ctxp->ts_isr_cmd)
626c8a4ce0bSDick Kennedy 			return;
6272b65e182SJames Smart 		seg10 = (ctxp->ts_isr_status -
6282b65e182SJames Smart 			ctxp->ts_isr_cmd);
6292b65e182SJames Smart 	} else {
630c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_data < ctxp->ts_isr_cmd)
631c8a4ce0bSDick Kennedy 			return;
6322b65e182SJames Smart 		seg6 =  0;
6332b65e182SJames Smart 		seg7 =  0;
6342b65e182SJames Smart 		seg8 =  0;
6352b65e182SJames Smart 		seg9 =  0;
6362b65e182SJames Smart 		seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
6372b65e182SJames Smart 	}
6382b65e182SJames Smart 
6392b65e182SJames Smart 	phba->ktime_seg1_total += seg1;
6402b65e182SJames Smart 	if (seg1 < phba->ktime_seg1_min)
6412b65e182SJames Smart 		phba->ktime_seg1_min = seg1;
6422b65e182SJames Smart 	else if (seg1 > phba->ktime_seg1_max)
6432b65e182SJames Smart 		phba->ktime_seg1_max = seg1;
6442b65e182SJames Smart 
6452b65e182SJames Smart 	phba->ktime_seg2_total += seg2;
6462b65e182SJames Smart 	if (seg2 < phba->ktime_seg2_min)
6472b65e182SJames Smart 		phba->ktime_seg2_min = seg2;
6482b65e182SJames Smart 	else if (seg2 > phba->ktime_seg2_max)
6492b65e182SJames Smart 		phba->ktime_seg2_max = seg2;
6502b65e182SJames Smart 
6512b65e182SJames Smart 	phba->ktime_seg3_total += seg3;
6522b65e182SJames Smart 	if (seg3 < phba->ktime_seg3_min)
6532b65e182SJames Smart 		phba->ktime_seg3_min = seg3;
6542b65e182SJames Smart 	else if (seg3 > phba->ktime_seg3_max)
6552b65e182SJames Smart 		phba->ktime_seg3_max = seg3;
6562b65e182SJames Smart 
6572b65e182SJames Smart 	phba->ktime_seg4_total += seg4;
6582b65e182SJames Smart 	if (seg4 < phba->ktime_seg4_min)
6592b65e182SJames Smart 		phba->ktime_seg4_min = seg4;
6602b65e182SJames Smart 	else if (seg4 > phba->ktime_seg4_max)
6612b65e182SJames Smart 		phba->ktime_seg4_max = seg4;
6622b65e182SJames Smart 
6632b65e182SJames Smart 	phba->ktime_seg5_total += seg5;
6642b65e182SJames Smart 	if (seg5 < phba->ktime_seg5_min)
6652b65e182SJames Smart 		phba->ktime_seg5_min = seg5;
6662b65e182SJames Smart 	else if (seg5 > phba->ktime_seg5_max)
6672b65e182SJames Smart 		phba->ktime_seg5_max = seg5;
6682b65e182SJames Smart 
6692b65e182SJames Smart 	phba->ktime_data_samples++;
6702b65e182SJames Smart 	if (!seg6)
6712b65e182SJames Smart 		goto out;
6722b65e182SJames Smart 
6732b65e182SJames Smart 	phba->ktime_seg6_total += seg6;
6742b65e182SJames Smart 	if (seg6 < phba->ktime_seg6_min)
6752b65e182SJames Smart 		phba->ktime_seg6_min = seg6;
6762b65e182SJames Smart 	else if (seg6 > phba->ktime_seg6_max)
6772b65e182SJames Smart 		phba->ktime_seg6_max = seg6;
6782b65e182SJames Smart 
6792b65e182SJames Smart 	phba->ktime_seg7_total += seg7;
6802b65e182SJames Smart 	if (seg7 < phba->ktime_seg7_min)
6812b65e182SJames Smart 		phba->ktime_seg7_min = seg7;
6822b65e182SJames Smart 	else if (seg7 > phba->ktime_seg7_max)
6832b65e182SJames Smart 		phba->ktime_seg7_max = seg7;
6842b65e182SJames Smart 
6852b65e182SJames Smart 	phba->ktime_seg8_total += seg8;
6862b65e182SJames Smart 	if (seg8 < phba->ktime_seg8_min)
6872b65e182SJames Smart 		phba->ktime_seg8_min = seg8;
6882b65e182SJames Smart 	else if (seg8 > phba->ktime_seg8_max)
6892b65e182SJames Smart 		phba->ktime_seg8_max = seg8;
6902b65e182SJames Smart 
6912b65e182SJames Smart 	phba->ktime_seg9_total += seg9;
6922b65e182SJames Smart 	if (seg9 < phba->ktime_seg9_min)
6932b65e182SJames Smart 		phba->ktime_seg9_min = seg9;
6942b65e182SJames Smart 	else if (seg9 > phba->ktime_seg9_max)
6952b65e182SJames Smart 		phba->ktime_seg9_max = seg9;
6962b65e182SJames Smart out:
6972b65e182SJames Smart 	phba->ktime_seg10_total += seg10;
6982b65e182SJames Smart 	if (seg10 < phba->ktime_seg10_min)
6992b65e182SJames Smart 		phba->ktime_seg10_min = seg10;
7002b65e182SJames Smart 	else if (seg10 > phba->ktime_seg10_max)
7012b65e182SJames Smart 		phba->ktime_seg10_max = seg10;
7022b65e182SJames Smart 	phba->ktime_status_samples++;
7032b65e182SJames Smart }
7042b65e182SJames Smart #endif
7052b65e182SJames Smart 
706d613b6a7SJames Smart /**
707d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
708d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
709d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
710d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
711d613b6a7SJames Smart  *
712d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
713d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME FCP commands
714d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
715d613b6a7SJames Smart  **/
716d613b6a7SJames Smart static void
717d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
718d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
719d613b6a7SJames Smart {
720d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
721d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
7227cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
723e3246a12SJames Smart 	uint32_t status, result, op, start_clean, logerr;
7242b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
725840eda96SJames Smart 	int id;
7262b65e182SJames Smart #endif
727d613b6a7SJames Smart 
728d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
7297b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_IO_INP;
73086c67379SJames Smart 
7317cacae2aSJames Smart 	rsp = &ctxp->hdlrctx.fcp_req;
732d613b6a7SJames Smart 	op = rsp->op;
733d613b6a7SJames Smart 
734d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
735d613b6a7SJames Smart 	result = wcqe->parameter;
736d613b6a7SJames Smart 
73786c67379SJames Smart 	if (phba->targetport)
73886c67379SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
73986c67379SJames Smart 	else
74086c67379SJames Smart 		tgtp = NULL;
741d613b6a7SJames Smart 
7422b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
7432b65e182SJames Smart 			 ctxp->oxid, op, status);
7442b65e182SJames Smart 
745d613b6a7SJames Smart 	if (status) {
746d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
747d613b6a7SJames Smart 		rsp->transferred_length = 0;
7484b056682SJames Smart 		if (tgtp) {
749d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_error);
7508ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
7514b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_aborted);
7524b056682SJames Smart 		}
75386c67379SJames Smart 
754e3246a12SJames Smart 		logerr = LOG_NVME_IOERR;
755e3246a12SJames Smart 
75686c67379SJames Smart 		/* pick up SLI4 exhange busy condition */
75786c67379SJames Smart 		if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
7587b7f551bSJames Smart 			ctxp->flag |= LPFC_NVME_XBUSY;
759e3246a12SJames Smart 			logerr |= LOG_NVME_ABTS;
7604b056682SJames Smart 			if (tgtp)
7614b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_xb_set);
76286c67379SJames Smart 
76386c67379SJames Smart 		} else {
7647b7f551bSJames Smart 			ctxp->flag &= ~LPFC_NVME_XBUSY;
76586c67379SJames Smart 		}
76686c67379SJames Smart 
767e3246a12SJames Smart 		lpfc_printf_log(phba, KERN_INFO, logerr,
76879d8c4ceSJames Smart 				"6315 IO Error Cmpl oxid: x%x xri: x%x %x/%x "
76979d8c4ceSJames Smart 				"XBUSY:x%x\n",
77079d8c4ceSJames Smart 				ctxp->oxid, ctxp->ctxbuf->sglq->sli4_xritag,
77179d8c4ceSJames Smart 				status, result, ctxp->flag);
772e3246a12SJames Smart 
773d613b6a7SJames Smart 	} else {
774d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_SUCCESS;
775d613b6a7SJames Smart 		if (op == NVMET_FCOP_RSP)
776d613b6a7SJames Smart 			rsp->transferred_length = rsp->rsplen;
777d613b6a7SJames Smart 		else
778d613b6a7SJames Smart 			rsp->transferred_length = rsp->transfer_length;
77986c67379SJames Smart 		if (tgtp)
780d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
781d613b6a7SJames Smart 	}
782d613b6a7SJames Smart 
783d613b6a7SJames Smart 	if ((op == NVMET_FCOP_READDATA_RSP) ||
784d613b6a7SJames Smart 	    (op == NVMET_FCOP_RSP)) {
785d613b6a7SJames Smart 		/* Sanity check */
7867b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_DONE;
787d613b6a7SJames Smart 		ctxp->entry_cnt++;
78886c67379SJames Smart 
7892b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
790c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
7912b65e182SJames Smart 			if (rsp->op == NVMET_FCOP_READDATA_RSP) {
7922b65e182SJames Smart 				ctxp->ts_isr_data =
7932b65e182SJames Smart 					cmdwqe->isr_timestamp;
7942b65e182SJames Smart 				ctxp->ts_data_nvme =
7952b65e182SJames Smart 					ktime_get_ns();
7962b65e182SJames Smart 				ctxp->ts_nvme_status =
7972b65e182SJames Smart 					ctxp->ts_data_nvme;
7982b65e182SJames Smart 				ctxp->ts_status_wqput =
7992b65e182SJames Smart 					ctxp->ts_data_nvme;
8002b65e182SJames Smart 				ctxp->ts_isr_status =
8012b65e182SJames Smart 					ctxp->ts_data_nvme;
8022b65e182SJames Smart 				ctxp->ts_status_nvme =
8032b65e182SJames Smart 					ctxp->ts_data_nvme;
8042b65e182SJames Smart 			} else {
8052b65e182SJames Smart 				ctxp->ts_isr_status =
8062b65e182SJames Smart 					cmdwqe->isr_timestamp;
8072b65e182SJames Smart 				ctxp->ts_status_nvme =
8082b65e182SJames Smart 					ktime_get_ns();
8092b65e182SJames Smart 			}
8102b65e182SJames Smart 		}
8112b65e182SJames Smart #endif
812d613b6a7SJames Smart 		rsp->done(rsp);
8132b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
814c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme)
8152b65e182SJames Smart 			lpfc_nvmet_ktime(phba, ctxp);
8162b65e182SJames Smart #endif
81719b58d94SJames Smart 		/* lpfc_nvmet_xmt_fcp_release() will recycle the context */
818d613b6a7SJames Smart 	} else {
819d613b6a7SJames Smart 		ctxp->entry_cnt++;
820c6e0c925SDick Kennedy 		start_clean = offsetof(struct lpfc_iocbq, iocb_flag);
821d613b6a7SJames Smart 		memset(((char *)cmdwqe) + start_clean, 0,
822d613b6a7SJames Smart 		       (sizeof(struct lpfc_iocbq) - start_clean));
8232b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
824c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
8252b65e182SJames Smart 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
8262b65e182SJames Smart 			ctxp->ts_data_nvme = ktime_get_ns();
8272b65e182SJames Smart 		}
8282b65e182SJames Smart #endif
829d613b6a7SJames Smart 		rsp->done(rsp);
830d613b6a7SJames Smart 	}
83163df6d63SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
832840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
833d6d189ceSBart Van Assche 		id = raw_smp_processor_id();
834840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
83563df6d63SJames Smart 		if (ctxp->cpu != id)
83663df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
83763df6d63SJames Smart 					"6704 CPU Check cmdcmpl: "
83863df6d63SJames Smart 					"cpu %d expect %d\n",
83963df6d63SJames Smart 					id, ctxp->cpu);
84063df6d63SJames Smart 	}
84163df6d63SJames Smart #endif
842d613b6a7SJames Smart }
843d613b6a7SJames Smart 
844fe1bedecSJames Smart /**
845fe1bedecSJames Smart  * __lpfc_nvme_xmt_ls_rsp - Generic service routine to issue transmit
846fe1bedecSJames Smart  *         an NVME LS rsp for a prior NVME LS request that was received.
847fe1bedecSJames Smart  * @axchg: pointer to exchange context for the NVME LS request the response
848fe1bedecSJames Smart  *         is for.
849fe1bedecSJames Smart  * @ls_rsp: pointer to the transport LS RSP that is to be sent
850fe1bedecSJames Smart  * @xmt_ls_rsp_cmp: completion routine to call upon RSP transmit done
851fe1bedecSJames Smart  *
852fe1bedecSJames Smart  * This routine is used to format and send a WQE to transmit a NVME LS
853fe1bedecSJames Smart  * Response.  The response is for a prior NVME LS request that was
854fe1bedecSJames Smart  * received and posted to the transport.
855fe1bedecSJames Smart  *
856fe1bedecSJames Smart  * Returns:
857fe1bedecSJames Smart  *  0 : if response successfully transmit
858fe1bedecSJames Smart  *  non-zero : if response failed to transmit, of the form -Exxx.
859fe1bedecSJames Smart  **/
860fe1bedecSJames Smart int
861fe1bedecSJames Smart __lpfc_nvme_xmt_ls_rsp(struct lpfc_async_xchg_ctx *axchg,
862fe1bedecSJames Smart 			struct nvmefc_ls_rsp *ls_rsp,
863fe1bedecSJames Smart 			void (*xmt_ls_rsp_cmp)(struct lpfc_hba *phba,
864fe1bedecSJames Smart 				struct lpfc_iocbq *cmdwqe,
865fe1bedecSJames Smart 				struct lpfc_wcqe_complete *wcqe))
866d613b6a7SJames Smart {
867fe1bedecSJames Smart 	struct lpfc_hba *phba = axchg->phba;
868fe1bedecSJames Smart 	struct hbq_dmabuf *nvmebuf = (struct hbq_dmabuf *)axchg->rqb_buffer;
869d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
870d613b6a7SJames Smart 	struct lpfc_dmabuf dmabuf;
871d613b6a7SJames Smart 	struct ulp_bde64 bpl;
872d613b6a7SJames Smart 	int rc;
873d613b6a7SJames Smart 
8741abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
8751abcb371SDick Kennedy 		return -ENODEV;
8761abcb371SDick Kennedy 
877d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
878fe1bedecSJames Smart 			"6023 NVMEx LS rsp oxid x%x\n", axchg->oxid);
879ce1b591cSJames Smart 
880fe1bedecSJames Smart 	if (axchg->state != LPFC_NVME_STE_LS_RCV || axchg->entry_cnt != 1) {
881372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
882fe1bedecSJames Smart 				"6412 NVMEx LS rsp state mismatch "
883ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
884fe1bedecSJames Smart 				axchg->oxid, axchg->state, axchg->entry_cnt);
885fe1bedecSJames Smart 		return -EALREADY;
886ce1b591cSJames Smart 	}
887fe1bedecSJames Smart 	axchg->state = LPFC_NVME_STE_LS_RSP;
888fe1bedecSJames Smart 	axchg->entry_cnt++;
889d613b6a7SJames Smart 
890fe1bedecSJames Smart 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, axchg, ls_rsp->rspdma,
891fe1bedecSJames Smart 					 ls_rsp->rsplen);
892d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
893372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
894fe1bedecSJames Smart 				"6150 NVMEx LS Drop Rsp x%x: Prep\n",
895fe1bedecSJames Smart 				axchg->oxid);
896fe1bedecSJames Smart 		rc = -ENOMEM;
897fe1bedecSJames Smart 		goto out_free_buf;
898d613b6a7SJames Smart 	}
899d613b6a7SJames Smart 
900d613b6a7SJames Smart 	/* Save numBdes for bpl2sgl */
901d613b6a7SJames Smart 	nvmewqeq->rsvd2 = 1;
902d613b6a7SJames Smart 	nvmewqeq->hba_wqidx = 0;
903d613b6a7SJames Smart 	nvmewqeq->context3 = &dmabuf;
904d613b6a7SJames Smart 	dmabuf.virt = &bpl;
905d613b6a7SJames Smart 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
906d613b6a7SJames Smart 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
907fe1bedecSJames Smart 	bpl.tus.f.bdeSize = ls_rsp->rsplen;
908d613b6a7SJames Smart 	bpl.tus.f.bdeFlags = 0;
909d613b6a7SJames Smart 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
910fe1bedecSJames Smart 	/*
911fe1bedecSJames Smart 	 * Note: although we're using stack space for the dmabuf, the
912fe1bedecSJames Smart 	 * call to lpfc_sli4_issue_wqe is synchronous, so it will not
913fe1bedecSJames Smart 	 * be referenced after it returns back to this routine.
914fe1bedecSJames Smart 	 */
915d613b6a7SJames Smart 
916fe1bedecSJames Smart 	nvmewqeq->wqe_cmpl = xmt_ls_rsp_cmp;
917d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
918fe1bedecSJames Smart 	nvmewqeq->context2 = axchg;
919d613b6a7SJames Smart 
920fe1bedecSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS RSP: xri x%x wqidx x%x len x%x\n",
921fe1bedecSJames Smart 			 axchg->oxid, nvmewqeq->hba_wqidx, ls_rsp->rsplen);
9222b65e182SJames Smart 
923fe1bedecSJames Smart 	rc = lpfc_sli4_issue_wqe(phba, axchg->hdwq, nvmewqeq);
924fe1bedecSJames Smart 
925fe1bedecSJames Smart 	/* clear to be sure there's no reference */
926fe1bedecSJames Smart 	nvmewqeq->context3 = NULL;
927fe1bedecSJames Smart 
928d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
929d613b6a7SJames Smart 		/*
930d613b6a7SJames Smart 		 * Okay to repost buffer here, but wait till cmpl
931d613b6a7SJames Smart 		 * before freeing ctxp and iocbq.
932d613b6a7SJames Smart 		 */
933d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
934d613b6a7SJames Smart 		return 0;
935d613b6a7SJames Smart 	}
936fe1bedecSJames Smart 
937372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
938fe1bedecSJames Smart 			"6151 NVMEx LS RSP x%x: failed to transmit %d\n",
939fe1bedecSJames Smart 			axchg->oxid, rc);
940fe1bedecSJames Smart 
941fe1bedecSJames Smart 	rc = -ENXIO;
942d613b6a7SJames Smart 
943d613b6a7SJames Smart 	lpfc_nlp_put(nvmewqeq->context1);
944d613b6a7SJames Smart 
945fe1bedecSJames Smart out_free_buf:
946fe1bedecSJames Smart 	/* Give back resources */
947d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
948fe1bedecSJames Smart 
949fe1bedecSJames Smart 	/*
950fe1bedecSJames Smart 	 * As transport doesn't track completions of responses, if the rsp
951fe1bedecSJames Smart 	 * fails to send, the transport will effectively ignore the rsp
952fe1bedecSJames Smart 	 * and consider the LS done. However, the driver has an active
953fe1bedecSJames Smart 	 * exchange open for the LS - so be sure to abort the exchange
954fe1bedecSJames Smart 	 * if the response isn't sent.
955fe1bedecSJames Smart 	 */
956fe1bedecSJames Smart 	lpfc_nvme_unsol_ls_issue_abort(phba, axchg, axchg->sid, axchg->oxid);
957fe1bedecSJames Smart 	return rc;
958fe1bedecSJames Smart }
959fe1bedecSJames Smart 
960fe1bedecSJames Smart /**
961fe1bedecSJames Smart  * lpfc_nvmet_xmt_ls_rsp - Transmit NVME LS response
962fe1bedecSJames Smart  * @tgtport: pointer to target port that NVME LS is to be transmit from.
963fe1bedecSJames Smart  * @ls_rsp: pointer to the transport LS RSP that is to be sent
964fe1bedecSJames Smart  *
965fe1bedecSJames Smart  * Driver registers this routine to transmit responses for received NVME
966fe1bedecSJames Smart  * LS requests.
967fe1bedecSJames Smart  *
968fe1bedecSJames Smart  * This routine is used to format and send a WQE to transmit a NVME LS
969fe1bedecSJames Smart  * Response. The ls_rsp is used to reverse-map the LS to the original
970fe1bedecSJames Smart  * NVME LS request sequence, which provides addressing information for
971fe1bedecSJames Smart  * the remote port the LS to be sent to, as well as the exchange id
972fe1bedecSJames Smart  * that is the LS is bound to.
973fe1bedecSJames Smart  *
974fe1bedecSJames Smart  * Returns:
975fe1bedecSJames Smart  *  0 : if response successfully transmit
976fe1bedecSJames Smart  *  non-zero : if response failed to transmit, of the form -Exxx.
977fe1bedecSJames Smart  **/
978fe1bedecSJames Smart static int
979fe1bedecSJames Smart lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
980fe1bedecSJames Smart 		      struct nvmefc_ls_rsp *ls_rsp)
981fe1bedecSJames Smart {
982fe1bedecSJames Smart 	struct lpfc_async_xchg_ctx *axchg =
983fe1bedecSJames Smart 		container_of(ls_rsp, struct lpfc_async_xchg_ctx, ls_rsp);
984fe1bedecSJames Smart 	struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
985fe1bedecSJames Smart 	int rc;
986fe1bedecSJames Smart 
987fe1bedecSJames Smart 	if (axchg->phba->pport->load_flag & FC_UNLOADING)
988fe1bedecSJames Smart 		return -ENODEV;
989fe1bedecSJames Smart 
990fe1bedecSJames Smart 	rc = __lpfc_nvme_xmt_ls_rsp(axchg, ls_rsp, lpfc_nvmet_xmt_ls_rsp_cmp);
991fe1bedecSJames Smart 
992fe1bedecSJames Smart 	if (rc) {
993fe1bedecSJames Smart 		atomic_inc(&nvmep->xmt_ls_drop);
994fe1bedecSJames Smart 		/*
995fe1bedecSJames Smart 		 * unless the failure is due to having already sent
996fe1bedecSJames Smart 		 * the response, an abort will be generated for the
997fe1bedecSJames Smart 		 * exchange if the rsp can't be sent.
998fe1bedecSJames Smart 		 */
999fe1bedecSJames Smart 		if (rc != -EALREADY)
1000547077a4SJames Smart 			atomic_inc(&nvmep->xmt_ls_abort);
1001fe1bedecSJames Smart 		return rc;
1002fe1bedecSJames Smart 	}
1003fe1bedecSJames Smart 
1004fe1bedecSJames Smart 	atomic_inc(&nvmep->xmt_ls_rsp);
1005fe1bedecSJames Smart 	return 0;
1006d613b6a7SJames Smart }
1007d613b6a7SJames Smart 
1008d613b6a7SJames Smart static int
1009d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
1010d613b6a7SJames Smart 		      struct nvmefc_tgt_fcp_req *rsp)
1011d613b6a7SJames Smart {
1012d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
10137cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
10147cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
1015d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
10166e8e1c14SJames Smart 	struct lpfc_queue *wq;
1017d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
10186e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
10196e8e1c14SJames Smart 	unsigned long iflags;
1020223b78eaSArnd Bergmann 	int rc;
1021840eda96SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1022840eda96SJames Smart 	int id;
1023840eda96SJames Smart #endif
10242b65e182SJames Smart 
10251abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING) {
10261abcb371SDick Kennedy 		rc = -ENODEV;
10271abcb371SDick Kennedy 		goto aerr;
10281abcb371SDick Kennedy 	}
10291abcb371SDick Kennedy 
10302b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1031c8a4ce0bSDick Kennedy 	if (ctxp->ts_cmd_nvme) {
10322b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
10332b65e182SJames Smart 			ctxp->ts_nvme_status = ktime_get_ns();
10342b65e182SJames Smart 		else
10352b65e182SJames Smart 			ctxp->ts_nvme_data = ktime_get_ns();
10362b65e182SJames Smart 	}
10371fbf9742SJames Smart 
10381fbf9742SJames Smart 	/* Setup the hdw queue if not already set */
10391fbf9742SJames Smart 	if (!ctxp->hdwq)
10401fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[rsp->hwqid];
10411fbf9742SJames Smart 
1042840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
1043840eda96SJames Smart 		id = raw_smp_processor_id();
1044840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
104563df6d63SJames Smart 		if (rsp->hwqid != id)
104663df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
10472b65e182SJames Smart 					"6705 CPU Check OP: "
10482b65e182SJames Smart 					"cpu %d expect %d\n",
10492b65e182SJames Smart 					id, rsp->hwqid);
105063df6d63SJames Smart 		ctxp->cpu = id; /* Setup cpu for cmpl check */
10512b65e182SJames Smart 	}
10522b65e182SJames Smart #endif
1053d613b6a7SJames Smart 
1054d613b6a7SJames Smart 	/* Sanity check */
10557b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_ABTS_RCV) ||
10567b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_ABORT)) {
1057d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1058372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
105979d8c4ceSJames Smart 				"6102 IO oxid x%x aborted\n",
1060d613b6a7SJames Smart 				ctxp->oxid);
1061a5068b46SJames Smart 		rc = -ENXIO;
1062d613b6a7SJames Smart 		goto aerr;
1063d613b6a7SJames Smart 	}
1064d613b6a7SJames Smart 
1065d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
1066d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
1067d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1068372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1069d613b6a7SJames Smart 				"6152 FCP Drop IO x%x: Prep\n",
1070d613b6a7SJames Smart 				ctxp->oxid);
1071a5068b46SJames Smart 		rc = -ENXIO;
1072d613b6a7SJames Smart 		goto aerr;
1073d613b6a7SJames Smart 	}
1074d613b6a7SJames Smart 
1075d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
1076d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
1077d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
1078d613b6a7SJames Smart 	nvmewqeq->iocb_flag |=  LPFC_IO_NVMET;
1079d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
1080d613b6a7SJames Smart 
10812b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
10822b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
10832b65e182SJames Smart 
10847b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_IO_INP;
10851fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
1086d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
10872b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1088c8a4ce0bSDick Kennedy 		if (!ctxp->ts_cmd_nvme)
10892b65e182SJames Smart 			return 0;
10902b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
10912b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
10922b65e182SJames Smart 		else
10932b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
10942b65e182SJames Smart #endif
1095d613b6a7SJames Smart 		return 0;
1096d613b6a7SJames Smart 	}
1097d613b6a7SJames Smart 
10986e8e1c14SJames Smart 	if (rc == -EBUSY) {
10996e8e1c14SJames Smart 		/*
11006e8e1c14SJames Smart 		 * WQ was full, so queue nvmewqeq to be sent after
11016e8e1c14SJames Smart 		 * WQE release CQE
11026e8e1c14SJames Smart 		 */
11037b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_DEFER_WQFULL;
1104c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
11056e8e1c14SJames Smart 		pring = wq->pring;
11066e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
11076e8e1c14SJames Smart 		list_add_tail(&nvmewqeq->list, &wq->wqfull_list);
11086e8e1c14SJames Smart 		wq->q_flag |= HBA_NVMET_WQFULL;
11096e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
1110411de511SJames Smart 		atomic_inc(&lpfc_nvmep->defer_wqfull);
11116e8e1c14SJames Smart 		return 0;
11126e8e1c14SJames Smart 	}
11136e8e1c14SJames Smart 
1114d613b6a7SJames Smart 	/* Give back resources */
1115d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1116372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1117d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
1118d613b6a7SJames Smart 			ctxp->oxid, rc);
1119d613b6a7SJames Smart 
1120d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
1121d613b6a7SJames Smart 	nvmewqeq->context2 = NULL;
1122d613b6a7SJames Smart 	nvmewqeq->context3 = NULL;
1123a5068b46SJames Smart 	rc = -EBUSY;
1124d613b6a7SJames Smart aerr:
1125a5068b46SJames Smart 	return rc;
1126d613b6a7SJames Smart }
1127d613b6a7SJames Smart 
1128d613b6a7SJames Smart static void
1129d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
1130d613b6a7SJames Smart {
1131d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
1132d613b6a7SJames Smart 
1133d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
1134c41f5988SEwan D. Milne 	if (tport->phba->targetport)
1135c41f5988SEwan D. Milne 		complete(tport->tport_unreg_cmp);
1136d613b6a7SJames Smart }
1137d613b6a7SJames Smart 
113819b58d94SJames Smart static void
1139a97ec51bSJames Smart lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
1140a97ec51bSJames Smart 			 struct nvmefc_tgt_fcp_req *req)
1141a97ec51bSJames Smart {
1142a97ec51bSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
11437cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
11447cacae2aSJames Smart 		container_of(req, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
1145a97ec51bSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
11466e8e1c14SJames Smart 	struct lpfc_queue *wq;
114786c67379SJames Smart 	unsigned long flags;
1148a97ec51bSJames Smart 
11491abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
11501abcb371SDick Kennedy 		return;
11511abcb371SDick Kennedy 
11521fbf9742SJames Smart 	if (!ctxp->hdwq)
11531fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[0];
11541fbf9742SJames Smart 
1155a97ec51bSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
115679d8c4ceSJames Smart 			"6103 NVMET Abort op: oxid x%x flg x%x ste %d\n",
1157ce1b591cSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
1158a97ec51bSJames Smart 
1159ce1b591cSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x flg x%x ste x%x\n",
1160ce1b591cSJames Smart 			 ctxp->oxid, ctxp->flag, ctxp->state);
1161a97ec51bSJames Smart 
1162a97ec51bSJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
1163ce1b591cSJames Smart 
116486c67379SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
116586c67379SJames Smart 
116686c67379SJames Smart 	/* Since iaab/iaar are NOT set, we need to check
116786c67379SJames Smart 	 * if the firmware is in process of aborting IO
116886c67379SJames Smart 	 */
11697b7f551bSJames Smart 	if (ctxp->flag & (LPFC_NVME_XBUSY | LPFC_NVME_ABORT_OP)) {
117086c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
117186c67379SJames Smart 		return;
117286c67379SJames Smart 	}
11737b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_ABORT_OP;
1174ce1b591cSJames Smart 
11757b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_DEFER_WQFULL) {
1176c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11776e8e1c14SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
11786e8e1c14SJames Smart 						 ctxp->oxid);
1179c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
11806e8e1c14SJames Smart 		lpfc_nvmet_wqfull_flush(phba, wq, ctxp);
11816e8e1c14SJames Smart 		return;
11826e8e1c14SJames Smart 	}
1183c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11846e8e1c14SJames Smart 
11857b7f551bSJames Smart 	/* A state of LPFC_NVME_STE_RCV means we have just received
1186ce1b591cSJames Smart 	 * the NVME command and have not started processing it.
1187ce1b591cSJames Smart 	 * (by issuing any IO WQEs on this exchange yet)
1188ce1b591cSJames Smart 	 */
11897b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_RCV)
1190ce1b591cSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1191a97ec51bSJames Smart 						 ctxp->oxid);
1192a97ec51bSJames Smart 	else
1193ce1b591cSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1194a97ec51bSJames Smart 					       ctxp->oxid);
1195a97ec51bSJames Smart }
1196a97ec51bSJames Smart 
1197a97ec51bSJames Smart static void
119819b58d94SJames Smart lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
119919b58d94SJames Smart 			   struct nvmefc_tgt_fcp_req *rsp)
120019b58d94SJames Smart {
1201547077a4SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
12027cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
12037cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
120419b58d94SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
120519b58d94SJames Smart 	unsigned long flags;
120619b58d94SJames Smart 	bool aborting = false;
120719b58d94SJames Smart 
1208c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
12097b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_XBUSY)
1210c160c0f8SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1211c160c0f8SJames Smart 				"6027 NVMET release with XBUSY flag x%x"
1212c160c0f8SJames Smart 				" oxid x%x\n",
1213c160c0f8SJames Smart 				ctxp->flag, ctxp->oxid);
12147b7f551bSJames Smart 	else if (ctxp->state != LPFC_NVME_STE_DONE &&
12157b7f551bSJames Smart 		 ctxp->state != LPFC_NVME_STE_ABORT)
1216372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1217ce1b591cSJames Smart 				"6413 NVMET release bad state %d %d oxid x%x\n",
1218ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
1219ce1b591cSJames Smart 
12207b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_ABORT_OP) ||
12217b7f551bSJames Smart 	    (ctxp->flag & LPFC_NVME_XBUSY)) {
122219b58d94SJames Smart 		aborting = true;
122386c67379SJames Smart 		/* let the abort path do the real release */
122486c67379SJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
122519b58d94SJames Smart 	}
122619b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
122719b58d94SJames Smart 
12289094367aSArnd Bergmann 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d abt %d\n", ctxp->oxid,
12299094367aSArnd Bergmann 			 ctxp->state, aborting);
123019b58d94SJames Smart 
1231547077a4SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_release);
12327b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_TNOTIFY;
1233547077a4SJames Smart 
123486c67379SJames Smart 	if (aborting)
123586c67379SJames Smart 		return;
123686c67379SJames Smart 
12376c621a22SJames Smart 	lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
123819b58d94SJames Smart }
123919b58d94SJames Smart 
124050738420SJames Smart static void
124150738420SJames Smart lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port *tgtport,
124250738420SJames Smart 		     struct nvmefc_tgt_fcp_req *rsp)
124350738420SJames Smart {
124450738420SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
12457cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
12467cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
124750738420SJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
124850738420SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
1249472e146dSJames Smart 	unsigned long iflag;
1250472e146dSJames Smart 
125150738420SJames Smart 
125250738420SJames Smart 	lpfc_nvmeio_data(phba, "NVMET DEFERRCV: xri x%x sz %d CPU %02x\n",
1253d6d189ceSBart Van Assche 			 ctxp->oxid, ctxp->size, raw_smp_processor_id());
125450738420SJames Smart 
12556871e814SJames Smart 	if (!nvmebuf) {
12566871e814SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
125779d8c4ceSJames Smart 				"6425 Defer rcv: no buffer oxid x%x: "
12586871e814SJames Smart 				"flg %x ste %x\n",
12596871e814SJames Smart 				ctxp->oxid, ctxp->flag, ctxp->state);
12606871e814SJames Smart 		return;
12616871e814SJames Smart 	}
12626871e814SJames Smart 
126350738420SJames Smart 	tgtp = phba->targetport->private;
12646871e814SJames Smart 	if (tgtp)
126550738420SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_defer);
1266411de511SJames Smart 
1267411de511SJames Smart 	/* Free the nvmebuf since a new buffer already replaced it */
1268cbc5de1bSJames Smart 	nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
1269472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflag);
1270472e146dSJames Smart 	ctxp->rqb_buffer = NULL;
1271472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
127250738420SJames Smart }
127350738420SJames Smart 
127454840bedSJames Smart /**
127554840bedSJames Smart  * lpfc_nvmet_ls_req_cmp - completion handler for a nvme ls request
127654840bedSJames Smart  * @phba: Pointer to HBA context object
127754840bedSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
127854840bedSJames Smart  * @wcqe: Pointer to driver response CQE object.
127954840bedSJames Smart  *
128054840bedSJames Smart  * This function is the completion handler for NVME LS requests.
128154840bedSJames Smart  * The function updates any states and statistics, then calls the
128254840bedSJames Smart  * generic completion handler to finish completion of the request.
128354840bedSJames Smart  **/
128454840bedSJames Smart static void
128554840bedSJames Smart lpfc_nvmet_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
128654840bedSJames Smart 		       struct lpfc_wcqe_complete *wcqe)
128754840bedSJames Smart {
128854840bedSJames Smart 	__lpfc_nvme_ls_req_cmp(phba, cmdwqe->vport, cmdwqe, wcqe);
128954840bedSJames Smart }
129054840bedSJames Smart 
129154840bedSJames Smart /**
129254840bedSJames Smart  * lpfc_nvmet_ls_req - Issue an Link Service request
1293d3cb79fcSLee Jones  * @targetport: pointer to target instance registered with nvmet transport.
1294d3cb79fcSLee Jones  * @hosthandle: hosthandle set by the driver in a prior ls_rqst_rcv.
129554840bedSJames Smart  *               Driver sets this value to the ndlp pointer.
1296d3cb79fcSLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
129754840bedSJames Smart  *
129854840bedSJames Smart  * Driver registers this routine to handle any link service request
129954840bedSJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
130054840bedSJames Smart  *
130154840bedSJames Smart  * Return value :
130254840bedSJames Smart  *   0 - Success
130354840bedSJames Smart  *   non-zero: various error codes, in form of -Exxx
130454840bedSJames Smart  **/
130554840bedSJames Smart static int
130654840bedSJames Smart lpfc_nvmet_ls_req(struct nvmet_fc_target_port *targetport,
130754840bedSJames Smart 		  void *hosthandle,
130854840bedSJames Smart 		  struct nvmefc_ls_req *pnvme_lsreq)
130954840bedSJames Smart {
131054840bedSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmet = targetport->private;
131154840bedSJames Smart 	struct lpfc_hba *phba;
131254840bedSJames Smart 	struct lpfc_nodelist *ndlp;
131354840bedSJames Smart 	int ret;
131454840bedSJames Smart 	u32 hstate;
131554840bedSJames Smart 
131654840bedSJames Smart 	if (!lpfc_nvmet)
131754840bedSJames Smart 		return -EINVAL;
131854840bedSJames Smart 
131954840bedSJames Smart 	phba = lpfc_nvmet->phba;
132054840bedSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
132154840bedSJames Smart 		return -EINVAL;
132254840bedSJames Smart 
132354840bedSJames Smart 	hstate = atomic_read(&lpfc_nvmet->state);
132454840bedSJames Smart 	if (hstate == LPFC_NVMET_INV_HOST_ACTIVE)
132554840bedSJames Smart 		return -EACCES;
132654840bedSJames Smart 
132754840bedSJames Smart 	ndlp = (struct lpfc_nodelist *)hosthandle;
132854840bedSJames Smart 
132954840bedSJames Smart 	ret = __lpfc_nvme_ls_req(phba->pport, ndlp, pnvme_lsreq,
133054840bedSJames Smart 				 lpfc_nvmet_ls_req_cmp);
133154840bedSJames Smart 
133254840bedSJames Smart 	return ret;
133354840bedSJames Smart }
133454840bedSJames Smart 
133554840bedSJames Smart /**
133654840bedSJames Smart  * lpfc_nvmet_ls_abort - Abort a prior NVME LS request
133754840bedSJames Smart  * @targetport: Transport targetport, that LS was issued from.
1338d3cb79fcSLee Jones  * @hosthandle: hosthandle set by the driver in a prior ls_rqst_rcv.
133954840bedSJames Smart  *               Driver sets this value to the ndlp pointer.
1340d3cb79fcSLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for LS to be aborted
134154840bedSJames Smart  *
134254840bedSJames Smart  * Driver registers this routine to abort an NVME LS request that is
134354840bedSJames Smart  * in progress (from the transports perspective).
134454840bedSJames Smart  **/
134554840bedSJames Smart static void
134654840bedSJames Smart lpfc_nvmet_ls_abort(struct nvmet_fc_target_port *targetport,
134754840bedSJames Smart 		    void *hosthandle,
134854840bedSJames Smart 		    struct nvmefc_ls_req *pnvme_lsreq)
134954840bedSJames Smart {
135054840bedSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmet = targetport->private;
135154840bedSJames Smart 	struct lpfc_hba *phba;
135254840bedSJames Smart 	struct lpfc_nodelist *ndlp;
135354840bedSJames Smart 	int ret;
135454840bedSJames Smart 
135554840bedSJames Smart 	phba = lpfc_nvmet->phba;
135654840bedSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
135754840bedSJames Smart 		return;
135854840bedSJames Smart 
135954840bedSJames Smart 	ndlp = (struct lpfc_nodelist *)hosthandle;
136054840bedSJames Smart 
136154840bedSJames Smart 	ret = __lpfc_nvme_ls_abort(phba->pport, ndlp, pnvme_lsreq);
136254840bedSJames Smart 	if (!ret)
136354840bedSJames Smart 		atomic_inc(&lpfc_nvmet->xmt_ls_abort);
136454840bedSJames Smart }
136554840bedSJames Smart 
1366ab723121SJames Smart static void
13674c2805aaSJames Smart lpfc_nvmet_host_release(void *hosthandle)
13684c2805aaSJames Smart {
13694c2805aaSJames Smart 	struct lpfc_nodelist *ndlp = hosthandle;
13704c2805aaSJames Smart 	struct lpfc_hba *phba = NULL;
13714c2805aaSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
13724c2805aaSJames Smart 
13734c2805aaSJames Smart 	phba = ndlp->phba;
13744c2805aaSJames Smart 	if (!phba->targetport || !phba->targetport->private)
13754c2805aaSJames Smart 		return;
13764c2805aaSJames Smart 
13774c2805aaSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13784c2805aaSJames Smart 			"6202 NVMET XPT releasing hosthandle x%px\n",
13794c2805aaSJames Smart 			hosthandle);
13804c2805aaSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
13814c2805aaSJames Smart 	atomic_set(&tgtp->state, 0);
13824c2805aaSJames Smart }
13834c2805aaSJames Smart 
13844c2805aaSJames Smart static void
1385ab723121SJames Smart lpfc_nvmet_discovery_event(struct nvmet_fc_target_port *tgtport)
1386ab723121SJames Smart {
1387ab723121SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1388ab723121SJames Smart 	struct lpfc_hba *phba;
1389ab723121SJames Smart 	uint32_t rc;
1390ab723121SJames Smart 
1391ab723121SJames Smart 	tgtp = tgtport->private;
1392ab723121SJames Smart 	phba = tgtp->phba;
1393ab723121SJames Smart 
1394ab723121SJames Smart 	rc = lpfc_issue_els_rscn(phba->pport, 0);
1395372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1396ab723121SJames Smart 			"6420 NVMET subsystem change: Notification %s\n",
1397ab723121SJames Smart 			(rc) ? "Failed" : "Sent");
1398ab723121SJames Smart }
1399ab723121SJames Smart 
1400d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
1401d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
1402d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
1403d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
1404a97ec51bSJames Smart 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
140519b58d94SJames Smart 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
140650738420SJames Smart 	.defer_rcv	= lpfc_nvmet_defer_rcv,
1407ab723121SJames Smart 	.discovery_event = lpfc_nvmet_discovery_event,
140854840bedSJames Smart 	.ls_req         = lpfc_nvmet_ls_req,
140954840bedSJames Smart 	.ls_abort       = lpfc_nvmet_ls_abort,
14104c2805aaSJames Smart 	.host_release   = lpfc_nvmet_host_release,
1411d613b6a7SJames Smart 
1412d613b6a7SJames Smart 	.max_hw_queues  = 1,
1413d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1414d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1415d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
1416d613b6a7SJames Smart 
1417d613b6a7SJames Smart 	/* optional features */
1418d613b6a7SJames Smart 	.target_features = 0,
1419d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
1420d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
142154840bedSJames Smart 	.lsrqst_priv_sz = 0,
1422d613b6a7SJames Smart };
1423d613b6a7SJames Smart 
142439aa23f9SColin Ian King static void
142566d7ce93SDick Kennedy __lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba *phba,
142666d7ce93SDick Kennedy 		struct lpfc_nvmet_ctx_info *infop)
14276c621a22SJames Smart {
14286c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf, *next_ctx_buf;
14296c621a22SJames Smart 	unsigned long flags;
14306c621a22SJames Smart 
143166d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, flags);
1432966bb5b7SJames Smart 	list_for_each_entry_safe(ctx_buf, next_ctx_buf,
143366d7ce93SDick Kennedy 				&infop->nvmet_ctx_list, list) {
14345e5b511dSJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
14356c621a22SJames Smart 		list_del_init(&ctx_buf->list);
14365e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
143766d7ce93SDick Kennedy 
143866d7ce93SDick Kennedy 		__lpfc_clear_active_sglq(phba, ctx_buf->sglq->sli4_lxritag);
14396c621a22SJames Smart 		ctx_buf->sglq->state = SGL_FREED;
14406c621a22SJames Smart 		ctx_buf->sglq->ndlp = NULL;
14416c621a22SJames Smart 
1442c4031db7SDan Carpenter 		spin_lock(&phba->sli4_hba.sgl_list_lock);
14436c621a22SJames Smart 		list_add_tail(&ctx_buf->sglq->list,
14446c621a22SJames Smart 				&phba->sli4_hba.lpfc_nvmet_sgl_list);
1445c4031db7SDan Carpenter 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
14466c621a22SJames Smart 
14476c621a22SJames Smart 		lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
14486c621a22SJames Smart 		kfree(ctx_buf->context);
14496c621a22SJames Smart 	}
145066d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, flags);
1451966bb5b7SJames Smart }
145266d7ce93SDick Kennedy 
145366d7ce93SDick Kennedy static void
145466d7ce93SDick Kennedy lpfc_nvmet_cleanup_io_context(struct lpfc_hba *phba)
145566d7ce93SDick Kennedy {
145666d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
145766d7ce93SDick Kennedy 	int i, j;
145866d7ce93SDick Kennedy 
145966d7ce93SDick Kennedy 	/* The first context list, MRQ 0 CPU 0 */
146066d7ce93SDick Kennedy 	infop = phba->sli4_hba.nvmet_ctx_info;
146166d7ce93SDick Kennedy 	if (!infop)
146266d7ce93SDick Kennedy 		return;
146366d7ce93SDick Kennedy 
146466d7ce93SDick Kennedy 	/* Cycle the the entire CPU context list for every MRQ */
146566d7ce93SDick Kennedy 	for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
1466222e9239SJames Smart 		for_each_present_cpu(j) {
1467222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, j, i);
146866d7ce93SDick Kennedy 			__lpfc_nvmet_clean_io_for_cpu(phba, infop);
146966d7ce93SDick Kennedy 		}
147066d7ce93SDick Kennedy 	}
147166d7ce93SDick Kennedy 	kfree(phba->sli4_hba.nvmet_ctx_info);
147266d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = NULL;
14736c621a22SJames Smart }
14746c621a22SJames Smart 
147539aa23f9SColin Ian King static int
14766c621a22SJames Smart lpfc_nvmet_setup_io_context(struct lpfc_hba *phba)
14776c621a22SJames Smart {
14786c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
14796c621a22SJames Smart 	struct lpfc_iocbq *nvmewqe;
14806c621a22SJames Smart 	union lpfc_wqe128 *wqe;
148166d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *last_infop;
148266d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
1483222e9239SJames Smart 	int i, j, idx, cpu;
14846c621a22SJames Smart 
14856c621a22SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
14866c621a22SJames Smart 			"6403 Allocate NVMET resources for %d XRIs\n",
14876c621a22SJames Smart 			phba->sli4_hba.nvmet_xri_cnt);
14886c621a22SJames Smart 
148966d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = kcalloc(
1490222e9239SJames Smart 		phba->sli4_hba.num_possible_cpu * phba->cfg_nvmet_mrq,
149166d7ce93SDick Kennedy 		sizeof(struct lpfc_nvmet_ctx_info), GFP_KERNEL);
149266d7ce93SDick Kennedy 	if (!phba->sli4_hba.nvmet_ctx_info) {
1493372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
149466d7ce93SDick Kennedy 				"6419 Failed allocate memory for "
149566d7ce93SDick Kennedy 				"nvmet context lists\n");
149666d7ce93SDick Kennedy 		return -ENOMEM;
149766d7ce93SDick Kennedy 	}
149866d7ce93SDick Kennedy 
149966d7ce93SDick Kennedy 	/*
150066d7ce93SDick Kennedy 	 * Assuming X CPUs in the system, and Y MRQs, allocate some
150166d7ce93SDick Kennedy 	 * lpfc_nvmet_ctx_info structures as follows:
150266d7ce93SDick Kennedy 	 *
150366d7ce93SDick Kennedy 	 * cpu0/mrq0 cpu1/mrq0 ... cpuX/mrq0
150466d7ce93SDick Kennedy 	 * cpu0/mrq1 cpu1/mrq1 ... cpuX/mrq1
150566d7ce93SDick Kennedy 	 * ...
150666d7ce93SDick Kennedy 	 * cpuX/mrqY cpuX/mrqY ... cpuX/mrqY
150766d7ce93SDick Kennedy 	 *
150866d7ce93SDick Kennedy 	 * Each line represents a MRQ "silo" containing an entry for
150966d7ce93SDick Kennedy 	 * every CPU.
151066d7ce93SDick Kennedy 	 *
151166d7ce93SDick Kennedy 	 * MRQ X is initially assumed to be associated with CPU X, thus
151266d7ce93SDick Kennedy 	 * contexts are initially distributed across all MRQs using
151366d7ce93SDick Kennedy 	 * the MRQ index (N) as follows cpuN/mrqN. When contexts are
151466d7ce93SDick Kennedy 	 * freed, the are freed to the MRQ silo based on the CPU number
151566d7ce93SDick Kennedy 	 * of the IO completion. Thus a context that was allocated for MRQ A
151666d7ce93SDick Kennedy 	 * whose IO completed on CPU B will be freed to cpuB/mrqA.
151766d7ce93SDick Kennedy 	 */
1518222e9239SJames Smart 	for_each_possible_cpu(i) {
151966d7ce93SDick Kennedy 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1520222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
152166d7ce93SDick Kennedy 			INIT_LIST_HEAD(&infop->nvmet_ctx_list);
152266d7ce93SDick Kennedy 			spin_lock_init(&infop->nvmet_ctx_list_lock);
152366d7ce93SDick Kennedy 			infop->nvmet_ctx_list_cnt = 0;
152466d7ce93SDick Kennedy 		}
152566d7ce93SDick Kennedy 	}
152666d7ce93SDick Kennedy 
152766d7ce93SDick Kennedy 	/*
152866d7ce93SDick Kennedy 	 * Setup the next CPU context info ptr for each MRQ.
152966d7ce93SDick Kennedy 	 * MRQ 0 will cycle thru CPUs 0 - X separately from
153066d7ce93SDick Kennedy 	 * MRQ 1 cycling thru CPUs 0 - X, and so on.
153166d7ce93SDick Kennedy 	 */
153266d7ce93SDick Kennedy 	for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1533222e9239SJames Smart 		last_infop = lpfc_get_ctx_list(phba,
1534222e9239SJames Smart 					       cpumask_first(cpu_present_mask),
1535222e9239SJames Smart 					       j);
1536222e9239SJames Smart 		for (i = phba->sli4_hba.num_possible_cpu - 1;  i >= 0; i--) {
153766d7ce93SDick Kennedy 			infop = lpfc_get_ctx_list(phba, i, j);
153866d7ce93SDick Kennedy 			infop->nvmet_ctx_next_cpu = last_infop;
153966d7ce93SDick Kennedy 			last_infop = infop;
154066d7ce93SDick Kennedy 		}
154166d7ce93SDick Kennedy 	}
154266d7ce93SDick Kennedy 
15436c621a22SJames Smart 	/* For all nvmet xris, allocate resources needed to process a
15446c621a22SJames Smart 	 * received command on a per xri basis.
15456c621a22SJames Smart 	 */
154666d7ce93SDick Kennedy 	idx = 0;
1547222e9239SJames Smart 	cpu = cpumask_first(cpu_present_mask);
15486c621a22SJames Smart 	for (i = 0; i < phba->sli4_hba.nvmet_xri_cnt; i++) {
15496c621a22SJames Smart 		ctx_buf = kzalloc(sizeof(*ctx_buf), GFP_KERNEL);
15506c621a22SJames Smart 		if (!ctx_buf) {
1551372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15526c621a22SJames Smart 					"6404 Ran out of memory for NVMET\n");
15536c621a22SJames Smart 			return -ENOMEM;
15546c621a22SJames Smart 		}
15556c621a22SJames Smart 
15566c621a22SJames Smart 		ctx_buf->context = kzalloc(sizeof(*ctx_buf->context),
15576c621a22SJames Smart 					   GFP_KERNEL);
15586c621a22SJames Smart 		if (!ctx_buf->context) {
15596c621a22SJames Smart 			kfree(ctx_buf);
1560372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15616c621a22SJames Smart 					"6405 Ran out of NVMET "
15626c621a22SJames Smart 					"context memory\n");
15636c621a22SJames Smart 			return -ENOMEM;
15646c621a22SJames Smart 		}
15656c621a22SJames Smart 		ctx_buf->context->ctxbuf = ctx_buf;
15667b7f551bSJames Smart 		ctx_buf->context->state = LPFC_NVME_STE_FREE;
15676c621a22SJames Smart 
15686c621a22SJames Smart 		ctx_buf->iocbq = lpfc_sli_get_iocbq(phba);
15696c621a22SJames Smart 		if (!ctx_buf->iocbq) {
15706c621a22SJames Smart 			kfree(ctx_buf->context);
15716c621a22SJames Smart 			kfree(ctx_buf);
1572372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15736c621a22SJames Smart 					"6406 Ran out of NVMET iocb/WQEs\n");
15746c621a22SJames Smart 			return -ENOMEM;
15756c621a22SJames Smart 		}
15766c621a22SJames Smart 		ctx_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
15776c621a22SJames Smart 		nvmewqe = ctx_buf->iocbq;
1578205e8240SJames Smart 		wqe = &nvmewqe->wqe;
1579bd3061baSJames Smart 
15806c621a22SJames Smart 		/* Initialize WQE */
15816c621a22SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
15826c621a22SJames Smart 
15836c621a22SJames Smart 		ctx_buf->iocbq->context1 = NULL;
15846c621a22SJames Smart 		spin_lock(&phba->sli4_hba.sgl_list_lock);
15856c621a22SJames Smart 		ctx_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, ctx_buf->iocbq);
15866c621a22SJames Smart 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
15876c621a22SJames Smart 		if (!ctx_buf->sglq) {
15886c621a22SJames Smart 			lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
15896c621a22SJames Smart 			kfree(ctx_buf->context);
15906c621a22SJames Smart 			kfree(ctx_buf);
1591372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15926c621a22SJames Smart 					"6407 Ran out of NVMET XRIs\n");
15936c621a22SJames Smart 			return -ENOMEM;
15946c621a22SJames Smart 		}
1595472e146dSJames Smart 		INIT_WORK(&ctx_buf->defer_work, lpfc_nvmet_fcp_rqst_defer_work);
159666d7ce93SDick Kennedy 
159766d7ce93SDick Kennedy 		/*
159866d7ce93SDick Kennedy 		 * Add ctx to MRQidx context list. Our initial assumption
159966d7ce93SDick Kennedy 		 * is MRQidx will be associated with CPUidx. This association
160066d7ce93SDick Kennedy 		 * can change on the fly.
160166d7ce93SDick Kennedy 		 */
1602222e9239SJames Smart 		infop = lpfc_get_ctx_list(phba, cpu, idx);
160366d7ce93SDick Kennedy 		spin_lock(&infop->nvmet_ctx_list_lock);
160466d7ce93SDick Kennedy 		list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
160566d7ce93SDick Kennedy 		infop->nvmet_ctx_list_cnt++;
160666d7ce93SDick Kennedy 		spin_unlock(&infop->nvmet_ctx_list_lock);
160766d7ce93SDick Kennedy 
160866d7ce93SDick Kennedy 		/* Spread ctx structures evenly across all MRQs */
160966d7ce93SDick Kennedy 		idx++;
1610222e9239SJames Smart 		if (idx >= phba->cfg_nvmet_mrq) {
161166d7ce93SDick Kennedy 			idx = 0;
1612222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1613222e9239SJames Smart 			continue;
1614222e9239SJames Smart 		}
1615222e9239SJames Smart 		cpu = cpumask_next(cpu, cpu_present_mask);
1616222e9239SJames Smart 		if (cpu == nr_cpu_ids)
1617222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1618222e9239SJames Smart 
16196c621a22SJames Smart 	}
162066d7ce93SDick Kennedy 
1621222e9239SJames Smart 	for_each_present_cpu(i) {
16222879265fSJames Smart 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
16232879265fSJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
162466d7ce93SDick Kennedy 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
162566d7ce93SDick Kennedy 					"6408 TOTAL NVMET ctx for CPU %d "
162632350664SJames Smart 					"MRQ %d: cnt %d nextcpu x%px\n",
162766d7ce93SDick Kennedy 					i, j, infop->nvmet_ctx_list_cnt,
162866d7ce93SDick Kennedy 					infop->nvmet_ctx_next_cpu);
162966d7ce93SDick Kennedy 		}
163066d7ce93SDick Kennedy 	}
16316c621a22SJames Smart 	return 0;
16326c621a22SJames Smart }
16336c621a22SJames Smart 
1634d613b6a7SJames Smart int
1635d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1636d613b6a7SJames Smart {
1637d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1638d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1639d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
16406c621a22SJames Smart 	int error;
1641d613b6a7SJames Smart 
1642d613b6a7SJames Smart 	if (phba->targetport)
1643d613b6a7SJames Smart 		return 0;
1644d613b6a7SJames Smart 
16456c621a22SJames Smart 	error = lpfc_nvmet_setup_io_context(phba);
16466c621a22SJames Smart 	if (error)
16476c621a22SJames Smart 		return error;
16486c621a22SJames Smart 
1649d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1650d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1651d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1652d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
1653d613b6a7SJames Smart 
16545b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
16555b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
16565b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
16574d4c4a4aSJames Smart 	 */
16584d4c4a4aSJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1659cdb42becSJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_hdw_queue;
1660f485c18dSDick Kennedy 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP;
1661d613b6a7SJames Smart 
16627d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1663d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1664d613b6a7SJames Smart 					     &phba->pcidev->dev,
1665d613b6a7SJames Smart 					     &phba->targetport);
1666166d7211SJames Smart #else
16676c621a22SJames Smart 	error = -ENOENT;
1668166d7211SJames Smart #endif
1669d613b6a7SJames Smart 	if (error) {
1670372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1671e7981a2cSDick Kennedy 				"6025 Cannot register NVME targetport x%x: "
1672e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1673e7981a2cSDick Kennedy 				error,
1674e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1675e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1676e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1677d613b6a7SJames Smart 		phba->targetport = NULL;
1678e7981a2cSDick Kennedy 		phba->nvmet_support = 0;
16796c621a22SJames Smart 
16806c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
16816c621a22SJames Smart 
1682d613b6a7SJames Smart 	} else {
1683d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
1684d613b6a7SJames Smart 			phba->targetport->private;
1685d613b6a7SJames Smart 		tgtp->phba = phba;
1686d613b6a7SJames Smart 
1687d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1688d613b6a7SJames Smart 				"6026 Registered NVME "
168932350664SJames Smart 				"targetport: x%px, private x%px "
1690e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1691d613b6a7SJames Smart 				phba->targetport, tgtp,
1692e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1693e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1694e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1695d613b6a7SJames Smart 
1696d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
1697d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
1698d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
1699d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
1700547077a4SJames Smart 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1701d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
1702d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
1703d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
17044b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_xb_set, 0);
17054b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_aborted, 0);
1706d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1707d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1708d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1709d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1710d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
1711d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1712d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
1713d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
1714d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
1715547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_release, 0);
1716d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1717d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
17184b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_xb_set, 0);
17194b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_aborted, 0);
1720d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
17214b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_xri_abort_cqe, 0);
1722547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
1723547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1724547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_unsol, 0);
1725547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_sol, 0);
1726d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
1727d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1728411de511SJames Smart 		atomic_set(&tgtp->defer_ctx, 0);
1729411de511SJames Smart 		atomic_set(&tgtp->defer_fod, 0);
1730411de511SJames Smart 		atomic_set(&tgtp->defer_wqfull, 0);
1731d613b6a7SJames Smart 	}
1732d613b6a7SJames Smart 	return error;
1733d613b6a7SJames Smart }
1734d613b6a7SJames Smart 
1735d613b6a7SJames Smart int
1736d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1737d613b6a7SJames Smart {
1738d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1739d613b6a7SJames Smart 
1740d613b6a7SJames Smart 	if (!phba->targetport)
1741d613b6a7SJames Smart 		return 0;
1742d613b6a7SJames Smart 
1743d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
174432350664SJames Smart 			 "6007 Update NVMET port x%px did x%x\n",
1745d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
1746d613b6a7SJames Smart 
1747d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
1748d613b6a7SJames Smart 	return 0;
1749d613b6a7SJames Smart }
1750d613b6a7SJames Smart 
1751318083adSJames Smart /**
1752318083adSJames Smart  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1753318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
1754318083adSJames Smart  * @axri: pointer to the nvmet xri abort wcqe structure.
1755318083adSJames Smart  *
1756318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
1757318083adSJames Smart  * NVMET aborted xri.
1758318083adSJames Smart  **/
1759318083adSJames Smart void
1760318083adSJames Smart lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
1761318083adSJames Smart 			    struct sli4_wcqe_xri_aborted *axri)
1762318083adSJames Smart {
176332b93865SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
176486c67379SJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
176586c67379SJames Smart 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
17667cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp, *next_ctxp;
17674b056682SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
176879d8c4ceSJames Smart 	struct nvmefc_tgt_fcp_req *req = NULL;
176986c67379SJames Smart 	struct lpfc_nodelist *ndlp;
177086c67379SJames Smart 	unsigned long iflag = 0;
177186c67379SJames Smart 	int rrq_empty = 0;
177286c67379SJames Smart 	bool released = false;
177386c67379SJames Smart 
177486c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
177586c67379SJames Smart 			"6317 XB aborted xri x%x rxid x%x\n", xri, rxid);
177686c67379SJames Smart 
177786c67379SJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
177886c67379SJames Smart 		return;
17794b056682SJames Smart 
17804b056682SJames Smart 	if (phba->targetport) {
17814b056682SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
17824b056682SJames Smart 		atomic_inc(&tgtp->xmt_fcp_xri_abort_cqe);
17834b056682SJames Smart 	}
17844b056682SJames Smart 
178586c67379SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
17865e5b511dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
178786c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
178886c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
178986c67379SJames Smart 				 list) {
17906c621a22SJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
179186c67379SJames Smart 			continue;
179286c67379SJames Smart 
1793c160c0f8SJames Smart 		spin_lock(&ctxp->ctxlock);
179486c67379SJames Smart 		/* Check if we already received a free context call
179586c67379SJames Smart 		 * and we have completed processing an abort situation.
179686c67379SJames Smart 		 */
17977b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_CTX_RLS &&
17987b7f551bSJames Smart 		    !(ctxp->flag & LPFC_NVME_ABORT_OP)) {
179979d8c4ceSJames Smart 			list_del_init(&ctxp->list);
180086c67379SJames Smart 			released = true;
180186c67379SJames Smart 		}
18027b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_XBUSY;
1803c160c0f8SJames Smart 		spin_unlock(&ctxp->ctxlock);
18045e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
180586c67379SJames Smart 
180686c67379SJames Smart 		rrq_empty = list_empty(&phba->active_rrq_list);
180786c67379SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, iflag);
180886c67379SJames Smart 		ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1809307e3380SJames Smart 		if (ndlp &&
181086c67379SJames Smart 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
181186c67379SJames Smart 		     ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
181286c67379SJames Smart 			lpfc_set_rrq_active(phba, ndlp,
18136c621a22SJames Smart 				ctxp->ctxbuf->sglq->sli4_lxritag,
181486c67379SJames Smart 				rxid, 1);
181586c67379SJames Smart 			lpfc_sli4_abts_err_handler(phba, ndlp, axri);
181686c67379SJames Smart 		}
181786c67379SJames Smart 
181886c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
181979d8c4ceSJames Smart 				"6318 XB aborted oxid x%x flg x%x (%x)\n",
182086c67379SJames Smart 				ctxp->oxid, ctxp->flag, released);
182186c67379SJames Smart 		if (released)
18226c621a22SJames Smart 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
18236c621a22SJames Smart 
182486c67379SJames Smart 		if (rrq_empty)
182586c67379SJames Smart 			lpfc_worker_wake_up(phba);
182686c67379SJames Smart 		return;
182786c67379SJames Smart 	}
18285e5b511dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
182986c67379SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
183079d8c4ceSJames Smart 
183179d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_xri(phba, xri);
183279d8c4ceSJames Smart 	if (ctxp) {
183379d8c4ceSJames Smart 		/*
183479d8c4ceSJames Smart 		 *  Abort already done by FW, so BA_ACC sent.
183579d8c4ceSJames Smart 		 *  However, the transport may be unaware.
183679d8c4ceSJames Smart 		 */
183779d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
183879d8c4ceSJames Smart 				"6323 NVMET Rcv ABTS xri x%x ctxp state x%x "
183979d8c4ceSJames Smart 				"flag x%x oxid x%x rxid x%x\n",
184079d8c4ceSJames Smart 				xri, ctxp->state, ctxp->flag, ctxp->oxid,
184179d8c4ceSJames Smart 				rxid);
184279d8c4ceSJames Smart 
184379d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
18447b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_ABTS_RCV;
18457b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_ABORT;
184679d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
184779d8c4ceSJames Smart 
184879d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
184979d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
185001d53c04SJames Smart 				 xri, raw_smp_processor_id(), 0);
185179d8c4ceSJames Smart 
18527cacae2aSJames Smart 		req = &ctxp->hdlrctx.fcp_req;
185379d8c4ceSJames Smart 		if (req)
185479d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport, req);
185579d8c4ceSJames Smart 	}
185632b93865SJames Smart #endif
185786c67379SJames Smart }
185886c67379SJames Smart 
185986c67379SJames Smart int
186086c67379SJames Smart lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport *vport,
186186c67379SJames Smart 			   struct fc_frame_header *fc_hdr)
186286c67379SJames Smart {
186386c67379SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
186486c67379SJames Smart 	struct lpfc_hba *phba = vport->phba;
18657cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp, *next_ctxp;
186686c67379SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
186732b93865SJames Smart 	uint32_t sid;
186879d8c4ceSJames Smart 	uint16_t oxid, xri;
186986c67379SJames Smart 	unsigned long iflag = 0;
187086c67379SJames Smart 
187132b93865SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
187279d8c4ceSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
187386c67379SJames Smart 
187486c67379SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
18755e5b511dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
187686c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
187786c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
187886c67379SJames Smart 				 list) {
187979d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
188086c67379SJames Smart 			continue;
188186c67379SJames Smart 
188232b93865SJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
188332b93865SJames Smart 
18845e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
188586c67379SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, iflag);
188686c67379SJames Smart 
188786c67379SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
18887b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_ABTS_RCV;
188986c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
189086c67379SJames Smart 
189186c67379SJames Smart 		lpfc_nvmeio_data(phba,
189286c67379SJames Smart 			"NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1893d6d189ceSBart Van Assche 			xri, raw_smp_processor_id(), 0);
189486c67379SJames Smart 
189586c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
189686c67379SJames Smart 				"6319 NVMET Rcv ABTS:acc xri x%x\n", xri);
189786c67379SJames Smart 
18987cacae2aSJames Smart 		rsp = &ctxp->hdlrctx.fcp_req;
189986c67379SJames Smart 		nvmet_fc_rcv_fcp_abort(phba->targetport, rsp);
190086c67379SJames Smart 
190186c67379SJames Smart 		/* Respond with BA_ACC accordingly */
190286c67379SJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
190386c67379SJames Smart 		return 0;
190486c67379SJames Smart 	}
19055e5b511dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
190686c67379SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
190786c67379SJames Smart 
190879d8c4ceSJames Smart 	/* check the wait list */
190979d8c4ceSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
191079d8c4ceSJames Smart 		struct rqb_dmabuf *nvmebuf;
191179d8c4ceSJames Smart 		struct fc_frame_header *fc_hdr_tmp;
191279d8c4ceSJames Smart 		u32 sid_tmp;
191379d8c4ceSJames Smart 		u16 oxid_tmp;
191479d8c4ceSJames Smart 		bool found = false;
191579d8c4ceSJames Smart 
191679d8c4ceSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
191779d8c4ceSJames Smart 
191879d8c4ceSJames Smart 		/* match by oxid and s_id */
191979d8c4ceSJames Smart 		list_for_each_entry(nvmebuf,
192079d8c4ceSJames Smart 				    &phba->sli4_hba.lpfc_nvmet_io_wait_list,
192179d8c4ceSJames Smart 				    hbuf.list) {
192279d8c4ceSJames Smart 			fc_hdr_tmp = (struct fc_frame_header *)
192379d8c4ceSJames Smart 					(nvmebuf->hbuf.virt);
192479d8c4ceSJames Smart 			oxid_tmp = be16_to_cpu(fc_hdr_tmp->fh_ox_id);
192579d8c4ceSJames Smart 			sid_tmp = sli4_sid_from_fc_hdr(fc_hdr_tmp);
192679d8c4ceSJames Smart 			if (oxid_tmp != oxid || sid_tmp != sid)
192779d8c4ceSJames Smart 				continue;
192886c67379SJames Smart 
192986c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
193079d8c4ceSJames Smart 					"6321 NVMET Rcv ABTS oxid x%x from x%x "
193179d8c4ceSJames Smart 					"is waiting for a ctxp\n",
193279d8c4ceSJames Smart 					oxid, sid);
193379d8c4ceSJames Smart 
193479d8c4ceSJames Smart 			list_del_init(&nvmebuf->hbuf.list);
193579d8c4ceSJames Smart 			phba->sli4_hba.nvmet_io_wait_cnt--;
193679d8c4ceSJames Smart 			found = true;
193779d8c4ceSJames Smart 			break;
193879d8c4ceSJames Smart 		}
193979d8c4ceSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
194079d8c4ceSJames Smart 				       iflag);
194179d8c4ceSJames Smart 
194279d8c4ceSJames Smart 		/* free buffer since already posted a new DMA buffer to RQ */
194379d8c4ceSJames Smart 		if (found) {
194479d8c4ceSJames Smart 			nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
194579d8c4ceSJames Smart 			/* Respond with BA_ACC accordingly */
194679d8c4ceSJames Smart 			lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
194779d8c4ceSJames Smart 			return 0;
194879d8c4ceSJames Smart 		}
194979d8c4ceSJames Smart 	}
195079d8c4ceSJames Smart 
195179d8c4ceSJames Smart 	/* check active list */
195279d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_oxid(phba, oxid, sid);
195379d8c4ceSJames Smart 	if (ctxp) {
195479d8c4ceSJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
195579d8c4ceSJames Smart 
195679d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
19577b7f551bSJames Smart 		ctxp->flag |= (LPFC_NVME_ABTS_RCV | LPFC_NVME_ABORT_OP);
195879d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
195979d8c4ceSJames Smart 
196079d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
196179d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
196279d8c4ceSJames Smart 				 xri, raw_smp_processor_id(), 0);
196379d8c4ceSJames Smart 
196479d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
196579d8c4ceSJames Smart 				"6322 NVMET Rcv ABTS:acc oxid x%x xri x%x "
196679d8c4ceSJames Smart 				"flag x%x state x%x\n",
196779d8c4ceSJames Smart 				ctxp->oxid, xri, ctxp->flag, ctxp->state);
196879d8c4ceSJames Smart 
19697b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_TNOTIFY) {
197079d8c4ceSJames Smart 			/* Notify the transport */
197179d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport,
19727cacae2aSJames Smart 					       &ctxp->hdlrctx.fcp_req);
197379d8c4ceSJames Smart 		} else {
19746594d31bSJames Smart 			cancel_work_sync(&ctxp->ctxbuf->defer_work);
197579d8c4ceSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
197679d8c4ceSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
197779d8c4ceSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
197879d8c4ceSJames Smart 		}
197979d8c4ceSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
198079d8c4ceSJames Smart 					       ctxp->oxid);
198179d8c4ceSJames Smart 
19826594d31bSJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
198379d8c4ceSJames Smart 		return 0;
198479d8c4ceSJames Smart 	}
198579d8c4ceSJames Smart 
198679d8c4ceSJames Smart 	lpfc_nvmeio_data(phba, "NVMET ABTS RCV: oxid x%x CPU %02x rjt %d\n",
198779d8c4ceSJames Smart 			 oxid, raw_smp_processor_id(), 1);
198879d8c4ceSJames Smart 
198979d8c4ceSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
199079d8c4ceSJames Smart 			"6320 NVMET Rcv ABTS:rjt oxid x%x\n", oxid);
199186c67379SJames Smart 
199286c67379SJames Smart 	/* Respond with BA_RJT accordingly */
199386c67379SJames Smart 	lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 0);
199486c67379SJames Smart #endif
1995b06e13c3SJens Axboe 	return 0;
1996318083adSJames Smart }
1997318083adSJames Smart 
19986e8e1c14SJames Smart static void
19996e8e1c14SJames Smart lpfc_nvmet_wqfull_flush(struct lpfc_hba *phba, struct lpfc_queue *wq,
20007cacae2aSJames Smart 			struct lpfc_async_xchg_ctx *ctxp)
20016e8e1c14SJames Smart {
20026e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
20036e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
20046e8e1c14SJames Smart 	struct lpfc_iocbq *next_nvmewqeq;
20056e8e1c14SJames Smart 	unsigned long iflags;
20066e8e1c14SJames Smart 	struct lpfc_wcqe_complete wcqe;
20076e8e1c14SJames Smart 	struct lpfc_wcqe_complete *wcqep;
20086e8e1c14SJames Smart 
20096e8e1c14SJames Smart 	pring = wq->pring;
20106e8e1c14SJames Smart 	wcqep = &wcqe;
20116e8e1c14SJames Smart 
20126e8e1c14SJames Smart 	/* Fake an ABORT error code back to cmpl routine */
20136e8e1c14SJames Smart 	memset(wcqep, 0, sizeof(struct lpfc_wcqe_complete));
20146e8e1c14SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, IOSTAT_LOCAL_REJECT);
20156e8e1c14SJames Smart 	wcqep->parameter = IOERR_ABORT_REQUESTED;
20166e8e1c14SJames Smart 
20176e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
20186e8e1c14SJames Smart 	list_for_each_entry_safe(nvmewqeq, next_nvmewqeq,
20196e8e1c14SJames Smart 				 &wq->wqfull_list, list) {
20206e8e1c14SJames Smart 		if (ctxp) {
20216e8e1c14SJames Smart 			/* Checking for a specific IO to flush */
20226e8e1c14SJames Smart 			if (nvmewqeq->context2 == ctxp) {
20236e8e1c14SJames Smart 				list_del(&nvmewqeq->list);
20246e8e1c14SJames Smart 				spin_unlock_irqrestore(&pring->ring_lock,
20256e8e1c14SJames Smart 						       iflags);
20266e8e1c14SJames Smart 				lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq,
20276e8e1c14SJames Smart 							  wcqep);
20286e8e1c14SJames Smart 				return;
20296e8e1c14SJames Smart 			}
20306e8e1c14SJames Smart 			continue;
20316e8e1c14SJames Smart 		} else {
20326e8e1c14SJames Smart 			/* Flush all IOs */
20336e8e1c14SJames Smart 			list_del(&nvmewqeq->list);
20346e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
20356e8e1c14SJames Smart 			lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq, wcqep);
20366e8e1c14SJames Smart 			spin_lock_irqsave(&pring->ring_lock, iflags);
20376e8e1c14SJames Smart 		}
20386e8e1c14SJames Smart 	}
20396e8e1c14SJames Smart 	if (!ctxp)
20406e8e1c14SJames Smart 		wq->q_flag &= ~HBA_NVMET_WQFULL;
20416e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
20426e8e1c14SJames Smart }
20436e8e1c14SJames Smart 
20446e8e1c14SJames Smart void
20456e8e1c14SJames Smart lpfc_nvmet_wqfull_process(struct lpfc_hba *phba,
20466e8e1c14SJames Smart 			  struct lpfc_queue *wq)
20476e8e1c14SJames Smart {
20486e8e1c14SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
20496e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
20506e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
20517cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
20526e8e1c14SJames Smart 	unsigned long iflags;
20536e8e1c14SJames Smart 	int rc;
20546e8e1c14SJames Smart 
20556e8e1c14SJames Smart 	/*
20566e8e1c14SJames Smart 	 * Some WQE slots are available, so try to re-issue anything
20576e8e1c14SJames Smart 	 * on the WQ wqfull_list.
20586e8e1c14SJames Smart 	 */
20596e8e1c14SJames Smart 	pring = wq->pring;
20606e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
20616e8e1c14SJames Smart 	while (!list_empty(&wq->wqfull_list)) {
20626e8e1c14SJames Smart 		list_remove_head(&wq->wqfull_list, nvmewqeq, struct lpfc_iocbq,
20636e8e1c14SJames Smart 				 list);
20646e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
20657cacae2aSJames Smart 		ctxp = (struct lpfc_async_xchg_ctx *)nvmewqeq->context2;
20661fbf9742SJames Smart 		rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
20676e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
20686e8e1c14SJames Smart 		if (rc == -EBUSY) {
20696e8e1c14SJames Smart 			/* WQ was full again, so put it back on the list */
20706e8e1c14SJames Smart 			list_add(&nvmewqeq->list, &wq->wqfull_list);
20716e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
20726e8e1c14SJames Smart 			return;
20736e8e1c14SJames Smart 		}
207479d8c4ceSJames Smart 		if (rc == WQE_SUCCESS) {
207579d8c4ceSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
207679d8c4ceSJames Smart 			if (ctxp->ts_cmd_nvme) {
20777cacae2aSJames Smart 				if (ctxp->hdlrctx.fcp_req.op == NVMET_FCOP_RSP)
207879d8c4ceSJames Smart 					ctxp->ts_status_wqput = ktime_get_ns();
207979d8c4ceSJames Smart 				else
208079d8c4ceSJames Smart 					ctxp->ts_data_wqput = ktime_get_ns();
208179d8c4ceSJames Smart 			}
208279d8c4ceSJames Smart #endif
208379d8c4ceSJames Smart 		} else {
208479d8c4ceSJames Smart 			WARN_ON(rc);
208579d8c4ceSJames Smart 		}
20866e8e1c14SJames Smart 	}
20876e8e1c14SJames Smart 	wq->q_flag &= ~HBA_NVMET_WQFULL;
20886e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
20896e8e1c14SJames Smart 
20906e8e1c14SJames Smart #endif
20916e8e1c14SJames Smart }
20926e8e1c14SJames Smart 
2093d613b6a7SJames Smart void
2094d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
2095d613b6a7SJames Smart {
20967d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2097d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
20986e8e1c14SJames Smart 	struct lpfc_queue *wq;
20996e8e1c14SJames Smart 	uint32_t qidx;
2100c41f5988SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(tport_unreg_cmp);
2101d613b6a7SJames Smart 
2102d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
2103d613b6a7SJames Smart 		return;
2104d613b6a7SJames Smart 	if (phba->targetport) {
2105d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2106cdb42becSJames Smart 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
2107c00f62e6SJames Smart 			wq = phba->sli4_hba.hdwq[qidx].io_wq;
21086e8e1c14SJames Smart 			lpfc_nvmet_wqfull_flush(phba, wq, NULL);
21096e8e1c14SJames Smart 		}
2110c41f5988SEwan D. Milne 		tgtp->tport_unreg_cmp = &tport_unreg_cmp;
2111d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
2112af6de8c6SEwan D. Milne 		if (!wait_for_completion_timeout(&tport_unreg_cmp,
21132a0fb340SJames Smart 					msecs_to_jiffies(LPFC_NVMET_WAIT_TMO)))
2114372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
211532350664SJames Smart 					"6179 Unreg targetport x%px timeout "
21162a0fb340SJames Smart 					"reached.\n", phba->targetport);
21176c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
2118d613b6a7SJames Smart 	}
2119d613b6a7SJames Smart 	phba->targetport = NULL;
2120166d7211SJames Smart #endif
2121d613b6a7SJames Smart }
2122d613b6a7SJames Smart 
2123d613b6a7SJames Smart /**
21243a8070c5SJames Smart  * lpfc_nvmet_handle_lsreq - Process an NVME LS request
2125d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
21263a8070c5SJames Smart  * @axchg: pointer to exchange context for the NVME LS request
2127d613b6a7SJames Smart  *
21283a8070c5SJames Smart  * This routine is used for processing an asychronously received NVME LS
21293a8070c5SJames Smart  * request. Any remaining validation is done and the LS is then forwarded
21303a8070c5SJames Smart  * to the nvmet-fc transport via nvmet_fc_rcv_ls_req().
21313a8070c5SJames Smart  *
21323a8070c5SJames Smart  * The calling sequence should be: nvmet_fc_rcv_ls_req() -> (processing)
21333a8070c5SJames Smart  * -> lpfc_nvmet_xmt_ls_rsp/cmp -> req->done.
21343a8070c5SJames Smart  * lpfc_nvme_xmt_ls_rsp_cmp should free the allocated axchg.
21353a8070c5SJames Smart  *
21363a8070c5SJames Smart  * Returns 0 if LS was handled and delivered to the transport
21373a8070c5SJames Smart  * Returns 1 if LS failed to be handled and should be dropped
21383a8070c5SJames Smart  */
21393a8070c5SJames Smart int
21403a8070c5SJames Smart lpfc_nvmet_handle_lsreq(struct lpfc_hba *phba,
21413a8070c5SJames Smart 			struct lpfc_async_xchg_ctx *axchg)
2142d613b6a7SJames Smart {
21437d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
21443a8070c5SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = phba->targetport->private;
21453a8070c5SJames Smart 	uint32_t *payload = axchg->payload;
21463a8070c5SJames Smart 	int rc;
2147d613b6a7SJames Smart 
2148d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
21493a8070c5SJames Smart 
21504c2805aaSJames Smart 	/*
21514c2805aaSJames Smart 	 * Driver passes the ndlp as the hosthandle argument allowing
21524c2805aaSJames Smart 	 * the transport to generate LS requests for any associateions
21534c2805aaSJames Smart 	 * that are created.
21544c2805aaSJames Smart 	 */
21554c2805aaSJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, axchg->ndlp, &axchg->ls_rsp,
21563a8070c5SJames Smart 				 axchg->payload, axchg->size);
2157d613b6a7SJames Smart 
2158d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2159ce1b591cSJames Smart 			"6037 NVMET Unsol rcv: sz %d rc %d: %08x %08x %08x "
21603a8070c5SJames Smart 			"%08x %08x %08x\n", axchg->size, rc,
2161d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
2162d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
21632b65e182SJames Smart 
21643a8070c5SJames Smart 	if (!rc) {
2165d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
21663a8070c5SJames Smart 		return 0;
2167d613b6a7SJames Smart 	}
21682b65e182SJames Smart 
2169d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
2170166d7211SJames Smart #endif
21713a8070c5SJames Smart 	return 1;
2172d613b6a7SJames Smart }
2173d613b6a7SJames Smart 
2174472e146dSJames Smart static void
2175472e146dSJames Smart lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf)
2176472e146dSJames Smart {
2177472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
21787cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp = ctx_buf->context;
2179472e146dSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
2180472e146dSJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
2181472e146dSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
21824552e0f6SJames Smart 	uint32_t *payload, qno;
2183472e146dSJames Smart 	uint32_t rc;
2184472e146dSJames Smart 	unsigned long iflags;
2185472e146dSJames Smart 
2186472e146dSJames Smart 	if (!nvmebuf) {
2187372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2188472e146dSJames Smart 			"6159 process_rcv_fcp_req, nvmebuf is NULL, "
2189472e146dSJames Smart 			"oxid: x%x flg: x%x state: x%x\n",
2190472e146dSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
2191472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
2192472e146dSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2193472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2194472e146dSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
2195472e146dSJames Smart 						 ctxp->oxid);
2196472e146dSJames Smart 		return;
2197472e146dSJames Smart 	}
2198472e146dSJames Smart 
21997b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABTS_RCV) {
2200372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
220179d8c4ceSJames Smart 				"6324 IO oxid x%x aborted\n",
220279d8c4ceSJames Smart 				ctxp->oxid);
220379d8c4ceSJames Smart 		return;
220479d8c4ceSJames Smart 	}
220579d8c4ceSJames Smart 
2206472e146dSJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
2207472e146dSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
22087b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_TNOTIFY;
2209d74a89aaSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2210d74a89aaSJames Smart 	if (ctxp->ts_isr_cmd)
2211d74a89aaSJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
2212d74a89aaSJames Smart #endif
2213472e146dSJames Smart 	/*
2214472e146dSJames Smart 	 * The calling sequence should be:
2215472e146dSJames Smart 	 * nvmet_fc_rcv_fcp_req->lpfc_nvmet_xmt_fcp_op/cmp- req->done
2216472e146dSJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
2217472e146dSJames Smart 	 * When we return from nvmet_fc_rcv_fcp_req, all relevant info
2218472e146dSJames Smart 	 * the NVME command / FC header is stored.
2219472e146dSJames Smart 	 * A buffer has already been reposted for this IO, so just free
2220472e146dSJames Smart 	 * the nvmebuf.
2221472e146dSJames Smart 	 */
22227cacae2aSJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->hdlrctx.fcp_req,
2223472e146dSJames Smart 				  payload, ctxp->size);
2224472e146dSJames Smart 	/* Process FCP command */
2225472e146dSJames Smart 	if (rc == 0) {
2226472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
22274552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
22287b7f551bSJames Smart 		if ((ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) ||
22294552e0f6SJames Smart 		    (nvmebuf != ctxp->rqb_buffer)) {
22304552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22314552e0f6SJames Smart 			return;
22324552e0f6SJames Smart 		}
22334552e0f6SJames Smart 		ctxp->rqb_buffer = NULL;
22344552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22354552e0f6SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
2236472e146dSJames Smart 		return;
2237472e146dSJames Smart 	}
2238472e146dSJames Smart 
2239472e146dSJames Smart 	/* Processing of FCP command is deferred */
2240472e146dSJames Smart 	if (rc == -EOVERFLOW) {
2241472e146dSJames Smart 		lpfc_nvmeio_data(phba, "NVMET RCV BUSY: xri x%x sz %d "
2242472e146dSJames Smart 				 "from %06x\n",
2243472e146dSJames Smart 				 ctxp->oxid, ctxp->size, ctxp->sid);
2244472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
2245472e146dSJames Smart 		atomic_inc(&tgtp->defer_fod);
22464552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
22477b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) {
22484552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22494552e0f6SJames Smart 			return;
22504552e0f6SJames Smart 		}
22514552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22524552e0f6SJames Smart 		/*
22534552e0f6SJames Smart 		 * Post a replacement DMA buffer to RQ and defer
22544552e0f6SJames Smart 		 * freeing rcv buffer till .defer_rcv callback
22554552e0f6SJames Smart 		 */
22564552e0f6SJames Smart 		qno = nvmebuf->idx;
22574552e0f6SJames Smart 		lpfc_post_rq_buffer(
22584552e0f6SJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
22594552e0f6SJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2260472e146dSJames Smart 		return;
2261472e146dSJames Smart 	}
22627b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_TNOTIFY;
2263472e146dSJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2264372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2265472e146dSJames Smart 			"2582 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
2266472e146dSJames Smart 			ctxp->oxid, rc,
2267472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_in),
2268472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_out),
2269472e146dSJames Smart 			atomic_read(&tgtp->xmt_fcp_release));
2270472e146dSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
2271472e146dSJames Smart 			 ctxp->oxid, ctxp->size, ctxp->sid);
2272472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflags);
2273472e146dSJames Smart 	lpfc_nvmet_defer_release(phba, ctxp);
2274472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2275472e146dSJames Smart 	lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
2276472e146dSJames Smart #endif
2277472e146dSJames Smart }
2278472e146dSJames Smart 
2279472e146dSJames Smart static void
2280472e146dSJames Smart lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *work)
2281472e146dSJames Smart {
2282472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2283472e146dSJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf =
2284472e146dSJames Smart 		container_of(work, struct lpfc_nvmet_ctxbuf, defer_work);
2285472e146dSJames Smart 
2286472e146dSJames Smart 	lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2287472e146dSJames Smart #endif
2288472e146dSJames Smart }
2289472e146dSJames Smart 
229066d7ce93SDick Kennedy static struct lpfc_nvmet_ctxbuf *
229166d7ce93SDick Kennedy lpfc_nvmet_replenish_context(struct lpfc_hba *phba,
229266d7ce93SDick Kennedy 			     struct lpfc_nvmet_ctx_info *current_infop)
229366d7ce93SDick Kennedy {
22942299e432SDick Kennedy #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
229566d7ce93SDick Kennedy 	struct lpfc_nvmet_ctxbuf *ctx_buf = NULL;
229666d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *get_infop;
229766d7ce93SDick Kennedy 	int i;
229866d7ce93SDick Kennedy 
229966d7ce93SDick Kennedy 	/*
230066d7ce93SDick Kennedy 	 * The current_infop for the MRQ a NVME command IU was received
230166d7ce93SDick Kennedy 	 * on is empty. Our goal is to replenish this MRQs context
230266d7ce93SDick Kennedy 	 * list from a another CPUs.
230366d7ce93SDick Kennedy 	 *
230466d7ce93SDick Kennedy 	 * First we need to pick a context list to start looking on.
230566d7ce93SDick Kennedy 	 * nvmet_ctx_start_cpu has available context the last time
230666d7ce93SDick Kennedy 	 * we needed to replenish this CPU where nvmet_ctx_next_cpu
230766d7ce93SDick Kennedy 	 * is just the next sequential CPU for this MRQ.
230866d7ce93SDick Kennedy 	 */
230966d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_start_cpu)
231066d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_start_cpu;
231166d7ce93SDick Kennedy 	else
231266d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_next_cpu;
231366d7ce93SDick Kennedy 
2314222e9239SJames Smart 	for (i = 0; i < phba->sli4_hba.num_possible_cpu; i++) {
231566d7ce93SDick Kennedy 		if (get_infop == current_infop) {
231666d7ce93SDick Kennedy 			get_infop = get_infop->nvmet_ctx_next_cpu;
231766d7ce93SDick Kennedy 			continue;
231866d7ce93SDick Kennedy 		}
231966d7ce93SDick Kennedy 		spin_lock(&get_infop->nvmet_ctx_list_lock);
232066d7ce93SDick Kennedy 
232166d7ce93SDick Kennedy 		/* Just take the entire context list, if there are any */
232266d7ce93SDick Kennedy 		if (get_infop->nvmet_ctx_list_cnt) {
232366d7ce93SDick Kennedy 			list_splice_init(&get_infop->nvmet_ctx_list,
232466d7ce93SDick Kennedy 				    &current_infop->nvmet_ctx_list);
232566d7ce93SDick Kennedy 			current_infop->nvmet_ctx_list_cnt =
232666d7ce93SDick Kennedy 				get_infop->nvmet_ctx_list_cnt - 1;
232766d7ce93SDick Kennedy 			get_infop->nvmet_ctx_list_cnt = 0;
232866d7ce93SDick Kennedy 			spin_unlock(&get_infop->nvmet_ctx_list_lock);
232966d7ce93SDick Kennedy 
233066d7ce93SDick Kennedy 			current_infop->nvmet_ctx_start_cpu = get_infop;
233166d7ce93SDick Kennedy 			list_remove_head(&current_infop->nvmet_ctx_list,
233266d7ce93SDick Kennedy 					 ctx_buf, struct lpfc_nvmet_ctxbuf,
233366d7ce93SDick Kennedy 					 list);
233466d7ce93SDick Kennedy 			return ctx_buf;
233566d7ce93SDick Kennedy 		}
233666d7ce93SDick Kennedy 
233766d7ce93SDick Kennedy 		/* Otherwise, move on to the next CPU for this MRQ */
233866d7ce93SDick Kennedy 		spin_unlock(&get_infop->nvmet_ctx_list_lock);
233966d7ce93SDick Kennedy 		get_infop = get_infop->nvmet_ctx_next_cpu;
234066d7ce93SDick Kennedy 	}
234166d7ce93SDick Kennedy 
23422299e432SDick Kennedy #endif
234366d7ce93SDick Kennedy 	/* Nothing found, all contexts for the MRQ are in-flight */
234466d7ce93SDick Kennedy 	return NULL;
234566d7ce93SDick Kennedy }
234666d7ce93SDick Kennedy 
2347d613b6a7SJames Smart /**
2348d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
2349d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
235066d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2351d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
2352d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2353d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2354d613b6a7SJames Smart  *
2355d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
2356d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
2357d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
2358d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
2359d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
2360d613b6a7SJames Smart  * of the discovery state machine.
2361d613b6a7SJames Smart  **/
2362d613b6a7SJames Smart static void
2363d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
236466d7ce93SDick Kennedy 			    uint32_t idx,
2365d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
2366d74a89aaSJames Smart 			    uint64_t isr_timestamp,
2367d74a89aaSJames Smart 			    uint8_t cqflag)
2368d613b6a7SJames Smart {
23697cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
2370d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2371d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
23726c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
237366d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *current_infop;
2374472e146dSJames Smart 	uint32_t size, oxid, sid, qno;
23756c621a22SJames Smart 	unsigned long iflag;
237666d7ce93SDick Kennedy 	int current_cpu;
2377d613b6a7SJames Smart 
237873626173SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_TARGET_FC))
237973626173SArnd Bergmann 		return;
238073626173SArnd Bergmann 
23816c621a22SJames Smart 	ctx_buf = NULL;
2382d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
2383372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
23846c621a22SJames Smart 				"6157 NVMET FCP Drop IO\n");
2385472e146dSJames Smart 		if (nvmebuf)
2386472e146dSJames Smart 			lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2387472e146dSJames Smart 		return;
2388d613b6a7SJames Smart 	}
2389d613b6a7SJames Smart 
239066d7ce93SDick Kennedy 	/*
239166d7ce93SDick Kennedy 	 * Get a pointer to the context list for this MRQ based on
239266d7ce93SDick Kennedy 	 * the CPU this MRQ IRQ is associated with. If the CPU association
239366d7ce93SDick Kennedy 	 * changes from our initial assumption, the context list could
239466d7ce93SDick Kennedy 	 * be empty, thus it would need to be replenished with the
239566d7ce93SDick Kennedy 	 * context list from another CPU for this MRQ.
239666d7ce93SDick Kennedy 	 */
2397d6d189ceSBart Van Assche 	current_cpu = raw_smp_processor_id();
239866d7ce93SDick Kennedy 	current_infop = lpfc_get_ctx_list(phba, current_cpu, idx);
239966d7ce93SDick Kennedy 	spin_lock_irqsave(&current_infop->nvmet_ctx_list_lock, iflag);
240066d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_list_cnt) {
240166d7ce93SDick Kennedy 		list_remove_head(&current_infop->nvmet_ctx_list,
24026c621a22SJames Smart 				 ctx_buf, struct lpfc_nvmet_ctxbuf, list);
240366d7ce93SDick Kennedy 		current_infop->nvmet_ctx_list_cnt--;
2404966bb5b7SJames Smart 	} else {
240566d7ce93SDick Kennedy 		ctx_buf = lpfc_nvmet_replenish_context(phba, current_infop);
24066c621a22SJames Smart 	}
240766d7ce93SDick Kennedy 	spin_unlock_irqrestore(&current_infop->nvmet_ctx_list_lock, iflag);
24086c621a22SJames Smart 
2409a8cf5dfeSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
2410a8cf5dfeSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2411a8cf5dfeSJames Smart 	size = nvmebuf->bytes_recv;
2412a8cf5dfeSJames Smart 
2413a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2414840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
2415840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->rcv_io);
241663df6d63SJames Smart 		if (idx != current_cpu)
241763df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
241863df6d63SJames Smart 					"6703 CPU Check rcv: "
241963df6d63SJames Smart 					"cpu %d expect %d\n",
242063df6d63SJames Smart 					current_cpu, idx);
2421a8cf5dfeSJames Smart 	}
2422a8cf5dfeSJames Smart #endif
2423a8cf5dfeSJames Smart 
2424a8cf5dfeSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d CPU %02x\n",
2425d6d189ceSBart Van Assche 			 oxid, size, raw_smp_processor_id());
2426a8cf5dfeSJames Smart 
2427411de511SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2428411de511SJames Smart 
24296c621a22SJames Smart 	if (!ctx_buf) {
2430a8cf5dfeSJames Smart 		/* Queue this NVME IO to process later */
2431a8cf5dfeSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
2432a8cf5dfeSJames Smart 		list_add_tail(&nvmebuf->hbuf.list,
2433a8cf5dfeSJames Smart 			      &phba->sli4_hba.lpfc_nvmet_io_wait_list);
2434a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt++;
2435a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_total++;
2436a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
2437a8cf5dfeSJames Smart 				       iflag);
2438a8cf5dfeSJames Smart 
2439a8cf5dfeSJames Smart 		/* Post a brand new DMA buffer to RQ */
2440a8cf5dfeSJames Smart 		qno = nvmebuf->idx;
2441a8cf5dfeSJames Smart 		lpfc_post_rq_buffer(
2442a8cf5dfeSJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
2443a8cf5dfeSJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2444411de511SJames Smart 
2445411de511SJames Smart 		atomic_inc(&tgtp->defer_ctx);
2446a8cf5dfeSJames Smart 		return;
24476c621a22SJames Smart 	}
2448d613b6a7SJames Smart 
2449d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
2450d613b6a7SJames Smart 
24517cacae2aSJames Smart 	ctxp = (struct lpfc_async_xchg_ctx *)ctx_buf->context;
245279d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
245379d8c4ceSJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.t_active_ctx_list);
245479d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
24557b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_FREE) {
2456372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2457ce1b591cSJames Smart 				"6414 NVMET Context corrupt %d %d oxid x%x\n",
2458ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
2459ce1b591cSJames Smart 	}
2460d613b6a7SJames Smart 	ctxp->wqeq = NULL;
2461d613b6a7SJames Smart 	ctxp->offset = 0;
2462d613b6a7SJames Smart 	ctxp->phba = phba;
2463d613b6a7SJames Smart 	ctxp->size = size;
2464d613b6a7SJames Smart 	ctxp->oxid = oxid;
2465d613b6a7SJames Smart 	ctxp->sid = sid;
246666d7ce93SDick Kennedy 	ctxp->idx = idx;
24677b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_RCV;
2468d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
2469d613b6a7SJames Smart 	ctxp->flag = 0;
24706c621a22SJames Smart 	ctxp->ctxbuf = ctx_buf;
2471cbc5de1bSJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
24721fbf9742SJames Smart 	ctxp->hdwq = NULL;
24732b7824d0SJames Smart 	spin_lock_init(&ctxp->ctxlock);
2474d613b6a7SJames Smart 
24752b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2476d74a89aaSJames Smart 	if (isr_timestamp)
24772b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
2478d74a89aaSJames Smart 	ctxp->ts_cmd_nvme = 0;
24792b65e182SJames Smart 	ctxp->ts_nvme_data = 0;
24802b65e182SJames Smart 	ctxp->ts_data_wqput = 0;
24812b65e182SJames Smart 	ctxp->ts_isr_data = 0;
24822b65e182SJames Smart 	ctxp->ts_data_nvme = 0;
24832b65e182SJames Smart 	ctxp->ts_nvme_status = 0;
24842b65e182SJames Smart 	ctxp->ts_status_wqput = 0;
24852b65e182SJames Smart 	ctxp->ts_isr_status = 0;
24862b65e182SJames Smart 	ctxp->ts_status_nvme = 0;
24872b65e182SJames Smart #endif
24882b65e182SJames Smart 
2489d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
2490d74a89aaSJames Smart 	/* check for cq processing load */
2491d74a89aaSJames Smart 	if (!cqflag) {
2492472e146dSJames Smart 		lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2493d74a89aaSJames Smart 		return;
2494d74a89aaSJames Smart 	}
2495d74a89aaSJames Smart 
2496d74a89aaSJames Smart 	if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
2497d74a89aaSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2498372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2499d74a89aaSJames Smart 				"6325 Unable to queue work for oxid x%x. "
2500d74a89aaSJames Smart 				"FCP Drop IO [x%x x%x x%x]\n",
2501d74a89aaSJames Smart 				ctxp->oxid,
2502d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_in),
2503d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_out),
2504d74a89aaSJames Smart 				atomic_read(&tgtp->xmt_fcp_release));
2505d74a89aaSJames Smart 
2506d74a89aaSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
2507d74a89aaSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2508d74a89aaSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
2509d74a89aaSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
2510d74a89aaSJames Smart 	}
2511d613b6a7SJames Smart }
2512d613b6a7SJames Smart 
2513d613b6a7SJames Smart /**
2514d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
2515d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
251666d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2517d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2518d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2519d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2520d613b6a7SJames Smart  *
2521d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2522d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2523d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2524d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
2525d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2526d613b6a7SJames Smart  **/
2527d613b6a7SJames Smart void
2528d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
252966d7ce93SDick Kennedy 			   uint32_t idx,
2530d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
2531d74a89aaSJames Smart 			   uint64_t isr_timestamp,
2532d74a89aaSJames Smart 			   uint8_t cqflag)
2533d613b6a7SJames Smart {
253443bfea1bSJames Smart 	if (!nvmebuf) {
2535372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
253643bfea1bSJames Smart 				"3167 NVMET FCP Drop IO\n");
253743bfea1bSJames Smart 		return;
253843bfea1bSJames Smart 	}
2539d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
25406c621a22SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2541d613b6a7SJames Smart 		return;
2542d613b6a7SJames Smart 	}
2543d74a89aaSJames Smart 	lpfc_nvmet_unsol_fcp_buffer(phba, idx, nvmebuf, isr_timestamp, cqflag);
2544d613b6a7SJames Smart }
2545d613b6a7SJames Smart 
2546d613b6a7SJames Smart /**
2547d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
2548d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
2549d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
2550d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
2551d613b6a7SJames Smart  * @rspsize: size of the NVME command.
2552d613b6a7SJames Smart  *
2553d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
2554d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
2555d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
2556d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
2557d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
2558d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
2559d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
2560d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
2561d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
2562d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
2563d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
2564d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
2565d613b6a7SJames Smart  * reference for the command's callback function to access later.
2566d613b6a7SJames Smart  *
2567d613b6a7SJames Smart  * Return code
2568d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
2569d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
2570d613b6a7SJames Smart  **/
2571d613b6a7SJames Smart static struct lpfc_iocbq *
2572d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
25737cacae2aSJames Smart 		       struct lpfc_async_xchg_ctx *ctxp,
2574d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
2575d613b6a7SJames Smart {
2576d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2577d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2578205e8240SJames Smart 	union lpfc_wqe128 *wqe;
2579d613b6a7SJames Smart 
2580d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2581372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2582ce1b591cSJames Smart 				"6104 NVMET prep LS wqe: link err: "
2583ce1b591cSJames Smart 				"NPORT x%x oxid:x%x ste %d\n",
2584ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2585d613b6a7SJames Smart 		return NULL;
2586d613b6a7SJames Smart 	}
2587d613b6a7SJames Smart 
2588d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
2589d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
2590d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2591372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2592ce1b591cSJames Smart 				"6105 NVMET prep LS wqe: No WQE: "
2593ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2594ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2595d613b6a7SJames Smart 		return NULL;
2596d613b6a7SJames Smart 	}
2597d613b6a7SJames Smart 
2598d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2599307e3380SJames Smart 	if (!ndlp ||
2600d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2601d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2602372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2603ce1b591cSJames Smart 				"6106 NVMET prep LS wqe: No ndlp: "
2604ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2605ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2606d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2607d613b6a7SJames Smart 	}
2608d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
2609d613b6a7SJames Smart 
2610d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
2611d613b6a7SJames Smart 	nvmewqe->context1 = lpfc_nlp_get(ndlp);
2612d613b6a7SJames Smart 	if (nvmewqe->context1 == NULL)
2613d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2614d613b6a7SJames Smart 	nvmewqe->context2 = ctxp;
2615d613b6a7SJames Smart 
2616d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
2617d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
2618d613b6a7SJames Smart 
2619d613b6a7SJames Smart 	/* Words 0 - 2 */
2620d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2621d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
2622d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
2623d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
2624d613b6a7SJames Smart 
2625d613b6a7SJames Smart 	/* Word 3 */
2626d613b6a7SJames Smart 
2627d613b6a7SJames Smart 	/* Word 4 */
2628d613b6a7SJames Smart 
2629d613b6a7SJames Smart 	/* Word 5 */
2630d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
2631d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
2632d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
26338b361639SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
2634d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
2635d613b6a7SJames Smart 
2636d613b6a7SJames Smart 	/* Word 6 */
2637d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
2638d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2639d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
2640d613b6a7SJames Smart 
2641d613b6a7SJames Smart 	/* Word 7 */
2642d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
2643d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
2644d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
2645d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
2646d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
2647d613b6a7SJames Smart 
2648d613b6a7SJames Smart 	/* Word 8 */
2649d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
2650d613b6a7SJames Smart 
2651d613b6a7SJames Smart 	/* Word 9 */
2652d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
2653d613b6a7SJames Smart 	/* Needs to be set by caller */
2654d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
2655d613b6a7SJames Smart 
2656d613b6a7SJames Smart 	/* Word 10 */
2657d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
2658d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2659d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
2660d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
2661d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
2662d613b6a7SJames Smart 
2663d613b6a7SJames Smart 	/* Word 11 */
2664d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
2665d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
2666d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
2667d613b6a7SJames Smart 	       OTHER_COMMAND);
2668d613b6a7SJames Smart 
2669d613b6a7SJames Smart 	/* Word 12 */
2670d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
2671d613b6a7SJames Smart 
2672d613b6a7SJames Smart 	nvmewqe->retry = 1;
2673d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2674d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2675d613b6a7SJames Smart 	nvmewqe->iocb_flag |= LPFC_IO_NVME_LS;
2676d613b6a7SJames Smart 
2677ce1b591cSJames Smart 	/* Xmit NVMET response to remote NPORT <did> */
2678d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2679ce1b591cSJames Smart 			"6039 Xmit NVMET LS response to remote "
2680d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
2681d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
2682d613b6a7SJames Smart 			rspsize);
2683d613b6a7SJames Smart 	return nvmewqe;
2684d613b6a7SJames Smart 
2685d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
2686d613b6a7SJames Smart 	nvmewqe->context2 = NULL;
2687d613b6a7SJames Smart 	nvmewqe->context3 = NULL;
2688d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
2689d613b6a7SJames Smart 	return NULL;
2690d613b6a7SJames Smart }
2691d613b6a7SJames Smart 
2692d613b6a7SJames Smart 
2693d613b6a7SJames Smart static struct lpfc_iocbq *
2694d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
26957cacae2aSJames Smart 			struct lpfc_async_xchg_ctx *ctxp)
2696d613b6a7SJames Smart {
26977cacae2aSJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->hdlrctx.fcp_req;
2698d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2699d613b6a7SJames Smart 	struct sli4_sge *sgl;
2700d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2701d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2702d613b6a7SJames Smart 	struct scatterlist *sgel;
2703d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
27040bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
2705d613b6a7SJames Smart 	dma_addr_t physaddr;
2706e7f40349SJames Smart 	int i, cnt, nsegs;
27070bc2b7c5SJames Smart 	int do_pbde;
2708d613b6a7SJames Smart 	int xc = 1;
2709d613b6a7SJames Smart 
2710d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2711372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2712ce1b591cSJames Smart 				"6107 NVMET prep FCP wqe: link err:"
2713ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2714ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2715d613b6a7SJames Smart 		return NULL;
2716d613b6a7SJames Smart 	}
2717d613b6a7SJames Smart 
2718d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2719307e3380SJames Smart 	if (!ndlp ||
2720d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2721d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2722372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2723ce1b591cSJames Smart 				"6108 NVMET prep FCP wqe: no ndlp: "
2724ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2725ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2726d613b6a7SJames Smart 		return NULL;
2727d613b6a7SJames Smart 	}
2728d613b6a7SJames Smart 
272981e6a637SJames Smart 	if (rsp->sg_cnt > lpfc_tgttemplate.max_sgl_segments) {
2730372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2731ce1b591cSJames Smart 				"6109 NVMET prep FCP wqe: seg cnt err: "
2732ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d cnt %d\n",
2733ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state,
2734ce1b591cSJames Smart 				phba->cfg_nvme_seg_cnt);
2735d613b6a7SJames Smart 		return NULL;
2736d613b6a7SJames Smart 	}
2737e7f40349SJames Smart 	nsegs = rsp->sg_cnt;
2738d613b6a7SJames Smart 
2739d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2740d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
2741d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2742d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
27436c621a22SJames Smart 		nvmewqe = ctxp->ctxbuf->iocbq;
2744d613b6a7SJames Smart 		if (nvmewqe == NULL) {
2745372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2746ce1b591cSJames Smart 					"6110 NVMET prep FCP wqe: No "
2747ce1b591cSJames Smart 					"WQE: NPORT x%x oxid x%x ste %d\n",
2748ce1b591cSJames Smart 					ctxp->sid, ctxp->oxid, ctxp->state);
2749d613b6a7SJames Smart 			return NULL;
2750d613b6a7SJames Smart 		}
2751d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
2752d613b6a7SJames Smart 		xc = 0; /* create new XRI */
2753d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
2754d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
2755d613b6a7SJames Smart 	}
2756d613b6a7SJames Smart 
2757d613b6a7SJames Smart 	/* Sanity check */
27587b7f551bSJames Smart 	if (((ctxp->state == LPFC_NVME_STE_RCV) &&
2759d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
27607b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_DATA)) {
2761205e8240SJames Smart 		wqe = &nvmewqe->wqe;
2762d613b6a7SJames Smart 	} else {
2763372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2764ce1b591cSJames Smart 				"6111 Wrong state NVMET FCP: %d  cnt %d\n",
2765ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt);
2766d613b6a7SJames Smart 		return NULL;
2767d613b6a7SJames Smart 	}
2768d613b6a7SJames Smart 
27696c621a22SJames Smart 	sgl  = (struct sli4_sge *)ctxp->ctxbuf->sglq->sgl;
2770d613b6a7SJames Smart 	switch (rsp->op) {
2771d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
2772d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
2773bd3061baSJames Smart 		/* From the tsend template, initialize words 7 - 11 */
2774bd3061baSJames Smart 		memcpy(&wqe->words[7],
2775bd3061baSJames Smart 		       &lpfc_tsend_cmd_template.words[7],
2776bd3061baSJames Smart 		       sizeof(uint32_t) * 5);
2777bd3061baSJames Smart 
2778d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2779d613b6a7SJames Smart 		sgel = &rsp->sg[0];
2780d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2781d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2782d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
2783d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
2784d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
2785d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2786d613b6a7SJames Smart 
2787d613b6a7SJames Smart 		/* Word 3 */
2788d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
2789d613b6a7SJames Smart 
2790d613b6a7SJames Smart 		/* Word 4 */
2791d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
2792d613b6a7SJames Smart 
2793d613b6a7SJames Smart 		/* Word 5 */
2794bd3061baSJames Smart 		wqe->fcp_tsend.reserved = 0;
2795d613b6a7SJames Smart 
2796d613b6a7SJames Smart 		/* Word 6 */
2797d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
2798d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2799d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
2800d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2801d613b6a7SJames Smart 
2802bd3061baSJames Smart 		/* Word 7 - set ar later */
2803d613b6a7SJames Smart 
2804d613b6a7SJames Smart 		/* Word 8 */
2805d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
2806d613b6a7SJames Smart 
2807d613b6a7SJames Smart 		/* Word 9 */
2808d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
2809d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
2810d613b6a7SJames Smart 
2811bd3061baSJames Smart 		/* Word 10 - set wqes later, in template xc=1 */
2812bd3061baSJames Smart 		if (!xc)
2813bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 0);
2814d613b6a7SJames Smart 
2815bd3061baSJames Smart 		/* Word 11 - set sup, irsp, irsplen later */
2816bd3061baSJames Smart 		do_pbde = 0;
2817d613b6a7SJames Smart 
2818d613b6a7SJames Smart 		/* Word 12 */
2819d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2820d613b6a7SJames Smart 
2821d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
2822d613b6a7SJames Smart 		sgl->addr_hi = 0;
2823d613b6a7SJames Smart 		sgl->addr_lo = 0;
2824d613b6a7SJames Smart 		sgl->word2 = 0;
2825d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2826d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2827d613b6a7SJames Smart 		sgl->sge_len = 0;
2828d613b6a7SJames Smart 		sgl++;
2829d613b6a7SJames Smart 		sgl->addr_hi = 0;
2830d613b6a7SJames Smart 		sgl->addr_lo = 0;
2831d613b6a7SJames Smart 		sgl->word2 = 0;
2832d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2833d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2834d613b6a7SJames Smart 		sgl->sge_len = 0;
2835d613b6a7SJames Smart 		sgl++;
2836d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
2837d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
2838bd3061baSJames Smart 
2839bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2840bd3061baSJames Smart 
284120aefac3SJames Smart 			if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
284220aefac3SJames Smart 				if (ndlp->nlp_flag & NLP_SUPPRESS_RSP)
284320aefac3SJames Smart 					bf_set(wqe_sup,
284420aefac3SJames Smart 					       &wqe->fcp_tsend.wqe_com, 1);
2845d613b6a7SJames Smart 			} else {
2846d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
2847d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
2848d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
2849d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
2850d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
2851d613b6a7SJames Smart 				       rsp->rsplen);
2852d613b6a7SJames Smart 			}
2853d613b6a7SJames Smart 		} else {
2854d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
2855d613b6a7SJames Smart 
2856bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2857d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
2858d613b6a7SJames Smart 		}
2859d613b6a7SJames Smart 		break;
2860d613b6a7SJames Smart 
2861d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
2862bd3061baSJames Smart 		/* From the treceive template, initialize words 3 - 11 */
2863bd3061baSJames Smart 		memcpy(&wqe->words[3],
2864bd3061baSJames Smart 		       &lpfc_treceive_cmd_template.words[3],
2865bd3061baSJames Smart 		       sizeof(uint32_t) * 9);
2866bd3061baSJames Smart 
2867ea85a20cSJames Smart 		/* Words 0 - 2 : First SGE is skipped, set invalid BDE type */
2868ea85a20cSJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = LPFC_SGE_TYPE_SKIP;
2869ea85a20cSJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = 0;
2870ea85a20cSJames Smart 		wqe->fcp_treceive.bde.addrLow = 0;
2871ea85a20cSJames Smart 		wqe->fcp_treceive.bde.addrHigh = 0;
2872d613b6a7SJames Smart 
2873d613b6a7SJames Smart 		/* Word 4 */
2874d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
2875d613b6a7SJames Smart 
2876d613b6a7SJames Smart 		/* Word 6 */
2877d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
2878d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2879d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
2880d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2881d613b6a7SJames Smart 
2882d613b6a7SJames Smart 		/* Word 7 */
2883d613b6a7SJames Smart 
2884d613b6a7SJames Smart 		/* Word 8 */
2885d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
2886d613b6a7SJames Smart 
2887d613b6a7SJames Smart 		/* Word 9 */
2888d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
2889d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
2890d613b6a7SJames Smart 
2891bd3061baSJames Smart 		/* Word 10 - in template xc=1 */
2892bd3061baSJames Smart 		if (!xc)
2893bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, 0);
2894d613b6a7SJames Smart 
2895bd3061baSJames Smart 		/* Word 11 - set pbde later */
2896414abe0aSJames Smart 		if (phba->cfg_enable_pbde) {
2897bd3061baSJames Smart 			do_pbde = 1;
2898bd3061baSJames Smart 		} else {
2899bd3061baSJames Smart 			bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 0);
2900bd3061baSJames Smart 			do_pbde = 0;
2901bd3061baSJames Smart 		}
2902d613b6a7SJames Smart 
2903d613b6a7SJames Smart 		/* Word 12 */
2904d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2905d613b6a7SJames Smart 
2906ea85a20cSJames Smart 		/* Setup 2 SKIP SGEs */
2907ea85a20cSJames Smart 		sgl->addr_hi = 0;
2908ea85a20cSJames Smart 		sgl->addr_lo = 0;
2909d613b6a7SJames Smart 		sgl->word2 = 0;
2910ea85a20cSJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2911d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2912ea85a20cSJames Smart 		sgl->sge_len = 0;
2913d613b6a7SJames Smart 		sgl++;
2914d613b6a7SJames Smart 		sgl->addr_hi = 0;
2915d613b6a7SJames Smart 		sgl->addr_lo = 0;
2916d613b6a7SJames Smart 		sgl->word2 = 0;
2917d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2918d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2919d613b6a7SJames Smart 		sgl->sge_len = 0;
2920d613b6a7SJames Smart 		sgl++;
2921d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
2922d613b6a7SJames Smart 		break;
2923d613b6a7SJames Smart 
2924d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
2925bd3061baSJames Smart 		/* From the treceive template, initialize words 4 - 11 */
2926bd3061baSJames Smart 		memcpy(&wqe->words[4],
2927bd3061baSJames Smart 		       &lpfc_trsp_cmd_template.words[4],
2928bd3061baSJames Smart 		       sizeof(uint32_t) * 8);
2929bd3061baSJames Smart 
2930d613b6a7SJames Smart 		/* Words 0 - 2 */
2931d613b6a7SJames Smart 		physaddr = rsp->rspdma;
2932d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2933d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
2934d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
2935d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2936d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
2937d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2938d613b6a7SJames Smart 
2939d613b6a7SJames Smart 		/* Word 3 */
2940d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
2941d613b6a7SJames Smart 
2942d613b6a7SJames Smart 		/* Word 6 */
2943d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
2944d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2945d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
2946d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2947d613b6a7SJames Smart 
2948d613b6a7SJames Smart 		/* Word 7 */
2949d613b6a7SJames Smart 
2950d613b6a7SJames Smart 		/* Word 8 */
2951d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
2952d613b6a7SJames Smart 
2953d613b6a7SJames Smart 		/* Word 9 */
2954d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
2955d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
2956d613b6a7SJames Smart 
2957d613b6a7SJames Smart 		/* Word 10 */
2958bd3061baSJames Smart 		if (xc)
2959bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 1);
2960d613b6a7SJames Smart 
2961d613b6a7SJames Smart 		/* Word 11 */
2962bd3061baSJames Smart 		/* In template wqes=0 irsp=0 irsplen=0 - good response */
2963bd3061baSJames Smart 		if (rsp->rsplen != LPFC_NVMET_SUCCESS_LEN) {
2964bd3061baSJames Smart 			/* Bad response - embed it */
2965d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
2966d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
2967d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
2968d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
2969d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
2970d613b6a7SJames Smart 		}
2971bd3061baSJames Smart 		do_pbde = 0;
2972bd3061baSJames Smart 
2973bd3061baSJames Smart 		/* Word 12 */
2974bd3061baSJames Smart 		wqe->fcp_trsp.rsvd_12_15[0] = 0;
2975d613b6a7SJames Smart 
2976d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
2977e7f40349SJames Smart 		nsegs = 0;
2978d613b6a7SJames Smart 		sgl->word2 = 0;
2979d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
2980d613b6a7SJames Smart 		break;
2981d613b6a7SJames Smart 
2982d613b6a7SJames Smart 	default:
2983d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2984d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
2985d613b6a7SJames Smart 				rsp->op);
2986d613b6a7SJames Smart 		return NULL;
2987d613b6a7SJames Smart 	}
2988d613b6a7SJames Smart 
2989d613b6a7SJames Smart 	nvmewqe->retry = 1;
2990d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2991d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2992d613b6a7SJames Smart 	nvmewqe->context1 = ndlp;
2993d613b6a7SJames Smart 
2994e7f40349SJames Smart 	for_each_sg(rsp->sg, sgel, nsegs, i) {
2995d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2996d613b6a7SJames Smart 		cnt = sg_dma_len(sgel);
2997d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
2998d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
2999d613b6a7SJames Smart 		sgl->word2 = 0;
3000d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
3001d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
3002d613b6a7SJames Smart 		if ((i+1) == rsp->sg_cnt)
3003d613b6a7SJames Smart 			bf_set(lpfc_sli4_sge_last, sgl, 1);
3004d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
3005d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(cnt);
3006414abe0aSJames Smart 		if (i == 0) {
30070bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)&wqe->words[13];
3008414abe0aSJames Smart 			if (do_pbde) {
30090bc2b7c5SJames Smart 				/* Words 13-15  (PBDE) */
30100bc2b7c5SJames Smart 				bde->addrLow = sgl->addr_lo;
30110bc2b7c5SJames Smart 				bde->addrHigh = sgl->addr_hi;
30120bc2b7c5SJames Smart 				bde->tus.f.bdeSize =
30130bc2b7c5SJames Smart 					le32_to_cpu(sgl->sge_len);
30140bc2b7c5SJames Smart 				bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
30150bc2b7c5SJames Smart 				bde->tus.w = cpu_to_le32(bde->tus.w);
3016414abe0aSJames Smart 			} else {
3017414abe0aSJames Smart 				memset(bde, 0, sizeof(struct ulp_bde64));
3018414abe0aSJames Smart 			}
30190bc2b7c5SJames Smart 		}
3020d613b6a7SJames Smart 		sgl++;
3021d613b6a7SJames Smart 		ctxp->offset += cnt;
3022d613b6a7SJames Smart 	}
30237b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DATA;
3024ce1b591cSJames Smart 	ctxp->entry_cnt++;
3025d613b6a7SJames Smart 	return nvmewqe;
3026d613b6a7SJames Smart }
3027d613b6a7SJames Smart 
3028d613b6a7SJames Smart /**
3029d613b6a7SJames Smart  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
3030d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3031d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3032d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
3033d613b6a7SJames Smart  *
3034d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3035d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
3036d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3037d613b6a7SJames Smart  **/
3038d613b6a7SJames Smart static void
3039d613b6a7SJames Smart lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3040d613b6a7SJames Smart 			     struct lpfc_wcqe_complete *wcqe)
3041d613b6a7SJames Smart {
30427cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3043d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3044b27cbd55SBart Van Assche 	uint32_t result;
304519b58d94SJames Smart 	unsigned long flags;
304619b58d94SJames Smart 	bool released = false;
3047d613b6a7SJames Smart 
3048d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
3049d613b6a7SJames Smart 	result = wcqe->parameter;
3050d613b6a7SJames Smart 
3051d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
30527b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABORT_OP)
3053547077a4SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
3054d613b6a7SJames Smart 
3055c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
30567b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DONE;
305786c67379SJames Smart 
305886c67379SJames Smart 	/* Check if we already received a free context call
305986c67379SJames Smart 	 * and we have completed processing an abort situation.
306086c67379SJames Smart 	 */
30617b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_CTX_RLS) &&
30627b7f551bSJames Smart 	    !(ctxp->flag & LPFC_NVME_XBUSY)) {
3063c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
306479d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3065c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
306619b58d94SJames Smart 		released = true;
306786c67379SJames Smart 	}
30687b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
306919b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3070547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
307119b58d94SJames Smart 
3072e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
307379d8c4ceSJames Smart 			"6165 ABORT cmpl: oxid x%x flg x%x (%d) "
307486c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
307586c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
307686c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
307786c67379SJames Smart 			result, wcqe->word3);
307886c67379SJames Smart 
30796c621a22SJames Smart 	cmdwqe->context2 = NULL;
30806c621a22SJames Smart 	cmdwqe->context3 = NULL;
308119b58d94SJames Smart 	/*
308219b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
308319b58d94SJames Smart 	 * will be recycled by transport release call.
308419b58d94SJames Smart 	 */
308519b58d94SJames Smart 	if (released)
30866c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3087d613b6a7SJames Smart 
30886c621a22SJames Smart 	/* This is the iocbq for the abort, not the command */
3089d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
309086c67379SJames Smart 
309186c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
30927b7f551bSJames Smart 	 * For LPFC_NVME_XBUSY, lpfc_sli4_nvmet_xri_aborted
309386c67379SJames Smart 	 * should have been called already.
309486c67379SJames Smart 	 */
3095d613b6a7SJames Smart }
3096d613b6a7SJames Smart 
3097d613b6a7SJames Smart /**
309886c67379SJames Smart  * lpfc_nvmet_unsol_fcp_abort_cmp - Completion handler for ABTS
3099d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3100d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3101d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
3102d613b6a7SJames Smart  *
3103d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3104d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
3105d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3106d613b6a7SJames Smart  **/
3107d613b6a7SJames Smart static void
310886c67379SJames Smart lpfc_nvmet_unsol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3109d613b6a7SJames Smart 			       struct lpfc_wcqe_complete *wcqe)
3110d613b6a7SJames Smart {
31117cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3112d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
311319b58d94SJames Smart 	unsigned long flags;
3114b27cbd55SBart Van Assche 	uint32_t result;
311519b58d94SJames Smart 	bool released = false;
3116d613b6a7SJames Smart 
3117d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
3118d613b6a7SJames Smart 	result = wcqe->parameter;
3119d613b6a7SJames Smart 
312086c67379SJames Smart 	if (!ctxp) {
312186c67379SJames Smart 		/* if context is clear, related io alrady complete */
3122d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
312386c67379SJames Smart 				"6070 ABTS cmpl: WCQE: %08x %08x %08x %08x\n",
312486c67379SJames Smart 				wcqe->word0, wcqe->total_data_placed,
3125d613b6a7SJames Smart 				result, wcqe->word3);
312686c67379SJames Smart 		return;
312786c67379SJames Smart 	}
3128d613b6a7SJames Smart 
312978e1d200SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3130c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
31317b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABORT_OP)
313278e1d200SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
313378e1d200SJames Smart 
3134d613b6a7SJames Smart 	/* Sanity check */
31357b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_ABORT) {
3136372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
313786c67379SJames Smart 				"6112 ABTS Wrong state:%d oxid x%x\n",
3138d613b6a7SJames Smart 				ctxp->state, ctxp->oxid);
3139d613b6a7SJames Smart 	}
314086c67379SJames Smart 
314186c67379SJames Smart 	/* Check if we already received a free context call
314286c67379SJames Smart 	 * and we have completed processing an abort situation.
314386c67379SJames Smart 	 */
31447b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DONE;
31457b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_CTX_RLS) &&
31467b7f551bSJames Smart 	    !(ctxp->flag & LPFC_NVME_XBUSY)) {
3147c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
314879d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3149c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
315019b58d94SJames Smart 		released = true;
315186c67379SJames Smart 	}
31527b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
315319b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3154547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
315519b58d94SJames Smart 
315686c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
315779d8c4ceSJames Smart 			"6316 ABTS cmpl oxid x%x flg x%x (%x) "
315886c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
315986c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
316086c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
316186c67379SJames Smart 			result, wcqe->word3);
31626c621a22SJames Smart 
31636c621a22SJames Smart 	cmdwqe->context2 = NULL;
31646c621a22SJames Smart 	cmdwqe->context3 = NULL;
316519b58d94SJames Smart 	/*
316619b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
316719b58d94SJames Smart 	 * will be recycled by transport release call.
316819b58d94SJames Smart 	 */
316919b58d94SJames Smart 	if (released)
31706c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
317186c67379SJames Smart 
317286c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
31737b7f551bSJames Smart 	 * For LPFC_NVME_XBUSY, lpfc_sli4_nvmet_xri_aborted
317486c67379SJames Smart 	 * should have been called already.
317586c67379SJames Smart 	 */
3176d613b6a7SJames Smart }
3177d613b6a7SJames Smart 
3178d613b6a7SJames Smart /**
3179d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
3180d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3181d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3182d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
3183d613b6a7SJames Smart  *
3184d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3185d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for LS cmds
3186d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3187d613b6a7SJames Smart  **/
3188d613b6a7SJames Smart static void
3189d613b6a7SJames Smart lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3190d613b6a7SJames Smart 			    struct lpfc_wcqe_complete *wcqe)
3191d613b6a7SJames Smart {
31927cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3193d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3194b27cbd55SBart Van Assche 	uint32_t result;
3195d613b6a7SJames Smart 
3196d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
3197d613b6a7SJames Smart 	result = wcqe->parameter;
3198d613b6a7SJames Smart 
3199fcdd14b8SJames Smart 	if (phba->nvmet_support) {
3200d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3201547077a4SJames Smart 		atomic_inc(&tgtp->xmt_ls_abort_cmpl);
3202fcdd14b8SJames Smart 	}
3203d613b6a7SJames Smart 
3204d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
320532350664SJames Smart 			"6083 Abort cmpl: ctx x%px WCQE:%08x %08x %08x %08x\n",
3206d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
3207d613b6a7SJames Smart 			result, wcqe->word3);
3208d613b6a7SJames Smart 
3209ce1b591cSJames Smart 	if (!ctxp) {
3210372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3211ce1b591cSJames Smart 				"6415 NVMET LS Abort No ctx: WCQE: "
3212ce1b591cSJames Smart 				 "%08x %08x %08x %08x\n",
3213ce1b591cSJames Smart 				wcqe->word0, wcqe->total_data_placed,
3214ce1b591cSJames Smart 				result, wcqe->word3);
3215ce1b591cSJames Smart 
3216ce1b591cSJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
3217ce1b591cSJames Smart 		return;
3218ce1b591cSJames Smart 	}
3219ce1b591cSJames Smart 
32207b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_LS_ABORT) {
3221372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3222ce1b591cSJames Smart 				"6416 NVMET LS abort cmpl state mismatch: "
3223ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
3224ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3225ce1b591cSJames Smart 	}
3226ce1b591cSJames Smart 
3227d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
3228d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
3229d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3230d613b6a7SJames Smart 	kfree(ctxp);
3231d613b6a7SJames Smart }
3232d613b6a7SJames Smart 
3233d613b6a7SJames Smart static int
3234d613b6a7SJames Smart lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
32357cacae2aSJames Smart 			     struct lpfc_async_xchg_ctx *ctxp,
3236d613b6a7SJames Smart 			     uint32_t sid, uint16_t xri)
3237d613b6a7SJames Smart {
3238fcdd14b8SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = NULL;
3239d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3240205e8240SJames Smart 	union lpfc_wqe128 *wqe_abts;
3241d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3242d613b6a7SJames Smart 
3243d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
324486c67379SJames Smart 			"6067 ABTS: sid %x xri x%x/x%x\n",
3245318083adSJames Smart 			sid, xri, ctxp->wqeq->sli4_xritag);
3246d613b6a7SJames Smart 
3247fcdd14b8SJames Smart 	if (phba->nvmet_support && phba->targetport)
3248d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3249d613b6a7SJames Smart 
3250d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3251307e3380SJames Smart 	if (!ndlp ||
3252d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3253d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3254fcdd14b8SJames Smart 		if (tgtp)
3255d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_abort_rsp_error);
3256372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3257d613b6a7SJames Smart 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
3258b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3259d613b6a7SJames Smart 
3260d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3261d613b6a7SJames Smart 		return 0;
3262d613b6a7SJames Smart 	}
3263d613b6a7SJames Smart 
3264d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
3265d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
3266d613b6a7SJames Smart 
3267d613b6a7SJames Smart 	/*
3268d613b6a7SJames Smart 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
3269d613b6a7SJames Smart 	 * that were initialized in lpfc_sli4_nvmet_alloc.
3270d613b6a7SJames Smart 	 */
3271d613b6a7SJames Smart 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
3272d613b6a7SJames Smart 
3273d613b6a7SJames Smart 	/* Word 5 */
3274d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
3275d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
3276d613b6a7SJames Smart 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
3277d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
3278d613b6a7SJames Smart 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
3279d613b6a7SJames Smart 
3280d613b6a7SJames Smart 	/* Word 6 */
3281d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
3282d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
3283d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
3284d613b6a7SJames Smart 	       abts_wqeq->sli4_xritag);
3285d613b6a7SJames Smart 
3286d613b6a7SJames Smart 	/* Word 7 */
3287d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
3288d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
3289d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
3290d613b6a7SJames Smart 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
3291d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
3292d613b6a7SJames Smart 
3293d613b6a7SJames Smart 	/* Word 8 */
3294d613b6a7SJames Smart 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
3295d613b6a7SJames Smart 
3296d613b6a7SJames Smart 	/* Word 9 */
3297d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
3298d613b6a7SJames Smart 	/* Needs to be set by caller */
3299d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
3300d613b6a7SJames Smart 
3301d613b6a7SJames Smart 	/* Word 10 */
3302d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe_abts->xmit_sequence.wqe_com, 1);
3303d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
3304d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
3305d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
3306d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
3307d613b6a7SJames Smart 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
3308d613b6a7SJames Smart 
3309d613b6a7SJames Smart 	/* Word 11 */
3310d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
3311d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
3312d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
3313d613b6a7SJames Smart 	       OTHER_COMMAND);
3314d613b6a7SJames Smart 
3315d613b6a7SJames Smart 	abts_wqeq->vport = phba->pport;
3316d613b6a7SJames Smart 	abts_wqeq->context1 = ndlp;
3317d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
3318d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
3319d613b6a7SJames Smart 	abts_wqeq->rsvd2 = 0;
3320d613b6a7SJames Smart 	/* hba_wqidx should already be setup from command we are aborting */
3321d613b6a7SJames Smart 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
3322d613b6a7SJames Smart 	abts_wqeq->iocb.ulpLe = 1;
3323d613b6a7SJames Smart 
3324d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
3325d613b6a7SJames Smart 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
3326d613b6a7SJames Smart 			xri, abts_wqeq->iotag);
3327d613b6a7SJames Smart 	return 1;
3328d613b6a7SJames Smart }
3329d613b6a7SJames Smart 
3330*db7531d2SJames Smart /**
3331*db7531d2SJames Smart  * lpfc_nvmet_prep_abort_wqe - set up 'abort' work queue entry.
3332*db7531d2SJames Smart  * @pwqeq: Pointer to command iocb.
3333*db7531d2SJames Smart  * @xritag: Tag that  uniqely identifies the local exchange resource.
3334*db7531d2SJames Smart  * @opt: Option bits -
3335*db7531d2SJames Smart  *		bit 0 = inhibit sending abts on the link
3336*db7531d2SJames Smart  *
3337*db7531d2SJames Smart  * This function is called with hbalock held.
3338*db7531d2SJames Smart  **/
3339*db7531d2SJames Smart void
3340*db7531d2SJames Smart lpfc_nvmet_prep_abort_wqe(struct lpfc_iocbq *pwqeq, u16 xritag, u8 opt)
3341*db7531d2SJames Smart {
3342*db7531d2SJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
3343*db7531d2SJames Smart 
3344*db7531d2SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
3345*db7531d2SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
3346*db7531d2SJames Smart 	 */
3347*db7531d2SJames Smart 	memset(wqe, 0, sizeof(*wqe));
3348*db7531d2SJames Smart 
3349*db7531d2SJames Smart 	if (opt & INHIBIT_ABORT)
3350*db7531d2SJames Smart 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
3351*db7531d2SJames Smart 	/* Abort specified xri tag, with the mask deliberately zeroed */
3352*db7531d2SJames Smart 	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
3353*db7531d2SJames Smart 
3354*db7531d2SJames Smart 	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
3355*db7531d2SJames Smart 
3356*db7531d2SJames Smart 	/* Abort the I/O associated with this outstanding exchange ID. */
3357*db7531d2SJames Smart 	wqe->abort_cmd.wqe_com.abort_tag = xritag;
3358*db7531d2SJames Smart 
3359*db7531d2SJames Smart 	/* iotag for the wqe completion. */
3360*db7531d2SJames Smart 	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, pwqeq->iotag);
3361*db7531d2SJames Smart 
3362*db7531d2SJames Smart 	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
3363*db7531d2SJames Smart 	bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
3364*db7531d2SJames Smart 
3365*db7531d2SJames Smart 	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);
3366*db7531d2SJames Smart 	bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);
3367*db7531d2SJames Smart 	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
3368*db7531d2SJames Smart }
3369*db7531d2SJames Smart 
3370d613b6a7SJames Smart static int
3371d613b6a7SJames Smart lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
33727cacae2aSJames Smart 			       struct lpfc_async_xchg_ctx *ctxp,
3373d613b6a7SJames Smart 			       uint32_t sid, uint16_t xri)
3374d613b6a7SJames Smart {
3375d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3376d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3377d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3378d613b6a7SJames Smart 	unsigned long flags;
337951f8e43eSJames Smart 	u8 opt;
3380d613b6a7SJames Smart 	int rc;
3381d613b6a7SJames Smart 
3382d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3383d613b6a7SJames Smart 	if (!ctxp->wqeq) {
33846c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3385d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3386d613b6a7SJames Smart 	}
3387d613b6a7SJames Smart 
3388d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3389307e3380SJames Smart 	if (!ndlp ||
3390d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3391d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3392d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3393372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
339486c67379SJames Smart 				"6160 Drop ABORT - wrong NDLP state x%x.\n",
3395b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3396d613b6a7SJames Smart 
3397d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3398c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
33997b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3400c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3401d613b6a7SJames Smart 		return 0;
3402d613b6a7SJames Smart 	}
3403d613b6a7SJames Smart 
3404d613b6a7SJames Smart 	/* Issue ABTS for this WQE based on iotag */
3405d613b6a7SJames Smart 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
3406c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3407d613b6a7SJames Smart 	if (!ctxp->abort_wqeq) {
3408547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3409372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
341086c67379SJames Smart 				"6161 ABORT failed: No wqeqs: "
3411d613b6a7SJames Smart 				"xri: x%x\n", ctxp->oxid);
3412d613b6a7SJames Smart 		/* No failure to an ABTS request. */
34137b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3414c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3415d613b6a7SJames Smart 		return 0;
3416d613b6a7SJames Smart 	}
3417d613b6a7SJames Smart 	abts_wqeq = ctxp->abort_wqeq;
34187b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_ABORT;
34197b7f551bSJames Smart 	opt = (ctxp->flag & LPFC_NVME_ABTS_RCV) ? INHIBIT_ABORT : 0;
3420c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3421d613b6a7SJames Smart 
3422d613b6a7SJames Smart 	/* Announce entry to new IO submit field. */
342386c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
342486c67379SJames Smart 			"6162 ABORT Request to rport DID x%06x "
3425d613b6a7SJames Smart 			"for xri x%x x%x\n",
3426d613b6a7SJames Smart 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
3427d613b6a7SJames Smart 
3428d613b6a7SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
3429d613b6a7SJames Smart 	 * cleared when the reset is complete and rings reestablished.
3430d613b6a7SJames Smart 	 */
3431d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3432d613b6a7SJames Smart 	/* driver queued commands are in process of being flushed */
3433c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
3434d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3435547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3436372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3437d613b6a7SJames Smart 				"6163 Driver in reset cleanup - flushing "
3438d613b6a7SJames Smart 				"NVME Req now. hba_flag x%x oxid x%x\n",
3439d613b6a7SJames Smart 				phba->hba_flag, ctxp->oxid);
3440d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3441c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
34427b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3443c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3444d613b6a7SJames Smart 		return 0;
3445d613b6a7SJames Smart 	}
3446d613b6a7SJames Smart 
3447d613b6a7SJames Smart 	/* Outstanding abort is in progress */
3448d613b6a7SJames Smart 	if (abts_wqeq->iocb_flag & LPFC_DRIVER_ABORTED) {
3449d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3450547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3451372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3452d613b6a7SJames Smart 				"6164 Outstanding NVME I/O Abort Request "
3453d613b6a7SJames Smart 				"still pending on oxid x%x\n",
3454d613b6a7SJames Smart 				ctxp->oxid);
3455d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3456c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
34577b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3458c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3459d613b6a7SJames Smart 		return 0;
3460d613b6a7SJames Smart 	}
3461d613b6a7SJames Smart 
3462d613b6a7SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
3463d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_DRIVER_ABORTED;
3464d613b6a7SJames Smart 
3465*db7531d2SJames Smart 	lpfc_nvmet_prep_abort_wqe(abts_wqeq, ctxp->wqeq->sli4_xritag, opt);
3466d613b6a7SJames Smart 
3467d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
3468d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
3469d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
34705314995eSSaurav Girepunje 	abts_wqeq->iocb_cmpl = NULL;
3471d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVME;
3472d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
34734550f9c7SJames Smart 	abts_wqeq->vport = phba->pport;
34741fbf9742SJames Smart 	if (!ctxp->hdwq)
34751fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
34761fbf9742SJames Smart 
34771fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3478d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3479547077a4SJames Smart 	if (rc == WQE_SUCCESS) {
3480547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_sol);
3481d613b6a7SJames Smart 		return 0;
3482547077a4SJames Smart 	}
3483d613b6a7SJames Smart 
3484547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3485c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
34867b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3487c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3488d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3489372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
349086c67379SJames Smart 			"6166 Failed ABORT issue_wqe with status x%x "
3491d613b6a7SJames Smart 			"for oxid x%x.\n",
3492d613b6a7SJames Smart 			rc, ctxp->oxid);
3493d613b6a7SJames Smart 	return 1;
3494d613b6a7SJames Smart }
3495d613b6a7SJames Smart 
3496d613b6a7SJames Smart static int
3497d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
34987cacae2aSJames Smart 				 struct lpfc_async_xchg_ctx *ctxp,
3499d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
3500d613b6a7SJames Smart {
3501d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3502d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3503d613b6a7SJames Smart 	unsigned long flags;
3504c160c0f8SJames Smart 	bool released = false;
3505d613b6a7SJames Smart 	int rc;
3506d613b6a7SJames Smart 
3507d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3508d613b6a7SJames Smart 	if (!ctxp->wqeq) {
35096c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3510d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3511d613b6a7SJames Smart 	}
3512d613b6a7SJames Smart 
35137b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_FREE) {
3514372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3515ce1b591cSJames Smart 				"6417 NVMET ABORT ctx freed %d %d oxid x%x\n",
3516ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
3517ce1b591cSJames Smart 		rc = WQE_BUSY;
3518ce1b591cSJames Smart 		goto aerr;
3519ce1b591cSJames Smart 	}
35207b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_ABORT;
3521ce1b591cSJames Smart 	ctxp->entry_cnt++;
3522d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
3523d613b6a7SJames Smart 	if (rc == 0)
3524d613b6a7SJames Smart 		goto aerr;
3525d613b6a7SJames Smart 
3526d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3527d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
352886c67379SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_unsol_fcp_abort_cmp;
352986c67379SJames Smart 	abts_wqeq->iocb_cmpl = NULL;
3530d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVMET;
35311fbf9742SJames Smart 	if (!ctxp->hdwq)
35321fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
35331fbf9742SJames Smart 
35341fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3535d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3536d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3537d613b6a7SJames Smart 		return 0;
3538d613b6a7SJames Smart 	}
3539d613b6a7SJames Smart 
3540d613b6a7SJames Smart aerr:
354168c9b55dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
35427b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_CTX_RLS) {
3543c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
354479d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3545c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
3546c160c0f8SJames Smart 		released = true;
3547c160c0f8SJames Smart 	}
35487b7f551bSJames Smart 	ctxp->flag &= ~(LPFC_NVME_ABORT_OP | LPFC_NVME_CTX_RLS);
354968c9b55dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
355068c9b55dSJames Smart 
3551d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3552372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
355379d8c4ceSJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x "
355479d8c4ceSJames Smart 			"(%x)\n",
355579d8c4ceSJames Smart 			ctxp->oxid, rc, released);
3556c160c0f8SJames Smart 	if (released)
355768c9b55dSJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3558d613b6a7SJames Smart 	return 1;
3559d613b6a7SJames Smart }
3560d613b6a7SJames Smart 
35613a8070c5SJames Smart /**
35623a8070c5SJames Smart  * lpfc_nvme_unsol_ls_issue_abort - issue ABTS on an exchange received
35633a8070c5SJames Smart  *        via async frame receive where the frame is not handled.
35643a8070c5SJames Smart  * @phba: pointer to adapter structure
35653a8070c5SJames Smart  * @ctxp: pointer to the asynchronously received received sequence
35663a8070c5SJames Smart  * @sid: address of the remote port to send the ABTS to
35673a8070c5SJames Smart  * @xri: oxid value to for the ABTS (other side's exchange id).
35683a8070c5SJames Smart  **/
35693a8070c5SJames Smart int
35703a8070c5SJames Smart lpfc_nvme_unsol_ls_issue_abort(struct lpfc_hba *phba,
35717cacae2aSJames Smart 				struct lpfc_async_xchg_ctx *ctxp,
3572d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
3573d613b6a7SJames Smart {
3574fcdd14b8SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = NULL;
3575d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3576d613b6a7SJames Smart 	unsigned long flags;
3577d613b6a7SJames Smart 	int rc;
3578d613b6a7SJames Smart 
35797b7f551bSJames Smart 	if ((ctxp->state == LPFC_NVME_STE_LS_RCV && ctxp->entry_cnt == 1) ||
35807b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_LS_RSP && ctxp->entry_cnt == 2)) {
35817b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_LS_ABORT;
3582ce1b591cSJames Smart 		ctxp->entry_cnt++;
3583ce1b591cSJames Smart 	} else {
3584372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3585ce1b591cSJames Smart 				"6418 NVMET LS abort state mismatch "
3586ce1b591cSJames Smart 				"IO x%x: %d %d\n",
3587ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
35887b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_LS_ABORT;
3589ce1b591cSJames Smart 	}
3590ce1b591cSJames Smart 
3591fcdd14b8SJames Smart 	if (phba->nvmet_support && phba->targetport)
3592d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3593fcdd14b8SJames Smart 
3594d613b6a7SJames Smart 	if (!ctxp->wqeq) {
3595d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
3596d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
3597d613b6a7SJames Smart 		if (!ctxp->wqeq) {
3598372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3599d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
3600d613b6a7SJames Smart 					"xri: x%x\n", xri);
3601d613b6a7SJames Smart 			/* No failure to an ABTS request. */
3602d613b6a7SJames Smart 			kfree(ctxp);
3603d613b6a7SJames Smart 			return 0;
3604d613b6a7SJames Smart 		}
3605d613b6a7SJames Smart 	}
3606d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
36076c621a22SJames Smart 
3608ce1b591cSJames Smart 	if (lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri) == 0) {
3609ce1b591cSJames Smart 		rc = WQE_BUSY;
3610ce1b591cSJames Smart 		goto out;
3611ce1b591cSJames Smart 	}
3612d613b6a7SJames Smart 
3613d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3614d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
36155314995eSSaurav Girepunje 	abts_wqeq->iocb_cmpl = NULL;
3616d613b6a7SJames Smart 	abts_wqeq->iocb_flag |=  LPFC_IO_NVME_LS;
36171fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3618d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3619d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3620fcdd14b8SJames Smart 		if (tgtp)
3621547077a4SJames Smart 			atomic_inc(&tgtp->xmt_abort_unsol);
3622d613b6a7SJames Smart 		return 0;
3623d613b6a7SJames Smart 	}
3624ce1b591cSJames Smart out:
3625fcdd14b8SJames Smart 	if (tgtp)
3626d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3627d613b6a7SJames Smart 	abts_wqeq->context2 = NULL;
3628d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
3629d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3630372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3631d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
36324e57e0b9SJames Smart 	return 1;
3633d613b6a7SJames Smart }
36344c2805aaSJames Smart 
36354c2805aaSJames Smart /**
36364c2805aaSJames Smart  * lpfc_nvmet_invalidate_host
36374c2805aaSJames Smart  *
3638d3cb79fcSLee Jones  * @phba: pointer to the driver instance bound to an adapter port.
3639d3cb79fcSLee Jones  * @ndlp: pointer to an lpfc_nodelist type
36404c2805aaSJames Smart  *
36414c2805aaSJames Smart  * This routine upcalls the nvmet transport to invalidate an NVME
36424c2805aaSJames Smart  * host to which this target instance had active connections.
36434c2805aaSJames Smart  */
36444c2805aaSJames Smart void
36454c2805aaSJames Smart lpfc_nvmet_invalidate_host(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
36464c2805aaSJames Smart {
36474c2805aaSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
36484c2805aaSJames Smart 
36494c2805aaSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_NVME_ABTS,
36504c2805aaSJames Smart 			"6203 Invalidating hosthandle x%px\n",
36514c2805aaSJames Smart 			ndlp);
36524c2805aaSJames Smart 
36534c2805aaSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
36544c2805aaSJames Smart 	atomic_set(&tgtp->state, LPFC_NVMET_INV_HOST_ACTIVE);
36554c2805aaSJames Smart 
36564c2805aaSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
36574c2805aaSJames Smart 	/* Need to get the nvmet_fc_target_port pointer here.*/
36584c2805aaSJames Smart 	nvmet_fc_invalidate_host(phba->targetport, ndlp);
36594c2805aaSJames Smart #endif
36604c2805aaSJames Smart }
3661