xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision f45775bf)
1d613b6a7SJames Smart /*******************************************************************
2d613b6a7SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
367073c69SJames Smart  * Fibre Channel Host Bus Adapters.                                *
4*f45775bfSJames 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 {
298fe1bedecSJames Smart 	struct lpfc_async_xchg_ctx *axchg = cmdwqe->context2;
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 
320fe1bedecSJames Smart 	lpfc_nlp_put(cmdwqe->context1);
321fe1bedecSJames Smart 	cmdwqe->context2 = NULL;
322fe1bedecSJames Smart 	cmdwqe->context3 = 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;
725e3246a12SJames Smart 	uint32_t status, result, op, start_clean, 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 
731d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
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++;
823a680a929SJames Smart 		start_clean = offsetof(struct lpfc_iocbq, cmd_flag);
824d613b6a7SJames Smart 		memset(((char *)cmdwqe) + start_clean, 0,
825d613b6a7SJames Smart 		       (sizeof(struct lpfc_iocbq) - start_clean));
8262b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
827c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
8282b65e182SJames Smart 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
8292b65e182SJames Smart 			ctxp->ts_data_nvme = ktime_get_ns();
8302b65e182SJames Smart 		}
8312b65e182SJames Smart #endif
832d613b6a7SJames Smart 		rsp->done(rsp);
833d613b6a7SJames Smart 	}
83463df6d63SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
835840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
836d6d189ceSBart Van Assche 		id = raw_smp_processor_id();
837840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
83863df6d63SJames Smart 		if (ctxp->cpu != id)
83963df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
84063df6d63SJames Smart 					"6704 CPU Check cmdcmpl: "
84163df6d63SJames Smart 					"cpu %d expect %d\n",
84263df6d63SJames Smart 					id, ctxp->cpu);
84363df6d63SJames Smart 	}
84463df6d63SJames Smart #endif
845d613b6a7SJames Smart }
846d613b6a7SJames Smart 
847fe1bedecSJames Smart /**
848fe1bedecSJames Smart  * __lpfc_nvme_xmt_ls_rsp - Generic service routine to issue transmit
849fe1bedecSJames Smart  *         an NVME LS rsp for a prior NVME LS request that was received.
850fe1bedecSJames Smart  * @axchg: pointer to exchange context for the NVME LS request the response
851fe1bedecSJames Smart  *         is for.
852fe1bedecSJames Smart  * @ls_rsp: pointer to the transport LS RSP that is to be sent
853fe1bedecSJames Smart  * @xmt_ls_rsp_cmp: completion routine to call upon RSP transmit done
854fe1bedecSJames Smart  *
855fe1bedecSJames Smart  * This routine is used to format and send a WQE to transmit a NVME LS
856fe1bedecSJames Smart  * Response.  The response is for a prior NVME LS request that was
857fe1bedecSJames Smart  * received and posted to the transport.
858fe1bedecSJames Smart  *
859fe1bedecSJames Smart  * Returns:
860fe1bedecSJames Smart  *  0 : if response successfully transmit
861fe1bedecSJames Smart  *  non-zero : if response failed to transmit, of the form -Exxx.
862fe1bedecSJames Smart  **/
863fe1bedecSJames Smart int
864fe1bedecSJames Smart __lpfc_nvme_xmt_ls_rsp(struct lpfc_async_xchg_ctx *axchg,
865fe1bedecSJames Smart 			struct nvmefc_ls_rsp *ls_rsp,
866fe1bedecSJames Smart 			void (*xmt_ls_rsp_cmp)(struct lpfc_hba *phba,
867fe1bedecSJames Smart 				struct lpfc_iocbq *cmdwqe,
868a680a929SJames Smart 				struct lpfc_iocbq *rspwqe))
869d613b6a7SJames Smart {
870fe1bedecSJames Smart 	struct lpfc_hba *phba = axchg->phba;
871fe1bedecSJames Smart 	struct hbq_dmabuf *nvmebuf = (struct hbq_dmabuf *)axchg->rqb_buffer;
872d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
873d613b6a7SJames Smart 	struct lpfc_dmabuf dmabuf;
874d613b6a7SJames Smart 	struct ulp_bde64 bpl;
875d613b6a7SJames Smart 	int rc;
876d613b6a7SJames Smart 
8771abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
8781abcb371SDick Kennedy 		return -ENODEV;
8791abcb371SDick Kennedy 
880d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
881fe1bedecSJames Smart 			"6023 NVMEx LS rsp oxid x%x\n", axchg->oxid);
882ce1b591cSJames Smart 
883fe1bedecSJames Smart 	if (axchg->state != LPFC_NVME_STE_LS_RCV || axchg->entry_cnt != 1) {
884372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
885fe1bedecSJames Smart 				"6412 NVMEx LS rsp state mismatch "
886ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
887fe1bedecSJames Smart 				axchg->oxid, axchg->state, axchg->entry_cnt);
888fe1bedecSJames Smart 		return -EALREADY;
889ce1b591cSJames Smart 	}
890fe1bedecSJames Smart 	axchg->state = LPFC_NVME_STE_LS_RSP;
891fe1bedecSJames Smart 	axchg->entry_cnt++;
892d613b6a7SJames Smart 
893fe1bedecSJames Smart 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, axchg, ls_rsp->rspdma,
894fe1bedecSJames Smart 					 ls_rsp->rsplen);
895d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
896372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
897fe1bedecSJames Smart 				"6150 NVMEx LS Drop Rsp x%x: Prep\n",
898fe1bedecSJames Smart 				axchg->oxid);
899fe1bedecSJames Smart 		rc = -ENOMEM;
900fe1bedecSJames Smart 		goto out_free_buf;
901d613b6a7SJames Smart 	}
902d613b6a7SJames Smart 
903d613b6a7SJames Smart 	/* Save numBdes for bpl2sgl */
904a680a929SJames Smart 	nvmewqeq->num_bdes = 1;
905d613b6a7SJames Smart 	nvmewqeq->hba_wqidx = 0;
906d613b6a7SJames Smart 	nvmewqeq->context3 = &dmabuf;
907d613b6a7SJames Smart 	dmabuf.virt = &bpl;
908d613b6a7SJames Smart 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
909d613b6a7SJames Smart 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
910fe1bedecSJames Smart 	bpl.tus.f.bdeSize = ls_rsp->rsplen;
911d613b6a7SJames Smart 	bpl.tus.f.bdeFlags = 0;
912d613b6a7SJames Smart 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
913fe1bedecSJames Smart 	/*
914fe1bedecSJames Smart 	 * Note: although we're using stack space for the dmabuf, the
915fe1bedecSJames Smart 	 * call to lpfc_sli4_issue_wqe is synchronous, so it will not
916fe1bedecSJames Smart 	 * be referenced after it returns back to this routine.
917fe1bedecSJames Smart 	 */
918d613b6a7SJames Smart 
919a680a929SJames Smart 	nvmewqeq->cmd_cmpl = xmt_ls_rsp_cmp;
920fe1bedecSJames Smart 	nvmewqeq->context2 = axchg;
921d613b6a7SJames Smart 
922fe1bedecSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS RSP: xri x%x wqidx x%x len x%x\n",
923fe1bedecSJames Smart 			 axchg->oxid, nvmewqeq->hba_wqidx, ls_rsp->rsplen);
9242b65e182SJames Smart 
925fe1bedecSJames Smart 	rc = lpfc_sli4_issue_wqe(phba, axchg->hdwq, nvmewqeq);
926fe1bedecSJames Smart 
927fe1bedecSJames Smart 	/* clear to be sure there's no reference */
928fe1bedecSJames Smart 	nvmewqeq->context3 = NULL;
929fe1bedecSJames Smart 
930d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
931d613b6a7SJames Smart 		/*
932d613b6a7SJames Smart 		 * Okay to repost buffer here, but wait till cmpl
933d613b6a7SJames Smart 		 * before freeing ctxp and iocbq.
934d613b6a7SJames Smart 		 */
935d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
936d613b6a7SJames Smart 		return 0;
937d613b6a7SJames Smart 	}
938fe1bedecSJames Smart 
939372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
940fe1bedecSJames Smart 			"6151 NVMEx LS RSP x%x: failed to transmit %d\n",
941fe1bedecSJames Smart 			axchg->oxid, rc);
942fe1bedecSJames Smart 
943fe1bedecSJames Smart 	rc = -ENXIO;
944d613b6a7SJames Smart 
945d613b6a7SJames Smart 	lpfc_nlp_put(nvmewqeq->context1);
946d613b6a7SJames Smart 
947fe1bedecSJames Smart out_free_buf:
948fe1bedecSJames Smart 	/* Give back resources */
949d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
950fe1bedecSJames Smart 
951fe1bedecSJames Smart 	/*
952fe1bedecSJames Smart 	 * As transport doesn't track completions of responses, if the rsp
953fe1bedecSJames Smart 	 * fails to send, the transport will effectively ignore the rsp
954fe1bedecSJames Smart 	 * and consider the LS done. However, the driver has an active
955fe1bedecSJames Smart 	 * exchange open for the LS - so be sure to abort the exchange
956fe1bedecSJames Smart 	 * if the response isn't sent.
957fe1bedecSJames Smart 	 */
958fe1bedecSJames Smart 	lpfc_nvme_unsol_ls_issue_abort(phba, axchg, axchg->sid, axchg->oxid);
959fe1bedecSJames Smart 	return rc;
960fe1bedecSJames Smart }
961fe1bedecSJames Smart 
962fe1bedecSJames Smart /**
963fe1bedecSJames Smart  * lpfc_nvmet_xmt_ls_rsp - Transmit NVME LS response
964fe1bedecSJames Smart  * @tgtport: pointer to target port that NVME LS is to be transmit from.
965fe1bedecSJames Smart  * @ls_rsp: pointer to the transport LS RSP that is to be sent
966fe1bedecSJames Smart  *
967fe1bedecSJames Smart  * Driver registers this routine to transmit responses for received NVME
968fe1bedecSJames Smart  * LS requests.
969fe1bedecSJames Smart  *
970fe1bedecSJames Smart  * This routine is used to format and send a WQE to transmit a NVME LS
971fe1bedecSJames Smart  * Response. The ls_rsp is used to reverse-map the LS to the original
972fe1bedecSJames Smart  * NVME LS request sequence, which provides addressing information for
973fe1bedecSJames Smart  * the remote port the LS to be sent to, as well as the exchange id
974fe1bedecSJames Smart  * that is the LS is bound to.
975fe1bedecSJames Smart  *
976fe1bedecSJames Smart  * Returns:
977fe1bedecSJames Smart  *  0 : if response successfully transmit
978fe1bedecSJames Smart  *  non-zero : if response failed to transmit, of the form -Exxx.
979fe1bedecSJames Smart  **/
980fe1bedecSJames Smart static int
981fe1bedecSJames Smart lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
982fe1bedecSJames Smart 		      struct nvmefc_ls_rsp *ls_rsp)
983fe1bedecSJames Smart {
984fe1bedecSJames Smart 	struct lpfc_async_xchg_ctx *axchg =
985fe1bedecSJames Smart 		container_of(ls_rsp, struct lpfc_async_xchg_ctx, ls_rsp);
986fe1bedecSJames Smart 	struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
987fe1bedecSJames Smart 	int rc;
988fe1bedecSJames Smart 
989fe1bedecSJames Smart 	if (axchg->phba->pport->load_flag & FC_UNLOADING)
990fe1bedecSJames Smart 		return -ENODEV;
991fe1bedecSJames Smart 
992fe1bedecSJames Smart 	rc = __lpfc_nvme_xmt_ls_rsp(axchg, ls_rsp, lpfc_nvmet_xmt_ls_rsp_cmp);
993fe1bedecSJames Smart 
994fe1bedecSJames Smart 	if (rc) {
995fe1bedecSJames Smart 		atomic_inc(&nvmep->xmt_ls_drop);
996fe1bedecSJames Smart 		/*
997fe1bedecSJames Smart 		 * unless the failure is due to having already sent
998fe1bedecSJames Smart 		 * the response, an abort will be generated for the
999fe1bedecSJames Smart 		 * exchange if the rsp can't be sent.
1000fe1bedecSJames Smart 		 */
1001fe1bedecSJames Smart 		if (rc != -EALREADY)
1002547077a4SJames Smart 			atomic_inc(&nvmep->xmt_ls_abort);
1003fe1bedecSJames Smart 		return rc;
1004fe1bedecSJames Smart 	}
1005fe1bedecSJames Smart 
1006fe1bedecSJames Smart 	atomic_inc(&nvmep->xmt_ls_rsp);
1007fe1bedecSJames Smart 	return 0;
1008d613b6a7SJames Smart }
1009d613b6a7SJames Smart 
1010d613b6a7SJames Smart static int
1011d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
1012d613b6a7SJames Smart 		      struct nvmefc_tgt_fcp_req *rsp)
1013d613b6a7SJames Smart {
1014d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
10157cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
10167cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
1017d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
10186e8e1c14SJames Smart 	struct lpfc_queue *wq;
1019d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
10206e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
10216e8e1c14SJames Smart 	unsigned long iflags;
1022223b78eaSArnd Bergmann 	int rc;
1023840eda96SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1024840eda96SJames Smart 	int id;
1025840eda96SJames Smart #endif
10262b65e182SJames Smart 
10271abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING) {
10281abcb371SDick Kennedy 		rc = -ENODEV;
10291abcb371SDick Kennedy 		goto aerr;
10301abcb371SDick Kennedy 	}
10311abcb371SDick Kennedy 
10322b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1033c8a4ce0bSDick Kennedy 	if (ctxp->ts_cmd_nvme) {
10342b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
10352b65e182SJames Smart 			ctxp->ts_nvme_status = ktime_get_ns();
10362b65e182SJames Smart 		else
10372b65e182SJames Smart 			ctxp->ts_nvme_data = ktime_get_ns();
10382b65e182SJames Smart 	}
10391fbf9742SJames Smart 
10401fbf9742SJames Smart 	/* Setup the hdw queue if not already set */
10411fbf9742SJames Smart 	if (!ctxp->hdwq)
10421fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[rsp->hwqid];
10431fbf9742SJames Smart 
1044840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
1045840eda96SJames Smart 		id = raw_smp_processor_id();
1046840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
104763df6d63SJames Smart 		if (rsp->hwqid != id)
104863df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
10492b65e182SJames Smart 					"6705 CPU Check OP: "
10502b65e182SJames Smart 					"cpu %d expect %d\n",
10512b65e182SJames Smart 					id, rsp->hwqid);
105263df6d63SJames Smart 		ctxp->cpu = id; /* Setup cpu for cmpl check */
10532b65e182SJames Smart 	}
10542b65e182SJames Smart #endif
1055d613b6a7SJames Smart 
1056d613b6a7SJames Smart 	/* Sanity check */
10577b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_ABTS_RCV) ||
10587b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_ABORT)) {
1059d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1060372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
106179d8c4ceSJames Smart 				"6102 IO oxid x%x aborted\n",
1062d613b6a7SJames Smart 				ctxp->oxid);
1063a5068b46SJames Smart 		rc = -ENXIO;
1064d613b6a7SJames Smart 		goto aerr;
1065d613b6a7SJames Smart 	}
1066d613b6a7SJames Smart 
1067d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
1068d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
1069d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1070372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1071d613b6a7SJames Smart 				"6152 FCP Drop IO x%x: Prep\n",
1072d613b6a7SJames Smart 				ctxp->oxid);
1073a5068b46SJames Smart 		rc = -ENXIO;
1074d613b6a7SJames Smart 		goto aerr;
1075d613b6a7SJames Smart 	}
1076d613b6a7SJames Smart 
1077a680a929SJames Smart 	nvmewqeq->cmd_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
1078d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
1079a680a929SJames Smart 	nvmewqeq->cmd_flag |=  LPFC_IO_NVMET;
1080d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
1081d613b6a7SJames Smart 
10822b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
10832b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
10842b65e182SJames Smart 
10857b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_IO_INP;
10861fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
1087d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
10882b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1089c8a4ce0bSDick Kennedy 		if (!ctxp->ts_cmd_nvme)
10902b65e182SJames Smart 			return 0;
10912b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
10922b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
10932b65e182SJames Smart 		else
10942b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
10952b65e182SJames Smart #endif
1096d613b6a7SJames Smart 		return 0;
1097d613b6a7SJames Smart 	}
1098d613b6a7SJames Smart 
10996e8e1c14SJames Smart 	if (rc == -EBUSY) {
11006e8e1c14SJames Smart 		/*
11016e8e1c14SJames Smart 		 * WQ was full, so queue nvmewqeq to be sent after
11026e8e1c14SJames Smart 		 * WQE release CQE
11036e8e1c14SJames Smart 		 */
11047b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_DEFER_WQFULL;
1105c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
11066e8e1c14SJames Smart 		pring = wq->pring;
11076e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
11086e8e1c14SJames Smart 		list_add_tail(&nvmewqeq->list, &wq->wqfull_list);
11096e8e1c14SJames Smart 		wq->q_flag |= HBA_NVMET_WQFULL;
11106e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
1111411de511SJames Smart 		atomic_inc(&lpfc_nvmep->defer_wqfull);
11126e8e1c14SJames Smart 		return 0;
11136e8e1c14SJames Smart 	}
11146e8e1c14SJames Smart 
1115d613b6a7SJames Smart 	/* Give back resources */
1116d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1117372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1118d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
1119d613b6a7SJames Smart 			ctxp->oxid, rc);
1120d613b6a7SJames Smart 
1121d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
1122d613b6a7SJames Smart 	nvmewqeq->context2 = NULL;
1123d613b6a7SJames Smart 	nvmewqeq->context3 = NULL;
1124a5068b46SJames Smart 	rc = -EBUSY;
1125d613b6a7SJames Smart aerr:
1126a5068b46SJames Smart 	return rc;
1127d613b6a7SJames Smart }
1128d613b6a7SJames Smart 
1129d613b6a7SJames Smart static void
1130d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
1131d613b6a7SJames Smart {
1132d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
1133d613b6a7SJames Smart 
1134d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
1135c41f5988SEwan D. Milne 	if (tport->phba->targetport)
1136c41f5988SEwan D. Milne 		complete(tport->tport_unreg_cmp);
1137d613b6a7SJames Smart }
1138d613b6a7SJames Smart 
113919b58d94SJames Smart static void
1140a97ec51bSJames Smart lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
1141a97ec51bSJames Smart 			 struct nvmefc_tgt_fcp_req *req)
1142a97ec51bSJames Smart {
1143a97ec51bSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
11447cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
11457cacae2aSJames Smart 		container_of(req, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
1146a97ec51bSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
11476e8e1c14SJames Smart 	struct lpfc_queue *wq;
114886c67379SJames Smart 	unsigned long flags;
1149a97ec51bSJames Smart 
11501abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
11511abcb371SDick Kennedy 		return;
11521abcb371SDick Kennedy 
11531fbf9742SJames Smart 	if (!ctxp->hdwq)
11541fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[0];
11551fbf9742SJames Smart 
1156a97ec51bSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
115779d8c4ceSJames Smart 			"6103 NVMET Abort op: oxid x%x flg x%x ste %d\n",
1158ce1b591cSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
1159a97ec51bSJames Smart 
1160ce1b591cSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x flg x%x ste x%x\n",
1161ce1b591cSJames Smart 			 ctxp->oxid, ctxp->flag, ctxp->state);
1162a97ec51bSJames Smart 
1163a97ec51bSJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
1164ce1b591cSJames Smart 
116586c67379SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
116686c67379SJames Smart 
116786c67379SJames Smart 	/* Since iaab/iaar are NOT set, we need to check
116886c67379SJames Smart 	 * if the firmware is in process of aborting IO
116986c67379SJames Smart 	 */
11707b7f551bSJames Smart 	if (ctxp->flag & (LPFC_NVME_XBUSY | LPFC_NVME_ABORT_OP)) {
117186c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
117286c67379SJames Smart 		return;
117386c67379SJames Smart 	}
11747b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_ABORT_OP;
1175ce1b591cSJames Smart 
11767b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_DEFER_WQFULL) {
1177c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11786e8e1c14SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
11796e8e1c14SJames Smart 						 ctxp->oxid);
1180c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
11816e8e1c14SJames Smart 		lpfc_nvmet_wqfull_flush(phba, wq, ctxp);
11826e8e1c14SJames Smart 		return;
11836e8e1c14SJames Smart 	}
1184c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11856e8e1c14SJames Smart 
11867b7f551bSJames Smart 	/* A state of LPFC_NVME_STE_RCV means we have just received
1187ce1b591cSJames Smart 	 * the NVME command and have not started processing it.
1188ce1b591cSJames Smart 	 * (by issuing any IO WQEs on this exchange yet)
1189ce1b591cSJames Smart 	 */
11907b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_RCV)
1191ce1b591cSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1192a97ec51bSJames Smart 						 ctxp->oxid);
1193a97ec51bSJames Smart 	else
1194ce1b591cSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1195a97ec51bSJames Smart 					       ctxp->oxid);
1196a97ec51bSJames Smart }
1197a97ec51bSJames Smart 
1198a97ec51bSJames Smart static void
119919b58d94SJames Smart lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
120019b58d94SJames Smart 			   struct nvmefc_tgt_fcp_req *rsp)
120119b58d94SJames Smart {
1202547077a4SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
12037cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
12047cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
120519b58d94SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
120619b58d94SJames Smart 	unsigned long flags;
120719b58d94SJames Smart 	bool aborting = false;
120819b58d94SJames Smart 
1209c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
12107b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_XBUSY)
1211c160c0f8SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1212c160c0f8SJames Smart 				"6027 NVMET release with XBUSY flag x%x"
1213c160c0f8SJames Smart 				" oxid x%x\n",
1214c160c0f8SJames Smart 				ctxp->flag, ctxp->oxid);
12157b7f551bSJames Smart 	else if (ctxp->state != LPFC_NVME_STE_DONE &&
12167b7f551bSJames Smart 		 ctxp->state != LPFC_NVME_STE_ABORT)
1217372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1218ce1b591cSJames Smart 				"6413 NVMET release bad state %d %d oxid x%x\n",
1219ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
1220ce1b591cSJames Smart 
12217b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_ABORT_OP) ||
12227b7f551bSJames Smart 	    (ctxp->flag & LPFC_NVME_XBUSY)) {
122319b58d94SJames Smart 		aborting = true;
122486c67379SJames Smart 		/* let the abort path do the real release */
122586c67379SJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
122619b58d94SJames Smart 	}
122719b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
122819b58d94SJames Smart 
12299094367aSArnd Bergmann 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d abt %d\n", ctxp->oxid,
12309094367aSArnd Bergmann 			 ctxp->state, aborting);
123119b58d94SJames Smart 
1232547077a4SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_release);
12337b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_TNOTIFY;
1234547077a4SJames Smart 
123586c67379SJames Smart 	if (aborting)
123686c67379SJames Smart 		return;
123786c67379SJames Smart 
12386c621a22SJames Smart 	lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
123919b58d94SJames Smart }
124019b58d94SJames Smart 
124150738420SJames Smart static void
124250738420SJames Smart lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port *tgtport,
124350738420SJames Smart 		     struct nvmefc_tgt_fcp_req *rsp)
124450738420SJames Smart {
124550738420SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
12467cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
12477cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
124850738420SJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
124950738420SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
1250472e146dSJames Smart 	unsigned long iflag;
1251472e146dSJames Smart 
125250738420SJames Smart 
125350738420SJames Smart 	lpfc_nvmeio_data(phba, "NVMET DEFERRCV: xri x%x sz %d CPU %02x\n",
1254d6d189ceSBart Van Assche 			 ctxp->oxid, ctxp->size, raw_smp_processor_id());
125550738420SJames Smart 
12566871e814SJames Smart 	if (!nvmebuf) {
12576871e814SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
125879d8c4ceSJames Smart 				"6425 Defer rcv: no buffer oxid x%x: "
12596871e814SJames Smart 				"flg %x ste %x\n",
12606871e814SJames Smart 				ctxp->oxid, ctxp->flag, ctxp->state);
12616871e814SJames Smart 		return;
12626871e814SJames Smart 	}
12636871e814SJames Smart 
126450738420SJames Smart 	tgtp = phba->targetport->private;
12656871e814SJames Smart 	if (tgtp)
126650738420SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_defer);
1267411de511SJames Smart 
1268411de511SJames Smart 	/* Free the nvmebuf since a new buffer already replaced it */
1269cbc5de1bSJames Smart 	nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
1270472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflag);
1271472e146dSJames Smart 	ctxp->rqb_buffer = NULL;
1272472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
127350738420SJames Smart }
127450738420SJames Smart 
127554840bedSJames Smart /**
127654840bedSJames Smart  * lpfc_nvmet_ls_req_cmp - completion handler for a nvme ls request
127754840bedSJames Smart  * @phba: Pointer to HBA context object
127854840bedSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1279a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
128054840bedSJames Smart  *
128154840bedSJames Smart  * This function is the completion handler for NVME LS requests.
128254840bedSJames Smart  * The function updates any states and statistics, then calls the
128354840bedSJames Smart  * generic completion handler to finish completion of the request.
128454840bedSJames Smart  **/
128554840bedSJames Smart static void
128654840bedSJames Smart lpfc_nvmet_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1287a680a929SJames Smart 		      struct lpfc_iocbq *rspwqe)
128854840bedSJames Smart {
1289a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
129054840bedSJames Smart 	__lpfc_nvme_ls_req_cmp(phba, cmdwqe->vport, cmdwqe, wcqe);
129154840bedSJames Smart }
129254840bedSJames Smart 
129354840bedSJames Smart /**
129454840bedSJames Smart  * lpfc_nvmet_ls_req - Issue an Link Service request
1295d3cb79fcSLee Jones  * @targetport: pointer to target instance registered with nvmet transport.
1296d3cb79fcSLee Jones  * @hosthandle: hosthandle set by the driver in a prior ls_rqst_rcv.
129754840bedSJames Smart  *               Driver sets this value to the ndlp pointer.
1298d3cb79fcSLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
129954840bedSJames Smart  *
130054840bedSJames Smart  * Driver registers this routine to handle any link service request
130154840bedSJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
130254840bedSJames Smart  *
130354840bedSJames Smart  * Return value :
130454840bedSJames Smart  *   0 - Success
130554840bedSJames Smart  *   non-zero: various error codes, in form of -Exxx
130654840bedSJames Smart  **/
130754840bedSJames Smart static int
130854840bedSJames Smart lpfc_nvmet_ls_req(struct nvmet_fc_target_port *targetport,
130954840bedSJames Smart 		  void *hosthandle,
131054840bedSJames Smart 		  struct nvmefc_ls_req *pnvme_lsreq)
131154840bedSJames Smart {
131254840bedSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmet = targetport->private;
131354840bedSJames Smart 	struct lpfc_hba *phba;
131454840bedSJames Smart 	struct lpfc_nodelist *ndlp;
131554840bedSJames Smart 	int ret;
131654840bedSJames Smart 	u32 hstate;
131754840bedSJames Smart 
131854840bedSJames Smart 	if (!lpfc_nvmet)
131954840bedSJames Smart 		return -EINVAL;
132054840bedSJames Smart 
132154840bedSJames Smart 	phba = lpfc_nvmet->phba;
132254840bedSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
132354840bedSJames Smart 		return -EINVAL;
132454840bedSJames Smart 
132554840bedSJames Smart 	hstate = atomic_read(&lpfc_nvmet->state);
132654840bedSJames Smart 	if (hstate == LPFC_NVMET_INV_HOST_ACTIVE)
132754840bedSJames Smart 		return -EACCES;
132854840bedSJames Smart 
132954840bedSJames Smart 	ndlp = (struct lpfc_nodelist *)hosthandle;
133054840bedSJames Smart 
133154840bedSJames Smart 	ret = __lpfc_nvme_ls_req(phba->pport, ndlp, pnvme_lsreq,
133254840bedSJames Smart 				 lpfc_nvmet_ls_req_cmp);
133354840bedSJames Smart 
133454840bedSJames Smart 	return ret;
133554840bedSJames Smart }
133654840bedSJames Smart 
133754840bedSJames Smart /**
133854840bedSJames Smart  * lpfc_nvmet_ls_abort - Abort a prior NVME LS request
133954840bedSJames Smart  * @targetport: Transport targetport, that LS was issued from.
1340d3cb79fcSLee Jones  * @hosthandle: hosthandle set by the driver in a prior ls_rqst_rcv.
134154840bedSJames Smart  *               Driver sets this value to the ndlp pointer.
1342d3cb79fcSLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for LS to be aborted
134354840bedSJames Smart  *
134454840bedSJames Smart  * Driver registers this routine to abort an NVME LS request that is
134554840bedSJames Smart  * in progress (from the transports perspective).
134654840bedSJames Smart  **/
134754840bedSJames Smart static void
134854840bedSJames Smart lpfc_nvmet_ls_abort(struct nvmet_fc_target_port *targetport,
134954840bedSJames Smart 		    void *hosthandle,
135054840bedSJames Smart 		    struct nvmefc_ls_req *pnvme_lsreq)
135154840bedSJames Smart {
135254840bedSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmet = targetport->private;
135354840bedSJames Smart 	struct lpfc_hba *phba;
135454840bedSJames Smart 	struct lpfc_nodelist *ndlp;
135554840bedSJames Smart 	int ret;
135654840bedSJames Smart 
135754840bedSJames Smart 	phba = lpfc_nvmet->phba;
135854840bedSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
135954840bedSJames Smart 		return;
136054840bedSJames Smart 
136154840bedSJames Smart 	ndlp = (struct lpfc_nodelist *)hosthandle;
136254840bedSJames Smart 
136354840bedSJames Smart 	ret = __lpfc_nvme_ls_abort(phba->pport, ndlp, pnvme_lsreq);
136454840bedSJames Smart 	if (!ret)
136554840bedSJames Smart 		atomic_inc(&lpfc_nvmet->xmt_ls_abort);
136654840bedSJames Smart }
136754840bedSJames Smart 
1368ab723121SJames Smart static void
13694c2805aaSJames Smart lpfc_nvmet_host_release(void *hosthandle)
13704c2805aaSJames Smart {
13714c2805aaSJames Smart 	struct lpfc_nodelist *ndlp = hosthandle;
1372243156c0SJames Smart 	struct lpfc_hba *phba = ndlp->phba;
13734c2805aaSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
13744c2805aaSJames Smart 
13754c2805aaSJames Smart 	if (!phba->targetport || !phba->targetport->private)
13764c2805aaSJames Smart 		return;
13774c2805aaSJames Smart 
13784c2805aaSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1379243156c0SJames Smart 			"6202 NVMET XPT releasing hosthandle x%px "
1380243156c0SJames Smart 			"DID x%x xflags x%x refcnt %d\n",
1381243156c0SJames Smart 			hosthandle, ndlp->nlp_DID, ndlp->fc4_xpt_flags,
1382243156c0SJames Smart 			kref_read(&ndlp->kref));
13834c2805aaSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1384243156c0SJames Smart 	spin_lock_irq(&ndlp->lock);
1385243156c0SJames Smart 	ndlp->fc4_xpt_flags &= ~NLP_XPT_HAS_HH;
1386243156c0SJames Smart 	spin_unlock_irq(&ndlp->lock);
1387243156c0SJames Smart 	lpfc_nlp_put(ndlp);
13884c2805aaSJames Smart 	atomic_set(&tgtp->state, 0);
13894c2805aaSJames Smart }
13904c2805aaSJames Smart 
13914c2805aaSJames Smart static void
1392ab723121SJames Smart lpfc_nvmet_discovery_event(struct nvmet_fc_target_port *tgtport)
1393ab723121SJames Smart {
1394ab723121SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1395ab723121SJames Smart 	struct lpfc_hba *phba;
1396ab723121SJames Smart 	uint32_t rc;
1397ab723121SJames Smart 
1398ab723121SJames Smart 	tgtp = tgtport->private;
1399ab723121SJames Smart 	phba = tgtp->phba;
1400ab723121SJames Smart 
1401ab723121SJames Smart 	rc = lpfc_issue_els_rscn(phba->pport, 0);
1402372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1403ab723121SJames Smart 			"6420 NVMET subsystem change: Notification %s\n",
1404ab723121SJames Smart 			(rc) ? "Failed" : "Sent");
1405ab723121SJames Smart }
1406ab723121SJames Smart 
1407d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
1408d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
1409d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
1410d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
1411a97ec51bSJames Smart 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
141219b58d94SJames Smart 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
141350738420SJames Smart 	.defer_rcv	= lpfc_nvmet_defer_rcv,
1414ab723121SJames Smart 	.discovery_event = lpfc_nvmet_discovery_event,
141554840bedSJames Smart 	.ls_req         = lpfc_nvmet_ls_req,
141654840bedSJames Smart 	.ls_abort       = lpfc_nvmet_ls_abort,
14174c2805aaSJames Smart 	.host_release   = lpfc_nvmet_host_release,
1418d613b6a7SJames Smart 
1419d613b6a7SJames Smart 	.max_hw_queues  = 1,
1420d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1421d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1422d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
1423d613b6a7SJames Smart 
1424d613b6a7SJames Smart 	/* optional features */
1425d613b6a7SJames Smart 	.target_features = 0,
1426d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
1427d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
142854840bedSJames Smart 	.lsrqst_priv_sz = 0,
1429d613b6a7SJames Smart };
1430d613b6a7SJames Smart 
143139aa23f9SColin Ian King static void
143266d7ce93SDick Kennedy __lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba *phba,
143366d7ce93SDick Kennedy 		struct lpfc_nvmet_ctx_info *infop)
14346c621a22SJames Smart {
14356c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf, *next_ctx_buf;
14366c621a22SJames Smart 	unsigned long flags;
14376c621a22SJames Smart 
143866d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, flags);
1439966bb5b7SJames Smart 	list_for_each_entry_safe(ctx_buf, next_ctx_buf,
144066d7ce93SDick Kennedy 				&infop->nvmet_ctx_list, list) {
14415e5b511dSJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
14426c621a22SJames Smart 		list_del_init(&ctx_buf->list);
14435e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
144466d7ce93SDick Kennedy 
1445a789241eSJames Smart 		spin_lock(&phba->hbalock);
144666d7ce93SDick Kennedy 		__lpfc_clear_active_sglq(phba, ctx_buf->sglq->sli4_lxritag);
1447a789241eSJames Smart 		spin_unlock(&phba->hbalock);
1448a789241eSJames Smart 
14496c621a22SJames Smart 		ctx_buf->sglq->state = SGL_FREED;
14506c621a22SJames Smart 		ctx_buf->sglq->ndlp = NULL;
14516c621a22SJames Smart 
1452c4031db7SDan Carpenter 		spin_lock(&phba->sli4_hba.sgl_list_lock);
14536c621a22SJames Smart 		list_add_tail(&ctx_buf->sglq->list,
14546c621a22SJames Smart 				&phba->sli4_hba.lpfc_nvmet_sgl_list);
1455c4031db7SDan Carpenter 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
14566c621a22SJames Smart 
14576c621a22SJames Smart 		lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
14586c621a22SJames Smart 		kfree(ctx_buf->context);
14596c621a22SJames Smart 	}
146066d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, flags);
1461966bb5b7SJames Smart }
146266d7ce93SDick Kennedy 
146366d7ce93SDick Kennedy static void
146466d7ce93SDick Kennedy lpfc_nvmet_cleanup_io_context(struct lpfc_hba *phba)
146566d7ce93SDick Kennedy {
146666d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
146766d7ce93SDick Kennedy 	int i, j;
146866d7ce93SDick Kennedy 
146966d7ce93SDick Kennedy 	/* The first context list, MRQ 0 CPU 0 */
147066d7ce93SDick Kennedy 	infop = phba->sli4_hba.nvmet_ctx_info;
147166d7ce93SDick Kennedy 	if (!infop)
147266d7ce93SDick Kennedy 		return;
147366d7ce93SDick Kennedy 
147466d7ce93SDick Kennedy 	/* Cycle the the entire CPU context list for every MRQ */
147566d7ce93SDick Kennedy 	for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
1476222e9239SJames Smart 		for_each_present_cpu(j) {
1477222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, j, i);
147866d7ce93SDick Kennedy 			__lpfc_nvmet_clean_io_for_cpu(phba, infop);
147966d7ce93SDick Kennedy 		}
148066d7ce93SDick Kennedy 	}
148166d7ce93SDick Kennedy 	kfree(phba->sli4_hba.nvmet_ctx_info);
148266d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = NULL;
14836c621a22SJames Smart }
14846c621a22SJames Smart 
148539aa23f9SColin Ian King static int
14866c621a22SJames Smart lpfc_nvmet_setup_io_context(struct lpfc_hba *phba)
14876c621a22SJames Smart {
14886c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
14896c621a22SJames Smart 	struct lpfc_iocbq *nvmewqe;
14906c621a22SJames Smart 	union lpfc_wqe128 *wqe;
149166d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *last_infop;
149266d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
1493222e9239SJames Smart 	int i, j, idx, cpu;
14946c621a22SJames Smart 
14956c621a22SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
14966c621a22SJames Smart 			"6403 Allocate NVMET resources for %d XRIs\n",
14976c621a22SJames Smart 			phba->sli4_hba.nvmet_xri_cnt);
14986c621a22SJames Smart 
149966d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = kcalloc(
1500222e9239SJames Smart 		phba->sli4_hba.num_possible_cpu * phba->cfg_nvmet_mrq,
150166d7ce93SDick Kennedy 		sizeof(struct lpfc_nvmet_ctx_info), GFP_KERNEL);
150266d7ce93SDick Kennedy 	if (!phba->sli4_hba.nvmet_ctx_info) {
1503372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
150466d7ce93SDick Kennedy 				"6419 Failed allocate memory for "
150566d7ce93SDick Kennedy 				"nvmet context lists\n");
150666d7ce93SDick Kennedy 		return -ENOMEM;
150766d7ce93SDick Kennedy 	}
150866d7ce93SDick Kennedy 
150966d7ce93SDick Kennedy 	/*
151066d7ce93SDick Kennedy 	 * Assuming X CPUs in the system, and Y MRQs, allocate some
151166d7ce93SDick Kennedy 	 * lpfc_nvmet_ctx_info structures as follows:
151266d7ce93SDick Kennedy 	 *
151366d7ce93SDick Kennedy 	 * cpu0/mrq0 cpu1/mrq0 ... cpuX/mrq0
151466d7ce93SDick Kennedy 	 * cpu0/mrq1 cpu1/mrq1 ... cpuX/mrq1
151566d7ce93SDick Kennedy 	 * ...
151666d7ce93SDick Kennedy 	 * cpuX/mrqY cpuX/mrqY ... cpuX/mrqY
151766d7ce93SDick Kennedy 	 *
151866d7ce93SDick Kennedy 	 * Each line represents a MRQ "silo" containing an entry for
151966d7ce93SDick Kennedy 	 * every CPU.
152066d7ce93SDick Kennedy 	 *
152166d7ce93SDick Kennedy 	 * MRQ X is initially assumed to be associated with CPU X, thus
152266d7ce93SDick Kennedy 	 * contexts are initially distributed across all MRQs using
152366d7ce93SDick Kennedy 	 * the MRQ index (N) as follows cpuN/mrqN. When contexts are
152466d7ce93SDick Kennedy 	 * freed, the are freed to the MRQ silo based on the CPU number
152566d7ce93SDick Kennedy 	 * of the IO completion. Thus a context that was allocated for MRQ A
152666d7ce93SDick Kennedy 	 * whose IO completed on CPU B will be freed to cpuB/mrqA.
152766d7ce93SDick Kennedy 	 */
1528222e9239SJames Smart 	for_each_possible_cpu(i) {
152966d7ce93SDick Kennedy 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1530222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
153166d7ce93SDick Kennedy 			INIT_LIST_HEAD(&infop->nvmet_ctx_list);
153266d7ce93SDick Kennedy 			spin_lock_init(&infop->nvmet_ctx_list_lock);
153366d7ce93SDick Kennedy 			infop->nvmet_ctx_list_cnt = 0;
153466d7ce93SDick Kennedy 		}
153566d7ce93SDick Kennedy 	}
153666d7ce93SDick Kennedy 
153766d7ce93SDick Kennedy 	/*
153866d7ce93SDick Kennedy 	 * Setup the next CPU context info ptr for each MRQ.
153966d7ce93SDick Kennedy 	 * MRQ 0 will cycle thru CPUs 0 - X separately from
154066d7ce93SDick Kennedy 	 * MRQ 1 cycling thru CPUs 0 - X, and so on.
154166d7ce93SDick Kennedy 	 */
154266d7ce93SDick Kennedy 	for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1543222e9239SJames Smart 		last_infop = lpfc_get_ctx_list(phba,
1544222e9239SJames Smart 					       cpumask_first(cpu_present_mask),
1545222e9239SJames Smart 					       j);
1546222e9239SJames Smart 		for (i = phba->sli4_hba.num_possible_cpu - 1;  i >= 0; i--) {
154766d7ce93SDick Kennedy 			infop = lpfc_get_ctx_list(phba, i, j);
154866d7ce93SDick Kennedy 			infop->nvmet_ctx_next_cpu = last_infop;
154966d7ce93SDick Kennedy 			last_infop = infop;
155066d7ce93SDick Kennedy 		}
155166d7ce93SDick Kennedy 	}
155266d7ce93SDick Kennedy 
15536c621a22SJames Smart 	/* For all nvmet xris, allocate resources needed to process a
15546c621a22SJames Smart 	 * received command on a per xri basis.
15556c621a22SJames Smart 	 */
155666d7ce93SDick Kennedy 	idx = 0;
1557222e9239SJames Smart 	cpu = cpumask_first(cpu_present_mask);
15586c621a22SJames Smart 	for (i = 0; i < phba->sli4_hba.nvmet_xri_cnt; i++) {
15596c621a22SJames Smart 		ctx_buf = kzalloc(sizeof(*ctx_buf), GFP_KERNEL);
15606c621a22SJames Smart 		if (!ctx_buf) {
1561372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15626c621a22SJames Smart 					"6404 Ran out of memory for NVMET\n");
15636c621a22SJames Smart 			return -ENOMEM;
15646c621a22SJames Smart 		}
15656c621a22SJames Smart 
15666c621a22SJames Smart 		ctx_buf->context = kzalloc(sizeof(*ctx_buf->context),
15676c621a22SJames Smart 					   GFP_KERNEL);
15686c621a22SJames Smart 		if (!ctx_buf->context) {
15696c621a22SJames Smart 			kfree(ctx_buf);
1570372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15716c621a22SJames Smart 					"6405 Ran out of NVMET "
15726c621a22SJames Smart 					"context memory\n");
15736c621a22SJames Smart 			return -ENOMEM;
15746c621a22SJames Smart 		}
15756c621a22SJames Smart 		ctx_buf->context->ctxbuf = ctx_buf;
15767b7f551bSJames Smart 		ctx_buf->context->state = LPFC_NVME_STE_FREE;
15776c621a22SJames Smart 
15786c621a22SJames Smart 		ctx_buf->iocbq = lpfc_sli_get_iocbq(phba);
15796c621a22SJames Smart 		if (!ctx_buf->iocbq) {
15806c621a22SJames Smart 			kfree(ctx_buf->context);
15816c621a22SJames Smart 			kfree(ctx_buf);
1582372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15836c621a22SJames Smart 					"6406 Ran out of NVMET iocb/WQEs\n");
15846c621a22SJames Smart 			return -ENOMEM;
15856c621a22SJames Smart 		}
1586a680a929SJames Smart 		ctx_buf->iocbq->cmd_flag = LPFC_IO_NVMET;
15876c621a22SJames Smart 		nvmewqe = ctx_buf->iocbq;
1588205e8240SJames Smart 		wqe = &nvmewqe->wqe;
1589bd3061baSJames Smart 
15906c621a22SJames Smart 		/* Initialize WQE */
15916c621a22SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
15926c621a22SJames Smart 
15936c621a22SJames Smart 		ctx_buf->iocbq->context1 = NULL;
15946c621a22SJames Smart 		spin_lock(&phba->sli4_hba.sgl_list_lock);
15956c621a22SJames Smart 		ctx_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, ctx_buf->iocbq);
15966c621a22SJames Smart 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
15976c621a22SJames Smart 		if (!ctx_buf->sglq) {
15986c621a22SJames Smart 			lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
15996c621a22SJames Smart 			kfree(ctx_buf->context);
16006c621a22SJames Smart 			kfree(ctx_buf);
1601372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16026c621a22SJames Smart 					"6407 Ran out of NVMET XRIs\n");
16036c621a22SJames Smart 			return -ENOMEM;
16046c621a22SJames Smart 		}
1605472e146dSJames Smart 		INIT_WORK(&ctx_buf->defer_work, lpfc_nvmet_fcp_rqst_defer_work);
160666d7ce93SDick Kennedy 
160766d7ce93SDick Kennedy 		/*
160866d7ce93SDick Kennedy 		 * Add ctx to MRQidx context list. Our initial assumption
160966d7ce93SDick Kennedy 		 * is MRQidx will be associated with CPUidx. This association
161066d7ce93SDick Kennedy 		 * can change on the fly.
161166d7ce93SDick Kennedy 		 */
1612222e9239SJames Smart 		infop = lpfc_get_ctx_list(phba, cpu, idx);
161366d7ce93SDick Kennedy 		spin_lock(&infop->nvmet_ctx_list_lock);
161466d7ce93SDick Kennedy 		list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
161566d7ce93SDick Kennedy 		infop->nvmet_ctx_list_cnt++;
161666d7ce93SDick Kennedy 		spin_unlock(&infop->nvmet_ctx_list_lock);
161766d7ce93SDick Kennedy 
161866d7ce93SDick Kennedy 		/* Spread ctx structures evenly across all MRQs */
161966d7ce93SDick Kennedy 		idx++;
1620222e9239SJames Smart 		if (idx >= phba->cfg_nvmet_mrq) {
162166d7ce93SDick Kennedy 			idx = 0;
1622222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1623222e9239SJames Smart 			continue;
1624222e9239SJames Smart 		}
1625222e9239SJames Smart 		cpu = cpumask_next(cpu, cpu_present_mask);
1626222e9239SJames Smart 		if (cpu == nr_cpu_ids)
1627222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1628222e9239SJames Smart 
16296c621a22SJames Smart 	}
163066d7ce93SDick Kennedy 
1631222e9239SJames Smart 	for_each_present_cpu(i) {
16322879265fSJames Smart 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
16332879265fSJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
163466d7ce93SDick Kennedy 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
163566d7ce93SDick Kennedy 					"6408 TOTAL NVMET ctx for CPU %d "
163632350664SJames Smart 					"MRQ %d: cnt %d nextcpu x%px\n",
163766d7ce93SDick Kennedy 					i, j, infop->nvmet_ctx_list_cnt,
163866d7ce93SDick Kennedy 					infop->nvmet_ctx_next_cpu);
163966d7ce93SDick Kennedy 		}
164066d7ce93SDick Kennedy 	}
16416c621a22SJames Smart 	return 0;
16426c621a22SJames Smart }
16436c621a22SJames Smart 
1644d613b6a7SJames Smart int
1645d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1646d613b6a7SJames Smart {
1647d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1648d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1649d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
16506c621a22SJames Smart 	int error;
1651d613b6a7SJames Smart 
1652d613b6a7SJames Smart 	if (phba->targetport)
1653d613b6a7SJames Smart 		return 0;
1654d613b6a7SJames Smart 
16556c621a22SJames Smart 	error = lpfc_nvmet_setup_io_context(phba);
16566c621a22SJames Smart 	if (error)
16576c621a22SJames Smart 		return error;
16586c621a22SJames Smart 
1659d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1660d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1661d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1662d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
1663d613b6a7SJames Smart 
16645b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
16655b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
16665b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
16674d4c4a4aSJames Smart 	 */
16684d4c4a4aSJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1669cdb42becSJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_hdw_queue;
1670f485c18dSDick Kennedy 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP;
1671d613b6a7SJames Smart 
16727d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1673d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1674d613b6a7SJames Smart 					     &phba->pcidev->dev,
1675d613b6a7SJames Smart 					     &phba->targetport);
1676166d7211SJames Smart #else
16776c621a22SJames Smart 	error = -ENOENT;
1678166d7211SJames Smart #endif
1679d613b6a7SJames Smart 	if (error) {
1680372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1681e7981a2cSDick Kennedy 				"6025 Cannot register NVME targetport x%x: "
1682e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1683e7981a2cSDick Kennedy 				error,
1684e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1685e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1686e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1687d613b6a7SJames Smart 		phba->targetport = NULL;
1688e7981a2cSDick Kennedy 		phba->nvmet_support = 0;
16896c621a22SJames Smart 
16906c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
16916c621a22SJames Smart 
1692d613b6a7SJames Smart 	} else {
1693d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
1694d613b6a7SJames Smart 			phba->targetport->private;
1695d613b6a7SJames Smart 		tgtp->phba = phba;
1696d613b6a7SJames Smart 
1697d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1698d613b6a7SJames Smart 				"6026 Registered NVME "
169932350664SJames Smart 				"targetport: x%px, private x%px "
1700e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1701d613b6a7SJames Smart 				phba->targetport, tgtp,
1702e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1703e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1704e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1705d613b6a7SJames Smart 
1706d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
1707d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
1708d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
1709d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
1710547077a4SJames Smart 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1711d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
1712d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
1713d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
17144b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_xb_set, 0);
17154b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_aborted, 0);
1716d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1717d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1718d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1719d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1720d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
1721d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1722d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
1723d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
1724d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
1725547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_release, 0);
1726d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1727d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
17284b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_xb_set, 0);
17294b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_aborted, 0);
1730d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
17314b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_xri_abort_cqe, 0);
1732547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
1733547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1734547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_unsol, 0);
1735547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_sol, 0);
1736d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
1737d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1738411de511SJames Smart 		atomic_set(&tgtp->defer_ctx, 0);
1739411de511SJames Smart 		atomic_set(&tgtp->defer_fod, 0);
1740411de511SJames Smart 		atomic_set(&tgtp->defer_wqfull, 0);
1741d613b6a7SJames Smart 	}
1742d613b6a7SJames Smart 	return error;
1743d613b6a7SJames Smart }
1744d613b6a7SJames Smart 
1745d613b6a7SJames Smart int
1746d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1747d613b6a7SJames Smart {
1748d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1749d613b6a7SJames Smart 
1750d613b6a7SJames Smart 	if (!phba->targetport)
1751d613b6a7SJames Smart 		return 0;
1752d613b6a7SJames Smart 
1753d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
175432350664SJames Smart 			 "6007 Update NVMET port x%px did x%x\n",
1755d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
1756d613b6a7SJames Smart 
1757d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
1758d613b6a7SJames Smart 	return 0;
1759d613b6a7SJames Smart }
1760d613b6a7SJames Smart 
1761318083adSJames Smart /**
1762318083adSJames Smart  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1763318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
1764318083adSJames Smart  * @axri: pointer to the nvmet xri abort wcqe structure.
1765318083adSJames Smart  *
1766318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
1767318083adSJames Smart  * NVMET aborted xri.
1768318083adSJames Smart  **/
1769318083adSJames Smart void
1770318083adSJames Smart lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
1771318083adSJames Smart 			    struct sli4_wcqe_xri_aborted *axri)
1772318083adSJames Smart {
177332b93865SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
177486c67379SJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
177586c67379SJames Smart 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
17767cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp, *next_ctxp;
17774b056682SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
177879d8c4ceSJames Smart 	struct nvmefc_tgt_fcp_req *req = NULL;
177986c67379SJames Smart 	struct lpfc_nodelist *ndlp;
178086c67379SJames Smart 	unsigned long iflag = 0;
178186c67379SJames Smart 	int rrq_empty = 0;
178286c67379SJames Smart 	bool released = false;
178386c67379SJames Smart 
178486c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
178586c67379SJames Smart 			"6317 XB aborted xri x%x rxid x%x\n", xri, rxid);
178686c67379SJames Smart 
178786c67379SJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
178886c67379SJames Smart 		return;
17894b056682SJames Smart 
17904b056682SJames Smart 	if (phba->targetport) {
17914b056682SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
17924b056682SJames Smart 		atomic_inc(&tgtp->xmt_fcp_xri_abort_cqe);
17934b056682SJames Smart 	}
17944b056682SJames Smart 
1795a789241eSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
179686c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
179786c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
179886c67379SJames Smart 				 list) {
17996c621a22SJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
180086c67379SJames Smart 			continue;
180186c67379SJames Smart 
18027740b615SJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock,
18037740b615SJames Smart 				       iflag);
18047740b615SJames Smart 
18057740b615SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
180686c67379SJames Smart 		/* Check if we already received a free context call
180786c67379SJames Smart 		 * and we have completed processing an abort situation.
180886c67379SJames Smart 		 */
18097b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_CTX_RLS &&
18107b7f551bSJames Smart 		    !(ctxp->flag & LPFC_NVME_ABORT_OP)) {
18117740b615SJames Smart 			spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
181279d8c4ceSJames Smart 			list_del_init(&ctxp->list);
18137740b615SJames Smart 			spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
181486c67379SJames Smart 			released = true;
181586c67379SJames Smart 		}
18167b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_XBUSY;
18177740b615SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
181886c67379SJames Smart 
181986c67379SJames Smart 		rrq_empty = list_empty(&phba->active_rrq_list);
182086c67379SJames Smart 		ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1821307e3380SJames Smart 		if (ndlp &&
182286c67379SJames Smart 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
182386c67379SJames Smart 		     ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
182486c67379SJames Smart 			lpfc_set_rrq_active(phba, ndlp,
18256c621a22SJames Smart 				ctxp->ctxbuf->sglq->sli4_lxritag,
182686c67379SJames Smart 				rxid, 1);
182786c67379SJames Smart 			lpfc_sli4_abts_err_handler(phba, ndlp, axri);
182886c67379SJames Smart 		}
182986c67379SJames Smart 
183086c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
183179d8c4ceSJames Smart 				"6318 XB aborted oxid x%x flg x%x (%x)\n",
183286c67379SJames Smart 				ctxp->oxid, ctxp->flag, released);
183386c67379SJames Smart 		if (released)
18346c621a22SJames Smart 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
18356c621a22SJames Smart 
183686c67379SJames Smart 		if (rrq_empty)
183786c67379SJames Smart 			lpfc_worker_wake_up(phba);
183886c67379SJames Smart 		return;
183986c67379SJames Smart 	}
1840a789241eSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
184179d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_xri(phba, xri);
184279d8c4ceSJames Smart 	if (ctxp) {
184379d8c4ceSJames Smart 		/*
184479d8c4ceSJames Smart 		 *  Abort already done by FW, so BA_ACC sent.
184579d8c4ceSJames Smart 		 *  However, the transport may be unaware.
184679d8c4ceSJames Smart 		 */
184779d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
184879d8c4ceSJames Smart 				"6323 NVMET Rcv ABTS xri x%x ctxp state x%x "
184979d8c4ceSJames Smart 				"flag x%x oxid x%x rxid x%x\n",
185079d8c4ceSJames Smart 				xri, ctxp->state, ctxp->flag, ctxp->oxid,
185179d8c4ceSJames Smart 				rxid);
185279d8c4ceSJames Smart 
185379d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
18547b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_ABTS_RCV;
18557b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_ABORT;
185679d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
185779d8c4ceSJames Smart 
185879d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
185979d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
186001d53c04SJames Smart 				 xri, raw_smp_processor_id(), 0);
186179d8c4ceSJames Smart 
18627cacae2aSJames Smart 		req = &ctxp->hdlrctx.fcp_req;
186379d8c4ceSJames Smart 		if (req)
186479d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport, req);
186579d8c4ceSJames Smart 	}
186632b93865SJames Smart #endif
186786c67379SJames Smart }
186886c67379SJames Smart 
186986c67379SJames Smart int
187086c67379SJames Smart lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport *vport,
187186c67379SJames Smart 			   struct fc_frame_header *fc_hdr)
187286c67379SJames Smart {
187386c67379SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
187486c67379SJames Smart 	struct lpfc_hba *phba = vport->phba;
18757cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp, *next_ctxp;
187686c67379SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
187732b93865SJames Smart 	uint32_t sid;
187879d8c4ceSJames Smart 	uint16_t oxid, xri;
187986c67379SJames Smart 	unsigned long iflag = 0;
188086c67379SJames Smart 
188132b93865SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
188279d8c4ceSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
188386c67379SJames Smart 
1884a789241eSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
188586c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
188686c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
188786c67379SJames Smart 				 list) {
188879d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
188986c67379SJames Smart 			continue;
189086c67379SJames Smart 
189132b93865SJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
189232b93865SJames Smart 
1893a789241eSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock,
1894a789241eSJames Smart 				       iflag);
189586c67379SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
18967b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_ABTS_RCV;
189786c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
189886c67379SJames Smart 
189986c67379SJames Smart 		lpfc_nvmeio_data(phba,
190086c67379SJames Smart 			"NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1901d6d189ceSBart Van Assche 			xri, raw_smp_processor_id(), 0);
190286c67379SJames Smart 
190386c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
190486c67379SJames Smart 				"6319 NVMET Rcv ABTS:acc xri x%x\n", xri);
190586c67379SJames Smart 
19067cacae2aSJames Smart 		rsp = &ctxp->hdlrctx.fcp_req;
190786c67379SJames Smart 		nvmet_fc_rcv_fcp_abort(phba->targetport, rsp);
190886c67379SJames Smart 
190986c67379SJames Smart 		/* Respond with BA_ACC accordingly */
191086c67379SJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
191186c67379SJames Smart 		return 0;
191286c67379SJames Smart 	}
1913a789241eSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
191479d8c4ceSJames Smart 	/* check the wait list */
191579d8c4ceSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
191679d8c4ceSJames Smart 		struct rqb_dmabuf *nvmebuf;
191779d8c4ceSJames Smart 		struct fc_frame_header *fc_hdr_tmp;
191879d8c4ceSJames Smart 		u32 sid_tmp;
191979d8c4ceSJames Smart 		u16 oxid_tmp;
192079d8c4ceSJames Smart 		bool found = false;
192179d8c4ceSJames Smart 
192279d8c4ceSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
192379d8c4ceSJames Smart 
192479d8c4ceSJames Smart 		/* match by oxid and s_id */
192579d8c4ceSJames Smart 		list_for_each_entry(nvmebuf,
192679d8c4ceSJames Smart 				    &phba->sli4_hba.lpfc_nvmet_io_wait_list,
192779d8c4ceSJames Smart 				    hbuf.list) {
192879d8c4ceSJames Smart 			fc_hdr_tmp = (struct fc_frame_header *)
192979d8c4ceSJames Smart 					(nvmebuf->hbuf.virt);
193079d8c4ceSJames Smart 			oxid_tmp = be16_to_cpu(fc_hdr_tmp->fh_ox_id);
193179d8c4ceSJames Smart 			sid_tmp = sli4_sid_from_fc_hdr(fc_hdr_tmp);
193279d8c4ceSJames Smart 			if (oxid_tmp != oxid || sid_tmp != sid)
193379d8c4ceSJames Smart 				continue;
193486c67379SJames Smart 
193586c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
193679d8c4ceSJames Smart 					"6321 NVMET Rcv ABTS oxid x%x from x%x "
193779d8c4ceSJames Smart 					"is waiting for a ctxp\n",
193879d8c4ceSJames Smart 					oxid, sid);
193979d8c4ceSJames Smart 
194079d8c4ceSJames Smart 			list_del_init(&nvmebuf->hbuf.list);
194179d8c4ceSJames Smart 			phba->sli4_hba.nvmet_io_wait_cnt--;
194279d8c4ceSJames Smart 			found = true;
194379d8c4ceSJames Smart 			break;
194479d8c4ceSJames Smart 		}
194579d8c4ceSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
194679d8c4ceSJames Smart 				       iflag);
194779d8c4ceSJames Smart 
194879d8c4ceSJames Smart 		/* free buffer since already posted a new DMA buffer to RQ */
194979d8c4ceSJames Smart 		if (found) {
195079d8c4ceSJames Smart 			nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
195179d8c4ceSJames Smart 			/* Respond with BA_ACC accordingly */
195279d8c4ceSJames Smart 			lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
195379d8c4ceSJames Smart 			return 0;
195479d8c4ceSJames Smart 		}
195579d8c4ceSJames Smart 	}
195679d8c4ceSJames Smart 
195779d8c4ceSJames Smart 	/* check active list */
195879d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_oxid(phba, oxid, sid);
195979d8c4ceSJames Smart 	if (ctxp) {
196079d8c4ceSJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
196179d8c4ceSJames Smart 
196279d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
19637b7f551bSJames Smart 		ctxp->flag |= (LPFC_NVME_ABTS_RCV | LPFC_NVME_ABORT_OP);
196479d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
196579d8c4ceSJames Smart 
196679d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
196779d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
196879d8c4ceSJames Smart 				 xri, raw_smp_processor_id(), 0);
196979d8c4ceSJames Smart 
197079d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
197179d8c4ceSJames Smart 				"6322 NVMET Rcv ABTS:acc oxid x%x xri x%x "
197279d8c4ceSJames Smart 				"flag x%x state x%x\n",
197379d8c4ceSJames Smart 				ctxp->oxid, xri, ctxp->flag, ctxp->state);
197479d8c4ceSJames Smart 
19757b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_TNOTIFY) {
197679d8c4ceSJames Smart 			/* Notify the transport */
197779d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport,
19787cacae2aSJames Smart 					       &ctxp->hdlrctx.fcp_req);
197979d8c4ceSJames Smart 		} else {
19806594d31bSJames Smart 			cancel_work_sync(&ctxp->ctxbuf->defer_work);
198179d8c4ceSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
198279d8c4ceSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
198379d8c4ceSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
198479d8c4ceSJames Smart 		}
198579d8c4ceSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
198679d8c4ceSJames Smart 					       ctxp->oxid);
198779d8c4ceSJames Smart 
19886594d31bSJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
198979d8c4ceSJames Smart 		return 0;
199079d8c4ceSJames Smart 	}
199179d8c4ceSJames Smart 
199279d8c4ceSJames Smart 	lpfc_nvmeio_data(phba, "NVMET ABTS RCV: oxid x%x CPU %02x rjt %d\n",
199379d8c4ceSJames Smart 			 oxid, raw_smp_processor_id(), 1);
199479d8c4ceSJames Smart 
199579d8c4ceSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
199679d8c4ceSJames Smart 			"6320 NVMET Rcv ABTS:rjt oxid x%x\n", oxid);
199786c67379SJames Smart 
199886c67379SJames Smart 	/* Respond with BA_RJT accordingly */
199986c67379SJames Smart 	lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 0);
200086c67379SJames Smart #endif
2001b06e13c3SJens Axboe 	return 0;
2002318083adSJames Smart }
2003318083adSJames Smart 
20046e8e1c14SJames Smart static void
20056e8e1c14SJames Smart lpfc_nvmet_wqfull_flush(struct lpfc_hba *phba, struct lpfc_queue *wq,
20067cacae2aSJames Smart 			struct lpfc_async_xchg_ctx *ctxp)
20076e8e1c14SJames Smart {
20086e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
20096e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
20106e8e1c14SJames Smart 	struct lpfc_iocbq *next_nvmewqeq;
20116e8e1c14SJames Smart 	unsigned long iflags;
20126e8e1c14SJames Smart 	struct lpfc_wcqe_complete wcqe;
20136e8e1c14SJames Smart 	struct lpfc_wcqe_complete *wcqep;
20146e8e1c14SJames Smart 
20156e8e1c14SJames Smart 	pring = wq->pring;
20166e8e1c14SJames Smart 	wcqep = &wcqe;
20176e8e1c14SJames Smart 
20186e8e1c14SJames Smart 	/* Fake an ABORT error code back to cmpl routine */
20196e8e1c14SJames Smart 	memset(wcqep, 0, sizeof(struct lpfc_wcqe_complete));
20206e8e1c14SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, IOSTAT_LOCAL_REJECT);
20216e8e1c14SJames Smart 	wcqep->parameter = IOERR_ABORT_REQUESTED;
20226e8e1c14SJames Smart 
20236e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
20246e8e1c14SJames Smart 	list_for_each_entry_safe(nvmewqeq, next_nvmewqeq,
20256e8e1c14SJames Smart 				 &wq->wqfull_list, list) {
20266e8e1c14SJames Smart 		if (ctxp) {
20276e8e1c14SJames Smart 			/* Checking for a specific IO to flush */
20286e8e1c14SJames Smart 			if (nvmewqeq->context2 == ctxp) {
20296e8e1c14SJames Smart 				list_del(&nvmewqeq->list);
20306e8e1c14SJames Smart 				spin_unlock_irqrestore(&pring->ring_lock,
20316e8e1c14SJames Smart 						       iflags);
2032a680a929SJames Smart 				memcpy(&nvmewqeq->wcqe_cmpl, wcqep,
2033a680a929SJames Smart 				       sizeof(*wcqep));
20346e8e1c14SJames Smart 				lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq,
2035a680a929SJames Smart 							  nvmewqeq);
20366e8e1c14SJames Smart 				return;
20376e8e1c14SJames Smart 			}
20386e8e1c14SJames Smart 			continue;
20396e8e1c14SJames Smart 		} else {
20406e8e1c14SJames Smart 			/* Flush all IOs */
20416e8e1c14SJames Smart 			list_del(&nvmewqeq->list);
20426e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
2043a680a929SJames Smart 			memcpy(&nvmewqeq->wcqe_cmpl, wcqep, sizeof(*wcqep));
2044a680a929SJames Smart 			lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq, nvmewqeq);
20456e8e1c14SJames Smart 			spin_lock_irqsave(&pring->ring_lock, iflags);
20466e8e1c14SJames Smart 		}
20476e8e1c14SJames Smart 	}
20486e8e1c14SJames Smart 	if (!ctxp)
20496e8e1c14SJames Smart 		wq->q_flag &= ~HBA_NVMET_WQFULL;
20506e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
20516e8e1c14SJames Smart }
20526e8e1c14SJames Smart 
20536e8e1c14SJames Smart void
20546e8e1c14SJames Smart lpfc_nvmet_wqfull_process(struct lpfc_hba *phba,
20556e8e1c14SJames Smart 			  struct lpfc_queue *wq)
20566e8e1c14SJames Smart {
20576e8e1c14SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
20586e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
20596e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
20607cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
20616e8e1c14SJames Smart 	unsigned long iflags;
20626e8e1c14SJames Smart 	int rc;
20636e8e1c14SJames Smart 
20646e8e1c14SJames Smart 	/*
20656e8e1c14SJames Smart 	 * Some WQE slots are available, so try to re-issue anything
20666e8e1c14SJames Smart 	 * on the WQ wqfull_list.
20676e8e1c14SJames Smart 	 */
20686e8e1c14SJames Smart 	pring = wq->pring;
20696e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
20706e8e1c14SJames Smart 	while (!list_empty(&wq->wqfull_list)) {
20716e8e1c14SJames Smart 		list_remove_head(&wq->wqfull_list, nvmewqeq, struct lpfc_iocbq,
20726e8e1c14SJames Smart 				 list);
20736e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
20747cacae2aSJames Smart 		ctxp = (struct lpfc_async_xchg_ctx *)nvmewqeq->context2;
20751fbf9742SJames Smart 		rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
20766e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
20776e8e1c14SJames Smart 		if (rc == -EBUSY) {
20786e8e1c14SJames Smart 			/* WQ was full again, so put it back on the list */
20796e8e1c14SJames Smart 			list_add(&nvmewqeq->list, &wq->wqfull_list);
20806e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
20816e8e1c14SJames Smart 			return;
20826e8e1c14SJames Smart 		}
208379d8c4ceSJames Smart 		if (rc == WQE_SUCCESS) {
208479d8c4ceSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
208579d8c4ceSJames Smart 			if (ctxp->ts_cmd_nvme) {
20867cacae2aSJames Smart 				if (ctxp->hdlrctx.fcp_req.op == NVMET_FCOP_RSP)
208779d8c4ceSJames Smart 					ctxp->ts_status_wqput = ktime_get_ns();
208879d8c4ceSJames Smart 				else
208979d8c4ceSJames Smart 					ctxp->ts_data_wqput = ktime_get_ns();
209079d8c4ceSJames Smart 			}
209179d8c4ceSJames Smart #endif
209279d8c4ceSJames Smart 		} else {
209379d8c4ceSJames Smart 			WARN_ON(rc);
209479d8c4ceSJames Smart 		}
20956e8e1c14SJames Smart 	}
20966e8e1c14SJames Smart 	wq->q_flag &= ~HBA_NVMET_WQFULL;
20976e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
20986e8e1c14SJames Smart 
20996e8e1c14SJames Smart #endif
21006e8e1c14SJames Smart }
21016e8e1c14SJames Smart 
2102d613b6a7SJames Smart void
2103d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
2104d613b6a7SJames Smart {
21057d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2106d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
21076e8e1c14SJames Smart 	struct lpfc_queue *wq;
21086e8e1c14SJames Smart 	uint32_t qidx;
2109c41f5988SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(tport_unreg_cmp);
2110d613b6a7SJames Smart 
2111d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
2112d613b6a7SJames Smart 		return;
2113d613b6a7SJames Smart 	if (phba->targetport) {
2114d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2115cdb42becSJames Smart 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
2116c00f62e6SJames Smart 			wq = phba->sli4_hba.hdwq[qidx].io_wq;
21176e8e1c14SJames Smart 			lpfc_nvmet_wqfull_flush(phba, wq, NULL);
21186e8e1c14SJames Smart 		}
2119c41f5988SEwan D. Milne 		tgtp->tport_unreg_cmp = &tport_unreg_cmp;
2120d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
2121af6de8c6SEwan D. Milne 		if (!wait_for_completion_timeout(&tport_unreg_cmp,
21222a0fb340SJames Smart 					msecs_to_jiffies(LPFC_NVMET_WAIT_TMO)))
2123372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
212432350664SJames Smart 					"6179 Unreg targetport x%px timeout "
21252a0fb340SJames Smart 					"reached.\n", phba->targetport);
21266c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
2127d613b6a7SJames Smart 	}
2128d613b6a7SJames Smart 	phba->targetport = NULL;
2129166d7211SJames Smart #endif
2130d613b6a7SJames Smart }
2131d613b6a7SJames Smart 
2132d613b6a7SJames Smart /**
21333a8070c5SJames Smart  * lpfc_nvmet_handle_lsreq - Process an NVME LS request
2134d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
21353a8070c5SJames Smart  * @axchg: pointer to exchange context for the NVME LS request
2136d613b6a7SJames Smart  *
21373a8070c5SJames Smart  * This routine is used for processing an asychronously received NVME LS
21383a8070c5SJames Smart  * request. Any remaining validation is done and the LS is then forwarded
21393a8070c5SJames Smart  * to the nvmet-fc transport via nvmet_fc_rcv_ls_req().
21403a8070c5SJames Smart  *
21413a8070c5SJames Smart  * The calling sequence should be: nvmet_fc_rcv_ls_req() -> (processing)
21423a8070c5SJames Smart  * -> lpfc_nvmet_xmt_ls_rsp/cmp -> req->done.
21433a8070c5SJames Smart  * lpfc_nvme_xmt_ls_rsp_cmp should free the allocated axchg.
21443a8070c5SJames Smart  *
21453a8070c5SJames Smart  * Returns 0 if LS was handled and delivered to the transport
21463a8070c5SJames Smart  * Returns 1 if LS failed to be handled and should be dropped
21473a8070c5SJames Smart  */
21483a8070c5SJames Smart int
21493a8070c5SJames Smart lpfc_nvmet_handle_lsreq(struct lpfc_hba *phba,
21503a8070c5SJames Smart 			struct lpfc_async_xchg_ctx *axchg)
2151d613b6a7SJames Smart {
21527d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
21533a8070c5SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = phba->targetport->private;
21543a8070c5SJames Smart 	uint32_t *payload = axchg->payload;
21553a8070c5SJames Smart 	int rc;
2156d613b6a7SJames Smart 
2157d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
21583a8070c5SJames Smart 
21594c2805aaSJames Smart 	/*
21604c2805aaSJames Smart 	 * Driver passes the ndlp as the hosthandle argument allowing
21614c2805aaSJames Smart 	 * the transport to generate LS requests for any associateions
21624c2805aaSJames Smart 	 * that are created.
21634c2805aaSJames Smart 	 */
21644c2805aaSJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, axchg->ndlp, &axchg->ls_rsp,
21653a8070c5SJames Smart 				 axchg->payload, axchg->size);
2166d613b6a7SJames Smart 
2167d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2168ce1b591cSJames Smart 			"6037 NVMET Unsol rcv: sz %d rc %d: %08x %08x %08x "
21693a8070c5SJames Smart 			"%08x %08x %08x\n", axchg->size, rc,
2170d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
2171d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
21722b65e182SJames Smart 
21733a8070c5SJames Smart 	if (!rc) {
2174d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
21753a8070c5SJames Smart 		return 0;
2176d613b6a7SJames Smart 	}
21772b65e182SJames Smart 
2178d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
2179166d7211SJames Smart #endif
21803a8070c5SJames Smart 	return 1;
2181d613b6a7SJames Smart }
2182d613b6a7SJames Smart 
2183472e146dSJames Smart static void
2184472e146dSJames Smart lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf)
2185472e146dSJames Smart {
2186472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
21877cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp = ctx_buf->context;
2188472e146dSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
2189472e146dSJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
2190472e146dSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
21914552e0f6SJames Smart 	uint32_t *payload, qno;
2192472e146dSJames Smart 	uint32_t rc;
2193472e146dSJames Smart 	unsigned long iflags;
2194472e146dSJames Smart 
2195472e146dSJames Smart 	if (!nvmebuf) {
2196372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2197472e146dSJames Smart 			"6159 process_rcv_fcp_req, nvmebuf is NULL, "
2198472e146dSJames Smart 			"oxid: x%x flg: x%x state: x%x\n",
2199472e146dSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
2200472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
2201472e146dSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2202472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2203472e146dSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
2204472e146dSJames Smart 						 ctxp->oxid);
2205472e146dSJames Smart 		return;
2206472e146dSJames Smart 	}
2207472e146dSJames Smart 
22087b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABTS_RCV) {
2209372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
221079d8c4ceSJames Smart 				"6324 IO oxid x%x aborted\n",
221179d8c4ceSJames Smart 				ctxp->oxid);
221279d8c4ceSJames Smart 		return;
221379d8c4ceSJames Smart 	}
221479d8c4ceSJames Smart 
2215472e146dSJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
2216472e146dSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
22177b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_TNOTIFY;
2218d74a89aaSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2219d74a89aaSJames Smart 	if (ctxp->ts_isr_cmd)
2220d74a89aaSJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
2221d74a89aaSJames Smart #endif
2222472e146dSJames Smart 	/*
2223472e146dSJames Smart 	 * The calling sequence should be:
2224472e146dSJames Smart 	 * nvmet_fc_rcv_fcp_req->lpfc_nvmet_xmt_fcp_op/cmp- req->done
2225472e146dSJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
2226472e146dSJames Smart 	 * When we return from nvmet_fc_rcv_fcp_req, all relevant info
2227472e146dSJames Smart 	 * the NVME command / FC header is stored.
2228472e146dSJames Smart 	 * A buffer has already been reposted for this IO, so just free
2229472e146dSJames Smart 	 * the nvmebuf.
2230472e146dSJames Smart 	 */
22317cacae2aSJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->hdlrctx.fcp_req,
2232472e146dSJames Smart 				  payload, ctxp->size);
2233472e146dSJames Smart 	/* Process FCP command */
2234472e146dSJames Smart 	if (rc == 0) {
2235472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
22364552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
22377b7f551bSJames Smart 		if ((ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) ||
22384552e0f6SJames Smart 		    (nvmebuf != ctxp->rqb_buffer)) {
22394552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22404552e0f6SJames Smart 			return;
22414552e0f6SJames Smart 		}
22424552e0f6SJames Smart 		ctxp->rqb_buffer = NULL;
22434552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22444552e0f6SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
2245472e146dSJames Smart 		return;
2246472e146dSJames Smart 	}
2247472e146dSJames Smart 
2248472e146dSJames Smart 	/* Processing of FCP command is deferred */
2249472e146dSJames Smart 	if (rc == -EOVERFLOW) {
2250472e146dSJames Smart 		lpfc_nvmeio_data(phba, "NVMET RCV BUSY: xri x%x sz %d "
2251472e146dSJames Smart 				 "from %06x\n",
2252472e146dSJames Smart 				 ctxp->oxid, ctxp->size, ctxp->sid);
2253472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
2254472e146dSJames Smart 		atomic_inc(&tgtp->defer_fod);
22554552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
22567b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) {
22574552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22584552e0f6SJames Smart 			return;
22594552e0f6SJames Smart 		}
22604552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22614552e0f6SJames Smart 		/*
22624552e0f6SJames Smart 		 * Post a replacement DMA buffer to RQ and defer
22634552e0f6SJames Smart 		 * freeing rcv buffer till .defer_rcv callback
22644552e0f6SJames Smart 		 */
22654552e0f6SJames Smart 		qno = nvmebuf->idx;
22664552e0f6SJames Smart 		lpfc_post_rq_buffer(
22674552e0f6SJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
22684552e0f6SJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2269472e146dSJames Smart 		return;
2270472e146dSJames Smart 	}
22717b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_TNOTIFY;
2272472e146dSJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2273372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2274472e146dSJames Smart 			"2582 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
2275472e146dSJames Smart 			ctxp->oxid, rc,
2276472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_in),
2277472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_out),
2278472e146dSJames Smart 			atomic_read(&tgtp->xmt_fcp_release));
2279472e146dSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
2280472e146dSJames Smart 			 ctxp->oxid, ctxp->size, ctxp->sid);
2281472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflags);
2282472e146dSJames Smart 	lpfc_nvmet_defer_release(phba, ctxp);
2283472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2284472e146dSJames Smart 	lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
2285472e146dSJames Smart #endif
2286472e146dSJames Smart }
2287472e146dSJames Smart 
2288472e146dSJames Smart static void
2289472e146dSJames Smart lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *work)
2290472e146dSJames Smart {
2291472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2292472e146dSJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf =
2293472e146dSJames Smart 		container_of(work, struct lpfc_nvmet_ctxbuf, defer_work);
2294472e146dSJames Smart 
2295472e146dSJames Smart 	lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2296472e146dSJames Smart #endif
2297472e146dSJames Smart }
2298472e146dSJames Smart 
229966d7ce93SDick Kennedy static struct lpfc_nvmet_ctxbuf *
230066d7ce93SDick Kennedy lpfc_nvmet_replenish_context(struct lpfc_hba *phba,
230166d7ce93SDick Kennedy 			     struct lpfc_nvmet_ctx_info *current_infop)
230266d7ce93SDick Kennedy {
23032299e432SDick Kennedy #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
230466d7ce93SDick Kennedy 	struct lpfc_nvmet_ctxbuf *ctx_buf = NULL;
230566d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *get_infop;
230666d7ce93SDick Kennedy 	int i;
230766d7ce93SDick Kennedy 
230866d7ce93SDick Kennedy 	/*
230966d7ce93SDick Kennedy 	 * The current_infop for the MRQ a NVME command IU was received
231066d7ce93SDick Kennedy 	 * on is empty. Our goal is to replenish this MRQs context
231166d7ce93SDick Kennedy 	 * list from a another CPUs.
231266d7ce93SDick Kennedy 	 *
231366d7ce93SDick Kennedy 	 * First we need to pick a context list to start looking on.
231466d7ce93SDick Kennedy 	 * nvmet_ctx_start_cpu has available context the last time
231566d7ce93SDick Kennedy 	 * we needed to replenish this CPU where nvmet_ctx_next_cpu
231666d7ce93SDick Kennedy 	 * is just the next sequential CPU for this MRQ.
231766d7ce93SDick Kennedy 	 */
231866d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_start_cpu)
231966d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_start_cpu;
232066d7ce93SDick Kennedy 	else
232166d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_next_cpu;
232266d7ce93SDick Kennedy 
2323222e9239SJames Smart 	for (i = 0; i < phba->sli4_hba.num_possible_cpu; i++) {
232466d7ce93SDick Kennedy 		if (get_infop == current_infop) {
232566d7ce93SDick Kennedy 			get_infop = get_infop->nvmet_ctx_next_cpu;
232666d7ce93SDick Kennedy 			continue;
232766d7ce93SDick Kennedy 		}
232866d7ce93SDick Kennedy 		spin_lock(&get_infop->nvmet_ctx_list_lock);
232966d7ce93SDick Kennedy 
233066d7ce93SDick Kennedy 		/* Just take the entire context list, if there are any */
233166d7ce93SDick Kennedy 		if (get_infop->nvmet_ctx_list_cnt) {
233266d7ce93SDick Kennedy 			list_splice_init(&get_infop->nvmet_ctx_list,
233366d7ce93SDick Kennedy 				    &current_infop->nvmet_ctx_list);
233466d7ce93SDick Kennedy 			current_infop->nvmet_ctx_list_cnt =
233566d7ce93SDick Kennedy 				get_infop->nvmet_ctx_list_cnt - 1;
233666d7ce93SDick Kennedy 			get_infop->nvmet_ctx_list_cnt = 0;
233766d7ce93SDick Kennedy 			spin_unlock(&get_infop->nvmet_ctx_list_lock);
233866d7ce93SDick Kennedy 
233966d7ce93SDick Kennedy 			current_infop->nvmet_ctx_start_cpu = get_infop;
234066d7ce93SDick Kennedy 			list_remove_head(&current_infop->nvmet_ctx_list,
234166d7ce93SDick Kennedy 					 ctx_buf, struct lpfc_nvmet_ctxbuf,
234266d7ce93SDick Kennedy 					 list);
234366d7ce93SDick Kennedy 			return ctx_buf;
234466d7ce93SDick Kennedy 		}
234566d7ce93SDick Kennedy 
234666d7ce93SDick Kennedy 		/* Otherwise, move on to the next CPU for this MRQ */
234766d7ce93SDick Kennedy 		spin_unlock(&get_infop->nvmet_ctx_list_lock);
234866d7ce93SDick Kennedy 		get_infop = get_infop->nvmet_ctx_next_cpu;
234966d7ce93SDick Kennedy 	}
235066d7ce93SDick Kennedy 
23512299e432SDick Kennedy #endif
235266d7ce93SDick Kennedy 	/* Nothing found, all contexts for the MRQ are in-flight */
235366d7ce93SDick Kennedy 	return NULL;
235466d7ce93SDick Kennedy }
235566d7ce93SDick Kennedy 
2356d613b6a7SJames Smart /**
2357d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
2358d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
235966d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2360d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
2361d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2362d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2363d613b6a7SJames Smart  *
2364d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
2365d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
2366d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
2367d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
2368d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
2369d613b6a7SJames Smart  * of the discovery state machine.
2370d613b6a7SJames Smart  **/
2371d613b6a7SJames Smart static void
2372d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
237366d7ce93SDick Kennedy 			    uint32_t idx,
2374d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
2375d74a89aaSJames Smart 			    uint64_t isr_timestamp,
2376d74a89aaSJames Smart 			    uint8_t cqflag)
2377d613b6a7SJames Smart {
23787cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
2379d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2380d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
23816c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
238266d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *current_infop;
2383472e146dSJames Smart 	uint32_t size, oxid, sid, qno;
23846c621a22SJames Smart 	unsigned long iflag;
238566d7ce93SDick Kennedy 	int current_cpu;
2386d613b6a7SJames Smart 
238773626173SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_TARGET_FC))
238873626173SArnd Bergmann 		return;
238973626173SArnd Bergmann 
23906c621a22SJames Smart 	ctx_buf = NULL;
2391d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
2392372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
23936c621a22SJames Smart 				"6157 NVMET FCP Drop IO\n");
2394472e146dSJames Smart 		if (nvmebuf)
2395472e146dSJames Smart 			lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2396472e146dSJames Smart 		return;
2397d613b6a7SJames Smart 	}
2398d613b6a7SJames Smart 
239966d7ce93SDick Kennedy 	/*
240066d7ce93SDick Kennedy 	 * Get a pointer to the context list for this MRQ based on
240166d7ce93SDick Kennedy 	 * the CPU this MRQ IRQ is associated with. If the CPU association
240266d7ce93SDick Kennedy 	 * changes from our initial assumption, the context list could
240366d7ce93SDick Kennedy 	 * be empty, thus it would need to be replenished with the
240466d7ce93SDick Kennedy 	 * context list from another CPU for this MRQ.
240566d7ce93SDick Kennedy 	 */
2406d6d189ceSBart Van Assche 	current_cpu = raw_smp_processor_id();
240766d7ce93SDick Kennedy 	current_infop = lpfc_get_ctx_list(phba, current_cpu, idx);
240866d7ce93SDick Kennedy 	spin_lock_irqsave(&current_infop->nvmet_ctx_list_lock, iflag);
240966d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_list_cnt) {
241066d7ce93SDick Kennedy 		list_remove_head(&current_infop->nvmet_ctx_list,
24116c621a22SJames Smart 				 ctx_buf, struct lpfc_nvmet_ctxbuf, list);
241266d7ce93SDick Kennedy 		current_infop->nvmet_ctx_list_cnt--;
2413966bb5b7SJames Smart 	} else {
241466d7ce93SDick Kennedy 		ctx_buf = lpfc_nvmet_replenish_context(phba, current_infop);
24156c621a22SJames Smart 	}
241666d7ce93SDick Kennedy 	spin_unlock_irqrestore(&current_infop->nvmet_ctx_list_lock, iflag);
24176c621a22SJames Smart 
2418a8cf5dfeSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
2419a8cf5dfeSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2420a8cf5dfeSJames Smart 	size = nvmebuf->bytes_recv;
2421a8cf5dfeSJames Smart 
2422a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2423840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
2424840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->rcv_io);
242563df6d63SJames Smart 		if (idx != current_cpu)
242663df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
242763df6d63SJames Smart 					"6703 CPU Check rcv: "
242863df6d63SJames Smart 					"cpu %d expect %d\n",
242963df6d63SJames Smart 					current_cpu, idx);
2430a8cf5dfeSJames Smart 	}
2431a8cf5dfeSJames Smart #endif
2432a8cf5dfeSJames Smart 
2433a8cf5dfeSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d CPU %02x\n",
2434d6d189ceSBart Van Assche 			 oxid, size, raw_smp_processor_id());
2435a8cf5dfeSJames Smart 
2436411de511SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2437411de511SJames Smart 
24386c621a22SJames Smart 	if (!ctx_buf) {
2439a8cf5dfeSJames Smart 		/* Queue this NVME IO to process later */
2440a8cf5dfeSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
2441a8cf5dfeSJames Smart 		list_add_tail(&nvmebuf->hbuf.list,
2442a8cf5dfeSJames Smart 			      &phba->sli4_hba.lpfc_nvmet_io_wait_list);
2443a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt++;
2444a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_total++;
2445a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
2446a8cf5dfeSJames Smart 				       iflag);
2447a8cf5dfeSJames Smart 
2448a8cf5dfeSJames Smart 		/* Post a brand new DMA buffer to RQ */
2449a8cf5dfeSJames Smart 		qno = nvmebuf->idx;
2450a8cf5dfeSJames Smart 		lpfc_post_rq_buffer(
2451a8cf5dfeSJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
2452a8cf5dfeSJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2453411de511SJames Smart 
2454411de511SJames Smart 		atomic_inc(&tgtp->defer_ctx);
2455a8cf5dfeSJames Smart 		return;
24566c621a22SJames Smart 	}
2457d613b6a7SJames Smart 
2458d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
2459d613b6a7SJames Smart 
24607cacae2aSJames Smart 	ctxp = (struct lpfc_async_xchg_ctx *)ctx_buf->context;
246179d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
246279d8c4ceSJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.t_active_ctx_list);
246379d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
24647b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_FREE) {
2465372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2466ce1b591cSJames Smart 				"6414 NVMET Context corrupt %d %d oxid x%x\n",
2467ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
2468ce1b591cSJames Smart 	}
2469d613b6a7SJames Smart 	ctxp->wqeq = NULL;
2470d613b6a7SJames Smart 	ctxp->offset = 0;
2471d613b6a7SJames Smart 	ctxp->phba = phba;
2472d613b6a7SJames Smart 	ctxp->size = size;
2473d613b6a7SJames Smart 	ctxp->oxid = oxid;
2474d613b6a7SJames Smart 	ctxp->sid = sid;
247566d7ce93SDick Kennedy 	ctxp->idx = idx;
24767b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_RCV;
2477d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
2478d613b6a7SJames Smart 	ctxp->flag = 0;
24796c621a22SJames Smart 	ctxp->ctxbuf = ctx_buf;
2480cbc5de1bSJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
24811fbf9742SJames Smart 	ctxp->hdwq = NULL;
24822b7824d0SJames Smart 	spin_lock_init(&ctxp->ctxlock);
2483d613b6a7SJames Smart 
24842b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2485d74a89aaSJames Smart 	if (isr_timestamp)
24862b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
2487d74a89aaSJames Smart 	ctxp->ts_cmd_nvme = 0;
24882b65e182SJames Smart 	ctxp->ts_nvme_data = 0;
24892b65e182SJames Smart 	ctxp->ts_data_wqput = 0;
24902b65e182SJames Smart 	ctxp->ts_isr_data = 0;
24912b65e182SJames Smart 	ctxp->ts_data_nvme = 0;
24922b65e182SJames Smart 	ctxp->ts_nvme_status = 0;
24932b65e182SJames Smart 	ctxp->ts_status_wqput = 0;
24942b65e182SJames Smart 	ctxp->ts_isr_status = 0;
24952b65e182SJames Smart 	ctxp->ts_status_nvme = 0;
24962b65e182SJames Smart #endif
24972b65e182SJames Smart 
2498d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
2499d74a89aaSJames Smart 	/* check for cq processing load */
2500d74a89aaSJames Smart 	if (!cqflag) {
2501472e146dSJames Smart 		lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2502d74a89aaSJames Smart 		return;
2503d74a89aaSJames Smart 	}
2504d74a89aaSJames Smart 
2505d74a89aaSJames Smart 	if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
2506d74a89aaSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2507372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2508d74a89aaSJames Smart 				"6325 Unable to queue work for oxid x%x. "
2509d74a89aaSJames Smart 				"FCP Drop IO [x%x x%x x%x]\n",
2510d74a89aaSJames Smart 				ctxp->oxid,
2511d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_in),
2512d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_out),
2513d74a89aaSJames Smart 				atomic_read(&tgtp->xmt_fcp_release));
2514d74a89aaSJames Smart 
2515d74a89aaSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
2516d74a89aaSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2517d74a89aaSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
2518d74a89aaSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
2519d74a89aaSJames Smart 	}
2520d613b6a7SJames Smart }
2521d613b6a7SJames Smart 
2522d613b6a7SJames Smart /**
2523d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
2524d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
252566d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2526d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2527d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2528d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2529d613b6a7SJames Smart  *
2530d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2531d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2532d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2533d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
2534d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2535d613b6a7SJames Smart  **/
2536d613b6a7SJames Smart void
2537d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
253866d7ce93SDick Kennedy 			   uint32_t idx,
2539d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
2540d74a89aaSJames Smart 			   uint64_t isr_timestamp,
2541d74a89aaSJames Smart 			   uint8_t cqflag)
2542d613b6a7SJames Smart {
254343bfea1bSJames Smart 	if (!nvmebuf) {
2544372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
254543bfea1bSJames Smart 				"3167 NVMET FCP Drop IO\n");
254643bfea1bSJames Smart 		return;
254743bfea1bSJames Smart 	}
2548d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
25496c621a22SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2550d613b6a7SJames Smart 		return;
2551d613b6a7SJames Smart 	}
2552d74a89aaSJames Smart 	lpfc_nvmet_unsol_fcp_buffer(phba, idx, nvmebuf, isr_timestamp, cqflag);
2553d613b6a7SJames Smart }
2554d613b6a7SJames Smart 
2555d613b6a7SJames Smart /**
2556d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
2557d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
2558d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
2559d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
2560d613b6a7SJames Smart  * @rspsize: size of the NVME command.
2561d613b6a7SJames Smart  *
2562d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
2563d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
2564d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
2565d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
2566d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
2567d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
2568d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
2569d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
2570d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
2571d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
2572d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
2573d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
2574d613b6a7SJames Smart  * reference for the command's callback function to access later.
2575d613b6a7SJames Smart  *
2576d613b6a7SJames Smart  * Return code
2577d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
2578d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
2579d613b6a7SJames Smart  **/
2580d613b6a7SJames Smart static struct lpfc_iocbq *
2581d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
25827cacae2aSJames Smart 		       struct lpfc_async_xchg_ctx *ctxp,
2583d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
2584d613b6a7SJames Smart {
2585d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2586d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2587205e8240SJames Smart 	union lpfc_wqe128 *wqe;
2588d613b6a7SJames Smart 
2589d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2590372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2591ce1b591cSJames Smart 				"6104 NVMET prep LS wqe: link err: "
2592ce1b591cSJames Smart 				"NPORT x%x oxid:x%x ste %d\n",
2593ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2594d613b6a7SJames Smart 		return NULL;
2595d613b6a7SJames Smart 	}
2596d613b6a7SJames Smart 
2597d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
2598d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
2599d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2600372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2601ce1b591cSJames Smart 				"6105 NVMET prep LS wqe: No WQE: "
2602ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2603ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2604d613b6a7SJames Smart 		return NULL;
2605d613b6a7SJames Smart 	}
2606d613b6a7SJames Smart 
2607d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2608307e3380SJames Smart 	if (!ndlp ||
2609d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2610d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2611372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2612ce1b591cSJames Smart 				"6106 NVMET prep LS wqe: No ndlp: "
2613ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2614ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2615d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2616d613b6a7SJames Smart 	}
2617d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
2618d613b6a7SJames Smart 
2619d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
2620d613b6a7SJames Smart 	nvmewqe->context1 = lpfc_nlp_get(ndlp);
2621d613b6a7SJames Smart 	if (nvmewqe->context1 == NULL)
2622d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2623d613b6a7SJames Smart 	nvmewqe->context2 = ctxp;
2624d613b6a7SJames Smart 
2625d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
2626d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
2627d613b6a7SJames Smart 
2628d613b6a7SJames Smart 	/* Words 0 - 2 */
2629d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2630d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
2631d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
2632d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
2633d613b6a7SJames Smart 
2634d613b6a7SJames Smart 	/* Word 3 */
2635d613b6a7SJames Smart 
2636d613b6a7SJames Smart 	/* Word 4 */
2637d613b6a7SJames Smart 
2638d613b6a7SJames Smart 	/* Word 5 */
2639d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
2640d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
2641d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
26428b361639SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
2643d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
2644d613b6a7SJames Smart 
2645d613b6a7SJames Smart 	/* Word 6 */
2646d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
2647d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2648d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
2649d613b6a7SJames Smart 
2650d613b6a7SJames Smart 	/* Word 7 */
2651d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
2652d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
2653d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
2654d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
2655d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
2656d613b6a7SJames Smart 
2657d613b6a7SJames Smart 	/* Word 8 */
2658d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
2659d613b6a7SJames Smart 
2660d613b6a7SJames Smart 	/* Word 9 */
2661d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
2662d613b6a7SJames Smart 	/* Needs to be set by caller */
2663d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
2664d613b6a7SJames Smart 
2665d613b6a7SJames Smart 	/* Word 10 */
2666d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
2667d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2668d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
2669d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
2670d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
2671d613b6a7SJames Smart 
2672d613b6a7SJames Smart 	/* Word 11 */
2673d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
2674d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
2675d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
2676d613b6a7SJames Smart 	       OTHER_COMMAND);
2677d613b6a7SJames Smart 
2678d613b6a7SJames Smart 	/* Word 12 */
2679d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
2680d613b6a7SJames Smart 
2681d613b6a7SJames Smart 	nvmewqe->retry = 1;
2682d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2683d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2684a680a929SJames Smart 	nvmewqe->cmd_flag |= LPFC_IO_NVME_LS;
2685d613b6a7SJames Smart 
2686ce1b591cSJames Smart 	/* Xmit NVMET response to remote NPORT <did> */
2687d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2688ce1b591cSJames Smart 			"6039 Xmit NVMET LS response to remote "
2689d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
2690d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
2691d613b6a7SJames Smart 			rspsize);
2692d613b6a7SJames Smart 	return nvmewqe;
2693d613b6a7SJames Smart 
2694d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
2695d613b6a7SJames Smart 	nvmewqe->context2 = NULL;
2696d613b6a7SJames Smart 	nvmewqe->context3 = NULL;
2697d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
2698d613b6a7SJames Smart 	return NULL;
2699d613b6a7SJames Smart }
2700d613b6a7SJames Smart 
2701d613b6a7SJames Smart 
2702d613b6a7SJames Smart static struct lpfc_iocbq *
2703d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
27047cacae2aSJames Smart 			struct lpfc_async_xchg_ctx *ctxp)
2705d613b6a7SJames Smart {
27067cacae2aSJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->hdlrctx.fcp_req;
2707d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2708d613b6a7SJames Smart 	struct sli4_sge *sgl;
2709d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2710d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2711d613b6a7SJames Smart 	struct scatterlist *sgel;
2712d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
27130bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
2714d613b6a7SJames Smart 	dma_addr_t physaddr;
2715e7f40349SJames Smart 	int i, cnt, nsegs;
2716315b3fd1SJames Smart 	bool use_pbde = false;
2717d613b6a7SJames Smart 	int xc = 1;
2718d613b6a7SJames Smart 
2719d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2720372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2721ce1b591cSJames Smart 				"6107 NVMET prep FCP wqe: link err:"
2722ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2723ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2724d613b6a7SJames Smart 		return NULL;
2725d613b6a7SJames Smart 	}
2726d613b6a7SJames Smart 
2727d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2728307e3380SJames Smart 	if (!ndlp ||
2729d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2730d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2731372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2732ce1b591cSJames Smart 				"6108 NVMET prep FCP wqe: no ndlp: "
2733ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2734ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2735d613b6a7SJames Smart 		return NULL;
2736d613b6a7SJames Smart 	}
2737d613b6a7SJames Smart 
273881e6a637SJames Smart 	if (rsp->sg_cnt > lpfc_tgttemplate.max_sgl_segments) {
2739372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2740ce1b591cSJames Smart 				"6109 NVMET prep FCP wqe: seg cnt err: "
2741ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d cnt %d\n",
2742ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state,
2743ce1b591cSJames Smart 				phba->cfg_nvme_seg_cnt);
2744d613b6a7SJames Smart 		return NULL;
2745d613b6a7SJames Smart 	}
2746e7f40349SJames Smart 	nsegs = rsp->sg_cnt;
2747d613b6a7SJames Smart 
2748d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2749d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
2750d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2751d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
27526c621a22SJames Smart 		nvmewqe = ctxp->ctxbuf->iocbq;
2753d613b6a7SJames Smart 		if (nvmewqe == NULL) {
2754372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2755ce1b591cSJames Smart 					"6110 NVMET prep FCP wqe: No "
2756ce1b591cSJames Smart 					"WQE: NPORT x%x oxid x%x ste %d\n",
2757ce1b591cSJames Smart 					ctxp->sid, ctxp->oxid, ctxp->state);
2758d613b6a7SJames Smart 			return NULL;
2759d613b6a7SJames Smart 		}
2760d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
2761d613b6a7SJames Smart 		xc = 0; /* create new XRI */
2762d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
2763d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
2764d613b6a7SJames Smart 	}
2765d613b6a7SJames Smart 
2766d613b6a7SJames Smart 	/* Sanity check */
27677b7f551bSJames Smart 	if (((ctxp->state == LPFC_NVME_STE_RCV) &&
2768d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
27697b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_DATA)) {
2770205e8240SJames Smart 		wqe = &nvmewqe->wqe;
2771d613b6a7SJames Smart 	} else {
2772372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2773ce1b591cSJames Smart 				"6111 Wrong state NVMET FCP: %d  cnt %d\n",
2774ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt);
2775d613b6a7SJames Smart 		return NULL;
2776d613b6a7SJames Smart 	}
2777d613b6a7SJames Smart 
27786c621a22SJames Smart 	sgl  = (struct sli4_sge *)ctxp->ctxbuf->sglq->sgl;
2779d613b6a7SJames Smart 	switch (rsp->op) {
2780d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
2781d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
2782bd3061baSJames Smart 		/* From the tsend template, initialize words 7 - 11 */
2783bd3061baSJames Smart 		memcpy(&wqe->words[7],
2784bd3061baSJames Smart 		       &lpfc_tsend_cmd_template.words[7],
2785bd3061baSJames Smart 		       sizeof(uint32_t) * 5);
2786bd3061baSJames Smart 
2787d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2788d613b6a7SJames Smart 		sgel = &rsp->sg[0];
2789d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2790d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2791d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
2792d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
2793d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
2794d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2795d613b6a7SJames Smart 
2796d613b6a7SJames Smart 		/* Word 3 */
2797d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
2798d613b6a7SJames Smart 
2799d613b6a7SJames Smart 		/* Word 4 */
2800d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
2801d613b6a7SJames Smart 
2802d613b6a7SJames Smart 		/* Word 5 */
2803bd3061baSJames Smart 		wqe->fcp_tsend.reserved = 0;
2804d613b6a7SJames Smart 
2805d613b6a7SJames Smart 		/* Word 6 */
2806d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
2807d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2808d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
2809d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2810d613b6a7SJames Smart 
2811bd3061baSJames Smart 		/* Word 7 - set ar later */
2812d613b6a7SJames Smart 
2813d613b6a7SJames Smart 		/* Word 8 */
2814d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
2815d613b6a7SJames Smart 
2816d613b6a7SJames Smart 		/* Word 9 */
2817d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
2818d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
2819d613b6a7SJames Smart 
2820bd3061baSJames Smart 		/* Word 10 - set wqes later, in template xc=1 */
2821bd3061baSJames Smart 		if (!xc)
2822bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 0);
2823d613b6a7SJames Smart 
2824d613b6a7SJames Smart 		/* Word 12 */
2825d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2826d613b6a7SJames Smart 
2827d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
2828d613b6a7SJames Smart 		sgl->addr_hi = 0;
2829d613b6a7SJames Smart 		sgl->addr_lo = 0;
2830d613b6a7SJames Smart 		sgl->word2 = 0;
2831d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2832d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2833d613b6a7SJames Smart 		sgl->sge_len = 0;
2834d613b6a7SJames Smart 		sgl++;
2835d613b6a7SJames Smart 		sgl->addr_hi = 0;
2836d613b6a7SJames Smart 		sgl->addr_lo = 0;
2837d613b6a7SJames Smart 		sgl->word2 = 0;
2838d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2839d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2840d613b6a7SJames Smart 		sgl->sge_len = 0;
2841d613b6a7SJames Smart 		sgl++;
2842d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
2843d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
2844bd3061baSJames Smart 
2845bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2846bd3061baSJames Smart 
284720aefac3SJames Smart 			if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
284820aefac3SJames Smart 				if (ndlp->nlp_flag & NLP_SUPPRESS_RSP)
284920aefac3SJames Smart 					bf_set(wqe_sup,
285020aefac3SJames Smart 					       &wqe->fcp_tsend.wqe_com, 1);
2851d613b6a7SJames Smart 			} else {
2852d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
2853d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
2854d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
2855d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
2856d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
2857d613b6a7SJames Smart 				       rsp->rsplen);
2858d613b6a7SJames Smart 			}
2859d613b6a7SJames Smart 		} else {
2860d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
2861d613b6a7SJames Smart 
2862bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2863d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
2864d613b6a7SJames Smart 		}
2865d613b6a7SJames Smart 		break;
2866d613b6a7SJames Smart 
2867d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
2868bd3061baSJames Smart 		/* From the treceive template, initialize words 3 - 11 */
2869bd3061baSJames Smart 		memcpy(&wqe->words[3],
2870bd3061baSJames Smart 		       &lpfc_treceive_cmd_template.words[3],
2871bd3061baSJames Smart 		       sizeof(uint32_t) * 9);
2872bd3061baSJames Smart 
2873ea85a20cSJames Smart 		/* Words 0 - 2 : First SGE is skipped, set invalid BDE type */
2874ea85a20cSJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = LPFC_SGE_TYPE_SKIP;
2875ea85a20cSJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = 0;
2876ea85a20cSJames Smart 		wqe->fcp_treceive.bde.addrLow = 0;
2877ea85a20cSJames Smart 		wqe->fcp_treceive.bde.addrHigh = 0;
2878d613b6a7SJames Smart 
2879d613b6a7SJames Smart 		/* Word 4 */
2880d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
2881d613b6a7SJames Smart 
2882d613b6a7SJames Smart 		/* Word 6 */
2883d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
2884d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2885d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
2886d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2887d613b6a7SJames Smart 
2888d613b6a7SJames Smart 		/* Word 7 */
2889d613b6a7SJames Smart 
2890d613b6a7SJames Smart 		/* Word 8 */
2891d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
2892d613b6a7SJames Smart 
2893d613b6a7SJames Smart 		/* Word 9 */
2894d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
2895d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
2896d613b6a7SJames Smart 
2897bd3061baSJames Smart 		/* Word 10 - in template xc=1 */
2898bd3061baSJames Smart 		if (!xc)
2899bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, 0);
2900d613b6a7SJames Smart 
2901315b3fd1SJames Smart 		/* Word 11 - check for pbde */
2902315b3fd1SJames Smart 		if (nsegs == 1 && phba->cfg_enable_pbde) {
2903315b3fd1SJames Smart 			use_pbde = true;
2904315b3fd1SJames Smart 			/* Word 11 - PBDE bit already preset by template */
2905bd3061baSJames Smart 		} else {
2906315b3fd1SJames Smart 			/* Overwrite default template setting */
2907bd3061baSJames Smart 			bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 0);
2908bd3061baSJames Smart 		}
2909d613b6a7SJames Smart 
2910d613b6a7SJames Smart 		/* Word 12 */
2911d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2912d613b6a7SJames Smart 
2913ea85a20cSJames Smart 		/* Setup 2 SKIP SGEs */
2914ea85a20cSJames Smart 		sgl->addr_hi = 0;
2915ea85a20cSJames Smart 		sgl->addr_lo = 0;
2916d613b6a7SJames Smart 		sgl->word2 = 0;
2917ea85a20cSJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2918d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2919ea85a20cSJames Smart 		sgl->sge_len = 0;
2920d613b6a7SJames Smart 		sgl++;
2921d613b6a7SJames Smart 		sgl->addr_hi = 0;
2922d613b6a7SJames Smart 		sgl->addr_lo = 0;
2923d613b6a7SJames Smart 		sgl->word2 = 0;
2924d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2925d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2926d613b6a7SJames Smart 		sgl->sge_len = 0;
2927d613b6a7SJames Smart 		sgl++;
2928d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
2929d613b6a7SJames Smart 		break;
2930d613b6a7SJames Smart 
2931d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
2932bd3061baSJames Smart 		/* From the treceive template, initialize words 4 - 11 */
2933bd3061baSJames Smart 		memcpy(&wqe->words[4],
2934bd3061baSJames Smart 		       &lpfc_trsp_cmd_template.words[4],
2935bd3061baSJames Smart 		       sizeof(uint32_t) * 8);
2936bd3061baSJames Smart 
2937d613b6a7SJames Smart 		/* Words 0 - 2 */
2938d613b6a7SJames Smart 		physaddr = rsp->rspdma;
2939d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2940d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
2941d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
2942d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2943d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
2944d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2945d613b6a7SJames Smart 
2946d613b6a7SJames Smart 		/* Word 3 */
2947d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
2948d613b6a7SJames Smart 
2949d613b6a7SJames Smart 		/* Word 6 */
2950d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
2951d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2952d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
2953d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2954d613b6a7SJames Smart 
2955d613b6a7SJames Smart 		/* Word 7 */
2956d613b6a7SJames Smart 
2957d613b6a7SJames Smart 		/* Word 8 */
2958d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
2959d613b6a7SJames Smart 
2960d613b6a7SJames Smart 		/* Word 9 */
2961d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
2962d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
2963d613b6a7SJames Smart 
2964d613b6a7SJames Smart 		/* Word 10 */
2965bd3061baSJames Smart 		if (xc)
2966bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 1);
2967d613b6a7SJames Smart 
2968d613b6a7SJames Smart 		/* Word 11 */
2969bd3061baSJames Smart 		/* In template wqes=0 irsp=0 irsplen=0 - good response */
2970bd3061baSJames Smart 		if (rsp->rsplen != LPFC_NVMET_SUCCESS_LEN) {
2971bd3061baSJames Smart 			/* Bad response - embed it */
2972d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
2973d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
2974d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
2975d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
2976d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
2977d613b6a7SJames Smart 		}
2978bd3061baSJames Smart 
2979bd3061baSJames Smart 		/* Word 12 */
2980bd3061baSJames Smart 		wqe->fcp_trsp.rsvd_12_15[0] = 0;
2981d613b6a7SJames Smart 
2982d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
2983e7f40349SJames Smart 		nsegs = 0;
2984d613b6a7SJames Smart 		sgl->word2 = 0;
2985d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
2986d613b6a7SJames Smart 		break;
2987d613b6a7SJames Smart 
2988d613b6a7SJames Smart 	default:
2989d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2990d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
2991d613b6a7SJames Smart 				rsp->op);
2992d613b6a7SJames Smart 		return NULL;
2993d613b6a7SJames Smart 	}
2994d613b6a7SJames Smart 
2995d613b6a7SJames Smart 	nvmewqe->retry = 1;
2996d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2997d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2998d613b6a7SJames Smart 	nvmewqe->context1 = ndlp;
2999d613b6a7SJames Smart 
3000e7f40349SJames Smart 	for_each_sg(rsp->sg, sgel, nsegs, i) {
3001d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
3002d613b6a7SJames Smart 		cnt = sg_dma_len(sgel);
3003d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
3004d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
3005d613b6a7SJames Smart 		sgl->word2 = 0;
3006d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
3007d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
3008d613b6a7SJames Smart 		if ((i+1) == rsp->sg_cnt)
3009d613b6a7SJames Smart 			bf_set(lpfc_sli4_sge_last, sgl, 1);
3010d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
3011d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(cnt);
3012315b3fd1SJames Smart 		sgl++;
3013315b3fd1SJames Smart 		ctxp->offset += cnt;
3014315b3fd1SJames Smart 	}
3015315b3fd1SJames Smart 
30160bc2b7c5SJames Smart 	bde = (struct ulp_bde64 *)&wqe->words[13];
3017315b3fd1SJames Smart 	if (use_pbde) {
3018315b3fd1SJames Smart 		/* decrement sgl ptr backwards once to first data sge */
3019315b3fd1SJames Smart 		sgl--;
3020315b3fd1SJames Smart 
30210bc2b7c5SJames Smart 		/* Words 13-15 (PBDE) */
30220bc2b7c5SJames Smart 		bde->addrLow = sgl->addr_lo;
30230bc2b7c5SJames Smart 		bde->addrHigh = sgl->addr_hi;
3024315b3fd1SJames Smart 		bde->tus.f.bdeSize = le32_to_cpu(sgl->sge_len);
30250bc2b7c5SJames Smart 		bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
30260bc2b7c5SJames Smart 		bde->tus.w = cpu_to_le32(bde->tus.w);
3027414abe0aSJames Smart 	} else {
3028414abe0aSJames Smart 		memset(bde, 0, sizeof(struct ulp_bde64));
3029414abe0aSJames Smart 	}
30307b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DATA;
3031ce1b591cSJames Smart 	ctxp->entry_cnt++;
3032d613b6a7SJames Smart 	return nvmewqe;
3033d613b6a7SJames Smart }
3034d613b6a7SJames Smart 
3035d613b6a7SJames Smart /**
3036d613b6a7SJames Smart  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
3037d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3038d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3039a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
3040d613b6a7SJames Smart  *
3041d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3042d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
3043d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3044d613b6a7SJames Smart  **/
3045d613b6a7SJames Smart static void
3046d613b6a7SJames Smart lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3047a680a929SJames Smart 			     struct lpfc_iocbq *rspwqe)
3048d613b6a7SJames Smart {
30497cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3050d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3051b27cbd55SBart Van Assche 	uint32_t result;
305219b58d94SJames Smart 	unsigned long flags;
305319b58d94SJames Smart 	bool released = false;
3054a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
3055d613b6a7SJames Smart 
3056d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
3057d613b6a7SJames Smart 	result = wcqe->parameter;
3058d613b6a7SJames Smart 
3059d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
30607b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABORT_OP)
3061547077a4SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
3062d613b6a7SJames Smart 
3063c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
30647b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DONE;
306586c67379SJames Smart 
306686c67379SJames Smart 	/* Check if we already received a free context call
306786c67379SJames Smart 	 * and we have completed processing an abort situation.
306886c67379SJames Smart 	 */
30697b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_CTX_RLS) &&
30707b7f551bSJames Smart 	    !(ctxp->flag & LPFC_NVME_XBUSY)) {
3071c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
307279d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3073c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
307419b58d94SJames Smart 		released = true;
307586c67379SJames Smart 	}
30767b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
307719b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3078547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
307919b58d94SJames Smart 
3080e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
308179d8c4ceSJames Smart 			"6165 ABORT cmpl: oxid x%x flg x%x (%d) "
308286c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
308386c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
308486c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
308586c67379SJames Smart 			result, wcqe->word3);
308686c67379SJames Smart 
30876c621a22SJames Smart 	cmdwqe->context2 = NULL;
30886c621a22SJames Smart 	cmdwqe->context3 = NULL;
308919b58d94SJames Smart 	/*
309019b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
309119b58d94SJames Smart 	 * will be recycled by transport release call.
309219b58d94SJames Smart 	 */
309319b58d94SJames Smart 	if (released)
30946c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3095d613b6a7SJames Smart 
30966c621a22SJames Smart 	/* This is the iocbq for the abort, not the command */
3097d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
309886c67379SJames Smart 
309986c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
31007b7f551bSJames Smart 	 * For LPFC_NVME_XBUSY, lpfc_sli4_nvmet_xri_aborted
310186c67379SJames Smart 	 * should have been called already.
310286c67379SJames Smart 	 */
3103d613b6a7SJames Smart }
3104d613b6a7SJames Smart 
3105d613b6a7SJames Smart /**
310686c67379SJames Smart  * lpfc_nvmet_unsol_fcp_abort_cmp - Completion handler for ABTS
3107d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3108d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3109a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
3110d613b6a7SJames Smart  *
3111d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3112d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
3113d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3114d613b6a7SJames Smart  **/
3115d613b6a7SJames Smart static void
311686c67379SJames Smart lpfc_nvmet_unsol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3117a680a929SJames Smart 			       struct lpfc_iocbq *rspwqe)
3118d613b6a7SJames Smart {
31197cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3120d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
312119b58d94SJames Smart 	unsigned long flags;
3122b27cbd55SBart Van Assche 	uint32_t result;
312319b58d94SJames Smart 	bool released = false;
3124a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
3125d613b6a7SJames Smart 
3126d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
3127d613b6a7SJames Smart 	result = wcqe->parameter;
3128d613b6a7SJames Smart 
312986c67379SJames Smart 	if (!ctxp) {
313086c67379SJames Smart 		/* if context is clear, related io alrady complete */
3131d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
313286c67379SJames Smart 				"6070 ABTS cmpl: WCQE: %08x %08x %08x %08x\n",
313386c67379SJames Smart 				wcqe->word0, wcqe->total_data_placed,
3134d613b6a7SJames Smart 				result, wcqe->word3);
313586c67379SJames Smart 		return;
313686c67379SJames Smart 	}
3137d613b6a7SJames Smart 
313878e1d200SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3139c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
31407b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABORT_OP)
314178e1d200SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
314278e1d200SJames Smart 
3143d613b6a7SJames Smart 	/* Sanity check */
31447b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_ABORT) {
3145372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
314686c67379SJames Smart 				"6112 ABTS Wrong state:%d oxid x%x\n",
3147d613b6a7SJames Smart 				ctxp->state, ctxp->oxid);
3148d613b6a7SJames Smart 	}
314986c67379SJames Smart 
315086c67379SJames Smart 	/* Check if we already received a free context call
315186c67379SJames Smart 	 * and we have completed processing an abort situation.
315286c67379SJames Smart 	 */
31537b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DONE;
31547b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_CTX_RLS) &&
31557b7f551bSJames Smart 	    !(ctxp->flag & LPFC_NVME_XBUSY)) {
3156c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
315779d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3158c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
315919b58d94SJames Smart 		released = true;
316086c67379SJames Smart 	}
31617b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
316219b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3163547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
316419b58d94SJames Smart 
316586c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
316679d8c4ceSJames Smart 			"6316 ABTS cmpl oxid x%x flg x%x (%x) "
316786c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
316886c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
316986c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
317086c67379SJames Smart 			result, wcqe->word3);
31716c621a22SJames Smart 
31726c621a22SJames Smart 	cmdwqe->context2 = NULL;
31736c621a22SJames Smart 	cmdwqe->context3 = NULL;
317419b58d94SJames Smart 	/*
317519b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
317619b58d94SJames Smart 	 * will be recycled by transport release call.
317719b58d94SJames Smart 	 */
317819b58d94SJames Smart 	if (released)
31796c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
318086c67379SJames Smart 
318186c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
31827b7f551bSJames Smart 	 * For LPFC_NVME_XBUSY, lpfc_sli4_nvmet_xri_aborted
318386c67379SJames Smart 	 * should have been called already.
318486c67379SJames Smart 	 */
3185d613b6a7SJames Smart }
3186d613b6a7SJames Smart 
3187d613b6a7SJames Smart /**
3188d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
3189d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3190d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3191a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
3192d613b6a7SJames Smart  *
3193d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3194d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for LS cmds
3195d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3196d613b6a7SJames Smart  **/
3197d613b6a7SJames Smart static void
3198d613b6a7SJames Smart lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3199a680a929SJames Smart 			    struct lpfc_iocbq *rspwqe)
3200d613b6a7SJames Smart {
32017cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3202d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3203b27cbd55SBart Van Assche 	uint32_t result;
3204a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
3205d613b6a7SJames Smart 
3206d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
3207d613b6a7SJames Smart 	result = wcqe->parameter;
3208d613b6a7SJames Smart 
3209fcdd14b8SJames Smart 	if (phba->nvmet_support) {
3210d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3211547077a4SJames Smart 		atomic_inc(&tgtp->xmt_ls_abort_cmpl);
3212fcdd14b8SJames Smart 	}
3213d613b6a7SJames Smart 
3214d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
321532350664SJames Smart 			"6083 Abort cmpl: ctx x%px WCQE:%08x %08x %08x %08x\n",
3216d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
3217d613b6a7SJames Smart 			result, wcqe->word3);
3218d613b6a7SJames Smart 
3219ce1b591cSJames Smart 	if (!ctxp) {
3220372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3221ce1b591cSJames Smart 				"6415 NVMET LS Abort No ctx: WCQE: "
3222ce1b591cSJames Smart 				 "%08x %08x %08x %08x\n",
3223ce1b591cSJames Smart 				wcqe->word0, wcqe->total_data_placed,
3224ce1b591cSJames Smart 				result, wcqe->word3);
3225ce1b591cSJames Smart 
3226ce1b591cSJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
3227ce1b591cSJames Smart 		return;
3228ce1b591cSJames Smart 	}
3229ce1b591cSJames Smart 
32307b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_LS_ABORT) {
3231372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3232ce1b591cSJames Smart 				"6416 NVMET LS abort cmpl state mismatch: "
3233ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
3234ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3235ce1b591cSJames Smart 	}
3236ce1b591cSJames Smart 
3237d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
3238d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
3239d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3240d613b6a7SJames Smart 	kfree(ctxp);
3241d613b6a7SJames Smart }
3242d613b6a7SJames Smart 
3243d613b6a7SJames Smart static int
3244d613b6a7SJames Smart lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
32457cacae2aSJames Smart 			     struct lpfc_async_xchg_ctx *ctxp,
3246d613b6a7SJames Smart 			     uint32_t sid, uint16_t xri)
3247d613b6a7SJames Smart {
3248fcdd14b8SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = NULL;
3249d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3250205e8240SJames Smart 	union lpfc_wqe128 *wqe_abts;
3251d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3252d613b6a7SJames Smart 
3253d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
325486c67379SJames Smart 			"6067 ABTS: sid %x xri x%x/x%x\n",
3255318083adSJames Smart 			sid, xri, ctxp->wqeq->sli4_xritag);
3256d613b6a7SJames Smart 
3257fcdd14b8SJames Smart 	if (phba->nvmet_support && phba->targetport)
3258d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3259d613b6a7SJames Smart 
3260d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3261307e3380SJames Smart 	if (!ndlp ||
3262d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3263d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3264fcdd14b8SJames Smart 		if (tgtp)
3265d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_abort_rsp_error);
3266372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3267d613b6a7SJames Smart 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
3268b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3269d613b6a7SJames Smart 
3270d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3271d613b6a7SJames Smart 		return 0;
3272d613b6a7SJames Smart 	}
3273d613b6a7SJames Smart 
3274d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
3275d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
3276d613b6a7SJames Smart 
3277d613b6a7SJames Smart 	/*
3278d613b6a7SJames Smart 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
3279d613b6a7SJames Smart 	 * that were initialized in lpfc_sli4_nvmet_alloc.
3280d613b6a7SJames Smart 	 */
3281d613b6a7SJames Smart 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
3282d613b6a7SJames Smart 
3283d613b6a7SJames Smart 	/* Word 5 */
3284d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
3285d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
3286d613b6a7SJames Smart 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
3287d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
3288d613b6a7SJames Smart 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
3289d613b6a7SJames Smart 
3290d613b6a7SJames Smart 	/* Word 6 */
3291d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
3292d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
3293d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
3294d613b6a7SJames Smart 	       abts_wqeq->sli4_xritag);
3295d613b6a7SJames Smart 
3296d613b6a7SJames Smart 	/* Word 7 */
3297d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
3298d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
3299d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
3300d613b6a7SJames Smart 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
3301d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
3302d613b6a7SJames Smart 
3303d613b6a7SJames Smart 	/* Word 8 */
3304d613b6a7SJames Smart 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
3305d613b6a7SJames Smart 
3306d613b6a7SJames Smart 	/* Word 9 */
3307d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
3308d613b6a7SJames Smart 	/* Needs to be set by caller */
3309d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
3310d613b6a7SJames Smart 
3311d613b6a7SJames Smart 	/* Word 10 */
3312d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
3313d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
3314d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
3315d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
3316d613b6a7SJames Smart 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
3317d613b6a7SJames Smart 
3318d613b6a7SJames Smart 	/* Word 11 */
3319d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
3320d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
3321d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
3322d613b6a7SJames Smart 	       OTHER_COMMAND);
3323d613b6a7SJames Smart 
3324d613b6a7SJames Smart 	abts_wqeq->vport = phba->pport;
3325d613b6a7SJames Smart 	abts_wqeq->context1 = ndlp;
3326d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
3327d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
3328a680a929SJames Smart 	abts_wqeq->num_bdes = 0;
3329d613b6a7SJames Smart 	/* hba_wqidx should already be setup from command we are aborting */
3330d613b6a7SJames Smart 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
3331d613b6a7SJames Smart 	abts_wqeq->iocb.ulpLe = 1;
3332d613b6a7SJames Smart 
3333d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
3334d613b6a7SJames Smart 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
3335d613b6a7SJames Smart 			xri, abts_wqeq->iotag);
3336d613b6a7SJames Smart 	return 1;
3337d613b6a7SJames Smart }
3338d613b6a7SJames Smart 
3339db7531d2SJames Smart /**
3340db7531d2SJames Smart  * lpfc_nvmet_prep_abort_wqe - set up 'abort' work queue entry.
3341db7531d2SJames Smart  * @pwqeq: Pointer to command iocb.
3342db7531d2SJames Smart  * @xritag: Tag that  uniqely identifies the local exchange resource.
3343db7531d2SJames Smart  * @opt: Option bits -
3344db7531d2SJames Smart  *		bit 0 = inhibit sending abts on the link
3345db7531d2SJames Smart  *
3346db7531d2SJames Smart  * This function is called with hbalock held.
3347db7531d2SJames Smart  **/
3348185d17e1SJames Smart static void
3349db7531d2SJames Smart lpfc_nvmet_prep_abort_wqe(struct lpfc_iocbq *pwqeq, u16 xritag, u8 opt)
3350db7531d2SJames Smart {
3351db7531d2SJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
3352db7531d2SJames Smart 
3353db7531d2SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
3354db7531d2SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
3355db7531d2SJames Smart 	 */
3356db7531d2SJames Smart 	memset(wqe, 0, sizeof(*wqe));
3357db7531d2SJames Smart 
3358db7531d2SJames Smart 	if (opt & INHIBIT_ABORT)
3359db7531d2SJames Smart 		bf_set(abort_cmd_ia, &wqe->abort_cmd, 1);
3360db7531d2SJames Smart 	/* Abort specified xri tag, with the mask deliberately zeroed */
3361db7531d2SJames Smart 	bf_set(abort_cmd_criteria, &wqe->abort_cmd, T_XRI_TAG);
3362db7531d2SJames Smart 
3363db7531d2SJames Smart 	bf_set(wqe_cmnd, &wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
3364db7531d2SJames Smart 
3365db7531d2SJames Smart 	/* Abort the I/O associated with this outstanding exchange ID. */
3366db7531d2SJames Smart 	wqe->abort_cmd.wqe_com.abort_tag = xritag;
3367db7531d2SJames Smart 
3368db7531d2SJames Smart 	/* iotag for the wqe completion. */
3369db7531d2SJames Smart 	bf_set(wqe_reqtag, &wqe->abort_cmd.wqe_com, pwqeq->iotag);
3370db7531d2SJames Smart 
3371db7531d2SJames Smart 	bf_set(wqe_qosd, &wqe->abort_cmd.wqe_com, 1);
3372db7531d2SJames Smart 	bf_set(wqe_lenloc, &wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
3373db7531d2SJames Smart 
3374db7531d2SJames Smart 	bf_set(wqe_cmd_type, &wqe->abort_cmd.wqe_com, OTHER_COMMAND);
3375db7531d2SJames Smart 	bf_set(wqe_wqec, &wqe->abort_cmd.wqe_com, 1);
3376db7531d2SJames Smart 	bf_set(wqe_cqid, &wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
3377db7531d2SJames Smart }
3378db7531d2SJames Smart 
3379d613b6a7SJames Smart static int
3380d613b6a7SJames Smart lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
33817cacae2aSJames Smart 			       struct lpfc_async_xchg_ctx *ctxp,
3382d613b6a7SJames Smart 			       uint32_t sid, uint16_t xri)
3383d613b6a7SJames Smart {
3384d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3385d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3386d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3387d613b6a7SJames Smart 	unsigned long flags;
338851f8e43eSJames Smart 	u8 opt;
3389d613b6a7SJames Smart 	int rc;
3390d613b6a7SJames Smart 
3391d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3392d613b6a7SJames Smart 	if (!ctxp->wqeq) {
33936c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3394d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3395d613b6a7SJames Smart 	}
3396d613b6a7SJames Smart 
3397d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3398307e3380SJames Smart 	if (!ndlp ||
3399d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3400d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3401d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3402372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
340386c67379SJames Smart 				"6160 Drop ABORT - wrong NDLP state x%x.\n",
3404b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3405d613b6a7SJames Smart 
3406d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3407c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
34087b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3409c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3410d613b6a7SJames Smart 		return 0;
3411d613b6a7SJames Smart 	}
3412d613b6a7SJames Smart 
3413d613b6a7SJames Smart 	/* Issue ABTS for this WQE based on iotag */
3414d613b6a7SJames Smart 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
3415c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3416d613b6a7SJames Smart 	if (!ctxp->abort_wqeq) {
3417547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3418372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
341986c67379SJames Smart 				"6161 ABORT failed: No wqeqs: "
3420d613b6a7SJames Smart 				"xri: x%x\n", ctxp->oxid);
3421d613b6a7SJames Smart 		/* No failure to an ABTS request. */
34227b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3423c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3424d613b6a7SJames Smart 		return 0;
3425d613b6a7SJames Smart 	}
3426d613b6a7SJames Smart 	abts_wqeq = ctxp->abort_wqeq;
34277b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_ABORT;
34287b7f551bSJames Smart 	opt = (ctxp->flag & LPFC_NVME_ABTS_RCV) ? INHIBIT_ABORT : 0;
3429c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3430d613b6a7SJames Smart 
3431d613b6a7SJames Smart 	/* Announce entry to new IO submit field. */
343286c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
343386c67379SJames Smart 			"6162 ABORT Request to rport DID x%06x "
3434d613b6a7SJames Smart 			"for xri x%x x%x\n",
3435d613b6a7SJames Smart 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
3436d613b6a7SJames Smart 
3437d613b6a7SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
3438d613b6a7SJames Smart 	 * cleared when the reset is complete and rings reestablished.
3439d613b6a7SJames Smart 	 */
3440d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3441d613b6a7SJames Smart 	/* driver queued commands are in process of being flushed */
3442c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
3443d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3444547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3445372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3446d613b6a7SJames Smart 				"6163 Driver in reset cleanup - flushing "
3447d613b6a7SJames Smart 				"NVME Req now. hba_flag x%x oxid x%x\n",
3448d613b6a7SJames Smart 				phba->hba_flag, ctxp->oxid);
3449d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3450c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
34517b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3452c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3453d613b6a7SJames Smart 		return 0;
3454d613b6a7SJames Smart 	}
3455d613b6a7SJames Smart 
3456d613b6a7SJames Smart 	/* Outstanding abort is in progress */
3457a680a929SJames Smart 	if (abts_wqeq->cmd_flag & LPFC_DRIVER_ABORTED) {
3458d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3459547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3460372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3461d613b6a7SJames Smart 				"6164 Outstanding NVME I/O Abort Request "
3462d613b6a7SJames Smart 				"still pending on oxid x%x\n",
3463d613b6a7SJames Smart 				ctxp->oxid);
3464d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3465c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
34667b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3467c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3468d613b6a7SJames Smart 		return 0;
3469d613b6a7SJames Smart 	}
3470d613b6a7SJames Smart 
3471d613b6a7SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
3472a680a929SJames Smart 	abts_wqeq->cmd_flag |= LPFC_DRIVER_ABORTED;
3473d613b6a7SJames Smart 
3474db7531d2SJames Smart 	lpfc_nvmet_prep_abort_wqe(abts_wqeq, ctxp->wqeq->sli4_xritag, opt);
3475d613b6a7SJames Smart 
3476d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
3477d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
3478a680a929SJames Smart 	abts_wqeq->cmd_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
3479a680a929SJames Smart 	abts_wqeq->cmd_flag |= LPFC_IO_NVME;
3480d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
34814550f9c7SJames Smart 	abts_wqeq->vport = phba->pport;
34821fbf9742SJames Smart 	if (!ctxp->hdwq)
34831fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
34841fbf9742SJames Smart 
34851fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3486d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3487547077a4SJames Smart 	if (rc == WQE_SUCCESS) {
3488547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_sol);
3489d613b6a7SJames Smart 		return 0;
3490547077a4SJames Smart 	}
3491d613b6a7SJames Smart 
3492547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3493c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
34947b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3495c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3496d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3497372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
349886c67379SJames Smart 			"6166 Failed ABORT issue_wqe with status x%x "
3499d613b6a7SJames Smart 			"for oxid x%x.\n",
3500d613b6a7SJames Smart 			rc, ctxp->oxid);
3501d613b6a7SJames Smart 	return 1;
3502d613b6a7SJames Smart }
3503d613b6a7SJames Smart 
3504d613b6a7SJames Smart static int
3505d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
35067cacae2aSJames Smart 				 struct lpfc_async_xchg_ctx *ctxp,
3507d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
3508d613b6a7SJames Smart {
3509d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3510d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3511d613b6a7SJames Smart 	unsigned long flags;
3512c160c0f8SJames Smart 	bool released = false;
3513d613b6a7SJames Smart 	int rc;
3514d613b6a7SJames Smart 
3515d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3516d613b6a7SJames Smart 	if (!ctxp->wqeq) {
35176c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3518d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3519d613b6a7SJames Smart 	}
3520d613b6a7SJames Smart 
35217b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_FREE) {
3522372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3523ce1b591cSJames Smart 				"6417 NVMET ABORT ctx freed %d %d oxid x%x\n",
3524ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
3525ce1b591cSJames Smart 		rc = WQE_BUSY;
3526ce1b591cSJames Smart 		goto aerr;
3527ce1b591cSJames Smart 	}
35287b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_ABORT;
3529ce1b591cSJames Smart 	ctxp->entry_cnt++;
3530d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
3531d613b6a7SJames Smart 	if (rc == 0)
3532d613b6a7SJames Smart 		goto aerr;
3533d613b6a7SJames Smart 
3534d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3535d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
3536a680a929SJames Smart 	abts_wqeq->cmd_cmpl = lpfc_nvmet_unsol_fcp_abort_cmp;
3537a680a929SJames Smart 	abts_wqeq->cmd_flag |= LPFC_IO_NVMET;
35381fbf9742SJames Smart 	if (!ctxp->hdwq)
35391fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
35401fbf9742SJames Smart 
35411fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3542d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3543d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3544d613b6a7SJames Smart 		return 0;
3545d613b6a7SJames Smart 	}
3546d613b6a7SJames Smart 
3547d613b6a7SJames Smart aerr:
354868c9b55dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
35497b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_CTX_RLS) {
3550c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
355179d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3552c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
3553c160c0f8SJames Smart 		released = true;
3554c160c0f8SJames Smart 	}
35557b7f551bSJames Smart 	ctxp->flag &= ~(LPFC_NVME_ABORT_OP | LPFC_NVME_CTX_RLS);
355668c9b55dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
355768c9b55dSJames Smart 
3558d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3559372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
356079d8c4ceSJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x "
356179d8c4ceSJames Smart 			"(%x)\n",
356279d8c4ceSJames Smart 			ctxp->oxid, rc, released);
3563c160c0f8SJames Smart 	if (released)
356468c9b55dSJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3565d613b6a7SJames Smart 	return 1;
3566d613b6a7SJames Smart }
3567d613b6a7SJames Smart 
35683a8070c5SJames Smart /**
35693a8070c5SJames Smart  * lpfc_nvme_unsol_ls_issue_abort - issue ABTS on an exchange received
35703a8070c5SJames Smart  *        via async frame receive where the frame is not handled.
35713a8070c5SJames Smart  * @phba: pointer to adapter structure
35723a8070c5SJames Smart  * @ctxp: pointer to the asynchronously received received sequence
35733a8070c5SJames Smart  * @sid: address of the remote port to send the ABTS to
35743a8070c5SJames Smart  * @xri: oxid value to for the ABTS (other side's exchange id).
35753a8070c5SJames Smart  **/
35763a8070c5SJames Smart int
35773a8070c5SJames Smart lpfc_nvme_unsol_ls_issue_abort(struct lpfc_hba *phba,
35787cacae2aSJames Smart 				struct lpfc_async_xchg_ctx *ctxp,
3579d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
3580d613b6a7SJames Smart {
3581fcdd14b8SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = NULL;
3582d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3583d613b6a7SJames Smart 	unsigned long flags;
3584d613b6a7SJames Smart 	int rc;
3585d613b6a7SJames Smart 
35867b7f551bSJames Smart 	if ((ctxp->state == LPFC_NVME_STE_LS_RCV && ctxp->entry_cnt == 1) ||
35877b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_LS_RSP && ctxp->entry_cnt == 2)) {
35887b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_LS_ABORT;
3589ce1b591cSJames Smart 		ctxp->entry_cnt++;
3590ce1b591cSJames Smart 	} else {
3591372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3592ce1b591cSJames Smart 				"6418 NVMET LS abort state mismatch "
3593ce1b591cSJames Smart 				"IO x%x: %d %d\n",
3594ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
35957b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_LS_ABORT;
3596ce1b591cSJames Smart 	}
3597ce1b591cSJames Smart 
3598fcdd14b8SJames Smart 	if (phba->nvmet_support && phba->targetport)
3599d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3600fcdd14b8SJames Smart 
3601d613b6a7SJames Smart 	if (!ctxp->wqeq) {
3602d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
3603d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
3604d613b6a7SJames Smart 		if (!ctxp->wqeq) {
3605372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3606d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
3607d613b6a7SJames Smart 					"xri: x%x\n", xri);
3608d613b6a7SJames Smart 			/* No failure to an ABTS request. */
3609d613b6a7SJames Smart 			kfree(ctxp);
3610d613b6a7SJames Smart 			return 0;
3611d613b6a7SJames Smart 		}
3612d613b6a7SJames Smart 	}
3613d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
36146c621a22SJames Smart 
3615ce1b591cSJames Smart 	if (lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri) == 0) {
3616ce1b591cSJames Smart 		rc = WQE_BUSY;
3617ce1b591cSJames Smart 		goto out;
3618ce1b591cSJames Smart 	}
3619d613b6a7SJames Smart 
3620d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3621a680a929SJames Smart 	abts_wqeq->cmd_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
3622a680a929SJames Smart 	abts_wqeq->cmd_flag |=  LPFC_IO_NVME_LS;
36231fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3624d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3625d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3626fcdd14b8SJames Smart 		if (tgtp)
3627547077a4SJames Smart 			atomic_inc(&tgtp->xmt_abort_unsol);
3628d613b6a7SJames Smart 		return 0;
3629d613b6a7SJames Smart 	}
3630ce1b591cSJames Smart out:
3631fcdd14b8SJames Smart 	if (tgtp)
3632d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3633d613b6a7SJames Smart 	abts_wqeq->context2 = NULL;
3634d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
3635d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3636372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3637d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
36384e57e0b9SJames Smart 	return 1;
3639d613b6a7SJames Smart }
36404c2805aaSJames Smart 
36414c2805aaSJames Smart /**
36424c2805aaSJames Smart  * lpfc_nvmet_invalidate_host
36434c2805aaSJames Smart  *
3644d3cb79fcSLee Jones  * @phba: pointer to the driver instance bound to an adapter port.
3645d3cb79fcSLee Jones  * @ndlp: pointer to an lpfc_nodelist type
36464c2805aaSJames Smart  *
36474c2805aaSJames Smart  * This routine upcalls the nvmet transport to invalidate an NVME
36484c2805aaSJames Smart  * host to which this target instance had active connections.
36494c2805aaSJames Smart  */
36504c2805aaSJames Smart void
36514c2805aaSJames Smart lpfc_nvmet_invalidate_host(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
36524c2805aaSJames Smart {
3653243156c0SJames Smart 	u32 ndlp_has_hh;
36544c2805aaSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
36554c2805aaSJames Smart 
3656243156c0SJames Smart 	lpfc_printf_log(phba, KERN_INFO,
3657243156c0SJames Smart 			LOG_NVME | LOG_NVME_ABTS | LOG_NVME_DISC,
36584c2805aaSJames Smart 			"6203 Invalidating hosthandle x%px\n",
36594c2805aaSJames Smart 			ndlp);
36604c2805aaSJames Smart 
36614c2805aaSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
36624c2805aaSJames Smart 	atomic_set(&tgtp->state, LPFC_NVMET_INV_HOST_ACTIVE);
36634c2805aaSJames Smart 
3664243156c0SJames Smart 	spin_lock_irq(&ndlp->lock);
3665243156c0SJames Smart 	ndlp_has_hh = ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH;
3666243156c0SJames Smart 	spin_unlock_irq(&ndlp->lock);
3667243156c0SJames Smart 
3668243156c0SJames Smart 	/* Do not invalidate any nodes that do not have a hosthandle.
3669243156c0SJames Smart 	 * The host_release callbk will cause a node reference
3670243156c0SJames Smart 	 * count imbalance and a crash.
3671243156c0SJames Smart 	 */
3672243156c0SJames Smart 	if (!ndlp_has_hh) {
3673243156c0SJames Smart 		lpfc_printf_log(phba, KERN_INFO,
3674243156c0SJames Smart 				LOG_NVME | LOG_NVME_ABTS | LOG_NVME_DISC,
3675243156c0SJames Smart 				"6204 Skip invalidate on node x%px DID x%x\n",
3676243156c0SJames Smart 				ndlp, ndlp->nlp_DID);
3677243156c0SJames Smart 		return;
3678243156c0SJames Smart 	}
3679243156c0SJames Smart 
36804c2805aaSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
36814c2805aaSJames Smart 	/* Need to get the nvmet_fc_target_port pointer here.*/
36824c2805aaSJames Smart 	nvmet_fc_invalidate_host(phba->targetport, ndlp);
36834c2805aaSJames Smart #endif
36844c2805aaSJames Smart }
3685