1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2009 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/interrupt.h>
23 #include <linux/delay.h>
24 #include <asm/unaligned.h>
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_eh.h>
29 #include <scsi/scsi_host.h>
30 #include <scsi/scsi_tcq.h>
31 #include <scsi/scsi_transport_fc.h>
33 #include "lpfc_version.h"
37 #include "lpfc_sli4.h"
39 #include "lpfc_disc.h"
40 #include "lpfc_scsi.h"
42 #include "lpfc_logmsg.h"
43 #include "lpfc_crtn.h"
44 #include "lpfc_vport.h"
46 #define LPFC_RESET_WAIT 2
47 #define LPFC_ABORT_WAIT 2
51 static char *dif_op_str[] = {
53 "SCSI_PROT_READ_INSERT",
54 "SCSI_PROT_WRITE_STRIP",
55 "SCSI_PROT_READ_STRIP",
56 "SCSI_PROT_WRITE_INSERT",
57 "SCSI_PROT_READ_PASS",
58 "SCSI_PROT_WRITE_PASS",
59 "SCSI_PROT_READ_CONVERT",
60 "SCSI_PROT_WRITE_CONVERT"
63 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
66 lpfc_debug_save_data(struct scsi_cmnd *cmnd)
69 struct scatterlist *sgde = scsi_sglist(cmnd);
71 if (!_dump_buf_data) {
72 printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
79 printk(KERN_ERR "BLKGRD ERROR: data scatterlist is null\n");
83 dst = (void *) _dump_buf_data;
86 memcpy(dst, src, sgde->length);
93 lpfc_debug_save_dif(struct scsi_cmnd *cmnd)
96 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
99 printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
105 printk(KERN_ERR "BLKGRD ERROR: prot scatterlist is null\n");
112 memcpy(dst, src, sgde->length);
114 sgde = sg_next(sgde);
119 * lpfc_update_stats - Update statistical data for the command completion
120 * @phba: Pointer to HBA object.
121 * @lpfc_cmd: lpfc scsi command object pointer.
123 * This function is called when there is a command completion and this
124 * function updates the statistical data for the command completion.
127 lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
129 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
130 struct lpfc_nodelist *pnode = rdata->pnode;
131 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
133 struct Scsi_Host *shost = cmd->device->host;
134 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
135 unsigned long latency;
141 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
143 spin_lock_irqsave(shost->host_lock, flags);
144 if (!vport->stat_data_enabled ||
145 vport->stat_data_blocked ||
147 (phba->bucket_type == LPFC_NO_BUCKET)) {
148 spin_unlock_irqrestore(shost->host_lock, flags);
152 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
153 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
155 /* check array subscript bounds */
158 else if (i >= LPFC_MAX_BUCKET_COUNT)
159 i = LPFC_MAX_BUCKET_COUNT - 1;
161 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
162 if (latency <= (phba->bucket_base +
163 ((1<<i)*phba->bucket_step)))
167 pnode->lat_data[i].cmd_count++;
168 spin_unlock_irqrestore(shost->host_lock, flags);
172 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
173 * @phba: Pointer to HBA context object.
174 * @vport: Pointer to vport object.
175 * @ndlp: Pointer to FC node associated with the target.
176 * @lun: Lun number of the scsi device.
177 * @old_val: Old value of the queue depth.
178 * @new_val: New value of the queue depth.
180 * This function sends an event to the mgmt application indicating
181 * there is a change in the scsi device queue depth.
184 lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
185 struct lpfc_vport *vport,
186 struct lpfc_nodelist *ndlp,
191 struct lpfc_fast_path_event *fast_path_evt;
194 fast_path_evt = lpfc_alloc_fast_evt(phba);
198 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
200 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
201 LPFC_EVENT_VARQUEDEPTH;
203 /* Report all luns with change in queue depth */
204 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
205 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
206 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
207 &ndlp->nlp_portname, sizeof(struct lpfc_name));
208 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
209 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
212 fast_path_evt->un.queue_depth_evt.oldval = old_val;
213 fast_path_evt->un.queue_depth_evt.newval = new_val;
214 fast_path_evt->vport = vport;
216 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
217 spin_lock_irqsave(&phba->hbalock, flags);
218 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
219 spin_unlock_irqrestore(&phba->hbalock, flags);
220 lpfc_worker_wake_up(phba);
226 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
227 * @phba: The Hba for which this call is being executed.
229 * This routine is called when there is resource error in driver or firmware.
230 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
231 * posts at most 1 event each second. This routine wakes up worker thread of
232 * @phba to process WORKER_RAM_DOWN_EVENT event.
234 * This routine should be called with no lock held.
237 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
242 spin_lock_irqsave(&phba->hbalock, flags);
243 atomic_inc(&phba->num_rsrc_err);
244 phba->last_rsrc_error_time = jiffies;
246 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
247 spin_unlock_irqrestore(&phba->hbalock, flags);
251 phba->last_ramp_down_time = jiffies;
253 spin_unlock_irqrestore(&phba->hbalock, flags);
255 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
256 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
258 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
259 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
262 lpfc_worker_wake_up(phba);
267 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
268 * @phba: The Hba for which this call is being executed.
270 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
271 * post at most 1 event every 5 minute after last_ramp_up_time or
272 * last_rsrc_error_time. This routine wakes up worker thread of @phba
273 * to process WORKER_RAM_DOWN_EVENT event.
275 * This routine should be called with no lock held.
278 lpfc_rampup_queue_depth(struct lpfc_vport *vport,
279 uint32_t queue_depth)
282 struct lpfc_hba *phba = vport->phba;
284 atomic_inc(&phba->num_cmd_success);
286 if (vport->cfg_lun_queue_depth <= queue_depth)
288 spin_lock_irqsave(&phba->hbalock, flags);
289 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
290 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
291 spin_unlock_irqrestore(&phba->hbalock, flags);
294 phba->last_ramp_up_time = jiffies;
295 spin_unlock_irqrestore(&phba->hbalock, flags);
297 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
298 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
300 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
301 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
304 lpfc_worker_wake_up(phba);
309 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
310 * @phba: The Hba for which this call is being executed.
312 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
313 * thread.This routine reduces queue depth for all scsi device on each vport
314 * associated with @phba.
317 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
319 struct lpfc_vport **vports;
320 struct Scsi_Host *shost;
321 struct scsi_device *sdev;
322 unsigned long new_queue_depth, old_queue_depth;
323 unsigned long num_rsrc_err, num_cmd_success;
325 struct lpfc_rport_data *rdata;
327 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
328 num_cmd_success = atomic_read(&phba->num_cmd_success);
330 vports = lpfc_create_vport_work_array(phba);
332 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
333 shost = lpfc_shost_from_vport(vports[i]);
334 shost_for_each_device(sdev, shost) {
336 sdev->queue_depth * num_rsrc_err /
337 (num_rsrc_err + num_cmd_success);
338 if (!new_queue_depth)
339 new_queue_depth = sdev->queue_depth - 1;
341 new_queue_depth = sdev->queue_depth -
343 old_queue_depth = sdev->queue_depth;
344 if (sdev->ordered_tags)
345 scsi_adjust_queue_depth(sdev,
349 scsi_adjust_queue_depth(sdev,
352 rdata = sdev->hostdata;
354 lpfc_send_sdev_queuedepth_change_event(
357 sdev->lun, old_queue_depth,
361 lpfc_destroy_vport_work_array(phba, vports);
362 atomic_set(&phba->num_rsrc_err, 0);
363 atomic_set(&phba->num_cmd_success, 0);
367 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
368 * @phba: The Hba for which this call is being executed.
370 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
371 * thread.This routine increases queue depth for all scsi device on each vport
372 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
373 * num_cmd_success to zero.
376 lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
378 struct lpfc_vport **vports;
379 struct Scsi_Host *shost;
380 struct scsi_device *sdev;
382 struct lpfc_rport_data *rdata;
384 vports = lpfc_create_vport_work_array(phba);
386 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
387 shost = lpfc_shost_from_vport(vports[i]);
388 shost_for_each_device(sdev, shost) {
389 if (vports[i]->cfg_lun_queue_depth <=
392 if (sdev->ordered_tags)
393 scsi_adjust_queue_depth(sdev,
395 sdev->queue_depth+1);
397 scsi_adjust_queue_depth(sdev,
399 sdev->queue_depth+1);
400 rdata = sdev->hostdata;
402 lpfc_send_sdev_queuedepth_change_event(
406 sdev->queue_depth - 1,
410 lpfc_destroy_vport_work_array(phba, vports);
411 atomic_set(&phba->num_rsrc_err, 0);
412 atomic_set(&phba->num_cmd_success, 0);
416 * lpfc_scsi_dev_block - set all scsi hosts to block state
417 * @phba: Pointer to HBA context object.
419 * This function walks vport list and set each SCSI host to block state
420 * by invoking fc_remote_port_delete() routine. This function is invoked
421 * with EEH when device's PCI slot has been permanently disabled.
424 lpfc_scsi_dev_block(struct lpfc_hba *phba)
426 struct lpfc_vport **vports;
427 struct Scsi_Host *shost;
428 struct scsi_device *sdev;
429 struct fc_rport *rport;
432 vports = lpfc_create_vport_work_array(phba);
434 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
435 shost = lpfc_shost_from_vport(vports[i]);
436 shost_for_each_device(sdev, shost) {
437 rport = starget_to_rport(scsi_target(sdev));
438 fc_remote_port_delete(rport);
441 lpfc_destroy_vport_work_array(phba, vports);
445 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
446 * @vport: The virtual port for which this call being executed.
447 * @num_to_allocate: The requested number of buffers to allocate.
449 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
450 * the scsi buffer contains all the necessary information needed to initiate
451 * a SCSI I/O. The non-DMAable buffer region contains information to build
452 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
453 * and the initial BPL. In addition to allocating memory, the FCP CMND and
454 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
457 * int - number of scsi buffers that were allocated.
458 * 0 = failure, less than num_to_alloc is a partial failure.
461 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
463 struct lpfc_hba *phba = vport->phba;
464 struct lpfc_scsi_buf *psb;
465 struct ulp_bde64 *bpl;
467 dma_addr_t pdma_phys_fcp_cmd;
468 dma_addr_t pdma_phys_fcp_rsp;
469 dma_addr_t pdma_phys_bpl;
473 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
474 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
479 * Get memory from the pci pool to map the virt space to pci
480 * bus space for an I/O. The DMA buffer includes space for the
481 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
482 * necessary to support the sg_tablesize.
484 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
485 GFP_KERNEL, &psb->dma_handle);
491 /* Initialize virtual ptrs to dma_buf region. */
492 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
494 /* Allocate iotag for psb->cur_iocbq. */
495 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
497 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
498 psb->data, psb->dma_handle);
502 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
504 psb->fcp_cmnd = psb->data;
505 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
506 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
507 sizeof(struct fcp_rsp);
509 /* Initialize local short-hand pointers. */
511 pdma_phys_fcp_cmd = psb->dma_handle;
512 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
513 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
514 sizeof(struct fcp_rsp);
517 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
518 * are sg list bdes. Initialize the first two and leave the
519 * rest for queuecommand.
521 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
522 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
523 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
524 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
525 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
527 /* Setup the physical region for the FCP RSP */
528 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
529 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
530 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
531 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
532 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
535 * Since the IOCB for the FCP I/O is built into this
536 * lpfc_scsi_buf, initialize it with all known data now.
538 iocb = &psb->cur_iocbq.iocb;
539 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
540 if ((phba->sli_rev == 3) &&
541 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
542 /* fill in immediate fcp command BDE */
543 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
544 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
545 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
547 iocb->un.fcpi64.bdl.addrHigh = 0;
548 iocb->ulpBdeCount = 0;
550 /* fill in responce BDE */
551 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
553 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
554 sizeof(struct fcp_rsp);
555 iocb->unsli3.fcp_ext.rbde.addrLow =
556 putPaddrLow(pdma_phys_fcp_rsp);
557 iocb->unsli3.fcp_ext.rbde.addrHigh =
558 putPaddrHigh(pdma_phys_fcp_rsp);
560 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
561 iocb->un.fcpi64.bdl.bdeSize =
562 (2 * sizeof(struct ulp_bde64));
563 iocb->un.fcpi64.bdl.addrLow =
564 putPaddrLow(pdma_phys_bpl);
565 iocb->un.fcpi64.bdl.addrHigh =
566 putPaddrHigh(pdma_phys_bpl);
567 iocb->ulpBdeCount = 1;
570 iocb->ulpClass = CLASS3;
571 psb->status = IOSTAT_SUCCESS;
572 /* Put it back into the SCSI buffer list */
573 lpfc_release_scsi_buf_s4(phba, psb);
581 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
582 * @phba: pointer to lpfc hba data structure.
583 * @axri: pointer to the fcp xri abort wcqe structure.
585 * This routine is invoked by the worker thread to process a SLI4 fast-path
589 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
590 struct sli4_wcqe_xri_aborted *axri)
592 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
593 struct lpfc_scsi_buf *psb, *next_psb;
594 unsigned long iflag = 0;
596 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock, iflag);
597 list_for_each_entry_safe(psb, next_psb,
598 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
599 if (psb->cur_iocbq.sli4_xritag == xri) {
600 list_del(&psb->list);
601 psb->status = IOSTAT_SUCCESS;
602 spin_unlock_irqrestore(
603 &phba->sli4_hba.abts_scsi_buf_list_lock,
605 lpfc_release_scsi_buf_s4(phba, psb);
609 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
614 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
615 * @phba: pointer to lpfc hba data structure.
617 * This routine walks the list of scsi buffers that have been allocated and
618 * repost them to the HBA by using SGL block post. This is needed after a
619 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
620 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
621 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
623 * Returns: 0 = success, non-zero failure.
626 lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
628 struct lpfc_scsi_buf *psb;
629 int index, status, bcnt = 0, rcnt = 0, rc = 0;
632 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
633 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
635 /* Remove from SCSI buffer list */
636 list_del(&psb->list);
637 /* Add it to a local SCSI buffer list */
638 list_add_tail(&psb->list, &sblist);
639 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
644 /* A hole present in the XRI array, need to skip */
647 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
648 /* End of XRI array for SCSI buffer, complete */
651 /* Continue until collect up to a nembed page worth of sgls */
654 /* Now, post the SCSI buffer list sgls as a block */
655 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
656 /* Reset SCSI buffer count for next round of posting */
658 while (!list_empty(&sblist)) {
659 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
662 /* Put this back on the abort scsi list */
663 psb->status = IOSTAT_LOCAL_REJECT;
664 psb->result = IOERR_ABORT_REQUESTED;
667 psb->status = IOSTAT_SUCCESS;
668 /* Put it back into the SCSI buffer list */
669 lpfc_release_scsi_buf_s4(phba, psb);
676 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
677 * @vport: The virtual port for which this call being executed.
678 * @num_to_allocate: The requested number of buffers to allocate.
680 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
681 * the scsi buffer contains all the necessary information needed to initiate
685 * int - number of scsi buffers that were allocated.
686 * 0 = failure, less than num_to_alloc is a partial failure.
689 lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
691 struct lpfc_hba *phba = vport->phba;
692 struct lpfc_scsi_buf *psb;
693 struct sli4_sge *sgl;
695 dma_addr_t pdma_phys_fcp_cmd;
696 dma_addr_t pdma_phys_fcp_rsp;
697 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
698 uint16_t iotag, last_xritag = NO_XRI;
699 int status = 0, index;
701 int non_sequential_xri = 0;
705 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
706 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
711 * Get memory from the pci pool to map the virt space to pci bus
712 * space for an I/O. The DMA buffer includes space for the
713 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
714 * necessary to support the sg_tablesize.
716 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
717 GFP_KERNEL, &psb->dma_handle);
723 /* Initialize virtual ptrs to dma_buf region. */
724 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
726 /* Allocate iotag for psb->cur_iocbq. */
727 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
733 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
734 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
735 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
736 psb->data, psb->dma_handle);
740 if (last_xritag != NO_XRI
741 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
742 non_sequential_xri = 1;
744 list_add_tail(&psb->list, &sblist);
745 last_xritag = psb->cur_iocbq.sli4_xritag;
747 index = phba->sli4_hba.scsi_xri_cnt++;
748 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
750 psb->fcp_bpl = psb->data;
751 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
752 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
753 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
754 sizeof(struct fcp_cmnd));
756 /* Initialize local short-hand pointers. */
757 sgl = (struct sli4_sge *)psb->fcp_bpl;
758 pdma_phys_bpl = psb->dma_handle;
760 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
761 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
762 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
765 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
766 * are sg list bdes. Initialize the first two and leave the
767 * rest for queuecommand.
769 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
770 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
771 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_cmnd));
772 bf_set(lpfc_sli4_sge_last, sgl, 0);
773 sgl->word2 = cpu_to_le32(sgl->word2);
774 sgl->word3 = cpu_to_le32(sgl->word3);
777 /* Setup the physical region for the FCP RSP */
778 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
779 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
780 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_rsp));
781 bf_set(lpfc_sli4_sge_last, sgl, 1);
782 sgl->word2 = cpu_to_le32(sgl->word2);
783 sgl->word3 = cpu_to_le32(sgl->word3);
786 * Since the IOCB for the FCP I/O is built into this
787 * lpfc_scsi_buf, initialize it with all known data now.
789 iocb = &psb->cur_iocbq.iocb;
790 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
791 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
792 /* setting the BLP size to 2 * sizeof BDE may not be correct.
793 * We are setting the bpl to point to out sgl. An sgl's
794 * entries are 16 bytes, a bpl entries are 12 bytes.
796 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
797 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
798 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
799 iocb->ulpBdeCount = 1;
801 iocb->ulpClass = CLASS3;
802 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
803 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
806 psb->dma_phys_bpl = pdma_phys_bpl;
807 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
808 if (non_sequential_xri) {
809 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
811 psb->cur_iocbq.sli4_xritag);
813 /* Put this back on the abort scsi list */
814 psb->status = IOSTAT_LOCAL_REJECT;
815 psb->result = IOERR_ABORT_REQUESTED;
818 psb->status = IOSTAT_SUCCESS;
819 /* Put it back into the SCSI buffer list */
820 lpfc_release_scsi_buf_s4(phba, psb);
825 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
826 /* Reset SCSI buffer count for next round of posting */
827 while (!list_empty(&sblist)) {
828 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
831 /* Put this back on the abort scsi list */
832 psb->status = IOSTAT_LOCAL_REJECT;
833 psb->result = IOERR_ABORT_REQUESTED;
836 psb->status = IOSTAT_SUCCESS;
837 /* Put it back into the SCSI buffer list */
838 lpfc_release_scsi_buf_s4(phba, psb);
842 return bcnt + non_sequential_xri - rc;
846 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
847 * @vport: The virtual port for which this call being executed.
848 * @num_to_allocate: The requested number of buffers to allocate.
850 * This routine wraps the actual SCSI buffer allocator function pointer from
851 * the lpfc_hba struct.
854 * int - number of scsi buffers that were allocated.
855 * 0 = failure, less than num_to_alloc is a partial failure.
858 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
860 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
864 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
865 * @phba: The HBA for which this call is being executed.
867 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
868 * and returns to caller.
872 * Pointer to lpfc_scsi_buf - Success
874 static struct lpfc_scsi_buf*
875 lpfc_get_scsi_buf(struct lpfc_hba * phba)
877 struct lpfc_scsi_buf * lpfc_cmd = NULL;
878 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
879 unsigned long iflag = 0;
881 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
882 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
884 lpfc_cmd->seg_cnt = 0;
885 lpfc_cmd->nonsg_phys = 0;
886 lpfc_cmd->prot_seg_cnt = 0;
888 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
893 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
894 * @phba: The Hba for which this call is being executed.
895 * @psb: The scsi buffer which is being released.
897 * This routine releases @psb scsi buffer by adding it to tail of @phba
898 * lpfc_scsi_buf_list list.
901 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
903 unsigned long iflag = 0;
905 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
907 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
908 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
912 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
913 * @phba: The Hba for which this call is being executed.
914 * @psb: The scsi buffer which is being released.
916 * This routine releases @psb scsi buffer by adding it to tail of @phba
917 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
918 * and cannot be reused for at least RA_TOV amount of time if it was
922 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
924 unsigned long iflag = 0;
926 if (psb->status == IOSTAT_LOCAL_REJECT
927 && psb->result == IOERR_ABORT_REQUESTED) {
928 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
931 list_add_tail(&psb->list,
932 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
933 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
937 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
939 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
940 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
945 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
946 * @phba: The Hba for which this call is being executed.
947 * @psb: The scsi buffer which is being released.
949 * This routine releases @psb scsi buffer by adding it to tail of @phba
950 * lpfc_scsi_buf_list list.
953 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
956 phba->lpfc_release_scsi_buf(phba, psb);
960 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
961 * @phba: The Hba for which this call is being executed.
962 * @lpfc_cmd: The scsi buffer which is going to be mapped.
964 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
965 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
966 * through sg elements and format the bdea. This routine also initializes all
967 * IOCB fields which are dependent on scsi command request buffer.
974 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
976 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
977 struct scatterlist *sgel = NULL;
978 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
979 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
980 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
981 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
983 uint32_t num_bde = 0;
984 int nseg, datadir = scsi_cmnd->sc_data_direction;
987 * There are three possibilities here - use scatter-gather segment, use
988 * the single mapping, or neither. Start the lpfc command prep by
989 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
993 if (scsi_sg_count(scsi_cmnd)) {
995 * The driver stores the segment count returned from pci_map_sg
996 * because this a count of dma-mappings used to map the use_sg
997 * pages. They are not guaranteed to be the same for those
998 * architectures that implement an IOMMU.
1001 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1002 scsi_sg_count(scsi_cmnd), datadir);
1003 if (unlikely(!nseg))
1006 lpfc_cmd->seg_cnt = nseg;
1007 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1008 printk(KERN_ERR "%s: Too many sg segments from "
1009 "dma_map_sg. Config %d, seg_cnt %d\n",
1010 __func__, phba->cfg_sg_seg_cnt,
1012 scsi_dma_unmap(scsi_cmnd);
1017 * The driver established a maximum scatter-gather segment count
1018 * during probe that limits the number of sg elements in any
1019 * single scsi command. Just run through the seg_cnt and format
1021 * When using SLI-3 the driver will try to fit all the BDEs into
1022 * the IOCB. If it can't then the BDEs get added to a BPL as it
1023 * does for SLI-2 mode.
1025 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1026 physaddr = sg_dma_address(sgel);
1027 if (phba->sli_rev == 3 &&
1028 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1029 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1030 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1031 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1032 data_bde->addrLow = putPaddrLow(physaddr);
1033 data_bde->addrHigh = putPaddrHigh(physaddr);
1036 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1037 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1038 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1040 le32_to_cpu(putPaddrLow(physaddr));
1042 le32_to_cpu(putPaddrHigh(physaddr));
1049 * Finish initializing those IOCB fields that are dependent on the
1050 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1051 * explicitly reinitialized and for SLI-3 the extended bde count is
1052 * explicitly reinitialized since all iocb memory resources are reused.
1054 if (phba->sli_rev == 3 &&
1055 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
1056 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1058 * The extended IOCB format can only fit 3 BDE or a BPL.
1059 * This I/O has more than 3 BDE so the 1st data bde will
1060 * be a BPL that is filled in here.
1062 physaddr = lpfc_cmd->dma_handle;
1063 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1064 data_bde->tus.f.bdeSize = (num_bde *
1065 sizeof(struct ulp_bde64));
1066 physaddr += (sizeof(struct fcp_cmnd) +
1067 sizeof(struct fcp_rsp) +
1068 (2 * sizeof(struct ulp_bde64)));
1069 data_bde->addrHigh = putPaddrHigh(physaddr);
1070 data_bde->addrLow = putPaddrLow(physaddr);
1071 /* ebde count includes the responce bde and data bpl */
1072 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1074 /* ebde count includes the responce bde and data bdes */
1075 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1078 iocb_cmd->un.fcpi64.bdl.bdeSize =
1079 ((num_bde + 2) * sizeof(struct ulp_bde64));
1081 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1084 * Due to difference in data length between DIF/non-DIF paths,
1085 * we need to set word 4 of IOCB here
1087 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1092 * Given a scsi cmnd, determine the BlockGuard profile to be used
1096 lpfc_sc_to_sli_prof(struct scsi_cmnd *sc)
1098 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1099 uint8_t ret_prof = LPFC_PROF_INVALID;
1101 if (guard_type == SHOST_DIX_GUARD_IP) {
1102 switch (scsi_get_prot_op(sc)) {
1103 case SCSI_PROT_READ_INSERT:
1104 case SCSI_PROT_WRITE_STRIP:
1105 ret_prof = LPFC_PROF_AST2;
1108 case SCSI_PROT_READ_STRIP:
1109 case SCSI_PROT_WRITE_INSERT:
1110 ret_prof = LPFC_PROF_A1;
1113 case SCSI_PROT_READ_CONVERT:
1114 case SCSI_PROT_WRITE_CONVERT:
1115 ret_prof = LPFC_PROF_AST1;
1118 case SCSI_PROT_READ_PASS:
1119 case SCSI_PROT_WRITE_PASS:
1120 case SCSI_PROT_NORMAL:
1122 printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
1123 scsi_get_prot_op(sc), guard_type);
1127 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1128 switch (scsi_get_prot_op(sc)) {
1129 case SCSI_PROT_READ_STRIP:
1130 case SCSI_PROT_WRITE_INSERT:
1131 ret_prof = LPFC_PROF_A1;
1134 case SCSI_PROT_READ_PASS:
1135 case SCSI_PROT_WRITE_PASS:
1136 ret_prof = LPFC_PROF_C1;
1139 case SCSI_PROT_READ_CONVERT:
1140 case SCSI_PROT_WRITE_CONVERT:
1141 case SCSI_PROT_READ_INSERT:
1142 case SCSI_PROT_WRITE_STRIP:
1143 case SCSI_PROT_NORMAL:
1145 printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
1146 scsi_get_prot_op(sc), guard_type);
1150 /* unsupported format */
1157 struct scsi_dif_tuple {
1158 __be16 guard_tag; /* Checksum */
1159 __be16 app_tag; /* Opaque storage */
1160 __be32 ref_tag; /* Target LBA or indirect LBA */
1163 static inline unsigned
1164 lpfc_cmd_blksize(struct scsi_cmnd *sc)
1166 return sc->device->sector_size;
1170 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1171 * @sc: in: SCSI command
1172 * @apptagmask: out: app tag mask
1173 * @apptagval: out: app tag value
1174 * @reftag: out: ref tag (reference tag)
1177 * Extract DIF paramters from the command if possible. Otherwise,
1178 * use default paratmers.
1182 lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1183 uint16_t *apptagval, uint32_t *reftag)
1185 struct scsi_dif_tuple *spt;
1186 unsigned char op = scsi_get_prot_op(sc);
1187 unsigned int protcnt = scsi_prot_sg_count(sc);
1190 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
1191 op == SCSI_PROT_WRITE_PASS ||
1192 op == SCSI_PROT_WRITE_CONVERT)) {
1195 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1196 scsi_prot_sglist(sc)[0].offset;
1199 *reftag = cpu_to_be32(spt->ref_tag);
1202 /* SBC defines ref tag to be lower 32bits of LBA */
1203 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1210 * This function sets up buffer list for protection groups of
1211 * type LPFC_PG_TYPE_NO_DIF
1213 * This is usually used when the HBA is instructed to generate
1214 * DIFs and insert them into data stream (or strip DIF from
1215 * incoming data stream)
1217 * The buffer list consists of just one protection group described
1219 * +-------------------------+
1220 * start of prot group --> | PDE_1 |
1221 * +-------------------------+
1223 * +-------------------------+
1224 * |more Data BDE's ... (opt)|
1225 * +-------------------------+
1227 * @sc: pointer to scsi command we're working on
1228 * @bpl: pointer to buffer list for protection groups
1229 * @datacnt: number of segments of data that have been dma mapped
1231 * Note: Data s/g buffers have been dma mapped
1234 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1235 struct ulp_bde64 *bpl, int datasegcnt)
1237 struct scatterlist *sgde = NULL; /* s/g data entry */
1238 struct lpfc_pde *pde1 = NULL;
1239 dma_addr_t physaddr;
1240 int i = 0, num_bde = 0;
1241 int datadir = sc->sc_data_direction;
1242 int prof = LPFC_PROF_INVALID;
1245 uint16_t apptagmask, apptagval;
1247 pde1 = (struct lpfc_pde *) bpl;
1248 prof = lpfc_sc_to_sli_prof(sc);
1250 if (prof == LPFC_PROF_INVALID)
1253 /* extract some info from the scsi command for PDE1*/
1254 blksize = lpfc_cmd_blksize(sc);
1255 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1257 /* setup PDE1 with what we have */
1258 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1260 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1265 /* assumption: caller has already run dma_map_sg on command data */
1266 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1267 physaddr = sg_dma_address(sgde);
1268 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1269 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1270 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1271 if (datadir == DMA_TO_DEVICE)
1272 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1274 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1275 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1285 * This function sets up buffer list for protection groups of
1286 * type LPFC_PG_TYPE_DIF_BUF
1288 * This is usually used when DIFs are in their own buffers,
1289 * separate from the data. The HBA can then by instructed
1290 * to place the DIFs in the outgoing stream. For read operations,
1291 * The HBA could extract the DIFs and place it in DIF buffers.
1293 * The buffer list for this type consists of one or more of the
1294 * protection groups described below:
1295 * +-------------------------+
1296 * start of first prot group --> | PDE_1 |
1297 * +-------------------------+
1298 * | PDE_3 (Prot BDE) |
1299 * +-------------------------+
1301 * +-------------------------+
1302 * |more Data BDE's ... (opt)|
1303 * +-------------------------+
1304 * start of new prot group --> | PDE_1 |
1305 * +-------------------------+
1307 * +-------------------------+
1309 * @sc: pointer to scsi command we're working on
1310 * @bpl: pointer to buffer list for protection groups
1311 * @datacnt: number of segments of data that have been dma mapped
1312 * @protcnt: number of segment of protection data that have been dma mapped
1314 * Note: It is assumed that both data and protection s/g buffers have been
1318 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1319 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1321 struct scatterlist *sgde = NULL; /* s/g data entry */
1322 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1323 struct lpfc_pde *pde1 = NULL;
1324 struct ulp_bde64 *prot_bde = NULL;
1325 dma_addr_t dataphysaddr, protphysaddr;
1326 unsigned short curr_data = 0, curr_prot = 0;
1327 unsigned int split_offset, protgroup_len;
1328 unsigned int protgrp_blks, protgrp_bytes;
1329 unsigned int remainder, subtotal;
1330 int prof = LPFC_PROF_INVALID;
1331 int datadir = sc->sc_data_direction;
1332 unsigned char pgdone = 0, alldone = 0;
1335 uint16_t apptagmask, apptagval;
1338 sgpe = scsi_prot_sglist(sc);
1339 sgde = scsi_sglist(sc);
1341 if (!sgpe || !sgde) {
1342 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1343 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1348 prof = lpfc_sc_to_sli_prof(sc);
1349 if (prof == LPFC_PROF_INVALID)
1352 /* extract some info from the scsi command for PDE1*/
1353 blksize = lpfc_cmd_blksize(sc);
1354 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1358 /* setup the first PDE_1 */
1359 pde1 = (struct lpfc_pde *) bpl;
1361 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1363 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1368 /* setup the first BDE that points to protection buffer */
1369 prot_bde = (struct ulp_bde64 *) bpl;
1370 protphysaddr = sg_dma_address(sgpe);
1371 prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1372 prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1373 protgroup_len = sg_dma_len(sgpe);
1376 /* must be integer multiple of the DIF block length */
1377 BUG_ON(protgroup_len % 8);
1379 protgrp_blks = protgroup_len / 8;
1380 protgrp_bytes = protgrp_blks * blksize;
1382 prot_bde->tus.f.bdeSize = protgroup_len;
1383 if (datadir == DMA_TO_DEVICE)
1384 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1386 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1387 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1392 /* setup BDE's for data blocks associated with DIF data */
1394 subtotal = 0; /* total bytes processed for current prot grp */
1397 printk(KERN_ERR "%s Invalid data segment\n",
1402 dataphysaddr = sg_dma_address(sgde) + split_offset;
1403 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1404 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1406 remainder = sg_dma_len(sgde) - split_offset;
1408 if ((subtotal + remainder) <= protgrp_bytes) {
1409 /* we can use this whole buffer */
1410 bpl->tus.f.bdeSize = remainder;
1413 if ((subtotal + remainder) == protgrp_bytes)
1416 /* must split this buffer with next prot grp */
1417 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1418 split_offset += bpl->tus.f.bdeSize;
1421 subtotal += bpl->tus.f.bdeSize;
1423 if (datadir == DMA_TO_DEVICE)
1424 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1426 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1427 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1435 /* Move to the next s/g segment if possible */
1436 sgde = sg_next(sgde);
1440 if (curr_prot == protcnt) {
1442 } else if (curr_prot < protcnt) {
1443 /* advance to next prot buffer */
1444 sgpe = sg_next(sgpe);
1447 /* update the reference tag */
1448 reftag += protgrp_blks;
1450 /* if we're here, we have a bug */
1451 printk(KERN_ERR "BLKGRD: bug in %s\n", __func__);
1462 * Given a SCSI command that supports DIF, determine composition of protection
1463 * groups involved in setting up buffer lists
1466 * for DIF (for both read and write)
1469 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1471 int ret = LPFC_PG_TYPE_INVALID;
1472 unsigned char op = scsi_get_prot_op(sc);
1475 case SCSI_PROT_READ_STRIP:
1476 case SCSI_PROT_WRITE_INSERT:
1477 ret = LPFC_PG_TYPE_NO_DIF;
1479 case SCSI_PROT_READ_INSERT:
1480 case SCSI_PROT_WRITE_STRIP:
1481 case SCSI_PROT_READ_PASS:
1482 case SCSI_PROT_WRITE_PASS:
1483 case SCSI_PROT_WRITE_CONVERT:
1484 case SCSI_PROT_READ_CONVERT:
1485 ret = LPFC_PG_TYPE_DIF_BUF;
1488 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1489 "9021 Unsupported protection op:%d\n", op);
1497 * This is the protection/DIF aware version of
1498 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1499 * two functions eventually, but for now, it's here
1502 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1503 struct lpfc_scsi_buf *lpfc_cmd)
1505 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1506 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1507 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1508 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1509 uint32_t num_bde = 0;
1510 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1511 int prot_group_type = 0;
1516 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1517 * fcp_rsp regions to the first data bde entry
1520 if (scsi_sg_count(scsi_cmnd)) {
1522 * The driver stores the segment count returned from pci_map_sg
1523 * because this a count of dma-mappings used to map the use_sg
1524 * pages. They are not guaranteed to be the same for those
1525 * architectures that implement an IOMMU.
1527 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1528 scsi_sglist(scsi_cmnd),
1529 scsi_sg_count(scsi_cmnd), datadir);
1530 if (unlikely(!datasegcnt))
1533 lpfc_cmd->seg_cnt = datasegcnt;
1534 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1535 printk(KERN_ERR "%s: Too many sg segments from "
1536 "dma_map_sg. Config %d, seg_cnt %d\n",
1537 __func__, phba->cfg_sg_seg_cnt,
1539 scsi_dma_unmap(scsi_cmnd);
1543 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1545 switch (prot_group_type) {
1546 case LPFC_PG_TYPE_NO_DIF:
1547 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1549 /* we shoud have 2 or more entries in buffer list */
1553 case LPFC_PG_TYPE_DIF_BUF:{
1555 * This type indicates that protection buffers are
1556 * passed to the driver, so that needs to be prepared
1559 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1560 scsi_prot_sglist(scsi_cmnd),
1561 scsi_prot_sg_count(scsi_cmnd), datadir);
1562 if (unlikely(!protsegcnt)) {
1563 scsi_dma_unmap(scsi_cmnd);
1567 lpfc_cmd->prot_seg_cnt = protsegcnt;
1568 if (lpfc_cmd->prot_seg_cnt
1569 > phba->cfg_prot_sg_seg_cnt) {
1570 printk(KERN_ERR "%s: Too many prot sg segments "
1571 "from dma_map_sg. Config %d,"
1572 "prot_seg_cnt %d\n", __func__,
1573 phba->cfg_prot_sg_seg_cnt,
1574 lpfc_cmd->prot_seg_cnt);
1575 dma_unmap_sg(&phba->pcidev->dev,
1576 scsi_prot_sglist(scsi_cmnd),
1577 scsi_prot_sg_count(scsi_cmnd),
1579 scsi_dma_unmap(scsi_cmnd);
1583 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1584 datasegcnt, protsegcnt);
1585 /* we shoud have 3 or more entries in buffer list */
1590 case LPFC_PG_TYPE_INVALID:
1592 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1593 "9022 Unexpected protection group %i\n",
1600 * Finish initializing those IOCB fields that are dependent on the
1601 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1602 * reinitialized since all iocb memory resources are used many times
1603 * for transmit, receive, and continuation bpl's.
1605 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1606 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1607 iocb_cmd->ulpBdeCount = 1;
1608 iocb_cmd->ulpLe = 1;
1610 fcpdl = scsi_bufflen(scsi_cmnd);
1612 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1614 * We are in DIF Type 1 mode
1615 * Every data block has a 8 byte DIF (trailer)
1616 * attached to it. Must ajust FCP data length
1618 blksize = lpfc_cmd_blksize(scsi_cmnd);
1619 diflen = (fcpdl / blksize) * 8;
1622 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1625 * Due to difference in data length between DIF/non-DIF paths,
1626 * we need to set word 4 of IOCB here
1628 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1632 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1633 "9023 Could not setup all needed BDE's"
1634 "prot_group_type=%d, num_bde=%d\n",
1635 prot_group_type, num_bde);
1640 * This function checks for BlockGuard errors detected by
1641 * the HBA. In case of errors, the ASC/ASCQ fields in the
1642 * sense buffer will be set accordingly, paired with
1643 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1644 * detected corruption.
1647 * 0 - No error found
1648 * 1 - BlockGuard error found
1649 * -1 - Internal error (bad profile, ...etc)
1652 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1653 struct lpfc_iocbq *pIocbOut)
1655 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1656 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1658 uint32_t bghm = bgf->bghm;
1659 uint32_t bgstat = bgf->bgstat;
1660 uint64_t failing_sector = 0;
1662 printk(KERN_ERR "BG ERROR in cmd 0x%x lba 0x%llx blk cnt 0x%x "
1663 "bgstat=0x%x bghm=0x%x\n",
1664 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
1665 blk_rq_sectors(cmd->request), bgstat, bghm);
1667 spin_lock(&_dump_buf_lock);
1668 if (!_dump_buf_done) {
1669 printk(KERN_ERR "Saving Data for %u blocks to debugfs\n",
1670 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1671 lpfc_debug_save_data(cmd);
1673 /* If we have a prot sgl, save the DIF buffer */
1674 if (lpfc_prot_group_type(phba, cmd) ==
1675 LPFC_PG_TYPE_DIF_BUF) {
1676 printk(KERN_ERR "Saving DIF for %u blocks to debugfs\n",
1677 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1678 lpfc_debug_save_dif(cmd);
1683 spin_unlock(&_dump_buf_lock);
1685 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1686 cmd->result = ScsiResult(DID_ERROR, 0);
1687 printk(KERN_ERR "Invalid BlockGuard profile. bgstat:0x%x\n",
1693 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1694 cmd->result = ScsiResult(DID_ERROR, 0);
1695 printk(KERN_ERR "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
1701 if (lpfc_bgs_get_guard_err(bgstat)) {
1704 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1706 cmd->result = DRIVER_SENSE << 24
1707 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1708 phba->bg_guard_err_cnt++;
1709 printk(KERN_ERR "BLKGRD: guard_tag error\n");
1712 if (lpfc_bgs_get_reftag_err(bgstat)) {
1715 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1717 cmd->result = DRIVER_SENSE << 24
1718 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1720 phba->bg_reftag_err_cnt++;
1721 printk(KERN_ERR "BLKGRD: ref_tag error\n");
1724 if (lpfc_bgs_get_apptag_err(bgstat)) {
1727 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1729 cmd->result = DRIVER_SENSE << 24
1730 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1732 phba->bg_apptag_err_cnt++;
1733 printk(KERN_ERR "BLKGRD: app_tag error\n");
1736 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1738 * setup sense data descriptor 0 per SPC-4 as an information
1739 * field, and put the failing LBA in it
1741 cmd->sense_buffer[8] = 0; /* Information */
1742 cmd->sense_buffer[9] = 0xa; /* Add. length */
1743 bghm /= cmd->device->sector_size;
1745 failing_sector = scsi_get_lba(cmd);
1746 failing_sector += bghm;
1748 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1752 /* No error was reported - problem in FW? */
1753 cmd->result = ScsiResult(DID_ERROR, 0);
1754 printk(KERN_ERR "BLKGRD: no errors reported!\n");
1762 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1763 * @phba: The Hba for which this call is being executed.
1764 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1766 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1767 * field of @lpfc_cmd for device with SLI-4 interface spec.
1774 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1776 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1777 struct scatterlist *sgel = NULL;
1778 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1779 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1780 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1781 dma_addr_t physaddr;
1782 uint32_t num_bde = 0;
1784 uint32_t dma_offset = 0;
1788 * There are three possibilities here - use scatter-gather segment, use
1789 * the single mapping, or neither. Start the lpfc command prep by
1790 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1793 if (scsi_sg_count(scsi_cmnd)) {
1795 * The driver stores the segment count returned from pci_map_sg
1796 * because this a count of dma-mappings used to map the use_sg
1797 * pages. They are not guaranteed to be the same for those
1798 * architectures that implement an IOMMU.
1801 nseg = scsi_dma_map(scsi_cmnd);
1802 if (unlikely(!nseg))
1805 /* clear the last flag in the fcp_rsp map entry */
1806 sgl->word2 = le32_to_cpu(sgl->word2);
1807 bf_set(lpfc_sli4_sge_last, sgl, 0);
1808 sgl->word2 = cpu_to_le32(sgl->word2);
1811 lpfc_cmd->seg_cnt = nseg;
1812 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1813 printk(KERN_ERR "%s: Too many sg segments from "
1814 "dma_map_sg. Config %d, seg_cnt %d\n",
1815 __func__, phba->cfg_sg_seg_cnt,
1817 scsi_dma_unmap(scsi_cmnd);
1822 * The driver established a maximum scatter-gather segment count
1823 * during probe that limits the number of sg elements in any
1824 * single scsi command. Just run through the seg_cnt and format
1826 * When using SLI-3 the driver will try to fit all the BDEs into
1827 * the IOCB. If it can't then the BDEs get added to a BPL as it
1828 * does for SLI-2 mode.
1830 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1831 physaddr = sg_dma_address(sgel);
1832 dma_len = sg_dma_len(sgel);
1833 bf_set(lpfc_sli4_sge_len, sgl, sg_dma_len(sgel));
1834 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1835 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1836 if ((num_bde + 1) == nseg)
1837 bf_set(lpfc_sli4_sge_last, sgl, 1);
1839 bf_set(lpfc_sli4_sge_last, sgl, 0);
1840 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1841 sgl->word2 = cpu_to_le32(sgl->word2);
1842 sgl->word3 = cpu_to_le32(sgl->word3);
1843 dma_offset += dma_len;
1848 /* clear the last flag in the fcp_rsp map entry */
1849 sgl->word2 = le32_to_cpu(sgl->word2);
1850 bf_set(lpfc_sli4_sge_last, sgl, 1);
1851 sgl->word2 = cpu_to_le32(sgl->word2);
1855 * Finish initializing those IOCB fields that are dependent on the
1856 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1857 * explicitly reinitialized.
1858 * all iocb memory resources are reused.
1860 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1863 * Due to difference in data length between DIF/non-DIF paths,
1864 * we need to set word 4 of IOCB here
1866 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1871 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
1872 * @phba: The Hba for which this call is being executed.
1873 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1875 * This routine wraps the actual DMA mapping function pointer from the
1883 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1885 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1889 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
1890 * @phba: Pointer to hba context object.
1891 * @vport: Pointer to vport object.
1892 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
1893 * @rsp_iocb: Pointer to response iocb object which reported error.
1895 * This function posts an event when there is a SCSI command reporting
1896 * error from the scsi device.
1899 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
1900 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
1901 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1902 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
1903 uint32_t resp_info = fcprsp->rspStatus2;
1904 uint32_t scsi_status = fcprsp->rspStatus3;
1905 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
1906 struct lpfc_fast_path_event *fast_path_evt = NULL;
1907 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
1908 unsigned long flags;
1910 /* If there is queuefull or busy condition send a scsi event */
1911 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
1912 (cmnd->result == SAM_STAT_BUSY)) {
1913 fast_path_evt = lpfc_alloc_fast_evt(phba);
1916 fast_path_evt->un.scsi_evt.event_type =
1918 fast_path_evt->un.scsi_evt.subcategory =
1919 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
1920 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
1921 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
1922 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
1923 &pnode->nlp_portname, sizeof(struct lpfc_name));
1924 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
1925 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1926 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
1927 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
1928 fast_path_evt = lpfc_alloc_fast_evt(phba);
1931 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
1933 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
1934 LPFC_EVENT_CHECK_COND;
1935 fast_path_evt->un.check_cond_evt.scsi_event.lun =
1937 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
1938 &pnode->nlp_portname, sizeof(struct lpfc_name));
1939 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
1940 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1941 fast_path_evt->un.check_cond_evt.sense_key =
1942 cmnd->sense_buffer[2] & 0xf;
1943 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
1944 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
1945 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
1947 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
1948 ((scsi_status == SAM_STAT_GOOD) &&
1949 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
1951 * If status is good or resid does not match with fcp_param and
1952 * there is valid fcpi_parm, then there is a read_check error
1954 fast_path_evt = lpfc_alloc_fast_evt(phba);
1957 fast_path_evt->un.read_check_error.header.event_type =
1958 FC_REG_FABRIC_EVENT;
1959 fast_path_evt->un.read_check_error.header.subcategory =
1960 LPFC_EVENT_FCPRDCHKERR;
1961 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
1962 &pnode->nlp_portname, sizeof(struct lpfc_name));
1963 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
1964 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1965 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
1966 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
1967 fast_path_evt->un.read_check_error.fcpiparam =
1972 fast_path_evt->vport = vport;
1973 spin_lock_irqsave(&phba->hbalock, flags);
1974 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
1975 spin_unlock_irqrestore(&phba->hbalock, flags);
1976 lpfc_worker_wake_up(phba);
1981 * lpfc_scsi_unprep_dma_buf_s3 - Un-map DMA mapping of SG-list for SLI3 dev
1982 * @phba: The HBA for which this call is being executed.
1983 * @psb: The scsi buffer which is going to be un-mapped.
1985 * This routine does DMA un-mapping of scatter gather list of scsi command
1986 * field of @lpfc_cmd for device with SLI-3 interface spec.
1989 lpfc_scsi_unprep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1992 * There are only two special cases to consider. (1) the scsi command
1993 * requested scatter-gather usage or (2) the scsi command allocated
1994 * a request buffer, but did not request use_sg. There is a third
1995 * case, but it does not require resource deallocation.
1997 if (psb->seg_cnt > 0)
1998 scsi_dma_unmap(psb->pCmd);
1999 if (psb->prot_seg_cnt > 0)
2000 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2001 scsi_prot_sg_count(psb->pCmd),
2002 psb->pCmd->sc_data_direction);
2006 * lpfc_scsi_unprep_dma_buf_s4 - Un-map DMA mapping of SG-list for SLI4 dev
2007 * @phba: The Hba for which this call is being executed.
2008 * @psb: The scsi buffer which is going to be un-mapped.
2010 * This routine does DMA un-mapping of scatter gather list of scsi command
2011 * field of @lpfc_cmd for device with SLI-4 interface spec. If we have to
2012 * remove the sgl for this scsi buffer then we will do it here. For now
2013 * we should be able to just call the sli3 unprep routine.
2016 lpfc_scsi_unprep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
2018 lpfc_scsi_unprep_dma_buf_s3(phba, psb);
2022 * lpfc_scsi_unprep_dma_buf - Wrapper function for unmap DMA mapping of SG-list
2023 * @phba: The Hba for which this call is being executed.
2024 * @psb: The scsi buffer which is going to be un-mapped.
2026 * This routine does DMA un-mapping of scatter gather list of scsi command
2027 * field of @lpfc_cmd for device with SLI-4 interface spec.
2030 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
2032 phba->lpfc_scsi_unprep_dma_buf(phba, psb);
2036 * lpfc_handler_fcp_err - FCP response handler
2037 * @vport: The virtual port for which this call is being executed.
2038 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2039 * @rsp_iocb: The response IOCB which contains FCP error.
2041 * This routine is called to process response IOCB with status field
2042 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2043 * based upon SCSI and FCP error.
2046 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2047 struct lpfc_iocbq *rsp_iocb)
2049 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2050 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2051 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2052 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2053 uint32_t resp_info = fcprsp->rspStatus2;
2054 uint32_t scsi_status = fcprsp->rspStatus3;
2056 uint32_t host_status = DID_OK;
2057 uint32_t rsplen = 0;
2058 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
2062 * If this is a task management command, there is no
2063 * scsi packet associated with this lpfc_cmd. The driver
2066 if (fcpcmd->fcpCntl2) {
2071 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2072 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2073 if (snslen > SCSI_SENSE_BUFFERSIZE)
2074 snslen = SCSI_SENSE_BUFFERSIZE;
2076 if (resp_info & RSP_LEN_VALID)
2077 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2078 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2080 lp = (uint32_t *)cmnd->sense_buffer;
2082 if (!scsi_status && (resp_info & RESID_UNDER))
2085 lpfc_printf_vlog(vport, KERN_WARNING, logit,
2086 "9024 FCP command x%x failed: x%x SNS x%x x%x "
2087 "Data: x%x x%x x%x x%x x%x\n",
2088 cmnd->cmnd[0], scsi_status,
2089 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2090 be32_to_cpu(fcprsp->rspResId),
2091 be32_to_cpu(fcprsp->rspSnsLen),
2092 be32_to_cpu(fcprsp->rspRspLen),
2095 if (resp_info & RSP_LEN_VALID) {
2096 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2097 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
2098 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
2099 host_status = DID_ERROR;
2104 scsi_set_resid(cmnd, 0);
2105 if (resp_info & RESID_UNDER) {
2106 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
2108 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2109 "9025 FCP Read Underrun, expected %d, "
2110 "residual %d Data: x%x x%x x%x\n",
2111 be32_to_cpu(fcpcmd->fcpDl),
2112 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2116 * If there is an under run check if under run reported by
2117 * storage array is same as the under run reported by HBA.
2118 * If this is not same, there is a dropped frame.
2120 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2122 (scsi_get_resid(cmnd) != fcpi_parm)) {
2123 lpfc_printf_vlog(vport, KERN_WARNING,
2124 LOG_FCP | LOG_FCP_ERROR,
2125 "9026 FCP Read Check Error "
2126 "and Underrun Data: x%x x%x x%x x%x\n",
2127 be32_to_cpu(fcpcmd->fcpDl),
2128 scsi_get_resid(cmnd), fcpi_parm,
2130 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
2131 host_status = DID_ERROR;
2134 * The cmnd->underflow is the minimum number of bytes that must
2135 * be transfered for this command. Provided a sense condition
2136 * is not present, make sure the actual amount transferred is at
2137 * least the underflow value or fail.
2139 if (!(resp_info & SNS_LEN_VALID) &&
2140 (scsi_status == SAM_STAT_GOOD) &&
2141 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2142 < cmnd->underflow)) {
2143 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2144 "9027 FCP command x%x residual "
2145 "underrun converted to error "
2146 "Data: x%x x%x x%x\n",
2147 cmnd->cmnd[0], scsi_bufflen(cmnd),
2148 scsi_get_resid(cmnd), cmnd->underflow);
2149 host_status = DID_ERROR;
2151 } else if (resp_info & RESID_OVER) {
2152 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2153 "9028 FCP command x%x residual overrun error. "
2154 "Data: x%x x%x \n", cmnd->cmnd[0],
2155 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
2156 host_status = DID_ERROR;
2159 * Check SLI validation that all the transfer was actually done
2160 * (fcpi_parm should be zero). Apply check only to reads.
2162 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
2163 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
2164 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
2165 "9029 FCP Read Check Error Data: "
2166 "x%x x%x x%x x%x\n",
2167 be32_to_cpu(fcpcmd->fcpDl),
2168 be32_to_cpu(fcprsp->rspResId),
2169 fcpi_parm, cmnd->cmnd[0]);
2170 host_status = DID_ERROR;
2171 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
2175 cmnd->result = ScsiResult(host_status, scsi_status);
2176 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
2180 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
2181 * @phba: The Hba for which this call is being executed.
2182 * @pIocbIn: The command IOCBQ for the scsi cmnd.
2183 * @pIocbOut: The response IOCBQ for the scsi cmnd.
2185 * This routine assigns scsi command result by looking into response IOCB
2186 * status field appropriately. This routine handles QUEUE FULL condition as
2187 * well by ramping down device queue depth.
2190 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2191 struct lpfc_iocbq *pIocbOut)
2193 struct lpfc_scsi_buf *lpfc_cmd =
2194 (struct lpfc_scsi_buf *) pIocbIn->context1;
2195 struct lpfc_vport *vport = pIocbIn->vport;
2196 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2197 struct lpfc_nodelist *pnode = rdata->pnode;
2198 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2200 struct scsi_device *tmp_sdev;
2202 unsigned long flags;
2203 struct lpfc_fast_path_event *fast_path_evt;
2204 struct Scsi_Host *shost = cmd->device->host;
2205 uint32_t queue_depth, scsi_id;
2207 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2208 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
2209 if (pnode && NLP_CHK_NODE_ACT(pnode))
2210 atomic_dec(&pnode->cmd_pending);
2212 if (lpfc_cmd->status) {
2213 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2214 (lpfc_cmd->result & IOERR_DRVR_MASK))
2215 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2216 else if (lpfc_cmd->status >= IOSTAT_CNT)
2217 lpfc_cmd->status = IOSTAT_DEFAULT;
2219 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2220 "9030 FCP cmd x%x failed <%d/%d> "
2221 "status: x%x result: x%x Data: x%x x%x\n",
2223 cmd->device ? cmd->device->id : 0xffff,
2224 cmd->device ? cmd->device->lun : 0xffff,
2225 lpfc_cmd->status, lpfc_cmd->result,
2226 pIocbOut->iocb.ulpContext,
2227 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
2229 switch (lpfc_cmd->status) {
2230 case IOSTAT_FCP_RSP_ERROR:
2231 /* Call FCP RSP handler to determine result */
2232 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
2234 case IOSTAT_NPORT_BSY:
2235 case IOSTAT_FABRIC_BSY:
2236 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2237 fast_path_evt = lpfc_alloc_fast_evt(phba);
2240 fast_path_evt->un.fabric_evt.event_type =
2241 FC_REG_FABRIC_EVENT;
2242 fast_path_evt->un.fabric_evt.subcategory =
2243 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2244 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2245 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2246 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2247 &pnode->nlp_portname,
2248 sizeof(struct lpfc_name));
2249 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2250 &pnode->nlp_nodename,
2251 sizeof(struct lpfc_name));
2253 fast_path_evt->vport = vport;
2254 fast_path_evt->work_evt.evt =
2255 LPFC_EVT_FASTPATH_MGMT_EVT;
2256 spin_lock_irqsave(&phba->hbalock, flags);
2257 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2259 spin_unlock_irqrestore(&phba->hbalock, flags);
2260 lpfc_worker_wake_up(phba);
2262 case IOSTAT_LOCAL_REJECT:
2263 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
2264 lpfc_cmd->result == IOERR_NO_RESOURCES ||
2265 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
2266 cmd->result = ScsiResult(DID_REQUEUE, 0);
2270 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2271 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2272 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2273 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2275 * This is a response for a BG enabled
2276 * cmd. Parse BG error
2278 lpfc_parse_bg_err(phba, lpfc_cmd,
2282 lpfc_printf_vlog(vport, KERN_WARNING,
2284 "9031 non-zero BGSTAT "
2285 "on unprotected cmd");
2289 /* else: fall through */
2291 cmd->result = ScsiResult(DID_ERROR, 0);
2295 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
2296 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
2297 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2300 cmd->result = ScsiResult(DID_OK, 0);
2303 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2304 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2306 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2307 "0710 Iodone <%d/%d> cmd %p, error "
2308 "x%x SNS x%x x%x Data: x%x x%x\n",
2309 cmd->device->id, cmd->device->lun, cmd,
2310 cmd->result, *lp, *(lp + 3), cmd->retries,
2311 scsi_get_resid(cmd));
2314 lpfc_update_stats(phba, lpfc_cmd);
2315 result = cmd->result;
2316 if (vport->cfg_max_scsicmpl_time &&
2317 time_after(jiffies, lpfc_cmd->start_time +
2318 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
2319 spin_lock_irqsave(shost->host_lock, flags);
2320 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2321 if (pnode->cmd_qdepth >
2322 atomic_read(&pnode->cmd_pending) &&
2323 (atomic_read(&pnode->cmd_pending) >
2324 LPFC_MIN_TGT_QDEPTH) &&
2325 ((cmd->cmnd[0] == READ_10) ||
2326 (cmd->cmnd[0] == WRITE_10)))
2328 atomic_read(&pnode->cmd_pending);
2330 pnode->last_change_time = jiffies;
2332 spin_unlock_irqrestore(shost->host_lock, flags);
2333 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2334 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
2335 time_after(jiffies, pnode->last_change_time +
2336 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
2337 spin_lock_irqsave(shost->host_lock, flags);
2338 pnode->cmd_qdepth += pnode->cmd_qdepth *
2339 LPFC_TGTQ_RAMPUP_PCENT / 100;
2340 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
2341 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2342 pnode->last_change_time = jiffies;
2343 spin_unlock_irqrestore(shost->host_lock, flags);
2347 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
2349 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2350 queue_depth = cmd->device->queue_depth;
2351 scsi_id = cmd->device->id;
2352 cmd->scsi_done(cmd);
2354 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2356 * If there is a thread waiting for command completion
2357 * wake up the thread.
2359 spin_lock_irqsave(shost->host_lock, flags);
2360 lpfc_cmd->pCmd = NULL;
2361 if (lpfc_cmd->waitq)
2362 wake_up(lpfc_cmd->waitq);
2363 spin_unlock_irqrestore(shost->host_lock, flags);
2364 lpfc_release_scsi_buf(phba, lpfc_cmd);
2370 lpfc_rampup_queue_depth(vport, queue_depth);
2372 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
2373 ((jiffies - pnode->last_ramp_up_time) >
2374 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
2375 ((jiffies - pnode->last_q_full_time) >
2376 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
2377 (vport->cfg_lun_queue_depth > queue_depth)) {
2378 shost_for_each_device(tmp_sdev, shost) {
2379 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
2380 if (tmp_sdev->id != scsi_id)
2382 if (tmp_sdev->ordered_tags)
2383 scsi_adjust_queue_depth(tmp_sdev,
2385 tmp_sdev->queue_depth+1);
2387 scsi_adjust_queue_depth(tmp_sdev,
2389 tmp_sdev->queue_depth+1);
2391 pnode->last_ramp_up_time = jiffies;
2394 lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
2396 queue_depth , queue_depth + 1);
2400 * Check for queue full. If the lun is reporting queue full, then
2401 * back off the lun queue depth to prevent target overloads.
2403 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2404 NLP_CHK_NODE_ACT(pnode)) {
2405 pnode->last_q_full_time = jiffies;
2407 shost_for_each_device(tmp_sdev, shost) {
2408 if (tmp_sdev->id != scsi_id)
2410 depth = scsi_track_queue_full(tmp_sdev,
2411 tmp_sdev->queue_depth - 1);
2414 * The queue depth cannot be lowered any more.
2415 * Modify the returned error code to store
2416 * the final depth value set by
2417 * scsi_track_queue_full.
2420 depth = shost->cmd_per_lun;
2423 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2424 "0711 detected queue full - lun queue "
2425 "depth adjusted to %d.\n", depth);
2426 lpfc_send_sdev_queuedepth_change_event(phba, vport,
2433 * If there is a thread waiting for command completion
2434 * wake up the thread.
2436 spin_lock_irqsave(shost->host_lock, flags);
2437 lpfc_cmd->pCmd = NULL;
2438 if (lpfc_cmd->waitq)
2439 wake_up(lpfc_cmd->waitq);
2440 spin_unlock_irqrestore(shost->host_lock, flags);
2442 lpfc_release_scsi_buf(phba, lpfc_cmd);
2446 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
2447 * @data: A pointer to the immediate command data portion of the IOCB.
2448 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2450 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2451 * byte swapping the data to big endian format for transmission on the wire.
2454 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2457 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2458 i += sizeof(uint32_t), j++) {
2459 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2464 * lpfc_scsi_prep_cmnd_s3 - Convert scsi cmnd to FCP infor unit for SLI3 dev
2465 * @vport: The virtual port for which this call is being executed.
2466 * @lpfc_cmd: The scsi command which needs to send.
2467 * @pnode: Pointer to lpfc_nodelist.
2469 * This routine initializes fcp_cmnd and iocb data structure from scsi command
2470 * to transfer for device with SLI3 interface spec.
2473 lpfc_scsi_prep_cmnd_s3(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2474 struct lpfc_nodelist *pnode)
2476 struct lpfc_hba *phba = vport->phba;
2477 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2478 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2479 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2480 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2481 int datadir = scsi_cmnd->sc_data_direction;
2484 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2487 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
2488 /* clear task management bits */
2489 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
2491 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2492 &lpfc_cmd->fcp_cmnd->fcp_lun);
2494 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2496 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2498 case HEAD_OF_QUEUE_TAG:
2499 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2501 case ORDERED_QUEUE_TAG:
2502 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2505 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2509 fcp_cmnd->fcpCntl1 = 0;
2512 * There are three possibilities here - use scatter-gather segment, use
2513 * the single mapping, or neither. Start the lpfc command prep by
2514 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2517 if (scsi_sg_count(scsi_cmnd)) {
2518 if (datadir == DMA_TO_DEVICE) {
2519 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
2520 if (phba->sli_rev < LPFC_SLI_REV4) {
2521 iocb_cmd->un.fcpi.fcpi_parm = 0;
2522 iocb_cmd->ulpPU = 0;
2524 iocb_cmd->ulpPU = PARM_READ_CHECK;
2525 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2526 phba->fc4OutputRequests++;
2528 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2529 iocb_cmd->ulpPU = PARM_READ_CHECK;
2530 fcp_cmnd->fcpCntl3 = READ_DATA;
2531 phba->fc4InputRequests++;
2534 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2535 iocb_cmd->un.fcpi.fcpi_parm = 0;
2536 iocb_cmd->ulpPU = 0;
2537 fcp_cmnd->fcpCntl3 = 0;
2538 phba->fc4ControlRequests++;
2540 if (phba->sli_rev == 3 &&
2541 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
2542 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
2544 * Finish initializing those IOCB fields that are independent
2545 * of the scsi_cmnd request_buffer
2547 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2548 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2549 piocbq->iocb.ulpFCP2Rcvy = 1;
2551 piocbq->iocb.ulpFCP2Rcvy = 0;
2553 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2554 piocbq->context1 = lpfc_cmd;
2555 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2556 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2557 piocbq->vport = vport;
2561 * lpfc_scsi_prep_cmnd_s4 - Convert scsi cmnd to FCP infor unit for SLI4 dev
2562 * @vport: The virtual port for which this call is being executed.
2563 * @lpfc_cmd: The scsi command which needs to send.
2564 * @pnode: Pointer to lpfc_nodelist.
2566 * This routine initializes fcp_cmnd and iocb data structure from scsi command
2567 * to transfer for device with SLI4 interface spec.
2570 lpfc_scsi_prep_cmnd_s4(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2571 struct lpfc_nodelist *pnode)
2574 * The prep cmnd routines do not touch the sgl or its
2575 * entries. We may not have to do anything different.
2576 * I will leave this function in place until we can
2577 * run some IO through the driver and determine if changes
2580 return lpfc_scsi_prep_cmnd_s3(vport, lpfc_cmd, pnode);
2584 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
2585 * @vport: The virtual port for which this call is being executed.
2586 * @lpfc_cmd: The scsi command which needs to send.
2587 * @pnode: Pointer to lpfc_nodelist.
2589 * This routine wraps the actual convert SCSI cmnd function pointer from
2590 * the lpfc_hba struct.
2593 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2594 struct lpfc_nodelist *pnode)
2596 vport->phba->lpfc_scsi_prep_cmnd(vport, lpfc_cmd, pnode);
2600 * lpfc_scsi_prep_task_mgmt_cmnd_s3 - Convert SLI3 scsi TM cmd to FCP info unit
2601 * @vport: The virtual port for which this call is being executed.
2602 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2603 * @lun: Logical unit number.
2604 * @task_mgmt_cmd: SCSI task management command.
2606 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2607 * for device with SLI-3 interface spec.
2614 lpfc_scsi_prep_task_mgmt_cmd_s3(struct lpfc_vport *vport,
2615 struct lpfc_scsi_buf *lpfc_cmd,
2617 uint8_t task_mgmt_cmd)
2619 struct lpfc_iocbq *piocbq;
2621 struct fcp_cmnd *fcp_cmnd;
2622 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2623 struct lpfc_nodelist *ndlp = rdata->pnode;
2625 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2626 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
2629 piocbq = &(lpfc_cmd->cur_iocbq);
2630 piocbq->vport = vport;
2632 piocb = &piocbq->iocb;
2634 fcp_cmnd = lpfc_cmd->fcp_cmnd;
2635 /* Clear out any old data in the FCP command area */
2636 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2637 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
2638 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
2639 if (vport->phba->sli_rev == 3 &&
2640 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
2641 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
2642 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
2643 piocb->ulpContext = ndlp->nlp_rpi;
2644 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2645 piocb->ulpFCP2Rcvy = 1;
2647 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2649 /* ulpTimeout is only one byte */
2650 if (lpfc_cmd->timeout > 0xff) {
2652 * Do not timeout the command at the firmware level.
2653 * The driver will provide the timeout mechanism.
2655 piocb->ulpTimeout = 0;
2657 piocb->ulpTimeout = lpfc_cmd->timeout;
2664 * lpfc_scsi_prep_task_mgmt_cmnd_s4 - Convert SLI4 scsi TM cmd to FCP info unit
2665 * @vport: The virtual port for which this call is being executed.
2666 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2667 * @lun: Logical unit number.
2668 * @task_mgmt_cmd: SCSI task management command.
2670 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2671 * for device with SLI-4 interface spec.
2678 lpfc_scsi_prep_task_mgmt_cmd_s4(struct lpfc_vport *vport,
2679 struct lpfc_scsi_buf *lpfc_cmd,
2681 uint8_t task_mgmt_cmd)
2684 * The prep cmnd routines do not touch the sgl or its
2685 * entries. We may not have to do anything different.
2686 * I will leave this function in place until we can
2687 * run some IO through the driver and determine if changes
2690 return lpfc_scsi_prep_task_mgmt_cmd_s3(vport, lpfc_cmd, lun,
2695 * lpfc_scsi_prep_task_mgmt_cmnd - Wrapper func convert scsi TM cmd to FCP info
2696 * @vport: The virtual port for which this call is being executed.
2697 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2698 * @lun: Logical unit number.
2699 * @task_mgmt_cmd: SCSI task management command.
2701 * This routine wraps the actual convert SCSI TM to FCP information unit
2702 * function pointer from the lpfc_hba struct.
2709 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
2710 struct lpfc_scsi_buf *lpfc_cmd,
2712 uint8_t task_mgmt_cmd)
2714 struct lpfc_hba *phba = vport->phba;
2716 return phba->lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
2721 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2722 * @phba: The hba struct for which this call is being executed.
2723 * @dev_grp: The HBA PCI-Device group number.
2725 * This routine sets up the SCSI interface API function jump table in @phba
2727 * Returns: 0 - success, -ENODEV - failure.
2730 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2734 case LPFC_PCI_DEV_LP:
2735 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2736 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
2737 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd_s3;
2738 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf_s3;
2739 phba->lpfc_scsi_prep_task_mgmt_cmd =
2740 lpfc_scsi_prep_task_mgmt_cmd_s3;
2741 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2743 case LPFC_PCI_DEV_OC:
2744 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2745 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
2746 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd_s4;
2747 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf_s4;
2748 phba->lpfc_scsi_prep_task_mgmt_cmd =
2749 lpfc_scsi_prep_task_mgmt_cmd_s4;
2750 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2753 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2754 "1418 Invalid HBA PCI-device group: 0x%x\n",
2759 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2760 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
2765 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
2766 * @phba: The Hba for which this call is being executed.
2767 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2768 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2770 * This routine is IOCB completion routine for device reset and target reset
2771 * routine. This routine release scsi buffer associated with lpfc_cmd.
2774 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2775 struct lpfc_iocbq *cmdiocbq,
2776 struct lpfc_iocbq *rspiocbq)
2778 struct lpfc_scsi_buf *lpfc_cmd =
2779 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2781 lpfc_release_scsi_buf(phba, lpfc_cmd);
2786 * lpfc_scsi_tgt_reset - Target reset handler
2787 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure
2788 * @vport: The virtual port for which this call is being executed.
2789 * @tgt_id: Target ID.
2791 * @rdata: Pointer to lpfc_rport_data.
2793 * This routine issues a TARGET RESET iocb to reset a target with @tgt_id ID.
2800 lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
2801 unsigned tgt_id, unsigned int lun,
2802 struct lpfc_rport_data *rdata)
2804 struct lpfc_hba *phba = vport->phba;
2805 struct lpfc_iocbq *iocbq;
2806 struct lpfc_iocbq *iocbqrsp;
2810 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
2813 lpfc_cmd->rdata = rdata;
2814 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
2819 iocbq = &lpfc_cmd->cur_iocbq;
2820 iocbqrsp = lpfc_sli_get_iocbq(phba);
2825 /* Issue Target Reset to TGT <num> */
2826 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2827 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
2828 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
2829 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
2830 iocbq, iocbqrsp, lpfc_cmd->timeout);
2831 if (status != IOCB_SUCCESS) {
2832 if (status == IOCB_TIMEDOUT) {
2833 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
2834 ret = TIMEOUT_ERROR;
2837 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2840 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
2841 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
2842 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2843 (lpfc_cmd->result & IOERR_DRVR_MASK))
2844 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2847 lpfc_sli_release_iocbq(phba, iocbqrsp);
2852 * lpfc_info - Info entry point of scsi_host_template data structure
2853 * @host: The scsi host for which this call is being executed.
2855 * This routine provides module information about hba.
2858 * Pointer to char - Success.
2861 lpfc_info(struct Scsi_Host *host)
2863 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2864 struct lpfc_hba *phba = vport->phba;
2866 static char lpfcinfobuf[384];
2868 memset(lpfcinfobuf,0,384);
2869 if (phba && phba->pcidev){
2870 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2871 len = strlen(lpfcinfobuf);
2872 snprintf(lpfcinfobuf + len,
2874 " on PCI bus %02x device %02x irq %d",
2875 phba->pcidev->bus->number,
2876 phba->pcidev->devfn,
2878 len = strlen(lpfcinfobuf);
2879 if (phba->Port[0]) {
2880 snprintf(lpfcinfobuf + len,
2890 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
2891 * @phba: The Hba for which this call is being executed.
2893 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2894 * The default value of cfg_poll_tmo is 10 milliseconds.
2896 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2898 unsigned long poll_tmo_expires =
2899 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2901 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2902 mod_timer(&phba->fcp_poll_timer,
2907 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
2908 * @phba: The Hba for which this call is being executed.
2910 * This routine starts the fcp_poll_timer of @phba.
2912 void lpfc_poll_start_timer(struct lpfc_hba * phba)
2914 lpfc_poll_rearm_timer(phba);
2918 * lpfc_poll_timeout - Restart polling timer
2919 * @ptr: Map to lpfc_hba data structure pointer.
2921 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2922 * and FCP Ring interrupt is disable.
2925 void lpfc_poll_timeout(unsigned long ptr)
2927 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
2929 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2930 lpfc_sli_poll_fcp_ring (phba);
2931 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2932 lpfc_poll_rearm_timer(phba);
2937 * lpfc_queuecommand - scsi_host_template queuecommand entry point
2938 * @cmnd: Pointer to scsi_cmnd data structure.
2939 * @done: Pointer to done routine.
2941 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2942 * This routine prepares an IOCB from scsi command and provides to firmware.
2943 * The @done callback is invoked after driver finished processing the command.
2947 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2950 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2952 struct Scsi_Host *shost = cmnd->device->host;
2953 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2954 struct lpfc_hba *phba = vport->phba;
2955 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
2956 struct lpfc_nodelist *ndlp = rdata->pnode;
2957 struct lpfc_scsi_buf *lpfc_cmd;
2958 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
2961 err = fc_remote_port_chkready(rport);
2964 goto out_fail_command;
2967 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2968 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2970 printk(KERN_ERR "BLKGRD ERROR: rcvd protected cmd:%02x op:%02x "
2971 "str=%s without registering for BlockGuard - "
2972 "Rejecting command\n",
2973 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2974 dif_op_str[scsi_get_prot_op(cmnd)]);
2975 goto out_fail_command;
2979 * Catch race where our node has transitioned, but the
2980 * transport is still transitioning.
2982 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2983 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2984 goto out_fail_command;
2986 if (vport->cfg_max_scsicmpl_time &&
2987 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
2990 lpfc_cmd = lpfc_get_scsi_buf(phba);
2991 if (lpfc_cmd == NULL) {
2992 lpfc_rampdown_queue_depth(phba);
2994 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2995 "0707 driver's buffer pool is empty, "
3001 * Store the midlayer's command structure for the completion phase
3002 * and complete the command initialization.
3004 lpfc_cmd->pCmd = cmnd;
3005 lpfc_cmd->rdata = rdata;
3006 lpfc_cmd->timeout = 0;
3007 lpfc_cmd->start_time = jiffies;
3008 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
3009 cmnd->scsi_done = done;
3011 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
3012 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3013 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
3015 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3016 dif_op_str[scsi_get_prot_op(cmnd)]);
3017 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3018 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
3019 "%02x %02x %02x %02x %02x \n",
3020 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
3021 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
3022 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
3024 if (cmnd->cmnd[0] == READ_10)
3025 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3026 "9035 BLKGRD: READ @ sector %llu, "
3028 (unsigned long long)scsi_get_lba(cmnd),
3029 blk_rq_sectors(cmnd->request));
3030 else if (cmnd->cmnd[0] == WRITE_10)
3031 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3032 "9036 BLKGRD: WRITE @ sector %llu, "
3033 "count %u cmd=%p\n",
3034 (unsigned long long)scsi_get_lba(cmnd),
3035 blk_rq_sectors(cmnd->request),
3038 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3040 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3041 "9038 BLKGRD: rcvd unprotected cmd:%02x op:%02x"
3043 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3044 dif_op_str[scsi_get_prot_op(cmnd)]);
3045 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3046 "9039 BLKGRD: CDB: %02x %02x %02x %02x %02x "
3047 "%02x %02x %02x %02x %02x \n",
3048 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
3049 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
3050 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
3052 if (cmnd->cmnd[0] == READ_10)
3053 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3054 "9040 dbg: READ @ sector %llu, "
3056 (unsigned long long)scsi_get_lba(cmnd),
3057 blk_rq_sectors(cmnd->request));
3058 else if (cmnd->cmnd[0] == WRITE_10)
3059 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3060 "9041 dbg: WRITE @ sector %llu, "
3061 "count %u cmd=%p\n",
3062 (unsigned long long)scsi_get_lba(cmnd),
3063 blk_rq_sectors(cmnd->request), cmnd);
3065 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3066 "9042 dbg: parser not implemented\n");
3067 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3071 goto out_host_busy_free_buf;
3073 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
3075 atomic_inc(&ndlp->cmd_pending);
3076 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
3077 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
3079 atomic_dec(&ndlp->cmd_pending);
3080 goto out_host_busy_free_buf;
3082 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3083 lpfc_sli_poll_fcp_ring(phba);
3084 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3085 lpfc_poll_rearm_timer(phba);
3090 out_host_busy_free_buf:
3091 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
3092 lpfc_release_scsi_buf(phba, lpfc_cmd);
3094 return SCSI_MLQUEUE_HOST_BUSY;
3102 * lpfc_block_error_handler - Routine to block error handler
3103 * @cmnd: Pointer to scsi_cmnd data structure.
3105 * This routine blocks execution till fc_rport state is not FC_PORSTAT_BLCOEKD.
3108 lpfc_block_error_handler(struct scsi_cmnd *cmnd)
3110 struct Scsi_Host *shost = cmnd->device->host;
3111 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3113 spin_lock_irq(shost->host_lock);
3114 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
3115 spin_unlock_irq(shost->host_lock);
3117 spin_lock_irq(shost->host_lock);
3119 spin_unlock_irq(shost->host_lock);
3124 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
3125 * @cmnd: Pointer to scsi_cmnd data structure.
3127 * This routine aborts @cmnd pending in base driver.
3134 lpfc_abort_handler(struct scsi_cmnd *cmnd)
3136 struct Scsi_Host *shost = cmnd->device->host;
3137 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3138 struct lpfc_hba *phba = vport->phba;
3139 struct lpfc_iocbq *iocb;
3140 struct lpfc_iocbq *abtsiocb;
3141 struct lpfc_scsi_buf *lpfc_cmd;
3144 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
3146 lpfc_block_error_handler(cmnd);
3147 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
3151 * If pCmd field of the corresponding lpfc_scsi_buf structure
3152 * points to a different SCSI command, then the driver has
3153 * already completed this command, but the midlayer did not
3154 * see the completion before the eh fired. Just return
3157 iocb = &lpfc_cmd->cur_iocbq;
3158 if (lpfc_cmd->pCmd != cmnd)
3161 BUG_ON(iocb->context1 != lpfc_cmd);
3163 abtsiocb = lpfc_sli_get_iocbq(phba);
3164 if (abtsiocb == NULL) {
3170 * The scsi command can not be in txq and it is in flight because the
3171 * pCmd is still pointig at the SCSI command we have to abort. There
3172 * is no need to search the txcmplq. Just send an abort to the FW.
3176 icmd = &abtsiocb->iocb;
3177 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3178 icmd->un.acxri.abortContextTag = cmd->ulpContext;
3179 if (phba->sli_rev == LPFC_SLI_REV4)
3180 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3182 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
3185 icmd->ulpClass = cmd->ulpClass;
3186 if (lpfc_is_link_up(phba))
3187 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3189 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
3191 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
3192 abtsiocb->vport = vport;
3193 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3195 lpfc_sli_release_iocbq(phba, abtsiocb);
3200 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3201 lpfc_sli_poll_fcp_ring (phba);
3203 lpfc_cmd->waitq = &waitq;
3204 /* Wait for abort to complete */
3205 wait_event_timeout(waitq,
3206 (lpfc_cmd->pCmd != cmnd),
3207 (2*vport->cfg_devloss_tmo*HZ));
3209 spin_lock_irq(shost->host_lock);
3210 lpfc_cmd->waitq = NULL;
3211 spin_unlock_irq(shost->host_lock);
3213 if (lpfc_cmd->pCmd == cmnd) {
3215 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3216 "0748 abort handler timed out waiting "
3217 "for abort to complete: ret %#x, ID %d, "
3218 "LUN %d, snum %#lx\n",
3219 ret, cmnd->device->id, cmnd->device->lun,
3220 cmnd->serial_number);
3224 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3225 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3226 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3227 cmnd->device->lun, cmnd->serial_number);
3232 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
3233 * @cmnd: Pointer to scsi_cmnd data structure.
3235 * This routine does a device reset by sending a TARGET_RESET task management
3243 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
3245 struct Scsi_Host *shost = cmnd->device->host;
3246 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3247 struct lpfc_hba *phba = vport->phba;
3248 struct lpfc_scsi_buf *lpfc_cmd;
3249 struct lpfc_iocbq *iocbq, *iocbqrsp;
3250 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3251 struct lpfc_nodelist *pnode = rdata->pnode;
3252 unsigned long later;
3256 struct lpfc_scsi_event_header scsi_event;
3258 lpfc_block_error_handler(cmnd);
3260 * If target is not in a MAPPED state, delay the reset until
3261 * target is rediscovered or devloss timeout expires.
3263 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3264 while (time_after(later, jiffies)) {
3265 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3267 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3269 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3270 rdata = cmnd->device->hostdata;
3273 pnode = rdata->pnode;
3276 scsi_event.event_type = FC_REG_SCSI_EVENT;
3277 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3279 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3280 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3282 fc_host_post_vendor_event(shost,
3283 fc_get_event_number(),
3285 (char *)&scsi_event,
3288 if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
3289 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3290 "0721 LUN Reset rport "
3291 "failure: msec x%x rdata x%p\n",
3292 jiffies_to_msecs(jiffies - later), rdata);
3295 lpfc_cmd = lpfc_get_scsi_buf(phba);
3296 if (lpfc_cmd == NULL)
3298 lpfc_cmd->timeout = 60;
3299 lpfc_cmd->rdata = rdata;
3301 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
3305 lpfc_release_scsi_buf(phba, lpfc_cmd);
3308 iocbq = &lpfc_cmd->cur_iocbq;
3310 /* get a buffer for this IOCB command response */
3311 iocbqrsp = lpfc_sli_get_iocbq(phba);
3312 if (iocbqrsp == NULL) {
3313 lpfc_release_scsi_buf(phba, lpfc_cmd);
3316 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3317 "0703 Issue target reset to TGT %d LUN %d "
3318 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
3319 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
3320 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3321 iocbq, iocbqrsp, lpfc_cmd->timeout);
3322 if (status == IOCB_TIMEDOUT) {
3323 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3324 ret = TIMEOUT_ERROR;
3326 if (status != IOCB_SUCCESS)
3328 lpfc_release_scsi_buf(phba, lpfc_cmd);
3330 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3331 "0713 SCSI layer issued device reset (%d, %d) "
3332 "return x%x status x%x result x%x\n",
3333 cmnd->device->id, cmnd->device->lun, ret,
3334 iocbqrsp->iocb.ulpStatus,
3335 iocbqrsp->iocb.un.ulpWord[4]);
3336 lpfc_sli_release_iocbq(phba, iocbqrsp);
3337 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
3340 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3341 cmnd->device->id, cmnd->device->lun,
3343 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3344 while (time_after(later, jiffies) && cnt) {
3345 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3346 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
3347 cmnd->device->lun, LPFC_CTX_TGT);
3350 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3351 "0719 device reset I/O flush failure: "
3359 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
3360 * @cmnd: Pointer to scsi_cmnd data structure.
3362 * This routine does target reset to all target on @cmnd->device->host.
3369 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
3371 struct Scsi_Host *shost = cmnd->device->host;
3372 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3373 struct lpfc_hba *phba = vport->phba;
3374 struct lpfc_nodelist *ndlp = NULL;
3376 int ret = SUCCESS, status = SUCCESS, i;
3378 struct lpfc_scsi_buf * lpfc_cmd;
3379 unsigned long later;
3380 struct lpfc_scsi_event_header scsi_event;
3382 scsi_event.event_type = FC_REG_SCSI_EVENT;
3383 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3385 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3386 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3388 fc_host_post_vendor_event(shost,
3389 fc_get_event_number(),
3391 (char *)&scsi_event,
3394 lpfc_block_error_handler(cmnd);
3396 * Since the driver manages a single bus device, reset all
3397 * targets known to the driver. Should any target reset
3398 * fail, this routine returns failure to the midlayer.
3400 for (i = 0; i < LPFC_MAX_TARGET; i++) {
3401 /* Search for mapped node by target ID */
3403 spin_lock_irq(shost->host_lock);
3404 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3405 if (!NLP_CHK_NODE_ACT(ndlp))
3407 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
3408 ndlp->nlp_sid == i &&
3414 spin_unlock_irq(shost->host_lock);
3417 lpfc_cmd = lpfc_get_scsi_buf(phba);
3419 lpfc_cmd->timeout = 60;
3420 status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
3422 ndlp->rport->dd_data);
3423 if (status != TIMEOUT_ERROR)
3424 lpfc_release_scsi_buf(phba, lpfc_cmd);
3426 if (!lpfc_cmd || status != SUCCESS) {
3427 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3428 "0700 Bus Reset on target %d failed\n",
3434 * All outstanding txcmplq I/Os should have been aborted by
3435 * the targets. Unfortunately, some targets do not abide by
3436 * this forcing the driver to double check.
3438 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
3440 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3441 0, 0, LPFC_CTX_HOST);
3442 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3443 while (time_after(later, jiffies) && cnt) {
3444 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3445 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
3448 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3449 "0715 Bus Reset I/O flush failure: "
3450 "cnt x%x left x%x\n", cnt, i);
3453 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3454 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
3459 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
3460 * @sdev: Pointer to scsi_device.
3462 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3463 * globally available list of scsi buffers. This routine also makes sure scsi
3464 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3465 * of scsi buffer exists for the lifetime of the driver.
3472 lpfc_slave_alloc(struct scsi_device *sdev)
3474 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3475 struct lpfc_hba *phba = vport->phba;
3476 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3478 uint32_t num_to_alloc = 0;
3479 int num_allocated = 0;
3481 if (!rport || fc_remote_port_chkready(rport))
3484 sdev->hostdata = rport->dd_data;
3487 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3488 * available list of scsi buffers. Don't allocate more than the
3489 * HBA limit conveyed to the midlayer via the host structure. The
3490 * formula accounts for the lun_queue_depth + error handlers + 1
3491 * extra. This list of scsi bufs exists for the lifetime of the driver.
3493 total = phba->total_scsi_bufs;
3494 num_to_alloc = vport->cfg_lun_queue_depth + 2;
3496 /* Allow some exchanges to be available always to complete discovery */
3497 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
3498 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3499 "0704 At limitation of %d preallocated "
3500 "command buffers\n", total);
3502 /* Allow some exchanges to be available always to complete discovery */
3503 } else if (total + num_to_alloc >
3504 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
3505 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3506 "0705 Allocation request of %d "
3507 "command buffers will exceed max of %d. "
3508 "Reducing allocation request to %d.\n",
3509 num_to_alloc, phba->cfg_hba_queue_depth,
3510 (phba->cfg_hba_queue_depth - total));
3511 num_to_alloc = phba->cfg_hba_queue_depth - total;
3513 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3514 if (num_to_alloc != num_allocated) {
3515 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3516 "0708 Allocation request of %d "
3517 "command buffers did not succeed. "
3518 "Allocated %d buffers.\n",
3519 num_to_alloc, num_allocated);
3525 * lpfc_slave_configure - scsi_host_template slave_configure entry point
3526 * @sdev: Pointer to scsi_device.
3528 * This routine configures following items
3529 * - Tag command queuing support for @sdev if supported.
3530 * - Dev loss time out value of fc_rport.
3531 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3537 lpfc_slave_configure(struct scsi_device *sdev)
3539 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3540 struct lpfc_hba *phba = vport->phba;
3541 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
3543 if (sdev->tagged_supported)
3544 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
3546 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
3549 * Initialize the fc transport attributes for the target
3550 * containing this scsi device. Also note that the driver's
3551 * target pointer is stored in the starget_data for the
3552 * driver's sysfs entry point functions.
3554 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
3556 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3557 lpfc_sli_poll_fcp_ring(phba);
3558 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3559 lpfc_poll_rearm_timer(phba);
3566 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
3567 * @sdev: Pointer to scsi_device.
3569 * This routine sets @sdev hostatdata filed to null.
3572 lpfc_slave_destroy(struct scsi_device *sdev)
3574 sdev->hostdata = NULL;
3579 struct scsi_host_template lpfc_template = {
3580 .module = THIS_MODULE,
3581 .name = LPFC_DRIVER_NAME,
3583 .queuecommand = lpfc_queuecommand,
3584 .eh_abort_handler = lpfc_abort_handler,
3585 .eh_device_reset_handler= lpfc_device_reset_handler,
3586 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3587 .slave_alloc = lpfc_slave_alloc,
3588 .slave_configure = lpfc_slave_configure,
3589 .slave_destroy = lpfc_slave_destroy,
3590 .scan_finished = lpfc_scan_finished,
3592 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3593 .cmd_per_lun = LPFC_CMD_PER_LUN,
3594 .use_clustering = ENABLE_CLUSTERING,
3595 .shost_attrs = lpfc_hba_attrs,
3596 .max_sectors = 0xFFFF,
3599 struct scsi_host_template lpfc_vport_template = {
3600 .module = THIS_MODULE,
3601 .name = LPFC_DRIVER_NAME,
3603 .queuecommand = lpfc_queuecommand,
3604 .eh_abort_handler = lpfc_abort_handler,
3605 .eh_device_reset_handler= lpfc_device_reset_handler,
3606 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3607 .slave_alloc = lpfc_slave_alloc,
3608 .slave_configure = lpfc_slave_configure,
3609 .slave_destroy = lpfc_slave_destroy,
3610 .scan_finished = lpfc_scan_finished,
3612 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3613 .cmd_per_lun = LPFC_CMD_PER_LUN,
3614 .use_clustering = ENABLE_CLUSTERING,
3615 .shost_attrs = lpfc_vport_attrs,
3616 .max_sectors = 0xFFFF,