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