1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2013 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>
27 #include <linux/crc-t10dif.h>
28 #include <net/checksum.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_tcq.h>
35 #include <scsi/scsi_transport_fc.h>
37 #include "lpfc_version.h"
41 #include "lpfc_sli4.h"
43 #include "lpfc_disc.h"
45 #include "lpfc_scsi.h"
46 #include "lpfc_logmsg.h"
47 #include "lpfc_crtn.h"
48 #include "lpfc_vport.h"
50 #define LPFC_RESET_WAIT 2
51 #define LPFC_ABORT_WAIT 2
53 int _dump_buf_done = 1;
55 static char *dif_op_str[] = {
65 struct scsi_dif_tuple {
66 __be16 guard_tag; /* Checksum */
67 __be16 app_tag; /* Opaque storage */
68 __be32 ref_tag; /* Target LBA or indirect LBA */
72 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
74 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
76 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
79 lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
82 struct scatterlist *sgde = scsi_sglist(cmnd);
84 if (!_dump_buf_data) {
85 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
86 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
93 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
94 "9051 BLKGRD: ERROR: data scatterlist is null\n");
98 dst = (void *) _dump_buf_data;
101 memcpy(dst, src, sgde->length);
103 sgde = sg_next(sgde);
108 lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
111 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
113 if (!_dump_buf_dif) {
114 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
115 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
121 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
122 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
129 memcpy(dst, src, sgde->length);
131 sgde = sg_next(sgde);
135 static inline unsigned
136 lpfc_cmd_blksize(struct scsi_cmnd *sc)
138 return sc->device->sector_size;
141 #define LPFC_CHECK_PROTECT_GUARD 1
142 #define LPFC_CHECK_PROTECT_REF 2
143 static inline unsigned
144 lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
149 static inline unsigned
150 lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
152 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
154 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
160 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
161 * @phba: Pointer to HBA object.
162 * @lpfc_cmd: lpfc scsi command object pointer.
164 * This function is called from the lpfc_prep_task_mgmt_cmd function to
165 * set the last bit in the response sge entry.
168 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
169 struct lpfc_scsi_buf *lpfc_cmd)
171 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
174 sgl->word2 = le32_to_cpu(sgl->word2);
175 bf_set(lpfc_sli4_sge_last, sgl, 1);
176 sgl->word2 = cpu_to_le32(sgl->word2);
181 * lpfc_update_stats - Update statistical data for the command completion
182 * @phba: Pointer to HBA object.
183 * @lpfc_cmd: lpfc scsi command object pointer.
185 * This function is called when there is a command completion and this
186 * function updates the statistical data for the command completion.
189 lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
191 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
192 struct lpfc_nodelist *pnode = rdata->pnode;
193 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
195 struct Scsi_Host *shost = cmd->device->host;
196 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
197 unsigned long latency;
203 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
205 spin_lock_irqsave(shost->host_lock, flags);
206 if (!vport->stat_data_enabled ||
207 vport->stat_data_blocked ||
210 (phba->bucket_type == LPFC_NO_BUCKET)) {
211 spin_unlock_irqrestore(shost->host_lock, flags);
215 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
216 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
218 /* check array subscript bounds */
221 else if (i >= LPFC_MAX_BUCKET_COUNT)
222 i = LPFC_MAX_BUCKET_COUNT - 1;
224 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
225 if (latency <= (phba->bucket_base +
226 ((1<<i)*phba->bucket_step)))
230 pnode->lat_data[i].cmd_count++;
231 spin_unlock_irqrestore(shost->host_lock, flags);
235 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
236 * @phba: Pointer to HBA context object.
237 * @vport: Pointer to vport object.
238 * @ndlp: Pointer to FC node associated with the target.
239 * @lun: Lun number of the scsi device.
240 * @old_val: Old value of the queue depth.
241 * @new_val: New value of the queue depth.
243 * This function sends an event to the mgmt application indicating
244 * there is a change in the scsi device queue depth.
247 lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
248 struct lpfc_vport *vport,
249 struct lpfc_nodelist *ndlp,
254 struct lpfc_fast_path_event *fast_path_evt;
257 fast_path_evt = lpfc_alloc_fast_evt(phba);
261 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
263 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
264 LPFC_EVENT_VARQUEDEPTH;
266 /* Report all luns with change in queue depth */
267 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
268 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
269 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
270 &ndlp->nlp_portname, sizeof(struct lpfc_name));
271 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
272 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
275 fast_path_evt->un.queue_depth_evt.oldval = old_val;
276 fast_path_evt->un.queue_depth_evt.newval = new_val;
277 fast_path_evt->vport = vport;
279 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
280 spin_lock_irqsave(&phba->hbalock, flags);
281 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
282 spin_unlock_irqrestore(&phba->hbalock, flags);
283 lpfc_worker_wake_up(phba);
289 * lpfc_change_queue_depth - Alter scsi device queue depth
290 * @sdev: Pointer the scsi device on which to change the queue depth.
291 * @qdepth: New queue depth to set the sdev to.
292 * @reason: The reason for the queue depth change.
294 * This function is called by the midlayer and the LLD to alter the queue
295 * depth for a scsi device. This function sets the queue depth to the new
296 * value and sends an event out to log the queue depth change.
299 lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
301 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
302 struct lpfc_hba *phba = vport->phba;
303 struct lpfc_rport_data *rdata;
304 unsigned long new_queue_depth, old_queue_depth;
306 old_queue_depth = sdev->queue_depth;
307 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
308 new_queue_depth = sdev->queue_depth;
309 rdata = sdev->hostdata;
311 lpfc_send_sdev_queuedepth_change_event(phba, vport,
312 rdata->pnode, sdev->lun,
315 return sdev->queue_depth;
319 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
320 * @sdev: Pointer the scsi device whose queue depth is to change
321 * @tag_type: Identifier for queue tag type
324 lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
326 if (sdev->tagged_supported) {
327 scsi_set_tag_type(sdev, tag_type);
329 scsi_activate_tcq(sdev, sdev->queue_depth);
331 scsi_deactivate_tcq(sdev, sdev->queue_depth);
339 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
340 * @phba: The Hba for which this call is being executed.
342 * This routine is called when there is resource error in driver or firmware.
343 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
344 * posts at most 1 event each second. This routine wakes up worker thread of
345 * @phba to process WORKER_RAM_DOWN_EVENT event.
347 * This routine should be called with no lock held.
350 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
355 spin_lock_irqsave(&phba->hbalock, flags);
356 atomic_inc(&phba->num_rsrc_err);
357 phba->last_rsrc_error_time = jiffies;
359 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
360 spin_unlock_irqrestore(&phba->hbalock, flags);
364 phba->last_ramp_down_time = jiffies;
366 spin_unlock_irqrestore(&phba->hbalock, flags);
368 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
369 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
371 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
372 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
375 lpfc_worker_wake_up(phba);
380 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
381 * @phba: The Hba for which this call is being executed.
383 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
384 * post at most 1 event every 5 minute after last_ramp_up_time or
385 * last_rsrc_error_time. This routine wakes up worker thread of @phba
386 * to process WORKER_RAM_DOWN_EVENT event.
388 * This routine should be called with no lock held.
391 lpfc_rampup_queue_depth(struct lpfc_vport *vport,
392 uint32_t queue_depth)
395 struct lpfc_hba *phba = vport->phba;
397 atomic_inc(&phba->num_cmd_success);
399 if (vport->cfg_lun_queue_depth <= queue_depth)
401 spin_lock_irqsave(&phba->hbalock, flags);
402 if (time_before(jiffies,
403 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
405 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
406 spin_unlock_irqrestore(&phba->hbalock, flags);
409 phba->last_ramp_up_time = jiffies;
410 spin_unlock_irqrestore(&phba->hbalock, flags);
412 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
413 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
415 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
416 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
419 lpfc_worker_wake_up(phba);
424 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
425 * @phba: The Hba for which this call is being executed.
427 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
428 * thread.This routine reduces queue depth for all scsi device on each vport
429 * associated with @phba.
432 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
434 struct lpfc_vport **vports;
435 struct Scsi_Host *shost;
436 struct scsi_device *sdev;
437 unsigned long new_queue_depth;
438 unsigned long num_rsrc_err, num_cmd_success;
441 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
442 num_cmd_success = atomic_read(&phba->num_cmd_success);
445 * The error and success command counters are global per
446 * driver instance. If another handler has already
447 * operated on this error event, just exit.
449 if (num_rsrc_err == 0)
452 vports = lpfc_create_vport_work_array(phba);
454 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
455 shost = lpfc_shost_from_vport(vports[i]);
456 shost_for_each_device(sdev, shost) {
458 sdev->queue_depth * num_rsrc_err /
459 (num_rsrc_err + num_cmd_success);
460 if (!new_queue_depth)
461 new_queue_depth = sdev->queue_depth - 1;
463 new_queue_depth = sdev->queue_depth -
465 lpfc_change_queue_depth(sdev, new_queue_depth,
466 SCSI_QDEPTH_DEFAULT);
469 lpfc_destroy_vport_work_array(phba, vports);
470 atomic_set(&phba->num_rsrc_err, 0);
471 atomic_set(&phba->num_cmd_success, 0);
475 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
476 * @phba: The Hba for which this call is being executed.
478 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
479 * thread.This routine increases queue depth for all scsi device on each vport
480 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
481 * num_cmd_success to zero.
484 lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
486 struct lpfc_vport **vports;
487 struct Scsi_Host *shost;
488 struct scsi_device *sdev;
491 vports = lpfc_create_vport_work_array(phba);
493 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
494 shost = lpfc_shost_from_vport(vports[i]);
495 shost_for_each_device(sdev, shost) {
496 if (vports[i]->cfg_lun_queue_depth <=
499 lpfc_change_queue_depth(sdev,
501 SCSI_QDEPTH_RAMP_UP);
504 lpfc_destroy_vport_work_array(phba, vports);
505 atomic_set(&phba->num_rsrc_err, 0);
506 atomic_set(&phba->num_cmd_success, 0);
510 * lpfc_scsi_dev_block - set all scsi hosts to block state
511 * @phba: Pointer to HBA context object.
513 * This function walks vport list and set each SCSI host to block state
514 * by invoking fc_remote_port_delete() routine. This function is invoked
515 * with EEH when device's PCI slot has been permanently disabled.
518 lpfc_scsi_dev_block(struct lpfc_hba *phba)
520 struct lpfc_vport **vports;
521 struct Scsi_Host *shost;
522 struct scsi_device *sdev;
523 struct fc_rport *rport;
526 vports = lpfc_create_vport_work_array(phba);
528 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
529 shost = lpfc_shost_from_vport(vports[i]);
530 shost_for_each_device(sdev, shost) {
531 rport = starget_to_rport(scsi_target(sdev));
532 fc_remote_port_delete(rport);
535 lpfc_destroy_vport_work_array(phba, vports);
539 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
540 * @vport: The virtual port for which this call being executed.
541 * @num_to_allocate: The requested number of buffers to allocate.
543 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
544 * the scsi buffer contains all the necessary information needed to initiate
545 * a SCSI I/O. The non-DMAable buffer region contains information to build
546 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
547 * and the initial BPL. In addition to allocating memory, the FCP CMND and
548 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
551 * int - number of scsi buffers that were allocated.
552 * 0 = failure, less than num_to_alloc is a partial failure.
555 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
557 struct lpfc_hba *phba = vport->phba;
558 struct lpfc_scsi_buf *psb;
559 struct ulp_bde64 *bpl;
561 dma_addr_t pdma_phys_fcp_cmd;
562 dma_addr_t pdma_phys_fcp_rsp;
563 dma_addr_t pdma_phys_bpl;
567 bpl_size = phba->cfg_sg_dma_buf_size -
568 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
570 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
571 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
572 num_to_alloc, phba->cfg_sg_dma_buf_size,
573 (int)sizeof(struct fcp_cmnd),
574 (int)sizeof(struct fcp_rsp), bpl_size);
576 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
577 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
582 * Get memory from the pci pool to map the virt space to pci
583 * bus space for an I/O. The DMA buffer includes space for the
584 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
585 * necessary to support the sg_tablesize.
587 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
588 GFP_KERNEL, &psb->dma_handle);
594 /* Initialize virtual ptrs to dma_buf region. */
595 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
597 /* Allocate iotag for psb->cur_iocbq. */
598 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
600 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
601 psb->data, psb->dma_handle);
605 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
607 psb->fcp_cmnd = psb->data;
608 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
609 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
610 sizeof(struct fcp_rsp);
612 /* Initialize local short-hand pointers. */
614 pdma_phys_fcp_cmd = psb->dma_handle;
615 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
616 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
617 sizeof(struct fcp_rsp);
620 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
621 * are sg list bdes. Initialize the first two and leave the
622 * rest for queuecommand.
624 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
625 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
626 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
627 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
628 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
630 /* Setup the physical region for the FCP RSP */
631 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
632 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
633 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
634 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
635 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
638 * Since the IOCB for the FCP I/O is built into this
639 * lpfc_scsi_buf, initialize it with all known data now.
641 iocb = &psb->cur_iocbq.iocb;
642 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
643 if ((phba->sli_rev == 3) &&
644 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
645 /* fill in immediate fcp command BDE */
646 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
647 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
648 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
650 iocb->un.fcpi64.bdl.addrHigh = 0;
651 iocb->ulpBdeCount = 0;
653 /* fill in response BDE */
654 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
656 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
657 sizeof(struct fcp_rsp);
658 iocb->unsli3.fcp_ext.rbde.addrLow =
659 putPaddrLow(pdma_phys_fcp_rsp);
660 iocb->unsli3.fcp_ext.rbde.addrHigh =
661 putPaddrHigh(pdma_phys_fcp_rsp);
663 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
664 iocb->un.fcpi64.bdl.bdeSize =
665 (2 * sizeof(struct ulp_bde64));
666 iocb->un.fcpi64.bdl.addrLow =
667 putPaddrLow(pdma_phys_bpl);
668 iocb->un.fcpi64.bdl.addrHigh =
669 putPaddrHigh(pdma_phys_bpl);
670 iocb->ulpBdeCount = 1;
673 iocb->ulpClass = CLASS3;
674 psb->status = IOSTAT_SUCCESS;
675 /* Put it back into the SCSI buffer list */
676 psb->cur_iocbq.context1 = psb;
677 lpfc_release_scsi_buf_s3(phba, psb);
685 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
686 * @vport: pointer to lpfc vport data structure.
688 * This routine is invoked by the vport cleanup for deletions and the cleanup
689 * for an ndlp on removal.
692 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
694 struct lpfc_hba *phba = vport->phba;
695 struct lpfc_scsi_buf *psb, *next_psb;
696 unsigned long iflag = 0;
698 spin_lock_irqsave(&phba->hbalock, iflag);
699 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
700 list_for_each_entry_safe(psb, next_psb,
701 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
702 if (psb->rdata && psb->rdata->pnode
703 && psb->rdata->pnode->vport == vport)
706 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
707 spin_unlock_irqrestore(&phba->hbalock, iflag);
711 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
712 * @phba: pointer to lpfc hba data structure.
713 * @axri: pointer to the fcp xri abort wcqe structure.
715 * This routine is invoked by the worker thread to process a SLI4 fast-path
719 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
720 struct sli4_wcqe_xri_aborted *axri)
722 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
723 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
724 struct lpfc_scsi_buf *psb, *next_psb;
725 unsigned long iflag = 0;
726 struct lpfc_iocbq *iocbq;
728 struct lpfc_nodelist *ndlp;
730 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
732 spin_lock_irqsave(&phba->hbalock, iflag);
733 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
734 list_for_each_entry_safe(psb, next_psb,
735 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
736 if (psb->cur_iocbq.sli4_xritag == xri) {
737 list_del(&psb->list);
739 psb->status = IOSTAT_SUCCESS;
741 &phba->sli4_hba.abts_scsi_buf_list_lock);
742 if (psb->rdata && psb->rdata->pnode)
743 ndlp = psb->rdata->pnode;
747 rrq_empty = list_empty(&phba->active_rrq_list);
748 spin_unlock_irqrestore(&phba->hbalock, iflag);
750 lpfc_set_rrq_active(phba, ndlp,
751 psb->cur_iocbq.sli4_lxritag, rxid, 1);
752 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
754 lpfc_release_scsi_buf_s4(phba, psb);
756 lpfc_worker_wake_up(phba);
760 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
761 for (i = 1; i <= phba->sli.last_iotag; i++) {
762 iocbq = phba->sli.iocbq_lookup[i];
764 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
765 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
767 if (iocbq->sli4_xritag != xri)
769 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
771 spin_unlock_irqrestore(&phba->hbalock, iflag);
772 if (!list_empty(&pring->txq))
773 lpfc_worker_wake_up(phba);
777 spin_unlock_irqrestore(&phba->hbalock, iflag);
781 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
782 * @phba: pointer to lpfc hba data structure.
783 * @post_sblist: pointer to the scsi buffer list.
785 * This routine walks a list of scsi buffers that was passed in. It attempts
786 * to construct blocks of scsi buffer sgls which contains contiguous xris and
787 * uses the non-embedded SGL block post mailbox commands to post to the port.
788 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
789 * embedded SGL post mailbox command for posting. The @post_sblist passed in
790 * must be local list, thus no lock is needed when manipulate the list.
792 * Returns: 0 = failure, non-zero number of successfully posted buffers.
795 lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
796 struct list_head *post_sblist, int sb_count)
798 struct lpfc_scsi_buf *psb, *psb_next;
799 int status, sgl_size;
800 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
801 dma_addr_t pdma_phys_bpl1;
802 int last_xritag = NO_XRI;
803 LIST_HEAD(prep_sblist);
804 LIST_HEAD(blck_sblist);
805 LIST_HEAD(scsi_sblist);
811 sgl_size = phba->cfg_sg_dma_buf_size -
812 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
814 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
815 list_del_init(&psb->list);
817 if ((last_xritag != NO_XRI) &&
818 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
819 /* a hole in xri block, form a sgl posting block */
820 list_splice_init(&prep_sblist, &blck_sblist);
821 post_cnt = block_cnt - 1;
822 /* prepare list for next posting block */
823 list_add_tail(&psb->list, &prep_sblist);
826 /* prepare list for next posting block */
827 list_add_tail(&psb->list, &prep_sblist);
828 /* enough sgls for non-embed sgl mbox command */
829 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
830 list_splice_init(&prep_sblist, &blck_sblist);
831 post_cnt = block_cnt;
836 last_xritag = psb->cur_iocbq.sli4_xritag;
838 /* end of repost sgl list condition for SCSI buffers */
839 if (num_posting == sb_count) {
841 /* last sgl posting block */
842 list_splice_init(&prep_sblist, &blck_sblist);
843 post_cnt = block_cnt;
844 } else if (block_cnt == 1) {
845 /* last single sgl with non-contiguous xri */
846 if (sgl_size > SGL_PAGE_SIZE)
847 pdma_phys_bpl1 = psb->dma_phys_bpl +
851 status = lpfc_sli4_post_sgl(phba,
854 psb->cur_iocbq.sli4_xritag);
856 /* failure, put on abort scsi list */
859 /* success, put on SCSI buffer list */
861 psb->status = IOSTAT_SUCCESS;
864 /* success, put on SCSI buffer sgl list */
865 list_add_tail(&psb->list, &scsi_sblist);
869 /* continue until a nembed page worth of sgls */
873 /* post block of SCSI buffer list sgls */
874 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
877 /* don't reset xirtag due to hole in xri block */
879 last_xritag = NO_XRI;
881 /* reset SCSI buffer post count for next round of posting */
884 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
885 while (!list_empty(&blck_sblist)) {
886 list_remove_head(&blck_sblist, psb,
887 struct lpfc_scsi_buf, list);
889 /* failure, put on abort scsi list */
892 /* success, put on SCSI buffer list */
894 psb->status = IOSTAT_SUCCESS;
897 list_add_tail(&psb->list, &scsi_sblist);
900 /* Push SCSI buffers with sgl posted to the availble list */
901 while (!list_empty(&scsi_sblist)) {
902 list_remove_head(&scsi_sblist, psb,
903 struct lpfc_scsi_buf, list);
904 lpfc_release_scsi_buf_s4(phba, psb);
910 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
911 * @phba: pointer to lpfc hba data structure.
913 * This routine walks the list of scsi buffers that have been allocated and
914 * repost them to the port by using SGL block post. This is needed after a
915 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
916 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
917 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
919 * Returns: 0 = success, non-zero failure.
922 lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
924 LIST_HEAD(post_sblist);
925 int num_posted, rc = 0;
927 /* get all SCSI buffers need to repost to a local list */
928 spin_lock_irq(&phba->scsi_buf_list_get_lock);
929 spin_lock_irq(&phba->scsi_buf_list_put_lock);
930 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
931 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
932 spin_unlock_irq(&phba->scsi_buf_list_put_lock);
933 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
935 /* post the list of scsi buffer sgls to port if available */
936 if (!list_empty(&post_sblist)) {
937 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
938 phba->sli4_hba.scsi_xri_cnt);
939 /* failed to post any scsi buffer, return error */
947 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
948 * @vport: The virtual port for which this call being executed.
949 * @num_to_allocate: The requested number of buffers to allocate.
951 * This routine allocates scsi buffers for device with SLI-4 interface spec,
952 * the scsi buffer contains all the necessary information needed to initiate
953 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
954 * them on a list, it post them to the port by using SGL block post.
957 * int - number of scsi buffers that were allocated and posted.
958 * 0 = failure, less than num_to_alloc is a partial failure.
961 lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
963 struct lpfc_hba *phba = vport->phba;
964 struct lpfc_scsi_buf *psb;
965 struct sli4_sge *sgl;
967 dma_addr_t pdma_phys_fcp_cmd;
968 dma_addr_t pdma_phys_fcp_rsp;
969 dma_addr_t pdma_phys_bpl;
970 uint16_t iotag, lxri = 0;
971 int bcnt, num_posted, sgl_size;
972 LIST_HEAD(prep_sblist);
973 LIST_HEAD(post_sblist);
974 LIST_HEAD(scsi_sblist);
976 sgl_size = phba->cfg_sg_dma_buf_size -
977 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
979 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
980 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
981 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
982 (int)sizeof(struct fcp_cmnd),
983 (int)sizeof(struct fcp_rsp));
985 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
986 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
990 * Get memory from the pci pool to map the virt space to
991 * pci bus space for an I/O. The DMA buffer includes space
992 * for the struct fcp_cmnd, struct fcp_rsp and the number
993 * of bde's necessary to support the sg_tablesize.
995 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
996 GFP_KERNEL, &psb->dma_handle);
1001 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
1003 /* Page alignment is CRITICAL, double check to be sure */
1004 if (((unsigned long)(psb->data) &
1005 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0) {
1006 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1007 psb->data, psb->dma_handle);
1012 /* Allocate iotag for psb->cur_iocbq. */
1013 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
1015 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1016 psb->data, psb->dma_handle);
1021 lxri = lpfc_sli4_next_xritag(phba);
1022 if (lxri == NO_XRI) {
1023 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1024 psb->data, psb->dma_handle);
1028 psb->cur_iocbq.sli4_lxritag = lxri;
1029 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
1030 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
1031 psb->fcp_bpl = psb->data;
1032 psb->fcp_cmnd = (psb->data + sgl_size);
1033 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1034 sizeof(struct fcp_cmnd));
1036 /* Initialize local short-hand pointers. */
1037 sgl = (struct sli4_sge *)psb->fcp_bpl;
1038 pdma_phys_bpl = psb->dma_handle;
1039 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
1040 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1043 * The first two bdes are the FCP_CMD and FCP_RSP.
1044 * The balance are sg list bdes. Initialize the
1045 * first two and leave the rest for queuecommand.
1047 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1048 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
1049 sgl->word2 = le32_to_cpu(sgl->word2);
1050 bf_set(lpfc_sli4_sge_last, sgl, 0);
1051 sgl->word2 = cpu_to_le32(sgl->word2);
1052 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
1055 /* Setup the physical region for the FCP RSP */
1056 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1057 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
1058 sgl->word2 = le32_to_cpu(sgl->word2);
1059 bf_set(lpfc_sli4_sge_last, sgl, 1);
1060 sgl->word2 = cpu_to_le32(sgl->word2);
1061 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
1064 * Since the IOCB for the FCP I/O is built into this
1065 * lpfc_scsi_buf, initialize it with all known data now.
1067 iocb = &psb->cur_iocbq.iocb;
1068 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1069 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1070 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1071 * We are setting the bpl to point to out sgl. An sgl's
1072 * entries are 16 bytes, a bpl entries are 12 bytes.
1074 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1075 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1076 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1077 iocb->ulpBdeCount = 1;
1079 iocb->ulpClass = CLASS3;
1080 psb->cur_iocbq.context1 = psb;
1081 psb->dma_phys_bpl = pdma_phys_bpl;
1083 /* add the scsi buffer to a post list */
1084 list_add_tail(&psb->list, &post_sblist);
1085 spin_lock_irq(&phba->scsi_buf_list_get_lock);
1086 phba->sli4_hba.scsi_xri_cnt++;
1087 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
1089 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1090 "3021 Allocate %d out of %d requested new SCSI "
1091 "buffers\n", bcnt, num_to_alloc);
1093 /* post the list of scsi buffer sgls to port if available */
1094 if (!list_empty(&post_sblist))
1095 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1096 &post_sblist, bcnt);
1104 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1105 * @vport: The virtual port for which this call being executed.
1106 * @num_to_allocate: The requested number of buffers to allocate.
1108 * This routine wraps the actual SCSI buffer allocator function pointer from
1109 * the lpfc_hba struct.
1112 * int - number of scsi buffers that were allocated.
1113 * 0 = failure, less than num_to_alloc is a partial failure.
1116 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1118 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1122 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1123 * @phba: The HBA for which this call is being executed.
1125 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1126 * and returns to caller.
1130 * Pointer to lpfc_scsi_buf - Success
1132 static struct lpfc_scsi_buf*
1133 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1135 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1136 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
1137 unsigned long gflag = 0;
1138 unsigned long pflag = 0;
1140 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, gflag);
1141 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1144 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, pflag);
1145 list_splice(&phba->lpfc_scsi_buf_list_put,
1146 &phba->lpfc_scsi_buf_list_get);
1147 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1148 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1149 struct lpfc_scsi_buf, list);
1150 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, pflag);
1152 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, gflag);
1156 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1157 * @phba: The HBA for which this call is being executed.
1159 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1160 * and returns to caller.
1164 * Pointer to lpfc_scsi_buf - Success
1166 static struct lpfc_scsi_buf*
1167 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1169 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
1170 unsigned long gflag = 0;
1171 unsigned long pflag = 0;
1174 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, gflag);
1175 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1176 &phba->lpfc_scsi_buf_list_get, list) {
1177 if (lpfc_test_rrq_active(phba, ndlp,
1178 lpfc_cmd->cur_iocbq.sli4_lxritag))
1180 list_del(&lpfc_cmd->list);
1185 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, pflag);
1186 list_splice(&phba->lpfc_scsi_buf_list_put,
1187 &phba->lpfc_scsi_buf_list_get);
1188 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1189 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, pflag);
1190 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1191 &phba->lpfc_scsi_buf_list_get, list) {
1192 if (lpfc_test_rrq_active(
1193 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1195 list_del(&lpfc_cmd->list);
1200 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, gflag);
1206 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1207 * @phba: The HBA for which this call is being executed.
1209 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1210 * and returns to caller.
1214 * Pointer to lpfc_scsi_buf - Success
1216 static struct lpfc_scsi_buf*
1217 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1219 return phba->lpfc_get_scsi_buf(phba, ndlp);
1223 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
1224 * @phba: The Hba for which this call is being executed.
1225 * @psb: The scsi buffer which is being released.
1227 * This routine releases @psb scsi buffer by adding it to tail of @phba
1228 * lpfc_scsi_buf_list list.
1231 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1233 unsigned long iflag = 0;
1236 psb->nonsg_phys = 0;
1237 psb->prot_seg_cnt = 0;
1239 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1241 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
1242 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1243 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1247 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1248 * @phba: The Hba for which this call is being executed.
1249 * @psb: The scsi buffer which is being released.
1251 * This routine releases @psb scsi buffer by adding it to tail of @phba
1252 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1253 * and cannot be reused for at least RA_TOV amount of time if it was
1257 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1259 unsigned long iflag = 0;
1262 psb->nonsg_phys = 0;
1263 psb->prot_seg_cnt = 0;
1265 if (psb->exch_busy) {
1266 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1269 list_add_tail(&psb->list,
1270 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1271 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1275 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
1276 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1277 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1278 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1283 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1284 * @phba: The Hba for which this call is being executed.
1285 * @psb: The scsi buffer which is being released.
1287 * This routine releases @psb scsi buffer by adding it to tail of @phba
1288 * lpfc_scsi_buf_list list.
1291 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1294 phba->lpfc_release_scsi_buf(phba, psb);
1298 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
1299 * @phba: The Hba for which this call is being executed.
1300 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1302 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1303 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1304 * through sg elements and format the bdea. This routine also initializes all
1305 * IOCB fields which are dependent on scsi command request buffer.
1312 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1314 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1315 struct scatterlist *sgel = NULL;
1316 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1317 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1318 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
1319 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1320 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1321 dma_addr_t physaddr;
1322 uint32_t num_bde = 0;
1323 int nseg, datadir = scsi_cmnd->sc_data_direction;
1326 * There are three possibilities here - use scatter-gather segment, use
1327 * the single mapping, or neither. Start the lpfc command prep by
1328 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1332 if (scsi_sg_count(scsi_cmnd)) {
1334 * The driver stores the segment count returned from pci_map_sg
1335 * because this a count of dma-mappings used to map the use_sg
1336 * pages. They are not guaranteed to be the same for those
1337 * architectures that implement an IOMMU.
1340 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1341 scsi_sg_count(scsi_cmnd), datadir);
1342 if (unlikely(!nseg))
1345 lpfc_cmd->seg_cnt = nseg;
1346 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1347 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1348 "9064 BLKGRD: %s: Too many sg segments from "
1349 "dma_map_sg. Config %d, seg_cnt %d\n",
1350 __func__, phba->cfg_sg_seg_cnt,
1352 lpfc_cmd->seg_cnt = 0;
1353 scsi_dma_unmap(scsi_cmnd);
1358 * The driver established a maximum scatter-gather segment count
1359 * during probe that limits the number of sg elements in any
1360 * single scsi command. Just run through the seg_cnt and format
1362 * When using SLI-3 the driver will try to fit all the BDEs into
1363 * the IOCB. If it can't then the BDEs get added to a BPL as it
1364 * does for SLI-2 mode.
1366 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1367 physaddr = sg_dma_address(sgel);
1368 if (phba->sli_rev == 3 &&
1369 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1370 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
1371 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1372 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1373 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1374 data_bde->addrLow = putPaddrLow(physaddr);
1375 data_bde->addrHigh = putPaddrHigh(physaddr);
1378 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1379 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1380 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1382 le32_to_cpu(putPaddrLow(physaddr));
1384 le32_to_cpu(putPaddrHigh(physaddr));
1391 * Finish initializing those IOCB fields that are dependent on the
1392 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1393 * explicitly reinitialized and for SLI-3 the extended bde count is
1394 * explicitly reinitialized since all iocb memory resources are reused.
1396 if (phba->sli_rev == 3 &&
1397 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1398 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
1399 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1401 * The extended IOCB format can only fit 3 BDE or a BPL.
1402 * This I/O has more than 3 BDE so the 1st data bde will
1403 * be a BPL that is filled in here.
1405 physaddr = lpfc_cmd->dma_handle;
1406 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1407 data_bde->tus.f.bdeSize = (num_bde *
1408 sizeof(struct ulp_bde64));
1409 physaddr += (sizeof(struct fcp_cmnd) +
1410 sizeof(struct fcp_rsp) +
1411 (2 * sizeof(struct ulp_bde64)));
1412 data_bde->addrHigh = putPaddrHigh(physaddr);
1413 data_bde->addrLow = putPaddrLow(physaddr);
1414 /* ebde count includes the response bde and data bpl */
1415 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1417 /* ebde count includes the response bde and data bdes */
1418 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1421 iocb_cmd->un.fcpi64.bdl.bdeSize =
1422 ((num_bde + 2) * sizeof(struct ulp_bde64));
1423 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1425 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1428 * Due to difference in data length between DIF/non-DIF paths,
1429 * we need to set word 4 of IOCB here
1431 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1435 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1437 /* Return if if error injection is detected by Initiator */
1438 #define BG_ERR_INIT 0x1
1439 /* Return if if error injection is detected by Target */
1440 #define BG_ERR_TGT 0x2
1441 /* Return if if swapping CSUM<-->CRC is required for error injection */
1442 #define BG_ERR_SWAP 0x10
1443 /* Return if disabling Guard/Ref/App checking is required for error injection */
1444 #define BG_ERR_CHECK 0x20
1447 * lpfc_bg_err_inject - Determine if we should inject an error
1448 * @phba: The Hba for which this call is being executed.
1449 * @sc: The SCSI command to examine
1450 * @reftag: (out) BlockGuard reference tag for transmitted data
1451 * @apptag: (out) BlockGuard application tag for transmitted data
1452 * @new_guard (in) Value to replace CRC with if needed
1454 * Returns BG_ERR_* bit mask or 0 if request ignored
1457 lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1458 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1460 struct scatterlist *sgpe; /* s/g prot entry */
1461 struct scatterlist *sgde; /* s/g data entry */
1462 struct lpfc_scsi_buf *lpfc_cmd = NULL;
1463 struct scsi_dif_tuple *src = NULL;
1464 struct lpfc_nodelist *ndlp;
1465 struct lpfc_rport_data *rdata;
1466 uint32_t op = scsi_get_prot_op(sc);
1473 if (op == SCSI_PROT_NORMAL)
1476 sgpe = scsi_prot_sglist(sc);
1477 sgde = scsi_sglist(sc);
1478 lba = scsi_get_lba(sc);
1480 /* First check if we need to match the LBA */
1481 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1482 blksize = lpfc_cmd_blksize(sc);
1483 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1485 /* Make sure we have the right LBA if one is specified */
1486 if ((phba->lpfc_injerr_lba < lba) ||
1487 (phba->lpfc_injerr_lba >= (lba + numblks)))
1490 blockoff = phba->lpfc_injerr_lba - lba;
1491 numblks = sg_dma_len(sgpe) /
1492 sizeof(struct scsi_dif_tuple);
1493 if (numblks < blockoff)
1498 /* Next check if we need to match the remote NPortID or WWPN */
1499 rdata = sc->device->hostdata;
1500 if (rdata && rdata->pnode) {
1501 ndlp = rdata->pnode;
1503 /* Make sure we have the right NPortID if one is specified */
1504 if (phba->lpfc_injerr_nportid &&
1505 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1509 * Make sure we have the right WWPN if one is specified.
1510 * wwn[0] should be a non-zero NAA in a good WWPN.
1512 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1513 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1514 sizeof(struct lpfc_name)) != 0))
1518 /* Setup a ptr to the protection data if the SCSI host provides it */
1520 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1522 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1525 /* Should we change the Reference Tag */
1527 if (phba->lpfc_injerr_wref_cnt) {
1529 case SCSI_PROT_WRITE_PASS:
1532 * For WRITE_PASS, force the error
1533 * to be sent on the wire. It should
1534 * be detected by the Target.
1535 * If blockoff != 0 error will be
1536 * inserted in middle of the IO.
1539 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1540 "9076 BLKGRD: Injecting reftag error: "
1541 "write lba x%lx + x%x oldrefTag x%x\n",
1542 (unsigned long)lba, blockoff,
1543 be32_to_cpu(src->ref_tag));
1546 * Save the old ref_tag so we can
1547 * restore it on completion.
1550 lpfc_cmd->prot_data_type =
1552 lpfc_cmd->prot_data_segment =
1554 lpfc_cmd->prot_data =
1557 src->ref_tag = cpu_to_be32(0xDEADBEEF);
1558 phba->lpfc_injerr_wref_cnt--;
1559 if (phba->lpfc_injerr_wref_cnt == 0) {
1560 phba->lpfc_injerr_nportid = 0;
1561 phba->lpfc_injerr_lba =
1562 LPFC_INJERR_LBA_OFF;
1563 memset(&phba->lpfc_injerr_wwpn,
1564 0, sizeof(struct lpfc_name));
1566 rc = BG_ERR_TGT | BG_ERR_CHECK;
1571 case SCSI_PROT_WRITE_INSERT:
1573 * For WRITE_INSERT, force the error
1574 * to be sent on the wire. It should be
1575 * detected by the Target.
1577 /* DEADBEEF will be the reftag on the wire */
1578 *reftag = 0xDEADBEEF;
1579 phba->lpfc_injerr_wref_cnt--;
1580 if (phba->lpfc_injerr_wref_cnt == 0) {
1581 phba->lpfc_injerr_nportid = 0;
1582 phba->lpfc_injerr_lba =
1583 LPFC_INJERR_LBA_OFF;
1584 memset(&phba->lpfc_injerr_wwpn,
1585 0, sizeof(struct lpfc_name));
1587 rc = BG_ERR_TGT | BG_ERR_CHECK;
1589 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1590 "9078 BLKGRD: Injecting reftag error: "
1591 "write lba x%lx\n", (unsigned long)lba);
1593 case SCSI_PROT_WRITE_STRIP:
1595 * For WRITE_STRIP and WRITE_PASS,
1596 * force the error on data
1597 * being copied from SLI-Host to SLI-Port.
1599 *reftag = 0xDEADBEEF;
1600 phba->lpfc_injerr_wref_cnt--;
1601 if (phba->lpfc_injerr_wref_cnt == 0) {
1602 phba->lpfc_injerr_nportid = 0;
1603 phba->lpfc_injerr_lba =
1604 LPFC_INJERR_LBA_OFF;
1605 memset(&phba->lpfc_injerr_wwpn,
1606 0, sizeof(struct lpfc_name));
1610 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1611 "9077 BLKGRD: Injecting reftag error: "
1612 "write lba x%lx\n", (unsigned long)lba);
1616 if (phba->lpfc_injerr_rref_cnt) {
1618 case SCSI_PROT_READ_INSERT:
1619 case SCSI_PROT_READ_STRIP:
1620 case SCSI_PROT_READ_PASS:
1622 * For READ_STRIP and READ_PASS, force the
1623 * error on data being read off the wire. It
1624 * should force an IO error to the driver.
1626 *reftag = 0xDEADBEEF;
1627 phba->lpfc_injerr_rref_cnt--;
1628 if (phba->lpfc_injerr_rref_cnt == 0) {
1629 phba->lpfc_injerr_nportid = 0;
1630 phba->lpfc_injerr_lba =
1631 LPFC_INJERR_LBA_OFF;
1632 memset(&phba->lpfc_injerr_wwpn,
1633 0, sizeof(struct lpfc_name));
1637 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1638 "9079 BLKGRD: Injecting reftag error: "
1639 "read lba x%lx\n", (unsigned long)lba);
1645 /* Should we change the Application Tag */
1647 if (phba->lpfc_injerr_wapp_cnt) {
1649 case SCSI_PROT_WRITE_PASS:
1652 * For WRITE_PASS, force the error
1653 * to be sent on the wire. It should
1654 * be detected by the Target.
1655 * If blockoff != 0 error will be
1656 * inserted in middle of the IO.
1659 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1660 "9080 BLKGRD: Injecting apptag error: "
1661 "write lba x%lx + x%x oldappTag x%x\n",
1662 (unsigned long)lba, blockoff,
1663 be16_to_cpu(src->app_tag));
1666 * Save the old app_tag so we can
1667 * restore it on completion.
1670 lpfc_cmd->prot_data_type =
1672 lpfc_cmd->prot_data_segment =
1674 lpfc_cmd->prot_data =
1677 src->app_tag = cpu_to_be16(0xDEAD);
1678 phba->lpfc_injerr_wapp_cnt--;
1679 if (phba->lpfc_injerr_wapp_cnt == 0) {
1680 phba->lpfc_injerr_nportid = 0;
1681 phba->lpfc_injerr_lba =
1682 LPFC_INJERR_LBA_OFF;
1683 memset(&phba->lpfc_injerr_wwpn,
1684 0, sizeof(struct lpfc_name));
1686 rc = BG_ERR_TGT | BG_ERR_CHECK;
1690 case SCSI_PROT_WRITE_INSERT:
1692 * For WRITE_INSERT, force the
1693 * error to be sent on the wire. It should be
1694 * detected by the Target.
1696 /* DEAD will be the apptag on the wire */
1698 phba->lpfc_injerr_wapp_cnt--;
1699 if (phba->lpfc_injerr_wapp_cnt == 0) {
1700 phba->lpfc_injerr_nportid = 0;
1701 phba->lpfc_injerr_lba =
1702 LPFC_INJERR_LBA_OFF;
1703 memset(&phba->lpfc_injerr_wwpn,
1704 0, sizeof(struct lpfc_name));
1706 rc = BG_ERR_TGT | BG_ERR_CHECK;
1708 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1709 "0813 BLKGRD: Injecting apptag error: "
1710 "write lba x%lx\n", (unsigned long)lba);
1712 case SCSI_PROT_WRITE_STRIP:
1714 * For WRITE_STRIP and WRITE_PASS,
1715 * force the error on data
1716 * being copied from SLI-Host to SLI-Port.
1719 phba->lpfc_injerr_wapp_cnt--;
1720 if (phba->lpfc_injerr_wapp_cnt == 0) {
1721 phba->lpfc_injerr_nportid = 0;
1722 phba->lpfc_injerr_lba =
1723 LPFC_INJERR_LBA_OFF;
1724 memset(&phba->lpfc_injerr_wwpn,
1725 0, sizeof(struct lpfc_name));
1729 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1730 "0812 BLKGRD: Injecting apptag error: "
1731 "write lba x%lx\n", (unsigned long)lba);
1735 if (phba->lpfc_injerr_rapp_cnt) {
1737 case SCSI_PROT_READ_INSERT:
1738 case SCSI_PROT_READ_STRIP:
1739 case SCSI_PROT_READ_PASS:
1741 * For READ_STRIP and READ_PASS, force the
1742 * error on data being read off the wire. It
1743 * should force an IO error to the driver.
1746 phba->lpfc_injerr_rapp_cnt--;
1747 if (phba->lpfc_injerr_rapp_cnt == 0) {
1748 phba->lpfc_injerr_nportid = 0;
1749 phba->lpfc_injerr_lba =
1750 LPFC_INJERR_LBA_OFF;
1751 memset(&phba->lpfc_injerr_wwpn,
1752 0, sizeof(struct lpfc_name));
1756 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1757 "0814 BLKGRD: Injecting apptag error: "
1758 "read lba x%lx\n", (unsigned long)lba);
1765 /* Should we change the Guard Tag */
1767 if (phba->lpfc_injerr_wgrd_cnt) {
1769 case SCSI_PROT_WRITE_PASS:
1773 case SCSI_PROT_WRITE_INSERT:
1775 * For WRITE_INSERT, force the
1776 * error to be sent on the wire. It should be
1777 * detected by the Target.
1779 phba->lpfc_injerr_wgrd_cnt--;
1780 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1781 phba->lpfc_injerr_nportid = 0;
1782 phba->lpfc_injerr_lba =
1783 LPFC_INJERR_LBA_OFF;
1784 memset(&phba->lpfc_injerr_wwpn,
1785 0, sizeof(struct lpfc_name));
1788 rc |= BG_ERR_TGT | BG_ERR_SWAP;
1789 /* Signals the caller to swap CRC->CSUM */
1791 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1792 "0817 BLKGRD: Injecting guard error: "
1793 "write lba x%lx\n", (unsigned long)lba);
1795 case SCSI_PROT_WRITE_STRIP:
1797 * For WRITE_STRIP and WRITE_PASS,
1798 * force the error on data
1799 * being copied from SLI-Host to SLI-Port.
1801 phba->lpfc_injerr_wgrd_cnt--;
1802 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1803 phba->lpfc_injerr_nportid = 0;
1804 phba->lpfc_injerr_lba =
1805 LPFC_INJERR_LBA_OFF;
1806 memset(&phba->lpfc_injerr_wwpn,
1807 0, sizeof(struct lpfc_name));
1810 rc = BG_ERR_INIT | BG_ERR_SWAP;
1811 /* Signals the caller to swap CRC->CSUM */
1813 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1814 "0816 BLKGRD: Injecting guard error: "
1815 "write lba x%lx\n", (unsigned long)lba);
1819 if (phba->lpfc_injerr_rgrd_cnt) {
1821 case SCSI_PROT_READ_INSERT:
1822 case SCSI_PROT_READ_STRIP:
1823 case SCSI_PROT_READ_PASS:
1825 * For READ_STRIP and READ_PASS, force the
1826 * error on data being read off the wire. It
1827 * should force an IO error to the driver.
1829 phba->lpfc_injerr_rgrd_cnt--;
1830 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1831 phba->lpfc_injerr_nportid = 0;
1832 phba->lpfc_injerr_lba =
1833 LPFC_INJERR_LBA_OFF;
1834 memset(&phba->lpfc_injerr_wwpn,
1835 0, sizeof(struct lpfc_name));
1838 rc = BG_ERR_INIT | BG_ERR_SWAP;
1839 /* Signals the caller to swap CRC->CSUM */
1841 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1842 "0818 BLKGRD: Injecting guard error: "
1843 "read lba x%lx\n", (unsigned long)lba);
1853 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1854 * the specified SCSI command.
1855 * @phba: The Hba for which this call is being executed.
1856 * @sc: The SCSI command to examine
1857 * @txopt: (out) BlockGuard operation for transmitted data
1858 * @rxopt: (out) BlockGuard operation for received data
1860 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1864 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1865 uint8_t *txop, uint8_t *rxop)
1869 if (lpfc_cmd_guard_csum(sc)) {
1870 switch (scsi_get_prot_op(sc)) {
1871 case SCSI_PROT_READ_INSERT:
1872 case SCSI_PROT_WRITE_STRIP:
1873 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1874 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1877 case SCSI_PROT_READ_STRIP:
1878 case SCSI_PROT_WRITE_INSERT:
1879 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1880 *txop = BG_OP_IN_NODIF_OUT_CRC;
1883 case SCSI_PROT_READ_PASS:
1884 case SCSI_PROT_WRITE_PASS:
1885 *rxop = BG_OP_IN_CRC_OUT_CSUM;
1886 *txop = BG_OP_IN_CSUM_OUT_CRC;
1889 case SCSI_PROT_NORMAL:
1891 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1892 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1893 scsi_get_prot_op(sc));
1899 switch (scsi_get_prot_op(sc)) {
1900 case SCSI_PROT_READ_STRIP:
1901 case SCSI_PROT_WRITE_INSERT:
1902 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1903 *txop = BG_OP_IN_NODIF_OUT_CRC;
1906 case SCSI_PROT_READ_PASS:
1907 case SCSI_PROT_WRITE_PASS:
1908 *rxop = BG_OP_IN_CRC_OUT_CRC;
1909 *txop = BG_OP_IN_CRC_OUT_CRC;
1912 case SCSI_PROT_READ_INSERT:
1913 case SCSI_PROT_WRITE_STRIP:
1914 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1915 *txop = BG_OP_IN_CRC_OUT_NODIF;
1918 case SCSI_PROT_NORMAL:
1920 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1921 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1922 scsi_get_prot_op(sc));
1931 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1933 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1934 * the specified SCSI command in order to force a guard tag error.
1935 * @phba: The Hba for which this call is being executed.
1936 * @sc: The SCSI command to examine
1937 * @txopt: (out) BlockGuard operation for transmitted data
1938 * @rxopt: (out) BlockGuard operation for received data
1940 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1944 lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1945 uint8_t *txop, uint8_t *rxop)
1949 if (lpfc_cmd_guard_csum(sc)) {
1950 switch (scsi_get_prot_op(sc)) {
1951 case SCSI_PROT_READ_INSERT:
1952 case SCSI_PROT_WRITE_STRIP:
1953 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1954 *txop = BG_OP_IN_CRC_OUT_NODIF;
1957 case SCSI_PROT_READ_STRIP:
1958 case SCSI_PROT_WRITE_INSERT:
1959 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1960 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1963 case SCSI_PROT_READ_PASS:
1964 case SCSI_PROT_WRITE_PASS:
1965 *rxop = BG_OP_IN_CSUM_OUT_CRC;
1966 *txop = BG_OP_IN_CRC_OUT_CSUM;
1969 case SCSI_PROT_NORMAL:
1975 switch (scsi_get_prot_op(sc)) {
1976 case SCSI_PROT_READ_STRIP:
1977 case SCSI_PROT_WRITE_INSERT:
1978 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1979 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1982 case SCSI_PROT_READ_PASS:
1983 case SCSI_PROT_WRITE_PASS:
1984 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
1985 *txop = BG_OP_IN_CSUM_OUT_CSUM;
1988 case SCSI_PROT_READ_INSERT:
1989 case SCSI_PROT_WRITE_STRIP:
1990 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1991 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1994 case SCSI_PROT_NORMAL:
2005 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
2006 * @phba: The Hba for which this call is being executed.
2007 * @sc: pointer to scsi command we're working on
2008 * @bpl: pointer to buffer list for protection groups
2009 * @datacnt: number of segments of data that have been dma mapped
2011 * This function sets up BPL buffer list for protection groups of
2012 * type LPFC_PG_TYPE_NO_DIF
2014 * This is usually used when the HBA is instructed to generate
2015 * DIFs and insert them into data stream (or strip DIF from
2016 * incoming data stream)
2018 * The buffer list consists of just one protection group described
2020 * +-------------------------+
2021 * start of prot group --> | PDE_5 |
2022 * +-------------------------+
2024 * +-------------------------+
2026 * +-------------------------+
2027 * |more Data BDE's ... (opt)|
2028 * +-------------------------+
2031 * Note: Data s/g buffers have been dma mapped
2033 * Returns the number of BDEs added to the BPL.
2036 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2037 struct ulp_bde64 *bpl, int datasegcnt)
2039 struct scatterlist *sgde = NULL; /* s/g data entry */
2040 struct lpfc_pde5 *pde5 = NULL;
2041 struct lpfc_pde6 *pde6 = NULL;
2042 dma_addr_t physaddr;
2043 int i = 0, num_bde = 0, status;
2044 int datadir = sc->sc_data_direction;
2045 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2048 uint32_t checking = 1;
2053 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2057 /* extract some info from the scsi command for pde*/
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)
2071 /* setup PDE5 with what we have */
2072 pde5 = (struct lpfc_pde5 *) bpl;
2073 memset(pde5, 0, sizeof(struct lpfc_pde5));
2074 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
2076 /* Endianness conversion if necessary for PDE5 */
2077 pde5->word0 = cpu_to_le32(pde5->word0);
2078 pde5->reftag = cpu_to_le32(reftag);
2080 /* advance bpl and increment bde count */
2083 pde6 = (struct lpfc_pde6 *) bpl;
2085 /* setup PDE6 with the rest of the info */
2086 memset(pde6, 0, sizeof(struct lpfc_pde6));
2087 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2088 bf_set(pde6_optx, pde6, txop);
2089 bf_set(pde6_oprx, pde6, rxop);
2092 * We only need to check the data on READs, for WRITEs
2093 * protection data is automatically generated, not checked.
2095 if (datadir == DMA_FROM_DEVICE) {
2096 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2097 bf_set(pde6_ce, pde6, checking);
2099 bf_set(pde6_ce, pde6, 0);
2101 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2102 bf_set(pde6_re, pde6, checking);
2104 bf_set(pde6_re, pde6, 0);
2106 bf_set(pde6_ai, pde6, 1);
2107 bf_set(pde6_ae, pde6, 0);
2108 bf_set(pde6_apptagval, pde6, 0);
2110 /* Endianness conversion if necessary for PDE6 */
2111 pde6->word0 = cpu_to_le32(pde6->word0);
2112 pde6->word1 = cpu_to_le32(pde6->word1);
2113 pde6->word2 = cpu_to_le32(pde6->word2);
2115 /* advance bpl and increment bde count */
2119 /* assumption: caller has already run dma_map_sg on command data */
2120 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2121 physaddr = sg_dma_address(sgde);
2122 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2123 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2124 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2125 if (datadir == DMA_TO_DEVICE)
2126 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2128 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2129 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2139 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2140 * @phba: The Hba for which this call is being executed.
2141 * @sc: pointer to scsi command we're working on
2142 * @bpl: pointer to buffer list for protection groups
2143 * @datacnt: number of segments of data that have been dma mapped
2144 * @protcnt: number of segment of protection data that have been dma mapped
2146 * This function sets up BPL buffer list for protection groups of
2147 * type LPFC_PG_TYPE_DIF
2149 * This is usually used when DIFs are in their own buffers,
2150 * separate from the data. The HBA can then by instructed
2151 * to place the DIFs in the outgoing stream. For read operations,
2152 * The HBA could extract the DIFs and place it in DIF buffers.
2154 * The buffer list for this type consists of one or more of the
2155 * protection groups described below:
2156 * +-------------------------+
2157 * start of first prot group --> | PDE_5 |
2158 * +-------------------------+
2160 * +-------------------------+
2161 * | PDE_7 (Prot BDE) |
2162 * +-------------------------+
2164 * +-------------------------+
2165 * |more Data BDE's ... (opt)|
2166 * +-------------------------+
2167 * start of new prot group --> | PDE_5 |
2168 * +-------------------------+
2170 * +-------------------------+
2172 * Note: It is assumed that both data and protection s/g buffers have been
2175 * Returns the number of BDEs added to the BPL.
2178 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2179 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2181 struct scatterlist *sgde = NULL; /* s/g data entry */
2182 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2183 struct lpfc_pde5 *pde5 = NULL;
2184 struct lpfc_pde6 *pde6 = NULL;
2185 struct lpfc_pde7 *pde7 = NULL;
2186 dma_addr_t dataphysaddr, protphysaddr;
2187 unsigned short curr_data = 0, curr_prot = 0;
2188 unsigned int split_offset;
2189 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2190 unsigned int protgrp_blks, protgrp_bytes;
2191 unsigned int remainder, subtotal;
2193 int datadir = sc->sc_data_direction;
2194 unsigned char pgdone = 0, alldone = 0;
2196 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2199 uint32_t checking = 1;
2204 sgpe = scsi_prot_sglist(sc);
2205 sgde = scsi_sglist(sc);
2207 if (!sgpe || !sgde) {
2208 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2209 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2214 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2218 /* extract some info from the scsi command */
2219 blksize = lpfc_cmd_blksize(sc);
2220 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2222 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2223 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2225 if (rc & BG_ERR_SWAP)
2226 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2227 if (rc & BG_ERR_CHECK)
2234 /* Check to see if we ran out of space */
2235 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2238 /* setup PDE5 with what we have */
2239 pde5 = (struct lpfc_pde5 *) bpl;
2240 memset(pde5, 0, sizeof(struct lpfc_pde5));
2241 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
2243 /* Endianness conversion if necessary for PDE5 */
2244 pde5->word0 = cpu_to_le32(pde5->word0);
2245 pde5->reftag = cpu_to_le32(reftag);
2247 /* advance bpl and increment bde count */
2250 pde6 = (struct lpfc_pde6 *) bpl;
2252 /* setup PDE6 with the rest of the info */
2253 memset(pde6, 0, sizeof(struct lpfc_pde6));
2254 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2255 bf_set(pde6_optx, pde6, txop);
2256 bf_set(pde6_oprx, pde6, rxop);
2258 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2259 bf_set(pde6_ce, pde6, checking);
2261 bf_set(pde6_ce, pde6, 0);
2263 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2264 bf_set(pde6_re, pde6, checking);
2266 bf_set(pde6_re, pde6, 0);
2268 bf_set(pde6_ai, pde6, 1);
2269 bf_set(pde6_ae, pde6, 0);
2270 bf_set(pde6_apptagval, pde6, 0);
2272 /* Endianness conversion if necessary for PDE6 */
2273 pde6->word0 = cpu_to_le32(pde6->word0);
2274 pde6->word1 = cpu_to_le32(pde6->word1);
2275 pde6->word2 = cpu_to_le32(pde6->word2);
2277 /* advance bpl and increment bde count */
2281 /* setup the first BDE that points to protection buffer */
2282 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2283 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2285 /* must be integer multiple of the DIF block length */
2286 BUG_ON(protgroup_len % 8);
2288 pde7 = (struct lpfc_pde7 *) bpl;
2289 memset(pde7, 0, sizeof(struct lpfc_pde7));
2290 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2292 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2293 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
2295 protgrp_blks = protgroup_len / 8;
2296 protgrp_bytes = protgrp_blks * blksize;
2298 /* check if this pde is crossing the 4K boundary; if so split */
2299 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2300 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2301 protgroup_offset += protgroup_remainder;
2302 protgrp_blks = protgroup_remainder / 8;
2303 protgrp_bytes = protgrp_blks * blksize;
2305 protgroup_offset = 0;
2311 /* setup BDE's for data blocks associated with DIF data */
2313 subtotal = 0; /* total bytes processed for current prot grp */
2315 /* Check to see if we ran out of space */
2316 if (num_bde >= phba->cfg_total_seg_cnt)
2320 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2321 "9065 BLKGRD:%s Invalid data segment\n",
2326 dataphysaddr = sg_dma_address(sgde) + split_offset;
2327 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2328 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2330 remainder = sg_dma_len(sgde) - split_offset;
2332 if ((subtotal + remainder) <= protgrp_bytes) {
2333 /* we can use this whole buffer */
2334 bpl->tus.f.bdeSize = remainder;
2337 if ((subtotal + remainder) == protgrp_bytes)
2340 /* must split this buffer with next prot grp */
2341 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2342 split_offset += bpl->tus.f.bdeSize;
2345 subtotal += bpl->tus.f.bdeSize;
2347 if (datadir == DMA_TO_DEVICE)
2348 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2350 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2351 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2359 /* Move to the next s/g segment if possible */
2360 sgde = sg_next(sgde);
2364 if (protgroup_offset) {
2365 /* update the reference tag */
2366 reftag += protgrp_blks;
2372 if (curr_prot == protcnt) {
2374 } else if (curr_prot < protcnt) {
2375 /* advance to next prot buffer */
2376 sgpe = sg_next(sgpe);
2379 /* update the reference tag */
2380 reftag += protgrp_blks;
2382 /* if we're here, we have a bug */
2383 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2384 "9054 BLKGRD: bug in %s\n", __func__);
2394 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2395 * @phba: The Hba for which this call is being executed.
2396 * @sc: pointer to scsi command we're working on
2397 * @sgl: pointer to buffer list for protection groups
2398 * @datacnt: number of segments of data that have been dma mapped
2400 * This function sets up SGL buffer list for protection groups of
2401 * type LPFC_PG_TYPE_NO_DIF
2403 * This is usually used when the HBA is instructed to generate
2404 * DIFs and insert them into data stream (or strip DIF from
2405 * incoming data stream)
2407 * The buffer list consists of just one protection group described
2409 * +-------------------------+
2410 * start of prot group --> | DI_SEED |
2411 * +-------------------------+
2413 * +-------------------------+
2414 * |more Data SGE's ... (opt)|
2415 * +-------------------------+
2418 * Note: Data s/g buffers have been dma mapped
2420 * Returns the number of SGEs added to the SGL.
2423 lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2424 struct sli4_sge *sgl, int datasegcnt)
2426 struct scatterlist *sgde = NULL; /* s/g data entry */
2427 struct sli4_sge_diseed *diseed = NULL;
2428 dma_addr_t physaddr;
2429 int i = 0, num_sge = 0, status;
2433 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2436 uint32_t checking = 1;
2438 uint32_t dma_offset = 0;
2440 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2444 /* extract some info from the scsi command for pde*/
2445 blksize = lpfc_cmd_blksize(sc);
2446 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2448 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2449 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2451 if (rc & BG_ERR_SWAP)
2452 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2453 if (rc & BG_ERR_CHECK)
2458 /* setup DISEED with what we have */
2459 diseed = (struct sli4_sge_diseed *) sgl;
2460 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2461 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2463 /* Endianness conversion if necessary */
2464 diseed->ref_tag = cpu_to_le32(reftag);
2465 diseed->ref_tag_tran = diseed->ref_tag;
2468 * We only need to check the data on READs, for WRITEs
2469 * protection data is automatically generated, not checked.
2471 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2472 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2473 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2475 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2477 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2478 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2480 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2483 /* setup DISEED with the rest of the info */
2484 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2485 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2487 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2488 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2490 /* Endianness conversion if necessary for DISEED */
2491 diseed->word2 = cpu_to_le32(diseed->word2);
2492 diseed->word3 = cpu_to_le32(diseed->word3);
2494 /* advance bpl and increment sge count */
2498 /* assumption: caller has already run dma_map_sg on command data */
2499 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2500 physaddr = sg_dma_address(sgde);
2501 dma_len = sg_dma_len(sgde);
2502 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2503 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2504 if ((i + 1) == datasegcnt)
2505 bf_set(lpfc_sli4_sge_last, sgl, 1);
2507 bf_set(lpfc_sli4_sge_last, sgl, 0);
2508 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2509 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2511 sgl->sge_len = cpu_to_le32(dma_len);
2512 dma_offset += dma_len;
2523 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2524 * @phba: The Hba for which this call is being executed.
2525 * @sc: pointer to scsi command we're working on
2526 * @sgl: pointer to buffer list for protection groups
2527 * @datacnt: number of segments of data that have been dma mapped
2528 * @protcnt: number of segment of protection data that have been dma mapped
2530 * This function sets up SGL buffer list for protection groups of
2531 * type LPFC_PG_TYPE_DIF
2533 * This is usually used when DIFs are in their own buffers,
2534 * separate from the data. The HBA can then by instructed
2535 * to place the DIFs in the outgoing stream. For read operations,
2536 * The HBA could extract the DIFs and place it in DIF buffers.
2538 * The buffer list for this type consists of one or more of the
2539 * protection groups described below:
2540 * +-------------------------+
2541 * start of first prot group --> | DISEED |
2542 * +-------------------------+
2543 * | DIF (Prot SGE) |
2544 * +-------------------------+
2546 * +-------------------------+
2547 * |more Data SGE's ... (opt)|
2548 * +-------------------------+
2549 * start of new prot group --> | DISEED |
2550 * +-------------------------+
2552 * +-------------------------+
2554 * Note: It is assumed that both data and protection s/g buffers have been
2557 * Returns the number of SGEs added to the SGL.
2560 lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2561 struct sli4_sge *sgl, int datacnt, int protcnt)
2563 struct scatterlist *sgde = NULL; /* s/g data entry */
2564 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2565 struct sli4_sge_diseed *diseed = NULL;
2566 dma_addr_t dataphysaddr, protphysaddr;
2567 unsigned short curr_data = 0, curr_prot = 0;
2568 unsigned int split_offset;
2569 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2570 unsigned int protgrp_blks, protgrp_bytes;
2571 unsigned int remainder, subtotal;
2573 unsigned char pgdone = 0, alldone = 0;
2578 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2581 uint32_t checking = 1;
2582 uint32_t dma_offset = 0;
2585 sgpe = scsi_prot_sglist(sc);
2586 sgde = scsi_sglist(sc);
2588 if (!sgpe || !sgde) {
2589 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2590 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2595 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2599 /* extract some info from the scsi command */
2600 blksize = lpfc_cmd_blksize(sc);
2601 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2603 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2604 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2606 if (rc & BG_ERR_SWAP)
2607 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2608 if (rc & BG_ERR_CHECK)
2615 /* Check to see if we ran out of space */
2616 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2619 /* setup DISEED with what we have */
2620 diseed = (struct sli4_sge_diseed *) sgl;
2621 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2622 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2624 /* Endianness conversion if necessary */
2625 diseed->ref_tag = cpu_to_le32(reftag);
2626 diseed->ref_tag_tran = diseed->ref_tag;
2628 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
2629 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2632 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2634 * When in this mode, the hardware will replace
2635 * the guard tag from the host with a
2636 * newly generated good CRC for the wire.
2637 * Switch to raw mode here to avoid this
2638 * behavior. What the host sends gets put on the wire.
2640 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2641 txop = BG_OP_RAW_MODE;
2642 rxop = BG_OP_RAW_MODE;
2647 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2648 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2650 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2652 /* setup DISEED with the rest of the info */
2653 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2654 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2656 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2657 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2659 /* Endianness conversion if necessary for DISEED */
2660 diseed->word2 = cpu_to_le32(diseed->word2);
2661 diseed->word3 = cpu_to_le32(diseed->word3);
2663 /* advance sgl and increment bde count */
2667 /* setup the first BDE that points to protection buffer */
2668 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2669 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2671 /* must be integer multiple of the DIF block length */
2672 BUG_ON(protgroup_len % 8);
2674 /* Now setup DIF SGE */
2676 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2677 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2678 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2679 sgl->word2 = cpu_to_le32(sgl->word2);
2681 protgrp_blks = protgroup_len / 8;
2682 protgrp_bytes = protgrp_blks * blksize;
2684 /* check if DIF SGE is crossing the 4K boundary; if so split */
2685 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2686 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2687 protgroup_offset += protgroup_remainder;
2688 protgrp_blks = protgroup_remainder / 8;
2689 protgrp_bytes = protgrp_blks * blksize;
2691 protgroup_offset = 0;
2697 /* setup SGE's for data blocks associated with DIF data */
2699 subtotal = 0; /* total bytes processed for current prot grp */
2701 /* Check to see if we ran out of space */
2702 if (num_sge >= phba->cfg_total_seg_cnt)
2706 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2707 "9086 BLKGRD:%s Invalid data segment\n",
2712 dataphysaddr = sg_dma_address(sgde) + split_offset;
2714 remainder = sg_dma_len(sgde) - split_offset;
2716 if ((subtotal + remainder) <= protgrp_bytes) {
2717 /* we can use this whole buffer */
2718 dma_len = remainder;
2721 if ((subtotal + remainder) == protgrp_bytes)
2724 /* must split this buffer with next prot grp */
2725 dma_len = protgrp_bytes - subtotal;
2726 split_offset += dma_len;
2729 subtotal += dma_len;
2731 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2732 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2733 bf_set(lpfc_sli4_sge_last, sgl, 0);
2734 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2735 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2737 sgl->sge_len = cpu_to_le32(dma_len);
2738 dma_offset += dma_len;
2746 /* Move to the next s/g segment if possible */
2747 sgde = sg_next(sgde);
2750 if (protgroup_offset) {
2751 /* update the reference tag */
2752 reftag += protgrp_blks;
2758 if (curr_prot == protcnt) {
2759 bf_set(lpfc_sli4_sge_last, sgl, 1);
2761 } else if (curr_prot < protcnt) {
2762 /* advance to next prot buffer */
2763 sgpe = sg_next(sgpe);
2766 /* update the reference tag */
2767 reftag += protgrp_blks;
2769 /* if we're here, we have a bug */
2770 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2771 "9085 BLKGRD: bug in %s\n", __func__);
2782 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2783 * @phba: The Hba for which this call is being executed.
2784 * @sc: pointer to scsi command we're working on
2786 * Given a SCSI command that supports DIF, determine composition of protection
2787 * groups involved in setting up buffer lists
2789 * Returns: Protection group type (with or without DIF)
2793 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2795 int ret = LPFC_PG_TYPE_INVALID;
2796 unsigned char op = scsi_get_prot_op(sc);
2799 case SCSI_PROT_READ_STRIP:
2800 case SCSI_PROT_WRITE_INSERT:
2801 ret = LPFC_PG_TYPE_NO_DIF;
2803 case SCSI_PROT_READ_INSERT:
2804 case SCSI_PROT_WRITE_STRIP:
2805 case SCSI_PROT_READ_PASS:
2806 case SCSI_PROT_WRITE_PASS:
2807 ret = LPFC_PG_TYPE_DIF_BUF;
2811 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2812 "9021 Unsupported protection op:%d\n",
2820 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2821 * @phba: The Hba for which this call is being executed.
2822 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2824 * Adjust the data length to account for how much data
2825 * is actually on the wire.
2827 * returns the adjusted data length
2830 lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2831 struct lpfc_scsi_buf *lpfc_cmd)
2833 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2836 fcpdl = scsi_bufflen(sc);
2838 /* Check if there is protection data on the wire */
2839 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2840 /* Read check for protection data */
2841 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2845 /* Write check for protection data */
2846 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2851 * If we are in DIF Type 1 mode every data block has a 8 byte
2852 * DIF (trailer) attached to it. Must ajust FCP data length
2853 * to account for the protection data.
2855 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
2861 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2862 * @phba: The Hba for which this call is being executed.
2863 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2865 * This is the protection/DIF aware version of
2866 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2867 * two functions eventually, but for now, it's here
2870 lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2871 struct lpfc_scsi_buf *lpfc_cmd)
2873 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2874 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2875 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2876 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2877 uint32_t num_bde = 0;
2878 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2879 int prot_group_type = 0;
2883 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2884 * fcp_rsp regions to the first data bde entry
2887 if (scsi_sg_count(scsi_cmnd)) {
2889 * The driver stores the segment count returned from pci_map_sg
2890 * because this a count of dma-mappings used to map the use_sg
2891 * pages. They are not guaranteed to be the same for those
2892 * architectures that implement an IOMMU.
2894 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2895 scsi_sglist(scsi_cmnd),
2896 scsi_sg_count(scsi_cmnd), datadir);
2897 if (unlikely(!datasegcnt))
2900 lpfc_cmd->seg_cnt = datasegcnt;
2902 /* First check if data segment count from SCSI Layer is good */
2903 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2906 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2908 switch (prot_group_type) {
2909 case LPFC_PG_TYPE_NO_DIF:
2911 /* Here we need to add a PDE5 and PDE6 to the count */
2912 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2915 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2917 /* we should have 2 or more entries in buffer list */
2922 case LPFC_PG_TYPE_DIF_BUF:
2924 * This type indicates that protection buffers are
2925 * passed to the driver, so that needs to be prepared
2928 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2929 scsi_prot_sglist(scsi_cmnd),
2930 scsi_prot_sg_count(scsi_cmnd), datadir);
2931 if (unlikely(!protsegcnt)) {
2932 scsi_dma_unmap(scsi_cmnd);
2936 lpfc_cmd->prot_seg_cnt = protsegcnt;
2939 * There is a minimun of 4 BPLs used for every
2940 * protection data segment.
2942 if ((lpfc_cmd->prot_seg_cnt * 4) >
2943 (phba->cfg_total_seg_cnt - 2))
2946 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2947 datasegcnt, protsegcnt);
2948 /* we should have 3 or more entries in buffer list */
2949 if ((num_bde < 3) ||
2950 (num_bde > phba->cfg_total_seg_cnt))
2954 case LPFC_PG_TYPE_INVALID:
2956 scsi_dma_unmap(scsi_cmnd);
2957 lpfc_cmd->seg_cnt = 0;
2959 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2960 "9022 Unexpected protection group %i\n",
2967 * Finish initializing those IOCB fields that are dependent on the
2968 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2969 * reinitialized since all iocb memory resources are used many times
2970 * for transmit, receive, and continuation bpl's.
2972 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2973 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2974 iocb_cmd->ulpBdeCount = 1;
2975 iocb_cmd->ulpLe = 1;
2977 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
2978 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2981 * Due to difference in data length between DIF/non-DIF paths,
2982 * we need to set word 4 of IOCB here
2984 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2988 if (lpfc_cmd->seg_cnt)
2989 scsi_dma_unmap(scsi_cmnd);
2990 if (lpfc_cmd->prot_seg_cnt)
2991 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2992 scsi_prot_sg_count(scsi_cmnd),
2993 scsi_cmnd->sc_data_direction);
2995 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2996 "9023 Cannot setup S/G List for HBA"
2997 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2998 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2999 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3000 prot_group_type, num_bde);
3002 lpfc_cmd->seg_cnt = 0;
3003 lpfc_cmd->prot_seg_cnt = 0;
3008 * This function calcuates the T10 DIF guard tag
3009 * on the specified data using a CRC algorithmn
3013 lpfc_bg_crc(uint8_t *data, int count)
3018 crc = crc_t10dif(data, count);
3019 x = cpu_to_be16(crc);
3024 * This function calcuates the T10 DIF guard tag
3025 * on the specified data using a CSUM algorithmn
3026 * using ip_compute_csum.
3029 lpfc_bg_csum(uint8_t *data, int count)
3033 ret = ip_compute_csum(data, count);
3038 * This function examines the protection data to try to determine
3039 * what type of T10-DIF error occurred.
3042 lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3044 struct scatterlist *sgpe; /* s/g prot entry */
3045 struct scatterlist *sgde; /* s/g data entry */
3046 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3047 struct scsi_dif_tuple *src = NULL;
3048 uint8_t *data_src = NULL;
3049 uint16_t guard_tag, guard_type;
3050 uint16_t start_app_tag, app_tag;
3051 uint32_t start_ref_tag, ref_tag;
3052 int prot, protsegcnt;
3053 int err_type, len, data_len;
3054 int chk_ref, chk_app, chk_guard;
3058 err_type = BGS_GUARD_ERR_MASK;
3062 /* First check to see if there is protection data to examine */
3063 prot = scsi_get_prot_op(cmd);
3064 if ((prot == SCSI_PROT_READ_STRIP) ||
3065 (prot == SCSI_PROT_WRITE_INSERT) ||
3066 (prot == SCSI_PROT_NORMAL))
3069 /* Currently the driver just supports ref_tag and guard_tag checking */
3074 /* Setup a ptr to the protection data provided by the SCSI host */
3075 sgpe = scsi_prot_sglist(cmd);
3076 protsegcnt = lpfc_cmd->prot_seg_cnt;
3078 if (sgpe && protsegcnt) {
3081 * We will only try to verify guard tag if the segment
3082 * data length is a multiple of the blksize.
3084 sgde = scsi_sglist(cmd);
3085 blksize = lpfc_cmd_blksize(cmd);
3086 data_src = (uint8_t *)sg_virt(sgde);
3087 data_len = sgde->length;
3088 if ((data_len & (blksize - 1)) == 0)
3090 guard_type = scsi_host_get_guard(cmd->device->host);
3092 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3093 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
3094 start_app_tag = src->app_tag;
3096 while (src && protsegcnt) {
3100 * First check to see if a protection data
3103 if ((src->ref_tag == 0xffffffff) ||
3104 (src->app_tag == 0xffff)) {
3109 /* First Guard Tag checking */
3111 guard_tag = src->guard_tag;
3112 if (lpfc_cmd_guard_csum(cmd))
3113 sum = lpfc_bg_csum(data_src,
3116 sum = lpfc_bg_crc(data_src,
3118 if ((guard_tag != sum)) {
3119 err_type = BGS_GUARD_ERR_MASK;
3124 /* Reference Tag checking */
3125 ref_tag = be32_to_cpu(src->ref_tag);
3126 if (chk_ref && (ref_tag != start_ref_tag)) {
3127 err_type = BGS_REFTAG_ERR_MASK;
3132 /* App Tag checking */
3133 app_tag = src->app_tag;
3134 if (chk_app && (app_tag != start_app_tag)) {
3135 err_type = BGS_APPTAG_ERR_MASK;
3139 len -= sizeof(struct scsi_dif_tuple);
3144 data_src += blksize;
3145 data_len -= blksize;
3148 * Are we at the end of the Data segment?
3149 * The data segment is only used for Guard
3152 if (chk_guard && (data_len == 0)) {
3154 sgde = sg_next(sgde);
3158 data_src = (uint8_t *)sg_virt(sgde);
3159 data_len = sgde->length;
3160 if ((data_len & (blksize - 1)) == 0)
3165 /* Goto the next Protection data segment */
3166 sgpe = sg_next(sgpe);
3168 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3177 if (err_type == BGS_GUARD_ERR_MASK) {
3178 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3180 cmd->result = DRIVER_SENSE << 24
3181 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3182 phba->bg_guard_err_cnt++;
3183 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3184 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3185 (unsigned long)scsi_get_lba(cmd),
3188 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3189 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3191 cmd->result = DRIVER_SENSE << 24
3192 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3194 phba->bg_reftag_err_cnt++;
3195 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3196 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3197 (unsigned long)scsi_get_lba(cmd),
3198 ref_tag, start_ref_tag);
3200 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3201 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3203 cmd->result = DRIVER_SENSE << 24
3204 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3206 phba->bg_apptag_err_cnt++;
3207 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3208 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3209 (unsigned long)scsi_get_lba(cmd),
3210 app_tag, start_app_tag);
3216 * This function checks for BlockGuard errors detected by
3217 * the HBA. In case of errors, the ASC/ASCQ fields in the
3218 * sense buffer will be set accordingly, paired with
3219 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3220 * detected corruption.
3223 * 0 - No error found
3224 * 1 - BlockGuard error found
3225 * -1 - Internal error (bad profile, ...etc)
3228 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3229 struct lpfc_iocbq *pIocbOut)
3231 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3232 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3234 uint32_t bghm = bgf->bghm;
3235 uint32_t bgstat = bgf->bgstat;
3236 uint64_t failing_sector = 0;
3238 spin_lock(&_dump_buf_lock);
3239 if (!_dump_buf_done) {
3240 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3241 " Data for %u blocks to debugfs\n",
3242 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3243 lpfc_debug_save_data(phba, cmd);
3245 /* If we have a prot sgl, save the DIF buffer */
3246 if (lpfc_prot_group_type(phba, cmd) ==
3247 LPFC_PG_TYPE_DIF_BUF) {
3248 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3249 "Saving DIF for %u blocks to debugfs\n",
3250 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3251 lpfc_debug_save_dif(phba, cmd);
3256 spin_unlock(&_dump_buf_lock);
3258 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3259 cmd->result = ScsiResult(DID_ERROR, 0);
3260 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3261 "9072 BLKGRD: Invalid BG Profile in cmd"
3262 " 0x%x lba 0x%llx blk cnt 0x%x "
3263 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3264 (unsigned long long)scsi_get_lba(cmd),
3265 blk_rq_sectors(cmd->request), bgstat, bghm);
3270 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3271 cmd->result = ScsiResult(DID_ERROR, 0);
3272 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3273 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3274 " 0x%x lba 0x%llx blk cnt 0x%x "
3275 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3276 (unsigned long long)scsi_get_lba(cmd),
3277 blk_rq_sectors(cmd->request), bgstat, bghm);
3282 if (lpfc_bgs_get_guard_err(bgstat)) {
3285 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3287 cmd->result = DRIVER_SENSE << 24
3288 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3289 phba->bg_guard_err_cnt++;
3290 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3291 "9055 BLKGRD: Guard Tag error in cmd"
3292 " 0x%x lba 0x%llx blk cnt 0x%x "
3293 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3294 (unsigned long long)scsi_get_lba(cmd),
3295 blk_rq_sectors(cmd->request), bgstat, bghm);
3298 if (lpfc_bgs_get_reftag_err(bgstat)) {
3301 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3303 cmd->result = DRIVER_SENSE << 24
3304 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3306 phba->bg_reftag_err_cnt++;
3307 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3308 "9056 BLKGRD: Ref Tag error in cmd"
3309 " 0x%x lba 0x%llx blk cnt 0x%x "
3310 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3311 (unsigned long long)scsi_get_lba(cmd),
3312 blk_rq_sectors(cmd->request), bgstat, bghm);
3315 if (lpfc_bgs_get_apptag_err(bgstat)) {
3318 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3320 cmd->result = DRIVER_SENSE << 24
3321 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3323 phba->bg_apptag_err_cnt++;
3324 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3325 "9061 BLKGRD: App Tag error in cmd"
3326 " 0x%x lba 0x%llx blk cnt 0x%x "
3327 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3328 (unsigned long long)scsi_get_lba(cmd),
3329 blk_rq_sectors(cmd->request), bgstat, bghm);
3332 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3334 * setup sense data descriptor 0 per SPC-4 as an information
3335 * field, and put the failing LBA in it.
3336 * This code assumes there was also a guard/app/ref tag error
3339 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3340 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3341 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3342 cmd->sense_buffer[10] = 0x80; /* Validity bit */
3344 /* bghm is a "on the wire" FC frame based count */
3345 switch (scsi_get_prot_op(cmd)) {
3346 case SCSI_PROT_READ_INSERT:
3347 case SCSI_PROT_WRITE_STRIP:
3348 bghm /= cmd->device->sector_size;
3350 case SCSI_PROT_READ_STRIP:
3351 case SCSI_PROT_WRITE_INSERT:
3352 case SCSI_PROT_READ_PASS:
3353 case SCSI_PROT_WRITE_PASS:
3354 bghm /= (cmd->device->sector_size +
3355 sizeof(struct scsi_dif_tuple));
3359 failing_sector = scsi_get_lba(cmd);
3360 failing_sector += bghm;
3362 /* Descriptor Information */
3363 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3367 /* No error was reported - problem in FW? */
3368 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3369 "9057 BLKGRD: Unknown error in cmd"
3370 " 0x%x lba 0x%llx blk cnt 0x%x "
3371 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3372 (unsigned long long)scsi_get_lba(cmd),
3373 blk_rq_sectors(cmd->request), bgstat, bghm);
3375 /* Calcuate what type of error it was */
3376 lpfc_calc_bg_err(phba, lpfc_cmd);
3383 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3384 * @phba: The Hba for which this call is being executed.
3385 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3387 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3388 * field of @lpfc_cmd for device with SLI-4 interface spec.
3395 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3397 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3398 struct scatterlist *sgel = NULL;
3399 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3400 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
3401 struct sli4_sge *first_data_sgl;
3402 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3403 dma_addr_t physaddr;
3404 uint32_t num_bde = 0;
3406 uint32_t dma_offset = 0;
3408 struct ulp_bde64 *bde;
3411 * There are three possibilities here - use scatter-gather segment, use
3412 * the single mapping, or neither. Start the lpfc command prep by
3413 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3416 if (scsi_sg_count(scsi_cmnd)) {
3418 * The driver stores the segment count returned from pci_map_sg
3419 * because this a count of dma-mappings used to map the use_sg
3420 * pages. They are not guaranteed to be the same for those
3421 * architectures that implement an IOMMU.
3424 nseg = scsi_dma_map(scsi_cmnd);
3425 if (unlikely(!nseg))
3428 /* clear the last flag in the fcp_rsp map entry */
3429 sgl->word2 = le32_to_cpu(sgl->word2);
3430 bf_set(lpfc_sli4_sge_last, sgl, 0);
3431 sgl->word2 = cpu_to_le32(sgl->word2);
3433 first_data_sgl = sgl;
3434 lpfc_cmd->seg_cnt = nseg;
3435 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3436 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3437 " %s: Too many sg segments from "
3438 "dma_map_sg. Config %d, seg_cnt %d\n",
3439 __func__, phba->cfg_sg_seg_cnt,
3441 lpfc_cmd->seg_cnt = 0;
3442 scsi_dma_unmap(scsi_cmnd);
3447 * The driver established a maximum scatter-gather segment count
3448 * during probe that limits the number of sg elements in any
3449 * single scsi command. Just run through the seg_cnt and format
3451 * When using SLI-3 the driver will try to fit all the BDEs into
3452 * the IOCB. If it can't then the BDEs get added to a BPL as it
3453 * does for SLI-2 mode.
3455 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3456 physaddr = sg_dma_address(sgel);
3457 dma_len = sg_dma_len(sgel);
3458 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3459 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
3460 sgl->word2 = le32_to_cpu(sgl->word2);
3461 if ((num_bde + 1) == nseg)
3462 bf_set(lpfc_sli4_sge_last, sgl, 1);
3464 bf_set(lpfc_sli4_sge_last, sgl, 0);
3465 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3466 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
3467 sgl->word2 = cpu_to_le32(sgl->word2);
3468 sgl->sge_len = cpu_to_le32(dma_len);
3469 dma_offset += dma_len;
3472 /* setup the performance hint (first data BDE) if enabled */
3473 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3474 bde = (struct ulp_bde64 *)
3475 &(iocb_cmd->unsli3.sli3Words[5]);
3476 bde->addrLow = first_data_sgl->addr_lo;
3477 bde->addrHigh = first_data_sgl->addr_hi;
3478 bde->tus.f.bdeSize =
3479 le32_to_cpu(first_data_sgl->sge_len);
3480 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3481 bde->tus.w = cpu_to_le32(bde->tus.w);
3485 /* clear the last flag in the fcp_rsp map entry */
3486 sgl->word2 = le32_to_cpu(sgl->word2);
3487 bf_set(lpfc_sli4_sge_last, sgl, 1);
3488 sgl->word2 = cpu_to_le32(sgl->word2);
3492 * Finish initializing those IOCB fields that are dependent on the
3493 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3494 * explicitly reinitialized.
3495 * all iocb memory resources are reused.
3497 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3500 * Due to difference in data length between DIF/non-DIF paths,
3501 * we need to set word 4 of IOCB here
3503 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3508 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3509 * @phba: The Hba for which this call is being executed.
3510 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3512 * This is the protection/DIF aware version of
3513 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3514 * two functions eventually, but for now, it's here
3517 lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3518 struct lpfc_scsi_buf *lpfc_cmd)
3520 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3521 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3522 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3523 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3524 uint32_t num_sge = 0;
3525 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3526 int prot_group_type = 0;
3530 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3531 * fcp_rsp regions to the first data sge entry
3533 if (scsi_sg_count(scsi_cmnd)) {
3535 * The driver stores the segment count returned from pci_map_sg
3536 * because this a count of dma-mappings used to map the use_sg
3537 * pages. They are not guaranteed to be the same for those
3538 * architectures that implement an IOMMU.
3540 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3541 scsi_sglist(scsi_cmnd),
3542 scsi_sg_count(scsi_cmnd), datadir);
3543 if (unlikely(!datasegcnt))
3547 /* clear the last flag in the fcp_rsp map entry */
3548 sgl->word2 = le32_to_cpu(sgl->word2);
3549 bf_set(lpfc_sli4_sge_last, sgl, 0);
3550 sgl->word2 = cpu_to_le32(sgl->word2);
3553 lpfc_cmd->seg_cnt = datasegcnt;
3555 /* First check if data segment count from SCSI Layer is good */
3556 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3559 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3561 switch (prot_group_type) {
3562 case LPFC_PG_TYPE_NO_DIF:
3563 /* Here we need to add a DISEED to the count */
3564 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3567 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3570 /* we should have 2 or more entries in buffer list */
3575 case LPFC_PG_TYPE_DIF_BUF:
3577 * This type indicates that protection buffers are
3578 * passed to the driver, so that needs to be prepared
3581 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3582 scsi_prot_sglist(scsi_cmnd),
3583 scsi_prot_sg_count(scsi_cmnd), datadir);
3584 if (unlikely(!protsegcnt)) {
3585 scsi_dma_unmap(scsi_cmnd);
3589 lpfc_cmd->prot_seg_cnt = protsegcnt;
3591 * There is a minimun of 3 SGEs used for every
3592 * protection data segment.
3594 if ((lpfc_cmd->prot_seg_cnt * 3) >
3595 (phba->cfg_total_seg_cnt - 2))
3598 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3599 datasegcnt, protsegcnt);
3601 /* we should have 3 or more entries in buffer list */
3602 if ((num_sge < 3) ||
3603 (num_sge > phba->cfg_total_seg_cnt))
3607 case LPFC_PG_TYPE_INVALID:
3609 scsi_dma_unmap(scsi_cmnd);
3610 lpfc_cmd->seg_cnt = 0;
3612 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3613 "9083 Unexpected protection group %i\n",
3619 switch (scsi_get_prot_op(scsi_cmnd)) {
3620 case SCSI_PROT_WRITE_STRIP:
3621 case SCSI_PROT_READ_STRIP:
3622 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3624 case SCSI_PROT_WRITE_INSERT:
3625 case SCSI_PROT_READ_INSERT:
3626 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3628 case SCSI_PROT_WRITE_PASS:
3629 case SCSI_PROT_READ_PASS:
3630 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3634 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3635 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3638 * Due to difference in data length between DIF/non-DIF paths,
3639 * we need to set word 4 of IOCB here
3641 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3645 if (lpfc_cmd->seg_cnt)
3646 scsi_dma_unmap(scsi_cmnd);
3647 if (lpfc_cmd->prot_seg_cnt)
3648 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3649 scsi_prot_sg_count(scsi_cmnd),
3650 scsi_cmnd->sc_data_direction);
3652 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3653 "9084 Cannot setup S/G List for HBA"
3654 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3655 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3656 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3657 prot_group_type, num_sge);
3659 lpfc_cmd->seg_cnt = 0;
3660 lpfc_cmd->prot_seg_cnt = 0;
3665 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3666 * @phba: The Hba for which this call is being executed.
3667 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3669 * This routine wraps the actual DMA mapping function pointer from the
3677 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3679 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3683 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3685 * @phba: The Hba for which this call is being executed.
3686 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3688 * This routine wraps the actual DMA mapping function pointer from the
3696 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3698 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3702 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
3703 * @phba: Pointer to hba context object.
3704 * @vport: Pointer to vport object.
3705 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3706 * @rsp_iocb: Pointer to response iocb object which reported error.
3708 * This function posts an event when there is a SCSI command reporting
3709 * error from the scsi device.
3712 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3713 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3714 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3715 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3716 uint32_t resp_info = fcprsp->rspStatus2;
3717 uint32_t scsi_status = fcprsp->rspStatus3;
3718 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3719 struct lpfc_fast_path_event *fast_path_evt = NULL;
3720 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3721 unsigned long flags;
3723 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3726 /* If there is queuefull or busy condition send a scsi event */
3727 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3728 (cmnd->result == SAM_STAT_BUSY)) {
3729 fast_path_evt = lpfc_alloc_fast_evt(phba);
3732 fast_path_evt->un.scsi_evt.event_type =
3734 fast_path_evt->un.scsi_evt.subcategory =
3735 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3736 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3737 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3738 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3739 &pnode->nlp_portname, sizeof(struct lpfc_name));
3740 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3741 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3742 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3743 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3744 fast_path_evt = lpfc_alloc_fast_evt(phba);
3747 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3749 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3750 LPFC_EVENT_CHECK_COND;
3751 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3753 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3754 &pnode->nlp_portname, sizeof(struct lpfc_name));
3755 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3756 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3757 fast_path_evt->un.check_cond_evt.sense_key =
3758 cmnd->sense_buffer[2] & 0xf;
3759 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3760 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3761 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3763 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3764 ((scsi_status == SAM_STAT_GOOD) &&
3765 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3767 * If status is good or resid does not match with fcp_param and
3768 * there is valid fcpi_parm, then there is a read_check error
3770 fast_path_evt = lpfc_alloc_fast_evt(phba);
3773 fast_path_evt->un.read_check_error.header.event_type =
3774 FC_REG_FABRIC_EVENT;
3775 fast_path_evt->un.read_check_error.header.subcategory =
3776 LPFC_EVENT_FCPRDCHKERR;
3777 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3778 &pnode->nlp_portname, sizeof(struct lpfc_name));
3779 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3780 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3781 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3782 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3783 fast_path_evt->un.read_check_error.fcpiparam =
3788 fast_path_evt->vport = vport;
3789 spin_lock_irqsave(&phba->hbalock, flags);
3790 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3791 spin_unlock_irqrestore(&phba->hbalock, flags);
3792 lpfc_worker_wake_up(phba);
3797 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3798 * @phba: The HBA for which this call is being executed.
3799 * @psb: The scsi buffer which is going to be un-mapped.
3801 * This routine does DMA un-mapping of scatter gather list of scsi command
3802 * field of @lpfc_cmd for device with SLI-3 interface spec.
3805 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
3808 * There are only two special cases to consider. (1) the scsi command
3809 * requested scatter-gather usage or (2) the scsi command allocated
3810 * a request buffer, but did not request use_sg. There is a third
3811 * case, but it does not require resource deallocation.
3813 if (psb->seg_cnt > 0)
3814 scsi_dma_unmap(psb->pCmd);
3815 if (psb->prot_seg_cnt > 0)
3816 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3817 scsi_prot_sg_count(psb->pCmd),
3818 psb->pCmd->sc_data_direction);
3822 * lpfc_handler_fcp_err - FCP response handler
3823 * @vport: The virtual port for which this call is being executed.
3824 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3825 * @rsp_iocb: The response IOCB which contains FCP error.
3827 * This routine is called to process response IOCB with status field
3828 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3829 * based upon SCSI and FCP error.
3832 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3833 struct lpfc_iocbq *rsp_iocb)
3835 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3836 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3837 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3838 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3839 uint32_t resp_info = fcprsp->rspStatus2;
3840 uint32_t scsi_status = fcprsp->rspStatus3;
3842 uint32_t host_status = DID_OK;
3843 uint32_t rsplen = 0;
3844 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
3848 * If this is a task management command, there is no
3849 * scsi packet associated with this lpfc_cmd. The driver
3852 if (fcpcmd->fcpCntl2) {
3857 if (resp_info & RSP_LEN_VALID) {
3858 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3859 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
3860 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3861 "2719 Invalid response length: "
3862 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3864 cmnd->device->lun, cmnd->cmnd[0],
3866 host_status = DID_ERROR;
3869 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3870 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3871 "2757 Protocol failure detected during "
3872 "processing of FCP I/O op: "
3873 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3875 cmnd->device->lun, cmnd->cmnd[0],
3877 host_status = DID_ERROR;
3882 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3883 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3884 if (snslen > SCSI_SENSE_BUFFERSIZE)
3885 snslen = SCSI_SENSE_BUFFERSIZE;
3887 if (resp_info & RSP_LEN_VALID)
3888 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3889 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3891 lp = (uint32_t *)cmnd->sense_buffer;
3893 /* special handling for under run conditions */
3894 if (!scsi_status && (resp_info & RESID_UNDER)) {
3895 /* don't log under runs if fcp set... */
3896 if (vport->cfg_log_verbose & LOG_FCP)
3897 logit = LOG_FCP_ERROR;
3898 /* unless operator says so */
3899 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3900 logit = LOG_FCP_UNDER;
3903 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3904 "9024 FCP command x%x failed: x%x SNS x%x x%x "
3905 "Data: x%x x%x x%x x%x x%x\n",
3906 cmnd->cmnd[0], scsi_status,
3907 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3908 be32_to_cpu(fcprsp->rspResId),
3909 be32_to_cpu(fcprsp->rspSnsLen),
3910 be32_to_cpu(fcprsp->rspRspLen),
3913 scsi_set_resid(cmnd, 0);
3914 if (resp_info & RESID_UNDER) {
3915 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
3917 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
3918 "9025 FCP Read Underrun, expected %d, "
3919 "residual %d Data: x%x x%x x%x\n",
3920 be32_to_cpu(fcpcmd->fcpDl),
3921 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3925 * If there is an under run check if under run reported by
3926 * storage array is same as the under run reported by HBA.
3927 * If this is not same, there is a dropped frame.
3929 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3931 (scsi_get_resid(cmnd) != fcpi_parm)) {
3932 lpfc_printf_vlog(vport, KERN_WARNING,
3933 LOG_FCP | LOG_FCP_ERROR,
3934 "9026 FCP Read Check Error "
3935 "and Underrun Data: x%x x%x x%x x%x\n",
3936 be32_to_cpu(fcpcmd->fcpDl),
3937 scsi_get_resid(cmnd), fcpi_parm,
3939 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3940 host_status = DID_ERROR;
3943 * The cmnd->underflow is the minimum number of bytes that must
3944 * be transferred for this command. Provided a sense condition
3945 * is not present, make sure the actual amount transferred is at
3946 * least the underflow value or fail.
3948 if (!(resp_info & SNS_LEN_VALID) &&
3949 (scsi_status == SAM_STAT_GOOD) &&
3950 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3951 < cmnd->underflow)) {
3952 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3953 "9027 FCP command x%x residual "
3954 "underrun converted to error "
3955 "Data: x%x x%x x%x\n",
3956 cmnd->cmnd[0], scsi_bufflen(cmnd),
3957 scsi_get_resid(cmnd), cmnd->underflow);
3958 host_status = DID_ERROR;
3960 } else if (resp_info & RESID_OVER) {
3961 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3962 "9028 FCP command x%x residual overrun error. "
3963 "Data: x%x x%x\n", cmnd->cmnd[0],
3964 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
3965 host_status = DID_ERROR;
3968 * Check SLI validation that all the transfer was actually done
3969 * (fcpi_parm should be zero).
3971 } else if (fcpi_parm) {
3972 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
3973 "9029 FCP Data Transfer Check Error: "
3974 "x%x x%x x%x x%x x%x\n",
3975 be32_to_cpu(fcpcmd->fcpDl),
3976 be32_to_cpu(fcprsp->rspResId),
3977 fcpi_parm, cmnd->cmnd[0], scsi_status);
3978 switch (scsi_status) {
3980 case SAM_STAT_CHECK_CONDITION:
3981 /* Fabric dropped a data frame. Fail any successful
3982 * command in which we detected dropped frames.
3983 * A status of good or some check conditions could
3984 * be considered a successful command.
3986 host_status = DID_ERROR;
3989 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3993 cmnd->result = ScsiResult(host_status, scsi_status);
3994 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
3998 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
3999 * @phba: The Hba for which this call is being executed.
4000 * @pIocbIn: The command IOCBQ for the scsi cmnd.
4001 * @pIocbOut: The response IOCBQ for the scsi cmnd.
4003 * This routine assigns scsi command result by looking into response IOCB
4004 * status field appropriately. This routine handles QUEUE FULL condition as
4005 * well by ramping down device queue depth.
4008 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4009 struct lpfc_iocbq *pIocbOut)
4011 struct lpfc_scsi_buf *lpfc_cmd =
4012 (struct lpfc_scsi_buf *) pIocbIn->context1;
4013 struct lpfc_vport *vport = pIocbIn->vport;
4014 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4015 struct lpfc_nodelist *pnode = rdata->pnode;
4016 struct scsi_cmnd *cmd;
4018 struct scsi_device *tmp_sdev;
4020 unsigned long flags;
4021 struct lpfc_fast_path_event *fast_path_evt;
4022 struct Scsi_Host *shost;
4023 uint32_t queue_depth, scsi_id;
4024 uint32_t logit = LOG_FCP;
4026 /* Sanity check on return of outstanding command */
4027 if (!(lpfc_cmd->pCmd))
4029 cmd = lpfc_cmd->pCmd;
4030 shost = cmd->device->host;
4032 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
4033 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
4034 /* pick up SLI4 exhange busy status from HBA */
4035 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4037 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4038 if (lpfc_cmd->prot_data_type) {
4039 struct scsi_dif_tuple *src = NULL;
4041 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4043 * Used to restore any changes to protection
4044 * data for error injection.
4046 switch (lpfc_cmd->prot_data_type) {
4047 case LPFC_INJERR_REFTAG:
4049 lpfc_cmd->prot_data;
4051 case LPFC_INJERR_APPTAG:
4053 (uint16_t)lpfc_cmd->prot_data;
4055 case LPFC_INJERR_GUARD:
4057 (uint16_t)lpfc_cmd->prot_data;
4063 lpfc_cmd->prot_data = 0;
4064 lpfc_cmd->prot_data_type = 0;
4065 lpfc_cmd->prot_data_segment = NULL;
4068 if (pnode && NLP_CHK_NODE_ACT(pnode))
4069 atomic_dec(&pnode->cmd_pending);
4071 if (lpfc_cmd->status) {
4072 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4073 (lpfc_cmd->result & IOERR_DRVR_MASK))
4074 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4075 else if (lpfc_cmd->status >= IOSTAT_CNT)
4076 lpfc_cmd->status = IOSTAT_DEFAULT;
4077 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4078 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4079 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4080 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
4083 logit = LOG_FCP | LOG_FCP_UNDER;
4084 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4085 "9030 FCP cmd x%x failed <%d/%d> "
4086 "status: x%x result: x%x "
4087 "sid: x%x did: x%x oxid: x%x "
4090 cmd->device ? cmd->device->id : 0xffff,
4091 cmd->device ? cmd->device->lun : 0xffff,
4092 lpfc_cmd->status, lpfc_cmd->result,
4094 (pnode) ? pnode->nlp_DID : 0,
4095 phba->sli_rev == LPFC_SLI_REV4 ?
4096 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4097 pIocbOut->iocb.ulpContext,
4098 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4100 switch (lpfc_cmd->status) {
4101 case IOSTAT_FCP_RSP_ERROR:
4102 /* Call FCP RSP handler to determine result */
4103 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
4105 case IOSTAT_NPORT_BSY:
4106 case IOSTAT_FABRIC_BSY:
4107 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
4108 fast_path_evt = lpfc_alloc_fast_evt(phba);
4111 fast_path_evt->un.fabric_evt.event_type =
4112 FC_REG_FABRIC_EVENT;
4113 fast_path_evt->un.fabric_evt.subcategory =
4114 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4115 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4116 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4117 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4118 &pnode->nlp_portname,
4119 sizeof(struct lpfc_name));
4120 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4121 &pnode->nlp_nodename,
4122 sizeof(struct lpfc_name));
4124 fast_path_evt->vport = vport;
4125 fast_path_evt->work_evt.evt =
4126 LPFC_EVT_FASTPATH_MGMT_EVT;
4127 spin_lock_irqsave(&phba->hbalock, flags);
4128 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4130 spin_unlock_irqrestore(&phba->hbalock, flags);
4131 lpfc_worker_wake_up(phba);
4133 case IOSTAT_LOCAL_REJECT:
4134 case IOSTAT_REMOTE_STOP:
4135 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4137 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4138 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4140 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4141 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4144 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
4145 lpfc_cmd->result == IOERR_NO_RESOURCES ||
4146 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4147 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
4148 cmd->result = ScsiResult(DID_REQUEUE, 0);
4151 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4152 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4153 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4154 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4156 * This is a response for a BG enabled
4157 * cmd. Parse BG error
4159 lpfc_parse_bg_err(phba, lpfc_cmd,
4163 lpfc_printf_vlog(vport, KERN_WARNING,
4165 "9031 non-zero BGSTAT "
4166 "on unprotected cmd\n");
4169 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4170 && (phba->sli_rev == LPFC_SLI_REV4)
4171 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4172 /* This IO was aborted by the target, we don't
4173 * know the rxid and because we did not send the
4174 * ABTS we cannot generate and RRQ.
4176 lpfc_set_rrq_active(phba, pnode,
4177 lpfc_cmd->cur_iocbq.sli4_lxritag,
4180 /* else: fall through */
4182 cmd->result = ScsiResult(DID_ERROR, 0);
4186 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
4187 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4188 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4191 cmd->result = ScsiResult(DID_OK, 0);
4193 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4194 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4196 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4197 "0710 Iodone <%d/%d> cmd %p, error "
4198 "x%x SNS x%x x%x Data: x%x x%x\n",
4199 cmd->device->id, cmd->device->lun, cmd,
4200 cmd->result, *lp, *(lp + 3), cmd->retries,
4201 scsi_get_resid(cmd));
4204 lpfc_update_stats(phba, lpfc_cmd);
4205 result = cmd->result;
4206 if (vport->cfg_max_scsicmpl_time &&
4207 time_after(jiffies, lpfc_cmd->start_time +
4208 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4209 spin_lock_irqsave(shost->host_lock, flags);
4210 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4211 if (pnode->cmd_qdepth >
4212 atomic_read(&pnode->cmd_pending) &&
4213 (atomic_read(&pnode->cmd_pending) >
4214 LPFC_MIN_TGT_QDEPTH) &&
4215 ((cmd->cmnd[0] == READ_10) ||
4216 (cmd->cmnd[0] == WRITE_10)))
4218 atomic_read(&pnode->cmd_pending);
4220 pnode->last_change_time = jiffies;
4222 spin_unlock_irqrestore(shost->host_lock, flags);
4223 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4224 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
4225 time_after(jiffies, pnode->last_change_time +
4226 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
4227 spin_lock_irqsave(shost->host_lock, flags);
4228 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4230 depth = depth ? depth : 1;
4231 pnode->cmd_qdepth += depth;
4232 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4233 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
4234 pnode->last_change_time = jiffies;
4235 spin_unlock_irqrestore(shost->host_lock, flags);
4239 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4241 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4242 queue_depth = cmd->device->queue_depth;
4243 scsi_id = cmd->device->id;
4244 cmd->scsi_done(cmd);
4246 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4247 spin_lock_irqsave(&phba->hbalock, flags);
4248 lpfc_cmd->pCmd = NULL;
4249 spin_unlock_irqrestore(&phba->hbalock, flags);
4252 * If there is a thread waiting for command completion
4253 * wake up the thread.
4255 spin_lock_irqsave(shost->host_lock, flags);
4256 if (lpfc_cmd->waitq)
4257 wake_up(lpfc_cmd->waitq);
4258 spin_unlock_irqrestore(shost->host_lock, flags);
4259 lpfc_release_scsi_buf(phba, lpfc_cmd);
4264 lpfc_rampup_queue_depth(vport, queue_depth);
4267 * Check for queue full. If the lun is reporting queue full, then
4268 * back off the lun queue depth to prevent target overloads.
4270 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4271 NLP_CHK_NODE_ACT(pnode)) {
4272 shost_for_each_device(tmp_sdev, shost) {
4273 if (tmp_sdev->id != scsi_id)
4275 depth = scsi_track_queue_full(tmp_sdev,
4276 tmp_sdev->queue_depth-1);
4279 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4280 "0711 detected queue full - lun queue "
4281 "depth adjusted to %d.\n", depth);
4282 lpfc_send_sdev_queuedepth_change_event(phba, vport,
4289 spin_lock_irqsave(&phba->hbalock, flags);
4290 lpfc_cmd->pCmd = NULL;
4291 spin_unlock_irqrestore(&phba->hbalock, flags);
4294 * If there is a thread waiting for command completion
4295 * wake up the thread.
4297 spin_lock_irqsave(shost->host_lock, flags);
4298 if (lpfc_cmd->waitq)
4299 wake_up(lpfc_cmd->waitq);
4300 spin_unlock_irqrestore(shost->host_lock, flags);
4302 lpfc_release_scsi_buf(phba, lpfc_cmd);
4306 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4307 * @data: A pointer to the immediate command data portion of the IOCB.
4308 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4310 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4311 * byte swapping the data to big endian format for transmission on the wire.
4314 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4317 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4318 i += sizeof(uint32_t), j++) {
4319 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4324 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
4325 * @vport: The virtual port for which this call is being executed.
4326 * @lpfc_cmd: The scsi command which needs to send.
4327 * @pnode: Pointer to lpfc_nodelist.
4329 * This routine initializes fcp_cmnd and iocb data structure from scsi command
4330 * to transfer for device with SLI3 interface spec.
4333 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
4334 struct lpfc_nodelist *pnode)
4336 struct lpfc_hba *phba = vport->phba;
4337 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4338 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4339 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4340 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4341 int datadir = scsi_cmnd->sc_data_direction;
4346 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4349 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
4350 /* clear task management bits */
4351 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
4353 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4354 &lpfc_cmd->fcp_cmnd->fcp_lun);
4356 ptr = &fcp_cmnd->fcpCdb[0];
4357 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4358 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4359 ptr += scsi_cmnd->cmd_len;
4360 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4363 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4365 case HEAD_OF_QUEUE_TAG:
4366 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4368 case ORDERED_QUEUE_TAG:
4369 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4372 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4376 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4378 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4381 * There are three possibilities here - use scatter-gather segment, use
4382 * the single mapping, or neither. Start the lpfc command prep by
4383 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4386 if (scsi_sg_count(scsi_cmnd)) {
4387 if (datadir == DMA_TO_DEVICE) {
4388 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
4390 iocb_cmd->ulpPU = PARM_READ_CHECK;
4392 iocb_cmd->un.fcpi.fcpi_parm = 0;
4393 iocb_cmd->ulpPU = 0;
4395 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4396 phba->fc4OutputRequests++;
4398 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4399 iocb_cmd->ulpPU = PARM_READ_CHECK;
4400 fcp_cmnd->fcpCntl3 = READ_DATA;
4401 phba->fc4InputRequests++;
4404 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4405 iocb_cmd->un.fcpi.fcpi_parm = 0;
4406 iocb_cmd->ulpPU = 0;
4407 fcp_cmnd->fcpCntl3 = 0;
4408 phba->fc4ControlRequests++;
4410 if (phba->sli_rev == 3 &&
4411 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4412 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
4414 * Finish initializing those IOCB fields that are independent
4415 * of the scsi_cmnd request_buffer
4417 piocbq->iocb.ulpContext = pnode->nlp_rpi;
4419 piocbq->iocb.ulpContext =
4420 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
4421 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4422 piocbq->iocb.ulpFCP2Rcvy = 1;
4424 piocbq->iocb.ulpFCP2Rcvy = 0;
4426 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4427 piocbq->context1 = lpfc_cmd;
4428 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4429 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
4430 piocbq->vport = vport;
4434 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
4435 * @vport: The virtual port for which this call is being executed.
4436 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4437 * @lun: Logical unit number.
4438 * @task_mgmt_cmd: SCSI task management command.
4440 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4441 * for device with SLI-3 interface spec.
4448 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
4449 struct lpfc_scsi_buf *lpfc_cmd,
4451 uint8_t task_mgmt_cmd)
4453 struct lpfc_iocbq *piocbq;
4455 struct fcp_cmnd *fcp_cmnd;
4456 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4457 struct lpfc_nodelist *ndlp = rdata->pnode;
4459 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4460 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
4463 piocbq = &(lpfc_cmd->cur_iocbq);
4464 piocbq->vport = vport;
4466 piocb = &piocbq->iocb;
4468 fcp_cmnd = lpfc_cmd->fcp_cmnd;
4469 /* Clear out any old data in the FCP command area */
4470 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4471 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
4472 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
4473 if (vport->phba->sli_rev == 3 &&
4474 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4475 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
4476 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
4477 piocb->ulpContext = ndlp->nlp_rpi;
4478 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4480 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4482 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4483 piocb->ulpFCP2Rcvy = 1;
4485 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4487 /* ulpTimeout is only one byte */
4488 if (lpfc_cmd->timeout > 0xff) {
4490 * Do not timeout the command at the firmware level.
4491 * The driver will provide the timeout mechanism.
4493 piocb->ulpTimeout = 0;
4495 piocb->ulpTimeout = lpfc_cmd->timeout;
4497 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4498 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
4504 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
4505 * @phba: The hba struct for which this call is being executed.
4506 * @dev_grp: The HBA PCI-Device group number.
4508 * This routine sets up the SCSI interface API function jump table in @phba
4510 * Returns: 0 - success, -ENODEV - failure.
4513 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4516 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4517 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
4520 case LPFC_PCI_DEV_LP:
4521 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4522 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
4523 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
4524 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
4525 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
4527 case LPFC_PCI_DEV_OC:
4528 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4529 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
4530 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
4531 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
4532 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
4535 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4536 "1418 Invalid HBA PCI-device group: 0x%x\n",
4541 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
4542 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4547 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
4548 * @phba: The Hba for which this call is being executed.
4549 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4550 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4552 * This routine is IOCB completion routine for device reset and target reset
4553 * routine. This routine release scsi buffer associated with lpfc_cmd.
4556 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4557 struct lpfc_iocbq *cmdiocbq,
4558 struct lpfc_iocbq *rspiocbq)
4560 struct lpfc_scsi_buf *lpfc_cmd =
4561 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4563 lpfc_release_scsi_buf(phba, lpfc_cmd);
4568 * lpfc_info - Info entry point of scsi_host_template data structure
4569 * @host: The scsi host for which this call is being executed.
4571 * This routine provides module information about hba.
4574 * Pointer to char - Success.
4577 lpfc_info(struct Scsi_Host *host)
4579 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4580 struct lpfc_hba *phba = vport->phba;
4581 int len, link_speed = 0;
4582 static char lpfcinfobuf[384];
4584 memset(lpfcinfobuf,0,384);
4585 if (phba && phba->pcidev){
4586 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4587 len = strlen(lpfcinfobuf);
4588 snprintf(lpfcinfobuf + len,
4590 " on PCI bus %02x device %02x irq %d",
4591 phba->pcidev->bus->number,
4592 phba->pcidev->devfn,
4594 len = strlen(lpfcinfobuf);
4595 if (phba->Port[0]) {
4596 snprintf(lpfcinfobuf + len,
4601 len = strlen(lpfcinfobuf);
4602 if (phba->sli_rev <= LPFC_SLI_REV3) {
4603 link_speed = lpfc_sli_port_speed_get(phba);
4605 if (phba->sli4_hba.link_state.logical_speed)
4607 phba->sli4_hba.link_state.logical_speed;
4609 link_speed = phba->sli4_hba.link_state.speed;
4611 if (link_speed != 0)
4612 snprintf(lpfcinfobuf + len, 384-len,
4613 " Logical Link Speed: %d Mbps", link_speed);
4619 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
4620 * @phba: The Hba for which this call is being executed.
4622 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4623 * The default value of cfg_poll_tmo is 10 milliseconds.
4625 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4627 unsigned long poll_tmo_expires =
4628 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4630 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
4631 mod_timer(&phba->fcp_poll_timer,
4636 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
4637 * @phba: The Hba for which this call is being executed.
4639 * This routine starts the fcp_poll_timer of @phba.
4641 void lpfc_poll_start_timer(struct lpfc_hba * phba)
4643 lpfc_poll_rearm_timer(phba);
4647 * lpfc_poll_timeout - Restart polling timer
4648 * @ptr: Map to lpfc_hba data structure pointer.
4650 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4651 * and FCP Ring interrupt is disable.
4654 void lpfc_poll_timeout(unsigned long ptr)
4656 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4658 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4659 lpfc_sli_handle_fast_ring_event(phba,
4660 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4662 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4663 lpfc_poll_rearm_timer(phba);
4668 * lpfc_queuecommand - scsi_host_template queuecommand entry point
4669 * @cmnd: Pointer to scsi_cmnd data structure.
4670 * @done: Pointer to done routine.
4672 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4673 * This routine prepares an IOCB from scsi command and provides to firmware.
4674 * The @done callback is invoked after driver finished processing the command.
4678 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4681 lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
4683 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4684 struct lpfc_hba *phba = vport->phba;
4685 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4686 struct lpfc_nodelist *ndlp;
4687 struct lpfc_scsi_buf *lpfc_cmd;
4688 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
4691 err = fc_remote_port_chkready(rport);
4694 goto out_fail_command;
4696 ndlp = rdata->pnode;
4698 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
4699 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
4701 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4702 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4703 " op:%02x str=%s without registering for"
4704 " BlockGuard - Rejecting command\n",
4705 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4706 dif_op_str[scsi_get_prot_op(cmnd)]);
4707 goto out_fail_command;
4711 * Catch race where our node has transitioned, but the
4712 * transport is still transitioning.
4714 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4716 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
4719 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
4720 if (lpfc_cmd == NULL) {
4721 lpfc_rampdown_queue_depth(phba);
4723 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4724 "0707 driver's buffer pool is empty, "
4730 * Store the midlayer's command structure for the completion phase
4731 * and complete the command initialization.
4733 lpfc_cmd->pCmd = cmnd;
4734 lpfc_cmd->rdata = rdata;
4735 lpfc_cmd->timeout = 0;
4736 lpfc_cmd->start_time = jiffies;
4737 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4739 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
4740 if (vport->phba->cfg_enable_bg) {
4741 lpfc_printf_vlog(vport,
4742 KERN_INFO, LOG_SCSI_CMD,
4743 "9033 BLKGRD: rcvd %s cmd:x%x "
4744 "sector x%llx cnt %u pt %x\n",
4745 dif_op_str[scsi_get_prot_op(cmnd)],
4747 (unsigned long long)scsi_get_lba(cmnd),
4748 blk_rq_sectors(cmnd->request),
4749 (cmnd->cmnd[1]>>5));
4751 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4753 if (vport->phba->cfg_enable_bg) {
4754 lpfc_printf_vlog(vport,
4755 KERN_INFO, LOG_SCSI_CMD,
4756 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4757 "x%x sector x%llx cnt %u pt %x\n",
4759 (unsigned long long)scsi_get_lba(cmnd),
4760 blk_rq_sectors(cmnd->request),
4761 (cmnd->cmnd[1]>>5));
4763 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4767 goto out_host_busy_free_buf;
4769 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
4771 atomic_inc(&ndlp->cmd_pending);
4772 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4773 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
4775 atomic_dec(&ndlp->cmd_pending);
4776 goto out_host_busy_free_buf;
4778 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4779 lpfc_sli_handle_fast_ring_event(phba,
4780 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4782 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4783 lpfc_poll_rearm_timer(phba);
4788 out_host_busy_free_buf:
4789 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4790 lpfc_release_scsi_buf(phba, lpfc_cmd);
4792 return SCSI_MLQUEUE_HOST_BUSY;
4795 return SCSI_MLQUEUE_TARGET_BUSY;
4798 cmnd->scsi_done(cmnd);
4804 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
4805 * @cmnd: Pointer to scsi_cmnd data structure.
4807 * This routine aborts @cmnd pending in base driver.
4814 lpfc_abort_handler(struct scsi_cmnd *cmnd)
4816 struct Scsi_Host *shost = cmnd->device->host;
4817 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4818 struct lpfc_hba *phba = vport->phba;
4819 struct lpfc_iocbq *iocb;
4820 struct lpfc_iocbq *abtsiocb;
4821 struct lpfc_scsi_buf *lpfc_cmd;
4823 int ret = SUCCESS, status = 0;
4824 unsigned long flags;
4825 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
4827 status = fc_block_scsi_eh(cmnd);
4828 if (status != 0 && status != SUCCESS)
4831 spin_lock_irqsave(&phba->hbalock, flags);
4832 /* driver queued commands are in process of being flushed */
4833 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
4834 spin_unlock_irqrestore(&phba->hbalock, flags);
4835 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4836 "3168 SCSI Layer abort requested I/O has been "
4837 "flushed by LLD.\n");
4841 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
4842 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
4843 spin_unlock_irqrestore(&phba->hbalock, flags);
4844 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4845 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
4846 "x%x ID %d LUN %d\n",
4847 SUCCESS, cmnd->device->id, cmnd->device->lun);
4851 iocb = &lpfc_cmd->cur_iocbq;
4852 /* the command is in process of being cancelled */
4853 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4854 spin_unlock_irqrestore(&phba->hbalock, flags);
4855 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4856 "3169 SCSI Layer abort requested I/O has been "
4857 "cancelled by LLD.\n");
4861 * If pCmd field of the corresponding lpfc_scsi_buf structure
4862 * points to a different SCSI command, then the driver has
4863 * already completed this command, but the midlayer did not
4864 * see the completion before the eh fired. Just return SUCCESS.
4866 if (lpfc_cmd->pCmd != cmnd) {
4867 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4868 "3170 SCSI Layer abort requested I/O has been "
4869 "completed by LLD.\n");
4873 BUG_ON(iocb->context1 != lpfc_cmd);
4875 abtsiocb = __lpfc_sli_get_iocbq(phba);
4876 if (abtsiocb == NULL) {
4882 * The scsi command can not be in txq and it is in flight because the
4883 * pCmd is still pointig at the SCSI command we have to abort. There
4884 * is no need to search the txcmplq. Just send an abort to the FW.
4888 icmd = &abtsiocb->iocb;
4889 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4890 icmd->un.acxri.abortContextTag = cmd->ulpContext;
4891 if (phba->sli_rev == LPFC_SLI_REV4)
4892 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4894 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
4897 icmd->ulpClass = cmd->ulpClass;
4899 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4900 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
4901 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
4903 if (lpfc_is_link_up(phba))
4904 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4906 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
4908 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
4909 abtsiocb->vport = vport;
4910 /* no longer need the lock after this point */
4911 spin_unlock_irqrestore(&phba->hbalock, flags);
4913 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4915 lpfc_sli_release_iocbq(phba, abtsiocb);
4920 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4921 lpfc_sli_handle_fast_ring_event(phba,
4922 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4924 lpfc_cmd->waitq = &waitq;
4925 /* Wait for abort to complete */
4926 wait_event_timeout(waitq,
4927 (lpfc_cmd->pCmd != cmnd),
4928 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
4929 lpfc_cmd->waitq = NULL;
4931 if (lpfc_cmd->pCmd == cmnd) {
4933 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4934 "0748 abort handler timed out waiting "
4935 "for abortng I/O (xri:x%x) to complete: "
4936 "ret %#x, ID %d, LUN %d\n",
4937 iocb->sli4_xritag, ret,
4938 cmnd->device->id, cmnd->device->lun);
4943 spin_unlock_irqrestore(&phba->hbalock, flags);
4945 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4946 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
4947 "LUN %d\n", ret, cmnd->device->id,
4953 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4955 switch (task_mgmt_cmd) {
4956 case FCP_ABORT_TASK_SET:
4957 return "ABORT_TASK_SET";
4958 case FCP_CLEAR_TASK_SET:
4959 return "FCP_CLEAR_TASK_SET";
4961 return "FCP_BUS_RESET";
4963 return "FCP_LUN_RESET";
4964 case FCP_TARGET_RESET:
4965 return "FCP_TARGET_RESET";
4967 return "FCP_CLEAR_ACA";
4968 case FCP_TERMINATE_TASK:
4969 return "FCP_TERMINATE_TASK";
4976 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4977 * @vport: The virtual port for which this call is being executed.
4978 * @rdata: Pointer to remote port local data
4979 * @tgt_id: Target ID of remote device.
4980 * @lun_id: Lun number for the TMF
4981 * @task_mgmt_cmd: type of TMF to send
4983 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4991 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
4992 unsigned tgt_id, unsigned int lun_id,
4993 uint8_t task_mgmt_cmd)
4995 struct lpfc_hba *phba = vport->phba;
4996 struct lpfc_scsi_buf *lpfc_cmd;
4997 struct lpfc_iocbq *iocbq;
4998 struct lpfc_iocbq *iocbqrsp;
4999 struct lpfc_nodelist *pnode = rdata->pnode;
5003 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5006 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
5007 if (lpfc_cmd == NULL)
5009 lpfc_cmd->timeout = 60;
5010 lpfc_cmd->rdata = rdata;
5012 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5015 lpfc_release_scsi_buf(phba, lpfc_cmd);
5019 iocbq = &lpfc_cmd->cur_iocbq;
5020 iocbqrsp = lpfc_sli_get_iocbq(phba);
5021 if (iocbqrsp == NULL) {
5022 lpfc_release_scsi_buf(phba, lpfc_cmd);
5026 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5027 "0702 Issue %s to TGT %d LUN %d "
5028 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
5029 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
5030 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5033 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5034 iocbq, iocbqrsp, lpfc_cmd->timeout);
5035 if (status != IOCB_SUCCESS) {
5036 if (status == IOCB_TIMEDOUT) {
5037 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
5038 ret = TIMEOUT_ERROR;
5041 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5042 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5043 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5045 lpfc_taskmgmt_name(task_mgmt_cmd),
5046 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
5047 iocbqrsp->iocb.un.ulpWord[4],
5049 } else if (status == IOCB_BUSY)
5054 lpfc_sli_release_iocbq(phba, iocbqrsp);
5056 if (ret != TIMEOUT_ERROR)
5057 lpfc_release_scsi_buf(phba, lpfc_cmd);
5063 * lpfc_chk_tgt_mapped -
5064 * @vport: The virtual port to check on
5065 * @cmnd: Pointer to scsi_cmnd data structure.
5067 * This routine delays until the scsi target (aka rport) for the
5068 * command exists (is present and logged in) or we declare it non-existent.
5075 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5077 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
5078 struct lpfc_nodelist *pnode;
5079 unsigned long later;
5082 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5083 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5086 pnode = rdata->pnode;
5088 * If target is not in a MAPPED state, delay until
5089 * target is rediscovered or devloss timeout expires.
5091 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5092 while (time_after(later, jiffies)) {
5093 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5095 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5097 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5098 rdata = cmnd->device->hostdata;
5101 pnode = rdata->pnode;
5103 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5104 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5110 * lpfc_reset_flush_io_context -
5111 * @vport: The virtual port (scsi_host) for the flush context
5112 * @tgt_id: If aborting by Target contect - specifies the target id
5113 * @lun_id: If aborting by Lun context - specifies the lun id
5114 * @context: specifies the context level to flush at.
5116 * After a reset condition via TMF, we need to flush orphaned i/o
5117 * contexts from the adapter. This routine aborts any contexts
5118 * outstanding, then waits for their completions. The wait is
5119 * bounded by devloss_tmo though.
5126 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5127 uint64_t lun_id, lpfc_ctx_cmd context)
5129 struct lpfc_hba *phba = vport->phba;
5130 unsigned long later;
5133 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5135 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5136 tgt_id, lun_id, context);
5137 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5138 while (time_after(later, jiffies) && cnt) {
5139 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5140 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5143 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5144 "0724 I/O flush failure for context %s : cnt x%x\n",
5145 ((context == LPFC_CTX_LUN) ? "LUN" :
5146 ((context == LPFC_CTX_TGT) ? "TGT" :
5147 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5155 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
5156 * @cmnd: Pointer to scsi_cmnd data structure.
5158 * This routine does a device reset by sending a LUN_RESET task management
5166 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
5168 struct Scsi_Host *shost = cmnd->device->host;
5169 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5170 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
5171 struct lpfc_nodelist *pnode;
5172 unsigned tgt_id = cmnd->device->id;
5173 unsigned int lun_id = cmnd->device->lun;
5174 struct lpfc_scsi_event_header scsi_event;
5175 int status, ret = SUCCESS;
5178 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5179 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5182 pnode = rdata->pnode;
5183 status = fc_block_scsi_eh(cmnd);
5184 if (status != 0 && status != SUCCESS)
5187 status = lpfc_chk_tgt_mapped(vport, cmnd);
5188 if (status == FAILED) {
5189 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5190 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5194 scsi_event.event_type = FC_REG_SCSI_EVENT;
5195 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5196 scsi_event.lun = lun_id;
5197 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5198 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5200 fc_host_post_vendor_event(shost, fc_get_event_number(),
5201 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5203 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5206 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5207 "0713 SCSI layer issued Device Reset (%d, %d) "
5208 "return x%x\n", tgt_id, lun_id, status);
5211 * We have to clean up i/o as : they may be orphaned by the TMF;
5212 * or if the TMF failed, they may be in an indeterminate state.
5214 * We will report success if all the i/o aborts successfully.
5216 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5222 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5223 * @cmnd: Pointer to scsi_cmnd data structure.
5225 * This routine does a target reset by sending a TARGET_RESET task management
5233 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5235 struct Scsi_Host *shost = cmnd->device->host;
5236 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5237 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
5238 struct lpfc_nodelist *pnode;
5239 unsigned tgt_id = cmnd->device->id;
5240 unsigned int lun_id = cmnd->device->lun;
5241 struct lpfc_scsi_event_header scsi_event;
5242 int status, ret = SUCCESS;
5245 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5246 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5249 pnode = rdata->pnode;
5250 status = fc_block_scsi_eh(cmnd);
5251 if (status != 0 && status != SUCCESS)
5254 status = lpfc_chk_tgt_mapped(vport, cmnd);
5255 if (status == FAILED) {
5256 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5257 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5261 scsi_event.event_type = FC_REG_SCSI_EVENT;
5262 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5264 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5265 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5267 fc_host_post_vendor_event(shost, fc_get_event_number(),
5268 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5270 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5273 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5274 "0723 SCSI layer issued Target Reset (%d, %d) "
5275 "return x%x\n", tgt_id, lun_id, status);
5278 * We have to clean up i/o as : they may be orphaned by the TMF;
5279 * or if the TMF failed, they may be in an indeterminate state.
5281 * We will report success if all the i/o aborts successfully.
5283 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5289 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
5290 * @cmnd: Pointer to scsi_cmnd data structure.
5292 * This routine does target reset to all targets on @cmnd->device->host.
5293 * This emulates Parallel SCSI Bus Reset Semantics.
5300 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
5302 struct Scsi_Host *shost = cmnd->device->host;
5303 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5304 struct lpfc_nodelist *ndlp = NULL;
5305 struct lpfc_scsi_event_header scsi_event;
5307 int ret = SUCCESS, status, i;
5309 scsi_event.event_type = FC_REG_SCSI_EVENT;
5310 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5312 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5313 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5315 fc_host_post_vendor_event(shost, fc_get_event_number(),
5316 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5318 status = fc_block_scsi_eh(cmnd);
5319 if (status != 0 && status != SUCCESS)
5323 * Since the driver manages a single bus device, reset all
5324 * targets known to the driver. Should any target reset
5325 * fail, this routine returns failure to the midlayer.
5327 for (i = 0; i < LPFC_MAX_TARGET; i++) {
5328 /* Search for mapped node by target ID */
5330 spin_lock_irq(shost->host_lock);
5331 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5332 if (!NLP_CHK_NODE_ACT(ndlp))
5334 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5335 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5337 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
5338 ndlp->nlp_sid == i &&
5344 spin_unlock_irq(shost->host_lock);
5348 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5349 i, 0, FCP_TARGET_RESET);
5351 if (status != SUCCESS) {
5352 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5353 "0700 Bus Reset on target %d failed\n",
5359 * We have to clean up i/o as : they may be orphaned by the TMFs
5360 * above; or if any of the TMFs failed, they may be in an
5361 * indeterminate state.
5362 * We will report success if all the i/o aborts successfully.
5365 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5366 if (status != SUCCESS)
5369 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5370 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
5375 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5376 * @cmnd: Pointer to scsi_cmnd data structure.
5378 * This routine does host reset to the adaptor port. It brings the HBA
5379 * offline, performs a board restart, and then brings the board back online.
5380 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5381 * reject all outstanding SCSI commands to the host and error returned
5382 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5383 * of error handling, it will only return error if resetting of the adapter
5384 * is not successful; in all other cases, will return success.
5391 lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5393 struct Scsi_Host *shost = cmnd->device->host;
5394 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5395 struct lpfc_hba *phba = vport->phba;
5396 int rc, ret = SUCCESS;
5398 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5399 "3172 SCSI layer issued Host Reset Data:\n");
5401 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5403 rc = lpfc_sli_brdrestart(phba);
5406 rc = lpfc_online(phba);
5409 lpfc_unblock_mgmt_io(phba);
5411 if (ret == FAILED) {
5412 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5413 "3323 Failed host reset, bring it offline\n");
5414 lpfc_sli4_offline_eratt(phba);
5420 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
5421 * @sdev: Pointer to scsi_device.
5423 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5424 * globally available list of scsi buffers. This routine also makes sure scsi
5425 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5426 * of scsi buffer exists for the lifetime of the driver.
5433 lpfc_slave_alloc(struct scsi_device *sdev)
5435 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5436 struct lpfc_hba *phba = vport->phba;
5437 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
5439 uint32_t num_to_alloc = 0;
5440 int num_allocated = 0;
5443 if (!rport || fc_remote_port_chkready(rport))
5446 sdev->hostdata = rport->dd_data;
5447 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
5450 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5451 * available list of scsi buffers. Don't allocate more than the
5452 * HBA limit conveyed to the midlayer via the host structure. The
5453 * formula accounts for the lun_queue_depth + error handlers + 1
5454 * extra. This list of scsi bufs exists for the lifetime of the driver.
5456 total = phba->total_scsi_bufs;
5457 num_to_alloc = vport->cfg_lun_queue_depth + 2;
5459 /* If allocated buffers are enough do nothing */
5460 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5463 /* Allow some exchanges to be available always to complete discovery */
5464 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5465 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5466 "0704 At limitation of %d preallocated "
5467 "command buffers\n", total);
5469 /* Allow some exchanges to be available always to complete discovery */
5470 } else if (total + num_to_alloc >
5471 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5472 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5473 "0705 Allocation request of %d "
5474 "command buffers will exceed max of %d. "
5475 "Reducing allocation request to %d.\n",
5476 num_to_alloc, phba->cfg_hba_queue_depth,
5477 (phba->cfg_hba_queue_depth - total));
5478 num_to_alloc = phba->cfg_hba_queue_depth - total;
5480 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5481 if (num_to_alloc != num_allocated) {
5482 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5483 "0708 Allocation request of %d "
5484 "command buffers did not succeed. "
5485 "Allocated %d buffers.\n",
5486 num_to_alloc, num_allocated);
5488 if (num_allocated > 0)
5489 phba->total_scsi_bufs += num_allocated;
5494 * lpfc_slave_configure - scsi_host_template slave_configure entry point
5495 * @sdev: Pointer to scsi_device.
5497 * This routine configures following items
5498 * - Tag command queuing support for @sdev if supported.
5499 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5505 lpfc_slave_configure(struct scsi_device *sdev)
5507 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5508 struct lpfc_hba *phba = vport->phba;
5510 if (sdev->tagged_supported)
5511 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
5513 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
5515 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5516 lpfc_sli_handle_fast_ring_event(phba,
5517 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
5518 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5519 lpfc_poll_rearm_timer(phba);
5526 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
5527 * @sdev: Pointer to scsi_device.
5529 * This routine sets @sdev hostatdata filed to null.
5532 lpfc_slave_destroy(struct scsi_device *sdev)
5534 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5535 struct lpfc_hba *phba = vport->phba;
5536 atomic_dec(&phba->sdev_cnt);
5537 sdev->hostdata = NULL;
5542 struct scsi_host_template lpfc_template = {
5543 .module = THIS_MODULE,
5544 .name = LPFC_DRIVER_NAME,
5546 .queuecommand = lpfc_queuecommand,
5547 .eh_abort_handler = lpfc_abort_handler,
5548 .eh_device_reset_handler = lpfc_device_reset_handler,
5549 .eh_target_reset_handler = lpfc_target_reset_handler,
5550 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5551 .eh_host_reset_handler = lpfc_host_reset_handler,
5552 .slave_alloc = lpfc_slave_alloc,
5553 .slave_configure = lpfc_slave_configure,
5554 .slave_destroy = lpfc_slave_destroy,
5555 .scan_finished = lpfc_scan_finished,
5557 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5558 .cmd_per_lun = LPFC_CMD_PER_LUN,
5559 .use_clustering = ENABLE_CLUSTERING,
5560 .shost_attrs = lpfc_hba_attrs,
5561 .max_sectors = 0xFFFF,
5562 .vendor_id = LPFC_NL_VENDOR_ID,
5563 .change_queue_depth = lpfc_change_queue_depth,
5564 .change_queue_type = lpfc_change_queue_type,
5567 struct scsi_host_template lpfc_vport_template = {
5568 .module = THIS_MODULE,
5569 .name = LPFC_DRIVER_NAME,
5571 .queuecommand = lpfc_queuecommand,
5572 .eh_abort_handler = lpfc_abort_handler,
5573 .eh_device_reset_handler = lpfc_device_reset_handler,
5574 .eh_target_reset_handler = lpfc_target_reset_handler,
5575 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5576 .slave_alloc = lpfc_slave_alloc,
5577 .slave_configure = lpfc_slave_configure,
5578 .slave_destroy = lpfc_slave_destroy,
5579 .scan_finished = lpfc_scan_finished,
5581 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5582 .cmd_per_lun = LPFC_CMD_PER_LUN,
5583 .use_clustering = ENABLE_CLUSTERING,
5584 .shost_attrs = lpfc_vport_attrs,
5585 .max_sectors = 0xFFFF,
5586 .change_queue_depth = lpfc_change_queue_depth,
5587 .change_queue_type = lpfc_change_queue_type,