xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision a44e4e8b)
1d613b6a7SJames Smart /*******************************************************************
2d613b6a7SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
3d613b6a7SJames Smart  * Fibre Channsel Host Bus Adapters.                               *
4d080abe0SJames Smart  * Copyright (C) 2017 Broadcom. All Rights Reserved. The term      *
5d080abe0SJames Smart  * “Broadcom” refers to Broadcom Limited 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 <../drivers/nvme/host/nvme.h>
40d613b6a7SJames Smart #include <linux/nvme-fc-driver.h>
41d613b6a7SJames Smart 
42d613b6a7SJames Smart #include "lpfc_version.h"
43d613b6a7SJames Smart #include "lpfc_hw4.h"
44d613b6a7SJames Smart #include "lpfc_hw.h"
45d613b6a7SJames Smart #include "lpfc_sli.h"
46d613b6a7SJames Smart #include "lpfc_sli4.h"
47d613b6a7SJames Smart #include "lpfc_nl.h"
48d613b6a7SJames Smart #include "lpfc_disc.h"
49d613b6a7SJames Smart #include "lpfc.h"
50d613b6a7SJames Smart #include "lpfc_scsi.h"
51d613b6a7SJames Smart #include "lpfc_nvme.h"
52d613b6a7SJames Smart #include "lpfc_nvmet.h"
53d613b6a7SJames Smart #include "lpfc_logmsg.h"
54d613b6a7SJames Smart #include "lpfc_crtn.h"
55d613b6a7SJames Smart #include "lpfc_vport.h"
562b65e182SJames Smart #include "lpfc_debugfs.h"
57d613b6a7SJames Smart 
58d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *,
59d613b6a7SJames Smart 						 struct lpfc_nvmet_rcv_ctx *,
60d613b6a7SJames Smart 						 dma_addr_t rspbuf,
61d613b6a7SJames Smart 						 uint16_t rspsize);
62d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *,
63d613b6a7SJames Smart 						  struct lpfc_nvmet_rcv_ctx *);
64d613b6a7SJames Smart static int lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *,
65d613b6a7SJames Smart 					  struct lpfc_nvmet_rcv_ctx *,
66d613b6a7SJames Smart 					  uint32_t, uint16_t);
67d613b6a7SJames Smart static int lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *,
68d613b6a7SJames Smart 					    struct lpfc_nvmet_rcv_ctx *,
69d613b6a7SJames Smart 					    uint32_t, uint16_t);
70d613b6a7SJames Smart static int lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *,
71d613b6a7SJames Smart 					   struct lpfc_nvmet_rcv_ctx *,
72d613b6a7SJames Smart 					   uint32_t, uint16_t);
73d613b6a7SJames Smart 
74d613b6a7SJames Smart /**
75d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
76d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
77d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
78d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
79d613b6a7SJames Smart  *
80d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
81d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME LS commands
82d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
83d613b6a7SJames Smart  **/
84d613b6a7SJames Smart static void
85d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
86d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
87d613b6a7SJames Smart {
88d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
89d613b6a7SJames Smart 	struct nvmefc_tgt_ls_req *rsp;
90d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
91d613b6a7SJames Smart 	uint32_t status, result;
92d613b6a7SJames Smart 
93d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
94d613b6a7SJames Smart 	result = wcqe->parameter;
95d613b6a7SJames Smart 	if (!phba->targetport)
96d613b6a7SJames Smart 		goto out;
97d613b6a7SJames Smart 
98d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
99d613b6a7SJames Smart 
100d613b6a7SJames Smart 	if (status)
101d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_ls_rsp_error);
102d613b6a7SJames Smart 	else
103d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
104d613b6a7SJames Smart 
105d613b6a7SJames Smart out:
106d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
107d613b6a7SJames Smart 	rsp = &ctxp->ctx.ls_req;
108d613b6a7SJames Smart 
1092b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  CMPL: xri x%x stat x%x result x%x\n",
1102b65e182SJames Smart 			 ctxp->oxid, status, result);
1112b65e182SJames Smart 
112d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
113d613b6a7SJames Smart 			"6038 %s: Entrypoint: ctx %p status %x/%x\n", __func__,
114d613b6a7SJames Smart 			ctxp, status, result);
115d613b6a7SJames Smart 
116d613b6a7SJames Smart 	lpfc_nlp_put(cmdwqe->context1);
117d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
118d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
119d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
120d613b6a7SJames Smart 	rsp->done(rsp);
121d613b6a7SJames Smart 	kfree(ctxp);
122d613b6a7SJames Smart }
123d613b6a7SJames Smart 
124d613b6a7SJames Smart /**
125d613b6a7SJames Smart  * lpfc_nvmet_rq_post - Repost a NVMET RQ DMA buffer and clean up context
126d613b6a7SJames Smart  * @phba: HBA buffer is associated with
127d613b6a7SJames Smart  * @ctxp: context to clean up
128d613b6a7SJames Smart  * @mp: Buffer to free
129d613b6a7SJames Smart  *
130d613b6a7SJames Smart  * Description: Frees the given DMA buffer in the appropriate way given by
131d613b6a7SJames Smart  * reposting it to its associated RQ so it can be reused.
132d613b6a7SJames Smart  *
133d613b6a7SJames Smart  * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
134d613b6a7SJames Smart  *
135d613b6a7SJames Smart  * Returns: None
136d613b6a7SJames Smart  **/
137d613b6a7SJames Smart void
138d613b6a7SJames Smart lpfc_nvmet_rq_post(struct lpfc_hba *phba, struct lpfc_nvmet_rcv_ctx *ctxp,
139d613b6a7SJames Smart 		   struct lpfc_dmabuf *mp)
140d613b6a7SJames Smart {
141d613b6a7SJames Smart 	if (ctxp) {
142d613b6a7SJames Smart 		if (ctxp->txrdy) {
143d613b6a7SJames Smart 			pci_pool_free(phba->txrdy_payload_pool, ctxp->txrdy,
144d613b6a7SJames Smart 				      ctxp->txrdy_phys);
145d613b6a7SJames Smart 			ctxp->txrdy = NULL;
146d613b6a7SJames Smart 			ctxp->txrdy_phys = 0;
147d613b6a7SJames Smart 		}
148d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_FREE;
149d613b6a7SJames Smart 	}
150d613b6a7SJames Smart 	lpfc_rq_buf_free(phba, mp);
151d613b6a7SJames Smart }
152d613b6a7SJames Smart 
1532b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1542b65e182SJames Smart static void
1552b65e182SJames Smart lpfc_nvmet_ktime(struct lpfc_hba *phba,
1562b65e182SJames Smart 		 struct lpfc_nvmet_rcv_ctx *ctxp)
1572b65e182SJames Smart {
1582b65e182SJames Smart 	uint64_t seg1, seg2, seg3, seg4, seg5;
1592b65e182SJames Smart 	uint64_t seg6, seg7, seg8, seg9, seg10;
1602b65e182SJames Smart 
1612b65e182SJames Smart 	if (!phba->ktime_on)
1622b65e182SJames Smart 		return;
1632b65e182SJames Smart 
1642b65e182SJames Smart 	if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
1652b65e182SJames Smart 	    !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
1662b65e182SJames Smart 	    !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
1672b65e182SJames Smart 	    !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
1682b65e182SJames Smart 	    !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
1692b65e182SJames Smart 		return;
1702b65e182SJames Smart 
1712b65e182SJames Smart 	if (ctxp->ts_isr_cmd  > ctxp->ts_cmd_nvme)
1722b65e182SJames Smart 		return;
1732b65e182SJames Smart 	if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
1742b65e182SJames Smart 		return;
1752b65e182SJames Smart 	if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
1762b65e182SJames Smart 		return;
1772b65e182SJames Smart 	if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
1782b65e182SJames Smart 		return;
1792b65e182SJames Smart 	if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
1802b65e182SJames Smart 		return;
1812b65e182SJames Smart 	if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
1822b65e182SJames Smart 		return;
1832b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
1842b65e182SJames Smart 		return;
1852b65e182SJames Smart 	if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
1862b65e182SJames Smart 		return;
1872b65e182SJames Smart 	if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
1882b65e182SJames Smart 		return;
1892b65e182SJames Smart 	/*
1902b65e182SJames Smart 	 * Segment 1 - Time from FCP command received by MSI-X ISR
1912b65e182SJames Smart 	 * to FCP command is passed to NVME Layer.
1922b65e182SJames Smart 	 * Segment 2 - Time from FCP command payload handed
1932b65e182SJames Smart 	 * off to NVME Layer to Driver receives a Command op
1942b65e182SJames Smart 	 * from NVME Layer.
1952b65e182SJames Smart 	 * Segment 3 - Time from Driver receives a Command op
1962b65e182SJames Smart 	 * from NVME Layer to Command is put on WQ.
1972b65e182SJames Smart 	 * Segment 4 - Time from Driver WQ put is done
1982b65e182SJames Smart 	 * to MSI-X ISR for Command cmpl.
1992b65e182SJames Smart 	 * Segment 5 - Time from MSI-X ISR for Command cmpl to
2002b65e182SJames Smart 	 * Command cmpl is passed to NVME Layer.
2012b65e182SJames Smart 	 * Segment 6 - Time from Command cmpl is passed to NVME
2022b65e182SJames Smart 	 * Layer to Driver receives a RSP op from NVME Layer.
2032b65e182SJames Smart 	 * Segment 7 - Time from Driver receives a RSP op from
2042b65e182SJames Smart 	 * NVME Layer to WQ put is done on TRSP FCP Status.
2052b65e182SJames Smart 	 * Segment 8 - Time from Driver WQ put is done on TRSP
2062b65e182SJames Smart 	 * FCP Status to MSI-X ISR for TRSP cmpl.
2072b65e182SJames Smart 	 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
2082b65e182SJames Smart 	 * TRSP cmpl is passed to NVME Layer.
2092b65e182SJames Smart 	 * Segment 10 - Time from FCP command received by
2102b65e182SJames Smart 	 * MSI-X ISR to command is completed on wire.
2112b65e182SJames Smart 	 * (Segments 1 thru 8) for READDATA / WRITEDATA
2122b65e182SJames Smart 	 * (Segments 1 thru 4) for READDATA_RSP
2132b65e182SJames Smart 	 */
2142b65e182SJames Smart 	seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
2152b65e182SJames Smart 	seg2 = (ctxp->ts_nvme_data - ctxp->ts_isr_cmd) - seg1;
2162b65e182SJames Smart 	seg3 = (ctxp->ts_data_wqput - ctxp->ts_isr_cmd) -
2172b65e182SJames Smart 		seg1 - seg2;
2182b65e182SJames Smart 	seg4 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd) -
2192b65e182SJames Smart 		seg1 - seg2 - seg3;
2202b65e182SJames Smart 	seg5 = (ctxp->ts_data_nvme - ctxp->ts_isr_cmd) -
2212b65e182SJames Smart 		seg1 - seg2 - seg3 - seg4;
2222b65e182SJames Smart 
2232b65e182SJames Smart 	/* For auto rsp commands seg6 thru seg10 will be 0 */
2242b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
2252b65e182SJames Smart 		seg6 = (ctxp->ts_nvme_status -
2262b65e182SJames Smart 			ctxp->ts_isr_cmd) -
2272b65e182SJames Smart 			seg1 - seg2 - seg3 - seg4 - seg5;
2282b65e182SJames Smart 		seg7 = (ctxp->ts_status_wqput -
2292b65e182SJames Smart 			ctxp->ts_isr_cmd) -
2302b65e182SJames Smart 			seg1 - seg2 - seg3 -
2312b65e182SJames Smart 			seg4 - seg5 - seg6;
2322b65e182SJames Smart 		seg8 = (ctxp->ts_isr_status -
2332b65e182SJames Smart 			ctxp->ts_isr_cmd) -
2342b65e182SJames Smart 			seg1 - seg2 - seg3 - seg4 -
2352b65e182SJames Smart 			seg5 - seg6 - seg7;
2362b65e182SJames Smart 		seg9 = (ctxp->ts_status_nvme -
2372b65e182SJames Smart 			ctxp->ts_isr_cmd) -
2382b65e182SJames Smart 			seg1 - seg2 - seg3 - seg4 -
2392b65e182SJames Smart 			seg5 - seg6 - seg7 - seg8;
2402b65e182SJames Smart 		seg10 = (ctxp->ts_isr_status -
2412b65e182SJames Smart 			ctxp->ts_isr_cmd);
2422b65e182SJames Smart 	} else {
2432b65e182SJames Smart 		seg6 =  0;
2442b65e182SJames Smart 		seg7 =  0;
2452b65e182SJames Smart 		seg8 =  0;
2462b65e182SJames Smart 		seg9 =  0;
2472b65e182SJames Smart 		seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
2482b65e182SJames Smart 	}
2492b65e182SJames Smart 
2502b65e182SJames Smart 	phba->ktime_seg1_total += seg1;
2512b65e182SJames Smart 	if (seg1 < phba->ktime_seg1_min)
2522b65e182SJames Smart 		phba->ktime_seg1_min = seg1;
2532b65e182SJames Smart 	else if (seg1 > phba->ktime_seg1_max)
2542b65e182SJames Smart 		phba->ktime_seg1_max = seg1;
2552b65e182SJames Smart 
2562b65e182SJames Smart 	phba->ktime_seg2_total += seg2;
2572b65e182SJames Smart 	if (seg2 < phba->ktime_seg2_min)
2582b65e182SJames Smart 		phba->ktime_seg2_min = seg2;
2592b65e182SJames Smart 	else if (seg2 > phba->ktime_seg2_max)
2602b65e182SJames Smart 		phba->ktime_seg2_max = seg2;
2612b65e182SJames Smart 
2622b65e182SJames Smart 	phba->ktime_seg3_total += seg3;
2632b65e182SJames Smart 	if (seg3 < phba->ktime_seg3_min)
2642b65e182SJames Smart 		phba->ktime_seg3_min = seg3;
2652b65e182SJames Smart 	else if (seg3 > phba->ktime_seg3_max)
2662b65e182SJames Smart 		phba->ktime_seg3_max = seg3;
2672b65e182SJames Smart 
2682b65e182SJames Smart 	phba->ktime_seg4_total += seg4;
2692b65e182SJames Smart 	if (seg4 < phba->ktime_seg4_min)
2702b65e182SJames Smart 		phba->ktime_seg4_min = seg4;
2712b65e182SJames Smart 	else if (seg4 > phba->ktime_seg4_max)
2722b65e182SJames Smart 		phba->ktime_seg4_max = seg4;
2732b65e182SJames Smart 
2742b65e182SJames Smart 	phba->ktime_seg5_total += seg5;
2752b65e182SJames Smart 	if (seg5 < phba->ktime_seg5_min)
2762b65e182SJames Smart 		phba->ktime_seg5_min = seg5;
2772b65e182SJames Smart 	else if (seg5 > phba->ktime_seg5_max)
2782b65e182SJames Smart 		phba->ktime_seg5_max = seg5;
2792b65e182SJames Smart 
2802b65e182SJames Smart 	phba->ktime_data_samples++;
2812b65e182SJames Smart 	if (!seg6)
2822b65e182SJames Smart 		goto out;
2832b65e182SJames Smart 
2842b65e182SJames Smart 	phba->ktime_seg6_total += seg6;
2852b65e182SJames Smart 	if (seg6 < phba->ktime_seg6_min)
2862b65e182SJames Smart 		phba->ktime_seg6_min = seg6;
2872b65e182SJames Smart 	else if (seg6 > phba->ktime_seg6_max)
2882b65e182SJames Smart 		phba->ktime_seg6_max = seg6;
2892b65e182SJames Smart 
2902b65e182SJames Smart 	phba->ktime_seg7_total += seg7;
2912b65e182SJames Smart 	if (seg7 < phba->ktime_seg7_min)
2922b65e182SJames Smart 		phba->ktime_seg7_min = seg7;
2932b65e182SJames Smart 	else if (seg7 > phba->ktime_seg7_max)
2942b65e182SJames Smart 		phba->ktime_seg7_max = seg7;
2952b65e182SJames Smart 
2962b65e182SJames Smart 	phba->ktime_seg8_total += seg8;
2972b65e182SJames Smart 	if (seg8 < phba->ktime_seg8_min)
2982b65e182SJames Smart 		phba->ktime_seg8_min = seg8;
2992b65e182SJames Smart 	else if (seg8 > phba->ktime_seg8_max)
3002b65e182SJames Smart 		phba->ktime_seg8_max = seg8;
3012b65e182SJames Smart 
3022b65e182SJames Smart 	phba->ktime_seg9_total += seg9;
3032b65e182SJames Smart 	if (seg9 < phba->ktime_seg9_min)
3042b65e182SJames Smart 		phba->ktime_seg9_min = seg9;
3052b65e182SJames Smart 	else if (seg9 > phba->ktime_seg9_max)
3062b65e182SJames Smart 		phba->ktime_seg9_max = seg9;
3072b65e182SJames Smart out:
3082b65e182SJames Smart 	phba->ktime_seg10_total += seg10;
3092b65e182SJames Smart 	if (seg10 < phba->ktime_seg10_min)
3102b65e182SJames Smart 		phba->ktime_seg10_min = seg10;
3112b65e182SJames Smart 	else if (seg10 > phba->ktime_seg10_max)
3122b65e182SJames Smart 		phba->ktime_seg10_max = seg10;
3132b65e182SJames Smart 	phba->ktime_status_samples++;
3142b65e182SJames Smart }
3152b65e182SJames Smart #endif
3162b65e182SJames Smart 
317d613b6a7SJames Smart /**
318d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
319d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
320d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
321d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
322d613b6a7SJames Smart  *
323d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
324d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME FCP commands
325d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
326d613b6a7SJames Smart  **/
327d613b6a7SJames Smart static void
328d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
329d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
330d613b6a7SJames Smart {
331d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
332d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
333d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
334d613b6a7SJames Smart 	uint32_t status, result, op, start_clean;
3352b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3362b65e182SJames Smart 	uint32_t id;
3372b65e182SJames Smart #endif
338d613b6a7SJames Smart 
339d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
340d613b6a7SJames Smart 	rsp = &ctxp->ctx.fcp_req;
341d613b6a7SJames Smart 	op = rsp->op;
342d613b6a7SJames Smart 	ctxp->flag &= ~LPFC_NVMET_IO_INP;
343d613b6a7SJames Smart 
344d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
345d613b6a7SJames Smart 	result = wcqe->parameter;
346d613b6a7SJames Smart 
347d613b6a7SJames Smart 	if (!phba->targetport)
348d613b6a7SJames Smart 		goto out;
349d613b6a7SJames Smart 
3502b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
3512b65e182SJames Smart 			 ctxp->oxid, op, status);
3522b65e182SJames Smart 
353d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
354d613b6a7SJames Smart 	if (status) {
355d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
356d613b6a7SJames Smart 		rsp->transferred_length = 0;
357d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp_error);
358d613b6a7SJames Smart 	} else {
359d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_SUCCESS;
360d613b6a7SJames Smart 		if (op == NVMET_FCOP_RSP)
361d613b6a7SJames Smart 			rsp->transferred_length = rsp->rsplen;
362d613b6a7SJames Smart 		else
363d613b6a7SJames Smart 			rsp->transferred_length = rsp->transfer_length;
364d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
365d613b6a7SJames Smart 	}
366d613b6a7SJames Smart 
367d613b6a7SJames Smart out:
368d613b6a7SJames Smart 	if ((op == NVMET_FCOP_READDATA_RSP) ||
369d613b6a7SJames Smart 	    (op == NVMET_FCOP_RSP)) {
370d613b6a7SJames Smart 		/* Sanity check */
371d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DONE;
372d613b6a7SJames Smart 		ctxp->entry_cnt++;
3732b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3742b65e182SJames Smart 		if (phba->ktime_on) {
3752b65e182SJames Smart 			if (rsp->op == NVMET_FCOP_READDATA_RSP) {
3762b65e182SJames Smart 				ctxp->ts_isr_data =
3772b65e182SJames Smart 					cmdwqe->isr_timestamp;
3782b65e182SJames Smart 				ctxp->ts_data_nvme =
3792b65e182SJames Smart 					ktime_get_ns();
3802b65e182SJames Smart 				ctxp->ts_nvme_status =
3812b65e182SJames Smart 					ctxp->ts_data_nvme;
3822b65e182SJames Smart 				ctxp->ts_status_wqput =
3832b65e182SJames Smart 					ctxp->ts_data_nvme;
3842b65e182SJames Smart 				ctxp->ts_isr_status =
3852b65e182SJames Smart 					ctxp->ts_data_nvme;
3862b65e182SJames Smart 				ctxp->ts_status_nvme =
3872b65e182SJames Smart 					ctxp->ts_data_nvme;
3882b65e182SJames Smart 			} else {
3892b65e182SJames Smart 				ctxp->ts_isr_status =
3902b65e182SJames Smart 					cmdwqe->isr_timestamp;
3912b65e182SJames Smart 				ctxp->ts_status_nvme =
3922b65e182SJames Smart 					ktime_get_ns();
3932b65e182SJames Smart 			}
3942b65e182SJames Smart 		}
3952b65e182SJames Smart 		if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
3962b65e182SJames Smart 			id = smp_processor_id();
3972b65e182SJames Smart 			if (ctxp->cpu != id)
3982b65e182SJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3992b65e182SJames Smart 						"6703 CPU Check cmpl: "
4002b65e182SJames Smart 						"cpu %d expect %d\n",
4012b65e182SJames Smart 						id, ctxp->cpu);
4022b65e182SJames Smart 			if (ctxp->cpu < LPFC_CHECK_CPU_CNT)
4032b65e182SJames Smart 				phba->cpucheck_cmpl_io[id]++;
4042b65e182SJames Smart 		}
4052b65e182SJames Smart #endif
406d613b6a7SJames Smart 		rsp->done(rsp);
4072b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4082b65e182SJames Smart 		if (phba->ktime_on)
4092b65e182SJames Smart 			lpfc_nvmet_ktime(phba, ctxp);
4102b65e182SJames Smart #endif
41119b58d94SJames Smart 		/* lpfc_nvmet_xmt_fcp_release() will recycle the context */
412d613b6a7SJames Smart 	} else {
413d613b6a7SJames Smart 		ctxp->entry_cnt++;
414d613b6a7SJames Smart 		start_clean = offsetof(struct lpfc_iocbq, wqe);
415d613b6a7SJames Smart 		memset(((char *)cmdwqe) + start_clean, 0,
416d613b6a7SJames Smart 		       (sizeof(struct lpfc_iocbq) - start_clean));
4172b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4182b65e182SJames Smart 		if (phba->ktime_on) {
4192b65e182SJames Smart 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
4202b65e182SJames Smart 			ctxp->ts_data_nvme = ktime_get_ns();
4212b65e182SJames Smart 		}
4222b65e182SJames Smart 		if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
4232b65e182SJames Smart 			id = smp_processor_id();
4242b65e182SJames Smart 			if (ctxp->cpu != id)
4252b65e182SJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
4262b65e182SJames Smart 						"6704 CPU Check cmdcmpl: "
4272b65e182SJames Smart 						"cpu %d expect %d\n",
4282b65e182SJames Smart 						id, ctxp->cpu);
4292b65e182SJames Smart 			if (ctxp->cpu < LPFC_CHECK_CPU_CNT)
4302b65e182SJames Smart 				phba->cpucheck_ccmpl_io[id]++;
4312b65e182SJames Smart 		}
4322b65e182SJames Smart #endif
433d613b6a7SJames Smart 		rsp->done(rsp);
434d613b6a7SJames Smart 	}
435d613b6a7SJames Smart }
436d613b6a7SJames Smart 
437d613b6a7SJames Smart static int
438d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
439d613b6a7SJames Smart 		      struct nvmefc_tgt_ls_req *rsp)
440d613b6a7SJames Smart {
441d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
442d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.ls_req);
443d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
444d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf =
445d613b6a7SJames Smart 		(struct hbq_dmabuf *)ctxp->rqb_buffer;
446d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
447d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
448d613b6a7SJames Smart 	struct lpfc_dmabuf dmabuf;
449d613b6a7SJames Smart 	struct ulp_bde64 bpl;
450d613b6a7SJames Smart 	int rc;
451d613b6a7SJames Smart 
452d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
453d613b6a7SJames Smart 			"6023 %s: Entrypoint ctx %p %p\n", __func__,
454d613b6a7SJames Smart 			ctxp, tgtport);
455d613b6a7SJames Smart 
456d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, ctxp, rsp->rspdma,
457d613b6a7SJames Smart 				      rsp->rsplen);
458d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
459d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_drop);
460d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
461d613b6a7SJames Smart 				"6150 LS Drop IO x%x: Prep\n",
462d613b6a7SJames Smart 				ctxp->oxid);
463d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
464d613b6a7SJames Smart 		lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp,
465d613b6a7SJames Smart 						ctxp->sid, ctxp->oxid);
466d613b6a7SJames Smart 		return -ENOMEM;
467d613b6a7SJames Smart 	}
468d613b6a7SJames Smart 
469d613b6a7SJames Smart 	/* Save numBdes for bpl2sgl */
470d613b6a7SJames Smart 	nvmewqeq->rsvd2 = 1;
471d613b6a7SJames Smart 	nvmewqeq->hba_wqidx = 0;
472d613b6a7SJames Smart 	nvmewqeq->context3 = &dmabuf;
473d613b6a7SJames Smart 	dmabuf.virt = &bpl;
474d613b6a7SJames Smart 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
475d613b6a7SJames Smart 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
476d613b6a7SJames Smart 	bpl.tus.f.bdeSize = rsp->rsplen;
477d613b6a7SJames Smart 	bpl.tus.f.bdeFlags = 0;
478d613b6a7SJames Smart 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
479d613b6a7SJames Smart 
480d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_rsp_cmp;
481d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
482d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
483d613b6a7SJames Smart 
4842b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  RESP: xri x%x wqidx x%x len x%x\n",
4852b65e182SJames Smart 			 ctxp->oxid, nvmewqeq->hba_wqidx, rsp->rsplen);
4862b65e182SJames Smart 
487d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, nvmewqeq);
488d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
489d613b6a7SJames Smart 		/*
490d613b6a7SJames Smart 		 * Okay to repost buffer here, but wait till cmpl
491d613b6a7SJames Smart 		 * before freeing ctxp and iocbq.
492d613b6a7SJames Smart 		 */
493d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
494d613b6a7SJames Smart 		ctxp->rqb_buffer = 0;
495d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_rsp);
496d613b6a7SJames Smart 		return 0;
497d613b6a7SJames Smart 	}
498d613b6a7SJames Smart 	/* Give back resources */
499d613b6a7SJames Smart 	atomic_inc(&nvmep->xmt_ls_drop);
500d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
501d613b6a7SJames Smart 			"6151 LS Drop IO x%x: Issue %d\n",
502d613b6a7SJames Smart 			ctxp->oxid, rc);
503d613b6a7SJames Smart 
504d613b6a7SJames Smart 	lpfc_nlp_put(nvmewqeq->context1);
505d613b6a7SJames Smart 
506d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
507d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
508d613b6a7SJames Smart 	return -ENXIO;
509d613b6a7SJames Smart }
510d613b6a7SJames Smart 
511d613b6a7SJames Smart static int
512d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
513d613b6a7SJames Smart 		      struct nvmefc_tgt_fcp_req *rsp)
514d613b6a7SJames Smart {
515d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
516d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
517d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
518d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
519d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
520d613b6a7SJames Smart 	unsigned long iflags;
5212b65e182SJames Smart 	int rc, id;
5222b65e182SJames Smart 
5232b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5242b65e182SJames Smart 	if (phba->ktime_on) {
5252b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
5262b65e182SJames Smart 			ctxp->ts_nvme_status = ktime_get_ns();
5272b65e182SJames Smart 		else
5282b65e182SJames Smart 			ctxp->ts_nvme_data = ktime_get_ns();
5292b65e182SJames Smart 	}
5302b65e182SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
5312b65e182SJames Smart 		id = smp_processor_id();
5322b65e182SJames Smart 		ctxp->cpu = id;
5332b65e182SJames Smart 		if (id < LPFC_CHECK_CPU_CNT)
5342b65e182SJames Smart 			phba->cpucheck_xmt_io[id]++;
5352b65e182SJames Smart 		if (rsp->hwqid != id) {
5362b65e182SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
5372b65e182SJames Smart 					"6705 CPU Check OP: "
5382b65e182SJames Smart 					"cpu %d expect %d\n",
5392b65e182SJames Smart 					id, rsp->hwqid);
5402b65e182SJames Smart 			ctxp->cpu = rsp->hwqid;
5412b65e182SJames Smart 		}
5422b65e182SJames Smart 	}
5432b65e182SJames Smart #endif
544d613b6a7SJames Smart 
545d613b6a7SJames Smart 	/* Sanity check */
546d613b6a7SJames Smart 	if (ctxp->state == LPFC_NVMET_STE_ABORT) {
547d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
548d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
549d613b6a7SJames Smart 				"6102 Bad state IO x%x aborted\n",
550d613b6a7SJames Smart 				ctxp->oxid);
551a5068b46SJames Smart 		rc = -ENXIO;
552d613b6a7SJames Smart 		goto aerr;
553d613b6a7SJames Smart 	}
554d613b6a7SJames Smart 
555d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
556d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
557d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
558d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
559d613b6a7SJames Smart 				"6152 FCP Drop IO x%x: Prep\n",
560d613b6a7SJames Smart 				ctxp->oxid);
561a5068b46SJames Smart 		rc = -ENXIO;
562d613b6a7SJames Smart 		goto aerr;
563d613b6a7SJames Smart 	}
564d613b6a7SJames Smart 
565d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
566d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
567d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
568d613b6a7SJames Smart 	nvmewqeq->iocb_flag |=  LPFC_IO_NVMET;
569d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
570d613b6a7SJames Smart 
5712b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
5722b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
5732b65e182SJames Smart 
574d613b6a7SJames Smart 	/* For now we take hbalock */
575d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflags);
576d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, nvmewqeq);
577d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflags);
578d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
579d613b6a7SJames Smart 		ctxp->flag |= LPFC_NVMET_IO_INP;
5802b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5812b65e182SJames Smart 		if (!phba->ktime_on)
5822b65e182SJames Smart 			return 0;
5832b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
5842b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
5852b65e182SJames Smart 		else
5862b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
5872b65e182SJames Smart #endif
588d613b6a7SJames Smart 		return 0;
589d613b6a7SJames Smart 	}
590d613b6a7SJames Smart 
591d613b6a7SJames Smart 	/* Give back resources */
592d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
593d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
594d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
595d613b6a7SJames Smart 			ctxp->oxid, rc);
596d613b6a7SJames Smart 
597d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
598d613b6a7SJames Smart 	nvmewqeq->context2 = NULL;
599d613b6a7SJames Smart 	nvmewqeq->context3 = NULL;
600a5068b46SJames Smart 	rc = -EBUSY;
601d613b6a7SJames Smart aerr:
602a5068b46SJames Smart 	return rc;
603d613b6a7SJames Smart }
604d613b6a7SJames Smart 
605d613b6a7SJames Smart static void
606d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
607d613b6a7SJames Smart {
608d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
609d613b6a7SJames Smart 
610d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
611d613b6a7SJames Smart 	complete(&tport->tport_unreg_done);
612d613b6a7SJames Smart }
613d613b6a7SJames Smart 
61419b58d94SJames Smart static void
615a97ec51bSJames Smart lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
616a97ec51bSJames Smart 			 struct nvmefc_tgt_fcp_req *req)
617a97ec51bSJames Smart {
618a97ec51bSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
619a97ec51bSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
620a97ec51bSJames Smart 		container_of(req, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
621a97ec51bSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
622a97ec51bSJames Smart 
623a97ec51bSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
624a97ec51bSJames Smart 			"6103 Abort op: oxri x%x %d cnt %d\n",
625a97ec51bSJames Smart 			ctxp->oxid, ctxp->state, ctxp->entry_cnt);
626a97ec51bSJames Smart 
627a97ec51bSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x state x%x cnt x%x\n",
628a97ec51bSJames Smart 			 ctxp->oxid, ctxp->state, ctxp->entry_cnt);
629a97ec51bSJames Smart 
630a97ec51bSJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
631a97ec51bSJames Smart 	ctxp->entry_cnt++;
632a97ec51bSJames Smart 	ctxp->flag |= LPFC_NVMET_ABORT_OP;
633a97ec51bSJames Smart 	if (ctxp->flag & LPFC_NVMET_IO_INP)
634a97ec51bSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
635a97ec51bSJames Smart 					       ctxp->oxid);
636a97ec51bSJames Smart 	else
637a97ec51bSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
638a97ec51bSJames Smart 						 ctxp->oxid);
639a97ec51bSJames Smart }
640a97ec51bSJames Smart 
641a97ec51bSJames Smart static void
64219b58d94SJames Smart lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
64319b58d94SJames Smart 			   struct nvmefc_tgt_fcp_req *rsp)
64419b58d94SJames Smart {
64519b58d94SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
64619b58d94SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
64719b58d94SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
64819b58d94SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
64919b58d94SJames Smart 	unsigned long flags;
65019b58d94SJames Smart 	bool aborting = false;
65119b58d94SJames Smart 
65219b58d94SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
65319b58d94SJames Smart 	if (ctxp->flag & LPFC_NVMET_ABORT_OP) {
65419b58d94SJames Smart 		aborting = true;
65519b58d94SJames Smart 		ctxp->flag |= LPFC_NVMET_CTX_RLS;
65619b58d94SJames Smart 	}
65719b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
65819b58d94SJames Smart 
65919b58d94SJames Smart 	if (aborting)
66019b58d94SJames Smart 		/* let the abort path do the real release */
66119b58d94SJames Smart 		return;
66219b58d94SJames Smart 
66319b58d94SJames Smart 	/* Sanity check */
66419b58d94SJames Smart 	if (ctxp->state != LPFC_NVMET_STE_DONE) {
66519b58d94SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
66619b58d94SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
66719b58d94SJames Smart 				"6117 Bad state IO x%x aborted\n",
66819b58d94SJames Smart 				ctxp->oxid);
66919b58d94SJames Smart 	}
67019b58d94SJames Smart 
67119b58d94SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d\n", ctxp->oxid,
67219b58d94SJames Smart 			 ctxp->state, 0);
67319b58d94SJames Smart 
67419b58d94SJames Smart 	lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
67519b58d94SJames Smart }
67619b58d94SJames Smart 
677d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
678d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
679d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
680d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
681a97ec51bSJames Smart 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
68219b58d94SJames Smart 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
683d613b6a7SJames Smart 
684d613b6a7SJames Smart 	.max_hw_queues  = 1,
685d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
686d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
687d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
688d613b6a7SJames Smart 
689d613b6a7SJames Smart 	/* optional features */
690d613b6a7SJames Smart 	.target_features = 0,
691d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
692d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
693d613b6a7SJames Smart };
694d613b6a7SJames Smart 
695d613b6a7SJames Smart int
696d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
697d613b6a7SJames Smart {
698d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
699d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
700d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
701d613b6a7SJames Smart 	int error = 0;
702d613b6a7SJames Smart 
703d613b6a7SJames Smart 	if (phba->targetport)
704d613b6a7SJames Smart 		return 0;
705d613b6a7SJames Smart 
706d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
707d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
708d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
709d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
710d613b6a7SJames Smart 
711d613b6a7SJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_nvme_io_channel;
712a44e4e8bSJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_sg_seg_cnt + 1;
713d613b6a7SJames Smart 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP |
71439498faeSJames Smart 					   NVMET_FCTGTFEAT_NEEDS_CMD_CPUSCHED |
71539498faeSJames Smart 					   NVMET_FCTGTFEAT_CMD_IN_ISR |
71639498faeSJames Smart 					   NVMET_FCTGTFEAT_OPDONE_IN_ISR;
717d613b6a7SJames Smart 
7187d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
719d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
720d613b6a7SJames Smart 					     &phba->pcidev->dev,
721d613b6a7SJames Smart 					     &phba->targetport);
722166d7211SJames Smart #else
723166d7211SJames Smart 	error = -ENOMEM;
724166d7211SJames Smart #endif
725d613b6a7SJames Smart 	if (error) {
726d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
727d613b6a7SJames Smart 				"6025 Cannot register NVME targetport "
728d613b6a7SJames Smart 				"x%x\n", error);
729d613b6a7SJames Smart 		phba->targetport = NULL;
730d613b6a7SJames Smart 	} else {
731d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
732d613b6a7SJames Smart 			phba->targetport->private;
733d613b6a7SJames Smart 		tgtp->phba = phba;
734d613b6a7SJames Smart 
735d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
736d613b6a7SJames Smart 				"6026 Registered NVME "
737d613b6a7SJames Smart 				"targetport: %p, private %p "
738d613b6a7SJames Smart 				"portnm %llx nodenm %llx\n",
739d613b6a7SJames Smart 				phba->targetport, tgtp,
740d613b6a7SJames Smart 				pinfo.port_name, pinfo.node_name);
741d613b6a7SJames Smart 
742d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
743d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
744d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
745d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
746d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
747d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
748d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
749d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
750d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
751d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
752d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
753d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
754d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
755d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
756d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
757d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
758d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
759d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
760d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
761d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
762d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
763d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
764d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_cmpl, 0);
765d613b6a7SJames Smart 	}
766d613b6a7SJames Smart 	return error;
767d613b6a7SJames Smart }
768d613b6a7SJames Smart 
769d613b6a7SJames Smart int
770d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
771d613b6a7SJames Smart {
772d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
773d613b6a7SJames Smart 
774d613b6a7SJames Smart 	if (!phba->targetport)
775d613b6a7SJames Smart 		return 0;
776d613b6a7SJames Smart 
777d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
778d613b6a7SJames Smart 			 "6007 Update NVMET port %p did x%x\n",
779d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
780d613b6a7SJames Smart 
781d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
782d613b6a7SJames Smart 	return 0;
783d613b6a7SJames Smart }
784d613b6a7SJames Smart 
785318083adSJames Smart /**
786318083adSJames Smart  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
787318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
788318083adSJames Smart  * @axri: pointer to the nvmet xri abort wcqe structure.
789318083adSJames Smart  *
790318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
791318083adSJames Smart  * NVMET aborted xri.
792318083adSJames Smart  **/
793318083adSJames Smart void
794318083adSJames Smart lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
795318083adSJames Smart 			    struct sli4_wcqe_xri_aborted *axri)
796318083adSJames Smart {
797318083adSJames Smart 	/* TODO: work in progress */
798318083adSJames Smart }
799318083adSJames Smart 
800d613b6a7SJames Smart void
801d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
802d613b6a7SJames Smart {
8037d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
804d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
805d613b6a7SJames Smart 
806d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
807d613b6a7SJames Smart 		return;
808d613b6a7SJames Smart 	if (phba->targetport) {
809d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
810d613b6a7SJames Smart 		init_completion(&tgtp->tport_unreg_done);
811d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
812d613b6a7SJames Smart 		wait_for_completion_timeout(&tgtp->tport_unreg_done, 5);
813d613b6a7SJames Smart 	}
814d613b6a7SJames Smart 	phba->targetport = NULL;
815166d7211SJames Smart #endif
816d613b6a7SJames Smart }
817d613b6a7SJames Smart 
818d613b6a7SJames Smart /**
819d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer - Process an unsolicited event data buffer
820d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
821d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
822d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
823d613b6a7SJames Smart  *
824d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
825d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
826d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
827d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
828d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
829d613b6a7SJames Smart  * of the discovery state machine.
830d613b6a7SJames Smart  **/
831d613b6a7SJames Smart static void
832d613b6a7SJames Smart lpfc_nvmet_unsol_ls_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
833d613b6a7SJames Smart 			   struct hbq_dmabuf *nvmebuf)
834d613b6a7SJames Smart {
8357d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
836d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
837d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
838d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
839d613b6a7SJames Smart 	uint32_t *payload;
840d613b6a7SJames Smart 	uint32_t size, oxid, sid, rc;
841d613b6a7SJames Smart 
842d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
843d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
844d613b6a7SJames Smart 				"6154 LS Drop IO\n");
8452b65e182SJames Smart 		oxid = 0;
8462b65e182SJames Smart 		size = 0;
8472b65e182SJames Smart 		sid = 0;
848d613b6a7SJames Smart 		goto dropit;
849d613b6a7SJames Smart 	}
850d613b6a7SJames Smart 
851d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
852d613b6a7SJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
853d613b6a7SJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
854d613b6a7SJames Smart 	size = bf_get(lpfc_rcqe_length,  &nvmebuf->cq_event.cqe.rcqe_cmpl);
855d613b6a7SJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
856d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
857d613b6a7SJames Smart 
858d613b6a7SJames Smart 	ctxp = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx), GFP_ATOMIC);
859d613b6a7SJames Smart 	if (ctxp == NULL) {
860d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_drop);
861d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
862d613b6a7SJames Smart 				"6155 LS Drop IO x%x: Alloc\n",
863d613b6a7SJames Smart 				oxid);
864d613b6a7SJames Smart dropit:
8652b65e182SJames Smart 		lpfc_nvmeio_data(phba, "NVMET LS  DROP: "
8662b65e182SJames Smart 				 "xri x%x sz %d from %06x\n",
8672b65e182SJames Smart 				 oxid, size, sid);
868d613b6a7SJames Smart 		if (nvmebuf)
869d613b6a7SJames Smart 			lpfc_in_buf_free(phba, &nvmebuf->dbuf);
870d613b6a7SJames Smart 		return;
871d613b6a7SJames Smart 	}
872d613b6a7SJames Smart 	ctxp->phba = phba;
873d613b6a7SJames Smart 	ctxp->size = size;
874d613b6a7SJames Smart 	ctxp->oxid = oxid;
875d613b6a7SJames Smart 	ctxp->sid = sid;
876d613b6a7SJames Smart 	ctxp->wqeq = NULL;
877d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_RCV;
878d613b6a7SJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
87919b58d94SJames Smart 	spin_lock_init(&ctxp->ctxlock);
8802b65e182SJames Smart 
8812b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS   RCV: xri x%x sz %d from %06x\n",
8822b65e182SJames Smart 			 oxid, size, sid);
883d613b6a7SJames Smart 	/*
884d613b6a7SJames Smart 	 * The calling sequence should be:
885d613b6a7SJames Smart 	 * nvmet_fc_rcv_ls_req -> lpfc_nvmet_xmt_ls_rsp/cmp ->_req->done
886d613b6a7SJames Smart 	 * lpfc_nvmet_xmt_ls_rsp_cmp should free the allocated ctxp.
887d613b6a7SJames Smart 	 */
888d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
889d613b6a7SJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, &ctxp->ctx.ls_req,
890d613b6a7SJames Smart 				 payload, size);
891d613b6a7SJames Smart 
892d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
893d613b6a7SJames Smart 			"6037 %s: ctx %p sz %d rc %d: %08x %08x %08x "
894d613b6a7SJames Smart 			"%08x %08x %08x\n", __func__, ctxp, size, rc,
895d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
896d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
8972b65e182SJames Smart 
898d613b6a7SJames Smart 	if (rc == 0) {
899d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
900d613b6a7SJames Smart 		return;
901d613b6a7SJames Smart 	}
9022b65e182SJames Smart 
9032b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  DROP: xri x%x sz %d from %06x\n",
9042b65e182SJames Smart 			 oxid, size, sid);
9052b65e182SJames Smart 
906d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
907d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
908d613b6a7SJames Smart 			"6156 LS Drop IO x%x: nvmet_fc_rcv_ls_req %d\n",
909d613b6a7SJames Smart 			ctxp->oxid, rc);
910d613b6a7SJames Smart 
911d613b6a7SJames Smart 	/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
912d613b6a7SJames Smart 	if (nvmebuf)
913d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
914d613b6a7SJames Smart 
915d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_ls_abort);
916d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, sid, oxid);
917166d7211SJames Smart #endif
918d613b6a7SJames Smart }
919d613b6a7SJames Smart 
920d613b6a7SJames Smart /**
921d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
922d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
923d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
924d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
925d613b6a7SJames Smart  *
926d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
927d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
928d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
929d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
930d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
931d613b6a7SJames Smart  * of the discovery state machine.
932d613b6a7SJames Smart  **/
933d613b6a7SJames Smart static void
934d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
935d613b6a7SJames Smart 			    struct lpfc_sli_ring *pring,
936d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
937d613b6a7SJames Smart 			    uint64_t isr_timestamp)
938d613b6a7SJames Smart {
9397d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
940d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
941d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
942d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
943d613b6a7SJames Smart 	uint32_t *payload;
944d613b6a7SJames Smart 	uint32_t size, oxid, sid, rc;
9452b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
9462b65e182SJames Smart 	uint32_t id;
9472b65e182SJames Smart #endif
948d613b6a7SJames Smart 
949d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
950d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
951d613b6a7SJames Smart 				"6157 FCP Drop IO\n");
9522b65e182SJames Smart 		oxid = 0;
9532b65e182SJames Smart 		size = 0;
9542b65e182SJames Smart 		sid = 0;
955d613b6a7SJames Smart 		goto dropit;
956d613b6a7SJames Smart 	}
957d613b6a7SJames Smart 
958d613b6a7SJames Smart 
959d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
960d613b6a7SJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
961d613b6a7SJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
962d613b6a7SJames Smart 	size = nvmebuf->bytes_recv;
963d613b6a7SJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
964d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
965d613b6a7SJames Smart 
966d613b6a7SJames Smart 	ctxp = (struct lpfc_nvmet_rcv_ctx *)nvmebuf->context;
967d613b6a7SJames Smart 	if (ctxp == NULL) {
968d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
969d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
970d613b6a7SJames Smart 				"6158 FCP Drop IO x%x: Alloc\n",
971d613b6a7SJames Smart 				oxid);
972d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, NULL, &nvmebuf->hbuf);
973d613b6a7SJames Smart 		/* Cannot send ABTS without context */
974d613b6a7SJames Smart 		return;
975d613b6a7SJames Smart 	}
976d613b6a7SJames Smart 	memset(ctxp, 0, sizeof(ctxp->ctx));
977d613b6a7SJames Smart 	ctxp->wqeq = NULL;
978d613b6a7SJames Smart 	ctxp->txrdy = NULL;
979d613b6a7SJames Smart 	ctxp->offset = 0;
980d613b6a7SJames Smart 	ctxp->phba = phba;
981d613b6a7SJames Smart 	ctxp->size = size;
982d613b6a7SJames Smart 	ctxp->oxid = oxid;
983d613b6a7SJames Smart 	ctxp->sid = sid;
984d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_RCV;
985d613b6a7SJames Smart 	ctxp->rqb_buffer = nvmebuf;
986d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
987d613b6a7SJames Smart 	ctxp->flag = 0;
988d613b6a7SJames Smart 
9892b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
9902b65e182SJames Smart 	if (phba->ktime_on) {
9912b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
9922b65e182SJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
9932b65e182SJames Smart 		ctxp->ts_nvme_data = 0;
9942b65e182SJames Smart 		ctxp->ts_data_wqput = 0;
9952b65e182SJames Smart 		ctxp->ts_isr_data = 0;
9962b65e182SJames Smart 		ctxp->ts_data_nvme = 0;
9972b65e182SJames Smart 		ctxp->ts_nvme_status = 0;
9982b65e182SJames Smart 		ctxp->ts_status_wqput = 0;
9992b65e182SJames Smart 		ctxp->ts_isr_status = 0;
10002b65e182SJames Smart 		ctxp->ts_status_nvme = 0;
10012b65e182SJames Smart 	}
10022b65e182SJames Smart 
10032b65e182SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_RCV) {
10042b65e182SJames Smart 		id = smp_processor_id();
10052b65e182SJames Smart 		if (id < LPFC_CHECK_CPU_CNT)
10062b65e182SJames Smart 			phba->cpucheck_rcv_io[id]++;
10072b65e182SJames Smart 	}
10082b65e182SJames Smart #endif
10092b65e182SJames Smart 
10102b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d from %06x\n",
10112b65e182SJames Smart 			 oxid, size, sid);
10122b65e182SJames Smart 
1013d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
1014d613b6a7SJames Smart 	/*
1015d613b6a7SJames Smart 	 * The calling sequence should be:
1016d613b6a7SJames Smart 	 * nvmet_fc_rcv_fcp_req -> lpfc_nvmet_xmt_fcp_op/cmp -> req->done
1017d613b6a7SJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
1018d613b6a7SJames Smart 	 */
1019d613b6a7SJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->ctx.fcp_req,
1020d613b6a7SJames Smart 				  payload, size);
1021d613b6a7SJames Smart 
1022d613b6a7SJames Smart 	/* Process FCP command */
1023d613b6a7SJames Smart 	if (rc == 0) {
1024d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
1025d613b6a7SJames Smart 		return;
1026d613b6a7SJames Smart 	}
1027d613b6a7SJames Smart 
1028d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
1029d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1030318083adSJames Smart 			"6159 FCP Drop IO x%x: err x%x\n",
1031d613b6a7SJames Smart 			ctxp->oxid, rc);
1032d613b6a7SJames Smart dropit:
10332b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
10342b65e182SJames Smart 			 oxid, size, sid);
1035d613b6a7SJames Smart 	if (oxid) {
1036d613b6a7SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
1037d613b6a7SJames Smart 		return;
1038d613b6a7SJames Smart 	}
1039d613b6a7SJames Smart 
1040d613b6a7SJames Smart 	if (nvmebuf) {
1041d613b6a7SJames Smart 		nvmebuf->iocbq->hba_wqidx = 0;
1042d613b6a7SJames Smart 		/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
1043d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, NULL, &nvmebuf->hbuf);
1044d613b6a7SJames Smart 	}
1045166d7211SJames Smart #endif
1046d613b6a7SJames Smart }
1047d613b6a7SJames Smart 
1048d613b6a7SJames Smart /**
1049d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_event - Process an unsolicited event from an nvme nport
1050d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
1051d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
1052d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
1053d613b6a7SJames Smart  *
1054d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
1055d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
1056d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
1057d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer() after properly set up the buffer from the
1058d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
1059d613b6a7SJames Smart  **/
1060d613b6a7SJames Smart void
1061d613b6a7SJames Smart lpfc_nvmet_unsol_ls_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1062d613b6a7SJames Smart 			  struct lpfc_iocbq *piocb)
1063d613b6a7SJames Smart {
1064d613b6a7SJames Smart 	struct lpfc_dmabuf *d_buf;
1065d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf;
1066d613b6a7SJames Smart 
1067d613b6a7SJames Smart 	d_buf = piocb->context2;
1068d613b6a7SJames Smart 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1069d613b6a7SJames Smart 
1070d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
1071d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1072d613b6a7SJames Smart 		return;
1073d613b6a7SJames Smart 	}
1074d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_buffer(phba, pring, nvmebuf);
1075d613b6a7SJames Smart }
1076d613b6a7SJames Smart 
1077d613b6a7SJames Smart /**
1078d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
1079d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
1080d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
1081d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
1082d613b6a7SJames Smart  *
1083d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
1084d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
1085d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
1086d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
1087d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
1088d613b6a7SJames Smart  **/
1089d613b6a7SJames Smart void
1090d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
1091d613b6a7SJames Smart 			   struct lpfc_sli_ring *pring,
1092d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
1093d613b6a7SJames Smart 			   uint64_t isr_timestamp)
1094d613b6a7SJames Smart {
1095d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
1096d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, NULL, &nvmebuf->hbuf);
1097d613b6a7SJames Smart 		return;
1098d613b6a7SJames Smart 	}
1099d613b6a7SJames Smart 	lpfc_nvmet_unsol_fcp_buffer(phba, pring, nvmebuf,
1100d613b6a7SJames Smart 				    isr_timestamp);
1101d613b6a7SJames Smart }
1102d613b6a7SJames Smart 
1103d613b6a7SJames Smart /**
1104d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
1105d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
1106d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
1107d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
1108d613b6a7SJames Smart  * @rspsize: size of the NVME command.
1109d613b6a7SJames Smart  *
1110d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
1111d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
1112d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
1113d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
1114d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
1115d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
1116d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
1117d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
1118d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
1119d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
1120d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
1121d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
1122d613b6a7SJames Smart  * reference for the command's callback function to access later.
1123d613b6a7SJames Smart  *
1124d613b6a7SJames Smart  * Return code
1125d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
1126d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
1127d613b6a7SJames Smart  **/
1128d613b6a7SJames Smart static struct lpfc_iocbq *
1129d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
1130d613b6a7SJames Smart 		       struct lpfc_nvmet_rcv_ctx *ctxp,
1131d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
1132d613b6a7SJames Smart {
1133d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1134d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
1135d613b6a7SJames Smart 	union lpfc_wqe *wqe;
1136d613b6a7SJames Smart 
1137d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
1138d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1139d613b6a7SJames Smart 				"6104 lpfc_nvmet_prep_ls_wqe: link err: "
1140d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1141d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1142d613b6a7SJames Smart 		return NULL;
1143d613b6a7SJames Smart 	}
1144d613b6a7SJames Smart 
1145d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
1146d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
1147d613b6a7SJames Smart 	if (nvmewqe == NULL) {
1148d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1149d613b6a7SJames Smart 				"6105 lpfc_nvmet_prep_ls_wqe: No WQE: "
1150d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1151d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1152d613b6a7SJames Smart 		return NULL;
1153d613b6a7SJames Smart 	}
1154d613b6a7SJames Smart 
1155d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1156d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1157d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1158d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1159d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1160d613b6a7SJames Smart 				"6106 lpfc_nvmet_prep_ls_wqe: No ndlp: "
1161d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1162d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1163d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
1164d613b6a7SJames Smart 	}
1165d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
1166d613b6a7SJames Smart 
1167d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
1168d613b6a7SJames Smart 	nvmewqe->context1 = lpfc_nlp_get(ndlp);
1169d613b6a7SJames Smart 	if (nvmewqe->context1 == NULL)
1170d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
1171d613b6a7SJames Smart 	nvmewqe->context2 = ctxp;
1172d613b6a7SJames Smart 
1173d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
1174d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
1175d613b6a7SJames Smart 
1176d613b6a7SJames Smart 	/* Words 0 - 2 */
1177d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1178d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
1179d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
1180d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
1181d613b6a7SJames Smart 
1182d613b6a7SJames Smart 	/* Word 3 */
1183d613b6a7SJames Smart 
1184d613b6a7SJames Smart 	/* Word 4 */
1185d613b6a7SJames Smart 
1186d613b6a7SJames Smart 	/* Word 5 */
1187d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
1188d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
1189d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
11908b361639SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
1191d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
1192d613b6a7SJames Smart 
1193d613b6a7SJames Smart 	/* Word 6 */
1194d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
1195d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1196d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
1197d613b6a7SJames Smart 
1198d613b6a7SJames Smart 	/* Word 7 */
1199d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
1200d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
1201d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
1202d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
1203d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
1204d613b6a7SJames Smart 
1205d613b6a7SJames Smart 	/* Word 8 */
1206d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
1207d613b6a7SJames Smart 
1208d613b6a7SJames Smart 	/* Word 9 */
1209d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
1210d613b6a7SJames Smart 	/* Needs to be set by caller */
1211d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
1212d613b6a7SJames Smart 
1213d613b6a7SJames Smart 	/* Word 10 */
1214d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
1215d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
1216d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
1217d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
1218d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
1219d613b6a7SJames Smart 
1220d613b6a7SJames Smart 	/* Word 11 */
1221d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
1222d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
1223d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
1224d613b6a7SJames Smart 	       OTHER_COMMAND);
1225d613b6a7SJames Smart 
1226d613b6a7SJames Smart 	/* Word 12 */
1227d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
1228d613b6a7SJames Smart 
1229d613b6a7SJames Smart 	nvmewqe->retry = 1;
1230d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
1231d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
1232d613b6a7SJames Smart 	nvmewqe->iocb_flag |= LPFC_IO_NVME_LS;
1233d613b6a7SJames Smart 
1234d613b6a7SJames Smart 	/* Xmit NVME response to remote NPORT <did> */
1235d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1236d613b6a7SJames Smart 			"6039 Xmit NVME LS response to remote "
1237d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
1238d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
1239d613b6a7SJames Smart 			rspsize);
1240d613b6a7SJames Smart 	return nvmewqe;
1241d613b6a7SJames Smart 
1242d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
1243d613b6a7SJames Smart 	nvmewqe->context2 = NULL;
1244d613b6a7SJames Smart 	nvmewqe->context3 = NULL;
1245d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
1246d613b6a7SJames Smart 	return NULL;
1247d613b6a7SJames Smart }
1248d613b6a7SJames Smart 
1249d613b6a7SJames Smart 
1250d613b6a7SJames Smart static struct lpfc_iocbq *
1251d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
1252d613b6a7SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
1253d613b6a7SJames Smart {
1254d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->ctx.fcp_req;
1255d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1256d613b6a7SJames Smart 	struct sli4_sge *sgl;
1257d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1258d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
1259d613b6a7SJames Smart 	struct scatterlist *sgel;
1260d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
1261d613b6a7SJames Smart 	uint32_t *txrdy;
1262d613b6a7SJames Smart 	dma_addr_t physaddr;
1263d613b6a7SJames Smart 	int i, cnt;
1264d613b6a7SJames Smart 	int xc = 1;
1265d613b6a7SJames Smart 
1266d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
1267d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1268d613b6a7SJames Smart 				"6107 lpfc_nvmet_prep_fcp_wqe: link err:"
1269d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n", ctxp->sid,
1270d613b6a7SJames Smart 				ctxp->oxid);
1271d613b6a7SJames Smart 		return NULL;
1272d613b6a7SJames Smart 	}
1273d613b6a7SJames Smart 
1274d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1275d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1276d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1277d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1278d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1279d613b6a7SJames Smart 				"6108 lpfc_nvmet_prep_fcp_wqe: no ndlp: "
1280d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1281d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1282d613b6a7SJames Smart 		return NULL;
1283d613b6a7SJames Smart 	}
1284d613b6a7SJames Smart 
1285d613b6a7SJames Smart 	if (rsp->sg_cnt > phba->cfg_sg_seg_cnt) {
1286d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1287d613b6a7SJames Smart 				"6109 lpfc_nvmet_prep_fcp_wqe: seg cnt err: "
1288d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1289d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1290d613b6a7SJames Smart 		return NULL;
1291d613b6a7SJames Smart 	}
1292d613b6a7SJames Smart 
1293d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1294d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
1295d613b6a7SJames Smart 	if (nvmewqe == NULL) {
1296d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
1297d613b6a7SJames Smart 		nvmewqe = ctxp->rqb_buffer->iocbq;
1298d613b6a7SJames Smart 		if (nvmewqe == NULL) {
1299d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1300d613b6a7SJames Smart 					"6110 lpfc_nvmet_prep_fcp_wqe: No "
1301d613b6a7SJames Smart 					"WQE: NPORT x%x oxid:x%x\n",
1302d613b6a7SJames Smart 					ctxp->sid, ctxp->oxid);
1303d613b6a7SJames Smart 			return NULL;
1304d613b6a7SJames Smart 		}
1305d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
1306d613b6a7SJames Smart 		xc = 0; /* create new XRI */
1307d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
1308d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
1309d613b6a7SJames Smart 	}
1310d613b6a7SJames Smart 
1311d613b6a7SJames Smart 	/* Sanity check */
1312d613b6a7SJames Smart 	if (((ctxp->state == LPFC_NVMET_STE_RCV) &&
1313d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
1314d613b6a7SJames Smart 	    ((ctxp->state == LPFC_NVMET_STE_DATA) &&
1315d613b6a7SJames Smart 	    (ctxp->entry_cnt > 1))) {
1316d613b6a7SJames Smart 		wqe = (union lpfc_wqe128 *)&nvmewqe->wqe;
1317d613b6a7SJames Smart 	} else {
1318d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1319d613b6a7SJames Smart 				"6111 Wrong state %s: %d  cnt %d\n",
1320d613b6a7SJames Smart 				__func__, ctxp->state, ctxp->entry_cnt);
1321d613b6a7SJames Smart 		return NULL;
1322d613b6a7SJames Smart 	}
1323d613b6a7SJames Smart 
1324d613b6a7SJames Smart 	sgl  = (struct sli4_sge *)ctxp->rqb_buffer->sglq->sgl;
1325d613b6a7SJames Smart 	switch (rsp->op) {
1326d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
1327d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
1328d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
1329d613b6a7SJames Smart 		sgel = &rsp->sg[0];
1330d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
1331d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1332d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
1333d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
1334d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
1335d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
1336d613b6a7SJames Smart 
1337d613b6a7SJames Smart 		/* Word 3 */
1338d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
1339d613b6a7SJames Smart 
1340d613b6a7SJames Smart 		/* Word 4 */
1341d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
1342d613b6a7SJames Smart 
1343d613b6a7SJames Smart 		/* Word 5 */
1344d613b6a7SJames Smart 
1345d613b6a7SJames Smart 		/* Word 6 */
1346d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
1347d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1348d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
1349d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
1350d613b6a7SJames Smart 
1351d613b6a7SJames Smart 		/* Word 7 */
1352d613b6a7SJames Smart 		bf_set(wqe_cmnd, &wqe->fcp_tsend.wqe_com, CMD_FCP_TSEND64_WQE);
1353d613b6a7SJames Smart 
1354d613b6a7SJames Smart 		/* Word 8 */
1355d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
1356d613b6a7SJames Smart 
1357d613b6a7SJames Smart 		/* Word 9 */
1358d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
1359d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
1360d613b6a7SJames Smart 
1361d613b6a7SJames Smart 		/* Word 10 */
1362d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
1363d613b6a7SJames Smart 		bf_set(wqe_dbde, &wqe->fcp_tsend.wqe_com, 1);
1364d613b6a7SJames Smart 		bf_set(wqe_iod, &wqe->fcp_tsend.wqe_com, LPFC_WQE_IOD_WRITE);
1365d613b6a7SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_tsend.wqe_com,
1366d613b6a7SJames Smart 		       LPFC_WQE_LENLOC_WORD12);
1367d613b6a7SJames Smart 		bf_set(wqe_ebde_cnt, &wqe->fcp_tsend.wqe_com, 0);
1368d613b6a7SJames Smart 		bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, xc);
1369d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
1370d613b6a7SJames Smart 		if (phba->cfg_nvme_oas)
1371d613b6a7SJames Smart 			bf_set(wqe_oas, &wqe->fcp_tsend.wqe_com, 1);
1372d613b6a7SJames Smart 
1373d613b6a7SJames Smart 		/* Word 11 */
1374d613b6a7SJames Smart 		bf_set(wqe_cqid, &wqe->fcp_tsend.wqe_com,
1375d613b6a7SJames Smart 		       LPFC_WQE_CQ_ID_DEFAULT);
1376d613b6a7SJames Smart 		bf_set(wqe_cmd_type, &wqe->fcp_tsend.wqe_com,
1377d613b6a7SJames Smart 		       FCP_COMMAND_TSEND);
1378d613b6a7SJames Smart 
1379d613b6a7SJames Smart 		/* Word 12 */
1380d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
1381d613b6a7SJames Smart 
1382d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
1383d613b6a7SJames Smart 		sgl->addr_hi = 0;
1384d613b6a7SJames Smart 		sgl->addr_lo = 0;
1385d613b6a7SJames Smart 		sgl->word2 = 0;
1386d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1387d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1388d613b6a7SJames Smart 		sgl->sge_len = 0;
1389d613b6a7SJames Smart 		sgl++;
1390d613b6a7SJames Smart 		sgl->addr_hi = 0;
1391d613b6a7SJames Smart 		sgl->addr_lo = 0;
1392d613b6a7SJames Smart 		sgl->word2 = 0;
1393d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1394d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1395d613b6a7SJames Smart 		sgl->sge_len = 0;
1396d613b6a7SJames Smart 		sgl++;
1397d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
1398d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
1399d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 1);
1400d613b6a7SJames Smart 			if ((ndlp->nlp_flag & NLP_SUPPRESS_RSP) &&
1401d613b6a7SJames Smart 			    (rsp->rsplen == 12)) {
1402d613b6a7SJames Smart 				bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 1);
1403d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
1404d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
1405d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
1406d613b6a7SJames Smart 			} else {
1407d613b6a7SJames Smart 				bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1408d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
1409d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
1410d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
1411d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
1412d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
1413d613b6a7SJames Smart 				       rsp->rsplen);
1414d613b6a7SJames Smart 			}
1415d613b6a7SJames Smart 		} else {
1416d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
1417d613b6a7SJames Smart 
1418d613b6a7SJames Smart 			bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1419d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
1420d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
1421d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
1422d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
1423d613b6a7SJames Smart 		}
1424d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DATA;
1425d613b6a7SJames Smart 		break;
1426d613b6a7SJames Smart 
1427d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
1428d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
1429d613b6a7SJames Smart 		txrdy = pci_pool_alloc(phba->txrdy_payload_pool,
1430d613b6a7SJames Smart 				       GFP_KERNEL, &physaddr);
1431d613b6a7SJames Smart 		if (!txrdy) {
1432d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1433d613b6a7SJames Smart 					"6041 Bad txrdy buffer: oxid x%x\n",
1434d613b6a7SJames Smart 					ctxp->oxid);
1435d613b6a7SJames Smart 			return NULL;
1436d613b6a7SJames Smart 		}
1437d613b6a7SJames Smart 		ctxp->txrdy = txrdy;
1438d613b6a7SJames Smart 		ctxp->txrdy_phys = physaddr;
1439d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1440d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = TXRDY_PAYLOAD_LEN;
1441d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrLow =
1442d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
1443d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrHigh =
1444d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
1445d613b6a7SJames Smart 
1446d613b6a7SJames Smart 		/* Word 3 */
1447d613b6a7SJames Smart 		wqe->fcp_treceive.payload_offset_len = TXRDY_PAYLOAD_LEN;
1448d613b6a7SJames Smart 
1449d613b6a7SJames Smart 		/* Word 4 */
1450d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
1451d613b6a7SJames Smart 
1452d613b6a7SJames Smart 		/* Word 5 */
1453d613b6a7SJames Smart 
1454d613b6a7SJames Smart 		/* Word 6 */
1455d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
1456d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1457d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
1458d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
1459d613b6a7SJames Smart 
1460d613b6a7SJames Smart 		/* Word 7 */
1461d613b6a7SJames Smart 		bf_set(wqe_ar, &wqe->fcp_treceive.wqe_com, 0);
1462d613b6a7SJames Smart 		bf_set(wqe_cmnd, &wqe->fcp_treceive.wqe_com,
1463d613b6a7SJames Smart 		       CMD_FCP_TRECEIVE64_WQE);
1464d613b6a7SJames Smart 
1465d613b6a7SJames Smart 		/* Word 8 */
1466d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
1467d613b6a7SJames Smart 
1468d613b6a7SJames Smart 		/* Word 9 */
1469d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
1470d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
1471d613b6a7SJames Smart 
1472d613b6a7SJames Smart 		/* Word 10 */
1473d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
1474d613b6a7SJames Smart 		bf_set(wqe_dbde, &wqe->fcp_treceive.wqe_com, 1);
1475d613b6a7SJames Smart 		bf_set(wqe_iod, &wqe->fcp_treceive.wqe_com, LPFC_WQE_IOD_READ);
1476d613b6a7SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_treceive.wqe_com,
1477d613b6a7SJames Smart 		       LPFC_WQE_LENLOC_WORD12);
1478d613b6a7SJames Smart 		bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, xc);
1479d613b6a7SJames Smart 		bf_set(wqe_wqes, &wqe->fcp_treceive.wqe_com, 0);
1480d613b6a7SJames Smart 		bf_set(wqe_irsp, &wqe->fcp_treceive.wqe_com, 0);
1481d613b6a7SJames Smart 		bf_set(wqe_irsplen, &wqe->fcp_treceive.wqe_com, 0);
1482d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
1483d613b6a7SJames Smart 		if (phba->cfg_nvme_oas)
1484d613b6a7SJames Smart 			bf_set(wqe_oas, &wqe->fcp_treceive.wqe_com, 1);
1485d613b6a7SJames Smart 
1486d613b6a7SJames Smart 		/* Word 11 */
1487d613b6a7SJames Smart 		bf_set(wqe_cqid, &wqe->fcp_treceive.wqe_com,
1488d613b6a7SJames Smart 		       LPFC_WQE_CQ_ID_DEFAULT);
1489d613b6a7SJames Smart 		bf_set(wqe_cmd_type, &wqe->fcp_treceive.wqe_com,
1490d613b6a7SJames Smart 		       FCP_COMMAND_TRECEIVE);
1491d613b6a7SJames Smart 		bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1492d613b6a7SJames Smart 
1493d613b6a7SJames Smart 		/* Word 12 */
1494d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
1495d613b6a7SJames Smart 
1496d613b6a7SJames Smart 		/* Setup 1 TXRDY and 1 SKIP SGE */
1497d613b6a7SJames Smart 		txrdy[0] = 0;
1498d613b6a7SJames Smart 		txrdy[1] = cpu_to_be32(rsp->transfer_length);
1499d613b6a7SJames Smart 		txrdy[2] = 0;
1500d613b6a7SJames Smart 
1501d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
1502d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
1503d613b6a7SJames Smart 		sgl->word2 = 0;
1504d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1505d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1506d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(TXRDY_PAYLOAD_LEN);
1507d613b6a7SJames Smart 		sgl++;
1508d613b6a7SJames Smart 		sgl->addr_hi = 0;
1509d613b6a7SJames Smart 		sgl->addr_lo = 0;
1510d613b6a7SJames Smart 		sgl->word2 = 0;
1511d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1512d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1513d613b6a7SJames Smart 		sgl->sge_len = 0;
1514d613b6a7SJames Smart 		sgl++;
1515d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DATA;
1516d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
1517d613b6a7SJames Smart 		break;
1518d613b6a7SJames Smart 
1519d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
1520d613b6a7SJames Smart 		/* Words 0 - 2 */
1521d613b6a7SJames Smart 		physaddr = rsp->rspdma;
1522d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1523d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
1524d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
1525d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
1526d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
1527d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
1528d613b6a7SJames Smart 
1529d613b6a7SJames Smart 		/* Word 3 */
1530d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
1531d613b6a7SJames Smart 
1532d613b6a7SJames Smart 		/* Word 4 */
1533d613b6a7SJames Smart 		wqe->fcp_trsp.rsvd_4_5[0] = 0;
1534d613b6a7SJames Smart 
1535d613b6a7SJames Smart 
1536d613b6a7SJames Smart 		/* Word 5 */
1537d613b6a7SJames Smart 
1538d613b6a7SJames Smart 		/* Word 6 */
1539d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
1540d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1541d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
1542d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
1543d613b6a7SJames Smart 
1544d613b6a7SJames Smart 		/* Word 7 */
1545d613b6a7SJames Smart 		bf_set(wqe_ag, &wqe->fcp_trsp.wqe_com, 1);
1546d613b6a7SJames Smart 		bf_set(wqe_cmnd, &wqe->fcp_trsp.wqe_com, CMD_FCP_TRSP64_WQE);
1547d613b6a7SJames Smart 
1548d613b6a7SJames Smart 		/* Word 8 */
1549d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
1550d613b6a7SJames Smart 
1551d613b6a7SJames Smart 		/* Word 9 */
1552d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
1553d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
1554d613b6a7SJames Smart 
1555d613b6a7SJames Smart 		/* Word 10 */
1556d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
1557d613b6a7SJames Smart 		bf_set(wqe_dbde, &wqe->fcp_trsp.wqe_com, 0);
1558d613b6a7SJames Smart 		bf_set(wqe_iod, &wqe->fcp_trsp.wqe_com, LPFC_WQE_IOD_WRITE);
1559d613b6a7SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_trsp.wqe_com,
1560d613b6a7SJames Smart 		       LPFC_WQE_LENLOC_WORD3);
1561d613b6a7SJames Smart 		bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, xc);
1562d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
1563d613b6a7SJames Smart 		if (phba->cfg_nvme_oas)
1564d613b6a7SJames Smart 			bf_set(wqe_oas, &wqe->fcp_trsp.wqe_com, 1);
1565d613b6a7SJames Smart 
1566d613b6a7SJames Smart 		/* Word 11 */
1567d613b6a7SJames Smart 		bf_set(wqe_cqid, &wqe->fcp_trsp.wqe_com,
1568d613b6a7SJames Smart 		       LPFC_WQE_CQ_ID_DEFAULT);
1569d613b6a7SJames Smart 		bf_set(wqe_cmd_type, &wqe->fcp_trsp.wqe_com,
1570d613b6a7SJames Smart 		       FCP_COMMAND_TRSP);
1571d613b6a7SJames Smart 		bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1572d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_RSP;
1573d613b6a7SJames Smart 
1574d613b6a7SJames Smart 		if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
1575d613b6a7SJames Smart 			/* Good response - all zero's on wire */
1576d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 0);
1577d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 0);
1578d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com, 0);
1579d613b6a7SJames Smart 		} else {
1580d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
1581d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
1582d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
1583d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
1584d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
1585d613b6a7SJames Smart 		}
1586d613b6a7SJames Smart 
1587d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
1588d613b6a7SJames Smart 		rsp->sg_cnt = 0;
1589d613b6a7SJames Smart 		sgl->word2 = 0;
1590d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
1591d613b6a7SJames Smart 		break;
1592d613b6a7SJames Smart 
1593d613b6a7SJames Smart 	default:
1594d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1595d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
1596d613b6a7SJames Smart 				rsp->op);
1597d613b6a7SJames Smart 		return NULL;
1598d613b6a7SJames Smart 	}
1599d613b6a7SJames Smart 
1600d613b6a7SJames Smart 	nvmewqe->retry = 1;
1601d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
1602d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
1603d613b6a7SJames Smart 	nvmewqe->context1 = ndlp;
1604d613b6a7SJames Smart 
1605d613b6a7SJames Smart 	for (i = 0; i < rsp->sg_cnt; i++) {
1606d613b6a7SJames Smart 		sgel = &rsp->sg[i];
1607d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
1608d613b6a7SJames Smart 		cnt = sg_dma_len(sgel);
1609d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
1610d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
1611d613b6a7SJames Smart 		sgl->word2 = 0;
1612d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1613d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
1614d613b6a7SJames Smart 		if ((i+1) == rsp->sg_cnt)
1615d613b6a7SJames Smart 			bf_set(lpfc_sli4_sge_last, sgl, 1);
1616d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1617d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(cnt);
1618d613b6a7SJames Smart 		sgl++;
1619d613b6a7SJames Smart 		ctxp->offset += cnt;
1620d613b6a7SJames Smart 	}
1621d613b6a7SJames Smart 	return nvmewqe;
1622d613b6a7SJames Smart }
1623d613b6a7SJames Smart 
1624d613b6a7SJames Smart /**
1625d613b6a7SJames Smart  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
1626d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
1627d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1628d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
1629d613b6a7SJames Smart  *
1630d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
1631d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
1632d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
1633d613b6a7SJames Smart  **/
1634d613b6a7SJames Smart static void
1635d613b6a7SJames Smart lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1636d613b6a7SJames Smart 			     struct lpfc_wcqe_complete *wcqe)
1637d613b6a7SJames Smart {
1638d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1639d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1640d613b6a7SJames Smart 	uint32_t status, result;
164119b58d94SJames Smart 	unsigned long flags;
164219b58d94SJames Smart 	bool released = false;
1643d613b6a7SJames Smart 
1644d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
1645d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1646d613b6a7SJames Smart 	result = wcqe->parameter;
1647d613b6a7SJames Smart 
1648d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1649d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_cmpl);
1650d613b6a7SJames Smart 
1651d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
1652d613b6a7SJames Smart 			"6165 Abort cmpl: xri x%x WCQE: %08x %08x %08x %08x\n",
1653d613b6a7SJames Smart 			ctxp->oxid, wcqe->word0, wcqe->total_data_placed,
1654d613b6a7SJames Smart 			result, wcqe->word3);
1655d613b6a7SJames Smart 
1656d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_DONE;
165719b58d94SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
165819b58d94SJames Smart 	if (ctxp->flag & LPFC_NVMET_CTX_RLS)
165919b58d94SJames Smart 		released = true;
166019b58d94SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
166119b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
166219b58d94SJames Smart 
166319b58d94SJames Smart 	/*
166419b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
166519b58d94SJames Smart 	 * will be recycled by transport release call.
166619b58d94SJames Smart 	 */
166719b58d94SJames Smart 	if (released)
1668d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
1669d613b6a7SJames Smart 
1670d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
1671d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
1672d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
1673d613b6a7SJames Smart }
1674d613b6a7SJames Smart 
1675d613b6a7SJames Smart /**
1676d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_abort_cmp - Completion handler for ABTS
1677d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
1678d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1679d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
1680d613b6a7SJames Smart  *
1681d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
1682d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
1683d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
1684d613b6a7SJames Smart  **/
1685d613b6a7SJames Smart static void
1686d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1687d613b6a7SJames Smart 			     struct lpfc_wcqe_complete *wcqe)
1688d613b6a7SJames Smart {
1689d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1690d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
169119b58d94SJames Smart 	unsigned long flags;
1692d613b6a7SJames Smart 	uint32_t status, result;
169319b58d94SJames Smart 	bool released = false;
1694d613b6a7SJames Smart 
1695d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
1696d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1697d613b6a7SJames Smart 	result = wcqe->parameter;
1698d613b6a7SJames Smart 
1699d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1700d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_cmpl);
1701d613b6a7SJames Smart 
1702d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1703d613b6a7SJames Smart 			"6070 Abort cmpl: ctx %p WCQE: %08x %08x %08x %08x\n",
1704d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
1705d613b6a7SJames Smart 			result, wcqe->word3);
1706d613b6a7SJames Smart 
1707d613b6a7SJames Smart 	if (ctxp) {
1708d613b6a7SJames Smart 		/* Sanity check */
1709d613b6a7SJames Smart 		if (ctxp->state != LPFC_NVMET_STE_ABORT) {
1710d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
1711d613b6a7SJames Smart 					"6112 ABORT Wrong state:%d oxid x%x\n",
1712d613b6a7SJames Smart 					ctxp->state, ctxp->oxid);
1713d613b6a7SJames Smart 		}
1714d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DONE;
171519b58d94SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
171619b58d94SJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_RLS)
171719b58d94SJames Smart 			released = true;
171819b58d94SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
171919b58d94SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
172019b58d94SJames Smart 
172119b58d94SJames Smart 		/*
172219b58d94SJames Smart 		 * if transport has released ctx, then can reuse it. Otherwise,
172319b58d94SJames Smart 		 * will be recycled by transport release call.
172419b58d94SJames Smart 		 */
172519b58d94SJames Smart 		if (released)
1726d613b6a7SJames Smart 			lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
172719b58d94SJames Smart 
1728d613b6a7SJames Smart 		cmdwqe->context2 = NULL;
1729d613b6a7SJames Smart 		cmdwqe->context3 = NULL;
1730d613b6a7SJames Smart 	}
1731d613b6a7SJames Smart }
1732d613b6a7SJames Smart 
1733d613b6a7SJames Smart /**
1734d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
1735d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
1736d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1737d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
1738d613b6a7SJames Smart  *
1739d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
1740d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for LS cmds
1741d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
1742d613b6a7SJames Smart  **/
1743d613b6a7SJames Smart static void
1744d613b6a7SJames Smart lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1745d613b6a7SJames Smart 			    struct lpfc_wcqe_complete *wcqe)
1746d613b6a7SJames Smart {
1747d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1748d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1749d613b6a7SJames Smart 	uint32_t status, result;
1750d613b6a7SJames Smart 
1751d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
1752d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1753d613b6a7SJames Smart 	result = wcqe->parameter;
1754d613b6a7SJames Smart 
1755d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1756d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_cmpl);
1757d613b6a7SJames Smart 
1758d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1759d613b6a7SJames Smart 			"6083 Abort cmpl: ctx %p WCQE: %08x %08x %08x %08x\n",
1760d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
1761d613b6a7SJames Smart 			result, wcqe->word3);
1762d613b6a7SJames Smart 
1763d613b6a7SJames Smart 	if (ctxp) {
1764d613b6a7SJames Smart 		cmdwqe->context2 = NULL;
1765d613b6a7SJames Smart 		cmdwqe->context3 = NULL;
1766d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
1767d613b6a7SJames Smart 		kfree(ctxp);
1768d613b6a7SJames Smart 	} else
1769d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
1770d613b6a7SJames Smart }
1771d613b6a7SJames Smart 
1772d613b6a7SJames Smart static int
1773d613b6a7SJames Smart lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
1774d613b6a7SJames Smart 			     struct lpfc_nvmet_rcv_ctx *ctxp,
1775d613b6a7SJames Smart 			     uint32_t sid, uint16_t xri)
1776d613b6a7SJames Smart {
1777d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1778d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
1779d613b6a7SJames Smart 	union lpfc_wqe *wqe_abts;
1780d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1781d613b6a7SJames Smart 
1782d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1783318083adSJames Smart 			"6067 Abort: sid %x xri x%x/x%x\n",
1784318083adSJames Smart 			sid, xri, ctxp->wqeq->sli4_xritag);
1785d613b6a7SJames Smart 
1786d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1787d613b6a7SJames Smart 
1788d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
1789d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1790d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1791d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1792d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
1793d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1794d613b6a7SJames Smart 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
1795b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
1796d613b6a7SJames Smart 
1797d613b6a7SJames Smart 		/* No failure to an ABTS request. */
1798d613b6a7SJames Smart 		return 0;
1799d613b6a7SJames Smart 	}
1800d613b6a7SJames Smart 
1801d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
1802d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
1803d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
1804d613b6a7SJames Smart 
1805d613b6a7SJames Smart 	/*
1806d613b6a7SJames Smart 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
1807d613b6a7SJames Smart 	 * that were initialized in lpfc_sli4_nvmet_alloc.
1808d613b6a7SJames Smart 	 */
1809d613b6a7SJames Smart 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
1810d613b6a7SJames Smart 
1811d613b6a7SJames Smart 	/* Word 5 */
1812d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
1813d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
1814d613b6a7SJames Smart 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
1815d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
1816d613b6a7SJames Smart 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
1817d613b6a7SJames Smart 
1818d613b6a7SJames Smart 	/* Word 6 */
1819d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
1820d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1821d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
1822d613b6a7SJames Smart 	       abts_wqeq->sli4_xritag);
1823d613b6a7SJames Smart 
1824d613b6a7SJames Smart 	/* Word 7 */
1825d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
1826d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
1827d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
1828d613b6a7SJames Smart 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
1829d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
1830d613b6a7SJames Smart 
1831d613b6a7SJames Smart 	/* Word 8 */
1832d613b6a7SJames Smart 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
1833d613b6a7SJames Smart 
1834d613b6a7SJames Smart 	/* Word 9 */
1835d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
1836d613b6a7SJames Smart 	/* Needs to be set by caller */
1837d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
1838d613b6a7SJames Smart 
1839d613b6a7SJames Smart 	/* Word 10 */
1840d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe_abts->xmit_sequence.wqe_com, 1);
1841d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
1842d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
1843d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
1844d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
1845d613b6a7SJames Smart 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
1846d613b6a7SJames Smart 
1847d613b6a7SJames Smart 	/* Word 11 */
1848d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
1849d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
1850d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
1851d613b6a7SJames Smart 	       OTHER_COMMAND);
1852d613b6a7SJames Smart 
1853d613b6a7SJames Smart 	abts_wqeq->vport = phba->pport;
1854d613b6a7SJames Smart 	abts_wqeq->context1 = ndlp;
1855d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
1856d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
1857d613b6a7SJames Smart 	abts_wqeq->rsvd2 = 0;
1858d613b6a7SJames Smart 	/* hba_wqidx should already be setup from command we are aborting */
1859d613b6a7SJames Smart 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
1860d613b6a7SJames Smart 	abts_wqeq->iocb.ulpLe = 1;
1861d613b6a7SJames Smart 
1862d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1863d613b6a7SJames Smart 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
1864d613b6a7SJames Smart 			xri, abts_wqeq->iotag);
1865d613b6a7SJames Smart 	return 1;
1866d613b6a7SJames Smart }
1867d613b6a7SJames Smart 
1868d613b6a7SJames Smart static int
1869d613b6a7SJames Smart lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
1870d613b6a7SJames Smart 			       struct lpfc_nvmet_rcv_ctx *ctxp,
1871d613b6a7SJames Smart 			       uint32_t sid, uint16_t xri)
1872d613b6a7SJames Smart {
1873d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1874d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
1875d613b6a7SJames Smart 	union lpfc_wqe *abts_wqe;
1876d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1877d613b6a7SJames Smart 	unsigned long flags;
1878d613b6a7SJames Smart 	int rc;
1879d613b6a7SJames Smart 
1880d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1881d613b6a7SJames Smart 	if (!ctxp->wqeq) {
1882d613b6a7SJames Smart 		ctxp->wqeq = ctxp->rqb_buffer->iocbq;
1883d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
1884d613b6a7SJames Smart 	}
1885d613b6a7SJames Smart 
1886d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
1887d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1888d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1889d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1890d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
1891d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1892d613b6a7SJames Smart 				"6160 Drop ABTS - wrong NDLP state x%x.\n",
1893b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
1894d613b6a7SJames Smart 
1895d613b6a7SJames Smart 		/* No failure to an ABTS request. */
1896d613b6a7SJames Smart 		return 0;
1897d613b6a7SJames Smart 	}
1898d613b6a7SJames Smart 
1899d613b6a7SJames Smart 	/* Issue ABTS for this WQE based on iotag */
1900d613b6a7SJames Smart 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
1901d613b6a7SJames Smart 	if (!ctxp->abort_wqeq) {
1902d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1903d613b6a7SJames Smart 				"6161 Abort failed: No wqeqs: "
1904d613b6a7SJames Smart 				"xri: x%x\n", ctxp->oxid);
1905d613b6a7SJames Smart 		/* No failure to an ABTS request. */
1906d613b6a7SJames Smart 		return 0;
1907d613b6a7SJames Smart 	}
1908d613b6a7SJames Smart 	abts_wqeq = ctxp->abort_wqeq;
1909d613b6a7SJames Smart 	abts_wqe = &abts_wqeq->wqe;
1910d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
1911d613b6a7SJames Smart 
1912d613b6a7SJames Smart 	/* Announce entry to new IO submit field. */
1913d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
1914d613b6a7SJames Smart 			"6162 Abort Request to rport DID x%06x "
1915d613b6a7SJames Smart 			"for xri x%x x%x\n",
1916d613b6a7SJames Smart 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
1917d613b6a7SJames Smart 
1918d613b6a7SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
1919d613b6a7SJames Smart 	 * cleared when the reset is complete and rings reestablished.
1920d613b6a7SJames Smart 	 */
1921d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
1922d613b6a7SJames Smart 	/* driver queued commands are in process of being flushed */
1923d613b6a7SJames Smart 	if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
1924d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1925d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1926d613b6a7SJames Smart 				"6163 Driver in reset cleanup - flushing "
1927d613b6a7SJames Smart 				"NVME Req now. hba_flag x%x oxid x%x\n",
1928d613b6a7SJames Smart 				phba->hba_flag, ctxp->oxid);
1929d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
1930d613b6a7SJames Smart 		return 0;
1931d613b6a7SJames Smart 	}
1932d613b6a7SJames Smart 
1933d613b6a7SJames Smart 	/* Outstanding abort is in progress */
1934d613b6a7SJames Smart 	if (abts_wqeq->iocb_flag & LPFC_DRIVER_ABORTED) {
1935d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1936d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1937d613b6a7SJames Smart 				"6164 Outstanding NVME I/O Abort Request "
1938d613b6a7SJames Smart 				"still pending on oxid x%x\n",
1939d613b6a7SJames Smart 				ctxp->oxid);
1940d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
1941d613b6a7SJames Smart 		return 0;
1942d613b6a7SJames Smart 	}
1943d613b6a7SJames Smart 
1944d613b6a7SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
1945d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_DRIVER_ABORTED;
1946d613b6a7SJames Smart 
1947d613b6a7SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
1948d613b6a7SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
1949d613b6a7SJames Smart 	 */
1950d613b6a7SJames Smart 	memset(abts_wqe, 0, sizeof(union lpfc_wqe));
1951d613b6a7SJames Smart 
1952d613b6a7SJames Smart 	/* word 3 */
1953d613b6a7SJames Smart 	bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
1954d613b6a7SJames Smart 
1955d613b6a7SJames Smart 	/* word 7 */
1956d613b6a7SJames Smart 	bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
1957d613b6a7SJames Smart 	bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
1958d613b6a7SJames Smart 
1959d613b6a7SJames Smart 	/* word 8 - tell the FW to abort the IO associated with this
1960d613b6a7SJames Smart 	 * outstanding exchange ID.
1961d613b6a7SJames Smart 	 */
1962d613b6a7SJames Smart 	abts_wqe->abort_cmd.wqe_com.abort_tag = ctxp->wqeq->sli4_xritag;
1963d613b6a7SJames Smart 
1964d613b6a7SJames Smart 	/* word 9 - this is the iotag for the abts_wqe completion. */
1965d613b6a7SJames Smart 	bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
1966d613b6a7SJames Smart 	       abts_wqeq->iotag);
1967d613b6a7SJames Smart 
1968d613b6a7SJames Smart 	/* word 10 */
1969d613b6a7SJames Smart 	bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
1970d613b6a7SJames Smart 	bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
1971d613b6a7SJames Smart 
1972d613b6a7SJames Smart 	/* word 11 */
1973d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
1974d613b6a7SJames Smart 	bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
1975d613b6a7SJames Smart 	bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1976d613b6a7SJames Smart 
1977d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
1978d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
1979d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
1980d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
1981d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVME;
1982d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
1983d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_wqeq);
1984d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1985d613b6a7SJames Smart 	if (rc == WQE_SUCCESS)
1986d613b6a7SJames Smart 		return 0;
1987d613b6a7SJames Smart 
1988d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
1989d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1990d613b6a7SJames Smart 			"6166 Failed abts issue_wqe with status x%x "
1991d613b6a7SJames Smart 			"for oxid x%x.\n",
1992d613b6a7SJames Smart 			rc, ctxp->oxid);
1993d613b6a7SJames Smart 	return 1;
1994d613b6a7SJames Smart }
1995d613b6a7SJames Smart 
1996d613b6a7SJames Smart 
1997d613b6a7SJames Smart static int
1998d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
1999d613b6a7SJames Smart 				 struct lpfc_nvmet_rcv_ctx *ctxp,
2000d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
2001d613b6a7SJames Smart {
2002d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2003d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
2004d613b6a7SJames Smart 	unsigned long flags;
2005d613b6a7SJames Smart 	int rc;
2006d613b6a7SJames Smart 
2007d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2008d613b6a7SJames Smart 	if (!ctxp->wqeq) {
2009d613b6a7SJames Smart 		ctxp->wqeq = ctxp->rqb_buffer->iocbq;
2010d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
2011d613b6a7SJames Smart 	}
2012d613b6a7SJames Smart 
2013d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
2014d613b6a7SJames Smart 	if (rc == 0)
2015d613b6a7SJames Smart 		goto aerr;
2016d613b6a7SJames Smart 
2017d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
2018d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
2019d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_abort_cmp;
2020d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
2021d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVMET;
2022d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_wqeq);
2023d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
2024d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
2025d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp);
2026d613b6a7SJames Smart 		return 0;
2027d613b6a7SJames Smart 	}
2028d613b6a7SJames Smart 
2029d613b6a7SJames Smart aerr:
2030d613b6a7SJames Smart 	lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
2031d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
2032d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
2033d613b6a7SJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x\n",
2034d613b6a7SJames Smart 			ctxp->oxid, rc);
2035d613b6a7SJames Smart 	return 1;
2036d613b6a7SJames Smart }
2037d613b6a7SJames Smart 
2038d613b6a7SJames Smart static int
2039d613b6a7SJames Smart lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *phba,
2040d613b6a7SJames Smart 				struct lpfc_nvmet_rcv_ctx *ctxp,
2041d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
2042d613b6a7SJames Smart {
2043d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2044d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
2045d613b6a7SJames Smart 	union lpfc_wqe *wqe_abts;
2046d613b6a7SJames Smart 	unsigned long flags;
2047d613b6a7SJames Smart 	int rc;
2048d613b6a7SJames Smart 
2049d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2050d613b6a7SJames Smart 	if (!ctxp->wqeq) {
2051d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
2052d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
2053d613b6a7SJames Smart 		if (!ctxp->wqeq) {
2054d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
2055d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
2056d613b6a7SJames Smart 					"xri: x%x\n", xri);
2057d613b6a7SJames Smart 			/* No failure to an ABTS request. */
2058d613b6a7SJames Smart 			kfree(ctxp);
2059d613b6a7SJames Smart 			return 0;
2060d613b6a7SJames Smart 		}
2061d613b6a7SJames Smart 	}
2062d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
2063d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
2064d613b6a7SJames Smart 	lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
2065d613b6a7SJames Smart 
2066d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
2067d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
2068d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
2069d613b6a7SJames Smart 	abts_wqeq->iocb_flag |=  LPFC_IO_NVME_LS;
2070d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, abts_wqeq);
2071d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
2072d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
2073d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp);
2074d613b6a7SJames Smart 		return 0;
2075d613b6a7SJames Smart 	}
2076d613b6a7SJames Smart 
2077d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
2078d613b6a7SJames Smart 	abts_wqeq->context2 = NULL;
2079d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
2080d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
2081d613b6a7SJames Smart 	kfree(ctxp);
2082d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
2083d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
2084d613b6a7SJames Smart 	return 0;
2085d613b6a7SJames Smart }
2086