xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision e733f8a8)
1d613b6a7SJames Smart /*******************************************************************
2d613b6a7SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
367073c69SJames Smart  * Fibre Channel Host Bus Adapters.                                *
4f45775bfSJames Smart  * Copyright (C) 2017-2022 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.
288a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE 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,
296a680a929SJames Smart 			   struct lpfc_iocbq *rspwqe)
297fe1bedecSJames Smart {
298d51cf5bdSJames Smart 	struct lpfc_async_xchg_ctx *axchg = cmdwqe->context_un.axchg;
299a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
300fe1bedecSJames Smart 	struct nvmefc_ls_rsp *ls_rsp = &axchg->ls_rsp;
301fe1bedecSJames Smart 	uint32_t status, result;
302fe1bedecSJames Smart 
303fe1bedecSJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
304fe1bedecSJames Smart 	result = wcqe->parameter;
305fe1bedecSJames Smart 
306fe1bedecSJames Smart 	if (axchg->state != LPFC_NVME_STE_LS_RSP || axchg->entry_cnt != 2) {
307372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
308fe1bedecSJames Smart 				"6410 NVMEx LS cmpl state mismatch IO x%x: "
309fe1bedecSJames Smart 				"%d %d\n",
310fe1bedecSJames Smart 				axchg->oxid, axchg->state, axchg->entry_cnt);
311fe1bedecSJames Smart 	}
312fe1bedecSJames Smart 
313fe1bedecSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS  CMPL: xri x%x stat x%x result x%x\n",
314fe1bedecSJames Smart 			 axchg->oxid, status, result);
315fe1bedecSJames Smart 
316fe1bedecSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
317fe1bedecSJames Smart 			"6038 NVMEx LS rsp cmpl: %d %d oxid x%x\n",
318fe1bedecSJames Smart 			status, result, axchg->oxid);
319fe1bedecSJames Smart 
320d51cf5bdSJames Smart 	lpfc_nlp_put(cmdwqe->ndlp);
321d51cf5bdSJames Smart 	cmdwqe->context_un.axchg = NULL;
322d51cf5bdSJames Smart 	cmdwqe->bpl_dmabuf = NULL;
323fe1bedecSJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
324fe1bedecSJames Smart 	ls_rsp->done(ls_rsp);
325fe1bedecSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
326fe1bedecSJames Smart 			"6200 NVMEx LS rsp cmpl done status %d oxid x%x\n",
327fe1bedecSJames Smart 			status, axchg->oxid);
328fe1bedecSJames Smart 	kfree(axchg);
329fe1bedecSJames Smart }
330fe1bedecSJames Smart 
331fe1bedecSJames Smart /**
332d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
333d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
334d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
335a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
336d613b6a7SJames Smart  *
337d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
338d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME LS commands
339fe1bedecSJames Smart  * The function updates any states and statistics, then calls the
340fe1bedecSJames Smart  * generic completion handler to free resources.
341d613b6a7SJames Smart  **/
342d613b6a7SJames Smart static void
343d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
344a680a929SJames Smart 			  struct lpfc_iocbq *rspwqe)
345d613b6a7SJames Smart {
346d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
347d613b6a7SJames Smart 	uint32_t status, result;
348a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
349d613b6a7SJames Smart 
350d613b6a7SJames Smart 	if (!phba->targetport)
351fe1bedecSJames Smart 		goto finish;
352fe1bedecSJames Smart 
353fe1bedecSJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
354fe1bedecSJames Smart 	result = wcqe->parameter;
355d613b6a7SJames Smart 
356d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3574b056682SJames Smart 	if (tgtp) {
3584b056682SJames Smart 		if (status) {
359d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_error);
3608ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
3614b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_aborted);
3624b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
3634b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_xb_set);
3644b056682SJames Smart 		} else {
365d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
3664b056682SJames Smart 		}
3674b056682SJames Smart 	}
368d613b6a7SJames Smart 
369fe1bedecSJames Smart finish:
370a680a929SJames Smart 	__lpfc_nvme_xmt_ls_rsp_cmp(phba, cmdwqe, rspwqe);
371d613b6a7SJames Smart }
372d613b6a7SJames Smart 
373d613b6a7SJames Smart /**
3746c621a22SJames Smart  * lpfc_nvmet_ctxbuf_post - Repost a NVMET RQ DMA buffer and clean up context
375d613b6a7SJames Smart  * @phba: HBA buffer is associated with
376d3cb79fcSLee Jones  * @ctx_buf: ctx buffer context
377d613b6a7SJames Smart  *
378d613b6a7SJames Smart  * Description: Frees the given DMA buffer in the appropriate way given by
379d613b6a7SJames Smart  * reposting it to its associated RQ so it can be reused.
380d613b6a7SJames Smart  *
381d613b6a7SJames Smart  * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
382d613b6a7SJames Smart  *
383d613b6a7SJames Smart  * Returns: None
384d613b6a7SJames Smart  **/
385d613b6a7SJames Smart void
3866c621a22SJames Smart lpfc_nvmet_ctxbuf_post(struct lpfc_hba *phba, struct lpfc_nvmet_ctxbuf *ctx_buf)
387d613b6a7SJames Smart {
388eeeb51d8SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
3897cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp = ctx_buf->context;
390a8cf5dfeSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
391a8cf5dfeSJames Smart 	struct fc_frame_header *fc_hdr;
392a8cf5dfeSJames Smart 	struct rqb_dmabuf *nvmebuf;
39366d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
394472e146dSJames Smart 	uint32_t size, oxid, sid;
39566d7ce93SDick Kennedy 	int cpu;
3966c621a22SJames Smart 	unsigned long iflag;
39786c67379SJames Smart 
3987b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_FREE) {
399372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
400ce1b591cSJames Smart 				"6411 NVMET free, already free IO x%x: %d %d\n",
401ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
402ce1b591cSJames Smart 	}
403472e146dSJames Smart 
404472e146dSJames Smart 	if (ctxp->rqb_buffer) {
405472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
4064767c58aSJames Smart 		nvmebuf = ctxp->rqb_buffer;
4074767c58aSJames Smart 		/* check if freed in another path whilst acquiring lock */
4084767c58aSJames Smart 		if (nvmebuf) {
409472e146dSJames Smart 			ctxp->rqb_buffer = NULL;
4107b7f551bSJames Smart 			if (ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) {
4117b7f551bSJames Smart 				ctxp->flag &= ~LPFC_NVME_CTX_REUSE_WQ;
412472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4134767c58aSJames Smart 				nvmebuf->hrq->rqbp->rqb_free_buffer(phba,
4144767c58aSJames Smart 								    nvmebuf);
415472e146dSJames Smart 			} else {
416472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4174767c58aSJames Smart 				/* repost */
4184767c58aSJames Smart 				lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
4194767c58aSJames Smart 			}
4204767c58aSJames Smart 		} else {
4214767c58aSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
422472e146dSJames Smart 		}
423472e146dSJames Smart 	}
4247b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_FREE;
4256c621a22SJames Smart 
426a8cf5dfeSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
427a8cf5dfeSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
428a8cf5dfeSJames Smart 		list_remove_head(&phba->sli4_hba.lpfc_nvmet_io_wait_list,
429a8cf5dfeSJames Smart 				 nvmebuf, struct rqb_dmabuf,
430a8cf5dfeSJames Smart 				 hbuf.list);
431a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt--;
432a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
433a8cf5dfeSJames Smart 				       iflag);
434a8cf5dfeSJames Smart 
435a8cf5dfeSJames Smart 		fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
436a8cf5dfeSJames Smart 		oxid = be16_to_cpu(fc_hdr->fh_ox_id);
437a8cf5dfeSJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
438a8cf5dfeSJames Smart 		size = nvmebuf->bytes_recv;
439a8cf5dfeSJames Smart 		sid = sli4_sid_from_fc_hdr(fc_hdr);
440a8cf5dfeSJames Smart 
4417cacae2aSJames Smart 		ctxp = (struct lpfc_async_xchg_ctx *)ctx_buf->context;
442a8cf5dfeSJames Smart 		ctxp->wqeq = NULL;
443a8cf5dfeSJames Smart 		ctxp->offset = 0;
444a8cf5dfeSJames Smart 		ctxp->phba = phba;
445a8cf5dfeSJames Smart 		ctxp->size = size;
446a8cf5dfeSJames Smart 		ctxp->oxid = oxid;
447a8cf5dfeSJames Smart 		ctxp->sid = sid;
4487b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_RCV;
449a8cf5dfeSJames Smart 		ctxp->entry_cnt = 1;
450a8cf5dfeSJames Smart 		ctxp->flag = 0;
451a8cf5dfeSJames Smart 		ctxp->ctxbuf = ctx_buf;
452cbc5de1bSJames Smart 		ctxp->rqb_buffer = (void *)nvmebuf;
453a8cf5dfeSJames Smart 		spin_lock_init(&ctxp->ctxlock);
454a8cf5dfeSJames Smart 
455a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
456d74a89aaSJames Smart 		/* NOTE: isr time stamp is stale when context is re-assigned*/
457d74a89aaSJames Smart 		if (ctxp->ts_isr_cmd) {
458d74a89aaSJames Smart 			ctxp->ts_cmd_nvme = 0;
459a8cf5dfeSJames Smart 			ctxp->ts_nvme_data = 0;
460a8cf5dfeSJames Smart 			ctxp->ts_data_wqput = 0;
461a8cf5dfeSJames Smart 			ctxp->ts_isr_data = 0;
462a8cf5dfeSJames Smart 			ctxp->ts_data_nvme = 0;
463a8cf5dfeSJames Smart 			ctxp->ts_nvme_status = 0;
464a8cf5dfeSJames Smart 			ctxp->ts_status_wqput = 0;
465a8cf5dfeSJames Smart 			ctxp->ts_isr_status = 0;
466a8cf5dfeSJames Smart 			ctxp->ts_status_nvme = 0;
467d613b6a7SJames Smart 		}
468a8cf5dfeSJames Smart #endif
469a8cf5dfeSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_in);
470472e146dSJames Smart 
47179d8c4ceSJames Smart 		/* Indicate that a replacement buffer has been posted */
472472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
4737b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_CTX_REUSE_WQ;
474472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
475a8cf5dfeSJames Smart 
476472e146dSJames Smart 		if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
477a8cf5dfeSJames Smart 			atomic_inc(&tgtp->rcv_fcp_cmd_drop);
478372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
479472e146dSJames Smart 					"6181 Unable to queue deferred work "
480472e146dSJames Smart 					"for oxid x%x. "
481472e146dSJames Smart 					"FCP Drop IO [x%x x%x x%x]\n",
482472e146dSJames Smart 					ctxp->oxid,
483a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_in),
484a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_out),
485a8cf5dfeSJames Smart 					atomic_read(&tgtp->xmt_fcp_release));
486a8cf5dfeSJames Smart 
487472e146dSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
488a8cf5dfeSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
489472e146dSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
490a8cf5dfeSJames Smart 			lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
491472e146dSJames Smart 		}
492a8cf5dfeSJames Smart 		return;
493a8cf5dfeSJames Smart 	}
494a8cf5dfeSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
495a8cf5dfeSJames Smart 
49666d7ce93SDick Kennedy 	/*
49766d7ce93SDick Kennedy 	 * Use the CPU context list, from the MRQ the IO was received on
49866d7ce93SDick Kennedy 	 * (ctxp->idx), to save context structure.
49966d7ce93SDick Kennedy 	 */
50079d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
50179d8c4ceSJames Smart 	list_del_init(&ctxp->list);
50279d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
503d6d189ceSBart Van Assche 	cpu = raw_smp_processor_id();
50466d7ce93SDick Kennedy 	infop = lpfc_get_ctx_list(phba, cpu, ctxp->idx);
50566d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, iflag);
50666d7ce93SDick Kennedy 	list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
50766d7ce93SDick Kennedy 	infop->nvmet_ctx_list_cnt++;
50866d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, iflag);
509eeeb51d8SJames Smart #endif
510d613b6a7SJames Smart }
511d613b6a7SJames Smart 
5122b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5132b65e182SJames Smart static void
5142b65e182SJames Smart lpfc_nvmet_ktime(struct lpfc_hba *phba,
5157cacae2aSJames Smart 		 struct lpfc_async_xchg_ctx *ctxp)
5162b65e182SJames Smart {
5172b65e182SJames Smart 	uint64_t seg1, seg2, seg3, seg4, seg5;
5182b65e182SJames Smart 	uint64_t seg6, seg7, seg8, seg9, seg10;
519c8a4ce0bSDick Kennedy 	uint64_t segsum;
5202b65e182SJames Smart 
5212b65e182SJames Smart 	if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
5222b65e182SJames Smart 	    !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
5232b65e182SJames Smart 	    !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
5242b65e182SJames Smart 	    !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
5252b65e182SJames Smart 	    !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
5262b65e182SJames Smart 		return;
5272b65e182SJames Smart 
528c8a4ce0bSDick Kennedy 	if (ctxp->ts_status_nvme < ctxp->ts_isr_cmd)
529c8a4ce0bSDick Kennedy 		return;
5302b65e182SJames Smart 	if (ctxp->ts_isr_cmd  > ctxp->ts_cmd_nvme)
5312b65e182SJames Smart 		return;
5322b65e182SJames Smart 	if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
5332b65e182SJames Smart 		return;
5342b65e182SJames Smart 	if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
5352b65e182SJames Smart 		return;
5362b65e182SJames Smart 	if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
5372b65e182SJames Smart 		return;
5382b65e182SJames Smart 	if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
5392b65e182SJames Smart 		return;
5402b65e182SJames Smart 	if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
5412b65e182SJames Smart 		return;
5422b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
5432b65e182SJames Smart 		return;
5442b65e182SJames Smart 	if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
5452b65e182SJames Smart 		return;
5462b65e182SJames Smart 	if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
5472b65e182SJames Smart 		return;
5482b65e182SJames Smart 	/*
5492b65e182SJames Smart 	 * Segment 1 - Time from FCP command received by MSI-X ISR
5502b65e182SJames Smart 	 * to FCP command is passed to NVME Layer.
5512b65e182SJames Smart 	 * Segment 2 - Time from FCP command payload handed
5522b65e182SJames Smart 	 * off to NVME Layer to Driver receives a Command op
5532b65e182SJames Smart 	 * from NVME Layer.
5542b65e182SJames Smart 	 * Segment 3 - Time from Driver receives a Command op
5552b65e182SJames Smart 	 * from NVME Layer to Command is put on WQ.
5562b65e182SJames Smart 	 * Segment 4 - Time from Driver WQ put is done
5572b65e182SJames Smart 	 * to MSI-X ISR for Command cmpl.
5582b65e182SJames Smart 	 * Segment 5 - Time from MSI-X ISR for Command cmpl to
5592b65e182SJames Smart 	 * Command cmpl is passed to NVME Layer.
5602b65e182SJames Smart 	 * Segment 6 - Time from Command cmpl is passed to NVME
5612b65e182SJames Smart 	 * Layer to Driver receives a RSP op from NVME Layer.
5622b65e182SJames Smart 	 * Segment 7 - Time from Driver receives a RSP op from
5632b65e182SJames Smart 	 * NVME Layer to WQ put is done on TRSP FCP Status.
5642b65e182SJames Smart 	 * Segment 8 - Time from Driver WQ put is done on TRSP
5652b65e182SJames Smart 	 * FCP Status to MSI-X ISR for TRSP cmpl.
5662b65e182SJames Smart 	 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
5672b65e182SJames Smart 	 * TRSP cmpl is passed to NVME Layer.
5682b65e182SJames Smart 	 * Segment 10 - Time from FCP command received by
5692b65e182SJames Smart 	 * MSI-X ISR to command is completed on wire.
5702b65e182SJames Smart 	 * (Segments 1 thru 8) for READDATA / WRITEDATA
5712b65e182SJames Smart 	 * (Segments 1 thru 4) for READDATA_RSP
5722b65e182SJames Smart 	 */
5732b65e182SJames Smart 	seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
574c8a4ce0bSDick Kennedy 	segsum = seg1;
575c8a4ce0bSDick Kennedy 
576c8a4ce0bSDick Kennedy 	seg2 = ctxp->ts_nvme_data - ctxp->ts_isr_cmd;
577c8a4ce0bSDick Kennedy 	if (segsum > seg2)
578c8a4ce0bSDick Kennedy 		return;
579c8a4ce0bSDick Kennedy 	seg2 -= segsum;
580c8a4ce0bSDick Kennedy 	segsum += seg2;
581c8a4ce0bSDick Kennedy 
582c8a4ce0bSDick Kennedy 	seg3 = ctxp->ts_data_wqput - ctxp->ts_isr_cmd;
583c8a4ce0bSDick Kennedy 	if (segsum > seg3)
584c8a4ce0bSDick Kennedy 		return;
585c8a4ce0bSDick Kennedy 	seg3 -= segsum;
586c8a4ce0bSDick Kennedy 	segsum += seg3;
587c8a4ce0bSDick Kennedy 
588c8a4ce0bSDick Kennedy 	seg4 = ctxp->ts_isr_data - ctxp->ts_isr_cmd;
589c8a4ce0bSDick Kennedy 	if (segsum > seg4)
590c8a4ce0bSDick Kennedy 		return;
591c8a4ce0bSDick Kennedy 	seg4 -= segsum;
592c8a4ce0bSDick Kennedy 	segsum += seg4;
593c8a4ce0bSDick Kennedy 
594c8a4ce0bSDick Kennedy 	seg5 = ctxp->ts_data_nvme - ctxp->ts_isr_cmd;
595c8a4ce0bSDick Kennedy 	if (segsum > seg5)
596c8a4ce0bSDick Kennedy 		return;
597c8a4ce0bSDick Kennedy 	seg5 -= segsum;
598c8a4ce0bSDick Kennedy 	segsum += seg5;
599c8a4ce0bSDick Kennedy 
6002b65e182SJames Smart 
6012b65e182SJames Smart 	/* For auto rsp commands seg6 thru seg10 will be 0 */
6022b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
603c8a4ce0bSDick Kennedy 		seg6 = ctxp->ts_nvme_status - ctxp->ts_isr_cmd;
604c8a4ce0bSDick Kennedy 		if (segsum > seg6)
605c8a4ce0bSDick Kennedy 			return;
606c8a4ce0bSDick Kennedy 		seg6 -= segsum;
607c8a4ce0bSDick Kennedy 		segsum += seg6;
608c8a4ce0bSDick Kennedy 
609c8a4ce0bSDick Kennedy 		seg7 = ctxp->ts_status_wqput - ctxp->ts_isr_cmd;
610c8a4ce0bSDick Kennedy 		if (segsum > seg7)
611c8a4ce0bSDick Kennedy 			return;
612c8a4ce0bSDick Kennedy 		seg7 -= segsum;
613c8a4ce0bSDick Kennedy 		segsum += seg7;
614c8a4ce0bSDick Kennedy 
615c8a4ce0bSDick Kennedy 		seg8 = ctxp->ts_isr_status - ctxp->ts_isr_cmd;
616c8a4ce0bSDick Kennedy 		if (segsum > seg8)
617c8a4ce0bSDick Kennedy 			return;
618c8a4ce0bSDick Kennedy 		seg8 -= segsum;
619c8a4ce0bSDick Kennedy 		segsum += seg8;
620c8a4ce0bSDick Kennedy 
621c8a4ce0bSDick Kennedy 		seg9 = ctxp->ts_status_nvme - ctxp->ts_isr_cmd;
622c8a4ce0bSDick Kennedy 		if (segsum > seg9)
623c8a4ce0bSDick Kennedy 			return;
624c8a4ce0bSDick Kennedy 		seg9 -= segsum;
625c8a4ce0bSDick Kennedy 		segsum += seg9;
626c8a4ce0bSDick Kennedy 
627c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_status < ctxp->ts_isr_cmd)
628c8a4ce0bSDick Kennedy 			return;
6292b65e182SJames Smart 		seg10 = (ctxp->ts_isr_status -
6302b65e182SJames Smart 			ctxp->ts_isr_cmd);
6312b65e182SJames Smart 	} else {
632c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_data < ctxp->ts_isr_cmd)
633c8a4ce0bSDick Kennedy 			return;
6342b65e182SJames Smart 		seg6 =  0;
6352b65e182SJames Smart 		seg7 =  0;
6362b65e182SJames Smart 		seg8 =  0;
6372b65e182SJames Smart 		seg9 =  0;
6382b65e182SJames Smart 		seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
6392b65e182SJames Smart 	}
6402b65e182SJames Smart 
6412b65e182SJames Smart 	phba->ktime_seg1_total += seg1;
6422b65e182SJames Smart 	if (seg1 < phba->ktime_seg1_min)
6432b65e182SJames Smart 		phba->ktime_seg1_min = seg1;
6442b65e182SJames Smart 	else if (seg1 > phba->ktime_seg1_max)
6452b65e182SJames Smart 		phba->ktime_seg1_max = seg1;
6462b65e182SJames Smart 
6472b65e182SJames Smart 	phba->ktime_seg2_total += seg2;
6482b65e182SJames Smart 	if (seg2 < phba->ktime_seg2_min)
6492b65e182SJames Smart 		phba->ktime_seg2_min = seg2;
6502b65e182SJames Smart 	else if (seg2 > phba->ktime_seg2_max)
6512b65e182SJames Smart 		phba->ktime_seg2_max = seg2;
6522b65e182SJames Smart 
6532b65e182SJames Smart 	phba->ktime_seg3_total += seg3;
6542b65e182SJames Smart 	if (seg3 < phba->ktime_seg3_min)
6552b65e182SJames Smart 		phba->ktime_seg3_min = seg3;
6562b65e182SJames Smart 	else if (seg3 > phba->ktime_seg3_max)
6572b65e182SJames Smart 		phba->ktime_seg3_max = seg3;
6582b65e182SJames Smart 
6592b65e182SJames Smart 	phba->ktime_seg4_total += seg4;
6602b65e182SJames Smart 	if (seg4 < phba->ktime_seg4_min)
6612b65e182SJames Smart 		phba->ktime_seg4_min = seg4;
6622b65e182SJames Smart 	else if (seg4 > phba->ktime_seg4_max)
6632b65e182SJames Smart 		phba->ktime_seg4_max = seg4;
6642b65e182SJames Smart 
6652b65e182SJames Smart 	phba->ktime_seg5_total += seg5;
6662b65e182SJames Smart 	if (seg5 < phba->ktime_seg5_min)
6672b65e182SJames Smart 		phba->ktime_seg5_min = seg5;
6682b65e182SJames Smart 	else if (seg5 > phba->ktime_seg5_max)
6692b65e182SJames Smart 		phba->ktime_seg5_max = seg5;
6702b65e182SJames Smart 
6712b65e182SJames Smart 	phba->ktime_data_samples++;
6722b65e182SJames Smart 	if (!seg6)
6732b65e182SJames Smart 		goto out;
6742b65e182SJames Smart 
6752b65e182SJames Smart 	phba->ktime_seg6_total += seg6;
6762b65e182SJames Smart 	if (seg6 < phba->ktime_seg6_min)
6772b65e182SJames Smart 		phba->ktime_seg6_min = seg6;
6782b65e182SJames Smart 	else if (seg6 > phba->ktime_seg6_max)
6792b65e182SJames Smart 		phba->ktime_seg6_max = seg6;
6802b65e182SJames Smart 
6812b65e182SJames Smart 	phba->ktime_seg7_total += seg7;
6822b65e182SJames Smart 	if (seg7 < phba->ktime_seg7_min)
6832b65e182SJames Smart 		phba->ktime_seg7_min = seg7;
6842b65e182SJames Smart 	else if (seg7 > phba->ktime_seg7_max)
6852b65e182SJames Smart 		phba->ktime_seg7_max = seg7;
6862b65e182SJames Smart 
6872b65e182SJames Smart 	phba->ktime_seg8_total += seg8;
6882b65e182SJames Smart 	if (seg8 < phba->ktime_seg8_min)
6892b65e182SJames Smart 		phba->ktime_seg8_min = seg8;
6902b65e182SJames Smart 	else if (seg8 > phba->ktime_seg8_max)
6912b65e182SJames Smart 		phba->ktime_seg8_max = seg8;
6922b65e182SJames Smart 
6932b65e182SJames Smart 	phba->ktime_seg9_total += seg9;
6942b65e182SJames Smart 	if (seg9 < phba->ktime_seg9_min)
6952b65e182SJames Smart 		phba->ktime_seg9_min = seg9;
6962b65e182SJames Smart 	else if (seg9 > phba->ktime_seg9_max)
6972b65e182SJames Smart 		phba->ktime_seg9_max = seg9;
6982b65e182SJames Smart out:
6992b65e182SJames Smart 	phba->ktime_seg10_total += seg10;
7002b65e182SJames Smart 	if (seg10 < phba->ktime_seg10_min)
7012b65e182SJames Smart 		phba->ktime_seg10_min = seg10;
7022b65e182SJames Smart 	else if (seg10 > phba->ktime_seg10_max)
7032b65e182SJames Smart 		phba->ktime_seg10_max = seg10;
7042b65e182SJames Smart 	phba->ktime_status_samples++;
7052b65e182SJames Smart }
7062b65e182SJames Smart #endif
7072b65e182SJames Smart 
708d613b6a7SJames Smart /**
709d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
710d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
711d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
712a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
713d613b6a7SJames Smart  *
714d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
715d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME FCP commands
716d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
717d613b6a7SJames Smart  **/
718d613b6a7SJames Smart static void
719d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
720a680a929SJames Smart 			  struct lpfc_iocbq *rspwqe)
721d613b6a7SJames Smart {
722d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
723d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
7247cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
725*e733f8a8SXiu Jianfeng 	uint32_t status, result, op, logerr;
726a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
7272b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
728840eda96SJames Smart 	int id;
7292b65e182SJames Smart #endif
730d613b6a7SJames Smart 
731d51cf5bdSJames Smart 	ctxp = cmdwqe->context_un.axchg;
7327b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_IO_INP;
73386c67379SJames Smart 
7347cacae2aSJames Smart 	rsp = &ctxp->hdlrctx.fcp_req;
735d613b6a7SJames Smart 	op = rsp->op;
736d613b6a7SJames Smart 
737d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
738d613b6a7SJames Smart 	result = wcqe->parameter;
739d613b6a7SJames Smart 
74086c67379SJames Smart 	if (phba->targetport)
74186c67379SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
74286c67379SJames Smart 	else
74386c67379SJames Smart 		tgtp = NULL;
744d613b6a7SJames Smart 
7452b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
7462b65e182SJames Smart 			 ctxp->oxid, op, status);
7472b65e182SJames Smart 
748d613b6a7SJames Smart 	if (status) {
749d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
750d613b6a7SJames Smart 		rsp->transferred_length = 0;
7514b056682SJames Smart 		if (tgtp) {
752d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_error);
7538ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
7544b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_aborted);
7554b056682SJames Smart 		}
75686c67379SJames Smart 
757e3246a12SJames Smart 		logerr = LOG_NVME_IOERR;
758e3246a12SJames Smart 
75986c67379SJames Smart 		/* pick up SLI4 exhange busy condition */
76086c67379SJames Smart 		if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
7617b7f551bSJames Smart 			ctxp->flag |= LPFC_NVME_XBUSY;
762e3246a12SJames Smart 			logerr |= LOG_NVME_ABTS;
7634b056682SJames Smart 			if (tgtp)
7644b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_xb_set);
76586c67379SJames Smart 
76686c67379SJames Smart 		} else {
7677b7f551bSJames Smart 			ctxp->flag &= ~LPFC_NVME_XBUSY;
76886c67379SJames Smart 		}
76986c67379SJames Smart 
770e3246a12SJames Smart 		lpfc_printf_log(phba, KERN_INFO, logerr,
77179d8c4ceSJames Smart 				"6315 IO Error Cmpl oxid: x%x xri: x%x %x/%x "
77279d8c4ceSJames Smart 				"XBUSY:x%x\n",
77379d8c4ceSJames Smart 				ctxp->oxid, ctxp->ctxbuf->sglq->sli4_xritag,
77479d8c4ceSJames Smart 				status, result, ctxp->flag);
775e3246a12SJames Smart 
776d613b6a7SJames Smart 	} else {
777d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_SUCCESS;
778d613b6a7SJames Smart 		if (op == NVMET_FCOP_RSP)
779d613b6a7SJames Smart 			rsp->transferred_length = rsp->rsplen;
780d613b6a7SJames Smart 		else
781d613b6a7SJames Smart 			rsp->transferred_length = rsp->transfer_length;
78286c67379SJames Smart 		if (tgtp)
783d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
784d613b6a7SJames Smart 	}
785d613b6a7SJames Smart 
786d613b6a7SJames Smart 	if ((op == NVMET_FCOP_READDATA_RSP) ||
787d613b6a7SJames Smart 	    (op == NVMET_FCOP_RSP)) {
788d613b6a7SJames Smart 		/* Sanity check */
7897b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_DONE;
790d613b6a7SJames Smart 		ctxp->entry_cnt++;
79186c67379SJames Smart 
7922b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
793c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
7942b65e182SJames Smart 			if (rsp->op == NVMET_FCOP_READDATA_RSP) {
7952b65e182SJames Smart 				ctxp->ts_isr_data =
7962b65e182SJames Smart 					cmdwqe->isr_timestamp;
7972b65e182SJames Smart 				ctxp->ts_data_nvme =
7982b65e182SJames Smart 					ktime_get_ns();
7992b65e182SJames Smart 				ctxp->ts_nvme_status =
8002b65e182SJames Smart 					ctxp->ts_data_nvme;
8012b65e182SJames Smart 				ctxp->ts_status_wqput =
8022b65e182SJames Smart 					ctxp->ts_data_nvme;
8032b65e182SJames Smart 				ctxp->ts_isr_status =
8042b65e182SJames Smart 					ctxp->ts_data_nvme;
8052b65e182SJames Smart 				ctxp->ts_status_nvme =
8062b65e182SJames Smart 					ctxp->ts_data_nvme;
8072b65e182SJames Smart 			} else {
8082b65e182SJames Smart 				ctxp->ts_isr_status =
8092b65e182SJames Smart 					cmdwqe->isr_timestamp;
8102b65e182SJames Smart 				ctxp->ts_status_nvme =
8112b65e182SJames Smart 					ktime_get_ns();
8122b65e182SJames Smart 			}
8132b65e182SJames Smart 		}
8142b65e182SJames Smart #endif
815d613b6a7SJames Smart 		rsp->done(rsp);
8162b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
817c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme)
8182b65e182SJames Smart 			lpfc_nvmet_ktime(phba, ctxp);
8192b65e182SJames Smart #endif
82019b58d94SJames Smart 		/* lpfc_nvmet_xmt_fcp_release() will recycle the context */
821d613b6a7SJames Smart 	} else {
822d613b6a7SJames Smart 		ctxp->entry_cnt++;
823*e733f8a8SXiu Jianfeng 		memset_startat(cmdwqe, 0, cmd_flag);
8242b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
825c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
8262b65e182SJames Smart 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
8272b65e182SJames Smart 			ctxp->ts_data_nvme = ktime_get_ns();
8282b65e182SJames Smart 		}
8292b65e182SJames Smart #endif
830d613b6a7SJames Smart 		rsp->done(rsp);
831d613b6a7SJames Smart 	}
83263df6d63SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
833840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
834d6d189ceSBart Van Assche 		id = raw_smp_processor_id();
835840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
83663df6d63SJames Smart 		if (ctxp->cpu != id)
83763df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
83863df6d63SJames Smart 					"6704 CPU Check cmdcmpl: "
83963df6d63SJames Smart 					"cpu %d expect %d\n",
84063df6d63SJames Smart 					id, ctxp->cpu);
84163df6d63SJames Smart 	}
84263df6d63SJames Smart #endif
843d613b6a7SJames Smart }
844d613b6a7SJames Smart 
845fe1bedecSJames Smart /**
846fe1bedecSJames Smart  * __lpfc_nvme_xmt_ls_rsp - Generic service routine to issue transmit
847fe1bedecSJames Smart  *         an NVME LS rsp for a prior NVME LS request that was received.
848fe1bedecSJames Smart  * @axchg: pointer to exchange context for the NVME LS request the response
849fe1bedecSJames Smart  *         is for.
850fe1bedecSJames Smart  * @ls_rsp: pointer to the transport LS RSP that is to be sent
851fe1bedecSJames Smart  * @xmt_ls_rsp_cmp: completion routine to call upon RSP transmit done
852fe1bedecSJames Smart  *
853fe1bedecSJames Smart  * This routine is used to format and send a WQE to transmit a NVME LS
854fe1bedecSJames Smart  * Response.  The response is for a prior NVME LS request that was
855fe1bedecSJames Smart  * received and posted to the transport.
856fe1bedecSJames Smart  *
857fe1bedecSJames Smart  * Returns:
858fe1bedecSJames Smart  *  0 : if response successfully transmit
859fe1bedecSJames Smart  *  non-zero : if response failed to transmit, of the form -Exxx.
860fe1bedecSJames Smart  **/
861fe1bedecSJames Smart int
862fe1bedecSJames Smart __lpfc_nvme_xmt_ls_rsp(struct lpfc_async_xchg_ctx *axchg,
863fe1bedecSJames Smart 			struct nvmefc_ls_rsp *ls_rsp,
864fe1bedecSJames Smart 			void (*xmt_ls_rsp_cmp)(struct lpfc_hba *phba,
865fe1bedecSJames Smart 				struct lpfc_iocbq *cmdwqe,
866a680a929SJames Smart 				struct lpfc_iocbq *rspwqe))
867d613b6a7SJames Smart {
868fe1bedecSJames Smart 	struct lpfc_hba *phba = axchg->phba;
869fe1bedecSJames Smart 	struct hbq_dmabuf *nvmebuf = (struct hbq_dmabuf *)axchg->rqb_buffer;
870d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
871d613b6a7SJames Smart 	struct lpfc_dmabuf dmabuf;
872d613b6a7SJames Smart 	struct ulp_bde64 bpl;
873d613b6a7SJames Smart 	int rc;
874d613b6a7SJames Smart 
8751abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
8761abcb371SDick Kennedy 		return -ENODEV;
8771abcb371SDick Kennedy 
878d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
879fe1bedecSJames Smart 			"6023 NVMEx LS rsp oxid x%x\n", axchg->oxid);
880ce1b591cSJames Smart 
881fe1bedecSJames Smart 	if (axchg->state != LPFC_NVME_STE_LS_RCV || axchg->entry_cnt != 1) {
882372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
883fe1bedecSJames Smart 				"6412 NVMEx LS rsp state mismatch "
884ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
885fe1bedecSJames Smart 				axchg->oxid, axchg->state, axchg->entry_cnt);
886fe1bedecSJames Smart 		return -EALREADY;
887ce1b591cSJames Smart 	}
888fe1bedecSJames Smart 	axchg->state = LPFC_NVME_STE_LS_RSP;
889fe1bedecSJames Smart 	axchg->entry_cnt++;
890d613b6a7SJames Smart 
891fe1bedecSJames Smart 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, axchg, ls_rsp->rspdma,
892fe1bedecSJames Smart 					 ls_rsp->rsplen);
893d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
894372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
895fe1bedecSJames Smart 				"6150 NVMEx LS Drop Rsp x%x: Prep\n",
896fe1bedecSJames Smart 				axchg->oxid);
897fe1bedecSJames Smart 		rc = -ENOMEM;
898fe1bedecSJames Smart 		goto out_free_buf;
899d613b6a7SJames Smart 	}
900d613b6a7SJames Smart 
901d613b6a7SJames Smart 	/* Save numBdes for bpl2sgl */
902a680a929SJames Smart 	nvmewqeq->num_bdes = 1;
903d613b6a7SJames Smart 	nvmewqeq->hba_wqidx = 0;
904d51cf5bdSJames Smart 	nvmewqeq->bpl_dmabuf = &dmabuf;
905d613b6a7SJames Smart 	dmabuf.virt = &bpl;
906d613b6a7SJames Smart 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
907d613b6a7SJames Smart 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
908fe1bedecSJames Smart 	bpl.tus.f.bdeSize = ls_rsp->rsplen;
909d613b6a7SJames Smart 	bpl.tus.f.bdeFlags = 0;
910d613b6a7SJames Smart 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
911fe1bedecSJames Smart 	/*
912fe1bedecSJames Smart 	 * Note: although we're using stack space for the dmabuf, the
913fe1bedecSJames Smart 	 * call to lpfc_sli4_issue_wqe is synchronous, so it will not
914fe1bedecSJames Smart 	 * be referenced after it returns back to this routine.
915fe1bedecSJames Smart 	 */
916d613b6a7SJames Smart 
917a680a929SJames Smart 	nvmewqeq->cmd_cmpl = xmt_ls_rsp_cmp;
918d51cf5bdSJames Smart 	nvmewqeq->context_un.axchg = 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 */
926d51cf5bdSJames Smart 	nvmewqeq->bpl_dmabuf = 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 
943d51cf5bdSJames Smart 	lpfc_nlp_put(nvmewqeq->ndlp);
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 
1075a680a929SJames Smart 	nvmewqeq->cmd_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
1076d51cf5bdSJames Smart 	nvmewqeq->context_un.axchg = ctxp;
1077a680a929SJames Smart 	nvmewqeq->cmd_flag |=  LPFC_IO_NVMET;
1078d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
1079d613b6a7SJames Smart 
10802b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
10812b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
10822b65e182SJames Smart 
10837b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_IO_INP;
10841fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
1085d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
10862b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1087c8a4ce0bSDick Kennedy 		if (!ctxp->ts_cmd_nvme)
10882b65e182SJames Smart 			return 0;
10892b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
10902b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
10912b65e182SJames Smart 		else
10922b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
10932b65e182SJames Smart #endif
1094d613b6a7SJames Smart 		return 0;
1095d613b6a7SJames Smart 	}
1096d613b6a7SJames Smart 
10976e8e1c14SJames Smart 	if (rc == -EBUSY) {
10986e8e1c14SJames Smart 		/*
10996e8e1c14SJames Smart 		 * WQ was full, so queue nvmewqeq to be sent after
11006e8e1c14SJames Smart 		 * WQE release CQE
11016e8e1c14SJames Smart 		 */
11027b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_DEFER_WQFULL;
1103c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
11046e8e1c14SJames Smart 		pring = wq->pring;
11056e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
11066e8e1c14SJames Smart 		list_add_tail(&nvmewqeq->list, &wq->wqfull_list);
11076e8e1c14SJames Smart 		wq->q_flag |= HBA_NVMET_WQFULL;
11086e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
1109411de511SJames Smart 		atomic_inc(&lpfc_nvmep->defer_wqfull);
11106e8e1c14SJames Smart 		return 0;
11116e8e1c14SJames Smart 	}
11126e8e1c14SJames Smart 
1113d613b6a7SJames Smart 	/* Give back resources */
1114d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1115372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1116d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
1117d613b6a7SJames Smart 			ctxp->oxid, rc);
1118d613b6a7SJames Smart 
1119d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
1120d51cf5bdSJames Smart 	nvmewqeq->context_un.axchg = NULL;
1121d51cf5bdSJames Smart 	nvmewqeq->bpl_dmabuf = NULL;
1122a5068b46SJames Smart 	rc = -EBUSY;
1123d613b6a7SJames Smart aerr:
1124a5068b46SJames Smart 	return rc;
1125d613b6a7SJames Smart }
1126d613b6a7SJames Smart 
1127d613b6a7SJames Smart static void
1128d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
1129d613b6a7SJames Smart {
1130d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
1131d613b6a7SJames Smart 
1132d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
1133c41f5988SEwan D. Milne 	if (tport->phba->targetport)
1134c41f5988SEwan D. Milne 		complete(tport->tport_unreg_cmp);
1135d613b6a7SJames Smart }
1136d613b6a7SJames Smart 
113719b58d94SJames Smart static void
1138a97ec51bSJames Smart lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
1139a97ec51bSJames Smart 			 struct nvmefc_tgt_fcp_req *req)
1140a97ec51bSJames Smart {
1141a97ec51bSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
11427cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
11437cacae2aSJames Smart 		container_of(req, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
1144a97ec51bSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
11456e8e1c14SJames Smart 	struct lpfc_queue *wq;
114686c67379SJames Smart 	unsigned long flags;
1147a97ec51bSJames Smart 
11481abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
11491abcb371SDick Kennedy 		return;
11501abcb371SDick Kennedy 
11511fbf9742SJames Smart 	if (!ctxp->hdwq)
11521fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[0];
11531fbf9742SJames Smart 
1154a97ec51bSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
115579d8c4ceSJames Smart 			"6103 NVMET Abort op: oxid x%x flg x%x ste %d\n",
1156ce1b591cSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
1157a97ec51bSJames Smart 
1158ce1b591cSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x flg x%x ste x%x\n",
1159ce1b591cSJames Smart 			 ctxp->oxid, ctxp->flag, ctxp->state);
1160a97ec51bSJames Smart 
1161a97ec51bSJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
1162ce1b591cSJames Smart 
116386c67379SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
116486c67379SJames Smart 
116586c67379SJames Smart 	/* Since iaab/iaar are NOT set, we need to check
116686c67379SJames Smart 	 * if the firmware is in process of aborting IO
116786c67379SJames Smart 	 */
11687b7f551bSJames Smart 	if (ctxp->flag & (LPFC_NVME_XBUSY | LPFC_NVME_ABORT_OP)) {
116986c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
117086c67379SJames Smart 		return;
117186c67379SJames Smart 	}
11727b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_ABORT_OP;
1173ce1b591cSJames Smart 
11747b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_DEFER_WQFULL) {
1175c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11766e8e1c14SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
11776e8e1c14SJames Smart 						 ctxp->oxid);
1178c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
11796e8e1c14SJames Smart 		lpfc_nvmet_wqfull_flush(phba, wq, ctxp);
11806e8e1c14SJames Smart 		return;
11816e8e1c14SJames Smart 	}
1182c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11836e8e1c14SJames Smart 
11847b7f551bSJames Smart 	/* A state of LPFC_NVME_STE_RCV means we have just received
1185ce1b591cSJames Smart 	 * the NVME command and have not started processing it.
1186ce1b591cSJames Smart 	 * (by issuing any IO WQEs on this exchange yet)
1187ce1b591cSJames Smart 	 */
11887b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_RCV)
1189ce1b591cSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1190a97ec51bSJames Smart 						 ctxp->oxid);
1191a97ec51bSJames Smart 	else
1192ce1b591cSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1193a97ec51bSJames Smart 					       ctxp->oxid);
1194a97ec51bSJames Smart }
1195a97ec51bSJames Smart 
1196a97ec51bSJames Smart static void
119719b58d94SJames Smart lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
119819b58d94SJames Smart 			   struct nvmefc_tgt_fcp_req *rsp)
119919b58d94SJames Smart {
1200547077a4SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
12017cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
12027cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
120319b58d94SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
120419b58d94SJames Smart 	unsigned long flags;
120519b58d94SJames Smart 	bool aborting = false;
120619b58d94SJames Smart 
1207c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
12087b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_XBUSY)
1209c160c0f8SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1210c160c0f8SJames Smart 				"6027 NVMET release with XBUSY flag x%x"
1211c160c0f8SJames Smart 				" oxid x%x\n",
1212c160c0f8SJames Smart 				ctxp->flag, ctxp->oxid);
12137b7f551bSJames Smart 	else if (ctxp->state != LPFC_NVME_STE_DONE &&
12147b7f551bSJames Smart 		 ctxp->state != LPFC_NVME_STE_ABORT)
1215372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1216ce1b591cSJames Smart 				"6413 NVMET release bad state %d %d oxid x%x\n",
1217ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
1218ce1b591cSJames Smart 
12197b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_ABORT_OP) ||
12207b7f551bSJames Smart 	    (ctxp->flag & LPFC_NVME_XBUSY)) {
122119b58d94SJames Smart 		aborting = true;
122286c67379SJames Smart 		/* let the abort path do the real release */
122386c67379SJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
122419b58d94SJames Smart 	}
122519b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
122619b58d94SJames Smart 
12279094367aSArnd Bergmann 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d abt %d\n", ctxp->oxid,
12289094367aSArnd Bergmann 			 ctxp->state, aborting);
122919b58d94SJames Smart 
1230547077a4SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_release);
12317b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_TNOTIFY;
1232547077a4SJames Smart 
123386c67379SJames Smart 	if (aborting)
123486c67379SJames Smart 		return;
123586c67379SJames Smart 
12366c621a22SJames Smart 	lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
123719b58d94SJames Smart }
123819b58d94SJames Smart 
123950738420SJames Smart static void
124050738420SJames Smart lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port *tgtport,
124150738420SJames Smart 		     struct nvmefc_tgt_fcp_req *rsp)
124250738420SJames Smart {
124350738420SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
12447cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
12457cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
124650738420SJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
124750738420SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
1248472e146dSJames Smart 	unsigned long iflag;
1249472e146dSJames Smart 
125050738420SJames Smart 
125150738420SJames Smart 	lpfc_nvmeio_data(phba, "NVMET DEFERRCV: xri x%x sz %d CPU %02x\n",
1252d6d189ceSBart Van Assche 			 ctxp->oxid, ctxp->size, raw_smp_processor_id());
125350738420SJames Smart 
12546871e814SJames Smart 	if (!nvmebuf) {
12556871e814SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
125679d8c4ceSJames Smart 				"6425 Defer rcv: no buffer oxid x%x: "
12576871e814SJames Smart 				"flg %x ste %x\n",
12586871e814SJames Smart 				ctxp->oxid, ctxp->flag, ctxp->state);
12596871e814SJames Smart 		return;
12606871e814SJames Smart 	}
12616871e814SJames Smart 
126250738420SJames Smart 	tgtp = phba->targetport->private;
12636871e814SJames Smart 	if (tgtp)
126450738420SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_defer);
1265411de511SJames Smart 
1266411de511SJames Smart 	/* Free the nvmebuf since a new buffer already replaced it */
1267cbc5de1bSJames Smart 	nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
1268472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflag);
1269472e146dSJames Smart 	ctxp->rqb_buffer = NULL;
1270472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
127150738420SJames Smart }
127250738420SJames Smart 
127354840bedSJames Smart /**
127454840bedSJames Smart  * lpfc_nvmet_ls_req_cmp - completion handler for a nvme ls request
127554840bedSJames Smart  * @phba: Pointer to HBA context object
127654840bedSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1277a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
127854840bedSJames Smart  *
127954840bedSJames Smart  * This function is the completion handler for NVME LS requests.
128054840bedSJames Smart  * The function updates any states and statistics, then calls the
128154840bedSJames Smart  * generic completion handler to finish completion of the request.
128254840bedSJames Smart  **/
128354840bedSJames Smart static void
128454840bedSJames Smart lpfc_nvmet_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1285a680a929SJames Smart 		      struct lpfc_iocbq *rspwqe)
128654840bedSJames Smart {
1287a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
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;
1370243156c0SJames Smart 	struct lpfc_hba *phba = ndlp->phba;
13714c2805aaSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
13724c2805aaSJames Smart 
13734c2805aaSJames Smart 	if (!phba->targetport || !phba->targetport->private)
13744c2805aaSJames Smart 		return;
13754c2805aaSJames Smart 
13764c2805aaSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1377243156c0SJames Smart 			"6202 NVMET XPT releasing hosthandle x%px "
1378243156c0SJames Smart 			"DID x%x xflags x%x refcnt %d\n",
1379243156c0SJames Smart 			hosthandle, ndlp->nlp_DID, ndlp->fc4_xpt_flags,
1380243156c0SJames Smart 			kref_read(&ndlp->kref));
13814c2805aaSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1382243156c0SJames Smart 	spin_lock_irq(&ndlp->lock);
1383243156c0SJames Smart 	ndlp->fc4_xpt_flags &= ~NLP_XPT_HAS_HH;
1384243156c0SJames Smart 	spin_unlock_irq(&ndlp->lock);
1385243156c0SJames Smart 	lpfc_nlp_put(ndlp);
13864c2805aaSJames Smart 	atomic_set(&tgtp->state, 0);
13874c2805aaSJames Smart }
13884c2805aaSJames Smart 
13894c2805aaSJames Smart static void
1390ab723121SJames Smart lpfc_nvmet_discovery_event(struct nvmet_fc_target_port *tgtport)
1391ab723121SJames Smart {
1392ab723121SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1393ab723121SJames Smart 	struct lpfc_hba *phba;
1394ab723121SJames Smart 	uint32_t rc;
1395ab723121SJames Smart 
1396ab723121SJames Smart 	tgtp = tgtport->private;
1397ab723121SJames Smart 	phba = tgtp->phba;
1398ab723121SJames Smart 
1399ab723121SJames Smart 	rc = lpfc_issue_els_rscn(phba->pport, 0);
1400372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1401ab723121SJames Smart 			"6420 NVMET subsystem change: Notification %s\n",
1402ab723121SJames Smart 			(rc) ? "Failed" : "Sent");
1403ab723121SJames Smart }
1404ab723121SJames Smart 
1405d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
1406d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
1407d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
1408d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
1409a97ec51bSJames Smart 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
141019b58d94SJames Smart 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
141150738420SJames Smart 	.defer_rcv	= lpfc_nvmet_defer_rcv,
1412ab723121SJames Smart 	.discovery_event = lpfc_nvmet_discovery_event,
141354840bedSJames Smart 	.ls_req         = lpfc_nvmet_ls_req,
141454840bedSJames Smart 	.ls_abort       = lpfc_nvmet_ls_abort,
14154c2805aaSJames Smart 	.host_release   = lpfc_nvmet_host_release,
1416d613b6a7SJames Smart 
1417d613b6a7SJames Smart 	.max_hw_queues  = 1,
1418d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1419d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1420d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
1421d613b6a7SJames Smart 
1422d613b6a7SJames Smart 	/* optional features */
1423d613b6a7SJames Smart 	.target_features = 0,
1424d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
1425d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
142654840bedSJames Smart 	.lsrqst_priv_sz = 0,
1427d613b6a7SJames Smart };
1428d613b6a7SJames Smart 
142939aa23f9SColin Ian King static void
143066d7ce93SDick Kennedy __lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba *phba,
143166d7ce93SDick Kennedy 		struct lpfc_nvmet_ctx_info *infop)
14326c621a22SJames Smart {
14336c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf, *next_ctx_buf;
14346c621a22SJames Smart 	unsigned long flags;
14356c621a22SJames Smart 
143666d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, flags);
1437966bb5b7SJames Smart 	list_for_each_entry_safe(ctx_buf, next_ctx_buf,
143866d7ce93SDick Kennedy 				&infop->nvmet_ctx_list, list) {
14395e5b511dSJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
14406c621a22SJames Smart 		list_del_init(&ctx_buf->list);
14415e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
144266d7ce93SDick Kennedy 
1443a789241eSJames Smart 		spin_lock(&phba->hbalock);
144466d7ce93SDick Kennedy 		__lpfc_clear_active_sglq(phba, ctx_buf->sglq->sli4_lxritag);
1445a789241eSJames Smart 		spin_unlock(&phba->hbalock);
1446a789241eSJames Smart 
14476c621a22SJames Smart 		ctx_buf->sglq->state = SGL_FREED;
14486c621a22SJames Smart 		ctx_buf->sglq->ndlp = NULL;
14496c621a22SJames Smart 
1450c4031db7SDan Carpenter 		spin_lock(&phba->sli4_hba.sgl_list_lock);
14516c621a22SJames Smart 		list_add_tail(&ctx_buf->sglq->list,
14526c621a22SJames Smart 				&phba->sli4_hba.lpfc_nvmet_sgl_list);
1453c4031db7SDan Carpenter 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
14546c621a22SJames Smart 
14556c621a22SJames Smart 		lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
14566c621a22SJames Smart 		kfree(ctx_buf->context);
14576c621a22SJames Smart 	}
145866d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, flags);
1459966bb5b7SJames Smart }
146066d7ce93SDick Kennedy 
146166d7ce93SDick Kennedy static void
146266d7ce93SDick Kennedy lpfc_nvmet_cleanup_io_context(struct lpfc_hba *phba)
146366d7ce93SDick Kennedy {
146466d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
146566d7ce93SDick Kennedy 	int i, j;
146666d7ce93SDick Kennedy 
146766d7ce93SDick Kennedy 	/* The first context list, MRQ 0 CPU 0 */
146866d7ce93SDick Kennedy 	infop = phba->sli4_hba.nvmet_ctx_info;
146966d7ce93SDick Kennedy 	if (!infop)
147066d7ce93SDick Kennedy 		return;
147166d7ce93SDick Kennedy 
147266d7ce93SDick Kennedy 	/* Cycle the the entire CPU context list for every MRQ */
147366d7ce93SDick Kennedy 	for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
1474222e9239SJames Smart 		for_each_present_cpu(j) {
1475222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, j, i);
147666d7ce93SDick Kennedy 			__lpfc_nvmet_clean_io_for_cpu(phba, infop);
147766d7ce93SDick Kennedy 		}
147866d7ce93SDick Kennedy 	}
147966d7ce93SDick Kennedy 	kfree(phba->sli4_hba.nvmet_ctx_info);
148066d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = NULL;
14816c621a22SJames Smart }
14826c621a22SJames Smart 
148339aa23f9SColin Ian King static int
14846c621a22SJames Smart lpfc_nvmet_setup_io_context(struct lpfc_hba *phba)
14856c621a22SJames Smart {
14866c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
14876c621a22SJames Smart 	struct lpfc_iocbq *nvmewqe;
14886c621a22SJames Smart 	union lpfc_wqe128 *wqe;
148966d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *last_infop;
149066d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
1491222e9239SJames Smart 	int i, j, idx, cpu;
14926c621a22SJames Smart 
14936c621a22SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
14946c621a22SJames Smart 			"6403 Allocate NVMET resources for %d XRIs\n",
14956c621a22SJames Smart 			phba->sli4_hba.nvmet_xri_cnt);
14966c621a22SJames Smart 
149766d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = kcalloc(
1498222e9239SJames Smart 		phba->sli4_hba.num_possible_cpu * phba->cfg_nvmet_mrq,
149966d7ce93SDick Kennedy 		sizeof(struct lpfc_nvmet_ctx_info), GFP_KERNEL);
150066d7ce93SDick Kennedy 	if (!phba->sli4_hba.nvmet_ctx_info) {
1501372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
150266d7ce93SDick Kennedy 				"6419 Failed allocate memory for "
150366d7ce93SDick Kennedy 				"nvmet context lists\n");
150466d7ce93SDick Kennedy 		return -ENOMEM;
150566d7ce93SDick Kennedy 	}
150666d7ce93SDick Kennedy 
150766d7ce93SDick Kennedy 	/*
150866d7ce93SDick Kennedy 	 * Assuming X CPUs in the system, and Y MRQs, allocate some
150966d7ce93SDick Kennedy 	 * lpfc_nvmet_ctx_info structures as follows:
151066d7ce93SDick Kennedy 	 *
151166d7ce93SDick Kennedy 	 * cpu0/mrq0 cpu1/mrq0 ... cpuX/mrq0
151266d7ce93SDick Kennedy 	 * cpu0/mrq1 cpu1/mrq1 ... cpuX/mrq1
151366d7ce93SDick Kennedy 	 * ...
151466d7ce93SDick Kennedy 	 * cpuX/mrqY cpuX/mrqY ... cpuX/mrqY
151566d7ce93SDick Kennedy 	 *
151666d7ce93SDick Kennedy 	 * Each line represents a MRQ "silo" containing an entry for
151766d7ce93SDick Kennedy 	 * every CPU.
151866d7ce93SDick Kennedy 	 *
151966d7ce93SDick Kennedy 	 * MRQ X is initially assumed to be associated with CPU X, thus
152066d7ce93SDick Kennedy 	 * contexts are initially distributed across all MRQs using
152166d7ce93SDick Kennedy 	 * the MRQ index (N) as follows cpuN/mrqN. When contexts are
152266d7ce93SDick Kennedy 	 * freed, the are freed to the MRQ silo based on the CPU number
152366d7ce93SDick Kennedy 	 * of the IO completion. Thus a context that was allocated for MRQ A
152466d7ce93SDick Kennedy 	 * whose IO completed on CPU B will be freed to cpuB/mrqA.
152566d7ce93SDick Kennedy 	 */
1526222e9239SJames Smart 	for_each_possible_cpu(i) {
152766d7ce93SDick Kennedy 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1528222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
152966d7ce93SDick Kennedy 			INIT_LIST_HEAD(&infop->nvmet_ctx_list);
153066d7ce93SDick Kennedy 			spin_lock_init(&infop->nvmet_ctx_list_lock);
153166d7ce93SDick Kennedy 			infop->nvmet_ctx_list_cnt = 0;
153266d7ce93SDick Kennedy 		}
153366d7ce93SDick Kennedy 	}
153466d7ce93SDick Kennedy 
153566d7ce93SDick Kennedy 	/*
153666d7ce93SDick Kennedy 	 * Setup the next CPU context info ptr for each MRQ.
153766d7ce93SDick Kennedy 	 * MRQ 0 will cycle thru CPUs 0 - X separately from
153866d7ce93SDick Kennedy 	 * MRQ 1 cycling thru CPUs 0 - X, and so on.
153966d7ce93SDick Kennedy 	 */
154066d7ce93SDick Kennedy 	for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1541222e9239SJames Smart 		last_infop = lpfc_get_ctx_list(phba,
1542222e9239SJames Smart 					       cpumask_first(cpu_present_mask),
1543222e9239SJames Smart 					       j);
1544222e9239SJames Smart 		for (i = phba->sli4_hba.num_possible_cpu - 1;  i >= 0; i--) {
154566d7ce93SDick Kennedy 			infop = lpfc_get_ctx_list(phba, i, j);
154666d7ce93SDick Kennedy 			infop->nvmet_ctx_next_cpu = last_infop;
154766d7ce93SDick Kennedy 			last_infop = infop;
154866d7ce93SDick Kennedy 		}
154966d7ce93SDick Kennedy 	}
155066d7ce93SDick Kennedy 
15516c621a22SJames Smart 	/* For all nvmet xris, allocate resources needed to process a
15526c621a22SJames Smart 	 * received command on a per xri basis.
15536c621a22SJames Smart 	 */
155466d7ce93SDick Kennedy 	idx = 0;
1555222e9239SJames Smart 	cpu = cpumask_first(cpu_present_mask);
15566c621a22SJames Smart 	for (i = 0; i < phba->sli4_hba.nvmet_xri_cnt; i++) {
15576c621a22SJames Smart 		ctx_buf = kzalloc(sizeof(*ctx_buf), GFP_KERNEL);
15586c621a22SJames Smart 		if (!ctx_buf) {
1559372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15606c621a22SJames Smart 					"6404 Ran out of memory for NVMET\n");
15616c621a22SJames Smart 			return -ENOMEM;
15626c621a22SJames Smart 		}
15636c621a22SJames Smart 
15646c621a22SJames Smart 		ctx_buf->context = kzalloc(sizeof(*ctx_buf->context),
15656c621a22SJames Smart 					   GFP_KERNEL);
15666c621a22SJames Smart 		if (!ctx_buf->context) {
15676c621a22SJames Smart 			kfree(ctx_buf);
1568372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15696c621a22SJames Smart 					"6405 Ran out of NVMET "
15706c621a22SJames Smart 					"context memory\n");
15716c621a22SJames Smart 			return -ENOMEM;
15726c621a22SJames Smart 		}
15736c621a22SJames Smart 		ctx_buf->context->ctxbuf = ctx_buf;
15747b7f551bSJames Smart 		ctx_buf->context->state = LPFC_NVME_STE_FREE;
15756c621a22SJames Smart 
15766c621a22SJames Smart 		ctx_buf->iocbq = lpfc_sli_get_iocbq(phba);
15776c621a22SJames Smart 		if (!ctx_buf->iocbq) {
15786c621a22SJames Smart 			kfree(ctx_buf->context);
15796c621a22SJames Smart 			kfree(ctx_buf);
1580372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15816c621a22SJames Smart 					"6406 Ran out of NVMET iocb/WQEs\n");
15826c621a22SJames Smart 			return -ENOMEM;
15836c621a22SJames Smart 		}
1584a680a929SJames Smart 		ctx_buf->iocbq->cmd_flag = LPFC_IO_NVMET;
15856c621a22SJames Smart 		nvmewqe = ctx_buf->iocbq;
1586205e8240SJames Smart 		wqe = &nvmewqe->wqe;
1587bd3061baSJames Smart 
15886c621a22SJames Smart 		/* Initialize WQE */
15896c621a22SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
15906c621a22SJames Smart 
1591d51cf5bdSJames Smart 		ctx_buf->iocbq->cmd_dmabuf = NULL;
15926c621a22SJames Smart 		spin_lock(&phba->sli4_hba.sgl_list_lock);
15936c621a22SJames Smart 		ctx_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, ctx_buf->iocbq);
15946c621a22SJames Smart 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
15956c621a22SJames Smart 		if (!ctx_buf->sglq) {
15966c621a22SJames Smart 			lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
15976c621a22SJames Smart 			kfree(ctx_buf->context);
15986c621a22SJames Smart 			kfree(ctx_buf);
1599372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16006c621a22SJames Smart 					"6407 Ran out of NVMET XRIs\n");
16016c621a22SJames Smart 			return -ENOMEM;
16026c621a22SJames Smart 		}
1603472e146dSJames Smart 		INIT_WORK(&ctx_buf->defer_work, lpfc_nvmet_fcp_rqst_defer_work);
160466d7ce93SDick Kennedy 
160566d7ce93SDick Kennedy 		/*
160666d7ce93SDick Kennedy 		 * Add ctx to MRQidx context list. Our initial assumption
160766d7ce93SDick Kennedy 		 * is MRQidx will be associated with CPUidx. This association
160866d7ce93SDick Kennedy 		 * can change on the fly.
160966d7ce93SDick Kennedy 		 */
1610222e9239SJames Smart 		infop = lpfc_get_ctx_list(phba, cpu, idx);
161166d7ce93SDick Kennedy 		spin_lock(&infop->nvmet_ctx_list_lock);
161266d7ce93SDick Kennedy 		list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
161366d7ce93SDick Kennedy 		infop->nvmet_ctx_list_cnt++;
161466d7ce93SDick Kennedy 		spin_unlock(&infop->nvmet_ctx_list_lock);
161566d7ce93SDick Kennedy 
161666d7ce93SDick Kennedy 		/* Spread ctx structures evenly across all MRQs */
161766d7ce93SDick Kennedy 		idx++;
1618222e9239SJames Smart 		if (idx >= phba->cfg_nvmet_mrq) {
161966d7ce93SDick Kennedy 			idx = 0;
1620222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1621222e9239SJames Smart 			continue;
1622222e9239SJames Smart 		}
1623222e9239SJames Smart 		cpu = cpumask_next(cpu, cpu_present_mask);
1624222e9239SJames Smart 		if (cpu == nr_cpu_ids)
1625222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1626222e9239SJames Smart 
16276c621a22SJames Smart 	}
162866d7ce93SDick Kennedy 
1629222e9239SJames Smart 	for_each_present_cpu(i) {
16302879265fSJames Smart 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
16312879265fSJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
163266d7ce93SDick Kennedy 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
163366d7ce93SDick Kennedy 					"6408 TOTAL NVMET ctx for CPU %d "
163432350664SJames Smart 					"MRQ %d: cnt %d nextcpu x%px\n",
163566d7ce93SDick Kennedy 					i, j, infop->nvmet_ctx_list_cnt,
163666d7ce93SDick Kennedy 					infop->nvmet_ctx_next_cpu);
163766d7ce93SDick Kennedy 		}
163866d7ce93SDick Kennedy 	}
16396c621a22SJames Smart 	return 0;
16406c621a22SJames Smart }
16416c621a22SJames Smart 
1642d613b6a7SJames Smart int
1643d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1644d613b6a7SJames Smart {
1645d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1646d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1647d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
16486c621a22SJames Smart 	int error;
1649d613b6a7SJames Smart 
1650d613b6a7SJames Smart 	if (phba->targetport)
1651d613b6a7SJames Smart 		return 0;
1652d613b6a7SJames Smart 
16536c621a22SJames Smart 	error = lpfc_nvmet_setup_io_context(phba);
16546c621a22SJames Smart 	if (error)
16556c621a22SJames Smart 		return error;
16566c621a22SJames Smart 
1657d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1658d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1659d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1660d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
1661d613b6a7SJames Smart 
16625b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
16635b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
16645b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
16654d4c4a4aSJames Smart 	 */
16664d4c4a4aSJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1667cdb42becSJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_hdw_queue;
1668f485c18dSDick Kennedy 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP;
1669d613b6a7SJames Smart 
16707d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1671d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1672d613b6a7SJames Smart 					     &phba->pcidev->dev,
1673d613b6a7SJames Smart 					     &phba->targetport);
1674166d7211SJames Smart #else
16756c621a22SJames Smart 	error = -ENOENT;
1676166d7211SJames Smart #endif
1677d613b6a7SJames Smart 	if (error) {
1678372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1679e7981a2cSDick Kennedy 				"6025 Cannot register NVME targetport x%x: "
1680e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1681e7981a2cSDick Kennedy 				error,
1682e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1683e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1684e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1685d613b6a7SJames Smart 		phba->targetport = NULL;
1686e7981a2cSDick Kennedy 		phba->nvmet_support = 0;
16876c621a22SJames Smart 
16886c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
16896c621a22SJames Smart 
1690d613b6a7SJames Smart 	} else {
1691d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
1692d613b6a7SJames Smart 			phba->targetport->private;
1693d613b6a7SJames Smart 		tgtp->phba = phba;
1694d613b6a7SJames Smart 
1695d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1696d613b6a7SJames Smart 				"6026 Registered NVME "
169732350664SJames Smart 				"targetport: x%px, private x%px "
1698e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1699d613b6a7SJames Smart 				phba->targetport, tgtp,
1700e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1701e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1702e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1703d613b6a7SJames Smart 
1704d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
1705d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
1706d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
1707d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
1708547077a4SJames Smart 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1709d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
1710d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
1711d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
17124b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_xb_set, 0);
17134b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_aborted, 0);
1714d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1715d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1716d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1717d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1718d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
1719d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1720d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
1721d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
1722d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
1723547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_release, 0);
1724d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1725d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
17264b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_xb_set, 0);
17274b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_aborted, 0);
1728d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
17294b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_xri_abort_cqe, 0);
1730547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
1731547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1732547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_unsol, 0);
1733547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_sol, 0);
1734d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
1735d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1736411de511SJames Smart 		atomic_set(&tgtp->defer_ctx, 0);
1737411de511SJames Smart 		atomic_set(&tgtp->defer_fod, 0);
1738411de511SJames Smart 		atomic_set(&tgtp->defer_wqfull, 0);
1739d613b6a7SJames Smart 	}
1740d613b6a7SJames Smart 	return error;
1741d613b6a7SJames Smart }
1742d613b6a7SJames Smart 
1743d613b6a7SJames Smart int
1744d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1745d613b6a7SJames Smart {
1746d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1747d613b6a7SJames Smart 
1748d613b6a7SJames Smart 	if (!phba->targetport)
1749d613b6a7SJames Smart 		return 0;
1750d613b6a7SJames Smart 
1751d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
175232350664SJames Smart 			 "6007 Update NVMET port x%px did x%x\n",
1753d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
1754d613b6a7SJames Smart 
1755d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
1756d613b6a7SJames Smart 	return 0;
1757d613b6a7SJames Smart }
1758d613b6a7SJames Smart 
1759318083adSJames Smart /**
1760318083adSJames Smart  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1761318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
1762318083adSJames Smart  * @axri: pointer to the nvmet xri abort wcqe structure.
1763318083adSJames Smart  *
1764318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
1765318083adSJames Smart  * NVMET aborted xri.
1766318083adSJames Smart  **/
1767318083adSJames Smart void
1768318083adSJames Smart lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
1769318083adSJames Smart 			    struct sli4_wcqe_xri_aborted *axri)
1770318083adSJames Smart {
177132b93865SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
177286c67379SJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
177386c67379SJames Smart 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
17747cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp, *next_ctxp;
17754b056682SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
177679d8c4ceSJames Smart 	struct nvmefc_tgt_fcp_req *req = NULL;
177786c67379SJames Smart 	struct lpfc_nodelist *ndlp;
177886c67379SJames Smart 	unsigned long iflag = 0;
177986c67379SJames Smart 	int rrq_empty = 0;
178086c67379SJames Smart 	bool released = false;
178186c67379SJames Smart 
178286c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
178386c67379SJames Smart 			"6317 XB aborted xri x%x rxid x%x\n", xri, rxid);
178486c67379SJames Smart 
178586c67379SJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
178686c67379SJames Smart 		return;
17874b056682SJames Smart 
17884b056682SJames Smart 	if (phba->targetport) {
17894b056682SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
17904b056682SJames Smart 		atomic_inc(&tgtp->xmt_fcp_xri_abort_cqe);
17914b056682SJames Smart 	}
17924b056682SJames Smart 
1793a789241eSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
179486c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
179586c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
179686c67379SJames Smart 				 list) {
17976c621a22SJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
179886c67379SJames Smart 			continue;
179986c67379SJames Smart 
18007740b615SJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock,
18017740b615SJames Smart 				       iflag);
18027740b615SJames Smart 
18037740b615SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
180486c67379SJames Smart 		/* Check if we already received a free context call
180586c67379SJames Smart 		 * and we have completed processing an abort situation.
180686c67379SJames Smart 		 */
18077b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_CTX_RLS &&
18087b7f551bSJames Smart 		    !(ctxp->flag & LPFC_NVME_ABORT_OP)) {
18097740b615SJames Smart 			spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
181079d8c4ceSJames Smart 			list_del_init(&ctxp->list);
18117740b615SJames Smart 			spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
181286c67379SJames Smart 			released = true;
181386c67379SJames Smart 		}
18147b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_XBUSY;
18157740b615SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
181686c67379SJames Smart 
181786c67379SJames Smart 		rrq_empty = list_empty(&phba->active_rrq_list);
181886c67379SJames Smart 		ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1819307e3380SJames Smart 		if (ndlp &&
182086c67379SJames Smart 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
182186c67379SJames Smart 		     ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
182286c67379SJames Smart 			lpfc_set_rrq_active(phba, ndlp,
18236c621a22SJames Smart 				ctxp->ctxbuf->sglq->sli4_lxritag,
182486c67379SJames Smart 				rxid, 1);
182586c67379SJames Smart 			lpfc_sli4_abts_err_handler(phba, ndlp, axri);
182686c67379SJames Smart 		}
182786c67379SJames Smart 
182886c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
182979d8c4ceSJames Smart 				"6318 XB aborted oxid x%x flg x%x (%x)\n",
183086c67379SJames Smart 				ctxp->oxid, ctxp->flag, released);
183186c67379SJames Smart 		if (released)
18326c621a22SJames Smart 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
18336c621a22SJames Smart 
183486c67379SJames Smart 		if (rrq_empty)
183586c67379SJames Smart 			lpfc_worker_wake_up(phba);
183686c67379SJames Smart 		return;
183786c67379SJames Smart 	}
1838a789241eSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
183979d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_xri(phba, xri);
184079d8c4ceSJames Smart 	if (ctxp) {
184179d8c4ceSJames Smart 		/*
184279d8c4ceSJames Smart 		 *  Abort already done by FW, so BA_ACC sent.
184379d8c4ceSJames Smart 		 *  However, the transport may be unaware.
184479d8c4ceSJames Smart 		 */
184579d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
184679d8c4ceSJames Smart 				"6323 NVMET Rcv ABTS xri x%x ctxp state x%x "
184779d8c4ceSJames Smart 				"flag x%x oxid x%x rxid x%x\n",
184879d8c4ceSJames Smart 				xri, ctxp->state, ctxp->flag, ctxp->oxid,
184979d8c4ceSJames Smart 				rxid);
185079d8c4ceSJames Smart 
185179d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
18527b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_ABTS_RCV;
18537b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_ABORT;
185479d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
185579d8c4ceSJames Smart 
185679d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
185779d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
185801d53c04SJames Smart 				 xri, raw_smp_processor_id(), 0);
185979d8c4ceSJames Smart 
18607cacae2aSJames Smart 		req = &ctxp->hdlrctx.fcp_req;
186179d8c4ceSJames Smart 		if (req)
186279d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport, req);
186379d8c4ceSJames Smart 	}
186432b93865SJames Smart #endif
186586c67379SJames Smart }
186686c67379SJames Smart 
186786c67379SJames Smart int
186886c67379SJames Smart lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport *vport,
186986c67379SJames Smart 			   struct fc_frame_header *fc_hdr)
187086c67379SJames Smart {
187186c67379SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
187286c67379SJames Smart 	struct lpfc_hba *phba = vport->phba;
18737cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp, *next_ctxp;
187486c67379SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
187532b93865SJames Smart 	uint32_t sid;
187679d8c4ceSJames Smart 	uint16_t oxid, xri;
187786c67379SJames Smart 	unsigned long iflag = 0;
187886c67379SJames Smart 
187932b93865SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
188079d8c4ceSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
188186c67379SJames Smart 
1882a789241eSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
188386c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
188486c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
188586c67379SJames Smart 				 list) {
188679d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
188786c67379SJames Smart 			continue;
188886c67379SJames Smart 
188932b93865SJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
189032b93865SJames Smart 
1891a789241eSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock,
1892a789241eSJames Smart 				       iflag);
189386c67379SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
18947b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_ABTS_RCV;
189586c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
189686c67379SJames Smart 
189786c67379SJames Smart 		lpfc_nvmeio_data(phba,
189886c67379SJames Smart 			"NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1899d6d189ceSBart Van Assche 			xri, raw_smp_processor_id(), 0);
190086c67379SJames Smart 
190186c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
190286c67379SJames Smart 				"6319 NVMET Rcv ABTS:acc xri x%x\n", xri);
190386c67379SJames Smart 
19047cacae2aSJames Smart 		rsp = &ctxp->hdlrctx.fcp_req;
190586c67379SJames Smart 		nvmet_fc_rcv_fcp_abort(phba->targetport, rsp);
190686c67379SJames Smart 
190786c67379SJames Smart 		/* Respond with BA_ACC accordingly */
190886c67379SJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
190986c67379SJames Smart 		return 0;
191086c67379SJames Smart 	}
1911a789241eSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
191279d8c4ceSJames Smart 	/* check the wait list */
191379d8c4ceSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
191479d8c4ceSJames Smart 		struct rqb_dmabuf *nvmebuf;
191579d8c4ceSJames Smart 		struct fc_frame_header *fc_hdr_tmp;
191679d8c4ceSJames Smart 		u32 sid_tmp;
191779d8c4ceSJames Smart 		u16 oxid_tmp;
191879d8c4ceSJames Smart 		bool found = false;
191979d8c4ceSJames Smart 
192079d8c4ceSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
192179d8c4ceSJames Smart 
192279d8c4ceSJames Smart 		/* match by oxid and s_id */
192379d8c4ceSJames Smart 		list_for_each_entry(nvmebuf,
192479d8c4ceSJames Smart 				    &phba->sli4_hba.lpfc_nvmet_io_wait_list,
192579d8c4ceSJames Smart 				    hbuf.list) {
192679d8c4ceSJames Smart 			fc_hdr_tmp = (struct fc_frame_header *)
192779d8c4ceSJames Smart 					(nvmebuf->hbuf.virt);
192879d8c4ceSJames Smart 			oxid_tmp = be16_to_cpu(fc_hdr_tmp->fh_ox_id);
192979d8c4ceSJames Smart 			sid_tmp = sli4_sid_from_fc_hdr(fc_hdr_tmp);
193079d8c4ceSJames Smart 			if (oxid_tmp != oxid || sid_tmp != sid)
193179d8c4ceSJames Smart 				continue;
193286c67379SJames Smart 
193386c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
193479d8c4ceSJames Smart 					"6321 NVMET Rcv ABTS oxid x%x from x%x "
193579d8c4ceSJames Smart 					"is waiting for a ctxp\n",
193679d8c4ceSJames Smart 					oxid, sid);
193779d8c4ceSJames Smart 
193879d8c4ceSJames Smart 			list_del_init(&nvmebuf->hbuf.list);
193979d8c4ceSJames Smart 			phba->sli4_hba.nvmet_io_wait_cnt--;
194079d8c4ceSJames Smart 			found = true;
194179d8c4ceSJames Smart 			break;
194279d8c4ceSJames Smart 		}
194379d8c4ceSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
194479d8c4ceSJames Smart 				       iflag);
194579d8c4ceSJames Smart 
194679d8c4ceSJames Smart 		/* free buffer since already posted a new DMA buffer to RQ */
194779d8c4ceSJames Smart 		if (found) {
194879d8c4ceSJames Smart 			nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
194979d8c4ceSJames Smart 			/* Respond with BA_ACC accordingly */
195079d8c4ceSJames Smart 			lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
195179d8c4ceSJames Smart 			return 0;
195279d8c4ceSJames Smart 		}
195379d8c4ceSJames Smart 	}
195479d8c4ceSJames Smart 
195579d8c4ceSJames Smart 	/* check active list */
195679d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_oxid(phba, oxid, sid);
195779d8c4ceSJames Smart 	if (ctxp) {
195879d8c4ceSJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
195979d8c4ceSJames Smart 
196079d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
19617b7f551bSJames Smart 		ctxp->flag |= (LPFC_NVME_ABTS_RCV | LPFC_NVME_ABORT_OP);
196279d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
196379d8c4ceSJames Smart 
196479d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
196579d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
196679d8c4ceSJames Smart 				 xri, raw_smp_processor_id(), 0);
196779d8c4ceSJames Smart 
196879d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
196979d8c4ceSJames Smart 				"6322 NVMET Rcv ABTS:acc oxid x%x xri x%x "
197079d8c4ceSJames Smart 				"flag x%x state x%x\n",
197179d8c4ceSJames Smart 				ctxp->oxid, xri, ctxp->flag, ctxp->state);
197279d8c4ceSJames Smart 
19737b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_TNOTIFY) {
197479d8c4ceSJames Smart 			/* Notify the transport */
197579d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport,
19767cacae2aSJames Smart 					       &ctxp->hdlrctx.fcp_req);
197779d8c4ceSJames Smart 		} else {
19786594d31bSJames Smart 			cancel_work_sync(&ctxp->ctxbuf->defer_work);
197979d8c4ceSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
198079d8c4ceSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
198179d8c4ceSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
198279d8c4ceSJames Smart 		}
198379d8c4ceSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
198479d8c4ceSJames Smart 					       ctxp->oxid);
198579d8c4ceSJames Smart 
19866594d31bSJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
198779d8c4ceSJames Smart 		return 0;
198879d8c4ceSJames Smart 	}
198979d8c4ceSJames Smart 
199079d8c4ceSJames Smart 	lpfc_nvmeio_data(phba, "NVMET ABTS RCV: oxid x%x CPU %02x rjt %d\n",
199179d8c4ceSJames Smart 			 oxid, raw_smp_processor_id(), 1);
199279d8c4ceSJames Smart 
199379d8c4ceSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
199479d8c4ceSJames Smart 			"6320 NVMET Rcv ABTS:rjt oxid x%x\n", oxid);
199586c67379SJames Smart 
199686c67379SJames Smart 	/* Respond with BA_RJT accordingly */
199786c67379SJames Smart 	lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 0);
199886c67379SJames Smart #endif
1999b06e13c3SJens Axboe 	return 0;
2000318083adSJames Smart }
2001318083adSJames Smart 
20026e8e1c14SJames Smart static void
20036e8e1c14SJames Smart lpfc_nvmet_wqfull_flush(struct lpfc_hba *phba, struct lpfc_queue *wq,
20047cacae2aSJames Smart 			struct lpfc_async_xchg_ctx *ctxp)
20056e8e1c14SJames Smart {
20066e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
20076e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
20086e8e1c14SJames Smart 	struct lpfc_iocbq *next_nvmewqeq;
20096e8e1c14SJames Smart 	unsigned long iflags;
20106e8e1c14SJames Smart 	struct lpfc_wcqe_complete wcqe;
20116e8e1c14SJames Smart 	struct lpfc_wcqe_complete *wcqep;
20126e8e1c14SJames Smart 
20136e8e1c14SJames Smart 	pring = wq->pring;
20146e8e1c14SJames Smart 	wcqep = &wcqe;
20156e8e1c14SJames Smart 
20166e8e1c14SJames Smart 	/* Fake an ABORT error code back to cmpl routine */
20176e8e1c14SJames Smart 	memset(wcqep, 0, sizeof(struct lpfc_wcqe_complete));
20186e8e1c14SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, IOSTAT_LOCAL_REJECT);
20196e8e1c14SJames Smart 	wcqep->parameter = IOERR_ABORT_REQUESTED;
20206e8e1c14SJames Smart 
20216e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
20226e8e1c14SJames Smart 	list_for_each_entry_safe(nvmewqeq, next_nvmewqeq,
20236e8e1c14SJames Smart 				 &wq->wqfull_list, list) {
20246e8e1c14SJames Smart 		if (ctxp) {
20256e8e1c14SJames Smart 			/* Checking for a specific IO to flush */
2026d51cf5bdSJames Smart 			if (nvmewqeq->context_un.axchg == ctxp) {
20276e8e1c14SJames Smart 				list_del(&nvmewqeq->list);
20286e8e1c14SJames Smart 				spin_unlock_irqrestore(&pring->ring_lock,
20296e8e1c14SJames Smart 						       iflags);
2030a680a929SJames Smart 				memcpy(&nvmewqeq->wcqe_cmpl, wcqep,
2031a680a929SJames Smart 				       sizeof(*wcqep));
20326e8e1c14SJames Smart 				lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq,
2033a680a929SJames Smart 							  nvmewqeq);
20346e8e1c14SJames Smart 				return;
20356e8e1c14SJames Smart 			}
20366e8e1c14SJames Smart 			continue;
20376e8e1c14SJames Smart 		} else {
20386e8e1c14SJames Smart 			/* Flush all IOs */
20396e8e1c14SJames Smart 			list_del(&nvmewqeq->list);
20406e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
2041a680a929SJames Smart 			memcpy(&nvmewqeq->wcqe_cmpl, wcqep, sizeof(*wcqep));
2042a680a929SJames Smart 			lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq, nvmewqeq);
20436e8e1c14SJames Smart 			spin_lock_irqsave(&pring->ring_lock, iflags);
20446e8e1c14SJames Smart 		}
20456e8e1c14SJames Smart 	}
20466e8e1c14SJames Smart 	if (!ctxp)
20476e8e1c14SJames Smart 		wq->q_flag &= ~HBA_NVMET_WQFULL;
20486e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
20496e8e1c14SJames Smart }
20506e8e1c14SJames Smart 
20516e8e1c14SJames Smart void
20526e8e1c14SJames Smart lpfc_nvmet_wqfull_process(struct lpfc_hba *phba,
20536e8e1c14SJames Smart 			  struct lpfc_queue *wq)
20546e8e1c14SJames Smart {
20556e8e1c14SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
20566e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
20576e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
20587cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
20596e8e1c14SJames Smart 	unsigned long iflags;
20606e8e1c14SJames Smart 	int rc;
20616e8e1c14SJames Smart 
20626e8e1c14SJames Smart 	/*
20636e8e1c14SJames Smart 	 * Some WQE slots are available, so try to re-issue anything
20646e8e1c14SJames Smart 	 * on the WQ wqfull_list.
20656e8e1c14SJames Smart 	 */
20666e8e1c14SJames Smart 	pring = wq->pring;
20676e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
20686e8e1c14SJames Smart 	while (!list_empty(&wq->wqfull_list)) {
20696e8e1c14SJames Smart 		list_remove_head(&wq->wqfull_list, nvmewqeq, struct lpfc_iocbq,
20706e8e1c14SJames Smart 				 list);
20716e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
2072d51cf5bdSJames Smart 		ctxp = nvmewqeq->context_un.axchg;
20731fbf9742SJames Smart 		rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
20746e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
20756e8e1c14SJames Smart 		if (rc == -EBUSY) {
20766e8e1c14SJames Smart 			/* WQ was full again, so put it back on the list */
20776e8e1c14SJames Smart 			list_add(&nvmewqeq->list, &wq->wqfull_list);
20786e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
20796e8e1c14SJames Smart 			return;
20806e8e1c14SJames Smart 		}
208179d8c4ceSJames Smart 		if (rc == WQE_SUCCESS) {
208279d8c4ceSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
208379d8c4ceSJames Smart 			if (ctxp->ts_cmd_nvme) {
20847cacae2aSJames Smart 				if (ctxp->hdlrctx.fcp_req.op == NVMET_FCOP_RSP)
208579d8c4ceSJames Smart 					ctxp->ts_status_wqput = ktime_get_ns();
208679d8c4ceSJames Smart 				else
208779d8c4ceSJames Smart 					ctxp->ts_data_wqput = ktime_get_ns();
208879d8c4ceSJames Smart 			}
208979d8c4ceSJames Smart #endif
209079d8c4ceSJames Smart 		} else {
209179d8c4ceSJames Smart 			WARN_ON(rc);
209279d8c4ceSJames Smart 		}
20936e8e1c14SJames Smart 	}
20946e8e1c14SJames Smart 	wq->q_flag &= ~HBA_NVMET_WQFULL;
20956e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
20966e8e1c14SJames Smart 
20976e8e1c14SJames Smart #endif
20986e8e1c14SJames Smart }
20996e8e1c14SJames Smart 
2100d613b6a7SJames Smart void
2101d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
2102d613b6a7SJames Smart {
21037d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2104d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
21056e8e1c14SJames Smart 	struct lpfc_queue *wq;
21066e8e1c14SJames Smart 	uint32_t qidx;
2107c41f5988SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(tport_unreg_cmp);
2108d613b6a7SJames Smart 
2109d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
2110d613b6a7SJames Smart 		return;
2111d613b6a7SJames Smart 	if (phba->targetport) {
2112d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2113cdb42becSJames Smart 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
2114c00f62e6SJames Smart 			wq = phba->sli4_hba.hdwq[qidx].io_wq;
21156e8e1c14SJames Smart 			lpfc_nvmet_wqfull_flush(phba, wq, NULL);
21166e8e1c14SJames Smart 		}
2117c41f5988SEwan D. Milne 		tgtp->tport_unreg_cmp = &tport_unreg_cmp;
2118d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
2119af6de8c6SEwan D. Milne 		if (!wait_for_completion_timeout(&tport_unreg_cmp,
21202a0fb340SJames Smart 					msecs_to_jiffies(LPFC_NVMET_WAIT_TMO)))
2121372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
212232350664SJames Smart 					"6179 Unreg targetport x%px timeout "
21232a0fb340SJames Smart 					"reached.\n", phba->targetport);
21246c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
2125d613b6a7SJames Smart 	}
2126d613b6a7SJames Smart 	phba->targetport = NULL;
2127166d7211SJames Smart #endif
2128d613b6a7SJames Smart }
2129d613b6a7SJames Smart 
2130d613b6a7SJames Smart /**
21313a8070c5SJames Smart  * lpfc_nvmet_handle_lsreq - Process an NVME LS request
2132d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
21333a8070c5SJames Smart  * @axchg: pointer to exchange context for the NVME LS request
2134d613b6a7SJames Smart  *
21353a8070c5SJames Smart  * This routine is used for processing an asychronously received NVME LS
21363a8070c5SJames Smart  * request. Any remaining validation is done and the LS is then forwarded
21373a8070c5SJames Smart  * to the nvmet-fc transport via nvmet_fc_rcv_ls_req().
21383a8070c5SJames Smart  *
21393a8070c5SJames Smart  * The calling sequence should be: nvmet_fc_rcv_ls_req() -> (processing)
21403a8070c5SJames Smart  * -> lpfc_nvmet_xmt_ls_rsp/cmp -> req->done.
21413a8070c5SJames Smart  * lpfc_nvme_xmt_ls_rsp_cmp should free the allocated axchg.
21423a8070c5SJames Smart  *
21433a8070c5SJames Smart  * Returns 0 if LS was handled and delivered to the transport
21443a8070c5SJames Smart  * Returns 1 if LS failed to be handled and should be dropped
21453a8070c5SJames Smart  */
21463a8070c5SJames Smart int
21473a8070c5SJames Smart lpfc_nvmet_handle_lsreq(struct lpfc_hba *phba,
21483a8070c5SJames Smart 			struct lpfc_async_xchg_ctx *axchg)
2149d613b6a7SJames Smart {
21507d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
21513a8070c5SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = phba->targetport->private;
21523a8070c5SJames Smart 	uint32_t *payload = axchg->payload;
21533a8070c5SJames Smart 	int rc;
2154d613b6a7SJames Smart 
2155d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
21563a8070c5SJames Smart 
21574c2805aaSJames Smart 	/*
21584c2805aaSJames Smart 	 * Driver passes the ndlp as the hosthandle argument allowing
21594c2805aaSJames Smart 	 * the transport to generate LS requests for any associateions
21604c2805aaSJames Smart 	 * that are created.
21614c2805aaSJames Smart 	 */
21624c2805aaSJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, axchg->ndlp, &axchg->ls_rsp,
21633a8070c5SJames Smart 				 axchg->payload, axchg->size);
2164d613b6a7SJames Smart 
2165d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2166ce1b591cSJames Smart 			"6037 NVMET Unsol rcv: sz %d rc %d: %08x %08x %08x "
21673a8070c5SJames Smart 			"%08x %08x %08x\n", axchg->size, rc,
2168d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
2169d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
21702b65e182SJames Smart 
21713a8070c5SJames Smart 	if (!rc) {
2172d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
21733a8070c5SJames Smart 		return 0;
2174d613b6a7SJames Smart 	}
21752b65e182SJames Smart 
2176d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
2177166d7211SJames Smart #endif
21783a8070c5SJames Smart 	return 1;
2179d613b6a7SJames Smart }
2180d613b6a7SJames Smart 
2181472e146dSJames Smart static void
2182472e146dSJames Smart lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf)
2183472e146dSJames Smart {
2184472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
21857cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp = ctx_buf->context;
2186472e146dSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
2187472e146dSJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
2188472e146dSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
21894552e0f6SJames Smart 	uint32_t *payload, qno;
2190472e146dSJames Smart 	uint32_t rc;
2191472e146dSJames Smart 	unsigned long iflags;
2192472e146dSJames Smart 
2193472e146dSJames Smart 	if (!nvmebuf) {
2194372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2195472e146dSJames Smart 			"6159 process_rcv_fcp_req, nvmebuf is NULL, "
2196472e146dSJames Smart 			"oxid: x%x flg: x%x state: x%x\n",
2197472e146dSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
2198472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
2199472e146dSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2200472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2201472e146dSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
2202472e146dSJames Smart 						 ctxp->oxid);
2203472e146dSJames Smart 		return;
2204472e146dSJames Smart 	}
2205472e146dSJames Smart 
22067b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABTS_RCV) {
2207372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
220879d8c4ceSJames Smart 				"6324 IO oxid x%x aborted\n",
220979d8c4ceSJames Smart 				ctxp->oxid);
221079d8c4ceSJames Smart 		return;
221179d8c4ceSJames Smart 	}
221279d8c4ceSJames Smart 
2213472e146dSJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
2214472e146dSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
22157b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_TNOTIFY;
2216d74a89aaSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2217d74a89aaSJames Smart 	if (ctxp->ts_isr_cmd)
2218d74a89aaSJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
2219d74a89aaSJames Smart #endif
2220472e146dSJames Smart 	/*
2221472e146dSJames Smart 	 * The calling sequence should be:
2222472e146dSJames Smart 	 * nvmet_fc_rcv_fcp_req->lpfc_nvmet_xmt_fcp_op/cmp- req->done
2223472e146dSJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
2224472e146dSJames Smart 	 * When we return from nvmet_fc_rcv_fcp_req, all relevant info
2225472e146dSJames Smart 	 * the NVME command / FC header is stored.
2226472e146dSJames Smart 	 * A buffer has already been reposted for this IO, so just free
2227472e146dSJames Smart 	 * the nvmebuf.
2228472e146dSJames Smart 	 */
22297cacae2aSJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->hdlrctx.fcp_req,
2230472e146dSJames Smart 				  payload, ctxp->size);
2231472e146dSJames Smart 	/* Process FCP command */
2232472e146dSJames Smart 	if (rc == 0) {
2233472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
22344552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
22357b7f551bSJames Smart 		if ((ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) ||
22364552e0f6SJames Smart 		    (nvmebuf != ctxp->rqb_buffer)) {
22374552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22384552e0f6SJames Smart 			return;
22394552e0f6SJames Smart 		}
22404552e0f6SJames Smart 		ctxp->rqb_buffer = NULL;
22414552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22424552e0f6SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
2243472e146dSJames Smart 		return;
2244472e146dSJames Smart 	}
2245472e146dSJames Smart 
2246472e146dSJames Smart 	/* Processing of FCP command is deferred */
2247472e146dSJames Smart 	if (rc == -EOVERFLOW) {
2248472e146dSJames Smart 		lpfc_nvmeio_data(phba, "NVMET RCV BUSY: xri x%x sz %d "
2249472e146dSJames Smart 				 "from %06x\n",
2250472e146dSJames Smart 				 ctxp->oxid, ctxp->size, ctxp->sid);
2251472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
2252472e146dSJames Smart 		atomic_inc(&tgtp->defer_fod);
22534552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
22547b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) {
22554552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22564552e0f6SJames Smart 			return;
22574552e0f6SJames Smart 		}
22584552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22594552e0f6SJames Smart 		/*
22604552e0f6SJames Smart 		 * Post a replacement DMA buffer to RQ and defer
22614552e0f6SJames Smart 		 * freeing rcv buffer till .defer_rcv callback
22624552e0f6SJames Smart 		 */
22634552e0f6SJames Smart 		qno = nvmebuf->idx;
22644552e0f6SJames Smart 		lpfc_post_rq_buffer(
22654552e0f6SJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
22664552e0f6SJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2267472e146dSJames Smart 		return;
2268472e146dSJames Smart 	}
22697b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_TNOTIFY;
2270472e146dSJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2271372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2272472e146dSJames Smart 			"2582 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
2273472e146dSJames Smart 			ctxp->oxid, rc,
2274472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_in),
2275472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_out),
2276472e146dSJames Smart 			atomic_read(&tgtp->xmt_fcp_release));
2277472e146dSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
2278472e146dSJames Smart 			 ctxp->oxid, ctxp->size, ctxp->sid);
2279472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflags);
2280472e146dSJames Smart 	lpfc_nvmet_defer_release(phba, ctxp);
2281472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2282472e146dSJames Smart 	lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
2283472e146dSJames Smart #endif
2284472e146dSJames Smart }
2285472e146dSJames Smart 
2286472e146dSJames Smart static void
2287472e146dSJames Smart lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *work)
2288472e146dSJames Smart {
2289472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2290472e146dSJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf =
2291472e146dSJames Smart 		container_of(work, struct lpfc_nvmet_ctxbuf, defer_work);
2292472e146dSJames Smart 
2293472e146dSJames Smart 	lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2294472e146dSJames Smart #endif
2295472e146dSJames Smart }
2296472e146dSJames Smart 
229766d7ce93SDick Kennedy static struct lpfc_nvmet_ctxbuf *
229866d7ce93SDick Kennedy lpfc_nvmet_replenish_context(struct lpfc_hba *phba,
229966d7ce93SDick Kennedy 			     struct lpfc_nvmet_ctx_info *current_infop)
230066d7ce93SDick Kennedy {
23012299e432SDick Kennedy #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
230266d7ce93SDick Kennedy 	struct lpfc_nvmet_ctxbuf *ctx_buf = NULL;
230366d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *get_infop;
230466d7ce93SDick Kennedy 	int i;
230566d7ce93SDick Kennedy 
230666d7ce93SDick Kennedy 	/*
230766d7ce93SDick Kennedy 	 * The current_infop for the MRQ a NVME command IU was received
230866d7ce93SDick Kennedy 	 * on is empty. Our goal is to replenish this MRQs context
230966d7ce93SDick Kennedy 	 * list from a another CPUs.
231066d7ce93SDick Kennedy 	 *
231166d7ce93SDick Kennedy 	 * First we need to pick a context list to start looking on.
231266d7ce93SDick Kennedy 	 * nvmet_ctx_start_cpu has available context the last time
231366d7ce93SDick Kennedy 	 * we needed to replenish this CPU where nvmet_ctx_next_cpu
231466d7ce93SDick Kennedy 	 * is just the next sequential CPU for this MRQ.
231566d7ce93SDick Kennedy 	 */
231666d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_start_cpu)
231766d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_start_cpu;
231866d7ce93SDick Kennedy 	else
231966d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_next_cpu;
232066d7ce93SDick Kennedy 
2321222e9239SJames Smart 	for (i = 0; i < phba->sli4_hba.num_possible_cpu; i++) {
232266d7ce93SDick Kennedy 		if (get_infop == current_infop) {
232366d7ce93SDick Kennedy 			get_infop = get_infop->nvmet_ctx_next_cpu;
232466d7ce93SDick Kennedy 			continue;
232566d7ce93SDick Kennedy 		}
232666d7ce93SDick Kennedy 		spin_lock(&get_infop->nvmet_ctx_list_lock);
232766d7ce93SDick Kennedy 
232866d7ce93SDick Kennedy 		/* Just take the entire context list, if there are any */
232966d7ce93SDick Kennedy 		if (get_infop->nvmet_ctx_list_cnt) {
233066d7ce93SDick Kennedy 			list_splice_init(&get_infop->nvmet_ctx_list,
233166d7ce93SDick Kennedy 				    &current_infop->nvmet_ctx_list);
233266d7ce93SDick Kennedy 			current_infop->nvmet_ctx_list_cnt =
233366d7ce93SDick Kennedy 				get_infop->nvmet_ctx_list_cnt - 1;
233466d7ce93SDick Kennedy 			get_infop->nvmet_ctx_list_cnt = 0;
233566d7ce93SDick Kennedy 			spin_unlock(&get_infop->nvmet_ctx_list_lock);
233666d7ce93SDick Kennedy 
233766d7ce93SDick Kennedy 			current_infop->nvmet_ctx_start_cpu = get_infop;
233866d7ce93SDick Kennedy 			list_remove_head(&current_infop->nvmet_ctx_list,
233966d7ce93SDick Kennedy 					 ctx_buf, struct lpfc_nvmet_ctxbuf,
234066d7ce93SDick Kennedy 					 list);
234166d7ce93SDick Kennedy 			return ctx_buf;
234266d7ce93SDick Kennedy 		}
234366d7ce93SDick Kennedy 
234466d7ce93SDick Kennedy 		/* Otherwise, move on to the next CPU for this MRQ */
234566d7ce93SDick Kennedy 		spin_unlock(&get_infop->nvmet_ctx_list_lock);
234666d7ce93SDick Kennedy 		get_infop = get_infop->nvmet_ctx_next_cpu;
234766d7ce93SDick Kennedy 	}
234866d7ce93SDick Kennedy 
23492299e432SDick Kennedy #endif
235066d7ce93SDick Kennedy 	/* Nothing found, all contexts for the MRQ are in-flight */
235166d7ce93SDick Kennedy 	return NULL;
235266d7ce93SDick Kennedy }
235366d7ce93SDick Kennedy 
2354d613b6a7SJames Smart /**
2355d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
2356d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
235766d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2358d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
2359d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2360d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2361d613b6a7SJames Smart  *
2362d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
2363d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
2364d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
2365d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
2366d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
2367d613b6a7SJames Smart  * of the discovery state machine.
2368d613b6a7SJames Smart  **/
2369d613b6a7SJames Smart static void
2370d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
237166d7ce93SDick Kennedy 			    uint32_t idx,
2372d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
2373d74a89aaSJames Smart 			    uint64_t isr_timestamp,
2374d74a89aaSJames Smart 			    uint8_t cqflag)
2375d613b6a7SJames Smart {
23767cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
2377d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2378d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
23796c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
238066d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *current_infop;
2381472e146dSJames Smart 	uint32_t size, oxid, sid, qno;
23826c621a22SJames Smart 	unsigned long iflag;
238366d7ce93SDick Kennedy 	int current_cpu;
2384d613b6a7SJames Smart 
238573626173SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_TARGET_FC))
238673626173SArnd Bergmann 		return;
238773626173SArnd Bergmann 
23886c621a22SJames Smart 	ctx_buf = NULL;
2389d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
2390372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
23916c621a22SJames Smart 				"6157 NVMET FCP Drop IO\n");
2392472e146dSJames Smart 		if (nvmebuf)
2393472e146dSJames Smart 			lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2394472e146dSJames Smart 		return;
2395d613b6a7SJames Smart 	}
2396d613b6a7SJames Smart 
239766d7ce93SDick Kennedy 	/*
239866d7ce93SDick Kennedy 	 * Get a pointer to the context list for this MRQ based on
239966d7ce93SDick Kennedy 	 * the CPU this MRQ IRQ is associated with. If the CPU association
240066d7ce93SDick Kennedy 	 * changes from our initial assumption, the context list could
240166d7ce93SDick Kennedy 	 * be empty, thus it would need to be replenished with the
240266d7ce93SDick Kennedy 	 * context list from another CPU for this MRQ.
240366d7ce93SDick Kennedy 	 */
2404d6d189ceSBart Van Assche 	current_cpu = raw_smp_processor_id();
240566d7ce93SDick Kennedy 	current_infop = lpfc_get_ctx_list(phba, current_cpu, idx);
240666d7ce93SDick Kennedy 	spin_lock_irqsave(&current_infop->nvmet_ctx_list_lock, iflag);
240766d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_list_cnt) {
240866d7ce93SDick Kennedy 		list_remove_head(&current_infop->nvmet_ctx_list,
24096c621a22SJames Smart 				 ctx_buf, struct lpfc_nvmet_ctxbuf, list);
241066d7ce93SDick Kennedy 		current_infop->nvmet_ctx_list_cnt--;
2411966bb5b7SJames Smart 	} else {
241266d7ce93SDick Kennedy 		ctx_buf = lpfc_nvmet_replenish_context(phba, current_infop);
24136c621a22SJames Smart 	}
241466d7ce93SDick Kennedy 	spin_unlock_irqrestore(&current_infop->nvmet_ctx_list_lock, iflag);
24156c621a22SJames Smart 
2416a8cf5dfeSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
2417a8cf5dfeSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2418a8cf5dfeSJames Smart 	size = nvmebuf->bytes_recv;
2419a8cf5dfeSJames Smart 
2420a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2421840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
2422840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->rcv_io);
242363df6d63SJames Smart 		if (idx != current_cpu)
242463df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
242563df6d63SJames Smart 					"6703 CPU Check rcv: "
242663df6d63SJames Smart 					"cpu %d expect %d\n",
242763df6d63SJames Smart 					current_cpu, idx);
2428a8cf5dfeSJames Smart 	}
2429a8cf5dfeSJames Smart #endif
2430a8cf5dfeSJames Smart 
2431a8cf5dfeSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d CPU %02x\n",
2432d6d189ceSBart Van Assche 			 oxid, size, raw_smp_processor_id());
2433a8cf5dfeSJames Smart 
2434411de511SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2435411de511SJames Smart 
24366c621a22SJames Smart 	if (!ctx_buf) {
2437a8cf5dfeSJames Smart 		/* Queue this NVME IO to process later */
2438a8cf5dfeSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
2439a8cf5dfeSJames Smart 		list_add_tail(&nvmebuf->hbuf.list,
2440a8cf5dfeSJames Smart 			      &phba->sli4_hba.lpfc_nvmet_io_wait_list);
2441a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt++;
2442a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_total++;
2443a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
2444a8cf5dfeSJames Smart 				       iflag);
2445a8cf5dfeSJames Smart 
2446a8cf5dfeSJames Smart 		/* Post a brand new DMA buffer to RQ */
2447a8cf5dfeSJames Smart 		qno = nvmebuf->idx;
2448a8cf5dfeSJames Smart 		lpfc_post_rq_buffer(
2449a8cf5dfeSJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
2450a8cf5dfeSJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2451411de511SJames Smart 
2452411de511SJames Smart 		atomic_inc(&tgtp->defer_ctx);
2453a8cf5dfeSJames Smart 		return;
24546c621a22SJames Smart 	}
2455d613b6a7SJames Smart 
2456d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
2457d613b6a7SJames Smart 
24587cacae2aSJames Smart 	ctxp = (struct lpfc_async_xchg_ctx *)ctx_buf->context;
245979d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
246079d8c4ceSJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.t_active_ctx_list);
246179d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
24627b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_FREE) {
2463372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2464ce1b591cSJames Smart 				"6414 NVMET Context corrupt %d %d oxid x%x\n",
2465ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
2466ce1b591cSJames Smart 	}
2467d613b6a7SJames Smart 	ctxp->wqeq = NULL;
2468d613b6a7SJames Smart 	ctxp->offset = 0;
2469d613b6a7SJames Smart 	ctxp->phba = phba;
2470d613b6a7SJames Smart 	ctxp->size = size;
2471d613b6a7SJames Smart 	ctxp->oxid = oxid;
2472d613b6a7SJames Smart 	ctxp->sid = sid;
247366d7ce93SDick Kennedy 	ctxp->idx = idx;
24747b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_RCV;
2475d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
2476d613b6a7SJames Smart 	ctxp->flag = 0;
24776c621a22SJames Smart 	ctxp->ctxbuf = ctx_buf;
2478cbc5de1bSJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
24791fbf9742SJames Smart 	ctxp->hdwq = NULL;
24802b7824d0SJames Smart 	spin_lock_init(&ctxp->ctxlock);
2481d613b6a7SJames Smart 
24822b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2483d74a89aaSJames Smart 	if (isr_timestamp)
24842b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
2485d74a89aaSJames Smart 	ctxp->ts_cmd_nvme = 0;
24862b65e182SJames Smart 	ctxp->ts_nvme_data = 0;
24872b65e182SJames Smart 	ctxp->ts_data_wqput = 0;
24882b65e182SJames Smart 	ctxp->ts_isr_data = 0;
24892b65e182SJames Smart 	ctxp->ts_data_nvme = 0;
24902b65e182SJames Smart 	ctxp->ts_nvme_status = 0;
24912b65e182SJames Smart 	ctxp->ts_status_wqput = 0;
24922b65e182SJames Smart 	ctxp->ts_isr_status = 0;
24932b65e182SJames Smart 	ctxp->ts_status_nvme = 0;
24942b65e182SJames Smart #endif
24952b65e182SJames Smart 
2496d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
2497d74a89aaSJames Smart 	/* check for cq processing load */
2498d74a89aaSJames Smart 	if (!cqflag) {
2499472e146dSJames Smart 		lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2500d74a89aaSJames Smart 		return;
2501d74a89aaSJames Smart 	}
2502d74a89aaSJames Smart 
2503d74a89aaSJames Smart 	if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
2504d74a89aaSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2505372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2506d74a89aaSJames Smart 				"6325 Unable to queue work for oxid x%x. "
2507d74a89aaSJames Smart 				"FCP Drop IO [x%x x%x x%x]\n",
2508d74a89aaSJames Smart 				ctxp->oxid,
2509d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_in),
2510d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_out),
2511d74a89aaSJames Smart 				atomic_read(&tgtp->xmt_fcp_release));
2512d74a89aaSJames Smart 
2513d74a89aaSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
2514d74a89aaSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2515d74a89aaSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
2516d74a89aaSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
2517d74a89aaSJames Smart 	}
2518d613b6a7SJames Smart }
2519d613b6a7SJames Smart 
2520d613b6a7SJames Smart /**
2521d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
2522d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
252366d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2524d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2525d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2526d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2527d613b6a7SJames Smart  *
2528d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2529d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2530d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2531d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
2532d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2533d613b6a7SJames Smart  **/
2534d613b6a7SJames Smart void
2535d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
253666d7ce93SDick Kennedy 			   uint32_t idx,
2537d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
2538d74a89aaSJames Smart 			   uint64_t isr_timestamp,
2539d74a89aaSJames Smart 			   uint8_t cqflag)
2540d613b6a7SJames Smart {
254143bfea1bSJames Smart 	if (!nvmebuf) {
2542372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
254343bfea1bSJames Smart 				"3167 NVMET FCP Drop IO\n");
254443bfea1bSJames Smart 		return;
254543bfea1bSJames Smart 	}
2546d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
25476c621a22SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2548d613b6a7SJames Smart 		return;
2549d613b6a7SJames Smart 	}
2550d74a89aaSJames Smart 	lpfc_nvmet_unsol_fcp_buffer(phba, idx, nvmebuf, isr_timestamp, cqflag);
2551d613b6a7SJames Smart }
2552d613b6a7SJames Smart 
2553d613b6a7SJames Smart /**
2554d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
2555d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
2556d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
2557d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
2558d613b6a7SJames Smart  * @rspsize: size of the NVME command.
2559d613b6a7SJames Smart  *
2560d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
2561d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
2562d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
2563d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
2564d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
2565d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
2566d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
2567d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
2568d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
2569d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
2570d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
2571d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
2572d613b6a7SJames Smart  * reference for the command's callback function to access later.
2573d613b6a7SJames Smart  *
2574d613b6a7SJames Smart  * Return code
2575d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
2576d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
2577d613b6a7SJames Smart  **/
2578d613b6a7SJames Smart static struct lpfc_iocbq *
2579d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
25807cacae2aSJames Smart 		       struct lpfc_async_xchg_ctx *ctxp,
2581d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
2582d613b6a7SJames Smart {
2583d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2584d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2585205e8240SJames Smart 	union lpfc_wqe128 *wqe;
2586d613b6a7SJames Smart 
2587d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2588372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2589ce1b591cSJames Smart 				"6104 NVMET prep LS wqe: link err: "
2590ce1b591cSJames Smart 				"NPORT x%x oxid:x%x ste %d\n",
2591ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2592d613b6a7SJames Smart 		return NULL;
2593d613b6a7SJames Smart 	}
2594d613b6a7SJames Smart 
2595d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
2596d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
2597d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2598372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2599ce1b591cSJames Smart 				"6105 NVMET prep LS wqe: No WQE: "
2600ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2601ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2602d613b6a7SJames Smart 		return NULL;
2603d613b6a7SJames Smart 	}
2604d613b6a7SJames Smart 
2605d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2606307e3380SJames Smart 	if (!ndlp ||
2607d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2608d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2609372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2610ce1b591cSJames Smart 				"6106 NVMET prep LS wqe: No ndlp: "
2611ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2612ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2613d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2614d613b6a7SJames Smart 	}
2615d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
2616d613b6a7SJames Smart 
2617d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
2618d51cf5bdSJames Smart 	nvmewqe->ndlp = lpfc_nlp_get(ndlp);
2619d51cf5bdSJames Smart 	if (!nvmewqe->ndlp)
2620d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2621d51cf5bdSJames Smart 	nvmewqe->context_un.axchg = ctxp;
2622d613b6a7SJames Smart 
2623d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
2624d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
2625d613b6a7SJames Smart 
2626d613b6a7SJames Smart 	/* Words 0 - 2 */
2627d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2628d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
2629d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
2630d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
2631d613b6a7SJames Smart 
2632d613b6a7SJames Smart 	/* Word 3 */
2633d613b6a7SJames Smart 
2634d613b6a7SJames Smart 	/* Word 4 */
2635d613b6a7SJames Smart 
2636d613b6a7SJames Smart 	/* Word 5 */
2637d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
2638d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
2639d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
26408b361639SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
2641d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
2642d613b6a7SJames Smart 
2643d613b6a7SJames Smart 	/* Word 6 */
2644d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
2645d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2646d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
2647d613b6a7SJames Smart 
2648d613b6a7SJames Smart 	/* Word 7 */
2649d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
2650d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
2651d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
2652d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
2653d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
2654d613b6a7SJames Smart 
2655d613b6a7SJames Smart 	/* Word 8 */
2656d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
2657d613b6a7SJames Smart 
2658d613b6a7SJames Smart 	/* Word 9 */
2659d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
2660d613b6a7SJames Smart 	/* Needs to be set by caller */
2661d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
2662d613b6a7SJames Smart 
2663d613b6a7SJames Smart 	/* Word 10 */
2664d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
2665d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2666d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
2667d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
2668d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
2669d613b6a7SJames Smart 
2670d613b6a7SJames Smart 	/* Word 11 */
2671d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
2672d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
2673d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
2674d613b6a7SJames Smart 	       OTHER_COMMAND);
2675d613b6a7SJames Smart 
2676d613b6a7SJames Smart 	/* Word 12 */
2677d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
2678d613b6a7SJames Smart 
2679d613b6a7SJames Smart 	nvmewqe->retry = 1;
2680d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2681d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2682a680a929SJames Smart 	nvmewqe->cmd_flag |= LPFC_IO_NVME_LS;
2683d613b6a7SJames Smart 
2684ce1b591cSJames Smart 	/* Xmit NVMET response to remote NPORT <did> */
2685d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2686ce1b591cSJames Smart 			"6039 Xmit NVMET LS response to remote "
2687d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
2688d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
2689d613b6a7SJames Smart 			rspsize);
2690d613b6a7SJames Smart 	return nvmewqe;
2691d613b6a7SJames Smart 
2692d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
2693d51cf5bdSJames Smart 	nvmewqe->context_un.axchg = NULL;
2694d51cf5bdSJames Smart 	nvmewqe->ndlp = NULL;
2695d51cf5bdSJames Smart 	nvmewqe->bpl_dmabuf = NULL;
2696d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
2697d613b6a7SJames Smart 	return NULL;
2698d613b6a7SJames Smart }
2699d613b6a7SJames Smart 
2700d613b6a7SJames Smart 
2701d613b6a7SJames Smart static struct lpfc_iocbq *
2702d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
27037cacae2aSJames Smart 			struct lpfc_async_xchg_ctx *ctxp)
2704d613b6a7SJames Smart {
27057cacae2aSJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->hdlrctx.fcp_req;
2706d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2707d613b6a7SJames Smart 	struct sli4_sge *sgl;
2708d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2709d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2710d613b6a7SJames Smart 	struct scatterlist *sgel;
2711d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
27120bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
2713d613b6a7SJames Smart 	dma_addr_t physaddr;
2714e7f40349SJames Smart 	int i, cnt, nsegs;
2715315b3fd1SJames Smart 	bool use_pbde = false;
2716d613b6a7SJames Smart 	int xc = 1;
2717d613b6a7SJames Smart 
2718d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2719372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2720ce1b591cSJames Smart 				"6107 NVMET prep FCP wqe: link err:"
2721ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2722ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2723d613b6a7SJames Smart 		return NULL;
2724d613b6a7SJames Smart 	}
2725d613b6a7SJames Smart 
2726d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2727307e3380SJames Smart 	if (!ndlp ||
2728d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2729d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2730372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2731ce1b591cSJames Smart 				"6108 NVMET prep FCP wqe: no ndlp: "
2732ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2733ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2734d613b6a7SJames Smart 		return NULL;
2735d613b6a7SJames Smart 	}
2736d613b6a7SJames Smart 
273781e6a637SJames Smart 	if (rsp->sg_cnt > lpfc_tgttemplate.max_sgl_segments) {
2738372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2739ce1b591cSJames Smart 				"6109 NVMET prep FCP wqe: seg cnt err: "
2740ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d cnt %d\n",
2741ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state,
2742ce1b591cSJames Smart 				phba->cfg_nvme_seg_cnt);
2743d613b6a7SJames Smart 		return NULL;
2744d613b6a7SJames Smart 	}
2745e7f40349SJames Smart 	nsegs = rsp->sg_cnt;
2746d613b6a7SJames Smart 
2747d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2748d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
2749d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2750d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
27516c621a22SJames Smart 		nvmewqe = ctxp->ctxbuf->iocbq;
2752d613b6a7SJames Smart 		if (nvmewqe == NULL) {
2753372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2754ce1b591cSJames Smart 					"6110 NVMET prep FCP wqe: No "
2755ce1b591cSJames Smart 					"WQE: NPORT x%x oxid x%x ste %d\n",
2756ce1b591cSJames Smart 					ctxp->sid, ctxp->oxid, ctxp->state);
2757d613b6a7SJames Smart 			return NULL;
2758d613b6a7SJames Smart 		}
2759d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
2760d613b6a7SJames Smart 		xc = 0; /* create new XRI */
2761d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
2762d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
2763d613b6a7SJames Smart 	}
2764d613b6a7SJames Smart 
2765d613b6a7SJames Smart 	/* Sanity check */
27667b7f551bSJames Smart 	if (((ctxp->state == LPFC_NVME_STE_RCV) &&
2767d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
27687b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_DATA)) {
2769205e8240SJames Smart 		wqe = &nvmewqe->wqe;
2770d613b6a7SJames Smart 	} else {
2771372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2772ce1b591cSJames Smart 				"6111 Wrong state NVMET FCP: %d  cnt %d\n",
2773ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt);
2774d613b6a7SJames Smart 		return NULL;
2775d613b6a7SJames Smart 	}
2776d613b6a7SJames Smart 
27776c621a22SJames Smart 	sgl  = (struct sli4_sge *)ctxp->ctxbuf->sglq->sgl;
2778d613b6a7SJames Smart 	switch (rsp->op) {
2779d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
2780d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
2781bd3061baSJames Smart 		/* From the tsend template, initialize words 7 - 11 */
2782bd3061baSJames Smart 		memcpy(&wqe->words[7],
2783bd3061baSJames Smart 		       &lpfc_tsend_cmd_template.words[7],
2784bd3061baSJames Smart 		       sizeof(uint32_t) * 5);
2785bd3061baSJames Smart 
2786d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2787d613b6a7SJames Smart 		sgel = &rsp->sg[0];
2788d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2789d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2790d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
2791d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
2792d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
2793d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2794d613b6a7SJames Smart 
2795d613b6a7SJames Smart 		/* Word 3 */
2796d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
2797d613b6a7SJames Smart 
2798d613b6a7SJames Smart 		/* Word 4 */
2799d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
2800d613b6a7SJames Smart 
2801d613b6a7SJames Smart 		/* Word 5 */
2802bd3061baSJames Smart 		wqe->fcp_tsend.reserved = 0;
2803d613b6a7SJames Smart 
2804d613b6a7SJames Smart 		/* Word 6 */
2805d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
2806d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2807d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
2808d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2809d613b6a7SJames Smart 
2810bd3061baSJames Smart 		/* Word 7 - set ar later */
2811d613b6a7SJames Smart 
2812d613b6a7SJames Smart 		/* Word 8 */
2813d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
2814d613b6a7SJames Smart 
2815d613b6a7SJames Smart 		/* Word 9 */
2816d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
2817d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
2818d613b6a7SJames Smart 
2819bd3061baSJames Smart 		/* Word 10 - set wqes later, in template xc=1 */
2820bd3061baSJames Smart 		if (!xc)
2821bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 0);
2822d613b6a7SJames Smart 
2823d613b6a7SJames Smart 		/* Word 12 */
2824d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2825d613b6a7SJames Smart 
2826d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
2827d613b6a7SJames Smart 		sgl->addr_hi = 0;
2828d613b6a7SJames Smart 		sgl->addr_lo = 0;
2829d613b6a7SJames Smart 		sgl->word2 = 0;
2830d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2831d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2832d613b6a7SJames Smart 		sgl->sge_len = 0;
2833d613b6a7SJames Smart 		sgl++;
2834d613b6a7SJames Smart 		sgl->addr_hi = 0;
2835d613b6a7SJames Smart 		sgl->addr_lo = 0;
2836d613b6a7SJames Smart 		sgl->word2 = 0;
2837d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2838d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2839d613b6a7SJames Smart 		sgl->sge_len = 0;
2840d613b6a7SJames Smart 		sgl++;
2841d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
2842d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
2843bd3061baSJames Smart 
2844bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2845bd3061baSJames Smart 
284620aefac3SJames Smart 			if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
284720aefac3SJames Smart 				if (ndlp->nlp_flag & NLP_SUPPRESS_RSP)
284820aefac3SJames Smart 					bf_set(wqe_sup,
284920aefac3SJames Smart 					       &wqe->fcp_tsend.wqe_com, 1);
2850d613b6a7SJames Smart 			} else {
2851d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
2852d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
2853d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
2854d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
2855d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
2856d613b6a7SJames Smart 				       rsp->rsplen);
2857d613b6a7SJames Smart 			}
2858d613b6a7SJames Smart 		} else {
2859d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
2860d613b6a7SJames Smart 
2861bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2862d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
2863d613b6a7SJames Smart 		}
2864d613b6a7SJames Smart 		break;
2865d613b6a7SJames Smart 
2866d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
2867bd3061baSJames Smart 		/* From the treceive template, initialize words 3 - 11 */
2868bd3061baSJames Smart 		memcpy(&wqe->words[3],
2869bd3061baSJames Smart 		       &lpfc_treceive_cmd_template.words[3],
2870bd3061baSJames Smart 		       sizeof(uint32_t) * 9);
2871bd3061baSJames Smart 
2872ea85a20cSJames Smart 		/* Words 0 - 2 : First SGE is skipped, set invalid BDE type */
2873ea85a20cSJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = LPFC_SGE_TYPE_SKIP;
2874ea85a20cSJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = 0;
2875ea85a20cSJames Smart 		wqe->fcp_treceive.bde.addrLow = 0;
2876ea85a20cSJames Smart 		wqe->fcp_treceive.bde.addrHigh = 0;
2877d613b6a7SJames Smart 
2878d613b6a7SJames Smart 		/* Word 4 */
2879d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
2880d613b6a7SJames Smart 
2881d613b6a7SJames Smart 		/* Word 6 */
2882d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
2883d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2884d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
2885d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2886d613b6a7SJames Smart 
2887d613b6a7SJames Smart 		/* Word 7 */
2888d613b6a7SJames Smart 
2889d613b6a7SJames Smart 		/* Word 8 */
2890d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
2891d613b6a7SJames Smart 
2892d613b6a7SJames Smart 		/* Word 9 */
2893d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
2894d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
2895d613b6a7SJames Smart 
2896bd3061baSJames Smart 		/* Word 10 - in template xc=1 */
2897bd3061baSJames Smart 		if (!xc)
2898bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, 0);
2899d613b6a7SJames Smart 
2900315b3fd1SJames Smart 		/* Word 11 - check for pbde */
2901315b3fd1SJames Smart 		if (nsegs == 1 && phba->cfg_enable_pbde) {
2902315b3fd1SJames Smart 			use_pbde = true;
2903315b3fd1SJames Smart 			/* Word 11 - PBDE bit already preset by template */
2904bd3061baSJames Smart 		} else {
2905315b3fd1SJames Smart 			/* Overwrite default template setting */
2906bd3061baSJames Smart 			bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 0);
2907bd3061baSJames Smart 		}
2908d613b6a7SJames Smart 
2909d613b6a7SJames Smart 		/* Word 12 */
2910d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2911d613b6a7SJames Smart 
2912ea85a20cSJames Smart 		/* Setup 2 SKIP SGEs */
2913ea85a20cSJames Smart 		sgl->addr_hi = 0;
2914ea85a20cSJames Smart 		sgl->addr_lo = 0;
2915d613b6a7SJames Smart 		sgl->word2 = 0;
2916ea85a20cSJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2917d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2918ea85a20cSJames Smart 		sgl->sge_len = 0;
2919d613b6a7SJames Smart 		sgl++;
2920d613b6a7SJames Smart 		sgl->addr_hi = 0;
2921d613b6a7SJames Smart 		sgl->addr_lo = 0;
2922d613b6a7SJames Smart 		sgl->word2 = 0;
2923d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2924d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2925d613b6a7SJames Smart 		sgl->sge_len = 0;
2926d613b6a7SJames Smart 		sgl++;
2927d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
2928d613b6a7SJames Smart 		break;
2929d613b6a7SJames Smart 
2930d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
2931bd3061baSJames Smart 		/* From the treceive template, initialize words 4 - 11 */
2932bd3061baSJames Smart 		memcpy(&wqe->words[4],
2933bd3061baSJames Smart 		       &lpfc_trsp_cmd_template.words[4],
2934bd3061baSJames Smart 		       sizeof(uint32_t) * 8);
2935bd3061baSJames Smart 
2936d613b6a7SJames Smart 		/* Words 0 - 2 */
2937d613b6a7SJames Smart 		physaddr = rsp->rspdma;
2938d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2939d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
2940d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
2941d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2942d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
2943d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2944d613b6a7SJames Smart 
2945d613b6a7SJames Smart 		/* Word 3 */
2946d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
2947d613b6a7SJames Smart 
2948d613b6a7SJames Smart 		/* Word 6 */
2949d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
2950d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2951d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
2952d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2953d613b6a7SJames Smart 
2954d613b6a7SJames Smart 		/* Word 7 */
2955d613b6a7SJames Smart 
2956d613b6a7SJames Smart 		/* Word 8 */
2957d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
2958d613b6a7SJames Smart 
2959d613b6a7SJames Smart 		/* Word 9 */
2960d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
2961d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
2962d613b6a7SJames Smart 
2963d613b6a7SJames Smart 		/* Word 10 */
2964bd3061baSJames Smart 		if (xc)
2965bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 1);
2966d613b6a7SJames Smart 
2967d613b6a7SJames Smart 		/* Word 11 */
2968bd3061baSJames Smart 		/* In template wqes=0 irsp=0 irsplen=0 - good response */
2969bd3061baSJames Smart 		if (rsp->rsplen != LPFC_NVMET_SUCCESS_LEN) {
2970bd3061baSJames Smart 			/* Bad response - embed it */
2971d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
2972d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
2973d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
2974d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
2975d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
2976d613b6a7SJames Smart 		}
2977bd3061baSJames Smart 
2978bd3061baSJames Smart 		/* Word 12 */
2979bd3061baSJames Smart 		wqe->fcp_trsp.rsvd_12_15[0] = 0;
2980d613b6a7SJames Smart 
2981d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
2982e7f40349SJames Smart 		nsegs = 0;
2983d613b6a7SJames Smart 		sgl->word2 = 0;
2984d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
2985d613b6a7SJames Smart 		break;
2986d613b6a7SJames Smart 
2987d613b6a7SJames Smart 	default:
2988d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2989d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
2990d613b6a7SJames Smart 				rsp->op);
2991d613b6a7SJames Smart 		return NULL;
2992d613b6a7SJames Smart 	}
2993d613b6a7SJames Smart 
2994d613b6a7SJames Smart 	nvmewqe->retry = 1;
2995d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2996d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2997d51cf5bdSJames Smart 	nvmewqe->ndlp = ndlp;
2998d613b6a7SJames Smart 
2999e7f40349SJames Smart 	for_each_sg(rsp->sg, sgel, nsegs, i) {
3000d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
3001d613b6a7SJames Smart 		cnt = sg_dma_len(sgel);
3002d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
3003d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
3004d613b6a7SJames Smart 		sgl->word2 = 0;
3005d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
3006d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
3007d613b6a7SJames Smart 		if ((i+1) == rsp->sg_cnt)
3008d613b6a7SJames Smart 			bf_set(lpfc_sli4_sge_last, sgl, 1);
3009d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
3010d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(cnt);
3011315b3fd1SJames Smart 		sgl++;
3012315b3fd1SJames Smart 		ctxp->offset += cnt;
3013315b3fd1SJames Smart 	}
3014315b3fd1SJames Smart 
30150bc2b7c5SJames Smart 	bde = (struct ulp_bde64 *)&wqe->words[13];
3016315b3fd1SJames Smart 	if (use_pbde) {
3017315b3fd1SJames Smart 		/* decrement sgl ptr backwards once to first data sge */
3018315b3fd1SJames Smart 		sgl--;
3019315b3fd1SJames Smart 
30200bc2b7c5SJames Smart 		/* Words 13-15 (PBDE) */
30210bc2b7c5SJames Smart 		bde->addrLow = sgl->addr_lo;
30220bc2b7c5SJames Smart 		bde->addrHigh = sgl->addr_hi;
3023315b3fd1SJames Smart 		bde->tus.f.bdeSize = le32_to_cpu(sgl->sge_len);
30240bc2b7c5SJames Smart 		bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
30250bc2b7c5SJames Smart 		bde->tus.w = cpu_to_le32(bde->tus.w);
3026414abe0aSJames Smart 	} else {
3027414abe0aSJames Smart 		memset(bde, 0, sizeof(struct ulp_bde64));
3028414abe0aSJames Smart 	}
30297b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DATA;
3030ce1b591cSJames Smart 	ctxp->entry_cnt++;
3031d613b6a7SJames Smart 	return nvmewqe;
3032d613b6a7SJames Smart }
3033d613b6a7SJames Smart 
3034d613b6a7SJames Smart /**
3035d613b6a7SJames Smart  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
3036d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3037d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3038a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
3039d613b6a7SJames Smart  *
3040d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3041d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
3042d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3043d613b6a7SJames Smart  **/
3044d613b6a7SJames Smart static void
3045d613b6a7SJames Smart lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3046a680a929SJames Smart 			     struct lpfc_iocbq *rspwqe)
3047d613b6a7SJames Smart {
30487cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3049d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3050b27cbd55SBart Van Assche 	uint32_t result;
305119b58d94SJames Smart 	unsigned long flags;
305219b58d94SJames Smart 	bool released = false;
3053a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
3054d613b6a7SJames Smart 
3055d51cf5bdSJames Smart 	ctxp = cmdwqe->context_un.axchg;
3056d613b6a7SJames Smart 	result = wcqe->parameter;
3057d613b6a7SJames Smart 
3058d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
30597b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABORT_OP)
3060547077a4SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
3061d613b6a7SJames Smart 
3062c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
30637b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DONE;
306486c67379SJames Smart 
306586c67379SJames Smart 	/* Check if we already received a free context call
306686c67379SJames Smart 	 * and we have completed processing an abort situation.
306786c67379SJames Smart 	 */
30687b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_CTX_RLS) &&
30697b7f551bSJames Smart 	    !(ctxp->flag & LPFC_NVME_XBUSY)) {
3070c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
307179d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3072c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
307319b58d94SJames Smart 		released = true;
307486c67379SJames Smart 	}
30757b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
307619b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3077547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
307819b58d94SJames Smart 
3079e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
308079d8c4ceSJames Smart 			"6165 ABORT cmpl: oxid x%x flg x%x (%d) "
308186c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
308286c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
308386c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
308486c67379SJames Smart 			result, wcqe->word3);
308586c67379SJames Smart 
3086d51cf5bdSJames Smart 	cmdwqe->rsp_dmabuf = NULL;
3087d51cf5bdSJames Smart 	cmdwqe->bpl_dmabuf = NULL;
308819b58d94SJames Smart 	/*
308919b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
309019b58d94SJames Smart 	 * will be recycled by transport release call.
309119b58d94SJames Smart 	 */
309219b58d94SJames Smart 	if (released)
30936c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3094d613b6a7SJames Smart 
30956c621a22SJames Smart 	/* This is the iocbq for the abort, not the command */
3096d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
309786c67379SJames Smart 
309886c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
30997b7f551bSJames Smart 	 * For LPFC_NVME_XBUSY, lpfc_sli4_nvmet_xri_aborted
310086c67379SJames Smart 	 * should have been called already.
310186c67379SJames Smart 	 */
3102d613b6a7SJames Smart }
3103d613b6a7SJames Smart 
3104d613b6a7SJames Smart /**
310586c67379SJames Smart  * lpfc_nvmet_unsol_fcp_abort_cmp - Completion handler for ABTS
3106d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3107d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3108a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
3109d613b6a7SJames Smart  *
3110d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3111d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
3112d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3113d613b6a7SJames Smart  **/
3114d613b6a7SJames Smart static void
311586c67379SJames Smart lpfc_nvmet_unsol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3116a680a929SJames Smart 			       struct lpfc_iocbq *rspwqe)
3117d613b6a7SJames Smart {
31187cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3119d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
312019b58d94SJames Smart 	unsigned long flags;
3121b27cbd55SBart Van Assche 	uint32_t result;
312219b58d94SJames Smart 	bool released = false;
3123a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
3124d613b6a7SJames Smart 
3125d51cf5bdSJames Smart 	ctxp = cmdwqe->context_un.axchg;
3126d613b6a7SJames Smart 	result = wcqe->parameter;
3127d613b6a7SJames Smart 
312886c67379SJames Smart 	if (!ctxp) {
312986c67379SJames Smart 		/* if context is clear, related io alrady complete */
3130d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
313186c67379SJames Smart 				"6070 ABTS cmpl: WCQE: %08x %08x %08x %08x\n",
313286c67379SJames Smart 				wcqe->word0, wcqe->total_data_placed,
3133d613b6a7SJames Smart 				result, wcqe->word3);
313486c67379SJames Smart 		return;
313586c67379SJames Smart 	}
3136d613b6a7SJames Smart 
313778e1d200SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3138c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
31397b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABORT_OP)
314078e1d200SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
314178e1d200SJames Smart 
3142d613b6a7SJames Smart 	/* Sanity check */
31437b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_ABORT) {
3144372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
314586c67379SJames Smart 				"6112 ABTS Wrong state:%d oxid x%x\n",
3146d613b6a7SJames Smart 				ctxp->state, ctxp->oxid);
3147d613b6a7SJames Smart 	}
314886c67379SJames Smart 
314986c67379SJames Smart 	/* Check if we already received a free context call
315086c67379SJames Smart 	 * and we have completed processing an abort situation.
315186c67379SJames Smart 	 */
31527b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DONE;
31537b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_CTX_RLS) &&
31547b7f551bSJames Smart 	    !(ctxp->flag & LPFC_NVME_XBUSY)) {
3155c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
315679d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3157c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
315819b58d94SJames Smart 		released = true;
315986c67379SJames Smart 	}
31607b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
316119b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3162547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
316319b58d94SJames Smart 
316486c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
316579d8c4ceSJames Smart 			"6316 ABTS cmpl oxid x%x flg x%x (%x) "
316686c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
316786c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
316886c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
316986c67379SJames Smart 			result, wcqe->word3);
31706c621a22SJames Smart 
3171d51cf5bdSJames Smart 	cmdwqe->rsp_dmabuf = NULL;
3172d51cf5bdSJames Smart 	cmdwqe->bpl_dmabuf = NULL;
317319b58d94SJames Smart 	/*
317419b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
317519b58d94SJames Smart 	 * will be recycled by transport release call.
317619b58d94SJames Smart 	 */
317719b58d94SJames Smart 	if (released)
31786c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
317986c67379SJames Smart 
318086c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
31817b7f551bSJames Smart 	 * For LPFC_NVME_XBUSY, lpfc_sli4_nvmet_xri_aborted
318286c67379SJames Smart 	 * should have been called already.
318386c67379SJames Smart 	 */
3184d613b6a7SJames Smart }
3185d613b6a7SJames Smart 
3186d613b6a7SJames Smart /**
3187d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
3188d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3189d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3190a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
3191d613b6a7SJames Smart  *
3192d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3193d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for LS cmds
3194d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3195d613b6a7SJames Smart  **/
3196d613b6a7SJames Smart static void
3197d613b6a7SJames Smart lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3198a680a929SJames Smart 			    struct lpfc_iocbq *rspwqe)
3199d613b6a7SJames Smart {
32007cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3201d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3202b27cbd55SBart Van Assche 	uint32_t result;
3203a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
3204d613b6a7SJames Smart 
3205d51cf5bdSJames Smart 	ctxp = cmdwqe->context_un.axchg;
3206d613b6a7SJames Smart 	result = wcqe->parameter;
3207d613b6a7SJames Smart 
3208fcdd14b8SJames Smart 	if (phba->nvmet_support) {
3209d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3210547077a4SJames Smart 		atomic_inc(&tgtp->xmt_ls_abort_cmpl);
3211fcdd14b8SJames Smart 	}
3212d613b6a7SJames Smart 
3213d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
321432350664SJames Smart 			"6083 Abort cmpl: ctx x%px WCQE:%08x %08x %08x %08x\n",
3215d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
3216d613b6a7SJames Smart 			result, wcqe->word3);
3217d613b6a7SJames Smart 
3218ce1b591cSJames Smart 	if (!ctxp) {
3219372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3220ce1b591cSJames Smart 				"6415 NVMET LS Abort No ctx: WCQE: "
3221ce1b591cSJames Smart 				 "%08x %08x %08x %08x\n",
3222ce1b591cSJames Smart 				wcqe->word0, wcqe->total_data_placed,
3223ce1b591cSJames Smart 				result, wcqe->word3);
3224ce1b591cSJames Smart 
3225ce1b591cSJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
3226ce1b591cSJames Smart 		return;
3227ce1b591cSJames Smart 	}
3228ce1b591cSJames Smart 
32297b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_LS_ABORT) {
3230372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3231ce1b591cSJames Smart 				"6416 NVMET LS abort cmpl state mismatch: "
3232ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
3233ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3234ce1b591cSJames Smart 	}
3235ce1b591cSJames Smart 
3236d51cf5bdSJames Smart 	cmdwqe->rsp_dmabuf = NULL;
3237d51cf5bdSJames Smart 	cmdwqe->bpl_dmabuf = NULL;
3238d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3239d613b6a7SJames Smart 	kfree(ctxp);
3240d613b6a7SJames Smart }
3241d613b6a7SJames Smart 
3242d613b6a7SJames Smart static int
3243d613b6a7SJames Smart lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
32447cacae2aSJames Smart 			     struct lpfc_async_xchg_ctx *ctxp,
3245d613b6a7SJames Smart 			     uint32_t sid, uint16_t xri)
3246d613b6a7SJames Smart {
3247fcdd14b8SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = NULL;
3248d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3249205e8240SJames Smart 	union lpfc_wqe128 *wqe_abts;
3250d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3251d613b6a7SJames Smart 
3252d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
325386c67379SJames Smart 			"6067 ABTS: sid %x xri x%x/x%x\n",
3254318083adSJames Smart 			sid, xri, ctxp->wqeq->sli4_xritag);
3255d613b6a7SJames Smart 
3256fcdd14b8SJames Smart 	if (phba->nvmet_support && phba->targetport)
3257d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3258d613b6a7SJames Smart 
3259d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3260307e3380SJames Smart 	if (!ndlp ||
3261d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3262d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3263fcdd14b8SJames Smart 		if (tgtp)
3264d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_abort_rsp_error);
3265372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3266d613b6a7SJames Smart 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
3267b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3268d613b6a7SJames Smart 
3269d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3270d613b6a7SJames Smart 		return 0;
3271d613b6a7SJames Smart 	}
3272d613b6a7SJames Smart 
3273d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
3274d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
3275d613b6a7SJames Smart 
3276d613b6a7SJames Smart 	/*
3277d613b6a7SJames Smart 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
3278d613b6a7SJames Smart 	 * that were initialized in lpfc_sli4_nvmet_alloc.
3279d613b6a7SJames Smart 	 */
3280d613b6a7SJames Smart 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
3281d613b6a7SJames Smart 
3282d613b6a7SJames Smart 	/* Word 5 */
3283d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
3284d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
3285d613b6a7SJames Smart 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
3286d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
3287d613b6a7SJames Smart 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
3288d613b6a7SJames Smart 
3289d613b6a7SJames Smart 	/* Word 6 */
3290d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
3291d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
3292d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
3293d613b6a7SJames Smart 	       abts_wqeq->sli4_xritag);
3294d613b6a7SJames Smart 
3295d613b6a7SJames Smart 	/* Word 7 */
3296d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
3297d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
3298d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
3299d613b6a7SJames Smart 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
3300d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
3301d613b6a7SJames Smart 
3302d613b6a7SJames Smart 	/* Word 8 */
3303d613b6a7SJames Smart 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
3304d613b6a7SJames Smart 
3305d613b6a7SJames Smart 	/* Word 9 */
3306d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
3307d613b6a7SJames Smart 	/* Needs to be set by caller */
3308d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
3309d613b6a7SJames Smart 
3310d613b6a7SJames Smart 	/* Word 10 */
3311d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
3312d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
3313d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
3314d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
3315d613b6a7SJames Smart 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
3316d613b6a7SJames Smart 
3317d613b6a7SJames Smart 	/* Word 11 */
3318d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
3319d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
3320d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
3321d613b6a7SJames Smart 	       OTHER_COMMAND);
3322d613b6a7SJames Smart 
3323d613b6a7SJames Smart 	abts_wqeq->vport = phba->pport;
3324d51cf5bdSJames Smart 	abts_wqeq->ndlp = ndlp;
3325d51cf5bdSJames Smart 	abts_wqeq->context_un.axchg = ctxp;
3326d51cf5bdSJames Smart 	abts_wqeq->bpl_dmabuf = NULL;
3327a680a929SJames Smart 	abts_wqeq->num_bdes = 0;
3328d613b6a7SJames Smart 	/* hba_wqidx should already be setup from command we are aborting */
3329d613b6a7SJames Smart 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
3330d613b6a7SJames Smart 	abts_wqeq->iocb.ulpLe = 1;
3331d613b6a7SJames Smart 
3332d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
3333d613b6a7SJames Smart 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
3334d613b6a7SJames Smart 			xri, abts_wqeq->iotag);
3335d613b6a7SJames Smart 	return 1;
3336d613b6a7SJames Smart }
3337d613b6a7SJames Smart 
3338db7531d2SJames Smart /**
3339db7531d2SJames Smart  * lpfc_nvmet_prep_abort_wqe - set up 'abort' work queue entry.
3340db7531d2SJames Smart  * @pwqeq: Pointer to command iocb.
3341db7531d2SJames Smart  * @xritag: Tag that  uniqely identifies the local exchange resource.
3342db7531d2SJames Smart  * @opt: Option bits -
3343db7531d2SJames Smart  *		bit 0 = inhibit sending abts on the link
3344db7531d2SJames Smart  *
3345db7531d2SJames Smart  * This function is called with hbalock held.
3346db7531d2SJames Smart  **/
3347185d17e1SJames Smart static void
3348db7531d2SJames Smart lpfc_nvmet_prep_abort_wqe(struct lpfc_iocbq *pwqeq, u16 xritag, u8 opt)
3349db7531d2SJames Smart {
3350db7531d2SJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
3351db7531d2SJames Smart 
3352db7531d2SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
3353db7531d2SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
3354db7531d2SJames Smart 	 */
3355db7531d2SJames Smart 	memset(wqe, 0, sizeof(*wqe));
3356db7531d2SJames Smart 
3357db7531d2SJames Smart 	if (opt & INHIBIT_ABORT)
3358db7531d2SJames Smart 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
3359db7531d2SJames Smart 	/* Abort specified xri tag, with the mask deliberately zeroed */
3360db7531d2SJames Smart 	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
3361db7531d2SJames Smart 
3362db7531d2SJames Smart 	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
3363db7531d2SJames Smart 
3364db7531d2SJames Smart 	/* Abort the I/O associated with this outstanding exchange ID. */
3365db7531d2SJames Smart 	wqe->abort_cmd.wqe_com.abort_tag = xritag;
3366db7531d2SJames Smart 
3367db7531d2SJames Smart 	/* iotag for the wqe completion. */
3368db7531d2SJames Smart 	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, pwqeq->iotag);
3369db7531d2SJames Smart 
3370db7531d2SJames Smart 	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
3371db7531d2SJames Smart 	bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
3372db7531d2SJames Smart 
3373db7531d2SJames Smart 	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);
3374db7531d2SJames Smart 	bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);
3375db7531d2SJames Smart 	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
3376db7531d2SJames Smart }
3377db7531d2SJames Smart 
3378d613b6a7SJames Smart static int
3379d613b6a7SJames Smart lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
33807cacae2aSJames Smart 			       struct lpfc_async_xchg_ctx *ctxp,
3381d613b6a7SJames Smart 			       uint32_t sid, uint16_t xri)
3382d613b6a7SJames Smart {
3383d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3384d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3385d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3386d613b6a7SJames Smart 	unsigned long flags;
338751f8e43eSJames Smart 	u8 opt;
3388d613b6a7SJames Smart 	int rc;
3389d613b6a7SJames Smart 
3390d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3391d613b6a7SJames Smart 	if (!ctxp->wqeq) {
33926c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3393d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3394d613b6a7SJames Smart 	}
3395d613b6a7SJames Smart 
3396d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3397307e3380SJames Smart 	if (!ndlp ||
3398d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3399d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3400d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3401372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
340286c67379SJames Smart 				"6160 Drop ABORT - wrong NDLP state x%x.\n",
3403b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3404d613b6a7SJames Smart 
3405d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3406c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
34077b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3408c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3409d613b6a7SJames Smart 		return 0;
3410d613b6a7SJames Smart 	}
3411d613b6a7SJames Smart 
3412d613b6a7SJames Smart 	/* Issue ABTS for this WQE based on iotag */
3413d613b6a7SJames Smart 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
3414c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3415d613b6a7SJames Smart 	if (!ctxp->abort_wqeq) {
3416547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3417372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
341886c67379SJames Smart 				"6161 ABORT failed: No wqeqs: "
3419d613b6a7SJames Smart 				"xri: x%x\n", ctxp->oxid);
3420d613b6a7SJames Smart 		/* No failure to an ABTS request. */
34217b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3422c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3423d613b6a7SJames Smart 		return 0;
3424d613b6a7SJames Smart 	}
3425d613b6a7SJames Smart 	abts_wqeq = ctxp->abort_wqeq;
34267b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_ABORT;
34277b7f551bSJames Smart 	opt = (ctxp->flag & LPFC_NVME_ABTS_RCV) ? INHIBIT_ABORT : 0;
3428c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3429d613b6a7SJames Smart 
3430d613b6a7SJames Smart 	/* Announce entry to new IO submit field. */
343186c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
343286c67379SJames Smart 			"6162 ABORT Request to rport DID x%06x "
3433d613b6a7SJames Smart 			"for xri x%x x%x\n",
3434d613b6a7SJames Smart 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
3435d613b6a7SJames Smart 
3436d613b6a7SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
3437d613b6a7SJames Smart 	 * cleared when the reset is complete and rings reestablished.
3438d613b6a7SJames Smart 	 */
3439d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3440d613b6a7SJames Smart 	/* driver queued commands are in process of being flushed */
3441c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
3442d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3443547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3444372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3445d613b6a7SJames Smart 				"6163 Driver in reset cleanup - flushing "
3446d613b6a7SJames Smart 				"NVME Req now. hba_flag x%x oxid x%x\n",
3447d613b6a7SJames Smart 				phba->hba_flag, ctxp->oxid);
3448d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3449c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
34507b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3451c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3452d613b6a7SJames Smart 		return 0;
3453d613b6a7SJames Smart 	}
3454d613b6a7SJames Smart 
3455d613b6a7SJames Smart 	/* Outstanding abort is in progress */
3456a680a929SJames Smart 	if (abts_wqeq->cmd_flag & LPFC_DRIVER_ABORTED) {
3457d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3458547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3459372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3460d613b6a7SJames Smart 				"6164 Outstanding NVME I/O Abort Request "
3461d613b6a7SJames Smart 				"still pending on oxid x%x\n",
3462d613b6a7SJames Smart 				ctxp->oxid);
3463d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3464c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
34657b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3466c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3467d613b6a7SJames Smart 		return 0;
3468d613b6a7SJames Smart 	}
3469d613b6a7SJames Smart 
3470d613b6a7SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
3471a680a929SJames Smart 	abts_wqeq->cmd_flag |= LPFC_DRIVER_ABORTED;
3472d613b6a7SJames Smart 
3473db7531d2SJames Smart 	lpfc_nvmet_prep_abort_wqe(abts_wqeq, ctxp->wqeq->sli4_xritag, opt);
3474d613b6a7SJames Smart 
3475d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
3476d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
3477a680a929SJames Smart 	abts_wqeq->cmd_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
3478a680a929SJames Smart 	abts_wqeq->cmd_flag |= LPFC_IO_NVME;
3479d51cf5bdSJames Smart 	abts_wqeq->context_un.axchg = ctxp;
34804550f9c7SJames Smart 	abts_wqeq->vport = phba->pport;
34811fbf9742SJames Smart 	if (!ctxp->hdwq)
34821fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
34831fbf9742SJames Smart 
34841fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3485d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3486547077a4SJames Smart 	if (rc == WQE_SUCCESS) {
3487547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_sol);
3488d613b6a7SJames Smart 		return 0;
3489547077a4SJames Smart 	}
3490d613b6a7SJames Smart 
3491547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3492c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
34937b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3494c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3495d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3496372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
349786c67379SJames Smart 			"6166 Failed ABORT issue_wqe with status x%x "
3498d613b6a7SJames Smart 			"for oxid x%x.\n",
3499d613b6a7SJames Smart 			rc, ctxp->oxid);
3500d613b6a7SJames Smart 	return 1;
3501d613b6a7SJames Smart }
3502d613b6a7SJames Smart 
3503d613b6a7SJames Smart static int
3504d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
35057cacae2aSJames Smart 				 struct lpfc_async_xchg_ctx *ctxp,
3506d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
3507d613b6a7SJames Smart {
3508d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3509d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3510d613b6a7SJames Smart 	unsigned long flags;
3511c160c0f8SJames Smart 	bool released = false;
3512d613b6a7SJames Smart 	int rc;
3513d613b6a7SJames Smart 
3514d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3515d613b6a7SJames Smart 	if (!ctxp->wqeq) {
35166c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3517d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3518d613b6a7SJames Smart 	}
3519d613b6a7SJames Smart 
35207b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_FREE) {
3521372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3522ce1b591cSJames Smart 				"6417 NVMET ABORT ctx freed %d %d oxid x%x\n",
3523ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
3524ce1b591cSJames Smart 		rc = WQE_BUSY;
3525ce1b591cSJames Smart 		goto aerr;
3526ce1b591cSJames Smart 	}
35277b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_ABORT;
3528ce1b591cSJames Smart 	ctxp->entry_cnt++;
3529d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
3530d613b6a7SJames Smart 	if (rc == 0)
3531d613b6a7SJames Smart 		goto aerr;
3532d613b6a7SJames Smart 
3533d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3534d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
3535a680a929SJames Smart 	abts_wqeq->cmd_cmpl = lpfc_nvmet_unsol_fcp_abort_cmp;
3536a680a929SJames Smart 	abts_wqeq->cmd_flag |= LPFC_IO_NVMET;
35371fbf9742SJames Smart 	if (!ctxp->hdwq)
35381fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
35391fbf9742SJames Smart 
35401fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3541d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3542d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3543d613b6a7SJames Smart 		return 0;
3544d613b6a7SJames Smart 	}
3545d613b6a7SJames Smart 
3546d613b6a7SJames Smart aerr:
354768c9b55dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
35487b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_CTX_RLS) {
3549c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
355079d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3551c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
3552c160c0f8SJames Smart 		released = true;
3553c160c0f8SJames Smart 	}
35547b7f551bSJames Smart 	ctxp->flag &= ~(LPFC_NVME_ABORT_OP | LPFC_NVME_CTX_RLS);
355568c9b55dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
355668c9b55dSJames Smart 
3557d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3558372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
355979d8c4ceSJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x "
356079d8c4ceSJames Smart 			"(%x)\n",
356179d8c4ceSJames Smart 			ctxp->oxid, rc, released);
3562c160c0f8SJames Smart 	if (released)
356368c9b55dSJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3564d613b6a7SJames Smart 	return 1;
3565d613b6a7SJames Smart }
3566d613b6a7SJames Smart 
35673a8070c5SJames Smart /**
35683a8070c5SJames Smart  * lpfc_nvme_unsol_ls_issue_abort - issue ABTS on an exchange received
35693a8070c5SJames Smart  *        via async frame receive where the frame is not handled.
35703a8070c5SJames Smart  * @phba: pointer to adapter structure
35713a8070c5SJames Smart  * @ctxp: pointer to the asynchronously received received sequence
35723a8070c5SJames Smart  * @sid: address of the remote port to send the ABTS to
35733a8070c5SJames Smart  * @xri: oxid value to for the ABTS (other side's exchange id).
35743a8070c5SJames Smart  **/
35753a8070c5SJames Smart int
35763a8070c5SJames Smart lpfc_nvme_unsol_ls_issue_abort(struct lpfc_hba *phba,
35777cacae2aSJames Smart 				struct lpfc_async_xchg_ctx *ctxp,
3578d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
3579d613b6a7SJames Smart {
3580fcdd14b8SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = NULL;
3581d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3582d613b6a7SJames Smart 	unsigned long flags;
3583d613b6a7SJames Smart 	int rc;
3584d613b6a7SJames Smart 
35857b7f551bSJames Smart 	if ((ctxp->state == LPFC_NVME_STE_LS_RCV && ctxp->entry_cnt == 1) ||
35867b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_LS_RSP && ctxp->entry_cnt == 2)) {
35877b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_LS_ABORT;
3588ce1b591cSJames Smart 		ctxp->entry_cnt++;
3589ce1b591cSJames Smart 	} else {
3590372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3591ce1b591cSJames Smart 				"6418 NVMET LS abort state mismatch "
3592ce1b591cSJames Smart 				"IO x%x: %d %d\n",
3593ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
35947b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_LS_ABORT;
3595ce1b591cSJames Smart 	}
3596ce1b591cSJames Smart 
3597fcdd14b8SJames Smart 	if (phba->nvmet_support && phba->targetport)
3598d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3599fcdd14b8SJames Smart 
3600d613b6a7SJames Smart 	if (!ctxp->wqeq) {
3601d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
3602d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
3603d613b6a7SJames Smart 		if (!ctxp->wqeq) {
3604372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3605d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
3606d613b6a7SJames Smart 					"xri: x%x\n", xri);
3607d613b6a7SJames Smart 			/* No failure to an ABTS request. */
3608d613b6a7SJames Smart 			kfree(ctxp);
3609d613b6a7SJames Smart 			return 0;
3610d613b6a7SJames Smart 		}
3611d613b6a7SJames Smart 	}
3612d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
36136c621a22SJames Smart 
3614ce1b591cSJames Smart 	if (lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri) == 0) {
3615ce1b591cSJames Smart 		rc = WQE_BUSY;
3616ce1b591cSJames Smart 		goto out;
3617ce1b591cSJames Smart 	}
3618d613b6a7SJames Smart 
3619d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3620a680a929SJames Smart 	abts_wqeq->cmd_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
3621a680a929SJames Smart 	abts_wqeq->cmd_flag |=  LPFC_IO_NVME_LS;
36221fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3623d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3624d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3625fcdd14b8SJames Smart 		if (tgtp)
3626547077a4SJames Smart 			atomic_inc(&tgtp->xmt_abort_unsol);
3627d613b6a7SJames Smart 		return 0;
3628d613b6a7SJames Smart 	}
3629ce1b591cSJames Smart out:
3630fcdd14b8SJames Smart 	if (tgtp)
3631d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3632d51cf5bdSJames Smart 	abts_wqeq->rsp_dmabuf = NULL;
3633d51cf5bdSJames Smart 	abts_wqeq->bpl_dmabuf = NULL;
3634d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3635372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3636d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
36374e57e0b9SJames Smart 	return 1;
3638d613b6a7SJames Smart }
36394c2805aaSJames Smart 
36404c2805aaSJames Smart /**
36414c2805aaSJames Smart  * lpfc_nvmet_invalidate_host
36424c2805aaSJames Smart  *
3643d3cb79fcSLee Jones  * @phba: pointer to the driver instance bound to an adapter port.
3644d3cb79fcSLee Jones  * @ndlp: pointer to an lpfc_nodelist type
36454c2805aaSJames Smart  *
36464c2805aaSJames Smart  * This routine upcalls the nvmet transport to invalidate an NVME
36474c2805aaSJames Smart  * host to which this target instance had active connections.
36484c2805aaSJames Smart  */
36494c2805aaSJames Smart void
36504c2805aaSJames Smart lpfc_nvmet_invalidate_host(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
36514c2805aaSJames Smart {
3652243156c0SJames Smart 	u32 ndlp_has_hh;
36534c2805aaSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
36544c2805aaSJames Smart 
3655243156c0SJames Smart 	lpfc_printf_log(phba, KERN_INFO,
3656243156c0SJames Smart 			LOG_NVME | LOG_NVME_ABTS | LOG_NVME_DISC,
36574c2805aaSJames Smart 			"6203 Invalidating hosthandle x%px\n",
36584c2805aaSJames Smart 			ndlp);
36594c2805aaSJames Smart 
36604c2805aaSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
36614c2805aaSJames Smart 	atomic_set(&tgtp->state, LPFC_NVMET_INV_HOST_ACTIVE);
36624c2805aaSJames Smart 
3663243156c0SJames Smart 	spin_lock_irq(&ndlp->lock);
3664243156c0SJames Smart 	ndlp_has_hh = ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH;
3665243156c0SJames Smart 	spin_unlock_irq(&ndlp->lock);
3666243156c0SJames Smart 
3667243156c0SJames Smart 	/* Do not invalidate any nodes that do not have a hosthandle.
3668243156c0SJames Smart 	 * The host_release callbk will cause a node reference
3669243156c0SJames Smart 	 * count imbalance and a crash.
3670243156c0SJames Smart 	 */
3671243156c0SJames Smart 	if (!ndlp_has_hh) {
3672243156c0SJames Smart 		lpfc_printf_log(phba, KERN_INFO,
3673243156c0SJames Smart 				LOG_NVME | LOG_NVME_ABTS | LOG_NVME_DISC,
3674243156c0SJames Smart 				"6204 Skip invalidate on node x%px DID x%x\n",
3675243156c0SJames Smart 				ndlp, ndlp->nlp_DID);
3676243156c0SJames Smart 		return;
3677243156c0SJames Smart 	}
3678243156c0SJames Smart 
36794c2805aaSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
36804c2805aaSJames Smart 	/* Need to get the nvmet_fc_target_port pointer here.*/
36814c2805aaSJames Smart 	nvmet_fc_invalidate_host(phba->targetport, ndlp);
36824c2805aaSJames Smart #endif
36834c2805aaSJames Smart }
3684