LiteX: driver for MMCM
[linux/fpc-iii.git] / drivers / scsi / lpfc / lpfc_nvme.c
blob1cb82fa6a60e4c40d6e1c9d35287c274dc286fd0
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2017-2020 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
6 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
7 * EMULEX and SLI are trademarks of Emulex. *
8 * www.broadcom.com *
9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
10 * *
11 * This program is free software; you can redistribute it and/or *
12 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
22 ********************************************************************/
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <asm/unaligned.h>
28 #include <linux/crc-t10dif.h>
29 #include <net/checksum.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_eh.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <scsi/fc/fc_fs.h>
39 #include "lpfc_version.h"
40 #include "lpfc_hw4.h"
41 #include "lpfc_hw.h"
42 #include "lpfc_sli.h"
43 #include "lpfc_sli4.h"
44 #include "lpfc_nl.h"
45 #include "lpfc_disc.h"
46 #include "lpfc.h"
47 #include "lpfc_nvme.h"
48 #include "lpfc_scsi.h"
49 #include "lpfc_logmsg.h"
50 #include "lpfc_crtn.h"
51 #include "lpfc_vport.h"
52 #include "lpfc_debugfs.h"
54 /* NVME initiator-based functions */
56 static struct lpfc_io_buf *
57 lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
58 int idx, int expedite);
60 static void
61 lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_io_buf *);
63 static struct nvme_fc_port_template lpfc_nvme_template;
65 /**
66 * lpfc_nvme_create_queue -
67 * @pnvme_lport: Transport localport that LS is to be issued from
68 * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
69 * @qsize: Size of the queue in bytes
70 * @handle: An opaque driver handle used in follow-up calls.
72 * Driver registers this routine to preallocate and initialize any
73 * internal data structures to bind the @qidx to its internal IO queues.
74 * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
76 * Return value :
77 * 0 - Success
78 * -EINVAL - Unsupported input value.
79 * -ENOMEM - Could not alloc necessary memory
80 **/
81 static int
82 lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
83 unsigned int qidx, u16 qsize,
84 void **handle)
86 struct lpfc_nvme_lport *lport;
87 struct lpfc_vport *vport;
88 struct lpfc_nvme_qhandle *qhandle;
89 char *str;
91 if (!pnvme_lport->private)
92 return -ENOMEM;
94 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
95 vport = lport->vport;
96 qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
97 if (qhandle == NULL)
98 return -ENOMEM;
100 qhandle->cpu_id = raw_smp_processor_id();
101 qhandle->qidx = qidx;
103 * NVME qidx == 0 is the admin queue, so both admin queue
104 * and first IO queue will use MSI-X vector and associated
105 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
107 if (qidx) {
108 str = "IO "; /* IO queue */
109 qhandle->index = ((qidx - 1) %
110 lpfc_nvme_template.max_hw_queues);
111 } else {
112 str = "ADM"; /* Admin queue */
113 qhandle->index = qidx;
116 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
117 "6073 Binding %s HdwQueue %d (cpu %d) to "
118 "hdw_queue %d qhandle x%px\n", str,
119 qidx, qhandle->cpu_id, qhandle->index, qhandle);
120 *handle = (void *)qhandle;
121 return 0;
125 * lpfc_nvme_delete_queue -
126 * @pnvme_lport: Transport localport that LS is to be issued from
127 * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
128 * @handle: An opaque driver handle from lpfc_nvme_create_queue
130 * Driver registers this routine to free
131 * any internal data structures to bind the @qidx to its internal
132 * IO queues.
134 * Return value :
135 * 0 - Success
136 * TODO: What are the failure codes.
138 static void
139 lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
140 unsigned int qidx,
141 void *handle)
143 struct lpfc_nvme_lport *lport;
144 struct lpfc_vport *vport;
146 if (!pnvme_lport->private)
147 return;
149 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
150 vport = lport->vport;
152 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
153 "6001 ENTER. lpfc_pnvme x%px, qidx x%x qhandle x%px\n",
154 lport, qidx, handle);
155 kfree(handle);
158 static void
159 lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
161 struct lpfc_nvme_lport *lport = localport->private;
163 lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
164 "6173 localport x%px delete complete\n",
165 lport);
167 /* release any threads waiting for the unreg to complete */
168 if (lport->vport->localport)
169 complete(lport->lport_unreg_cmp);
172 /* lpfc_nvme_remoteport_delete
174 * @remoteport: Pointer to an nvme transport remoteport instance.
176 * This is a template downcall. NVME transport calls this function
177 * when it has completed the unregistration of a previously
178 * registered remoteport.
180 * Return value :
181 * None
183 static void
184 lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
186 struct lpfc_nvme_rport *rport = remoteport->private;
187 struct lpfc_vport *vport;
188 struct lpfc_nodelist *ndlp;
189 u32 fc4_xpt_flags;
191 ndlp = rport->ndlp;
192 if (!ndlp) {
193 pr_err("**** %s: NULL ndlp on rport %p remoteport %p\n",
194 __func__, rport, remoteport);
195 goto rport_err;
198 vport = ndlp->vport;
199 if (!vport) {
200 pr_err("**** %s: Null vport on ndlp %p, ste x%x rport %p\n",
201 __func__, ndlp, ndlp->nlp_state, rport);
202 goto rport_err;
205 fc4_xpt_flags = NVME_XPT_REGD | SCSI_XPT_REGD;
207 /* Remove this rport from the lport's list - memory is owned by the
208 * transport. Remove the ndlp reference for the NVME transport before
209 * calling state machine to remove the node.
211 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
212 "6146 remoteport delete of remoteport %p\n",
213 remoteport);
214 spin_lock_irq(&ndlp->lock);
216 /* The register rebind might have occurred before the delete
217 * downcall. Guard against this race.
219 if (ndlp->fc4_xpt_flags & NLP_WAIT_FOR_UNREG)
220 ndlp->fc4_xpt_flags &= ~(NLP_WAIT_FOR_UNREG | NVME_XPT_REGD);
222 spin_unlock_irq(&ndlp->lock);
224 /* On a devloss timeout event, one more put is executed provided the
225 * NVME and SCSI rport unregister requests are complete. If the vport
226 * is unloading, this extra put is executed by lpfc_drop_node.
228 if (!(ndlp->fc4_xpt_flags & fc4_xpt_flags))
229 lpfc_disc_state_machine(vport, ndlp, NULL, NLP_EVT_DEVICE_RM);
231 rport_err:
232 return;
236 * lpfc_nvme_handle_lsreq - Process an unsolicited NVME LS request
237 * @phba: pointer to lpfc hba data structure.
238 * @axchg: pointer to exchange context for the NVME LS request
240 * This routine is used for processing an asychronously received NVME LS
241 * request. Any remaining validation is done and the LS is then forwarded
242 * to the nvme-fc transport via nvme_fc_rcv_ls_req().
244 * The calling sequence should be: nvme_fc_rcv_ls_req() -> (processing)
245 * -> lpfc_nvme_xmt_ls_rsp/cmp -> req->done.
246 * __lpfc_nvme_xmt_ls_rsp_cmp should free the allocated axchg.
248 * Returns 0 if LS was handled and delivered to the transport
249 * Returns 1 if LS failed to be handled and should be dropped
252 lpfc_nvme_handle_lsreq(struct lpfc_hba *phba,
253 struct lpfc_async_xchg_ctx *axchg)
255 #if (IS_ENABLED(CONFIG_NVME_FC))
256 struct lpfc_vport *vport;
257 struct lpfc_nvme_rport *lpfc_rport;
258 struct nvme_fc_remote_port *remoteport;
259 struct lpfc_nvme_lport *lport;
260 uint32_t *payload = axchg->payload;
261 int rc;
263 vport = axchg->ndlp->vport;
264 lpfc_rport = axchg->ndlp->nrport;
265 if (!lpfc_rport)
266 return -EINVAL;
268 remoteport = lpfc_rport->remoteport;
269 if (!vport->localport)
270 return -EINVAL;
272 lport = vport->localport->private;
273 if (!lport)
274 return -EINVAL;
276 rc = nvme_fc_rcv_ls_req(remoteport, &axchg->ls_rsp, axchg->payload,
277 axchg->size);
279 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
280 "6205 NVME Unsol rcv: sz %d rc %d: %08x %08x %08x "
281 "%08x %08x %08x\n",
282 axchg->size, rc,
283 *payload, *(payload+1), *(payload+2),
284 *(payload+3), *(payload+4), *(payload+5));
286 if (!rc)
287 return 0;
288 #endif
289 return 1;
293 * __lpfc_nvme_ls_req_cmp - Generic completion handler for a NVME
294 * LS request.
295 * @phba: Pointer to HBA context object
296 * @vport: The local port that issued the LS
297 * @cmdwqe: Pointer to driver command WQE object.
298 * @wcqe: Pointer to driver response CQE object.
300 * This function is the generic completion handler for NVME LS requests.
301 * The function updates any states and statistics, calls the transport
302 * ls_req done() routine, then tears down the command and buffers used
303 * for the LS request.
305 void
306 __lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_vport *vport,
307 struct lpfc_iocbq *cmdwqe,
308 struct lpfc_wcqe_complete *wcqe)
310 struct nvmefc_ls_req *pnvme_lsreq;
311 struct lpfc_dmabuf *buf_ptr;
312 struct lpfc_nodelist *ndlp;
313 uint32_t status;
315 pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
316 ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
317 status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
319 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
320 "6047 NVMEx LS REQ %px cmpl DID %x Xri: %x "
321 "status %x reason x%x cmd:x%px lsreg:x%px bmp:x%px "
322 "ndlp:x%px\n",
323 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
324 cmdwqe->sli4_xritag, status,
325 (wcqe->parameter & 0xffff),
326 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
328 lpfc_nvmeio_data(phba, "NVMEx LS CMPL: xri x%x stat x%x parm x%x\n",
329 cmdwqe->sli4_xritag, status, wcqe->parameter);
331 if (cmdwqe->context3) {
332 buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
333 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
334 kfree(buf_ptr);
335 cmdwqe->context3 = NULL;
337 if (pnvme_lsreq->done)
338 pnvme_lsreq->done(pnvme_lsreq, status);
339 else
340 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
341 "6046 NVMEx cmpl without done call back? "
342 "Data %px DID %x Xri: %x status %x\n",
343 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
344 cmdwqe->sli4_xritag, status);
345 if (ndlp) {
346 lpfc_nlp_put(ndlp);
347 cmdwqe->context1 = NULL;
349 lpfc_sli_release_iocbq(phba, cmdwqe);
352 static void
353 lpfc_nvme_ls_req_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
354 struct lpfc_wcqe_complete *wcqe)
356 struct lpfc_vport *vport = cmdwqe->vport;
357 struct lpfc_nvme_lport *lport;
358 uint32_t status;
360 status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
362 if (vport->localport) {
363 lport = (struct lpfc_nvme_lport *)vport->localport->private;
364 if (lport) {
365 atomic_inc(&lport->fc4NvmeLsCmpls);
366 if (status) {
367 if (bf_get(lpfc_wcqe_c_xb, wcqe))
368 atomic_inc(&lport->cmpl_ls_xb);
369 atomic_inc(&lport->cmpl_ls_err);
374 __lpfc_nvme_ls_req_cmp(phba, vport, cmdwqe, wcqe);
377 static int
378 lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
379 struct lpfc_dmabuf *inp,
380 struct nvmefc_ls_req *pnvme_lsreq,
381 void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
382 struct lpfc_wcqe_complete *),
383 struct lpfc_nodelist *ndlp, uint32_t num_entry,
384 uint32_t tmo, uint8_t retry)
386 struct lpfc_hba *phba = vport->phba;
387 union lpfc_wqe128 *wqe;
388 struct lpfc_iocbq *genwqe;
389 struct ulp_bde64 *bpl;
390 struct ulp_bde64 bde;
391 int i, rc, xmit_len, first_len;
393 /* Allocate buffer for command WQE */
394 genwqe = lpfc_sli_get_iocbq(phba);
395 if (genwqe == NULL)
396 return 1;
398 wqe = &genwqe->wqe;
399 /* Initialize only 64 bytes */
400 memset(wqe, 0, sizeof(union lpfc_wqe));
402 genwqe->context3 = (uint8_t *)bmp;
403 genwqe->iocb_flag |= LPFC_IO_NVME_LS;
405 /* Save for completion so we can release these resources */
406 genwqe->context1 = lpfc_nlp_get(ndlp);
407 if (!genwqe->context1) {
408 dev_warn(&phba->pcidev->dev,
409 "Warning: Failed node ref, not sending LS_REQ\n");
410 lpfc_sli_release_iocbq(phba, genwqe);
411 return 1;
414 genwqe->context2 = (uint8_t *)pnvme_lsreq;
415 /* Fill in payload, bp points to frame payload */
417 if (!tmo)
418 /* FC spec states we need 3 * ratov for CT requests */
419 tmo = (3 * phba->fc_ratov);
421 /* For this command calculate the xmit length of the request bde. */
422 xmit_len = 0;
423 first_len = 0;
424 bpl = (struct ulp_bde64 *)bmp->virt;
425 for (i = 0; i < num_entry; i++) {
426 bde.tus.w = bpl[i].tus.w;
427 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
428 break;
429 xmit_len += bde.tus.f.bdeSize;
430 if (i == 0)
431 first_len = xmit_len;
434 genwqe->rsvd2 = num_entry;
435 genwqe->hba_wqidx = 0;
437 /* Words 0 - 2 */
438 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
439 wqe->generic.bde.tus.f.bdeSize = first_len;
440 wqe->generic.bde.addrLow = bpl[0].addrLow;
441 wqe->generic.bde.addrHigh = bpl[0].addrHigh;
443 /* Word 3 */
444 wqe->gen_req.request_payload_len = first_len;
446 /* Word 4 */
448 /* Word 5 */
449 bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
450 bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
451 bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
452 bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
453 bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
455 /* Word 6 */
456 bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
457 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
458 bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
460 /* Word 7 */
461 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
462 bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
463 bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
464 bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
466 /* Word 8 */
467 wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
469 /* Word 9 */
470 bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
472 /* Word 10 */
473 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
474 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
475 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
476 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
477 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
479 /* Word 11 */
480 bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
481 bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
484 /* Issue GEN REQ WQE for NPORT <did> */
485 genwqe->wqe_cmpl = cmpl;
486 genwqe->iocb_cmpl = NULL;
487 genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
488 genwqe->vport = vport;
489 genwqe->retry = retry;
491 lpfc_nvmeio_data(phba, "NVME LS XMIT: xri x%x iotag x%x to x%06x\n",
492 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
494 rc = lpfc_sli4_issue_wqe(phba, &phba->sli4_hba.hdwq[0], genwqe);
495 if (rc) {
496 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
497 "6045 Issue GEN REQ WQE to NPORT x%x "
498 "Data: x%x x%x rc x%x\n",
499 ndlp->nlp_DID, genwqe->iotag,
500 vport->port_state, rc);
501 lpfc_nlp_put(ndlp);
502 lpfc_sli_release_iocbq(phba, genwqe);
503 return 1;
506 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_ELS,
507 "6050 Issue GEN REQ WQE to NPORT x%x "
508 "Data: oxid: x%x state: x%x wq:x%px lsreq:x%px "
509 "bmp:x%px xmit:%d 1st:%d\n",
510 ndlp->nlp_DID, genwqe->sli4_xritag,
511 vport->port_state,
512 genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
513 return 0;
518 * __lpfc_nvme_ls_req - Generic service routine to issue an NVME LS request
519 * @vport: The local port issuing the LS
520 * @ndlp: The remote port to send the LS to
521 * @pnvme_lsreq: Pointer to LS request structure from the transport
522 * @gen_req_cmp: Completion call-back
524 * Routine validates the ndlp, builds buffers and sends a GEN_REQUEST
525 * WQE to perform the LS operation.
527 * Return value :
528 * 0 - Success
529 * non-zero: various error codes, in form of -Exxx
532 __lpfc_nvme_ls_req(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
533 struct nvmefc_ls_req *pnvme_lsreq,
534 void (*gen_req_cmp)(struct lpfc_hba *phba,
535 struct lpfc_iocbq *cmdwqe,
536 struct lpfc_wcqe_complete *wcqe))
538 struct lpfc_dmabuf *bmp;
539 struct ulp_bde64 *bpl;
540 int ret;
541 uint16_t ntype, nstate;
543 if (!ndlp) {
544 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
545 "6051 NVMEx LS REQ: Bad NDLP x%px, Failing "
546 "LS Req\n",
547 ndlp);
548 return -ENODEV;
551 ntype = ndlp->nlp_type;
552 nstate = ndlp->nlp_state;
553 if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
554 (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
555 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
556 "6088 NVMEx LS REQ: Fail DID x%06x not "
557 "ready for IO. Type x%x, State x%x\n",
558 ndlp->nlp_DID, ntype, nstate);
559 return -ENODEV;
563 * there are two dma buf in the request, actually there is one and
564 * the second one is just the start address + cmd size.
565 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
566 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
567 * because the nvem layer owns the data bufs.
568 * We do not have to break these packets open, we don't care what is
569 * in them. And we do not have to look at the resonse data, we only
570 * care that we got a response. All of the caring is going to happen
571 * in the nvme-fc layer.
574 bmp = kmalloc(sizeof(*bmp), GFP_KERNEL);
575 if (!bmp) {
576 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
577 "6044 NVMEx LS REQ: Could not alloc LS buf "
578 "for DID %x\n",
579 ndlp->nlp_DID);
580 return -ENOMEM;
583 bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
584 if (!bmp->virt) {
585 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
586 "6042 NVMEx LS REQ: Could not alloc mbuf "
587 "for DID %x\n",
588 ndlp->nlp_DID);
589 kfree(bmp);
590 return -ENOMEM;
593 INIT_LIST_HEAD(&bmp->list);
595 bpl = (struct ulp_bde64 *)bmp->virt;
596 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
597 bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
598 bpl->tus.f.bdeFlags = 0;
599 bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
600 bpl->tus.w = le32_to_cpu(bpl->tus.w);
601 bpl++;
603 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
604 bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
605 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
606 bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
607 bpl->tus.w = le32_to_cpu(bpl->tus.w);
609 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
610 "6149 NVMEx LS REQ: Issue to DID 0x%06x lsreq x%px, "
611 "rqstlen:%d rsplen:%d %pad %pad\n",
612 ndlp->nlp_DID, pnvme_lsreq, pnvme_lsreq->rqstlen,
613 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
614 &pnvme_lsreq->rspdma);
616 ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
617 pnvme_lsreq, gen_req_cmp, ndlp, 2,
618 LPFC_NVME_LS_TIMEOUT, 0);
619 if (ret != WQE_SUCCESS) {
620 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
621 "6052 NVMEx REQ: EXIT. issue ls wqe failed "
622 "lsreq x%px Status %x DID %x\n",
623 pnvme_lsreq, ret, ndlp->nlp_DID);
624 lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
625 kfree(bmp);
626 return -EIO;
629 return 0;
633 * lpfc_nvme_ls_req - Issue an NVME Link Service request
634 * @pnvme_lport: Transport localport that LS is to be issued from.
635 * @pnvme_rport: Transport remoteport that LS is to be sent to.
636 * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
638 * Driver registers this routine to handle any link service request
639 * from the nvme_fc transport to a remote nvme-aware port.
641 * Return value :
642 * 0 - Success
643 * non-zero: various error codes, in form of -Exxx
645 static int
646 lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
647 struct nvme_fc_remote_port *pnvme_rport,
648 struct nvmefc_ls_req *pnvme_lsreq)
650 struct lpfc_nvme_lport *lport;
651 struct lpfc_nvme_rport *rport;
652 struct lpfc_vport *vport;
653 int ret;
655 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
656 rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
657 if (unlikely(!lport) || unlikely(!rport))
658 return -EINVAL;
660 vport = lport->vport;
661 if (vport->load_flag & FC_UNLOADING)
662 return -ENODEV;
664 atomic_inc(&lport->fc4NvmeLsRequests);
666 ret = __lpfc_nvme_ls_req(vport, rport->ndlp, pnvme_lsreq,
667 lpfc_nvme_ls_req_cmp);
668 if (ret)
669 atomic_inc(&lport->xmt_ls_err);
671 return ret;
675 * __lpfc_nvme_ls_abort - Generic service routine to abort a prior
676 * NVME LS request
677 * @vport: The local port that issued the LS
678 * @ndlp: The remote port the LS was sent to
679 * @pnvme_lsreq: Pointer to LS request structure from the transport
681 * The driver validates the ndlp, looks for the LS, and aborts the
682 * LS if found.
684 * Returns:
685 * 0 : if LS found and aborted
686 * non-zero: various error conditions in form -Exxx
689 __lpfc_nvme_ls_abort(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp,
690 struct nvmefc_ls_req *pnvme_lsreq)
692 struct lpfc_hba *phba = vport->phba;
693 struct lpfc_sli_ring *pring;
694 struct lpfc_iocbq *wqe, *next_wqe;
695 bool foundit = false;
697 if (!ndlp) {
698 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
699 "6049 NVMEx LS REQ Abort: Bad NDLP x%px DID "
700 "x%06x, Failing LS Req\n",
701 ndlp, ndlp ? ndlp->nlp_DID : 0);
702 return -EINVAL;
705 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
706 "6040 NVMEx LS REQ Abort: Issue LS_ABORT for lsreq "
707 "x%p rqstlen:%d rsplen:%d %pad %pad\n",
708 pnvme_lsreq, pnvme_lsreq->rqstlen,
709 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
710 &pnvme_lsreq->rspdma);
713 * Lock the ELS ring txcmplq and look for the wqe that matches
714 * this ELS. If found, issue an abort on the wqe.
716 pring = phba->sli4_hba.nvmels_wq->pring;
717 spin_lock_irq(&phba->hbalock);
718 spin_lock(&pring->ring_lock);
719 list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
720 if (wqe->context2 == pnvme_lsreq) {
721 wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
722 foundit = true;
723 break;
726 spin_unlock(&pring->ring_lock);
728 if (foundit)
729 lpfc_sli_issue_abort_iotag(phba, pring, wqe, NULL);
730 spin_unlock_irq(&phba->hbalock);
732 if (foundit)
733 return 0;
735 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC | LOG_NVME_ABTS,
736 "6213 NVMEx LS REQ Abort: Unable to locate req x%p\n",
737 pnvme_lsreq);
738 return -EINVAL;
741 static int
742 lpfc_nvme_xmt_ls_rsp(struct nvme_fc_local_port *localport,
743 struct nvme_fc_remote_port *remoteport,
744 struct nvmefc_ls_rsp *ls_rsp)
746 struct lpfc_async_xchg_ctx *axchg =
747 container_of(ls_rsp, struct lpfc_async_xchg_ctx, ls_rsp);
748 struct lpfc_nvme_lport *lport;
749 int rc;
751 if (axchg->phba->pport->load_flag & FC_UNLOADING)
752 return -ENODEV;
754 lport = (struct lpfc_nvme_lport *)localport->private;
756 rc = __lpfc_nvme_xmt_ls_rsp(axchg, ls_rsp, __lpfc_nvme_xmt_ls_rsp_cmp);
758 if (rc) {
760 * unless the failure is due to having already sent
761 * the response, an abort will be generated for the
762 * exchange if the rsp can't be sent.
764 if (rc != -EALREADY)
765 atomic_inc(&lport->xmt_ls_abort);
766 return rc;
769 return 0;
773 * lpfc_nvme_ls_abort - Abort a prior NVME LS request
774 * @pnvme_lport: Transport localport that LS is to be issued from.
775 * @pnvme_rport: Transport remoteport that LS is to be sent to.
776 * @pnvme_lsreq: the transport nvme_ls_req structure for the LS
778 * Driver registers this routine to abort a NVME LS request that is
779 * in progress (from the transports perspective).
781 static void
782 lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
783 struct nvme_fc_remote_port *pnvme_rport,
784 struct nvmefc_ls_req *pnvme_lsreq)
786 struct lpfc_nvme_lport *lport;
787 struct lpfc_vport *vport;
788 struct lpfc_nodelist *ndlp;
789 int ret;
791 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
792 if (unlikely(!lport))
793 return;
794 vport = lport->vport;
796 if (vport->load_flag & FC_UNLOADING)
797 return;
799 ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
801 ret = __lpfc_nvme_ls_abort(vport, ndlp, pnvme_lsreq);
802 if (!ret)
803 atomic_inc(&lport->xmt_ls_abort);
806 /* Fix up the existing sgls for NVME IO. */
807 static inline void
808 lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
809 struct lpfc_io_buf *lpfc_ncmd,
810 struct nvmefc_fcp_req *nCmd)
812 struct lpfc_hba *phba = vport->phba;
813 struct sli4_sge *sgl;
814 union lpfc_wqe128 *wqe;
815 uint32_t *wptr, *dptr;
818 * Get a local pointer to the built-in wqe and correct
819 * the cmd size to match NVME's 96 bytes and fix
820 * the dma address.
823 wqe = &lpfc_ncmd->cur_iocbq.wqe;
826 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
827 * match NVME. NVME sends 96 bytes. Also, use the
828 * nvme commands command and response dma addresses
829 * rather than the virtual memory to ease the restore
830 * operation.
832 sgl = lpfc_ncmd->dma_sgl;
833 sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
834 if (phba->cfg_nvme_embed_cmd) {
835 sgl->addr_hi = 0;
836 sgl->addr_lo = 0;
838 /* Word 0-2 - NVME CMND IU (embedded payload) */
839 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
840 wqe->generic.bde.tus.f.bdeSize = 56;
841 wqe->generic.bde.addrHigh = 0;
842 wqe->generic.bde.addrLow = 64; /* Word 16 */
844 /* Word 10 - dbde is 0, wqes is 1 in template */
847 * Embed the payload in the last half of the WQE
848 * WQE words 16-30 get the NVME CMD IU payload
850 * WQE words 16-19 get payload Words 1-4
851 * WQE words 20-21 get payload Words 6-7
852 * WQE words 22-29 get payload Words 16-23
854 wptr = &wqe->words[16]; /* WQE ptr */
855 dptr = (uint32_t *)nCmd->cmdaddr; /* payload ptr */
856 dptr++; /* Skip Word 0 in payload */
858 *wptr++ = *dptr++; /* Word 1 */
859 *wptr++ = *dptr++; /* Word 2 */
860 *wptr++ = *dptr++; /* Word 3 */
861 *wptr++ = *dptr++; /* Word 4 */
862 dptr++; /* Skip Word 5 in payload */
863 *wptr++ = *dptr++; /* Word 6 */
864 *wptr++ = *dptr++; /* Word 7 */
865 dptr += 8; /* Skip Words 8-15 in payload */
866 *wptr++ = *dptr++; /* Word 16 */
867 *wptr++ = *dptr++; /* Word 17 */
868 *wptr++ = *dptr++; /* Word 18 */
869 *wptr++ = *dptr++; /* Word 19 */
870 *wptr++ = *dptr++; /* Word 20 */
871 *wptr++ = *dptr++; /* Word 21 */
872 *wptr++ = *dptr++; /* Word 22 */
873 *wptr = *dptr; /* Word 23 */
874 } else {
875 sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
876 sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
878 /* Word 0-2 - NVME CMND IU Inline BDE */
879 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
880 wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
881 wqe->generic.bde.addrHigh = sgl->addr_hi;
882 wqe->generic.bde.addrLow = sgl->addr_lo;
884 /* Word 10 */
885 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
886 bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
889 sgl++;
891 /* Setup the physical region for the FCP RSP */
892 sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
893 sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
894 sgl->word2 = le32_to_cpu(sgl->word2);
895 if (nCmd->sg_cnt)
896 bf_set(lpfc_sli4_sge_last, sgl, 0);
897 else
898 bf_set(lpfc_sli4_sge_last, sgl, 1);
899 sgl->word2 = cpu_to_le32(sgl->word2);
900 sgl->sge_len = cpu_to_le32(nCmd->rsplen);
905 * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
907 * Driver registers this routine as it io request handler. This
908 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
909 * data structure to the rport indicated in @lpfc_nvme_rport.
911 * Return value :
912 * 0 - Success
913 * TODO: What are the failure codes.
915 static void
916 lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
917 struct lpfc_wcqe_complete *wcqe)
919 struct lpfc_io_buf *lpfc_ncmd =
920 (struct lpfc_io_buf *)pwqeIn->context1;
921 struct lpfc_vport *vport = pwqeIn->vport;
922 struct nvmefc_fcp_req *nCmd;
923 struct nvme_fc_ersp_iu *ep;
924 struct nvme_fc_cmd_iu *cp;
925 struct lpfc_nodelist *ndlp;
926 struct lpfc_nvme_fcpreq_priv *freqpriv;
927 struct lpfc_nvme_lport *lport;
928 uint32_t code, status, idx;
929 uint16_t cid, sqhd, data;
930 uint32_t *ptr;
931 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
932 int cpu;
933 #endif
935 /* Sanity check on return of outstanding command */
936 if (!lpfc_ncmd) {
937 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
938 "6071 Null lpfc_ncmd pointer. No "
939 "release, skip completion\n");
940 return;
943 /* Guard against abort handler being called at same time */
944 spin_lock(&lpfc_ncmd->buf_lock);
946 if (!lpfc_ncmd->nvmeCmd) {
947 spin_unlock(&lpfc_ncmd->buf_lock);
948 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
949 "6066 Missing cmpl ptrs: lpfc_ncmd x%px, "
950 "nvmeCmd x%px\n",
951 lpfc_ncmd, lpfc_ncmd->nvmeCmd);
953 /* Release the lpfc_ncmd regardless of the missing elements. */
954 lpfc_release_nvme_buf(phba, lpfc_ncmd);
955 return;
957 nCmd = lpfc_ncmd->nvmeCmd;
958 status = bf_get(lpfc_wcqe_c_status, wcqe);
960 idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
961 phba->sli4_hba.hdwq[idx].nvme_cstat.io_cmpls++;
963 if (unlikely(status && vport->localport)) {
964 lport = (struct lpfc_nvme_lport *)vport->localport->private;
965 if (lport) {
966 if (bf_get(lpfc_wcqe_c_xb, wcqe))
967 atomic_inc(&lport->cmpl_fcp_xb);
968 atomic_inc(&lport->cmpl_fcp_err);
972 lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
973 lpfc_ncmd->cur_iocbq.sli4_xritag,
974 status, wcqe->parameter);
976 * Catch race where our node has transitioned, but the
977 * transport is still transitioning.
979 ndlp = lpfc_ncmd->ndlp;
980 if (!ndlp) {
981 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
982 "6062 Ignoring NVME cmpl. No ndlp\n");
983 goto out_err;
986 code = bf_get(lpfc_wcqe_c_code, wcqe);
987 if (code == CQE_CODE_NVME_ERSP) {
988 /* For this type of CQE, we need to rebuild the rsp */
989 ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
992 * Get Command Id from cmd to plug into response. This
993 * code is not needed in the next NVME Transport drop.
995 cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
996 cid = cp->sqe.common.command_id;
999 * RSN is in CQE word 2
1000 * SQHD is in CQE Word 3 bits 15:0
1001 * Cmd Specific info is in CQE Word 1
1002 * and in CQE Word 0 bits 15:0
1004 sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
1006 /* Now lets build the NVME ERSP IU */
1007 ep->iu_len = cpu_to_be16(8);
1008 ep->rsn = wcqe->parameter;
1009 ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
1010 ep->rsvd12 = 0;
1011 ptr = (uint32_t *)&ep->cqe.result.u64;
1012 *ptr++ = wcqe->total_data_placed;
1013 data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
1014 *ptr = (uint32_t)data;
1015 ep->cqe.sq_head = sqhd;
1016 ep->cqe.sq_id = nCmd->sqid;
1017 ep->cqe.command_id = cid;
1018 ep->cqe.status = 0;
1020 lpfc_ncmd->status = IOSTAT_SUCCESS;
1021 lpfc_ncmd->result = 0;
1022 nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
1023 nCmd->transferred_length = nCmd->payload_length;
1024 } else {
1025 lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
1026 lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
1028 /* For NVME, the only failure path that results in an
1029 * IO error is when the adapter rejects it. All other
1030 * conditions are a success case and resolved by the
1031 * transport.
1032 * IOSTAT_FCP_RSP_ERROR means:
1033 * 1. Length of data received doesn't match total
1034 * transfer length in WQE
1035 * 2. If the RSP payload does NOT match these cases:
1036 * a. RSP length 12/24 bytes and all zeros
1037 * b. NVME ERSP
1039 switch (lpfc_ncmd->status) {
1040 case IOSTAT_SUCCESS:
1041 nCmd->transferred_length = wcqe->total_data_placed;
1042 nCmd->rcv_rsplen = 0;
1043 nCmd->status = 0;
1044 break;
1045 case IOSTAT_FCP_RSP_ERROR:
1046 nCmd->transferred_length = wcqe->total_data_placed;
1047 nCmd->rcv_rsplen = wcqe->parameter;
1048 nCmd->status = 0;
1049 /* Sanity check */
1050 if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
1051 break;
1052 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1053 "6081 NVME Completion Protocol Error: "
1054 "xri %x status x%x result x%x "
1055 "placed x%x\n",
1056 lpfc_ncmd->cur_iocbq.sli4_xritag,
1057 lpfc_ncmd->status, lpfc_ncmd->result,
1058 wcqe->total_data_placed);
1059 break;
1060 case IOSTAT_LOCAL_REJECT:
1061 /* Let fall through to set command final state. */
1062 if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1063 lpfc_printf_vlog(vport, KERN_INFO,
1064 LOG_NVME_IOERR,
1065 "6032 Delay Aborted cmd x%px "
1066 "nvme cmd x%px, xri x%x, "
1067 "xb %d\n",
1068 lpfc_ncmd, nCmd,
1069 lpfc_ncmd->cur_iocbq.sli4_xritag,
1070 bf_get(lpfc_wcqe_c_xb, wcqe));
1071 fallthrough;
1072 default:
1073 out_err:
1074 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1075 "6072 NVME Completion Error: xri %x "
1076 "status x%x result x%x [x%x] "
1077 "placed x%x\n",
1078 lpfc_ncmd->cur_iocbq.sli4_xritag,
1079 lpfc_ncmd->status, lpfc_ncmd->result,
1080 wcqe->parameter,
1081 wcqe->total_data_placed);
1082 nCmd->transferred_length = 0;
1083 nCmd->rcv_rsplen = 0;
1084 nCmd->status = NVME_SC_INTERNAL;
1088 /* pick up SLI4 exhange busy condition */
1089 if (bf_get(lpfc_wcqe_c_xb, wcqe))
1090 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
1091 else
1092 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
1094 /* Update stats and complete the IO. There is
1095 * no need for dma unprep because the nvme_transport
1096 * owns the dma address.
1098 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1099 if (lpfc_ncmd->ts_cmd_start) {
1100 lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
1101 lpfc_ncmd->ts_data_io = ktime_get_ns();
1102 phba->ktime_last_cmd = lpfc_ncmd->ts_data_io;
1103 lpfc_io_ktime(phba, lpfc_ncmd);
1105 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_NVME_IO)) {
1106 cpu = raw_smp_processor_id();
1107 this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
1108 if (lpfc_ncmd->cpu != cpu)
1109 lpfc_printf_vlog(vport,
1110 KERN_INFO, LOG_NVME_IOERR,
1111 "6701 CPU Check cmpl: "
1112 "cpu %d expect %d\n",
1113 cpu, lpfc_ncmd->cpu);
1115 #endif
1117 /* NVME targets need completion held off until the abort exchange
1118 * completes unless the NVME Rport is getting unregistered.
1121 if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
1122 freqpriv = nCmd->private;
1123 freqpriv->nvme_buf = NULL;
1124 lpfc_ncmd->nvmeCmd = NULL;
1125 spin_unlock(&lpfc_ncmd->buf_lock);
1126 nCmd->done(nCmd);
1127 } else
1128 spin_unlock(&lpfc_ncmd->buf_lock);
1130 /* Call release with XB=1 to queue the IO into the abort list. */
1131 lpfc_release_nvme_buf(phba, lpfc_ncmd);
1136 * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
1137 * @vport: pointer to a host virtual N_Port data structure
1138 * @lpfc_ncmd: Pointer to lpfc scsi command
1139 * @pnode: pointer to a node-list data structure
1140 * @cstat: pointer to the control status structure
1142 * Driver registers this routine as it io request handler. This
1143 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1144 * data structure to the rport indicated in @lpfc_nvme_rport.
1146 * Return value :
1147 * 0 - Success
1148 * TODO: What are the failure codes.
1150 static int
1151 lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1152 struct lpfc_io_buf *lpfc_ncmd,
1153 struct lpfc_nodelist *pnode,
1154 struct lpfc_fc4_ctrl_stat *cstat)
1156 struct lpfc_hba *phba = vport->phba;
1157 struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1158 struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
1159 union lpfc_wqe128 *wqe = &pwqeq->wqe;
1160 uint32_t req_len;
1163 * There are three possibilities here - use scatter-gather segment, use
1164 * the single mapping, or neither.
1166 if (nCmd->sg_cnt) {
1167 if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
1168 /* From the iwrite template, initialize words 7 - 11 */
1169 memcpy(&wqe->words[7],
1170 &lpfc_iwrite_cmd_template.words[7],
1171 sizeof(uint32_t) * 5);
1173 /* Word 4 */
1174 wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
1176 /* Word 5 */
1177 if ((phba->cfg_nvme_enable_fb) &&
1178 (pnode->nlp_flag & NLP_FIRSTBURST)) {
1179 req_len = lpfc_ncmd->nvmeCmd->payload_length;
1180 if (req_len < pnode->nvme_fb_size)
1181 wqe->fcp_iwrite.initial_xfer_len =
1182 req_len;
1183 else
1184 wqe->fcp_iwrite.initial_xfer_len =
1185 pnode->nvme_fb_size;
1186 } else {
1187 wqe->fcp_iwrite.initial_xfer_len = 0;
1189 cstat->output_requests++;
1190 } else {
1191 /* From the iread template, initialize words 7 - 11 */
1192 memcpy(&wqe->words[7],
1193 &lpfc_iread_cmd_template.words[7],
1194 sizeof(uint32_t) * 5);
1196 /* Word 4 */
1197 wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
1199 /* Word 5 */
1200 wqe->fcp_iread.rsrvd5 = 0;
1202 cstat->input_requests++;
1204 } else {
1205 /* From the icmnd template, initialize words 4 - 11 */
1206 memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
1207 sizeof(uint32_t) * 8);
1208 cstat->control_requests++;
1211 if (pnode->nlp_nvme_info & NLP_NVME_NSLER)
1212 bf_set(wqe_erp, &wqe->generic.wqe_com, 1);
1214 * Finish initializing those WQE fields that are independent
1215 * of the nvme_cmnd request_buffer
1218 /* Word 3 */
1219 bf_set(payload_offset_len, &wqe->fcp_icmd,
1220 (nCmd->rsplen + nCmd->cmdlen));
1222 /* Word 6 */
1223 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
1224 phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
1225 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
1227 /* Word 8 */
1228 wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
1230 /* Word 9 */
1231 bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
1233 /* Word 10 */
1234 bf_set(wqe_xchg, &wqe->fcp_iwrite.wqe_com, LPFC_NVME_XCHG);
1236 /* Words 13 14 15 are for PBDE support */
1238 pwqeq->vport = vport;
1239 return 0;
1244 * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
1245 * @vport: pointer to a host virtual N_Port data structure
1246 * @lpfc_ncmd: Pointer to lpfc scsi command
1248 * Driver registers this routine as it io request handler. This
1249 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1250 * data structure to the rport indicated in @lpfc_nvme_rport.
1252 * Return value :
1253 * 0 - Success
1254 * TODO: What are the failure codes.
1256 static int
1257 lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1258 struct lpfc_io_buf *lpfc_ncmd)
1260 struct lpfc_hba *phba = vport->phba;
1261 struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1262 union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
1263 struct sli4_sge *sgl = lpfc_ncmd->dma_sgl;
1264 struct sli4_hybrid_sgl *sgl_xtra = NULL;
1265 struct scatterlist *data_sg;
1266 struct sli4_sge *first_data_sgl;
1267 struct ulp_bde64 *bde;
1268 dma_addr_t physaddr = 0;
1269 uint32_t num_bde = 0;
1270 uint32_t dma_len = 0;
1271 uint32_t dma_offset = 0;
1272 int nseg, i, j;
1273 bool lsp_just_set = false;
1275 /* Fix up the command and response DMA stuff. */
1276 lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
1279 * There are three possibilities here - use scatter-gather segment, use
1280 * the single mapping, or neither.
1282 if (nCmd->sg_cnt) {
1284 * Jump over the cmd and rsp SGEs. The fix routine
1285 * has already adjusted for this.
1287 sgl += 2;
1289 first_data_sgl = sgl;
1290 lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
1291 if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
1292 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1293 "6058 Too many sg segments from "
1294 "NVME Transport. Max %d, "
1295 "nvmeIO sg_cnt %d\n",
1296 phba->cfg_nvme_seg_cnt + 1,
1297 lpfc_ncmd->seg_cnt);
1298 lpfc_ncmd->seg_cnt = 0;
1299 return 1;
1303 * The driver established a maximum scatter-gather segment count
1304 * during probe that limits the number of sg elements in any
1305 * single nvme command. Just run through the seg_cnt and format
1306 * the sge's.
1308 nseg = nCmd->sg_cnt;
1309 data_sg = nCmd->first_sgl;
1311 /* for tracking the segment boundaries */
1312 j = 2;
1313 for (i = 0; i < nseg; i++) {
1314 if (data_sg == NULL) {
1315 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1316 "6059 dptr err %d, nseg %d\n",
1317 i, nseg);
1318 lpfc_ncmd->seg_cnt = 0;
1319 return 1;
1322 sgl->word2 = 0;
1323 if ((num_bde + 1) == nseg) {
1324 bf_set(lpfc_sli4_sge_last, sgl, 1);
1325 bf_set(lpfc_sli4_sge_type, sgl,
1326 LPFC_SGE_TYPE_DATA);
1327 } else {
1328 bf_set(lpfc_sli4_sge_last, sgl, 0);
1330 /* expand the segment */
1331 if (!lsp_just_set &&
1332 !((j + 1) % phba->border_sge_num) &&
1333 ((nseg - 1) != i)) {
1334 /* set LSP type */
1335 bf_set(lpfc_sli4_sge_type, sgl,
1336 LPFC_SGE_TYPE_LSP);
1338 sgl_xtra = lpfc_get_sgl_per_hdwq(
1339 phba, lpfc_ncmd);
1341 if (unlikely(!sgl_xtra)) {
1342 lpfc_ncmd->seg_cnt = 0;
1343 return 1;
1345 sgl->addr_lo = cpu_to_le32(putPaddrLow(
1346 sgl_xtra->dma_phys_sgl));
1347 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
1348 sgl_xtra->dma_phys_sgl));
1350 } else {
1351 bf_set(lpfc_sli4_sge_type, sgl,
1352 LPFC_SGE_TYPE_DATA);
1356 if (!(bf_get(lpfc_sli4_sge_type, sgl) &
1357 LPFC_SGE_TYPE_LSP)) {
1358 if ((nseg - 1) == i)
1359 bf_set(lpfc_sli4_sge_last, sgl, 1);
1361 physaddr = data_sg->dma_address;
1362 dma_len = data_sg->length;
1363 sgl->addr_lo = cpu_to_le32(
1364 putPaddrLow(physaddr));
1365 sgl->addr_hi = cpu_to_le32(
1366 putPaddrHigh(physaddr));
1368 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1369 sgl->word2 = cpu_to_le32(sgl->word2);
1370 sgl->sge_len = cpu_to_le32(dma_len);
1372 dma_offset += dma_len;
1373 data_sg = sg_next(data_sg);
1375 sgl++;
1377 lsp_just_set = false;
1378 } else {
1379 sgl->word2 = cpu_to_le32(sgl->word2);
1381 sgl->sge_len = cpu_to_le32(
1382 phba->cfg_sg_dma_buf_size);
1384 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
1385 i = i - 1;
1387 lsp_just_set = true;
1390 j++;
1392 if (phba->cfg_enable_pbde) {
1393 /* Use PBDE support for first SGL only, offset == 0 */
1394 /* Words 13-15 */
1395 bde = (struct ulp_bde64 *)
1396 &wqe->words[13];
1397 bde->addrLow = first_data_sgl->addr_lo;
1398 bde->addrHigh = first_data_sgl->addr_hi;
1399 bde->tus.f.bdeSize =
1400 le32_to_cpu(first_data_sgl->sge_len);
1401 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1402 bde->tus.w = cpu_to_le32(bde->tus.w);
1404 /* Word 11 */
1405 bf_set(wqe_pbde, &wqe->generic.wqe_com, 1);
1406 } else {
1407 memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
1408 bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
1411 } else {
1412 lpfc_ncmd->seg_cnt = 0;
1414 /* For this clause to be valid, the payload_length
1415 * and sg_cnt must zero.
1417 if (nCmd->payload_length != 0) {
1418 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1419 "6063 NVME DMA Prep Err: sg_cnt %d "
1420 "payload_length x%x\n",
1421 nCmd->sg_cnt, nCmd->payload_length);
1422 return 1;
1425 return 0;
1429 * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
1430 * @pnvme_lport: Pointer to the driver's local port data
1431 * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1432 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1433 * @pnvme_fcreq: IO request from nvme fc to driver.
1435 * Driver registers this routine as it io request handler. This
1436 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1437 * data structure to the rport indicated in @lpfc_nvme_rport.
1439 * Return value :
1440 * 0 - Success
1441 * TODO: What are the failure codes.
1443 static int
1444 lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
1445 struct nvme_fc_remote_port *pnvme_rport,
1446 void *hw_queue_handle,
1447 struct nvmefc_fcp_req *pnvme_fcreq)
1449 int ret = 0;
1450 int expedite = 0;
1451 int idx, cpu;
1452 struct lpfc_nvme_lport *lport;
1453 struct lpfc_fc4_ctrl_stat *cstat;
1454 struct lpfc_vport *vport;
1455 struct lpfc_hba *phba;
1456 struct lpfc_nodelist *ndlp;
1457 struct lpfc_io_buf *lpfc_ncmd;
1458 struct lpfc_nvme_rport *rport;
1459 struct lpfc_nvme_qhandle *lpfc_queue_info;
1460 struct lpfc_nvme_fcpreq_priv *freqpriv;
1461 struct nvme_common_command *sqe;
1462 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1463 uint64_t start = 0;
1464 #endif
1466 /* Validate pointers. LLDD fault handling with transport does
1467 * have timing races.
1469 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1470 if (unlikely(!lport)) {
1471 ret = -EINVAL;
1472 goto out_fail;
1475 vport = lport->vport;
1477 if (unlikely(!hw_queue_handle)) {
1478 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1479 "6117 Fail IO, NULL hw_queue_handle\n");
1480 atomic_inc(&lport->xmt_fcp_err);
1481 ret = -EBUSY;
1482 goto out_fail;
1485 phba = vport->phba;
1487 if (unlikely(vport->load_flag & FC_UNLOADING)) {
1488 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1489 "6124 Fail IO, Driver unload\n");
1490 atomic_inc(&lport->xmt_fcp_err);
1491 ret = -ENODEV;
1492 goto out_fail;
1495 freqpriv = pnvme_fcreq->private;
1496 if (unlikely(!freqpriv)) {
1497 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1498 "6158 Fail IO, NULL request data\n");
1499 atomic_inc(&lport->xmt_fcp_err);
1500 ret = -EINVAL;
1501 goto out_fail;
1504 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1505 if (phba->ktime_on)
1506 start = ktime_get_ns();
1507 #endif
1508 rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
1509 lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
1512 * Catch race where our node has transitioned, but the
1513 * transport is still transitioning.
1515 ndlp = rport->ndlp;
1516 if (!ndlp) {
1517 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
1518 "6053 Busy IO, ndlp not ready: rport x%px "
1519 "ndlp x%px, DID x%06x\n",
1520 rport, ndlp, pnvme_rport->port_id);
1521 atomic_inc(&lport->xmt_fcp_err);
1522 ret = -EBUSY;
1523 goto out_fail;
1526 /* The remote node has to be a mapped target or it's an error. */
1527 if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
1528 (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
1529 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
1530 "6036 Fail IO, DID x%06x not ready for "
1531 "IO. State x%x, Type x%x Flg x%x\n",
1532 pnvme_rport->port_id,
1533 ndlp->nlp_state, ndlp->nlp_type,
1534 ndlp->fc4_xpt_flags);
1535 atomic_inc(&lport->xmt_fcp_bad_ndlp);
1536 ret = -EBUSY;
1537 goto out_fail;
1541 /* Currently only NVME Keep alive commands should be expedited
1542 * if the driver runs out of a resource. These should only be
1543 * issued on the admin queue, qidx 0
1545 if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1546 sqe = &((struct nvme_fc_cmd_iu *)
1547 pnvme_fcreq->cmdaddr)->sqe.common;
1548 if (sqe->opcode == nvme_admin_keep_alive)
1549 expedite = 1;
1552 /* The node is shared with FCP IO, make sure the IO pending count does
1553 * not exceed the programmed depth.
1555 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1556 if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1557 !expedite) {
1558 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1559 "6174 Fail IO, ndlp qdepth exceeded: "
1560 "idx %d DID %x pend %d qdepth %d\n",
1561 lpfc_queue_info->index, ndlp->nlp_DID,
1562 atomic_read(&ndlp->cmd_pending),
1563 ndlp->cmd_qdepth);
1564 atomic_inc(&lport->xmt_fcp_qdepth);
1565 ret = -EBUSY;
1566 goto out_fail;
1570 /* Lookup Hardware Queue index based on fcp_io_sched module parameter */
1571 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
1572 idx = lpfc_queue_info->index;
1573 } else {
1574 cpu = raw_smp_processor_id();
1575 idx = phba->sli4_hba.cpu_map[cpu].hdwq;
1578 lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, idx, expedite);
1579 if (lpfc_ncmd == NULL) {
1580 atomic_inc(&lport->xmt_fcp_noxri);
1581 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1582 "6065 Fail IO, driver buffer pool is empty: "
1583 "idx %d DID %x\n",
1584 lpfc_queue_info->index, ndlp->nlp_DID);
1585 ret = -EBUSY;
1586 goto out_fail;
1588 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1589 if (start) {
1590 lpfc_ncmd->ts_cmd_start = start;
1591 lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
1592 } else {
1593 lpfc_ncmd->ts_cmd_start = 0;
1595 #endif
1598 * Store the data needed by the driver to issue, abort, and complete
1599 * an IO.
1600 * Do not let the IO hang out forever. There is no midlayer issuing
1601 * an abort so inform the FW of the maximum IO pending time.
1603 freqpriv->nvme_buf = lpfc_ncmd;
1604 lpfc_ncmd->nvmeCmd = pnvme_fcreq;
1605 lpfc_ncmd->ndlp = ndlp;
1606 lpfc_ncmd->qidx = lpfc_queue_info->qidx;
1609 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
1610 * This identfier was create in our hardware queue create callback
1611 * routine. The driver now is dependent on the IO queue steering from
1612 * the transport. We are trusting the upper NVME layers know which
1613 * index to use and that they have affinitized a CPU to this hardware
1614 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
1616 lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
1617 cstat = &phba->sli4_hba.hdwq[idx].nvme_cstat;
1619 lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
1620 ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
1621 if (ret) {
1622 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1623 "6175 Fail IO, Prep DMA: "
1624 "idx %d DID %x\n",
1625 lpfc_queue_info->index, ndlp->nlp_DID);
1626 atomic_inc(&lport->xmt_fcp_err);
1627 ret = -ENOMEM;
1628 goto out_free_nvme_buf;
1631 lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1632 lpfc_ncmd->cur_iocbq.sli4_xritag,
1633 lpfc_queue_info->index, ndlp->nlp_DID);
1635 ret = lpfc_sli4_issue_wqe(phba, lpfc_ncmd->hdwq, &lpfc_ncmd->cur_iocbq);
1636 if (ret) {
1637 atomic_inc(&lport->xmt_fcp_wqerr);
1638 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1639 "6113 Fail IO, Could not issue WQE err %x "
1640 "sid: x%x did: x%x oxid: x%x\n",
1641 ret, vport->fc_myDID, ndlp->nlp_DID,
1642 lpfc_ncmd->cur_iocbq.sli4_xritag);
1643 goto out_free_nvme_buf;
1646 if (phba->cfg_xri_rebalancing)
1647 lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_ncmd->hdwq_no);
1649 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1650 if (lpfc_ncmd->ts_cmd_start)
1651 lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1653 if (phba->hdwqstat_on & LPFC_CHECK_NVME_IO) {
1654 cpu = raw_smp_processor_id();
1655 this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
1656 lpfc_ncmd->cpu = cpu;
1657 if (idx != cpu)
1658 lpfc_printf_vlog(vport,
1659 KERN_INFO, LOG_NVME_IOERR,
1660 "6702 CPU Check cmd: "
1661 "cpu %d wq %d\n",
1662 lpfc_ncmd->cpu,
1663 lpfc_queue_info->index);
1665 #endif
1666 return 0;
1668 out_free_nvme_buf:
1669 if (lpfc_ncmd->nvmeCmd->sg_cnt) {
1670 if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
1671 cstat->output_requests--;
1672 else
1673 cstat->input_requests--;
1674 } else
1675 cstat->control_requests--;
1676 lpfc_release_nvme_buf(phba, lpfc_ncmd);
1677 out_fail:
1678 return ret;
1682 * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
1683 * @phba: Pointer to HBA context object
1684 * @cmdiocb: Pointer to command iocb object.
1685 * @abts_cmpl: Pointer to wcqe complete object.
1687 * This is the callback function for any NVME FCP IO that was aborted.
1689 * Return value:
1690 * None
1692 void
1693 lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1694 struct lpfc_wcqe_complete *abts_cmpl)
1696 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
1697 "6145 ABORT_XRI_CN completing on rpi x%x "
1698 "original iotag x%x, abort cmd iotag x%x "
1699 "req_tag x%x, status x%x, hwstatus x%x\n",
1700 cmdiocb->iocb.un.acxri.abortContextTag,
1701 cmdiocb->iocb.un.acxri.abortIoTag,
1702 cmdiocb->iotag,
1703 bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
1704 bf_get(lpfc_wcqe_c_status, abts_cmpl),
1705 bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
1706 lpfc_sli_release_iocbq(phba, cmdiocb);
1710 * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
1711 * @pnvme_lport: Pointer to the driver's local port data
1712 * @pnvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1713 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1714 * @pnvme_fcreq: IO request from nvme fc to driver.
1716 * Driver registers this routine as its nvme request io abort handler. This
1717 * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
1718 * data structure to the rport indicated in @lpfc_nvme_rport. This routine
1719 * is executed asynchronously - one the target is validated as "MAPPED" and
1720 * ready for IO, the driver issues the abort request and returns.
1722 * Return value:
1723 * None
1725 static void
1726 lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
1727 struct nvme_fc_remote_port *pnvme_rport,
1728 void *hw_queue_handle,
1729 struct nvmefc_fcp_req *pnvme_fcreq)
1731 struct lpfc_nvme_lport *lport;
1732 struct lpfc_vport *vport;
1733 struct lpfc_hba *phba;
1734 struct lpfc_io_buf *lpfc_nbuf;
1735 struct lpfc_iocbq *nvmereq_wqe;
1736 struct lpfc_nvme_fcpreq_priv *freqpriv;
1737 unsigned long flags;
1738 int ret_val;
1740 /* Validate pointers. LLDD fault handling with transport does
1741 * have timing races.
1743 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
1744 if (unlikely(!lport))
1745 return;
1747 vport = lport->vport;
1749 if (unlikely(!hw_queue_handle)) {
1750 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1751 "6129 Fail Abort, HW Queue Handle NULL.\n");
1752 return;
1755 phba = vport->phba;
1756 freqpriv = pnvme_fcreq->private;
1758 if (unlikely(!freqpriv))
1759 return;
1760 if (vport->load_flag & FC_UNLOADING)
1761 return;
1763 /* Announce entry to new IO submit field. */
1764 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1765 "6002 Abort Request to rport DID x%06x "
1766 "for nvme_fc_req x%px\n",
1767 pnvme_rport->port_id,
1768 pnvme_fcreq);
1770 /* If the hba is getting reset, this flag is set. It is
1771 * cleared when the reset is complete and rings reestablished.
1773 spin_lock_irqsave(&phba->hbalock, flags);
1774 /* driver queued commands are in process of being flushed */
1775 if (phba->hba_flag & HBA_IOQ_FLUSH) {
1776 spin_unlock_irqrestore(&phba->hbalock, flags);
1777 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1778 "6139 Driver in reset cleanup - flushing "
1779 "NVME Req now. hba_flag x%x\n",
1780 phba->hba_flag);
1781 return;
1784 lpfc_nbuf = freqpriv->nvme_buf;
1785 if (!lpfc_nbuf) {
1786 spin_unlock_irqrestore(&phba->hbalock, flags);
1787 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1788 "6140 NVME IO req has no matching lpfc nvme "
1789 "io buffer. Skipping abort req.\n");
1790 return;
1791 } else if (!lpfc_nbuf->nvmeCmd) {
1792 spin_unlock_irqrestore(&phba->hbalock, flags);
1793 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1794 "6141 lpfc NVME IO req has no nvme_fcreq "
1795 "io buffer. Skipping abort req.\n");
1796 return;
1798 nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
1800 /* Guard against IO completion being called at same time */
1801 spin_lock(&lpfc_nbuf->buf_lock);
1804 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
1805 * state must match the nvme_fcreq passed by the nvme
1806 * transport. If they don't match, it is likely the driver
1807 * has already completed the NVME IO and the nvme transport
1808 * has not seen it yet.
1810 if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1811 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1812 "6143 NVME req mismatch: "
1813 "lpfc_nbuf x%px nvmeCmd x%px, "
1814 "pnvme_fcreq x%px. Skipping Abort xri x%x\n",
1815 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
1816 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1817 goto out_unlock;
1820 /* Don't abort IOs no longer on the pending queue. */
1821 if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
1822 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1823 "6142 NVME IO req x%px not queued - skipping "
1824 "abort req xri x%x\n",
1825 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
1826 goto out_unlock;
1829 atomic_inc(&lport->xmt_fcp_abort);
1830 lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1831 nvmereq_wqe->sli4_xritag,
1832 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
1834 /* Outstanding abort is in progress */
1835 if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
1836 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1837 "6144 Outstanding NVME I/O Abort Request "
1838 "still pending on nvme_fcreq x%px, "
1839 "lpfc_ncmd %px xri x%x\n",
1840 pnvme_fcreq, lpfc_nbuf,
1841 nvmereq_wqe->sli4_xritag);
1842 goto out_unlock;
1845 ret_val = lpfc_sli4_issue_abort_iotag(phba, nvmereq_wqe,
1846 lpfc_nvme_abort_fcreq_cmpl);
1848 spin_unlock(&lpfc_nbuf->buf_lock);
1849 spin_unlock_irqrestore(&phba->hbalock, flags);
1850 if (ret_val != WQE_SUCCESS) {
1851 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
1852 "6137 Failed abts issue_wqe with status x%x "
1853 "for nvme_fcreq x%px.\n",
1854 ret_val, pnvme_fcreq);
1855 return;
1858 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1859 "6138 Transport Abort NVME Request Issued for "
1860 "ox_id x%x\n",
1861 nvmereq_wqe->sli4_xritag);
1862 return;
1864 out_unlock:
1865 spin_unlock(&lpfc_nbuf->buf_lock);
1866 spin_unlock_irqrestore(&phba->hbalock, flags);
1867 return;
1870 /* Declare and initialization an instance of the FC NVME template. */
1871 static struct nvme_fc_port_template lpfc_nvme_template = {
1872 /* initiator-based functions */
1873 .localport_delete = lpfc_nvme_localport_delete,
1874 .remoteport_delete = lpfc_nvme_remoteport_delete,
1875 .create_queue = lpfc_nvme_create_queue,
1876 .delete_queue = lpfc_nvme_delete_queue,
1877 .ls_req = lpfc_nvme_ls_req,
1878 .fcp_io = lpfc_nvme_fcp_io_submit,
1879 .ls_abort = lpfc_nvme_ls_abort,
1880 .fcp_abort = lpfc_nvme_fcp_abort,
1881 .xmt_ls_rsp = lpfc_nvme_xmt_ls_rsp,
1883 .max_hw_queues = 1,
1884 .max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1885 .max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1886 .dma_boundary = 0xFFFFFFFF,
1888 /* Sizes of additional private data for data structures.
1889 * No use for the last two sizes at this time.
1891 .local_priv_sz = sizeof(struct lpfc_nvme_lport),
1892 .remote_priv_sz = sizeof(struct lpfc_nvme_rport),
1893 .lsrqst_priv_sz = 0,
1894 .fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
1898 * lpfc_get_nvme_buf - Get a nvme buffer from io_buf_list of the HBA
1900 * This routine removes a nvme buffer from head of @hdwq io_buf_list
1901 * and returns to caller.
1903 * Return codes:
1904 * NULL - Error
1905 * Pointer to lpfc_nvme_buf - Success
1907 static struct lpfc_io_buf *
1908 lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
1909 int idx, int expedite)
1911 struct lpfc_io_buf *lpfc_ncmd;
1912 struct lpfc_sli4_hdw_queue *qp;
1913 struct sli4_sge *sgl;
1914 struct lpfc_iocbq *pwqeq;
1915 union lpfc_wqe128 *wqe;
1917 lpfc_ncmd = lpfc_get_io_buf(phba, NULL, idx, expedite);
1919 if (lpfc_ncmd) {
1920 pwqeq = &(lpfc_ncmd->cur_iocbq);
1921 wqe = &pwqeq->wqe;
1923 /* Setup key fields in buffer that may have been changed
1924 * if other protocols used this buffer.
1926 pwqeq->iocb_flag = LPFC_IO_NVME;
1927 pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
1928 lpfc_ncmd->start_time = jiffies;
1929 lpfc_ncmd->flags = 0;
1931 /* Rsp SGE will be filled in when we rcv an IO
1932 * from the NVME Layer to be sent.
1933 * The cmd is going to be embedded so we need a SKIP SGE.
1935 sgl = lpfc_ncmd->dma_sgl;
1936 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
1937 bf_set(lpfc_sli4_sge_last, sgl, 0);
1938 sgl->word2 = cpu_to_le32(sgl->word2);
1939 /* Fill in word 3 / sgl_len during cmd submission */
1941 /* Initialize 64 bytes only */
1942 memset(wqe, 0, sizeof(union lpfc_wqe));
1944 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1945 atomic_inc(&ndlp->cmd_pending);
1946 lpfc_ncmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
1949 } else {
1950 qp = &phba->sli4_hba.hdwq[idx];
1951 qp->empty_io_bufs++;
1954 return lpfc_ncmd;
1958 * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
1959 * @phba: The Hba for which this call is being executed.
1960 * @lpfc_ncmd: The nvme buffer which is being released.
1962 * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
1963 * lpfc_io_buf_list list. For SLI4 XRI's are tied to the nvme buffer
1964 * and cannot be reused for at least RA_TOV amount of time if it was
1965 * aborted.
1967 static void
1968 lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_ncmd)
1970 struct lpfc_sli4_hdw_queue *qp;
1971 unsigned long iflag = 0;
1973 if ((lpfc_ncmd->flags & LPFC_SBUF_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
1974 atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
1976 lpfc_ncmd->ndlp = NULL;
1977 lpfc_ncmd->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
1979 qp = lpfc_ncmd->hdwq;
1980 if (unlikely(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
1981 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1982 "6310 XB release deferred for "
1983 "ox_id x%x on reqtag x%x\n",
1984 lpfc_ncmd->cur_iocbq.sli4_xritag,
1985 lpfc_ncmd->cur_iocbq.iotag);
1987 spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
1988 list_add_tail(&lpfc_ncmd->list,
1989 &qp->lpfc_abts_io_buf_list);
1990 qp->abts_nvme_io_bufs++;
1991 spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
1992 } else
1993 lpfc_release_io_buf(phba, (struct lpfc_io_buf *)lpfc_ncmd, qp);
1997 * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
1998 * @vport - the lpfc_vport instance requesting a localport.
2000 * This routine is invoked to create an nvme localport instance to bind
2001 * to the nvme_fc_transport. It is called once during driver load
2002 * like lpfc_create_shost after all other services are initialized.
2003 * It requires a vport, vpi, and wwns at call time. Other localport
2004 * parameters are modified as the driver's FCID and the Fabric WWN
2005 * are established.
2007 * Return codes
2008 * 0 - successful
2009 * -ENOMEM - no heap memory available
2010 * other values - from nvme registration upcall
2013 lpfc_nvme_create_localport(struct lpfc_vport *vport)
2015 int ret = 0;
2016 struct lpfc_hba *phba = vport->phba;
2017 struct nvme_fc_port_info nfcp_info;
2018 struct nvme_fc_local_port *localport;
2019 struct lpfc_nvme_lport *lport;
2021 /* Initialize this localport instance. The vport wwn usage ensures
2022 * that NPIV is accounted for.
2024 memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
2025 nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
2026 nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
2027 nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
2029 /* We need to tell the transport layer + 1 because it takes page
2030 * alignment into account. When space for the SGL is allocated we
2031 * allocate + 3, one for cmd, one for rsp and one for this alignment
2033 lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
2035 /* Advertise how many hw queues we support based on cfg_hdw_queue,
2036 * which will not exceed cpu count.
2038 lpfc_nvme_template.max_hw_queues = phba->cfg_hdw_queue;
2040 if (!IS_ENABLED(CONFIG_NVME_FC))
2041 return ret;
2043 /* localport is allocated from the stack, but the registration
2044 * call allocates heap memory as well as the private area.
2047 ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
2048 &vport->phba->pcidev->dev, &localport);
2049 if (!ret) {
2050 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
2051 "6005 Successfully registered local "
2052 "NVME port num %d, localP x%px, private "
2053 "x%px, sg_seg %d\n",
2054 localport->port_num, localport,
2055 localport->private,
2056 lpfc_nvme_template.max_sgl_segments);
2058 /* Private is our lport size declared in the template. */
2059 lport = (struct lpfc_nvme_lport *)localport->private;
2060 vport->localport = localport;
2061 lport->vport = vport;
2062 vport->nvmei_support = 1;
2064 atomic_set(&lport->xmt_fcp_noxri, 0);
2065 atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
2066 atomic_set(&lport->xmt_fcp_qdepth, 0);
2067 atomic_set(&lport->xmt_fcp_err, 0);
2068 atomic_set(&lport->xmt_fcp_wqerr, 0);
2069 atomic_set(&lport->xmt_fcp_abort, 0);
2070 atomic_set(&lport->xmt_ls_abort, 0);
2071 atomic_set(&lport->xmt_ls_err, 0);
2072 atomic_set(&lport->cmpl_fcp_xb, 0);
2073 atomic_set(&lport->cmpl_fcp_err, 0);
2074 atomic_set(&lport->cmpl_ls_xb, 0);
2075 atomic_set(&lport->cmpl_ls_err, 0);
2077 atomic_set(&lport->fc4NvmeLsRequests, 0);
2078 atomic_set(&lport->fc4NvmeLsCmpls, 0);
2081 return ret;
2084 #if (IS_ENABLED(CONFIG_NVME_FC))
2085 /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2087 * The driver has to wait for the host nvme transport to callback
2088 * indicating the localport has successfully unregistered all
2089 * resources. Since this is an uninterruptible wait, loop every ten
2090 * seconds and print a message indicating no progress.
2092 * An uninterruptible wait is used because of the risk of transport-to-
2093 * driver state mismatch.
2095 static void
2096 lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
2097 struct lpfc_nvme_lport *lport,
2098 struct completion *lport_unreg_cmp)
2100 u32 wait_tmo;
2101 int ret, i, pending = 0;
2102 struct lpfc_sli_ring *pring;
2103 struct lpfc_hba *phba = vport->phba;
2104 struct lpfc_sli4_hdw_queue *qp;
2105 int abts_scsi, abts_nvme;
2107 /* Host transport has to clean up and confirm requiring an indefinite
2108 * wait. Print a message if a 10 second wait expires and renew the
2109 * wait. This is unexpected.
2111 wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2112 while (true) {
2113 ret = wait_for_completion_timeout(lport_unreg_cmp, wait_tmo);
2114 if (unlikely(!ret)) {
2115 pending = 0;
2116 abts_scsi = 0;
2117 abts_nvme = 0;
2118 for (i = 0; i < phba->cfg_hdw_queue; i++) {
2119 qp = &phba->sli4_hba.hdwq[i];
2120 pring = qp->io_wq->pring;
2121 if (!pring)
2122 continue;
2123 pending += pring->txcmplq_cnt;
2124 abts_scsi += qp->abts_scsi_io_bufs;
2125 abts_nvme += qp->abts_nvme_io_bufs;
2127 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2128 "6176 Lport x%px Localport x%px wait "
2129 "timed out. Pending %d [%d:%d]. "
2130 "Renewing.\n",
2131 lport, vport->localport, pending,
2132 abts_scsi, abts_nvme);
2133 continue;
2135 break;
2137 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
2138 "6177 Lport x%px Localport x%px Complete Success\n",
2139 lport, vport->localport);
2141 #endif
2144 * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
2145 * @vport: pointer to a host virtual N_Port data structure
2147 * This routine is invoked to destroy all lports bound to the phba.
2148 * The lport memory was allocated by the nvme fc transport and is
2149 * released there. This routine ensures all rports bound to the
2150 * lport have been disconnected.
2153 void
2154 lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
2156 #if (IS_ENABLED(CONFIG_NVME_FC))
2157 struct nvme_fc_local_port *localport;
2158 struct lpfc_nvme_lport *lport;
2159 int ret;
2160 DECLARE_COMPLETION_ONSTACK(lport_unreg_cmp);
2162 if (vport->nvmei_support == 0)
2163 return;
2165 localport = vport->localport;
2166 lport = (struct lpfc_nvme_lport *)localport->private;
2168 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2169 "6011 Destroying NVME localport x%px\n",
2170 localport);
2172 /* lport's rport list is clear. Unregister
2173 * lport and release resources.
2175 lport->lport_unreg_cmp = &lport_unreg_cmp;
2176 ret = nvme_fc_unregister_localport(localport);
2178 /* Wait for completion. This either blocks
2179 * indefinitely or succeeds
2181 lpfc_nvme_lport_unreg_wait(vport, lport, &lport_unreg_cmp);
2182 vport->localport = NULL;
2184 /* Regardless of the unregister upcall response, clear
2185 * nvmei_support. All rports are unregistered and the
2186 * driver will clean up.
2188 vport->nvmei_support = 0;
2189 if (ret == 0) {
2190 lpfc_printf_vlog(vport,
2191 KERN_INFO, LOG_NVME_DISC,
2192 "6009 Unregistered lport Success\n");
2193 } else {
2194 lpfc_printf_vlog(vport,
2195 KERN_INFO, LOG_NVME_DISC,
2196 "6010 Unregistered lport "
2197 "Failed, status x%x\n",
2198 ret);
2200 #endif
2203 void
2204 lpfc_nvme_update_localport(struct lpfc_vport *vport)
2206 #if (IS_ENABLED(CONFIG_NVME_FC))
2207 struct nvme_fc_local_port *localport;
2208 struct lpfc_nvme_lport *lport;
2210 localport = vport->localport;
2211 if (!localport) {
2212 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
2213 "6710 Update NVME fail. No localport\n");
2214 return;
2216 lport = (struct lpfc_nvme_lport *)localport->private;
2217 if (!lport) {
2218 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
2219 "6171 Update NVME fail. localP x%px, No lport\n",
2220 localport);
2221 return;
2223 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2224 "6012 Update NVME lport x%px did x%x\n",
2225 localport, vport->fc_myDID);
2227 localport->port_id = vport->fc_myDID;
2228 if (localport->port_id == 0)
2229 localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
2230 else
2231 localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
2233 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2234 "6030 bound lport x%px to DID x%06x\n",
2235 lport, localport->port_id);
2236 #endif
2240 lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2242 #if (IS_ENABLED(CONFIG_NVME_FC))
2243 int ret = 0;
2244 struct nvme_fc_local_port *localport;
2245 struct lpfc_nvme_lport *lport;
2246 struct lpfc_nvme_rport *rport;
2247 struct lpfc_nvme_rport *oldrport;
2248 struct nvme_fc_remote_port *remote_port;
2249 struct nvme_fc_port_info rpinfo;
2250 struct lpfc_nodelist *prev_ndlp = NULL;
2251 struct fc_rport *srport = ndlp->rport;
2253 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
2254 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
2255 ndlp->nlp_DID, ndlp->nlp_type);
2257 localport = vport->localport;
2258 if (!localport)
2259 return 0;
2261 lport = (struct lpfc_nvme_lport *)localport->private;
2263 /* NVME rports are not preserved across devloss.
2264 * Just register this instance. Note, rpinfo->dev_loss_tmo
2265 * is left 0 to indicate accept transport defaults. The
2266 * driver communicates port role capabilities consistent
2267 * with the PRLI response data.
2269 memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
2270 rpinfo.port_id = ndlp->nlp_DID;
2271 if (ndlp->nlp_type & NLP_NVME_TARGET)
2272 rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
2273 if (ndlp->nlp_type & NLP_NVME_INITIATOR)
2274 rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
2276 if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
2277 rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
2279 rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
2280 rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
2281 if (srport)
2282 rpinfo.dev_loss_tmo = srport->dev_loss_tmo;
2283 else
2284 rpinfo.dev_loss_tmo = vport->cfg_devloss_tmo;
2286 spin_lock_irq(&ndlp->lock);
2287 oldrport = lpfc_ndlp_get_nrport(ndlp);
2288 if (oldrport) {
2289 prev_ndlp = oldrport->ndlp;
2290 spin_unlock_irq(&ndlp->lock);
2291 } else {
2292 spin_unlock_irq(&ndlp->lock);
2293 if (!lpfc_nlp_get(ndlp)) {
2294 dev_warn(&vport->phba->pcidev->dev,
2295 "Warning - No node ref - exit register\n");
2296 return 0;
2300 ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
2301 if (!ret) {
2302 /* If the ndlp already has an nrport, this is just
2303 * a resume of the existing rport. Else this is a
2304 * new rport.
2306 /* Guard against an unregister/reregister
2307 * race that leaves the WAIT flag set.
2309 spin_lock_irq(&ndlp->lock);
2310 ndlp->fc4_xpt_flags &= ~NLP_WAIT_FOR_UNREG;
2311 ndlp->fc4_xpt_flags |= NVME_XPT_REGD;
2312 spin_unlock_irq(&ndlp->lock);
2313 rport = remote_port->private;
2314 if (oldrport) {
2316 /* Sever the ndlp<->rport association
2317 * before dropping the ndlp ref from
2318 * register.
2320 spin_lock_irq(&ndlp->lock);
2321 ndlp->nrport = NULL;
2322 ndlp->fc4_xpt_flags &= ~NLP_WAIT_FOR_UNREG;
2323 spin_unlock_irq(&ndlp->lock);
2324 rport->ndlp = NULL;
2325 rport->remoteport = NULL;
2327 /* Reference only removed if previous NDLP is no longer
2328 * active. It might be just a swap and removing the
2329 * reference would cause a premature cleanup.
2331 if (prev_ndlp && prev_ndlp != ndlp) {
2332 if (!prev_ndlp->nrport)
2333 lpfc_nlp_put(prev_ndlp);
2337 /* Clean bind the rport to the ndlp. */
2338 rport->remoteport = remote_port;
2339 rport->lport = lport;
2340 rport->ndlp = ndlp;
2341 spin_lock_irq(&ndlp->lock);
2342 ndlp->nrport = rport;
2343 spin_unlock_irq(&ndlp->lock);
2344 lpfc_printf_vlog(vport, KERN_INFO,
2345 LOG_NVME_DISC | LOG_NODE,
2346 "6022 Bind lport x%px to remoteport x%px "
2347 "rport x%px WWNN 0x%llx, "
2348 "Rport WWPN 0x%llx DID "
2349 "x%06x Role x%x, ndlp %p prev_ndlp x%px\n",
2350 lport, remote_port, rport,
2351 rpinfo.node_name, rpinfo.port_name,
2352 rpinfo.port_id, rpinfo.port_role,
2353 ndlp, prev_ndlp);
2354 } else {
2355 lpfc_printf_vlog(vport, KERN_ERR,
2356 LOG_TRACE_EVENT,
2357 "6031 RemotePort Registration failed "
2358 "err: %d, DID x%06x\n",
2359 ret, ndlp->nlp_DID);
2362 return ret;
2363 #else
2364 return 0;
2365 #endif
2369 * lpfc_nvme_rescan_port - Check to see if we should rescan this remoteport
2371 * If the ndlp represents an NVME Target, that we are logged into,
2372 * ping the NVME FC Transport layer to initiate a device rescan
2373 * on this remote NPort.
2375 void
2376 lpfc_nvme_rescan_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2378 #if (IS_ENABLED(CONFIG_NVME_FC))
2379 struct lpfc_nvme_rport *nrport;
2380 struct nvme_fc_remote_port *remoteport = NULL;
2382 spin_lock_irq(&ndlp->lock);
2383 nrport = lpfc_ndlp_get_nrport(ndlp);
2384 if (nrport)
2385 remoteport = nrport->remoteport;
2386 spin_unlock_irq(&ndlp->lock);
2388 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2389 "6170 Rescan NPort DID x%06x type x%x "
2390 "state x%x nrport x%px remoteport x%px\n",
2391 ndlp->nlp_DID, ndlp->nlp_type, ndlp->nlp_state,
2392 nrport, remoteport);
2394 if (!nrport || !remoteport)
2395 goto rescan_exit;
2397 /* Only rescan if we are an NVME target in the MAPPED state */
2398 if (remoteport->port_role & FC_PORT_ROLE_NVME_DISCOVERY &&
2399 ndlp->nlp_state == NLP_STE_MAPPED_NODE) {
2400 nvme_fc_rescan_remoteport(remoteport);
2402 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2403 "6172 NVME rescanned DID x%06x "
2404 "port_state x%x\n",
2405 ndlp->nlp_DID, remoteport->port_state);
2407 return;
2408 rescan_exit:
2409 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2410 "6169 Skip NVME Rport Rescan, NVME remoteport "
2411 "unregistered\n");
2412 #endif
2415 /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
2417 * There is no notion of Devloss or rport recovery from the current
2418 * nvme_transport perspective. Loss of an rport just means IO cannot
2419 * be sent and recovery is completely up to the initator.
2420 * For now, the driver just unbinds the DID and port_role so that
2421 * no further IO can be issued. Changes are planned for later.
2423 * Notes - the ndlp reference count is not decremented here since
2424 * since there is no nvme_transport api for devloss. Node ref count
2425 * is only adjusted in driver unload.
2427 void
2428 lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2430 #if (IS_ENABLED(CONFIG_NVME_FC))
2431 int ret;
2432 struct nvme_fc_local_port *localport;
2433 struct lpfc_nvme_lport *lport;
2434 struct lpfc_nvme_rport *rport;
2435 struct nvme_fc_remote_port *remoteport = NULL;
2437 localport = vport->localport;
2439 /* This is fundamental error. The localport is always
2440 * available until driver unload. Just exit.
2442 if (!localport)
2443 return;
2445 lport = (struct lpfc_nvme_lport *)localport->private;
2446 if (!lport)
2447 goto input_err;
2449 spin_lock_irq(&ndlp->lock);
2450 rport = lpfc_ndlp_get_nrport(ndlp);
2451 if (rport)
2452 remoteport = rport->remoteport;
2453 spin_unlock_irq(&ndlp->lock);
2454 if (!remoteport)
2455 goto input_err;
2457 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2458 "6033 Unreg nvme remoteport x%px, portname x%llx, "
2459 "port_id x%06x, portstate x%x port type x%x "
2460 "refcnt %d\n",
2461 remoteport, remoteport->port_name,
2462 remoteport->port_id, remoteport->port_state,
2463 ndlp->nlp_type, kref_read(&ndlp->kref));
2465 /* Sanity check ndlp type. Only call for NVME ports. Don't
2466 * clear any rport state until the transport calls back.
2469 if (ndlp->nlp_type & NLP_NVME_TARGET) {
2470 /* No concern about the role change on the nvme remoteport.
2471 * The transport will update it.
2473 spin_lock_irq(&vport->phba->hbalock);
2474 ndlp->fc4_xpt_flags |= NLP_WAIT_FOR_UNREG;
2475 spin_unlock_irq(&vport->phba->hbalock);
2477 /* Don't let the host nvme transport keep sending keep-alives
2478 * on this remoteport. Vport is unloading, no recovery. The
2479 * return values is ignored. The upcall is a courtesy to the
2480 * transport.
2482 if (vport->load_flag & FC_UNLOADING)
2483 (void)nvme_fc_set_remoteport_devloss(remoteport, 0);
2485 ret = nvme_fc_unregister_remoteport(remoteport);
2487 /* The driver no longer knows if the nrport memory is valid.
2488 * because the controller teardown process has begun and
2489 * is asynchronous. Break the binding in the ndlp. Also
2490 * remove the register ndlp reference to setup node release.
2492 ndlp->nrport = NULL;
2493 lpfc_nlp_put(ndlp);
2494 if (ret != 0) {
2495 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2496 "6167 NVME unregister failed %d "
2497 "port_state x%x\n",
2498 ret, remoteport->port_state);
2501 return;
2503 input_err:
2504 #endif
2505 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
2506 "6168 State error: lport x%px, rport x%px FCID x%06x\n",
2507 vport->localport, ndlp->rport, ndlp->nlp_DID);
2511 * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2512 * @phba: pointer to lpfc hba data structure.
2513 * @axri: pointer to the fcp xri abort wcqe structure.
2514 * @lpfc_ncmd: The nvme job structure for the request being aborted.
2516 * This routine is invoked by the worker thread to process a SLI4 fast-path
2517 * NVME aborted xri. Aborted NVME IO commands are completed to the transport
2518 * here.
2520 void
2521 lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2522 struct sli4_wcqe_xri_aborted *axri,
2523 struct lpfc_io_buf *lpfc_ncmd)
2525 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
2526 struct nvmefc_fcp_req *nvme_cmd = NULL;
2527 struct lpfc_nodelist *ndlp = lpfc_ncmd->ndlp;
2530 if (ndlp)
2531 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
2533 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2534 "6311 nvme_cmd %p xri x%x tag x%x abort complete and "
2535 "xri released\n",
2536 lpfc_ncmd->nvmeCmd, xri,
2537 lpfc_ncmd->cur_iocbq.iotag);
2539 /* Aborted NVME commands are required to not complete
2540 * before the abort exchange command fully completes.
2541 * Once completed, it is available via the put list.
2543 if (lpfc_ncmd->nvmeCmd) {
2544 nvme_cmd = lpfc_ncmd->nvmeCmd;
2545 nvme_cmd->done(nvme_cmd);
2546 lpfc_ncmd->nvmeCmd = NULL;
2548 lpfc_release_nvme_buf(phba, lpfc_ncmd);
2552 * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2553 * @phba: Pointer to HBA context object.
2555 * This function flushes all wqes in the nvme rings and frees all resources
2556 * in the txcmplq. This function does not issue abort wqes for the IO
2557 * commands in txcmplq, they will just be returned with
2558 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2559 * slot has been permanently disabled.
2561 void
2562 lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2564 struct lpfc_sli_ring *pring;
2565 u32 i, wait_cnt = 0;
2567 if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.hdwq)
2568 return;
2570 /* Cycle through all IO rings and make sure all outstanding
2571 * WQEs have been removed from the txcmplqs.
2573 for (i = 0; i < phba->cfg_hdw_queue; i++) {
2574 if (!phba->sli4_hba.hdwq[i].io_wq)
2575 continue;
2576 pring = phba->sli4_hba.hdwq[i].io_wq->pring;
2578 if (!pring)
2579 continue;
2581 /* Retrieve everything on the txcmplq */
2582 while (!list_empty(&pring->txcmplq)) {
2583 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2584 wait_cnt++;
2586 /* The sleep is 10mS. Every ten seconds,
2587 * dump a message. Something is wrong.
2589 if ((wait_cnt % 1000) == 0) {
2590 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2591 "6178 NVME IO not empty, "
2592 "cnt %d\n", wait_cnt);
2598 void
2599 lpfc_nvme_cancel_iocb(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn)
2601 #if (IS_ENABLED(CONFIG_NVME_FC))
2602 struct lpfc_io_buf *lpfc_ncmd;
2603 struct nvmefc_fcp_req *nCmd;
2604 struct lpfc_nvme_fcpreq_priv *freqpriv;
2606 if (!pwqeIn->context1) {
2607 lpfc_sli_release_iocbq(phba, pwqeIn);
2608 return;
2610 /* For abort iocb just return, IO iocb will do a done call */
2611 if (bf_get(wqe_cmnd, &pwqeIn->wqe.gen_req.wqe_com) ==
2612 CMD_ABORT_XRI_CX) {
2613 lpfc_sli_release_iocbq(phba, pwqeIn);
2614 return;
2616 lpfc_ncmd = (struct lpfc_io_buf *)pwqeIn->context1;
2618 spin_lock(&lpfc_ncmd->buf_lock);
2619 if (!lpfc_ncmd->nvmeCmd) {
2620 spin_unlock(&lpfc_ncmd->buf_lock);
2621 lpfc_release_nvme_buf(phba, lpfc_ncmd);
2622 return;
2625 nCmd = lpfc_ncmd->nvmeCmd;
2626 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2627 "6194 NVME Cancel xri %x\n",
2628 lpfc_ncmd->cur_iocbq.sli4_xritag);
2630 nCmd->transferred_length = 0;
2631 nCmd->rcv_rsplen = 0;
2632 nCmd->status = NVME_SC_INTERNAL;
2633 freqpriv = nCmd->private;
2634 freqpriv->nvme_buf = NULL;
2635 lpfc_ncmd->nvmeCmd = NULL;
2637 spin_unlock(&lpfc_ncmd->buf_lock);
2638 nCmd->done(nCmd);
2640 /* Call release with XB=1 to queue the IO into the abort list. */
2641 lpfc_release_nvme_buf(phba, lpfc_ncmd);
2642 #endif