1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2017-2023 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/export.h>
27 #include <linux/delay.h>
28 #include <asm/unaligned.h>
29 #include <linux/t10-pi.h>
30 #include <linux/crc-t10dif.h>
31 #include <linux/blk-cgroup.h>
32 #include <net/checksum.h>
34 #include <scsi/scsi.h>
35 #include <scsi/scsi_device.h>
36 #include <scsi/scsi_eh.h>
37 #include <scsi/scsi_host.h>
38 #include <scsi/scsi_tcq.h>
39 #include <scsi/scsi_transport_fc.h>
41 #include "lpfc_version.h"
45 #include "lpfc_sli4.h"
47 #include "lpfc_disc.h"
49 #include "lpfc_scsi.h"
50 #include "lpfc_logmsg.h"
51 #include "lpfc_crtn.h"
52 #include "lpfc_vport.h"
54 #define LPFC_RESET_WAIT 2
55 #define LPFC_ABORT_WAIT 2
57 static char *dif_op_str[] = {
67 struct scsi_dif_tuple {
68 __be16 guard_tag; /* Checksum */
69 __be16 app_tag; /* Opaque storage */
70 __be32 ref_tag; /* Target LBA or indirect LBA */
73 static struct lpfc_rport_data *
74 lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
76 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
78 if (vport->phba->cfg_fof)
79 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
81 return (struct lpfc_rport_data *)sdev->hostdata;
85 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
87 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
89 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
92 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
93 * @phba: Pointer to HBA object.
94 * @lpfc_cmd: lpfc scsi command object pointer.
96 * This function is called from the lpfc_prep_task_mgmt_cmd function to
97 * set the last bit in the response sge entry.
100 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
101 struct lpfc_io_buf *lpfc_cmd)
103 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
106 sgl->word2 = le32_to_cpu(sgl->word2);
107 bf_set(lpfc_sli4_sge_last, sgl, 1);
108 sgl->word2 = cpu_to_le32(sgl->word2);
112 #define LPFC_INVALID_REFTAG ((u32)-1)
115 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
116 * @phba: The Hba for which this call is being executed.
118 * This routine is called when there is resource error in driver or firmware.
119 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
120 * posts at most 1 event each second. This routine wakes up worker thread of
121 * @phba to process WORKER_RAM_DOWN_EVENT event.
123 * This routine should be called with no lock held.
126 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
130 unsigned long expires;
132 spin_lock_irqsave(&phba->hbalock, flags);
133 atomic_inc(&phba->num_rsrc_err);
134 phba->last_rsrc_error_time = jiffies;
136 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
137 if (time_after(expires, jiffies)) {
138 spin_unlock_irqrestore(&phba->hbalock, flags);
142 phba->last_ramp_down_time = jiffies;
144 spin_unlock_irqrestore(&phba->hbalock, flags);
146 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
147 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
149 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
150 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
153 lpfc_worker_wake_up(phba);
158 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
159 * @phba: The Hba for which this call is being executed.
161 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
162 * thread.This routine reduces queue depth for all scsi device on each vport
163 * associated with @phba.
166 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
168 struct lpfc_vport **vports;
169 struct Scsi_Host *shost;
170 struct scsi_device *sdev;
171 unsigned long new_queue_depth;
172 unsigned long num_rsrc_err, num_cmd_success;
175 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
176 num_cmd_success = atomic_read(&phba->num_cmd_success);
179 * The error and success command counters are global per
180 * driver instance. If another handler has already
181 * operated on this error event, just exit.
183 if (num_rsrc_err == 0)
186 vports = lpfc_create_vport_work_array(phba);
188 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
189 shost = lpfc_shost_from_vport(vports[i]);
190 shost_for_each_device(sdev, shost) {
192 sdev->queue_depth * num_rsrc_err /
193 (num_rsrc_err + num_cmd_success);
194 if (!new_queue_depth)
195 new_queue_depth = sdev->queue_depth - 1;
197 new_queue_depth = sdev->queue_depth -
199 scsi_change_queue_depth(sdev, new_queue_depth);
202 lpfc_destroy_vport_work_array(phba, vports);
203 atomic_set(&phba->num_rsrc_err, 0);
204 atomic_set(&phba->num_cmd_success, 0);
208 * lpfc_scsi_dev_block - set all scsi hosts to block state
209 * @phba: Pointer to HBA context object.
211 * This function walks vport list and set each SCSI host to block state
212 * by invoking fc_remote_port_delete() routine. This function is invoked
213 * with EEH when device's PCI slot has been permanently disabled.
216 lpfc_scsi_dev_block(struct lpfc_hba *phba)
218 struct lpfc_vport **vports;
219 struct Scsi_Host *shost;
220 struct scsi_device *sdev;
221 struct fc_rport *rport;
224 vports = lpfc_create_vport_work_array(phba);
226 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
227 shost = lpfc_shost_from_vport(vports[i]);
228 shost_for_each_device(sdev, shost) {
229 rport = starget_to_rport(scsi_target(sdev));
230 fc_remote_port_delete(rport);
233 lpfc_destroy_vport_work_array(phba, vports);
237 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
238 * @vport: The virtual port for which this call being executed.
239 * @num_to_alloc: The requested number of buffers to allocate.
241 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
242 * the scsi buffer contains all the necessary information needed to initiate
243 * a SCSI I/O. The non-DMAable buffer region contains information to build
244 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
245 * and the initial BPL. In addition to allocating memory, the FCP CMND and
246 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
249 * int - number of scsi buffers that were allocated.
250 * 0 = failure, less than num_to_alloc is a partial failure.
253 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
255 struct lpfc_hba *phba = vport->phba;
256 struct lpfc_io_buf *psb;
257 struct ulp_bde64 *bpl;
259 dma_addr_t pdma_phys_fcp_cmd;
260 dma_addr_t pdma_phys_fcp_rsp;
261 dma_addr_t pdma_phys_sgl;
265 bpl_size = phba->cfg_sg_dma_buf_size -
266 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
268 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
269 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
270 num_to_alloc, phba->cfg_sg_dma_buf_size,
271 (int)sizeof(struct fcp_cmnd),
272 (int)sizeof(struct fcp_rsp), bpl_size);
274 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
275 psb = kzalloc(sizeof(struct lpfc_io_buf), GFP_KERNEL);
280 * Get memory from the pci pool to map the virt space to pci
281 * bus space for an I/O. The DMA buffer includes space for the
282 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
283 * necessary to support the sg_tablesize.
285 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
286 GFP_KERNEL, &psb->dma_handle);
293 /* Allocate iotag for psb->cur_iocbq. */
294 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
296 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
297 psb->data, psb->dma_handle);
301 psb->cur_iocbq.cmd_flag |= LPFC_IO_FCP;
303 psb->fcp_cmnd = psb->data;
304 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
305 psb->dma_sgl = psb->data + sizeof(struct fcp_cmnd) +
306 sizeof(struct fcp_rsp);
308 /* Initialize local short-hand pointers. */
309 bpl = (struct ulp_bde64 *)psb->dma_sgl;
310 pdma_phys_fcp_cmd = psb->dma_handle;
311 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
312 pdma_phys_sgl = psb->dma_handle + sizeof(struct fcp_cmnd) +
313 sizeof(struct fcp_rsp);
316 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
317 * are sg list bdes. Initialize the first two and leave the
318 * rest for queuecommand.
320 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
321 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
322 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
323 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
324 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
326 /* Setup the physical region for the FCP RSP */
327 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
328 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
329 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
330 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
331 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
334 * Since the IOCB for the FCP I/O is built into this
335 * lpfc_scsi_buf, initialize it with all known data now.
337 iocb = &psb->cur_iocbq.iocb;
338 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
339 if ((phba->sli_rev == 3) &&
340 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
341 /* fill in immediate fcp command BDE */
342 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
343 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
344 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
346 iocb->un.fcpi64.bdl.addrHigh = 0;
347 iocb->ulpBdeCount = 0;
349 /* fill in response BDE */
350 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
352 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
353 sizeof(struct fcp_rsp);
354 iocb->unsli3.fcp_ext.rbde.addrLow =
355 putPaddrLow(pdma_phys_fcp_rsp);
356 iocb->unsli3.fcp_ext.rbde.addrHigh =
357 putPaddrHigh(pdma_phys_fcp_rsp);
359 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
360 iocb->un.fcpi64.bdl.bdeSize =
361 (2 * sizeof(struct ulp_bde64));
362 iocb->un.fcpi64.bdl.addrLow =
363 putPaddrLow(pdma_phys_sgl);
364 iocb->un.fcpi64.bdl.addrHigh =
365 putPaddrHigh(pdma_phys_sgl);
366 iocb->ulpBdeCount = 1;
369 iocb->ulpClass = CLASS3;
370 psb->status = IOSTAT_SUCCESS;
371 /* Put it back into the SCSI buffer list */
372 psb->cur_iocbq.io_buf = psb;
373 spin_lock_init(&psb->buf_lock);
374 lpfc_release_scsi_buf_s3(phba, psb);
382 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
383 * @vport: pointer to lpfc vport data structure.
385 * This routine is invoked by the vport cleanup for deletions and the cleanup
386 * for an ndlp on removal.
389 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
391 struct lpfc_hba *phba = vport->phba;
392 struct lpfc_io_buf *psb, *next_psb;
393 struct lpfc_sli4_hdw_queue *qp;
394 unsigned long iflag = 0;
397 if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
400 spin_lock_irqsave(&phba->hbalock, iflag);
401 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
402 qp = &phba->sli4_hba.hdwq[idx];
404 spin_lock(&qp->abts_io_buf_list_lock);
405 list_for_each_entry_safe(psb, next_psb,
406 &qp->lpfc_abts_io_buf_list, list) {
407 if (psb->cur_iocbq.cmd_flag & LPFC_IO_NVME)
410 if (psb->rdata && psb->rdata->pnode &&
411 psb->rdata->pnode->vport == vport)
414 spin_unlock(&qp->abts_io_buf_list_lock);
416 spin_unlock_irqrestore(&phba->hbalock, iflag);
420 * lpfc_sli4_io_xri_aborted - Fast-path process of fcp xri abort
421 * @phba: pointer to lpfc hba data structure.
422 * @axri: pointer to the fcp xri abort wcqe structure.
423 * @idx: index into hdwq
425 * This routine is invoked by the worker thread to process a SLI4 fast-path
426 * FCP or NVME aborted xri.
429 lpfc_sli4_io_xri_aborted(struct lpfc_hba *phba,
430 struct sli4_wcqe_xri_aborted *axri, int idx)
434 struct lpfc_io_buf *psb, *next_psb;
435 struct lpfc_sli4_hdw_queue *qp;
436 unsigned long iflag = 0;
437 struct lpfc_iocbq *iocbq;
439 struct lpfc_nodelist *ndlp;
441 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
442 struct scsi_cmnd *cmd;
445 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
447 offline = pci_channel_offline(phba->pcidev);
449 xri = bf_get(lpfc_wcqe_xa_xri, axri);
450 rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
452 qp = &phba->sli4_hba.hdwq[idx];
453 spin_lock_irqsave(&phba->hbalock, iflag);
454 spin_lock(&qp->abts_io_buf_list_lock);
455 list_for_each_entry_safe(psb, next_psb,
456 &qp->lpfc_abts_io_buf_list, list) {
458 xri = psb->cur_iocbq.sli4_xritag;
459 if (psb->cur_iocbq.sli4_xritag == xri) {
460 list_del_init(&psb->list);
461 psb->flags &= ~LPFC_SBUF_XBUSY;
462 psb->status = IOSTAT_SUCCESS;
463 if (psb->cur_iocbq.cmd_flag & LPFC_IO_NVME) {
464 qp->abts_nvme_io_bufs--;
465 spin_unlock(&qp->abts_io_buf_list_lock);
466 spin_unlock_irqrestore(&phba->hbalock, iflag);
468 lpfc_sli4_nvme_xri_aborted(phba, axri,
472 lpfc_sli4_nvme_pci_offline_aborted(phba, psb);
473 spin_lock_irqsave(&phba->hbalock, iflag);
474 spin_lock(&qp->abts_io_buf_list_lock);
477 qp->abts_scsi_io_bufs--;
478 spin_unlock(&qp->abts_io_buf_list_lock);
480 if (psb->rdata && psb->rdata->pnode)
481 ndlp = psb->rdata->pnode;
485 rrq_empty = list_empty(&phba->active_rrq_list);
486 spin_unlock_irqrestore(&phba->hbalock, iflag);
487 if (ndlp && !offline) {
488 lpfc_set_rrq_active(phba, ndlp,
489 psb->cur_iocbq.sli4_lxritag, rxid, 1);
490 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
493 if (phba->cfg_fcp_wait_abts_rsp || offline) {
494 spin_lock_irqsave(&psb->buf_lock, iflag);
497 spin_unlock_irqrestore(&psb->buf_lock, iflag);
499 /* The sdev is not guaranteed to be valid post
506 * We expect there is an abort thread waiting
507 * for command completion wake up the thread.
509 spin_lock_irqsave(&psb->buf_lock, iflag);
510 psb->cur_iocbq.cmd_flag &=
511 ~LPFC_DRIVER_ABORTED;
514 spin_unlock_irqrestore(&psb->buf_lock, iflag);
517 lpfc_release_scsi_buf_s4(phba, psb);
519 lpfc_worker_wake_up(phba);
522 spin_lock_irqsave(&phba->hbalock, iflag);
523 spin_lock(&qp->abts_io_buf_list_lock);
527 spin_unlock(&qp->abts_io_buf_list_lock);
529 for (i = 1; i <= phba->sli.last_iotag; i++) {
530 iocbq = phba->sli.iocbq_lookup[i];
532 if (!(iocbq->cmd_flag & LPFC_IO_FCP) ||
533 (iocbq->cmd_flag & LPFC_IO_LIBDFC))
535 if (iocbq->sli4_xritag != xri)
537 psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
538 psb->flags &= ~LPFC_SBUF_XBUSY;
539 spin_unlock_irqrestore(&phba->hbalock, iflag);
540 if (!list_empty(&pring->txq))
541 lpfc_worker_wake_up(phba);
545 spin_unlock_irqrestore(&phba->hbalock, iflag);
549 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
550 * @phba: The HBA for which this call is being executed.
551 * @ndlp: pointer to a node-list data structure.
552 * @cmnd: Pointer to scsi_cmnd data structure.
554 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
555 * and returns to caller.
559 * Pointer to lpfc_scsi_buf - Success
561 static struct lpfc_io_buf *
562 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
563 struct scsi_cmnd *cmnd)
565 struct lpfc_io_buf *lpfc_cmd = NULL;
566 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
567 unsigned long iflag = 0;
569 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
570 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
573 spin_lock(&phba->scsi_buf_list_put_lock);
574 list_splice(&phba->lpfc_scsi_buf_list_put,
575 &phba->lpfc_scsi_buf_list_get);
576 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
577 list_remove_head(scsi_buf_list_get, lpfc_cmd,
578 struct lpfc_io_buf, list);
579 spin_unlock(&phba->scsi_buf_list_put_lock);
581 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
583 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
584 atomic_inc(&ndlp->cmd_pending);
585 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
590 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
591 * @phba: The HBA for which this call is being executed.
592 * @ndlp: pointer to a node-list data structure.
593 * @cmnd: Pointer to scsi_cmnd data structure.
595 * This routine removes a scsi buffer from head of @hdwq io_buf_list
596 * and returns to caller.
600 * Pointer to lpfc_scsi_buf - Success
602 static struct lpfc_io_buf *
603 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
604 struct scsi_cmnd *cmnd)
606 struct lpfc_io_buf *lpfc_cmd;
607 struct lpfc_sli4_hdw_queue *qp;
608 struct sli4_sge *sgl;
609 dma_addr_t pdma_phys_fcp_rsp;
610 dma_addr_t pdma_phys_fcp_cmd;
613 struct fcp_cmd_rsp_buf *tmp = NULL;
615 cpu = raw_smp_processor_id();
616 if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
617 tag = blk_mq_unique_tag(scsi_cmd_to_rq(cmnd));
618 idx = blk_mq_unique_tag_to_hwq(tag);
620 idx = phba->sli4_hba.cpu_map[cpu].hdwq;
623 lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
624 !phba->cfg_xri_rebalancing);
626 qp = &phba->sli4_hba.hdwq[idx];
631 /* Setup key fields in buffer that may have been changed
632 * if other protocols used this buffer.
634 lpfc_cmd->cur_iocbq.cmd_flag = LPFC_IO_FCP;
635 lpfc_cmd->prot_seg_cnt = 0;
636 lpfc_cmd->seg_cnt = 0;
637 lpfc_cmd->timeout = 0;
639 lpfc_cmd->start_time = jiffies;
640 lpfc_cmd->waitq = NULL;
642 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
643 lpfc_cmd->prot_data_type = 0;
645 tmp = lpfc_get_cmd_rsp_buf_per_hdwq(phba, lpfc_cmd);
647 lpfc_release_io_buf(phba, lpfc_cmd, lpfc_cmd->hdwq);
651 lpfc_cmd->fcp_cmnd = tmp->fcp_cmnd;
652 lpfc_cmd->fcp_rsp = tmp->fcp_rsp;
655 * The first two SGEs are the FCP_CMD and FCP_RSP.
656 * The balance are sg list bdes. Initialize the
657 * first two and leave the rest for queuecommand.
659 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
660 pdma_phys_fcp_cmd = tmp->fcp_cmd_rsp_dma_handle;
661 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
662 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
663 sgl->word2 = le32_to_cpu(sgl->word2);
664 bf_set(lpfc_sli4_sge_last, sgl, 0);
665 sgl->word2 = cpu_to_le32(sgl->word2);
666 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
669 /* Setup the physical region for the FCP RSP */
670 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
671 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
672 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
673 sgl->word2 = le32_to_cpu(sgl->word2);
674 bf_set(lpfc_sli4_sge_last, sgl, 1);
675 sgl->word2 = cpu_to_le32(sgl->word2);
676 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
678 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
679 atomic_inc(&ndlp->cmd_pending);
680 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
685 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
686 * @phba: The HBA for which this call is being executed.
687 * @ndlp: pointer to a node-list data structure.
688 * @cmnd: Pointer to scsi_cmnd data structure.
690 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
691 * and returns to caller.
695 * Pointer to lpfc_scsi_buf - Success
697 static struct lpfc_io_buf*
698 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
699 struct scsi_cmnd *cmnd)
701 return phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
705 * lpfc_release_scsi_buf_s3 - Return a scsi buffer back to hba scsi buf list
706 * @phba: The Hba for which this call is being executed.
707 * @psb: The scsi buffer which is being released.
709 * This routine releases @psb scsi buffer by adding it to tail of @phba
710 * lpfc_scsi_buf_list list.
713 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
715 unsigned long iflag = 0;
718 psb->prot_seg_cnt = 0;
720 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
722 psb->cur_iocbq.cmd_flag = LPFC_IO_FCP;
723 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
724 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
728 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
729 * @phba: The Hba for which this call is being executed.
730 * @psb: The scsi buffer which is being released.
732 * This routine releases @psb scsi buffer by adding it to tail of @hdwq
733 * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
734 * and cannot be reused for at least RA_TOV amount of time if it was
738 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
740 struct lpfc_sli4_hdw_queue *qp;
741 unsigned long iflag = 0;
744 psb->prot_seg_cnt = 0;
747 if (psb->flags & LPFC_SBUF_XBUSY) {
748 spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
749 if (!phba->cfg_fcp_wait_abts_rsp)
751 list_add_tail(&psb->list, &qp->lpfc_abts_io_buf_list);
752 qp->abts_scsi_io_bufs++;
753 spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
755 lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
760 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
761 * @phba: The Hba for which this call is being executed.
762 * @psb: The scsi buffer which is being released.
764 * This routine releases @psb scsi buffer by adding it to tail of @phba
765 * lpfc_scsi_buf_list list.
768 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
770 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
771 atomic_dec(&psb->ndlp->cmd_pending);
773 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
774 phba->lpfc_release_scsi_buf(phba, psb);
778 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
779 * @data: A pointer to the immediate command data portion of the IOCB.
780 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
782 * The routine copies the entire FCP command from @fcp_cmnd to @data while
783 * byte swapping the data to big endian format for transmission on the wire.
786 lpfc_fcpcmd_to_iocb(u8 *data, struct fcp_cmnd *fcp_cmnd)
790 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
791 i += sizeof(uint32_t), j++) {
792 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
797 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
798 * @phba: The Hba for which this call is being executed.
799 * @lpfc_cmd: The scsi buffer which is going to be mapped.
801 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
802 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
803 * through sg elements and format the bde. This routine also initializes all
804 * IOCB fields which are dependent on scsi command request buffer.
811 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
813 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
814 struct scatterlist *sgel = NULL;
815 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
816 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
817 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
818 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
819 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
821 uint32_t num_bde = 0;
822 int nseg, datadir = scsi_cmnd->sc_data_direction;
825 * There are three possibilities here - use scatter-gather segment, use
826 * the single mapping, or neither. Start the lpfc command prep by
827 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
831 if (scsi_sg_count(scsi_cmnd)) {
833 * The driver stores the segment count returned from dma_map_sg
834 * because this a count of dma-mappings used to map the use_sg
835 * pages. They are not guaranteed to be the same for those
836 * architectures that implement an IOMMU.
839 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
840 scsi_sg_count(scsi_cmnd), datadir);
844 lpfc_cmd->seg_cnt = nseg;
845 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
846 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
847 "9064 BLKGRD: %s: Too many sg segments"
848 " from dma_map_sg. Config %d, seg_cnt"
849 " %d\n", __func__, phba->cfg_sg_seg_cnt,
851 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
852 lpfc_cmd->seg_cnt = 0;
853 scsi_dma_unmap(scsi_cmnd);
858 * The driver established a maximum scatter-gather segment count
859 * during probe that limits the number of sg elements in any
860 * single scsi command. Just run through the seg_cnt and format
862 * When using SLI-3 the driver will try to fit all the BDEs into
863 * the IOCB. If it can't then the BDEs get added to a BPL as it
864 * does for SLI-2 mode.
866 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
867 physaddr = sg_dma_address(sgel);
868 if (phba->sli_rev == 3 &&
869 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
870 !(iocbq->cmd_flag & DSS_SECURITY_OP) &&
871 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
872 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
873 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
874 data_bde->addrLow = putPaddrLow(physaddr);
875 data_bde->addrHigh = putPaddrHigh(physaddr);
878 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
879 bpl->tus.f.bdeSize = sg_dma_len(sgel);
880 bpl->tus.w = le32_to_cpu(bpl->tus.w);
882 le32_to_cpu(putPaddrLow(physaddr));
884 le32_to_cpu(putPaddrHigh(physaddr));
891 * Finish initializing those IOCB fields that are dependent on the
892 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
893 * explicitly reinitialized and for SLI-3 the extended bde count is
894 * explicitly reinitialized since all iocb memory resources are reused.
896 if (phba->sli_rev == 3 &&
897 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
898 !(iocbq->cmd_flag & DSS_SECURITY_OP)) {
899 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
901 * The extended IOCB format can only fit 3 BDE or a BPL.
902 * This I/O has more than 3 BDE so the 1st data bde will
903 * be a BPL that is filled in here.
905 physaddr = lpfc_cmd->dma_handle;
906 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
907 data_bde->tus.f.bdeSize = (num_bde *
908 sizeof(struct ulp_bde64));
909 physaddr += (sizeof(struct fcp_cmnd) +
910 sizeof(struct fcp_rsp) +
911 (2 * sizeof(struct ulp_bde64)));
912 data_bde->addrHigh = putPaddrHigh(physaddr);
913 data_bde->addrLow = putPaddrLow(physaddr);
914 /* ebde count includes the response bde and data bpl */
915 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
917 /* ebde count includes the response bde and data bdes */
918 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
921 iocb_cmd->un.fcpi64.bdl.bdeSize =
922 ((num_bde + 2) * sizeof(struct ulp_bde64));
923 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
925 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
928 * Due to difference in data length between DIF/non-DIF paths,
929 * we need to set word 4 of IOCB here
931 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
932 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
936 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
938 /* Return BG_ERR_INIT if error injection is detected by Initiator */
939 #define BG_ERR_INIT 0x1
940 /* Return BG_ERR_TGT if error injection is detected by Target */
941 #define BG_ERR_TGT 0x2
942 /* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
943 #define BG_ERR_SWAP 0x10
945 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
948 #define BG_ERR_CHECK 0x20
951 * lpfc_bg_err_inject - Determine if we should inject an error
952 * @phba: The Hba for which this call is being executed.
953 * @sc: The SCSI command to examine
954 * @reftag: (out) BlockGuard reference tag for transmitted data
955 * @apptag: (out) BlockGuard application tag for transmitted data
956 * @new_guard: (in) Value to replace CRC with if needed
958 * Returns BG_ERR_* bit mask or 0 if request ignored
961 lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
962 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
964 struct scatterlist *sgpe; /* s/g prot entry */
965 struct lpfc_io_buf *lpfc_cmd = NULL;
966 struct scsi_dif_tuple *src = NULL;
967 struct lpfc_nodelist *ndlp;
968 struct lpfc_rport_data *rdata;
969 uint32_t op = scsi_get_prot_op(sc);
976 if (op == SCSI_PROT_NORMAL)
979 sgpe = scsi_prot_sglist(sc);
980 lba = scsi_prot_ref_tag(sc);
981 if (lba == LPFC_INVALID_REFTAG)
984 /* First check if we need to match the LBA */
985 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
986 blksize = scsi_prot_interval(sc);
987 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
989 /* Make sure we have the right LBA if one is specified */
990 if (phba->lpfc_injerr_lba < (u64)lba ||
991 (phba->lpfc_injerr_lba >= (u64)(lba + numblks)))
994 blockoff = phba->lpfc_injerr_lba - (u64)lba;
995 numblks = sg_dma_len(sgpe) /
996 sizeof(struct scsi_dif_tuple);
997 if (numblks < blockoff)
1002 /* Next check if we need to match the remote NPortID or WWPN */
1003 rdata = lpfc_rport_data_from_scsi_device(sc->device);
1004 if (rdata && rdata->pnode) {
1005 ndlp = rdata->pnode;
1007 /* Make sure we have the right NPortID if one is specified */
1008 if (phba->lpfc_injerr_nportid &&
1009 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1013 * Make sure we have the right WWPN if one is specified.
1014 * wwn[0] should be a non-zero NAA in a good WWPN.
1016 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1017 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1018 sizeof(struct lpfc_name)) != 0))
1022 /* Setup a ptr to the protection data if the SCSI host provides it */
1024 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1026 lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
1029 /* Should we change the Reference Tag */
1031 if (phba->lpfc_injerr_wref_cnt) {
1033 case SCSI_PROT_WRITE_PASS:
1036 * For WRITE_PASS, force the error
1037 * to be sent on the wire. It should
1038 * be detected by the Target.
1039 * If blockoff != 0 error will be
1040 * inserted in middle of the IO.
1043 lpfc_printf_log(phba, KERN_ERR,
1045 "9076 BLKGRD: Injecting reftag error: "
1046 "write lba x%lx + x%x oldrefTag x%x\n",
1047 (unsigned long)lba, blockoff,
1048 be32_to_cpu(src->ref_tag));
1051 * Save the old ref_tag so we can
1052 * restore it on completion.
1055 lpfc_cmd->prot_data_type =
1057 lpfc_cmd->prot_data_segment =
1059 lpfc_cmd->prot_data =
1062 src->ref_tag = cpu_to_be32(0xDEADBEEF);
1063 phba->lpfc_injerr_wref_cnt--;
1064 if (phba->lpfc_injerr_wref_cnt == 0) {
1065 phba->lpfc_injerr_nportid = 0;
1066 phba->lpfc_injerr_lba =
1067 LPFC_INJERR_LBA_OFF;
1068 memset(&phba->lpfc_injerr_wwpn,
1069 0, sizeof(struct lpfc_name));
1071 rc = BG_ERR_TGT | BG_ERR_CHECK;
1076 case SCSI_PROT_WRITE_INSERT:
1078 * For WRITE_INSERT, force the error
1079 * to be sent on the wire. It should be
1080 * detected by the Target.
1082 /* DEADBEEF will be the reftag on the wire */
1083 *reftag = 0xDEADBEEF;
1084 phba->lpfc_injerr_wref_cnt--;
1085 if (phba->lpfc_injerr_wref_cnt == 0) {
1086 phba->lpfc_injerr_nportid = 0;
1087 phba->lpfc_injerr_lba =
1088 LPFC_INJERR_LBA_OFF;
1089 memset(&phba->lpfc_injerr_wwpn,
1090 0, sizeof(struct lpfc_name));
1092 rc = BG_ERR_TGT | BG_ERR_CHECK;
1094 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1095 "9078 BLKGRD: Injecting reftag error: "
1096 "write lba x%lx\n", (unsigned long)lba);
1098 case SCSI_PROT_WRITE_STRIP:
1100 * For WRITE_STRIP and WRITE_PASS,
1101 * force the error on data
1102 * being copied from SLI-Host to SLI-Port.
1104 *reftag = 0xDEADBEEF;
1105 phba->lpfc_injerr_wref_cnt--;
1106 if (phba->lpfc_injerr_wref_cnt == 0) {
1107 phba->lpfc_injerr_nportid = 0;
1108 phba->lpfc_injerr_lba =
1109 LPFC_INJERR_LBA_OFF;
1110 memset(&phba->lpfc_injerr_wwpn,
1111 0, sizeof(struct lpfc_name));
1115 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1116 "9077 BLKGRD: Injecting reftag error: "
1117 "write lba x%lx\n", (unsigned long)lba);
1121 if (phba->lpfc_injerr_rref_cnt) {
1123 case SCSI_PROT_READ_INSERT:
1124 case SCSI_PROT_READ_STRIP:
1125 case SCSI_PROT_READ_PASS:
1127 * For READ_STRIP and READ_PASS, force the
1128 * error on data being read off the wire. It
1129 * should force an IO error to the driver.
1131 *reftag = 0xDEADBEEF;
1132 phba->lpfc_injerr_rref_cnt--;
1133 if (phba->lpfc_injerr_rref_cnt == 0) {
1134 phba->lpfc_injerr_nportid = 0;
1135 phba->lpfc_injerr_lba =
1136 LPFC_INJERR_LBA_OFF;
1137 memset(&phba->lpfc_injerr_wwpn,
1138 0, sizeof(struct lpfc_name));
1142 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1143 "9079 BLKGRD: Injecting reftag error: "
1144 "read lba x%lx\n", (unsigned long)lba);
1150 /* Should we change the Application Tag */
1152 if (phba->lpfc_injerr_wapp_cnt) {
1154 case SCSI_PROT_WRITE_PASS:
1157 * For WRITE_PASS, force the error
1158 * to be sent on the wire. It should
1159 * be detected by the Target.
1160 * If blockoff != 0 error will be
1161 * inserted in middle of the IO.
1164 lpfc_printf_log(phba, KERN_ERR,
1166 "9080 BLKGRD: Injecting apptag error: "
1167 "write lba x%lx + x%x oldappTag x%x\n",
1168 (unsigned long)lba, blockoff,
1169 be16_to_cpu(src->app_tag));
1172 * Save the old app_tag so we can
1173 * restore it on completion.
1176 lpfc_cmd->prot_data_type =
1178 lpfc_cmd->prot_data_segment =
1180 lpfc_cmd->prot_data =
1183 src->app_tag = cpu_to_be16(0xDEAD);
1184 phba->lpfc_injerr_wapp_cnt--;
1185 if (phba->lpfc_injerr_wapp_cnt == 0) {
1186 phba->lpfc_injerr_nportid = 0;
1187 phba->lpfc_injerr_lba =
1188 LPFC_INJERR_LBA_OFF;
1189 memset(&phba->lpfc_injerr_wwpn,
1190 0, sizeof(struct lpfc_name));
1192 rc = BG_ERR_TGT | BG_ERR_CHECK;
1196 case SCSI_PROT_WRITE_INSERT:
1198 * For WRITE_INSERT, force the
1199 * error to be sent on the wire. It should be
1200 * detected by the Target.
1202 /* DEAD will be the apptag on the wire */
1204 phba->lpfc_injerr_wapp_cnt--;
1205 if (phba->lpfc_injerr_wapp_cnt == 0) {
1206 phba->lpfc_injerr_nportid = 0;
1207 phba->lpfc_injerr_lba =
1208 LPFC_INJERR_LBA_OFF;
1209 memset(&phba->lpfc_injerr_wwpn,
1210 0, sizeof(struct lpfc_name));
1212 rc = BG_ERR_TGT | BG_ERR_CHECK;
1214 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1215 "0813 BLKGRD: Injecting apptag error: "
1216 "write lba x%lx\n", (unsigned long)lba);
1218 case SCSI_PROT_WRITE_STRIP:
1220 * For WRITE_STRIP and WRITE_PASS,
1221 * force the error on data
1222 * being copied from SLI-Host to SLI-Port.
1225 phba->lpfc_injerr_wapp_cnt--;
1226 if (phba->lpfc_injerr_wapp_cnt == 0) {
1227 phba->lpfc_injerr_nportid = 0;
1228 phba->lpfc_injerr_lba =
1229 LPFC_INJERR_LBA_OFF;
1230 memset(&phba->lpfc_injerr_wwpn,
1231 0, sizeof(struct lpfc_name));
1235 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1236 "0812 BLKGRD: Injecting apptag error: "
1237 "write lba x%lx\n", (unsigned long)lba);
1241 if (phba->lpfc_injerr_rapp_cnt) {
1243 case SCSI_PROT_READ_INSERT:
1244 case SCSI_PROT_READ_STRIP:
1245 case SCSI_PROT_READ_PASS:
1247 * For READ_STRIP and READ_PASS, force the
1248 * error on data being read off the wire. It
1249 * should force an IO error to the driver.
1252 phba->lpfc_injerr_rapp_cnt--;
1253 if (phba->lpfc_injerr_rapp_cnt == 0) {
1254 phba->lpfc_injerr_nportid = 0;
1255 phba->lpfc_injerr_lba =
1256 LPFC_INJERR_LBA_OFF;
1257 memset(&phba->lpfc_injerr_wwpn,
1258 0, sizeof(struct lpfc_name));
1262 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1263 "0814 BLKGRD: Injecting apptag error: "
1264 "read lba x%lx\n", (unsigned long)lba);
1271 /* Should we change the Guard Tag */
1273 if (phba->lpfc_injerr_wgrd_cnt) {
1275 case SCSI_PROT_WRITE_PASS:
1279 case SCSI_PROT_WRITE_INSERT:
1281 * For WRITE_INSERT, force the
1282 * error to be sent on the wire. It should be
1283 * detected by the Target.
1285 phba->lpfc_injerr_wgrd_cnt--;
1286 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1287 phba->lpfc_injerr_nportid = 0;
1288 phba->lpfc_injerr_lba =
1289 LPFC_INJERR_LBA_OFF;
1290 memset(&phba->lpfc_injerr_wwpn,
1291 0, sizeof(struct lpfc_name));
1294 rc |= BG_ERR_TGT | BG_ERR_SWAP;
1295 /* Signals the caller to swap CRC->CSUM */
1297 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1298 "0817 BLKGRD: Injecting guard error: "
1299 "write lba x%lx\n", (unsigned long)lba);
1301 case SCSI_PROT_WRITE_STRIP:
1303 * For WRITE_STRIP and WRITE_PASS,
1304 * force the error on data
1305 * being copied from SLI-Host to SLI-Port.
1307 phba->lpfc_injerr_wgrd_cnt--;
1308 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1309 phba->lpfc_injerr_nportid = 0;
1310 phba->lpfc_injerr_lba =
1311 LPFC_INJERR_LBA_OFF;
1312 memset(&phba->lpfc_injerr_wwpn,
1313 0, sizeof(struct lpfc_name));
1316 rc = BG_ERR_INIT | BG_ERR_SWAP;
1317 /* Signals the caller to swap CRC->CSUM */
1319 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1320 "0816 BLKGRD: Injecting guard error: "
1321 "write lba x%lx\n", (unsigned long)lba);
1325 if (phba->lpfc_injerr_rgrd_cnt) {
1327 case SCSI_PROT_READ_INSERT:
1328 case SCSI_PROT_READ_STRIP:
1329 case SCSI_PROT_READ_PASS:
1331 * For READ_STRIP and READ_PASS, force the
1332 * error on data being read off the wire. It
1333 * should force an IO error to the driver.
1335 phba->lpfc_injerr_rgrd_cnt--;
1336 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1337 phba->lpfc_injerr_nportid = 0;
1338 phba->lpfc_injerr_lba =
1339 LPFC_INJERR_LBA_OFF;
1340 memset(&phba->lpfc_injerr_wwpn,
1341 0, sizeof(struct lpfc_name));
1344 rc = BG_ERR_INIT | BG_ERR_SWAP;
1345 /* Signals the caller to swap CRC->CSUM */
1347 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1348 "0818 BLKGRD: Injecting guard error: "
1349 "read lba x%lx\n", (unsigned long)lba);
1359 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1360 * the specified SCSI command.
1361 * @phba: The Hba for which this call is being executed.
1362 * @sc: The SCSI command to examine
1363 * @txop: (out) BlockGuard operation for transmitted data
1364 * @rxop: (out) BlockGuard operation for received data
1366 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1370 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1371 uint8_t *txop, uint8_t *rxop)
1375 if (sc->prot_flags & SCSI_PROT_IP_CHECKSUM) {
1376 switch (scsi_get_prot_op(sc)) {
1377 case SCSI_PROT_READ_INSERT:
1378 case SCSI_PROT_WRITE_STRIP:
1379 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1380 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1383 case SCSI_PROT_READ_STRIP:
1384 case SCSI_PROT_WRITE_INSERT:
1385 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1386 *txop = BG_OP_IN_NODIF_OUT_CRC;
1389 case SCSI_PROT_READ_PASS:
1390 case SCSI_PROT_WRITE_PASS:
1391 *rxop = BG_OP_IN_CRC_OUT_CSUM;
1392 *txop = BG_OP_IN_CSUM_OUT_CRC;
1395 case SCSI_PROT_NORMAL:
1397 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1398 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1399 scsi_get_prot_op(sc));
1405 switch (scsi_get_prot_op(sc)) {
1406 case SCSI_PROT_READ_STRIP:
1407 case SCSI_PROT_WRITE_INSERT:
1408 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1409 *txop = BG_OP_IN_NODIF_OUT_CRC;
1412 case SCSI_PROT_READ_PASS:
1413 case SCSI_PROT_WRITE_PASS:
1414 *rxop = BG_OP_IN_CRC_OUT_CRC;
1415 *txop = BG_OP_IN_CRC_OUT_CRC;
1418 case SCSI_PROT_READ_INSERT:
1419 case SCSI_PROT_WRITE_STRIP:
1420 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1421 *txop = BG_OP_IN_CRC_OUT_NODIF;
1424 case SCSI_PROT_NORMAL:
1426 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1427 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1428 scsi_get_prot_op(sc));
1437 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1439 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1440 * the specified SCSI command in order to force a guard tag error.
1441 * @phba: The Hba for which this call is being executed.
1442 * @sc: The SCSI command to examine
1443 * @txop: (out) BlockGuard operation for transmitted data
1444 * @rxop: (out) BlockGuard operation for received data
1446 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1450 lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1451 uint8_t *txop, uint8_t *rxop)
1454 if (sc->prot_flags & SCSI_PROT_IP_CHECKSUM) {
1455 switch (scsi_get_prot_op(sc)) {
1456 case SCSI_PROT_READ_INSERT:
1457 case SCSI_PROT_WRITE_STRIP:
1458 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1459 *txop = BG_OP_IN_CRC_OUT_NODIF;
1462 case SCSI_PROT_READ_STRIP:
1463 case SCSI_PROT_WRITE_INSERT:
1464 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1465 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1468 case SCSI_PROT_READ_PASS:
1469 case SCSI_PROT_WRITE_PASS:
1470 *rxop = BG_OP_IN_CSUM_OUT_CRC;
1471 *txop = BG_OP_IN_CRC_OUT_CSUM;
1474 case SCSI_PROT_NORMAL:
1480 switch (scsi_get_prot_op(sc)) {
1481 case SCSI_PROT_READ_STRIP:
1482 case SCSI_PROT_WRITE_INSERT:
1483 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1484 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1487 case SCSI_PROT_READ_PASS:
1488 case SCSI_PROT_WRITE_PASS:
1489 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
1490 *txop = BG_OP_IN_CSUM_OUT_CSUM;
1493 case SCSI_PROT_READ_INSERT:
1494 case SCSI_PROT_WRITE_STRIP:
1495 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1496 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1499 case SCSI_PROT_NORMAL:
1510 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1511 * @phba: The Hba for which this call is being executed.
1512 * @sc: pointer to scsi command we're working on
1513 * @bpl: pointer to buffer list for protection groups
1514 * @datasegcnt: number of segments of data that have been dma mapped
1516 * This function sets up BPL buffer list for protection groups of
1517 * type LPFC_PG_TYPE_NO_DIF
1519 * This is usually used when the HBA is instructed to generate
1520 * DIFs and insert them into data stream (or strip DIF from
1521 * incoming data stream)
1523 * The buffer list consists of just one protection group described
1525 * +-------------------------+
1526 * start of prot group --> | PDE_5 |
1527 * +-------------------------+
1529 * +-------------------------+
1531 * +-------------------------+
1532 * |more Data BDE's ... (opt)|
1533 * +-------------------------+
1536 * Note: Data s/g buffers have been dma mapped
1538 * Returns the number of BDEs added to the BPL.
1541 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1542 struct ulp_bde64 *bpl, int datasegcnt)
1544 struct scatterlist *sgde = NULL; /* s/g data entry */
1545 struct lpfc_pde5 *pde5 = NULL;
1546 struct lpfc_pde6 *pde6 = NULL;
1547 dma_addr_t physaddr;
1548 int i = 0, num_bde = 0, status;
1549 int datadir = sc->sc_data_direction;
1550 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1553 uint32_t checking = 1;
1557 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1561 /* extract some info from the scsi command for pde*/
1562 reftag = scsi_prot_ref_tag(sc);
1563 if (reftag == LPFC_INVALID_REFTAG)
1566 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1567 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1569 if (rc & BG_ERR_SWAP)
1570 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1571 if (rc & BG_ERR_CHECK)
1576 /* setup PDE5 with what we have */
1577 pde5 = (struct lpfc_pde5 *) bpl;
1578 memset(pde5, 0, sizeof(struct lpfc_pde5));
1579 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1581 /* Endianness conversion if necessary for PDE5 */
1582 pde5->word0 = cpu_to_le32(pde5->word0);
1583 pde5->reftag = cpu_to_le32(reftag);
1585 /* advance bpl and increment bde count */
1588 pde6 = (struct lpfc_pde6 *) bpl;
1590 /* setup PDE6 with the rest of the info */
1591 memset(pde6, 0, sizeof(struct lpfc_pde6));
1592 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1593 bf_set(pde6_optx, pde6, txop);
1594 bf_set(pde6_oprx, pde6, rxop);
1597 * We only need to check the data on READs, for WRITEs
1598 * protection data is automatically generated, not checked.
1600 if (datadir == DMA_FROM_DEVICE) {
1601 if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
1602 bf_set(pde6_ce, pde6, checking);
1604 bf_set(pde6_ce, pde6, 0);
1606 if (sc->prot_flags & SCSI_PROT_REF_CHECK)
1607 bf_set(pde6_re, pde6, checking);
1609 bf_set(pde6_re, pde6, 0);
1611 bf_set(pde6_ai, pde6, 1);
1612 bf_set(pde6_ae, pde6, 0);
1613 bf_set(pde6_apptagval, pde6, 0);
1615 /* Endianness conversion if necessary for PDE6 */
1616 pde6->word0 = cpu_to_le32(pde6->word0);
1617 pde6->word1 = cpu_to_le32(pde6->word1);
1618 pde6->word2 = cpu_to_le32(pde6->word2);
1620 /* advance bpl and increment bde count */
1624 /* assumption: caller has already run dma_map_sg on command data */
1625 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1626 physaddr = sg_dma_address(sgde);
1627 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1628 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1629 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1630 if (datadir == DMA_TO_DEVICE)
1631 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1633 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1634 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1644 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1645 * @phba: The Hba for which this call is being executed.
1646 * @sc: pointer to scsi command we're working on
1647 * @bpl: pointer to buffer list for protection groups
1648 * @datacnt: number of segments of data that have been dma mapped
1649 * @protcnt: number of segment of protection data that have been dma mapped
1651 * This function sets up BPL buffer list for protection groups of
1652 * type LPFC_PG_TYPE_DIF
1654 * This is usually used when DIFs are in their own buffers,
1655 * separate from the data. The HBA can then by instructed
1656 * to place the DIFs in the outgoing stream. For read operations,
1657 * The HBA could extract the DIFs and place it in DIF buffers.
1659 * The buffer list for this type consists of one or more of the
1660 * protection groups described below:
1661 * +-------------------------+
1662 * start of first prot group --> | PDE_5 |
1663 * +-------------------------+
1665 * +-------------------------+
1666 * | PDE_7 (Prot BDE) |
1667 * +-------------------------+
1669 * +-------------------------+
1670 * |more Data BDE's ... (opt)|
1671 * +-------------------------+
1672 * start of new prot group --> | PDE_5 |
1673 * +-------------------------+
1675 * +-------------------------+
1677 * Note: It is assumed that both data and protection s/g buffers have been
1680 * Returns the number of BDEs added to the BPL.
1683 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1684 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1686 struct scatterlist *sgde = NULL; /* s/g data entry */
1687 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1688 struct lpfc_pde5 *pde5 = NULL;
1689 struct lpfc_pde6 *pde6 = NULL;
1690 struct lpfc_pde7 *pde7 = NULL;
1691 dma_addr_t dataphysaddr, protphysaddr;
1692 unsigned short curr_prot = 0;
1693 unsigned int split_offset;
1694 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
1695 unsigned int protgrp_blks, protgrp_bytes;
1696 unsigned int remainder, subtotal;
1698 int datadir = sc->sc_data_direction;
1699 unsigned char pgdone = 0, alldone = 0;
1701 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1704 uint32_t checking = 1;
1709 sgpe = scsi_prot_sglist(sc);
1710 sgde = scsi_sglist(sc);
1712 if (!sgpe || !sgde) {
1713 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1714 "9020 Invalid s/g entry: data=x%px prot=x%px\n",
1719 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1723 /* extract some info from the scsi command */
1724 blksize = scsi_prot_interval(sc);
1725 reftag = scsi_prot_ref_tag(sc);
1726 if (reftag == LPFC_INVALID_REFTAG)
1729 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1730 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1732 if (rc & BG_ERR_SWAP)
1733 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1734 if (rc & BG_ERR_CHECK)
1741 /* Check to see if we ran out of space */
1742 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1745 /* setup PDE5 with what we have */
1746 pde5 = (struct lpfc_pde5 *) bpl;
1747 memset(pde5, 0, sizeof(struct lpfc_pde5));
1748 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1750 /* Endianness conversion if necessary for PDE5 */
1751 pde5->word0 = cpu_to_le32(pde5->word0);
1752 pde5->reftag = cpu_to_le32(reftag);
1754 /* advance bpl and increment bde count */
1757 pde6 = (struct lpfc_pde6 *) bpl;
1759 /* setup PDE6 with the rest of the info */
1760 memset(pde6, 0, sizeof(struct lpfc_pde6));
1761 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1762 bf_set(pde6_optx, pde6, txop);
1763 bf_set(pde6_oprx, pde6, rxop);
1765 if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
1766 bf_set(pde6_ce, pde6, checking);
1768 bf_set(pde6_ce, pde6, 0);
1770 if (sc->prot_flags & SCSI_PROT_REF_CHECK)
1771 bf_set(pde6_re, pde6, checking);
1773 bf_set(pde6_re, pde6, 0);
1775 bf_set(pde6_ai, pde6, 1);
1776 bf_set(pde6_ae, pde6, 0);
1777 bf_set(pde6_apptagval, pde6, 0);
1779 /* Endianness conversion if necessary for PDE6 */
1780 pde6->word0 = cpu_to_le32(pde6->word0);
1781 pde6->word1 = cpu_to_le32(pde6->word1);
1782 pde6->word2 = cpu_to_le32(pde6->word2);
1784 /* advance bpl and increment bde count */
1788 /* setup the first BDE that points to protection buffer */
1789 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1790 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
1792 /* must be integer multiple of the DIF block length */
1793 BUG_ON(protgroup_len % 8);
1795 pde7 = (struct lpfc_pde7 *) bpl;
1796 memset(pde7, 0, sizeof(struct lpfc_pde7));
1797 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1799 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1800 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1802 protgrp_blks = protgroup_len / 8;
1803 protgrp_bytes = protgrp_blks * blksize;
1805 /* check if this pde is crossing the 4K boundary; if so split */
1806 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1807 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1808 protgroup_offset += protgroup_remainder;
1809 protgrp_blks = protgroup_remainder / 8;
1810 protgrp_bytes = protgrp_blks * blksize;
1812 protgroup_offset = 0;
1818 /* setup BDE's for data blocks associated with DIF data */
1820 subtotal = 0; /* total bytes processed for current prot grp */
1822 /* Check to see if we ran out of space */
1823 if (num_bde >= phba->cfg_total_seg_cnt)
1827 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1828 "9065 BLKGRD:%s Invalid data segment\n",
1833 dataphysaddr = sg_dma_address(sgde) + split_offset;
1834 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1835 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1837 remainder = sg_dma_len(sgde) - split_offset;
1839 if ((subtotal + remainder) <= protgrp_bytes) {
1840 /* we can use this whole buffer */
1841 bpl->tus.f.bdeSize = remainder;
1844 if ((subtotal + remainder) == protgrp_bytes)
1847 /* must split this buffer with next prot grp */
1848 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1849 split_offset += bpl->tus.f.bdeSize;
1852 subtotal += bpl->tus.f.bdeSize;
1854 if (datadir == DMA_TO_DEVICE)
1855 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1857 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1858 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1865 /* Move to the next s/g segment if possible */
1866 sgde = sg_next(sgde);
1870 if (protgroup_offset) {
1871 /* update the reference tag */
1872 reftag += protgrp_blks;
1878 if (curr_prot == protcnt) {
1880 } else if (curr_prot < protcnt) {
1881 /* advance to next prot buffer */
1882 sgpe = sg_next(sgpe);
1885 /* update the reference tag */
1886 reftag += protgrp_blks;
1888 /* if we're here, we have a bug */
1889 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
1890 "9054 BLKGRD: bug in %s\n", __func__);
1900 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1901 * @phba: The Hba for which this call is being executed.
1902 * @sc: pointer to scsi command we're working on
1903 * @sgl: pointer to buffer list for protection groups
1904 * @datasegcnt: number of segments of data that have been dma mapped
1905 * @lpfc_cmd: lpfc scsi command object pointer.
1907 * This function sets up SGL buffer list for protection groups of
1908 * type LPFC_PG_TYPE_NO_DIF
1910 * This is usually used when the HBA is instructed to generate
1911 * DIFs and insert them into data stream (or strip DIF from
1912 * incoming data stream)
1914 * The buffer list consists of just one protection group described
1916 * +-------------------------+
1917 * start of prot group --> | DI_SEED |
1918 * +-------------------------+
1920 * +-------------------------+
1921 * |more Data SGE's ... (opt)|
1922 * +-------------------------+
1925 * Note: Data s/g buffers have been dma mapped
1927 * Returns the number of SGEs added to the SGL.
1930 lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1931 struct sli4_sge *sgl, int datasegcnt,
1932 struct lpfc_io_buf *lpfc_cmd)
1934 struct scatterlist *sgde = NULL; /* s/g data entry */
1935 struct sli4_sge_diseed *diseed = NULL;
1936 dma_addr_t physaddr;
1937 int i = 0, num_sge = 0, status;
1940 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1943 uint32_t checking = 1;
1945 uint32_t dma_offset = 0;
1946 struct sli4_hybrid_sgl *sgl_xtra = NULL;
1948 bool lsp_just_set = false;
1950 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1954 /* extract some info from the scsi command for pde*/
1955 reftag = scsi_prot_ref_tag(sc);
1956 if (reftag == LPFC_INVALID_REFTAG)
1959 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1960 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1962 if (rc & BG_ERR_SWAP)
1963 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1964 if (rc & BG_ERR_CHECK)
1969 /* setup DISEED with what we have */
1970 diseed = (struct sli4_sge_diseed *) sgl;
1971 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
1972 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
1974 /* Endianness conversion if necessary */
1975 diseed->ref_tag = cpu_to_le32(reftag);
1976 diseed->ref_tag_tran = diseed->ref_tag;
1979 * We only need to check the data on READs, for WRITEs
1980 * protection data is automatically generated, not checked.
1982 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
1983 if (sc->prot_flags & SCSI_PROT_GUARD_CHECK)
1984 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
1986 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
1988 if (sc->prot_flags & SCSI_PROT_REF_CHECK)
1989 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
1991 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
1994 /* setup DISEED with the rest of the info */
1995 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
1996 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
1998 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
1999 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2001 /* Endianness conversion if necessary for DISEED */
2002 diseed->word2 = cpu_to_le32(diseed->word2);
2003 diseed->word3 = cpu_to_le32(diseed->word3);
2005 /* advance bpl and increment sge count */
2009 /* assumption: caller has already run dma_map_sg on command data */
2010 sgde = scsi_sglist(sc);
2012 for (i = 0; i < datasegcnt; i++) {
2016 /* do we need to expand the segment */
2017 if (!lsp_just_set && !((j + 1) % phba->border_sge_num) &&
2018 ((datasegcnt - 1) != i)) {
2020 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2022 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2024 if (unlikely(!sgl_xtra)) {
2025 lpfc_cmd->seg_cnt = 0;
2028 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2029 sgl_xtra->dma_phys_sgl));
2030 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2031 sgl_xtra->dma_phys_sgl));
2034 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2037 if (!(bf_get(lpfc_sli4_sge_type, sgl) & LPFC_SGE_TYPE_LSP)) {
2038 if ((datasegcnt - 1) == i)
2039 bf_set(lpfc_sli4_sge_last, sgl, 1);
2040 physaddr = sg_dma_address(sgde);
2041 dma_len = sg_dma_len(sgde);
2042 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2043 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2045 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2046 sgl->word2 = cpu_to_le32(sgl->word2);
2047 sgl->sge_len = cpu_to_le32(dma_len);
2049 dma_offset += dma_len;
2050 sgde = sg_next(sgde);
2054 lsp_just_set = false;
2057 sgl->word2 = cpu_to_le32(sgl->word2);
2058 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2060 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2063 lsp_just_set = true;
2075 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2076 * @phba: The Hba for which this call is being executed.
2077 * @sc: pointer to scsi command we're working on
2078 * @sgl: pointer to buffer list for protection groups
2079 * @datacnt: number of segments of data that have been dma mapped
2080 * @protcnt: number of segment of protection data that have been dma mapped
2081 * @lpfc_cmd: lpfc scsi command object pointer.
2083 * This function sets up SGL buffer list for protection groups of
2084 * type LPFC_PG_TYPE_DIF
2086 * This is usually used when DIFs are in their own buffers,
2087 * separate from the data. The HBA can then by instructed
2088 * to place the DIFs in the outgoing stream. For read operations,
2089 * The HBA could extract the DIFs and place it in DIF buffers.
2091 * The buffer list for this type consists of one or more of the
2092 * protection groups described below:
2093 * +-------------------------+
2094 * start of first prot group --> | DISEED |
2095 * +-------------------------+
2096 * | DIF (Prot SGE) |
2097 * +-------------------------+
2099 * +-------------------------+
2100 * |more Data SGE's ... (opt)|
2101 * +-------------------------+
2102 * start of new prot group --> | DISEED |
2103 * +-------------------------+
2105 * +-------------------------+
2107 * Note: It is assumed that both data and protection s/g buffers have been
2110 * Returns the number of SGEs added to the SGL.
2113 lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2114 struct sli4_sge *sgl, int datacnt, int protcnt,
2115 struct lpfc_io_buf *lpfc_cmd)
2117 struct scatterlist *sgde = NULL; /* s/g data entry */
2118 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2119 struct sli4_sge_diseed *diseed = NULL;
2120 dma_addr_t dataphysaddr, protphysaddr;
2121 unsigned short curr_prot = 0;
2122 unsigned int split_offset;
2123 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2124 unsigned int protgrp_blks, protgrp_bytes;
2125 unsigned int remainder, subtotal;
2127 unsigned char pgdone = 0, alldone = 0;
2132 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2135 uint32_t checking = 1;
2136 uint32_t dma_offset = 0;
2137 int num_sge = 0, j = 2;
2138 struct sli4_hybrid_sgl *sgl_xtra = NULL;
2140 sgpe = scsi_prot_sglist(sc);
2141 sgde = scsi_sglist(sc);
2143 if (!sgpe || !sgde) {
2144 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2145 "9082 Invalid s/g entry: data=x%px prot=x%px\n",
2150 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2154 /* extract some info from the scsi command */
2155 blksize = scsi_prot_interval(sc);
2156 reftag = scsi_prot_ref_tag(sc);
2157 if (reftag == LPFC_INVALID_REFTAG)
2160 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2161 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2163 if (rc & BG_ERR_SWAP)
2164 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2165 if (rc & BG_ERR_CHECK)
2172 /* Check to see if we ran out of space */
2173 if ((num_sge >= (phba->cfg_total_seg_cnt - 2)) &&
2177 /* DISEED and DIF have to be together */
2178 if (!((j + 1) % phba->border_sge_num) ||
2179 !((j + 2) % phba->border_sge_num) ||
2180 !((j + 3) % phba->border_sge_num)) {
2184 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2186 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2188 if (unlikely(!sgl_xtra)) {
2191 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2192 sgl_xtra->dma_phys_sgl));
2193 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2194 sgl_xtra->dma_phys_sgl));
2197 sgl->word2 = cpu_to_le32(sgl->word2);
2198 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2200 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2204 /* setup DISEED with what we have */
2205 diseed = (struct sli4_sge_diseed *) sgl;
2206 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2207 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2209 /* Endianness conversion if necessary */
2210 diseed->ref_tag = cpu_to_le32(reftag);
2211 diseed->ref_tag_tran = diseed->ref_tag;
2213 if (sc->prot_flags & SCSI_PROT_GUARD_CHECK) {
2214 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2216 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2218 * When in this mode, the hardware will replace
2219 * the guard tag from the host with a
2220 * newly generated good CRC for the wire.
2221 * Switch to raw mode here to avoid this
2222 * behavior. What the host sends gets put on the wire.
2224 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2225 txop = BG_OP_RAW_MODE;
2226 rxop = BG_OP_RAW_MODE;
2231 if (sc->prot_flags & SCSI_PROT_REF_CHECK)
2232 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2234 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2236 /* setup DISEED with the rest of the info */
2237 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2238 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2240 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2241 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2243 /* Endianness conversion if necessary for DISEED */
2244 diseed->word2 = cpu_to_le32(diseed->word2);
2245 diseed->word3 = cpu_to_le32(diseed->word3);
2247 /* advance sgl and increment bde count */
2253 /* setup the first BDE that points to protection buffer */
2254 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2255 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2257 /* must be integer multiple of the DIF block length */
2258 BUG_ON(protgroup_len % 8);
2260 /* Now setup DIF SGE */
2262 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2263 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2264 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2265 sgl->word2 = cpu_to_le32(sgl->word2);
2268 protgrp_blks = protgroup_len / 8;
2269 protgrp_bytes = protgrp_blks * blksize;
2271 /* check if DIF SGE is crossing the 4K boundary; if so split */
2272 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2273 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2274 protgroup_offset += protgroup_remainder;
2275 protgrp_blks = protgroup_remainder / 8;
2276 protgrp_bytes = protgrp_blks * blksize;
2278 protgroup_offset = 0;
2284 /* setup SGE's for data blocks associated with DIF data */
2286 subtotal = 0; /* total bytes processed for current prot grp */
2292 /* Check to see if we ran out of space */
2293 if ((num_sge >= phba->cfg_total_seg_cnt) &&
2298 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2299 "9086 BLKGRD:%s Invalid data segment\n",
2304 if (!((j + 1) % phba->border_sge_num)) {
2308 bf_set(lpfc_sli4_sge_type, sgl,
2311 sgl_xtra = lpfc_get_sgl_per_hdwq(phba,
2314 if (unlikely(!sgl_xtra)) {
2317 sgl->addr_lo = cpu_to_le32(
2318 putPaddrLow(sgl_xtra->dma_phys_sgl));
2319 sgl->addr_hi = cpu_to_le32(
2320 putPaddrHigh(sgl_xtra->dma_phys_sgl));
2323 sgl->word2 = cpu_to_le32(sgl->word2);
2324 sgl->sge_len = cpu_to_le32(
2325 phba->cfg_sg_dma_buf_size);
2327 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2329 dataphysaddr = sg_dma_address(sgde) +
2332 remainder = sg_dma_len(sgde) - split_offset;
2334 if ((subtotal + remainder) <= protgrp_bytes) {
2335 /* we can use this whole buffer */
2336 dma_len = remainder;
2339 if ((subtotal + remainder) ==
2343 /* must split this buffer with next
2346 dma_len = protgrp_bytes - subtotal;
2347 split_offset += dma_len;
2350 subtotal += dma_len;
2353 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2355 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2357 bf_set(lpfc_sli4_sge_last, sgl, 0);
2358 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2359 bf_set(lpfc_sli4_sge_type, sgl,
2360 LPFC_SGE_TYPE_DATA);
2362 sgl->sge_len = cpu_to_le32(dma_len);
2363 dma_offset += dma_len;
2373 /* Move to the next s/g segment if possible */
2374 sgde = sg_next(sgde);
2382 if (protgroup_offset) {
2383 /* update the reference tag */
2384 reftag += protgrp_blks;
2389 if (curr_prot == protcnt) {
2390 /* mark the last SGL */
2392 bf_set(lpfc_sli4_sge_last, sgl, 1);
2394 } else if (curr_prot < protcnt) {
2395 /* advance to next prot buffer */
2396 sgpe = sg_next(sgpe);
2398 /* update the reference tag */
2399 reftag += protgrp_blks;
2401 /* if we're here, we have a bug */
2402 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2403 "9085 BLKGRD: bug in %s\n", __func__);
2414 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2415 * @phba: The Hba for which this call is being executed.
2416 * @sc: pointer to scsi command we're working on
2418 * Given a SCSI command that supports DIF, determine composition of protection
2419 * groups involved in setting up buffer lists
2421 * Returns: Protection group type (with or without DIF)
2425 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2427 int ret = LPFC_PG_TYPE_INVALID;
2428 unsigned char op = scsi_get_prot_op(sc);
2431 case SCSI_PROT_READ_STRIP:
2432 case SCSI_PROT_WRITE_INSERT:
2433 ret = LPFC_PG_TYPE_NO_DIF;
2435 case SCSI_PROT_READ_INSERT:
2436 case SCSI_PROT_WRITE_STRIP:
2437 case SCSI_PROT_READ_PASS:
2438 case SCSI_PROT_WRITE_PASS:
2439 ret = LPFC_PG_TYPE_DIF_BUF;
2443 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2444 "9021 Unsupported protection op:%d\n",
2452 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2453 * @phba: The Hba for which this call is being executed.
2454 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2456 * Adjust the data length to account for how much data
2457 * is actually on the wire.
2459 * returns the adjusted data length
2462 lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2463 struct lpfc_io_buf *lpfc_cmd)
2465 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2468 fcpdl = scsi_bufflen(sc);
2470 /* Check if there is protection data on the wire */
2471 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2472 /* Read check for protection data */
2473 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2477 /* Write check for protection data */
2478 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2483 * If we are in DIF Type 1 mode every data block has a 8 byte
2484 * DIF (trailer) attached to it. Must ajust FCP data length
2485 * to account for the protection data.
2487 fcpdl += (fcpdl / scsi_prot_interval(sc)) * 8;
2493 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2494 * @phba: The Hba for which this call is being executed.
2495 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2497 * This is the protection/DIF aware version of
2498 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2499 * two functions eventually, but for now, it's here.
2500 * RETURNS 0 - SUCCESS,
2501 * 1 - Failed DMA map, retry.
2502 * 2 - Invalid scsi cmd or prot-type. Do not rety.
2505 lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2506 struct lpfc_io_buf *lpfc_cmd)
2508 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2509 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2510 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
2511 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2512 uint32_t num_bde = 0;
2513 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2514 int prot_group_type = 0;
2517 struct lpfc_vport *vport = phba->pport;
2520 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2521 * fcp_rsp regions to the first data bde entry
2524 if (scsi_sg_count(scsi_cmnd)) {
2526 * The driver stores the segment count returned from dma_map_sg
2527 * because this a count of dma-mappings used to map the use_sg
2528 * pages. They are not guaranteed to be the same for those
2529 * architectures that implement an IOMMU.
2531 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2532 scsi_sglist(scsi_cmnd),
2533 scsi_sg_count(scsi_cmnd), datadir);
2534 if (unlikely(!datasegcnt))
2537 lpfc_cmd->seg_cnt = datasegcnt;
2539 /* First check if data segment count from SCSI Layer is good */
2540 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2541 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
2546 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2548 switch (prot_group_type) {
2549 case LPFC_PG_TYPE_NO_DIF:
2551 /* Here we need to add a PDE5 and PDE6 to the count */
2552 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt) {
2557 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2559 /* we should have 2 or more entries in buffer list */
2566 case LPFC_PG_TYPE_DIF_BUF:
2568 * This type indicates that protection buffers are
2569 * passed to the driver, so that needs to be prepared
2572 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2573 scsi_prot_sglist(scsi_cmnd),
2574 scsi_prot_sg_count(scsi_cmnd), datadir);
2575 if (unlikely(!protsegcnt)) {
2576 scsi_dma_unmap(scsi_cmnd);
2580 lpfc_cmd->prot_seg_cnt = protsegcnt;
2583 * There is a minimun of 4 BPLs used for every
2584 * protection data segment.
2586 if ((lpfc_cmd->prot_seg_cnt * 4) >
2587 (phba->cfg_total_seg_cnt - 2)) {
2592 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2593 datasegcnt, protsegcnt);
2594 /* we should have 3 or more entries in buffer list */
2595 if ((num_bde < 3) ||
2596 (num_bde > phba->cfg_total_seg_cnt)) {
2602 case LPFC_PG_TYPE_INVALID:
2604 scsi_dma_unmap(scsi_cmnd);
2605 lpfc_cmd->seg_cnt = 0;
2607 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2608 "9022 Unexpected protection group %i\n",
2615 * Finish initializing those IOCB fields that are dependent on the
2616 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2617 * reinitialized since all iocb memory resources are used many times
2618 * for transmit, receive, and continuation bpl's.
2620 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2621 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2622 iocb_cmd->ulpBdeCount = 1;
2623 iocb_cmd->ulpLe = 1;
2625 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
2626 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2629 * Due to difference in data length between DIF/non-DIF paths,
2630 * we need to set word 4 of IOCB here
2632 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2635 * For First burst, we may need to adjust the initial transfer
2638 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2639 (fcpdl < vport->cfg_first_burst_size))
2640 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2644 if (lpfc_cmd->seg_cnt)
2645 scsi_dma_unmap(scsi_cmnd);
2646 if (lpfc_cmd->prot_seg_cnt)
2647 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2648 scsi_prot_sg_count(scsi_cmnd),
2649 scsi_cmnd->sc_data_direction);
2651 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
2652 "9023 Cannot setup S/G List for HBA"
2653 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2654 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2655 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
2656 prot_group_type, num_bde);
2658 lpfc_cmd->seg_cnt = 0;
2659 lpfc_cmd->prot_seg_cnt = 0;
2664 * This function calcuates the T10 DIF guard tag
2665 * on the specified data using a CRC algorithmn
2669 lpfc_bg_crc(uint8_t *data, int count)
2674 crc = crc_t10dif(data, count);
2675 x = cpu_to_be16(crc);
2680 * This function calcuates the T10 DIF guard tag
2681 * on the specified data using a CSUM algorithmn
2682 * using ip_compute_csum.
2685 lpfc_bg_csum(uint8_t *data, int count)
2689 ret = ip_compute_csum(data, count);
2694 * This function examines the protection data to try to determine
2695 * what type of T10-DIF error occurred.
2698 lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
2700 struct scatterlist *sgpe; /* s/g prot entry */
2701 struct scatterlist *sgde; /* s/g data entry */
2702 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2703 struct scsi_dif_tuple *src = NULL;
2704 uint8_t *data_src = NULL;
2706 uint16_t start_app_tag, app_tag;
2707 uint32_t start_ref_tag, ref_tag;
2708 int prot, protsegcnt;
2709 int err_type, len, data_len;
2710 int chk_ref, chk_app, chk_guard;
2714 err_type = BGS_GUARD_ERR_MASK;
2718 /* First check to see if there is protection data to examine */
2719 prot = scsi_get_prot_op(cmd);
2720 if ((prot == SCSI_PROT_READ_STRIP) ||
2721 (prot == SCSI_PROT_WRITE_INSERT) ||
2722 (prot == SCSI_PROT_NORMAL))
2725 /* Currently the driver just supports ref_tag and guard_tag checking */
2730 /* Setup a ptr to the protection data provided by the SCSI host */
2731 sgpe = scsi_prot_sglist(cmd);
2732 protsegcnt = lpfc_cmd->prot_seg_cnt;
2734 if (sgpe && protsegcnt) {
2737 * We will only try to verify guard tag if the segment
2738 * data length is a multiple of the blksize.
2740 sgde = scsi_sglist(cmd);
2741 blksize = scsi_prot_interval(cmd);
2742 data_src = (uint8_t *)sg_virt(sgde);
2743 data_len = sgde->length;
2744 if ((data_len & (blksize - 1)) == 0)
2747 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2748 start_ref_tag = scsi_prot_ref_tag(cmd);
2749 if (start_ref_tag == LPFC_INVALID_REFTAG)
2751 start_app_tag = src->app_tag;
2753 while (src && protsegcnt) {
2757 * First check to see if a protection data
2760 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2761 (src->app_tag == T10_PI_APP_ESCAPE)) {
2766 /* First Guard Tag checking */
2768 guard_tag = src->guard_tag;
2770 & SCSI_PROT_IP_CHECKSUM)
2771 sum = lpfc_bg_csum(data_src,
2774 sum = lpfc_bg_crc(data_src,
2776 if ((guard_tag != sum)) {
2777 err_type = BGS_GUARD_ERR_MASK;
2782 /* Reference Tag checking */
2783 ref_tag = be32_to_cpu(src->ref_tag);
2784 if (chk_ref && (ref_tag != start_ref_tag)) {
2785 err_type = BGS_REFTAG_ERR_MASK;
2790 /* App Tag checking */
2791 app_tag = src->app_tag;
2792 if (chk_app && (app_tag != start_app_tag)) {
2793 err_type = BGS_APPTAG_ERR_MASK;
2797 len -= sizeof(struct scsi_dif_tuple);
2802 data_src += blksize;
2803 data_len -= blksize;
2806 * Are we at the end of the Data segment?
2807 * The data segment is only used for Guard
2810 if (chk_guard && (data_len == 0)) {
2812 sgde = sg_next(sgde);
2816 data_src = (uint8_t *)sg_virt(sgde);
2817 data_len = sgde->length;
2818 if ((data_len & (blksize - 1)) == 0)
2823 /* Goto the next Protection data segment */
2824 sgpe = sg_next(sgpe);
2826 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2835 if (err_type == BGS_GUARD_ERR_MASK) {
2836 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
2837 set_host_byte(cmd, DID_ABORT);
2838 phba->bg_guard_err_cnt++;
2839 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2840 "9069 BLKGRD: reftag %x grd_tag err %x != %x\n",
2841 scsi_prot_ref_tag(cmd),
2844 } else if (err_type == BGS_REFTAG_ERR_MASK) {
2845 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
2846 set_host_byte(cmd, DID_ABORT);
2848 phba->bg_reftag_err_cnt++;
2849 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2850 "9066 BLKGRD: reftag %x ref_tag err %x != %x\n",
2851 scsi_prot_ref_tag(cmd),
2852 ref_tag, start_ref_tag);
2854 } else if (err_type == BGS_APPTAG_ERR_MASK) {
2855 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
2856 set_host_byte(cmd, DID_ABORT);
2858 phba->bg_apptag_err_cnt++;
2859 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2860 "9041 BLKGRD: reftag %x app_tag err %x != %x\n",
2861 scsi_prot_ref_tag(cmd),
2862 app_tag, start_app_tag);
2867 * This function checks for BlockGuard errors detected by
2868 * the HBA. In case of errors, the ASC/ASCQ fields in the
2869 * sense buffer will be set accordingly, paired with
2870 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2871 * detected corruption.
2874 * 0 - No error found
2875 * 1 - BlockGuard error found
2876 * -1 - Internal error (bad profile, ...etc)
2879 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
2880 struct lpfc_iocbq *pIocbOut)
2882 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2883 struct sli3_bg_fields *bgf;
2885 struct lpfc_wcqe_complete *wcqe;
2889 u64 failing_sector = 0;
2891 if (phba->sli_rev == LPFC_SLI_REV4) {
2892 wcqe = &pIocbOut->wcqe_cmpl;
2893 status = bf_get(lpfc_wcqe_c_status, wcqe);
2895 if (status == CQE_STATUS_DI_ERROR) {
2896 /* Guard Check failed */
2897 if (bf_get(lpfc_wcqe_c_bg_ge, wcqe))
2898 bgstat |= BGS_GUARD_ERR_MASK;
2900 /* AppTag Check failed */
2901 if (bf_get(lpfc_wcqe_c_bg_ae, wcqe))
2902 bgstat |= BGS_APPTAG_ERR_MASK;
2904 /* RefTag Check failed */
2905 if (bf_get(lpfc_wcqe_c_bg_re, wcqe))
2906 bgstat |= BGS_REFTAG_ERR_MASK;
2908 /* Check to see if there was any good data before the
2911 if (bf_get(lpfc_wcqe_c_bg_tdpv, wcqe)) {
2912 bgstat |= BGS_HI_WATER_MARK_PRESENT_MASK;
2913 bghm = wcqe->total_data_placed;
2917 * Set ALL the error bits to indicate we don't know what
2918 * type of error it is.
2921 bgstat |= (BGS_REFTAG_ERR_MASK |
2922 BGS_APPTAG_ERR_MASK |
2923 BGS_GUARD_ERR_MASK);
2927 bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2929 bgstat = bgf->bgstat;
2932 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2933 cmd->result = DID_ERROR << 16;
2934 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2935 "9072 BLKGRD: Invalid BG Profile in cmd "
2936 "0x%x reftag 0x%x blk cnt 0x%x "
2937 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2938 scsi_prot_ref_tag(cmd),
2939 scsi_logical_block_count(cmd), bgstat, bghm);
2944 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2945 cmd->result = DID_ERROR << 16;
2946 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2947 "9073 BLKGRD: Invalid BG PDIF Block in cmd "
2948 "0x%x reftag 0x%x blk cnt 0x%x "
2949 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2950 scsi_prot_ref_tag(cmd),
2951 scsi_logical_block_count(cmd), bgstat, bghm);
2956 if (lpfc_bgs_get_guard_err(bgstat)) {
2958 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x1);
2959 set_host_byte(cmd, DID_ABORT);
2960 phba->bg_guard_err_cnt++;
2961 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2962 "9055 BLKGRD: Guard Tag error in cmd "
2963 "0x%x reftag 0x%x blk cnt 0x%x "
2964 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2965 scsi_prot_ref_tag(cmd),
2966 scsi_logical_block_count(cmd), bgstat, bghm);
2969 if (lpfc_bgs_get_reftag_err(bgstat)) {
2971 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x3);
2972 set_host_byte(cmd, DID_ABORT);
2973 phba->bg_reftag_err_cnt++;
2974 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2975 "9056 BLKGRD: Ref Tag error in cmd "
2976 "0x%x reftag 0x%x blk cnt 0x%x "
2977 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2978 scsi_prot_ref_tag(cmd),
2979 scsi_logical_block_count(cmd), bgstat, bghm);
2982 if (lpfc_bgs_get_apptag_err(bgstat)) {
2984 scsi_build_sense(cmd, 1, ILLEGAL_REQUEST, 0x10, 0x2);
2985 set_host_byte(cmd, DID_ABORT);
2986 phba->bg_apptag_err_cnt++;
2987 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2988 "9061 BLKGRD: App Tag error in cmd "
2989 "0x%x reftag 0x%x blk cnt 0x%x "
2990 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2991 scsi_prot_ref_tag(cmd),
2992 scsi_logical_block_count(cmd), bgstat, bghm);
2995 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2997 * setup sense data descriptor 0 per SPC-4 as an information
2998 * field, and put the failing LBA in it.
2999 * This code assumes there was also a guard/app/ref tag error
3002 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3003 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3004 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3005 cmd->sense_buffer[10] = 0x80; /* Validity bit */
3007 /* bghm is a "on the wire" FC frame based count */
3008 switch (scsi_get_prot_op(cmd)) {
3009 case SCSI_PROT_READ_INSERT:
3010 case SCSI_PROT_WRITE_STRIP:
3011 bghm /= cmd->device->sector_size;
3013 case SCSI_PROT_READ_STRIP:
3014 case SCSI_PROT_WRITE_INSERT:
3015 case SCSI_PROT_READ_PASS:
3016 case SCSI_PROT_WRITE_PASS:
3017 bghm /= (cmd->device->sector_size +
3018 sizeof(struct scsi_dif_tuple));
3022 failing_sector = scsi_get_lba(cmd);
3023 failing_sector += bghm;
3025 /* Descriptor Information */
3026 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3030 /* No error was reported - problem in FW? */
3031 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3032 "9057 BLKGRD: Unknown error in cmd "
3033 "0x%x reftag 0x%x blk cnt 0x%x "
3034 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3035 scsi_prot_ref_tag(cmd),
3036 scsi_logical_block_count(cmd), bgstat, bghm);
3038 /* Calculate what type of error it was */
3039 lpfc_calc_bg_err(phba, lpfc_cmd);
3046 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3047 * @phba: The Hba for which this call is being executed.
3048 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3050 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3051 * field of @lpfc_cmd for device with SLI-4 interface spec.
3054 * 2 - Error - Do not retry
3059 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3061 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3062 struct scatterlist *sgel = NULL;
3063 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3064 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
3065 struct sli4_sge *first_data_sgl;
3066 struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
3067 struct lpfc_vport *vport = phba->pport;
3068 union lpfc_wqe128 *wqe = &pwqeq->wqe;
3069 dma_addr_t physaddr;
3071 uint32_t dma_offset = 0;
3073 struct ulp_bde64 *bde;
3074 bool lsp_just_set = false;
3075 struct sli4_hybrid_sgl *sgl_xtra = NULL;
3078 * There are three possibilities here - use scatter-gather segment, use
3079 * the single mapping, or neither. Start the lpfc command prep by
3080 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3083 if (scsi_sg_count(scsi_cmnd)) {
3085 * The driver stores the segment count returned from dma_map_sg
3086 * because this a count of dma-mappings used to map the use_sg
3087 * pages. They are not guaranteed to be the same for those
3088 * architectures that implement an IOMMU.
3091 nseg = scsi_dma_map(scsi_cmnd);
3092 if (unlikely(nseg <= 0))
3095 /* clear the last flag in the fcp_rsp map entry */
3096 sgl->word2 = le32_to_cpu(sgl->word2);
3097 bf_set(lpfc_sli4_sge_last, sgl, 0);
3098 sgl->word2 = cpu_to_le32(sgl->word2);
3100 first_data_sgl = sgl;
3101 lpfc_cmd->seg_cnt = nseg;
3102 if (!phba->cfg_xpsgl &&
3103 lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3104 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3106 " %s: Too many sg segments from "
3107 "dma_map_sg. Config %d, seg_cnt %d\n",
3108 __func__, phba->cfg_sg_seg_cnt,
3110 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3111 lpfc_cmd->seg_cnt = 0;
3112 scsi_dma_unmap(scsi_cmnd);
3117 * The driver established a maximum scatter-gather segment count
3118 * during probe that limits the number of sg elements in any
3119 * single scsi command. Just run through the seg_cnt and format
3121 * When using SLI-3 the driver will try to fit all the BDEs into
3122 * the IOCB. If it can't then the BDEs get added to a BPL as it
3123 * does for SLI-2 mode.
3126 /* for tracking segment boundaries */
3127 sgel = scsi_sglist(scsi_cmnd);
3129 for (i = 0; i < nseg; i++) {
3132 bf_set(lpfc_sli4_sge_last, sgl, 1);
3133 bf_set(lpfc_sli4_sge_type, sgl,
3134 LPFC_SGE_TYPE_DATA);
3136 bf_set(lpfc_sli4_sge_last, sgl, 0);
3138 /* do we need to expand the segment */
3139 if (!lsp_just_set &&
3140 !((j + 1) % phba->border_sge_num) &&
3141 ((nseg - 1) != i)) {
3143 bf_set(lpfc_sli4_sge_type, sgl,
3146 sgl_xtra = lpfc_get_sgl_per_hdwq(
3149 if (unlikely(!sgl_xtra)) {
3150 lpfc_cmd->seg_cnt = 0;
3151 scsi_dma_unmap(scsi_cmnd);
3154 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3155 sgl_xtra->dma_phys_sgl));
3156 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3157 sgl_xtra->dma_phys_sgl));
3160 bf_set(lpfc_sli4_sge_type, sgl,
3161 LPFC_SGE_TYPE_DATA);
3165 if (!(bf_get(lpfc_sli4_sge_type, sgl) &
3166 LPFC_SGE_TYPE_LSP)) {
3167 if ((nseg - 1) == i)
3168 bf_set(lpfc_sli4_sge_last, sgl, 1);
3170 physaddr = sg_dma_address(sgel);
3171 dma_len = sg_dma_len(sgel);
3172 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3174 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3177 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3178 sgl->word2 = cpu_to_le32(sgl->word2);
3179 sgl->sge_len = cpu_to_le32(dma_len);
3181 dma_offset += dma_len;
3182 sgel = sg_next(sgel);
3185 lsp_just_set = false;
3188 sgl->word2 = cpu_to_le32(sgl->word2);
3189 sgl->sge_len = cpu_to_le32(
3190 phba->cfg_sg_dma_buf_size);
3192 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
3195 lsp_just_set = true;
3201 /* PBDE support for first data SGE only.
3202 * For FCoE, we key off Performance Hints.
3203 * For FC, we key off lpfc_enable_pbde.
3206 ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3207 phba->cfg_enable_pbde)) {
3209 bde = (struct ulp_bde64 *)
3211 bde->addrLow = first_data_sgl->addr_lo;
3212 bde->addrHigh = first_data_sgl->addr_hi;
3213 bde->tus.f.bdeSize =
3214 le32_to_cpu(first_data_sgl->sge_len);
3215 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3216 bde->tus.w = cpu_to_le32(bde->tus.w);
3218 /* Word 11 - set PBDE bit */
3219 bf_set(wqe_pbde, &wqe->generic.wqe_com, 1);
3221 memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
3222 /* Word 11 - PBDE bit disabled by default template */
3226 /* set the last flag in the fcp_rsp map entry */
3227 sgl->word2 = le32_to_cpu(sgl->word2);
3228 bf_set(lpfc_sli4_sge_last, sgl, 1);
3229 sgl->word2 = cpu_to_le32(sgl->word2);
3231 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3232 phba->cfg_enable_pbde) {
3233 bde = (struct ulp_bde64 *)
3235 memset(bde, 0, (sizeof(uint32_t) * 3));
3240 * Finish initializing those IOCB fields that are dependent on the
3241 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3242 * explicitly reinitialized.
3243 * all iocb memory resources are reused.
3245 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3246 /* Set first-burst provided it was successfully negotiated */
3247 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
3248 vport->cfg_first_burst_size &&
3249 scsi_cmnd->sc_data_direction == DMA_TO_DEVICE) {
3250 u32 init_len, total_len;
3252 total_len = be32_to_cpu(fcp_cmnd->fcpDl);
3253 init_len = min(total_len, vport->cfg_first_burst_size);
3256 wqe->fcp_iwrite.initial_xfer_len = init_len;
3257 wqe->fcp_iwrite.total_xfer_len = total_len;
3260 wqe->fcp_iwrite.total_xfer_len =
3261 be32_to_cpu(fcp_cmnd->fcpDl);
3265 * If the OAS driver feature is enabled and the lun is enabled for
3266 * OAS, set the oas iocb related flags.
3268 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3269 scsi_cmnd->device->hostdata)->oas_enabled) {
3270 lpfc_cmd->cur_iocbq.cmd_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3271 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3272 scsi_cmnd->device->hostdata)->priority;
3275 bf_set(wqe_oas, &wqe->generic.wqe_com, 1);
3276 bf_set(wqe_ccpe, &wqe->generic.wqe_com, 1);
3278 if (lpfc_cmd->cur_iocbq.priority)
3279 bf_set(wqe_ccp, &wqe->generic.wqe_com,
3280 (lpfc_cmd->cur_iocbq.priority << 1));
3282 bf_set(wqe_ccp, &wqe->generic.wqe_com,
3283 (phba->cfg_XLanePriority << 1));
3290 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3291 * @phba: The Hba for which this call is being executed.
3292 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3294 * This is the protection/DIF aware version of
3295 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3296 * two functions eventually, but for now, it's here
3298 * 2 - Error - Do not retry
3303 lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3304 struct lpfc_io_buf *lpfc_cmd)
3306 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3307 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3308 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
3309 struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
3310 union lpfc_wqe128 *wqe = &pwqeq->wqe;
3311 uint32_t num_sge = 0;
3312 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3313 int prot_group_type = 0;
3316 struct lpfc_vport *vport = phba->pport;
3319 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3320 * fcp_rsp regions to the first data sge entry
3322 if (scsi_sg_count(scsi_cmnd)) {
3324 * The driver stores the segment count returned from dma_map_sg
3325 * because this a count of dma-mappings used to map the use_sg
3326 * pages. They are not guaranteed to be the same for those
3327 * architectures that implement an IOMMU.
3329 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3330 scsi_sglist(scsi_cmnd),
3331 scsi_sg_count(scsi_cmnd), datadir);
3332 if (unlikely(!datasegcnt))
3336 /* clear the last flag in the fcp_rsp map entry */
3337 sgl->word2 = le32_to_cpu(sgl->word2);
3338 bf_set(lpfc_sli4_sge_last, sgl, 0);
3339 sgl->word2 = cpu_to_le32(sgl->word2);
3342 lpfc_cmd->seg_cnt = datasegcnt;
3344 /* First check if data segment count from SCSI Layer is good */
3345 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt &&
3347 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3352 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3354 switch (prot_group_type) {
3355 case LPFC_PG_TYPE_NO_DIF:
3356 /* Here we need to add a DISEED to the count */
3357 if (((lpfc_cmd->seg_cnt + 1) >
3358 phba->cfg_total_seg_cnt) &&
3364 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3365 datasegcnt, lpfc_cmd);
3367 /* we should have 2 or more entries in buffer list */
3374 case LPFC_PG_TYPE_DIF_BUF:
3376 * This type indicates that protection buffers are
3377 * passed to the driver, so that needs to be prepared
3380 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3381 scsi_prot_sglist(scsi_cmnd),
3382 scsi_prot_sg_count(scsi_cmnd), datadir);
3383 if (unlikely(!protsegcnt)) {
3384 scsi_dma_unmap(scsi_cmnd);
3388 lpfc_cmd->prot_seg_cnt = protsegcnt;
3390 * There is a minimun of 3 SGEs used for every
3391 * protection data segment.
3393 if (((lpfc_cmd->prot_seg_cnt * 3) >
3394 (phba->cfg_total_seg_cnt - 2)) &&
3400 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3401 datasegcnt, protsegcnt, lpfc_cmd);
3403 /* we should have 3 or more entries in buffer list */
3405 (num_sge > phba->cfg_total_seg_cnt &&
3406 !phba->cfg_xpsgl)) {
3412 case LPFC_PG_TYPE_INVALID:
3414 scsi_dma_unmap(scsi_cmnd);
3415 lpfc_cmd->seg_cnt = 0;
3417 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3418 "9083 Unexpected protection group %i\n",
3424 switch (scsi_get_prot_op(scsi_cmnd)) {
3425 case SCSI_PROT_WRITE_STRIP:
3426 case SCSI_PROT_READ_STRIP:
3427 lpfc_cmd->cur_iocbq.cmd_flag |= LPFC_IO_DIF_STRIP;
3429 case SCSI_PROT_WRITE_INSERT:
3430 case SCSI_PROT_READ_INSERT:
3431 lpfc_cmd->cur_iocbq.cmd_flag |= LPFC_IO_DIF_INSERT;
3433 case SCSI_PROT_WRITE_PASS:
3434 case SCSI_PROT_READ_PASS:
3435 lpfc_cmd->cur_iocbq.cmd_flag |= LPFC_IO_DIF_PASS;
3439 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3440 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3442 /* Set first-burst provided it was successfully negotiated */
3443 if (!(phba->hba_flag & HBA_FCOE_MODE) &&
3444 vport->cfg_first_burst_size &&
3445 scsi_cmnd->sc_data_direction == DMA_TO_DEVICE) {
3446 u32 init_len, total_len;
3448 total_len = be32_to_cpu(fcp_cmnd->fcpDl);
3449 init_len = min(total_len, vport->cfg_first_burst_size);
3452 wqe->fcp_iwrite.initial_xfer_len = init_len;
3453 wqe->fcp_iwrite.total_xfer_len = total_len;
3456 wqe->fcp_iwrite.total_xfer_len =
3457 be32_to_cpu(fcp_cmnd->fcpDl);
3461 * If the OAS driver feature is enabled and the lun is enabled for
3462 * OAS, set the oas iocb related flags.
3464 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3465 scsi_cmnd->device->hostdata)->oas_enabled) {
3466 lpfc_cmd->cur_iocbq.cmd_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3469 bf_set(wqe_oas, &wqe->generic.wqe_com, 1);
3470 bf_set(wqe_ccpe, &wqe->generic.wqe_com, 1);
3471 bf_set(wqe_ccp, &wqe->generic.wqe_com,
3472 (phba->cfg_XLanePriority << 1));
3475 /* Word 7. DIF Flags */
3476 if (lpfc_cmd->cur_iocbq.cmd_flag & LPFC_IO_DIF_PASS)
3477 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_PASSTHRU);
3478 else if (lpfc_cmd->cur_iocbq.cmd_flag & LPFC_IO_DIF_STRIP)
3479 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_STRIP);
3480 else if (lpfc_cmd->cur_iocbq.cmd_flag & LPFC_IO_DIF_INSERT)
3481 bf_set(wqe_dif, &wqe->generic.wqe_com, LPFC_WQE_DIF_INSERT);
3483 lpfc_cmd->cur_iocbq.cmd_flag &= ~(LPFC_IO_DIF_PASS |
3484 LPFC_IO_DIF_STRIP | LPFC_IO_DIF_INSERT);
3488 if (lpfc_cmd->seg_cnt)
3489 scsi_dma_unmap(scsi_cmnd);
3490 if (lpfc_cmd->prot_seg_cnt)
3491 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3492 scsi_prot_sg_count(scsi_cmnd),
3493 scsi_cmnd->sc_data_direction);
3495 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
3496 "9084 Cannot setup S/G List for HBA"
3497 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3498 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3499 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3500 prot_group_type, num_sge);
3502 lpfc_cmd->seg_cnt = 0;
3503 lpfc_cmd->prot_seg_cnt = 0;
3508 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3509 * @phba: The Hba for which this call is being executed.
3510 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3512 * This routine wraps the actual DMA mapping function pointer from the
3520 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3522 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3526 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3528 * @phba: The Hba for which this call is being executed.
3529 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3531 * This routine wraps the actual DMA mapping function pointer from the
3539 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3541 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3545 * lpfc_scsi_prep_cmnd_buf - Wrapper function for IOCB/WQE mapping of scsi
3547 * @vport: Pointer to vport object.
3548 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3549 * @tmo: Timeout value for IO
3551 * This routine initializes IOCB/WQE data structure from scsi command
3558 lpfc_scsi_prep_cmnd_buf(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
3561 return vport->phba->lpfc_scsi_prep_cmnd_buf(vport, lpfc_cmd, tmo);
3565 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
3566 * @phba: Pointer to hba context object.
3567 * @vport: Pointer to vport object.
3568 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3569 * @fcpi_parm: FCP Initiator parameter.
3571 * This function posts an event when there is a SCSI command reporting
3572 * error from the scsi device.
3575 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3576 struct lpfc_io_buf *lpfc_cmd, uint32_t fcpi_parm) {
3577 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3578 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3579 uint32_t resp_info = fcprsp->rspStatus2;
3580 uint32_t scsi_status = fcprsp->rspStatus3;
3581 struct lpfc_fast_path_event *fast_path_evt = NULL;
3582 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3583 unsigned long flags;
3588 /* If there is queuefull or busy condition send a scsi event */
3589 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3590 (cmnd->result == SAM_STAT_BUSY)) {
3591 fast_path_evt = lpfc_alloc_fast_evt(phba);
3594 fast_path_evt->un.scsi_evt.event_type =
3596 fast_path_evt->un.scsi_evt.subcategory =
3597 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3598 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3599 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3600 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3601 &pnode->nlp_portname, sizeof(struct lpfc_name));
3602 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3603 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3604 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3605 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3606 fast_path_evt = lpfc_alloc_fast_evt(phba);
3609 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3611 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3612 LPFC_EVENT_CHECK_COND;
3613 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3615 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3616 &pnode->nlp_portname, sizeof(struct lpfc_name));
3617 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3618 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3619 fast_path_evt->un.check_cond_evt.sense_key =
3620 cmnd->sense_buffer[2] & 0xf;
3621 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3622 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3623 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3625 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3626 ((scsi_status == SAM_STAT_GOOD) &&
3627 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3629 * If status is good or resid does not match with fcp_param and
3630 * there is valid fcpi_parm, then there is a read_check error
3632 fast_path_evt = lpfc_alloc_fast_evt(phba);
3635 fast_path_evt->un.read_check_error.header.event_type =
3636 FC_REG_FABRIC_EVENT;
3637 fast_path_evt->un.read_check_error.header.subcategory =
3638 LPFC_EVENT_FCPRDCHKERR;
3639 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3640 &pnode->nlp_portname, sizeof(struct lpfc_name));
3641 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3642 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3643 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3644 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3645 fast_path_evt->un.read_check_error.fcpiparam =
3650 fast_path_evt->vport = vport;
3651 spin_lock_irqsave(&phba->hbalock, flags);
3652 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3653 spin_unlock_irqrestore(&phba->hbalock, flags);
3654 lpfc_worker_wake_up(phba);
3659 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3660 * @phba: The HBA for which this call is being executed.
3661 * @psb: The scsi buffer which is going to be un-mapped.
3663 * This routine does DMA un-mapping of scatter gather list of scsi command
3664 * field of @lpfc_cmd for device with SLI-3 interface spec.
3667 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
3670 * There are only two special cases to consider. (1) the scsi command
3671 * requested scatter-gather usage or (2) the scsi command allocated
3672 * a request buffer, but did not request use_sg. There is a third
3673 * case, but it does not require resource deallocation.
3675 if (psb->seg_cnt > 0)
3676 scsi_dma_unmap(psb->pCmd);
3677 if (psb->prot_seg_cnt > 0)
3678 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3679 scsi_prot_sg_count(psb->pCmd),
3680 psb->pCmd->sc_data_direction);
3684 * lpfc_unblock_requests - allow further commands to be queued.
3685 * @phba: pointer to phba object
3687 * For single vport, just call scsi_unblock_requests on physical port.
3688 * For multiple vports, send scsi_unblock_requests for all the vports.
3691 lpfc_unblock_requests(struct lpfc_hba *phba)
3693 struct lpfc_vport **vports;
3694 struct Scsi_Host *shost;
3697 if (phba->sli_rev == LPFC_SLI_REV4 &&
3698 !phba->sli4_hba.max_cfg_param.vpi_used) {
3699 shost = lpfc_shost_from_vport(phba->pport);
3700 scsi_unblock_requests(shost);
3704 vports = lpfc_create_vport_work_array(phba);
3706 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3707 shost = lpfc_shost_from_vport(vports[i]);
3708 scsi_unblock_requests(shost);
3710 lpfc_destroy_vport_work_array(phba, vports);
3714 * lpfc_block_requests - prevent further commands from being queued.
3715 * @phba: pointer to phba object
3717 * For single vport, just call scsi_block_requests on physical port.
3718 * For multiple vports, send scsi_block_requests for all the vports.
3721 lpfc_block_requests(struct lpfc_hba *phba)
3723 struct lpfc_vport **vports;
3724 struct Scsi_Host *shost;
3727 if (atomic_read(&phba->cmf_stop_io))
3730 if (phba->sli_rev == LPFC_SLI_REV4 &&
3731 !phba->sli4_hba.max_cfg_param.vpi_used) {
3732 shost = lpfc_shost_from_vport(phba->pport);
3733 scsi_block_requests(shost);
3737 vports = lpfc_create_vport_work_array(phba);
3739 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
3740 shost = lpfc_shost_from_vport(vports[i]);
3741 scsi_block_requests(shost);
3743 lpfc_destroy_vport_work_array(phba, vports);
3747 * lpfc_update_cmf_cmpl - Adjust CMF counters for IO completion
3748 * @phba: The HBA for which this call is being executed.
3749 * @time: The latency of the IO that completed (in ns)
3750 * @size: The size of the IO that completed
3751 * @shost: SCSI host the IO completed on (NULL for a NVME IO)
3753 * The routine adjusts the various Burst and Bandwidth counters used in
3754 * Congestion management and E2E. If time is set to LPFC_CGN_NOT_SENT,
3755 * that means the IO was never issued to the HBA, so this routine is
3756 * just being called to cleanup the counter from a previous
3757 * lpfc_update_cmf_cmd call.
3760 lpfc_update_cmf_cmpl(struct lpfc_hba *phba,
3761 uint64_t time, uint32_t size, struct Scsi_Host *shost)
3763 struct lpfc_cgn_stat *cgs;
3765 if (time != LPFC_CGN_NOT_SENT) {
3766 /* lat is ns coming in, save latency in us */
3770 time = div_u64(time + 500, 1000); /* round it */
3772 cgs = per_cpu_ptr(phba->cmf_stat, raw_smp_processor_id());
3773 atomic64_add(size, &cgs->rcv_bytes);
3774 atomic64_add(time, &cgs->rx_latency);
3775 atomic_inc(&cgs->rx_io_cnt);
3781 * lpfc_update_cmf_cmd - Adjust CMF counters for IO submission
3782 * @phba: The HBA for which this call is being executed.
3783 * @size: The size of the IO that will be issued
3785 * The routine adjusts the various Burst and Bandwidth counters used in
3786 * Congestion management and E2E.
3789 lpfc_update_cmf_cmd(struct lpfc_hba *phba, uint32_t size)
3792 struct lpfc_cgn_stat *cgs;
3795 /* At this point we are either LPFC_CFG_MANAGED or LPFC_CFG_MONITOR */
3796 if (phba->cmf_active_mode == LPFC_CFG_MANAGED &&
3797 phba->cmf_max_bytes_per_interval) {
3799 for_each_present_cpu(cpu) {
3800 cgs = per_cpu_ptr(phba->cmf_stat, cpu);
3801 total += atomic64_read(&cgs->total_bytes);
3803 if (total >= phba->cmf_max_bytes_per_interval) {
3804 if (!atomic_xchg(&phba->cmf_bw_wait, 1)) {
3805 lpfc_block_requests(phba);
3807 lpfc_calc_cmf_latency(phba);
3809 atomic_inc(&phba->cmf_busy);
3812 if (size > atomic_read(&phba->rx_max_read_cnt))
3813 atomic_set(&phba->rx_max_read_cnt, size);
3816 cgs = per_cpu_ptr(phba->cmf_stat, raw_smp_processor_id());
3817 atomic64_add(size, &cgs->total_bytes);
3822 * lpfc_handle_fcp_err - FCP response handler
3823 * @vport: The virtual port for which this call is being executed.
3824 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
3825 * @fcpi_parm: FCP Initiator parameter.
3827 * This routine is called to process response IOCB with status field
3828 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3829 * based upon SCSI and FCP error.
3832 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
3835 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3836 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3837 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3838 uint32_t resp_info = fcprsp->rspStatus2;
3839 uint32_t scsi_status = fcprsp->rspStatus3;
3841 uint32_t host_status = DID_OK;
3842 uint32_t rsplen = 0;
3844 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
3848 * If this is a task management command, there is no
3849 * scsi packet associated with this lpfc_cmd. The driver
3852 if (fcpcmd->fcpCntl2) {
3857 if (resp_info & RSP_LEN_VALID) {
3858 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3859 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
3860 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3861 "2719 Invalid response length: "
3862 "tgt x%x lun x%llx cmnd x%x rsplen "
3863 "x%x\n", cmnd->device->id,
3864 cmnd->device->lun, cmnd->cmnd[0],
3866 host_status = DID_ERROR;
3869 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3870 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
3871 "2757 Protocol failure detected during "
3872 "processing of FCP I/O op: "
3873 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
3875 cmnd->device->lun, cmnd->cmnd[0],
3877 host_status = DID_ERROR;
3882 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3883 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3884 if (snslen > SCSI_SENSE_BUFFERSIZE)
3885 snslen = SCSI_SENSE_BUFFERSIZE;
3887 if (resp_info & RSP_LEN_VALID)
3888 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3889 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3891 lp = (uint32_t *)cmnd->sense_buffer;
3893 /* special handling for under run conditions */
3894 if (!scsi_status && (resp_info & RESID_UNDER)) {
3895 /* don't log under runs if fcp set... */
3896 if (vport->cfg_log_verbose & LOG_FCP)
3897 logit = LOG_FCP_ERROR;
3898 /* unless operator says so */
3899 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3900 logit = LOG_FCP_UNDER;
3903 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3904 "9024 FCP command x%x failed: x%x SNS x%x x%x "
3905 "Data: x%x x%x x%x x%x x%x\n",
3906 cmnd->cmnd[0], scsi_status,
3907 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3908 be32_to_cpu(fcprsp->rspResId),
3909 be32_to_cpu(fcprsp->rspSnsLen),
3910 be32_to_cpu(fcprsp->rspRspLen),
3913 scsi_set_resid(cmnd, 0);
3914 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
3915 if (resp_info & RESID_UNDER) {
3916 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
3918 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
3919 "9025 FCP Underrun, expected %d, "
3920 "residual %d Data: x%x x%x x%x\n",
3922 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3926 * If there is an under run, check if under run reported by
3927 * storage array is same as the under run reported by HBA.
3928 * If this is not same, there is a dropped frame.
3930 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
3931 lpfc_printf_vlog(vport, KERN_WARNING,
3932 LOG_FCP | LOG_FCP_ERROR,
3933 "9026 FCP Read Check Error "
3934 "and Underrun Data: x%x x%x x%x x%x\n",
3936 scsi_get_resid(cmnd), fcpi_parm,
3938 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3939 host_status = DID_ERROR;
3942 * The cmnd->underflow is the minimum number of bytes that must
3943 * be transferred for this command. Provided a sense condition
3944 * is not present, make sure the actual amount transferred is at
3945 * least the underflow value or fail.
3947 if (!(resp_info & SNS_LEN_VALID) &&
3948 (scsi_status == SAM_STAT_GOOD) &&
3949 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3950 < cmnd->underflow)) {
3951 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3952 "9027 FCP command x%x residual "
3953 "underrun converted to error "
3954 "Data: x%x x%x x%x\n",
3955 cmnd->cmnd[0], scsi_bufflen(cmnd),
3956 scsi_get_resid(cmnd), cmnd->underflow);
3957 host_status = DID_ERROR;
3959 } else if (resp_info & RESID_OVER) {
3960 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3961 "9028 FCP command x%x residual overrun error. "
3962 "Data: x%x x%x\n", cmnd->cmnd[0],
3963 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
3964 host_status = DID_ERROR;
3967 * Check SLI validation that all the transfer was actually done
3968 * (fcpi_parm should be zero). Apply check only to reads.
3970 } else if (fcpi_parm) {
3971 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
3972 "9029 FCP %s Check Error Data: "
3973 "x%x x%x x%x x%x x%x\n",
3974 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3976 fcpDl, be32_to_cpu(fcprsp->rspResId),
3977 fcpi_parm, cmnd->cmnd[0], scsi_status);
3979 /* There is some issue with the LPe12000 that causes it
3980 * to miscalculate the fcpi_parm and falsely trip this
3981 * recovery logic. Detect this case and don't error when true.
3983 if (fcpi_parm > fcpDl)
3986 switch (scsi_status) {
3988 case SAM_STAT_CHECK_CONDITION:
3989 /* Fabric dropped a data frame. Fail any successful
3990 * command in which we detected dropped frames.
3991 * A status of good or some check conditions could
3992 * be considered a successful command.
3994 host_status = DID_ERROR;
3997 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
4001 cmnd->result = host_status << 16 | scsi_status;
4002 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, fcpi_parm);
4006 * lpfc_fcp_io_cmd_wqe_cmpl - Complete a FCP IO
4007 * @phba: The hba for which this call is being executed.
4008 * @pwqeIn: The command WQE for the scsi cmnd.
4009 * @pwqeOut: Pointer to driver response WQE object.
4011 * This routine assigns scsi command result by looking into response WQE
4012 * status field appropriately. This routine handles QUEUE FULL condition as
4013 * well by ramping down device queue depth.
4016 lpfc_fcp_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
4017 struct lpfc_iocbq *pwqeOut)
4019 struct lpfc_io_buf *lpfc_cmd = pwqeIn->io_buf;
4020 struct lpfc_wcqe_complete *wcqe = &pwqeOut->wcqe_cmpl;
4021 struct lpfc_vport *vport = pwqeIn->vport;
4022 struct lpfc_rport_data *rdata;
4023 struct lpfc_nodelist *ndlp;
4024 struct scsi_cmnd *cmd;
4025 unsigned long flags;
4026 struct lpfc_fast_path_event *fast_path_evt;
4027 struct Scsi_Host *shost;
4028 u32 logit = LOG_FCP;
4033 /* Sanity check on return of outstanding command */
4035 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4036 "9032 Null lpfc_cmd pointer. No "
4037 "release, skip completion\n");
4041 rdata = lpfc_cmd->rdata;
4042 ndlp = rdata->pnode;
4044 /* Sanity check on return of outstanding command */
4045 cmd = lpfc_cmd->pCmd;
4047 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4048 "9042 I/O completion: Not an active IO\n");
4049 lpfc_release_scsi_buf(phba, lpfc_cmd);
4052 /* Guard against abort handler being called at same time */
4053 spin_lock(&lpfc_cmd->buf_lock);
4054 idx = lpfc_cmd->cur_iocbq.hba_wqidx;
4055 if (phba->sli4_hba.hdwq)
4056 phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
4058 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4059 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
4060 this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
4062 shost = cmd->device->host;
4064 lpfc_cmd->status = bf_get(lpfc_wcqe_c_status, wcqe);
4065 lpfc_cmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
4067 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
4068 if (bf_get(lpfc_wcqe_c_xb, wcqe)) {
4069 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
4070 if (phba->cfg_fcp_wait_abts_rsp)
4074 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4075 if (lpfc_cmd->prot_data_type) {
4076 struct scsi_dif_tuple *src = NULL;
4078 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4080 * Used to restore any changes to protection
4081 * data for error injection.
4083 switch (lpfc_cmd->prot_data_type) {
4084 case LPFC_INJERR_REFTAG:
4086 lpfc_cmd->prot_data;
4088 case LPFC_INJERR_APPTAG:
4090 (uint16_t)lpfc_cmd->prot_data;
4092 case LPFC_INJERR_GUARD:
4094 (uint16_t)lpfc_cmd->prot_data;
4100 lpfc_cmd->prot_data = 0;
4101 lpfc_cmd->prot_data_type = 0;
4102 lpfc_cmd->prot_data_segment = NULL;
4105 if (unlikely(lpfc_cmd->status)) {
4106 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4107 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4108 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4109 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
4112 logit = LOG_FCP | LOG_FCP_UNDER;
4113 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4114 "9034 FCP cmd x%x failed <%d/%lld> "
4115 "status: x%x result: x%x "
4116 "sid: x%x did: x%x oxid: x%x "
4117 "Data: x%x x%x x%x\n",
4119 cmd->device ? cmd->device->id : 0xffff,
4120 cmd->device ? cmd->device->lun : 0xffff,
4121 lpfc_cmd->status, lpfc_cmd->result,
4123 (ndlp) ? ndlp->nlp_DID : 0,
4124 lpfc_cmd->cur_iocbq.sli4_xritag,
4125 wcqe->parameter, wcqe->total_data_placed,
4126 lpfc_cmd->cur_iocbq.iotag);
4129 switch (lpfc_cmd->status) {
4130 case CQE_STATUS_SUCCESS:
4131 cmd->result = DID_OK << 16;
4133 case CQE_STATUS_FCP_RSP_FAILURE:
4134 lpfc_handle_fcp_err(vport, lpfc_cmd,
4135 pwqeIn->wqe.fcp_iread.total_xfer_len -
4136 wcqe->total_data_placed);
4138 case CQE_STATUS_NPORT_BSY:
4139 case CQE_STATUS_FABRIC_BSY:
4140 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
4141 fast_path_evt = lpfc_alloc_fast_evt(phba);
4144 fast_path_evt->un.fabric_evt.event_type =
4145 FC_REG_FABRIC_EVENT;
4146 fast_path_evt->un.fabric_evt.subcategory =
4147 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4148 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4150 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4151 &ndlp->nlp_portname,
4152 sizeof(struct lpfc_name));
4153 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4154 &ndlp->nlp_nodename,
4155 sizeof(struct lpfc_name));
4157 fast_path_evt->vport = vport;
4158 fast_path_evt->work_evt.evt =
4159 LPFC_EVT_FASTPATH_MGMT_EVT;
4160 spin_lock_irqsave(&phba->hbalock, flags);
4161 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4163 spin_unlock_irqrestore(&phba->hbalock, flags);
4164 lpfc_worker_wake_up(phba);
4165 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4166 "9035 Fabric/Node busy FCP cmd x%x failed"
4168 "status: x%x result: x%x "
4169 "sid: x%x did: x%x oxid: x%x "
4170 "Data: x%x x%x x%x\n",
4172 cmd->device ? cmd->device->id : 0xffff,
4173 cmd->device ? cmd->device->lun : 0xffff,
4174 lpfc_cmd->status, lpfc_cmd->result,
4176 (ndlp) ? ndlp->nlp_DID : 0,
4177 lpfc_cmd->cur_iocbq.sli4_xritag,
4179 wcqe->total_data_placed,
4180 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4182 case CQE_STATUS_DI_ERROR:
4183 if (bf_get(lpfc_wcqe_c_bg_edir, wcqe))
4184 lpfc_cmd->result = IOERR_RX_DMA_FAILED;
4186 lpfc_cmd->result = IOERR_TX_DMA_FAILED;
4187 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_BG,
4188 "9048 DI Error xri x%x status x%x DI ext "
4189 "status x%x data placed x%x\n",
4190 lpfc_cmd->cur_iocbq.sli4_xritag,
4191 lpfc_cmd->status, wcqe->parameter,
4192 wcqe->total_data_placed);
4193 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4194 /* BG enabled cmd. Parse BG error */
4195 lpfc_parse_bg_err(phba, lpfc_cmd, pwqeOut);
4198 cmd->result = DID_ERROR << 16;
4199 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4200 "9040 DI Error on unprotected cmd\n");
4202 case CQE_STATUS_REMOTE_STOP:
4204 /* This I/O was aborted by the target, we don't
4205 * know the rxid and because we did not send the
4206 * ABTS we cannot generate and RRQ.
4208 lpfc_set_rrq_active(phba, ndlp,
4209 lpfc_cmd->cur_iocbq.sli4_lxritag,
4213 case CQE_STATUS_LOCAL_REJECT:
4214 if (lpfc_cmd->result & IOERR_DRVR_MASK)
4215 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4216 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4218 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4219 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4221 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4222 cmd->result = DID_NO_CONNECT << 16;
4225 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
4226 lpfc_cmd->result == IOERR_LINK_DOWN ||
4227 lpfc_cmd->result == IOERR_NO_RESOURCES ||
4228 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4229 lpfc_cmd->result == IOERR_RPI_SUSPENDED ||
4230 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
4231 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
4234 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4235 "9036 Local Reject FCP cmd x%x failed"
4237 "status: x%x result: x%x "
4238 "sid: x%x did: x%x oxid: x%x "
4239 "Data: x%x x%x x%x\n",
4241 cmd->device ? cmd->device->id : 0xffff,
4242 cmd->device ? cmd->device->lun : 0xffff,
4243 lpfc_cmd->status, lpfc_cmd->result,
4245 (ndlp) ? ndlp->nlp_DID : 0,
4246 lpfc_cmd->cur_iocbq.sli4_xritag,
4248 wcqe->total_data_placed,
4249 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4252 cmd->result = DID_ERROR << 16;
4253 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4254 "9037 FCP Completion Error: xri %x "
4255 "status x%x result x%x [x%x] "
4257 lpfc_cmd->cur_iocbq.sli4_xritag,
4258 lpfc_cmd->status, lpfc_cmd->result,
4260 wcqe->total_data_placed);
4262 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4263 u32 *lp = (u32 *)cmd->sense_buffer;
4265 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4266 "9039 Iodone <%d/%llu> cmd x%px, error "
4267 "x%x SNS x%x x%x LBA x%llx Data: x%x x%x\n",
4268 cmd->device->id, cmd->device->lun, cmd,
4269 cmd->result, *lp, *(lp + 3),
4270 (cmd->device->sector_size) ?
4271 (u64)scsi_get_lba(cmd) : 0,
4272 cmd->retries, scsi_get_resid(cmd));
4275 if (vport->cfg_max_scsicmpl_time &&
4276 time_after(jiffies, lpfc_cmd->start_time +
4277 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4278 spin_lock_irqsave(shost->host_lock, flags);
4280 if (ndlp->cmd_qdepth >
4281 atomic_read(&ndlp->cmd_pending) &&
4282 (atomic_read(&ndlp->cmd_pending) >
4283 LPFC_MIN_TGT_QDEPTH) &&
4284 (cmd->cmnd[0] == READ_10 ||
4285 cmd->cmnd[0] == WRITE_10))
4287 atomic_read(&ndlp->cmd_pending);
4289 ndlp->last_change_time = jiffies;
4291 spin_unlock_irqrestore(shost->host_lock, flags);
4293 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4295 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4296 if (lpfc_cmd->ts_cmd_start) {
4297 lpfc_cmd->ts_isr_cmpl = lpfc_cmd->cur_iocbq.isr_timestamp;
4298 lpfc_cmd->ts_data_io = ktime_get_ns();
4299 phba->ktime_last_cmd = lpfc_cmd->ts_data_io;
4300 lpfc_io_ktime(phba, lpfc_cmd);
4303 if (likely(!wait_xb_clr))
4304 lpfc_cmd->pCmd = NULL;
4305 spin_unlock(&lpfc_cmd->buf_lock);
4307 /* Check if IO qualified for CMF */
4308 if (phba->cmf_active_mode != LPFC_CFG_OFF &&
4309 cmd->sc_data_direction == DMA_FROM_DEVICE &&
4310 (scsi_sg_count(cmd))) {
4311 /* Used when calculating average latency */
4312 lat = ktime_get_ns() - lpfc_cmd->rx_cmd_start;
4313 lpfc_update_cmf_cmpl(phba, lat, scsi_bufflen(cmd), shost);
4319 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4323 * If there is an abort thread waiting for command completion
4324 * wake up the thread.
4326 spin_lock(&lpfc_cmd->buf_lock);
4327 lpfc_cmd->cur_iocbq.cmd_flag &= ~LPFC_DRIVER_ABORTED;
4328 if (lpfc_cmd->waitq)
4329 wake_up(lpfc_cmd->waitq);
4330 spin_unlock(&lpfc_cmd->buf_lock);
4332 lpfc_release_scsi_buf(phba, lpfc_cmd);
4336 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
4337 * @phba: The Hba for which this call is being executed.
4338 * @pIocbIn: The command IOCBQ for the scsi cmnd.
4339 * @pIocbOut: The response IOCBQ for the scsi cmnd.
4341 * This routine assigns scsi command result by looking into response IOCB
4342 * status field appropriately. This routine handles QUEUE FULL condition as
4343 * well by ramping down device queue depth.
4346 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4347 struct lpfc_iocbq *pIocbOut)
4349 struct lpfc_io_buf *lpfc_cmd =
4350 (struct lpfc_io_buf *) pIocbIn->io_buf;
4351 struct lpfc_vport *vport = pIocbIn->vport;
4352 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4353 struct lpfc_nodelist *pnode = rdata->pnode;
4354 struct scsi_cmnd *cmd;
4355 unsigned long flags;
4356 struct lpfc_fast_path_event *fast_path_evt;
4357 struct Scsi_Host *shost;
4359 uint32_t logit = LOG_FCP;
4361 /* Guard against abort handler being called at same time */
4362 spin_lock(&lpfc_cmd->buf_lock);
4364 /* Sanity check on return of outstanding command */
4365 cmd = lpfc_cmd->pCmd;
4366 if (!cmd || !phba) {
4367 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
4368 "2621 IO completion: Not an active IO\n");
4369 spin_unlock(&lpfc_cmd->buf_lock);
4373 idx = lpfc_cmd->cur_iocbq.hba_wqidx;
4374 if (phba->sli4_hba.hdwq)
4375 phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
4377 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4378 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
4379 this_cpu_inc(phba->sli4_hba.c_stat->cmpl_io);
4381 shost = cmd->device->host;
4383 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
4384 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
4385 /* pick up SLI4 exchange busy status from HBA */
4386 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
4387 if (pIocbOut->cmd_flag & LPFC_EXCHANGE_BUSY)
4388 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
4390 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4391 if (lpfc_cmd->prot_data_type) {
4392 struct scsi_dif_tuple *src = NULL;
4394 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4396 * Used to restore any changes to protection
4397 * data for error injection.
4399 switch (lpfc_cmd->prot_data_type) {
4400 case LPFC_INJERR_REFTAG:
4402 lpfc_cmd->prot_data;
4404 case LPFC_INJERR_APPTAG:
4406 (uint16_t)lpfc_cmd->prot_data;
4408 case LPFC_INJERR_GUARD:
4410 (uint16_t)lpfc_cmd->prot_data;
4416 lpfc_cmd->prot_data = 0;
4417 lpfc_cmd->prot_data_type = 0;
4418 lpfc_cmd->prot_data_segment = NULL;
4422 if (unlikely(lpfc_cmd->status)) {
4423 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4424 (lpfc_cmd->result & IOERR_DRVR_MASK))
4425 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4426 else if (lpfc_cmd->status >= IOSTAT_CNT)
4427 lpfc_cmd->status = IOSTAT_DEFAULT;
4428 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4429 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4430 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4431 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
4434 logit = LOG_FCP | LOG_FCP_UNDER;
4435 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4436 "9030 FCP cmd x%x failed <%d/%lld> "
4437 "status: x%x result: x%x "
4438 "sid: x%x did: x%x oxid: x%x "
4441 cmd->device ? cmd->device->id : 0xffff,
4442 cmd->device ? cmd->device->lun : 0xffff,
4443 lpfc_cmd->status, lpfc_cmd->result,
4445 (pnode) ? pnode->nlp_DID : 0,
4446 phba->sli_rev == LPFC_SLI_REV4 ?
4447 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4448 pIocbOut->iocb.ulpContext,
4449 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4451 switch (lpfc_cmd->status) {
4452 case IOSTAT_FCP_RSP_ERROR:
4453 /* Call FCP RSP handler to determine result */
4454 lpfc_handle_fcp_err(vport, lpfc_cmd,
4455 pIocbOut->iocb.un.fcpi.fcpi_parm);
4457 case IOSTAT_NPORT_BSY:
4458 case IOSTAT_FABRIC_BSY:
4459 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
4460 fast_path_evt = lpfc_alloc_fast_evt(phba);
4463 fast_path_evt->un.fabric_evt.event_type =
4464 FC_REG_FABRIC_EVENT;
4465 fast_path_evt->un.fabric_evt.subcategory =
4466 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4467 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4469 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4470 &pnode->nlp_portname,
4471 sizeof(struct lpfc_name));
4472 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4473 &pnode->nlp_nodename,
4474 sizeof(struct lpfc_name));
4476 fast_path_evt->vport = vport;
4477 fast_path_evt->work_evt.evt =
4478 LPFC_EVT_FASTPATH_MGMT_EVT;
4479 spin_lock_irqsave(&phba->hbalock, flags);
4480 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4482 spin_unlock_irqrestore(&phba->hbalock, flags);
4483 lpfc_worker_wake_up(phba);
4485 case IOSTAT_LOCAL_REJECT:
4486 case IOSTAT_REMOTE_STOP:
4487 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4489 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4490 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4492 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4493 cmd->result = DID_NO_CONNECT << 16;
4496 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
4497 lpfc_cmd->result == IOERR_NO_RESOURCES ||
4498 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4499 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
4500 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
4503 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4504 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4505 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4506 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4508 * This is a response for a BG enabled
4509 * cmd. Parse BG error
4511 lpfc_parse_bg_err(phba, lpfc_cmd,
4515 lpfc_printf_vlog(vport, KERN_WARNING,
4517 "9031 non-zero BGSTAT "
4518 "on unprotected cmd\n");
4521 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4522 && (phba->sli_rev == LPFC_SLI_REV4)
4524 /* This IO was aborted by the target, we don't
4525 * know the rxid and because we did not send the
4526 * ABTS we cannot generate and RRQ.
4528 lpfc_set_rrq_active(phba, pnode,
4529 lpfc_cmd->cur_iocbq.sli4_lxritag,
4534 cmd->result = DID_ERROR << 16;
4538 if (!pnode || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4539 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
4542 cmd->result = DID_OK << 16;
4544 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4545 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4547 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4548 "0710 Iodone <%d/%llu> cmd x%px, error "
4549 "x%x SNS x%x x%x Data: x%x x%x\n",
4550 cmd->device->id, cmd->device->lun, cmd,
4551 cmd->result, *lp, *(lp + 3), cmd->retries,
4552 scsi_get_resid(cmd));
4555 if (vport->cfg_max_scsicmpl_time &&
4556 time_after(jiffies, lpfc_cmd->start_time +
4557 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4558 spin_lock_irqsave(shost->host_lock, flags);
4560 if (pnode->cmd_qdepth >
4561 atomic_read(&pnode->cmd_pending) &&
4562 (atomic_read(&pnode->cmd_pending) >
4563 LPFC_MIN_TGT_QDEPTH) &&
4564 ((cmd->cmnd[0] == READ_10) ||
4565 (cmd->cmnd[0] == WRITE_10)))
4567 atomic_read(&pnode->cmd_pending);
4569 pnode->last_change_time = jiffies;
4571 spin_unlock_irqrestore(shost->host_lock, flags);
4573 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4575 lpfc_cmd->pCmd = NULL;
4576 spin_unlock(&lpfc_cmd->buf_lock);
4578 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4579 if (lpfc_cmd->ts_cmd_start) {
4580 lpfc_cmd->ts_isr_cmpl = pIocbIn->isr_timestamp;
4581 lpfc_cmd->ts_data_io = ktime_get_ns();
4582 phba->ktime_last_cmd = lpfc_cmd->ts_data_io;
4583 lpfc_io_ktime(phba, lpfc_cmd);
4587 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4591 * If there is an abort thread waiting for command completion
4592 * wake up the thread.
4594 spin_lock(&lpfc_cmd->buf_lock);
4595 lpfc_cmd->cur_iocbq.cmd_flag &= ~LPFC_DRIVER_ABORTED;
4596 if (lpfc_cmd->waitq)
4597 wake_up(lpfc_cmd->waitq);
4598 spin_unlock(&lpfc_cmd->buf_lock);
4600 lpfc_release_scsi_buf(phba, lpfc_cmd);
4604 * lpfc_scsi_prep_cmnd_buf_s3 - SLI-3 IOCB init for the IO
4605 * @vport: Pointer to vport object.
4606 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
4607 * @tmo: timeout value for the IO
4609 * Based on the data-direction of the command, initialize IOCB
4610 * in the I/O buffer. Fill in the IOCB fields which are independent
4611 * of the scsi buffer
4613 * RETURNS 0 - SUCCESS,
4615 static int lpfc_scsi_prep_cmnd_buf_s3(struct lpfc_vport *vport,
4616 struct lpfc_io_buf *lpfc_cmd,
4619 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4620 struct lpfc_iocbq *piocbq = &lpfc_cmd->cur_iocbq;
4621 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4622 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4623 struct lpfc_nodelist *pnode = lpfc_cmd->ndlp;
4624 int datadir = scsi_cmnd->sc_data_direction;
4627 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
4630 * There are three possibilities here - use scatter-gather segment, use
4631 * the single mapping, or neither. Start the lpfc command prep by
4632 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4635 if (scsi_sg_count(scsi_cmnd)) {
4636 if (datadir == DMA_TO_DEVICE) {
4637 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
4638 iocb_cmd->ulpPU = PARM_READ_CHECK;
4639 if (vport->cfg_first_burst_size &&
4640 (pnode->nlp_flag & NLP_FIRSTBURST)) {
4643 fcpdl = scsi_bufflen(scsi_cmnd);
4644 xrdy_len = min(fcpdl,
4645 vport->cfg_first_burst_size);
4646 piocbq->iocb.un.fcpi.fcpi_XRdy = xrdy_len;
4648 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4650 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4651 iocb_cmd->ulpPU = PARM_READ_CHECK;
4652 fcp_cmnd->fcpCntl3 = READ_DATA;
4655 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4656 iocb_cmd->un.fcpi.fcpi_parm = 0;
4657 iocb_cmd->ulpPU = 0;
4658 fcp_cmnd->fcpCntl3 = 0;
4662 * Finish initializing those IOCB fields that are independent
4663 * of the scsi_cmnd request_buffer
4665 piocbq->iocb.ulpContext = pnode->nlp_rpi;
4666 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4667 piocbq->iocb.ulpFCP2Rcvy = 1;
4669 piocbq->iocb.ulpFCP2Rcvy = 0;
4671 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4672 piocbq->io_buf = lpfc_cmd;
4673 if (!piocbq->cmd_cmpl)
4674 piocbq->cmd_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4675 piocbq->iocb.ulpTimeout = tmo;
4676 piocbq->vport = vport;
4681 * lpfc_scsi_prep_cmnd_buf_s4 - SLI-4 WQE init for the IO
4682 * @vport: Pointer to vport object.
4683 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
4684 * @tmo: timeout value for the IO
4686 * Based on the data-direction of the command copy WQE template
4687 * to I/O buffer WQE. Fill in the WQE fields which are independent
4688 * of the scsi buffer
4690 * RETURNS 0 - SUCCESS,
4692 static int lpfc_scsi_prep_cmnd_buf_s4(struct lpfc_vport *vport,
4693 struct lpfc_io_buf *lpfc_cmd,
4696 struct lpfc_hba *phba = vport->phba;
4697 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4698 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4699 struct lpfc_sli4_hdw_queue *hdwq = NULL;
4700 struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
4701 struct lpfc_nodelist *pnode = lpfc_cmd->ndlp;
4702 union lpfc_wqe128 *wqe = &pwqeq->wqe;
4703 u16 idx = lpfc_cmd->hdwq_no;
4704 int datadir = scsi_cmnd->sc_data_direction;
4706 hdwq = &phba->sli4_hba.hdwq[idx];
4708 /* Initialize 64 bytes only */
4709 memset(wqe, 0, sizeof(union lpfc_wqe128));
4712 * There are three possibilities here - use scatter-gather segment, use
4713 * the single mapping, or neither.
4715 if (scsi_sg_count(scsi_cmnd)) {
4716 if (datadir == DMA_TO_DEVICE) {
4717 /* From the iwrite template, initialize words 7 - 11 */
4718 memcpy(&wqe->words[7],
4719 &lpfc_iwrite_cmd_template.words[7],
4720 sizeof(uint32_t) * 5);
4722 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4724 hdwq->scsi_cstat.output_requests++;
4726 /* From the iread template, initialize words 7 - 11 */
4727 memcpy(&wqe->words[7],
4728 &lpfc_iread_cmd_template.words[7],
4729 sizeof(uint32_t) * 5);
4732 bf_set(wqe_tmo, &wqe->fcp_iread.wqe_com, tmo);
4734 fcp_cmnd->fcpCntl3 = READ_DATA;
4736 hdwq->scsi_cstat.input_requests++;
4738 /* For a CMF Managed port, iod must be zero'ed */
4739 if (phba->cmf_active_mode == LPFC_CFG_MANAGED)
4740 bf_set(wqe_iod, &wqe->fcp_iread.wqe_com,
4744 /* From the icmnd template, initialize words 4 - 11 */
4745 memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
4746 sizeof(uint32_t) * 8);
4749 bf_set(wqe_tmo, &wqe->fcp_icmd.wqe_com, tmo);
4751 fcp_cmnd->fcpCntl3 = 0;
4753 hdwq->scsi_cstat.control_requests++;
4757 * Finish initializing those WQE fields that are independent
4758 * of the request_buffer
4762 bf_set(payload_offset_len, &wqe->fcp_icmd,
4763 sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
4766 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
4767 phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
4768 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
4771 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4772 bf_set(wqe_erp, &wqe->generic.wqe_com, 1);
4774 bf_set(wqe_class, &wqe->generic.wqe_com,
4775 (pnode->nlp_fcp_info & 0x0f));
4778 wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
4781 bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
4783 pwqeq->vport = vport;
4784 pwqeq->io_buf = lpfc_cmd;
4785 pwqeq->hba_wqidx = lpfc_cmd->hdwq_no;
4786 pwqeq->cmd_cmpl = lpfc_fcp_io_cmd_wqe_cmpl;
4792 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
4793 * @vport: The virtual port for which this call is being executed.
4794 * @lpfc_cmd: The scsi command which needs to send.
4795 * @pnode: Pointer to lpfc_nodelist.
4797 * This routine initializes fcp_cmnd and iocb data structure from scsi command
4798 * to transfer for device with SLI3 interface spec.
4801 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
4802 struct lpfc_nodelist *pnode)
4804 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4805 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4811 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
4812 /* clear task management bits */
4813 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
4815 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4816 &lpfc_cmd->fcp_cmnd->fcp_lun);
4818 ptr = &fcp_cmnd->fcpCdb[0];
4819 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4820 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4821 ptr += scsi_cmnd->cmd_len;
4822 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4825 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4827 lpfc_scsi_prep_cmnd_buf(vport, lpfc_cmd, lpfc_cmd->timeout);
4833 * lpfc_scsi_prep_task_mgmt_cmd_s3 - Convert SLI3 scsi TM cmd to FCP info unit
4834 * @vport: The virtual port for which this call is being executed.
4835 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
4836 * @lun: Logical unit number.
4837 * @task_mgmt_cmd: SCSI task management command.
4839 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4840 * for device with SLI-3 interface spec.
4847 lpfc_scsi_prep_task_mgmt_cmd_s3(struct lpfc_vport *vport,
4848 struct lpfc_io_buf *lpfc_cmd,
4849 u64 lun, u8 task_mgmt_cmd)
4851 struct lpfc_iocbq *piocbq;
4853 struct fcp_cmnd *fcp_cmnd;
4854 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4855 struct lpfc_nodelist *ndlp = rdata->pnode;
4857 if (!ndlp || ndlp->nlp_state != NLP_STE_MAPPED_NODE)
4860 piocbq = &(lpfc_cmd->cur_iocbq);
4861 piocbq->vport = vport;
4863 piocb = &piocbq->iocb;
4865 fcp_cmnd = lpfc_cmd->fcp_cmnd;
4866 /* Clear out any old data in the FCP command area */
4867 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4868 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
4869 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
4870 if (!(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4871 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
4872 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
4873 piocb->ulpContext = ndlp->nlp_rpi;
4874 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
4875 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4877 piocb->un.fcpi.fcpi_parm = 0;
4879 /* ulpTimeout is only one byte */
4880 if (lpfc_cmd->timeout > 0xff) {
4882 * Do not timeout the command at the firmware level.
4883 * The driver will provide the timeout mechanism.
4885 piocb->ulpTimeout = 0;
4887 piocb->ulpTimeout = lpfc_cmd->timeout;
4893 * lpfc_scsi_prep_task_mgmt_cmd_s4 - Convert SLI4 scsi TM cmd to FCP info unit
4894 * @vport: The virtual port for which this call is being executed.
4895 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
4896 * @lun: Logical unit number.
4897 * @task_mgmt_cmd: SCSI task management command.
4899 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4900 * for device with SLI-4 interface spec.
4907 lpfc_scsi_prep_task_mgmt_cmd_s4(struct lpfc_vport *vport,
4908 struct lpfc_io_buf *lpfc_cmd,
4909 u64 lun, u8 task_mgmt_cmd)
4911 struct lpfc_iocbq *pwqeq = &lpfc_cmd->cur_iocbq;
4912 union lpfc_wqe128 *wqe = &pwqeq->wqe;
4913 struct fcp_cmnd *fcp_cmnd;
4914 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4915 struct lpfc_nodelist *ndlp = rdata->pnode;
4917 if (!ndlp || ndlp->nlp_state != NLP_STE_MAPPED_NODE)
4920 pwqeq->vport = vport;
4921 /* Initialize 64 bytes only */
4922 memset(wqe, 0, sizeof(union lpfc_wqe128));
4924 /* From the icmnd template, initialize words 4 - 11 */
4925 memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
4926 sizeof(uint32_t) * 8);
4928 fcp_cmnd = lpfc_cmd->fcp_cmnd;
4929 /* Clear out any old data in the FCP command area */
4930 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4931 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
4932 fcp_cmnd->fcpCntl3 = 0;
4933 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
4935 bf_set(payload_offset_len, &wqe->fcp_icmd,
4936 sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
4937 bf_set(cmd_buff_len, &wqe->fcp_icmd, 0);
4938 bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com, /* ulpContext */
4939 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
4940 bf_set(wqe_erp, &wqe->fcp_icmd.wqe_com,
4941 ((ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0));
4942 bf_set(wqe_class, &wqe->fcp_icmd.wqe_com,
4943 (ndlp->nlp_fcp_info & 0x0f));
4945 /* ulpTimeout is only one byte */
4946 if (lpfc_cmd->timeout > 0xff) {
4948 * Do not timeout the command at the firmware level.
4949 * The driver will provide the timeout mechanism.
4951 bf_set(wqe_tmo, &wqe->fcp_icmd.wqe_com, 0);
4953 bf_set(wqe_tmo, &wqe->fcp_icmd.wqe_com, lpfc_cmd->timeout);
4956 lpfc_prep_embed_io(vport->phba, lpfc_cmd);
4957 bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
4958 wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
4959 bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
4961 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
4967 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
4968 * @phba: The hba struct for which this call is being executed.
4969 * @dev_grp: The HBA PCI-Device group number.
4971 * This routine sets up the SCSI interface API function jump table in @phba
4973 * Returns: 0 - success, -ENODEV - failure.
4976 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4979 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4982 case LPFC_PCI_DEV_LP:
4983 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
4984 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
4985 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
4986 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
4987 phba->lpfc_scsi_prep_cmnd_buf = lpfc_scsi_prep_cmnd_buf_s3;
4988 phba->lpfc_scsi_prep_task_mgmt_cmd =
4989 lpfc_scsi_prep_task_mgmt_cmd_s3;
4991 case LPFC_PCI_DEV_OC:
4992 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
4993 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
4994 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
4995 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
4996 phba->lpfc_scsi_prep_cmnd_buf = lpfc_scsi_prep_cmnd_buf_s4;
4997 phba->lpfc_scsi_prep_task_mgmt_cmd =
4998 lpfc_scsi_prep_task_mgmt_cmd_s4;
5001 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
5002 "1418 Invalid HBA PCI-device group: 0x%x\n",
5006 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
5011 * lpfc_tskmgmt_def_cmpl - IOCB completion routine for task management command
5012 * @phba: The Hba for which this call is being executed.
5013 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
5014 * @rspiocbq: Pointer to lpfc_iocbq data structure.
5016 * This routine is IOCB completion routine for device reset and target reset
5017 * routine. This routine release scsi buffer associated with lpfc_cmd.
5020 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
5021 struct lpfc_iocbq *cmdiocbq,
5022 struct lpfc_iocbq *rspiocbq)
5024 struct lpfc_io_buf *lpfc_cmd = cmdiocbq->io_buf;
5026 lpfc_release_scsi_buf(phba, lpfc_cmd);
5031 * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
5032 * if issuing a pci_bus_reset is possibly unsafe
5033 * @phba: lpfc_hba pointer.
5036 * Walks the bus_list to ensure only PCI devices with Emulex
5037 * vendor id, device ids that support hot reset, and only one occurrence
5041 * -EBADSLT, detected invalid device
5045 lpfc_check_pci_resettable(struct lpfc_hba *phba)
5047 const struct pci_dev *pdev = phba->pcidev;
5048 struct pci_dev *ptr = NULL;
5051 /* Walk the list of devices on the pci_dev's bus */
5052 list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
5053 /* Check for Emulex Vendor ID */
5054 if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
5055 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5056 "8346 Non-Emulex vendor found: "
5057 "0x%04x\n", ptr->vendor);
5061 /* Check for valid Emulex Device ID */
5062 if (phba->sli_rev != LPFC_SLI_REV4 ||
5063 phba->hba_flag & HBA_FCOE_MODE) {
5064 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5065 "8347 Incapable PCI reset device: "
5066 "0x%04x\n", ptr->device);
5070 /* Check for only one function 0 ID to ensure only one HBA on
5073 if (ptr->devfn == 0) {
5074 if (++counter > 1) {
5075 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
5076 "8348 More than one device on "
5077 "secondary bus found\n");
5087 * lpfc_info - Info entry point of scsi_host_template data structure
5088 * @host: The scsi host for which this call is being executed.
5090 * This routine provides module information about hba.
5093 * Pointer to char - Success.
5096 lpfc_info(struct Scsi_Host *host)
5098 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
5099 struct lpfc_hba *phba = vport->phba;
5101 static char lpfcinfobuf[384];
5102 char tmp[384] = {0};
5104 memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
5105 if (phba && phba->pcidev){
5106 /* Model Description */
5107 scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
5108 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5109 sizeof(lpfcinfobuf))
5113 scnprintf(tmp, sizeof(tmp),
5114 " on PCI bus %02x device %02x irq %d",
5115 phba->pcidev->bus->number, phba->pcidev->devfn,
5117 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5118 sizeof(lpfcinfobuf))
5122 if (phba->Port[0]) {
5123 scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
5124 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5125 sizeof(lpfcinfobuf))
5130 link_speed = lpfc_sli_port_speed_get(phba);
5131 if (link_speed != 0) {
5132 scnprintf(tmp, sizeof(tmp),
5133 " Logical Link Speed: %d Mbps", link_speed);
5134 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
5135 sizeof(lpfcinfobuf))
5139 /* PCI resettable */
5140 if (!lpfc_check_pci_resettable(phba)) {
5141 scnprintf(tmp, sizeof(tmp), " PCI resettable");
5142 strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
5151 * lpfc_poll_rearm_timer - Routine to modify fcp_poll timer of hba
5152 * @phba: The Hba for which this call is being executed.
5154 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
5155 * The default value of cfg_poll_tmo is 10 milliseconds.
5157 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
5159 unsigned long poll_tmo_expires =
5160 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
5162 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
5163 mod_timer(&phba->fcp_poll_timer,
5168 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
5169 * @phba: The Hba for which this call is being executed.
5171 * This routine starts the fcp_poll_timer of @phba.
5173 void lpfc_poll_start_timer(struct lpfc_hba * phba)
5175 lpfc_poll_rearm_timer(phba);
5179 * lpfc_poll_timeout - Restart polling timer
5180 * @t: Timer construct where lpfc_hba data structure pointer is obtained.
5182 * This routine restarts fcp_poll timer, when FCP ring polling is enable
5183 * and FCP Ring interrupt is disable.
5185 void lpfc_poll_timeout(struct timer_list *t)
5187 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
5189 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5190 lpfc_sli_handle_fast_ring_event(phba,
5191 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
5193 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5194 lpfc_poll_rearm_timer(phba);
5199 * lpfc_is_command_vm_io - get the UUID from blk cgroup
5200 * @cmd: Pointer to scsi_cmnd data structure
5201 * Returns UUID if present, otherwise NULL
5203 static char *lpfc_is_command_vm_io(struct scsi_cmnd *cmd)
5205 struct bio *bio = scsi_cmd_to_rq(cmd)->bio;
5207 if (!IS_ENABLED(CONFIG_BLK_CGROUP_FC_APPID) || !bio)
5209 return blkcg_get_fc_appid(bio);
5213 * lpfc_queuecommand - scsi_host_template queuecommand entry point
5214 * @shost: kernel scsi host pointer.
5215 * @cmnd: Pointer to scsi_cmnd data structure.
5217 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
5218 * This routine prepares an IOCB from scsi command and provides to firmware.
5219 * The @done callback is invoked after driver finished processing the command.
5223 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
5226 lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
5228 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5229 struct lpfc_hba *phba = vport->phba;
5230 struct lpfc_iocbq *cur_iocbq = NULL;
5231 struct lpfc_rport_data *rdata;
5232 struct lpfc_nodelist *ndlp;
5233 struct lpfc_io_buf *lpfc_cmd;
5234 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
5239 start = ktime_get_ns();
5240 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5242 /* sanity check on references */
5243 if (unlikely(!rdata) || unlikely(!rport))
5244 goto out_fail_command;
5246 err = fc_remote_port_chkready(rport);
5249 goto out_fail_command;
5251 ndlp = rdata->pnode;
5253 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
5254 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
5256 lpfc_printf_log(phba, KERN_ERR, LOG_TRACE_EVENT,
5257 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
5258 " op:%02x str=%s without registering for"
5259 " BlockGuard - Rejecting command\n",
5260 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
5261 dif_op_str[scsi_get_prot_op(cmnd)]);
5262 goto out_fail_command;
5266 * Catch race where our node has transitioned, but the
5267 * transport is still transitioning.
5272 /* Check if IO qualifies for CMF */
5273 if (phba->cmf_active_mode != LPFC_CFG_OFF &&
5274 cmnd->sc_data_direction == DMA_FROM_DEVICE &&
5275 (scsi_sg_count(cmnd))) {
5276 /* Latency start time saved in rx_cmd_start later in routine */
5277 err = lpfc_update_cmf_cmd(phba, scsi_bufflen(cmnd));
5282 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
5283 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
5284 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
5285 "3377 Target Queue Full, scsi Id:%d "
5286 "Qdepth:%d Pending command:%d"
5287 " WWNN:%02x:%02x:%02x:%02x:"
5288 "%02x:%02x:%02x:%02x, "
5289 " WWPN:%02x:%02x:%02x:%02x:"
5290 "%02x:%02x:%02x:%02x",
5291 ndlp->nlp_sid, ndlp->cmd_qdepth,
5292 atomic_read(&ndlp->cmd_pending),
5293 ndlp->nlp_nodename.u.wwn[0],
5294 ndlp->nlp_nodename.u.wwn[1],
5295 ndlp->nlp_nodename.u.wwn[2],
5296 ndlp->nlp_nodename.u.wwn[3],
5297 ndlp->nlp_nodename.u.wwn[4],
5298 ndlp->nlp_nodename.u.wwn[5],
5299 ndlp->nlp_nodename.u.wwn[6],
5300 ndlp->nlp_nodename.u.wwn[7],
5301 ndlp->nlp_portname.u.wwn[0],
5302 ndlp->nlp_portname.u.wwn[1],
5303 ndlp->nlp_portname.u.wwn[2],
5304 ndlp->nlp_portname.u.wwn[3],
5305 ndlp->nlp_portname.u.wwn[4],
5306 ndlp->nlp_portname.u.wwn[5],
5307 ndlp->nlp_portname.u.wwn[6],
5308 ndlp->nlp_portname.u.wwn[7]);
5313 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp, cmnd);
5314 if (lpfc_cmd == NULL) {
5315 lpfc_rampdown_queue_depth(phba);
5317 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
5318 "0707 driver's buffer pool is empty, "
5322 lpfc_cmd->rx_cmd_start = start;
5324 cur_iocbq = &lpfc_cmd->cur_iocbq;
5326 * Store the midlayer's command structure for the completion phase
5327 * and complete the command initialization.
5329 lpfc_cmd->pCmd = cmnd;
5330 lpfc_cmd->rdata = rdata;
5331 lpfc_cmd->ndlp = ndlp;
5332 cur_iocbq->cmd_cmpl = NULL;
5333 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
5335 err = lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
5337 goto out_host_busy_release_buf;
5339 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
5340 if (vport->phba->cfg_enable_bg) {
5341 lpfc_printf_vlog(vport,
5342 KERN_INFO, LOG_SCSI_CMD,
5343 "9033 BLKGRD: rcvd %s cmd:x%x "
5344 "reftag x%x cnt %u pt %x\n",
5345 dif_op_str[scsi_get_prot_op(cmnd)],
5347 scsi_prot_ref_tag(cmnd),
5348 scsi_logical_block_count(cmnd),
5349 (cmnd->cmnd[1]>>5));
5351 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
5353 if (vport->phba->cfg_enable_bg) {
5354 lpfc_printf_vlog(vport,
5355 KERN_INFO, LOG_SCSI_CMD,
5356 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
5357 "x%x reftag x%x cnt %u pt %x\n",
5359 scsi_prot_ref_tag(cmnd),
5360 scsi_logical_block_count(cmnd),
5361 (cmnd->cmnd[1]>>5));
5363 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
5366 if (unlikely(err)) {
5368 cmnd->result = DID_ERROR << 16;
5369 goto out_fail_command_release_buf;
5371 goto out_host_busy_free_buf;
5374 /* check the necessary and sufficient condition to support VMID */
5375 if (lpfc_is_vmid_enabled(phba) &&
5376 (ndlp->vmid_support ||
5377 phba->pport->vmid_priority_tagging ==
5378 LPFC_VMID_PRIO_TAG_ALL_TARGETS)) {
5379 /* is the I/O generated by a VM, get the associated virtual */
5381 uuid = lpfc_is_command_vm_io(cmnd);
5384 err = lpfc_vmid_get_appid(vport, uuid,
5385 cmnd->sc_data_direction,
5386 (union lpfc_vmid_io_tag *)
5387 &cur_iocbq->vmid_tag);
5389 cur_iocbq->cmd_flag |= LPFC_IO_VMID;
5393 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5394 if (unlikely(phba->hdwqstat_on & LPFC_CHECK_SCSI_IO))
5395 this_cpu_inc(phba->sli4_hba.c_stat->xmt_io);
5397 /* Issue I/O to adapter */
5398 err = lpfc_sli_issue_fcp_io(phba, LPFC_FCP_RING, cur_iocbq,
5400 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
5402 lpfc_cmd->ts_cmd_start = start;
5403 lpfc_cmd->ts_last_cmd = phba->ktime_last_cmd;
5404 lpfc_cmd->ts_cmd_wqput = ktime_get_ns();
5406 lpfc_cmd->ts_cmd_start = 0;
5410 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5411 "3376 FCP could not issue iocb err %x "
5412 "FCP cmd x%x <%d/%llu> "
5413 "sid: x%x did: x%x oxid: x%x "
5414 "Data: x%x x%x x%x x%x\n",
5416 cmnd->device ? cmnd->device->id : 0xffff,
5417 cmnd->device ? cmnd->device->lun : (u64)-1,
5418 vport->fc_myDID, ndlp->nlp_DID,
5419 phba->sli_rev == LPFC_SLI_REV4 ?
5420 cur_iocbq->sli4_xritag : 0xffff,
5421 phba->sli_rev == LPFC_SLI_REV4 ?
5422 phba->sli4_hba.rpi_ids[ndlp->nlp_rpi] :
5423 cur_iocbq->iocb.ulpContext,
5425 phba->sli_rev == LPFC_SLI_REV4 ?
5427 &cur_iocbq->wqe.generic.wqe_com) :
5428 cur_iocbq->iocb.ulpTimeout,
5429 (uint32_t)(scsi_cmd_to_rq(cmnd)->timeout / 1000));
5431 goto out_host_busy_free_buf;
5434 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5435 lpfc_sli_handle_fast_ring_event(phba,
5436 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
5438 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5439 lpfc_poll_rearm_timer(phba);
5442 if (phba->cfg_xri_rebalancing)
5443 lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_cmd->hdwq_no);
5447 out_host_busy_free_buf:
5448 idx = lpfc_cmd->hdwq_no;
5449 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
5450 if (phba->sli4_hba.hdwq) {
5451 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
5453 phba->sli4_hba.hdwq[idx].scsi_cstat.output_requests--;
5456 phba->sli4_hba.hdwq[idx].scsi_cstat.input_requests--;
5459 phba->sli4_hba.hdwq[idx].scsi_cstat.control_requests--;
5462 out_host_busy_release_buf:
5463 lpfc_release_scsi_buf(phba, lpfc_cmd);
5465 lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
5467 return SCSI_MLQUEUE_HOST_BUSY;
5470 lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
5473 return SCSI_MLQUEUE_TARGET_BUSY;
5475 out_fail_command_release_buf:
5476 lpfc_release_scsi_buf(phba, lpfc_cmd);
5477 lpfc_update_cmf_cmpl(phba, LPFC_CGN_NOT_SENT, scsi_bufflen(cmnd),
5486 * lpfc_vmid_vport_cleanup - cleans up the resources associated with a vport
5487 * @vport: The virtual port for which this call is being executed.
5489 void lpfc_vmid_vport_cleanup(struct lpfc_vport *vport)
5492 struct lpfc_vmid *cur;
5494 if (vport->port_type == LPFC_PHYSICAL_PORT)
5495 del_timer_sync(&vport->phba->inactive_vmid_poll);
5497 kfree(vport->qfpa_res);
5498 kfree(vport->vmid_priority.vmid_range);
5501 if (!hash_empty(vport->hash_table))
5502 hash_for_each(vport->hash_table, bucket, cur, hnode)
5503 hash_del(&cur->hnode);
5505 vport->qfpa_res = NULL;
5506 vport->vmid_priority.vmid_range = NULL;
5508 vport->cur_vmid_cnt = 0;
5512 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
5513 * @cmnd: Pointer to scsi_cmnd data structure.
5515 * This routine aborts @cmnd pending in base driver.
5522 lpfc_abort_handler(struct scsi_cmnd *cmnd)
5524 struct Scsi_Host *shost = cmnd->device->host;
5525 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
5526 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5527 struct lpfc_hba *phba = vport->phba;
5528 struct lpfc_iocbq *iocb;
5529 struct lpfc_io_buf *lpfc_cmd;
5530 int ret = SUCCESS, status = 0;
5531 struct lpfc_sli_ring *pring_s4 = NULL;
5532 struct lpfc_sli_ring *pring = NULL;
5534 unsigned long flags;
5535 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
5537 status = fc_block_rport(rport);
5538 if (status != 0 && status != SUCCESS)
5541 lpfc_cmd = (struct lpfc_io_buf *)cmnd->host_scribble;
5545 /* Guard against IO completion being called at same time */
5546 spin_lock_irqsave(&lpfc_cmd->buf_lock, flags);
5548 spin_lock(&phba->hbalock);
5549 /* driver queued commands are in process of being flushed */
5550 if (phba->hba_flag & HBA_IOQ_FLUSH) {
5551 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5552 "3168 SCSI Layer abort requested I/O has been "
5553 "flushed by LLD.\n");
5555 goto out_unlock_hba;
5558 if (!lpfc_cmd->pCmd) {
5559 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5560 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
5561 "x%x ID %d LUN %llu\n",
5562 SUCCESS, cmnd->device->id, cmnd->device->lun);
5563 goto out_unlock_hba;
5566 iocb = &lpfc_cmd->cur_iocbq;
5567 if (phba->sli_rev == LPFC_SLI_REV4) {
5568 pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].io_wq->pring;
5571 goto out_unlock_hba;
5573 spin_lock(&pring_s4->ring_lock);
5575 /* the command is in process of being cancelled */
5576 if (!(iocb->cmd_flag & LPFC_IO_ON_TXCMPLQ)) {
5577 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5578 "3169 SCSI Layer abort requested I/O has been "
5579 "cancelled by LLD.\n");
5581 goto out_unlock_ring;
5584 * If pCmd field of the corresponding lpfc_io_buf structure
5585 * points to a different SCSI command, then the driver has
5586 * already completed this command, but the midlayer did not
5587 * see the completion before the eh fired. Just return SUCCESS.
5589 if (lpfc_cmd->pCmd != cmnd) {
5590 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5591 "3170 SCSI Layer abort requested I/O has been "
5592 "completed by LLD.\n");
5593 goto out_unlock_ring;
5596 WARN_ON(iocb->io_buf != lpfc_cmd);
5598 /* abort issued in recovery is still in progress */
5599 if (iocb->cmd_flag & LPFC_DRIVER_ABORTED) {
5600 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5601 "3389 SCSI Layer I/O Abort Request is pending\n");
5602 if (phba->sli_rev == LPFC_SLI_REV4)
5603 spin_unlock(&pring_s4->ring_lock);
5604 spin_unlock(&phba->hbalock);
5605 spin_unlock_irqrestore(&lpfc_cmd->buf_lock, flags);
5609 lpfc_cmd->waitq = &waitq;
5610 if (phba->sli_rev == LPFC_SLI_REV4) {
5611 spin_unlock(&pring_s4->ring_lock);
5612 ret_val = lpfc_sli4_issue_abort_iotag(phba, iocb,
5613 lpfc_sli_abort_fcp_cmpl);
5615 pring = &phba->sli.sli3_ring[LPFC_FCP_RING];
5616 ret_val = lpfc_sli_issue_abort_iotag(phba, pring, iocb,
5617 lpfc_sli_abort_fcp_cmpl);
5620 /* Make sure HBA is alive */
5621 lpfc_issue_hb_tmo(phba);
5623 if (ret_val != IOCB_SUCCESS) {
5624 /* Indicate the IO is not being aborted by the driver. */
5625 lpfc_cmd->waitq = NULL;
5627 goto out_unlock_hba;
5630 /* no longer need the lock after this point */
5631 spin_unlock(&phba->hbalock);
5632 spin_unlock_irqrestore(&lpfc_cmd->buf_lock, flags);
5634 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5635 lpfc_sli_handle_fast_ring_event(phba,
5636 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
5640 * cmd_flag is set to LPFC_DRIVER_ABORTED before we wait
5641 * for abort to complete.
5643 wait_event_timeout(waitq,
5644 (lpfc_cmd->pCmd != cmnd),
5645 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
5647 spin_lock(&lpfc_cmd->buf_lock);
5649 if (lpfc_cmd->pCmd == cmnd) {
5651 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5652 "0748 abort handler timed out waiting "
5653 "for aborting I/O (xri:x%x) to complete: "
5654 "ret %#x, ID %d, LUN %llu\n",
5655 iocb->sli4_xritag, ret,
5656 cmnd->device->id, cmnd->device->lun);
5659 lpfc_cmd->waitq = NULL;
5661 spin_unlock(&lpfc_cmd->buf_lock);
5665 if (phba->sli_rev == LPFC_SLI_REV4)
5666 spin_unlock(&pring_s4->ring_lock);
5668 spin_unlock(&phba->hbalock);
5669 spin_unlock_irqrestore(&lpfc_cmd->buf_lock, flags);
5671 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5672 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
5673 "LUN %llu\n", ret, cmnd->device->id,
5679 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
5681 switch (task_mgmt_cmd) {
5682 case FCP_ABORT_TASK_SET:
5683 return "ABORT_TASK_SET";
5684 case FCP_CLEAR_TASK_SET:
5685 return "FCP_CLEAR_TASK_SET";
5687 return "FCP_BUS_RESET";
5689 return "FCP_LUN_RESET";
5690 case FCP_TARGET_RESET:
5691 return "FCP_TARGET_RESET";
5693 return "FCP_CLEAR_ACA";
5694 case FCP_TERMINATE_TASK:
5695 return "FCP_TERMINATE_TASK";
5703 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
5704 * @vport: The virtual port for which this call is being executed.
5705 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
5707 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
5714 lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
5716 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
5719 uint8_t rsp_info_code;
5724 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5725 "0703 fcp_rsp is missing\n");
5727 rsp_info = fcprsp->rspStatus2;
5728 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
5729 rsp_info_code = fcprsp->rspInfo3;
5732 lpfc_printf_vlog(vport, KERN_INFO,
5734 "0706 fcp_rsp valid 0x%x,"
5735 " rsp len=%d code 0x%x\n",
5737 rsp_len, rsp_info_code);
5739 /* If FCP_RSP_LEN_VALID bit is one, then the FCP_RSP_LEN
5740 * field specifies the number of valid bytes of FCP_RSP_INFO.
5741 * The FCP_RSP_LEN field shall be set to 0x04 or 0x08
5743 if ((fcprsp->rspStatus2 & RSP_LEN_VALID) &&
5744 ((rsp_len == 8) || (rsp_len == 4))) {
5745 switch (rsp_info_code) {
5746 case RSP_NO_FAILURE:
5747 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5748 "0715 Task Mgmt No Failure\n");
5751 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
5752 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5753 "0716 Task Mgmt Target "
5756 case RSP_TM_NOT_COMPLETED: /* TM failed */
5757 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5758 "0717 Task Mgmt Target "
5761 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5762 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5763 "0718 Task Mgmt to invalid "
5774 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5775 * @vport: The virtual port for which this call is being executed.
5776 * @rport: Pointer to remote port
5777 * @tgt_id: Target ID of remote device.
5778 * @lun_id: Lun number for the TMF
5779 * @task_mgmt_cmd: type of TMF to send
5781 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5789 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct fc_rport *rport,
5790 unsigned int tgt_id, uint64_t lun_id,
5791 uint8_t task_mgmt_cmd)
5793 struct lpfc_hba *phba = vport->phba;
5794 struct lpfc_io_buf *lpfc_cmd;
5795 struct lpfc_iocbq *iocbq;
5796 struct lpfc_iocbq *iocbqrsp;
5797 struct lpfc_rport_data *rdata;
5798 struct lpfc_nodelist *pnode;
5802 rdata = rport->dd_data;
5803 if (!rdata || !rdata->pnode)
5805 pnode = rdata->pnode;
5807 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode, NULL);
5808 if (lpfc_cmd == NULL)
5810 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
5811 lpfc_cmd->rdata = rdata;
5812 lpfc_cmd->pCmd = NULL;
5813 lpfc_cmd->ndlp = pnode;
5815 status = phba->lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5818 lpfc_release_scsi_buf(phba, lpfc_cmd);
5822 iocbq = &lpfc_cmd->cur_iocbq;
5823 iocbqrsp = lpfc_sli_get_iocbq(phba);
5824 if (iocbqrsp == NULL) {
5825 lpfc_release_scsi_buf(phba, lpfc_cmd);
5828 iocbq->cmd_cmpl = lpfc_tskmgmt_def_cmpl;
5829 iocbq->vport = vport;
5831 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5832 "0702 Issue %s to TGT %d LUN %llu "
5833 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
5834 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
5835 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5838 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5839 iocbq, iocbqrsp, lpfc_cmd->timeout);
5840 if ((status != IOCB_SUCCESS) ||
5841 (get_job_ulpstatus(phba, iocbqrsp) != IOSTAT_SUCCESS)) {
5842 if (status != IOCB_SUCCESS ||
5843 get_job_ulpstatus(phba, iocbqrsp) != IOSTAT_FCP_RSP_ERROR)
5844 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5845 "0727 TMF %s to TGT %d LUN %llu "
5846 "failed (%d, %d) cmd_flag x%x\n",
5847 lpfc_taskmgmt_name(task_mgmt_cmd),
5849 get_job_ulpstatus(phba, iocbqrsp),
5850 get_job_word4(phba, iocbqrsp),
5852 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5853 if (status == IOCB_SUCCESS) {
5854 if (get_job_ulpstatus(phba, iocbqrsp) ==
5855 IOSTAT_FCP_RSP_ERROR)
5856 /* Something in the FCP_RSP was invalid.
5857 * Check conditions */
5858 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5861 } else if ((status == IOCB_TIMEDOUT) ||
5862 (status == IOCB_ABORTED)) {
5863 ret = TIMEOUT_ERROR;
5870 lpfc_sli_release_iocbq(phba, iocbqrsp);
5872 if (status != IOCB_TIMEDOUT)
5873 lpfc_release_scsi_buf(phba, lpfc_cmd);
5879 * lpfc_chk_tgt_mapped -
5880 * @vport: The virtual port to check on
5881 * @rport: Pointer to fc_rport data structure.
5883 * This routine delays until the scsi target (aka rport) for the
5884 * command exists (is present and logged in) or we declare it non-existent.
5891 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct fc_rport *rport)
5893 struct lpfc_rport_data *rdata;
5894 struct lpfc_nodelist *pnode = NULL;
5895 unsigned long later;
5897 rdata = rport->dd_data;
5899 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5900 "0797 Tgt Map rport failure: rdata x%px\n", rdata);
5903 pnode = rdata->pnode;
5906 * If target is not in a MAPPED state, delay until
5907 * target is rediscovered or devloss timeout expires.
5909 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5910 while (time_after(later, jiffies)) {
5913 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5915 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5916 rdata = rport->dd_data;
5919 pnode = rdata->pnode;
5921 if (!pnode || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5927 * lpfc_reset_flush_io_context -
5928 * @vport: The virtual port (scsi_host) for the flush context
5929 * @tgt_id: If aborting by Target contect - specifies the target id
5930 * @lun_id: If aborting by Lun context - specifies the lun id
5931 * @context: specifies the context level to flush at.
5933 * After a reset condition via TMF, we need to flush orphaned i/o
5934 * contexts from the adapter. This routine aborts any contexts
5935 * outstanding, then waits for their completions. The wait is
5936 * bounded by devloss_tmo though.
5943 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5944 uint64_t lun_id, lpfc_ctx_cmd context)
5946 struct lpfc_hba *phba = vport->phba;
5947 unsigned long later;
5950 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5952 lpfc_sli_abort_taskmgmt(vport,
5953 &phba->sli.sli3_ring[LPFC_FCP_RING],
5954 tgt_id, lun_id, context);
5955 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5956 while (time_after(later, jiffies) && cnt) {
5957 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5958 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5961 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
5962 "0724 I/O flush failure for context %s : cnt x%x\n",
5963 ((context == LPFC_CTX_LUN) ? "LUN" :
5964 ((context == LPFC_CTX_TGT) ? "TGT" :
5965 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5973 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
5974 * @cmnd: Pointer to scsi_cmnd data structure.
5976 * This routine does a device reset by sending a LUN_RESET task management
5984 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
5986 struct Scsi_Host *shost = cmnd->device->host;
5987 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
5988 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5989 struct lpfc_rport_data *rdata;
5990 struct lpfc_nodelist *pnode;
5991 unsigned tgt_id = cmnd->device->id;
5992 uint64_t lun_id = cmnd->device->lun;
5993 struct lpfc_scsi_event_header scsi_event;
5995 u32 logit = LOG_FCP;
6000 rdata = rport->dd_data;
6001 if (!rdata || !rdata->pnode) {
6002 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6003 "0798 Device Reset rdata failure: rdata x%px\n",
6007 pnode = rdata->pnode;
6008 status = fc_block_rport(rport);
6009 if (status != 0 && status != SUCCESS)
6012 status = lpfc_chk_tgt_mapped(vport, rport);
6013 if (status == FAILED) {
6014 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6015 "0721 Device Reset rport failure: rdata x%px\n", rdata);
6019 scsi_event.event_type = FC_REG_SCSI_EVENT;
6020 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
6021 scsi_event.lun = lun_id;
6022 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
6023 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
6025 fc_host_post_vendor_event(shost, fc_get_event_number(),
6026 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
6028 status = lpfc_send_taskmgmt(vport, rport, tgt_id, lun_id,
6030 if (status != SUCCESS)
6031 logit = LOG_TRACE_EVENT;
6033 lpfc_printf_vlog(vport, KERN_ERR, logit,
6034 "0713 SCSI layer issued Device Reset (%d, %llu) "
6035 "return x%x\n", tgt_id, lun_id, status);
6038 * We have to clean up i/o as : they may be orphaned by the TMF;
6039 * or if the TMF failed, they may be in an indeterminate state.
6041 * We will report success if all the i/o aborts successfully.
6043 if (status == SUCCESS)
6044 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
6051 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
6052 * @cmnd: Pointer to scsi_cmnd data structure.
6054 * This routine does a target reset by sending a TARGET_RESET task management
6062 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
6064 struct Scsi_Host *shost = cmnd->device->host;
6065 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
6066 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6067 struct lpfc_rport_data *rdata;
6068 struct lpfc_nodelist *pnode;
6069 unsigned tgt_id = cmnd->device->id;
6070 uint64_t lun_id = cmnd->device->lun;
6071 struct lpfc_scsi_event_header scsi_event;
6073 u32 logit = LOG_FCP;
6074 u32 dev_loss_tmo = vport->cfg_devloss_tmo;
6075 unsigned long flags;
6076 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
6081 rdata = rport->dd_data;
6082 if (!rdata || !rdata->pnode) {
6083 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6084 "0799 Target Reset rdata failure: rdata x%px\n",
6088 pnode = rdata->pnode;
6089 status = fc_block_rport(rport);
6090 if (status != 0 && status != SUCCESS)
6093 status = lpfc_chk_tgt_mapped(vport, rport);
6094 if (status == FAILED) {
6095 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6096 "0722 Target Reset rport failure: rdata x%px\n", rdata);
6098 spin_lock_irqsave(&pnode->lock, flags);
6099 pnode->nlp_flag &= ~NLP_NPR_ADISC;
6100 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
6101 spin_unlock_irqrestore(&pnode->lock, flags);
6103 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
6105 return FAST_IO_FAIL;
6108 scsi_event.event_type = FC_REG_SCSI_EVENT;
6109 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
6111 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
6112 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
6114 fc_host_post_vendor_event(shost, fc_get_event_number(),
6115 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
6117 status = lpfc_send_taskmgmt(vport, rport, tgt_id, lun_id,
6119 if (status != SUCCESS) {
6120 logit = LOG_TRACE_EVENT;
6122 /* Issue LOGO, if no LOGO is outstanding */
6123 spin_lock_irqsave(&pnode->lock, flags);
6124 if (!(pnode->save_flags & NLP_WAIT_FOR_LOGO) &&
6125 !pnode->logo_waitq) {
6126 pnode->logo_waitq = &waitq;
6127 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
6128 pnode->nlp_flag |= NLP_ISSUE_LOGO;
6129 pnode->save_flags |= NLP_WAIT_FOR_LOGO;
6130 spin_unlock_irqrestore(&pnode->lock, flags);
6131 lpfc_unreg_rpi(vport, pnode);
6132 wait_event_timeout(waitq,
6133 (!(pnode->save_flags &
6134 NLP_WAIT_FOR_LOGO)),
6135 msecs_to_jiffies(dev_loss_tmo *
6138 if (pnode->save_flags & NLP_WAIT_FOR_LOGO) {
6139 lpfc_printf_vlog(vport, KERN_ERR, logit,
6140 "0725 SCSI layer TGTRST "
6141 "failed & LOGO TMO (%d, %llu) "
6143 tgt_id, lun_id, status);
6144 spin_lock_irqsave(&pnode->lock, flags);
6145 pnode->save_flags &= ~NLP_WAIT_FOR_LOGO;
6147 spin_lock_irqsave(&pnode->lock, flags);
6149 pnode->logo_waitq = NULL;
6150 spin_unlock_irqrestore(&pnode->lock, flags);
6154 spin_unlock_irqrestore(&pnode->lock, flags);
6159 lpfc_printf_vlog(vport, KERN_ERR, logit,
6160 "0723 SCSI layer issued Target Reset (%d, %llu) "
6161 "return x%x\n", tgt_id, lun_id, status);
6164 * We have to clean up i/o as : they may be orphaned by the TMF;
6165 * or if the TMF failed, they may be in an indeterminate state.
6167 * We will report success if all the i/o aborts successfully.
6169 if (status == SUCCESS)
6170 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
6176 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
6177 * @cmnd: Pointer to scsi_cmnd data structure.
6179 * This routine does host reset to the adaptor port. It brings the HBA
6180 * offline, performs a board restart, and then brings the board back online.
6181 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
6182 * reject all outstanding SCSI commands to the host and error returned
6183 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
6184 * of error handling, it will only return error if resetting of the adapter
6185 * is not successful; in all other cases, will return success.
6192 lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
6194 struct Scsi_Host *shost = cmnd->device->host;
6195 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
6196 struct lpfc_hba *phba = vport->phba;
6197 int rc, ret = SUCCESS;
6199 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
6200 "3172 SCSI layer issued Host Reset Data:\n");
6202 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
6204 rc = lpfc_sli_brdrestart(phba);
6208 /* Wait for successful restart of adapter */
6209 if (phba->sli_rev < LPFC_SLI_REV4) {
6210 rc = lpfc_sli_chipset_init(phba);
6215 rc = lpfc_online(phba);
6219 lpfc_unblock_mgmt_io(phba);
6223 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6224 "3323 Failed host reset\n");
6225 lpfc_unblock_mgmt_io(phba);
6230 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
6231 * @sdev: Pointer to scsi_device.
6233 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
6234 * globally available list of scsi buffers. This routine also makes sure scsi
6235 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
6236 * of scsi buffer exists for the lifetime of the driver.
6243 lpfc_slave_alloc(struct scsi_device *sdev)
6245 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
6246 struct lpfc_hba *phba = vport->phba;
6247 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
6249 uint32_t num_to_alloc = 0;
6250 int num_allocated = 0;
6252 struct lpfc_device_data *device_data;
6253 unsigned long flags;
6254 struct lpfc_name target_wwpn;
6256 if (!rport || fc_remote_port_chkready(rport))
6259 if (phba->cfg_fof) {
6262 * Check to see if the device data structure for the lun
6263 * exists. If not, create one.
6266 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
6267 spin_lock_irqsave(&phba->devicelock, flags);
6268 device_data = __lpfc_get_device_data(phba,
6270 &vport->fc_portname,
6274 spin_unlock_irqrestore(&phba->devicelock, flags);
6275 device_data = lpfc_create_device_data(phba,
6276 &vport->fc_portname,
6279 phba->cfg_XLanePriority,
6283 spin_lock_irqsave(&phba->devicelock, flags);
6284 list_add_tail(&device_data->listentry, &phba->luns);
6286 device_data->rport_data = rport->dd_data;
6287 device_data->available = true;
6288 spin_unlock_irqrestore(&phba->devicelock, flags);
6289 sdev->hostdata = device_data;
6291 sdev->hostdata = rport->dd_data;
6293 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
6295 /* For SLI4, all IO buffers are pre-allocated */
6296 if (phba->sli_rev == LPFC_SLI_REV4)
6299 /* This code path is now ONLY for SLI3 adapters */
6302 * Populate the cmds_per_lun count scsi_bufs into this host's globally
6303 * available list of scsi buffers. Don't allocate more than the
6304 * HBA limit conveyed to the midlayer via the host structure. The
6305 * formula accounts for the lun_queue_depth + error handlers + 1
6306 * extra. This list of scsi bufs exists for the lifetime of the driver.
6308 total = phba->total_scsi_bufs;
6309 num_to_alloc = vport->cfg_lun_queue_depth + 2;
6311 /* If allocated buffers are enough do nothing */
6312 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
6315 /* Allow some exchanges to be available always to complete discovery */
6316 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
6317 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
6318 "0704 At limitation of %d preallocated "
6319 "command buffers\n", total);
6321 /* Allow some exchanges to be available always to complete discovery */
6322 } else if (total + num_to_alloc >
6323 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
6324 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
6325 "0705 Allocation request of %d "
6326 "command buffers will exceed max of %d. "
6327 "Reducing allocation request to %d.\n",
6328 num_to_alloc, phba->cfg_hba_queue_depth,
6329 (phba->cfg_hba_queue_depth - total));
6330 num_to_alloc = phba->cfg_hba_queue_depth - total;
6332 num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
6333 if (num_to_alloc != num_allocated) {
6334 lpfc_printf_vlog(vport, KERN_ERR, LOG_TRACE_EVENT,
6335 "0708 Allocation request of %d "
6336 "command buffers did not succeed. "
6337 "Allocated %d buffers.\n",
6338 num_to_alloc, num_allocated);
6340 if (num_allocated > 0)
6341 phba->total_scsi_bufs += num_allocated;
6346 * lpfc_slave_configure - scsi_host_template slave_configure entry point
6347 * @sdev: Pointer to scsi_device.
6349 * This routine configures following items
6350 * - Tag command queuing support for @sdev if supported.
6351 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
6357 lpfc_slave_configure(struct scsi_device *sdev)
6359 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
6360 struct lpfc_hba *phba = vport->phba;
6362 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
6364 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
6365 lpfc_sli_handle_fast_ring_event(phba,
6366 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
6367 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
6368 lpfc_poll_rearm_timer(phba);
6375 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
6376 * @sdev: Pointer to scsi_device.
6378 * This routine sets @sdev hostatdata filed to null.
6381 lpfc_slave_destroy(struct scsi_device *sdev)
6383 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
6384 struct lpfc_hba *phba = vport->phba;
6385 unsigned long flags;
6386 struct lpfc_device_data *device_data = sdev->hostdata;
6388 atomic_dec(&phba->sdev_cnt);
6389 if ((phba->cfg_fof) && (device_data)) {
6390 spin_lock_irqsave(&phba->devicelock, flags);
6391 device_data->available = false;
6392 if (!device_data->oas_enabled)
6393 lpfc_delete_device_data(phba, device_data);
6394 spin_unlock_irqrestore(&phba->devicelock, flags);
6396 sdev->hostdata = NULL;
6401 * lpfc_create_device_data - creates and initializes device data structure for OAS
6402 * @phba: Pointer to host bus adapter structure.
6403 * @vport_wwpn: Pointer to vport's wwpn information
6404 * @target_wwpn: Pointer to target's wwpn information
6405 * @lun: Lun on target
6407 * @atomic_create: Flag to indicate if memory should be allocated using the
6408 * GFP_ATOMIC flag or not.
6410 * This routine creates a device data structure which will contain identifying
6411 * information for the device (host wwpn, target wwpn, lun), state of OAS,
6412 * whether or not the corresponding lun is available by the system,
6413 * and pointer to the rport data.
6417 * Pointer to lpfc_device_data - Success
6419 struct lpfc_device_data*
6420 lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
6421 struct lpfc_name *target_wwpn, uint64_t lun,
6422 uint32_t pri, bool atomic_create)
6425 struct lpfc_device_data *lun_info;
6428 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
6432 /* Attempt to create the device data to contain lun info */
6435 memory_flags = GFP_ATOMIC;
6437 memory_flags = GFP_KERNEL;
6438 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
6441 INIT_LIST_HEAD(&lun_info->listentry);
6442 lun_info->rport_data = NULL;
6443 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
6444 sizeof(struct lpfc_name));
6445 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
6446 sizeof(struct lpfc_name));
6447 lun_info->device_id.lun = lun;
6448 lun_info->oas_enabled = false;
6449 lun_info->priority = pri;
6450 lun_info->available = false;
6455 * lpfc_delete_device_data - frees a device data structure for OAS
6456 * @phba: Pointer to host bus adapter structure.
6457 * @lun_info: Pointer to device data structure to free.
6459 * This routine frees the previously allocated device data structure passed.
6463 lpfc_delete_device_data(struct lpfc_hba *phba,
6464 struct lpfc_device_data *lun_info)
6467 if (unlikely(!phba) || !lun_info ||
6471 if (!list_empty(&lun_info->listentry))
6472 list_del(&lun_info->listentry);
6473 mempool_free(lun_info, phba->device_data_mem_pool);
6478 * __lpfc_get_device_data - returns the device data for the specified lun
6479 * @phba: Pointer to host bus adapter structure.
6480 * @list: Point to list to search.
6481 * @vport_wwpn: Pointer to vport's wwpn information
6482 * @target_wwpn: Pointer to target's wwpn information
6483 * @lun: Lun on target
6485 * This routine searches the list passed for the specified lun's device data.
6486 * This function does not hold locks, it is the responsibility of the caller
6487 * to ensure the proper lock is held before calling the function.
6491 * Pointer to lpfc_device_data - Success
6493 struct lpfc_device_data*
6494 __lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
6495 struct lpfc_name *vport_wwpn,
6496 struct lpfc_name *target_wwpn, uint64_t lun)
6499 struct lpfc_device_data *lun_info;
6501 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
6505 /* Check to see if the lun is already enabled for OAS. */
6507 list_for_each_entry(lun_info, list, listentry) {
6508 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
6509 sizeof(struct lpfc_name)) == 0) &&
6510 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
6511 sizeof(struct lpfc_name)) == 0) &&
6512 (lun_info->device_id.lun == lun))
6520 * lpfc_find_next_oas_lun - searches for the next oas lun
6521 * @phba: Pointer to host bus adapter structure.
6522 * @vport_wwpn: Pointer to vport's wwpn information
6523 * @target_wwpn: Pointer to target's wwpn information
6524 * @starting_lun: Pointer to the lun to start searching for
6525 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
6526 * @found_target_wwpn: Pointer to the found lun's target wwpn information
6527 * @found_lun: Pointer to the found lun.
6528 * @found_lun_status: Pointer to status of the found lun.
6529 * @found_lun_pri: Pointer to priority of the found lun.
6531 * This routine searches the luns list for the specified lun
6532 * or the first lun for the vport/target. If the vport wwpn contains
6533 * a zero value then a specific vport is not specified. In this case
6534 * any vport which contains the lun will be considered a match. If the
6535 * target wwpn contains a zero value then a specific target is not specified.
6536 * In this case any target which contains the lun will be considered a
6537 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
6538 * are returned. The function will also return the next lun if available.
6539 * If the next lun is not found, starting_lun parameter will be set to
6547 lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
6548 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
6549 struct lpfc_name *found_vport_wwpn,
6550 struct lpfc_name *found_target_wwpn,
6551 uint64_t *found_lun,
6552 uint32_t *found_lun_status,
6553 uint32_t *found_lun_pri)
6556 unsigned long flags;
6557 struct lpfc_device_data *lun_info;
6558 struct lpfc_device_id *device_id;
6562 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
6563 !starting_lun || !found_vport_wwpn ||
6564 !found_target_wwpn || !found_lun || !found_lun_status ||
6565 (*starting_lun == NO_MORE_OAS_LUN) ||
6569 lun = *starting_lun;
6570 *found_lun = NO_MORE_OAS_LUN;
6571 *starting_lun = NO_MORE_OAS_LUN;
6573 /* Search for lun or the lun closet in value */
6575 spin_lock_irqsave(&phba->devicelock, flags);
6576 list_for_each_entry(lun_info, &phba->luns, listentry) {
6577 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
6578 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
6579 sizeof(struct lpfc_name)) == 0)) &&
6580 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
6581 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
6582 sizeof(struct lpfc_name)) == 0)) &&
6583 (lun_info->oas_enabled)) {
6584 device_id = &lun_info->device_id;
6586 ((lun == FIND_FIRST_OAS_LUN) ||
6587 (device_id->lun == lun))) {
6588 *found_lun = device_id->lun;
6589 memcpy(found_vport_wwpn,
6590 &device_id->vport_wwpn,
6591 sizeof(struct lpfc_name));
6592 memcpy(found_target_wwpn,
6593 &device_id->target_wwpn,
6594 sizeof(struct lpfc_name));
6595 if (lun_info->available)
6597 OAS_LUN_STATUS_EXISTS;
6599 *found_lun_status = 0;
6600 *found_lun_pri = lun_info->priority;
6601 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
6602 memset(vport_wwpn, 0x0,
6603 sizeof(struct lpfc_name));
6604 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
6605 memset(target_wwpn, 0x0,
6606 sizeof(struct lpfc_name));
6609 *starting_lun = device_id->lun;
6610 memcpy(vport_wwpn, &device_id->vport_wwpn,
6611 sizeof(struct lpfc_name));
6612 memcpy(target_wwpn, &device_id->target_wwpn,
6613 sizeof(struct lpfc_name));
6618 spin_unlock_irqrestore(&phba->devicelock, flags);
6623 * lpfc_enable_oas_lun - enables a lun for OAS operations
6624 * @phba: Pointer to host bus adapter structure.
6625 * @vport_wwpn: Pointer to vport's wwpn information
6626 * @target_wwpn: Pointer to target's wwpn information
6630 * This routine enables a lun for oas operations. The routines does so by
6631 * doing the following :
6633 * 1) Checks to see if the device data for the lun has been created.
6634 * 2) If found, sets the OAS enabled flag if not set and returns.
6635 * 3) Otherwise, creates a device data structure.
6636 * 4) If successfully created, indicates the device data is for an OAS lun,
6637 * indicates the lun is not available and add to the list of luns.
6644 lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
6645 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
6648 struct lpfc_device_data *lun_info;
6649 unsigned long flags;
6651 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
6655 spin_lock_irqsave(&phba->devicelock, flags);
6657 /* Check to see if the device data for the lun has been created */
6658 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
6661 if (!lun_info->oas_enabled)
6662 lun_info->oas_enabled = true;
6663 lun_info->priority = pri;
6664 spin_unlock_irqrestore(&phba->devicelock, flags);
6668 /* Create an lun info structure and add to list of luns */
6669 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
6672 lun_info->oas_enabled = true;
6673 lun_info->priority = pri;
6674 lun_info->available = false;
6675 list_add_tail(&lun_info->listentry, &phba->luns);
6676 spin_unlock_irqrestore(&phba->devicelock, flags);
6679 spin_unlock_irqrestore(&phba->devicelock, flags);
6684 * lpfc_disable_oas_lun - disables a lun for OAS operations
6685 * @phba: Pointer to host bus adapter structure.
6686 * @vport_wwpn: Pointer to vport's wwpn information
6687 * @target_wwpn: Pointer to target's wwpn information
6691 * This routine disables a lun for oas operations. The routines does so by
6692 * doing the following :
6694 * 1) Checks to see if the device data for the lun is created.
6695 * 2) If present, clears the flag indicating this lun is for OAS.
6696 * 3) If the lun is not available by the system, the device data is
6704 lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
6705 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
6708 struct lpfc_device_data *lun_info;
6709 unsigned long flags;
6711 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
6715 spin_lock_irqsave(&phba->devicelock, flags);
6717 /* Check to see if the lun is available. */
6718 lun_info = __lpfc_get_device_data(phba,
6719 &phba->luns, vport_wwpn,
6722 lun_info->oas_enabled = false;
6723 lun_info->priority = pri;
6724 if (!lun_info->available)
6725 lpfc_delete_device_data(phba, lun_info);
6726 spin_unlock_irqrestore(&phba->devicelock, flags);
6730 spin_unlock_irqrestore(&phba->devicelock, flags);
6735 lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
6737 return SCSI_MLQUEUE_HOST_BUSY;
6741 lpfc_no_slave(struct scsi_device *sdev)
6746 struct scsi_host_template lpfc_template_nvme = {
6747 .module = THIS_MODULE,
6748 .name = LPFC_DRIVER_NAME,
6749 .proc_name = LPFC_DRIVER_NAME,
6751 .queuecommand = lpfc_no_command,
6752 .slave_alloc = lpfc_no_slave,
6753 .slave_configure = lpfc_no_slave,
6754 .scan_finished = lpfc_scan_finished,
6758 .shost_groups = lpfc_hba_groups,
6759 .max_sectors = 0xFFFFFFFF,
6760 .vendor_id = LPFC_NL_VENDOR_ID,
6761 .track_queue_depth = 0,
6764 struct scsi_host_template lpfc_template = {
6765 .module = THIS_MODULE,
6766 .name = LPFC_DRIVER_NAME,
6767 .proc_name = LPFC_DRIVER_NAME,
6769 .queuecommand = lpfc_queuecommand,
6770 .eh_timed_out = fc_eh_timed_out,
6771 .eh_should_retry_cmd = fc_eh_should_retry_cmd,
6772 .eh_abort_handler = lpfc_abort_handler,
6773 .eh_device_reset_handler = lpfc_device_reset_handler,
6774 .eh_target_reset_handler = lpfc_target_reset_handler,
6775 .eh_host_reset_handler = lpfc_host_reset_handler,
6776 .slave_alloc = lpfc_slave_alloc,
6777 .slave_configure = lpfc_slave_configure,
6778 .slave_destroy = lpfc_slave_destroy,
6779 .scan_finished = lpfc_scan_finished,
6781 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6782 .cmd_per_lun = LPFC_CMD_PER_LUN,
6783 .shost_groups = lpfc_hba_groups,
6784 .max_sectors = 0xFFFFFFFF,
6785 .vendor_id = LPFC_NL_VENDOR_ID,
6786 .change_queue_depth = scsi_change_queue_depth,
6787 .track_queue_depth = 1,
6790 struct scsi_host_template lpfc_vport_template = {
6791 .module = THIS_MODULE,
6792 .name = LPFC_DRIVER_NAME,
6793 .proc_name = LPFC_DRIVER_NAME,
6795 .queuecommand = lpfc_queuecommand,
6796 .eh_timed_out = fc_eh_timed_out,
6797 .eh_should_retry_cmd = fc_eh_should_retry_cmd,
6798 .eh_abort_handler = lpfc_abort_handler,
6799 .eh_device_reset_handler = lpfc_device_reset_handler,
6800 .eh_target_reset_handler = lpfc_target_reset_handler,
6801 .eh_bus_reset_handler = NULL,
6802 .eh_host_reset_handler = NULL,
6803 .slave_alloc = lpfc_slave_alloc,
6804 .slave_configure = lpfc_slave_configure,
6805 .slave_destroy = lpfc_slave_destroy,
6806 .scan_finished = lpfc_scan_finished,
6808 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6809 .cmd_per_lun = LPFC_CMD_PER_LUN,
6810 .shost_groups = lpfc_vport_groups,
6811 .max_sectors = 0xFFFFFFFF,
6813 .change_queue_depth = scsi_change_queue_depth,
6814 .track_queue_depth = 1,