xref: /linux/drivers/scsi/lpfc/lpfc_nvme.c (revision b15bd3e6212e747ebd1b37a542898e88ad05bb17)
101649561SJames Smart /*******************************************************************
201649561SJames Smart  * This file is part of the Emulex Linux Device Driver for         *
301649561SJames Smart  * Fibre Channel Host Bus Adapters.                                *
4128bddacSJames Smart  * Copyright (C) 2017-2018 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 *
60cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
61cf1a1d3eSJames Smart 		  int expedite);
6201649561SJames Smart 
6301649561SJames Smart static void
6401649561SJames Smart lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_nvme_buf *);
6501649561SJames Smart 
6681e6a637SJames Smart static struct nvme_fc_port_template lpfc_nvme_template;
6701649561SJames Smart 
68fab2e466SColin Ian King static union lpfc_wqe128 lpfc_iread_cmd_template;
69fab2e466SColin Ian King static union lpfc_wqe128 lpfc_iwrite_cmd_template;
70fab2e466SColin Ian King static union lpfc_wqe128 lpfc_icmnd_cmd_template;
715fd11085SJames Smart 
725fd11085SJames Smart /* Setup WQE templates for NVME IOs */
735fd11085SJames Smart void
74fab2e466SColin Ian King lpfc_nvme_cmd_template(void)
755fd11085SJames Smart {
765fd11085SJames Smart 	union lpfc_wqe128 *wqe;
775fd11085SJames Smart 
785fd11085SJames Smart 	/* IREAD template */
795fd11085SJames Smart 	wqe = &lpfc_iread_cmd_template;
805fd11085SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
815fd11085SJames Smart 
825fd11085SJames Smart 	/* Word 0, 1, 2 - BDE is variable */
835fd11085SJames Smart 
845fd11085SJames Smart 	/* Word 3 - cmd_buff_len, payload_offset_len is zero */
855fd11085SJames Smart 
865fd11085SJames Smart 	/* Word 4 - total_xfer_len is variable */
875fd11085SJames Smart 
885fd11085SJames Smart 	/* Word 5 - is zero */
895fd11085SJames Smart 
905fd11085SJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
915fd11085SJames Smart 
925fd11085SJames Smart 	/* Word 7 */
935fd11085SJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
945fd11085SJames Smart 	bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
955fd11085SJames Smart 	bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
965fd11085SJames Smart 	bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
975fd11085SJames Smart 
985fd11085SJames Smart 	/* Word 8 - abort_tag is variable */
995fd11085SJames Smart 
1005fd11085SJames Smart 	/* Word 9  - reqtag is variable */
1015fd11085SJames Smart 
1025fd11085SJames Smart 	/* Word 10 - dbde, wqes is variable */
1035fd11085SJames Smart 	bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
1045fd11085SJames Smart 	bf_set(wqe_nvme, &wqe->fcp_iread.wqe_com, 1);
1055fd11085SJames Smart 	bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
1065fd11085SJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
1075fd11085SJames Smart 	bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
1085fd11085SJames Smart 	bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
1095fd11085SJames Smart 
1105fd11085SJames Smart 	/* Word 11 - pbde is variable */
1115fd11085SJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, NVME_READ_CMD);
1125fd11085SJames Smart 	bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1135fd11085SJames Smart 	bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
1145fd11085SJames Smart 
1155fd11085SJames Smart 	/* Word 12 - is zero */
1165fd11085SJames Smart 
1175fd11085SJames Smart 	/* Word 13, 14, 15 - PBDE is variable */
1185fd11085SJames Smart 
1195fd11085SJames Smart 	/* IWRITE template */
1205fd11085SJames Smart 	wqe = &lpfc_iwrite_cmd_template;
1215fd11085SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
1225fd11085SJames Smart 
1235fd11085SJames Smart 	/* Word 0, 1, 2 - BDE is variable */
1245fd11085SJames Smart 
1255fd11085SJames Smart 	/* Word 3 - cmd_buff_len, payload_offset_len is zero */
1265fd11085SJames Smart 
1275fd11085SJames Smart 	/* Word 4 - total_xfer_len is variable */
1285fd11085SJames Smart 
1295fd11085SJames Smart 	/* Word 5 - initial_xfer_len is variable */
1305fd11085SJames Smart 
1315fd11085SJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
1325fd11085SJames Smart 
1335fd11085SJames Smart 	/* Word 7 */
1345fd11085SJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);
1355fd11085SJames Smart 	bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);
1365fd11085SJames Smart 	bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);
1375fd11085SJames Smart 	bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);
1385fd11085SJames Smart 
1395fd11085SJames Smart 	/* Word 8 - abort_tag is variable */
1405fd11085SJames Smart 
1415fd11085SJames Smart 	/* Word 9  - reqtag is variable */
1425fd11085SJames Smart 
1435fd11085SJames Smart 	/* Word 10 - dbde, wqes is variable */
1445fd11085SJames Smart 	bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
1455fd11085SJames Smart 	bf_set(wqe_nvme, &wqe->fcp_iwrite.wqe_com, 1);
1465fd11085SJames Smart 	bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
1475fd11085SJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);
1485fd11085SJames Smart 	bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
1495fd11085SJames Smart 	bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
1505fd11085SJames Smart 
1515fd11085SJames Smart 	/* Word 11 - pbde is variable */
1525fd11085SJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, NVME_WRITE_CMD);
1535fd11085SJames Smart 	bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1545fd11085SJames Smart 	bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
1555fd11085SJames Smart 
1565fd11085SJames Smart 	/* Word 12 - is zero */
1575fd11085SJames Smart 
1585fd11085SJames Smart 	/* Word 13, 14, 15 - PBDE is variable */
1595fd11085SJames Smart 
1605fd11085SJames Smart 	/* ICMND template */
1615fd11085SJames Smart 	wqe = &lpfc_icmnd_cmd_template;
1625fd11085SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe128));
1635fd11085SJames Smart 
1645fd11085SJames Smart 	/* Word 0, 1, 2 - BDE is variable */
1655fd11085SJames Smart 
1665fd11085SJames Smart 	/* Word 3 - payload_offset_len is variable */
1675fd11085SJames Smart 
1685fd11085SJames Smart 	/* Word 4, 5 - is zero */
1695fd11085SJames Smart 
1705fd11085SJames Smart 	/* Word 6 - ctxt_tag, xri_tag is variable */
1715fd11085SJames Smart 
1725fd11085SJames Smart 	/* Word 7 */
1735fd11085SJames Smart 	bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
1745fd11085SJames Smart 	bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
1755fd11085SJames Smart 	bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
1765fd11085SJames Smart 	bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
1775fd11085SJames Smart 
1785fd11085SJames Smart 	/* Word 8 - abort_tag is variable */
1795fd11085SJames Smart 
1805fd11085SJames Smart 	/* Word 9  - reqtag is variable */
1815fd11085SJames Smart 
1825fd11085SJames Smart 	/* Word 10 - dbde, wqes is variable */
1835fd11085SJames Smart 	bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
1845fd11085SJames Smart 	bf_set(wqe_nvme, &wqe->fcp_icmd.wqe_com, 1);
1855fd11085SJames Smart 	bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
1865fd11085SJames Smart 	bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
1875fd11085SJames Smart 	bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
1885fd11085SJames Smart 	bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
1895fd11085SJames Smart 
1905fd11085SJames Smart 	/* Word 11 */
1915fd11085SJames Smart 	bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, FCP_COMMAND);
1925fd11085SJames Smart 	bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1935fd11085SJames Smart 	bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);
1945fd11085SJames Smart 
1955fd11085SJames Smart 	/* Word 12, 13, 14, 15 - is zero */
1965fd11085SJames Smart }
1975fd11085SJames Smart 
19801649561SJames Smart /**
19901649561SJames Smart  * lpfc_nvme_create_queue -
20001649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
20101649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
20201649561SJames Smart  * @handle: An opaque driver handle used in follow-up calls.
20301649561SJames Smart  *
20401649561SJames Smart  * Driver registers this routine to preallocate and initialize any
20501649561SJames Smart  * internal data structures to bind the @qidx to its internal IO queues.
20601649561SJames Smart  * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
20701649561SJames Smart  *
20801649561SJames Smart  * Return value :
20901649561SJames Smart  *   0 - Success
21001649561SJames Smart  *   -EINVAL - Unsupported input value.
21101649561SJames Smart  *   -ENOMEM - Could not alloc necessary memory
21201649561SJames Smart  **/
21301649561SJames Smart static int
21401649561SJames Smart lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
21501649561SJames Smart 		       unsigned int qidx, u16 qsize,
21601649561SJames Smart 		       void **handle)
21701649561SJames Smart {
21801649561SJames Smart 	struct lpfc_nvme_lport *lport;
21901649561SJames Smart 	struct lpfc_vport *vport;
22001649561SJames Smart 	struct lpfc_nvme_qhandle *qhandle;
22101649561SJames Smart 	char *str;
22201649561SJames Smart 
223c3725bdcSJames Smart 	if (!pnvme_lport->private)
224c3725bdcSJames Smart 		return -ENOMEM;
225c3725bdcSJames Smart 
22601649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
22701649561SJames Smart 	vport = lport->vport;
22801649561SJames Smart 	qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
22901649561SJames Smart 	if (qhandle == NULL)
23001649561SJames Smart 		return -ENOMEM;
23101649561SJames Smart 
23201649561SJames Smart 	qhandle->cpu_id = smp_processor_id();
23301649561SJames Smart 	qhandle->qidx = qidx;
23401649561SJames Smart 	/*
23501649561SJames Smart 	 * NVME qidx == 0 is the admin queue, so both admin queue
23601649561SJames Smart 	 * and first IO queue will use MSI-X vector and associated
23701649561SJames Smart 	 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
23801649561SJames Smart 	 */
23901649561SJames Smart 	if (qidx) {
24001649561SJames Smart 		str = "IO ";  /* IO queue */
24101649561SJames Smart 		qhandle->index = ((qidx - 1) %
24201649561SJames Smart 			vport->phba->cfg_nvme_io_channel);
24301649561SJames Smart 	} else {
24401649561SJames Smart 		str = "ADM";  /* Admin queue */
24501649561SJames Smart 		qhandle->index = qidx;
24601649561SJames Smart 	}
24701649561SJames Smart 
248d58734f0SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
24901649561SJames Smart 			 "6073 Binding %s HdwQueue %d  (cpu %d) to "
25001649561SJames Smart 			 "io_channel %d qhandle %p\n", str,
25101649561SJames Smart 			 qidx, qhandle->cpu_id, qhandle->index, qhandle);
25201649561SJames Smart 	*handle = (void *)qhandle;
25301649561SJames Smart 	return 0;
25401649561SJames Smart }
25501649561SJames Smart 
25601649561SJames Smart /**
25701649561SJames Smart  * lpfc_nvme_delete_queue -
25801649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
25901649561SJames Smart  * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
26001649561SJames Smart  * @handle: An opaque driver handle from lpfc_nvme_create_queue
26101649561SJames Smart  *
26201649561SJames Smart  * Driver registers this routine to free
26301649561SJames Smart  * any internal data structures to bind the @qidx to its internal
26401649561SJames Smart  * IO queues.
26501649561SJames Smart  *
26601649561SJames Smart  * Return value :
26701649561SJames Smart  *   0 - Success
26801649561SJames Smart  *   TODO:  What are the failure codes.
26901649561SJames Smart  **/
27001649561SJames Smart static void
27101649561SJames Smart lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
27201649561SJames Smart 		       unsigned int qidx,
27301649561SJames Smart 		       void *handle)
27401649561SJames Smart {
27501649561SJames Smart 	struct lpfc_nvme_lport *lport;
27601649561SJames Smart 	struct lpfc_vport *vport;
27701649561SJames Smart 
278c3725bdcSJames Smart 	if (!pnvme_lport->private)
279c3725bdcSJames Smart 		return;
280c3725bdcSJames Smart 
28101649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
28201649561SJames Smart 	vport = lport->vport;
28301649561SJames Smart 
28401649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
28501649561SJames Smart 			"6001 ENTER.  lpfc_pnvme %p, qidx x%xi qhandle %p\n",
28601649561SJames Smart 			lport, qidx, handle);
28701649561SJames Smart 	kfree(handle);
28801649561SJames Smart }
28901649561SJames Smart 
29001649561SJames Smart static void
29101649561SJames Smart lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
29201649561SJames Smart {
29301649561SJames Smart 	struct lpfc_nvme_lport *lport = localport->private;
29401649561SJames Smart 
295add9d6beSJames Smart 	lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
296add9d6beSJames Smart 			 "6173 localport %p delete complete\n",
297add9d6beSJames Smart 			 lport);
298add9d6beSJames Smart 
29901649561SJames Smart 	/* release any threads waiting for the unreg to complete */
30001649561SJames Smart 	complete(&lport->lport_unreg_done);
30101649561SJames Smart }
30201649561SJames Smart 
30301649561SJames Smart /* lpfc_nvme_remoteport_delete
30401649561SJames Smart  *
30501649561SJames Smart  * @remoteport: Pointer to an nvme transport remoteport instance.
30601649561SJames Smart  *
30701649561SJames Smart  * This is a template downcall.  NVME transport calls this function
30801649561SJames Smart  * when it has completed the unregistration of a previously
30901649561SJames Smart  * registered remoteport.
31001649561SJames Smart  *
31101649561SJames Smart  * Return value :
31201649561SJames Smart  * None
31301649561SJames Smart  */
31401649561SJames Smart void
31501649561SJames Smart lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
31601649561SJames Smart {
31701649561SJames Smart 	struct lpfc_nvme_rport *rport = remoteport->private;
31801649561SJames Smart 	struct lpfc_vport *vport;
31901649561SJames Smart 	struct lpfc_nodelist *ndlp;
32001649561SJames Smart 
32101649561SJames Smart 	ndlp = rport->ndlp;
32201649561SJames Smart 	if (!ndlp)
32301649561SJames Smart 		goto rport_err;
32401649561SJames Smart 
32501649561SJames Smart 	vport = ndlp->vport;
32601649561SJames Smart 	if (!vport)
32701649561SJames Smart 		goto rport_err;
32801649561SJames Smart 
32901649561SJames Smart 	/* Remove this rport from the lport's list - memory is owned by the
33001649561SJames Smart 	 * transport. Remove the ndlp reference for the NVME transport before
3317a06dcd3SJames Smart 	 * calling state machine to remove the node.
33201649561SJames Smart 	 */
33301649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
3343fd78355SJames Smart 			"6146 remoteport delete of remoteport %p\n",
33501649561SJames Smart 			remoteport);
3363fd78355SJames Smart 	spin_lock_irq(&vport->phba->hbalock);
337*b15bd3e6SJames Smart 
338*b15bd3e6SJames Smart 	/* The register rebind might have occurred before the delete
339*b15bd3e6SJames Smart 	 * downcall.  Guard against this race.
340*b15bd3e6SJames Smart 	 */
341*b15bd3e6SJames Smart 	if (ndlp->upcall_flags & NLP_WAIT_FOR_UNREG) {
3427a06dcd3SJames Smart 		ndlp->nrport = NULL;
34301466024SJames Smart 		ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
344*b15bd3e6SJames Smart 	}
3453fd78355SJames Smart 	spin_unlock_irq(&vport->phba->hbalock);
3463fd78355SJames Smart 
3473fd78355SJames Smart 	/* Remove original register reference. The host transport
3483fd78355SJames Smart 	 * won't reference this rport/remoteport any further.
3493fd78355SJames Smart 	 */
35001649561SJames Smart 	lpfc_nlp_put(ndlp);
35101649561SJames Smart 
35201649561SJames Smart  rport_err:
3533fd78355SJames Smart 	return;
35401649561SJames Smart }
35501649561SJames Smart 
35601649561SJames Smart static void
35701649561SJames Smart lpfc_nvme_cmpl_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
35801649561SJames Smart 		       struct lpfc_wcqe_complete *wcqe)
35901649561SJames Smart {
36001649561SJames Smart 	struct lpfc_vport *vport = cmdwqe->vport;
3614b056682SJames Smart 	struct lpfc_nvme_lport *lport;
36201649561SJames Smart 	uint32_t status;
36301649561SJames Smart 	struct nvmefc_ls_req *pnvme_lsreq;
36401649561SJames Smart 	struct lpfc_dmabuf *buf_ptr;
36501649561SJames Smart 	struct lpfc_nodelist *ndlp;
36601649561SJames Smart 
36766a210ffSJames Smart 	lport = (struct lpfc_nvme_lport *)vport->localport->private;
36801649561SJames Smart 	pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
36901649561SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
37066a210ffSJames Smart 
37166a210ffSJames Smart 	if (lport) {
37266a210ffSJames Smart 		atomic_inc(&lport->fc4NvmeLsCmpls);
3734b056682SJames Smart 		if (status) {
3744b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
3754b056682SJames Smart 				atomic_inc(&lport->cmpl_ls_xb);
3764b056682SJames Smart 			atomic_inc(&lport->cmpl_ls_err);
3774b056682SJames Smart 		}
37866a210ffSJames Smart 	}
3794b056682SJames Smart 
38001649561SJames Smart 	ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
38101649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
38201649561SJames Smart 			 "6047 nvme cmpl Enter "
383815a9c43SJames Smart 			 "Data %p DID %x Xri: %x status %x reason x%x cmd:%p "
384815a9c43SJames Smart 			 "lsreg:%p bmp:%p ndlp:%p\n",
38501649561SJames Smart 			 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
38601649561SJames Smart 			 cmdwqe->sli4_xritag, status,
387815a9c43SJames Smart 			 (wcqe->parameter & 0xffff),
38801649561SJames Smart 			 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
38901649561SJames Smart 
390bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  CMPL: xri x%x stat x%x parm x%x\n",
391bd2cdd5eSJames Smart 			 cmdwqe->sli4_xritag, status, wcqe->parameter);
392bd2cdd5eSJames Smart 
39301649561SJames Smart 	if (cmdwqe->context3) {
39401649561SJames Smart 		buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
39501649561SJames Smart 		lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
39601649561SJames Smart 		kfree(buf_ptr);
39701649561SJames Smart 		cmdwqe->context3 = NULL;
39801649561SJames Smart 	}
39901649561SJames Smart 	if (pnvme_lsreq->done)
40001649561SJames Smart 		pnvme_lsreq->done(pnvme_lsreq, status);
40101649561SJames Smart 	else
40201649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
40301649561SJames Smart 				 "6046 nvme cmpl without done call back? "
40401649561SJames Smart 				 "Data %p DID %x Xri: %x status %x\n",
40501649561SJames Smart 				pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
40601649561SJames Smart 				cmdwqe->sli4_xritag, status);
40701649561SJames Smart 	if (ndlp) {
40801649561SJames Smart 		lpfc_nlp_put(ndlp);
40901649561SJames Smart 		cmdwqe->context1 = NULL;
41001649561SJames Smart 	}
41101649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdwqe);
41201649561SJames Smart }
41301649561SJames Smart 
41401649561SJames Smart static int
41501649561SJames Smart lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
41601649561SJames Smart 		  struct lpfc_dmabuf *inp,
41701649561SJames Smart 		  struct nvmefc_ls_req *pnvme_lsreq,
41801649561SJames Smart 		  void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
41901649561SJames Smart 			       struct lpfc_wcqe_complete *),
42001649561SJames Smart 		  struct lpfc_nodelist *ndlp, uint32_t num_entry,
42101649561SJames Smart 		  uint32_t tmo, uint8_t retry)
42201649561SJames Smart {
42301649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
424205e8240SJames Smart 	union lpfc_wqe128 *wqe;
42501649561SJames Smart 	struct lpfc_iocbq *genwqe;
42601649561SJames Smart 	struct ulp_bde64 *bpl;
42701649561SJames Smart 	struct ulp_bde64 bde;
42801649561SJames Smart 	int i, rc, xmit_len, first_len;
42901649561SJames Smart 
43001649561SJames Smart 	/* Allocate buffer for  command WQE */
43101649561SJames Smart 	genwqe = lpfc_sli_get_iocbq(phba);
43201649561SJames Smart 	if (genwqe == NULL)
43301649561SJames Smart 		return 1;
43401649561SJames Smart 
43501649561SJames Smart 	wqe = &genwqe->wqe;
43601649561SJames Smart 	memset(wqe, 0, sizeof(union lpfc_wqe));
43701649561SJames Smart 
43801649561SJames Smart 	genwqe->context3 = (uint8_t *)bmp;
43901649561SJames Smart 	genwqe->iocb_flag |= LPFC_IO_NVME_LS;
44001649561SJames Smart 
44101649561SJames Smart 	/* Save for completion so we can release these resources */
44201649561SJames Smart 	genwqe->context1 = lpfc_nlp_get(ndlp);
44301649561SJames Smart 	genwqe->context2 = (uint8_t *)pnvme_lsreq;
44401649561SJames Smart 	/* Fill in payload, bp points to frame payload */
44501649561SJames Smart 
44601649561SJames Smart 	if (!tmo)
44701649561SJames Smart 		/* FC spec states we need 3 * ratov for CT requests */
44801649561SJames Smart 		tmo = (3 * phba->fc_ratov);
44901649561SJames Smart 
45001649561SJames Smart 	/* For this command calculate the xmit length of the request bde. */
45101649561SJames Smart 	xmit_len = 0;
45201649561SJames Smart 	first_len = 0;
45301649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
45401649561SJames Smart 	for (i = 0; i < num_entry; i++) {
45501649561SJames Smart 		bde.tus.w = bpl[i].tus.w;
45601649561SJames Smart 		if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
45701649561SJames Smart 			break;
45801649561SJames Smart 		xmit_len += bde.tus.f.bdeSize;
45901649561SJames Smart 		if (i == 0)
46001649561SJames Smart 			first_len = xmit_len;
46101649561SJames Smart 	}
46201649561SJames Smart 
46301649561SJames Smart 	genwqe->rsvd2 = num_entry;
46401649561SJames Smart 	genwqe->hba_wqidx = 0;
46501649561SJames Smart 
46601649561SJames Smart 	/* Words 0 - 2 */
46701649561SJames Smart 	wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
46801649561SJames Smart 	wqe->generic.bde.tus.f.bdeSize = first_len;
46901649561SJames Smart 	wqe->generic.bde.addrLow = bpl[0].addrLow;
47001649561SJames Smart 	wqe->generic.bde.addrHigh = bpl[0].addrHigh;
47101649561SJames Smart 
47201649561SJames Smart 	/* Word 3 */
47301649561SJames Smart 	wqe->gen_req.request_payload_len = first_len;
47401649561SJames Smart 
47501649561SJames Smart 	/* Word 4 */
47601649561SJames Smart 
47701649561SJames Smart 	/* Word 5 */
47801649561SJames Smart 	bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
47901649561SJames Smart 	bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
48001649561SJames Smart 	bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
4818b361639SJames Smart 	bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
48201649561SJames Smart 	bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
48301649561SJames Smart 
48401649561SJames Smart 	/* Word 6 */
48501649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
48601649561SJames Smart 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
48701649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
48801649561SJames Smart 
48901649561SJames Smart 	/* Word 7 */
49001649561SJames Smart 	bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
49101649561SJames Smart 	bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
49201649561SJames Smart 	bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
49301649561SJames Smart 	bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
49401649561SJames Smart 
49501649561SJames Smart 	/* Word 8 */
49601649561SJames Smart 	wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
49701649561SJames Smart 
49801649561SJames Smart 	/* Word 9 */
49901649561SJames Smart 	bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
50001649561SJames Smart 
50101649561SJames Smart 	/* Word 10 */
50201649561SJames Smart 	bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
50301649561SJames Smart 	bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
50401649561SJames Smart 	bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
50501649561SJames Smart 	bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
50601649561SJames Smart 	bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
50701649561SJames Smart 
50801649561SJames Smart 	/* Word 11 */
50901649561SJames Smart 	bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
51001649561SJames Smart 	bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
51101649561SJames Smart 
51201649561SJames Smart 
51301649561SJames Smart 	/* Issue GEN REQ WQE for NPORT <did> */
51401649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
51501649561SJames Smart 			 "6050 Issue GEN REQ WQE to NPORT x%x "
51601649561SJames Smart 			 "Data: x%x x%x wq:%p lsreq:%p bmp:%p xmit:%d 1st:%d\n",
51701649561SJames Smart 			 ndlp->nlp_DID, genwqe->iotag,
51801649561SJames Smart 			 vport->port_state,
51901649561SJames Smart 			genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
52001649561SJames Smart 	genwqe->wqe_cmpl = cmpl;
52101649561SJames Smart 	genwqe->iocb_cmpl = NULL;
52201649561SJames Smart 	genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
52301649561SJames Smart 	genwqe->vport = vport;
52401649561SJames Smart 	genwqe->retry = retry;
52501649561SJames Smart 
526bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME LS  XMIT: xri x%x iotag x%x to x%06x\n",
527bd2cdd5eSJames Smart 			 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
528bd2cdd5eSJames Smart 
52901649561SJames Smart 	rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, genwqe);
530cd22d605SDick Kennedy 	if (rc) {
53101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
53201649561SJames Smart 				 "6045 Issue GEN REQ WQE to NPORT x%x "
53301649561SJames Smart 				 "Data: x%x x%x\n",
53401649561SJames Smart 				 ndlp->nlp_DID, genwqe->iotag,
53501649561SJames Smart 				 vport->port_state);
53601649561SJames Smart 		lpfc_sli_release_iocbq(phba, genwqe);
53701649561SJames Smart 		return 1;
53801649561SJames Smart 	}
53901649561SJames Smart 	return 0;
54001649561SJames Smart }
54101649561SJames Smart 
54201649561SJames Smart /**
54301649561SJames Smart  * lpfc_nvme_ls_req - Issue an Link Service request
54401649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
54501649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
54601649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
54701649561SJames Smart  *
54801649561SJames Smart  * Driver registers this routine to handle any link service request
54901649561SJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
55001649561SJames Smart  *
55101649561SJames Smart  * Return value :
55201649561SJames Smart  *   0 - Success
55301649561SJames Smart  *   TODO: What are the failure codes.
55401649561SJames Smart  **/
55501649561SJames Smart static int
55601649561SJames Smart lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
55701649561SJames Smart 		 struct nvme_fc_remote_port *pnvme_rport,
55801649561SJames Smart 		 struct nvmefc_ls_req *pnvme_lsreq)
55901649561SJames Smart {
56001649561SJames Smart 	int ret = 0;
56101649561SJames Smart 	struct lpfc_nvme_lport *lport;
562815a9c43SJames Smart 	struct lpfc_nvme_rport *rport;
56301649561SJames Smart 	struct lpfc_vport *vport;
56401649561SJames Smart 	struct lpfc_nodelist *ndlp;
56501649561SJames Smart 	struct ulp_bde64 *bpl;
56601649561SJames Smart 	struct lpfc_dmabuf *bmp;
567ba43c4d0SJames Smart 	uint16_t ntype, nstate;
56801649561SJames Smart 
56901649561SJames Smart 	/* there are two dma buf in the request, actually there is one and
57001649561SJames Smart 	 * the second one is just the start address + cmd size.
57101649561SJames Smart 	 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
57201649561SJames Smart 	 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
57301649561SJames Smart 	 * because the nvem layer owns the data bufs.
57401649561SJames Smart 	 * We do not have to break these packets open, we don't care what is in
57501649561SJames Smart 	 * them. And we do not have to look at the resonse data, we only care
57601649561SJames Smart 	 * that we got a response. All of the caring is going to happen in the
57701649561SJames Smart 	 * nvme-fc layer.
57801649561SJames Smart 	 */
57901649561SJames Smart 
58001649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
581815a9c43SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
58266a210ffSJames Smart 	if (unlikely(!lport) || unlikely(!rport))
58366a210ffSJames Smart 		return -EINVAL;
58466a210ffSJames Smart 
58501649561SJames Smart 	vport = lport->vport;
58601649561SJames Smart 
5871abcb371SDick Kennedy 	if (vport->load_flag & FC_UNLOADING)
5881abcb371SDick Kennedy 		return -ENODEV;
5891abcb371SDick Kennedy 
590815a9c43SJames Smart 	/* Need the ndlp.  It is stored in the driver's rport. */
591815a9c43SJames Smart 	ndlp = rport->ndlp;
592ba43c4d0SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
593ba43c4d0SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
594815a9c43SJames Smart 				 "6051 Remoteport %p, rport has invalid ndlp. "
595815a9c43SJames Smart 				 "Failing LS Req\n", pnvme_rport);
596ba43c4d0SJames Smart 		return -ENODEV;
597ba43c4d0SJames Smart 	}
598ba43c4d0SJames Smart 
599ba43c4d0SJames Smart 	/* The remote node has to be a mapped nvme target or an
600ba43c4d0SJames Smart 	 * unmapped nvme initiator or it's an error.
601ba43c4d0SJames Smart 	 */
602ba43c4d0SJames Smart 	ntype = ndlp->nlp_type;
603ba43c4d0SJames Smart 	nstate = ndlp->nlp_state;
604ba43c4d0SJames Smart 	if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
605ba43c4d0SJames Smart 	    (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
606ba43c4d0SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
607ba43c4d0SJames Smart 				 "6088 DID x%06x not ready for "
608ba43c4d0SJames Smart 				 "IO. State x%x, Type x%x\n",
609ba43c4d0SJames Smart 				 pnvme_rport->port_id,
610ba43c4d0SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type);
611ba43c4d0SJames Smart 		return -ENODEV;
61201649561SJames Smart 	}
61301649561SJames Smart 	bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
61401649561SJames Smart 	if (!bmp) {
61501649561SJames Smart 
61601649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
61701649561SJames Smart 				 "6044 Could not find node for DID %x\n",
61801649561SJames Smart 				 pnvme_rport->port_id);
61901649561SJames Smart 		return 2;
62001649561SJames Smart 	}
62101649561SJames Smart 	INIT_LIST_HEAD(&bmp->list);
62201649561SJames Smart 	bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
62301649561SJames Smart 	if (!bmp->virt) {
62401649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
62501649561SJames Smart 				 "6042 Could not find node for DID %x\n",
62601649561SJames Smart 				 pnvme_rport->port_id);
62701649561SJames Smart 		kfree(bmp);
62801649561SJames Smart 		return 3;
62901649561SJames Smart 	}
63001649561SJames Smart 	bpl = (struct ulp_bde64 *)bmp->virt;
63101649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
63201649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
63301649561SJames Smart 	bpl->tus.f.bdeFlags = 0;
63401649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
63501649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
63601649561SJames Smart 	bpl++;
63701649561SJames Smart 
63801649561SJames Smart 	bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
63901649561SJames Smart 	bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
64001649561SJames Smart 	bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
64101649561SJames Smart 	bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
64201649561SJames Smart 	bpl->tus.w = le32_to_cpu(bpl->tus.w);
64301649561SJames Smart 
64401649561SJames Smart 	/* Expand print to include key fields. */
64501649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
646815a9c43SJames Smart 			 "6149 Issue LS Req to DID 0x%06x lport %p, rport %p "
647815a9c43SJames Smart 			 "lsreq%p rqstlen:%d rsplen:%d %pad %pad\n",
648815a9c43SJames Smart 			 ndlp->nlp_DID,
64901649561SJames Smart 			 pnvme_lport, pnvme_rport,
65001649561SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
651825c6abbSArnd Bergmann 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
652825c6abbSArnd Bergmann 			 &pnvme_lsreq->rspdma);
65301649561SJames Smart 
65466a210ffSJames Smart 	atomic_inc(&lport->fc4NvmeLsRequests);
65501649561SJames Smart 
65601649561SJames Smart 	/* Hardcode the wait to 30 seconds.  Connections are failing otherwise.
65701649561SJames Smart 	 * This code allows it all to work.
65801649561SJames Smart 	 */
65901649561SJames Smart 	ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
66001649561SJames Smart 				pnvme_lsreq, lpfc_nvme_cmpl_gen_req,
66101649561SJames Smart 				ndlp, 2, 30, 0);
66201649561SJames Smart 	if (ret != WQE_SUCCESS) {
6634b056682SJames Smart 		atomic_inc(&lport->xmt_ls_err);
664815a9c43SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
66501649561SJames Smart 				 "6052 EXIT. issue ls wqe failed lport %p, "
66601649561SJames Smart 				 "rport %p lsreq%p Status %x DID %x\n",
66701649561SJames Smart 				 pnvme_lport, pnvme_rport, pnvme_lsreq,
66801649561SJames Smart 				 ret, ndlp->nlp_DID);
66901649561SJames Smart 		lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
67001649561SJames Smart 		kfree(bmp);
67101649561SJames Smart 		return ret;
67201649561SJames Smart 	}
67301649561SJames Smart 
67401649561SJames Smart 	/* Stub in routine and return 0 for now. */
67501649561SJames Smart 	return ret;
67601649561SJames Smart }
67701649561SJames Smart 
67801649561SJames Smart /**
67901649561SJames Smart  * lpfc_nvme_ls_abort - Issue an Link Service request
68001649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
68101649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
68201649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
68301649561SJames Smart  *
68401649561SJames Smart  * Driver registers this routine to handle any link service request
68501649561SJames Smart  * from the nvme_fc transport to a remote nvme-aware port.
68601649561SJames Smart  *
68701649561SJames Smart  * Return value :
68801649561SJames Smart  *   0 - Success
68901649561SJames Smart  *   TODO: What are the failure codes.
69001649561SJames Smart  **/
69101649561SJames Smart static void
69201649561SJames Smart lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
69301649561SJames Smart 		   struct nvme_fc_remote_port *pnvme_rport,
69401649561SJames Smart 		   struct nvmefc_ls_req *pnvme_lsreq)
69501649561SJames Smart {
69601649561SJames Smart 	struct lpfc_nvme_lport *lport;
69701649561SJames Smart 	struct lpfc_vport *vport;
69801649561SJames Smart 	struct lpfc_hba *phba;
69901649561SJames Smart 	struct lpfc_nodelist *ndlp;
70001649561SJames Smart 	LIST_HEAD(abort_list);
70101649561SJames Smart 	struct lpfc_sli_ring *pring;
70201649561SJames Smart 	struct lpfc_iocbq *wqe, *next_wqe;
70301649561SJames Smart 
70401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
70566a210ffSJames Smart 	if (unlikely(!lport))
70666a210ffSJames Smart 		return;
70701649561SJames Smart 	vport = lport->vport;
70801649561SJames Smart 	phba = vport->phba;
70901649561SJames Smart 
7103386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
7113386f4bdSJames Smart 		return;
7123386f4bdSJames Smart 
71301649561SJames Smart 	ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
71401649561SJames Smart 	if (!ndlp) {
71501649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
71601649561SJames Smart 				 "6049 Could not find node for DID %x\n",
71701649561SJames Smart 				 pnvme_rport->port_id);
71801649561SJames Smart 		return;
71901649561SJames Smart 	}
72001649561SJames Smart 
72101649561SJames Smart 	/* Expand print to include key fields. */
72201649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
72301649561SJames Smart 			 "6040 ENTER.  lport %p, rport %p lsreq %p rqstlen:%d "
724825c6abbSArnd Bergmann 			 "rsplen:%d %pad %pad\n",
72501649561SJames Smart 			 pnvme_lport, pnvme_rport,
72601649561SJames Smart 			 pnvme_lsreq, pnvme_lsreq->rqstlen,
727825c6abbSArnd Bergmann 			 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
728825c6abbSArnd Bergmann 			 &pnvme_lsreq->rspdma);
72901649561SJames Smart 
73001649561SJames Smart 	/*
73101649561SJames Smart 	 * Lock the ELS ring txcmplq and build a local list of all ELS IOs
73201649561SJames Smart 	 * that need an ABTS.  The IOs need to stay on the txcmplq so that
73301649561SJames Smart 	 * the abort operation completes them successfully.
73401649561SJames Smart 	 */
73501649561SJames Smart 	pring = phba->sli4_hba.nvmels_wq->pring;
73601649561SJames Smart 	spin_lock_irq(&phba->hbalock);
73701649561SJames Smart 	spin_lock(&pring->ring_lock);
73801649561SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
73901649561SJames Smart 		/* Add to abort_list on on NDLP match. */
74001649561SJames Smart 		if (lpfc_check_sli_ndlp(phba, pring, wqe, ndlp)) {
74101649561SJames Smart 			wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
74201649561SJames Smart 			list_add_tail(&wqe->dlist, &abort_list);
74301649561SJames Smart 		}
74401649561SJames Smart 	}
74501649561SJames Smart 	spin_unlock(&pring->ring_lock);
74601649561SJames Smart 	spin_unlock_irq(&phba->hbalock);
74701649561SJames Smart 
74801649561SJames Smart 	/* Abort the targeted IOs and remove them from the abort list. */
74901649561SJames Smart 	list_for_each_entry_safe(wqe, next_wqe, &abort_list, dlist) {
7504b056682SJames Smart 		atomic_inc(&lport->xmt_ls_abort);
75101649561SJames Smart 		spin_lock_irq(&phba->hbalock);
75201649561SJames Smart 		list_del_init(&wqe->dlist);
75301649561SJames Smart 		lpfc_sli_issue_abort_iotag(phba, pring, wqe);
75401649561SJames Smart 		spin_unlock_irq(&phba->hbalock);
75501649561SJames Smart 	}
75601649561SJames Smart }
75701649561SJames Smart 
75801649561SJames Smart /* Fix up the existing sgls for NVME IO. */
7595fd11085SJames Smart static inline void
76001649561SJames Smart lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
76101649561SJames Smart 		       struct lpfc_nvme_buf *lpfc_ncmd,
76201649561SJames Smart 		       struct nvmefc_fcp_req *nCmd)
76301649561SJames Smart {
7644e565cf0SJames Smart 	struct lpfc_hba  *phba = vport->phba;
76501649561SJames Smart 	struct sli4_sge *sgl;
76601649561SJames Smart 	union lpfc_wqe128 *wqe;
76701649561SJames Smart 	uint32_t *wptr, *dptr;
76801649561SJames Smart 
76901649561SJames Smart 	/*
7704e565cf0SJames Smart 	 * Get a local pointer to the built-in wqe and correct
7714e565cf0SJames Smart 	 * the cmd size to match NVME's 96 bytes and fix
7724e565cf0SJames Smart 	 * the dma address.
7734e565cf0SJames Smart 	 */
7744e565cf0SJames Smart 
775205e8240SJames Smart 	wqe = &lpfc_ncmd->cur_iocbq.wqe;
7764e565cf0SJames Smart 
7774e565cf0SJames Smart 	/*
77801649561SJames Smart 	 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
77901649561SJames Smart 	 * match NVME.  NVME sends 96 bytes. Also, use the
78001649561SJames Smart 	 * nvme commands command and response dma addresses
78101649561SJames Smart 	 * rather than the virtual memory to ease the restore
78201649561SJames Smart 	 * operation.
78301649561SJames Smart 	 */
78401649561SJames Smart 	sgl = lpfc_ncmd->nvme_sgl;
78501649561SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
7864e565cf0SJames Smart 	if (phba->cfg_nvme_embed_cmd) {
7874e565cf0SJames Smart 		sgl->addr_hi = 0;
7884e565cf0SJames Smart 		sgl->addr_lo = 0;
7894e565cf0SJames Smart 
7904e565cf0SJames Smart 		/* Word 0-2 - NVME CMND IU (embedded payload) */
7914e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
7924e565cf0SJames Smart 		wqe->generic.bde.tus.f.bdeSize = 56;
7934e565cf0SJames Smart 		wqe->generic.bde.addrHigh = 0;
7944e565cf0SJames Smart 		wqe->generic.bde.addrLow =  64;  /* Word 16 */
7954e565cf0SJames Smart 
7965fd11085SJames Smart 		/* Word 10  - dbde is 0, wqes is 1 in template */
79701649561SJames Smart 
79801649561SJames Smart 		/*
79901649561SJames Smart 		 * Embed the payload in the last half of the WQE
80001649561SJames Smart 		 * WQE words 16-30 get the NVME CMD IU payload
80101649561SJames Smart 		 *
802b06a622fSJames Smart 		 * WQE words 16-19 get payload Words 1-4
80301649561SJames Smart 		 * WQE words 20-21 get payload Words 6-7
80401649561SJames Smart 		 * WQE words 22-29 get payload Words 16-23
80501649561SJames Smart 		 */
806b06a622fSJames Smart 		wptr = &wqe->words[16];  /* WQE ptr */
80701649561SJames Smart 		dptr = (uint32_t *)nCmd->cmdaddr;  /* payload ptr */
808b06a622fSJames Smart 		dptr++;			/* Skip Word 0 in payload */
80901649561SJames Smart 
810b06a622fSJames Smart 		*wptr++ = *dptr++;	/* Word 1 */
81101649561SJames Smart 		*wptr++ = *dptr++;	/* Word 2 */
81201649561SJames Smart 		*wptr++ = *dptr++;	/* Word 3 */
81301649561SJames Smart 		*wptr++ = *dptr++;	/* Word 4 */
81401649561SJames Smart 		dptr++;			/* Skip Word 5 in payload */
81501649561SJames Smart 		*wptr++ = *dptr++;	/* Word 6 */
81601649561SJames Smart 		*wptr++ = *dptr++;	/* Word 7 */
81701649561SJames Smart 		dptr += 8;		/* Skip Words 8-15 in payload */
81801649561SJames Smart 		*wptr++ = *dptr++;	/* Word 16 */
81901649561SJames Smart 		*wptr++ = *dptr++;	/* Word 17 */
82001649561SJames Smart 		*wptr++ = *dptr++;	/* Word 18 */
82101649561SJames Smart 		*wptr++ = *dptr++;	/* Word 19 */
82201649561SJames Smart 		*wptr++ = *dptr++;	/* Word 20 */
82301649561SJames Smart 		*wptr++ = *dptr++;	/* Word 21 */
82401649561SJames Smart 		*wptr++ = *dptr++;	/* Word 22 */
82501649561SJames Smart 		*wptr   = *dptr;	/* Word 23 */
8265fd11085SJames Smart 	} else {
8275fd11085SJames Smart 		sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
8285fd11085SJames Smart 		sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
8295fd11085SJames Smart 
8305fd11085SJames Smart 		/* Word 0-2 - NVME CMND IU Inline BDE */
8315fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeFlags =  BUFF_TYPE_BDE_64;
8325fd11085SJames Smart 		wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
8335fd11085SJames Smart 		wqe->generic.bde.addrHigh = sgl->addr_hi;
8345fd11085SJames Smart 		wqe->generic.bde.addrLow =  sgl->addr_lo;
8355fd11085SJames Smart 
8365fd11085SJames Smart 		/* Word 10 */
8375fd11085SJames Smart 		bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
8385fd11085SJames Smart 		bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
8395fd11085SJames Smart 	}
8405fd11085SJames Smart 
8415fd11085SJames Smart 	sgl++;
8425fd11085SJames Smart 
8435fd11085SJames Smart 	/* Setup the physical region for the FCP RSP */
8445fd11085SJames Smart 	sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
8455fd11085SJames Smart 	sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
8465fd11085SJames Smart 	sgl->word2 = le32_to_cpu(sgl->word2);
8475fd11085SJames Smart 	if (nCmd->sg_cnt)
8485fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
8495fd11085SJames Smart 	else
8505fd11085SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 1);
8515fd11085SJames Smart 	sgl->word2 = cpu_to_le32(sgl->word2);
8525fd11085SJames Smart 	sgl->sge_len = cpu_to_le32(nCmd->rsplen);
85301649561SJames Smart }
85401649561SJames Smart 
855bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
856bd2cdd5eSJames Smart static void
857bd2cdd5eSJames Smart lpfc_nvme_ktime(struct lpfc_hba *phba,
858bd2cdd5eSJames Smart 		struct lpfc_nvme_buf *lpfc_ncmd)
859bd2cdd5eSJames Smart {
860bd2cdd5eSJames Smart 	uint64_t seg1, seg2, seg3, seg4;
861c8a4ce0bSDick Kennedy 	uint64_t segsum;
862bd2cdd5eSJames Smart 
863bd2cdd5eSJames Smart 	if (!lpfc_ncmd->ts_last_cmd ||
864bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_cmd_start ||
865bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_cmd_wqput ||
866bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_isr_cmpl ||
867bd2cdd5eSJames Smart 	    !lpfc_ncmd->ts_data_nvme)
868bd2cdd5eSJames Smart 		return;
869c8a4ce0bSDick Kennedy 
870c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_cmd_start)
871c8a4ce0bSDick Kennedy 		return;
872bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_cmd_start < lpfc_ncmd->ts_last_cmd)
873bd2cdd5eSJames Smart 		return;
874bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_cmd_wqput < lpfc_ncmd->ts_cmd_start)
875bd2cdd5eSJames Smart 		return;
876bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_isr_cmpl < lpfc_ncmd->ts_cmd_wqput)
877bd2cdd5eSJames Smart 		return;
878bd2cdd5eSJames Smart 	if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_isr_cmpl)
879bd2cdd5eSJames Smart 		return;
880bd2cdd5eSJames Smart 	/*
881bd2cdd5eSJames Smart 	 * Segment 1 - Time from Last FCP command cmpl is handed
882bd2cdd5eSJames Smart 	 * off to NVME Layer to start of next command.
883bd2cdd5eSJames Smart 	 * Segment 2 - Time from Driver receives a IO cmd start
884bd2cdd5eSJames Smart 	 * from NVME Layer to WQ put is done on IO cmd.
885bd2cdd5eSJames Smart 	 * Segment 3 - Time from Driver WQ put is done on IO cmd
886bd2cdd5eSJames Smart 	 * to MSI-X ISR for IO cmpl.
887bd2cdd5eSJames Smart 	 * Segment 4 - Time from MSI-X ISR for IO cmpl to when
888bd2cdd5eSJames Smart 	 * cmpl is handled off to the NVME Layer.
889bd2cdd5eSJames Smart 	 */
890bd2cdd5eSJames Smart 	seg1 = lpfc_ncmd->ts_cmd_start - lpfc_ncmd->ts_last_cmd;
891c8a4ce0bSDick Kennedy 	if (seg1 > 5000000)  /* 5 ms - for sequential IOs only */
892c8a4ce0bSDick Kennedy 		seg1 = 0;
893bd2cdd5eSJames Smart 
894bd2cdd5eSJames Smart 	/* Calculate times relative to start of IO */
895bd2cdd5eSJames Smart 	seg2 = (lpfc_ncmd->ts_cmd_wqput - lpfc_ncmd->ts_cmd_start);
896c8a4ce0bSDick Kennedy 	segsum = seg2;
897c8a4ce0bSDick Kennedy 	seg3 = lpfc_ncmd->ts_isr_cmpl - lpfc_ncmd->ts_cmd_start;
898c8a4ce0bSDick Kennedy 	if (segsum > seg3)
899c8a4ce0bSDick Kennedy 		return;
900c8a4ce0bSDick Kennedy 	seg3 -= segsum;
901c8a4ce0bSDick Kennedy 	segsum += seg3;
902c8a4ce0bSDick Kennedy 
903c8a4ce0bSDick Kennedy 	seg4 = lpfc_ncmd->ts_data_nvme - lpfc_ncmd->ts_cmd_start;
904c8a4ce0bSDick Kennedy 	if (segsum > seg4)
905c8a4ce0bSDick Kennedy 		return;
906c8a4ce0bSDick Kennedy 	seg4 -= segsum;
907c8a4ce0bSDick Kennedy 
908bd2cdd5eSJames Smart 	phba->ktime_data_samples++;
909bd2cdd5eSJames Smart 	phba->ktime_seg1_total += seg1;
910bd2cdd5eSJames Smart 	if (seg1 < phba->ktime_seg1_min)
911bd2cdd5eSJames Smart 		phba->ktime_seg1_min = seg1;
912bd2cdd5eSJames Smart 	else if (seg1 > phba->ktime_seg1_max)
913bd2cdd5eSJames Smart 		phba->ktime_seg1_max = seg1;
914bd2cdd5eSJames Smart 	phba->ktime_seg2_total += seg2;
915bd2cdd5eSJames Smart 	if (seg2 < phba->ktime_seg2_min)
916bd2cdd5eSJames Smart 		phba->ktime_seg2_min = seg2;
917bd2cdd5eSJames Smart 	else if (seg2 > phba->ktime_seg2_max)
918bd2cdd5eSJames Smart 		phba->ktime_seg2_max = seg2;
919bd2cdd5eSJames Smart 	phba->ktime_seg3_total += seg3;
920bd2cdd5eSJames Smart 	if (seg3 < phba->ktime_seg3_min)
921bd2cdd5eSJames Smart 		phba->ktime_seg3_min = seg3;
922bd2cdd5eSJames Smart 	else if (seg3 > phba->ktime_seg3_max)
923bd2cdd5eSJames Smart 		phba->ktime_seg3_max = seg3;
924bd2cdd5eSJames Smart 	phba->ktime_seg4_total += seg4;
925bd2cdd5eSJames Smart 	if (seg4 < phba->ktime_seg4_min)
926bd2cdd5eSJames Smart 		phba->ktime_seg4_min = seg4;
927bd2cdd5eSJames Smart 	else if (seg4 > phba->ktime_seg4_max)
928bd2cdd5eSJames Smart 		phba->ktime_seg4_max = seg4;
929bd2cdd5eSJames Smart 
930bd2cdd5eSJames Smart 	lpfc_ncmd->ts_last_cmd = 0;
931bd2cdd5eSJames Smart 	lpfc_ncmd->ts_cmd_start = 0;
932bd2cdd5eSJames Smart 	lpfc_ncmd->ts_cmd_wqput  = 0;
933bd2cdd5eSJames Smart 	lpfc_ncmd->ts_isr_cmpl = 0;
934bd2cdd5eSJames Smart 	lpfc_ncmd->ts_data_nvme = 0;
935bd2cdd5eSJames Smart }
936bd2cdd5eSJames Smart #endif
937bd2cdd5eSJames Smart 
93801649561SJames Smart /**
93901649561SJames Smart  * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
94001649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
94101649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
94201649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
94301649561SJames Smart  *
94401649561SJames Smart  * Driver registers this routine as it io request handler.  This
94501649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
94601649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
94701649561SJames Smart  *
94801649561SJames Smart  * Return value :
94901649561SJames Smart  *   0 - Success
95001649561SJames Smart  *   TODO: What are the failure codes.
95101649561SJames Smart  **/
95201649561SJames Smart static void
95301649561SJames Smart lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
95401649561SJames Smart 			  struct lpfc_wcqe_complete *wcqe)
95501649561SJames Smart {
95601649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd =
95701649561SJames Smart 		(struct lpfc_nvme_buf *)pwqeIn->context1;
95801649561SJames Smart 	struct lpfc_vport *vport = pwqeIn->vport;
95901649561SJames Smart 	struct nvmefc_fcp_req *nCmd;
96001649561SJames Smart 	struct nvme_fc_ersp_iu *ep;
96101649561SJames Smart 	struct nvme_fc_cmd_iu *cp;
96201649561SJames Smart 	struct lpfc_nvme_rport *rport;
96301649561SJames Smart 	struct lpfc_nodelist *ndlp;
964bbe3012bSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
9654b056682SJames Smart 	struct lpfc_nvme_lport *lport;
96666a210ffSJames Smart 	struct lpfc_nvme_ctrl_stat *cstat;
96701649561SJames Smart 	unsigned long flags;
96866a210ffSJames Smart 	uint32_t code, status, idx;
96901649561SJames Smart 	uint16_t cid, sqhd, data;
97001649561SJames Smart 	uint32_t *ptr;
97101649561SJames Smart 
97201649561SJames Smart 	/* Sanity check on return of outstanding command */
97301649561SJames Smart 	if (!lpfc_ncmd || !lpfc_ncmd->nvmeCmd || !lpfc_ncmd->nrport) {
97401649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
97501649561SJames Smart 				 "6071 Completion pointers bad on wqe %p.\n",
97601649561SJames Smart 				 wcqe);
97701649561SJames Smart 		return;
97801649561SJames Smart 	}
97901649561SJames Smart 	nCmd = lpfc_ncmd->nvmeCmd;
98001649561SJames Smart 	rport = lpfc_ncmd->nrport;
9814b056682SJames Smart 	status = bf_get(lpfc_wcqe_c_status, wcqe);
98266a210ffSJames Smart 
9834b056682SJames Smart 	lport = (struct lpfc_nvme_lport *)vport->localport->private;
98466a210ffSJames Smart 	if (lport) {
98566a210ffSJames Smart 		idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
98666a210ffSJames Smart 		cstat = &lport->cstat[idx];
98766a210ffSJames Smart 		atomic_inc(&cstat->fc4NvmeIoCmpls);
98866a210ffSJames Smart 		if (status) {
9894b056682SJames Smart 			if (bf_get(lpfc_wcqe_c_xb, wcqe))
9904b056682SJames Smart 				atomic_inc(&lport->cmpl_fcp_xb);
9914b056682SJames Smart 			atomic_inc(&lport->cmpl_fcp_err);
9924b056682SJames Smart 		}
99366a210ffSJames Smart 	}
99401649561SJames Smart 
995bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
996bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
9974b056682SJames Smart 			 status, wcqe->parameter);
99801649561SJames Smart 	/*
99901649561SJames Smart 	 * Catch race where our node has transitioned, but the
100001649561SJames Smart 	 * transport is still transitioning.
100101649561SJames Smart 	 */
100201649561SJames Smart 	ndlp = rport->ndlp;
100301649561SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
100401649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
1005c154e750SJames Smart 				 "6061 rport %p,  DID x%06x node not ready.\n",
1006c154e750SJames Smart 				 rport, rport->remoteport->port_id);
100701649561SJames Smart 
100801649561SJames Smart 		ndlp = lpfc_findnode_did(vport, rport->remoteport->port_id);
100901649561SJames Smart 		if (!ndlp) {
101001649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
101101649561SJames Smart 					 "6062 Ignoring NVME cmpl.  No ndlp\n");
101201649561SJames Smart 			goto out_err;
101301649561SJames Smart 		}
101401649561SJames Smart 	}
101501649561SJames Smart 
101601649561SJames Smart 	code = bf_get(lpfc_wcqe_c_code, wcqe);
101701649561SJames Smart 	if (code == CQE_CODE_NVME_ERSP) {
101801649561SJames Smart 		/* For this type of CQE, we need to rebuild the rsp */
101901649561SJames Smart 		ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
102001649561SJames Smart 
102101649561SJames Smart 		/*
102201649561SJames Smart 		 * Get Command Id from cmd to plug into response. This
102301649561SJames Smart 		 * code is not needed in the next NVME Transport drop.
102401649561SJames Smart 		 */
102501649561SJames Smart 		cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
102601649561SJames Smart 		cid = cp->sqe.common.command_id;
102701649561SJames Smart 
102801649561SJames Smart 		/*
102901649561SJames Smart 		 * RSN is in CQE word 2
103001649561SJames Smart 		 * SQHD is in CQE Word 3 bits 15:0
103101649561SJames Smart 		 * Cmd Specific info is in CQE Word 1
103201649561SJames Smart 		 * and in CQE Word 0 bits 15:0
103301649561SJames Smart 		 */
103401649561SJames Smart 		sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
103501649561SJames Smart 
103601649561SJames Smart 		/* Now lets build the NVME ERSP IU */
103701649561SJames Smart 		ep->iu_len = cpu_to_be16(8);
103801649561SJames Smart 		ep->rsn = wcqe->parameter;
103901649561SJames Smart 		ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
104001649561SJames Smart 		ep->rsvd12 = 0;
104101649561SJames Smart 		ptr = (uint32_t *)&ep->cqe.result.u64;
104201649561SJames Smart 		*ptr++ = wcqe->total_data_placed;
104301649561SJames Smart 		data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
104401649561SJames Smart 		*ptr = (uint32_t)data;
104501649561SJames Smart 		ep->cqe.sq_head = sqhd;
104601649561SJames Smart 		ep->cqe.sq_id =  nCmd->sqid;
104701649561SJames Smart 		ep->cqe.command_id = cid;
104801649561SJames Smart 		ep->cqe.status = 0;
104901649561SJames Smart 
105001649561SJames Smart 		lpfc_ncmd->status = IOSTAT_SUCCESS;
105101649561SJames Smart 		lpfc_ncmd->result = 0;
105201649561SJames Smart 		nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
105301649561SJames Smart 		nCmd->transferred_length = nCmd->payload_length;
105401649561SJames Smart 	} else {
10554b056682SJames Smart 		lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
1056952c303bSDick Kennedy 		lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
105701649561SJames Smart 
105801649561SJames Smart 		/* For NVME, the only failure path that results in an
105901649561SJames Smart 		 * IO error is when the adapter rejects it.  All other
106001649561SJames Smart 		 * conditions are a success case and resolved by the
106101649561SJames Smart 		 * transport.
106201649561SJames Smart 		 * IOSTAT_FCP_RSP_ERROR means:
106301649561SJames Smart 		 * 1. Length of data received doesn't match total
106401649561SJames Smart 		 *    transfer length in WQE
106501649561SJames Smart 		 * 2. If the RSP payload does NOT match these cases:
106601649561SJames Smart 		 *    a. RSP length 12/24 bytes and all zeros
106701649561SJames Smart 		 *    b. NVME ERSP
106801649561SJames Smart 		 */
106901649561SJames Smart 		switch (lpfc_ncmd->status) {
107001649561SJames Smart 		case IOSTAT_SUCCESS:
107101649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
107201649561SJames Smart 			nCmd->rcv_rsplen = 0;
107301649561SJames Smart 			nCmd->status = 0;
107401649561SJames Smart 			break;
107501649561SJames Smart 		case IOSTAT_FCP_RSP_ERROR:
107601649561SJames Smart 			nCmd->transferred_length = wcqe->total_data_placed;
107701649561SJames Smart 			nCmd->rcv_rsplen = wcqe->parameter;
107801649561SJames Smart 			nCmd->status = 0;
107901649561SJames Smart 			/* Sanity check */
108001649561SJames Smart 			if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
108101649561SJames Smart 				break;
108201649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
108301649561SJames Smart 					 "6081 NVME Completion Protocol Error: "
108486c67379SJames Smart 					 "xri %x status x%x result x%x "
108586c67379SJames Smart 					 "placed x%x\n",
108686c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
108701649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
108801649561SJames Smart 					 wcqe->total_data_placed);
108901649561SJames Smart 			break;
1090952c303bSDick Kennedy 		case IOSTAT_LOCAL_REJECT:
1091952c303bSDick Kennedy 			/* Let fall through to set command final state. */
1092952c303bSDick Kennedy 			if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1093952c303bSDick Kennedy 				lpfc_printf_vlog(vport, KERN_INFO,
1094952c303bSDick Kennedy 					 LOG_NVME_IOERR,
1095952c303bSDick Kennedy 					 "6032 Delay Aborted cmd %p "
1096952c303bSDick Kennedy 					 "nvme cmd %p, xri x%x, "
1097952c303bSDick Kennedy 					 "xb %d\n",
1098952c303bSDick Kennedy 					 lpfc_ncmd, nCmd,
1099952c303bSDick Kennedy 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
1100952c303bSDick Kennedy 					 bf_get(lpfc_wcqe_c_xb, wcqe));
110101649561SJames Smart 		default:
110201649561SJames Smart out_err:
1103e3246a12SJames Smart 			lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
110486c67379SJames Smart 					 "6072 NVME Completion Error: xri %x "
110501649561SJames Smart 					 "status x%x result x%x placed x%x\n",
110686c67379SJames Smart 					 lpfc_ncmd->cur_iocbq.sli4_xritag,
110701649561SJames Smart 					 lpfc_ncmd->status, lpfc_ncmd->result,
110801649561SJames Smart 					 wcqe->total_data_placed);
110901649561SJames Smart 			nCmd->transferred_length = 0;
111001649561SJames Smart 			nCmd->rcv_rsplen = 0;
11118e009ce8SJames Smart 			nCmd->status = NVME_SC_INTERNAL;
111201649561SJames Smart 		}
111301649561SJames Smart 	}
111401649561SJames Smart 
111501649561SJames Smart 	/* pick up SLI4 exhange busy condition */
111601649561SJames Smart 	if (bf_get(lpfc_wcqe_c_xb, wcqe))
111701649561SJames Smart 		lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
111801649561SJames Smart 	else
111901649561SJames Smart 		lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
112001649561SJames Smart 
112101649561SJames Smart 	if (ndlp && NLP_CHK_NODE_ACT(ndlp))
112201649561SJames Smart 		atomic_dec(&ndlp->cmd_pending);
112301649561SJames Smart 
112401649561SJames Smart 	/* Update stats and complete the IO.  There is
112501649561SJames Smart 	 * no need for dma unprep because the nvme_transport
112601649561SJames Smart 	 * owns the dma address.
112701649561SJames Smart 	 */
1128bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1129c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start) {
1130bd2cdd5eSJames Smart 		lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
1131bd2cdd5eSJames Smart 		lpfc_ncmd->ts_data_nvme = ktime_get_ns();
1132bd2cdd5eSJames Smart 		phba->ktime_last_cmd = lpfc_ncmd->ts_data_nvme;
1133bd2cdd5eSJames Smart 		lpfc_nvme_ktime(phba, lpfc_ncmd);
1134bd2cdd5eSJames Smart 	}
1135bd2cdd5eSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1136bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu != smp_processor_id())
1137bd2cdd5eSJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1138bd2cdd5eSJames Smart 					 "6701 CPU Check cmpl: "
1139bd2cdd5eSJames Smart 					 "cpu %d expect %d\n",
1140bd2cdd5eSJames Smart 					 smp_processor_id(), lpfc_ncmd->cpu);
1141bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1142bd2cdd5eSJames Smart 			phba->cpucheck_cmpl_io[lpfc_ncmd->cpu]++;
1143bd2cdd5eSJames Smart 	}
1144bd2cdd5eSJames Smart #endif
1145952c303bSDick Kennedy 
1146952c303bSDick Kennedy 	/* NVME targets need completion held off until the abort exchange
1147add9d6beSJames Smart 	 * completes unless the NVME Rport is getting unregistered.
1148952c303bSDick Kennedy 	 */
1149add9d6beSJames Smart 
11503fd78355SJames Smart 	if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
115191455b85SJames Smart 		freqpriv = nCmd->private;
115291455b85SJames Smart 		freqpriv->nvme_buf = NULL;
115301649561SJames Smart 		nCmd->done(nCmd);
11543fd78355SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
1155add9d6beSJames Smart 	}
115601649561SJames Smart 
115701649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
115801649561SJames Smart 	lpfc_ncmd->nrport = NULL;
115901649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
116001649561SJames Smart 
1161952c303bSDick Kennedy 	/* Call release with XB=1 to queue the IO into the abort list. */
116201649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
116301649561SJames Smart }
116401649561SJames Smart 
116501649561SJames Smart 
116601649561SJames Smart /**
116701649561SJames Smart  * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
116801649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
116901649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
117001649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
117101649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
117201649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
117301649561SJames Smart  *
117401649561SJames Smart  * Driver registers this routine as it io request handler.  This
117501649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
117601649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
117701649561SJames Smart  *
117801649561SJames Smart  * Return value :
117901649561SJames Smart  *   0 - Success
118001649561SJames Smart  *   TODO: What are the failure codes.
118101649561SJames Smart  **/
118201649561SJames Smart static int
118301649561SJames Smart lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
118401649561SJames Smart 		      struct lpfc_nvme_buf *lpfc_ncmd,
118566a210ffSJames Smart 		      struct lpfc_nodelist *pnode,
118666a210ffSJames Smart 		      struct lpfc_nvme_ctrl_stat *cstat)
118701649561SJames Smart {
118801649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
118901649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
119001649561SJames Smart 	struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
1191205e8240SJames Smart 	union lpfc_wqe128 *wqe = &pwqeq->wqe;
119201649561SJames Smart 	uint32_t req_len;
119301649561SJames Smart 
119401649561SJames Smart 	if (!pnode || !NLP_CHK_NODE_ACT(pnode))
119501649561SJames Smart 		return -EINVAL;
119601649561SJames Smart 
119701649561SJames Smart 	/*
119801649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
119901649561SJames Smart 	 * the single mapping, or neither.
120001649561SJames Smart 	 */
120101649561SJames Smart 	if (nCmd->sg_cnt) {
120201649561SJames Smart 		if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
12035fd11085SJames Smart 			/* From the iwrite template, initialize words 7 - 11 */
12045fd11085SJames Smart 			memcpy(&wqe->words[7],
12055fd11085SJames Smart 			       &lpfc_iwrite_cmd_template.words[7],
12065fd11085SJames Smart 			       sizeof(uint32_t) * 5);
12075fd11085SJames Smart 
12085fd11085SJames Smart 			/* Word 4 */
12095fd11085SJames Smart 			wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
12105fd11085SJames Smart 
121101649561SJames Smart 			/* Word 5 */
121201649561SJames Smart 			if ((phba->cfg_nvme_enable_fb) &&
121301649561SJames Smart 			    (pnode->nlp_flag & NLP_FIRSTBURST)) {
121401649561SJames Smart 				req_len = lpfc_ncmd->nvmeCmd->payload_length;
121501649561SJames Smart 				if (req_len < pnode->nvme_fb_size)
121601649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
121701649561SJames Smart 						req_len;
121801649561SJames Smart 				else
121901649561SJames Smart 					wqe->fcp_iwrite.initial_xfer_len =
122001649561SJames Smart 						pnode->nvme_fb_size;
12215fd11085SJames Smart 			} else {
12225fd11085SJames Smart 				wqe->fcp_iwrite.initial_xfer_len = 0;
122301649561SJames Smart 			}
122466a210ffSJames Smart 			atomic_inc(&cstat->fc4NvmeOutputRequests);
122501649561SJames Smart 		} else {
12265fd11085SJames Smart 			/* From the iread template, initialize words 7 - 11 */
12275fd11085SJames Smart 			memcpy(&wqe->words[7],
12285fd11085SJames Smart 			       &lpfc_iread_cmd_template.words[7],
12295fd11085SJames Smart 			       sizeof(uint32_t) * 5);
123001649561SJames Smart 
12315fd11085SJames Smart 			/* Word 4 */
12325fd11085SJames Smart 			wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
123301649561SJames Smart 
12345fd11085SJames Smart 			/* Word 5 */
12355fd11085SJames Smart 			wqe->fcp_iread.rsrvd5 = 0;
123601649561SJames Smart 
123766a210ffSJames Smart 			atomic_inc(&cstat->fc4NvmeInputRequests);
123801649561SJames Smart 		}
123901649561SJames Smart 	} else {
12405fd11085SJames Smart 		/* From the icmnd template, initialize words 4 - 11 */
12415fd11085SJames Smart 		memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
12425fd11085SJames Smart 		       sizeof(uint32_t) * 8);
124366a210ffSJames Smart 		atomic_inc(&cstat->fc4NvmeControlRequests);
124401649561SJames Smart 	}
124501649561SJames Smart 	/*
124601649561SJames Smart 	 * Finish initializing those WQE fields that are independent
124701649561SJames Smart 	 * of the nvme_cmnd request_buffer
124801649561SJames Smart 	 */
124901649561SJames Smart 
12505fd11085SJames Smart 	/* Word 3 */
12515fd11085SJames Smart 	bf_set(payload_offset_len, &wqe->fcp_icmd,
12525fd11085SJames Smart 	       (nCmd->rsplen + nCmd->cmdlen));
12535fd11085SJames Smart 
125401649561SJames Smart 	/* Word 6 */
125501649561SJames Smart 	bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
125601649561SJames Smart 	       phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
125701649561SJames Smart 	bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
125801649561SJames Smart 
125901649561SJames Smart 	/* Word 8 */
126001649561SJames Smart 	wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
126101649561SJames Smart 
126201649561SJames Smart 	/* Word 9 */
126301649561SJames Smart 	bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
126401649561SJames Smart 
126501649561SJames Smart 	pwqeq->vport = vport;
126601649561SJames Smart 	return 0;
126701649561SJames Smart }
126801649561SJames Smart 
126901649561SJames Smart 
127001649561SJames Smart /**
127101649561SJames Smart  * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
127201649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
127301649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
127401649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
127501649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
127601649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
127701649561SJames Smart  *
127801649561SJames Smart  * Driver registers this routine as it io request handler.  This
127901649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
128001649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.
128101649561SJames Smart  *
128201649561SJames Smart  * Return value :
128301649561SJames Smart  *   0 - Success
128401649561SJames Smart  *   TODO: What are the failure codes.
128501649561SJames Smart  **/
128601649561SJames Smart static int
128701649561SJames Smart lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
128801649561SJames Smart 		      struct lpfc_nvme_buf *lpfc_ncmd)
128901649561SJames Smart {
129001649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
129101649561SJames Smart 	struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1292205e8240SJames Smart 	union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
129301649561SJames Smart 	struct sli4_sge *sgl = lpfc_ncmd->nvme_sgl;
129401649561SJames Smart 	struct scatterlist *data_sg;
129501649561SJames Smart 	struct sli4_sge *first_data_sgl;
12960bc2b7c5SJames Smart 	struct ulp_bde64 *bde;
129701649561SJames Smart 	dma_addr_t physaddr;
129801649561SJames Smart 	uint32_t num_bde = 0;
129901649561SJames Smart 	uint32_t dma_len;
130001649561SJames Smart 	uint32_t dma_offset = 0;
130101649561SJames Smart 	int nseg, i;
130201649561SJames Smart 
130301649561SJames Smart 	/* Fix up the command and response DMA stuff. */
130401649561SJames Smart 	lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
130501649561SJames Smart 
130601649561SJames Smart 	/*
130701649561SJames Smart 	 * There are three possibilities here - use scatter-gather segment, use
130801649561SJames Smart 	 * the single mapping, or neither.
130901649561SJames Smart 	 */
131001649561SJames Smart 	if (nCmd->sg_cnt) {
131101649561SJames Smart 		/*
131201649561SJames Smart 		 * Jump over the cmd and rsp SGEs.  The fix routine
131301649561SJames Smart 		 * has already adjusted for this.
131401649561SJames Smart 		 */
131501649561SJames Smart 		sgl += 2;
131601649561SJames Smart 
131701649561SJames Smart 		first_data_sgl = sgl;
131801649561SJames Smart 		lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
131981e6a637SJames Smart 		if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
132001649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
132101649561SJames Smart 					"6058 Too many sg segments from "
132201649561SJames Smart 					"NVME Transport.  Max %d, "
132301649561SJames Smart 					"nvmeIO sg_cnt %d\n",
132429bfd55aSJames Smart 					phba->cfg_nvme_seg_cnt + 1,
132501649561SJames Smart 					lpfc_ncmd->seg_cnt);
132601649561SJames Smart 			lpfc_ncmd->seg_cnt = 0;
132701649561SJames Smart 			return 1;
132801649561SJames Smart 		}
132901649561SJames Smart 
133001649561SJames Smart 		/*
133101649561SJames Smart 		 * The driver established a maximum scatter-gather segment count
133201649561SJames Smart 		 * during probe that limits the number of sg elements in any
133301649561SJames Smart 		 * single nvme command.  Just run through the seg_cnt and format
133401649561SJames Smart 		 * the sge's.
133501649561SJames Smart 		 */
133601649561SJames Smart 		nseg = nCmd->sg_cnt;
133701649561SJames Smart 		data_sg = nCmd->first_sgl;
133801649561SJames Smart 		for (i = 0; i < nseg; i++) {
133901649561SJames Smart 			if (data_sg == NULL) {
134001649561SJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
134101649561SJames Smart 						"6059 dptr err %d, nseg %d\n",
134201649561SJames Smart 						i, nseg);
134301649561SJames Smart 				lpfc_ncmd->seg_cnt = 0;
134401649561SJames Smart 				return 1;
134501649561SJames Smart 			}
134601649561SJames Smart 			physaddr = data_sg->dma_address;
134701649561SJames Smart 			dma_len = data_sg->length;
134801649561SJames Smart 			sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
134901649561SJames Smart 			sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
135001649561SJames Smart 			sgl->word2 = le32_to_cpu(sgl->word2);
135101649561SJames Smart 			if ((num_bde + 1) == nseg)
135201649561SJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 1);
135301649561SJames Smart 			else
135401649561SJames Smart 				bf_set(lpfc_sli4_sge_last, sgl, 0);
135501649561SJames Smart 			bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
135601649561SJames Smart 			bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
135701649561SJames Smart 			sgl->word2 = cpu_to_le32(sgl->word2);
135801649561SJames Smart 			sgl->sge_len = cpu_to_le32(dma_len);
135901649561SJames Smart 
136001649561SJames Smart 			dma_offset += dma_len;
136101649561SJames Smart 			data_sg = sg_next(data_sg);
136201649561SJames Smart 			sgl++;
136301649561SJames Smart 		}
13640bc2b7c5SJames Smart 		if (phba->nvme_embed_pbde) {
13650bc2b7c5SJames Smart 			/* Use PBDE support for first SGL only, offset == 0 */
13660bc2b7c5SJames Smart 			/* Words 13-15 */
13670bc2b7c5SJames Smart 			bde = (struct ulp_bde64 *)
13680bc2b7c5SJames Smart 				&wqe->words[13];
13690bc2b7c5SJames Smart 			bde->addrLow = first_data_sgl->addr_lo;
13700bc2b7c5SJames Smart 			bde->addrHigh = first_data_sgl->addr_hi;
13710bc2b7c5SJames Smart 			bde->tus.f.bdeSize =
13720bc2b7c5SJames Smart 				le32_to_cpu(first_data_sgl->sge_len);
13730bc2b7c5SJames Smart 			bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
13740bc2b7c5SJames Smart 			bde->tus.w = cpu_to_le32(bde->tus.w);
13755fd11085SJames Smart 			/* wqe_pbde is 1 in template */
13765fd11085SJames Smart 		} else {
13775fd11085SJames Smart 			memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
13780bc2b7c5SJames Smart 			bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
13795fd11085SJames Smart 		}
138001649561SJames Smart 	} else {
13810bc2b7c5SJames Smart 		bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
13825fd11085SJames Smart 		memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
13830bc2b7c5SJames Smart 
138401649561SJames Smart 		/* For this clause to be valid, the payload_length
138501649561SJames Smart 		 * and sg_cnt must zero.
138601649561SJames Smart 		 */
138701649561SJames Smart 		if (nCmd->payload_length != 0) {
138801649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
138901649561SJames Smart 					"6063 NVME DMA Prep Err: sg_cnt %d "
139001649561SJames Smart 					"payload_length x%x\n",
139101649561SJames Smart 					nCmd->sg_cnt, nCmd->payload_length);
139201649561SJames Smart 			return 1;
139301649561SJames Smart 		}
139401649561SJames Smart 	}
139501649561SJames Smart 	return 0;
139601649561SJames Smart }
139701649561SJames Smart 
139801649561SJames Smart /**
139901649561SJames Smart  * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
140001649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
140101649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
140201649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
140301649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
140401649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
140501649561SJames Smart  *
140601649561SJames Smart  * Driver registers this routine as it io request handler.  This
140701649561SJames Smart  * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
140801649561SJames Smart  * data structure to the rport
140901649561SJames Smart  indicated in @lpfc_nvme_rport.
141001649561SJames Smart  *
141101649561SJames Smart  * Return value :
141201649561SJames Smart  *   0 - Success
141301649561SJames Smart  *   TODO: What are the failure codes.
141401649561SJames Smart  **/
141501649561SJames Smart static int
141601649561SJames Smart lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
141701649561SJames Smart 			struct nvme_fc_remote_port *pnvme_rport,
141801649561SJames Smart 			void *hw_queue_handle,
141901649561SJames Smart 			struct nvmefc_fcp_req *pnvme_fcreq)
142001649561SJames Smart {
142101649561SJames Smart 	int ret = 0;
1422cf1a1d3eSJames Smart 	int expedite = 0;
142366a210ffSJames Smart 	int idx;
142401649561SJames Smart 	struct lpfc_nvme_lport *lport;
142566a210ffSJames Smart 	struct lpfc_nvme_ctrl_stat *cstat;
142601649561SJames Smart 	struct lpfc_vport *vport;
142701649561SJames Smart 	struct lpfc_hba *phba;
142801649561SJames Smart 	struct lpfc_nodelist *ndlp;
142901649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd;
143001649561SJames Smart 	struct lpfc_nvme_rport *rport;
143101649561SJames Smart 	struct lpfc_nvme_qhandle *lpfc_queue_info;
1432c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
1433cf1a1d3eSJames Smart 	struct nvme_common_command *sqe;
1434bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1435bd2cdd5eSJames Smart 	uint64_t start = 0;
1436bd2cdd5eSJames Smart #endif
143701649561SJames Smart 
1438c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1439c3725bdcSJames Smart 	 * have timing races.
1440c3725bdcSJames Smart 	 */
144101649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1442c3725bdcSJames Smart 	if (unlikely(!lport)) {
1443c3725bdcSJames Smart 		ret = -EINVAL;
1444c3725bdcSJames Smart 		goto out_fail;
1445c3725bdcSJames Smart 	}
1446c3725bdcSJames Smart 
144701649561SJames Smart 	vport = lport->vport;
1448c3725bdcSJames Smart 
1449c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
1450c3725bdcSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1451c3725bdcSJames Smart 				 "6129 Fail Abort, NULL hw_queue_handle\n");
1452c3725bdcSJames Smart 		ret = -EINVAL;
1453c3725bdcSJames Smart 		goto out_fail;
1454c3725bdcSJames Smart 	}
1455c3725bdcSJames Smart 
145601649561SJames Smart 	phba = vport->phba;
145701649561SJames Smart 
14581abcb371SDick Kennedy 	if (vport->load_flag & FC_UNLOADING) {
14591abcb371SDick Kennedy 		ret = -ENODEV;
14601abcb371SDick Kennedy 		goto out_fail;
14611abcb371SDick Kennedy 	}
14621abcb371SDick Kennedy 
14633386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING) {
14643386f4bdSJames Smart 		ret = -ENODEV;
14653386f4bdSJames Smart 		goto out_fail;
14663386f4bdSJames Smart 	}
14673386f4bdSJames Smart 
1468c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1469c3725bdcSJames Smart 	if (unlikely(!freqpriv)) {
1470c3725bdcSJames Smart 		ret = -EINVAL;
1471b7672ae6SDick Kennedy 		goto out_fail;
1472b7672ae6SDick Kennedy 	}
1473b7672ae6SDick Kennedy 
1474bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1475bd2cdd5eSJames Smart 	if (phba->ktime_on)
1476bd2cdd5eSJames Smart 		start = ktime_get_ns();
1477bd2cdd5eSJames Smart #endif
147801649561SJames Smart 	rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
147901649561SJames Smart 	lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
148001649561SJames Smart 
148101649561SJames Smart 	/*
148201649561SJames Smart 	 * Catch race where our node has transitioned, but the
148301649561SJames Smart 	 * transport is still transitioning.
148401649561SJames Smart 	 */
148501649561SJames Smart 	ndlp = rport->ndlp;
148601649561SJames Smart 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
148701649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
148801649561SJames Smart 				 "6053 rport %p, ndlp %p, DID x%06x "
148901649561SJames Smart 				 "ndlp not ready.\n",
149001649561SJames Smart 				 rport, ndlp, pnvme_rport->port_id);
149101649561SJames Smart 
149201649561SJames Smart 		ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
149301649561SJames Smart 		if (!ndlp) {
149401649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
149501649561SJames Smart 					 "6066 Missing node for DID %x\n",
149601649561SJames Smart 					 pnvme_rport->port_id);
14974b056682SJames Smart 			atomic_inc(&lport->xmt_fcp_bad_ndlp);
149801649561SJames Smart 			ret = -ENODEV;
149901649561SJames Smart 			goto out_fail;
150001649561SJames Smart 		}
150101649561SJames Smart 	}
150201649561SJames Smart 
150301649561SJames Smart 	/* The remote node has to be a mapped target or it's an error. */
150401649561SJames Smart 	if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
150501649561SJames Smart 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
150601649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
150701649561SJames Smart 				 "6036 rport %p, DID x%06x not ready for "
150801649561SJames Smart 				 "IO. State x%x, Type x%x\n",
150901649561SJames Smart 				 rport, pnvme_rport->port_id,
151001649561SJames Smart 				 ndlp->nlp_state, ndlp->nlp_type);
15114b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_bad_ndlp);
151201649561SJames Smart 		ret = -ENODEV;
151301649561SJames Smart 		goto out_fail;
151401649561SJames Smart 
151501649561SJames Smart 	}
151601649561SJames Smart 
1517cf1a1d3eSJames Smart 	/* Currently only NVME Keep alive commands should be expedited
1518cf1a1d3eSJames Smart 	 * if the driver runs out of a resource. These should only be
1519cf1a1d3eSJames Smart 	 * issued on the admin queue, qidx 0
1520cf1a1d3eSJames Smart 	 */
1521cf1a1d3eSJames Smart 	if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1522cf1a1d3eSJames Smart 		sqe = &((struct nvme_fc_cmd_iu *)
1523cf1a1d3eSJames Smart 			pnvme_fcreq->cmdaddr)->sqe.common;
1524cf1a1d3eSJames Smart 		if (sqe->opcode == nvme_admin_keep_alive)
1525cf1a1d3eSJames Smart 			expedite = 1;
1526cf1a1d3eSJames Smart 	}
1527cf1a1d3eSJames Smart 
152801649561SJames Smart 	/* The node is shared with FCP IO, make sure the IO pending count does
152901649561SJames Smart 	 * not exceed the programmed depth.
153001649561SJames Smart 	 */
1531cf1a1d3eSJames Smart 	if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1532cf1a1d3eSJames Smart 	    !expedite) {
15334b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_qdepth);
1534cd22d605SDick Kennedy 		ret = -EBUSY;
153501649561SJames Smart 		goto out_fail;
153601649561SJames Smart 	}
153701649561SJames Smart 
1538cf1a1d3eSJames Smart 	lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, expedite);
153901649561SJames Smart 	if (lpfc_ncmd == NULL) {
15404b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_noxri);
154101649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
154201649561SJames Smart 				 "6065 driver's buffer pool is empty, "
154301649561SJames Smart 				 "IO failed\n");
1544cd22d605SDick Kennedy 		ret = -EBUSY;
154501649561SJames Smart 		goto out_fail;
154601649561SJames Smart 	}
1547bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1548c8a4ce0bSDick Kennedy 	if (start) {
1549bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_start = start;
1550bd2cdd5eSJames Smart 		lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1551c8a4ce0bSDick Kennedy 	} else {
1552c8a4ce0bSDick Kennedy 		lpfc_ncmd->ts_cmd_start = 0;
1553bd2cdd5eSJames Smart 	}
1554bd2cdd5eSJames Smart #endif
155501649561SJames Smart 
155601649561SJames Smart 	/*
155701649561SJames Smart 	 * Store the data needed by the driver to issue, abort, and complete
155801649561SJames Smart 	 * an IO.
155901649561SJames Smart 	 * Do not let the IO hang out forever.  There is no midlayer issuing
156001649561SJames Smart 	 * an abort so inform the FW of the maximum IO pending time.
156101649561SJames Smart 	 */
1562bbe3012bSJames Smart 	freqpriv->nvme_buf = lpfc_ncmd;
156301649561SJames Smart 	lpfc_ncmd->nvmeCmd = pnvme_fcreq;
156401649561SJames Smart 	lpfc_ncmd->nrport = rport;
1565318083adSJames Smart 	lpfc_ncmd->ndlp = ndlp;
156601649561SJames Smart 	lpfc_ncmd->start_time = jiffies;
156701649561SJames Smart 
156801649561SJames Smart 	/*
156901649561SJames Smart 	 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
157001649561SJames Smart 	 * This identfier was create in our hardware queue create callback
157101649561SJames Smart 	 * routine. The driver now is dependent on the IO queue steering from
157201649561SJames Smart 	 * the transport.  We are trusting the upper NVME layers know which
157301649561SJames Smart 	 * index to use and that they have affinitized a CPU to this hardware
157401649561SJames Smart 	 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
157501649561SJames Smart 	 */
157666a210ffSJames Smart 	idx = lpfc_queue_info->index;
157766a210ffSJames Smart 	lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
157866a210ffSJames Smart 	cstat = &lport->cstat[idx];
157966a210ffSJames Smart 
158066a210ffSJames Smart 	lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
158166a210ffSJames Smart 	ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
158266a210ffSJames Smart 	if (ret) {
158366a210ffSJames Smart 		ret = -ENOMEM;
158466a210ffSJames Smart 		goto out_free_nvme_buf;
158566a210ffSJames Smart 	}
158666a210ffSJames Smart 
158766a210ffSJames Smart 	atomic_inc(&ndlp->cmd_pending);
158801649561SJames Smart 
1589bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1590bd2cdd5eSJames Smart 			 lpfc_ncmd->cur_iocbq.sli4_xritag,
1591bd2cdd5eSJames Smart 			 lpfc_queue_info->index, ndlp->nlp_DID);
1592bd2cdd5eSJames Smart 
159301649561SJames Smart 	ret = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, &lpfc_ncmd->cur_iocbq);
159401649561SJames Smart 	if (ret) {
15954b056682SJames Smart 		atomic_inc(&lport->xmt_fcp_wqerr);
159601649561SJames Smart 		atomic_dec(&ndlp->cmd_pending);
1597e3246a12SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
159801649561SJames Smart 				 "6113 FCP could not issue WQE err %x "
159901649561SJames Smart 				 "sid: x%x did: x%x oxid: x%x\n",
160001649561SJames Smart 				 ret, vport->fc_myDID, ndlp->nlp_DID,
160101649561SJames Smart 				 lpfc_ncmd->cur_iocbq.sli4_xritag);
160201649561SJames Smart 		goto out_free_nvme_buf;
160301649561SJames Smart 	}
160401649561SJames Smart 
1605bd2cdd5eSJames Smart #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1606c8a4ce0bSDick Kennedy 	if (lpfc_ncmd->ts_cmd_start)
1607bd2cdd5eSJames Smart 		lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1608bd2cdd5eSJames Smart 
1609bd2cdd5eSJames Smart 	if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1610bd2cdd5eSJames Smart 		lpfc_ncmd->cpu = smp_processor_id();
1611bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu != lpfc_queue_info->index) {
1612bd2cdd5eSJames Smart 			/* Check for admin queue */
1613bd2cdd5eSJames Smart 			if (lpfc_queue_info->qidx) {
1614bd2cdd5eSJames Smart 				lpfc_printf_vlog(vport,
1615bd2cdd5eSJames Smart 						 KERN_ERR, LOG_NVME_IOERR,
1616bd2cdd5eSJames Smart 						"6702 CPU Check cmd: "
1617bd2cdd5eSJames Smart 						"cpu %d wq %d\n",
1618bd2cdd5eSJames Smart 						lpfc_ncmd->cpu,
1619bd2cdd5eSJames Smart 						lpfc_queue_info->index);
1620bd2cdd5eSJames Smart 			}
1621bd2cdd5eSJames Smart 			lpfc_ncmd->cpu = lpfc_queue_info->index;
1622bd2cdd5eSJames Smart 		}
1623bd2cdd5eSJames Smart 		if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1624bd2cdd5eSJames Smart 			phba->cpucheck_xmt_io[lpfc_ncmd->cpu]++;
1625bd2cdd5eSJames Smart 	}
1626bd2cdd5eSJames Smart #endif
162701649561SJames Smart 	return 0;
162801649561SJames Smart 
162901649561SJames Smart  out_free_nvme_buf:
16302cee7808SJames Smart 	if (lpfc_ncmd->nvmeCmd->sg_cnt) {
16312cee7808SJames Smart 		if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
163266a210ffSJames Smart 			atomic_dec(&cstat->fc4NvmeOutputRequests);
16332cee7808SJames Smart 		else
163466a210ffSJames Smart 			atomic_dec(&cstat->fc4NvmeInputRequests);
16352cee7808SJames Smart 	} else
163666a210ffSJames Smart 		atomic_dec(&cstat->fc4NvmeControlRequests);
163701649561SJames Smart 	lpfc_release_nvme_buf(phba, lpfc_ncmd);
163801649561SJames Smart  out_fail:
163901649561SJames Smart 	return ret;
164001649561SJames Smart }
164101649561SJames Smart 
164201649561SJames Smart /**
164301649561SJames Smart  * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
164401649561SJames Smart  * @phba: Pointer to HBA context object
164501649561SJames Smart  * @cmdiocb: Pointer to command iocb object.
164601649561SJames Smart  * @rspiocb: Pointer to response iocb object.
164701649561SJames Smart  *
164801649561SJames Smart  * This is the callback function for any NVME FCP IO that was aborted.
164901649561SJames Smart  *
165001649561SJames Smart  * Return value:
165101649561SJames Smart  *   None
165201649561SJames Smart  **/
165301649561SJames Smart void
165401649561SJames Smart lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
165501649561SJames Smart 			   struct lpfc_wcqe_complete *abts_cmpl)
165601649561SJames Smart {
1657e3246a12SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
165801649561SJames Smart 			"6145 ABORT_XRI_CN completing on rpi x%x "
165901649561SJames Smart 			"original iotag x%x, abort cmd iotag x%x "
166001649561SJames Smart 			"req_tag x%x, status x%x, hwstatus x%x\n",
166101649561SJames Smart 			cmdiocb->iocb.un.acxri.abortContextTag,
166201649561SJames Smart 			cmdiocb->iocb.un.acxri.abortIoTag,
166301649561SJames Smart 			cmdiocb->iotag,
166401649561SJames Smart 			bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
166501649561SJames Smart 			bf_get(lpfc_wcqe_c_status, abts_cmpl),
166601649561SJames Smart 			bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
166701649561SJames Smart 	lpfc_sli_release_iocbq(phba, cmdiocb);
166801649561SJames Smart }
166901649561SJames Smart 
167001649561SJames Smart /**
167101649561SJames Smart  * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
167201649561SJames Smart  * @lpfc_pnvme: Pointer to the driver's nvme instance data
167301649561SJames Smart  * @lpfc_nvme_lport: Pointer to the driver's local port data
167401649561SJames Smart  * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
167501649561SJames Smart  * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
167601649561SJames Smart  * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
167701649561SJames Smart  *
167801649561SJames Smart  * Driver registers this routine as its nvme request io abort handler.  This
167901649561SJames Smart  * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
168001649561SJames Smart  * data structure to the rport indicated in @lpfc_nvme_rport.  This routine
168101649561SJames Smart  * is executed asynchronously - one the target is validated as "MAPPED" and
168201649561SJames Smart  * ready for IO, the driver issues the abort request and returns.
168301649561SJames Smart  *
168401649561SJames Smart  * Return value:
168501649561SJames Smart  *   None
168601649561SJames Smart  **/
168701649561SJames Smart static void
168801649561SJames Smart lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
168901649561SJames Smart 		    struct nvme_fc_remote_port *pnvme_rport,
169001649561SJames Smart 		    void *hw_queue_handle,
169101649561SJames Smart 		    struct nvmefc_fcp_req *pnvme_fcreq)
169201649561SJames Smart {
169301649561SJames Smart 	struct lpfc_nvme_lport *lport;
169401649561SJames Smart 	struct lpfc_vport *vport;
169501649561SJames Smart 	struct lpfc_hba *phba;
169601649561SJames Smart 	struct lpfc_nvme_buf *lpfc_nbuf;
169701649561SJames Smart 	struct lpfc_iocbq *abts_buf;
169801649561SJames Smart 	struct lpfc_iocbq *nvmereq_wqe;
1699c3725bdcSJames Smart 	struct lpfc_nvme_fcpreq_priv *freqpriv;
1700205e8240SJames Smart 	union lpfc_wqe128 *abts_wqe;
170101649561SJames Smart 	unsigned long flags;
170201649561SJames Smart 	int ret_val;
170301649561SJames Smart 
1704c3725bdcSJames Smart 	/* Validate pointers. LLDD fault handling with transport does
1705c3725bdcSJames Smart 	 * have timing races.
1706c3725bdcSJames Smart 	 */
170701649561SJames Smart 	lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1708c3725bdcSJames Smart 	if (unlikely(!lport))
1709c3725bdcSJames Smart 		return;
171001649561SJames Smart 
1711c3725bdcSJames Smart 	vport = lport->vport;
1712c3725bdcSJames Smart 
1713c3725bdcSJames Smart 	if (unlikely(!hw_queue_handle)) {
1714c3725bdcSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1715c3725bdcSJames Smart 				 "6129 Fail Abort, HW Queue Handle NULL.\n");
1716c3725bdcSJames Smart 		return;
1717c3725bdcSJames Smart 	}
1718c3725bdcSJames Smart 
1719c3725bdcSJames Smart 	phba = vport->phba;
1720c3725bdcSJames Smart 	freqpriv = pnvme_fcreq->private;
1721c3725bdcSJames Smart 
1722c3725bdcSJames Smart 	if (unlikely(!freqpriv))
1723c3725bdcSJames Smart 		return;
17243386f4bdSJames Smart 	if (vport->load_flag & FC_UNLOADING)
17253386f4bdSJames Smart 		return;
17263386f4bdSJames Smart 
172701649561SJames Smart 	/* Announce entry to new IO submit field. */
172886c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
172901649561SJames Smart 			 "6002 Abort Request to rport DID x%06x "
173001649561SJames Smart 			 "for nvme_fc_req %p\n",
173101649561SJames Smart 			 pnvme_rport->port_id,
173201649561SJames Smart 			 pnvme_fcreq);
173301649561SJames Smart 
173401649561SJames Smart 	/* If the hba is getting reset, this flag is set.  It is
173501649561SJames Smart 	 * cleared when the reset is complete and rings reestablished.
173601649561SJames Smart 	 */
173701649561SJames Smart 	spin_lock_irqsave(&phba->hbalock, flags);
173801649561SJames Smart 	/* driver queued commands are in process of being flushed */
173901649561SJames Smart 	if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
174001649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
174186c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
174201649561SJames Smart 				 "6139 Driver in reset cleanup - flushing "
174301649561SJames Smart 				 "NVME Req now.  hba_flag x%x\n",
174401649561SJames Smart 				 phba->hba_flag);
174501649561SJames Smart 		return;
174601649561SJames Smart 	}
174701649561SJames Smart 
1748bbe3012bSJames Smart 	lpfc_nbuf = freqpriv->nvme_buf;
174901649561SJames Smart 	if (!lpfc_nbuf) {
175001649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
175186c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
175201649561SJames Smart 				 "6140 NVME IO req has no matching lpfc nvme "
175301649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
175401649561SJames Smart 		return;
175501649561SJames Smart 	} else if (!lpfc_nbuf->nvmeCmd) {
175601649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
175786c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
175801649561SJames Smart 				 "6141 lpfc NVME IO req has no nvme_fcreq "
175901649561SJames Smart 				 "io buffer.  Skipping abort req.\n");
176001649561SJames Smart 		return;
176101649561SJames Smart 	}
17622b7824d0SJames Smart 	nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
176301649561SJames Smart 
176401649561SJames Smart 	/*
176501649561SJames Smart 	 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
176601649561SJames Smart 	 * state must match the nvme_fcreq passed by the nvme
176701649561SJames Smart 	 * transport.  If they don't match, it is likely the driver
176801649561SJames Smart 	 * has already completed the NVME IO and the nvme transport
176901649561SJames Smart 	 * has not seen it yet.
177001649561SJames Smart 	 */
177101649561SJames Smart 	if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
177201649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
177386c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
177401649561SJames Smart 				 "6143 NVME req mismatch: "
177501649561SJames Smart 				 "lpfc_nbuf %p nvmeCmd %p, "
17762b7824d0SJames Smart 				 "pnvme_fcreq %p.  Skipping Abort xri x%x\n",
177701649561SJames Smart 				 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
17782b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
177901649561SJames Smart 		return;
178001649561SJames Smart 	}
178101649561SJames Smart 
178201649561SJames Smart 	/* Don't abort IOs no longer on the pending queue. */
178301649561SJames Smart 	if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
178401649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
178586c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
178601649561SJames Smart 				 "6142 NVME IO req %p not queued - skipping "
17872b7824d0SJames Smart 				 "abort req xri x%x\n",
17882b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
178901649561SJames Smart 		return;
179001649561SJames Smart 	}
179101649561SJames Smart 
17924b056682SJames Smart 	atomic_inc(&lport->xmt_fcp_abort);
1793bd2cdd5eSJames Smart 	lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1794bd2cdd5eSJames Smart 			 nvmereq_wqe->sli4_xritag,
179500cefeb9SJames Smart 			 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1796bd2cdd5eSJames Smart 
179701649561SJames Smart 	/* Outstanding abort is in progress */
179801649561SJames Smart 	if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
179901649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
180086c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
180101649561SJames Smart 				 "6144 Outstanding NVME I/O Abort Request "
180201649561SJames Smart 				 "still pending on nvme_fcreq %p, "
18032b7824d0SJames Smart 				 "lpfc_ncmd %p xri x%x\n",
18042b7824d0SJames Smart 				 pnvme_fcreq, lpfc_nbuf,
18052b7824d0SJames Smart 				 nvmereq_wqe->sli4_xritag);
180601649561SJames Smart 		return;
180701649561SJames Smart 	}
180801649561SJames Smart 
180901649561SJames Smart 	abts_buf = __lpfc_sli_get_iocbq(phba);
181001649561SJames Smart 	if (!abts_buf) {
181101649561SJames Smart 		spin_unlock_irqrestore(&phba->hbalock, flags);
181286c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
181301649561SJames Smart 				 "6136 No available abort wqes. Skipping "
18142b7824d0SJames Smart 				 "Abts req for nvme_fcreq %p xri x%x\n",
18152b7824d0SJames Smart 				 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
181601649561SJames Smart 		return;
181701649561SJames Smart 	}
181801649561SJames Smart 
181901649561SJames Smart 	/* Ready - mark outstanding as aborted by driver. */
182001649561SJames Smart 	nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
182101649561SJames Smart 
182201649561SJames Smart 	/* Complete prepping the abort wqe and issue to the FW. */
182301649561SJames Smart 	abts_wqe = &abts_buf->wqe;
182401649561SJames Smart 
182501649561SJames Smart 	/* WQEs are reused.  Clear stale data and set key fields to
182601649561SJames Smart 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
182701649561SJames Smart 	 */
182801649561SJames Smart 	memset(abts_wqe, 0, sizeof(union lpfc_wqe));
182901649561SJames Smart 	bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
183001649561SJames Smart 
183101649561SJames Smart 	/* word 7 */
183201649561SJames Smart 	bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
183301649561SJames Smart 	bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
183401649561SJames Smart 	bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
183501649561SJames Smart 	       nvmereq_wqe->iocb.ulpClass);
183601649561SJames Smart 
183701649561SJames Smart 	/* word 8 - tell the FW to abort the IO associated with this
183801649561SJames Smart 	 * outstanding exchange ID.
183901649561SJames Smart 	 */
184001649561SJames Smart 	abts_wqe->abort_cmd.wqe_com.abort_tag = nvmereq_wqe->sli4_xritag;
184101649561SJames Smart 
184201649561SJames Smart 	/* word 9 - this is the iotag for the abts_wqe completion. */
184301649561SJames Smart 	bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
184401649561SJames Smart 	       abts_buf->iotag);
184501649561SJames Smart 
184601649561SJames Smart 	/* word 10 */
184701649561SJames Smart 	bf_set(wqe_wqid, &abts_wqe->abort_cmd.wqe_com, nvmereq_wqe->hba_wqidx);
184801649561SJames Smart 	bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
184901649561SJames Smart 	bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
185001649561SJames Smart 
185101649561SJames Smart 	/* word 11 */
185201649561SJames Smart 	bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
185301649561SJames Smart 	bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
185401649561SJames Smart 	bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
185501649561SJames Smart 
185601649561SJames Smart 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
185701649561SJames Smart 	abts_buf->iocb_flag |= LPFC_IO_NVME;
185801649561SJames Smart 	abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
185901649561SJames Smart 	abts_buf->vport = vport;
186001649561SJames Smart 	abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
186101649561SJames Smart 	ret_val = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_buf);
186201649561SJames Smart 	spin_unlock_irqrestore(&phba->hbalock, flags);
1863cd22d605SDick Kennedy 	if (ret_val) {
186486c67379SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
186501649561SJames Smart 				 "6137 Failed abts issue_wqe with status x%x "
186601649561SJames Smart 				 "for nvme_fcreq %p.\n",
186701649561SJames Smart 				 ret_val, pnvme_fcreq);
186801649561SJames Smart 		lpfc_sli_release_iocbq(phba, abts_buf);
186901649561SJames Smart 		return;
187001649561SJames Smart 	}
187101649561SJames Smart 
187286c67379SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
18732b7824d0SJames Smart 			 "6138 Transport Abort NVME Request Issued for "
187401649561SJames Smart 			 "ox_id x%x on reqtag x%x\n",
187501649561SJames Smart 			 nvmereq_wqe->sli4_xritag,
187601649561SJames Smart 			 abts_buf->iotag);
187701649561SJames Smart }
187801649561SJames Smart 
187901649561SJames Smart /* Declare and initialization an instance of the FC NVME template. */
188001649561SJames Smart static struct nvme_fc_port_template lpfc_nvme_template = {
188101649561SJames Smart 	/* initiator-based functions */
188201649561SJames Smart 	.localport_delete  = lpfc_nvme_localport_delete,
188301649561SJames Smart 	.remoteport_delete = lpfc_nvme_remoteport_delete,
188401649561SJames Smart 	.create_queue = lpfc_nvme_create_queue,
188501649561SJames Smart 	.delete_queue = lpfc_nvme_delete_queue,
188601649561SJames Smart 	.ls_req       = lpfc_nvme_ls_req,
188701649561SJames Smart 	.fcp_io       = lpfc_nvme_fcp_io_submit,
188801649561SJames Smart 	.ls_abort     = lpfc_nvme_ls_abort,
188901649561SJames Smart 	.fcp_abort    = lpfc_nvme_fcp_abort,
189001649561SJames Smart 
189101649561SJames Smart 	.max_hw_queues = 1,
189201649561SJames Smart 	.max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
189301649561SJames Smart 	.max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
189401649561SJames Smart 	.dma_boundary = 0xFFFFFFFF,
189501649561SJames Smart 
189601649561SJames Smart 	/* Sizes of additional private data for data structures.
189701649561SJames Smart 	 * No use for the last two sizes at this time.
189801649561SJames Smart 	 */
189901649561SJames Smart 	.local_priv_sz = sizeof(struct lpfc_nvme_lport),
190001649561SJames Smart 	.remote_priv_sz = sizeof(struct lpfc_nvme_rport),
190101649561SJames Smart 	.lsrqst_priv_sz = 0,
1902bbe3012bSJames Smart 	.fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
190301649561SJames Smart };
190401649561SJames Smart 
190501649561SJames Smart /**
190601649561SJames Smart  * lpfc_sli4_post_nvme_sgl_block - post a block of nvme sgl list to firmware
190701649561SJames Smart  * @phba: pointer to lpfc hba data structure.
190801649561SJames Smart  * @nblist: pointer to nvme buffer list.
190901649561SJames Smart  * @count: number of scsi buffers on the list.
191001649561SJames Smart  *
191101649561SJames Smart  * This routine is invoked to post a block of @count scsi sgl pages from a
191201649561SJames Smart  * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
191301649561SJames Smart  * No Lock is held.
191401649561SJames Smart  *
191501649561SJames Smart  **/
191601649561SJames Smart static int
191701649561SJames Smart lpfc_sli4_post_nvme_sgl_block(struct lpfc_hba *phba,
191801649561SJames Smart 			      struct list_head *nblist,
191901649561SJames Smart 			      int count)
192001649561SJames Smart {
192101649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd;
192201649561SJames Smart 	struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
192301649561SJames Smart 	struct sgl_page_pairs *sgl_pg_pairs;
192401649561SJames Smart 	void *viraddr;
192501649561SJames Smart 	LPFC_MBOXQ_t *mbox;
192601649561SJames Smart 	uint32_t reqlen, alloclen, pg_pairs;
192701649561SJames Smart 	uint32_t mbox_tmo;
192801649561SJames Smart 	uint16_t xritag_start = 0;
192901649561SJames Smart 	int rc = 0;
193001649561SJames Smart 	uint32_t shdr_status, shdr_add_status;
193101649561SJames Smart 	dma_addr_t pdma_phys_bpl1;
193201649561SJames Smart 	union lpfc_sli4_cfg_shdr *shdr;
193301649561SJames Smart 
193401649561SJames Smart 	/* Calculate the requested length of the dma memory */
193501649561SJames Smart 	reqlen = count * sizeof(struct sgl_page_pairs) +
193601649561SJames Smart 		 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
193701649561SJames Smart 	if (reqlen > SLI4_PAGE_SIZE) {
193801649561SJames Smart 		lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
193901649561SJames Smart 				"6118 Block sgl registration required DMA "
194001649561SJames Smart 				"size (%d) great than a page\n", reqlen);
194101649561SJames Smart 		return -ENOMEM;
194201649561SJames Smart 	}
194301649561SJames Smart 	mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
194401649561SJames Smart 	if (!mbox) {
194501649561SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
194601649561SJames Smart 				"6119 Failed to allocate mbox cmd memory\n");
194701649561SJames Smart 		return -ENOMEM;
194801649561SJames Smart 	}
194901649561SJames Smart 
195001649561SJames Smart 	/* Allocate DMA memory and set up the non-embedded mailbox command */
195101649561SJames Smart 	alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
195201649561SJames Smart 				LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
195301649561SJames Smart 				LPFC_SLI4_MBX_NEMBED);
195401649561SJames Smart 
195501649561SJames Smart 	if (alloclen < reqlen) {
195601649561SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
195701649561SJames Smart 				"6120 Allocated DMA memory size (%d) is "
195801649561SJames Smart 				"less than the requested DMA memory "
195901649561SJames Smart 				"size (%d)\n", alloclen, reqlen);
196001649561SJames Smart 		lpfc_sli4_mbox_cmd_free(phba, mbox);
196101649561SJames Smart 		return -ENOMEM;
196201649561SJames Smart 	}
196301649561SJames Smart 
196401649561SJames Smart 	/* Get the first SGE entry from the non-embedded DMA memory */
196501649561SJames Smart 	viraddr = mbox->sge_array->addr[0];
196601649561SJames Smart 
196701649561SJames Smart 	/* Set up the SGL pages in the non-embedded DMA pages */
196801649561SJames Smart 	sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
196901649561SJames Smart 	sgl_pg_pairs = &sgl->sgl_pg_pairs;
197001649561SJames Smart 
197101649561SJames Smart 	pg_pairs = 0;
197201649561SJames Smart 	list_for_each_entry(lpfc_ncmd, nblist, list) {
197301649561SJames Smart 		/* Set up the sge entry */
197401649561SJames Smart 		sgl_pg_pairs->sgl_pg0_addr_lo =
197501649561SJames Smart 			cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
197601649561SJames Smart 		sgl_pg_pairs->sgl_pg0_addr_hi =
197701649561SJames Smart 			cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
197801649561SJames Smart 		if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
197901649561SJames Smart 			pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
198001649561SJames Smart 						SGL_PAGE_SIZE;
198101649561SJames Smart 		else
198201649561SJames Smart 			pdma_phys_bpl1 = 0;
198301649561SJames Smart 		sgl_pg_pairs->sgl_pg1_addr_lo =
198401649561SJames Smart 			cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
198501649561SJames Smart 		sgl_pg_pairs->sgl_pg1_addr_hi =
198601649561SJames Smart 			cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
198701649561SJames Smart 		/* Keep the first xritag on the list */
198801649561SJames Smart 		if (pg_pairs == 0)
198901649561SJames Smart 			xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
199001649561SJames Smart 		sgl_pg_pairs++;
199101649561SJames Smart 		pg_pairs++;
199201649561SJames Smart 	}
199301649561SJames Smart 	bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
199401649561SJames Smart 	bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
199501649561SJames Smart 	/* Perform endian conversion if necessary */
199601649561SJames Smart 	sgl->word0 = cpu_to_le32(sgl->word0);
199701649561SJames Smart 
199801649561SJames Smart 	if (!phba->sli4_hba.intr_enable)
199901649561SJames Smart 		rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
200001649561SJames Smart 	else {
200101649561SJames Smart 		mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
200201649561SJames Smart 		rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
200301649561SJames Smart 	}
200401649561SJames Smart 	shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
200501649561SJames Smart 	shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
200601649561SJames Smart 	shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
200701649561SJames Smart 	if (rc != MBX_TIMEOUT)
200801649561SJames Smart 		lpfc_sli4_mbox_cmd_free(phba, mbox);
200901649561SJames Smart 	if (shdr_status || shdr_add_status || rc) {
201001649561SJames Smart 		lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
201101649561SJames Smart 				"6125 POST_SGL_BLOCK mailbox command failed "
201201649561SJames Smart 				"status x%x add_status x%x mbx status x%x\n",
201301649561SJames Smart 				shdr_status, shdr_add_status, rc);
201401649561SJames Smart 		rc = -ENXIO;
201501649561SJames Smart 	}
201601649561SJames Smart 	return rc;
201701649561SJames Smart }
201801649561SJames Smart 
201901649561SJames Smart /**
202001649561SJames Smart  * lpfc_post_nvme_sgl_list - Post blocks of nvme buffer sgls from a list
202101649561SJames Smart  * @phba: pointer to lpfc hba data structure.
202201649561SJames Smart  * @post_nblist: pointer to the nvme buffer list.
202301649561SJames Smart  *
202401649561SJames Smart  * This routine walks a list of nvme buffers that was passed in. It attempts
202501649561SJames Smart  * to construct blocks of nvme buffer sgls which contains contiguous xris and
202601649561SJames Smart  * uses the non-embedded SGL block post mailbox commands to post to the port.
202701649561SJames Smart  * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
202801649561SJames Smart  * embedded SGL post mailbox command for posting. The @post_nblist passed in
202901649561SJames Smart  * must be local list, thus no lock is needed when manipulate the list.
203001649561SJames Smart  *
203101649561SJames Smart  * Returns: 0 = failure, non-zero number of successfully posted buffers.
203201649561SJames Smart  **/
203301649561SJames Smart static int
203401649561SJames Smart lpfc_post_nvme_sgl_list(struct lpfc_hba *phba,
203501649561SJames Smart 			     struct list_head *post_nblist, int sb_count)
203601649561SJames Smart {
203701649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
203801649561SJames Smart 	int status, sgl_size;
203901649561SJames Smart 	int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
204001649561SJames Smart 	dma_addr_t pdma_phys_sgl1;
204101649561SJames Smart 	int last_xritag = NO_XRI;
204201649561SJames Smart 	int cur_xritag;
204301649561SJames Smart 	LIST_HEAD(prep_nblist);
204401649561SJames Smart 	LIST_HEAD(blck_nblist);
204501649561SJames Smart 	LIST_HEAD(nvme_nblist);
204601649561SJames Smart 
204701649561SJames Smart 	/* sanity check */
204801649561SJames Smart 	if (sb_count <= 0)
204901649561SJames Smart 		return -EINVAL;
205001649561SJames Smart 
205101649561SJames Smart 	sgl_size = phba->cfg_sg_dma_buf_size;
205201649561SJames Smart 
205301649561SJames Smart 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
205401649561SJames Smart 		list_del_init(&lpfc_ncmd->list);
205501649561SJames Smart 		block_cnt++;
205601649561SJames Smart 		if ((last_xritag != NO_XRI) &&
205701649561SJames Smart 		    (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
205801649561SJames Smart 			/* a hole in xri block, form a sgl posting block */
205901649561SJames Smart 			list_splice_init(&prep_nblist, &blck_nblist);
206001649561SJames Smart 			post_cnt = block_cnt - 1;
206101649561SJames Smart 			/* prepare list for next posting block */
206201649561SJames Smart 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
206301649561SJames Smart 			block_cnt = 1;
206401649561SJames Smart 		} else {
206501649561SJames Smart 			/* prepare list for next posting block */
206601649561SJames Smart 			list_add_tail(&lpfc_ncmd->list, &prep_nblist);
206701649561SJames Smart 			/* enough sgls for non-embed sgl mbox command */
206801649561SJames Smart 			if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
206901649561SJames Smart 				list_splice_init(&prep_nblist, &blck_nblist);
207001649561SJames Smart 				post_cnt = block_cnt;
207101649561SJames Smart 				block_cnt = 0;
207201649561SJames Smart 			}
207301649561SJames Smart 		}
207401649561SJames Smart 		num_posting++;
207501649561SJames Smart 		last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
207601649561SJames Smart 
207701649561SJames Smart 		/* end of repost sgl list condition for NVME buffers */
207801649561SJames Smart 		if (num_posting == sb_count) {
207901649561SJames Smart 			if (post_cnt == 0) {
208001649561SJames Smart 				/* last sgl posting block */
208101649561SJames Smart 				list_splice_init(&prep_nblist, &blck_nblist);
208201649561SJames Smart 				post_cnt = block_cnt;
208301649561SJames Smart 			} else if (block_cnt == 1) {
208401649561SJames Smart 				/* last single sgl with non-contiguous xri */
208501649561SJames Smart 				if (sgl_size > SGL_PAGE_SIZE)
208601649561SJames Smart 					pdma_phys_sgl1 =
208701649561SJames Smart 						lpfc_ncmd->dma_phys_sgl +
208801649561SJames Smart 						SGL_PAGE_SIZE;
208901649561SJames Smart 				else
209001649561SJames Smart 					pdma_phys_sgl1 = 0;
209101649561SJames Smart 				cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
209201649561SJames Smart 				status = lpfc_sli4_post_sgl(phba,
209301649561SJames Smart 						lpfc_ncmd->dma_phys_sgl,
209401649561SJames Smart 						pdma_phys_sgl1, cur_xritag);
209501649561SJames Smart 				if (status) {
209601649561SJames Smart 					/* failure, put on abort nvme list */
2097318083adSJames Smart 					lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
209801649561SJames Smart 				} else {
209901649561SJames Smart 					/* success, put on NVME buffer list */
2100318083adSJames Smart 					lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
210101649561SJames Smart 					lpfc_ncmd->status = IOSTAT_SUCCESS;
210201649561SJames Smart 					num_posted++;
210301649561SJames Smart 				}
210401649561SJames Smart 				/* success, put on NVME buffer sgl list */
210501649561SJames Smart 				list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
210601649561SJames Smart 			}
210701649561SJames Smart 		}
210801649561SJames Smart 
210901649561SJames Smart 		/* continue until a nembed page worth of sgls */
211001649561SJames Smart 		if (post_cnt == 0)
211101649561SJames Smart 			continue;
211201649561SJames Smart 
211301649561SJames Smart 		/* post block of NVME buffer list sgls */
211401649561SJames Smart 		status = lpfc_sli4_post_nvme_sgl_block(phba, &blck_nblist,
211501649561SJames Smart 						       post_cnt);
211601649561SJames Smart 
211701649561SJames Smart 		/* don't reset xirtag due to hole in xri block */
211801649561SJames Smart 		if (block_cnt == 0)
211901649561SJames Smart 			last_xritag = NO_XRI;
212001649561SJames Smart 
212101649561SJames Smart 		/* reset NVME buffer post count for next round of posting */
212201649561SJames Smart 		post_cnt = 0;
212301649561SJames Smart 
212401649561SJames Smart 		/* put posted NVME buffer-sgl posted on NVME buffer sgl list */
212501649561SJames Smart 		while (!list_empty(&blck_nblist)) {
212601649561SJames Smart 			list_remove_head(&blck_nblist, lpfc_ncmd,
212701649561SJames Smart 					 struct lpfc_nvme_buf, list);
212801649561SJames Smart 			if (status) {
212901649561SJames Smart 				/* failure, put on abort nvme list */
2130318083adSJames Smart 				lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
213101649561SJames Smart 			} else {
213201649561SJames Smart 				/* success, put on NVME buffer list */
2133318083adSJames Smart 				lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
213401649561SJames Smart 				lpfc_ncmd->status = IOSTAT_SUCCESS;
213501649561SJames Smart 				num_posted++;
213601649561SJames Smart 			}
213701649561SJames Smart 			list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
213801649561SJames Smart 		}
213901649561SJames Smart 	}
214001649561SJames Smart 	/* Push NVME buffers with sgl posted to the available list */
214101649561SJames Smart 	while (!list_empty(&nvme_nblist)) {
214201649561SJames Smart 		list_remove_head(&nvme_nblist, lpfc_ncmd,
214301649561SJames Smart 				 struct lpfc_nvme_buf, list);
214401649561SJames Smart 		lpfc_release_nvme_buf(phba, lpfc_ncmd);
214501649561SJames Smart 	}
214601649561SJames Smart 	return num_posted;
214701649561SJames Smart }
214801649561SJames Smart 
214901649561SJames Smart /**
215001649561SJames Smart  * lpfc_repost_nvme_sgl_list - Repost all the allocated nvme buffer sgls
215101649561SJames Smart  * @phba: pointer to lpfc hba data structure.
215201649561SJames Smart  *
215301649561SJames Smart  * This routine walks the list of nvme buffers that have been allocated and
215401649561SJames Smart  * repost them to the port by using SGL block post. This is needed after a
215501649561SJames Smart  * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
215601649561SJames Smart  * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
215701649561SJames Smart  * to the lpfc_nvme_buf_list. If the repost fails, reject all nvme buffers.
215801649561SJames Smart  *
215901649561SJames Smart  * Returns: 0 = success, non-zero failure.
216001649561SJames Smart  **/
216101649561SJames Smart int
216201649561SJames Smart lpfc_repost_nvme_sgl_list(struct lpfc_hba *phba)
216301649561SJames Smart {
216401649561SJames Smart 	LIST_HEAD(post_nblist);
216501649561SJames Smart 	int num_posted, rc = 0;
216601649561SJames Smart 
216701649561SJames Smart 	/* get all NVME buffers need to repost to a local list */
216801649561SJames Smart 	spin_lock_irq(&phba->nvme_buf_list_get_lock);
216901649561SJames Smart 	spin_lock(&phba->nvme_buf_list_put_lock);
217001649561SJames Smart 	list_splice_init(&phba->lpfc_nvme_buf_list_get, &post_nblist);
217101649561SJames Smart 	list_splice(&phba->lpfc_nvme_buf_list_put, &post_nblist);
2172cf1a1d3eSJames Smart 	phba->get_nvme_bufs = 0;
2173cf1a1d3eSJames Smart 	phba->put_nvme_bufs = 0;
217401649561SJames Smart 	spin_unlock(&phba->nvme_buf_list_put_lock);
217501649561SJames Smart 	spin_unlock_irq(&phba->nvme_buf_list_get_lock);
217601649561SJames Smart 
217701649561SJames Smart 	/* post the list of nvme buffer sgls to port if available */
217801649561SJames Smart 	if (!list_empty(&post_nblist)) {
217901649561SJames Smart 		num_posted = lpfc_post_nvme_sgl_list(phba, &post_nblist,
218001649561SJames Smart 						phba->sli4_hba.nvme_xri_cnt);
218101649561SJames Smart 		/* failed to post any nvme buffer, return error */
218201649561SJames Smart 		if (num_posted == 0)
218301649561SJames Smart 			rc = -EIO;
218401649561SJames Smart 	}
218501649561SJames Smart 	return rc;
218601649561SJames Smart }
218701649561SJames Smart 
218801649561SJames Smart /**
218901649561SJames Smart  * lpfc_new_nvme_buf - Scsi buffer allocator for HBA with SLI4 IF spec
219001649561SJames Smart  * @vport: The virtual port for which this call being executed.
219101649561SJames Smart  * @num_to_allocate: The requested number of buffers to allocate.
219201649561SJames Smart  *
219301649561SJames Smart  * This routine allocates nvme buffers for device with SLI-4 interface spec,
219401649561SJames Smart  * the nvme buffer contains all the necessary information needed to initiate
219501649561SJames Smart  * a NVME I/O. After allocating up to @num_to_allocate NVME buffers and put
219601649561SJames Smart  * them on a list, it post them to the port by using SGL block post.
219701649561SJames Smart  *
219801649561SJames Smart  * Return codes:
219901649561SJames Smart  *   int - number of nvme buffers that were allocated and posted.
220001649561SJames Smart  *   0 = failure, less than num_to_alloc is a partial failure.
220101649561SJames Smart  **/
220201649561SJames Smart static int
220301649561SJames Smart lpfc_new_nvme_buf(struct lpfc_vport *vport, int num_to_alloc)
220401649561SJames Smart {
220501649561SJames Smart 	struct lpfc_hba *phba = vport->phba;
220601649561SJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd;
220701649561SJames Smart 	struct lpfc_iocbq *pwqeq;
220801649561SJames Smart 	union lpfc_wqe128 *wqe;
220901649561SJames Smart 	struct sli4_sge *sgl;
221001649561SJames Smart 	dma_addr_t pdma_phys_sgl;
221101649561SJames Smart 	uint16_t iotag, lxri = 0;
221201649561SJames Smart 	int bcnt, num_posted, sgl_size;
221301649561SJames Smart 	LIST_HEAD(prep_nblist);
221401649561SJames Smart 	LIST_HEAD(post_nblist);
221501649561SJames Smart 	LIST_HEAD(nvme_nblist);
221601649561SJames Smart 
221701649561SJames Smart 	sgl_size = phba->cfg_sg_dma_buf_size;
221801649561SJames Smart 
221901649561SJames Smart 	for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
222001649561SJames Smart 		lpfc_ncmd = kzalloc(sizeof(struct lpfc_nvme_buf), GFP_KERNEL);
222101649561SJames Smart 		if (!lpfc_ncmd)
222201649561SJames Smart 			break;
222301649561SJames Smart 		/*
222401649561SJames Smart 		 * Get memory from the pci pool to map the virt space to
222501649561SJames Smart 		 * pci bus space for an I/O. The DMA buffer includes the
222601649561SJames Smart 		 * number of SGE's necessary to support the sg_tablesize.
222701649561SJames Smart 		 */
2228df2f7729SThomas Meyer 		lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
222901649561SJames Smart 						  GFP_KERNEL,
223001649561SJames Smart 						  &lpfc_ncmd->dma_handle);
223101649561SJames Smart 		if (!lpfc_ncmd->data) {
223201649561SJames Smart 			kfree(lpfc_ncmd);
223301649561SJames Smart 			break;
223401649561SJames Smart 		}
223501649561SJames Smart 
223601649561SJames Smart 		lxri = lpfc_sli4_next_xritag(phba);
223701649561SJames Smart 		if (lxri == NO_XRI) {
2238771db5c0SRomain Perier 			dma_pool_free(phba->lpfc_sg_dma_buf_pool,
223901649561SJames Smart 				      lpfc_ncmd->data, lpfc_ncmd->dma_handle);
224001649561SJames Smart 			kfree(lpfc_ncmd);
224101649561SJames Smart 			break;
224201649561SJames Smart 		}
224301649561SJames Smart 		pwqeq = &(lpfc_ncmd->cur_iocbq);
2244205e8240SJames Smart 		wqe = &pwqeq->wqe;
224501649561SJames Smart 
224601649561SJames Smart 		/* Allocate iotag for lpfc_ncmd->cur_iocbq. */
224701649561SJames Smart 		iotag = lpfc_sli_next_iotag(phba, pwqeq);
224801649561SJames Smart 		if (iotag == 0) {
2249771db5c0SRomain Perier 			dma_pool_free(phba->lpfc_sg_dma_buf_pool,
225001649561SJames Smart 				      lpfc_ncmd->data, lpfc_ncmd->dma_handle);
225101649561SJames Smart 			kfree(lpfc_ncmd);
225201649561SJames Smart 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
225301649561SJames Smart 					"6121 Failed to allocated IOTAG for"
225401649561SJames Smart 					" XRI:0x%x\n", lxri);
225501649561SJames Smart 			lpfc_sli4_free_xri(phba, lxri);
225601649561SJames Smart 			break;
225701649561SJames Smart 		}
225801649561SJames Smart 		pwqeq->sli4_lxritag = lxri;
225901649561SJames Smart 		pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
226001649561SJames Smart 		pwqeq->iocb_flag |= LPFC_IO_NVME;
226101649561SJames Smart 		pwqeq->context1 = lpfc_ncmd;
226201649561SJames Smart 		pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
226301649561SJames Smart 
226401649561SJames Smart 		/* Initialize local short-hand pointers. */
226501649561SJames Smart 		lpfc_ncmd->nvme_sgl = lpfc_ncmd->data;
226601649561SJames Smart 		sgl = lpfc_ncmd->nvme_sgl;
226701649561SJames Smart 		pdma_phys_sgl = lpfc_ncmd->dma_handle;
226801649561SJames Smart 		lpfc_ncmd->dma_phys_sgl = pdma_phys_sgl;
226901649561SJames Smart 
227001649561SJames Smart 		/* Rsp SGE will be filled in when we rcv an IO
227101649561SJames Smart 		 * from the NVME Layer to be sent.
227201649561SJames Smart 		 * The cmd is going to be embedded so we need a SKIP SGE.
227301649561SJames Smart 		 */
227401649561SJames Smart 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
227501649561SJames Smart 		bf_set(lpfc_sli4_sge_last, sgl, 0);
227601649561SJames Smart 		sgl->word2 = cpu_to_le32(sgl->word2);
227701649561SJames Smart 		/* Fill in word 3 / sgl_len during cmd submission */
227801649561SJames Smart 
227901649561SJames Smart 		lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
228001649561SJames Smart 
22815fd11085SJames Smart 		/* Initialize WQE */
22825fd11085SJames Smart 		memset(wqe, 0, sizeof(union lpfc_wqe));
228301649561SJames Smart 
228401649561SJames Smart 		/* add the nvme buffer to a post list */
228501649561SJames Smart 		list_add_tail(&lpfc_ncmd->list, &post_nblist);
228601649561SJames Smart 		spin_lock_irq(&phba->nvme_buf_list_get_lock);
228701649561SJames Smart 		phba->sli4_hba.nvme_xri_cnt++;
228801649561SJames Smart 		spin_unlock_irq(&phba->nvme_buf_list_get_lock);
228901649561SJames Smart 	}
229001649561SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
229101649561SJames Smart 			"6114 Allocate %d out of %d requested new NVME "
229201649561SJames Smart 			"buffers\n", bcnt, num_to_alloc);
229301649561SJames Smart 
229401649561SJames Smart 	/* post the list of nvme buffer sgls to port if available */
229501649561SJames Smart 	if (!list_empty(&post_nblist))
229601649561SJames Smart 		num_posted = lpfc_post_nvme_sgl_list(phba,
229701649561SJames Smart 						     &post_nblist, bcnt);
229801649561SJames Smart 	else
229901649561SJames Smart 		num_posted = 0;
230001649561SJames Smart 
230101649561SJames Smart 	return num_posted;
230201649561SJames Smart }
230301649561SJames Smart 
2304cf1a1d3eSJames Smart static inline struct lpfc_nvme_buf *
2305cf1a1d3eSJames Smart lpfc_nvme_buf(struct lpfc_hba *phba)
2306cf1a1d3eSJames Smart {
2307cf1a1d3eSJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
2308cf1a1d3eSJames Smart 
2309cf1a1d3eSJames Smart 	list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
2310cf1a1d3eSJames Smart 				 &phba->lpfc_nvme_buf_list_get, list) {
2311cf1a1d3eSJames Smart 		list_del_init(&lpfc_ncmd->list);
2312cf1a1d3eSJames Smart 		phba->get_nvme_bufs--;
2313cf1a1d3eSJames Smart 		return lpfc_ncmd;
2314cf1a1d3eSJames Smart 	}
2315cf1a1d3eSJames Smart 	return NULL;
2316cf1a1d3eSJames Smart }
2317cf1a1d3eSJames Smart 
231801649561SJames Smart /**
231901649561SJames Smart  * lpfc_get_nvme_buf - Get a nvme buffer from lpfc_nvme_buf_list of the HBA
232001649561SJames Smart  * @phba: The HBA for which this call is being executed.
232101649561SJames Smart  *
232201649561SJames Smart  * This routine removes a nvme buffer from head of @phba lpfc_nvme_buf_list list
232301649561SJames Smart  * and returns to caller.
232401649561SJames Smart  *
232501649561SJames Smart  * Return codes:
232601649561SJames Smart  *   NULL - Error
232701649561SJames Smart  *   Pointer to lpfc_nvme_buf - Success
232801649561SJames Smart  **/
232901649561SJames Smart static struct lpfc_nvme_buf *
2330cf1a1d3eSJames Smart lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
2331cf1a1d3eSJames Smart 		  int expedite)
233201649561SJames Smart {
2333cf1a1d3eSJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd = NULL;
233401649561SJames Smart 	unsigned long iflag = 0;
233501649561SJames Smart 
233601649561SJames Smart 	spin_lock_irqsave(&phba->nvme_buf_list_get_lock, iflag);
2337cf1a1d3eSJames Smart 	if (phba->get_nvme_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
2338cf1a1d3eSJames Smart 		lpfc_ncmd = lpfc_nvme_buf(phba);
2339cf1a1d3eSJames Smart 	if (!lpfc_ncmd) {
234001649561SJames Smart 		spin_lock(&phba->nvme_buf_list_put_lock);
234101649561SJames Smart 		list_splice(&phba->lpfc_nvme_buf_list_put,
234201649561SJames Smart 			    &phba->lpfc_nvme_buf_list_get);
2343cf1a1d3eSJames Smart 		phba->get_nvme_bufs += phba->put_nvme_bufs;
234401649561SJames Smart 		INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
2345cf1a1d3eSJames Smart 		phba->put_nvme_bufs = 0;
234601649561SJames Smart 		spin_unlock(&phba->nvme_buf_list_put_lock);
2347cf1a1d3eSJames Smart 		if (phba->get_nvme_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
2348cf1a1d3eSJames Smart 			lpfc_ncmd = lpfc_nvme_buf(phba);
234901649561SJames Smart 	}
235001649561SJames Smart 	spin_unlock_irqrestore(&phba->nvme_buf_list_get_lock, iflag);
235101649561SJames Smart 	return  lpfc_ncmd;
235201649561SJames Smart }
235301649561SJames Smart 
235401649561SJames Smart /**
235501649561SJames Smart  * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
235601649561SJames Smart  * @phba: The Hba for which this call is being executed.
235701649561SJames Smart  * @lpfc_ncmd: The nvme buffer which is being released.
235801649561SJames Smart  *
235901649561SJames Smart  * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
236001649561SJames Smart  * lpfc_nvme_buf_list list. For SLI4 XRI's are tied to the nvme buffer
236101649561SJames Smart  * and cannot be reused for at least RA_TOV amount of time if it was
236201649561SJames Smart  * aborted.
236301649561SJames Smart  **/
236401649561SJames Smart static void
236501649561SJames Smart lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_nvme_buf *lpfc_ncmd)
236601649561SJames Smart {
236701649561SJames Smart 	unsigned long iflag = 0;
236801649561SJames Smart 
236901649561SJames Smart 	lpfc_ncmd->nonsg_phys = 0;
2370318083adSJames Smart 	if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY) {
237186c67379SJames Smart 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
237286c67379SJames Smart 				"6310 XB release deferred for "
237386c67379SJames Smart 				"ox_id x%x on reqtag x%x\n",
237486c67379SJames Smart 				lpfc_ncmd->cur_iocbq.sli4_xritag,
237586c67379SJames Smart 				lpfc_ncmd->cur_iocbq.iotag);
237686c67379SJames Smart 
237701649561SJames Smart 		spin_lock_irqsave(&phba->sli4_hba.abts_nvme_buf_list_lock,
237801649561SJames Smart 					iflag);
237901649561SJames Smart 		list_add_tail(&lpfc_ncmd->list,
238001649561SJames Smart 			&phba->sli4_hba.lpfc_abts_nvme_buf_list);
238101649561SJames Smart 		spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock,
238201649561SJames Smart 					iflag);
238301649561SJames Smart 	} else {
238401649561SJames Smart 		lpfc_ncmd->nvmeCmd = NULL;
238501649561SJames Smart 		lpfc_ncmd->cur_iocbq.iocb_flag = LPFC_IO_NVME;
238601649561SJames Smart 		spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
238701649561SJames Smart 		list_add_tail(&lpfc_ncmd->list, &phba->lpfc_nvme_buf_list_put);
2388cf1a1d3eSJames Smart 		phba->put_nvme_bufs++;
238901649561SJames Smart 		spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
239001649561SJames Smart 	}
239101649561SJames Smart }
239201649561SJames Smart 
239301649561SJames Smart /**
239401649561SJames Smart  * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
239501649561SJames Smart  * @pvport - the lpfc_vport instance requesting a localport.
239601649561SJames Smart  *
239701649561SJames Smart  * This routine is invoked to create an nvme localport instance to bind
239801649561SJames Smart  * to the nvme_fc_transport.  It is called once during driver load
239901649561SJames Smart  * like lpfc_create_shost after all other services are initialized.
240001649561SJames Smart  * It requires a vport, vpi, and wwns at call time.  Other localport
240101649561SJames Smart  * parameters are modified as the driver's FCID and the Fabric WWN
240201649561SJames Smart  * are established.
240301649561SJames Smart  *
240401649561SJames Smart  * Return codes
240501649561SJames Smart  *      0 - successful
240601649561SJames Smart  *      -ENOMEM - no heap memory available
240701649561SJames Smart  *      other values - from nvme registration upcall
240801649561SJames Smart  **/
240901649561SJames Smart int
241001649561SJames Smart lpfc_nvme_create_localport(struct lpfc_vport *vport)
241101649561SJames Smart {
2412166d7211SJames Smart 	int ret = 0;
241301649561SJames Smart 	struct lpfc_hba  *phba = vport->phba;
241401649561SJames Smart 	struct nvme_fc_port_info nfcp_info;
241501649561SJames Smart 	struct nvme_fc_local_port *localport;
241601649561SJames Smart 	struct lpfc_nvme_lport *lport;
241766a210ffSJames Smart 	struct lpfc_nvme_ctrl_stat *cstat;
241866a210ffSJames Smart 	int len, i;
241901649561SJames Smart 
242001649561SJames Smart 	/* Initialize this localport instance.  The vport wwn usage ensures
242101649561SJames Smart 	 * that NPIV is accounted for.
242201649561SJames Smart 	 */
242301649561SJames Smart 	memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
242401649561SJames Smart 	nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
242501649561SJames Smart 	nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
242601649561SJames Smart 	nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
242701649561SJames Smart 
24284d4c4a4aSJames Smart 	/* Limit to LPFC_MAX_NVME_SEG_CNT.
24294d4c4a4aSJames Smart 	 * For now need + 1 to get around NVME transport logic.
24304d4c4a4aSJames Smart 	 */
24314d4c4a4aSJames Smart 	if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
24324d4c4a4aSJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_INIT,
24334d4c4a4aSJames Smart 				 "6300 Reducing sg segment cnt to %d\n",
24344d4c4a4aSJames Smart 				 LPFC_MAX_NVME_SEG_CNT);
24354d4c4a4aSJames Smart 		phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
24364d4c4a4aSJames Smart 	} else {
24374d4c4a4aSJames Smart 		phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
24384d4c4a4aSJames Smart 	}
24394d4c4a4aSJames Smart 	lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
244001649561SJames Smart 	lpfc_nvme_template.max_hw_queues = phba->cfg_nvme_io_channel;
244101649561SJames Smart 
244266a210ffSJames Smart 	cstat = kmalloc((sizeof(struct lpfc_nvme_ctrl_stat) *
244366a210ffSJames Smart 			phba->cfg_nvme_io_channel), GFP_KERNEL);
244466a210ffSJames Smart 	if (!cstat)
244566a210ffSJames Smart 		return -ENOMEM;
244666a210ffSJames Smart 
244701649561SJames Smart 	/* localport is allocated from the stack, but the registration
244801649561SJames Smart 	 * call allocates heap memory as well as the private area.
244901649561SJames Smart 	 */
24507d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
245101649561SJames Smart 	ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
245201649561SJames Smart 					 &vport->phba->pcidev->dev, &localport);
2453166d7211SJames Smart #else
2454166d7211SJames Smart 	ret = -ENOMEM;
2455166d7211SJames Smart #endif
245601649561SJames Smart 	if (!ret) {
245701649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
245801649561SJames Smart 				 "6005 Successfully registered local "
245901649561SJames Smart 				 "NVME port num %d, localP %p, private %p, "
246001649561SJames Smart 				 "sg_seg %d\n",
246101649561SJames Smart 				 localport->port_num, localport,
246201649561SJames Smart 				 localport->private,
246301649561SJames Smart 				 lpfc_nvme_template.max_sgl_segments);
246401649561SJames Smart 
246501649561SJames Smart 		/* Private is our lport size declared in the template. */
246601649561SJames Smart 		lport = (struct lpfc_nvme_lport *)localport->private;
246701649561SJames Smart 		vport->localport = localport;
246801649561SJames Smart 		lport->vport = vport;
246966a210ffSJames Smart 		lport->cstat = cstat;
247001649561SJames Smart 		vport->nvmei_support = 1;
24716b486ce9SDick Kennedy 
24724b056682SJames Smart 		atomic_set(&lport->xmt_fcp_noxri, 0);
24734b056682SJames Smart 		atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
24744b056682SJames Smart 		atomic_set(&lport->xmt_fcp_qdepth, 0);
24754b056682SJames Smart 		atomic_set(&lport->xmt_fcp_wqerr, 0);
24764b056682SJames Smart 		atomic_set(&lport->xmt_fcp_abort, 0);
24774b056682SJames Smart 		atomic_set(&lport->xmt_ls_abort, 0);
24784b056682SJames Smart 		atomic_set(&lport->xmt_ls_err, 0);
24794b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_xb, 0);
24804b056682SJames Smart 		atomic_set(&lport->cmpl_fcp_err, 0);
24814b056682SJames Smart 		atomic_set(&lport->cmpl_ls_xb, 0);
24824b056682SJames Smart 		atomic_set(&lport->cmpl_ls_err, 0);
248366a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsRequests, 0);
248466a210ffSJames Smart 		atomic_set(&lport->fc4NvmeLsCmpls, 0);
248566a210ffSJames Smart 
248666a210ffSJames Smart 		for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
248766a210ffSJames Smart 			cstat = &lport->cstat[i];
248866a210ffSJames Smart 			atomic_set(&cstat->fc4NvmeInputRequests, 0);
248966a210ffSJames Smart 			atomic_set(&cstat->fc4NvmeOutputRequests, 0);
249066a210ffSJames Smart 			atomic_set(&cstat->fc4NvmeControlRequests, 0);
249166a210ffSJames Smart 			atomic_set(&cstat->fc4NvmeIoCmpls, 0);
249266a210ffSJames Smart 		}
24934b056682SJames Smart 
24946b486ce9SDick Kennedy 		/* Don't post more new bufs if repost already recovered
24956b486ce9SDick Kennedy 		 * the nvme sgls.
24966b486ce9SDick Kennedy 		 */
24976b486ce9SDick Kennedy 		if (phba->sli4_hba.nvme_xri_cnt == 0) {
24986b486ce9SDick Kennedy 			len  = lpfc_new_nvme_buf(vport,
24996b486ce9SDick Kennedy 						 phba->sli4_hba.nvme_xri_max);
250001649561SJames Smart 			vport->phba->total_nvme_bufs += len;
25013ebd9b47SJames Smart 		}
250266a210ffSJames Smart 	} else {
250366a210ffSJames Smart 		kfree(cstat);
25046b486ce9SDick Kennedy 	}
25053ebd9b47SJames Smart 
250601649561SJames Smart 	return ret;
250701649561SJames Smart }
250801649561SJames Smart 
2509add9d6beSJames Smart /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2510add9d6beSJames Smart  *
2511add9d6beSJames Smart  * The driver has to wait for the host nvme transport to callback
2512add9d6beSJames Smart  * indicating the localport has successfully unregistered all
2513add9d6beSJames Smart  * resources.  Since this is an uninterruptible wait, loop every ten
2514add9d6beSJames Smart  * seconds and print a message indicating no progress.
2515add9d6beSJames Smart  *
2516add9d6beSJames Smart  * An uninterruptible wait is used because of the risk of transport-to-
2517add9d6beSJames Smart  * driver state mismatch.
2518add9d6beSJames Smart  */
2519add9d6beSJames Smart void
2520add9d6beSJames Smart lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
2521add9d6beSJames Smart 			   struct lpfc_nvme_lport *lport)
2522add9d6beSJames Smart {
2523add9d6beSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
2524add9d6beSJames Smart 	u32 wait_tmo;
2525add9d6beSJames Smart 	int ret;
2526add9d6beSJames Smart 
2527add9d6beSJames Smart 	/* Host transport has to clean up and confirm requiring an indefinite
2528add9d6beSJames Smart 	 * wait. Print a message if a 10 second wait expires and renew the
2529add9d6beSJames Smart 	 * wait. This is unexpected.
2530add9d6beSJames Smart 	 */
2531add9d6beSJames Smart 	wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2532add9d6beSJames Smart 	while (true) {
2533add9d6beSJames Smart 		ret = wait_for_completion_timeout(&lport->lport_unreg_done,
2534add9d6beSJames Smart 						  wait_tmo);
2535add9d6beSJames Smart 		if (unlikely(!ret)) {
2536add9d6beSJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
2537add9d6beSJames Smart 					 "6176 Lport %p Localport %p wait "
2538add9d6beSJames Smart 					 "timed out. Renewing.\n",
2539add9d6beSJames Smart 					 lport, vport->localport);
2540add9d6beSJames Smart 			continue;
2541add9d6beSJames Smart 		}
2542add9d6beSJames Smart 		break;
2543add9d6beSJames Smart 	}
2544add9d6beSJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
2545add9d6beSJames Smart 			 "6177 Lport %p Localport %p Complete Success\n",
2546add9d6beSJames Smart 			 lport, vport->localport);
2547add9d6beSJames Smart #endif
2548add9d6beSJames Smart }
2549add9d6beSJames Smart 
255001649561SJames Smart /**
255101649561SJames Smart  * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
255201649561SJames Smart  * @pnvme: pointer to lpfc nvme data structure.
255301649561SJames Smart  *
255401649561SJames Smart  * This routine is invoked to destroy all lports bound to the phba.
255501649561SJames Smart  * The lport memory was allocated by the nvme fc transport and is
255601649561SJames Smart  * released there.  This routine ensures all rports bound to the
255701649561SJames Smart  * lport have been disconnected.
255801649561SJames Smart  *
255901649561SJames Smart  **/
256001649561SJames Smart void
256101649561SJames Smart lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
256201649561SJames Smart {
25637d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
256401649561SJames Smart 	struct nvme_fc_local_port *localport;
256501649561SJames Smart 	struct lpfc_nvme_lport *lport;
256666a210ffSJames Smart 	struct lpfc_nvme_ctrl_stat *cstat;
256701649561SJames Smart 	int ret;
256801649561SJames Smart 
256901649561SJames Smart 	if (vport->nvmei_support == 0)
257001649561SJames Smart 		return;
257101649561SJames Smart 
257201649561SJames Smart 	localport = vport->localport;
257301649561SJames Smart 	vport->localport = NULL;
257401649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
257566a210ffSJames Smart 	cstat = lport->cstat;
257601649561SJames Smart 
257701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
257801649561SJames Smart 			 "6011 Destroying NVME localport %p\n",
257901649561SJames Smart 			 localport);
2580166d7211SJames Smart 
258101649561SJames Smart 	/* lport's rport list is clear.  Unregister
258201649561SJames Smart 	 * lport and release resources.
258301649561SJames Smart 	 */
258401649561SJames Smart 	init_completion(&lport->lport_unreg_done);
258501649561SJames Smart 	ret = nvme_fc_unregister_localport(localport);
2586add9d6beSJames Smart 
2587add9d6beSJames Smart 	/* Wait for completion.  This either blocks
2588add9d6beSJames Smart 	 * indefinitely or succeeds
2589add9d6beSJames Smart 	 */
2590add9d6beSJames Smart 	lpfc_nvme_lport_unreg_wait(vport, lport);
259166a210ffSJames Smart 	kfree(cstat);
259201649561SJames Smart 
259301649561SJames Smart 	/* Regardless of the unregister upcall response, clear
259401649561SJames Smart 	 * nvmei_support.  All rports are unregistered and the
259501649561SJames Smart 	 * driver will clean up.
259601649561SJames Smart 	 */
259701649561SJames Smart 	vport->nvmei_support = 0;
259801649561SJames Smart 	if (ret == 0) {
259901649561SJames Smart 		lpfc_printf_vlog(vport,
260001649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
260101649561SJames Smart 				 "6009 Unregistered lport Success\n");
260201649561SJames Smart 	} else {
260301649561SJames Smart 		lpfc_printf_vlog(vport,
260401649561SJames Smart 				 KERN_INFO, LOG_NVME_DISC,
260501649561SJames Smart 				 "6010 Unregistered lport "
260601649561SJames Smart 				 "Failed, status x%x\n",
260701649561SJames Smart 				 ret);
260801649561SJames Smart 	}
2609166d7211SJames Smart #endif
261001649561SJames Smart }
261101649561SJames Smart 
261201649561SJames Smart void
261301649561SJames Smart lpfc_nvme_update_localport(struct lpfc_vport *vport)
261401649561SJames Smart {
26154410a67aSJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
261601649561SJames Smart 	struct nvme_fc_local_port *localport;
261701649561SJames Smart 	struct lpfc_nvme_lport *lport;
261801649561SJames Smart 
261901649561SJames Smart 	localport = vport->localport;
26204410a67aSJames Smart 	if (!localport) {
26214410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
26224410a67aSJames Smart 				 "6710 Update NVME fail. No localport\n");
26234410a67aSJames Smart 		return;
26244410a67aSJames Smart 	}
262501649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
26264410a67aSJames Smart 	if (!lport) {
26274410a67aSJames Smart 		lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
26284410a67aSJames Smart 				 "6171 Update NVME fail. localP %p, No lport\n",
26294410a67aSJames Smart 				 localport);
26304410a67aSJames Smart 		return;
26314410a67aSJames Smart 	}
263201649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
263301649561SJames Smart 			 "6012 Update NVME lport %p did x%x\n",
263401649561SJames Smart 			 localport, vport->fc_myDID);
263501649561SJames Smart 
263601649561SJames Smart 	localport->port_id = vport->fc_myDID;
263701649561SJames Smart 	if (localport->port_id == 0)
263801649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
263901649561SJames Smart 	else
264001649561SJames Smart 		localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
264101649561SJames Smart 
264201649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
264301649561SJames Smart 			 "6030 bound lport %p to DID x%06x\n",
264401649561SJames Smart 			 lport, localport->port_id);
26454410a67aSJames Smart #endif
264601649561SJames Smart }
264701649561SJames Smart 
264801649561SJames Smart int
264901649561SJames Smart lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
265001649561SJames Smart {
26517d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
265201649561SJames Smart 	int ret = 0;
265301649561SJames Smart 	struct nvme_fc_local_port *localport;
265401649561SJames Smart 	struct lpfc_nvme_lport *lport;
265501649561SJames Smart 	struct lpfc_nvme_rport *rport;
265601466024SJames Smart 	struct lpfc_nvme_rport *oldrport;
265701649561SJames Smart 	struct nvme_fc_remote_port *remote_port;
265801649561SJames Smart 	struct nvme_fc_port_info rpinfo;
26592b75d0f9SDick Kennedy 	struct lpfc_nodelist *prev_ndlp;
266001649561SJames Smart 
266101649561SJames Smart 	lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
266201649561SJames Smart 			 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
266301649561SJames Smart 			 ndlp->nlp_DID, ndlp->nlp_type);
266401649561SJames Smart 
266501649561SJames Smart 	localport = vport->localport;
2666bb6a8a2cSDick Kennedy 	if (!localport)
2667bb6a8a2cSDick Kennedy 		return 0;
2668bb6a8a2cSDick Kennedy 
266901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
267001649561SJames Smart 
26717a06dcd3SJames Smart 	/* NVME rports are not preserved across devloss.
26727a06dcd3SJames Smart 	 * Just register this instance.  Note, rpinfo->dev_loss_tmo
26737a06dcd3SJames Smart 	 * is left 0 to indicate accept transport defaults.  The
26747a06dcd3SJames Smart 	 * driver communicates port role capabilities consistent
26757a06dcd3SJames Smart 	 * with the PRLI response data.
267601649561SJames Smart 	 */
26777a06dcd3SJames Smart 	memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
26787a06dcd3SJames Smart 	rpinfo.port_id = ndlp->nlp_DID;
267901649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET)
26807a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
268101649561SJames Smart 	if (ndlp->nlp_type & NLP_NVME_INITIATOR)
26827a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
268301649561SJames Smart 
26847a06dcd3SJames Smart 	if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
26857a06dcd3SJames Smart 		rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
26867a06dcd3SJames Smart 
26877a06dcd3SJames Smart 	rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
26887a06dcd3SJames Smart 	rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
268901466024SJames Smart 
269001466024SJames Smart 	oldrport = lpfc_ndlp_get_nrport(ndlp);
269101466024SJames Smart 	if (!oldrport)
26923fd78355SJames Smart 		lpfc_nlp_get(ndlp);
26933fd78355SJames Smart 
26947a06dcd3SJames Smart 	ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
26957a06dcd3SJames Smart 	if (!ret) {
26967a06dcd3SJames Smart 		/* If the ndlp already has an nrport, this is just
26977a06dcd3SJames Smart 		 * a resume of the existing rport.  Else this is a
26987a06dcd3SJames Smart 		 * new rport.
26997a06dcd3SJames Smart 		 */
2700*b15bd3e6SJames Smart 		/* Guard against an unregister/reregister
2701*b15bd3e6SJames Smart 		 * race that leaves the WAIT flag set.
2702*b15bd3e6SJames Smart 		 */
2703*b15bd3e6SJames Smart 		spin_lock_irq(&vport->phba->hbalock);
2704*b15bd3e6SJames Smart 		ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
2705*b15bd3e6SJames Smart 		spin_unlock_irq(&vport->phba->hbalock);
27067a06dcd3SJames Smart 		rport = remote_port->private;
270701466024SJames Smart 		if (oldrport) {
270801466024SJames Smart 			if (oldrport == remote_port->private) {
27093fd78355SJames Smart 				/* Same remoteport.  Just reuse. */
271001649561SJames Smart 				lpfc_printf_vlog(ndlp->vport, KERN_INFO,
271101649561SJames Smart 						 LOG_NVME_DISC,
271201649561SJames Smart 						 "6014 Rebinding lport to "
27133fd78355SJames Smart 						 "remoteport %p wwpn 0x%llx, "
27143fd78355SJames Smart 						 "Data: x%x x%x %p x%x x%06x\n",
27153fd78355SJames Smart 						 remote_port,
271601649561SJames Smart 						 remote_port->port_name,
271701649561SJames Smart 						 remote_port->port_id,
271801649561SJames Smart 						 remote_port->port_role,
27193fd78355SJames Smart 						 ndlp,
272001649561SJames Smart 						 ndlp->nlp_type,
272101649561SJames Smart 						 ndlp->nlp_DID);
27223fd78355SJames Smart 				return 0;
27233fd78355SJames Smart 			}
27242b75d0f9SDick Kennedy 			prev_ndlp = rport->ndlp;
27252b75d0f9SDick Kennedy 
27263fd78355SJames Smart 			/* Sever the ndlp<->rport association
27273fd78355SJames Smart 			 * before dropping the ndlp ref from
27283fd78355SJames Smart 			 * register.
27292b75d0f9SDick Kennedy 			 */
27303fd78355SJames Smart 			spin_lock_irq(&vport->phba->hbalock);
27312b75d0f9SDick Kennedy 			ndlp->nrport = NULL;
273201466024SJames Smart 			ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
27333fd78355SJames Smart 			spin_unlock_irq(&vport->phba->hbalock);
27342b75d0f9SDick Kennedy 			rport->ndlp = NULL;
27353fd78355SJames Smart 			rport->remoteport = NULL;
2736b04744ceSJames Smart 
2737b04744ceSJames Smart 			/* Reference only removed if previous NDLP is no longer
2738b04744ceSJames Smart 			 * active. It might be just a swap and removing the
2739b04744ceSJames Smart 			 * reference would cause a premature cleanup.
2740b04744ceSJames Smart 			 */
2741b04744ceSJames Smart 			if (prev_ndlp && prev_ndlp != ndlp) {
2742b04744ceSJames Smart 				if ((!NLP_CHK_NODE_ACT(prev_ndlp)) ||
2743b04744ceSJames Smart 				    (!prev_ndlp->nrport))
2744b04744ceSJames Smart 					lpfc_nlp_put(prev_ndlp);
2745b04744ceSJames Smart 			}
27462b75d0f9SDick Kennedy 		}
27472b75d0f9SDick Kennedy 
27482b75d0f9SDick Kennedy 		/* Clean bind the rport to the ndlp. */
274901649561SJames Smart 		rport->remoteport = remote_port;
275001649561SJames Smart 		rport->lport = lport;
27513fd78355SJames Smart 		rport->ndlp = ndlp;
27523fd78355SJames Smart 		spin_lock_irq(&vport->phba->hbalock);
275301649561SJames Smart 		ndlp->nrport = rport;
27543fd78355SJames Smart 		spin_unlock_irq(&vport->phba->hbalock);
275501649561SJames Smart 		lpfc_printf_vlog(vport, KERN_INFO,
275601649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
27577a06dcd3SJames Smart 				 "6022 Binding new rport to "
27583fd78355SJames Smart 				 "lport %p Remoteport %p  WWNN 0x%llx, "
27597a06dcd3SJames Smart 				 "Rport WWPN 0x%llx DID "
27603fd78355SJames Smart 				 "x%06x Role x%x, ndlp %p\n",
27613fd78355SJames Smart 				 lport, remote_port,
276201649561SJames Smart 				 rpinfo.node_name, rpinfo.port_name,
27633fd78355SJames Smart 				 rpinfo.port_id, rpinfo.port_role,
27643fd78355SJames Smart 				 ndlp);
276501649561SJames Smart 	} else {
276601649561SJames Smart 		lpfc_printf_vlog(vport, KERN_ERR,
276701649561SJames Smart 				 LOG_NVME_DISC | LOG_NODE,
276801649561SJames Smart 				 "6031 RemotePort Registration failed "
276901649561SJames Smart 				 "err: %d, DID x%06x\n",
277001649561SJames Smart 				 ret, ndlp->nlp_DID);
277101649561SJames Smart 	}
27727a06dcd3SJames Smart 
277301649561SJames Smart 	return ret;
2774166d7211SJames Smart #else
2775166d7211SJames Smart 	return 0;
2776166d7211SJames Smart #endif
277701649561SJames Smart }
277801649561SJames Smart 
277901649561SJames Smart /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
278001649561SJames Smart  *
278101649561SJames Smart  * There is no notion of Devloss or rport recovery from the current
278201649561SJames Smart  * nvme_transport perspective.  Loss of an rport just means IO cannot
278301649561SJames Smart  * be sent and recovery is completely up to the initator.
278401649561SJames Smart  * For now, the driver just unbinds the DID and port_role so that
278501649561SJames Smart  * no further IO can be issued.  Changes are planned for later.
278601649561SJames Smart  *
278701649561SJames Smart  * Notes - the ndlp reference count is not decremented here since
278801649561SJames Smart  * since there is no nvme_transport api for devloss.  Node ref count
278901649561SJames Smart  * is only adjusted in driver unload.
279001649561SJames Smart  */
279101649561SJames Smart void
279201649561SJames Smart lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
279301649561SJames Smart {
27947d708033SJames Smart #if (IS_ENABLED(CONFIG_NVME_FC))
279501649561SJames Smart 	int ret;
279601649561SJames Smart 	struct nvme_fc_local_port *localport;
279701649561SJames Smart 	struct lpfc_nvme_lport *lport;
279801649561SJames Smart 	struct lpfc_nvme_rport *rport;
279901649561SJames Smart 	struct nvme_fc_remote_port *remoteport;
280001649561SJames Smart 
280101649561SJames Smart 	localport = vport->localport;
280201649561SJames Smart 
280301649561SJames Smart 	/* This is fundamental error.  The localport is always
280401649561SJames Smart 	 * available until driver unload.  Just exit.
280501649561SJames Smart 	 */
280601649561SJames Smart 	if (!localport)
280701649561SJames Smart 		return;
280801649561SJames Smart 
280901649561SJames Smart 	lport = (struct lpfc_nvme_lport *)localport->private;
281001649561SJames Smart 	if (!lport)
281101649561SJames Smart 		goto input_err;
281201649561SJames Smart 
281301466024SJames Smart 	rport = lpfc_ndlp_get_nrport(ndlp);
281401649561SJames Smart 	if (!rport)
281501649561SJames Smart 		goto input_err;
281601649561SJames Smart 
281701649561SJames Smart 	remoteport = rport->remoteport;
281801649561SJames Smart 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
281901649561SJames Smart 			 "6033 Unreg nvme remoteport %p, portname x%llx, "
282001649561SJames Smart 			 "port_id x%06x, portstate x%x port type x%x\n",
282101649561SJames Smart 			 remoteport, remoteport->port_name,
282201649561SJames Smart 			 remoteport->port_id, remoteport->port_state,
282301649561SJames Smart 			 ndlp->nlp_type);
282401649561SJames Smart 
282501649561SJames Smart 	/* Sanity check ndlp type.  Only call for NVME ports. Don't
282601649561SJames Smart 	 * clear any rport state until the transport calls back.
282701649561SJames Smart 	 */
28283b5bde69SJames Smart 
28293b5bde69SJames Smart 	if (ndlp->nlp_type & NLP_NVME_TARGET) {
28307a06dcd3SJames Smart 		/* No concern about the role change on the nvme remoteport.
28317a06dcd3SJames Smart 		 * The transport will update it.
28327a06dcd3SJames Smart 		 */
2833add9d6beSJames Smart 		ndlp->upcall_flags |= NLP_WAIT_FOR_UNREG;
283401649561SJames Smart 		ret = nvme_fc_unregister_remoteport(remoteport);
28353fd78355SJames Smart 		if (ret != 0) {
28363fd78355SJames Smart 			lpfc_nlp_put(ndlp);
283701649561SJames Smart 			lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
283801649561SJames Smart 					 "6167 NVME unregister failed %d "
283901649561SJames Smart 					 "port_state x%x\n",
284001649561SJames Smart 					 ret, remoteport->port_state);
28413fd78355SJames Smart 		}
284201649561SJames Smart 	}
284301649561SJames Smart 	return;
284401649561SJames Smart 
284501649561SJames Smart  input_err:
2846166d7211SJames Smart #endif
284701649561SJames Smart 	lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
28482b7824d0SJames Smart 			 "6168 State error: lport %p, rport%p FCID x%06x\n",
284901649561SJames Smart 			 vport->localport, ndlp->rport, ndlp->nlp_DID);
285001649561SJames Smart }
2851318083adSJames Smart 
2852318083adSJames Smart /**
2853318083adSJames Smart  * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2854318083adSJames Smart  * @phba: pointer to lpfc hba data structure.
2855318083adSJames Smart  * @axri: pointer to the fcp xri abort wcqe structure.
2856318083adSJames Smart  *
2857318083adSJames Smart  * This routine is invoked by the worker thread to process a SLI4 fast-path
2858952c303bSDick Kennedy  * NVME aborted xri.  Aborted NVME IO commands are completed to the transport
2859952c303bSDick Kennedy  * here.
2860318083adSJames Smart  **/
2861318083adSJames Smart void
2862318083adSJames Smart lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2863318083adSJames Smart 			   struct sli4_wcqe_xri_aborted *axri)
2864318083adSJames Smart {
2865318083adSJames Smart 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2866318083adSJames Smart 	struct lpfc_nvme_buf *lpfc_ncmd, *next_lpfc_ncmd;
2867952c303bSDick Kennedy 	struct nvmefc_fcp_req *nvme_cmd = NULL;
2868318083adSJames Smart 	struct lpfc_nodelist *ndlp;
2869318083adSJames Smart 	unsigned long iflag = 0;
2870318083adSJames Smart 
2871318083adSJames Smart 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
2872318083adSJames Smart 		return;
2873318083adSJames Smart 	spin_lock_irqsave(&phba->hbalock, iflag);
2874318083adSJames Smart 	spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2875318083adSJames Smart 	list_for_each_entry_safe(lpfc_ncmd, next_lpfc_ncmd,
2876318083adSJames Smart 				 &phba->sli4_hba.lpfc_abts_nvme_buf_list,
2877318083adSJames Smart 				 list) {
2878318083adSJames Smart 		if (lpfc_ncmd->cur_iocbq.sli4_xritag == xri) {
2879bbe3012bSJames Smart 			list_del_init(&lpfc_ncmd->list);
2880318083adSJames Smart 			lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
2881318083adSJames Smart 			lpfc_ncmd->status = IOSTAT_SUCCESS;
2882318083adSJames Smart 			spin_unlock(
2883318083adSJames Smart 				&phba->sli4_hba.abts_nvme_buf_list_lock);
2884318083adSJames Smart 
2885318083adSJames Smart 			spin_unlock_irqrestore(&phba->hbalock, iflag);
2886318083adSJames Smart 			ndlp = lpfc_ncmd->ndlp;
2887952c303bSDick Kennedy 			if (ndlp)
2888318083adSJames Smart 				lpfc_sli4_abts_err_handler(phba, ndlp, axri);
288986c67379SJames Smart 
289086c67379SJames Smart 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2891952c303bSDick Kennedy 					"6311 nvme_cmd %p xri x%x tag x%x "
2892952c303bSDick Kennedy 					"abort complete and xri released\n",
2893952c303bSDick Kennedy 					lpfc_ncmd->nvmeCmd, xri,
2894952c303bSDick Kennedy 					lpfc_ncmd->cur_iocbq.iotag);
289586c67379SJames Smart 
2896952c303bSDick Kennedy 			/* Aborted NVME commands are required to not complete
2897952c303bSDick Kennedy 			 * before the abort exchange command fully completes.
2898952c303bSDick Kennedy 			 * Once completed, it is available via the put list.
2899952c303bSDick Kennedy 			 */
29003fd78355SJames Smart 			if (lpfc_ncmd->nvmeCmd) {
2901952c303bSDick Kennedy 				nvme_cmd = lpfc_ncmd->nvmeCmd;
2902952c303bSDick Kennedy 				nvme_cmd->done(nvme_cmd);
29033fd78355SJames Smart 				lpfc_ncmd->nvmeCmd = NULL;
29043fd78355SJames Smart 			}
2905318083adSJames Smart 			lpfc_release_nvme_buf(phba, lpfc_ncmd);
2906318083adSJames Smart 			return;
2907318083adSJames Smart 		}
2908318083adSJames Smart 	}
2909318083adSJames Smart 	spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2910318083adSJames Smart 	spin_unlock_irqrestore(&phba->hbalock, iflag);
291186c67379SJames Smart 
291286c67379SJames Smart 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
291386c67379SJames Smart 			"6312 XRI Aborted xri x%x not found\n", xri);
291486c67379SJames Smart 
2915318083adSJames Smart }
2916c3725bdcSJames Smart 
2917c3725bdcSJames Smart /**
2918c3725bdcSJames Smart  * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2919c3725bdcSJames Smart  * @phba: Pointer to HBA context object.
2920c3725bdcSJames Smart  *
2921c3725bdcSJames Smart  * This function flushes all wqes in the nvme rings and frees all resources
2922c3725bdcSJames Smart  * in the txcmplq. This function does not issue abort wqes for the IO
2923c3725bdcSJames Smart  * commands in txcmplq, they will just be returned with
2924c3725bdcSJames Smart  * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2925c3725bdcSJames Smart  * slot has been permanently disabled.
2926c3725bdcSJames Smart  **/
2927c3725bdcSJames Smart void
2928c3725bdcSJames Smart lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2929c3725bdcSJames Smart {
2930c3725bdcSJames Smart 	struct lpfc_sli_ring  *pring;
2931c3725bdcSJames Smart 	u32 i, wait_cnt = 0;
2932c3725bdcSJames Smart 
2933c3725bdcSJames Smart 	if (phba->sli_rev < LPFC_SLI_REV4)
2934c3725bdcSJames Smart 		return;
2935c3725bdcSJames Smart 
2936c3725bdcSJames Smart 	/* Cycle through all NVME rings and make sure all outstanding
2937c3725bdcSJames Smart 	 * WQEs have been removed from the txcmplqs.
2938c3725bdcSJames Smart 	 */
2939c3725bdcSJames Smart 	for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
2940c3725bdcSJames Smart 		pring = phba->sli4_hba.nvme_wq[i]->pring;
2941c3725bdcSJames Smart 
2942c3725bdcSJames Smart 		/* Retrieve everything on the txcmplq */
2943c3725bdcSJames Smart 		while (!list_empty(&pring->txcmplq)) {
2944c3725bdcSJames Smart 			msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2945c3725bdcSJames Smart 			wait_cnt++;
2946c3725bdcSJames Smart 
2947c3725bdcSJames Smart 			/* The sleep is 10mS.  Every ten seconds,
2948c3725bdcSJames Smart 			 * dump a message.  Something is wrong.
2949c3725bdcSJames Smart 			 */
2950c3725bdcSJames Smart 			if ((wait_cnt % 1000) == 0) {
2951c3725bdcSJames Smart 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2952c3725bdcSJames Smart 						"6178 NVME IO not empty, "
2953c3725bdcSJames Smart 						"cnt %d\n", wait_cnt);
2954c3725bdcSJames Smart 			}
2955c3725bdcSJames Smart 		}
2956c3725bdcSJames Smart 	}
2957c3725bdcSJames Smart }
2958