thinkpad-acpi: explain errors from acpi_install_notify_handler
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / lpfc / lpfc_scsi.c
blobc88f59f0ce307c5ab12e57081c9e32443a50f63a
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. *
6 * www.emulex.com *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
8 * *
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"
34 #include "lpfc_hw4.h"
35 #include "lpfc_hw.h"
36 #include "lpfc_sli.h"
37 #include "lpfc_sli4.h"
38 #include "lpfc_nl.h"
39 #include "lpfc_disc.h"
40 #include "lpfc_scsi.h"
41 #include "lpfc.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
49 int _dump_buf_done;
51 static char *dif_op_str[] = {
52 "SCSI_PROT_NORMAL",
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",
60 static void
61 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
63 static void
64 lpfc_debug_save_data(struct scsi_cmnd *cmnd)
66 void *src, *dst;
67 struct scatterlist *sgde = scsi_sglist(cmnd);
69 if (!_dump_buf_data) {
70 printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
71 __func__);
72 return;
76 if (!sgde) {
77 printk(KERN_ERR "BLKGRD ERROR: data scatterlist is null\n");
78 return;
81 dst = (void *) _dump_buf_data;
82 while (sgde) {
83 src = sg_virt(sgde);
84 memcpy(dst, src, sgde->length);
85 dst += sgde->length;
86 sgde = sg_next(sgde);
90 static void
91 lpfc_debug_save_dif(struct scsi_cmnd *cmnd)
93 void *src, *dst;
94 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
96 if (!_dump_buf_dif) {
97 printk(KERN_ERR "BLKGRD ERROR %s _dump_buf_data is NULL\n",
98 __func__);
99 return;
102 if (!sgde) {
103 printk(KERN_ERR "BLKGRD ERROR: prot scatterlist is null\n");
104 return;
107 dst = _dump_buf_dif;
108 while (sgde) {
109 src = sg_virt(sgde);
110 memcpy(dst, src, sgde->length);
111 dst += sgde->length;
112 sgde = sg_next(sgde);
117 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
118 * @phba: Pointer to HBA object.
119 * @lpfc_cmd: lpfc scsi command object pointer.
121 * This function is called from the lpfc_prep_task_mgmt_cmd function to
122 * set the last bit in the response sge entry.
124 static void
125 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
126 struct lpfc_scsi_buf *lpfc_cmd)
128 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
129 if (sgl) {
130 sgl += 1;
131 sgl->word2 = le32_to_cpu(sgl->word2);
132 bf_set(lpfc_sli4_sge_last, sgl, 1);
133 sgl->word2 = cpu_to_le32(sgl->word2);
138 * lpfc_update_stats - Update statistical data for the command completion
139 * @phba: Pointer to HBA object.
140 * @lpfc_cmd: lpfc scsi command object pointer.
142 * This function is called when there is a command completion and this
143 * function updates the statistical data for the command completion.
145 static void
146 lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
148 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
149 struct lpfc_nodelist *pnode = rdata->pnode;
150 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
151 unsigned long flags;
152 struct Scsi_Host *shost = cmd->device->host;
153 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
154 unsigned long latency;
155 int i;
157 if (cmd->result)
158 return;
160 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
162 spin_lock_irqsave(shost->host_lock, flags);
163 if (!vport->stat_data_enabled ||
164 vport->stat_data_blocked ||
165 !pnode->lat_data ||
166 (phba->bucket_type == LPFC_NO_BUCKET)) {
167 spin_unlock_irqrestore(shost->host_lock, flags);
168 return;
171 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
172 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
173 phba->bucket_step;
174 /* check array subscript bounds */
175 if (i < 0)
176 i = 0;
177 else if (i >= LPFC_MAX_BUCKET_COUNT)
178 i = LPFC_MAX_BUCKET_COUNT - 1;
179 } else {
180 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
181 if (latency <= (phba->bucket_base +
182 ((1<<i)*phba->bucket_step)))
183 break;
186 pnode->lat_data[i].cmd_count++;
187 spin_unlock_irqrestore(shost->host_lock, flags);
191 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
192 * @phba: Pointer to HBA context object.
193 * @vport: Pointer to vport object.
194 * @ndlp: Pointer to FC node associated with the target.
195 * @lun: Lun number of the scsi device.
196 * @old_val: Old value of the queue depth.
197 * @new_val: New value of the queue depth.
199 * This function sends an event to the mgmt application indicating
200 * there is a change in the scsi device queue depth.
202 static void
203 lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
204 struct lpfc_vport *vport,
205 struct lpfc_nodelist *ndlp,
206 uint32_t lun,
207 uint32_t old_val,
208 uint32_t new_val)
210 struct lpfc_fast_path_event *fast_path_evt;
211 unsigned long flags;
213 fast_path_evt = lpfc_alloc_fast_evt(phba);
214 if (!fast_path_evt)
215 return;
217 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
218 FC_REG_SCSI_EVENT;
219 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
220 LPFC_EVENT_VARQUEDEPTH;
222 /* Report all luns with change in queue depth */
223 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
224 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
225 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
226 &ndlp->nlp_portname, sizeof(struct lpfc_name));
227 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
228 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
231 fast_path_evt->un.queue_depth_evt.oldval = old_val;
232 fast_path_evt->un.queue_depth_evt.newval = new_val;
233 fast_path_evt->vport = vport;
235 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
236 spin_lock_irqsave(&phba->hbalock, flags);
237 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
238 spin_unlock_irqrestore(&phba->hbalock, flags);
239 lpfc_worker_wake_up(phba);
241 return;
245 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
246 * @phba: The Hba for which this call is being executed.
248 * This routine is called when there is resource error in driver or firmware.
249 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
250 * posts at most 1 event each second. This routine wakes up worker thread of
251 * @phba to process WORKER_RAM_DOWN_EVENT event.
253 * This routine should be called with no lock held.
255 void
256 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
258 unsigned long flags;
259 uint32_t evt_posted;
261 spin_lock_irqsave(&phba->hbalock, flags);
262 atomic_inc(&phba->num_rsrc_err);
263 phba->last_rsrc_error_time = jiffies;
265 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
266 spin_unlock_irqrestore(&phba->hbalock, flags);
267 return;
270 phba->last_ramp_down_time = jiffies;
272 spin_unlock_irqrestore(&phba->hbalock, flags);
274 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
275 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
276 if (!evt_posted)
277 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
278 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
280 if (!evt_posted)
281 lpfc_worker_wake_up(phba);
282 return;
286 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
287 * @phba: The Hba for which this call is being executed.
289 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
290 * post at most 1 event every 5 minute after last_ramp_up_time or
291 * last_rsrc_error_time. This routine wakes up worker thread of @phba
292 * to process WORKER_RAM_DOWN_EVENT event.
294 * This routine should be called with no lock held.
296 static inline void
297 lpfc_rampup_queue_depth(struct lpfc_vport *vport,
298 uint32_t queue_depth)
300 unsigned long flags;
301 struct lpfc_hba *phba = vport->phba;
302 uint32_t evt_posted;
303 atomic_inc(&phba->num_cmd_success);
305 if (vport->cfg_lun_queue_depth <= queue_depth)
306 return;
307 spin_lock_irqsave(&phba->hbalock, flags);
308 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
309 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
310 spin_unlock_irqrestore(&phba->hbalock, flags);
311 return;
313 phba->last_ramp_up_time = jiffies;
314 spin_unlock_irqrestore(&phba->hbalock, flags);
316 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
317 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
318 if (!evt_posted)
319 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
320 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
322 if (!evt_posted)
323 lpfc_worker_wake_up(phba);
324 return;
328 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
329 * @phba: The Hba for which this call is being executed.
331 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
332 * thread.This routine reduces queue depth for all scsi device on each vport
333 * associated with @phba.
335 void
336 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
338 struct lpfc_vport **vports;
339 struct Scsi_Host *shost;
340 struct scsi_device *sdev;
341 unsigned long new_queue_depth, old_queue_depth;
342 unsigned long num_rsrc_err, num_cmd_success;
343 int i;
344 struct lpfc_rport_data *rdata;
346 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
347 num_cmd_success = atomic_read(&phba->num_cmd_success);
349 vports = lpfc_create_vport_work_array(phba);
350 if (vports != NULL)
351 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
352 shost = lpfc_shost_from_vport(vports[i]);
353 shost_for_each_device(sdev, shost) {
354 new_queue_depth =
355 sdev->queue_depth * num_rsrc_err /
356 (num_rsrc_err + num_cmd_success);
357 if (!new_queue_depth)
358 new_queue_depth = sdev->queue_depth - 1;
359 else
360 new_queue_depth = sdev->queue_depth -
361 new_queue_depth;
362 old_queue_depth = sdev->queue_depth;
363 if (sdev->ordered_tags)
364 scsi_adjust_queue_depth(sdev,
365 MSG_ORDERED_TAG,
366 new_queue_depth);
367 else
368 scsi_adjust_queue_depth(sdev,
369 MSG_SIMPLE_TAG,
370 new_queue_depth);
371 rdata = sdev->hostdata;
372 if (rdata)
373 lpfc_send_sdev_queuedepth_change_event(
374 phba, vports[i],
375 rdata->pnode,
376 sdev->lun, old_queue_depth,
377 new_queue_depth);
380 lpfc_destroy_vport_work_array(phba, vports);
381 atomic_set(&phba->num_rsrc_err, 0);
382 atomic_set(&phba->num_cmd_success, 0);
386 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
387 * @phba: The Hba for which this call is being executed.
389 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
390 * thread.This routine increases queue depth for all scsi device on each vport
391 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
392 * num_cmd_success to zero.
394 void
395 lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
397 struct lpfc_vport **vports;
398 struct Scsi_Host *shost;
399 struct scsi_device *sdev;
400 int i;
401 struct lpfc_rport_data *rdata;
403 vports = lpfc_create_vport_work_array(phba);
404 if (vports != NULL)
405 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
406 shost = lpfc_shost_from_vport(vports[i]);
407 shost_for_each_device(sdev, shost) {
408 if (vports[i]->cfg_lun_queue_depth <=
409 sdev->queue_depth)
410 continue;
411 if (sdev->ordered_tags)
412 scsi_adjust_queue_depth(sdev,
413 MSG_ORDERED_TAG,
414 sdev->queue_depth+1);
415 else
416 scsi_adjust_queue_depth(sdev,
417 MSG_SIMPLE_TAG,
418 sdev->queue_depth+1);
419 rdata = sdev->hostdata;
420 if (rdata)
421 lpfc_send_sdev_queuedepth_change_event(
422 phba, vports[i],
423 rdata->pnode,
424 sdev->lun,
425 sdev->queue_depth - 1,
426 sdev->queue_depth);
429 lpfc_destroy_vport_work_array(phba, vports);
430 atomic_set(&phba->num_rsrc_err, 0);
431 atomic_set(&phba->num_cmd_success, 0);
435 * lpfc_scsi_dev_block - set all scsi hosts to block state
436 * @phba: Pointer to HBA context object.
438 * This function walks vport list and set each SCSI host to block state
439 * by invoking fc_remote_port_delete() routine. This function is invoked
440 * with EEH when device's PCI slot has been permanently disabled.
442 void
443 lpfc_scsi_dev_block(struct lpfc_hba *phba)
445 struct lpfc_vport **vports;
446 struct Scsi_Host *shost;
447 struct scsi_device *sdev;
448 struct fc_rport *rport;
449 int i;
451 vports = lpfc_create_vport_work_array(phba);
452 if (vports != NULL)
453 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
454 shost = lpfc_shost_from_vport(vports[i]);
455 shost_for_each_device(sdev, shost) {
456 rport = starget_to_rport(scsi_target(sdev));
457 fc_remote_port_delete(rport);
460 lpfc_destroy_vport_work_array(phba, vports);
464 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
465 * @vport: The virtual port for which this call being executed.
466 * @num_to_allocate: The requested number of buffers to allocate.
468 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
469 * the scsi buffer contains all the necessary information needed to initiate
470 * a SCSI I/O. The non-DMAable buffer region contains information to build
471 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
472 * and the initial BPL. In addition to allocating memory, the FCP CMND and
473 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
475 * Return codes:
476 * int - number of scsi buffers that were allocated.
477 * 0 = failure, less than num_to_alloc is a partial failure.
479 static int
480 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
482 struct lpfc_hba *phba = vport->phba;
483 struct lpfc_scsi_buf *psb;
484 struct ulp_bde64 *bpl;
485 IOCB_t *iocb;
486 dma_addr_t pdma_phys_fcp_cmd;
487 dma_addr_t pdma_phys_fcp_rsp;
488 dma_addr_t pdma_phys_bpl;
489 uint16_t iotag;
490 int bcnt;
492 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
493 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
494 if (!psb)
495 break;
498 * Get memory from the pci pool to map the virt space to pci
499 * bus space for an I/O. The DMA buffer includes space for the
500 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
501 * necessary to support the sg_tablesize.
503 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
504 GFP_KERNEL, &psb->dma_handle);
505 if (!psb->data) {
506 kfree(psb);
507 break;
510 /* Initialize virtual ptrs to dma_buf region. */
511 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
513 /* Allocate iotag for psb->cur_iocbq. */
514 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
515 if (iotag == 0) {
516 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
517 psb->data, psb->dma_handle);
518 kfree(psb);
519 break;
521 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
523 psb->fcp_cmnd = psb->data;
524 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
525 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
526 sizeof(struct fcp_rsp);
528 /* Initialize local short-hand pointers. */
529 bpl = psb->fcp_bpl;
530 pdma_phys_fcp_cmd = psb->dma_handle;
531 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
532 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
533 sizeof(struct fcp_rsp);
536 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
537 * are sg list bdes. Initialize the first two and leave the
538 * rest for queuecommand.
540 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
541 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
542 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
543 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
544 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
546 /* Setup the physical region for the FCP RSP */
547 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
548 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
549 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
550 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
551 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
554 * Since the IOCB for the FCP I/O is built into this
555 * lpfc_scsi_buf, initialize it with all known data now.
557 iocb = &psb->cur_iocbq.iocb;
558 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
559 if ((phba->sli_rev == 3) &&
560 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
561 /* fill in immediate fcp command BDE */
562 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
563 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
564 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
565 unsli3.fcp_ext.icd);
566 iocb->un.fcpi64.bdl.addrHigh = 0;
567 iocb->ulpBdeCount = 0;
568 iocb->ulpLe = 0;
569 /* fill in responce BDE */
570 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
571 BUFF_TYPE_BDE_64;
572 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
573 sizeof(struct fcp_rsp);
574 iocb->unsli3.fcp_ext.rbde.addrLow =
575 putPaddrLow(pdma_phys_fcp_rsp);
576 iocb->unsli3.fcp_ext.rbde.addrHigh =
577 putPaddrHigh(pdma_phys_fcp_rsp);
578 } else {
579 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
580 iocb->un.fcpi64.bdl.bdeSize =
581 (2 * sizeof(struct ulp_bde64));
582 iocb->un.fcpi64.bdl.addrLow =
583 putPaddrLow(pdma_phys_bpl);
584 iocb->un.fcpi64.bdl.addrHigh =
585 putPaddrHigh(pdma_phys_bpl);
586 iocb->ulpBdeCount = 1;
587 iocb->ulpLe = 1;
589 iocb->ulpClass = CLASS3;
590 psb->status = IOSTAT_SUCCESS;
591 /* Put it back into the SCSI buffer list */
592 lpfc_release_scsi_buf_s4(phba, psb);
596 return bcnt;
600 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
601 * @phba: pointer to lpfc hba data structure.
602 * @axri: pointer to the fcp xri abort wcqe structure.
604 * This routine is invoked by the worker thread to process a SLI4 fast-path
605 * FCP aborted xri.
607 void
608 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
609 struct sli4_wcqe_xri_aborted *axri)
611 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
612 struct lpfc_scsi_buf *psb, *next_psb;
613 unsigned long iflag = 0;
615 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock, iflag);
616 list_for_each_entry_safe(psb, next_psb,
617 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
618 if (psb->cur_iocbq.sli4_xritag == xri) {
619 list_del(&psb->list);
620 psb->status = IOSTAT_SUCCESS;
621 spin_unlock_irqrestore(
622 &phba->sli4_hba.abts_scsi_buf_list_lock,
623 iflag);
624 lpfc_release_scsi_buf_s4(phba, psb);
625 return;
628 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
629 iflag);
633 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
634 * @phba: pointer to lpfc hba data structure.
636 * This routine walks the list of scsi buffers that have been allocated and
637 * repost them to the HBA by using SGL block post. This is needed after a
638 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
639 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
640 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
642 * Returns: 0 = success, non-zero failure.
645 lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
647 struct lpfc_scsi_buf *psb;
648 int index, status, bcnt = 0, rcnt = 0, rc = 0;
649 LIST_HEAD(sblist);
651 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
652 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
653 if (psb) {
654 /* Remove from SCSI buffer list */
655 list_del(&psb->list);
656 /* Add it to a local SCSI buffer list */
657 list_add_tail(&psb->list, &sblist);
658 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
659 bcnt = rcnt;
660 rcnt = 0;
662 } else
663 /* A hole present in the XRI array, need to skip */
664 bcnt = rcnt;
666 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
667 /* End of XRI array for SCSI buffer, complete */
668 bcnt = rcnt;
670 /* Continue until collect up to a nembed page worth of sgls */
671 if (bcnt == 0)
672 continue;
673 /* Now, post the SCSI buffer list sgls as a block */
674 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
675 /* Reset SCSI buffer count for next round of posting */
676 bcnt = 0;
677 while (!list_empty(&sblist)) {
678 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
679 list);
680 if (status) {
681 /* Put this back on the abort scsi list */
682 psb->status = IOSTAT_LOCAL_REJECT;
683 psb->result = IOERR_ABORT_REQUESTED;
684 rc++;
685 } else
686 psb->status = IOSTAT_SUCCESS;
687 /* Put it back into the SCSI buffer list */
688 lpfc_release_scsi_buf_s4(phba, psb);
691 return rc;
695 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
696 * @vport: The virtual port for which this call being executed.
697 * @num_to_allocate: The requested number of buffers to allocate.
699 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
700 * the scsi buffer contains all the necessary information needed to initiate
701 * a SCSI I/O.
703 * Return codes:
704 * int - number of scsi buffers that were allocated.
705 * 0 = failure, less than num_to_alloc is a partial failure.
707 static int
708 lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
710 struct lpfc_hba *phba = vport->phba;
711 struct lpfc_scsi_buf *psb;
712 struct sli4_sge *sgl;
713 IOCB_t *iocb;
714 dma_addr_t pdma_phys_fcp_cmd;
715 dma_addr_t pdma_phys_fcp_rsp;
716 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
717 uint16_t iotag, last_xritag = NO_XRI;
718 int status = 0, index;
719 int bcnt;
720 int non_sequential_xri = 0;
721 int rc = 0;
722 LIST_HEAD(sblist);
724 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
725 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
726 if (!psb)
727 break;
730 * Get memory from the pci pool to map the virt space to pci bus
731 * space for an I/O. The DMA buffer includes space for the
732 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
733 * necessary to support the sg_tablesize.
735 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
736 GFP_KERNEL, &psb->dma_handle);
737 if (!psb->data) {
738 kfree(psb);
739 break;
742 /* Initialize virtual ptrs to dma_buf region. */
743 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
745 /* Allocate iotag for psb->cur_iocbq. */
746 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
747 if (iotag == 0) {
748 kfree(psb);
749 break;
752 psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
753 if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
754 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
755 psb->data, psb->dma_handle);
756 kfree(psb);
757 break;
759 if (last_xritag != NO_XRI
760 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
761 non_sequential_xri = 1;
762 } else
763 list_add_tail(&psb->list, &sblist);
764 last_xritag = psb->cur_iocbq.sli4_xritag;
766 index = phba->sli4_hba.scsi_xri_cnt++;
767 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
769 psb->fcp_bpl = psb->data;
770 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
771 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
772 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
773 sizeof(struct fcp_cmnd));
775 /* Initialize local short-hand pointers. */
776 sgl = (struct sli4_sge *)psb->fcp_bpl;
777 pdma_phys_bpl = psb->dma_handle;
778 pdma_phys_fcp_cmd =
779 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
780 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
781 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
784 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
785 * are sg list bdes. Initialize the first two and leave the
786 * rest for queuecommand.
788 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
789 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
790 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_cmnd));
791 bf_set(lpfc_sli4_sge_last, sgl, 0);
792 sgl->word2 = cpu_to_le32(sgl->word2);
793 sgl->word3 = cpu_to_le32(sgl->word3);
794 sgl++;
796 /* Setup the physical region for the FCP RSP */
797 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
798 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
799 bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_rsp));
800 bf_set(lpfc_sli4_sge_last, sgl, 1);
801 sgl->word2 = cpu_to_le32(sgl->word2);
802 sgl->word3 = cpu_to_le32(sgl->word3);
805 * Since the IOCB for the FCP I/O is built into this
806 * lpfc_scsi_buf, initialize it with all known data now.
808 iocb = &psb->cur_iocbq.iocb;
809 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
810 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
811 /* setting the BLP size to 2 * sizeof BDE may not be correct.
812 * We are setting the bpl to point to out sgl. An sgl's
813 * entries are 16 bytes, a bpl entries are 12 bytes.
815 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
816 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
817 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
818 iocb->ulpBdeCount = 1;
819 iocb->ulpLe = 1;
820 iocb->ulpClass = CLASS3;
821 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
822 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
823 else
824 pdma_phys_bpl1 = 0;
825 psb->dma_phys_bpl = pdma_phys_bpl;
826 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
827 if (non_sequential_xri) {
828 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
829 pdma_phys_bpl1,
830 psb->cur_iocbq.sli4_xritag);
831 if (status) {
832 /* Put this back on the abort scsi list */
833 psb->status = IOSTAT_LOCAL_REJECT;
834 psb->result = IOERR_ABORT_REQUESTED;
835 rc++;
836 } else
837 psb->status = IOSTAT_SUCCESS;
838 /* Put it back into the SCSI buffer list */
839 lpfc_release_scsi_buf_s4(phba, psb);
840 break;
843 if (bcnt) {
844 status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
845 /* Reset SCSI buffer count for next round of posting */
846 while (!list_empty(&sblist)) {
847 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
848 list);
849 if (status) {
850 /* Put this back on the abort scsi list */
851 psb->status = IOSTAT_LOCAL_REJECT;
852 psb->result = IOERR_ABORT_REQUESTED;
853 rc++;
854 } else
855 psb->status = IOSTAT_SUCCESS;
856 /* Put it back into the SCSI buffer list */
857 lpfc_release_scsi_buf_s4(phba, psb);
861 return bcnt + non_sequential_xri - rc;
865 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
866 * @vport: The virtual port for which this call being executed.
867 * @num_to_allocate: The requested number of buffers to allocate.
869 * This routine wraps the actual SCSI buffer allocator function pointer from
870 * the lpfc_hba struct.
872 * Return codes:
873 * int - number of scsi buffers that were allocated.
874 * 0 = failure, less than num_to_alloc is a partial failure.
876 static inline int
877 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
879 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
883 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
884 * @phba: The HBA for which this call is being executed.
886 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
887 * and returns to caller.
889 * Return codes:
890 * NULL - Error
891 * Pointer to lpfc_scsi_buf - Success
893 static struct lpfc_scsi_buf*
894 lpfc_get_scsi_buf(struct lpfc_hba * phba)
896 struct lpfc_scsi_buf * lpfc_cmd = NULL;
897 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
898 unsigned long iflag = 0;
900 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
901 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
902 if (lpfc_cmd) {
903 lpfc_cmd->seg_cnt = 0;
904 lpfc_cmd->nonsg_phys = 0;
905 lpfc_cmd->prot_seg_cnt = 0;
907 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
908 return lpfc_cmd;
912 * lpfc_release_scsi_buf - 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.
919 static void
920 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
922 unsigned long iflag = 0;
924 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
925 psb->pCmd = NULL;
926 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
927 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
931 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
932 * @phba: The Hba for which this call is being executed.
933 * @psb: The scsi buffer which is being released.
935 * This routine releases @psb scsi buffer by adding it to tail of @phba
936 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
937 * and cannot be reused for at least RA_TOV amount of time if it was
938 * aborted.
940 static void
941 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
943 unsigned long iflag = 0;
945 if (psb->status == IOSTAT_LOCAL_REJECT
946 && psb->result == IOERR_ABORT_REQUESTED) {
947 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
948 iflag);
949 psb->pCmd = NULL;
950 list_add_tail(&psb->list,
951 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
952 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
953 iflag);
954 } else {
956 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
957 psb->pCmd = NULL;
958 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
959 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
964 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
965 * @phba: The Hba for which this call is being executed.
966 * @psb: The scsi buffer which is being released.
968 * This routine releases @psb scsi buffer by adding it to tail of @phba
969 * lpfc_scsi_buf_list list.
971 static void
972 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
975 phba->lpfc_release_scsi_buf(phba, psb);
979 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
980 * @phba: The Hba for which this call is being executed.
981 * @lpfc_cmd: The scsi buffer which is going to be mapped.
983 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
984 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
985 * through sg elements and format the bdea. This routine also initializes all
986 * IOCB fields which are dependent on scsi command request buffer.
988 * Return codes:
989 * 1 - Error
990 * 0 - Success
992 static int
993 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
995 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
996 struct scatterlist *sgel = NULL;
997 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
998 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
999 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1000 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1001 dma_addr_t physaddr;
1002 uint32_t num_bde = 0;
1003 int nseg, datadir = scsi_cmnd->sc_data_direction;
1006 * There are three possibilities here - use scatter-gather segment, use
1007 * the single mapping, or neither. Start the lpfc command prep by
1008 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1009 * data bde entry.
1011 bpl += 2;
1012 if (scsi_sg_count(scsi_cmnd)) {
1014 * The driver stores the segment count returned from pci_map_sg
1015 * because this a count of dma-mappings used to map the use_sg
1016 * pages. They are not guaranteed to be the same for those
1017 * architectures that implement an IOMMU.
1020 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1021 scsi_sg_count(scsi_cmnd), datadir);
1022 if (unlikely(!nseg))
1023 return 1;
1025 lpfc_cmd->seg_cnt = nseg;
1026 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1027 printk(KERN_ERR "%s: Too many sg segments from "
1028 "dma_map_sg. Config %d, seg_cnt %d\n",
1029 __func__, phba->cfg_sg_seg_cnt,
1030 lpfc_cmd->seg_cnt);
1031 scsi_dma_unmap(scsi_cmnd);
1032 return 1;
1036 * The driver established a maximum scatter-gather segment count
1037 * during probe that limits the number of sg elements in any
1038 * single scsi command. Just run through the seg_cnt and format
1039 * the bde's.
1040 * When using SLI-3 the driver will try to fit all the BDEs into
1041 * the IOCB. If it can't then the BDEs get added to a BPL as it
1042 * does for SLI-2 mode.
1044 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1045 physaddr = sg_dma_address(sgel);
1046 if (phba->sli_rev == 3 &&
1047 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1048 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1049 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1050 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1051 data_bde->addrLow = putPaddrLow(physaddr);
1052 data_bde->addrHigh = putPaddrHigh(physaddr);
1053 data_bde++;
1054 } else {
1055 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1056 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1057 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1058 bpl->addrLow =
1059 le32_to_cpu(putPaddrLow(physaddr));
1060 bpl->addrHigh =
1061 le32_to_cpu(putPaddrHigh(physaddr));
1062 bpl++;
1068 * Finish initializing those IOCB fields that are dependent on the
1069 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1070 * explicitly reinitialized and for SLI-3 the extended bde count is
1071 * explicitly reinitialized since all iocb memory resources are reused.
1073 if (phba->sli_rev == 3 &&
1074 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
1075 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1077 * The extended IOCB format can only fit 3 BDE or a BPL.
1078 * This I/O has more than 3 BDE so the 1st data bde will
1079 * be a BPL that is filled in here.
1081 physaddr = lpfc_cmd->dma_handle;
1082 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1083 data_bde->tus.f.bdeSize = (num_bde *
1084 sizeof(struct ulp_bde64));
1085 physaddr += (sizeof(struct fcp_cmnd) +
1086 sizeof(struct fcp_rsp) +
1087 (2 * sizeof(struct ulp_bde64)));
1088 data_bde->addrHigh = putPaddrHigh(physaddr);
1089 data_bde->addrLow = putPaddrLow(physaddr);
1090 /* ebde count includes the responce bde and data bpl */
1091 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1092 } else {
1093 /* ebde count includes the responce bde and data bdes */
1094 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1096 } else {
1097 iocb_cmd->un.fcpi64.bdl.bdeSize =
1098 ((num_bde + 2) * sizeof(struct ulp_bde64));
1100 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1103 * Due to difference in data length between DIF/non-DIF paths,
1104 * we need to set word 4 of IOCB here
1106 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1107 return 0;
1111 * Given a scsi cmnd, determine the BlockGuard profile to be used
1112 * with the cmd
1114 static int
1115 lpfc_sc_to_sli_prof(struct scsi_cmnd *sc)
1117 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1118 uint8_t ret_prof = LPFC_PROF_INVALID;
1120 if (guard_type == SHOST_DIX_GUARD_IP) {
1121 switch (scsi_get_prot_op(sc)) {
1122 case SCSI_PROT_READ_INSERT:
1123 case SCSI_PROT_WRITE_STRIP:
1124 ret_prof = LPFC_PROF_AST2;
1125 break;
1127 case SCSI_PROT_READ_STRIP:
1128 case SCSI_PROT_WRITE_INSERT:
1129 ret_prof = LPFC_PROF_A1;
1130 break;
1132 case SCSI_PROT_READ_PASS:
1133 case SCSI_PROT_WRITE_PASS:
1134 ret_prof = LPFC_PROF_AST1;
1135 break;
1137 case SCSI_PROT_NORMAL:
1138 default:
1139 printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
1140 scsi_get_prot_op(sc), guard_type);
1141 break;
1144 } else if (guard_type == SHOST_DIX_GUARD_CRC) {
1145 switch (scsi_get_prot_op(sc)) {
1146 case SCSI_PROT_READ_STRIP:
1147 case SCSI_PROT_WRITE_INSERT:
1148 ret_prof = LPFC_PROF_A1;
1149 break;
1151 case SCSI_PROT_READ_PASS:
1152 case SCSI_PROT_WRITE_PASS:
1153 ret_prof = LPFC_PROF_C1;
1154 break;
1156 case SCSI_PROT_READ_INSERT:
1157 case SCSI_PROT_WRITE_STRIP:
1158 case SCSI_PROT_NORMAL:
1159 default:
1160 printk(KERN_ERR "Bad op/guard:%d/%d combination\n",
1161 scsi_get_prot_op(sc), guard_type);
1162 break;
1164 } else {
1165 /* unsupported format */
1166 BUG();
1169 return ret_prof;
1172 struct scsi_dif_tuple {
1173 __be16 guard_tag; /* Checksum */
1174 __be16 app_tag; /* Opaque storage */
1175 __be32 ref_tag; /* Target LBA or indirect LBA */
1178 static inline unsigned
1179 lpfc_cmd_blksize(struct scsi_cmnd *sc)
1181 return sc->device->sector_size;
1185 * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
1186 * @sc: in: SCSI command
1187 * @apptagmask: out: app tag mask
1188 * @apptagval: out: app tag value
1189 * @reftag: out: ref tag (reference tag)
1191 * Description:
1192 * Extract DIF parameters from the command if possible. Otherwise,
1193 * use default parameters.
1196 static inline void
1197 lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
1198 uint16_t *apptagval, uint32_t *reftag)
1200 struct scsi_dif_tuple *spt;
1201 unsigned char op = scsi_get_prot_op(sc);
1202 unsigned int protcnt = scsi_prot_sg_count(sc);
1203 static int cnt;
1205 if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
1206 op == SCSI_PROT_WRITE_PASS)) {
1208 cnt++;
1209 spt = page_address(sg_page(scsi_prot_sglist(sc))) +
1210 scsi_prot_sglist(sc)[0].offset;
1211 *apptagmask = 0;
1212 *apptagval = 0;
1213 *reftag = cpu_to_be32(spt->ref_tag);
1215 } else {
1216 /* SBC defines ref tag to be lower 32bits of LBA */
1217 *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
1218 *apptagmask = 0;
1219 *apptagval = 0;
1224 * This function sets up buffer list for protection groups of
1225 * type LPFC_PG_TYPE_NO_DIF
1227 * This is usually used when the HBA is instructed to generate
1228 * DIFs and insert them into data stream (or strip DIF from
1229 * incoming data stream)
1231 * The buffer list consists of just one protection group described
1232 * below:
1233 * +-------------------------+
1234 * start of prot group --> | PDE_1 |
1235 * +-------------------------+
1236 * | Data BDE |
1237 * +-------------------------+
1238 * |more Data BDE's ... (opt)|
1239 * +-------------------------+
1241 * @sc: pointer to scsi command we're working on
1242 * @bpl: pointer to buffer list for protection groups
1243 * @datacnt: number of segments of data that have been dma mapped
1245 * Note: Data s/g buffers have been dma mapped
1247 static int
1248 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1249 struct ulp_bde64 *bpl, int datasegcnt)
1251 struct scatterlist *sgde = NULL; /* s/g data entry */
1252 struct lpfc_pde *pde1 = NULL;
1253 dma_addr_t physaddr;
1254 int i = 0, num_bde = 0;
1255 int datadir = sc->sc_data_direction;
1256 int prof = LPFC_PROF_INVALID;
1257 unsigned blksize;
1258 uint32_t reftag;
1259 uint16_t apptagmask, apptagval;
1261 pde1 = (struct lpfc_pde *) bpl;
1262 prof = lpfc_sc_to_sli_prof(sc);
1264 if (prof == LPFC_PROF_INVALID)
1265 goto out;
1267 /* extract some info from the scsi command for PDE1*/
1268 blksize = lpfc_cmd_blksize(sc);
1269 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1271 /* setup PDE1 with what we have */
1272 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1273 BG_EC_STOP_ERR);
1274 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1276 num_bde++;
1277 bpl++;
1279 /* assumption: caller has already run dma_map_sg on command data */
1280 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1281 physaddr = sg_dma_address(sgde);
1282 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1283 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1284 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1285 if (datadir == DMA_TO_DEVICE)
1286 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1287 else
1288 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1289 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1290 bpl++;
1291 num_bde++;
1294 out:
1295 return num_bde;
1299 * This function sets up buffer list for protection groups of
1300 * type LPFC_PG_TYPE_DIF_BUF
1302 * This is usually used when DIFs are in their own buffers,
1303 * separate from the data. The HBA can then by instructed
1304 * to place the DIFs in the outgoing stream. For read operations,
1305 * The HBA could extract the DIFs and place it in DIF buffers.
1307 * The buffer list for this type consists of one or more of the
1308 * protection groups described below:
1309 * +-------------------------+
1310 * start of first prot group --> | PDE_1 |
1311 * +-------------------------+
1312 * | PDE_3 (Prot BDE) |
1313 * +-------------------------+
1314 * | Data BDE |
1315 * +-------------------------+
1316 * |more Data BDE's ... (opt)|
1317 * +-------------------------+
1318 * start of new prot group --> | PDE_1 |
1319 * +-------------------------+
1320 * | ... |
1321 * +-------------------------+
1323 * @sc: pointer to scsi command we're working on
1324 * @bpl: pointer to buffer list for protection groups
1325 * @datacnt: number of segments of data that have been dma mapped
1326 * @protcnt: number of segment of protection data that have been dma mapped
1328 * Note: It is assumed that both data and protection s/g buffers have been
1329 * mapped for DMA
1331 static int
1332 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1333 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1335 struct scatterlist *sgde = NULL; /* s/g data entry */
1336 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1337 struct lpfc_pde *pde1 = NULL;
1338 struct ulp_bde64 *prot_bde = NULL;
1339 dma_addr_t dataphysaddr, protphysaddr;
1340 unsigned short curr_data = 0, curr_prot = 0;
1341 unsigned int split_offset, protgroup_len;
1342 unsigned int protgrp_blks, protgrp_bytes;
1343 unsigned int remainder, subtotal;
1344 int prof = LPFC_PROF_INVALID;
1345 int datadir = sc->sc_data_direction;
1346 unsigned char pgdone = 0, alldone = 0;
1347 unsigned blksize;
1348 uint32_t reftag;
1349 uint16_t apptagmask, apptagval;
1350 int num_bde = 0;
1352 sgpe = scsi_prot_sglist(sc);
1353 sgde = scsi_sglist(sc);
1355 if (!sgpe || !sgde) {
1356 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1357 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1358 sgpe, sgde);
1359 return 0;
1362 prof = lpfc_sc_to_sli_prof(sc);
1363 if (prof == LPFC_PROF_INVALID)
1364 goto out;
1366 /* extract some info from the scsi command for PDE1*/
1367 blksize = lpfc_cmd_blksize(sc);
1368 lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
1370 split_offset = 0;
1371 do {
1372 /* setup the first PDE_1 */
1373 pde1 = (struct lpfc_pde *) bpl;
1375 lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
1376 BG_EC_STOP_ERR);
1377 lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
1379 num_bde++;
1380 bpl++;
1382 /* setup the first BDE that points to protection buffer */
1383 prot_bde = (struct ulp_bde64 *) bpl;
1384 protphysaddr = sg_dma_address(sgpe);
1385 prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1386 prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1387 protgroup_len = sg_dma_len(sgpe);
1390 /* must be integer multiple of the DIF block length */
1391 BUG_ON(protgroup_len % 8);
1393 protgrp_blks = protgroup_len / 8;
1394 protgrp_bytes = protgrp_blks * blksize;
1396 prot_bde->tus.f.bdeSize = protgroup_len;
1397 if (datadir == DMA_TO_DEVICE)
1398 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1399 else
1400 prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1401 prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
1403 curr_prot++;
1404 num_bde++;
1406 /* setup BDE's for data blocks associated with DIF data */
1407 pgdone = 0;
1408 subtotal = 0; /* total bytes processed for current prot grp */
1409 while (!pgdone) {
1410 if (!sgde) {
1411 printk(KERN_ERR "%s Invalid data segment\n",
1412 __func__);
1413 return 0;
1415 bpl++;
1416 dataphysaddr = sg_dma_address(sgde) + split_offset;
1417 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1418 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1420 remainder = sg_dma_len(sgde) - split_offset;
1422 if ((subtotal + remainder) <= protgrp_bytes) {
1423 /* we can use this whole buffer */
1424 bpl->tus.f.bdeSize = remainder;
1425 split_offset = 0;
1427 if ((subtotal + remainder) == protgrp_bytes)
1428 pgdone = 1;
1429 } else {
1430 /* must split this buffer with next prot grp */
1431 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1432 split_offset += bpl->tus.f.bdeSize;
1435 subtotal += bpl->tus.f.bdeSize;
1437 if (datadir == DMA_TO_DEVICE)
1438 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1439 else
1440 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1441 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1443 num_bde++;
1444 curr_data++;
1446 if (split_offset)
1447 break;
1449 /* Move to the next s/g segment if possible */
1450 sgde = sg_next(sgde);
1453 /* are we done ? */
1454 if (curr_prot == protcnt) {
1455 alldone = 1;
1456 } else if (curr_prot < protcnt) {
1457 /* advance to next prot buffer */
1458 sgpe = sg_next(sgpe);
1459 bpl++;
1461 /* update the reference tag */
1462 reftag += protgrp_blks;
1463 } else {
1464 /* if we're here, we have a bug */
1465 printk(KERN_ERR "BLKGRD: bug in %s\n", __func__);
1468 } while (!alldone);
1470 out:
1473 return num_bde;
1476 * Given a SCSI command that supports DIF, determine composition of protection
1477 * groups involved in setting up buffer lists
1479 * Returns:
1480 * for DIF (for both read and write)
1481 * */
1482 static int
1483 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1485 int ret = LPFC_PG_TYPE_INVALID;
1486 unsigned char op = scsi_get_prot_op(sc);
1488 switch (op) {
1489 case SCSI_PROT_READ_STRIP:
1490 case SCSI_PROT_WRITE_INSERT:
1491 ret = LPFC_PG_TYPE_NO_DIF;
1492 break;
1493 case SCSI_PROT_READ_INSERT:
1494 case SCSI_PROT_WRITE_STRIP:
1495 case SCSI_PROT_READ_PASS:
1496 case SCSI_PROT_WRITE_PASS:
1497 ret = LPFC_PG_TYPE_DIF_BUF;
1498 break;
1499 default:
1500 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1501 "9021 Unsupported protection op:%d\n", op);
1502 break;
1505 return ret;
1509 * This is the protection/DIF aware version of
1510 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1511 * two functions eventually, but for now, it's here
1513 static int
1514 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1515 struct lpfc_scsi_buf *lpfc_cmd)
1517 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1518 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1519 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1520 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1521 uint32_t num_bde = 0;
1522 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1523 int prot_group_type = 0;
1524 int diflen, fcpdl;
1525 unsigned blksize;
1528 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1529 * fcp_rsp regions to the first data bde entry
1531 bpl += 2;
1532 if (scsi_sg_count(scsi_cmnd)) {
1534 * The driver stores the segment count returned from pci_map_sg
1535 * because this a count of dma-mappings used to map the use_sg
1536 * pages. They are not guaranteed to be the same for those
1537 * architectures that implement an IOMMU.
1539 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1540 scsi_sglist(scsi_cmnd),
1541 scsi_sg_count(scsi_cmnd), datadir);
1542 if (unlikely(!datasegcnt))
1543 return 1;
1545 lpfc_cmd->seg_cnt = datasegcnt;
1546 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1547 printk(KERN_ERR "%s: Too many sg segments from "
1548 "dma_map_sg. Config %d, seg_cnt %d\n",
1549 __func__, phba->cfg_sg_seg_cnt,
1550 lpfc_cmd->seg_cnt);
1551 scsi_dma_unmap(scsi_cmnd);
1552 return 1;
1555 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1557 switch (prot_group_type) {
1558 case LPFC_PG_TYPE_NO_DIF:
1559 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1560 datasegcnt);
1561 /* we shoud have 2 or more entries in buffer list */
1562 if (num_bde < 2)
1563 goto err;
1564 break;
1565 case LPFC_PG_TYPE_DIF_BUF:{
1567 * This type indicates that protection buffers are
1568 * passed to the driver, so that needs to be prepared
1569 * for DMA
1571 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1572 scsi_prot_sglist(scsi_cmnd),
1573 scsi_prot_sg_count(scsi_cmnd), datadir);
1574 if (unlikely(!protsegcnt)) {
1575 scsi_dma_unmap(scsi_cmnd);
1576 return 1;
1579 lpfc_cmd->prot_seg_cnt = protsegcnt;
1580 if (lpfc_cmd->prot_seg_cnt
1581 > phba->cfg_prot_sg_seg_cnt) {
1582 printk(KERN_ERR "%s: Too many prot sg segments "
1583 "from dma_map_sg. Config %d,"
1584 "prot_seg_cnt %d\n", __func__,
1585 phba->cfg_prot_sg_seg_cnt,
1586 lpfc_cmd->prot_seg_cnt);
1587 dma_unmap_sg(&phba->pcidev->dev,
1588 scsi_prot_sglist(scsi_cmnd),
1589 scsi_prot_sg_count(scsi_cmnd),
1590 datadir);
1591 scsi_dma_unmap(scsi_cmnd);
1592 return 1;
1595 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1596 datasegcnt, protsegcnt);
1597 /* we shoud have 3 or more entries in buffer list */
1598 if (num_bde < 3)
1599 goto err;
1600 break;
1602 case LPFC_PG_TYPE_INVALID:
1603 default:
1604 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1605 "9022 Unexpected protection group %i\n",
1606 prot_group_type);
1607 return 1;
1612 * Finish initializing those IOCB fields that are dependent on the
1613 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1614 * reinitialized since all iocb memory resources are used many times
1615 * for transmit, receive, and continuation bpl's.
1617 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1618 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1619 iocb_cmd->ulpBdeCount = 1;
1620 iocb_cmd->ulpLe = 1;
1622 fcpdl = scsi_bufflen(scsi_cmnd);
1624 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1626 * We are in DIF Type 1 mode
1627 * Every data block has a 8 byte DIF (trailer)
1628 * attached to it. Must ajust FCP data length
1630 blksize = lpfc_cmd_blksize(scsi_cmnd);
1631 diflen = (fcpdl / blksize) * 8;
1632 fcpdl += diflen;
1634 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1637 * Due to difference in data length between DIF/non-DIF paths,
1638 * we need to set word 4 of IOCB here
1640 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1642 return 0;
1643 err:
1644 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1645 "9023 Could not setup all needed BDE's"
1646 "prot_group_type=%d, num_bde=%d\n",
1647 prot_group_type, num_bde);
1648 return 1;
1652 * This function checks for BlockGuard errors detected by
1653 * the HBA. In case of errors, the ASC/ASCQ fields in the
1654 * sense buffer will be set accordingly, paired with
1655 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1656 * detected corruption.
1658 * Returns:
1659 * 0 - No error found
1660 * 1 - BlockGuard error found
1661 * -1 - Internal error (bad profile, ...etc)
1663 static int
1664 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1665 struct lpfc_iocbq *pIocbOut)
1667 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1668 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1669 int ret = 0;
1670 uint32_t bghm = bgf->bghm;
1671 uint32_t bgstat = bgf->bgstat;
1672 uint64_t failing_sector = 0;
1674 printk(KERN_ERR "BG ERROR in cmd 0x%x lba 0x%llx blk cnt 0x%x "
1675 "bgstat=0x%x bghm=0x%x\n",
1676 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
1677 blk_rq_sectors(cmd->request), bgstat, bghm);
1679 spin_lock(&_dump_buf_lock);
1680 if (!_dump_buf_done) {
1681 printk(KERN_ERR "Saving Data for %u blocks to debugfs\n",
1682 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1683 lpfc_debug_save_data(cmd);
1685 /* If we have a prot sgl, save the DIF buffer */
1686 if (lpfc_prot_group_type(phba, cmd) ==
1687 LPFC_PG_TYPE_DIF_BUF) {
1688 printk(KERN_ERR "Saving DIF for %u blocks to debugfs\n",
1689 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1690 lpfc_debug_save_dif(cmd);
1693 _dump_buf_done = 1;
1695 spin_unlock(&_dump_buf_lock);
1697 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1698 cmd->result = ScsiResult(DID_ERROR, 0);
1699 printk(KERN_ERR "Invalid BlockGuard profile. bgstat:0x%x\n",
1700 bgstat);
1701 ret = (-1);
1702 goto out;
1705 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1706 cmd->result = ScsiResult(DID_ERROR, 0);
1707 printk(KERN_ERR "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
1708 bgstat);
1709 ret = (-1);
1710 goto out;
1713 if (lpfc_bgs_get_guard_err(bgstat)) {
1714 ret = 1;
1716 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1717 0x10, 0x1);
1718 cmd->result = DRIVER_SENSE << 24
1719 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1720 phba->bg_guard_err_cnt++;
1721 printk(KERN_ERR "BLKGRD: guard_tag error\n");
1724 if (lpfc_bgs_get_reftag_err(bgstat)) {
1725 ret = 1;
1727 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1728 0x10, 0x3);
1729 cmd->result = DRIVER_SENSE << 24
1730 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1732 phba->bg_reftag_err_cnt++;
1733 printk(KERN_ERR "BLKGRD: ref_tag error\n");
1736 if (lpfc_bgs_get_apptag_err(bgstat)) {
1737 ret = 1;
1739 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1740 0x10, 0x2);
1741 cmd->result = DRIVER_SENSE << 24
1742 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1744 phba->bg_apptag_err_cnt++;
1745 printk(KERN_ERR "BLKGRD: app_tag error\n");
1748 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1750 * setup sense data descriptor 0 per SPC-4 as an information
1751 * field, and put the failing LBA in it
1753 cmd->sense_buffer[8] = 0; /* Information */
1754 cmd->sense_buffer[9] = 0xa; /* Add. length */
1755 bghm /= cmd->device->sector_size;
1757 failing_sector = scsi_get_lba(cmd);
1758 failing_sector += bghm;
1760 put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
1763 if (!ret) {
1764 /* No error was reported - problem in FW? */
1765 cmd->result = ScsiResult(DID_ERROR, 0);
1766 printk(KERN_ERR "BLKGRD: no errors reported!\n");
1769 out:
1770 return ret;
1774 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1775 * @phba: The Hba for which this call is being executed.
1776 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1778 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1779 * field of @lpfc_cmd for device with SLI-4 interface spec.
1781 * Return codes:
1782 * 1 - Error
1783 * 0 - Success
1785 static int
1786 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1788 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1789 struct scatterlist *sgel = NULL;
1790 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1791 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
1792 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1793 dma_addr_t physaddr;
1794 uint32_t num_bde = 0;
1795 uint32_t dma_len;
1796 uint32_t dma_offset = 0;
1797 int nseg;
1800 * There are three possibilities here - use scatter-gather segment, use
1801 * the single mapping, or neither. Start the lpfc command prep by
1802 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1803 * data bde entry.
1805 if (scsi_sg_count(scsi_cmnd)) {
1807 * The driver stores the segment count returned from pci_map_sg
1808 * because this a count of dma-mappings used to map the use_sg
1809 * pages. They are not guaranteed to be the same for those
1810 * architectures that implement an IOMMU.
1813 nseg = scsi_dma_map(scsi_cmnd);
1814 if (unlikely(!nseg))
1815 return 1;
1816 sgl += 1;
1817 /* clear the last flag in the fcp_rsp map entry */
1818 sgl->word2 = le32_to_cpu(sgl->word2);
1819 bf_set(lpfc_sli4_sge_last, sgl, 0);
1820 sgl->word2 = cpu_to_le32(sgl->word2);
1821 sgl += 1;
1823 lpfc_cmd->seg_cnt = nseg;
1824 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1825 printk(KERN_ERR "%s: Too many sg segments from "
1826 "dma_map_sg. Config %d, seg_cnt %d\n",
1827 __func__, phba->cfg_sg_seg_cnt,
1828 lpfc_cmd->seg_cnt);
1829 scsi_dma_unmap(scsi_cmnd);
1830 return 1;
1834 * The driver established a maximum scatter-gather segment count
1835 * during probe that limits the number of sg elements in any
1836 * single scsi command. Just run through the seg_cnt and format
1837 * the sge's.
1838 * When using SLI-3 the driver will try to fit all the BDEs into
1839 * the IOCB. If it can't then the BDEs get added to a BPL as it
1840 * does for SLI-2 mode.
1842 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1843 physaddr = sg_dma_address(sgel);
1844 dma_len = sg_dma_len(sgel);
1845 bf_set(lpfc_sli4_sge_len, sgl, sg_dma_len(sgel));
1846 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
1847 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
1848 if ((num_bde + 1) == nseg)
1849 bf_set(lpfc_sli4_sge_last, sgl, 1);
1850 else
1851 bf_set(lpfc_sli4_sge_last, sgl, 0);
1852 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
1853 sgl->word2 = cpu_to_le32(sgl->word2);
1854 sgl->word3 = cpu_to_le32(sgl->word3);
1855 dma_offset += dma_len;
1856 sgl++;
1858 } else {
1859 sgl += 1;
1860 /* clear the last flag in the fcp_rsp map entry */
1861 sgl->word2 = le32_to_cpu(sgl->word2);
1862 bf_set(lpfc_sli4_sge_last, sgl, 1);
1863 sgl->word2 = cpu_to_le32(sgl->word2);
1867 * Finish initializing those IOCB fields that are dependent on the
1868 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1869 * explicitly reinitialized.
1870 * all iocb memory resources are reused.
1872 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1875 * Due to difference in data length between DIF/non-DIF paths,
1876 * we need to set word 4 of IOCB here
1878 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1879 return 0;
1883 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
1884 * @phba: The Hba for which this call is being executed.
1885 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1887 * This routine wraps the actual DMA mapping function pointer from the
1888 * lpfc_hba struct.
1890 * Return codes:
1891 * 1 - Error
1892 * 0 - Success
1894 static inline int
1895 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1897 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1901 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
1902 * @phba: Pointer to hba context object.
1903 * @vport: Pointer to vport object.
1904 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
1905 * @rsp_iocb: Pointer to response iocb object which reported error.
1907 * This function posts an event when there is a SCSI command reporting
1908 * error from the scsi device.
1910 static void
1911 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
1912 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
1913 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
1914 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
1915 uint32_t resp_info = fcprsp->rspStatus2;
1916 uint32_t scsi_status = fcprsp->rspStatus3;
1917 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
1918 struct lpfc_fast_path_event *fast_path_evt = NULL;
1919 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
1920 unsigned long flags;
1922 /* If there is queuefull or busy condition send a scsi event */
1923 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
1924 (cmnd->result == SAM_STAT_BUSY)) {
1925 fast_path_evt = lpfc_alloc_fast_evt(phba);
1926 if (!fast_path_evt)
1927 return;
1928 fast_path_evt->un.scsi_evt.event_type =
1929 FC_REG_SCSI_EVENT;
1930 fast_path_evt->un.scsi_evt.subcategory =
1931 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
1932 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
1933 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
1934 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
1935 &pnode->nlp_portname, sizeof(struct lpfc_name));
1936 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
1937 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1938 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
1939 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
1940 fast_path_evt = lpfc_alloc_fast_evt(phba);
1941 if (!fast_path_evt)
1942 return;
1943 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
1944 FC_REG_SCSI_EVENT;
1945 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
1946 LPFC_EVENT_CHECK_COND;
1947 fast_path_evt->un.check_cond_evt.scsi_event.lun =
1948 cmnd->device->lun;
1949 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
1950 &pnode->nlp_portname, sizeof(struct lpfc_name));
1951 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
1952 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1953 fast_path_evt->un.check_cond_evt.sense_key =
1954 cmnd->sense_buffer[2] & 0xf;
1955 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
1956 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
1957 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
1958 fcpi_parm &&
1959 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
1960 ((scsi_status == SAM_STAT_GOOD) &&
1961 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
1963 * If status is good or resid does not match with fcp_param and
1964 * there is valid fcpi_parm, then there is a read_check error
1966 fast_path_evt = lpfc_alloc_fast_evt(phba);
1967 if (!fast_path_evt)
1968 return;
1969 fast_path_evt->un.read_check_error.header.event_type =
1970 FC_REG_FABRIC_EVENT;
1971 fast_path_evt->un.read_check_error.header.subcategory =
1972 LPFC_EVENT_FCPRDCHKERR;
1973 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
1974 &pnode->nlp_portname, sizeof(struct lpfc_name));
1975 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
1976 &pnode->nlp_nodename, sizeof(struct lpfc_name));
1977 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
1978 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
1979 fast_path_evt->un.read_check_error.fcpiparam =
1980 fcpi_parm;
1981 } else
1982 return;
1984 fast_path_evt->vport = vport;
1985 spin_lock_irqsave(&phba->hbalock, flags);
1986 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
1987 spin_unlock_irqrestore(&phba->hbalock, flags);
1988 lpfc_worker_wake_up(phba);
1989 return;
1993 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
1994 * @phba: The HBA for which this call is being executed.
1995 * @psb: The scsi buffer which is going to be un-mapped.
1997 * This routine does DMA un-mapping of scatter gather list of scsi command
1998 * field of @lpfc_cmd for device with SLI-3 interface spec.
2000 static void
2001 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
2004 * There are only two special cases to consider. (1) the scsi command
2005 * requested scatter-gather usage or (2) the scsi command allocated
2006 * a request buffer, but did not request use_sg. There is a third
2007 * case, but it does not require resource deallocation.
2009 if (psb->seg_cnt > 0)
2010 scsi_dma_unmap(psb->pCmd);
2011 if (psb->prot_seg_cnt > 0)
2012 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2013 scsi_prot_sg_count(psb->pCmd),
2014 psb->pCmd->sc_data_direction);
2018 * lpfc_handler_fcp_err - FCP response handler
2019 * @vport: The virtual port for which this call is being executed.
2020 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2021 * @rsp_iocb: The response IOCB which contains FCP error.
2023 * This routine is called to process response IOCB with status field
2024 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2025 * based upon SCSI and FCP error.
2027 static void
2028 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2029 struct lpfc_iocbq *rsp_iocb)
2031 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2032 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2033 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2034 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2035 uint32_t resp_info = fcprsp->rspStatus2;
2036 uint32_t scsi_status = fcprsp->rspStatus3;
2037 uint32_t *lp;
2038 uint32_t host_status = DID_OK;
2039 uint32_t rsplen = 0;
2040 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
2044 * If this is a task management command, there is no
2045 * scsi packet associated with this lpfc_cmd. The driver
2046 * consumes it.
2048 if (fcpcmd->fcpCntl2) {
2049 scsi_status = 0;
2050 goto out;
2053 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2054 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2055 if (snslen > SCSI_SENSE_BUFFERSIZE)
2056 snslen = SCSI_SENSE_BUFFERSIZE;
2058 if (resp_info & RSP_LEN_VALID)
2059 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2060 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2062 lp = (uint32_t *)cmnd->sense_buffer;
2064 if (!scsi_status && (resp_info & RESID_UNDER))
2065 logit = LOG_FCP;
2067 lpfc_printf_vlog(vport, KERN_WARNING, logit,
2068 "9024 FCP command x%x failed: x%x SNS x%x x%x "
2069 "Data: x%x x%x x%x x%x x%x\n",
2070 cmnd->cmnd[0], scsi_status,
2071 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2072 be32_to_cpu(fcprsp->rspResId),
2073 be32_to_cpu(fcprsp->rspSnsLen),
2074 be32_to_cpu(fcprsp->rspRspLen),
2075 fcprsp->rspInfo3);
2077 if (resp_info & RSP_LEN_VALID) {
2078 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2079 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
2080 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
2081 host_status = DID_ERROR;
2082 goto out;
2086 scsi_set_resid(cmnd, 0);
2087 if (resp_info & RESID_UNDER) {
2088 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
2090 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2091 "9025 FCP Read Underrun, expected %d, "
2092 "residual %d Data: x%x x%x x%x\n",
2093 be32_to_cpu(fcpcmd->fcpDl),
2094 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2095 cmnd->underflow);
2098 * If there is an under run check if under run reported by
2099 * storage array is same as the under run reported by HBA.
2100 * If this is not same, there is a dropped frame.
2102 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2103 fcpi_parm &&
2104 (scsi_get_resid(cmnd) != fcpi_parm)) {
2105 lpfc_printf_vlog(vport, KERN_WARNING,
2106 LOG_FCP | LOG_FCP_ERROR,
2107 "9026 FCP Read Check Error "
2108 "and Underrun Data: x%x x%x x%x x%x\n",
2109 be32_to_cpu(fcpcmd->fcpDl),
2110 scsi_get_resid(cmnd), fcpi_parm,
2111 cmnd->cmnd[0]);
2112 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
2113 host_status = DID_ERROR;
2116 * The cmnd->underflow is the minimum number of bytes that must
2117 * be transfered for this command. Provided a sense condition
2118 * is not present, make sure the actual amount transferred is at
2119 * least the underflow value or fail.
2121 if (!(resp_info & SNS_LEN_VALID) &&
2122 (scsi_status == SAM_STAT_GOOD) &&
2123 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2124 < cmnd->underflow)) {
2125 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2126 "9027 FCP command x%x residual "
2127 "underrun converted to error "
2128 "Data: x%x x%x x%x\n",
2129 cmnd->cmnd[0], scsi_bufflen(cmnd),
2130 scsi_get_resid(cmnd), cmnd->underflow);
2131 host_status = DID_ERROR;
2133 } else if (resp_info & RESID_OVER) {
2134 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2135 "9028 FCP command x%x residual overrun error. "
2136 "Data: x%x x%x\n", cmnd->cmnd[0],
2137 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
2138 host_status = DID_ERROR;
2141 * Check SLI validation that all the transfer was actually done
2142 * (fcpi_parm should be zero). Apply check only to reads.
2144 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
2145 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
2146 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
2147 "9029 FCP Read Check Error Data: "
2148 "x%x x%x x%x x%x\n",
2149 be32_to_cpu(fcpcmd->fcpDl),
2150 be32_to_cpu(fcprsp->rspResId),
2151 fcpi_parm, cmnd->cmnd[0]);
2152 host_status = DID_ERROR;
2153 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
2156 out:
2157 cmnd->result = ScsiResult(host_status, scsi_status);
2158 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
2162 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
2163 * @phba: The Hba for which this call is being executed.
2164 * @pIocbIn: The command IOCBQ for the scsi cmnd.
2165 * @pIocbOut: The response IOCBQ for the scsi cmnd.
2167 * This routine assigns scsi command result by looking into response IOCB
2168 * status field appropriately. This routine handles QUEUE FULL condition as
2169 * well by ramping down device queue depth.
2171 static void
2172 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2173 struct lpfc_iocbq *pIocbOut)
2175 struct lpfc_scsi_buf *lpfc_cmd =
2176 (struct lpfc_scsi_buf *) pIocbIn->context1;
2177 struct lpfc_vport *vport = pIocbIn->vport;
2178 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2179 struct lpfc_nodelist *pnode = rdata->pnode;
2180 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2181 int result;
2182 struct scsi_device *tmp_sdev;
2183 int depth = 0;
2184 unsigned long flags;
2185 struct lpfc_fast_path_event *fast_path_evt;
2186 struct Scsi_Host *shost = cmd->device->host;
2187 uint32_t queue_depth, scsi_id;
2189 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2190 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
2191 if (pnode && NLP_CHK_NODE_ACT(pnode))
2192 atomic_dec(&pnode->cmd_pending);
2194 if (lpfc_cmd->status) {
2195 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2196 (lpfc_cmd->result & IOERR_DRVR_MASK))
2197 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2198 else if (lpfc_cmd->status >= IOSTAT_CNT)
2199 lpfc_cmd->status = IOSTAT_DEFAULT;
2201 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2202 "9030 FCP cmd x%x failed <%d/%d> "
2203 "status: x%x result: x%x Data: x%x x%x\n",
2204 cmd->cmnd[0],
2205 cmd->device ? cmd->device->id : 0xffff,
2206 cmd->device ? cmd->device->lun : 0xffff,
2207 lpfc_cmd->status, lpfc_cmd->result,
2208 pIocbOut->iocb.ulpContext,
2209 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
2211 switch (lpfc_cmd->status) {
2212 case IOSTAT_FCP_RSP_ERROR:
2213 /* Call FCP RSP handler to determine result */
2214 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
2215 break;
2216 case IOSTAT_NPORT_BSY:
2217 case IOSTAT_FABRIC_BSY:
2218 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2219 fast_path_evt = lpfc_alloc_fast_evt(phba);
2220 if (!fast_path_evt)
2221 break;
2222 fast_path_evt->un.fabric_evt.event_type =
2223 FC_REG_FABRIC_EVENT;
2224 fast_path_evt->un.fabric_evt.subcategory =
2225 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2226 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2227 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2228 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2229 &pnode->nlp_portname,
2230 sizeof(struct lpfc_name));
2231 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2232 &pnode->nlp_nodename,
2233 sizeof(struct lpfc_name));
2235 fast_path_evt->vport = vport;
2236 fast_path_evt->work_evt.evt =
2237 LPFC_EVT_FASTPATH_MGMT_EVT;
2238 spin_lock_irqsave(&phba->hbalock, flags);
2239 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2240 &phba->work_list);
2241 spin_unlock_irqrestore(&phba->hbalock, flags);
2242 lpfc_worker_wake_up(phba);
2243 break;
2244 case IOSTAT_LOCAL_REJECT:
2245 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
2246 lpfc_cmd->result == IOERR_NO_RESOURCES ||
2247 lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
2248 cmd->result = ScsiResult(DID_REQUEUE, 0);
2249 break;
2252 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2253 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2254 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2255 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2257 * This is a response for a BG enabled
2258 * cmd. Parse BG error
2260 lpfc_parse_bg_err(phba, lpfc_cmd,
2261 pIocbOut);
2262 break;
2263 } else {
2264 lpfc_printf_vlog(vport, KERN_WARNING,
2265 LOG_BG,
2266 "9031 non-zero BGSTAT "
2267 "on unprotected cmd");
2271 /* else: fall through */
2272 default:
2273 cmd->result = ScsiResult(DID_ERROR, 0);
2274 break;
2277 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
2278 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
2279 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2280 SAM_STAT_BUSY);
2281 } else {
2282 cmd->result = ScsiResult(DID_OK, 0);
2285 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2286 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2288 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2289 "0710 Iodone <%d/%d> cmd %p, error "
2290 "x%x SNS x%x x%x Data: x%x x%x\n",
2291 cmd->device->id, cmd->device->lun, cmd,
2292 cmd->result, *lp, *(lp + 3), cmd->retries,
2293 scsi_get_resid(cmd));
2296 lpfc_update_stats(phba, lpfc_cmd);
2297 result = cmd->result;
2298 if (vport->cfg_max_scsicmpl_time &&
2299 time_after(jiffies, lpfc_cmd->start_time +
2300 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
2301 spin_lock_irqsave(shost->host_lock, flags);
2302 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2303 if (pnode->cmd_qdepth >
2304 atomic_read(&pnode->cmd_pending) &&
2305 (atomic_read(&pnode->cmd_pending) >
2306 LPFC_MIN_TGT_QDEPTH) &&
2307 ((cmd->cmnd[0] == READ_10) ||
2308 (cmd->cmnd[0] == WRITE_10)))
2309 pnode->cmd_qdepth =
2310 atomic_read(&pnode->cmd_pending);
2312 pnode->last_change_time = jiffies;
2314 spin_unlock_irqrestore(shost->host_lock, flags);
2315 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2316 if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
2317 time_after(jiffies, pnode->last_change_time +
2318 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
2319 spin_lock_irqsave(shost->host_lock, flags);
2320 pnode->cmd_qdepth += pnode->cmd_qdepth *
2321 LPFC_TGTQ_RAMPUP_PCENT / 100;
2322 if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
2323 pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
2324 pnode->last_change_time = jiffies;
2325 spin_unlock_irqrestore(shost->host_lock, flags);
2329 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
2331 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2332 queue_depth = cmd->device->queue_depth;
2333 scsi_id = cmd->device->id;
2334 cmd->scsi_done(cmd);
2336 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2338 * If there is a thread waiting for command completion
2339 * wake up the thread.
2341 spin_lock_irqsave(shost->host_lock, flags);
2342 lpfc_cmd->pCmd = NULL;
2343 if (lpfc_cmd->waitq)
2344 wake_up(lpfc_cmd->waitq);
2345 spin_unlock_irqrestore(shost->host_lock, flags);
2346 lpfc_release_scsi_buf(phba, lpfc_cmd);
2347 return;
2351 if (!result)
2352 lpfc_rampup_queue_depth(vport, queue_depth);
2354 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
2355 ((jiffies - pnode->last_ramp_up_time) >
2356 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
2357 ((jiffies - pnode->last_q_full_time) >
2358 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
2359 (vport->cfg_lun_queue_depth > queue_depth)) {
2360 shost_for_each_device(tmp_sdev, shost) {
2361 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
2362 if (tmp_sdev->id != scsi_id)
2363 continue;
2364 if (tmp_sdev->ordered_tags)
2365 scsi_adjust_queue_depth(tmp_sdev,
2366 MSG_ORDERED_TAG,
2367 tmp_sdev->queue_depth+1);
2368 else
2369 scsi_adjust_queue_depth(tmp_sdev,
2370 MSG_SIMPLE_TAG,
2371 tmp_sdev->queue_depth+1);
2373 pnode->last_ramp_up_time = jiffies;
2376 lpfc_send_sdev_queuedepth_change_event(phba, vport, pnode,
2377 0xFFFFFFFF,
2378 queue_depth , queue_depth + 1);
2382 * Check for queue full. If the lun is reporting queue full, then
2383 * back off the lun queue depth to prevent target overloads.
2385 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2386 NLP_CHK_NODE_ACT(pnode)) {
2387 pnode->last_q_full_time = jiffies;
2389 shost_for_each_device(tmp_sdev, shost) {
2390 if (tmp_sdev->id != scsi_id)
2391 continue;
2392 depth = scsi_track_queue_full(tmp_sdev,
2393 tmp_sdev->queue_depth - 1);
2396 * The queue depth cannot be lowered any more.
2397 * Modify the returned error code to store
2398 * the final depth value set by
2399 * scsi_track_queue_full.
2401 if (depth == -1)
2402 depth = shost->cmd_per_lun;
2404 if (depth) {
2405 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2406 "0711 detected queue full - lun queue "
2407 "depth adjusted to %d.\n", depth);
2408 lpfc_send_sdev_queuedepth_change_event(phba, vport,
2409 pnode, 0xFFFFFFFF,
2410 depth+1, depth);
2415 * If there is a thread waiting for command completion
2416 * wake up the thread.
2418 spin_lock_irqsave(shost->host_lock, flags);
2419 lpfc_cmd->pCmd = NULL;
2420 if (lpfc_cmd->waitq)
2421 wake_up(lpfc_cmd->waitq);
2422 spin_unlock_irqrestore(shost->host_lock, flags);
2424 lpfc_release_scsi_buf(phba, lpfc_cmd);
2428 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
2429 * @data: A pointer to the immediate command data portion of the IOCB.
2430 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2432 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2433 * byte swapping the data to big endian format for transmission on the wire.
2435 static void
2436 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2438 int i, j;
2439 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2440 i += sizeof(uint32_t), j++) {
2441 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2446 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
2447 * @vport: The virtual port for which this call is being executed.
2448 * @lpfc_cmd: The scsi command which needs to send.
2449 * @pnode: Pointer to lpfc_nodelist.
2451 * This routine initializes fcp_cmnd and iocb data structure from scsi command
2452 * to transfer for device with SLI3 interface spec.
2454 static void
2455 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2456 struct lpfc_nodelist *pnode)
2458 struct lpfc_hba *phba = vport->phba;
2459 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2460 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2461 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2462 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2463 int datadir = scsi_cmnd->sc_data_direction;
2464 char tag[2];
2466 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2467 return;
2469 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
2470 /* clear task management bits */
2471 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
2473 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2474 &lpfc_cmd->fcp_cmnd->fcp_lun);
2476 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2478 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2479 switch (tag[0]) {
2480 case HEAD_OF_QUEUE_TAG:
2481 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2482 break;
2483 case ORDERED_QUEUE_TAG:
2484 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2485 break;
2486 default:
2487 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2488 break;
2490 } else
2491 fcp_cmnd->fcpCntl1 = 0;
2494 * There are three possibilities here - use scatter-gather segment, use
2495 * the single mapping, or neither. Start the lpfc command prep by
2496 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2497 * data bde entry.
2499 if (scsi_sg_count(scsi_cmnd)) {
2500 if (datadir == DMA_TO_DEVICE) {
2501 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
2502 if (phba->sli_rev < LPFC_SLI_REV4) {
2503 iocb_cmd->un.fcpi.fcpi_parm = 0;
2504 iocb_cmd->ulpPU = 0;
2505 } else
2506 iocb_cmd->ulpPU = PARM_READ_CHECK;
2507 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2508 phba->fc4OutputRequests++;
2509 } else {
2510 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2511 iocb_cmd->ulpPU = PARM_READ_CHECK;
2512 fcp_cmnd->fcpCntl3 = READ_DATA;
2513 phba->fc4InputRequests++;
2515 } else {
2516 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2517 iocb_cmd->un.fcpi.fcpi_parm = 0;
2518 iocb_cmd->ulpPU = 0;
2519 fcp_cmnd->fcpCntl3 = 0;
2520 phba->fc4ControlRequests++;
2522 if (phba->sli_rev == 3 &&
2523 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
2524 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
2526 * Finish initializing those IOCB fields that are independent
2527 * of the scsi_cmnd request_buffer
2529 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2530 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2531 piocbq->iocb.ulpFCP2Rcvy = 1;
2532 else
2533 piocbq->iocb.ulpFCP2Rcvy = 0;
2535 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2536 piocbq->context1 = lpfc_cmd;
2537 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2538 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2539 piocbq->vport = vport;
2543 * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
2544 * @vport: The virtual port for which this call is being executed.
2545 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2546 * @lun: Logical unit number.
2547 * @task_mgmt_cmd: SCSI task management command.
2549 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2550 * for device with SLI-3 interface spec.
2552 * Return codes:
2553 * 0 - Error
2554 * 1 - Success
2556 static int
2557 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
2558 struct lpfc_scsi_buf *lpfc_cmd,
2559 unsigned int lun,
2560 uint8_t task_mgmt_cmd)
2562 struct lpfc_iocbq *piocbq;
2563 IOCB_t *piocb;
2564 struct fcp_cmnd *fcp_cmnd;
2565 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2566 struct lpfc_nodelist *ndlp = rdata->pnode;
2568 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2569 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
2570 return 0;
2572 piocbq = &(lpfc_cmd->cur_iocbq);
2573 piocbq->vport = vport;
2575 piocb = &piocbq->iocb;
2577 fcp_cmnd = lpfc_cmd->fcp_cmnd;
2578 /* Clear out any old data in the FCP command area */
2579 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2580 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
2581 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
2582 if (vport->phba->sli_rev == 3 &&
2583 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
2584 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
2585 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
2586 piocb->ulpContext = ndlp->nlp_rpi;
2587 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2588 piocb->ulpFCP2Rcvy = 1;
2590 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2592 /* ulpTimeout is only one byte */
2593 if (lpfc_cmd->timeout > 0xff) {
2595 * Do not timeout the command at the firmware level.
2596 * The driver will provide the timeout mechanism.
2598 piocb->ulpTimeout = 0;
2599 } else
2600 piocb->ulpTimeout = lpfc_cmd->timeout;
2602 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2603 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
2605 return 1;
2609 * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
2610 * @phba: The hba struct for which this call is being executed.
2611 * @dev_grp: The HBA PCI-Device group number.
2613 * This routine sets up the SCSI interface API function jump table in @phba
2614 * struct.
2615 * Returns: 0 - success, -ENODEV - failure.
2618 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2621 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2622 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2623 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2625 switch (dev_grp) {
2626 case LPFC_PCI_DEV_LP:
2627 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2628 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
2629 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2630 break;
2631 case LPFC_PCI_DEV_OC:
2632 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2633 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
2634 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2635 break;
2636 default:
2637 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2638 "1418 Invalid HBA PCI-device group: 0x%x\n",
2639 dev_grp);
2640 return -ENODEV;
2641 break;
2643 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
2644 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
2645 return 0;
2649 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
2650 * @phba: The Hba for which this call is being executed.
2651 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2652 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2654 * This routine is IOCB completion routine for device reset and target reset
2655 * routine. This routine release scsi buffer associated with lpfc_cmd.
2657 static void
2658 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2659 struct lpfc_iocbq *cmdiocbq,
2660 struct lpfc_iocbq *rspiocbq)
2662 struct lpfc_scsi_buf *lpfc_cmd =
2663 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2664 if (lpfc_cmd)
2665 lpfc_release_scsi_buf(phba, lpfc_cmd);
2666 return;
2670 * lpfc_info - Info entry point of scsi_host_template data structure
2671 * @host: The scsi host for which this call is being executed.
2673 * This routine provides module information about hba.
2675 * Reutrn code:
2676 * Pointer to char - Success.
2678 const char *
2679 lpfc_info(struct Scsi_Host *host)
2681 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2682 struct lpfc_hba *phba = vport->phba;
2683 int len;
2684 static char lpfcinfobuf[384];
2686 memset(lpfcinfobuf,0,384);
2687 if (phba && phba->pcidev){
2688 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2689 len = strlen(lpfcinfobuf);
2690 snprintf(lpfcinfobuf + len,
2691 384-len,
2692 " on PCI bus %02x device %02x irq %d",
2693 phba->pcidev->bus->number,
2694 phba->pcidev->devfn,
2695 phba->pcidev->irq);
2696 len = strlen(lpfcinfobuf);
2697 if (phba->Port[0]) {
2698 snprintf(lpfcinfobuf + len,
2699 384-len,
2700 " port %s",
2701 phba->Port);
2704 return lpfcinfobuf;
2708 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
2709 * @phba: The Hba for which this call is being executed.
2711 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2712 * The default value of cfg_poll_tmo is 10 milliseconds.
2714 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2716 unsigned long poll_tmo_expires =
2717 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2719 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2720 mod_timer(&phba->fcp_poll_timer,
2721 poll_tmo_expires);
2725 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
2726 * @phba: The Hba for which this call is being executed.
2728 * This routine starts the fcp_poll_timer of @phba.
2730 void lpfc_poll_start_timer(struct lpfc_hba * phba)
2732 lpfc_poll_rearm_timer(phba);
2736 * lpfc_poll_timeout - Restart polling timer
2737 * @ptr: Map to lpfc_hba data structure pointer.
2739 * This routine restarts fcp_poll timer, when FCP ring polling is enable
2740 * and FCP Ring interrupt is disable.
2743 void lpfc_poll_timeout(unsigned long ptr)
2745 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
2747 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2748 lpfc_sli_poll_fcp_ring (phba);
2749 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2750 lpfc_poll_rearm_timer(phba);
2755 * lpfc_queuecommand - scsi_host_template queuecommand entry point
2756 * @cmnd: Pointer to scsi_cmnd data structure.
2757 * @done: Pointer to done routine.
2759 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
2760 * This routine prepares an IOCB from scsi command and provides to firmware.
2761 * The @done callback is invoked after driver finished processing the command.
2763 * Return value :
2764 * 0 - Success
2765 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
2767 static int
2768 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
2770 struct Scsi_Host *shost = cmnd->device->host;
2771 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2772 struct lpfc_hba *phba = vport->phba;
2773 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
2774 struct lpfc_nodelist *ndlp = rdata->pnode;
2775 struct lpfc_scsi_buf *lpfc_cmd;
2776 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
2777 int err;
2779 err = fc_remote_port_chkready(rport);
2780 if (err) {
2781 cmnd->result = err;
2782 goto out_fail_command;
2785 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
2786 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2788 printk(KERN_ERR "BLKGRD ERROR: rcvd protected cmd:%02x op:%02x "
2789 "str=%s without registering for BlockGuard - "
2790 "Rejecting command\n",
2791 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2792 dif_op_str[scsi_get_prot_op(cmnd)]);
2793 goto out_fail_command;
2797 * Catch race where our node has transitioned, but the
2798 * transport is still transitioning.
2800 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
2801 cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2802 goto out_fail_command;
2804 if (vport->cfg_max_scsicmpl_time &&
2805 (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
2806 goto out_host_busy;
2808 lpfc_cmd = lpfc_get_scsi_buf(phba);
2809 if (lpfc_cmd == NULL) {
2810 lpfc_rampdown_queue_depth(phba);
2812 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2813 "0707 driver's buffer pool is empty, "
2814 "IO busied\n");
2815 goto out_host_busy;
2819 * Store the midlayer's command structure for the completion phase
2820 * and complete the command initialization.
2822 lpfc_cmd->pCmd = cmnd;
2823 lpfc_cmd->rdata = rdata;
2824 lpfc_cmd->timeout = 0;
2825 lpfc_cmd->start_time = jiffies;
2826 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
2827 cmnd->scsi_done = done;
2829 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
2830 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2831 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
2832 "str=%s\n",
2833 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2834 dif_op_str[scsi_get_prot_op(cmnd)]);
2835 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2836 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
2837 "%02x %02x %02x %02x %02x\n",
2838 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2839 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2840 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2841 cmnd->cmnd[9]);
2842 if (cmnd->cmnd[0] == READ_10)
2843 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2844 "9035 BLKGRD: READ @ sector %llu, "
2845 "count %u\n",
2846 (unsigned long long)scsi_get_lba(cmnd),
2847 blk_rq_sectors(cmnd->request));
2848 else if (cmnd->cmnd[0] == WRITE_10)
2849 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2850 "9036 BLKGRD: WRITE @ sector %llu, "
2851 "count %u cmd=%p\n",
2852 (unsigned long long)scsi_get_lba(cmnd),
2853 blk_rq_sectors(cmnd->request),
2854 cmnd);
2856 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
2857 } else {
2858 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2859 "9038 BLKGRD: rcvd unprotected cmd:%02x op:%02x"
2860 " str=%s\n",
2861 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
2862 dif_op_str[scsi_get_prot_op(cmnd)]);
2863 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2864 "9039 BLKGRD: CDB: %02x %02x %02x %02x %02x "
2865 "%02x %02x %02x %02x %02x\n",
2866 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
2867 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
2868 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
2869 cmnd->cmnd[9]);
2870 if (cmnd->cmnd[0] == READ_10)
2871 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2872 "9040 dbg: READ @ sector %llu, "
2873 "count %u\n",
2874 (unsigned long long)scsi_get_lba(cmnd),
2875 blk_rq_sectors(cmnd->request));
2876 else if (cmnd->cmnd[0] == WRITE_10)
2877 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2878 "9041 dbg: WRITE @ sector %llu, "
2879 "count %u cmd=%p\n",
2880 (unsigned long long)scsi_get_lba(cmnd),
2881 blk_rq_sectors(cmnd->request), cmnd);
2882 else
2883 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
2884 "9042 dbg: parser not implemented\n");
2885 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2888 if (err)
2889 goto out_host_busy_free_buf;
2891 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
2893 atomic_inc(&ndlp->cmd_pending);
2894 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
2895 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
2896 if (err) {
2897 atomic_dec(&ndlp->cmd_pending);
2898 goto out_host_busy_free_buf;
2900 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2901 lpfc_sli_poll_fcp_ring(phba);
2902 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
2903 lpfc_poll_rearm_timer(phba);
2906 return 0;
2908 out_host_busy_free_buf:
2909 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
2910 lpfc_release_scsi_buf(phba, lpfc_cmd);
2911 out_host_busy:
2912 return SCSI_MLQUEUE_HOST_BUSY;
2914 out_fail_command:
2915 done(cmnd);
2916 return 0;
2920 * lpfc_block_error_handler - Routine to block error handler
2921 * @cmnd: Pointer to scsi_cmnd data structure.
2923 * This routine blocks execution till fc_rport state is not FC_PORSTAT_BLCOEKD.
2925 static void
2926 lpfc_block_error_handler(struct scsi_cmnd *cmnd)
2928 struct Scsi_Host *shost = cmnd->device->host;
2929 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
2931 spin_lock_irq(shost->host_lock);
2932 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
2933 spin_unlock_irq(shost->host_lock);
2934 msleep(1000);
2935 spin_lock_irq(shost->host_lock);
2937 spin_unlock_irq(shost->host_lock);
2938 return;
2942 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
2943 * @cmnd: Pointer to scsi_cmnd data structure.
2945 * This routine aborts @cmnd pending in base driver.
2947 * Return code :
2948 * 0x2003 - Error
2949 * 0x2002 - Success
2951 static int
2952 lpfc_abort_handler(struct scsi_cmnd *cmnd)
2954 struct Scsi_Host *shost = cmnd->device->host;
2955 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
2956 struct lpfc_hba *phba = vport->phba;
2957 struct lpfc_iocbq *iocb;
2958 struct lpfc_iocbq *abtsiocb;
2959 struct lpfc_scsi_buf *lpfc_cmd;
2960 IOCB_t *cmd, *icmd;
2961 int ret = SUCCESS;
2962 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
2964 lpfc_block_error_handler(cmnd);
2965 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
2966 BUG_ON(!lpfc_cmd);
2969 * If pCmd field of the corresponding lpfc_scsi_buf structure
2970 * points to a different SCSI command, then the driver has
2971 * already completed this command, but the midlayer did not
2972 * see the completion before the eh fired. Just return
2973 * SUCCESS.
2975 iocb = &lpfc_cmd->cur_iocbq;
2976 if (lpfc_cmd->pCmd != cmnd)
2977 goto out;
2979 BUG_ON(iocb->context1 != lpfc_cmd);
2981 abtsiocb = lpfc_sli_get_iocbq(phba);
2982 if (abtsiocb == NULL) {
2983 ret = FAILED;
2984 goto out;
2988 * The scsi command can not be in txq and it is in flight because the
2989 * pCmd is still pointig at the SCSI command we have to abort. There
2990 * is no need to search the txcmplq. Just send an abort to the FW.
2993 cmd = &iocb->iocb;
2994 icmd = &abtsiocb->iocb;
2995 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
2996 icmd->un.acxri.abortContextTag = cmd->ulpContext;
2997 if (phba->sli_rev == LPFC_SLI_REV4)
2998 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
2999 else
3000 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
3002 icmd->ulpLe = 1;
3003 icmd->ulpClass = cmd->ulpClass;
3004 if (lpfc_is_link_up(phba))
3005 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3006 else
3007 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
3009 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
3010 abtsiocb->vport = vport;
3011 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3012 IOCB_ERROR) {
3013 lpfc_sli_release_iocbq(phba, abtsiocb);
3014 ret = FAILED;
3015 goto out;
3018 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3019 lpfc_sli_poll_fcp_ring (phba);
3021 lpfc_cmd->waitq = &waitq;
3022 /* Wait for abort to complete */
3023 wait_event_timeout(waitq,
3024 (lpfc_cmd->pCmd != cmnd),
3025 (2*vport->cfg_devloss_tmo*HZ));
3027 spin_lock_irq(shost->host_lock);
3028 lpfc_cmd->waitq = NULL;
3029 spin_unlock_irq(shost->host_lock);
3031 if (lpfc_cmd->pCmd == cmnd) {
3032 ret = FAILED;
3033 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3034 "0748 abort handler timed out waiting "
3035 "for abort to complete: ret %#x, ID %d, "
3036 "LUN %d, snum %#lx\n",
3037 ret, cmnd->device->id, cmnd->device->lun,
3038 cmnd->serial_number);
3041 out:
3042 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3043 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3044 "LUN %d snum %#lx\n", ret, cmnd->device->id,
3045 cmnd->device->lun, cmnd->serial_number);
3046 return ret;
3049 static char *
3050 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3052 switch (task_mgmt_cmd) {
3053 case FCP_ABORT_TASK_SET:
3054 return "ABORT_TASK_SET";
3055 case FCP_CLEAR_TASK_SET:
3056 return "FCP_CLEAR_TASK_SET";
3057 case FCP_BUS_RESET:
3058 return "FCP_BUS_RESET";
3059 case FCP_LUN_RESET:
3060 return "FCP_LUN_RESET";
3061 case FCP_TARGET_RESET:
3062 return "FCP_TARGET_RESET";
3063 case FCP_CLEAR_ACA:
3064 return "FCP_CLEAR_ACA";
3065 case FCP_TERMINATE_TASK:
3066 return "FCP_TERMINATE_TASK";
3067 default:
3068 return "unknown";
3073 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3074 * @vport: The virtual port for which this call is being executed.
3075 * @rdata: Pointer to remote port local data
3076 * @tgt_id: Target ID of remote device.
3077 * @lun_id: Lun number for the TMF
3078 * @task_mgmt_cmd: type of TMF to send
3080 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3081 * a remote port.
3083 * Return Code:
3084 * 0x2003 - Error
3085 * 0x2002 - Success.
3087 static int
3088 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3089 unsigned tgt_id, unsigned int lun_id,
3090 uint8_t task_mgmt_cmd)
3092 struct lpfc_hba *phba = vport->phba;
3093 struct lpfc_scsi_buf *lpfc_cmd;
3094 struct lpfc_iocbq *iocbq;
3095 struct lpfc_iocbq *iocbqrsp;
3096 int ret;
3097 int status;
3099 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
3100 return FAILED;
3102 lpfc_cmd = lpfc_get_scsi_buf(phba);
3103 if (lpfc_cmd == NULL)
3104 return FAILED;
3105 lpfc_cmd->timeout = 60;
3106 lpfc_cmd->rdata = rdata;
3108 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3109 task_mgmt_cmd);
3110 if (!status) {
3111 lpfc_release_scsi_buf(phba, lpfc_cmd);
3112 return FAILED;
3115 iocbq = &lpfc_cmd->cur_iocbq;
3116 iocbqrsp = lpfc_sli_get_iocbq(phba);
3117 if (iocbqrsp == NULL) {
3118 lpfc_release_scsi_buf(phba, lpfc_cmd);
3119 return FAILED;
3122 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3123 "0702 Issue %s to TGT %d LUN %d "
3124 "rpi x%x nlp_flag x%x\n",
3125 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3126 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
3128 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3129 iocbq, iocbqrsp, lpfc_cmd->timeout);
3130 if (status != IOCB_SUCCESS) {
3131 if (status == IOCB_TIMEDOUT) {
3132 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3133 ret = TIMEOUT_ERROR;
3134 } else
3135 ret = FAILED;
3136 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3137 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3138 "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
3139 lpfc_taskmgmt_name(task_mgmt_cmd),
3140 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3141 iocbqrsp->iocb.un.ulpWord[4]);
3142 } else
3143 ret = SUCCESS;
3145 lpfc_sli_release_iocbq(phba, iocbqrsp);
3147 if (ret != TIMEOUT_ERROR)
3148 lpfc_release_scsi_buf(phba, lpfc_cmd);
3150 return ret;
3154 * lpfc_chk_tgt_mapped -
3155 * @vport: The virtual port to check on
3156 * @cmnd: Pointer to scsi_cmnd data structure.
3158 * This routine delays until the scsi target (aka rport) for the
3159 * command exists (is present and logged in) or we declare it non-existent.
3161 * Return code :
3162 * 0x2003 - Error
3163 * 0x2002 - Success
3165 static int
3166 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3168 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3169 struct lpfc_nodelist *pnode = rdata->pnode;
3170 unsigned long later;
3173 * If target is not in a MAPPED state, delay until
3174 * target is rediscovered or devloss timeout expires.
3176 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3177 while (time_after(later, jiffies)) {
3178 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3179 return FAILED;
3180 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3181 return SUCCESS;
3182 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3183 rdata = cmnd->device->hostdata;
3184 if (!rdata)
3185 return FAILED;
3186 pnode = rdata->pnode;
3188 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3189 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3190 return FAILED;
3191 return SUCCESS;
3195 * lpfc_reset_flush_io_context -
3196 * @vport: The virtual port (scsi_host) for the flush context
3197 * @tgt_id: If aborting by Target contect - specifies the target id
3198 * @lun_id: If aborting by Lun context - specifies the lun id
3199 * @context: specifies the context level to flush at.
3201 * After a reset condition via TMF, we need to flush orphaned i/o
3202 * contexts from the adapter. This routine aborts any contexts
3203 * outstanding, then waits for their completions. The wait is
3204 * bounded by devloss_tmo though.
3206 * Return code :
3207 * 0x2003 - Error
3208 * 0x2002 - Success
3210 static int
3211 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3212 uint64_t lun_id, lpfc_ctx_cmd context)
3214 struct lpfc_hba *phba = vport->phba;
3215 unsigned long later;
3216 int cnt;
3218 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3219 if (cnt)
3220 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3221 tgt_id, lun_id, context);
3222 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3223 while (time_after(later, jiffies) && cnt) {
3224 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3225 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3227 if (cnt) {
3228 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3229 "0724 I/O flush failure for context %s : cnt x%x\n",
3230 ((context == LPFC_CTX_LUN) ? "LUN" :
3231 ((context == LPFC_CTX_TGT) ? "TGT" :
3232 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3233 cnt);
3234 return FAILED;
3236 return SUCCESS;
3240 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
3241 * @cmnd: Pointer to scsi_cmnd data structure.
3243 * This routine does a device reset by sending a LUN_RESET task management
3244 * command.
3246 * Return code :
3247 * 0x2003 - Error
3248 * 0x2002 - Success
3250 static int
3251 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
3253 struct Scsi_Host *shost = cmnd->device->host;
3254 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3255 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3256 struct lpfc_nodelist *pnode = rdata->pnode;
3257 unsigned tgt_id = cmnd->device->id;
3258 unsigned int lun_id = cmnd->device->lun;
3259 struct lpfc_scsi_event_header scsi_event;
3260 int status;
3262 lpfc_block_error_handler(cmnd);
3264 status = lpfc_chk_tgt_mapped(vport, cmnd);
3265 if (status == FAILED) {
3266 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3267 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3268 return FAILED;
3271 scsi_event.event_type = FC_REG_SCSI_EVENT;
3272 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3273 scsi_event.lun = lun_id;
3274 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3275 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3277 fc_host_post_vendor_event(shost, fc_get_event_number(),
3278 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3280 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3281 FCP_LUN_RESET);
3283 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3284 "0713 SCSI layer issued Device Reset (%d, %d) "
3285 "return x%x\n", tgt_id, lun_id, status);
3288 * We have to clean up i/o as : they may be orphaned by the TMF;
3289 * or if the TMF failed, they may be in an indeterminate state.
3290 * So, continue on.
3291 * We will report success if all the i/o aborts successfully.
3293 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3294 LPFC_CTX_LUN);
3295 return status;
3299 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3300 * @cmnd: Pointer to scsi_cmnd data structure.
3302 * This routine does a target reset by sending a TARGET_RESET task management
3303 * command.
3305 * Return code :
3306 * 0x2003 - Error
3307 * 0x2002 - Success
3309 static int
3310 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3312 struct Scsi_Host *shost = cmnd->device->host;
3313 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3314 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3315 struct lpfc_nodelist *pnode = rdata->pnode;
3316 unsigned tgt_id = cmnd->device->id;
3317 unsigned int lun_id = cmnd->device->lun;
3318 struct lpfc_scsi_event_header scsi_event;
3319 int status;
3321 lpfc_block_error_handler(cmnd);
3323 status = lpfc_chk_tgt_mapped(vport, cmnd);
3324 if (status == FAILED) {
3325 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3326 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3327 return FAILED;
3330 scsi_event.event_type = FC_REG_SCSI_EVENT;
3331 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3332 scsi_event.lun = 0;
3333 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3334 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3336 fc_host_post_vendor_event(shost, fc_get_event_number(),
3337 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3339 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3340 FCP_TARGET_RESET);
3342 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3343 "0723 SCSI layer issued Target Reset (%d, %d) "
3344 "return x%x\n", tgt_id, lun_id, status);
3347 * We have to clean up i/o as : they may be orphaned by the TMF;
3348 * or if the TMF failed, they may be in an indeterminate state.
3349 * So, continue on.
3350 * We will report success if all the i/o aborts successfully.
3352 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3353 LPFC_CTX_TGT);
3354 return status;
3358 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
3359 * @cmnd: Pointer to scsi_cmnd data structure.
3361 * This routine does target reset to all targets on @cmnd->device->host.
3362 * This emulates Parallel SCSI Bus Reset Semantics.
3364 * Return code :
3365 * 0x2003 - Error
3366 * 0x2002 - Success
3368 static int
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_nodelist *ndlp = NULL;
3374 struct lpfc_scsi_event_header scsi_event;
3375 int match;
3376 int ret = SUCCESS, status, i;
3378 scsi_event.event_type = FC_REG_SCSI_EVENT;
3379 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3380 scsi_event.lun = 0;
3381 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3382 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3384 fc_host_post_vendor_event(shost, fc_get_event_number(),
3385 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3387 lpfc_block_error_handler(cmnd);
3390 * Since the driver manages a single bus device, reset all
3391 * targets known to the driver. Should any target reset
3392 * fail, this routine returns failure to the midlayer.
3394 for (i = 0; i < LPFC_MAX_TARGET; i++) {
3395 /* Search for mapped node by target ID */
3396 match = 0;
3397 spin_lock_irq(shost->host_lock);
3398 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3399 if (!NLP_CHK_NODE_ACT(ndlp))
3400 continue;
3401 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
3402 ndlp->nlp_sid == i &&
3403 ndlp->rport) {
3404 match = 1;
3405 break;
3408 spin_unlock_irq(shost->host_lock);
3409 if (!match)
3410 continue;
3412 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3413 i, 0, FCP_TARGET_RESET);
3415 if (status != SUCCESS) {
3416 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3417 "0700 Bus Reset on target %d failed\n",
3419 ret = FAILED;
3423 * We have to clean up i/o as : they may be orphaned by the TMFs
3424 * above; or if any of the TMFs failed, they may be in an
3425 * indeterminate state.
3426 * We will report success if all the i/o aborts successfully.
3429 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3430 if (status != SUCCESS)
3431 ret = FAILED;
3433 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3434 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
3435 return ret;
3439 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
3440 * @sdev: Pointer to scsi_device.
3442 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3443 * globally available list of scsi buffers. This routine also makes sure scsi
3444 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3445 * of scsi buffer exists for the lifetime of the driver.
3447 * Return codes:
3448 * non-0 - Error
3449 * 0 - Success
3451 static int
3452 lpfc_slave_alloc(struct scsi_device *sdev)
3454 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3455 struct lpfc_hba *phba = vport->phba;
3456 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3457 uint32_t total = 0;
3458 uint32_t num_to_alloc = 0;
3459 int num_allocated = 0;
3461 if (!rport || fc_remote_port_chkready(rport))
3462 return -ENXIO;
3464 sdev->hostdata = rport->dd_data;
3467 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3468 * available list of scsi buffers. Don't allocate more than the
3469 * HBA limit conveyed to the midlayer via the host structure. The
3470 * formula accounts for the lun_queue_depth + error handlers + 1
3471 * extra. This list of scsi bufs exists for the lifetime of the driver.
3473 total = phba->total_scsi_bufs;
3474 num_to_alloc = vport->cfg_lun_queue_depth + 2;
3476 /* Allow some exchanges to be available always to complete discovery */
3477 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
3478 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3479 "0704 At limitation of %d preallocated "
3480 "command buffers\n", total);
3481 return 0;
3482 /* Allow some exchanges to be available always to complete discovery */
3483 } else if (total + num_to_alloc >
3484 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
3485 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3486 "0705 Allocation request of %d "
3487 "command buffers will exceed max of %d. "
3488 "Reducing allocation request to %d.\n",
3489 num_to_alloc, phba->cfg_hba_queue_depth,
3490 (phba->cfg_hba_queue_depth - total));
3491 num_to_alloc = phba->cfg_hba_queue_depth - total;
3493 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3494 if (num_to_alloc != num_allocated) {
3495 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3496 "0708 Allocation request of %d "
3497 "command buffers did not succeed. "
3498 "Allocated %d buffers.\n",
3499 num_to_alloc, num_allocated);
3501 return 0;
3505 * lpfc_slave_configure - scsi_host_template slave_configure entry point
3506 * @sdev: Pointer to scsi_device.
3508 * This routine configures following items
3509 * - Tag command queuing support for @sdev if supported.
3510 * - Dev loss time out value of fc_rport.
3511 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3513 * Return codes:
3514 * 0 - Success
3516 static int
3517 lpfc_slave_configure(struct scsi_device *sdev)
3519 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3520 struct lpfc_hba *phba = vport->phba;
3521 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
3523 if (sdev->tagged_supported)
3524 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
3525 else
3526 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
3529 * Initialize the fc transport attributes for the target
3530 * containing this scsi device. Also note that the driver's
3531 * target pointer is stored in the starget_data for the
3532 * driver's sysfs entry point functions.
3534 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
3536 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3537 lpfc_sli_poll_fcp_ring(phba);
3538 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3539 lpfc_poll_rearm_timer(phba);
3542 return 0;
3546 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
3547 * @sdev: Pointer to scsi_device.
3549 * This routine sets @sdev hostatdata filed to null.
3551 static void
3552 lpfc_slave_destroy(struct scsi_device *sdev)
3554 sdev->hostdata = NULL;
3555 return;
3559 struct scsi_host_template lpfc_template = {
3560 .module = THIS_MODULE,
3561 .name = LPFC_DRIVER_NAME,
3562 .info = lpfc_info,
3563 .queuecommand = lpfc_queuecommand,
3564 .eh_abort_handler = lpfc_abort_handler,
3565 .eh_device_reset_handler = lpfc_device_reset_handler,
3566 .eh_target_reset_handler = lpfc_target_reset_handler,
3567 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3568 .slave_alloc = lpfc_slave_alloc,
3569 .slave_configure = lpfc_slave_configure,
3570 .slave_destroy = lpfc_slave_destroy,
3571 .scan_finished = lpfc_scan_finished,
3572 .this_id = -1,
3573 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3574 .cmd_per_lun = LPFC_CMD_PER_LUN,
3575 .use_clustering = ENABLE_CLUSTERING,
3576 .shost_attrs = lpfc_hba_attrs,
3577 .max_sectors = 0xFFFF,
3578 .vendor_id = LPFC_NL_VENDOR_ID,
3581 struct scsi_host_template lpfc_vport_template = {
3582 .module = THIS_MODULE,
3583 .name = LPFC_DRIVER_NAME,
3584 .info = lpfc_info,
3585 .queuecommand = lpfc_queuecommand,
3586 .eh_abort_handler = lpfc_abort_handler,
3587 .eh_device_reset_handler = lpfc_device_reset_handler,
3588 .eh_target_reset_handler = lpfc_target_reset_handler,
3589 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3590 .slave_alloc = lpfc_slave_alloc,
3591 .slave_configure = lpfc_slave_configure,
3592 .slave_destroy = lpfc_slave_destroy,
3593 .scan_finished = lpfc_scan_finished,
3594 .this_id = -1,
3595 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3596 .cmd_per_lun = LPFC_CMD_PER_LUN,
3597 .use_clustering = ENABLE_CLUSTERING,
3598 .shost_attrs = lpfc_vport_attrs,
3599 .max_sectors = 0xFFFF,