xref: /linux/drivers/scsi/lpfc/lpfc_nvme.c (revision 00cefeb964f525672b5344446e5bb72188b03e38)
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 *
6001649561SJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp);
6101649561SJames Smart 
6201649561SJames Smart static void
6301649561SJames Smart lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_nvme_buf *);
6401649561SJames Smart 
6501649561SJames Smart 
6601649561SJames Smart /**
6701649561SJames Smart  * lpfc_nvme_create_queue -
6801649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
6901649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
7001649561SJames Smart  * @handle: An opaque driver handle used in follow-up calls.
7101649561SJames Smart  *
7201649561SJames Smart  * Driver registers this routine to preallocate and initialize any
7301649561SJames Smart  * internal data structures to bind the @qidx to its internal IO queues.
7401649561SJames Smart  * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
7501649561SJames Smart  *
7601649561SJames Smart  * Return value :
7701649561SJames Smart  *   0 - Success
7801649561SJames Smart  *   -EINVAL - Unsupported input value.
7901649561SJames Smart  *   -ENOMEM - Could not alloc necessary memory
8001649561SJames Smart  **/
8101649561SJames Smart static int
8201649561SJames Smart lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
8301649561SJames Smart 		       unsigned int qidx, u16 qsize,
8401649561SJames Smart 		       void **handle)
8501649561SJames Smart {
8601649561SJames Smart 	struct lpfc_nvme_lport *lport;
8701649561SJames Smart 	struct lpfc_vport *vport;
8801649561SJames Smart 	struct lpfc_nvme_qhandle *qhandle;
8901649561SJames Smart 	char *str;
9001649561SJames Smart 
9101649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
9201649561SJames Smart 	vport = lport->vport;
9301649561SJames Smart 	qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
9401649561SJames Smart 	if (qhandle == NULL)
9501649561SJames Smart 		return -ENOMEM;
9601649561SJames Smart 
9701649561SJames Smart 	qhandle->cpu_id = smp_processor_id();
9801649561SJames Smart 	qhandle->qidx = qidx;
9901649561SJames Smart 	/*
10001649561SJames Smart 	 * NVME qidx == 0 is the admin queue, so both admin queue
10101649561SJames Smart 	 * and first IO queue will use MSI-X vector and associated
10201649561SJames Smart 	 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
10301649561SJames Smart 	 */
10401649561SJames Smart 	if (qidx) {
10501649561SJames Smart 		str = "IO ";  /* IO queue */
10601649561SJames Smart 		qhandle->index = ((qidx - 1) %
10701649561SJames Smart 			vport->phba->cfg_nvme_io_channel);
10801649561SJames Smart 	} else {
10901649561SJames Smart 		str = "ADM";  /* Admin queue */
11001649561SJames Smart 		qhandle->index = qidx;
11101649561SJames Smart 	}
11201649561SJames Smart 
11301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME,
11401649561SJames Smart 			 "6073 Binding %s HdwQueue %d  (cpu %d) to "
11501649561SJames Smart 			 "io_channel %d qhandle %p\n", str,
11601649561SJames Smart 			 qidx, qhandle->cpu_id, qhandle->index, qhandle);
11701649561SJames Smart 	*handle = (void *)qhandle;
11801649561SJames Smart 	return 0;
11901649561SJames Smart }
12001649561SJames Smart 
12101649561SJames Smart /**
12201649561SJames Smart  * lpfc_nvme_delete_queue -
12301649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
12401649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
12501649561SJames Smart  * @handle: An opaque driver handle from lpfc_nvme_create_queue
12601649561SJames Smart  *
12701649561SJames Smart  * Driver registers this routine to free
12801649561SJames Smart  * any internal data structures to bind the @qidx to its internal
12901649561SJames Smart  * IO queues.
13001649561SJames Smart  *
13101649561SJames Smart  * Return value :
13201649561SJames Smart  *   0 - Success
13301649561SJames Smart  *   TODO:  What are the failure codes.
13401649561SJames Smart  **/
13501649561SJames Smart static void
13601649561SJames Smart lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
13701649561SJames Smart 		       unsigned int qidx,
13801649561SJames Smart 		       void *handle)
13901649561SJames Smart {
14001649561SJames Smart 	struct lpfc_nvme_lport *lport;
14101649561SJames Smart 	struct lpfc_vport *vport;
14201649561SJames Smart 
14301649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
14401649561SJames Smart 	vport = lport->vport;
14501649561SJames Smart 
14601649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
14701649561SJames Smart 			"6001 ENTER.  lpfc_pnvme %p, qidx x%xi qhandle %p\n",
14801649561SJames Smart 			lport, qidx, handle);
14901649561SJames Smart 	kfree(handle);
15001649561SJames Smart }
15101649561SJames Smart 
15201649561SJames Smart static void
15301649561SJames Smart lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
15401649561SJames Smart {
15501649561SJames Smart 	struct lpfc_nvme_lport *lport = localport->private;
15601649561SJames Smart 
15701649561SJames Smart 	/* release any threads waiting for the unreg to complete */
15801649561SJames Smart 	complete(&lport->lport_unreg_done);
15901649561SJames Smart }
16001649561SJames Smart 
16101649561SJames Smart /* lpfc_nvme_remoteport_delete
16201649561SJames Smart  *
16301649561SJames Smart  * @remoteport: Pointer to an nvme transport remoteport instance.
16401649561SJames Smart  *
16501649561SJames Smart  * This is a template downcall.  NVME transport calls this function
16601649561SJames Smart  * when it has completed the unregistration of a previously
16701649561SJames Smart  * registered remoteport.
16801649561SJames Smart  *
16901649561SJames Smart  * Return value :
17001649561SJames Smart  * None
17101649561SJames Smart  */
17201649561SJames Smart void
17301649561SJames Smart lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
17401649561SJames Smart {
17501649561SJames Smart 	struct lpfc_nvme_rport *rport = remoteport->private;
17601649561SJames Smart 	struct lpfc_vport *vport;
17701649561SJames Smart 	struct lpfc_nodelist *ndlp;
17801649561SJames Smart 
17901649561SJames Smart 	ndlp = rport->ndlp;
18001649561SJames Smart 	if (!ndlp)
18101649561SJames Smart 		goto rport_err;
18201649561SJames Smart 
18301649561SJames Smart 	vport = ndlp->vport;
18401649561SJames Smart 	if (!vport)
18501649561SJames Smart 		goto rport_err;
18601649561SJames Smart 
18701649561SJames Smart 	/* Remove this rport from the lport's list - memory is owned by the
18801649561SJames Smart 	 * transport. Remove the ndlp reference for the NVME transport before
1897a06dcd3SJames Smart 	 * calling state machine to remove the node.
19001649561SJames Smart 	 */
19101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
19201649561SJames Smart 			"6146 remoteport delete complete %p\n",
19301649561SJames Smart 			remoteport);
1947a06dcd3SJames Smart 	ndlp->nrport = NULL;
19501649561SJames Smart 	lpfc_nlp_put(ndlp);
19601649561SJames Smart 
19701649561SJames Smart  rport_err:
19801649561SJames Smart 	/* This call has to execute as long as the rport is valid.
19901649561SJames Smart 	 * Release any threads waiting for the unreg to complete.
20001649561SJames Smart 	 */
20101649561SJames Smart 	complete(&rport->rport_unreg_done);
20201649561SJames Smart }
20301649561SJames Smart 
20401649561SJames Smart static void
20501649561SJames Smart lpfc_nvme_cmpl_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
20601649561SJames Smart 		       struct lpfc_wcqe_complete *wcqe)
20701649561SJames Smart {
20801649561SJames Smart 	struct lpfc_vport *vport = cmdwqe->vport;
20901649561SJames Smart 	uint32_t status;
21001649561SJames Smart 	struct nvmefc_ls_req *pnvme_lsreq;
21101649561SJames Smart 	struct lpfc_dmabuf *buf_ptr;
21201649561SJames Smart 	struct lpfc_nodelist *ndlp;
21301649561SJames Smart 
2142cee7808SJames Smart 	atomic_inc(&vport->phba->fc4NvmeLsCmpls);
21501649561SJames Smart 
21601649561SJames Smart 	pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
21701649561SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
21801649561SJames Smart 	ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
21901649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
22001649561SJames Smart 			 "6047 nvme cmpl Enter "
22101649561SJames Smart 			 "Data %p DID %x Xri: %x status %x cmd:%p lsreg:%p "
22201649561SJames Smart 			 "bmp:%p ndlp:%p\n",
22301649561SJames Smart 			 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
22401649561SJames Smart 			 cmdwqe->sli4_xritag, status,
22501649561SJames Smart 			 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
22601649561SJames Smart 
227bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  CMPL: xri x%x stat x%x parm x%x\n",
228bd2cdd5eSJames Smart 			 cmdwqe->sli4_xritag, status, wcqe->parameter);
229bd2cdd5eSJames Smart 
23001649561SJames Smart 	if (cmdwqe->context3) {
23101649561SJames Smart 		buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
23201649561SJames Smart 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
23301649561SJames Smart 		kfree(buf_ptr);
23401649561SJames Smart 		cmdwqe->context3 = NULL;
23501649561SJames Smart 	}
23601649561SJames Smart 	if (pnvme_lsreq->done)
23701649561SJames Smart 		pnvme_lsreq->done(pnvme_lsreq, status);
23801649561SJames Smart 	else
23901649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
24001649561SJames Smart 				 "6046 nvme cmpl without done call back? "
24101649561SJames Smart 				 "Data %p DID %x Xri: %x status %x\n",
24201649561SJames Smart 				pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
24301649561SJames Smart 				cmdwqe->sli4_xritag, status);
24401649561SJames Smart 	if (ndlp) {
24501649561SJames Smart 		lpfc_nlp_put(ndlp);
24601649561SJames Smart 		cmdwqe->context1 = NULL;
24701649561SJames Smart 	}
24801649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
24901649561SJames Smart }
25001649561SJames Smart 
25101649561SJames Smart static int
25201649561SJames Smart lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
25301649561SJames Smart 		  struct lpfc_dmabuf *inp,
25401649561SJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq,
25501649561SJames Smart 	     void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
25601649561SJames Smart 			   struct lpfc_wcqe_complete *),
25701649561SJames Smart 	     struct lpfc_nodelist *ndlp, uint32_t num_entry,
25801649561SJames Smart 	     uint32_t tmo, uint8_t retry)
25901649561SJames Smart {
26001649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
26101649561SJames Smart 	union lpfc_wqe *wqe;
26201649561SJames Smart 	struct lpfc_iocbq *genwqe;
26301649561SJames Smart 	struct ulp_bde64 *bpl;
26401649561SJames Smart 	struct ulp_bde64 bde;
26501649561SJames Smart 	int i, rc, xmit_len, first_len;
26601649561SJames Smart 
26701649561SJames Smart 	/* Allocate buffer for  command WQE */
26801649561SJames Smart 	genwqe = lpfc_sli_get_iocbq(phba);
26901649561SJames Smart 	if (genwqe == NULL)
27001649561SJames Smart 		return 1;
27101649561SJames Smart 
27201649561SJames Smart 	wqe = &genwqe->wqe;
27301649561SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
27401649561SJames Smart 
27501649561SJames Smart 	genwqe->context3 = (uint8_t *)bmp;
27601649561SJames Smart 	genwqe->iocb_flag |= LPFC_IO_NVME_LS;
27701649561SJames Smart 
27801649561SJames Smart 	/* Save for completion so we can release these resources */
27901649561SJames Smart 	genwqe->context1 = lpfc_nlp_get(ndlp);
28001649561SJames Smart 	genwqe->context2 = (uint8_t *)pnvme_lsreq;
28101649561SJames Smart 	/* Fill in payload, bp points to frame payload */
28201649561SJames Smart 
28301649561SJames Smart 	if (!tmo)
28401649561SJames Smart 		/* FC spec states we need 3 * ratov for CT requests */
28501649561SJames Smart 		tmo = (3 * phba->fc_ratov);
28601649561SJames Smart 
28701649561SJames Smart 	/* For this command calculate the xmit length of the request bde. */
28801649561SJames Smart 	xmit_len = 0;
28901649561SJames Smart 	first_len = 0;
29001649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
29101649561SJames Smart 	for (i = 0; i < num_entry; i++) {
29201649561SJames Smart 		bde.tus.w = bpl[i].tus.w;
29301649561SJames Smart 		if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
29401649561SJames Smart 			break;
29501649561SJames Smart 		xmit_len += bde.tus.f.bdeSize;
29601649561SJames Smart 		if (i == 0)
29701649561SJames Smart 			first_len = xmit_len;
29801649561SJames Smart 	}
29901649561SJames Smart 
30001649561SJames Smart 	genwqe->rsvd2 = num_entry;
30101649561SJames Smart 	genwqe->hba_wqidx = 0;
30201649561SJames Smart 
30301649561SJames Smart 	/* Words 0 - 2 */
30401649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
30501649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = first_len;
30601649561SJames Smart 	wqe->generic.bde.addrLow = bpl[0].addrLow;
30701649561SJames Smart 	wqe->generic.bde.addrHigh = bpl[0].addrHigh;
30801649561SJames Smart 
30901649561SJames Smart 	/* Word 3 */
31001649561SJames Smart 	wqe->gen_req.request_payload_len = first_len;
31101649561SJames Smart 
31201649561SJames Smart 	/* Word 4 */
31301649561SJames Smart 
31401649561SJames Smart 	/* Word 5 */
31501649561SJames Smart 	bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
31601649561SJames Smart 	bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
31701649561SJames Smart 	bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
3188b361639SJames Smart 	bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
31901649561SJames Smart 	bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
32001649561SJames Smart 
32101649561SJames Smart 	/* Word 6 */
32201649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
32301649561SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
32401649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
32501649561SJames Smart 
32601649561SJames Smart 	/* Word 7 */
32701649561SJames Smart 	bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
32801649561SJames Smart 	bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
32901649561SJames Smart 	bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
33001649561SJames Smart 	bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
33101649561SJames Smart 
33201649561SJames Smart 	/* Word 8 */
33301649561SJames Smart 	wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
33401649561SJames Smart 
33501649561SJames Smart 	/* Word 9 */
33601649561SJames Smart 	bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
33701649561SJames Smart 
33801649561SJames Smart 	/* Word 10 */
33901649561SJames Smart 	bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
34001649561SJames Smart 	bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
34101649561SJames Smart 	bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
34201649561SJames Smart 	bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
34301649561SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
34401649561SJames Smart 
34501649561SJames Smart 	/* Word 11 */
34601649561SJames Smart 	bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
34701649561SJames Smart 	bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
34801649561SJames Smart 
34901649561SJames Smart 
35001649561SJames Smart 	/* Issue GEN REQ WQE for NPORT <did> */
35101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
35201649561SJames Smart 			 "6050 Issue GEN REQ WQE to NPORT x%x "
35301649561SJames Smart 			 "Data: x%x x%x wq:%p lsreq:%p bmp:%p xmit:%d 1st:%d\n",
35401649561SJames Smart 			 ndlp->nlp_DID, genwqe->iotag,
35501649561SJames Smart 			 vport->port_state,
35601649561SJames Smart 			genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
35701649561SJames Smart 	genwqe->wqe_cmpl = cmpl;
35801649561SJames Smart 	genwqe->iocb_cmpl = NULL;
35901649561SJames Smart 	genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
36001649561SJames Smart 	genwqe->vport = vport;
36101649561SJames Smart 	genwqe->retry = retry;
36201649561SJames Smart 
363bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
364bd2cdd5eSJames Smart 			 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
365bd2cdd5eSJames Smart 
36601649561SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, genwqe);
36701649561SJames Smart 	if (rc == WQE_ERROR) {
36801649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
36901649561SJames Smart 				 "6045 Issue GEN REQ WQE to NPORT x%x "
37001649561SJames Smart 				 "Data: x%x x%x\n",
37101649561SJames Smart 				 ndlp->nlp_DID, genwqe->iotag,
37201649561SJames Smart 				 vport->port_state);
37301649561SJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
37401649561SJames Smart 		return 1;
37501649561SJames Smart 	}
37601649561SJames Smart 	return 0;
37701649561SJames Smart }
37801649561SJames Smart 
37901649561SJames Smart /**
38001649561SJames Smart  * lpfc_nvme_ls_req - Issue an Link Service request
38101649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
38201649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
38301649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
38401649561SJames Smart  *
38501649561SJames Smart  * Driver registers this routine to handle any link service request
38601649561SJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
38701649561SJames Smart  *
38801649561SJames Smart  * Return value :
38901649561SJames Smart  *   0 - Success
39001649561SJames Smart  *   TODO: What are the failure codes.
39101649561SJames Smart  **/
39201649561SJames Smart static int
39301649561SJames Smart lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
39401649561SJames Smart 		 struct nvme_fc_remote_port *pnvme_rport,
39501649561SJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq)
39601649561SJames Smart {
39701649561SJames Smart 	int ret = 0;
39801649561SJames Smart 	struct lpfc_nvme_lport *lport;
39901649561SJames Smart 	struct lpfc_vport *vport;
40001649561SJames Smart 	struct lpfc_nodelist *ndlp;
40101649561SJames Smart 	struct ulp_bde64 *bpl;
40201649561SJames Smart 	struct lpfc_dmabuf *bmp;
403ba43c4d0SJames Smart 	uint16_t ntype, nstate;
40401649561SJames Smart 
40501649561SJames Smart 	/* there are two dma buf in the request, actually there is one and
40601649561SJames Smart 	 * the second one is just the start address + cmd size.
40701649561SJames Smart 	 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
40801649561SJames Smart 	 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
40901649561SJames Smart 	 * because the nvem layer owns the data bufs.
41001649561SJames Smart 	 * We do not have to break these packets open, we don't care what is in
41101649561SJames Smart 	 * them. And we do not have to look at the resonse data, we only care
41201649561SJames Smart 	 * that we got a response. All of the caring is going to happen in the
41301649561SJames Smart 	 * nvme-fc layer.
41401649561SJames Smart 	 */
41501649561SJames Smart 
41601649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
41701649561SJames Smart 	vport = lport->vport;
41801649561SJames Smart 
41901649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
420ba43c4d0SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
421ba43c4d0SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
422ba43c4d0SJames Smart 				 "6051 DID x%06x not an active rport.\n",
42301649561SJames Smart 				 pnvme_rport->port_id);
424ba43c4d0SJames Smart 		return -ENODEV;
425ba43c4d0SJames Smart 	}
426ba43c4d0SJames Smart 
427ba43c4d0SJames Smart 	/* The remote node has to be a mapped nvme target or an
428ba43c4d0SJames Smart 	 * unmapped nvme initiator or it's an error.
429ba43c4d0SJames Smart 	 */
430ba43c4d0SJames Smart 	ntype = ndlp->nlp_type;
431ba43c4d0SJames Smart 	nstate = ndlp->nlp_state;
432ba43c4d0SJames Smart 	if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
433ba43c4d0SJames Smart 	    (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
434ba43c4d0SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
435ba43c4d0SJames Smart 				 "6088 DID x%06x not ready for "
436ba43c4d0SJames Smart 				 "IO. State x%x, Type x%x\n",
437ba43c4d0SJames Smart 				 pnvme_rport->port_id,
438ba43c4d0SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type);
439ba43c4d0SJames Smart 		return -ENODEV;
44001649561SJames Smart 	}
44101649561SJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
44201649561SJames Smart 	if (!bmp) {
44301649561SJames Smart 
44401649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
44501649561SJames Smart 				 "6044 Could not find node for DID %x\n",
44601649561SJames Smart 				 pnvme_rport->port_id);
44701649561SJames Smart 		return 2;
44801649561SJames Smart 	}
44901649561SJames Smart 	INIT_LIST_HEAD(&bmp->list);
45001649561SJames Smart 	bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
45101649561SJames Smart 	if (!bmp->virt) {
45201649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
45301649561SJames Smart 				 "6042 Could not find node for DID %x\n",
45401649561SJames Smart 				 pnvme_rport->port_id);
45501649561SJames Smart 		kfree(bmp);
45601649561SJames Smart 		return 3;
45701649561SJames Smart 	}
45801649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
45901649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
46001649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
46101649561SJames Smart 	bpl->tus.f.bdeFlags = 0;
46201649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
46301649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
46401649561SJames Smart 	bpl++;
46501649561SJames Smart 
46601649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
46701649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
46801649561SJames Smart 	bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
46901649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
47001649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
47101649561SJames Smart 
47201649561SJames Smart 	/* Expand print to include key fields. */
47301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
474ba43c4d0SJames Smart 			 "6149 ENTER.  lport %p, rport %p lsreq%p rqstlen:%d "
475825c6abbSArnd Bergmann 			 "rsplen:%d %pad %pad\n",
47601649561SJames Smart 			 pnvme_lport, pnvme_rport,
47701649561SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
478825c6abbSArnd Bergmann 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
479825c6abbSArnd Bergmann 			 &pnvme_lsreq->rspdma);
48001649561SJames Smart 
4812cee7808SJames Smart 	atomic_inc(&vport->phba->fc4NvmeLsRequests);
48201649561SJames Smart 
48301649561SJames Smart 	/* Hardcode the wait to 30 seconds.  Connections are failing otherwise.
48401649561SJames Smart 	 * This code allows it all to work.
48501649561SJames Smart 	 */
48601649561SJames Smart 	ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
48701649561SJames Smart 				pnvme_lsreq, lpfc_nvme_cmpl_gen_req,
48801649561SJames Smart 				ndlp, 2, 30, 0);
48901649561SJames Smart 	if (ret != WQE_SUCCESS) {
49001649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
49101649561SJames Smart 				 "6052 EXIT. issue ls wqe failed lport %p, "
49201649561SJames Smart 				 "rport %p lsreq%p Status %x DID %x\n",
49301649561SJames Smart 				 pnvme_lport, pnvme_rport, pnvme_lsreq,
49401649561SJames Smart 				 ret, ndlp->nlp_DID);
49501649561SJames Smart 		lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
49601649561SJames Smart 		kfree(bmp);
49701649561SJames Smart 		return ret;
49801649561SJames Smart 	}
49901649561SJames Smart 
50001649561SJames Smart 	/* Stub in routine and return 0 for now. */
50101649561SJames Smart 	return ret;
50201649561SJames Smart }
50301649561SJames Smart 
50401649561SJames Smart /**
50501649561SJames Smart  * lpfc_nvme_ls_abort - Issue an Link Service request
50601649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
50701649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
50801649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
50901649561SJames Smart  *
51001649561SJames Smart  * Driver registers this routine to handle any link service request
51101649561SJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
51201649561SJames Smart  *
51301649561SJames Smart  * Return value :
51401649561SJames Smart  *   0 - Success
51501649561SJames Smart  *   TODO: What are the failure codes.
51601649561SJames Smart  **/
51701649561SJames Smart static void
51801649561SJames Smart lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
51901649561SJames Smart 		   struct nvme_fc_remote_port *pnvme_rport,
52001649561SJames Smart 		   struct nvmefc_ls_req *pnvme_lsreq)
52101649561SJames Smart {
52201649561SJames Smart 	struct lpfc_nvme_lport *lport;
52301649561SJames Smart 	struct lpfc_vport *vport;
52401649561SJames Smart 	struct lpfc_hba *phba;
52501649561SJames Smart 	struct lpfc_nodelist *ndlp;
52601649561SJames Smart 	LIST_HEAD(abort_list);
52701649561SJames Smart 	struct lpfc_sli_ring *pring;
52801649561SJames Smart 	struct lpfc_iocbq *wqe, *next_wqe;
52901649561SJames Smart 
53001649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
53101649561SJames Smart 	vport = lport->vport;
53201649561SJames Smart 	phba = vport->phba;
53301649561SJames Smart 
53401649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
53501649561SJames Smart 	if (!ndlp) {
53601649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
53701649561SJames Smart 				 "6049 Could not find node for DID %x\n",
53801649561SJames Smart 				 pnvme_rport->port_id);
53901649561SJames Smart 		return;
54001649561SJames Smart 	}
54101649561SJames Smart 
54201649561SJames Smart 	/* Expand print to include key fields. */
54301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
54401649561SJames Smart 			 "6040 ENTER.  lport %p, rport %p lsreq %p rqstlen:%d "
545825c6abbSArnd Bergmann 			 "rsplen:%d %pad %pad\n",
54601649561SJames Smart 			 pnvme_lport, pnvme_rport,
54701649561SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
548825c6abbSArnd Bergmann 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
549825c6abbSArnd Bergmann 			 &pnvme_lsreq->rspdma);
55001649561SJames Smart 
55101649561SJames Smart 	/*
55201649561SJames Smart 	 * Lock the ELS ring txcmplq and build a local list of all ELS IOs
55301649561SJames Smart 	 * that need an ABTS.  The IOs need to stay on the txcmplq so that
55401649561SJames Smart 	 * the abort operation completes them successfully.
55501649561SJames Smart 	 */
55601649561SJames Smart 	pring = phba->sli4_hba.nvmels_wq->pring;
55701649561SJames Smart 	spin_lock_irq(&phba->hbalock);
55801649561SJames Smart 	spin_lock(&pring->ring_lock);
55901649561SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
56001649561SJames Smart 		/* Add to abort_list on on NDLP match. */
56101649561SJames Smart 		if (lpfc_check_sli_ndlp(phba, pring, wqe, ndlp)) {
56201649561SJames Smart 			wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
56301649561SJames Smart 			list_add_tail(&wqe->dlist, &abort_list);
56401649561SJames Smart 		}
56501649561SJames Smart 	}
56601649561SJames Smart 	spin_unlock(&pring->ring_lock);
56701649561SJames Smart 	spin_unlock_irq(&phba->hbalock);
56801649561SJames Smart 
56901649561SJames Smart 	/* Abort the targeted IOs and remove them from the abort list. */
57001649561SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &abort_list, dlist) {
57101649561SJames Smart 		spin_lock_irq(&phba->hbalock);
57201649561SJames Smart 		list_del_init(&wqe->dlist);
57301649561SJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, wqe);
57401649561SJames Smart 		spin_unlock_irq(&phba->hbalock);
57501649561SJames Smart 	}
57601649561SJames Smart }
57701649561SJames Smart 
57801649561SJames Smart /* Fix up the existing sgls for NVME IO. */
57901649561SJames Smart static void
58001649561SJames Smart lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
58101649561SJames Smart 		       struct lpfc_nvme_buf *lpfc_ncmd,
58201649561SJames Smart 		       struct nvmefc_fcp_req *nCmd)
58301649561SJames Smart {
58401649561SJames Smart 	struct sli4_sge *sgl;
58501649561SJames Smart 	union lpfc_wqe128 *wqe;
58601649561SJames Smart 	uint32_t *wptr, *dptr;
58701649561SJames Smart 
58801649561SJames Smart 	/*
58901649561SJames Smart 	 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
59001649561SJames Smart 	 * match NVME.  NVME sends 96 bytes. Also, use the
59101649561SJames Smart 	 * nvme commands command and response dma addresses
59201649561SJames Smart 	 * rather than the virtual memory to ease the restore
59301649561SJames Smart 	 * operation.
59401649561SJames Smart 	 */
59501649561SJames Smart 	sgl = lpfc_ncmd->nvme_sgl;
59601649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
59701649561SJames Smart 
59801649561SJames Smart 	sgl++;
59901649561SJames Smart 
60001649561SJames Smart 	/* Setup the physical region for the FCP RSP */
60101649561SJames Smart 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
60201649561SJames Smart 	sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
60301649561SJames Smart 	sgl->word2 = le32_to_cpu(sgl->word2);
60401649561SJames Smart 	if (nCmd->sg_cnt)
60501649561SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
60601649561SJames Smart 	else
60701649561SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 1);
60801649561SJames Smart 	sgl->word2 = cpu_to_le32(sgl->word2);
60901649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->rsplen);
61001649561SJames Smart 
61101649561SJames Smart 	/*
61201649561SJames Smart 	 * Get a local pointer to the built-in wqe and correct
61301649561SJames Smart 	 * the cmd size to match NVME's 96 bytes and fix
61401649561SJames Smart 	 * the dma address.
61501649561SJames Smart 	 */
61601649561SJames Smart 
61701649561SJames Smart 	/* 128 byte wqe support here */
61801649561SJames Smart 	wqe = (union lpfc_wqe128 *)&lpfc_ncmd->cur_iocbq.wqe;
61901649561SJames Smart 
62001649561SJames Smart 	/* Word 0-2 - NVME CMND IU (embedded payload) */
62101649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
62201649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = 60;
62301649561SJames Smart 	wqe->generic.bde.addrHigh = 0;
62401649561SJames Smart 	wqe->generic.bde.addrLow =  64;  /* Word 16 */
62501649561SJames Smart 
62601649561SJames Smart 	/* Word 3 */
62701649561SJames Smart 	bf_set(payload_offset_len, &wqe->fcp_icmd,
62801649561SJames Smart 	       (nCmd->rsplen + nCmd->cmdlen));
62901649561SJames Smart 
63001649561SJames Smart 	/* Word 10 */
63101649561SJames Smart 	bf_set(wqe_nvme, &wqe->fcp_icmd.wqe_com, 1);
63201649561SJames Smart 	bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
63301649561SJames Smart 
63401649561SJames Smart 	/*
63501649561SJames Smart 	 * Embed the payload in the last half of the WQE
63601649561SJames Smart 	 * WQE words 16-30 get the NVME CMD IU payload
63701649561SJames Smart 	 *
638b06a622fSJames Smart 	 * WQE words 16-19 get payload Words 1-4
63901649561SJames Smart 	 * WQE words 20-21 get payload Words 6-7
64001649561SJames Smart 	 * WQE words 22-29 get payload Words 16-23
64101649561SJames Smart 	 */
642b06a622fSJames Smart 	wptr = &wqe->words[16];  /* WQE ptr */
64301649561SJames Smart 	dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
644b06a622fSJames Smart 	dptr++;			/* Skip Word 0 in payload */
64501649561SJames Smart 
646b06a622fSJames Smart 	*wptr++ = *dptr++;	/* Word 1 */
64701649561SJames Smart 	*wptr++ = *dptr++;	/* Word 2 */
64801649561SJames Smart 	*wptr++ = *dptr++;	/* Word 3 */
64901649561SJames Smart 	*wptr++ = *dptr++;	/* Word 4 */
65001649561SJames Smart 	dptr++;			/* Skip Word 5 in payload */
65101649561SJames Smart 	*wptr++ = *dptr++;	/* Word 6 */
65201649561SJames Smart 	*wptr++ = *dptr++;	/* Word 7 */
65301649561SJames Smart 	dptr += 8;		/* Skip Words 8-15 in payload */
65401649561SJames Smart 	*wptr++ = *dptr++;	/* Word 16 */
65501649561SJames Smart 	*wptr++ = *dptr++;	/* Word 17 */
65601649561SJames Smart 	*wptr++ = *dptr++;	/* Word 18 */
65701649561SJames Smart 	*wptr++ = *dptr++;	/* Word 19 */
65801649561SJames Smart 	*wptr++ = *dptr++;	/* Word 20 */
65901649561SJames Smart 	*wptr++ = *dptr++;	/* Word 21 */
66001649561SJames Smart 	*wptr++ = *dptr++;	/* Word 22 */
66101649561SJames Smart 	*wptr   = *dptr;	/* Word 23 */
66201649561SJames Smart }
66301649561SJames Smart 
664bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
665bd2cdd5eSJames Smart static void
666bd2cdd5eSJames Smart lpfc_nvme_ktime(struct lpfc_hba *phba,
667bd2cdd5eSJames Smart 		struct lpfc_nvme_buf *lpfc_ncmd)
668bd2cdd5eSJames Smart {
669bd2cdd5eSJames Smart 	uint64_t seg1, seg2, seg3, seg4;
670bd2cdd5eSJames Smart 
671bd2cdd5eSJames Smart 	if (!phba->ktime_on)
672bd2cdd5eSJames Smart 		return;
673bd2cdd5eSJames Smart 	if (!lpfc_ncmd->ts_last_cmd ||
674bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_cmd_start ||
675bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_cmd_wqput ||
676bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_isr_cmpl ||
677bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_data_nvme)
678bd2cdd5eSJames Smart 		return;
679bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_cmd_start < lpfc_ncmd->ts_last_cmd)
680bd2cdd5eSJames Smart 		return;
681bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_cmd_wqput < lpfc_ncmd->ts_cmd_start)
682bd2cdd5eSJames Smart 		return;
683bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_isr_cmpl < lpfc_ncmd->ts_cmd_wqput)
684bd2cdd5eSJames Smart 		return;
685bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_isr_cmpl)
686bd2cdd5eSJames Smart 		return;
687bd2cdd5eSJames Smart 	/*
688bd2cdd5eSJames Smart 	 * Segment 1 - Time from Last FCP command cmpl is handed
689bd2cdd5eSJames Smart 	 * off to NVME Layer to start of next command.
690bd2cdd5eSJames Smart 	 * Segment 2 - Time from Driver receives a IO cmd start
691bd2cdd5eSJames Smart 	 * from NVME Layer to WQ put is done on IO cmd.
692bd2cdd5eSJames Smart 	 * Segment 3 - Time from Driver WQ put is done on IO cmd
693bd2cdd5eSJames Smart 	 * to MSI-X ISR for IO cmpl.
694bd2cdd5eSJames Smart 	 * Segment 4 - Time from MSI-X ISR for IO cmpl to when
695bd2cdd5eSJames Smart 	 * cmpl is handled off to the NVME Layer.
696bd2cdd5eSJames Smart 	 */
697bd2cdd5eSJames Smart 	seg1 = lpfc_ncmd->ts_cmd_start - lpfc_ncmd->ts_last_cmd;
698bd2cdd5eSJames Smart 	if (seg1 > 5000000)  /* 5 ms - for sequential IOs */
699bd2cdd5eSJames Smart 		return;
700bd2cdd5eSJames Smart 
701bd2cdd5eSJames Smart 	/* Calculate times relative to start of IO */
702bd2cdd5eSJames Smart 	seg2 = (lpfc_ncmd->ts_cmd_wqput - lpfc_ncmd->ts_cmd_start);
703bd2cdd5eSJames Smart 	seg3 = (lpfc_ncmd->ts_isr_cmpl -
704bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start) - seg2;
705bd2cdd5eSJames Smart 	seg4 = (lpfc_ncmd->ts_data_nvme -
706bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start) - seg2 - seg3;
707bd2cdd5eSJames Smart 	phba->ktime_data_samples++;
708bd2cdd5eSJames Smart 	phba->ktime_seg1_total += seg1;
709bd2cdd5eSJames Smart 	if (seg1 < phba->ktime_seg1_min)
710bd2cdd5eSJames Smart 		phba->ktime_seg1_min = seg1;
711bd2cdd5eSJames Smart 	else if (seg1 > phba->ktime_seg1_max)
712bd2cdd5eSJames Smart 		phba->ktime_seg1_max = seg1;
713bd2cdd5eSJames Smart 	phba->ktime_seg2_total += seg2;
714bd2cdd5eSJames Smart 	if (seg2 < phba->ktime_seg2_min)
715bd2cdd5eSJames Smart 		phba->ktime_seg2_min = seg2;
716bd2cdd5eSJames Smart 	else if (seg2 > phba->ktime_seg2_max)
717bd2cdd5eSJames Smart 		phba->ktime_seg2_max = seg2;
718bd2cdd5eSJames Smart 	phba->ktime_seg3_total += seg3;
719bd2cdd5eSJames Smart 	if (seg3 < phba->ktime_seg3_min)
720bd2cdd5eSJames Smart 		phba->ktime_seg3_min = seg3;
721bd2cdd5eSJames Smart 	else if (seg3 > phba->ktime_seg3_max)
722bd2cdd5eSJames Smart 		phba->ktime_seg3_max = seg3;
723bd2cdd5eSJames Smart 	phba->ktime_seg4_total += seg4;
724bd2cdd5eSJames Smart 	if (seg4 < phba->ktime_seg4_min)
725bd2cdd5eSJames Smart 		phba->ktime_seg4_min = seg4;
726bd2cdd5eSJames Smart 	else if (seg4 > phba->ktime_seg4_max)
727bd2cdd5eSJames Smart 		phba->ktime_seg4_max = seg4;
728bd2cdd5eSJames Smart 
729bd2cdd5eSJames Smart 	lpfc_ncmd->ts_last_cmd = 0;
730bd2cdd5eSJames Smart 	lpfc_ncmd->ts_cmd_start = 0;
731bd2cdd5eSJames Smart 	lpfc_ncmd->ts_cmd_wqput  = 0;
732bd2cdd5eSJames Smart 	lpfc_ncmd->ts_isr_cmpl = 0;
733bd2cdd5eSJames Smart 	lpfc_ncmd->ts_data_nvme = 0;
734bd2cdd5eSJames Smart }
735bd2cdd5eSJames Smart #endif
736bd2cdd5eSJames Smart 
73701649561SJames Smart /**
73801649561SJames Smart  * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
73901649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
74001649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
74101649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
74201649561SJames Smart  *
74301649561SJames Smart  * Driver registers this routine as it io request handler.  This
74401649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
74501649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
74601649561SJames Smart  *
74701649561SJames Smart  * Return value :
74801649561SJames Smart  *   0 - Success
74901649561SJames Smart  *   TODO: What are the failure codes.
75001649561SJames Smart  **/
75101649561SJames Smart static void
75201649561SJames Smart lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
75301649561SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
75401649561SJames Smart {
75501649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd =
75601649561SJames Smart 		(struct lpfc_nvme_buf *)pwqeIn->context1;
75701649561SJames Smart 	struct lpfc_vport *vport = pwqeIn->vport;
75801649561SJames Smart 	struct nvmefc_fcp_req *nCmd;
75901649561SJames Smart 	struct nvme_fc_ersp_iu *ep;
76001649561SJames Smart 	struct nvme_fc_cmd_iu *cp;
76101649561SJames Smart 	struct lpfc_nvme_rport *rport;
76201649561SJames Smart 	struct lpfc_nodelist *ndlp;
763bbe3012bSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
76401649561SJames Smart 	unsigned long flags;
76501649561SJames Smart 	uint32_t code;
76601649561SJames Smart 	uint16_t cid, sqhd, data;
76701649561SJames Smart 	uint32_t *ptr;
76801649561SJames Smart 
76901649561SJames Smart 	/* Sanity check on return of outstanding command */
77001649561SJames Smart 	if (!lpfc_ncmd || !lpfc_ncmd->nvmeCmd || !lpfc_ncmd->nrport) {
77101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
77201649561SJames Smart 				 "6071 Completion pointers bad on wqe %p.\n",
77301649561SJames Smart 				 wcqe);
77401649561SJames Smart 		return;
77501649561SJames Smart 	}
7762cee7808SJames Smart 	atomic_inc(&phba->fc4NvmeIoCmpls);
77701649561SJames Smart 
77801649561SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
77901649561SJames Smart 	rport = lpfc_ncmd->nrport;
78001649561SJames Smart 
781bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
782bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
783bd2cdd5eSJames Smart 			 bf_get(lpfc_wcqe_c_status, wcqe), wcqe->parameter);
78401649561SJames Smart 	/*
78501649561SJames Smart 	 * Catch race where our node has transitioned, but the
78601649561SJames Smart 	 * transport is still transitioning.
78701649561SJames Smart 	 */
78801649561SJames Smart 	ndlp = rport->ndlp;
78901649561SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
79001649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
791c154e750SJames Smart 				 "6061 rport %p,  DID x%06x node not ready.\n",
792c154e750SJames Smart 				 rport, rport->remoteport->port_id);
79301649561SJames Smart 
79401649561SJames Smart 		ndlp = lpfc_findnode_did(vport, rport->remoteport->port_id);
79501649561SJames Smart 		if (!ndlp) {
79601649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
79701649561SJames Smart 					 "6062 Ignoring NVME cmpl.  No ndlp\n");
79801649561SJames Smart 			goto out_err;
79901649561SJames Smart 		}
80001649561SJames Smart 	}
80101649561SJames Smart 
80201649561SJames Smart 	code = bf_get(lpfc_wcqe_c_code, wcqe);
80301649561SJames Smart 	if (code == CQE_CODE_NVME_ERSP) {
80401649561SJames Smart 		/* For this type of CQE, we need to rebuild the rsp */
80501649561SJames Smart 		ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
80601649561SJames Smart 
80701649561SJames Smart 		/*
80801649561SJames Smart 		 * Get Command Id from cmd to plug into response. This
80901649561SJames Smart 		 * code is not needed in the next NVME Transport drop.
81001649561SJames Smart 		 */
81101649561SJames Smart 		cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
81201649561SJames Smart 		cid = cp->sqe.common.command_id;
81301649561SJames Smart 
81401649561SJames Smart 		/*
81501649561SJames Smart 		 * RSN is in CQE word 2
81601649561SJames Smart 		 * SQHD is in CQE Word 3 bits 15:0
81701649561SJames Smart 		 * Cmd Specific info is in CQE Word 1
81801649561SJames Smart 		 * and in CQE Word 0 bits 15:0
81901649561SJames Smart 		 */
82001649561SJames Smart 		sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
82101649561SJames Smart 
82201649561SJames Smart 		/* Now lets build the NVME ERSP IU */
82301649561SJames Smart 		ep->iu_len = cpu_to_be16(8);
82401649561SJames Smart 		ep->rsn = wcqe->parameter;
82501649561SJames Smart 		ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
82601649561SJames Smart 		ep->rsvd12 = 0;
82701649561SJames Smart 		ptr = (uint32_t *)&ep->cqe.result.u64;
82801649561SJames Smart 		*ptr++ = wcqe->total_data_placed;
82901649561SJames Smart 		data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
83001649561SJames Smart 		*ptr = (uint32_t)data;
83101649561SJames Smart 		ep->cqe.sq_head = sqhd;
83201649561SJames Smart 		ep->cqe.sq_id =  nCmd->sqid;
83301649561SJames Smart 		ep->cqe.command_id = cid;
83401649561SJames Smart 		ep->cqe.status = 0;
83501649561SJames Smart 
83601649561SJames Smart 		lpfc_ncmd->status = IOSTAT_SUCCESS;
83701649561SJames Smart 		lpfc_ncmd->result = 0;
83801649561SJames Smart 		nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
83901649561SJames Smart 		nCmd->transferred_length = nCmd->payload_length;
84001649561SJames Smart 	} else {
84101649561SJames Smart 		lpfc_ncmd->status = (bf_get(lpfc_wcqe_c_status, wcqe) &
84201649561SJames Smart 			    LPFC_IOCB_STATUS_MASK);
84301649561SJames Smart 		lpfc_ncmd->result = wcqe->parameter;
84401649561SJames Smart 
84501649561SJames Smart 		/* For NVME, the only failure path that results in an
84601649561SJames Smart 		 * IO error is when the adapter rejects it.  All other
84701649561SJames Smart 		 * conditions are a success case and resolved by the
84801649561SJames Smart 		 * transport.
84901649561SJames Smart 		 * IOSTAT_FCP_RSP_ERROR means:
85001649561SJames Smart 		 * 1. Length of data received doesn't match total
85101649561SJames Smart 		 *    transfer length in WQE
85201649561SJames Smart 		 * 2. If the RSP payload does NOT match these cases:
85301649561SJames Smart 		 *    a. RSP length 12/24 bytes and all zeros
85401649561SJames Smart 		 *    b. NVME ERSP
85501649561SJames Smart 		 */
85601649561SJames Smart 		switch (lpfc_ncmd->status) {
85701649561SJames Smart 		case IOSTAT_SUCCESS:
85801649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
85901649561SJames Smart 			nCmd->rcv_rsplen = 0;
86001649561SJames Smart 			nCmd->status = 0;
86101649561SJames Smart 			break;
86201649561SJames Smart 		case IOSTAT_FCP_RSP_ERROR:
86301649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
86401649561SJames Smart 			nCmd->rcv_rsplen = wcqe->parameter;
86501649561SJames Smart 			nCmd->status = 0;
86601649561SJames Smart 			/* Sanity check */
86701649561SJames Smart 			if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
86801649561SJames Smart 				break;
86901649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
87001649561SJames Smart 					 "6081 NVME Completion Protocol Error: "
87186c67379SJames Smart 					 "xri %x status x%x result x%x "
87286c67379SJames Smart 					 "placed x%x\n",
87386c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
87401649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
87501649561SJames Smart 					 wcqe->total_data_placed);
87601649561SJames Smart 			break;
87701649561SJames Smart 		default:
87801649561SJames Smart out_err:
87901649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
88086c67379SJames Smart 					 "6072 NVME Completion Error: xri %x "
88101649561SJames Smart 					 "status x%x result x%x placed x%x\n",
88286c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
88301649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
88401649561SJames Smart 					 wcqe->total_data_placed);
88501649561SJames Smart 			nCmd->transferred_length = 0;
88601649561SJames Smart 			nCmd->rcv_rsplen = 0;
88701649561SJames Smart 			nCmd->status = NVME_SC_FC_TRANSPORT_ERROR;
88801649561SJames Smart 		}
88901649561SJames Smart 	}
89001649561SJames Smart 
89101649561SJames Smart 	/* pick up SLI4 exhange busy condition */
89201649561SJames Smart 	if (bf_get(lpfc_wcqe_c_xb, wcqe))
89301649561SJames Smart 		lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
89401649561SJames Smart 	else
89501649561SJames Smart 		lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
89601649561SJames Smart 
89701649561SJames Smart 	if (ndlp && NLP_CHK_NODE_ACT(ndlp))
89801649561SJames Smart 		atomic_dec(&ndlp->cmd_pending);
89901649561SJames Smart 
90001649561SJames Smart 	/* Update stats and complete the IO.  There is
90101649561SJames Smart 	 * no need for dma unprep because the nvme_transport
90201649561SJames Smart 	 * owns the dma address.
90301649561SJames Smart 	 */
904bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
905bd2cdd5eSJames Smart 	if (phba->ktime_on) {
906bd2cdd5eSJames Smart 		lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
907bd2cdd5eSJames Smart 		lpfc_ncmd->ts_data_nvme = ktime_get_ns();
908bd2cdd5eSJames Smart 		phba->ktime_last_cmd = lpfc_ncmd->ts_data_nvme;
909bd2cdd5eSJames Smart 		lpfc_nvme_ktime(phba, lpfc_ncmd);
910bd2cdd5eSJames Smart 	}
911bd2cdd5eSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
912bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu != smp_processor_id())
913bd2cdd5eSJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
914bd2cdd5eSJames Smart 					 "6701 CPU Check cmpl: "
915bd2cdd5eSJames Smart 					 "cpu %d expect %d\n",
916bd2cdd5eSJames Smart 					 smp_processor_id(), lpfc_ncmd->cpu);
917bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
918bd2cdd5eSJames Smart 			phba->cpucheck_cmpl_io[lpfc_ncmd->cpu]++;
919bd2cdd5eSJames Smart 	}
920bd2cdd5eSJames Smart #endif
921bbe3012bSJames Smart 	freqpriv = nCmd->private;
922bbe3012bSJames Smart 	freqpriv->nvme_buf = NULL;
92301649561SJames Smart 	nCmd->done(nCmd);
92401649561SJames Smart 
92501649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
92601649561SJames Smart 	lpfc_ncmd->nrport = NULL;
92701649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
92801649561SJames Smart 
92901649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
93001649561SJames Smart }
93101649561SJames Smart 
93201649561SJames Smart 
93301649561SJames Smart /**
93401649561SJames Smart  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
93501649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
93601649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
93701649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
93801649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
93901649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
94001649561SJames Smart  *
94101649561SJames Smart  * Driver registers this routine as it io request handler.  This
94201649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
94301649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
94401649561SJames Smart  *
94501649561SJames Smart  * Return value :
94601649561SJames Smart  *   0 - Success
94701649561SJames Smart  *   TODO: What are the failure codes.
94801649561SJames Smart  **/
94901649561SJames Smart static int
95001649561SJames Smart lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
95101649561SJames Smart 		      struct lpfc_nvme_buf *lpfc_ncmd,
95201649561SJames Smart 		      struct lpfc_nodelist *pnode)
95301649561SJames Smart {
95401649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
95501649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
95601649561SJames Smart 	struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
95701649561SJames Smart 	union lpfc_wqe128 *wqe = (union lpfc_wqe128 *)&pwqeq->wqe;
95801649561SJames Smart 	uint32_t req_len;
95901649561SJames Smart 
96001649561SJames Smart 	if (!pnode || !NLP_CHK_NODE_ACT(pnode))
96101649561SJames Smart 		return -EINVAL;
96201649561SJames Smart 
96301649561SJames Smart 	/*
96401649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
96501649561SJames Smart 	 * the single mapping, or neither.
96601649561SJames Smart 	 */
96701649561SJames Smart 	wqe->fcp_iwrite.initial_xfer_len = 0;
96801649561SJames Smart 	if (nCmd->sg_cnt) {
96901649561SJames Smart 		if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
97001649561SJames Smart 			/* Word 5 */
97101649561SJames Smart 			if ((phba->cfg_nvme_enable_fb) &&
97201649561SJames Smart 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
97301649561SJames Smart 				req_len = lpfc_ncmd->nvmeCmd->payload_length;
97401649561SJames Smart 				if (req_len < pnode->nvme_fb_size)
97501649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
97601649561SJames Smart 						req_len;
97701649561SJames Smart 				else
97801649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
97901649561SJames Smart 						pnode->nvme_fb_size;
98001649561SJames Smart 			}
98101649561SJames Smart 
98201649561SJames Smart 			/* Word 7 */
98301649561SJames Smart 			bf_set(wqe_cmnd, &wqe->generic.wqe_com,
98401649561SJames Smart 			       CMD_FCP_IWRITE64_WQE);
98501649561SJames Smart 			bf_set(wqe_pu, &wqe->generic.wqe_com,
98601649561SJames Smart 			       PARM_READ_CHECK);
98701649561SJames Smart 
98801649561SJames Smart 			/* Word 10 */
98901649561SJames Smart 			bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
99001649561SJames Smart 			bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com,
99101649561SJames Smart 			       LPFC_WQE_IOD_WRITE);
99201649561SJames Smart 			bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com,
99301649561SJames Smart 			       LPFC_WQE_LENLOC_WORD4);
99401649561SJames Smart 			if (phba->cfg_nvme_oas)
99501649561SJames Smart 				bf_set(wqe_oas, &wqe->fcp_iwrite.wqe_com, 1);
99601649561SJames Smart 
99701649561SJames Smart 			/* Word 11 */
99801649561SJames Smart 			bf_set(wqe_cmd_type, &wqe->generic.wqe_com,
99901649561SJames Smart 			       NVME_WRITE_CMD);
100001649561SJames Smart 
10012cee7808SJames Smart 			atomic_inc(&phba->fc4NvmeOutputRequests);
100201649561SJames Smart 		} else {
100301649561SJames Smart 			/* Word 7 */
100401649561SJames Smart 			bf_set(wqe_cmnd, &wqe->generic.wqe_com,
100501649561SJames Smart 			       CMD_FCP_IREAD64_WQE);
100601649561SJames Smart 			bf_set(wqe_pu, &wqe->generic.wqe_com,
100701649561SJames Smart 			       PARM_READ_CHECK);
100801649561SJames Smart 
100901649561SJames Smart 			/* Word 10 */
101001649561SJames Smart 			bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
101101649561SJames Smart 			bf_set(wqe_iod, &wqe->fcp_iread.wqe_com,
101201649561SJames Smart 			       LPFC_WQE_IOD_READ);
101301649561SJames Smart 			bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com,
101401649561SJames Smart 			       LPFC_WQE_LENLOC_WORD4);
101501649561SJames Smart 			if (phba->cfg_nvme_oas)
101601649561SJames Smart 				bf_set(wqe_oas, &wqe->fcp_iread.wqe_com, 1);
101701649561SJames Smart 
101801649561SJames Smart 			/* Word 11 */
101901649561SJames Smart 			bf_set(wqe_cmd_type, &wqe->generic.wqe_com,
102001649561SJames Smart 			       NVME_READ_CMD);
102101649561SJames Smart 
10222cee7808SJames Smart 			atomic_inc(&phba->fc4NvmeInputRequests);
102301649561SJames Smart 		}
102401649561SJames Smart 	} else {
102501649561SJames Smart 		/* Word 4 */
102601649561SJames Smart 		wqe->fcp_icmd.rsrvd4 = 0;
102701649561SJames Smart 
102801649561SJames Smart 		/* Word 7 */
102901649561SJames Smart 		bf_set(wqe_cmnd, &wqe->generic.wqe_com, CMD_FCP_ICMND64_WQE);
103001649561SJames Smart 		bf_set(wqe_pu, &wqe->generic.wqe_com, 0);
103101649561SJames Smart 
103201649561SJames Smart 		/* Word 10 */
103301649561SJames Smart 		bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
103401649561SJames Smart 		bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_WRITE);
103501649561SJames Smart 		bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com,
103601649561SJames Smart 		       LPFC_WQE_LENLOC_NONE);
103701649561SJames Smart 		if (phba->cfg_nvme_oas)
103801649561SJames Smart 			bf_set(wqe_oas, &wqe->fcp_icmd.wqe_com, 1);
103901649561SJames Smart 
104001649561SJames Smart 		/* Word 11 */
104101649561SJames Smart 		bf_set(wqe_cmd_type, &wqe->generic.wqe_com, NVME_READ_CMD);
104201649561SJames Smart 
10432cee7808SJames Smart 		atomic_inc(&phba->fc4NvmeControlRequests);
104401649561SJames Smart 	}
104501649561SJames Smart 	/*
104601649561SJames Smart 	 * Finish initializing those WQE fields that are independent
104701649561SJames Smart 	 * of the nvme_cmnd request_buffer
104801649561SJames Smart 	 */
104901649561SJames Smart 
105001649561SJames Smart 	/* Word 6 */
105101649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
105201649561SJames Smart 	       phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
105301649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
105401649561SJames Smart 
105501649561SJames Smart 	/* Word 7 */
105601649561SJames Smart 	/* Preserve Class data in the ndlp. */
105701649561SJames Smart 	bf_set(wqe_class, &wqe->generic.wqe_com,
105801649561SJames Smart 	       (pnode->nlp_fcp_info & 0x0f));
105901649561SJames Smart 
106001649561SJames Smart 	/* Word 8 */
106101649561SJames Smart 	wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
106201649561SJames Smart 
106301649561SJames Smart 	/* Word 9 */
106401649561SJames Smart 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
106501649561SJames Smart 
106601649561SJames Smart 	/* Word 11 */
106701649561SJames Smart 	bf_set(wqe_cqid, &wqe->generic.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
106801649561SJames Smart 
106901649561SJames Smart 	pwqeq->vport = vport;
107001649561SJames Smart 	return 0;
107101649561SJames Smart }
107201649561SJames Smart 
107301649561SJames Smart 
107401649561SJames Smart /**
107501649561SJames Smart  * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
107601649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
107701649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
107801649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
107901649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
108001649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
108101649561SJames Smart  *
108201649561SJames Smart  * Driver registers this routine as it io request handler.  This
108301649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
108401649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
108501649561SJames Smart  *
108601649561SJames Smart  * Return value :
108701649561SJames Smart  *   0 - Success
108801649561SJames Smart  *   TODO: What are the failure codes.
108901649561SJames Smart  **/
109001649561SJames Smart static int
109101649561SJames Smart lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
109201649561SJames Smart 		      struct lpfc_nvme_buf *lpfc_ncmd)
109301649561SJames Smart {
109401649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
109501649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
109601649561SJames Smart 	union lpfc_wqe128 *wqe = (union lpfc_wqe128 *)&lpfc_ncmd->cur_iocbq.wqe;
109701649561SJames Smart 	struct sli4_sge *sgl = lpfc_ncmd->nvme_sgl;
109801649561SJames Smart 	struct scatterlist *data_sg;
109901649561SJames Smart 	struct sli4_sge *first_data_sgl;
110001649561SJames Smart 	dma_addr_t physaddr;
110101649561SJames Smart 	uint32_t num_bde = 0;
110201649561SJames Smart 	uint32_t dma_len;
110301649561SJames Smart 	uint32_t dma_offset = 0;
110401649561SJames Smart 	int nseg, i;
110501649561SJames Smart 
110601649561SJames Smart 	/* Fix up the command and response DMA stuff. */
110701649561SJames Smart 	lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
110801649561SJames Smart 
110901649561SJames Smart 	/*
111001649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
111101649561SJames Smart 	 * the single mapping, or neither.
111201649561SJames Smart 	 */
111301649561SJames Smart 	if (nCmd->sg_cnt) {
111401649561SJames Smart 		/*
111501649561SJames Smart 		 * Jump over the cmd and rsp SGEs.  The fix routine
111601649561SJames Smart 		 * has already adjusted for this.
111701649561SJames Smart 		 */
111801649561SJames Smart 		sgl += 2;
111901649561SJames Smart 
112001649561SJames Smart 		first_data_sgl = sgl;
112101649561SJames Smart 		lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
11224d4c4a4aSJames Smart 		if (lpfc_ncmd->seg_cnt > phba->cfg_nvme_seg_cnt) {
112301649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
112401649561SJames Smart 					"6058 Too many sg segments from "
112501649561SJames Smart 					"NVME Transport.  Max %d, "
112601649561SJames Smart 					"nvmeIO sg_cnt %d\n",
11274d4c4a4aSJames Smart 					phba->cfg_nvme_seg_cnt,
112801649561SJames Smart 					lpfc_ncmd->seg_cnt);
112901649561SJames Smart 			lpfc_ncmd->seg_cnt = 0;
113001649561SJames Smart 			return 1;
113101649561SJames Smart 		}
113201649561SJames Smart 
113301649561SJames Smart 		/*
113401649561SJames Smart 		 * The driver established a maximum scatter-gather segment count
113501649561SJames Smart 		 * during probe that limits the number of sg elements in any
113601649561SJames Smart 		 * single nvme command.  Just run through the seg_cnt and format
113701649561SJames Smart 		 * the sge's.
113801649561SJames Smart 		 */
113901649561SJames Smart 		nseg = nCmd->sg_cnt;
114001649561SJames Smart 		data_sg = nCmd->first_sgl;
114101649561SJames Smart 		for (i = 0; i < nseg; i++) {
114201649561SJames Smart 			if (data_sg == NULL) {
114301649561SJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
114401649561SJames Smart 						"6059 dptr err %d, nseg %d\n",
114501649561SJames Smart 						i, nseg);
114601649561SJames Smart 				lpfc_ncmd->seg_cnt = 0;
114701649561SJames Smart 				return 1;
114801649561SJames Smart 			}
114901649561SJames Smart 			physaddr = data_sg->dma_address;
115001649561SJames Smart 			dma_len = data_sg->length;
115101649561SJames Smart 			sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
115201649561SJames Smart 			sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
115301649561SJames Smart 			sgl->word2 = le32_to_cpu(sgl->word2);
115401649561SJames Smart 			if ((num_bde + 1) == nseg)
115501649561SJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 1);
115601649561SJames Smart 			else
115701649561SJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 0);
115801649561SJames Smart 			bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
115901649561SJames Smart 			bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
116001649561SJames Smart 			sgl->word2 = cpu_to_le32(sgl->word2);
116101649561SJames Smart 			sgl->sge_len = cpu_to_le32(dma_len);
116201649561SJames Smart 
116301649561SJames Smart 			dma_offset += dma_len;
116401649561SJames Smart 			data_sg = sg_next(data_sg);
116501649561SJames Smart 			sgl++;
116601649561SJames Smart 		}
116701649561SJames Smart 	} else {
116801649561SJames Smart 		/* For this clause to be valid, the payload_length
116901649561SJames Smart 		 * and sg_cnt must zero.
117001649561SJames Smart 		 */
117101649561SJames Smart 		if (nCmd->payload_length != 0) {
117201649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
117301649561SJames Smart 					"6063 NVME DMA Prep Err: sg_cnt %d "
117401649561SJames Smart 					"payload_length x%x\n",
117501649561SJames Smart 					nCmd->sg_cnt, nCmd->payload_length);
117601649561SJames Smart 			return 1;
117701649561SJames Smart 		}
117801649561SJames Smart 	}
117901649561SJames Smart 
118001649561SJames Smart 	/*
118101649561SJames Smart 	 * Due to difference in data length between DIF/non-DIF paths,
118201649561SJames Smart 	 * we need to set word 4 of WQE here
118301649561SJames Smart 	 */
118401649561SJames Smart 	wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
118501649561SJames Smart 	return 0;
118601649561SJames Smart }
118701649561SJames Smart 
118801649561SJames Smart /**
118901649561SJames Smart  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
119001649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
119101649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
119201649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
119301649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
119401649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
119501649561SJames Smart  *
119601649561SJames Smart  * Driver registers this routine as it io request handler.  This
119701649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
119801649561SJames Smart  * data structure to the rport
119901649561SJames Smart  indicated in @lpfc_nvme_rport.
120001649561SJames Smart  *
120101649561SJames Smart  * Return value :
120201649561SJames Smart  *   0 - Success
120301649561SJames Smart  *   TODO: What are the failure codes.
120401649561SJames Smart  **/
120501649561SJames Smart static int
120601649561SJames Smart lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
120701649561SJames Smart 			struct nvme_fc_remote_port *pnvme_rport,
120801649561SJames Smart 			void *hw_queue_handle,
120901649561SJames Smart 			struct nvmefc_fcp_req *pnvme_fcreq)
121001649561SJames Smart {
121101649561SJames Smart 	int ret = 0;
121201649561SJames Smart 	struct lpfc_nvme_lport *lport;
121301649561SJames Smart 	struct lpfc_vport *vport;
121401649561SJames Smart 	struct lpfc_hba *phba;
121501649561SJames Smart 	struct lpfc_nodelist *ndlp;
121601649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd;
121701649561SJames Smart 	struct lpfc_nvme_rport *rport;
121801649561SJames Smart 	struct lpfc_nvme_qhandle *lpfc_queue_info;
1219bbe3012bSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv = pnvme_fcreq->private;
1220bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1221bd2cdd5eSJames Smart 	uint64_t start = 0;
1222bd2cdd5eSJames Smart #endif
122301649561SJames Smart 
122401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
122501649561SJames Smart 	vport = lport->vport;
122601649561SJames Smart 	phba = vport->phba;
122701649561SJames Smart 
1228bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1229bd2cdd5eSJames Smart 	if (phba->ktime_on)
1230bd2cdd5eSJames Smart 		start = ktime_get_ns();
1231bd2cdd5eSJames Smart #endif
123201649561SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
123301649561SJames Smart 	lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
123401649561SJames Smart 
123501649561SJames Smart 	/*
123601649561SJames Smart 	 * Catch race where our node has transitioned, but the
123701649561SJames Smart 	 * transport is still transitioning.
123801649561SJames Smart 	 */
123901649561SJames Smart 	ndlp = rport->ndlp;
124001649561SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
124101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
124201649561SJames Smart 				 "6053 rport %p, ndlp %p, DID x%06x "
124301649561SJames Smart 				 "ndlp not ready.\n",
124401649561SJames Smart 				 rport, ndlp, pnvme_rport->port_id);
124501649561SJames Smart 
124601649561SJames Smart 		ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
124701649561SJames Smart 		if (!ndlp) {
124801649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
124901649561SJames Smart 					 "6066 Missing node for DID %x\n",
125001649561SJames Smart 					 pnvme_rport->port_id);
125101649561SJames Smart 			ret = -ENODEV;
125201649561SJames Smart 			goto out_fail;
125301649561SJames Smart 		}
125401649561SJames Smart 	}
125501649561SJames Smart 
125601649561SJames Smart 	/* The remote node has to be a mapped target or it's an error. */
125701649561SJames Smart 	if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
125801649561SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
125901649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
126001649561SJames Smart 				 "6036 rport %p, DID x%06x not ready for "
126101649561SJames Smart 				 "IO. State x%x, Type x%x\n",
126201649561SJames Smart 				 rport, pnvme_rport->port_id,
126301649561SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type);
126401649561SJames Smart 		ret = -ENODEV;
126501649561SJames Smart 		goto out_fail;
126601649561SJames Smart 
126701649561SJames Smart 	}
126801649561SJames Smart 
126901649561SJames Smart 	/* The node is shared with FCP IO, make sure the IO pending count does
127001649561SJames Smart 	 * not exceed the programmed depth.
127101649561SJames Smart 	 */
127201649561SJames Smart 	if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
127301649561SJames Smart 		ret = -EAGAIN;
127401649561SJames Smart 		goto out_fail;
127501649561SJames Smart 	}
127601649561SJames Smart 
127701649561SJames Smart 	lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp);
127801649561SJames Smart 	if (lpfc_ncmd == NULL) {
127901649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
128001649561SJames Smart 				 "6065 driver's buffer pool is empty, "
128101649561SJames Smart 				 "IO failed\n");
128201649561SJames Smart 		ret = -ENOMEM;
128301649561SJames Smart 		goto out_fail;
128401649561SJames Smart 	}
1285bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1286bd2cdd5eSJames Smart 	if (phba->ktime_on) {
1287bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start = start;
1288bd2cdd5eSJames Smart 		lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1289bd2cdd5eSJames Smart 	}
1290bd2cdd5eSJames Smart #endif
129101649561SJames Smart 
129201649561SJames Smart 	/*
129301649561SJames Smart 	 * Store the data needed by the driver to issue, abort, and complete
129401649561SJames Smart 	 * an IO.
129501649561SJames Smart 	 * Do not let the IO hang out forever.  There is no midlayer issuing
129601649561SJames Smart 	 * an abort so inform the FW of the maximum IO pending time.
129701649561SJames Smart 	 */
1298bbe3012bSJames Smart 	freqpriv->nvme_buf = lpfc_ncmd;
129901649561SJames Smart 	lpfc_ncmd->nvmeCmd = pnvme_fcreq;
130001649561SJames Smart 	lpfc_ncmd->nrport = rport;
1301318083adSJames Smart 	lpfc_ncmd->ndlp = ndlp;
130201649561SJames Smart 	lpfc_ncmd->start_time = jiffies;
130301649561SJames Smart 
130401649561SJames Smart 	lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp);
130501649561SJames Smart 	ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
130601649561SJames Smart 	if (ret) {
130701649561SJames Smart 		ret = -ENOMEM;
130801649561SJames Smart 		goto out_free_nvme_buf;
130901649561SJames Smart 	}
131001649561SJames Smart 
131101649561SJames Smart 	atomic_inc(&ndlp->cmd_pending);
131201649561SJames Smart 
131301649561SJames Smart 	/*
131401649561SJames Smart 	 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
131501649561SJames Smart 	 * This identfier was create in our hardware queue create callback
131601649561SJames Smart 	 * routine. The driver now is dependent on the IO queue steering from
131701649561SJames Smart 	 * the transport.  We are trusting the upper NVME layers know which
131801649561SJames Smart 	 * index to use and that they have affinitized a CPU to this hardware
131901649561SJames Smart 	 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
132001649561SJames Smart 	 */
132101649561SJames Smart 	lpfc_ncmd->cur_iocbq.hba_wqidx = lpfc_queue_info->index;
132201649561SJames Smart 
1323bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1324bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
1325bd2cdd5eSJames Smart 			 lpfc_queue_info->index, ndlp->nlp_DID);
1326bd2cdd5eSJames Smart 
132701649561SJames Smart 	ret = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, &lpfc_ncmd->cur_iocbq);
132801649561SJames Smart 	if (ret) {
132901649561SJames Smart 		atomic_dec(&ndlp->cmd_pending);
133001649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
133101649561SJames Smart 				 "6113 FCP could not issue WQE err %x "
133201649561SJames Smart 				 "sid: x%x did: x%x oxid: x%x\n",
133301649561SJames Smart 				 ret, vport->fc_myDID, ndlp->nlp_DID,
133401649561SJames Smart 				 lpfc_ncmd->cur_iocbq.sli4_xritag);
1335a5068b46SJames Smart 		ret = -EBUSY;
133601649561SJames Smart 		goto out_free_nvme_buf;
133701649561SJames Smart 	}
133801649561SJames Smart 
1339bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1340bd2cdd5eSJames Smart 	if (phba->ktime_on)
1341bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1342bd2cdd5eSJames Smart 
1343bd2cdd5eSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1344bd2cdd5eSJames Smart 		lpfc_ncmd->cpu = smp_processor_id();
1345bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu != lpfc_queue_info->index) {
1346bd2cdd5eSJames Smart 			/* Check for admin queue */
1347bd2cdd5eSJames Smart 			if (lpfc_queue_info->qidx) {
1348bd2cdd5eSJames Smart 				lpfc_printf_vlog(vport,
1349bd2cdd5eSJames Smart 						 KERN_ERR, LOG_NVME_IOERR,
1350bd2cdd5eSJames Smart 						"6702 CPU Check cmd: "
1351bd2cdd5eSJames Smart 						"cpu %d wq %d\n",
1352bd2cdd5eSJames Smart 						lpfc_ncmd->cpu,
1353bd2cdd5eSJames Smart 						lpfc_queue_info->index);
1354bd2cdd5eSJames Smart 			}
1355bd2cdd5eSJames Smart 			lpfc_ncmd->cpu = lpfc_queue_info->index;
1356bd2cdd5eSJames Smart 		}
1357bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1358bd2cdd5eSJames Smart 			phba->cpucheck_xmt_io[lpfc_ncmd->cpu]++;
1359bd2cdd5eSJames Smart 	}
1360bd2cdd5eSJames Smart #endif
136101649561SJames Smart 	return 0;
136201649561SJames Smart 
136301649561SJames Smart  out_free_nvme_buf:
13642cee7808SJames Smart 	if (lpfc_ncmd->nvmeCmd->sg_cnt) {
13652cee7808SJames Smart 		if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
13662cee7808SJames Smart 			atomic_dec(&phba->fc4NvmeOutputRequests);
13672cee7808SJames Smart 		else
13682cee7808SJames Smart 			atomic_dec(&phba->fc4NvmeInputRequests);
13692cee7808SJames Smart 	} else
13702cee7808SJames Smart 		atomic_dec(&phba->fc4NvmeControlRequests);
137101649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
137201649561SJames Smart  out_fail:
137301649561SJames Smart 	return ret;
137401649561SJames Smart }
137501649561SJames Smart 
137601649561SJames Smart /**
137701649561SJames Smart  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
137801649561SJames Smart  * @phba: Pointer to HBA context object
137901649561SJames Smart  * @cmdiocb: Pointer to command iocb object.
138001649561SJames Smart  * @rspiocb: Pointer to response iocb object.
138101649561SJames Smart  *
138201649561SJames Smart  * This is the callback function for any NVME FCP IO that was aborted.
138301649561SJames Smart  *
138401649561SJames Smart  * Return value:
138501649561SJames Smart  *   None
138601649561SJames Smart  **/
138701649561SJames Smart void
138801649561SJames Smart lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
138901649561SJames Smart 			   struct lpfc_wcqe_complete *abts_cmpl)
139001649561SJames Smart {
139101649561SJames Smart 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
139201649561SJames Smart 			"6145 ABORT_XRI_CN completing on rpi x%x "
139301649561SJames Smart 			"original iotag x%x, abort cmd iotag x%x "
139401649561SJames Smart 			"req_tag x%x, status x%x, hwstatus x%x\n",
139501649561SJames Smart 			cmdiocb->iocb.un.acxri.abortContextTag,
139601649561SJames Smart 			cmdiocb->iocb.un.acxri.abortIoTag,
139701649561SJames Smart 			cmdiocb->iotag,
139801649561SJames Smart 			bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
139901649561SJames Smart 			bf_get(lpfc_wcqe_c_status, abts_cmpl),
140001649561SJames Smart 			bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
140101649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocb);
140201649561SJames Smart }
140301649561SJames Smart 
140401649561SJames Smart /**
140501649561SJames Smart  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
140601649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
140701649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
140801649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
140901649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
141001649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
141101649561SJames Smart  *
141201649561SJames Smart  * Driver registers this routine as its nvme request io abort handler.  This
141301649561SJames Smart  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
141401649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
141501649561SJames Smart  * is executed asynchronously - one the target is validated as "MAPPED" and
141601649561SJames Smart  * ready for IO, the driver issues the abort request and returns.
141701649561SJames Smart  *
141801649561SJames Smart  * Return value:
141901649561SJames Smart  *   None
142001649561SJames Smart  **/
142101649561SJames Smart static void
142201649561SJames Smart lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
142301649561SJames Smart 		    struct nvme_fc_remote_port *pnvme_rport,
142401649561SJames Smart 		    void *hw_queue_handle,
142501649561SJames Smart 		    struct nvmefc_fcp_req *pnvme_fcreq)
142601649561SJames Smart {
142701649561SJames Smart 	struct lpfc_nvme_lport *lport;
142801649561SJames Smart 	struct lpfc_vport *vport;
142901649561SJames Smart 	struct lpfc_hba *phba;
143001649561SJames Smart 	struct lpfc_nvme_rport *rport;
143101649561SJames Smart 	struct lpfc_nvme_buf *lpfc_nbuf;
143201649561SJames Smart 	struct lpfc_iocbq *abts_buf;
143301649561SJames Smart 	struct lpfc_iocbq *nvmereq_wqe;
1434bbe3012bSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv = pnvme_fcreq->private;
143501649561SJames Smart 	union lpfc_wqe *abts_wqe;
143601649561SJames Smart 	unsigned long flags;
143701649561SJames Smart 	int ret_val;
143801649561SJames Smart 
143901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
144001649561SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
144101649561SJames Smart 	vport = lport->vport;
144201649561SJames Smart 	phba = vport->phba;
144301649561SJames Smart 
144401649561SJames Smart 	/* Announce entry to new IO submit field. */
144586c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
144601649561SJames Smart 			 "6002 Abort Request to rport DID x%06x "
144701649561SJames Smart 			 "for nvme_fc_req %p\n",
144801649561SJames Smart 			 pnvme_rport->port_id,
144901649561SJames Smart 			 pnvme_fcreq);
145001649561SJames Smart 
145101649561SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
145201649561SJames Smart 	 * cleared when the reset is complete and rings reestablished.
145301649561SJames Smart 	 */
145401649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
145501649561SJames Smart 	/* driver queued commands are in process of being flushed */
145601649561SJames Smart 	if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
145701649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
145886c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
145901649561SJames Smart 				 "6139 Driver in reset cleanup - flushing "
146001649561SJames Smart 				 "NVME Req now.  hba_flag x%x\n",
146101649561SJames Smart 				 phba->hba_flag);
146201649561SJames Smart 		return;
146301649561SJames Smart 	}
146401649561SJames Smart 
1465bbe3012bSJames Smart 	lpfc_nbuf = freqpriv->nvme_buf;
146601649561SJames Smart 	if (!lpfc_nbuf) {
146701649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
146886c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
146901649561SJames Smart 				 "6140 NVME IO req has no matching lpfc nvme "
147001649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
147101649561SJames Smart 		return;
147201649561SJames Smart 	} else if (!lpfc_nbuf->nvmeCmd) {
147301649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
147486c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
147501649561SJames Smart 				 "6141 lpfc NVME IO req has no nvme_fcreq "
147601649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
147701649561SJames Smart 		return;
147801649561SJames Smart 	}
14792b7824d0SJames Smart 	nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
148001649561SJames Smart 
148101649561SJames Smart 	/*
148201649561SJames Smart 	 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
148301649561SJames Smart 	 * state must match the nvme_fcreq passed by the nvme
148401649561SJames Smart 	 * transport.  If they don't match, it is likely the driver
148501649561SJames Smart 	 * has already completed the NVME IO and the nvme transport
148601649561SJames Smart 	 * has not seen it yet.
148701649561SJames Smart 	 */
148801649561SJames Smart 	if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
148901649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
149086c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
149101649561SJames Smart 				 "6143 NVME req mismatch: "
149201649561SJames Smart 				 "lpfc_nbuf %p nvmeCmd %p, "
14932b7824d0SJames Smart 				 "pnvme_fcreq %p.  Skipping Abort xri x%x\n",
149401649561SJames Smart 				 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
14952b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
149601649561SJames Smart 		return;
149701649561SJames Smart 	}
149801649561SJames Smart 
149901649561SJames Smart 	/* Don't abort IOs no longer on the pending queue. */
150001649561SJames Smart 	if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
150101649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
150286c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
150301649561SJames Smart 				 "6142 NVME IO req %p not queued - skipping "
15042b7824d0SJames Smart 				 "abort req xri x%x\n",
15052b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
150601649561SJames Smart 		return;
150701649561SJames Smart 	}
150801649561SJames Smart 
1509bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1510bd2cdd5eSJames Smart 			 nvmereq_wqe->sli4_xritag,
1511*00cefeb9SJames Smart 			 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1512bd2cdd5eSJames Smart 
151301649561SJames Smart 	/* Outstanding abort is in progress */
151401649561SJames Smart 	if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
151501649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
151686c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
151701649561SJames Smart 				 "6144 Outstanding NVME I/O Abort Request "
151801649561SJames Smart 				 "still pending on nvme_fcreq %p, "
15192b7824d0SJames Smart 				 "lpfc_ncmd %p xri x%x\n",
15202b7824d0SJames Smart 				 pnvme_fcreq, lpfc_nbuf,
15212b7824d0SJames Smart 				 nvmereq_wqe->sli4_xritag);
152201649561SJames Smart 		return;
152301649561SJames Smart 	}
152401649561SJames Smart 
152501649561SJames Smart 	abts_buf = __lpfc_sli_get_iocbq(phba);
152601649561SJames Smart 	if (!abts_buf) {
152701649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
152886c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
152901649561SJames Smart 				 "6136 No available abort wqes. Skipping "
15302b7824d0SJames Smart 				 "Abts req for nvme_fcreq %p xri x%x\n",
15312b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
153201649561SJames Smart 		return;
153301649561SJames Smart 	}
153401649561SJames Smart 
153501649561SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
153601649561SJames Smart 	nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
153701649561SJames Smart 
153801649561SJames Smart 	/* Complete prepping the abort wqe and issue to the FW. */
153901649561SJames Smart 	abts_wqe = &abts_buf->wqe;
154001649561SJames Smart 
154101649561SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
154201649561SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
154301649561SJames Smart 	 */
154401649561SJames Smart 	memset(abts_wqe, 0, sizeof(union lpfc_wqe));
154501649561SJames Smart 	bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
154601649561SJames Smart 
154701649561SJames Smart 	/* word 7 */
154801649561SJames Smart 	bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
154901649561SJames Smart 	bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
155001649561SJames Smart 	bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
155101649561SJames Smart 	       nvmereq_wqe->iocb.ulpClass);
155201649561SJames Smart 
155301649561SJames Smart 	/* word 8 - tell the FW to abort the IO associated with this
155401649561SJames Smart 	 * outstanding exchange ID.
155501649561SJames Smart 	 */
155601649561SJames Smart 	abts_wqe->abort_cmd.wqe_com.abort_tag = nvmereq_wqe->sli4_xritag;
155701649561SJames Smart 
155801649561SJames Smart 	/* word 9 - this is the iotag for the abts_wqe completion. */
155901649561SJames Smart 	bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
156001649561SJames Smart 	       abts_buf->iotag);
156101649561SJames Smart 
156201649561SJames Smart 	/* word 10 */
156301649561SJames Smart 	bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, nvmereq_wqe->hba_wqidx);
156401649561SJames Smart 	bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
156501649561SJames Smart 	bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
156601649561SJames Smart 
156701649561SJames Smart 	/* word 11 */
156801649561SJames Smart 	bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
156901649561SJames Smart 	bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
157001649561SJames Smart 	bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
157101649561SJames Smart 
157201649561SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
157301649561SJames Smart 	abts_buf->iocb_flag |= LPFC_IO_NVME;
157401649561SJames Smart 	abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
157501649561SJames Smart 	abts_buf->vport = vport;
157601649561SJames Smart 	abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
157701649561SJames Smart 	ret_val = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_buf);
157801649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
157901649561SJames Smart 	if (ret_val == IOCB_ERROR) {
158086c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
158101649561SJames Smart 				 "6137 Failed abts issue_wqe with status x%x "
158201649561SJames Smart 				 "for nvme_fcreq %p.\n",
158301649561SJames Smart 				 ret_val, pnvme_fcreq);
158401649561SJames Smart 		lpfc_sli_release_iocbq(phba, abts_buf);
158501649561SJames Smart 		return;
158601649561SJames Smart 	}
158701649561SJames Smart 
158886c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
15892b7824d0SJames Smart 			 "6138 Transport Abort NVME Request Issued for "
159001649561SJames Smart 			 "ox_id x%x on reqtag x%x\n",
159101649561SJames Smart 			 nvmereq_wqe->sli4_xritag,
159201649561SJames Smart 			 abts_buf->iotag);
159301649561SJames Smart }
159401649561SJames Smart 
159501649561SJames Smart /* Declare and initialization an instance of the FC NVME template. */
159601649561SJames Smart static struct nvme_fc_port_template lpfc_nvme_template = {
159701649561SJames Smart 	/* initiator-based functions */
159801649561SJames Smart 	.localport_delete  = lpfc_nvme_localport_delete,
159901649561SJames Smart 	.remoteport_delete = lpfc_nvme_remoteport_delete,
160001649561SJames Smart 	.create_queue = lpfc_nvme_create_queue,
160101649561SJames Smart 	.delete_queue = lpfc_nvme_delete_queue,
160201649561SJames Smart 	.ls_req       = lpfc_nvme_ls_req,
160301649561SJames Smart 	.fcp_io       = lpfc_nvme_fcp_io_submit,
160401649561SJames Smart 	.ls_abort     = lpfc_nvme_ls_abort,
160501649561SJames Smart 	.fcp_abort    = lpfc_nvme_fcp_abort,
160601649561SJames Smart 
160701649561SJames Smart 	.max_hw_queues = 1,
160801649561SJames Smart 	.max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
160901649561SJames Smart 	.max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
161001649561SJames Smart 	.dma_boundary = 0xFFFFFFFF,
161101649561SJames Smart 
161201649561SJames Smart 	/* Sizes of additional private data for data structures.
161301649561SJames Smart 	 * No use for the last two sizes at this time.
161401649561SJames Smart 	 */
161501649561SJames Smart 	.local_priv_sz = sizeof(struct lpfc_nvme_lport),
161601649561SJames Smart 	.remote_priv_sz = sizeof(struct lpfc_nvme_rport),
161701649561SJames Smart 	.lsrqst_priv_sz = 0,
1618bbe3012bSJames Smart 	.fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
161901649561SJames Smart };
162001649561SJames Smart 
162101649561SJames Smart /**
162201649561SJames Smart  * lpfc_sli4_post_nvme_sgl_block - post a block of nvme sgl list to firmware
162301649561SJames Smart  * @phba: pointer to lpfc hba data structure.
162401649561SJames Smart  * @nblist: pointer to nvme buffer list.
162501649561SJames Smart  * @count: number of scsi buffers on the list.
162601649561SJames Smart  *
162701649561SJames Smart  * This routine is invoked to post a block of @count scsi sgl pages from a
162801649561SJames Smart  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
162901649561SJames Smart  * No Lock is held.
163001649561SJames Smart  *
163101649561SJames Smart  **/
163201649561SJames Smart static int
163301649561SJames Smart lpfc_sli4_post_nvme_sgl_block(struct lpfc_hba *phba,
163401649561SJames Smart 			      struct list_head *nblist,
163501649561SJames Smart 			      int count)
163601649561SJames Smart {
163701649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd;
163801649561SJames Smart 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
163901649561SJames Smart 	struct sgl_page_pairs *sgl_pg_pairs;
164001649561SJames Smart 	void *viraddr;
164101649561SJames Smart 	LPFC_MBOXQ_t *mbox;
164201649561SJames Smart 	uint32_t reqlen, alloclen, pg_pairs;
164301649561SJames Smart 	uint32_t mbox_tmo;
164401649561SJames Smart 	uint16_t xritag_start = 0;
164501649561SJames Smart 	int rc = 0;
164601649561SJames Smart 	uint32_t shdr_status, shdr_add_status;
164701649561SJames Smart 	dma_addr_t pdma_phys_bpl1;
164801649561SJames Smart 	union lpfc_sli4_cfg_shdr *shdr;
164901649561SJames Smart 
165001649561SJames Smart 	/* Calculate the requested length of the dma memory */
165101649561SJames Smart 	reqlen = count * sizeof(struct sgl_page_pairs) +
165201649561SJames Smart 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
165301649561SJames Smart 	if (reqlen > SLI4_PAGE_SIZE) {
165401649561SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
165501649561SJames Smart 				"6118 Block sgl registration required DMA "
165601649561SJames Smart 				"size (%d) great than a page\n", reqlen);
165701649561SJames Smart 		return -ENOMEM;
165801649561SJames Smart 	}
165901649561SJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
166001649561SJames Smart 	if (!mbox) {
166101649561SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
166201649561SJames Smart 				"6119 Failed to allocate mbox cmd memory\n");
166301649561SJames Smart 		return -ENOMEM;
166401649561SJames Smart 	}
166501649561SJames Smart 
166601649561SJames Smart 	/* Allocate DMA memory and set up the non-embedded mailbox command */
166701649561SJames Smart 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
166801649561SJames Smart 				LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
166901649561SJames Smart 				LPFC_SLI4_MBX_NEMBED);
167001649561SJames Smart 
167101649561SJames Smart 	if (alloclen < reqlen) {
167201649561SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
167301649561SJames Smart 				"6120 Allocated DMA memory size (%d) is "
167401649561SJames Smart 				"less than the requested DMA memory "
167501649561SJames Smart 				"size (%d)\n", alloclen, reqlen);
167601649561SJames Smart 		lpfc_sli4_mbox_cmd_free(phba, mbox);
167701649561SJames Smart 		return -ENOMEM;
167801649561SJames Smart 	}
167901649561SJames Smart 
168001649561SJames Smart 	/* Get the first SGE entry from the non-embedded DMA memory */
168101649561SJames Smart 	viraddr = mbox->sge_array->addr[0];
168201649561SJames Smart 
168301649561SJames Smart 	/* Set up the SGL pages in the non-embedded DMA pages */
168401649561SJames Smart 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
168501649561SJames Smart 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
168601649561SJames Smart 
168701649561SJames Smart 	pg_pairs = 0;
168801649561SJames Smart 	list_for_each_entry(lpfc_ncmd, nblist, list) {
168901649561SJames Smart 		/* Set up the sge entry */
169001649561SJames Smart 		sgl_pg_pairs->sgl_pg0_addr_lo =
169101649561SJames Smart 			cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
169201649561SJames Smart 		sgl_pg_pairs->sgl_pg0_addr_hi =
169301649561SJames Smart 			cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
169401649561SJames Smart 		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
169501649561SJames Smart 			pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
169601649561SJames Smart 						SGL_PAGE_SIZE;
169701649561SJames Smart 		else
169801649561SJames Smart 			pdma_phys_bpl1 = 0;
169901649561SJames Smart 		sgl_pg_pairs->sgl_pg1_addr_lo =
170001649561SJames Smart 			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
170101649561SJames Smart 		sgl_pg_pairs->sgl_pg1_addr_hi =
170201649561SJames Smart 			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
170301649561SJames Smart 		/* Keep the first xritag on the list */
170401649561SJames Smart 		if (pg_pairs == 0)
170501649561SJames Smart 			xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
170601649561SJames Smart 		sgl_pg_pairs++;
170701649561SJames Smart 		pg_pairs++;
170801649561SJames Smart 	}
170901649561SJames Smart 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
171001649561SJames Smart 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
171101649561SJames Smart 	/* Perform endian conversion if necessary */
171201649561SJames Smart 	sgl->word0 = cpu_to_le32(sgl->word0);
171301649561SJames Smart 
171401649561SJames Smart 	if (!phba->sli4_hba.intr_enable)
171501649561SJames Smart 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
171601649561SJames Smart 	else {
171701649561SJames Smart 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
171801649561SJames Smart 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
171901649561SJames Smart 	}
172001649561SJames Smart 	shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
172101649561SJames Smart 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
172201649561SJames Smart 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
172301649561SJames Smart 	if (rc != MBX_TIMEOUT)
172401649561SJames Smart 		lpfc_sli4_mbox_cmd_free(phba, mbox);
172501649561SJames Smart 	if (shdr_status || shdr_add_status || rc) {
172601649561SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
172701649561SJames Smart 				"6125 POST_SGL_BLOCK mailbox command failed "
172801649561SJames Smart 				"status x%x add_status x%x mbx status x%x\n",
172901649561SJames Smart 				shdr_status, shdr_add_status, rc);
173001649561SJames Smart 		rc = -ENXIO;
173101649561SJames Smart 	}
173201649561SJames Smart 	return rc;
173301649561SJames Smart }
173401649561SJames Smart 
173501649561SJames Smart /**
173601649561SJames Smart  * lpfc_post_nvme_sgl_list - Post blocks of nvme buffer sgls from a list
173701649561SJames Smart  * @phba: pointer to lpfc hba data structure.
173801649561SJames Smart  * @post_nblist: pointer to the nvme buffer list.
173901649561SJames Smart  *
174001649561SJames Smart  * This routine walks a list of nvme buffers that was passed in. It attempts
174101649561SJames Smart  * to construct blocks of nvme buffer sgls which contains contiguous xris and
174201649561SJames Smart  * uses the non-embedded SGL block post mailbox commands to post to the port.
174301649561SJames Smart  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
174401649561SJames Smart  * embedded SGL post mailbox command for posting. The @post_nblist passed in
174501649561SJames Smart  * must be local list, thus no lock is needed when manipulate the list.
174601649561SJames Smart  *
174701649561SJames Smart  * Returns: 0 = failure, non-zero number of successfully posted buffers.
174801649561SJames Smart  **/
174901649561SJames Smart static int
175001649561SJames Smart lpfc_post_nvme_sgl_list(struct lpfc_hba *phba,
175101649561SJames Smart 			     struct list_head *post_nblist, int sb_count)
175201649561SJames Smart {
175301649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
175401649561SJames Smart 	int status, sgl_size;
175501649561SJames Smart 	int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
175601649561SJames Smart 	dma_addr_t pdma_phys_sgl1;
175701649561SJames Smart 	int last_xritag = NO_XRI;
175801649561SJames Smart 	int cur_xritag;
175901649561SJames Smart 	LIST_HEAD(prep_nblist);
176001649561SJames Smart 	LIST_HEAD(blck_nblist);
176101649561SJames Smart 	LIST_HEAD(nvme_nblist);
176201649561SJames Smart 
176301649561SJames Smart 	/* sanity check */
176401649561SJames Smart 	if (sb_count <= 0)
176501649561SJames Smart 		return -EINVAL;
176601649561SJames Smart 
176701649561SJames Smart 	sgl_size = phba->cfg_sg_dma_buf_size;
176801649561SJames Smart 
176901649561SJames Smart 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
177001649561SJames Smart 		list_del_init(&lpfc_ncmd->list);
177101649561SJames Smart 		block_cnt++;
177201649561SJames Smart 		if ((last_xritag != NO_XRI) &&
177301649561SJames Smart 		    (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
177401649561SJames Smart 			/* a hole in xri block, form a sgl posting block */
177501649561SJames Smart 			list_splice_init(&prep_nblist, &blck_nblist);
177601649561SJames Smart 			post_cnt = block_cnt - 1;
177701649561SJames Smart 			/* prepare list for next posting block */
177801649561SJames Smart 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
177901649561SJames Smart 			block_cnt = 1;
178001649561SJames Smart 		} else {
178101649561SJames Smart 			/* prepare list for next posting block */
178201649561SJames Smart 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
178301649561SJames Smart 			/* enough sgls for non-embed sgl mbox command */
178401649561SJames Smart 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
178501649561SJames Smart 				list_splice_init(&prep_nblist, &blck_nblist);
178601649561SJames Smart 				post_cnt = block_cnt;
178701649561SJames Smart 				block_cnt = 0;
178801649561SJames Smart 			}
178901649561SJames Smart 		}
179001649561SJames Smart 		num_posting++;
179101649561SJames Smart 		last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
179201649561SJames Smart 
179301649561SJames Smart 		/* end of repost sgl list condition for NVME buffers */
179401649561SJames Smart 		if (num_posting == sb_count) {
179501649561SJames Smart 			if (post_cnt == 0) {
179601649561SJames Smart 				/* last sgl posting block */
179701649561SJames Smart 				list_splice_init(&prep_nblist, &blck_nblist);
179801649561SJames Smart 				post_cnt = block_cnt;
179901649561SJames Smart 			} else if (block_cnt == 1) {
180001649561SJames Smart 				/* last single sgl with non-contiguous xri */
180101649561SJames Smart 				if (sgl_size > SGL_PAGE_SIZE)
180201649561SJames Smart 					pdma_phys_sgl1 =
180301649561SJames Smart 						lpfc_ncmd->dma_phys_sgl +
180401649561SJames Smart 						SGL_PAGE_SIZE;
180501649561SJames Smart 				else
180601649561SJames Smart 					pdma_phys_sgl1 = 0;
180701649561SJames Smart 				cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
180801649561SJames Smart 				status = lpfc_sli4_post_sgl(phba,
180901649561SJames Smart 						lpfc_ncmd->dma_phys_sgl,
181001649561SJames Smart 						pdma_phys_sgl1, cur_xritag);
181101649561SJames Smart 				if (status) {
181201649561SJames Smart 					/* failure, put on abort nvme list */
1813318083adSJames Smart 					lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
181401649561SJames Smart 				} else {
181501649561SJames Smart 					/* success, put on NVME buffer list */
1816318083adSJames Smart 					lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
181701649561SJames Smart 					lpfc_ncmd->status = IOSTAT_SUCCESS;
181801649561SJames Smart 					num_posted++;
181901649561SJames Smart 				}
182001649561SJames Smart 				/* success, put on NVME buffer sgl list */
182101649561SJames Smart 				list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
182201649561SJames Smart 			}
182301649561SJames Smart 		}
182401649561SJames Smart 
182501649561SJames Smart 		/* continue until a nembed page worth of sgls */
182601649561SJames Smart 		if (post_cnt == 0)
182701649561SJames Smart 			continue;
182801649561SJames Smart 
182901649561SJames Smart 		/* post block of NVME buffer list sgls */
183001649561SJames Smart 		status = lpfc_sli4_post_nvme_sgl_block(phba, &blck_nblist,
183101649561SJames Smart 						       post_cnt);
183201649561SJames Smart 
183301649561SJames Smart 		/* don't reset xirtag due to hole in xri block */
183401649561SJames Smart 		if (block_cnt == 0)
183501649561SJames Smart 			last_xritag = NO_XRI;
183601649561SJames Smart 
183701649561SJames Smart 		/* reset NVME buffer post count for next round of posting */
183801649561SJames Smart 		post_cnt = 0;
183901649561SJames Smart 
184001649561SJames Smart 		/* put posted NVME buffer-sgl posted on NVME buffer sgl list */
184101649561SJames Smart 		while (!list_empty(&blck_nblist)) {
184201649561SJames Smart 			list_remove_head(&blck_nblist, lpfc_ncmd,
184301649561SJames Smart 					 struct lpfc_nvme_buf, list);
184401649561SJames Smart 			if (status) {
184501649561SJames Smart 				/* failure, put on abort nvme list */
1846318083adSJames Smart 				lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
184701649561SJames Smart 			} else {
184801649561SJames Smart 				/* success, put on NVME buffer list */
1849318083adSJames Smart 				lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
185001649561SJames Smart 				lpfc_ncmd->status = IOSTAT_SUCCESS;
185101649561SJames Smart 				num_posted++;
185201649561SJames Smart 			}
185301649561SJames Smart 			list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
185401649561SJames Smart 		}
185501649561SJames Smart 	}
185601649561SJames Smart 	/* Push NVME buffers with sgl posted to the available list */
185701649561SJames Smart 	while (!list_empty(&nvme_nblist)) {
185801649561SJames Smart 		list_remove_head(&nvme_nblist, lpfc_ncmd,
185901649561SJames Smart 				 struct lpfc_nvme_buf, list);
186001649561SJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
186101649561SJames Smart 	}
186201649561SJames Smart 	return num_posted;
186301649561SJames Smart }
186401649561SJames Smart 
186501649561SJames Smart /**
186601649561SJames Smart  * lpfc_repost_nvme_sgl_list - Repost all the allocated nvme buffer sgls
186701649561SJames Smart  * @phba: pointer to lpfc hba data structure.
186801649561SJames Smart  *
186901649561SJames Smart  * This routine walks the list of nvme buffers that have been allocated and
187001649561SJames Smart  * repost them to the port by using SGL block post. This is needed after a
187101649561SJames Smart  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
187201649561SJames Smart  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
187301649561SJames Smart  * to the lpfc_nvme_buf_list. If the repost fails, reject all nvme buffers.
187401649561SJames Smart  *
187501649561SJames Smart  * Returns: 0 = success, non-zero failure.
187601649561SJames Smart  **/
187701649561SJames Smart int
187801649561SJames Smart lpfc_repost_nvme_sgl_list(struct lpfc_hba *phba)
187901649561SJames Smart {
188001649561SJames Smart 	LIST_HEAD(post_nblist);
188101649561SJames Smart 	int num_posted, rc = 0;
188201649561SJames Smart 
188301649561SJames Smart 	/* get all NVME buffers need to repost to a local list */
188401649561SJames Smart 	spin_lock_irq(&phba->nvme_buf_list_get_lock);
188501649561SJames Smart 	spin_lock(&phba->nvme_buf_list_put_lock);
188601649561SJames Smart 	list_splice_init(&phba->lpfc_nvme_buf_list_get, &post_nblist);
188701649561SJames Smart 	list_splice(&phba->lpfc_nvme_buf_list_put, &post_nblist);
188801649561SJames Smart 	spin_unlock(&phba->nvme_buf_list_put_lock);
188901649561SJames Smart 	spin_unlock_irq(&phba->nvme_buf_list_get_lock);
189001649561SJames Smart 
189101649561SJames Smart 	/* post the list of nvme buffer sgls to port if available */
189201649561SJames Smart 	if (!list_empty(&post_nblist)) {
189301649561SJames Smart 		num_posted = lpfc_post_nvme_sgl_list(phba, &post_nblist,
189401649561SJames Smart 						phba->sli4_hba.nvme_xri_cnt);
189501649561SJames Smart 		/* failed to post any nvme buffer, return error */
189601649561SJames Smart 		if (num_posted == 0)
189701649561SJames Smart 			rc = -EIO;
189801649561SJames Smart 	}
189901649561SJames Smart 	return rc;
190001649561SJames Smart }
190101649561SJames Smart 
190201649561SJames Smart /**
190301649561SJames Smart  * lpfc_new_nvme_buf - Scsi buffer allocator for HBA with SLI4 IF spec
190401649561SJames Smart  * @vport: The virtual port for which this call being executed.
190501649561SJames Smart  * @num_to_allocate: The requested number of buffers to allocate.
190601649561SJames Smart  *
190701649561SJames Smart  * This routine allocates nvme buffers for device with SLI-4 interface spec,
190801649561SJames Smart  * the nvme buffer contains all the necessary information needed to initiate
190901649561SJames Smart  * a NVME I/O. After allocating up to @num_to_allocate NVME buffers and put
191001649561SJames Smart  * them on a list, it post them to the port by using SGL block post.
191101649561SJames Smart  *
191201649561SJames Smart  * Return codes:
191301649561SJames Smart  *   int - number of nvme buffers that were allocated and posted.
191401649561SJames Smart  *   0 = failure, less than num_to_alloc is a partial failure.
191501649561SJames Smart  **/
191601649561SJames Smart static int
191701649561SJames Smart lpfc_new_nvme_buf(struct lpfc_vport *vport, int num_to_alloc)
191801649561SJames Smart {
191901649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
192001649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd;
192101649561SJames Smart 	struct lpfc_iocbq *pwqeq;
192201649561SJames Smart 	union lpfc_wqe128 *wqe;
192301649561SJames Smart 	struct sli4_sge *sgl;
192401649561SJames Smart 	dma_addr_t pdma_phys_sgl;
192501649561SJames Smart 	uint16_t iotag, lxri = 0;
192601649561SJames Smart 	int bcnt, num_posted, sgl_size;
192701649561SJames Smart 	LIST_HEAD(prep_nblist);
192801649561SJames Smart 	LIST_HEAD(post_nblist);
192901649561SJames Smart 	LIST_HEAD(nvme_nblist);
193001649561SJames Smart 
193101649561SJames Smart 	sgl_size = phba->cfg_sg_dma_buf_size;
193201649561SJames Smart 
193301649561SJames Smart 	for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
193401649561SJames Smart 		lpfc_ncmd = kzalloc(sizeof(struct lpfc_nvme_buf), GFP_KERNEL);
193501649561SJames Smart 		if (!lpfc_ncmd)
193601649561SJames Smart 			break;
193701649561SJames Smart 		/*
193801649561SJames Smart 		 * Get memory from the pci pool to map the virt space to
193901649561SJames Smart 		 * pci bus space for an I/O. The DMA buffer includes the
194001649561SJames Smart 		 * number of SGE's necessary to support the sg_tablesize.
194101649561SJames Smart 		 */
194201649561SJames Smart 		lpfc_ncmd->data = pci_pool_alloc(phba->lpfc_sg_dma_buf_pool,
194301649561SJames Smart 						 GFP_KERNEL,
194401649561SJames Smart 						 &lpfc_ncmd->dma_handle);
194501649561SJames Smart 		if (!lpfc_ncmd->data) {
194601649561SJames Smart 			kfree(lpfc_ncmd);
194701649561SJames Smart 			break;
194801649561SJames Smart 		}
194901649561SJames Smart 		memset(lpfc_ncmd->data, 0, phba->cfg_sg_dma_buf_size);
195001649561SJames Smart 
195101649561SJames Smart 		lxri = lpfc_sli4_next_xritag(phba);
195201649561SJames Smart 		if (lxri == NO_XRI) {
195301649561SJames Smart 			pci_pool_free(phba->lpfc_sg_dma_buf_pool,
195401649561SJames Smart 				      lpfc_ncmd->data, lpfc_ncmd->dma_handle);
195501649561SJames Smart 			kfree(lpfc_ncmd);
195601649561SJames Smart 			break;
195701649561SJames Smart 		}
195801649561SJames Smart 		pwqeq = &(lpfc_ncmd->cur_iocbq);
195901649561SJames Smart 		wqe = (union lpfc_wqe128 *)&pwqeq->wqe;
196001649561SJames Smart 
196101649561SJames Smart 		/* Allocate iotag for lpfc_ncmd->cur_iocbq. */
196201649561SJames Smart 		iotag = lpfc_sli_next_iotag(phba, pwqeq);
196301649561SJames Smart 		if (iotag == 0) {
196401649561SJames Smart 			pci_pool_free(phba->lpfc_sg_dma_buf_pool,
196501649561SJames Smart 				      lpfc_ncmd->data, lpfc_ncmd->dma_handle);
196601649561SJames Smart 			kfree(lpfc_ncmd);
196701649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
196801649561SJames Smart 					"6121 Failed to allocated IOTAG for"
196901649561SJames Smart 					" XRI:0x%x\n", lxri);
197001649561SJames Smart 			lpfc_sli4_free_xri(phba, lxri);
197101649561SJames Smart 			break;
197201649561SJames Smart 		}
197301649561SJames Smart 		pwqeq->sli4_lxritag = lxri;
197401649561SJames Smart 		pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
197501649561SJames Smart 		pwqeq->iocb_flag |= LPFC_IO_NVME;
197601649561SJames Smart 		pwqeq->context1 = lpfc_ncmd;
197701649561SJames Smart 		pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
197801649561SJames Smart 
197901649561SJames Smart 		/* Initialize local short-hand pointers. */
198001649561SJames Smart 		lpfc_ncmd->nvme_sgl = lpfc_ncmd->data;
198101649561SJames Smart 		sgl = lpfc_ncmd->nvme_sgl;
198201649561SJames Smart 		pdma_phys_sgl = lpfc_ncmd->dma_handle;
198301649561SJames Smart 		lpfc_ncmd->dma_phys_sgl = pdma_phys_sgl;
198401649561SJames Smart 
198501649561SJames Smart 		/* Rsp SGE will be filled in when we rcv an IO
198601649561SJames Smart 		 * from the NVME Layer to be sent.
198701649561SJames Smart 		 * The cmd is going to be embedded so we need a SKIP SGE.
198801649561SJames Smart 		 */
198901649561SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
199001649561SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
199101649561SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
199201649561SJames Smart 		/* Fill in word 3 / sgl_len during cmd submission */
199301649561SJames Smart 
199401649561SJames Smart 		lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
199501649561SJames Smart 
199601649561SJames Smart 		/* Word 7 */
199701649561SJames Smart 		bf_set(wqe_erp, &wqe->generic.wqe_com, 0);
199801649561SJames Smart 		/* NVME upper layers will time things out, if needed */
199901649561SJames Smart 		bf_set(wqe_tmo, &wqe->generic.wqe_com, 0);
200001649561SJames Smart 
200101649561SJames Smart 		/* Word 10 */
200201649561SJames Smart 		bf_set(wqe_ebde_cnt, &wqe->generic.wqe_com, 0);
200301649561SJames Smart 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
200401649561SJames Smart 
200501649561SJames Smart 		/* add the nvme buffer to a post list */
200601649561SJames Smart 		list_add_tail(&lpfc_ncmd->list, &post_nblist);
200701649561SJames Smart 		spin_lock_irq(&phba->nvme_buf_list_get_lock);
200801649561SJames Smart 		phba->sli4_hba.nvme_xri_cnt++;
200901649561SJames Smart 		spin_unlock_irq(&phba->nvme_buf_list_get_lock);
201001649561SJames Smart 	}
201101649561SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
201201649561SJames Smart 			"6114 Allocate %d out of %d requested new NVME "
201301649561SJames Smart 			"buffers\n", bcnt, num_to_alloc);
201401649561SJames Smart 
201501649561SJames Smart 	/* post the list of nvme buffer sgls to port if available */
201601649561SJames Smart 	if (!list_empty(&post_nblist))
201701649561SJames Smart 		num_posted = lpfc_post_nvme_sgl_list(phba,
201801649561SJames Smart 						     &post_nblist, bcnt);
201901649561SJames Smart 	else
202001649561SJames Smart 		num_posted = 0;
202101649561SJames Smart 
202201649561SJames Smart 	return num_posted;
202301649561SJames Smart }
202401649561SJames Smart 
202501649561SJames Smart /**
202601649561SJames Smart  * lpfc_get_nvme_buf - Get a nvme buffer from lpfc_nvme_buf_list of the HBA
202701649561SJames Smart  * @phba: The HBA for which this call is being executed.
202801649561SJames Smart  *
202901649561SJames Smart  * This routine removes a nvme buffer from head of @phba lpfc_nvme_buf_list list
203001649561SJames Smart  * and returns to caller.
203101649561SJames Smart  *
203201649561SJames Smart  * Return codes:
203301649561SJames Smart  *   NULL - Error
203401649561SJames Smart  *   Pointer to lpfc_nvme_buf - Success
203501649561SJames Smart  **/
203601649561SJames Smart static struct lpfc_nvme_buf *
203701649561SJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
203801649561SJames Smart {
203901649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
204001649561SJames Smart 	unsigned long iflag = 0;
204101649561SJames Smart 	int found = 0;
204201649561SJames Smart 
204301649561SJames Smart 	spin_lock_irqsave(&phba->nvme_buf_list_get_lock, iflag);
204401649561SJames Smart 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
204501649561SJames Smart 				 &phba->lpfc_nvme_buf_list_get, list) {
204601649561SJames Smart 		if (lpfc_test_rrq_active(phba, ndlp,
204701649561SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_lxritag))
204801649561SJames Smart 			continue;
2049bbe3012bSJames Smart 		list_del_init(&lpfc_ncmd->list);
205001649561SJames Smart 		found = 1;
205101649561SJames Smart 		break;
205201649561SJames Smart 	}
205301649561SJames Smart 	if (!found) {
205401649561SJames Smart 		spin_lock(&phba->nvme_buf_list_put_lock);
205501649561SJames Smart 		list_splice(&phba->lpfc_nvme_buf_list_put,
205601649561SJames Smart 			    &phba->lpfc_nvme_buf_list_get);
205701649561SJames Smart 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
205801649561SJames Smart 		spin_unlock(&phba->nvme_buf_list_put_lock);
205901649561SJames Smart 		list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
206001649561SJames Smart 					 &phba->lpfc_nvme_buf_list_get, list) {
206101649561SJames Smart 			if (lpfc_test_rrq_active(
206201649561SJames Smart 				phba, ndlp, lpfc_ncmd->cur_iocbq.sli4_lxritag))
206301649561SJames Smart 				continue;
2064bbe3012bSJames Smart 			list_del_init(&lpfc_ncmd->list);
206501649561SJames Smart 			found = 1;
206601649561SJames Smart 			break;
206701649561SJames Smart 		}
206801649561SJames Smart 	}
206901649561SJames Smart 	spin_unlock_irqrestore(&phba->nvme_buf_list_get_lock, iflag);
207001649561SJames Smart 	if (!found)
207101649561SJames Smart 		return NULL;
207201649561SJames Smart 	return  lpfc_ncmd;
207301649561SJames Smart }
207401649561SJames Smart 
207501649561SJames Smart /**
207601649561SJames Smart  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
207701649561SJames Smart  * @phba: The Hba for which this call is being executed.
207801649561SJames Smart  * @lpfc_ncmd: The nvme buffer which is being released.
207901649561SJames Smart  *
208001649561SJames Smart  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
208101649561SJames Smart  * lpfc_nvme_buf_list list. For SLI4 XRI's are tied to the nvme buffer
208201649561SJames Smart  * and cannot be reused for at least RA_TOV amount of time if it was
208301649561SJames Smart  * aborted.
208401649561SJames Smart  **/
208501649561SJames Smart static void
208601649561SJames Smart lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_nvme_buf *lpfc_ncmd)
208701649561SJames Smart {
208801649561SJames Smart 	unsigned long iflag = 0;
208901649561SJames Smart 
209001649561SJames Smart 	lpfc_ncmd->nonsg_phys = 0;
2091318083adSJames Smart 	if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY) {
209286c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
209386c67379SJames Smart 				"6310 XB release deferred for "
209486c67379SJames Smart 				"ox_id x%x on reqtag x%x\n",
209586c67379SJames Smart 				lpfc_ncmd->cur_iocbq.sli4_xritag,
209686c67379SJames Smart 				lpfc_ncmd->cur_iocbq.iotag);
209786c67379SJames Smart 
209801649561SJames Smart 		spin_lock_irqsave(&phba->sli4_hba.abts_nvme_buf_list_lock,
209901649561SJames Smart 					iflag);
210001649561SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
210101649561SJames Smart 		list_add_tail(&lpfc_ncmd->list,
210201649561SJames Smart 			&phba->sli4_hba.lpfc_abts_nvme_buf_list);
210301649561SJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock,
210401649561SJames Smart 					iflag);
210501649561SJames Smart 	} else {
210601649561SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
210701649561SJames Smart 		lpfc_ncmd->cur_iocbq.iocb_flag = LPFC_IO_NVME;
210801649561SJames Smart 		spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
210901649561SJames Smart 		list_add_tail(&lpfc_ncmd->list, &phba->lpfc_nvme_buf_list_put);
211001649561SJames Smart 		spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
211101649561SJames Smart 	}
211201649561SJames Smart }
211301649561SJames Smart 
211401649561SJames Smart /**
211501649561SJames Smart  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
211601649561SJames Smart  * @pvport - the lpfc_vport instance requesting a localport.
211701649561SJames Smart  *
211801649561SJames Smart  * This routine is invoked to create an nvme localport instance to bind
211901649561SJames Smart  * to the nvme_fc_transport.  It is called once during driver load
212001649561SJames Smart  * like lpfc_create_shost after all other services are initialized.
212101649561SJames Smart  * It requires a vport, vpi, and wwns at call time.  Other localport
212201649561SJames Smart  * parameters are modified as the driver's FCID and the Fabric WWN
212301649561SJames Smart  * are established.
212401649561SJames Smart  *
212501649561SJames Smart  * Return codes
212601649561SJames Smart  *      0 - successful
212701649561SJames Smart  *      -ENOMEM - no heap memory available
212801649561SJames Smart  *      other values - from nvme registration upcall
212901649561SJames Smart  **/
213001649561SJames Smart int
213101649561SJames Smart lpfc_nvme_create_localport(struct lpfc_vport *vport)
213201649561SJames Smart {
2133166d7211SJames Smart 	int ret = 0;
213401649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
213501649561SJames Smart 	struct nvme_fc_port_info nfcp_info;
213601649561SJames Smart 	struct nvme_fc_local_port *localport;
213701649561SJames Smart 	struct lpfc_nvme_lport *lport;
2138166d7211SJames Smart 	int len;
213901649561SJames Smart 
214001649561SJames Smart 	/* Initialize this localport instance.  The vport wwn usage ensures
214101649561SJames Smart 	 * that NPIV is accounted for.
214201649561SJames Smart 	 */
214301649561SJames Smart 	memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
214401649561SJames Smart 	nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
214501649561SJames Smart 	nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
214601649561SJames Smart 	nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
214701649561SJames Smart 
21484d4c4a4aSJames Smart 	/* Limit to LPFC_MAX_NVME_SEG_CNT.
21494d4c4a4aSJames Smart 	 * For now need + 1 to get around NVME transport logic.
21504d4c4a4aSJames Smart 	 */
21514d4c4a4aSJames Smart 	if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
21524d4c4a4aSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_INIT,
21534d4c4a4aSJames Smart 				 "6300 Reducing sg segment cnt to %d\n",
21544d4c4a4aSJames Smart 				 LPFC_MAX_NVME_SEG_CNT);
21554d4c4a4aSJames Smart 		phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
21564d4c4a4aSJames Smart 	} else {
21574d4c4a4aSJames Smart 		phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
21584d4c4a4aSJames Smart 	}
21594d4c4a4aSJames Smart 	lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
216001649561SJames Smart 	lpfc_nvme_template.max_hw_queues = phba->cfg_nvme_io_channel;
216101649561SJames Smart 
216201649561SJames Smart 	/* localport is allocated from the stack, but the registration
216301649561SJames Smart 	 * call allocates heap memory as well as the private area.
216401649561SJames Smart 	 */
21657d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
216601649561SJames Smart 	ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
216701649561SJames Smart 					 &vport->phba->pcidev->dev, &localport);
2168166d7211SJames Smart #else
2169166d7211SJames Smart 	ret = -ENOMEM;
2170166d7211SJames Smart #endif
217101649561SJames Smart 	if (!ret) {
217201649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
217301649561SJames Smart 				 "6005 Successfully registered local "
217401649561SJames Smart 				 "NVME port num %d, localP %p, private %p, "
217501649561SJames Smart 				 "sg_seg %d\n",
217601649561SJames Smart 				 localport->port_num, localport,
217701649561SJames Smart 				 localport->private,
217801649561SJames Smart 				 lpfc_nvme_template.max_sgl_segments);
217901649561SJames Smart 
218001649561SJames Smart 		/* Private is our lport size declared in the template. */
218101649561SJames Smart 		lport = (struct lpfc_nvme_lport *)localport->private;
218201649561SJames Smart 		vport->localport = localport;
218301649561SJames Smart 		lport->vport = vport;
218401649561SJames Smart 		vport->nvmei_support = 1;
218501649561SJames Smart 		len  = lpfc_new_nvme_buf(vport, phba->sli4_hba.nvme_xri_max);
218601649561SJames Smart 		vport->phba->total_nvme_bufs += len;
21873ebd9b47SJames Smart 	}
21883ebd9b47SJames Smart 
218901649561SJames Smart 	return ret;
219001649561SJames Smart }
219101649561SJames Smart 
219201649561SJames Smart /**
219301649561SJames Smart  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
219401649561SJames Smart  * @pnvme: pointer to lpfc nvme data structure.
219501649561SJames Smart  *
219601649561SJames Smart  * This routine is invoked to destroy all lports bound to the phba.
219701649561SJames Smart  * The lport memory was allocated by the nvme fc transport and is
219801649561SJames Smart  * released there.  This routine ensures all rports bound to the
219901649561SJames Smart  * lport have been disconnected.
220001649561SJames Smart  *
220101649561SJames Smart  **/
220201649561SJames Smart void
220301649561SJames Smart lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
220401649561SJames Smart {
22057d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
220601649561SJames Smart 	struct nvme_fc_local_port *localport;
220701649561SJames Smart 	struct lpfc_nvme_lport *lport;
220801649561SJames Smart 	int ret;
220901649561SJames Smart 
221001649561SJames Smart 	if (vport->nvmei_support == 0)
221101649561SJames Smart 		return;
221201649561SJames Smart 
221301649561SJames Smart 	localport = vport->localport;
221401649561SJames Smart 	vport->localport = NULL;
221501649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
221601649561SJames Smart 
221701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
221801649561SJames Smart 			 "6011 Destroying NVME localport %p\n",
221901649561SJames Smart 			 localport);
2220166d7211SJames Smart 
222101649561SJames Smart 	/* lport's rport list is clear.  Unregister
222201649561SJames Smart 	 * lport and release resources.
222301649561SJames Smart 	 */
222401649561SJames Smart 	init_completion(&lport->lport_unreg_done);
222501649561SJames Smart 	ret = nvme_fc_unregister_localport(localport);
222601649561SJames Smart 	wait_for_completion_timeout(&lport->lport_unreg_done, 5);
222701649561SJames Smart 
222801649561SJames Smart 	/* Regardless of the unregister upcall response, clear
222901649561SJames Smart 	 * nvmei_support.  All rports are unregistered and the
223001649561SJames Smart 	 * driver will clean up.
223101649561SJames Smart 	 */
223201649561SJames Smart 	vport->nvmei_support = 0;
223301649561SJames Smart 	if (ret == 0) {
223401649561SJames Smart 		lpfc_printf_vlog(vport,
223501649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
223601649561SJames Smart 				 "6009 Unregistered lport Success\n");
223701649561SJames Smart 	} else {
223801649561SJames Smart 		lpfc_printf_vlog(vport,
223901649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
224001649561SJames Smart 				 "6010 Unregistered lport "
224101649561SJames Smart 				 "Failed, status x%x\n",
224201649561SJames Smart 				 ret);
224301649561SJames Smart 	}
2244166d7211SJames Smart #endif
224501649561SJames Smart }
224601649561SJames Smart 
224701649561SJames Smart void
224801649561SJames Smart lpfc_nvme_update_localport(struct lpfc_vport *vport)
224901649561SJames Smart {
22504410a67aSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
225101649561SJames Smart 	struct nvme_fc_local_port *localport;
225201649561SJames Smart 	struct lpfc_nvme_lport *lport;
225301649561SJames Smart 
225401649561SJames Smart 	localport = vport->localport;
22554410a67aSJames Smart 	if (!localport) {
22564410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
22574410a67aSJames Smart 				 "6710 Update NVME fail. No localport\n");
22584410a67aSJames Smart 		return;
22594410a67aSJames Smart 	}
226001649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
22614410a67aSJames Smart 	if (!lport) {
22624410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
22634410a67aSJames Smart 				 "6171 Update NVME fail. localP %p, No lport\n",
22644410a67aSJames Smart 				 localport);
22654410a67aSJames Smart 		return;
22664410a67aSJames Smart 	}
226701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
226801649561SJames Smart 			 "6012 Update NVME lport %p did x%x\n",
226901649561SJames Smart 			 localport, vport->fc_myDID);
227001649561SJames Smart 
227101649561SJames Smart 	localport->port_id = vport->fc_myDID;
227201649561SJames Smart 	if (localport->port_id == 0)
227301649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
227401649561SJames Smart 	else
227501649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
227601649561SJames Smart 
227701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
227801649561SJames Smart 			 "6030 bound lport %p to DID x%06x\n",
227901649561SJames Smart 			 lport, localport->port_id);
22804410a67aSJames Smart #endif
228101649561SJames Smart }
228201649561SJames Smart 
228301649561SJames Smart int
228401649561SJames Smart lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
228501649561SJames Smart {
22867d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
228701649561SJames Smart 	int ret = 0;
228801649561SJames Smart 	struct nvme_fc_local_port *localport;
228901649561SJames Smart 	struct lpfc_nvme_lport *lport;
229001649561SJames Smart 	struct lpfc_nvme_rport *rport;
229101649561SJames Smart 	struct nvme_fc_remote_port *remote_port;
229201649561SJames Smart 	struct nvme_fc_port_info rpinfo;
229301649561SJames Smart 
229401649561SJames Smart 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
229501649561SJames Smart 			 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
229601649561SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type);
229701649561SJames Smart 
229801649561SJames Smart 	localport = vport->localport;
229901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
230001649561SJames Smart 
23017a06dcd3SJames Smart 	/* NVME rports are not preserved across devloss.
23027a06dcd3SJames Smart 	 * Just register this instance.  Note, rpinfo->dev_loss_tmo
23037a06dcd3SJames Smart 	 * is left 0 to indicate accept transport defaults.  The
23047a06dcd3SJames Smart 	 * driver communicates port role capabilities consistent
23057a06dcd3SJames Smart 	 * with the PRLI response data.
230601649561SJames Smart 	 */
23077a06dcd3SJames Smart 	memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
23087a06dcd3SJames Smart 	rpinfo.port_id = ndlp->nlp_DID;
230901649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET)
23107a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
231101649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
23127a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
231301649561SJames Smart 
23147a06dcd3SJames Smart 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
23157a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
23167a06dcd3SJames Smart 
23177a06dcd3SJames Smart 	rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
23187a06dcd3SJames Smart 	rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
23197a06dcd3SJames Smart 	ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
23207a06dcd3SJames Smart 	if (!ret) {
23217a06dcd3SJames Smart 		/* If the ndlp already has an nrport, this is just
23227a06dcd3SJames Smart 		 * a resume of the existing rport.  Else this is a
23237a06dcd3SJames Smart 		 * new rport.
23247a06dcd3SJames Smart 		 */
23257a06dcd3SJames Smart 		rport = remote_port->private;
23267a06dcd3SJames Smart 		if (ndlp->nrport == rport) {
232701649561SJames Smart 			lpfc_printf_vlog(ndlp->vport, KERN_INFO,
232801649561SJames Smart 					 LOG_NVME_DISC,
232901649561SJames Smart 					 "6014 Rebinding lport to "
233001649561SJames Smart 					 "rport wwpn 0x%llx, "
233101649561SJames Smart 					 "Data: x%x x%x x%x x%06x\n",
233201649561SJames Smart 					 remote_port->port_name,
233301649561SJames Smart 					 remote_port->port_id,
233401649561SJames Smart 					 remote_port->port_role,
233501649561SJames Smart 					 ndlp->nlp_type,
233601649561SJames Smart 					 ndlp->nlp_DID);
23377a06dcd3SJames Smart 		} else {
23387a06dcd3SJames Smart 			/* New rport. */
233901649561SJames Smart 			rport->remoteport = remote_port;
234001649561SJames Smart 			rport->lport = lport;
234101649561SJames Smart 			rport->ndlp = lpfc_nlp_get(ndlp);
234201649561SJames Smart 			if (!rport->ndlp)
234301649561SJames Smart 				return -1;
234401649561SJames Smart 			ndlp->nrport = rport;
234501649561SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO,
234601649561SJames Smart 					 LOG_NVME_DISC | LOG_NODE,
23477a06dcd3SJames Smart 					 "6022 Binding new rport to "
23487a06dcd3SJames Smart 					 "lport %p Rport WWNN 0x%llx, "
23497a06dcd3SJames Smart 					 "Rport WWPN 0x%llx DID "
23507a06dcd3SJames Smart 					 "x%06x Role x%x\n",
235101649561SJames Smart 					 lport,
235201649561SJames Smart 					 rpinfo.node_name, rpinfo.port_name,
235301649561SJames Smart 					 rpinfo.port_id, rpinfo.port_role);
23547a06dcd3SJames Smart 		}
235501649561SJames Smart 	} else {
235601649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR,
235701649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
235801649561SJames Smart 				 "6031 RemotePort Registration failed "
235901649561SJames Smart 				 "err: %d, DID x%06x\n",
236001649561SJames Smart 				 ret, ndlp->nlp_DID);
236101649561SJames Smart 	}
23627a06dcd3SJames Smart 
236301649561SJames Smart 	return ret;
2364166d7211SJames Smart #else
2365166d7211SJames Smart 	return 0;
2366166d7211SJames Smart #endif
236701649561SJames Smart }
236801649561SJames Smart 
236901649561SJames Smart /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
237001649561SJames Smart  *
237101649561SJames Smart  * There is no notion of Devloss or rport recovery from the current
237201649561SJames Smart  * nvme_transport perspective.  Loss of an rport just means IO cannot
237301649561SJames Smart  * be sent and recovery is completely up to the initator.
237401649561SJames Smart  * For now, the driver just unbinds the DID and port_role so that
237501649561SJames Smart  * no further IO can be issued.  Changes are planned for later.
237601649561SJames Smart  *
237701649561SJames Smart  * Notes - the ndlp reference count is not decremented here since
237801649561SJames Smart  * since there is no nvme_transport api for devloss.  Node ref count
237901649561SJames Smart  * is only adjusted in driver unload.
238001649561SJames Smart  */
238101649561SJames Smart void
238201649561SJames Smart lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
238301649561SJames Smart {
23847d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
238501649561SJames Smart 	int ret;
238601649561SJames Smart 	struct nvme_fc_local_port *localport;
238701649561SJames Smart 	struct lpfc_nvme_lport *lport;
238801649561SJames Smart 	struct lpfc_nvme_rport *rport;
238901649561SJames Smart 	struct nvme_fc_remote_port *remoteport;
239001649561SJames Smart 
239101649561SJames Smart 	localport = vport->localport;
239201649561SJames Smart 
239301649561SJames Smart 	/* This is fundamental error.  The localport is always
239401649561SJames Smart 	 * available until driver unload.  Just exit.
239501649561SJames Smart 	 */
239601649561SJames Smart 	if (!localport)
239701649561SJames Smart 		return;
239801649561SJames Smart 
239901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
240001649561SJames Smart 	if (!lport)
240101649561SJames Smart 		goto input_err;
240201649561SJames Smart 
240301649561SJames Smart 	rport = ndlp->nrport;
240401649561SJames Smart 	if (!rport)
240501649561SJames Smart 		goto input_err;
240601649561SJames Smart 
240701649561SJames Smart 	remoteport = rport->remoteport;
240801649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
240901649561SJames Smart 			 "6033 Unreg nvme remoteport %p, portname x%llx, "
241001649561SJames Smart 			 "port_id x%06x, portstate x%x port type x%x\n",
241101649561SJames Smart 			 remoteport, remoteport->port_name,
241201649561SJames Smart 			 remoteport->port_id, remoteport->port_state,
241301649561SJames Smart 			 ndlp->nlp_type);
241401649561SJames Smart 
241501649561SJames Smart 	/* Sanity check ndlp type.  Only call for NVME ports. Don't
241601649561SJames Smart 	 * clear any rport state until the transport calls back.
241701649561SJames Smart 	 */
241801649561SJames Smart 	if (ndlp->nlp_type & (NLP_NVME_TARGET | NLP_NVME_INITIATOR)) {
241901649561SJames Smart 		init_completion(&rport->rport_unreg_done);
24207a06dcd3SJames Smart 
24217a06dcd3SJames Smart 		/* No concern about the role change on the nvme remoteport.
24227a06dcd3SJames Smart 		 * The transport will update it.
24237a06dcd3SJames Smart 		 */
242401649561SJames Smart 		ret = nvme_fc_unregister_remoteport(remoteport);
242501649561SJames Smart 		if (ret != 0) {
242601649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
242701649561SJames Smart 					 "6167 NVME unregister failed %d "
242801649561SJames Smart 					 "port_state x%x\n",
242901649561SJames Smart 					 ret, remoteport->port_state);
243001649561SJames Smart 		}
243101649561SJames Smart 
243201649561SJames Smart 	}
243301649561SJames Smart 	return;
243401649561SJames Smart 
243501649561SJames Smart  input_err:
2436166d7211SJames Smart #endif
243701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
24382b7824d0SJames Smart 			 "6168 State error: lport %p, rport%p FCID x%06x\n",
243901649561SJames Smart 			 vport->localport, ndlp->rport, ndlp->nlp_DID);
244001649561SJames Smart }
2441318083adSJames Smart 
2442318083adSJames Smart /**
2443318083adSJames Smart  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2444318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
2445318083adSJames Smart  * @axri: pointer to the fcp xri abort wcqe structure.
2446318083adSJames Smart  *
2447318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
2448318083adSJames Smart  * FCP aborted xri.
2449318083adSJames Smart  **/
2450318083adSJames Smart void
2451318083adSJames Smart lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2452318083adSJames Smart 			   struct sli4_wcqe_xri_aborted *axri)
2453318083adSJames Smart {
2454318083adSJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2455318083adSJames Smart 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
2456318083adSJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd, *next_lpfc_ncmd;
2457318083adSJames Smart 	struct lpfc_nodelist *ndlp;
2458318083adSJames Smart 	unsigned long iflag = 0;
2459318083adSJames Smart 	int rrq_empty = 0;
2460318083adSJames Smart 
2461318083adSJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
2462318083adSJames Smart 		return;
2463318083adSJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
2464318083adSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2465318083adSJames Smart 	list_for_each_entry_safe(lpfc_ncmd, next_lpfc_ncmd,
2466318083adSJames Smart 				 &phba->sli4_hba.lpfc_abts_nvme_buf_list,
2467318083adSJames Smart 				 list) {
2468318083adSJames Smart 		if (lpfc_ncmd->cur_iocbq.sli4_xritag == xri) {
2469bbe3012bSJames Smart 			list_del_init(&lpfc_ncmd->list);
2470318083adSJames Smart 			lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
2471318083adSJames Smart 			lpfc_ncmd->status = IOSTAT_SUCCESS;
2472318083adSJames Smart 			spin_unlock(
2473318083adSJames Smart 				&phba->sli4_hba.abts_nvme_buf_list_lock);
2474318083adSJames Smart 
2475318083adSJames Smart 			rrq_empty = list_empty(&phba->active_rrq_list);
2476318083adSJames Smart 			spin_unlock_irqrestore(&phba->hbalock, iflag);
2477318083adSJames Smart 			ndlp = lpfc_ncmd->ndlp;
2478318083adSJames Smart 			if (ndlp) {
2479318083adSJames Smart 				lpfc_set_rrq_active(
2480318083adSJames Smart 					phba, ndlp,
2481318083adSJames Smart 					lpfc_ncmd->cur_iocbq.sli4_lxritag,
2482318083adSJames Smart 					rxid, 1);
2483318083adSJames Smart 				lpfc_sli4_abts_err_handler(phba, ndlp, axri);
2484318083adSJames Smart 			}
248586c67379SJames Smart 
248686c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
248786c67379SJames Smart 					"6311 XRI Aborted xri x%x tag x%x "
248886c67379SJames Smart 					"released\n",
248986c67379SJames Smart 					xri, lpfc_ncmd->cur_iocbq.iotag);
249086c67379SJames Smart 
2491318083adSJames Smart 			lpfc_release_nvme_buf(phba, lpfc_ncmd);
2492318083adSJames Smart 			if (rrq_empty)
2493318083adSJames Smart 				lpfc_worker_wake_up(phba);
2494318083adSJames Smart 			return;
2495318083adSJames Smart 		}
2496318083adSJames Smart 	}
2497318083adSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2498318083adSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
249986c67379SJames Smart 
250086c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
250186c67379SJames Smart 			"6312 XRI Aborted xri x%x not found\n", xri);
250286c67379SJames Smart 
2503318083adSJames Smart }
2504