1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channsel Host Bus Adapters. *
4 * Copyright (C) 2017-2019 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
6 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
7 * EMULEX and SLI are trademarks of Emulex. *
9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
11 * This program is free software; you can redistribute it and/or *
12 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
22 ********************************************************************/
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/delay.h>
27 #include <asm/unaligned.h>
28 #include <linux/crc-t10dif.h>
29 #include <net/checksum.h>
31 #include <scsi/scsi.h>
32 #include <scsi/scsi_device.h>
33 #include <scsi/scsi_eh.h>
34 #include <scsi/scsi_host.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <scsi/fc/fc_fs.h>
39 #include <linux/nvme.h>
40 #include <linux/nvme-fc-driver.h>
41 #include <linux/nvme-fc.h>
43 #include "lpfc_version.h"
47 #include "lpfc_sli4.h"
49 #include "lpfc_disc.h"
51 #include "lpfc_scsi.h"
52 #include "lpfc_nvme.h"
53 #include "lpfc_nvmet.h"
54 #include "lpfc_logmsg.h"
55 #include "lpfc_crtn.h"
56 #include "lpfc_vport.h"
57 #include "lpfc_debugfs.h"
59 static struct lpfc_iocbq *lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *,
60 struct lpfc_nvmet_rcv_ctx *,
63 static struct lpfc_iocbq *lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *,
64 struct lpfc_nvmet_rcv_ctx *);
65 static int lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *,
66 struct lpfc_nvmet_rcv_ctx *,
68 static int lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *,
69 struct lpfc_nvmet_rcv_ctx *,
71 static int lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *,
72 struct lpfc_nvmet_rcv_ctx *,
74 static void lpfc_nvmet_wqfull_flush(struct lpfc_hba *, struct lpfc_queue *,
75 struct lpfc_nvmet_rcv_ctx *);
76 static void lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *);
78 static void lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf);
80 static union lpfc_wqe128 lpfc_tsend_cmd_template;
81 static union lpfc_wqe128 lpfc_treceive_cmd_template;
82 static union lpfc_wqe128 lpfc_trsp_cmd_template;
84 /* Setup WQE templates for NVME IOs */
86 lpfc_nvmet_cmd_template(void)
88 union lpfc_wqe128 *wqe;
91 wqe = &lpfc_tsend_cmd_template;
92 memset(wqe, 0, sizeof(union lpfc_wqe128));
94 /* Word 0, 1, 2 - BDE is variable */
96 /* Word 3 - payload_offset_len is zero */
98 /* Word 4 - relative_offset is variable */
100 /* Word 5 - is zero */
102 /* Word 6 - ctxt_tag, xri_tag is variable */
104 /* Word 7 - wqe_ar is variable */
105 bf_set(wqe_cmnd, &wqe->fcp_tsend.wqe_com, CMD_FCP_TSEND64_WQE);
106 bf_set(wqe_pu, &wqe->fcp_tsend.wqe_com, PARM_REL_OFF);
107 bf_set(wqe_class, &wqe->fcp_tsend.wqe_com, CLASS3);
108 bf_set(wqe_ct, &wqe->fcp_tsend.wqe_com, SLI4_CT_RPI);
109 bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 1);
111 /* Word 8 - abort_tag is variable */
113 /* Word 9 - reqtag, rcvoxid is variable */
115 /* Word 10 - wqes, xc is variable */
116 bf_set(wqe_nvme, &wqe->fcp_tsend.wqe_com, 1);
117 bf_set(wqe_dbde, &wqe->fcp_tsend.wqe_com, 1);
118 bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 0);
119 bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 1);
120 bf_set(wqe_iod, &wqe->fcp_tsend.wqe_com, LPFC_WQE_IOD_WRITE);
121 bf_set(wqe_lenloc, &wqe->fcp_tsend.wqe_com, LPFC_WQE_LENLOC_WORD12);
123 /* Word 11 - sup, irsp, irsplen is variable */
124 bf_set(wqe_cmd_type, &wqe->fcp_tsend.wqe_com, FCP_COMMAND_TSEND);
125 bf_set(wqe_cqid, &wqe->fcp_tsend.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
126 bf_set(wqe_sup, &wqe->fcp_tsend.wqe_com, 0);
127 bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 0);
128 bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com, 0);
129 bf_set(wqe_pbde, &wqe->fcp_tsend.wqe_com, 0);
131 /* Word 12 - fcp_data_len is variable */
133 /* Word 13, 14, 15 - PBDE is zero */
135 /* TRECEIVE template */
136 wqe = &lpfc_treceive_cmd_template;
137 memset(wqe, 0, sizeof(union lpfc_wqe128));
139 /* Word 0, 1, 2 - BDE is variable */
142 wqe->fcp_treceive.payload_offset_len = TXRDY_PAYLOAD_LEN;
144 /* Word 4 - relative_offset is variable */
146 /* Word 5 - is zero */
148 /* Word 6 - ctxt_tag, xri_tag is variable */
151 bf_set(wqe_cmnd, &wqe->fcp_treceive.wqe_com, CMD_FCP_TRECEIVE64_WQE);
152 bf_set(wqe_pu, &wqe->fcp_treceive.wqe_com, PARM_REL_OFF);
153 bf_set(wqe_class, &wqe->fcp_treceive.wqe_com, CLASS3);
154 bf_set(wqe_ct, &wqe->fcp_treceive.wqe_com, SLI4_CT_RPI);
155 bf_set(wqe_ar, &wqe->fcp_treceive.wqe_com, 0);
157 /* Word 8 - abort_tag is variable */
159 /* Word 9 - reqtag, rcvoxid is variable */
161 /* Word 10 - xc is variable */
162 bf_set(wqe_dbde, &wqe->fcp_treceive.wqe_com, 1);
163 bf_set(wqe_wqes, &wqe->fcp_treceive.wqe_com, 0);
164 bf_set(wqe_nvme, &wqe->fcp_treceive.wqe_com, 1);
165 bf_set(wqe_iod, &wqe->fcp_treceive.wqe_com, LPFC_WQE_IOD_READ);
166 bf_set(wqe_lenloc, &wqe->fcp_treceive.wqe_com, LPFC_WQE_LENLOC_WORD12);
167 bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 1);
169 /* Word 11 - pbde is variable */
170 bf_set(wqe_cmd_type, &wqe->fcp_treceive.wqe_com, FCP_COMMAND_TRECEIVE);
171 bf_set(wqe_cqid, &wqe->fcp_treceive.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
172 bf_set(wqe_sup, &wqe->fcp_treceive.wqe_com, 0);
173 bf_set(wqe_irsp, &wqe->fcp_treceive.wqe_com, 0);
174 bf_set(wqe_irsplen, &wqe->fcp_treceive.wqe_com, 0);
175 bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 1);
177 /* Word 12 - fcp_data_len is variable */
179 /* Word 13, 14, 15 - PBDE is variable */
182 wqe = &lpfc_trsp_cmd_template;
183 memset(wqe, 0, sizeof(union lpfc_wqe128));
185 /* Word 0, 1, 2 - BDE is variable */
187 /* Word 3 - response_len is variable */
189 /* Word 4, 5 - is zero */
191 /* Word 6 - ctxt_tag, xri_tag is variable */
194 bf_set(wqe_cmnd, &wqe->fcp_trsp.wqe_com, CMD_FCP_TRSP64_WQE);
195 bf_set(wqe_pu, &wqe->fcp_trsp.wqe_com, PARM_UNUSED);
196 bf_set(wqe_class, &wqe->fcp_trsp.wqe_com, CLASS3);
197 bf_set(wqe_ct, &wqe->fcp_trsp.wqe_com, SLI4_CT_RPI);
198 bf_set(wqe_ag, &wqe->fcp_trsp.wqe_com, 1); /* wqe_ar */
200 /* Word 8 - abort_tag is variable */
202 /* Word 9 - reqtag is variable */
204 /* Word 10 wqes, xc is variable */
205 bf_set(wqe_dbde, &wqe->fcp_trsp.wqe_com, 1);
206 bf_set(wqe_nvme, &wqe->fcp_trsp.wqe_com, 1);
207 bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 0);
208 bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 0);
209 bf_set(wqe_iod, &wqe->fcp_trsp.wqe_com, LPFC_WQE_IOD_NONE);
210 bf_set(wqe_lenloc, &wqe->fcp_trsp.wqe_com, LPFC_WQE_LENLOC_WORD3);
212 /* Word 11 irsp, irsplen is variable */
213 bf_set(wqe_cmd_type, &wqe->fcp_trsp.wqe_com, FCP_COMMAND_TRSP);
214 bf_set(wqe_cqid, &wqe->fcp_trsp.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
215 bf_set(wqe_sup, &wqe->fcp_trsp.wqe_com, 0);
216 bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 0);
217 bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com, 0);
218 bf_set(wqe_pbde, &wqe->fcp_trsp.wqe_com, 0);
220 /* Word 12, 13, 14, 15 - is zero */
224 lpfc_nvmet_defer_release(struct lpfc_hba *phba, struct lpfc_nvmet_rcv_ctx *ctxp)
226 lockdep_assert_held(&ctxp->ctxlock);
228 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
229 "6313 NVMET Defer ctx release xri x%x flg x%x\n",
230 ctxp->oxid, ctxp->flag);
232 if (ctxp->flag & LPFC_NVMET_CTX_RLS)
235 ctxp->flag |= LPFC_NVMET_CTX_RLS;
236 spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
237 list_add_tail(&ctxp->list, &phba->sli4_hba.lpfc_abts_nvmet_ctx_list);
238 spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
242 * lpfc_nvmet_xmt_ls_rsp_cmp - Completion handler for LS Response
243 * @phba: Pointer to HBA context object.
244 * @cmdwqe: Pointer to driver command WQE object.
245 * @wcqe: Pointer to driver response CQE object.
247 * The function is called from SLI ring event handler with no
248 * lock held. This function is the completion handler for NVME LS commands
249 * The function frees memory resources used for the NVME commands.
252 lpfc_nvmet_xmt_ls_rsp_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
253 struct lpfc_wcqe_complete *wcqe)
255 struct lpfc_nvmet_tgtport *tgtp;
256 struct nvmefc_tgt_ls_req *rsp;
257 struct lpfc_nvmet_rcv_ctx *ctxp;
258 uint32_t status, result;
260 status = bf_get(lpfc_wcqe_c_status, wcqe);
261 result = wcqe->parameter;
262 ctxp = cmdwqe->context2;
264 if (ctxp->state != LPFC_NVMET_STE_LS_RSP || ctxp->entry_cnt != 2) {
265 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
266 "6410 NVMET LS cmpl state mismatch IO x%x: "
268 ctxp->oxid, ctxp->state, ctxp->entry_cnt);
271 if (!phba->targetport)
274 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
278 atomic_inc(&tgtp->xmt_ls_rsp_error);
279 if (result == IOERR_ABORT_REQUESTED)
280 atomic_inc(&tgtp->xmt_ls_rsp_aborted);
281 if (bf_get(lpfc_wcqe_c_xb, wcqe))
282 atomic_inc(&tgtp->xmt_ls_rsp_xb_set);
284 atomic_inc(&tgtp->xmt_ls_rsp_cmpl);
289 rsp = &ctxp->ctx.ls_req;
291 lpfc_nvmeio_data(phba, "NVMET LS CMPL: xri x%x stat x%x result x%x\n",
292 ctxp->oxid, status, result);
294 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
295 "6038 NVMET LS rsp cmpl: %d %d oxid x%x\n",
296 status, result, ctxp->oxid);
298 lpfc_nlp_put(cmdwqe->context1);
299 cmdwqe->context2 = NULL;
300 cmdwqe->context3 = NULL;
301 lpfc_sli_release_iocbq(phba, cmdwqe);
307 * lpfc_nvmet_ctxbuf_post - Repost a NVMET RQ DMA buffer and clean up context
308 * @phba: HBA buffer is associated with
309 * @ctxp: context to clean up
310 * @mp: Buffer to free
312 * Description: Frees the given DMA buffer in the appropriate way given by
313 * reposting it to its associated RQ so it can be reused.
315 * Notes: Takes phba->hbalock. Can be called with or without other locks held.
320 lpfc_nvmet_ctxbuf_post(struct lpfc_hba *phba, struct lpfc_nvmet_ctxbuf *ctx_buf)
322 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
323 struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
324 struct lpfc_nvmet_tgtport *tgtp;
325 struct fc_frame_header *fc_hdr;
326 struct rqb_dmabuf *nvmebuf;
327 struct lpfc_nvmet_ctx_info *infop;
328 uint32_t size, oxid, sid;
333 dma_pool_free(phba->txrdy_payload_pool, ctxp->txrdy,
336 ctxp->txrdy_phys = 0;
339 if (ctxp->state == LPFC_NVMET_STE_FREE) {
340 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
341 "6411 NVMET free, already free IO x%x: %d %d\n",
342 ctxp->oxid, ctxp->state, ctxp->entry_cnt);
345 if (ctxp->rqb_buffer) {
346 nvmebuf = ctxp->rqb_buffer;
347 spin_lock_irqsave(&ctxp->ctxlock, iflag);
348 ctxp->rqb_buffer = NULL;
349 if (ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) {
350 ctxp->flag &= ~LPFC_NVMET_CTX_REUSE_WQ;
351 spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
352 nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
354 spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
355 lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
358 ctxp->state = LPFC_NVMET_STE_FREE;
360 spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
361 if (phba->sli4_hba.nvmet_io_wait_cnt) {
362 list_remove_head(&phba->sli4_hba.lpfc_nvmet_io_wait_list,
363 nvmebuf, struct rqb_dmabuf,
365 phba->sli4_hba.nvmet_io_wait_cnt--;
366 spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
369 fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
370 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
371 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
372 size = nvmebuf->bytes_recv;
373 sid = sli4_sid_from_fc_hdr(fc_hdr);
375 ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
383 ctxp->state = LPFC_NVMET_STE_RCV;
386 ctxp->ctxbuf = ctx_buf;
387 ctxp->rqb_buffer = (void *)nvmebuf;
388 spin_lock_init(&ctxp->ctxlock);
390 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
391 if (ctxp->ts_cmd_nvme) {
392 ctxp->ts_cmd_nvme = ktime_get_ns();
393 ctxp->ts_nvme_data = 0;
394 ctxp->ts_data_wqput = 0;
395 ctxp->ts_isr_data = 0;
396 ctxp->ts_data_nvme = 0;
397 ctxp->ts_nvme_status = 0;
398 ctxp->ts_status_wqput = 0;
399 ctxp->ts_isr_status = 0;
400 ctxp->ts_status_nvme = 0;
403 atomic_inc(&tgtp->rcv_fcp_cmd_in);
405 /* flag new work queued, replacement buffer has already
408 spin_lock_irqsave(&ctxp->ctxlock, iflag);
409 ctxp->flag |= LPFC_NVMET_CTX_REUSE_WQ;
410 spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
412 if (!queue_work(phba->wq, &ctx_buf->defer_work)) {
413 atomic_inc(&tgtp->rcv_fcp_cmd_drop);
414 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
415 "6181 Unable to queue deferred work "
417 "FCP Drop IO [x%x x%x x%x]\n",
419 atomic_read(&tgtp->rcv_fcp_cmd_in),
420 atomic_read(&tgtp->rcv_fcp_cmd_out),
421 atomic_read(&tgtp->xmt_fcp_release));
423 spin_lock_irqsave(&ctxp->ctxlock, iflag);
424 lpfc_nvmet_defer_release(phba, ctxp);
425 spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
426 lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, sid, oxid);
430 spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
433 * Use the CPU context list, from the MRQ the IO was received on
434 * (ctxp->idx), to save context structure.
436 cpu = raw_smp_processor_id();
437 infop = lpfc_get_ctx_list(phba, cpu, ctxp->idx);
438 spin_lock_irqsave(&infop->nvmet_ctx_list_lock, iflag);
439 list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
440 infop->nvmet_ctx_list_cnt++;
441 spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, iflag);
445 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
447 lpfc_nvmet_ktime(struct lpfc_hba *phba,
448 struct lpfc_nvmet_rcv_ctx *ctxp)
450 uint64_t seg1, seg2, seg3, seg4, seg5;
451 uint64_t seg6, seg7, seg8, seg9, seg10;
454 if (!ctxp->ts_isr_cmd || !ctxp->ts_cmd_nvme ||
455 !ctxp->ts_nvme_data || !ctxp->ts_data_wqput ||
456 !ctxp->ts_isr_data || !ctxp->ts_data_nvme ||
457 !ctxp->ts_nvme_status || !ctxp->ts_status_wqput ||
458 !ctxp->ts_isr_status || !ctxp->ts_status_nvme)
461 if (ctxp->ts_status_nvme < ctxp->ts_isr_cmd)
463 if (ctxp->ts_isr_cmd > ctxp->ts_cmd_nvme)
465 if (ctxp->ts_cmd_nvme > ctxp->ts_nvme_data)
467 if (ctxp->ts_nvme_data > ctxp->ts_data_wqput)
469 if (ctxp->ts_data_wqput > ctxp->ts_isr_data)
471 if (ctxp->ts_isr_data > ctxp->ts_data_nvme)
473 if (ctxp->ts_data_nvme > ctxp->ts_nvme_status)
475 if (ctxp->ts_nvme_status > ctxp->ts_status_wqput)
477 if (ctxp->ts_status_wqput > ctxp->ts_isr_status)
479 if (ctxp->ts_isr_status > ctxp->ts_status_nvme)
482 * Segment 1 - Time from FCP command received by MSI-X ISR
483 * to FCP command is passed to NVME Layer.
484 * Segment 2 - Time from FCP command payload handed
485 * off to NVME Layer to Driver receives a Command op
487 * Segment 3 - Time from Driver receives a Command op
488 * from NVME Layer to Command is put on WQ.
489 * Segment 4 - Time from Driver WQ put is done
490 * to MSI-X ISR for Command cmpl.
491 * Segment 5 - Time from MSI-X ISR for Command cmpl to
492 * Command cmpl is passed to NVME Layer.
493 * Segment 6 - Time from Command cmpl is passed to NVME
494 * Layer to Driver receives a RSP op from NVME Layer.
495 * Segment 7 - Time from Driver receives a RSP op from
496 * NVME Layer to WQ put is done on TRSP FCP Status.
497 * Segment 8 - Time from Driver WQ put is done on TRSP
498 * FCP Status to MSI-X ISR for TRSP cmpl.
499 * Segment 9 - Time from MSI-X ISR for TRSP cmpl to
500 * TRSP cmpl is passed to NVME Layer.
501 * Segment 10 - Time from FCP command received by
502 * MSI-X ISR to command is completed on wire.
503 * (Segments 1 thru 8) for READDATA / WRITEDATA
504 * (Segments 1 thru 4) for READDATA_RSP
506 seg1 = ctxp->ts_cmd_nvme - ctxp->ts_isr_cmd;
509 seg2 = ctxp->ts_nvme_data - ctxp->ts_isr_cmd;
515 seg3 = ctxp->ts_data_wqput - ctxp->ts_isr_cmd;
521 seg4 = ctxp->ts_isr_data - ctxp->ts_isr_cmd;
527 seg5 = ctxp->ts_data_nvme - ctxp->ts_isr_cmd;
534 /* For auto rsp commands seg6 thru seg10 will be 0 */
535 if (ctxp->ts_nvme_status > ctxp->ts_data_nvme) {
536 seg6 = ctxp->ts_nvme_status - ctxp->ts_isr_cmd;
542 seg7 = ctxp->ts_status_wqput - ctxp->ts_isr_cmd;
548 seg8 = ctxp->ts_isr_status - ctxp->ts_isr_cmd;
554 seg9 = ctxp->ts_status_nvme - ctxp->ts_isr_cmd;
560 if (ctxp->ts_isr_status < ctxp->ts_isr_cmd)
562 seg10 = (ctxp->ts_isr_status -
565 if (ctxp->ts_isr_data < ctxp->ts_isr_cmd)
571 seg10 = (ctxp->ts_isr_data - ctxp->ts_isr_cmd);
574 phba->ktime_seg1_total += seg1;
575 if (seg1 < phba->ktime_seg1_min)
576 phba->ktime_seg1_min = seg1;
577 else if (seg1 > phba->ktime_seg1_max)
578 phba->ktime_seg1_max = seg1;
580 phba->ktime_seg2_total += seg2;
581 if (seg2 < phba->ktime_seg2_min)
582 phba->ktime_seg2_min = seg2;
583 else if (seg2 > phba->ktime_seg2_max)
584 phba->ktime_seg2_max = seg2;
586 phba->ktime_seg3_total += seg3;
587 if (seg3 < phba->ktime_seg3_min)
588 phba->ktime_seg3_min = seg3;
589 else if (seg3 > phba->ktime_seg3_max)
590 phba->ktime_seg3_max = seg3;
592 phba->ktime_seg4_total += seg4;
593 if (seg4 < phba->ktime_seg4_min)
594 phba->ktime_seg4_min = seg4;
595 else if (seg4 > phba->ktime_seg4_max)
596 phba->ktime_seg4_max = seg4;
598 phba->ktime_seg5_total += seg5;
599 if (seg5 < phba->ktime_seg5_min)
600 phba->ktime_seg5_min = seg5;
601 else if (seg5 > phba->ktime_seg5_max)
602 phba->ktime_seg5_max = seg5;
604 phba->ktime_data_samples++;
608 phba->ktime_seg6_total += seg6;
609 if (seg6 < phba->ktime_seg6_min)
610 phba->ktime_seg6_min = seg6;
611 else if (seg6 > phba->ktime_seg6_max)
612 phba->ktime_seg6_max = seg6;
614 phba->ktime_seg7_total += seg7;
615 if (seg7 < phba->ktime_seg7_min)
616 phba->ktime_seg7_min = seg7;
617 else if (seg7 > phba->ktime_seg7_max)
618 phba->ktime_seg7_max = seg7;
620 phba->ktime_seg8_total += seg8;
621 if (seg8 < phba->ktime_seg8_min)
622 phba->ktime_seg8_min = seg8;
623 else if (seg8 > phba->ktime_seg8_max)
624 phba->ktime_seg8_max = seg8;
626 phba->ktime_seg9_total += seg9;
627 if (seg9 < phba->ktime_seg9_min)
628 phba->ktime_seg9_min = seg9;
629 else if (seg9 > phba->ktime_seg9_max)
630 phba->ktime_seg9_max = seg9;
632 phba->ktime_seg10_total += seg10;
633 if (seg10 < phba->ktime_seg10_min)
634 phba->ktime_seg10_min = seg10;
635 else if (seg10 > phba->ktime_seg10_max)
636 phba->ktime_seg10_max = seg10;
637 phba->ktime_status_samples++;
642 * lpfc_nvmet_xmt_fcp_op_cmp - Completion handler for FCP Response
643 * @phba: Pointer to HBA context object.
644 * @cmdwqe: Pointer to driver command WQE object.
645 * @wcqe: Pointer to driver response CQE object.
647 * The function is called from SLI ring event handler with no
648 * lock held. This function is the completion handler for NVME FCP commands
649 * The function frees memory resources used for the NVME commands.
652 lpfc_nvmet_xmt_fcp_op_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
653 struct lpfc_wcqe_complete *wcqe)
655 struct lpfc_nvmet_tgtport *tgtp;
656 struct nvmefc_tgt_fcp_req *rsp;
657 struct lpfc_nvmet_rcv_ctx *ctxp;
658 uint32_t status, result, op, start_clean, logerr;
659 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
663 ctxp = cmdwqe->context2;
664 ctxp->flag &= ~LPFC_NVMET_IO_INP;
666 rsp = &ctxp->ctx.fcp_req;
669 status = bf_get(lpfc_wcqe_c_status, wcqe);
670 result = wcqe->parameter;
672 if (phba->targetport)
673 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
677 lpfc_nvmeio_data(phba, "NVMET FCP CMPL: xri x%x op x%x status x%x\n",
678 ctxp->oxid, op, status);
681 rsp->fcp_error = NVME_SC_DATA_XFER_ERROR;
682 rsp->transferred_length = 0;
684 atomic_inc(&tgtp->xmt_fcp_rsp_error);
685 if (result == IOERR_ABORT_REQUESTED)
686 atomic_inc(&tgtp->xmt_fcp_rsp_aborted);
689 logerr = LOG_NVME_IOERR;
691 /* pick up SLI4 exhange busy condition */
692 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
693 ctxp->flag |= LPFC_NVMET_XBUSY;
694 logerr |= LOG_NVME_ABTS;
696 atomic_inc(&tgtp->xmt_fcp_rsp_xb_set);
699 ctxp->flag &= ~LPFC_NVMET_XBUSY;
702 lpfc_printf_log(phba, KERN_INFO, logerr,
703 "6315 IO Error Cmpl xri x%x: %x/%x XBUSY:x%x\n",
704 ctxp->oxid, status, result, ctxp->flag);
707 rsp->fcp_error = NVME_SC_SUCCESS;
708 if (op == NVMET_FCOP_RSP)
709 rsp->transferred_length = rsp->rsplen;
711 rsp->transferred_length = rsp->transfer_length;
713 atomic_inc(&tgtp->xmt_fcp_rsp_cmpl);
716 if ((op == NVMET_FCOP_READDATA_RSP) ||
717 (op == NVMET_FCOP_RSP)) {
719 ctxp->state = LPFC_NVMET_STE_DONE;
722 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
723 if (ctxp->ts_cmd_nvme) {
724 if (rsp->op == NVMET_FCOP_READDATA_RSP) {
726 cmdwqe->isr_timestamp;
729 ctxp->ts_nvme_status =
731 ctxp->ts_status_wqput =
733 ctxp->ts_isr_status =
735 ctxp->ts_status_nvme =
738 ctxp->ts_isr_status =
739 cmdwqe->isr_timestamp;
740 ctxp->ts_status_nvme =
746 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
747 if (ctxp->ts_cmd_nvme)
748 lpfc_nvmet_ktime(phba, ctxp);
750 /* lpfc_nvmet_xmt_fcp_release() will recycle the context */
753 start_clean = offsetof(struct lpfc_iocbq, iocb_flag);
754 memset(((char *)cmdwqe) + start_clean, 0,
755 (sizeof(struct lpfc_iocbq) - start_clean));
756 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
757 if (ctxp->ts_cmd_nvme) {
758 ctxp->ts_isr_data = cmdwqe->isr_timestamp;
759 ctxp->ts_data_nvme = ktime_get_ns();
764 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
765 if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
766 id = raw_smp_processor_id();
767 if (id < LPFC_CHECK_CPU_CNT) {
769 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
770 "6704 CPU Check cmdcmpl: "
771 "cpu %d expect %d\n",
773 phba->sli4_hba.hdwq[rsp->hwqid].cpucheck_cmpl_io[id]++;
780 lpfc_nvmet_xmt_ls_rsp(struct nvmet_fc_target_port *tgtport,
781 struct nvmefc_tgt_ls_req *rsp)
783 struct lpfc_nvmet_rcv_ctx *ctxp =
784 container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.ls_req);
785 struct lpfc_hba *phba = ctxp->phba;
786 struct hbq_dmabuf *nvmebuf =
787 (struct hbq_dmabuf *)ctxp->rqb_buffer;
788 struct lpfc_iocbq *nvmewqeq;
789 struct lpfc_nvmet_tgtport *nvmep = tgtport->private;
790 struct lpfc_dmabuf dmabuf;
791 struct ulp_bde64 bpl;
794 if (phba->pport->load_flag & FC_UNLOADING)
797 if (phba->pport->load_flag & FC_UNLOADING)
800 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
801 "6023 NVMET LS rsp oxid x%x\n", ctxp->oxid);
803 if ((ctxp->state != LPFC_NVMET_STE_LS_RCV) ||
804 (ctxp->entry_cnt != 1)) {
805 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
806 "6412 NVMET LS rsp state mismatch "
808 ctxp->oxid, ctxp->state, ctxp->entry_cnt);
810 ctxp->state = LPFC_NVMET_STE_LS_RSP;
813 nvmewqeq = lpfc_nvmet_prep_ls_wqe(phba, ctxp, rsp->rspdma,
815 if (nvmewqeq == NULL) {
816 atomic_inc(&nvmep->xmt_ls_drop);
817 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
818 "6150 LS Drop IO x%x: Prep\n",
820 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
821 atomic_inc(&nvmep->xmt_ls_abort);
822 lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp,
823 ctxp->sid, ctxp->oxid);
827 /* Save numBdes for bpl2sgl */
829 nvmewqeq->hba_wqidx = 0;
830 nvmewqeq->context3 = &dmabuf;
832 bpl.addrLow = nvmewqeq->wqe.xmit_sequence.bde.addrLow;
833 bpl.addrHigh = nvmewqeq->wqe.xmit_sequence.bde.addrHigh;
834 bpl.tus.f.bdeSize = rsp->rsplen;
835 bpl.tus.f.bdeFlags = 0;
836 bpl.tus.w = le32_to_cpu(bpl.tus.w);
838 nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_rsp_cmp;
839 nvmewqeq->iocb_cmpl = NULL;
840 nvmewqeq->context2 = ctxp;
842 lpfc_nvmeio_data(phba, "NVMET LS RESP: xri x%x wqidx x%x len x%x\n",
843 ctxp->oxid, nvmewqeq->hba_wqidx, rsp->rsplen);
845 rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
846 if (rc == WQE_SUCCESS) {
848 * Okay to repost buffer here, but wait till cmpl
849 * before freeing ctxp and iocbq.
851 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
852 ctxp->rqb_buffer = 0;
853 atomic_inc(&nvmep->xmt_ls_rsp);
856 /* Give back resources */
857 atomic_inc(&nvmep->xmt_ls_drop);
858 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
859 "6151 LS Drop IO x%x: Issue %d\n",
862 lpfc_nlp_put(nvmewqeq->context1);
864 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
865 atomic_inc(&nvmep->xmt_ls_abort);
866 lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
871 lpfc_nvmet_xmt_fcp_op(struct nvmet_fc_target_port *tgtport,
872 struct nvmefc_tgt_fcp_req *rsp)
874 struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
875 struct lpfc_nvmet_rcv_ctx *ctxp =
876 container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
877 struct lpfc_hba *phba = ctxp->phba;
878 struct lpfc_queue *wq;
879 struct lpfc_iocbq *nvmewqeq;
880 struct lpfc_sli_ring *pring;
881 unsigned long iflags;
884 if (phba->pport->load_flag & FC_UNLOADING) {
889 if (phba->pport->load_flag & FC_UNLOADING) {
894 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
895 if (ctxp->ts_cmd_nvme) {
896 if (rsp->op == NVMET_FCOP_RSP)
897 ctxp->ts_nvme_status = ktime_get_ns();
899 ctxp->ts_nvme_data = ktime_get_ns();
902 /* Setup the hdw queue if not already set */
904 ctxp->hdwq = &phba->sli4_hba.hdwq[rsp->hwqid];
906 if (phba->cpucheck_on & LPFC_CHECK_NVMET_IO) {
907 int id = raw_smp_processor_id();
908 if (id < LPFC_CHECK_CPU_CNT) {
909 if (rsp->hwqid != id)
910 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
911 "6705 CPU Check OP: "
912 "cpu %d expect %d\n",
914 phba->sli4_hba.hdwq[rsp->hwqid].cpucheck_xmt_io[id]++;
916 ctxp->cpu = id; /* Setup cpu for cmpl check */
921 if ((ctxp->flag & LPFC_NVMET_ABTS_RCV) ||
922 (ctxp->state == LPFC_NVMET_STE_ABORT)) {
923 atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
924 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
925 "6102 IO xri x%x aborted\n",
931 nvmewqeq = lpfc_nvmet_prep_fcp_wqe(phba, ctxp);
932 if (nvmewqeq == NULL) {
933 atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
934 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
935 "6152 FCP Drop IO x%x: Prep\n",
941 nvmewqeq->wqe_cmpl = lpfc_nvmet_xmt_fcp_op_cmp;
942 nvmewqeq->iocb_cmpl = NULL;
943 nvmewqeq->context2 = ctxp;
944 nvmewqeq->iocb_flag |= LPFC_IO_NVMET;
945 ctxp->wqeq->hba_wqidx = rsp->hwqid;
947 lpfc_nvmeio_data(phba, "NVMET FCP CMND: xri x%x op x%x len x%x\n",
948 ctxp->oxid, rsp->op, rsp->rsplen);
950 ctxp->flag |= LPFC_NVMET_IO_INP;
951 rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
952 if (rc == WQE_SUCCESS) {
953 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
954 if (!ctxp->ts_cmd_nvme)
956 if (rsp->op == NVMET_FCOP_RSP)
957 ctxp->ts_status_wqput = ktime_get_ns();
959 ctxp->ts_data_wqput = ktime_get_ns();
966 * WQ was full, so queue nvmewqeq to be sent after
969 ctxp->flag |= LPFC_NVMET_DEFER_WQFULL;
970 wq = ctxp->hdwq->nvme_wq;
972 spin_lock_irqsave(&pring->ring_lock, iflags);
973 list_add_tail(&nvmewqeq->list, &wq->wqfull_list);
974 wq->q_flag |= HBA_NVMET_WQFULL;
975 spin_unlock_irqrestore(&pring->ring_lock, iflags);
976 atomic_inc(&lpfc_nvmep->defer_wqfull);
980 /* Give back resources */
981 atomic_inc(&lpfc_nvmep->xmt_fcp_drop);
982 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
983 "6153 FCP Drop IO x%x: Issue: %d\n",
986 ctxp->wqeq->hba_wqidx = 0;
987 nvmewqeq->context2 = NULL;
988 nvmewqeq->context3 = NULL;
995 lpfc_nvmet_targetport_delete(struct nvmet_fc_target_port *targetport)
997 struct lpfc_nvmet_tgtport *tport = targetport->private;
999 /* release any threads waiting for the unreg to complete */
1000 if (tport->phba->targetport)
1001 complete(tport->tport_unreg_cmp);
1005 lpfc_nvmet_xmt_fcp_abort(struct nvmet_fc_target_port *tgtport,
1006 struct nvmefc_tgt_fcp_req *req)
1008 struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
1009 struct lpfc_nvmet_rcv_ctx *ctxp =
1010 container_of(req, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
1011 struct lpfc_hba *phba = ctxp->phba;
1012 struct lpfc_queue *wq;
1013 unsigned long flags;
1015 if (phba->pport->load_flag & FC_UNLOADING)
1018 if (phba->pport->load_flag & FC_UNLOADING)
1022 ctxp->hdwq = &phba->sli4_hba.hdwq[0];
1024 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1025 "6103 NVMET Abort op: oxri x%x flg x%x ste %d\n",
1026 ctxp->oxid, ctxp->flag, ctxp->state);
1028 lpfc_nvmeio_data(phba, "NVMET FCP ABRT: xri x%x flg x%x ste x%x\n",
1029 ctxp->oxid, ctxp->flag, ctxp->state);
1031 atomic_inc(&lpfc_nvmep->xmt_fcp_abort);
1033 spin_lock_irqsave(&ctxp->ctxlock, flags);
1035 /* Since iaab/iaar are NOT set, we need to check
1036 * if the firmware is in process of aborting IO
1038 if (ctxp->flag & LPFC_NVMET_XBUSY) {
1039 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
1042 ctxp->flag |= LPFC_NVMET_ABORT_OP;
1044 if (ctxp->flag & LPFC_NVMET_DEFER_WQFULL) {
1045 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
1046 lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1048 wq = ctxp->hdwq->nvme_wq;
1049 lpfc_nvmet_wqfull_flush(phba, wq, ctxp);
1052 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
1054 /* An state of LPFC_NVMET_STE_RCV means we have just received
1055 * the NVME command and have not started processing it.
1056 * (by issuing any IO WQEs on this exchange yet)
1058 if (ctxp->state == LPFC_NVMET_STE_RCV)
1059 lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1062 lpfc_nvmet_sol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1067 lpfc_nvmet_xmt_fcp_release(struct nvmet_fc_target_port *tgtport,
1068 struct nvmefc_tgt_fcp_req *rsp)
1070 struct lpfc_nvmet_tgtport *lpfc_nvmep = tgtport->private;
1071 struct lpfc_nvmet_rcv_ctx *ctxp =
1072 container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
1073 struct lpfc_hba *phba = ctxp->phba;
1074 unsigned long flags;
1075 bool aborting = false;
1077 spin_lock_irqsave(&ctxp->ctxlock, flags);
1078 if (ctxp->flag & LPFC_NVMET_XBUSY)
1079 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1080 "6027 NVMET release with XBUSY flag x%x"
1082 ctxp->flag, ctxp->oxid);
1083 else if (ctxp->state != LPFC_NVMET_STE_DONE &&
1084 ctxp->state != LPFC_NVMET_STE_ABORT)
1085 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1086 "6413 NVMET release bad state %d %d oxid x%x\n",
1087 ctxp->state, ctxp->entry_cnt, ctxp->oxid);
1089 if ((ctxp->flag & LPFC_NVMET_ABORT_OP) ||
1090 (ctxp->flag & LPFC_NVMET_XBUSY)) {
1092 /* let the abort path do the real release */
1093 lpfc_nvmet_defer_release(phba, ctxp);
1095 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
1097 lpfc_nvmeio_data(phba, "NVMET FCP FREE: xri x%x ste %d abt %d\n", ctxp->oxid,
1098 ctxp->state, aborting);
1100 atomic_inc(&lpfc_nvmep->xmt_fcp_release);
1105 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
1109 lpfc_nvmet_defer_rcv(struct nvmet_fc_target_port *tgtport,
1110 struct nvmefc_tgt_fcp_req *rsp)
1112 struct lpfc_nvmet_tgtport *tgtp;
1113 struct lpfc_nvmet_rcv_ctx *ctxp =
1114 container_of(rsp, struct lpfc_nvmet_rcv_ctx, ctx.fcp_req);
1115 struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
1116 struct lpfc_hba *phba = ctxp->phba;
1117 unsigned long iflag;
1120 lpfc_nvmeio_data(phba, "NVMET DEFERRCV: xri x%x sz %d CPU %02x\n",
1121 ctxp->oxid, ctxp->size, raw_smp_processor_id());
1124 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
1125 "6425 Defer rcv: no buffer xri x%x: "
1127 ctxp->oxid, ctxp->flag, ctxp->state);
1131 tgtp = phba->targetport->private;
1133 atomic_inc(&tgtp->rcv_fcp_cmd_defer);
1135 /* Free the nvmebuf since a new buffer already replaced it */
1136 nvmebuf->hrq->rqbp->rqb_free_buffer(phba, nvmebuf);
1137 spin_lock_irqsave(&ctxp->ctxlock, iflag);
1138 ctxp->rqb_buffer = NULL;
1139 spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
1142 static struct nvmet_fc_target_template lpfc_tgttemplate = {
1143 .targetport_delete = lpfc_nvmet_targetport_delete,
1144 .xmt_ls_rsp = lpfc_nvmet_xmt_ls_rsp,
1145 .fcp_op = lpfc_nvmet_xmt_fcp_op,
1146 .fcp_abort = lpfc_nvmet_xmt_fcp_abort,
1147 .fcp_req_release = lpfc_nvmet_xmt_fcp_release,
1148 .defer_rcv = lpfc_nvmet_defer_rcv,
1151 .max_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1152 .max_dif_sgl_segments = LPFC_NVMET_DEFAULT_SEGS,
1153 .dma_boundary = 0xFFFFFFFF,
1155 /* optional features */
1156 .target_features = 0,
1157 /* sizes of additional private data for data structures */
1158 .target_priv_sz = sizeof(struct lpfc_nvmet_tgtport),
1162 __lpfc_nvmet_clean_io_for_cpu(struct lpfc_hba *phba,
1163 struct lpfc_nvmet_ctx_info *infop)
1165 struct lpfc_nvmet_ctxbuf *ctx_buf, *next_ctx_buf;
1166 unsigned long flags;
1168 spin_lock_irqsave(&infop->nvmet_ctx_list_lock, flags);
1169 list_for_each_entry_safe(ctx_buf, next_ctx_buf,
1170 &infop->nvmet_ctx_list, list) {
1171 spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1172 list_del_init(&ctx_buf->list);
1173 spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1175 __lpfc_clear_active_sglq(phba, ctx_buf->sglq->sli4_lxritag);
1176 ctx_buf->sglq->state = SGL_FREED;
1177 ctx_buf->sglq->ndlp = NULL;
1179 spin_lock(&phba->sli4_hba.sgl_list_lock);
1180 list_add_tail(&ctx_buf->sglq->list,
1181 &phba->sli4_hba.lpfc_nvmet_sgl_list);
1182 spin_unlock(&phba->sli4_hba.sgl_list_lock);
1184 lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
1185 kfree(ctx_buf->context);
1187 spin_unlock_irqrestore(&infop->nvmet_ctx_list_lock, flags);
1191 lpfc_nvmet_cleanup_io_context(struct lpfc_hba *phba)
1193 struct lpfc_nvmet_ctx_info *infop;
1196 /* The first context list, MRQ 0 CPU 0 */
1197 infop = phba->sli4_hba.nvmet_ctx_info;
1201 /* Cycle the the entire CPU context list for every MRQ */
1202 for (i = 0; i < phba->cfg_nvmet_mrq; i++) {
1203 for_each_present_cpu(j) {
1204 infop = lpfc_get_ctx_list(phba, j, i);
1205 __lpfc_nvmet_clean_io_for_cpu(phba, infop);
1208 kfree(phba->sli4_hba.nvmet_ctx_info);
1209 phba->sli4_hba.nvmet_ctx_info = NULL;
1213 lpfc_nvmet_setup_io_context(struct lpfc_hba *phba)
1215 struct lpfc_nvmet_ctxbuf *ctx_buf;
1216 struct lpfc_iocbq *nvmewqe;
1217 union lpfc_wqe128 *wqe;
1218 struct lpfc_nvmet_ctx_info *last_infop;
1219 struct lpfc_nvmet_ctx_info *infop;
1222 lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
1223 "6403 Allocate NVMET resources for %d XRIs\n",
1224 phba->sli4_hba.nvmet_xri_cnt);
1226 phba->sli4_hba.nvmet_ctx_info = kcalloc(
1227 phba->sli4_hba.num_possible_cpu * phba->cfg_nvmet_mrq,
1228 sizeof(struct lpfc_nvmet_ctx_info), GFP_KERNEL);
1229 if (!phba->sli4_hba.nvmet_ctx_info) {
1230 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
1231 "6419 Failed allocate memory for "
1232 "nvmet context lists\n");
1237 * Assuming X CPUs in the system, and Y MRQs, allocate some
1238 * lpfc_nvmet_ctx_info structures as follows:
1240 * cpu0/mrq0 cpu1/mrq0 ... cpuX/mrq0
1241 * cpu0/mrq1 cpu1/mrq1 ... cpuX/mrq1
1243 * cpuX/mrqY cpuX/mrqY ... cpuX/mrqY
1245 * Each line represents a MRQ "silo" containing an entry for
1248 * MRQ X is initially assumed to be associated with CPU X, thus
1249 * contexts are initially distributed across all MRQs using
1250 * the MRQ index (N) as follows cpuN/mrqN. When contexts are
1251 * freed, the are freed to the MRQ silo based on the CPU number
1252 * of the IO completion. Thus a context that was allocated for MRQ A
1253 * whose IO completed on CPU B will be freed to cpuB/mrqA.
1255 for_each_possible_cpu(i) {
1256 for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1257 infop = lpfc_get_ctx_list(phba, i, j);
1258 INIT_LIST_HEAD(&infop->nvmet_ctx_list);
1259 spin_lock_init(&infop->nvmet_ctx_list_lock);
1260 infop->nvmet_ctx_list_cnt = 0;
1265 * Setup the next CPU context info ptr for each MRQ.
1266 * MRQ 0 will cycle thru CPUs 0 - X separately from
1267 * MRQ 1 cycling thru CPUs 0 - X, and so on.
1269 for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1270 last_infop = lpfc_get_ctx_list(phba,
1271 cpumask_first(cpu_present_mask),
1273 for (i = phba->sli4_hba.num_possible_cpu - 1; i >= 0; i--) {
1274 infop = lpfc_get_ctx_list(phba, i, j);
1275 infop->nvmet_ctx_next_cpu = last_infop;
1280 /* For all nvmet xris, allocate resources needed to process a
1281 * received command on a per xri basis.
1284 cpu = cpumask_first(cpu_present_mask);
1285 for (i = 0; i < phba->sli4_hba.nvmet_xri_cnt; i++) {
1286 ctx_buf = kzalloc(sizeof(*ctx_buf), GFP_KERNEL);
1288 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1289 "6404 Ran out of memory for NVMET\n");
1293 ctx_buf->context = kzalloc(sizeof(*ctx_buf->context),
1295 if (!ctx_buf->context) {
1297 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1298 "6405 Ran out of NVMET "
1299 "context memory\n");
1302 ctx_buf->context->ctxbuf = ctx_buf;
1303 ctx_buf->context->state = LPFC_NVMET_STE_FREE;
1305 ctx_buf->iocbq = lpfc_sli_get_iocbq(phba);
1306 if (!ctx_buf->iocbq) {
1307 kfree(ctx_buf->context);
1309 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1310 "6406 Ran out of NVMET iocb/WQEs\n");
1313 ctx_buf->iocbq->iocb_flag = LPFC_IO_NVMET;
1314 nvmewqe = ctx_buf->iocbq;
1315 wqe = &nvmewqe->wqe;
1317 /* Initialize WQE */
1318 memset(wqe, 0, sizeof(union lpfc_wqe));
1320 ctx_buf->iocbq->context1 = NULL;
1321 spin_lock(&phba->sli4_hba.sgl_list_lock);
1322 ctx_buf->sglq = __lpfc_sli_get_nvmet_sglq(phba, ctx_buf->iocbq);
1323 spin_unlock(&phba->sli4_hba.sgl_list_lock);
1324 if (!ctx_buf->sglq) {
1325 lpfc_sli_release_iocbq(phba, ctx_buf->iocbq);
1326 kfree(ctx_buf->context);
1328 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1329 "6407 Ran out of NVMET XRIs\n");
1332 INIT_WORK(&ctx_buf->defer_work, lpfc_nvmet_fcp_rqst_defer_work);
1335 * Add ctx to MRQidx context list. Our initial assumption
1336 * is MRQidx will be associated with CPUidx. This association
1337 * can change on the fly.
1339 infop = lpfc_get_ctx_list(phba, cpu, idx);
1340 spin_lock(&infop->nvmet_ctx_list_lock);
1341 list_add_tail(&ctx_buf->list, &infop->nvmet_ctx_list);
1342 infop->nvmet_ctx_list_cnt++;
1343 spin_unlock(&infop->nvmet_ctx_list_lock);
1345 /* Spread ctx structures evenly across all MRQs */
1347 if (idx >= phba->cfg_nvmet_mrq) {
1349 cpu = cpumask_first(cpu_present_mask);
1352 cpu = cpumask_next(cpu, cpu_present_mask);
1353 if (cpu == nr_cpu_ids)
1354 cpu = cpumask_first(cpu_present_mask);
1358 for_each_present_cpu(i) {
1359 for (j = 0; j < phba->cfg_nvmet_mrq; j++) {
1360 infop = lpfc_get_ctx_list(phba, i, j);
1361 lpfc_printf_log(phba, KERN_INFO, LOG_NVME | LOG_INIT,
1362 "6408 TOTAL NVMET ctx for CPU %d "
1363 "MRQ %d: cnt %d nextcpu %p\n",
1364 i, j, infop->nvmet_ctx_list_cnt,
1365 infop->nvmet_ctx_next_cpu);
1372 lpfc_nvmet_create_targetport(struct lpfc_hba *phba)
1374 struct lpfc_vport *vport = phba->pport;
1375 struct lpfc_nvmet_tgtport *tgtp;
1376 struct nvmet_fc_port_info pinfo;
1379 if (phba->targetport)
1382 error = lpfc_nvmet_setup_io_context(phba);
1386 memset(&pinfo, 0, sizeof(struct nvmet_fc_port_info));
1387 pinfo.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
1388 pinfo.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
1389 pinfo.port_id = vport->fc_myDID;
1391 /* We need to tell the transport layer + 1 because it takes page
1392 * alignment into account. When space for the SGL is allocated we
1393 * allocate + 3, one for cmd, one for rsp and one for this alignment
1395 lpfc_tgttemplate.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
1396 lpfc_tgttemplate.max_hw_queues = phba->cfg_hdw_queue;
1397 lpfc_tgttemplate.target_features = NVMET_FCTGTFEAT_READDATA_RSP;
1399 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1400 error = nvmet_fc_register_targetport(&pinfo, &lpfc_tgttemplate,
1407 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
1408 "6025 Cannot register NVME targetport x%x: "
1409 "portnm %llx nodenm %llx segs %d qs %d\n",
1411 pinfo.port_name, pinfo.node_name,
1412 lpfc_tgttemplate.max_sgl_segments,
1413 lpfc_tgttemplate.max_hw_queues);
1414 phba->targetport = NULL;
1415 phba->nvmet_support = 0;
1417 lpfc_nvmet_cleanup_io_context(phba);
1420 tgtp = (struct lpfc_nvmet_tgtport *)
1421 phba->targetport->private;
1424 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1425 "6026 Registered NVME "
1426 "targetport: %p, private %p "
1427 "portnm %llx nodenm %llx segs %d qs %d\n",
1428 phba->targetport, tgtp,
1429 pinfo.port_name, pinfo.node_name,
1430 lpfc_tgttemplate.max_sgl_segments,
1431 lpfc_tgttemplate.max_hw_queues);
1433 atomic_set(&tgtp->rcv_ls_req_in, 0);
1434 atomic_set(&tgtp->rcv_ls_req_out, 0);
1435 atomic_set(&tgtp->rcv_ls_req_drop, 0);
1436 atomic_set(&tgtp->xmt_ls_abort, 0);
1437 atomic_set(&tgtp->xmt_ls_abort_cmpl, 0);
1438 atomic_set(&tgtp->xmt_ls_rsp, 0);
1439 atomic_set(&tgtp->xmt_ls_drop, 0);
1440 atomic_set(&tgtp->xmt_ls_rsp_error, 0);
1441 atomic_set(&tgtp->xmt_ls_rsp_xb_set, 0);
1442 atomic_set(&tgtp->xmt_ls_rsp_aborted, 0);
1443 atomic_set(&tgtp->xmt_ls_rsp_cmpl, 0);
1444 atomic_set(&tgtp->rcv_fcp_cmd_in, 0);
1445 atomic_set(&tgtp->rcv_fcp_cmd_out, 0);
1446 atomic_set(&tgtp->rcv_fcp_cmd_drop, 0);
1447 atomic_set(&tgtp->xmt_fcp_drop, 0);
1448 atomic_set(&tgtp->xmt_fcp_read_rsp, 0);
1449 atomic_set(&tgtp->xmt_fcp_read, 0);
1450 atomic_set(&tgtp->xmt_fcp_write, 0);
1451 atomic_set(&tgtp->xmt_fcp_rsp, 0);
1452 atomic_set(&tgtp->xmt_fcp_release, 0);
1453 atomic_set(&tgtp->xmt_fcp_rsp_cmpl, 0);
1454 atomic_set(&tgtp->xmt_fcp_rsp_error, 0);
1455 atomic_set(&tgtp->xmt_fcp_rsp_xb_set, 0);
1456 atomic_set(&tgtp->xmt_fcp_rsp_aborted, 0);
1457 atomic_set(&tgtp->xmt_fcp_rsp_drop, 0);
1458 atomic_set(&tgtp->xmt_fcp_xri_abort_cqe, 0);
1459 atomic_set(&tgtp->xmt_fcp_abort, 0);
1460 atomic_set(&tgtp->xmt_fcp_abort_cmpl, 0);
1461 atomic_set(&tgtp->xmt_abort_unsol, 0);
1462 atomic_set(&tgtp->xmt_abort_sol, 0);
1463 atomic_set(&tgtp->xmt_abort_rsp, 0);
1464 atomic_set(&tgtp->xmt_abort_rsp_error, 0);
1465 atomic_set(&tgtp->defer_ctx, 0);
1466 atomic_set(&tgtp->defer_fod, 0);
1467 atomic_set(&tgtp->defer_wqfull, 0);
1473 lpfc_nvmet_update_targetport(struct lpfc_hba *phba)
1475 struct lpfc_vport *vport = phba->pport;
1477 if (!phba->targetport)
1480 lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
1481 "6007 Update NVMET port %p did x%x\n",
1482 phba->targetport, vport->fc_myDID);
1484 phba->targetport->port_id = vport->fc_myDID;
1489 * lpfc_sli4_nvmet_xri_aborted - Fast-path process of nvmet xri abort
1490 * @phba: pointer to lpfc hba data structure.
1491 * @axri: pointer to the nvmet xri abort wcqe structure.
1493 * This routine is invoked by the worker thread to process a SLI4 fast-path
1494 * NVMET aborted xri.
1497 lpfc_sli4_nvmet_xri_aborted(struct lpfc_hba *phba,
1498 struct sli4_wcqe_xri_aborted *axri)
1500 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
1501 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
1502 struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
1503 struct lpfc_nvmet_tgtport *tgtp;
1504 struct lpfc_nodelist *ndlp;
1505 unsigned long iflag = 0;
1507 bool released = false;
1509 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1510 "6317 XB aborted xri x%x rxid x%x\n", xri, rxid);
1512 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
1515 if (phba->targetport) {
1516 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1517 atomic_inc(&tgtp->xmt_fcp_xri_abort_cqe);
1520 spin_lock_irqsave(&phba->hbalock, iflag);
1521 spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1522 list_for_each_entry_safe(ctxp, next_ctxp,
1523 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1525 if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
1528 spin_lock(&ctxp->ctxlock);
1529 /* Check if we already received a free context call
1530 * and we have completed processing an abort situation.
1532 if (ctxp->flag & LPFC_NVMET_CTX_RLS &&
1533 !(ctxp->flag & LPFC_NVMET_ABORT_OP)) {
1534 list_del(&ctxp->list);
1537 ctxp->flag &= ~LPFC_NVMET_XBUSY;
1538 spin_unlock(&ctxp->ctxlock);
1539 spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1541 rrq_empty = list_empty(&phba->active_rrq_list);
1542 spin_unlock_irqrestore(&phba->hbalock, iflag);
1543 ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
1544 if (ndlp && NLP_CHK_NODE_ACT(ndlp) &&
1545 (ndlp->nlp_state == NLP_STE_UNMAPPED_NODE ||
1546 ndlp->nlp_state == NLP_STE_MAPPED_NODE)) {
1547 lpfc_set_rrq_active(phba, ndlp,
1548 ctxp->ctxbuf->sglq->sli4_lxritag,
1550 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
1553 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1554 "6318 XB aborted oxid %x flg x%x (%x)\n",
1555 ctxp->oxid, ctxp->flag, released);
1557 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
1560 lpfc_worker_wake_up(phba);
1563 spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1564 spin_unlock_irqrestore(&phba->hbalock, iflag);
1568 lpfc_nvmet_rcv_unsol_abort(struct lpfc_vport *vport,
1569 struct fc_frame_header *fc_hdr)
1571 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1572 struct lpfc_hba *phba = vport->phba;
1573 struct lpfc_nvmet_rcv_ctx *ctxp, *next_ctxp;
1574 struct nvmefc_tgt_fcp_req *rsp;
1576 unsigned long iflag = 0;
1578 xri = be16_to_cpu(fc_hdr->fh_ox_id);
1580 spin_lock_irqsave(&phba->hbalock, iflag);
1581 spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1582 list_for_each_entry_safe(ctxp, next_ctxp,
1583 &phba->sli4_hba.lpfc_abts_nvmet_ctx_list,
1585 if (ctxp->ctxbuf->sglq->sli4_xritag != xri)
1588 spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1589 spin_unlock_irqrestore(&phba->hbalock, iflag);
1591 spin_lock_irqsave(&ctxp->ctxlock, iflag);
1592 ctxp->flag |= LPFC_NVMET_ABTS_RCV;
1593 spin_unlock_irqrestore(&ctxp->ctxlock, iflag);
1595 lpfc_nvmeio_data(phba,
1596 "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1597 xri, raw_smp_processor_id(), 0);
1599 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1600 "6319 NVMET Rcv ABTS:acc xri x%x\n", xri);
1602 rsp = &ctxp->ctx.fcp_req;
1603 nvmet_fc_rcv_fcp_abort(phba->targetport, rsp);
1605 /* Respond with BA_ACC accordingly */
1606 lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 1);
1609 spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
1610 spin_unlock_irqrestore(&phba->hbalock, iflag);
1612 lpfc_nvmeio_data(phba, "NVMET ABTS RCV: xri x%x CPU %02x rjt %d\n",
1613 xri, raw_smp_processor_id(), 1);
1615 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
1616 "6320 NVMET Rcv ABTS:rjt xri x%x\n", xri);
1618 /* Respond with BA_RJT accordingly */
1619 lpfc_sli4_seq_abort_rsp(vport, fc_hdr, 0);
1625 lpfc_nvmet_wqfull_flush(struct lpfc_hba *phba, struct lpfc_queue *wq,
1626 struct lpfc_nvmet_rcv_ctx *ctxp)
1628 struct lpfc_sli_ring *pring;
1629 struct lpfc_iocbq *nvmewqeq;
1630 struct lpfc_iocbq *next_nvmewqeq;
1631 unsigned long iflags;
1632 struct lpfc_wcqe_complete wcqe;
1633 struct lpfc_wcqe_complete *wcqep;
1638 /* Fake an ABORT error code back to cmpl routine */
1639 memset(wcqep, 0, sizeof(struct lpfc_wcqe_complete));
1640 bf_set(lpfc_wcqe_c_status, wcqep, IOSTAT_LOCAL_REJECT);
1641 wcqep->parameter = IOERR_ABORT_REQUESTED;
1643 spin_lock_irqsave(&pring->ring_lock, iflags);
1644 list_for_each_entry_safe(nvmewqeq, next_nvmewqeq,
1645 &wq->wqfull_list, list) {
1647 /* Checking for a specific IO to flush */
1648 if (nvmewqeq->context2 == ctxp) {
1649 list_del(&nvmewqeq->list);
1650 spin_unlock_irqrestore(&pring->ring_lock,
1652 lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq,
1659 list_del(&nvmewqeq->list);
1660 spin_unlock_irqrestore(&pring->ring_lock, iflags);
1661 lpfc_nvmet_xmt_fcp_op_cmp(phba, nvmewqeq, wcqep);
1662 spin_lock_irqsave(&pring->ring_lock, iflags);
1666 wq->q_flag &= ~HBA_NVMET_WQFULL;
1667 spin_unlock_irqrestore(&pring->ring_lock, iflags);
1671 lpfc_nvmet_wqfull_process(struct lpfc_hba *phba,
1672 struct lpfc_queue *wq)
1674 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1675 struct lpfc_sli_ring *pring;
1676 struct lpfc_iocbq *nvmewqeq;
1677 struct lpfc_nvmet_rcv_ctx *ctxp;
1678 unsigned long iflags;
1682 * Some WQE slots are available, so try to re-issue anything
1683 * on the WQ wqfull_list.
1686 spin_lock_irqsave(&pring->ring_lock, iflags);
1687 while (!list_empty(&wq->wqfull_list)) {
1688 list_remove_head(&wq->wqfull_list, nvmewqeq, struct lpfc_iocbq,
1690 spin_unlock_irqrestore(&pring->ring_lock, iflags);
1691 ctxp = (struct lpfc_nvmet_rcv_ctx *)nvmewqeq->context2;
1692 rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, nvmewqeq);
1693 spin_lock_irqsave(&pring->ring_lock, iflags);
1695 /* WQ was full again, so put it back on the list */
1696 list_add(&nvmewqeq->list, &wq->wqfull_list);
1697 spin_unlock_irqrestore(&pring->ring_lock, iflags);
1701 wq->q_flag &= ~HBA_NVMET_WQFULL;
1702 spin_unlock_irqrestore(&pring->ring_lock, iflags);
1708 lpfc_nvmet_destroy_targetport(struct lpfc_hba *phba)
1710 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1711 struct lpfc_nvmet_tgtport *tgtp;
1712 struct lpfc_queue *wq;
1714 DECLARE_COMPLETION_ONSTACK(tport_unreg_cmp);
1716 if (phba->nvmet_support == 0)
1718 if (phba->targetport) {
1719 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1720 for (qidx = 0; qidx < phba->cfg_hdw_queue; qidx++) {
1721 wq = phba->sli4_hba.hdwq[qidx].nvme_wq;
1722 lpfc_nvmet_wqfull_flush(phba, wq, NULL);
1724 tgtp->tport_unreg_cmp = &tport_unreg_cmp;
1725 nvmet_fc_unregister_targetport(phba->targetport);
1726 if (!wait_for_completion_timeout(tgtp->tport_unreg_cmp,
1727 msecs_to_jiffies(LPFC_NVMET_WAIT_TMO)))
1728 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
1729 "6179 Unreg targetport %p timeout "
1730 "reached.\n", phba->targetport);
1731 lpfc_nvmet_cleanup_io_context(phba);
1733 phba->targetport = NULL;
1738 * lpfc_nvmet_unsol_ls_buffer - Process an unsolicited event data buffer
1739 * @phba: pointer to lpfc hba data structure.
1740 * @pring: pointer to a SLI ring.
1741 * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
1743 * This routine is used for processing the WQE associated with a unsolicited
1744 * event. It first determines whether there is an existing ndlp that matches
1745 * the DID from the unsolicited WQE. If not, it will create a new one with
1746 * the DID from the unsolicited WQE. The ELS command from the unsolicited
1747 * WQE is then used to invoke the proper routine and to set up proper state
1748 * of the discovery state machine.
1751 lpfc_nvmet_unsol_ls_buffer(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
1752 struct hbq_dmabuf *nvmebuf)
1754 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1755 struct lpfc_nvmet_tgtport *tgtp;
1756 struct fc_frame_header *fc_hdr;
1757 struct lpfc_nvmet_rcv_ctx *ctxp;
1759 uint32_t size, oxid, sid, rc;
1761 fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
1762 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
1764 if (!phba->targetport) {
1765 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1766 "6154 LS Drop IO x%x\n", oxid);
1774 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1775 payload = (uint32_t *)(nvmebuf->dbuf.virt);
1776 size = bf_get(lpfc_rcqe_length, &nvmebuf->cq_event.cqe.rcqe_cmpl);
1777 sid = sli4_sid_from_fc_hdr(fc_hdr);
1779 ctxp = kzalloc(sizeof(struct lpfc_nvmet_rcv_ctx), GFP_ATOMIC);
1781 atomic_inc(&tgtp->rcv_ls_req_drop);
1782 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1783 "6155 LS Drop IO x%x: Alloc\n",
1786 lpfc_nvmeio_data(phba, "NVMET LS DROP: "
1787 "xri x%x sz %d from %06x\n",
1789 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1797 ctxp->state = LPFC_NVMET_STE_LS_RCV;
1798 ctxp->entry_cnt = 1;
1799 ctxp->rqb_buffer = (void *)nvmebuf;
1800 ctxp->hdwq = &phba->sli4_hba.hdwq[0];
1802 lpfc_nvmeio_data(phba, "NVMET LS RCV: xri x%x sz %d from %06x\n",
1805 * The calling sequence should be:
1806 * nvmet_fc_rcv_ls_req -> lpfc_nvmet_xmt_ls_rsp/cmp ->_req->done
1807 * lpfc_nvmet_xmt_ls_rsp_cmp should free the allocated ctxp.
1809 atomic_inc(&tgtp->rcv_ls_req_in);
1810 rc = nvmet_fc_rcv_ls_req(phba->targetport, &ctxp->ctx.ls_req,
1813 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
1814 "6037 NVMET Unsol rcv: sz %d rc %d: %08x %08x %08x "
1815 "%08x %08x %08x\n", size, rc,
1816 *payload, *(payload+1), *(payload+2),
1817 *(payload+3), *(payload+4), *(payload+5));
1820 atomic_inc(&tgtp->rcv_ls_req_out);
1824 lpfc_nvmeio_data(phba, "NVMET LS DROP: xri x%x sz %d from %06x\n",
1827 atomic_inc(&tgtp->rcv_ls_req_drop);
1828 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1829 "6156 LS Drop IO x%x: nvmet_fc_rcv_ls_req %d\n",
1832 /* We assume a rcv'ed cmd ALWAYs fits into 1 buffer */
1833 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
1835 atomic_inc(&tgtp->xmt_ls_abort);
1836 lpfc_nvmet_unsol_ls_issue_abort(phba, ctxp, sid, oxid);
1841 lpfc_nvmet_process_rcv_fcp_req(struct lpfc_nvmet_ctxbuf *ctx_buf)
1843 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1844 struct lpfc_nvmet_rcv_ctx *ctxp = ctx_buf->context;
1845 struct lpfc_hba *phba = ctxp->phba;
1846 struct rqb_dmabuf *nvmebuf = ctxp->rqb_buffer;
1847 struct lpfc_nvmet_tgtport *tgtp;
1848 uint32_t *payload, qno;
1850 unsigned long iflags;
1853 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1854 "6159 process_rcv_fcp_req, nvmebuf is NULL, "
1855 "oxid: x%x flg: x%x state: x%x\n",
1856 ctxp->oxid, ctxp->flag, ctxp->state);
1857 spin_lock_irqsave(&ctxp->ctxlock, iflags);
1858 lpfc_nvmet_defer_release(phba, ctxp);
1859 spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
1860 lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid,
1865 payload = (uint32_t *)(nvmebuf->dbuf.virt);
1866 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
1868 * The calling sequence should be:
1869 * nvmet_fc_rcv_fcp_req->lpfc_nvmet_xmt_fcp_op/cmp- req->done
1870 * lpfc_nvmet_xmt_fcp_op_cmp should free the allocated ctxp.
1871 * When we return from nvmet_fc_rcv_fcp_req, all relevant info
1872 * the NVME command / FC header is stored.
1873 * A buffer has already been reposted for this IO, so just free
1876 rc = nvmet_fc_rcv_fcp_req(phba->targetport, &ctxp->ctx.fcp_req,
1877 payload, ctxp->size);
1878 /* Process FCP command */
1880 atomic_inc(&tgtp->rcv_fcp_cmd_out);
1881 spin_lock_irqsave(&ctxp->ctxlock, iflags);
1882 if ((ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) ||
1883 (nvmebuf != ctxp->rqb_buffer)) {
1884 spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
1887 ctxp->rqb_buffer = NULL;
1888 spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
1889 lpfc_rq_buf_free(phba, &nvmebuf->hbuf); /* repost */
1893 /* Processing of FCP command is deferred */
1894 if (rc == -EOVERFLOW) {
1895 lpfc_nvmeio_data(phba, "NVMET RCV BUSY: xri x%x sz %d "
1897 ctxp->oxid, ctxp->size, ctxp->sid);
1898 atomic_inc(&tgtp->rcv_fcp_cmd_out);
1899 atomic_inc(&tgtp->defer_fod);
1900 spin_lock_irqsave(&ctxp->ctxlock, iflags);
1901 if (ctxp->flag & LPFC_NVMET_CTX_REUSE_WQ) {
1902 spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
1905 spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
1907 * Post a replacement DMA buffer to RQ and defer
1908 * freeing rcv buffer till .defer_rcv callback
1911 lpfc_post_rq_buffer(
1912 phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
1913 phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
1916 atomic_inc(&tgtp->rcv_fcp_cmd_drop);
1917 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
1918 "2582 FCP Drop IO x%x: err x%x: x%x x%x x%x\n",
1920 atomic_read(&tgtp->rcv_fcp_cmd_in),
1921 atomic_read(&tgtp->rcv_fcp_cmd_out),
1922 atomic_read(&tgtp->xmt_fcp_release));
1923 lpfc_nvmeio_data(phba, "NVMET FCP DROP: xri x%x sz %d from %06x\n",
1924 ctxp->oxid, ctxp->size, ctxp->sid);
1925 spin_lock_irqsave(&ctxp->ctxlock, iflags);
1926 lpfc_nvmet_defer_release(phba, ctxp);
1927 spin_unlock_irqrestore(&ctxp->ctxlock, iflags);
1928 lpfc_nvmet_unsol_fcp_issue_abort(phba, ctxp, ctxp->sid, ctxp->oxid);
1933 lpfc_nvmet_fcp_rqst_defer_work(struct work_struct *work)
1935 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1936 struct lpfc_nvmet_ctxbuf *ctx_buf =
1937 container_of(work, struct lpfc_nvmet_ctxbuf, defer_work);
1939 lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
1943 static struct lpfc_nvmet_ctxbuf *
1944 lpfc_nvmet_replenish_context(struct lpfc_hba *phba,
1945 struct lpfc_nvmet_ctx_info *current_infop)
1947 #if (IS_ENABLED(CONFIG_NVME_TARGET_FC))
1948 struct lpfc_nvmet_ctxbuf *ctx_buf = NULL;
1949 struct lpfc_nvmet_ctx_info *get_infop;
1953 * The current_infop for the MRQ a NVME command IU was received
1954 * on is empty. Our goal is to replenish this MRQs context
1955 * list from a another CPUs.
1957 * First we need to pick a context list to start looking on.
1958 * nvmet_ctx_start_cpu has available context the last time
1959 * we needed to replenish this CPU where nvmet_ctx_next_cpu
1960 * is just the next sequential CPU for this MRQ.
1962 if (current_infop->nvmet_ctx_start_cpu)
1963 get_infop = current_infop->nvmet_ctx_start_cpu;
1965 get_infop = current_infop->nvmet_ctx_next_cpu;
1967 for (i = 0; i < phba->sli4_hba.num_possible_cpu; i++) {
1968 if (get_infop == current_infop) {
1969 get_infop = get_infop->nvmet_ctx_next_cpu;
1972 spin_lock(&get_infop->nvmet_ctx_list_lock);
1974 /* Just take the entire context list, if there are any */
1975 if (get_infop->nvmet_ctx_list_cnt) {
1976 list_splice_init(&get_infop->nvmet_ctx_list,
1977 ¤t_infop->nvmet_ctx_list);
1978 current_infop->nvmet_ctx_list_cnt =
1979 get_infop->nvmet_ctx_list_cnt - 1;
1980 get_infop->nvmet_ctx_list_cnt = 0;
1981 spin_unlock(&get_infop->nvmet_ctx_list_lock);
1983 current_infop->nvmet_ctx_start_cpu = get_infop;
1984 list_remove_head(¤t_infop->nvmet_ctx_list,
1985 ctx_buf, struct lpfc_nvmet_ctxbuf,
1990 /* Otherwise, move on to the next CPU for this MRQ */
1991 spin_unlock(&get_infop->nvmet_ctx_list_lock);
1992 get_infop = get_infop->nvmet_ctx_next_cpu;
1996 /* Nothing found, all contexts for the MRQ are in-flight */
2001 * lpfc_nvmet_unsol_fcp_buffer - Process an unsolicited event data buffer
2002 * @phba: pointer to lpfc hba data structure.
2003 * @idx: relative index of MRQ vector
2004 * @nvmebuf: pointer to lpfc nvme command HBQ data structure.
2006 * This routine is used for processing the WQE associated with a unsolicited
2007 * event. It first determines whether there is an existing ndlp that matches
2008 * the DID from the unsolicited WQE. If not, it will create a new one with
2009 * the DID from the unsolicited WQE. The ELS command from the unsolicited
2010 * WQE is then used to invoke the proper routine and to set up proper state
2011 * of the discovery state machine.
2014 lpfc_nvmet_unsol_fcp_buffer(struct lpfc_hba *phba,
2016 struct rqb_dmabuf *nvmebuf,
2017 uint64_t isr_timestamp)
2019 struct lpfc_nvmet_rcv_ctx *ctxp;
2020 struct lpfc_nvmet_tgtport *tgtp;
2021 struct fc_frame_header *fc_hdr;
2022 struct lpfc_nvmet_ctxbuf *ctx_buf;
2023 struct lpfc_nvmet_ctx_info *current_infop;
2024 uint32_t size, oxid, sid, qno;
2025 unsigned long iflag;
2028 if (!IS_ENABLED(CONFIG_NVME_TARGET_FC))
2032 if (!nvmebuf || !phba->targetport) {
2033 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2034 "6157 NVMET FCP Drop IO\n");
2036 lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2041 * Get a pointer to the context list for this MRQ based on
2042 * the CPU this MRQ IRQ is associated with. If the CPU association
2043 * changes from our initial assumption, the context list could
2044 * be empty, thus it would need to be replenished with the
2045 * context list from another CPU for this MRQ.
2047 current_cpu = raw_smp_processor_id();
2048 current_infop = lpfc_get_ctx_list(phba, current_cpu, idx);
2049 spin_lock_irqsave(¤t_infop->nvmet_ctx_list_lock, iflag);
2050 if (current_infop->nvmet_ctx_list_cnt) {
2051 list_remove_head(¤t_infop->nvmet_ctx_list,
2052 ctx_buf, struct lpfc_nvmet_ctxbuf, list);
2053 current_infop->nvmet_ctx_list_cnt--;
2055 ctx_buf = lpfc_nvmet_replenish_context(phba, current_infop);
2057 spin_unlock_irqrestore(¤t_infop->nvmet_ctx_list_lock, iflag);
2059 fc_hdr = (struct fc_frame_header *)(nvmebuf->hbuf.virt);
2060 oxid = be16_to_cpu(fc_hdr->fh_ox_id);
2061 size = nvmebuf->bytes_recv;
2063 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2064 if (phba->cpucheck_on & LPFC_CHECK_NVMET_RCV) {
2065 if (current_cpu < LPFC_CHECK_CPU_CNT) {
2066 if (idx != current_cpu)
2067 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2068 "6703 CPU Check rcv: "
2069 "cpu %d expect %d\n",
2071 phba->sli4_hba.hdwq[idx].cpucheck_rcv_io[current_cpu]++;
2076 lpfc_nvmeio_data(phba, "NVMET FCP RCV: xri x%x sz %d CPU %02x\n",
2077 oxid, size, raw_smp_processor_id());
2079 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2082 /* Queue this NVME IO to process later */
2083 spin_lock_irqsave(&phba->sli4_hba.nvmet_io_wait_lock, iflag);
2084 list_add_tail(&nvmebuf->hbuf.list,
2085 &phba->sli4_hba.lpfc_nvmet_io_wait_list);
2086 phba->sli4_hba.nvmet_io_wait_cnt++;
2087 phba->sli4_hba.nvmet_io_wait_total++;
2088 spin_unlock_irqrestore(&phba->sli4_hba.nvmet_io_wait_lock,
2091 /* Post a brand new DMA buffer to RQ */
2093 lpfc_post_rq_buffer(
2094 phba, phba->sli4_hba.nvmet_mrq_hdr[qno],
2095 phba->sli4_hba.nvmet_mrq_data[qno], 1, qno);
2097 atomic_inc(&tgtp->defer_ctx);
2101 sid = sli4_sid_from_fc_hdr(fc_hdr);
2103 ctxp = (struct lpfc_nvmet_rcv_ctx *)ctx_buf->context;
2104 if (ctxp->state != LPFC_NVMET_STE_FREE) {
2105 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2106 "6414 NVMET Context corrupt %d %d oxid x%x\n",
2107 ctxp->state, ctxp->entry_cnt, ctxp->oxid);
2117 ctxp->state = LPFC_NVMET_STE_RCV;
2118 ctxp->entry_cnt = 1;
2120 ctxp->ctxbuf = ctx_buf;
2121 ctxp->rqb_buffer = (void *)nvmebuf;
2123 spin_lock_init(&ctxp->ctxlock);
2125 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2126 if (isr_timestamp) {
2127 ctxp->ts_isr_cmd = isr_timestamp;
2128 ctxp->ts_cmd_nvme = ktime_get_ns();
2129 ctxp->ts_nvme_data = 0;
2130 ctxp->ts_data_wqput = 0;
2131 ctxp->ts_isr_data = 0;
2132 ctxp->ts_data_nvme = 0;
2133 ctxp->ts_nvme_status = 0;
2134 ctxp->ts_status_wqput = 0;
2135 ctxp->ts_isr_status = 0;
2136 ctxp->ts_status_nvme = 0;
2138 ctxp->ts_cmd_nvme = 0;
2142 atomic_inc(&tgtp->rcv_fcp_cmd_in);
2143 lpfc_nvmet_process_rcv_fcp_req(ctx_buf);
2147 * lpfc_nvmet_unsol_ls_event - Process an unsolicited event from an nvme nport
2148 * @phba: pointer to lpfc hba data structure.
2149 * @pring: pointer to a SLI ring.
2150 * @nvmebuf: pointer to received nvme data structure.
2152 * This routine is used to process an unsolicited event received from a SLI
2153 * (Service Level Interface) ring. The actual processing of the data buffer
2154 * associated with the unsolicited event is done by invoking the routine
2155 * lpfc_nvmet_unsol_ls_buffer() after properly set up the buffer from the
2156 * SLI RQ on which the unsolicited event was received.
2159 lpfc_nvmet_unsol_ls_event(struct lpfc_hba *phba, struct lpfc_sli_ring *pring,
2160 struct lpfc_iocbq *piocb)
2162 struct lpfc_dmabuf *d_buf;
2163 struct hbq_dmabuf *nvmebuf;
2165 d_buf = piocb->context2;
2166 nvmebuf = container_of(d_buf, struct hbq_dmabuf, dbuf);
2168 if (phba->nvmet_support == 0) {
2169 lpfc_in_buf_free(phba, &nvmebuf->dbuf);
2172 lpfc_nvmet_unsol_ls_buffer(phba, pring, nvmebuf);
2176 * lpfc_nvmet_unsol_fcp_event - Process an unsolicited event from an nvme nport
2177 * @phba: pointer to lpfc hba data structure.
2178 * @idx: relative index of MRQ vector
2179 * @nvmebuf: pointer to received nvme data structure.
2181 * This routine is used to process an unsolicited event received from a SLI
2182 * (Service Level Interface) ring. The actual processing of the data buffer
2183 * associated with the unsolicited event is done by invoking the routine
2184 * lpfc_nvmet_unsol_fcp_buffer() after properly set up the buffer from the
2185 * SLI RQ on which the unsolicited event was received.
2188 lpfc_nvmet_unsol_fcp_event(struct lpfc_hba *phba,
2190 struct rqb_dmabuf *nvmebuf,
2191 uint64_t isr_timestamp)
2193 if (phba->nvmet_support == 0) {
2194 lpfc_rq_buf_free(phba, &nvmebuf->hbuf);
2197 lpfc_nvmet_unsol_fcp_buffer(phba, idx, nvmebuf,
2202 * lpfc_nvmet_prep_ls_wqe - Allocate and prepare a lpfc wqe data structure
2203 * @phba: pointer to a host N_Port data structure.
2204 * @ctxp: Context info for NVME LS Request
2205 * @rspbuf: DMA buffer of NVME command.
2206 * @rspsize: size of the NVME command.
2208 * This routine is used for allocating a lpfc-WQE data structure from
2209 * the driver lpfc-WQE free-list and prepare the WQE with the parameters
2210 * passed into the routine for discovery state machine to issue an Extended
2211 * Link Service (NVME) commands. It is a generic lpfc-WQE allocation
2212 * and preparation routine that is used by all the discovery state machine
2213 * routines and the NVME command-specific fields will be later set up by
2214 * the individual discovery machine routines after calling this routine
2215 * allocating and preparing a generic WQE data structure. It fills in the
2216 * Buffer Descriptor Entries (BDEs), allocates buffers for both command
2217 * payload and response payload (if expected). The reference count on the
2218 * ndlp is incremented by 1 and the reference to the ndlp is put into
2219 * context1 of the WQE data structure for this WQE to hold the ndlp
2220 * reference for the command's callback function to access later.
2223 * Pointer to the newly allocated/prepared nvme wqe data structure
2224 * NULL - when nvme wqe data structure allocation/preparation failed
2226 static struct lpfc_iocbq *
2227 lpfc_nvmet_prep_ls_wqe(struct lpfc_hba *phba,
2228 struct lpfc_nvmet_rcv_ctx *ctxp,
2229 dma_addr_t rspbuf, uint16_t rspsize)
2231 struct lpfc_nodelist *ndlp;
2232 struct lpfc_iocbq *nvmewqe;
2233 union lpfc_wqe128 *wqe;
2235 if (!lpfc_is_link_up(phba)) {
2236 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2237 "6104 NVMET prep LS wqe: link err: "
2238 "NPORT x%x oxid:x%x ste %d\n",
2239 ctxp->sid, ctxp->oxid, ctxp->state);
2243 /* Allocate buffer for command wqe */
2244 nvmewqe = lpfc_sli_get_iocbq(phba);
2245 if (nvmewqe == NULL) {
2246 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2247 "6105 NVMET prep LS wqe: No WQE: "
2248 "NPORT x%x oxid x%x ste %d\n",
2249 ctxp->sid, ctxp->oxid, ctxp->state);
2253 ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2254 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2255 ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2256 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2257 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_DISC,
2258 "6106 NVMET prep LS wqe: No ndlp: "
2259 "NPORT x%x oxid x%x ste %d\n",
2260 ctxp->sid, ctxp->oxid, ctxp->state);
2261 goto nvme_wqe_free_wqeq_exit;
2263 ctxp->wqeq = nvmewqe;
2265 /* prevent preparing wqe with NULL ndlp reference */
2266 nvmewqe->context1 = lpfc_nlp_get(ndlp);
2267 if (nvmewqe->context1 == NULL)
2268 goto nvme_wqe_free_wqeq_exit;
2269 nvmewqe->context2 = ctxp;
2271 wqe = &nvmewqe->wqe;
2272 memset(wqe, 0, sizeof(union lpfc_wqe));
2275 wqe->xmit_sequence.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2276 wqe->xmit_sequence.bde.tus.f.bdeSize = rspsize;
2277 wqe->xmit_sequence.bde.addrLow = le32_to_cpu(putPaddrLow(rspbuf));
2278 wqe->xmit_sequence.bde.addrHigh = le32_to_cpu(putPaddrHigh(rspbuf));
2285 bf_set(wqe_dfctl, &wqe->xmit_sequence.wge_ctl, 0);
2286 bf_set(wqe_ls, &wqe->xmit_sequence.wge_ctl, 1);
2287 bf_set(wqe_la, &wqe->xmit_sequence.wge_ctl, 0);
2288 bf_set(wqe_rctl, &wqe->xmit_sequence.wge_ctl, FC_RCTL_ELS4_REP);
2289 bf_set(wqe_type, &wqe->xmit_sequence.wge_ctl, FC_TYPE_NVME);
2292 bf_set(wqe_ctxt_tag, &wqe->xmit_sequence.wqe_com,
2293 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2294 bf_set(wqe_xri_tag, &wqe->xmit_sequence.wqe_com, nvmewqe->sli4_xritag);
2297 bf_set(wqe_cmnd, &wqe->xmit_sequence.wqe_com,
2298 CMD_XMIT_SEQUENCE64_WQE);
2299 bf_set(wqe_ct, &wqe->xmit_sequence.wqe_com, SLI4_CT_RPI);
2300 bf_set(wqe_class, &wqe->xmit_sequence.wqe_com, CLASS3);
2301 bf_set(wqe_pu, &wqe->xmit_sequence.wqe_com, 0);
2304 wqe->xmit_sequence.wqe_com.abort_tag = nvmewqe->iotag;
2307 bf_set(wqe_reqtag, &wqe->xmit_sequence.wqe_com, nvmewqe->iotag);
2308 /* Needs to be set by caller */
2309 bf_set(wqe_rcvoxid, &wqe->xmit_sequence.wqe_com, ctxp->oxid);
2312 bf_set(wqe_dbde, &wqe->xmit_sequence.wqe_com, 1);
2313 bf_set(wqe_iod, &wqe->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2314 bf_set(wqe_lenloc, &wqe->xmit_sequence.wqe_com,
2315 LPFC_WQE_LENLOC_WORD12);
2316 bf_set(wqe_ebde_cnt, &wqe->xmit_sequence.wqe_com, 0);
2319 bf_set(wqe_cqid, &wqe->xmit_sequence.wqe_com,
2320 LPFC_WQE_CQ_ID_DEFAULT);
2321 bf_set(wqe_cmd_type, &wqe->xmit_sequence.wqe_com,
2325 wqe->xmit_sequence.xmit_len = rspsize;
2328 nvmewqe->vport = phba->pport;
2329 nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2330 nvmewqe->iocb_flag |= LPFC_IO_NVME_LS;
2332 /* Xmit NVMET response to remote NPORT <did> */
2333 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_DISC,
2334 "6039 Xmit NVMET LS response to remote "
2335 "NPORT x%x iotag:x%x oxid:x%x size:x%x\n",
2336 ndlp->nlp_DID, nvmewqe->iotag, ctxp->oxid,
2340 nvme_wqe_free_wqeq_exit:
2341 nvmewqe->context2 = NULL;
2342 nvmewqe->context3 = NULL;
2343 lpfc_sli_release_iocbq(phba, nvmewqe);
2348 static struct lpfc_iocbq *
2349 lpfc_nvmet_prep_fcp_wqe(struct lpfc_hba *phba,
2350 struct lpfc_nvmet_rcv_ctx *ctxp)
2352 struct nvmefc_tgt_fcp_req *rsp = &ctxp->ctx.fcp_req;
2353 struct lpfc_nvmet_tgtport *tgtp;
2354 struct sli4_sge *sgl;
2355 struct lpfc_nodelist *ndlp;
2356 struct lpfc_iocbq *nvmewqe;
2357 struct scatterlist *sgel;
2358 union lpfc_wqe128 *wqe;
2359 struct ulp_bde64 *bde;
2361 dma_addr_t physaddr;
2366 if (!lpfc_is_link_up(phba)) {
2367 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2368 "6107 NVMET prep FCP wqe: link err:"
2369 "NPORT x%x oxid x%x ste %d\n",
2370 ctxp->sid, ctxp->oxid, ctxp->state);
2374 ndlp = lpfc_findnode_did(phba->pport, ctxp->sid);
2375 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2376 ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2377 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2378 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2379 "6108 NVMET prep FCP wqe: no ndlp: "
2380 "NPORT x%x oxid x%x ste %d\n",
2381 ctxp->sid, ctxp->oxid, ctxp->state);
2385 if (rsp->sg_cnt > lpfc_tgttemplate.max_sgl_segments) {
2386 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2387 "6109 NVMET prep FCP wqe: seg cnt err: "
2388 "NPORT x%x oxid x%x ste %d cnt %d\n",
2389 ctxp->sid, ctxp->oxid, ctxp->state,
2390 phba->cfg_nvme_seg_cnt);
2394 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2395 nvmewqe = ctxp->wqeq;
2396 if (nvmewqe == NULL) {
2397 /* Allocate buffer for command wqe */
2398 nvmewqe = ctxp->ctxbuf->iocbq;
2399 if (nvmewqe == NULL) {
2400 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2401 "6110 NVMET prep FCP wqe: No "
2402 "WQE: NPORT x%x oxid x%x ste %d\n",
2403 ctxp->sid, ctxp->oxid, ctxp->state);
2406 ctxp->wqeq = nvmewqe;
2407 xc = 0; /* create new XRI */
2408 nvmewqe->sli4_lxritag = NO_XRI;
2409 nvmewqe->sli4_xritag = NO_XRI;
2413 if (((ctxp->state == LPFC_NVMET_STE_RCV) &&
2414 (ctxp->entry_cnt == 1)) ||
2415 (ctxp->state == LPFC_NVMET_STE_DATA)) {
2416 wqe = &nvmewqe->wqe;
2418 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2419 "6111 Wrong state NVMET FCP: %d cnt %d\n",
2420 ctxp->state, ctxp->entry_cnt);
2424 sgl = (struct sli4_sge *)ctxp->ctxbuf->sglq->sgl;
2426 case NVMET_FCOP_READDATA:
2427 case NVMET_FCOP_READDATA_RSP:
2428 /* From the tsend template, initialize words 7 - 11 */
2429 memcpy(&wqe->words[7],
2430 &lpfc_tsend_cmd_template.words[7],
2431 sizeof(uint32_t) * 5);
2433 /* Words 0 - 2 : The first sg segment */
2435 physaddr = sg_dma_address(sgel);
2436 wqe->fcp_tsend.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2437 wqe->fcp_tsend.bde.tus.f.bdeSize = sg_dma_len(sgel);
2438 wqe->fcp_tsend.bde.addrLow = cpu_to_le32(putPaddrLow(physaddr));
2439 wqe->fcp_tsend.bde.addrHigh =
2440 cpu_to_le32(putPaddrHigh(physaddr));
2443 wqe->fcp_tsend.payload_offset_len = 0;
2446 wqe->fcp_tsend.relative_offset = ctxp->offset;
2449 wqe->fcp_tsend.reserved = 0;
2452 bf_set(wqe_ctxt_tag, &wqe->fcp_tsend.wqe_com,
2453 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2454 bf_set(wqe_xri_tag, &wqe->fcp_tsend.wqe_com,
2455 nvmewqe->sli4_xritag);
2457 /* Word 7 - set ar later */
2460 wqe->fcp_tsend.wqe_com.abort_tag = nvmewqe->iotag;
2463 bf_set(wqe_reqtag, &wqe->fcp_tsend.wqe_com, nvmewqe->iotag);
2464 bf_set(wqe_rcvoxid, &wqe->fcp_tsend.wqe_com, ctxp->oxid);
2466 /* Word 10 - set wqes later, in template xc=1 */
2468 bf_set(wqe_xc, &wqe->fcp_tsend.wqe_com, 0);
2470 /* Word 11 - set sup, irsp, irsplen later */
2474 wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2476 /* Setup 2 SKIP SGEs */
2480 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2481 sgl->word2 = cpu_to_le32(sgl->word2);
2487 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2488 sgl->word2 = cpu_to_le32(sgl->word2);
2491 if (rsp->op == NVMET_FCOP_READDATA_RSP) {
2492 atomic_inc(&tgtp->xmt_fcp_read_rsp);
2494 /* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2496 if (rsp->rsplen == LPFC_NVMET_SUCCESS_LEN) {
2497 if (ndlp->nlp_flag & NLP_SUPPRESS_RSP)
2499 &wqe->fcp_tsend.wqe_com, 1);
2501 bf_set(wqe_wqes, &wqe->fcp_tsend.wqe_com, 1);
2502 bf_set(wqe_irsp, &wqe->fcp_tsend.wqe_com, 1);
2503 bf_set(wqe_irsplen, &wqe->fcp_tsend.wqe_com,
2504 ((rsp->rsplen >> 2) - 1));
2505 memcpy(&wqe->words[16], rsp->rspaddr,
2509 atomic_inc(&tgtp->xmt_fcp_read);
2511 /* In template ar=1 wqes=0 sup=0 irsp=0 irsplen=0 */
2512 bf_set(wqe_ar, &wqe->fcp_tsend.wqe_com, 0);
2516 case NVMET_FCOP_WRITEDATA:
2517 /* From the treceive template, initialize words 3 - 11 */
2518 memcpy(&wqe->words[3],
2519 &lpfc_treceive_cmd_template.words[3],
2520 sizeof(uint32_t) * 9);
2522 /* Words 0 - 2 : The first sg segment */
2523 txrdy = dma_pool_alloc(phba->txrdy_payload_pool,
2524 GFP_KERNEL, &physaddr);
2526 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2527 "6041 Bad txrdy buffer: oxid x%x\n",
2531 ctxp->txrdy = txrdy;
2532 ctxp->txrdy_phys = physaddr;
2533 wqe->fcp_treceive.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2534 wqe->fcp_treceive.bde.tus.f.bdeSize = TXRDY_PAYLOAD_LEN;
2535 wqe->fcp_treceive.bde.addrLow =
2536 cpu_to_le32(putPaddrLow(physaddr));
2537 wqe->fcp_treceive.bde.addrHigh =
2538 cpu_to_le32(putPaddrHigh(physaddr));
2541 wqe->fcp_treceive.relative_offset = ctxp->offset;
2544 bf_set(wqe_ctxt_tag, &wqe->fcp_treceive.wqe_com,
2545 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2546 bf_set(wqe_xri_tag, &wqe->fcp_treceive.wqe_com,
2547 nvmewqe->sli4_xritag);
2552 wqe->fcp_treceive.wqe_com.abort_tag = nvmewqe->iotag;
2555 bf_set(wqe_reqtag, &wqe->fcp_treceive.wqe_com, nvmewqe->iotag);
2556 bf_set(wqe_rcvoxid, &wqe->fcp_treceive.wqe_com, ctxp->oxid);
2558 /* Word 10 - in template xc=1 */
2560 bf_set(wqe_xc, &wqe->fcp_treceive.wqe_com, 0);
2562 /* Word 11 - set pbde later */
2563 if (phba->cfg_enable_pbde) {
2566 bf_set(wqe_pbde, &wqe->fcp_treceive.wqe_com, 0);
2571 wqe->fcp_tsend.fcp_data_len = rsp->transfer_length;
2573 /* Setup 1 TXRDY and 1 SKIP SGE */
2575 txrdy[1] = cpu_to_be32(rsp->transfer_length);
2578 sgl->addr_hi = putPaddrHigh(physaddr);
2579 sgl->addr_lo = putPaddrLow(physaddr);
2581 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2582 sgl->word2 = cpu_to_le32(sgl->word2);
2583 sgl->sge_len = cpu_to_le32(TXRDY_PAYLOAD_LEN);
2588 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
2589 sgl->word2 = cpu_to_le32(sgl->word2);
2592 atomic_inc(&tgtp->xmt_fcp_write);
2595 case NVMET_FCOP_RSP:
2596 /* From the treceive template, initialize words 4 - 11 */
2597 memcpy(&wqe->words[4],
2598 &lpfc_trsp_cmd_template.words[4],
2599 sizeof(uint32_t) * 8);
2602 physaddr = rsp->rspdma;
2603 wqe->fcp_trsp.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2604 wqe->fcp_trsp.bde.tus.f.bdeSize = rsp->rsplen;
2605 wqe->fcp_trsp.bde.addrLow =
2606 cpu_to_le32(putPaddrLow(physaddr));
2607 wqe->fcp_trsp.bde.addrHigh =
2608 cpu_to_le32(putPaddrHigh(physaddr));
2611 wqe->fcp_trsp.response_len = rsp->rsplen;
2614 bf_set(wqe_ctxt_tag, &wqe->fcp_trsp.wqe_com,
2615 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2616 bf_set(wqe_xri_tag, &wqe->fcp_trsp.wqe_com,
2617 nvmewqe->sli4_xritag);
2622 wqe->fcp_trsp.wqe_com.abort_tag = nvmewqe->iotag;
2625 bf_set(wqe_reqtag, &wqe->fcp_trsp.wqe_com, nvmewqe->iotag);
2626 bf_set(wqe_rcvoxid, &wqe->fcp_trsp.wqe_com, ctxp->oxid);
2630 bf_set(wqe_xc, &wqe->fcp_trsp.wqe_com, 1);
2633 /* In template wqes=0 irsp=0 irsplen=0 - good response */
2634 if (rsp->rsplen != LPFC_NVMET_SUCCESS_LEN) {
2635 /* Bad response - embed it */
2636 bf_set(wqe_wqes, &wqe->fcp_trsp.wqe_com, 1);
2637 bf_set(wqe_irsp, &wqe->fcp_trsp.wqe_com, 1);
2638 bf_set(wqe_irsplen, &wqe->fcp_trsp.wqe_com,
2639 ((rsp->rsplen >> 2) - 1));
2640 memcpy(&wqe->words[16], rsp->rspaddr, rsp->rsplen);
2645 wqe->fcp_trsp.rsvd_12_15[0] = 0;
2647 /* Use rspbuf, NOT sg list */
2650 atomic_inc(&tgtp->xmt_fcp_rsp);
2654 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_IOERR,
2655 "6064 Unknown Rsp Op %d\n",
2661 nvmewqe->vport = phba->pport;
2662 nvmewqe->drvrTimeout = (phba->fc_ratov * 3) + LPFC_DRVR_TIMEOUT;
2663 nvmewqe->context1 = ndlp;
2665 for_each_sg(rsp->sg, sgel, rsp->sg_cnt, i) {
2666 physaddr = sg_dma_address(sgel);
2667 cnt = sg_dma_len(sgel);
2668 sgl->addr_hi = putPaddrHigh(physaddr);
2669 sgl->addr_lo = putPaddrLow(physaddr);
2671 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2672 bf_set(lpfc_sli4_sge_offset, sgl, ctxp->offset);
2673 if ((i+1) == rsp->sg_cnt)
2674 bf_set(lpfc_sli4_sge_last, sgl, 1);
2675 sgl->word2 = cpu_to_le32(sgl->word2);
2676 sgl->sge_len = cpu_to_le32(cnt);
2678 bde = (struct ulp_bde64 *)&wqe->words[13];
2680 /* Words 13-15 (PBDE) */
2681 bde->addrLow = sgl->addr_lo;
2682 bde->addrHigh = sgl->addr_hi;
2683 bde->tus.f.bdeSize =
2684 le32_to_cpu(sgl->sge_len);
2685 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2686 bde->tus.w = cpu_to_le32(bde->tus.w);
2688 memset(bde, 0, sizeof(struct ulp_bde64));
2692 ctxp->offset += cnt;
2694 ctxp->state = LPFC_NVMET_STE_DATA;
2700 * lpfc_nvmet_sol_fcp_abort_cmp - Completion handler for ABTS
2701 * @phba: Pointer to HBA context object.
2702 * @cmdwqe: Pointer to driver command WQE object.
2703 * @wcqe: Pointer to driver response CQE object.
2705 * The function is called from SLI ring event handler with no
2706 * lock held. This function is the completion handler for NVME ABTS for FCP cmds
2707 * The function frees memory resources used for the NVME commands.
2710 lpfc_nvmet_sol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2711 struct lpfc_wcqe_complete *wcqe)
2713 struct lpfc_nvmet_rcv_ctx *ctxp;
2714 struct lpfc_nvmet_tgtport *tgtp;
2716 unsigned long flags;
2717 bool released = false;
2719 ctxp = cmdwqe->context2;
2720 result = wcqe->parameter;
2722 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2723 if (ctxp->flag & LPFC_NVMET_ABORT_OP)
2724 atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
2726 spin_lock_irqsave(&ctxp->ctxlock, flags);
2727 ctxp->state = LPFC_NVMET_STE_DONE;
2729 /* Check if we already received a free context call
2730 * and we have completed processing an abort situation.
2732 if ((ctxp->flag & LPFC_NVMET_CTX_RLS) &&
2733 !(ctxp->flag & LPFC_NVMET_XBUSY)) {
2734 spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
2735 list_del(&ctxp->list);
2736 spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
2739 ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
2740 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
2741 atomic_inc(&tgtp->xmt_abort_rsp);
2743 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2744 "6165 ABORT cmpl: xri x%x flg x%x (%d) "
2745 "WCQE: %08x %08x %08x %08x\n",
2746 ctxp->oxid, ctxp->flag, released,
2747 wcqe->word0, wcqe->total_data_placed,
2748 result, wcqe->word3);
2750 cmdwqe->context2 = NULL;
2751 cmdwqe->context3 = NULL;
2753 * if transport has released ctx, then can reuse it. Otherwise,
2754 * will be recycled by transport release call.
2757 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
2759 /* This is the iocbq for the abort, not the command */
2760 lpfc_sli_release_iocbq(phba, cmdwqe);
2762 /* Since iaab/iaar are NOT set, there is no work left.
2763 * For LPFC_NVMET_XBUSY, lpfc_sli4_nvmet_xri_aborted
2764 * should have been called already.
2769 * lpfc_nvmet_unsol_fcp_abort_cmp - Completion handler for ABTS
2770 * @phba: Pointer to HBA context object.
2771 * @cmdwqe: Pointer to driver command WQE object.
2772 * @wcqe: Pointer to driver response CQE object.
2774 * The function is called from SLI ring event handler with no
2775 * lock held. This function is the completion handler for NVME ABTS for FCP cmds
2776 * The function frees memory resources used for the NVME commands.
2779 lpfc_nvmet_unsol_fcp_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2780 struct lpfc_wcqe_complete *wcqe)
2782 struct lpfc_nvmet_rcv_ctx *ctxp;
2783 struct lpfc_nvmet_tgtport *tgtp;
2784 unsigned long flags;
2786 bool released = false;
2788 ctxp = cmdwqe->context2;
2789 result = wcqe->parameter;
2792 /* if context is clear, related io alrady complete */
2793 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2794 "6070 ABTS cmpl: WCQE: %08x %08x %08x %08x\n",
2795 wcqe->word0, wcqe->total_data_placed,
2796 result, wcqe->word3);
2800 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2801 spin_lock_irqsave(&ctxp->ctxlock, flags);
2802 if (ctxp->flag & LPFC_NVMET_ABORT_OP)
2803 atomic_inc(&tgtp->xmt_fcp_abort_cmpl);
2806 if (ctxp->state != LPFC_NVMET_STE_ABORT) {
2807 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2808 "6112 ABTS Wrong state:%d oxid x%x\n",
2809 ctxp->state, ctxp->oxid);
2812 /* Check if we already received a free context call
2813 * and we have completed processing an abort situation.
2815 ctxp->state = LPFC_NVMET_STE_DONE;
2816 if ((ctxp->flag & LPFC_NVMET_CTX_RLS) &&
2817 !(ctxp->flag & LPFC_NVMET_XBUSY)) {
2818 spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
2819 list_del(&ctxp->list);
2820 spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
2823 ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
2824 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
2825 atomic_inc(&tgtp->xmt_abort_rsp);
2827 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2828 "6316 ABTS cmpl xri x%x flg x%x (%x) "
2829 "WCQE: %08x %08x %08x %08x\n",
2830 ctxp->oxid, ctxp->flag, released,
2831 wcqe->word0, wcqe->total_data_placed,
2832 result, wcqe->word3);
2834 cmdwqe->context2 = NULL;
2835 cmdwqe->context3 = NULL;
2837 * if transport has released ctx, then can reuse it. Otherwise,
2838 * will be recycled by transport release call.
2841 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
2843 /* Since iaab/iaar are NOT set, there is no work left.
2844 * For LPFC_NVMET_XBUSY, lpfc_sli4_nvmet_xri_aborted
2845 * should have been called already.
2850 * lpfc_nvmet_xmt_ls_abort_cmp - Completion handler for ABTS
2851 * @phba: Pointer to HBA context object.
2852 * @cmdwqe: Pointer to driver command WQE object.
2853 * @wcqe: Pointer to driver response CQE object.
2855 * The function is called from SLI ring event handler with no
2856 * lock held. This function is the completion handler for NVME ABTS for LS cmds
2857 * The function frees memory resources used for the NVME commands.
2860 lpfc_nvmet_xmt_ls_abort_cmp(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
2861 struct lpfc_wcqe_complete *wcqe)
2863 struct lpfc_nvmet_rcv_ctx *ctxp;
2864 struct lpfc_nvmet_tgtport *tgtp;
2867 ctxp = cmdwqe->context2;
2868 result = wcqe->parameter;
2870 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2871 atomic_inc(&tgtp->xmt_ls_abort_cmpl);
2873 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2874 "6083 Abort cmpl: ctx %p WCQE:%08x %08x %08x %08x\n",
2875 ctxp, wcqe->word0, wcqe->total_data_placed,
2876 result, wcqe->word3);
2879 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2880 "6415 NVMET LS Abort No ctx: WCQE: "
2881 "%08x %08x %08x %08x\n",
2882 wcqe->word0, wcqe->total_data_placed,
2883 result, wcqe->word3);
2885 lpfc_sli_release_iocbq(phba, cmdwqe);
2889 if (ctxp->state != LPFC_NVMET_STE_LS_ABORT) {
2890 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
2891 "6416 NVMET LS abort cmpl state mismatch: "
2892 "oxid x%x: %d %d\n",
2893 ctxp->oxid, ctxp->state, ctxp->entry_cnt);
2896 cmdwqe->context2 = NULL;
2897 cmdwqe->context3 = NULL;
2898 lpfc_sli_release_iocbq(phba, cmdwqe);
2903 lpfc_nvmet_unsol_issue_abort(struct lpfc_hba *phba,
2904 struct lpfc_nvmet_rcv_ctx *ctxp,
2905 uint32_t sid, uint16_t xri)
2907 struct lpfc_nvmet_tgtport *tgtp;
2908 struct lpfc_iocbq *abts_wqeq;
2909 union lpfc_wqe128 *wqe_abts;
2910 struct lpfc_nodelist *ndlp;
2912 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2913 "6067 ABTS: sid %x xri x%x/x%x\n",
2914 sid, xri, ctxp->wqeq->sli4_xritag);
2916 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
2918 ndlp = lpfc_findnode_did(phba->pport, sid);
2919 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2920 ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
2921 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
2922 atomic_inc(&tgtp->xmt_abort_rsp_error);
2923 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
2924 "6134 Drop ABTS - wrong NDLP state x%x.\n",
2925 (ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
2927 /* No failure to an ABTS request. */
2931 abts_wqeq = ctxp->wqeq;
2932 wqe_abts = &abts_wqeq->wqe;
2935 * Since we zero the whole WQE, we need to ensure we set the WQE fields
2936 * that were initialized in lpfc_sli4_nvmet_alloc.
2938 memset(wqe_abts, 0, sizeof(union lpfc_wqe));
2941 bf_set(wqe_dfctl, &wqe_abts->xmit_sequence.wge_ctl, 0);
2942 bf_set(wqe_ls, &wqe_abts->xmit_sequence.wge_ctl, 1);
2943 bf_set(wqe_la, &wqe_abts->xmit_sequence.wge_ctl, 0);
2944 bf_set(wqe_rctl, &wqe_abts->xmit_sequence.wge_ctl, FC_RCTL_BA_ABTS);
2945 bf_set(wqe_type, &wqe_abts->xmit_sequence.wge_ctl, FC_TYPE_BLS);
2948 bf_set(wqe_ctxt_tag, &wqe_abts->xmit_sequence.wqe_com,
2949 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
2950 bf_set(wqe_xri_tag, &wqe_abts->xmit_sequence.wqe_com,
2951 abts_wqeq->sli4_xritag);
2954 bf_set(wqe_cmnd, &wqe_abts->xmit_sequence.wqe_com,
2955 CMD_XMIT_SEQUENCE64_WQE);
2956 bf_set(wqe_ct, &wqe_abts->xmit_sequence.wqe_com, SLI4_CT_RPI);
2957 bf_set(wqe_class, &wqe_abts->xmit_sequence.wqe_com, CLASS3);
2958 bf_set(wqe_pu, &wqe_abts->xmit_sequence.wqe_com, 0);
2961 wqe_abts->xmit_sequence.wqe_com.abort_tag = abts_wqeq->iotag;
2964 bf_set(wqe_reqtag, &wqe_abts->xmit_sequence.wqe_com, abts_wqeq->iotag);
2965 /* Needs to be set by caller */
2966 bf_set(wqe_rcvoxid, &wqe_abts->xmit_sequence.wqe_com, xri);
2969 bf_set(wqe_dbde, &wqe_abts->xmit_sequence.wqe_com, 1);
2970 bf_set(wqe_iod, &wqe_abts->xmit_sequence.wqe_com, LPFC_WQE_IOD_WRITE);
2971 bf_set(wqe_lenloc, &wqe_abts->xmit_sequence.wqe_com,
2972 LPFC_WQE_LENLOC_WORD12);
2973 bf_set(wqe_ebde_cnt, &wqe_abts->xmit_sequence.wqe_com, 0);
2974 bf_set(wqe_qosd, &wqe_abts->xmit_sequence.wqe_com, 0);
2977 bf_set(wqe_cqid, &wqe_abts->xmit_sequence.wqe_com,
2978 LPFC_WQE_CQ_ID_DEFAULT);
2979 bf_set(wqe_cmd_type, &wqe_abts->xmit_sequence.wqe_com,
2982 abts_wqeq->vport = phba->pport;
2983 abts_wqeq->context1 = ndlp;
2984 abts_wqeq->context2 = ctxp;
2985 abts_wqeq->context3 = NULL;
2986 abts_wqeq->rsvd2 = 0;
2987 /* hba_wqidx should already be setup from command we are aborting */
2988 abts_wqeq->iocb.ulpCommand = CMD_XMIT_SEQUENCE64_CR;
2989 abts_wqeq->iocb.ulpLe = 1;
2991 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
2992 "6069 Issue ABTS to xri x%x reqtag x%x\n",
2993 xri, abts_wqeq->iotag);
2998 lpfc_nvmet_sol_fcp_issue_abort(struct lpfc_hba *phba,
2999 struct lpfc_nvmet_rcv_ctx *ctxp,
3000 uint32_t sid, uint16_t xri)
3002 struct lpfc_nvmet_tgtport *tgtp;
3003 struct lpfc_iocbq *abts_wqeq;
3004 union lpfc_wqe128 *abts_wqe;
3005 struct lpfc_nodelist *ndlp;
3006 unsigned long flags;
3009 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3011 ctxp->wqeq = ctxp->ctxbuf->iocbq;
3012 ctxp->wqeq->hba_wqidx = 0;
3015 ndlp = lpfc_findnode_did(phba->pport, sid);
3016 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3017 ((ndlp->nlp_state != NLP_STE_UNMAPPED_NODE) &&
3018 (ndlp->nlp_state != NLP_STE_MAPPED_NODE))) {
3019 atomic_inc(&tgtp->xmt_abort_rsp_error);
3020 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3021 "6160 Drop ABORT - wrong NDLP state x%x.\n",
3022 (ndlp) ? ndlp->nlp_state : NLP_STE_MAX_STATE);
3024 /* No failure to an ABTS request. */
3025 spin_lock_irqsave(&ctxp->ctxlock, flags);
3026 ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3027 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3031 /* Issue ABTS for this WQE based on iotag */
3032 ctxp->abort_wqeq = lpfc_sli_get_iocbq(phba);
3033 spin_lock_irqsave(&ctxp->ctxlock, flags);
3034 if (!ctxp->abort_wqeq) {
3035 atomic_inc(&tgtp->xmt_abort_rsp_error);
3036 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3037 "6161 ABORT failed: No wqeqs: "
3038 "xri: x%x\n", ctxp->oxid);
3039 /* No failure to an ABTS request. */
3040 ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3041 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3044 abts_wqeq = ctxp->abort_wqeq;
3045 abts_wqe = &abts_wqeq->wqe;
3046 ctxp->state = LPFC_NVMET_STE_ABORT;
3047 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3049 /* Announce entry to new IO submit field. */
3050 lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
3051 "6162 ABORT Request to rport DID x%06x "
3052 "for xri x%x x%x\n",
3053 ctxp->sid, ctxp->oxid, ctxp->wqeq->sli4_xritag);
3055 /* If the hba is getting reset, this flag is set. It is
3056 * cleared when the reset is complete and rings reestablished.
3058 spin_lock_irqsave(&phba->hbalock, flags);
3059 /* driver queued commands are in process of being flushed */
3060 if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
3061 spin_unlock_irqrestore(&phba->hbalock, flags);
3062 atomic_inc(&tgtp->xmt_abort_rsp_error);
3063 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
3064 "6163 Driver in reset cleanup - flushing "
3065 "NVME Req now. hba_flag x%x oxid x%x\n",
3066 phba->hba_flag, ctxp->oxid);
3067 lpfc_sli_release_iocbq(phba, abts_wqeq);
3068 spin_lock_irqsave(&ctxp->ctxlock, flags);
3069 ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3070 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3074 /* Outstanding abort is in progress */
3075 if (abts_wqeq->iocb_flag & LPFC_DRIVER_ABORTED) {
3076 spin_unlock_irqrestore(&phba->hbalock, flags);
3077 atomic_inc(&tgtp->xmt_abort_rsp_error);
3078 lpfc_printf_log(phba, KERN_ERR, LOG_NVME,
3079 "6164 Outstanding NVME I/O Abort Request "
3080 "still pending on oxid x%x\n",
3082 lpfc_sli_release_iocbq(phba, abts_wqeq);
3083 spin_lock_irqsave(&ctxp->ctxlock, flags);
3084 ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3085 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3089 /* Ready - mark outstanding as aborted by driver. */
3090 abts_wqeq->iocb_flag |= LPFC_DRIVER_ABORTED;
3092 /* WQEs are reused. Clear stale data and set key fields to
3093 * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
3095 memset(abts_wqe, 0, sizeof(union lpfc_wqe));
3098 bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
3101 bf_set(wqe_ct, &abts_wqe->abort_cmd.wqe_com, 0);
3102 bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
3104 /* word 8 - tell the FW to abort the IO associated with this
3105 * outstanding exchange ID.
3107 abts_wqe->abort_cmd.wqe_com.abort_tag = ctxp->wqeq->sli4_xritag;
3109 /* word 9 - this is the iotag for the abts_wqe completion. */
3110 bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
3114 bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
3115 bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
3118 bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
3119 bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
3120 bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
3122 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3123 abts_wqeq->hba_wqidx = ctxp->wqeq->hba_wqidx;
3124 abts_wqeq->wqe_cmpl = lpfc_nvmet_sol_fcp_abort_cmp;
3125 abts_wqeq->iocb_cmpl = 0;
3126 abts_wqeq->iocb_flag |= LPFC_IO_NVME;
3127 abts_wqeq->context2 = ctxp;
3128 abts_wqeq->vport = phba->pport;
3130 ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
3132 rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3133 spin_unlock_irqrestore(&phba->hbalock, flags);
3134 if (rc == WQE_SUCCESS) {
3135 atomic_inc(&tgtp->xmt_abort_sol);
3139 atomic_inc(&tgtp->xmt_abort_rsp_error);
3140 spin_lock_irqsave(&ctxp->ctxlock, flags);
3141 ctxp->flag &= ~LPFC_NVMET_ABORT_OP;
3142 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3143 lpfc_sli_release_iocbq(phba, abts_wqeq);
3144 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3145 "6166 Failed ABORT issue_wqe with status x%x "
3152 lpfc_nvmet_unsol_fcp_issue_abort(struct lpfc_hba *phba,
3153 struct lpfc_nvmet_rcv_ctx *ctxp,
3154 uint32_t sid, uint16_t xri)
3156 struct lpfc_nvmet_tgtport *tgtp;
3157 struct lpfc_iocbq *abts_wqeq;
3158 unsigned long flags;
3159 bool released = false;
3162 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3164 ctxp->wqeq = ctxp->ctxbuf->iocbq;
3165 ctxp->wqeq->hba_wqidx = 0;
3168 if (ctxp->state == LPFC_NVMET_STE_FREE) {
3169 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3170 "6417 NVMET ABORT ctx freed %d %d oxid x%x\n",
3171 ctxp->state, ctxp->entry_cnt, ctxp->oxid);
3175 ctxp->state = LPFC_NVMET_STE_ABORT;
3177 rc = lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri);
3181 spin_lock_irqsave(&phba->hbalock, flags);
3182 abts_wqeq = ctxp->wqeq;
3183 abts_wqeq->wqe_cmpl = lpfc_nvmet_unsol_fcp_abort_cmp;
3184 abts_wqeq->iocb_cmpl = NULL;
3185 abts_wqeq->iocb_flag |= LPFC_IO_NVMET;
3187 ctxp->hdwq = &phba->sli4_hba.hdwq[abts_wqeq->hba_wqidx];
3189 rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3190 spin_unlock_irqrestore(&phba->hbalock, flags);
3191 if (rc == WQE_SUCCESS) {
3196 spin_lock_irqsave(&ctxp->ctxlock, flags);
3197 if (ctxp->flag & LPFC_NVMET_CTX_RLS) {
3198 spin_lock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
3199 list_del(&ctxp->list);
3200 spin_unlock(&phba->sli4_hba.abts_nvmet_buf_list_lock);
3203 ctxp->flag &= ~(LPFC_NVMET_ABORT_OP | LPFC_NVMET_CTX_RLS);
3204 spin_unlock_irqrestore(&ctxp->ctxlock, flags);
3206 atomic_inc(&tgtp->xmt_abort_rsp_error);
3207 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3208 "6135 Failed to Issue ABTS for oxid x%x. Status x%x\n",
3211 lpfc_nvmet_ctxbuf_post(phba, ctxp->ctxbuf);
3216 lpfc_nvmet_unsol_ls_issue_abort(struct lpfc_hba *phba,
3217 struct lpfc_nvmet_rcv_ctx *ctxp,
3218 uint32_t sid, uint16_t xri)
3220 struct lpfc_nvmet_tgtport *tgtp;
3221 struct lpfc_iocbq *abts_wqeq;
3222 unsigned long flags;
3225 if ((ctxp->state == LPFC_NVMET_STE_LS_RCV && ctxp->entry_cnt == 1) ||
3226 (ctxp->state == LPFC_NVMET_STE_LS_RSP && ctxp->entry_cnt == 2)) {
3227 ctxp->state = LPFC_NVMET_STE_LS_ABORT;
3230 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
3231 "6418 NVMET LS abort state mismatch "
3233 ctxp->oxid, ctxp->state, ctxp->entry_cnt);
3234 ctxp->state = LPFC_NVMET_STE_LS_ABORT;
3237 tgtp = (struct lpfc_nvmet_tgtport *)phba->targetport->private;
3239 /* Issue ABTS for this WQE based on iotag */
3240 ctxp->wqeq = lpfc_sli_get_iocbq(phba);
3242 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3243 "6068 Abort failed: No wqeqs: "
3245 /* No failure to an ABTS request. */
3250 abts_wqeq = ctxp->wqeq;
3252 if (lpfc_nvmet_unsol_issue_abort(phba, ctxp, sid, xri) == 0) {
3257 spin_lock_irqsave(&phba->hbalock, flags);
3258 abts_wqeq->wqe_cmpl = lpfc_nvmet_xmt_ls_abort_cmp;
3259 abts_wqeq->iocb_cmpl = 0;
3260 abts_wqeq->iocb_flag |= LPFC_IO_NVME_LS;
3261 rc = lpfc_sli4_issue_wqe(phba, ctxp->hdwq, abts_wqeq);
3262 spin_unlock_irqrestore(&phba->hbalock, flags);
3263 if (rc == WQE_SUCCESS) {
3264 atomic_inc(&tgtp->xmt_abort_unsol);
3268 atomic_inc(&tgtp->xmt_abort_rsp_error);
3269 abts_wqeq->context2 = NULL;
3270 abts_wqeq->context3 = NULL;
3271 lpfc_sli_release_iocbq(phba, abts_wqeq);
3273 lpfc_printf_log(phba, KERN_ERR, LOG_NVME_ABTS,
3274 "6056 Failed to Issue ABTS. Status x%x\n", rc);