1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2012 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
20 *******************************************************************/
21 #include <linux/pci.h>
22 #include <linux/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/export.h>
25 #include <linux/delay.h>
26 #include <asm/unaligned.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_eh.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi_transport_fc.h>
35 #include "lpfc_version.h"
39 #include "lpfc_sli4.h"
41 #include "lpfc_disc.h"
43 #include "lpfc_scsi.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_crtn.h"
46 #include "lpfc_vport.h"
48 #define LPFC_RESET_WAIT 2
49 #define LPFC_ABORT_WAIT 2
53 static char *dif_op_str[] = {
63 static char *dif_grd_str[] = {
69 struct scsi_dif_tuple {
70 __be16 guard_tag; /* Checksum */
71 __be16 app_tag; /* Opaque storage */
72 __be32 ref_tag; /* Target LBA or indirect LBA */
76 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
78 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
81 lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
84 struct scatterlist *sgde = scsi_sglist(cmnd);
86 if (!_dump_buf_data) {
87 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
88 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
95 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
96 "9051 BLKGRD: ERROR: data scatterlist is null\n");
100 dst = (void *) _dump_buf_data;
103 memcpy(dst, src, sgde->length);
105 sgde = sg_next(sgde);
110 lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
113 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
115 if (!_dump_buf_dif) {
116 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
117 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
123 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
124 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
131 memcpy(dst, src, sgde->length);
133 sgde = sg_next(sgde);
138 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
139 * @phba: Pointer to HBA object.
140 * @lpfc_cmd: lpfc scsi command object pointer.
142 * This function is called from the lpfc_prep_task_mgmt_cmd function to
143 * set the last bit in the response sge entry.
146 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
147 struct lpfc_scsi_buf *lpfc_cmd)
149 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
152 sgl->word2 = le32_to_cpu(sgl->word2);
153 bf_set(lpfc_sli4_sge_last, sgl, 1);
154 sgl->word2 = cpu_to_le32(sgl->word2);
159 * lpfc_update_stats - Update statistical data for the command completion
160 * @phba: Pointer to HBA object.
161 * @lpfc_cmd: lpfc scsi command object pointer.
163 * This function is called when there is a command completion and this
164 * function updates the statistical data for the command completion.
167 lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
169 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
170 struct lpfc_nodelist *pnode = rdata->pnode;
171 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
173 struct Scsi_Host *shost = cmd->device->host;
174 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
175 unsigned long latency;
181 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
183 spin_lock_irqsave(shost->host_lock, flags);
184 if (!vport->stat_data_enabled ||
185 vport->stat_data_blocked ||
188 (phba->bucket_type == LPFC_NO_BUCKET)) {
189 spin_unlock_irqrestore(shost->host_lock, flags);
193 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
194 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
196 /* check array subscript bounds */
199 else if (i >= LPFC_MAX_BUCKET_COUNT)
200 i = LPFC_MAX_BUCKET_COUNT - 1;
202 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
203 if (latency <= (phba->bucket_base +
204 ((1<<i)*phba->bucket_step)))
208 pnode->lat_data[i].cmd_count++;
209 spin_unlock_irqrestore(shost->host_lock, flags);
213 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
214 * @phba: Pointer to HBA context object.
215 * @vport: Pointer to vport object.
216 * @ndlp: Pointer to FC node associated with the target.
217 * @lun: Lun number of the scsi device.
218 * @old_val: Old value of the queue depth.
219 * @new_val: New value of the queue depth.
221 * This function sends an event to the mgmt application indicating
222 * there is a change in the scsi device queue depth.
225 lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
226 struct lpfc_vport *vport,
227 struct lpfc_nodelist *ndlp,
232 struct lpfc_fast_path_event *fast_path_evt;
235 fast_path_evt = lpfc_alloc_fast_evt(phba);
239 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
241 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
242 LPFC_EVENT_VARQUEDEPTH;
244 /* Report all luns with change in queue depth */
245 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
246 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
247 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
248 &ndlp->nlp_portname, sizeof(struct lpfc_name));
249 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
250 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
253 fast_path_evt->un.queue_depth_evt.oldval = old_val;
254 fast_path_evt->un.queue_depth_evt.newval = new_val;
255 fast_path_evt->vport = vport;
257 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
258 spin_lock_irqsave(&phba->hbalock, flags);
259 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
260 spin_unlock_irqrestore(&phba->hbalock, flags);
261 lpfc_worker_wake_up(phba);
267 * lpfc_change_queue_depth - Alter scsi device queue depth
268 * @sdev: Pointer the scsi device on which to change the queue depth.
269 * @qdepth: New queue depth to set the sdev to.
270 * @reason: The reason for the queue depth change.
272 * This function is called by the midlayer and the LLD to alter the queue
273 * depth for a scsi device. This function sets the queue depth to the new
274 * value and sends an event out to log the queue depth change.
277 lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
279 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
280 struct lpfc_hba *phba = vport->phba;
281 struct lpfc_rport_data *rdata;
282 unsigned long new_queue_depth, old_queue_depth;
284 old_queue_depth = sdev->queue_depth;
285 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
286 new_queue_depth = sdev->queue_depth;
287 rdata = sdev->hostdata;
289 lpfc_send_sdev_queuedepth_change_event(phba, vport,
290 rdata->pnode, sdev->lun,
293 return sdev->queue_depth;
297 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
298 * @phba: The Hba for which this call is being executed.
300 * This routine is called when there is resource error in driver or firmware.
301 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
302 * posts at most 1 event each second. This routine wakes up worker thread of
303 * @phba to process WORKER_RAM_DOWN_EVENT event.
305 * This routine should be called with no lock held.
308 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
313 spin_lock_irqsave(&phba->hbalock, flags);
314 atomic_inc(&phba->num_rsrc_err);
315 phba->last_rsrc_error_time = jiffies;
317 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
318 spin_unlock_irqrestore(&phba->hbalock, flags);
322 phba->last_ramp_down_time = jiffies;
324 spin_unlock_irqrestore(&phba->hbalock, flags);
326 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
327 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
329 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
330 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
333 lpfc_worker_wake_up(phba);
338 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
339 * @phba: The Hba for which this call is being executed.
341 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
342 * post at most 1 event every 5 minute after last_ramp_up_time or
343 * last_rsrc_error_time. This routine wakes up worker thread of @phba
344 * to process WORKER_RAM_DOWN_EVENT event.
346 * This routine should be called with no lock held.
349 lpfc_rampup_queue_depth(struct lpfc_vport *vport,
350 uint32_t queue_depth)
353 struct lpfc_hba *phba = vport->phba;
355 atomic_inc(&phba->num_cmd_success);
357 if (vport->cfg_lun_queue_depth <= queue_depth)
359 spin_lock_irqsave(&phba->hbalock, flags);
360 if (time_before(jiffies,
361 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
363 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
364 spin_unlock_irqrestore(&phba->hbalock, flags);
367 phba->last_ramp_up_time = jiffies;
368 spin_unlock_irqrestore(&phba->hbalock, flags);
370 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
371 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
373 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
374 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
377 lpfc_worker_wake_up(phba);
382 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
383 * @phba: The Hba for which this call is being executed.
385 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
386 * thread.This routine reduces queue depth for all scsi device on each vport
387 * associated with @phba.
390 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
392 struct lpfc_vport **vports;
393 struct Scsi_Host *shost;
394 struct scsi_device *sdev;
395 unsigned long new_queue_depth;
396 unsigned long num_rsrc_err, num_cmd_success;
399 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
400 num_cmd_success = atomic_read(&phba->num_cmd_success);
402 vports = lpfc_create_vport_work_array(phba);
404 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
405 shost = lpfc_shost_from_vport(vports[i]);
406 shost_for_each_device(sdev, shost) {
408 sdev->queue_depth * num_rsrc_err /
409 (num_rsrc_err + num_cmd_success);
410 if (!new_queue_depth)
411 new_queue_depth = sdev->queue_depth - 1;
413 new_queue_depth = sdev->queue_depth -
415 lpfc_change_queue_depth(sdev, new_queue_depth,
416 SCSI_QDEPTH_DEFAULT);
419 lpfc_destroy_vport_work_array(phba, vports);
420 atomic_set(&phba->num_rsrc_err, 0);
421 atomic_set(&phba->num_cmd_success, 0);
425 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
426 * @phba: The Hba for which this call is being executed.
428 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
429 * thread.This routine increases queue depth for all scsi device on each vport
430 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
431 * num_cmd_success to zero.
434 lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
436 struct lpfc_vport **vports;
437 struct Scsi_Host *shost;
438 struct scsi_device *sdev;
441 vports = lpfc_create_vport_work_array(phba);
443 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
444 shost = lpfc_shost_from_vport(vports[i]);
445 shost_for_each_device(sdev, shost) {
446 if (vports[i]->cfg_lun_queue_depth <=
449 lpfc_change_queue_depth(sdev,
451 SCSI_QDEPTH_RAMP_UP);
454 lpfc_destroy_vport_work_array(phba, vports);
455 atomic_set(&phba->num_rsrc_err, 0);
456 atomic_set(&phba->num_cmd_success, 0);
460 * lpfc_scsi_dev_block - set all scsi hosts to block state
461 * @phba: Pointer to HBA context object.
463 * This function walks vport list and set each SCSI host to block state
464 * by invoking fc_remote_port_delete() routine. This function is invoked
465 * with EEH when device's PCI slot has been permanently disabled.
468 lpfc_scsi_dev_block(struct lpfc_hba *phba)
470 struct lpfc_vport **vports;
471 struct Scsi_Host *shost;
472 struct scsi_device *sdev;
473 struct fc_rport *rport;
476 vports = lpfc_create_vport_work_array(phba);
478 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
479 shost = lpfc_shost_from_vport(vports[i]);
480 shost_for_each_device(sdev, shost) {
481 rport = starget_to_rport(scsi_target(sdev));
482 fc_remote_port_delete(rport);
485 lpfc_destroy_vport_work_array(phba, vports);
489 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
490 * @vport: The virtual port for which this call being executed.
491 * @num_to_allocate: The requested number of buffers to allocate.
493 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
494 * the scsi buffer contains all the necessary information needed to initiate
495 * a SCSI I/O. The non-DMAable buffer region contains information to build
496 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
497 * and the initial BPL. In addition to allocating memory, the FCP CMND and
498 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
501 * int - number of scsi buffers that were allocated.
502 * 0 = failure, less than num_to_alloc is a partial failure.
505 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
507 struct lpfc_hba *phba = vport->phba;
508 struct lpfc_scsi_buf *psb;
509 struct ulp_bde64 *bpl;
511 dma_addr_t pdma_phys_fcp_cmd;
512 dma_addr_t pdma_phys_fcp_rsp;
513 dma_addr_t pdma_phys_bpl;
517 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
518 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
523 * Get memory from the pci pool to map the virt space to pci
524 * bus space for an I/O. The DMA buffer includes space for the
525 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
526 * necessary to support the sg_tablesize.
528 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
529 GFP_KERNEL, &psb->dma_handle);
535 /* Initialize virtual ptrs to dma_buf region. */
536 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
538 /* Allocate iotag for psb->cur_iocbq. */
539 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
541 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
542 psb->data, psb->dma_handle);
546 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
548 psb->fcp_cmnd = psb->data;
549 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
550 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
551 sizeof(struct fcp_rsp);
553 /* Initialize local short-hand pointers. */
555 pdma_phys_fcp_cmd = psb->dma_handle;
556 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
557 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
558 sizeof(struct fcp_rsp);
561 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
562 * are sg list bdes. Initialize the first two and leave the
563 * rest for queuecommand.
565 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
566 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
567 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
568 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
569 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
571 /* Setup the physical region for the FCP RSP */
572 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
573 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
574 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
575 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
576 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
579 * Since the IOCB for the FCP I/O is built into this
580 * lpfc_scsi_buf, initialize it with all known data now.
582 iocb = &psb->cur_iocbq.iocb;
583 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
584 if ((phba->sli_rev == 3) &&
585 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
586 /* fill in immediate fcp command BDE */
587 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
588 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
589 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
591 iocb->un.fcpi64.bdl.addrHigh = 0;
592 iocb->ulpBdeCount = 0;
594 /* fill in response BDE */
595 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
597 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
598 sizeof(struct fcp_rsp);
599 iocb->unsli3.fcp_ext.rbde.addrLow =
600 putPaddrLow(pdma_phys_fcp_rsp);
601 iocb->unsli3.fcp_ext.rbde.addrHigh =
602 putPaddrHigh(pdma_phys_fcp_rsp);
604 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
605 iocb->un.fcpi64.bdl.bdeSize =
606 (2 * sizeof(struct ulp_bde64));
607 iocb->un.fcpi64.bdl.addrLow =
608 putPaddrLow(pdma_phys_bpl);
609 iocb->un.fcpi64.bdl.addrHigh =
610 putPaddrHigh(pdma_phys_bpl);
611 iocb->ulpBdeCount = 1;
614 iocb->ulpClass = CLASS3;
615 psb->status = IOSTAT_SUCCESS;
616 /* Put it back into the SCSI buffer list */
617 psb->cur_iocbq.context1 = psb;
618 lpfc_release_scsi_buf_s3(phba, psb);
626 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
627 * @vport: pointer to lpfc vport data structure.
629 * This routine is invoked by the vport cleanup for deletions and the cleanup
630 * for an ndlp on removal.
633 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
635 struct lpfc_hba *phba = vport->phba;
636 struct lpfc_scsi_buf *psb, *next_psb;
637 unsigned long iflag = 0;
639 spin_lock_irqsave(&phba->hbalock, iflag);
640 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
641 list_for_each_entry_safe(psb, next_psb,
642 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
643 if (psb->rdata && psb->rdata->pnode
644 && psb->rdata->pnode->vport == vport)
647 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
648 spin_unlock_irqrestore(&phba->hbalock, iflag);
652 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
653 * @phba: pointer to lpfc hba data structure.
654 * @axri: pointer to the fcp xri abort wcqe structure.
656 * This routine is invoked by the worker thread to process a SLI4 fast-path
660 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
661 struct sli4_wcqe_xri_aborted *axri)
663 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
664 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
665 struct lpfc_scsi_buf *psb, *next_psb;
666 unsigned long iflag = 0;
667 struct lpfc_iocbq *iocbq;
669 struct lpfc_nodelist *ndlp;
671 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
673 spin_lock_irqsave(&phba->hbalock, iflag);
674 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
675 list_for_each_entry_safe(psb, next_psb,
676 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
677 if (psb->cur_iocbq.sli4_xritag == xri) {
678 list_del(&psb->list);
680 psb->status = IOSTAT_SUCCESS;
682 &phba->sli4_hba.abts_scsi_buf_list_lock);
683 if (psb->rdata && psb->rdata->pnode)
684 ndlp = psb->rdata->pnode;
688 rrq_empty = list_empty(&phba->active_rrq_list);
689 spin_unlock_irqrestore(&phba->hbalock, iflag);
691 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
692 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
694 lpfc_release_scsi_buf_s4(phba, psb);
696 lpfc_worker_wake_up(phba);
700 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
701 for (i = 1; i <= phba->sli.last_iotag; i++) {
702 iocbq = phba->sli.iocbq_lookup[i];
704 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
705 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
707 if (iocbq->sli4_xritag != xri)
709 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
711 spin_unlock_irqrestore(&phba->hbalock, iflag);
713 lpfc_worker_wake_up(phba);
717 spin_unlock_irqrestore(&phba->hbalock, iflag);
721 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
722 * @phba: pointer to lpfc hba data structure.
724 * This routine walks the list of scsi buffers that have been allocated and
725 * repost them to the HBA by using SGL block post. This is needed after a
726 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
727 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
728 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
730 * Returns: 0 = success, non-zero failure.
733 lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
735 struct lpfc_scsi_buf *psb;
736 int index, status, bcnt = 0, rcnt = 0, rc = 0;
739 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
740 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
742 /* Remove from SCSI buffer list */
743 list_del(&psb->list);
744 /* Add it to a local SCSI buffer list */
745 list_add_tail(&psb->list, &sblist);
746 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
751 /* A hole present in the XRI array, need to skip */
754 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
755 /* End of XRI array for SCSI buffer, complete */
758 /* Continue until collect up to a nembed page worth of sgls */
761 /* Now, post the SCSI buffer list sgls as a block */
762 if (!phba->sli4_hba.extents_in_use)
763 status = lpfc_sli4_post_scsi_sgl_block(phba,
767 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
770 /* Reset SCSI buffer count for next round of posting */
772 while (!list_empty(&sblist)) {
773 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
776 /* Put this back on the abort scsi list */
781 psb->status = IOSTAT_SUCCESS;
783 /* Put it back into the SCSI buffer list */
784 lpfc_release_scsi_buf_s4(phba, psb);
791 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
792 * @vport: The virtual port for which this call being executed.
793 * @num_to_allocate: The requested number of buffers to allocate.
795 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
796 * the scsi buffer contains all the necessary information needed to initiate
800 * int - number of scsi buffers that were allocated.
801 * 0 = failure, less than num_to_alloc is a partial failure.
804 lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
806 struct lpfc_hba *phba = vport->phba;
807 struct lpfc_scsi_buf *psb;
808 struct sli4_sge *sgl;
810 dma_addr_t pdma_phys_fcp_cmd;
811 dma_addr_t pdma_phys_fcp_rsp;
812 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
813 uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
814 int status = 0, index;
816 int non_sequential_xri = 0;
819 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
820 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
825 * Get memory from the pci pool to map the virt space to pci bus
826 * space for an I/O. The DMA buffer includes space for the
827 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
828 * necessary to support the sg_tablesize.
830 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
831 GFP_KERNEL, &psb->dma_handle);
837 /* Initialize virtual ptrs to dma_buf region. */
838 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
840 /* Allocate iotag for psb->cur_iocbq. */
841 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
843 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
844 psb->data, psb->dma_handle);
849 lxri = lpfc_sli4_next_xritag(phba);
850 if (lxri == NO_XRI) {
851 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
852 psb->data, psb->dma_handle);
856 psb->cur_iocbq.sli4_lxritag = lxri;
857 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
858 if (last_xritag != NO_XRI
859 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
860 non_sequential_xri = 1;
862 list_add_tail(&psb->list, &sblist);
863 last_xritag = psb->cur_iocbq.sli4_xritag;
865 index = phba->sli4_hba.scsi_xri_cnt++;
866 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
868 psb->fcp_bpl = psb->data;
869 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
870 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
871 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
872 sizeof(struct fcp_cmnd));
874 /* Initialize local short-hand pointers. */
875 sgl = (struct sli4_sge *)psb->fcp_bpl;
876 pdma_phys_bpl = psb->dma_handle;
878 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
879 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
880 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
883 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
884 * are sg list bdes. Initialize the first two and leave the
885 * rest for queuecommand.
887 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
888 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
889 sgl->word2 = le32_to_cpu(sgl->word2);
890 bf_set(lpfc_sli4_sge_last, sgl, 0);
891 sgl->word2 = cpu_to_le32(sgl->word2);
892 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
895 /* Setup the physical region for the FCP RSP */
896 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
897 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
898 sgl->word2 = le32_to_cpu(sgl->word2);
899 bf_set(lpfc_sli4_sge_last, sgl, 1);
900 sgl->word2 = cpu_to_le32(sgl->word2);
901 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
904 * Since the IOCB for the FCP I/O is built into this
905 * lpfc_scsi_buf, initialize it with all known data now.
907 iocb = &psb->cur_iocbq.iocb;
908 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
909 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
910 /* setting the BLP size to 2 * sizeof BDE may not be correct.
911 * We are setting the bpl to point to out sgl. An sgl's
912 * entries are 16 bytes, a bpl entries are 12 bytes.
914 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
915 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
916 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
917 iocb->ulpBdeCount = 1;
919 iocb->ulpClass = CLASS3;
920 psb->cur_iocbq.context1 = psb;
921 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
922 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
925 psb->dma_phys_bpl = pdma_phys_bpl;
926 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
927 if (non_sequential_xri) {
928 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
930 psb->cur_iocbq.sli4_xritag);
932 /* Put this back on the abort scsi list */
936 psb->status = IOSTAT_SUCCESS;
938 /* Put it back into the SCSI buffer list */
939 lpfc_release_scsi_buf_s4(phba, psb);
944 if (!phba->sli4_hba.extents_in_use)
945 status = lpfc_sli4_post_scsi_sgl_block(phba,
949 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
954 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
955 "3021 SCSI SGL post error %d\n",
959 /* Reset SCSI buffer count for next round of posting */
960 while (!list_empty(&sblist)) {
961 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
964 /* Put this back on the abort scsi list */
968 psb->status = IOSTAT_SUCCESS;
970 /* Put it back into the SCSI buffer list */
971 lpfc_release_scsi_buf_s4(phba, psb);
975 return bcnt + non_sequential_xri;
979 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
980 * @vport: The virtual port for which this call being executed.
981 * @num_to_allocate: The requested number of buffers to allocate.
983 * This routine wraps the actual SCSI buffer allocator function pointer from
984 * the lpfc_hba struct.
987 * int - number of scsi buffers that were allocated.
988 * 0 = failure, less than num_to_alloc is a partial failure.
991 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
993 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
997 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
998 * @phba: The HBA for which this call is being executed.
1000 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1001 * and returns to caller.
1005 * Pointer to lpfc_scsi_buf - Success
1007 static struct lpfc_scsi_buf*
1008 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1010 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1011 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
1012 unsigned long iflag = 0;
1014 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1015 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1017 lpfc_cmd->seg_cnt = 0;
1018 lpfc_cmd->nonsg_phys = 0;
1019 lpfc_cmd->prot_seg_cnt = 0;
1021 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1025 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1026 * @phba: The HBA for which this call is being executed.
1028 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1029 * and returns to caller.
1033 * Pointer to lpfc_scsi_buf - Success
1035 static struct lpfc_scsi_buf*
1036 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1038 struct lpfc_scsi_buf *lpfc_cmd ;
1039 unsigned long iflag = 0;
1042 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1043 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1045 if (lpfc_test_rrq_active(phba, ndlp,
1046 lpfc_cmd->cur_iocbq.sli4_xritag))
1048 list_del(&lpfc_cmd->list);
1050 lpfc_cmd->seg_cnt = 0;
1051 lpfc_cmd->nonsg_phys = 0;
1052 lpfc_cmd->prot_seg_cnt = 0;
1055 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1063 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1064 * @phba: The HBA for which this call is being executed.
1066 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1067 * and returns to caller.
1071 * Pointer to lpfc_scsi_buf - Success
1073 static struct lpfc_scsi_buf*
1074 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1076 return phba->lpfc_get_scsi_buf(phba, ndlp);
1080 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
1081 * @phba: The Hba for which this call is being executed.
1082 * @psb: The scsi buffer which is being released.
1084 * This routine releases @psb scsi buffer by adding it to tail of @phba
1085 * lpfc_scsi_buf_list list.
1088 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1090 unsigned long iflag = 0;
1092 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1094 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1095 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1099 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1100 * @phba: The Hba for which this call is being executed.
1101 * @psb: The scsi buffer which is being released.
1103 * This routine releases @psb scsi buffer by adding it to tail of @phba
1104 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1105 * and cannot be reused for at least RA_TOV amount of time if it was
1109 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1111 unsigned long iflag = 0;
1113 if (psb->exch_busy) {
1114 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1117 list_add_tail(&psb->list,
1118 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1119 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1123 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1125 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1126 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1131 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1132 * @phba: The Hba for which this call is being executed.
1133 * @psb: The scsi buffer which is being released.
1135 * This routine releases @psb scsi buffer by adding it to tail of @phba
1136 * lpfc_scsi_buf_list list.
1139 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1142 phba->lpfc_release_scsi_buf(phba, psb);
1146 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
1147 * @phba: The Hba for which this call is being executed.
1148 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1150 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1151 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1152 * through sg elements and format the bdea. This routine also initializes all
1153 * IOCB fields which are dependent on scsi command request buffer.
1160 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1162 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1163 struct scatterlist *sgel = NULL;
1164 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1165 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1166 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
1167 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1168 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1169 dma_addr_t physaddr;
1170 uint32_t num_bde = 0;
1171 int nseg, datadir = scsi_cmnd->sc_data_direction;
1174 * There are three possibilities here - use scatter-gather segment, use
1175 * the single mapping, or neither. Start the lpfc command prep by
1176 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1180 if (scsi_sg_count(scsi_cmnd)) {
1182 * The driver stores the segment count returned from pci_map_sg
1183 * because this a count of dma-mappings used to map the use_sg
1184 * pages. They are not guaranteed to be the same for those
1185 * architectures that implement an IOMMU.
1188 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1189 scsi_sg_count(scsi_cmnd), datadir);
1190 if (unlikely(!nseg))
1193 lpfc_cmd->seg_cnt = nseg;
1194 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1195 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1196 "9064 BLKGRD: %s: Too many sg segments from "
1197 "dma_map_sg. Config %d, seg_cnt %d\n",
1198 __func__, phba->cfg_sg_seg_cnt,
1200 scsi_dma_unmap(scsi_cmnd);
1205 * The driver established a maximum scatter-gather segment count
1206 * during probe that limits the number of sg elements in any
1207 * single scsi command. Just run through the seg_cnt and format
1209 * When using SLI-3 the driver will try to fit all the BDEs into
1210 * the IOCB. If it can't then the BDEs get added to a BPL as it
1211 * does for SLI-2 mode.
1213 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1214 physaddr = sg_dma_address(sgel);
1215 if (phba->sli_rev == 3 &&
1216 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1217 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
1218 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1219 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1220 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1221 data_bde->addrLow = putPaddrLow(physaddr);
1222 data_bde->addrHigh = putPaddrHigh(physaddr);
1225 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1226 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1227 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1229 le32_to_cpu(putPaddrLow(physaddr));
1231 le32_to_cpu(putPaddrHigh(physaddr));
1238 * Finish initializing those IOCB fields that are dependent on the
1239 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1240 * explicitly reinitialized and for SLI-3 the extended bde count is
1241 * explicitly reinitialized since all iocb memory resources are reused.
1243 if (phba->sli_rev == 3 &&
1244 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1245 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
1246 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1248 * The extended IOCB format can only fit 3 BDE or a BPL.
1249 * This I/O has more than 3 BDE so the 1st data bde will
1250 * be a BPL that is filled in here.
1252 physaddr = lpfc_cmd->dma_handle;
1253 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1254 data_bde->tus.f.bdeSize = (num_bde *
1255 sizeof(struct ulp_bde64));
1256 physaddr += (sizeof(struct fcp_cmnd) +
1257 sizeof(struct fcp_rsp) +
1258 (2 * sizeof(struct ulp_bde64)));
1259 data_bde->addrHigh = putPaddrHigh(physaddr);
1260 data_bde->addrLow = putPaddrLow(physaddr);
1261 /* ebde count includes the response bde and data bpl */
1262 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1264 /* ebde count includes the response bde and data bdes */
1265 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1268 iocb_cmd->un.fcpi64.bdl.bdeSize =
1269 ((num_bde + 2) * sizeof(struct ulp_bde64));
1270 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1272 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1275 * Due to difference in data length between DIF/non-DIF paths,
1276 * we need to set word 4 of IOCB here
1278 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1282 static inline unsigned
1283 lpfc_cmd_blksize(struct scsi_cmnd *sc)
1285 return sc->device->sector_size;
1288 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1290 /* Return if if error injection is detected by Initiator */
1291 #define BG_ERR_INIT 0x1
1292 /* Return if if error injection is detected by Target */
1293 #define BG_ERR_TGT 0x2
1294 /* Return if if swapping CSUM<-->CRC is required for error injection */
1295 #define BG_ERR_SWAP 0x10
1296 /* Return if disabling Guard/Ref/App checking is required for error injection */
1297 #define BG_ERR_CHECK 0x20
1300 * lpfc_bg_err_inject - Determine if we should inject an error
1301 * @phba: The Hba for which this call is being executed.
1302 * @sc: The SCSI command to examine
1303 * @reftag: (out) BlockGuard reference tag for transmitted data
1304 * @apptag: (out) BlockGuard application tag for transmitted data
1305 * @new_guard (in) Value to replace CRC with if needed
1307 * Returns BG_ERR_* bit mask or 0 if request ignored
1310 lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1311 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1313 struct scatterlist *sgpe; /* s/g prot entry */
1314 struct scatterlist *sgde; /* s/g data entry */
1315 struct lpfc_scsi_buf *lpfc_cmd = NULL;
1316 struct scsi_dif_tuple *src = NULL;
1317 struct lpfc_nodelist *ndlp;
1318 struct lpfc_rport_data *rdata;
1319 uint32_t op = scsi_get_prot_op(sc);
1326 if (op == SCSI_PROT_NORMAL)
1329 sgpe = scsi_prot_sglist(sc);
1330 sgde = scsi_sglist(sc);
1331 lba = scsi_get_lba(sc);
1333 /* First check if we need to match the LBA */
1334 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1335 blksize = lpfc_cmd_blksize(sc);
1336 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1338 /* Make sure we have the right LBA if one is specified */
1339 if ((phba->lpfc_injerr_lba < lba) ||
1340 (phba->lpfc_injerr_lba >= (lba + numblks)))
1343 blockoff = phba->lpfc_injerr_lba - lba;
1344 numblks = sg_dma_len(sgpe) /
1345 sizeof(struct scsi_dif_tuple);
1346 if (numblks < blockoff)
1351 /* Next check if we need to match the remote NPortID or WWPN */
1352 rdata = sc->device->hostdata;
1353 if (rdata && rdata->pnode) {
1354 ndlp = rdata->pnode;
1356 /* Make sure we have the right NPortID if one is specified */
1357 if (phba->lpfc_injerr_nportid &&
1358 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1362 * Make sure we have the right WWPN if one is specified.
1363 * wwn[0] should be a non-zero NAA in a good WWPN.
1365 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1366 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1367 sizeof(struct lpfc_name)) != 0))
1371 /* Setup a ptr to the protection data if the SCSI host provides it */
1373 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1375 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1378 /* Should we change the Reference Tag */
1380 if (phba->lpfc_injerr_wref_cnt) {
1382 case SCSI_PROT_WRITE_PASS:
1385 * For WRITE_PASS, force the error
1386 * to be sent on the wire. It should
1387 * be detected by the Target.
1388 * If blockoff != 0 error will be
1389 * inserted in middle of the IO.
1392 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1393 "9076 BLKGRD: Injecting reftag error: "
1394 "write lba x%lx + x%x oldrefTag x%x\n",
1395 (unsigned long)lba, blockoff,
1396 be32_to_cpu(src->ref_tag));
1399 * Save the old ref_tag so we can
1400 * restore it on completion.
1403 lpfc_cmd->prot_data_type =
1405 lpfc_cmd->prot_data_segment =
1407 lpfc_cmd->prot_data =
1410 src->ref_tag = cpu_to_be32(0xDEADBEEF);
1411 phba->lpfc_injerr_wref_cnt--;
1412 if (phba->lpfc_injerr_wref_cnt == 0) {
1413 phba->lpfc_injerr_nportid = 0;
1414 phba->lpfc_injerr_lba =
1415 LPFC_INJERR_LBA_OFF;
1416 memset(&phba->lpfc_injerr_wwpn,
1417 0, sizeof(struct lpfc_name));
1419 rc = BG_ERR_TGT | BG_ERR_CHECK;
1424 case SCSI_PROT_WRITE_INSERT:
1426 * For WRITE_INSERT, force the error
1427 * to be sent on the wire. It should be
1428 * detected by the Target.
1430 /* DEADBEEF will be the reftag on the wire */
1431 *reftag = 0xDEADBEEF;
1432 phba->lpfc_injerr_wref_cnt--;
1433 if (phba->lpfc_injerr_wref_cnt == 0) {
1434 phba->lpfc_injerr_nportid = 0;
1435 phba->lpfc_injerr_lba =
1436 LPFC_INJERR_LBA_OFF;
1437 memset(&phba->lpfc_injerr_wwpn,
1438 0, sizeof(struct lpfc_name));
1440 rc = BG_ERR_TGT | BG_ERR_CHECK;
1442 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1443 "9078 BLKGRD: Injecting reftag error: "
1444 "write lba x%lx\n", (unsigned long)lba);
1446 case SCSI_PROT_WRITE_STRIP:
1448 * For WRITE_STRIP and WRITE_PASS,
1449 * force the error on data
1450 * being copied from SLI-Host to SLI-Port.
1452 *reftag = 0xDEADBEEF;
1453 phba->lpfc_injerr_wref_cnt--;
1454 if (phba->lpfc_injerr_wref_cnt == 0) {
1455 phba->lpfc_injerr_nportid = 0;
1456 phba->lpfc_injerr_lba =
1457 LPFC_INJERR_LBA_OFF;
1458 memset(&phba->lpfc_injerr_wwpn,
1459 0, sizeof(struct lpfc_name));
1463 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1464 "9077 BLKGRD: Injecting reftag error: "
1465 "write lba x%lx\n", (unsigned long)lba);
1469 if (phba->lpfc_injerr_rref_cnt) {
1471 case SCSI_PROT_READ_INSERT:
1472 case SCSI_PROT_READ_STRIP:
1473 case SCSI_PROT_READ_PASS:
1475 * For READ_STRIP and READ_PASS, force the
1476 * error on data being read off the wire. It
1477 * should force an IO error to the driver.
1479 *reftag = 0xDEADBEEF;
1480 phba->lpfc_injerr_rref_cnt--;
1481 if (phba->lpfc_injerr_rref_cnt == 0) {
1482 phba->lpfc_injerr_nportid = 0;
1483 phba->lpfc_injerr_lba =
1484 LPFC_INJERR_LBA_OFF;
1485 memset(&phba->lpfc_injerr_wwpn,
1486 0, sizeof(struct lpfc_name));
1490 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1491 "9079 BLKGRD: Injecting reftag error: "
1492 "read lba x%lx\n", (unsigned long)lba);
1498 /* Should we change the Application Tag */
1500 if (phba->lpfc_injerr_wapp_cnt) {
1502 case SCSI_PROT_WRITE_PASS:
1505 * For WRITE_PASS, force the error
1506 * to be sent on the wire. It should
1507 * be detected by the Target.
1508 * If blockoff != 0 error will be
1509 * inserted in middle of the IO.
1512 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1513 "9080 BLKGRD: Injecting apptag error: "
1514 "write lba x%lx + x%x oldappTag x%x\n",
1515 (unsigned long)lba, blockoff,
1516 be16_to_cpu(src->app_tag));
1519 * Save the old app_tag so we can
1520 * restore it on completion.
1523 lpfc_cmd->prot_data_type =
1525 lpfc_cmd->prot_data_segment =
1527 lpfc_cmd->prot_data =
1530 src->app_tag = cpu_to_be16(0xDEAD);
1531 phba->lpfc_injerr_wapp_cnt--;
1532 if (phba->lpfc_injerr_wapp_cnt == 0) {
1533 phba->lpfc_injerr_nportid = 0;
1534 phba->lpfc_injerr_lba =
1535 LPFC_INJERR_LBA_OFF;
1536 memset(&phba->lpfc_injerr_wwpn,
1537 0, sizeof(struct lpfc_name));
1539 rc = BG_ERR_TGT | BG_ERR_CHECK;
1543 case SCSI_PROT_WRITE_INSERT:
1545 * For WRITE_INSERT, force the
1546 * error to be sent on the wire. It should be
1547 * detected by the Target.
1549 /* DEAD will be the apptag on the wire */
1551 phba->lpfc_injerr_wapp_cnt--;
1552 if (phba->lpfc_injerr_wapp_cnt == 0) {
1553 phba->lpfc_injerr_nportid = 0;
1554 phba->lpfc_injerr_lba =
1555 LPFC_INJERR_LBA_OFF;
1556 memset(&phba->lpfc_injerr_wwpn,
1557 0, sizeof(struct lpfc_name));
1559 rc = BG_ERR_TGT | BG_ERR_CHECK;
1561 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1562 "0813 BLKGRD: Injecting apptag error: "
1563 "write lba x%lx\n", (unsigned long)lba);
1565 case SCSI_PROT_WRITE_STRIP:
1567 * For WRITE_STRIP and WRITE_PASS,
1568 * force the error on data
1569 * being copied from SLI-Host to SLI-Port.
1572 phba->lpfc_injerr_wapp_cnt--;
1573 if (phba->lpfc_injerr_wapp_cnt == 0) {
1574 phba->lpfc_injerr_nportid = 0;
1575 phba->lpfc_injerr_lba =
1576 LPFC_INJERR_LBA_OFF;
1577 memset(&phba->lpfc_injerr_wwpn,
1578 0, sizeof(struct lpfc_name));
1582 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1583 "0812 BLKGRD: Injecting apptag error: "
1584 "write lba x%lx\n", (unsigned long)lba);
1588 if (phba->lpfc_injerr_rapp_cnt) {
1590 case SCSI_PROT_READ_INSERT:
1591 case SCSI_PROT_READ_STRIP:
1592 case SCSI_PROT_READ_PASS:
1594 * For READ_STRIP and READ_PASS, force the
1595 * error on data being read off the wire. It
1596 * should force an IO error to the driver.
1599 phba->lpfc_injerr_rapp_cnt--;
1600 if (phba->lpfc_injerr_rapp_cnt == 0) {
1601 phba->lpfc_injerr_nportid = 0;
1602 phba->lpfc_injerr_lba =
1603 LPFC_INJERR_LBA_OFF;
1604 memset(&phba->lpfc_injerr_wwpn,
1605 0, sizeof(struct lpfc_name));
1609 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1610 "0814 BLKGRD: Injecting apptag error: "
1611 "read lba x%lx\n", (unsigned long)lba);
1618 /* Should we change the Guard Tag */
1620 if (phba->lpfc_injerr_wgrd_cnt) {
1622 case SCSI_PROT_WRITE_PASS:
1626 case SCSI_PROT_WRITE_INSERT:
1628 * For WRITE_INSERT, force the
1629 * error to be sent on the wire. It should be
1630 * detected by the Target.
1632 phba->lpfc_injerr_wgrd_cnt--;
1633 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1634 phba->lpfc_injerr_nportid = 0;
1635 phba->lpfc_injerr_lba =
1636 LPFC_INJERR_LBA_OFF;
1637 memset(&phba->lpfc_injerr_wwpn,
1638 0, sizeof(struct lpfc_name));
1641 rc |= BG_ERR_TGT | BG_ERR_SWAP;
1642 /* Signals the caller to swap CRC->CSUM */
1644 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1645 "0817 BLKGRD: Injecting guard error: "
1646 "write lba x%lx\n", (unsigned long)lba);
1648 case SCSI_PROT_WRITE_STRIP:
1650 * For WRITE_STRIP and WRITE_PASS,
1651 * force the error on data
1652 * being copied from SLI-Host to SLI-Port.
1654 phba->lpfc_injerr_wgrd_cnt--;
1655 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1656 phba->lpfc_injerr_nportid = 0;
1657 phba->lpfc_injerr_lba =
1658 LPFC_INJERR_LBA_OFF;
1659 memset(&phba->lpfc_injerr_wwpn,
1660 0, sizeof(struct lpfc_name));
1663 rc = BG_ERR_INIT | BG_ERR_SWAP;
1664 /* Signals the caller to swap CRC->CSUM */
1666 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1667 "0816 BLKGRD: Injecting guard error: "
1668 "write lba x%lx\n", (unsigned long)lba);
1672 if (phba->lpfc_injerr_rgrd_cnt) {
1674 case SCSI_PROT_READ_INSERT:
1675 case SCSI_PROT_READ_STRIP:
1676 case SCSI_PROT_READ_PASS:
1678 * For READ_STRIP and READ_PASS, force the
1679 * error on data being read off the wire. It
1680 * should force an IO error to the driver.
1682 phba->lpfc_injerr_rgrd_cnt--;
1683 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1684 phba->lpfc_injerr_nportid = 0;
1685 phba->lpfc_injerr_lba =
1686 LPFC_INJERR_LBA_OFF;
1687 memset(&phba->lpfc_injerr_wwpn,
1688 0, sizeof(struct lpfc_name));
1691 rc = BG_ERR_INIT | BG_ERR_SWAP;
1692 /* Signals the caller to swap CRC->CSUM */
1694 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1695 "0818 BLKGRD: Injecting guard error: "
1696 "read lba x%lx\n", (unsigned long)lba);
1706 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1707 * the specified SCSI command.
1708 * @phba: The Hba for which this call is being executed.
1709 * @sc: The SCSI command to examine
1710 * @txopt: (out) BlockGuard operation for transmitted data
1711 * @rxopt: (out) BlockGuard operation for received data
1713 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1717 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1718 uint8_t *txop, uint8_t *rxop)
1720 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1723 if (guard_type == SHOST_DIX_GUARD_IP) {
1724 switch (scsi_get_prot_op(sc)) {
1725 case SCSI_PROT_READ_INSERT:
1726 case SCSI_PROT_WRITE_STRIP:
1727 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1728 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1731 case SCSI_PROT_READ_STRIP:
1732 case SCSI_PROT_WRITE_INSERT:
1733 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1734 *txop = BG_OP_IN_NODIF_OUT_CRC;
1737 case SCSI_PROT_READ_PASS:
1738 case SCSI_PROT_WRITE_PASS:
1739 *rxop = BG_OP_IN_CRC_OUT_CSUM;
1740 *txop = BG_OP_IN_CSUM_OUT_CRC;
1743 case SCSI_PROT_NORMAL:
1745 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1746 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1747 scsi_get_prot_op(sc));
1753 switch (scsi_get_prot_op(sc)) {
1754 case SCSI_PROT_READ_STRIP:
1755 case SCSI_PROT_WRITE_INSERT:
1756 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1757 *txop = BG_OP_IN_NODIF_OUT_CRC;
1760 case SCSI_PROT_READ_PASS:
1761 case SCSI_PROT_WRITE_PASS:
1762 *rxop = BG_OP_IN_CRC_OUT_CRC;
1763 *txop = BG_OP_IN_CRC_OUT_CRC;
1766 case SCSI_PROT_READ_INSERT:
1767 case SCSI_PROT_WRITE_STRIP:
1768 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1769 *txop = BG_OP_IN_CRC_OUT_NODIF;
1772 case SCSI_PROT_NORMAL:
1774 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1775 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1776 scsi_get_prot_op(sc));
1785 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1787 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1788 * the specified SCSI command in order to force a guard tag error.
1789 * @phba: The Hba for which this call is being executed.
1790 * @sc: The SCSI command to examine
1791 * @txopt: (out) BlockGuard operation for transmitted data
1792 * @rxopt: (out) BlockGuard operation for received data
1794 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1798 lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1799 uint8_t *txop, uint8_t *rxop)
1801 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1804 if (guard_type == SHOST_DIX_GUARD_IP) {
1805 switch (scsi_get_prot_op(sc)) {
1806 case SCSI_PROT_READ_INSERT:
1807 case SCSI_PROT_WRITE_STRIP:
1808 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1809 *txop = BG_OP_IN_CRC_OUT_NODIF;
1812 case SCSI_PROT_READ_STRIP:
1813 case SCSI_PROT_WRITE_INSERT:
1814 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1815 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1818 case SCSI_PROT_READ_PASS:
1819 case SCSI_PROT_WRITE_PASS:
1820 *rxop = BG_OP_IN_CSUM_OUT_CRC;
1821 *txop = BG_OP_IN_CRC_OUT_CSUM;
1824 case SCSI_PROT_NORMAL:
1830 switch (scsi_get_prot_op(sc)) {
1831 case SCSI_PROT_READ_STRIP:
1832 case SCSI_PROT_WRITE_INSERT:
1833 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1834 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1837 case SCSI_PROT_READ_PASS:
1838 case SCSI_PROT_WRITE_PASS:
1839 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
1840 *txop = BG_OP_IN_CSUM_OUT_CSUM;
1843 case SCSI_PROT_READ_INSERT:
1844 case SCSI_PROT_WRITE_STRIP:
1845 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1846 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1849 case SCSI_PROT_NORMAL:
1860 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1861 * @phba: The Hba for which this call is being executed.
1862 * @sc: pointer to scsi command we're working on
1863 * @bpl: pointer to buffer list for protection groups
1864 * @datacnt: number of segments of data that have been dma mapped
1866 * This function sets up BPL buffer list for protection groups of
1867 * type LPFC_PG_TYPE_NO_DIF
1869 * This is usually used when the HBA is instructed to generate
1870 * DIFs and insert them into data stream (or strip DIF from
1871 * incoming data stream)
1873 * The buffer list consists of just one protection group described
1875 * +-------------------------+
1876 * start of prot group --> | PDE_5 |
1877 * +-------------------------+
1879 * +-------------------------+
1881 * +-------------------------+
1882 * |more Data BDE's ... (opt)|
1883 * +-------------------------+
1886 * Note: Data s/g buffers have been dma mapped
1888 * Returns the number of BDEs added to the BPL.
1891 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1892 struct ulp_bde64 *bpl, int datasegcnt)
1894 struct scatterlist *sgde = NULL; /* s/g data entry */
1895 struct lpfc_pde5 *pde5 = NULL;
1896 struct lpfc_pde6 *pde6 = NULL;
1897 dma_addr_t physaddr;
1898 int i = 0, num_bde = 0, status;
1899 int datadir = sc->sc_data_direction;
1901 uint32_t checking = 1;
1906 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1910 /* extract some info from the scsi command for pde*/
1911 blksize = lpfc_cmd_blksize(sc);
1912 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
1914 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1915 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1917 if (rc & BG_ERR_SWAP)
1918 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1919 if (rc & BG_ERR_CHECK)
1924 /* setup PDE5 with what we have */
1925 pde5 = (struct lpfc_pde5 *) bpl;
1926 memset(pde5, 0, sizeof(struct lpfc_pde5));
1927 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1929 /* Endianness conversion if necessary for PDE5 */
1930 pde5->word0 = cpu_to_le32(pde5->word0);
1931 pde5->reftag = cpu_to_le32(reftag);
1933 /* advance bpl and increment bde count */
1936 pde6 = (struct lpfc_pde6 *) bpl;
1938 /* setup PDE6 with the rest of the info */
1939 memset(pde6, 0, sizeof(struct lpfc_pde6));
1940 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1941 bf_set(pde6_optx, pde6, txop);
1942 bf_set(pde6_oprx, pde6, rxop);
1943 if (datadir == DMA_FROM_DEVICE) {
1944 bf_set(pde6_ce, pde6, checking);
1945 bf_set(pde6_re, pde6, checking);
1947 bf_set(pde6_ai, pde6, 1);
1948 bf_set(pde6_ae, pde6, 0);
1949 bf_set(pde6_apptagval, pde6, 0);
1951 /* Endianness conversion if necessary for PDE6 */
1952 pde6->word0 = cpu_to_le32(pde6->word0);
1953 pde6->word1 = cpu_to_le32(pde6->word1);
1954 pde6->word2 = cpu_to_le32(pde6->word2);
1956 /* advance bpl and increment bde count */
1960 /* assumption: caller has already run dma_map_sg on command data */
1961 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1962 physaddr = sg_dma_address(sgde);
1963 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1964 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1965 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1966 if (datadir == DMA_TO_DEVICE)
1967 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1969 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1970 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1980 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1981 * @phba: The Hba for which this call is being executed.
1982 * @sc: pointer to scsi command we're working on
1983 * @bpl: pointer to buffer list for protection groups
1984 * @datacnt: number of segments of data that have been dma mapped
1985 * @protcnt: number of segment of protection data that have been dma mapped
1987 * This function sets up BPL buffer list for protection groups of
1988 * type LPFC_PG_TYPE_DIF
1990 * This is usually used when DIFs are in their own buffers,
1991 * separate from the data. The HBA can then by instructed
1992 * to place the DIFs in the outgoing stream. For read operations,
1993 * The HBA could extract the DIFs and place it in DIF buffers.
1995 * The buffer list for this type consists of one or more of the
1996 * protection groups described below:
1997 * +-------------------------+
1998 * start of first prot group --> | PDE_5 |
1999 * +-------------------------+
2001 * +-------------------------+
2002 * | PDE_7 (Prot BDE) |
2003 * +-------------------------+
2005 * +-------------------------+
2006 * |more Data BDE's ... (opt)|
2007 * +-------------------------+
2008 * start of new prot group --> | PDE_5 |
2009 * +-------------------------+
2011 * +-------------------------+
2013 * Note: It is assumed that both data and protection s/g buffers have been
2016 * Returns the number of BDEs added to the BPL.
2019 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2020 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2022 struct scatterlist *sgde = NULL; /* s/g data entry */
2023 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2024 struct lpfc_pde5 *pde5 = NULL;
2025 struct lpfc_pde6 *pde6 = NULL;
2026 struct lpfc_pde7 *pde7 = NULL;
2027 dma_addr_t dataphysaddr, protphysaddr;
2028 unsigned short curr_data = 0, curr_prot = 0;
2029 unsigned int split_offset;
2030 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2031 unsigned int protgrp_blks, protgrp_bytes;
2032 unsigned int remainder, subtotal;
2034 int datadir = sc->sc_data_direction;
2035 unsigned char pgdone = 0, alldone = 0;
2038 uint32_t checking = 1;
2043 sgpe = scsi_prot_sglist(sc);
2044 sgde = scsi_sglist(sc);
2046 if (!sgpe || !sgde) {
2047 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2048 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2053 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2057 /* extract some info from the scsi command */
2058 blksize = lpfc_cmd_blksize(sc);
2059 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2061 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2062 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2064 if (rc & BG_ERR_SWAP)
2065 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2066 if (rc & BG_ERR_CHECK)
2073 /* setup PDE5 with what we have */
2074 pde5 = (struct lpfc_pde5 *) bpl;
2075 memset(pde5, 0, sizeof(struct lpfc_pde5));
2076 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
2078 /* Endianness conversion if necessary for PDE5 */
2079 pde5->word0 = cpu_to_le32(pde5->word0);
2080 pde5->reftag = cpu_to_le32(reftag);
2082 /* advance bpl and increment bde count */
2085 pde6 = (struct lpfc_pde6 *) bpl;
2087 /* setup PDE6 with the rest of the info */
2088 memset(pde6, 0, sizeof(struct lpfc_pde6));
2089 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2090 bf_set(pde6_optx, pde6, txop);
2091 bf_set(pde6_oprx, pde6, rxop);
2092 bf_set(pde6_ce, pde6, checking);
2093 bf_set(pde6_re, pde6, checking);
2094 bf_set(pde6_ai, pde6, 1);
2095 bf_set(pde6_ae, pde6, 0);
2096 bf_set(pde6_apptagval, pde6, 0);
2098 /* Endianness conversion if necessary for PDE6 */
2099 pde6->word0 = cpu_to_le32(pde6->word0);
2100 pde6->word1 = cpu_to_le32(pde6->word1);
2101 pde6->word2 = cpu_to_le32(pde6->word2);
2103 /* advance bpl and increment bde count */
2107 /* setup the first BDE that points to protection buffer */
2108 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2109 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2111 /* must be integer multiple of the DIF block length */
2112 BUG_ON(protgroup_len % 8);
2114 pde7 = (struct lpfc_pde7 *) bpl;
2115 memset(pde7, 0, sizeof(struct lpfc_pde7));
2116 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2118 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2119 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
2121 protgrp_blks = protgroup_len / 8;
2122 protgrp_bytes = protgrp_blks * blksize;
2124 /* check if this pde is crossing the 4K boundary; if so split */
2125 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2126 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2127 protgroup_offset += protgroup_remainder;
2128 protgrp_blks = protgroup_remainder / 8;
2129 protgrp_bytes = protgrp_blks * blksize;
2131 protgroup_offset = 0;
2137 /* setup BDE's for data blocks associated with DIF data */
2139 subtotal = 0; /* total bytes processed for current prot grp */
2142 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2143 "9065 BLKGRD:%s Invalid data segment\n",
2148 dataphysaddr = sg_dma_address(sgde) + split_offset;
2149 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2150 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2152 remainder = sg_dma_len(sgde) - split_offset;
2154 if ((subtotal + remainder) <= protgrp_bytes) {
2155 /* we can use this whole buffer */
2156 bpl->tus.f.bdeSize = remainder;
2159 if ((subtotal + remainder) == protgrp_bytes)
2162 /* must split this buffer with next prot grp */
2163 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2164 split_offset += bpl->tus.f.bdeSize;
2167 subtotal += bpl->tus.f.bdeSize;
2169 if (datadir == DMA_TO_DEVICE)
2170 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2172 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2173 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2181 /* Move to the next s/g segment if possible */
2182 sgde = sg_next(sgde);
2186 if (protgroup_offset) {
2187 /* update the reference tag */
2188 reftag += protgrp_blks;
2194 if (curr_prot == protcnt) {
2196 } else if (curr_prot < protcnt) {
2197 /* advance to next prot buffer */
2198 sgpe = sg_next(sgpe);
2201 /* update the reference tag */
2202 reftag += protgrp_blks;
2204 /* if we're here, we have a bug */
2205 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2206 "9054 BLKGRD: bug in %s\n", __func__);
2216 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2217 * @phba: The Hba for which this call is being executed.
2218 * @sc: pointer to scsi command we're working on
2219 * @sgl: pointer to buffer list for protection groups
2220 * @datacnt: number of segments of data that have been dma mapped
2222 * This function sets up SGL buffer list for protection groups of
2223 * type LPFC_PG_TYPE_NO_DIF
2225 * This is usually used when the HBA is instructed to generate
2226 * DIFs and insert them into data stream (or strip DIF from
2227 * incoming data stream)
2229 * The buffer list consists of just one protection group described
2231 * +-------------------------+
2232 * start of prot group --> | DI_SEED |
2233 * +-------------------------+
2235 * +-------------------------+
2236 * |more Data SGE's ... (opt)|
2237 * +-------------------------+
2240 * Note: Data s/g buffers have been dma mapped
2242 * Returns the number of SGEs added to the SGL.
2245 lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2246 struct sli4_sge *sgl, int datasegcnt)
2248 struct scatterlist *sgde = NULL; /* s/g data entry */
2249 struct sli4_sge_diseed *diseed = NULL;
2250 dma_addr_t physaddr;
2251 int i = 0, num_sge = 0, status;
2252 int datadir = sc->sc_data_direction;
2257 uint32_t checking = 1;
2259 uint32_t dma_offset = 0;
2261 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2265 /* extract some info from the scsi command for pde*/
2266 blksize = lpfc_cmd_blksize(sc);
2267 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2269 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2270 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2272 if (rc & BG_ERR_SWAP)
2273 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2274 if (rc & BG_ERR_CHECK)
2279 /* setup DISEED with what we have */
2280 diseed = (struct sli4_sge_diseed *) sgl;
2281 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2282 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2284 /* Endianness conversion if necessary */
2285 diseed->ref_tag = cpu_to_le32(reftag);
2286 diseed->ref_tag_tran = diseed->ref_tag;
2288 /* setup DISEED with the rest of the info */
2289 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2290 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2291 if (datadir == DMA_FROM_DEVICE) {
2292 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2293 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2295 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2296 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2298 /* Endianness conversion if necessary for DISEED */
2299 diseed->word2 = cpu_to_le32(diseed->word2);
2300 diseed->word3 = cpu_to_le32(diseed->word3);
2302 /* advance bpl and increment sge count */
2306 /* assumption: caller has already run dma_map_sg on command data */
2307 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2308 physaddr = sg_dma_address(sgde);
2309 dma_len = sg_dma_len(sgde);
2310 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2311 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2312 if ((i + 1) == datasegcnt)
2313 bf_set(lpfc_sli4_sge_last, sgl, 1);
2315 bf_set(lpfc_sli4_sge_last, sgl, 0);
2316 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2317 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2319 sgl->sge_len = cpu_to_le32(dma_len);
2320 dma_offset += dma_len;
2331 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2332 * @phba: The Hba for which this call is being executed.
2333 * @sc: pointer to scsi command we're working on
2334 * @sgl: pointer to buffer list for protection groups
2335 * @datacnt: number of segments of data that have been dma mapped
2336 * @protcnt: number of segment of protection data that have been dma mapped
2338 * This function sets up SGL buffer list for protection groups of
2339 * type LPFC_PG_TYPE_DIF
2341 * This is usually used when DIFs are in their own buffers,
2342 * separate from the data. The HBA can then by instructed
2343 * to place the DIFs in the outgoing stream. For read operations,
2344 * The HBA could extract the DIFs and place it in DIF buffers.
2346 * The buffer list for this type consists of one or more of the
2347 * protection groups described below:
2348 * +-------------------------+
2349 * start of first prot group --> | DISEED |
2350 * +-------------------------+
2351 * | DIF (Prot SGE) |
2352 * +-------------------------+
2354 * +-------------------------+
2355 * |more Data SGE's ... (opt)|
2356 * +-------------------------+
2357 * start of new prot group --> | DISEED |
2358 * +-------------------------+
2360 * +-------------------------+
2362 * Note: It is assumed that both data and protection s/g buffers have been
2365 * Returns the number of SGEs added to the SGL.
2368 lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2369 struct sli4_sge *sgl, int datacnt, int protcnt)
2371 struct scatterlist *sgde = NULL; /* s/g data entry */
2372 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2373 struct sli4_sge_diseed *diseed = NULL;
2374 dma_addr_t dataphysaddr, protphysaddr;
2375 unsigned short curr_data = 0, curr_prot = 0;
2376 unsigned int split_offset;
2377 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2378 unsigned int protgrp_blks, protgrp_bytes;
2379 unsigned int remainder, subtotal;
2381 unsigned char pgdone = 0, alldone = 0;
2387 uint32_t checking = 1;
2388 uint32_t dma_offset = 0;
2391 sgpe = scsi_prot_sglist(sc);
2392 sgde = scsi_sglist(sc);
2394 if (!sgpe || !sgde) {
2395 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2396 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2401 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2405 /* extract some info from the scsi command */
2406 blksize = lpfc_cmd_blksize(sc);
2407 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2409 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2410 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2412 if (rc & BG_ERR_SWAP)
2413 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2414 if (rc & BG_ERR_CHECK)
2421 /* setup DISEED with what we have */
2422 diseed = (struct sli4_sge_diseed *) sgl;
2423 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2424 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2426 /* Endianness conversion if necessary */
2427 diseed->ref_tag = cpu_to_le32(reftag);
2428 diseed->ref_tag_tran = diseed->ref_tag;
2430 /* setup DISEED with the rest of the info */
2431 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2432 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2433 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2434 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2435 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2436 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2438 /* Endianness conversion if necessary for DISEED */
2439 diseed->word2 = cpu_to_le32(diseed->word2);
2440 diseed->word3 = cpu_to_le32(diseed->word3);
2442 /* advance sgl and increment bde count */
2446 /* setup the first BDE that points to protection buffer */
2447 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2448 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2450 /* must be integer multiple of the DIF block length */
2451 BUG_ON(protgroup_len % 8);
2453 /* Now setup DIF SGE */
2455 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2456 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2457 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2458 sgl->word2 = cpu_to_le32(sgl->word2);
2460 protgrp_blks = protgroup_len / 8;
2461 protgrp_bytes = protgrp_blks * blksize;
2463 /* check if DIF SGE is crossing the 4K boundary; if so split */
2464 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2465 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2466 protgroup_offset += protgroup_remainder;
2467 protgrp_blks = protgroup_remainder / 8;
2468 protgrp_bytes = protgrp_blks * blksize;
2470 protgroup_offset = 0;
2476 /* setup SGE's for data blocks associated with DIF data */
2478 subtotal = 0; /* total bytes processed for current prot grp */
2481 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2482 "9086 BLKGRD:%s Invalid data segment\n",
2487 dataphysaddr = sg_dma_address(sgde) + split_offset;
2489 remainder = sg_dma_len(sgde) - split_offset;
2491 if ((subtotal + remainder) <= protgrp_bytes) {
2492 /* we can use this whole buffer */
2493 dma_len = remainder;
2496 if ((subtotal + remainder) == protgrp_bytes)
2499 /* must split this buffer with next prot grp */
2500 dma_len = protgrp_bytes - subtotal;
2501 split_offset += dma_len;
2504 subtotal += dma_len;
2506 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2507 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2508 bf_set(lpfc_sli4_sge_last, sgl, 0);
2509 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2510 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2512 sgl->sge_len = cpu_to_le32(dma_len);
2513 dma_offset += dma_len;
2521 /* Move to the next s/g segment if possible */
2522 sgde = sg_next(sgde);
2525 if (protgroup_offset) {
2526 /* update the reference tag */
2527 reftag += protgrp_blks;
2533 if (curr_prot == protcnt) {
2534 bf_set(lpfc_sli4_sge_last, sgl, 1);
2536 } else if (curr_prot < protcnt) {
2537 /* advance to next prot buffer */
2538 sgpe = sg_next(sgpe);
2541 /* update the reference tag */
2542 reftag += protgrp_blks;
2544 /* if we're here, we have a bug */
2545 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2546 "9085 BLKGRD: bug in %s\n", __func__);
2557 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2558 * @phba: The Hba for which this call is being executed.
2559 * @sc: pointer to scsi command we're working on
2561 * Given a SCSI command that supports DIF, determine composition of protection
2562 * groups involved in setting up buffer lists
2564 * Returns: Protection group type (with or without DIF)
2568 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2570 int ret = LPFC_PG_TYPE_INVALID;
2571 unsigned char op = scsi_get_prot_op(sc);
2574 case SCSI_PROT_READ_STRIP:
2575 case SCSI_PROT_WRITE_INSERT:
2576 ret = LPFC_PG_TYPE_NO_DIF;
2578 case SCSI_PROT_READ_INSERT:
2579 case SCSI_PROT_WRITE_STRIP:
2580 case SCSI_PROT_READ_PASS:
2581 case SCSI_PROT_WRITE_PASS:
2582 ret = LPFC_PG_TYPE_DIF_BUF;
2585 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2586 "9021 Unsupported protection op:%d\n", op);
2594 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2595 * @phba: The Hba for which this call is being executed.
2596 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2598 * This is the protection/DIF aware version of
2599 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2600 * two functions eventually, but for now, it's here
2603 lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2604 struct lpfc_scsi_buf *lpfc_cmd)
2606 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2607 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2608 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2609 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2610 uint32_t num_bde = 0;
2611 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2612 int prot_group_type = 0;
2617 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2618 * fcp_rsp regions to the first data bde entry
2621 if (scsi_sg_count(scsi_cmnd)) {
2623 * The driver stores the segment count returned from pci_map_sg
2624 * because this a count of dma-mappings used to map the use_sg
2625 * pages. They are not guaranteed to be the same for those
2626 * architectures that implement an IOMMU.
2628 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2629 scsi_sglist(scsi_cmnd),
2630 scsi_sg_count(scsi_cmnd), datadir);
2631 if (unlikely(!datasegcnt))
2634 lpfc_cmd->seg_cnt = datasegcnt;
2635 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2636 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2637 "9067 BLKGRD: %s: Too many sg segments"
2638 " from dma_map_sg. Config %d, seg_cnt"
2640 __func__, phba->cfg_sg_seg_cnt,
2642 scsi_dma_unmap(scsi_cmnd);
2646 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2648 switch (prot_group_type) {
2649 case LPFC_PG_TYPE_NO_DIF:
2650 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2652 /* we should have 2 or more entries in buffer list */
2656 case LPFC_PG_TYPE_DIF_BUF:{
2658 * This type indicates that protection buffers are
2659 * passed to the driver, so that needs to be prepared
2662 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2663 scsi_prot_sglist(scsi_cmnd),
2664 scsi_prot_sg_count(scsi_cmnd), datadir);
2665 if (unlikely(!protsegcnt)) {
2666 scsi_dma_unmap(scsi_cmnd);
2670 lpfc_cmd->prot_seg_cnt = protsegcnt;
2671 if (lpfc_cmd->prot_seg_cnt
2672 > phba->cfg_prot_sg_seg_cnt) {
2673 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2674 "9068 BLKGRD: %s: Too many prot sg "
2675 "segments from dma_map_sg. Config %d,"
2676 "prot_seg_cnt %d\n", __func__,
2677 phba->cfg_prot_sg_seg_cnt,
2678 lpfc_cmd->prot_seg_cnt);
2679 dma_unmap_sg(&phba->pcidev->dev,
2680 scsi_prot_sglist(scsi_cmnd),
2681 scsi_prot_sg_count(scsi_cmnd),
2683 scsi_dma_unmap(scsi_cmnd);
2687 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2688 datasegcnt, protsegcnt);
2689 /* we should have 3 or more entries in buffer list */
2694 case LPFC_PG_TYPE_INVALID:
2696 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2697 "9022 Unexpected protection group %i\n",
2704 * Finish initializing those IOCB fields that are dependent on the
2705 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2706 * reinitialized since all iocb memory resources are used many times
2707 * for transmit, receive, and continuation bpl's.
2709 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2710 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2711 iocb_cmd->ulpBdeCount = 1;
2712 iocb_cmd->ulpLe = 1;
2714 fcpdl = scsi_bufflen(scsi_cmnd);
2716 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
2718 * We are in DIF Type 1 mode
2719 * Every data block has a 8 byte DIF (trailer)
2720 * attached to it. Must ajust FCP data length
2722 blksize = lpfc_cmd_blksize(scsi_cmnd);
2723 diflen = (fcpdl / blksize) * 8;
2726 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2729 * Due to difference in data length between DIF/non-DIF paths,
2730 * we need to set word 4 of IOCB here
2732 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2736 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2737 "9023 Could not setup all needed BDE's"
2738 "prot_group_type=%d, num_bde=%d\n",
2739 prot_group_type, num_bde);
2744 * This function checks for BlockGuard errors detected by
2745 * the HBA. In case of errors, the ASC/ASCQ fields in the
2746 * sense buffer will be set accordingly, paired with
2747 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2748 * detected corruption.
2751 * 0 - No error found
2752 * 1 - BlockGuard error found
2753 * -1 - Internal error (bad profile, ...etc)
2756 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
2757 struct lpfc_iocbq *pIocbOut)
2759 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2760 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2762 uint32_t bghm = bgf->bghm;
2763 uint32_t bgstat = bgf->bgstat;
2764 uint64_t failing_sector = 0;
2766 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
2767 " 0x%x lba 0x%llx blk cnt 0x%x "
2768 "bgstat=0x%x bghm=0x%x\n",
2769 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
2770 blk_rq_sectors(cmd->request), bgstat, bghm);
2772 spin_lock(&_dump_buf_lock);
2773 if (!_dump_buf_done) {
2774 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
2775 " Data for %u blocks to debugfs\n",
2776 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2777 lpfc_debug_save_data(phba, cmd);
2779 /* If we have a prot sgl, save the DIF buffer */
2780 if (lpfc_prot_group_type(phba, cmd) ==
2781 LPFC_PG_TYPE_DIF_BUF) {
2782 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
2783 "Saving DIF for %u blocks to debugfs\n",
2784 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
2785 lpfc_debug_save_dif(phba, cmd);
2790 spin_unlock(&_dump_buf_lock);
2792 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2793 cmd->result = ScsiResult(DID_ERROR, 0);
2794 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
2795 " BlockGuard profile. bgstat:0x%x\n",
2801 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2802 cmd->result = ScsiResult(DID_ERROR, 0);
2803 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
2804 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
2810 if (lpfc_bgs_get_guard_err(bgstat)) {
2813 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2815 cmd->result = DRIVER_SENSE << 24
2816 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2817 phba->bg_guard_err_cnt++;
2818 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2819 "9055 BLKGRD: guard_tag error\n");
2822 if (lpfc_bgs_get_reftag_err(bgstat)) {
2825 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2827 cmd->result = DRIVER_SENSE << 24
2828 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2830 phba->bg_reftag_err_cnt++;
2831 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2832 "9056 BLKGRD: ref_tag error\n");
2835 if (lpfc_bgs_get_apptag_err(bgstat)) {
2838 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2840 cmd->result = DRIVER_SENSE << 24
2841 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
2843 phba->bg_apptag_err_cnt++;
2844 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2845 "9061 BLKGRD: app_tag error\n");
2848 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2850 * setup sense data descriptor 0 per SPC-4 as an information
2851 * field, and put the failing LBA in it.
2852 * This code assumes there was also a guard/app/ref tag error
2855 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2856 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2857 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2858 cmd->sense_buffer[10] = 0x80; /* Validity bit */
2860 /* bghm is a "on the wire" FC frame based count */
2861 switch (scsi_get_prot_op(cmd)) {
2862 case SCSI_PROT_READ_INSERT:
2863 case SCSI_PROT_WRITE_STRIP:
2864 bghm /= cmd->device->sector_size;
2866 case SCSI_PROT_READ_STRIP:
2867 case SCSI_PROT_WRITE_INSERT:
2868 case SCSI_PROT_READ_PASS:
2869 case SCSI_PROT_WRITE_PASS:
2870 bghm /= (cmd->device->sector_size +
2871 sizeof(struct scsi_dif_tuple));
2875 failing_sector = scsi_get_lba(cmd);
2876 failing_sector += bghm;
2878 /* Descriptor Information */
2879 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
2883 /* No error was reported - problem in FW? */
2884 cmd->result = ScsiResult(DID_ERROR, 0);
2885 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2886 "9057 BLKGRD: Unknown error reported!\n");
2894 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
2895 * @phba: The Hba for which this call is being executed.
2896 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2898 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2899 * field of @lpfc_cmd for device with SLI-4 interface spec.
2906 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2908 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2909 struct scatterlist *sgel = NULL;
2910 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2911 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
2912 struct sli4_sge *first_data_sgl;
2913 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2914 dma_addr_t physaddr;
2915 uint32_t num_bde = 0;
2917 uint32_t dma_offset = 0;
2919 struct ulp_bde64 *bde;
2922 * There are three possibilities here - use scatter-gather segment, use
2923 * the single mapping, or neither. Start the lpfc command prep by
2924 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2927 if (scsi_sg_count(scsi_cmnd)) {
2929 * The driver stores the segment count returned from pci_map_sg
2930 * because this a count of dma-mappings used to map the use_sg
2931 * pages. They are not guaranteed to be the same for those
2932 * architectures that implement an IOMMU.
2935 nseg = scsi_dma_map(scsi_cmnd);
2936 if (unlikely(!nseg))
2939 /* clear the last flag in the fcp_rsp map entry */
2940 sgl->word2 = le32_to_cpu(sgl->word2);
2941 bf_set(lpfc_sli4_sge_last, sgl, 0);
2942 sgl->word2 = cpu_to_le32(sgl->word2);
2944 first_data_sgl = sgl;
2945 lpfc_cmd->seg_cnt = nseg;
2946 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2947 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2948 " %s: Too many sg segments from "
2949 "dma_map_sg. Config %d, seg_cnt %d\n",
2950 __func__, phba->cfg_sg_seg_cnt,
2952 scsi_dma_unmap(scsi_cmnd);
2957 * The driver established a maximum scatter-gather segment count
2958 * during probe that limits the number of sg elements in any
2959 * single scsi command. Just run through the seg_cnt and format
2961 * When using SLI-3 the driver will try to fit all the BDEs into
2962 * the IOCB. If it can't then the BDEs get added to a BPL as it
2963 * does for SLI-2 mode.
2965 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2966 physaddr = sg_dma_address(sgel);
2967 dma_len = sg_dma_len(sgel);
2968 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2969 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2970 sgl->word2 = le32_to_cpu(sgl->word2);
2971 if ((num_bde + 1) == nseg)
2972 bf_set(lpfc_sli4_sge_last, sgl, 1);
2974 bf_set(lpfc_sli4_sge_last, sgl, 0);
2975 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2976 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2977 sgl->word2 = cpu_to_le32(sgl->word2);
2978 sgl->sge_len = cpu_to_le32(dma_len);
2979 dma_offset += dma_len;
2982 /* setup the performance hint (first data BDE) if enabled */
2983 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2984 bde = (struct ulp_bde64 *)
2985 &(iocb_cmd->unsli3.sli3Words[5]);
2986 bde->addrLow = first_data_sgl->addr_lo;
2987 bde->addrHigh = first_data_sgl->addr_hi;
2988 bde->tus.f.bdeSize =
2989 le32_to_cpu(first_data_sgl->sge_len);
2990 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2991 bde->tus.w = cpu_to_le32(bde->tus.w);
2995 /* clear the last flag in the fcp_rsp map entry */
2996 sgl->word2 = le32_to_cpu(sgl->word2);
2997 bf_set(lpfc_sli4_sge_last, sgl, 1);
2998 sgl->word2 = cpu_to_le32(sgl->word2);
3002 * Finish initializing those IOCB fields that are dependent on the
3003 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3004 * explicitly reinitialized.
3005 * all iocb memory resources are reused.
3007 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3010 * Due to difference in data length between DIF/non-DIF paths,
3011 * we need to set word 4 of IOCB here
3013 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3018 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
3019 * @phba: The Hba for which this call is being executed.
3020 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
3022 * Adjust the data length to account for how much data
3023 * is actually on the wire.
3025 * returns the adjusted data length
3028 lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
3029 struct lpfc_scsi_buf *lpfc_cmd)
3031 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
3035 fcpdl = scsi_bufflen(sc);
3037 /* Check if there is protection data on the wire */
3038 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
3040 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
3045 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
3049 /* If protection data on the wire, adjust the count accordingly */
3050 blksize = lpfc_cmd_blksize(sc);
3051 diflen = (fcpdl / blksize) * 8;
3057 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3058 * @phba: The Hba for which this call is being executed.
3059 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3061 * This is the protection/DIF aware version of
3062 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3063 * two functions eventually, but for now, it's here
3066 lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3067 struct lpfc_scsi_buf *lpfc_cmd)
3069 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3070 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3071 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3072 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3073 uint32_t num_bde = 0;
3074 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3075 int prot_group_type = 0;
3079 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3080 * fcp_rsp regions to the first data bde entry
3082 if (scsi_sg_count(scsi_cmnd)) {
3084 * The driver stores the segment count returned from pci_map_sg
3085 * because this a count of dma-mappings used to map the use_sg
3086 * pages. They are not guaranteed to be the same for those
3087 * architectures that implement an IOMMU.
3089 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3090 scsi_sglist(scsi_cmnd),
3091 scsi_sg_count(scsi_cmnd), datadir);
3092 if (unlikely(!datasegcnt))
3096 /* clear the last flag in the fcp_rsp map entry */
3097 sgl->word2 = le32_to_cpu(sgl->word2);
3098 bf_set(lpfc_sli4_sge_last, sgl, 0);
3099 sgl->word2 = cpu_to_le32(sgl->word2);
3102 lpfc_cmd->seg_cnt = datasegcnt;
3103 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3104 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3105 "9087 BLKGRD: %s: Too many sg segments"
3106 " from dma_map_sg. Config %d, seg_cnt"
3108 __func__, phba->cfg_sg_seg_cnt,
3110 scsi_dma_unmap(scsi_cmnd);
3114 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3116 switch (prot_group_type) {
3117 case LPFC_PG_TYPE_NO_DIF:
3118 num_bde = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3120 /* we should have 2 or more entries in buffer list */
3124 case LPFC_PG_TYPE_DIF_BUF:{
3126 * This type indicates that protection buffers are
3127 * passed to the driver, so that needs to be prepared
3130 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3131 scsi_prot_sglist(scsi_cmnd),
3132 scsi_prot_sg_count(scsi_cmnd), datadir);
3133 if (unlikely(!protsegcnt)) {
3134 scsi_dma_unmap(scsi_cmnd);
3138 lpfc_cmd->prot_seg_cnt = protsegcnt;
3139 if (lpfc_cmd->prot_seg_cnt
3140 > phba->cfg_prot_sg_seg_cnt) {
3141 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3142 "9088 BLKGRD: %s: Too many prot sg "
3143 "segments from dma_map_sg. Config %d,"
3144 "prot_seg_cnt %d\n", __func__,
3145 phba->cfg_prot_sg_seg_cnt,
3146 lpfc_cmd->prot_seg_cnt);
3147 dma_unmap_sg(&phba->pcidev->dev,
3148 scsi_prot_sglist(scsi_cmnd),
3149 scsi_prot_sg_count(scsi_cmnd),
3151 scsi_dma_unmap(scsi_cmnd);
3155 num_bde = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3156 datasegcnt, protsegcnt);
3157 /* we should have 3 or more entries in buffer list */
3162 case LPFC_PG_TYPE_INVALID:
3164 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3165 "9083 Unexpected protection group %i\n",
3171 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3173 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3176 * Due to difference in data length between DIF/non-DIF paths,
3177 * we need to set word 4 of IOCB here
3179 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3180 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF;
3184 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3185 "9084 Could not setup all needed BDE's"
3186 "prot_group_type=%d, num_bde=%d\n",
3187 prot_group_type, num_bde);
3192 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3193 * @phba: The Hba for which this call is being executed.
3194 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3196 * This routine wraps the actual DMA mapping function pointer from the
3204 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3206 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3210 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3212 * @phba: The Hba for which this call is being executed.
3213 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3215 * This routine wraps the actual DMA mapping function pointer from the
3223 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3225 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3229 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
3230 * @phba: Pointer to hba context object.
3231 * @vport: Pointer to vport object.
3232 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3233 * @rsp_iocb: Pointer to response iocb object which reported error.
3235 * This function posts an event when there is a SCSI command reporting
3236 * error from the scsi device.
3239 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3240 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3241 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3242 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3243 uint32_t resp_info = fcprsp->rspStatus2;
3244 uint32_t scsi_status = fcprsp->rspStatus3;
3245 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3246 struct lpfc_fast_path_event *fast_path_evt = NULL;
3247 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3248 unsigned long flags;
3250 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3253 /* If there is queuefull or busy condition send a scsi event */
3254 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3255 (cmnd->result == SAM_STAT_BUSY)) {
3256 fast_path_evt = lpfc_alloc_fast_evt(phba);
3259 fast_path_evt->un.scsi_evt.event_type =
3261 fast_path_evt->un.scsi_evt.subcategory =
3262 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3263 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3264 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3265 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3266 &pnode->nlp_portname, sizeof(struct lpfc_name));
3267 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3268 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3269 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3270 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3271 fast_path_evt = lpfc_alloc_fast_evt(phba);
3274 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3276 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3277 LPFC_EVENT_CHECK_COND;
3278 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3280 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3281 &pnode->nlp_portname, sizeof(struct lpfc_name));
3282 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3283 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3284 fast_path_evt->un.check_cond_evt.sense_key =
3285 cmnd->sense_buffer[2] & 0xf;
3286 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3287 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3288 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3290 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3291 ((scsi_status == SAM_STAT_GOOD) &&
3292 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3294 * If status is good or resid does not match with fcp_param and
3295 * there is valid fcpi_parm, then there is a read_check error
3297 fast_path_evt = lpfc_alloc_fast_evt(phba);
3300 fast_path_evt->un.read_check_error.header.event_type =
3301 FC_REG_FABRIC_EVENT;
3302 fast_path_evt->un.read_check_error.header.subcategory =
3303 LPFC_EVENT_FCPRDCHKERR;
3304 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3305 &pnode->nlp_portname, sizeof(struct lpfc_name));
3306 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3307 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3308 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3309 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3310 fast_path_evt->un.read_check_error.fcpiparam =
3315 fast_path_evt->vport = vport;
3316 spin_lock_irqsave(&phba->hbalock, flags);
3317 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3318 spin_unlock_irqrestore(&phba->hbalock, flags);
3319 lpfc_worker_wake_up(phba);
3324 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3325 * @phba: The HBA for which this call is being executed.
3326 * @psb: The scsi buffer which is going to be un-mapped.
3328 * This routine does DMA un-mapping of scatter gather list of scsi command
3329 * field of @lpfc_cmd for device with SLI-3 interface spec.
3332 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
3335 * There are only two special cases to consider. (1) the scsi command
3336 * requested scatter-gather usage or (2) the scsi command allocated
3337 * a request buffer, but did not request use_sg. There is a third
3338 * case, but it does not require resource deallocation.
3340 if (psb->seg_cnt > 0)
3341 scsi_dma_unmap(psb->pCmd);
3342 if (psb->prot_seg_cnt > 0)
3343 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3344 scsi_prot_sg_count(psb->pCmd),
3345 psb->pCmd->sc_data_direction);
3349 * lpfc_handler_fcp_err - FCP response handler
3350 * @vport: The virtual port for which this call is being executed.
3351 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3352 * @rsp_iocb: The response IOCB which contains FCP error.
3354 * This routine is called to process response IOCB with status field
3355 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3356 * based upon SCSI and FCP error.
3359 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3360 struct lpfc_iocbq *rsp_iocb)
3362 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3363 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3364 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3365 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3366 uint32_t resp_info = fcprsp->rspStatus2;
3367 uint32_t scsi_status = fcprsp->rspStatus3;
3369 uint32_t host_status = DID_OK;
3370 uint32_t rsplen = 0;
3371 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
3375 * If this is a task management command, there is no
3376 * scsi packet associated with this lpfc_cmd. The driver
3379 if (fcpcmd->fcpCntl2) {
3384 if (resp_info & RSP_LEN_VALID) {
3385 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3386 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
3387 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3388 "2719 Invalid response length: "
3389 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3391 cmnd->device->lun, cmnd->cmnd[0],
3393 host_status = DID_ERROR;
3396 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3397 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3398 "2757 Protocol failure detected during "
3399 "processing of FCP I/O op: "
3400 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3402 cmnd->device->lun, cmnd->cmnd[0],
3404 host_status = DID_ERROR;
3409 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3410 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3411 if (snslen > SCSI_SENSE_BUFFERSIZE)
3412 snslen = SCSI_SENSE_BUFFERSIZE;
3414 if (resp_info & RSP_LEN_VALID)
3415 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3416 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3418 lp = (uint32_t *)cmnd->sense_buffer;
3420 if (!scsi_status && (resp_info & RESID_UNDER) &&
3421 vport->cfg_log_verbose & LOG_FCP_UNDER)
3422 logit = LOG_FCP_UNDER;
3424 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3425 "9024 FCP command x%x failed: x%x SNS x%x x%x "
3426 "Data: x%x x%x x%x x%x x%x\n",
3427 cmnd->cmnd[0], scsi_status,
3428 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3429 be32_to_cpu(fcprsp->rspResId),
3430 be32_to_cpu(fcprsp->rspSnsLen),
3431 be32_to_cpu(fcprsp->rspRspLen),
3434 scsi_set_resid(cmnd, 0);
3435 if (resp_info & RESID_UNDER) {
3436 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
3438 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
3439 "9025 FCP Read Underrun, expected %d, "
3440 "residual %d Data: x%x x%x x%x\n",
3441 be32_to_cpu(fcpcmd->fcpDl),
3442 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3446 * If there is an under run check if under run reported by
3447 * storage array is same as the under run reported by HBA.
3448 * If this is not same, there is a dropped frame.
3450 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3452 (scsi_get_resid(cmnd) != fcpi_parm)) {
3453 lpfc_printf_vlog(vport, KERN_WARNING,
3454 LOG_FCP | LOG_FCP_ERROR,
3455 "9026 FCP Read Check Error "
3456 "and Underrun Data: x%x x%x x%x x%x\n",
3457 be32_to_cpu(fcpcmd->fcpDl),
3458 scsi_get_resid(cmnd), fcpi_parm,
3460 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3461 host_status = DID_ERROR;
3464 * The cmnd->underflow is the minimum number of bytes that must
3465 * be transferred for this command. Provided a sense condition
3466 * is not present, make sure the actual amount transferred is at
3467 * least the underflow value or fail.
3469 if (!(resp_info & SNS_LEN_VALID) &&
3470 (scsi_status == SAM_STAT_GOOD) &&
3471 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3472 < cmnd->underflow)) {
3473 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3474 "9027 FCP command x%x residual "
3475 "underrun converted to error "
3476 "Data: x%x x%x x%x\n",
3477 cmnd->cmnd[0], scsi_bufflen(cmnd),
3478 scsi_get_resid(cmnd), cmnd->underflow);
3479 host_status = DID_ERROR;
3481 } else if (resp_info & RESID_OVER) {
3482 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3483 "9028 FCP command x%x residual overrun error. "
3484 "Data: x%x x%x\n", cmnd->cmnd[0],
3485 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
3486 host_status = DID_ERROR;
3489 * Check SLI validation that all the transfer was actually done
3490 * (fcpi_parm should be zero). Apply check only to reads.
3492 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
3493 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
3494 "9029 FCP Read Check Error Data: "
3495 "x%x x%x x%x x%x x%x\n",
3496 be32_to_cpu(fcpcmd->fcpDl),
3497 be32_to_cpu(fcprsp->rspResId),
3498 fcpi_parm, cmnd->cmnd[0], scsi_status);
3499 switch (scsi_status) {
3501 case SAM_STAT_CHECK_CONDITION:
3502 /* Fabric dropped a data frame. Fail any successful
3503 * command in which we detected dropped frames.
3504 * A status of good or some check conditions could
3505 * be considered a successful command.
3507 host_status = DID_ERROR;
3510 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3514 cmnd->result = ScsiResult(host_status, scsi_status);
3515 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
3519 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
3520 * @phba: The Hba for which this call is being executed.
3521 * @pIocbIn: The command IOCBQ for the scsi cmnd.
3522 * @pIocbOut: The response IOCBQ for the scsi cmnd.
3524 * This routine assigns scsi command result by looking into response IOCB
3525 * status field appropriately. This routine handles QUEUE FULL condition as
3526 * well by ramping down device queue depth.
3529 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3530 struct lpfc_iocbq *pIocbOut)
3532 struct lpfc_scsi_buf *lpfc_cmd =
3533 (struct lpfc_scsi_buf *) pIocbIn->context1;
3534 struct lpfc_vport *vport = pIocbIn->vport;
3535 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3536 struct lpfc_nodelist *pnode = rdata->pnode;
3537 struct scsi_cmnd *cmd;
3539 struct scsi_device *tmp_sdev;
3541 unsigned long flags;
3542 struct lpfc_fast_path_event *fast_path_evt;
3543 struct Scsi_Host *shost;
3544 uint32_t queue_depth, scsi_id;
3545 uint32_t logit = LOG_FCP;
3547 /* Sanity check on return of outstanding command */
3548 if (!(lpfc_cmd->pCmd))
3550 cmd = lpfc_cmd->pCmd;
3551 shost = cmd->device->host;
3553 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
3554 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
3555 /* pick up SLI4 exhange busy status from HBA */
3556 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3558 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3559 if (lpfc_cmd->prot_data_type) {
3560 struct scsi_dif_tuple *src = NULL;
3562 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3564 * Used to restore any changes to protection
3565 * data for error injection.
3567 switch (lpfc_cmd->prot_data_type) {
3568 case LPFC_INJERR_REFTAG:
3570 lpfc_cmd->prot_data;
3572 case LPFC_INJERR_APPTAG:
3574 (uint16_t)lpfc_cmd->prot_data;
3576 case LPFC_INJERR_GUARD:
3578 (uint16_t)lpfc_cmd->prot_data;
3584 lpfc_cmd->prot_data = 0;
3585 lpfc_cmd->prot_data_type = 0;
3586 lpfc_cmd->prot_data_segment = NULL;
3589 if (pnode && NLP_CHK_NODE_ACT(pnode))
3590 atomic_dec(&pnode->cmd_pending);
3592 if (lpfc_cmd->status) {
3593 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3594 (lpfc_cmd->result & IOERR_DRVR_MASK))
3595 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3596 else if (lpfc_cmd->status >= IOSTAT_CNT)
3597 lpfc_cmd->status = IOSTAT_DEFAULT;
3598 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
3599 && !lpfc_cmd->fcp_rsp->rspStatus3
3600 && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
3601 && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
3604 logit = LOG_FCP | LOG_FCP_UNDER;
3605 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3606 "9030 FCP cmd x%x failed <%d/%d> "
3607 "status: x%x result: x%x Data: x%x x%x\n",
3609 cmd->device ? cmd->device->id : 0xffff,
3610 cmd->device ? cmd->device->lun : 0xffff,
3611 lpfc_cmd->status, lpfc_cmd->result,
3612 pIocbOut->iocb.ulpContext,
3613 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
3615 switch (lpfc_cmd->status) {
3616 case IOSTAT_FCP_RSP_ERROR:
3617 /* Call FCP RSP handler to determine result */
3618 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
3620 case IOSTAT_NPORT_BSY:
3621 case IOSTAT_FABRIC_BSY:
3622 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
3623 fast_path_evt = lpfc_alloc_fast_evt(phba);
3626 fast_path_evt->un.fabric_evt.event_type =
3627 FC_REG_FABRIC_EVENT;
3628 fast_path_evt->un.fabric_evt.subcategory =
3629 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3630 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3631 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3632 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3633 &pnode->nlp_portname,
3634 sizeof(struct lpfc_name));
3635 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3636 &pnode->nlp_nodename,
3637 sizeof(struct lpfc_name));
3639 fast_path_evt->vport = vport;
3640 fast_path_evt->work_evt.evt =
3641 LPFC_EVT_FASTPATH_MGMT_EVT;
3642 spin_lock_irqsave(&phba->hbalock, flags);
3643 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3645 spin_unlock_irqrestore(&phba->hbalock, flags);
3646 lpfc_worker_wake_up(phba);
3648 case IOSTAT_LOCAL_REJECT:
3649 case IOSTAT_REMOTE_STOP:
3650 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3652 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3653 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3655 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3656 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
3659 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
3660 lpfc_cmd->result == IOERR_NO_RESOURCES ||
3661 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3662 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
3663 cmd->result = ScsiResult(DID_REQUEUE, 0);
3666 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3667 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3668 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3669 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3671 * This is a response for a BG enabled
3672 * cmd. Parse BG error
3674 lpfc_parse_bg_err(phba, lpfc_cmd,
3678 lpfc_printf_vlog(vport, KERN_WARNING,
3680 "9031 non-zero BGSTAT "
3681 "on unprotected cmd\n");
3684 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3685 && (phba->sli_rev == LPFC_SLI_REV4)
3686 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3687 /* This IO was aborted by the target, we don't
3688 * know the rxid and because we did not send the
3689 * ABTS we cannot generate and RRQ.
3691 lpfc_set_rrq_active(phba, pnode,
3692 lpfc_cmd->cur_iocbq.sli4_xritag,
3695 /* else: fall through */
3697 cmd->result = ScsiResult(DID_ERROR, 0);
3701 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
3702 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3703 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
3706 cmd->result = ScsiResult(DID_OK, 0);
3708 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3709 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
3711 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3712 "0710 Iodone <%d/%d> cmd %p, error "
3713 "x%x SNS x%x x%x Data: x%x x%x\n",
3714 cmd->device->id, cmd->device->lun, cmd,
3715 cmd->result, *lp, *(lp + 3), cmd->retries,
3716 scsi_get_resid(cmd));
3719 lpfc_update_stats(phba, lpfc_cmd);
3720 result = cmd->result;
3721 if (vport->cfg_max_scsicmpl_time &&
3722 time_after(jiffies, lpfc_cmd->start_time +
3723 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
3724 spin_lock_irqsave(shost->host_lock, flags);
3725 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3726 if (pnode->cmd_qdepth >
3727 atomic_read(&pnode->cmd_pending) &&
3728 (atomic_read(&pnode->cmd_pending) >
3729 LPFC_MIN_TGT_QDEPTH) &&
3730 ((cmd->cmnd[0] == READ_10) ||
3731 (cmd->cmnd[0] == WRITE_10)))
3733 atomic_read(&pnode->cmd_pending);
3735 pnode->last_change_time = jiffies;
3737 spin_unlock_irqrestore(shost->host_lock, flags);
3738 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3739 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
3740 time_after(jiffies, pnode->last_change_time +
3741 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
3742 spin_lock_irqsave(shost->host_lock, flags);
3743 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
3745 depth = depth ? depth : 1;
3746 pnode->cmd_qdepth += depth;
3747 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
3748 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
3749 pnode->last_change_time = jiffies;
3750 spin_unlock_irqrestore(shost->host_lock, flags);
3754 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
3756 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
3757 queue_depth = cmd->device->queue_depth;
3758 scsi_id = cmd->device->id;
3759 cmd->scsi_done(cmd);
3761 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3763 * If there is a thread waiting for command completion
3764 * wake up the thread.
3766 spin_lock_irqsave(shost->host_lock, flags);
3767 lpfc_cmd->pCmd = NULL;
3768 if (lpfc_cmd->waitq)
3769 wake_up(lpfc_cmd->waitq);
3770 spin_unlock_irqrestore(shost->host_lock, flags);
3771 lpfc_release_scsi_buf(phba, lpfc_cmd);
3776 lpfc_rampup_queue_depth(vport, queue_depth);
3779 * Check for queue full. If the lun is reporting queue full, then
3780 * back off the lun queue depth to prevent target overloads.
3782 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
3783 NLP_CHK_NODE_ACT(pnode)) {
3784 shost_for_each_device(tmp_sdev, shost) {
3785 if (tmp_sdev->id != scsi_id)
3787 depth = scsi_track_queue_full(tmp_sdev,
3788 tmp_sdev->queue_depth-1);
3791 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3792 "0711 detected queue full - lun queue "
3793 "depth adjusted to %d.\n", depth);
3794 lpfc_send_sdev_queuedepth_change_event(phba, vport,
3802 * If there is a thread waiting for command completion
3803 * wake up the thread.
3805 spin_lock_irqsave(shost->host_lock, flags);
3806 lpfc_cmd->pCmd = NULL;
3807 if (lpfc_cmd->waitq)
3808 wake_up(lpfc_cmd->waitq);
3809 spin_unlock_irqrestore(shost->host_lock, flags);
3811 lpfc_release_scsi_buf(phba, lpfc_cmd);
3815 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
3816 * @data: A pointer to the immediate command data portion of the IOCB.
3817 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
3819 * The routine copies the entire FCP command from @fcp_cmnd to @data while
3820 * byte swapping the data to big endian format for transmission on the wire.
3823 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
3826 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
3827 i += sizeof(uint32_t), j++) {
3828 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
3833 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
3834 * @vport: The virtual port for which this call is being executed.
3835 * @lpfc_cmd: The scsi command which needs to send.
3836 * @pnode: Pointer to lpfc_nodelist.
3838 * This routine initializes fcp_cmnd and iocb data structure from scsi command
3839 * to transfer for device with SLI3 interface spec.
3842 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3843 struct lpfc_nodelist *pnode)
3845 struct lpfc_hba *phba = vport->phba;
3846 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3847 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3848 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3849 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
3850 int datadir = scsi_cmnd->sc_data_direction;
3853 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3856 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
3857 /* clear task management bits */
3858 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
3860 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
3861 &lpfc_cmd->fcp_cmnd->fcp_lun);
3863 memset(&fcp_cmnd->fcpCdb[0], 0, LPFC_FCP_CDB_LEN);
3864 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
3865 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
3867 case HEAD_OF_QUEUE_TAG:
3868 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
3870 case ORDERED_QUEUE_TAG:
3871 fcp_cmnd->fcpCntl1 = ORDERED_Q;
3874 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
3878 fcp_cmnd->fcpCntl1 = 0;
3881 * There are three possibilities here - use scatter-gather segment, use
3882 * the single mapping, or neither. Start the lpfc command prep by
3883 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3886 if (scsi_sg_count(scsi_cmnd)) {
3887 if (datadir == DMA_TO_DEVICE) {
3888 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
3889 if (phba->sli_rev < LPFC_SLI_REV4) {
3890 iocb_cmd->un.fcpi.fcpi_parm = 0;
3891 iocb_cmd->ulpPU = 0;
3893 iocb_cmd->ulpPU = PARM_READ_CHECK;
3894 fcp_cmnd->fcpCntl3 = WRITE_DATA;
3895 phba->fc4OutputRequests++;
3897 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
3898 iocb_cmd->ulpPU = PARM_READ_CHECK;
3899 fcp_cmnd->fcpCntl3 = READ_DATA;
3900 phba->fc4InputRequests++;
3903 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
3904 iocb_cmd->un.fcpi.fcpi_parm = 0;
3905 iocb_cmd->ulpPU = 0;
3906 fcp_cmnd->fcpCntl3 = 0;
3907 phba->fc4ControlRequests++;
3909 if (phba->sli_rev == 3 &&
3910 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
3911 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
3913 * Finish initializing those IOCB fields that are independent
3914 * of the scsi_cmnd request_buffer
3916 piocbq->iocb.ulpContext = pnode->nlp_rpi;
3917 if (phba->sli_rev == LPFC_SLI_REV4)
3918 piocbq->iocb.ulpContext =
3919 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
3920 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
3921 piocbq->iocb.ulpFCP2Rcvy = 1;
3923 piocbq->iocb.ulpFCP2Rcvy = 0;
3925 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
3926 piocbq->context1 = lpfc_cmd;
3927 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
3928 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
3929 piocbq->vport = vport;
3933 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
3934 * @vport: The virtual port for which this call is being executed.
3935 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3936 * @lun: Logical unit number.
3937 * @task_mgmt_cmd: SCSI task management command.
3939 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
3940 * for device with SLI-3 interface spec.
3947 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
3948 struct lpfc_scsi_buf *lpfc_cmd,
3950 uint8_t task_mgmt_cmd)
3952 struct lpfc_iocbq *piocbq;
3954 struct fcp_cmnd *fcp_cmnd;
3955 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3956 struct lpfc_nodelist *ndlp = rdata->pnode;
3958 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
3959 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
3962 piocbq = &(lpfc_cmd->cur_iocbq);
3963 piocbq->vport = vport;
3965 piocb = &piocbq->iocb;
3967 fcp_cmnd = lpfc_cmd->fcp_cmnd;
3968 /* Clear out any old data in the FCP command area */
3969 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
3970 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
3971 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
3972 if (vport->phba->sli_rev == 3 &&
3973 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
3974 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
3975 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
3976 piocb->ulpContext = ndlp->nlp_rpi;
3977 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
3979 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
3981 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
3982 piocb->ulpFCP2Rcvy = 1;
3984 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
3986 /* ulpTimeout is only one byte */
3987 if (lpfc_cmd->timeout > 0xff) {
3989 * Do not timeout the command at the firmware level.
3990 * The driver will provide the timeout mechanism.
3992 piocb->ulpTimeout = 0;
3994 piocb->ulpTimeout = lpfc_cmd->timeout;
3996 if (vport->phba->sli_rev == LPFC_SLI_REV4)
3997 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
4003 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
4004 * @phba: The hba struct for which this call is being executed.
4005 * @dev_grp: The HBA PCI-Device group number.
4007 * This routine sets up the SCSI interface API function jump table in @phba
4009 * Returns: 0 - success, -ENODEV - failure.
4012 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4015 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4016 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
4019 case LPFC_PCI_DEV_LP:
4020 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4021 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
4022 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
4023 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
4024 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
4026 case LPFC_PCI_DEV_OC:
4027 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4028 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
4029 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
4030 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
4031 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
4034 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4035 "1418 Invalid HBA PCI-device group: 0x%x\n",
4040 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
4041 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4046 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
4047 * @phba: The Hba for which this call is being executed.
4048 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4049 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4051 * This routine is IOCB completion routine for device reset and target reset
4052 * routine. This routine release scsi buffer associated with lpfc_cmd.
4055 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4056 struct lpfc_iocbq *cmdiocbq,
4057 struct lpfc_iocbq *rspiocbq)
4059 struct lpfc_scsi_buf *lpfc_cmd =
4060 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4062 lpfc_release_scsi_buf(phba, lpfc_cmd);
4067 * lpfc_info - Info entry point of scsi_host_template data structure
4068 * @host: The scsi host for which this call is being executed.
4070 * This routine provides module information about hba.
4073 * Pointer to char - Success.
4076 lpfc_info(struct Scsi_Host *host)
4078 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4079 struct lpfc_hba *phba = vport->phba;
4081 static char lpfcinfobuf[384];
4083 memset(lpfcinfobuf,0,384);
4084 if (phba && phba->pcidev){
4085 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4086 len = strlen(lpfcinfobuf);
4087 snprintf(lpfcinfobuf + len,
4089 " on PCI bus %02x device %02x irq %d",
4090 phba->pcidev->bus->number,
4091 phba->pcidev->devfn,
4093 len = strlen(lpfcinfobuf);
4094 if (phba->Port[0]) {
4095 snprintf(lpfcinfobuf + len,
4100 len = strlen(lpfcinfobuf);
4101 if (phba->sli4_hba.link_state.logical_speed) {
4102 snprintf(lpfcinfobuf + len,
4104 " Logical Link Speed: %d Mbps",
4105 phba->sli4_hba.link_state.logical_speed * 10);
4112 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
4113 * @phba: The Hba for which this call is being executed.
4115 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4116 * The default value of cfg_poll_tmo is 10 milliseconds.
4118 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4120 unsigned long poll_tmo_expires =
4121 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4123 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
4124 mod_timer(&phba->fcp_poll_timer,
4129 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
4130 * @phba: The Hba for which this call is being executed.
4132 * This routine starts the fcp_poll_timer of @phba.
4134 void lpfc_poll_start_timer(struct lpfc_hba * phba)
4136 lpfc_poll_rearm_timer(phba);
4140 * lpfc_poll_timeout - Restart polling timer
4141 * @ptr: Map to lpfc_hba data structure pointer.
4143 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4144 * and FCP Ring interrupt is disable.
4147 void lpfc_poll_timeout(unsigned long ptr)
4149 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4151 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4152 lpfc_sli_handle_fast_ring_event(phba,
4153 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4155 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4156 lpfc_poll_rearm_timer(phba);
4161 * lpfc_queuecommand - scsi_host_template queuecommand entry point
4162 * @cmnd: Pointer to scsi_cmnd data structure.
4163 * @done: Pointer to done routine.
4165 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4166 * This routine prepares an IOCB from scsi command and provides to firmware.
4167 * The @done callback is invoked after driver finished processing the command.
4171 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4174 lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
4176 struct Scsi_Host *shost = cmnd->device->host;
4177 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4178 struct lpfc_hba *phba = vport->phba;
4179 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4180 struct lpfc_nodelist *ndlp;
4181 struct lpfc_scsi_buf *lpfc_cmd;
4182 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
4185 err = fc_remote_port_chkready(rport);
4188 goto out_fail_command;
4190 ndlp = rdata->pnode;
4192 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
4193 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
4195 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4196 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4197 " op:%02x str=%s without registering for"
4198 " BlockGuard - Rejecting command\n",
4199 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4200 dif_op_str[scsi_get_prot_op(cmnd)]);
4201 goto out_fail_command;
4205 * Catch race where our node has transitioned, but the
4206 * transport is still transitioning.
4208 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
4209 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
4210 goto out_fail_command;
4212 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
4215 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
4216 if (lpfc_cmd == NULL) {
4217 lpfc_rampdown_queue_depth(phba);
4219 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4220 "0707 driver's buffer pool is empty, "
4226 * Store the midlayer's command structure for the completion phase
4227 * and complete the command initialization.
4229 lpfc_cmd->pCmd = cmnd;
4230 lpfc_cmd->rdata = rdata;
4231 lpfc_cmd->timeout = 0;
4232 lpfc_cmd->start_time = jiffies;
4233 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4234 cmnd->scsi_done = done;
4236 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
4237 if (vport->phba->cfg_enable_bg) {
4238 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4239 "9033 BLKGRD: rcvd protected cmd:%02x op=%s "
4240 "guard=%s\n", cmnd->cmnd[0],
4241 dif_op_str[scsi_get_prot_op(cmnd)],
4242 dif_grd_str[scsi_host_get_guard(shost)]);
4243 if (cmnd->cmnd[0] == READ_10)
4244 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4245 "9035 BLKGRD: READ @ sector %llu, "
4247 (unsigned long long)scsi_get_lba(cmnd),
4248 blk_rq_sectors(cmnd->request),
4249 (cmnd->cmnd[1]>>5));
4250 else if (cmnd->cmnd[0] == WRITE_10)
4251 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4252 "9036 BLKGRD: WRITE @ sector %llu, "
4254 (unsigned long long)scsi_get_lba(cmnd),
4255 blk_rq_sectors(cmnd->request),
4256 (cmnd->cmnd[1]>>5));
4259 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4261 if (vport->phba->cfg_enable_bg) {
4262 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4263 "9038 BLKGRD: rcvd unprotected cmd:"
4264 "%02x op=%s guard=%s\n", cmnd->cmnd[0],
4265 dif_op_str[scsi_get_prot_op(cmnd)],
4266 dif_grd_str[scsi_host_get_guard(shost)]);
4267 if (cmnd->cmnd[0] == READ_10)
4268 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4269 "9040 dbg: READ @ sector %llu, "
4271 (unsigned long long)scsi_get_lba(cmnd),
4272 blk_rq_sectors(cmnd->request),
4273 (cmnd->cmnd[1]>>5));
4274 else if (cmnd->cmnd[0] == WRITE_10)
4275 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
4276 "9041 dbg: WRITE @ sector %llu, "
4278 (unsigned long long)scsi_get_lba(cmnd),
4279 blk_rq_sectors(cmnd->request),
4280 (cmnd->cmnd[1]>>5));
4282 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4286 goto out_host_busy_free_buf;
4288 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
4290 atomic_inc(&ndlp->cmd_pending);
4291 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4292 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
4294 atomic_dec(&ndlp->cmd_pending);
4295 goto out_host_busy_free_buf;
4297 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4298 spin_unlock(shost->host_lock);
4299 lpfc_sli_handle_fast_ring_event(phba,
4300 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4302 spin_lock(shost->host_lock);
4303 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4304 lpfc_poll_rearm_timer(phba);
4309 out_host_busy_free_buf:
4310 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4311 lpfc_release_scsi_buf(phba, lpfc_cmd);
4313 return SCSI_MLQUEUE_HOST_BUSY;
4316 return SCSI_MLQUEUE_TARGET_BUSY;
4323 static DEF_SCSI_QCMD(lpfc_queuecommand)
4326 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
4327 * @cmnd: Pointer to scsi_cmnd data structure.
4329 * This routine aborts @cmnd pending in base driver.
4336 lpfc_abort_handler(struct scsi_cmnd *cmnd)
4338 struct Scsi_Host *shost = cmnd->device->host;
4339 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4340 struct lpfc_hba *phba = vport->phba;
4341 struct lpfc_iocbq *iocb;
4342 struct lpfc_iocbq *abtsiocb;
4343 struct lpfc_scsi_buf *lpfc_cmd;
4346 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
4348 ret = fc_block_scsi_eh(cmnd);
4351 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
4353 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4354 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
4355 "x%x ID %d LUN %d\n",
4356 ret, cmnd->device->id, cmnd->device->lun);
4361 * If pCmd field of the corresponding lpfc_scsi_buf structure
4362 * points to a different SCSI command, then the driver has
4363 * already completed this command, but the midlayer did not
4364 * see the completion before the eh fired. Just return
4367 iocb = &lpfc_cmd->cur_iocbq;
4368 if (lpfc_cmd->pCmd != cmnd)
4371 BUG_ON(iocb->context1 != lpfc_cmd);
4373 abtsiocb = lpfc_sli_get_iocbq(phba);
4374 if (abtsiocb == NULL) {
4380 * The scsi command can not be in txq and it is in flight because the
4381 * pCmd is still pointig at the SCSI command we have to abort. There
4382 * is no need to search the txcmplq. Just send an abort to the FW.
4386 icmd = &abtsiocb->iocb;
4387 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4388 icmd->un.acxri.abortContextTag = cmd->ulpContext;
4389 if (phba->sli_rev == LPFC_SLI_REV4)
4390 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4392 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
4395 icmd->ulpClass = cmd->ulpClass;
4397 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4398 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
4399 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
4401 if (lpfc_is_link_up(phba))
4402 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4404 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
4406 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
4407 abtsiocb->vport = vport;
4408 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4410 lpfc_sli_release_iocbq(phba, abtsiocb);
4415 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4416 lpfc_sli_handle_fast_ring_event(phba,
4417 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4419 lpfc_cmd->waitq = &waitq;
4420 /* Wait for abort to complete */
4421 wait_event_timeout(waitq,
4422 (lpfc_cmd->pCmd != cmnd),
4423 (2*vport->cfg_devloss_tmo*HZ));
4425 spin_lock_irq(shost->host_lock);
4426 lpfc_cmd->waitq = NULL;
4427 spin_unlock_irq(shost->host_lock);
4429 if (lpfc_cmd->pCmd == cmnd) {
4431 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4432 "0748 abort handler timed out waiting "
4433 "for abort to complete: ret %#x, ID %d, "
4435 ret, cmnd->device->id, cmnd->device->lun);
4439 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4440 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
4441 "LUN %d\n", ret, cmnd->device->id,
4447 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4449 switch (task_mgmt_cmd) {
4450 case FCP_ABORT_TASK_SET:
4451 return "ABORT_TASK_SET";
4452 case FCP_CLEAR_TASK_SET:
4453 return "FCP_CLEAR_TASK_SET";
4455 return "FCP_BUS_RESET";
4457 return "FCP_LUN_RESET";
4458 case FCP_TARGET_RESET:
4459 return "FCP_TARGET_RESET";
4461 return "FCP_CLEAR_ACA";
4462 case FCP_TERMINATE_TASK:
4463 return "FCP_TERMINATE_TASK";
4470 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4471 * @vport: The virtual port for which this call is being executed.
4472 * @rdata: Pointer to remote port local data
4473 * @tgt_id: Target ID of remote device.
4474 * @lun_id: Lun number for the TMF
4475 * @task_mgmt_cmd: type of TMF to send
4477 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4485 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4486 unsigned tgt_id, unsigned int lun_id,
4487 uint8_t task_mgmt_cmd)
4489 struct lpfc_hba *phba = vport->phba;
4490 struct lpfc_scsi_buf *lpfc_cmd;
4491 struct lpfc_iocbq *iocbq;
4492 struct lpfc_iocbq *iocbqrsp;
4493 struct lpfc_nodelist *pnode = rdata->pnode;
4497 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4500 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
4501 if (lpfc_cmd == NULL)
4503 lpfc_cmd->timeout = 60;
4504 lpfc_cmd->rdata = rdata;
4506 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
4509 lpfc_release_scsi_buf(phba, lpfc_cmd);
4513 iocbq = &lpfc_cmd->cur_iocbq;
4514 iocbqrsp = lpfc_sli_get_iocbq(phba);
4515 if (iocbqrsp == NULL) {
4516 lpfc_release_scsi_buf(phba, lpfc_cmd);
4520 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4521 "0702 Issue %s to TGT %d LUN %d "
4522 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
4523 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
4524 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
4527 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
4528 iocbq, iocbqrsp, lpfc_cmd->timeout);
4529 if (status != IOCB_SUCCESS) {
4530 if (status == IOCB_TIMEDOUT) {
4531 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
4532 ret = TIMEOUT_ERROR;
4535 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4536 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4537 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
4539 lpfc_taskmgmt_name(task_mgmt_cmd),
4540 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
4541 iocbqrsp->iocb.un.ulpWord[4],
4543 } else if (status == IOCB_BUSY)
4548 lpfc_sli_release_iocbq(phba, iocbqrsp);
4550 if (ret != TIMEOUT_ERROR)
4551 lpfc_release_scsi_buf(phba, lpfc_cmd);
4557 * lpfc_chk_tgt_mapped -
4558 * @vport: The virtual port to check on
4559 * @cmnd: Pointer to scsi_cmnd data structure.
4561 * This routine delays until the scsi target (aka rport) for the
4562 * command exists (is present and logged in) or we declare it non-existent.
4569 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
4571 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4572 struct lpfc_nodelist *pnode;
4573 unsigned long later;
4576 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4577 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
4580 pnode = rdata->pnode;
4582 * If target is not in a MAPPED state, delay until
4583 * target is rediscovered or devloss timeout expires.
4585 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4586 while (time_after(later, jiffies)) {
4587 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4589 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
4591 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
4592 rdata = cmnd->device->hostdata;
4595 pnode = rdata->pnode;
4597 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
4598 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4604 * lpfc_reset_flush_io_context -
4605 * @vport: The virtual port (scsi_host) for the flush context
4606 * @tgt_id: If aborting by Target contect - specifies the target id
4607 * @lun_id: If aborting by Lun context - specifies the lun id
4608 * @context: specifies the context level to flush at.
4610 * After a reset condition via TMF, we need to flush orphaned i/o
4611 * contexts from the adapter. This routine aborts any contexts
4612 * outstanding, then waits for their completions. The wait is
4613 * bounded by devloss_tmo though.
4620 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
4621 uint64_t lun_id, lpfc_ctx_cmd context)
4623 struct lpfc_hba *phba = vport->phba;
4624 unsigned long later;
4627 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4629 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
4630 tgt_id, lun_id, context);
4631 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
4632 while (time_after(later, jiffies) && cnt) {
4633 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
4634 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
4637 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4638 "0724 I/O flush failure for context %s : cnt x%x\n",
4639 ((context == LPFC_CTX_LUN) ? "LUN" :
4640 ((context == LPFC_CTX_TGT) ? "TGT" :
4641 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
4649 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
4650 * @cmnd: Pointer to scsi_cmnd data structure.
4652 * This routine does a device reset by sending a LUN_RESET task management
4660 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
4662 struct Scsi_Host *shost = cmnd->device->host;
4663 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4664 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4665 struct lpfc_nodelist *pnode;
4666 unsigned tgt_id = cmnd->device->id;
4667 unsigned int lun_id = cmnd->device->lun;
4668 struct lpfc_scsi_event_header scsi_event;
4672 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4673 "0798 Device Reset rport failure: rdata x%p\n", rdata);
4676 pnode = rdata->pnode;
4677 status = fc_block_scsi_eh(cmnd);
4681 status = lpfc_chk_tgt_mapped(vport, cmnd);
4682 if (status == FAILED) {
4683 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4684 "0721 Device Reset rport failure: rdata x%p\n", rdata);
4688 scsi_event.event_type = FC_REG_SCSI_EVENT;
4689 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
4690 scsi_event.lun = lun_id;
4691 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4692 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4694 fc_host_post_vendor_event(shost, fc_get_event_number(),
4695 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4697 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4700 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4701 "0713 SCSI layer issued Device Reset (%d, %d) "
4702 "return x%x\n", tgt_id, lun_id, status);
4705 * We have to clean up i/o as : they may be orphaned by the TMF;
4706 * or if the TMF failed, they may be in an indeterminate state.
4708 * We will report success if all the i/o aborts successfully.
4710 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4716 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
4717 * @cmnd: Pointer to scsi_cmnd data structure.
4719 * This routine does a target reset by sending a TARGET_RESET task management
4727 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
4729 struct Scsi_Host *shost = cmnd->device->host;
4730 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4731 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4732 struct lpfc_nodelist *pnode;
4733 unsigned tgt_id = cmnd->device->id;
4734 unsigned int lun_id = cmnd->device->lun;
4735 struct lpfc_scsi_event_header scsi_event;
4739 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4740 "0799 Target Reset rport failure: rdata x%p\n", rdata);
4743 pnode = rdata->pnode;
4744 status = fc_block_scsi_eh(cmnd);
4748 status = lpfc_chk_tgt_mapped(vport, cmnd);
4749 if (status == FAILED) {
4750 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4751 "0722 Target Reset rport failure: rdata x%p\n", rdata);
4755 scsi_event.event_type = FC_REG_SCSI_EVENT;
4756 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
4758 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
4759 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
4761 fc_host_post_vendor_event(shost, fc_get_event_number(),
4762 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4764 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
4767 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4768 "0723 SCSI layer issued Target Reset (%d, %d) "
4769 "return x%x\n", tgt_id, lun_id, status);
4772 * We have to clean up i/o as : they may be orphaned by the TMF;
4773 * or if the TMF failed, they may be in an indeterminate state.
4775 * We will report success if all the i/o aborts successfully.
4777 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
4783 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
4784 * @cmnd: Pointer to scsi_cmnd data structure.
4786 * This routine does target reset to all targets on @cmnd->device->host.
4787 * This emulates Parallel SCSI Bus Reset Semantics.
4794 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
4796 struct Scsi_Host *shost = cmnd->device->host;
4797 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4798 struct lpfc_nodelist *ndlp = NULL;
4799 struct lpfc_scsi_event_header scsi_event;
4801 int ret = SUCCESS, status, i;
4803 scsi_event.event_type = FC_REG_SCSI_EVENT;
4804 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
4806 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
4807 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
4809 fc_host_post_vendor_event(shost, fc_get_event_number(),
4810 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
4812 status = fc_block_scsi_eh(cmnd);
4817 * Since the driver manages a single bus device, reset all
4818 * targets known to the driver. Should any target reset
4819 * fail, this routine returns failure to the midlayer.
4821 for (i = 0; i < LPFC_MAX_TARGET; i++) {
4822 /* Search for mapped node by target ID */
4824 spin_lock_irq(shost->host_lock);
4825 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
4826 if (!NLP_CHK_NODE_ACT(ndlp))
4828 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
4829 ndlp->nlp_sid == i &&
4835 spin_unlock_irq(shost->host_lock);
4839 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
4840 i, 0, FCP_TARGET_RESET);
4842 if (status != SUCCESS) {
4843 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4844 "0700 Bus Reset on target %d failed\n",
4850 * We have to clean up i/o as : they may be orphaned by the TMFs
4851 * above; or if any of the TMFs failed, they may be in an
4852 * indeterminate state.
4853 * We will report success if all the i/o aborts successfully.
4856 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
4857 if (status != SUCCESS)
4860 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4861 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
4866 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
4867 * @sdev: Pointer to scsi_device.
4869 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
4870 * globally available list of scsi buffers. This routine also makes sure scsi
4871 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
4872 * of scsi buffer exists for the lifetime of the driver.
4879 lpfc_slave_alloc(struct scsi_device *sdev)
4881 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4882 struct lpfc_hba *phba = vport->phba;
4883 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
4885 uint32_t num_to_alloc = 0;
4886 int num_allocated = 0;
4889 if (!rport || fc_remote_port_chkready(rport))
4892 sdev->hostdata = rport->dd_data;
4893 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
4896 * Populate the cmds_per_lun count scsi_bufs into this host's globally
4897 * available list of scsi buffers. Don't allocate more than the
4898 * HBA limit conveyed to the midlayer via the host structure. The
4899 * formula accounts for the lun_queue_depth + error handlers + 1
4900 * extra. This list of scsi bufs exists for the lifetime of the driver.
4902 total = phba->total_scsi_bufs;
4903 num_to_alloc = vport->cfg_lun_queue_depth + 2;
4905 /* If allocated buffers are enough do nothing */
4906 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
4909 /* Allow some exchanges to be available always to complete discovery */
4910 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
4911 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4912 "0704 At limitation of %d preallocated "
4913 "command buffers\n", total);
4915 /* Allow some exchanges to be available always to complete discovery */
4916 } else if (total + num_to_alloc >
4917 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
4918 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4919 "0705 Allocation request of %d "
4920 "command buffers will exceed max of %d. "
4921 "Reducing allocation request to %d.\n",
4922 num_to_alloc, phba->cfg_hba_queue_depth,
4923 (phba->cfg_hba_queue_depth - total));
4924 num_to_alloc = phba->cfg_hba_queue_depth - total;
4926 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
4927 if (num_to_alloc != num_allocated) {
4928 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4929 "0708 Allocation request of %d "
4930 "command buffers did not succeed. "
4931 "Allocated %d buffers.\n",
4932 num_to_alloc, num_allocated);
4934 if (num_allocated > 0)
4935 phba->total_scsi_bufs += num_allocated;
4940 * lpfc_slave_configure - scsi_host_template slave_configure entry point
4941 * @sdev: Pointer to scsi_device.
4943 * This routine configures following items
4944 * - Tag command queuing support for @sdev if supported.
4945 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
4951 lpfc_slave_configure(struct scsi_device *sdev)
4953 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4954 struct lpfc_hba *phba = vport->phba;
4956 if (sdev->tagged_supported)
4957 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
4959 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
4961 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4962 lpfc_sli_handle_fast_ring_event(phba,
4963 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4964 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4965 lpfc_poll_rearm_timer(phba);
4972 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
4973 * @sdev: Pointer to scsi_device.
4975 * This routine sets @sdev hostatdata filed to null.
4978 lpfc_slave_destroy(struct scsi_device *sdev)
4980 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
4981 struct lpfc_hba *phba = vport->phba;
4982 atomic_dec(&phba->sdev_cnt);
4983 sdev->hostdata = NULL;
4988 struct scsi_host_template lpfc_template = {
4989 .module = THIS_MODULE,
4990 .name = LPFC_DRIVER_NAME,
4992 .queuecommand = lpfc_queuecommand,
4993 .eh_abort_handler = lpfc_abort_handler,
4994 .eh_device_reset_handler = lpfc_device_reset_handler,
4995 .eh_target_reset_handler = lpfc_target_reset_handler,
4996 .eh_bus_reset_handler = lpfc_bus_reset_handler,
4997 .slave_alloc = lpfc_slave_alloc,
4998 .slave_configure = lpfc_slave_configure,
4999 .slave_destroy = lpfc_slave_destroy,
5000 .scan_finished = lpfc_scan_finished,
5002 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5003 .cmd_per_lun = LPFC_CMD_PER_LUN,
5004 .use_clustering = ENABLE_CLUSTERING,
5005 .shost_attrs = lpfc_hba_attrs,
5006 .max_sectors = 0xFFFF,
5007 .vendor_id = LPFC_NL_VENDOR_ID,
5008 .change_queue_depth = lpfc_change_queue_depth,
5011 struct scsi_host_template lpfc_vport_template = {
5012 .module = THIS_MODULE,
5013 .name = LPFC_DRIVER_NAME,
5015 .queuecommand = lpfc_queuecommand,
5016 .eh_abort_handler = lpfc_abort_handler,
5017 .eh_device_reset_handler = lpfc_device_reset_handler,
5018 .eh_target_reset_handler = lpfc_target_reset_handler,
5019 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5020 .slave_alloc = lpfc_slave_alloc,
5021 .slave_configure = lpfc_slave_configure,
5022 .slave_destroy = lpfc_slave_destroy,
5023 .scan_finished = lpfc_scan_finished,
5025 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5026 .cmd_per_lun = LPFC_CMD_PER_LUN,
5027 .use_clustering = ENABLE_CLUSTERING,
5028 .shost_attrs = lpfc_vport_attrs,
5029 .max_sectors = 0xFFFF,
5030 .change_queue_depth = lpfc_change_queue_depth,