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