xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision 76edb986)
1d613b6a7SJames Smart /*******************************************************************
2d613b6a7SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
367073c69SJames Smart  * Fibre Channel Host Bus Adapters.                                *
4b93f9eb8SJustin Tee  * Copyright (C) 2017-2023 Broadcom. All Rights Reserved. The term *
54ae2ebdeSJames Smart  * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.     *
6d613b6a7SJames Smart  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
7d613b6a7SJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
8d080abe0SJames Smart  * www.broadcom.com                                                *
9d613b6a7SJames Smart  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
10d613b6a7SJames Smart  *                                                                 *
11d613b6a7SJames Smart  * This program is free software; you can redistribute it and/or   *
12d613b6a7SJames Smart  * modify it under the terms of version 2 of the GNU General       *
13d613b6a7SJames Smart  * Public License as published by the Free Software Foundation.    *
14d613b6a7SJames Smart  * This program is distributed in the hope that it will be useful. *
15d613b6a7SJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
16d613b6a7SJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
17d613b6a7SJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
18d613b6a7SJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19d613b6a7SJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
20d613b6a7SJames Smart  * more details, a copy of which can be found in the file COPYING  *
21d613b6a7SJames Smart  * included with this package.                                     *
22d613b6a7SJames Smart  ********************************************************************/
23d613b6a7SJames Smart #include <linux/pci.h>
24d613b6a7SJames Smart #include <linux/slab.h>
25d613b6a7SJames Smart #include <linux/interrupt.h>
26d613b6a7SJames Smart #include <linux/delay.h>
27d613b6a7SJames Smart #include <asm/unaligned.h>
28d613b6a7SJames Smart #include <linux/crc-t10dif.h>
29d613b6a7SJames Smart #include <net/checksum.h>
30d613b6a7SJames Smart 
31d613b6a7SJames Smart #include <scsi/scsi.h>
32d613b6a7SJames Smart #include <scsi/scsi_device.h>
33d613b6a7SJames Smart #include <scsi/scsi_eh.h>
34d613b6a7SJames Smart #include <scsi/scsi_host.h>
35d613b6a7SJames Smart #include <scsi/scsi_tcq.h>
36d613b6a7SJames Smart #include <scsi/scsi_transport_fc.h>
37d613b6a7SJames Smart #include <scsi/fc/fc_fs.h>
38d613b6a7SJames Smart 
39d613b6a7SJames Smart #include "lpfc_version.h"
40d613b6a7SJames Smart #include "lpfc_hw4.h"
41d613b6a7SJames Smart #include "lpfc_hw.h"
42d613b6a7SJames Smart #include "lpfc_sli.h"
43d613b6a7SJames Smart #include "lpfc_sli4.h"
44d613b6a7SJames Smart #include "lpfc_nl.h"
45d613b6a7SJames Smart #include "lpfc_disc.h"
46d613b6a7SJames Smart #include "lpfc.h"
47d613b6a7SJames Smart #include "lpfc_scsi.h"
48d613b6a7SJames Smart #include "lpfc_nvme.h"
49d613b6a7SJames Smart #include "lpfc_logmsg.h"
50d613b6a7SJames Smart #include "lpfc_crtn.h"
51d613b6a7SJames Smart #include "lpfc_vport.h"
522b65e182SJames Smart #include "lpfc_debugfs.h"
53d613b6a7SJames Smart 
54d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *,
557cacae2aSJames Smart 						 struct lpfc_async_xchg_ctx *,
56d613b6a7SJames Smart 						 dma_addr_t rspbuf,
57d613b6a7SJames Smart 						 uint16_t rspsize);
58d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *,
597cacae2aSJames Smart 						  struct lpfc_async_xchg_ctx *);
60d613b6a7SJames Smart static int lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *,
617cacae2aSJames Smart 					  struct lpfc_async_xchg_ctx *,
62d613b6a7SJames Smart 					  uint32_t, uint16_t);
63d613b6a7SJames Smart static int lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *,
647cacae2aSJames Smart 					    struct lpfc_async_xchg_ctx *,
65d613b6a7SJames Smart 					    uint32_t, uint16_t);
666e8e1c14SJames Smart static void lpfc_nvmet_wqfull_flush(struct lpfc_hba *, struct lpfc_queue *,
677cacae2aSJames Smart 				    struct lpfc_async_xchg_ctx *);
68472e146dSJames Smart static void lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *);
69472e146dSJames Smart 
70472e146dSJames Smart static void lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf);
71d613b6a7SJames Smart 
72fab2e466SColin Ian King static union lpfc_wqe128 lpfc_tsend_cmd_template;
73fab2e466SColin Ian King static union lpfc_wqe128 lpfc_treceive_cmd_template;
74fab2e466SColin Ian King static union lpfc_wqe128 lpfc_trsp_cmd_template;
75bd3061baSJames Smart 
76bd3061baSJames Smart /* Setup WQE templates for NVME IOs */
77bd3061baSJames Smart void
lpfc_nvmet_cmd_template(void)78fab2e466SColin Ian King lpfc_nvmet_cmd_template(void)
79bd3061baSJames Smart {
80bd3061baSJames Smart 	union lpfc_wqe128 *wqe;
81bd3061baSJames Smart 
82bd3061baSJames Smart 	/* TSEND template */
83bd3061baSJames Smart 	wqe = &lpfc_tsend_cmd_template;
84bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
85bd3061baSJames Smart 
86bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
87bd3061baSJames Smart 
88bd3061baSJames Smart 	/* Word 3 - payload_offset_len is zero */
89bd3061baSJames Smart 
90bd3061baSJames Smart 	/* Word 4 - relative_offset is variable */
91bd3061baSJames Smart 
92bd3061baSJames Smart 	/* Word 5 - is zero */
93bd3061baSJames Smart 
94bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
95bd3061baSJames Smart 
96bd3061baSJames Smart 	/* Word 7 - wqe_ar is variable */
97bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_tsend.wqe_com, CMD_FCP_TSEND64_WQE);
98bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_tsend.wqe_com, PARM_REL_OFF);
99bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_tsend.wqe_com, CLASS3);
100bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_tsend.wqe_com, SLI4_CT_RPI);
101bd3061baSJames Smart 	bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 1);
102bd3061baSJames Smart 
103bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
104bd3061baSJames Smart 
105bd3061baSJames Smart 	/* Word 9  - reqtag, rcvoxid is variable */
106bd3061baSJames Smart 
107bd3061baSJames Smart 	/* Word 10 - wqes, xc is variable */
108b101eb27SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_tsend.wqe_com, LPFC_NVME_XCHG);
109bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_tsend.wqe_com, 1);
110bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
111bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 1);
112bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_tsend.wqe_com, LPFC_WQE_IOD_WRITE);
113bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_tsend.wqe_com, LPFC_WQE_LENLOC_WORD12);
114bd3061baSJames Smart 
115bd3061baSJames Smart 	/* Word 11 - sup, irsp, irsplen is variable */
116bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_tsend.wqe_com, FCP_COMMAND_TSEND);
117bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_tsend.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
118bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
119bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
120bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
121bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_tsend.wqe_com, 0);
122bd3061baSJames Smart 
123bd3061baSJames Smart 	/* Word 12 - fcp_data_len is variable */
124bd3061baSJames Smart 
125bd3061baSJames Smart 	/* Word 13, 14, 15 - PBDE is zero */
126bd3061baSJames Smart 
127bd3061baSJames Smart 	/* TRECEIVE template */
128bd3061baSJames Smart 	wqe = &lpfc_treceive_cmd_template;
129bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
130bd3061baSJames Smart 
131bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
132bd3061baSJames Smart 
133bd3061baSJames Smart 	/* Word 3 */
134bd3061baSJames Smart 	wqe->fcp_treceive.payload_offset_len = TXRDY_PAYLOAD_LEN;
135bd3061baSJames Smart 
136bd3061baSJames Smart 	/* Word 4 - relative_offset is variable */
137bd3061baSJames Smart 
138bd3061baSJames Smart 	/* Word 5 - is zero */
139bd3061baSJames Smart 
140bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
141bd3061baSJames Smart 
142bd3061baSJames Smart 	/* Word 7 */
143bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_treceive.wqe_com, CMD_FCP_TRECEIVE64_WQE);
144bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_treceive.wqe_com, PARM_REL_OFF);
145bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_treceive.wqe_com, CLASS3);
146bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_treceive.wqe_com, SLI4_CT_RPI);
147bd3061baSJames Smart 	bf_set(wqe_ar, &wqe->fcp_treceive.wqe_com, 0);
148bd3061baSJames Smart 
149bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
150bd3061baSJames Smart 
151bd3061baSJames Smart 	/* Word 9  - reqtag, rcvoxid is variable */
152bd3061baSJames Smart 
153bd3061baSJames Smart 	/* Word 10 - xc is variable */
154bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_treceive.wqe_com, 1);
155bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_treceive.wqe_com, 0);
156b101eb27SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_treceive.wqe_com, LPFC_NVME_XCHG);
157bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_treceive.wqe_com, LPFC_WQE_IOD_READ);
158bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_treceive.wqe_com, LPFC_WQE_LENLOC_WORD12);
159bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 1);
160bd3061baSJames Smart 
161bd3061baSJames Smart 	/* Word 11 - pbde is variable */
162bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_treceive.wqe_com, FCP_COMMAND_TRECEIVE);
163bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_treceive.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
164bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_treceive.wqe_com, 0);
165bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_treceive.wqe_com, 0);
166bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_treceive.wqe_com, 0);
167bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 1);
168bd3061baSJames Smart 
169bd3061baSJames Smart 	/* Word 12 - fcp_data_len is variable */
170bd3061baSJames Smart 
171bd3061baSJames Smart 	/* Word 13, 14, 15 - PBDE is variable */
172bd3061baSJames Smart 
173bd3061baSJames Smart 	/* TRSP template */
174bd3061baSJames Smart 	wqe = &lpfc_trsp_cmd_template;
175bd3061baSJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
176bd3061baSJames Smart 
177bd3061baSJames Smart 	/* Word 0, 1, 2 - BDE is variable */
178bd3061baSJames Smart 
179bd3061baSJames Smart 	/* Word 3 - response_len is variable */
180bd3061baSJames Smart 
181bd3061baSJames Smart 	/* Word 4, 5 - is zero */
182bd3061baSJames Smart 
183bd3061baSJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
184bd3061baSJames Smart 
185bd3061baSJames Smart 	/* Word 7 */
186bd3061baSJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_trsp.wqe_com, CMD_FCP_TRSP64_WQE);
187bd3061baSJames Smart 	bf_set(wqe_pu, &wqe->fcp_trsp.wqe_com, PARM_UNUSED);
188bd3061baSJames Smart 	bf_set(wqe_class, &wqe->fcp_trsp.wqe_com, CLASS3);
189bd3061baSJames Smart 	bf_set(wqe_ct, &wqe->fcp_trsp.wqe_com, SLI4_CT_RPI);
190bd3061baSJames Smart 	bf_set(wqe_ag, &wqe->fcp_trsp.wqe_com, 1); /* wqe_ar */
191bd3061baSJames Smart 
192bd3061baSJames Smart 	/* Word 8 - abort_tag is variable */
193bd3061baSJames Smart 
194bd3061baSJames Smart 	/* Word 9  - reqtag is variable */
195bd3061baSJames Smart 
196bd3061baSJames Smart 	/* Word 10 wqes, xc is variable */
197bd3061baSJames Smart 	bf_set(wqe_dbde, &wqe->fcp_trsp.wqe_com, 1);
198b101eb27SJames Smart 	bf_set(wqe_xchg, &wqe->fcp_trsp.wqe_com, LPFC_NVME_XCHG);
199bd3061baSJames Smart 	bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 0);
200bd3061baSJames Smart 	bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 0);
201bd3061baSJames Smart 	bf_set(wqe_iod, &wqe->fcp_trsp.wqe_com, LPFC_WQE_IOD_NONE);
202bd3061baSJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_trsp.wqe_com, LPFC_WQE_LENLOC_WORD3);
203bd3061baSJames Smart 
204bd3061baSJames Smart 	/* Word 11 irsp, irsplen is variable */
205bd3061baSJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_trsp.wqe_com, FCP_COMMAND_TRSP);
206bd3061baSJames Smart 	bf_set(wqe_cqid, &wqe->fcp_trsp.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
207bd3061baSJames Smart 	bf_set(wqe_sup, &wqe->fcp_trsp.wqe_com, 0);
208bd3061baSJames Smart 	bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 0);
209bd3061baSJames Smart 	bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com, 0);
210bd3061baSJames Smart 	bf_set(wqe_pbde, &wqe->fcp_trsp.wqe_com, 0);
211bd3061baSJames Smart 
212bd3061baSJames Smart 	/* Word 12, 13, 14, 15 - is zero */
213bd3061baSJames Smart }
214bd3061baSJames Smart 
215336df6ebSNathan Chancellor #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2167cacae2aSJames Smart static struct lpfc_async_xchg_ctx *
lpfc_nvmet_get_ctx_for_xri(struct lpfc_hba * phba,u16 xri)21779d8c4ceSJames Smart lpfc_nvmet_get_ctx_for_xri(struct lpfc_hba *phba, u16 xri)
21879d8c4ceSJames Smart {
2197cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
22079d8c4ceSJames Smart 	unsigned long iflag;
22179d8c4ceSJames Smart 	bool found = false;
22279d8c4ceSJames Smart 
22379d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
22479d8c4ceSJames Smart 	list_for_each_entry(ctxp, &phba->sli4_hba.t_active_ctx_list, list) {
22579d8c4ceSJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
22679d8c4ceSJames Smart 			continue;
22779d8c4ceSJames Smart 
22879d8c4ceSJames Smart 		found = true;
22979d8c4ceSJames Smart 		break;
23079d8c4ceSJames Smart 	}
23179d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
23279d8c4ceSJames Smart 	if (found)
23379d8c4ceSJames Smart 		return ctxp;
23479d8c4ceSJames Smart 
23579d8c4ceSJames Smart 	return NULL;
23679d8c4ceSJames Smart }
23779d8c4ceSJames Smart 
2387cacae2aSJames Smart static struct lpfc_async_xchg_ctx *
lpfc_nvmet_get_ctx_for_oxid(struct lpfc_hba * phba,u16 oxid,u32 sid)23979d8c4ceSJames Smart lpfc_nvmet_get_ctx_for_oxid(struct lpfc_hba *phba, u16 oxid, u32 sid)
24079d8c4ceSJames Smart {
2417cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
24279d8c4ceSJames Smart 	unsigned long iflag;
24379d8c4ceSJames Smart 	bool found = false;
24479d8c4ceSJames Smart 
24579d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
24679d8c4ceSJames Smart 	list_for_each_entry(ctxp, &phba->sli4_hba.t_active_ctx_list, list) {
24779d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
24879d8c4ceSJames Smart 			continue;
24979d8c4ceSJames Smart 
25079d8c4ceSJames Smart 		found = true;
25179d8c4ceSJames Smart 		break;
25279d8c4ceSJames Smart 	}
25379d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
25479d8c4ceSJames Smart 	if (found)
25579d8c4ceSJames Smart 		return ctxp;
25679d8c4ceSJames Smart 
25779d8c4ceSJames Smart 	return NULL;
25879d8c4ceSJames Smart }
259336df6ebSNathan Chancellor #endif
26079d8c4ceSJames Smart 
2613999df75SBart Van Assche static void
lpfc_nvmet_defer_release(struct lpfc_hba * phba,struct lpfc_async_xchg_ctx * ctxp)2627cacae2aSJames Smart lpfc_nvmet_defer_release(struct lpfc_hba *phba,
2637cacae2aSJames Smart 			struct lpfc_async_xchg_ctx *ctxp)
26486c67379SJames Smart {
265472e146dSJames Smart 	lockdep_assert_held(&ctxp->ctxlock);
26686c67379SJames Smart 
267e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
26879d8c4ceSJames Smart 			"6313 NVMET Defer ctx release oxid x%x flg x%x\n",
26986c67379SJames Smart 			ctxp->oxid, ctxp->flag);
27086c67379SJames Smart 
2717b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_CTX_RLS)
27286c67379SJames Smart 		return;
273472e146dSJames Smart 
2747b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_CTX_RLS;
27579d8c4ceSJames Smart 	spin_lock(&phba->sli4_hba.t_active_list_lock);
27679d8c4ceSJames Smart 	list_del(&ctxp->list);
27779d8c4ceSJames Smart 	spin_unlock(&phba->sli4_hba.t_active_list_lock);
278472e146dSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
27986c67379SJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
280472e146dSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
28186c67379SJames Smart }
28286c67379SJames Smart 
283d613b6a7SJames Smart /**
284fe1bedecSJames Smart  * __lpfc_nvme_xmt_ls_rsp_cmp - Generic completion handler for the
285fe1bedecSJames Smart  *         transmission of an NVME LS response.
286fe1bedecSJames Smart  * @phba: Pointer to HBA context object.
287fe1bedecSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
288a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
289fe1bedecSJames Smart  *
290fe1bedecSJames Smart  * The function is called from SLI ring event handler with no
291fe1bedecSJames Smart  * lock held. The function frees memory resources used for the command
292fe1bedecSJames Smart  * used to send the NVME LS RSP.
293fe1bedecSJames Smart  **/
294fe1bedecSJames Smart void
__lpfc_nvme_xmt_ls_rsp_cmp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdwqe,struct lpfc_iocbq * rspwqe)295fe1bedecSJames Smart __lpfc_nvme_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
296a680a929SJames Smart 			   struct lpfc_iocbq *rspwqe)
297fe1bedecSJames Smart {
298d51cf5bdSJames Smart 	struct lpfc_async_xchg_ctx *axchg = cmdwqe->context_un.axchg;
299a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
300fe1bedecSJames Smart 	struct nvmefc_ls_rsp *ls_rsp = &axchg->ls_rsp;
301fe1bedecSJames Smart 	uint32_t status, result;
302fe1bedecSJames Smart 
3036a84d015SJustin Tee 	status = bf_get(lpfc_wcqe_c_status, wcqe);
304fe1bedecSJames Smart 	result = wcqe->parameter;
305fe1bedecSJames Smart 
306fe1bedecSJames Smart 	if (axchg->state != LPFC_NVME_STE_LS_RSP || axchg->entry_cnt != 2) {
307372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
308fe1bedecSJames Smart 				"6410 NVMEx LS cmpl state mismatch IO x%x: "
309fe1bedecSJames Smart 				"%d %d\n",
310fe1bedecSJames Smart 				axchg->oxid, axchg->state, axchg->entry_cnt);
311fe1bedecSJames Smart 	}
312fe1bedecSJames Smart 
313fe1bedecSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS  CMPL: xri x%x stat x%x result x%x\n",
314fe1bedecSJames Smart 			 axchg->oxid, status, result);
315fe1bedecSJames Smart 
316fe1bedecSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
317fe1bedecSJames Smart 			"6038 NVMEx LS rsp cmpl: %d %d oxid x%x\n",
318fe1bedecSJames Smart 			status, result, axchg->oxid);
319fe1bedecSJames Smart 
320d51cf5bdSJames Smart 	lpfc_nlp_put(cmdwqe->ndlp);
321d51cf5bdSJames Smart 	cmdwqe->context_un.axchg = NULL;
322d51cf5bdSJames Smart 	cmdwqe->bpl_dmabuf = NULL;
323fe1bedecSJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
324fe1bedecSJames Smart 	ls_rsp->done(ls_rsp);
325fe1bedecSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
326fe1bedecSJames Smart 			"6200 NVMEx LS rsp cmpl done status %d oxid x%x\n",
327fe1bedecSJames Smart 			status, axchg->oxid);
328fe1bedecSJames Smart 	kfree(axchg);
329fe1bedecSJames Smart }
330fe1bedecSJames Smart 
331fe1bedecSJames Smart /**
332d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
333d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
334d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
335a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
336d613b6a7SJames Smart  *
337d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
338d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME LS commands
339fe1bedecSJames Smart  * The function updates any states and statistics, then calls the
340fe1bedecSJames Smart  * generic completion handler to free resources.
341d613b6a7SJames Smart  **/
342d613b6a7SJames Smart static void
lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdwqe,struct lpfc_iocbq * rspwqe)343d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
344a680a929SJames Smart 			  struct lpfc_iocbq *rspwqe)
345d613b6a7SJames Smart {
346d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
347d613b6a7SJames Smart 	uint32_t status, result;
348a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
349d613b6a7SJames Smart 
350d613b6a7SJames Smart 	if (!phba->targetport)
351fe1bedecSJames Smart 		goto finish;
352fe1bedecSJames Smart 
3536a84d015SJustin Tee 	status = bf_get(lpfc_wcqe_c_status, wcqe);
354fe1bedecSJames Smart 	result = wcqe->parameter;
355d613b6a7SJames Smart 
356d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3574b056682SJames Smart 	if (tgtp) {
3584b056682SJames Smart 		if (status) {
359d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_error);
3608ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
3614b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_aborted);
3624b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
3634b056682SJames Smart 				atomic_inc(&tgtp->xmt_ls_rsp_xb_set);
3644b056682SJames Smart 		} else {
365d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
3664b056682SJames Smart 		}
3674b056682SJames Smart 	}
368d613b6a7SJames Smart 
369fe1bedecSJames Smart finish:
370a680a929SJames Smart 	__lpfc_nvme_xmt_ls_rsp_cmp(phba, cmdwqe, rspwqe);
371d613b6a7SJames Smart }
372d613b6a7SJames Smart 
373d613b6a7SJames Smart /**
3746c621a22SJames Smart  * lpfc_nvmet_ctxbuf_post - Repost a NVMET RQ DMA buffer and clean up context
375d613b6a7SJames Smart  * @phba: HBA buffer is associated with
376d3cb79fcSLee Jones  * @ctx_buf: ctx buffer context
377d613b6a7SJames Smart  *
378d613b6a7SJames Smart  * Description: Frees the given DMA buffer in the appropriate way given by
379d613b6a7SJames Smart  * reposting it to its associated RQ so it can be reused.
380d613b6a7SJames Smart  *
381d613b6a7SJames Smart  * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
382d613b6a7SJames Smart  *
383d613b6a7SJames Smart  * Returns: None
384d613b6a7SJames Smart  **/
385d613b6a7SJames Smart void
lpfc_nvmet_ctxbuf_post(struct lpfc_hba * phba,struct lpfc_nvmet_ctxbuf * ctx_buf)3866c621a22SJames Smart lpfc_nvmet_ctxbuf_post(struct lpfc_hba *phba, struct lpfc_nvmet_ctxbuf *ctx_buf)
387d613b6a7SJames Smart {
388eeeb51d8SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
3897cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp = ctx_buf->context;
390a8cf5dfeSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
391a8cf5dfeSJames Smart 	struct fc_frame_header *fc_hdr;
392a8cf5dfeSJames Smart 	struct rqb_dmabuf *nvmebuf;
39366d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
394472e146dSJames Smart 	uint32_t size, oxid, sid;
39566d7ce93SDick Kennedy 	int cpu;
3966c621a22SJames Smart 	unsigned long iflag;
39786c67379SJames Smart 
3987b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_FREE) {
399372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
400ce1b591cSJames Smart 				"6411 NVMET free, already free IO x%x: %d %d\n",
401ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
402ce1b591cSJames Smart 	}
403472e146dSJames Smart 
404472e146dSJames Smart 	if (ctxp->rqb_buffer) {
405472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
4064767c58aSJames Smart 		nvmebuf = ctxp->rqb_buffer;
4074767c58aSJames Smart 		/* check if freed in another path whilst acquiring lock */
4084767c58aSJames Smart 		if (nvmebuf) {
409472e146dSJames Smart 			ctxp->rqb_buffer = NULL;
4107b7f551bSJames Smart 			if (ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) {
4117b7f551bSJames Smart 				ctxp->flag &= ~LPFC_NVME_CTX_REUSE_WQ;
412472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4134767c58aSJames Smart 				nvmebuf->hrq->rqbp->rqb_free_buffer(phba,
4144767c58aSJames Smart 								    nvmebuf);
415472e146dSJames Smart 			} else {
416472e146dSJames Smart 				spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
4174767c58aSJames Smart 				/* repost */
4184767c58aSJames Smart 				lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
4194767c58aSJames Smart 			}
4204767c58aSJames Smart 		} else {
4214767c58aSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
422472e146dSJames Smart 		}
423472e146dSJames Smart 	}
4247b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_FREE;
4256c621a22SJames Smart 
426a8cf5dfeSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
427a8cf5dfeSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
428a8cf5dfeSJames Smart 		list_remove_head(&phba->sli4_hba.lpfc_nvmet_io_wait_list,
429a8cf5dfeSJames Smart 				 nvmebuf, struct rqb_dmabuf,
430a8cf5dfeSJames Smart 				 hbuf.list);
431a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt--;
432a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
433a8cf5dfeSJames Smart 				       iflag);
434a8cf5dfeSJames Smart 
435a8cf5dfeSJames Smart 		fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
436a8cf5dfeSJames Smart 		oxid = be16_to_cpu(fc_hdr->fh_ox_id);
437a8cf5dfeSJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
438a8cf5dfeSJames Smart 		size = nvmebuf->bytes_recv;
439a8cf5dfeSJames Smart 		sid = sli4_sid_from_fc_hdr(fc_hdr);
440a8cf5dfeSJames Smart 
4417cacae2aSJames Smart 		ctxp = (struct lpfc_async_xchg_ctx *)ctx_buf->context;
442a8cf5dfeSJames Smart 		ctxp->wqeq = NULL;
443a8cf5dfeSJames Smart 		ctxp->offset = 0;
444a8cf5dfeSJames Smart 		ctxp->phba = phba;
445a8cf5dfeSJames Smart 		ctxp->size = size;
446a8cf5dfeSJames Smart 		ctxp->oxid = oxid;
447a8cf5dfeSJames Smart 		ctxp->sid = sid;
4487b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_RCV;
449a8cf5dfeSJames Smart 		ctxp->entry_cnt = 1;
450a8cf5dfeSJames Smart 		ctxp->flag = 0;
451a8cf5dfeSJames Smart 		ctxp->ctxbuf = ctx_buf;
452cbc5de1bSJames Smart 		ctxp->rqb_buffer = (void *)nvmebuf;
453a8cf5dfeSJames Smart 		spin_lock_init(&ctxp->ctxlock);
454a8cf5dfeSJames Smart 
455a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
456d74a89aaSJames Smart 		/* NOTE: isr time stamp is stale when context is re-assigned*/
457d74a89aaSJames Smart 		if (ctxp->ts_isr_cmd) {
458d74a89aaSJames Smart 			ctxp->ts_cmd_nvme = 0;
459a8cf5dfeSJames Smart 			ctxp->ts_nvme_data = 0;
460a8cf5dfeSJames Smart 			ctxp->ts_data_wqput = 0;
461a8cf5dfeSJames Smart 			ctxp->ts_isr_data = 0;
462a8cf5dfeSJames Smart 			ctxp->ts_data_nvme = 0;
463a8cf5dfeSJames Smart 			ctxp->ts_nvme_status = 0;
464a8cf5dfeSJames Smart 			ctxp->ts_status_wqput = 0;
465a8cf5dfeSJames Smart 			ctxp->ts_isr_status = 0;
466a8cf5dfeSJames Smart 			ctxp->ts_status_nvme = 0;
467d613b6a7SJames Smart 		}
468a8cf5dfeSJames Smart #endif
469a8cf5dfeSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_in);
470472e146dSJames Smart 
47179d8c4ceSJames Smart 		/* Indicate that a replacement buffer has been posted */
472472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
4737b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_CTX_REUSE_WQ;
474472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
475a8cf5dfeSJames Smart 
476472e146dSJames Smart 		if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
477a8cf5dfeSJames Smart 			atomic_inc(&tgtp->rcv_fcp_cmd_drop);
478372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
479472e146dSJames Smart 					"6181 Unable to queue deferred work "
480472e146dSJames Smart 					"for oxid x%x. "
481472e146dSJames Smart 					"FCP Drop IO [x%x x%x x%x]\n",
482472e146dSJames Smart 					ctxp->oxid,
483a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_in),
484a8cf5dfeSJames Smart 					atomic_read(&tgtp->rcv_fcp_cmd_out),
485a8cf5dfeSJames Smart 					atomic_read(&tgtp->xmt_fcp_release));
486a8cf5dfeSJames Smart 
487472e146dSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
488a8cf5dfeSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
489472e146dSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
490a8cf5dfeSJames Smart 			lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
491472e146dSJames Smart 		}
492a8cf5dfeSJames Smart 		return;
493a8cf5dfeSJames Smart 	}
494a8cf5dfeSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
495a8cf5dfeSJames Smart 
49666d7ce93SDick Kennedy 	/*
49766d7ce93SDick Kennedy 	 * Use the CPU context list, from the MRQ the IO was received on
49866d7ce93SDick Kennedy 	 * (ctxp->idx), to save context structure.
49966d7ce93SDick Kennedy 	 */
50079d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
50179d8c4ceSJames Smart 	list_del_init(&ctxp->list);
50279d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
503d6d189ceSBart Van Assche 	cpu = raw_smp_processor_id();
50466d7ce93SDick Kennedy 	infop = lpfc_get_ctx_list(phba, cpu, ctxp->idx);
50566d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, iflag);
50666d7ce93SDick Kennedy 	list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
50766d7ce93SDick Kennedy 	infop->nvmet_ctx_list_cnt++;
50866d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, iflag);
509eeeb51d8SJames Smart #endif
510d613b6a7SJames Smart }
511d613b6a7SJames Smart 
5122b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5132b65e182SJames Smart static void
lpfc_nvmet_ktime(struct lpfc_hba * phba,struct lpfc_async_xchg_ctx * ctxp)5142b65e182SJames Smart lpfc_nvmet_ktime(struct lpfc_hba *phba,
5157cacae2aSJames Smart 		 struct lpfc_async_xchg_ctx *ctxp)
5162b65e182SJames Smart {
5172b65e182SJames Smart 	uint64_t seg1, seg2, seg3, seg4, seg5;
5182b65e182SJames Smart 	uint64_t seg6, seg7, seg8, seg9, seg10;
519c8a4ce0bSDick Kennedy 	uint64_t segsum;
5202b65e182SJames Smart 
5212b65e182SJames Smart 	if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
5222b65e182SJames Smart 	    !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
5232b65e182SJames Smart 	    !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
5242b65e182SJames Smart 	    !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
5252b65e182SJames Smart 	    !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
5262b65e182SJames Smart 		return;
5272b65e182SJames Smart 
528c8a4ce0bSDick Kennedy 	if (ctxp->ts_status_nvme < ctxp->ts_isr_cmd)
529c8a4ce0bSDick Kennedy 		return;
5302b65e182SJames Smart 	if (ctxp->ts_isr_cmd  > ctxp->ts_cmd_nvme)
5312b65e182SJames Smart 		return;
5322b65e182SJames Smart 	if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
5332b65e182SJames Smart 		return;
5342b65e182SJames Smart 	if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
5352b65e182SJames Smart 		return;
5362b65e182SJames Smart 	if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
5372b65e182SJames Smart 		return;
5382b65e182SJames Smart 	if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
5392b65e182SJames Smart 		return;
5402b65e182SJames Smart 	if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
5412b65e182SJames Smart 		return;
5422b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
5432b65e182SJames Smart 		return;
5442b65e182SJames Smart 	if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
5452b65e182SJames Smart 		return;
5462b65e182SJames Smart 	if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
5472b65e182SJames Smart 		return;
5482b65e182SJames Smart 	/*
5492b65e182SJames Smart 	 * Segment 1 - Time from FCP command received by MSI-X ISR
5502b65e182SJames Smart 	 * to FCP command is passed to NVME Layer.
5512b65e182SJames Smart 	 * Segment 2 - Time from FCP command payload handed
5522b65e182SJames Smart 	 * off to NVME Layer to Driver receives a Command op
5532b65e182SJames Smart 	 * from NVME Layer.
5542b65e182SJames Smart 	 * Segment 3 - Time from Driver receives a Command op
5552b65e182SJames Smart 	 * from NVME Layer to Command is put on WQ.
5562b65e182SJames Smart 	 * Segment 4 - Time from Driver WQ put is done
5572b65e182SJames Smart 	 * to MSI-X ISR for Command cmpl.
5582b65e182SJames Smart 	 * Segment 5 - Time from MSI-X ISR for Command cmpl to
5592b65e182SJames Smart 	 * Command cmpl is passed to NVME Layer.
5602b65e182SJames Smart 	 * Segment 6 - Time from Command cmpl is passed to NVME
5612b65e182SJames Smart 	 * Layer to Driver receives a RSP op from NVME Layer.
5622b65e182SJames Smart 	 * Segment 7 - Time from Driver receives a RSP op from
5632b65e182SJames Smart 	 * NVME Layer to WQ put is done on TRSP FCP Status.
5642b65e182SJames Smart 	 * Segment 8 - Time from Driver WQ put is done on TRSP
5652b65e182SJames Smart 	 * FCP Status to MSI-X ISR for TRSP cmpl.
5662b65e182SJames Smart 	 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
5672b65e182SJames Smart 	 * TRSP cmpl is passed to NVME Layer.
5682b65e182SJames Smart 	 * Segment 10 - Time from FCP command received by
5692b65e182SJames Smart 	 * MSI-X ISR to command is completed on wire.
5702b65e182SJames Smart 	 * (Segments 1 thru 8) for READDATA / WRITEDATA
5712b65e182SJames Smart 	 * (Segments 1 thru 4) for READDATA_RSP
5722b65e182SJames Smart 	 */
5732b65e182SJames Smart 	seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
574c8a4ce0bSDick Kennedy 	segsum = seg1;
575c8a4ce0bSDick Kennedy 
576c8a4ce0bSDick Kennedy 	seg2 = ctxp->ts_nvme_data - ctxp->ts_isr_cmd;
577c8a4ce0bSDick Kennedy 	if (segsum > seg2)
578c8a4ce0bSDick Kennedy 		return;
579c8a4ce0bSDick Kennedy 	seg2 -= segsum;
580c8a4ce0bSDick Kennedy 	segsum += seg2;
581c8a4ce0bSDick Kennedy 
582c8a4ce0bSDick Kennedy 	seg3 = ctxp->ts_data_wqput - ctxp->ts_isr_cmd;
583c8a4ce0bSDick Kennedy 	if (segsum > seg3)
584c8a4ce0bSDick Kennedy 		return;
585c8a4ce0bSDick Kennedy 	seg3 -= segsum;
586c8a4ce0bSDick Kennedy 	segsum += seg3;
587c8a4ce0bSDick Kennedy 
588c8a4ce0bSDick Kennedy 	seg4 = ctxp->ts_isr_data - ctxp->ts_isr_cmd;
589c8a4ce0bSDick Kennedy 	if (segsum > seg4)
590c8a4ce0bSDick Kennedy 		return;
591c8a4ce0bSDick Kennedy 	seg4 -= segsum;
592c8a4ce0bSDick Kennedy 	segsum += seg4;
593c8a4ce0bSDick Kennedy 
594c8a4ce0bSDick Kennedy 	seg5 = ctxp->ts_data_nvme - ctxp->ts_isr_cmd;
595c8a4ce0bSDick Kennedy 	if (segsum > seg5)
596c8a4ce0bSDick Kennedy 		return;
597c8a4ce0bSDick Kennedy 	seg5 -= segsum;
598c8a4ce0bSDick Kennedy 	segsum += seg5;
599c8a4ce0bSDick Kennedy 
6002b65e182SJames Smart 
6012b65e182SJames Smart 	/* For auto rsp commands seg6 thru seg10 will be 0 */
6022b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
603c8a4ce0bSDick Kennedy 		seg6 = ctxp->ts_nvme_status - ctxp->ts_isr_cmd;
604c8a4ce0bSDick Kennedy 		if (segsum > seg6)
605c8a4ce0bSDick Kennedy 			return;
606c8a4ce0bSDick Kennedy 		seg6 -= segsum;
607c8a4ce0bSDick Kennedy 		segsum += seg6;
608c8a4ce0bSDick Kennedy 
609c8a4ce0bSDick Kennedy 		seg7 = ctxp->ts_status_wqput - ctxp->ts_isr_cmd;
610c8a4ce0bSDick Kennedy 		if (segsum > seg7)
611c8a4ce0bSDick Kennedy 			return;
612c8a4ce0bSDick Kennedy 		seg7 -= segsum;
613c8a4ce0bSDick Kennedy 		segsum += seg7;
614c8a4ce0bSDick Kennedy 
615c8a4ce0bSDick Kennedy 		seg8 = ctxp->ts_isr_status - ctxp->ts_isr_cmd;
616c8a4ce0bSDick Kennedy 		if (segsum > seg8)
617c8a4ce0bSDick Kennedy 			return;
618c8a4ce0bSDick Kennedy 		seg8 -= segsum;
619c8a4ce0bSDick Kennedy 		segsum += seg8;
620c8a4ce0bSDick Kennedy 
621c8a4ce0bSDick Kennedy 		seg9 = ctxp->ts_status_nvme - ctxp->ts_isr_cmd;
622c8a4ce0bSDick Kennedy 		if (segsum > seg9)
623c8a4ce0bSDick Kennedy 			return;
624c8a4ce0bSDick Kennedy 		seg9 -= segsum;
625c8a4ce0bSDick Kennedy 		segsum += seg9;
626c8a4ce0bSDick Kennedy 
627c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_status < ctxp->ts_isr_cmd)
628c8a4ce0bSDick Kennedy 			return;
6292b65e182SJames Smart 		seg10 = (ctxp->ts_isr_status -
6302b65e182SJames Smart 			ctxp->ts_isr_cmd);
6312b65e182SJames Smart 	} else {
632c8a4ce0bSDick Kennedy 		if (ctxp->ts_isr_data < ctxp->ts_isr_cmd)
633c8a4ce0bSDick Kennedy 			return;
6342b65e182SJames Smart 		seg6 =  0;
6352b65e182SJames Smart 		seg7 =  0;
6362b65e182SJames Smart 		seg8 =  0;
6372b65e182SJames Smart 		seg9 =  0;
6382b65e182SJames Smart 		seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
6392b65e182SJames Smart 	}
6402b65e182SJames Smart 
6412b65e182SJames Smart 	phba->ktime_seg1_total += seg1;
6422b65e182SJames Smart 	if (seg1 < phba->ktime_seg1_min)
6432b65e182SJames Smart 		phba->ktime_seg1_min = seg1;
6442b65e182SJames Smart 	else if (seg1 > phba->ktime_seg1_max)
6452b65e182SJames Smart 		phba->ktime_seg1_max = seg1;
6462b65e182SJames Smart 
6472b65e182SJames Smart 	phba->ktime_seg2_total += seg2;
6482b65e182SJames Smart 	if (seg2 < phba->ktime_seg2_min)
6492b65e182SJames Smart 		phba->ktime_seg2_min = seg2;
6502b65e182SJames Smart 	else if (seg2 > phba->ktime_seg2_max)
6512b65e182SJames Smart 		phba->ktime_seg2_max = seg2;
6522b65e182SJames Smart 
6532b65e182SJames Smart 	phba->ktime_seg3_total += seg3;
6542b65e182SJames Smart 	if (seg3 < phba->ktime_seg3_min)
6552b65e182SJames Smart 		phba->ktime_seg3_min = seg3;
6562b65e182SJames Smart 	else if (seg3 > phba->ktime_seg3_max)
6572b65e182SJames Smart 		phba->ktime_seg3_max = seg3;
6582b65e182SJames Smart 
6592b65e182SJames Smart 	phba->ktime_seg4_total += seg4;
6602b65e182SJames Smart 	if (seg4 < phba->ktime_seg4_min)
6612b65e182SJames Smart 		phba->ktime_seg4_min = seg4;
6622b65e182SJames Smart 	else if (seg4 > phba->ktime_seg4_max)
6632b65e182SJames Smart 		phba->ktime_seg4_max = seg4;
6642b65e182SJames Smart 
6652b65e182SJames Smart 	phba->ktime_seg5_total += seg5;
6662b65e182SJames Smart 	if (seg5 < phba->ktime_seg5_min)
6672b65e182SJames Smart 		phba->ktime_seg5_min = seg5;
6682b65e182SJames Smart 	else if (seg5 > phba->ktime_seg5_max)
6692b65e182SJames Smart 		phba->ktime_seg5_max = seg5;
6702b65e182SJames Smart 
6712b65e182SJames Smart 	phba->ktime_data_samples++;
6722b65e182SJames Smart 	if (!seg6)
6732b65e182SJames Smart 		goto out;
6742b65e182SJames Smart 
6752b65e182SJames Smart 	phba->ktime_seg6_total += seg6;
6762b65e182SJames Smart 	if (seg6 < phba->ktime_seg6_min)
6772b65e182SJames Smart 		phba->ktime_seg6_min = seg6;
6782b65e182SJames Smart 	else if (seg6 > phba->ktime_seg6_max)
6792b65e182SJames Smart 		phba->ktime_seg6_max = seg6;
6802b65e182SJames Smart 
6812b65e182SJames Smart 	phba->ktime_seg7_total += seg7;
6822b65e182SJames Smart 	if (seg7 < phba->ktime_seg7_min)
6832b65e182SJames Smart 		phba->ktime_seg7_min = seg7;
6842b65e182SJames Smart 	else if (seg7 > phba->ktime_seg7_max)
6852b65e182SJames Smart 		phba->ktime_seg7_max = seg7;
6862b65e182SJames Smart 
6872b65e182SJames Smart 	phba->ktime_seg8_total += seg8;
6882b65e182SJames Smart 	if (seg8 < phba->ktime_seg8_min)
6892b65e182SJames Smart 		phba->ktime_seg8_min = seg8;
6902b65e182SJames Smart 	else if (seg8 > phba->ktime_seg8_max)
6912b65e182SJames Smart 		phba->ktime_seg8_max = seg8;
6922b65e182SJames Smart 
6932b65e182SJames Smart 	phba->ktime_seg9_total += seg9;
6942b65e182SJames Smart 	if (seg9 < phba->ktime_seg9_min)
6952b65e182SJames Smart 		phba->ktime_seg9_min = seg9;
6962b65e182SJames Smart 	else if (seg9 > phba->ktime_seg9_max)
6972b65e182SJames Smart 		phba->ktime_seg9_max = seg9;
6982b65e182SJames Smart out:
6992b65e182SJames Smart 	phba->ktime_seg10_total += seg10;
7002b65e182SJames Smart 	if (seg10 < phba->ktime_seg10_min)
7012b65e182SJames Smart 		phba->ktime_seg10_min = seg10;
7022b65e182SJames Smart 	else if (seg10 > phba->ktime_seg10_max)
7032b65e182SJames Smart 		phba->ktime_seg10_max = seg10;
7042b65e182SJames Smart 	phba->ktime_status_samples++;
7052b65e182SJames Smart }
7062b65e182SJames Smart #endif
7072b65e182SJames Smart 
708d613b6a7SJames Smart /**
709d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
710d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
711d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
712a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
713d613b6a7SJames Smart  *
714d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
715d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME FCP commands
716d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
717d613b6a7SJames Smart  **/
718d613b6a7SJames Smart static void
lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdwqe,struct lpfc_iocbq * rspwqe)719d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
720a680a929SJames Smart 			  struct lpfc_iocbq *rspwqe)
721d613b6a7SJames Smart {
722d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
723d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
7247cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
725e733f8a8SXiu Jianfeng 	uint32_t status, result, op, logerr;
726a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
7272b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
728840eda96SJames Smart 	int id;
7292b65e182SJames Smart #endif
730d613b6a7SJames Smart 
731d51cf5bdSJames Smart 	ctxp = cmdwqe->context_un.axchg;
7327b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_IO_INP;
73386c67379SJames Smart 
7347cacae2aSJames Smart 	rsp = &ctxp->hdlrctx.fcp_req;
735d613b6a7SJames Smart 	op = rsp->op;
736d613b6a7SJames Smart 
737d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
738d613b6a7SJames Smart 	result = wcqe->parameter;
739d613b6a7SJames Smart 
74086c67379SJames Smart 	if (phba->targetport)
74186c67379SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
74286c67379SJames Smart 	else
74386c67379SJames Smart 		tgtp = NULL;
744d613b6a7SJames Smart 
7452b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
7462b65e182SJames Smart 			 ctxp->oxid, op, status);
7472b65e182SJames Smart 
748d613b6a7SJames Smart 	if (status) {
749d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
750d613b6a7SJames Smart 		rsp->transferred_length = 0;
7514b056682SJames Smart 		if (tgtp) {
752d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_error);
7538ae33701SJames Smart 			if (result == IOERR_ABORT_REQUESTED)
7544b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_aborted);
7554b056682SJames Smart 		}
75686c67379SJames Smart 
757e3246a12SJames Smart 		logerr = LOG_NVME_IOERR;
758e3246a12SJames Smart 
75986c67379SJames Smart 		/* pick up SLI4 exhange busy condition */
76086c67379SJames Smart 		if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
7617b7f551bSJames Smart 			ctxp->flag |= LPFC_NVME_XBUSY;
762e3246a12SJames Smart 			logerr |= LOG_NVME_ABTS;
7634b056682SJames Smart 			if (tgtp)
7644b056682SJames Smart 				atomic_inc(&tgtp->xmt_fcp_rsp_xb_set);
76586c67379SJames Smart 
76686c67379SJames Smart 		} else {
7677b7f551bSJames Smart 			ctxp->flag &= ~LPFC_NVME_XBUSY;
76886c67379SJames Smart 		}
76986c67379SJames Smart 
770e3246a12SJames Smart 		lpfc_printf_log(phba, KERN_INFO, logerr,
77179d8c4ceSJames Smart 				"6315 IO Error Cmpl oxid: x%x xri: x%x %x/%x "
77279d8c4ceSJames Smart 				"XBUSY:x%x\n",
77379d8c4ceSJames Smart 				ctxp->oxid, ctxp->ctxbuf->sglq->sli4_xritag,
77479d8c4ceSJames Smart 				status, result, ctxp->flag);
775e3246a12SJames Smart 
776d613b6a7SJames Smart 	} else {
777d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_SUCCESS;
778d613b6a7SJames Smart 		if (op == NVMET_FCOP_RSP)
779d613b6a7SJames Smart 			rsp->transferred_length = rsp->rsplen;
780d613b6a7SJames Smart 		else
781d613b6a7SJames Smart 			rsp->transferred_length = rsp->transfer_length;
78286c67379SJames Smart 		if (tgtp)
783d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
784d613b6a7SJames Smart 	}
785d613b6a7SJames Smart 
786d613b6a7SJames Smart 	if ((op == NVMET_FCOP_READDATA_RSP) ||
787d613b6a7SJames Smart 	    (op == NVMET_FCOP_RSP)) {
788d613b6a7SJames Smart 		/* Sanity check */
7897b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_DONE;
790d613b6a7SJames Smart 		ctxp->entry_cnt++;
79186c67379SJames Smart 
7922b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
793c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
7942b65e182SJames Smart 			if (rsp->op == NVMET_FCOP_READDATA_RSP) {
7952b65e182SJames Smart 				ctxp->ts_isr_data =
7962b65e182SJames Smart 					cmdwqe->isr_timestamp;
7972b65e182SJames Smart 				ctxp->ts_data_nvme =
7982b65e182SJames Smart 					ktime_get_ns();
7992b65e182SJames Smart 				ctxp->ts_nvme_status =
8002b65e182SJames Smart 					ctxp->ts_data_nvme;
8012b65e182SJames Smart 				ctxp->ts_status_wqput =
8022b65e182SJames Smart 					ctxp->ts_data_nvme;
8032b65e182SJames Smart 				ctxp->ts_isr_status =
8042b65e182SJames Smart 					ctxp->ts_data_nvme;
8052b65e182SJames Smart 				ctxp->ts_status_nvme =
8062b65e182SJames Smart 					ctxp->ts_data_nvme;
8072b65e182SJames Smart 			} else {
8082b65e182SJames Smart 				ctxp->ts_isr_status =
8092b65e182SJames Smart 					cmdwqe->isr_timestamp;
8102b65e182SJames Smart 				ctxp->ts_status_nvme =
8112b65e182SJames Smart 					ktime_get_ns();
8122b65e182SJames Smart 			}
8132b65e182SJames Smart 		}
8142b65e182SJames Smart #endif
815d613b6a7SJames Smart 		rsp->done(rsp);
8162b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
817c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme)
8182b65e182SJames Smart 			lpfc_nvmet_ktime(phba, ctxp);
8192b65e182SJames Smart #endif
82019b58d94SJames Smart 		/* lpfc_nvmet_xmt_fcp_release() will recycle the context */
821d613b6a7SJames Smart 	} else {
822d613b6a7SJames Smart 		ctxp->entry_cnt++;
823e733f8a8SXiu Jianfeng 		memset_startat(cmdwqe, 0, cmd_flag);
8242b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
825c8a4ce0bSDick Kennedy 		if (ctxp->ts_cmd_nvme) {
8262b65e182SJames Smart 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
8272b65e182SJames Smart 			ctxp->ts_data_nvme = ktime_get_ns();
8282b65e182SJames Smart 		}
8292b65e182SJames Smart #endif
830d613b6a7SJames Smart 		rsp->done(rsp);
831d613b6a7SJames Smart 	}
83263df6d63SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
833840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
834d6d189ceSBart Van Assche 		id = raw_smp_processor_id();
835840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
83663df6d63SJames Smart 		if (ctxp->cpu != id)
83763df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
83863df6d63SJames Smart 					"6704 CPU Check cmdcmpl: "
83963df6d63SJames Smart 					"cpu %d expect %d\n",
84063df6d63SJames Smart 					id, ctxp->cpu);
84163df6d63SJames Smart 	}
84263df6d63SJames Smart #endif
843d613b6a7SJames Smart }
844d613b6a7SJames Smart 
845fe1bedecSJames Smart /**
846fe1bedecSJames Smart  * __lpfc_nvme_xmt_ls_rsp - Generic service routine to issue transmit
847fe1bedecSJames Smart  *         an NVME LS rsp for a prior NVME LS request that was received.
848fe1bedecSJames Smart  * @axchg: pointer to exchange context for the NVME LS request the response
849fe1bedecSJames Smart  *         is for.
850fe1bedecSJames Smart  * @ls_rsp: pointer to the transport LS RSP that is to be sent
851fe1bedecSJames Smart  * @xmt_ls_rsp_cmp: completion routine to call upon RSP transmit done
852fe1bedecSJames Smart  *
853fe1bedecSJames Smart  * This routine is used to format and send a WQE to transmit a NVME LS
854fe1bedecSJames Smart  * Response.  The response is for a prior NVME LS request that was
855fe1bedecSJames Smart  * received and posted to the transport.
856fe1bedecSJames Smart  *
857fe1bedecSJames Smart  * Returns:
858fe1bedecSJames Smart  *  0 : if response successfully transmit
859fe1bedecSJames Smart  *  non-zero : if response failed to transmit, of the form -Exxx.
860fe1bedecSJames Smart  **/
861fe1bedecSJames Smart int
__lpfc_nvme_xmt_ls_rsp(struct lpfc_async_xchg_ctx * axchg,struct nvmefc_ls_rsp * ls_rsp,void (* xmt_ls_rsp_cmp)(struct lpfc_hba * phba,struct lpfc_iocbq * cmdwqe,struct lpfc_iocbq * rspwqe))862fe1bedecSJames Smart __lpfc_nvme_xmt_ls_rsp(struct lpfc_async_xchg_ctx *axchg,
863fe1bedecSJames Smart 			struct nvmefc_ls_rsp *ls_rsp,
864fe1bedecSJames Smart 			void (*xmt_ls_rsp_cmp)(struct lpfc_hba *phba,
865fe1bedecSJames Smart 				struct lpfc_iocbq *cmdwqe,
866a680a929SJames Smart 				struct lpfc_iocbq *rspwqe))
867d613b6a7SJames Smart {
868fe1bedecSJames Smart 	struct lpfc_hba *phba = axchg->phba;
869fe1bedecSJames Smart 	struct hbq_dmabuf *nvmebuf = (struct hbq_dmabuf *)axchg->rqb_buffer;
870d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
871d613b6a7SJames Smart 	struct lpfc_dmabuf dmabuf;
872d613b6a7SJames Smart 	struct ulp_bde64 bpl;
873d613b6a7SJames Smart 	int rc;
874d613b6a7SJames Smart 
8751abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
8761abcb371SDick Kennedy 		return -ENODEV;
8771abcb371SDick Kennedy 
878d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
879fe1bedecSJames Smart 			"6023 NVMEx LS rsp oxid x%x\n", axchg->oxid);
880ce1b591cSJames Smart 
881fe1bedecSJames Smart 	if (axchg->state != LPFC_NVME_STE_LS_RCV || axchg->entry_cnt != 1) {
882372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
883fe1bedecSJames Smart 				"6412 NVMEx LS rsp state mismatch "
884ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
885fe1bedecSJames Smart 				axchg->oxid, axchg->state, axchg->entry_cnt);
886fe1bedecSJames Smart 		return -EALREADY;
887ce1b591cSJames Smart 	}
888fe1bedecSJames Smart 	axchg->state = LPFC_NVME_STE_LS_RSP;
889fe1bedecSJames Smart 	axchg->entry_cnt++;
890d613b6a7SJames Smart 
891fe1bedecSJames Smart 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, axchg, ls_rsp->rspdma,
892fe1bedecSJames Smart 					 ls_rsp->rsplen);
893d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
894372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
895fe1bedecSJames Smart 				"6150 NVMEx LS Drop Rsp x%x: Prep\n",
896fe1bedecSJames Smart 				axchg->oxid);
897fe1bedecSJames Smart 		rc = -ENOMEM;
898fe1bedecSJames Smart 		goto out_free_buf;
899d613b6a7SJames Smart 	}
900d613b6a7SJames Smart 
901d613b6a7SJames Smart 	/* Save numBdes for bpl2sgl */
902a680a929SJames Smart 	nvmewqeq->num_bdes = 1;
903d613b6a7SJames Smart 	nvmewqeq->hba_wqidx = 0;
904d51cf5bdSJames Smart 	nvmewqeq->bpl_dmabuf = &dmabuf;
905d613b6a7SJames Smart 	dmabuf.virt = &bpl;
906d613b6a7SJames Smart 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
907d613b6a7SJames Smart 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
908fe1bedecSJames Smart 	bpl.tus.f.bdeSize = ls_rsp->rsplen;
909d613b6a7SJames Smart 	bpl.tus.f.bdeFlags = 0;
910d613b6a7SJames Smart 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
911fe1bedecSJames Smart 	/*
912fe1bedecSJames Smart 	 * Note: although we're using stack space for the dmabuf, the
913fe1bedecSJames Smart 	 * call to lpfc_sli4_issue_wqe is synchronous, so it will not
914fe1bedecSJames Smart 	 * be referenced after it returns back to this routine.
915fe1bedecSJames Smart 	 */
916d613b6a7SJames Smart 
917a680a929SJames Smart 	nvmewqeq->cmd_cmpl = xmt_ls_rsp_cmp;
918d51cf5bdSJames Smart 	nvmewqeq->context_un.axchg = axchg;
919d613b6a7SJames Smart 
920fe1bedecSJames Smart 	lpfc_nvmeio_data(phba, "NVMEx LS RSP: xri x%x wqidx x%x len x%x\n",
921fe1bedecSJames Smart 			 axchg->oxid, nvmewqeq->hba_wqidx, ls_rsp->rsplen);
9222b65e182SJames Smart 
923fe1bedecSJames Smart 	rc = lpfc_sli4_issue_wqe(phba, axchg->hdwq, nvmewqeq);
924fe1bedecSJames Smart 
925fe1bedecSJames Smart 	/* clear to be sure there's no reference */
926d51cf5bdSJames Smart 	nvmewqeq->bpl_dmabuf = NULL;
927fe1bedecSJames Smart 
928d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
929d613b6a7SJames Smart 		/*
930d613b6a7SJames Smart 		 * Okay to repost buffer here, but wait till cmpl
931d613b6a7SJames Smart 		 * before freeing ctxp and iocbq.
932d613b6a7SJames Smart 		 */
933d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
934d613b6a7SJames Smart 		return 0;
935d613b6a7SJames Smart 	}
936fe1bedecSJames Smart 
937372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
938fe1bedecSJames Smart 			"6151 NVMEx LS RSP x%x: failed to transmit %d\n",
939fe1bedecSJames Smart 			axchg->oxid, rc);
940fe1bedecSJames Smart 
941fe1bedecSJames Smart 	rc = -ENXIO;
942d613b6a7SJames Smart 
943d51cf5bdSJames Smart 	lpfc_nlp_put(nvmewqeq->ndlp);
944d613b6a7SJames Smart 
945fe1bedecSJames Smart out_free_buf:
946fe1bedecSJames Smart 	/* Give back resources */
947d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
948fe1bedecSJames Smart 
949fe1bedecSJames Smart 	/*
950fe1bedecSJames Smart 	 * As transport doesn't track completions of responses, if the rsp
951fe1bedecSJames Smart 	 * fails to send, the transport will effectively ignore the rsp
952fe1bedecSJames Smart 	 * and consider the LS done. However, the driver has an active
953fe1bedecSJames Smart 	 * exchange open for the LS - so be sure to abort the exchange
954fe1bedecSJames Smart 	 * if the response isn't sent.
955fe1bedecSJames Smart 	 */
956fe1bedecSJames Smart 	lpfc_nvme_unsol_ls_issue_abort(phba, axchg, axchg->sid, axchg->oxid);
957fe1bedecSJames Smart 	return rc;
958fe1bedecSJames Smart }
959fe1bedecSJames Smart 
960fe1bedecSJames Smart /**
961fe1bedecSJames Smart  * lpfc_nvmet_xmt_ls_rsp - Transmit NVME LS response
962fe1bedecSJames Smart  * @tgtport: pointer to target port that NVME LS is to be transmit from.
963fe1bedecSJames Smart  * @ls_rsp: pointer to the transport LS RSP that is to be sent
964fe1bedecSJames Smart  *
965fe1bedecSJames Smart  * Driver registers this routine to transmit responses for received NVME
966fe1bedecSJames Smart  * LS requests.
967fe1bedecSJames Smart  *
968fe1bedecSJames Smart  * This routine is used to format and send a WQE to transmit a NVME LS
969fe1bedecSJames Smart  * Response. The ls_rsp is used to reverse-map the LS to the original
970fe1bedecSJames Smart  * NVME LS request sequence, which provides addressing information for
971fe1bedecSJames Smart  * the remote port the LS to be sent to, as well as the exchange id
972fe1bedecSJames Smart  * that is the LS is bound to.
973fe1bedecSJames Smart  *
974fe1bedecSJames Smart  * Returns:
975fe1bedecSJames Smart  *  0 : if response successfully transmit
976fe1bedecSJames Smart  *  non-zero : if response failed to transmit, of the form -Exxx.
977fe1bedecSJames Smart  **/
978fe1bedecSJames Smart static int
lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port * tgtport,struct nvmefc_ls_rsp * ls_rsp)979fe1bedecSJames Smart lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
980fe1bedecSJames Smart 		      struct nvmefc_ls_rsp *ls_rsp)
981fe1bedecSJames Smart {
982fe1bedecSJames Smart 	struct lpfc_async_xchg_ctx *axchg =
983fe1bedecSJames Smart 		container_of(ls_rsp, struct lpfc_async_xchg_ctx, ls_rsp);
984fe1bedecSJames Smart 	struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
985fe1bedecSJames Smart 	int rc;
986fe1bedecSJames Smart 
987fe1bedecSJames Smart 	if (axchg->phba->pport->load_flag & FC_UNLOADING)
988fe1bedecSJames Smart 		return -ENODEV;
989fe1bedecSJames Smart 
990fe1bedecSJames Smart 	rc = __lpfc_nvme_xmt_ls_rsp(axchg, ls_rsp, lpfc_nvmet_xmt_ls_rsp_cmp);
991fe1bedecSJames Smart 
992fe1bedecSJames Smart 	if (rc) {
993fe1bedecSJames Smart 		atomic_inc(&nvmep->xmt_ls_drop);
994fe1bedecSJames Smart 		/*
995fe1bedecSJames Smart 		 * unless the failure is due to having already sent
996fe1bedecSJames Smart 		 * the response, an abort will be generated for the
997fe1bedecSJames Smart 		 * exchange if the rsp can't be sent.
998fe1bedecSJames Smart 		 */
999fe1bedecSJames Smart 		if (rc != -EALREADY)
1000547077a4SJames Smart 			atomic_inc(&nvmep->xmt_ls_abort);
1001fe1bedecSJames Smart 		return rc;
1002fe1bedecSJames Smart 	}
1003fe1bedecSJames Smart 
1004fe1bedecSJames Smart 	atomic_inc(&nvmep->xmt_ls_rsp);
1005fe1bedecSJames Smart 	return 0;
1006d613b6a7SJames Smart }
1007d613b6a7SJames Smart 
1008d613b6a7SJames Smart static int
lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port * tgtport,struct nvmefc_tgt_fcp_req * rsp)1009d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
1010d613b6a7SJames Smart 		      struct nvmefc_tgt_fcp_req *rsp)
1011d613b6a7SJames Smart {
1012d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
10137cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
10147cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
1015d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
10166e8e1c14SJames Smart 	struct lpfc_queue *wq;
1017d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
10186e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
10196e8e1c14SJames Smart 	unsigned long iflags;
1020223b78eaSArnd Bergmann 	int rc;
1021840eda96SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1022840eda96SJames Smart 	int id;
1023840eda96SJames Smart #endif
10242b65e182SJames Smart 
10251abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING) {
10261abcb371SDick Kennedy 		rc = -ENODEV;
10271abcb371SDick Kennedy 		goto aerr;
10281abcb371SDick Kennedy 	}
10291abcb371SDick Kennedy 
10302b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1031c8a4ce0bSDick Kennedy 	if (ctxp->ts_cmd_nvme) {
10322b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
10332b65e182SJames Smart 			ctxp->ts_nvme_status = ktime_get_ns();
10342b65e182SJames Smart 		else
10352b65e182SJames Smart 			ctxp->ts_nvme_data = ktime_get_ns();
10362b65e182SJames Smart 	}
10371fbf9742SJames Smart 
10381fbf9742SJames Smart 	/* Setup the hdw queue if not already set */
10391fbf9742SJames Smart 	if (!ctxp->hdwq)
10401fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[rsp->hwqid];
10411fbf9742SJames Smart 
1042840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
1043840eda96SJames Smart 		id = raw_smp_processor_id();
1044840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
104563df6d63SJames Smart 		if (rsp->hwqid != id)
104663df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
10472b65e182SJames Smart 					"6705 CPU Check OP: "
10482b65e182SJames Smart 					"cpu %d expect %d\n",
10492b65e182SJames Smart 					id, rsp->hwqid);
105063df6d63SJames Smart 		ctxp->cpu = id; /* Setup cpu for cmpl check */
10512b65e182SJames Smart 	}
10522b65e182SJames Smart #endif
1053d613b6a7SJames Smart 
1054d613b6a7SJames Smart 	/* Sanity check */
10557b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_ABTS_RCV) ||
10567b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_ABORT)) {
1057d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1058372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
105979d8c4ceSJames Smart 				"6102 IO oxid x%x aborted\n",
1060d613b6a7SJames Smart 				ctxp->oxid);
1061a5068b46SJames Smart 		rc = -ENXIO;
1062d613b6a7SJames Smart 		goto aerr;
1063d613b6a7SJames Smart 	}
1064d613b6a7SJames Smart 
1065d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
1066d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
1067d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1068372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1069d613b6a7SJames Smart 				"6152 FCP Drop IO x%x: Prep\n",
1070d613b6a7SJames Smart 				ctxp->oxid);
1071a5068b46SJames Smart 		rc = -ENXIO;
1072d613b6a7SJames Smart 		goto aerr;
1073d613b6a7SJames Smart 	}
1074d613b6a7SJames Smart 
1075a680a929SJames Smart 	nvmewqeq->cmd_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
1076d51cf5bdSJames Smart 	nvmewqeq->context_un.axchg = ctxp;
1077a680a929SJames Smart 	nvmewqeq->cmd_flag |=  LPFC_IO_NVMET;
1078d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
1079d613b6a7SJames Smart 
10802b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
10812b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
10822b65e182SJames Smart 
10837b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_IO_INP;
10841fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
1085d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
10862b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1087c8a4ce0bSDick Kennedy 		if (!ctxp->ts_cmd_nvme)
10882b65e182SJames Smart 			return 0;
10892b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
10902b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
10912b65e182SJames Smart 		else
10922b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
10932b65e182SJames Smart #endif
1094d613b6a7SJames Smart 		return 0;
1095d613b6a7SJames Smart 	}
1096d613b6a7SJames Smart 
10976e8e1c14SJames Smart 	if (rc == -EBUSY) {
10986e8e1c14SJames Smart 		/*
10996e8e1c14SJames Smart 		 * WQ was full, so queue nvmewqeq to be sent after
11006e8e1c14SJames Smart 		 * WQE release CQE
11016e8e1c14SJames Smart 		 */
11027b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_DEFER_WQFULL;
1103c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
11046e8e1c14SJames Smart 		pring = wq->pring;
11056e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
11066e8e1c14SJames Smart 		list_add_tail(&nvmewqeq->list, &wq->wqfull_list);
11076e8e1c14SJames Smart 		wq->q_flag |= HBA_NVMET_WQFULL;
11086e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
1109411de511SJames Smart 		atomic_inc(&lpfc_nvmep->defer_wqfull);
11106e8e1c14SJames Smart 		return 0;
11116e8e1c14SJames Smart 	}
11126e8e1c14SJames Smart 
1113d613b6a7SJames Smart 	/* Give back resources */
1114d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
1115372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1116d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
1117d613b6a7SJames Smart 			ctxp->oxid, rc);
1118d613b6a7SJames Smart 
1119d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
1120d51cf5bdSJames Smart 	nvmewqeq->context_un.axchg = NULL;
1121d51cf5bdSJames Smart 	nvmewqeq->bpl_dmabuf = NULL;
1122a5068b46SJames Smart 	rc = -EBUSY;
1123d613b6a7SJames Smart aerr:
1124a5068b46SJames Smart 	return rc;
1125d613b6a7SJames Smart }
1126d613b6a7SJames Smart 
1127d613b6a7SJames Smart static void
lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port * targetport)1128d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
1129d613b6a7SJames Smart {
1130d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
1131d613b6a7SJames Smart 
1132d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
1133c41f5988SEwan D. Milne 	if (tport->phba->targetport)
1134c41f5988SEwan D. Milne 		complete(tport->tport_unreg_cmp);
1135d613b6a7SJames Smart }
1136d613b6a7SJames Smart 
113719b58d94SJames Smart static void
lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port * tgtport,struct nvmefc_tgt_fcp_req * req)1138a97ec51bSJames Smart lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
1139a97ec51bSJames Smart 			 struct nvmefc_tgt_fcp_req *req)
1140a97ec51bSJames Smart {
1141a97ec51bSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
11427cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
11437cacae2aSJames Smart 		container_of(req, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
1144a97ec51bSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
11456e8e1c14SJames Smart 	struct lpfc_queue *wq;
114686c67379SJames Smart 	unsigned long flags;
1147a97ec51bSJames Smart 
11481abcb371SDick Kennedy 	if (phba->pport->load_flag & FC_UNLOADING)
11491abcb371SDick Kennedy 		return;
11501abcb371SDick Kennedy 
11511fbf9742SJames Smart 	if (!ctxp->hdwq)
11521fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[0];
11531fbf9742SJames Smart 
1154a97ec51bSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
115579d8c4ceSJames Smart 			"6103 NVMET Abort op: oxid x%x flg x%x ste %d\n",
1156ce1b591cSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
1157a97ec51bSJames Smart 
1158ce1b591cSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x flg x%x ste x%x\n",
1159ce1b591cSJames Smart 			 ctxp->oxid, ctxp->flag, ctxp->state);
1160a97ec51bSJames Smart 
1161a97ec51bSJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
1162ce1b591cSJames Smart 
116386c67379SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
116486c67379SJames Smart 
116586c67379SJames Smart 	/* Since iaab/iaar are NOT set, we need to check
116686c67379SJames Smart 	 * if the firmware is in process of aborting IO
116786c67379SJames Smart 	 */
11687b7f551bSJames Smart 	if (ctxp->flag & (LPFC_NVME_XBUSY | LPFC_NVME_ABORT_OP)) {
116986c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
117086c67379SJames Smart 		return;
117186c67379SJames Smart 	}
11727b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_ABORT_OP;
1173ce1b591cSJames Smart 
11747b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_DEFER_WQFULL) {
1175c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11766e8e1c14SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
11776e8e1c14SJames Smart 						 ctxp->oxid);
1178c00f62e6SJames Smart 		wq = ctxp->hdwq->io_wq;
11796e8e1c14SJames Smart 		lpfc_nvmet_wqfull_flush(phba, wq, ctxp);
11806e8e1c14SJames Smart 		return;
11816e8e1c14SJames Smart 	}
1182c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
11836e8e1c14SJames Smart 
11847b7f551bSJames Smart 	/* A state of LPFC_NVME_STE_RCV means we have just received
1185ce1b591cSJames Smart 	 * the NVME command and have not started processing it.
1186ce1b591cSJames Smart 	 * (by issuing any IO WQEs on this exchange yet)
1187ce1b591cSJames Smart 	 */
11887b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_RCV)
1189ce1b591cSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1190a97ec51bSJames Smart 						 ctxp->oxid);
1191a97ec51bSJames Smart 	else
1192ce1b591cSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1193a97ec51bSJames Smart 					       ctxp->oxid);
1194a97ec51bSJames Smart }
1195a97ec51bSJames Smart 
1196a97ec51bSJames Smart static void
lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port * tgtport,struct nvmefc_tgt_fcp_req * rsp)119719b58d94SJames Smart lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
119819b58d94SJames Smart 			   struct nvmefc_tgt_fcp_req *rsp)
119919b58d94SJames Smart {
1200547077a4SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
12017cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
12027cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
120319b58d94SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
120419b58d94SJames Smart 	unsigned long flags;
120519b58d94SJames Smart 	bool aborting = false;
120619b58d94SJames Smart 
1207c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
12087b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_XBUSY)
1209c160c0f8SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1210c160c0f8SJames Smart 				"6027 NVMET release with XBUSY flag x%x"
1211c160c0f8SJames Smart 				" oxid x%x\n",
1212c160c0f8SJames Smart 				ctxp->flag, ctxp->oxid);
12137b7f551bSJames Smart 	else if (ctxp->state != LPFC_NVME_STE_DONE &&
12147b7f551bSJames Smart 		 ctxp->state != LPFC_NVME_STE_ABORT)
1215372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1216ce1b591cSJames Smart 				"6413 NVMET release bad state %d %d oxid x%x\n",
1217ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
1218ce1b591cSJames Smart 
12197b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_ABORT_OP) ||
12207b7f551bSJames Smart 	    (ctxp->flag & LPFC_NVME_XBUSY)) {
122119b58d94SJames Smart 		aborting = true;
122286c67379SJames Smart 		/* let the abort path do the real release */
122386c67379SJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
122419b58d94SJames Smart 	}
122519b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
122619b58d94SJames Smart 
12279094367aSArnd Bergmann 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d abt %d\n", ctxp->oxid,
12289094367aSArnd Bergmann 			 ctxp->state, aborting);
122919b58d94SJames Smart 
1230547077a4SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_release);
12317b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_TNOTIFY;
1232547077a4SJames Smart 
123386c67379SJames Smart 	if (aborting)
123486c67379SJames Smart 		return;
123586c67379SJames Smart 
12366c621a22SJames Smart 	lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
123719b58d94SJames Smart }
123819b58d94SJames Smart 
123950738420SJames Smart static void
lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port * tgtport,struct nvmefc_tgt_fcp_req * rsp)124050738420SJames Smart lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port *tgtport,
124150738420SJames Smart 		     struct nvmefc_tgt_fcp_req *rsp)
124250738420SJames Smart {
124350738420SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
12447cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp =
12457cacae2aSJames Smart 		container_of(rsp, struct lpfc_async_xchg_ctx, hdlrctx.fcp_req);
124650738420SJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
124750738420SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
1248472e146dSJames Smart 	unsigned long iflag;
1249472e146dSJames Smart 
125050738420SJames Smart 
125150738420SJames Smart 	lpfc_nvmeio_data(phba, "NVMET DEFERRCV: xri x%x sz %d CPU %02x\n",
1252d6d189ceSBart Van Assche 			 ctxp->oxid, ctxp->size, raw_smp_processor_id());
125350738420SJames Smart 
12546871e814SJames Smart 	if (!nvmebuf) {
12556871e814SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
125679d8c4ceSJames Smart 				"6425 Defer rcv: no buffer oxid x%x: "
12576871e814SJames Smart 				"flg %x ste %x\n",
12586871e814SJames Smart 				ctxp->oxid, ctxp->flag, ctxp->state);
12596871e814SJames Smart 		return;
12606871e814SJames Smart 	}
12616871e814SJames Smart 
126250738420SJames Smart 	tgtp = phba->targetport->private;
12636871e814SJames Smart 	if (tgtp)
126450738420SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_defer);
1265411de511SJames Smart 
1266411de511SJames Smart 	/* Free the nvmebuf since a new buffer already replaced it */
1267cbc5de1bSJames Smart 	nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
1268472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflag);
1269472e146dSJames Smart 	ctxp->rqb_buffer = NULL;
1270472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
127150738420SJames Smart }
127250738420SJames Smart 
127354840bedSJames Smart /**
127454840bedSJames Smart  * lpfc_nvmet_ls_req_cmp - completion handler for a nvme ls request
127554840bedSJames Smart  * @phba: Pointer to HBA context object
127654840bedSJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1277a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
127854840bedSJames Smart  *
127954840bedSJames Smart  * This function is the completion handler for NVME LS requests.
128054840bedSJames Smart  * The function updates any states and statistics, then calls the
128154840bedSJames Smart  * generic completion handler to finish completion of the request.
128254840bedSJames Smart  **/
128354840bedSJames Smart static void
lpfc_nvmet_ls_req_cmp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdwqe,struct lpfc_iocbq * rspwqe)128454840bedSJames Smart lpfc_nvmet_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1285a680a929SJames Smart 		      struct lpfc_iocbq *rspwqe)
128654840bedSJames Smart {
1287a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
128854840bedSJames Smart 	__lpfc_nvme_ls_req_cmp(phba, cmdwqe->vport, cmdwqe, wcqe);
128954840bedSJames Smart }
129054840bedSJames Smart 
129154840bedSJames Smart /**
129254840bedSJames Smart  * lpfc_nvmet_ls_req - Issue an Link Service request
1293d3cb79fcSLee Jones  * @targetport: pointer to target instance registered with nvmet transport.
1294d3cb79fcSLee Jones  * @hosthandle: hosthandle set by the driver in a prior ls_rqst_rcv.
129554840bedSJames Smart  *               Driver sets this value to the ndlp pointer.
1296d3cb79fcSLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
129754840bedSJames Smart  *
129854840bedSJames Smart  * Driver registers this routine to handle any link service request
129954840bedSJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
130054840bedSJames Smart  *
130154840bedSJames Smart  * Return value :
130254840bedSJames Smart  *   0 - Success
130354840bedSJames Smart  *   non-zero: various error codes, in form of -Exxx
130454840bedSJames Smart  **/
130554840bedSJames Smart static int
lpfc_nvmet_ls_req(struct nvmet_fc_target_port * targetport,void * hosthandle,struct nvmefc_ls_req * pnvme_lsreq)130654840bedSJames Smart lpfc_nvmet_ls_req(struct nvmet_fc_target_port *targetport,
130754840bedSJames Smart 		  void *hosthandle,
130854840bedSJames Smart 		  struct nvmefc_ls_req *pnvme_lsreq)
130954840bedSJames Smart {
131054840bedSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmet = targetport->private;
131154840bedSJames Smart 	struct lpfc_hba *phba;
131254840bedSJames Smart 	struct lpfc_nodelist *ndlp;
131354840bedSJames Smart 	int ret;
131454840bedSJames Smart 	u32 hstate;
131554840bedSJames Smart 
131654840bedSJames Smart 	if (!lpfc_nvmet)
131754840bedSJames Smart 		return -EINVAL;
131854840bedSJames Smart 
131954840bedSJames Smart 	phba = lpfc_nvmet->phba;
132054840bedSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
132154840bedSJames Smart 		return -EINVAL;
132254840bedSJames Smart 
132354840bedSJames Smart 	hstate = atomic_read(&lpfc_nvmet->state);
132454840bedSJames Smart 	if (hstate == LPFC_NVMET_INV_HOST_ACTIVE)
132554840bedSJames Smart 		return -EACCES;
132654840bedSJames Smart 
132754840bedSJames Smart 	ndlp = (struct lpfc_nodelist *)hosthandle;
132854840bedSJames Smart 
132954840bedSJames Smart 	ret = __lpfc_nvme_ls_req(phba->pport, ndlp, pnvme_lsreq,
133054840bedSJames Smart 				 lpfc_nvmet_ls_req_cmp);
133154840bedSJames Smart 
133254840bedSJames Smart 	return ret;
133354840bedSJames Smart }
133454840bedSJames Smart 
133554840bedSJames Smart /**
133654840bedSJames Smart  * lpfc_nvmet_ls_abort - Abort a prior NVME LS request
133754840bedSJames Smart  * @targetport: Transport targetport, that LS was issued from.
1338d3cb79fcSLee Jones  * @hosthandle: hosthandle set by the driver in a prior ls_rqst_rcv.
133954840bedSJames Smart  *               Driver sets this value to the ndlp pointer.
1340d3cb79fcSLee Jones  * @pnvme_lsreq: the transport nvme_ls_req structure for LS to be aborted
134154840bedSJames Smart  *
134254840bedSJames Smart  * Driver registers this routine to abort an NVME LS request that is
134354840bedSJames Smart  * in progress (from the transports perspective).
134454840bedSJames Smart  **/
134554840bedSJames Smart static void
lpfc_nvmet_ls_abort(struct nvmet_fc_target_port * targetport,void * hosthandle,struct nvmefc_ls_req * pnvme_lsreq)134654840bedSJames Smart lpfc_nvmet_ls_abort(struct nvmet_fc_target_port *targetport,
134754840bedSJames Smart 		    void *hosthandle,
134854840bedSJames Smart 		    struct nvmefc_ls_req *pnvme_lsreq)
134954840bedSJames Smart {
135054840bedSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmet = targetport->private;
135154840bedSJames Smart 	struct lpfc_hba *phba;
135254840bedSJames Smart 	struct lpfc_nodelist *ndlp;
135354840bedSJames Smart 	int ret;
135454840bedSJames Smart 
135554840bedSJames Smart 	phba = lpfc_nvmet->phba;
135654840bedSJames Smart 	if (phba->pport->load_flag & FC_UNLOADING)
135754840bedSJames Smart 		return;
135854840bedSJames Smart 
135954840bedSJames Smart 	ndlp = (struct lpfc_nodelist *)hosthandle;
136054840bedSJames Smart 
136154840bedSJames Smart 	ret = __lpfc_nvme_ls_abort(phba->pport, ndlp, pnvme_lsreq);
136254840bedSJames Smart 	if (!ret)
136354840bedSJames Smart 		atomic_inc(&lpfc_nvmet->xmt_ls_abort);
136454840bedSJames Smart }
136554840bedSJames Smart 
1366ab723121SJames Smart static void
lpfc_nvmet_host_release(void * hosthandle)13674c2805aaSJames Smart lpfc_nvmet_host_release(void *hosthandle)
13684c2805aaSJames Smart {
13694c2805aaSJames Smart 	struct lpfc_nodelist *ndlp = hosthandle;
1370243156c0SJames Smart 	struct lpfc_hba *phba = ndlp->phba;
13714c2805aaSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
13724c2805aaSJames Smart 
13734c2805aaSJames Smart 	if (!phba->targetport || !phba->targetport->private)
13744c2805aaSJames Smart 		return;
13754c2805aaSJames Smart 
13764c2805aaSJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1377243156c0SJames Smart 			"6202 NVMET XPT releasing hosthandle x%px "
1378243156c0SJames Smart 			"DID x%x xflags x%x refcnt %d\n",
1379243156c0SJames Smart 			hosthandle, ndlp->nlp_DID, ndlp->fc4_xpt_flags,
1380243156c0SJames Smart 			kref_read(&ndlp->kref));
13814c2805aaSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1382243156c0SJames Smart 	spin_lock_irq(&ndlp->lock);
1383243156c0SJames Smart 	ndlp->fc4_xpt_flags &= ~NLP_XPT_HAS_HH;
1384243156c0SJames Smart 	spin_unlock_irq(&ndlp->lock);
1385243156c0SJames Smart 	lpfc_nlp_put(ndlp);
13864c2805aaSJames Smart 	atomic_set(&tgtp->state, 0);
13874c2805aaSJames Smart }
13884c2805aaSJames Smart 
13894c2805aaSJames Smart static void
lpfc_nvmet_discovery_event(struct nvmet_fc_target_port * tgtport)1390ab723121SJames Smart lpfc_nvmet_discovery_event(struct nvmet_fc_target_port *tgtport)
1391ab723121SJames Smart {
1392ab723121SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1393ab723121SJames Smart 	struct lpfc_hba *phba;
1394ab723121SJames Smart 	uint32_t rc;
1395ab723121SJames Smart 
1396ab723121SJames Smart 	tgtp = tgtport->private;
1397ab723121SJames Smart 	phba = tgtp->phba;
1398ab723121SJames Smart 
1399ab723121SJames Smart 	rc = lpfc_issue_els_rscn(phba->pport, 0);
1400372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1401ab723121SJames Smart 			"6420 NVMET subsystem change: Notification %s\n",
1402ab723121SJames Smart 			(rc) ? "Failed" : "Sent");
1403ab723121SJames Smart }
1404ab723121SJames Smart 
1405d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
1406d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
1407d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
1408d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
1409a97ec51bSJames Smart 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
141019b58d94SJames Smart 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
141150738420SJames Smart 	.defer_rcv	= lpfc_nvmet_defer_rcv,
1412ab723121SJames Smart 	.discovery_event = lpfc_nvmet_discovery_event,
141354840bedSJames Smart 	.ls_req         = lpfc_nvmet_ls_req,
141454840bedSJames Smart 	.ls_abort       = lpfc_nvmet_ls_abort,
14154c2805aaSJames Smart 	.host_release   = lpfc_nvmet_host_release,
1416d613b6a7SJames Smart 
1417d613b6a7SJames Smart 	.max_hw_queues  = 1,
1418d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1419d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1420d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
1421d613b6a7SJames Smart 
1422d613b6a7SJames Smart 	/* optional features */
1423d613b6a7SJames Smart 	.target_features = 0,
1424d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
1425d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
142654840bedSJames Smart 	.lsrqst_priv_sz = 0,
1427d613b6a7SJames Smart };
1428d613b6a7SJames Smart 
142939aa23f9SColin Ian King static void
__lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba * phba,struct lpfc_nvmet_ctx_info * infop)143066d7ce93SDick Kennedy __lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba *phba,
143166d7ce93SDick Kennedy 		struct lpfc_nvmet_ctx_info *infop)
14326c621a22SJames Smart {
14336c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf, *next_ctx_buf;
14346c621a22SJames Smart 	unsigned long flags;
14356c621a22SJames Smart 
143666d7ce93SDick Kennedy 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, flags);
1437966bb5b7SJames Smart 	list_for_each_entry_safe(ctx_buf, next_ctx_buf,
143866d7ce93SDick Kennedy 				&infop->nvmet_ctx_list, list) {
14395e5b511dSJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
14406c621a22SJames Smart 		list_del_init(&ctx_buf->list);
14415e5b511dSJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
144266d7ce93SDick Kennedy 
1443a789241eSJames Smart 		spin_lock(&phba->hbalock);
144466d7ce93SDick Kennedy 		__lpfc_clear_active_sglq(phba, ctx_buf->sglq->sli4_lxritag);
1445a789241eSJames Smart 		spin_unlock(&phba->hbalock);
1446a789241eSJames Smart 
14476c621a22SJames Smart 		ctx_buf->sglq->state = SGL_FREED;
14486c621a22SJames Smart 		ctx_buf->sglq->ndlp = NULL;
14496c621a22SJames Smart 
1450c4031db7SDan Carpenter 		spin_lock(&phba->sli4_hba.sgl_list_lock);
14516c621a22SJames Smart 		list_add_tail(&ctx_buf->sglq->list,
14526c621a22SJames Smart 				&phba->sli4_hba.lpfc_nvmet_sgl_list);
1453c4031db7SDan Carpenter 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
14546c621a22SJames Smart 
14556c621a22SJames Smart 		lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
14566c621a22SJames Smart 		kfree(ctx_buf->context);
14576c621a22SJames Smart 	}
145866d7ce93SDick Kennedy 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, flags);
1459966bb5b7SJames Smart }
146066d7ce93SDick Kennedy 
146166d7ce93SDick Kennedy static void
lpfc_nvmet_cleanup_io_context(struct lpfc_hba * phba)146266d7ce93SDick Kennedy lpfc_nvmet_cleanup_io_context(struct lpfc_hba *phba)
146366d7ce93SDick Kennedy {
146466d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
146566d7ce93SDick Kennedy 	int i, j;
146666d7ce93SDick Kennedy 
146766d7ce93SDick Kennedy 	/* The first context list, MRQ 0 CPU 0 */
146866d7ce93SDick Kennedy 	infop = phba->sli4_hba.nvmet_ctx_info;
146966d7ce93SDick Kennedy 	if (!infop)
147066d7ce93SDick Kennedy 		return;
147166d7ce93SDick Kennedy 
1472442336a5SBo Liu 	/* Cycle the entire CPU context list for every MRQ */
147366d7ce93SDick Kennedy 	for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
1474222e9239SJames Smart 		for_each_present_cpu(j) {
1475222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, j, i);
147666d7ce93SDick Kennedy 			__lpfc_nvmet_clean_io_for_cpu(phba, infop);
147766d7ce93SDick Kennedy 		}
147866d7ce93SDick Kennedy 	}
147966d7ce93SDick Kennedy 	kfree(phba->sli4_hba.nvmet_ctx_info);
148066d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = NULL;
14816c621a22SJames Smart }
14826c621a22SJames Smart 
148339aa23f9SColin Ian King static int
lpfc_nvmet_setup_io_context(struct lpfc_hba * phba)14846c621a22SJames Smart lpfc_nvmet_setup_io_context(struct lpfc_hba *phba)
14856c621a22SJames Smart {
14866c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
14876c621a22SJames Smart 	struct lpfc_iocbq *nvmewqe;
14886c621a22SJames Smart 	union lpfc_wqe128 *wqe;
148966d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *last_infop;
149066d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *infop;
1491222e9239SJames Smart 	int i, j, idx, cpu;
14926c621a22SJames Smart 
14936c621a22SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
14946c621a22SJames Smart 			"6403 Allocate NVMET resources for %d XRIs\n",
14956c621a22SJames Smart 			phba->sli4_hba.nvmet_xri_cnt);
14966c621a22SJames Smart 
149766d7ce93SDick Kennedy 	phba->sli4_hba.nvmet_ctx_info = kcalloc(
1498222e9239SJames Smart 		phba->sli4_hba.num_possible_cpu * phba->cfg_nvmet_mrq,
149966d7ce93SDick Kennedy 		sizeof(struct lpfc_nvmet_ctx_info), GFP_KERNEL);
150066d7ce93SDick Kennedy 	if (!phba->sli4_hba.nvmet_ctx_info) {
1501372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
150266d7ce93SDick Kennedy 				"6419 Failed allocate memory for "
150366d7ce93SDick Kennedy 				"nvmet context lists\n");
150466d7ce93SDick Kennedy 		return -ENOMEM;
150566d7ce93SDick Kennedy 	}
150666d7ce93SDick Kennedy 
150766d7ce93SDick Kennedy 	/*
150866d7ce93SDick Kennedy 	 * Assuming X CPUs in the system, and Y MRQs, allocate some
150966d7ce93SDick Kennedy 	 * lpfc_nvmet_ctx_info structures as follows:
151066d7ce93SDick Kennedy 	 *
151166d7ce93SDick Kennedy 	 * cpu0/mrq0 cpu1/mrq0 ... cpuX/mrq0
151266d7ce93SDick Kennedy 	 * cpu0/mrq1 cpu1/mrq1 ... cpuX/mrq1
151366d7ce93SDick Kennedy 	 * ...
151466d7ce93SDick Kennedy 	 * cpuX/mrqY cpuX/mrqY ... cpuX/mrqY
151566d7ce93SDick Kennedy 	 *
151666d7ce93SDick Kennedy 	 * Each line represents a MRQ "silo" containing an entry for
151766d7ce93SDick Kennedy 	 * every CPU.
151866d7ce93SDick Kennedy 	 *
151966d7ce93SDick Kennedy 	 * MRQ X is initially assumed to be associated with CPU X, thus
152066d7ce93SDick Kennedy 	 * contexts are initially distributed across all MRQs using
152166d7ce93SDick Kennedy 	 * the MRQ index (N) as follows cpuN/mrqN. When contexts are
152266d7ce93SDick Kennedy 	 * freed, the are freed to the MRQ silo based on the CPU number
152366d7ce93SDick Kennedy 	 * of the IO completion. Thus a context that was allocated for MRQ A
152466d7ce93SDick Kennedy 	 * whose IO completed on CPU B will be freed to cpuB/mrqA.
152566d7ce93SDick Kennedy 	 */
1526222e9239SJames Smart 	for_each_possible_cpu(i) {
152766d7ce93SDick Kennedy 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1528222e9239SJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
152966d7ce93SDick Kennedy 			INIT_LIST_HEAD(&infop->nvmet_ctx_list);
153066d7ce93SDick Kennedy 			spin_lock_init(&infop->nvmet_ctx_list_lock);
153166d7ce93SDick Kennedy 			infop->nvmet_ctx_list_cnt = 0;
153266d7ce93SDick Kennedy 		}
153366d7ce93SDick Kennedy 	}
153466d7ce93SDick Kennedy 
153566d7ce93SDick Kennedy 	/*
153666d7ce93SDick Kennedy 	 * Setup the next CPU context info ptr for each MRQ.
153766d7ce93SDick Kennedy 	 * MRQ 0 will cycle thru CPUs 0 - X separately from
153866d7ce93SDick Kennedy 	 * MRQ 1 cycling thru CPUs 0 - X, and so on.
153966d7ce93SDick Kennedy 	 */
154066d7ce93SDick Kennedy 	for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1541222e9239SJames Smart 		last_infop = lpfc_get_ctx_list(phba,
1542222e9239SJames Smart 					       cpumask_first(cpu_present_mask),
1543222e9239SJames Smart 					       j);
1544222e9239SJames Smart 		for (i = phba->sli4_hba.num_possible_cpu - 1;  i >= 0; i--) {
154566d7ce93SDick Kennedy 			infop = lpfc_get_ctx_list(phba, i, j);
154666d7ce93SDick Kennedy 			infop->nvmet_ctx_next_cpu = last_infop;
154766d7ce93SDick Kennedy 			last_infop = infop;
154866d7ce93SDick Kennedy 		}
154966d7ce93SDick Kennedy 	}
155066d7ce93SDick Kennedy 
15516c621a22SJames Smart 	/* For all nvmet xris, allocate resources needed to process a
15526c621a22SJames Smart 	 * received command on a per xri basis.
15536c621a22SJames Smart 	 */
155466d7ce93SDick Kennedy 	idx = 0;
1555222e9239SJames Smart 	cpu = cpumask_first(cpu_present_mask);
15566c621a22SJames Smart 	for (i = 0; i < phba->sli4_hba.nvmet_xri_cnt; i++) {
15576c621a22SJames Smart 		ctx_buf = kzalloc(sizeof(*ctx_buf), GFP_KERNEL);
15586c621a22SJames Smart 		if (!ctx_buf) {
1559372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15606c621a22SJames Smart 					"6404 Ran out of memory for NVMET\n");
15616c621a22SJames Smart 			return -ENOMEM;
15626c621a22SJames Smart 		}
15636c621a22SJames Smart 
15646c621a22SJames Smart 		ctx_buf->context = kzalloc(sizeof(*ctx_buf->context),
15656c621a22SJames Smart 					   GFP_KERNEL);
15666c621a22SJames Smart 		if (!ctx_buf->context) {
15676c621a22SJames Smart 			kfree(ctx_buf);
1568372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15696c621a22SJames Smart 					"6405 Ran out of NVMET "
15706c621a22SJames Smart 					"context memory\n");
15716c621a22SJames Smart 			return -ENOMEM;
15726c621a22SJames Smart 		}
15736c621a22SJames Smart 		ctx_buf->context->ctxbuf = ctx_buf;
15747b7f551bSJames Smart 		ctx_buf->context->state = LPFC_NVME_STE_FREE;
15756c621a22SJames Smart 
15766c621a22SJames Smart 		ctx_buf->iocbq = lpfc_sli_get_iocbq(phba);
15776c621a22SJames Smart 		if (!ctx_buf->iocbq) {
15786c621a22SJames Smart 			kfree(ctx_buf->context);
15796c621a22SJames Smart 			kfree(ctx_buf);
1580372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
15816c621a22SJames Smart 					"6406 Ran out of NVMET iocb/WQEs\n");
15826c621a22SJames Smart 			return -ENOMEM;
15836c621a22SJames Smart 		}
1584a680a929SJames Smart 		ctx_buf->iocbq->cmd_flag = LPFC_IO_NVMET;
15856c621a22SJames Smart 		nvmewqe = ctx_buf->iocbq;
1586205e8240SJames Smart 		wqe = &nvmewqe->wqe;
1587bd3061baSJames Smart 
15886c621a22SJames Smart 		/* Initialize WQE */
158976edb986SMuhammad Usama Anjum 		memset(wqe, 0, sizeof(*wqe));
15906c621a22SJames Smart 
1591d51cf5bdSJames Smart 		ctx_buf->iocbq->cmd_dmabuf = NULL;
15926c621a22SJames Smart 		spin_lock(&phba->sli4_hba.sgl_list_lock);
15936c621a22SJames Smart 		ctx_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, ctx_buf->iocbq);
15946c621a22SJames Smart 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
15956c621a22SJames Smart 		if (!ctx_buf->sglq) {
15966c621a22SJames Smart 			lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
15976c621a22SJames Smart 			kfree(ctx_buf->context);
15986c621a22SJames Smart 			kfree(ctx_buf);
1599372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
16006c621a22SJames Smart 					"6407 Ran out of NVMET XRIs\n");
16016c621a22SJames Smart 			return -ENOMEM;
16026c621a22SJames Smart 		}
1603472e146dSJames Smart 		INIT_WORK(&ctx_buf->defer_work, lpfc_nvmet_fcp_rqst_defer_work);
160466d7ce93SDick Kennedy 
160566d7ce93SDick Kennedy 		/*
160666d7ce93SDick Kennedy 		 * Add ctx to MRQidx context list. Our initial assumption
160766d7ce93SDick Kennedy 		 * is MRQidx will be associated with CPUidx. This association
160866d7ce93SDick Kennedy 		 * can change on the fly.
160966d7ce93SDick Kennedy 		 */
1610222e9239SJames Smart 		infop = lpfc_get_ctx_list(phba, cpu, idx);
161166d7ce93SDick Kennedy 		spin_lock(&infop->nvmet_ctx_list_lock);
161266d7ce93SDick Kennedy 		list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
161366d7ce93SDick Kennedy 		infop->nvmet_ctx_list_cnt++;
161466d7ce93SDick Kennedy 		spin_unlock(&infop->nvmet_ctx_list_lock);
161566d7ce93SDick Kennedy 
161666d7ce93SDick Kennedy 		/* Spread ctx structures evenly across all MRQs */
161766d7ce93SDick Kennedy 		idx++;
1618222e9239SJames Smart 		if (idx >= phba->cfg_nvmet_mrq) {
161966d7ce93SDick Kennedy 			idx = 0;
1620222e9239SJames Smart 			cpu = cpumask_first(cpu_present_mask);
1621222e9239SJames Smart 			continue;
1622222e9239SJames Smart 		}
1623d668b368SJustin Tee 		cpu = lpfc_next_present_cpu(cpu);
16246c621a22SJames Smart 	}
162566d7ce93SDick Kennedy 
1626222e9239SJames Smart 	for_each_present_cpu(i) {
16272879265fSJames Smart 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
16282879265fSJames Smart 			infop = lpfc_get_ctx_list(phba, i, j);
162966d7ce93SDick Kennedy 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
163066d7ce93SDick Kennedy 					"6408 TOTAL NVMET ctx for CPU %d "
163132350664SJames Smart 					"MRQ %d: cnt %d nextcpu x%px\n",
163266d7ce93SDick Kennedy 					i, j, infop->nvmet_ctx_list_cnt,
163366d7ce93SDick Kennedy 					infop->nvmet_ctx_next_cpu);
163466d7ce93SDick Kennedy 		}
163566d7ce93SDick Kennedy 	}
16366c621a22SJames Smart 	return 0;
16376c621a22SJames Smart }
16386c621a22SJames Smart 
1639d613b6a7SJames Smart int
lpfc_nvmet_create_targetport(struct lpfc_hba * phba)1640d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1641d613b6a7SJames Smart {
1642d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1643d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1644d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
16456c621a22SJames Smart 	int error;
1646d613b6a7SJames Smart 
1647d613b6a7SJames Smart 	if (phba->targetport)
1648d613b6a7SJames Smart 		return 0;
1649d613b6a7SJames Smart 
16506c621a22SJames Smart 	error = lpfc_nvmet_setup_io_context(phba);
16516c621a22SJames Smart 	if (error)
16526c621a22SJames Smart 		return error;
16536c621a22SJames Smart 
1654d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1655d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1656d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1657d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
1658d613b6a7SJames Smart 
16595b9e70b2SJames Smart 	/* We need to tell the transport layer + 1 because it takes page
16605b9e70b2SJames Smart 	 * alignment into account. When space for the SGL is allocated we
16615b9e70b2SJames Smart 	 * allocate + 3, one for cmd, one for rsp and one for this alignment
16624d4c4a4aSJames Smart 	 */
16634d4c4a4aSJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1664cdb42becSJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_hdw_queue;
1665f485c18dSDick Kennedy 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP;
1666d613b6a7SJames Smart 
16677d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1668d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1669d613b6a7SJames Smart 					     &phba->pcidev->dev,
1670d613b6a7SJames Smart 					     &phba->targetport);
1671166d7211SJames Smart #else
16726c621a22SJames Smart 	error = -ENOENT;
1673166d7211SJames Smart #endif
1674d613b6a7SJames Smart 	if (error) {
1675372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1676e7981a2cSDick Kennedy 				"6025 Cannot register NVME targetport x%x: "
1677e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1678e7981a2cSDick Kennedy 				error,
1679e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1680e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1681e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1682d613b6a7SJames Smart 		phba->targetport = NULL;
1683e7981a2cSDick Kennedy 		phba->nvmet_support = 0;
16846c621a22SJames Smart 
16856c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
16866c621a22SJames Smart 
1687d613b6a7SJames Smart 	} else {
1688d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
1689d613b6a7SJames Smart 			phba->targetport->private;
1690d613b6a7SJames Smart 		tgtp->phba = phba;
1691d613b6a7SJames Smart 
1692d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1693d613b6a7SJames Smart 				"6026 Registered NVME "
169432350664SJames Smart 				"targetport: x%px, private x%px "
1695e7981a2cSDick Kennedy 				"portnm %llx nodenm %llx segs %d qs %d\n",
1696d613b6a7SJames Smart 				phba->targetport, tgtp,
1697e7981a2cSDick Kennedy 				pinfo.port_name, pinfo.node_name,
1698e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_sgl_segments,
1699e7981a2cSDick Kennedy 				lpfc_tgttemplate.max_hw_queues);
1700d613b6a7SJames Smart 
1701d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
1702d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
1703d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
1704d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
1705547077a4SJames Smart 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1706d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
1707d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
1708d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
17094b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_xb_set, 0);
17104b056682SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_aborted, 0);
1711d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1712d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1713d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1714d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1715d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
1716d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1717d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
1718d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
1719d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
1720547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_release, 0);
1721d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1722d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
17234b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_xb_set, 0);
17244b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_aborted, 0);
1725d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
17264b056682SJames Smart 		atomic_set(&tgtp->xmt_fcp_xri_abort_cqe, 0);
1727547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
1728547077a4SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1729547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_unsol, 0);
1730547077a4SJames Smart 		atomic_set(&tgtp->xmt_abort_sol, 0);
1731d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
1732d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1733411de511SJames Smart 		atomic_set(&tgtp->defer_ctx, 0);
1734411de511SJames Smart 		atomic_set(&tgtp->defer_fod, 0);
1735411de511SJames Smart 		atomic_set(&tgtp->defer_wqfull, 0);
1736d613b6a7SJames Smart 	}
1737d613b6a7SJames Smart 	return error;
1738d613b6a7SJames Smart }
1739d613b6a7SJames Smart 
1740d613b6a7SJames Smart int
lpfc_nvmet_update_targetport(struct lpfc_hba * phba)1741d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1742d613b6a7SJames Smart {
1743d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
1744d613b6a7SJames Smart 
1745d613b6a7SJames Smart 	if (!phba->targetport)
1746d613b6a7SJames Smart 		return 0;
1747d613b6a7SJames Smart 
1748d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
174932350664SJames Smart 			 "6007 Update NVMET port x%px did x%x\n",
1750d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
1751d613b6a7SJames Smart 
1752d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
1753d613b6a7SJames Smart 	return 0;
1754d613b6a7SJames Smart }
1755d613b6a7SJames Smart 
1756318083adSJames Smart /**
1757318083adSJames Smart  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1758318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
1759318083adSJames Smart  * @axri: pointer to the nvmet xri abort wcqe structure.
1760318083adSJames Smart  *
1761318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
1762318083adSJames Smart  * NVMET aborted xri.
1763318083adSJames Smart  **/
1764318083adSJames Smart void
lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba * phba,struct sli4_wcqe_xri_aborted * axri)1765318083adSJames Smart lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
1766318083adSJames Smart 			    struct sli4_wcqe_xri_aborted *axri)
1767318083adSJames Smart {
176832b93865SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
176986c67379SJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
177086c67379SJames Smart 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
17717cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp, *next_ctxp;
17724b056682SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
177379d8c4ceSJames Smart 	struct nvmefc_tgt_fcp_req *req = NULL;
177486c67379SJames Smart 	struct lpfc_nodelist *ndlp;
177586c67379SJames Smart 	unsigned long iflag = 0;
177686c67379SJames Smart 	int rrq_empty = 0;
177786c67379SJames Smart 	bool released = false;
177886c67379SJames Smart 
177986c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
178086c67379SJames Smart 			"6317 XB aborted xri x%x rxid x%x\n", xri, rxid);
178186c67379SJames Smart 
178286c67379SJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
178386c67379SJames Smart 		return;
17844b056682SJames Smart 
17854b056682SJames Smart 	if (phba->targetport) {
17864b056682SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
17874b056682SJames Smart 		atomic_inc(&tgtp->xmt_fcp_xri_abort_cqe);
17884b056682SJames Smart 	}
17894b056682SJames Smart 
1790a789241eSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
179186c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
179286c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
179386c67379SJames Smart 				 list) {
17946c621a22SJames Smart 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
179586c67379SJames Smart 			continue;
179686c67379SJames Smart 
17977740b615SJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock,
17987740b615SJames Smart 				       iflag);
17997740b615SJames Smart 
18007740b615SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
180186c67379SJames Smart 		/* Check if we already received a free context call
180286c67379SJames Smart 		 * and we have completed processing an abort situation.
180386c67379SJames Smart 		 */
18047b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_CTX_RLS &&
18057b7f551bSJames Smart 		    !(ctxp->flag & LPFC_NVME_ABORT_OP)) {
18067740b615SJames Smart 			spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
180779d8c4ceSJames Smart 			list_del_init(&ctxp->list);
18087740b615SJames Smart 			spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
180986c67379SJames Smart 			released = true;
181086c67379SJames Smart 		}
18117b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_XBUSY;
18127740b615SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
181386c67379SJames Smart 
181486c67379SJames Smart 		rrq_empty = list_empty(&phba->active_rrq_list);
181586c67379SJames Smart 		ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1816307e3380SJames Smart 		if (ndlp &&
181786c67379SJames Smart 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
181886c67379SJames Smart 		     ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
181986c67379SJames Smart 			lpfc_set_rrq_active(phba, ndlp,
18206c621a22SJames Smart 				ctxp->ctxbuf->sglq->sli4_lxritag,
182186c67379SJames Smart 				rxid, 1);
182286c67379SJames Smart 			lpfc_sli4_abts_err_handler(phba, ndlp, axri);
182386c67379SJames Smart 		}
182486c67379SJames Smart 
182586c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
182679d8c4ceSJames Smart 				"6318 XB aborted oxid x%x flg x%x (%x)\n",
182786c67379SJames Smart 				ctxp->oxid, ctxp->flag, released);
182886c67379SJames Smart 		if (released)
18296c621a22SJames Smart 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
18306c621a22SJames Smart 
183186c67379SJames Smart 		if (rrq_empty)
183286c67379SJames Smart 			lpfc_worker_wake_up(phba);
183386c67379SJames Smart 		return;
183486c67379SJames Smart 	}
1835a789241eSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
183679d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_xri(phba, xri);
183779d8c4ceSJames Smart 	if (ctxp) {
183879d8c4ceSJames Smart 		/*
183979d8c4ceSJames Smart 		 *  Abort already done by FW, so BA_ACC sent.
184079d8c4ceSJames Smart 		 *  However, the transport may be unaware.
184179d8c4ceSJames Smart 		 */
184279d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
184379d8c4ceSJames Smart 				"6323 NVMET Rcv ABTS xri x%x ctxp state x%x "
184479d8c4ceSJames Smart 				"flag x%x oxid x%x rxid x%x\n",
184579d8c4ceSJames Smart 				xri, ctxp->state, ctxp->flag, ctxp->oxid,
184679d8c4ceSJames Smart 				rxid);
184779d8c4ceSJames Smart 
184879d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
18497b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_ABTS_RCV;
18507b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_ABORT;
185179d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
185279d8c4ceSJames Smart 
185379d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
185479d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
185501d53c04SJames Smart 				 xri, raw_smp_processor_id(), 0);
185679d8c4ceSJames Smart 
18577cacae2aSJames Smart 		req = &ctxp->hdlrctx.fcp_req;
185879d8c4ceSJames Smart 		if (req)
185979d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport, req);
186079d8c4ceSJames Smart 	}
186132b93865SJames Smart #endif
186286c67379SJames Smart }
186386c67379SJames Smart 
186486c67379SJames Smart int
lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport * vport,struct fc_frame_header * fc_hdr)186586c67379SJames Smart lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport *vport,
186686c67379SJames Smart 			   struct fc_frame_header *fc_hdr)
186786c67379SJames Smart {
186886c67379SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
186986c67379SJames Smart 	struct lpfc_hba *phba = vport->phba;
18707cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp, *next_ctxp;
187186c67379SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
187232b93865SJames Smart 	uint32_t sid;
187379d8c4ceSJames Smart 	uint16_t oxid, xri;
187486c67379SJames Smart 	unsigned long iflag = 0;
187586c67379SJames Smart 
187632b93865SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
187779d8c4ceSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
187886c67379SJames Smart 
1879a789241eSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
188086c67379SJames Smart 	list_for_each_entry_safe(ctxp, next_ctxp,
188186c67379SJames Smart 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
188286c67379SJames Smart 				 list) {
188379d8c4ceSJames Smart 		if (ctxp->oxid != oxid || ctxp->sid != sid)
188486c67379SJames Smart 			continue;
188586c67379SJames Smart 
188632b93865SJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
188732b93865SJames Smart 
1888a789241eSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock,
1889a789241eSJames Smart 				       iflag);
189086c67379SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
18917b7f551bSJames Smart 		ctxp->flag |= LPFC_NVME_ABTS_RCV;
189286c67379SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
189386c67379SJames Smart 
189486c67379SJames Smart 		lpfc_nvmeio_data(phba,
189586c67379SJames Smart 			"NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1896d6d189ceSBart Van Assche 			xri, raw_smp_processor_id(), 0);
189786c67379SJames Smart 
189886c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
189986c67379SJames Smart 				"6319 NVMET Rcv ABTS:acc xri x%x\n", xri);
190086c67379SJames Smart 
19017cacae2aSJames Smart 		rsp = &ctxp->hdlrctx.fcp_req;
190286c67379SJames Smart 		nvmet_fc_rcv_fcp_abort(phba->targetport, rsp);
190386c67379SJames Smart 
190486c67379SJames Smart 		/* Respond with BA_ACC accordingly */
190586c67379SJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
190686c67379SJames Smart 		return 0;
190786c67379SJames Smart 	}
1908a789241eSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.abts_nvmet_buf_list_lock, iflag);
190979d8c4ceSJames Smart 	/* check the wait list */
191079d8c4ceSJames Smart 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
191179d8c4ceSJames Smart 		struct rqb_dmabuf *nvmebuf;
191279d8c4ceSJames Smart 		struct fc_frame_header *fc_hdr_tmp;
191379d8c4ceSJames Smart 		u32 sid_tmp;
191479d8c4ceSJames Smart 		u16 oxid_tmp;
191579d8c4ceSJames Smart 		bool found = false;
191679d8c4ceSJames Smart 
191779d8c4ceSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
191879d8c4ceSJames Smart 
191979d8c4ceSJames Smart 		/* match by oxid and s_id */
192079d8c4ceSJames Smart 		list_for_each_entry(nvmebuf,
192179d8c4ceSJames Smart 				    &phba->sli4_hba.lpfc_nvmet_io_wait_list,
192279d8c4ceSJames Smart 				    hbuf.list) {
192379d8c4ceSJames Smart 			fc_hdr_tmp = (struct fc_frame_header *)
192479d8c4ceSJames Smart 					(nvmebuf->hbuf.virt);
192579d8c4ceSJames Smart 			oxid_tmp = be16_to_cpu(fc_hdr_tmp->fh_ox_id);
192679d8c4ceSJames Smart 			sid_tmp = sli4_sid_from_fc_hdr(fc_hdr_tmp);
192779d8c4ceSJames Smart 			if (oxid_tmp != oxid || sid_tmp != sid)
192879d8c4ceSJames Smart 				continue;
192986c67379SJames Smart 
193086c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
193179d8c4ceSJames Smart 					"6321 NVMET Rcv ABTS oxid x%x from x%x "
193279d8c4ceSJames Smart 					"is waiting for a ctxp\n",
193379d8c4ceSJames Smart 					oxid, sid);
193479d8c4ceSJames Smart 
193579d8c4ceSJames Smart 			list_del_init(&nvmebuf->hbuf.list);
193679d8c4ceSJames Smart 			phba->sli4_hba.nvmet_io_wait_cnt--;
193779d8c4ceSJames Smart 			found = true;
193879d8c4ceSJames Smart 			break;
193979d8c4ceSJames Smart 		}
194079d8c4ceSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
194179d8c4ceSJames Smart 				       iflag);
194279d8c4ceSJames Smart 
194379d8c4ceSJames Smart 		/* free buffer since already posted a new DMA buffer to RQ */
194479d8c4ceSJames Smart 		if (found) {
194579d8c4ceSJames Smart 			nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
194679d8c4ceSJames Smart 			/* Respond with BA_ACC accordingly */
194779d8c4ceSJames Smart 			lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
194879d8c4ceSJames Smart 			return 0;
194979d8c4ceSJames Smart 		}
195079d8c4ceSJames Smart 	}
195179d8c4ceSJames Smart 
195279d8c4ceSJames Smart 	/* check active list */
195379d8c4ceSJames Smart 	ctxp = lpfc_nvmet_get_ctx_for_oxid(phba, oxid, sid);
195479d8c4ceSJames Smart 	if (ctxp) {
195579d8c4ceSJames Smart 		xri = ctxp->ctxbuf->sglq->sli4_xritag;
195679d8c4ceSJames Smart 
195779d8c4ceSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
19587b7f551bSJames Smart 		ctxp->flag |= (LPFC_NVME_ABTS_RCV | LPFC_NVME_ABORT_OP);
195979d8c4ceSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
196079d8c4ceSJames Smart 
196179d8c4ceSJames Smart 		lpfc_nvmeio_data(phba,
196279d8c4ceSJames Smart 				 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
196379d8c4ceSJames Smart 				 xri, raw_smp_processor_id(), 0);
196479d8c4ceSJames Smart 
196579d8c4ceSJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
196679d8c4ceSJames Smart 				"6322 NVMET Rcv ABTS:acc oxid x%x xri x%x "
196779d8c4ceSJames Smart 				"flag x%x state x%x\n",
196879d8c4ceSJames Smart 				ctxp->oxid, xri, ctxp->flag, ctxp->state);
196979d8c4ceSJames Smart 
19707b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_TNOTIFY) {
197179d8c4ceSJames Smart 			/* Notify the transport */
197279d8c4ceSJames Smart 			nvmet_fc_rcv_fcp_abort(phba->targetport,
19737cacae2aSJames Smart 					       &ctxp->hdlrctx.fcp_req);
197479d8c4ceSJames Smart 		} else {
19756594d31bSJames Smart 			cancel_work_sync(&ctxp->ctxbuf->defer_work);
197679d8c4ceSJames Smart 			spin_lock_irqsave(&ctxp->ctxlock, iflag);
197779d8c4ceSJames Smart 			lpfc_nvmet_defer_release(phba, ctxp);
197879d8c4ceSJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
197979d8c4ceSJames Smart 		}
198079d8c4ceSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
198179d8c4ceSJames Smart 					       ctxp->oxid);
198279d8c4ceSJames Smart 
19836594d31bSJames Smart 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
198479d8c4ceSJames Smart 		return 0;
198579d8c4ceSJames Smart 	}
198679d8c4ceSJames Smart 
198779d8c4ceSJames Smart 	lpfc_nvmeio_data(phba, "NVMET ABTS RCV: oxid x%x CPU %02x rjt %d\n",
198879d8c4ceSJames Smart 			 oxid, raw_smp_processor_id(), 1);
198979d8c4ceSJames Smart 
199079d8c4ceSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
199179d8c4ceSJames Smart 			"6320 NVMET Rcv ABTS:rjt oxid x%x\n", oxid);
199286c67379SJames Smart 
199386c67379SJames Smart 	/* Respond with BA_RJT accordingly */
199486c67379SJames Smart 	lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 0);
199586c67379SJames Smart #endif
1996b06e13c3SJens Axboe 	return 0;
1997318083adSJames Smart }
1998318083adSJames Smart 
19996e8e1c14SJames Smart static void
lpfc_nvmet_wqfull_flush(struct lpfc_hba * phba,struct lpfc_queue * wq,struct lpfc_async_xchg_ctx * ctxp)20006e8e1c14SJames Smart lpfc_nvmet_wqfull_flush(struct lpfc_hba *phba, struct lpfc_queue *wq,
20017cacae2aSJames Smart 			struct lpfc_async_xchg_ctx *ctxp)
20026e8e1c14SJames Smart {
20036e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
20046e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
20056e8e1c14SJames Smart 	struct lpfc_iocbq *next_nvmewqeq;
20066e8e1c14SJames Smart 	unsigned long iflags;
20076e8e1c14SJames Smart 	struct lpfc_wcqe_complete wcqe;
20086e8e1c14SJames Smart 	struct lpfc_wcqe_complete *wcqep;
20096e8e1c14SJames Smart 
20106e8e1c14SJames Smart 	pring = wq->pring;
20116e8e1c14SJames Smart 	wcqep = &wcqe;
20126e8e1c14SJames Smart 
20136e8e1c14SJames Smart 	/* Fake an ABORT error code back to cmpl routine */
20146e8e1c14SJames Smart 	memset(wcqep, 0, sizeof(struct lpfc_wcqe_complete));
20156e8e1c14SJames Smart 	bf_set(lpfc_wcqe_c_status, wcqep, IOSTAT_LOCAL_REJECT);
20166e8e1c14SJames Smart 	wcqep->parameter = IOERR_ABORT_REQUESTED;
20176e8e1c14SJames Smart 
20186e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
20196e8e1c14SJames Smart 	list_for_each_entry_safe(nvmewqeq, next_nvmewqeq,
20206e8e1c14SJames Smart 				 &wq->wqfull_list, list) {
20216e8e1c14SJames Smart 		if (ctxp) {
20226e8e1c14SJames Smart 			/* Checking for a specific IO to flush */
2023d51cf5bdSJames Smart 			if (nvmewqeq->context_un.axchg == ctxp) {
20246e8e1c14SJames Smart 				list_del(&nvmewqeq->list);
20256e8e1c14SJames Smart 				spin_unlock_irqrestore(&pring->ring_lock,
20266e8e1c14SJames Smart 						       iflags);
2027a680a929SJames Smart 				memcpy(&nvmewqeq->wcqe_cmpl, wcqep,
2028a680a929SJames Smart 				       sizeof(*wcqep));
20296e8e1c14SJames Smart 				lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq,
2030a680a929SJames Smart 							  nvmewqeq);
20316e8e1c14SJames Smart 				return;
20326e8e1c14SJames Smart 			}
20336e8e1c14SJames Smart 			continue;
20346e8e1c14SJames Smart 		} else {
20356e8e1c14SJames Smart 			/* Flush all IOs */
20366e8e1c14SJames Smart 			list_del(&nvmewqeq->list);
20376e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
2038a680a929SJames Smart 			memcpy(&nvmewqeq->wcqe_cmpl, wcqep, sizeof(*wcqep));
2039a680a929SJames Smart 			lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq, nvmewqeq);
20406e8e1c14SJames Smart 			spin_lock_irqsave(&pring->ring_lock, iflags);
20416e8e1c14SJames Smart 		}
20426e8e1c14SJames Smart 	}
20436e8e1c14SJames Smart 	if (!ctxp)
20446e8e1c14SJames Smart 		wq->q_flag &= ~HBA_NVMET_WQFULL;
20456e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
20466e8e1c14SJames Smart }
20476e8e1c14SJames Smart 
20486e8e1c14SJames Smart void
lpfc_nvmet_wqfull_process(struct lpfc_hba * phba,struct lpfc_queue * wq)20496e8e1c14SJames Smart lpfc_nvmet_wqfull_process(struct lpfc_hba *phba,
20506e8e1c14SJames Smart 			  struct lpfc_queue *wq)
20516e8e1c14SJames Smart {
20526e8e1c14SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
20536e8e1c14SJames Smart 	struct lpfc_sli_ring *pring;
20546e8e1c14SJames Smart 	struct lpfc_iocbq *nvmewqeq;
20557cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
20566e8e1c14SJames Smart 	unsigned long iflags;
20576e8e1c14SJames Smart 	int rc;
20586e8e1c14SJames Smart 
20596e8e1c14SJames Smart 	/*
20606e8e1c14SJames Smart 	 * Some WQE slots are available, so try to re-issue anything
20616e8e1c14SJames Smart 	 * on the WQ wqfull_list.
20626e8e1c14SJames Smart 	 */
20636e8e1c14SJames Smart 	pring = wq->pring;
20646e8e1c14SJames Smart 	spin_lock_irqsave(&pring->ring_lock, iflags);
20656e8e1c14SJames Smart 	while (!list_empty(&wq->wqfull_list)) {
20666e8e1c14SJames Smart 		list_remove_head(&wq->wqfull_list, nvmewqeq, struct lpfc_iocbq,
20676e8e1c14SJames Smart 				 list);
20686e8e1c14SJames Smart 		spin_unlock_irqrestore(&pring->ring_lock, iflags);
2069d51cf5bdSJames Smart 		ctxp = nvmewqeq->context_un.axchg;
20701fbf9742SJames Smart 		rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
20716e8e1c14SJames Smart 		spin_lock_irqsave(&pring->ring_lock, iflags);
20726e8e1c14SJames Smart 		if (rc == -EBUSY) {
20736e8e1c14SJames Smart 			/* WQ was full again, so put it back on the list */
20746e8e1c14SJames Smart 			list_add(&nvmewqeq->list, &wq->wqfull_list);
20756e8e1c14SJames Smart 			spin_unlock_irqrestore(&pring->ring_lock, iflags);
20766e8e1c14SJames Smart 			return;
20776e8e1c14SJames Smart 		}
207879d8c4ceSJames Smart 		if (rc == WQE_SUCCESS) {
207979d8c4ceSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
208079d8c4ceSJames Smart 			if (ctxp->ts_cmd_nvme) {
20817cacae2aSJames Smart 				if (ctxp->hdlrctx.fcp_req.op == NVMET_FCOP_RSP)
208279d8c4ceSJames Smart 					ctxp->ts_status_wqput = ktime_get_ns();
208379d8c4ceSJames Smart 				else
208479d8c4ceSJames Smart 					ctxp->ts_data_wqput = ktime_get_ns();
208579d8c4ceSJames Smart 			}
208679d8c4ceSJames Smart #endif
208779d8c4ceSJames Smart 		} else {
208879d8c4ceSJames Smart 			WARN_ON(rc);
208979d8c4ceSJames Smart 		}
20906e8e1c14SJames Smart 	}
20916e8e1c14SJames Smart 	wq->q_flag &= ~HBA_NVMET_WQFULL;
20926e8e1c14SJames Smart 	spin_unlock_irqrestore(&pring->ring_lock, iflags);
20936e8e1c14SJames Smart 
20946e8e1c14SJames Smart #endif
20956e8e1c14SJames Smart }
20966e8e1c14SJames Smart 
2097d613b6a7SJames Smart void
lpfc_nvmet_destroy_targetport(struct lpfc_hba * phba)2098d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
2099d613b6a7SJames Smart {
21007d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2101d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
21026e8e1c14SJames Smart 	struct lpfc_queue *wq;
21036e8e1c14SJames Smart 	uint32_t qidx;
2104c41f5988SEwan D. Milne 	DECLARE_COMPLETION_ONSTACK(tport_unreg_cmp);
2105d613b6a7SJames Smart 
2106d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
2107d613b6a7SJames Smart 		return;
2108d613b6a7SJames Smart 	if (phba->targetport) {
2109d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2110cdb42becSJames Smart 		for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
2111c00f62e6SJames Smart 			wq = phba->sli4_hba.hdwq[qidx].io_wq;
21126e8e1c14SJames Smart 			lpfc_nvmet_wqfull_flush(phba, wq, NULL);
21136e8e1c14SJames Smart 		}
2114c41f5988SEwan D. Milne 		tgtp->tport_unreg_cmp = &tport_unreg_cmp;
2115d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
2116af6de8c6SEwan D. Milne 		if (!wait_for_completion_timeout(&tport_unreg_cmp,
21172a0fb340SJames Smart 					msecs_to_jiffies(LPFC_NVMET_WAIT_TMO)))
2118372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
211932350664SJames Smart 					"6179 Unreg targetport x%px timeout "
21202a0fb340SJames Smart 					"reached.\n", phba->targetport);
21216c621a22SJames Smart 		lpfc_nvmet_cleanup_io_context(phba);
2122d613b6a7SJames Smart 	}
2123d613b6a7SJames Smart 	phba->targetport = NULL;
2124166d7211SJames Smart #endif
2125d613b6a7SJames Smart }
2126d613b6a7SJames Smart 
2127d613b6a7SJames Smart /**
21283a8070c5SJames Smart  * lpfc_nvmet_handle_lsreq - Process an NVME LS request
2129d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
21303a8070c5SJames Smart  * @axchg: pointer to exchange context for the NVME LS request
2131d613b6a7SJames Smart  *
21323a8070c5SJames Smart  * This routine is used for processing an asychronously received NVME LS
21333a8070c5SJames Smart  * request. Any remaining validation is done and the LS is then forwarded
21343a8070c5SJames Smart  * to the nvmet-fc transport via nvmet_fc_rcv_ls_req().
21353a8070c5SJames Smart  *
21363a8070c5SJames Smart  * The calling sequence should be: nvmet_fc_rcv_ls_req() -> (processing)
21373a8070c5SJames Smart  * -> lpfc_nvmet_xmt_ls_rsp/cmp -> req->done.
21383a8070c5SJames Smart  * lpfc_nvme_xmt_ls_rsp_cmp should free the allocated axchg.
21393a8070c5SJames Smart  *
21403a8070c5SJames Smart  * Returns 0 if LS was handled and delivered to the transport
21413a8070c5SJames Smart  * Returns 1 if LS failed to be handled and should be dropped
21423a8070c5SJames Smart  */
21433a8070c5SJames Smart int
lpfc_nvmet_handle_lsreq(struct lpfc_hba * phba,struct lpfc_async_xchg_ctx * axchg)21443a8070c5SJames Smart lpfc_nvmet_handle_lsreq(struct lpfc_hba *phba,
21453a8070c5SJames Smart 			struct lpfc_async_xchg_ctx *axchg)
2146d613b6a7SJames Smart {
21477d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
21483a8070c5SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = phba->targetport->private;
21493a8070c5SJames Smart 	uint32_t *payload = axchg->payload;
21503a8070c5SJames Smart 	int rc;
2151d613b6a7SJames Smart 
2152d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
21533a8070c5SJames Smart 
21544c2805aaSJames Smart 	/*
21554c2805aaSJames Smart 	 * Driver passes the ndlp as the hosthandle argument allowing
21564c2805aaSJames Smart 	 * the transport to generate LS requests for any associateions
21574c2805aaSJames Smart 	 * that are created.
21584c2805aaSJames Smart 	 */
21594c2805aaSJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, axchg->ndlp, &axchg->ls_rsp,
21603a8070c5SJames Smart 				 axchg->payload, axchg->size);
2161d613b6a7SJames Smart 
2162d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2163ce1b591cSJames Smart 			"6037 NVMET Unsol rcv: sz %d rc %d: %08x %08x %08x "
21643a8070c5SJames Smart 			"%08x %08x %08x\n", axchg->size, rc,
2165d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
2166d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
21672b65e182SJames Smart 
21683a8070c5SJames Smart 	if (!rc) {
2169d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
21703a8070c5SJames Smart 		return 0;
2171d613b6a7SJames Smart 	}
21722b65e182SJames Smart 
2173d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
2174166d7211SJames Smart #endif
21753a8070c5SJames Smart 	return 1;
2176d613b6a7SJames Smart }
2177d613b6a7SJames Smart 
2178472e146dSJames Smart static void
lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf * ctx_buf)2179472e146dSJames Smart lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf)
2180472e146dSJames Smart {
2181472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
21827cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp = ctx_buf->context;
2183472e146dSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
2184472e146dSJames Smart 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
2185472e146dSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
21864552e0f6SJames Smart 	uint32_t *payload, qno;
2187472e146dSJames Smart 	uint32_t rc;
2188472e146dSJames Smart 	unsigned long iflags;
2189472e146dSJames Smart 
2190472e146dSJames Smart 	if (!nvmebuf) {
2191372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2192472e146dSJames Smart 			"6159 process_rcv_fcp_req, nvmebuf is NULL, "
2193472e146dSJames Smart 			"oxid: x%x flg: x%x state: x%x\n",
2194472e146dSJames Smart 			ctxp->oxid, ctxp->flag, ctxp->state);
2195472e146dSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
2196472e146dSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2197472e146dSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2198472e146dSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
2199472e146dSJames Smart 						 ctxp->oxid);
2200472e146dSJames Smart 		return;
2201472e146dSJames Smart 	}
2202472e146dSJames Smart 
22037b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABTS_RCV) {
2204372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
220579d8c4ceSJames Smart 				"6324 IO oxid x%x aborted\n",
220679d8c4ceSJames Smart 				ctxp->oxid);
220779d8c4ceSJames Smart 		return;
220879d8c4ceSJames Smart 	}
220979d8c4ceSJames Smart 
2210472e146dSJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
2211472e146dSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
22127b7f551bSJames Smart 	ctxp->flag |= LPFC_NVME_TNOTIFY;
2213d74a89aaSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2214d74a89aaSJames Smart 	if (ctxp->ts_isr_cmd)
2215d74a89aaSJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
2216d74a89aaSJames Smart #endif
2217472e146dSJames Smart 	/*
2218472e146dSJames Smart 	 * The calling sequence should be:
2219472e146dSJames Smart 	 * nvmet_fc_rcv_fcp_req->lpfc_nvmet_xmt_fcp_op/cmp- req->done
2220472e146dSJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
2221472e146dSJames Smart 	 * When we return from nvmet_fc_rcv_fcp_req, all relevant info
2222472e146dSJames Smart 	 * the NVME command / FC header is stored.
2223472e146dSJames Smart 	 * A buffer has already been reposted for this IO, so just free
2224472e146dSJames Smart 	 * the nvmebuf.
2225472e146dSJames Smart 	 */
22267cacae2aSJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->hdlrctx.fcp_req,
2227472e146dSJames Smart 				  payload, ctxp->size);
2228472e146dSJames Smart 	/* Process FCP command */
2229472e146dSJames Smart 	if (rc == 0) {
2230472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
22314552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
22327b7f551bSJames Smart 		if ((ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) ||
22334552e0f6SJames Smart 		    (nvmebuf != ctxp->rqb_buffer)) {
22344552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22354552e0f6SJames Smart 			return;
22364552e0f6SJames Smart 		}
22374552e0f6SJames Smart 		ctxp->rqb_buffer = NULL;
22384552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22394552e0f6SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
2240472e146dSJames Smart 		return;
2241472e146dSJames Smart 	}
2242472e146dSJames Smart 
2243472e146dSJames Smart 	/* Processing of FCP command is deferred */
2244472e146dSJames Smart 	if (rc == -EOVERFLOW) {
2245472e146dSJames Smart 		lpfc_nvmeio_data(phba, "NVMET RCV BUSY: xri x%x sz %d "
2246472e146dSJames Smart 				 "from %06x\n",
2247472e146dSJames Smart 				 ctxp->oxid, ctxp->size, ctxp->sid);
2248472e146dSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
2249472e146dSJames Smart 		atomic_inc(&tgtp->defer_fod);
22504552e0f6SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflags);
22517b7f551bSJames Smart 		if (ctxp->flag & LPFC_NVME_CTX_REUSE_WQ) {
22524552e0f6SJames Smart 			spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22534552e0f6SJames Smart 			return;
22544552e0f6SJames Smart 		}
22554552e0f6SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
22564552e0f6SJames Smart 		/*
22574552e0f6SJames Smart 		 * Post a replacement DMA buffer to RQ and defer
22584552e0f6SJames Smart 		 * freeing rcv buffer till .defer_rcv callback
22594552e0f6SJames Smart 		 */
22604552e0f6SJames Smart 		qno = nvmebuf->idx;
22614552e0f6SJames Smart 		lpfc_post_rq_buffer(
22624552e0f6SJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
22634552e0f6SJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2264472e146dSJames Smart 		return;
2265472e146dSJames Smart 	}
22667b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_TNOTIFY;
2267472e146dSJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2268372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2269472e146dSJames Smart 			"2582 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
2270472e146dSJames Smart 			ctxp->oxid, rc,
2271472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_in),
2272472e146dSJames Smart 			atomic_read(&tgtp->rcv_fcp_cmd_out),
2273472e146dSJames Smart 			atomic_read(&tgtp->xmt_fcp_release));
2274472e146dSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
2275472e146dSJames Smart 			 ctxp->oxid, ctxp->size, ctxp->sid);
2276472e146dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, iflags);
2277472e146dSJames Smart 	lpfc_nvmet_defer_release(phba, ctxp);
2278472e146dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
2279472e146dSJames Smart 	lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
2280472e146dSJames Smart #endif
2281472e146dSJames Smart }
2282472e146dSJames Smart 
2283472e146dSJames Smart static void
lpfc_nvmet_fcp_rqst_defer_work(struct work_struct * work)2284472e146dSJames Smart lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *work)
2285472e146dSJames Smart {
2286472e146dSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
2287472e146dSJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf =
2288472e146dSJames Smart 		container_of(work, struct lpfc_nvmet_ctxbuf, defer_work);
2289472e146dSJames Smart 
2290472e146dSJames Smart 	lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2291472e146dSJames Smart #endif
2292472e146dSJames Smart }
2293472e146dSJames Smart 
229466d7ce93SDick Kennedy static struct lpfc_nvmet_ctxbuf *
lpfc_nvmet_replenish_context(struct lpfc_hba * phba,struct lpfc_nvmet_ctx_info * current_infop)229566d7ce93SDick Kennedy lpfc_nvmet_replenish_context(struct lpfc_hba *phba,
229666d7ce93SDick Kennedy 			     struct lpfc_nvmet_ctx_info *current_infop)
229766d7ce93SDick Kennedy {
22982299e432SDick Kennedy #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
229966d7ce93SDick Kennedy 	struct lpfc_nvmet_ctxbuf *ctx_buf = NULL;
230066d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *get_infop;
230166d7ce93SDick Kennedy 	int i;
230266d7ce93SDick Kennedy 
230366d7ce93SDick Kennedy 	/*
230466d7ce93SDick Kennedy 	 * The current_infop for the MRQ a NVME command IU was received
230566d7ce93SDick Kennedy 	 * on is empty. Our goal is to replenish this MRQs context
230666d7ce93SDick Kennedy 	 * list from a another CPUs.
230766d7ce93SDick Kennedy 	 *
230866d7ce93SDick Kennedy 	 * First we need to pick a context list to start looking on.
230966d7ce93SDick Kennedy 	 * nvmet_ctx_start_cpu has available context the last time
231066d7ce93SDick Kennedy 	 * we needed to replenish this CPU where nvmet_ctx_next_cpu
231166d7ce93SDick Kennedy 	 * is just the next sequential CPU for this MRQ.
231266d7ce93SDick Kennedy 	 */
231366d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_start_cpu)
231466d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_start_cpu;
231566d7ce93SDick Kennedy 	else
231666d7ce93SDick Kennedy 		get_infop = current_infop->nvmet_ctx_next_cpu;
231766d7ce93SDick Kennedy 
2318222e9239SJames Smart 	for (i = 0; i < phba->sli4_hba.num_possible_cpu; i++) {
231966d7ce93SDick Kennedy 		if (get_infop == current_infop) {
232066d7ce93SDick Kennedy 			get_infop = get_infop->nvmet_ctx_next_cpu;
232166d7ce93SDick Kennedy 			continue;
232266d7ce93SDick Kennedy 		}
232366d7ce93SDick Kennedy 		spin_lock(&get_infop->nvmet_ctx_list_lock);
232466d7ce93SDick Kennedy 
232566d7ce93SDick Kennedy 		/* Just take the entire context list, if there are any */
232666d7ce93SDick Kennedy 		if (get_infop->nvmet_ctx_list_cnt) {
232766d7ce93SDick Kennedy 			list_splice_init(&get_infop->nvmet_ctx_list,
232866d7ce93SDick Kennedy 				    &current_infop->nvmet_ctx_list);
232966d7ce93SDick Kennedy 			current_infop->nvmet_ctx_list_cnt =
233066d7ce93SDick Kennedy 				get_infop->nvmet_ctx_list_cnt - 1;
233166d7ce93SDick Kennedy 			get_infop->nvmet_ctx_list_cnt = 0;
233266d7ce93SDick Kennedy 			spin_unlock(&get_infop->nvmet_ctx_list_lock);
233366d7ce93SDick Kennedy 
233466d7ce93SDick Kennedy 			current_infop->nvmet_ctx_start_cpu = get_infop;
233566d7ce93SDick Kennedy 			list_remove_head(&current_infop->nvmet_ctx_list,
233666d7ce93SDick Kennedy 					 ctx_buf, struct lpfc_nvmet_ctxbuf,
233766d7ce93SDick Kennedy 					 list);
233866d7ce93SDick Kennedy 			return ctx_buf;
233966d7ce93SDick Kennedy 		}
234066d7ce93SDick Kennedy 
234166d7ce93SDick Kennedy 		/* Otherwise, move on to the next CPU for this MRQ */
234266d7ce93SDick Kennedy 		spin_unlock(&get_infop->nvmet_ctx_list_lock);
234366d7ce93SDick Kennedy 		get_infop = get_infop->nvmet_ctx_next_cpu;
234466d7ce93SDick Kennedy 	}
234566d7ce93SDick Kennedy 
23462299e432SDick Kennedy #endif
234766d7ce93SDick Kennedy 	/* Nothing found, all contexts for the MRQ are in-flight */
234866d7ce93SDick Kennedy 	return NULL;
234966d7ce93SDick Kennedy }
235066d7ce93SDick Kennedy 
2351d613b6a7SJames Smart /**
2352d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
2353d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
235466d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2355d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
2356d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2357d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2358d613b6a7SJames Smart  *
2359d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
2360d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
2361d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
2362d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
2363d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
2364d613b6a7SJames Smart  * of the discovery state machine.
2365d613b6a7SJames Smart  **/
2366d613b6a7SJames Smart static void
lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba * phba,uint32_t idx,struct rqb_dmabuf * nvmebuf,uint64_t isr_timestamp,uint8_t cqflag)2367d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
236866d7ce93SDick Kennedy 			    uint32_t idx,
2369d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
2370d74a89aaSJames Smart 			    uint64_t isr_timestamp,
2371d74a89aaSJames Smart 			    uint8_t cqflag)
2372d613b6a7SJames Smart {
23737cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
2374d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2375d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
23766c621a22SJames Smart 	struct lpfc_nvmet_ctxbuf *ctx_buf;
237766d7ce93SDick Kennedy 	struct lpfc_nvmet_ctx_info *current_infop;
2378472e146dSJames Smart 	uint32_t size, oxid, sid, qno;
23796c621a22SJames Smart 	unsigned long iflag;
238066d7ce93SDick Kennedy 	int current_cpu;
2381d613b6a7SJames Smart 
238273626173SArnd Bergmann 	if (!IS_ENABLED(CONFIG_NVME_TARGET_FC))
238373626173SArnd Bergmann 		return;
238473626173SArnd Bergmann 
23856c621a22SJames Smart 	ctx_buf = NULL;
2386d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
2387372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
23886c621a22SJames Smart 				"6157 NVMET FCP Drop IO\n");
2389472e146dSJames Smart 		if (nvmebuf)
2390472e146dSJames Smart 			lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2391472e146dSJames Smart 		return;
2392d613b6a7SJames Smart 	}
2393d613b6a7SJames Smart 
239466d7ce93SDick Kennedy 	/*
239566d7ce93SDick Kennedy 	 * Get a pointer to the context list for this MRQ based on
239666d7ce93SDick Kennedy 	 * the CPU this MRQ IRQ is associated with. If the CPU association
239766d7ce93SDick Kennedy 	 * changes from our initial assumption, the context list could
239866d7ce93SDick Kennedy 	 * be empty, thus it would need to be replenished with the
239966d7ce93SDick Kennedy 	 * context list from another CPU for this MRQ.
240066d7ce93SDick Kennedy 	 */
2401d6d189ceSBart Van Assche 	current_cpu = raw_smp_processor_id();
240266d7ce93SDick Kennedy 	current_infop = lpfc_get_ctx_list(phba, current_cpu, idx);
240366d7ce93SDick Kennedy 	spin_lock_irqsave(&current_infop->nvmet_ctx_list_lock, iflag);
240466d7ce93SDick Kennedy 	if (current_infop->nvmet_ctx_list_cnt) {
240566d7ce93SDick Kennedy 		list_remove_head(&current_infop->nvmet_ctx_list,
24066c621a22SJames Smart 				 ctx_buf, struct lpfc_nvmet_ctxbuf, list);
240766d7ce93SDick Kennedy 		current_infop->nvmet_ctx_list_cnt--;
2408966bb5b7SJames Smart 	} else {
240966d7ce93SDick Kennedy 		ctx_buf = lpfc_nvmet_replenish_context(phba, current_infop);
24106c621a22SJames Smart 	}
241166d7ce93SDick Kennedy 	spin_unlock_irqrestore(&current_infop->nvmet_ctx_list_lock, iflag);
24126c621a22SJames Smart 
2413a8cf5dfeSJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
2414a8cf5dfeSJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2415a8cf5dfeSJames Smart 	size = nvmebuf->bytes_recv;
2416a8cf5dfeSJames Smart 
2417a8cf5dfeSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2418840eda96SJames Smart 	if (phba->hdwqstat_on & LPFC_CHECK_NVMET_IO) {
2419840eda96SJames Smart 		this_cpu_inc(phba->sli4_hba.c_stat->rcv_io);
242063df6d63SJames Smart 		if (idx != current_cpu)
242163df6d63SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
242263df6d63SJames Smart 					"6703 CPU Check rcv: "
242363df6d63SJames Smart 					"cpu %d expect %d\n",
242463df6d63SJames Smart 					current_cpu, idx);
2425a8cf5dfeSJames Smart 	}
2426a8cf5dfeSJames Smart #endif
2427a8cf5dfeSJames Smart 
2428a8cf5dfeSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d CPU %02x\n",
2429d6d189ceSBart Van Assche 			 oxid, size, raw_smp_processor_id());
2430a8cf5dfeSJames Smart 
2431411de511SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2432411de511SJames Smart 
24336c621a22SJames Smart 	if (!ctx_buf) {
2434a8cf5dfeSJames Smart 		/* Queue this NVME IO to process later */
2435a8cf5dfeSJames Smart 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
2436a8cf5dfeSJames Smart 		list_add_tail(&nvmebuf->hbuf.list,
2437a8cf5dfeSJames Smart 			      &phba->sli4_hba.lpfc_nvmet_io_wait_list);
2438a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_cnt++;
2439a8cf5dfeSJames Smart 		phba->sli4_hba.nvmet_io_wait_total++;
2440a8cf5dfeSJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
2441a8cf5dfeSJames Smart 				       iflag);
2442a8cf5dfeSJames Smart 
2443a8cf5dfeSJames Smart 		/* Post a brand new DMA buffer to RQ */
2444a8cf5dfeSJames Smart 		qno = nvmebuf->idx;
2445a8cf5dfeSJames Smart 		lpfc_post_rq_buffer(
2446a8cf5dfeSJames Smart 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
2447a8cf5dfeSJames Smart 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2448411de511SJames Smart 
2449411de511SJames Smart 		atomic_inc(&tgtp->defer_ctx);
2450a8cf5dfeSJames Smart 		return;
24516c621a22SJames Smart 	}
2452d613b6a7SJames Smart 
2453d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
2454d613b6a7SJames Smart 
24557cacae2aSJames Smart 	ctxp = (struct lpfc_async_xchg_ctx *)ctx_buf->context;
245679d8c4ceSJames Smart 	spin_lock_irqsave(&phba->sli4_hba.t_active_list_lock, iflag);
245779d8c4ceSJames Smart 	list_add_tail(&ctxp->list, &phba->sli4_hba.t_active_ctx_list);
245879d8c4ceSJames Smart 	spin_unlock_irqrestore(&phba->sli4_hba.t_active_list_lock, iflag);
24597b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_FREE) {
2460372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2461ce1b591cSJames Smart 				"6414 NVMET Context corrupt %d %d oxid x%x\n",
2462ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
2463ce1b591cSJames Smart 	}
2464d613b6a7SJames Smart 	ctxp->wqeq = NULL;
2465d613b6a7SJames Smart 	ctxp->offset = 0;
2466d613b6a7SJames Smart 	ctxp->phba = phba;
2467d613b6a7SJames Smart 	ctxp->size = size;
2468d613b6a7SJames Smart 	ctxp->oxid = oxid;
2469d613b6a7SJames Smart 	ctxp->sid = sid;
247066d7ce93SDick Kennedy 	ctxp->idx = idx;
24717b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_RCV;
2472d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
2473d613b6a7SJames Smart 	ctxp->flag = 0;
24746c621a22SJames Smart 	ctxp->ctxbuf = ctx_buf;
2475cbc5de1bSJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
24761fbf9742SJames Smart 	ctxp->hdwq = NULL;
24772b7824d0SJames Smart 	spin_lock_init(&ctxp->ctxlock);
2478d613b6a7SJames Smart 
24792b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2480d74a89aaSJames Smart 	if (isr_timestamp)
24812b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
2482d74a89aaSJames Smart 	ctxp->ts_cmd_nvme = 0;
24832b65e182SJames Smart 	ctxp->ts_nvme_data = 0;
24842b65e182SJames Smart 	ctxp->ts_data_wqput = 0;
24852b65e182SJames Smart 	ctxp->ts_isr_data = 0;
24862b65e182SJames Smart 	ctxp->ts_data_nvme = 0;
24872b65e182SJames Smart 	ctxp->ts_nvme_status = 0;
24882b65e182SJames Smart 	ctxp->ts_status_wqput = 0;
24892b65e182SJames Smart 	ctxp->ts_isr_status = 0;
24902b65e182SJames Smart 	ctxp->ts_status_nvme = 0;
24912b65e182SJames Smart #endif
24922b65e182SJames Smart 
2493d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
2494d74a89aaSJames Smart 	/* check for cq processing load */
2495d74a89aaSJames Smart 	if (!cqflag) {
2496472e146dSJames Smart 		lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2497d74a89aaSJames Smart 		return;
2498d74a89aaSJames Smart 	}
2499d74a89aaSJames Smart 
2500d74a89aaSJames Smart 	if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
2501d74a89aaSJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
2502372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2503d74a89aaSJames Smart 				"6325 Unable to queue work for oxid x%x. "
2504d74a89aaSJames Smart 				"FCP Drop IO [x%x x%x x%x]\n",
2505d74a89aaSJames Smart 				ctxp->oxid,
2506d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_in),
2507d74a89aaSJames Smart 				atomic_read(&tgtp->rcv_fcp_cmd_out),
2508d74a89aaSJames Smart 				atomic_read(&tgtp->xmt_fcp_release));
2509d74a89aaSJames Smart 
2510d74a89aaSJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
2511d74a89aaSJames Smart 		lpfc_nvmet_defer_release(phba, ctxp);
2512d74a89aaSJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
2513d74a89aaSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
2514d74a89aaSJames Smart 	}
2515d613b6a7SJames Smart }
2516d613b6a7SJames Smart 
2517d613b6a7SJames Smart /**
2518d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
2519d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
252066d7ce93SDick Kennedy  * @idx: relative index of MRQ vector
2521d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
2522d74a89aaSJames Smart  * @isr_timestamp: in jiffies.
2523d74a89aaSJames Smart  * @cqflag: cq processing information regarding workload.
2524d613b6a7SJames Smart  *
2525d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
2526d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
2527d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
2528d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
2529d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
2530d613b6a7SJames Smart  **/
2531d613b6a7SJames Smart void
lpfc_nvmet_unsol_fcp_event(struct lpfc_hba * phba,uint32_t idx,struct rqb_dmabuf * nvmebuf,uint64_t isr_timestamp,uint8_t cqflag)2532d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
253366d7ce93SDick Kennedy 			   uint32_t idx,
2534d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
2535d74a89aaSJames Smart 			   uint64_t isr_timestamp,
2536d74a89aaSJames Smart 			   uint8_t cqflag)
2537d613b6a7SJames Smart {
253843bfea1bSJames Smart 	if (!nvmebuf) {
2539372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
254043bfea1bSJames Smart 				"3167 NVMET FCP Drop IO\n");
254143bfea1bSJames Smart 		return;
254243bfea1bSJames Smart 	}
2543d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
25446c621a22SJames Smart 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2545d613b6a7SJames Smart 		return;
2546d613b6a7SJames Smart 	}
2547d74a89aaSJames Smart 	lpfc_nvmet_unsol_fcp_buffer(phba, idx, nvmebuf, isr_timestamp, cqflag);
2548d613b6a7SJames Smart }
2549d613b6a7SJames Smart 
2550d613b6a7SJames Smart /**
2551d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
2552d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
2553d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
2554d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
2555d613b6a7SJames Smart  * @rspsize: size of the NVME command.
2556d613b6a7SJames Smart  *
2557d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
2558d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
2559d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
2560d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
2561d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
2562d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
2563d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
2564d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
2565d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
2566d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
2567d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
2568d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
2569d613b6a7SJames Smart  * reference for the command's callback function to access later.
2570d613b6a7SJames Smart  *
2571d613b6a7SJames Smart  * Return code
2572d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
2573d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
2574d613b6a7SJames Smart  **/
2575d613b6a7SJames Smart static struct lpfc_iocbq *
lpfc_nvmet_prep_ls_wqe(struct lpfc_hba * phba,struct lpfc_async_xchg_ctx * ctxp,dma_addr_t rspbuf,uint16_t rspsize)2576d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
25777cacae2aSJames Smart 		       struct lpfc_async_xchg_ctx *ctxp,
2578d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
2579d613b6a7SJames Smart {
2580d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2581d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2582205e8240SJames Smart 	union lpfc_wqe128 *wqe;
2583d613b6a7SJames Smart 
2584d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2585372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2586ce1b591cSJames Smart 				"6104 NVMET prep LS wqe: link err: "
2587ce1b591cSJames Smart 				"NPORT x%x oxid:x%x ste %d\n",
2588ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2589d613b6a7SJames Smart 		return NULL;
2590d613b6a7SJames Smart 	}
2591d613b6a7SJames Smart 
2592d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
2593d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
2594d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2595372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2596ce1b591cSJames Smart 				"6105 NVMET prep LS wqe: No WQE: "
2597ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2598ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2599d613b6a7SJames Smart 		return NULL;
2600d613b6a7SJames Smart 	}
2601d613b6a7SJames Smart 
2602d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2603307e3380SJames Smart 	if (!ndlp ||
2604d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2605d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2606372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2607ce1b591cSJames Smart 				"6106 NVMET prep LS wqe: No ndlp: "
2608ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2609ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2610d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2611d613b6a7SJames Smart 	}
2612d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
2613d613b6a7SJames Smart 
2614d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
2615d51cf5bdSJames Smart 	nvmewqe->ndlp = lpfc_nlp_get(ndlp);
2616d51cf5bdSJames Smart 	if (!nvmewqe->ndlp)
2617d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
2618d51cf5bdSJames Smart 	nvmewqe->context_un.axchg = ctxp;
2619d613b6a7SJames Smart 
2620d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
2621d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
2622d613b6a7SJames Smart 
2623d613b6a7SJames Smart 	/* Words 0 - 2 */
2624d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2625d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
2626d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
2627d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
2628d613b6a7SJames Smart 
2629d613b6a7SJames Smart 	/* Word 3 */
2630d613b6a7SJames Smart 
2631d613b6a7SJames Smart 	/* Word 4 */
2632d613b6a7SJames Smart 
2633d613b6a7SJames Smart 	/* Word 5 */
2634d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
2635d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
2636d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
26378b361639SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
2638d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
2639d613b6a7SJames Smart 
2640d613b6a7SJames Smart 	/* Word 6 */
2641d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
2642d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2643d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
2644d613b6a7SJames Smart 
2645d613b6a7SJames Smart 	/* Word 7 */
2646d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
2647d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
2648d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
2649d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
2650d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
2651d613b6a7SJames Smart 
2652d613b6a7SJames Smart 	/* Word 8 */
2653d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
2654d613b6a7SJames Smart 
2655d613b6a7SJames Smart 	/* Word 9 */
2656d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
2657d613b6a7SJames Smart 	/* Needs to be set by caller */
2658d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
2659d613b6a7SJames Smart 
2660d613b6a7SJames Smart 	/* Word 10 */
2661d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
2662d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2663d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
2664d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
2665d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
2666d613b6a7SJames Smart 
2667d613b6a7SJames Smart 	/* Word 11 */
2668d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
2669d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
2670d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
2671d613b6a7SJames Smart 	       OTHER_COMMAND);
2672d613b6a7SJames Smart 
2673d613b6a7SJames Smart 	/* Word 12 */
2674d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
2675d613b6a7SJames Smart 
2676d613b6a7SJames Smart 	nvmewqe->retry = 1;
2677d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2678d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2679a680a929SJames Smart 	nvmewqe->cmd_flag |= LPFC_IO_NVME_LS;
2680d613b6a7SJames Smart 
2681ce1b591cSJames Smart 	/* Xmit NVMET response to remote NPORT <did> */
2682d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2683ce1b591cSJames Smart 			"6039 Xmit NVMET LS response to remote "
2684d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
2685d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
2686d613b6a7SJames Smart 			rspsize);
2687d613b6a7SJames Smart 	return nvmewqe;
2688d613b6a7SJames Smart 
2689d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
2690d51cf5bdSJames Smart 	nvmewqe->context_un.axchg = NULL;
2691d51cf5bdSJames Smart 	nvmewqe->ndlp = NULL;
2692d51cf5bdSJames Smart 	nvmewqe->bpl_dmabuf = NULL;
2693d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
2694d613b6a7SJames Smart 	return NULL;
2695d613b6a7SJames Smart }
2696d613b6a7SJames Smart 
2697d613b6a7SJames Smart 
2698d613b6a7SJames Smart static struct lpfc_iocbq *
lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba * phba,struct lpfc_async_xchg_ctx * ctxp)2699d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
27007cacae2aSJames Smart 			struct lpfc_async_xchg_ctx *ctxp)
2701d613b6a7SJames Smart {
27027cacae2aSJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->hdlrctx.fcp_req;
2703d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2704d613b6a7SJames Smart 	struct sli4_sge *sgl;
2705d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
2706d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
2707d613b6a7SJames Smart 	struct scatterlist *sgel;
2708d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
27090bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
2710d613b6a7SJames Smart 	dma_addr_t physaddr;
2711e7f40349SJames Smart 	int i, cnt, nsegs;
2712315b3fd1SJames Smart 	bool use_pbde = false;
2713d613b6a7SJames Smart 	int xc = 1;
2714d613b6a7SJames Smart 
2715d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
2716372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2717ce1b591cSJames Smart 				"6107 NVMET prep FCP wqe: link err:"
2718ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2719ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2720d613b6a7SJames Smart 		return NULL;
2721d613b6a7SJames Smart 	}
2722d613b6a7SJames Smart 
2723d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2724307e3380SJames Smart 	if (!ndlp ||
2725d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2726d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2727372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2728ce1b591cSJames Smart 				"6108 NVMET prep FCP wqe: no ndlp: "
2729ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d\n",
2730ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state);
2731d613b6a7SJames Smart 		return NULL;
2732d613b6a7SJames Smart 	}
2733d613b6a7SJames Smart 
273481e6a637SJames Smart 	if (rsp->sg_cnt > lpfc_tgttemplate.max_sgl_segments) {
2735372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2736ce1b591cSJames Smart 				"6109 NVMET prep FCP wqe: seg cnt err: "
2737ce1b591cSJames Smart 				"NPORT x%x oxid x%x ste %d cnt %d\n",
2738ce1b591cSJames Smart 				ctxp->sid, ctxp->oxid, ctxp->state,
2739ce1b591cSJames Smart 				phba->cfg_nvme_seg_cnt);
2740d613b6a7SJames Smart 		return NULL;
2741d613b6a7SJames Smart 	}
2742e7f40349SJames Smart 	nsegs = rsp->sg_cnt;
2743d613b6a7SJames Smart 
2744d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2745d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
2746d613b6a7SJames Smart 	if (nvmewqe == NULL) {
2747d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
27486c621a22SJames Smart 		nvmewqe = ctxp->ctxbuf->iocbq;
2749d613b6a7SJames Smart 		if (nvmewqe == NULL) {
2750372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2751ce1b591cSJames Smart 					"6110 NVMET prep FCP wqe: No "
2752ce1b591cSJames Smart 					"WQE: NPORT x%x oxid x%x ste %d\n",
2753ce1b591cSJames Smart 					ctxp->sid, ctxp->oxid, ctxp->state);
2754d613b6a7SJames Smart 			return NULL;
2755d613b6a7SJames Smart 		}
2756d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
2757d613b6a7SJames Smart 		xc = 0; /* create new XRI */
2758d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
2759d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
2760d613b6a7SJames Smart 	}
2761d613b6a7SJames Smart 
2762d613b6a7SJames Smart 	/* Sanity check */
27637b7f551bSJames Smart 	if (((ctxp->state == LPFC_NVME_STE_RCV) &&
2764d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
27657b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_DATA)) {
2766205e8240SJames Smart 		wqe = &nvmewqe->wqe;
2767d613b6a7SJames Smart 	} else {
2768372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2769ce1b591cSJames Smart 				"6111 Wrong state NVMET FCP: %d  cnt %d\n",
2770ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt);
2771d613b6a7SJames Smart 		return NULL;
2772d613b6a7SJames Smart 	}
2773d613b6a7SJames Smart 
27746c621a22SJames Smart 	sgl  = (struct sli4_sge *)ctxp->ctxbuf->sglq->sgl;
2775d613b6a7SJames Smart 	switch (rsp->op) {
2776d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
2777d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
2778bd3061baSJames Smart 		/* From the tsend template, initialize words 7 - 11 */
2779bd3061baSJames Smart 		memcpy(&wqe->words[7],
2780bd3061baSJames Smart 		       &lpfc_tsend_cmd_template.words[7],
2781bd3061baSJames Smart 		       sizeof(uint32_t) * 5);
2782bd3061baSJames Smart 
2783d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
2784d613b6a7SJames Smart 		sgel = &rsp->sg[0];
2785d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2786d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2787d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
2788d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
2789d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
2790d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2791d613b6a7SJames Smart 
2792d613b6a7SJames Smart 		/* Word 3 */
2793d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
2794d613b6a7SJames Smart 
2795d613b6a7SJames Smart 		/* Word 4 */
2796d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
2797d613b6a7SJames Smart 
2798d613b6a7SJames Smart 		/* Word 5 */
2799bd3061baSJames Smart 		wqe->fcp_tsend.reserved = 0;
2800d613b6a7SJames Smart 
2801d613b6a7SJames Smart 		/* Word 6 */
2802d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
2803d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2804d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
2805d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2806d613b6a7SJames Smart 
2807bd3061baSJames Smart 		/* Word 7 - set ar later */
2808d613b6a7SJames Smart 
2809d613b6a7SJames Smart 		/* Word 8 */
2810d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
2811d613b6a7SJames Smart 
2812d613b6a7SJames Smart 		/* Word 9 */
2813d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
2814d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
2815d613b6a7SJames Smart 
2816bd3061baSJames Smart 		/* Word 10 - set wqes later, in template xc=1 */
2817bd3061baSJames Smart 		if (!xc)
2818bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 0);
2819d613b6a7SJames Smart 
2820d613b6a7SJames Smart 		/* Word 12 */
2821d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2822d613b6a7SJames Smart 
2823d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
2824d613b6a7SJames Smart 		sgl->addr_hi = 0;
2825d613b6a7SJames Smart 		sgl->addr_lo = 0;
2826d613b6a7SJames Smart 		sgl->word2 = 0;
2827d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2828d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2829d613b6a7SJames Smart 		sgl->sge_len = 0;
2830d613b6a7SJames Smart 		sgl++;
2831d613b6a7SJames Smart 		sgl->addr_hi = 0;
2832d613b6a7SJames Smart 		sgl->addr_lo = 0;
2833d613b6a7SJames Smart 		sgl->word2 = 0;
2834d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2835d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2836d613b6a7SJames Smart 		sgl->sge_len = 0;
2837d613b6a7SJames Smart 		sgl++;
2838d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
2839d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
2840bd3061baSJames Smart 
2841bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2842bd3061baSJames Smart 
284320aefac3SJames Smart 			if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
284420aefac3SJames Smart 				if (ndlp->nlp_flag & NLP_SUPPRESS_RSP)
284520aefac3SJames Smart 					bf_set(wqe_sup,
284620aefac3SJames Smart 					       &wqe->fcp_tsend.wqe_com, 1);
2847d613b6a7SJames Smart 			} else {
2848d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
2849d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
2850d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
2851d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
2852d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
2853d613b6a7SJames Smart 				       rsp->rsplen);
2854d613b6a7SJames Smart 			}
2855d613b6a7SJames Smart 		} else {
2856d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
2857d613b6a7SJames Smart 
2858bd3061baSJames Smart 			/* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2859d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
2860d613b6a7SJames Smart 		}
2861d613b6a7SJames Smart 		break;
2862d613b6a7SJames Smart 
2863d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
2864bd3061baSJames Smart 		/* From the treceive template, initialize words 3 - 11 */
2865bd3061baSJames Smart 		memcpy(&wqe->words[3],
2866bd3061baSJames Smart 		       &lpfc_treceive_cmd_template.words[3],
2867bd3061baSJames Smart 		       sizeof(uint32_t) * 9);
2868bd3061baSJames Smart 
2869ea85a20cSJames Smart 		/* Words 0 - 2 : First SGE is skipped, set invalid BDE type */
2870ea85a20cSJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = LPFC_SGE_TYPE_SKIP;
2871ea85a20cSJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = 0;
2872ea85a20cSJames Smart 		wqe->fcp_treceive.bde.addrLow = 0;
2873ea85a20cSJames Smart 		wqe->fcp_treceive.bde.addrHigh = 0;
2874d613b6a7SJames Smart 
2875d613b6a7SJames Smart 		/* Word 4 */
2876d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
2877d613b6a7SJames Smart 
2878d613b6a7SJames Smart 		/* Word 6 */
2879d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
2880d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2881d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
2882d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2883d613b6a7SJames Smart 
2884d613b6a7SJames Smart 		/* Word 7 */
2885d613b6a7SJames Smart 
2886d613b6a7SJames Smart 		/* Word 8 */
2887d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
2888d613b6a7SJames Smart 
2889d613b6a7SJames Smart 		/* Word 9 */
2890d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
2891d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
2892d613b6a7SJames Smart 
2893bd3061baSJames Smart 		/* Word 10 - in template xc=1 */
2894bd3061baSJames Smart 		if (!xc)
2895bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, 0);
2896d613b6a7SJames Smart 
2897315b3fd1SJames Smart 		/* Word 11 - check for pbde */
2898315b3fd1SJames Smart 		if (nsegs == 1 && phba->cfg_enable_pbde) {
2899315b3fd1SJames Smart 			use_pbde = true;
2900315b3fd1SJames Smart 			/* Word 11 - PBDE bit already preset by template */
2901bd3061baSJames Smart 		} else {
2902315b3fd1SJames Smart 			/* Overwrite default template setting */
2903bd3061baSJames Smart 			bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 0);
2904bd3061baSJames Smart 		}
2905d613b6a7SJames Smart 
2906d613b6a7SJames Smart 		/* Word 12 */
2907d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2908d613b6a7SJames Smart 
2909ea85a20cSJames Smart 		/* Setup 2 SKIP SGEs */
2910ea85a20cSJames Smart 		sgl->addr_hi = 0;
2911ea85a20cSJames Smart 		sgl->addr_lo = 0;
2912d613b6a7SJames Smart 		sgl->word2 = 0;
2913ea85a20cSJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2914d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2915ea85a20cSJames Smart 		sgl->sge_len = 0;
2916d613b6a7SJames Smart 		sgl++;
2917d613b6a7SJames Smart 		sgl->addr_hi = 0;
2918d613b6a7SJames Smart 		sgl->addr_lo = 0;
2919d613b6a7SJames Smart 		sgl->word2 = 0;
2920d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2921d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
2922d613b6a7SJames Smart 		sgl->sge_len = 0;
2923d613b6a7SJames Smart 		sgl++;
2924d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
2925d613b6a7SJames Smart 		break;
2926d613b6a7SJames Smart 
2927d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
2928bd3061baSJames Smart 		/* From the treceive template, initialize words 4 - 11 */
2929bd3061baSJames Smart 		memcpy(&wqe->words[4],
2930bd3061baSJames Smart 		       &lpfc_trsp_cmd_template.words[4],
2931bd3061baSJames Smart 		       sizeof(uint32_t) * 8);
2932bd3061baSJames Smart 
2933d613b6a7SJames Smart 		/* Words 0 - 2 */
2934d613b6a7SJames Smart 		physaddr = rsp->rspdma;
2935d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2936d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
2937d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
2938d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
2939d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
2940d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
2941d613b6a7SJames Smart 
2942d613b6a7SJames Smart 		/* Word 3 */
2943d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
2944d613b6a7SJames Smart 
2945d613b6a7SJames Smart 		/* Word 6 */
2946d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
2947d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2948d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
2949d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
2950d613b6a7SJames Smart 
2951d613b6a7SJames Smart 		/* Word 7 */
2952d613b6a7SJames Smart 
2953d613b6a7SJames Smart 		/* Word 8 */
2954d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
2955d613b6a7SJames Smart 
2956d613b6a7SJames Smart 		/* Word 9 */
2957d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
2958d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
2959d613b6a7SJames Smart 
2960d613b6a7SJames Smart 		/* Word 10 */
2961bd3061baSJames Smart 		if (xc)
2962bd3061baSJames Smart 			bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 1);
2963d613b6a7SJames Smart 
2964d613b6a7SJames Smart 		/* Word 11 */
2965bd3061baSJames Smart 		/* In template wqes=0 irsp=0 irsplen=0 - good response */
2966bd3061baSJames Smart 		if (rsp->rsplen != LPFC_NVMET_SUCCESS_LEN) {
2967bd3061baSJames Smart 			/* Bad response - embed it */
2968d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
2969d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
2970d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
2971d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
2972d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
2973d613b6a7SJames Smart 		}
2974bd3061baSJames Smart 
2975bd3061baSJames Smart 		/* Word 12 */
2976bd3061baSJames Smart 		wqe->fcp_trsp.rsvd_12_15[0] = 0;
2977d613b6a7SJames Smart 
2978d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
2979e7f40349SJames Smart 		nsegs = 0;
2980d613b6a7SJames Smart 		sgl->word2 = 0;
2981d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
2982d613b6a7SJames Smart 		break;
2983d613b6a7SJames Smart 
2984d613b6a7SJames Smart 	default:
2985d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2986d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
2987d613b6a7SJames Smart 				rsp->op);
2988d613b6a7SJames Smart 		return NULL;
2989d613b6a7SJames Smart 	}
2990d613b6a7SJames Smart 
2991d613b6a7SJames Smart 	nvmewqe->retry = 1;
2992d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
2993d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2994d51cf5bdSJames Smart 	nvmewqe->ndlp = ndlp;
2995d613b6a7SJames Smart 
2996e7f40349SJames Smart 	for_each_sg(rsp->sg, sgel, nsegs, i) {
2997d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
2998d613b6a7SJames Smart 		cnt = sg_dma_len(sgel);
2999d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
3000d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
3001d613b6a7SJames Smart 		sgl->word2 = 0;
3002d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
3003d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
3004d613b6a7SJames Smart 		if ((i+1) == rsp->sg_cnt)
3005d613b6a7SJames Smart 			bf_set(lpfc_sli4_sge_last, sgl, 1);
3006d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
3007d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(cnt);
3008315b3fd1SJames Smart 		sgl++;
3009315b3fd1SJames Smart 		ctxp->offset += cnt;
3010315b3fd1SJames Smart 	}
3011315b3fd1SJames Smart 
30120bc2b7c5SJames Smart 	bde = (struct ulp_bde64 *)&wqe->words[13];
3013315b3fd1SJames Smart 	if (use_pbde) {
3014315b3fd1SJames Smart 		/* decrement sgl ptr backwards once to first data sge */
3015315b3fd1SJames Smart 		sgl--;
3016315b3fd1SJames Smart 
30170bc2b7c5SJames Smart 		/* Words 13-15 (PBDE) */
30180bc2b7c5SJames Smart 		bde->addrLow = sgl->addr_lo;
30190bc2b7c5SJames Smart 		bde->addrHigh = sgl->addr_hi;
3020315b3fd1SJames Smart 		bde->tus.f.bdeSize = le32_to_cpu(sgl->sge_len);
30210bc2b7c5SJames Smart 		bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
30220bc2b7c5SJames Smart 		bde->tus.w = cpu_to_le32(bde->tus.w);
3023414abe0aSJames Smart 	} else {
3024414abe0aSJames Smart 		memset(bde, 0, sizeof(struct ulp_bde64));
3025414abe0aSJames Smart 	}
30267b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DATA;
3027ce1b591cSJames Smart 	ctxp->entry_cnt++;
3028d613b6a7SJames Smart 	return nvmewqe;
3029d613b6a7SJames Smart }
3030d613b6a7SJames Smart 
3031d613b6a7SJames Smart /**
3032d613b6a7SJames Smart  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
3033d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3034d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3035a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
3036d613b6a7SJames Smart  *
3037d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3038d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
3039d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3040d613b6a7SJames Smart  **/
3041d613b6a7SJames Smart static void
lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdwqe,struct lpfc_iocbq * rspwqe)3042d613b6a7SJames Smart lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3043a680a929SJames Smart 			     struct lpfc_iocbq *rspwqe)
3044d613b6a7SJames Smart {
30457cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3046d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3047b27cbd55SBart Van Assche 	uint32_t result;
304819b58d94SJames Smart 	unsigned long flags;
304919b58d94SJames Smart 	bool released = false;
3050a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
3051d613b6a7SJames Smart 
3052d51cf5bdSJames Smart 	ctxp = cmdwqe->context_un.axchg;
3053d613b6a7SJames Smart 	result = wcqe->parameter;
3054d613b6a7SJames Smart 
3055d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
30567b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABORT_OP)
3057547077a4SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
3058d613b6a7SJames Smart 
3059c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
30607b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DONE;
306186c67379SJames Smart 
306286c67379SJames Smart 	/* Check if we already received a free context call
306386c67379SJames Smart 	 * and we have completed processing an abort situation.
306486c67379SJames Smart 	 */
30657b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_CTX_RLS) &&
30667b7f551bSJames Smart 	    !(ctxp->flag & LPFC_NVME_XBUSY)) {
3067c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
306879d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3069c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
307019b58d94SJames Smart 		released = true;
307186c67379SJames Smart 	}
30727b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
307319b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3074547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
307519b58d94SJames Smart 
3076e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
307779d8c4ceSJames Smart 			"6165 ABORT cmpl: oxid x%x flg x%x (%d) "
307886c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
307986c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
308086c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
308186c67379SJames Smart 			result, wcqe->word3);
308286c67379SJames Smart 
3083d51cf5bdSJames Smart 	cmdwqe->rsp_dmabuf = NULL;
3084d51cf5bdSJames Smart 	cmdwqe->bpl_dmabuf = NULL;
308519b58d94SJames Smart 	/*
308619b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
308719b58d94SJames Smart 	 * will be recycled by transport release call.
308819b58d94SJames Smart 	 */
308919b58d94SJames Smart 	if (released)
30906c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3091d613b6a7SJames Smart 
30926c621a22SJames Smart 	/* This is the iocbq for the abort, not the command */
3093d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
309486c67379SJames Smart 
309586c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
30967b7f551bSJames Smart 	 * For LPFC_NVME_XBUSY, lpfc_sli4_nvmet_xri_aborted
309786c67379SJames Smart 	 * should have been called already.
309886c67379SJames Smart 	 */
3099d613b6a7SJames Smart }
3100d613b6a7SJames Smart 
3101d613b6a7SJames Smart /**
310286c67379SJames Smart  * lpfc_nvmet_unsol_fcp_abort_cmp - Completion handler for ABTS
3103d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3104d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3105a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
3106d613b6a7SJames Smart  *
3107d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3108d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
3109d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3110d613b6a7SJames Smart  **/
3111d613b6a7SJames Smart static void
lpfc_nvmet_unsol_fcp_abort_cmp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdwqe,struct lpfc_iocbq * rspwqe)311286c67379SJames Smart lpfc_nvmet_unsol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3113a680a929SJames Smart 			       struct lpfc_iocbq *rspwqe)
3114d613b6a7SJames Smart {
31157cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3116d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
311719b58d94SJames Smart 	unsigned long flags;
3118b27cbd55SBart Van Assche 	uint32_t result;
311919b58d94SJames Smart 	bool released = false;
3120a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
3121d613b6a7SJames Smart 
3122d51cf5bdSJames Smart 	ctxp = cmdwqe->context_un.axchg;
3123d613b6a7SJames Smart 	result = wcqe->parameter;
3124d613b6a7SJames Smart 
312586c67379SJames Smart 	if (!ctxp) {
312686c67379SJames Smart 		/* if context is clear, related io alrady complete */
3127d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
312886c67379SJames Smart 				"6070 ABTS cmpl: WCQE: %08x %08x %08x %08x\n",
312986c67379SJames Smart 				wcqe->word0, wcqe->total_data_placed,
3130d613b6a7SJames Smart 				result, wcqe->word3);
313186c67379SJames Smart 		return;
313286c67379SJames Smart 	}
3133d613b6a7SJames Smart 
313478e1d200SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3135c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
31367b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_ABORT_OP)
313778e1d200SJames Smart 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
313878e1d200SJames Smart 
3139d613b6a7SJames Smart 	/* Sanity check */
31407b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_ABORT) {
3141372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
314286c67379SJames Smart 				"6112 ABTS Wrong state:%d oxid x%x\n",
3143d613b6a7SJames Smart 				ctxp->state, ctxp->oxid);
3144d613b6a7SJames Smart 	}
314586c67379SJames Smart 
314686c67379SJames Smart 	/* Check if we already received a free context call
314786c67379SJames Smart 	 * and we have completed processing an abort situation.
314886c67379SJames Smart 	 */
31497b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_DONE;
31507b7f551bSJames Smart 	if ((ctxp->flag & LPFC_NVME_CTX_RLS) &&
31517b7f551bSJames Smart 	    !(ctxp->flag & LPFC_NVME_XBUSY)) {
3152c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
315379d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3154c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
315519b58d94SJames Smart 		released = true;
315686c67379SJames Smart 	}
31577b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
315819b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3159547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp);
316019b58d94SJames Smart 
316186c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
316279d8c4ceSJames Smart 			"6316 ABTS cmpl oxid x%x flg x%x (%x) "
316386c67379SJames Smart 			"WCQE: %08x %08x %08x %08x\n",
316486c67379SJames Smart 			ctxp->oxid, ctxp->flag, released,
316586c67379SJames Smart 			wcqe->word0, wcqe->total_data_placed,
316686c67379SJames Smart 			result, wcqe->word3);
31676c621a22SJames Smart 
3168d51cf5bdSJames Smart 	cmdwqe->rsp_dmabuf = NULL;
3169d51cf5bdSJames Smart 	cmdwqe->bpl_dmabuf = NULL;
317019b58d94SJames Smart 	/*
317119b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
317219b58d94SJames Smart 	 * will be recycled by transport release call.
317319b58d94SJames Smart 	 */
317419b58d94SJames Smart 	if (released)
31756c621a22SJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
317686c67379SJames Smart 
317786c67379SJames Smart 	/* Since iaab/iaar are NOT set, there is no work left.
31787b7f551bSJames Smart 	 * For LPFC_NVME_XBUSY, lpfc_sli4_nvmet_xri_aborted
317986c67379SJames Smart 	 * should have been called already.
318086c67379SJames Smart 	 */
3181d613b6a7SJames Smart }
3182d613b6a7SJames Smart 
3183d613b6a7SJames Smart /**
3184d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
3185d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
3186d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
3187a680a929SJames Smart  * @rspwqe: Pointer to driver response WQE object.
3188d613b6a7SJames Smart  *
3189d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
3190d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for LS cmds
3191d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
3192d613b6a7SJames Smart  **/
3193d613b6a7SJames Smart static void
lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba * phba,struct lpfc_iocbq * cmdwqe,struct lpfc_iocbq * rspwqe)3194d613b6a7SJames Smart lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
3195a680a929SJames Smart 			    struct lpfc_iocbq *rspwqe)
3196d613b6a7SJames Smart {
31977cacae2aSJames Smart 	struct lpfc_async_xchg_ctx *ctxp;
3198d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3199b27cbd55SBart Van Assche 	uint32_t result;
3200a680a929SJames Smart 	struct lpfc_wcqe_complete *wcqe = &rspwqe->wcqe_cmpl;
3201d613b6a7SJames Smart 
3202d51cf5bdSJames Smart 	ctxp = cmdwqe->context_un.axchg;
3203d613b6a7SJames Smart 	result = wcqe->parameter;
3204d613b6a7SJames Smart 
3205fcdd14b8SJames Smart 	if (phba->nvmet_support) {
3206d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3207547077a4SJames Smart 		atomic_inc(&tgtp->xmt_ls_abort_cmpl);
3208fcdd14b8SJames Smart 	}
3209d613b6a7SJames Smart 
3210d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
321132350664SJames Smart 			"6083 Abort cmpl: ctx x%px WCQE:%08x %08x %08x %08x\n",
3212d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
3213d613b6a7SJames Smart 			result, wcqe->word3);
3214d613b6a7SJames Smart 
3215ce1b591cSJames Smart 	if (!ctxp) {
3216372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3217ce1b591cSJames Smart 				"6415 NVMET LS Abort No ctx: WCQE: "
3218ce1b591cSJames Smart 				 "%08x %08x %08x %08x\n",
3219ce1b591cSJames Smart 				wcqe->word0, wcqe->total_data_placed,
3220ce1b591cSJames Smart 				result, wcqe->word3);
3221ce1b591cSJames Smart 
3222ce1b591cSJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
3223ce1b591cSJames Smart 		return;
3224ce1b591cSJames Smart 	}
3225ce1b591cSJames Smart 
32267b7f551bSJames Smart 	if (ctxp->state != LPFC_NVME_STE_LS_ABORT) {
3227372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3228ce1b591cSJames Smart 				"6416 NVMET LS abort cmpl state mismatch: "
3229ce1b591cSJames Smart 				"oxid x%x: %d %d\n",
3230ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3231ce1b591cSJames Smart 	}
3232ce1b591cSJames Smart 
3233d51cf5bdSJames Smart 	cmdwqe->rsp_dmabuf = NULL;
3234d51cf5bdSJames Smart 	cmdwqe->bpl_dmabuf = NULL;
3235d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
3236d613b6a7SJames Smart 	kfree(ctxp);
3237d613b6a7SJames Smart }
3238d613b6a7SJames Smart 
3239d613b6a7SJames Smart static int
lpfc_nvmet_unsol_issue_abort(struct lpfc_hba * phba,struct lpfc_async_xchg_ctx * ctxp,uint32_t sid,uint16_t xri)3240d613b6a7SJames Smart lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
32417cacae2aSJames Smart 			     struct lpfc_async_xchg_ctx *ctxp,
3242d613b6a7SJames Smart 			     uint32_t sid, uint16_t xri)
3243d613b6a7SJames Smart {
3244fcdd14b8SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = NULL;
3245d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3246205e8240SJames Smart 	union lpfc_wqe128 *wqe_abts;
3247d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3248d613b6a7SJames Smart 
3249d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
325086c67379SJames Smart 			"6067 ABTS: sid %x xri x%x/x%x\n",
3251318083adSJames Smart 			sid, xri, ctxp->wqeq->sli4_xritag);
3252d613b6a7SJames Smart 
3253fcdd14b8SJames Smart 	if (phba->nvmet_support && phba->targetport)
3254d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3255d613b6a7SJames Smart 
3256d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3257307e3380SJames Smart 	if (!ndlp ||
3258d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3259d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3260fcdd14b8SJames Smart 		if (tgtp)
3261d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_abort_rsp_error);
3262372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3263d613b6a7SJames Smart 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
3264b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3265d613b6a7SJames Smart 
3266d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3267d613b6a7SJames Smart 		return 0;
3268d613b6a7SJames Smart 	}
3269d613b6a7SJames Smart 
3270d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
3271d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
3272d613b6a7SJames Smart 
3273d613b6a7SJames Smart 	/*
3274d613b6a7SJames Smart 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
3275d613b6a7SJames Smart 	 * that were initialized in lpfc_sli4_nvmet_alloc.
3276d613b6a7SJames Smart 	 */
3277d613b6a7SJames Smart 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
3278d613b6a7SJames Smart 
3279d613b6a7SJames Smart 	/* Word 5 */
3280d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
3281d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
3282d613b6a7SJames Smart 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
3283d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
3284d613b6a7SJames Smart 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
3285d613b6a7SJames Smart 
3286d613b6a7SJames Smart 	/* Word 6 */
3287d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
3288d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
3289d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
3290d613b6a7SJames Smart 	       abts_wqeq->sli4_xritag);
3291d613b6a7SJames Smart 
3292d613b6a7SJames Smart 	/* Word 7 */
3293d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
3294d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
3295d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
3296d613b6a7SJames Smart 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
3297d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
3298d613b6a7SJames Smart 
3299d613b6a7SJames Smart 	/* Word 8 */
3300d613b6a7SJames Smart 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
3301d613b6a7SJames Smart 
3302d613b6a7SJames Smart 	/* Word 9 */
3303d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
3304d613b6a7SJames Smart 	/* Needs to be set by caller */
3305d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
3306d613b6a7SJames Smart 
3307d613b6a7SJames Smart 	/* Word 10 */
3308d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
3309d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
3310d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
3311d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
3312d613b6a7SJames Smart 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
3313d613b6a7SJames Smart 
3314d613b6a7SJames Smart 	/* Word 11 */
3315d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
3316d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
3317d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
3318d613b6a7SJames Smart 	       OTHER_COMMAND);
3319d613b6a7SJames Smart 
3320d613b6a7SJames Smart 	abts_wqeq->vport = phba->pport;
3321d51cf5bdSJames Smart 	abts_wqeq->ndlp = ndlp;
3322d51cf5bdSJames Smart 	abts_wqeq->context_un.axchg = ctxp;
3323d51cf5bdSJames Smart 	abts_wqeq->bpl_dmabuf = NULL;
3324a680a929SJames Smart 	abts_wqeq->num_bdes = 0;
3325d613b6a7SJames Smart 	/* hba_wqidx should already be setup from command we are aborting */
3326d613b6a7SJames Smart 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
3327d613b6a7SJames Smart 	abts_wqeq->iocb.ulpLe = 1;
3328d613b6a7SJames Smart 
3329d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
3330d613b6a7SJames Smart 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
3331d613b6a7SJames Smart 			xri, abts_wqeq->iotag);
3332d613b6a7SJames Smart 	return 1;
3333d613b6a7SJames Smart }
3334d613b6a7SJames Smart 
3335d613b6a7SJames Smart static int
lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba * phba,struct lpfc_async_xchg_ctx * ctxp,uint32_t sid,uint16_t xri)3336d613b6a7SJames Smart lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
33377cacae2aSJames Smart 			       struct lpfc_async_xchg_ctx *ctxp,
3338d613b6a7SJames Smart 			       uint32_t sid, uint16_t xri)
3339d613b6a7SJames Smart {
3340d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3341d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3342d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
3343d613b6a7SJames Smart 	unsigned long flags;
3344b21c9debSJames Smart 	bool ia;
3345d613b6a7SJames Smart 	int rc;
3346d613b6a7SJames Smart 
3347d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3348d613b6a7SJames Smart 	if (!ctxp->wqeq) {
33496c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3350d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3351d613b6a7SJames Smart 	}
3352d613b6a7SJames Smart 
3353d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
3354307e3380SJames Smart 	if (!ndlp ||
3355d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3356d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3357d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3358372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
335986c67379SJames Smart 				"6160 Drop ABORT - wrong NDLP state x%x.\n",
3360b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3361d613b6a7SJames Smart 
3362d613b6a7SJames Smart 		/* No failure to an ABTS request. */
3363c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
33647b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3365c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3366d613b6a7SJames Smart 		return 0;
3367d613b6a7SJames Smart 	}
3368d613b6a7SJames Smart 
3369d613b6a7SJames Smart 	/* Issue ABTS for this WQE based on iotag */
3370d613b6a7SJames Smart 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
3371c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
3372d613b6a7SJames Smart 	if (!ctxp->abort_wqeq) {
3373547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3374372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
337586c67379SJames Smart 				"6161 ABORT failed: No wqeqs: "
3376d613b6a7SJames Smart 				"xri: x%x\n", ctxp->oxid);
3377d613b6a7SJames Smart 		/* No failure to an ABTS request. */
33787b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3379c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3380d613b6a7SJames Smart 		return 0;
3381d613b6a7SJames Smart 	}
3382d613b6a7SJames Smart 	abts_wqeq = ctxp->abort_wqeq;
33837b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_ABORT;
3384b21c9debSJames Smart 	ia = (ctxp->flag & LPFC_NVME_ABTS_RCV) ? true : false;
3385c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3386d613b6a7SJames Smart 
3387d613b6a7SJames Smart 	/* Announce entry to new IO submit field. */
338886c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
338986c67379SJames Smart 			"6162 ABORT Request to rport DID x%06x "
3390d613b6a7SJames Smart 			"for xri x%x x%x\n",
3391d613b6a7SJames Smart 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
3392d613b6a7SJames Smart 
3393d613b6a7SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
3394d613b6a7SJames Smart 	 * cleared when the reset is complete and rings reestablished.
3395d613b6a7SJames Smart 	 */
3396d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3397d613b6a7SJames Smart 	/* driver queued commands are in process of being flushed */
3398c00f62e6SJames Smart 	if (phba->hba_flag & HBA_IOQ_FLUSH) {
3399d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3400547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3401372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3402d613b6a7SJames Smart 				"6163 Driver in reset cleanup - flushing "
3403d613b6a7SJames Smart 				"NVME Req now. hba_flag x%x oxid x%x\n",
3404d613b6a7SJames Smart 				phba->hba_flag, ctxp->oxid);
3405d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3406c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
34077b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3408c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3409d613b6a7SJames Smart 		return 0;
3410d613b6a7SJames Smart 	}
3411d613b6a7SJames Smart 
3412d613b6a7SJames Smart 	/* Outstanding abort is in progress */
3413a680a929SJames Smart 	if (abts_wqeq->cmd_flag & LPFC_DRIVER_ABORTED) {
3414d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
3415547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3416372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3417d613b6a7SJames Smart 				"6164 Outstanding NVME I/O Abort Request "
3418d613b6a7SJames Smart 				"still pending on oxid x%x\n",
3419d613b6a7SJames Smart 				ctxp->oxid);
3420d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
3421c160c0f8SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
34227b7f551bSJames Smart 		ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3423c160c0f8SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3424d613b6a7SJames Smart 		return 0;
3425d613b6a7SJames Smart 	}
3426d613b6a7SJames Smart 
3427d613b6a7SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
3428a680a929SJames Smart 	abts_wqeq->cmd_flag |= LPFC_DRIVER_ABORTED;
3429d613b6a7SJames Smart 
3430b21c9debSJames Smart 	lpfc_sli_prep_abort_xri(phba, abts_wqeq, ctxp->wqeq->sli4_xritag,
3431b21c9debSJames Smart 				abts_wqeq->iotag, CLASS3,
3432b21c9debSJames Smart 				LPFC_WQE_CQ_ID_DEFAULT, ia, true);
3433d613b6a7SJames Smart 
3434d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
3435d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
3436a680a929SJames Smart 	abts_wqeq->cmd_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
3437a680a929SJames Smart 	abts_wqeq->cmd_flag |= LPFC_IO_NVME;
3438d51cf5bdSJames Smart 	abts_wqeq->context_un.axchg = ctxp;
34394550f9c7SJames Smart 	abts_wqeq->vport = phba->pport;
34401fbf9742SJames Smart 	if (!ctxp->hdwq)
34411fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
34421fbf9742SJames Smart 
34431fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3444d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3445547077a4SJames Smart 	if (rc == WQE_SUCCESS) {
3446547077a4SJames Smart 		atomic_inc(&tgtp->xmt_abort_sol);
3447d613b6a7SJames Smart 		return 0;
3448547077a4SJames Smart 	}
3449d613b6a7SJames Smart 
3450547077a4SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3451c160c0f8SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
34527b7f551bSJames Smart 	ctxp->flag &= ~LPFC_NVME_ABORT_OP;
3453c160c0f8SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3454d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3455372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
345686c67379SJames Smart 			"6166 Failed ABORT issue_wqe with status x%x "
3457d613b6a7SJames Smart 			"for oxid x%x.\n",
3458d613b6a7SJames Smart 			rc, ctxp->oxid);
3459d613b6a7SJames Smart 	return 1;
3460d613b6a7SJames Smart }
3461d613b6a7SJames Smart 
3462d613b6a7SJames Smart static int
lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba * phba,struct lpfc_async_xchg_ctx * ctxp,uint32_t sid,uint16_t xri)3463d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
34647cacae2aSJames Smart 				 struct lpfc_async_xchg_ctx *ctxp,
3465d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
3466d613b6a7SJames Smart {
3467d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
3468d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3469d613b6a7SJames Smart 	unsigned long flags;
3470c160c0f8SJames Smart 	bool released = false;
3471d613b6a7SJames Smart 	int rc;
3472d613b6a7SJames Smart 
3473d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3474d613b6a7SJames Smart 	if (!ctxp->wqeq) {
34756c621a22SJames Smart 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
3476d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
3477d613b6a7SJames Smart 	}
3478d613b6a7SJames Smart 
34797b7f551bSJames Smart 	if (ctxp->state == LPFC_NVME_STE_FREE) {
3480372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3481ce1b591cSJames Smart 				"6417 NVMET ABORT ctx freed %d %d oxid x%x\n",
3482ce1b591cSJames Smart 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
3483ce1b591cSJames Smart 		rc = WQE_BUSY;
3484ce1b591cSJames Smart 		goto aerr;
3485ce1b591cSJames Smart 	}
34867b7f551bSJames Smart 	ctxp->state = LPFC_NVME_STE_ABORT;
3487ce1b591cSJames Smart 	ctxp->entry_cnt++;
3488d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
3489d613b6a7SJames Smart 	if (rc == 0)
3490d613b6a7SJames Smart 		goto aerr;
3491d613b6a7SJames Smart 
3492d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3493d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
3494a680a929SJames Smart 	abts_wqeq->cmd_cmpl = lpfc_nvmet_unsol_fcp_abort_cmp;
3495a680a929SJames Smart 	abts_wqeq->cmd_flag |= LPFC_IO_NVMET;
34961fbf9742SJames Smart 	if (!ctxp->hdwq)
34971fbf9742SJames Smart 		ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
34981fbf9742SJames Smart 
34991fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3500d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3501d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3502d613b6a7SJames Smart 		return 0;
3503d613b6a7SJames Smart 	}
3504d613b6a7SJames Smart 
3505d613b6a7SJames Smart aerr:
350668c9b55dSJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
35077b7f551bSJames Smart 	if (ctxp->flag & LPFC_NVME_CTX_RLS) {
3508c160c0f8SJames Smart 		spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
350979d8c4ceSJames Smart 		list_del_init(&ctxp->list);
3510c160c0f8SJames Smart 		spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
3511c160c0f8SJames Smart 		released = true;
3512c160c0f8SJames Smart 	}
35137b7f551bSJames Smart 	ctxp->flag &= ~(LPFC_NVME_ABORT_OP | LPFC_NVME_CTX_RLS);
351468c9b55dSJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
351568c9b55dSJames Smart 
3516d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
3517372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
351879d8c4ceSJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x "
351979d8c4ceSJames Smart 			"(%x)\n",
352079d8c4ceSJames Smart 			ctxp->oxid, rc, released);
3521c160c0f8SJames Smart 	if (released)
352268c9b55dSJames Smart 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3523d613b6a7SJames Smart 	return 1;
3524d613b6a7SJames Smart }
3525d613b6a7SJames Smart 
35263a8070c5SJames Smart /**
35273a8070c5SJames Smart  * lpfc_nvme_unsol_ls_issue_abort - issue ABTS on an exchange received
35283a8070c5SJames Smart  *        via async frame receive where the frame is not handled.
35293a8070c5SJames Smart  * @phba: pointer to adapter structure
35303a8070c5SJames Smart  * @ctxp: pointer to the asynchronously received received sequence
35313a8070c5SJames Smart  * @sid: address of the remote port to send the ABTS to
35323a8070c5SJames Smart  * @xri: oxid value to for the ABTS (other side's exchange id).
35333a8070c5SJames Smart  **/
35343a8070c5SJames Smart int
lpfc_nvme_unsol_ls_issue_abort(struct lpfc_hba * phba,struct lpfc_async_xchg_ctx * ctxp,uint32_t sid,uint16_t xri)35353a8070c5SJames Smart lpfc_nvme_unsol_ls_issue_abort(struct lpfc_hba *phba,
35367cacae2aSJames Smart 				struct lpfc_async_xchg_ctx *ctxp,
3537d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
3538d613b6a7SJames Smart {
3539fcdd14b8SJames Smart 	struct lpfc_nvmet_tgtport *tgtp = NULL;
3540d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
3541d613b6a7SJames Smart 	unsigned long flags;
3542d613b6a7SJames Smart 	int rc;
3543d613b6a7SJames Smart 
35447b7f551bSJames Smart 	if ((ctxp->state == LPFC_NVME_STE_LS_RCV && ctxp->entry_cnt == 1) ||
35457b7f551bSJames Smart 	    (ctxp->state == LPFC_NVME_STE_LS_RSP && ctxp->entry_cnt == 2)) {
35467b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_LS_ABORT;
3547ce1b591cSJames Smart 		ctxp->entry_cnt++;
3548ce1b591cSJames Smart 	} else {
3549372c187bSDick Kennedy 		lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3550ce1b591cSJames Smart 				"6418 NVMET LS abort state mismatch "
3551ce1b591cSJames Smart 				"IO x%x: %d %d\n",
3552ce1b591cSJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
35537b7f551bSJames Smart 		ctxp->state = LPFC_NVME_STE_LS_ABORT;
3554ce1b591cSJames Smart 	}
3555ce1b591cSJames Smart 
3556fcdd14b8SJames Smart 	if (phba->nvmet_support && phba->targetport)
3557d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3558fcdd14b8SJames Smart 
3559d613b6a7SJames Smart 	if (!ctxp->wqeq) {
3560d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
3561d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
3562d613b6a7SJames Smart 		if (!ctxp->wqeq) {
3563372c187bSDick Kennedy 			lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3564d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
3565d613b6a7SJames Smart 					"xri: x%x\n", xri);
3566d613b6a7SJames Smart 			/* No failure to an ABTS request. */
3567d613b6a7SJames Smart 			kfree(ctxp);
3568d613b6a7SJames Smart 			return 0;
3569d613b6a7SJames Smart 		}
3570d613b6a7SJames Smart 	}
3571d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
35726c621a22SJames Smart 
3573ce1b591cSJames Smart 	if (lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri) == 0) {
3574ce1b591cSJames Smart 		rc = WQE_BUSY;
3575ce1b591cSJames Smart 		goto out;
3576ce1b591cSJames Smart 	}
3577d613b6a7SJames Smart 
3578d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
3579a680a929SJames Smart 	abts_wqeq->cmd_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
3580a680a929SJames Smart 	abts_wqeq->cmd_flag |=  LPFC_IO_NVME_LS;
35811fbf9742SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3582d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
3583d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
3584fcdd14b8SJames Smart 		if (tgtp)
3585547077a4SJames Smart 			atomic_inc(&tgtp->xmt_abort_unsol);
3586d613b6a7SJames Smart 		return 0;
3587d613b6a7SJames Smart 	}
3588ce1b591cSJames Smart out:
3589fcdd14b8SJames Smart 	if (tgtp)
3590d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
3591d51cf5bdSJames Smart 	abts_wqeq->rsp_dmabuf = NULL;
3592d51cf5bdSJames Smart 	abts_wqeq->bpl_dmabuf = NULL;
3593d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
3594372c187bSDick Kennedy 	lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3595d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
35964e57e0b9SJames Smart 	return 1;
3597d613b6a7SJames Smart }
35984c2805aaSJames Smart 
35994c2805aaSJames Smart /**
36004c2805aaSJames Smart  * lpfc_nvmet_invalidate_host
36014c2805aaSJames Smart  *
3602d3cb79fcSLee Jones  * @phba: pointer to the driver instance bound to an adapter port.
3603d3cb79fcSLee Jones  * @ndlp: pointer to an lpfc_nodelist type
36044c2805aaSJames Smart  *
36054c2805aaSJames Smart  * This routine upcalls the nvmet transport to invalidate an NVME
36064c2805aaSJames Smart  * host to which this target instance had active connections.
36074c2805aaSJames Smart  */
36084c2805aaSJames Smart void
lpfc_nvmet_invalidate_host(struct lpfc_hba * phba,struct lpfc_nodelist * ndlp)36094c2805aaSJames Smart lpfc_nvmet_invalidate_host(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
36104c2805aaSJames Smart {
3611243156c0SJames Smart 	u32 ndlp_has_hh;
36124c2805aaSJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
36134c2805aaSJames Smart 
3614243156c0SJames Smart 	lpfc_printf_log(phba, KERN_INFO,
3615243156c0SJames Smart 			LOG_NVME | LOG_NVME_ABTS | LOG_NVME_DISC,
36164c2805aaSJames Smart 			"6203 Invalidating hosthandle x%px\n",
36174c2805aaSJames Smart 			ndlp);
36184c2805aaSJames Smart 
36194c2805aaSJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
36204c2805aaSJames Smart 	atomic_set(&tgtp->state, LPFC_NVMET_INV_HOST_ACTIVE);
36214c2805aaSJames Smart 
3622243156c0SJames Smart 	spin_lock_irq(&ndlp->lock);
3623243156c0SJames Smart 	ndlp_has_hh = ndlp->fc4_xpt_flags & NLP_XPT_HAS_HH;
3624243156c0SJames Smart 	spin_unlock_irq(&ndlp->lock);
3625243156c0SJames Smart 
3626243156c0SJames Smart 	/* Do not invalidate any nodes that do not have a hosthandle.
3627243156c0SJames Smart 	 * The host_release callbk will cause a node reference
3628243156c0SJames Smart 	 * count imbalance and a crash.
3629243156c0SJames Smart 	 */
3630243156c0SJames Smart 	if (!ndlp_has_hh) {
3631243156c0SJames Smart 		lpfc_printf_log(phba, KERN_INFO,
3632243156c0SJames Smart 				LOG_NVME | LOG_NVME_ABTS | LOG_NVME_DISC,
3633243156c0SJames Smart 				"6204 Skip invalidate on node x%px DID x%x\n",
3634243156c0SJames Smart 				ndlp, ndlp->nlp_DID);
3635243156c0SJames Smart 		return;
3636243156c0SJames Smart 	}
3637243156c0SJames Smart 
36384c2805aaSJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
36394c2805aaSJames Smart 	/* Need to get the nvmet_fc_target_port pointer here.*/
36404c2805aaSJames Smart 	nvmet_fc_invalidate_host(phba->targetport, ndlp);
36414c2805aaSJames Smart #endif
36424c2805aaSJames Smart }
3643