xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision 2b65e182)
1d613b6a7SJames Smart /*******************************************************************
2d613b6a7SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
3d613b6a7SJames Smart  * Fibre Channsel Host Bus Adapters.                                *
4d613b6a7SJames Smart  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
5d613b6a7SJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
6d613b6a7SJames Smart  * www.emulex.com                                                  *
7d613b6a7SJames Smart  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
8d613b6a7SJames Smart  *                                                                 *
9d613b6a7SJames Smart  * This program is free software; you can redistribute it and/or   *
10d613b6a7SJames Smart  * modify it under the terms of version 2 of the GNU General       *
11d613b6a7SJames Smart  * Public License as published by the Free Software Foundation.    *
12d613b6a7SJames Smart  * This program is distributed in the hope that it will be useful. *
13d613b6a7SJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
14d613b6a7SJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
15d613b6a7SJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
16d613b6a7SJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17d613b6a7SJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
18d613b6a7SJames Smart  * more details, a copy of which can be found in the file COPYING  *
19d613b6a7SJames Smart  * included with this package.                                     *
20d613b6a7SJames Smart  ********************************************************************/
21d613b6a7SJames Smart #include <linux/pci.h>
22d613b6a7SJames Smart #include <linux/slab.h>
23d613b6a7SJames Smart #include <linux/interrupt.h>
24d613b6a7SJames Smart #include <linux/delay.h>
25d613b6a7SJames Smart #include <asm/unaligned.h>
26d613b6a7SJames Smart #include <linux/crc-t10dif.h>
27d613b6a7SJames Smart #include <net/checksum.h>
28d613b6a7SJames Smart 
29d613b6a7SJames Smart #include <scsi/scsi.h>
30d613b6a7SJames Smart #include <scsi/scsi_device.h>
31d613b6a7SJames Smart #include <scsi/scsi_eh.h>
32d613b6a7SJames Smart #include <scsi/scsi_host.h>
33d613b6a7SJames Smart #include <scsi/scsi_tcq.h>
34d613b6a7SJames Smart #include <scsi/scsi_transport_fc.h>
35d613b6a7SJames Smart #include <scsi/fc/fc_fs.h>
36d613b6a7SJames Smart 
37d613b6a7SJames Smart #include <../drivers/nvme/host/nvme.h>
38d613b6a7SJames Smart #include <linux/nvme-fc-driver.h>
39d613b6a7SJames Smart 
40d613b6a7SJames Smart #include "lpfc_version.h"
41d613b6a7SJames Smart #include "lpfc_hw4.h"
42d613b6a7SJames Smart #include "lpfc_hw.h"
43d613b6a7SJames Smart #include "lpfc_sli.h"
44d613b6a7SJames Smart #include "lpfc_sli4.h"
45d613b6a7SJames Smart #include "lpfc_nl.h"
46d613b6a7SJames Smart #include "lpfc_disc.h"
47d613b6a7SJames Smart #include "lpfc.h"
48d613b6a7SJames Smart #include "lpfc_scsi.h"
49d613b6a7SJames Smart #include "lpfc_nvme.h"
50d613b6a7SJames Smart #include "lpfc_nvmet.h"
51d613b6a7SJames Smart #include "lpfc_logmsg.h"
52d613b6a7SJames Smart #include "lpfc_crtn.h"
53d613b6a7SJames Smart #include "lpfc_vport.h"
542b65e182SJames Smart #include "lpfc_debugfs.h"
55d613b6a7SJames Smart 
56d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *,
57d613b6a7SJames Smart 						 struct lpfc_nvmet_rcv_ctx *,
58d613b6a7SJames Smart 						 dma_addr_t rspbuf,
59d613b6a7SJames Smart 						 uint16_t rspsize);
60d613b6a7SJames Smart static struct lpfc_iocbq *lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *,
61d613b6a7SJames Smart 						  struct lpfc_nvmet_rcv_ctx *);
62d613b6a7SJames Smart static int lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *,
63d613b6a7SJames Smart 					  struct lpfc_nvmet_rcv_ctx *,
64d613b6a7SJames Smart 					  uint32_t, uint16_t);
65d613b6a7SJames Smart static int lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *,
66d613b6a7SJames Smart 					    struct lpfc_nvmet_rcv_ctx *,
67d613b6a7SJames Smart 					    uint32_t, uint16_t);
68d613b6a7SJames Smart static int lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *,
69d613b6a7SJames Smart 					   struct lpfc_nvmet_rcv_ctx *,
70d613b6a7SJames Smart 					   uint32_t, uint16_t);
71d613b6a7SJames Smart 
72d613b6a7SJames Smart /**
73d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
74d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
75d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
76d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
77d613b6a7SJames Smart  *
78d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
79d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME LS commands
80d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
81d613b6a7SJames Smart  **/
82d613b6a7SJames Smart static void
83d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
84d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
85d613b6a7SJames Smart {
86d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
87d613b6a7SJames Smart 	struct nvmefc_tgt_ls_req *rsp;
88d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
89d613b6a7SJames Smart 	uint32_t status, result;
90d613b6a7SJames Smart 
91d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
92d613b6a7SJames Smart 	result = wcqe->parameter;
93d613b6a7SJames Smart 	if (!phba->targetport)
94d613b6a7SJames Smart 		goto out;
95d613b6a7SJames Smart 
96d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
97d613b6a7SJames Smart 
98d613b6a7SJames Smart 	if (status)
99d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_ls_rsp_error);
100d613b6a7SJames Smart 	else
101d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
102d613b6a7SJames Smart 
103d613b6a7SJames Smart out:
104d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
105d613b6a7SJames Smart 	rsp = &ctxp->ctx.ls_req;
106d613b6a7SJames Smart 
1072b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  CMPL: xri x%x stat x%x result x%x\n",
1082b65e182SJames Smart 			 ctxp->oxid, status, result);
1092b65e182SJames Smart 
110d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
111d613b6a7SJames Smart 			"6038 %s: Entrypoint: ctx %p status %x/%x\n", __func__,
112d613b6a7SJames Smart 			ctxp, status, result);
113d613b6a7SJames Smart 
114d613b6a7SJames Smart 	lpfc_nlp_put(cmdwqe->context1);
115d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
116d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
117d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
118d613b6a7SJames Smart 	rsp->done(rsp);
119d613b6a7SJames Smart 	kfree(ctxp);
120d613b6a7SJames Smart }
121d613b6a7SJames Smart 
122d613b6a7SJames Smart /**
123d613b6a7SJames Smart  * lpfc_nvmet_rq_post - Repost a NVMET RQ DMA buffer and clean up context
124d613b6a7SJames Smart  * @phba: HBA buffer is associated with
125d613b6a7SJames Smart  * @ctxp: context to clean up
126d613b6a7SJames Smart  * @mp: Buffer to free
127d613b6a7SJames Smart  *
128d613b6a7SJames Smart  * Description: Frees the given DMA buffer in the appropriate way given by
129d613b6a7SJames Smart  * reposting it to its associated RQ so it can be reused.
130d613b6a7SJames Smart  *
131d613b6a7SJames Smart  * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
132d613b6a7SJames Smart  *
133d613b6a7SJames Smart  * Returns: None
134d613b6a7SJames Smart  **/
135d613b6a7SJames Smart void
136d613b6a7SJames Smart lpfc_nvmet_rq_post(struct lpfc_hba *phba, struct lpfc_nvmet_rcv_ctx *ctxp,
137d613b6a7SJames Smart 		   struct lpfc_dmabuf *mp)
138d613b6a7SJames Smart {
139d613b6a7SJames Smart 	if (ctxp) {
140d613b6a7SJames Smart 		if (ctxp->txrdy) {
141d613b6a7SJames Smart 			pci_pool_free(phba->txrdy_payload_pool, ctxp->txrdy,
142d613b6a7SJames Smart 				      ctxp->txrdy_phys);
143d613b6a7SJames Smart 			ctxp->txrdy = NULL;
144d613b6a7SJames Smart 			ctxp->txrdy_phys = 0;
145d613b6a7SJames Smart 		}
146d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_FREE;
147d613b6a7SJames Smart 	}
148d613b6a7SJames Smart 	lpfc_rq_buf_free(phba, mp);
149d613b6a7SJames Smart }
150d613b6a7SJames Smart 
1512b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1522b65e182SJames Smart static void
1532b65e182SJames Smart lpfc_nvmet_ktime(struct lpfc_hba *phba,
1542b65e182SJames Smart 		 struct lpfc_nvmet_rcv_ctx *ctxp)
1552b65e182SJames Smart {
1562b65e182SJames Smart 	uint64_t seg1, seg2, seg3, seg4, seg5;
1572b65e182SJames Smart 	uint64_t seg6, seg7, seg8, seg9, seg10;
1582b65e182SJames Smart 
1592b65e182SJames Smart 	if (!phba->ktime_on)
1602b65e182SJames Smart 		return;
1612b65e182SJames Smart 
1622b65e182SJames Smart 	if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
1632b65e182SJames Smart 	    !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
1642b65e182SJames Smart 	    !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
1652b65e182SJames Smart 	    !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
1662b65e182SJames Smart 	    !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
1672b65e182SJames Smart 		return;
1682b65e182SJames Smart 
1692b65e182SJames Smart 	if (ctxp->ts_isr_cmd  > ctxp->ts_cmd_nvme)
1702b65e182SJames Smart 		return;
1712b65e182SJames Smart 	if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
1722b65e182SJames Smart 		return;
1732b65e182SJames Smart 	if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
1742b65e182SJames Smart 		return;
1752b65e182SJames Smart 	if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
1762b65e182SJames Smart 		return;
1772b65e182SJames Smart 	if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
1782b65e182SJames Smart 		return;
1792b65e182SJames Smart 	if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
1802b65e182SJames Smart 		return;
1812b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
1822b65e182SJames Smart 		return;
1832b65e182SJames Smart 	if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
1842b65e182SJames Smart 		return;
1852b65e182SJames Smart 	if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
1862b65e182SJames Smart 		return;
1872b65e182SJames Smart 	/*
1882b65e182SJames Smart 	 * Segment 1 - Time from FCP command received by MSI-X ISR
1892b65e182SJames Smart 	 * to FCP command is passed to NVME Layer.
1902b65e182SJames Smart 	 * Segment 2 - Time from FCP command payload handed
1912b65e182SJames Smart 	 * off to NVME Layer to Driver receives a Command op
1922b65e182SJames Smart 	 * from NVME Layer.
1932b65e182SJames Smart 	 * Segment 3 - Time from Driver receives a Command op
1942b65e182SJames Smart 	 * from NVME Layer to Command is put on WQ.
1952b65e182SJames Smart 	 * Segment 4 - Time from Driver WQ put is done
1962b65e182SJames Smart 	 * to MSI-X ISR for Command cmpl.
1972b65e182SJames Smart 	 * Segment 5 - Time from MSI-X ISR for Command cmpl to
1982b65e182SJames Smart 	 * Command cmpl is passed to NVME Layer.
1992b65e182SJames Smart 	 * Segment 6 - Time from Command cmpl is passed to NVME
2002b65e182SJames Smart 	 * Layer to Driver receives a RSP op from NVME Layer.
2012b65e182SJames Smart 	 * Segment 7 - Time from Driver receives a RSP op from
2022b65e182SJames Smart 	 * NVME Layer to WQ put is done on TRSP FCP Status.
2032b65e182SJames Smart 	 * Segment 8 - Time from Driver WQ put is done on TRSP
2042b65e182SJames Smart 	 * FCP Status to MSI-X ISR for TRSP cmpl.
2052b65e182SJames Smart 	 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
2062b65e182SJames Smart 	 * TRSP cmpl is passed to NVME Layer.
2072b65e182SJames Smart 	 * Segment 10 - Time from FCP command received by
2082b65e182SJames Smart 	 * MSI-X ISR to command is completed on wire.
2092b65e182SJames Smart 	 * (Segments 1 thru 8) for READDATA / WRITEDATA
2102b65e182SJames Smart 	 * (Segments 1 thru 4) for READDATA_RSP
2112b65e182SJames Smart 	 */
2122b65e182SJames Smart 	seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
2132b65e182SJames Smart 	seg2 = (ctxp->ts_nvme_data - ctxp->ts_isr_cmd) - seg1;
2142b65e182SJames Smart 	seg3 = (ctxp->ts_data_wqput - ctxp->ts_isr_cmd) -
2152b65e182SJames Smart 		seg1 - seg2;
2162b65e182SJames Smart 	seg4 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd) -
2172b65e182SJames Smart 		seg1 - seg2 - seg3;
2182b65e182SJames Smart 	seg5 = (ctxp->ts_data_nvme - ctxp->ts_isr_cmd) -
2192b65e182SJames Smart 		seg1 - seg2 - seg3 - seg4;
2202b65e182SJames Smart 
2212b65e182SJames Smart 	/* For auto rsp commands seg6 thru seg10 will be 0 */
2222b65e182SJames Smart 	if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
2232b65e182SJames Smart 		seg6 = (ctxp->ts_nvme_status -
2242b65e182SJames Smart 			ctxp->ts_isr_cmd) -
2252b65e182SJames Smart 			seg1 - seg2 - seg3 - seg4 - seg5;
2262b65e182SJames Smart 		seg7 = (ctxp->ts_status_wqput -
2272b65e182SJames Smart 			ctxp->ts_isr_cmd) -
2282b65e182SJames Smart 			seg1 - seg2 - seg3 -
2292b65e182SJames Smart 			seg4 - seg5 - seg6;
2302b65e182SJames Smart 		seg8 = (ctxp->ts_isr_status -
2312b65e182SJames Smart 			ctxp->ts_isr_cmd) -
2322b65e182SJames Smart 			seg1 - seg2 - seg3 - seg4 -
2332b65e182SJames Smart 			seg5 - seg6 - seg7;
2342b65e182SJames Smart 		seg9 = (ctxp->ts_status_nvme -
2352b65e182SJames Smart 			ctxp->ts_isr_cmd) -
2362b65e182SJames Smart 			seg1 - seg2 - seg3 - seg4 -
2372b65e182SJames Smart 			seg5 - seg6 - seg7 - seg8;
2382b65e182SJames Smart 		seg10 = (ctxp->ts_isr_status -
2392b65e182SJames Smart 			ctxp->ts_isr_cmd);
2402b65e182SJames Smart 	} else {
2412b65e182SJames Smart 		seg6 =  0;
2422b65e182SJames Smart 		seg7 =  0;
2432b65e182SJames Smart 		seg8 =  0;
2442b65e182SJames Smart 		seg9 =  0;
2452b65e182SJames Smart 		seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
2462b65e182SJames Smart 	}
2472b65e182SJames Smart 
2482b65e182SJames Smart 	phba->ktime_seg1_total += seg1;
2492b65e182SJames Smart 	if (seg1 < phba->ktime_seg1_min)
2502b65e182SJames Smart 		phba->ktime_seg1_min = seg1;
2512b65e182SJames Smart 	else if (seg1 > phba->ktime_seg1_max)
2522b65e182SJames Smart 		phba->ktime_seg1_max = seg1;
2532b65e182SJames Smart 
2542b65e182SJames Smart 	phba->ktime_seg2_total += seg2;
2552b65e182SJames Smart 	if (seg2 < phba->ktime_seg2_min)
2562b65e182SJames Smart 		phba->ktime_seg2_min = seg2;
2572b65e182SJames Smart 	else if (seg2 > phba->ktime_seg2_max)
2582b65e182SJames Smart 		phba->ktime_seg2_max = seg2;
2592b65e182SJames Smart 
2602b65e182SJames Smart 	phba->ktime_seg3_total += seg3;
2612b65e182SJames Smart 	if (seg3 < phba->ktime_seg3_min)
2622b65e182SJames Smart 		phba->ktime_seg3_min = seg3;
2632b65e182SJames Smart 	else if (seg3 > phba->ktime_seg3_max)
2642b65e182SJames Smart 		phba->ktime_seg3_max = seg3;
2652b65e182SJames Smart 
2662b65e182SJames Smart 	phba->ktime_seg4_total += seg4;
2672b65e182SJames Smart 	if (seg4 < phba->ktime_seg4_min)
2682b65e182SJames Smart 		phba->ktime_seg4_min = seg4;
2692b65e182SJames Smart 	else if (seg4 > phba->ktime_seg4_max)
2702b65e182SJames Smart 		phba->ktime_seg4_max = seg4;
2712b65e182SJames Smart 
2722b65e182SJames Smart 	phba->ktime_seg5_total += seg5;
2732b65e182SJames Smart 	if (seg5 < phba->ktime_seg5_min)
2742b65e182SJames Smart 		phba->ktime_seg5_min = seg5;
2752b65e182SJames Smart 	else if (seg5 > phba->ktime_seg5_max)
2762b65e182SJames Smart 		phba->ktime_seg5_max = seg5;
2772b65e182SJames Smart 
2782b65e182SJames Smart 	phba->ktime_data_samples++;
2792b65e182SJames Smart 	if (!seg6)
2802b65e182SJames Smart 		goto out;
2812b65e182SJames Smart 
2822b65e182SJames Smart 	phba->ktime_seg6_total += seg6;
2832b65e182SJames Smart 	if (seg6 < phba->ktime_seg6_min)
2842b65e182SJames Smart 		phba->ktime_seg6_min = seg6;
2852b65e182SJames Smart 	else if (seg6 > phba->ktime_seg6_max)
2862b65e182SJames Smart 		phba->ktime_seg6_max = seg6;
2872b65e182SJames Smart 
2882b65e182SJames Smart 	phba->ktime_seg7_total += seg7;
2892b65e182SJames Smart 	if (seg7 < phba->ktime_seg7_min)
2902b65e182SJames Smart 		phba->ktime_seg7_min = seg7;
2912b65e182SJames Smart 	else if (seg7 > phba->ktime_seg7_max)
2922b65e182SJames Smart 		phba->ktime_seg7_max = seg7;
2932b65e182SJames Smart 
2942b65e182SJames Smart 	phba->ktime_seg8_total += seg8;
2952b65e182SJames Smart 	if (seg8 < phba->ktime_seg8_min)
2962b65e182SJames Smart 		phba->ktime_seg8_min = seg8;
2972b65e182SJames Smart 	else if (seg8 > phba->ktime_seg8_max)
2982b65e182SJames Smart 		phba->ktime_seg8_max = seg8;
2992b65e182SJames Smart 
3002b65e182SJames Smart 	phba->ktime_seg9_total += seg9;
3012b65e182SJames Smart 	if (seg9 < phba->ktime_seg9_min)
3022b65e182SJames Smart 		phba->ktime_seg9_min = seg9;
3032b65e182SJames Smart 	else if (seg9 > phba->ktime_seg9_max)
3042b65e182SJames Smart 		phba->ktime_seg9_max = seg9;
3052b65e182SJames Smart out:
3062b65e182SJames Smart 	phba->ktime_seg10_total += seg10;
3072b65e182SJames Smart 	if (seg10 < phba->ktime_seg10_min)
3082b65e182SJames Smart 		phba->ktime_seg10_min = seg10;
3092b65e182SJames Smart 	else if (seg10 > phba->ktime_seg10_max)
3102b65e182SJames Smart 		phba->ktime_seg10_max = seg10;
3112b65e182SJames Smart 	phba->ktime_status_samples++;
3122b65e182SJames Smart }
3132b65e182SJames Smart #endif
3142b65e182SJames Smart 
315d613b6a7SJames Smart /**
316d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
317d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
318d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
319d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
320d613b6a7SJames Smart  *
321d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
322d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME FCP commands
323d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
324d613b6a7SJames Smart  **/
325d613b6a7SJames Smart static void
326d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
327d613b6a7SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
328d613b6a7SJames Smart {
329d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
330d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp;
331d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
332d613b6a7SJames Smart 	uint32_t status, result, op, start_clean;
3332b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3342b65e182SJames Smart 	uint32_t id;
3352b65e182SJames Smart #endif
336d613b6a7SJames Smart 
337d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
338d613b6a7SJames Smart 	rsp = &ctxp->ctx.fcp_req;
339d613b6a7SJames Smart 	op = rsp->op;
340d613b6a7SJames Smart 	ctxp->flag &= ~LPFC_NVMET_IO_INP;
341d613b6a7SJames Smart 
342d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
343d613b6a7SJames Smart 	result = wcqe->parameter;
344d613b6a7SJames Smart 
345d613b6a7SJames Smart 	if (!phba->targetport)
346d613b6a7SJames Smart 		goto out;
347d613b6a7SJames Smart 
3482b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
3492b65e182SJames Smart 			 ctxp->oxid, op, status);
3502b65e182SJames Smart 
351d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
352d613b6a7SJames Smart 	if (status) {
353d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
354d613b6a7SJames Smart 		rsp->transferred_length = 0;
355d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp_error);
356d613b6a7SJames Smart 	} else {
357d613b6a7SJames Smart 		rsp->fcp_error = NVME_SC_SUCCESS;
358d613b6a7SJames Smart 		if (op == NVMET_FCOP_RSP)
359d613b6a7SJames Smart 			rsp->transferred_length = rsp->rsplen;
360d613b6a7SJames Smart 		else
361d613b6a7SJames Smart 			rsp->transferred_length = rsp->transfer_length;
362d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
363d613b6a7SJames Smart 	}
364d613b6a7SJames Smart 
365d613b6a7SJames Smart out:
366d613b6a7SJames Smart 	if ((op == NVMET_FCOP_READDATA_RSP) ||
367d613b6a7SJames Smart 	    (op == NVMET_FCOP_RSP)) {
368d613b6a7SJames Smart 		/* Sanity check */
369d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DONE;
370d613b6a7SJames Smart 		ctxp->entry_cnt++;
3712b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3722b65e182SJames Smart 		if (phba->ktime_on) {
3732b65e182SJames Smart 			if (rsp->op == NVMET_FCOP_READDATA_RSP) {
3742b65e182SJames Smart 				ctxp->ts_isr_data =
3752b65e182SJames Smart 					cmdwqe->isr_timestamp;
3762b65e182SJames Smart 				ctxp->ts_data_nvme =
3772b65e182SJames Smart 					ktime_get_ns();
3782b65e182SJames Smart 				ctxp->ts_nvme_status =
3792b65e182SJames Smart 					ctxp->ts_data_nvme;
3802b65e182SJames Smart 				ctxp->ts_status_wqput =
3812b65e182SJames Smart 					ctxp->ts_data_nvme;
3822b65e182SJames Smart 				ctxp->ts_isr_status =
3832b65e182SJames Smart 					ctxp->ts_data_nvme;
3842b65e182SJames Smart 				ctxp->ts_status_nvme =
3852b65e182SJames Smart 					ctxp->ts_data_nvme;
3862b65e182SJames Smart 			} else {
3872b65e182SJames Smart 				ctxp->ts_isr_status =
3882b65e182SJames Smart 					cmdwqe->isr_timestamp;
3892b65e182SJames Smart 				ctxp->ts_status_nvme =
3902b65e182SJames Smart 					ktime_get_ns();
3912b65e182SJames Smart 			}
3922b65e182SJames Smart 		}
3932b65e182SJames Smart 		if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
3942b65e182SJames Smart 			id = smp_processor_id();
3952b65e182SJames Smart 			if (ctxp->cpu != id)
3962b65e182SJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3972b65e182SJames Smart 						"6703 CPU Check cmpl: "
3982b65e182SJames Smart 						"cpu %d expect %d\n",
3992b65e182SJames Smart 						id, ctxp->cpu);
4002b65e182SJames Smart 			if (ctxp->cpu < LPFC_CHECK_CPU_CNT)
4012b65e182SJames Smart 				phba->cpucheck_cmpl_io[id]++;
4022b65e182SJames Smart 		}
4032b65e182SJames Smart #endif
404d613b6a7SJames Smart 		rsp->done(rsp);
4052b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4062b65e182SJames Smart 		if (phba->ktime_on)
4072b65e182SJames Smart 			lpfc_nvmet_ktime(phba, ctxp);
4082b65e182SJames Smart #endif
409d613b6a7SJames Smart 		/* Let Abort cmpl repost the context */
410d613b6a7SJames Smart 		if (!(ctxp->flag & LPFC_NVMET_ABORT_OP))
411d613b6a7SJames Smart 			lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
412d613b6a7SJames Smart 	} else {
413d613b6a7SJames Smart 		ctxp->entry_cnt++;
414d613b6a7SJames Smart 		start_clean = offsetof(struct lpfc_iocbq, wqe);
415d613b6a7SJames Smart 		memset(((char *)cmdwqe) + start_clean, 0,
416d613b6a7SJames Smart 		       (sizeof(struct lpfc_iocbq) - start_clean));
4172b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4182b65e182SJames Smart 		if (phba->ktime_on) {
4192b65e182SJames Smart 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
4202b65e182SJames Smart 			ctxp->ts_data_nvme = ktime_get_ns();
4212b65e182SJames Smart 		}
4222b65e182SJames Smart 		if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
4232b65e182SJames Smart 			id = smp_processor_id();
4242b65e182SJames Smart 			if (ctxp->cpu != id)
4252b65e182SJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
4262b65e182SJames Smart 						"6704 CPU Check cmdcmpl: "
4272b65e182SJames Smart 						"cpu %d expect %d\n",
4282b65e182SJames Smart 						id, ctxp->cpu);
4292b65e182SJames Smart 			if (ctxp->cpu < LPFC_CHECK_CPU_CNT)
4302b65e182SJames Smart 				phba->cpucheck_ccmpl_io[id]++;
4312b65e182SJames Smart 		}
4322b65e182SJames Smart #endif
433d613b6a7SJames Smart 		rsp->done(rsp);
434d613b6a7SJames Smart 	}
435d613b6a7SJames Smart }
436d613b6a7SJames Smart 
437d613b6a7SJames Smart static int
438d613b6a7SJames Smart lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
439d613b6a7SJames Smart 		      struct nvmefc_tgt_ls_req *rsp)
440d613b6a7SJames Smart {
441d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
442d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.ls_req);
443d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
444d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf =
445d613b6a7SJames Smart 		(struct hbq_dmabuf *)ctxp->rqb_buffer;
446d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
447d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
448d613b6a7SJames Smart 	struct lpfc_dmabuf dmabuf;
449d613b6a7SJames Smart 	struct ulp_bde64 bpl;
450d613b6a7SJames Smart 	int rc;
451d613b6a7SJames Smart 
452d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
453d613b6a7SJames Smart 			"6023 %s: Entrypoint ctx %p %p\n", __func__,
454d613b6a7SJames Smart 			ctxp, tgtport);
455d613b6a7SJames Smart 
456d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, ctxp, rsp->rspdma,
457d613b6a7SJames Smart 				      rsp->rsplen);
458d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
459d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_drop);
460d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
461d613b6a7SJames Smart 				"6150 LS Drop IO x%x: Prep\n",
462d613b6a7SJames Smart 				ctxp->oxid);
463d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
464d613b6a7SJames Smart 		lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp,
465d613b6a7SJames Smart 						ctxp->sid, ctxp->oxid);
466d613b6a7SJames Smart 		return -ENOMEM;
467d613b6a7SJames Smart 	}
468d613b6a7SJames Smart 
469d613b6a7SJames Smart 	/* Save numBdes for bpl2sgl */
470d613b6a7SJames Smart 	nvmewqeq->rsvd2 = 1;
471d613b6a7SJames Smart 	nvmewqeq->hba_wqidx = 0;
472d613b6a7SJames Smart 	nvmewqeq->context3 = &dmabuf;
473d613b6a7SJames Smart 	dmabuf.virt = &bpl;
474d613b6a7SJames Smart 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
475d613b6a7SJames Smart 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
476d613b6a7SJames Smart 	bpl.tus.f.bdeSize = rsp->rsplen;
477d613b6a7SJames Smart 	bpl.tus.f.bdeFlags = 0;
478d613b6a7SJames Smart 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
479d613b6a7SJames Smart 
480d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_rsp_cmp;
481d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
482d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
483d613b6a7SJames Smart 
4842b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  RESP: xri x%x wqidx x%x len x%x\n",
4852b65e182SJames Smart 			 ctxp->oxid, nvmewqeq->hba_wqidx, rsp->rsplen);
4862b65e182SJames Smart 
487d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, nvmewqeq);
488d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
489d613b6a7SJames Smart 		/*
490d613b6a7SJames Smart 		 * Okay to repost buffer here, but wait till cmpl
491d613b6a7SJames Smart 		 * before freeing ctxp and iocbq.
492d613b6a7SJames Smart 		 */
493d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
494d613b6a7SJames Smart 		ctxp->rqb_buffer = 0;
495d613b6a7SJames Smart 		atomic_inc(&nvmep->xmt_ls_rsp);
496d613b6a7SJames Smart 		return 0;
497d613b6a7SJames Smart 	}
498d613b6a7SJames Smart 	/* Give back resources */
499d613b6a7SJames Smart 	atomic_inc(&nvmep->xmt_ls_drop);
500d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
501d613b6a7SJames Smart 			"6151 LS Drop IO x%x: Issue %d\n",
502d613b6a7SJames Smart 			ctxp->oxid, rc);
503d613b6a7SJames Smart 
504d613b6a7SJames Smart 	lpfc_nlp_put(nvmewqeq->context1);
505d613b6a7SJames Smart 
506d613b6a7SJames Smart 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
507d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
508d613b6a7SJames Smart 	return -ENXIO;
509d613b6a7SJames Smart }
510d613b6a7SJames Smart 
511d613b6a7SJames Smart static int
512d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
513d613b6a7SJames Smart 		      struct nvmefc_tgt_fcp_req *rsp)
514d613b6a7SJames Smart {
515d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
516d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp =
517d613b6a7SJames Smart 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
518d613b6a7SJames Smart 	struct lpfc_hba *phba = ctxp->phba;
519d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqeq;
520d613b6a7SJames Smart 	unsigned long iflags;
5212b65e182SJames Smart 	int rc, id;
5222b65e182SJames Smart 
5232b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5242b65e182SJames Smart 	if (phba->ktime_on) {
5252b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
5262b65e182SJames Smart 			ctxp->ts_nvme_status = ktime_get_ns();
5272b65e182SJames Smart 		else
5282b65e182SJames Smart 			ctxp->ts_nvme_data = ktime_get_ns();
5292b65e182SJames Smart 	}
5302b65e182SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
5312b65e182SJames Smart 		id = smp_processor_id();
5322b65e182SJames Smart 		ctxp->cpu = id;
5332b65e182SJames Smart 		if (id < LPFC_CHECK_CPU_CNT)
5342b65e182SJames Smart 			phba->cpucheck_xmt_io[id]++;
5352b65e182SJames Smart 		if (rsp->hwqid != id) {
5362b65e182SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
5372b65e182SJames Smart 					"6705 CPU Check OP: "
5382b65e182SJames Smart 					"cpu %d expect %d\n",
5392b65e182SJames Smart 					id, rsp->hwqid);
5402b65e182SJames Smart 			ctxp->cpu = rsp->hwqid;
5412b65e182SJames Smart 		}
5422b65e182SJames Smart 	}
5432b65e182SJames Smart #endif
544d613b6a7SJames Smart 
545d613b6a7SJames Smart 	if (rsp->op == NVMET_FCOP_ABORT) {
546d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
547d613b6a7SJames Smart 				"6103 Abort op: oxri x%x %d cnt %d\n",
548d613b6a7SJames Smart 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
5492b65e182SJames Smart 
5502b65e182SJames Smart 		lpfc_nvmeio_data(phba, "NVMET FCP ABRT: "
5512b65e182SJames Smart 				 "xri x%x state x%x cnt x%x\n",
5522b65e182SJames Smart 				 ctxp->oxid, ctxp->state, ctxp->entry_cnt);
5532b65e182SJames Smart 
554d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
555d613b6a7SJames Smart 		ctxp->entry_cnt++;
556d613b6a7SJames Smart 		ctxp->flag |= LPFC_NVMET_ABORT_OP;
557d613b6a7SJames Smart 		if (ctxp->flag & LPFC_NVMET_IO_INP)
558d613b6a7SJames Smart 			lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
559d613b6a7SJames Smart 						       ctxp->oxid);
560d613b6a7SJames Smart 		else
561d613b6a7SJames Smart 			lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
562d613b6a7SJames Smart 							 ctxp->oxid);
563d613b6a7SJames Smart 		return 0;
564d613b6a7SJames Smart 	}
565d613b6a7SJames Smart 
566d613b6a7SJames Smart 	/* Sanity check */
567d613b6a7SJames Smart 	if (ctxp->state == LPFC_NVMET_STE_ABORT) {
568d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
569d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
570d613b6a7SJames Smart 				"6102 Bad state IO x%x aborted\n",
571d613b6a7SJames Smart 				ctxp->oxid);
572d613b6a7SJames Smart 		goto aerr;
573d613b6a7SJames Smart 	}
574d613b6a7SJames Smart 
575d613b6a7SJames Smart 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
576d613b6a7SJames Smart 	if (nvmewqeq == NULL) {
577d613b6a7SJames Smart 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
578d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
579d613b6a7SJames Smart 				"6152 FCP Drop IO x%x: Prep\n",
580d613b6a7SJames Smart 				ctxp->oxid);
581d613b6a7SJames Smart 		goto aerr;
582d613b6a7SJames Smart 	}
583d613b6a7SJames Smart 
584d613b6a7SJames Smart 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
585d613b6a7SJames Smart 	nvmewqeq->iocb_cmpl = NULL;
586d613b6a7SJames Smart 	nvmewqeq->context2 = ctxp;
587d613b6a7SJames Smart 	nvmewqeq->iocb_flag |=  LPFC_IO_NVMET;
588d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
589d613b6a7SJames Smart 
5902b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
5912b65e182SJames Smart 			 ctxp->oxid, rsp->op, rsp->rsplen);
5922b65e182SJames Smart 
593d613b6a7SJames Smart 	/* For now we take hbalock */
594d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, iflags);
595d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, nvmewqeq);
596d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflags);
597d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
598d613b6a7SJames Smart 		ctxp->flag |= LPFC_NVMET_IO_INP;
5992b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
6002b65e182SJames Smart 		if (!phba->ktime_on)
6012b65e182SJames Smart 			return 0;
6022b65e182SJames Smart 		if (rsp->op == NVMET_FCOP_RSP)
6032b65e182SJames Smart 			ctxp->ts_status_wqput = ktime_get_ns();
6042b65e182SJames Smart 		else
6052b65e182SJames Smart 			ctxp->ts_data_wqput = ktime_get_ns();
6062b65e182SJames Smart #endif
607d613b6a7SJames Smart 		return 0;
608d613b6a7SJames Smart 	}
609d613b6a7SJames Smart 
610d613b6a7SJames Smart 	/* Give back resources */
611d613b6a7SJames Smart 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
612d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
613d613b6a7SJames Smart 			"6153 FCP Drop IO x%x: Issue: %d\n",
614d613b6a7SJames Smart 			ctxp->oxid, rc);
615d613b6a7SJames Smart 
616d613b6a7SJames Smart 	ctxp->wqeq->hba_wqidx = 0;
617d613b6a7SJames Smart 	nvmewqeq->context2 = NULL;
618d613b6a7SJames Smart 	nvmewqeq->context3 = NULL;
619d613b6a7SJames Smart aerr:
620d613b6a7SJames Smart 	return -ENXIO;
621d613b6a7SJames Smart }
622d613b6a7SJames Smart 
623d613b6a7SJames Smart static void
624d613b6a7SJames Smart lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
625d613b6a7SJames Smart {
626d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tport = targetport->private;
627d613b6a7SJames Smart 
628d613b6a7SJames Smart 	/* release any threads waiting for the unreg to complete */
629d613b6a7SJames Smart 	complete(&tport->tport_unreg_done);
630d613b6a7SJames Smart }
631d613b6a7SJames Smart 
632d613b6a7SJames Smart static struct nvmet_fc_target_template lpfc_tgttemplate = {
633d613b6a7SJames Smart 	.targetport_delete = lpfc_nvmet_targetport_delete,
634d613b6a7SJames Smart 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
635d613b6a7SJames Smart 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
636d613b6a7SJames Smart 
637d613b6a7SJames Smart 	.max_hw_queues  = 1,
638d613b6a7SJames Smart 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
639d613b6a7SJames Smart 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
640d613b6a7SJames Smart 	.dma_boundary = 0xFFFFFFFF,
641d613b6a7SJames Smart 
642d613b6a7SJames Smart 	/* optional features */
643d613b6a7SJames Smart 	.target_features = 0,
644d613b6a7SJames Smart 	/* sizes of additional private data for data structures */
645d613b6a7SJames Smart 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
646d613b6a7SJames Smart };
647d613b6a7SJames Smart 
648d613b6a7SJames Smart int
649d613b6a7SJames Smart lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
650d613b6a7SJames Smart {
651d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
652d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
653d613b6a7SJames Smart 	struct nvmet_fc_port_info pinfo;
654d613b6a7SJames Smart 	int error = 0;
655d613b6a7SJames Smart 
656d613b6a7SJames Smart 	if (phba->targetport)
657d613b6a7SJames Smart 		return 0;
658d613b6a7SJames Smart 
659d613b6a7SJames Smart 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
660d613b6a7SJames Smart 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
661d613b6a7SJames Smart 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
662d613b6a7SJames Smart 	pinfo.port_id = vport->fc_myDID;
663d613b6a7SJames Smart 
664d613b6a7SJames Smart 	lpfc_tgttemplate.max_hw_queues = phba->cfg_nvme_io_channel;
665d613b6a7SJames Smart 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_sg_seg_cnt;
666d613b6a7SJames Smart 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP |
667d613b6a7SJames Smart 					   NVMET_FCTGTFEAT_NEEDS_CMD_CPUSCHED;
668d613b6a7SJames Smart 
669d613b6a7SJames Smart 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
670d613b6a7SJames Smart 					     &phba->pcidev->dev,
671d613b6a7SJames Smart 					     &phba->targetport);
672d613b6a7SJames Smart 	if (error) {
673d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
674d613b6a7SJames Smart 				"6025 Cannot register NVME targetport "
675d613b6a7SJames Smart 				"x%x\n", error);
676d613b6a7SJames Smart 		phba->targetport = NULL;
677d613b6a7SJames Smart 	} else {
678d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)
679d613b6a7SJames Smart 			phba->targetport->private;
680d613b6a7SJames Smart 		tgtp->phba = phba;
681d613b6a7SJames Smart 
682d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
683d613b6a7SJames Smart 				"6026 Registered NVME "
684d613b6a7SJames Smart 				"targetport: %p, private %p "
685d613b6a7SJames Smart 				"portnm %llx nodenm %llx\n",
686d613b6a7SJames Smart 				phba->targetport, tgtp,
687d613b6a7SJames Smart 				pinfo.port_name, pinfo.node_name);
688d613b6a7SJames Smart 
689d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_in, 0);
690d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_out, 0);
691d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
692d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_abort, 0);
693d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp, 0);
694d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_drop, 0);
695d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
696d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
697d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
698d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
699d613b6a7SJames Smart 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
700d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_abort, 0);
701d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_drop, 0);
702d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
703d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_read, 0);
704d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_write, 0);
705d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
706d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
707d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
708d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
709d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp, 0);
710d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
711d613b6a7SJames Smart 		atomic_set(&tgtp->xmt_abort_cmpl, 0);
712d613b6a7SJames Smart 	}
713d613b6a7SJames Smart 	return error;
714d613b6a7SJames Smart }
715d613b6a7SJames Smart 
716d613b6a7SJames Smart int
717d613b6a7SJames Smart lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
718d613b6a7SJames Smart {
719d613b6a7SJames Smart 	struct lpfc_vport  *vport = phba->pport;
720d613b6a7SJames Smart 
721d613b6a7SJames Smart 	if (!phba->targetport)
722d613b6a7SJames Smart 		return 0;
723d613b6a7SJames Smart 
724d613b6a7SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
725d613b6a7SJames Smart 			 "6007 Update NVMET port %p did x%x\n",
726d613b6a7SJames Smart 			 phba->targetport, vport->fc_myDID);
727d613b6a7SJames Smart 
728d613b6a7SJames Smart 	phba->targetport->port_id = vport->fc_myDID;
729d613b6a7SJames Smart 	return 0;
730d613b6a7SJames Smart }
731d613b6a7SJames Smart 
732d613b6a7SJames Smart void
733d613b6a7SJames Smart lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
734d613b6a7SJames Smart {
735d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
736d613b6a7SJames Smart 
737d613b6a7SJames Smart 	if (phba->nvmet_support == 0)
738d613b6a7SJames Smart 		return;
739d613b6a7SJames Smart 	if (phba->targetport) {
740d613b6a7SJames Smart 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
741d613b6a7SJames Smart 		init_completion(&tgtp->tport_unreg_done);
742d613b6a7SJames Smart 		nvmet_fc_unregister_targetport(phba->targetport);
743d613b6a7SJames Smart 		wait_for_completion_timeout(&tgtp->tport_unreg_done, 5);
744d613b6a7SJames Smart 	}
745d613b6a7SJames Smart 	phba->targetport = NULL;
746d613b6a7SJames Smart }
747d613b6a7SJames Smart 
748d613b6a7SJames Smart /**
749d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer - Process an unsolicited event data buffer
750d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
751d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
752d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
753d613b6a7SJames Smart  *
754d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
755d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
756d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
757d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
758d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
759d613b6a7SJames Smart  * of the discovery state machine.
760d613b6a7SJames Smart  **/
761d613b6a7SJames Smart static void
762d613b6a7SJames Smart lpfc_nvmet_unsol_ls_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
763d613b6a7SJames Smart 			   struct hbq_dmabuf *nvmebuf)
764d613b6a7SJames Smart {
765d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
766d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
767d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
768d613b6a7SJames Smart 	uint32_t *payload;
769d613b6a7SJames Smart 	uint32_t size, oxid, sid, rc;
770d613b6a7SJames Smart 
771d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
772d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
773d613b6a7SJames Smart 				"6154 LS Drop IO\n");
7742b65e182SJames Smart 		oxid = 0;
7752b65e182SJames Smart 		size = 0;
7762b65e182SJames Smart 		sid = 0;
777d613b6a7SJames Smart 		goto dropit;
778d613b6a7SJames Smart 	}
779d613b6a7SJames Smart 
780d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
781d613b6a7SJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
782d613b6a7SJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
783d613b6a7SJames Smart 	size = bf_get(lpfc_rcqe_length,  &nvmebuf->cq_event.cqe.rcqe_cmpl);
784d613b6a7SJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
785d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
786d613b6a7SJames Smart 
787d613b6a7SJames Smart 	ctxp = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx), GFP_ATOMIC);
788d613b6a7SJames Smart 	if (ctxp == NULL) {
789d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_drop);
790d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
791d613b6a7SJames Smart 				"6155 LS Drop IO x%x: Alloc\n",
792d613b6a7SJames Smart 				oxid);
793d613b6a7SJames Smart dropit:
7942b65e182SJames Smart 		lpfc_nvmeio_data(phba, "NVMET LS  DROP: "
7952b65e182SJames Smart 				 "xri x%x sz %d from %06x\n",
7962b65e182SJames Smart 				 oxid, size, sid);
797d613b6a7SJames Smart 		if (nvmebuf)
798d613b6a7SJames Smart 			lpfc_in_buf_free(phba, &nvmebuf->dbuf);
799d613b6a7SJames Smart 		return;
800d613b6a7SJames Smart 	}
801d613b6a7SJames Smart 	ctxp->phba = phba;
802d613b6a7SJames Smart 	ctxp->size = size;
803d613b6a7SJames Smart 	ctxp->oxid = oxid;
804d613b6a7SJames Smart 	ctxp->sid = sid;
805d613b6a7SJames Smart 	ctxp->wqeq = NULL;
806d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_RCV;
807d613b6a7SJames Smart 	ctxp->rqb_buffer = (void *)nvmebuf;
8082b65e182SJames Smart 
8092b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS   RCV: xri x%x sz %d from %06x\n",
8102b65e182SJames Smart 			 oxid, size, sid);
811d613b6a7SJames Smart 	/*
812d613b6a7SJames Smart 	 * The calling sequence should be:
813d613b6a7SJames Smart 	 * nvmet_fc_rcv_ls_req -> lpfc_nvmet_xmt_ls_rsp/cmp ->_req->done
814d613b6a7SJames Smart 	 * lpfc_nvmet_xmt_ls_rsp_cmp should free the allocated ctxp.
815d613b6a7SJames Smart 	 */
816d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_in);
817d613b6a7SJames Smart 	rc = nvmet_fc_rcv_ls_req(phba->targetport, &ctxp->ctx.ls_req,
818d613b6a7SJames Smart 				 payload, size);
819d613b6a7SJames Smart 
820d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
821d613b6a7SJames Smart 			"6037 %s: ctx %p sz %d rc %d: %08x %08x %08x "
822d613b6a7SJames Smart 			"%08x %08x %08x\n", __func__, ctxp, size, rc,
823d613b6a7SJames Smart 			*payload, *(payload+1), *(payload+2),
824d613b6a7SJames Smart 			*(payload+3), *(payload+4), *(payload+5));
8252b65e182SJames Smart 
826d613b6a7SJames Smart 	if (rc == 0) {
827d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_ls_req_out);
828d613b6a7SJames Smart 		return;
829d613b6a7SJames Smart 	}
8302b65e182SJames Smart 
8312b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET LS  DROP: xri x%x sz %d from %06x\n",
8322b65e182SJames Smart 			 oxid, size, sid);
8332b65e182SJames Smart 
834d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_ls_req_drop);
835d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
836d613b6a7SJames Smart 			"6156 LS Drop IO x%x: nvmet_fc_rcv_ls_req %d\n",
837d613b6a7SJames Smart 			ctxp->oxid, rc);
838d613b6a7SJames Smart 
839d613b6a7SJames Smart 	/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
840d613b6a7SJames Smart 	if (nvmebuf)
841d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
842d613b6a7SJames Smart 
843d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_ls_abort);
844d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, sid, oxid);
845d613b6a7SJames Smart }
846d613b6a7SJames Smart 
847d613b6a7SJames Smart /**
848d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
849d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
850d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
851d613b6a7SJames Smart  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
852d613b6a7SJames Smart  *
853d613b6a7SJames Smart  * This routine is used for processing the WQE associated with a unsolicited
854d613b6a7SJames Smart  * event. It first determines whether there is an existing ndlp that matches
855d613b6a7SJames Smart  * the DID from the unsolicited WQE. If not, it will create a new one with
856d613b6a7SJames Smart  * the DID from the unsolicited WQE. The ELS command from the unsolicited
857d613b6a7SJames Smart  * WQE is then used to invoke the proper routine and to set up proper state
858d613b6a7SJames Smart  * of the discovery state machine.
859d613b6a7SJames Smart  **/
860d613b6a7SJames Smart static void
861d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
862d613b6a7SJames Smart 			    struct lpfc_sli_ring *pring,
863d613b6a7SJames Smart 			    struct rqb_dmabuf *nvmebuf,
864d613b6a7SJames Smart 			    uint64_t isr_timestamp)
865d613b6a7SJames Smart {
866d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
867d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
868d613b6a7SJames Smart 	struct fc_frame_header *fc_hdr;
869d613b6a7SJames Smart 	uint32_t *payload;
870d613b6a7SJames Smart 	uint32_t size, oxid, sid, rc;
8712b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
8722b65e182SJames Smart 	uint32_t id;
8732b65e182SJames Smart #endif
874d613b6a7SJames Smart 
875d613b6a7SJames Smart 	if (!nvmebuf || !phba->targetport) {
876d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
877d613b6a7SJames Smart 				"6157 FCP Drop IO\n");
8782b65e182SJames Smart 		oxid = 0;
8792b65e182SJames Smart 		size = 0;
8802b65e182SJames Smart 		sid = 0;
881d613b6a7SJames Smart 		goto dropit;
882d613b6a7SJames Smart 	}
883d613b6a7SJames Smart 
884d613b6a7SJames Smart 
885d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
886d613b6a7SJames Smart 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
887d613b6a7SJames Smart 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
888d613b6a7SJames Smart 	size = nvmebuf->bytes_recv;
889d613b6a7SJames Smart 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
890d613b6a7SJames Smart 	sid = sli4_sid_from_fc_hdr(fc_hdr);
891d613b6a7SJames Smart 
892d613b6a7SJames Smart 	ctxp = (struct lpfc_nvmet_rcv_ctx *)nvmebuf->context;
893d613b6a7SJames Smart 	if (ctxp == NULL) {
894d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
895d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
896d613b6a7SJames Smart 				"6158 FCP Drop IO x%x: Alloc\n",
897d613b6a7SJames Smart 				oxid);
898d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, NULL, &nvmebuf->hbuf);
899d613b6a7SJames Smart 		/* Cannot send ABTS without context */
900d613b6a7SJames Smart 		return;
901d613b6a7SJames Smart 	}
902d613b6a7SJames Smart 	memset(ctxp, 0, sizeof(ctxp->ctx));
903d613b6a7SJames Smart 	ctxp->wqeq = NULL;
904d613b6a7SJames Smart 	ctxp->txrdy = NULL;
905d613b6a7SJames Smart 	ctxp->offset = 0;
906d613b6a7SJames Smart 	ctxp->phba = phba;
907d613b6a7SJames Smart 	ctxp->size = size;
908d613b6a7SJames Smart 	ctxp->oxid = oxid;
909d613b6a7SJames Smart 	ctxp->sid = sid;
910d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_RCV;
911d613b6a7SJames Smart 	ctxp->rqb_buffer = nvmebuf;
912d613b6a7SJames Smart 	ctxp->entry_cnt = 1;
913d613b6a7SJames Smart 	ctxp->flag = 0;
914d613b6a7SJames Smart 
9152b65e182SJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
9162b65e182SJames Smart 	if (phba->ktime_on) {
9172b65e182SJames Smart 		ctxp->ts_isr_cmd = isr_timestamp;
9182b65e182SJames Smart 		ctxp->ts_cmd_nvme = ktime_get_ns();
9192b65e182SJames Smart 		ctxp->ts_nvme_data = 0;
9202b65e182SJames Smart 		ctxp->ts_data_wqput = 0;
9212b65e182SJames Smart 		ctxp->ts_isr_data = 0;
9222b65e182SJames Smart 		ctxp->ts_data_nvme = 0;
9232b65e182SJames Smart 		ctxp->ts_nvme_status = 0;
9242b65e182SJames Smart 		ctxp->ts_status_wqput = 0;
9252b65e182SJames Smart 		ctxp->ts_isr_status = 0;
9262b65e182SJames Smart 		ctxp->ts_status_nvme = 0;
9272b65e182SJames Smart 	}
9282b65e182SJames Smart 
9292b65e182SJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_RCV) {
9302b65e182SJames Smart 		id = smp_processor_id();
9312b65e182SJames Smart 		if (id < LPFC_CHECK_CPU_CNT)
9322b65e182SJames Smart 			phba->cpucheck_rcv_io[id]++;
9332b65e182SJames Smart 	}
9342b65e182SJames Smart #endif
9352b65e182SJames Smart 
9362b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d from %06x\n",
9372b65e182SJames Smart 			 oxid, size, sid);
9382b65e182SJames Smart 
939d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
940d613b6a7SJames Smart 	/*
941d613b6a7SJames Smart 	 * The calling sequence should be:
942d613b6a7SJames Smart 	 * nvmet_fc_rcv_fcp_req -> lpfc_nvmet_xmt_fcp_op/cmp -> req->done
943d613b6a7SJames Smart 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
944d613b6a7SJames Smart 	 */
945d613b6a7SJames Smart 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->ctx.fcp_req,
946d613b6a7SJames Smart 				  payload, size);
947d613b6a7SJames Smart 
948d613b6a7SJames Smart 	/* Process FCP command */
949d613b6a7SJames Smart 	if (rc == 0) {
950d613b6a7SJames Smart 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
951d613b6a7SJames Smart 		return;
952d613b6a7SJames Smart 	}
953d613b6a7SJames Smart 
954d613b6a7SJames Smart 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
955d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
956d613b6a7SJames Smart 			"6159 FCP Drop IO x%x: nvmet_fc_rcv_fcp_req x%x\n",
957d613b6a7SJames Smart 			ctxp->oxid, rc);
958d613b6a7SJames Smart dropit:
9592b65e182SJames Smart 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
9602b65e182SJames Smart 			 oxid, size, sid);
961d613b6a7SJames Smart 	if (oxid) {
962d613b6a7SJames Smart 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
963d613b6a7SJames Smart 		return;
964d613b6a7SJames Smart 	}
965d613b6a7SJames Smart 
966d613b6a7SJames Smart 	if (nvmebuf) {
967d613b6a7SJames Smart 		nvmebuf->iocbq->hba_wqidx = 0;
968d613b6a7SJames Smart 		/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
969d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, NULL, &nvmebuf->hbuf);
970d613b6a7SJames Smart 	}
971d613b6a7SJames Smart }
972d613b6a7SJames Smart 
973d613b6a7SJames Smart /**
974d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_event - Process an unsolicited event from an nvme nport
975d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
976d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
977d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
978d613b6a7SJames Smart  *
979d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
980d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
981d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
982d613b6a7SJames Smart  * lpfc_nvmet_unsol_ls_buffer() after properly set up the buffer from the
983d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
984d613b6a7SJames Smart  **/
985d613b6a7SJames Smart void
986d613b6a7SJames Smart lpfc_nvmet_unsol_ls_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
987d613b6a7SJames Smart 			  struct lpfc_iocbq *piocb)
988d613b6a7SJames Smart {
989d613b6a7SJames Smart 	struct lpfc_dmabuf *d_buf;
990d613b6a7SJames Smart 	struct hbq_dmabuf *nvmebuf;
991d613b6a7SJames Smart 
992d613b6a7SJames Smart 	d_buf = piocb->context2;
993d613b6a7SJames Smart 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
994d613b6a7SJames Smart 
995d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
996d613b6a7SJames Smart 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
997d613b6a7SJames Smart 		return;
998d613b6a7SJames Smart 	}
999d613b6a7SJames Smart 	lpfc_nvmet_unsol_ls_buffer(phba, pring, nvmebuf);
1000d613b6a7SJames Smart }
1001d613b6a7SJames Smart 
1002d613b6a7SJames Smart /**
1003d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
1004d613b6a7SJames Smart  * @phba: pointer to lpfc hba data structure.
1005d613b6a7SJames Smart  * @pring: pointer to a SLI ring.
1006d613b6a7SJames Smart  * @nvmebuf: pointer to received nvme data structure.
1007d613b6a7SJames Smart  *
1008d613b6a7SJames Smart  * This routine is used to process an unsolicited event received from a SLI
1009d613b6a7SJames Smart  * (Service Level Interface) ring. The actual processing of the data buffer
1010d613b6a7SJames Smart  * associated with the unsolicited event is done by invoking the routine
1011d613b6a7SJames Smart  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
1012d613b6a7SJames Smart  * SLI RQ on which the unsolicited event was received.
1013d613b6a7SJames Smart  **/
1014d613b6a7SJames Smart void
1015d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
1016d613b6a7SJames Smart 			   struct lpfc_sli_ring *pring,
1017d613b6a7SJames Smart 			   struct rqb_dmabuf *nvmebuf,
1018d613b6a7SJames Smart 			   uint64_t isr_timestamp)
1019d613b6a7SJames Smart {
1020d613b6a7SJames Smart 	if (phba->nvmet_support == 0) {
1021d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, NULL, &nvmebuf->hbuf);
1022d613b6a7SJames Smart 		return;
1023d613b6a7SJames Smart 	}
1024d613b6a7SJames Smart 	lpfc_nvmet_unsol_fcp_buffer(phba, pring, nvmebuf,
1025d613b6a7SJames Smart 				    isr_timestamp);
1026d613b6a7SJames Smart }
1027d613b6a7SJames Smart 
1028d613b6a7SJames Smart /**
1029d613b6a7SJames Smart  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
1030d613b6a7SJames Smart  * @phba: pointer to a host N_Port data structure.
1031d613b6a7SJames Smart  * @ctxp: Context info for NVME LS Request
1032d613b6a7SJames Smart  * @rspbuf: DMA buffer of NVME command.
1033d613b6a7SJames Smart  * @rspsize: size of the NVME command.
1034d613b6a7SJames Smart  *
1035d613b6a7SJames Smart  * This routine is used for allocating a lpfc-WQE data structure from
1036d613b6a7SJames Smart  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
1037d613b6a7SJames Smart  * passed into the routine for discovery state machine to issue an Extended
1038d613b6a7SJames Smart  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
1039d613b6a7SJames Smart  * and preparation routine that is used by all the discovery state machine
1040d613b6a7SJames Smart  * routines and the NVME command-specific fields will be later set up by
1041d613b6a7SJames Smart  * the individual discovery machine routines after calling this routine
1042d613b6a7SJames Smart  * allocating and preparing a generic WQE data structure. It fills in the
1043d613b6a7SJames Smart  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
1044d613b6a7SJames Smart  * payload and response payload (if expected). The reference count on the
1045d613b6a7SJames Smart  * ndlp is incremented by 1 and the reference to the ndlp is put into
1046d613b6a7SJames Smart  * context1 of the WQE data structure for this WQE to hold the ndlp
1047d613b6a7SJames Smart  * reference for the command's callback function to access later.
1048d613b6a7SJames Smart  *
1049d613b6a7SJames Smart  * Return code
1050d613b6a7SJames Smart  *   Pointer to the newly allocated/prepared nvme wqe data structure
1051d613b6a7SJames Smart  *   NULL - when nvme wqe data structure allocation/preparation failed
1052d613b6a7SJames Smart  **/
1053d613b6a7SJames Smart static struct lpfc_iocbq *
1054d613b6a7SJames Smart lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
1055d613b6a7SJames Smart 		       struct lpfc_nvmet_rcv_ctx *ctxp,
1056d613b6a7SJames Smart 		       dma_addr_t rspbuf, uint16_t rspsize)
1057d613b6a7SJames Smart {
1058d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1059d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
1060d613b6a7SJames Smart 	union lpfc_wqe *wqe;
1061d613b6a7SJames Smart 
1062d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
1063d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1064d613b6a7SJames Smart 				"6104 lpfc_nvmet_prep_ls_wqe: link err: "
1065d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1066d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1067d613b6a7SJames Smart 		return NULL;
1068d613b6a7SJames Smart 	}
1069d613b6a7SJames Smart 
1070d613b6a7SJames Smart 	/* Allocate buffer for  command wqe */
1071d613b6a7SJames Smart 	nvmewqe = lpfc_sli_get_iocbq(phba);
1072d613b6a7SJames Smart 	if (nvmewqe == NULL) {
1073d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1074d613b6a7SJames Smart 				"6105 lpfc_nvmet_prep_ls_wqe: No WQE: "
1075d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1076d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1077d613b6a7SJames Smart 		return NULL;
1078d613b6a7SJames Smart 	}
1079d613b6a7SJames Smart 
1080d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1081d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1082d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1083d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1084d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1085d613b6a7SJames Smart 				"6106 lpfc_nvmet_prep_ls_wqe: No ndlp: "
1086d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1087d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1088d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
1089d613b6a7SJames Smart 	}
1090d613b6a7SJames Smart 	ctxp->wqeq = nvmewqe;
1091d613b6a7SJames Smart 
1092d613b6a7SJames Smart 	/* prevent preparing wqe with NULL ndlp reference */
1093d613b6a7SJames Smart 	nvmewqe->context1 = lpfc_nlp_get(ndlp);
1094d613b6a7SJames Smart 	if (nvmewqe->context1 == NULL)
1095d613b6a7SJames Smart 		goto nvme_wqe_free_wqeq_exit;
1096d613b6a7SJames Smart 	nvmewqe->context2 = ctxp;
1097d613b6a7SJames Smart 
1098d613b6a7SJames Smart 	wqe = &nvmewqe->wqe;
1099d613b6a7SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
1100d613b6a7SJames Smart 
1101d613b6a7SJames Smart 	/* Words 0 - 2 */
1102d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1103d613b6a7SJames Smart 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
1104d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
1105d613b6a7SJames Smart 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
1106d613b6a7SJames Smart 
1107d613b6a7SJames Smart 	/* Word 3 */
1108d613b6a7SJames Smart 
1109d613b6a7SJames Smart 	/* Word 4 */
1110d613b6a7SJames Smart 
1111d613b6a7SJames Smart 	/* Word 5 */
1112d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
1113d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
1114d613b6a7SJames Smart 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
1115d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_DD_SOL_CTL);
1116d613b6a7SJames Smart 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
1117d613b6a7SJames Smart 
1118d613b6a7SJames Smart 	/* Word 6 */
1119d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
1120d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1121d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
1122d613b6a7SJames Smart 
1123d613b6a7SJames Smart 	/* Word 7 */
1124d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
1125d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
1126d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
1127d613b6a7SJames Smart 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
1128d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
1129d613b6a7SJames Smart 
1130d613b6a7SJames Smart 	/* Word 8 */
1131d613b6a7SJames Smart 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
1132d613b6a7SJames Smart 
1133d613b6a7SJames Smart 	/* Word 9 */
1134d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
1135d613b6a7SJames Smart 	/* Needs to be set by caller */
1136d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
1137d613b6a7SJames Smart 
1138d613b6a7SJames Smart 	/* Word 10 */
1139d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
1140d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
1141d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
1142d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
1143d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
1144d613b6a7SJames Smart 
1145d613b6a7SJames Smart 	/* Word 11 */
1146d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
1147d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
1148d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
1149d613b6a7SJames Smart 	       OTHER_COMMAND);
1150d613b6a7SJames Smart 
1151d613b6a7SJames Smart 	/* Word 12 */
1152d613b6a7SJames Smart 	wqe->xmit_sequence.xmit_len = rspsize;
1153d613b6a7SJames Smart 
1154d613b6a7SJames Smart 	nvmewqe->retry = 1;
1155d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
1156d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
1157d613b6a7SJames Smart 	nvmewqe->iocb_flag |= LPFC_IO_NVME_LS;
1158d613b6a7SJames Smart 
1159d613b6a7SJames Smart 	/* Xmit NVME response to remote NPORT <did> */
1160d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1161d613b6a7SJames Smart 			"6039 Xmit NVME LS response to remote "
1162d613b6a7SJames Smart 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
1163d613b6a7SJames Smart 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
1164d613b6a7SJames Smart 			rspsize);
1165d613b6a7SJames Smart 	return nvmewqe;
1166d613b6a7SJames Smart 
1167d613b6a7SJames Smart nvme_wqe_free_wqeq_exit:
1168d613b6a7SJames Smart 	nvmewqe->context2 = NULL;
1169d613b6a7SJames Smart 	nvmewqe->context3 = NULL;
1170d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, nvmewqe);
1171d613b6a7SJames Smart 	return NULL;
1172d613b6a7SJames Smart }
1173d613b6a7SJames Smart 
1174d613b6a7SJames Smart 
1175d613b6a7SJames Smart static struct lpfc_iocbq *
1176d613b6a7SJames Smart lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
1177d613b6a7SJames Smart 			struct lpfc_nvmet_rcv_ctx *ctxp)
1178d613b6a7SJames Smart {
1179d613b6a7SJames Smart 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->ctx.fcp_req;
1180d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1181d613b6a7SJames Smart 	struct sli4_sge *sgl;
1182d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1183d613b6a7SJames Smart 	struct lpfc_iocbq *nvmewqe;
1184d613b6a7SJames Smart 	struct scatterlist *sgel;
1185d613b6a7SJames Smart 	union lpfc_wqe128 *wqe;
1186d613b6a7SJames Smart 	uint32_t *txrdy;
1187d613b6a7SJames Smart 	dma_addr_t physaddr;
1188d613b6a7SJames Smart 	int i, cnt;
1189d613b6a7SJames Smart 	int xc = 1;
1190d613b6a7SJames Smart 
1191d613b6a7SJames Smart 	if (!lpfc_is_link_up(phba)) {
1192d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1193d613b6a7SJames Smart 				"6107 lpfc_nvmet_prep_fcp_wqe: link err:"
1194d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n", ctxp->sid,
1195d613b6a7SJames Smart 				ctxp->oxid);
1196d613b6a7SJames Smart 		return NULL;
1197d613b6a7SJames Smart 	}
1198d613b6a7SJames Smart 
1199d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1200d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1201d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1202d613b6a7SJames Smart 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1203d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1204d613b6a7SJames Smart 				"6108 lpfc_nvmet_prep_fcp_wqe: no ndlp: "
1205d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1206d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1207d613b6a7SJames Smart 		return NULL;
1208d613b6a7SJames Smart 	}
1209d613b6a7SJames Smart 
1210d613b6a7SJames Smart 	if (rsp->sg_cnt > phba->cfg_sg_seg_cnt) {
1211d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1212d613b6a7SJames Smart 				"6109 lpfc_nvmet_prep_fcp_wqe: seg cnt err: "
1213d613b6a7SJames Smart 				"NPORT x%x oxid:x%x\n",
1214d613b6a7SJames Smart 				ctxp->sid, ctxp->oxid);
1215d613b6a7SJames Smart 		return NULL;
1216d613b6a7SJames Smart 	}
1217d613b6a7SJames Smart 
1218d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1219d613b6a7SJames Smart 	nvmewqe = ctxp->wqeq;
1220d613b6a7SJames Smart 	if (nvmewqe == NULL) {
1221d613b6a7SJames Smart 		/* Allocate buffer for  command wqe */
1222d613b6a7SJames Smart 		nvmewqe = ctxp->rqb_buffer->iocbq;
1223d613b6a7SJames Smart 		if (nvmewqe == NULL) {
1224d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1225d613b6a7SJames Smart 					"6110 lpfc_nvmet_prep_fcp_wqe: No "
1226d613b6a7SJames Smart 					"WQE: NPORT x%x oxid:x%x\n",
1227d613b6a7SJames Smart 					ctxp->sid, ctxp->oxid);
1228d613b6a7SJames Smart 			return NULL;
1229d613b6a7SJames Smart 		}
1230d613b6a7SJames Smart 		ctxp->wqeq = nvmewqe;
1231d613b6a7SJames Smart 		xc = 0; /* create new XRI */
1232d613b6a7SJames Smart 		nvmewqe->sli4_lxritag = NO_XRI;
1233d613b6a7SJames Smart 		nvmewqe->sli4_xritag = NO_XRI;
1234d613b6a7SJames Smart 	}
1235d613b6a7SJames Smart 
1236d613b6a7SJames Smart 	/* Sanity check */
1237d613b6a7SJames Smart 	if (((ctxp->state == LPFC_NVMET_STE_RCV) &&
1238d613b6a7SJames Smart 	    (ctxp->entry_cnt == 1)) ||
1239d613b6a7SJames Smart 	    ((ctxp->state == LPFC_NVMET_STE_DATA) &&
1240d613b6a7SJames Smart 	    (ctxp->entry_cnt > 1))) {
1241d613b6a7SJames Smart 		wqe = (union lpfc_wqe128 *)&nvmewqe->wqe;
1242d613b6a7SJames Smart 	} else {
1243d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1244d613b6a7SJames Smart 				"6111 Wrong state %s: %d  cnt %d\n",
1245d613b6a7SJames Smart 				__func__, ctxp->state, ctxp->entry_cnt);
1246d613b6a7SJames Smart 		return NULL;
1247d613b6a7SJames Smart 	}
1248d613b6a7SJames Smart 
1249d613b6a7SJames Smart 	sgl  = (struct sli4_sge *)ctxp->rqb_buffer->sglq->sgl;
1250d613b6a7SJames Smart 	switch (rsp->op) {
1251d613b6a7SJames Smart 	case NVMET_FCOP_READDATA:
1252d613b6a7SJames Smart 	case NVMET_FCOP_READDATA_RSP:
1253d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
1254d613b6a7SJames Smart 		sgel = &rsp->sg[0];
1255d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
1256d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1257d613b6a7SJames Smart 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
1258d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
1259d613b6a7SJames Smart 		wqe->fcp_tsend.bde.addrHigh =
1260d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
1261d613b6a7SJames Smart 
1262d613b6a7SJames Smart 		/* Word 3 */
1263d613b6a7SJames Smart 		wqe->fcp_tsend.payload_offset_len = 0;
1264d613b6a7SJames Smart 
1265d613b6a7SJames Smart 		/* Word 4 */
1266d613b6a7SJames Smart 		wqe->fcp_tsend.relative_offset = ctxp->offset;
1267d613b6a7SJames Smart 
1268d613b6a7SJames Smart 		/* Word 5 */
1269d613b6a7SJames Smart 
1270d613b6a7SJames Smart 		/* Word 6 */
1271d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
1272d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1273d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
1274d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
1275d613b6a7SJames Smart 
1276d613b6a7SJames Smart 		/* Word 7 */
1277d613b6a7SJames Smart 		bf_set(wqe_cmnd, &wqe->fcp_tsend.wqe_com, CMD_FCP_TSEND64_WQE);
1278d613b6a7SJames Smart 
1279d613b6a7SJames Smart 		/* Word 8 */
1280d613b6a7SJames Smart 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
1281d613b6a7SJames Smart 
1282d613b6a7SJames Smart 		/* Word 9 */
1283d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
1284d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
1285d613b6a7SJames Smart 
1286d613b6a7SJames Smart 		/* Word 10 */
1287d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
1288d613b6a7SJames Smart 		bf_set(wqe_dbde, &wqe->fcp_tsend.wqe_com, 1);
1289d613b6a7SJames Smart 		bf_set(wqe_iod, &wqe->fcp_tsend.wqe_com, LPFC_WQE_IOD_WRITE);
1290d613b6a7SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_tsend.wqe_com,
1291d613b6a7SJames Smart 		       LPFC_WQE_LENLOC_WORD12);
1292d613b6a7SJames Smart 		bf_set(wqe_ebde_cnt, &wqe->fcp_tsend.wqe_com, 0);
1293d613b6a7SJames Smart 		bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, xc);
1294d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
1295d613b6a7SJames Smart 		if (phba->cfg_nvme_oas)
1296d613b6a7SJames Smart 			bf_set(wqe_oas, &wqe->fcp_tsend.wqe_com, 1);
1297d613b6a7SJames Smart 
1298d613b6a7SJames Smart 		/* Word 11 */
1299d613b6a7SJames Smart 		bf_set(wqe_cqid, &wqe->fcp_tsend.wqe_com,
1300d613b6a7SJames Smart 		       LPFC_WQE_CQ_ID_DEFAULT);
1301d613b6a7SJames Smart 		bf_set(wqe_cmd_type, &wqe->fcp_tsend.wqe_com,
1302d613b6a7SJames Smart 		       FCP_COMMAND_TSEND);
1303d613b6a7SJames Smart 
1304d613b6a7SJames Smart 		/* Word 12 */
1305d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
1306d613b6a7SJames Smart 
1307d613b6a7SJames Smart 		/* Setup 2 SKIP SGEs */
1308d613b6a7SJames Smart 		sgl->addr_hi = 0;
1309d613b6a7SJames Smart 		sgl->addr_lo = 0;
1310d613b6a7SJames Smart 		sgl->word2 = 0;
1311d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1312d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1313d613b6a7SJames Smart 		sgl->sge_len = 0;
1314d613b6a7SJames Smart 		sgl++;
1315d613b6a7SJames Smart 		sgl->addr_hi = 0;
1316d613b6a7SJames Smart 		sgl->addr_lo = 0;
1317d613b6a7SJames Smart 		sgl->word2 = 0;
1318d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1319d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1320d613b6a7SJames Smart 		sgl->sge_len = 0;
1321d613b6a7SJames Smart 		sgl++;
1322d613b6a7SJames Smart 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
1323d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
1324d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 1);
1325d613b6a7SJames Smart 			if ((ndlp->nlp_flag & NLP_SUPPRESS_RSP) &&
1326d613b6a7SJames Smart 			    (rsp->rsplen == 12)) {
1327d613b6a7SJames Smart 				bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 1);
1328d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
1329d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
1330d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
1331d613b6a7SJames Smart 			} else {
1332d613b6a7SJames Smart 				bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1333d613b6a7SJames Smart 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
1334d613b6a7SJames Smart 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
1335d613b6a7SJames Smart 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
1336d613b6a7SJames Smart 				       ((rsp->rsplen >> 2) - 1));
1337d613b6a7SJames Smart 				memcpy(&wqe->words[16], rsp->rspaddr,
1338d613b6a7SJames Smart 				       rsp->rsplen);
1339d613b6a7SJames Smart 			}
1340d613b6a7SJames Smart 		} else {
1341d613b6a7SJames Smart 			atomic_inc(&tgtp->xmt_fcp_read);
1342d613b6a7SJames Smart 
1343d613b6a7SJames Smart 			bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1344d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
1345d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
1346d613b6a7SJames Smart 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
1347d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
1348d613b6a7SJames Smart 		}
1349d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DATA;
1350d613b6a7SJames Smart 		break;
1351d613b6a7SJames Smart 
1352d613b6a7SJames Smart 	case NVMET_FCOP_WRITEDATA:
1353d613b6a7SJames Smart 		/* Words 0 - 2 : The first sg segment */
1354d613b6a7SJames Smart 		txrdy = pci_pool_alloc(phba->txrdy_payload_pool,
1355d613b6a7SJames Smart 				       GFP_KERNEL, &physaddr);
1356d613b6a7SJames Smart 		if (!txrdy) {
1357d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1358d613b6a7SJames Smart 					"6041 Bad txrdy buffer: oxid x%x\n",
1359d613b6a7SJames Smart 					ctxp->oxid);
1360d613b6a7SJames Smart 			return NULL;
1361d613b6a7SJames Smart 		}
1362d613b6a7SJames Smart 		ctxp->txrdy = txrdy;
1363d613b6a7SJames Smart 		ctxp->txrdy_phys = physaddr;
1364d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1365d613b6a7SJames Smart 		wqe->fcp_treceive.bde.tus.f.bdeSize = TXRDY_PAYLOAD_LEN;
1366d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrLow =
1367d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
1368d613b6a7SJames Smart 		wqe->fcp_treceive.bde.addrHigh =
1369d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
1370d613b6a7SJames Smart 
1371d613b6a7SJames Smart 		/* Word 3 */
1372d613b6a7SJames Smart 		wqe->fcp_treceive.payload_offset_len = TXRDY_PAYLOAD_LEN;
1373d613b6a7SJames Smart 
1374d613b6a7SJames Smart 		/* Word 4 */
1375d613b6a7SJames Smart 		wqe->fcp_treceive.relative_offset = ctxp->offset;
1376d613b6a7SJames Smart 
1377d613b6a7SJames Smart 		/* Word 5 */
1378d613b6a7SJames Smart 
1379d613b6a7SJames Smart 		/* Word 6 */
1380d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
1381d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1382d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
1383d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
1384d613b6a7SJames Smart 
1385d613b6a7SJames Smart 		/* Word 7 */
1386d613b6a7SJames Smart 		bf_set(wqe_ar, &wqe->fcp_treceive.wqe_com, 0);
1387d613b6a7SJames Smart 		bf_set(wqe_cmnd, &wqe->fcp_treceive.wqe_com,
1388d613b6a7SJames Smart 		       CMD_FCP_TRECEIVE64_WQE);
1389d613b6a7SJames Smart 
1390d613b6a7SJames Smart 		/* Word 8 */
1391d613b6a7SJames Smart 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
1392d613b6a7SJames Smart 
1393d613b6a7SJames Smart 		/* Word 9 */
1394d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
1395d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
1396d613b6a7SJames Smart 
1397d613b6a7SJames Smart 		/* Word 10 */
1398d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
1399d613b6a7SJames Smart 		bf_set(wqe_dbde, &wqe->fcp_treceive.wqe_com, 1);
1400d613b6a7SJames Smart 		bf_set(wqe_iod, &wqe->fcp_treceive.wqe_com, LPFC_WQE_IOD_READ);
1401d613b6a7SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_treceive.wqe_com,
1402d613b6a7SJames Smart 		       LPFC_WQE_LENLOC_WORD12);
1403d613b6a7SJames Smart 		bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, xc);
1404d613b6a7SJames Smart 		bf_set(wqe_wqes, &wqe->fcp_treceive.wqe_com, 0);
1405d613b6a7SJames Smart 		bf_set(wqe_irsp, &wqe->fcp_treceive.wqe_com, 0);
1406d613b6a7SJames Smart 		bf_set(wqe_irsplen, &wqe->fcp_treceive.wqe_com, 0);
1407d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
1408d613b6a7SJames Smart 		if (phba->cfg_nvme_oas)
1409d613b6a7SJames Smart 			bf_set(wqe_oas, &wqe->fcp_treceive.wqe_com, 1);
1410d613b6a7SJames Smart 
1411d613b6a7SJames Smart 		/* Word 11 */
1412d613b6a7SJames Smart 		bf_set(wqe_cqid, &wqe->fcp_treceive.wqe_com,
1413d613b6a7SJames Smart 		       LPFC_WQE_CQ_ID_DEFAULT);
1414d613b6a7SJames Smart 		bf_set(wqe_cmd_type, &wqe->fcp_treceive.wqe_com,
1415d613b6a7SJames Smart 		       FCP_COMMAND_TRECEIVE);
1416d613b6a7SJames Smart 		bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1417d613b6a7SJames Smart 
1418d613b6a7SJames Smart 		/* Word 12 */
1419d613b6a7SJames Smart 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
1420d613b6a7SJames Smart 
1421d613b6a7SJames Smart 		/* Setup 1 TXRDY and 1 SKIP SGE */
1422d613b6a7SJames Smart 		txrdy[0] = 0;
1423d613b6a7SJames Smart 		txrdy[1] = cpu_to_be32(rsp->transfer_length);
1424d613b6a7SJames Smart 		txrdy[2] = 0;
1425d613b6a7SJames Smart 
1426d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
1427d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
1428d613b6a7SJames Smart 		sgl->word2 = 0;
1429d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1430d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1431d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(TXRDY_PAYLOAD_LEN);
1432d613b6a7SJames Smart 		sgl++;
1433d613b6a7SJames Smart 		sgl->addr_hi = 0;
1434d613b6a7SJames Smart 		sgl->addr_lo = 0;
1435d613b6a7SJames Smart 		sgl->word2 = 0;
1436d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1437d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1438d613b6a7SJames Smart 		sgl->sge_len = 0;
1439d613b6a7SJames Smart 		sgl++;
1440d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DATA;
1441d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_write);
1442d613b6a7SJames Smart 		break;
1443d613b6a7SJames Smart 
1444d613b6a7SJames Smart 	case NVMET_FCOP_RSP:
1445d613b6a7SJames Smart 		/* Words 0 - 2 */
1446d613b6a7SJames Smart 		sgel = &rsp->sg[0];
1447d613b6a7SJames Smart 		physaddr = rsp->rspdma;
1448d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1449d613b6a7SJames Smart 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
1450d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrLow =
1451d613b6a7SJames Smart 			cpu_to_le32(putPaddrLow(physaddr));
1452d613b6a7SJames Smart 		wqe->fcp_trsp.bde.addrHigh =
1453d613b6a7SJames Smart 			cpu_to_le32(putPaddrHigh(physaddr));
1454d613b6a7SJames Smart 
1455d613b6a7SJames Smart 		/* Word 3 */
1456d613b6a7SJames Smart 		wqe->fcp_trsp.response_len = rsp->rsplen;
1457d613b6a7SJames Smart 
1458d613b6a7SJames Smart 		/* Word 4 */
1459d613b6a7SJames Smart 		wqe->fcp_trsp.rsvd_4_5[0] = 0;
1460d613b6a7SJames Smart 
1461d613b6a7SJames Smart 
1462d613b6a7SJames Smart 		/* Word 5 */
1463d613b6a7SJames Smart 
1464d613b6a7SJames Smart 		/* Word 6 */
1465d613b6a7SJames Smart 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
1466d613b6a7SJames Smart 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1467d613b6a7SJames Smart 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
1468d613b6a7SJames Smart 		       nvmewqe->sli4_xritag);
1469d613b6a7SJames Smart 
1470d613b6a7SJames Smart 		/* Word 7 */
1471d613b6a7SJames Smart 		bf_set(wqe_ag, &wqe->fcp_trsp.wqe_com, 1);
1472d613b6a7SJames Smart 		bf_set(wqe_cmnd, &wqe->fcp_trsp.wqe_com, CMD_FCP_TRSP64_WQE);
1473d613b6a7SJames Smart 
1474d613b6a7SJames Smart 		/* Word 8 */
1475d613b6a7SJames Smart 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
1476d613b6a7SJames Smart 
1477d613b6a7SJames Smart 		/* Word 9 */
1478d613b6a7SJames Smart 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
1479d613b6a7SJames Smart 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
1480d613b6a7SJames Smart 
1481d613b6a7SJames Smart 		/* Word 10 */
1482d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
1483d613b6a7SJames Smart 		bf_set(wqe_dbde, &wqe->fcp_trsp.wqe_com, 0);
1484d613b6a7SJames Smart 		bf_set(wqe_iod, &wqe->fcp_trsp.wqe_com, LPFC_WQE_IOD_WRITE);
1485d613b6a7SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_trsp.wqe_com,
1486d613b6a7SJames Smart 		       LPFC_WQE_LENLOC_WORD3);
1487d613b6a7SJames Smart 		bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, xc);
1488d613b6a7SJames Smart 		bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
1489d613b6a7SJames Smart 		if (phba->cfg_nvme_oas)
1490d613b6a7SJames Smart 			bf_set(wqe_oas, &wqe->fcp_trsp.wqe_com, 1);
1491d613b6a7SJames Smart 
1492d613b6a7SJames Smart 		/* Word 11 */
1493d613b6a7SJames Smart 		bf_set(wqe_cqid, &wqe->fcp_trsp.wqe_com,
1494d613b6a7SJames Smart 		       LPFC_WQE_CQ_ID_DEFAULT);
1495d613b6a7SJames Smart 		bf_set(wqe_cmd_type, &wqe->fcp_trsp.wqe_com,
1496d613b6a7SJames Smart 		       FCP_COMMAND_TRSP);
1497d613b6a7SJames Smart 		bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
1498d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_RSP;
1499d613b6a7SJames Smart 
1500d613b6a7SJames Smart 		if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
1501d613b6a7SJames Smart 			/* Good response - all zero's on wire */
1502d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 0);
1503d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 0);
1504d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com, 0);
1505d613b6a7SJames Smart 		} else {
1506d613b6a7SJames Smart 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
1507d613b6a7SJames Smart 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
1508d613b6a7SJames Smart 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
1509d613b6a7SJames Smart 			       ((rsp->rsplen >> 2) - 1));
1510d613b6a7SJames Smart 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
1511d613b6a7SJames Smart 		}
1512d613b6a7SJames Smart 
1513d613b6a7SJames Smart 		/* Use rspbuf, NOT sg list */
1514d613b6a7SJames Smart 		rsp->sg_cnt = 0;
1515d613b6a7SJames Smart 		sgl->word2 = 0;
1516d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_fcp_rsp);
1517d613b6a7SJames Smart 		break;
1518d613b6a7SJames Smart 
1519d613b6a7SJames Smart 	default:
1520d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1521d613b6a7SJames Smart 				"6064 Unknown Rsp Op %d\n",
1522d613b6a7SJames Smart 				rsp->op);
1523d613b6a7SJames Smart 		return NULL;
1524d613b6a7SJames Smart 	}
1525d613b6a7SJames Smart 
1526d613b6a7SJames Smart 	nvmewqe->retry = 1;
1527d613b6a7SJames Smart 	nvmewqe->vport = phba->pport;
1528d613b6a7SJames Smart 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
1529d613b6a7SJames Smart 	nvmewqe->context1 = ndlp;
1530d613b6a7SJames Smart 
1531d613b6a7SJames Smart 	for (i = 0; i < rsp->sg_cnt; i++) {
1532d613b6a7SJames Smart 		sgel = &rsp->sg[i];
1533d613b6a7SJames Smart 		physaddr = sg_dma_address(sgel);
1534d613b6a7SJames Smart 		cnt = sg_dma_len(sgel);
1535d613b6a7SJames Smart 		sgl->addr_hi = putPaddrHigh(physaddr);
1536d613b6a7SJames Smart 		sgl->addr_lo = putPaddrLow(physaddr);
1537d613b6a7SJames Smart 		sgl->word2 = 0;
1538d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1539d613b6a7SJames Smart 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
1540d613b6a7SJames Smart 		if ((i+1) == rsp->sg_cnt)
1541d613b6a7SJames Smart 			bf_set(lpfc_sli4_sge_last, sgl, 1);
1542d613b6a7SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
1543d613b6a7SJames Smart 		sgl->sge_len = cpu_to_le32(cnt);
1544d613b6a7SJames Smart 		sgl++;
1545d613b6a7SJames Smart 		ctxp->offset += cnt;
1546d613b6a7SJames Smart 	}
1547d613b6a7SJames Smart 	return nvmewqe;
1548d613b6a7SJames Smart }
1549d613b6a7SJames Smart 
1550d613b6a7SJames Smart /**
1551d613b6a7SJames Smart  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
1552d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
1553d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1554d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
1555d613b6a7SJames Smart  *
1556d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
1557d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
1558d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
1559d613b6a7SJames Smart  **/
1560d613b6a7SJames Smart static void
1561d613b6a7SJames Smart lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1562d613b6a7SJames Smart 			     struct lpfc_wcqe_complete *wcqe)
1563d613b6a7SJames Smart {
1564d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1565d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1566d613b6a7SJames Smart 	uint32_t status, result;
1567d613b6a7SJames Smart 
1568d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
1569d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1570d613b6a7SJames Smart 	result = wcqe->parameter;
1571d613b6a7SJames Smart 
1572d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1573d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_cmpl);
1574d613b6a7SJames Smart 
1575d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
1576d613b6a7SJames Smart 			"6165 Abort cmpl: xri x%x WCQE: %08x %08x %08x %08x\n",
1577d613b6a7SJames Smart 			ctxp->oxid, wcqe->word0, wcqe->total_data_placed,
1578d613b6a7SJames Smart 			result, wcqe->word3);
1579d613b6a7SJames Smart 
1580d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_DONE;
1581d613b6a7SJames Smart 	lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
1582d613b6a7SJames Smart 
1583d613b6a7SJames Smart 	cmdwqe->context2 = NULL;
1584d613b6a7SJames Smart 	cmdwqe->context3 = NULL;
1585d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
1586d613b6a7SJames Smart }
1587d613b6a7SJames Smart 
1588d613b6a7SJames Smart /**
1589d613b6a7SJames Smart  * lpfc_nvmet_xmt_fcp_abort_cmp - Completion handler for ABTS
1590d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
1591d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1592d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
1593d613b6a7SJames Smart  *
1594d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
1595d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
1596d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
1597d613b6a7SJames Smart  **/
1598d613b6a7SJames Smart static void
1599d613b6a7SJames Smart lpfc_nvmet_xmt_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1600d613b6a7SJames Smart 			     struct lpfc_wcqe_complete *wcqe)
1601d613b6a7SJames Smart {
1602d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1603d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1604d613b6a7SJames Smart 	uint32_t status, result;
1605d613b6a7SJames Smart 
1606d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
1607d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1608d613b6a7SJames Smart 	result = wcqe->parameter;
1609d613b6a7SJames Smart 
1610d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1611d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_cmpl);
1612d613b6a7SJames Smart 
1613d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1614d613b6a7SJames Smart 			"6070 Abort cmpl: ctx %p WCQE: %08x %08x %08x %08x\n",
1615d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
1616d613b6a7SJames Smart 			result, wcqe->word3);
1617d613b6a7SJames Smart 
1618d613b6a7SJames Smart 	if (ctxp) {
1619d613b6a7SJames Smart 		/* Sanity check */
1620d613b6a7SJames Smart 		if (ctxp->state != LPFC_NVMET_STE_ABORT) {
1621d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
1622d613b6a7SJames Smart 					"6112 ABORT Wrong state:%d oxid x%x\n",
1623d613b6a7SJames Smart 					ctxp->state, ctxp->oxid);
1624d613b6a7SJames Smart 		}
1625d613b6a7SJames Smart 		ctxp->state = LPFC_NVMET_STE_DONE;
1626d613b6a7SJames Smart 		lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
1627d613b6a7SJames Smart 		cmdwqe->context2 = NULL;
1628d613b6a7SJames Smart 		cmdwqe->context3 = NULL;
1629d613b6a7SJames Smart 	}
1630d613b6a7SJames Smart }
1631d613b6a7SJames Smart 
1632d613b6a7SJames Smart /**
1633d613b6a7SJames Smart  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
1634d613b6a7SJames Smart  * @phba: Pointer to HBA context object.
1635d613b6a7SJames Smart  * @cmdwqe: Pointer to driver command WQE object.
1636d613b6a7SJames Smart  * @wcqe: Pointer to driver response CQE object.
1637d613b6a7SJames Smart  *
1638d613b6a7SJames Smart  * The function is called from SLI ring event handler with no
1639d613b6a7SJames Smart  * lock held. This function is the completion handler for NVME ABTS for LS cmds
1640d613b6a7SJames Smart  * The function frees memory resources used for the NVME commands.
1641d613b6a7SJames Smart  **/
1642d613b6a7SJames Smart static void
1643d613b6a7SJames Smart lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
1644d613b6a7SJames Smart 			    struct lpfc_wcqe_complete *wcqe)
1645d613b6a7SJames Smart {
1646d613b6a7SJames Smart 	struct lpfc_nvmet_rcv_ctx *ctxp;
1647d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1648d613b6a7SJames Smart 	uint32_t status, result;
1649d613b6a7SJames Smart 
1650d613b6a7SJames Smart 	ctxp = cmdwqe->context2;
1651d613b6a7SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
1652d613b6a7SJames Smart 	result = wcqe->parameter;
1653d613b6a7SJames Smart 
1654d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1655d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_cmpl);
1656d613b6a7SJames Smart 
1657d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1658d613b6a7SJames Smart 			"6083 Abort cmpl: ctx %p WCQE: %08x %08x %08x %08x\n",
1659d613b6a7SJames Smart 			ctxp, wcqe->word0, wcqe->total_data_placed,
1660d613b6a7SJames Smart 			result, wcqe->word3);
1661d613b6a7SJames Smart 
1662d613b6a7SJames Smart 	if (ctxp) {
1663d613b6a7SJames Smart 		cmdwqe->context2 = NULL;
1664d613b6a7SJames Smart 		cmdwqe->context3 = NULL;
1665d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
1666d613b6a7SJames Smart 		kfree(ctxp);
1667d613b6a7SJames Smart 	} else
1668d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, cmdwqe);
1669d613b6a7SJames Smart }
1670d613b6a7SJames Smart 
1671d613b6a7SJames Smart static int
1672d613b6a7SJames Smart lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
1673d613b6a7SJames Smart 			     struct lpfc_nvmet_rcv_ctx *ctxp,
1674d613b6a7SJames Smart 			     uint32_t sid, uint16_t xri)
1675d613b6a7SJames Smart {
1676d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1677d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
1678d613b6a7SJames Smart 	union lpfc_wqe *wqe_abts;
1679d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1680d613b6a7SJames Smart 
1681d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1682d613b6a7SJames Smart 			"6067 %s: Entrypoint: sid %x xri %x\n", __func__,
1683d613b6a7SJames Smart 			sid, xri);
1684d613b6a7SJames Smart 
1685d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1686d613b6a7SJames Smart 
1687d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
1688d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1689d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1690d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1691d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
1692d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1693d613b6a7SJames Smart 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
1694d613b6a7SJames Smart 				ndlp->nlp_state);
1695d613b6a7SJames Smart 
1696d613b6a7SJames Smart 		/* No failure to an ABTS request. */
1697d613b6a7SJames Smart 		return 0;
1698d613b6a7SJames Smart 	}
1699d613b6a7SJames Smart 
1700d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
1701d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
1702d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
1703d613b6a7SJames Smart 
1704d613b6a7SJames Smart 	/*
1705d613b6a7SJames Smart 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
1706d613b6a7SJames Smart 	 * that were initialized in lpfc_sli4_nvmet_alloc.
1707d613b6a7SJames Smart 	 */
1708d613b6a7SJames Smart 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
1709d613b6a7SJames Smart 
1710d613b6a7SJames Smart 	/* Word 5 */
1711d613b6a7SJames Smart 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
1712d613b6a7SJames Smart 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
1713d613b6a7SJames Smart 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
1714d613b6a7SJames Smart 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
1715d613b6a7SJames Smart 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
1716d613b6a7SJames Smart 
1717d613b6a7SJames Smart 	/* Word 6 */
1718d613b6a7SJames Smart 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
1719d613b6a7SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1720d613b6a7SJames Smart 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
1721d613b6a7SJames Smart 	       abts_wqeq->sli4_xritag);
1722d613b6a7SJames Smart 
1723d613b6a7SJames Smart 	/* Word 7 */
1724d613b6a7SJames Smart 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
1725d613b6a7SJames Smart 	       CMD_XMIT_SEQUENCE64_WQE);
1726d613b6a7SJames Smart 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
1727d613b6a7SJames Smart 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
1728d613b6a7SJames Smart 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
1729d613b6a7SJames Smart 
1730d613b6a7SJames Smart 	/* Word 8 */
1731d613b6a7SJames Smart 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
1732d613b6a7SJames Smart 
1733d613b6a7SJames Smart 	/* Word 9 */
1734d613b6a7SJames Smart 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
1735d613b6a7SJames Smart 	/* Needs to be set by caller */
1736d613b6a7SJames Smart 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
1737d613b6a7SJames Smart 
1738d613b6a7SJames Smart 	/* Word 10 */
1739d613b6a7SJames Smart 	bf_set(wqe_dbde, &wqe_abts->xmit_sequence.wqe_com, 1);
1740d613b6a7SJames Smart 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
1741d613b6a7SJames Smart 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
1742d613b6a7SJames Smart 	       LPFC_WQE_LENLOC_WORD12);
1743d613b6a7SJames Smart 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
1744d613b6a7SJames Smart 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
1745d613b6a7SJames Smart 
1746d613b6a7SJames Smart 	/* Word 11 */
1747d613b6a7SJames Smart 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
1748d613b6a7SJames Smart 	       LPFC_WQE_CQ_ID_DEFAULT);
1749d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
1750d613b6a7SJames Smart 	       OTHER_COMMAND);
1751d613b6a7SJames Smart 
1752d613b6a7SJames Smart 	abts_wqeq->vport = phba->pport;
1753d613b6a7SJames Smart 	abts_wqeq->context1 = ndlp;
1754d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
1755d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
1756d613b6a7SJames Smart 	abts_wqeq->rsvd2 = 0;
1757d613b6a7SJames Smart 	/* hba_wqidx should already be setup from command we are aborting */
1758d613b6a7SJames Smart 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
1759d613b6a7SJames Smart 	abts_wqeq->iocb.ulpLe = 1;
1760d613b6a7SJames Smart 
1761d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1762d613b6a7SJames Smart 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
1763d613b6a7SJames Smart 			xri, abts_wqeq->iotag);
1764d613b6a7SJames Smart 	return 1;
1765d613b6a7SJames Smart }
1766d613b6a7SJames Smart 
1767d613b6a7SJames Smart static int
1768d613b6a7SJames Smart lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
1769d613b6a7SJames Smart 			       struct lpfc_nvmet_rcv_ctx *ctxp,
1770d613b6a7SJames Smart 			       uint32_t sid, uint16_t xri)
1771d613b6a7SJames Smart {
1772d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1773d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
1774d613b6a7SJames Smart 	union lpfc_wqe *abts_wqe;
1775d613b6a7SJames Smart 	struct lpfc_nodelist *ndlp;
1776d613b6a7SJames Smart 	unsigned long flags;
1777d613b6a7SJames Smart 	int rc;
1778d613b6a7SJames Smart 
1779d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1780d613b6a7SJames Smart 	if (!ctxp->wqeq) {
1781d613b6a7SJames Smart 		ctxp->wqeq = ctxp->rqb_buffer->iocbq;
1782d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
1783d613b6a7SJames Smart 	}
1784d613b6a7SJames Smart 
1785d613b6a7SJames Smart 	ndlp = lpfc_findnode_did(phba->pport, sid);
1786d613b6a7SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1787d613b6a7SJames Smart 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1788d613b6a7SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1789d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp_error);
1790d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1791d613b6a7SJames Smart 				"6160 Drop ABTS - wrong NDLP state x%x.\n",
1792d613b6a7SJames Smart 				ndlp->nlp_state);
1793d613b6a7SJames Smart 
1794d613b6a7SJames Smart 		/* No failure to an ABTS request. */
1795d613b6a7SJames Smart 		return 0;
1796d613b6a7SJames Smart 	}
1797d613b6a7SJames Smart 
1798d613b6a7SJames Smart 	/* Issue ABTS for this WQE based on iotag */
1799d613b6a7SJames Smart 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
1800d613b6a7SJames Smart 	if (!ctxp->abort_wqeq) {
1801d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1802d613b6a7SJames Smart 				"6161 Abort failed: No wqeqs: "
1803d613b6a7SJames Smart 				"xri: x%x\n", ctxp->oxid);
1804d613b6a7SJames Smart 		/* No failure to an ABTS request. */
1805d613b6a7SJames Smart 		return 0;
1806d613b6a7SJames Smart 	}
1807d613b6a7SJames Smart 	abts_wqeq = ctxp->abort_wqeq;
1808d613b6a7SJames Smart 	abts_wqe = &abts_wqeq->wqe;
1809d613b6a7SJames Smart 	ctxp->state = LPFC_NVMET_STE_ABORT;
1810d613b6a7SJames Smart 
1811d613b6a7SJames Smart 	/* Announce entry to new IO submit field. */
1812d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
1813d613b6a7SJames Smart 			"6162 Abort Request to rport DID x%06x "
1814d613b6a7SJames Smart 			"for xri x%x x%x\n",
1815d613b6a7SJames Smart 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
1816d613b6a7SJames Smart 
1817d613b6a7SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
1818d613b6a7SJames Smart 	 * cleared when the reset is complete and rings reestablished.
1819d613b6a7SJames Smart 	 */
1820d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
1821d613b6a7SJames Smart 	/* driver queued commands are in process of being flushed */
1822d613b6a7SJames Smart 	if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
1823d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1824d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1825d613b6a7SJames Smart 				"6163 Driver in reset cleanup - flushing "
1826d613b6a7SJames Smart 				"NVME Req now. hba_flag x%x oxid x%x\n",
1827d613b6a7SJames Smart 				phba->hba_flag, ctxp->oxid);
1828d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
1829d613b6a7SJames Smart 		return 0;
1830d613b6a7SJames Smart 	}
1831d613b6a7SJames Smart 
1832d613b6a7SJames Smart 	/* Outstanding abort is in progress */
1833d613b6a7SJames Smart 	if (abts_wqeq->iocb_flag & LPFC_DRIVER_ABORTED) {
1834d613b6a7SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
1835d613b6a7SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1836d613b6a7SJames Smart 				"6164 Outstanding NVME I/O Abort Request "
1837d613b6a7SJames Smart 				"still pending on oxid x%x\n",
1838d613b6a7SJames Smart 				ctxp->oxid);
1839d613b6a7SJames Smart 		lpfc_sli_release_iocbq(phba, abts_wqeq);
1840d613b6a7SJames Smart 		return 0;
1841d613b6a7SJames Smart 	}
1842d613b6a7SJames Smart 
1843d613b6a7SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
1844d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_DRIVER_ABORTED;
1845d613b6a7SJames Smart 
1846d613b6a7SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
1847d613b6a7SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
1848d613b6a7SJames Smart 	 */
1849d613b6a7SJames Smart 	memset(abts_wqe, 0, sizeof(union lpfc_wqe));
1850d613b6a7SJames Smart 
1851d613b6a7SJames Smart 	/* word 3 */
1852d613b6a7SJames Smart 	bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
1853d613b6a7SJames Smart 
1854d613b6a7SJames Smart 	/* word 7 */
1855d613b6a7SJames Smart 	bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
1856d613b6a7SJames Smart 	bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
1857d613b6a7SJames Smart 
1858d613b6a7SJames Smart 	/* word 8 - tell the FW to abort the IO associated with this
1859d613b6a7SJames Smart 	 * outstanding exchange ID.
1860d613b6a7SJames Smart 	 */
1861d613b6a7SJames Smart 	abts_wqe->abort_cmd.wqe_com.abort_tag = ctxp->wqeq->sli4_xritag;
1862d613b6a7SJames Smart 
1863d613b6a7SJames Smart 	/* word 9 - this is the iotag for the abts_wqe completion. */
1864d613b6a7SJames Smart 	bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
1865d613b6a7SJames Smart 	       abts_wqeq->iotag);
1866d613b6a7SJames Smart 
1867d613b6a7SJames Smart 	/* word 10 */
1868d613b6a7SJames Smart 	bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
1869d613b6a7SJames Smart 	bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
1870d613b6a7SJames Smart 
1871d613b6a7SJames Smart 	/* word 11 */
1872d613b6a7SJames Smart 	bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
1873d613b6a7SJames Smart 	bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
1874d613b6a7SJames Smart 	bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1875d613b6a7SJames Smart 
1876d613b6a7SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
1877d613b6a7SJames Smart 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
1878d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
1879d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
1880d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVME;
1881d613b6a7SJames Smart 	abts_wqeq->context2 = ctxp;
1882d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_wqeq);
1883d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1884d613b6a7SJames Smart 	if (rc == WQE_SUCCESS)
1885d613b6a7SJames Smart 		return 0;
1886d613b6a7SJames Smart 
1887d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
1888d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1889d613b6a7SJames Smart 			"6166 Failed abts issue_wqe with status x%x "
1890d613b6a7SJames Smart 			"for oxid x%x.\n",
1891d613b6a7SJames Smart 			rc, ctxp->oxid);
1892d613b6a7SJames Smart 	return 1;
1893d613b6a7SJames Smart }
1894d613b6a7SJames Smart 
1895d613b6a7SJames Smart 
1896d613b6a7SJames Smart static int
1897d613b6a7SJames Smart lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
1898d613b6a7SJames Smart 				 struct lpfc_nvmet_rcv_ctx *ctxp,
1899d613b6a7SJames Smart 				 uint32_t sid, uint16_t xri)
1900d613b6a7SJames Smart {
1901d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1902d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
1903d613b6a7SJames Smart 	unsigned long flags;
1904d613b6a7SJames Smart 	int rc;
1905d613b6a7SJames Smart 
1906d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1907d613b6a7SJames Smart 	if (!ctxp->wqeq) {
1908d613b6a7SJames Smart 		ctxp->wqeq = ctxp->rqb_buffer->iocbq;
1909d613b6a7SJames Smart 		ctxp->wqeq->hba_wqidx = 0;
1910d613b6a7SJames Smart 	}
1911d613b6a7SJames Smart 
1912d613b6a7SJames Smart 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
1913d613b6a7SJames Smart 	if (rc == 0)
1914d613b6a7SJames Smart 		goto aerr;
1915d613b6a7SJames Smart 
1916d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
1917d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
1918d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_abort_cmp;
1919d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
1920d613b6a7SJames Smart 	abts_wqeq->iocb_flag |= LPFC_IO_NVMET;
1921d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_wqeq);
1922d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1923d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
1924d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp);
1925d613b6a7SJames Smart 		return 0;
1926d613b6a7SJames Smart 	}
1927d613b6a7SJames Smart 
1928d613b6a7SJames Smart aerr:
1929d613b6a7SJames Smart 	lpfc_nvmet_rq_post(phba, ctxp, &ctxp->rqb_buffer->hbuf);
1930d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
1931d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1932d613b6a7SJames Smart 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x\n",
1933d613b6a7SJames Smart 			ctxp->oxid, rc);
1934d613b6a7SJames Smart 	return 1;
1935d613b6a7SJames Smart }
1936d613b6a7SJames Smart 
1937d613b6a7SJames Smart static int
1938d613b6a7SJames Smart lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *phba,
1939d613b6a7SJames Smart 				struct lpfc_nvmet_rcv_ctx *ctxp,
1940d613b6a7SJames Smart 				uint32_t sid, uint16_t xri)
1941d613b6a7SJames Smart {
1942d613b6a7SJames Smart 	struct lpfc_nvmet_tgtport *tgtp;
1943d613b6a7SJames Smart 	struct lpfc_iocbq *abts_wqeq;
1944d613b6a7SJames Smart 	union lpfc_wqe *wqe_abts;
1945d613b6a7SJames Smart 	unsigned long flags;
1946d613b6a7SJames Smart 	int rc;
1947d613b6a7SJames Smart 
1948d613b6a7SJames Smart 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1949d613b6a7SJames Smart 	if (!ctxp->wqeq) {
1950d613b6a7SJames Smart 		/* Issue ABTS for this WQE based on iotag */
1951d613b6a7SJames Smart 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
1952d613b6a7SJames Smart 		if (!ctxp->wqeq) {
1953d613b6a7SJames Smart 			lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1954d613b6a7SJames Smart 					"6068 Abort failed: No wqeqs: "
1955d613b6a7SJames Smart 					"xri: x%x\n", xri);
1956d613b6a7SJames Smart 			/* No failure to an ABTS request. */
1957d613b6a7SJames Smart 			kfree(ctxp);
1958d613b6a7SJames Smart 			return 0;
1959d613b6a7SJames Smart 		}
1960d613b6a7SJames Smart 	}
1961d613b6a7SJames Smart 	abts_wqeq = ctxp->wqeq;
1962d613b6a7SJames Smart 	wqe_abts = &abts_wqeq->wqe;
1963d613b6a7SJames Smart 	lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
1964d613b6a7SJames Smart 
1965d613b6a7SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
1966d613b6a7SJames Smart 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
1967d613b6a7SJames Smart 	abts_wqeq->iocb_cmpl = 0;
1968d613b6a7SJames Smart 	abts_wqeq->iocb_flag |=  LPFC_IO_NVME_LS;
1969d613b6a7SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, abts_wqeq);
1970d613b6a7SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1971d613b6a7SJames Smart 	if (rc == WQE_SUCCESS) {
1972d613b6a7SJames Smart 		atomic_inc(&tgtp->xmt_abort_rsp);
1973d613b6a7SJames Smart 		return 0;
1974d613b6a7SJames Smart 	}
1975d613b6a7SJames Smart 
1976d613b6a7SJames Smart 	atomic_inc(&tgtp->xmt_abort_rsp_error);
1977d613b6a7SJames Smart 	abts_wqeq->context2 = NULL;
1978d613b6a7SJames Smart 	abts_wqeq->context3 = NULL;
1979d613b6a7SJames Smart 	lpfc_sli_release_iocbq(phba, abts_wqeq);
1980d613b6a7SJames Smart 	kfree(ctxp);
1981d613b6a7SJames Smart 	lpfc_printf_log(phba, KERN_WARNING, LOG_NVME_ABTS,
1982d613b6a7SJames Smart 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
1983d613b6a7SJames Smart 	return 0;
1984d613b6a7SJames Smart }
1985