xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision 2b7824d0)
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;
5202b65e182SJames Smart 	int rc, id;
5212b65e182SJames Smart 
5222b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5232b65e182SJames Smart 	if (phba->ktime_on) {
5242b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
5252b65e182SJames Smart 			ctxp->ts_nvme_status = ktime_get_ns();
5262b65e182SJames Smart 		else
5272b65e182SJames Smart 			ctxp->ts_nvme_data = ktime_get_ns();
5282b65e182SJames Smart 	}
5292b65e182SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
5302b65e182SJames Smart 		id = smp_processor_id();
5312b65e182SJames Smart 		ctxp->cpu = id;
5322b65e182SJames Smart 		if (id < LPFC_CHECK_CPU_CNT)
5332b65e182SJames Smart 			phba->cpucheck_xmt_io[id]++;
5342b65e182SJames Smart 		if (rsp->hwqid != id) {
5352b65e182SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
5362b65e182SJames Smart 					"6705 CPU Check OP: "
5372b65e182SJames Smart 					"cpu %d expect %d\n",
5382b65e182SJames Smart 					id, rsp->hwqid);
5392b65e182SJames Smart 			ctxp->cpu = rsp->hwqid;
5402b65e182SJames Smart 		}
5412b65e182SJames Smart 	}
5422b65e182SJames Smart #endif
543d613b6a7SJames Smart 
544d613b6a7SJames Smart 	/* Sanity check */
545d613b6a7SJames Smart 	if (ctxp->state == LPFC_NVMET_STE_ABORT) {
546d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
547d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
548d613b6a7SJames Smart 				"6102 Bad state IO x%x aborted\n",
549d613b6a7SJames Smart 				ctxp->oxid);
550a5068b46SJames Smart 		rc = -ENXIO;
551d613b6a7SJames Smart 		goto aerr;
552d613b6a7SJames Smart 	}
553d613b6a7SJames Smart 
554d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
555d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
556d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
557d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
558d613b6a7SJames Smart 				"6152 FCP Drop IO x%x: Prep\n",
559d613b6a7SJames Smart 				ctxp->oxid);
560a5068b46SJames Smart 		rc = -ENXIO;
561d613b6a7SJames Smart 		goto aerr;
562d613b6a7SJames Smart 	}
563d613b6a7SJames Smart 
564d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
565d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
566d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
567d613b6a7SJames Smart 	nvmewqeq->iocb_flag |=  LPFC_IO_NVMET;
568d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
569d613b6a7SJames Smart 
5702b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
5712b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
5722b65e182SJames Smart 
573d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, nvmewqeq);
574d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
575d613b6a7SJames Smart 		ctxp->flag |= LPFC_NVMET_IO_INP;
5762b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5772b65e182SJames Smart 		if (!phba->ktime_on)
5782b65e182SJames Smart 			return 0;
5792b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
5802b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
5812b65e182SJames Smart 		else
5822b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
5832b65e182SJames Smart #endif
584d613b6a7SJames Smart 		return 0;
585d613b6a7SJames Smart 	}
586d613b6a7SJames Smart 
587d613b6a7SJames Smart 	/* Give back resources */
588d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
589d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
590d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
591d613b6a7SJames Smart 			ctxp->oxid, rc);
592d613b6a7SJames Smart 
593d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
594d613b6a7SJames Smart 	nvmewqeq->context2 = NULL;
595d613b6a7SJames Smart 	nvmewqeq->context3 = NULL;
596a5068b46SJames Smart 	rc = -EBUSY;
597d613b6a7SJames Smart aerr:
598a5068b46SJames Smart 	return rc;
599d613b6a7SJames Smart }
600d613b6a7SJames Smart 
601d613b6a7SJames Smart static void
602d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
603d613b6a7SJames Smart {
604d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
605d613b6a7SJames Smart 
606d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
607d613b6a7SJames Smart 	complete(&tport->tport_unreg_done);
608d613b6a7SJames Smart }
609d613b6a7SJames Smart 
61019b58d94SJames Smart static void
611a97ec51bSJames Smart lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
612a97ec51bSJames Smart 			 struct nvmefc_tgt_fcp_req *req)
613a97ec51bSJames Smart {
614a97ec51bSJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
615a97ec51bSJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
616a97ec51bSJames Smart 		container_of(req, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
617a97ec51bSJames Smart 	struct lpfc_hba *phba = ctxp->phba;
618a97ec51bSJames Smart 
619a97ec51bSJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
620a97ec51bSJames Smart 			"6103 Abort op: oxri x%x %d cnt %d\n",
621a97ec51bSJames Smart 			ctxp->oxid, ctxp->state, ctxp->entry_cnt);
622a97ec51bSJames Smart 
623a97ec51bSJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x state x%x cnt x%x\n",
624a97ec51bSJames Smart 			 ctxp->oxid, ctxp->state, ctxp->entry_cnt);
625a97ec51bSJames Smart 
626a97ec51bSJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
627a97ec51bSJames Smart 	ctxp->entry_cnt++;
628a97ec51bSJames Smart 	ctxp->flag |= LPFC_NVMET_ABORT_OP;
629a97ec51bSJames Smart 	if (ctxp->flag & LPFC_NVMET_IO_INP)
630a97ec51bSJames Smart 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
631a97ec51bSJames Smart 					       ctxp->oxid);
632a97ec51bSJames Smart 	else
633a97ec51bSJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
634a97ec51bSJames Smart 						 ctxp->oxid);
635a97ec51bSJames Smart }
636a97ec51bSJames Smart 
637a97ec51bSJames Smart static void
63819b58d94SJames Smart lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
63919b58d94SJames Smart 			   struct nvmefc_tgt_fcp_req *rsp)
64019b58d94SJames Smart {
64119b58d94SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
64219b58d94SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
64319b58d94SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
64419b58d94SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
64519b58d94SJames Smart 	unsigned long flags;
64619b58d94SJames Smart 	bool aborting = false;
64719b58d94SJames Smart 
64819b58d94SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
64919b58d94SJames Smart 	if (ctxp->flag & LPFC_NVMET_ABORT_OP) {
65019b58d94SJames Smart 		aborting = true;
65119b58d94SJames Smart 		ctxp->flag |= LPFC_NVMET_CTX_RLS;
65219b58d94SJames Smart 	}
65319b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
65419b58d94SJames Smart 
65519b58d94SJames Smart 	if (aborting)
65619b58d94SJames Smart 		/* let the abort path do the real release */
65719b58d94SJames Smart 		return;
65819b58d94SJames Smart 
65919b58d94SJames Smart 	/* Sanity check */
66019b58d94SJames Smart 	if (ctxp->state != LPFC_NVMET_STE_DONE) {
66119b58d94SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
66219b58d94SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
66319b58d94SJames Smart 				"6117 Bad state IO x%x aborted\n",
66419b58d94SJames Smart 				ctxp->oxid);
66519b58d94SJames Smart 	}
66619b58d94SJames Smart 
66719b58d94SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d\n", ctxp->oxid,
66819b58d94SJames Smart 			 ctxp->state, 0);
66919b58d94SJames Smart 
67019b58d94SJames Smart 	lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
67119b58d94SJames Smart }
67219b58d94SJames Smart 
673d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
674d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
675d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
676d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
677a97ec51bSJames Smart 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
67819b58d94SJames Smart 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
679d613b6a7SJames Smart 
680d613b6a7SJames Smart 	.max_hw_queues  = 1,
681d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
682d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
683d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
684d613b6a7SJames Smart 
685d613b6a7SJames Smart 	/* optional features */
686d613b6a7SJames Smart 	.target_features = 0,
687d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
688d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
689d613b6a7SJames Smart };
690d613b6a7SJames Smart 
691d613b6a7SJames Smart int
692d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
693d613b6a7SJames Smart {
694d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
695d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
696d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
697d613b6a7SJames Smart 	int error = 0;
698d613b6a7SJames Smart 
699d613b6a7SJames Smart 	if (phba->targetport)
700d613b6a7SJames Smart 		return 0;
701d613b6a7SJames Smart 
702d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
703d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
704d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
705d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
706d613b6a7SJames Smart 
707d613b6a7SJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_nvme_io_channel;
708a44e4e8bSJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_sg_seg_cnt + 1;
709d613b6a7SJames Smart 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP |
71039498faeSJames Smart 					   NVMET_FCTGTFEAT_NEEDS_CMD_CPUSCHED |
71139498faeSJames Smart 					   NVMET_FCTGTFEAT_CMD_IN_ISR |
71239498faeSJames Smart 					   NVMET_FCTGTFEAT_OPDONE_IN_ISR;
713d613b6a7SJames Smart 
7147d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
715d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
716d613b6a7SJames Smart 					     &phba->pcidev->dev,
717d613b6a7SJames Smart 					     &phba->targetport);
718166d7211SJames Smart #else
719166d7211SJames Smart 	error = -ENOMEM;
720166d7211SJames Smart #endif
721d613b6a7SJames Smart 	if (error) {
722d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
723d613b6a7SJames Smart 				"6025 Cannot register NVME targetport "
724d613b6a7SJames Smart 				"x%x\n", error);
725d613b6a7SJames Smart 		phba->targetport = NULL;
726d613b6a7SJames Smart 	} else {
727d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
728d613b6a7SJames Smart 			phba->targetport->private;
729d613b6a7SJames Smart 		tgtp->phba = phba;
730d613b6a7SJames Smart 
731d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
732d613b6a7SJames Smart 				"6026 Registered NVME "
733d613b6a7SJames Smart 				"targetport: %p, private %p "
734d613b6a7SJames Smart 				"portnm %llx nodenm %llx\n",
735d613b6a7SJames Smart 				phba->targetport, tgtp,
736d613b6a7SJames Smart 				pinfo.port_name, pinfo.node_name);
737d613b6a7SJames Smart 
738d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
739d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
740d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
741d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
742d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
743d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
744d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
745d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
746d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
747d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
748d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
749d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
750d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
751d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
752d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
753d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
754d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
755d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
756d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
757d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
758d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
759d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
760d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_cmpl, 0);
761d613b6a7SJames Smart 	}
762d613b6a7SJames Smart 	return error;
763d613b6a7SJames Smart }
764d613b6a7SJames Smart 
765d613b6a7SJames Smart int
766d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
767d613b6a7SJames Smart {
768d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
769d613b6a7SJames Smart 
770d613b6a7SJames Smart 	if (!phba->targetport)
771d613b6a7SJames Smart 		return 0;
772d613b6a7SJames Smart 
773d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
774d613b6a7SJames Smart 			 "6007 Update NVMET port %p did x%x\n",
775d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
776d613b6a7SJames Smart 
777d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
778d613b6a7SJames Smart 	return 0;
779d613b6a7SJames Smart }
780d613b6a7SJames Smart 
781318083adSJames Smart /**
782318083adSJames Smart  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
783318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
784318083adSJames Smart  * @axri: pointer to the nvmet xri abort wcqe structure.
785318083adSJames Smart  *
786318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
787318083adSJames Smart  * NVMET aborted xri.
788318083adSJames Smart  **/
789318083adSJames Smart void
790318083adSJames Smart lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
791318083adSJames Smart 			    struct sli4_wcqe_xri_aborted *axri)
792318083adSJames Smart {
793318083adSJames Smart 	/* TODO: work in progress */
794318083adSJames Smart }
795318083adSJames Smart 
796d613b6a7SJames Smart void
797d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
798d613b6a7SJames Smart {
7997d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
800d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
801d613b6a7SJames Smart 
802d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
803d613b6a7SJames Smart 		return;
804d613b6a7SJames Smart 	if (phba->targetport) {
805d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
806d613b6a7SJames Smart 		init_completion(&tgtp->tport_unreg_done);
807d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
808d613b6a7SJames Smart 		wait_for_completion_timeout(&tgtp->tport_unreg_done, 5);
809d613b6a7SJames Smart 	}
810d613b6a7SJames Smart 	phba->targetport = NULL;
811166d7211SJames Smart #endif
812d613b6a7SJames Smart }
813d613b6a7SJames Smart 
814d613b6a7SJames Smart /**
815d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer - Process an unsolicited event data buffer
816d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
817d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
818d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
819d613b6a7SJames Smart  *
820d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
821d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
822d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
823d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
824d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
825d613b6a7SJames Smart  * of the discovery state machine.
826d613b6a7SJames Smart  **/
827d613b6a7SJames Smart static void
828d613b6a7SJames Smart lpfc_nvmet_unsol_ls_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
829d613b6a7SJames Smart 			   struct hbq_dmabuf *nvmebuf)
830d613b6a7SJames Smart {
8317d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
832d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
833d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
834d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
835d613b6a7SJames Smart 	uint32_t *payload;
836d613b6a7SJames Smart 	uint32_t size, oxid, sid, rc;
837d613b6a7SJames Smart 
838d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
839d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
840d613b6a7SJames Smart 				"6154 LS Drop IO\n");
8412b65e182SJames Smart 		oxid = 0;
8422b65e182SJames Smart 		size = 0;
8432b65e182SJames Smart 		sid = 0;
844d613b6a7SJames Smart 		goto dropit;
845d613b6a7SJames Smart 	}
846d613b6a7SJames Smart 
847d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
848d613b6a7SJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
849d613b6a7SJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
850d613b6a7SJames Smart 	size = bf_get(lpfc_rcqe_length,  &nvmebuf->cq_event.cqe.rcqe_cmpl);
851d613b6a7SJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
852d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
853d613b6a7SJames Smart 
854d613b6a7SJames Smart 	ctxp = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx), GFP_ATOMIC);
855d613b6a7SJames Smart 	if (ctxp == NULL) {
856d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_drop);
857d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
858d613b6a7SJames Smart 				"6155 LS Drop IO x%x: Alloc\n",
859d613b6a7SJames Smart 				oxid);
860d613b6a7SJames Smart dropit:
8612b65e182SJames Smart 		lpfc_nvmeio_data(phba, "NVMET LS  DROP: "
8622b65e182SJames Smart 				 "xri x%x sz %d from %06x\n",
8632b65e182SJames Smart 				 oxid, size, sid);
864d613b6a7SJames Smart 		if (nvmebuf)
865d613b6a7SJames Smart 			lpfc_in_buf_free(phba, &nvmebuf->dbuf);
866d613b6a7SJames Smart 		return;
867d613b6a7SJames Smart 	}
868d613b6a7SJames Smart 	ctxp->phba = phba;
869d613b6a7SJames Smart 	ctxp->size = size;
870d613b6a7SJames Smart 	ctxp->oxid = oxid;
871d613b6a7SJames Smart 	ctxp->sid = sid;
872d613b6a7SJames Smart 	ctxp->wqeq = NULL;
873d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_RCV;
874d613b6a7SJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
8752b65e182SJames Smart 
8762b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS   RCV: xri x%x sz %d from %06x\n",
8772b65e182SJames Smart 			 oxid, size, sid);
878d613b6a7SJames Smart 	/*
879d613b6a7SJames Smart 	 * The calling sequence should be:
880d613b6a7SJames Smart 	 * nvmet_fc_rcv_ls_req -> lpfc_nvmet_xmt_ls_rsp/cmp ->_req->done
881d613b6a7SJames Smart 	 * lpfc_nvmet_xmt_ls_rsp_cmp should free the allocated ctxp.
882d613b6a7SJames Smart 	 */
883d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
884d613b6a7SJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, &ctxp->ctx.ls_req,
885d613b6a7SJames Smart 				 payload, size);
886d613b6a7SJames Smart 
887d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
888d613b6a7SJames Smart 			"6037 %s: ctx %p sz %d rc %d: %08x %08x %08x "
889d613b6a7SJames Smart 			"%08x %08x %08x\n", __func__, ctxp, size, rc,
890d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
891d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
8922b65e182SJames Smart 
893d613b6a7SJames Smart 	if (rc == 0) {
894d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
895d613b6a7SJames Smart 		return;
896d613b6a7SJames Smart 	}
8972b65e182SJames Smart 
8982b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  DROP: xri x%x sz %d from %06x\n",
8992b65e182SJames Smart 			 oxid, size, sid);
9002b65e182SJames Smart 
901d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
902d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
903d613b6a7SJames Smart 			"6156 LS Drop IO x%x: nvmet_fc_rcv_ls_req %d\n",
904d613b6a7SJames Smart 			ctxp->oxid, rc);
905d613b6a7SJames Smart 
906d613b6a7SJames Smart 	/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
907d613b6a7SJames Smart 	if (nvmebuf)
908d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
909d613b6a7SJames Smart 
910d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_ls_abort);
911d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, sid, oxid);
912166d7211SJames Smart #endif
913d613b6a7SJames Smart }
914d613b6a7SJames Smart 
915d613b6a7SJames Smart /**
916d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
917d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
918d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
919d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
920d613b6a7SJames Smart  *
921d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
922d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
923d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
924d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
925d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
926d613b6a7SJames Smart  * of the discovery state machine.
927d613b6a7SJames Smart  **/
928d613b6a7SJames Smart static void
929d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
930d613b6a7SJames Smart 			    struct lpfc_sli_ring *pring,
931d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
932d613b6a7SJames Smart 			    uint64_t isr_timestamp)
933d613b6a7SJames Smart {
9347d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
935d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
936d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
937d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
938d613b6a7SJames Smart 	uint32_t *payload;
939d613b6a7SJames Smart 	uint32_t size, oxid, sid, rc;
9402b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
9412b65e182SJames Smart 	uint32_t id;
9422b65e182SJames Smart #endif
943d613b6a7SJames Smart 
944d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
945d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
946d613b6a7SJames Smart 				"6157 FCP Drop IO\n");
9472b65e182SJames Smart 		oxid = 0;
9482b65e182SJames Smart 		size = 0;
9492b65e182SJames Smart 		sid = 0;
950d613b6a7SJames Smart 		goto dropit;
951d613b6a7SJames Smart 	}
952d613b6a7SJames Smart 
953d613b6a7SJames Smart 
954d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
955d613b6a7SJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
956d613b6a7SJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
957d613b6a7SJames Smart 	size = nvmebuf->bytes_recv;
958d613b6a7SJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
959d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
960d613b6a7SJames Smart 
961d613b6a7SJames Smart 	ctxp = (struct lpfc_nvmet_rcv_ctx *)nvmebuf->context;
962d613b6a7SJames Smart 	if (ctxp == NULL) {
963d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
964d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
965d613b6a7SJames Smart 				"6158 FCP Drop IO x%x: Alloc\n",
966d613b6a7SJames Smart 				oxid);
967d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, NULL, &nvmebuf->hbuf);
968d613b6a7SJames Smart 		/* Cannot send ABTS without context */
969d613b6a7SJames Smart 		return;
970d613b6a7SJames Smart 	}
971d613b6a7SJames Smart 	memset(ctxp, 0, sizeof(ctxp->ctx));
972d613b6a7SJames Smart 	ctxp->wqeq = NULL;
973d613b6a7SJames Smart 	ctxp->txrdy = NULL;
974d613b6a7SJames Smart 	ctxp->offset = 0;
975d613b6a7SJames Smart 	ctxp->phba = phba;
976d613b6a7SJames Smart 	ctxp->size = size;
977d613b6a7SJames Smart 	ctxp->oxid = oxid;
978d613b6a7SJames Smart 	ctxp->sid = sid;
979d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_RCV;
980d613b6a7SJames Smart 	ctxp->rqb_buffer = nvmebuf;
981d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
982d613b6a7SJames Smart 	ctxp->flag = 0;
9832b7824d0SJames Smart 	spin_lock_init(&ctxp->ctxlock);
984d613b6a7SJames Smart 
9852b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
9862b65e182SJames Smart 	if (phba->ktime_on) {
9872b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
9882b65e182SJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
9892b65e182SJames Smart 		ctxp->ts_nvme_data = 0;
9902b65e182SJames Smart 		ctxp->ts_data_wqput = 0;
9912b65e182SJames Smart 		ctxp->ts_isr_data = 0;
9922b65e182SJames Smart 		ctxp->ts_data_nvme = 0;
9932b65e182SJames Smart 		ctxp->ts_nvme_status = 0;
9942b65e182SJames Smart 		ctxp->ts_status_wqput = 0;
9952b65e182SJames Smart 		ctxp->ts_isr_status = 0;
9962b65e182SJames Smart 		ctxp->ts_status_nvme = 0;
9972b65e182SJames Smart 	}
9982b65e182SJames Smart 
9992b65e182SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_RCV) {
10002b65e182SJames Smart 		id = smp_processor_id();
10012b65e182SJames Smart 		if (id < LPFC_CHECK_CPU_CNT)
10022b65e182SJames Smart 			phba->cpucheck_rcv_io[id]++;
10032b65e182SJames Smart 	}
10042b65e182SJames Smart #endif
10052b65e182SJames Smart 
10062b7824d0SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d CPU %02x\n",
10072b7824d0SJames Smart 			 oxid, size, smp_processor_id());
10082b65e182SJames Smart 
1009d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
1010d613b6a7SJames Smart 	/*
1011d613b6a7SJames Smart 	 * The calling sequence should be:
1012d613b6a7SJames Smart 	 * nvmet_fc_rcv_fcp_req -> lpfc_nvmet_xmt_fcp_op/cmp -> req->done
1013d613b6a7SJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
1014d613b6a7SJames Smart 	 */
1015d613b6a7SJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->ctx.fcp_req,
1016d613b6a7SJames Smart 				  payload, size);
1017d613b6a7SJames Smart 
1018d613b6a7SJames Smart 	/* Process FCP command */
1019d613b6a7SJames Smart 	if (rc == 0) {
1020d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
1021d613b6a7SJames Smart 		return;
1022d613b6a7SJames Smart 	}
1023d613b6a7SJames Smart 
1024d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
1025d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1026318083adSJames Smart 			"6159 FCP Drop IO x%x: err x%x\n",
1027d613b6a7SJames Smart 			ctxp->oxid, rc);
1028d613b6a7SJames Smart dropit:
10292b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
10302b65e182SJames Smart 			 oxid, size, sid);
1031d613b6a7SJames Smart 	if (oxid) {
1032d613b6a7SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
1033d613b6a7SJames Smart 		return;
1034d613b6a7SJames Smart 	}
1035d613b6a7SJames Smart 
1036d613b6a7SJames Smart 	if (nvmebuf) {
1037d613b6a7SJames Smart 		nvmebuf->iocbq->hba_wqidx = 0;
1038d613b6a7SJames Smart 		/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
1039d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, NULL, &nvmebuf->hbuf);
1040d613b6a7SJames Smart 	}
1041166d7211SJames Smart #endif
1042d613b6a7SJames Smart }
1043d613b6a7SJames Smart 
1044d613b6a7SJames Smart /**
1045d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_event - Process an unsolicited event from an nvme nport
1046d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
1047d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
1048d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
1049d613b6a7SJames Smart  *
1050d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
1051d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
1052d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
1053d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer() after properly set up the buffer from the
1054d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
1055d613b6a7SJames Smart  **/
1056d613b6a7SJames Smart void
1057d613b6a7SJames Smart lpfc_nvmet_unsol_ls_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1058d613b6a7SJames Smart 			  struct lpfc_iocbq *piocb)
1059d613b6a7SJames Smart {
1060d613b6a7SJames Smart 	struct lpfc_dmabuf *d_buf;
1061d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf;
1062d613b6a7SJames Smart 
1063d613b6a7SJames Smart 	d_buf = piocb->context2;
1064d613b6a7SJames Smart 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1065d613b6a7SJames Smart 
1066d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
1067d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1068d613b6a7SJames Smart 		return;
1069d613b6a7SJames Smart 	}
1070d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_buffer(phba, pring, nvmebuf);
1071d613b6a7SJames Smart }
1072d613b6a7SJames Smart 
1073d613b6a7SJames Smart /**
1074d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
1075d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
1076d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
1077d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
1078d613b6a7SJames Smart  *
1079d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
1080d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
1081d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
1082d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
1083d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
1084d613b6a7SJames Smart  **/
1085d613b6a7SJames Smart void
1086d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
1087d613b6a7SJames Smart 			   struct lpfc_sli_ring *pring,
1088d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
1089d613b6a7SJames Smart 			   uint64_t isr_timestamp)
1090d613b6a7SJames Smart {
1091d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
1092d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, NULL, &nvmebuf->hbuf);
1093d613b6a7SJames Smart 		return;
1094d613b6a7SJames Smart 	}
1095d613b6a7SJames Smart 	lpfc_nvmet_unsol_fcp_buffer(phba, pring, nvmebuf,
1096d613b6a7SJames Smart 				    isr_timestamp);
1097d613b6a7SJames Smart }
1098d613b6a7SJames Smart 
1099d613b6a7SJames Smart /**
1100d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
1101d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
1102d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
1103d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
1104d613b6a7SJames Smart  * @rspsize: size of the NVME command.
1105d613b6a7SJames Smart  *
1106d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
1107d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
1108d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
1109d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
1110d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
1111d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
1112d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
1113d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
1114d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
1115d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
1116d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
1117d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
1118d613b6a7SJames Smart  * reference for the command's callback function to access later.
1119d613b6a7SJames Smart  *
1120d613b6a7SJames Smart  * Return code
1121d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
1122d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
1123d613b6a7SJames Smart  **/
1124d613b6a7SJames Smart static struct lpfc_iocbq *
1125d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
1126d613b6a7SJames Smart 		       struct lpfc_nvmet_rcv_ctx *ctxp,
1127d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
1128d613b6a7SJames Smart {
1129d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1130d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
1131d613b6a7SJames Smart 	union lpfc_wqe *wqe;
1132d613b6a7SJames Smart 
1133d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
1134d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1135d613b6a7SJames Smart 				"6104 lpfc_nvmet_prep_ls_wqe: link err: "
1136d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1137d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1138d613b6a7SJames Smart 		return NULL;
1139d613b6a7SJames Smart 	}
1140d613b6a7SJames Smart 
1141d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
1142d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
1143d613b6a7SJames Smart 	if (nvmewqe == NULL) {
1144d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1145d613b6a7SJames Smart 				"6105 lpfc_nvmet_prep_ls_wqe: No WQE: "
1146d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1147d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1148d613b6a7SJames Smart 		return NULL;
1149d613b6a7SJames Smart 	}
1150d613b6a7SJames Smart 
1151d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1152d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1153d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1154d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1155d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1156d613b6a7SJames Smart 				"6106 lpfc_nvmet_prep_ls_wqe: No ndlp: "
1157d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1158d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1159d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
1160d613b6a7SJames Smart 	}
1161d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
1162d613b6a7SJames Smart 
1163d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
1164d613b6a7SJames Smart 	nvmewqe->context1 = lpfc_nlp_get(ndlp);
1165d613b6a7SJames Smart 	if (nvmewqe->context1 == NULL)
1166d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
1167d613b6a7SJames Smart 	nvmewqe->context2 = ctxp;
1168d613b6a7SJames Smart 
1169d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
1170d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
1171d613b6a7SJames Smart 
1172d613b6a7SJames Smart 	/* Words 0 - 2 */
1173d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1174d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
1175d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
1176d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
1177d613b6a7SJames Smart 
1178d613b6a7SJames Smart 	/* Word 3 */
1179d613b6a7SJames Smart 
1180d613b6a7SJames Smart 	/* Word 4 */
1181d613b6a7SJames Smart 
1182d613b6a7SJames Smart 	/* Word 5 */
1183d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
1184d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
1185d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
11868b361639SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
1187d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
1188d613b6a7SJames Smart 
1189d613b6a7SJames Smart 	/* Word 6 */
1190d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
1191d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1192d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
1193d613b6a7SJames Smart 
1194d613b6a7SJames Smart 	/* Word 7 */
1195d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
1196d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
1197d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
1198d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
1199d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
1200d613b6a7SJames Smart 
1201d613b6a7SJames Smart 	/* Word 8 */
1202d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
1203d613b6a7SJames Smart 
1204d613b6a7SJames Smart 	/* Word 9 */
1205d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
1206d613b6a7SJames Smart 	/* Needs to be set by caller */
1207d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
1208d613b6a7SJames Smart 
1209d613b6a7SJames Smart 	/* Word 10 */
1210d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
1211d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
1212d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
1213d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
1214d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
1215d613b6a7SJames Smart 
1216d613b6a7SJames Smart 	/* Word 11 */
1217d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
1218d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
1219d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
1220d613b6a7SJames Smart 	       OTHER_COMMAND);
1221d613b6a7SJames Smart 
1222d613b6a7SJames Smart 	/* Word 12 */
1223d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
1224d613b6a7SJames Smart 
1225d613b6a7SJames Smart 	nvmewqe->retry = 1;
1226d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
1227d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
1228d613b6a7SJames Smart 	nvmewqe->iocb_flag |= LPFC_IO_NVME_LS;
1229d613b6a7SJames Smart 
1230d613b6a7SJames Smart 	/* Xmit NVME response to remote NPORT <did> */
1231d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1232d613b6a7SJames Smart 			"6039 Xmit NVME LS response to remote "
1233d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
1234d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
1235d613b6a7SJames Smart 			rspsize);
1236d613b6a7SJames Smart 	return nvmewqe;
1237d613b6a7SJames Smart 
1238d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
1239d613b6a7SJames Smart 	nvmewqe->context2 = NULL;
1240d613b6a7SJames Smart 	nvmewqe->context3 = NULL;
1241d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
1242d613b6a7SJames Smart 	return NULL;
1243d613b6a7SJames Smart }
1244d613b6a7SJames Smart 
1245d613b6a7SJames Smart 
1246d613b6a7SJames Smart static struct lpfc_iocbq *
1247d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
1248d613b6a7SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
1249d613b6a7SJames Smart {
1250d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->ctx.fcp_req;
1251d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1252d613b6a7SJames Smart 	struct sli4_sge *sgl;
1253d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1254d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
1255d613b6a7SJames Smart 	struct scatterlist *sgel;
1256d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
1257d613b6a7SJames Smart 	uint32_t *txrdy;
1258d613b6a7SJames Smart 	dma_addr_t physaddr;
1259d613b6a7SJames Smart 	int i, cnt;
1260d613b6a7SJames Smart 	int xc = 1;
1261d613b6a7SJames Smart 
1262d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
1263d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1264d613b6a7SJames Smart 				"6107 lpfc_nvmet_prep_fcp_wqe: link err:"
1265d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n", ctxp->sid,
1266d613b6a7SJames Smart 				ctxp->oxid);
1267d613b6a7SJames Smart 		return NULL;
1268d613b6a7SJames Smart 	}
1269d613b6a7SJames Smart 
1270d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1271d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1272d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1273d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1274d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1275d613b6a7SJames Smart 				"6108 lpfc_nvmet_prep_fcp_wqe: no ndlp: "
1276d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1277d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1278d613b6a7SJames Smart 		return NULL;
1279d613b6a7SJames Smart 	}
1280d613b6a7SJames Smart 
1281d613b6a7SJames Smart 	if (rsp->sg_cnt > phba->cfg_sg_seg_cnt) {
1282d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1283d613b6a7SJames Smart 				"6109 lpfc_nvmet_prep_fcp_wqe: seg cnt err: "
1284d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1285d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1286d613b6a7SJames Smart 		return NULL;
1287d613b6a7SJames Smart 	}
1288d613b6a7SJames Smart 
1289d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1290d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
1291d613b6a7SJames Smart 	if (nvmewqe == NULL) {
1292d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
1293d613b6a7SJames Smart 		nvmewqe = ctxp->rqb_buffer->iocbq;
1294d613b6a7SJames Smart 		if (nvmewqe == NULL) {
1295d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1296d613b6a7SJames Smart 					"6110 lpfc_nvmet_prep_fcp_wqe: No "
1297d613b6a7SJames Smart 					"WQE: NPORT x%x oxid:x%x\n",
1298d613b6a7SJames Smart 					ctxp->sid, ctxp->oxid);
1299d613b6a7SJames Smart 			return NULL;
1300d613b6a7SJames Smart 		}
1301d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
1302d613b6a7SJames Smart 		xc = 0; /* create new XRI */
1303d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
1304d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
1305d613b6a7SJames Smart 	}
1306d613b6a7SJames Smart 
1307d613b6a7SJames Smart 	/* Sanity check */
1308d613b6a7SJames Smart 	if (((ctxp->state == LPFC_NVMET_STE_RCV) &&
1309d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
1310d613b6a7SJames Smart 	    ((ctxp->state == LPFC_NVMET_STE_DATA) &&
1311d613b6a7SJames Smart 	    (ctxp->entry_cnt > 1))) {
1312d613b6a7SJames Smart 		wqe = (union lpfc_wqe128 *)&nvmewqe->wqe;
1313d613b6a7SJames Smart 	} else {
1314d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1315d613b6a7SJames Smart 				"6111 Wrong state %s: %d  cnt %d\n",
1316d613b6a7SJames Smart 				__func__, ctxp->state, ctxp->entry_cnt);
1317d613b6a7SJames Smart 		return NULL;
1318d613b6a7SJames Smart 	}
1319d613b6a7SJames Smart 
1320d613b6a7SJames Smart 	sgl  = (struct sli4_sge *)ctxp->rqb_buffer->sglq->sgl;
1321d613b6a7SJames Smart 	switch (rsp->op) {
1322d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
1323d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
1324d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
1325d613b6a7SJames Smart 		sgel = &rsp->sg[0];
1326d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
1327d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1328d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
1329d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
1330d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
1331d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
1332d613b6a7SJames Smart 
1333d613b6a7SJames Smart 		/* Word 3 */
1334d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
1335d613b6a7SJames Smart 
1336d613b6a7SJames Smart 		/* Word 4 */
1337d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
1338d613b6a7SJames Smart 
1339d613b6a7SJames Smart 		/* Word 5 */
1340d613b6a7SJames Smart 
1341d613b6a7SJames Smart 		/* Word 6 */
1342d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
1343d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1344d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
1345d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
1346d613b6a7SJames Smart 
1347d613b6a7SJames Smart 		/* Word 7 */
1348d613b6a7SJames Smart 		bf_set(wqe_cmnd, &wqe->fcp_tsend.wqe_com, CMD_FCP_TSEND64_WQE);
1349d613b6a7SJames Smart 
1350d613b6a7SJames Smart 		/* Word 8 */
1351d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
1352d613b6a7SJames Smart 
1353d613b6a7SJames Smart 		/* Word 9 */
1354d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
1355d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
1356d613b6a7SJames Smart 
1357d613b6a7SJames Smart 		/* Word 10 */
1358d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
1359d613b6a7SJames Smart 		bf_set(wqe_dbde, &wqe->fcp_tsend.wqe_com, 1);
1360d613b6a7SJames Smart 		bf_set(wqe_iod, &wqe->fcp_tsend.wqe_com, LPFC_WQE_IOD_WRITE);
1361d613b6a7SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_tsend.wqe_com,
1362d613b6a7SJames Smart 		       LPFC_WQE_LENLOC_WORD12);
1363d613b6a7SJames Smart 		bf_set(wqe_ebde_cnt, &wqe->fcp_tsend.wqe_com, 0);
1364d613b6a7SJames Smart 		bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, xc);
1365d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
1366d613b6a7SJames Smart 		if (phba->cfg_nvme_oas)
1367d613b6a7SJames Smart 			bf_set(wqe_oas, &wqe->fcp_tsend.wqe_com, 1);
1368d613b6a7SJames Smart 
1369d613b6a7SJames Smart 		/* Word 11 */
1370d613b6a7SJames Smart 		bf_set(wqe_cqid, &wqe->fcp_tsend.wqe_com,
1371d613b6a7SJames Smart 		       LPFC_WQE_CQ_ID_DEFAULT);
1372d613b6a7SJames Smart 		bf_set(wqe_cmd_type, &wqe->fcp_tsend.wqe_com,
1373d613b6a7SJames Smart 		       FCP_COMMAND_TSEND);
1374d613b6a7SJames Smart 
1375d613b6a7SJames Smart 		/* Word 12 */
1376d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
1377d613b6a7SJames Smart 
1378d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
1379d613b6a7SJames Smart 		sgl->addr_hi = 0;
1380d613b6a7SJames Smart 		sgl->addr_lo = 0;
1381d613b6a7SJames Smart 		sgl->word2 = 0;
1382d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1383d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1384d613b6a7SJames Smart 		sgl->sge_len = 0;
1385d613b6a7SJames Smart 		sgl++;
1386d613b6a7SJames Smart 		sgl->addr_hi = 0;
1387d613b6a7SJames Smart 		sgl->addr_lo = 0;
1388d613b6a7SJames Smart 		sgl->word2 = 0;
1389d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1390d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1391d613b6a7SJames Smart 		sgl->sge_len = 0;
1392d613b6a7SJames Smart 		sgl++;
1393d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
1394d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
1395d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 1);
1396d613b6a7SJames Smart 			if ((ndlp->nlp_flag & NLP_SUPPRESS_RSP) &&
1397d613b6a7SJames Smart 			    (rsp->rsplen == 12)) {
1398d613b6a7SJames Smart 				bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 1);
1399d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
1400d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
1401d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
1402d613b6a7SJames Smart 			} else {
1403d613b6a7SJames Smart 				bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1404d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
1405d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
1406d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
1407d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
1408d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
1409d613b6a7SJames Smart 				       rsp->rsplen);
1410d613b6a7SJames Smart 			}
1411d613b6a7SJames Smart 		} else {
1412d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
1413d613b6a7SJames Smart 
1414d613b6a7SJames Smart 			bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1415d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
1416d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
1417d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
1418d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
1419d613b6a7SJames Smart 		}
1420d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DATA;
1421d613b6a7SJames Smart 		break;
1422d613b6a7SJames Smart 
1423d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
1424d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
1425d613b6a7SJames Smart 		txrdy = pci_pool_alloc(phba->txrdy_payload_pool,
1426d613b6a7SJames Smart 				       GFP_KERNEL, &physaddr);
1427d613b6a7SJames Smart 		if (!txrdy) {
1428d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1429d613b6a7SJames Smart 					"6041 Bad txrdy buffer: oxid x%x\n",
1430d613b6a7SJames Smart 					ctxp->oxid);
1431d613b6a7SJames Smart 			return NULL;
1432d613b6a7SJames Smart 		}
1433d613b6a7SJames Smart 		ctxp->txrdy = txrdy;
1434d613b6a7SJames Smart 		ctxp->txrdy_phys = physaddr;
1435d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1436d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = TXRDY_PAYLOAD_LEN;
1437d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrLow =
1438d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
1439d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrHigh =
1440d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
1441d613b6a7SJames Smart 
1442d613b6a7SJames Smart 		/* Word 3 */
1443d613b6a7SJames Smart 		wqe->fcp_treceive.payload_offset_len = TXRDY_PAYLOAD_LEN;
1444d613b6a7SJames Smart 
1445d613b6a7SJames Smart 		/* Word 4 */
1446d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
1447d613b6a7SJames Smart 
1448d613b6a7SJames Smart 		/* Word 5 */
1449d613b6a7SJames Smart 
1450d613b6a7SJames Smart 		/* Word 6 */
1451d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
1452d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1453d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
1454d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
1455d613b6a7SJames Smart 
1456d613b6a7SJames Smart 		/* Word 7 */
1457d613b6a7SJames Smart 		bf_set(wqe_ar, &wqe->fcp_treceive.wqe_com, 0);
1458d613b6a7SJames Smart 		bf_set(wqe_cmnd, &wqe->fcp_treceive.wqe_com,
1459d613b6a7SJames Smart 		       CMD_FCP_TRECEIVE64_WQE);
1460d613b6a7SJames Smart 
1461d613b6a7SJames Smart 		/* Word 8 */
1462d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
1463d613b6a7SJames Smart 
1464d613b6a7SJames Smart 		/* Word 9 */
1465d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
1466d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
1467d613b6a7SJames Smart 
1468d613b6a7SJames Smart 		/* Word 10 */
1469d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
1470d613b6a7SJames Smart 		bf_set(wqe_dbde, &wqe->fcp_treceive.wqe_com, 1);
1471d613b6a7SJames Smart 		bf_set(wqe_iod, &wqe->fcp_treceive.wqe_com, LPFC_WQE_IOD_READ);
1472d613b6a7SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_treceive.wqe_com,
1473d613b6a7SJames Smart 		       LPFC_WQE_LENLOC_WORD12);
1474d613b6a7SJames Smart 		bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, xc);
1475d613b6a7SJames Smart 		bf_set(wqe_wqes, &wqe->fcp_treceive.wqe_com, 0);
1476d613b6a7SJames Smart 		bf_set(wqe_irsp, &wqe->fcp_treceive.wqe_com, 0);
1477d613b6a7SJames Smart 		bf_set(wqe_irsplen, &wqe->fcp_treceive.wqe_com, 0);
1478d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
1479d613b6a7SJames Smart 		if (phba->cfg_nvme_oas)
1480d613b6a7SJames Smart 			bf_set(wqe_oas, &wqe->fcp_treceive.wqe_com, 1);
1481d613b6a7SJames Smart 
1482d613b6a7SJames Smart 		/* Word 11 */
1483d613b6a7SJames Smart 		bf_set(wqe_cqid, &wqe->fcp_treceive.wqe_com,
1484d613b6a7SJames Smart 		       LPFC_WQE_CQ_ID_DEFAULT);
1485d613b6a7SJames Smart 		bf_set(wqe_cmd_type, &wqe->fcp_treceive.wqe_com,
1486d613b6a7SJames Smart 		       FCP_COMMAND_TRECEIVE);
1487d613b6a7SJames Smart 		bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1488d613b6a7SJames Smart 
1489d613b6a7SJames Smart 		/* Word 12 */
1490d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
1491d613b6a7SJames Smart 
1492d613b6a7SJames Smart 		/* Setup 1 TXRDY and 1 SKIP SGE */
1493d613b6a7SJames Smart 		txrdy[0] = 0;
1494d613b6a7SJames Smart 		txrdy[1] = cpu_to_be32(rsp->transfer_length);
1495d613b6a7SJames Smart 		txrdy[2] = 0;
1496d613b6a7SJames Smart 
1497d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
1498d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
1499d613b6a7SJames Smart 		sgl->word2 = 0;
1500d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1501d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1502d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(TXRDY_PAYLOAD_LEN);
1503d613b6a7SJames Smart 		sgl++;
1504d613b6a7SJames Smart 		sgl->addr_hi = 0;
1505d613b6a7SJames Smart 		sgl->addr_lo = 0;
1506d613b6a7SJames Smart 		sgl->word2 = 0;
1507d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1508d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1509d613b6a7SJames Smart 		sgl->sge_len = 0;
1510d613b6a7SJames Smart 		sgl++;
1511d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DATA;
1512d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
1513d613b6a7SJames Smart 		break;
1514d613b6a7SJames Smart 
1515d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
1516d613b6a7SJames Smart 		/* Words 0 - 2 */
1517d613b6a7SJames Smart 		physaddr = rsp->rspdma;
1518d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1519d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
1520d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
1521d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
1522d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
1523d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
1524d613b6a7SJames Smart 
1525d613b6a7SJames Smart 		/* Word 3 */
1526d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
1527d613b6a7SJames Smart 
1528d613b6a7SJames Smart 		/* Word 4 */
1529d613b6a7SJames Smart 		wqe->fcp_trsp.rsvd_4_5[0] = 0;
1530d613b6a7SJames Smart 
1531d613b6a7SJames Smart 
1532d613b6a7SJames Smart 		/* Word 5 */
1533d613b6a7SJames Smart 
1534d613b6a7SJames Smart 		/* Word 6 */
1535d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
1536d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1537d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
1538d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
1539d613b6a7SJames Smart 
1540d613b6a7SJames Smart 		/* Word 7 */
1541d613b6a7SJames Smart 		bf_set(wqe_ag, &wqe->fcp_trsp.wqe_com, 1);
1542d613b6a7SJames Smart 		bf_set(wqe_cmnd, &wqe->fcp_trsp.wqe_com, CMD_FCP_TRSP64_WQE);
1543d613b6a7SJames Smart 
1544d613b6a7SJames Smart 		/* Word 8 */
1545d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
1546d613b6a7SJames Smart 
1547d613b6a7SJames Smart 		/* Word 9 */
1548d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
1549d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
1550d613b6a7SJames Smart 
1551d613b6a7SJames Smart 		/* Word 10 */
1552d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
1553d613b6a7SJames Smart 		bf_set(wqe_dbde, &wqe->fcp_trsp.wqe_com, 0);
1554d613b6a7SJames Smart 		bf_set(wqe_iod, &wqe->fcp_trsp.wqe_com, LPFC_WQE_IOD_WRITE);
1555d613b6a7SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_trsp.wqe_com,
1556d613b6a7SJames Smart 		       LPFC_WQE_LENLOC_WORD3);
1557d613b6a7SJames Smart 		bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, xc);
1558d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
1559d613b6a7SJames Smart 		if (phba->cfg_nvme_oas)
1560d613b6a7SJames Smart 			bf_set(wqe_oas, &wqe->fcp_trsp.wqe_com, 1);
1561d613b6a7SJames Smart 
1562d613b6a7SJames Smart 		/* Word 11 */
1563d613b6a7SJames Smart 		bf_set(wqe_cqid, &wqe->fcp_trsp.wqe_com,
1564d613b6a7SJames Smart 		       LPFC_WQE_CQ_ID_DEFAULT);
1565d613b6a7SJames Smart 		bf_set(wqe_cmd_type, &wqe->fcp_trsp.wqe_com,
1566d613b6a7SJames Smart 		       FCP_COMMAND_TRSP);
1567d613b6a7SJames Smart 		bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1568d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_RSP;
1569d613b6a7SJames Smart 
1570d613b6a7SJames Smart 		if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
1571d613b6a7SJames Smart 			/* Good response - all zero's on wire */
1572d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 0);
1573d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 0);
1574d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com, 0);
1575d613b6a7SJames Smart 		} else {
1576d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
1577d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
1578d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
1579d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
1580d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
1581d613b6a7SJames Smart 		}
1582d613b6a7SJames Smart 
1583d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
1584d613b6a7SJames Smart 		rsp->sg_cnt = 0;
1585d613b6a7SJames Smart 		sgl->word2 = 0;
1586d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
1587d613b6a7SJames Smart 		break;
1588d613b6a7SJames Smart 
1589d613b6a7SJames Smart 	default:
1590d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1591d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
1592d613b6a7SJames Smart 				rsp->op);
1593d613b6a7SJames Smart 		return NULL;
1594d613b6a7SJames Smart 	}
1595d613b6a7SJames Smart 
1596d613b6a7SJames Smart 	nvmewqe->retry = 1;
1597d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
1598d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
1599d613b6a7SJames Smart 	nvmewqe->context1 = ndlp;
1600d613b6a7SJames Smart 
1601d613b6a7SJames Smart 	for (i = 0; i < rsp->sg_cnt; i++) {
1602d613b6a7SJames Smart 		sgel = &rsp->sg[i];
1603d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
1604d613b6a7SJames Smart 		cnt = sg_dma_len(sgel);
1605d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
1606d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
1607d613b6a7SJames Smart 		sgl->word2 = 0;
1608d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1609d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
1610d613b6a7SJames Smart 		if ((i+1) == rsp->sg_cnt)
1611d613b6a7SJames Smart 			bf_set(lpfc_sli4_sge_last, sgl, 1);
1612d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1613d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(cnt);
1614d613b6a7SJames Smart 		sgl++;
1615d613b6a7SJames Smart 		ctxp->offset += cnt;
1616d613b6a7SJames Smart 	}
1617d613b6a7SJames Smart 	return nvmewqe;
1618d613b6a7SJames Smart }
1619d613b6a7SJames Smart 
1620d613b6a7SJames Smart /**
1621d613b6a7SJames Smart  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
1622d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
1623d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1624d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
1625d613b6a7SJames Smart  *
1626d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
1627d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
1628d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
1629d613b6a7SJames Smart  **/
1630d613b6a7SJames Smart static void
1631d613b6a7SJames Smart lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1632d613b6a7SJames Smart 			     struct lpfc_wcqe_complete *wcqe)
1633d613b6a7SJames Smart {
1634d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1635d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1636d613b6a7SJames Smart 	uint32_t status, result;
163719b58d94SJames Smart 	unsigned long flags;
163819b58d94SJames Smart 	bool released = false;
1639d613b6a7SJames Smart 
1640d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
1641d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1642d613b6a7SJames Smart 	result = wcqe->parameter;
1643d613b6a7SJames Smart 
1644d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1645d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_cmpl);
1646d613b6a7SJames Smart 
1647d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
1648d613b6a7SJames Smart 			"6165 Abort cmpl: xri x%x WCQE: %08x %08x %08x %08x\n",
1649d613b6a7SJames Smart 			ctxp->oxid, wcqe->word0, wcqe->total_data_placed,
1650d613b6a7SJames Smart 			result, wcqe->word3);
1651d613b6a7SJames Smart 
1652d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_DONE;
165319b58d94SJames Smart 	spin_lock_irqsave(&ctxp->ctxlock, flags);
165419b58d94SJames Smart 	if (ctxp->flag & LPFC_NVMET_CTX_RLS)
165519b58d94SJames Smart 		released = true;
165619b58d94SJames Smart 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
165719b58d94SJames Smart 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
165819b58d94SJames Smart 
165919b58d94SJames Smart 	/*
166019b58d94SJames Smart 	 * if transport has released ctx, then can reuse it. Otherwise,
166119b58d94SJames Smart 	 * will be recycled by transport release call.
166219b58d94SJames Smart 	 */
166319b58d94SJames Smart 	if (released)
1664d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
1665d613b6a7SJames Smart 
1666d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
1667d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
1668d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
1669d613b6a7SJames Smart }
1670d613b6a7SJames Smart 
1671d613b6a7SJames Smart /**
1672d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_abort_cmp - Completion handler for ABTS
1673d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
1674d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1675d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
1676d613b6a7SJames Smart  *
1677d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
1678d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
1679d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
1680d613b6a7SJames Smart  **/
1681d613b6a7SJames Smart static void
1682d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1683d613b6a7SJames Smart 			     struct lpfc_wcqe_complete *wcqe)
1684d613b6a7SJames Smart {
1685d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1686d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
168719b58d94SJames Smart 	unsigned long flags;
1688d613b6a7SJames Smart 	uint32_t status, result;
168919b58d94SJames Smart 	bool released = false;
1690d613b6a7SJames Smart 
1691d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
1692d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1693d613b6a7SJames Smart 	result = wcqe->parameter;
1694d613b6a7SJames Smart 
1695d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1696d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_cmpl);
1697d613b6a7SJames Smart 
1698d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1699d613b6a7SJames Smart 			"6070 Abort cmpl: ctx %p WCQE: %08x %08x %08x %08x\n",
1700d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
1701d613b6a7SJames Smart 			result, wcqe->word3);
1702d613b6a7SJames Smart 
1703d613b6a7SJames Smart 	if (ctxp) {
1704d613b6a7SJames Smart 		/* Sanity check */
1705d613b6a7SJames Smart 		if (ctxp->state != LPFC_NVMET_STE_ABORT) {
1706d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
1707d613b6a7SJames Smart 					"6112 ABORT Wrong state:%d oxid x%x\n",
1708d613b6a7SJames Smart 					ctxp->state, ctxp->oxid);
1709d613b6a7SJames Smart 		}
1710d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DONE;
171119b58d94SJames Smart 		spin_lock_irqsave(&ctxp->ctxlock, flags);
171219b58d94SJames Smart 		if (ctxp->flag & LPFC_NVMET_CTX_RLS)
171319b58d94SJames Smart 			released = true;
171419b58d94SJames Smart 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
171519b58d94SJames Smart 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
171619b58d94SJames Smart 
171719b58d94SJames Smart 		/*
171819b58d94SJames Smart 		 * if transport has released ctx, then can reuse it. Otherwise,
171919b58d94SJames Smart 		 * will be recycled by transport release call.
172019b58d94SJames Smart 		 */
172119b58d94SJames Smart 		if (released)
1722d613b6a7SJames Smart 			lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
172319b58d94SJames Smart 
1724d613b6a7SJames Smart 		cmdwqe->context2 = NULL;
1725d613b6a7SJames Smart 		cmdwqe->context3 = NULL;
1726d613b6a7SJames Smart 	}
1727d613b6a7SJames Smart }
1728d613b6a7SJames Smart 
1729d613b6a7SJames Smart /**
1730d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
1731d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
1732d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1733d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
1734d613b6a7SJames Smart  *
1735d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
1736d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for LS cmds
1737d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
1738d613b6a7SJames Smart  **/
1739d613b6a7SJames Smart static void
1740d613b6a7SJames Smart lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1741d613b6a7SJames Smart 			    struct lpfc_wcqe_complete *wcqe)
1742d613b6a7SJames Smart {
1743d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1744d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1745d613b6a7SJames Smart 	uint32_t status, result;
1746d613b6a7SJames Smart 
1747d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
1748d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1749d613b6a7SJames Smart 	result = wcqe->parameter;
1750d613b6a7SJames Smart 
1751d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1752d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_cmpl);
1753d613b6a7SJames Smart 
1754d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1755d613b6a7SJames Smart 			"6083 Abort cmpl: ctx %p WCQE: %08x %08x %08x %08x\n",
1756d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
1757d613b6a7SJames Smart 			result, wcqe->word3);
1758d613b6a7SJames Smart 
1759d613b6a7SJames Smart 	if (ctxp) {
1760d613b6a7SJames Smart 		cmdwqe->context2 = NULL;
1761d613b6a7SJames Smart 		cmdwqe->context3 = NULL;
1762d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
1763d613b6a7SJames Smart 		kfree(ctxp);
1764d613b6a7SJames Smart 	} else
1765d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
1766d613b6a7SJames Smart }
1767d613b6a7SJames Smart 
1768d613b6a7SJames Smart static int
1769d613b6a7SJames Smart lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
1770d613b6a7SJames Smart 			     struct lpfc_nvmet_rcv_ctx *ctxp,
1771d613b6a7SJames Smart 			     uint32_t sid, uint16_t xri)
1772d613b6a7SJames Smart {
1773d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1774d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
1775d613b6a7SJames Smart 	union lpfc_wqe *wqe_abts;
1776d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1777d613b6a7SJames Smart 
1778d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1779318083adSJames Smart 			"6067 Abort: sid %x xri x%x/x%x\n",
1780318083adSJames Smart 			sid, xri, ctxp->wqeq->sli4_xritag);
1781d613b6a7SJames Smart 
1782d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1783d613b6a7SJames Smart 
1784d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
1785d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1786d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1787d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1788d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
1789d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1790d613b6a7SJames Smart 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
1791b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
1792d613b6a7SJames Smart 
1793d613b6a7SJames Smart 		/* No failure to an ABTS request. */
1794d613b6a7SJames Smart 		return 0;
1795d613b6a7SJames Smart 	}
1796d613b6a7SJames Smart 
1797d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
1798d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
1799d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
1800d613b6a7SJames Smart 
1801d613b6a7SJames Smart 	/*
1802d613b6a7SJames Smart 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
1803d613b6a7SJames Smart 	 * that were initialized in lpfc_sli4_nvmet_alloc.
1804d613b6a7SJames Smart 	 */
1805d613b6a7SJames Smart 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
1806d613b6a7SJames Smart 
1807d613b6a7SJames Smart 	/* Word 5 */
1808d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
1809d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
1810d613b6a7SJames Smart 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
1811d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
1812d613b6a7SJames Smart 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
1813d613b6a7SJames Smart 
1814d613b6a7SJames Smart 	/* Word 6 */
1815d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
1816d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1817d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
1818d613b6a7SJames Smart 	       abts_wqeq->sli4_xritag);
1819d613b6a7SJames Smart 
1820d613b6a7SJames Smart 	/* Word 7 */
1821d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
1822d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
1823d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
1824d613b6a7SJames Smart 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
1825d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
1826d613b6a7SJames Smart 
1827d613b6a7SJames Smart 	/* Word 8 */
1828d613b6a7SJames Smart 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
1829d613b6a7SJames Smart 
1830d613b6a7SJames Smart 	/* Word 9 */
1831d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
1832d613b6a7SJames Smart 	/* Needs to be set by caller */
1833d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
1834d613b6a7SJames Smart 
1835d613b6a7SJames Smart 	/* Word 10 */
1836d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe_abts->xmit_sequence.wqe_com, 1);
1837d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
1838d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
1839d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
1840d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
1841d613b6a7SJames Smart 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
1842d613b6a7SJames Smart 
1843d613b6a7SJames Smart 	/* Word 11 */
1844d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
1845d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
1846d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
1847d613b6a7SJames Smart 	       OTHER_COMMAND);
1848d613b6a7SJames Smart 
1849d613b6a7SJames Smart 	abts_wqeq->vport = phba->pport;
1850d613b6a7SJames Smart 	abts_wqeq->context1 = ndlp;
1851d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
1852d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
1853d613b6a7SJames Smart 	abts_wqeq->rsvd2 = 0;
1854d613b6a7SJames Smart 	/* hba_wqidx should already be setup from command we are aborting */
1855d613b6a7SJames Smart 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
1856d613b6a7SJames Smart 	abts_wqeq->iocb.ulpLe = 1;
1857d613b6a7SJames Smart 
1858d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1859d613b6a7SJames Smart 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
1860d613b6a7SJames Smart 			xri, abts_wqeq->iotag);
1861d613b6a7SJames Smart 	return 1;
1862d613b6a7SJames Smart }
1863d613b6a7SJames Smart 
1864d613b6a7SJames Smart static int
1865d613b6a7SJames Smart lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
1866d613b6a7SJames Smart 			       struct lpfc_nvmet_rcv_ctx *ctxp,
1867d613b6a7SJames Smart 			       uint32_t sid, uint16_t xri)
1868d613b6a7SJames Smart {
1869d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1870d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
1871d613b6a7SJames Smart 	union lpfc_wqe *abts_wqe;
1872d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1873d613b6a7SJames Smart 	unsigned long flags;
1874d613b6a7SJames Smart 	int rc;
1875d613b6a7SJames Smart 
1876d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1877d613b6a7SJames Smart 	if (!ctxp->wqeq) {
1878d613b6a7SJames Smart 		ctxp->wqeq = ctxp->rqb_buffer->iocbq;
1879d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
1880d613b6a7SJames Smart 	}
1881d613b6a7SJames Smart 
1882d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
1883d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1884d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1885d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1886d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
1887d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1888d613b6a7SJames Smart 				"6160 Drop ABTS - wrong NDLP state x%x.\n",
1889b5ccc7d6SJames Smart 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
1890d613b6a7SJames Smart 
1891d613b6a7SJames Smart 		/* No failure to an ABTS request. */
1892d613b6a7SJames Smart 		return 0;
1893d613b6a7SJames Smart 	}
1894d613b6a7SJames Smart 
1895d613b6a7SJames Smart 	/* Issue ABTS for this WQE based on iotag */
1896d613b6a7SJames Smart 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
1897d613b6a7SJames Smart 	if (!ctxp->abort_wqeq) {
1898d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1899d613b6a7SJames Smart 				"6161 Abort failed: No wqeqs: "
1900d613b6a7SJames Smart 				"xri: x%x\n", ctxp->oxid);
1901d613b6a7SJames Smart 		/* No failure to an ABTS request. */
1902d613b6a7SJames Smart 		return 0;
1903d613b6a7SJames Smart 	}
1904d613b6a7SJames Smart 	abts_wqeq = ctxp->abort_wqeq;
1905d613b6a7SJames Smart 	abts_wqe = &abts_wqeq->wqe;
1906d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
1907d613b6a7SJames Smart 
1908d613b6a7SJames Smart 	/* Announce entry to new IO submit field. */
1909d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
1910d613b6a7SJames Smart 			"6162 Abort Request to rport DID x%06x "
1911d613b6a7SJames Smart 			"for xri x%x x%x\n",
1912d613b6a7SJames Smart 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
1913d613b6a7SJames Smart 
1914d613b6a7SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
1915d613b6a7SJames Smart 	 * cleared when the reset is complete and rings reestablished.
1916d613b6a7SJames Smart 	 */
1917d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
1918d613b6a7SJames Smart 	/* driver queued commands are in process of being flushed */
1919d613b6a7SJames Smart 	if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
1920d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1921d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1922d613b6a7SJames Smart 				"6163 Driver in reset cleanup - flushing "
1923d613b6a7SJames Smart 				"NVME Req now. hba_flag x%x oxid x%x\n",
1924d613b6a7SJames Smart 				phba->hba_flag, ctxp->oxid);
1925d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
1926d613b6a7SJames Smart 		return 0;
1927d613b6a7SJames Smart 	}
1928d613b6a7SJames Smart 
1929d613b6a7SJames Smart 	/* Outstanding abort is in progress */
1930d613b6a7SJames Smart 	if (abts_wqeq->iocb_flag & LPFC_DRIVER_ABORTED) {
1931d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1932d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1933d613b6a7SJames Smart 				"6164 Outstanding NVME I/O Abort Request "
1934d613b6a7SJames Smart 				"still pending on oxid x%x\n",
1935d613b6a7SJames Smart 				ctxp->oxid);
1936d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
1937d613b6a7SJames Smart 		return 0;
1938d613b6a7SJames Smart 	}
1939d613b6a7SJames Smart 
1940d613b6a7SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
1941d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_DRIVER_ABORTED;
1942d613b6a7SJames Smart 
1943d613b6a7SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
1944d613b6a7SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
1945d613b6a7SJames Smart 	 */
1946d613b6a7SJames Smart 	memset(abts_wqe, 0, sizeof(union lpfc_wqe));
1947d613b6a7SJames Smart 
1948d613b6a7SJames Smart 	/* word 3 */
1949d613b6a7SJames Smart 	bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
1950d613b6a7SJames Smart 
1951d613b6a7SJames Smart 	/* word 7 */
1952d613b6a7SJames Smart 	bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
1953d613b6a7SJames Smart 	bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
1954d613b6a7SJames Smart 
1955d613b6a7SJames Smart 	/* word 8 - tell the FW to abort the IO associated with this
1956d613b6a7SJames Smart 	 * outstanding exchange ID.
1957d613b6a7SJames Smart 	 */
1958d613b6a7SJames Smart 	abts_wqe->abort_cmd.wqe_com.abort_tag = ctxp->wqeq->sli4_xritag;
1959d613b6a7SJames Smart 
1960d613b6a7SJames Smart 	/* word 9 - this is the iotag for the abts_wqe completion. */
1961d613b6a7SJames Smart 	bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
1962d613b6a7SJames Smart 	       abts_wqeq->iotag);
1963d613b6a7SJames Smart 
1964d613b6a7SJames Smart 	/* word 10 */
1965d613b6a7SJames Smart 	bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
1966d613b6a7SJames Smart 	bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
1967d613b6a7SJames Smart 
1968d613b6a7SJames Smart 	/* word 11 */
1969d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
1970d613b6a7SJames Smart 	bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
1971d613b6a7SJames Smart 	bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1972d613b6a7SJames Smart 
1973d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
1974d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
1975d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
1976d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
1977d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVME;
1978d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
1979d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_wqeq);
1980d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1981d613b6a7SJames Smart 	if (rc == WQE_SUCCESS)
1982d613b6a7SJames Smart 		return 0;
1983d613b6a7SJames Smart 
1984d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
1985d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1986d613b6a7SJames Smart 			"6166 Failed abts issue_wqe with status x%x "
1987d613b6a7SJames Smart 			"for oxid x%x.\n",
1988d613b6a7SJames Smart 			rc, ctxp->oxid);
1989d613b6a7SJames Smart 	return 1;
1990d613b6a7SJames Smart }
1991d613b6a7SJames Smart 
1992d613b6a7SJames Smart 
1993d613b6a7SJames Smart static int
1994d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
1995d613b6a7SJames Smart 				 struct lpfc_nvmet_rcv_ctx *ctxp,
1996d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
1997d613b6a7SJames Smart {
1998d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1999d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
2000d613b6a7SJames Smart 	unsigned long flags;
2001d613b6a7SJames Smart 	int rc;
2002d613b6a7SJames Smart 
2003d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2004d613b6a7SJames Smart 	if (!ctxp->wqeq) {
2005d613b6a7SJames Smart 		ctxp->wqeq = ctxp->rqb_buffer->iocbq;
2006d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
2007d613b6a7SJames Smart 	}
2008d613b6a7SJames Smart 
2009d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
2010d613b6a7SJames Smart 	if (rc == 0)
2011d613b6a7SJames Smart 		goto aerr;
2012d613b6a7SJames Smart 
2013d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
2014d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
2015d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_abort_cmp;
2016d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
2017d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVMET;
2018d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_wqeq);
2019d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
2020d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
2021d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp);
2022d613b6a7SJames Smart 		return 0;
2023d613b6a7SJames Smart 	}
2024d613b6a7SJames Smart 
2025d613b6a7SJames Smart aerr:
2026d613b6a7SJames Smart 	lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
2027d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
2028d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
2029d613b6a7SJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x\n",
2030d613b6a7SJames Smart 			ctxp->oxid, rc);
2031d613b6a7SJames Smart 	return 1;
2032d613b6a7SJames Smart }
2033d613b6a7SJames Smart 
2034d613b6a7SJames Smart static int
2035d613b6a7SJames Smart lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *phba,
2036d613b6a7SJames Smart 				struct lpfc_nvmet_rcv_ctx *ctxp,
2037d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
2038d613b6a7SJames Smart {
2039d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
2040d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
2041d613b6a7SJames Smart 	union lpfc_wqe *wqe_abts;
2042d613b6a7SJames Smart 	unsigned long flags;
2043d613b6a7SJames Smart 	int rc;
2044d613b6a7SJames Smart 
2045d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2046d613b6a7SJames Smart 	if (!ctxp->wqeq) {
2047d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
2048d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
2049d613b6a7SJames Smart 		if (!ctxp->wqeq) {
2050d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
2051d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
2052d613b6a7SJames Smart 					"xri: x%x\n", xri);
2053d613b6a7SJames Smart 			/* No failure to an ABTS request. */
2054d613b6a7SJames Smart 			kfree(ctxp);
2055d613b6a7SJames Smart 			return 0;
2056d613b6a7SJames Smart 		}
2057d613b6a7SJames Smart 	}
2058d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
2059d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
2060d613b6a7SJames Smart 	lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
2061d613b6a7SJames Smart 
2062d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
2063d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
2064d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
2065d613b6a7SJames Smart 	abts_wqeq->iocb_flag |=  LPFC_IO_NVME_LS;
2066d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, abts_wqeq);
2067d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
2068d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
2069d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp);
2070d613b6a7SJames Smart 		return 0;
2071d613b6a7SJames Smart 	}
2072d613b6a7SJames Smart 
2073d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
2074d613b6a7SJames Smart 	abts_wqeq->context2 = NULL;
2075d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
2076d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
2077d613b6a7SJames Smart 	kfree(ctxp);
2078d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
2079d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
2080d613b6a7SJames Smart 	return 0;
2081d613b6a7SJames Smart }
2082