xref: /openbmc/linux/drivers/scsi/lpfc/lpfc_nvmet.c (revision d3964221)
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channsel Host Bus Adapters.                               *
4  * Copyright (C) 2017 Broadcom. All Rights Reserved. The term      *
5  * “Broadcom” refers to Broadcom Limited 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 <../drivers/nvme/host/nvme.h>
40 #include <linux/nvme-fc-driver.h>
41 
42 #include "lpfc_version.h"
43 #include "lpfc_hw4.h"
44 #include "lpfc_hw.h"
45 #include "lpfc_sli.h"
46 #include "lpfc_sli4.h"
47 #include "lpfc_nl.h"
48 #include "lpfc_disc.h"
49 #include "lpfc.h"
50 #include "lpfc_scsi.h"
51 #include "lpfc_nvme.h"
52 #include "lpfc_nvmet.h"
53 #include "lpfc_logmsg.h"
54 #include "lpfc_crtn.h"
55 #include "lpfc_vport.h"
56 #include "lpfc_debugfs.h"
57 
58 static struct lpfc_iocbq *lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *,
59 						 struct lpfc_nvmet_rcv_ctx *,
60 						 dma_addr_t rspbuf,
61 						 uint16_t rspsize);
62 static struct lpfc_iocbq *lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *,
63 						  struct lpfc_nvmet_rcv_ctx *);
64 static int lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *,
65 					  struct lpfc_nvmet_rcv_ctx *,
66 					  uint32_t, uint16_t);
67 static int lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *,
68 					    struct lpfc_nvmet_rcv_ctx *,
69 					    uint32_t, uint16_t);
70 static int lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *,
71 					   struct lpfc_nvmet_rcv_ctx *,
72 					   uint32_t, uint16_t);
73 
74 void
75 lpfc_nvmet_defer_release(struct lpfc_hba *phba, struct lpfc_nvmet_rcv_ctx *ctxp)
76 {
77 	unsigned long iflag;
78 
79 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
80 			"6313 NVMET Defer ctx release xri x%x flg x%x\n",
81 			ctxp->oxid, ctxp->flag);
82 
83 	spin_lock_irqsave(&phba->sli4_hba.abts_nvme_buf_list_lock, iflag);
84 	if (ctxp->flag & LPFC_NVMET_CTX_RLS) {
85 		spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock,
86 				       iflag);
87 		return;
88 	}
89 	ctxp->flag |= LPFC_NVMET_CTX_RLS;
90 	list_add_tail(&ctxp->list, &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
91 	spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock, iflag);
92 }
93 
94 /**
95  * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
96  * @phba: Pointer to HBA context object.
97  * @cmdwqe: Pointer to driver command WQE object.
98  * @wcqe: Pointer to driver response CQE object.
99  *
100  * The function is called from SLI ring event handler with no
101  * lock held. This function is the completion handler for NVME LS commands
102  * The function frees memory resources used for the NVME commands.
103  **/
104 static void
105 lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
106 			  struct lpfc_wcqe_complete *wcqe)
107 {
108 	struct lpfc_nvmet_tgtport *tgtp;
109 	struct nvmefc_tgt_ls_req *rsp;
110 	struct lpfc_nvmet_rcv_ctx *ctxp;
111 	uint32_t status, result;
112 
113 	status = bf_get(lpfc_wcqe_c_status, wcqe);
114 	result = wcqe->parameter;
115 	ctxp = cmdwqe->context2;
116 
117 	if (ctxp->state != LPFC_NVMET_STE_LS_RSP || ctxp->entry_cnt != 2) {
118 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
119 				"6410 NVMET LS cmpl state mismatch IO x%x: "
120 				"%d %d\n",
121 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
122 	}
123 
124 	if (!phba->targetport)
125 		goto out;
126 
127 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
128 
129 	if (status)
130 		atomic_inc(&tgtp->xmt_ls_rsp_error);
131 	else
132 		atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
133 
134 out:
135 	rsp = &ctxp->ctx.ls_req;
136 
137 	lpfc_nvmeio_data(phba, "NVMET LS  CMPL: xri x%x stat x%x result x%x\n",
138 			 ctxp->oxid, status, result);
139 
140 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
141 			"6038 NVMET LS rsp cmpl: %d %d oxid x%x\n",
142 			status, result, ctxp->oxid);
143 
144 	lpfc_nlp_put(cmdwqe->context1);
145 	cmdwqe->context2 = NULL;
146 	cmdwqe->context3 = NULL;
147 	lpfc_sli_release_iocbq(phba, cmdwqe);
148 	rsp->done(rsp);
149 	kfree(ctxp);
150 }
151 
152 /**
153  * lpfc_nvmet_ctxbuf_post - Repost a NVMET RQ DMA buffer and clean up context
154  * @phba: HBA buffer is associated with
155  * @ctxp: context to clean up
156  * @mp: Buffer to free
157  *
158  * Description: Frees the given DMA buffer in the appropriate way given by
159  * reposting it to its associated RQ so it can be reused.
160  *
161  * Notes: Takes phba->hbalock.  Can be called with or without other locks held.
162  *
163  * Returns: None
164  **/
165 void
166 lpfc_nvmet_ctxbuf_post(struct lpfc_hba *phba, struct lpfc_nvmet_ctxbuf *ctx_buf)
167 {
168 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
169 	struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
170 	struct lpfc_nvmet_tgtport *tgtp;
171 	struct fc_frame_header *fc_hdr;
172 	struct rqb_dmabuf *nvmebuf;
173 	struct lpfc_nvmet_ctx_info *infop;
174 	uint32_t *payload;
175 	uint32_t size, oxid, sid, rc;
176 	int cpu;
177 	unsigned long iflag;
178 
179 	if (ctxp->txrdy) {
180 		dma_pool_free(phba->txrdy_payload_pool, ctxp->txrdy,
181 			      ctxp->txrdy_phys);
182 		ctxp->txrdy = NULL;
183 		ctxp->txrdy_phys = 0;
184 	}
185 
186 	if (ctxp->state == LPFC_NVMET_STE_FREE) {
187 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
188 				"6411 NVMET free, already free IO x%x: %d %d\n",
189 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
190 	}
191 	ctxp->state = LPFC_NVMET_STE_FREE;
192 
193 	spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
194 	if (phba->sli4_hba.nvmet_io_wait_cnt) {
195 		list_remove_head(&phba->sli4_hba.lpfc_nvmet_io_wait_list,
196 				 nvmebuf, struct rqb_dmabuf,
197 				 hbuf.list);
198 		phba->sli4_hba.nvmet_io_wait_cnt--;
199 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
200 				       iflag);
201 
202 		fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
203 		oxid = be16_to_cpu(fc_hdr->fh_ox_id);
204 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
205 		payload = (uint32_t *)(nvmebuf->dbuf.virt);
206 		size = nvmebuf->bytes_recv;
207 		sid = sli4_sid_from_fc_hdr(fc_hdr);
208 
209 		ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
210 		ctxp->wqeq = NULL;
211 		ctxp->txrdy = NULL;
212 		ctxp->offset = 0;
213 		ctxp->phba = phba;
214 		ctxp->size = size;
215 		ctxp->oxid = oxid;
216 		ctxp->sid = sid;
217 		ctxp->state = LPFC_NVMET_STE_RCV;
218 		ctxp->entry_cnt = 1;
219 		ctxp->flag = 0;
220 		ctxp->ctxbuf = ctx_buf;
221 		spin_lock_init(&ctxp->ctxlock);
222 
223 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
224 		if (phba->ktime_on) {
225 			ctxp->ts_cmd_nvme = ktime_get_ns();
226 			ctxp->ts_isr_cmd = ctxp->ts_cmd_nvme;
227 			ctxp->ts_nvme_data = 0;
228 			ctxp->ts_data_wqput = 0;
229 			ctxp->ts_isr_data = 0;
230 			ctxp->ts_data_nvme = 0;
231 			ctxp->ts_nvme_status = 0;
232 			ctxp->ts_status_wqput = 0;
233 			ctxp->ts_isr_status = 0;
234 			ctxp->ts_status_nvme = 0;
235 		}
236 #endif
237 		atomic_inc(&tgtp->rcv_fcp_cmd_in);
238 		/*
239 		 * The calling sequence should be:
240 		 * nvmet_fc_rcv_fcp_req->lpfc_nvmet_xmt_fcp_op/cmp- req->done
241 		 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
242 		 * When we return from nvmet_fc_rcv_fcp_req, all relevant info
243 		 * the NVME command / FC header is stored.
244 		 * A buffer has already been reposted for this IO, so just free
245 		 * the nvmebuf.
246 		 */
247 		rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->ctx.fcp_req,
248 					  payload, size);
249 
250 		/* Process FCP command */
251 		if (rc == 0) {
252 			atomic_inc(&tgtp->rcv_fcp_cmd_out);
253 			nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
254 			return;
255 		}
256 
257 		atomic_inc(&tgtp->rcv_fcp_cmd_drop);
258 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
259 				"2582 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
260 				ctxp->oxid, rc,
261 				atomic_read(&tgtp->rcv_fcp_cmd_in),
262 				atomic_read(&tgtp->rcv_fcp_cmd_out),
263 				atomic_read(&tgtp->xmt_fcp_release));
264 
265 		lpfc_nvmet_defer_release(phba, ctxp);
266 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
267 		nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
268 		return;
269 	}
270 	spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
271 
272 	/*
273 	 * Use the CPU context list, from the MRQ the IO was received on
274 	 * (ctxp->idx), to save context structure.
275 	 */
276 	cpu = smp_processor_id();
277 	infop = lpfc_get_ctx_list(phba, cpu, ctxp->idx);
278 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, iflag);
279 	list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
280 	infop->nvmet_ctx_list_cnt++;
281 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, iflag);
282 #endif
283 }
284 
285 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
286 static void
287 lpfc_nvmet_ktime(struct lpfc_hba *phba,
288 		 struct lpfc_nvmet_rcv_ctx *ctxp)
289 {
290 	uint64_t seg1, seg2, seg3, seg4, seg5;
291 	uint64_t seg6, seg7, seg8, seg9, seg10;
292 
293 	if (!phba->ktime_on)
294 		return;
295 
296 	if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
297 	    !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
298 	    !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
299 	    !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
300 	    !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
301 		return;
302 
303 	if (ctxp->ts_isr_cmd  > ctxp->ts_cmd_nvme)
304 		return;
305 	if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
306 		return;
307 	if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
308 		return;
309 	if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
310 		return;
311 	if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
312 		return;
313 	if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
314 		return;
315 	if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
316 		return;
317 	if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
318 		return;
319 	if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
320 		return;
321 	/*
322 	 * Segment 1 - Time from FCP command received by MSI-X ISR
323 	 * to FCP command is passed to NVME Layer.
324 	 * Segment 2 - Time from FCP command payload handed
325 	 * off to NVME Layer to Driver receives a Command op
326 	 * from NVME Layer.
327 	 * Segment 3 - Time from Driver receives a Command op
328 	 * from NVME Layer to Command is put on WQ.
329 	 * Segment 4 - Time from Driver WQ put is done
330 	 * to MSI-X ISR for Command cmpl.
331 	 * Segment 5 - Time from MSI-X ISR for Command cmpl to
332 	 * Command cmpl is passed to NVME Layer.
333 	 * Segment 6 - Time from Command cmpl is passed to NVME
334 	 * Layer to Driver receives a RSP op from NVME Layer.
335 	 * Segment 7 - Time from Driver receives a RSP op from
336 	 * NVME Layer to WQ put is done on TRSP FCP Status.
337 	 * Segment 8 - Time from Driver WQ put is done on TRSP
338 	 * FCP Status to MSI-X ISR for TRSP cmpl.
339 	 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
340 	 * TRSP cmpl is passed to NVME Layer.
341 	 * Segment 10 - Time from FCP command received by
342 	 * MSI-X ISR to command is completed on wire.
343 	 * (Segments 1 thru 8) for READDATA / WRITEDATA
344 	 * (Segments 1 thru 4) for READDATA_RSP
345 	 */
346 	seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
347 	seg2 = (ctxp->ts_nvme_data - ctxp->ts_isr_cmd) - seg1;
348 	seg3 = (ctxp->ts_data_wqput - ctxp->ts_isr_cmd) -
349 		seg1 - seg2;
350 	seg4 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd) -
351 		seg1 - seg2 - seg3;
352 	seg5 = (ctxp->ts_data_nvme - ctxp->ts_isr_cmd) -
353 		seg1 - seg2 - seg3 - seg4;
354 
355 	/* For auto rsp commands seg6 thru seg10 will be 0 */
356 	if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
357 		seg6 = (ctxp->ts_nvme_status -
358 			ctxp->ts_isr_cmd) -
359 			seg1 - seg2 - seg3 - seg4 - seg5;
360 		seg7 = (ctxp->ts_status_wqput -
361 			ctxp->ts_isr_cmd) -
362 			seg1 - seg2 - seg3 -
363 			seg4 - seg5 - seg6;
364 		seg8 = (ctxp->ts_isr_status -
365 			ctxp->ts_isr_cmd) -
366 			seg1 - seg2 - seg3 - seg4 -
367 			seg5 - seg6 - seg7;
368 		seg9 = (ctxp->ts_status_nvme -
369 			ctxp->ts_isr_cmd) -
370 			seg1 - seg2 - seg3 - seg4 -
371 			seg5 - seg6 - seg7 - seg8;
372 		seg10 = (ctxp->ts_isr_status -
373 			ctxp->ts_isr_cmd);
374 	} else {
375 		seg6 =  0;
376 		seg7 =  0;
377 		seg8 =  0;
378 		seg9 =  0;
379 		seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
380 	}
381 
382 	phba->ktime_seg1_total += seg1;
383 	if (seg1 < phba->ktime_seg1_min)
384 		phba->ktime_seg1_min = seg1;
385 	else if (seg1 > phba->ktime_seg1_max)
386 		phba->ktime_seg1_max = seg1;
387 
388 	phba->ktime_seg2_total += seg2;
389 	if (seg2 < phba->ktime_seg2_min)
390 		phba->ktime_seg2_min = seg2;
391 	else if (seg2 > phba->ktime_seg2_max)
392 		phba->ktime_seg2_max = seg2;
393 
394 	phba->ktime_seg3_total += seg3;
395 	if (seg3 < phba->ktime_seg3_min)
396 		phba->ktime_seg3_min = seg3;
397 	else if (seg3 > phba->ktime_seg3_max)
398 		phba->ktime_seg3_max = seg3;
399 
400 	phba->ktime_seg4_total += seg4;
401 	if (seg4 < phba->ktime_seg4_min)
402 		phba->ktime_seg4_min = seg4;
403 	else if (seg4 > phba->ktime_seg4_max)
404 		phba->ktime_seg4_max = seg4;
405 
406 	phba->ktime_seg5_total += seg5;
407 	if (seg5 < phba->ktime_seg5_min)
408 		phba->ktime_seg5_min = seg5;
409 	else if (seg5 > phba->ktime_seg5_max)
410 		phba->ktime_seg5_max = seg5;
411 
412 	phba->ktime_data_samples++;
413 	if (!seg6)
414 		goto out;
415 
416 	phba->ktime_seg6_total += seg6;
417 	if (seg6 < phba->ktime_seg6_min)
418 		phba->ktime_seg6_min = seg6;
419 	else if (seg6 > phba->ktime_seg6_max)
420 		phba->ktime_seg6_max = seg6;
421 
422 	phba->ktime_seg7_total += seg7;
423 	if (seg7 < phba->ktime_seg7_min)
424 		phba->ktime_seg7_min = seg7;
425 	else if (seg7 > phba->ktime_seg7_max)
426 		phba->ktime_seg7_max = seg7;
427 
428 	phba->ktime_seg8_total += seg8;
429 	if (seg8 < phba->ktime_seg8_min)
430 		phba->ktime_seg8_min = seg8;
431 	else if (seg8 > phba->ktime_seg8_max)
432 		phba->ktime_seg8_max = seg8;
433 
434 	phba->ktime_seg9_total += seg9;
435 	if (seg9 < phba->ktime_seg9_min)
436 		phba->ktime_seg9_min = seg9;
437 	else if (seg9 > phba->ktime_seg9_max)
438 		phba->ktime_seg9_max = seg9;
439 out:
440 	phba->ktime_seg10_total += seg10;
441 	if (seg10 < phba->ktime_seg10_min)
442 		phba->ktime_seg10_min = seg10;
443 	else if (seg10 > phba->ktime_seg10_max)
444 		phba->ktime_seg10_max = seg10;
445 	phba->ktime_status_samples++;
446 }
447 #endif
448 
449 /**
450  * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
451  * @phba: Pointer to HBA context object.
452  * @cmdwqe: Pointer to driver command WQE object.
453  * @wcqe: Pointer to driver response CQE object.
454  *
455  * The function is called from SLI ring event handler with no
456  * lock held. This function is the completion handler for NVME FCP commands
457  * The function frees memory resources used for the NVME commands.
458  **/
459 static void
460 lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
461 			  struct lpfc_wcqe_complete *wcqe)
462 {
463 	struct lpfc_nvmet_tgtport *tgtp;
464 	struct nvmefc_tgt_fcp_req *rsp;
465 	struct lpfc_nvmet_rcv_ctx *ctxp;
466 	uint32_t status, result, op, start_clean;
467 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
468 	uint32_t id;
469 #endif
470 
471 	ctxp = cmdwqe->context2;
472 	ctxp->flag &= ~LPFC_NVMET_IO_INP;
473 
474 	rsp = &ctxp->ctx.fcp_req;
475 	op = rsp->op;
476 
477 	status = bf_get(lpfc_wcqe_c_status, wcqe);
478 	result = wcqe->parameter;
479 
480 	if (phba->targetport)
481 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
482 	else
483 		tgtp = NULL;
484 
485 	lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
486 			 ctxp->oxid, op, status);
487 
488 	if (status) {
489 		rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
490 		rsp->transferred_length = 0;
491 		if (tgtp)
492 			atomic_inc(&tgtp->xmt_fcp_rsp_error);
493 
494 		/* pick up SLI4 exhange busy condition */
495 		if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
496 			ctxp->flag |= LPFC_NVMET_XBUSY;
497 
498 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
499 					"6315 IO Cmpl XBUSY: xri x%x: %x/%x\n",
500 					ctxp->oxid, status, result);
501 		} else {
502 			ctxp->flag &= ~LPFC_NVMET_XBUSY;
503 		}
504 
505 	} else {
506 		rsp->fcp_error = NVME_SC_SUCCESS;
507 		if (op == NVMET_FCOP_RSP)
508 			rsp->transferred_length = rsp->rsplen;
509 		else
510 			rsp->transferred_length = rsp->transfer_length;
511 		if (tgtp)
512 			atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
513 	}
514 
515 	if ((op == NVMET_FCOP_READDATA_RSP) ||
516 	    (op == NVMET_FCOP_RSP)) {
517 		/* Sanity check */
518 		ctxp->state = LPFC_NVMET_STE_DONE;
519 		ctxp->entry_cnt++;
520 
521 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
522 		if (phba->ktime_on) {
523 			if (rsp->op == NVMET_FCOP_READDATA_RSP) {
524 				ctxp->ts_isr_data =
525 					cmdwqe->isr_timestamp;
526 				ctxp->ts_data_nvme =
527 					ktime_get_ns();
528 				ctxp->ts_nvme_status =
529 					ctxp->ts_data_nvme;
530 				ctxp->ts_status_wqput =
531 					ctxp->ts_data_nvme;
532 				ctxp->ts_isr_status =
533 					ctxp->ts_data_nvme;
534 				ctxp->ts_status_nvme =
535 					ctxp->ts_data_nvme;
536 			} else {
537 				ctxp->ts_isr_status =
538 					cmdwqe->isr_timestamp;
539 				ctxp->ts_status_nvme =
540 					ktime_get_ns();
541 			}
542 		}
543 		if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
544 			id = smp_processor_id();
545 			if (ctxp->cpu != id)
546 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
547 						"6703 CPU Check cmpl: "
548 						"cpu %d expect %d\n",
549 						id, ctxp->cpu);
550 			if (ctxp->cpu < LPFC_CHECK_CPU_CNT)
551 				phba->cpucheck_cmpl_io[id]++;
552 		}
553 #endif
554 		rsp->done(rsp);
555 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
556 		if (phba->ktime_on)
557 			lpfc_nvmet_ktime(phba, ctxp);
558 #endif
559 		/* lpfc_nvmet_xmt_fcp_release() will recycle the context */
560 	} else {
561 		ctxp->entry_cnt++;
562 		start_clean = offsetof(struct lpfc_iocbq, iocb_flag);
563 		memset(((char *)cmdwqe) + start_clean, 0,
564 		       (sizeof(struct lpfc_iocbq) - start_clean));
565 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
566 		if (phba->ktime_on) {
567 			ctxp->ts_isr_data = cmdwqe->isr_timestamp;
568 			ctxp->ts_data_nvme = ktime_get_ns();
569 		}
570 		if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
571 			id = smp_processor_id();
572 			if (ctxp->cpu != id)
573 				lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
574 						"6704 CPU Check cmdcmpl: "
575 						"cpu %d expect %d\n",
576 						id, ctxp->cpu);
577 			if (ctxp->cpu < LPFC_CHECK_CPU_CNT)
578 				phba->cpucheck_ccmpl_io[id]++;
579 		}
580 #endif
581 		rsp->done(rsp);
582 	}
583 }
584 
585 static int
586 lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
587 		      struct nvmefc_tgt_ls_req *rsp)
588 {
589 	struct lpfc_nvmet_rcv_ctx *ctxp =
590 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.ls_req);
591 	struct lpfc_hba *phba = ctxp->phba;
592 	struct hbq_dmabuf *nvmebuf =
593 		(struct hbq_dmabuf *)ctxp->rqb_buffer;
594 	struct lpfc_iocbq *nvmewqeq;
595 	struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
596 	struct lpfc_dmabuf dmabuf;
597 	struct ulp_bde64 bpl;
598 	int rc;
599 
600 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
601 			"6023 NVMET LS rsp oxid x%x\n", ctxp->oxid);
602 
603 	if ((ctxp->state != LPFC_NVMET_STE_LS_RCV) ||
604 	    (ctxp->entry_cnt != 1)) {
605 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
606 				"6412 NVMET LS rsp state mismatch "
607 				"oxid x%x: %d %d\n",
608 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
609 	}
610 	ctxp->state = LPFC_NVMET_STE_LS_RSP;
611 	ctxp->entry_cnt++;
612 
613 	nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, ctxp, rsp->rspdma,
614 				      rsp->rsplen);
615 	if (nvmewqeq == NULL) {
616 		atomic_inc(&nvmep->xmt_ls_drop);
617 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
618 				"6150 LS Drop IO x%x: Prep\n",
619 				ctxp->oxid);
620 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
621 		atomic_inc(&nvmep->xmt_ls_abort);
622 		lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp,
623 						ctxp->sid, ctxp->oxid);
624 		return -ENOMEM;
625 	}
626 
627 	/* Save numBdes for bpl2sgl */
628 	nvmewqeq->rsvd2 = 1;
629 	nvmewqeq->hba_wqidx = 0;
630 	nvmewqeq->context3 = &dmabuf;
631 	dmabuf.virt = &bpl;
632 	bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
633 	bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
634 	bpl.tus.f.bdeSize = rsp->rsplen;
635 	bpl.tus.f.bdeFlags = 0;
636 	bpl.tus.w = le32_to_cpu(bpl.tus.w);
637 
638 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_rsp_cmp;
639 	nvmewqeq->iocb_cmpl = NULL;
640 	nvmewqeq->context2 = ctxp;
641 
642 	lpfc_nvmeio_data(phba, "NVMET LS  RESP: xri x%x wqidx x%x len x%x\n",
643 			 ctxp->oxid, nvmewqeq->hba_wqidx, rsp->rsplen);
644 
645 	rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, nvmewqeq);
646 	if (rc == WQE_SUCCESS) {
647 		/*
648 		 * Okay to repost buffer here, but wait till cmpl
649 		 * before freeing ctxp and iocbq.
650 		 */
651 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
652 		ctxp->rqb_buffer = 0;
653 		atomic_inc(&nvmep->xmt_ls_rsp);
654 		return 0;
655 	}
656 	/* Give back resources */
657 	atomic_inc(&nvmep->xmt_ls_drop);
658 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
659 			"6151 LS Drop IO x%x: Issue %d\n",
660 			ctxp->oxid, rc);
661 
662 	lpfc_nlp_put(nvmewqeq->context1);
663 
664 	lpfc_in_buf_free(phba, &nvmebuf->dbuf);
665 	atomic_inc(&nvmep->xmt_ls_abort);
666 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
667 	return -ENXIO;
668 }
669 
670 static int
671 lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
672 		      struct nvmefc_tgt_fcp_req *rsp)
673 {
674 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
675 	struct lpfc_nvmet_rcv_ctx *ctxp =
676 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
677 	struct lpfc_hba *phba = ctxp->phba;
678 	struct lpfc_iocbq *nvmewqeq;
679 	int rc;
680 
681 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
682 	if (phba->ktime_on) {
683 		if (rsp->op == NVMET_FCOP_RSP)
684 			ctxp->ts_nvme_status = ktime_get_ns();
685 		else
686 			ctxp->ts_nvme_data = ktime_get_ns();
687 	}
688 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
689 		int id = smp_processor_id();
690 		ctxp->cpu = id;
691 		if (id < LPFC_CHECK_CPU_CNT)
692 			phba->cpucheck_xmt_io[id]++;
693 		if (rsp->hwqid != id) {
694 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
695 					"6705 CPU Check OP: "
696 					"cpu %d expect %d\n",
697 					id, rsp->hwqid);
698 			ctxp->cpu = rsp->hwqid;
699 		}
700 	}
701 #endif
702 
703 	/* Sanity check */
704 	if ((ctxp->flag & LPFC_NVMET_ABTS_RCV) ||
705 	    (ctxp->state == LPFC_NVMET_STE_ABORT)) {
706 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
707 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
708 				"6102 IO xri x%x aborted\n",
709 				ctxp->oxid);
710 		rc = -ENXIO;
711 		goto aerr;
712 	}
713 
714 	nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
715 	if (nvmewqeq == NULL) {
716 		atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
717 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
718 				"6152 FCP Drop IO x%x: Prep\n",
719 				ctxp->oxid);
720 		rc = -ENXIO;
721 		goto aerr;
722 	}
723 
724 	nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
725 	nvmewqeq->iocb_cmpl = NULL;
726 	nvmewqeq->context2 = ctxp;
727 	nvmewqeq->iocb_flag |=  LPFC_IO_NVMET;
728 	ctxp->wqeq->hba_wqidx = rsp->hwqid;
729 
730 	lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
731 			 ctxp->oxid, rsp->op, rsp->rsplen);
732 
733 	ctxp->flag |= LPFC_NVMET_IO_INP;
734 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, nvmewqeq);
735 	if (rc == WQE_SUCCESS) {
736 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
737 		if (!phba->ktime_on)
738 			return 0;
739 		if (rsp->op == NVMET_FCOP_RSP)
740 			ctxp->ts_status_wqput = ktime_get_ns();
741 		else
742 			ctxp->ts_data_wqput = ktime_get_ns();
743 #endif
744 		return 0;
745 	}
746 
747 	/* Give back resources */
748 	atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
749 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
750 			"6153 FCP Drop IO x%x: Issue: %d\n",
751 			ctxp->oxid, rc);
752 
753 	ctxp->wqeq->hba_wqidx = 0;
754 	nvmewqeq->context2 = NULL;
755 	nvmewqeq->context3 = NULL;
756 	rc = -EBUSY;
757 aerr:
758 	return rc;
759 }
760 
761 static void
762 lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
763 {
764 	struct lpfc_nvmet_tgtport *tport = targetport->private;
765 
766 	/* release any threads waiting for the unreg to complete */
767 	complete(&tport->tport_unreg_done);
768 }
769 
770 static void
771 lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
772 			 struct nvmefc_tgt_fcp_req *req)
773 {
774 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
775 	struct lpfc_nvmet_rcv_ctx *ctxp =
776 		container_of(req, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
777 	struct lpfc_hba *phba = ctxp->phba;
778 	unsigned long flags;
779 
780 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
781 			"6103 NVMET Abort op: oxri x%x flg x%x ste %d\n",
782 			ctxp->oxid, ctxp->flag, ctxp->state);
783 
784 	lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x flg x%x ste x%x\n",
785 			 ctxp->oxid, ctxp->flag, ctxp->state);
786 
787 	atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
788 
789 	spin_lock_irqsave(&ctxp->ctxlock, flags);
790 
791 	/* Since iaab/iaar are NOT set, we need to check
792 	 * if the firmware is in process of aborting IO
793 	 */
794 	if (ctxp->flag & LPFC_NVMET_XBUSY) {
795 		spin_unlock_irqrestore(&ctxp->ctxlock, flags);
796 		return;
797 	}
798 	ctxp->flag |= LPFC_NVMET_ABORT_OP;
799 
800 	/* An state of LPFC_NVMET_STE_RCV means we have just received
801 	 * the NVME command and have not started processing it.
802 	 * (by issuing any IO WQEs on this exchange yet)
803 	 */
804 	if (ctxp->state == LPFC_NVMET_STE_RCV)
805 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
806 						 ctxp->oxid);
807 	else
808 		lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
809 					       ctxp->oxid);
810 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
811 }
812 
813 static void
814 lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
815 			   struct nvmefc_tgt_fcp_req *rsp)
816 {
817 	struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
818 	struct lpfc_nvmet_rcv_ctx *ctxp =
819 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
820 	struct lpfc_hba *phba = ctxp->phba;
821 	unsigned long flags;
822 	bool aborting = false;
823 
824 	if (ctxp->state != LPFC_NVMET_STE_DONE &&
825 	    ctxp->state != LPFC_NVMET_STE_ABORT) {
826 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
827 				"6413 NVMET release bad state %d %d oxid x%x\n",
828 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
829 	}
830 
831 	spin_lock_irqsave(&ctxp->ctxlock, flags);
832 	if ((ctxp->flag & LPFC_NVMET_ABORT_OP) ||
833 	    (ctxp->flag & LPFC_NVMET_XBUSY)) {
834 		aborting = true;
835 		/* let the abort path do the real release */
836 		lpfc_nvmet_defer_release(phba, ctxp);
837 	}
838 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
839 
840 	lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d abt %d\n", ctxp->oxid,
841 			 ctxp->state, aborting);
842 
843 	atomic_inc(&lpfc_nvmep->xmt_fcp_release);
844 
845 	if (aborting)
846 		return;
847 
848 	lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
849 }
850 
851 static void
852 lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port *tgtport,
853 		     struct nvmefc_tgt_fcp_req *rsp)
854 {
855 	struct lpfc_nvmet_tgtport *tgtp;
856 	struct lpfc_nvmet_rcv_ctx *ctxp =
857 		container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
858 	struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
859 	struct lpfc_hba *phba = ctxp->phba;
860 
861 	lpfc_nvmeio_data(phba, "NVMET DEFERRCV: xri x%x sz %d CPU %02x\n",
862 			 ctxp->oxid, ctxp->size, smp_processor_id());
863 
864 	tgtp = phba->targetport->private;
865 	atomic_inc(&tgtp->rcv_fcp_cmd_defer);
866 	lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
867 }
868 
869 static struct nvmet_fc_target_template lpfc_tgttemplate = {
870 	.targetport_delete = lpfc_nvmet_targetport_delete,
871 	.xmt_ls_rsp     = lpfc_nvmet_xmt_ls_rsp,
872 	.fcp_op         = lpfc_nvmet_xmt_fcp_op,
873 	.fcp_abort      = lpfc_nvmet_xmt_fcp_abort,
874 	.fcp_req_release = lpfc_nvmet_xmt_fcp_release,
875 	.defer_rcv	= lpfc_nvmet_defer_rcv,
876 
877 	.max_hw_queues  = 1,
878 	.max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
879 	.max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
880 	.dma_boundary = 0xFFFFFFFF,
881 
882 	/* optional features */
883 	.target_features = 0,
884 	/* sizes of additional private data for data structures */
885 	.target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
886 };
887 
888 static void
889 __lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba *phba,
890 		struct lpfc_nvmet_ctx_info *infop)
891 {
892 	struct lpfc_nvmet_ctxbuf *ctx_buf, *next_ctx_buf;
893 	unsigned long flags;
894 
895 	spin_lock_irqsave(&infop->nvmet_ctx_list_lock, flags);
896 	list_for_each_entry_safe(ctx_buf, next_ctx_buf,
897 				&infop->nvmet_ctx_list, list) {
898 		spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
899 		list_del_init(&ctx_buf->list);
900 		spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
901 
902 		__lpfc_clear_active_sglq(phba, ctx_buf->sglq->sli4_lxritag);
903 		ctx_buf->sglq->state = SGL_FREED;
904 		ctx_buf->sglq->ndlp = NULL;
905 
906 		spin_lock(&phba->sli4_hba.sgl_list_lock);
907 		list_add_tail(&ctx_buf->sglq->list,
908 				&phba->sli4_hba.lpfc_nvmet_sgl_list);
909 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
910 
911 		lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
912 		kfree(ctx_buf->context);
913 	}
914 	spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, flags);
915 }
916 
917 static void
918 lpfc_nvmet_cleanup_io_context(struct lpfc_hba *phba)
919 {
920 	struct lpfc_nvmet_ctx_info *infop;
921 	int i, j;
922 
923 	/* The first context list, MRQ 0 CPU 0 */
924 	infop = phba->sli4_hba.nvmet_ctx_info;
925 	if (!infop)
926 		return;
927 
928 	/* Cycle the the entire CPU context list for every MRQ */
929 	for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
930 		for (j = 0; j < phba->sli4_hba.num_present_cpu; j++) {
931 			__lpfc_nvmet_clean_io_for_cpu(phba, infop);
932 			infop++; /* next */
933 		}
934 	}
935 	kfree(phba->sli4_hba.nvmet_ctx_info);
936 	phba->sli4_hba.nvmet_ctx_info = NULL;
937 }
938 
939 static int
940 lpfc_nvmet_setup_io_context(struct lpfc_hba *phba)
941 {
942 	struct lpfc_nvmet_ctxbuf *ctx_buf;
943 	struct lpfc_iocbq *nvmewqe;
944 	union lpfc_wqe128 *wqe;
945 	struct lpfc_nvmet_ctx_info *last_infop;
946 	struct lpfc_nvmet_ctx_info *infop;
947 	int i, j, idx;
948 
949 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
950 			"6403 Allocate NVMET resources for %d XRIs\n",
951 			phba->sli4_hba.nvmet_xri_cnt);
952 
953 	phba->sli4_hba.nvmet_ctx_info = kcalloc(
954 		phba->sli4_hba.num_present_cpu * phba->cfg_nvmet_mrq,
955 		sizeof(struct lpfc_nvmet_ctx_info), GFP_KERNEL);
956 	if (!phba->sli4_hba.nvmet_ctx_info) {
957 		lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
958 				"6419 Failed allocate memory for "
959 				"nvmet context lists\n");
960 		return -ENOMEM;
961 	}
962 
963 	/*
964 	 * Assuming X CPUs in the system, and Y MRQs, allocate some
965 	 * lpfc_nvmet_ctx_info structures as follows:
966 	 *
967 	 * cpu0/mrq0 cpu1/mrq0 ... cpuX/mrq0
968 	 * cpu0/mrq1 cpu1/mrq1 ... cpuX/mrq1
969 	 * ...
970 	 * cpuX/mrqY cpuX/mrqY ... cpuX/mrqY
971 	 *
972 	 * Each line represents a MRQ "silo" containing an entry for
973 	 * every CPU.
974 	 *
975 	 * MRQ X is initially assumed to be associated with CPU X, thus
976 	 * contexts are initially distributed across all MRQs using
977 	 * the MRQ index (N) as follows cpuN/mrqN. When contexts are
978 	 * freed, the are freed to the MRQ silo based on the CPU number
979 	 * of the IO completion. Thus a context that was allocated for MRQ A
980 	 * whose IO completed on CPU B will be freed to cpuB/mrqA.
981 	 */
982 	infop = phba->sli4_hba.nvmet_ctx_info;
983 	for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
984 		for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
985 			INIT_LIST_HEAD(&infop->nvmet_ctx_list);
986 			spin_lock_init(&infop->nvmet_ctx_list_lock);
987 			infop->nvmet_ctx_list_cnt = 0;
988 			infop++;
989 		}
990 	}
991 
992 	/*
993 	 * Setup the next CPU context info ptr for each MRQ.
994 	 * MRQ 0 will cycle thru CPUs 0 - X separately from
995 	 * MRQ 1 cycling thru CPUs 0 - X, and so on.
996 	 */
997 	for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
998 		last_infop = lpfc_get_ctx_list(phba, 0, j);
999 		for (i = phba->sli4_hba.num_present_cpu - 1;  i >= 0; i--) {
1000 			infop = lpfc_get_ctx_list(phba, i, j);
1001 			infop->nvmet_ctx_next_cpu = last_infop;
1002 			last_infop = infop;
1003 		}
1004 	}
1005 
1006 	/* For all nvmet xris, allocate resources needed to process a
1007 	 * received command on a per xri basis.
1008 	 */
1009 	idx = 0;
1010 	for (i = 0; i < phba->sli4_hba.nvmet_xri_cnt; i++) {
1011 		ctx_buf = kzalloc(sizeof(*ctx_buf), GFP_KERNEL);
1012 		if (!ctx_buf) {
1013 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1014 					"6404 Ran out of memory for NVMET\n");
1015 			return -ENOMEM;
1016 		}
1017 
1018 		ctx_buf->context = kzalloc(sizeof(*ctx_buf->context),
1019 					   GFP_KERNEL);
1020 		if (!ctx_buf->context) {
1021 			kfree(ctx_buf);
1022 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1023 					"6405 Ran out of NVMET "
1024 					"context memory\n");
1025 			return -ENOMEM;
1026 		}
1027 		ctx_buf->context->ctxbuf = ctx_buf;
1028 		ctx_buf->context->state = LPFC_NVMET_STE_FREE;
1029 
1030 		ctx_buf->iocbq = lpfc_sli_get_iocbq(phba);
1031 		if (!ctx_buf->iocbq) {
1032 			kfree(ctx_buf->context);
1033 			kfree(ctx_buf);
1034 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1035 					"6406 Ran out of NVMET iocb/WQEs\n");
1036 			return -ENOMEM;
1037 		}
1038 		ctx_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
1039 		nvmewqe = ctx_buf->iocbq;
1040 		wqe = (union lpfc_wqe128 *)&nvmewqe->wqe;
1041 		/* Initialize WQE */
1042 		memset(wqe, 0, sizeof(union lpfc_wqe));
1043 		/* Word 7 */
1044 		bf_set(wqe_ct, &wqe->generic.wqe_com, SLI4_CT_RPI);
1045 		bf_set(wqe_class, &wqe->generic.wqe_com, CLASS3);
1046 		/* Word 10 */
1047 		bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
1048 		bf_set(wqe_ebde_cnt, &wqe->generic.wqe_com, 0);
1049 		bf_set(wqe_qosd, &wqe->generic.wqe_com, 0);
1050 
1051 		ctx_buf->iocbq->context1 = NULL;
1052 		spin_lock(&phba->sli4_hba.sgl_list_lock);
1053 		ctx_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, ctx_buf->iocbq);
1054 		spin_unlock(&phba->sli4_hba.sgl_list_lock);
1055 		if (!ctx_buf->sglq) {
1056 			lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
1057 			kfree(ctx_buf->context);
1058 			kfree(ctx_buf);
1059 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1060 					"6407 Ran out of NVMET XRIs\n");
1061 			return -ENOMEM;
1062 		}
1063 
1064 		/*
1065 		 * Add ctx to MRQidx context list. Our initial assumption
1066 		 * is MRQidx will be associated with CPUidx. This association
1067 		 * can change on the fly.
1068 		 */
1069 		infop = lpfc_get_ctx_list(phba, idx, idx);
1070 		spin_lock(&infop->nvmet_ctx_list_lock);
1071 		list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
1072 		infop->nvmet_ctx_list_cnt++;
1073 		spin_unlock(&infop->nvmet_ctx_list_lock);
1074 
1075 		/* Spread ctx structures evenly across all MRQs */
1076 		idx++;
1077 		if (idx >= phba->cfg_nvmet_mrq)
1078 			idx = 0;
1079 	}
1080 
1081 	infop = phba->sli4_hba.nvmet_ctx_info;
1082 	for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1083 		for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
1084 			lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
1085 					"6408 TOTAL NVMET ctx for CPU %d "
1086 					"MRQ %d: cnt %d nextcpu %p\n",
1087 					i, j, infop->nvmet_ctx_list_cnt,
1088 					infop->nvmet_ctx_next_cpu);
1089 			infop++;
1090 		}
1091 	}
1092 	return 0;
1093 }
1094 
1095 int
1096 lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1097 {
1098 	struct lpfc_vport  *vport = phba->pport;
1099 	struct lpfc_nvmet_tgtport *tgtp;
1100 	struct nvmet_fc_port_info pinfo;
1101 	int error;
1102 
1103 	if (phba->targetport)
1104 		return 0;
1105 
1106 	error = lpfc_nvmet_setup_io_context(phba);
1107 	if (error)
1108 		return error;
1109 
1110 	memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1111 	pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1112 	pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1113 	pinfo.port_id = vport->fc_myDID;
1114 
1115 	/* Limit to LPFC_MAX_NVME_SEG_CNT.
1116 	 * For now need + 1 to get around NVME transport logic.
1117 	 */
1118 	if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
1119 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
1120 				"6400 Reducing sg segment cnt to %d\n",
1121 				LPFC_MAX_NVME_SEG_CNT);
1122 		phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
1123 	} else {
1124 		phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
1125 	}
1126 	lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1127 	lpfc_tgttemplate.max_hw_queues = phba->cfg_nvme_io_channel;
1128 	lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP |
1129 					   NVMET_FCTGTFEAT_CMD_IN_ISR |
1130 					   NVMET_FCTGTFEAT_OPDONE_IN_ISR;
1131 
1132 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1133 	error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1134 					     &phba->pcidev->dev,
1135 					     &phba->targetport);
1136 #else
1137 	error = -ENOENT;
1138 #endif
1139 	if (error) {
1140 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1141 				"6025 Cannot register NVME targetport "
1142 				"x%x\n", error);
1143 		phba->targetport = NULL;
1144 
1145 		lpfc_nvmet_cleanup_io_context(phba);
1146 
1147 	} else {
1148 		tgtp = (struct lpfc_nvmet_tgtport *)
1149 			phba->targetport->private;
1150 		tgtp->phba = phba;
1151 
1152 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1153 				"6026 Registered NVME "
1154 				"targetport: %p, private %p "
1155 				"portnm %llx nodenm %llx\n",
1156 				phba->targetport, tgtp,
1157 				pinfo.port_name, pinfo.node_name);
1158 
1159 		atomic_set(&tgtp->rcv_ls_req_in, 0);
1160 		atomic_set(&tgtp->rcv_ls_req_out, 0);
1161 		atomic_set(&tgtp->rcv_ls_req_drop, 0);
1162 		atomic_set(&tgtp->xmt_ls_abort, 0);
1163 		atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1164 		atomic_set(&tgtp->xmt_ls_rsp, 0);
1165 		atomic_set(&tgtp->xmt_ls_drop, 0);
1166 		atomic_set(&tgtp->xmt_ls_rsp_error, 0);
1167 		atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1168 		atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1169 		atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1170 		atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1171 		atomic_set(&tgtp->xmt_fcp_drop, 0);
1172 		atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1173 		atomic_set(&tgtp->xmt_fcp_read, 0);
1174 		atomic_set(&tgtp->xmt_fcp_write, 0);
1175 		atomic_set(&tgtp->xmt_fcp_rsp, 0);
1176 		atomic_set(&tgtp->xmt_fcp_release, 0);
1177 		atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1178 		atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
1179 		atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
1180 		atomic_set(&tgtp->xmt_fcp_abort, 0);
1181 		atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1182 		atomic_set(&tgtp->xmt_abort_unsol, 0);
1183 		atomic_set(&tgtp->xmt_abort_sol, 0);
1184 		atomic_set(&tgtp->xmt_abort_rsp, 0);
1185 		atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1186 	}
1187 	return error;
1188 }
1189 
1190 int
1191 lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1192 {
1193 	struct lpfc_vport  *vport = phba->pport;
1194 
1195 	if (!phba->targetport)
1196 		return 0;
1197 
1198 	lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
1199 			 "6007 Update NVMET port %p did x%x\n",
1200 			 phba->targetport, vport->fc_myDID);
1201 
1202 	phba->targetport->port_id = vport->fc_myDID;
1203 	return 0;
1204 }
1205 
1206 /**
1207  * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1208  * @phba: pointer to lpfc hba data structure.
1209  * @axri: pointer to the nvmet xri abort wcqe structure.
1210  *
1211  * This routine is invoked by the worker thread to process a SLI4 fast-path
1212  * NVMET aborted xri.
1213  **/
1214 void
1215 lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
1216 			    struct sli4_wcqe_xri_aborted *axri)
1217 {
1218 	uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
1219 	uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
1220 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
1221 	struct lpfc_nodelist *ndlp;
1222 	unsigned long iflag = 0;
1223 	int rrq_empty = 0;
1224 	bool released = false;
1225 
1226 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1227 			"6317 XB aborted xri x%x rxid x%x\n", xri, rxid);
1228 
1229 	if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
1230 		return;
1231 	spin_lock_irqsave(&phba->hbalock, iflag);
1232 	spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1233 	list_for_each_entry_safe(ctxp, next_ctxp,
1234 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1235 				 list) {
1236 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
1237 			continue;
1238 
1239 		/* Check if we already received a free context call
1240 		 * and we have completed processing an abort situation.
1241 		 */
1242 		if (ctxp->flag & LPFC_NVMET_CTX_RLS &&
1243 		    !(ctxp->flag & LPFC_NVMET_ABORT_OP)) {
1244 			list_del(&ctxp->list);
1245 			released = true;
1246 		}
1247 		ctxp->flag &= ~LPFC_NVMET_XBUSY;
1248 		spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1249 
1250 		rrq_empty = list_empty(&phba->active_rrq_list);
1251 		spin_unlock_irqrestore(&phba->hbalock, iflag);
1252 		ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1253 		if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1254 		    (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
1255 		     ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
1256 			lpfc_set_rrq_active(phba, ndlp,
1257 				ctxp->ctxbuf->sglq->sli4_lxritag,
1258 				rxid, 1);
1259 			lpfc_sli4_abts_err_handler(phba, ndlp, axri);
1260 		}
1261 
1262 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1263 				"6318 XB aborted oxid %x flg x%x (%x)\n",
1264 				ctxp->oxid, ctxp->flag, released);
1265 		if (released)
1266 			lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
1267 
1268 		if (rrq_empty)
1269 			lpfc_worker_wake_up(phba);
1270 		return;
1271 	}
1272 	spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1273 	spin_unlock_irqrestore(&phba->hbalock, iflag);
1274 }
1275 
1276 int
1277 lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport *vport,
1278 			   struct fc_frame_header *fc_hdr)
1279 
1280 {
1281 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1282 	struct lpfc_hba *phba = vport->phba;
1283 	struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
1284 	struct nvmefc_tgt_fcp_req *rsp;
1285 	uint16_t xri;
1286 	unsigned long iflag = 0;
1287 
1288 	xri = be16_to_cpu(fc_hdr->fh_ox_id);
1289 
1290 	spin_lock_irqsave(&phba->hbalock, iflag);
1291 	spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1292 	list_for_each_entry_safe(ctxp, next_ctxp,
1293 				 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1294 				 list) {
1295 		if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
1296 			continue;
1297 
1298 		spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1299 		spin_unlock_irqrestore(&phba->hbalock, iflag);
1300 
1301 		spin_lock_irqsave(&ctxp->ctxlock, iflag);
1302 		ctxp->flag |= LPFC_NVMET_ABTS_RCV;
1303 		spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
1304 
1305 		lpfc_nvmeio_data(phba,
1306 			"NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1307 			xri, smp_processor_id(), 0);
1308 
1309 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1310 				"6319 NVMET Rcv ABTS:acc xri x%x\n", xri);
1311 
1312 		rsp = &ctxp->ctx.fcp_req;
1313 		nvmet_fc_rcv_fcp_abort(phba->targetport, rsp);
1314 
1315 		/* Respond with BA_ACC accordingly */
1316 		lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
1317 		return 0;
1318 	}
1319 	spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
1320 	spin_unlock_irqrestore(&phba->hbalock, iflag);
1321 
1322 	lpfc_nvmeio_data(phba, "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1323 			 xri, smp_processor_id(), 1);
1324 
1325 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1326 			"6320 NVMET Rcv ABTS:rjt xri x%x\n", xri);
1327 
1328 	/* Respond with BA_RJT accordingly */
1329 	lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 0);
1330 #endif
1331 	return 0;
1332 }
1333 
1334 void
1335 lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
1336 {
1337 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1338 	struct lpfc_nvmet_tgtport *tgtp;
1339 
1340 	if (phba->nvmet_support == 0)
1341 		return;
1342 	if (phba->targetport) {
1343 		tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1344 		init_completion(&tgtp->tport_unreg_done);
1345 		nvmet_fc_unregister_targetport(phba->targetport);
1346 		wait_for_completion_timeout(&tgtp->tport_unreg_done, 5);
1347 		lpfc_nvmet_cleanup_io_context(phba);
1348 	}
1349 	phba->targetport = NULL;
1350 #endif
1351 }
1352 
1353 /**
1354  * lpfc_nvmet_unsol_ls_buffer - Process an unsolicited event data buffer
1355  * @phba: pointer to lpfc hba data structure.
1356  * @pring: pointer to a SLI ring.
1357  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
1358  *
1359  * This routine is used for processing the WQE associated with a unsolicited
1360  * event. It first determines whether there is an existing ndlp that matches
1361  * the DID from the unsolicited WQE. If not, it will create a new one with
1362  * the DID from the unsolicited WQE. The ELS command from the unsolicited
1363  * WQE is then used to invoke the proper routine and to set up proper state
1364  * of the discovery state machine.
1365  **/
1366 static void
1367 lpfc_nvmet_unsol_ls_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1368 			   struct hbq_dmabuf *nvmebuf)
1369 {
1370 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1371 	struct lpfc_nvmet_tgtport *tgtp;
1372 	struct fc_frame_header *fc_hdr;
1373 	struct lpfc_nvmet_rcv_ctx *ctxp;
1374 	uint32_t *payload;
1375 	uint32_t size, oxid, sid, rc;
1376 
1377 	if (!nvmebuf || !phba->targetport) {
1378 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1379 				"6154 LS Drop IO\n");
1380 		oxid = 0;
1381 		size = 0;
1382 		sid = 0;
1383 		ctxp = NULL;
1384 		goto dropit;
1385 	}
1386 
1387 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1388 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
1389 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
1390 	size = bf_get(lpfc_rcqe_length,  &nvmebuf->cq_event.cqe.rcqe_cmpl);
1391 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
1392 	sid = sli4_sid_from_fc_hdr(fc_hdr);
1393 
1394 	ctxp = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx), GFP_ATOMIC);
1395 	if (ctxp == NULL) {
1396 		atomic_inc(&tgtp->rcv_ls_req_drop);
1397 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1398 				"6155 LS Drop IO x%x: Alloc\n",
1399 				oxid);
1400 dropit:
1401 		lpfc_nvmeio_data(phba, "NVMET LS  DROP: "
1402 				 "xri x%x sz %d from %06x\n",
1403 				 oxid, size, sid);
1404 		if (nvmebuf)
1405 			lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1406 		return;
1407 	}
1408 	ctxp->phba = phba;
1409 	ctxp->size = size;
1410 	ctxp->oxid = oxid;
1411 	ctxp->sid = sid;
1412 	ctxp->wqeq = NULL;
1413 	ctxp->state = LPFC_NVMET_STE_LS_RCV;
1414 	ctxp->entry_cnt = 1;
1415 	ctxp->rqb_buffer = (void *)nvmebuf;
1416 
1417 	lpfc_nvmeio_data(phba, "NVMET LS   RCV: xri x%x sz %d from %06x\n",
1418 			 oxid, size, sid);
1419 	/*
1420 	 * The calling sequence should be:
1421 	 * nvmet_fc_rcv_ls_req -> lpfc_nvmet_xmt_ls_rsp/cmp ->_req->done
1422 	 * lpfc_nvmet_xmt_ls_rsp_cmp should free the allocated ctxp.
1423 	 */
1424 	atomic_inc(&tgtp->rcv_ls_req_in);
1425 	rc = nvmet_fc_rcv_ls_req(phba->targetport, &ctxp->ctx.ls_req,
1426 				 payload, size);
1427 
1428 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1429 			"6037 NVMET Unsol rcv: sz %d rc %d: %08x %08x %08x "
1430 			"%08x %08x %08x\n", size, rc,
1431 			*payload, *(payload+1), *(payload+2),
1432 			*(payload+3), *(payload+4), *(payload+5));
1433 
1434 	if (rc == 0) {
1435 		atomic_inc(&tgtp->rcv_ls_req_out);
1436 		return;
1437 	}
1438 
1439 	lpfc_nvmeio_data(phba, "NVMET LS  DROP: xri x%x sz %d from %06x\n",
1440 			 oxid, size, sid);
1441 
1442 	atomic_inc(&tgtp->rcv_ls_req_drop);
1443 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1444 			"6156 LS Drop IO x%x: nvmet_fc_rcv_ls_req %d\n",
1445 			ctxp->oxid, rc);
1446 
1447 	/* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
1448 	if (nvmebuf)
1449 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1450 
1451 	atomic_inc(&tgtp->xmt_ls_abort);
1452 	lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, sid, oxid);
1453 #endif
1454 }
1455 
1456 static struct lpfc_nvmet_ctxbuf *
1457 lpfc_nvmet_replenish_context(struct lpfc_hba *phba,
1458 			     struct lpfc_nvmet_ctx_info *current_infop)
1459 {
1460 	struct lpfc_nvmet_ctxbuf *ctx_buf = NULL;
1461 	struct lpfc_nvmet_ctx_info *get_infop;
1462 	int i;
1463 
1464 	/*
1465 	 * The current_infop for the MRQ a NVME command IU was received
1466 	 * on is empty. Our goal is to replenish this MRQs context
1467 	 * list from a another CPUs.
1468 	 *
1469 	 * First we need to pick a context list to start looking on.
1470 	 * nvmet_ctx_start_cpu has available context the last time
1471 	 * we needed to replenish this CPU where nvmet_ctx_next_cpu
1472 	 * is just the next sequential CPU for this MRQ.
1473 	 */
1474 	if (current_infop->nvmet_ctx_start_cpu)
1475 		get_infop = current_infop->nvmet_ctx_start_cpu;
1476 	else
1477 		get_infop = current_infop->nvmet_ctx_next_cpu;
1478 
1479 	for (i = 0; i < phba->sli4_hba.num_present_cpu; i++) {
1480 		if (get_infop == current_infop) {
1481 			get_infop = get_infop->nvmet_ctx_next_cpu;
1482 			continue;
1483 		}
1484 		spin_lock(&get_infop->nvmet_ctx_list_lock);
1485 
1486 		/* Just take the entire context list, if there are any */
1487 		if (get_infop->nvmet_ctx_list_cnt) {
1488 			list_splice_init(&get_infop->nvmet_ctx_list,
1489 				    &current_infop->nvmet_ctx_list);
1490 			current_infop->nvmet_ctx_list_cnt =
1491 				get_infop->nvmet_ctx_list_cnt - 1;
1492 			get_infop->nvmet_ctx_list_cnt = 0;
1493 			spin_unlock(&get_infop->nvmet_ctx_list_lock);
1494 
1495 			current_infop->nvmet_ctx_start_cpu = get_infop;
1496 			list_remove_head(&current_infop->nvmet_ctx_list,
1497 					 ctx_buf, struct lpfc_nvmet_ctxbuf,
1498 					 list);
1499 			return ctx_buf;
1500 		}
1501 
1502 		/* Otherwise, move on to the next CPU for this MRQ */
1503 		spin_unlock(&get_infop->nvmet_ctx_list_lock);
1504 		get_infop = get_infop->nvmet_ctx_next_cpu;
1505 	}
1506 
1507 	/* Nothing found, all contexts for the MRQ are in-flight */
1508 	return NULL;
1509 }
1510 
1511 /**
1512  * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
1513  * @phba: pointer to lpfc hba data structure.
1514  * @idx: relative index of MRQ vector
1515  * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
1516  *
1517  * This routine is used for processing the WQE associated with a unsolicited
1518  * event. It first determines whether there is an existing ndlp that matches
1519  * the DID from the unsolicited WQE. If not, it will create a new one with
1520  * the DID from the unsolicited WQE. The ELS command from the unsolicited
1521  * WQE is then used to invoke the proper routine and to set up proper state
1522  * of the discovery state machine.
1523  **/
1524 static void
1525 lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
1526 			    uint32_t idx,
1527 			    struct rqb_dmabuf *nvmebuf,
1528 			    uint64_t isr_timestamp)
1529 {
1530 	struct lpfc_nvmet_rcv_ctx *ctxp;
1531 	struct lpfc_nvmet_tgtport *tgtp;
1532 	struct fc_frame_header *fc_hdr;
1533 	struct lpfc_nvmet_ctxbuf *ctx_buf;
1534 	struct lpfc_nvmet_ctx_info *current_infop;
1535 	uint32_t *payload;
1536 	uint32_t size, oxid, sid, rc, qno;
1537 	unsigned long iflag;
1538 	int current_cpu;
1539 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1540 	uint32_t id;
1541 #endif
1542 
1543 	if (!IS_ENABLED(CONFIG_NVME_TARGET_FC))
1544 		return;
1545 
1546 	ctx_buf = NULL;
1547 	if (!nvmebuf || !phba->targetport) {
1548 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1549 				"6157 NVMET FCP Drop IO\n");
1550 		oxid = 0;
1551 		size = 0;
1552 		sid = 0;
1553 		ctxp = NULL;
1554 		goto dropit;
1555 	}
1556 
1557 	/*
1558 	 * Get a pointer to the context list for this MRQ based on
1559 	 * the CPU this MRQ IRQ is associated with. If the CPU association
1560 	 * changes from our initial assumption, the context list could
1561 	 * be empty, thus it would need to be replenished with the
1562 	 * context list from another CPU for this MRQ.
1563 	 */
1564 	current_cpu = smp_processor_id();
1565 	current_infop = lpfc_get_ctx_list(phba, current_cpu, idx);
1566 	spin_lock_irqsave(&current_infop->nvmet_ctx_list_lock, iflag);
1567 	if (current_infop->nvmet_ctx_list_cnt) {
1568 		list_remove_head(&current_infop->nvmet_ctx_list,
1569 				 ctx_buf, struct lpfc_nvmet_ctxbuf, list);
1570 		current_infop->nvmet_ctx_list_cnt--;
1571 	} else {
1572 		ctx_buf = lpfc_nvmet_replenish_context(phba, current_infop);
1573 	}
1574 	spin_unlock_irqrestore(&current_infop->nvmet_ctx_list_lock, iflag);
1575 
1576 	fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
1577 	oxid = be16_to_cpu(fc_hdr->fh_ox_id);
1578 	size = nvmebuf->bytes_recv;
1579 
1580 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1581 	if (phba->cpucheck_on & LPFC_CHECK_NVMET_RCV) {
1582 		id = smp_processor_id();
1583 		if (id < LPFC_CHECK_CPU_CNT)
1584 			phba->cpucheck_rcv_io[id]++;
1585 	}
1586 #endif
1587 
1588 	lpfc_nvmeio_data(phba, "NVMET FCP  RCV: xri x%x sz %d CPU %02x\n",
1589 			 oxid, size, smp_processor_id());
1590 
1591 	if (!ctx_buf) {
1592 		/* Queue this NVME IO to process later */
1593 		spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
1594 		list_add_tail(&nvmebuf->hbuf.list,
1595 			      &phba->sli4_hba.lpfc_nvmet_io_wait_list);
1596 		phba->sli4_hba.nvmet_io_wait_cnt++;
1597 		phba->sli4_hba.nvmet_io_wait_total++;
1598 		spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
1599 				       iflag);
1600 
1601 		/* Post a brand new DMA buffer to RQ */
1602 		qno = nvmebuf->idx;
1603 		lpfc_post_rq_buffer(
1604 			phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
1605 			phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
1606 		return;
1607 	}
1608 
1609 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1610 	payload = (uint32_t *)(nvmebuf->dbuf.virt);
1611 	sid = sli4_sid_from_fc_hdr(fc_hdr);
1612 
1613 	ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
1614 	if (ctxp->state != LPFC_NVMET_STE_FREE) {
1615 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1616 				"6414 NVMET Context corrupt %d %d oxid x%x\n",
1617 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
1618 	}
1619 	ctxp->wqeq = NULL;
1620 	ctxp->txrdy = NULL;
1621 	ctxp->offset = 0;
1622 	ctxp->phba = phba;
1623 	ctxp->size = size;
1624 	ctxp->oxid = oxid;
1625 	ctxp->sid = sid;
1626 	ctxp->idx = idx;
1627 	ctxp->state = LPFC_NVMET_STE_RCV;
1628 	ctxp->entry_cnt = 1;
1629 	ctxp->flag = 0;
1630 	ctxp->ctxbuf = ctx_buf;
1631 	spin_lock_init(&ctxp->ctxlock);
1632 
1633 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1634 	if (phba->ktime_on) {
1635 		ctxp->ts_isr_cmd = isr_timestamp;
1636 		ctxp->ts_cmd_nvme = ktime_get_ns();
1637 		ctxp->ts_nvme_data = 0;
1638 		ctxp->ts_data_wqput = 0;
1639 		ctxp->ts_isr_data = 0;
1640 		ctxp->ts_data_nvme = 0;
1641 		ctxp->ts_nvme_status = 0;
1642 		ctxp->ts_status_wqput = 0;
1643 		ctxp->ts_isr_status = 0;
1644 		ctxp->ts_status_nvme = 0;
1645 	}
1646 #endif
1647 
1648 	atomic_inc(&tgtp->rcv_fcp_cmd_in);
1649 	/*
1650 	 * The calling sequence should be:
1651 	 * nvmet_fc_rcv_fcp_req -> lpfc_nvmet_xmt_fcp_op/cmp -> req->done
1652 	 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
1653 	 * When we return from nvmet_fc_rcv_fcp_req, all relevant info in
1654 	 * the NVME command / FC header is stored, so we are free to repost
1655 	 * the buffer.
1656 	 */
1657 	rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->ctx.fcp_req,
1658 				  payload, size);
1659 
1660 	/* Process FCP command */
1661 	if (rc == 0) {
1662 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
1663 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
1664 		return;
1665 	}
1666 
1667 	/* Processing of FCP command is deferred */
1668 	if (rc == -EOVERFLOW) {
1669 		lpfc_nvmeio_data(phba,
1670 				 "NVMET RCV BUSY: xri x%x sz %d from %06x\n",
1671 				 oxid, size, sid);
1672 		/* defer reposting rcv buffer till .defer_rcv callback */
1673 		ctxp->rqb_buffer = nvmebuf;
1674 		atomic_inc(&tgtp->rcv_fcp_cmd_out);
1675 		return;
1676 	}
1677 
1678 	atomic_inc(&tgtp->rcv_fcp_cmd_drop);
1679 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1680 			"6159 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
1681 			ctxp->oxid, rc,
1682 			atomic_read(&tgtp->rcv_fcp_cmd_in),
1683 			atomic_read(&tgtp->rcv_fcp_cmd_out),
1684 			atomic_read(&tgtp->xmt_fcp_release));
1685 dropit:
1686 	lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
1687 			 oxid, size, sid);
1688 	if (oxid) {
1689 		lpfc_nvmet_defer_release(phba, ctxp);
1690 		lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
1691 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
1692 		return;
1693 	}
1694 
1695 	if (ctx_buf)
1696 		lpfc_nvmet_ctxbuf_post(phba, ctx_buf);
1697 
1698 	if (nvmebuf)
1699 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
1700 }
1701 
1702 /**
1703  * lpfc_nvmet_unsol_ls_event - Process an unsolicited event from an nvme nport
1704  * @phba: pointer to lpfc hba data structure.
1705  * @pring: pointer to a SLI ring.
1706  * @nvmebuf: pointer to received nvme data structure.
1707  *
1708  * This routine is used to process an unsolicited event received from a SLI
1709  * (Service Level Interface) ring. The actual processing of the data buffer
1710  * associated with the unsolicited event is done by invoking the routine
1711  * lpfc_nvmet_unsol_ls_buffer() after properly set up the buffer from the
1712  * SLI RQ on which the unsolicited event was received.
1713  **/
1714 void
1715 lpfc_nvmet_unsol_ls_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1716 			  struct lpfc_iocbq *piocb)
1717 {
1718 	struct lpfc_dmabuf *d_buf;
1719 	struct hbq_dmabuf *nvmebuf;
1720 
1721 	d_buf = piocb->context2;
1722 	nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
1723 
1724 	if (phba->nvmet_support == 0) {
1725 		lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1726 		return;
1727 	}
1728 	lpfc_nvmet_unsol_ls_buffer(phba, pring, nvmebuf);
1729 }
1730 
1731 /**
1732  * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
1733  * @phba: pointer to lpfc hba data structure.
1734  * @idx: relative index of MRQ vector
1735  * @nvmebuf: pointer to received nvme data structure.
1736  *
1737  * This routine is used to process an unsolicited event received from a SLI
1738  * (Service Level Interface) ring. The actual processing of the data buffer
1739  * associated with the unsolicited event is done by invoking the routine
1740  * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
1741  * SLI RQ on which the unsolicited event was received.
1742  **/
1743 void
1744 lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
1745 			   uint32_t idx,
1746 			   struct rqb_dmabuf *nvmebuf,
1747 			   uint64_t isr_timestamp)
1748 {
1749 	if (phba->nvmet_support == 0) {
1750 		lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
1751 		return;
1752 	}
1753 	lpfc_nvmet_unsol_fcp_buffer(phba, idx, nvmebuf,
1754 				    isr_timestamp);
1755 }
1756 
1757 /**
1758  * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
1759  * @phba: pointer to a host N_Port data structure.
1760  * @ctxp: Context info for NVME LS Request
1761  * @rspbuf: DMA buffer of NVME command.
1762  * @rspsize: size of the NVME command.
1763  *
1764  * This routine is used for allocating a lpfc-WQE data structure from
1765  * the driver lpfc-WQE free-list and prepare the WQE with the parameters
1766  * passed into the routine for discovery state machine to issue an Extended
1767  * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
1768  * and preparation routine that is used by all the discovery state machine
1769  * routines and the NVME command-specific fields will be later set up by
1770  * the individual discovery machine routines after calling this routine
1771  * allocating and preparing a generic WQE data structure. It fills in the
1772  * Buffer Descriptor Entries (BDEs), allocates buffers for both command
1773  * payload and response payload (if expected). The reference count on the
1774  * ndlp is incremented by 1 and the reference to the ndlp is put into
1775  * context1 of the WQE data structure for this WQE to hold the ndlp
1776  * reference for the command's callback function to access later.
1777  *
1778  * Return code
1779  *   Pointer to the newly allocated/prepared nvme wqe data structure
1780  *   NULL - when nvme wqe data structure allocation/preparation failed
1781  **/
1782 static struct lpfc_iocbq *
1783 lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
1784 		       struct lpfc_nvmet_rcv_ctx *ctxp,
1785 		       dma_addr_t rspbuf, uint16_t rspsize)
1786 {
1787 	struct lpfc_nodelist *ndlp;
1788 	struct lpfc_iocbq *nvmewqe;
1789 	union lpfc_wqe *wqe;
1790 
1791 	if (!lpfc_is_link_up(phba)) {
1792 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1793 				"6104 NVMET prep LS wqe: link err: "
1794 				"NPORT x%x oxid:x%x ste %d\n",
1795 				ctxp->sid, ctxp->oxid, ctxp->state);
1796 		return NULL;
1797 	}
1798 
1799 	/* Allocate buffer for  command wqe */
1800 	nvmewqe = lpfc_sli_get_iocbq(phba);
1801 	if (nvmewqe == NULL) {
1802 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1803 				"6105 NVMET prep LS wqe: No WQE: "
1804 				"NPORT x%x oxid x%x ste %d\n",
1805 				ctxp->sid, ctxp->oxid, ctxp->state);
1806 		return NULL;
1807 	}
1808 
1809 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1810 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1811 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1812 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1813 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1814 				"6106 NVMET prep LS wqe: No ndlp: "
1815 				"NPORT x%x oxid x%x ste %d\n",
1816 				ctxp->sid, ctxp->oxid, ctxp->state);
1817 		goto nvme_wqe_free_wqeq_exit;
1818 	}
1819 	ctxp->wqeq = nvmewqe;
1820 
1821 	/* prevent preparing wqe with NULL ndlp reference */
1822 	nvmewqe->context1 = lpfc_nlp_get(ndlp);
1823 	if (nvmewqe->context1 == NULL)
1824 		goto nvme_wqe_free_wqeq_exit;
1825 	nvmewqe->context2 = ctxp;
1826 
1827 	wqe = &nvmewqe->wqe;
1828 	memset(wqe, 0, sizeof(union lpfc_wqe));
1829 
1830 	/* Words 0 - 2 */
1831 	wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1832 	wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
1833 	wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
1834 	wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
1835 
1836 	/* Word 3 */
1837 
1838 	/* Word 4 */
1839 
1840 	/* Word 5 */
1841 	bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
1842 	bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
1843 	bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
1844 	bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
1845 	bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
1846 
1847 	/* Word 6 */
1848 	bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
1849 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
1850 	bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
1851 
1852 	/* Word 7 */
1853 	bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
1854 	       CMD_XMIT_SEQUENCE64_WQE);
1855 	bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
1856 	bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
1857 	bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
1858 
1859 	/* Word 8 */
1860 	wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
1861 
1862 	/* Word 9 */
1863 	bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
1864 	/* Needs to be set by caller */
1865 	bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
1866 
1867 	/* Word 10 */
1868 	bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
1869 	bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
1870 	bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
1871 	       LPFC_WQE_LENLOC_WORD12);
1872 	bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
1873 
1874 	/* Word 11 */
1875 	bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
1876 	       LPFC_WQE_CQ_ID_DEFAULT);
1877 	bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
1878 	       OTHER_COMMAND);
1879 
1880 	/* Word 12 */
1881 	wqe->xmit_sequence.xmit_len = rspsize;
1882 
1883 	nvmewqe->retry = 1;
1884 	nvmewqe->vport = phba->pport;
1885 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
1886 	nvmewqe->iocb_flag |= LPFC_IO_NVME_LS;
1887 
1888 	/* Xmit NVMET response to remote NPORT <did> */
1889 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1890 			"6039 Xmit NVMET LS response to remote "
1891 			"NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
1892 			ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
1893 			rspsize);
1894 	return nvmewqe;
1895 
1896 nvme_wqe_free_wqeq_exit:
1897 	nvmewqe->context2 = NULL;
1898 	nvmewqe->context3 = NULL;
1899 	lpfc_sli_release_iocbq(phba, nvmewqe);
1900 	return NULL;
1901 }
1902 
1903 
1904 static struct lpfc_iocbq *
1905 lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
1906 			struct lpfc_nvmet_rcv_ctx *ctxp)
1907 {
1908 	struct nvmefc_tgt_fcp_req *rsp = &ctxp->ctx.fcp_req;
1909 	struct lpfc_nvmet_tgtport *tgtp;
1910 	struct sli4_sge *sgl;
1911 	struct lpfc_nodelist *ndlp;
1912 	struct lpfc_iocbq *nvmewqe;
1913 	struct scatterlist *sgel;
1914 	union lpfc_wqe128 *wqe;
1915 	uint32_t *txrdy;
1916 	dma_addr_t physaddr;
1917 	int i, cnt;
1918 	int xc = 1;
1919 
1920 	if (!lpfc_is_link_up(phba)) {
1921 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1922 				"6107 NVMET prep FCP wqe: link err:"
1923 				"NPORT x%x oxid x%x ste %d\n",
1924 				ctxp->sid, ctxp->oxid, ctxp->state);
1925 		return NULL;
1926 	}
1927 
1928 	ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1929 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
1930 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
1931 	     (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
1932 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1933 				"6108 NVMET prep FCP wqe: no ndlp: "
1934 				"NPORT x%x oxid x%x ste %d\n",
1935 				ctxp->sid, ctxp->oxid, ctxp->state);
1936 		return NULL;
1937 	}
1938 
1939 	if (rsp->sg_cnt > phba->cfg_nvme_seg_cnt) {
1940 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1941 				"6109 NVMET prep FCP wqe: seg cnt err: "
1942 				"NPORT x%x oxid x%x ste %d cnt %d\n",
1943 				ctxp->sid, ctxp->oxid, ctxp->state,
1944 				phba->cfg_nvme_seg_cnt);
1945 		return NULL;
1946 	}
1947 
1948 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1949 	nvmewqe = ctxp->wqeq;
1950 	if (nvmewqe == NULL) {
1951 		/* Allocate buffer for  command wqe */
1952 		nvmewqe = ctxp->ctxbuf->iocbq;
1953 		if (nvmewqe == NULL) {
1954 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1955 					"6110 NVMET prep FCP wqe: No "
1956 					"WQE: NPORT x%x oxid x%x ste %d\n",
1957 					ctxp->sid, ctxp->oxid, ctxp->state);
1958 			return NULL;
1959 		}
1960 		ctxp->wqeq = nvmewqe;
1961 		xc = 0; /* create new XRI */
1962 		nvmewqe->sli4_lxritag = NO_XRI;
1963 		nvmewqe->sli4_xritag = NO_XRI;
1964 	}
1965 
1966 	/* Sanity check */
1967 	if (((ctxp->state == LPFC_NVMET_STE_RCV) &&
1968 	    (ctxp->entry_cnt == 1)) ||
1969 	    (ctxp->state == LPFC_NVMET_STE_DATA)) {
1970 		wqe = (union lpfc_wqe128 *)&nvmewqe->wqe;
1971 	} else {
1972 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1973 				"6111 Wrong state NVMET FCP: %d  cnt %d\n",
1974 				ctxp->state, ctxp->entry_cnt);
1975 		return NULL;
1976 	}
1977 
1978 	sgl  = (struct sli4_sge *)ctxp->ctxbuf->sglq->sgl;
1979 	switch (rsp->op) {
1980 	case NVMET_FCOP_READDATA:
1981 	case NVMET_FCOP_READDATA_RSP:
1982 		/* Words 0 - 2 : The first sg segment */
1983 		sgel = &rsp->sg[0];
1984 		physaddr = sg_dma_address(sgel);
1985 		wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1986 		wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
1987 		wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
1988 		wqe->fcp_tsend.bde.addrHigh =
1989 			cpu_to_le32(putPaddrHigh(physaddr));
1990 
1991 		/* Word 3 */
1992 		wqe->fcp_tsend.payload_offset_len = 0;
1993 
1994 		/* Word 4 */
1995 		wqe->fcp_tsend.relative_offset = ctxp->offset;
1996 
1997 		/* Word 5 */
1998 
1999 		/* Word 6 */
2000 		bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
2001 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2002 		bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
2003 		       nvmewqe->sli4_xritag);
2004 
2005 		/* Word 7 */
2006 		bf_set(wqe_pu, &wqe->fcp_tsend.wqe_com, 1);
2007 		bf_set(wqe_cmnd, &wqe->fcp_tsend.wqe_com, CMD_FCP_TSEND64_WQE);
2008 
2009 		/* Word 8 */
2010 		wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
2011 
2012 		/* Word 9 */
2013 		bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
2014 		bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
2015 
2016 		/* Word 10 */
2017 		bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
2018 		bf_set(wqe_dbde, &wqe->fcp_tsend.wqe_com, 1);
2019 		bf_set(wqe_iod, &wqe->fcp_tsend.wqe_com, LPFC_WQE_IOD_WRITE);
2020 		bf_set(wqe_lenloc, &wqe->fcp_tsend.wqe_com,
2021 		       LPFC_WQE_LENLOC_WORD12);
2022 		bf_set(wqe_ebde_cnt, &wqe->fcp_tsend.wqe_com, 0);
2023 		bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, xc);
2024 		bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
2025 		if (phba->cfg_nvme_oas)
2026 			bf_set(wqe_oas, &wqe->fcp_tsend.wqe_com, 1);
2027 
2028 		/* Word 11 */
2029 		bf_set(wqe_cqid, &wqe->fcp_tsend.wqe_com,
2030 		       LPFC_WQE_CQ_ID_DEFAULT);
2031 		bf_set(wqe_cmd_type, &wqe->fcp_tsend.wqe_com,
2032 		       FCP_COMMAND_TSEND);
2033 
2034 		/* Word 12 */
2035 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2036 
2037 		/* Setup 2 SKIP SGEs */
2038 		sgl->addr_hi = 0;
2039 		sgl->addr_lo = 0;
2040 		sgl->word2 = 0;
2041 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2042 		sgl->word2 = cpu_to_le32(sgl->word2);
2043 		sgl->sge_len = 0;
2044 		sgl++;
2045 		sgl->addr_hi = 0;
2046 		sgl->addr_lo = 0;
2047 		sgl->word2 = 0;
2048 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2049 		sgl->word2 = cpu_to_le32(sgl->word2);
2050 		sgl->sge_len = 0;
2051 		sgl++;
2052 		if (rsp->op == NVMET_FCOP_READDATA_RSP) {
2053 			atomic_inc(&tgtp->xmt_fcp_read_rsp);
2054 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 1);
2055 			if ((ndlp->nlp_flag & NLP_SUPPRESS_RSP) &&
2056 			    (rsp->rsplen == 12)) {
2057 				bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 1);
2058 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
2059 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
2060 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
2061 			} else {
2062 				bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
2063 				bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
2064 				bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
2065 				bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
2066 				       ((rsp->rsplen >> 2) - 1));
2067 				memcpy(&wqe->words[16], rsp->rspaddr,
2068 				       rsp->rsplen);
2069 			}
2070 		} else {
2071 			atomic_inc(&tgtp->xmt_fcp_read);
2072 
2073 			bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
2074 			bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
2075 			bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
2076 			bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
2077 			bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
2078 		}
2079 		break;
2080 
2081 	case NVMET_FCOP_WRITEDATA:
2082 		/* Words 0 - 2 : The first sg segment */
2083 		txrdy = dma_pool_alloc(phba->txrdy_payload_pool,
2084 				       GFP_KERNEL, &physaddr);
2085 		if (!txrdy) {
2086 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2087 					"6041 Bad txrdy buffer: oxid x%x\n",
2088 					ctxp->oxid);
2089 			return NULL;
2090 		}
2091 		ctxp->txrdy = txrdy;
2092 		ctxp->txrdy_phys = physaddr;
2093 		wqe->fcp_treceive.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2094 		wqe->fcp_treceive.bde.tus.f.bdeSize = TXRDY_PAYLOAD_LEN;
2095 		wqe->fcp_treceive.bde.addrLow =
2096 			cpu_to_le32(putPaddrLow(physaddr));
2097 		wqe->fcp_treceive.bde.addrHigh =
2098 			cpu_to_le32(putPaddrHigh(physaddr));
2099 
2100 		/* Word 3 */
2101 		wqe->fcp_treceive.payload_offset_len = TXRDY_PAYLOAD_LEN;
2102 
2103 		/* Word 4 */
2104 		wqe->fcp_treceive.relative_offset = ctxp->offset;
2105 
2106 		/* Word 5 */
2107 
2108 		/* Word 6 */
2109 		bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
2110 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2111 		bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
2112 		       nvmewqe->sli4_xritag);
2113 
2114 		/* Word 7 */
2115 		bf_set(wqe_pu, &wqe->fcp_treceive.wqe_com, 1);
2116 		bf_set(wqe_ar, &wqe->fcp_treceive.wqe_com, 0);
2117 		bf_set(wqe_cmnd, &wqe->fcp_treceive.wqe_com,
2118 		       CMD_FCP_TRECEIVE64_WQE);
2119 
2120 		/* Word 8 */
2121 		wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
2122 
2123 		/* Word 9 */
2124 		bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
2125 		bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
2126 
2127 		/* Word 10 */
2128 		bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
2129 		bf_set(wqe_dbde, &wqe->fcp_treceive.wqe_com, 1);
2130 		bf_set(wqe_iod, &wqe->fcp_treceive.wqe_com, LPFC_WQE_IOD_READ);
2131 		bf_set(wqe_lenloc, &wqe->fcp_treceive.wqe_com,
2132 		       LPFC_WQE_LENLOC_WORD12);
2133 		bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, xc);
2134 		bf_set(wqe_wqes, &wqe->fcp_treceive.wqe_com, 0);
2135 		bf_set(wqe_irsp, &wqe->fcp_treceive.wqe_com, 0);
2136 		bf_set(wqe_irsplen, &wqe->fcp_treceive.wqe_com, 0);
2137 		bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
2138 		if (phba->cfg_nvme_oas)
2139 			bf_set(wqe_oas, &wqe->fcp_treceive.wqe_com, 1);
2140 
2141 		/* Word 11 */
2142 		bf_set(wqe_cqid, &wqe->fcp_treceive.wqe_com,
2143 		       LPFC_WQE_CQ_ID_DEFAULT);
2144 		bf_set(wqe_cmd_type, &wqe->fcp_treceive.wqe_com,
2145 		       FCP_COMMAND_TRECEIVE);
2146 		bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
2147 
2148 		/* Word 12 */
2149 		wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2150 
2151 		/* Setup 1 TXRDY and 1 SKIP SGE */
2152 		txrdy[0] = 0;
2153 		txrdy[1] = cpu_to_be32(rsp->transfer_length);
2154 		txrdy[2] = 0;
2155 
2156 		sgl->addr_hi = putPaddrHigh(physaddr);
2157 		sgl->addr_lo = putPaddrLow(physaddr);
2158 		sgl->word2 = 0;
2159 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2160 		sgl->word2 = cpu_to_le32(sgl->word2);
2161 		sgl->sge_len = cpu_to_le32(TXRDY_PAYLOAD_LEN);
2162 		sgl++;
2163 		sgl->addr_hi = 0;
2164 		sgl->addr_lo = 0;
2165 		sgl->word2 = 0;
2166 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2167 		sgl->word2 = cpu_to_le32(sgl->word2);
2168 		sgl->sge_len = 0;
2169 		sgl++;
2170 		atomic_inc(&tgtp->xmt_fcp_write);
2171 		break;
2172 
2173 	case NVMET_FCOP_RSP:
2174 		/* Words 0 - 2 */
2175 		physaddr = rsp->rspdma;
2176 		wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2177 		wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
2178 		wqe->fcp_trsp.bde.addrLow =
2179 			cpu_to_le32(putPaddrLow(physaddr));
2180 		wqe->fcp_trsp.bde.addrHigh =
2181 			cpu_to_le32(putPaddrHigh(physaddr));
2182 
2183 		/* Word 3 */
2184 		wqe->fcp_trsp.response_len = rsp->rsplen;
2185 
2186 		/* Word 4 */
2187 		wqe->fcp_trsp.rsvd_4_5[0] = 0;
2188 
2189 
2190 		/* Word 5 */
2191 
2192 		/* Word 6 */
2193 		bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
2194 		       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2195 		bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
2196 		       nvmewqe->sli4_xritag);
2197 
2198 		/* Word 7 */
2199 		bf_set(wqe_pu, &wqe->fcp_trsp.wqe_com, 0);
2200 		bf_set(wqe_ag, &wqe->fcp_trsp.wqe_com, 1);
2201 		bf_set(wqe_cmnd, &wqe->fcp_trsp.wqe_com, CMD_FCP_TRSP64_WQE);
2202 
2203 		/* Word 8 */
2204 		wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
2205 
2206 		/* Word 9 */
2207 		bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
2208 		bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
2209 
2210 		/* Word 10 */
2211 		bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
2212 		bf_set(wqe_dbde, &wqe->fcp_trsp.wqe_com, 0);
2213 		bf_set(wqe_iod, &wqe->fcp_trsp.wqe_com, LPFC_WQE_IOD_WRITE);
2214 		bf_set(wqe_lenloc, &wqe->fcp_trsp.wqe_com,
2215 		       LPFC_WQE_LENLOC_WORD3);
2216 		bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, xc);
2217 		bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
2218 		if (phba->cfg_nvme_oas)
2219 			bf_set(wqe_oas, &wqe->fcp_trsp.wqe_com, 1);
2220 
2221 		/* Word 11 */
2222 		bf_set(wqe_cqid, &wqe->fcp_trsp.wqe_com,
2223 		       LPFC_WQE_CQ_ID_DEFAULT);
2224 		bf_set(wqe_cmd_type, &wqe->fcp_trsp.wqe_com,
2225 		       FCP_COMMAND_TRSP);
2226 		bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
2227 
2228 		if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
2229 			/* Good response - all zero's on wire */
2230 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 0);
2231 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 0);
2232 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com, 0);
2233 		} else {
2234 			bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
2235 			bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
2236 			bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
2237 			       ((rsp->rsplen >> 2) - 1));
2238 			memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
2239 		}
2240 
2241 		/* Use rspbuf, NOT sg list */
2242 		rsp->sg_cnt = 0;
2243 		sgl->word2 = 0;
2244 		atomic_inc(&tgtp->xmt_fcp_rsp);
2245 		break;
2246 
2247 	default:
2248 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2249 				"6064 Unknown Rsp Op %d\n",
2250 				rsp->op);
2251 		return NULL;
2252 	}
2253 
2254 	nvmewqe->retry = 1;
2255 	nvmewqe->vport = phba->pport;
2256 	nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2257 	nvmewqe->context1 = ndlp;
2258 
2259 	for (i = 0; i < rsp->sg_cnt; i++) {
2260 		sgel = &rsp->sg[i];
2261 		physaddr = sg_dma_address(sgel);
2262 		cnt = sg_dma_len(sgel);
2263 		sgl->addr_hi = putPaddrHigh(physaddr);
2264 		sgl->addr_lo = putPaddrLow(physaddr);
2265 		sgl->word2 = 0;
2266 		bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2267 		bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
2268 		if ((i+1) == rsp->sg_cnt)
2269 			bf_set(lpfc_sli4_sge_last, sgl, 1);
2270 		sgl->word2 = cpu_to_le32(sgl->word2);
2271 		sgl->sge_len = cpu_to_le32(cnt);
2272 		sgl++;
2273 		ctxp->offset += cnt;
2274 	}
2275 	ctxp->state = LPFC_NVMET_STE_DATA;
2276 	ctxp->entry_cnt++;
2277 	return nvmewqe;
2278 }
2279 
2280 /**
2281  * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
2282  * @phba: Pointer to HBA context object.
2283  * @cmdwqe: Pointer to driver command WQE object.
2284  * @wcqe: Pointer to driver response CQE object.
2285  *
2286  * The function is called from SLI ring event handler with no
2287  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
2288  * The function frees memory resources used for the NVME commands.
2289  **/
2290 static void
2291 lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2292 			     struct lpfc_wcqe_complete *wcqe)
2293 {
2294 	struct lpfc_nvmet_rcv_ctx *ctxp;
2295 	struct lpfc_nvmet_tgtport *tgtp;
2296 	uint32_t status, result;
2297 	unsigned long flags;
2298 	bool released = false;
2299 
2300 	ctxp = cmdwqe->context2;
2301 	status = bf_get(lpfc_wcqe_c_status, wcqe);
2302 	result = wcqe->parameter;
2303 
2304 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2305 	if (ctxp->flag & LPFC_NVMET_ABORT_OP)
2306 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
2307 
2308 	ctxp->state = LPFC_NVMET_STE_DONE;
2309 
2310 	/* Check if we already received a free context call
2311 	 * and we have completed processing an abort situation.
2312 	 */
2313 	spin_lock_irqsave(&ctxp->ctxlock, flags);
2314 	if ((ctxp->flag & LPFC_NVMET_CTX_RLS) &&
2315 	    !(ctxp->flag & LPFC_NVMET_XBUSY)) {
2316 		list_del(&ctxp->list);
2317 		released = true;
2318 	}
2319 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
2320 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
2321 	atomic_inc(&tgtp->xmt_abort_rsp);
2322 
2323 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2324 			"6165 ABORT cmpl: xri x%x flg x%x (%d) "
2325 			"WCQE: %08x %08x %08x %08x\n",
2326 			ctxp->oxid, ctxp->flag, released,
2327 			wcqe->word0, wcqe->total_data_placed,
2328 			result, wcqe->word3);
2329 
2330 	cmdwqe->context2 = NULL;
2331 	cmdwqe->context3 = NULL;
2332 	/*
2333 	 * if transport has released ctx, then can reuse it. Otherwise,
2334 	 * will be recycled by transport release call.
2335 	 */
2336 	if (released)
2337 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
2338 
2339 	/* This is the iocbq for the abort, not the command */
2340 	lpfc_sli_release_iocbq(phba, cmdwqe);
2341 
2342 	/* Since iaab/iaar are NOT set, there is no work left.
2343 	 * For LPFC_NVMET_XBUSY, lpfc_sli4_nvmet_xri_aborted
2344 	 * should have been called already.
2345 	 */
2346 }
2347 
2348 /**
2349  * lpfc_nvmet_unsol_fcp_abort_cmp - Completion handler for ABTS
2350  * @phba: Pointer to HBA context object.
2351  * @cmdwqe: Pointer to driver command WQE object.
2352  * @wcqe: Pointer to driver response CQE object.
2353  *
2354  * The function is called from SLI ring event handler with no
2355  * lock held. This function is the completion handler for NVME ABTS for FCP cmds
2356  * The function frees memory resources used for the NVME commands.
2357  **/
2358 static void
2359 lpfc_nvmet_unsol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2360 			       struct lpfc_wcqe_complete *wcqe)
2361 {
2362 	struct lpfc_nvmet_rcv_ctx *ctxp;
2363 	struct lpfc_nvmet_tgtport *tgtp;
2364 	unsigned long flags;
2365 	uint32_t status, result;
2366 	bool released = false;
2367 
2368 	ctxp = cmdwqe->context2;
2369 	status = bf_get(lpfc_wcqe_c_status, wcqe);
2370 	result = wcqe->parameter;
2371 
2372 	if (!ctxp) {
2373 		/* if context is clear, related io alrady complete */
2374 		lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2375 				"6070 ABTS cmpl: WCQE: %08x %08x %08x %08x\n",
2376 				wcqe->word0, wcqe->total_data_placed,
2377 				result, wcqe->word3);
2378 		return;
2379 	}
2380 
2381 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2382 	if (ctxp->flag & LPFC_NVMET_ABORT_OP)
2383 		atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
2384 
2385 	/* Sanity check */
2386 	if (ctxp->state != LPFC_NVMET_STE_ABORT) {
2387 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2388 				"6112 ABTS Wrong state:%d oxid x%x\n",
2389 				ctxp->state, ctxp->oxid);
2390 	}
2391 
2392 	/* Check if we already received a free context call
2393 	 * and we have completed processing an abort situation.
2394 	 */
2395 	ctxp->state = LPFC_NVMET_STE_DONE;
2396 	spin_lock_irqsave(&ctxp->ctxlock, flags);
2397 	if ((ctxp->flag & LPFC_NVMET_CTX_RLS) &&
2398 	    !(ctxp->flag & LPFC_NVMET_XBUSY)) {
2399 		list_del(&ctxp->list);
2400 		released = true;
2401 	}
2402 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
2403 	spin_unlock_irqrestore(&ctxp->ctxlock, flags);
2404 	atomic_inc(&tgtp->xmt_abort_rsp);
2405 
2406 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2407 			"6316 ABTS cmpl xri x%x flg x%x (%x) "
2408 			"WCQE: %08x %08x %08x %08x\n",
2409 			ctxp->oxid, ctxp->flag, released,
2410 			wcqe->word0, wcqe->total_data_placed,
2411 			result, wcqe->word3);
2412 
2413 	cmdwqe->context2 = NULL;
2414 	cmdwqe->context3 = NULL;
2415 	/*
2416 	 * if transport has released ctx, then can reuse it. Otherwise,
2417 	 * will be recycled by transport release call.
2418 	 */
2419 	if (released)
2420 		lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
2421 
2422 	/* Since iaab/iaar are NOT set, there is no work left.
2423 	 * For LPFC_NVMET_XBUSY, lpfc_sli4_nvmet_xri_aborted
2424 	 * should have been called already.
2425 	 */
2426 }
2427 
2428 /**
2429  * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
2430  * @phba: Pointer to HBA context object.
2431  * @cmdwqe: Pointer to driver command WQE object.
2432  * @wcqe: Pointer to driver response CQE object.
2433  *
2434  * The function is called from SLI ring event handler with no
2435  * lock held. This function is the completion handler for NVME ABTS for LS cmds
2436  * The function frees memory resources used for the NVME commands.
2437  **/
2438 static void
2439 lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2440 			    struct lpfc_wcqe_complete *wcqe)
2441 {
2442 	struct lpfc_nvmet_rcv_ctx *ctxp;
2443 	struct lpfc_nvmet_tgtport *tgtp;
2444 	uint32_t status, result;
2445 
2446 	ctxp = cmdwqe->context2;
2447 	status = bf_get(lpfc_wcqe_c_status, wcqe);
2448 	result = wcqe->parameter;
2449 
2450 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2451 	atomic_inc(&tgtp->xmt_ls_abort_cmpl);
2452 
2453 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2454 			"6083 Abort cmpl: ctx %p WCQE:%08x %08x %08x %08x\n",
2455 			ctxp, wcqe->word0, wcqe->total_data_placed,
2456 			result, wcqe->word3);
2457 
2458 	if (!ctxp) {
2459 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2460 				"6415 NVMET LS Abort No ctx: WCQE: "
2461 				 "%08x %08x %08x %08x\n",
2462 				wcqe->word0, wcqe->total_data_placed,
2463 				result, wcqe->word3);
2464 
2465 		lpfc_sli_release_iocbq(phba, cmdwqe);
2466 		return;
2467 	}
2468 
2469 	if (ctxp->state != LPFC_NVMET_STE_LS_ABORT) {
2470 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2471 				"6416 NVMET LS abort cmpl state mismatch: "
2472 				"oxid x%x: %d %d\n",
2473 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
2474 	}
2475 
2476 	cmdwqe->context2 = NULL;
2477 	cmdwqe->context3 = NULL;
2478 	lpfc_sli_release_iocbq(phba, cmdwqe);
2479 	kfree(ctxp);
2480 }
2481 
2482 static int
2483 lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
2484 			     struct lpfc_nvmet_rcv_ctx *ctxp,
2485 			     uint32_t sid, uint16_t xri)
2486 {
2487 	struct lpfc_nvmet_tgtport *tgtp;
2488 	struct lpfc_iocbq *abts_wqeq;
2489 	union lpfc_wqe *wqe_abts;
2490 	struct lpfc_nodelist *ndlp;
2491 
2492 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2493 			"6067 ABTS: sid %x xri x%x/x%x\n",
2494 			sid, xri, ctxp->wqeq->sli4_xritag);
2495 
2496 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2497 
2498 	ndlp = lpfc_findnode_did(phba->pport, sid);
2499 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2500 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2501 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2502 		atomic_inc(&tgtp->xmt_abort_rsp_error);
2503 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2504 				"6134 Drop ABTS - wrong NDLP state x%x.\n",
2505 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
2506 
2507 		/* No failure to an ABTS request. */
2508 		return 0;
2509 	}
2510 
2511 	abts_wqeq = ctxp->wqeq;
2512 	wqe_abts = &abts_wqeq->wqe;
2513 
2514 	/*
2515 	 * Since we zero the whole WQE, we need to ensure we set the WQE fields
2516 	 * that were initialized in lpfc_sli4_nvmet_alloc.
2517 	 */
2518 	memset(wqe_abts, 0, sizeof(union lpfc_wqe));
2519 
2520 	/* Word 5 */
2521 	bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
2522 	bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
2523 	bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
2524 	bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
2525 	bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
2526 
2527 	/* Word 6 */
2528 	bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
2529 	       phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2530 	bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
2531 	       abts_wqeq->sli4_xritag);
2532 
2533 	/* Word 7 */
2534 	bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
2535 	       CMD_XMIT_SEQUENCE64_WQE);
2536 	bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
2537 	bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
2538 	bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
2539 
2540 	/* Word 8 */
2541 	wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
2542 
2543 	/* Word 9 */
2544 	bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
2545 	/* Needs to be set by caller */
2546 	bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
2547 
2548 	/* Word 10 */
2549 	bf_set(wqe_dbde, &wqe_abts->xmit_sequence.wqe_com, 1);
2550 	bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2551 	bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
2552 	       LPFC_WQE_LENLOC_WORD12);
2553 	bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
2554 	bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
2555 
2556 	/* Word 11 */
2557 	bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
2558 	       LPFC_WQE_CQ_ID_DEFAULT);
2559 	bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
2560 	       OTHER_COMMAND);
2561 
2562 	abts_wqeq->vport = phba->pport;
2563 	abts_wqeq->context1 = ndlp;
2564 	abts_wqeq->context2 = ctxp;
2565 	abts_wqeq->context3 = NULL;
2566 	abts_wqeq->rsvd2 = 0;
2567 	/* hba_wqidx should already be setup from command we are aborting */
2568 	abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2569 	abts_wqeq->iocb.ulpLe = 1;
2570 
2571 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2572 			"6069 Issue ABTS to xri x%x reqtag x%x\n",
2573 			xri, abts_wqeq->iotag);
2574 	return 1;
2575 }
2576 
2577 static int
2578 lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
2579 			       struct lpfc_nvmet_rcv_ctx *ctxp,
2580 			       uint32_t sid, uint16_t xri)
2581 {
2582 	struct lpfc_nvmet_tgtport *tgtp;
2583 	struct lpfc_iocbq *abts_wqeq;
2584 	union lpfc_wqe *abts_wqe;
2585 	struct lpfc_nodelist *ndlp;
2586 	unsigned long flags;
2587 	int rc;
2588 
2589 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2590 	if (!ctxp->wqeq) {
2591 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
2592 		ctxp->wqeq->hba_wqidx = 0;
2593 	}
2594 
2595 	ndlp = lpfc_findnode_did(phba->pport, sid);
2596 	if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2597 	    ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2598 	    (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2599 		atomic_inc(&tgtp->xmt_abort_rsp_error);
2600 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2601 				"6160 Drop ABORT - wrong NDLP state x%x.\n",
2602 				(ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
2603 
2604 		/* No failure to an ABTS request. */
2605 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
2606 		return 0;
2607 	}
2608 
2609 	/* Issue ABTS for this WQE based on iotag */
2610 	ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
2611 	if (!ctxp->abort_wqeq) {
2612 		atomic_inc(&tgtp->xmt_abort_rsp_error);
2613 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2614 				"6161 ABORT failed: No wqeqs: "
2615 				"xri: x%x\n", ctxp->oxid);
2616 		/* No failure to an ABTS request. */
2617 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
2618 		return 0;
2619 	}
2620 	abts_wqeq = ctxp->abort_wqeq;
2621 	abts_wqe = &abts_wqeq->wqe;
2622 	ctxp->state = LPFC_NVMET_STE_ABORT;
2623 
2624 	/* Announce entry to new IO submit field. */
2625 	lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2626 			"6162 ABORT Request to rport DID x%06x "
2627 			"for xri x%x x%x\n",
2628 			ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
2629 
2630 	/* If the hba is getting reset, this flag is set.  It is
2631 	 * cleared when the reset is complete and rings reestablished.
2632 	 */
2633 	spin_lock_irqsave(&phba->hbalock, flags);
2634 	/* driver queued commands are in process of being flushed */
2635 	if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
2636 		spin_unlock_irqrestore(&phba->hbalock, flags);
2637 		atomic_inc(&tgtp->xmt_abort_rsp_error);
2638 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
2639 				"6163 Driver in reset cleanup - flushing "
2640 				"NVME Req now. hba_flag x%x oxid x%x\n",
2641 				phba->hba_flag, ctxp->oxid);
2642 		lpfc_sli_release_iocbq(phba, abts_wqeq);
2643 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
2644 		return 0;
2645 	}
2646 
2647 	/* Outstanding abort is in progress */
2648 	if (abts_wqeq->iocb_flag & LPFC_DRIVER_ABORTED) {
2649 		spin_unlock_irqrestore(&phba->hbalock, flags);
2650 		atomic_inc(&tgtp->xmt_abort_rsp_error);
2651 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
2652 				"6164 Outstanding NVME I/O Abort Request "
2653 				"still pending on oxid x%x\n",
2654 				ctxp->oxid);
2655 		lpfc_sli_release_iocbq(phba, abts_wqeq);
2656 		ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
2657 		return 0;
2658 	}
2659 
2660 	/* Ready - mark outstanding as aborted by driver. */
2661 	abts_wqeq->iocb_flag |= LPFC_DRIVER_ABORTED;
2662 
2663 	/* WQEs are reused.  Clear stale data and set key fields to
2664 	 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
2665 	 */
2666 	memset(abts_wqe, 0, sizeof(union lpfc_wqe));
2667 
2668 	/* word 3 */
2669 	bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
2670 
2671 	/* word 7 */
2672 	bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
2673 	bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
2674 
2675 	/* word 8 - tell the FW to abort the IO associated with this
2676 	 * outstanding exchange ID.
2677 	 */
2678 	abts_wqe->abort_cmd.wqe_com.abort_tag = ctxp->wqeq->sli4_xritag;
2679 
2680 	/* word 9 - this is the iotag for the abts_wqe completion. */
2681 	bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
2682 	       abts_wqeq->iotag);
2683 
2684 	/* word 10 */
2685 	bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
2686 	bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
2687 
2688 	/* word 11 */
2689 	bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
2690 	bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
2691 	bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
2692 
2693 	/* ABTS WQE must go to the same WQ as the WQE to be aborted */
2694 	abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
2695 	abts_wqeq->wqe_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
2696 	abts_wqeq->iocb_cmpl = 0;
2697 	abts_wqeq->iocb_flag |= LPFC_IO_NVME;
2698 	abts_wqeq->context2 = ctxp;
2699 	abts_wqeq->vport = phba->pport;
2700 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_wqeq);
2701 	spin_unlock_irqrestore(&phba->hbalock, flags);
2702 	if (rc == WQE_SUCCESS) {
2703 		atomic_inc(&tgtp->xmt_abort_sol);
2704 		return 0;
2705 	}
2706 
2707 	atomic_inc(&tgtp->xmt_abort_rsp_error);
2708 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
2709 	lpfc_sli_release_iocbq(phba, abts_wqeq);
2710 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2711 			"6166 Failed ABORT issue_wqe with status x%x "
2712 			"for oxid x%x.\n",
2713 			rc, ctxp->oxid);
2714 	return 1;
2715 }
2716 
2717 
2718 static int
2719 lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
2720 				 struct lpfc_nvmet_rcv_ctx *ctxp,
2721 				 uint32_t sid, uint16_t xri)
2722 {
2723 	struct lpfc_nvmet_tgtport *tgtp;
2724 	struct lpfc_iocbq *abts_wqeq;
2725 	unsigned long flags;
2726 	int rc;
2727 
2728 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2729 	if (!ctxp->wqeq) {
2730 		ctxp->wqeq = ctxp->ctxbuf->iocbq;
2731 		ctxp->wqeq->hba_wqidx = 0;
2732 	}
2733 
2734 	if (ctxp->state == LPFC_NVMET_STE_FREE) {
2735 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2736 				"6417 NVMET ABORT ctx freed %d %d oxid x%x\n",
2737 				ctxp->state, ctxp->entry_cnt, ctxp->oxid);
2738 		rc = WQE_BUSY;
2739 		goto aerr;
2740 	}
2741 	ctxp->state = LPFC_NVMET_STE_ABORT;
2742 	ctxp->entry_cnt++;
2743 	rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
2744 	if (rc == 0)
2745 		goto aerr;
2746 
2747 	spin_lock_irqsave(&phba->hbalock, flags);
2748 	abts_wqeq = ctxp->wqeq;
2749 	abts_wqeq->wqe_cmpl = lpfc_nvmet_unsol_fcp_abort_cmp;
2750 	abts_wqeq->iocb_cmpl = NULL;
2751 	abts_wqeq->iocb_flag |= LPFC_IO_NVMET;
2752 	rc = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_wqeq);
2753 	spin_unlock_irqrestore(&phba->hbalock, flags);
2754 	if (rc == WQE_SUCCESS) {
2755 		return 0;
2756 	}
2757 
2758 aerr:
2759 	ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
2760 	atomic_inc(&tgtp->xmt_abort_rsp_error);
2761 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2762 			"6135 Failed to Issue ABTS for oxid x%x. Status x%x\n",
2763 			ctxp->oxid, rc);
2764 	return 1;
2765 }
2766 
2767 static int
2768 lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *phba,
2769 				struct lpfc_nvmet_rcv_ctx *ctxp,
2770 				uint32_t sid, uint16_t xri)
2771 {
2772 	struct lpfc_nvmet_tgtport *tgtp;
2773 	struct lpfc_iocbq *abts_wqeq;
2774 	union lpfc_wqe *wqe_abts;
2775 	unsigned long flags;
2776 	int rc;
2777 
2778 	if ((ctxp->state == LPFC_NVMET_STE_LS_RCV && ctxp->entry_cnt == 1) ||
2779 	    (ctxp->state == LPFC_NVMET_STE_LS_RSP && ctxp->entry_cnt == 2)) {
2780 		ctxp->state = LPFC_NVMET_STE_LS_ABORT;
2781 		ctxp->entry_cnt++;
2782 	} else {
2783 		lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2784 				"6418 NVMET LS abort state mismatch "
2785 				"IO x%x: %d %d\n",
2786 				ctxp->oxid, ctxp->state, ctxp->entry_cnt);
2787 		ctxp->state = LPFC_NVMET_STE_LS_ABORT;
2788 	}
2789 
2790 	tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2791 	if (!ctxp->wqeq) {
2792 		/* Issue ABTS for this WQE based on iotag */
2793 		ctxp->wqeq = lpfc_sli_get_iocbq(phba);
2794 		if (!ctxp->wqeq) {
2795 			lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2796 					"6068 Abort failed: No wqeqs: "
2797 					"xri: x%x\n", xri);
2798 			/* No failure to an ABTS request. */
2799 			kfree(ctxp);
2800 			return 0;
2801 		}
2802 	}
2803 	abts_wqeq = ctxp->wqeq;
2804 	wqe_abts = &abts_wqeq->wqe;
2805 
2806 	if (lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri) == 0) {
2807 		rc = WQE_BUSY;
2808 		goto out;
2809 	}
2810 
2811 	spin_lock_irqsave(&phba->hbalock, flags);
2812 	abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
2813 	abts_wqeq->iocb_cmpl = 0;
2814 	abts_wqeq->iocb_flag |=  LPFC_IO_NVME_LS;
2815 	rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, abts_wqeq);
2816 	spin_unlock_irqrestore(&phba->hbalock, flags);
2817 	if (rc == WQE_SUCCESS) {
2818 		atomic_inc(&tgtp->xmt_abort_unsol);
2819 		return 0;
2820 	}
2821 out:
2822 	atomic_inc(&tgtp->xmt_abort_rsp_error);
2823 	abts_wqeq->context2 = NULL;
2824 	abts_wqeq->context3 = NULL;
2825 	lpfc_sli_release_iocbq(phba, abts_wqeq);
2826 	kfree(ctxp);
2827 	lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2828 			"6056 Failed to Issue ABTS. Status x%x\n", rc);
2829 	return 0;
2830 }
2831