xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision 46e8e475)
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 
2233999df75SBart Van Assche static void
22486c67379SJames Smart lpfc_nvmet_defer_release(struct lpfc_hba *phba, struct lpfc_nvmet_rcv_ctx *ctxp)
22586c67379SJames Smart {
226472e146dSJames Smart 	lockdep_assert_held(&ctxp->ctxlock);
22786c67379SJames Smart 
228e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
22986c67379SJames Smart 			"6313 NVMET Defer ctx release xri x%x flg x%x\n",
23086c67379SJames Smart 			ctxp->oxid, ctxp->flag);
23186c67379SJames Smart 
232472e146dSJames Smart 	if (ctxp->flag & LPFC_NVMET_CTX_RLS)
23386c67379SJames Smart 		return;
234472e146dSJames Smart 
23586c67379SJames Smart 	ctxp->flag |= LPFC_NVMET_CTX_RLS;
236472e146dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
23786c67379SJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
238472e146dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
23986c67379SJames Smart }
24086c67379SJames Smart 
241d613b6a7SJames Smart /**
242d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
243d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
244d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
245d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
246d613b6a7SJames Smart  *
247d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
248d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME LS commands
249d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
250d613b6a7SJames Smart  **/
251d613b6a7SJames Smart static void
252d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
253d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
254d613b6a7SJames Smart {
255d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
256d613b6a7SJames Smart 	struct nvmefc_tgt_ls_req *rsp;
257d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
258d613b6a7SJames Smart 	uint32_t status, result;
259d613b6a7SJames Smart 
260d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
261d613b6a7SJames Smart 	result = wcqe->parameter;
262ce1b591cSJames Smart 	ctxp = cmdwqe->context2;
263ce1b591cSJames Smart 
264ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_LS_RSP || ctxp->entry_cnt != 2) {
265ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
266ce1b591cSJames Smart 				"6410 NVMET LS cmpl state mismatch IO x%x: "
267ce1b591cSJames Smart 				"%d %d\n",
268ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
269ce1b591cSJames Smart 	}
270ce1b591cSJames Smart 
271d613b6a7SJames Smart 	if (!phba->targetport)
272d613b6a7SJames Smart 		goto out;
273d613b6a7SJames Smart 
274d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
275d613b6a7SJames Smart 
2764b056682SJames Smart 	if (tgtp) {
2774b056682SJames Smart 		if (status) {
278d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_error);
2798ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
2804b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_aborted);
2814b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
2824b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_xb_set);
2834b056682SJames Smart 		} else {
284d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
2854b056682SJames Smart 		}
2864b056682SJames Smart 	}
287d613b6a7SJames Smart 
288d613b6a7SJames Smart out:
289d613b6a7SJames Smart 	rsp = &ctxp->ctx.ls_req;
290d613b6a7SJames Smart 
2912b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  CMPL: xri x%x stat x%x result x%x\n",
2922b65e182SJames Smart 			 ctxp->oxid, status, result);
2932b65e182SJames Smart 
294d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
295ce1b591cSJames Smart 			"6038 NVMET LS rsp cmpl: %d %d oxid x%x\n",
296ce1b591cSJames Smart 			status, result, ctxp->oxid);
297d613b6a7SJames Smart 
298d613b6a7SJames Smart 	lpfc_nlp_put(cmdwqe->context1);
299d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
300d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
301d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
302d613b6a7SJames Smart 	rsp->done(rsp);
303d613b6a7SJames Smart 	kfree(ctxp);
304d613b6a7SJames Smart }
305d613b6a7SJames Smart 
306d613b6a7SJames Smart /**
3076c621a22SJames Smart  * lpfc_nvmet_ctxbuf_post - Repost a NVMET RQ DMA buffer and clean up context
308d613b6a7SJames Smart  * @phba: HBA buffer is associated with
309d613b6a7SJames Smart  * @ctxp: context to clean up
310d613b6a7SJames Smart  * @mp: Buffer to free
311d613b6a7SJames Smart  *
312d613b6a7SJames Smart  * Description: Frees the given DMA buffer in the appropriate way given by
313d613b6a7SJames Smart  * reposting it to its associated RQ so it can be reused.
314d613b6a7SJames Smart  *
315d613b6a7SJames Smart  * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
316d613b6a7SJames Smart  *
317d613b6a7SJames Smart  * Returns: None
318d613b6a7SJames Smart  **/
319d613b6a7SJames Smart void
3206c621a22SJames Smart lpfc_nvmet_ctxbuf_post(struct lpfc_hba *phba, struct lpfc_nvmet_ctxbuf *ctx_buf)
321d613b6a7SJames Smart {
322eeeb51d8SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
3236c621a22SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
324a8cf5dfeSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
325a8cf5dfeSJames Smart 	struct fc_frame_header *fc_hdr;
326a8cf5dfeSJames Smart 	struct rqb_dmabuf *nvmebuf;
32766d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
328472e146dSJames Smart 	uint32_t size, oxid, sid;
32966d7ce93SDick Kennedy 	int cpu;
3306c621a22SJames Smart 	unsigned long iflag;
33186c67379SJames Smart 
332d613b6a7SJames Smart 	if (ctxp->txrdy) {
333771db5c0SRomain Perier 		dma_pool_free(phba->txrdy_payload_pool, ctxp->txrdy,
334d613b6a7SJames Smart 			      ctxp->txrdy_phys);
335d613b6a7SJames Smart 		ctxp->txrdy = NULL;
336d613b6a7SJames Smart 		ctxp->txrdy_phys = 0;
337d613b6a7SJames Smart 	}
338ce1b591cSJames Smart 
339ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_FREE) {
340ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
341ce1b591cSJames Smart 				"6411 NVMET free, already free IO x%x: %d %d\n",
342ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
343ce1b591cSJames Smart 	}
344472e146dSJames Smart 
345472e146dSJames Smart 	if (ctxp->rqb_buffer) {
346472e146dSJames Smart 		nvmebuf = ctxp->rqb_buffer;
347472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
348472e146dSJames Smart 		ctxp->rqb_buffer = NULL;
349472e146dSJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) {
350472e146dSJames Smart 			ctxp->flag &= ~LPFC_NVMET_CTX_REUSE_WQ;
351472e146dSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
352472e146dSJames Smart 			nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
353472e146dSJames Smart 		} else {
354472e146dSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
355472e146dSJames Smart 			lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
356472e146dSJames Smart 		}
357472e146dSJames Smart 	}
358d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_FREE;
3596c621a22SJames Smart 
360a8cf5dfeSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
361a8cf5dfeSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
362a8cf5dfeSJames Smart 		list_remove_head(&phba->sli4_hba.lpfc_nvmet_io_wait_list,
363a8cf5dfeSJames Smart 				 nvmebuf, struct rqb_dmabuf,
364a8cf5dfeSJames Smart 				 hbuf.list);
365a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt--;
366a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
367a8cf5dfeSJames Smart 				       iflag);
368a8cf5dfeSJames Smart 
369a8cf5dfeSJames Smart 		fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
370a8cf5dfeSJames Smart 		oxid = be16_to_cpu(fc_hdr->fh_ox_id);
371a8cf5dfeSJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
372a8cf5dfeSJames Smart 		size = nvmebuf->bytes_recv;
373a8cf5dfeSJames Smart 		sid = sli4_sid_from_fc_hdr(fc_hdr);
374a8cf5dfeSJames Smart 
375a8cf5dfeSJames Smart 		ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
376a8cf5dfeSJames Smart 		ctxp->wqeq = NULL;
377a8cf5dfeSJames Smart 		ctxp->txrdy = NULL;
378a8cf5dfeSJames Smart 		ctxp->offset = 0;
379a8cf5dfeSJames Smart 		ctxp->phba = phba;
380a8cf5dfeSJames Smart 		ctxp->size = size;
381a8cf5dfeSJames Smart 		ctxp->oxid = oxid;
382a8cf5dfeSJames Smart 		ctxp->sid = sid;
383a8cf5dfeSJames Smart 		ctxp->state = LPFC_NVMET_STE_RCV;
384a8cf5dfeSJames Smart 		ctxp->entry_cnt = 1;
385a8cf5dfeSJames Smart 		ctxp->flag = 0;
386a8cf5dfeSJames Smart 		ctxp->ctxbuf = ctx_buf;
387cbc5de1bSJames Smart 		ctxp->rqb_buffer = (void *)nvmebuf;
388a8cf5dfeSJames Smart 		spin_lock_init(&ctxp->ctxlock);
389a8cf5dfeSJames Smart 
390a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
391c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
392a8cf5dfeSJames Smart 			ctxp->ts_cmd_nvme = ktime_get_ns();
393a8cf5dfeSJames Smart 			ctxp->ts_nvme_data = 0;
394a8cf5dfeSJames Smart 			ctxp->ts_data_wqput = 0;
395a8cf5dfeSJames Smart 			ctxp->ts_isr_data = 0;
396a8cf5dfeSJames Smart 			ctxp->ts_data_nvme = 0;
397a8cf5dfeSJames Smart 			ctxp->ts_nvme_status = 0;
398a8cf5dfeSJames Smart 			ctxp->ts_status_wqput = 0;
399a8cf5dfeSJames Smart 			ctxp->ts_isr_status = 0;
400a8cf5dfeSJames Smart 			ctxp->ts_status_nvme = 0;
401d613b6a7SJames Smart 		}
402a8cf5dfeSJames Smart #endif
403a8cf5dfeSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_in);
404472e146dSJames Smart 
405472e146dSJames Smart 		/*  flag new work queued, replacement buffer has already
406472e146dSJames Smart 		 *  been reposted
407a8cf5dfeSJames Smart 		 */
408472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
409472e146dSJames Smart 		ctxp->flag |= LPFC_NVMET_CTX_REUSE_WQ;
410472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
411a8cf5dfeSJames Smart 
412472e146dSJames Smart 		if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
413a8cf5dfeSJames Smart 			atomic_inc(&tgtp->rcv_fcp_cmd_drop);
414472e146dSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
415472e146dSJames Smart 					"6181 Unable to queue deferred work "
416472e146dSJames Smart 					"for oxid x%x. "
417472e146dSJames Smart 					"FCP Drop IO [x%x x%x x%x]\n",
418472e146dSJames Smart 					ctxp->oxid,
419a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_in),
420a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_out),
421a8cf5dfeSJames Smart 					atomic_read(&tgtp->xmt_fcp_release));
422a8cf5dfeSJames Smart 
423472e146dSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
424a8cf5dfeSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
425472e146dSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
426a8cf5dfeSJames Smart 			lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
427472e146dSJames Smart 		}
428a8cf5dfeSJames Smart 		return;
429a8cf5dfeSJames Smart 	}
430a8cf5dfeSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
431a8cf5dfeSJames Smart 
43266d7ce93SDick Kennedy 	/*
43366d7ce93SDick Kennedy 	 * Use the CPU context list, from the MRQ the IO was received on
43466d7ce93SDick Kennedy 	 * (ctxp->idx), to save context structure.
43566d7ce93SDick Kennedy 	 */
436d6d189ceSBart Van Assche 	cpu = raw_smp_processor_id();
43766d7ce93SDick Kennedy 	infop = lpfc_get_ctx_list(phba, cpu, ctxp->idx);
43866d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, iflag);
43966d7ce93SDick Kennedy 	list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
44066d7ce93SDick Kennedy 	infop->nvmet_ctx_list_cnt++;
44166d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, iflag);
442eeeb51d8SJames Smart #endif
443d613b6a7SJames Smart }
444d613b6a7SJames Smart 
4452b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4462b65e182SJames Smart static void
4472b65e182SJames Smart lpfc_nvmet_ktime(struct lpfc_hba *phba,
4482b65e182SJames Smart 		 struct lpfc_nvmet_rcv_ctx *ctxp)
4492b65e182SJames Smart {
4502b65e182SJames Smart 	uint64_t seg1, seg2, seg3, seg4, seg5;
4512b65e182SJames Smart 	uint64_t seg6, seg7, seg8, seg9, seg10;
452c8a4ce0bSDick Kennedy 	uint64_t segsum;
4532b65e182SJames Smart 
4542b65e182SJames Smart 	if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
4552b65e182SJames Smart 	    !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
4562b65e182SJames Smart 	    !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
4572b65e182SJames Smart 	    !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
4582b65e182SJames Smart 	    !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
4592b65e182SJames Smart 		return;
4602b65e182SJames Smart 
461c8a4ce0bSDick Kennedy 	if (ctxp->ts_status_nvme < ctxp->ts_isr_cmd)
462c8a4ce0bSDick Kennedy 		return;
4632b65e182SJames Smart 	if (ctxp->ts_isr_cmd  > ctxp->ts_cmd_nvme)
4642b65e182SJames Smart 		return;
4652b65e182SJames Smart 	if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
4662b65e182SJames Smart 		return;
4672b65e182SJames Smart 	if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
4682b65e182SJames Smart 		return;
4692b65e182SJames Smart 	if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
4702b65e182SJames Smart 		return;
4712b65e182SJames Smart 	if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
4722b65e182SJames Smart 		return;
4732b65e182SJames Smart 	if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
4742b65e182SJames Smart 		return;
4752b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
4762b65e182SJames Smart 		return;
4772b65e182SJames Smart 	if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
4782b65e182SJames Smart 		return;
4792b65e182SJames Smart 	if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
4802b65e182SJames Smart 		return;
4812b65e182SJames Smart 	/*
4822b65e182SJames Smart 	 * Segment 1 - Time from FCP command received by MSI-X ISR
4832b65e182SJames Smart 	 * to FCP command is passed to NVME Layer.
4842b65e182SJames Smart 	 * Segment 2 - Time from FCP command payload handed
4852b65e182SJames Smart 	 * off to NVME Layer to Driver receives a Command op
4862b65e182SJames Smart 	 * from NVME Layer.
4872b65e182SJames Smart 	 * Segment 3 - Time from Driver receives a Command op
4882b65e182SJames Smart 	 * from NVME Layer to Command is put on WQ.
4892b65e182SJames Smart 	 * Segment 4 - Time from Driver WQ put is done
4902b65e182SJames Smart 	 * to MSI-X ISR for Command cmpl.
4912b65e182SJames Smart 	 * Segment 5 - Time from MSI-X ISR for Command cmpl to
4922b65e182SJames Smart 	 * Command cmpl is passed to NVME Layer.
4932b65e182SJames Smart 	 * Segment 6 - Time from Command cmpl is passed to NVME
4942b65e182SJames Smart 	 * Layer to Driver receives a RSP op from NVME Layer.
4952b65e182SJames Smart 	 * Segment 7 - Time from Driver receives a RSP op from
4962b65e182SJames Smart 	 * NVME Layer to WQ put is done on TRSP FCP Status.
4972b65e182SJames Smart 	 * Segment 8 - Time from Driver WQ put is done on TRSP
4982b65e182SJames Smart 	 * FCP Status to MSI-X ISR for TRSP cmpl.
4992b65e182SJames Smart 	 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
5002b65e182SJames Smart 	 * TRSP cmpl is passed to NVME Layer.
5012b65e182SJames Smart 	 * Segment 10 - Time from FCP command received by
5022b65e182SJames Smart 	 * MSI-X ISR to command is completed on wire.
5032b65e182SJames Smart 	 * (Segments 1 thru 8) for READDATA / WRITEDATA
5042b65e182SJames Smart 	 * (Segments 1 thru 4) for READDATA_RSP
5052b65e182SJames Smart 	 */
5062b65e182SJames Smart 	seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
507c8a4ce0bSDick Kennedy 	segsum = seg1;
508c8a4ce0bSDick Kennedy 
509c8a4ce0bSDick Kennedy 	seg2 = ctxp->ts_nvme_data - ctxp->ts_isr_cmd;
510c8a4ce0bSDick Kennedy 	if (segsum > seg2)
511c8a4ce0bSDick Kennedy 		return;
512c8a4ce0bSDick Kennedy 	seg2 -= segsum;
513c8a4ce0bSDick Kennedy 	segsum += seg2;
514c8a4ce0bSDick Kennedy 
515c8a4ce0bSDick Kennedy 	seg3 = ctxp->ts_data_wqput - ctxp->ts_isr_cmd;
516c8a4ce0bSDick Kennedy 	if (segsum > seg3)
517c8a4ce0bSDick Kennedy 		return;
518c8a4ce0bSDick Kennedy 	seg3 -= segsum;
519c8a4ce0bSDick Kennedy 	segsum += seg3;
520c8a4ce0bSDick Kennedy 
521c8a4ce0bSDick Kennedy 	seg4 = ctxp->ts_isr_data - ctxp->ts_isr_cmd;
522c8a4ce0bSDick Kennedy 	if (segsum > seg4)
523c8a4ce0bSDick Kennedy 		return;
524c8a4ce0bSDick Kennedy 	seg4 -= segsum;
525c8a4ce0bSDick Kennedy 	segsum += seg4;
526c8a4ce0bSDick Kennedy 
527c8a4ce0bSDick Kennedy 	seg5 = ctxp->ts_data_nvme - ctxp->ts_isr_cmd;
528c8a4ce0bSDick Kennedy 	if (segsum > seg5)
529c8a4ce0bSDick Kennedy 		return;
530c8a4ce0bSDick Kennedy 	seg5 -= segsum;
531c8a4ce0bSDick Kennedy 	segsum += seg5;
532c8a4ce0bSDick Kennedy 
5332b65e182SJames Smart 
5342b65e182SJames Smart 	/* For auto rsp commands seg6 thru seg10 will be 0 */
5352b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
536c8a4ce0bSDick Kennedy 		seg6 = ctxp->ts_nvme_status - ctxp->ts_isr_cmd;
537c8a4ce0bSDick Kennedy 		if (segsum > seg6)
538c8a4ce0bSDick Kennedy 			return;
539c8a4ce0bSDick Kennedy 		seg6 -= segsum;
540c8a4ce0bSDick Kennedy 		segsum += seg6;
541c8a4ce0bSDick Kennedy 
542c8a4ce0bSDick Kennedy 		seg7 = ctxp->ts_status_wqput - ctxp->ts_isr_cmd;
543c8a4ce0bSDick Kennedy 		if (segsum > seg7)
544c8a4ce0bSDick Kennedy 			return;
545c8a4ce0bSDick Kennedy 		seg7 -= segsum;
546c8a4ce0bSDick Kennedy 		segsum += seg7;
547c8a4ce0bSDick Kennedy 
548c8a4ce0bSDick Kennedy 		seg8 = ctxp->ts_isr_status - ctxp->ts_isr_cmd;
549c8a4ce0bSDick Kennedy 		if (segsum > seg8)
550c8a4ce0bSDick Kennedy 			return;
551c8a4ce0bSDick Kennedy 		seg8 -= segsum;
552c8a4ce0bSDick Kennedy 		segsum += seg8;
553c8a4ce0bSDick Kennedy 
554c8a4ce0bSDick Kennedy 		seg9 = ctxp->ts_status_nvme - ctxp->ts_isr_cmd;
555c8a4ce0bSDick Kennedy 		if (segsum > seg9)
556c8a4ce0bSDick Kennedy 			return;
557c8a4ce0bSDick Kennedy 		seg9 -= segsum;
558c8a4ce0bSDick Kennedy 		segsum += seg9;
559c8a4ce0bSDick Kennedy 
560c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_status < ctxp->ts_isr_cmd)
561c8a4ce0bSDick Kennedy 			return;
5622b65e182SJames Smart 		seg10 = (ctxp->ts_isr_status -
5632b65e182SJames Smart 			ctxp->ts_isr_cmd);
5642b65e182SJames Smart 	} else {
565c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_data < ctxp->ts_isr_cmd)
566c8a4ce0bSDick Kennedy 			return;
5672b65e182SJames Smart 		seg6 =  0;
5682b65e182SJames Smart 		seg7 =  0;
5692b65e182SJames Smart 		seg8 =  0;
5702b65e182SJames Smart 		seg9 =  0;
5712b65e182SJames Smart 		seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
5722b65e182SJames Smart 	}
5732b65e182SJames Smart 
5742b65e182SJames Smart 	phba->ktime_seg1_total += seg1;
5752b65e182SJames Smart 	if (seg1 < phba->ktime_seg1_min)
5762b65e182SJames Smart 		phba->ktime_seg1_min = seg1;
5772b65e182SJames Smart 	else if (seg1 > phba->ktime_seg1_max)
5782b65e182SJames Smart 		phba->ktime_seg1_max = seg1;
5792b65e182SJames Smart 
5802b65e182SJames Smart 	phba->ktime_seg2_total += seg2;
5812b65e182SJames Smart 	if (seg2 < phba->ktime_seg2_min)
5822b65e182SJames Smart 		phba->ktime_seg2_min = seg2;
5832b65e182SJames Smart 	else if (seg2 > phba->ktime_seg2_max)
5842b65e182SJames Smart 		phba->ktime_seg2_max = seg2;
5852b65e182SJames Smart 
5862b65e182SJames Smart 	phba->ktime_seg3_total += seg3;
5872b65e182SJames Smart 	if (seg3 < phba->ktime_seg3_min)
5882b65e182SJames Smart 		phba->ktime_seg3_min = seg3;
5892b65e182SJames Smart 	else if (seg3 > phba->ktime_seg3_max)
5902b65e182SJames Smart 		phba->ktime_seg3_max = seg3;
5912b65e182SJames Smart 
5922b65e182SJames Smart 	phba->ktime_seg4_total += seg4;
5932b65e182SJames Smart 	if (seg4 < phba->ktime_seg4_min)
5942b65e182SJames Smart 		phba->ktime_seg4_min = seg4;
5952b65e182SJames Smart 	else if (seg4 > phba->ktime_seg4_max)
5962b65e182SJames Smart 		phba->ktime_seg4_max = seg4;
5972b65e182SJames Smart 
5982b65e182SJames Smart 	phba->ktime_seg5_total += seg5;
5992b65e182SJames Smart 	if (seg5 < phba->ktime_seg5_min)
6002b65e182SJames Smart 		phba->ktime_seg5_min = seg5;
6012b65e182SJames Smart 	else if (seg5 > phba->ktime_seg5_max)
6022b65e182SJames Smart 		phba->ktime_seg5_max = seg5;
6032b65e182SJames Smart 
6042b65e182SJames Smart 	phba->ktime_data_samples++;
6052b65e182SJames Smart 	if (!seg6)
6062b65e182SJames Smart 		goto out;
6072b65e182SJames Smart 
6082b65e182SJames Smart 	phba->ktime_seg6_total += seg6;
6092b65e182SJames Smart 	if (seg6 < phba->ktime_seg6_min)
6102b65e182SJames Smart 		phba->ktime_seg6_min = seg6;
6112b65e182SJames Smart 	else if (seg6 > phba->ktime_seg6_max)
6122b65e182SJames Smart 		phba->ktime_seg6_max = seg6;
6132b65e182SJames Smart 
6142b65e182SJames Smart 	phba->ktime_seg7_total += seg7;
6152b65e182SJames Smart 	if (seg7 < phba->ktime_seg7_min)
6162b65e182SJames Smart 		phba->ktime_seg7_min = seg7;
6172b65e182SJames Smart 	else if (seg7 > phba->ktime_seg7_max)
6182b65e182SJames Smart 		phba->ktime_seg7_max = seg7;
6192b65e182SJames Smart 
6202b65e182SJames Smart 	phba->ktime_seg8_total += seg8;
6212b65e182SJames Smart 	if (seg8 < phba->ktime_seg8_min)
6222b65e182SJames Smart 		phba->ktime_seg8_min = seg8;
6232b65e182SJames Smart 	else if (seg8 > phba->ktime_seg8_max)
6242b65e182SJames Smart 		phba->ktime_seg8_max = seg8;
6252b65e182SJames Smart 
6262b65e182SJames Smart 	phba->ktime_seg9_total += seg9;
6272b65e182SJames Smart 	if (seg9 < phba->ktime_seg9_min)
6282b65e182SJames Smart 		phba->ktime_seg9_min = seg9;
6292b65e182SJames Smart 	else if (seg9 > phba->ktime_seg9_max)
6302b65e182SJames Smart 		phba->ktime_seg9_max = seg9;
6312b65e182SJames Smart out:
6322b65e182SJames Smart 	phba->ktime_seg10_total += seg10;
6332b65e182SJames Smart 	if (seg10 < phba->ktime_seg10_min)
6342b65e182SJames Smart 		phba->ktime_seg10_min = seg10;
6352b65e182SJames Smart 	else if (seg10 > phba->ktime_seg10_max)
6362b65e182SJames Smart 		phba->ktime_seg10_max = seg10;
6372b65e182SJames Smart 	phba->ktime_status_samples++;
6382b65e182SJames Smart }
6392b65e182SJames Smart #endif
6402b65e182SJames Smart 
641d613b6a7SJames Smart /**
642d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
643d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
644d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
645d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
646d613b6a7SJames Smart  *
647d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
648d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME FCP commands
649d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
650d613b6a7SJames Smart  **/
651d613b6a7SJames Smart static void
652d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
653d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
654d613b6a7SJames Smart {
655d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
656d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
657d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
658e3246a12SJames Smart 	uint32_t status, result, op, start_clean, logerr;
6592b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
6602b65e182SJames Smart 	uint32_t id;
6612b65e182SJames Smart #endif
662d613b6a7SJames Smart 
663d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
66486c67379SJames Smart 	ctxp->flag &= ~LPFC_NVMET_IO_INP;
66586c67379SJames Smart 
666d613b6a7SJames Smart 	rsp = &ctxp->ctx.fcp_req;
667d613b6a7SJames Smart 	op = rsp->op;
668d613b6a7SJames Smart 
669d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
670d613b6a7SJames Smart 	result = wcqe->parameter;
671d613b6a7SJames Smart 
67286c67379SJames Smart 	if (phba->targetport)
67386c67379SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
67486c67379SJames Smart 	else
67586c67379SJames Smart 		tgtp = NULL;
676d613b6a7SJames Smart 
6772b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
6782b65e182SJames Smart 			 ctxp->oxid, op, status);
6792b65e182SJames Smart 
680d613b6a7SJames Smart 	if (status) {
681d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
682d613b6a7SJames Smart 		rsp->transferred_length = 0;
6834b056682SJames Smart 		if (tgtp) {
684d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_error);
6858ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
6864b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_aborted);
6874b056682SJames Smart 		}
68886c67379SJames Smart 
689e3246a12SJames Smart 		logerr = LOG_NVME_IOERR;
690e3246a12SJames Smart 
69186c67379SJames Smart 		/* pick up SLI4 exhange busy condition */
69286c67379SJames Smart 		if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
69386c67379SJames Smart 			ctxp->flag |= LPFC_NVMET_XBUSY;
694e3246a12SJames Smart 			logerr |= LOG_NVME_ABTS;
6954b056682SJames Smart 			if (tgtp)
6964b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_xb_set);
69786c67379SJames Smart 
69886c67379SJames Smart 		} else {
69986c67379SJames Smart 			ctxp->flag &= ~LPFC_NVMET_XBUSY;
70086c67379SJames Smart 		}
70186c67379SJames Smart 
702e3246a12SJames Smart 		lpfc_printf_log(phba, KERN_INFO, logerr,
703e3246a12SJames Smart 				"6315 IO Error Cmpl xri x%x: %x/%x XBUSY:x%x\n",
704e3246a12SJames Smart 				ctxp->oxid, status, result, ctxp->flag);
705e3246a12SJames Smart 
706d613b6a7SJames Smart 	} else {
707d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_SUCCESS;
708d613b6a7SJames Smart 		if (op == NVMET_FCOP_RSP)
709d613b6a7SJames Smart 			rsp->transferred_length = rsp->rsplen;
710d613b6a7SJames Smart 		else
711d613b6a7SJames Smart 			rsp->transferred_length = rsp->transfer_length;
71286c67379SJames Smart 		if (tgtp)
713d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
714d613b6a7SJames Smart 	}
715d613b6a7SJames Smart 
716d613b6a7SJames Smart 	if ((op == NVMET_FCOP_READDATA_RSP) ||
717d613b6a7SJames Smart 	    (op == NVMET_FCOP_RSP)) {
718d613b6a7SJames Smart 		/* Sanity check */
719d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DONE;
720d613b6a7SJames Smart 		ctxp->entry_cnt++;
72186c67379SJames Smart 
7222b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
723c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
7242b65e182SJames Smart 			if (rsp->op == NVMET_FCOP_READDATA_RSP) {
7252b65e182SJames Smart 				ctxp->ts_isr_data =
7262b65e182SJames Smart 					cmdwqe->isr_timestamp;
7272b65e182SJames Smart 				ctxp->ts_data_nvme =
7282b65e182SJames Smart 					ktime_get_ns();
7292b65e182SJames Smart 				ctxp->ts_nvme_status =
7302b65e182SJames Smart 					ctxp->ts_data_nvme;
7312b65e182SJames Smart 				ctxp->ts_status_wqput =
7322b65e182SJames Smart 					ctxp->ts_data_nvme;
7332b65e182SJames Smart 				ctxp->ts_isr_status =
7342b65e182SJames Smart 					ctxp->ts_data_nvme;
7352b65e182SJames Smart 				ctxp->ts_status_nvme =
7362b65e182SJames Smart 					ctxp->ts_data_nvme;
7372b65e182SJames Smart 			} else {
7382b65e182SJames Smart 				ctxp->ts_isr_status =
7392b65e182SJames Smart 					cmdwqe->isr_timestamp;
7402b65e182SJames Smart 				ctxp->ts_status_nvme =
7412b65e182SJames Smart 					ktime_get_ns();
7422b65e182SJames Smart 			}
7432b65e182SJames Smart 		}
7442b65e182SJames Smart #endif
745d613b6a7SJames Smart 		rsp->done(rsp);
7462b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
747c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme)
7482b65e182SJames Smart 			lpfc_nvmet_ktime(phba, ctxp);
7492b65e182SJames Smart #endif
75019b58d94SJames Smart 		/* lpfc_nvmet_xmt_fcp_release() will recycle the context */
751d613b6a7SJames Smart 	} else {
752d613b6a7SJames Smart 		ctxp->entry_cnt++;
753c6e0c925SDick Kennedy 		start_clean = offsetof(struct lpfc_iocbq, iocb_flag);
754d613b6a7SJames Smart 		memset(((char *)cmdwqe) + start_clean, 0,
755d613b6a7SJames Smart 		       (sizeof(struct lpfc_iocbq) - start_clean));
7562b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
757c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
7582b65e182SJames Smart 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
7592b65e182SJames Smart 			ctxp->ts_data_nvme = ktime_get_ns();
7602b65e182SJames Smart 		}
7612b65e182SJames Smart #endif
762d613b6a7SJames Smart 		rsp->done(rsp);
763d613b6a7SJames Smart 	}
76463df6d63SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
76563df6d63SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
766d6d189ceSBart Van Assche 		id = raw_smp_processor_id();
76763df6d63SJames Smart 		if (id < LPFC_CHECK_CPU_CNT) {
76863df6d63SJames Smart 			if (ctxp->cpu != id)
76963df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
77063df6d63SJames Smart 						"6704 CPU Check cmdcmpl: "
77163df6d63SJames Smart 						"cpu %d expect %d\n",
77263df6d63SJames Smart 						id, ctxp->cpu);
77363df6d63SJames Smart 			phba->sli4_hba.hdwq[rsp->hwqid].cpucheck_cmpl_io[id]++;
77463df6d63SJames Smart 		}
77563df6d63SJames Smart 	}
77663df6d63SJames Smart #endif
777d613b6a7SJames Smart }
778d613b6a7SJames Smart 
779d613b6a7SJames Smart static int
780d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
781d613b6a7SJames Smart 		      struct nvmefc_tgt_ls_req *rsp)
782d613b6a7SJames Smart {
783d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
784d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.ls_req);
785d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
786d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf =
787d613b6a7SJames Smart 		(struct hbq_dmabuf *)ctxp->rqb_buffer;
788d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
789d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
790d613b6a7SJames Smart 	struct lpfc_dmabuf dmabuf;
791d613b6a7SJames Smart 	struct ulp_bde64 bpl;
792d613b6a7SJames Smart 	int rc;
793d613b6a7SJames Smart 
7941abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
7951abcb371SDick Kennedy 		return -ENODEV;
7961abcb371SDick Kennedy 
7973386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
7983386f4bdSJames Smart 		return -ENODEV;
7993386f4bdSJames Smart 
800d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
801ce1b591cSJames Smart 			"6023 NVMET LS rsp oxid x%x\n", ctxp->oxid);
802ce1b591cSJames Smart 
803ce1b591cSJames Smart 	if ((ctxp->state != LPFC_NVMET_STE_LS_RCV) ||
804ce1b591cSJames Smart 	    (ctxp->entry_cnt != 1)) {
805ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
806ce1b591cSJames Smart 				"6412 NVMET LS rsp state mismatch "
807ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
808ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
809ce1b591cSJames Smart 	}
810ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_LS_RSP;
811ce1b591cSJames Smart 	ctxp->entry_cnt++;
812d613b6a7SJames Smart 
813d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, ctxp, rsp->rspdma,
814d613b6a7SJames Smart 				      rsp->rsplen);
815d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
816d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_drop);
817d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
818d613b6a7SJames Smart 				"6150 LS Drop IO x%x: Prep\n",
819d613b6a7SJames Smart 				ctxp->oxid);
820d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
821547077a4SJames Smart 		atomic_inc(&nvmep->xmt_ls_abort);
822d613b6a7SJames Smart 		lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp,
823d613b6a7SJames Smart 						ctxp->sid, ctxp->oxid);
824d613b6a7SJames Smart 		return -ENOMEM;
825d613b6a7SJames Smart 	}
826d613b6a7SJames Smart 
827d613b6a7SJames Smart 	/* Save numBdes for bpl2sgl */
828d613b6a7SJames Smart 	nvmewqeq->rsvd2 = 1;
829d613b6a7SJames Smart 	nvmewqeq->hba_wqidx = 0;
830d613b6a7SJames Smart 	nvmewqeq->context3 = &dmabuf;
831d613b6a7SJames Smart 	dmabuf.virt = &bpl;
832d613b6a7SJames Smart 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
833d613b6a7SJames Smart 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
834d613b6a7SJames Smart 	bpl.tus.f.bdeSize = rsp->rsplen;
835d613b6a7SJames Smart 	bpl.tus.f.bdeFlags = 0;
836d613b6a7SJames Smart 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
837d613b6a7SJames Smart 
838d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_rsp_cmp;
839d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
840d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
841d613b6a7SJames Smart 
8422b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  RESP: xri x%x wqidx x%x len x%x\n",
8432b65e182SJames Smart 			 ctxp->oxid, nvmewqeq->hba_wqidx, rsp->rsplen);
8442b65e182SJames Smart 
8451fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
846d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
847d613b6a7SJames Smart 		/*
848d613b6a7SJames Smart 		 * Okay to repost buffer here, but wait till cmpl
849d613b6a7SJames Smart 		 * before freeing ctxp and iocbq.
850d613b6a7SJames Smart 		 */
851d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
852d613b6a7SJames Smart 		ctxp->rqb_buffer = 0;
853d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_rsp);
854d613b6a7SJames Smart 		return 0;
855d613b6a7SJames Smart 	}
856d613b6a7SJames Smart 	/* Give back resources */
857d613b6a7SJames Smart 	atomic_inc(&nvmep->xmt_ls_drop);
858d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
859d613b6a7SJames Smart 			"6151 LS Drop IO x%x: Issue %d\n",
860d613b6a7SJames Smart 			ctxp->oxid, rc);
861d613b6a7SJames Smart 
862d613b6a7SJames Smart 	lpfc_nlp_put(nvmewqeq->context1);
863d613b6a7SJames Smart 
864d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
865547077a4SJames Smart 	atomic_inc(&nvmep->xmt_ls_abort);
866d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
867d613b6a7SJames Smart 	return -ENXIO;
868d613b6a7SJames Smart }
869d613b6a7SJames Smart 
870d613b6a7SJames Smart static int
871d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
872d613b6a7SJames Smart 		      struct nvmefc_tgt_fcp_req *rsp)
873d613b6a7SJames Smart {
874d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
875d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
876d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
877d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
8786e8e1c14SJames Smart 	struct lpfc_queue *wq;
879d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
8806e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
8816e8e1c14SJames Smart 	unsigned long iflags;
882223b78eaSArnd Bergmann 	int rc;
8832b65e182SJames Smart 
8841abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING) {
8851abcb371SDick Kennedy 		rc = -ENODEV;
8861abcb371SDick Kennedy 		goto aerr;
8871abcb371SDick Kennedy 	}
8881abcb371SDick Kennedy 
8893386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING) {
8903386f4bdSJames Smart 		rc = -ENODEV;
8913386f4bdSJames Smart 		goto aerr;
8923386f4bdSJames Smart 	}
8933386f4bdSJames Smart 
8942b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
895c8a4ce0bSDick Kennedy 	if (ctxp->ts_cmd_nvme) {
8962b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
8972b65e182SJames Smart 			ctxp->ts_nvme_status = ktime_get_ns();
8982b65e182SJames Smart 		else
8992b65e182SJames Smart 			ctxp->ts_nvme_data = ktime_get_ns();
9002b65e182SJames Smart 	}
9011fbf9742SJames Smart 
9021fbf9742SJames Smart 	/* Setup the hdw queue if not already set */
9031fbf9742SJames Smart 	if (!ctxp->hdwq)
9041fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[rsp->hwqid];
9051fbf9742SJames Smart 
9062b65e182SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
907d6d189ceSBart Van Assche 		int id = raw_smp_processor_id();
90863df6d63SJames Smart 		if (id < LPFC_CHECK_CPU_CNT) {
90963df6d63SJames Smart 			if (rsp->hwqid != id)
91063df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
9112b65e182SJames Smart 						"6705 CPU Check OP: "
9122b65e182SJames Smart 						"cpu %d expect %d\n",
9132b65e182SJames Smart 						id, rsp->hwqid);
91463df6d63SJames Smart 			phba->sli4_hba.hdwq[rsp->hwqid].cpucheck_xmt_io[id]++;
9152b65e182SJames Smart 		}
91663df6d63SJames Smart 		ctxp->cpu = id; /* Setup cpu for cmpl check */
9172b65e182SJames Smart 	}
9182b65e182SJames Smart #endif
919d613b6a7SJames Smart 
920d613b6a7SJames Smart 	/* Sanity check */
92186c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_ABTS_RCV) ||
92286c67379SJames Smart 	    (ctxp->state == LPFC_NVMET_STE_ABORT)) {
923d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
924d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
92586c67379SJames Smart 				"6102 IO xri x%x aborted\n",
926d613b6a7SJames Smart 				ctxp->oxid);
927a5068b46SJames Smart 		rc = -ENXIO;
928d613b6a7SJames Smart 		goto aerr;
929d613b6a7SJames Smart 	}
930d613b6a7SJames Smart 
931d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
932d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
933d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
934d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
935d613b6a7SJames Smart 				"6152 FCP Drop IO x%x: Prep\n",
936d613b6a7SJames Smart 				ctxp->oxid);
937a5068b46SJames Smart 		rc = -ENXIO;
938d613b6a7SJames Smart 		goto aerr;
939d613b6a7SJames Smart 	}
940d613b6a7SJames Smart 
941d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
942d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
943d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
944d613b6a7SJames Smart 	nvmewqeq->iocb_flag |=  LPFC_IO_NVMET;
945d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
946d613b6a7SJames Smart 
9472b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
9482b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
9492b65e182SJames Smart 
95061f3d4bfSJames Smart 	ctxp->flag |= LPFC_NVMET_IO_INP;
9511fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
952d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
9532b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
954c8a4ce0bSDick Kennedy 		if (!ctxp->ts_cmd_nvme)
9552b65e182SJames Smart 			return 0;
9562b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
9572b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
9582b65e182SJames Smart 		else
9592b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
9602b65e182SJames Smart #endif
961d613b6a7SJames Smart 		return 0;
962d613b6a7SJames Smart 	}
963d613b6a7SJames Smart 
9646e8e1c14SJames Smart 	if (rc == -EBUSY) {
9656e8e1c14SJames Smart 		/*
9666e8e1c14SJames Smart 		 * WQ was full, so queue nvmewqeq to be sent after
9676e8e1c14SJames Smart 		 * WQE release CQE
9686e8e1c14SJames Smart 		 */
9696e8e1c14SJames Smart 		ctxp->flag |= LPFC_NVMET_DEFER_WQFULL;
9701fbf9742SJames Smart 		wq = ctxp->hdwq->nvme_wq;
9716e8e1c14SJames Smart 		pring = wq->pring;
9726e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
9736e8e1c14SJames Smart 		list_add_tail(&nvmewqeq->list, &wq->wqfull_list);
9746e8e1c14SJames Smart 		wq->q_flag |= HBA_NVMET_WQFULL;
9756e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
976411de511SJames Smart 		atomic_inc(&lpfc_nvmep->defer_wqfull);
9776e8e1c14SJames Smart 		return 0;
9786e8e1c14SJames Smart 	}
9796e8e1c14SJames Smart 
980d613b6a7SJames Smart 	/* Give back resources */
981d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
982d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
983d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
984d613b6a7SJames Smart 			ctxp->oxid, rc);
985d613b6a7SJames Smart 
986d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
987d613b6a7SJames Smart 	nvmewqeq->context2 = NULL;
988d613b6a7SJames Smart 	nvmewqeq->context3 = NULL;
989a5068b46SJames Smart 	rc = -EBUSY;
990d613b6a7SJames Smart aerr:
991a5068b46SJames Smart 	return rc;
992d613b6a7SJames Smart }
993d613b6a7SJames Smart 
994d613b6a7SJames Smart static void
995d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
996d613b6a7SJames Smart {
997d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
998d613b6a7SJames Smart 
999d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
1000c41f5988SEwan D. Milne 	if (tport->phba->targetport)
1001c41f5988SEwan D. Milne 		complete(tport->tport_unreg_cmp);
1002d613b6a7SJames Smart }
1003d613b6a7SJames Smart 
100419b58d94SJames Smart static void
1005a97ec51bSJames Smart lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
1006a97ec51bSJames Smart 			 struct nvmefc_tgt_fcp_req *req)
1007a97ec51bSJames Smart {
1008a97ec51bSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
1009a97ec51bSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
1010a97ec51bSJames Smart 		container_of(req, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
1011a97ec51bSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
10126e8e1c14SJames Smart 	struct lpfc_queue *wq;
101386c67379SJames Smart 	unsigned long flags;
1014a97ec51bSJames Smart 
10151abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
10161abcb371SDick Kennedy 		return;
10171abcb371SDick Kennedy 
10183386f4bdSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
10193386f4bdSJames Smart 		return;
10203386f4bdSJames Smart 
10211fbf9742SJames Smart 	if (!ctxp->hdwq)
10221fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[0];
10231fbf9742SJames Smart 
1024a97ec51bSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1025ce1b591cSJames Smart 			"6103 NVMET Abort op: oxri x%x flg x%x ste %d\n",
1026ce1b591cSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
1027a97ec51bSJames Smart 
1028ce1b591cSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x flg x%x ste x%x\n",
1029ce1b591cSJames Smart 			 ctxp->oxid, ctxp->flag, ctxp->state);
1030a97ec51bSJames Smart 
1031a97ec51bSJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
1032ce1b591cSJames Smart 
103386c67379SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
103486c67379SJames Smart 
103586c67379SJames Smart 	/* Since iaab/iaar are NOT set, we need to check
103686c67379SJames Smart 	 * if the firmware is in process of aborting IO
103786c67379SJames Smart 	 */
103886c67379SJames Smart 	if (ctxp->flag & LPFC_NVMET_XBUSY) {
103986c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
104086c67379SJames Smart 		return;
104186c67379SJames Smart 	}
1042a97ec51bSJames Smart 	ctxp->flag |= LPFC_NVMET_ABORT_OP;
1043ce1b591cSJames Smart 
10446e8e1c14SJames Smart 	if (ctxp->flag & LPFC_NVMET_DEFER_WQFULL) {
1045c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
10466e8e1c14SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
10476e8e1c14SJames Smart 						 ctxp->oxid);
10481fbf9742SJames Smart 		wq = ctxp->hdwq->nvme_wq;
10496e8e1c14SJames Smart 		lpfc_nvmet_wqfull_flush(phba, wq, ctxp);
10506e8e1c14SJames Smart 		return;
10516e8e1c14SJames Smart 	}
1052c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
10536e8e1c14SJames Smart 
1054ce1b591cSJames Smart 	/* An state of LPFC_NVMET_STE_RCV means we have just received
1055ce1b591cSJames Smart 	 * the NVME command and have not started processing it.
1056ce1b591cSJames Smart 	 * (by issuing any IO WQEs on this exchange yet)
1057ce1b591cSJames Smart 	 */
1058ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_RCV)
1059ce1b591cSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1060a97ec51bSJames Smart 						 ctxp->oxid);
1061a97ec51bSJames Smart 	else
1062ce1b591cSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1063a97ec51bSJames Smart 					       ctxp->oxid);
1064a97ec51bSJames Smart }
1065a97ec51bSJames Smart 
1066a97ec51bSJames Smart static void
106719b58d94SJames Smart lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
106819b58d94SJames Smart 			   struct nvmefc_tgt_fcp_req *rsp)
106919b58d94SJames Smart {
1070547077a4SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
107119b58d94SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
107219b58d94SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
107319b58d94SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
107419b58d94SJames Smart 	unsigned long flags;
107519b58d94SJames Smart 	bool aborting = false;
107619b58d94SJames Smart 
1077c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
1078c160c0f8SJames Smart 	if (ctxp->flag & LPFC_NVMET_XBUSY)
1079c160c0f8SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1080c160c0f8SJames Smart 				"6027 NVMET release with XBUSY flag x%x"
1081c160c0f8SJames Smart 				" oxid x%x\n",
1082c160c0f8SJames Smart 				ctxp->flag, ctxp->oxid);
1083c160c0f8SJames Smart 	else if (ctxp->state != LPFC_NVMET_STE_DONE &&
1084c160c0f8SJames Smart 		 ctxp->state != LPFC_NVMET_STE_ABORT)
1085ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1086ce1b591cSJames Smart 				"6413 NVMET release bad state %d %d oxid x%x\n",
1087ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
1088ce1b591cSJames Smart 
108986c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_ABORT_OP) ||
109086c67379SJames Smart 	    (ctxp->flag & LPFC_NVMET_XBUSY)) {
109119b58d94SJames Smart 		aborting = true;
109286c67379SJames Smart 		/* let the abort path do the real release */
109386c67379SJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
109419b58d94SJames Smart 	}
109519b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
109619b58d94SJames Smart 
10979094367aSArnd Bergmann 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d abt %d\n", ctxp->oxid,
10989094367aSArnd Bergmann 			 ctxp->state, aborting);
109919b58d94SJames Smart 
1100547077a4SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_release);
1101547077a4SJames Smart 
110286c67379SJames Smart 	if (aborting)
110386c67379SJames Smart 		return;
110486c67379SJames Smart 
11056c621a22SJames Smart 	lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
110619b58d94SJames Smart }
110719b58d94SJames Smart 
110850738420SJames Smart static void
110950738420SJames Smart lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port *tgtport,
111050738420SJames Smart 		     struct nvmefc_tgt_fcp_req *rsp)
111150738420SJames Smart {
111250738420SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
111350738420SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
111450738420SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
111550738420SJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
111650738420SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
1117472e146dSJames Smart 	unsigned long iflag;
1118472e146dSJames Smart 
111950738420SJames Smart 
112050738420SJames Smart 	lpfc_nvmeio_data(phba, "NVMET DEFERRCV: xri x%x sz %d CPU %02x\n",
1121d6d189ceSBart Van Assche 			 ctxp->oxid, ctxp->size, raw_smp_processor_id());
112250738420SJames Smart 
11236871e814SJames Smart 	if (!nvmebuf) {
11246871e814SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
11256871e814SJames Smart 				"6425 Defer rcv: no buffer xri x%x: "
11266871e814SJames Smart 				"flg %x ste %x\n",
11276871e814SJames Smart 				ctxp->oxid, ctxp->flag, ctxp->state);
11286871e814SJames Smart 		return;
11296871e814SJames Smart 	}
11306871e814SJames Smart 
113150738420SJames Smart 	tgtp = phba->targetport->private;
11326871e814SJames Smart 	if (tgtp)
113350738420SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_defer);
1134411de511SJames Smart 
1135411de511SJames Smart 	/* Free the nvmebuf since a new buffer already replaced it */
1136cbc5de1bSJames Smart 	nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
1137472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflag);
1138472e146dSJames Smart 	ctxp->rqb_buffer = NULL;
1139472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
114050738420SJames Smart }
114150738420SJames Smart 
1142d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
1143d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
1144d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
1145d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
1146a97ec51bSJames Smart 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
114719b58d94SJames Smart 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
114850738420SJames Smart 	.defer_rcv	= lpfc_nvmet_defer_rcv,
1149d613b6a7SJames Smart 
1150d613b6a7SJames Smart 	.max_hw_queues  = 1,
1151d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1152d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1153d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
1154d613b6a7SJames Smart 
1155d613b6a7SJames Smart 	/* optional features */
1156d613b6a7SJames Smart 	.target_features = 0,
1157d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
1158d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
1159d613b6a7SJames Smart };
1160d613b6a7SJames Smart 
116139aa23f9SColin Ian King static void
116266d7ce93SDick Kennedy __lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba *phba,
116366d7ce93SDick Kennedy 		struct lpfc_nvmet_ctx_info *infop)
11646c621a22SJames Smart {
11656c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf, *next_ctx_buf;
11666c621a22SJames Smart 	unsigned long flags;
11676c621a22SJames Smart 
116866d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, flags);
1169966bb5b7SJames Smart 	list_for_each_entry_safe(ctx_buf, next_ctx_buf,
117066d7ce93SDick Kennedy 				&infop->nvmet_ctx_list, list) {
11715e5b511dSJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
11726c621a22SJames Smart 		list_del_init(&ctx_buf->list);
11735e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
117466d7ce93SDick Kennedy 
117566d7ce93SDick Kennedy 		__lpfc_clear_active_sglq(phba, ctx_buf->sglq->sli4_lxritag);
11766c621a22SJames Smart 		ctx_buf->sglq->state = SGL_FREED;
11776c621a22SJames Smart 		ctx_buf->sglq->ndlp = NULL;
11786c621a22SJames Smart 
1179c4031db7SDan Carpenter 		spin_lock(&phba->sli4_hba.sgl_list_lock);
11806c621a22SJames Smart 		list_add_tail(&ctx_buf->sglq->list,
11816c621a22SJames Smart 				&phba->sli4_hba.lpfc_nvmet_sgl_list);
1182c4031db7SDan Carpenter 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
11836c621a22SJames Smart 
11846c621a22SJames Smart 		lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
11856c621a22SJames Smart 		kfree(ctx_buf->context);
11866c621a22SJames Smart 	}
118766d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, flags);
1188966bb5b7SJames Smart }
118966d7ce93SDick Kennedy 
119066d7ce93SDick Kennedy static void
119166d7ce93SDick Kennedy lpfc_nvmet_cleanup_io_context(struct lpfc_hba *phba)
119266d7ce93SDick Kennedy {
119366d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
119466d7ce93SDick Kennedy 	int i, j;
119566d7ce93SDick Kennedy 
119666d7ce93SDick Kennedy 	/* The first context list, MRQ 0 CPU 0 */
119766d7ce93SDick Kennedy 	infop = phba->sli4_hba.nvmet_ctx_info;
119866d7ce93SDick Kennedy 	if (!infop)
119966d7ce93SDick Kennedy 		return;
120066d7ce93SDick Kennedy 
120166d7ce93SDick Kennedy 	/* Cycle the the entire CPU context list for every MRQ */
120266d7ce93SDick Kennedy 	for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
1203222e9239SJames Smart 		for_each_present_cpu(j) {
1204222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, j, i);
120566d7ce93SDick Kennedy 			__lpfc_nvmet_clean_io_for_cpu(phba, infop);
120666d7ce93SDick Kennedy 		}
120766d7ce93SDick Kennedy 	}
120866d7ce93SDick Kennedy 	kfree(phba->sli4_hba.nvmet_ctx_info);
120966d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = NULL;
12106c621a22SJames Smart }
12116c621a22SJames Smart 
121239aa23f9SColin Ian King static int
12136c621a22SJames Smart lpfc_nvmet_setup_io_context(struct lpfc_hba *phba)
12146c621a22SJames Smart {
12156c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
12166c621a22SJames Smart 	struct lpfc_iocbq *nvmewqe;
12176c621a22SJames Smart 	union lpfc_wqe128 *wqe;
121866d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *last_infop;
121966d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
1220222e9239SJames Smart 	int i, j, idx, cpu;
12216c621a22SJames Smart 
12226c621a22SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
12236c621a22SJames Smart 			"6403 Allocate NVMET resources for %d XRIs\n",
12246c621a22SJames Smart 			phba->sli4_hba.nvmet_xri_cnt);
12256c621a22SJames Smart 
122666d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = kcalloc(
1227222e9239SJames Smart 		phba->sli4_hba.num_possible_cpu * phba->cfg_nvmet_mrq,
122866d7ce93SDick Kennedy 		sizeof(struct lpfc_nvmet_ctx_info), GFP_KERNEL);
122966d7ce93SDick Kennedy 	if (!phba->sli4_hba.nvmet_ctx_info) {
123066d7ce93SDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
123166d7ce93SDick Kennedy 				"6419 Failed allocate memory for "
123266d7ce93SDick Kennedy 				"nvmet context lists\n");
123366d7ce93SDick Kennedy 		return -ENOMEM;
123466d7ce93SDick Kennedy 	}
123566d7ce93SDick Kennedy 
123666d7ce93SDick Kennedy 	/*
123766d7ce93SDick Kennedy 	 * Assuming X CPUs in the system, and Y MRQs, allocate some
123866d7ce93SDick Kennedy 	 * lpfc_nvmet_ctx_info structures as follows:
123966d7ce93SDick Kennedy 	 *
124066d7ce93SDick Kennedy 	 * cpu0/mrq0 cpu1/mrq0 ... cpuX/mrq0
124166d7ce93SDick Kennedy 	 * cpu0/mrq1 cpu1/mrq1 ... cpuX/mrq1
124266d7ce93SDick Kennedy 	 * ...
124366d7ce93SDick Kennedy 	 * cpuX/mrqY cpuX/mrqY ... cpuX/mrqY
124466d7ce93SDick Kennedy 	 *
124566d7ce93SDick Kennedy 	 * Each line represents a MRQ "silo" containing an entry for
124666d7ce93SDick Kennedy 	 * every CPU.
124766d7ce93SDick Kennedy 	 *
124866d7ce93SDick Kennedy 	 * MRQ X is initially assumed to be associated with CPU X, thus
124966d7ce93SDick Kennedy 	 * contexts are initially distributed across all MRQs using
125066d7ce93SDick Kennedy 	 * the MRQ index (N) as follows cpuN/mrqN. When contexts are
125166d7ce93SDick Kennedy 	 * freed, the are freed to the MRQ silo based on the CPU number
125266d7ce93SDick Kennedy 	 * of the IO completion. Thus a context that was allocated for MRQ A
125366d7ce93SDick Kennedy 	 * whose IO completed on CPU B will be freed to cpuB/mrqA.
125466d7ce93SDick Kennedy 	 */
1255222e9239SJames Smart 	for_each_possible_cpu(i) {
125666d7ce93SDick Kennedy 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1257222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
125866d7ce93SDick Kennedy 			INIT_LIST_HEAD(&infop->nvmet_ctx_list);
125966d7ce93SDick Kennedy 			spin_lock_init(&infop->nvmet_ctx_list_lock);
126066d7ce93SDick Kennedy 			infop->nvmet_ctx_list_cnt = 0;
126166d7ce93SDick Kennedy 		}
126266d7ce93SDick Kennedy 	}
126366d7ce93SDick Kennedy 
126466d7ce93SDick Kennedy 	/*
126566d7ce93SDick Kennedy 	 * Setup the next CPU context info ptr for each MRQ.
126666d7ce93SDick Kennedy 	 * MRQ 0 will cycle thru CPUs 0 - X separately from
126766d7ce93SDick Kennedy 	 * MRQ 1 cycling thru CPUs 0 - X, and so on.
126866d7ce93SDick Kennedy 	 */
126966d7ce93SDick Kennedy 	for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1270222e9239SJames Smart 		last_infop = lpfc_get_ctx_list(phba,
1271222e9239SJames Smart 					       cpumask_first(cpu_present_mask),
1272222e9239SJames Smart 					       j);
1273222e9239SJames Smart 		for (i = phba->sli4_hba.num_possible_cpu - 1;  i >= 0; i--) {
127466d7ce93SDick Kennedy 			infop = lpfc_get_ctx_list(phba, i, j);
127566d7ce93SDick Kennedy 			infop->nvmet_ctx_next_cpu = last_infop;
127666d7ce93SDick Kennedy 			last_infop = infop;
127766d7ce93SDick Kennedy 		}
127866d7ce93SDick Kennedy 	}
127966d7ce93SDick Kennedy 
12806c621a22SJames Smart 	/* For all nvmet xris, allocate resources needed to process a
12816c621a22SJames Smart 	 * received command on a per xri basis.
12826c621a22SJames Smart 	 */
128366d7ce93SDick Kennedy 	idx = 0;
1284222e9239SJames Smart 	cpu = cpumask_first(cpu_present_mask);
12856c621a22SJames Smart 	for (i = 0; i < phba->sli4_hba.nvmet_xri_cnt; i++) {
12866c621a22SJames Smart 		ctx_buf = kzalloc(sizeof(*ctx_buf), GFP_KERNEL);
12876c621a22SJames Smart 		if (!ctx_buf) {
12886c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
12896c621a22SJames Smart 					"6404 Ran out of memory for NVMET\n");
12906c621a22SJames Smart 			return -ENOMEM;
12916c621a22SJames Smart 		}
12926c621a22SJames Smart 
12936c621a22SJames Smart 		ctx_buf->context = kzalloc(sizeof(*ctx_buf->context),
12946c621a22SJames Smart 					   GFP_KERNEL);
12956c621a22SJames Smart 		if (!ctx_buf->context) {
12966c621a22SJames Smart 			kfree(ctx_buf);
12976c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
12986c621a22SJames Smart 					"6405 Ran out of NVMET "
12996c621a22SJames Smart 					"context memory\n");
13006c621a22SJames Smart 			return -ENOMEM;
13016c621a22SJames Smart 		}
13026c621a22SJames Smart 		ctx_buf->context->ctxbuf = ctx_buf;
1303ce1b591cSJames Smart 		ctx_buf->context->state = LPFC_NVMET_STE_FREE;
13046c621a22SJames Smart 
13056c621a22SJames Smart 		ctx_buf->iocbq = lpfc_sli_get_iocbq(phba);
13066c621a22SJames Smart 		if (!ctx_buf->iocbq) {
13076c621a22SJames Smart 			kfree(ctx_buf->context);
13086c621a22SJames Smart 			kfree(ctx_buf);
13096c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13106c621a22SJames Smart 					"6406 Ran out of NVMET iocb/WQEs\n");
13116c621a22SJames Smart 			return -ENOMEM;
13126c621a22SJames Smart 		}
13136c621a22SJames Smart 		ctx_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
13146c621a22SJames Smart 		nvmewqe = ctx_buf->iocbq;
1315205e8240SJames Smart 		wqe = &nvmewqe->wqe;
1316bd3061baSJames Smart 
13176c621a22SJames Smart 		/* Initialize WQE */
13186c621a22SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
13196c621a22SJames Smart 
13206c621a22SJames Smart 		ctx_buf->iocbq->context1 = NULL;
13216c621a22SJames Smart 		spin_lock(&phba->sli4_hba.sgl_list_lock);
13226c621a22SJames Smart 		ctx_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, ctx_buf->iocbq);
13236c621a22SJames Smart 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
13246c621a22SJames Smart 		if (!ctx_buf->sglq) {
13256c621a22SJames Smart 			lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
13266c621a22SJames Smart 			kfree(ctx_buf->context);
13276c621a22SJames Smart 			kfree(ctx_buf);
13286c621a22SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
13296c621a22SJames Smart 					"6407 Ran out of NVMET XRIs\n");
13306c621a22SJames Smart 			return -ENOMEM;
13316c621a22SJames Smart 		}
1332472e146dSJames Smart 		INIT_WORK(&ctx_buf->defer_work, lpfc_nvmet_fcp_rqst_defer_work);
133366d7ce93SDick Kennedy 
133466d7ce93SDick Kennedy 		/*
133566d7ce93SDick Kennedy 		 * Add ctx to MRQidx context list. Our initial assumption
133666d7ce93SDick Kennedy 		 * is MRQidx will be associated with CPUidx. This association
133766d7ce93SDick Kennedy 		 * can change on the fly.
133866d7ce93SDick Kennedy 		 */
1339222e9239SJames Smart 		infop = lpfc_get_ctx_list(phba, cpu, idx);
134066d7ce93SDick Kennedy 		spin_lock(&infop->nvmet_ctx_list_lock);
134166d7ce93SDick Kennedy 		list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
134266d7ce93SDick Kennedy 		infop->nvmet_ctx_list_cnt++;
134366d7ce93SDick Kennedy 		spin_unlock(&infop->nvmet_ctx_list_lock);
134466d7ce93SDick Kennedy 
134566d7ce93SDick Kennedy 		/* Spread ctx structures evenly across all MRQs */
134666d7ce93SDick Kennedy 		idx++;
1347222e9239SJames Smart 		if (idx >= phba->cfg_nvmet_mrq) {
134866d7ce93SDick Kennedy 			idx = 0;
1349222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1350222e9239SJames Smart 			continue;
1351222e9239SJames Smart 		}
1352222e9239SJames Smart 		cpu = cpumask_next(cpu, cpu_present_mask);
1353222e9239SJames Smart 		if (cpu == nr_cpu_ids)
1354222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1355222e9239SJames Smart 
13566c621a22SJames Smart 	}
135766d7ce93SDick Kennedy 
1358222e9239SJames Smart 	for_each_present_cpu(i) {
13592879265fSJames Smart 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
13602879265fSJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
136166d7ce93SDick Kennedy 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
136266d7ce93SDick Kennedy 					"6408 TOTAL NVMET ctx for CPU %d "
136366d7ce93SDick Kennedy 					"MRQ %d: cnt %d nextcpu %p\n",
136466d7ce93SDick Kennedy 					i, j, infop->nvmet_ctx_list_cnt,
136566d7ce93SDick Kennedy 					infop->nvmet_ctx_next_cpu);
136666d7ce93SDick Kennedy 		}
136766d7ce93SDick Kennedy 	}
13686c621a22SJames Smart 	return 0;
13696c621a22SJames Smart }
13706c621a22SJames Smart 
1371d613b6a7SJames Smart int
1372d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1373d613b6a7SJames Smart {
1374d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1375d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1376d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
13776c621a22SJames Smart 	int error;
1378d613b6a7SJames Smart 
1379d613b6a7SJames Smart 	if (phba->targetport)
1380d613b6a7SJames Smart 		return 0;
1381d613b6a7SJames Smart 
13826c621a22SJames Smart 	error = lpfc_nvmet_setup_io_context(phba);
13836c621a22SJames Smart 	if (error)
13846c621a22SJames Smart 		return error;
13856c621a22SJames Smart 
1386d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1387d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1388d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1389d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
1390d613b6a7SJames Smart 
13915b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
13925b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
13935b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
13944d4c4a4aSJames Smart 	 */
13954d4c4a4aSJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1396cdb42becSJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_hdw_queue;
1397f485c18dSDick Kennedy 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP;
1398d613b6a7SJames Smart 
13997d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1400d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1401d613b6a7SJames Smart 					     &phba->pcidev->dev,
1402d613b6a7SJames Smart 					     &phba->targetport);
1403166d7211SJames Smart #else
14046c621a22SJames Smart 	error = -ENOENT;
1405166d7211SJames Smart #endif
1406d613b6a7SJames Smart 	if (error) {
1407d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1408e7981a2cSDick Kennedy 				"6025 Cannot register NVME targetport x%x: "
1409e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1410e7981a2cSDick Kennedy 				error,
1411e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1412e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1413e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1414d613b6a7SJames Smart 		phba->targetport = NULL;
1415e7981a2cSDick Kennedy 		phba->nvmet_support = 0;
14166c621a22SJames Smart 
14176c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
14186c621a22SJames Smart 
1419d613b6a7SJames Smart 	} else {
1420d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
1421d613b6a7SJames Smart 			phba->targetport->private;
1422d613b6a7SJames Smart 		tgtp->phba = phba;
1423d613b6a7SJames Smart 
1424d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1425d613b6a7SJames Smart 				"6026 Registered NVME "
1426d613b6a7SJames Smart 				"targetport: %p, private %p "
1427e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1428d613b6a7SJames Smart 				phba->targetport, tgtp,
1429e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1430e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1431e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1432d613b6a7SJames Smart 
1433d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
1434d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
1435d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
1436d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
1437547077a4SJames Smart 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1438d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
1439d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
1440d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
14414b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_xb_set, 0);
14424b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_aborted, 0);
1443d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1444d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1445d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1446d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1447d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
1448d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1449d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
1450d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
1451d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
1452547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_release, 0);
1453d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1454d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
14554b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_xb_set, 0);
14564b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_aborted, 0);
1457d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
14584b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_xri_abort_cqe, 0);
1459547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
1460547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1461547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_unsol, 0);
1462547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_sol, 0);
1463d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
1464d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1465411de511SJames Smart 		atomic_set(&tgtp->defer_ctx, 0);
1466411de511SJames Smart 		atomic_set(&tgtp->defer_fod, 0);
1467411de511SJames Smart 		atomic_set(&tgtp->defer_wqfull, 0);
1468d613b6a7SJames Smart 	}
1469d613b6a7SJames Smart 	return error;
1470d613b6a7SJames Smart }
1471d613b6a7SJames Smart 
1472d613b6a7SJames Smart int
1473d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1474d613b6a7SJames Smart {
1475d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1476d613b6a7SJames Smart 
1477d613b6a7SJames Smart 	if (!phba->targetport)
1478d613b6a7SJames Smart 		return 0;
1479d613b6a7SJames Smart 
1480d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
1481d613b6a7SJames Smart 			 "6007 Update NVMET port %p did x%x\n",
1482d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
1483d613b6a7SJames Smart 
1484d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
1485d613b6a7SJames Smart 	return 0;
1486d613b6a7SJames Smart }
1487d613b6a7SJames Smart 
1488318083adSJames Smart /**
1489318083adSJames Smart  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1490318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
1491318083adSJames Smart  * @axri: pointer to the nvmet xri abort wcqe structure.
1492318083adSJames Smart  *
1493318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
1494318083adSJames Smart  * NVMET aborted xri.
1495318083adSJames Smart  **/
1496318083adSJames Smart void
1497318083adSJames Smart lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
1498318083adSJames Smart 			    struct sli4_wcqe_xri_aborted *axri)
1499318083adSJames Smart {
150086c67379SJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
150186c67379SJames Smart 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
150286c67379SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
15034b056682SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
150486c67379SJames Smart 	struct lpfc_nodelist *ndlp;
150586c67379SJames Smart 	unsigned long iflag = 0;
150686c67379SJames Smart 	int rrq_empty = 0;
150786c67379SJames Smart 	bool released = false;
150886c67379SJames Smart 
150986c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
151086c67379SJames Smart 			"6317 XB aborted xri x%x rxid x%x\n", xri, rxid);
151186c67379SJames Smart 
151286c67379SJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
151386c67379SJames Smart 		return;
15144b056682SJames Smart 
15154b056682SJames Smart 	if (phba->targetport) {
15164b056682SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
15174b056682SJames Smart 		atomic_inc(&tgtp->xmt_fcp_xri_abort_cqe);
15184b056682SJames Smart 	}
15194b056682SJames Smart 
152086c67379SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
15215e5b511dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
152286c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
152386c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
152486c67379SJames Smart 				 list) {
15256c621a22SJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
152686c67379SJames Smart 			continue;
152786c67379SJames Smart 
1528c160c0f8SJames Smart 		spin_lock(&ctxp->ctxlock);
152986c67379SJames Smart 		/* Check if we already received a free context call
153086c67379SJames Smart 		 * and we have completed processing an abort situation.
153186c67379SJames Smart 		 */
153286c67379SJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_RLS &&
153386c67379SJames Smart 		    !(ctxp->flag & LPFC_NVMET_ABORT_OP)) {
153486c67379SJames Smart 			list_del(&ctxp->list);
153586c67379SJames Smart 			released = true;
153686c67379SJames Smart 		}
153786c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_XBUSY;
1538c160c0f8SJames Smart 		spin_unlock(&ctxp->ctxlock);
15395e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
154086c67379SJames Smart 
154186c67379SJames Smart 		rrq_empty = list_empty(&phba->active_rrq_list);
154286c67379SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, iflag);
154386c67379SJames Smart 		ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
154486c67379SJames Smart 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
154586c67379SJames Smart 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
154686c67379SJames Smart 		     ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
154786c67379SJames Smart 			lpfc_set_rrq_active(phba, ndlp,
15486c621a22SJames Smart 				ctxp->ctxbuf->sglq->sli4_lxritag,
154986c67379SJames Smart 				rxid, 1);
155086c67379SJames Smart 			lpfc_sli4_abts_err_handler(phba, ndlp, axri);
155186c67379SJames Smart 		}
155286c67379SJames Smart 
155386c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1554ce1b591cSJames Smart 				"6318 XB aborted oxid %x flg x%x (%x)\n",
155586c67379SJames Smart 				ctxp->oxid, ctxp->flag, released);
155686c67379SJames Smart 		if (released)
15576c621a22SJames Smart 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
15586c621a22SJames Smart 
155986c67379SJames Smart 		if (rrq_empty)
156086c67379SJames Smart 			lpfc_worker_wake_up(phba);
156186c67379SJames Smart 		return;
156286c67379SJames Smart 	}
15635e5b511dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
156486c67379SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
156586c67379SJames Smart }
156686c67379SJames Smart 
156786c67379SJames Smart int
156886c67379SJames Smart lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport *vport,
156986c67379SJames Smart 			   struct fc_frame_header *fc_hdr)
157086c67379SJames Smart {
157186c67379SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
157286c67379SJames Smart 	struct lpfc_hba *phba = vport->phba;
157386c67379SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
157486c67379SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
157586c67379SJames Smart 	uint16_t xri;
157686c67379SJames Smart 	unsigned long iflag = 0;
157786c67379SJames Smart 
157886c67379SJames Smart 	xri = be16_to_cpu(fc_hdr->fh_ox_id);
157986c67379SJames 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 
15885e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
158986c67379SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, iflag);
159086c67379SJames Smart 
159186c67379SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
159286c67379SJames Smart 		ctxp->flag |= LPFC_NVMET_ABTS_RCV;
159386c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
159486c67379SJames Smart 
159586c67379SJames Smart 		lpfc_nvmeio_data(phba,
159686c67379SJames Smart 			"NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1597d6d189ceSBart Van Assche 			xri, raw_smp_processor_id(), 0);
159886c67379SJames Smart 
159986c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
160086c67379SJames Smart 				"6319 NVMET Rcv ABTS:acc xri x%x\n", xri);
160186c67379SJames Smart 
160286c67379SJames Smart 		rsp = &ctxp->ctx.fcp_req;
160386c67379SJames Smart 		nvmet_fc_rcv_fcp_abort(phba->targetport, rsp);
160486c67379SJames Smart 
160586c67379SJames Smart 		/* Respond with BA_ACC accordingly */
160686c67379SJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
160786c67379SJames Smart 		return 0;
160886c67379SJames Smart 	}
16095e5b511dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
161086c67379SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
161186c67379SJames Smart 
161286c67379SJames Smart 	lpfc_nvmeio_data(phba, "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1613d6d189ceSBart Van Assche 			 xri, raw_smp_processor_id(), 1);
161486c67379SJames Smart 
161586c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
161686c67379SJames Smart 			"6320 NVMET Rcv ABTS:rjt xri x%x\n", xri);
161786c67379SJames Smart 
161886c67379SJames Smart 	/* Respond with BA_RJT accordingly */
161986c67379SJames Smart 	lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 0);
162086c67379SJames Smart #endif
1621b06e13c3SJens Axboe 	return 0;
1622318083adSJames Smart }
1623318083adSJames Smart 
16246e8e1c14SJames Smart static void
16256e8e1c14SJames Smart lpfc_nvmet_wqfull_flush(struct lpfc_hba *phba, struct lpfc_queue *wq,
16266e8e1c14SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
16276e8e1c14SJames Smart {
16286e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
16296e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
16306e8e1c14SJames Smart 	struct lpfc_iocbq *next_nvmewqeq;
16316e8e1c14SJames Smart 	unsigned long iflags;
16326e8e1c14SJames Smart 	struct lpfc_wcqe_complete wcqe;
16336e8e1c14SJames Smart 	struct lpfc_wcqe_complete *wcqep;
16346e8e1c14SJames Smart 
16356e8e1c14SJames Smart 	pring = wq->pring;
16366e8e1c14SJames Smart 	wcqep = &wcqe;
16376e8e1c14SJames Smart 
16386e8e1c14SJames Smart 	/* Fake an ABORT error code back to cmpl routine */
16396e8e1c14SJames Smart 	memset(wcqep, 0, sizeof(struct lpfc_wcqe_complete));
16406e8e1c14SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, IOSTAT_LOCAL_REJECT);
16416e8e1c14SJames Smart 	wcqep->parameter = IOERR_ABORT_REQUESTED;
16426e8e1c14SJames Smart 
16436e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
16446e8e1c14SJames Smart 	list_for_each_entry_safe(nvmewqeq, next_nvmewqeq,
16456e8e1c14SJames Smart 				 &wq->wqfull_list, list) {
16466e8e1c14SJames Smart 		if (ctxp) {
16476e8e1c14SJames Smart 			/* Checking for a specific IO to flush */
16486e8e1c14SJames Smart 			if (nvmewqeq->context2 == ctxp) {
16496e8e1c14SJames Smart 				list_del(&nvmewqeq->list);
16506e8e1c14SJames Smart 				spin_unlock_irqrestore(&pring->ring_lock,
16516e8e1c14SJames Smart 						       iflags);
16526e8e1c14SJames Smart 				lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq,
16536e8e1c14SJames Smart 							  wcqep);
16546e8e1c14SJames Smart 				return;
16556e8e1c14SJames Smart 			}
16566e8e1c14SJames Smart 			continue;
16576e8e1c14SJames Smart 		} else {
16586e8e1c14SJames Smart 			/* Flush all IOs */
16596e8e1c14SJames Smart 			list_del(&nvmewqeq->list);
16606e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
16616e8e1c14SJames Smart 			lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq, wcqep);
16626e8e1c14SJames Smart 			spin_lock_irqsave(&pring->ring_lock, iflags);
16636e8e1c14SJames Smart 		}
16646e8e1c14SJames Smart 	}
16656e8e1c14SJames Smart 	if (!ctxp)
16666e8e1c14SJames Smart 		wq->q_flag &= ~HBA_NVMET_WQFULL;
16676e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
16686e8e1c14SJames Smart }
16696e8e1c14SJames Smart 
16706e8e1c14SJames Smart void
16716e8e1c14SJames Smart lpfc_nvmet_wqfull_process(struct lpfc_hba *phba,
16726e8e1c14SJames Smart 			  struct lpfc_queue *wq)
16736e8e1c14SJames Smart {
16746e8e1c14SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
16756e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
16766e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
16771fbf9742SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
16786e8e1c14SJames Smart 	unsigned long iflags;
16796e8e1c14SJames Smart 	int rc;
16806e8e1c14SJames Smart 
16816e8e1c14SJames Smart 	/*
16826e8e1c14SJames Smart 	 * Some WQE slots are available, so try to re-issue anything
16836e8e1c14SJames Smart 	 * on the WQ wqfull_list.
16846e8e1c14SJames Smart 	 */
16856e8e1c14SJames Smart 	pring = wq->pring;
16866e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
16876e8e1c14SJames Smart 	while (!list_empty(&wq->wqfull_list)) {
16886e8e1c14SJames Smart 		list_remove_head(&wq->wqfull_list, nvmewqeq, struct lpfc_iocbq,
16896e8e1c14SJames Smart 				 list);
16906e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
16911fbf9742SJames Smart 		ctxp = (struct lpfc_nvmet_rcv_ctx *)nvmewqeq->context2;
16921fbf9742SJames Smart 		rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
16936e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
16946e8e1c14SJames Smart 		if (rc == -EBUSY) {
16956e8e1c14SJames Smart 			/* WQ was full again, so put it back on the list */
16966e8e1c14SJames Smart 			list_add(&nvmewqeq->list, &wq->wqfull_list);
16976e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
16986e8e1c14SJames Smart 			return;
16996e8e1c14SJames Smart 		}
17006e8e1c14SJames Smart 	}
17016e8e1c14SJames Smart 	wq->q_flag &= ~HBA_NVMET_WQFULL;
17026e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
17036e8e1c14SJames Smart 
17046e8e1c14SJames Smart #endif
17056e8e1c14SJames Smart }
17066e8e1c14SJames Smart 
1707d613b6a7SJames Smart void
1708d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
1709d613b6a7SJames Smart {
17107d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1711d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
17126e8e1c14SJames Smart 	struct lpfc_queue *wq;
17136e8e1c14SJames Smart 	uint32_t qidx;
1714c41f5988SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(tport_unreg_cmp);
1715d613b6a7SJames Smart 
1716d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
1717d613b6a7SJames Smart 		return;
1718d613b6a7SJames Smart 	if (phba->targetport) {
1719d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1720cdb42becSJames Smart 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
1721cdb42becSJames Smart 			wq = phba->sli4_hba.hdwq[qidx].nvme_wq;
17226e8e1c14SJames Smart 			lpfc_nvmet_wqfull_flush(phba, wq, NULL);
17236e8e1c14SJames Smart 		}
1724c41f5988SEwan D. Milne 		tgtp->tport_unreg_cmp = &tport_unreg_cmp;
1725d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
17262a0fb340SJames Smart 		if (!wait_for_completion_timeout(tgtp->tport_unreg_cmp,
17272a0fb340SJames Smart 					msecs_to_jiffies(LPFC_NVMET_WAIT_TMO)))
17282a0fb340SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
17292a0fb340SJames Smart 					"6179 Unreg targetport %p timeout "
17302a0fb340SJames Smart 					"reached.\n", phba->targetport);
17316c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
1732d613b6a7SJames Smart 	}
1733d613b6a7SJames Smart 	phba->targetport = NULL;
1734166d7211SJames Smart #endif
1735d613b6a7SJames Smart }
1736d613b6a7SJames Smart 
1737d613b6a7SJames Smart /**
1738d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer - Process an unsolicited event data buffer
1739d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
1740d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
1741d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
1742d613b6a7SJames Smart  *
1743d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
1744d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
1745d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
1746d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
1747d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
1748d613b6a7SJames Smart  * of the discovery state machine.
1749d613b6a7SJames Smart  **/
1750d613b6a7SJames Smart static void
1751d613b6a7SJames Smart lpfc_nvmet_unsol_ls_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1752d613b6a7SJames Smart 			   struct hbq_dmabuf *nvmebuf)
1753d613b6a7SJames Smart {
17547d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1755d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1756d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
1757d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1758d613b6a7SJames Smart 	uint32_t *payload;
1759d613b6a7SJames Smart 	uint32_t size, oxid, sid, rc;
1760d613b6a7SJames Smart 
176168c9b55dSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
176268c9b55dSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
176368c9b55dSJames Smart 
1764cc74e31dSColin Ian King 	if (!phba->targetport) {
1765d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
176668c9b55dSJames Smart 				"6154 LS Drop IO x%x\n", oxid);
17672b65e182SJames Smart 		oxid = 0;
17682b65e182SJames Smart 		size = 0;
17692b65e182SJames Smart 		sid = 0;
1770547077a4SJames Smart 		ctxp = NULL;
1771d613b6a7SJames Smart 		goto dropit;
1772d613b6a7SJames Smart 	}
1773d613b6a7SJames Smart 
1774d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1775d613b6a7SJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
1776d613b6a7SJames Smart 	size = bf_get(lpfc_rcqe_length,  &nvmebuf->cq_event.cqe.rcqe_cmpl);
1777d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
1778d613b6a7SJames Smart 
1779d613b6a7SJames Smart 	ctxp = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx), GFP_ATOMIC);
1780d613b6a7SJames Smart 	if (ctxp == NULL) {
1781d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_drop);
1782d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1783d613b6a7SJames Smart 				"6155 LS Drop IO x%x: Alloc\n",
1784d613b6a7SJames Smart 				oxid);
1785d613b6a7SJames Smart dropit:
17862b65e182SJames Smart 		lpfc_nvmeio_data(phba, "NVMET LS  DROP: "
17872b65e182SJames Smart 				 "xri x%x sz %d from %06x\n",
17882b65e182SJames Smart 				 oxid, size, sid);
1789d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1790d613b6a7SJames Smart 		return;
1791d613b6a7SJames Smart 	}
1792d613b6a7SJames Smart 	ctxp->phba = phba;
1793d613b6a7SJames Smart 	ctxp->size = size;
1794d613b6a7SJames Smart 	ctxp->oxid = oxid;
1795d613b6a7SJames Smart 	ctxp->sid = sid;
1796d613b6a7SJames Smart 	ctxp->wqeq = NULL;
1797ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_LS_RCV;
1798ce1b591cSJames Smart 	ctxp->entry_cnt = 1;
1799d613b6a7SJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
18001fbf9742SJames Smart 	ctxp->hdwq = &phba->sli4_hba.hdwq[0];
18012b65e182SJames Smart 
18022b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS   RCV: xri x%x sz %d from %06x\n",
18032b65e182SJames Smart 			 oxid, size, sid);
1804d613b6a7SJames Smart 	/*
1805d613b6a7SJames Smart 	 * The calling sequence should be:
1806d613b6a7SJames Smart 	 * nvmet_fc_rcv_ls_req -> lpfc_nvmet_xmt_ls_rsp/cmp ->_req->done
1807d613b6a7SJames Smart 	 * lpfc_nvmet_xmt_ls_rsp_cmp should free the allocated ctxp.
1808d613b6a7SJames Smart 	 */
1809d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
1810d613b6a7SJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, &ctxp->ctx.ls_req,
1811d613b6a7SJames Smart 				 payload, size);
1812d613b6a7SJames Smart 
1813d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1814ce1b591cSJames Smart 			"6037 NVMET Unsol rcv: sz %d rc %d: %08x %08x %08x "
1815ce1b591cSJames Smart 			"%08x %08x %08x\n", size, rc,
1816d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
1817d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
18182b65e182SJames Smart 
1819d613b6a7SJames Smart 	if (rc == 0) {
1820d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
1821d613b6a7SJames Smart 		return;
1822d613b6a7SJames Smart 	}
18232b65e182SJames Smart 
18242b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  DROP: xri x%x sz %d from %06x\n",
18252b65e182SJames Smart 			 oxid, size, sid);
18262b65e182SJames Smart 
1827d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
1828d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1829d613b6a7SJames Smart 			"6156 LS Drop IO x%x: nvmet_fc_rcv_ls_req %d\n",
1830d613b6a7SJames Smart 			ctxp->oxid, rc);
1831d613b6a7SJames Smart 
1832d613b6a7SJames Smart 	/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
1833d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1834d613b6a7SJames Smart 
1835d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_ls_abort);
1836d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, sid, oxid);
1837166d7211SJames Smart #endif
1838d613b6a7SJames Smart }
1839d613b6a7SJames Smart 
1840472e146dSJames Smart static void
1841472e146dSJames Smart lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf)
1842472e146dSJames Smart {
1843472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1844472e146dSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
1845472e146dSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
1846472e146dSJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
1847472e146dSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
18484552e0f6SJames Smart 	uint32_t *payload, qno;
1849472e146dSJames Smart 	uint32_t rc;
1850472e146dSJames Smart 	unsigned long iflags;
1851472e146dSJames Smart 
1852472e146dSJames Smart 	if (!nvmebuf) {
1853472e146dSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1854472e146dSJames Smart 			"6159 process_rcv_fcp_req, nvmebuf is NULL, "
1855472e146dSJames Smart 			"oxid: x%x flg: x%x state: x%x\n",
1856472e146dSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
1857472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
1858472e146dSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
1859472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
1860472e146dSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1861472e146dSJames Smart 						 ctxp->oxid);
1862472e146dSJames Smart 		return;
1863472e146dSJames Smart 	}
1864472e146dSJames Smart 
1865472e146dSJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
1866472e146dSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1867472e146dSJames Smart 	/*
1868472e146dSJames Smart 	 * The calling sequence should be:
1869472e146dSJames Smart 	 * nvmet_fc_rcv_fcp_req->lpfc_nvmet_xmt_fcp_op/cmp- req->done
1870472e146dSJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
1871472e146dSJames Smart 	 * When we return from nvmet_fc_rcv_fcp_req, all relevant info
1872472e146dSJames Smart 	 * the NVME command / FC header is stored.
1873472e146dSJames Smart 	 * A buffer has already been reposted for this IO, so just free
1874472e146dSJames Smart 	 * the nvmebuf.
1875472e146dSJames Smart 	 */
1876472e146dSJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->ctx.fcp_req,
1877472e146dSJames Smart 				  payload, ctxp->size);
1878472e146dSJames Smart 	/* Process FCP command */
1879472e146dSJames Smart 	if (rc == 0) {
1880472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
18814552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
18824552e0f6SJames Smart 		if ((ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) ||
18834552e0f6SJames Smart 		    (nvmebuf != ctxp->rqb_buffer)) {
18844552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
18854552e0f6SJames Smart 			return;
18864552e0f6SJames Smart 		}
18874552e0f6SJames Smart 		ctxp->rqb_buffer = NULL;
18884552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
18894552e0f6SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
1890472e146dSJames Smart 		return;
1891472e146dSJames Smart 	}
1892472e146dSJames Smart 
1893472e146dSJames Smart 	/* Processing of FCP command is deferred */
1894472e146dSJames Smart 	if (rc == -EOVERFLOW) {
1895472e146dSJames Smart 		lpfc_nvmeio_data(phba, "NVMET RCV BUSY: xri x%x sz %d "
1896472e146dSJames Smart 				 "from %06x\n",
1897472e146dSJames Smart 				 ctxp->oxid, ctxp->size, ctxp->sid);
1898472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
1899472e146dSJames Smart 		atomic_inc(&tgtp->defer_fod);
19004552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
19014552e0f6SJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) {
19024552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
19034552e0f6SJames Smart 			return;
19044552e0f6SJames Smart 		}
19054552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
19064552e0f6SJames Smart 		/*
19074552e0f6SJames Smart 		 * Post a replacement DMA buffer to RQ and defer
19084552e0f6SJames Smart 		 * freeing rcv buffer till .defer_rcv callback
19094552e0f6SJames Smart 		 */
19104552e0f6SJames Smart 		qno = nvmebuf->idx;
19114552e0f6SJames Smart 		lpfc_post_rq_buffer(
19124552e0f6SJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
19134552e0f6SJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
1914472e146dSJames Smart 		return;
1915472e146dSJames Smart 	}
1916472e146dSJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
1917472e146dSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1918472e146dSJames Smart 			"2582 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
1919472e146dSJames Smart 			ctxp->oxid, rc,
1920472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_in),
1921472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_out),
1922472e146dSJames Smart 			atomic_read(&tgtp->xmt_fcp_release));
1923472e146dSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
1924472e146dSJames Smart 			 ctxp->oxid, ctxp->size, ctxp->sid);
1925472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflags);
1926472e146dSJames Smart 	lpfc_nvmet_defer_release(phba, ctxp);
1927472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
1928472e146dSJames Smart 	lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
1929472e146dSJames Smart #endif
1930472e146dSJames Smart }
1931472e146dSJames Smart 
1932472e146dSJames Smart static void
1933472e146dSJames Smart lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *work)
1934472e146dSJames Smart {
1935472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1936472e146dSJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf =
1937472e146dSJames Smart 		container_of(work, struct lpfc_nvmet_ctxbuf, defer_work);
1938472e146dSJames Smart 
1939472e146dSJames Smart 	lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
1940472e146dSJames Smart #endif
1941472e146dSJames Smart }
1942472e146dSJames Smart 
194366d7ce93SDick Kennedy static struct lpfc_nvmet_ctxbuf *
194466d7ce93SDick Kennedy lpfc_nvmet_replenish_context(struct lpfc_hba *phba,
194566d7ce93SDick Kennedy 			     struct lpfc_nvmet_ctx_info *current_infop)
194666d7ce93SDick Kennedy {
19472299e432SDick Kennedy #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
194866d7ce93SDick Kennedy 	struct lpfc_nvmet_ctxbuf *ctx_buf = NULL;
194966d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *get_infop;
195066d7ce93SDick Kennedy 	int i;
195166d7ce93SDick Kennedy 
195266d7ce93SDick Kennedy 	/*
195366d7ce93SDick Kennedy 	 * The current_infop for the MRQ a NVME command IU was received
195466d7ce93SDick Kennedy 	 * on is empty. Our goal is to replenish this MRQs context
195566d7ce93SDick Kennedy 	 * list from a another CPUs.
195666d7ce93SDick Kennedy 	 *
195766d7ce93SDick Kennedy 	 * First we need to pick a context list to start looking on.
195866d7ce93SDick Kennedy 	 * nvmet_ctx_start_cpu has available context the last time
195966d7ce93SDick Kennedy 	 * we needed to replenish this CPU where nvmet_ctx_next_cpu
196066d7ce93SDick Kennedy 	 * is just the next sequential CPU for this MRQ.
196166d7ce93SDick Kennedy 	 */
196266d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_start_cpu)
196366d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_start_cpu;
196466d7ce93SDick Kennedy 	else
196566d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_next_cpu;
196666d7ce93SDick Kennedy 
1967222e9239SJames Smart 	for (i = 0; i < phba->sli4_hba.num_possible_cpu; i++) {
196866d7ce93SDick Kennedy 		if (get_infop == current_infop) {
196966d7ce93SDick Kennedy 			get_infop = get_infop->nvmet_ctx_next_cpu;
197066d7ce93SDick Kennedy 			continue;
197166d7ce93SDick Kennedy 		}
197266d7ce93SDick Kennedy 		spin_lock(&get_infop->nvmet_ctx_list_lock);
197366d7ce93SDick Kennedy 
197466d7ce93SDick Kennedy 		/* Just take the entire context list, if there are any */
197566d7ce93SDick Kennedy 		if (get_infop->nvmet_ctx_list_cnt) {
197666d7ce93SDick Kennedy 			list_splice_init(&get_infop->nvmet_ctx_list,
197766d7ce93SDick Kennedy 				    &current_infop->nvmet_ctx_list);
197866d7ce93SDick Kennedy 			current_infop->nvmet_ctx_list_cnt =
197966d7ce93SDick Kennedy 				get_infop->nvmet_ctx_list_cnt - 1;
198066d7ce93SDick Kennedy 			get_infop->nvmet_ctx_list_cnt = 0;
198166d7ce93SDick Kennedy 			spin_unlock(&get_infop->nvmet_ctx_list_lock);
198266d7ce93SDick Kennedy 
198366d7ce93SDick Kennedy 			current_infop->nvmet_ctx_start_cpu = get_infop;
198466d7ce93SDick Kennedy 			list_remove_head(&current_infop->nvmet_ctx_list,
198566d7ce93SDick Kennedy 					 ctx_buf, struct lpfc_nvmet_ctxbuf,
198666d7ce93SDick Kennedy 					 list);
198766d7ce93SDick Kennedy 			return ctx_buf;
198866d7ce93SDick Kennedy 		}
198966d7ce93SDick Kennedy 
199066d7ce93SDick Kennedy 		/* Otherwise, move on to the next CPU for this MRQ */
199166d7ce93SDick Kennedy 		spin_unlock(&get_infop->nvmet_ctx_list_lock);
199266d7ce93SDick Kennedy 		get_infop = get_infop->nvmet_ctx_next_cpu;
199366d7ce93SDick Kennedy 	}
199466d7ce93SDick Kennedy 
19952299e432SDick Kennedy #endif
199666d7ce93SDick Kennedy 	/* Nothing found, all contexts for the MRQ are in-flight */
199766d7ce93SDick Kennedy 	return NULL;
199866d7ce93SDick Kennedy }
199966d7ce93SDick Kennedy 
2000d613b6a7SJames Smart /**
2001d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
2002d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
200366d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2004d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
2005d613b6a7SJames Smart  *
2006d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
2007d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
2008d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
2009d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
2010d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
2011d613b6a7SJames Smart  * of the discovery state machine.
2012d613b6a7SJames Smart  **/
2013d613b6a7SJames Smart static void
2014d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
201566d7ce93SDick Kennedy 			    uint32_t idx,
2016d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
2017d613b6a7SJames Smart 			    uint64_t isr_timestamp)
2018d613b6a7SJames Smart {
2019d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
2020d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2021d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
20226c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
202366d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *current_infop;
2024472e146dSJames Smart 	uint32_t size, oxid, sid, qno;
20256c621a22SJames Smart 	unsigned long iflag;
202666d7ce93SDick Kennedy 	int current_cpu;
2027d613b6a7SJames Smart 
202873626173SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_TARGET_FC))
202973626173SArnd Bergmann 		return;
203073626173SArnd Bergmann 
20316c621a22SJames Smart 	ctx_buf = NULL;
2032d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
2033d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
20346c621a22SJames Smart 				"6157 NVMET FCP Drop IO\n");
2035472e146dSJames Smart 		if (nvmebuf)
2036472e146dSJames Smart 			lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2037472e146dSJames Smart 		return;
2038d613b6a7SJames Smart 	}
2039d613b6a7SJames Smart 
204066d7ce93SDick Kennedy 	/*
204166d7ce93SDick Kennedy 	 * Get a pointer to the context list for this MRQ based on
204266d7ce93SDick Kennedy 	 * the CPU this MRQ IRQ is associated with. If the CPU association
204366d7ce93SDick Kennedy 	 * changes from our initial assumption, the context list could
204466d7ce93SDick Kennedy 	 * be empty, thus it would need to be replenished with the
204566d7ce93SDick Kennedy 	 * context list from another CPU for this MRQ.
204666d7ce93SDick Kennedy 	 */
2047d6d189ceSBart Van Assche 	current_cpu = raw_smp_processor_id();
204866d7ce93SDick Kennedy 	current_infop = lpfc_get_ctx_list(phba, current_cpu, idx);
204966d7ce93SDick Kennedy 	spin_lock_irqsave(&current_infop->nvmet_ctx_list_lock, iflag);
205066d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_list_cnt) {
205166d7ce93SDick Kennedy 		list_remove_head(&current_infop->nvmet_ctx_list,
20526c621a22SJames Smart 				 ctx_buf, struct lpfc_nvmet_ctxbuf, list);
205366d7ce93SDick Kennedy 		current_infop->nvmet_ctx_list_cnt--;
2054966bb5b7SJames Smart 	} else {
205566d7ce93SDick Kennedy 		ctx_buf = lpfc_nvmet_replenish_context(phba, current_infop);
20566c621a22SJames Smart 	}
205766d7ce93SDick Kennedy 	spin_unlock_irqrestore(&current_infop->nvmet_ctx_list_lock, iflag);
20586c621a22SJames Smart 
2059a8cf5dfeSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
2060a8cf5dfeSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2061a8cf5dfeSJames Smart 	size = nvmebuf->bytes_recv;
2062a8cf5dfeSJames Smart 
2063a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2064a8cf5dfeSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_RCV) {
206563df6d63SJames Smart 		if (current_cpu < LPFC_CHECK_CPU_CNT) {
206663df6d63SJames Smart 			if (idx != current_cpu)
206763df6d63SJames Smart 				lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
206863df6d63SJames Smart 						"6703 CPU Check rcv: "
206963df6d63SJames Smart 						"cpu %d expect %d\n",
207063df6d63SJames Smart 						current_cpu, idx);
207163df6d63SJames Smart 			phba->sli4_hba.hdwq[idx].cpucheck_rcv_io[current_cpu]++;
207263df6d63SJames Smart 		}
2073a8cf5dfeSJames Smart 	}
2074a8cf5dfeSJames Smart #endif
2075a8cf5dfeSJames Smart 
2076a8cf5dfeSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d CPU %02x\n",
2077d6d189ceSBart Van Assche 			 oxid, size, raw_smp_processor_id());
2078a8cf5dfeSJames Smart 
2079411de511SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2080411de511SJames Smart 
20816c621a22SJames Smart 	if (!ctx_buf) {
2082a8cf5dfeSJames Smart 		/* Queue this NVME IO to process later */
2083a8cf5dfeSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
2084a8cf5dfeSJames Smart 		list_add_tail(&nvmebuf->hbuf.list,
2085a8cf5dfeSJames Smart 			      &phba->sli4_hba.lpfc_nvmet_io_wait_list);
2086a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt++;
2087a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_total++;
2088a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
2089a8cf5dfeSJames Smart 				       iflag);
2090a8cf5dfeSJames Smart 
2091a8cf5dfeSJames Smart 		/* Post a brand new DMA buffer to RQ */
2092a8cf5dfeSJames Smart 		qno = nvmebuf->idx;
2093a8cf5dfeSJames Smart 		lpfc_post_rq_buffer(
2094a8cf5dfeSJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
2095a8cf5dfeSJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2096411de511SJames Smart 
2097411de511SJames Smart 		atomic_inc(&tgtp->defer_ctx);
2098a8cf5dfeSJames Smart 		return;
20996c621a22SJames Smart 	}
2100d613b6a7SJames Smart 
2101d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
2102d613b6a7SJames Smart 
21036c621a22SJames Smart 	ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
2104ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_FREE) {
2105ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2106ce1b591cSJames Smart 				"6414 NVMET Context corrupt %d %d oxid x%x\n",
2107ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
2108ce1b591cSJames Smart 	}
2109d613b6a7SJames Smart 	ctxp->wqeq = NULL;
2110d613b6a7SJames Smart 	ctxp->txrdy = NULL;
2111d613b6a7SJames Smart 	ctxp->offset = 0;
2112d613b6a7SJames Smart 	ctxp->phba = phba;
2113d613b6a7SJames Smart 	ctxp->size = size;
2114d613b6a7SJames Smart 	ctxp->oxid = oxid;
2115d613b6a7SJames Smart 	ctxp->sid = sid;
211666d7ce93SDick Kennedy 	ctxp->idx = idx;
2117d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_RCV;
2118d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
2119d613b6a7SJames Smart 	ctxp->flag = 0;
21206c621a22SJames Smart 	ctxp->ctxbuf = ctx_buf;
2121cbc5de1bSJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
21221fbf9742SJames Smart 	ctxp->hdwq = NULL;
21232b7824d0SJames Smart 	spin_lock_init(&ctxp->ctxlock);
2124d613b6a7SJames Smart 
21252b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2126c8a4ce0bSDick Kennedy 	if (isr_timestamp) {
21272b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
21282b65e182SJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
21292b65e182SJames Smart 		ctxp->ts_nvme_data = 0;
21302b65e182SJames Smart 		ctxp->ts_data_wqput = 0;
21312b65e182SJames Smart 		ctxp->ts_isr_data = 0;
21322b65e182SJames Smart 		ctxp->ts_data_nvme = 0;
21332b65e182SJames Smart 		ctxp->ts_nvme_status = 0;
21342b65e182SJames Smart 		ctxp->ts_status_wqput = 0;
21352b65e182SJames Smart 		ctxp->ts_isr_status = 0;
21362b65e182SJames Smart 		ctxp->ts_status_nvme = 0;
2137c8a4ce0bSDick Kennedy 	} else {
2138c8a4ce0bSDick Kennedy 		ctxp->ts_cmd_nvme = 0;
21392b65e182SJames Smart 	}
21402b65e182SJames Smart #endif
21412b65e182SJames Smart 
2142d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
2143472e146dSJames Smart 	lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2144d613b6a7SJames Smart }
2145d613b6a7SJames Smart 
2146d613b6a7SJames Smart /**
2147d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_event - Process an unsolicited event from an nvme nport
2148d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
2149d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
2150d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2151d613b6a7SJames Smart  *
2152d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2153d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2154d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2155d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer() after properly set up the buffer from the
2156d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2157d613b6a7SJames Smart  **/
2158d613b6a7SJames Smart void
2159d613b6a7SJames Smart lpfc_nvmet_unsol_ls_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2160d613b6a7SJames Smart 			  struct lpfc_iocbq *piocb)
2161d613b6a7SJames Smart {
2162d613b6a7SJames Smart 	struct lpfc_dmabuf *d_buf;
2163d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf;
2164d613b6a7SJames Smart 
2165d613b6a7SJames Smart 	d_buf = piocb->context2;
2166d613b6a7SJames Smart 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2167d613b6a7SJames Smart 
2168d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
2169d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
2170d613b6a7SJames Smart 		return;
2171d613b6a7SJames Smart 	}
2172d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_buffer(phba, pring, nvmebuf);
2173d613b6a7SJames Smart }
2174d613b6a7SJames Smart 
2175d613b6a7SJames Smart /**
2176d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
2177d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
217866d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2179d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2180d613b6a7SJames Smart  *
2181d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2182d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2183d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2184d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
2185d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2186d613b6a7SJames Smart  **/
2187d613b6a7SJames Smart void
2188d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
218966d7ce93SDick Kennedy 			   uint32_t idx,
2190d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
2191d613b6a7SJames Smart 			   uint64_t isr_timestamp)
2192d613b6a7SJames Smart {
2193d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
21946c621a22SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2195d613b6a7SJames Smart 		return;
2196d613b6a7SJames Smart 	}
219766d7ce93SDick Kennedy 	lpfc_nvmet_unsol_fcp_buffer(phba, idx, nvmebuf,
2198d613b6a7SJames Smart 				    isr_timestamp);
2199d613b6a7SJames Smart }
2200d613b6a7SJames Smart 
2201d613b6a7SJames Smart /**
2202d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
2203d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
2204d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
2205d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
2206d613b6a7SJames Smart  * @rspsize: size of the NVME command.
2207d613b6a7SJames Smart  *
2208d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
2209d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
2210d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
2211d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
2212d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
2213d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
2214d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
2215d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
2216d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
2217d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
2218d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
2219d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
2220d613b6a7SJames Smart  * reference for the command's callback function to access later.
2221d613b6a7SJames Smart  *
2222d613b6a7SJames Smart  * Return code
2223d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
2224d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
2225d613b6a7SJames Smart  **/
2226d613b6a7SJames Smart static struct lpfc_iocbq *
2227d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
2228d613b6a7SJames Smart 		       struct lpfc_nvmet_rcv_ctx *ctxp,
2229d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
2230d613b6a7SJames Smart {
2231d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2232d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2233205e8240SJames Smart 	union lpfc_wqe128 *wqe;
2234d613b6a7SJames Smart 
2235d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2236d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2237ce1b591cSJames Smart 				"6104 NVMET prep LS wqe: link err: "
2238ce1b591cSJames Smart 				"NPORT x%x oxid:x%x ste %d\n",
2239ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2240d613b6a7SJames Smart 		return NULL;
2241d613b6a7SJames Smart 	}
2242d613b6a7SJames Smart 
2243d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
2244d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
2245d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2246d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2247ce1b591cSJames Smart 				"6105 NVMET prep LS wqe: No WQE: "
2248ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2249ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2250d613b6a7SJames Smart 		return NULL;
2251d613b6a7SJames Smart 	}
2252d613b6a7SJames Smart 
2253d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2254d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2255d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2256d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2257d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2258ce1b591cSJames Smart 				"6106 NVMET prep LS wqe: No ndlp: "
2259ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2260ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2261d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2262d613b6a7SJames Smart 	}
2263d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
2264d613b6a7SJames Smart 
2265d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
2266d613b6a7SJames Smart 	nvmewqe->context1 = lpfc_nlp_get(ndlp);
2267d613b6a7SJames Smart 	if (nvmewqe->context1 == NULL)
2268d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2269d613b6a7SJames Smart 	nvmewqe->context2 = ctxp;
2270d613b6a7SJames Smart 
2271d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
2272d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
2273d613b6a7SJames Smart 
2274d613b6a7SJames Smart 	/* Words 0 - 2 */
2275d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2276d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
2277d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
2278d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
2279d613b6a7SJames Smart 
2280d613b6a7SJames Smart 	/* Word 3 */
2281d613b6a7SJames Smart 
2282d613b6a7SJames Smart 	/* Word 4 */
2283d613b6a7SJames Smart 
2284d613b6a7SJames Smart 	/* Word 5 */
2285d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
2286d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
2287d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
22888b361639SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
2289d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
2290d613b6a7SJames Smart 
2291d613b6a7SJames Smart 	/* Word 6 */
2292d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
2293d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2294d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
2295d613b6a7SJames Smart 
2296d613b6a7SJames Smart 	/* Word 7 */
2297d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
2298d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
2299d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
2300d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
2301d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
2302d613b6a7SJames Smart 
2303d613b6a7SJames Smart 	/* Word 8 */
2304d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
2305d613b6a7SJames Smart 
2306d613b6a7SJames Smart 	/* Word 9 */
2307d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
2308d613b6a7SJames Smart 	/* Needs to be set by caller */
2309d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
2310d613b6a7SJames Smart 
2311d613b6a7SJames Smart 	/* Word 10 */
2312d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
2313d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2314d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
2315d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
2316d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
2317d613b6a7SJames Smart 
2318d613b6a7SJames Smart 	/* Word 11 */
2319d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
2320d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
2321d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
2322d613b6a7SJames Smart 	       OTHER_COMMAND);
2323d613b6a7SJames Smart 
2324d613b6a7SJames Smart 	/* Word 12 */
2325d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
2326d613b6a7SJames Smart 
2327d613b6a7SJames Smart 	nvmewqe->retry = 1;
2328d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2329d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2330d613b6a7SJames Smart 	nvmewqe->iocb_flag |= LPFC_IO_NVME_LS;
2331d613b6a7SJames Smart 
2332ce1b591cSJames Smart 	/* Xmit NVMET response to remote NPORT <did> */
2333d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2334ce1b591cSJames Smart 			"6039 Xmit NVMET LS response to remote "
2335d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
2336d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
2337d613b6a7SJames Smart 			rspsize);
2338d613b6a7SJames Smart 	return nvmewqe;
2339d613b6a7SJames Smart 
2340d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
2341d613b6a7SJames Smart 	nvmewqe->context2 = NULL;
2342d613b6a7SJames Smart 	nvmewqe->context3 = NULL;
2343d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
2344d613b6a7SJames Smart 	return NULL;
2345d613b6a7SJames Smart }
2346d613b6a7SJames Smart 
2347d613b6a7SJames Smart 
2348d613b6a7SJames Smart static struct lpfc_iocbq *
2349d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
2350d613b6a7SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
2351d613b6a7SJames Smart {
2352d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->ctx.fcp_req;
2353d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2354d613b6a7SJames Smart 	struct sli4_sge *sgl;
2355d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2356d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2357d613b6a7SJames Smart 	struct scatterlist *sgel;
2358d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
23590bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
2360d613b6a7SJames Smart 	uint32_t *txrdy;
2361d613b6a7SJames Smart 	dma_addr_t physaddr;
2362d613b6a7SJames Smart 	int i, cnt;
23630bc2b7c5SJames Smart 	int do_pbde;
2364d613b6a7SJames Smart 	int xc = 1;
2365d613b6a7SJames Smart 
2366d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2367d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2368ce1b591cSJames Smart 				"6107 NVMET prep FCP wqe: link err:"
2369ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2370ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2371d613b6a7SJames Smart 		return NULL;
2372d613b6a7SJames Smart 	}
2373d613b6a7SJames Smart 
2374d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2375d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2376d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2377d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2378d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2379ce1b591cSJames Smart 				"6108 NVMET prep FCP wqe: no ndlp: "
2380ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2381ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2382d613b6a7SJames Smart 		return NULL;
2383d613b6a7SJames Smart 	}
2384d613b6a7SJames Smart 
238581e6a637SJames Smart 	if (rsp->sg_cnt > lpfc_tgttemplate.max_sgl_segments) {
2386d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2387ce1b591cSJames Smart 				"6109 NVMET prep FCP wqe: seg cnt err: "
2388ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d cnt %d\n",
2389ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state,
2390ce1b591cSJames Smart 				phba->cfg_nvme_seg_cnt);
2391d613b6a7SJames Smart 		return NULL;
2392d613b6a7SJames Smart 	}
2393d613b6a7SJames Smart 
2394d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2395d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
2396d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2397d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
23986c621a22SJames Smart 		nvmewqe = ctxp->ctxbuf->iocbq;
2399d613b6a7SJames Smart 		if (nvmewqe == NULL) {
2400d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2401ce1b591cSJames Smart 					"6110 NVMET prep FCP wqe: No "
2402ce1b591cSJames Smart 					"WQE: NPORT x%x oxid x%x ste %d\n",
2403ce1b591cSJames Smart 					ctxp->sid, ctxp->oxid, ctxp->state);
2404d613b6a7SJames Smart 			return NULL;
2405d613b6a7SJames Smart 		}
2406d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
2407d613b6a7SJames Smart 		xc = 0; /* create new XRI */
2408d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
2409d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
2410d613b6a7SJames Smart 	}
2411d613b6a7SJames Smart 
2412d613b6a7SJames Smart 	/* Sanity check */
2413d613b6a7SJames Smart 	if (((ctxp->state == LPFC_NVMET_STE_RCV) &&
2414d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
2415ce1b591cSJames Smart 	    (ctxp->state == LPFC_NVMET_STE_DATA)) {
2416205e8240SJames Smart 		wqe = &nvmewqe->wqe;
2417d613b6a7SJames Smart 	} else {
2418d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2419ce1b591cSJames Smart 				"6111 Wrong state NVMET FCP: %d  cnt %d\n",
2420ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt);
2421d613b6a7SJames Smart 		return NULL;
2422d613b6a7SJames Smart 	}
2423d613b6a7SJames Smart 
24246c621a22SJames Smart 	sgl  = (struct sli4_sge *)ctxp->ctxbuf->sglq->sgl;
2425d613b6a7SJames Smart 	switch (rsp->op) {
2426d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
2427d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
2428bd3061baSJames Smart 		/* From the tsend template, initialize words 7 - 11 */
2429bd3061baSJames Smart 		memcpy(&wqe->words[7],
2430bd3061baSJames Smart 		       &lpfc_tsend_cmd_template.words[7],
2431bd3061baSJames Smart 		       sizeof(uint32_t) * 5);
2432bd3061baSJames Smart 
2433d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2434d613b6a7SJames Smart 		sgel = &rsp->sg[0];
2435d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2436d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2437d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
2438d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
2439d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
2440d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2441d613b6a7SJames Smart 
2442d613b6a7SJames Smart 		/* Word 3 */
2443d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
2444d613b6a7SJames Smart 
2445d613b6a7SJames Smart 		/* Word 4 */
2446d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
2447d613b6a7SJames Smart 
2448d613b6a7SJames Smart 		/* Word 5 */
2449bd3061baSJames Smart 		wqe->fcp_tsend.reserved = 0;
2450d613b6a7SJames Smart 
2451d613b6a7SJames Smart 		/* Word 6 */
2452d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
2453d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2454d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
2455d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2456d613b6a7SJames Smart 
2457bd3061baSJames Smart 		/* Word 7 - set ar later */
2458d613b6a7SJames Smart 
2459d613b6a7SJames Smart 		/* Word 8 */
2460d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
2461d613b6a7SJames Smart 
2462d613b6a7SJames Smart 		/* Word 9 */
2463d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
2464d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
2465d613b6a7SJames Smart 
2466bd3061baSJames Smart 		/* Word 10 - set wqes later, in template xc=1 */
2467bd3061baSJames Smart 		if (!xc)
2468bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 0);
2469d613b6a7SJames Smart 
2470bd3061baSJames Smart 		/* Word 11 - set sup, irsp, irsplen later */
2471bd3061baSJames Smart 		do_pbde = 0;
2472d613b6a7SJames Smart 
2473d613b6a7SJames Smart 		/* Word 12 */
2474d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2475d613b6a7SJames Smart 
2476d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
2477d613b6a7SJames Smart 		sgl->addr_hi = 0;
2478d613b6a7SJames Smart 		sgl->addr_lo = 0;
2479d613b6a7SJames Smart 		sgl->word2 = 0;
2480d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2481d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2482d613b6a7SJames Smart 		sgl->sge_len = 0;
2483d613b6a7SJames Smart 		sgl++;
2484d613b6a7SJames Smart 		sgl->addr_hi = 0;
2485d613b6a7SJames Smart 		sgl->addr_lo = 0;
2486d613b6a7SJames Smart 		sgl->word2 = 0;
2487d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2488d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2489d613b6a7SJames Smart 		sgl->sge_len = 0;
2490d613b6a7SJames Smart 		sgl++;
2491d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
2492d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
2493bd3061baSJames Smart 
2494bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2495bd3061baSJames Smart 
249620aefac3SJames Smart 			if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
249720aefac3SJames Smart 				if (ndlp->nlp_flag & NLP_SUPPRESS_RSP)
249820aefac3SJames Smart 					bf_set(wqe_sup,
249920aefac3SJames Smart 					       &wqe->fcp_tsend.wqe_com, 1);
2500d613b6a7SJames Smart 			} else {
2501d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
2502d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
2503d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
2504d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
2505d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
2506d613b6a7SJames Smart 				       rsp->rsplen);
2507d613b6a7SJames Smart 			}
2508d613b6a7SJames Smart 		} else {
2509d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
2510d613b6a7SJames Smart 
2511bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2512d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
2513d613b6a7SJames Smart 		}
2514d613b6a7SJames Smart 		break;
2515d613b6a7SJames Smart 
2516d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
2517bd3061baSJames Smart 		/* From the treceive template, initialize words 3 - 11 */
2518bd3061baSJames Smart 		memcpy(&wqe->words[3],
2519bd3061baSJames Smart 		       &lpfc_treceive_cmd_template.words[3],
2520bd3061baSJames Smart 		       sizeof(uint32_t) * 9);
2521bd3061baSJames Smart 
2522d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2523771db5c0SRomain Perier 		txrdy = dma_pool_alloc(phba->txrdy_payload_pool,
2524d613b6a7SJames Smart 				       GFP_KERNEL, &physaddr);
2525d613b6a7SJames Smart 		if (!txrdy) {
2526d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2527d613b6a7SJames Smart 					"6041 Bad txrdy buffer: oxid x%x\n",
2528d613b6a7SJames Smart 					ctxp->oxid);
2529d613b6a7SJames Smart 			return NULL;
2530d613b6a7SJames Smart 		}
2531d613b6a7SJames Smart 		ctxp->txrdy = txrdy;
2532d613b6a7SJames Smart 		ctxp->txrdy_phys = physaddr;
2533d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2534d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = TXRDY_PAYLOAD_LEN;
2535d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrLow =
2536d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2537d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrHigh =
2538d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2539d613b6a7SJames Smart 
2540d613b6a7SJames Smart 		/* Word 4 */
2541d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
2542d613b6a7SJames Smart 
2543d613b6a7SJames Smart 		/* Word 6 */
2544d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
2545d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2546d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
2547d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2548d613b6a7SJames Smart 
2549d613b6a7SJames Smart 		/* Word 7 */
2550d613b6a7SJames Smart 
2551d613b6a7SJames Smart 		/* Word 8 */
2552d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
2553d613b6a7SJames Smart 
2554d613b6a7SJames Smart 		/* Word 9 */
2555d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
2556d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
2557d613b6a7SJames Smart 
2558bd3061baSJames Smart 		/* Word 10 - in template xc=1 */
2559bd3061baSJames Smart 		if (!xc)
2560bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, 0);
2561d613b6a7SJames Smart 
2562bd3061baSJames Smart 		/* Word 11 - set pbde later */
2563414abe0aSJames Smart 		if (phba->cfg_enable_pbde) {
2564bd3061baSJames Smart 			do_pbde = 1;
2565bd3061baSJames Smart 		} else {
2566bd3061baSJames Smart 			bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 0);
2567bd3061baSJames Smart 			do_pbde = 0;
2568bd3061baSJames Smart 		}
2569d613b6a7SJames Smart 
2570d613b6a7SJames Smart 		/* Word 12 */
2571d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2572d613b6a7SJames Smart 
2573d613b6a7SJames Smart 		/* Setup 1 TXRDY and 1 SKIP SGE */
2574d613b6a7SJames Smart 		txrdy[0] = 0;
2575d613b6a7SJames Smart 		txrdy[1] = cpu_to_be32(rsp->transfer_length);
2576d613b6a7SJames Smart 		txrdy[2] = 0;
2577d613b6a7SJames Smart 
2578d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
2579d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
2580d613b6a7SJames Smart 		sgl->word2 = 0;
2581d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2582d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2583d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(TXRDY_PAYLOAD_LEN);
2584d613b6a7SJames Smart 		sgl++;
2585d613b6a7SJames Smart 		sgl->addr_hi = 0;
2586d613b6a7SJames Smart 		sgl->addr_lo = 0;
2587d613b6a7SJames Smart 		sgl->word2 = 0;
2588d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2589d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2590d613b6a7SJames Smart 		sgl->sge_len = 0;
2591d613b6a7SJames Smart 		sgl++;
2592d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
2593d613b6a7SJames Smart 		break;
2594d613b6a7SJames Smart 
2595d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
2596bd3061baSJames Smart 		/* From the treceive template, initialize words 4 - 11 */
2597bd3061baSJames Smart 		memcpy(&wqe->words[4],
2598bd3061baSJames Smart 		       &lpfc_trsp_cmd_template.words[4],
2599bd3061baSJames Smart 		       sizeof(uint32_t) * 8);
2600bd3061baSJames Smart 
2601d613b6a7SJames Smart 		/* Words 0 - 2 */
2602d613b6a7SJames Smart 		physaddr = rsp->rspdma;
2603d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2604d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
2605d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
2606d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2607d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
2608d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2609d613b6a7SJames Smart 
2610d613b6a7SJames Smart 		/* Word 3 */
2611d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
2612d613b6a7SJames Smart 
2613d613b6a7SJames Smart 		/* Word 6 */
2614d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
2615d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2616d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
2617d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2618d613b6a7SJames Smart 
2619d613b6a7SJames Smart 		/* Word 7 */
2620d613b6a7SJames Smart 
2621d613b6a7SJames Smart 		/* Word 8 */
2622d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
2623d613b6a7SJames Smart 
2624d613b6a7SJames Smart 		/* Word 9 */
2625d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
2626d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
2627d613b6a7SJames Smart 
2628d613b6a7SJames Smart 		/* Word 10 */
2629bd3061baSJames Smart 		if (xc)
2630bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 1);
2631d613b6a7SJames Smart 
2632d613b6a7SJames Smart 		/* Word 11 */
2633bd3061baSJames Smart 		/* In template wqes=0 irsp=0 irsplen=0 - good response */
2634bd3061baSJames Smart 		if (rsp->rsplen != LPFC_NVMET_SUCCESS_LEN) {
2635bd3061baSJames Smart 			/* Bad response - embed it */
2636d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
2637d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
2638d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
2639d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
2640d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
2641d613b6a7SJames Smart 		}
2642bd3061baSJames Smart 		do_pbde = 0;
2643bd3061baSJames Smart 
2644bd3061baSJames Smart 		/* Word 12 */
2645bd3061baSJames Smart 		wqe->fcp_trsp.rsvd_12_15[0] = 0;
2646d613b6a7SJames Smart 
2647d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
2648d613b6a7SJames Smart 		rsp->sg_cnt = 0;
2649d613b6a7SJames Smart 		sgl->word2 = 0;
2650d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
2651d613b6a7SJames Smart 		break;
2652d613b6a7SJames Smart 
2653d613b6a7SJames Smart 	default:
2654d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2655d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
2656d613b6a7SJames Smart 				rsp->op);
2657d613b6a7SJames Smart 		return NULL;
2658d613b6a7SJames Smart 	}
2659d613b6a7SJames Smart 
2660d613b6a7SJames Smart 	nvmewqe->retry = 1;
2661d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2662d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2663d613b6a7SJames Smart 	nvmewqe->context1 = ndlp;
2664d613b6a7SJames Smart 
266546e8e475SMing Lei 	for_each_sg(rsp->sg, sgel, rsp->sg_cnt, i) {
2666d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2667d613b6a7SJames Smart 		cnt = sg_dma_len(sgel);
2668d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
2669d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
2670d613b6a7SJames Smart 		sgl->word2 = 0;
2671d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2672d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
2673d613b6a7SJames Smart 		if ((i+1) == rsp->sg_cnt)
2674d613b6a7SJames Smart 			bf_set(lpfc_sli4_sge_last, sgl, 1);
2675d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2676d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(cnt);
2677414abe0aSJames Smart 		if (i == 0) {
26780bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)&wqe->words[13];
2679414abe0aSJames Smart 			if (do_pbde) {
26800bc2b7c5SJames Smart 				/* Words 13-15  (PBDE) */
26810bc2b7c5SJames Smart 				bde->addrLow = sgl->addr_lo;
26820bc2b7c5SJames Smart 				bde->addrHigh = sgl->addr_hi;
26830bc2b7c5SJames Smart 				bde->tus.f.bdeSize =
26840bc2b7c5SJames Smart 					le32_to_cpu(sgl->sge_len);
26850bc2b7c5SJames Smart 				bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
26860bc2b7c5SJames Smart 				bde->tus.w = cpu_to_le32(bde->tus.w);
2687414abe0aSJames Smart 			} else {
2688414abe0aSJames Smart 				memset(bde, 0, sizeof(struct ulp_bde64));
2689414abe0aSJames Smart 			}
26900bc2b7c5SJames Smart 		}
2691d613b6a7SJames Smart 		sgl++;
2692d613b6a7SJames Smart 		ctxp->offset += cnt;
2693d613b6a7SJames Smart 	}
2694ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_DATA;
2695ce1b591cSJames Smart 	ctxp->entry_cnt++;
2696d613b6a7SJames Smart 	return nvmewqe;
2697d613b6a7SJames Smart }
2698d613b6a7SJames Smart 
2699d613b6a7SJames Smart /**
2700d613b6a7SJames Smart  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
2701d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
2702d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
2703d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
2704d613b6a7SJames Smart  *
2705d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
2706d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
2707d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
2708d613b6a7SJames Smart  **/
2709d613b6a7SJames Smart static void
2710d613b6a7SJames Smart lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2711d613b6a7SJames Smart 			     struct lpfc_wcqe_complete *wcqe)
2712d613b6a7SJames Smart {
2713d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
2714d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2715b27cbd55SBart Van Assche 	uint32_t result;
271619b58d94SJames Smart 	unsigned long flags;
271719b58d94SJames Smart 	bool released = false;
2718d613b6a7SJames Smart 
2719d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
2720d613b6a7SJames Smart 	result = wcqe->parameter;
2721d613b6a7SJames Smart 
2722d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2723547077a4SJames Smart 	if (ctxp->flag & LPFC_NVMET_ABORT_OP)
2724547077a4SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
2725d613b6a7SJames Smart 
2726c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
2727d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_DONE;
272886c67379SJames Smart 
272986c67379SJames Smart 	/* Check if we already received a free context call
273086c67379SJames Smart 	 * and we have completed processing an abort situation.
273186c67379SJames Smart 	 */
273286c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_CTX_RLS) &&
273386c67379SJames Smart 	    !(ctxp->flag & LPFC_NVMET_XBUSY)) {
2734c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
273586c67379SJames Smart 		list_del(&ctxp->list);
2736c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
273719b58d94SJames Smart 		released = true;
273886c67379SJames Smart 	}
273919b58d94SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
274019b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
2741547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
274219b58d94SJames Smart 
2743e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
274486c67379SJames Smart 			"6165 ABORT cmpl: xri x%x flg x%x (%d) "
274586c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
274686c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
274786c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
274886c67379SJames Smart 			result, wcqe->word3);
274986c67379SJames Smart 
27506c621a22SJames Smart 	cmdwqe->context2 = NULL;
27516c621a22SJames Smart 	cmdwqe->context3 = NULL;
275219b58d94SJames Smart 	/*
275319b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
275419b58d94SJames Smart 	 * will be recycled by transport release call.
275519b58d94SJames Smart 	 */
275619b58d94SJames Smart 	if (released)
27576c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
2758d613b6a7SJames Smart 
27596c621a22SJames Smart 	/* This is the iocbq for the abort, not the command */
2760d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
276186c67379SJames Smart 
276286c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
276386c67379SJames Smart 	 * For LPFC_NVMET_XBUSY, lpfc_sli4_nvmet_xri_aborted
276486c67379SJames Smart 	 * should have been called already.
276586c67379SJames Smart 	 */
2766d613b6a7SJames Smart }
2767d613b6a7SJames Smart 
2768d613b6a7SJames Smart /**
276986c67379SJames Smart  * lpfc_nvmet_unsol_fcp_abort_cmp - Completion handler for ABTS
2770d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
2771d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
2772d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
2773d613b6a7SJames Smart  *
2774d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
2775d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
2776d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
2777d613b6a7SJames Smart  **/
2778d613b6a7SJames Smart static void
277986c67379SJames Smart lpfc_nvmet_unsol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2780d613b6a7SJames Smart 			       struct lpfc_wcqe_complete *wcqe)
2781d613b6a7SJames Smart {
2782d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
2783d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
278419b58d94SJames Smart 	unsigned long flags;
2785b27cbd55SBart Van Assche 	uint32_t result;
278619b58d94SJames Smart 	bool released = false;
2787d613b6a7SJames Smart 
2788d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
2789d613b6a7SJames Smart 	result = wcqe->parameter;
2790d613b6a7SJames Smart 
279186c67379SJames Smart 	if (!ctxp) {
279286c67379SJames Smart 		/* if context is clear, related io alrady complete */
2793d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
279486c67379SJames Smart 				"6070 ABTS cmpl: WCQE: %08x %08x %08x %08x\n",
279586c67379SJames Smart 				wcqe->word0, wcqe->total_data_placed,
2796d613b6a7SJames Smart 				result, wcqe->word3);
279786c67379SJames Smart 		return;
279886c67379SJames Smart 	}
2799d613b6a7SJames Smart 
280078e1d200SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2801c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
280278e1d200SJames Smart 	if (ctxp->flag & LPFC_NVMET_ABORT_OP)
280378e1d200SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
280478e1d200SJames Smart 
2805d613b6a7SJames Smart 	/* Sanity check */
2806d613b6a7SJames Smart 	if (ctxp->state != LPFC_NVMET_STE_ABORT) {
2807d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
280886c67379SJames Smart 				"6112 ABTS Wrong state:%d oxid x%x\n",
2809d613b6a7SJames Smart 				ctxp->state, ctxp->oxid);
2810d613b6a7SJames Smart 	}
281186c67379SJames Smart 
281286c67379SJames Smart 	/* Check if we already received a free context call
281386c67379SJames Smart 	 * and we have completed processing an abort situation.
281486c67379SJames Smart 	 */
2815d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_DONE;
281686c67379SJames Smart 	if ((ctxp->flag & LPFC_NVMET_CTX_RLS) &&
281786c67379SJames Smart 	    !(ctxp->flag & LPFC_NVMET_XBUSY)) {
2818c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
281986c67379SJames Smart 		list_del(&ctxp->list);
2820c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
282119b58d94SJames Smart 		released = true;
282286c67379SJames Smart 	}
282319b58d94SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
282419b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
2825547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
282619b58d94SJames Smart 
282786c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
282886c67379SJames Smart 			"6316 ABTS cmpl xri x%x flg x%x (%x) "
282986c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
283086c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
283186c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
283286c67379SJames Smart 			result, wcqe->word3);
28336c621a22SJames Smart 
28346c621a22SJames Smart 	cmdwqe->context2 = NULL;
28356c621a22SJames Smart 	cmdwqe->context3 = NULL;
283619b58d94SJames Smart 	/*
283719b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
283819b58d94SJames Smart 	 * will be recycled by transport release call.
283919b58d94SJames Smart 	 */
284019b58d94SJames Smart 	if (released)
28416c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
284286c67379SJames Smart 
284386c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
284486c67379SJames Smart 	 * For LPFC_NVMET_XBUSY, lpfc_sli4_nvmet_xri_aborted
284586c67379SJames Smart 	 * should have been called already.
284686c67379SJames Smart 	 */
2847d613b6a7SJames Smart }
2848d613b6a7SJames Smart 
2849d613b6a7SJames Smart /**
2850d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
2851d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
2852d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
2853d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
2854d613b6a7SJames Smart  *
2855d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
2856d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for LS cmds
2857d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
2858d613b6a7SJames Smart  **/
2859d613b6a7SJames Smart static void
2860d613b6a7SJames Smart lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2861d613b6a7SJames Smart 			    struct lpfc_wcqe_complete *wcqe)
2862d613b6a7SJames Smart {
2863d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
2864d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2865b27cbd55SBart Van Assche 	uint32_t result;
2866d613b6a7SJames Smart 
2867d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
2868d613b6a7SJames Smart 	result = wcqe->parameter;
2869d613b6a7SJames Smart 
2870d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2871547077a4SJames Smart 	atomic_inc(&tgtp->xmt_ls_abort_cmpl);
2872d613b6a7SJames Smart 
2873d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2874d613b6a7SJames Smart 			"6083 Abort cmpl: ctx %p WCQE:%08x %08x %08x %08x\n",
2875d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
2876d613b6a7SJames Smart 			result, wcqe->word3);
2877d613b6a7SJames Smart 
2878ce1b591cSJames Smart 	if (!ctxp) {
2879ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2880ce1b591cSJames Smart 				"6415 NVMET LS Abort No ctx: WCQE: "
2881ce1b591cSJames Smart 				 "%08x %08x %08x %08x\n",
2882ce1b591cSJames Smart 				wcqe->word0, wcqe->total_data_placed,
2883ce1b591cSJames Smart 				result, wcqe->word3);
2884ce1b591cSJames Smart 
2885ce1b591cSJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
2886ce1b591cSJames Smart 		return;
2887ce1b591cSJames Smart 	}
2888ce1b591cSJames Smart 
2889ce1b591cSJames Smart 	if (ctxp->state != LPFC_NVMET_STE_LS_ABORT) {
2890ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2891ce1b591cSJames Smart 				"6416 NVMET LS abort cmpl state mismatch: "
2892ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
2893ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
2894ce1b591cSJames Smart 	}
2895ce1b591cSJames Smart 
2896d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
2897d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
2898d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
2899d613b6a7SJames Smart 	kfree(ctxp);
2900d613b6a7SJames Smart }
2901d613b6a7SJames Smart 
2902d613b6a7SJames Smart static int
2903d613b6a7SJames Smart lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
2904d613b6a7SJames Smart 			     struct lpfc_nvmet_rcv_ctx *ctxp,
2905d613b6a7SJames Smart 			     uint32_t sid, uint16_t xri)
2906d613b6a7SJames Smart {
2907d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2908d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
2909205e8240SJames Smart 	union lpfc_wqe128 *wqe_abts;
2910d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2911d613b6a7SJames Smart 
2912d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
291386c67379SJames Smart 			"6067 ABTS: sid %x xri x%x/x%x\n",
2914318083adSJames Smart 			sid, xri, ctxp->wqeq->sli4_xritag);
2915d613b6a7SJames Smart 
2916d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2917d613b6a7SJames Smart 
2918d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
2919d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2920d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2921d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2922d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
2923ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2924d613b6a7SJames Smart 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
2925b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
2926d613b6a7SJames Smart 
2927d613b6a7SJames Smart 		/* No failure to an ABTS request. */
2928d613b6a7SJames Smart 		return 0;
2929d613b6a7SJames Smart 	}
2930d613b6a7SJames Smart 
2931d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
2932d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
2933d613b6a7SJames Smart 
2934d613b6a7SJames Smart 	/*
2935d613b6a7SJames Smart 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
2936d613b6a7SJames Smart 	 * that were initialized in lpfc_sli4_nvmet_alloc.
2937d613b6a7SJames Smart 	 */
2938d613b6a7SJames Smart 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
2939d613b6a7SJames Smart 
2940d613b6a7SJames Smart 	/* Word 5 */
2941d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
2942d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
2943d613b6a7SJames Smart 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
2944d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
2945d613b6a7SJames Smart 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
2946d613b6a7SJames Smart 
2947d613b6a7SJames Smart 	/* Word 6 */
2948d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
2949d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2950d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
2951d613b6a7SJames Smart 	       abts_wqeq->sli4_xritag);
2952d613b6a7SJames Smart 
2953d613b6a7SJames Smart 	/* Word 7 */
2954d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
2955d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
2956d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
2957d613b6a7SJames Smart 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
2958d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
2959d613b6a7SJames Smart 
2960d613b6a7SJames Smart 	/* Word 8 */
2961d613b6a7SJames Smart 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
2962d613b6a7SJames Smart 
2963d613b6a7SJames Smart 	/* Word 9 */
2964d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
2965d613b6a7SJames Smart 	/* Needs to be set by caller */
2966d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
2967d613b6a7SJames Smart 
2968d613b6a7SJames Smart 	/* Word 10 */
2969d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe_abts->xmit_sequence.wqe_com, 1);
2970d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2971d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
2972d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
2973d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
2974d613b6a7SJames Smart 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
2975d613b6a7SJames Smart 
2976d613b6a7SJames Smart 	/* Word 11 */
2977d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
2978d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
2979d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
2980d613b6a7SJames Smart 	       OTHER_COMMAND);
2981d613b6a7SJames Smart 
2982d613b6a7SJames Smart 	abts_wqeq->vport = phba->pport;
2983d613b6a7SJames Smart 	abts_wqeq->context1 = ndlp;
2984d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
2985d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
2986d613b6a7SJames Smart 	abts_wqeq->rsvd2 = 0;
2987d613b6a7SJames Smart 	/* hba_wqidx should already be setup from command we are aborting */
2988d613b6a7SJames Smart 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2989d613b6a7SJames Smart 	abts_wqeq->iocb.ulpLe = 1;
2990d613b6a7SJames Smart 
2991d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2992d613b6a7SJames Smart 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
2993d613b6a7SJames Smart 			xri, abts_wqeq->iotag);
2994d613b6a7SJames Smart 	return 1;
2995d613b6a7SJames Smart }
2996d613b6a7SJames Smart 
2997d613b6a7SJames Smart static int
2998d613b6a7SJames Smart lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
2999d613b6a7SJames Smart 			       struct lpfc_nvmet_rcv_ctx *ctxp,
3000d613b6a7SJames Smart 			       uint32_t sid, uint16_t xri)
3001d613b6a7SJames Smart {
3002d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3003d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3004205e8240SJames Smart 	union lpfc_wqe128 *abts_wqe;
3005d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3006d613b6a7SJames Smart 	unsigned long flags;
3007d613b6a7SJames Smart 	int rc;
3008d613b6a7SJames Smart 
3009d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3010d613b6a7SJames Smart 	if (!ctxp->wqeq) {
30116c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3012d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3013d613b6a7SJames Smart 	}
3014d613b6a7SJames Smart 
3015d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3016d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3017d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3018d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3019d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3020ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
302186c67379SJames Smart 				"6160 Drop ABORT - wrong NDLP state x%x.\n",
3022b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3023d613b6a7SJames Smart 
3024d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3025c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
302686c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3027c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3028d613b6a7SJames Smart 		return 0;
3029d613b6a7SJames Smart 	}
3030d613b6a7SJames Smart 
3031d613b6a7SJames Smart 	/* Issue ABTS for this WQE based on iotag */
3032d613b6a7SJames Smart 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
3033c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3034d613b6a7SJames Smart 	if (!ctxp->abort_wqeq) {
3035547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3036ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
303786c67379SJames Smart 				"6161 ABORT failed: No wqeqs: "
3038d613b6a7SJames Smart 				"xri: x%x\n", ctxp->oxid);
3039d613b6a7SJames Smart 		/* No failure to an ABTS request. */
304086c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3041c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3042d613b6a7SJames Smart 		return 0;
3043d613b6a7SJames Smart 	}
3044d613b6a7SJames Smart 	abts_wqeq = ctxp->abort_wqeq;
3045d613b6a7SJames Smart 	abts_wqe = &abts_wqeq->wqe;
3046d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
3047c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3048d613b6a7SJames Smart 
3049d613b6a7SJames Smart 	/* Announce entry to new IO submit field. */
305086c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
305186c67379SJames Smart 			"6162 ABORT Request to rport DID x%06x "
3052d613b6a7SJames Smart 			"for xri x%x x%x\n",
3053d613b6a7SJames Smart 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
3054d613b6a7SJames Smart 
3055d613b6a7SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
3056d613b6a7SJames Smart 	 * cleared when the reset is complete and rings reestablished.
3057d613b6a7SJames Smart 	 */
3058d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3059d613b6a7SJames Smart 	/* driver queued commands are in process of being flushed */
3060d613b6a7SJames Smart 	if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
3061d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3062547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3063d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
3064d613b6a7SJames Smart 				"6163 Driver in reset cleanup - flushing "
3065d613b6a7SJames Smart 				"NVME Req now. hba_flag x%x oxid x%x\n",
3066d613b6a7SJames Smart 				phba->hba_flag, ctxp->oxid);
3067d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3068c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
306986c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3070c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3071d613b6a7SJames Smart 		return 0;
3072d613b6a7SJames Smart 	}
3073d613b6a7SJames Smart 
3074d613b6a7SJames Smart 	/* Outstanding abort is in progress */
3075d613b6a7SJames Smart 	if (abts_wqeq->iocb_flag & LPFC_DRIVER_ABORTED) {
3076d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3077547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3078d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
3079d613b6a7SJames Smart 				"6164 Outstanding NVME I/O Abort Request "
3080d613b6a7SJames Smart 				"still pending on oxid x%x\n",
3081d613b6a7SJames Smart 				ctxp->oxid);
3082d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3083c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
308486c67379SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3085c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3086d613b6a7SJames Smart 		return 0;
3087d613b6a7SJames Smart 	}
3088d613b6a7SJames Smart 
3089d613b6a7SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
3090d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_DRIVER_ABORTED;
3091d613b6a7SJames Smart 
3092d613b6a7SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
3093d613b6a7SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
3094d613b6a7SJames Smart 	 */
3095d613b6a7SJames Smart 	memset(abts_wqe, 0, sizeof(union lpfc_wqe));
3096d613b6a7SJames Smart 
3097d613b6a7SJames Smart 	/* word 3 */
3098d613b6a7SJames Smart 	bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
3099d613b6a7SJames Smart 
3100d613b6a7SJames Smart 	/* word 7 */
3101d613b6a7SJames Smart 	bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
3102d613b6a7SJames Smart 	bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
3103d613b6a7SJames Smart 
3104d613b6a7SJames Smart 	/* word 8 - tell the FW to abort the IO associated with this
3105d613b6a7SJames Smart 	 * outstanding exchange ID.
3106d613b6a7SJames Smart 	 */
3107d613b6a7SJames Smart 	abts_wqe->abort_cmd.wqe_com.abort_tag = ctxp->wqeq->sli4_xritag;
3108d613b6a7SJames Smart 
3109d613b6a7SJames Smart 	/* word 9 - this is the iotag for the abts_wqe completion. */
3110d613b6a7SJames Smart 	bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
3111d613b6a7SJames Smart 	       abts_wqeq->iotag);
3112d613b6a7SJames Smart 
3113d613b6a7SJames Smart 	/* word 10 */
3114d613b6a7SJames Smart 	bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
3115d613b6a7SJames Smart 	bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
3116d613b6a7SJames Smart 
3117d613b6a7SJames Smart 	/* word 11 */
3118d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
3119d613b6a7SJames Smart 	bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
3120d613b6a7SJames Smart 	bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
3121d613b6a7SJames Smart 
3122d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
3123d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
3124d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
3125d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
3126d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVME;
3127d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
31284550f9c7SJames Smart 	abts_wqeq->vport = phba->pport;
31291fbf9742SJames Smart 	if (!ctxp->hdwq)
31301fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
31311fbf9742SJames Smart 
31321fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3133d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3134547077a4SJames Smart 	if (rc == WQE_SUCCESS) {
3135547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_sol);
3136d613b6a7SJames Smart 		return 0;
3137547077a4SJames Smart 	}
3138d613b6a7SJames Smart 
3139547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3140c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
314186c67379SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3142c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3143d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
314486c67379SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
314586c67379SJames Smart 			"6166 Failed ABORT issue_wqe with status x%x "
3146d613b6a7SJames Smart 			"for oxid x%x.\n",
3147d613b6a7SJames Smart 			rc, ctxp->oxid);
3148d613b6a7SJames Smart 	return 1;
3149d613b6a7SJames Smart }
3150d613b6a7SJames Smart 
3151d613b6a7SJames Smart static int
3152d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
3153d613b6a7SJames Smart 				 struct lpfc_nvmet_rcv_ctx *ctxp,
3154d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
3155d613b6a7SJames Smart {
3156d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3157d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3158d613b6a7SJames Smart 	unsigned long flags;
3159c160c0f8SJames Smart 	bool released = false;
3160d613b6a7SJames Smart 	int rc;
3161d613b6a7SJames Smart 
3162d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3163d613b6a7SJames Smart 	if (!ctxp->wqeq) {
31646c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3165d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3166d613b6a7SJames Smart 	}
3167d613b6a7SJames Smart 
3168ce1b591cSJames Smart 	if (ctxp->state == LPFC_NVMET_STE_FREE) {
3169ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3170ce1b591cSJames Smart 				"6417 NVMET ABORT ctx freed %d %d oxid x%x\n",
3171ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
3172ce1b591cSJames Smart 		rc = WQE_BUSY;
3173ce1b591cSJames Smart 		goto aerr;
3174ce1b591cSJames Smart 	}
3175ce1b591cSJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
3176ce1b591cSJames Smart 	ctxp->entry_cnt++;
3177d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
3178d613b6a7SJames Smart 	if (rc == 0)
3179d613b6a7SJames Smart 		goto aerr;
3180d613b6a7SJames Smart 
3181d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3182d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
318386c67379SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_unsol_fcp_abort_cmp;
318486c67379SJames Smart 	abts_wqeq->iocb_cmpl = NULL;
3185d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVMET;
31861fbf9742SJames Smart 	if (!ctxp->hdwq)
31871fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
31881fbf9742SJames Smart 
31891fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3190d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3191d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3192d613b6a7SJames Smart 		return 0;
3193d613b6a7SJames Smart 	}
3194d613b6a7SJames Smart 
3195d613b6a7SJames Smart aerr:
319668c9b55dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3197c160c0f8SJames Smart 	if (ctxp->flag & LPFC_NVMET_CTX_RLS) {
3198c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
319968c9b55dSJames Smart 		list_del(&ctxp->list);
3200c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
3201c160c0f8SJames Smart 		released = true;
3202c160c0f8SJames Smart 	}
320368c9b55dSJames Smart 	ctxp->flag &= ~(LPFC_NVMET_ABORT_OP | LPFC_NVMET_CTX_RLS);
320468c9b55dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
320568c9b55dSJames Smart 
3206d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3207ce1b591cSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3208d613b6a7SJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x\n",
3209d613b6a7SJames Smart 			ctxp->oxid, rc);
3210c160c0f8SJames Smart 	if (released)
321168c9b55dSJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3212d613b6a7SJames Smart 	return 1;
3213d613b6a7SJames Smart }
3214d613b6a7SJames Smart 
3215d613b6a7SJames Smart static int
3216d613b6a7SJames Smart lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *phba,
3217d613b6a7SJames Smart 				struct lpfc_nvmet_rcv_ctx *ctxp,
3218d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
3219d613b6a7SJames Smart {
3220d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3221d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3222d613b6a7SJames Smart 	unsigned long flags;
3223d613b6a7SJames Smart 	int rc;
3224d613b6a7SJames Smart 
3225ce1b591cSJames Smart 	if ((ctxp->state == LPFC_NVMET_STE_LS_RCV && ctxp->entry_cnt == 1) ||
3226ce1b591cSJames Smart 	    (ctxp->state == LPFC_NVMET_STE_LS_RSP && ctxp->entry_cnt == 2)) {
3227ce1b591cSJames Smart 		ctxp->state = LPFC_NVMET_STE_LS_ABORT;
3228ce1b591cSJames Smart 		ctxp->entry_cnt++;
3229ce1b591cSJames Smart 	} else {
3230ce1b591cSJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3231ce1b591cSJames Smart 				"6418 NVMET LS abort state mismatch "
3232ce1b591cSJames Smart 				"IO x%x: %d %d\n",
3233ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3234ce1b591cSJames Smart 		ctxp->state = LPFC_NVMET_STE_LS_ABORT;
3235ce1b591cSJames Smart 	}
3236ce1b591cSJames Smart 
3237d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3238d613b6a7SJames Smart 	if (!ctxp->wqeq) {
3239d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
3240d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
3241d613b6a7SJames Smart 		if (!ctxp->wqeq) {
3242ce1b591cSJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3243d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
3244d613b6a7SJames Smart 					"xri: x%x\n", xri);
3245d613b6a7SJames Smart 			/* No failure to an ABTS request. */
3246d613b6a7SJames Smart 			kfree(ctxp);
3247d613b6a7SJames Smart 			return 0;
3248d613b6a7SJames Smart 		}
3249d613b6a7SJames Smart 	}
3250d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
32516c621a22SJames Smart 
3252ce1b591cSJames Smart 	if (lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri) == 0) {
3253ce1b591cSJames Smart 		rc = WQE_BUSY;
3254ce1b591cSJames Smart 		goto out;
3255ce1b591cSJames Smart 	}
3256d613b6a7SJames Smart 
3257d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3258d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
3259d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
3260d613b6a7SJames Smart 	abts_wqeq->iocb_flag |=  LPFC_IO_NVME_LS;
32611fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3262d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3263d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3264547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_unsol);
3265d613b6a7SJames Smart 		return 0;
3266d613b6a7SJames Smart 	}
3267ce1b591cSJames Smart out:
3268d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3269d613b6a7SJames Smart 	abts_wqeq->context2 = NULL;
3270d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
3271d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3272d613b6a7SJames Smart 	kfree(ctxp);
3273ce1b591cSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3274d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
3275d613b6a7SJames Smart 	return 0;
3276d613b6a7SJames Smart }
3277