xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision d7b761b0)
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 
223d7b761b0SYueHaibing static struct lpfc_nvmet_rcv_ctx *
22479d8c4ceSJames Smart lpfc_nvmet_get_ctx_for_xri(struct lpfc_hba *phba, u16 xri)
22579d8c4ceSJames Smart {
22679d8c4ceSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
22779d8c4ceSJames Smart 	unsigned long iflag;
22879d8c4ceSJames Smart 	bool found = false;
22979d8c4ceSJames Smart 
23079d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
23179d8c4ceSJames Smart 	list_for_each_entry(ctxp, &phba->sli4_hba.t_active_ctx_list, list) {
23279d8c4ceSJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
23379d8c4ceSJames Smart 			continue;
23479d8c4ceSJames Smart 
23579d8c4ceSJames Smart 		found = true;
23679d8c4ceSJames Smart 		break;
23779d8c4ceSJames Smart 	}
23879d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
23979d8c4ceSJames Smart 	if (found)
24079d8c4ceSJames Smart 		return ctxp;
24179d8c4ceSJames Smart 
24279d8c4ceSJames Smart 	return NULL;
24379d8c4ceSJames Smart }
24479d8c4ceSJames Smart 
245d7b761b0SYueHaibing static struct lpfc_nvmet_rcv_ctx *
24679d8c4ceSJames Smart lpfc_nvmet_get_ctx_for_oxid(struct lpfc_hba *phba, u16 oxid, u32 sid)
24779d8c4ceSJames Smart {
24879d8c4ceSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
24979d8c4ceSJames Smart 	unsigned long iflag;
25079d8c4ceSJames Smart 	bool found = false;
25179d8c4ceSJames Smart 
25279d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
25379d8c4ceSJames Smart 	list_for_each_entry(ctxp, &phba->sli4_hba.t_active_ctx_list, list) {
25479d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
25579d8c4ceSJames Smart 			continue;
25679d8c4ceSJames Smart 
25779d8c4ceSJames Smart 		found = true;
25879d8c4ceSJames Smart 		break;
25979d8c4ceSJames Smart 	}
26079d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
26179d8c4ceSJames Smart 	if (found)
26279d8c4ceSJames Smart 		return ctxp;
26379d8c4ceSJames Smart 
26479d8c4ceSJames Smart 	return NULL;
26579d8c4ceSJames Smart }
26679d8c4ceSJames Smart 
2673999df75SBart Van Assche static void
26886c67379SJames Smart lpfc_nvmet_defer_release(struct lpfc_hba *phba, struct lpfc_nvmet_rcv_ctx *ctxp)
26986c67379SJames Smart {
270472e146dSJames Smart 	lockdep_assert_held(&ctxp->ctxlock);
27186c67379SJames Smart 
272e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
27379d8c4ceSJames Smart 			"6313 NVMET Defer ctx release oxid x%x flg x%x\n",
27486c67379SJames Smart 			ctxp->oxid, ctxp->flag);
27586c67379SJames Smart 
276472e146dSJames Smart 	if (ctxp->flag & LPFC_NVMET_CTX_RLS)
27786c67379SJames Smart 		return;
278472e146dSJames Smart 
27986c67379SJames Smart 	ctxp->flag |= LPFC_NVMET_CTX_RLS;
28079d8c4ceSJames Smart 	spin_lock(&phba->sli4_hba.t_active_list_lock);
28179d8c4ceSJames Smart 	list_del(&ctxp->list);
28279d8c4ceSJames Smart 	spin_unlock(&phba->sli4_hba.t_active_list_lock);
283472e146dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
28486c67379SJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
285472e146dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
28686c67379SJames Smart }
28786c67379SJames Smart 
288d613b6a7SJames Smart /**
289d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
290d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
291d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
292d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
293d613b6a7SJames Smart  *
294d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
295d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME LS commands
296d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
297d613b6a7SJames Smart  **/
298d613b6a7SJames Smart static void
299d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
300d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
301d613b6a7SJames Smart {
302d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
303d613b6a7SJames Smart 	struct nvmefc_tgt_ls_req *rsp;
304d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
305d613b6a7SJames Smart 	uint32_t status, result;
306d613b6a7SJames Smart 
307d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
308d613b6a7SJames Smart 	result = wcqe->parameter;
309ce1b591cSJames Smart 	ctxp = cmdwqe->context2;
310ce1b591cSJames Smart 
311ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_LS_RSP || ctxp->entry_cnt != 2) {
312ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
313ce1b591cSJames Smart 				"6410 NVMET LS cmpl state mismatch IO x%x: "
314ce1b591cSJames Smart 				"%d %d\n",
315ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
316ce1b591cSJames Smart 	}
317ce1b591cSJames Smart 
318d613b6a7SJames Smart 	if (!phba->targetport)
319d613b6a7SJames Smart 		goto out;
320d613b6a7SJames Smart 
321d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
322d613b6a7SJames Smart 
3234b056682SJames Smart 	if (tgtp) {
3244b056682SJames Smart 		if (status) {
325d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_error);
3268ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
3274b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_aborted);
3284b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
3294b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_xb_set);
3304b056682SJames Smart 		} else {
331d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
3324b056682SJames Smart 		}
3334b056682SJames Smart 	}
334d613b6a7SJames Smart 
335d613b6a7SJames Smart out:
336d613b6a7SJames Smart 	rsp = &ctxp->ctx.ls_req;
337d613b6a7SJames Smart 
3382b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  CMPL: xri x%x stat x%x result x%x\n",
3392b65e182SJames Smart 			 ctxp->oxid, status, result);
3402b65e182SJames Smart 
341d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
342ce1b591cSJames Smart 			"6038 NVMET LS rsp cmpl: %d %d oxid x%x\n",
343ce1b591cSJames Smart 			status, result, ctxp->oxid);
344d613b6a7SJames Smart 
345d613b6a7SJames Smart 	lpfc_nlp_put(cmdwqe->context1);
346d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
347d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
348d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
349d613b6a7SJames Smart 	rsp->done(rsp);
350d613b6a7SJames Smart 	kfree(ctxp);
351d613b6a7SJames Smart }
352d613b6a7SJames Smart 
353d613b6a7SJames Smart /**
3546c621a22SJames Smart  * lpfc_nvmet_ctxbuf_post - Repost a NVMET RQ DMA buffer and clean up context
355d613b6a7SJames Smart  * @phba: HBA buffer is associated with
356d613b6a7SJames Smart  * @ctxp: context to clean up
357d613b6a7SJames Smart  * @mp: Buffer to free
358d613b6a7SJames Smart  *
359d613b6a7SJames Smart  * Description: Frees the given DMA buffer in the appropriate way given by
360d613b6a7SJames Smart  * reposting it to its associated RQ so it can be reused.
361d613b6a7SJames Smart  *
362d613b6a7SJames Smart  * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
363d613b6a7SJames Smart  *
364d613b6a7SJames Smart  * Returns: None
365d613b6a7SJames Smart  **/
366d613b6a7SJames Smart void
3676c621a22SJames Smart lpfc_nvmet_ctxbuf_post(struct lpfc_hba *phba, struct lpfc_nvmet_ctxbuf *ctx_buf)
368d613b6a7SJames Smart {
369eeeb51d8SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
3706c621a22SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
371a8cf5dfeSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
372a8cf5dfeSJames Smart 	struct fc_frame_header *fc_hdr;
373a8cf5dfeSJames Smart 	struct rqb_dmabuf *nvmebuf;
37466d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
375472e146dSJames Smart 	uint32_t size, oxid, sid;
37666d7ce93SDick Kennedy 	int cpu;
3776c621a22SJames Smart 	unsigned long iflag;
37886c67379SJames Smart 
379d613b6a7SJames Smart 	if (ctxp->txrdy) {
380771db5c0SRomain Perier 		dma_pool_free(phba->txrdy_payload_pool, ctxp->txrdy,
381d613b6a7SJames Smart 			      ctxp->txrdy_phys);
382d613b6a7SJames Smart 		ctxp->txrdy = NULL;
383d613b6a7SJames Smart 		ctxp->txrdy_phys = 0;
384d613b6a7SJames Smart 	}
385ce1b591cSJames Smart 
386ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_FREE) {
387ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
388ce1b591cSJames Smart 				"6411 NVMET free, already free IO x%x: %d %d\n",
389ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
390ce1b591cSJames Smart 	}
391472e146dSJames Smart 
392472e146dSJames Smart 	if (ctxp->rqb_buffer) {
393472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
3944767c58aSJames Smart 		nvmebuf = ctxp->rqb_buffer;
3954767c58aSJames Smart 		/* check if freed in another path whilst acquiring lock */
3964767c58aSJames Smart 		if (nvmebuf) {
397472e146dSJames Smart 			ctxp->rqb_buffer = NULL;
398472e146dSJames Smart 			if (ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) {
399472e146dSJames Smart 				ctxp->flag &= ~LPFC_NVMET_CTX_REUSE_WQ;
400472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4014767c58aSJames Smart 				nvmebuf->hrq->rqbp->rqb_free_buffer(phba,
4024767c58aSJames Smart 								    nvmebuf);
403472e146dSJames Smart 			} else {
404472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4054767c58aSJames Smart 				/* repost */
4064767c58aSJames Smart 				lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
4074767c58aSJames Smart 			}
4084767c58aSJames Smart 		} else {
4094767c58aSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
410472e146dSJames Smart 		}
411472e146dSJames Smart 	}
412d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_FREE;
4136c621a22SJames Smart 
414a8cf5dfeSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
415a8cf5dfeSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
416a8cf5dfeSJames Smart 		list_remove_head(&phba->sli4_hba.lpfc_nvmet_io_wait_list,
417a8cf5dfeSJames Smart 				 nvmebuf, struct rqb_dmabuf,
418a8cf5dfeSJames Smart 				 hbuf.list);
419a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt--;
420a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
421a8cf5dfeSJames Smart 				       iflag);
422a8cf5dfeSJames Smart 
423a8cf5dfeSJames Smart 		fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
424a8cf5dfeSJames Smart 		oxid = be16_to_cpu(fc_hdr->fh_ox_id);
425a8cf5dfeSJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
426a8cf5dfeSJames Smart 		size = nvmebuf->bytes_recv;
427a8cf5dfeSJames Smart 		sid = sli4_sid_from_fc_hdr(fc_hdr);
428a8cf5dfeSJames Smart 
429a8cf5dfeSJames Smart 		ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
430a8cf5dfeSJames Smart 		ctxp->wqeq = NULL;
431a8cf5dfeSJames Smart 		ctxp->txrdy = NULL;
432a8cf5dfeSJames Smart 		ctxp->offset = 0;
433a8cf5dfeSJames Smart 		ctxp->phba = phba;
434a8cf5dfeSJames Smart 		ctxp->size = size;
435a8cf5dfeSJames Smart 		ctxp->oxid = oxid;
436a8cf5dfeSJames Smart 		ctxp->sid = sid;
437a8cf5dfeSJames Smart 		ctxp->state = LPFC_NVMET_STE_RCV;
438a8cf5dfeSJames Smart 		ctxp->entry_cnt = 1;
439a8cf5dfeSJames Smart 		ctxp->flag = 0;
440a8cf5dfeSJames Smart 		ctxp->ctxbuf = ctx_buf;
441cbc5de1bSJames Smart 		ctxp->rqb_buffer = (void *)nvmebuf;
442a8cf5dfeSJames Smart 		spin_lock_init(&ctxp->ctxlock);
443a8cf5dfeSJames Smart 
444a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
445d74a89aaSJames Smart 		/* NOTE: isr time stamp is stale when context is re-assigned*/
446d74a89aaSJames Smart 		if (ctxp->ts_isr_cmd) {
447d74a89aaSJames Smart 			ctxp->ts_cmd_nvme = 0;
448a8cf5dfeSJames Smart 			ctxp->ts_nvme_data = 0;
449a8cf5dfeSJames Smart 			ctxp->ts_data_wqput = 0;
450a8cf5dfeSJames Smart 			ctxp->ts_isr_data = 0;
451a8cf5dfeSJames Smart 			ctxp->ts_data_nvme = 0;
452a8cf5dfeSJames Smart 			ctxp->ts_nvme_status = 0;
453a8cf5dfeSJames Smart 			ctxp->ts_status_wqput = 0;
454a8cf5dfeSJames Smart 			ctxp->ts_isr_status = 0;
455a8cf5dfeSJames Smart 			ctxp->ts_status_nvme = 0;
456d613b6a7SJames Smart 		}
457a8cf5dfeSJames Smart #endif
458a8cf5dfeSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_in);
459472e146dSJames Smart 
46079d8c4ceSJames Smart 		/* Indicate that a replacement buffer has been posted */
461472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
462472e146dSJames Smart 		ctxp->flag |= LPFC_NVMET_CTX_REUSE_WQ;
463472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
464a8cf5dfeSJames Smart 
465472e146dSJames Smart 		if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
466a8cf5dfeSJames Smart 			atomic_inc(&tgtp->rcv_fcp_cmd_drop);
467472e146dSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
468472e146dSJames Smart 					"6181 Unable to queue deferred work "
469472e146dSJames Smart 					"for oxid x%x. "
470472e146dSJames Smart 					"FCP Drop IO [x%x x%x x%x]\n",
471472e146dSJames Smart 					ctxp->oxid,
472a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_in),
473a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_out),
474a8cf5dfeSJames Smart 					atomic_read(&tgtp->xmt_fcp_release));
475a8cf5dfeSJames Smart 
476472e146dSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
477a8cf5dfeSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
478472e146dSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
479a8cf5dfeSJames Smart 			lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
480472e146dSJames Smart 		}
481a8cf5dfeSJames Smart 		return;
482a8cf5dfeSJames Smart 	}
483a8cf5dfeSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
484a8cf5dfeSJames Smart 
48566d7ce93SDick Kennedy 	/*
48666d7ce93SDick Kennedy 	 * Use the CPU context list, from the MRQ the IO was received on
48766d7ce93SDick Kennedy 	 * (ctxp->idx), to save context structure.
48866d7ce93SDick Kennedy 	 */
48979d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
49079d8c4ceSJames Smart 	list_del_init(&ctxp->list);
49179d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
492d6d189ceSBart Van Assche 	cpu = raw_smp_processor_id();
49366d7ce93SDick Kennedy 	infop = lpfc_get_ctx_list(phba, cpu, ctxp->idx);
49466d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, iflag);
49566d7ce93SDick Kennedy 	list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
49666d7ce93SDick Kennedy 	infop->nvmet_ctx_list_cnt++;
49766d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, iflag);
498eeeb51d8SJames Smart #endif
499d613b6a7SJames Smart }
500d613b6a7SJames Smart 
5012b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5022b65e182SJames Smart static void
5032b65e182SJames Smart lpfc_nvmet_ktime(struct lpfc_hba *phba,
5042b65e182SJames Smart 		 struct lpfc_nvmet_rcv_ctx *ctxp)
5052b65e182SJames Smart {
5062b65e182SJames Smart 	uint64_t seg1, seg2, seg3, seg4, seg5;
5072b65e182SJames Smart 	uint64_t seg6, seg7, seg8, seg9, seg10;
508c8a4ce0bSDick Kennedy 	uint64_t segsum;
5092b65e182SJames Smart 
5102b65e182SJames Smart 	if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
5112b65e182SJames Smart 	    !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
5122b65e182SJames Smart 	    !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
5132b65e182SJames Smart 	    !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
5142b65e182SJames Smart 	    !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
5152b65e182SJames Smart 		return;
5162b65e182SJames Smart 
517c8a4ce0bSDick Kennedy 	if (ctxp->ts_status_nvme < ctxp->ts_isr_cmd)
518c8a4ce0bSDick Kennedy 		return;
5192b65e182SJames Smart 	if (ctxp->ts_isr_cmd  > ctxp->ts_cmd_nvme)
5202b65e182SJames Smart 		return;
5212b65e182SJames Smart 	if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
5222b65e182SJames Smart 		return;
5232b65e182SJames Smart 	if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
5242b65e182SJames Smart 		return;
5252b65e182SJames Smart 	if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
5262b65e182SJames Smart 		return;
5272b65e182SJames Smart 	if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
5282b65e182SJames Smart 		return;
5292b65e182SJames Smart 	if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
5302b65e182SJames Smart 		return;
5312b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
5322b65e182SJames Smart 		return;
5332b65e182SJames Smart 	if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
5342b65e182SJames Smart 		return;
5352b65e182SJames Smart 	if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
5362b65e182SJames Smart 		return;
5372b65e182SJames Smart 	/*
5382b65e182SJames Smart 	 * Segment 1 - Time from FCP command received by MSI-X ISR
5392b65e182SJames Smart 	 * to FCP command is passed to NVME Layer.
5402b65e182SJames Smart 	 * Segment 2 - Time from FCP command payload handed
5412b65e182SJames Smart 	 * off to NVME Layer to Driver receives a Command op
5422b65e182SJames Smart 	 * from NVME Layer.
5432b65e182SJames Smart 	 * Segment 3 - Time from Driver receives a Command op
5442b65e182SJames Smart 	 * from NVME Layer to Command is put on WQ.
5452b65e182SJames Smart 	 * Segment 4 - Time from Driver WQ put is done
5462b65e182SJames Smart 	 * to MSI-X ISR for Command cmpl.
5472b65e182SJames Smart 	 * Segment 5 - Time from MSI-X ISR for Command cmpl to
5482b65e182SJames Smart 	 * Command cmpl is passed to NVME Layer.
5492b65e182SJames Smart 	 * Segment 6 - Time from Command cmpl is passed to NVME
5502b65e182SJames Smart 	 * Layer to Driver receives a RSP op from NVME Layer.
5512b65e182SJames Smart 	 * Segment 7 - Time from Driver receives a RSP op from
5522b65e182SJames Smart 	 * NVME Layer to WQ put is done on TRSP FCP Status.
5532b65e182SJames Smart 	 * Segment 8 - Time from Driver WQ put is done on TRSP
5542b65e182SJames Smart 	 * FCP Status to MSI-X ISR for TRSP cmpl.
5552b65e182SJames Smart 	 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
5562b65e182SJames Smart 	 * TRSP cmpl is passed to NVME Layer.
5572b65e182SJames Smart 	 * Segment 10 - Time from FCP command received by
5582b65e182SJames Smart 	 * MSI-X ISR to command is completed on wire.
5592b65e182SJames Smart 	 * (Segments 1 thru 8) for READDATA / WRITEDATA
5602b65e182SJames Smart 	 * (Segments 1 thru 4) for READDATA_RSP
5612b65e182SJames Smart 	 */
5622b65e182SJames Smart 	seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
563c8a4ce0bSDick Kennedy 	segsum = seg1;
564c8a4ce0bSDick Kennedy 
565c8a4ce0bSDick Kennedy 	seg2 = ctxp->ts_nvme_data - ctxp->ts_isr_cmd;
566c8a4ce0bSDick Kennedy 	if (segsum > seg2)
567c8a4ce0bSDick Kennedy 		return;
568c8a4ce0bSDick Kennedy 	seg2 -= segsum;
569c8a4ce0bSDick Kennedy 	segsum += seg2;
570c8a4ce0bSDick Kennedy 
571c8a4ce0bSDick Kennedy 	seg3 = ctxp->ts_data_wqput - ctxp->ts_isr_cmd;
572c8a4ce0bSDick Kennedy 	if (segsum > seg3)
573c8a4ce0bSDick Kennedy 		return;
574c8a4ce0bSDick Kennedy 	seg3 -= segsum;
575c8a4ce0bSDick Kennedy 	segsum += seg3;
576c8a4ce0bSDick Kennedy 
577c8a4ce0bSDick Kennedy 	seg4 = ctxp->ts_isr_data - ctxp->ts_isr_cmd;
578c8a4ce0bSDick Kennedy 	if (segsum > seg4)
579c8a4ce0bSDick Kennedy 		return;
580c8a4ce0bSDick Kennedy 	seg4 -= segsum;
581c8a4ce0bSDick Kennedy 	segsum += seg4;
582c8a4ce0bSDick Kennedy 
583c8a4ce0bSDick Kennedy 	seg5 = ctxp->ts_data_nvme - ctxp->ts_isr_cmd;
584c8a4ce0bSDick Kennedy 	if (segsum > seg5)
585c8a4ce0bSDick Kennedy 		return;
586c8a4ce0bSDick Kennedy 	seg5 -= segsum;
587c8a4ce0bSDick Kennedy 	segsum += seg5;
588c8a4ce0bSDick Kennedy 
5892b65e182SJames Smart 
5902b65e182SJames Smart 	/* For auto rsp commands seg6 thru seg10 will be 0 */
5912b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
592c8a4ce0bSDick Kennedy 		seg6 = ctxp->ts_nvme_status - ctxp->ts_isr_cmd;
593c8a4ce0bSDick Kennedy 		if (segsum > seg6)
594c8a4ce0bSDick Kennedy 			return;
595c8a4ce0bSDick Kennedy 		seg6 -= segsum;
596c8a4ce0bSDick Kennedy 		segsum += seg6;
597c8a4ce0bSDick Kennedy 
598c8a4ce0bSDick Kennedy 		seg7 = ctxp->ts_status_wqput - ctxp->ts_isr_cmd;
599c8a4ce0bSDick Kennedy 		if (segsum > seg7)
600c8a4ce0bSDick Kennedy 			return;
601c8a4ce0bSDick Kennedy 		seg7 -= segsum;
602c8a4ce0bSDick Kennedy 		segsum += seg7;
603c8a4ce0bSDick Kennedy 
604c8a4ce0bSDick Kennedy 		seg8 = ctxp->ts_isr_status - ctxp->ts_isr_cmd;
605c8a4ce0bSDick Kennedy 		if (segsum > seg8)
606c8a4ce0bSDick Kennedy 			return;
607c8a4ce0bSDick Kennedy 		seg8 -= segsum;
608c8a4ce0bSDick Kennedy 		segsum += seg8;
609c8a4ce0bSDick Kennedy 
610c8a4ce0bSDick Kennedy 		seg9 = ctxp->ts_status_nvme - ctxp->ts_isr_cmd;
611c8a4ce0bSDick Kennedy 		if (segsum > seg9)
612c8a4ce0bSDick Kennedy 			return;
613c8a4ce0bSDick Kennedy 		seg9 -= segsum;
614c8a4ce0bSDick Kennedy 		segsum += seg9;
615c8a4ce0bSDick Kennedy 
616c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_status < ctxp->ts_isr_cmd)
617c8a4ce0bSDick Kennedy 			return;
6182b65e182SJames Smart 		seg10 = (ctxp->ts_isr_status -
6192b65e182SJames Smart 			ctxp->ts_isr_cmd);
6202b65e182SJames Smart 	} else {
621c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_data < ctxp->ts_isr_cmd)
622c8a4ce0bSDick Kennedy 			return;
6232b65e182SJames Smart 		seg6 =  0;
6242b65e182SJames Smart 		seg7 =  0;
6252b65e182SJames Smart 		seg8 =  0;
6262b65e182SJames Smart 		seg9 =  0;
6272b65e182SJames Smart 		seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
6282b65e182SJames Smart 	}
6292b65e182SJames Smart 
6302b65e182SJames Smart 	phba->ktime_seg1_total += seg1;
6312b65e182SJames Smart 	if (seg1 < phba->ktime_seg1_min)
6322b65e182SJames Smart 		phba->ktime_seg1_min = seg1;
6332b65e182SJames Smart 	else if (seg1 > phba->ktime_seg1_max)
6342b65e182SJames Smart 		phba->ktime_seg1_max = seg1;
6352b65e182SJames Smart 
6362b65e182SJames Smart 	phba->ktime_seg2_total += seg2;
6372b65e182SJames Smart 	if (seg2 < phba->ktime_seg2_min)
6382b65e182SJames Smart 		phba->ktime_seg2_min = seg2;
6392b65e182SJames Smart 	else if (seg2 > phba->ktime_seg2_max)
6402b65e182SJames Smart 		phba->ktime_seg2_max = seg2;
6412b65e182SJames Smart 
6422b65e182SJames Smart 	phba->ktime_seg3_total += seg3;
6432b65e182SJames Smart 	if (seg3 < phba->ktime_seg3_min)
6442b65e182SJames Smart 		phba->ktime_seg3_min = seg3;
6452b65e182SJames Smart 	else if (seg3 > phba->ktime_seg3_max)
6462b65e182SJames Smart 		phba->ktime_seg3_max = seg3;
6472b65e182SJames Smart 
6482b65e182SJames Smart 	phba->ktime_seg4_total += seg4;
6492b65e182SJames Smart 	if (seg4 < phba->ktime_seg4_min)
6502b65e182SJames Smart 		phba->ktime_seg4_min = seg4;
6512b65e182SJames Smart 	else if (seg4 > phba->ktime_seg4_max)
6522b65e182SJames Smart 		phba->ktime_seg4_max = seg4;
6532b65e182SJames Smart 
6542b65e182SJames Smart 	phba->ktime_seg5_total += seg5;
6552b65e182SJames Smart 	if (seg5 < phba->ktime_seg5_min)
6562b65e182SJames Smart 		phba->ktime_seg5_min = seg5;
6572b65e182SJames Smart 	else if (seg5 > phba->ktime_seg5_max)
6582b65e182SJames Smart 		phba->ktime_seg5_max = seg5;
6592b65e182SJames Smart 
6602b65e182SJames Smart 	phba->ktime_data_samples++;
6612b65e182SJames Smart 	if (!seg6)
6622b65e182SJames Smart 		goto out;
6632b65e182SJames Smart 
6642b65e182SJames Smart 	phba->ktime_seg6_total += seg6;
6652b65e182SJames Smart 	if (seg6 < phba->ktime_seg6_min)
6662b65e182SJames Smart 		phba->ktime_seg6_min = seg6;
6672b65e182SJames Smart 	else if (seg6 > phba->ktime_seg6_max)
6682b65e182SJames Smart 		phba->ktime_seg6_max = seg6;
6692b65e182SJames Smart 
6702b65e182SJames Smart 	phba->ktime_seg7_total += seg7;
6712b65e182SJames Smart 	if (seg7 < phba->ktime_seg7_min)
6722b65e182SJames Smart 		phba->ktime_seg7_min = seg7;
6732b65e182SJames Smart 	else if (seg7 > phba->ktime_seg7_max)
6742b65e182SJames Smart 		phba->ktime_seg7_max = seg7;
6752b65e182SJames Smart 
6762b65e182SJames Smart 	phba->ktime_seg8_total += seg8;
6772b65e182SJames Smart 	if (seg8 < phba->ktime_seg8_min)
6782b65e182SJames Smart 		phba->ktime_seg8_min = seg8;
6792b65e182SJames Smart 	else if (seg8 > phba->ktime_seg8_max)
6802b65e182SJames Smart 		phba->ktime_seg8_max = seg8;
6812b65e182SJames Smart 
6822b65e182SJames Smart 	phba->ktime_seg9_total += seg9;
6832b65e182SJames Smart 	if (seg9 < phba->ktime_seg9_min)
6842b65e182SJames Smart 		phba->ktime_seg9_min = seg9;
6852b65e182SJames Smart 	else if (seg9 > phba->ktime_seg9_max)
6862b65e182SJames Smart 		phba->ktime_seg9_max = seg9;
6872b65e182SJames Smart out:
6882b65e182SJames Smart 	phba->ktime_seg10_total += seg10;
6892b65e182SJames Smart 	if (seg10 < phba->ktime_seg10_min)
6902b65e182SJames Smart 		phba->ktime_seg10_min = seg10;
6912b65e182SJames Smart 	else if (seg10 > phba->ktime_seg10_max)
6922b65e182SJames Smart 		phba->ktime_seg10_max = seg10;
6932b65e182SJames Smart 	phba->ktime_status_samples++;
6942b65e182SJames Smart }
6952b65e182SJames Smart #endif
6962b65e182SJames Smart 
697d613b6a7SJames Smart /**
698d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
699d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
700d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
701d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
702d613b6a7SJames Smart  *
703d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
704d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME FCP commands
705d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
706d613b6a7SJames Smart  **/
707d613b6a7SJames Smart static void
708d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
709d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
710d613b6a7SJames Smart {
711d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
712d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
713d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
714e3246a12SJames Smart 	uint32_t status, result, op, start_clean, logerr;
7152b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
7162b65e182SJames Smart 	uint32_t id;
7172b65e182SJames Smart #endif
718d613b6a7SJames Smart 
719d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
72086c67379SJames Smart 	ctxp->flag &= ~LPFC_NVMET_IO_INP;
72186c67379SJames Smart 
722d613b6a7SJames Smart 	rsp = &ctxp->ctx.fcp_req;
723d613b6a7SJames Smart 	op = rsp->op;
724d613b6a7SJames Smart 
725d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
726d613b6a7SJames Smart 	result = wcqe->parameter;
727d613b6a7SJames Smart 
72886c67379SJames Smart 	if (phba->targetport)
72986c67379SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
73086c67379SJames Smart 	else
73186c67379SJames Smart 		tgtp = NULL;
732d613b6a7SJames Smart 
7332b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
7342b65e182SJames Smart 			 ctxp->oxid, op, status);
7352b65e182SJames Smart 
736d613b6a7SJames Smart 	if (status) {
737d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
738d613b6a7SJames Smart 		rsp->transferred_length = 0;
7394b056682SJames Smart 		if (tgtp) {
740d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_error);
7418ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
7424b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_aborted);
7434b056682SJames Smart 		}
74486c67379SJames Smart 
745e3246a12SJames Smart 		logerr = LOG_NVME_IOERR;
746e3246a12SJames Smart 
74786c67379SJames Smart 		/* pick up SLI4 exhange busy condition */
74886c67379SJames Smart 		if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
74986c67379SJames Smart 			ctxp->flag |= LPFC_NVMET_XBUSY;
750e3246a12SJames Smart 			logerr |= LOG_NVME_ABTS;
7514b056682SJames Smart 			if (tgtp)
7524b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_xb_set);
75386c67379SJames Smart 
75486c67379SJames Smart 		} else {
75586c67379SJames Smart 			ctxp->flag &= ~LPFC_NVMET_XBUSY;
75686c67379SJames Smart 		}
75786c67379SJames Smart 
758e3246a12SJames Smart 		lpfc_printf_log(phba, KERN_INFO, logerr,
75979d8c4ceSJames Smart 				"6315 IO Error Cmpl oxid: x%x xri: x%x %x/%x "
76079d8c4ceSJames Smart 				"XBUSY:x%x\n",
76179d8c4ceSJames Smart 				ctxp->oxid, ctxp->ctxbuf->sglq->sli4_xritag,
76279d8c4ceSJames Smart 				status, result, ctxp->flag);
763e3246a12SJames Smart 
764d613b6a7SJames Smart 	} else {
765d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_SUCCESS;
766d613b6a7SJames Smart 		if (op == NVMET_FCOP_RSP)
767d613b6a7SJames Smart 			rsp->transferred_length = rsp->rsplen;
768d613b6a7SJames Smart 		else
769d613b6a7SJames Smart 			rsp->transferred_length = rsp->transfer_length;
77086c67379SJames Smart 		if (tgtp)
771d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
772d613b6a7SJames Smart 	}
773d613b6a7SJames Smart 
774d613b6a7SJames Smart 	if ((op == NVMET_FCOP_READDATA_RSP) ||
775d613b6a7SJames Smart 	    (op == NVMET_FCOP_RSP)) {
776d613b6a7SJames Smart 		/* Sanity check */
777d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DONE;
778d613b6a7SJames Smart 		ctxp->entry_cnt++;
77986c67379SJames Smart 
7802b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
781c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
7822b65e182SJames Smart 			if (rsp->op == NVMET_FCOP_READDATA_RSP) {
7832b65e182SJames Smart 				ctxp->ts_isr_data =
7842b65e182SJames Smart 					cmdwqe->isr_timestamp;
7852b65e182SJames Smart 				ctxp->ts_data_nvme =
7862b65e182SJames Smart 					ktime_get_ns();
7872b65e182SJames Smart 				ctxp->ts_nvme_status =
7882b65e182SJames Smart 					ctxp->ts_data_nvme;
7892b65e182SJames Smart 				ctxp->ts_status_wqput =
7902b65e182SJames Smart 					ctxp->ts_data_nvme;
7912b65e182SJames Smart 				ctxp->ts_isr_status =
7922b65e182SJames Smart 					ctxp->ts_data_nvme;
7932b65e182SJames Smart 				ctxp->ts_status_nvme =
7942b65e182SJames Smart 					ctxp->ts_data_nvme;
7952b65e182SJames Smart 			} else {
7962b65e182SJames Smart 				ctxp->ts_isr_status =
7972b65e182SJames Smart 					cmdwqe->isr_timestamp;
7982b65e182SJames Smart 				ctxp->ts_status_nvme =
7992b65e182SJames Smart 					ktime_get_ns();
8002b65e182SJames Smart 			}
8012b65e182SJames Smart 		}
8022b65e182SJames Smart #endif
803d613b6a7SJames Smart 		rsp->done(rsp);
8042b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
805c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme)
8062b65e182SJames Smart 			lpfc_nvmet_ktime(phba, ctxp);
8072b65e182SJames Smart #endif
80819b58d94SJames Smart 		/* lpfc_nvmet_xmt_fcp_release() will recycle the context */
809d613b6a7SJames Smart 	} else {
810d613b6a7SJames Smart 		ctxp->entry_cnt++;
811c6e0c925SDick Kennedy 		start_clean = offsetof(struct lpfc_iocbq, iocb_flag);
812d613b6a7SJames Smart 		memset(((char *)cmdwqe) + start_clean, 0,
813d613b6a7SJames Smart 		       (sizeof(struct lpfc_iocbq) - start_clean));
8142b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
815c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
8162b65e182SJames Smart 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
8172b65e182SJames Smart 			ctxp->ts_data_nvme = ktime_get_ns();
8182b65e182SJames Smart 		}
8192b65e182SJames Smart #endif
820d613b6a7SJames Smart 		rsp->done(rsp);
821d613b6a7SJames Smart 	}
82263df6d63SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
82363df6d63SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
824d6d189ceSBart Van Assche 		id = raw_smp_processor_id();
82563df6d63SJames Smart 		if (id < LPFC_CHECK_CPU_CNT) {
82663df6d63SJames Smart 			if (ctxp->cpu != id)
82763df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
82863df6d63SJames Smart 						"6704 CPU Check cmdcmpl: "
82963df6d63SJames Smart 						"cpu %d expect %d\n",
83063df6d63SJames Smart 						id, ctxp->cpu);
83163df6d63SJames Smart 			phba->sli4_hba.hdwq[rsp->hwqid].cpucheck_cmpl_io[id]++;
83263df6d63SJames Smart 		}
83363df6d63SJames Smart 	}
83463df6d63SJames Smart #endif
835d613b6a7SJames Smart }
836d613b6a7SJames Smart 
837d613b6a7SJames Smart static int
838d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
839d613b6a7SJames Smart 		      struct nvmefc_tgt_ls_req *rsp)
840d613b6a7SJames Smart {
841d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
842d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.ls_req);
843d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
844d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf =
845d613b6a7SJames Smart 		(struct hbq_dmabuf *)ctxp->rqb_buffer;
846d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
847d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
848d613b6a7SJames Smart 	struct lpfc_dmabuf dmabuf;
849d613b6a7SJames Smart 	struct ulp_bde64 bpl;
850d613b6a7SJames Smart 	int rc;
851d613b6a7SJames Smart 
8521abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
8531abcb371SDick Kennedy 		return -ENODEV;
8541abcb371SDick Kennedy 
8553386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
8563386f4bdSJames Smart 		return -ENODEV;
8573386f4bdSJames Smart 
858d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
859ce1b591cSJames Smart 			"6023 NVMET LS rsp oxid x%x\n", ctxp->oxid);
860ce1b591cSJames Smart 
861ce1b591cSJames Smart 	if ((ctxp->state != LPFC_NVMET_STE_LS_RCV) ||
862ce1b591cSJames Smart 	    (ctxp->entry_cnt != 1)) {
863ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
864ce1b591cSJames Smart 				"6412 NVMET LS rsp state mismatch "
865ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
866ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
867ce1b591cSJames Smart 	}
868ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_LS_RSP;
869ce1b591cSJames Smart 	ctxp->entry_cnt++;
870d613b6a7SJames Smart 
871d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, ctxp, rsp->rspdma,
872d613b6a7SJames Smart 				      rsp->rsplen);
873d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
874d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_drop);
875d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
876d613b6a7SJames Smart 				"6150 LS Drop IO x%x: Prep\n",
877d613b6a7SJames Smart 				ctxp->oxid);
878d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
879547077a4SJames Smart 		atomic_inc(&nvmep->xmt_ls_abort);
880d613b6a7SJames Smart 		lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp,
881d613b6a7SJames Smart 						ctxp->sid, ctxp->oxid);
882d613b6a7SJames Smart 		return -ENOMEM;
883d613b6a7SJames Smart 	}
884d613b6a7SJames Smart 
885d613b6a7SJames Smart 	/* Save numBdes for bpl2sgl */
886d613b6a7SJames Smart 	nvmewqeq->rsvd2 = 1;
887d613b6a7SJames Smart 	nvmewqeq->hba_wqidx = 0;
888d613b6a7SJames Smart 	nvmewqeq->context3 = &dmabuf;
889d613b6a7SJames Smart 	dmabuf.virt = &bpl;
890d613b6a7SJames Smart 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
891d613b6a7SJames Smart 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
892d613b6a7SJames Smart 	bpl.tus.f.bdeSize = rsp->rsplen;
893d613b6a7SJames Smart 	bpl.tus.f.bdeFlags = 0;
894d613b6a7SJames Smart 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
895d613b6a7SJames Smart 
896d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_rsp_cmp;
897d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
898d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
899d613b6a7SJames Smart 
9002b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  RESP: xri x%x wqidx x%x len x%x\n",
9012b65e182SJames Smart 			 ctxp->oxid, nvmewqeq->hba_wqidx, rsp->rsplen);
9022b65e182SJames Smart 
9031fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
904d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
905d613b6a7SJames Smart 		/*
906d613b6a7SJames Smart 		 * Okay to repost buffer here, but wait till cmpl
907d613b6a7SJames Smart 		 * before freeing ctxp and iocbq.
908d613b6a7SJames Smart 		 */
909d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
910d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_rsp);
911d613b6a7SJames Smart 		return 0;
912d613b6a7SJames Smart 	}
913d613b6a7SJames Smart 	/* Give back resources */
914d613b6a7SJames Smart 	atomic_inc(&nvmep->xmt_ls_drop);
915d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
916d613b6a7SJames Smart 			"6151 LS Drop IO x%x: Issue %d\n",
917d613b6a7SJames Smart 			ctxp->oxid, rc);
918d613b6a7SJames Smart 
919d613b6a7SJames Smart 	lpfc_nlp_put(nvmewqeq->context1);
920d613b6a7SJames Smart 
921d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
922547077a4SJames Smart 	atomic_inc(&nvmep->xmt_ls_abort);
923d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
924d613b6a7SJames Smart 	return -ENXIO;
925d613b6a7SJames Smart }
926d613b6a7SJames Smart 
927d613b6a7SJames Smart static int
928d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
929d613b6a7SJames Smart 		      struct nvmefc_tgt_fcp_req *rsp)
930d613b6a7SJames Smart {
931d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
932d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
933d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
934d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
9356e8e1c14SJames Smart 	struct lpfc_queue *wq;
936d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
9376e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
9386e8e1c14SJames Smart 	unsigned long iflags;
939223b78eaSArnd Bergmann 	int rc;
9402b65e182SJames Smart 
9411abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING) {
9421abcb371SDick Kennedy 		rc = -ENODEV;
9431abcb371SDick Kennedy 		goto aerr;
9441abcb371SDick Kennedy 	}
9451abcb371SDick Kennedy 
9463386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING) {
9473386f4bdSJames Smart 		rc = -ENODEV;
9483386f4bdSJames Smart 		goto aerr;
9493386f4bdSJames Smart 	}
9503386f4bdSJames Smart 
9512b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
952c8a4ce0bSDick Kennedy 	if (ctxp->ts_cmd_nvme) {
9532b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
9542b65e182SJames Smart 			ctxp->ts_nvme_status = ktime_get_ns();
9552b65e182SJames Smart 		else
9562b65e182SJames Smart 			ctxp->ts_nvme_data = ktime_get_ns();
9572b65e182SJames Smart 	}
9581fbf9742SJames Smart 
9591fbf9742SJames Smart 	/* Setup the hdw queue if not already set */
9601fbf9742SJames Smart 	if (!ctxp->hdwq)
9611fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[rsp->hwqid];
9621fbf9742SJames Smart 
9632b65e182SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
964d6d189ceSBart Van Assche 		int id = raw_smp_processor_id();
96563df6d63SJames Smart 		if (id < LPFC_CHECK_CPU_CNT) {
96663df6d63SJames Smart 			if (rsp->hwqid != id)
96763df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
9682b65e182SJames Smart 						"6705 CPU Check OP: "
9692b65e182SJames Smart 						"cpu %d expect %d\n",
9702b65e182SJames Smart 						id, rsp->hwqid);
97163df6d63SJames Smart 			phba->sli4_hba.hdwq[rsp->hwqid].cpucheck_xmt_io[id]++;
9722b65e182SJames Smart 		}
97363df6d63SJames Smart 		ctxp->cpu = id; /* Setup cpu for cmpl check */
9742b65e182SJames Smart 	}
9752b65e182SJames Smart #endif
976d613b6a7SJames Smart 
977d613b6a7SJames Smart 	/* Sanity check */
97886c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_ABTS_RCV) ||
97986c67379SJames Smart 	    (ctxp->state == LPFC_NVMET_STE_ABORT)) {
980d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
981d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
98279d8c4ceSJames Smart 				"6102 IO oxid x%x aborted\n",
983d613b6a7SJames Smart 				ctxp->oxid);
984a5068b46SJames Smart 		rc = -ENXIO;
985d613b6a7SJames Smart 		goto aerr;
986d613b6a7SJames Smart 	}
987d613b6a7SJames Smart 
988d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
989d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
990d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
991d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
992d613b6a7SJames Smart 				"6152 FCP Drop IO x%x: Prep\n",
993d613b6a7SJames Smart 				ctxp->oxid);
994a5068b46SJames Smart 		rc = -ENXIO;
995d613b6a7SJames Smart 		goto aerr;
996d613b6a7SJames Smart 	}
997d613b6a7SJames Smart 
998d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
999d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
1000d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
1001d613b6a7SJames Smart 	nvmewqeq->iocb_flag |=  LPFC_IO_NVMET;
1002d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
1003d613b6a7SJames Smart 
10042b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
10052b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
10062b65e182SJames Smart 
100761f3d4bfSJames Smart 	ctxp->flag |= LPFC_NVMET_IO_INP;
10081fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
1009d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
10102b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1011c8a4ce0bSDick Kennedy 		if (!ctxp->ts_cmd_nvme)
10122b65e182SJames Smart 			return 0;
10132b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
10142b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
10152b65e182SJames Smart 		else
10162b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
10172b65e182SJames Smart #endif
1018d613b6a7SJames Smart 		return 0;
1019d613b6a7SJames Smart 	}
1020d613b6a7SJames Smart 
10216e8e1c14SJames Smart 	if (rc == -EBUSY) {
10226e8e1c14SJames Smart 		/*
10236e8e1c14SJames Smart 		 * WQ was full, so queue nvmewqeq to be sent after
10246e8e1c14SJames Smart 		 * WQE release CQE
10256e8e1c14SJames Smart 		 */
10266e8e1c14SJames Smart 		ctxp->flag |= LPFC_NVMET_DEFER_WQFULL;
10271fbf9742SJames Smart 		wq = ctxp->hdwq->nvme_wq;
10286e8e1c14SJames Smart 		pring = wq->pring;
10296e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
10306e8e1c14SJames Smart 		list_add_tail(&nvmewqeq->list, &wq->wqfull_list);
10316e8e1c14SJames Smart 		wq->q_flag |= HBA_NVMET_WQFULL;
10326e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
1033411de511SJames Smart 		atomic_inc(&lpfc_nvmep->defer_wqfull);
10346e8e1c14SJames Smart 		return 0;
10356e8e1c14SJames Smart 	}
10366e8e1c14SJames Smart 
1037d613b6a7SJames Smart 	/* Give back resources */
1038d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1039d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1040d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
1041d613b6a7SJames Smart 			ctxp->oxid, rc);
1042d613b6a7SJames Smart 
1043d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
1044d613b6a7SJames Smart 	nvmewqeq->context2 = NULL;
1045d613b6a7SJames Smart 	nvmewqeq->context3 = NULL;
1046a5068b46SJames Smart 	rc = -EBUSY;
1047d613b6a7SJames Smart aerr:
1048a5068b46SJames Smart 	return rc;
1049d613b6a7SJames Smart }
1050d613b6a7SJames Smart 
1051d613b6a7SJames Smart static void
1052d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
1053d613b6a7SJames Smart {
1054d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
1055d613b6a7SJames Smart 
1056d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
1057c41f5988SEwan D. Milne 	if (tport->phba->targetport)
1058c41f5988SEwan D. Milne 		complete(tport->tport_unreg_cmp);
1059d613b6a7SJames Smart }
1060d613b6a7SJames Smart 
106119b58d94SJames Smart static void
1062a97ec51bSJames Smart lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
1063a97ec51bSJames Smart 			 struct nvmefc_tgt_fcp_req *req)
1064a97ec51bSJames Smart {
1065a97ec51bSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
1066a97ec51bSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
1067a97ec51bSJames Smart 		container_of(req, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
1068a97ec51bSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
10696e8e1c14SJames Smart 	struct lpfc_queue *wq;
107086c67379SJames Smart 	unsigned long flags;
1071a97ec51bSJames Smart 
10721abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
10731abcb371SDick Kennedy 		return;
10741abcb371SDick Kennedy 
10753386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
10763386f4bdSJames Smart 		return;
10773386f4bdSJames Smart 
10781fbf9742SJames Smart 	if (!ctxp->hdwq)
10791fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[0];
10801fbf9742SJames Smart 
1081a97ec51bSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
108279d8c4ceSJames Smart 			"6103 NVMET Abort op: oxid x%x flg x%x ste %d\n",
1083ce1b591cSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
1084a97ec51bSJames Smart 
1085ce1b591cSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x flg x%x ste x%x\n",
1086ce1b591cSJames Smart 			 ctxp->oxid, ctxp->flag, ctxp->state);
1087a97ec51bSJames Smart 
1088a97ec51bSJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
1089ce1b591cSJames Smart 
109086c67379SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
109186c67379SJames Smart 
109286c67379SJames Smart 	/* Since iaab/iaar are NOT set, we need to check
109386c67379SJames Smart 	 * if the firmware is in process of aborting IO
109486c67379SJames Smart 	 */
109579d8c4ceSJames Smart 	if (ctxp->flag & (LPFC_NVMET_XBUSY | LPFC_NVMET_ABORT_OP)) {
109686c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
109786c67379SJames Smart 		return;
109886c67379SJames Smart 	}
1099a97ec51bSJames Smart 	ctxp->flag |= LPFC_NVMET_ABORT_OP;
1100ce1b591cSJames Smart 
11016e8e1c14SJames Smart 	if (ctxp->flag & LPFC_NVMET_DEFER_WQFULL) {
1102c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11036e8e1c14SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
11046e8e1c14SJames Smart 						 ctxp->oxid);
11051fbf9742SJames Smart 		wq = ctxp->hdwq->nvme_wq;
11066e8e1c14SJames Smart 		lpfc_nvmet_wqfull_flush(phba, wq, ctxp);
11076e8e1c14SJames Smart 		return;
11086e8e1c14SJames Smart 	}
1109c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11106e8e1c14SJames Smart 
1111ce1b591cSJames Smart 	/* An state of LPFC_NVMET_STE_RCV means we have just received
1112ce1b591cSJames Smart 	 * the NVME command and have not started processing it.
1113ce1b591cSJames Smart 	 * (by issuing any IO WQEs on this exchange yet)
1114ce1b591cSJames Smart 	 */
1115ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_RCV)
1116ce1b591cSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1117a97ec51bSJames Smart 						 ctxp->oxid);
1118a97ec51bSJames Smart 	else
1119ce1b591cSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1120a97ec51bSJames Smart 					       ctxp->oxid);
1121a97ec51bSJames Smart }
1122a97ec51bSJames Smart 
1123a97ec51bSJames Smart static void
112419b58d94SJames Smart lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
112519b58d94SJames Smart 			   struct nvmefc_tgt_fcp_req *rsp)
112619b58d94SJames Smart {
1127547077a4SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
112819b58d94SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
112919b58d94SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
113019b58d94SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
113119b58d94SJames Smart 	unsigned long flags;
113219b58d94SJames Smart 	bool aborting = false;
113319b58d94SJames Smart 
1134c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
1135c160c0f8SJames Smart 	if (ctxp->flag & LPFC_NVMET_XBUSY)
1136c160c0f8SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1137c160c0f8SJames Smart 				"6027 NVMET release with XBUSY flag x%x"
1138c160c0f8SJames Smart 				" oxid x%x\n",
1139c160c0f8SJames Smart 				ctxp->flag, ctxp->oxid);
1140c160c0f8SJames Smart 	else if (ctxp->state != LPFC_NVMET_STE_DONE &&
1141c160c0f8SJames Smart 		 ctxp->state != LPFC_NVMET_STE_ABORT)
1142ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1143ce1b591cSJames Smart 				"6413 NVMET release bad state %d %d oxid x%x\n",
1144ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
1145ce1b591cSJames Smart 
114686c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_ABORT_OP) ||
114786c67379SJames Smart 	    (ctxp->flag & LPFC_NVMET_XBUSY)) {
114819b58d94SJames Smart 		aborting = true;
114986c67379SJames Smart 		/* let the abort path do the real release */
115086c67379SJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
115119b58d94SJames Smart 	}
115219b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
115319b58d94SJames Smart 
11549094367aSArnd Bergmann 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d abt %d\n", ctxp->oxid,
11559094367aSArnd Bergmann 			 ctxp->state, aborting);
115619b58d94SJames Smart 
1157547077a4SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_release);
115879d8c4ceSJames Smart 	ctxp->flag &= ~LPFC_NVMET_TNOTIFY;
1159547077a4SJames Smart 
116086c67379SJames Smart 	if (aborting)
116186c67379SJames Smart 		return;
116286c67379SJames Smart 
11636c621a22SJames Smart 	lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
116419b58d94SJames Smart }
116519b58d94SJames Smart 
116650738420SJames Smart static void
116750738420SJames Smart lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port *tgtport,
116850738420SJames Smart 		     struct nvmefc_tgt_fcp_req *rsp)
116950738420SJames Smart {
117050738420SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
117150738420SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
117250738420SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
117350738420SJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
117450738420SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
1175472e146dSJames Smart 	unsigned long iflag;
1176472e146dSJames Smart 
117750738420SJames Smart 
117850738420SJames Smart 	lpfc_nvmeio_data(phba, "NVMET DEFERRCV: xri x%x sz %d CPU %02x\n",
1179d6d189ceSBart Van Assche 			 ctxp->oxid, ctxp->size, raw_smp_processor_id());
118050738420SJames Smart 
11816871e814SJames Smart 	if (!nvmebuf) {
11826871e814SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
118379d8c4ceSJames Smart 				"6425 Defer rcv: no buffer oxid x%x: "
11846871e814SJames Smart 				"flg %x ste %x\n",
11856871e814SJames Smart 				ctxp->oxid, ctxp->flag, ctxp->state);
11866871e814SJames Smart 		return;
11876871e814SJames Smart 	}
11886871e814SJames Smart 
118950738420SJames Smart 	tgtp = phba->targetport->private;
11906871e814SJames Smart 	if (tgtp)
119150738420SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_defer);
1192411de511SJames Smart 
1193411de511SJames Smart 	/* Free the nvmebuf since a new buffer already replaced it */
1194cbc5de1bSJames Smart 	nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
1195472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflag);
1196472e146dSJames Smart 	ctxp->rqb_buffer = NULL;
1197472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
119850738420SJames Smart }
119950738420SJames Smart 
1200d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
1201d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
1202d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
1203d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
1204a97ec51bSJames Smart 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
120519b58d94SJames Smart 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
120650738420SJames Smart 	.defer_rcv	= lpfc_nvmet_defer_rcv,
1207d613b6a7SJames Smart 
1208d613b6a7SJames Smart 	.max_hw_queues  = 1,
1209d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1210d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1211d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
1212d613b6a7SJames Smart 
1213d613b6a7SJames Smart 	/* optional features */
1214d613b6a7SJames Smart 	.target_features = 0,
1215d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
1216d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
1217d613b6a7SJames Smart };
1218d613b6a7SJames Smart 
121939aa23f9SColin Ian King static void
122066d7ce93SDick Kennedy __lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba *phba,
122166d7ce93SDick Kennedy 		struct lpfc_nvmet_ctx_info *infop)
12226c621a22SJames Smart {
12236c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf, *next_ctx_buf;
12246c621a22SJames Smart 	unsigned long flags;
12256c621a22SJames Smart 
122666d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, flags);
1227966bb5b7SJames Smart 	list_for_each_entry_safe(ctx_buf, next_ctx_buf,
122866d7ce93SDick Kennedy 				&infop->nvmet_ctx_list, list) {
12295e5b511dSJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
12306c621a22SJames Smart 		list_del_init(&ctx_buf->list);
12315e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
123266d7ce93SDick Kennedy 
123366d7ce93SDick Kennedy 		__lpfc_clear_active_sglq(phba, ctx_buf->sglq->sli4_lxritag);
12346c621a22SJames Smart 		ctx_buf->sglq->state = SGL_FREED;
12356c621a22SJames Smart 		ctx_buf->sglq->ndlp = NULL;
12366c621a22SJames Smart 
1237c4031db7SDan Carpenter 		spin_lock(&phba->sli4_hba.sgl_list_lock);
12386c621a22SJames Smart 		list_add_tail(&ctx_buf->sglq->list,
12396c621a22SJames Smart 				&phba->sli4_hba.lpfc_nvmet_sgl_list);
1240c4031db7SDan Carpenter 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
12416c621a22SJames Smart 
12426c621a22SJames Smart 		lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
12436c621a22SJames Smart 		kfree(ctx_buf->context);
12446c621a22SJames Smart 	}
124566d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, flags);
1246966bb5b7SJames Smart }
124766d7ce93SDick Kennedy 
124866d7ce93SDick Kennedy static void
124966d7ce93SDick Kennedy lpfc_nvmet_cleanup_io_context(struct lpfc_hba *phba)
125066d7ce93SDick Kennedy {
125166d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
125266d7ce93SDick Kennedy 	int i, j;
125366d7ce93SDick Kennedy 
125466d7ce93SDick Kennedy 	/* The first context list, MRQ 0 CPU 0 */
125566d7ce93SDick Kennedy 	infop = phba->sli4_hba.nvmet_ctx_info;
125666d7ce93SDick Kennedy 	if (!infop)
125766d7ce93SDick Kennedy 		return;
125866d7ce93SDick Kennedy 
125966d7ce93SDick Kennedy 	/* Cycle the the entire CPU context list for every MRQ */
126066d7ce93SDick Kennedy 	for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
1261222e9239SJames Smart 		for_each_present_cpu(j) {
1262222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, j, i);
126366d7ce93SDick Kennedy 			__lpfc_nvmet_clean_io_for_cpu(phba, infop);
126466d7ce93SDick Kennedy 		}
126566d7ce93SDick Kennedy 	}
126666d7ce93SDick Kennedy 	kfree(phba->sli4_hba.nvmet_ctx_info);
126766d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = NULL;
12686c621a22SJames Smart }
12696c621a22SJames Smart 
127039aa23f9SColin Ian King static int
12716c621a22SJames Smart lpfc_nvmet_setup_io_context(struct lpfc_hba *phba)
12726c621a22SJames Smart {
12736c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
12746c621a22SJames Smart 	struct lpfc_iocbq *nvmewqe;
12756c621a22SJames Smart 	union lpfc_wqe128 *wqe;
127666d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *last_infop;
127766d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
1278222e9239SJames Smart 	int i, j, idx, cpu;
12796c621a22SJames Smart 
12806c621a22SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
12816c621a22SJames Smart 			"6403 Allocate NVMET resources for %d XRIs\n",
12826c621a22SJames Smart 			phba->sli4_hba.nvmet_xri_cnt);
12836c621a22SJames Smart 
128466d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = kcalloc(
1285222e9239SJames Smart 		phba->sli4_hba.num_possible_cpu * phba->cfg_nvmet_mrq,
128666d7ce93SDick Kennedy 		sizeof(struct lpfc_nvmet_ctx_info), GFP_KERNEL);
128766d7ce93SDick Kennedy 	if (!phba->sli4_hba.nvmet_ctx_info) {
128866d7ce93SDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
128966d7ce93SDick Kennedy 				"6419 Failed allocate memory for "
129066d7ce93SDick Kennedy 				"nvmet context lists\n");
129166d7ce93SDick Kennedy 		return -ENOMEM;
129266d7ce93SDick Kennedy 	}
129366d7ce93SDick Kennedy 
129466d7ce93SDick Kennedy 	/*
129566d7ce93SDick Kennedy 	 * Assuming X CPUs in the system, and Y MRQs, allocate some
129666d7ce93SDick Kennedy 	 * lpfc_nvmet_ctx_info structures as follows:
129766d7ce93SDick Kennedy 	 *
129866d7ce93SDick Kennedy 	 * cpu0/mrq0 cpu1/mrq0 ... cpuX/mrq0
129966d7ce93SDick Kennedy 	 * cpu0/mrq1 cpu1/mrq1 ... cpuX/mrq1
130066d7ce93SDick Kennedy 	 * ...
130166d7ce93SDick Kennedy 	 * cpuX/mrqY cpuX/mrqY ... cpuX/mrqY
130266d7ce93SDick Kennedy 	 *
130366d7ce93SDick Kennedy 	 * Each line represents a MRQ "silo" containing an entry for
130466d7ce93SDick Kennedy 	 * every CPU.
130566d7ce93SDick Kennedy 	 *
130666d7ce93SDick Kennedy 	 * MRQ X is initially assumed to be associated with CPU X, thus
130766d7ce93SDick Kennedy 	 * contexts are initially distributed across all MRQs using
130866d7ce93SDick Kennedy 	 * the MRQ index (N) as follows cpuN/mrqN. When contexts are
130966d7ce93SDick Kennedy 	 * freed, the are freed to the MRQ silo based on the CPU number
131066d7ce93SDick Kennedy 	 * of the IO completion. Thus a context that was allocated for MRQ A
131166d7ce93SDick Kennedy 	 * whose IO completed on CPU B will be freed to cpuB/mrqA.
131266d7ce93SDick Kennedy 	 */
1313222e9239SJames Smart 	for_each_possible_cpu(i) {
131466d7ce93SDick Kennedy 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1315222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
131666d7ce93SDick Kennedy 			INIT_LIST_HEAD(&infop->nvmet_ctx_list);
131766d7ce93SDick Kennedy 			spin_lock_init(&infop->nvmet_ctx_list_lock);
131866d7ce93SDick Kennedy 			infop->nvmet_ctx_list_cnt = 0;
131966d7ce93SDick Kennedy 		}
132066d7ce93SDick Kennedy 	}
132166d7ce93SDick Kennedy 
132266d7ce93SDick Kennedy 	/*
132366d7ce93SDick Kennedy 	 * Setup the next CPU context info ptr for each MRQ.
132466d7ce93SDick Kennedy 	 * MRQ 0 will cycle thru CPUs 0 - X separately from
132566d7ce93SDick Kennedy 	 * MRQ 1 cycling thru CPUs 0 - X, and so on.
132666d7ce93SDick Kennedy 	 */
132766d7ce93SDick Kennedy 	for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1328222e9239SJames Smart 		last_infop = lpfc_get_ctx_list(phba,
1329222e9239SJames Smart 					       cpumask_first(cpu_present_mask),
1330222e9239SJames Smart 					       j);
1331222e9239SJames Smart 		for (i = phba->sli4_hba.num_possible_cpu - 1;  i >= 0; i--) {
133266d7ce93SDick Kennedy 			infop = lpfc_get_ctx_list(phba, i, j);
133366d7ce93SDick Kennedy 			infop->nvmet_ctx_next_cpu = last_infop;
133466d7ce93SDick Kennedy 			last_infop = infop;
133566d7ce93SDick Kennedy 		}
133666d7ce93SDick Kennedy 	}
133766d7ce93SDick Kennedy 
13386c621a22SJames Smart 	/* For all nvmet xris, allocate resources needed to process a
13396c621a22SJames Smart 	 * received command on a per xri basis.
13406c621a22SJames Smart 	 */
134166d7ce93SDick Kennedy 	idx = 0;
1342222e9239SJames Smart 	cpu = cpumask_first(cpu_present_mask);
13436c621a22SJames Smart 	for (i = 0; i < phba->sli4_hba.nvmet_xri_cnt; i++) {
13446c621a22SJames Smart 		ctx_buf = kzalloc(sizeof(*ctx_buf), GFP_KERNEL);
13456c621a22SJames Smart 		if (!ctx_buf) {
13466c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13476c621a22SJames Smart 					"6404 Ran out of memory for NVMET\n");
13486c621a22SJames Smart 			return -ENOMEM;
13496c621a22SJames Smart 		}
13506c621a22SJames Smart 
13516c621a22SJames Smart 		ctx_buf->context = kzalloc(sizeof(*ctx_buf->context),
13526c621a22SJames Smart 					   GFP_KERNEL);
13536c621a22SJames Smart 		if (!ctx_buf->context) {
13546c621a22SJames Smart 			kfree(ctx_buf);
13556c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13566c621a22SJames Smart 					"6405 Ran out of NVMET "
13576c621a22SJames Smart 					"context memory\n");
13586c621a22SJames Smart 			return -ENOMEM;
13596c621a22SJames Smart 		}
13606c621a22SJames Smart 		ctx_buf->context->ctxbuf = ctx_buf;
1361ce1b591cSJames Smart 		ctx_buf->context->state = LPFC_NVMET_STE_FREE;
13626c621a22SJames Smart 
13636c621a22SJames Smart 		ctx_buf->iocbq = lpfc_sli_get_iocbq(phba);
13646c621a22SJames Smart 		if (!ctx_buf->iocbq) {
13656c621a22SJames Smart 			kfree(ctx_buf->context);
13666c621a22SJames Smart 			kfree(ctx_buf);
13676c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13686c621a22SJames Smart 					"6406 Ran out of NVMET iocb/WQEs\n");
13696c621a22SJames Smart 			return -ENOMEM;
13706c621a22SJames Smart 		}
13716c621a22SJames Smart 		ctx_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
13726c621a22SJames Smart 		nvmewqe = ctx_buf->iocbq;
1373205e8240SJames Smart 		wqe = &nvmewqe->wqe;
1374bd3061baSJames Smart 
13756c621a22SJames Smart 		/* Initialize WQE */
13766c621a22SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
13776c621a22SJames Smart 
13786c621a22SJames Smart 		ctx_buf->iocbq->context1 = NULL;
13796c621a22SJames Smart 		spin_lock(&phba->sli4_hba.sgl_list_lock);
13806c621a22SJames Smart 		ctx_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, ctx_buf->iocbq);
13816c621a22SJames Smart 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
13826c621a22SJames Smart 		if (!ctx_buf->sglq) {
13836c621a22SJames Smart 			lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
13846c621a22SJames Smart 			kfree(ctx_buf->context);
13856c621a22SJames Smart 			kfree(ctx_buf);
13866c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13876c621a22SJames Smart 					"6407 Ran out of NVMET XRIs\n");
13886c621a22SJames Smart 			return -ENOMEM;
13896c621a22SJames Smart 		}
1390472e146dSJames Smart 		INIT_WORK(&ctx_buf->defer_work, lpfc_nvmet_fcp_rqst_defer_work);
139166d7ce93SDick Kennedy 
139266d7ce93SDick Kennedy 		/*
139366d7ce93SDick Kennedy 		 * Add ctx to MRQidx context list. Our initial assumption
139466d7ce93SDick Kennedy 		 * is MRQidx will be associated with CPUidx. This association
139566d7ce93SDick Kennedy 		 * can change on the fly.
139666d7ce93SDick Kennedy 		 */
1397222e9239SJames Smart 		infop = lpfc_get_ctx_list(phba, cpu, idx);
139866d7ce93SDick Kennedy 		spin_lock(&infop->nvmet_ctx_list_lock);
139966d7ce93SDick Kennedy 		list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
140066d7ce93SDick Kennedy 		infop->nvmet_ctx_list_cnt++;
140166d7ce93SDick Kennedy 		spin_unlock(&infop->nvmet_ctx_list_lock);
140266d7ce93SDick Kennedy 
140366d7ce93SDick Kennedy 		/* Spread ctx structures evenly across all MRQs */
140466d7ce93SDick Kennedy 		idx++;
1405222e9239SJames Smart 		if (idx >= phba->cfg_nvmet_mrq) {
140666d7ce93SDick Kennedy 			idx = 0;
1407222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1408222e9239SJames Smart 			continue;
1409222e9239SJames Smart 		}
1410222e9239SJames Smart 		cpu = cpumask_next(cpu, cpu_present_mask);
1411222e9239SJames Smart 		if (cpu == nr_cpu_ids)
1412222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1413222e9239SJames Smart 
14146c621a22SJames Smart 	}
141566d7ce93SDick Kennedy 
1416222e9239SJames Smart 	for_each_present_cpu(i) {
14172879265fSJames Smart 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
14182879265fSJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
141966d7ce93SDick Kennedy 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
142066d7ce93SDick Kennedy 					"6408 TOTAL NVMET ctx for CPU %d "
142166d7ce93SDick Kennedy 					"MRQ %d: cnt %d nextcpu %p\n",
142266d7ce93SDick Kennedy 					i, j, infop->nvmet_ctx_list_cnt,
142366d7ce93SDick Kennedy 					infop->nvmet_ctx_next_cpu);
142466d7ce93SDick Kennedy 		}
142566d7ce93SDick Kennedy 	}
14266c621a22SJames Smart 	return 0;
14276c621a22SJames Smart }
14286c621a22SJames Smart 
1429d613b6a7SJames Smart int
1430d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1431d613b6a7SJames Smart {
1432d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1433d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1434d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
14356c621a22SJames Smart 	int error;
1436d613b6a7SJames Smart 
1437d613b6a7SJames Smart 	if (phba->targetport)
1438d613b6a7SJames Smart 		return 0;
1439d613b6a7SJames Smart 
14406c621a22SJames Smart 	error = lpfc_nvmet_setup_io_context(phba);
14416c621a22SJames Smart 	if (error)
14426c621a22SJames Smart 		return error;
14436c621a22SJames Smart 
1444d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1445d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1446d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1447d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
1448d613b6a7SJames Smart 
14495b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
14505b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
14515b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
14524d4c4a4aSJames Smart 	 */
14534d4c4a4aSJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1454cdb42becSJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_hdw_queue;
1455f485c18dSDick Kennedy 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP;
1456d613b6a7SJames Smart 
14577d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1458d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1459d613b6a7SJames Smart 					     &phba->pcidev->dev,
1460d613b6a7SJames Smart 					     &phba->targetport);
1461166d7211SJames Smart #else
14626c621a22SJames Smart 	error = -ENOENT;
1463166d7211SJames Smart #endif
1464d613b6a7SJames Smart 	if (error) {
1465d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1466e7981a2cSDick Kennedy 				"6025 Cannot register NVME targetport x%x: "
1467e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1468e7981a2cSDick Kennedy 				error,
1469e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1470e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1471e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1472d613b6a7SJames Smart 		phba->targetport = NULL;
1473e7981a2cSDick Kennedy 		phba->nvmet_support = 0;
14746c621a22SJames Smart 
14756c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
14766c621a22SJames Smart 
1477d613b6a7SJames Smart 	} else {
1478d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
1479d613b6a7SJames Smart 			phba->targetport->private;
1480d613b6a7SJames Smart 		tgtp->phba = phba;
1481d613b6a7SJames Smart 
1482d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1483d613b6a7SJames Smart 				"6026 Registered NVME "
1484d613b6a7SJames Smart 				"targetport: %p, private %p "
1485e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1486d613b6a7SJames Smart 				phba->targetport, tgtp,
1487e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1488e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1489e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1490d613b6a7SJames Smart 
1491d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
1492d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
1493d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
1494d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
1495547077a4SJames Smart 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1496d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
1497d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
1498d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
14994b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_xb_set, 0);
15004b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_aborted, 0);
1501d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1502d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1503d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1504d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1505d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
1506d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1507d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
1508d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
1509d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
1510547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_release, 0);
1511d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1512d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
15134b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_xb_set, 0);
15144b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_aborted, 0);
1515d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
15164b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_xri_abort_cqe, 0);
1517547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
1518547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1519547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_unsol, 0);
1520547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_sol, 0);
1521d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
1522d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1523411de511SJames Smart 		atomic_set(&tgtp->defer_ctx, 0);
1524411de511SJames Smart 		atomic_set(&tgtp->defer_fod, 0);
1525411de511SJames Smart 		atomic_set(&tgtp->defer_wqfull, 0);
1526d613b6a7SJames Smart 	}
1527d613b6a7SJames Smart 	return error;
1528d613b6a7SJames Smart }
1529d613b6a7SJames Smart 
1530d613b6a7SJames Smart int
1531d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1532d613b6a7SJames Smart {
1533d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1534d613b6a7SJames Smart 
1535d613b6a7SJames Smart 	if (!phba->targetport)
1536d613b6a7SJames Smart 		return 0;
1537d613b6a7SJames Smart 
1538d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
1539d613b6a7SJames Smart 			 "6007 Update NVMET port %p did x%x\n",
1540d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
1541d613b6a7SJames Smart 
1542d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
1543d613b6a7SJames Smart 	return 0;
1544d613b6a7SJames Smart }
1545d613b6a7SJames Smart 
1546318083adSJames Smart /**
1547318083adSJames Smart  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1548318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
1549318083adSJames Smart  * @axri: pointer to the nvmet xri abort wcqe structure.
1550318083adSJames Smart  *
1551318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
1552318083adSJames Smart  * NVMET aborted xri.
1553318083adSJames Smart  **/
1554318083adSJames Smart void
1555318083adSJames Smart lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
1556318083adSJames Smart 			    struct sli4_wcqe_xri_aborted *axri)
1557318083adSJames Smart {
155832b93865SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
155986c67379SJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
156086c67379SJames Smart 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
156186c67379SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
15624b056682SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
156379d8c4ceSJames Smart 	struct nvmefc_tgt_fcp_req *req = NULL;
156486c67379SJames Smart 	struct lpfc_nodelist *ndlp;
156586c67379SJames Smart 	unsigned long iflag = 0;
156686c67379SJames Smart 	int rrq_empty = 0;
156786c67379SJames Smart 	bool released = false;
156886c67379SJames Smart 
156986c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
157086c67379SJames Smart 			"6317 XB aborted xri x%x rxid x%x\n", xri, rxid);
157186c67379SJames Smart 
157286c67379SJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
157386c67379SJames Smart 		return;
15744b056682SJames Smart 
15754b056682SJames Smart 	if (phba->targetport) {
15764b056682SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
15774b056682SJames Smart 		atomic_inc(&tgtp->xmt_fcp_xri_abort_cqe);
15784b056682SJames Smart 	}
15794b056682SJames Smart 
158086c67379SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
15815e5b511dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
158286c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
158386c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
158486c67379SJames Smart 				 list) {
15856c621a22SJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
158686c67379SJames Smart 			continue;
158786c67379SJames Smart 
1588c160c0f8SJames Smart 		spin_lock(&ctxp->ctxlock);
158986c67379SJames Smart 		/* Check if we already received a free context call
159086c67379SJames Smart 		 * and we have completed processing an abort situation.
159186c67379SJames Smart 		 */
159286c67379SJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_RLS &&
159386c67379SJames Smart 		    !(ctxp->flag & LPFC_NVMET_ABORT_OP)) {
159479d8c4ceSJames Smart 			list_del_init(&ctxp->list);
159586c67379SJames Smart 			released = true;
159686c67379SJames Smart 		}
159786c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_XBUSY;
1598c160c0f8SJames Smart 		spin_unlock(&ctxp->ctxlock);
15995e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
160086c67379SJames Smart 
160186c67379SJames Smart 		rrq_empty = list_empty(&phba->active_rrq_list);
160286c67379SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, iflag);
160386c67379SJames Smart 		ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
160486c67379SJames Smart 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
160586c67379SJames Smart 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
160686c67379SJames Smart 		     ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
160786c67379SJames Smart 			lpfc_set_rrq_active(phba, ndlp,
16086c621a22SJames Smart 				ctxp->ctxbuf->sglq->sli4_lxritag,
160986c67379SJames Smart 				rxid, 1);
161086c67379SJames Smart 			lpfc_sli4_abts_err_handler(phba, ndlp, axri);
161186c67379SJames Smart 		}
161286c67379SJames Smart 
161386c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
161479d8c4ceSJames Smart 				"6318 XB aborted oxid x%x flg x%x (%x)\n",
161586c67379SJames Smart 				ctxp->oxid, ctxp->flag, released);
161686c67379SJames Smart 		if (released)
16176c621a22SJames Smart 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
16186c621a22SJames Smart 
161986c67379SJames Smart 		if (rrq_empty)
162086c67379SJames Smart 			lpfc_worker_wake_up(phba);
162186c67379SJames Smart 		return;
162286c67379SJames Smart 	}
16235e5b511dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
162486c67379SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
162579d8c4ceSJames Smart 
162679d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_xri(phba, xri);
162779d8c4ceSJames Smart 	if (ctxp) {
162879d8c4ceSJames Smart 		/*
162979d8c4ceSJames Smart 		 *  Abort already done by FW, so BA_ACC sent.
163079d8c4ceSJames Smart 		 *  However, the transport may be unaware.
163179d8c4ceSJames Smart 		 */
163279d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
163379d8c4ceSJames Smart 				"6323 NVMET Rcv ABTS xri x%x ctxp state x%x "
163479d8c4ceSJames Smart 				"flag x%x oxid x%x rxid x%x\n",
163579d8c4ceSJames Smart 				xri, ctxp->state, ctxp->flag, ctxp->oxid,
163679d8c4ceSJames Smart 				rxid);
163779d8c4ceSJames Smart 
163879d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
163979d8c4ceSJames Smart 		ctxp->flag |= LPFC_NVMET_ABTS_RCV;
164079d8c4ceSJames Smart 		ctxp->state = LPFC_NVMET_STE_ABORT;
164179d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
164279d8c4ceSJames Smart 
164379d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
164479d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
164501d53c04SJames Smart 				 xri, raw_smp_processor_id(), 0);
164679d8c4ceSJames Smart 
164779d8c4ceSJames Smart 		req = &ctxp->ctx.fcp_req;
164879d8c4ceSJames Smart 		if (req)
164979d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport, req);
165079d8c4ceSJames Smart 	}
165132b93865SJames Smart #endif
165286c67379SJames Smart }
165386c67379SJames Smart 
165486c67379SJames Smart int
165586c67379SJames Smart lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport *vport,
165686c67379SJames Smart 			   struct fc_frame_header *fc_hdr)
165786c67379SJames Smart {
165886c67379SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
165986c67379SJames Smart 	struct lpfc_hba *phba = vport->phba;
166086c67379SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
166186c67379SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
166232b93865SJames Smart 	uint32_t sid;
166379d8c4ceSJames Smart 	uint16_t oxid, xri;
166486c67379SJames Smart 	unsigned long iflag = 0;
166586c67379SJames Smart 
166632b93865SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
166779d8c4ceSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
166886c67379SJames Smart 
166986c67379SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
16705e5b511dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
167186c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
167286c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
167386c67379SJames Smart 				 list) {
167479d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
167586c67379SJames Smart 			continue;
167686c67379SJames Smart 
167732b93865SJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
167832b93865SJames Smart 
16795e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
168086c67379SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, iflag);
168186c67379SJames Smart 
168286c67379SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
168386c67379SJames Smart 		ctxp->flag |= LPFC_NVMET_ABTS_RCV;
168486c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
168586c67379SJames Smart 
168686c67379SJames Smart 		lpfc_nvmeio_data(phba,
168786c67379SJames Smart 			"NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1688d6d189ceSBart Van Assche 			xri, raw_smp_processor_id(), 0);
168986c67379SJames Smart 
169086c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
169186c67379SJames Smart 				"6319 NVMET Rcv ABTS:acc xri x%x\n", xri);
169286c67379SJames Smart 
169386c67379SJames Smart 		rsp = &ctxp->ctx.fcp_req;
169486c67379SJames Smart 		nvmet_fc_rcv_fcp_abort(phba->targetport, rsp);
169586c67379SJames Smart 
169686c67379SJames Smart 		/* Respond with BA_ACC accordingly */
169786c67379SJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
169886c67379SJames Smart 		return 0;
169986c67379SJames Smart 	}
17005e5b511dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
170186c67379SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
170286c67379SJames Smart 
170379d8c4ceSJames Smart 	/* check the wait list */
170479d8c4ceSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
170579d8c4ceSJames Smart 		struct rqb_dmabuf *nvmebuf;
170679d8c4ceSJames Smart 		struct fc_frame_header *fc_hdr_tmp;
170779d8c4ceSJames Smart 		u32 sid_tmp;
170879d8c4ceSJames Smart 		u16 oxid_tmp;
170979d8c4ceSJames Smart 		bool found = false;
171079d8c4ceSJames Smart 
171179d8c4ceSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
171279d8c4ceSJames Smart 
171379d8c4ceSJames Smart 		/* match by oxid and s_id */
171479d8c4ceSJames Smart 		list_for_each_entry(nvmebuf,
171579d8c4ceSJames Smart 				    &phba->sli4_hba.lpfc_nvmet_io_wait_list,
171679d8c4ceSJames Smart 				    hbuf.list) {
171779d8c4ceSJames Smart 			fc_hdr_tmp = (struct fc_frame_header *)
171879d8c4ceSJames Smart 					(nvmebuf->hbuf.virt);
171979d8c4ceSJames Smart 			oxid_tmp = be16_to_cpu(fc_hdr_tmp->fh_ox_id);
172079d8c4ceSJames Smart 			sid_tmp = sli4_sid_from_fc_hdr(fc_hdr_tmp);
172179d8c4ceSJames Smart 			if (oxid_tmp != oxid || sid_tmp != sid)
172279d8c4ceSJames Smart 				continue;
172386c67379SJames Smart 
172486c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
172579d8c4ceSJames Smart 					"6321 NVMET Rcv ABTS oxid x%x from x%x "
172679d8c4ceSJames Smart 					"is waiting for a ctxp\n",
172779d8c4ceSJames Smart 					oxid, sid);
172879d8c4ceSJames Smart 
172979d8c4ceSJames Smart 			list_del_init(&nvmebuf->hbuf.list);
173079d8c4ceSJames Smart 			phba->sli4_hba.nvmet_io_wait_cnt--;
173179d8c4ceSJames Smart 			found = true;
173279d8c4ceSJames Smart 			break;
173379d8c4ceSJames Smart 		}
173479d8c4ceSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
173579d8c4ceSJames Smart 				       iflag);
173679d8c4ceSJames Smart 
173779d8c4ceSJames Smart 		/* free buffer since already posted a new DMA buffer to RQ */
173879d8c4ceSJames Smart 		if (found) {
173979d8c4ceSJames Smart 			nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
174079d8c4ceSJames Smart 			/* Respond with BA_ACC accordingly */
174179d8c4ceSJames Smart 			lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
174279d8c4ceSJames Smart 			return 0;
174379d8c4ceSJames Smart 		}
174479d8c4ceSJames Smart 	}
174579d8c4ceSJames Smart 
174679d8c4ceSJames Smart 	/* check active list */
174779d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_oxid(phba, oxid, sid);
174879d8c4ceSJames Smart 	if (ctxp) {
174979d8c4ceSJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
175079d8c4ceSJames Smart 
175179d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
175279d8c4ceSJames Smart 		ctxp->flag |= (LPFC_NVMET_ABTS_RCV | LPFC_NVMET_ABORT_OP);
175379d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
175479d8c4ceSJames Smart 
175579d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
175679d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
175779d8c4ceSJames Smart 				 xri, raw_smp_processor_id(), 0);
175879d8c4ceSJames Smart 
175979d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
176079d8c4ceSJames Smart 				"6322 NVMET Rcv ABTS:acc oxid x%x xri x%x "
176179d8c4ceSJames Smart 				"flag x%x state x%x\n",
176279d8c4ceSJames Smart 				ctxp->oxid, xri, ctxp->flag, ctxp->state);
176379d8c4ceSJames Smart 
176479d8c4ceSJames Smart 		if (ctxp->flag & LPFC_NVMET_TNOTIFY) {
176579d8c4ceSJames Smart 			/* Notify the transport */
176679d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport,
176779d8c4ceSJames Smart 					       &ctxp->ctx.fcp_req);
176879d8c4ceSJames Smart 		} else {
17696594d31bSJames Smart 			cancel_work_sync(&ctxp->ctxbuf->defer_work);
177079d8c4ceSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
177179d8c4ceSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
177279d8c4ceSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
177379d8c4ceSJames Smart 		}
177479d8c4ceSJames Smart 		if (ctxp->state == LPFC_NVMET_STE_RCV)
177579d8c4ceSJames Smart 			lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
177679d8c4ceSJames Smart 							 ctxp->oxid);
177779d8c4ceSJames Smart 		else
177879d8c4ceSJames Smart 			lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
177979d8c4ceSJames Smart 						       ctxp->oxid);
178079d8c4ceSJames Smart 
17816594d31bSJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
178279d8c4ceSJames Smart 		return 0;
178379d8c4ceSJames Smart 	}
178479d8c4ceSJames Smart 
178579d8c4ceSJames Smart 	lpfc_nvmeio_data(phba, "NVMET ABTS RCV: oxid x%x CPU %02x rjt %d\n",
178679d8c4ceSJames Smart 			 oxid, raw_smp_processor_id(), 1);
178779d8c4ceSJames Smart 
178879d8c4ceSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
178979d8c4ceSJames Smart 			"6320 NVMET Rcv ABTS:rjt oxid x%x\n", oxid);
179086c67379SJames Smart 
179186c67379SJames Smart 	/* Respond with BA_RJT accordingly */
179286c67379SJames Smart 	lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 0);
179386c67379SJames Smart #endif
1794b06e13c3SJens Axboe 	return 0;
1795318083adSJames Smart }
1796318083adSJames Smart 
17976e8e1c14SJames Smart static void
17986e8e1c14SJames Smart lpfc_nvmet_wqfull_flush(struct lpfc_hba *phba, struct lpfc_queue *wq,
17996e8e1c14SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
18006e8e1c14SJames Smart {
18016e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
18026e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
18036e8e1c14SJames Smart 	struct lpfc_iocbq *next_nvmewqeq;
18046e8e1c14SJames Smart 	unsigned long iflags;
18056e8e1c14SJames Smart 	struct lpfc_wcqe_complete wcqe;
18066e8e1c14SJames Smart 	struct lpfc_wcqe_complete *wcqep;
18076e8e1c14SJames Smart 
18086e8e1c14SJames Smart 	pring = wq->pring;
18096e8e1c14SJames Smart 	wcqep = &wcqe;
18106e8e1c14SJames Smart 
18116e8e1c14SJames Smart 	/* Fake an ABORT error code back to cmpl routine */
18126e8e1c14SJames Smart 	memset(wcqep, 0, sizeof(struct lpfc_wcqe_complete));
18136e8e1c14SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, IOSTAT_LOCAL_REJECT);
18146e8e1c14SJames Smart 	wcqep->parameter = IOERR_ABORT_REQUESTED;
18156e8e1c14SJames Smart 
18166e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
18176e8e1c14SJames Smart 	list_for_each_entry_safe(nvmewqeq, next_nvmewqeq,
18186e8e1c14SJames Smart 				 &wq->wqfull_list, list) {
18196e8e1c14SJames Smart 		if (ctxp) {
18206e8e1c14SJames Smart 			/* Checking for a specific IO to flush */
18216e8e1c14SJames Smart 			if (nvmewqeq->context2 == ctxp) {
18226e8e1c14SJames Smart 				list_del(&nvmewqeq->list);
18236e8e1c14SJames Smart 				spin_unlock_irqrestore(&pring->ring_lock,
18246e8e1c14SJames Smart 						       iflags);
18256e8e1c14SJames Smart 				lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq,
18266e8e1c14SJames Smart 							  wcqep);
18276e8e1c14SJames Smart 				return;
18286e8e1c14SJames Smart 			}
18296e8e1c14SJames Smart 			continue;
18306e8e1c14SJames Smart 		} else {
18316e8e1c14SJames Smart 			/* Flush all IOs */
18326e8e1c14SJames Smart 			list_del(&nvmewqeq->list);
18336e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
18346e8e1c14SJames Smart 			lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq, wcqep);
18356e8e1c14SJames Smart 			spin_lock_irqsave(&pring->ring_lock, iflags);
18366e8e1c14SJames Smart 		}
18376e8e1c14SJames Smart 	}
18386e8e1c14SJames Smart 	if (!ctxp)
18396e8e1c14SJames Smart 		wq->q_flag &= ~HBA_NVMET_WQFULL;
18406e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
18416e8e1c14SJames Smart }
18426e8e1c14SJames Smart 
18436e8e1c14SJames Smart void
18446e8e1c14SJames Smart lpfc_nvmet_wqfull_process(struct lpfc_hba *phba,
18456e8e1c14SJames Smart 			  struct lpfc_queue *wq)
18466e8e1c14SJames Smart {
18476e8e1c14SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
18486e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
18496e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
18501fbf9742SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
18516e8e1c14SJames Smart 	unsigned long iflags;
18526e8e1c14SJames Smart 	int rc;
18536e8e1c14SJames Smart 
18546e8e1c14SJames Smart 	/*
18556e8e1c14SJames Smart 	 * Some WQE slots are available, so try to re-issue anything
18566e8e1c14SJames Smart 	 * on the WQ wqfull_list.
18576e8e1c14SJames Smart 	 */
18586e8e1c14SJames Smart 	pring = wq->pring;
18596e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
18606e8e1c14SJames Smart 	while (!list_empty(&wq->wqfull_list)) {
18616e8e1c14SJames Smart 		list_remove_head(&wq->wqfull_list, nvmewqeq, struct lpfc_iocbq,
18626e8e1c14SJames Smart 				 list);
18636e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
18641fbf9742SJames Smart 		ctxp = (struct lpfc_nvmet_rcv_ctx *)nvmewqeq->context2;
18651fbf9742SJames Smart 		rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
18666e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
18676e8e1c14SJames Smart 		if (rc == -EBUSY) {
18686e8e1c14SJames Smart 			/* WQ was full again, so put it back on the list */
18696e8e1c14SJames Smart 			list_add(&nvmewqeq->list, &wq->wqfull_list);
18706e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
18716e8e1c14SJames Smart 			return;
18726e8e1c14SJames Smart 		}
187379d8c4ceSJames Smart 		if (rc == WQE_SUCCESS) {
187479d8c4ceSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
187579d8c4ceSJames Smart 			if (ctxp->ts_cmd_nvme) {
187679d8c4ceSJames Smart 				if (ctxp->ctx.fcp_req.op == NVMET_FCOP_RSP)
187779d8c4ceSJames Smart 					ctxp->ts_status_wqput = ktime_get_ns();
187879d8c4ceSJames Smart 				else
187979d8c4ceSJames Smart 					ctxp->ts_data_wqput = ktime_get_ns();
188079d8c4ceSJames Smart 			}
188179d8c4ceSJames Smart #endif
188279d8c4ceSJames Smart 		} else {
188379d8c4ceSJames Smart 			WARN_ON(rc);
188479d8c4ceSJames Smart 		}
18856e8e1c14SJames Smart 	}
18866e8e1c14SJames Smart 	wq->q_flag &= ~HBA_NVMET_WQFULL;
18876e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
18886e8e1c14SJames Smart 
18896e8e1c14SJames Smart #endif
18906e8e1c14SJames Smart }
18916e8e1c14SJames Smart 
1892d613b6a7SJames Smart void
1893d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
1894d613b6a7SJames Smart {
18957d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1896d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
18976e8e1c14SJames Smart 	struct lpfc_queue *wq;
18986e8e1c14SJames Smart 	uint32_t qidx;
1899c41f5988SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(tport_unreg_cmp);
1900d613b6a7SJames Smart 
1901d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
1902d613b6a7SJames Smart 		return;
1903d613b6a7SJames Smart 	if (phba->targetport) {
1904d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1905cdb42becSJames Smart 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
1906cdb42becSJames Smart 			wq = phba->sli4_hba.hdwq[qidx].nvme_wq;
19076e8e1c14SJames Smart 			lpfc_nvmet_wqfull_flush(phba, wq, NULL);
19086e8e1c14SJames Smart 		}
1909c41f5988SEwan D. Milne 		tgtp->tport_unreg_cmp = &tport_unreg_cmp;
1910d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
19112a0fb340SJames Smart 		if (!wait_for_completion_timeout(tgtp->tport_unreg_cmp,
19122a0fb340SJames Smart 					msecs_to_jiffies(LPFC_NVMET_WAIT_TMO)))
19132a0fb340SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
19142a0fb340SJames Smart 					"6179 Unreg targetport %p timeout "
19152a0fb340SJames Smart 					"reached.\n", phba->targetport);
19166c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
1917d613b6a7SJames Smart 	}
1918d613b6a7SJames Smart 	phba->targetport = NULL;
1919166d7211SJames Smart #endif
1920d613b6a7SJames Smart }
1921d613b6a7SJames Smart 
1922d613b6a7SJames Smart /**
1923d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer - Process an unsolicited event data buffer
1924d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
1925d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
1926d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
1927d613b6a7SJames Smart  *
1928d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
1929d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
1930d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
1931d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
1932d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
1933d613b6a7SJames Smart  * of the discovery state machine.
1934d613b6a7SJames Smart  **/
1935d613b6a7SJames Smart static void
1936d613b6a7SJames Smart lpfc_nvmet_unsol_ls_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1937d613b6a7SJames Smart 			   struct hbq_dmabuf *nvmebuf)
1938d613b6a7SJames Smart {
19397d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1940d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1941d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
1942d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1943d613b6a7SJames Smart 	uint32_t *payload;
1944d613b6a7SJames Smart 	uint32_t size, oxid, sid, rc;
1945d613b6a7SJames Smart 
194668c9b55dSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
194768c9b55dSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
194868c9b55dSJames Smart 
1949cc74e31dSColin Ian King 	if (!phba->targetport) {
1950d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
195168c9b55dSJames Smart 				"6154 LS Drop IO x%x\n", oxid);
19522b65e182SJames Smart 		oxid = 0;
19532b65e182SJames Smart 		size = 0;
19542b65e182SJames Smart 		sid = 0;
1955547077a4SJames Smart 		ctxp = NULL;
1956d613b6a7SJames Smart 		goto dropit;
1957d613b6a7SJames Smart 	}
1958d613b6a7SJames Smart 
1959d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1960d613b6a7SJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
1961d613b6a7SJames Smart 	size = bf_get(lpfc_rcqe_length,  &nvmebuf->cq_event.cqe.rcqe_cmpl);
1962d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
1963d613b6a7SJames Smart 
1964d613b6a7SJames Smart 	ctxp = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx), GFP_ATOMIC);
1965d613b6a7SJames Smart 	if (ctxp == NULL) {
1966d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_drop);
1967d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1968d613b6a7SJames Smart 				"6155 LS Drop IO x%x: Alloc\n",
1969d613b6a7SJames Smart 				oxid);
1970d613b6a7SJames Smart dropit:
19712b65e182SJames Smart 		lpfc_nvmeio_data(phba, "NVMET LS  DROP: "
19722b65e182SJames Smart 				 "xri x%x sz %d from %06x\n",
19732b65e182SJames Smart 				 oxid, size, sid);
1974d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1975d613b6a7SJames Smart 		return;
1976d613b6a7SJames Smart 	}
1977d613b6a7SJames Smart 	ctxp->phba = phba;
1978d613b6a7SJames Smart 	ctxp->size = size;
1979d613b6a7SJames Smart 	ctxp->oxid = oxid;
1980d613b6a7SJames Smart 	ctxp->sid = sid;
1981d613b6a7SJames Smart 	ctxp->wqeq = NULL;
1982ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_LS_RCV;
1983ce1b591cSJames Smart 	ctxp->entry_cnt = 1;
1984d613b6a7SJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
19851fbf9742SJames Smart 	ctxp->hdwq = &phba->sli4_hba.hdwq[0];
19862b65e182SJames Smart 
19872b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS   RCV: xri x%x sz %d from %06x\n",
19882b65e182SJames Smart 			 oxid, size, sid);
1989d613b6a7SJames Smart 	/*
1990d613b6a7SJames Smart 	 * The calling sequence should be:
1991d613b6a7SJames Smart 	 * nvmet_fc_rcv_ls_req -> lpfc_nvmet_xmt_ls_rsp/cmp ->_req->done
1992d613b6a7SJames Smart 	 * lpfc_nvmet_xmt_ls_rsp_cmp should free the allocated ctxp.
1993d613b6a7SJames Smart 	 */
1994d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
1995d613b6a7SJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, &ctxp->ctx.ls_req,
1996d613b6a7SJames Smart 				 payload, size);
1997d613b6a7SJames Smart 
1998d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1999ce1b591cSJames Smart 			"6037 NVMET Unsol rcv: sz %d rc %d: %08x %08x %08x "
2000ce1b591cSJames Smart 			"%08x %08x %08x\n", size, rc,
2001d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
2002d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
20032b65e182SJames Smart 
2004d613b6a7SJames Smart 	if (rc == 0) {
2005d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
2006d613b6a7SJames Smart 		return;
2007d613b6a7SJames Smart 	}
20082b65e182SJames Smart 
20092b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  DROP: xri x%x sz %d from %06x\n",
20102b65e182SJames Smart 			 oxid, size, sid);
20112b65e182SJames Smart 
2012d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
2013d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2014d613b6a7SJames Smart 			"6156 LS Drop IO x%x: nvmet_fc_rcv_ls_req %d\n",
2015d613b6a7SJames Smart 			ctxp->oxid, rc);
2016d613b6a7SJames Smart 
2017d613b6a7SJames Smart 	/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
2018d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
2019d613b6a7SJames Smart 
2020d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_ls_abort);
2021d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, sid, oxid);
2022166d7211SJames Smart #endif
2023d613b6a7SJames Smart }
2024d613b6a7SJames Smart 
2025472e146dSJames Smart static void
2026472e146dSJames Smart lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf)
2027472e146dSJames Smart {
2028472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2029472e146dSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
2030472e146dSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
2031472e146dSJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
2032472e146dSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
20334552e0f6SJames Smart 	uint32_t *payload, qno;
2034472e146dSJames Smart 	uint32_t rc;
2035472e146dSJames Smart 	unsigned long iflags;
2036472e146dSJames Smart 
2037472e146dSJames Smart 	if (!nvmebuf) {
2038472e146dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2039472e146dSJames Smart 			"6159 process_rcv_fcp_req, nvmebuf is NULL, "
2040472e146dSJames Smart 			"oxid: x%x flg: x%x state: x%x\n",
2041472e146dSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
2042472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
2043472e146dSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2044472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2045472e146dSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
2046472e146dSJames Smart 						 ctxp->oxid);
2047472e146dSJames Smart 		return;
2048472e146dSJames Smart 	}
2049472e146dSJames Smart 
205079d8c4ceSJames Smart 	if (ctxp->flag & LPFC_NVMET_ABTS_RCV) {
205179d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
205279d8c4ceSJames Smart 				"6324 IO oxid x%x aborted\n",
205379d8c4ceSJames Smart 				ctxp->oxid);
205479d8c4ceSJames Smart 		return;
205579d8c4ceSJames Smart 	}
205679d8c4ceSJames Smart 
2057472e146dSJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
2058472e146dSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
205979d8c4ceSJames Smart 	ctxp->flag |= LPFC_NVMET_TNOTIFY;
2060d74a89aaSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2061d74a89aaSJames Smart 	if (ctxp->ts_isr_cmd)
2062d74a89aaSJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
2063d74a89aaSJames Smart #endif
2064472e146dSJames Smart 	/*
2065472e146dSJames Smart 	 * The calling sequence should be:
2066472e146dSJames Smart 	 * nvmet_fc_rcv_fcp_req->lpfc_nvmet_xmt_fcp_op/cmp- req->done
2067472e146dSJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
2068472e146dSJames Smart 	 * When we return from nvmet_fc_rcv_fcp_req, all relevant info
2069472e146dSJames Smart 	 * the NVME command / FC header is stored.
2070472e146dSJames Smart 	 * A buffer has already been reposted for this IO, so just free
2071472e146dSJames Smart 	 * the nvmebuf.
2072472e146dSJames Smart 	 */
2073472e146dSJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->ctx.fcp_req,
2074472e146dSJames Smart 				  payload, ctxp->size);
2075472e146dSJames Smart 	/* Process FCP command */
2076472e146dSJames Smart 	if (rc == 0) {
2077472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
20784552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
20794552e0f6SJames Smart 		if ((ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) ||
20804552e0f6SJames Smart 		    (nvmebuf != ctxp->rqb_buffer)) {
20814552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
20824552e0f6SJames Smart 			return;
20834552e0f6SJames Smart 		}
20844552e0f6SJames Smart 		ctxp->rqb_buffer = NULL;
20854552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
20864552e0f6SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
2087472e146dSJames Smart 		return;
2088472e146dSJames Smart 	}
2089472e146dSJames Smart 
2090472e146dSJames Smart 	/* Processing of FCP command is deferred */
2091472e146dSJames Smart 	if (rc == -EOVERFLOW) {
2092472e146dSJames Smart 		lpfc_nvmeio_data(phba, "NVMET RCV BUSY: xri x%x sz %d "
2093472e146dSJames Smart 				 "from %06x\n",
2094472e146dSJames Smart 				 ctxp->oxid, ctxp->size, ctxp->sid);
2095472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
2096472e146dSJames Smart 		atomic_inc(&tgtp->defer_fod);
20974552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
20984552e0f6SJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) {
20994552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
21004552e0f6SJames Smart 			return;
21014552e0f6SJames Smart 		}
21024552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
21034552e0f6SJames Smart 		/*
21044552e0f6SJames Smart 		 * Post a replacement DMA buffer to RQ and defer
21054552e0f6SJames Smart 		 * freeing rcv buffer till .defer_rcv callback
21064552e0f6SJames Smart 		 */
21074552e0f6SJames Smart 		qno = nvmebuf->idx;
21084552e0f6SJames Smart 		lpfc_post_rq_buffer(
21094552e0f6SJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
21104552e0f6SJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2111472e146dSJames Smart 		return;
2112472e146dSJames Smart 	}
211379d8c4ceSJames Smart 	ctxp->flag &= ~LPFC_NVMET_TNOTIFY;
2114472e146dSJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2115472e146dSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2116472e146dSJames Smart 			"2582 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
2117472e146dSJames Smart 			ctxp->oxid, rc,
2118472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_in),
2119472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_out),
2120472e146dSJames Smart 			atomic_read(&tgtp->xmt_fcp_release));
2121472e146dSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
2122472e146dSJames Smart 			 ctxp->oxid, ctxp->size, ctxp->sid);
2123472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflags);
2124472e146dSJames Smart 	lpfc_nvmet_defer_release(phba, ctxp);
2125472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2126472e146dSJames Smart 	lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
2127472e146dSJames Smart #endif
2128472e146dSJames Smart }
2129472e146dSJames Smart 
2130472e146dSJames Smart static void
2131472e146dSJames Smart lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *work)
2132472e146dSJames Smart {
2133472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2134472e146dSJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf =
2135472e146dSJames Smart 		container_of(work, struct lpfc_nvmet_ctxbuf, defer_work);
2136472e146dSJames Smart 
2137472e146dSJames Smart 	lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2138472e146dSJames Smart #endif
2139472e146dSJames Smart }
2140472e146dSJames Smart 
214166d7ce93SDick Kennedy static struct lpfc_nvmet_ctxbuf *
214266d7ce93SDick Kennedy lpfc_nvmet_replenish_context(struct lpfc_hba *phba,
214366d7ce93SDick Kennedy 			     struct lpfc_nvmet_ctx_info *current_infop)
214466d7ce93SDick Kennedy {
21452299e432SDick Kennedy #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
214666d7ce93SDick Kennedy 	struct lpfc_nvmet_ctxbuf *ctx_buf = NULL;
214766d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *get_infop;
214866d7ce93SDick Kennedy 	int i;
214966d7ce93SDick Kennedy 
215066d7ce93SDick Kennedy 	/*
215166d7ce93SDick Kennedy 	 * The current_infop for the MRQ a NVME command IU was received
215266d7ce93SDick Kennedy 	 * on is empty. Our goal is to replenish this MRQs context
215366d7ce93SDick Kennedy 	 * list from a another CPUs.
215466d7ce93SDick Kennedy 	 *
215566d7ce93SDick Kennedy 	 * First we need to pick a context list to start looking on.
215666d7ce93SDick Kennedy 	 * nvmet_ctx_start_cpu has available context the last time
215766d7ce93SDick Kennedy 	 * we needed to replenish this CPU where nvmet_ctx_next_cpu
215866d7ce93SDick Kennedy 	 * is just the next sequential CPU for this MRQ.
215966d7ce93SDick Kennedy 	 */
216066d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_start_cpu)
216166d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_start_cpu;
216266d7ce93SDick Kennedy 	else
216366d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_next_cpu;
216466d7ce93SDick Kennedy 
2165222e9239SJames Smart 	for (i = 0; i < phba->sli4_hba.num_possible_cpu; i++) {
216666d7ce93SDick Kennedy 		if (get_infop == current_infop) {
216766d7ce93SDick Kennedy 			get_infop = get_infop->nvmet_ctx_next_cpu;
216866d7ce93SDick Kennedy 			continue;
216966d7ce93SDick Kennedy 		}
217066d7ce93SDick Kennedy 		spin_lock(&get_infop->nvmet_ctx_list_lock);
217166d7ce93SDick Kennedy 
217266d7ce93SDick Kennedy 		/* Just take the entire context list, if there are any */
217366d7ce93SDick Kennedy 		if (get_infop->nvmet_ctx_list_cnt) {
217466d7ce93SDick Kennedy 			list_splice_init(&get_infop->nvmet_ctx_list,
217566d7ce93SDick Kennedy 				    &current_infop->nvmet_ctx_list);
217666d7ce93SDick Kennedy 			current_infop->nvmet_ctx_list_cnt =
217766d7ce93SDick Kennedy 				get_infop->nvmet_ctx_list_cnt - 1;
217866d7ce93SDick Kennedy 			get_infop->nvmet_ctx_list_cnt = 0;
217966d7ce93SDick Kennedy 			spin_unlock(&get_infop->nvmet_ctx_list_lock);
218066d7ce93SDick Kennedy 
218166d7ce93SDick Kennedy 			current_infop->nvmet_ctx_start_cpu = get_infop;
218266d7ce93SDick Kennedy 			list_remove_head(&current_infop->nvmet_ctx_list,
218366d7ce93SDick Kennedy 					 ctx_buf, struct lpfc_nvmet_ctxbuf,
218466d7ce93SDick Kennedy 					 list);
218566d7ce93SDick Kennedy 			return ctx_buf;
218666d7ce93SDick Kennedy 		}
218766d7ce93SDick Kennedy 
218866d7ce93SDick Kennedy 		/* Otherwise, move on to the next CPU for this MRQ */
218966d7ce93SDick Kennedy 		spin_unlock(&get_infop->nvmet_ctx_list_lock);
219066d7ce93SDick Kennedy 		get_infop = get_infop->nvmet_ctx_next_cpu;
219166d7ce93SDick Kennedy 	}
219266d7ce93SDick Kennedy 
21932299e432SDick Kennedy #endif
219466d7ce93SDick Kennedy 	/* Nothing found, all contexts for the MRQ are in-flight */
219566d7ce93SDick Kennedy 	return NULL;
219666d7ce93SDick Kennedy }
219766d7ce93SDick Kennedy 
2198d613b6a7SJames Smart /**
2199d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
2200d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
220166d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2202d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
2203d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2204d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2205d613b6a7SJames Smart  *
2206d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
2207d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
2208d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
2209d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
2210d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
2211d613b6a7SJames Smart  * of the discovery state machine.
2212d613b6a7SJames Smart  **/
2213d613b6a7SJames Smart static void
2214d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
221566d7ce93SDick Kennedy 			    uint32_t idx,
2216d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
2217d74a89aaSJames Smart 			    uint64_t isr_timestamp,
2218d74a89aaSJames Smart 			    uint8_t cqflag)
2219d613b6a7SJames Smart {
2220d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
2221d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2222d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
22236c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
222466d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *current_infop;
2225472e146dSJames Smart 	uint32_t size, oxid, sid, qno;
22266c621a22SJames Smart 	unsigned long iflag;
222766d7ce93SDick Kennedy 	int current_cpu;
2228d613b6a7SJames Smart 
222973626173SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_TARGET_FC))
223073626173SArnd Bergmann 		return;
223173626173SArnd Bergmann 
22326c621a22SJames Smart 	ctx_buf = NULL;
2233d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
2234d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
22356c621a22SJames Smart 				"6157 NVMET FCP Drop IO\n");
2236472e146dSJames Smart 		if (nvmebuf)
2237472e146dSJames Smart 			lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2238472e146dSJames Smart 		return;
2239d613b6a7SJames Smart 	}
2240d613b6a7SJames Smart 
224166d7ce93SDick Kennedy 	/*
224266d7ce93SDick Kennedy 	 * Get a pointer to the context list for this MRQ based on
224366d7ce93SDick Kennedy 	 * the CPU this MRQ IRQ is associated with. If the CPU association
224466d7ce93SDick Kennedy 	 * changes from our initial assumption, the context list could
224566d7ce93SDick Kennedy 	 * be empty, thus it would need to be replenished with the
224666d7ce93SDick Kennedy 	 * context list from another CPU for this MRQ.
224766d7ce93SDick Kennedy 	 */
2248d6d189ceSBart Van Assche 	current_cpu = raw_smp_processor_id();
224966d7ce93SDick Kennedy 	current_infop = lpfc_get_ctx_list(phba, current_cpu, idx);
225066d7ce93SDick Kennedy 	spin_lock_irqsave(&current_infop->nvmet_ctx_list_lock, iflag);
225166d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_list_cnt) {
225266d7ce93SDick Kennedy 		list_remove_head(&current_infop->nvmet_ctx_list,
22536c621a22SJames Smart 				 ctx_buf, struct lpfc_nvmet_ctxbuf, list);
225466d7ce93SDick Kennedy 		current_infop->nvmet_ctx_list_cnt--;
2255966bb5b7SJames Smart 	} else {
225666d7ce93SDick Kennedy 		ctx_buf = lpfc_nvmet_replenish_context(phba, current_infop);
22576c621a22SJames Smart 	}
225866d7ce93SDick Kennedy 	spin_unlock_irqrestore(&current_infop->nvmet_ctx_list_lock, iflag);
22596c621a22SJames Smart 
2260a8cf5dfeSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
2261a8cf5dfeSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2262a8cf5dfeSJames Smart 	size = nvmebuf->bytes_recv;
2263a8cf5dfeSJames Smart 
2264a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2265a8cf5dfeSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_RCV) {
226663df6d63SJames Smart 		if (current_cpu < LPFC_CHECK_CPU_CNT) {
226763df6d63SJames Smart 			if (idx != current_cpu)
226863df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
226963df6d63SJames Smart 						"6703 CPU Check rcv: "
227063df6d63SJames Smart 						"cpu %d expect %d\n",
227163df6d63SJames Smart 						current_cpu, idx);
227263df6d63SJames Smart 			phba->sli4_hba.hdwq[idx].cpucheck_rcv_io[current_cpu]++;
227363df6d63SJames Smart 		}
2274a8cf5dfeSJames Smart 	}
2275a8cf5dfeSJames Smart #endif
2276a8cf5dfeSJames Smart 
2277a8cf5dfeSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d CPU %02x\n",
2278d6d189ceSBart Van Assche 			 oxid, size, raw_smp_processor_id());
2279a8cf5dfeSJames Smart 
2280411de511SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2281411de511SJames Smart 
22826c621a22SJames Smart 	if (!ctx_buf) {
2283a8cf5dfeSJames Smart 		/* Queue this NVME IO to process later */
2284a8cf5dfeSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
2285a8cf5dfeSJames Smart 		list_add_tail(&nvmebuf->hbuf.list,
2286a8cf5dfeSJames Smart 			      &phba->sli4_hba.lpfc_nvmet_io_wait_list);
2287a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt++;
2288a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_total++;
2289a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
2290a8cf5dfeSJames Smart 				       iflag);
2291a8cf5dfeSJames Smart 
2292a8cf5dfeSJames Smart 		/* Post a brand new DMA buffer to RQ */
2293a8cf5dfeSJames Smart 		qno = nvmebuf->idx;
2294a8cf5dfeSJames Smart 		lpfc_post_rq_buffer(
2295a8cf5dfeSJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
2296a8cf5dfeSJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2297411de511SJames Smart 
2298411de511SJames Smart 		atomic_inc(&tgtp->defer_ctx);
2299a8cf5dfeSJames Smart 		return;
23006c621a22SJames Smart 	}
2301d613b6a7SJames Smart 
2302d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
2303d613b6a7SJames Smart 
23046c621a22SJames Smart 	ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
230579d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
230679d8c4ceSJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.t_active_ctx_list);
230779d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
2308ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_FREE) {
2309ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2310ce1b591cSJames Smart 				"6414 NVMET Context corrupt %d %d oxid x%x\n",
2311ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
2312ce1b591cSJames Smart 	}
2313d613b6a7SJames Smart 	ctxp->wqeq = NULL;
2314d613b6a7SJames Smart 	ctxp->txrdy = NULL;
2315d613b6a7SJames Smart 	ctxp->offset = 0;
2316d613b6a7SJames Smart 	ctxp->phba = phba;
2317d613b6a7SJames Smart 	ctxp->size = size;
2318d613b6a7SJames Smart 	ctxp->oxid = oxid;
2319d613b6a7SJames Smart 	ctxp->sid = sid;
232066d7ce93SDick Kennedy 	ctxp->idx = idx;
2321d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_RCV;
2322d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
2323d613b6a7SJames Smart 	ctxp->flag = 0;
23246c621a22SJames Smart 	ctxp->ctxbuf = ctx_buf;
2325cbc5de1bSJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
23261fbf9742SJames Smart 	ctxp->hdwq = NULL;
23272b7824d0SJames Smart 	spin_lock_init(&ctxp->ctxlock);
2328d613b6a7SJames Smart 
23292b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2330d74a89aaSJames Smart 	if (isr_timestamp)
23312b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
2332d74a89aaSJames Smart 	ctxp->ts_cmd_nvme = 0;
23332b65e182SJames Smart 	ctxp->ts_nvme_data = 0;
23342b65e182SJames Smart 	ctxp->ts_data_wqput = 0;
23352b65e182SJames Smart 	ctxp->ts_isr_data = 0;
23362b65e182SJames Smart 	ctxp->ts_data_nvme = 0;
23372b65e182SJames Smart 	ctxp->ts_nvme_status = 0;
23382b65e182SJames Smart 	ctxp->ts_status_wqput = 0;
23392b65e182SJames Smart 	ctxp->ts_isr_status = 0;
23402b65e182SJames Smart 	ctxp->ts_status_nvme = 0;
23412b65e182SJames Smart #endif
23422b65e182SJames Smart 
2343d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
2344d74a89aaSJames Smart 	/* check for cq processing load */
2345d74a89aaSJames Smart 	if (!cqflag) {
2346472e146dSJames Smart 		lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2347d74a89aaSJames Smart 		return;
2348d74a89aaSJames Smart 	}
2349d74a89aaSJames Smart 
2350d74a89aaSJames Smart 	if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
2351d74a89aaSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2352d74a89aaSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
2353d74a89aaSJames Smart 				"6325 Unable to queue work for oxid x%x. "
2354d74a89aaSJames Smart 				"FCP Drop IO [x%x x%x x%x]\n",
2355d74a89aaSJames Smart 				ctxp->oxid,
2356d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_in),
2357d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_out),
2358d74a89aaSJames Smart 				atomic_read(&tgtp->xmt_fcp_release));
2359d74a89aaSJames Smart 
2360d74a89aaSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
2361d74a89aaSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2362d74a89aaSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
2363d74a89aaSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
2364d74a89aaSJames Smart 	}
2365d613b6a7SJames Smart }
2366d613b6a7SJames Smart 
2367d613b6a7SJames Smart /**
2368d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_event - Process an unsolicited event from an nvme nport
2369d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
2370d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
2371d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2372d613b6a7SJames Smart  *
2373d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2374d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2375d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2376d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer() after properly set up the buffer from the
2377d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2378d613b6a7SJames Smart  **/
2379d613b6a7SJames Smart void
2380d613b6a7SJames Smart lpfc_nvmet_unsol_ls_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2381d613b6a7SJames Smart 			  struct lpfc_iocbq *piocb)
2382d613b6a7SJames Smart {
2383d613b6a7SJames Smart 	struct lpfc_dmabuf *d_buf;
2384d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf;
2385d613b6a7SJames Smart 
2386d613b6a7SJames Smart 	d_buf = piocb->context2;
2387d613b6a7SJames Smart 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2388d613b6a7SJames Smart 
2389d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
2390d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
2391d613b6a7SJames Smart 		return;
2392d613b6a7SJames Smart 	}
2393d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_buffer(phba, pring, nvmebuf);
2394d613b6a7SJames Smart }
2395d613b6a7SJames Smart 
2396d613b6a7SJames Smart /**
2397d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
2398d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
239966d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2400d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2401d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2402d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2403d613b6a7SJames Smart  *
2404d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2405d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2406d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2407d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
2408d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2409d613b6a7SJames Smart  **/
2410d613b6a7SJames Smart void
2411d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
241266d7ce93SDick Kennedy 			   uint32_t idx,
2413d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
2414d74a89aaSJames Smart 			   uint64_t isr_timestamp,
2415d74a89aaSJames Smart 			   uint8_t cqflag)
2416d613b6a7SJames Smart {
2417d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
24186c621a22SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2419d613b6a7SJames Smart 		return;
2420d613b6a7SJames Smart 	}
2421d74a89aaSJames Smart 	lpfc_nvmet_unsol_fcp_buffer(phba, idx, nvmebuf, isr_timestamp, cqflag);
2422d613b6a7SJames Smart }
2423d613b6a7SJames Smart 
2424d613b6a7SJames Smart /**
2425d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
2426d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
2427d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
2428d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
2429d613b6a7SJames Smart  * @rspsize: size of the NVME command.
2430d613b6a7SJames Smart  *
2431d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
2432d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
2433d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
2434d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
2435d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
2436d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
2437d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
2438d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
2439d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
2440d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
2441d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
2442d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
2443d613b6a7SJames Smart  * reference for the command's callback function to access later.
2444d613b6a7SJames Smart  *
2445d613b6a7SJames Smart  * Return code
2446d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
2447d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
2448d613b6a7SJames Smart  **/
2449d613b6a7SJames Smart static struct lpfc_iocbq *
2450d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
2451d613b6a7SJames Smart 		       struct lpfc_nvmet_rcv_ctx *ctxp,
2452d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
2453d613b6a7SJames Smart {
2454d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2455d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2456205e8240SJames Smart 	union lpfc_wqe128 *wqe;
2457d613b6a7SJames Smart 
2458d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2459d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2460ce1b591cSJames Smart 				"6104 NVMET prep LS wqe: link err: "
2461ce1b591cSJames Smart 				"NPORT x%x oxid:x%x ste %d\n",
2462ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2463d613b6a7SJames Smart 		return NULL;
2464d613b6a7SJames Smart 	}
2465d613b6a7SJames Smart 
2466d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
2467d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
2468d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2469d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2470ce1b591cSJames Smart 				"6105 NVMET prep LS wqe: No WQE: "
2471ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2472ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2473d613b6a7SJames Smart 		return NULL;
2474d613b6a7SJames Smart 	}
2475d613b6a7SJames Smart 
2476d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2477d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2478d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2479d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2480d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2481ce1b591cSJames Smart 				"6106 NVMET prep LS wqe: No ndlp: "
2482ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2483ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2484d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2485d613b6a7SJames Smart 	}
2486d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
2487d613b6a7SJames Smart 
2488d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
2489d613b6a7SJames Smart 	nvmewqe->context1 = lpfc_nlp_get(ndlp);
2490d613b6a7SJames Smart 	if (nvmewqe->context1 == NULL)
2491d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2492d613b6a7SJames Smart 	nvmewqe->context2 = ctxp;
2493d613b6a7SJames Smart 
2494d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
2495d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
2496d613b6a7SJames Smart 
2497d613b6a7SJames Smart 	/* Words 0 - 2 */
2498d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2499d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
2500d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
2501d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
2502d613b6a7SJames Smart 
2503d613b6a7SJames Smart 	/* Word 3 */
2504d613b6a7SJames Smart 
2505d613b6a7SJames Smart 	/* Word 4 */
2506d613b6a7SJames Smart 
2507d613b6a7SJames Smart 	/* Word 5 */
2508d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
2509d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
2510d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
25118b361639SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
2512d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
2513d613b6a7SJames Smart 
2514d613b6a7SJames Smart 	/* Word 6 */
2515d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
2516d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2517d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
2518d613b6a7SJames Smart 
2519d613b6a7SJames Smart 	/* Word 7 */
2520d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
2521d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
2522d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
2523d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
2524d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
2525d613b6a7SJames Smart 
2526d613b6a7SJames Smart 	/* Word 8 */
2527d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
2528d613b6a7SJames Smart 
2529d613b6a7SJames Smart 	/* Word 9 */
2530d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
2531d613b6a7SJames Smart 	/* Needs to be set by caller */
2532d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
2533d613b6a7SJames Smart 
2534d613b6a7SJames Smart 	/* Word 10 */
2535d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
2536d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2537d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
2538d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
2539d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
2540d613b6a7SJames Smart 
2541d613b6a7SJames Smart 	/* Word 11 */
2542d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
2543d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
2544d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
2545d613b6a7SJames Smart 	       OTHER_COMMAND);
2546d613b6a7SJames Smart 
2547d613b6a7SJames Smart 	/* Word 12 */
2548d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
2549d613b6a7SJames Smart 
2550d613b6a7SJames Smart 	nvmewqe->retry = 1;
2551d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2552d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2553d613b6a7SJames Smart 	nvmewqe->iocb_flag |= LPFC_IO_NVME_LS;
2554d613b6a7SJames Smart 
2555ce1b591cSJames Smart 	/* Xmit NVMET response to remote NPORT <did> */
2556d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2557ce1b591cSJames Smart 			"6039 Xmit NVMET LS response to remote "
2558d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
2559d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
2560d613b6a7SJames Smart 			rspsize);
2561d613b6a7SJames Smart 	return nvmewqe;
2562d613b6a7SJames Smart 
2563d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
2564d613b6a7SJames Smart 	nvmewqe->context2 = NULL;
2565d613b6a7SJames Smart 	nvmewqe->context3 = NULL;
2566d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
2567d613b6a7SJames Smart 	return NULL;
2568d613b6a7SJames Smart }
2569d613b6a7SJames Smart 
2570d613b6a7SJames Smart 
2571d613b6a7SJames Smart static struct lpfc_iocbq *
2572d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
2573d613b6a7SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
2574d613b6a7SJames Smart {
2575d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->ctx.fcp_req;
2576d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2577d613b6a7SJames Smart 	struct sli4_sge *sgl;
2578d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2579d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2580d613b6a7SJames Smart 	struct scatterlist *sgel;
2581d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
25820bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
2583d613b6a7SJames Smart 	uint32_t *txrdy;
2584d613b6a7SJames Smart 	dma_addr_t physaddr;
2585d613b6a7SJames Smart 	int i, cnt;
25860bc2b7c5SJames Smart 	int do_pbde;
2587d613b6a7SJames Smart 	int xc = 1;
2588d613b6a7SJames Smart 
2589d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2590d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2591ce1b591cSJames Smart 				"6107 NVMET prep FCP wqe: link err:"
2592ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2593ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2594d613b6a7SJames Smart 		return NULL;
2595d613b6a7SJames Smart 	}
2596d613b6a7SJames Smart 
2597d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2598d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2599d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2600d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2601d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2602ce1b591cSJames Smart 				"6108 NVMET prep FCP wqe: no ndlp: "
2603ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2604ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2605d613b6a7SJames Smart 		return NULL;
2606d613b6a7SJames Smart 	}
2607d613b6a7SJames Smart 
260881e6a637SJames Smart 	if (rsp->sg_cnt > lpfc_tgttemplate.max_sgl_segments) {
2609d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2610ce1b591cSJames Smart 				"6109 NVMET prep FCP wqe: seg cnt err: "
2611ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d cnt %d\n",
2612ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state,
2613ce1b591cSJames Smart 				phba->cfg_nvme_seg_cnt);
2614d613b6a7SJames Smart 		return NULL;
2615d613b6a7SJames Smart 	}
2616d613b6a7SJames Smart 
2617d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2618d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
2619d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2620d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
26216c621a22SJames Smart 		nvmewqe = ctxp->ctxbuf->iocbq;
2622d613b6a7SJames Smart 		if (nvmewqe == NULL) {
2623d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2624ce1b591cSJames Smart 					"6110 NVMET prep FCP wqe: No "
2625ce1b591cSJames Smart 					"WQE: NPORT x%x oxid x%x ste %d\n",
2626ce1b591cSJames Smart 					ctxp->sid, ctxp->oxid, ctxp->state);
2627d613b6a7SJames Smart 			return NULL;
2628d613b6a7SJames Smart 		}
2629d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
2630d613b6a7SJames Smart 		xc = 0; /* create new XRI */
2631d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
2632d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
2633d613b6a7SJames Smart 	}
2634d613b6a7SJames Smart 
2635d613b6a7SJames Smart 	/* Sanity check */
2636d613b6a7SJames Smart 	if (((ctxp->state == LPFC_NVMET_STE_RCV) &&
2637d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
2638ce1b591cSJames Smart 	    (ctxp->state == LPFC_NVMET_STE_DATA)) {
2639205e8240SJames Smart 		wqe = &nvmewqe->wqe;
2640d613b6a7SJames Smart 	} else {
2641d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2642ce1b591cSJames Smart 				"6111 Wrong state NVMET FCP: %d  cnt %d\n",
2643ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt);
2644d613b6a7SJames Smart 		return NULL;
2645d613b6a7SJames Smart 	}
2646d613b6a7SJames Smart 
26476c621a22SJames Smart 	sgl  = (struct sli4_sge *)ctxp->ctxbuf->sglq->sgl;
2648d613b6a7SJames Smart 	switch (rsp->op) {
2649d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
2650d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
2651bd3061baSJames Smart 		/* From the tsend template, initialize words 7 - 11 */
2652bd3061baSJames Smart 		memcpy(&wqe->words[7],
2653bd3061baSJames Smart 		       &lpfc_tsend_cmd_template.words[7],
2654bd3061baSJames Smart 		       sizeof(uint32_t) * 5);
2655bd3061baSJames Smart 
2656d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2657d613b6a7SJames Smart 		sgel = &rsp->sg[0];
2658d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2659d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2660d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
2661d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
2662d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
2663d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2664d613b6a7SJames Smart 
2665d613b6a7SJames Smart 		/* Word 3 */
2666d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
2667d613b6a7SJames Smart 
2668d613b6a7SJames Smart 		/* Word 4 */
2669d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
2670d613b6a7SJames Smart 
2671d613b6a7SJames Smart 		/* Word 5 */
2672bd3061baSJames Smart 		wqe->fcp_tsend.reserved = 0;
2673d613b6a7SJames Smart 
2674d613b6a7SJames Smart 		/* Word 6 */
2675d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
2676d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2677d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
2678d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2679d613b6a7SJames Smart 
2680bd3061baSJames Smart 		/* Word 7 - set ar later */
2681d613b6a7SJames Smart 
2682d613b6a7SJames Smart 		/* Word 8 */
2683d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
2684d613b6a7SJames Smart 
2685d613b6a7SJames Smart 		/* Word 9 */
2686d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
2687d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
2688d613b6a7SJames Smart 
2689bd3061baSJames Smart 		/* Word 10 - set wqes later, in template xc=1 */
2690bd3061baSJames Smart 		if (!xc)
2691bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 0);
2692d613b6a7SJames Smart 
2693bd3061baSJames Smart 		/* Word 11 - set sup, irsp, irsplen later */
2694bd3061baSJames Smart 		do_pbde = 0;
2695d613b6a7SJames Smart 
2696d613b6a7SJames Smart 		/* Word 12 */
2697d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2698d613b6a7SJames Smart 
2699d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
2700d613b6a7SJames Smart 		sgl->addr_hi = 0;
2701d613b6a7SJames Smart 		sgl->addr_lo = 0;
2702d613b6a7SJames Smart 		sgl->word2 = 0;
2703d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2704d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2705d613b6a7SJames Smart 		sgl->sge_len = 0;
2706d613b6a7SJames Smart 		sgl++;
2707d613b6a7SJames Smart 		sgl->addr_hi = 0;
2708d613b6a7SJames Smart 		sgl->addr_lo = 0;
2709d613b6a7SJames Smart 		sgl->word2 = 0;
2710d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2711d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2712d613b6a7SJames Smart 		sgl->sge_len = 0;
2713d613b6a7SJames Smart 		sgl++;
2714d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
2715d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
2716bd3061baSJames Smart 
2717bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2718bd3061baSJames Smart 
271920aefac3SJames Smart 			if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
272020aefac3SJames Smart 				if (ndlp->nlp_flag & NLP_SUPPRESS_RSP)
272120aefac3SJames Smart 					bf_set(wqe_sup,
272220aefac3SJames Smart 					       &wqe->fcp_tsend.wqe_com, 1);
2723d613b6a7SJames Smart 			} else {
2724d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
2725d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
2726d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
2727d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
2728d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
2729d613b6a7SJames Smart 				       rsp->rsplen);
2730d613b6a7SJames Smart 			}
2731d613b6a7SJames Smart 		} else {
2732d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
2733d613b6a7SJames Smart 
2734bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2735d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
2736d613b6a7SJames Smart 		}
2737d613b6a7SJames Smart 		break;
2738d613b6a7SJames Smart 
2739d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
2740bd3061baSJames Smart 		/* From the treceive template, initialize words 3 - 11 */
2741bd3061baSJames Smart 		memcpy(&wqe->words[3],
2742bd3061baSJames Smart 		       &lpfc_treceive_cmd_template.words[3],
2743bd3061baSJames Smart 		       sizeof(uint32_t) * 9);
2744bd3061baSJames Smart 
2745d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2746771db5c0SRomain Perier 		txrdy = dma_pool_alloc(phba->txrdy_payload_pool,
2747d613b6a7SJames Smart 				       GFP_KERNEL, &physaddr);
2748d613b6a7SJames Smart 		if (!txrdy) {
2749d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2750d613b6a7SJames Smart 					"6041 Bad txrdy buffer: oxid x%x\n",
2751d613b6a7SJames Smart 					ctxp->oxid);
2752d613b6a7SJames Smart 			return NULL;
2753d613b6a7SJames Smart 		}
2754d613b6a7SJames Smart 		ctxp->txrdy = txrdy;
2755d613b6a7SJames Smart 		ctxp->txrdy_phys = physaddr;
2756d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2757d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = TXRDY_PAYLOAD_LEN;
2758d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrLow =
2759d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2760d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrHigh =
2761d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2762d613b6a7SJames Smart 
2763d613b6a7SJames Smart 		/* Word 4 */
2764d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
2765d613b6a7SJames Smart 
2766d613b6a7SJames Smart 		/* Word 6 */
2767d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
2768d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2769d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
2770d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2771d613b6a7SJames Smart 
2772d613b6a7SJames Smart 		/* Word 7 */
2773d613b6a7SJames Smart 
2774d613b6a7SJames Smart 		/* Word 8 */
2775d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
2776d613b6a7SJames Smart 
2777d613b6a7SJames Smart 		/* Word 9 */
2778d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
2779d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
2780d613b6a7SJames Smart 
2781bd3061baSJames Smart 		/* Word 10 - in template xc=1 */
2782bd3061baSJames Smart 		if (!xc)
2783bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, 0);
2784d613b6a7SJames Smart 
2785bd3061baSJames Smart 		/* Word 11 - set pbde later */
2786414abe0aSJames Smart 		if (phba->cfg_enable_pbde) {
2787bd3061baSJames Smart 			do_pbde = 1;
2788bd3061baSJames Smart 		} else {
2789bd3061baSJames Smart 			bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 0);
2790bd3061baSJames Smart 			do_pbde = 0;
2791bd3061baSJames Smart 		}
2792d613b6a7SJames Smart 
2793d613b6a7SJames Smart 		/* Word 12 */
2794d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2795d613b6a7SJames Smart 
2796d613b6a7SJames Smart 		/* Setup 1 TXRDY and 1 SKIP SGE */
2797d613b6a7SJames Smart 		txrdy[0] = 0;
2798d613b6a7SJames Smart 		txrdy[1] = cpu_to_be32(rsp->transfer_length);
2799d613b6a7SJames Smart 		txrdy[2] = 0;
2800d613b6a7SJames Smart 
2801d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
2802d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
2803d613b6a7SJames Smart 		sgl->word2 = 0;
2804d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2805d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2806d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(TXRDY_PAYLOAD_LEN);
2807d613b6a7SJames Smart 		sgl++;
2808d613b6a7SJames Smart 		sgl->addr_hi = 0;
2809d613b6a7SJames Smart 		sgl->addr_lo = 0;
2810d613b6a7SJames Smart 		sgl->word2 = 0;
2811d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2812d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2813d613b6a7SJames Smart 		sgl->sge_len = 0;
2814d613b6a7SJames Smart 		sgl++;
2815d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
2816d613b6a7SJames Smart 		break;
2817d613b6a7SJames Smart 
2818d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
2819bd3061baSJames Smart 		/* From the treceive template, initialize words 4 - 11 */
2820bd3061baSJames Smart 		memcpy(&wqe->words[4],
2821bd3061baSJames Smart 		       &lpfc_trsp_cmd_template.words[4],
2822bd3061baSJames Smart 		       sizeof(uint32_t) * 8);
2823bd3061baSJames Smart 
2824d613b6a7SJames Smart 		/* Words 0 - 2 */
2825d613b6a7SJames Smart 		physaddr = rsp->rspdma;
2826d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2827d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
2828d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
2829d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2830d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
2831d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2832d613b6a7SJames Smart 
2833d613b6a7SJames Smart 		/* Word 3 */
2834d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
2835d613b6a7SJames Smart 
2836d613b6a7SJames Smart 		/* Word 6 */
2837d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
2838d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2839d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
2840d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2841d613b6a7SJames Smart 
2842d613b6a7SJames Smart 		/* Word 7 */
2843d613b6a7SJames Smart 
2844d613b6a7SJames Smart 		/* Word 8 */
2845d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
2846d613b6a7SJames Smart 
2847d613b6a7SJames Smart 		/* Word 9 */
2848d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
2849d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
2850d613b6a7SJames Smart 
2851d613b6a7SJames Smart 		/* Word 10 */
2852bd3061baSJames Smart 		if (xc)
2853bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 1);
2854d613b6a7SJames Smart 
2855d613b6a7SJames Smart 		/* Word 11 */
2856bd3061baSJames Smart 		/* In template wqes=0 irsp=0 irsplen=0 - good response */
2857bd3061baSJames Smart 		if (rsp->rsplen != LPFC_NVMET_SUCCESS_LEN) {
2858bd3061baSJames Smart 			/* Bad response - embed it */
2859d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
2860d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
2861d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
2862d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
2863d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
2864d613b6a7SJames Smart 		}
2865bd3061baSJames Smart 		do_pbde = 0;
2866bd3061baSJames Smart 
2867bd3061baSJames Smart 		/* Word 12 */
2868bd3061baSJames Smart 		wqe->fcp_trsp.rsvd_12_15[0] = 0;
2869d613b6a7SJames Smart 
2870d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
2871d613b6a7SJames Smart 		rsp->sg_cnt = 0;
2872d613b6a7SJames Smart 		sgl->word2 = 0;
2873d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
2874d613b6a7SJames Smart 		break;
2875d613b6a7SJames Smart 
2876d613b6a7SJames Smart 	default:
2877d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2878d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
2879d613b6a7SJames Smart 				rsp->op);
2880d613b6a7SJames Smart 		return NULL;
2881d613b6a7SJames Smart 	}
2882d613b6a7SJames Smart 
2883d613b6a7SJames Smart 	nvmewqe->retry = 1;
2884d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2885d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2886d613b6a7SJames Smart 	nvmewqe->context1 = ndlp;
2887d613b6a7SJames Smart 
2888d613b6a7SJames Smart 	for (i = 0; i < rsp->sg_cnt; i++) {
2889d613b6a7SJames Smart 		sgel = &rsp->sg[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,
3098d613b6a7SJames Smart 			"6083 Abort cmpl: ctx %p 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;
3228205e8240SJames Smart 	union lpfc_wqe128 *abts_wqe;
3229d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3230d613b6a7SJames Smart 	unsigned long flags;
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 	abts_wqe = &abts_wqeq->wqe;
3270d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
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 */
3284d613b6a7SJames Smart 	if (phba->hba_flag & HBA_NVME_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 
3316d613b6a7SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
3317d613b6a7SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
3318d613b6a7SJames Smart 	 */
3319d613b6a7SJames Smart 	memset(abts_wqe, 0, sizeof(union lpfc_wqe));
3320d613b6a7SJames Smart 
3321d613b6a7SJames Smart 	/* word 3 */
3322d613b6a7SJames Smart 	bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
3323d613b6a7SJames Smart 
3324d613b6a7SJames Smart 	/* word 7 */
3325d613b6a7SJames Smart 	bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
3326d613b6a7SJames Smart 	bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
3327d613b6a7SJames Smart 
3328d613b6a7SJames Smart 	/* word 8 - tell the FW to abort the IO associated with this
3329d613b6a7SJames Smart 	 * outstanding exchange ID.
3330d613b6a7SJames Smart 	 */
3331d613b6a7SJames Smart 	abts_wqe->abort_cmd.wqe_com.abort_tag = ctxp->wqeq->sli4_xritag;
3332d613b6a7SJames Smart 
3333d613b6a7SJames Smart 	/* word 9 - this is the iotag for the abts_wqe completion. */
3334d613b6a7SJames Smart 	bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
3335d613b6a7SJames Smart 	       abts_wqeq->iotag);
3336d613b6a7SJames Smart 
3337d613b6a7SJames Smart 	/* word 10 */
3338d613b6a7SJames Smart 	bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
3339d613b6a7SJames Smart 	bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
3340d613b6a7SJames Smart 
3341d613b6a7SJames Smart 	/* word 11 */
3342d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
3343d613b6a7SJames Smart 	bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
3344d613b6a7SJames Smart 	bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
3345d613b6a7SJames Smart 
3346d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
3347d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
3348d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
3349d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
3350d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVME;
3351d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
33524550f9c7SJames Smart 	abts_wqeq->vport = phba->pport;
33531fbf9742SJames Smart 	if (!ctxp->hdwq)
33541fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
33551fbf9742SJames Smart 
33561fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3357d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3358547077a4SJames Smart 	if (rc == WQE_SUCCESS) {
3359547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_sol);
3360d613b6a7SJames Smart 		return 0;
3361547077a4SJames Smart 	}
3362d613b6a7SJames Smart 
3363547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3364c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
336586c67379SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3366c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3367d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
336886c67379SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
336986c67379SJames Smart 			"6166 Failed ABORT issue_wqe with status x%x "
3370d613b6a7SJames Smart 			"for oxid x%x.\n",
3371d613b6a7SJames Smart 			rc, ctxp->oxid);
3372d613b6a7SJames Smart 	return 1;
3373d613b6a7SJames Smart }
3374d613b6a7SJames Smart 
3375d613b6a7SJames Smart static int
3376d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
3377d613b6a7SJames Smart 				 struct lpfc_nvmet_rcv_ctx *ctxp,
3378d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
3379d613b6a7SJames Smart {
3380d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3381d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3382d613b6a7SJames Smart 	unsigned long flags;
3383c160c0f8SJames Smart 	bool released = false;
3384d613b6a7SJames Smart 	int rc;
3385d613b6a7SJames Smart 
3386d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3387d613b6a7SJames Smart 	if (!ctxp->wqeq) {
33886c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3389d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3390d613b6a7SJames Smart 	}
3391d613b6a7SJames Smart 
3392ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_FREE) {
3393ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3394ce1b591cSJames Smart 				"6417 NVMET ABORT ctx freed %d %d oxid x%x\n",
3395ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
3396ce1b591cSJames Smart 		rc = WQE_BUSY;
3397ce1b591cSJames Smart 		goto aerr;
3398ce1b591cSJames Smart 	}
3399ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
3400ce1b591cSJames Smart 	ctxp->entry_cnt++;
3401d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
3402d613b6a7SJames Smart 	if (rc == 0)
3403d613b6a7SJames Smart 		goto aerr;
3404d613b6a7SJames Smart 
3405d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3406d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
340786c67379SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_unsol_fcp_abort_cmp;
340886c67379SJames Smart 	abts_wqeq->iocb_cmpl = NULL;
3409d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVMET;
34101fbf9742SJames Smart 	if (!ctxp->hdwq)
34111fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
34121fbf9742SJames Smart 
34131fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3414d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3415d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3416d613b6a7SJames Smart 		return 0;
3417d613b6a7SJames Smart 	}
3418d613b6a7SJames Smart 
3419d613b6a7SJames Smart aerr:
342068c9b55dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3421c160c0f8SJames Smart 	if (ctxp->flag & LPFC_NVMET_CTX_RLS) {
3422c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
342379d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3424c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
3425c160c0f8SJames Smart 		released = true;
3426c160c0f8SJames Smart 	}
342768c9b55dSJames Smart 	ctxp->flag &= ~(LPFC_NVMET_ABORT_OP | LPFC_NVMET_CTX_RLS);
342868c9b55dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
342968c9b55dSJames Smart 
3430d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3431ce1b591cSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
343279d8c4ceSJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x "
343379d8c4ceSJames Smart 			"(%x)\n",
343479d8c4ceSJames Smart 			ctxp->oxid, rc, released);
3435c160c0f8SJames Smart 	if (released)
343668c9b55dSJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3437d613b6a7SJames Smart 	return 1;
3438d613b6a7SJames Smart }
3439d613b6a7SJames Smart 
3440d613b6a7SJames Smart static int
3441d613b6a7SJames Smart lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *phba,
3442d613b6a7SJames Smart 				struct lpfc_nvmet_rcv_ctx *ctxp,
3443d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
3444d613b6a7SJames Smart {
3445d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3446d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3447d613b6a7SJames Smart 	unsigned long flags;
3448d613b6a7SJames Smart 	int rc;
3449d613b6a7SJames Smart 
3450ce1b591cSJames Smart 	if ((ctxp->state == LPFC_NVMET_STE_LS_RCV && ctxp->entry_cnt == 1) ||
3451ce1b591cSJames Smart 	    (ctxp->state == LPFC_NVMET_STE_LS_RSP && ctxp->entry_cnt == 2)) {
3452ce1b591cSJames Smart 		ctxp->state = LPFC_NVMET_STE_LS_ABORT;
3453ce1b591cSJames Smart 		ctxp->entry_cnt++;
3454ce1b591cSJames Smart 	} else {
3455ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3456ce1b591cSJames Smart 				"6418 NVMET LS abort state mismatch "
3457ce1b591cSJames Smart 				"IO x%x: %d %d\n",
3458ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3459ce1b591cSJames Smart 		ctxp->state = LPFC_NVMET_STE_LS_ABORT;
3460ce1b591cSJames Smart 	}
3461ce1b591cSJames Smart 
3462d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3463d613b6a7SJames Smart 	if (!ctxp->wqeq) {
3464d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
3465d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
3466d613b6a7SJames Smart 		if (!ctxp->wqeq) {
3467ce1b591cSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3468d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
3469d613b6a7SJames Smart 					"xri: x%x\n", xri);
3470d613b6a7SJames Smart 			/* No failure to an ABTS request. */
3471d613b6a7SJames Smart 			kfree(ctxp);
3472d613b6a7SJames Smart 			return 0;
3473d613b6a7SJames Smart 		}
3474d613b6a7SJames Smart 	}
3475d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
34766c621a22SJames Smart 
3477ce1b591cSJames Smart 	if (lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri) == 0) {
3478ce1b591cSJames Smart 		rc = WQE_BUSY;
3479ce1b591cSJames Smart 		goto out;
3480ce1b591cSJames Smart 	}
3481d613b6a7SJames Smart 
3482d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3483d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
3484d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
3485d613b6a7SJames Smart 	abts_wqeq->iocb_flag |=  LPFC_IO_NVME_LS;
34861fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3487d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3488d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3489547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_unsol);
3490d613b6a7SJames Smart 		return 0;
3491d613b6a7SJames Smart 	}
3492ce1b591cSJames Smart out:
3493d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3494d613b6a7SJames Smart 	abts_wqeq->context2 = NULL;
3495d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
3496d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3497d613b6a7SJames Smart 	kfree(ctxp);
3498ce1b591cSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3499d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
3500d613b6a7SJames Smart 	return 0;
3501d613b6a7SJames Smart }
3502