xref: /linux/drivers/scsi/lpfc/lpfc_nvme.c (revision cf1a1d3e2d88af49472014db0c82779b4fe85455)
101649561SJames Smart /*******************************************************************
201649561SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
301649561SJames Smart  * Fibre Channel Host Bus Adapters.                                *
4d080abe0SJames Smart  * Copyright (C) 2017 Broadcom. All Rights Reserved. The term      *
5d080abe0SJames Smart  * “Broadcom” refers to Broadcom Limited and/or its subsidiaries.  *
601649561SJames Smart  * Copyright (C) 2004-2016 Emulex.  All rights reserved.           *
701649561SJames Smart  * EMULEX and SLI are trademarks of Emulex.                        *
8d080abe0SJames Smart  * www.broadcom.com                                                *
901649561SJames Smart  * Portions Copyright (C) 2004-2005 Christoph Hellwig              *
1001649561SJames Smart  *                                                                 *
1101649561SJames Smart  * This program is free software; you can redistribute it and/or   *
1201649561SJames Smart  * modify it under the terms of version 2 of the GNU General       *
1301649561SJames Smart  * Public License as published by the Free Software Foundation.    *
1401649561SJames Smart  * This program is distributed in the hope that it will be useful. *
1501649561SJames Smart  * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND          *
1601649561SJames Smart  * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,  *
1701649561SJames Smart  * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE      *
1801649561SJames Smart  * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
1901649561SJames Smart  * TO BE LEGALLY INVALID.  See the GNU General Public License for  *
2001649561SJames Smart  * more details, a copy of which can be found in the file COPYING  *
2101649561SJames Smart  * included with this package.                                     *
2201649561SJames Smart  ********************************************************************/
2301649561SJames Smart #include <linux/pci.h>
2401649561SJames Smart #include <linux/slab.h>
2501649561SJames Smart #include <linux/interrupt.h>
2601649561SJames Smart #include <linux/delay.h>
2701649561SJames Smart #include <asm/unaligned.h>
2801649561SJames Smart #include <linux/crc-t10dif.h>
2901649561SJames Smart #include <net/checksum.h>
3001649561SJames Smart 
3101649561SJames Smart #include <scsi/scsi.h>
3201649561SJames Smart #include <scsi/scsi_device.h>
3301649561SJames Smart #include <scsi/scsi_eh.h>
3401649561SJames Smart #include <scsi/scsi_host.h>
3501649561SJames Smart #include <scsi/scsi_tcq.h>
3601649561SJames Smart #include <scsi/scsi_transport_fc.h>
3701649561SJames Smart #include <scsi/fc/fc_fs.h>
3801649561SJames Smart 
3901649561SJames Smart #include <linux/nvme.h>
4001649561SJames Smart #include <linux/nvme-fc-driver.h>
4101649561SJames Smart #include <linux/nvme-fc.h>
4201649561SJames Smart #include "lpfc_version.h"
4301649561SJames Smart #include "lpfc_hw4.h"
4401649561SJames Smart #include "lpfc_hw.h"
4501649561SJames Smart #include "lpfc_sli.h"
4601649561SJames Smart #include "lpfc_sli4.h"
4701649561SJames Smart #include "lpfc_nl.h"
4801649561SJames Smart #include "lpfc_disc.h"
4901649561SJames Smart #include "lpfc.h"
5001649561SJames Smart #include "lpfc_nvme.h"
5101649561SJames Smart #include "lpfc_scsi.h"
5201649561SJames Smart #include "lpfc_logmsg.h"
5301649561SJames Smart #include "lpfc_crtn.h"
5401649561SJames Smart #include "lpfc_vport.h"
55bd2cdd5eSJames Smart #include "lpfc_debugfs.h"
5601649561SJames Smart 
5701649561SJames Smart /* NVME initiator-based functions */
5801649561SJames Smart 
5901649561SJames Smart static struct lpfc_nvme_buf *
60*cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
61*cf1a1d3eSJames Smart 		  int expedite);
6201649561SJames Smart 
6301649561SJames Smart static void
6401649561SJames Smart lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_nvme_buf *);
6501649561SJames Smart 
6681e6a637SJames Smart static struct nvme_fc_port_template lpfc_nvme_template;
6701649561SJames Smart 
6801649561SJames Smart /**
6901649561SJames Smart  * lpfc_nvme_create_queue -
7001649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
7101649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
7201649561SJames Smart  * @handle: An opaque driver handle used in follow-up calls.
7301649561SJames Smart  *
7401649561SJames Smart  * Driver registers this routine to preallocate and initialize any
7501649561SJames Smart  * internal data structures to bind the @qidx to its internal IO queues.
7601649561SJames Smart  * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
7701649561SJames Smart  *
7801649561SJames Smart  * Return value :
7901649561SJames Smart  *   0 - Success
8001649561SJames Smart  *   -EINVAL - Unsupported input value.
8101649561SJames Smart  *   -ENOMEM - Could not alloc necessary memory
8201649561SJames Smart  **/
8301649561SJames Smart static int
8401649561SJames Smart lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
8501649561SJames Smart 		       unsigned int qidx, u16 qsize,
8601649561SJames Smart 		       void **handle)
8701649561SJames Smart {
8801649561SJames Smart 	struct lpfc_nvme_lport *lport;
8901649561SJames Smart 	struct lpfc_vport *vport;
9001649561SJames Smart 	struct lpfc_nvme_qhandle *qhandle;
9101649561SJames Smart 	char *str;
9201649561SJames Smart 
93c3725bdcSJames Smart 	if (!pnvme_lport->private)
94c3725bdcSJames Smart 		return -ENOMEM;
95c3725bdcSJames Smart 
9601649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
9701649561SJames Smart 	vport = lport->vport;
9801649561SJames Smart 	qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
9901649561SJames Smart 	if (qhandle == NULL)
10001649561SJames Smart 		return -ENOMEM;
10101649561SJames Smart 
10201649561SJames Smart 	qhandle->cpu_id = smp_processor_id();
10301649561SJames Smart 	qhandle->qidx = qidx;
10401649561SJames Smart 	/*
10501649561SJames Smart 	 * NVME qidx == 0 is the admin queue, so both admin queue
10601649561SJames Smart 	 * and first IO queue will use MSI-X vector and associated
10701649561SJames Smart 	 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
10801649561SJames Smart 	 */
10901649561SJames Smart 	if (qidx) {
11001649561SJames Smart 		str = "IO ";  /* IO queue */
11101649561SJames Smart 		qhandle->index = ((qidx - 1) %
11201649561SJames Smart 			vport->phba->cfg_nvme_io_channel);
11301649561SJames Smart 	} else {
11401649561SJames Smart 		str = "ADM";  /* Admin queue */
11501649561SJames Smart 		qhandle->index = qidx;
11601649561SJames Smart 	}
11701649561SJames Smart 
118d58734f0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
11901649561SJames Smart 			 "6073 Binding %s HdwQueue %d  (cpu %d) to "
12001649561SJames Smart 			 "io_channel %d qhandle %p\n", str,
12101649561SJames Smart 			 qidx, qhandle->cpu_id, qhandle->index, qhandle);
12201649561SJames Smart 	*handle = (void *)qhandle;
12301649561SJames Smart 	return 0;
12401649561SJames Smart }
12501649561SJames Smart 
12601649561SJames Smart /**
12701649561SJames Smart  * lpfc_nvme_delete_queue -
12801649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
12901649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
13001649561SJames Smart  * @handle: An opaque driver handle from lpfc_nvme_create_queue
13101649561SJames Smart  *
13201649561SJames Smart  * Driver registers this routine to free
13301649561SJames Smart  * any internal data structures to bind the @qidx to its internal
13401649561SJames Smart  * IO queues.
13501649561SJames Smart  *
13601649561SJames Smart  * Return value :
13701649561SJames Smart  *   0 - Success
13801649561SJames Smart  *   TODO:  What are the failure codes.
13901649561SJames Smart  **/
14001649561SJames Smart static void
14101649561SJames Smart lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
14201649561SJames Smart 		       unsigned int qidx,
14301649561SJames Smart 		       void *handle)
14401649561SJames Smart {
14501649561SJames Smart 	struct lpfc_nvme_lport *lport;
14601649561SJames Smart 	struct lpfc_vport *vport;
14701649561SJames Smart 
148c3725bdcSJames Smart 	if (!pnvme_lport->private)
149c3725bdcSJames Smart 		return;
150c3725bdcSJames Smart 
15101649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
15201649561SJames Smart 	vport = lport->vport;
15301649561SJames Smart 
15401649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
15501649561SJames Smart 			"6001 ENTER.  lpfc_pnvme %p, qidx x%xi qhandle %p\n",
15601649561SJames Smart 			lport, qidx, handle);
15701649561SJames Smart 	kfree(handle);
15801649561SJames Smart }
15901649561SJames Smart 
16001649561SJames Smart static void
16101649561SJames Smart lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
16201649561SJames Smart {
16301649561SJames Smart 	struct lpfc_nvme_lport *lport = localport->private;
16401649561SJames Smart 
165add9d6beSJames Smart 	lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
166add9d6beSJames Smart 			 "6173 localport %p delete complete\n",
167add9d6beSJames Smart 			 lport);
168add9d6beSJames Smart 
16901649561SJames Smart 	/* release any threads waiting for the unreg to complete */
17001649561SJames Smart 	complete(&lport->lport_unreg_done);
17101649561SJames Smart }
17201649561SJames Smart 
17301649561SJames Smart /* lpfc_nvme_remoteport_delete
17401649561SJames Smart  *
17501649561SJames Smart  * @remoteport: Pointer to an nvme transport remoteport instance.
17601649561SJames Smart  *
17701649561SJames Smart  * This is a template downcall.  NVME transport calls this function
17801649561SJames Smart  * when it has completed the unregistration of a previously
17901649561SJames Smart  * registered remoteport.
18001649561SJames Smart  *
18101649561SJames Smart  * Return value :
18201649561SJames Smart  * None
18301649561SJames Smart  */
18401649561SJames Smart void
18501649561SJames Smart lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
18601649561SJames Smart {
18701649561SJames Smart 	struct lpfc_nvme_rport *rport = remoteport->private;
18801649561SJames Smart 	struct lpfc_vport *vport;
18901649561SJames Smart 	struct lpfc_nodelist *ndlp;
19001649561SJames Smart 
19101649561SJames Smart 	ndlp = rport->ndlp;
19201649561SJames Smart 	if (!ndlp)
19301649561SJames Smart 		goto rport_err;
19401649561SJames Smart 
19501649561SJames Smart 	vport = ndlp->vport;
19601649561SJames Smart 	if (!vport)
19701649561SJames Smart 		goto rport_err;
19801649561SJames Smart 
19901649561SJames Smart 	/* Remove this rport from the lport's list - memory is owned by the
20001649561SJames Smart 	 * transport. Remove the ndlp reference for the NVME transport before
2017a06dcd3SJames Smart 	 * calling state machine to remove the node.
20201649561SJames Smart 	 */
20301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
20401649561SJames Smart 			"6146 remoteport delete complete %p\n",
20501649561SJames Smart 			remoteport);
2067a06dcd3SJames Smart 	ndlp->nrport = NULL;
20701649561SJames Smart 	lpfc_nlp_put(ndlp);
20801649561SJames Smart 
20901649561SJames Smart  rport_err:
21001649561SJames Smart 	/* This call has to execute as long as the rport is valid.
21101649561SJames Smart 	 * Release any threads waiting for the unreg to complete.
21201649561SJames Smart 	 */
21301649561SJames Smart 	complete(&rport->rport_unreg_done);
21401649561SJames Smart }
21501649561SJames Smart 
21601649561SJames Smart static void
21701649561SJames Smart lpfc_nvme_cmpl_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
21801649561SJames Smart 		       struct lpfc_wcqe_complete *wcqe)
21901649561SJames Smart {
22001649561SJames Smart 	struct lpfc_vport *vport = cmdwqe->vport;
22101649561SJames Smart 	uint32_t status;
22201649561SJames Smart 	struct nvmefc_ls_req *pnvme_lsreq;
22301649561SJames Smart 	struct lpfc_dmabuf *buf_ptr;
22401649561SJames Smart 	struct lpfc_nodelist *ndlp;
22501649561SJames Smart 
2262cee7808SJames Smart 	atomic_inc(&vport->phba->fc4NvmeLsCmpls);
22701649561SJames Smart 
22801649561SJames Smart 	pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
22901649561SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
23001649561SJames Smart 	ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
23101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
23201649561SJames Smart 			 "6047 nvme cmpl Enter "
23301649561SJames Smart 			 "Data %p DID %x Xri: %x status %x cmd:%p lsreg:%p "
23401649561SJames Smart 			 "bmp:%p ndlp:%p\n",
23501649561SJames Smart 			 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
23601649561SJames Smart 			 cmdwqe->sli4_xritag, status,
23701649561SJames Smart 			 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
23801649561SJames Smart 
239bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  CMPL: xri x%x stat x%x parm x%x\n",
240bd2cdd5eSJames Smart 			 cmdwqe->sli4_xritag, status, wcqe->parameter);
241bd2cdd5eSJames Smart 
24201649561SJames Smart 	if (cmdwqe->context3) {
24301649561SJames Smart 		buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
24401649561SJames Smart 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
24501649561SJames Smart 		kfree(buf_ptr);
24601649561SJames Smart 		cmdwqe->context3 = NULL;
24701649561SJames Smart 	}
24801649561SJames Smart 	if (pnvme_lsreq->done)
24901649561SJames Smart 		pnvme_lsreq->done(pnvme_lsreq, status);
25001649561SJames Smart 	else
25101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
25201649561SJames Smart 				 "6046 nvme cmpl without done call back? "
25301649561SJames Smart 				 "Data %p DID %x Xri: %x status %x\n",
25401649561SJames Smart 				pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
25501649561SJames Smart 				cmdwqe->sli4_xritag, status);
25601649561SJames Smart 	if (ndlp) {
25701649561SJames Smart 		lpfc_nlp_put(ndlp);
25801649561SJames Smart 		cmdwqe->context1 = NULL;
25901649561SJames Smart 	}
26001649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
26101649561SJames Smart }
26201649561SJames Smart 
26301649561SJames Smart static int
26401649561SJames Smart lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
26501649561SJames Smart 		  struct lpfc_dmabuf *inp,
26601649561SJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq,
26701649561SJames Smart 	     void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
26801649561SJames Smart 			   struct lpfc_wcqe_complete *),
26901649561SJames Smart 	     struct lpfc_nodelist *ndlp, uint32_t num_entry,
27001649561SJames Smart 	     uint32_t tmo, uint8_t retry)
27101649561SJames Smart {
27201649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
27301649561SJames Smart 	union lpfc_wqe *wqe;
27401649561SJames Smart 	struct lpfc_iocbq *genwqe;
27501649561SJames Smart 	struct ulp_bde64 *bpl;
27601649561SJames Smart 	struct ulp_bde64 bde;
27701649561SJames Smart 	int i, rc, xmit_len, first_len;
27801649561SJames Smart 
27901649561SJames Smart 	/* Allocate buffer for  command WQE */
28001649561SJames Smart 	genwqe = lpfc_sli_get_iocbq(phba);
28101649561SJames Smart 	if (genwqe == NULL)
28201649561SJames Smart 		return 1;
28301649561SJames Smart 
28401649561SJames Smart 	wqe = &genwqe->wqe;
28501649561SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
28601649561SJames Smart 
28701649561SJames Smart 	genwqe->context3 = (uint8_t *)bmp;
28801649561SJames Smart 	genwqe->iocb_flag |= LPFC_IO_NVME_LS;
28901649561SJames Smart 
29001649561SJames Smart 	/* Save for completion so we can release these resources */
29101649561SJames Smart 	genwqe->context1 = lpfc_nlp_get(ndlp);
29201649561SJames Smart 	genwqe->context2 = (uint8_t *)pnvme_lsreq;
29301649561SJames Smart 	/* Fill in payload, bp points to frame payload */
29401649561SJames Smart 
29501649561SJames Smart 	if (!tmo)
29601649561SJames Smart 		/* FC spec states we need 3 * ratov for CT requests */
29701649561SJames Smart 		tmo = (3 * phba->fc_ratov);
29801649561SJames Smart 
29901649561SJames Smart 	/* For this command calculate the xmit length of the request bde. */
30001649561SJames Smart 	xmit_len = 0;
30101649561SJames Smart 	first_len = 0;
30201649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
30301649561SJames Smart 	for (i = 0; i < num_entry; i++) {
30401649561SJames Smart 		bde.tus.w = bpl[i].tus.w;
30501649561SJames Smart 		if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
30601649561SJames Smart 			break;
30701649561SJames Smart 		xmit_len += bde.tus.f.bdeSize;
30801649561SJames Smart 		if (i == 0)
30901649561SJames Smart 			first_len = xmit_len;
31001649561SJames Smart 	}
31101649561SJames Smart 
31201649561SJames Smart 	genwqe->rsvd2 = num_entry;
31301649561SJames Smart 	genwqe->hba_wqidx = 0;
31401649561SJames Smart 
31501649561SJames Smart 	/* Words 0 - 2 */
31601649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
31701649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = first_len;
31801649561SJames Smart 	wqe->generic.bde.addrLow = bpl[0].addrLow;
31901649561SJames Smart 	wqe->generic.bde.addrHigh = bpl[0].addrHigh;
32001649561SJames Smart 
32101649561SJames Smart 	/* Word 3 */
32201649561SJames Smart 	wqe->gen_req.request_payload_len = first_len;
32301649561SJames Smart 
32401649561SJames Smart 	/* Word 4 */
32501649561SJames Smart 
32601649561SJames Smart 	/* Word 5 */
32701649561SJames Smart 	bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
32801649561SJames Smart 	bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
32901649561SJames Smart 	bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
3308b361639SJames Smart 	bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
33101649561SJames Smart 	bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
33201649561SJames Smart 
33301649561SJames Smart 	/* Word 6 */
33401649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
33501649561SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
33601649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
33701649561SJames Smart 
33801649561SJames Smart 	/* Word 7 */
33901649561SJames Smart 	bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
34001649561SJames Smart 	bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
34101649561SJames Smart 	bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
34201649561SJames Smart 	bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
34301649561SJames Smart 
34401649561SJames Smart 	/* Word 8 */
34501649561SJames Smart 	wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
34601649561SJames Smart 
34701649561SJames Smart 	/* Word 9 */
34801649561SJames Smart 	bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
34901649561SJames Smart 
35001649561SJames Smart 	/* Word 10 */
35101649561SJames Smart 	bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
35201649561SJames Smart 	bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
35301649561SJames Smart 	bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
35401649561SJames Smart 	bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
35501649561SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
35601649561SJames Smart 
35701649561SJames Smart 	/* Word 11 */
35801649561SJames Smart 	bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
35901649561SJames Smart 	bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
36001649561SJames Smart 
36101649561SJames Smart 
36201649561SJames Smart 	/* Issue GEN REQ WQE for NPORT <did> */
36301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
36401649561SJames Smart 			 "6050 Issue GEN REQ WQE to NPORT x%x "
36501649561SJames Smart 			 "Data: x%x x%x wq:%p lsreq:%p bmp:%p xmit:%d 1st:%d\n",
36601649561SJames Smart 			 ndlp->nlp_DID, genwqe->iotag,
36701649561SJames Smart 			 vport->port_state,
36801649561SJames Smart 			genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
36901649561SJames Smart 	genwqe->wqe_cmpl = cmpl;
37001649561SJames Smart 	genwqe->iocb_cmpl = NULL;
37101649561SJames Smart 	genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
37201649561SJames Smart 	genwqe->vport = vport;
37301649561SJames Smart 	genwqe->retry = retry;
37401649561SJames Smart 
375bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
376bd2cdd5eSJames Smart 			 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
377bd2cdd5eSJames Smart 
37801649561SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, genwqe);
379cd22d605SDick Kennedy 	if (rc) {
38001649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
38101649561SJames Smart 				 "6045 Issue GEN REQ WQE to NPORT x%x "
38201649561SJames Smart 				 "Data: x%x x%x\n",
38301649561SJames Smart 				 ndlp->nlp_DID, genwqe->iotag,
38401649561SJames Smart 				 vport->port_state);
38501649561SJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
38601649561SJames Smart 		return 1;
38701649561SJames Smart 	}
38801649561SJames Smart 	return 0;
38901649561SJames Smart }
39001649561SJames Smart 
39101649561SJames Smart /**
39201649561SJames Smart  * lpfc_nvme_ls_req - Issue an Link Service request
39301649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
39401649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
39501649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
39601649561SJames Smart  *
39701649561SJames Smart  * Driver registers this routine to handle any link service request
39801649561SJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
39901649561SJames Smart  *
40001649561SJames Smart  * Return value :
40101649561SJames Smart  *   0 - Success
40201649561SJames Smart  *   TODO: What are the failure codes.
40301649561SJames Smart  **/
40401649561SJames Smart static int
40501649561SJames Smart lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
40601649561SJames Smart 		 struct nvme_fc_remote_port *pnvme_rport,
40701649561SJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq)
40801649561SJames Smart {
40901649561SJames Smart 	int ret = 0;
41001649561SJames Smart 	struct lpfc_nvme_lport *lport;
41101649561SJames Smart 	struct lpfc_vport *vport;
41201649561SJames Smart 	struct lpfc_nodelist *ndlp;
41301649561SJames Smart 	struct ulp_bde64 *bpl;
41401649561SJames Smart 	struct lpfc_dmabuf *bmp;
415ba43c4d0SJames Smart 	uint16_t ntype, nstate;
41601649561SJames Smart 
41701649561SJames Smart 	/* there are two dma buf in the request, actually there is one and
41801649561SJames Smart 	 * the second one is just the start address + cmd size.
41901649561SJames Smart 	 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
42001649561SJames Smart 	 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
42101649561SJames Smart 	 * because the nvem layer owns the data bufs.
42201649561SJames Smart 	 * We do not have to break these packets open, we don't care what is in
42301649561SJames Smart 	 * them. And we do not have to look at the resonse data, we only care
42401649561SJames Smart 	 * that we got a response. All of the caring is going to happen in the
42501649561SJames Smart 	 * nvme-fc layer.
42601649561SJames Smart 	 */
42701649561SJames Smart 
42801649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
42901649561SJames Smart 	vport = lport->vport;
43001649561SJames Smart 
4311abcb371SDick Kennedy 	if (vport->load_flag & FC_UNLOADING)
4321abcb371SDick Kennedy 		return -ENODEV;
4331abcb371SDick Kennedy 
4343386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
4353386f4bdSJames Smart 		return -ENODEV;
4363386f4bdSJames Smart 
43701649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
438ba43c4d0SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
439ba43c4d0SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
440ba43c4d0SJames Smart 				 "6051 DID x%06x not an active rport.\n",
44101649561SJames Smart 				 pnvme_rport->port_id);
442ba43c4d0SJames Smart 		return -ENODEV;
443ba43c4d0SJames Smart 	}
444ba43c4d0SJames Smart 
445ba43c4d0SJames Smart 	/* The remote node has to be a mapped nvme target or an
446ba43c4d0SJames Smart 	 * unmapped nvme initiator or it's an error.
447ba43c4d0SJames Smart 	 */
448ba43c4d0SJames Smart 	ntype = ndlp->nlp_type;
449ba43c4d0SJames Smart 	nstate = ndlp->nlp_state;
450ba43c4d0SJames Smart 	if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
451ba43c4d0SJames Smart 	    (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
452ba43c4d0SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
453ba43c4d0SJames Smart 				 "6088 DID x%06x not ready for "
454ba43c4d0SJames Smart 				 "IO. State x%x, Type x%x\n",
455ba43c4d0SJames Smart 				 pnvme_rport->port_id,
456ba43c4d0SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type);
457ba43c4d0SJames Smart 		return -ENODEV;
45801649561SJames Smart 	}
45901649561SJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
46001649561SJames Smart 	if (!bmp) {
46101649561SJames Smart 
46201649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
46301649561SJames Smart 				 "6044 Could not find node for DID %x\n",
46401649561SJames Smart 				 pnvme_rport->port_id);
46501649561SJames Smart 		return 2;
46601649561SJames Smart 	}
46701649561SJames Smart 	INIT_LIST_HEAD(&bmp->list);
46801649561SJames Smart 	bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
46901649561SJames Smart 	if (!bmp->virt) {
47001649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
47101649561SJames Smart 				 "6042 Could not find node for DID %x\n",
47201649561SJames Smart 				 pnvme_rport->port_id);
47301649561SJames Smart 		kfree(bmp);
47401649561SJames Smart 		return 3;
47501649561SJames Smart 	}
47601649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
47701649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
47801649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
47901649561SJames Smart 	bpl->tus.f.bdeFlags = 0;
48001649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
48101649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
48201649561SJames Smart 	bpl++;
48301649561SJames Smart 
48401649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
48501649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
48601649561SJames Smart 	bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
48701649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
48801649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
48901649561SJames Smart 
49001649561SJames Smart 	/* Expand print to include key fields. */
49101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
492ba43c4d0SJames Smart 			 "6149 ENTER.  lport %p, rport %p lsreq%p rqstlen:%d "
493825c6abbSArnd Bergmann 			 "rsplen:%d %pad %pad\n",
49401649561SJames Smart 			 pnvme_lport, pnvme_rport,
49501649561SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
496825c6abbSArnd Bergmann 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
497825c6abbSArnd Bergmann 			 &pnvme_lsreq->rspdma);
49801649561SJames Smart 
4992cee7808SJames Smart 	atomic_inc(&vport->phba->fc4NvmeLsRequests);
50001649561SJames Smart 
50101649561SJames Smart 	/* Hardcode the wait to 30 seconds.  Connections are failing otherwise.
50201649561SJames Smart 	 * This code allows it all to work.
50301649561SJames Smart 	 */
50401649561SJames Smart 	ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
50501649561SJames Smart 				pnvme_lsreq, lpfc_nvme_cmpl_gen_req,
50601649561SJames Smart 				ndlp, 2, 30, 0);
50701649561SJames Smart 	if (ret != WQE_SUCCESS) {
50801649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
50901649561SJames Smart 				 "6052 EXIT. issue ls wqe failed lport %p, "
51001649561SJames Smart 				 "rport %p lsreq%p Status %x DID %x\n",
51101649561SJames Smart 				 pnvme_lport, pnvme_rport, pnvme_lsreq,
51201649561SJames Smart 				 ret, ndlp->nlp_DID);
51301649561SJames Smart 		lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
51401649561SJames Smart 		kfree(bmp);
51501649561SJames Smart 		return ret;
51601649561SJames Smart 	}
51701649561SJames Smart 
51801649561SJames Smart 	/* Stub in routine and return 0 for now. */
51901649561SJames Smart 	return ret;
52001649561SJames Smart }
52101649561SJames Smart 
52201649561SJames Smart /**
52301649561SJames Smart  * lpfc_nvme_ls_abort - Issue an Link Service request
52401649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
52501649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
52601649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
52701649561SJames Smart  *
52801649561SJames Smart  * Driver registers this routine to handle any link service request
52901649561SJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
53001649561SJames Smart  *
53101649561SJames Smart  * Return value :
53201649561SJames Smart  *   0 - Success
53301649561SJames Smart  *   TODO: What are the failure codes.
53401649561SJames Smart  **/
53501649561SJames Smart static void
53601649561SJames Smart lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
53701649561SJames Smart 		   struct nvme_fc_remote_port *pnvme_rport,
53801649561SJames Smart 		   struct nvmefc_ls_req *pnvme_lsreq)
53901649561SJames Smart {
54001649561SJames Smart 	struct lpfc_nvme_lport *lport;
54101649561SJames Smart 	struct lpfc_vport *vport;
54201649561SJames Smart 	struct lpfc_hba *phba;
54301649561SJames Smart 	struct lpfc_nodelist *ndlp;
54401649561SJames Smart 	LIST_HEAD(abort_list);
54501649561SJames Smart 	struct lpfc_sli_ring *pring;
54601649561SJames Smart 	struct lpfc_iocbq *wqe, *next_wqe;
54701649561SJames Smart 
54801649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
54901649561SJames Smart 	vport = lport->vport;
55001649561SJames Smart 	phba = vport->phba;
55101649561SJames Smart 
5523386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
5533386f4bdSJames Smart 		return;
5543386f4bdSJames Smart 
55501649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
55601649561SJames Smart 	if (!ndlp) {
55701649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
55801649561SJames Smart 				 "6049 Could not find node for DID %x\n",
55901649561SJames Smart 				 pnvme_rport->port_id);
56001649561SJames Smart 		return;
56101649561SJames Smart 	}
56201649561SJames Smart 
56301649561SJames Smart 	/* Expand print to include key fields. */
56401649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
56501649561SJames Smart 			 "6040 ENTER.  lport %p, rport %p lsreq %p rqstlen:%d "
566825c6abbSArnd Bergmann 			 "rsplen:%d %pad %pad\n",
56701649561SJames Smart 			 pnvme_lport, pnvme_rport,
56801649561SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
569825c6abbSArnd Bergmann 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
570825c6abbSArnd Bergmann 			 &pnvme_lsreq->rspdma);
57101649561SJames Smart 
57201649561SJames Smart 	/*
57301649561SJames Smart 	 * Lock the ELS ring txcmplq and build a local list of all ELS IOs
57401649561SJames Smart 	 * that need an ABTS.  The IOs need to stay on the txcmplq so that
57501649561SJames Smart 	 * the abort operation completes them successfully.
57601649561SJames Smart 	 */
57701649561SJames Smart 	pring = phba->sli4_hba.nvmels_wq->pring;
57801649561SJames Smart 	spin_lock_irq(&phba->hbalock);
57901649561SJames Smart 	spin_lock(&pring->ring_lock);
58001649561SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
58101649561SJames Smart 		/* Add to abort_list on on NDLP match. */
58201649561SJames Smart 		if (lpfc_check_sli_ndlp(phba, pring, wqe, ndlp)) {
58301649561SJames Smart 			wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
58401649561SJames Smart 			list_add_tail(&wqe->dlist, &abort_list);
58501649561SJames Smart 		}
58601649561SJames Smart 	}
58701649561SJames Smart 	spin_unlock(&pring->ring_lock);
58801649561SJames Smart 	spin_unlock_irq(&phba->hbalock);
58901649561SJames Smart 
59001649561SJames Smart 	/* Abort the targeted IOs and remove them from the abort list. */
59101649561SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &abort_list, dlist) {
59201649561SJames Smart 		spin_lock_irq(&phba->hbalock);
59301649561SJames Smart 		list_del_init(&wqe->dlist);
59401649561SJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, wqe);
59501649561SJames Smart 		spin_unlock_irq(&phba->hbalock);
59601649561SJames Smart 	}
59701649561SJames Smart }
59801649561SJames Smart 
59901649561SJames Smart /* Fix up the existing sgls for NVME IO. */
60001649561SJames Smart static void
60101649561SJames Smart lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
60201649561SJames Smart 		       struct lpfc_nvme_buf *lpfc_ncmd,
60301649561SJames Smart 		       struct nvmefc_fcp_req *nCmd)
60401649561SJames Smart {
60501649561SJames Smart 	struct sli4_sge *sgl;
60601649561SJames Smart 	union lpfc_wqe128 *wqe;
60701649561SJames Smart 	uint32_t *wptr, *dptr;
60801649561SJames Smart 
60901649561SJames Smart 	/*
61001649561SJames Smart 	 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
61101649561SJames Smart 	 * match NVME.  NVME sends 96 bytes. Also, use the
61201649561SJames Smart 	 * nvme commands command and response dma addresses
61301649561SJames Smart 	 * rather than the virtual memory to ease the restore
61401649561SJames Smart 	 * operation.
61501649561SJames Smart 	 */
61601649561SJames Smart 	sgl = lpfc_ncmd->nvme_sgl;
61701649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
61801649561SJames Smart 
61901649561SJames Smart 	sgl++;
62001649561SJames Smart 
62101649561SJames Smart 	/* Setup the physical region for the FCP RSP */
62201649561SJames Smart 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
62301649561SJames Smart 	sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
62401649561SJames Smart 	sgl->word2 = le32_to_cpu(sgl->word2);
62501649561SJames Smart 	if (nCmd->sg_cnt)
62601649561SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
62701649561SJames Smart 	else
62801649561SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 1);
62901649561SJames Smart 	sgl->word2 = cpu_to_le32(sgl->word2);
63001649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->rsplen);
63101649561SJames Smart 
63201649561SJames Smart 	/*
63301649561SJames Smart 	 * Get a local pointer to the built-in wqe and correct
63401649561SJames Smart 	 * the cmd size to match NVME's 96 bytes and fix
63501649561SJames Smart 	 * the dma address.
63601649561SJames Smart 	 */
63701649561SJames Smart 
63801649561SJames Smart 	/* 128 byte wqe support here */
63901649561SJames Smart 	wqe = (union lpfc_wqe128 *)&lpfc_ncmd->cur_iocbq.wqe;
64001649561SJames Smart 
64101649561SJames Smart 	/* Word 0-2 - NVME CMND IU (embedded payload) */
64201649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
64301649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = 60;
64401649561SJames Smart 	wqe->generic.bde.addrHigh = 0;
64501649561SJames Smart 	wqe->generic.bde.addrLow =  64;  /* Word 16 */
64601649561SJames Smart 
64701649561SJames Smart 	/* Word 3 */
64801649561SJames Smart 	bf_set(payload_offset_len, &wqe->fcp_icmd,
64901649561SJames Smart 	       (nCmd->rsplen + nCmd->cmdlen));
65001649561SJames Smart 
65101649561SJames Smart 	/* Word 10 */
65201649561SJames Smart 	bf_set(wqe_nvme, &wqe->fcp_icmd.wqe_com, 1);
65301649561SJames Smart 	bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
65401649561SJames Smart 
65501649561SJames Smart 	/*
65601649561SJames Smart 	 * Embed the payload in the last half of the WQE
65701649561SJames Smart 	 * WQE words 16-30 get the NVME CMD IU payload
65801649561SJames Smart 	 *
659b06a622fSJames Smart 	 * WQE words 16-19 get payload Words 1-4
66001649561SJames Smart 	 * WQE words 20-21 get payload Words 6-7
66101649561SJames Smart 	 * WQE words 22-29 get payload Words 16-23
66201649561SJames Smart 	 */
663b06a622fSJames Smart 	wptr = &wqe->words[16];  /* WQE ptr */
66401649561SJames Smart 	dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
665b06a622fSJames Smart 	dptr++;			/* Skip Word 0 in payload */
66601649561SJames Smart 
667b06a622fSJames Smart 	*wptr++ = *dptr++;	/* Word 1 */
66801649561SJames Smart 	*wptr++ = *dptr++;	/* Word 2 */
66901649561SJames Smart 	*wptr++ = *dptr++;	/* Word 3 */
67001649561SJames Smart 	*wptr++ = *dptr++;	/* Word 4 */
67101649561SJames Smart 	dptr++;			/* Skip Word 5 in payload */
67201649561SJames Smart 	*wptr++ = *dptr++;	/* Word 6 */
67301649561SJames Smart 	*wptr++ = *dptr++;	/* Word 7 */
67401649561SJames Smart 	dptr += 8;		/* Skip Words 8-15 in payload */
67501649561SJames Smart 	*wptr++ = *dptr++;	/* Word 16 */
67601649561SJames Smart 	*wptr++ = *dptr++;	/* Word 17 */
67701649561SJames Smart 	*wptr++ = *dptr++;	/* Word 18 */
67801649561SJames Smart 	*wptr++ = *dptr++;	/* Word 19 */
67901649561SJames Smart 	*wptr++ = *dptr++;	/* Word 20 */
68001649561SJames Smart 	*wptr++ = *dptr++;	/* Word 21 */
68101649561SJames Smart 	*wptr++ = *dptr++;	/* Word 22 */
68201649561SJames Smart 	*wptr   = *dptr;	/* Word 23 */
68301649561SJames Smart }
68401649561SJames Smart 
685bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
686bd2cdd5eSJames Smart static void
687bd2cdd5eSJames Smart lpfc_nvme_ktime(struct lpfc_hba *phba,
688bd2cdd5eSJames Smart 		struct lpfc_nvme_buf *lpfc_ncmd)
689bd2cdd5eSJames Smart {
690bd2cdd5eSJames Smart 	uint64_t seg1, seg2, seg3, seg4;
691c8a4ce0bSDick Kennedy 	uint64_t segsum;
692bd2cdd5eSJames Smart 
693bd2cdd5eSJames Smart 	if (!lpfc_ncmd->ts_last_cmd ||
694bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_cmd_start ||
695bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_cmd_wqput ||
696bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_isr_cmpl ||
697bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_data_nvme)
698bd2cdd5eSJames Smart 		return;
699c8a4ce0bSDick Kennedy 
700c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_cmd_start)
701c8a4ce0bSDick Kennedy 		return;
702bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_cmd_start < lpfc_ncmd->ts_last_cmd)
703bd2cdd5eSJames Smart 		return;
704bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_cmd_wqput < lpfc_ncmd->ts_cmd_start)
705bd2cdd5eSJames Smart 		return;
706bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_isr_cmpl < lpfc_ncmd->ts_cmd_wqput)
707bd2cdd5eSJames Smart 		return;
708bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_isr_cmpl)
709bd2cdd5eSJames Smart 		return;
710bd2cdd5eSJames Smart 	/*
711bd2cdd5eSJames Smart 	 * Segment 1 - Time from Last FCP command cmpl is handed
712bd2cdd5eSJames Smart 	 * off to NVME Layer to start of next command.
713bd2cdd5eSJames Smart 	 * Segment 2 - Time from Driver receives a IO cmd start
714bd2cdd5eSJames Smart 	 * from NVME Layer to WQ put is done on IO cmd.
715bd2cdd5eSJames Smart 	 * Segment 3 - Time from Driver WQ put is done on IO cmd
716bd2cdd5eSJames Smart 	 * to MSI-X ISR for IO cmpl.
717bd2cdd5eSJames Smart 	 * Segment 4 - Time from MSI-X ISR for IO cmpl to when
718bd2cdd5eSJames Smart 	 * cmpl is handled off to the NVME Layer.
719bd2cdd5eSJames Smart 	 */
720bd2cdd5eSJames Smart 	seg1 = lpfc_ncmd->ts_cmd_start - lpfc_ncmd->ts_last_cmd;
721c8a4ce0bSDick Kennedy 	if (seg1 > 5000000)  /* 5 ms - for sequential IOs only */
722c8a4ce0bSDick Kennedy 		seg1 = 0;
723bd2cdd5eSJames Smart 
724bd2cdd5eSJames Smart 	/* Calculate times relative to start of IO */
725bd2cdd5eSJames Smart 	seg2 = (lpfc_ncmd->ts_cmd_wqput - lpfc_ncmd->ts_cmd_start);
726c8a4ce0bSDick Kennedy 	segsum = seg2;
727c8a4ce0bSDick Kennedy 	seg3 = lpfc_ncmd->ts_isr_cmpl - lpfc_ncmd->ts_cmd_start;
728c8a4ce0bSDick Kennedy 	if (segsum > seg3)
729c8a4ce0bSDick Kennedy 		return;
730c8a4ce0bSDick Kennedy 	seg3 -= segsum;
731c8a4ce0bSDick Kennedy 	segsum += seg3;
732c8a4ce0bSDick Kennedy 
733c8a4ce0bSDick Kennedy 	seg4 = lpfc_ncmd->ts_data_nvme - lpfc_ncmd->ts_cmd_start;
734c8a4ce0bSDick Kennedy 	if (segsum > seg4)
735c8a4ce0bSDick Kennedy 		return;
736c8a4ce0bSDick Kennedy 	seg4 -= segsum;
737c8a4ce0bSDick Kennedy 
738bd2cdd5eSJames Smart 	phba->ktime_data_samples++;
739bd2cdd5eSJames Smart 	phba->ktime_seg1_total += seg1;
740bd2cdd5eSJames Smart 	if (seg1 < phba->ktime_seg1_min)
741bd2cdd5eSJames Smart 		phba->ktime_seg1_min = seg1;
742bd2cdd5eSJames Smart 	else if (seg1 > phba->ktime_seg1_max)
743bd2cdd5eSJames Smart 		phba->ktime_seg1_max = seg1;
744bd2cdd5eSJames Smart 	phba->ktime_seg2_total += seg2;
745bd2cdd5eSJames Smart 	if (seg2 < phba->ktime_seg2_min)
746bd2cdd5eSJames Smart 		phba->ktime_seg2_min = seg2;
747bd2cdd5eSJames Smart 	else if (seg2 > phba->ktime_seg2_max)
748bd2cdd5eSJames Smart 		phba->ktime_seg2_max = seg2;
749bd2cdd5eSJames Smart 	phba->ktime_seg3_total += seg3;
750bd2cdd5eSJames Smart 	if (seg3 < phba->ktime_seg3_min)
751bd2cdd5eSJames Smart 		phba->ktime_seg3_min = seg3;
752bd2cdd5eSJames Smart 	else if (seg3 > phba->ktime_seg3_max)
753bd2cdd5eSJames Smart 		phba->ktime_seg3_max = seg3;
754bd2cdd5eSJames Smart 	phba->ktime_seg4_total += seg4;
755bd2cdd5eSJames Smart 	if (seg4 < phba->ktime_seg4_min)
756bd2cdd5eSJames Smart 		phba->ktime_seg4_min = seg4;
757bd2cdd5eSJames Smart 	else if (seg4 > phba->ktime_seg4_max)
758bd2cdd5eSJames Smart 		phba->ktime_seg4_max = seg4;
759bd2cdd5eSJames Smart 
760bd2cdd5eSJames Smart 	lpfc_ncmd->ts_last_cmd = 0;
761bd2cdd5eSJames Smart 	lpfc_ncmd->ts_cmd_start = 0;
762bd2cdd5eSJames Smart 	lpfc_ncmd->ts_cmd_wqput  = 0;
763bd2cdd5eSJames Smart 	lpfc_ncmd->ts_isr_cmpl = 0;
764bd2cdd5eSJames Smart 	lpfc_ncmd->ts_data_nvme = 0;
765bd2cdd5eSJames Smart }
766bd2cdd5eSJames Smart #endif
767bd2cdd5eSJames Smart 
76801649561SJames Smart /**
76901649561SJames Smart  * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
77001649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
77101649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
77201649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
77301649561SJames Smart  *
77401649561SJames Smart  * Driver registers this routine as it io request handler.  This
77501649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
77601649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
77701649561SJames Smart  *
77801649561SJames Smart  * Return value :
77901649561SJames Smart  *   0 - Success
78001649561SJames Smart  *   TODO: What are the failure codes.
78101649561SJames Smart  **/
78201649561SJames Smart static void
78301649561SJames Smart lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
78401649561SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
78501649561SJames Smart {
78601649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd =
78701649561SJames Smart 		(struct lpfc_nvme_buf *)pwqeIn->context1;
78801649561SJames Smart 	struct lpfc_vport *vport = pwqeIn->vport;
78901649561SJames Smart 	struct nvmefc_fcp_req *nCmd;
79001649561SJames Smart 	struct nvme_fc_ersp_iu *ep;
79101649561SJames Smart 	struct nvme_fc_cmd_iu *cp;
79201649561SJames Smart 	struct lpfc_nvme_rport *rport;
79301649561SJames Smart 	struct lpfc_nodelist *ndlp;
794bbe3012bSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
79501649561SJames Smart 	unsigned long flags;
79601649561SJames Smart 	uint32_t code;
79701649561SJames Smart 	uint16_t cid, sqhd, data;
79801649561SJames Smart 	uint32_t *ptr;
79901649561SJames Smart 
80001649561SJames Smart 	/* Sanity check on return of outstanding command */
80101649561SJames Smart 	if (!lpfc_ncmd || !lpfc_ncmd->nvmeCmd || !lpfc_ncmd->nrport) {
80201649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
80301649561SJames Smart 				 "6071 Completion pointers bad on wqe %p.\n",
80401649561SJames Smart 				 wcqe);
80501649561SJames Smart 		return;
80601649561SJames Smart 	}
8072cee7808SJames Smart 	atomic_inc(&phba->fc4NvmeIoCmpls);
80801649561SJames Smart 
80901649561SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
81001649561SJames Smart 	rport = lpfc_ncmd->nrport;
81101649561SJames Smart 
812bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
813bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
814bd2cdd5eSJames Smart 			 bf_get(lpfc_wcqe_c_status, wcqe), wcqe->parameter);
81501649561SJames Smart 	/*
81601649561SJames Smart 	 * Catch race where our node has transitioned, but the
81701649561SJames Smart 	 * transport is still transitioning.
81801649561SJames Smart 	 */
81901649561SJames Smart 	ndlp = rport->ndlp;
82001649561SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
82101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
822c154e750SJames Smart 				 "6061 rport %p,  DID x%06x node not ready.\n",
823c154e750SJames Smart 				 rport, rport->remoteport->port_id);
82401649561SJames Smart 
82501649561SJames Smart 		ndlp = lpfc_findnode_did(vport, rport->remoteport->port_id);
82601649561SJames Smart 		if (!ndlp) {
82701649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
82801649561SJames Smart 					 "6062 Ignoring NVME cmpl.  No ndlp\n");
82901649561SJames Smart 			goto out_err;
83001649561SJames Smart 		}
83101649561SJames Smart 	}
83201649561SJames Smart 
83301649561SJames Smart 	code = bf_get(lpfc_wcqe_c_code, wcqe);
83401649561SJames Smart 	if (code == CQE_CODE_NVME_ERSP) {
83501649561SJames Smart 		/* For this type of CQE, we need to rebuild the rsp */
83601649561SJames Smart 		ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
83701649561SJames Smart 
83801649561SJames Smart 		/*
83901649561SJames Smart 		 * Get Command Id from cmd to plug into response. This
84001649561SJames Smart 		 * code is not needed in the next NVME Transport drop.
84101649561SJames Smart 		 */
84201649561SJames Smart 		cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
84301649561SJames Smart 		cid = cp->sqe.common.command_id;
84401649561SJames Smart 
84501649561SJames Smart 		/*
84601649561SJames Smart 		 * RSN is in CQE word 2
84701649561SJames Smart 		 * SQHD is in CQE Word 3 bits 15:0
84801649561SJames Smart 		 * Cmd Specific info is in CQE Word 1
84901649561SJames Smart 		 * and in CQE Word 0 bits 15:0
85001649561SJames Smart 		 */
85101649561SJames Smart 		sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
85201649561SJames Smart 
85301649561SJames Smart 		/* Now lets build the NVME ERSP IU */
85401649561SJames Smart 		ep->iu_len = cpu_to_be16(8);
85501649561SJames Smart 		ep->rsn = wcqe->parameter;
85601649561SJames Smart 		ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
85701649561SJames Smart 		ep->rsvd12 = 0;
85801649561SJames Smart 		ptr = (uint32_t *)&ep->cqe.result.u64;
85901649561SJames Smart 		*ptr++ = wcqe->total_data_placed;
86001649561SJames Smart 		data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
86101649561SJames Smart 		*ptr = (uint32_t)data;
86201649561SJames Smart 		ep->cqe.sq_head = sqhd;
86301649561SJames Smart 		ep->cqe.sq_id =  nCmd->sqid;
86401649561SJames Smart 		ep->cqe.command_id = cid;
86501649561SJames Smart 		ep->cqe.status = 0;
86601649561SJames Smart 
86701649561SJames Smart 		lpfc_ncmd->status = IOSTAT_SUCCESS;
86801649561SJames Smart 		lpfc_ncmd->result = 0;
86901649561SJames Smart 		nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
87001649561SJames Smart 		nCmd->transferred_length = nCmd->payload_length;
87101649561SJames Smart 	} else {
87201649561SJames Smart 		lpfc_ncmd->status = (bf_get(lpfc_wcqe_c_status, wcqe) &
87301649561SJames Smart 			    LPFC_IOCB_STATUS_MASK);
874952c303bSDick Kennedy 		lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
87501649561SJames Smart 
87601649561SJames Smart 		/* For NVME, the only failure path that results in an
87701649561SJames Smart 		 * IO error is when the adapter rejects it.  All other
87801649561SJames Smart 		 * conditions are a success case and resolved by the
87901649561SJames Smart 		 * transport.
88001649561SJames Smart 		 * IOSTAT_FCP_RSP_ERROR means:
88101649561SJames Smart 		 * 1. Length of data received doesn't match total
88201649561SJames Smart 		 *    transfer length in WQE
88301649561SJames Smart 		 * 2. If the RSP payload does NOT match these cases:
88401649561SJames Smart 		 *    a. RSP length 12/24 bytes and all zeros
88501649561SJames Smart 		 *    b. NVME ERSP
88601649561SJames Smart 		 */
88701649561SJames Smart 		switch (lpfc_ncmd->status) {
88801649561SJames Smart 		case IOSTAT_SUCCESS:
88901649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
89001649561SJames Smart 			nCmd->rcv_rsplen = 0;
89101649561SJames Smart 			nCmd->status = 0;
89201649561SJames Smart 			break;
89301649561SJames Smart 		case IOSTAT_FCP_RSP_ERROR:
89401649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
89501649561SJames Smart 			nCmd->rcv_rsplen = wcqe->parameter;
89601649561SJames Smart 			nCmd->status = 0;
89701649561SJames Smart 			/* Sanity check */
89801649561SJames Smart 			if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
89901649561SJames Smart 				break;
90001649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
90101649561SJames Smart 					 "6081 NVME Completion Protocol Error: "
90286c67379SJames Smart 					 "xri %x status x%x result x%x "
90386c67379SJames Smart 					 "placed x%x\n",
90486c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
90501649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
90601649561SJames Smart 					 wcqe->total_data_placed);
90701649561SJames Smart 			break;
908952c303bSDick Kennedy 		case IOSTAT_LOCAL_REJECT:
909952c303bSDick Kennedy 			/* Let fall through to set command final state. */
910952c303bSDick Kennedy 			if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
911952c303bSDick Kennedy 				lpfc_printf_vlog(vport, KERN_INFO,
912952c303bSDick Kennedy 					 LOG_NVME_IOERR,
913952c303bSDick Kennedy 					 "6032 Delay Aborted cmd %p "
914952c303bSDick Kennedy 					 "nvme cmd %p, xri x%x, "
915952c303bSDick Kennedy 					 "xb %d\n",
916952c303bSDick Kennedy 					 lpfc_ncmd, nCmd,
917952c303bSDick Kennedy 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
918952c303bSDick Kennedy 					 bf_get(lpfc_wcqe_c_xb, wcqe));
91901649561SJames Smart 		default:
92001649561SJames Smart out_err:
921e3246a12SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
92286c67379SJames Smart 					 "6072 NVME Completion Error: xri %x "
92301649561SJames Smart 					 "status x%x result x%x placed x%x\n",
92486c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
92501649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
92601649561SJames Smart 					 wcqe->total_data_placed);
92701649561SJames Smart 			nCmd->transferred_length = 0;
92801649561SJames Smart 			nCmd->rcv_rsplen = 0;
9298e009ce8SJames Smart 			nCmd->status = NVME_SC_INTERNAL;
93001649561SJames Smart 		}
93101649561SJames Smart 	}
93201649561SJames Smart 
93301649561SJames Smart 	/* pick up SLI4 exhange busy condition */
93401649561SJames Smart 	if (bf_get(lpfc_wcqe_c_xb, wcqe))
93501649561SJames Smart 		lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
93601649561SJames Smart 	else
93701649561SJames Smart 		lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
93801649561SJames Smart 
93901649561SJames Smart 	if (ndlp && NLP_CHK_NODE_ACT(ndlp))
94001649561SJames Smart 		atomic_dec(&ndlp->cmd_pending);
94101649561SJames Smart 
94201649561SJames Smart 	/* Update stats and complete the IO.  There is
94301649561SJames Smart 	 * no need for dma unprep because the nvme_transport
94401649561SJames Smart 	 * owns the dma address.
94501649561SJames Smart 	 */
946bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
947c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start) {
948bd2cdd5eSJames Smart 		lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
949bd2cdd5eSJames Smart 		lpfc_ncmd->ts_data_nvme = ktime_get_ns();
950bd2cdd5eSJames Smart 		phba->ktime_last_cmd = lpfc_ncmd->ts_data_nvme;
951bd2cdd5eSJames Smart 		lpfc_nvme_ktime(phba, lpfc_ncmd);
952bd2cdd5eSJames Smart 	}
953bd2cdd5eSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
954bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu != smp_processor_id())
955bd2cdd5eSJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
956bd2cdd5eSJames Smart 					 "6701 CPU Check cmpl: "
957bd2cdd5eSJames Smart 					 "cpu %d expect %d\n",
958bd2cdd5eSJames Smart 					 smp_processor_id(), lpfc_ncmd->cpu);
959bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
960bd2cdd5eSJames Smart 			phba->cpucheck_cmpl_io[lpfc_ncmd->cpu]++;
961bd2cdd5eSJames Smart 	}
962bd2cdd5eSJames Smart #endif
963bbe3012bSJames Smart 	freqpriv = nCmd->private;
964bbe3012bSJames Smart 	freqpriv->nvme_buf = NULL;
965952c303bSDick Kennedy 
966952c303bSDick Kennedy 	/* NVME targets need completion held off until the abort exchange
967add9d6beSJames Smart 	 * completes unless the NVME Rport is getting unregistered.
968952c303bSDick Kennedy 	 */
969add9d6beSJames Smart 	if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY) ||
970add9d6beSJames Smart 	    ndlp->upcall_flags & NLP_WAIT_FOR_UNREG) {
971add9d6beSJames Smart 		/* Clear the XBUSY flag to prevent double completions.
972add9d6beSJames Smart 		 * The nvme rport is getting unregistered and there is
973add9d6beSJames Smart 		 * no need to defer the IO.
974add9d6beSJames Smart 		 */
975add9d6beSJames Smart 		if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY)
976add9d6beSJames Smart 			lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
977add9d6beSJames Smart 
97801649561SJames Smart 		nCmd->done(nCmd);
979add9d6beSJames Smart 	}
98001649561SJames Smart 
98101649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
98201649561SJames Smart 	lpfc_ncmd->nrport = NULL;
98301649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
98401649561SJames Smart 
985952c303bSDick Kennedy 	/* Call release with XB=1 to queue the IO into the abort list. */
98601649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
98701649561SJames Smart }
98801649561SJames Smart 
98901649561SJames Smart 
99001649561SJames Smart /**
99101649561SJames Smart  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
99201649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
99301649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
99401649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
99501649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
99601649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
99701649561SJames Smart  *
99801649561SJames Smart  * Driver registers this routine as it io request handler.  This
99901649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
100001649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
100101649561SJames Smart  *
100201649561SJames Smart  * Return value :
100301649561SJames Smart  *   0 - Success
100401649561SJames Smart  *   TODO: What are the failure codes.
100501649561SJames Smart  **/
100601649561SJames Smart static int
100701649561SJames Smart lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
100801649561SJames Smart 		      struct lpfc_nvme_buf *lpfc_ncmd,
100901649561SJames Smart 		      struct lpfc_nodelist *pnode)
101001649561SJames Smart {
101101649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
101201649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
101301649561SJames Smart 	struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
101401649561SJames Smart 	union lpfc_wqe128 *wqe = (union lpfc_wqe128 *)&pwqeq->wqe;
101501649561SJames Smart 	uint32_t req_len;
101601649561SJames Smart 
101701649561SJames Smart 	if (!pnode || !NLP_CHK_NODE_ACT(pnode))
101801649561SJames Smart 		return -EINVAL;
101901649561SJames Smart 
102001649561SJames Smart 	/*
102101649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
102201649561SJames Smart 	 * the single mapping, or neither.
102301649561SJames Smart 	 */
102401649561SJames Smart 	wqe->fcp_iwrite.initial_xfer_len = 0;
102501649561SJames Smart 	if (nCmd->sg_cnt) {
102601649561SJames Smart 		if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
102701649561SJames Smart 			/* Word 5 */
102801649561SJames Smart 			if ((phba->cfg_nvme_enable_fb) &&
102901649561SJames Smart 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
103001649561SJames Smart 				req_len = lpfc_ncmd->nvmeCmd->payload_length;
103101649561SJames Smart 				if (req_len < pnode->nvme_fb_size)
103201649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
103301649561SJames Smart 						req_len;
103401649561SJames Smart 				else
103501649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
103601649561SJames Smart 						pnode->nvme_fb_size;
103701649561SJames Smart 			}
103801649561SJames Smart 
103901649561SJames Smart 			/* Word 7 */
104001649561SJames Smart 			bf_set(wqe_cmnd, &wqe->generic.wqe_com,
104101649561SJames Smart 			       CMD_FCP_IWRITE64_WQE);
104201649561SJames Smart 			bf_set(wqe_pu, &wqe->generic.wqe_com,
104301649561SJames Smart 			       PARM_READ_CHECK);
104401649561SJames Smart 
104501649561SJames Smart 			/* Word 10 */
104601649561SJames Smart 			bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
104701649561SJames Smart 			bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com,
104801649561SJames Smart 			       LPFC_WQE_IOD_WRITE);
104901649561SJames Smart 			bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
105001649561SJames Smart 			       LPFC_WQE_LENLOC_WORD4);
105101649561SJames Smart 			if (phba->cfg_nvme_oas)
105201649561SJames Smart 				bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
105301649561SJames Smart 
105401649561SJames Smart 			/* Word 11 */
105501649561SJames Smart 			bf_set(wqe_cmd_type, &wqe->generic.wqe_com,
105601649561SJames Smart 			       NVME_WRITE_CMD);
105701649561SJames Smart 
10582cee7808SJames Smart 			atomic_inc(&phba->fc4NvmeOutputRequests);
105901649561SJames Smart 		} else {
106001649561SJames Smart 			/* Word 7 */
106101649561SJames Smart 			bf_set(wqe_cmnd, &wqe->generic.wqe_com,
106201649561SJames Smart 			       CMD_FCP_IREAD64_WQE);
106301649561SJames Smart 			bf_set(wqe_pu, &wqe->generic.wqe_com,
106401649561SJames Smart 			       PARM_READ_CHECK);
106501649561SJames Smart 
106601649561SJames Smart 			/* Word 10 */
106701649561SJames Smart 			bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
106801649561SJames Smart 			bf_set(wqe_iod, &wqe->fcp_iread.wqe_com,
106901649561SJames Smart 			       LPFC_WQE_IOD_READ);
107001649561SJames Smart 			bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
107101649561SJames Smart 			       LPFC_WQE_LENLOC_WORD4);
107201649561SJames Smart 			if (phba->cfg_nvme_oas)
107301649561SJames Smart 				bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
107401649561SJames Smart 
107501649561SJames Smart 			/* Word 11 */
107601649561SJames Smart 			bf_set(wqe_cmd_type, &wqe->generic.wqe_com,
107701649561SJames Smart 			       NVME_READ_CMD);
107801649561SJames Smart 
10792cee7808SJames Smart 			atomic_inc(&phba->fc4NvmeInputRequests);
108001649561SJames Smart 		}
108101649561SJames Smart 	} else {
108201649561SJames Smart 		/* Word 4 */
108301649561SJames Smart 		wqe->fcp_icmd.rsrvd4 = 0;
108401649561SJames Smart 
108501649561SJames Smart 		/* Word 7 */
108601649561SJames Smart 		bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_FCP_ICMND64_WQE);
108701649561SJames Smart 		bf_set(wqe_pu, &wqe->generic.wqe_com, 0);
108801649561SJames Smart 
108901649561SJames Smart 		/* Word 10 */
109001649561SJames Smart 		bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
109101649561SJames Smart 		bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
109201649561SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
109301649561SJames Smart 		       LPFC_WQE_LENLOC_NONE);
109401649561SJames Smart 		if (phba->cfg_nvme_oas)
109501649561SJames Smart 			bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
109601649561SJames Smart 
109701649561SJames Smart 		/* Word 11 */
109801649561SJames Smart 		bf_set(wqe_cmd_type, &wqe->generic.wqe_com, NVME_READ_CMD);
109901649561SJames Smart 
11002cee7808SJames Smart 		atomic_inc(&phba->fc4NvmeControlRequests);
110101649561SJames Smart 	}
110201649561SJames Smart 	/*
110301649561SJames Smart 	 * Finish initializing those WQE fields that are independent
110401649561SJames Smart 	 * of the nvme_cmnd request_buffer
110501649561SJames Smart 	 */
110601649561SJames Smart 
110701649561SJames Smart 	/* Word 6 */
110801649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
110901649561SJames Smart 	       phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
111001649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
111101649561SJames Smart 
111201649561SJames Smart 	/* Word 7 */
111301649561SJames Smart 	/* Preserve Class data in the ndlp. */
111401649561SJames Smart 	bf_set(wqe_class, &wqe->generic.wqe_com,
111501649561SJames Smart 	       (pnode->nlp_fcp_info & 0x0f));
111601649561SJames Smart 
111701649561SJames Smart 	/* Word 8 */
111801649561SJames Smart 	wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
111901649561SJames Smart 
112001649561SJames Smart 	/* Word 9 */
112101649561SJames Smart 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
112201649561SJames Smart 
112301649561SJames Smart 	/* Word 11 */
112401649561SJames Smart 	bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
112501649561SJames Smart 
112601649561SJames Smart 	pwqeq->vport = vport;
112701649561SJames Smart 	return 0;
112801649561SJames Smart }
112901649561SJames Smart 
113001649561SJames Smart 
113101649561SJames Smart /**
113201649561SJames Smart  * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
113301649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
113401649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
113501649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
113601649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
113701649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
113801649561SJames Smart  *
113901649561SJames Smart  * Driver registers this routine as it io request handler.  This
114001649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
114101649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
114201649561SJames Smart  *
114301649561SJames Smart  * Return value :
114401649561SJames Smart  *   0 - Success
114501649561SJames Smart  *   TODO: What are the failure codes.
114601649561SJames Smart  **/
114701649561SJames Smart static int
114801649561SJames Smart lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
114901649561SJames Smart 		      struct lpfc_nvme_buf *lpfc_ncmd)
115001649561SJames Smart {
115101649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
115201649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
115301649561SJames Smart 	union lpfc_wqe128 *wqe = (union lpfc_wqe128 *)&lpfc_ncmd->cur_iocbq.wqe;
115401649561SJames Smart 	struct sli4_sge *sgl = lpfc_ncmd->nvme_sgl;
115501649561SJames Smart 	struct scatterlist *data_sg;
115601649561SJames Smart 	struct sli4_sge *first_data_sgl;
115701649561SJames Smart 	dma_addr_t physaddr;
115801649561SJames Smart 	uint32_t num_bde = 0;
115901649561SJames Smart 	uint32_t dma_len;
116001649561SJames Smart 	uint32_t dma_offset = 0;
116101649561SJames Smart 	int nseg, i;
116201649561SJames Smart 
116301649561SJames Smart 	/* Fix up the command and response DMA stuff. */
116401649561SJames Smart 	lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
116501649561SJames Smart 
116601649561SJames Smart 	/*
116701649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
116801649561SJames Smart 	 * the single mapping, or neither.
116901649561SJames Smart 	 */
117001649561SJames Smart 	if (nCmd->sg_cnt) {
117101649561SJames Smart 		/*
117201649561SJames Smart 		 * Jump over the cmd and rsp SGEs.  The fix routine
117301649561SJames Smart 		 * has already adjusted for this.
117401649561SJames Smart 		 */
117501649561SJames Smart 		sgl += 2;
117601649561SJames Smart 
117701649561SJames Smart 		first_data_sgl = sgl;
117801649561SJames Smart 		lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
117981e6a637SJames Smart 		if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
118001649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
118101649561SJames Smart 					"6058 Too many sg segments from "
118201649561SJames Smart 					"NVME Transport.  Max %d, "
118301649561SJames Smart 					"nvmeIO sg_cnt %d\n",
118429bfd55aSJames Smart 					phba->cfg_nvme_seg_cnt + 1,
118501649561SJames Smart 					lpfc_ncmd->seg_cnt);
118601649561SJames Smart 			lpfc_ncmd->seg_cnt = 0;
118701649561SJames Smart 			return 1;
118801649561SJames Smart 		}
118901649561SJames Smart 
119001649561SJames Smart 		/*
119101649561SJames Smart 		 * The driver established a maximum scatter-gather segment count
119201649561SJames Smart 		 * during probe that limits the number of sg elements in any
119301649561SJames Smart 		 * single nvme command.  Just run through the seg_cnt and format
119401649561SJames Smart 		 * the sge's.
119501649561SJames Smart 		 */
119601649561SJames Smart 		nseg = nCmd->sg_cnt;
119701649561SJames Smart 		data_sg = nCmd->first_sgl;
119801649561SJames Smart 		for (i = 0; i < nseg; i++) {
119901649561SJames Smart 			if (data_sg == NULL) {
120001649561SJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
120101649561SJames Smart 						"6059 dptr err %d, nseg %d\n",
120201649561SJames Smart 						i, nseg);
120301649561SJames Smart 				lpfc_ncmd->seg_cnt = 0;
120401649561SJames Smart 				return 1;
120501649561SJames Smart 			}
120601649561SJames Smart 			physaddr = data_sg->dma_address;
120701649561SJames Smart 			dma_len = data_sg->length;
120801649561SJames Smart 			sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
120901649561SJames Smart 			sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
121001649561SJames Smart 			sgl->word2 = le32_to_cpu(sgl->word2);
121101649561SJames Smart 			if ((num_bde + 1) == nseg)
121201649561SJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 1);
121301649561SJames Smart 			else
121401649561SJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 0);
121501649561SJames Smart 			bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
121601649561SJames Smart 			bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
121701649561SJames Smart 			sgl->word2 = cpu_to_le32(sgl->word2);
121801649561SJames Smart 			sgl->sge_len = cpu_to_le32(dma_len);
121901649561SJames Smart 
122001649561SJames Smart 			dma_offset += dma_len;
122101649561SJames Smart 			data_sg = sg_next(data_sg);
122201649561SJames Smart 			sgl++;
122301649561SJames Smart 		}
122401649561SJames Smart 	} else {
122501649561SJames Smart 		/* For this clause to be valid, the payload_length
122601649561SJames Smart 		 * and sg_cnt must zero.
122701649561SJames Smart 		 */
122801649561SJames Smart 		if (nCmd->payload_length != 0) {
122901649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
123001649561SJames Smart 					"6063 NVME DMA Prep Err: sg_cnt %d "
123101649561SJames Smart 					"payload_length x%x\n",
123201649561SJames Smart 					nCmd->sg_cnt, nCmd->payload_length);
123301649561SJames Smart 			return 1;
123401649561SJames Smart 		}
123501649561SJames Smart 	}
123601649561SJames Smart 
123701649561SJames Smart 	/*
123801649561SJames Smart 	 * Due to difference in data length between DIF/non-DIF paths,
123901649561SJames Smart 	 * we need to set word 4 of WQE here
124001649561SJames Smart 	 */
124101649561SJames Smart 	wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
124201649561SJames Smart 	return 0;
124301649561SJames Smart }
124401649561SJames Smart 
124501649561SJames Smart /**
124601649561SJames Smart  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
124701649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
124801649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
124901649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
125001649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
125101649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
125201649561SJames Smart  *
125301649561SJames Smart  * Driver registers this routine as it io request handler.  This
125401649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
125501649561SJames Smart  * data structure to the rport
125601649561SJames Smart  indicated in @lpfc_nvme_rport.
125701649561SJames Smart  *
125801649561SJames Smart  * Return value :
125901649561SJames Smart  *   0 - Success
126001649561SJames Smart  *   TODO: What are the failure codes.
126101649561SJames Smart  **/
126201649561SJames Smart static int
126301649561SJames Smart lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
126401649561SJames Smart 			struct nvme_fc_remote_port *pnvme_rport,
126501649561SJames Smart 			void *hw_queue_handle,
126601649561SJames Smart 			struct nvmefc_fcp_req *pnvme_fcreq)
126701649561SJames Smart {
126801649561SJames Smart 	int ret = 0;
1269*cf1a1d3eSJames Smart 	int expedite = 0;
127001649561SJames Smart 	struct lpfc_nvme_lport *lport;
127101649561SJames Smart 	struct lpfc_vport *vport;
127201649561SJames Smart 	struct lpfc_hba *phba;
127301649561SJames Smart 	struct lpfc_nodelist *ndlp;
127401649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd;
127501649561SJames Smart 	struct lpfc_nvme_rport *rport;
127601649561SJames Smart 	struct lpfc_nvme_qhandle *lpfc_queue_info;
1277c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
1278*cf1a1d3eSJames Smart 	struct nvme_common_command *sqe;
1279bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1280bd2cdd5eSJames Smart 	uint64_t start = 0;
1281bd2cdd5eSJames Smart #endif
128201649561SJames Smart 
1283c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1284c3725bdcSJames Smart 	 * have timing races.
1285c3725bdcSJames Smart 	 */
128601649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1287c3725bdcSJames Smart 	if (unlikely(!lport)) {
1288c3725bdcSJames Smart 		ret = -EINVAL;
1289c3725bdcSJames Smart 		goto out_fail;
1290c3725bdcSJames Smart 	}
1291c3725bdcSJames Smart 
129201649561SJames Smart 	vport = lport->vport;
1293c3725bdcSJames Smart 
1294c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
1295c3725bdcSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1296c3725bdcSJames Smart 				 "6129 Fail Abort, NULL hw_queue_handle\n");
1297c3725bdcSJames Smart 		ret = -EINVAL;
1298c3725bdcSJames Smart 		goto out_fail;
1299c3725bdcSJames Smart 	}
1300c3725bdcSJames Smart 
130101649561SJames Smart 	phba = vport->phba;
130201649561SJames Smart 
13031abcb371SDick Kennedy 	if (vport->load_flag & FC_UNLOADING) {
13041abcb371SDick Kennedy 		ret = -ENODEV;
13051abcb371SDick Kennedy 		goto out_fail;
13061abcb371SDick Kennedy 	}
13071abcb371SDick Kennedy 
13083386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING) {
13093386f4bdSJames Smart 		ret = -ENODEV;
13103386f4bdSJames Smart 		goto out_fail;
13113386f4bdSJames Smart 	}
13123386f4bdSJames Smart 
1313c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1314c3725bdcSJames Smart 	if (unlikely(!freqpriv)) {
1315c3725bdcSJames Smart 		ret = -EINVAL;
1316b7672ae6SDick Kennedy 		goto out_fail;
1317b7672ae6SDick Kennedy 	}
1318b7672ae6SDick Kennedy 
1319bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1320bd2cdd5eSJames Smart 	if (phba->ktime_on)
1321bd2cdd5eSJames Smart 		start = ktime_get_ns();
1322bd2cdd5eSJames Smart #endif
132301649561SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
132401649561SJames Smart 	lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
132501649561SJames Smart 
132601649561SJames Smart 	/*
132701649561SJames Smart 	 * Catch race where our node has transitioned, but the
132801649561SJames Smart 	 * transport is still transitioning.
132901649561SJames Smart 	 */
133001649561SJames Smart 	ndlp = rport->ndlp;
133101649561SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
133201649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
133301649561SJames Smart 				 "6053 rport %p, ndlp %p, DID x%06x "
133401649561SJames Smart 				 "ndlp not ready.\n",
133501649561SJames Smart 				 rport, ndlp, pnvme_rport->port_id);
133601649561SJames Smart 
133701649561SJames Smart 		ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
133801649561SJames Smart 		if (!ndlp) {
133901649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
134001649561SJames Smart 					 "6066 Missing node for DID %x\n",
134101649561SJames Smart 					 pnvme_rport->port_id);
134201649561SJames Smart 			ret = -ENODEV;
134301649561SJames Smart 			goto out_fail;
134401649561SJames Smart 		}
134501649561SJames Smart 	}
134601649561SJames Smart 
134701649561SJames Smart 	/* The remote node has to be a mapped target or it's an error. */
134801649561SJames Smart 	if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
134901649561SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
135001649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
135101649561SJames Smart 				 "6036 rport %p, DID x%06x not ready for "
135201649561SJames Smart 				 "IO. State x%x, Type x%x\n",
135301649561SJames Smart 				 rport, pnvme_rport->port_id,
135401649561SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type);
135501649561SJames Smart 		ret = -ENODEV;
135601649561SJames Smart 		goto out_fail;
135701649561SJames Smart 
135801649561SJames Smart 	}
135901649561SJames Smart 
1360*cf1a1d3eSJames Smart 	/* Currently only NVME Keep alive commands should be expedited
1361*cf1a1d3eSJames Smart 	 * if the driver runs out of a resource. These should only be
1362*cf1a1d3eSJames Smart 	 * issued on the admin queue, qidx 0
1363*cf1a1d3eSJames Smart 	 */
1364*cf1a1d3eSJames Smart 	if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1365*cf1a1d3eSJames Smart 		sqe = &((struct nvme_fc_cmd_iu *)
1366*cf1a1d3eSJames Smart 			pnvme_fcreq->cmdaddr)->sqe.common;
1367*cf1a1d3eSJames Smart 		if (sqe->opcode == nvme_admin_keep_alive)
1368*cf1a1d3eSJames Smart 			expedite = 1;
1369*cf1a1d3eSJames Smart 	}
1370*cf1a1d3eSJames Smart 
137101649561SJames Smart 	/* The node is shared with FCP IO, make sure the IO pending count does
137201649561SJames Smart 	 * not exceed the programmed depth.
137301649561SJames Smart 	 */
1374*cf1a1d3eSJames Smart 	if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1375*cf1a1d3eSJames Smart 	    !expedite) {
1376cd22d605SDick Kennedy 		ret = -EBUSY;
137701649561SJames Smart 		goto out_fail;
137801649561SJames Smart 	}
137901649561SJames Smart 
1380*cf1a1d3eSJames Smart 	lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, expedite);
138101649561SJames Smart 	if (lpfc_ncmd == NULL) {
138201649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
138301649561SJames Smart 				 "6065 driver's buffer pool is empty, "
138401649561SJames Smart 				 "IO failed\n");
1385cd22d605SDick Kennedy 		ret = -EBUSY;
138601649561SJames Smart 		goto out_fail;
138701649561SJames Smart 	}
1388bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1389c8a4ce0bSDick Kennedy 	if (start) {
1390bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start = start;
1391bd2cdd5eSJames Smart 		lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1392c8a4ce0bSDick Kennedy 	} else {
1393c8a4ce0bSDick Kennedy 		lpfc_ncmd->ts_cmd_start = 0;
1394bd2cdd5eSJames Smart 	}
1395bd2cdd5eSJames Smart #endif
139601649561SJames Smart 
139701649561SJames Smart 	/*
139801649561SJames Smart 	 * Store the data needed by the driver to issue, abort, and complete
139901649561SJames Smart 	 * an IO.
140001649561SJames Smart 	 * Do not let the IO hang out forever.  There is no midlayer issuing
140101649561SJames Smart 	 * an abort so inform the FW of the maximum IO pending time.
140201649561SJames Smart 	 */
1403bbe3012bSJames Smart 	freqpriv->nvme_buf = lpfc_ncmd;
140401649561SJames Smart 	lpfc_ncmd->nvmeCmd = pnvme_fcreq;
140501649561SJames Smart 	lpfc_ncmd->nrport = rport;
1406318083adSJames Smart 	lpfc_ncmd->ndlp = ndlp;
140701649561SJames Smart 	lpfc_ncmd->start_time = jiffies;
140801649561SJames Smart 
140901649561SJames Smart 	lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp);
141001649561SJames Smart 	ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
141101649561SJames Smart 	if (ret) {
141201649561SJames Smart 		ret = -ENOMEM;
141301649561SJames Smart 		goto out_free_nvme_buf;
141401649561SJames Smart 	}
141501649561SJames Smart 
141601649561SJames Smart 	atomic_inc(&ndlp->cmd_pending);
141701649561SJames Smart 
141801649561SJames Smart 	/*
141901649561SJames Smart 	 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
142001649561SJames Smart 	 * This identfier was create in our hardware queue create callback
142101649561SJames Smart 	 * routine. The driver now is dependent on the IO queue steering from
142201649561SJames Smart 	 * the transport.  We are trusting the upper NVME layers know which
142301649561SJames Smart 	 * index to use and that they have affinitized a CPU to this hardware
142401649561SJames Smart 	 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
142501649561SJames Smart 	 */
142601649561SJames Smart 	lpfc_ncmd->cur_iocbq.hba_wqidx = lpfc_queue_info->index;
142701649561SJames Smart 
1428bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1429bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
1430bd2cdd5eSJames Smart 			 lpfc_queue_info->index, ndlp->nlp_DID);
1431bd2cdd5eSJames Smart 
143201649561SJames Smart 	ret = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, &lpfc_ncmd->cur_iocbq);
143301649561SJames Smart 	if (ret) {
143401649561SJames Smart 		atomic_dec(&ndlp->cmd_pending);
1435e3246a12SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
143601649561SJames Smart 				 "6113 FCP could not issue WQE err %x "
143701649561SJames Smart 				 "sid: x%x did: x%x oxid: x%x\n",
143801649561SJames Smart 				 ret, vport->fc_myDID, ndlp->nlp_DID,
143901649561SJames Smart 				 lpfc_ncmd->cur_iocbq.sli4_xritag);
144001649561SJames Smart 		goto out_free_nvme_buf;
144101649561SJames Smart 	}
144201649561SJames Smart 
1443bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1444c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start)
1445bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1446bd2cdd5eSJames Smart 
1447bd2cdd5eSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1448bd2cdd5eSJames Smart 		lpfc_ncmd->cpu = smp_processor_id();
1449bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu != lpfc_queue_info->index) {
1450bd2cdd5eSJames Smart 			/* Check for admin queue */
1451bd2cdd5eSJames Smart 			if (lpfc_queue_info->qidx) {
1452bd2cdd5eSJames Smart 				lpfc_printf_vlog(vport,
1453bd2cdd5eSJames Smart 						 KERN_ERR, LOG_NVME_IOERR,
1454bd2cdd5eSJames Smart 						"6702 CPU Check cmd: "
1455bd2cdd5eSJames Smart 						"cpu %d wq %d\n",
1456bd2cdd5eSJames Smart 						lpfc_ncmd->cpu,
1457bd2cdd5eSJames Smart 						lpfc_queue_info->index);
1458bd2cdd5eSJames Smart 			}
1459bd2cdd5eSJames Smart 			lpfc_ncmd->cpu = lpfc_queue_info->index;
1460bd2cdd5eSJames Smart 		}
1461bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1462bd2cdd5eSJames Smart 			phba->cpucheck_xmt_io[lpfc_ncmd->cpu]++;
1463bd2cdd5eSJames Smart 	}
1464bd2cdd5eSJames Smart #endif
146501649561SJames Smart 	return 0;
146601649561SJames Smart 
146701649561SJames Smart  out_free_nvme_buf:
14682cee7808SJames Smart 	if (lpfc_ncmd->nvmeCmd->sg_cnt) {
14692cee7808SJames Smart 		if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
14702cee7808SJames Smart 			atomic_dec(&phba->fc4NvmeOutputRequests);
14712cee7808SJames Smart 		else
14722cee7808SJames Smart 			atomic_dec(&phba->fc4NvmeInputRequests);
14732cee7808SJames Smart 	} else
14742cee7808SJames Smart 		atomic_dec(&phba->fc4NvmeControlRequests);
147501649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
147601649561SJames Smart  out_fail:
147701649561SJames Smart 	return ret;
147801649561SJames Smart }
147901649561SJames Smart 
148001649561SJames Smart /**
148101649561SJames Smart  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
148201649561SJames Smart  * @phba: Pointer to HBA context object
148301649561SJames Smart  * @cmdiocb: Pointer to command iocb object.
148401649561SJames Smart  * @rspiocb: Pointer to response iocb object.
148501649561SJames Smart  *
148601649561SJames Smart  * This is the callback function for any NVME FCP IO that was aborted.
148701649561SJames Smart  *
148801649561SJames Smart  * Return value:
148901649561SJames Smart  *   None
149001649561SJames Smart  **/
149101649561SJames Smart void
149201649561SJames Smart lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
149301649561SJames Smart 			   struct lpfc_wcqe_complete *abts_cmpl)
149401649561SJames Smart {
1495e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
149601649561SJames Smart 			"6145 ABORT_XRI_CN completing on rpi x%x "
149701649561SJames Smart 			"original iotag x%x, abort cmd iotag x%x "
149801649561SJames Smart 			"req_tag x%x, status x%x, hwstatus x%x\n",
149901649561SJames Smart 			cmdiocb->iocb.un.acxri.abortContextTag,
150001649561SJames Smart 			cmdiocb->iocb.un.acxri.abortIoTag,
150101649561SJames Smart 			cmdiocb->iotag,
150201649561SJames Smart 			bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
150301649561SJames Smart 			bf_get(lpfc_wcqe_c_status, abts_cmpl),
150401649561SJames Smart 			bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
150501649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocb);
150601649561SJames Smart }
150701649561SJames Smart 
150801649561SJames Smart /**
150901649561SJames Smart  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
151001649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
151101649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
151201649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
151301649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
151401649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
151501649561SJames Smart  *
151601649561SJames Smart  * Driver registers this routine as its nvme request io abort handler.  This
151701649561SJames Smart  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
151801649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
151901649561SJames Smart  * is executed asynchronously - one the target is validated as "MAPPED" and
152001649561SJames Smart  * ready for IO, the driver issues the abort request and returns.
152101649561SJames Smart  *
152201649561SJames Smart  * Return value:
152301649561SJames Smart  *   None
152401649561SJames Smart  **/
152501649561SJames Smart static void
152601649561SJames Smart lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
152701649561SJames Smart 		    struct nvme_fc_remote_port *pnvme_rport,
152801649561SJames Smart 		    void *hw_queue_handle,
152901649561SJames Smart 		    struct nvmefc_fcp_req *pnvme_fcreq)
153001649561SJames Smart {
153101649561SJames Smart 	struct lpfc_nvme_lport *lport;
153201649561SJames Smart 	struct lpfc_vport *vport;
153301649561SJames Smart 	struct lpfc_hba *phba;
153401649561SJames Smart 	struct lpfc_nvme_buf *lpfc_nbuf;
153501649561SJames Smart 	struct lpfc_iocbq *abts_buf;
153601649561SJames Smart 	struct lpfc_iocbq *nvmereq_wqe;
1537c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
153801649561SJames Smart 	union lpfc_wqe *abts_wqe;
153901649561SJames Smart 	unsigned long flags;
154001649561SJames Smart 	int ret_val;
154101649561SJames Smart 
1542c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1543c3725bdcSJames Smart 	 * have timing races.
1544c3725bdcSJames Smart 	 */
154501649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1546c3725bdcSJames Smart 	if (unlikely(!lport))
1547c3725bdcSJames Smart 		return;
154801649561SJames Smart 
1549c3725bdcSJames Smart 	vport = lport->vport;
1550c3725bdcSJames Smart 
1551c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
1552c3725bdcSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1553c3725bdcSJames Smart 				 "6129 Fail Abort, HW Queue Handle NULL.\n");
1554c3725bdcSJames Smart 		return;
1555c3725bdcSJames Smart 	}
1556c3725bdcSJames Smart 
1557c3725bdcSJames Smart 	phba = vport->phba;
1558c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1559c3725bdcSJames Smart 
1560c3725bdcSJames Smart 	if (unlikely(!freqpriv))
1561c3725bdcSJames Smart 		return;
15623386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
15633386f4bdSJames Smart 		return;
15643386f4bdSJames Smart 
156501649561SJames Smart 	/* Announce entry to new IO submit field. */
156686c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
156701649561SJames Smart 			 "6002 Abort Request to rport DID x%06x "
156801649561SJames Smart 			 "for nvme_fc_req %p\n",
156901649561SJames Smart 			 pnvme_rport->port_id,
157001649561SJames Smart 			 pnvme_fcreq);
157101649561SJames Smart 
157201649561SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
157301649561SJames Smart 	 * cleared when the reset is complete and rings reestablished.
157401649561SJames Smart 	 */
157501649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
157601649561SJames Smart 	/* driver queued commands are in process of being flushed */
157701649561SJames Smart 	if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
157801649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
157986c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
158001649561SJames Smart 				 "6139 Driver in reset cleanup - flushing "
158101649561SJames Smart 				 "NVME Req now.  hba_flag x%x\n",
158201649561SJames Smart 				 phba->hba_flag);
158301649561SJames Smart 		return;
158401649561SJames Smart 	}
158501649561SJames Smart 
1586bbe3012bSJames Smart 	lpfc_nbuf = freqpriv->nvme_buf;
158701649561SJames Smart 	if (!lpfc_nbuf) {
158801649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
158986c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
159001649561SJames Smart 				 "6140 NVME IO req has no matching lpfc nvme "
159101649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
159201649561SJames Smart 		return;
159301649561SJames Smart 	} else if (!lpfc_nbuf->nvmeCmd) {
159401649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
159586c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
159601649561SJames Smart 				 "6141 lpfc NVME IO req has no nvme_fcreq "
159701649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
159801649561SJames Smart 		return;
159901649561SJames Smart 	}
16002b7824d0SJames Smart 	nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
160101649561SJames Smart 
160201649561SJames Smart 	/*
160301649561SJames Smart 	 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
160401649561SJames Smart 	 * state must match the nvme_fcreq passed by the nvme
160501649561SJames Smart 	 * transport.  If they don't match, it is likely the driver
160601649561SJames Smart 	 * has already completed the NVME IO and the nvme transport
160701649561SJames Smart 	 * has not seen it yet.
160801649561SJames Smart 	 */
160901649561SJames Smart 	if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
161001649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
161186c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
161201649561SJames Smart 				 "6143 NVME req mismatch: "
161301649561SJames Smart 				 "lpfc_nbuf %p nvmeCmd %p, "
16142b7824d0SJames Smart 				 "pnvme_fcreq %p.  Skipping Abort xri x%x\n",
161501649561SJames Smart 				 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
16162b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
161701649561SJames Smart 		return;
161801649561SJames Smart 	}
161901649561SJames Smart 
162001649561SJames Smart 	/* Don't abort IOs no longer on the pending queue. */
162101649561SJames Smart 	if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
162201649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
162386c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
162401649561SJames Smart 				 "6142 NVME IO req %p not queued - skipping "
16252b7824d0SJames Smart 				 "abort req xri x%x\n",
16262b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
162701649561SJames Smart 		return;
162801649561SJames Smart 	}
162901649561SJames Smart 
1630bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1631bd2cdd5eSJames Smart 			 nvmereq_wqe->sli4_xritag,
163200cefeb9SJames Smart 			 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1633bd2cdd5eSJames Smart 
163401649561SJames Smart 	/* Outstanding abort is in progress */
163501649561SJames Smart 	if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
163601649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
163786c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
163801649561SJames Smart 				 "6144 Outstanding NVME I/O Abort Request "
163901649561SJames Smart 				 "still pending on nvme_fcreq %p, "
16402b7824d0SJames Smart 				 "lpfc_ncmd %p xri x%x\n",
16412b7824d0SJames Smart 				 pnvme_fcreq, lpfc_nbuf,
16422b7824d0SJames Smart 				 nvmereq_wqe->sli4_xritag);
164301649561SJames Smart 		return;
164401649561SJames Smart 	}
164501649561SJames Smart 
164601649561SJames Smart 	abts_buf = __lpfc_sli_get_iocbq(phba);
164701649561SJames Smart 	if (!abts_buf) {
164801649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
164986c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
165001649561SJames Smart 				 "6136 No available abort wqes. Skipping "
16512b7824d0SJames Smart 				 "Abts req for nvme_fcreq %p xri x%x\n",
16522b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
165301649561SJames Smart 		return;
165401649561SJames Smart 	}
165501649561SJames Smart 
165601649561SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
165701649561SJames Smart 	nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
165801649561SJames Smart 
165901649561SJames Smart 	/* Complete prepping the abort wqe and issue to the FW. */
166001649561SJames Smart 	abts_wqe = &abts_buf->wqe;
166101649561SJames Smart 
166201649561SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
166301649561SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
166401649561SJames Smart 	 */
166501649561SJames Smart 	memset(abts_wqe, 0, sizeof(union lpfc_wqe));
166601649561SJames Smart 	bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
166701649561SJames Smart 
166801649561SJames Smart 	/* word 7 */
166901649561SJames Smart 	bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
167001649561SJames Smart 	bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
167101649561SJames Smart 	bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
167201649561SJames Smart 	       nvmereq_wqe->iocb.ulpClass);
167301649561SJames Smart 
167401649561SJames Smart 	/* word 8 - tell the FW to abort the IO associated with this
167501649561SJames Smart 	 * outstanding exchange ID.
167601649561SJames Smart 	 */
167701649561SJames Smart 	abts_wqe->abort_cmd.wqe_com.abort_tag = nvmereq_wqe->sli4_xritag;
167801649561SJames Smart 
167901649561SJames Smart 	/* word 9 - this is the iotag for the abts_wqe completion. */
168001649561SJames Smart 	bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
168101649561SJames Smart 	       abts_buf->iotag);
168201649561SJames Smart 
168301649561SJames Smart 	/* word 10 */
168401649561SJames Smart 	bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, nvmereq_wqe->hba_wqidx);
168501649561SJames Smart 	bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
168601649561SJames Smart 	bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
168701649561SJames Smart 
168801649561SJames Smart 	/* word 11 */
168901649561SJames Smart 	bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
169001649561SJames Smart 	bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
169101649561SJames Smart 	bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
169201649561SJames Smart 
169301649561SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
169401649561SJames Smart 	abts_buf->iocb_flag |= LPFC_IO_NVME;
169501649561SJames Smart 	abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
169601649561SJames Smart 	abts_buf->vport = vport;
169701649561SJames Smart 	abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
169801649561SJames Smart 	ret_val = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_buf);
169901649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1700cd22d605SDick Kennedy 	if (ret_val) {
170186c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
170201649561SJames Smart 				 "6137 Failed abts issue_wqe with status x%x "
170301649561SJames Smart 				 "for nvme_fcreq %p.\n",
170401649561SJames Smart 				 ret_val, pnvme_fcreq);
170501649561SJames Smart 		lpfc_sli_release_iocbq(phba, abts_buf);
170601649561SJames Smart 		return;
170701649561SJames Smart 	}
170801649561SJames Smart 
170986c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
17102b7824d0SJames Smart 			 "6138 Transport Abort NVME Request Issued for "
171101649561SJames Smart 			 "ox_id x%x on reqtag x%x\n",
171201649561SJames Smart 			 nvmereq_wqe->sli4_xritag,
171301649561SJames Smart 			 abts_buf->iotag);
171401649561SJames Smart }
171501649561SJames Smart 
171601649561SJames Smart /* Declare and initialization an instance of the FC NVME template. */
171701649561SJames Smart static struct nvme_fc_port_template lpfc_nvme_template = {
171801649561SJames Smart 	/* initiator-based functions */
171901649561SJames Smart 	.localport_delete  = lpfc_nvme_localport_delete,
172001649561SJames Smart 	.remoteport_delete = lpfc_nvme_remoteport_delete,
172101649561SJames Smart 	.create_queue = lpfc_nvme_create_queue,
172201649561SJames Smart 	.delete_queue = lpfc_nvme_delete_queue,
172301649561SJames Smart 	.ls_req       = lpfc_nvme_ls_req,
172401649561SJames Smart 	.fcp_io       = lpfc_nvme_fcp_io_submit,
172501649561SJames Smart 	.ls_abort     = lpfc_nvme_ls_abort,
172601649561SJames Smart 	.fcp_abort    = lpfc_nvme_fcp_abort,
172701649561SJames Smart 
172801649561SJames Smart 	.max_hw_queues = 1,
172901649561SJames Smart 	.max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
173001649561SJames Smart 	.max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
173101649561SJames Smart 	.dma_boundary = 0xFFFFFFFF,
173201649561SJames Smart 
173301649561SJames Smart 	/* Sizes of additional private data for data structures.
173401649561SJames Smart 	 * No use for the last two sizes at this time.
173501649561SJames Smart 	 */
173601649561SJames Smart 	.local_priv_sz = sizeof(struct lpfc_nvme_lport),
173701649561SJames Smart 	.remote_priv_sz = sizeof(struct lpfc_nvme_rport),
173801649561SJames Smart 	.lsrqst_priv_sz = 0,
1739bbe3012bSJames Smart 	.fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
174001649561SJames Smart };
174101649561SJames Smart 
174201649561SJames Smart /**
174301649561SJames Smart  * lpfc_sli4_post_nvme_sgl_block - post a block of nvme sgl list to firmware
174401649561SJames Smart  * @phba: pointer to lpfc hba data structure.
174501649561SJames Smart  * @nblist: pointer to nvme buffer list.
174601649561SJames Smart  * @count: number of scsi buffers on the list.
174701649561SJames Smart  *
174801649561SJames Smart  * This routine is invoked to post a block of @count scsi sgl pages from a
174901649561SJames Smart  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
175001649561SJames Smart  * No Lock is held.
175101649561SJames Smart  *
175201649561SJames Smart  **/
175301649561SJames Smart static int
175401649561SJames Smart lpfc_sli4_post_nvme_sgl_block(struct lpfc_hba *phba,
175501649561SJames Smart 			      struct list_head *nblist,
175601649561SJames Smart 			      int count)
175701649561SJames Smart {
175801649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd;
175901649561SJames Smart 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
176001649561SJames Smart 	struct sgl_page_pairs *sgl_pg_pairs;
176101649561SJames Smart 	void *viraddr;
176201649561SJames Smart 	LPFC_MBOXQ_t *mbox;
176301649561SJames Smart 	uint32_t reqlen, alloclen, pg_pairs;
176401649561SJames Smart 	uint32_t mbox_tmo;
176501649561SJames Smart 	uint16_t xritag_start = 0;
176601649561SJames Smart 	int rc = 0;
176701649561SJames Smart 	uint32_t shdr_status, shdr_add_status;
176801649561SJames Smart 	dma_addr_t pdma_phys_bpl1;
176901649561SJames Smart 	union lpfc_sli4_cfg_shdr *shdr;
177001649561SJames Smart 
177101649561SJames Smart 	/* Calculate the requested length of the dma memory */
177201649561SJames Smart 	reqlen = count * sizeof(struct sgl_page_pairs) +
177301649561SJames Smart 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
177401649561SJames Smart 	if (reqlen > SLI4_PAGE_SIZE) {
177501649561SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
177601649561SJames Smart 				"6118 Block sgl registration required DMA "
177701649561SJames Smart 				"size (%d) great than a page\n", reqlen);
177801649561SJames Smart 		return -ENOMEM;
177901649561SJames Smart 	}
178001649561SJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
178101649561SJames Smart 	if (!mbox) {
178201649561SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
178301649561SJames Smart 				"6119 Failed to allocate mbox cmd memory\n");
178401649561SJames Smart 		return -ENOMEM;
178501649561SJames Smart 	}
178601649561SJames Smart 
178701649561SJames Smart 	/* Allocate DMA memory and set up the non-embedded mailbox command */
178801649561SJames Smart 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
178901649561SJames Smart 				LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
179001649561SJames Smart 				LPFC_SLI4_MBX_NEMBED);
179101649561SJames Smart 
179201649561SJames Smart 	if (alloclen < reqlen) {
179301649561SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
179401649561SJames Smart 				"6120 Allocated DMA memory size (%d) is "
179501649561SJames Smart 				"less than the requested DMA memory "
179601649561SJames Smart 				"size (%d)\n", alloclen, reqlen);
179701649561SJames Smart 		lpfc_sli4_mbox_cmd_free(phba, mbox);
179801649561SJames Smart 		return -ENOMEM;
179901649561SJames Smart 	}
180001649561SJames Smart 
180101649561SJames Smart 	/* Get the first SGE entry from the non-embedded DMA memory */
180201649561SJames Smart 	viraddr = mbox->sge_array->addr[0];
180301649561SJames Smart 
180401649561SJames Smart 	/* Set up the SGL pages in the non-embedded DMA pages */
180501649561SJames Smart 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
180601649561SJames Smart 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
180701649561SJames Smart 
180801649561SJames Smart 	pg_pairs = 0;
180901649561SJames Smart 	list_for_each_entry(lpfc_ncmd, nblist, list) {
181001649561SJames Smart 		/* Set up the sge entry */
181101649561SJames Smart 		sgl_pg_pairs->sgl_pg0_addr_lo =
181201649561SJames Smart 			cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
181301649561SJames Smart 		sgl_pg_pairs->sgl_pg0_addr_hi =
181401649561SJames Smart 			cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
181501649561SJames Smart 		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
181601649561SJames Smart 			pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
181701649561SJames Smart 						SGL_PAGE_SIZE;
181801649561SJames Smart 		else
181901649561SJames Smart 			pdma_phys_bpl1 = 0;
182001649561SJames Smart 		sgl_pg_pairs->sgl_pg1_addr_lo =
182101649561SJames Smart 			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
182201649561SJames Smart 		sgl_pg_pairs->sgl_pg1_addr_hi =
182301649561SJames Smart 			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
182401649561SJames Smart 		/* Keep the first xritag on the list */
182501649561SJames Smart 		if (pg_pairs == 0)
182601649561SJames Smart 			xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
182701649561SJames Smart 		sgl_pg_pairs++;
182801649561SJames Smart 		pg_pairs++;
182901649561SJames Smart 	}
183001649561SJames Smart 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
183101649561SJames Smart 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
183201649561SJames Smart 	/* Perform endian conversion if necessary */
183301649561SJames Smart 	sgl->word0 = cpu_to_le32(sgl->word0);
183401649561SJames Smart 
183501649561SJames Smart 	if (!phba->sli4_hba.intr_enable)
183601649561SJames Smart 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
183701649561SJames Smart 	else {
183801649561SJames Smart 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
183901649561SJames Smart 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
184001649561SJames Smart 	}
184101649561SJames Smart 	shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
184201649561SJames Smart 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
184301649561SJames Smart 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
184401649561SJames Smart 	if (rc != MBX_TIMEOUT)
184501649561SJames Smart 		lpfc_sli4_mbox_cmd_free(phba, mbox);
184601649561SJames Smart 	if (shdr_status || shdr_add_status || rc) {
184701649561SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
184801649561SJames Smart 				"6125 POST_SGL_BLOCK mailbox command failed "
184901649561SJames Smart 				"status x%x add_status x%x mbx status x%x\n",
185001649561SJames Smart 				shdr_status, shdr_add_status, rc);
185101649561SJames Smart 		rc = -ENXIO;
185201649561SJames Smart 	}
185301649561SJames Smart 	return rc;
185401649561SJames Smart }
185501649561SJames Smart 
185601649561SJames Smart /**
185701649561SJames Smart  * lpfc_post_nvme_sgl_list - Post blocks of nvme buffer sgls from a list
185801649561SJames Smart  * @phba: pointer to lpfc hba data structure.
185901649561SJames Smart  * @post_nblist: pointer to the nvme buffer list.
186001649561SJames Smart  *
186101649561SJames Smart  * This routine walks a list of nvme buffers that was passed in. It attempts
186201649561SJames Smart  * to construct blocks of nvme buffer sgls which contains contiguous xris and
186301649561SJames Smart  * uses the non-embedded SGL block post mailbox commands to post to the port.
186401649561SJames Smart  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
186501649561SJames Smart  * embedded SGL post mailbox command for posting. The @post_nblist passed in
186601649561SJames Smart  * must be local list, thus no lock is needed when manipulate the list.
186701649561SJames Smart  *
186801649561SJames Smart  * Returns: 0 = failure, non-zero number of successfully posted buffers.
186901649561SJames Smart  **/
187001649561SJames Smart static int
187101649561SJames Smart lpfc_post_nvme_sgl_list(struct lpfc_hba *phba,
187201649561SJames Smart 			     struct list_head *post_nblist, int sb_count)
187301649561SJames Smart {
187401649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
187501649561SJames Smart 	int status, sgl_size;
187601649561SJames Smart 	int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
187701649561SJames Smart 	dma_addr_t pdma_phys_sgl1;
187801649561SJames Smart 	int last_xritag = NO_XRI;
187901649561SJames Smart 	int cur_xritag;
188001649561SJames Smart 	LIST_HEAD(prep_nblist);
188101649561SJames Smart 	LIST_HEAD(blck_nblist);
188201649561SJames Smart 	LIST_HEAD(nvme_nblist);
188301649561SJames Smart 
188401649561SJames Smart 	/* sanity check */
188501649561SJames Smart 	if (sb_count <= 0)
188601649561SJames Smart 		return -EINVAL;
188701649561SJames Smart 
188801649561SJames Smart 	sgl_size = phba->cfg_sg_dma_buf_size;
188901649561SJames Smart 
189001649561SJames Smart 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
189101649561SJames Smart 		list_del_init(&lpfc_ncmd->list);
189201649561SJames Smart 		block_cnt++;
189301649561SJames Smart 		if ((last_xritag != NO_XRI) &&
189401649561SJames Smart 		    (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
189501649561SJames Smart 			/* a hole in xri block, form a sgl posting block */
189601649561SJames Smart 			list_splice_init(&prep_nblist, &blck_nblist);
189701649561SJames Smart 			post_cnt = block_cnt - 1;
189801649561SJames Smart 			/* prepare list for next posting block */
189901649561SJames Smart 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
190001649561SJames Smart 			block_cnt = 1;
190101649561SJames Smart 		} else {
190201649561SJames Smart 			/* prepare list for next posting block */
190301649561SJames Smart 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
190401649561SJames Smart 			/* enough sgls for non-embed sgl mbox command */
190501649561SJames Smart 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
190601649561SJames Smart 				list_splice_init(&prep_nblist, &blck_nblist);
190701649561SJames Smart 				post_cnt = block_cnt;
190801649561SJames Smart 				block_cnt = 0;
190901649561SJames Smart 			}
191001649561SJames Smart 		}
191101649561SJames Smart 		num_posting++;
191201649561SJames Smart 		last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
191301649561SJames Smart 
191401649561SJames Smart 		/* end of repost sgl list condition for NVME buffers */
191501649561SJames Smart 		if (num_posting == sb_count) {
191601649561SJames Smart 			if (post_cnt == 0) {
191701649561SJames Smart 				/* last sgl posting block */
191801649561SJames Smart 				list_splice_init(&prep_nblist, &blck_nblist);
191901649561SJames Smart 				post_cnt = block_cnt;
192001649561SJames Smart 			} else if (block_cnt == 1) {
192101649561SJames Smart 				/* last single sgl with non-contiguous xri */
192201649561SJames Smart 				if (sgl_size > SGL_PAGE_SIZE)
192301649561SJames Smart 					pdma_phys_sgl1 =
192401649561SJames Smart 						lpfc_ncmd->dma_phys_sgl +
192501649561SJames Smart 						SGL_PAGE_SIZE;
192601649561SJames Smart 				else
192701649561SJames Smart 					pdma_phys_sgl1 = 0;
192801649561SJames Smart 				cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
192901649561SJames Smart 				status = lpfc_sli4_post_sgl(phba,
193001649561SJames Smart 						lpfc_ncmd->dma_phys_sgl,
193101649561SJames Smart 						pdma_phys_sgl1, cur_xritag);
193201649561SJames Smart 				if (status) {
193301649561SJames Smart 					/* failure, put on abort nvme list */
1934318083adSJames Smart 					lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
193501649561SJames Smart 				} else {
193601649561SJames Smart 					/* success, put on NVME buffer list */
1937318083adSJames Smart 					lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
193801649561SJames Smart 					lpfc_ncmd->status = IOSTAT_SUCCESS;
193901649561SJames Smart 					num_posted++;
194001649561SJames Smart 				}
194101649561SJames Smart 				/* success, put on NVME buffer sgl list */
194201649561SJames Smart 				list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
194301649561SJames Smart 			}
194401649561SJames Smart 		}
194501649561SJames Smart 
194601649561SJames Smart 		/* continue until a nembed page worth of sgls */
194701649561SJames Smart 		if (post_cnt == 0)
194801649561SJames Smart 			continue;
194901649561SJames Smart 
195001649561SJames Smart 		/* post block of NVME buffer list sgls */
195101649561SJames Smart 		status = lpfc_sli4_post_nvme_sgl_block(phba, &blck_nblist,
195201649561SJames Smart 						       post_cnt);
195301649561SJames Smart 
195401649561SJames Smart 		/* don't reset xirtag due to hole in xri block */
195501649561SJames Smart 		if (block_cnt == 0)
195601649561SJames Smart 			last_xritag = NO_XRI;
195701649561SJames Smart 
195801649561SJames Smart 		/* reset NVME buffer post count for next round of posting */
195901649561SJames Smart 		post_cnt = 0;
196001649561SJames Smart 
196101649561SJames Smart 		/* put posted NVME buffer-sgl posted on NVME buffer sgl list */
196201649561SJames Smart 		while (!list_empty(&blck_nblist)) {
196301649561SJames Smart 			list_remove_head(&blck_nblist, lpfc_ncmd,
196401649561SJames Smart 					 struct lpfc_nvme_buf, list);
196501649561SJames Smart 			if (status) {
196601649561SJames Smart 				/* failure, put on abort nvme list */
1967318083adSJames Smart 				lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
196801649561SJames Smart 			} else {
196901649561SJames Smart 				/* success, put on NVME buffer list */
1970318083adSJames Smart 				lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
197101649561SJames Smart 				lpfc_ncmd->status = IOSTAT_SUCCESS;
197201649561SJames Smart 				num_posted++;
197301649561SJames Smart 			}
197401649561SJames Smart 			list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
197501649561SJames Smart 		}
197601649561SJames Smart 	}
197701649561SJames Smart 	/* Push NVME buffers with sgl posted to the available list */
197801649561SJames Smart 	while (!list_empty(&nvme_nblist)) {
197901649561SJames Smart 		list_remove_head(&nvme_nblist, lpfc_ncmd,
198001649561SJames Smart 				 struct lpfc_nvme_buf, list);
198101649561SJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
198201649561SJames Smart 	}
198301649561SJames Smart 	return num_posted;
198401649561SJames Smart }
198501649561SJames Smart 
198601649561SJames Smart /**
198701649561SJames Smart  * lpfc_repost_nvme_sgl_list - Repost all the allocated nvme buffer sgls
198801649561SJames Smart  * @phba: pointer to lpfc hba data structure.
198901649561SJames Smart  *
199001649561SJames Smart  * This routine walks the list of nvme buffers that have been allocated and
199101649561SJames Smart  * repost them to the port by using SGL block post. This is needed after a
199201649561SJames Smart  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
199301649561SJames Smart  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
199401649561SJames Smart  * to the lpfc_nvme_buf_list. If the repost fails, reject all nvme buffers.
199501649561SJames Smart  *
199601649561SJames Smart  * Returns: 0 = success, non-zero failure.
199701649561SJames Smart  **/
199801649561SJames Smart int
199901649561SJames Smart lpfc_repost_nvme_sgl_list(struct lpfc_hba *phba)
200001649561SJames Smart {
200101649561SJames Smart 	LIST_HEAD(post_nblist);
200201649561SJames Smart 	int num_posted, rc = 0;
200301649561SJames Smart 
200401649561SJames Smart 	/* get all NVME buffers need to repost to a local list */
200501649561SJames Smart 	spin_lock_irq(&phba->nvme_buf_list_get_lock);
200601649561SJames Smart 	spin_lock(&phba->nvme_buf_list_put_lock);
200701649561SJames Smart 	list_splice_init(&phba->lpfc_nvme_buf_list_get, &post_nblist);
200801649561SJames Smart 	list_splice(&phba->lpfc_nvme_buf_list_put, &post_nblist);
2009*cf1a1d3eSJames Smart 	phba->get_nvme_bufs = 0;
2010*cf1a1d3eSJames Smart 	phba->put_nvme_bufs = 0;
201101649561SJames Smart 	spin_unlock(&phba->nvme_buf_list_put_lock);
201201649561SJames Smart 	spin_unlock_irq(&phba->nvme_buf_list_get_lock);
201301649561SJames Smart 
201401649561SJames Smart 	/* post the list of nvme buffer sgls to port if available */
201501649561SJames Smart 	if (!list_empty(&post_nblist)) {
201601649561SJames Smart 		num_posted = lpfc_post_nvme_sgl_list(phba, &post_nblist,
201701649561SJames Smart 						phba->sli4_hba.nvme_xri_cnt);
201801649561SJames Smart 		/* failed to post any nvme buffer, return error */
201901649561SJames Smart 		if (num_posted == 0)
202001649561SJames Smart 			rc = -EIO;
202101649561SJames Smart 	}
202201649561SJames Smart 	return rc;
202301649561SJames Smart }
202401649561SJames Smart 
202501649561SJames Smart /**
202601649561SJames Smart  * lpfc_new_nvme_buf - Scsi buffer allocator for HBA with SLI4 IF spec
202701649561SJames Smart  * @vport: The virtual port for which this call being executed.
202801649561SJames Smart  * @num_to_allocate: The requested number of buffers to allocate.
202901649561SJames Smart  *
203001649561SJames Smart  * This routine allocates nvme buffers for device with SLI-4 interface spec,
203101649561SJames Smart  * the nvme buffer contains all the necessary information needed to initiate
203201649561SJames Smart  * a NVME I/O. After allocating up to @num_to_allocate NVME buffers and put
203301649561SJames Smart  * them on a list, it post them to the port by using SGL block post.
203401649561SJames Smart  *
203501649561SJames Smart  * Return codes:
203601649561SJames Smart  *   int - number of nvme buffers that were allocated and posted.
203701649561SJames Smart  *   0 = failure, less than num_to_alloc is a partial failure.
203801649561SJames Smart  **/
203901649561SJames Smart static int
204001649561SJames Smart lpfc_new_nvme_buf(struct lpfc_vport *vport, int num_to_alloc)
204101649561SJames Smart {
204201649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
204301649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd;
204401649561SJames Smart 	struct lpfc_iocbq *pwqeq;
204501649561SJames Smart 	union lpfc_wqe128 *wqe;
204601649561SJames Smart 	struct sli4_sge *sgl;
204701649561SJames Smart 	dma_addr_t pdma_phys_sgl;
204801649561SJames Smart 	uint16_t iotag, lxri = 0;
204901649561SJames Smart 	int bcnt, num_posted, sgl_size;
205001649561SJames Smart 	LIST_HEAD(prep_nblist);
205101649561SJames Smart 	LIST_HEAD(post_nblist);
205201649561SJames Smart 	LIST_HEAD(nvme_nblist);
205301649561SJames Smart 
205401649561SJames Smart 	sgl_size = phba->cfg_sg_dma_buf_size;
205501649561SJames Smart 
205601649561SJames Smart 	for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
205701649561SJames Smart 		lpfc_ncmd = kzalloc(sizeof(struct lpfc_nvme_buf), GFP_KERNEL);
205801649561SJames Smart 		if (!lpfc_ncmd)
205901649561SJames Smart 			break;
206001649561SJames Smart 		/*
206101649561SJames Smart 		 * Get memory from the pci pool to map the virt space to
206201649561SJames Smart 		 * pci bus space for an I/O. The DMA buffer includes the
206301649561SJames Smart 		 * number of SGE's necessary to support the sg_tablesize.
206401649561SJames Smart 		 */
2065df2f7729SThomas Meyer 		lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
206601649561SJames Smart 						  GFP_KERNEL,
206701649561SJames Smart 						  &lpfc_ncmd->dma_handle);
206801649561SJames Smart 		if (!lpfc_ncmd->data) {
206901649561SJames Smart 			kfree(lpfc_ncmd);
207001649561SJames Smart 			break;
207101649561SJames Smart 		}
207201649561SJames Smart 
207301649561SJames Smart 		lxri = lpfc_sli4_next_xritag(phba);
207401649561SJames Smart 		if (lxri == NO_XRI) {
2075771db5c0SRomain Perier 			dma_pool_free(phba->lpfc_sg_dma_buf_pool,
207601649561SJames Smart 				      lpfc_ncmd->data, lpfc_ncmd->dma_handle);
207701649561SJames Smart 			kfree(lpfc_ncmd);
207801649561SJames Smart 			break;
207901649561SJames Smart 		}
208001649561SJames Smart 		pwqeq = &(lpfc_ncmd->cur_iocbq);
208101649561SJames Smart 		wqe = (union lpfc_wqe128 *)&pwqeq->wqe;
208201649561SJames Smart 
208301649561SJames Smart 		/* Allocate iotag for lpfc_ncmd->cur_iocbq. */
208401649561SJames Smart 		iotag = lpfc_sli_next_iotag(phba, pwqeq);
208501649561SJames Smart 		if (iotag == 0) {
2086771db5c0SRomain Perier 			dma_pool_free(phba->lpfc_sg_dma_buf_pool,
208701649561SJames Smart 				      lpfc_ncmd->data, lpfc_ncmd->dma_handle);
208801649561SJames Smart 			kfree(lpfc_ncmd);
208901649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
209001649561SJames Smart 					"6121 Failed to allocated IOTAG for"
209101649561SJames Smart 					" XRI:0x%x\n", lxri);
209201649561SJames Smart 			lpfc_sli4_free_xri(phba, lxri);
209301649561SJames Smart 			break;
209401649561SJames Smart 		}
209501649561SJames Smart 		pwqeq->sli4_lxritag = lxri;
209601649561SJames Smart 		pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
209701649561SJames Smart 		pwqeq->iocb_flag |= LPFC_IO_NVME;
209801649561SJames Smart 		pwqeq->context1 = lpfc_ncmd;
209901649561SJames Smart 		pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
210001649561SJames Smart 
210101649561SJames Smart 		/* Initialize local short-hand pointers. */
210201649561SJames Smart 		lpfc_ncmd->nvme_sgl = lpfc_ncmd->data;
210301649561SJames Smart 		sgl = lpfc_ncmd->nvme_sgl;
210401649561SJames Smart 		pdma_phys_sgl = lpfc_ncmd->dma_handle;
210501649561SJames Smart 		lpfc_ncmd->dma_phys_sgl = pdma_phys_sgl;
210601649561SJames Smart 
210701649561SJames Smart 		/* Rsp SGE will be filled in when we rcv an IO
210801649561SJames Smart 		 * from the NVME Layer to be sent.
210901649561SJames Smart 		 * The cmd is going to be embedded so we need a SKIP SGE.
211001649561SJames Smart 		 */
211101649561SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
211201649561SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
211301649561SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
211401649561SJames Smart 		/* Fill in word 3 / sgl_len during cmd submission */
211501649561SJames Smart 
211601649561SJames Smart 		lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
211701649561SJames Smart 
211801649561SJames Smart 		/* Word 7 */
211901649561SJames Smart 		bf_set(wqe_erp, &wqe->generic.wqe_com, 0);
212001649561SJames Smart 		/* NVME upper layers will time things out, if needed */
212101649561SJames Smart 		bf_set(wqe_tmo, &wqe->generic.wqe_com, 0);
212201649561SJames Smart 
212301649561SJames Smart 		/* Word 10 */
212401649561SJames Smart 		bf_set(wqe_ebde_cnt, &wqe->generic.wqe_com, 0);
212501649561SJames Smart 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
212601649561SJames Smart 
212701649561SJames Smart 		/* add the nvme buffer to a post list */
212801649561SJames Smart 		list_add_tail(&lpfc_ncmd->list, &post_nblist);
212901649561SJames Smart 		spin_lock_irq(&phba->nvme_buf_list_get_lock);
213001649561SJames Smart 		phba->sli4_hba.nvme_xri_cnt++;
213101649561SJames Smart 		spin_unlock_irq(&phba->nvme_buf_list_get_lock);
213201649561SJames Smart 	}
213301649561SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
213401649561SJames Smart 			"6114 Allocate %d out of %d requested new NVME "
213501649561SJames Smart 			"buffers\n", bcnt, num_to_alloc);
213601649561SJames Smart 
213701649561SJames Smart 	/* post the list of nvme buffer sgls to port if available */
213801649561SJames Smart 	if (!list_empty(&post_nblist))
213901649561SJames Smart 		num_posted = lpfc_post_nvme_sgl_list(phba,
214001649561SJames Smart 						     &post_nblist, bcnt);
214101649561SJames Smart 	else
214201649561SJames Smart 		num_posted = 0;
214301649561SJames Smart 
214401649561SJames Smart 	return num_posted;
214501649561SJames Smart }
214601649561SJames Smart 
2147*cf1a1d3eSJames Smart static inline struct lpfc_nvme_buf *
2148*cf1a1d3eSJames Smart lpfc_nvme_buf(struct lpfc_hba *phba)
2149*cf1a1d3eSJames Smart {
2150*cf1a1d3eSJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
2151*cf1a1d3eSJames Smart 
2152*cf1a1d3eSJames Smart 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
2153*cf1a1d3eSJames Smart 				 &phba->lpfc_nvme_buf_list_get, list) {
2154*cf1a1d3eSJames Smart 		list_del_init(&lpfc_ncmd->list);
2155*cf1a1d3eSJames Smart 		phba->get_nvme_bufs--;
2156*cf1a1d3eSJames Smart 		return lpfc_ncmd;
2157*cf1a1d3eSJames Smart 	}
2158*cf1a1d3eSJames Smart 	return NULL;
2159*cf1a1d3eSJames Smart }
2160*cf1a1d3eSJames Smart 
216101649561SJames Smart /**
216201649561SJames Smart  * lpfc_get_nvme_buf - Get a nvme buffer from lpfc_nvme_buf_list of the HBA
216301649561SJames Smart  * @phba: The HBA for which this call is being executed.
216401649561SJames Smart  *
216501649561SJames Smart  * This routine removes a nvme buffer from head of @phba lpfc_nvme_buf_list list
216601649561SJames Smart  * and returns to caller.
216701649561SJames Smart  *
216801649561SJames Smart  * Return codes:
216901649561SJames Smart  *   NULL - Error
217001649561SJames Smart  *   Pointer to lpfc_nvme_buf - Success
217101649561SJames Smart  **/
217201649561SJames Smart static struct lpfc_nvme_buf *
2173*cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
2174*cf1a1d3eSJames Smart 		  int expedite)
217501649561SJames Smart {
2176*cf1a1d3eSJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd = NULL;
217701649561SJames Smart 	unsigned long iflag = 0;
217801649561SJames Smart 
217901649561SJames Smart 	spin_lock_irqsave(&phba->nvme_buf_list_get_lock, iflag);
2180*cf1a1d3eSJames Smart 	if (phba->get_nvme_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
2181*cf1a1d3eSJames Smart 		lpfc_ncmd = lpfc_nvme_buf(phba);
2182*cf1a1d3eSJames Smart 	if (!lpfc_ncmd) {
218301649561SJames Smart 		spin_lock(&phba->nvme_buf_list_put_lock);
218401649561SJames Smart 		list_splice(&phba->lpfc_nvme_buf_list_put,
218501649561SJames Smart 			    &phba->lpfc_nvme_buf_list_get);
2186*cf1a1d3eSJames Smart 		phba->get_nvme_bufs += phba->put_nvme_bufs;
218701649561SJames Smart 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
2188*cf1a1d3eSJames Smart 		phba->put_nvme_bufs = 0;
218901649561SJames Smart 		spin_unlock(&phba->nvme_buf_list_put_lock);
2190*cf1a1d3eSJames Smart 		if (phba->get_nvme_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
2191*cf1a1d3eSJames Smart 			lpfc_ncmd = lpfc_nvme_buf(phba);
219201649561SJames Smart 	}
219301649561SJames Smart 	spin_unlock_irqrestore(&phba->nvme_buf_list_get_lock, iflag);
219401649561SJames Smart 	return  lpfc_ncmd;
219501649561SJames Smart }
219601649561SJames Smart 
219701649561SJames Smart /**
219801649561SJames Smart  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
219901649561SJames Smart  * @phba: The Hba for which this call is being executed.
220001649561SJames Smart  * @lpfc_ncmd: The nvme buffer which is being released.
220101649561SJames Smart  *
220201649561SJames Smart  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
220301649561SJames Smart  * lpfc_nvme_buf_list list. For SLI4 XRI's are tied to the nvme buffer
220401649561SJames Smart  * and cannot be reused for at least RA_TOV amount of time if it was
220501649561SJames Smart  * aborted.
220601649561SJames Smart  **/
220701649561SJames Smart static void
220801649561SJames Smart lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_nvme_buf *lpfc_ncmd)
220901649561SJames Smart {
221001649561SJames Smart 	unsigned long iflag = 0;
221101649561SJames Smart 
221201649561SJames Smart 	lpfc_ncmd->nonsg_phys = 0;
2213318083adSJames Smart 	if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY) {
221486c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
221586c67379SJames Smart 				"6310 XB release deferred for "
221686c67379SJames Smart 				"ox_id x%x on reqtag x%x\n",
221786c67379SJames Smart 				lpfc_ncmd->cur_iocbq.sli4_xritag,
221886c67379SJames Smart 				lpfc_ncmd->cur_iocbq.iotag);
221986c67379SJames Smart 
222001649561SJames Smart 		spin_lock_irqsave(&phba->sli4_hba.abts_nvme_buf_list_lock,
222101649561SJames Smart 					iflag);
222201649561SJames Smart 		list_add_tail(&lpfc_ncmd->list,
222301649561SJames Smart 			&phba->sli4_hba.lpfc_abts_nvme_buf_list);
222401649561SJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock,
222501649561SJames Smart 					iflag);
222601649561SJames Smart 	} else {
222701649561SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
222801649561SJames Smart 		lpfc_ncmd->cur_iocbq.iocb_flag = LPFC_IO_NVME;
222901649561SJames Smart 		spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
223001649561SJames Smart 		list_add_tail(&lpfc_ncmd->list, &phba->lpfc_nvme_buf_list_put);
2231*cf1a1d3eSJames Smart 		phba->put_nvme_bufs++;
223201649561SJames Smart 		spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
223301649561SJames Smart 	}
223401649561SJames Smart }
223501649561SJames Smart 
223601649561SJames Smart /**
223701649561SJames Smart  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
223801649561SJames Smart  * @pvport - the lpfc_vport instance requesting a localport.
223901649561SJames Smart  *
224001649561SJames Smart  * This routine is invoked to create an nvme localport instance to bind
224101649561SJames Smart  * to the nvme_fc_transport.  It is called once during driver load
224201649561SJames Smart  * like lpfc_create_shost after all other services are initialized.
224301649561SJames Smart  * It requires a vport, vpi, and wwns at call time.  Other localport
224401649561SJames Smart  * parameters are modified as the driver's FCID and the Fabric WWN
224501649561SJames Smart  * are established.
224601649561SJames Smart  *
224701649561SJames Smart  * Return codes
224801649561SJames Smart  *      0 - successful
224901649561SJames Smart  *      -ENOMEM - no heap memory available
225001649561SJames Smart  *      other values - from nvme registration upcall
225101649561SJames Smart  **/
225201649561SJames Smart int
225301649561SJames Smart lpfc_nvme_create_localport(struct lpfc_vport *vport)
225401649561SJames Smart {
2255166d7211SJames Smart 	int ret = 0;
225601649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
225701649561SJames Smart 	struct nvme_fc_port_info nfcp_info;
225801649561SJames Smart 	struct nvme_fc_local_port *localport;
225901649561SJames Smart 	struct lpfc_nvme_lport *lport;
2260166d7211SJames Smart 	int len;
226101649561SJames Smart 
226201649561SJames Smart 	/* Initialize this localport instance.  The vport wwn usage ensures
226301649561SJames Smart 	 * that NPIV is accounted for.
226401649561SJames Smart 	 */
226501649561SJames Smart 	memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
226601649561SJames Smart 	nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
226701649561SJames Smart 	nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
226801649561SJames Smart 	nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
226901649561SJames Smart 
22704d4c4a4aSJames Smart 	/* Limit to LPFC_MAX_NVME_SEG_CNT.
22714d4c4a4aSJames Smart 	 * For now need + 1 to get around NVME transport logic.
22724d4c4a4aSJames Smart 	 */
22734d4c4a4aSJames Smart 	if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
22744d4c4a4aSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_INIT,
22754d4c4a4aSJames Smart 				 "6300 Reducing sg segment cnt to %d\n",
22764d4c4a4aSJames Smart 				 LPFC_MAX_NVME_SEG_CNT);
22774d4c4a4aSJames Smart 		phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
22784d4c4a4aSJames Smart 	} else {
22794d4c4a4aSJames Smart 		phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
22804d4c4a4aSJames Smart 	}
22814d4c4a4aSJames Smart 	lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
228201649561SJames Smart 	lpfc_nvme_template.max_hw_queues = phba->cfg_nvme_io_channel;
228301649561SJames Smart 
228401649561SJames Smart 	/* localport is allocated from the stack, but the registration
228501649561SJames Smart 	 * call allocates heap memory as well as the private area.
228601649561SJames Smart 	 */
22877d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
228801649561SJames Smart 	ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
228901649561SJames Smart 					 &vport->phba->pcidev->dev, &localport);
2290166d7211SJames Smart #else
2291166d7211SJames Smart 	ret = -ENOMEM;
2292166d7211SJames Smart #endif
229301649561SJames Smart 	if (!ret) {
229401649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
229501649561SJames Smart 				 "6005 Successfully registered local "
229601649561SJames Smart 				 "NVME port num %d, localP %p, private %p, "
229701649561SJames Smart 				 "sg_seg %d\n",
229801649561SJames Smart 				 localport->port_num, localport,
229901649561SJames Smart 				 localport->private,
230001649561SJames Smart 				 lpfc_nvme_template.max_sgl_segments);
230101649561SJames Smart 
230201649561SJames Smart 		/* Private is our lport size declared in the template. */
230301649561SJames Smart 		lport = (struct lpfc_nvme_lport *)localport->private;
230401649561SJames Smart 		vport->localport = localport;
230501649561SJames Smart 		lport->vport = vport;
230601649561SJames Smart 		vport->nvmei_support = 1;
23076b486ce9SDick Kennedy 
23086b486ce9SDick Kennedy 		/* Don't post more new bufs if repost already recovered
23096b486ce9SDick Kennedy 		 * the nvme sgls.
23106b486ce9SDick Kennedy 		 */
23116b486ce9SDick Kennedy 		if (phba->sli4_hba.nvme_xri_cnt == 0) {
23126b486ce9SDick Kennedy 			len  = lpfc_new_nvme_buf(vport,
23136b486ce9SDick Kennedy 						 phba->sli4_hba.nvme_xri_max);
231401649561SJames Smart 			vport->phba->total_nvme_bufs += len;
23153ebd9b47SJames Smart 		}
23166b486ce9SDick Kennedy 	}
23173ebd9b47SJames Smart 
231801649561SJames Smart 	return ret;
231901649561SJames Smart }
232001649561SJames Smart 
2321add9d6beSJames Smart /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2322add9d6beSJames Smart  *
2323add9d6beSJames Smart  * The driver has to wait for the host nvme transport to callback
2324add9d6beSJames Smart  * indicating the localport has successfully unregistered all
2325add9d6beSJames Smart  * resources.  Since this is an uninterruptible wait, loop every ten
2326add9d6beSJames Smart  * seconds and print a message indicating no progress.
2327add9d6beSJames Smart  *
2328add9d6beSJames Smart  * An uninterruptible wait is used because of the risk of transport-to-
2329add9d6beSJames Smart  * driver state mismatch.
2330add9d6beSJames Smart  */
2331add9d6beSJames Smart void
2332add9d6beSJames Smart lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
2333add9d6beSJames Smart 			   struct lpfc_nvme_lport *lport)
2334add9d6beSJames Smart {
2335add9d6beSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2336add9d6beSJames Smart 	u32 wait_tmo;
2337add9d6beSJames Smart 	int ret;
2338add9d6beSJames Smart 
2339add9d6beSJames Smart 	/* Host transport has to clean up and confirm requiring an indefinite
2340add9d6beSJames Smart 	 * wait. Print a message if a 10 second wait expires and renew the
2341add9d6beSJames Smart 	 * wait. This is unexpected.
2342add9d6beSJames Smart 	 */
2343add9d6beSJames Smart 	wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2344add9d6beSJames Smart 	while (true) {
2345add9d6beSJames Smart 		ret = wait_for_completion_timeout(&lport->lport_unreg_done,
2346add9d6beSJames Smart 						  wait_tmo);
2347add9d6beSJames Smart 		if (unlikely(!ret)) {
2348add9d6beSJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
2349add9d6beSJames Smart 					 "6176 Lport %p Localport %p wait "
2350add9d6beSJames Smart 					 "timed out. Renewing.\n",
2351add9d6beSJames Smart 					 lport, vport->localport);
2352add9d6beSJames Smart 			continue;
2353add9d6beSJames Smart 		}
2354add9d6beSJames Smart 		break;
2355add9d6beSJames Smart 	}
2356add9d6beSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
2357add9d6beSJames Smart 			 "6177 Lport %p Localport %p Complete Success\n",
2358add9d6beSJames Smart 			 lport, vport->localport);
2359add9d6beSJames Smart #endif
2360add9d6beSJames Smart }
2361add9d6beSJames Smart 
236201649561SJames Smart /**
236301649561SJames Smart  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
236401649561SJames Smart  * @pnvme: pointer to lpfc nvme data structure.
236501649561SJames Smart  *
236601649561SJames Smart  * This routine is invoked to destroy all lports bound to the phba.
236701649561SJames Smart  * The lport memory was allocated by the nvme fc transport and is
236801649561SJames Smart  * released there.  This routine ensures all rports bound to the
236901649561SJames Smart  * lport have been disconnected.
237001649561SJames Smart  *
237101649561SJames Smart  **/
237201649561SJames Smart void
237301649561SJames Smart lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
237401649561SJames Smart {
23757d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
237601649561SJames Smart 	struct nvme_fc_local_port *localport;
237701649561SJames Smart 	struct lpfc_nvme_lport *lport;
237801649561SJames Smart 	int ret;
237901649561SJames Smart 
238001649561SJames Smart 	if (vport->nvmei_support == 0)
238101649561SJames Smart 		return;
238201649561SJames Smart 
238301649561SJames Smart 	localport = vport->localport;
238401649561SJames Smart 	vport->localport = NULL;
238501649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
238601649561SJames Smart 
238701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
238801649561SJames Smart 			 "6011 Destroying NVME localport %p\n",
238901649561SJames Smart 			 localport);
2390166d7211SJames Smart 
239101649561SJames Smart 	/* lport's rport list is clear.  Unregister
239201649561SJames Smart 	 * lport and release resources.
239301649561SJames Smart 	 */
239401649561SJames Smart 	init_completion(&lport->lport_unreg_done);
239501649561SJames Smart 	ret = nvme_fc_unregister_localport(localport);
2396add9d6beSJames Smart 
2397add9d6beSJames Smart 	/* Wait for completion.  This either blocks
2398add9d6beSJames Smart 	 * indefinitely or succeeds
2399add9d6beSJames Smart 	 */
2400add9d6beSJames Smart 	lpfc_nvme_lport_unreg_wait(vport, lport);
240101649561SJames Smart 
240201649561SJames Smart 	/* Regardless of the unregister upcall response, clear
240301649561SJames Smart 	 * nvmei_support.  All rports are unregistered and the
240401649561SJames Smart 	 * driver will clean up.
240501649561SJames Smart 	 */
240601649561SJames Smart 	vport->nvmei_support = 0;
240701649561SJames Smart 	if (ret == 0) {
240801649561SJames Smart 		lpfc_printf_vlog(vport,
240901649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
241001649561SJames Smart 				 "6009 Unregistered lport Success\n");
241101649561SJames Smart 	} else {
241201649561SJames Smart 		lpfc_printf_vlog(vport,
241301649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
241401649561SJames Smart 				 "6010 Unregistered lport "
241501649561SJames Smart 				 "Failed, status x%x\n",
241601649561SJames Smart 				 ret);
241701649561SJames Smart 	}
2418166d7211SJames Smart #endif
241901649561SJames Smart }
242001649561SJames Smart 
242101649561SJames Smart void
242201649561SJames Smart lpfc_nvme_update_localport(struct lpfc_vport *vport)
242301649561SJames Smart {
24244410a67aSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
242501649561SJames Smart 	struct nvme_fc_local_port *localport;
242601649561SJames Smart 	struct lpfc_nvme_lport *lport;
242701649561SJames Smart 
242801649561SJames Smart 	localport = vport->localport;
24294410a67aSJames Smart 	if (!localport) {
24304410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
24314410a67aSJames Smart 				 "6710 Update NVME fail. No localport\n");
24324410a67aSJames Smart 		return;
24334410a67aSJames Smart 	}
243401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
24354410a67aSJames Smart 	if (!lport) {
24364410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
24374410a67aSJames Smart 				 "6171 Update NVME fail. localP %p, No lport\n",
24384410a67aSJames Smart 				 localport);
24394410a67aSJames Smart 		return;
24404410a67aSJames Smart 	}
244101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
244201649561SJames Smart 			 "6012 Update NVME lport %p did x%x\n",
244301649561SJames Smart 			 localport, vport->fc_myDID);
244401649561SJames Smart 
244501649561SJames Smart 	localport->port_id = vport->fc_myDID;
244601649561SJames Smart 	if (localport->port_id == 0)
244701649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
244801649561SJames Smart 	else
244901649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
245001649561SJames Smart 
245101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
245201649561SJames Smart 			 "6030 bound lport %p to DID x%06x\n",
245301649561SJames Smart 			 lport, localport->port_id);
24544410a67aSJames Smart #endif
245501649561SJames Smart }
245601649561SJames Smart 
245701649561SJames Smart int
245801649561SJames Smart lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
245901649561SJames Smart {
24607d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
246101649561SJames Smart 	int ret = 0;
246201649561SJames Smart 	struct nvme_fc_local_port *localport;
246301649561SJames Smart 	struct lpfc_nvme_lport *lport;
246401649561SJames Smart 	struct lpfc_nvme_rport *rport;
246501649561SJames Smart 	struct nvme_fc_remote_port *remote_port;
246601649561SJames Smart 	struct nvme_fc_port_info rpinfo;
24672b75d0f9SDick Kennedy 	struct lpfc_nodelist *prev_ndlp;
246801649561SJames Smart 
246901649561SJames Smart 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
247001649561SJames Smart 			 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
247101649561SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type);
247201649561SJames Smart 
247301649561SJames Smart 	localport = vport->localport;
2474bb6a8a2cSDick Kennedy 	if (!localport)
2475bb6a8a2cSDick Kennedy 		return 0;
2476bb6a8a2cSDick Kennedy 
247701649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
247801649561SJames Smart 
24797a06dcd3SJames Smart 	/* NVME rports are not preserved across devloss.
24807a06dcd3SJames Smart 	 * Just register this instance.  Note, rpinfo->dev_loss_tmo
24817a06dcd3SJames Smart 	 * is left 0 to indicate accept transport defaults.  The
24827a06dcd3SJames Smart 	 * driver communicates port role capabilities consistent
24837a06dcd3SJames Smart 	 * with the PRLI response data.
248401649561SJames Smart 	 */
24857a06dcd3SJames Smart 	memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
24867a06dcd3SJames Smart 	rpinfo.port_id = ndlp->nlp_DID;
248701649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET)
24887a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
248901649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
24907a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
249101649561SJames Smart 
24927a06dcd3SJames Smart 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
24937a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
24947a06dcd3SJames Smart 
24957a06dcd3SJames Smart 	rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
24967a06dcd3SJames Smart 	rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
24977a06dcd3SJames Smart 	ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
24987a06dcd3SJames Smart 	if (!ret) {
24997a06dcd3SJames Smart 		/* If the ndlp already has an nrport, this is just
25007a06dcd3SJames Smart 		 * a resume of the existing rport.  Else this is a
25017a06dcd3SJames Smart 		 * new rport.
25027a06dcd3SJames Smart 		 */
25037a06dcd3SJames Smart 		rport = remote_port->private;
25042b75d0f9SDick Kennedy 		if (ndlp->nrport) {
250501649561SJames Smart 			lpfc_printf_vlog(ndlp->vport, KERN_INFO,
250601649561SJames Smart 					 LOG_NVME_DISC,
250701649561SJames Smart 					 "6014 Rebinding lport to "
250801649561SJames Smart 					 "rport wwpn 0x%llx, "
250901649561SJames Smart 					 "Data: x%x x%x x%x x%06x\n",
251001649561SJames Smart 					 remote_port->port_name,
251101649561SJames Smart 					 remote_port->port_id,
251201649561SJames Smart 					 remote_port->port_role,
251301649561SJames Smart 					 ndlp->nlp_type,
251401649561SJames Smart 					 ndlp->nlp_DID);
25152b75d0f9SDick Kennedy 			prev_ndlp = rport->ndlp;
25162b75d0f9SDick Kennedy 
25172b75d0f9SDick Kennedy 			/* Sever the ndlp<->rport connection before dropping
25182b75d0f9SDick Kennedy 			 * the ndlp ref from register.
25192b75d0f9SDick Kennedy 			 */
25202b75d0f9SDick Kennedy 			ndlp->nrport = NULL;
25212b75d0f9SDick Kennedy 			rport->ndlp = NULL;
25222b75d0f9SDick Kennedy 			if (prev_ndlp)
25232b75d0f9SDick Kennedy 				lpfc_nlp_put(ndlp);
25242b75d0f9SDick Kennedy 		}
25252b75d0f9SDick Kennedy 
25262b75d0f9SDick Kennedy 		/* Clean bind the rport to the ndlp. */
252701649561SJames Smart 		rport->remoteport = remote_port;
252801649561SJames Smart 		rport->lport = lport;
252901649561SJames Smart 		rport->ndlp = lpfc_nlp_get(ndlp);
253001649561SJames Smart 		if (!rport->ndlp)
253101649561SJames Smart 			return -1;
253201649561SJames Smart 		ndlp->nrport = rport;
253301649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO,
253401649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
25357a06dcd3SJames Smart 				 "6022 Binding new rport to "
25367a06dcd3SJames Smart 				 "lport %p Rport WWNN 0x%llx, "
25377a06dcd3SJames Smart 				 "Rport WWPN 0x%llx DID "
25387a06dcd3SJames Smart 				 "x%06x Role x%x\n",
253901649561SJames Smart 				 lport,
254001649561SJames Smart 				 rpinfo.node_name, rpinfo.port_name,
254101649561SJames Smart 				 rpinfo.port_id, rpinfo.port_role);
254201649561SJames Smart 	} else {
254301649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR,
254401649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
254501649561SJames Smart 				 "6031 RemotePort Registration failed "
254601649561SJames Smart 				 "err: %d, DID x%06x\n",
254701649561SJames Smart 				 ret, ndlp->nlp_DID);
254801649561SJames Smart 	}
25497a06dcd3SJames Smart 
255001649561SJames Smart 	return ret;
2551166d7211SJames Smart #else
2552166d7211SJames Smart 	return 0;
2553166d7211SJames Smart #endif
255401649561SJames Smart }
255501649561SJames Smart 
2556add9d6beSJames Smart /* lpfc_nvme_rport_unreg_wait - Wait for the host to complete an rport unreg.
2557add9d6beSJames Smart  *
2558add9d6beSJames Smart  * The driver has to wait for the host nvme transport to callback
2559add9d6beSJames Smart  * indicating the remoteport has successfully unregistered all
2560add9d6beSJames Smart  * resources.  Since this is an uninterruptible wait, loop every ten
2561add9d6beSJames Smart  * seconds and print a message indicating no progress.
2562add9d6beSJames Smart  *
2563add9d6beSJames Smart  * An uninterruptible wait is used because of the risk of transport-to-
2564add9d6beSJames Smart  * driver state mismatch.
2565add9d6beSJames Smart  */
2566add9d6beSJames Smart void
2567add9d6beSJames Smart lpfc_nvme_rport_unreg_wait(struct lpfc_vport *vport,
2568add9d6beSJames Smart 			   struct lpfc_nvme_rport *rport)
2569add9d6beSJames Smart {
2570add9d6beSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2571add9d6beSJames Smart 	u32 wait_tmo;
2572add9d6beSJames Smart 	int ret;
2573add9d6beSJames Smart 
2574add9d6beSJames Smart 	/* Host transport has to clean up and confirm requiring an indefinite
2575add9d6beSJames Smart 	 * wait. Print a message if a 10 second wait expires and renew the
2576add9d6beSJames Smart 	 * wait. This is unexpected.
2577add9d6beSJames Smart 	 */
2578add9d6beSJames Smart 	wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2579add9d6beSJames Smart 	while (true) {
2580add9d6beSJames Smart 		ret = wait_for_completion_timeout(&rport->rport_unreg_done,
2581add9d6beSJames Smart 						  wait_tmo);
2582add9d6beSJames Smart 		if (unlikely(!ret)) {
2583add9d6beSJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
2584add9d6beSJames Smart 					 "6174 Rport %p Remoteport %p wait "
2585add9d6beSJames Smart 					 "timed out. Renewing.\n",
2586add9d6beSJames Smart 					 rport, rport->remoteport);
2587add9d6beSJames Smart 			continue;
2588add9d6beSJames Smart 		}
2589add9d6beSJames Smart 		break;
2590add9d6beSJames Smart 	}
2591add9d6beSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
2592add9d6beSJames Smart 			 "6175 Rport %p Remoteport %p Complete Success\n",
2593add9d6beSJames Smart 			 rport, rport->remoteport);
2594add9d6beSJames Smart #endif
2595add9d6beSJames Smart }
2596add9d6beSJames Smart 
259701649561SJames Smart /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
259801649561SJames Smart  *
259901649561SJames Smart  * There is no notion of Devloss or rport recovery from the current
260001649561SJames Smart  * nvme_transport perspective.  Loss of an rport just means IO cannot
260101649561SJames Smart  * be sent and recovery is completely up to the initator.
260201649561SJames Smart  * For now, the driver just unbinds the DID and port_role so that
260301649561SJames Smart  * no further IO can be issued.  Changes are planned for later.
260401649561SJames Smart  *
260501649561SJames Smart  * Notes - the ndlp reference count is not decremented here since
260601649561SJames Smart  * since there is no nvme_transport api for devloss.  Node ref count
260701649561SJames Smart  * is only adjusted in driver unload.
260801649561SJames Smart  */
260901649561SJames Smart void
261001649561SJames Smart lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
261101649561SJames Smart {
26127d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
261301649561SJames Smart 	int ret;
261401649561SJames Smart 	struct nvme_fc_local_port *localport;
261501649561SJames Smart 	struct lpfc_nvme_lport *lport;
261601649561SJames Smart 	struct lpfc_nvme_rport *rport;
261701649561SJames Smart 	struct nvme_fc_remote_port *remoteport;
261801649561SJames Smart 
261901649561SJames Smart 	localport = vport->localport;
262001649561SJames Smart 
262101649561SJames Smart 	/* This is fundamental error.  The localport is always
262201649561SJames Smart 	 * available until driver unload.  Just exit.
262301649561SJames Smart 	 */
262401649561SJames Smart 	if (!localport)
262501649561SJames Smart 		return;
262601649561SJames Smart 
262701649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
262801649561SJames Smart 	if (!lport)
262901649561SJames Smart 		goto input_err;
263001649561SJames Smart 
263101649561SJames Smart 	rport = ndlp->nrport;
263201649561SJames Smart 	if (!rport)
263301649561SJames Smart 		goto input_err;
263401649561SJames Smart 
263501649561SJames Smart 	remoteport = rport->remoteport;
263601649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
263701649561SJames Smart 			 "6033 Unreg nvme remoteport %p, portname x%llx, "
263801649561SJames Smart 			 "port_id x%06x, portstate x%x port type x%x\n",
263901649561SJames Smart 			 remoteport, remoteport->port_name,
264001649561SJames Smart 			 remoteport->port_id, remoteport->port_state,
264101649561SJames Smart 			 ndlp->nlp_type);
264201649561SJames Smart 
264301649561SJames Smart 	/* Sanity check ndlp type.  Only call for NVME ports. Don't
264401649561SJames Smart 	 * clear any rport state until the transport calls back.
264501649561SJames Smart 	 */
26463b5bde69SJames Smart 
26473b5bde69SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET) {
264801649561SJames Smart 		init_completion(&rport->rport_unreg_done);
26497a06dcd3SJames Smart 
26507a06dcd3SJames Smart 		/* No concern about the role change on the nvme remoteport.
26517a06dcd3SJames Smart 		 * The transport will update it.
26527a06dcd3SJames Smart 		 */
2653add9d6beSJames Smart 		ndlp->upcall_flags |= NLP_WAIT_FOR_UNREG;
265401649561SJames Smart 		ret = nvme_fc_unregister_remoteport(remoteport);
2655add9d6beSJames Smart 		if (ret != 0)
265601649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
265701649561SJames Smart 					 "6167 NVME unregister failed %d "
265801649561SJames Smart 					 "port_state x%x\n",
265901649561SJames Smart 					 ret, remoteport->port_state);
2660add9d6beSJames Smart 		else
2661add9d6beSJames Smart 			/* Wait for completion.  This either blocks
2662add9d6beSJames Smart 			 * indefinitely or succeeds
2663add9d6beSJames Smart 			 */
2664add9d6beSJames Smart 			lpfc_nvme_rport_unreg_wait(vport, rport);
2665add9d6beSJames Smart 		ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
266601649561SJames Smart 	}
266701649561SJames Smart 	return;
266801649561SJames Smart 
266901649561SJames Smart  input_err:
2670166d7211SJames Smart #endif
267101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
26722b7824d0SJames Smart 			 "6168 State error: lport %p, rport%p FCID x%06x\n",
267301649561SJames Smart 			 vport->localport, ndlp->rport, ndlp->nlp_DID);
267401649561SJames Smart }
2675318083adSJames Smart 
2676318083adSJames Smart /**
2677318083adSJames Smart  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2678318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
2679318083adSJames Smart  * @axri: pointer to the fcp xri abort wcqe structure.
2680318083adSJames Smart  *
2681318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
2682952c303bSDick Kennedy  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
2683952c303bSDick Kennedy  * here.
2684318083adSJames Smart  **/
2685318083adSJames Smart void
2686318083adSJames Smart lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2687318083adSJames Smart 			   struct sli4_wcqe_xri_aborted *axri)
2688318083adSJames Smart {
2689318083adSJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2690318083adSJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd, *next_lpfc_ncmd;
2691952c303bSDick Kennedy 	struct nvmefc_fcp_req *nvme_cmd = NULL;
2692318083adSJames Smart 	struct lpfc_nodelist *ndlp;
2693318083adSJames Smart 	unsigned long iflag = 0;
2694318083adSJames Smart 
2695318083adSJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
2696318083adSJames Smart 		return;
2697318083adSJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
2698318083adSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2699318083adSJames Smart 	list_for_each_entry_safe(lpfc_ncmd, next_lpfc_ncmd,
2700318083adSJames Smart 				 &phba->sli4_hba.lpfc_abts_nvme_buf_list,
2701318083adSJames Smart 				 list) {
2702318083adSJames Smart 		if (lpfc_ncmd->cur_iocbq.sli4_xritag == xri) {
2703bbe3012bSJames Smart 			list_del_init(&lpfc_ncmd->list);
2704318083adSJames Smart 			lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
2705318083adSJames Smart 			lpfc_ncmd->status = IOSTAT_SUCCESS;
2706318083adSJames Smart 			spin_unlock(
2707318083adSJames Smart 				&phba->sli4_hba.abts_nvme_buf_list_lock);
2708318083adSJames Smart 
2709318083adSJames Smart 			spin_unlock_irqrestore(&phba->hbalock, iflag);
2710318083adSJames Smart 			ndlp = lpfc_ncmd->ndlp;
2711952c303bSDick Kennedy 			if (ndlp)
2712318083adSJames Smart 				lpfc_sli4_abts_err_handler(phba, ndlp, axri);
271386c67379SJames Smart 
271486c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2715952c303bSDick Kennedy 					"6311 nvme_cmd %p xri x%x tag x%x "
2716952c303bSDick Kennedy 					"abort complete and xri released\n",
2717952c303bSDick Kennedy 					lpfc_ncmd->nvmeCmd, xri,
2718952c303bSDick Kennedy 					lpfc_ncmd->cur_iocbq.iotag);
271986c67379SJames Smart 
2720952c303bSDick Kennedy 			/* Aborted NVME commands are required to not complete
2721952c303bSDick Kennedy 			 * before the abort exchange command fully completes.
2722952c303bSDick Kennedy 			 * Once completed, it is available via the put list.
2723952c303bSDick Kennedy 			 */
2724952c303bSDick Kennedy 			nvme_cmd = lpfc_ncmd->nvmeCmd;
2725952c303bSDick Kennedy 			nvme_cmd->done(nvme_cmd);
2726318083adSJames Smart 			lpfc_release_nvme_buf(phba, lpfc_ncmd);
2727318083adSJames Smart 			return;
2728318083adSJames Smart 		}
2729318083adSJames Smart 	}
2730318083adSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2731318083adSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
273286c67379SJames Smart 
273386c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
273486c67379SJames Smart 			"6312 XRI Aborted xri x%x not found\n", xri);
273586c67379SJames Smart 
2736318083adSJames Smart }
2737c3725bdcSJames Smart 
2738c3725bdcSJames Smart /**
2739c3725bdcSJames Smart  * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2740c3725bdcSJames Smart  * @phba: Pointer to HBA context object.
2741c3725bdcSJames Smart  *
2742c3725bdcSJames Smart  * This function flushes all wqes in the nvme rings and frees all resources
2743c3725bdcSJames Smart  * in the txcmplq. This function does not issue abort wqes for the IO
2744c3725bdcSJames Smart  * commands in txcmplq, they will just be returned with
2745c3725bdcSJames Smart  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2746c3725bdcSJames Smart  * slot has been permanently disabled.
2747c3725bdcSJames Smart  **/
2748c3725bdcSJames Smart void
2749c3725bdcSJames Smart lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2750c3725bdcSJames Smart {
2751c3725bdcSJames Smart 	struct lpfc_sli_ring  *pring;
2752c3725bdcSJames Smart 	u32 i, wait_cnt = 0;
2753c3725bdcSJames Smart 
2754c3725bdcSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
2755c3725bdcSJames Smart 		return;
2756c3725bdcSJames Smart 
2757c3725bdcSJames Smart 	/* Cycle through all NVME rings and make sure all outstanding
2758c3725bdcSJames Smart 	 * WQEs have been removed from the txcmplqs.
2759c3725bdcSJames Smart 	 */
2760c3725bdcSJames Smart 	for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
2761c3725bdcSJames Smart 		pring = phba->sli4_hba.nvme_wq[i]->pring;
2762c3725bdcSJames Smart 
2763c3725bdcSJames Smart 		/* Retrieve everything on the txcmplq */
2764c3725bdcSJames Smart 		while (!list_empty(&pring->txcmplq)) {
2765c3725bdcSJames Smart 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2766c3725bdcSJames Smart 			wait_cnt++;
2767c3725bdcSJames Smart 
2768c3725bdcSJames Smart 			/* The sleep is 10mS.  Every ten seconds,
2769c3725bdcSJames Smart 			 * dump a message.  Something is wrong.
2770c3725bdcSJames Smart 			 */
2771c3725bdcSJames Smart 			if ((wait_cnt % 1000) == 0) {
2772c3725bdcSJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2773c3725bdcSJames Smart 						"6178 NVME IO not empty, "
2774c3725bdcSJames Smart 						"cnt %d\n", wait_cnt);
2775c3725bdcSJames Smart 			}
2776c3725bdcSJames Smart 		}
2777c3725bdcSJames Smart 	}
2778c3725bdcSJames Smart }
2779