scsi: lpfc: cleanup: remove nrport from nvme command structure
[linux-2.6-block.git] / drivers / scsi / lpfc / lpfc_nvme.c
CommitLineData
01649561
JS
1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
128bddac 4 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
3e21d1cb 5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
01649561
JS
6 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
7 * EMULEX and SLI are trademarks of Emulex. *
d080abe0 8 * www.broadcom.com *
01649561
JS
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>
30
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>
38
39#include <linux/nvme.h>
40#include <linux/nvme-fc-driver.h>
41#include <linux/nvme-fc.h>
42#include "lpfc_version.h"
43#include "lpfc_hw4.h"
44#include "lpfc_hw.h"
45#include "lpfc_sli.h"
46#include "lpfc_sli4.h"
47#include "lpfc_nl.h"
48#include "lpfc_disc.h"
49#include "lpfc.h"
50#include "lpfc_nvme.h"
51#include "lpfc_scsi.h"
52#include "lpfc_logmsg.h"
53#include "lpfc_crtn.h"
54#include "lpfc_vport.h"
bd2cdd5e 55#include "lpfc_debugfs.h"
01649561
JS
56
57/* NVME initiator-based functions */
58
59static struct lpfc_nvme_buf *
cf1a1d3e
JS
60lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
61 int expedite);
01649561
JS
62
63static void
64lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_nvme_buf *);
65
81e6a637 66static struct nvme_fc_port_template lpfc_nvme_template;
01649561 67
fab2e466
CIK
68static union lpfc_wqe128 lpfc_iread_cmd_template;
69static union lpfc_wqe128 lpfc_iwrite_cmd_template;
70static union lpfc_wqe128 lpfc_icmnd_cmd_template;
5fd11085
JS
71
72/* Setup WQE templates for NVME IOs */
73void
fab2e466 74lpfc_nvme_cmd_template(void)
5fd11085
JS
75{
76 union lpfc_wqe128 *wqe;
77
78 /* IREAD template */
79 wqe = &lpfc_iread_cmd_template;
80 memset(wqe, 0, sizeof(union lpfc_wqe128));
81
82 /* Word 0, 1, 2 - BDE is variable */
83
84 /* Word 3 - cmd_buff_len, payload_offset_len is zero */
85
86 /* Word 4 - total_xfer_len is variable */
87
88 /* Word 5 - is zero */
89
90 /* Word 6 - ctxt_tag, xri_tag is variable */
91
92 /* Word 7 */
93 bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
94 bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
95 bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
96 bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
97
98 /* Word 8 - abort_tag is variable */
99
100 /* Word 9 - reqtag is variable */
101
102 /* Word 10 - dbde, wqes is variable */
103 bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
104 bf_set(wqe_nvme, &wqe->fcp_iread.wqe_com, 1);
105 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
106 bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
107 bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
108 bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
109
110 /* Word 11 - pbde is variable */
111 bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, NVME_READ_CMD);
112 bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
113 bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
114
115 /* Word 12 - is zero */
116
117 /* Word 13, 14, 15 - PBDE is variable */
118
119 /* IWRITE template */
120 wqe = &lpfc_iwrite_cmd_template;
121 memset(wqe, 0, sizeof(union lpfc_wqe128));
122
123 /* Word 0, 1, 2 - BDE is variable */
124
125 /* Word 3 - cmd_buff_len, payload_offset_len is zero */
126
127 /* Word 4 - total_xfer_len is variable */
128
129 /* Word 5 - initial_xfer_len is variable */
130
131 /* Word 6 - ctxt_tag, xri_tag is variable */
132
133 /* Word 7 */
134 bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);
135 bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);
136 bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);
137 bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);
138
139 /* Word 8 - abort_tag is variable */
140
141 /* Word 9 - reqtag is variable */
142
143 /* Word 10 - dbde, wqes is variable */
144 bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
145 bf_set(wqe_nvme, &wqe->fcp_iwrite.wqe_com, 1);
146 bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
147 bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);
148 bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
149 bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
150
151 /* Word 11 - pbde is variable */
152 bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, NVME_WRITE_CMD);
153 bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
154 bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
155
156 /* Word 12 - is zero */
157
158 /* Word 13, 14, 15 - PBDE is variable */
159
160 /* ICMND template */
161 wqe = &lpfc_icmnd_cmd_template;
162 memset(wqe, 0, sizeof(union lpfc_wqe128));
163
164 /* Word 0, 1, 2 - BDE is variable */
165
166 /* Word 3 - payload_offset_len is variable */
167
168 /* Word 4, 5 - is zero */
169
170 /* Word 6 - ctxt_tag, xri_tag is variable */
171
172 /* Word 7 */
173 bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
174 bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
175 bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
176 bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
177
178 /* Word 8 - abort_tag is variable */
179
180 /* Word 9 - reqtag is variable */
181
182 /* Word 10 - dbde, wqes is variable */
183 bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
184 bf_set(wqe_nvme, &wqe->fcp_icmd.wqe_com, 1);
185 bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
186 bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
187 bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
188 bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
189
190 /* Word 11 */
191 bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, FCP_COMMAND);
192 bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
193 bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);
194
195 /* Word 12, 13, 14, 15 - is zero */
196}
197
01649561
JS
198/**
199 * lpfc_nvme_create_queue -
200 * @lpfc_pnvme: Pointer to the driver's nvme instance data
201 * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
202 * @handle: An opaque driver handle used in follow-up calls.
203 *
204 * Driver registers this routine to preallocate and initialize any
205 * internal data structures to bind the @qidx to its internal IO queues.
206 * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
207 *
208 * Return value :
209 * 0 - Success
210 * -EINVAL - Unsupported input value.
211 * -ENOMEM - Could not alloc necessary memory
212 **/
213static int
214lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
215 unsigned int qidx, u16 qsize,
216 void **handle)
217{
218 struct lpfc_nvme_lport *lport;
219 struct lpfc_vport *vport;
220 struct lpfc_nvme_qhandle *qhandle;
221 char *str;
222
c3725bdc
JS
223 if (!pnvme_lport->private)
224 return -ENOMEM;
225
01649561
JS
226 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
227 vport = lport->vport;
228 qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
229 if (qhandle == NULL)
230 return -ENOMEM;
231
232 qhandle->cpu_id = smp_processor_id();
233 qhandle->qidx = qidx;
234 /*
235 * NVME qidx == 0 is the admin queue, so both admin queue
236 * and first IO queue will use MSI-X vector and associated
237 * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
238 */
239 if (qidx) {
240 str = "IO "; /* IO queue */
241 qhandle->index = ((qidx - 1) %
242 vport->phba->cfg_nvme_io_channel);
243 } else {
244 str = "ADM"; /* Admin queue */
245 qhandle->index = qidx;
246 }
247
d58734f0 248 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
01649561
JS
249 "6073 Binding %s HdwQueue %d (cpu %d) to "
250 "io_channel %d qhandle %p\n", str,
251 qidx, qhandle->cpu_id, qhandle->index, qhandle);
252 *handle = (void *)qhandle;
253 return 0;
254}
255
256/**
257 * lpfc_nvme_delete_queue -
258 * @lpfc_pnvme: Pointer to the driver's nvme instance data
259 * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
260 * @handle: An opaque driver handle from lpfc_nvme_create_queue
261 *
262 * Driver registers this routine to free
263 * any internal data structures to bind the @qidx to its internal
264 * IO queues.
265 *
266 * Return value :
267 * 0 - Success
268 * TODO: What are the failure codes.
269 **/
270static void
271lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
272 unsigned int qidx,
273 void *handle)
274{
275 struct lpfc_nvme_lport *lport;
276 struct lpfc_vport *vport;
277
c3725bdc
JS
278 if (!pnvme_lport->private)
279 return;
280
01649561
JS
281 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
282 vport = lport->vport;
283
284 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2879265f 285 "6001 ENTER. lpfc_pnvme %p, qidx x%x qhandle %p\n",
01649561
JS
286 lport, qidx, handle);
287 kfree(handle);
288}
289
290static void
291lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
292{
293 struct lpfc_nvme_lport *lport = localport->private;
294
add9d6be
JS
295 lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
296 "6173 localport %p delete complete\n",
297 lport);
298
01649561
JS
299 /* release any threads waiting for the unreg to complete */
300 complete(&lport->lport_unreg_done);
301}
302
303/* lpfc_nvme_remoteport_delete
304 *
305 * @remoteport: Pointer to an nvme transport remoteport instance.
306 *
307 * This is a template downcall. NVME transport calls this function
308 * when it has completed the unregistration of a previously
309 * registered remoteport.
310 *
311 * Return value :
312 * None
313 */
314void
315lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
316{
317 struct lpfc_nvme_rport *rport = remoteport->private;
318 struct lpfc_vport *vport;
319 struct lpfc_nodelist *ndlp;
320
321 ndlp = rport->ndlp;
322 if (!ndlp)
323 goto rport_err;
324
325 vport = ndlp->vport;
326 if (!vport)
327 goto rport_err;
328
329 /* Remove this rport from the lport's list - memory is owned by the
330 * transport. Remove the ndlp reference for the NVME transport before
7a06dcd3 331 * calling state machine to remove the node.
01649561
JS
332 */
333 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
3fd78355 334 "6146 remoteport delete of remoteport %p\n",
01649561 335 remoteport);
3fd78355 336 spin_lock_irq(&vport->phba->hbalock);
b15bd3e6
JS
337
338 /* The register rebind might have occurred before the delete
339 * downcall. Guard against this race.
340 */
341 if (ndlp->upcall_flags & NLP_WAIT_FOR_UNREG) {
342 ndlp->nrport = NULL;
343 ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
344 }
3fd78355
JS
345 spin_unlock_irq(&vport->phba->hbalock);
346
347 /* Remove original register reference. The host transport
348 * won't reference this rport/remoteport any further.
349 */
01649561
JS
350 lpfc_nlp_put(ndlp);
351
352 rport_err:
3fd78355 353 return;
01649561
JS
354}
355
356static void
357lpfc_nvme_cmpl_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
358 struct lpfc_wcqe_complete *wcqe)
359{
360 struct lpfc_vport *vport = cmdwqe->vport;
4b056682 361 struct lpfc_nvme_lport *lport;
01649561
JS
362 uint32_t status;
363 struct nvmefc_ls_req *pnvme_lsreq;
364 struct lpfc_dmabuf *buf_ptr;
365 struct lpfc_nodelist *ndlp;
366
01649561
JS
367 pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
368 status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
66a210ff 369
66a85155
JS
370 if (vport->localport) {
371 lport = (struct lpfc_nvme_lport *)vport->localport->private;
372 if (lport) {
373 atomic_inc(&lport->fc4NvmeLsCmpls);
374 if (status) {
375 if (bf_get(lpfc_wcqe_c_xb, wcqe))
376 atomic_inc(&lport->cmpl_ls_xb);
377 atomic_inc(&lport->cmpl_ls_err);
378 }
66a210ff 379 }
4b056682
JS
380 }
381
01649561
JS
382 ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
383 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
384 "6047 nvme cmpl Enter "
815a9c43
JS
385 "Data %p DID %x Xri: %x status %x reason x%x cmd:%p "
386 "lsreg:%p bmp:%p ndlp:%p\n",
01649561
JS
387 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
388 cmdwqe->sli4_xritag, status,
815a9c43 389 (wcqe->parameter & 0xffff),
01649561
JS
390 cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
391
bd2cdd5e
JS
392 lpfc_nvmeio_data(phba, "NVME LS CMPL: xri x%x stat x%x parm x%x\n",
393 cmdwqe->sli4_xritag, status, wcqe->parameter);
394
01649561
JS
395 if (cmdwqe->context3) {
396 buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
397 lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
398 kfree(buf_ptr);
399 cmdwqe->context3 = NULL;
400 }
401 if (pnvme_lsreq->done)
402 pnvme_lsreq->done(pnvme_lsreq, status);
403 else
404 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
405 "6046 nvme cmpl without done call back? "
406 "Data %p DID %x Xri: %x status %x\n",
407 pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
408 cmdwqe->sli4_xritag, status);
409 if (ndlp) {
410 lpfc_nlp_put(ndlp);
411 cmdwqe->context1 = NULL;
412 }
413 lpfc_sli_release_iocbq(phba, cmdwqe);
414}
415
416static int
417lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
418 struct lpfc_dmabuf *inp,
205e8240
JS
419 struct nvmefc_ls_req *pnvme_lsreq,
420 void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
421 struct lpfc_wcqe_complete *),
422 struct lpfc_nodelist *ndlp, uint32_t num_entry,
423 uint32_t tmo, uint8_t retry)
01649561 424{
205e8240
JS
425 struct lpfc_hba *phba = vport->phba;
426 union lpfc_wqe128 *wqe;
01649561
JS
427 struct lpfc_iocbq *genwqe;
428 struct ulp_bde64 *bpl;
429 struct ulp_bde64 bde;
430 int i, rc, xmit_len, first_len;
431
432 /* Allocate buffer for command WQE */
433 genwqe = lpfc_sli_get_iocbq(phba);
434 if (genwqe == NULL)
435 return 1;
436
437 wqe = &genwqe->wqe;
438 memset(wqe, 0, sizeof(union lpfc_wqe));
439
440 genwqe->context3 = (uint8_t *)bmp;
441 genwqe->iocb_flag |= LPFC_IO_NVME_LS;
442
443 /* Save for completion so we can release these resources */
444 genwqe->context1 = lpfc_nlp_get(ndlp);
445 genwqe->context2 = (uint8_t *)pnvme_lsreq;
446 /* Fill in payload, bp points to frame payload */
447
448 if (!tmo)
449 /* FC spec states we need 3 * ratov for CT requests */
450 tmo = (3 * phba->fc_ratov);
451
452 /* For this command calculate the xmit length of the request bde. */
453 xmit_len = 0;
454 first_len = 0;
455 bpl = (struct ulp_bde64 *)bmp->virt;
456 for (i = 0; i < num_entry; i++) {
457 bde.tus.w = bpl[i].tus.w;
458 if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
459 break;
460 xmit_len += bde.tus.f.bdeSize;
461 if (i == 0)
462 first_len = xmit_len;
463 }
464
465 genwqe->rsvd2 = num_entry;
466 genwqe->hba_wqidx = 0;
467
468 /* Words 0 - 2 */
469 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
470 wqe->generic.bde.tus.f.bdeSize = first_len;
471 wqe->generic.bde.addrLow = bpl[0].addrLow;
472 wqe->generic.bde.addrHigh = bpl[0].addrHigh;
473
474 /* Word 3 */
475 wqe->gen_req.request_payload_len = first_len;
476
477 /* Word 4 */
478
479 /* Word 5 */
480 bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
481 bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
482 bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
8b361639 483 bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
01649561
JS
484 bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
485
486 /* Word 6 */
487 bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
488 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
489 bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
490
491 /* Word 7 */
492 bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
493 bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
494 bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
495 bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
496
497 /* Word 8 */
498 wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
499
500 /* Word 9 */
501 bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
502
503 /* Word 10 */
504 bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
505 bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
506 bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
507 bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
508 bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
509
510 /* Word 11 */
511 bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
512 bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
513
514
515 /* Issue GEN REQ WQE for NPORT <did> */
516 lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
517 "6050 Issue GEN REQ WQE to NPORT x%x "
518 "Data: x%x x%x wq:%p lsreq:%p bmp:%p xmit:%d 1st:%d\n",
519 ndlp->nlp_DID, genwqe->iotag,
520 vport->port_state,
521 genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
522 genwqe->wqe_cmpl = cmpl;
523 genwqe->iocb_cmpl = NULL;
524 genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
525 genwqe->vport = vport;
526 genwqe->retry = retry;
527
bd2cdd5e
JS
528 lpfc_nvmeio_data(phba, "NVME LS XMIT: xri x%x iotag x%x to x%06x\n",
529 genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
530
01649561 531 rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, genwqe);
cd22d605 532 if (rc) {
01649561
JS
533 lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
534 "6045 Issue GEN REQ WQE to NPORT x%x "
535 "Data: x%x x%x\n",
536 ndlp->nlp_DID, genwqe->iotag,
537 vport->port_state);
538 lpfc_sli_release_iocbq(phba, genwqe);
539 return 1;
540 }
541 return 0;
542}
543
544/**
545 * lpfc_nvme_ls_req - Issue an Link Service request
546 * @lpfc_pnvme: Pointer to the driver's nvme instance data
547 * @lpfc_nvme_lport: Pointer to the driver's local port data
548 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
549 *
550 * Driver registers this routine to handle any link service request
551 * from the nvme_fc transport to a remote nvme-aware port.
552 *
553 * Return value :
554 * 0 - Success
555 * TODO: What are the failure codes.
556 **/
557static int
558lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
559 struct nvme_fc_remote_port *pnvme_rport,
560 struct nvmefc_ls_req *pnvme_lsreq)
561{
562 int ret = 0;
563 struct lpfc_nvme_lport *lport;
815a9c43 564 struct lpfc_nvme_rport *rport;
01649561
JS
565 struct lpfc_vport *vport;
566 struct lpfc_nodelist *ndlp;
567 struct ulp_bde64 *bpl;
568 struct lpfc_dmabuf *bmp;
ba43c4d0 569 uint16_t ntype, nstate;
01649561
JS
570
571 /* there are two dma buf in the request, actually there is one and
572 * the second one is just the start address + cmd size.
573 * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
574 * in a lpfc_dmabuf struct. When freeing we just free the wrapper
575 * because the nvem layer owns the data bufs.
576 * We do not have to break these packets open, we don't care what is in
577 * them. And we do not have to look at the resonse data, we only care
578 * that we got a response. All of the caring is going to happen in the
579 * nvme-fc layer.
580 */
581
582 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
815a9c43 583 rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
66a210ff
JS
584 if (unlikely(!lport) || unlikely(!rport))
585 return -EINVAL;
586
01649561
JS
587 vport = lport->vport;
588
3386f4bd
JS
589 if (vport->load_flag & FC_UNLOADING)
590 return -ENODEV;
591
815a9c43
JS
592 /* Need the ndlp. It is stored in the driver's rport. */
593 ndlp = rport->ndlp;
ba43c4d0
JS
594 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
595 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
815a9c43
JS
596 "6051 Remoteport %p, rport has invalid ndlp. "
597 "Failing LS Req\n", pnvme_rport);
ba43c4d0
JS
598 return -ENODEV;
599 }
600
601 /* The remote node has to be a mapped nvme target or an
602 * unmapped nvme initiator or it's an error.
603 */
604 ntype = ndlp->nlp_type;
605 nstate = ndlp->nlp_state;
606 if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
607 (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
608 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
609 "6088 DID x%06x not ready for "
610 "IO. State x%x, Type x%x\n",
611 pnvme_rport->port_id,
612 ndlp->nlp_state, ndlp->nlp_type);
613 return -ENODEV;
01649561
JS
614 }
615 bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
616 if (!bmp) {
617
618 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
619 "6044 Could not find node for DID %x\n",
620 pnvme_rport->port_id);
621 return 2;
622 }
623 INIT_LIST_HEAD(&bmp->list);
624 bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
625 if (!bmp->virt) {
626 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
627 "6042 Could not find node for DID %x\n",
628 pnvme_rport->port_id);
629 kfree(bmp);
630 return 3;
631 }
632 bpl = (struct ulp_bde64 *)bmp->virt;
633 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
634 bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
635 bpl->tus.f.bdeFlags = 0;
636 bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
637 bpl->tus.w = le32_to_cpu(bpl->tus.w);
638 bpl++;
639
640 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
641 bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
642 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
643 bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
644 bpl->tus.w = le32_to_cpu(bpl->tus.w);
645
646 /* Expand print to include key fields. */
647 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
815a9c43
JS
648 "6149 Issue LS Req to DID 0x%06x lport %p, rport %p "
649 "lsreq%p rqstlen:%d rsplen:%d %pad %pad\n",
650 ndlp->nlp_DID,
01649561
JS
651 pnvme_lport, pnvme_rport,
652 pnvme_lsreq, pnvme_lsreq->rqstlen,
825c6abb
AB
653 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
654 &pnvme_lsreq->rspdma);
01649561 655
66a210ff 656 atomic_inc(&lport->fc4NvmeLsRequests);
01649561
JS
657
658 /* Hardcode the wait to 30 seconds. Connections are failing otherwise.
659 * This code allows it all to work.
660 */
661 ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
662 pnvme_lsreq, lpfc_nvme_cmpl_gen_req,
663 ndlp, 2, 30, 0);
664 if (ret != WQE_SUCCESS) {
4b056682 665 atomic_inc(&lport->xmt_ls_err);
815a9c43 666 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
01649561
JS
667 "6052 EXIT. issue ls wqe failed lport %p, "
668 "rport %p lsreq%p Status %x DID %x\n",
669 pnvme_lport, pnvme_rport, pnvme_lsreq,
670 ret, ndlp->nlp_DID);
671 lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
672 kfree(bmp);
673 return ret;
674 }
675
676 /* Stub in routine and return 0 for now. */
677 return ret;
678}
679
680/**
681 * lpfc_nvme_ls_abort - Issue an Link Service request
682 * @lpfc_pnvme: Pointer to the driver's nvme instance data
683 * @lpfc_nvme_lport: Pointer to the driver's local port data
684 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
685 *
686 * Driver registers this routine to handle any link service request
687 * from the nvme_fc transport to a remote nvme-aware port.
688 *
689 * Return value :
690 * 0 - Success
691 * TODO: What are the failure codes.
692 **/
693static void
694lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
695 struct nvme_fc_remote_port *pnvme_rport,
696 struct nvmefc_ls_req *pnvme_lsreq)
697{
698 struct lpfc_nvme_lport *lport;
699 struct lpfc_vport *vport;
700 struct lpfc_hba *phba;
701 struct lpfc_nodelist *ndlp;
702 LIST_HEAD(abort_list);
703 struct lpfc_sli_ring *pring;
704 struct lpfc_iocbq *wqe, *next_wqe;
705
706 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
66a210ff
JS
707 if (unlikely(!lport))
708 return;
01649561
JS
709 vport = lport->vport;
710 phba = vport->phba;
711
3386f4bd
JS
712 if (vport->load_flag & FC_UNLOADING)
713 return;
714
01649561
JS
715 ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
716 if (!ndlp) {
717 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
718 "6049 Could not find node for DID %x\n",
719 pnvme_rport->port_id);
720 return;
721 }
722
723 /* Expand print to include key fields. */
724 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
725 "6040 ENTER. lport %p, rport %p lsreq %p rqstlen:%d "
825c6abb 726 "rsplen:%d %pad %pad\n",
01649561
JS
727 pnvme_lport, pnvme_rport,
728 pnvme_lsreq, pnvme_lsreq->rqstlen,
825c6abb
AB
729 pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
730 &pnvme_lsreq->rspdma);
01649561
JS
731
732 /*
733 * Lock the ELS ring txcmplq and build a local list of all ELS IOs
734 * that need an ABTS. The IOs need to stay on the txcmplq so that
735 * the abort operation completes them successfully.
736 */
737 pring = phba->sli4_hba.nvmels_wq->pring;
738 spin_lock_irq(&phba->hbalock);
739 spin_lock(&pring->ring_lock);
740 list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
741 /* Add to abort_list on on NDLP match. */
742 if (lpfc_check_sli_ndlp(phba, pring, wqe, ndlp)) {
743 wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
744 list_add_tail(&wqe->dlist, &abort_list);
745 }
746 }
747 spin_unlock(&pring->ring_lock);
748 spin_unlock_irq(&phba->hbalock);
749
750 /* Abort the targeted IOs and remove them from the abort list. */
751 list_for_each_entry_safe(wqe, next_wqe, &abort_list, dlist) {
4b056682 752 atomic_inc(&lport->xmt_ls_abort);
01649561
JS
753 spin_lock_irq(&phba->hbalock);
754 list_del_init(&wqe->dlist);
755 lpfc_sli_issue_abort_iotag(phba, pring, wqe);
756 spin_unlock_irq(&phba->hbalock);
757 }
758}
759
760/* Fix up the existing sgls for NVME IO. */
5fd11085 761static inline void
01649561
JS
762lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
763 struct lpfc_nvme_buf *lpfc_ncmd,
764 struct nvmefc_fcp_req *nCmd)
765{
4e565cf0 766 struct lpfc_hba *phba = vport->phba;
01649561
JS
767 struct sli4_sge *sgl;
768 union lpfc_wqe128 *wqe;
769 uint32_t *wptr, *dptr;
770
4e565cf0
JS
771 /*
772 * Get a local pointer to the built-in wqe and correct
773 * the cmd size to match NVME's 96 bytes and fix
774 * the dma address.
775 */
776
205e8240 777 wqe = &lpfc_ncmd->cur_iocbq.wqe;
4e565cf0 778
01649561
JS
779 /*
780 * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
781 * match NVME. NVME sends 96 bytes. Also, use the
782 * nvme commands command and response dma addresses
783 * rather than the virtual memory to ease the restore
784 * operation.
785 */
786 sgl = lpfc_ncmd->nvme_sgl;
787 sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
4e565cf0
JS
788 if (phba->cfg_nvme_embed_cmd) {
789 sgl->addr_hi = 0;
790 sgl->addr_lo = 0;
791
792 /* Word 0-2 - NVME CMND IU (embedded payload) */
793 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
794 wqe->generic.bde.tus.f.bdeSize = 56;
795 wqe->generic.bde.addrHigh = 0;
796 wqe->generic.bde.addrLow = 64; /* Word 16 */
5fd11085
JS
797
798 /* Word 10 - dbde is 0, wqes is 1 in template */
799
800 /*
801 * Embed the payload in the last half of the WQE
802 * WQE words 16-30 get the NVME CMD IU payload
803 *
804 * WQE words 16-19 get payload Words 1-4
805 * WQE words 20-21 get payload Words 6-7
806 * WQE words 22-29 get payload Words 16-23
807 */
808 wptr = &wqe->words[16]; /* WQE ptr */
809 dptr = (uint32_t *)nCmd->cmdaddr; /* payload ptr */
810 dptr++; /* Skip Word 0 in payload */
811
812 *wptr++ = *dptr++; /* Word 1 */
813 *wptr++ = *dptr++; /* Word 2 */
814 *wptr++ = *dptr++; /* Word 3 */
815 *wptr++ = *dptr++; /* Word 4 */
816 dptr++; /* Skip Word 5 in payload */
817 *wptr++ = *dptr++; /* Word 6 */
818 *wptr++ = *dptr++; /* Word 7 */
819 dptr += 8; /* Skip Words 8-15 in payload */
820 *wptr++ = *dptr++; /* Word 16 */
821 *wptr++ = *dptr++; /* Word 17 */
822 *wptr++ = *dptr++; /* Word 18 */
823 *wptr++ = *dptr++; /* Word 19 */
824 *wptr++ = *dptr++; /* Word 20 */
825 *wptr++ = *dptr++; /* Word 21 */
826 *wptr++ = *dptr++; /* Word 22 */
827 *wptr = *dptr; /* Word 23 */
4e565cf0
JS
828 } else {
829 sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
830 sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
831
832 /* Word 0-2 - NVME CMND IU Inline BDE */
833 wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
834 wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
835 wqe->generic.bde.addrHigh = sgl->addr_hi;
836 wqe->generic.bde.addrLow = sgl->addr_lo;
5fd11085
JS
837
838 /* Word 10 */
839 bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
840 bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
4e565cf0 841 }
01649561
JS
842
843 sgl++;
844
845 /* Setup the physical region for the FCP RSP */
846 sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
847 sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
848 sgl->word2 = le32_to_cpu(sgl->word2);
849 if (nCmd->sg_cnt)
850 bf_set(lpfc_sli4_sge_last, sgl, 0);
851 else
852 bf_set(lpfc_sli4_sge_last, sgl, 1);
853 sgl->word2 = cpu_to_le32(sgl->word2);
854 sgl->sge_len = cpu_to_le32(nCmd->rsplen);
01649561
JS
855}
856
bd2cdd5e
JS
857#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
858static void
859lpfc_nvme_ktime(struct lpfc_hba *phba,
860 struct lpfc_nvme_buf *lpfc_ncmd)
861{
862 uint64_t seg1, seg2, seg3, seg4;
c8a4ce0b 863 uint64_t segsum;
bd2cdd5e 864
bd2cdd5e
JS
865 if (!lpfc_ncmd->ts_last_cmd ||
866 !lpfc_ncmd->ts_cmd_start ||
867 !lpfc_ncmd->ts_cmd_wqput ||
868 !lpfc_ncmd->ts_isr_cmpl ||
869 !lpfc_ncmd->ts_data_nvme)
870 return;
c8a4ce0b
DK
871
872 if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_cmd_start)
873 return;
bd2cdd5e
JS
874 if (lpfc_ncmd->ts_cmd_start < lpfc_ncmd->ts_last_cmd)
875 return;
876 if (lpfc_ncmd->ts_cmd_wqput < lpfc_ncmd->ts_cmd_start)
877 return;
878 if (lpfc_ncmd->ts_isr_cmpl < lpfc_ncmd->ts_cmd_wqput)
879 return;
880 if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_isr_cmpl)
881 return;
882 /*
883 * Segment 1 - Time from Last FCP command cmpl is handed
884 * off to NVME Layer to start of next command.
885 * Segment 2 - Time from Driver receives a IO cmd start
886 * from NVME Layer to WQ put is done on IO cmd.
887 * Segment 3 - Time from Driver WQ put is done on IO cmd
888 * to MSI-X ISR for IO cmpl.
889 * Segment 4 - Time from MSI-X ISR for IO cmpl to when
890 * cmpl is handled off to the NVME Layer.
891 */
892 seg1 = lpfc_ncmd->ts_cmd_start - lpfc_ncmd->ts_last_cmd;
c8a4ce0b
DK
893 if (seg1 > 5000000) /* 5 ms - for sequential IOs only */
894 seg1 = 0;
bd2cdd5e
JS
895
896 /* Calculate times relative to start of IO */
897 seg2 = (lpfc_ncmd->ts_cmd_wqput - lpfc_ncmd->ts_cmd_start);
c8a4ce0b
DK
898 segsum = seg2;
899 seg3 = lpfc_ncmd->ts_isr_cmpl - lpfc_ncmd->ts_cmd_start;
900 if (segsum > seg3)
901 return;
902 seg3 -= segsum;
903 segsum += seg3;
904
905 seg4 = lpfc_ncmd->ts_data_nvme - lpfc_ncmd->ts_cmd_start;
906 if (segsum > seg4)
907 return;
908 seg4 -= segsum;
909
bd2cdd5e
JS
910 phba->ktime_data_samples++;
911 phba->ktime_seg1_total += seg1;
912 if (seg1 < phba->ktime_seg1_min)
913 phba->ktime_seg1_min = seg1;
914 else if (seg1 > phba->ktime_seg1_max)
915 phba->ktime_seg1_max = seg1;
916 phba->ktime_seg2_total += seg2;
917 if (seg2 < phba->ktime_seg2_min)
918 phba->ktime_seg2_min = seg2;
919 else if (seg2 > phba->ktime_seg2_max)
920 phba->ktime_seg2_max = seg2;
921 phba->ktime_seg3_total += seg3;
922 if (seg3 < phba->ktime_seg3_min)
923 phba->ktime_seg3_min = seg3;
924 else if (seg3 > phba->ktime_seg3_max)
925 phba->ktime_seg3_max = seg3;
926 phba->ktime_seg4_total += seg4;
927 if (seg4 < phba->ktime_seg4_min)
928 phba->ktime_seg4_min = seg4;
929 else if (seg4 > phba->ktime_seg4_max)
930 phba->ktime_seg4_max = seg4;
931
932 lpfc_ncmd->ts_last_cmd = 0;
933 lpfc_ncmd->ts_cmd_start = 0;
934 lpfc_ncmd->ts_cmd_wqput = 0;
935 lpfc_ncmd->ts_isr_cmpl = 0;
936 lpfc_ncmd->ts_data_nvme = 0;
937}
938#endif
939
01649561
JS
940/**
941 * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
942 * @lpfc_pnvme: Pointer to the driver's nvme instance data
943 * @lpfc_nvme_lport: Pointer to the driver's local port data
944 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
945 *
946 * Driver registers this routine as it io request handler. This
947 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
948 * data structure to the rport indicated in @lpfc_nvme_rport.
949 *
950 * Return value :
951 * 0 - Success
952 * TODO: What are the failure codes.
953 **/
954static void
955lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
956 struct lpfc_wcqe_complete *wcqe)
957{
958 struct lpfc_nvme_buf *lpfc_ncmd =
959 (struct lpfc_nvme_buf *)pwqeIn->context1;
960 struct lpfc_vport *vport = pwqeIn->vport;
961 struct nvmefc_fcp_req *nCmd;
962 struct nvme_fc_ersp_iu *ep;
963 struct nvme_fc_cmd_iu *cp;
01649561 964 struct lpfc_nodelist *ndlp;
bbe3012b 965 struct lpfc_nvme_fcpreq_priv *freqpriv;
4b056682 966 struct lpfc_nvme_lport *lport;
66a210ff 967 struct lpfc_nvme_ctrl_stat *cstat;
66a210ff 968 uint32_t code, status, idx;
01649561
JS
969 uint16_t cid, sqhd, data;
970 uint32_t *ptr;
971
972 /* Sanity check on return of outstanding command */
0b05e9fe 973 if (!lpfc_ncmd || !lpfc_ncmd->nvmeCmd) {
4d5e789a
JS
974 if (!lpfc_ncmd) {
975 lpfc_printf_vlog(vport, KERN_ERR,
976 LOG_NODE | LOG_NVME_IOERR,
977 "6071 Null lpfc_ncmd pointer. No "
978 "release, skip completion\n");
979 return;
980 }
981
01649561 982 lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
4d5e789a 983 "6066 Missing cmpl ptrs: lpfc_ncmd %p, "
0b05e9fe
JS
984 "nvmeCmd %p\n",
985 lpfc_ncmd, lpfc_ncmd->nvmeCmd);
4d5e789a
JS
986
987 /* Release the lpfc_ncmd regardless of the missing elements. */
988 lpfc_release_nvme_buf(phba, lpfc_ncmd);
01649561
JS
989 return;
990 }
01649561 991 nCmd = lpfc_ncmd->nvmeCmd;
4b056682 992 status = bf_get(lpfc_wcqe_c_status, wcqe);
66a210ff 993
66a85155
JS
994 if (vport->localport) {
995 lport = (struct lpfc_nvme_lport *)vport->localport->private;
996 if (lport) {
997 idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
998 cstat = &lport->cstat[idx];
999 atomic_inc(&cstat->fc4NvmeIoCmpls);
1000 if (status) {
1001 if (bf_get(lpfc_wcqe_c_xb, wcqe))
1002 atomic_inc(&lport->cmpl_fcp_xb);
1003 atomic_inc(&lport->cmpl_fcp_err);
1004 }
66a210ff 1005 }
4b056682 1006 }
01649561 1007
bd2cdd5e
JS
1008 lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
1009 lpfc_ncmd->cur_iocbq.sli4_xritag,
4b056682 1010 status, wcqe->parameter);
01649561
JS
1011 /*
1012 * Catch race where our node has transitioned, but the
1013 * transport is still transitioning.
1014 */
0b05e9fe 1015 ndlp = lpfc_ncmd->ndlp;
01649561 1016 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
0b05e9fe
JS
1017 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1018 "6062 Ignoring NVME cmpl. No ndlp\n");
1019 goto out_err;
01649561
JS
1020 }
1021
1022 code = bf_get(lpfc_wcqe_c_code, wcqe);
1023 if (code == CQE_CODE_NVME_ERSP) {
1024 /* For this type of CQE, we need to rebuild the rsp */
1025 ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
1026
1027 /*
1028 * Get Command Id from cmd to plug into response. This
1029 * code is not needed in the next NVME Transport drop.
1030 */
1031 cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
1032 cid = cp->sqe.common.command_id;
1033
1034 /*
1035 * RSN is in CQE word 2
1036 * SQHD is in CQE Word 3 bits 15:0
1037 * Cmd Specific info is in CQE Word 1
1038 * and in CQE Word 0 bits 15:0
1039 */
1040 sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
1041
1042 /* Now lets build the NVME ERSP IU */
1043 ep->iu_len = cpu_to_be16(8);
1044 ep->rsn = wcqe->parameter;
1045 ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
1046 ep->rsvd12 = 0;
1047 ptr = (uint32_t *)&ep->cqe.result.u64;
1048 *ptr++ = wcqe->total_data_placed;
1049 data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
1050 *ptr = (uint32_t)data;
1051 ep->cqe.sq_head = sqhd;
1052 ep->cqe.sq_id = nCmd->sqid;
1053 ep->cqe.command_id = cid;
1054 ep->cqe.status = 0;
1055
1056 lpfc_ncmd->status = IOSTAT_SUCCESS;
1057 lpfc_ncmd->result = 0;
1058 nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
1059 nCmd->transferred_length = nCmd->payload_length;
1060 } else {
4b056682 1061 lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
952c303b 1062 lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
01649561
JS
1063
1064 /* For NVME, the only failure path that results in an
1065 * IO error is when the adapter rejects it. All other
1066 * conditions are a success case and resolved by the
1067 * transport.
1068 * IOSTAT_FCP_RSP_ERROR means:
1069 * 1. Length of data received doesn't match total
1070 * transfer length in WQE
1071 * 2. If the RSP payload does NOT match these cases:
1072 * a. RSP length 12/24 bytes and all zeros
1073 * b. NVME ERSP
1074 */
1075 switch (lpfc_ncmd->status) {
1076 case IOSTAT_SUCCESS:
1077 nCmd->transferred_length = wcqe->total_data_placed;
1078 nCmd->rcv_rsplen = 0;
1079 nCmd->status = 0;
1080 break;
1081 case IOSTAT_FCP_RSP_ERROR:
1082 nCmd->transferred_length = wcqe->total_data_placed;
1083 nCmd->rcv_rsplen = wcqe->parameter;
1084 nCmd->status = 0;
1085 /* Sanity check */
1086 if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
1087 break;
1088 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1089 "6081 NVME Completion Protocol Error: "
86c67379
JS
1090 "xri %x status x%x result x%x "
1091 "placed x%x\n",
1092 lpfc_ncmd->cur_iocbq.sli4_xritag,
01649561
JS
1093 lpfc_ncmd->status, lpfc_ncmd->result,
1094 wcqe->total_data_placed);
1095 break;
952c303b
DK
1096 case IOSTAT_LOCAL_REJECT:
1097 /* Let fall through to set command final state. */
1098 if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
1099 lpfc_printf_vlog(vport, KERN_INFO,
1100 LOG_NVME_IOERR,
1101 "6032 Delay Aborted cmd %p "
1102 "nvme cmd %p, xri x%x, "
1103 "xb %d\n",
1104 lpfc_ncmd, nCmd,
1105 lpfc_ncmd->cur_iocbq.sli4_xritag,
1106 bf_get(lpfc_wcqe_c_xb, wcqe));
01649561
JS
1107 default:
1108out_err:
e3246a12 1109 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
86c67379 1110 "6072 NVME Completion Error: xri %x "
01649561 1111 "status x%x result x%x placed x%x\n",
86c67379 1112 lpfc_ncmd->cur_iocbq.sli4_xritag,
01649561
JS
1113 lpfc_ncmd->status, lpfc_ncmd->result,
1114 wcqe->total_data_placed);
1115 nCmd->transferred_length = 0;
1116 nCmd->rcv_rsplen = 0;
8e009ce8 1117 nCmd->status = NVME_SC_INTERNAL;
01649561
JS
1118 }
1119 }
1120
1121 /* pick up SLI4 exhange busy condition */
1122 if (bf_get(lpfc_wcqe_c_xb, wcqe))
1123 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
1124 else
1125 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
1126
01649561
JS
1127 /* Update stats and complete the IO. There is
1128 * no need for dma unprep because the nvme_transport
1129 * owns the dma address.
1130 */
bd2cdd5e 1131#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
c8a4ce0b 1132 if (lpfc_ncmd->ts_cmd_start) {
bd2cdd5e
JS
1133 lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
1134 lpfc_ncmd->ts_data_nvme = ktime_get_ns();
1135 phba->ktime_last_cmd = lpfc_ncmd->ts_data_nvme;
1136 lpfc_nvme_ktime(phba, lpfc_ncmd);
1137 }
1138 if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1139 if (lpfc_ncmd->cpu != smp_processor_id())
1140 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
1141 "6701 CPU Check cmpl: "
1142 "cpu %d expect %d\n",
1143 smp_processor_id(), lpfc_ncmd->cpu);
1144 if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1145 phba->cpucheck_cmpl_io[lpfc_ncmd->cpu]++;
1146 }
1147#endif
952c303b
DK
1148
1149 /* NVME targets need completion held off until the abort exchange
add9d6be 1150 * completes unless the NVME Rport is getting unregistered.
952c303b 1151 */
add9d6be 1152
3fd78355 1153 if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
91455b85
JS
1154 freqpriv = nCmd->private;
1155 freqpriv->nvme_buf = NULL;
952c303b 1156 nCmd->done(nCmd);
3fd78355 1157 lpfc_ncmd->nvmeCmd = NULL;
add9d6be 1158 }
01649561 1159
952c303b 1160 /* Call release with XB=1 to queue the IO into the abort list. */
01649561
JS
1161 lpfc_release_nvme_buf(phba, lpfc_ncmd);
1162}
1163
1164
1165/**
1166 * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
1167 * @lpfc_pnvme: Pointer to the driver's nvme instance data
1168 * @lpfc_nvme_lport: Pointer to the driver's local port data
1169 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1170 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1171 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1172 *
1173 * Driver registers this routine as it io request handler. This
1174 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1175 * data structure to the rport indicated in @lpfc_nvme_rport.
1176 *
1177 * Return value :
1178 * 0 - Success
1179 * TODO: What are the failure codes.
1180 **/
1181static int
1182lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
1183 struct lpfc_nvme_buf *lpfc_ncmd,
66a210ff
JS
1184 struct lpfc_nodelist *pnode,
1185 struct lpfc_nvme_ctrl_stat *cstat)
01649561
JS
1186{
1187 struct lpfc_hba *phba = vport->phba;
1188 struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
1189 struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
205e8240 1190 union lpfc_wqe128 *wqe = &pwqeq->wqe;
01649561
JS
1191 uint32_t req_len;
1192
1193 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
1194 return -EINVAL;
1195
1196 /*
1197 * There are three possibilities here - use scatter-gather segment, use
1198 * the single mapping, or neither.
1199 */
01649561
JS
1200 if (nCmd->sg_cnt) {
1201 if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
5fd11085
JS
1202 /* From the iwrite template, initialize words 7 - 11 */
1203 memcpy(&wqe->words[7],
1204 &lpfc_iwrite_cmd_template.words[7],
1205 sizeof(uint32_t) * 5);
1206
1207 /* Word 4 */
1208 wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
1209
01649561
JS
1210 /* Word 5 */
1211 if ((phba->cfg_nvme_enable_fb) &&
1212 (pnode->nlp_flag & NLP_FIRSTBURST)) {
1213 req_len = lpfc_ncmd->nvmeCmd->payload_length;
1214 if (req_len < pnode->nvme_fb_size)
1215 wqe->fcp_iwrite.initial_xfer_len =
1216 req_len;
1217 else
1218 wqe->fcp_iwrite.initial_xfer_len =
1219 pnode->nvme_fb_size;
5fd11085
JS
1220 } else {
1221 wqe->fcp_iwrite.initial_xfer_len = 0;
01649561 1222 }
66a210ff 1223 atomic_inc(&cstat->fc4NvmeOutputRequests);
01649561 1224 } else {
5fd11085
JS
1225 /* From the iread template, initialize words 7 - 11 */
1226 memcpy(&wqe->words[7],
1227 &lpfc_iread_cmd_template.words[7],
1228 sizeof(uint32_t) * 5);
1229
1230 /* Word 4 */
1231 wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
1232
1233 /* Word 5 */
1234 wqe->fcp_iread.rsrvd5 = 0;
01649561 1235
66a210ff 1236 atomic_inc(&cstat->fc4NvmeInputRequests);
01649561
JS
1237 }
1238 } else {
5fd11085
JS
1239 /* From the icmnd template, initialize words 4 - 11 */
1240 memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
1241 sizeof(uint32_t) * 8);
66a210ff 1242 atomic_inc(&cstat->fc4NvmeControlRequests);
01649561
JS
1243 }
1244 /*
1245 * Finish initializing those WQE fields that are independent
1246 * of the nvme_cmnd request_buffer
1247 */
1248
5fd11085
JS
1249 /* Word 3 */
1250 bf_set(payload_offset_len, &wqe->fcp_icmd,
1251 (nCmd->rsplen + nCmd->cmdlen));
1252
01649561
JS
1253 /* Word 6 */
1254 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
1255 phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
1256 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
1257
01649561
JS
1258 /* Word 8 */
1259 wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
1260
1261 /* Word 9 */
1262 bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
1263
414abe0a
JS
1264 /* Words 13 14 15 are for PBDE support */
1265
01649561
JS
1266 pwqeq->vport = vport;
1267 return 0;
1268}
1269
1270
1271/**
1272 * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
1273 * @lpfc_pnvme: Pointer to the driver's nvme instance data
1274 * @lpfc_nvme_lport: Pointer to the driver's local port data
1275 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1276 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1277 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1278 *
1279 * Driver registers this routine as it io request handler. This
1280 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1281 * data structure to the rport indicated in @lpfc_nvme_rport.
1282 *
1283 * Return value :
1284 * 0 - Success
1285 * TODO: What are the failure codes.
1286 **/
1287static int
1288lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
1289 struct lpfc_nvme_buf *lpfc_ncmd)
1290{
1291 struct lpfc_hba *phba = vport->phba;
1292 struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
205e8240 1293 union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
01649561
JS
1294 struct sli4_sge *sgl = lpfc_ncmd->nvme_sgl;
1295 struct scatterlist *data_sg;
1296 struct sli4_sge *first_data_sgl;
0bc2b7c5 1297 struct ulp_bde64 *bde;
01649561
JS
1298 dma_addr_t physaddr;
1299 uint32_t num_bde = 0;
1300 uint32_t dma_len;
1301 uint32_t dma_offset = 0;
1302 int nseg, i;
1303
1304 /* Fix up the command and response DMA stuff. */
1305 lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
1306
1307 /*
1308 * There are three possibilities here - use scatter-gather segment, use
1309 * the single mapping, or neither.
1310 */
1311 if (nCmd->sg_cnt) {
1312 /*
1313 * Jump over the cmd and rsp SGEs. The fix routine
1314 * has already adjusted for this.
1315 */
1316 sgl += 2;
1317
1318 first_data_sgl = sgl;
1319 lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
81e6a637 1320 if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
01649561
JS
1321 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1322 "6058 Too many sg segments from "
1323 "NVME Transport. Max %d, "
1324 "nvmeIO sg_cnt %d\n",
29bfd55a 1325 phba->cfg_nvme_seg_cnt + 1,
01649561
JS
1326 lpfc_ncmd->seg_cnt);
1327 lpfc_ncmd->seg_cnt = 0;
1328 return 1;
1329 }
1330
1331 /*
1332 * The driver established a maximum scatter-gather segment count
1333 * during probe that limits the number of sg elements in any
1334 * single nvme command. Just run through the seg_cnt and format
1335 * the sge's.
1336 */
1337 nseg = nCmd->sg_cnt;
1338 data_sg = nCmd->first_sgl;
1339 for (i = 0; i < nseg; i++) {
1340 if (data_sg == NULL) {
1341 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1342 "6059 dptr err %d, nseg %d\n",
1343 i, nseg);
1344 lpfc_ncmd->seg_cnt = 0;
1345 return 1;
1346 }
1347 physaddr = data_sg->dma_address;
1348 dma_len = data_sg->length;
1349 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1350 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1351 sgl->word2 = le32_to_cpu(sgl->word2);
1352 if ((num_bde + 1) == nseg)
1353 bf_set(lpfc_sli4_sge_last, sgl, 1);
1354 else
1355 bf_set(lpfc_sli4_sge_last, sgl, 0);
1356 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1357 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
1358 sgl->word2 = cpu_to_le32(sgl->word2);
1359 sgl->sge_len = cpu_to_le32(dma_len);
1360
1361 dma_offset += dma_len;
1362 data_sg = sg_next(data_sg);
1363 sgl++;
1364 }
414abe0a 1365 if (phba->cfg_enable_pbde) {
0bc2b7c5
JS
1366 /* Use PBDE support for first SGL only, offset == 0 */
1367 /* Words 13-15 */
1368 bde = (struct ulp_bde64 *)
1369 &wqe->words[13];
1370 bde->addrLow = first_data_sgl->addr_lo;
1371 bde->addrHigh = first_data_sgl->addr_hi;
1372 bde->tus.f.bdeSize =
1373 le32_to_cpu(first_data_sgl->sge_len);
1374 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1375 bde->tus.w = cpu_to_le32(bde->tus.w);
5fd11085
JS
1376 /* wqe_pbde is 1 in template */
1377 } else {
1378 memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
0bc2b7c5 1379 bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
5fd11085 1380 }
0bc2b7c5 1381
414abe0a 1382 } else {
01649561
JS
1383 /* For this clause to be valid, the payload_length
1384 * and sg_cnt must zero.
1385 */
1386 if (nCmd->payload_length != 0) {
1387 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1388 "6063 NVME DMA Prep Err: sg_cnt %d "
1389 "payload_length x%x\n",
1390 nCmd->sg_cnt, nCmd->payload_length);
1391 return 1;
1392 }
1393 }
01649561
JS
1394 return 0;
1395}
1396
1397/**
1398 * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
1399 * @lpfc_pnvme: Pointer to the driver's nvme instance data
1400 * @lpfc_nvme_lport: Pointer to the driver's local port data
1401 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1402 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1403 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1404 *
1405 * Driver registers this routine as it io request handler. This
1406 * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
1407 * data structure to the rport
1408 indicated in @lpfc_nvme_rport.
1409 *
1410 * Return value :
1411 * 0 - Success
1412 * TODO: What are the failure codes.
1413 **/
1414static int
1415lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
1416 struct nvme_fc_remote_port *pnvme_rport,
1417 void *hw_queue_handle,
1418 struct nvmefc_fcp_req *pnvme_fcreq)
1419{
1420 int ret = 0;
cf1a1d3e 1421 int expedite = 0;
66a210ff 1422 int idx;
01649561 1423 struct lpfc_nvme_lport *lport;
66a210ff 1424 struct lpfc_nvme_ctrl_stat *cstat;
01649561
JS
1425 struct lpfc_vport *vport;
1426 struct lpfc_hba *phba;
1427 struct lpfc_nodelist *ndlp;
1428 struct lpfc_nvme_buf *lpfc_ncmd;
1429 struct lpfc_nvme_rport *rport;
1430 struct lpfc_nvme_qhandle *lpfc_queue_info;
c3725bdc 1431 struct lpfc_nvme_fcpreq_priv *freqpriv;
cf1a1d3e 1432 struct nvme_common_command *sqe;
bd2cdd5e
JS
1433#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1434 uint64_t start = 0;
1435#endif
01649561 1436
c3725bdc
JS
1437 /* Validate pointers. LLDD fault handling with transport does
1438 * have timing races.
1439 */
01649561 1440 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
c3725bdc
JS
1441 if (unlikely(!lport)) {
1442 ret = -EINVAL;
1443 goto out_fail;
1444 }
1445
01649561 1446 vport = lport->vport;
c3725bdc
JS
1447
1448 if (unlikely(!hw_queue_handle)) {
44c2757b
JS
1449 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1450 "6117 Fail IO, NULL hw_queue_handle\n");
1451 atomic_inc(&lport->xmt_fcp_err);
cd240071 1452 ret = -EBUSY;
c3725bdc
JS
1453 goto out_fail;
1454 }
1455
01649561
JS
1456 phba = vport->phba;
1457
1abcb371
DK
1458 if (vport->load_flag & FC_UNLOADING) {
1459 ret = -ENODEV;
1460 goto out_fail;
1461 }
1462
3386f4bd 1463 if (vport->load_flag & FC_UNLOADING) {
44c2757b
JS
1464 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1465 "6124 Fail IO, Driver unload\n");
1466 atomic_inc(&lport->xmt_fcp_err);
3386f4bd
JS
1467 ret = -ENODEV;
1468 goto out_fail;
1469 }
1470
c3725bdc
JS
1471 freqpriv = pnvme_fcreq->private;
1472 if (unlikely(!freqpriv)) {
44c2757b
JS
1473 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1474 "6158 Fail IO, NULL request data\n");
1475 atomic_inc(&lport->xmt_fcp_err);
c3725bdc 1476 ret = -EINVAL;
b7672ae6
DK
1477 goto out_fail;
1478 }
1479
bd2cdd5e
JS
1480#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1481 if (phba->ktime_on)
1482 start = ktime_get_ns();
1483#endif
01649561
JS
1484 rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
1485 lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
1486
1487 /*
1488 * Catch race where our node has transitioned, but the
1489 * transport is still transitioning.
1490 */
1491 ndlp = rport->ndlp;
1492 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
44c2757b
JS
1493 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
1494 "6053 Fail IO, ndlp not ready: rport %p "
1495 "ndlp %p, DID x%06x\n",
01649561 1496 rport, ndlp, pnvme_rport->port_id);
44c2757b
JS
1497 atomic_inc(&lport->xmt_fcp_err);
1498 ret = -EBUSY;
1499 goto out_fail;
01649561
JS
1500 }
1501
1502 /* The remote node has to be a mapped target or it's an error. */
1503 if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
1504 (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
44c2757b
JS
1505 lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
1506 "6036 Fail IO, DID x%06x not ready for "
cd240071 1507 "IO. State x%x, Type x%x Flg x%x\n",
44c2757b 1508 pnvme_rport->port_id,
cd240071
JS
1509 ndlp->nlp_state, ndlp->nlp_type,
1510 ndlp->upcall_flags);
4b056682 1511 atomic_inc(&lport->xmt_fcp_bad_ndlp);
cd240071 1512 ret = -EBUSY;
01649561
JS
1513 goto out_fail;
1514
1515 }
1516
cf1a1d3e
JS
1517 /* Currently only NVME Keep alive commands should be expedited
1518 * if the driver runs out of a resource. These should only be
1519 * issued on the admin queue, qidx 0
1520 */
1521 if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
1522 sqe = &((struct nvme_fc_cmd_iu *)
1523 pnvme_fcreq->cmdaddr)->sqe.common;
1524 if (sqe->opcode == nvme_admin_keep_alive)
1525 expedite = 1;
1526 }
1527
01649561
JS
1528 /* The node is shared with FCP IO, make sure the IO pending count does
1529 * not exceed the programmed depth.
1530 */
2a5b7d62
JS
1531 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1532 if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
1533 !expedite) {
1534 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1535 "6174 Fail IO, ndlp qdepth exceeded: "
1536 "idx %d DID %x pend %d qdepth %d\n",
1537 lpfc_queue_info->index, ndlp->nlp_DID,
1538 atomic_read(&ndlp->cmd_pending),
1539 ndlp->cmd_qdepth);
1540 atomic_inc(&lport->xmt_fcp_qdepth);
1541 ret = -EBUSY;
1542 goto out_fail;
1543 }
01649561
JS
1544 }
1545
cf1a1d3e 1546 lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, expedite);
01649561 1547 if (lpfc_ncmd == NULL) {
4b056682 1548 atomic_inc(&lport->xmt_fcp_noxri);
01649561 1549 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
44c2757b
JS
1550 "6065 Fail IO, driver buffer pool is empty: "
1551 "idx %d DID %x\n",
1552 lpfc_queue_info->index, ndlp->nlp_DID);
cd22d605 1553 ret = -EBUSY;
01649561
JS
1554 goto out_fail;
1555 }
bd2cdd5e 1556#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
c8a4ce0b 1557 if (start) {
bd2cdd5e
JS
1558 lpfc_ncmd->ts_cmd_start = start;
1559 lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
c8a4ce0b
DK
1560 } else {
1561 lpfc_ncmd->ts_cmd_start = 0;
bd2cdd5e
JS
1562 }
1563#endif
01649561
JS
1564
1565 /*
1566 * Store the data needed by the driver to issue, abort, and complete
1567 * an IO.
1568 * Do not let the IO hang out forever. There is no midlayer issuing
1569 * an abort so inform the FW of the maximum IO pending time.
1570 */
bbe3012b 1571 freqpriv->nvme_buf = lpfc_ncmd;
01649561 1572 lpfc_ncmd->nvmeCmd = pnvme_fcreq;
318083ad 1573 lpfc_ncmd->ndlp = ndlp;
01649561
JS
1574 lpfc_ncmd->start_time = jiffies;
1575
01649561
JS
1576 /*
1577 * Issue the IO on the WQ indicated by index in the hw_queue_handle.
1578 * This identfier was create in our hardware queue create callback
1579 * routine. The driver now is dependent on the IO queue steering from
1580 * the transport. We are trusting the upper NVME layers know which
1581 * index to use and that they have affinitized a CPU to this hardware
1582 * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
1583 */
66a210ff
JS
1584 idx = lpfc_queue_info->index;
1585 lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
1586 cstat = &lport->cstat[idx];
1587
1588 lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
1589 ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
1590 if (ret) {
44c2757b
JS
1591 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
1592 "6175 Fail IO, Prep DMA: "
1593 "idx %d DID %x\n",
1594 lpfc_queue_info->index, ndlp->nlp_DID);
1595 atomic_inc(&lport->xmt_fcp_err);
66a210ff
JS
1596 ret = -ENOMEM;
1597 goto out_free_nvme_buf;
1598 }
1599
bd2cdd5e
JS
1600 lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
1601 lpfc_ncmd->cur_iocbq.sli4_xritag,
1602 lpfc_queue_info->index, ndlp->nlp_DID);
1603
01649561
JS
1604 ret = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, &lpfc_ncmd->cur_iocbq);
1605 if (ret) {
4b056682 1606 atomic_inc(&lport->xmt_fcp_wqerr);
e3246a12 1607 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
44c2757b 1608 "6113 Fail IO, Could not issue WQE err %x "
01649561
JS
1609 "sid: x%x did: x%x oxid: x%x\n",
1610 ret, vport->fc_myDID, ndlp->nlp_DID,
1611 lpfc_ncmd->cur_iocbq.sli4_xritag);
01649561
JS
1612 goto out_free_nvme_buf;
1613 }
1614
bd2cdd5e 1615#ifdef CONFIG_SCSI_LPFC_DEBUG_FS
c8a4ce0b 1616 if (lpfc_ncmd->ts_cmd_start)
bd2cdd5e
JS
1617 lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
1618
1619 if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
1620 lpfc_ncmd->cpu = smp_processor_id();
1621 if (lpfc_ncmd->cpu != lpfc_queue_info->index) {
1622 /* Check for admin queue */
1623 if (lpfc_queue_info->qidx) {
1624 lpfc_printf_vlog(vport,
1625 KERN_ERR, LOG_NVME_IOERR,
1626 "6702 CPU Check cmd: "
1627 "cpu %d wq %d\n",
1628 lpfc_ncmd->cpu,
1629 lpfc_queue_info->index);
1630 }
1631 lpfc_ncmd->cpu = lpfc_queue_info->index;
1632 }
1633 if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
1634 phba->cpucheck_xmt_io[lpfc_ncmd->cpu]++;
1635 }
1636#endif
01649561
JS
1637 return 0;
1638
1639 out_free_nvme_buf:
2cee7808
JS
1640 if (lpfc_ncmd->nvmeCmd->sg_cnt) {
1641 if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
66a210ff 1642 atomic_dec(&cstat->fc4NvmeOutputRequests);
2cee7808 1643 else
66a210ff 1644 atomic_dec(&cstat->fc4NvmeInputRequests);
2cee7808 1645 } else
66a210ff 1646 atomic_dec(&cstat->fc4NvmeControlRequests);
01649561
JS
1647 lpfc_release_nvme_buf(phba, lpfc_ncmd);
1648 out_fail:
1649 return ret;
1650}
1651
1652/**
1653 * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
1654 * @phba: Pointer to HBA context object
1655 * @cmdiocb: Pointer to command iocb object.
1656 * @rspiocb: Pointer to response iocb object.
1657 *
1658 * This is the callback function for any NVME FCP IO that was aborted.
1659 *
1660 * Return value:
1661 * None
1662 **/
1663void
1664lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
1665 struct lpfc_wcqe_complete *abts_cmpl)
1666{
e3246a12 1667 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
01649561
JS
1668 "6145 ABORT_XRI_CN completing on rpi x%x "
1669 "original iotag x%x, abort cmd iotag x%x "
1670 "req_tag x%x, status x%x, hwstatus x%x\n",
1671 cmdiocb->iocb.un.acxri.abortContextTag,
1672 cmdiocb->iocb.un.acxri.abortIoTag,
1673 cmdiocb->iotag,
1674 bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
1675 bf_get(lpfc_wcqe_c_status, abts_cmpl),
1676 bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
1677 lpfc_sli_release_iocbq(phba, cmdiocb);
1678}
1679
1680/**
1681 * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
1682 * @lpfc_pnvme: Pointer to the driver's nvme instance data
1683 * @lpfc_nvme_lport: Pointer to the driver's local port data
1684 * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
1685 * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
1686 * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
1687 *
1688 * Driver registers this routine as its nvme request io abort handler. This
1689 * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
1690 * data structure to the rport indicated in @lpfc_nvme_rport. This routine
1691 * is executed asynchronously - one the target is validated as "MAPPED" and
1692 * ready for IO, the driver issues the abort request and returns.
1693 *
1694 * Return value:
1695 * None
1696 **/
1697static void
1698lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
1699 struct nvme_fc_remote_port *pnvme_rport,
1700 void *hw_queue_handle,
1701 struct nvmefc_fcp_req *pnvme_fcreq)
1702{
1703 struct lpfc_nvme_lport *lport;
1704 struct lpfc_vport *vport;
1705 struct lpfc_hba *phba;
01649561
JS
1706 struct lpfc_nvme_buf *lpfc_nbuf;
1707 struct lpfc_iocbq *abts_buf;
1708 struct lpfc_iocbq *nvmereq_wqe;
c3725bdc 1709 struct lpfc_nvme_fcpreq_priv *freqpriv;
205e8240 1710 union lpfc_wqe128 *abts_wqe;
01649561
JS
1711 unsigned long flags;
1712 int ret_val;
1713
c3725bdc
JS
1714 /* Validate pointers. LLDD fault handling with transport does
1715 * have timing races.
1716 */
01649561 1717 lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
c3725bdc
JS
1718 if (unlikely(!lport))
1719 return;
1720
01649561 1721 vport = lport->vport;
c3725bdc
JS
1722
1723 if (unlikely(!hw_queue_handle)) {
1724 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
1725 "6129 Fail Abort, HW Queue Handle NULL.\n");
1726 return;
1727 }
1728
01649561 1729 phba = vport->phba;
c3725bdc 1730 freqpriv = pnvme_fcreq->private;
01649561 1731
c3725bdc
JS
1732 if (unlikely(!freqpriv))
1733 return;
3386f4bd
JS
1734 if (vport->load_flag & FC_UNLOADING)
1735 return;
1736
01649561 1737 /* Announce entry to new IO submit field. */
86c67379 1738 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
01649561
JS
1739 "6002 Abort Request to rport DID x%06x "
1740 "for nvme_fc_req %p\n",
1741 pnvme_rport->port_id,
1742 pnvme_fcreq);
1743
01649561
JS
1744 /* If the hba is getting reset, this flag is set. It is
1745 * cleared when the reset is complete and rings reestablished.
1746 */
1747 spin_lock_irqsave(&phba->hbalock, flags);
1748 /* driver queued commands are in process of being flushed */
1749 if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
1750 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1751 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1752 "6139 Driver in reset cleanup - flushing "
1753 "NVME Req now. hba_flag x%x\n",
1754 phba->hba_flag);
1755 return;
1756 }
1757
bbe3012b 1758 lpfc_nbuf = freqpriv->nvme_buf;
01649561
JS
1759 if (!lpfc_nbuf) {
1760 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1761 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1762 "6140 NVME IO req has no matching lpfc nvme "
1763 "io buffer. Skipping abort req.\n");
1764 return;
1765 } else if (!lpfc_nbuf->nvmeCmd) {
1766 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1767 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1768 "6141 lpfc NVME IO req has no nvme_fcreq "
1769 "io buffer. Skipping abort req.\n");
1770 return;
1771 }
2b7824d0 1772 nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
01649561
JS
1773
1774 /*
1775 * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
1776 * state must match the nvme_fcreq passed by the nvme
1777 * transport. If they don't match, it is likely the driver
1778 * has already completed the NVME IO and the nvme transport
1779 * has not seen it yet.
1780 */
1781 if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
1782 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1783 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1784 "6143 NVME req mismatch: "
1785 "lpfc_nbuf %p nvmeCmd %p, "
2b7824d0 1786 "pnvme_fcreq %p. Skipping Abort xri x%x\n",
01649561 1787 lpfc_nbuf, lpfc_nbuf->nvmeCmd,
2b7824d0 1788 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
01649561
JS
1789 return;
1790 }
1791
1792 /* Don't abort IOs no longer on the pending queue. */
01649561
JS
1793 if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
1794 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1795 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561 1796 "6142 NVME IO req %p not queued - skipping "
2b7824d0
JS
1797 "abort req xri x%x\n",
1798 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
01649561
JS
1799 return;
1800 }
1801
4b056682 1802 atomic_inc(&lport->xmt_fcp_abort);
bd2cdd5e
JS
1803 lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
1804 nvmereq_wqe->sli4_xritag,
00cefeb9 1805 nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
bd2cdd5e 1806
01649561
JS
1807 /* Outstanding abort is in progress */
1808 if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
1809 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1810 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1811 "6144 Outstanding NVME I/O Abort Request "
1812 "still pending on nvme_fcreq %p, "
2b7824d0
JS
1813 "lpfc_ncmd %p xri x%x\n",
1814 pnvme_fcreq, lpfc_nbuf,
1815 nvmereq_wqe->sli4_xritag);
01649561
JS
1816 return;
1817 }
1818
1819 abts_buf = __lpfc_sli_get_iocbq(phba);
1820 if (!abts_buf) {
1821 spin_unlock_irqrestore(&phba->hbalock, flags);
86c67379 1822 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561 1823 "6136 No available abort wqes. Skipping "
2b7824d0
JS
1824 "Abts req for nvme_fcreq %p xri x%x\n",
1825 pnvme_fcreq, nvmereq_wqe->sli4_xritag);
01649561
JS
1826 return;
1827 }
1828
1829 /* Ready - mark outstanding as aborted by driver. */
1830 nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
1831
1832 /* Complete prepping the abort wqe and issue to the FW. */
1833 abts_wqe = &abts_buf->wqe;
1834
1835 /* WQEs are reused. Clear stale data and set key fields to
1836 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
1837 */
1838 memset(abts_wqe, 0, sizeof(union lpfc_wqe));
1839 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
1840
1841 /* word 7 */
01649561
JS
1842 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
1843 bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
1844 nvmereq_wqe->iocb.ulpClass);
1845
1846 /* word 8 - tell the FW to abort the IO associated with this
1847 * outstanding exchange ID.
1848 */
1849 abts_wqe->abort_cmd.wqe_com.abort_tag = nvmereq_wqe->sli4_xritag;
1850
1851 /* word 9 - this is the iotag for the abts_wqe completion. */
1852 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
1853 abts_buf->iotag);
1854
1855 /* word 10 */
01649561
JS
1856 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
1857 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
1858
1859 /* word 11 */
1860 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
1861 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
1862 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
1863
1864 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
1865 abts_buf->iocb_flag |= LPFC_IO_NVME;
1866 abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
1867 abts_buf->vport = vport;
1868 abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
1869 ret_val = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_buf);
1870 spin_unlock_irqrestore(&phba->hbalock, flags);
cd22d605 1871 if (ret_val) {
86c67379 1872 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
01649561
JS
1873 "6137 Failed abts issue_wqe with status x%x "
1874 "for nvme_fcreq %p.\n",
1875 ret_val, pnvme_fcreq);
1876 lpfc_sli_release_iocbq(phba, abts_buf);
1877 return;
1878 }
1879
86c67379 1880 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
2b7824d0 1881 "6138 Transport Abort NVME Request Issued for "
01649561
JS
1882 "ox_id x%x on reqtag x%x\n",
1883 nvmereq_wqe->sli4_xritag,
1884 abts_buf->iotag);
1885}
1886
1887/* Declare and initialization an instance of the FC NVME template. */
1888static struct nvme_fc_port_template lpfc_nvme_template = {
1889 /* initiator-based functions */
1890 .localport_delete = lpfc_nvme_localport_delete,
1891 .remoteport_delete = lpfc_nvme_remoteport_delete,
1892 .create_queue = lpfc_nvme_create_queue,
1893 .delete_queue = lpfc_nvme_delete_queue,
1894 .ls_req = lpfc_nvme_ls_req,
1895 .fcp_io = lpfc_nvme_fcp_io_submit,
1896 .ls_abort = lpfc_nvme_ls_abort,
1897 .fcp_abort = lpfc_nvme_fcp_abort,
1898
1899 .max_hw_queues = 1,
1900 .max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1901 .max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
1902 .dma_boundary = 0xFFFFFFFF,
1903
1904 /* Sizes of additional private data for data structures.
1905 * No use for the last two sizes at this time.
1906 */
1907 .local_priv_sz = sizeof(struct lpfc_nvme_lport),
1908 .remote_priv_sz = sizeof(struct lpfc_nvme_rport),
1909 .lsrqst_priv_sz = 0,
bbe3012b 1910 .fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
01649561
JS
1911};
1912
1913/**
1914 * lpfc_sli4_post_nvme_sgl_block - post a block of nvme sgl list to firmware
1915 * @phba: pointer to lpfc hba data structure.
1916 * @nblist: pointer to nvme buffer list.
1917 * @count: number of scsi buffers on the list.
1918 *
1919 * This routine is invoked to post a block of @count scsi sgl pages from a
1920 * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
1921 * No Lock is held.
1922 *
1923 **/
1924static int
1925lpfc_sli4_post_nvme_sgl_block(struct lpfc_hba *phba,
1926 struct list_head *nblist,
1927 int count)
1928{
1929 struct lpfc_nvme_buf *lpfc_ncmd;
1930 struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
1931 struct sgl_page_pairs *sgl_pg_pairs;
1932 void *viraddr;
1933 LPFC_MBOXQ_t *mbox;
1934 uint32_t reqlen, alloclen, pg_pairs;
1935 uint32_t mbox_tmo;
1936 uint16_t xritag_start = 0;
1937 int rc = 0;
1938 uint32_t shdr_status, shdr_add_status;
1939 dma_addr_t pdma_phys_bpl1;
1940 union lpfc_sli4_cfg_shdr *shdr;
1941
1942 /* Calculate the requested length of the dma memory */
1943 reqlen = count * sizeof(struct sgl_page_pairs) +
1944 sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
1945 if (reqlen > SLI4_PAGE_SIZE) {
1946 lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
1947 "6118 Block sgl registration required DMA "
1948 "size (%d) great than a page\n", reqlen);
1949 return -ENOMEM;
1950 }
1951 mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
1952 if (!mbox) {
1953 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1954 "6119 Failed to allocate mbox cmd memory\n");
1955 return -ENOMEM;
1956 }
1957
1958 /* Allocate DMA memory and set up the non-embedded mailbox command */
1959 alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
1960 LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
1961 LPFC_SLI4_MBX_NEMBED);
1962
1963 if (alloclen < reqlen) {
1964 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1965 "6120 Allocated DMA memory size (%d) is "
1966 "less than the requested DMA memory "
1967 "size (%d)\n", alloclen, reqlen);
1968 lpfc_sli4_mbox_cmd_free(phba, mbox);
1969 return -ENOMEM;
1970 }
1971
1972 /* Get the first SGE entry from the non-embedded DMA memory */
1973 viraddr = mbox->sge_array->addr[0];
1974
1975 /* Set up the SGL pages in the non-embedded DMA pages */
1976 sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
1977 sgl_pg_pairs = &sgl->sgl_pg_pairs;
1978
1979 pg_pairs = 0;
1980 list_for_each_entry(lpfc_ncmd, nblist, list) {
1981 /* Set up the sge entry */
1982 sgl_pg_pairs->sgl_pg0_addr_lo =
1983 cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
1984 sgl_pg_pairs->sgl_pg0_addr_hi =
1985 cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
1986 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
1987 pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
1988 SGL_PAGE_SIZE;
1989 else
1990 pdma_phys_bpl1 = 0;
1991 sgl_pg_pairs->sgl_pg1_addr_lo =
1992 cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
1993 sgl_pg_pairs->sgl_pg1_addr_hi =
1994 cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
1995 /* Keep the first xritag on the list */
1996 if (pg_pairs == 0)
1997 xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
1998 sgl_pg_pairs++;
1999 pg_pairs++;
2000 }
2001 bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
2002 bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
2003 /* Perform endian conversion if necessary */
2004 sgl->word0 = cpu_to_le32(sgl->word0);
2005
2006 if (!phba->sli4_hba.intr_enable)
2007 rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
2008 else {
2009 mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
2010 rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
2011 }
2012 shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
2013 shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
2014 shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
2015 if (rc != MBX_TIMEOUT)
2016 lpfc_sli4_mbox_cmd_free(phba, mbox);
2017 if (shdr_status || shdr_add_status || rc) {
2018 lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
2019 "6125 POST_SGL_BLOCK mailbox command failed "
2020 "status x%x add_status x%x mbx status x%x\n",
2021 shdr_status, shdr_add_status, rc);
2022 rc = -ENXIO;
2023 }
2024 return rc;
2025}
2026
2027/**
2028 * lpfc_post_nvme_sgl_list - Post blocks of nvme buffer sgls from a list
2029 * @phba: pointer to lpfc hba data structure.
2030 * @post_nblist: pointer to the nvme buffer list.
2031 *
2032 * This routine walks a list of nvme buffers that was passed in. It attempts
2033 * to construct blocks of nvme buffer sgls which contains contiguous xris and
2034 * uses the non-embedded SGL block post mailbox commands to post to the port.
2035 * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
2036 * embedded SGL post mailbox command for posting. The @post_nblist passed in
2037 * must be local list, thus no lock is needed when manipulate the list.
2038 *
2039 * Returns: 0 = failure, non-zero number of successfully posted buffers.
2040 **/
2041static int
2042lpfc_post_nvme_sgl_list(struct lpfc_hba *phba,
2043 struct list_head *post_nblist, int sb_count)
2044{
2045 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
2046 int status, sgl_size;
2047 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
2048 dma_addr_t pdma_phys_sgl1;
2049 int last_xritag = NO_XRI;
2050 int cur_xritag;
2051 LIST_HEAD(prep_nblist);
2052 LIST_HEAD(blck_nblist);
2053 LIST_HEAD(nvme_nblist);
2054
2055 /* sanity check */
2056 if (sb_count <= 0)
2057 return -EINVAL;
2058
2059 sgl_size = phba->cfg_sg_dma_buf_size;
2060
2061 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
2062 list_del_init(&lpfc_ncmd->list);
2063 block_cnt++;
2064 if ((last_xritag != NO_XRI) &&
2065 (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
2066 /* a hole in xri block, form a sgl posting block */
2067 list_splice_init(&prep_nblist, &blck_nblist);
2068 post_cnt = block_cnt - 1;
2069 /* prepare list for next posting block */
2070 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
2071 block_cnt = 1;
2072 } else {
2073 /* prepare list for next posting block */
2074 list_add_tail(&lpfc_ncmd->list, &prep_nblist);
2075 /* enough sgls for non-embed sgl mbox command */
2076 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
2077 list_splice_init(&prep_nblist, &blck_nblist);
2078 post_cnt = block_cnt;
2079 block_cnt = 0;
2080 }
2081 }
2082 num_posting++;
2083 last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
2084
2085 /* end of repost sgl list condition for NVME buffers */
2086 if (num_posting == sb_count) {
2087 if (post_cnt == 0) {
2088 /* last sgl posting block */
2089 list_splice_init(&prep_nblist, &blck_nblist);
2090 post_cnt = block_cnt;
2091 } else if (block_cnt == 1) {
2092 /* last single sgl with non-contiguous xri */
2093 if (sgl_size > SGL_PAGE_SIZE)
2094 pdma_phys_sgl1 =
2095 lpfc_ncmd->dma_phys_sgl +
2096 SGL_PAGE_SIZE;
2097 else
2098 pdma_phys_sgl1 = 0;
2099 cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
2100 status = lpfc_sli4_post_sgl(phba,
2101 lpfc_ncmd->dma_phys_sgl,
2102 pdma_phys_sgl1, cur_xritag);
2103 if (status) {
2104 /* failure, put on abort nvme list */
318083ad 2105 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
01649561
JS
2106 } else {
2107 /* success, put on NVME buffer list */
318083ad 2108 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
01649561
JS
2109 lpfc_ncmd->status = IOSTAT_SUCCESS;
2110 num_posted++;
2111 }
2112 /* success, put on NVME buffer sgl list */
2113 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
2114 }
2115 }
2116
2117 /* continue until a nembed page worth of sgls */
2118 if (post_cnt == 0)
2119 continue;
2120
2121 /* post block of NVME buffer list sgls */
2122 status = lpfc_sli4_post_nvme_sgl_block(phba, &blck_nblist,
2123 post_cnt);
2124
2125 /* don't reset xirtag due to hole in xri block */
2126 if (block_cnt == 0)
2127 last_xritag = NO_XRI;
2128
2129 /* reset NVME buffer post count for next round of posting */
2130 post_cnt = 0;
2131
2132 /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
2133 while (!list_empty(&blck_nblist)) {
2134 list_remove_head(&blck_nblist, lpfc_ncmd,
2135 struct lpfc_nvme_buf, list);
2136 if (status) {
2137 /* failure, put on abort nvme list */
318083ad 2138 lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
01649561
JS
2139 } else {
2140 /* success, put on NVME buffer list */
318083ad 2141 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
01649561
JS
2142 lpfc_ncmd->status = IOSTAT_SUCCESS;
2143 num_posted++;
2144 }
2145 list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
2146 }
2147 }
2148 /* Push NVME buffers with sgl posted to the available list */
2149 while (!list_empty(&nvme_nblist)) {
2150 list_remove_head(&nvme_nblist, lpfc_ncmd,
2151 struct lpfc_nvme_buf, list);
2152 lpfc_release_nvme_buf(phba, lpfc_ncmd);
2153 }
2154 return num_posted;
2155}
2156
2157/**
2158 * lpfc_repost_nvme_sgl_list - Repost all the allocated nvme buffer sgls
2159 * @phba: pointer to lpfc hba data structure.
2160 *
2161 * This routine walks the list of nvme buffers that have been allocated and
2162 * repost them to the port by using SGL block post. This is needed after a
2163 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
2164 * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
2165 * to the lpfc_nvme_buf_list. If the repost fails, reject all nvme buffers.
2166 *
2167 * Returns: 0 = success, non-zero failure.
2168 **/
2169int
2170lpfc_repost_nvme_sgl_list(struct lpfc_hba *phba)
2171{
2172 LIST_HEAD(post_nblist);
2173 int num_posted, rc = 0;
2174
2175 /* get all NVME buffers need to repost to a local list */
2176 spin_lock_irq(&phba->nvme_buf_list_get_lock);
2177 spin_lock(&phba->nvme_buf_list_put_lock);
2178 list_splice_init(&phba->lpfc_nvme_buf_list_get, &post_nblist);
2179 list_splice(&phba->lpfc_nvme_buf_list_put, &post_nblist);
cf1a1d3e
JS
2180 phba->get_nvme_bufs = 0;
2181 phba->put_nvme_bufs = 0;
01649561
JS
2182 spin_unlock(&phba->nvme_buf_list_put_lock);
2183 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
2184
2185 /* post the list of nvme buffer sgls to port if available */
2186 if (!list_empty(&post_nblist)) {
2187 num_posted = lpfc_post_nvme_sgl_list(phba, &post_nblist,
2188 phba->sli4_hba.nvme_xri_cnt);
2189 /* failed to post any nvme buffer, return error */
2190 if (num_posted == 0)
2191 rc = -EIO;
2192 }
2193 return rc;
2194}
2195
2196/**
2197 * lpfc_new_nvme_buf - Scsi buffer allocator for HBA with SLI4 IF spec
2198 * @vport: The virtual port for which this call being executed.
2199 * @num_to_allocate: The requested number of buffers to allocate.
2200 *
2201 * This routine allocates nvme buffers for device with SLI-4 interface spec,
2202 * the nvme buffer contains all the necessary information needed to initiate
2203 * a NVME I/O. After allocating up to @num_to_allocate NVME buffers and put
2204 * them on a list, it post them to the port by using SGL block post.
2205 *
2206 * Return codes:
2207 * int - number of nvme buffers that were allocated and posted.
2208 * 0 = failure, less than num_to_alloc is a partial failure.
2209 **/
2210static int
2211lpfc_new_nvme_buf(struct lpfc_vport *vport, int num_to_alloc)
2212{
2213 struct lpfc_hba *phba = vport->phba;
2214 struct lpfc_nvme_buf *lpfc_ncmd;
2215 struct lpfc_iocbq *pwqeq;
2216 union lpfc_wqe128 *wqe;
2217 struct sli4_sge *sgl;
2218 dma_addr_t pdma_phys_sgl;
2219 uint16_t iotag, lxri = 0;
a63eba9e 2220 int bcnt, num_posted;
01649561
JS
2221 LIST_HEAD(prep_nblist);
2222 LIST_HEAD(post_nblist);
2223 LIST_HEAD(nvme_nblist);
2224
01649561
JS
2225 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
2226 lpfc_ncmd = kzalloc(sizeof(struct lpfc_nvme_buf), GFP_KERNEL);
2227 if (!lpfc_ncmd)
2228 break;
2229 /*
2230 * Get memory from the pci pool to map the virt space to
2231 * pci bus space for an I/O. The DMA buffer includes the
2232 * number of SGE's necessary to support the sg_tablesize.
2233 */
df2f7729
TM
2234 lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
2235 GFP_KERNEL,
2236 &lpfc_ncmd->dma_handle);
01649561
JS
2237 if (!lpfc_ncmd->data) {
2238 kfree(lpfc_ncmd);
2239 break;
2240 }
01649561
JS
2241
2242 lxri = lpfc_sli4_next_xritag(phba);
2243 if (lxri == NO_XRI) {
771db5c0 2244 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
01649561
JS
2245 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
2246 kfree(lpfc_ncmd);
2247 break;
2248 }
2249 pwqeq = &(lpfc_ncmd->cur_iocbq);
205e8240 2250 wqe = &pwqeq->wqe;
01649561
JS
2251
2252 /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
2253 iotag = lpfc_sli_next_iotag(phba, pwqeq);
2254 if (iotag == 0) {
771db5c0 2255 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
01649561
JS
2256 lpfc_ncmd->data, lpfc_ncmd->dma_handle);
2257 kfree(lpfc_ncmd);
2258 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2259 "6121 Failed to allocated IOTAG for"
2260 " XRI:0x%x\n", lxri);
2261 lpfc_sli4_free_xri(phba, lxri);
2262 break;
2263 }
2264 pwqeq->sli4_lxritag = lxri;
2265 pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
2266 pwqeq->iocb_flag |= LPFC_IO_NVME;
2267 pwqeq->context1 = lpfc_ncmd;
2268 pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
2269
2270 /* Initialize local short-hand pointers. */
2271 lpfc_ncmd->nvme_sgl = lpfc_ncmd->data;
2272 sgl = lpfc_ncmd->nvme_sgl;
2273 pdma_phys_sgl = lpfc_ncmd->dma_handle;
2274 lpfc_ncmd->dma_phys_sgl = pdma_phys_sgl;
2275
2276 /* Rsp SGE will be filled in when we rcv an IO
2277 * from the NVME Layer to be sent.
2278 * The cmd is going to be embedded so we need a SKIP SGE.
2279 */
2280 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2281 bf_set(lpfc_sli4_sge_last, sgl, 0);
2282 sgl->word2 = cpu_to_le32(sgl->word2);
2283 /* Fill in word 3 / sgl_len during cmd submission */
2284
2285 lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
2286
5fd11085
JS
2287 /* Initialize WQE */
2288 memset(wqe, 0, sizeof(union lpfc_wqe));
01649561
JS
2289
2290 /* add the nvme buffer to a post list */
2291 list_add_tail(&lpfc_ncmd->list, &post_nblist);
2292 spin_lock_irq(&phba->nvme_buf_list_get_lock);
2293 phba->sli4_hba.nvme_xri_cnt++;
2294 spin_unlock_irq(&phba->nvme_buf_list_get_lock);
2295 }
2296 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
2297 "6114 Allocate %d out of %d requested new NVME "
2298 "buffers\n", bcnt, num_to_alloc);
2299
2300 /* post the list of nvme buffer sgls to port if available */
2301 if (!list_empty(&post_nblist))
2302 num_posted = lpfc_post_nvme_sgl_list(phba,
2303 &post_nblist, bcnt);
2304 else
2305 num_posted = 0;
2306
2307 return num_posted;
2308}
2309
cf1a1d3e
JS
2310static inline struct lpfc_nvme_buf *
2311lpfc_nvme_buf(struct lpfc_hba *phba)
2312{
2313 struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
2314
2315 list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
2316 &phba->lpfc_nvme_buf_list_get, list) {
2317 list_del_init(&lpfc_ncmd->list);
2318 phba->get_nvme_bufs--;
2319 return lpfc_ncmd;
2320 }
2321 return NULL;
2322}
2323
01649561
JS
2324/**
2325 * lpfc_get_nvme_buf - Get a nvme buffer from lpfc_nvme_buf_list of the HBA
2326 * @phba: The HBA for which this call is being executed.
2327 *
2328 * This routine removes a nvme buffer from head of @phba lpfc_nvme_buf_list list
2329 * and returns to caller.
2330 *
2331 * Return codes:
2332 * NULL - Error
2333 * Pointer to lpfc_nvme_buf - Success
2334 **/
2335static struct lpfc_nvme_buf *
cf1a1d3e
JS
2336lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
2337 int expedite)
01649561 2338{
cf1a1d3e 2339 struct lpfc_nvme_buf *lpfc_ncmd = NULL;
01649561 2340 unsigned long iflag = 0;
01649561
JS
2341
2342 spin_lock_irqsave(&phba->nvme_buf_list_get_lock, iflag);
cf1a1d3e
JS
2343 if (phba->get_nvme_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
2344 lpfc_ncmd = lpfc_nvme_buf(phba);
2345 if (!lpfc_ncmd) {
01649561
JS
2346 spin_lock(&phba->nvme_buf_list_put_lock);
2347 list_splice(&phba->lpfc_nvme_buf_list_put,
2348 &phba->lpfc_nvme_buf_list_get);
cf1a1d3e 2349 phba->get_nvme_bufs += phba->put_nvme_bufs;
01649561 2350 INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
cf1a1d3e 2351 phba->put_nvme_bufs = 0;
01649561 2352 spin_unlock(&phba->nvme_buf_list_put_lock);
cf1a1d3e
JS
2353 if (phba->get_nvme_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
2354 lpfc_ncmd = lpfc_nvme_buf(phba);
01649561
JS
2355 }
2356 spin_unlock_irqrestore(&phba->nvme_buf_list_get_lock, iflag);
2a5b7d62
JS
2357
2358 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_ncmd) {
2359 atomic_inc(&ndlp->cmd_pending);
2360 lpfc_ncmd->flags |= LPFC_BUMP_QDEPTH;
2361 }
01649561
JS
2362 return lpfc_ncmd;
2363}
2364
2365/**
2366 * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
2367 * @phba: The Hba for which this call is being executed.
2368 * @lpfc_ncmd: The nvme buffer which is being released.
2369 *
2370 * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
2371 * lpfc_nvme_buf_list list. For SLI4 XRI's are tied to the nvme buffer
2372 * and cannot be reused for at least RA_TOV amount of time if it was
2373 * aborted.
2374 **/
2375static void
2376lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_nvme_buf *lpfc_ncmd)
2377{
2378 unsigned long iflag = 0;
2379
2a5b7d62
JS
2380 if ((lpfc_ncmd->flags & LPFC_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
2381 atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
2382
01649561 2383 lpfc_ncmd->nonsg_phys = 0;
2a5b7d62
JS
2384 lpfc_ncmd->ndlp = NULL;
2385 lpfc_ncmd->flags &= ~LPFC_BUMP_QDEPTH;
2386
318083ad 2387 if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY) {
86c67379
JS
2388 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2389 "6310 XB release deferred for "
2390 "ox_id x%x on reqtag x%x\n",
2391 lpfc_ncmd->cur_iocbq.sli4_xritag,
2392 lpfc_ncmd->cur_iocbq.iotag);
2393
01649561
JS
2394 spin_lock_irqsave(&phba->sli4_hba.abts_nvme_buf_list_lock,
2395 iflag);
01649561
JS
2396 list_add_tail(&lpfc_ncmd->list,
2397 &phba->sli4_hba.lpfc_abts_nvme_buf_list);
2398 spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock,
2399 iflag);
2400 } else {
2401 lpfc_ncmd->nvmeCmd = NULL;
2402 lpfc_ncmd->cur_iocbq.iocb_flag = LPFC_IO_NVME;
2403 spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
2404 list_add_tail(&lpfc_ncmd->list, &phba->lpfc_nvme_buf_list_put);
cf1a1d3e 2405 phba->put_nvme_bufs++;
01649561
JS
2406 spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
2407 }
2408}
2409
2410/**
2411 * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
2412 * @pvport - the lpfc_vport instance requesting a localport.
2413 *
2414 * This routine is invoked to create an nvme localport instance to bind
2415 * to the nvme_fc_transport. It is called once during driver load
2416 * like lpfc_create_shost after all other services are initialized.
2417 * It requires a vport, vpi, and wwns at call time. Other localport
2418 * parameters are modified as the driver's FCID and the Fabric WWN
2419 * are established.
2420 *
2421 * Return codes
2422 * 0 - successful
2423 * -ENOMEM - no heap memory available
2424 * other values - from nvme registration upcall
2425 **/
2426int
2427lpfc_nvme_create_localport(struct lpfc_vport *vport)
2428{
166d7211 2429 int ret = 0;
01649561
JS
2430 struct lpfc_hba *phba = vport->phba;
2431 struct nvme_fc_port_info nfcp_info;
2432 struct nvme_fc_local_port *localport;
2433 struct lpfc_nvme_lport *lport;
66a210ff
JS
2434 struct lpfc_nvme_ctrl_stat *cstat;
2435 int len, i;
01649561
JS
2436
2437 /* Initialize this localport instance. The vport wwn usage ensures
2438 * that NPIV is accounted for.
2439 */
2440 memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
2441 nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
2442 nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
2443 nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
2444
5b9e70b2
JS
2445 /* We need to tell the transport layer + 1 because it takes page
2446 * alignment into account. When space for the SGL is allocated we
2447 * allocate + 3, one for cmd, one for rsp and one for this alignment
4d4c4a4a 2448 */
4d4c4a4a 2449 lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
01649561
JS
2450 lpfc_nvme_template.max_hw_queues = phba->cfg_nvme_io_channel;
2451
66a210ff
JS
2452 cstat = kmalloc((sizeof(struct lpfc_nvme_ctrl_stat) *
2453 phba->cfg_nvme_io_channel), GFP_KERNEL);
2454 if (!cstat)
2455 return -ENOMEM;
2456
01649561
JS
2457 /* localport is allocated from the stack, but the registration
2458 * call allocates heap memory as well as the private area.
2459 */
7d708033 2460#if (IS_ENABLED(CONFIG_NVME_FC))
01649561
JS
2461 ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
2462 &vport->phba->pcidev->dev, &localport);
166d7211
JS
2463#else
2464 ret = -ENOMEM;
2465#endif
01649561
JS
2466 if (!ret) {
2467 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
2468 "6005 Successfully registered local "
2469 "NVME port num %d, localP %p, private %p, "
2470 "sg_seg %d\n",
2471 localport->port_num, localport,
2472 localport->private,
2473 lpfc_nvme_template.max_sgl_segments);
2474
2475 /* Private is our lport size declared in the template. */
2476 lport = (struct lpfc_nvme_lport *)localport->private;
2477 vport->localport = localport;
2478 lport->vport = vport;
66a210ff 2479 lport->cstat = cstat;
01649561 2480 vport->nvmei_support = 1;
6b486ce9 2481
4b056682
JS
2482 atomic_set(&lport->xmt_fcp_noxri, 0);
2483 atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
2484 atomic_set(&lport->xmt_fcp_qdepth, 0);
44c2757b 2485 atomic_set(&lport->xmt_fcp_err, 0);
4b056682
JS
2486 atomic_set(&lport->xmt_fcp_wqerr, 0);
2487 atomic_set(&lport->xmt_fcp_abort, 0);
2488 atomic_set(&lport->xmt_ls_abort, 0);
2489 atomic_set(&lport->xmt_ls_err, 0);
2490 atomic_set(&lport->cmpl_fcp_xb, 0);
2491 atomic_set(&lport->cmpl_fcp_err, 0);
2492 atomic_set(&lport->cmpl_ls_xb, 0);
2493 atomic_set(&lport->cmpl_ls_err, 0);
66a210ff
JS
2494 atomic_set(&lport->fc4NvmeLsRequests, 0);
2495 atomic_set(&lport->fc4NvmeLsCmpls, 0);
2496
2497 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
2498 cstat = &lport->cstat[i];
2499 atomic_set(&cstat->fc4NvmeInputRequests, 0);
2500 atomic_set(&cstat->fc4NvmeOutputRequests, 0);
2501 atomic_set(&cstat->fc4NvmeControlRequests, 0);
2502 atomic_set(&cstat->fc4NvmeIoCmpls, 0);
2503 }
4b056682 2504
6b486ce9
DK
2505 /* Don't post more new bufs if repost already recovered
2506 * the nvme sgls.
2507 */
2508 if (phba->sli4_hba.nvme_xri_cnt == 0) {
2509 len = lpfc_new_nvme_buf(vport,
2510 phba->sli4_hba.nvme_xri_max);
2511 vport->phba->total_nvme_bufs += len;
2512 }
66a210ff
JS
2513 } else {
2514 kfree(cstat);
01649561
JS
2515 }
2516
01649561
JS
2517 return ret;
2518}
2519
add9d6be
JS
2520/* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
2521 *
2522 * The driver has to wait for the host nvme transport to callback
2523 * indicating the localport has successfully unregistered all
2524 * resources. Since this is an uninterruptible wait, loop every ten
2525 * seconds and print a message indicating no progress.
2526 *
2527 * An uninterruptible wait is used because of the risk of transport-to-
2528 * driver state mismatch.
2529 */
2530void
2531lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
2532 struct lpfc_nvme_lport *lport)
2533{
2534#if (IS_ENABLED(CONFIG_NVME_FC))
2535 u32 wait_tmo;
2536 int ret;
2537
2538 /* Host transport has to clean up and confirm requiring an indefinite
2539 * wait. Print a message if a 10 second wait expires and renew the
2540 * wait. This is unexpected.
2541 */
2542 wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
2543 while (true) {
2544 ret = wait_for_completion_timeout(&lport->lport_unreg_done,
2545 wait_tmo);
2546 if (unlikely(!ret)) {
2547 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
2548 "6176 Lport %p Localport %p wait "
2549 "timed out. Renewing.\n",
2550 lport, vport->localport);
2551 continue;
2552 }
2553 break;
2554 }
2555 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
2556 "6177 Lport %p Localport %p Complete Success\n",
2557 lport, vport->localport);
2558#endif
2559}
2560
01649561
JS
2561/**
2562 * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
2563 * @pnvme: pointer to lpfc nvme data structure.
2564 *
2565 * This routine is invoked to destroy all lports bound to the phba.
2566 * The lport memory was allocated by the nvme fc transport and is
2567 * released there. This routine ensures all rports bound to the
2568 * lport have been disconnected.
2569 *
2570 **/
2571void
2572lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
2573{
7d708033 2574#if (IS_ENABLED(CONFIG_NVME_FC))
01649561
JS
2575 struct nvme_fc_local_port *localport;
2576 struct lpfc_nvme_lport *lport;
66a210ff 2577 struct lpfc_nvme_ctrl_stat *cstat;
01649561
JS
2578 int ret;
2579
2580 if (vport->nvmei_support == 0)
2581 return;
2582
2583 localport = vport->localport;
2584 vport->localport = NULL;
2585 lport = (struct lpfc_nvme_lport *)localport->private;
66a210ff 2586 cstat = lport->cstat;
01649561
JS
2587
2588 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2589 "6011 Destroying NVME localport %p\n",
2590 localport);
166d7211 2591
01649561
JS
2592 /* lport's rport list is clear. Unregister
2593 * lport and release resources.
2594 */
2595 init_completion(&lport->lport_unreg_done);
2596 ret = nvme_fc_unregister_localport(localport);
add9d6be
JS
2597
2598 /* Wait for completion. This either blocks
2599 * indefinitely or succeeds
2600 */
2601 lpfc_nvme_lport_unreg_wait(vport, lport);
66a210ff 2602 kfree(cstat);
01649561
JS
2603
2604 /* Regardless of the unregister upcall response, clear
2605 * nvmei_support. All rports are unregistered and the
2606 * driver will clean up.
2607 */
2608 vport->nvmei_support = 0;
2609 if (ret == 0) {
2610 lpfc_printf_vlog(vport,
2611 KERN_INFO, LOG_NVME_DISC,
2612 "6009 Unregistered lport Success\n");
2613 } else {
2614 lpfc_printf_vlog(vport,
2615 KERN_INFO, LOG_NVME_DISC,
2616 "6010 Unregistered lport "
2617 "Failed, status x%x\n",
2618 ret);
2619 }
166d7211 2620#endif
01649561
JS
2621}
2622
2623void
2624lpfc_nvme_update_localport(struct lpfc_vport *vport)
2625{
4410a67a 2626#if (IS_ENABLED(CONFIG_NVME_FC))
01649561
JS
2627 struct nvme_fc_local_port *localport;
2628 struct lpfc_nvme_lport *lport;
2629
2630 localport = vport->localport;
4410a67a
JS
2631 if (!localport) {
2632 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
2633 "6710 Update NVME fail. No localport\n");
2634 return;
2635 }
01649561 2636 lport = (struct lpfc_nvme_lport *)localport->private;
4410a67a
JS
2637 if (!lport) {
2638 lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
2639 "6171 Update NVME fail. localP %p, No lport\n",
2640 localport);
2641 return;
2642 }
01649561
JS
2643 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
2644 "6012 Update NVME lport %p did x%x\n",
2645 localport, vport->fc_myDID);
2646
2647 localport->port_id = vport->fc_myDID;
2648 if (localport->port_id == 0)
2649 localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
2650 else
2651 localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
2652
2653 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2654 "6030 bound lport %p to DID x%06x\n",
2655 lport, localport->port_id);
4410a67a 2656#endif
01649561
JS
2657}
2658
2659int
2660lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2661{
7d708033 2662#if (IS_ENABLED(CONFIG_NVME_FC))
01649561
JS
2663 int ret = 0;
2664 struct nvme_fc_local_port *localport;
2665 struct lpfc_nvme_lport *lport;
2666 struct lpfc_nvme_rport *rport;
01466024 2667 struct lpfc_nvme_rport *oldrport;
01649561
JS
2668 struct nvme_fc_remote_port *remote_port;
2669 struct nvme_fc_port_info rpinfo;
93a3922d 2670 struct lpfc_nodelist *prev_ndlp = NULL;
01649561
JS
2671
2672 lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
2673 "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
2674 ndlp->nlp_DID, ndlp->nlp_type);
2675
2676 localport = vport->localport;
bb6a8a2c
DK
2677 if (!localport)
2678 return 0;
2679
01649561
JS
2680 lport = (struct lpfc_nvme_lport *)localport->private;
2681
7a06dcd3
JS
2682 /* NVME rports are not preserved across devloss.
2683 * Just register this instance. Note, rpinfo->dev_loss_tmo
2684 * is left 0 to indicate accept transport defaults. The
2685 * driver communicates port role capabilities consistent
2686 * with the PRLI response data.
2687 */
2688 memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
2689 rpinfo.port_id = ndlp->nlp_DID;
2690 if (ndlp->nlp_type & NLP_NVME_TARGET)
2691 rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
2692 if (ndlp->nlp_type & NLP_NVME_INITIATOR)
2693 rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
2694
2695 if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
2696 rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
2697
2698 rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
2699 rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
01466024 2700
9e210178 2701 spin_lock_irq(&vport->phba->hbalock);
01466024 2702 oldrport = lpfc_ndlp_get_nrport(ndlp);
9e210178 2703 spin_unlock_irq(&vport->phba->hbalock);
01466024 2704 if (!oldrport)
3fd78355
JS
2705 lpfc_nlp_get(ndlp);
2706
7a06dcd3
JS
2707 ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
2708 if (!ret) {
2709 /* If the ndlp already has an nrport, this is just
2710 * a resume of the existing rport. Else this is a
2711 * new rport.
01649561 2712 */
b15bd3e6
JS
2713 /* Guard against an unregister/reregister
2714 * race that leaves the WAIT flag set.
2715 */
2716 spin_lock_irq(&vport->phba->hbalock);
2717 ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
2718 spin_unlock_irq(&vport->phba->hbalock);
7a06dcd3 2719 rport = remote_port->private;
01466024 2720 if (oldrport) {
93a3922d
JS
2721 /* New remoteport record does not guarantee valid
2722 * host private memory area.
2723 */
2724 prev_ndlp = oldrport->ndlp;
01466024 2725 if (oldrport == remote_port->private) {
93a3922d
JS
2726 /* Same remoteport - ndlp should match.
2727 * Just reuse.
2728 */
3fd78355
JS
2729 lpfc_printf_vlog(ndlp->vport, KERN_INFO,
2730 LOG_NVME_DISC,
2731 "6014 Rebinding lport to "
2732 "remoteport %p wwpn 0x%llx, "
93a3922d 2733 "Data: x%x x%x %p %p x%x x%06x\n",
3fd78355
JS
2734 remote_port,
2735 remote_port->port_name,
2736 remote_port->port_id,
2737 remote_port->port_role,
93a3922d 2738 prev_ndlp,
3fd78355
JS
2739 ndlp,
2740 ndlp->nlp_type,
2741 ndlp->nlp_DID);
2742 return 0;
2743 }
2b75d0f9 2744
3fd78355
JS
2745 /* Sever the ndlp<->rport association
2746 * before dropping the ndlp ref from
2747 * register.
2b75d0f9 2748 */
3fd78355 2749 spin_lock_irq(&vport->phba->hbalock);
2b75d0f9 2750 ndlp->nrport = NULL;
01466024 2751 ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
3fd78355 2752 spin_unlock_irq(&vport->phba->hbalock);
2b75d0f9 2753 rport->ndlp = NULL;
3fd78355 2754 rport->remoteport = NULL;
b04744ce
JS
2755
2756 /* Reference only removed if previous NDLP is no longer
2757 * active. It might be just a swap and removing the
2758 * reference would cause a premature cleanup.
2759 */
2760 if (prev_ndlp && prev_ndlp != ndlp) {
2761 if ((!NLP_CHK_NODE_ACT(prev_ndlp)) ||
2762 (!prev_ndlp->nrport))
2763 lpfc_nlp_put(prev_ndlp);
2764 }
01649561 2765 }
2b75d0f9
DK
2766
2767 /* Clean bind the rport to the ndlp. */
2768 rport->remoteport = remote_port;
2769 rport->lport = lport;
3fd78355
JS
2770 rport->ndlp = ndlp;
2771 spin_lock_irq(&vport->phba->hbalock);
2b75d0f9 2772 ndlp->nrport = rport;
3fd78355 2773 spin_unlock_irq(&vport->phba->hbalock);
2b75d0f9
DK
2774 lpfc_printf_vlog(vport, KERN_INFO,
2775 LOG_NVME_DISC | LOG_NODE,
2776 "6022 Binding new rport to "
93a3922d 2777 "lport %p Remoteport %p rport %p WWNN 0x%llx, "
2b75d0f9 2778 "Rport WWPN 0x%llx DID "
93a3922d
JS
2779 "x%06x Role x%x, ndlp %p prev_ndlp %p\n",
2780 lport, remote_port, rport,
2b75d0f9 2781 rpinfo.node_name, rpinfo.port_name,
3fd78355 2782 rpinfo.port_id, rpinfo.port_role,
93a3922d 2783 ndlp, prev_ndlp);
01649561 2784 } else {
7a06dcd3
JS
2785 lpfc_printf_vlog(vport, KERN_ERR,
2786 LOG_NVME_DISC | LOG_NODE,
2787 "6031 RemotePort Registration failed "
2788 "err: %d, DID x%06x\n",
2789 ret, ndlp->nlp_DID);
01649561 2790 }
7a06dcd3 2791
01649561 2792 return ret;
166d7211
JS
2793#else
2794 return 0;
2795#endif
01649561
JS
2796}
2797
2798/* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
2799 *
2800 * There is no notion of Devloss or rport recovery from the current
2801 * nvme_transport perspective. Loss of an rport just means IO cannot
2802 * be sent and recovery is completely up to the initator.
2803 * For now, the driver just unbinds the DID and port_role so that
2804 * no further IO can be issued. Changes are planned for later.
2805 *
2806 * Notes - the ndlp reference count is not decremented here since
2807 * since there is no nvme_transport api for devloss. Node ref count
2808 * is only adjusted in driver unload.
2809 */
2810void
2811lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
2812{
7d708033 2813#if (IS_ENABLED(CONFIG_NVME_FC))
01649561
JS
2814 int ret;
2815 struct nvme_fc_local_port *localport;
2816 struct lpfc_nvme_lport *lport;
2817 struct lpfc_nvme_rport *rport;
9e210178 2818 struct nvme_fc_remote_port *remoteport = NULL;
01649561
JS
2819
2820 localport = vport->localport;
2821
2822 /* This is fundamental error. The localport is always
2823 * available until driver unload. Just exit.
2824 */
2825 if (!localport)
2826 return;
2827
2828 lport = (struct lpfc_nvme_lport *)localport->private;
2829 if (!lport)
2830 goto input_err;
2831
9e210178 2832 spin_lock_irq(&vport->phba->hbalock);
01466024 2833 rport = lpfc_ndlp_get_nrport(ndlp);
9e210178
JS
2834 if (rport)
2835 remoteport = rport->remoteport;
2836 spin_unlock_irq(&vport->phba->hbalock);
2837 if (!remoteport)
01649561
JS
2838 goto input_err;
2839
01649561
JS
2840 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
2841 "6033 Unreg nvme remoteport %p, portname x%llx, "
2842 "port_id x%06x, portstate x%x port type x%x\n",
2843 remoteport, remoteport->port_name,
2844 remoteport->port_id, remoteport->port_state,
2845 ndlp->nlp_type);
2846
2847 /* Sanity check ndlp type. Only call for NVME ports. Don't
2848 * clear any rport state until the transport calls back.
2849 */
3b5bde69
JS
2850
2851 if (ndlp->nlp_type & NLP_NVME_TARGET) {
7a06dcd3
JS
2852 /* No concern about the role change on the nvme remoteport.
2853 * The transport will update it.
2854 */
add9d6be 2855 ndlp->upcall_flags |= NLP_WAIT_FOR_UNREG;
7438273f
JS
2856
2857 /* Don't let the host nvme transport keep sending keep-alives
2858 * on this remoteport. Vport is unloading, no recovery. The
2859 * return values is ignored. The upcall is a courtesy to the
2860 * transport.
2861 */
2862 if (vport->load_flag & FC_UNLOADING)
2863 (void)nvme_fc_set_remoteport_devloss(remoteport, 0);
2864
01649561 2865 ret = nvme_fc_unregister_remoteport(remoteport);
3fd78355
JS
2866 if (ret != 0) {
2867 lpfc_nlp_put(ndlp);
01649561
JS
2868 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2869 "6167 NVME unregister failed %d "
2870 "port_state x%x\n",
2871 ret, remoteport->port_state);
3fd78355 2872 }
01649561
JS
2873 }
2874 return;
2875
2876 input_err:
166d7211 2877#endif
01649561 2878 lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
2b7824d0 2879 "6168 State error: lport %p, rport%p FCID x%06x\n",
01649561
JS
2880 vport->localport, ndlp->rport, ndlp->nlp_DID);
2881}
318083ad
JS
2882
2883/**
2884 * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
2885 * @phba: pointer to lpfc hba data structure.
2886 * @axri: pointer to the fcp xri abort wcqe structure.
2887 *
2888 * This routine is invoked by the worker thread to process a SLI4 fast-path
952c303b
DK
2889 * NVME aborted xri. Aborted NVME IO commands are completed to the transport
2890 * here.
318083ad
JS
2891 **/
2892void
2893lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
2894 struct sli4_wcqe_xri_aborted *axri)
2895{
2896 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
318083ad 2897 struct lpfc_nvme_buf *lpfc_ncmd, *next_lpfc_ncmd;
952c303b 2898 struct nvmefc_fcp_req *nvme_cmd = NULL;
318083ad
JS
2899 struct lpfc_nodelist *ndlp;
2900 unsigned long iflag = 0;
318083ad
JS
2901
2902 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
2903 return;
2904 spin_lock_irqsave(&phba->hbalock, iflag);
2905 spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2906 list_for_each_entry_safe(lpfc_ncmd, next_lpfc_ncmd,
2907 &phba->sli4_hba.lpfc_abts_nvme_buf_list,
2908 list) {
2909 if (lpfc_ncmd->cur_iocbq.sli4_xritag == xri) {
bbe3012b 2910 list_del_init(&lpfc_ncmd->list);
318083ad
JS
2911 lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
2912 lpfc_ncmd->status = IOSTAT_SUCCESS;
2913 spin_unlock(
2914 &phba->sli4_hba.abts_nvme_buf_list_lock);
2915
318083ad
JS
2916 spin_unlock_irqrestore(&phba->hbalock, iflag);
2917 ndlp = lpfc_ncmd->ndlp;
952c303b 2918 if (ndlp)
318083ad 2919 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
86c67379
JS
2920
2921 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
952c303b
DK
2922 "6311 nvme_cmd %p xri x%x tag x%x "
2923 "abort complete and xri released\n",
2924 lpfc_ncmd->nvmeCmd, xri,
2925 lpfc_ncmd->cur_iocbq.iotag);
2926
2927 /* Aborted NVME commands are required to not complete
2928 * before the abort exchange command fully completes.
2929 * Once completed, it is available via the put list.
2930 */
3fd78355
JS
2931 if (lpfc_ncmd->nvmeCmd) {
2932 nvme_cmd = lpfc_ncmd->nvmeCmd;
2933 nvme_cmd->done(nvme_cmd);
2934 lpfc_ncmd->nvmeCmd = NULL;
2935 }
318083ad 2936 lpfc_release_nvme_buf(phba, lpfc_ncmd);
318083ad
JS
2937 return;
2938 }
2939 }
2940 spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
2941 spin_unlock_irqrestore(&phba->hbalock, iflag);
86c67379
JS
2942
2943 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2944 "6312 XRI Aborted xri x%x not found\n", xri);
2945
318083ad 2946}
c3725bdc
JS
2947
2948/**
2949 * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
2950 * @phba: Pointer to HBA context object.
2951 *
2952 * This function flushes all wqes in the nvme rings and frees all resources
2953 * in the txcmplq. This function does not issue abort wqes for the IO
2954 * commands in txcmplq, they will just be returned with
2955 * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
2956 * slot has been permanently disabled.
2957 **/
2958void
2959lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
2960{
2961 struct lpfc_sli_ring *pring;
2962 u32 i, wait_cnt = 0;
2963
d580c613 2964 if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.nvme_wq)
c3725bdc
JS
2965 return;
2966
2967 /* Cycle through all NVME rings and make sure all outstanding
2968 * WQEs have been removed from the txcmplqs.
2969 */
2970 for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
2971 pring = phba->sli4_hba.nvme_wq[i]->pring;
2972
d580c613
JS
2973 if (!pring)
2974 continue;
2975
c3725bdc
JS
2976 /* Retrieve everything on the txcmplq */
2977 while (!list_empty(&pring->txcmplq)) {
2978 msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
2979 wait_cnt++;
2980
2981 /* The sleep is 10mS. Every ten seconds,
2982 * dump a message. Something is wrong.
2983 */
2984 if ((wait_cnt % 1000) == 0) {
2985 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2986 "6178 NVME IO not empty, "
2987 "cnt %d\n", wait_cnt);
2988 }
2989 }
2990 }
2991}