xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision e7f40349)
1d613b6a7SJames Smart /*******************************************************************
2d613b6a7SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
3d613b6a7SJames Smart  * Fibre Channsel Host Bus Adapters.                               *
40d041215SJames Smart  * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
54ae2ebdeSJames Smart  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6d613b6a7SJames Smart  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7d613b6a7SJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
8d080abe0SJames Smart  * www.broadcom.com                                                *
9d613b6a7SJames Smart  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10d613b6a7SJames Smart  *                                                                 *
11d613b6a7SJames Smart  * This program is free software; you can redistribute it and/or   *
12d613b6a7SJames Smart  * modify it under the terms of version 2 of the GNU General       *
13d613b6a7SJames Smart  * Public License as published by the Free Software Foundation.    *
14d613b6a7SJames Smart  * This program is distributed in the hope that it will be useful. *
15d613b6a7SJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16d613b6a7SJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17d613b6a7SJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18d613b6a7SJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19d613b6a7SJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20d613b6a7SJames Smart  * more details, a copy of which can be found in the file COPYING  *
21d613b6a7SJames Smart  * included with this package.                                     *
22d613b6a7SJames Smart  ********************************************************************/
23d613b6a7SJames Smart #include <linux/pci.h>
24d613b6a7SJames Smart #include <linux/slab.h>
25d613b6a7SJames Smart #include <linux/interrupt.h>
26d613b6a7SJames Smart #include <linux/delay.h>
27d613b6a7SJames Smart #include <asm/unaligned.h>
28d613b6a7SJames Smart #include <linux/crc-t10dif.h>
29d613b6a7SJames Smart #include <net/checksum.h>
30d613b6a7SJames Smart 
31d613b6a7SJames Smart #include <scsi/scsi.h>
32d613b6a7SJames Smart #include <scsi/scsi_device.h>
33d613b6a7SJames Smart #include <scsi/scsi_eh.h>
34d613b6a7SJames Smart #include <scsi/scsi_host.h>
35d613b6a7SJames Smart #include <scsi/scsi_tcq.h>
36d613b6a7SJames Smart #include <scsi/scsi_transport_fc.h>
37d613b6a7SJames Smart #include <scsi/fc/fc_fs.h>
38d613b6a7SJames Smart 
398d731d1aSJames Smart #include <linux/nvme.h>
40d613b6a7SJames Smart #include <linux/nvme-fc-driver.h>
41cbc5de1bSJames Smart #include <linux/nvme-fc.h>
42d613b6a7SJames Smart 
43d613b6a7SJames Smart #include "lpfc_version.h"
44d613b6a7SJames Smart #include "lpfc_hw4.h"
45d613b6a7SJames Smart #include "lpfc_hw.h"
46d613b6a7SJames Smart #include "lpfc_sli.h"
47d613b6a7SJames Smart #include "lpfc_sli4.h"
48d613b6a7SJames Smart #include "lpfc_nl.h"
49d613b6a7SJames Smart #include "lpfc_disc.h"
50d613b6a7SJames Smart #include "lpfc.h"
51d613b6a7SJames Smart #include "lpfc_scsi.h"
52d613b6a7SJames Smart #include "lpfc_nvme.h"
53d613b6a7SJames Smart #include "lpfc_nvmet.h"
54d613b6a7SJames Smart #include "lpfc_logmsg.h"
55d613b6a7SJames Smart #include "lpfc_crtn.h"
56d613b6a7SJames Smart #include "lpfc_vport.h"
572b65e182SJames Smart #include "lpfc_debugfs.h"
58d613b6a7SJames Smart 
59d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *,
60d613b6a7SJames Smart 						 struct lpfc_nvmet_rcv_ctx *,
61d613b6a7SJames Smart 						 dma_addr_t rspbuf,
62d613b6a7SJames Smart 						 uint16_t rspsize);
63d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *,
64d613b6a7SJames Smart 						  struct lpfc_nvmet_rcv_ctx *);
65d613b6a7SJames Smart static int lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *,
66d613b6a7SJames Smart 					  struct lpfc_nvmet_rcv_ctx *,
67d613b6a7SJames Smart 					  uint32_t, uint16_t);
68d613b6a7SJames Smart static int lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *,
69d613b6a7SJames Smart 					    struct lpfc_nvmet_rcv_ctx *,
70d613b6a7SJames Smart 					    uint32_t, uint16_t);
71d613b6a7SJames Smart static int lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *,
72d613b6a7SJames Smart 					   struct lpfc_nvmet_rcv_ctx *,
73d613b6a7SJames Smart 					   uint32_t, uint16_t);
746e8e1c14SJames Smart static void lpfc_nvmet_wqfull_flush(struct lpfc_hba *, struct lpfc_queue *,
756e8e1c14SJames Smart 				    struct lpfc_nvmet_rcv_ctx *);
76472e146dSJames Smart static void lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *);
77472e146dSJames Smart 
78472e146dSJames Smart static void lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf);
79d613b6a7SJames Smart 
80fab2e466SColin Ian King static union lpfc_wqe128 lpfc_tsend_cmd_template;
81fab2e466SColin Ian King static union lpfc_wqe128 lpfc_treceive_cmd_template;
82fab2e466SColin Ian King static union lpfc_wqe128 lpfc_trsp_cmd_template;
83bd3061baSJames Smart 
84bd3061baSJames Smart /* Setup WQE templates for NVME IOs */
85bd3061baSJames Smart void
86fab2e466SColin Ian King lpfc_nvmet_cmd_template(void)
87bd3061baSJames Smart {
88bd3061baSJames Smart 	union lpfc_wqe128 *wqe;
89bd3061baSJames Smart 
90bd3061baSJames Smart 	/* TSEND template */
91bd3061baSJames Smart 	wqe = &lpfc_tsend_cmd_template;
92bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
93bd3061baSJames Smart 
94bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
95bd3061baSJames Smart 
96bd3061baSJames Smart 	/* Word 3 - payload_offset_len is zero */
97bd3061baSJames Smart 
98bd3061baSJames Smart 	/* Word 4 - relative_offset is variable */
99bd3061baSJames Smart 
100bd3061baSJames Smart 	/* Word 5 - is zero */
101bd3061baSJames Smart 
102bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
103bd3061baSJames Smart 
104bd3061baSJames Smart 	/* Word 7 - wqe_ar is variable */
105bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_tsend.wqe_com, CMD_FCP_TSEND64_WQE);
106bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_tsend.wqe_com, PARM_REL_OFF);
107bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_tsend.wqe_com, CLASS3);
108bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_tsend.wqe_com, SLI4_CT_RPI);
109bd3061baSJames Smart 	bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 1);
110bd3061baSJames Smart 
111bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
112bd3061baSJames Smart 
113bd3061baSJames Smart 	/* Word 9  - reqtag, rcvoxid is variable */
114bd3061baSJames Smart 
115bd3061baSJames Smart 	/* Word 10 - wqes, xc is variable */
116bd3061baSJames Smart 	bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
117bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_tsend.wqe_com, 1);
118bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
119bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 1);
120bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_tsend.wqe_com, LPFC_WQE_IOD_WRITE);
121bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_tsend.wqe_com, LPFC_WQE_LENLOC_WORD12);
122bd3061baSJames Smart 
123bd3061baSJames Smart 	/* Word 11 - sup, irsp, irsplen is variable */
124bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_tsend.wqe_com, FCP_COMMAND_TSEND);
125bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_tsend.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
126bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
127bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
128bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
129bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_tsend.wqe_com, 0);
130bd3061baSJames Smart 
131bd3061baSJames Smart 	/* Word 12 - fcp_data_len is variable */
132bd3061baSJames Smart 
133bd3061baSJames Smart 	/* Word 13, 14, 15 - PBDE is zero */
134bd3061baSJames Smart 
135bd3061baSJames Smart 	/* TRECEIVE template */
136bd3061baSJames Smart 	wqe = &lpfc_treceive_cmd_template;
137bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
138bd3061baSJames Smart 
139bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
140bd3061baSJames Smart 
141bd3061baSJames Smart 	/* Word 3 */
142bd3061baSJames Smart 	wqe->fcp_treceive.payload_offset_len = TXRDY_PAYLOAD_LEN;
143bd3061baSJames Smart 
144bd3061baSJames Smart 	/* Word 4 - relative_offset is variable */
145bd3061baSJames Smart 
146bd3061baSJames Smart 	/* Word 5 - is zero */
147bd3061baSJames Smart 
148bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
149bd3061baSJames Smart 
150bd3061baSJames Smart 	/* Word 7 */
151bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_treceive.wqe_com, CMD_FCP_TRECEIVE64_WQE);
152bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_treceive.wqe_com, PARM_REL_OFF);
153bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_treceive.wqe_com, CLASS3);
154bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_treceive.wqe_com, SLI4_CT_RPI);
155bd3061baSJames Smart 	bf_set(wqe_ar, &wqe->fcp_treceive.wqe_com, 0);
156bd3061baSJames Smart 
157bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
158bd3061baSJames Smart 
159bd3061baSJames Smart 	/* Word 9  - reqtag, rcvoxid is variable */
160bd3061baSJames Smart 
161bd3061baSJames Smart 	/* Word 10 - xc is variable */
162bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_treceive.wqe_com, 1);
163bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_treceive.wqe_com, 0);
164bd3061baSJames Smart 	bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
165bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_treceive.wqe_com, LPFC_WQE_IOD_READ);
166bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_treceive.wqe_com, LPFC_WQE_LENLOC_WORD12);
167bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 1);
168bd3061baSJames Smart 
169bd3061baSJames Smart 	/* Word 11 - pbde is variable */
170bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_treceive.wqe_com, FCP_COMMAND_TRECEIVE);
171bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_treceive.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
172bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_treceive.wqe_com, 0);
173bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_treceive.wqe_com, 0);
174bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_treceive.wqe_com, 0);
175bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 1);
176bd3061baSJames Smart 
177bd3061baSJames Smart 	/* Word 12 - fcp_data_len is variable */
178bd3061baSJames Smart 
179bd3061baSJames Smart 	/* Word 13, 14, 15 - PBDE is variable */
180bd3061baSJames Smart 
181bd3061baSJames Smart 	/* TRSP template */
182bd3061baSJames Smart 	wqe = &lpfc_trsp_cmd_template;
183bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
184bd3061baSJames Smart 
185bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
186bd3061baSJames Smart 
187bd3061baSJames Smart 	/* Word 3 - response_len is variable */
188bd3061baSJames Smart 
189bd3061baSJames Smart 	/* Word 4, 5 - is zero */
190bd3061baSJames Smart 
191bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
192bd3061baSJames Smart 
193bd3061baSJames Smart 	/* Word 7 */
194bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_trsp.wqe_com, CMD_FCP_TRSP64_WQE);
195bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_trsp.wqe_com, PARM_UNUSED);
196bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_trsp.wqe_com, CLASS3);
197bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_trsp.wqe_com, SLI4_CT_RPI);
198bd3061baSJames Smart 	bf_set(wqe_ag, &wqe->fcp_trsp.wqe_com, 1); /* wqe_ar */
199bd3061baSJames Smart 
200bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
201bd3061baSJames Smart 
202bd3061baSJames Smart 	/* Word 9  - reqtag is variable */
203bd3061baSJames Smart 
204bd3061baSJames Smart 	/* Word 10 wqes, xc is variable */
205bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_trsp.wqe_com, 1);
206bd3061baSJames Smart 	bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
207bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 0);
208bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 0);
209bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_trsp.wqe_com, LPFC_WQE_IOD_NONE);
210bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_trsp.wqe_com, LPFC_WQE_LENLOC_WORD3);
211bd3061baSJames Smart 
212bd3061baSJames Smart 	/* Word 11 irsp, irsplen is variable */
213bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_trsp.wqe_com, FCP_COMMAND_TRSP);
214bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_trsp.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
215bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_trsp.wqe_com, 0);
216bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 0);
217bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com, 0);
218bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_trsp.wqe_com, 0);
219bd3061baSJames Smart 
220bd3061baSJames Smart 	/* Word 12, 13, 14, 15 - is zero */
221bd3061baSJames Smart }
222bd3061baSJames Smart 
223336df6ebSNathan Chancellor #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
224d7b761b0SYueHaibing static struct lpfc_nvmet_rcv_ctx *
22579d8c4ceSJames Smart lpfc_nvmet_get_ctx_for_xri(struct lpfc_hba *phba, u16 xri)
22679d8c4ceSJames Smart {
22779d8c4ceSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
22879d8c4ceSJames Smart 	unsigned long iflag;
22979d8c4ceSJames Smart 	bool found = false;
23079d8c4ceSJames Smart 
23179d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
23279d8c4ceSJames Smart 	list_for_each_entry(ctxp, &phba->sli4_hba.t_active_ctx_list, list) {
23379d8c4ceSJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
23479d8c4ceSJames Smart 			continue;
23579d8c4ceSJames Smart 
23679d8c4ceSJames Smart 		found = true;
23779d8c4ceSJames Smart 		break;
23879d8c4ceSJames Smart 	}
23979d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
24079d8c4ceSJames Smart 	if (found)
24179d8c4ceSJames Smart 		return ctxp;
24279d8c4ceSJames Smart 
24379d8c4ceSJames Smart 	return NULL;
24479d8c4ceSJames Smart }
24579d8c4ceSJames Smart 
246d7b761b0SYueHaibing static struct lpfc_nvmet_rcv_ctx *
24779d8c4ceSJames Smart lpfc_nvmet_get_ctx_for_oxid(struct lpfc_hba *phba, u16 oxid, u32 sid)
24879d8c4ceSJames Smart {
24979d8c4ceSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
25079d8c4ceSJames Smart 	unsigned long iflag;
25179d8c4ceSJames Smart 	bool found = false;
25279d8c4ceSJames Smart 
25379d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
25479d8c4ceSJames Smart 	list_for_each_entry(ctxp, &phba->sli4_hba.t_active_ctx_list, list) {
25579d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
25679d8c4ceSJames Smart 			continue;
25779d8c4ceSJames Smart 
25879d8c4ceSJames Smart 		found = true;
25979d8c4ceSJames Smart 		break;
26079d8c4ceSJames Smart 	}
26179d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
26279d8c4ceSJames Smart 	if (found)
26379d8c4ceSJames Smart 		return ctxp;
26479d8c4ceSJames Smart 
26579d8c4ceSJames Smart 	return NULL;
26679d8c4ceSJames Smart }
267336df6ebSNathan Chancellor #endif
26879d8c4ceSJames Smart 
2693999df75SBart Van Assche static void
27086c67379SJames Smart lpfc_nvmet_defer_release(struct lpfc_hba *phba, struct lpfc_nvmet_rcv_ctx *ctxp)
27186c67379SJames Smart {
272472e146dSJames Smart 	lockdep_assert_held(&ctxp->ctxlock);
27386c67379SJames Smart 
274e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
27579d8c4ceSJames Smart 			"6313 NVMET Defer ctx release oxid x%x flg x%x\n",
27686c67379SJames Smart 			ctxp->oxid, ctxp->flag);
27786c67379SJames Smart 
278472e146dSJames Smart 	if (ctxp->flag & LPFC_NVMET_CTX_RLS)
27986c67379SJames Smart 		return;
280472e146dSJames Smart 
28186c67379SJames Smart 	ctxp->flag |= LPFC_NVMET_CTX_RLS;
28279d8c4ceSJames Smart 	spin_lock(&phba->sli4_hba.t_active_list_lock);
28379d8c4ceSJames Smart 	list_del(&ctxp->list);
28479d8c4ceSJames Smart 	spin_unlock(&phba->sli4_hba.t_active_list_lock);
285472e146dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
28686c67379SJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
287472e146dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
28886c67379SJames Smart }
28986c67379SJames Smart 
290d613b6a7SJames Smart /**
291d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
292d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
293d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
294d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
295d613b6a7SJames Smart  *
296d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
297d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME LS commands
298d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
299d613b6a7SJames Smart  **/
300d613b6a7SJames Smart static void
301d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
302d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
303d613b6a7SJames Smart {
304d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
305d613b6a7SJames Smart 	struct nvmefc_tgt_ls_req *rsp;
306d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
307d613b6a7SJames Smart 	uint32_t status, result;
308d613b6a7SJames Smart 
309d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
310d613b6a7SJames Smart 	result = wcqe->parameter;
311ce1b591cSJames Smart 	ctxp = cmdwqe->context2;
312ce1b591cSJames Smart 
313ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_LS_RSP || ctxp->entry_cnt != 2) {
314ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
315ce1b591cSJames Smart 				"6410 NVMET LS cmpl state mismatch IO x%x: "
316ce1b591cSJames Smart 				"%d %d\n",
317ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
318ce1b591cSJames Smart 	}
319ce1b591cSJames Smart 
320d613b6a7SJames Smart 	if (!phba->targetport)
321d613b6a7SJames Smart 		goto out;
322d613b6a7SJames Smart 
323d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
324d613b6a7SJames Smart 
3254b056682SJames Smart 	if (tgtp) {
3264b056682SJames Smart 		if (status) {
327d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_error);
3288ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
3294b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_aborted);
3304b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
3314b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_xb_set);
3324b056682SJames Smart 		} else {
333d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
3344b056682SJames Smart 		}
3354b056682SJames Smart 	}
336d613b6a7SJames Smart 
337d613b6a7SJames Smart out:
338d613b6a7SJames Smart 	rsp = &ctxp->ctx.ls_req;
339d613b6a7SJames Smart 
3402b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  CMPL: xri x%x stat x%x result x%x\n",
3412b65e182SJames Smart 			 ctxp->oxid, status, result);
3422b65e182SJames Smart 
343d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
344ce1b591cSJames Smart 			"6038 NVMET LS rsp cmpl: %d %d oxid x%x\n",
345ce1b591cSJames Smart 			status, result, ctxp->oxid);
346d613b6a7SJames Smart 
347d613b6a7SJames Smart 	lpfc_nlp_put(cmdwqe->context1);
348d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
349d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
350d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
351d613b6a7SJames Smart 	rsp->done(rsp);
352d613b6a7SJames Smart 	kfree(ctxp);
353d613b6a7SJames Smart }
354d613b6a7SJames Smart 
355d613b6a7SJames Smart /**
3566c621a22SJames Smart  * lpfc_nvmet_ctxbuf_post - Repost a NVMET RQ DMA buffer and clean up context
357d613b6a7SJames Smart  * @phba: HBA buffer is associated with
358d613b6a7SJames Smart  * @ctxp: context to clean up
359d613b6a7SJames Smart  * @mp: Buffer to free
360d613b6a7SJames Smart  *
361d613b6a7SJames Smart  * Description: Frees the given DMA buffer in the appropriate way given by
362d613b6a7SJames Smart  * reposting it to its associated RQ so it can be reused.
363d613b6a7SJames Smart  *
364d613b6a7SJames Smart  * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
365d613b6a7SJames Smart  *
366d613b6a7SJames Smart  * Returns: None
367d613b6a7SJames Smart  **/
368d613b6a7SJames Smart void
3696c621a22SJames Smart lpfc_nvmet_ctxbuf_post(struct lpfc_hba *phba, struct lpfc_nvmet_ctxbuf *ctx_buf)
370d613b6a7SJames Smart {
371eeeb51d8SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
3726c621a22SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
373a8cf5dfeSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
374a8cf5dfeSJames Smart 	struct fc_frame_header *fc_hdr;
375a8cf5dfeSJames Smart 	struct rqb_dmabuf *nvmebuf;
37666d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
377472e146dSJames Smart 	uint32_t size, oxid, sid;
37866d7ce93SDick Kennedy 	int cpu;
3796c621a22SJames Smart 	unsigned long iflag;
38086c67379SJames Smart 
381ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_FREE) {
382ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
383ce1b591cSJames Smart 				"6411 NVMET free, already free IO x%x: %d %d\n",
384ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
385ce1b591cSJames Smart 	}
386472e146dSJames Smart 
387472e146dSJames Smart 	if (ctxp->rqb_buffer) {
388472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
3894767c58aSJames Smart 		nvmebuf = ctxp->rqb_buffer;
3904767c58aSJames Smart 		/* check if freed in another path whilst acquiring lock */
3914767c58aSJames Smart 		if (nvmebuf) {
392472e146dSJames Smart 			ctxp->rqb_buffer = NULL;
393472e146dSJames Smart 			if (ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) {
394472e146dSJames Smart 				ctxp->flag &= ~LPFC_NVMET_CTX_REUSE_WQ;
395472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
3964767c58aSJames Smart 				nvmebuf->hrq->rqbp->rqb_free_buffer(phba,
3974767c58aSJames Smart 								    nvmebuf);
398472e146dSJames Smart 			} else {
399472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4004767c58aSJames Smart 				/* repost */
4014767c58aSJames Smart 				lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
4024767c58aSJames Smart 			}
4034767c58aSJames Smart 		} else {
4044767c58aSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
405472e146dSJames Smart 		}
406472e146dSJames Smart 	}
407d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_FREE;
4086c621a22SJames Smart 
409a8cf5dfeSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
410a8cf5dfeSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
411a8cf5dfeSJames Smart 		list_remove_head(&phba->sli4_hba.lpfc_nvmet_io_wait_list,
412a8cf5dfeSJames Smart 				 nvmebuf, struct rqb_dmabuf,
413a8cf5dfeSJames Smart 				 hbuf.list);
414a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt--;
415a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
416a8cf5dfeSJames Smart 				       iflag);
417a8cf5dfeSJames Smart 
418a8cf5dfeSJames Smart 		fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
419a8cf5dfeSJames Smart 		oxid = be16_to_cpu(fc_hdr->fh_ox_id);
420a8cf5dfeSJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
421a8cf5dfeSJames Smart 		size = nvmebuf->bytes_recv;
422a8cf5dfeSJames Smart 		sid = sli4_sid_from_fc_hdr(fc_hdr);
423a8cf5dfeSJames Smart 
424a8cf5dfeSJames Smart 		ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
425a8cf5dfeSJames Smart 		ctxp->wqeq = NULL;
426a8cf5dfeSJames Smart 		ctxp->offset = 0;
427a8cf5dfeSJames Smart 		ctxp->phba = phba;
428a8cf5dfeSJames Smart 		ctxp->size = size;
429a8cf5dfeSJames Smart 		ctxp->oxid = oxid;
430a8cf5dfeSJames Smart 		ctxp->sid = sid;
431a8cf5dfeSJames Smart 		ctxp->state = LPFC_NVMET_STE_RCV;
432a8cf5dfeSJames Smart 		ctxp->entry_cnt = 1;
433a8cf5dfeSJames Smart 		ctxp->flag = 0;
434a8cf5dfeSJames Smart 		ctxp->ctxbuf = ctx_buf;
435cbc5de1bSJames Smart 		ctxp->rqb_buffer = (void *)nvmebuf;
436a8cf5dfeSJames Smart 		spin_lock_init(&ctxp->ctxlock);
437a8cf5dfeSJames Smart 
438a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
439d74a89aaSJames Smart 		/* NOTE: isr time stamp is stale when context is re-assigned*/
440d74a89aaSJames Smart 		if (ctxp->ts_isr_cmd) {
441d74a89aaSJames Smart 			ctxp->ts_cmd_nvme = 0;
442a8cf5dfeSJames Smart 			ctxp->ts_nvme_data = 0;
443a8cf5dfeSJames Smart 			ctxp->ts_data_wqput = 0;
444a8cf5dfeSJames Smart 			ctxp->ts_isr_data = 0;
445a8cf5dfeSJames Smart 			ctxp->ts_data_nvme = 0;
446a8cf5dfeSJames Smart 			ctxp->ts_nvme_status = 0;
447a8cf5dfeSJames Smart 			ctxp->ts_status_wqput = 0;
448a8cf5dfeSJames Smart 			ctxp->ts_isr_status = 0;
449a8cf5dfeSJames Smart 			ctxp->ts_status_nvme = 0;
450d613b6a7SJames Smart 		}
451a8cf5dfeSJames Smart #endif
452a8cf5dfeSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_in);
453472e146dSJames Smart 
45479d8c4ceSJames Smart 		/* Indicate that a replacement buffer has been posted */
455472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
456472e146dSJames Smart 		ctxp->flag |= LPFC_NVMET_CTX_REUSE_WQ;
457472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
458a8cf5dfeSJames Smart 
459472e146dSJames Smart 		if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
460a8cf5dfeSJames Smart 			atomic_inc(&tgtp->rcv_fcp_cmd_drop);
461472e146dSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
462472e146dSJames Smart 					"6181 Unable to queue deferred work "
463472e146dSJames Smart 					"for oxid x%x. "
464472e146dSJames Smart 					"FCP Drop IO [x%x x%x x%x]\n",
465472e146dSJames Smart 					ctxp->oxid,
466a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_in),
467a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_out),
468a8cf5dfeSJames Smart 					atomic_read(&tgtp->xmt_fcp_release));
469a8cf5dfeSJames Smart 
470472e146dSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
471a8cf5dfeSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
472472e146dSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
473a8cf5dfeSJames Smart 			lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
474472e146dSJames Smart 		}
475a8cf5dfeSJames Smart 		return;
476a8cf5dfeSJames Smart 	}
477a8cf5dfeSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
478a8cf5dfeSJames Smart 
47966d7ce93SDick Kennedy 	/*
48066d7ce93SDick Kennedy 	 * Use the CPU context list, from the MRQ the IO was received on
48166d7ce93SDick Kennedy 	 * (ctxp->idx), to save context structure.
48266d7ce93SDick Kennedy 	 */
48379d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
48479d8c4ceSJames Smart 	list_del_init(&ctxp->list);
48579d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
486d6d189ceSBart Van Assche 	cpu = raw_smp_processor_id();
48766d7ce93SDick Kennedy 	infop = lpfc_get_ctx_list(phba, cpu, ctxp->idx);
48866d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, iflag);
48966d7ce93SDick Kennedy 	list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
49066d7ce93SDick Kennedy 	infop->nvmet_ctx_list_cnt++;
49166d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, iflag);
492eeeb51d8SJames Smart #endif
493d613b6a7SJames Smart }
494d613b6a7SJames Smart 
4952b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4962b65e182SJames Smart static void
4972b65e182SJames Smart lpfc_nvmet_ktime(struct lpfc_hba *phba,
4982b65e182SJames Smart 		 struct lpfc_nvmet_rcv_ctx *ctxp)
4992b65e182SJames Smart {
5002b65e182SJames Smart 	uint64_t seg1, seg2, seg3, seg4, seg5;
5012b65e182SJames Smart 	uint64_t seg6, seg7, seg8, seg9, seg10;
502c8a4ce0bSDick Kennedy 	uint64_t segsum;
5032b65e182SJames Smart 
5042b65e182SJames Smart 	if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
5052b65e182SJames Smart 	    !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
5062b65e182SJames Smart 	    !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
5072b65e182SJames Smart 	    !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
5082b65e182SJames Smart 	    !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
5092b65e182SJames Smart 		return;
5102b65e182SJames Smart 
511c8a4ce0bSDick Kennedy 	if (ctxp->ts_status_nvme < ctxp->ts_isr_cmd)
512c8a4ce0bSDick Kennedy 		return;
5132b65e182SJames Smart 	if (ctxp->ts_isr_cmd  > ctxp->ts_cmd_nvme)
5142b65e182SJames Smart 		return;
5152b65e182SJames Smart 	if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
5162b65e182SJames Smart 		return;
5172b65e182SJames Smart 	if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
5182b65e182SJames Smart 		return;
5192b65e182SJames Smart 	if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
5202b65e182SJames Smart 		return;
5212b65e182SJames Smart 	if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
5222b65e182SJames Smart 		return;
5232b65e182SJames Smart 	if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
5242b65e182SJames Smart 		return;
5252b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
5262b65e182SJames Smart 		return;
5272b65e182SJames Smart 	if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
5282b65e182SJames Smart 		return;
5292b65e182SJames Smart 	if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
5302b65e182SJames Smart 		return;
5312b65e182SJames Smart 	/*
5322b65e182SJames Smart 	 * Segment 1 - Time from FCP command received by MSI-X ISR
5332b65e182SJames Smart 	 * to FCP command is passed to NVME Layer.
5342b65e182SJames Smart 	 * Segment 2 - Time from FCP command payload handed
5352b65e182SJames Smart 	 * off to NVME Layer to Driver receives a Command op
5362b65e182SJames Smart 	 * from NVME Layer.
5372b65e182SJames Smart 	 * Segment 3 - Time from Driver receives a Command op
5382b65e182SJames Smart 	 * from NVME Layer to Command is put on WQ.
5392b65e182SJames Smart 	 * Segment 4 - Time from Driver WQ put is done
5402b65e182SJames Smart 	 * to MSI-X ISR for Command cmpl.
5412b65e182SJames Smart 	 * Segment 5 - Time from MSI-X ISR for Command cmpl to
5422b65e182SJames Smart 	 * Command cmpl is passed to NVME Layer.
5432b65e182SJames Smart 	 * Segment 6 - Time from Command cmpl is passed to NVME
5442b65e182SJames Smart 	 * Layer to Driver receives a RSP op from NVME Layer.
5452b65e182SJames Smart 	 * Segment 7 - Time from Driver receives a RSP op from
5462b65e182SJames Smart 	 * NVME Layer to WQ put is done on TRSP FCP Status.
5472b65e182SJames Smart 	 * Segment 8 - Time from Driver WQ put is done on TRSP
5482b65e182SJames Smart 	 * FCP Status to MSI-X ISR for TRSP cmpl.
5492b65e182SJames Smart 	 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
5502b65e182SJames Smart 	 * TRSP cmpl is passed to NVME Layer.
5512b65e182SJames Smart 	 * Segment 10 - Time from FCP command received by
5522b65e182SJames Smart 	 * MSI-X ISR to command is completed on wire.
5532b65e182SJames Smart 	 * (Segments 1 thru 8) for READDATA / WRITEDATA
5542b65e182SJames Smart 	 * (Segments 1 thru 4) for READDATA_RSP
5552b65e182SJames Smart 	 */
5562b65e182SJames Smart 	seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
557c8a4ce0bSDick Kennedy 	segsum = seg1;
558c8a4ce0bSDick Kennedy 
559c8a4ce0bSDick Kennedy 	seg2 = ctxp->ts_nvme_data - ctxp->ts_isr_cmd;
560c8a4ce0bSDick Kennedy 	if (segsum > seg2)
561c8a4ce0bSDick Kennedy 		return;
562c8a4ce0bSDick Kennedy 	seg2 -= segsum;
563c8a4ce0bSDick Kennedy 	segsum += seg2;
564c8a4ce0bSDick Kennedy 
565c8a4ce0bSDick Kennedy 	seg3 = ctxp->ts_data_wqput - ctxp->ts_isr_cmd;
566c8a4ce0bSDick Kennedy 	if (segsum > seg3)
567c8a4ce0bSDick Kennedy 		return;
568c8a4ce0bSDick Kennedy 	seg3 -= segsum;
569c8a4ce0bSDick Kennedy 	segsum += seg3;
570c8a4ce0bSDick Kennedy 
571c8a4ce0bSDick Kennedy 	seg4 = ctxp->ts_isr_data - ctxp->ts_isr_cmd;
572c8a4ce0bSDick Kennedy 	if (segsum > seg4)
573c8a4ce0bSDick Kennedy 		return;
574c8a4ce0bSDick Kennedy 	seg4 -= segsum;
575c8a4ce0bSDick Kennedy 	segsum += seg4;
576c8a4ce0bSDick Kennedy 
577c8a4ce0bSDick Kennedy 	seg5 = ctxp->ts_data_nvme - ctxp->ts_isr_cmd;
578c8a4ce0bSDick Kennedy 	if (segsum > seg5)
579c8a4ce0bSDick Kennedy 		return;
580c8a4ce0bSDick Kennedy 	seg5 -= segsum;
581c8a4ce0bSDick Kennedy 	segsum += seg5;
582c8a4ce0bSDick Kennedy 
5832b65e182SJames Smart 
5842b65e182SJames Smart 	/* For auto rsp commands seg6 thru seg10 will be 0 */
5852b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
586c8a4ce0bSDick Kennedy 		seg6 = ctxp->ts_nvme_status - ctxp->ts_isr_cmd;
587c8a4ce0bSDick Kennedy 		if (segsum > seg6)
588c8a4ce0bSDick Kennedy 			return;
589c8a4ce0bSDick Kennedy 		seg6 -= segsum;
590c8a4ce0bSDick Kennedy 		segsum += seg6;
591c8a4ce0bSDick Kennedy 
592c8a4ce0bSDick Kennedy 		seg7 = ctxp->ts_status_wqput - ctxp->ts_isr_cmd;
593c8a4ce0bSDick Kennedy 		if (segsum > seg7)
594c8a4ce0bSDick Kennedy 			return;
595c8a4ce0bSDick Kennedy 		seg7 -= segsum;
596c8a4ce0bSDick Kennedy 		segsum += seg7;
597c8a4ce0bSDick Kennedy 
598c8a4ce0bSDick Kennedy 		seg8 = ctxp->ts_isr_status - ctxp->ts_isr_cmd;
599c8a4ce0bSDick Kennedy 		if (segsum > seg8)
600c8a4ce0bSDick Kennedy 			return;
601c8a4ce0bSDick Kennedy 		seg8 -= segsum;
602c8a4ce0bSDick Kennedy 		segsum += seg8;
603c8a4ce0bSDick Kennedy 
604c8a4ce0bSDick Kennedy 		seg9 = ctxp->ts_status_nvme - ctxp->ts_isr_cmd;
605c8a4ce0bSDick Kennedy 		if (segsum > seg9)
606c8a4ce0bSDick Kennedy 			return;
607c8a4ce0bSDick Kennedy 		seg9 -= segsum;
608c8a4ce0bSDick Kennedy 		segsum += seg9;
609c8a4ce0bSDick Kennedy 
610c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_status < ctxp->ts_isr_cmd)
611c8a4ce0bSDick Kennedy 			return;
6122b65e182SJames Smart 		seg10 = (ctxp->ts_isr_status -
6132b65e182SJames Smart 			ctxp->ts_isr_cmd);
6142b65e182SJames Smart 	} else {
615c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_data < ctxp->ts_isr_cmd)
616c8a4ce0bSDick Kennedy 			return;
6172b65e182SJames Smart 		seg6 =  0;
6182b65e182SJames Smart 		seg7 =  0;
6192b65e182SJames Smart 		seg8 =  0;
6202b65e182SJames Smart 		seg9 =  0;
6212b65e182SJames Smart 		seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
6222b65e182SJames Smart 	}
6232b65e182SJames Smart 
6242b65e182SJames Smart 	phba->ktime_seg1_total += seg1;
6252b65e182SJames Smart 	if (seg1 < phba->ktime_seg1_min)
6262b65e182SJames Smart 		phba->ktime_seg1_min = seg1;
6272b65e182SJames Smart 	else if (seg1 > phba->ktime_seg1_max)
6282b65e182SJames Smart 		phba->ktime_seg1_max = seg1;
6292b65e182SJames Smart 
6302b65e182SJames Smart 	phba->ktime_seg2_total += seg2;
6312b65e182SJames Smart 	if (seg2 < phba->ktime_seg2_min)
6322b65e182SJames Smart 		phba->ktime_seg2_min = seg2;
6332b65e182SJames Smart 	else if (seg2 > phba->ktime_seg2_max)
6342b65e182SJames Smart 		phba->ktime_seg2_max = seg2;
6352b65e182SJames Smart 
6362b65e182SJames Smart 	phba->ktime_seg3_total += seg3;
6372b65e182SJames Smart 	if (seg3 < phba->ktime_seg3_min)
6382b65e182SJames Smart 		phba->ktime_seg3_min = seg3;
6392b65e182SJames Smart 	else if (seg3 > phba->ktime_seg3_max)
6402b65e182SJames Smart 		phba->ktime_seg3_max = seg3;
6412b65e182SJames Smart 
6422b65e182SJames Smart 	phba->ktime_seg4_total += seg4;
6432b65e182SJames Smart 	if (seg4 < phba->ktime_seg4_min)
6442b65e182SJames Smart 		phba->ktime_seg4_min = seg4;
6452b65e182SJames Smart 	else if (seg4 > phba->ktime_seg4_max)
6462b65e182SJames Smart 		phba->ktime_seg4_max = seg4;
6472b65e182SJames Smart 
6482b65e182SJames Smart 	phba->ktime_seg5_total += seg5;
6492b65e182SJames Smart 	if (seg5 < phba->ktime_seg5_min)
6502b65e182SJames Smart 		phba->ktime_seg5_min = seg5;
6512b65e182SJames Smart 	else if (seg5 > phba->ktime_seg5_max)
6522b65e182SJames Smart 		phba->ktime_seg5_max = seg5;
6532b65e182SJames Smart 
6542b65e182SJames Smart 	phba->ktime_data_samples++;
6552b65e182SJames Smart 	if (!seg6)
6562b65e182SJames Smart 		goto out;
6572b65e182SJames Smart 
6582b65e182SJames Smart 	phba->ktime_seg6_total += seg6;
6592b65e182SJames Smart 	if (seg6 < phba->ktime_seg6_min)
6602b65e182SJames Smart 		phba->ktime_seg6_min = seg6;
6612b65e182SJames Smart 	else if (seg6 > phba->ktime_seg6_max)
6622b65e182SJames Smart 		phba->ktime_seg6_max = seg6;
6632b65e182SJames Smart 
6642b65e182SJames Smart 	phba->ktime_seg7_total += seg7;
6652b65e182SJames Smart 	if (seg7 < phba->ktime_seg7_min)
6662b65e182SJames Smart 		phba->ktime_seg7_min = seg7;
6672b65e182SJames Smart 	else if (seg7 > phba->ktime_seg7_max)
6682b65e182SJames Smart 		phba->ktime_seg7_max = seg7;
6692b65e182SJames Smart 
6702b65e182SJames Smart 	phba->ktime_seg8_total += seg8;
6712b65e182SJames Smart 	if (seg8 < phba->ktime_seg8_min)
6722b65e182SJames Smart 		phba->ktime_seg8_min = seg8;
6732b65e182SJames Smart 	else if (seg8 > phba->ktime_seg8_max)
6742b65e182SJames Smart 		phba->ktime_seg8_max = seg8;
6752b65e182SJames Smart 
6762b65e182SJames Smart 	phba->ktime_seg9_total += seg9;
6772b65e182SJames Smart 	if (seg9 < phba->ktime_seg9_min)
6782b65e182SJames Smart 		phba->ktime_seg9_min = seg9;
6792b65e182SJames Smart 	else if (seg9 > phba->ktime_seg9_max)
6802b65e182SJames Smart 		phba->ktime_seg9_max = seg9;
6812b65e182SJames Smart out:
6822b65e182SJames Smart 	phba->ktime_seg10_total += seg10;
6832b65e182SJames Smart 	if (seg10 < phba->ktime_seg10_min)
6842b65e182SJames Smart 		phba->ktime_seg10_min = seg10;
6852b65e182SJames Smart 	else if (seg10 > phba->ktime_seg10_max)
6862b65e182SJames Smart 		phba->ktime_seg10_max = seg10;
6872b65e182SJames Smart 	phba->ktime_status_samples++;
6882b65e182SJames Smart }
6892b65e182SJames Smart #endif
6902b65e182SJames Smart 
691d613b6a7SJames Smart /**
692d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
693d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
694d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
695d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
696d613b6a7SJames Smart  *
697d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
698d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME FCP commands
699d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
700d613b6a7SJames Smart  **/
701d613b6a7SJames Smart static void
702d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
703d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
704d613b6a7SJames Smart {
705d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
706d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
707d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
708e3246a12SJames Smart 	uint32_t status, result, op, start_clean, logerr;
7092b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
7102b65e182SJames Smart 	uint32_t id;
7112b65e182SJames Smart #endif
712d613b6a7SJames Smart 
713d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
71486c67379SJames Smart 	ctxp->flag &= ~LPFC_NVMET_IO_INP;
71586c67379SJames Smart 
716d613b6a7SJames Smart 	rsp = &ctxp->ctx.fcp_req;
717d613b6a7SJames Smart 	op = rsp->op;
718d613b6a7SJames Smart 
719d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
720d613b6a7SJames Smart 	result = wcqe->parameter;
721d613b6a7SJames Smart 
72286c67379SJames Smart 	if (phba->targetport)
72386c67379SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
72486c67379SJames Smart 	else
72586c67379SJames Smart 		tgtp = NULL;
726d613b6a7SJames Smart 
7272b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
7282b65e182SJames Smart 			 ctxp->oxid, op, status);
7292b65e182SJames Smart 
730d613b6a7SJames Smart 	if (status) {
731d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
732d613b6a7SJames Smart 		rsp->transferred_length = 0;
7334b056682SJames Smart 		if (tgtp) {
734d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_error);
7358ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
7364b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_aborted);
7374b056682SJames Smart 		}
73886c67379SJames Smart 
739e3246a12SJames Smart 		logerr = LOG_NVME_IOERR;
740e3246a12SJames Smart 
74186c67379SJames Smart 		/* pick up SLI4 exhange busy condition */
74286c67379SJames Smart 		if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
74386c67379SJames Smart 			ctxp->flag |= LPFC_NVMET_XBUSY;
744e3246a12SJames Smart 			logerr |= LOG_NVME_ABTS;
7454b056682SJames Smart 			if (tgtp)
7464b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_xb_set);
74786c67379SJames Smart 
74886c67379SJames Smart 		} else {
74986c67379SJames Smart 			ctxp->flag &= ~LPFC_NVMET_XBUSY;
75086c67379SJames Smart 		}
75186c67379SJames Smart 
752e3246a12SJames Smart 		lpfc_printf_log(phba, KERN_INFO, logerr,
75379d8c4ceSJames Smart 				"6315 IO Error Cmpl oxid: x%x xri: x%x %x/%x "
75479d8c4ceSJames Smart 				"XBUSY:x%x\n",
75579d8c4ceSJames Smart 				ctxp->oxid, ctxp->ctxbuf->sglq->sli4_xritag,
75679d8c4ceSJames Smart 				status, result, ctxp->flag);
757e3246a12SJames Smart 
758d613b6a7SJames Smart 	} else {
759d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_SUCCESS;
760d613b6a7SJames Smart 		if (op == NVMET_FCOP_RSP)
761d613b6a7SJames Smart 			rsp->transferred_length = rsp->rsplen;
762d613b6a7SJames Smart 		else
763d613b6a7SJames Smart 			rsp->transferred_length = rsp->transfer_length;
76486c67379SJames Smart 		if (tgtp)
765d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
766d613b6a7SJames Smart 	}
767d613b6a7SJames Smart 
768d613b6a7SJames Smart 	if ((op == NVMET_FCOP_READDATA_RSP) ||
769d613b6a7SJames Smart 	    (op == NVMET_FCOP_RSP)) {
770d613b6a7SJames Smart 		/* Sanity check */
771d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DONE;
772d613b6a7SJames Smart 		ctxp->entry_cnt++;
77386c67379SJames Smart 
7742b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
775c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
7762b65e182SJames Smart 			if (rsp->op == NVMET_FCOP_READDATA_RSP) {
7772b65e182SJames Smart 				ctxp->ts_isr_data =
7782b65e182SJames Smart 					cmdwqe->isr_timestamp;
7792b65e182SJames Smart 				ctxp->ts_data_nvme =
7802b65e182SJames Smart 					ktime_get_ns();
7812b65e182SJames Smart 				ctxp->ts_nvme_status =
7822b65e182SJames Smart 					ctxp->ts_data_nvme;
7832b65e182SJames Smart 				ctxp->ts_status_wqput =
7842b65e182SJames Smart 					ctxp->ts_data_nvme;
7852b65e182SJames Smart 				ctxp->ts_isr_status =
7862b65e182SJames Smart 					ctxp->ts_data_nvme;
7872b65e182SJames Smart 				ctxp->ts_status_nvme =
7882b65e182SJames Smart 					ctxp->ts_data_nvme;
7892b65e182SJames Smart 			} else {
7902b65e182SJames Smart 				ctxp->ts_isr_status =
7912b65e182SJames Smart 					cmdwqe->isr_timestamp;
7922b65e182SJames Smart 				ctxp->ts_status_nvme =
7932b65e182SJames Smart 					ktime_get_ns();
7942b65e182SJames Smart 			}
7952b65e182SJames Smart 		}
7962b65e182SJames Smart #endif
797d613b6a7SJames Smart 		rsp->done(rsp);
7982b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
799c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme)
8002b65e182SJames Smart 			lpfc_nvmet_ktime(phba, ctxp);
8012b65e182SJames Smart #endif
80219b58d94SJames Smart 		/* lpfc_nvmet_xmt_fcp_release() will recycle the context */
803d613b6a7SJames Smart 	} else {
804d613b6a7SJames Smart 		ctxp->entry_cnt++;
805c6e0c925SDick Kennedy 		start_clean = offsetof(struct lpfc_iocbq, iocb_flag);
806d613b6a7SJames Smart 		memset(((char *)cmdwqe) + start_clean, 0,
807d613b6a7SJames Smart 		       (sizeof(struct lpfc_iocbq) - start_clean));
8082b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
809c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
8102b65e182SJames Smart 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
8112b65e182SJames Smart 			ctxp->ts_data_nvme = ktime_get_ns();
8122b65e182SJames Smart 		}
8132b65e182SJames Smart #endif
814d613b6a7SJames Smart 		rsp->done(rsp);
815d613b6a7SJames Smart 	}
81663df6d63SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
81763df6d63SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
818d6d189ceSBart Van Assche 		id = raw_smp_processor_id();
81963df6d63SJames Smart 		if (id < LPFC_CHECK_CPU_CNT) {
82063df6d63SJames Smart 			if (ctxp->cpu != id)
82163df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
82263df6d63SJames Smart 						"6704 CPU Check cmdcmpl: "
82363df6d63SJames Smart 						"cpu %d expect %d\n",
82463df6d63SJames Smart 						id, ctxp->cpu);
82563df6d63SJames Smart 			phba->sli4_hba.hdwq[rsp->hwqid].cpucheck_cmpl_io[id]++;
82663df6d63SJames Smart 		}
82763df6d63SJames Smart 	}
82863df6d63SJames Smart #endif
829d613b6a7SJames Smart }
830d613b6a7SJames Smart 
831d613b6a7SJames Smart static int
832d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
833d613b6a7SJames Smart 		      struct nvmefc_tgt_ls_req *rsp)
834d613b6a7SJames Smart {
835d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
836d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.ls_req);
837d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
838d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf =
839d613b6a7SJames Smart 		(struct hbq_dmabuf *)ctxp->rqb_buffer;
840d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
841d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
842d613b6a7SJames Smart 	struct lpfc_dmabuf dmabuf;
843d613b6a7SJames Smart 	struct ulp_bde64 bpl;
844d613b6a7SJames Smart 	int rc;
845d613b6a7SJames Smart 
8461abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
8471abcb371SDick Kennedy 		return -ENODEV;
8481abcb371SDick Kennedy 
8493386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
8503386f4bdSJames Smart 		return -ENODEV;
8513386f4bdSJames Smart 
852d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
853ce1b591cSJames Smart 			"6023 NVMET LS rsp oxid x%x\n", ctxp->oxid);
854ce1b591cSJames Smart 
855ce1b591cSJames Smart 	if ((ctxp->state != LPFC_NVMET_STE_LS_RCV) ||
856ce1b591cSJames Smart 	    (ctxp->entry_cnt != 1)) {
857ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
858ce1b591cSJames Smart 				"6412 NVMET LS rsp state mismatch "
859ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
860ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
861ce1b591cSJames Smart 	}
862ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_LS_RSP;
863ce1b591cSJames Smart 	ctxp->entry_cnt++;
864d613b6a7SJames Smart 
865d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, ctxp, rsp->rspdma,
866d613b6a7SJames Smart 				      rsp->rsplen);
867d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
868d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_drop);
869d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
870d613b6a7SJames Smart 				"6150 LS Drop IO x%x: Prep\n",
871d613b6a7SJames Smart 				ctxp->oxid);
872d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
873547077a4SJames Smart 		atomic_inc(&nvmep->xmt_ls_abort);
874d613b6a7SJames Smart 		lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp,
875d613b6a7SJames Smart 						ctxp->sid, ctxp->oxid);
876d613b6a7SJames Smart 		return -ENOMEM;
877d613b6a7SJames Smart 	}
878d613b6a7SJames Smart 
879d613b6a7SJames Smart 	/* Save numBdes for bpl2sgl */
880d613b6a7SJames Smart 	nvmewqeq->rsvd2 = 1;
881d613b6a7SJames Smart 	nvmewqeq->hba_wqidx = 0;
882d613b6a7SJames Smart 	nvmewqeq->context3 = &dmabuf;
883d613b6a7SJames Smart 	dmabuf.virt = &bpl;
884d613b6a7SJames Smart 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
885d613b6a7SJames Smart 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
886d613b6a7SJames Smart 	bpl.tus.f.bdeSize = rsp->rsplen;
887d613b6a7SJames Smart 	bpl.tus.f.bdeFlags = 0;
888d613b6a7SJames Smart 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
889d613b6a7SJames Smart 
890d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_rsp_cmp;
891d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
892d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
893d613b6a7SJames Smart 
8942b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  RESP: xri x%x wqidx x%x len x%x\n",
8952b65e182SJames Smart 			 ctxp->oxid, nvmewqeq->hba_wqidx, rsp->rsplen);
8962b65e182SJames Smart 
8971fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
898d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
899d613b6a7SJames Smart 		/*
900d613b6a7SJames Smart 		 * Okay to repost buffer here, but wait till cmpl
901d613b6a7SJames Smart 		 * before freeing ctxp and iocbq.
902d613b6a7SJames Smart 		 */
903d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
904d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_rsp);
905d613b6a7SJames Smart 		return 0;
906d613b6a7SJames Smart 	}
907d613b6a7SJames Smart 	/* Give back resources */
908d613b6a7SJames Smart 	atomic_inc(&nvmep->xmt_ls_drop);
909d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
910d613b6a7SJames Smart 			"6151 LS Drop IO x%x: Issue %d\n",
911d613b6a7SJames Smart 			ctxp->oxid, rc);
912d613b6a7SJames Smart 
913d613b6a7SJames Smart 	lpfc_nlp_put(nvmewqeq->context1);
914d613b6a7SJames Smart 
915d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
916547077a4SJames Smart 	atomic_inc(&nvmep->xmt_ls_abort);
917d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
918d613b6a7SJames Smart 	return -ENXIO;
919d613b6a7SJames Smart }
920d613b6a7SJames Smart 
921d613b6a7SJames Smart static int
922d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
923d613b6a7SJames Smart 		      struct nvmefc_tgt_fcp_req *rsp)
924d613b6a7SJames Smart {
925d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
926d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
927d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
928d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
9296e8e1c14SJames Smart 	struct lpfc_queue *wq;
930d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
9316e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
9326e8e1c14SJames Smart 	unsigned long iflags;
933223b78eaSArnd Bergmann 	int rc;
9342b65e182SJames Smart 
9351abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING) {
9361abcb371SDick Kennedy 		rc = -ENODEV;
9371abcb371SDick Kennedy 		goto aerr;
9381abcb371SDick Kennedy 	}
9391abcb371SDick Kennedy 
9403386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING) {
9413386f4bdSJames Smart 		rc = -ENODEV;
9423386f4bdSJames Smart 		goto aerr;
9433386f4bdSJames Smart 	}
9443386f4bdSJames Smart 
9452b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
946c8a4ce0bSDick Kennedy 	if (ctxp->ts_cmd_nvme) {
9472b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
9482b65e182SJames Smart 			ctxp->ts_nvme_status = ktime_get_ns();
9492b65e182SJames Smart 		else
9502b65e182SJames Smart 			ctxp->ts_nvme_data = ktime_get_ns();
9512b65e182SJames Smart 	}
9521fbf9742SJames Smart 
9531fbf9742SJames Smart 	/* Setup the hdw queue if not already set */
9541fbf9742SJames Smart 	if (!ctxp->hdwq)
9551fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[rsp->hwqid];
9561fbf9742SJames Smart 
9572b65e182SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
958d6d189ceSBart Van Assche 		int id = raw_smp_processor_id();
95963df6d63SJames Smart 		if (id < LPFC_CHECK_CPU_CNT) {
96063df6d63SJames Smart 			if (rsp->hwqid != id)
96163df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
9622b65e182SJames Smart 						"6705 CPU Check OP: "
9632b65e182SJames Smart 						"cpu %d expect %d\n",
9642b65e182SJames Smart 						id, rsp->hwqid);
96563df6d63SJames Smart 			phba->sli4_hba.hdwq[rsp->hwqid].cpucheck_xmt_io[id]++;
9662b65e182SJames Smart 		}
96763df6d63SJames Smart 		ctxp->cpu = id; /* Setup cpu for cmpl check */
9682b65e182SJames Smart 	}
9692b65e182SJames Smart #endif
970d613b6a7SJames Smart 
971d613b6a7SJames Smart 	/* Sanity check */
97286c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_ABTS_RCV) ||
97386c67379SJames Smart 	    (ctxp->state == LPFC_NVMET_STE_ABORT)) {
974d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
975d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
97679d8c4ceSJames Smart 				"6102 IO oxid x%x aborted\n",
977d613b6a7SJames Smart 				ctxp->oxid);
978a5068b46SJames Smart 		rc = -ENXIO;
979d613b6a7SJames Smart 		goto aerr;
980d613b6a7SJames Smart 	}
981d613b6a7SJames Smart 
982d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
983d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
984d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
985d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
986d613b6a7SJames Smart 				"6152 FCP Drop IO x%x: Prep\n",
987d613b6a7SJames Smart 				ctxp->oxid);
988a5068b46SJames Smart 		rc = -ENXIO;
989d613b6a7SJames Smart 		goto aerr;
990d613b6a7SJames Smart 	}
991d613b6a7SJames Smart 
992d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
993d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
994d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
995d613b6a7SJames Smart 	nvmewqeq->iocb_flag |=  LPFC_IO_NVMET;
996d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
997d613b6a7SJames Smart 
9982b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
9992b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
10002b65e182SJames Smart 
100161f3d4bfSJames Smart 	ctxp->flag |= LPFC_NVMET_IO_INP;
10021fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
1003d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
10042b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1005c8a4ce0bSDick Kennedy 		if (!ctxp->ts_cmd_nvme)
10062b65e182SJames Smart 			return 0;
10072b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
10082b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
10092b65e182SJames Smart 		else
10102b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
10112b65e182SJames Smart #endif
1012d613b6a7SJames Smart 		return 0;
1013d613b6a7SJames Smart 	}
1014d613b6a7SJames Smart 
10156e8e1c14SJames Smart 	if (rc == -EBUSY) {
10166e8e1c14SJames Smart 		/*
10176e8e1c14SJames Smart 		 * WQ was full, so queue nvmewqeq to be sent after
10186e8e1c14SJames Smart 		 * WQE release CQE
10196e8e1c14SJames Smart 		 */
10206e8e1c14SJames Smart 		ctxp->flag |= LPFC_NVMET_DEFER_WQFULL;
1021c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
10226e8e1c14SJames Smart 		pring = wq->pring;
10236e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
10246e8e1c14SJames Smart 		list_add_tail(&nvmewqeq->list, &wq->wqfull_list);
10256e8e1c14SJames Smart 		wq->q_flag |= HBA_NVMET_WQFULL;
10266e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
1027411de511SJames Smart 		atomic_inc(&lpfc_nvmep->defer_wqfull);
10286e8e1c14SJames Smart 		return 0;
10296e8e1c14SJames Smart 	}
10306e8e1c14SJames Smart 
1031d613b6a7SJames Smart 	/* Give back resources */
1032d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1033d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1034d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
1035d613b6a7SJames Smart 			ctxp->oxid, rc);
1036d613b6a7SJames Smart 
1037d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
1038d613b6a7SJames Smart 	nvmewqeq->context2 = NULL;
1039d613b6a7SJames Smart 	nvmewqeq->context3 = NULL;
1040a5068b46SJames Smart 	rc = -EBUSY;
1041d613b6a7SJames Smart aerr:
1042a5068b46SJames Smart 	return rc;
1043d613b6a7SJames Smart }
1044d613b6a7SJames Smart 
1045d613b6a7SJames Smart static void
1046d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
1047d613b6a7SJames Smart {
1048d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
1049d613b6a7SJames Smart 
1050d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
1051c41f5988SEwan D. Milne 	if (tport->phba->targetport)
1052c41f5988SEwan D. Milne 		complete(tport->tport_unreg_cmp);
1053d613b6a7SJames Smart }
1054d613b6a7SJames Smart 
105519b58d94SJames Smart static void
1056a97ec51bSJames Smart lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
1057a97ec51bSJames Smart 			 struct nvmefc_tgt_fcp_req *req)
1058a97ec51bSJames Smart {
1059a97ec51bSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
1060a97ec51bSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
1061a97ec51bSJames Smart 		container_of(req, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
1062a97ec51bSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
10636e8e1c14SJames Smart 	struct lpfc_queue *wq;
106486c67379SJames Smart 	unsigned long flags;
1065a97ec51bSJames Smart 
10661abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
10671abcb371SDick Kennedy 		return;
10681abcb371SDick Kennedy 
10693386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
10703386f4bdSJames Smart 		return;
10713386f4bdSJames Smart 
10721fbf9742SJames Smart 	if (!ctxp->hdwq)
10731fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[0];
10741fbf9742SJames Smart 
1075a97ec51bSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
107679d8c4ceSJames Smart 			"6103 NVMET Abort op: oxid x%x flg x%x ste %d\n",
1077ce1b591cSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
1078a97ec51bSJames Smart 
1079ce1b591cSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x flg x%x ste x%x\n",
1080ce1b591cSJames Smart 			 ctxp->oxid, ctxp->flag, ctxp->state);
1081a97ec51bSJames Smart 
1082a97ec51bSJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
1083ce1b591cSJames Smart 
108486c67379SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
108586c67379SJames Smart 
108686c67379SJames Smart 	/* Since iaab/iaar are NOT set, we need to check
108786c67379SJames Smart 	 * if the firmware is in process of aborting IO
108886c67379SJames Smart 	 */
108979d8c4ceSJames Smart 	if (ctxp->flag & (LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP)) {
109086c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
109186c67379SJames Smart 		return;
109286c67379SJames Smart 	}
1093a97ec51bSJames Smart 	ctxp->flag |= LPFC_NVMET_ABORT_OP;
1094ce1b591cSJames Smart 
10956e8e1c14SJames Smart 	if (ctxp->flag & LPFC_NVMET_DEFER_WQFULL) {
1096c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
10976e8e1c14SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
10986e8e1c14SJames Smart 						 ctxp->oxid);
1099c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
11006e8e1c14SJames Smart 		lpfc_nvmet_wqfull_flush(phba, wq, ctxp);
11016e8e1c14SJames Smart 		return;
11026e8e1c14SJames Smart 	}
1103c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11046e8e1c14SJames Smart 
1105ce1b591cSJames Smart 	/* An state of LPFC_NVMET_STE_RCV means we have just received
1106ce1b591cSJames Smart 	 * the NVME command and have not started processing it.
1107ce1b591cSJames Smart 	 * (by issuing any IO WQEs on this exchange yet)
1108ce1b591cSJames Smart 	 */
1109ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_RCV)
1110ce1b591cSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1111a97ec51bSJames Smart 						 ctxp->oxid);
1112a97ec51bSJames Smart 	else
1113ce1b591cSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1114a97ec51bSJames Smart 					       ctxp->oxid);
1115a97ec51bSJames Smart }
1116a97ec51bSJames Smart 
1117a97ec51bSJames Smart static void
111819b58d94SJames Smart lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
111919b58d94SJames Smart 			   struct nvmefc_tgt_fcp_req *rsp)
112019b58d94SJames Smart {
1121547077a4SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
112219b58d94SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
112319b58d94SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
112419b58d94SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
112519b58d94SJames Smart 	unsigned long flags;
112619b58d94SJames Smart 	bool aborting = false;
112719b58d94SJames Smart 
1128c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
1129c160c0f8SJames Smart 	if (ctxp->flag & LPFC_NVMET_XBUSY)
1130c160c0f8SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1131c160c0f8SJames Smart 				"6027 NVMET release with XBUSY flag x%x"
1132c160c0f8SJames Smart 				" oxid x%x\n",
1133c160c0f8SJames Smart 				ctxp->flag, ctxp->oxid);
1134c160c0f8SJames Smart 	else if (ctxp->state != LPFC_NVMET_STE_DONE &&
1135c160c0f8SJames Smart 		 ctxp->state != LPFC_NVMET_STE_ABORT)
1136ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1137ce1b591cSJames Smart 				"6413 NVMET release bad state %d %d oxid x%x\n",
1138ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
1139ce1b591cSJames Smart 
114086c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_ABORT_OP) ||
114186c67379SJames Smart 	    (ctxp->flag & LPFC_NVMET_XBUSY)) {
114219b58d94SJames Smart 		aborting = true;
114386c67379SJames Smart 		/* let the abort path do the real release */
114486c67379SJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
114519b58d94SJames Smart 	}
114619b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
114719b58d94SJames Smart 
11489094367aSArnd Bergmann 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d abt %d\n", ctxp->oxid,
11499094367aSArnd Bergmann 			 ctxp->state, aborting);
115019b58d94SJames Smart 
1151547077a4SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_release);
115279d8c4ceSJames Smart 	ctxp->flag &= ~LPFC_NVMET_TNOTIFY;
1153547077a4SJames Smart 
115486c67379SJames Smart 	if (aborting)
115586c67379SJames Smart 		return;
115686c67379SJames Smart 
11576c621a22SJames Smart 	lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
115819b58d94SJames Smart }
115919b58d94SJames Smart 
116050738420SJames Smart static void
116150738420SJames Smart lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port *tgtport,
116250738420SJames Smart 		     struct nvmefc_tgt_fcp_req *rsp)
116350738420SJames Smart {
116450738420SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
116550738420SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
116650738420SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
116750738420SJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
116850738420SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
1169472e146dSJames Smart 	unsigned long iflag;
1170472e146dSJames Smart 
117150738420SJames Smart 
117250738420SJames Smart 	lpfc_nvmeio_data(phba, "NVMET DEFERRCV: xri x%x sz %d CPU %02x\n",
1173d6d189ceSBart Van Assche 			 ctxp->oxid, ctxp->size, raw_smp_processor_id());
117450738420SJames Smart 
11756871e814SJames Smart 	if (!nvmebuf) {
11766871e814SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
117779d8c4ceSJames Smart 				"6425 Defer rcv: no buffer oxid x%x: "
11786871e814SJames Smart 				"flg %x ste %x\n",
11796871e814SJames Smart 				ctxp->oxid, ctxp->flag, ctxp->state);
11806871e814SJames Smart 		return;
11816871e814SJames Smart 	}
11826871e814SJames Smart 
118350738420SJames Smart 	tgtp = phba->targetport->private;
11846871e814SJames Smart 	if (tgtp)
118550738420SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_defer);
1186411de511SJames Smart 
1187411de511SJames Smart 	/* Free the nvmebuf since a new buffer already replaced it */
1188cbc5de1bSJames Smart 	nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
1189472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflag);
1190472e146dSJames Smart 	ctxp->rqb_buffer = NULL;
1191472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
119250738420SJames Smart }
119350738420SJames Smart 
1194ab723121SJames Smart static void
1195ab723121SJames Smart lpfc_nvmet_discovery_event(struct nvmet_fc_target_port *tgtport)
1196ab723121SJames Smart {
1197ab723121SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1198ab723121SJames Smart 	struct lpfc_hba *phba;
1199ab723121SJames Smart 	uint32_t rc;
1200ab723121SJames Smart 
1201ab723121SJames Smart 	tgtp = tgtport->private;
1202ab723121SJames Smart 	phba = tgtp->phba;
1203ab723121SJames Smart 
1204ab723121SJames Smart 	rc = lpfc_issue_els_rscn(phba->pport, 0);
1205ab723121SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1206ab723121SJames Smart 			"6420 NVMET subsystem change: Notification %s\n",
1207ab723121SJames Smart 			(rc) ? "Failed" : "Sent");
1208ab723121SJames Smart }
1209ab723121SJames Smart 
1210d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
1211d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
1212d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
1213d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
1214a97ec51bSJames Smart 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
121519b58d94SJames Smart 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
121650738420SJames Smart 	.defer_rcv	= lpfc_nvmet_defer_rcv,
1217ab723121SJames Smart 	.discovery_event = lpfc_nvmet_discovery_event,
1218d613b6a7SJames Smart 
1219d613b6a7SJames Smart 	.max_hw_queues  = 1,
1220d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1221d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1222d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
1223d613b6a7SJames Smart 
1224d613b6a7SJames Smart 	/* optional features */
1225d613b6a7SJames Smart 	.target_features = 0,
1226d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
1227d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
1228d613b6a7SJames Smart };
1229d613b6a7SJames Smart 
123039aa23f9SColin Ian King static void
123166d7ce93SDick Kennedy __lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba *phba,
123266d7ce93SDick Kennedy 		struct lpfc_nvmet_ctx_info *infop)
12336c621a22SJames Smart {
12346c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf, *next_ctx_buf;
12356c621a22SJames Smart 	unsigned long flags;
12366c621a22SJames Smart 
123766d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, flags);
1238966bb5b7SJames Smart 	list_for_each_entry_safe(ctx_buf, next_ctx_buf,
123966d7ce93SDick Kennedy 				&infop->nvmet_ctx_list, list) {
12405e5b511dSJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
12416c621a22SJames Smart 		list_del_init(&ctx_buf->list);
12425e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
124366d7ce93SDick Kennedy 
124466d7ce93SDick Kennedy 		__lpfc_clear_active_sglq(phba, ctx_buf->sglq->sli4_lxritag);
12456c621a22SJames Smart 		ctx_buf->sglq->state = SGL_FREED;
12466c621a22SJames Smart 		ctx_buf->sglq->ndlp = NULL;
12476c621a22SJames Smart 
1248c4031db7SDan Carpenter 		spin_lock(&phba->sli4_hba.sgl_list_lock);
12496c621a22SJames Smart 		list_add_tail(&ctx_buf->sglq->list,
12506c621a22SJames Smart 				&phba->sli4_hba.lpfc_nvmet_sgl_list);
1251c4031db7SDan Carpenter 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
12526c621a22SJames Smart 
12536c621a22SJames Smart 		lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
12546c621a22SJames Smart 		kfree(ctx_buf->context);
12556c621a22SJames Smart 	}
125666d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, flags);
1257966bb5b7SJames Smart }
125866d7ce93SDick Kennedy 
125966d7ce93SDick Kennedy static void
126066d7ce93SDick Kennedy lpfc_nvmet_cleanup_io_context(struct lpfc_hba *phba)
126166d7ce93SDick Kennedy {
126266d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
126366d7ce93SDick Kennedy 	int i, j;
126466d7ce93SDick Kennedy 
126566d7ce93SDick Kennedy 	/* The first context list, MRQ 0 CPU 0 */
126666d7ce93SDick Kennedy 	infop = phba->sli4_hba.nvmet_ctx_info;
126766d7ce93SDick Kennedy 	if (!infop)
126866d7ce93SDick Kennedy 		return;
126966d7ce93SDick Kennedy 
127066d7ce93SDick Kennedy 	/* Cycle the the entire CPU context list for every MRQ */
127166d7ce93SDick Kennedy 	for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
1272222e9239SJames Smart 		for_each_present_cpu(j) {
1273222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, j, i);
127466d7ce93SDick Kennedy 			__lpfc_nvmet_clean_io_for_cpu(phba, infop);
127566d7ce93SDick Kennedy 		}
127666d7ce93SDick Kennedy 	}
127766d7ce93SDick Kennedy 	kfree(phba->sli4_hba.nvmet_ctx_info);
127866d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = NULL;
12796c621a22SJames Smart }
12806c621a22SJames Smart 
128139aa23f9SColin Ian King static int
12826c621a22SJames Smart lpfc_nvmet_setup_io_context(struct lpfc_hba *phba)
12836c621a22SJames Smart {
12846c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
12856c621a22SJames Smart 	struct lpfc_iocbq *nvmewqe;
12866c621a22SJames Smart 	union lpfc_wqe128 *wqe;
128766d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *last_infop;
128866d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
1289222e9239SJames Smart 	int i, j, idx, cpu;
12906c621a22SJames Smart 
12916c621a22SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
12926c621a22SJames Smart 			"6403 Allocate NVMET resources for %d XRIs\n",
12936c621a22SJames Smart 			phba->sli4_hba.nvmet_xri_cnt);
12946c621a22SJames Smart 
129566d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = kcalloc(
1296222e9239SJames Smart 		phba->sli4_hba.num_possible_cpu * phba->cfg_nvmet_mrq,
129766d7ce93SDick Kennedy 		sizeof(struct lpfc_nvmet_ctx_info), GFP_KERNEL);
129866d7ce93SDick Kennedy 	if (!phba->sli4_hba.nvmet_ctx_info) {
129966d7ce93SDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
130066d7ce93SDick Kennedy 				"6419 Failed allocate memory for "
130166d7ce93SDick Kennedy 				"nvmet context lists\n");
130266d7ce93SDick Kennedy 		return -ENOMEM;
130366d7ce93SDick Kennedy 	}
130466d7ce93SDick Kennedy 
130566d7ce93SDick Kennedy 	/*
130666d7ce93SDick Kennedy 	 * Assuming X CPUs in the system, and Y MRQs, allocate some
130766d7ce93SDick Kennedy 	 * lpfc_nvmet_ctx_info structures as follows:
130866d7ce93SDick Kennedy 	 *
130966d7ce93SDick Kennedy 	 * cpu0/mrq0 cpu1/mrq0 ... cpuX/mrq0
131066d7ce93SDick Kennedy 	 * cpu0/mrq1 cpu1/mrq1 ... cpuX/mrq1
131166d7ce93SDick Kennedy 	 * ...
131266d7ce93SDick Kennedy 	 * cpuX/mrqY cpuX/mrqY ... cpuX/mrqY
131366d7ce93SDick Kennedy 	 *
131466d7ce93SDick Kennedy 	 * Each line represents a MRQ "silo" containing an entry for
131566d7ce93SDick Kennedy 	 * every CPU.
131666d7ce93SDick Kennedy 	 *
131766d7ce93SDick Kennedy 	 * MRQ X is initially assumed to be associated with CPU X, thus
131866d7ce93SDick Kennedy 	 * contexts are initially distributed across all MRQs using
131966d7ce93SDick Kennedy 	 * the MRQ index (N) as follows cpuN/mrqN. When contexts are
132066d7ce93SDick Kennedy 	 * freed, the are freed to the MRQ silo based on the CPU number
132166d7ce93SDick Kennedy 	 * of the IO completion. Thus a context that was allocated for MRQ A
132266d7ce93SDick Kennedy 	 * whose IO completed on CPU B will be freed to cpuB/mrqA.
132366d7ce93SDick Kennedy 	 */
1324222e9239SJames Smart 	for_each_possible_cpu(i) {
132566d7ce93SDick Kennedy 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1326222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
132766d7ce93SDick Kennedy 			INIT_LIST_HEAD(&infop->nvmet_ctx_list);
132866d7ce93SDick Kennedy 			spin_lock_init(&infop->nvmet_ctx_list_lock);
132966d7ce93SDick Kennedy 			infop->nvmet_ctx_list_cnt = 0;
133066d7ce93SDick Kennedy 		}
133166d7ce93SDick Kennedy 	}
133266d7ce93SDick Kennedy 
133366d7ce93SDick Kennedy 	/*
133466d7ce93SDick Kennedy 	 * Setup the next CPU context info ptr for each MRQ.
133566d7ce93SDick Kennedy 	 * MRQ 0 will cycle thru CPUs 0 - X separately from
133666d7ce93SDick Kennedy 	 * MRQ 1 cycling thru CPUs 0 - X, and so on.
133766d7ce93SDick Kennedy 	 */
133866d7ce93SDick Kennedy 	for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1339222e9239SJames Smart 		last_infop = lpfc_get_ctx_list(phba,
1340222e9239SJames Smart 					       cpumask_first(cpu_present_mask),
1341222e9239SJames Smart 					       j);
1342222e9239SJames Smart 		for (i = phba->sli4_hba.num_possible_cpu - 1;  i >= 0; i--) {
134366d7ce93SDick Kennedy 			infop = lpfc_get_ctx_list(phba, i, j);
134466d7ce93SDick Kennedy 			infop->nvmet_ctx_next_cpu = last_infop;
134566d7ce93SDick Kennedy 			last_infop = infop;
134666d7ce93SDick Kennedy 		}
134766d7ce93SDick Kennedy 	}
134866d7ce93SDick Kennedy 
13496c621a22SJames Smart 	/* For all nvmet xris, allocate resources needed to process a
13506c621a22SJames Smart 	 * received command on a per xri basis.
13516c621a22SJames Smart 	 */
135266d7ce93SDick Kennedy 	idx = 0;
1353222e9239SJames Smart 	cpu = cpumask_first(cpu_present_mask);
13546c621a22SJames Smart 	for (i = 0; i < phba->sli4_hba.nvmet_xri_cnt; i++) {
13556c621a22SJames Smart 		ctx_buf = kzalloc(sizeof(*ctx_buf), GFP_KERNEL);
13566c621a22SJames Smart 		if (!ctx_buf) {
13576c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13586c621a22SJames Smart 					"6404 Ran out of memory for NVMET\n");
13596c621a22SJames Smart 			return -ENOMEM;
13606c621a22SJames Smart 		}
13616c621a22SJames Smart 
13626c621a22SJames Smart 		ctx_buf->context = kzalloc(sizeof(*ctx_buf->context),
13636c621a22SJames Smart 					   GFP_KERNEL);
13646c621a22SJames Smart 		if (!ctx_buf->context) {
13656c621a22SJames Smart 			kfree(ctx_buf);
13666c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13676c621a22SJames Smart 					"6405 Ran out of NVMET "
13686c621a22SJames Smart 					"context memory\n");
13696c621a22SJames Smart 			return -ENOMEM;
13706c621a22SJames Smart 		}
13716c621a22SJames Smart 		ctx_buf->context->ctxbuf = ctx_buf;
1372ce1b591cSJames Smart 		ctx_buf->context->state = LPFC_NVMET_STE_FREE;
13736c621a22SJames Smart 
13746c621a22SJames Smart 		ctx_buf->iocbq = lpfc_sli_get_iocbq(phba);
13756c621a22SJames Smart 		if (!ctx_buf->iocbq) {
13766c621a22SJames Smart 			kfree(ctx_buf->context);
13776c621a22SJames Smart 			kfree(ctx_buf);
13786c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13796c621a22SJames Smart 					"6406 Ran out of NVMET iocb/WQEs\n");
13806c621a22SJames Smart 			return -ENOMEM;
13816c621a22SJames Smart 		}
13826c621a22SJames Smart 		ctx_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
13836c621a22SJames Smart 		nvmewqe = ctx_buf->iocbq;
1384205e8240SJames Smart 		wqe = &nvmewqe->wqe;
1385bd3061baSJames Smart 
13866c621a22SJames Smart 		/* Initialize WQE */
13876c621a22SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
13886c621a22SJames Smart 
13896c621a22SJames Smart 		ctx_buf->iocbq->context1 = NULL;
13906c621a22SJames Smart 		spin_lock(&phba->sli4_hba.sgl_list_lock);
13916c621a22SJames Smart 		ctx_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, ctx_buf->iocbq);
13926c621a22SJames Smart 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
13936c621a22SJames Smart 		if (!ctx_buf->sglq) {
13946c621a22SJames Smart 			lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
13956c621a22SJames Smart 			kfree(ctx_buf->context);
13966c621a22SJames Smart 			kfree(ctx_buf);
13976c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13986c621a22SJames Smart 					"6407 Ran out of NVMET XRIs\n");
13996c621a22SJames Smart 			return -ENOMEM;
14006c621a22SJames Smart 		}
1401472e146dSJames Smart 		INIT_WORK(&ctx_buf->defer_work, lpfc_nvmet_fcp_rqst_defer_work);
140266d7ce93SDick Kennedy 
140366d7ce93SDick Kennedy 		/*
140466d7ce93SDick Kennedy 		 * Add ctx to MRQidx context list. Our initial assumption
140566d7ce93SDick Kennedy 		 * is MRQidx will be associated with CPUidx. This association
140666d7ce93SDick Kennedy 		 * can change on the fly.
140766d7ce93SDick Kennedy 		 */
1408222e9239SJames Smart 		infop = lpfc_get_ctx_list(phba, cpu, idx);
140966d7ce93SDick Kennedy 		spin_lock(&infop->nvmet_ctx_list_lock);
141066d7ce93SDick Kennedy 		list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
141166d7ce93SDick Kennedy 		infop->nvmet_ctx_list_cnt++;
141266d7ce93SDick Kennedy 		spin_unlock(&infop->nvmet_ctx_list_lock);
141366d7ce93SDick Kennedy 
141466d7ce93SDick Kennedy 		/* Spread ctx structures evenly across all MRQs */
141566d7ce93SDick Kennedy 		idx++;
1416222e9239SJames Smart 		if (idx >= phba->cfg_nvmet_mrq) {
141766d7ce93SDick Kennedy 			idx = 0;
1418222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1419222e9239SJames Smart 			continue;
1420222e9239SJames Smart 		}
1421222e9239SJames Smart 		cpu = cpumask_next(cpu, cpu_present_mask);
1422222e9239SJames Smart 		if (cpu == nr_cpu_ids)
1423222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1424222e9239SJames Smart 
14256c621a22SJames Smart 	}
142666d7ce93SDick Kennedy 
1427222e9239SJames Smart 	for_each_present_cpu(i) {
14282879265fSJames Smart 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
14292879265fSJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
143066d7ce93SDick Kennedy 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
143166d7ce93SDick Kennedy 					"6408 TOTAL NVMET ctx for CPU %d "
143232350664SJames Smart 					"MRQ %d: cnt %d nextcpu x%px\n",
143366d7ce93SDick Kennedy 					i, j, infop->nvmet_ctx_list_cnt,
143466d7ce93SDick Kennedy 					infop->nvmet_ctx_next_cpu);
143566d7ce93SDick Kennedy 		}
143666d7ce93SDick Kennedy 	}
14376c621a22SJames Smart 	return 0;
14386c621a22SJames Smart }
14396c621a22SJames Smart 
1440d613b6a7SJames Smart int
1441d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1442d613b6a7SJames Smart {
1443d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1444d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1445d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
14466c621a22SJames Smart 	int error;
1447d613b6a7SJames Smart 
1448d613b6a7SJames Smart 	if (phba->targetport)
1449d613b6a7SJames Smart 		return 0;
1450d613b6a7SJames Smart 
14516c621a22SJames Smart 	error = lpfc_nvmet_setup_io_context(phba);
14526c621a22SJames Smart 	if (error)
14536c621a22SJames Smart 		return error;
14546c621a22SJames Smart 
1455d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1456d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1457d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1458d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
1459d613b6a7SJames Smart 
14605b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
14615b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
14625b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
14634d4c4a4aSJames Smart 	 */
14644d4c4a4aSJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1465cdb42becSJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_hdw_queue;
1466f485c18dSDick Kennedy 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP;
1467d613b6a7SJames Smart 
14687d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1469d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1470d613b6a7SJames Smart 					     &phba->pcidev->dev,
1471d613b6a7SJames Smart 					     &phba->targetport);
1472166d7211SJames Smart #else
14736c621a22SJames Smart 	error = -ENOENT;
1474166d7211SJames Smart #endif
1475d613b6a7SJames Smart 	if (error) {
1476d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1477e7981a2cSDick Kennedy 				"6025 Cannot register NVME targetport x%x: "
1478e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1479e7981a2cSDick Kennedy 				error,
1480e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1481e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1482e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1483d613b6a7SJames Smart 		phba->targetport = NULL;
1484e7981a2cSDick Kennedy 		phba->nvmet_support = 0;
14856c621a22SJames Smart 
14866c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
14876c621a22SJames Smart 
1488d613b6a7SJames Smart 	} else {
1489d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
1490d613b6a7SJames Smart 			phba->targetport->private;
1491d613b6a7SJames Smart 		tgtp->phba = phba;
1492d613b6a7SJames Smart 
1493d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1494d613b6a7SJames Smart 				"6026 Registered NVME "
149532350664SJames Smart 				"targetport: x%px, private x%px "
1496e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1497d613b6a7SJames Smart 				phba->targetport, tgtp,
1498e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1499e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1500e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1501d613b6a7SJames Smart 
1502d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
1503d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
1504d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
1505d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
1506547077a4SJames Smart 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1507d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
1508d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
1509d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
15104b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_xb_set, 0);
15114b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_aborted, 0);
1512d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1513d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1514d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1515d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1516d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
1517d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1518d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
1519d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
1520d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
1521547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_release, 0);
1522d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1523d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
15244b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_xb_set, 0);
15254b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_aborted, 0);
1526d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
15274b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_xri_abort_cqe, 0);
1528547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
1529547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1530547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_unsol, 0);
1531547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_sol, 0);
1532d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
1533d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1534411de511SJames Smart 		atomic_set(&tgtp->defer_ctx, 0);
1535411de511SJames Smart 		atomic_set(&tgtp->defer_fod, 0);
1536411de511SJames Smart 		atomic_set(&tgtp->defer_wqfull, 0);
1537d613b6a7SJames Smart 	}
1538d613b6a7SJames Smart 	return error;
1539d613b6a7SJames Smart }
1540d613b6a7SJames Smart 
1541d613b6a7SJames Smart int
1542d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1543d613b6a7SJames Smart {
1544d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1545d613b6a7SJames Smart 
1546d613b6a7SJames Smart 	if (!phba->targetport)
1547d613b6a7SJames Smart 		return 0;
1548d613b6a7SJames Smart 
1549d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
155032350664SJames Smart 			 "6007 Update NVMET port x%px did x%x\n",
1551d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
1552d613b6a7SJames Smart 
1553d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
1554d613b6a7SJames Smart 	return 0;
1555d613b6a7SJames Smart }
1556d613b6a7SJames Smart 
1557318083adSJames Smart /**
1558318083adSJames Smart  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1559318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
1560318083adSJames Smart  * @axri: pointer to the nvmet xri abort wcqe structure.
1561318083adSJames Smart  *
1562318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
1563318083adSJames Smart  * NVMET aborted xri.
1564318083adSJames Smart  **/
1565318083adSJames Smart void
1566318083adSJames Smart lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
1567318083adSJames Smart 			    struct sli4_wcqe_xri_aborted *axri)
1568318083adSJames Smart {
156932b93865SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
157086c67379SJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
157186c67379SJames Smart 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
157286c67379SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
15734b056682SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
157479d8c4ceSJames Smart 	struct nvmefc_tgt_fcp_req *req = NULL;
157586c67379SJames Smart 	struct lpfc_nodelist *ndlp;
157686c67379SJames Smart 	unsigned long iflag = 0;
157786c67379SJames Smart 	int rrq_empty = 0;
157886c67379SJames Smart 	bool released = false;
157986c67379SJames Smart 
158086c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
158186c67379SJames Smart 			"6317 XB aborted xri x%x rxid x%x\n", xri, rxid);
158286c67379SJames Smart 
158386c67379SJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
158486c67379SJames Smart 		return;
15854b056682SJames Smart 
15864b056682SJames Smart 	if (phba->targetport) {
15874b056682SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
15884b056682SJames Smart 		atomic_inc(&tgtp->xmt_fcp_xri_abort_cqe);
15894b056682SJames Smart 	}
15904b056682SJames Smart 
159186c67379SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
15925e5b511dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
159386c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
159486c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
159586c67379SJames Smart 				 list) {
15966c621a22SJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
159786c67379SJames Smart 			continue;
159886c67379SJames Smart 
1599c160c0f8SJames Smart 		spin_lock(&ctxp->ctxlock);
160086c67379SJames Smart 		/* Check if we already received a free context call
160186c67379SJames Smart 		 * and we have completed processing an abort situation.
160286c67379SJames Smart 		 */
160386c67379SJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_RLS &&
160486c67379SJames Smart 		    !(ctxp->flag & LPFC_NVMET_ABORT_OP)) {
160579d8c4ceSJames Smart 			list_del_init(&ctxp->list);
160686c67379SJames Smart 			released = true;
160786c67379SJames Smart 		}
160886c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_XBUSY;
1609c160c0f8SJames Smart 		spin_unlock(&ctxp->ctxlock);
16105e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
161186c67379SJames Smart 
161286c67379SJames Smart 		rrq_empty = list_empty(&phba->active_rrq_list);
161386c67379SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, iflag);
161486c67379SJames Smart 		ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
161586c67379SJames Smart 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
161686c67379SJames Smart 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
161786c67379SJames Smart 		     ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
161886c67379SJames Smart 			lpfc_set_rrq_active(phba, ndlp,
16196c621a22SJames Smart 				ctxp->ctxbuf->sglq->sli4_lxritag,
162086c67379SJames Smart 				rxid, 1);
162186c67379SJames Smart 			lpfc_sli4_abts_err_handler(phba, ndlp, axri);
162286c67379SJames Smart 		}
162386c67379SJames Smart 
162486c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
162579d8c4ceSJames Smart 				"6318 XB aborted oxid x%x flg x%x (%x)\n",
162686c67379SJames Smart 				ctxp->oxid, ctxp->flag, released);
162786c67379SJames Smart 		if (released)
16286c621a22SJames Smart 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
16296c621a22SJames Smart 
163086c67379SJames Smart 		if (rrq_empty)
163186c67379SJames Smart 			lpfc_worker_wake_up(phba);
163286c67379SJames Smart 		return;
163386c67379SJames Smart 	}
16345e5b511dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
163586c67379SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
163679d8c4ceSJames Smart 
163779d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_xri(phba, xri);
163879d8c4ceSJames Smart 	if (ctxp) {
163979d8c4ceSJames Smart 		/*
164079d8c4ceSJames Smart 		 *  Abort already done by FW, so BA_ACC sent.
164179d8c4ceSJames Smart 		 *  However, the transport may be unaware.
164279d8c4ceSJames Smart 		 */
164379d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
164479d8c4ceSJames Smart 				"6323 NVMET Rcv ABTS xri x%x ctxp state x%x "
164579d8c4ceSJames Smart 				"flag x%x oxid x%x rxid x%x\n",
164679d8c4ceSJames Smart 				xri, ctxp->state, ctxp->flag, ctxp->oxid,
164779d8c4ceSJames Smart 				rxid);
164879d8c4ceSJames Smart 
164979d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
165079d8c4ceSJames Smart 		ctxp->flag |= LPFC_NVMET_ABTS_RCV;
165179d8c4ceSJames Smart 		ctxp->state = LPFC_NVMET_STE_ABORT;
165279d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
165379d8c4ceSJames Smart 
165479d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
165579d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
165601d53c04SJames Smart 				 xri, raw_smp_processor_id(), 0);
165779d8c4ceSJames Smart 
165879d8c4ceSJames Smart 		req = &ctxp->ctx.fcp_req;
165979d8c4ceSJames Smart 		if (req)
166079d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport, req);
166179d8c4ceSJames Smart 	}
166232b93865SJames Smart #endif
166386c67379SJames Smart }
166486c67379SJames Smart 
166586c67379SJames Smart int
166686c67379SJames Smart lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport *vport,
166786c67379SJames Smart 			   struct fc_frame_header *fc_hdr)
166886c67379SJames Smart {
166986c67379SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
167086c67379SJames Smart 	struct lpfc_hba *phba = vport->phba;
167186c67379SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
167286c67379SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
167332b93865SJames Smart 	uint32_t sid;
167479d8c4ceSJames Smart 	uint16_t oxid, xri;
167586c67379SJames Smart 	unsigned long iflag = 0;
167686c67379SJames Smart 
167732b93865SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
167879d8c4ceSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
167986c67379SJames Smart 
168086c67379SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
16815e5b511dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
168286c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
168386c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
168486c67379SJames Smart 				 list) {
168579d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
168686c67379SJames Smart 			continue;
168786c67379SJames Smart 
168832b93865SJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
168932b93865SJames Smart 
16905e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
169186c67379SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, iflag);
169286c67379SJames Smart 
169386c67379SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
169486c67379SJames Smart 		ctxp->flag |= LPFC_NVMET_ABTS_RCV;
169586c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
169686c67379SJames Smart 
169786c67379SJames Smart 		lpfc_nvmeio_data(phba,
169886c67379SJames Smart 			"NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1699d6d189ceSBart Van Assche 			xri, raw_smp_processor_id(), 0);
170086c67379SJames Smart 
170186c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
170286c67379SJames Smart 				"6319 NVMET Rcv ABTS:acc xri x%x\n", xri);
170386c67379SJames Smart 
170486c67379SJames Smart 		rsp = &ctxp->ctx.fcp_req;
170586c67379SJames Smart 		nvmet_fc_rcv_fcp_abort(phba->targetport, rsp);
170686c67379SJames Smart 
170786c67379SJames Smart 		/* Respond with BA_ACC accordingly */
170886c67379SJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
170986c67379SJames Smart 		return 0;
171086c67379SJames Smart 	}
17115e5b511dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
171286c67379SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
171386c67379SJames Smart 
171479d8c4ceSJames Smart 	/* check the wait list */
171579d8c4ceSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
171679d8c4ceSJames Smart 		struct rqb_dmabuf *nvmebuf;
171779d8c4ceSJames Smart 		struct fc_frame_header *fc_hdr_tmp;
171879d8c4ceSJames Smart 		u32 sid_tmp;
171979d8c4ceSJames Smart 		u16 oxid_tmp;
172079d8c4ceSJames Smart 		bool found = false;
172179d8c4ceSJames Smart 
172279d8c4ceSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
172379d8c4ceSJames Smart 
172479d8c4ceSJames Smart 		/* match by oxid and s_id */
172579d8c4ceSJames Smart 		list_for_each_entry(nvmebuf,
172679d8c4ceSJames Smart 				    &phba->sli4_hba.lpfc_nvmet_io_wait_list,
172779d8c4ceSJames Smart 				    hbuf.list) {
172879d8c4ceSJames Smart 			fc_hdr_tmp = (struct fc_frame_header *)
172979d8c4ceSJames Smart 					(nvmebuf->hbuf.virt);
173079d8c4ceSJames Smart 			oxid_tmp = be16_to_cpu(fc_hdr_tmp->fh_ox_id);
173179d8c4ceSJames Smart 			sid_tmp = sli4_sid_from_fc_hdr(fc_hdr_tmp);
173279d8c4ceSJames Smart 			if (oxid_tmp != oxid || sid_tmp != sid)
173379d8c4ceSJames Smart 				continue;
173486c67379SJames Smart 
173586c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
173679d8c4ceSJames Smart 					"6321 NVMET Rcv ABTS oxid x%x from x%x "
173779d8c4ceSJames Smart 					"is waiting for a ctxp\n",
173879d8c4ceSJames Smart 					oxid, sid);
173979d8c4ceSJames Smart 
174079d8c4ceSJames Smart 			list_del_init(&nvmebuf->hbuf.list);
174179d8c4ceSJames Smart 			phba->sli4_hba.nvmet_io_wait_cnt--;
174279d8c4ceSJames Smart 			found = true;
174379d8c4ceSJames Smart 			break;
174479d8c4ceSJames Smart 		}
174579d8c4ceSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
174679d8c4ceSJames Smart 				       iflag);
174779d8c4ceSJames Smart 
174879d8c4ceSJames Smart 		/* free buffer since already posted a new DMA buffer to RQ */
174979d8c4ceSJames Smart 		if (found) {
175079d8c4ceSJames Smart 			nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
175179d8c4ceSJames Smart 			/* Respond with BA_ACC accordingly */
175279d8c4ceSJames Smart 			lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
175379d8c4ceSJames Smart 			return 0;
175479d8c4ceSJames Smart 		}
175579d8c4ceSJames Smart 	}
175679d8c4ceSJames Smart 
175779d8c4ceSJames Smart 	/* check active list */
175879d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_oxid(phba, oxid, sid);
175979d8c4ceSJames Smart 	if (ctxp) {
176079d8c4ceSJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
176179d8c4ceSJames Smart 
176279d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
176379d8c4ceSJames Smart 		ctxp->flag |= (LPFC_NVMET_ABTS_RCV | LPFC_NVMET_ABORT_OP);
176479d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
176579d8c4ceSJames Smart 
176679d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
176779d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
176879d8c4ceSJames Smart 				 xri, raw_smp_processor_id(), 0);
176979d8c4ceSJames Smart 
177079d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
177179d8c4ceSJames Smart 				"6322 NVMET Rcv ABTS:acc oxid x%x xri x%x "
177279d8c4ceSJames Smart 				"flag x%x state x%x\n",
177379d8c4ceSJames Smart 				ctxp->oxid, xri, ctxp->flag, ctxp->state);
177479d8c4ceSJames Smart 
177579d8c4ceSJames Smart 		if (ctxp->flag & LPFC_NVMET_TNOTIFY) {
177679d8c4ceSJames Smart 			/* Notify the transport */
177779d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport,
177879d8c4ceSJames Smart 					       &ctxp->ctx.fcp_req);
177979d8c4ceSJames Smart 		} else {
17806594d31bSJames Smart 			cancel_work_sync(&ctxp->ctxbuf->defer_work);
178179d8c4ceSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
178279d8c4ceSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
178379d8c4ceSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
178479d8c4ceSJames Smart 		}
178579d8c4ceSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
178679d8c4ceSJames Smart 					       ctxp->oxid);
178779d8c4ceSJames Smart 
17886594d31bSJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
178979d8c4ceSJames Smart 		return 0;
179079d8c4ceSJames Smart 	}
179179d8c4ceSJames Smart 
179279d8c4ceSJames Smart 	lpfc_nvmeio_data(phba, "NVMET ABTS RCV: oxid x%x CPU %02x rjt %d\n",
179379d8c4ceSJames Smart 			 oxid, raw_smp_processor_id(), 1);
179479d8c4ceSJames Smart 
179579d8c4ceSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
179679d8c4ceSJames Smart 			"6320 NVMET Rcv ABTS:rjt oxid x%x\n", oxid);
179786c67379SJames Smart 
179886c67379SJames Smart 	/* Respond with BA_RJT accordingly */
179986c67379SJames Smart 	lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 0);
180086c67379SJames Smart #endif
1801b06e13c3SJens Axboe 	return 0;
1802318083adSJames Smart }
1803318083adSJames Smart 
18046e8e1c14SJames Smart static void
18056e8e1c14SJames Smart lpfc_nvmet_wqfull_flush(struct lpfc_hba *phba, struct lpfc_queue *wq,
18066e8e1c14SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
18076e8e1c14SJames Smart {
18086e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
18096e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
18106e8e1c14SJames Smart 	struct lpfc_iocbq *next_nvmewqeq;
18116e8e1c14SJames Smart 	unsigned long iflags;
18126e8e1c14SJames Smart 	struct lpfc_wcqe_complete wcqe;
18136e8e1c14SJames Smart 	struct lpfc_wcqe_complete *wcqep;
18146e8e1c14SJames Smart 
18156e8e1c14SJames Smart 	pring = wq->pring;
18166e8e1c14SJames Smart 	wcqep = &wcqe;
18176e8e1c14SJames Smart 
18186e8e1c14SJames Smart 	/* Fake an ABORT error code back to cmpl routine */
18196e8e1c14SJames Smart 	memset(wcqep, 0, sizeof(struct lpfc_wcqe_complete));
18206e8e1c14SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, IOSTAT_LOCAL_REJECT);
18216e8e1c14SJames Smart 	wcqep->parameter = IOERR_ABORT_REQUESTED;
18226e8e1c14SJames Smart 
18236e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
18246e8e1c14SJames Smart 	list_for_each_entry_safe(nvmewqeq, next_nvmewqeq,
18256e8e1c14SJames Smart 				 &wq->wqfull_list, list) {
18266e8e1c14SJames Smart 		if (ctxp) {
18276e8e1c14SJames Smart 			/* Checking for a specific IO to flush */
18286e8e1c14SJames Smart 			if (nvmewqeq->context2 == ctxp) {
18296e8e1c14SJames Smart 				list_del(&nvmewqeq->list);
18306e8e1c14SJames Smart 				spin_unlock_irqrestore(&pring->ring_lock,
18316e8e1c14SJames Smart 						       iflags);
18326e8e1c14SJames Smart 				lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq,
18336e8e1c14SJames Smart 							  wcqep);
18346e8e1c14SJames Smart 				return;
18356e8e1c14SJames Smart 			}
18366e8e1c14SJames Smart 			continue;
18376e8e1c14SJames Smart 		} else {
18386e8e1c14SJames Smart 			/* Flush all IOs */
18396e8e1c14SJames Smart 			list_del(&nvmewqeq->list);
18406e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
18416e8e1c14SJames Smart 			lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq, wcqep);
18426e8e1c14SJames Smart 			spin_lock_irqsave(&pring->ring_lock, iflags);
18436e8e1c14SJames Smart 		}
18446e8e1c14SJames Smart 	}
18456e8e1c14SJames Smart 	if (!ctxp)
18466e8e1c14SJames Smart 		wq->q_flag &= ~HBA_NVMET_WQFULL;
18476e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
18486e8e1c14SJames Smart }
18496e8e1c14SJames Smart 
18506e8e1c14SJames Smart void
18516e8e1c14SJames Smart lpfc_nvmet_wqfull_process(struct lpfc_hba *phba,
18526e8e1c14SJames Smart 			  struct lpfc_queue *wq)
18536e8e1c14SJames Smart {
18546e8e1c14SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
18556e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
18566e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
18571fbf9742SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
18586e8e1c14SJames Smart 	unsigned long iflags;
18596e8e1c14SJames Smart 	int rc;
18606e8e1c14SJames Smart 
18616e8e1c14SJames Smart 	/*
18626e8e1c14SJames Smart 	 * Some WQE slots are available, so try to re-issue anything
18636e8e1c14SJames Smart 	 * on the WQ wqfull_list.
18646e8e1c14SJames Smart 	 */
18656e8e1c14SJames Smart 	pring = wq->pring;
18666e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
18676e8e1c14SJames Smart 	while (!list_empty(&wq->wqfull_list)) {
18686e8e1c14SJames Smart 		list_remove_head(&wq->wqfull_list, nvmewqeq, struct lpfc_iocbq,
18696e8e1c14SJames Smart 				 list);
18706e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
18711fbf9742SJames Smart 		ctxp = (struct lpfc_nvmet_rcv_ctx *)nvmewqeq->context2;
18721fbf9742SJames Smart 		rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
18736e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
18746e8e1c14SJames Smart 		if (rc == -EBUSY) {
18756e8e1c14SJames Smart 			/* WQ was full again, so put it back on the list */
18766e8e1c14SJames Smart 			list_add(&nvmewqeq->list, &wq->wqfull_list);
18776e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
18786e8e1c14SJames Smart 			return;
18796e8e1c14SJames Smart 		}
188079d8c4ceSJames Smart 		if (rc == WQE_SUCCESS) {
188179d8c4ceSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
188279d8c4ceSJames Smart 			if (ctxp->ts_cmd_nvme) {
188379d8c4ceSJames Smart 				if (ctxp->ctx.fcp_req.op == NVMET_FCOP_RSP)
188479d8c4ceSJames Smart 					ctxp->ts_status_wqput = ktime_get_ns();
188579d8c4ceSJames Smart 				else
188679d8c4ceSJames Smart 					ctxp->ts_data_wqput = ktime_get_ns();
188779d8c4ceSJames Smart 			}
188879d8c4ceSJames Smart #endif
188979d8c4ceSJames Smart 		} else {
189079d8c4ceSJames Smart 			WARN_ON(rc);
189179d8c4ceSJames Smart 		}
18926e8e1c14SJames Smart 	}
18936e8e1c14SJames Smart 	wq->q_flag &= ~HBA_NVMET_WQFULL;
18946e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
18956e8e1c14SJames Smart 
18966e8e1c14SJames Smart #endif
18976e8e1c14SJames Smart }
18986e8e1c14SJames Smart 
1899d613b6a7SJames Smart void
1900d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
1901d613b6a7SJames Smart {
19027d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1903d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
19046e8e1c14SJames Smart 	struct lpfc_queue *wq;
19056e8e1c14SJames Smart 	uint32_t qidx;
1906c41f5988SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(tport_unreg_cmp);
1907d613b6a7SJames Smart 
1908d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
1909d613b6a7SJames Smart 		return;
1910d613b6a7SJames Smart 	if (phba->targetport) {
1911d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1912cdb42becSJames Smart 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
1913c00f62e6SJames Smart 			wq = phba->sli4_hba.hdwq[qidx].io_wq;
19146e8e1c14SJames Smart 			lpfc_nvmet_wqfull_flush(phba, wq, NULL);
19156e8e1c14SJames Smart 		}
1916c41f5988SEwan D. Milne 		tgtp->tport_unreg_cmp = &tport_unreg_cmp;
1917d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
19182a0fb340SJames Smart 		if (!wait_for_completion_timeout(tgtp->tport_unreg_cmp,
19192a0fb340SJames Smart 					msecs_to_jiffies(LPFC_NVMET_WAIT_TMO)))
19202a0fb340SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
192132350664SJames Smart 					"6179 Unreg targetport x%px timeout "
19222a0fb340SJames Smart 					"reached.\n", phba->targetport);
19236c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
1924d613b6a7SJames Smart 	}
1925d613b6a7SJames Smart 	phba->targetport = NULL;
1926166d7211SJames Smart #endif
1927d613b6a7SJames Smart }
1928d613b6a7SJames Smart 
1929d613b6a7SJames Smart /**
1930d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer - Process an unsolicited event data buffer
1931d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
1932d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
1933d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
1934d613b6a7SJames Smart  *
1935d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
1936d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
1937d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
1938d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
1939d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
1940d613b6a7SJames Smart  * of the discovery state machine.
1941d613b6a7SJames Smart  **/
1942d613b6a7SJames Smart static void
1943d613b6a7SJames Smart lpfc_nvmet_unsol_ls_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1944d613b6a7SJames Smart 			   struct hbq_dmabuf *nvmebuf)
1945d613b6a7SJames Smart {
19467d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1947d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1948d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
1949d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1950d613b6a7SJames Smart 	uint32_t *payload;
1951d613b6a7SJames Smart 	uint32_t size, oxid, sid, rc;
1952d613b6a7SJames Smart 
195368c9b55dSJames Smart 
195443bfea1bSJames Smart 	if (!nvmebuf || !phba->targetport) {
1955d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
195643bfea1bSJames Smart 				"6154 LS Drop IO\n");
19572b65e182SJames Smart 		oxid = 0;
19582b65e182SJames Smart 		size = 0;
19592b65e182SJames Smart 		sid = 0;
1960547077a4SJames Smart 		ctxp = NULL;
1961d613b6a7SJames Smart 		goto dropit;
1962d613b6a7SJames Smart 	}
1963d613b6a7SJames Smart 
196443bfea1bSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
196543bfea1bSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
196643bfea1bSJames Smart 
1967d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1968d613b6a7SJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
1969d613b6a7SJames Smart 	size = bf_get(lpfc_rcqe_length,  &nvmebuf->cq_event.cqe.rcqe_cmpl);
1970d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
1971d613b6a7SJames Smart 
1972d613b6a7SJames Smart 	ctxp = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx), GFP_ATOMIC);
1973d613b6a7SJames Smart 	if (ctxp == NULL) {
1974d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_drop);
1975d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1976d613b6a7SJames Smart 				"6155 LS Drop IO x%x: Alloc\n",
1977d613b6a7SJames Smart 				oxid);
1978d613b6a7SJames Smart dropit:
19792b65e182SJames Smart 		lpfc_nvmeio_data(phba, "NVMET LS  DROP: "
19802b65e182SJames Smart 				 "xri x%x sz %d from %06x\n",
19812b65e182SJames Smart 				 oxid, size, sid);
1982d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1983d613b6a7SJames Smart 		return;
1984d613b6a7SJames Smart 	}
1985d613b6a7SJames Smart 	ctxp->phba = phba;
1986d613b6a7SJames Smart 	ctxp->size = size;
1987d613b6a7SJames Smart 	ctxp->oxid = oxid;
1988d613b6a7SJames Smart 	ctxp->sid = sid;
1989d613b6a7SJames Smart 	ctxp->wqeq = NULL;
1990ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_LS_RCV;
1991ce1b591cSJames Smart 	ctxp->entry_cnt = 1;
1992d613b6a7SJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
19931fbf9742SJames Smart 	ctxp->hdwq = &phba->sli4_hba.hdwq[0];
19942b65e182SJames Smart 
19952b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS   RCV: xri x%x sz %d from %06x\n",
19962b65e182SJames Smart 			 oxid, size, sid);
1997d613b6a7SJames Smart 	/*
1998d613b6a7SJames Smart 	 * The calling sequence should be:
1999d613b6a7SJames Smart 	 * nvmet_fc_rcv_ls_req -> lpfc_nvmet_xmt_ls_rsp/cmp ->_req->done
2000d613b6a7SJames Smart 	 * lpfc_nvmet_xmt_ls_rsp_cmp should free the allocated ctxp.
2001d613b6a7SJames Smart 	 */
2002d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
2003d613b6a7SJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, &ctxp->ctx.ls_req,
2004d613b6a7SJames Smart 				 payload, size);
2005d613b6a7SJames Smart 
2006d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2007ce1b591cSJames Smart 			"6037 NVMET Unsol rcv: sz %d rc %d: %08x %08x %08x "
2008ce1b591cSJames Smart 			"%08x %08x %08x\n", size, rc,
2009d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
2010d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
20112b65e182SJames Smart 
2012d613b6a7SJames Smart 	if (rc == 0) {
2013d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
2014d613b6a7SJames Smart 		return;
2015d613b6a7SJames Smart 	}
20162b65e182SJames Smart 
20172b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  DROP: xri x%x sz %d from %06x\n",
20182b65e182SJames Smart 			 oxid, size, sid);
20192b65e182SJames Smart 
2020d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
2021d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2022d613b6a7SJames Smart 			"6156 LS Drop IO x%x: nvmet_fc_rcv_ls_req %d\n",
2023d613b6a7SJames Smart 			ctxp->oxid, rc);
2024d613b6a7SJames Smart 
2025d613b6a7SJames Smart 	/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
2026d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
2027d613b6a7SJames Smart 
2028d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_ls_abort);
2029d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, sid, oxid);
2030166d7211SJames Smart #endif
2031d613b6a7SJames Smart }
2032d613b6a7SJames Smart 
2033472e146dSJames Smart static void
2034472e146dSJames Smart lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf)
2035472e146dSJames Smart {
2036472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2037472e146dSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
2038472e146dSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
2039472e146dSJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
2040472e146dSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
20414552e0f6SJames Smart 	uint32_t *payload, qno;
2042472e146dSJames Smart 	uint32_t rc;
2043472e146dSJames Smart 	unsigned long iflags;
2044472e146dSJames Smart 
2045472e146dSJames Smart 	if (!nvmebuf) {
2046472e146dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2047472e146dSJames Smart 			"6159 process_rcv_fcp_req, nvmebuf is NULL, "
2048472e146dSJames Smart 			"oxid: x%x flg: x%x state: x%x\n",
2049472e146dSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
2050472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
2051472e146dSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2052472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2053472e146dSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
2054472e146dSJames Smart 						 ctxp->oxid);
2055472e146dSJames Smart 		return;
2056472e146dSJames Smart 	}
2057472e146dSJames Smart 
205879d8c4ceSJames Smart 	if (ctxp->flag & LPFC_NVMET_ABTS_RCV) {
205979d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
206079d8c4ceSJames Smart 				"6324 IO oxid x%x aborted\n",
206179d8c4ceSJames Smart 				ctxp->oxid);
206279d8c4ceSJames Smart 		return;
206379d8c4ceSJames Smart 	}
206479d8c4ceSJames Smart 
2065472e146dSJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
2066472e146dSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
206779d8c4ceSJames Smart 	ctxp->flag |= LPFC_NVMET_TNOTIFY;
2068d74a89aaSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2069d74a89aaSJames Smart 	if (ctxp->ts_isr_cmd)
2070d74a89aaSJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
2071d74a89aaSJames Smart #endif
2072472e146dSJames Smart 	/*
2073472e146dSJames Smart 	 * The calling sequence should be:
2074472e146dSJames Smart 	 * nvmet_fc_rcv_fcp_req->lpfc_nvmet_xmt_fcp_op/cmp- req->done
2075472e146dSJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
2076472e146dSJames Smart 	 * When we return from nvmet_fc_rcv_fcp_req, all relevant info
2077472e146dSJames Smart 	 * the NVME command / FC header is stored.
2078472e146dSJames Smart 	 * A buffer has already been reposted for this IO, so just free
2079472e146dSJames Smart 	 * the nvmebuf.
2080472e146dSJames Smart 	 */
2081472e146dSJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->ctx.fcp_req,
2082472e146dSJames Smart 				  payload, ctxp->size);
2083472e146dSJames Smart 	/* Process FCP command */
2084472e146dSJames Smart 	if (rc == 0) {
2085472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
20864552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
20874552e0f6SJames Smart 		if ((ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) ||
20884552e0f6SJames Smart 		    (nvmebuf != ctxp->rqb_buffer)) {
20894552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
20904552e0f6SJames Smart 			return;
20914552e0f6SJames Smart 		}
20924552e0f6SJames Smart 		ctxp->rqb_buffer = NULL;
20934552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
20944552e0f6SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
2095472e146dSJames Smart 		return;
2096472e146dSJames Smart 	}
2097472e146dSJames Smart 
2098472e146dSJames Smart 	/* Processing of FCP command is deferred */
2099472e146dSJames Smart 	if (rc == -EOVERFLOW) {
2100472e146dSJames Smart 		lpfc_nvmeio_data(phba, "NVMET RCV BUSY: xri x%x sz %d "
2101472e146dSJames Smart 				 "from %06x\n",
2102472e146dSJames Smart 				 ctxp->oxid, ctxp->size, ctxp->sid);
2103472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
2104472e146dSJames Smart 		atomic_inc(&tgtp->defer_fod);
21054552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
21064552e0f6SJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) {
21074552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
21084552e0f6SJames Smart 			return;
21094552e0f6SJames Smart 		}
21104552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
21114552e0f6SJames Smart 		/*
21124552e0f6SJames Smart 		 * Post a replacement DMA buffer to RQ and defer
21134552e0f6SJames Smart 		 * freeing rcv buffer till .defer_rcv callback
21144552e0f6SJames Smart 		 */
21154552e0f6SJames Smart 		qno = nvmebuf->idx;
21164552e0f6SJames Smart 		lpfc_post_rq_buffer(
21174552e0f6SJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
21184552e0f6SJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2119472e146dSJames Smart 		return;
2120472e146dSJames Smart 	}
212179d8c4ceSJames Smart 	ctxp->flag &= ~LPFC_NVMET_TNOTIFY;
2122472e146dSJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2123472e146dSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2124472e146dSJames Smart 			"2582 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
2125472e146dSJames Smart 			ctxp->oxid, rc,
2126472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_in),
2127472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_out),
2128472e146dSJames Smart 			atomic_read(&tgtp->xmt_fcp_release));
2129472e146dSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
2130472e146dSJames Smart 			 ctxp->oxid, ctxp->size, ctxp->sid);
2131472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflags);
2132472e146dSJames Smart 	lpfc_nvmet_defer_release(phba, ctxp);
2133472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2134472e146dSJames Smart 	lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
2135472e146dSJames Smart #endif
2136472e146dSJames Smart }
2137472e146dSJames Smart 
2138472e146dSJames Smart static void
2139472e146dSJames Smart lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *work)
2140472e146dSJames Smart {
2141472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2142472e146dSJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf =
2143472e146dSJames Smart 		container_of(work, struct lpfc_nvmet_ctxbuf, defer_work);
2144472e146dSJames Smart 
2145472e146dSJames Smart 	lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2146472e146dSJames Smart #endif
2147472e146dSJames Smart }
2148472e146dSJames Smart 
214966d7ce93SDick Kennedy static struct lpfc_nvmet_ctxbuf *
215066d7ce93SDick Kennedy lpfc_nvmet_replenish_context(struct lpfc_hba *phba,
215166d7ce93SDick Kennedy 			     struct lpfc_nvmet_ctx_info *current_infop)
215266d7ce93SDick Kennedy {
21532299e432SDick Kennedy #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
215466d7ce93SDick Kennedy 	struct lpfc_nvmet_ctxbuf *ctx_buf = NULL;
215566d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *get_infop;
215666d7ce93SDick Kennedy 	int i;
215766d7ce93SDick Kennedy 
215866d7ce93SDick Kennedy 	/*
215966d7ce93SDick Kennedy 	 * The current_infop for the MRQ a NVME command IU was received
216066d7ce93SDick Kennedy 	 * on is empty. Our goal is to replenish this MRQs context
216166d7ce93SDick Kennedy 	 * list from a another CPUs.
216266d7ce93SDick Kennedy 	 *
216366d7ce93SDick Kennedy 	 * First we need to pick a context list to start looking on.
216466d7ce93SDick Kennedy 	 * nvmet_ctx_start_cpu has available context the last time
216566d7ce93SDick Kennedy 	 * we needed to replenish this CPU where nvmet_ctx_next_cpu
216666d7ce93SDick Kennedy 	 * is just the next sequential CPU for this MRQ.
216766d7ce93SDick Kennedy 	 */
216866d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_start_cpu)
216966d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_start_cpu;
217066d7ce93SDick Kennedy 	else
217166d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_next_cpu;
217266d7ce93SDick Kennedy 
2173222e9239SJames Smart 	for (i = 0; i < phba->sli4_hba.num_possible_cpu; i++) {
217466d7ce93SDick Kennedy 		if (get_infop == current_infop) {
217566d7ce93SDick Kennedy 			get_infop = get_infop->nvmet_ctx_next_cpu;
217666d7ce93SDick Kennedy 			continue;
217766d7ce93SDick Kennedy 		}
217866d7ce93SDick Kennedy 		spin_lock(&get_infop->nvmet_ctx_list_lock);
217966d7ce93SDick Kennedy 
218066d7ce93SDick Kennedy 		/* Just take the entire context list, if there are any */
218166d7ce93SDick Kennedy 		if (get_infop->nvmet_ctx_list_cnt) {
218266d7ce93SDick Kennedy 			list_splice_init(&get_infop->nvmet_ctx_list,
218366d7ce93SDick Kennedy 				    &current_infop->nvmet_ctx_list);
218466d7ce93SDick Kennedy 			current_infop->nvmet_ctx_list_cnt =
218566d7ce93SDick Kennedy 				get_infop->nvmet_ctx_list_cnt - 1;
218666d7ce93SDick Kennedy 			get_infop->nvmet_ctx_list_cnt = 0;
218766d7ce93SDick Kennedy 			spin_unlock(&get_infop->nvmet_ctx_list_lock);
218866d7ce93SDick Kennedy 
218966d7ce93SDick Kennedy 			current_infop->nvmet_ctx_start_cpu = get_infop;
219066d7ce93SDick Kennedy 			list_remove_head(&current_infop->nvmet_ctx_list,
219166d7ce93SDick Kennedy 					 ctx_buf, struct lpfc_nvmet_ctxbuf,
219266d7ce93SDick Kennedy 					 list);
219366d7ce93SDick Kennedy 			return ctx_buf;
219466d7ce93SDick Kennedy 		}
219566d7ce93SDick Kennedy 
219666d7ce93SDick Kennedy 		/* Otherwise, move on to the next CPU for this MRQ */
219766d7ce93SDick Kennedy 		spin_unlock(&get_infop->nvmet_ctx_list_lock);
219866d7ce93SDick Kennedy 		get_infop = get_infop->nvmet_ctx_next_cpu;
219966d7ce93SDick Kennedy 	}
220066d7ce93SDick Kennedy 
22012299e432SDick Kennedy #endif
220266d7ce93SDick Kennedy 	/* Nothing found, all contexts for the MRQ are in-flight */
220366d7ce93SDick Kennedy 	return NULL;
220466d7ce93SDick Kennedy }
220566d7ce93SDick Kennedy 
2206d613b6a7SJames Smart /**
2207d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
2208d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
220966d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2210d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
2211d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2212d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2213d613b6a7SJames Smart  *
2214d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
2215d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
2216d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
2217d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
2218d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
2219d613b6a7SJames Smart  * of the discovery state machine.
2220d613b6a7SJames Smart  **/
2221d613b6a7SJames Smart static void
2222d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
222366d7ce93SDick Kennedy 			    uint32_t idx,
2224d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
2225d74a89aaSJames Smart 			    uint64_t isr_timestamp,
2226d74a89aaSJames Smart 			    uint8_t cqflag)
2227d613b6a7SJames Smart {
2228d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
2229d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2230d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
22316c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
223266d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *current_infop;
2233472e146dSJames Smart 	uint32_t size, oxid, sid, qno;
22346c621a22SJames Smart 	unsigned long iflag;
223566d7ce93SDick Kennedy 	int current_cpu;
2236d613b6a7SJames Smart 
223773626173SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_TARGET_FC))
223873626173SArnd Bergmann 		return;
223973626173SArnd Bergmann 
22406c621a22SJames Smart 	ctx_buf = NULL;
2241d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
2242d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
22436c621a22SJames Smart 				"6157 NVMET FCP Drop IO\n");
2244472e146dSJames Smart 		if (nvmebuf)
2245472e146dSJames Smart 			lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2246472e146dSJames Smart 		return;
2247d613b6a7SJames Smart 	}
2248d613b6a7SJames Smart 
224966d7ce93SDick Kennedy 	/*
225066d7ce93SDick Kennedy 	 * Get a pointer to the context list for this MRQ based on
225166d7ce93SDick Kennedy 	 * the CPU this MRQ IRQ is associated with. If the CPU association
225266d7ce93SDick Kennedy 	 * changes from our initial assumption, the context list could
225366d7ce93SDick Kennedy 	 * be empty, thus it would need to be replenished with the
225466d7ce93SDick Kennedy 	 * context list from another CPU for this MRQ.
225566d7ce93SDick Kennedy 	 */
2256d6d189ceSBart Van Assche 	current_cpu = raw_smp_processor_id();
225766d7ce93SDick Kennedy 	current_infop = lpfc_get_ctx_list(phba, current_cpu, idx);
225866d7ce93SDick Kennedy 	spin_lock_irqsave(&current_infop->nvmet_ctx_list_lock, iflag);
225966d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_list_cnt) {
226066d7ce93SDick Kennedy 		list_remove_head(&current_infop->nvmet_ctx_list,
22616c621a22SJames Smart 				 ctx_buf, struct lpfc_nvmet_ctxbuf, list);
226266d7ce93SDick Kennedy 		current_infop->nvmet_ctx_list_cnt--;
2263966bb5b7SJames Smart 	} else {
226466d7ce93SDick Kennedy 		ctx_buf = lpfc_nvmet_replenish_context(phba, current_infop);
22656c621a22SJames Smart 	}
226666d7ce93SDick Kennedy 	spin_unlock_irqrestore(&current_infop->nvmet_ctx_list_lock, iflag);
22676c621a22SJames Smart 
2268a8cf5dfeSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
2269a8cf5dfeSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2270a8cf5dfeSJames Smart 	size = nvmebuf->bytes_recv;
2271a8cf5dfeSJames Smart 
2272a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2273a8cf5dfeSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_RCV) {
227463df6d63SJames Smart 		if (current_cpu < LPFC_CHECK_CPU_CNT) {
227563df6d63SJames Smart 			if (idx != current_cpu)
227663df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
227763df6d63SJames Smart 						"6703 CPU Check rcv: "
227863df6d63SJames Smart 						"cpu %d expect %d\n",
227963df6d63SJames Smart 						current_cpu, idx);
228063df6d63SJames Smart 			phba->sli4_hba.hdwq[idx].cpucheck_rcv_io[current_cpu]++;
228163df6d63SJames Smart 		}
2282a8cf5dfeSJames Smart 	}
2283a8cf5dfeSJames Smart #endif
2284a8cf5dfeSJames Smart 
2285a8cf5dfeSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d CPU %02x\n",
2286d6d189ceSBart Van Assche 			 oxid, size, raw_smp_processor_id());
2287a8cf5dfeSJames Smart 
2288411de511SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2289411de511SJames Smart 
22906c621a22SJames Smart 	if (!ctx_buf) {
2291a8cf5dfeSJames Smart 		/* Queue this NVME IO to process later */
2292a8cf5dfeSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
2293a8cf5dfeSJames Smart 		list_add_tail(&nvmebuf->hbuf.list,
2294a8cf5dfeSJames Smart 			      &phba->sli4_hba.lpfc_nvmet_io_wait_list);
2295a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt++;
2296a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_total++;
2297a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
2298a8cf5dfeSJames Smart 				       iflag);
2299a8cf5dfeSJames Smart 
2300a8cf5dfeSJames Smart 		/* Post a brand new DMA buffer to RQ */
2301a8cf5dfeSJames Smart 		qno = nvmebuf->idx;
2302a8cf5dfeSJames Smart 		lpfc_post_rq_buffer(
2303a8cf5dfeSJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
2304a8cf5dfeSJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2305411de511SJames Smart 
2306411de511SJames Smart 		atomic_inc(&tgtp->defer_ctx);
2307a8cf5dfeSJames Smart 		return;
23086c621a22SJames Smart 	}
2309d613b6a7SJames Smart 
2310d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
2311d613b6a7SJames Smart 
23126c621a22SJames Smart 	ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
231379d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
231479d8c4ceSJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.t_active_ctx_list);
231579d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
2316ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_FREE) {
2317ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2318ce1b591cSJames Smart 				"6414 NVMET Context corrupt %d %d oxid x%x\n",
2319ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
2320ce1b591cSJames Smart 	}
2321d613b6a7SJames Smart 	ctxp->wqeq = NULL;
2322d613b6a7SJames Smart 	ctxp->offset = 0;
2323d613b6a7SJames Smart 	ctxp->phba = phba;
2324d613b6a7SJames Smart 	ctxp->size = size;
2325d613b6a7SJames Smart 	ctxp->oxid = oxid;
2326d613b6a7SJames Smart 	ctxp->sid = sid;
232766d7ce93SDick Kennedy 	ctxp->idx = idx;
2328d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_RCV;
2329d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
2330d613b6a7SJames Smart 	ctxp->flag = 0;
23316c621a22SJames Smart 	ctxp->ctxbuf = ctx_buf;
2332cbc5de1bSJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
23331fbf9742SJames Smart 	ctxp->hdwq = NULL;
23342b7824d0SJames Smart 	spin_lock_init(&ctxp->ctxlock);
2335d613b6a7SJames Smart 
23362b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2337d74a89aaSJames Smart 	if (isr_timestamp)
23382b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
2339d74a89aaSJames Smart 	ctxp->ts_cmd_nvme = 0;
23402b65e182SJames Smart 	ctxp->ts_nvme_data = 0;
23412b65e182SJames Smart 	ctxp->ts_data_wqput = 0;
23422b65e182SJames Smart 	ctxp->ts_isr_data = 0;
23432b65e182SJames Smart 	ctxp->ts_data_nvme = 0;
23442b65e182SJames Smart 	ctxp->ts_nvme_status = 0;
23452b65e182SJames Smart 	ctxp->ts_status_wqput = 0;
23462b65e182SJames Smart 	ctxp->ts_isr_status = 0;
23472b65e182SJames Smart 	ctxp->ts_status_nvme = 0;
23482b65e182SJames Smart #endif
23492b65e182SJames Smart 
2350d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
2351d74a89aaSJames Smart 	/* check for cq processing load */
2352d74a89aaSJames Smart 	if (!cqflag) {
2353472e146dSJames Smart 		lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2354d74a89aaSJames Smart 		return;
2355d74a89aaSJames Smart 	}
2356d74a89aaSJames Smart 
2357d74a89aaSJames Smart 	if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
2358d74a89aaSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2359d74a89aaSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
2360d74a89aaSJames Smart 				"6325 Unable to queue work for oxid x%x. "
2361d74a89aaSJames Smart 				"FCP Drop IO [x%x x%x x%x]\n",
2362d74a89aaSJames Smart 				ctxp->oxid,
2363d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_in),
2364d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_out),
2365d74a89aaSJames Smart 				atomic_read(&tgtp->xmt_fcp_release));
2366d74a89aaSJames Smart 
2367d74a89aaSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
2368d74a89aaSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2369d74a89aaSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
2370d74a89aaSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
2371d74a89aaSJames Smart 	}
2372d613b6a7SJames Smart }
2373d613b6a7SJames Smart 
2374d613b6a7SJames Smart /**
2375d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_event - Process an unsolicited event from an nvme nport
2376d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
2377d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
2378d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2379d613b6a7SJames Smart  *
2380d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2381d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2382d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2383d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer() after properly set up the buffer from the
2384d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2385d613b6a7SJames Smart  **/
2386d613b6a7SJames Smart void
2387d613b6a7SJames Smart lpfc_nvmet_unsol_ls_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2388d613b6a7SJames Smart 			  struct lpfc_iocbq *piocb)
2389d613b6a7SJames Smart {
2390d613b6a7SJames Smart 	struct lpfc_dmabuf *d_buf;
2391d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf;
2392d613b6a7SJames Smart 
2393d613b6a7SJames Smart 	d_buf = piocb->context2;
2394d613b6a7SJames Smart 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2395d613b6a7SJames Smart 
239643bfea1bSJames Smart 	if (!nvmebuf) {
239743bfea1bSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
239843bfea1bSJames Smart 				"3015 LS Drop IO\n");
239943bfea1bSJames Smart 		return;
240043bfea1bSJames Smart 	}
2401d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
2402d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
2403d613b6a7SJames Smart 		return;
2404d613b6a7SJames Smart 	}
2405d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_buffer(phba, pring, nvmebuf);
2406d613b6a7SJames Smart }
2407d613b6a7SJames Smart 
2408d613b6a7SJames Smart /**
2409d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
2410d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
241166d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2412d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2413d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2414d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2415d613b6a7SJames Smart  *
2416d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2417d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2418d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2419d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
2420d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2421d613b6a7SJames Smart  **/
2422d613b6a7SJames Smart void
2423d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
242466d7ce93SDick Kennedy 			   uint32_t idx,
2425d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
2426d74a89aaSJames Smart 			   uint64_t isr_timestamp,
2427d74a89aaSJames Smart 			   uint8_t cqflag)
2428d613b6a7SJames Smart {
242943bfea1bSJames Smart 	if (!nvmebuf) {
243043bfea1bSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
243143bfea1bSJames Smart 				"3167 NVMET FCP Drop IO\n");
243243bfea1bSJames Smart 		return;
243343bfea1bSJames Smart 	}
2434d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
24356c621a22SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2436d613b6a7SJames Smart 		return;
2437d613b6a7SJames Smart 	}
2438d74a89aaSJames Smart 	lpfc_nvmet_unsol_fcp_buffer(phba, idx, nvmebuf, isr_timestamp, cqflag);
2439d613b6a7SJames Smart }
2440d613b6a7SJames Smart 
2441d613b6a7SJames Smart /**
2442d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
2443d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
2444d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
2445d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
2446d613b6a7SJames Smart  * @rspsize: size of the NVME command.
2447d613b6a7SJames Smart  *
2448d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
2449d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
2450d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
2451d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
2452d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
2453d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
2454d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
2455d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
2456d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
2457d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
2458d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
2459d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
2460d613b6a7SJames Smart  * reference for the command's callback function to access later.
2461d613b6a7SJames Smart  *
2462d613b6a7SJames Smart  * Return code
2463d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
2464d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
2465d613b6a7SJames Smart  **/
2466d613b6a7SJames Smart static struct lpfc_iocbq *
2467d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
2468d613b6a7SJames Smart 		       struct lpfc_nvmet_rcv_ctx *ctxp,
2469d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
2470d613b6a7SJames Smart {
2471d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2472d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2473205e8240SJames Smart 	union lpfc_wqe128 *wqe;
2474d613b6a7SJames Smart 
2475d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2476d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2477ce1b591cSJames Smart 				"6104 NVMET prep LS wqe: link err: "
2478ce1b591cSJames Smart 				"NPORT x%x oxid:x%x ste %d\n",
2479ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2480d613b6a7SJames Smart 		return NULL;
2481d613b6a7SJames Smart 	}
2482d613b6a7SJames Smart 
2483d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
2484d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
2485d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2486d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2487ce1b591cSJames Smart 				"6105 NVMET prep LS wqe: No WQE: "
2488ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2489ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2490d613b6a7SJames Smart 		return NULL;
2491d613b6a7SJames Smart 	}
2492d613b6a7SJames Smart 
2493d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2494d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2495d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2496d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2497d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2498ce1b591cSJames Smart 				"6106 NVMET prep LS wqe: No ndlp: "
2499ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2500ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2501d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2502d613b6a7SJames Smart 	}
2503d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
2504d613b6a7SJames Smart 
2505d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
2506d613b6a7SJames Smart 	nvmewqe->context1 = lpfc_nlp_get(ndlp);
2507d613b6a7SJames Smart 	if (nvmewqe->context1 == NULL)
2508d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2509d613b6a7SJames Smart 	nvmewqe->context2 = ctxp;
2510d613b6a7SJames Smart 
2511d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
2512d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
2513d613b6a7SJames Smart 
2514d613b6a7SJames Smart 	/* Words 0 - 2 */
2515d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2516d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
2517d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
2518d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
2519d613b6a7SJames Smart 
2520d613b6a7SJames Smart 	/* Word 3 */
2521d613b6a7SJames Smart 
2522d613b6a7SJames Smart 	/* Word 4 */
2523d613b6a7SJames Smart 
2524d613b6a7SJames Smart 	/* Word 5 */
2525d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
2526d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
2527d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
25288b361639SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
2529d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
2530d613b6a7SJames Smart 
2531d613b6a7SJames Smart 	/* Word 6 */
2532d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
2533d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2534d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
2535d613b6a7SJames Smart 
2536d613b6a7SJames Smart 	/* Word 7 */
2537d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
2538d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
2539d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
2540d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
2541d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
2542d613b6a7SJames Smart 
2543d613b6a7SJames Smart 	/* Word 8 */
2544d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
2545d613b6a7SJames Smart 
2546d613b6a7SJames Smart 	/* Word 9 */
2547d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
2548d613b6a7SJames Smart 	/* Needs to be set by caller */
2549d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
2550d613b6a7SJames Smart 
2551d613b6a7SJames Smart 	/* Word 10 */
2552d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
2553d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2554d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
2555d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
2556d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
2557d613b6a7SJames Smart 
2558d613b6a7SJames Smart 	/* Word 11 */
2559d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
2560d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
2561d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
2562d613b6a7SJames Smart 	       OTHER_COMMAND);
2563d613b6a7SJames Smart 
2564d613b6a7SJames Smart 	/* Word 12 */
2565d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
2566d613b6a7SJames Smart 
2567d613b6a7SJames Smart 	nvmewqe->retry = 1;
2568d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2569d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2570d613b6a7SJames Smart 	nvmewqe->iocb_flag |= LPFC_IO_NVME_LS;
2571d613b6a7SJames Smart 
2572ce1b591cSJames Smart 	/* Xmit NVMET response to remote NPORT <did> */
2573d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2574ce1b591cSJames Smart 			"6039 Xmit NVMET LS response to remote "
2575d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
2576d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
2577d613b6a7SJames Smart 			rspsize);
2578d613b6a7SJames Smart 	return nvmewqe;
2579d613b6a7SJames Smart 
2580d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
2581d613b6a7SJames Smart 	nvmewqe->context2 = NULL;
2582d613b6a7SJames Smart 	nvmewqe->context3 = NULL;
2583d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
2584d613b6a7SJames Smart 	return NULL;
2585d613b6a7SJames Smart }
2586d613b6a7SJames Smart 
2587d613b6a7SJames Smart 
2588d613b6a7SJames Smart static struct lpfc_iocbq *
2589d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
2590d613b6a7SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
2591d613b6a7SJames Smart {
2592d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->ctx.fcp_req;
2593d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2594d613b6a7SJames Smart 	struct sli4_sge *sgl;
2595d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2596d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2597d613b6a7SJames Smart 	struct scatterlist *sgel;
2598d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
25990bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
2600d613b6a7SJames Smart 	dma_addr_t physaddr;
2601e7f40349SJames Smart 	int i, cnt, nsegs;
26020bc2b7c5SJames Smart 	int do_pbde;
2603d613b6a7SJames Smart 	int xc = 1;
2604d613b6a7SJames Smart 
2605d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2606d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2607ce1b591cSJames Smart 				"6107 NVMET prep FCP wqe: link err:"
2608ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2609ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2610d613b6a7SJames Smart 		return NULL;
2611d613b6a7SJames Smart 	}
2612d613b6a7SJames Smart 
2613d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2614d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2615d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2616d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2617d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2618ce1b591cSJames Smart 				"6108 NVMET prep FCP wqe: no ndlp: "
2619ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2620ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2621d613b6a7SJames Smart 		return NULL;
2622d613b6a7SJames Smart 	}
2623d613b6a7SJames Smart 
262481e6a637SJames Smart 	if (rsp->sg_cnt > lpfc_tgttemplate.max_sgl_segments) {
2625d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2626ce1b591cSJames Smart 				"6109 NVMET prep FCP wqe: seg cnt err: "
2627ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d cnt %d\n",
2628ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state,
2629ce1b591cSJames Smart 				phba->cfg_nvme_seg_cnt);
2630d613b6a7SJames Smart 		return NULL;
2631d613b6a7SJames Smart 	}
2632e7f40349SJames Smart 	nsegs = rsp->sg_cnt;
2633d613b6a7SJames Smart 
2634d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2635d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
2636d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2637d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
26386c621a22SJames Smart 		nvmewqe = ctxp->ctxbuf->iocbq;
2639d613b6a7SJames Smart 		if (nvmewqe == NULL) {
2640d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2641ce1b591cSJames Smart 					"6110 NVMET prep FCP wqe: No "
2642ce1b591cSJames Smart 					"WQE: NPORT x%x oxid x%x ste %d\n",
2643ce1b591cSJames Smart 					ctxp->sid, ctxp->oxid, ctxp->state);
2644d613b6a7SJames Smart 			return NULL;
2645d613b6a7SJames Smart 		}
2646d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
2647d613b6a7SJames Smart 		xc = 0; /* create new XRI */
2648d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
2649d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
2650d613b6a7SJames Smart 	}
2651d613b6a7SJames Smart 
2652d613b6a7SJames Smart 	/* Sanity check */
2653d613b6a7SJames Smart 	if (((ctxp->state == LPFC_NVMET_STE_RCV) &&
2654d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
2655ce1b591cSJames Smart 	    (ctxp->state == LPFC_NVMET_STE_DATA)) {
2656205e8240SJames Smart 		wqe = &nvmewqe->wqe;
2657d613b6a7SJames Smart 	} else {
2658d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2659ce1b591cSJames Smart 				"6111 Wrong state NVMET FCP: %d  cnt %d\n",
2660ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt);
2661d613b6a7SJames Smart 		return NULL;
2662d613b6a7SJames Smart 	}
2663d613b6a7SJames Smart 
26646c621a22SJames Smart 	sgl  = (struct sli4_sge *)ctxp->ctxbuf->sglq->sgl;
2665d613b6a7SJames Smart 	switch (rsp->op) {
2666d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
2667d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
2668bd3061baSJames Smart 		/* From the tsend template, initialize words 7 - 11 */
2669bd3061baSJames Smart 		memcpy(&wqe->words[7],
2670bd3061baSJames Smart 		       &lpfc_tsend_cmd_template.words[7],
2671bd3061baSJames Smart 		       sizeof(uint32_t) * 5);
2672bd3061baSJames Smart 
2673d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2674d613b6a7SJames Smart 		sgel = &rsp->sg[0];
2675d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2676d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2677d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
2678d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
2679d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
2680d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2681d613b6a7SJames Smart 
2682d613b6a7SJames Smart 		/* Word 3 */
2683d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
2684d613b6a7SJames Smart 
2685d613b6a7SJames Smart 		/* Word 4 */
2686d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
2687d613b6a7SJames Smart 
2688d613b6a7SJames Smart 		/* Word 5 */
2689bd3061baSJames Smart 		wqe->fcp_tsend.reserved = 0;
2690d613b6a7SJames Smart 
2691d613b6a7SJames Smart 		/* Word 6 */
2692d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
2693d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2694d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
2695d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2696d613b6a7SJames Smart 
2697bd3061baSJames Smart 		/* Word 7 - set ar later */
2698d613b6a7SJames Smart 
2699d613b6a7SJames Smart 		/* Word 8 */
2700d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
2701d613b6a7SJames Smart 
2702d613b6a7SJames Smart 		/* Word 9 */
2703d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
2704d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
2705d613b6a7SJames Smart 
2706bd3061baSJames Smart 		/* Word 10 - set wqes later, in template xc=1 */
2707bd3061baSJames Smart 		if (!xc)
2708bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 0);
2709d613b6a7SJames Smart 
2710bd3061baSJames Smart 		/* Word 11 - set sup, irsp, irsplen later */
2711bd3061baSJames Smart 		do_pbde = 0;
2712d613b6a7SJames Smart 
2713d613b6a7SJames Smart 		/* Word 12 */
2714d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2715d613b6a7SJames Smart 
2716d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
2717d613b6a7SJames Smart 		sgl->addr_hi = 0;
2718d613b6a7SJames Smart 		sgl->addr_lo = 0;
2719d613b6a7SJames Smart 		sgl->word2 = 0;
2720d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2721d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2722d613b6a7SJames Smart 		sgl->sge_len = 0;
2723d613b6a7SJames Smart 		sgl++;
2724d613b6a7SJames Smart 		sgl->addr_hi = 0;
2725d613b6a7SJames Smart 		sgl->addr_lo = 0;
2726d613b6a7SJames Smart 		sgl->word2 = 0;
2727d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2728d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2729d613b6a7SJames Smart 		sgl->sge_len = 0;
2730d613b6a7SJames Smart 		sgl++;
2731d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
2732d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
2733bd3061baSJames Smart 
2734bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2735bd3061baSJames Smart 
273620aefac3SJames Smart 			if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
273720aefac3SJames Smart 				if (ndlp->nlp_flag & NLP_SUPPRESS_RSP)
273820aefac3SJames Smart 					bf_set(wqe_sup,
273920aefac3SJames Smart 					       &wqe->fcp_tsend.wqe_com, 1);
2740d613b6a7SJames Smart 			} else {
2741d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
2742d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
2743d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
2744d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
2745d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
2746d613b6a7SJames Smart 				       rsp->rsplen);
2747d613b6a7SJames Smart 			}
2748d613b6a7SJames Smart 		} else {
2749d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
2750d613b6a7SJames Smart 
2751bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2752d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
2753d613b6a7SJames Smart 		}
2754d613b6a7SJames Smart 		break;
2755d613b6a7SJames Smart 
2756d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
2757bd3061baSJames Smart 		/* From the treceive template, initialize words 3 - 11 */
2758bd3061baSJames Smart 		memcpy(&wqe->words[3],
2759bd3061baSJames Smart 		       &lpfc_treceive_cmd_template.words[3],
2760bd3061baSJames Smart 		       sizeof(uint32_t) * 9);
2761bd3061baSJames Smart 
2762ea85a20cSJames Smart 		/* Words 0 - 2 : First SGE is skipped, set invalid BDE type */
2763ea85a20cSJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = LPFC_SGE_TYPE_SKIP;
2764ea85a20cSJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = 0;
2765ea85a20cSJames Smart 		wqe->fcp_treceive.bde.addrLow = 0;
2766ea85a20cSJames Smart 		wqe->fcp_treceive.bde.addrHigh = 0;
2767d613b6a7SJames Smart 
2768d613b6a7SJames Smart 		/* Word 4 */
2769d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
2770d613b6a7SJames Smart 
2771d613b6a7SJames Smart 		/* Word 6 */
2772d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
2773d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2774d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
2775d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2776d613b6a7SJames Smart 
2777d613b6a7SJames Smart 		/* Word 7 */
2778d613b6a7SJames Smart 
2779d613b6a7SJames Smart 		/* Word 8 */
2780d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
2781d613b6a7SJames Smart 
2782d613b6a7SJames Smart 		/* Word 9 */
2783d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
2784d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
2785d613b6a7SJames Smart 
2786bd3061baSJames Smart 		/* Word 10 - in template xc=1 */
2787bd3061baSJames Smart 		if (!xc)
2788bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, 0);
2789d613b6a7SJames Smart 
2790bd3061baSJames Smart 		/* Word 11 - set pbde later */
2791414abe0aSJames Smart 		if (phba->cfg_enable_pbde) {
2792bd3061baSJames Smart 			do_pbde = 1;
2793bd3061baSJames Smart 		} else {
2794bd3061baSJames Smart 			bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 0);
2795bd3061baSJames Smart 			do_pbde = 0;
2796bd3061baSJames Smart 		}
2797d613b6a7SJames Smart 
2798d613b6a7SJames Smart 		/* Word 12 */
2799d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2800d613b6a7SJames Smart 
2801ea85a20cSJames Smart 		/* Setup 2 SKIP SGEs */
2802ea85a20cSJames Smart 		sgl->addr_hi = 0;
2803ea85a20cSJames Smart 		sgl->addr_lo = 0;
2804d613b6a7SJames Smart 		sgl->word2 = 0;
2805ea85a20cSJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2806d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2807ea85a20cSJames Smart 		sgl->sge_len = 0;
2808d613b6a7SJames Smart 		sgl++;
2809d613b6a7SJames Smart 		sgl->addr_hi = 0;
2810d613b6a7SJames Smart 		sgl->addr_lo = 0;
2811d613b6a7SJames Smart 		sgl->word2 = 0;
2812d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2813d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2814d613b6a7SJames Smart 		sgl->sge_len = 0;
2815d613b6a7SJames Smart 		sgl++;
2816d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
2817d613b6a7SJames Smart 		break;
2818d613b6a7SJames Smart 
2819d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
2820bd3061baSJames Smart 		/* From the treceive template, initialize words 4 - 11 */
2821bd3061baSJames Smart 		memcpy(&wqe->words[4],
2822bd3061baSJames Smart 		       &lpfc_trsp_cmd_template.words[4],
2823bd3061baSJames Smart 		       sizeof(uint32_t) * 8);
2824bd3061baSJames Smart 
2825d613b6a7SJames Smart 		/* Words 0 - 2 */
2826d613b6a7SJames Smart 		physaddr = rsp->rspdma;
2827d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2828d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
2829d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
2830d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2831d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
2832d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2833d613b6a7SJames Smart 
2834d613b6a7SJames Smart 		/* Word 3 */
2835d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
2836d613b6a7SJames Smart 
2837d613b6a7SJames Smart 		/* Word 6 */
2838d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
2839d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2840d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
2841d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2842d613b6a7SJames Smart 
2843d613b6a7SJames Smart 		/* Word 7 */
2844d613b6a7SJames Smart 
2845d613b6a7SJames Smart 		/* Word 8 */
2846d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
2847d613b6a7SJames Smart 
2848d613b6a7SJames Smart 		/* Word 9 */
2849d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
2850d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
2851d613b6a7SJames Smart 
2852d613b6a7SJames Smart 		/* Word 10 */
2853bd3061baSJames Smart 		if (xc)
2854bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 1);
2855d613b6a7SJames Smart 
2856d613b6a7SJames Smart 		/* Word 11 */
2857bd3061baSJames Smart 		/* In template wqes=0 irsp=0 irsplen=0 - good response */
2858bd3061baSJames Smart 		if (rsp->rsplen != LPFC_NVMET_SUCCESS_LEN) {
2859bd3061baSJames Smart 			/* Bad response - embed it */
2860d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
2861d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
2862d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
2863d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
2864d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
2865d613b6a7SJames Smart 		}
2866bd3061baSJames Smart 		do_pbde = 0;
2867bd3061baSJames Smart 
2868bd3061baSJames Smart 		/* Word 12 */
2869bd3061baSJames Smart 		wqe->fcp_trsp.rsvd_12_15[0] = 0;
2870d613b6a7SJames Smart 
2871d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
2872e7f40349SJames Smart 		nsegs = 0;
2873d613b6a7SJames Smart 		sgl->word2 = 0;
2874d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
2875d613b6a7SJames Smart 		break;
2876d613b6a7SJames Smart 
2877d613b6a7SJames Smart 	default:
2878d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2879d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
2880d613b6a7SJames Smart 				rsp->op);
2881d613b6a7SJames Smart 		return NULL;
2882d613b6a7SJames Smart 	}
2883d613b6a7SJames Smart 
2884d613b6a7SJames Smart 	nvmewqe->retry = 1;
2885d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2886d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2887d613b6a7SJames Smart 	nvmewqe->context1 = ndlp;
2888d613b6a7SJames Smart 
2889e7f40349SJames Smart 	for_each_sg(rsp->sg, sgel, nsegs, i) {
2890d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2891d613b6a7SJames Smart 		cnt = sg_dma_len(sgel);
2892d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
2893d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
2894d613b6a7SJames Smart 		sgl->word2 = 0;
2895d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2896d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
2897d613b6a7SJames Smart 		if ((i+1) == rsp->sg_cnt)
2898d613b6a7SJames Smart 			bf_set(lpfc_sli4_sge_last, sgl, 1);
2899d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2900d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(cnt);
2901414abe0aSJames Smart 		if (i == 0) {
29020bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)&wqe->words[13];
2903414abe0aSJames Smart 			if (do_pbde) {
29040bc2b7c5SJames Smart 				/* Words 13-15  (PBDE) */
29050bc2b7c5SJames Smart 				bde->addrLow = sgl->addr_lo;
29060bc2b7c5SJames Smart 				bde->addrHigh = sgl->addr_hi;
29070bc2b7c5SJames Smart 				bde->tus.f.bdeSize =
29080bc2b7c5SJames Smart 					le32_to_cpu(sgl->sge_len);
29090bc2b7c5SJames Smart 				bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
29100bc2b7c5SJames Smart 				bde->tus.w = cpu_to_le32(bde->tus.w);
2911414abe0aSJames Smart 			} else {
2912414abe0aSJames Smart 				memset(bde, 0, sizeof(struct ulp_bde64));
2913414abe0aSJames Smart 			}
29140bc2b7c5SJames Smart 		}
2915d613b6a7SJames Smart 		sgl++;
2916d613b6a7SJames Smart 		ctxp->offset += cnt;
2917d613b6a7SJames Smart 	}
2918ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_DATA;
2919ce1b591cSJames Smart 	ctxp->entry_cnt++;
2920d613b6a7SJames Smart 	return nvmewqe;
2921d613b6a7SJames Smart }
2922d613b6a7SJames Smart 
2923d613b6a7SJames Smart /**
2924d613b6a7SJames Smart  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
2925d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
2926d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
2927d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
2928d613b6a7SJames Smart  *
2929d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
2930d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
2931d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
2932d613b6a7SJames Smart  **/
2933d613b6a7SJames Smart static void
2934d613b6a7SJames Smart lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2935d613b6a7SJames Smart 			     struct lpfc_wcqe_complete *wcqe)
2936d613b6a7SJames Smart {
2937d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
2938d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2939b27cbd55SBart Van Assche 	uint32_t result;
294019b58d94SJames Smart 	unsigned long flags;
294119b58d94SJames Smart 	bool released = false;
2942d613b6a7SJames Smart 
2943d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
2944d613b6a7SJames Smart 	result = wcqe->parameter;
2945d613b6a7SJames Smart 
2946d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2947547077a4SJames Smart 	if (ctxp->flag & LPFC_NVMET_ABORT_OP)
2948547077a4SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
2949d613b6a7SJames Smart 
2950c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
2951d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_DONE;
295286c67379SJames Smart 
295386c67379SJames Smart 	/* Check if we already received a free context call
295486c67379SJames Smart 	 * and we have completed processing an abort situation.
295586c67379SJames Smart 	 */
295686c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_CTX_RLS) &&
295786c67379SJames Smart 	    !(ctxp->flag & LPFC_NVMET_XBUSY)) {
2958c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
295979d8c4ceSJames Smart 		list_del_init(&ctxp->list);
2960c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
296119b58d94SJames Smart 		released = true;
296286c67379SJames Smart 	}
296319b58d94SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
296419b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
2965547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
296619b58d94SJames Smart 
2967e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
296879d8c4ceSJames Smart 			"6165 ABORT cmpl: oxid x%x flg x%x (%d) "
296986c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
297086c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
297186c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
297286c67379SJames Smart 			result, wcqe->word3);
297386c67379SJames Smart 
29746c621a22SJames Smart 	cmdwqe->context2 = NULL;
29756c621a22SJames Smart 	cmdwqe->context3 = NULL;
297619b58d94SJames Smart 	/*
297719b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
297819b58d94SJames Smart 	 * will be recycled by transport release call.
297919b58d94SJames Smart 	 */
298019b58d94SJames Smart 	if (released)
29816c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
2982d613b6a7SJames Smart 
29836c621a22SJames Smart 	/* This is the iocbq for the abort, not the command */
2984d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
298586c67379SJames Smart 
298686c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
298786c67379SJames Smart 	 * For LPFC_NVMET_XBUSY, lpfc_sli4_nvmet_xri_aborted
298886c67379SJames Smart 	 * should have been called already.
298986c67379SJames Smart 	 */
2990d613b6a7SJames Smart }
2991d613b6a7SJames Smart 
2992d613b6a7SJames Smart /**
299386c67379SJames Smart  * lpfc_nvmet_unsol_fcp_abort_cmp - Completion handler for ABTS
2994d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
2995d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
2996d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
2997d613b6a7SJames Smart  *
2998d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
2999d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
3000d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3001d613b6a7SJames Smart  **/
3002d613b6a7SJames Smart static void
300386c67379SJames Smart lpfc_nvmet_unsol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3004d613b6a7SJames Smart 			       struct lpfc_wcqe_complete *wcqe)
3005d613b6a7SJames Smart {
3006d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
3007d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
300819b58d94SJames Smart 	unsigned long flags;
3009b27cbd55SBart Van Assche 	uint32_t result;
301019b58d94SJames Smart 	bool released = false;
3011d613b6a7SJames Smart 
3012d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
3013d613b6a7SJames Smart 	result = wcqe->parameter;
3014d613b6a7SJames Smart 
301586c67379SJames Smart 	if (!ctxp) {
301686c67379SJames Smart 		/* if context is clear, related io alrady complete */
3017d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
301886c67379SJames Smart 				"6070 ABTS cmpl: WCQE: %08x %08x %08x %08x\n",
301986c67379SJames Smart 				wcqe->word0, wcqe->total_data_placed,
3020d613b6a7SJames Smart 				result, wcqe->word3);
302186c67379SJames Smart 		return;
302286c67379SJames Smart 	}
3023d613b6a7SJames Smart 
302478e1d200SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3025c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
302678e1d200SJames Smart 	if (ctxp->flag & LPFC_NVMET_ABORT_OP)
302778e1d200SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
302878e1d200SJames Smart 
3029d613b6a7SJames Smart 	/* Sanity check */
3030d613b6a7SJames Smart 	if (ctxp->state != LPFC_NVMET_STE_ABORT) {
3031d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
303286c67379SJames Smart 				"6112 ABTS Wrong state:%d oxid x%x\n",
3033d613b6a7SJames Smart 				ctxp->state, ctxp->oxid);
3034d613b6a7SJames Smart 	}
303586c67379SJames Smart 
303686c67379SJames Smart 	/* Check if we already received a free context call
303786c67379SJames Smart 	 * and we have completed processing an abort situation.
303886c67379SJames Smart 	 */
3039d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_DONE;
304086c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_CTX_RLS) &&
304186c67379SJames Smart 	    !(ctxp->flag & LPFC_NVMET_XBUSY)) {
3042c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
304379d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3044c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
304519b58d94SJames Smart 		released = true;
304686c67379SJames Smart 	}
304719b58d94SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
304819b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3049547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
305019b58d94SJames Smart 
305186c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
305279d8c4ceSJames Smart 			"6316 ABTS cmpl oxid x%x flg x%x (%x) "
305386c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
305486c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
305586c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
305686c67379SJames Smart 			result, wcqe->word3);
30576c621a22SJames Smart 
30586c621a22SJames Smart 	cmdwqe->context2 = NULL;
30596c621a22SJames Smart 	cmdwqe->context3 = NULL;
306019b58d94SJames Smart 	/*
306119b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
306219b58d94SJames Smart 	 * will be recycled by transport release call.
306319b58d94SJames Smart 	 */
306419b58d94SJames Smart 	if (released)
30656c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
306686c67379SJames Smart 
306786c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
306886c67379SJames Smart 	 * For LPFC_NVMET_XBUSY, lpfc_sli4_nvmet_xri_aborted
306986c67379SJames Smart 	 * should have been called already.
307086c67379SJames Smart 	 */
3071d613b6a7SJames Smart }
3072d613b6a7SJames Smart 
3073d613b6a7SJames Smart /**
3074d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
3075d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3076d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3077d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
3078d613b6a7SJames Smart  *
3079d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3080d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for LS cmds
3081d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3082d613b6a7SJames Smart  **/
3083d613b6a7SJames Smart static void
3084d613b6a7SJames Smart lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3085d613b6a7SJames Smart 			    struct lpfc_wcqe_complete *wcqe)
3086d613b6a7SJames Smart {
3087d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
3088d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3089b27cbd55SBart Van Assche 	uint32_t result;
3090d613b6a7SJames Smart 
3091d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
3092d613b6a7SJames Smart 	result = wcqe->parameter;
3093d613b6a7SJames Smart 
3094d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3095547077a4SJames Smart 	atomic_inc(&tgtp->xmt_ls_abort_cmpl);
3096d613b6a7SJames Smart 
3097d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
309832350664SJames Smart 			"6083 Abort cmpl: ctx x%px WCQE:%08x %08x %08x %08x\n",
3099d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
3100d613b6a7SJames Smart 			result, wcqe->word3);
3101d613b6a7SJames Smart 
3102ce1b591cSJames Smart 	if (!ctxp) {
3103ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3104ce1b591cSJames Smart 				"6415 NVMET LS Abort No ctx: WCQE: "
3105ce1b591cSJames Smart 				 "%08x %08x %08x %08x\n",
3106ce1b591cSJames Smart 				wcqe->word0, wcqe->total_data_placed,
3107ce1b591cSJames Smart 				result, wcqe->word3);
3108ce1b591cSJames Smart 
3109ce1b591cSJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
3110ce1b591cSJames Smart 		return;
3111ce1b591cSJames Smart 	}
3112ce1b591cSJames Smart 
3113ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_LS_ABORT) {
3114ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3115ce1b591cSJames Smart 				"6416 NVMET LS abort cmpl state mismatch: "
3116ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
3117ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3118ce1b591cSJames Smart 	}
3119ce1b591cSJames Smart 
3120d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
3121d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
3122d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3123d613b6a7SJames Smart 	kfree(ctxp);
3124d613b6a7SJames Smart }
3125d613b6a7SJames Smart 
3126d613b6a7SJames Smart static int
3127d613b6a7SJames Smart lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
3128d613b6a7SJames Smart 			     struct lpfc_nvmet_rcv_ctx *ctxp,
3129d613b6a7SJames Smart 			     uint32_t sid, uint16_t xri)
3130d613b6a7SJames Smart {
3131d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3132d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3133205e8240SJames Smart 	union lpfc_wqe128 *wqe_abts;
3134d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3135d613b6a7SJames Smart 
3136d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
313786c67379SJames Smart 			"6067 ABTS: sid %x xri x%x/x%x\n",
3138318083adSJames Smart 			sid, xri, ctxp->wqeq->sli4_xritag);
3139d613b6a7SJames Smart 
3140d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3141d613b6a7SJames Smart 
3142d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3143d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3144d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3145d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3146d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3147ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3148d613b6a7SJames Smart 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
3149b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3150d613b6a7SJames Smart 
3151d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3152d613b6a7SJames Smart 		return 0;
3153d613b6a7SJames Smart 	}
3154d613b6a7SJames Smart 
3155d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
3156d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
3157d613b6a7SJames Smart 
3158d613b6a7SJames Smart 	/*
3159d613b6a7SJames Smart 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
3160d613b6a7SJames Smart 	 * that were initialized in lpfc_sli4_nvmet_alloc.
3161d613b6a7SJames Smart 	 */
3162d613b6a7SJames Smart 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
3163d613b6a7SJames Smart 
3164d613b6a7SJames Smart 	/* Word 5 */
3165d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
3166d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
3167d613b6a7SJames Smart 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
3168d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
3169d613b6a7SJames Smart 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
3170d613b6a7SJames Smart 
3171d613b6a7SJames Smart 	/* Word 6 */
3172d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
3173d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
3174d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
3175d613b6a7SJames Smart 	       abts_wqeq->sli4_xritag);
3176d613b6a7SJames Smart 
3177d613b6a7SJames Smart 	/* Word 7 */
3178d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
3179d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
3180d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
3181d613b6a7SJames Smart 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
3182d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
3183d613b6a7SJames Smart 
3184d613b6a7SJames Smart 	/* Word 8 */
3185d613b6a7SJames Smart 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
3186d613b6a7SJames Smart 
3187d613b6a7SJames Smart 	/* Word 9 */
3188d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
3189d613b6a7SJames Smart 	/* Needs to be set by caller */
3190d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
3191d613b6a7SJames Smart 
3192d613b6a7SJames Smart 	/* Word 10 */
3193d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe_abts->xmit_sequence.wqe_com, 1);
3194d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
3195d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
3196d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
3197d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
3198d613b6a7SJames Smart 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
3199d613b6a7SJames Smart 
3200d613b6a7SJames Smart 	/* Word 11 */
3201d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
3202d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
3203d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
3204d613b6a7SJames Smart 	       OTHER_COMMAND);
3205d613b6a7SJames Smart 
3206d613b6a7SJames Smart 	abts_wqeq->vport = phba->pport;
3207d613b6a7SJames Smart 	abts_wqeq->context1 = ndlp;
3208d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
3209d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
3210d613b6a7SJames Smart 	abts_wqeq->rsvd2 = 0;
3211d613b6a7SJames Smart 	/* hba_wqidx should already be setup from command we are aborting */
3212d613b6a7SJames Smart 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
3213d613b6a7SJames Smart 	abts_wqeq->iocb.ulpLe = 1;
3214d613b6a7SJames Smart 
3215d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
3216d613b6a7SJames Smart 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
3217d613b6a7SJames Smart 			xri, abts_wqeq->iotag);
3218d613b6a7SJames Smart 	return 1;
3219d613b6a7SJames Smart }
3220d613b6a7SJames Smart 
3221d613b6a7SJames Smart static int
3222d613b6a7SJames Smart lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
3223d613b6a7SJames Smart 			       struct lpfc_nvmet_rcv_ctx *ctxp,
3224d613b6a7SJames Smart 			       uint32_t sid, uint16_t xri)
3225d613b6a7SJames Smart {
3226d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3227d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3228d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3229d613b6a7SJames Smart 	unsigned long flags;
323051f8e43eSJames Smart 	u8 opt;
3231d613b6a7SJames Smart 	int rc;
3232d613b6a7SJames Smart 
3233d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3234d613b6a7SJames Smart 	if (!ctxp->wqeq) {
32356c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3236d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3237d613b6a7SJames Smart 	}
3238d613b6a7SJames Smart 
3239d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3240d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3241d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3242d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3243d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3244ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
324586c67379SJames Smart 				"6160 Drop ABORT - wrong NDLP state x%x.\n",
3246b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3247d613b6a7SJames Smart 
3248d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3249c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
325086c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3251c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3252d613b6a7SJames Smart 		return 0;
3253d613b6a7SJames Smart 	}
3254d613b6a7SJames Smart 
3255d613b6a7SJames Smart 	/* Issue ABTS for this WQE based on iotag */
3256d613b6a7SJames Smart 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
3257c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3258d613b6a7SJames Smart 	if (!ctxp->abort_wqeq) {
3259547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3260ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
326186c67379SJames Smart 				"6161 ABORT failed: No wqeqs: "
3262d613b6a7SJames Smart 				"xri: x%x\n", ctxp->oxid);
3263d613b6a7SJames Smart 		/* No failure to an ABTS request. */
326486c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3265c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3266d613b6a7SJames Smart 		return 0;
3267d613b6a7SJames Smart 	}
3268d613b6a7SJames Smart 	abts_wqeq = ctxp->abort_wqeq;
3269d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
327051f8e43eSJames Smart 	opt = (ctxp->flag & LPFC_NVMET_ABTS_RCV) ? INHIBIT_ABORT : 0;
3271c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3272d613b6a7SJames Smart 
3273d613b6a7SJames Smart 	/* Announce entry to new IO submit field. */
327486c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
327586c67379SJames Smart 			"6162 ABORT Request to rport DID x%06x "
3276d613b6a7SJames Smart 			"for xri x%x x%x\n",
3277d613b6a7SJames Smart 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
3278d613b6a7SJames Smart 
3279d613b6a7SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
3280d613b6a7SJames Smart 	 * cleared when the reset is complete and rings reestablished.
3281d613b6a7SJames Smart 	 */
3282d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3283d613b6a7SJames Smart 	/* driver queued commands are in process of being flushed */
3284c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
3285d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3286547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3287d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
3288d613b6a7SJames Smart 				"6163 Driver in reset cleanup - flushing "
3289d613b6a7SJames Smart 				"NVME Req now. hba_flag x%x oxid x%x\n",
3290d613b6a7SJames Smart 				phba->hba_flag, ctxp->oxid);
3291d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3292c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
329386c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3294c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3295d613b6a7SJames Smart 		return 0;
3296d613b6a7SJames Smart 	}
3297d613b6a7SJames Smart 
3298d613b6a7SJames Smart 	/* Outstanding abort is in progress */
3299d613b6a7SJames Smart 	if (abts_wqeq->iocb_flag & LPFC_DRIVER_ABORTED) {
3300d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3301547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3302d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
3303d613b6a7SJames Smart 				"6164 Outstanding NVME I/O Abort Request "
3304d613b6a7SJames Smart 				"still pending on oxid x%x\n",
3305d613b6a7SJames Smart 				ctxp->oxid);
3306d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3307c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
330886c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3309c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3310d613b6a7SJames Smart 		return 0;
3311d613b6a7SJames Smart 	}
3312d613b6a7SJames Smart 
3313d613b6a7SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
3314d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_DRIVER_ABORTED;
3315d613b6a7SJames Smart 
331651f8e43eSJames Smart 	lpfc_nvme_prep_abort_wqe(abts_wqeq, ctxp->wqeq->sli4_xritag, opt);
3317d613b6a7SJames Smart 
3318d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
3319d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
3320d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
33215314995eSSaurav Girepunje 	abts_wqeq->iocb_cmpl = NULL;
3322d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVME;
3323d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
33244550f9c7SJames Smart 	abts_wqeq->vport = phba->pport;
33251fbf9742SJames Smart 	if (!ctxp->hdwq)
33261fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
33271fbf9742SJames Smart 
33281fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3329d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3330547077a4SJames Smart 	if (rc == WQE_SUCCESS) {
3331547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_sol);
3332d613b6a7SJames Smart 		return 0;
3333547077a4SJames Smart 	}
3334d613b6a7SJames Smart 
3335547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3336c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
333786c67379SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3338c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3339d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
334086c67379SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
334186c67379SJames Smart 			"6166 Failed ABORT issue_wqe with status x%x "
3342d613b6a7SJames Smart 			"for oxid x%x.\n",
3343d613b6a7SJames Smart 			rc, ctxp->oxid);
3344d613b6a7SJames Smart 	return 1;
3345d613b6a7SJames Smart }
3346d613b6a7SJames Smart 
3347d613b6a7SJames Smart static int
3348d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
3349d613b6a7SJames Smart 				 struct lpfc_nvmet_rcv_ctx *ctxp,
3350d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
3351d613b6a7SJames Smart {
3352d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3353d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3354d613b6a7SJames Smart 	unsigned long flags;
3355c160c0f8SJames Smart 	bool released = false;
3356d613b6a7SJames Smart 	int rc;
3357d613b6a7SJames Smart 
3358d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3359d613b6a7SJames Smart 	if (!ctxp->wqeq) {
33606c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3361d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3362d613b6a7SJames Smart 	}
3363d613b6a7SJames Smart 
3364ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_FREE) {
3365ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3366ce1b591cSJames Smart 				"6417 NVMET ABORT ctx freed %d %d oxid x%x\n",
3367ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
3368ce1b591cSJames Smart 		rc = WQE_BUSY;
3369ce1b591cSJames Smart 		goto aerr;
3370ce1b591cSJames Smart 	}
3371ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
3372ce1b591cSJames Smart 	ctxp->entry_cnt++;
3373d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
3374d613b6a7SJames Smart 	if (rc == 0)
3375d613b6a7SJames Smart 		goto aerr;
3376d613b6a7SJames Smart 
3377d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3378d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
337986c67379SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_unsol_fcp_abort_cmp;
338086c67379SJames Smart 	abts_wqeq->iocb_cmpl = NULL;
3381d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVMET;
33821fbf9742SJames Smart 	if (!ctxp->hdwq)
33831fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
33841fbf9742SJames Smart 
33851fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3386d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3387d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3388d613b6a7SJames Smart 		return 0;
3389d613b6a7SJames Smart 	}
3390d613b6a7SJames Smart 
3391d613b6a7SJames Smart aerr:
339268c9b55dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3393c160c0f8SJames Smart 	if (ctxp->flag & LPFC_NVMET_CTX_RLS) {
3394c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
339579d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3396c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
3397c160c0f8SJames Smart 		released = true;
3398c160c0f8SJames Smart 	}
339968c9b55dSJames Smart 	ctxp->flag &= ~(LPFC_NVMET_ABORT_OP | LPFC_NVMET_CTX_RLS);
340068c9b55dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
340168c9b55dSJames Smart 
3402d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3403ce1b591cSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
340479d8c4ceSJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x "
340579d8c4ceSJames Smart 			"(%x)\n",
340679d8c4ceSJames Smart 			ctxp->oxid, rc, released);
3407c160c0f8SJames Smart 	if (released)
340868c9b55dSJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3409d613b6a7SJames Smart 	return 1;
3410d613b6a7SJames Smart }
3411d613b6a7SJames Smart 
3412d613b6a7SJames Smart static int
3413d613b6a7SJames Smart lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *phba,
3414d613b6a7SJames Smart 				struct lpfc_nvmet_rcv_ctx *ctxp,
3415d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
3416d613b6a7SJames Smart {
3417d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3418d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3419d613b6a7SJames Smart 	unsigned long flags;
3420d613b6a7SJames Smart 	int rc;
3421d613b6a7SJames Smart 
3422ce1b591cSJames Smart 	if ((ctxp->state == LPFC_NVMET_STE_LS_RCV && ctxp->entry_cnt == 1) ||
3423ce1b591cSJames Smart 	    (ctxp->state == LPFC_NVMET_STE_LS_RSP && ctxp->entry_cnt == 2)) {
3424ce1b591cSJames Smart 		ctxp->state = LPFC_NVMET_STE_LS_ABORT;
3425ce1b591cSJames Smart 		ctxp->entry_cnt++;
3426ce1b591cSJames Smart 	} else {
3427ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3428ce1b591cSJames Smart 				"6418 NVMET LS abort state mismatch "
3429ce1b591cSJames Smart 				"IO x%x: %d %d\n",
3430ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3431ce1b591cSJames Smart 		ctxp->state = LPFC_NVMET_STE_LS_ABORT;
3432ce1b591cSJames Smart 	}
3433ce1b591cSJames Smart 
3434d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3435d613b6a7SJames Smart 	if (!ctxp->wqeq) {
3436d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
3437d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
3438d613b6a7SJames Smart 		if (!ctxp->wqeq) {
3439ce1b591cSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3440d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
3441d613b6a7SJames Smart 					"xri: x%x\n", xri);
3442d613b6a7SJames Smart 			/* No failure to an ABTS request. */
3443d613b6a7SJames Smart 			kfree(ctxp);
3444d613b6a7SJames Smart 			return 0;
3445d613b6a7SJames Smart 		}
3446d613b6a7SJames Smart 	}
3447d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
34486c621a22SJames Smart 
3449ce1b591cSJames Smart 	if (lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri) == 0) {
3450ce1b591cSJames Smart 		rc = WQE_BUSY;
3451ce1b591cSJames Smart 		goto out;
3452ce1b591cSJames Smart 	}
3453d613b6a7SJames Smart 
3454d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3455d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
34565314995eSSaurav Girepunje 	abts_wqeq->iocb_cmpl = NULL;
3457d613b6a7SJames Smart 	abts_wqeq->iocb_flag |=  LPFC_IO_NVME_LS;
34581fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3459d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3460d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3461547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_unsol);
3462d613b6a7SJames Smart 		return 0;
3463d613b6a7SJames Smart 	}
3464ce1b591cSJames Smart out:
3465d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3466d613b6a7SJames Smart 	abts_wqeq->context2 = NULL;
3467d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
3468d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3469d613b6a7SJames Smart 	kfree(ctxp);
3470ce1b591cSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3471d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
3472d613b6a7SJames Smart 	return 0;
3473d613b6a7SJames Smart }
3474