include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / scsi / lpfc / lpfc_scsi.c
blob8cac0b039856ee86d7780db0e7eca26996a9eb06
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2011 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/slab.h>
23 #include <linux/interrupt.h>
24 #include <linux/export.h>
25 #include <linux/delay.h>
26 #include <asm/unaligned.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_eh.h>
31 #include <scsi/scsi_host.h>
32 #include <scsi/scsi_tcq.h>
33 #include <scsi/scsi_transport_fc.h>
35 #include "lpfc_version.h"
36 #include "lpfc_hw4.h"
37 #include "lpfc_hw.h"
38 #include "lpfc_sli.h"
39 #include "lpfc_sli4.h"
40 #include "lpfc_nl.h"
41 #include "lpfc_disc.h"
42 #include "lpfc_scsi.h"
43 #include "lpfc.h"
44 #include "lpfc_logmsg.h"
45 #include "lpfc_crtn.h"
46 #include "lpfc_vport.h"
48 #define LPFC_RESET_WAIT 2
49 #define LPFC_ABORT_WAIT 2
51 int _dump_buf_done;
53 static char *dif_op_str[] = {
54 "SCSI_PROT_NORMAL",
55 "SCSI_PROT_READ_INSERT",
56 "SCSI_PROT_WRITE_STRIP",
57 "SCSI_PROT_READ_STRIP",
58 "SCSI_PROT_WRITE_INSERT",
59 "SCSI_PROT_READ_PASS",
60 "SCSI_PROT_WRITE_PASS",
62 static void
63 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
64 static void
65 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
67 static void
68 lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
70 void *src, *dst;
71 struct scatterlist *sgde = scsi_sglist(cmnd);
73 if (!_dump_buf_data) {
74 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
75 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
76 __func__);
77 return;
81 if (!sgde) {
82 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
83 "9051 BLKGRD: ERROR: data scatterlist is null\n");
84 return;
87 dst = (void *) _dump_buf_data;
88 while (sgde) {
89 src = sg_virt(sgde);
90 memcpy(dst, src, sgde->length);
91 dst += sgde->length;
92 sgde = sg_next(sgde);
96 static void
97 lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
99 void *src, *dst;
100 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
102 if (!_dump_buf_dif) {
103 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
104 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
105 __func__);
106 return;
109 if (!sgde) {
110 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
111 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
112 return;
115 dst = _dump_buf_dif;
116 while (sgde) {
117 src = sg_virt(sgde);
118 memcpy(dst, src, sgde->length);
119 dst += sgde->length;
120 sgde = sg_next(sgde);
125 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
126 * @phba: Pointer to HBA object.
127 * @lpfc_cmd: lpfc scsi command object pointer.
129 * This function is called from the lpfc_prep_task_mgmt_cmd function to
130 * set the last bit in the response sge entry.
132 static void
133 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
134 struct lpfc_scsi_buf *lpfc_cmd)
136 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
137 if (sgl) {
138 sgl += 1;
139 sgl->word2 = le32_to_cpu(sgl->word2);
140 bf_set(lpfc_sli4_sge_last, sgl, 1);
141 sgl->word2 = cpu_to_le32(sgl->word2);
146 * lpfc_update_stats - Update statistical data for the command completion
147 * @phba: Pointer to HBA object.
148 * @lpfc_cmd: lpfc scsi command object pointer.
150 * This function is called when there is a command completion and this
151 * function updates the statistical data for the command completion.
153 static void
154 lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
156 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
157 struct lpfc_nodelist *pnode = rdata->pnode;
158 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
159 unsigned long flags;
160 struct Scsi_Host *shost = cmd->device->host;
161 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
162 unsigned long latency;
163 int i;
165 if (cmd->result)
166 return;
168 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
170 spin_lock_irqsave(shost->host_lock, flags);
171 if (!vport->stat_data_enabled ||
172 vport->stat_data_blocked ||
173 !pnode ||
174 !pnode->lat_data ||
175 (phba->bucket_type == LPFC_NO_BUCKET)) {
176 spin_unlock_irqrestore(shost->host_lock, flags);
177 return;
180 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
181 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
182 phba->bucket_step;
183 /* check array subscript bounds */
184 if (i < 0)
185 i = 0;
186 else if (i >= LPFC_MAX_BUCKET_COUNT)
187 i = LPFC_MAX_BUCKET_COUNT - 1;
188 } else {
189 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
190 if (latency <= (phba->bucket_base +
191 ((1<<i)*phba->bucket_step)))
192 break;
195 pnode->lat_data[i].cmd_count++;
196 spin_unlock_irqrestore(shost->host_lock, flags);
200 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
201 * @phba: Pointer to HBA context object.
202 * @vport: Pointer to vport object.
203 * @ndlp: Pointer to FC node associated with the target.
204 * @lun: Lun number of the scsi device.
205 * @old_val: Old value of the queue depth.
206 * @new_val: New value of the queue depth.
208 * This function sends an event to the mgmt application indicating
209 * there is a change in the scsi device queue depth.
211 static void
212 lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
213 struct lpfc_vport *vport,
214 struct lpfc_nodelist *ndlp,
215 uint32_t lun,
216 uint32_t old_val,
217 uint32_t new_val)
219 struct lpfc_fast_path_event *fast_path_evt;
220 unsigned long flags;
222 fast_path_evt = lpfc_alloc_fast_evt(phba);
223 if (!fast_path_evt)
224 return;
226 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
227 FC_REG_SCSI_EVENT;
228 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
229 LPFC_EVENT_VARQUEDEPTH;
231 /* Report all luns with change in queue depth */
232 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
233 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
234 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
235 &ndlp->nlp_portname, sizeof(struct lpfc_name));
236 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
237 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
240 fast_path_evt->un.queue_depth_evt.oldval = old_val;
241 fast_path_evt->un.queue_depth_evt.newval = new_val;
242 fast_path_evt->vport = vport;
244 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
245 spin_lock_irqsave(&phba->hbalock, flags);
246 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
247 spin_unlock_irqrestore(&phba->hbalock, flags);
248 lpfc_worker_wake_up(phba);
250 return;
254 * lpfc_change_queue_depth - Alter scsi device queue depth
255 * @sdev: Pointer the scsi device on which to change the queue depth.
256 * @qdepth: New queue depth to set the sdev to.
257 * @reason: The reason for the queue depth change.
259 * This function is called by the midlayer and the LLD to alter the queue
260 * depth for a scsi device. This function sets the queue depth to the new
261 * value and sends an event out to log the queue depth change.
264 lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
266 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
267 struct lpfc_hba *phba = vport->phba;
268 struct lpfc_rport_data *rdata;
269 unsigned long new_queue_depth, old_queue_depth;
271 old_queue_depth = sdev->queue_depth;
272 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
273 new_queue_depth = sdev->queue_depth;
274 rdata = sdev->hostdata;
275 if (rdata)
276 lpfc_send_sdev_queuedepth_change_event(phba, vport,
277 rdata->pnode, sdev->lun,
278 old_queue_depth,
279 new_queue_depth);
280 return sdev->queue_depth;
284 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
285 * @phba: The Hba for which this call is being executed.
287 * This routine is called when there is resource error in driver or firmware.
288 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
289 * posts at most 1 event each second. This routine wakes up worker thread of
290 * @phba to process WORKER_RAM_DOWN_EVENT event.
292 * This routine should be called with no lock held.
294 void
295 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
297 unsigned long flags;
298 uint32_t evt_posted;
300 spin_lock_irqsave(&phba->hbalock, flags);
301 atomic_inc(&phba->num_rsrc_err);
302 phba->last_rsrc_error_time = jiffies;
304 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
305 spin_unlock_irqrestore(&phba->hbalock, flags);
306 return;
309 phba->last_ramp_down_time = jiffies;
311 spin_unlock_irqrestore(&phba->hbalock, flags);
313 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
314 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
315 if (!evt_posted)
316 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
317 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
319 if (!evt_posted)
320 lpfc_worker_wake_up(phba);
321 return;
325 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
326 * @phba: The Hba for which this call is being executed.
328 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
329 * post at most 1 event every 5 minute after last_ramp_up_time or
330 * last_rsrc_error_time. This routine wakes up worker thread of @phba
331 * to process WORKER_RAM_DOWN_EVENT event.
333 * This routine should be called with no lock held.
335 static inline void
336 lpfc_rampup_queue_depth(struct lpfc_vport *vport,
337 uint32_t queue_depth)
339 unsigned long flags;
340 struct lpfc_hba *phba = vport->phba;
341 uint32_t evt_posted;
342 atomic_inc(&phba->num_cmd_success);
344 if (vport->cfg_lun_queue_depth <= queue_depth)
345 return;
346 spin_lock_irqsave(&phba->hbalock, flags);
347 if (time_before(jiffies,
348 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
349 time_before(jiffies,
350 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
351 spin_unlock_irqrestore(&phba->hbalock, flags);
352 return;
354 phba->last_ramp_up_time = jiffies;
355 spin_unlock_irqrestore(&phba->hbalock, flags);
357 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
358 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
359 if (!evt_posted)
360 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
361 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
363 if (!evt_posted)
364 lpfc_worker_wake_up(phba);
365 return;
369 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
370 * @phba: The Hba for which this call is being executed.
372 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
373 * thread.This routine reduces queue depth for all scsi device on each vport
374 * associated with @phba.
376 void
377 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
379 struct lpfc_vport **vports;
380 struct Scsi_Host *shost;
381 struct scsi_device *sdev;
382 unsigned long new_queue_depth;
383 unsigned long num_rsrc_err, num_cmd_success;
384 int i;
386 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
387 num_cmd_success = atomic_read(&phba->num_cmd_success);
389 vports = lpfc_create_vport_work_array(phba);
390 if (vports != NULL)
391 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
392 shost = lpfc_shost_from_vport(vports[i]);
393 shost_for_each_device(sdev, shost) {
394 new_queue_depth =
395 sdev->queue_depth * num_rsrc_err /
396 (num_rsrc_err + num_cmd_success);
397 if (!new_queue_depth)
398 new_queue_depth = sdev->queue_depth - 1;
399 else
400 new_queue_depth = sdev->queue_depth -
401 new_queue_depth;
402 lpfc_change_queue_depth(sdev, new_queue_depth,
403 SCSI_QDEPTH_DEFAULT);
406 lpfc_destroy_vport_work_array(phba, vports);
407 atomic_set(&phba->num_rsrc_err, 0);
408 atomic_set(&phba->num_cmd_success, 0);
412 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
413 * @phba: The Hba for which this call is being executed.
415 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
416 * thread.This routine increases queue depth for all scsi device on each vport
417 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
418 * num_cmd_success to zero.
420 void
421 lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
423 struct lpfc_vport **vports;
424 struct Scsi_Host *shost;
425 struct scsi_device *sdev;
426 int i;
428 vports = lpfc_create_vport_work_array(phba);
429 if (vports != NULL)
430 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
431 shost = lpfc_shost_from_vport(vports[i]);
432 shost_for_each_device(sdev, shost) {
433 if (vports[i]->cfg_lun_queue_depth <=
434 sdev->queue_depth)
435 continue;
436 lpfc_change_queue_depth(sdev,
437 sdev->queue_depth+1,
438 SCSI_QDEPTH_RAMP_UP);
441 lpfc_destroy_vport_work_array(phba, vports);
442 atomic_set(&phba->num_rsrc_err, 0);
443 atomic_set(&phba->num_cmd_success, 0);
447 * lpfc_scsi_dev_block - set all scsi hosts to block state
448 * @phba: Pointer to HBA context object.
450 * This function walks vport list and set each SCSI host to block state
451 * by invoking fc_remote_port_delete() routine. This function is invoked
452 * with EEH when device's PCI slot has been permanently disabled.
454 void
455 lpfc_scsi_dev_block(struct lpfc_hba *phba)
457 struct lpfc_vport **vports;
458 struct Scsi_Host *shost;
459 struct scsi_device *sdev;
460 struct fc_rport *rport;
461 int i;
463 vports = lpfc_create_vport_work_array(phba);
464 if (vports != NULL)
465 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
466 shost = lpfc_shost_from_vport(vports[i]);
467 shost_for_each_device(sdev, shost) {
468 rport = starget_to_rport(scsi_target(sdev));
469 fc_remote_port_delete(rport);
472 lpfc_destroy_vport_work_array(phba, vports);
476 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
477 * @vport: The virtual port for which this call being executed.
478 * @num_to_allocate: The requested number of buffers to allocate.
480 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
481 * the scsi buffer contains all the necessary information needed to initiate
482 * a SCSI I/O. The non-DMAable buffer region contains information to build
483 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
484 * and the initial BPL. In addition to allocating memory, the FCP CMND and
485 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
487 * Return codes:
488 * int - number of scsi buffers that were allocated.
489 * 0 = failure, less than num_to_alloc is a partial failure.
491 static int
492 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
494 struct lpfc_hba *phba = vport->phba;
495 struct lpfc_scsi_buf *psb;
496 struct ulp_bde64 *bpl;
497 IOCB_t *iocb;
498 dma_addr_t pdma_phys_fcp_cmd;
499 dma_addr_t pdma_phys_fcp_rsp;
500 dma_addr_t pdma_phys_bpl;
501 uint16_t iotag;
502 int bcnt;
504 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
505 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
506 if (!psb)
507 break;
510 * Get memory from the pci pool to map the virt space to pci
511 * bus space for an I/O. The DMA buffer includes space for the
512 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
513 * necessary to support the sg_tablesize.
515 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
516 GFP_KERNEL, &psb->dma_handle);
517 if (!psb->data) {
518 kfree(psb);
519 break;
522 /* Initialize virtual ptrs to dma_buf region. */
523 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
525 /* Allocate iotag for psb->cur_iocbq. */
526 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
527 if (iotag == 0) {
528 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
529 psb->data, psb->dma_handle);
530 kfree(psb);
531 break;
533 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
535 psb->fcp_cmnd = psb->data;
536 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
537 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
538 sizeof(struct fcp_rsp);
540 /* Initialize local short-hand pointers. */
541 bpl = psb->fcp_bpl;
542 pdma_phys_fcp_cmd = psb->dma_handle;
543 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
544 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
545 sizeof(struct fcp_rsp);
548 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
549 * are sg list bdes. Initialize the first two and leave the
550 * rest for queuecommand.
552 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
553 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
554 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
555 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
556 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
558 /* Setup the physical region for the FCP RSP */
559 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
560 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
561 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
562 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
563 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
566 * Since the IOCB for the FCP I/O is built into this
567 * lpfc_scsi_buf, initialize it with all known data now.
569 iocb = &psb->cur_iocbq.iocb;
570 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
571 if ((phba->sli_rev == 3) &&
572 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
573 /* fill in immediate fcp command BDE */
574 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
575 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
576 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
577 unsli3.fcp_ext.icd);
578 iocb->un.fcpi64.bdl.addrHigh = 0;
579 iocb->ulpBdeCount = 0;
580 iocb->ulpLe = 0;
581 /* fill in response BDE */
582 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
583 BUFF_TYPE_BDE_64;
584 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
585 sizeof(struct fcp_rsp);
586 iocb->unsli3.fcp_ext.rbde.addrLow =
587 putPaddrLow(pdma_phys_fcp_rsp);
588 iocb->unsli3.fcp_ext.rbde.addrHigh =
589 putPaddrHigh(pdma_phys_fcp_rsp);
590 } else {
591 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
592 iocb->un.fcpi64.bdl.bdeSize =
593 (2 * sizeof(struct ulp_bde64));
594 iocb->un.fcpi64.bdl.addrLow =
595 putPaddrLow(pdma_phys_bpl);
596 iocb->un.fcpi64.bdl.addrHigh =
597 putPaddrHigh(pdma_phys_bpl);
598 iocb->ulpBdeCount = 1;
599 iocb->ulpLe = 1;
601 iocb->ulpClass = CLASS3;
602 psb->status = IOSTAT_SUCCESS;
603 /* Put it back into the SCSI buffer list */
604 psb->cur_iocbq.context1 = psb;
605 lpfc_release_scsi_buf_s3(phba, psb);
609 return bcnt;
613 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
614 * @vport: pointer to lpfc vport data structure.
616 * This routine is invoked by the vport cleanup for deletions and the cleanup
617 * for an ndlp on removal.
619 void
620 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
622 struct lpfc_hba *phba = vport->phba;
623 struct lpfc_scsi_buf *psb, *next_psb;
624 unsigned long iflag = 0;
626 spin_lock_irqsave(&phba->hbalock, iflag);
627 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
628 list_for_each_entry_safe(psb, next_psb,
629 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
630 if (psb->rdata && psb->rdata->pnode
631 && psb->rdata->pnode->vport == vport)
632 psb->rdata = NULL;
634 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
635 spin_unlock_irqrestore(&phba->hbalock, iflag);
639 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
640 * @phba: pointer to lpfc hba data structure.
641 * @axri: pointer to the fcp xri abort wcqe structure.
643 * This routine is invoked by the worker thread to process a SLI4 fast-path
644 * FCP aborted xri.
646 void
647 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
648 struct sli4_wcqe_xri_aborted *axri)
650 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
651 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
652 struct lpfc_scsi_buf *psb, *next_psb;
653 unsigned long iflag = 0;
654 struct lpfc_iocbq *iocbq;
655 int i;
656 struct lpfc_nodelist *ndlp;
657 int rrq_empty = 0;
658 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
660 spin_lock_irqsave(&phba->hbalock, iflag);
661 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
662 list_for_each_entry_safe(psb, next_psb,
663 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
664 if (psb->cur_iocbq.sli4_xritag == xri) {
665 list_del(&psb->list);
666 psb->exch_busy = 0;
667 psb->status = IOSTAT_SUCCESS;
668 spin_unlock(
669 &phba->sli4_hba.abts_scsi_buf_list_lock);
670 if (psb->rdata && psb->rdata->pnode)
671 ndlp = psb->rdata->pnode;
672 else
673 ndlp = NULL;
675 rrq_empty = list_empty(&phba->active_rrq_list);
676 spin_unlock_irqrestore(&phba->hbalock, iflag);
677 if (ndlp)
678 lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
679 lpfc_release_scsi_buf_s4(phba, psb);
680 if (rrq_empty)
681 lpfc_worker_wake_up(phba);
682 return;
685 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
686 for (i = 1; i <= phba->sli.last_iotag; i++) {
687 iocbq = phba->sli.iocbq_lookup[i];
689 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
690 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
691 continue;
692 if (iocbq->sli4_xritag != xri)
693 continue;
694 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
695 psb->exch_busy = 0;
696 spin_unlock_irqrestore(&phba->hbalock, iflag);
697 if (pring->txq_cnt)
698 lpfc_worker_wake_up(phba);
699 return;
702 spin_unlock_irqrestore(&phba->hbalock, iflag);
706 * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
707 * @phba: pointer to lpfc hba data structure.
709 * This routine walks the list of scsi buffers that have been allocated and
710 * repost them to the HBA by using SGL block post. This is needed after a
711 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
712 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
713 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
715 * Returns: 0 = success, non-zero failure.
718 lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
720 struct lpfc_scsi_buf *psb;
721 int index, status, bcnt = 0, rcnt = 0, rc = 0;
722 LIST_HEAD(sblist);
724 for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
725 psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
726 if (psb) {
727 /* Remove from SCSI buffer list */
728 list_del(&psb->list);
729 /* Add it to a local SCSI buffer list */
730 list_add_tail(&psb->list, &sblist);
731 if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
732 bcnt = rcnt;
733 rcnt = 0;
735 } else
736 /* A hole present in the XRI array, need to skip */
737 bcnt = rcnt;
739 if (index == phba->sli4_hba.scsi_xri_cnt - 1)
740 /* End of XRI array for SCSI buffer, complete */
741 bcnt = rcnt;
743 /* Continue until collect up to a nembed page worth of sgls */
744 if (bcnt == 0)
745 continue;
746 /* Now, post the SCSI buffer list sgls as a block */
747 if (!phba->sli4_hba.extents_in_use)
748 status = lpfc_sli4_post_scsi_sgl_block(phba,
749 &sblist,
750 bcnt);
751 else
752 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
753 &sblist,
754 bcnt);
755 /* Reset SCSI buffer count for next round of posting */
756 bcnt = 0;
757 while (!list_empty(&sblist)) {
758 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
759 list);
760 if (status) {
761 /* Put this back on the abort scsi list */
762 psb->exch_busy = 1;
763 rc++;
764 } else {
765 psb->exch_busy = 0;
766 psb->status = IOSTAT_SUCCESS;
768 /* Put it back into the SCSI buffer list */
769 lpfc_release_scsi_buf_s4(phba, psb);
772 return rc;
776 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
777 * @vport: The virtual port for which this call being executed.
778 * @num_to_allocate: The requested number of buffers to allocate.
780 * This routine allocates a scsi buffer for device with SLI-4 interface spec,
781 * the scsi buffer contains all the necessary information needed to initiate
782 * a SCSI I/O.
784 * Return codes:
785 * int - number of scsi buffers that were allocated.
786 * 0 = failure, less than num_to_alloc is a partial failure.
788 static int
789 lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
791 struct lpfc_hba *phba = vport->phba;
792 struct lpfc_scsi_buf *psb;
793 struct sli4_sge *sgl;
794 IOCB_t *iocb;
795 dma_addr_t pdma_phys_fcp_cmd;
796 dma_addr_t pdma_phys_fcp_rsp;
797 dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
798 uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
799 int status = 0, index;
800 int bcnt;
801 int non_sequential_xri = 0;
802 LIST_HEAD(sblist);
804 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
805 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
806 if (!psb)
807 break;
810 * Get memory from the pci pool to map the virt space to pci bus
811 * space for an I/O. The DMA buffer includes space for the
812 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
813 * necessary to support the sg_tablesize.
815 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
816 GFP_KERNEL, &psb->dma_handle);
817 if (!psb->data) {
818 kfree(psb);
819 break;
822 /* Initialize virtual ptrs to dma_buf region. */
823 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
825 /* Allocate iotag for psb->cur_iocbq. */
826 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
827 if (iotag == 0) {
828 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
829 psb->data, psb->dma_handle);
830 kfree(psb);
831 break;
834 lxri = lpfc_sli4_next_xritag(phba);
835 if (lxri == NO_XRI) {
836 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
837 psb->data, psb->dma_handle);
838 kfree(psb);
839 break;
841 psb->cur_iocbq.sli4_lxritag = lxri;
842 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
843 if (last_xritag != NO_XRI
844 && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
845 non_sequential_xri = 1;
846 } else
847 list_add_tail(&psb->list, &sblist);
848 last_xritag = psb->cur_iocbq.sli4_xritag;
850 index = phba->sli4_hba.scsi_xri_cnt++;
851 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
853 psb->fcp_bpl = psb->data;
854 psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
855 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
856 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
857 sizeof(struct fcp_cmnd));
859 /* Initialize local short-hand pointers. */
860 sgl = (struct sli4_sge *)psb->fcp_bpl;
861 pdma_phys_bpl = psb->dma_handle;
862 pdma_phys_fcp_cmd =
863 (psb->dma_handle + phba->cfg_sg_dma_buf_size)
864 - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
865 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
868 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
869 * are sg list bdes. Initialize the first two and leave the
870 * rest for queuecommand.
872 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
873 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
874 sgl->word2 = le32_to_cpu(sgl->word2);
875 bf_set(lpfc_sli4_sge_last, sgl, 0);
876 sgl->word2 = cpu_to_le32(sgl->word2);
877 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
878 sgl++;
880 /* Setup the physical region for the FCP RSP */
881 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
882 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
883 sgl->word2 = le32_to_cpu(sgl->word2);
884 bf_set(lpfc_sli4_sge_last, sgl, 1);
885 sgl->word2 = cpu_to_le32(sgl->word2);
886 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
889 * Since the IOCB for the FCP I/O is built into this
890 * lpfc_scsi_buf, initialize it with all known data now.
892 iocb = &psb->cur_iocbq.iocb;
893 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
894 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
895 /* setting the BLP size to 2 * sizeof BDE may not be correct.
896 * We are setting the bpl to point to out sgl. An sgl's
897 * entries are 16 bytes, a bpl entries are 12 bytes.
899 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
900 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
901 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
902 iocb->ulpBdeCount = 1;
903 iocb->ulpLe = 1;
904 iocb->ulpClass = CLASS3;
905 psb->cur_iocbq.context1 = psb;
906 if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
907 pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
908 else
909 pdma_phys_bpl1 = 0;
910 psb->dma_phys_bpl = pdma_phys_bpl;
911 phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
912 if (non_sequential_xri) {
913 status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
914 pdma_phys_bpl1,
915 psb->cur_iocbq.sli4_xritag);
916 if (status) {
917 /* Put this back on the abort scsi list */
918 psb->exch_busy = 1;
919 } else {
920 psb->exch_busy = 0;
921 psb->status = IOSTAT_SUCCESS;
923 /* Put it back into the SCSI buffer list */
924 lpfc_release_scsi_buf_s4(phba, psb);
925 break;
928 if (bcnt) {
929 if (!phba->sli4_hba.extents_in_use)
930 status = lpfc_sli4_post_scsi_sgl_block(phba,
931 &sblist,
932 bcnt);
933 else
934 status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
935 &sblist,
936 bcnt);
938 if (status) {
939 lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
940 "3021 SCSI SGL post error %d\n",
941 status);
942 bcnt = 0;
944 /* Reset SCSI buffer count for next round of posting */
945 while (!list_empty(&sblist)) {
946 list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
947 list);
948 if (status) {
949 /* Put this back on the abort scsi list */
950 psb->exch_busy = 1;
951 } else {
952 psb->exch_busy = 0;
953 psb->status = IOSTAT_SUCCESS;
955 /* Put it back into the SCSI buffer list */
956 lpfc_release_scsi_buf_s4(phba, psb);
960 return bcnt + non_sequential_xri;
964 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
965 * @vport: The virtual port for which this call being executed.
966 * @num_to_allocate: The requested number of buffers to allocate.
968 * This routine wraps the actual SCSI buffer allocator function pointer from
969 * the lpfc_hba struct.
971 * Return codes:
972 * int - number of scsi buffers that were allocated.
973 * 0 = failure, less than num_to_alloc is a partial failure.
975 static inline int
976 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
978 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
982 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
983 * @phba: The HBA for which this call is being executed.
985 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
986 * and returns to caller.
988 * Return codes:
989 * NULL - Error
990 * Pointer to lpfc_scsi_buf - Success
992 static struct lpfc_scsi_buf*
993 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
995 struct lpfc_scsi_buf * lpfc_cmd = NULL;
996 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
997 unsigned long iflag = 0;
999 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1000 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1001 if (lpfc_cmd) {
1002 lpfc_cmd->seg_cnt = 0;
1003 lpfc_cmd->nonsg_phys = 0;
1004 lpfc_cmd->prot_seg_cnt = 0;
1006 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1007 return lpfc_cmd;
1010 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1011 * @phba: The HBA for which this call is being executed.
1013 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1014 * and returns to caller.
1016 * Return codes:
1017 * NULL - Error
1018 * Pointer to lpfc_scsi_buf - Success
1020 static struct lpfc_scsi_buf*
1021 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1023 struct lpfc_scsi_buf *lpfc_cmd ;
1024 unsigned long iflag = 0;
1025 int found = 0;
1027 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1028 list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
1029 list) {
1030 if (lpfc_test_rrq_active(phba, ndlp,
1031 lpfc_cmd->cur_iocbq.sli4_xritag))
1032 continue;
1033 list_del(&lpfc_cmd->list);
1034 found = 1;
1035 lpfc_cmd->seg_cnt = 0;
1036 lpfc_cmd->nonsg_phys = 0;
1037 lpfc_cmd->prot_seg_cnt = 0;
1038 break;
1040 spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
1041 iflag);
1042 if (!found)
1043 return NULL;
1044 else
1045 return lpfc_cmd;
1048 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1049 * @phba: The HBA for which this call is being executed.
1051 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1052 * and returns to caller.
1054 * Return codes:
1055 * NULL - Error
1056 * Pointer to lpfc_scsi_buf - Success
1058 static struct lpfc_scsi_buf*
1059 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1061 return phba->lpfc_get_scsi_buf(phba, ndlp);
1065 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
1066 * @phba: The Hba for which this call is being executed.
1067 * @psb: The scsi buffer which is being released.
1069 * This routine releases @psb scsi buffer by adding it to tail of @phba
1070 * lpfc_scsi_buf_list list.
1072 static void
1073 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1075 unsigned long iflag = 0;
1077 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1078 psb->pCmd = NULL;
1079 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1080 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1084 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1085 * @phba: The Hba for which this call is being executed.
1086 * @psb: The scsi buffer which is being released.
1088 * This routine releases @psb scsi buffer by adding it to tail of @phba
1089 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1090 * and cannot be reused for at least RA_TOV amount of time if it was
1091 * aborted.
1093 static void
1094 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1096 unsigned long iflag = 0;
1098 if (psb->exch_busy) {
1099 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1100 iflag);
1101 psb->pCmd = NULL;
1102 list_add_tail(&psb->list,
1103 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1104 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1105 iflag);
1106 } else {
1108 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
1109 psb->pCmd = NULL;
1110 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
1111 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
1116 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1117 * @phba: The Hba for which this call is being executed.
1118 * @psb: The scsi buffer which is being released.
1120 * This routine releases @psb scsi buffer by adding it to tail of @phba
1121 * lpfc_scsi_buf_list list.
1123 static void
1124 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1127 phba->lpfc_release_scsi_buf(phba, psb);
1131 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
1132 * @phba: The Hba for which this call is being executed.
1133 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1135 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1136 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1137 * through sg elements and format the bdea. This routine also initializes all
1138 * IOCB fields which are dependent on scsi command request buffer.
1140 * Return codes:
1141 * 1 - Error
1142 * 0 - Success
1144 static int
1145 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1147 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1148 struct scatterlist *sgel = NULL;
1149 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1150 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1151 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
1152 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1153 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1154 dma_addr_t physaddr;
1155 uint32_t num_bde = 0;
1156 int nseg, datadir = scsi_cmnd->sc_data_direction;
1159 * There are three possibilities here - use scatter-gather segment, use
1160 * the single mapping, or neither. Start the lpfc command prep by
1161 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1162 * data bde entry.
1164 bpl += 2;
1165 if (scsi_sg_count(scsi_cmnd)) {
1167 * The driver stores the segment count returned from pci_map_sg
1168 * because this a count of dma-mappings used to map the use_sg
1169 * pages. They are not guaranteed to be the same for those
1170 * architectures that implement an IOMMU.
1173 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1174 scsi_sg_count(scsi_cmnd), datadir);
1175 if (unlikely(!nseg))
1176 return 1;
1178 lpfc_cmd->seg_cnt = nseg;
1179 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1180 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1181 "9064 BLKGRD: %s: Too many sg segments from "
1182 "dma_map_sg. Config %d, seg_cnt %d\n",
1183 __func__, phba->cfg_sg_seg_cnt,
1184 lpfc_cmd->seg_cnt);
1185 scsi_dma_unmap(scsi_cmnd);
1186 return 1;
1190 * The driver established a maximum scatter-gather segment count
1191 * during probe that limits the number of sg elements in any
1192 * single scsi command. Just run through the seg_cnt and format
1193 * the bde's.
1194 * When using SLI-3 the driver will try to fit all the BDEs into
1195 * the IOCB. If it can't then the BDEs get added to a BPL as it
1196 * does for SLI-2 mode.
1198 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1199 physaddr = sg_dma_address(sgel);
1200 if (phba->sli_rev == 3 &&
1201 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1202 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
1203 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1204 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1205 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1206 data_bde->addrLow = putPaddrLow(physaddr);
1207 data_bde->addrHigh = putPaddrHigh(physaddr);
1208 data_bde++;
1209 } else {
1210 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1211 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1212 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1213 bpl->addrLow =
1214 le32_to_cpu(putPaddrLow(physaddr));
1215 bpl->addrHigh =
1216 le32_to_cpu(putPaddrHigh(physaddr));
1217 bpl++;
1223 * Finish initializing those IOCB fields that are dependent on the
1224 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1225 * explicitly reinitialized and for SLI-3 the extended bde count is
1226 * explicitly reinitialized since all iocb memory resources are reused.
1228 if (phba->sli_rev == 3 &&
1229 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1230 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
1231 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1233 * The extended IOCB format can only fit 3 BDE or a BPL.
1234 * This I/O has more than 3 BDE so the 1st data bde will
1235 * be a BPL that is filled in here.
1237 physaddr = lpfc_cmd->dma_handle;
1238 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1239 data_bde->tus.f.bdeSize = (num_bde *
1240 sizeof(struct ulp_bde64));
1241 physaddr += (sizeof(struct fcp_cmnd) +
1242 sizeof(struct fcp_rsp) +
1243 (2 * sizeof(struct ulp_bde64)));
1244 data_bde->addrHigh = putPaddrHigh(physaddr);
1245 data_bde->addrLow = putPaddrLow(physaddr);
1246 /* ebde count includes the response bde and data bpl */
1247 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1248 } else {
1249 /* ebde count includes the response bde and data bdes */
1250 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1252 } else {
1253 iocb_cmd->un.fcpi64.bdl.bdeSize =
1254 ((num_bde + 2) * sizeof(struct ulp_bde64));
1255 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1257 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1260 * Due to difference in data length between DIF/non-DIF paths,
1261 * we need to set word 4 of IOCB here
1263 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1264 return 0;
1268 * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
1269 * @sc: The SCSI command to examine
1270 * @txopt: (out) BlockGuard operation for transmitted data
1271 * @rxopt: (out) BlockGuard operation for received data
1273 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1276 static int
1277 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1278 uint8_t *txop, uint8_t *rxop)
1280 uint8_t guard_type = scsi_host_get_guard(sc->device->host);
1281 uint8_t ret = 0;
1283 if (guard_type == SHOST_DIX_GUARD_IP) {
1284 switch (scsi_get_prot_op(sc)) {
1285 case SCSI_PROT_READ_INSERT:
1286 case SCSI_PROT_WRITE_STRIP:
1287 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1288 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1289 break;
1291 case SCSI_PROT_READ_STRIP:
1292 case SCSI_PROT_WRITE_INSERT:
1293 *txop = BG_OP_IN_NODIF_OUT_CRC;
1294 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1295 break;
1297 case SCSI_PROT_READ_PASS:
1298 case SCSI_PROT_WRITE_PASS:
1299 *txop = BG_OP_IN_CSUM_OUT_CRC;
1300 *rxop = BG_OP_IN_CRC_OUT_CSUM;
1301 break;
1303 case SCSI_PROT_NORMAL:
1304 default:
1305 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1306 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1307 scsi_get_prot_op(sc));
1308 ret = 1;
1309 break;
1312 } else {
1313 switch (scsi_get_prot_op(sc)) {
1314 case SCSI_PROT_READ_STRIP:
1315 case SCSI_PROT_WRITE_INSERT:
1316 *txop = BG_OP_IN_NODIF_OUT_CRC;
1317 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1318 break;
1320 case SCSI_PROT_READ_PASS:
1321 case SCSI_PROT_WRITE_PASS:
1322 *txop = BG_OP_IN_CRC_OUT_CRC;
1323 *rxop = BG_OP_IN_CRC_OUT_CRC;
1324 break;
1326 case SCSI_PROT_READ_INSERT:
1327 case SCSI_PROT_WRITE_STRIP:
1328 *txop = BG_OP_IN_CRC_OUT_NODIF;
1329 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1330 break;
1332 case SCSI_PROT_NORMAL:
1333 default:
1334 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1335 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1336 scsi_get_prot_op(sc));
1337 ret = 1;
1338 break;
1342 return ret;
1345 struct scsi_dif_tuple {
1346 __be16 guard_tag; /* Checksum */
1347 __be16 app_tag; /* Opaque storage */
1348 __be32 ref_tag; /* Target LBA or indirect LBA */
1351 static inline unsigned
1352 lpfc_cmd_blksize(struct scsi_cmnd *sc)
1354 return sc->device->sector_size;
1358 * This function sets up buffer list for protection groups of
1359 * type LPFC_PG_TYPE_NO_DIF
1361 * This is usually used when the HBA is instructed to generate
1362 * DIFs and insert them into data stream (or strip DIF from
1363 * incoming data stream)
1365 * The buffer list consists of just one protection group described
1366 * below:
1367 * +-------------------------+
1368 * start of prot group --> | PDE_5 |
1369 * +-------------------------+
1370 * | PDE_6 |
1371 * +-------------------------+
1372 * | Data BDE |
1373 * +-------------------------+
1374 * |more Data BDE's ... (opt)|
1375 * +-------------------------+
1377 * @sc: pointer to scsi command we're working on
1378 * @bpl: pointer to buffer list for protection groups
1379 * @datacnt: number of segments of data that have been dma mapped
1381 * Note: Data s/g buffers have been dma mapped
1383 static int
1384 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1385 struct ulp_bde64 *bpl, int datasegcnt)
1387 struct scatterlist *sgde = NULL; /* s/g data entry */
1388 struct lpfc_pde5 *pde5 = NULL;
1389 struct lpfc_pde6 *pde6 = NULL;
1390 dma_addr_t physaddr;
1391 int i = 0, num_bde = 0, status;
1392 int datadir = sc->sc_data_direction;
1393 uint32_t reftag;
1394 unsigned blksize;
1395 uint8_t txop, rxop;
1397 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1398 if (status)
1399 goto out;
1401 /* extract some info from the scsi command for pde*/
1402 blksize = lpfc_cmd_blksize(sc);
1403 reftag = scsi_get_lba(sc) & 0xffffffff;
1405 /* setup PDE5 with what we have */
1406 pde5 = (struct lpfc_pde5 *) bpl;
1407 memset(pde5, 0, sizeof(struct lpfc_pde5));
1408 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1410 /* Endianness conversion if necessary for PDE5 */
1411 pde5->word0 = cpu_to_le32(pde5->word0);
1412 pde5->reftag = cpu_to_le32(reftag);
1414 /* advance bpl and increment bde count */
1415 num_bde++;
1416 bpl++;
1417 pde6 = (struct lpfc_pde6 *) bpl;
1419 /* setup PDE6 with the rest of the info */
1420 memset(pde6, 0, sizeof(struct lpfc_pde6));
1421 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1422 bf_set(pde6_optx, pde6, txop);
1423 bf_set(pde6_oprx, pde6, rxop);
1424 if (datadir == DMA_FROM_DEVICE) {
1425 bf_set(pde6_ce, pde6, 1);
1426 bf_set(pde6_re, pde6, 1);
1428 bf_set(pde6_ai, pde6, 1);
1429 bf_set(pde6_ae, pde6, 0);
1430 bf_set(pde6_apptagval, pde6, 0);
1432 /* Endianness conversion if necessary for PDE6 */
1433 pde6->word0 = cpu_to_le32(pde6->word0);
1434 pde6->word1 = cpu_to_le32(pde6->word1);
1435 pde6->word2 = cpu_to_le32(pde6->word2);
1437 /* advance bpl and increment bde count */
1438 num_bde++;
1439 bpl++;
1441 /* assumption: caller has already run dma_map_sg on command data */
1442 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1443 physaddr = sg_dma_address(sgde);
1444 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1445 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1446 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1447 if (datadir == DMA_TO_DEVICE)
1448 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1449 else
1450 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1451 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1452 bpl++;
1453 num_bde++;
1456 out:
1457 return num_bde;
1461 * This function sets up buffer list for protection groups of
1462 * type LPFC_PG_TYPE_DIF_BUF
1464 * This is usually used when DIFs are in their own buffers,
1465 * separate from the data. The HBA can then by instructed
1466 * to place the DIFs in the outgoing stream. For read operations,
1467 * The HBA could extract the DIFs and place it in DIF buffers.
1469 * The buffer list for this type consists of one or more of the
1470 * protection groups described below:
1471 * +-------------------------+
1472 * start of first prot group --> | PDE_5 |
1473 * +-------------------------+
1474 * | PDE_6 |
1475 * +-------------------------+
1476 * | PDE_7 (Prot BDE) |
1477 * +-------------------------+
1478 * | Data BDE |
1479 * +-------------------------+
1480 * |more Data BDE's ... (opt)|
1481 * +-------------------------+
1482 * start of new prot group --> | PDE_5 |
1483 * +-------------------------+
1484 * | ... |
1485 * +-------------------------+
1487 * @sc: pointer to scsi command we're working on
1488 * @bpl: pointer to buffer list for protection groups
1489 * @datacnt: number of segments of data that have been dma mapped
1490 * @protcnt: number of segment of protection data that have been dma mapped
1492 * Note: It is assumed that both data and protection s/g buffers have been
1493 * mapped for DMA
1495 static int
1496 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1497 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1499 struct scatterlist *sgde = NULL; /* s/g data entry */
1500 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1501 struct lpfc_pde5 *pde5 = NULL;
1502 struct lpfc_pde6 *pde6 = NULL;
1503 struct lpfc_pde7 *pde7 = NULL;
1504 dma_addr_t dataphysaddr, protphysaddr;
1505 unsigned short curr_data = 0, curr_prot = 0;
1506 unsigned int split_offset;
1507 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
1508 unsigned int protgrp_blks, protgrp_bytes;
1509 unsigned int remainder, subtotal;
1510 int status;
1511 int datadir = sc->sc_data_direction;
1512 unsigned char pgdone = 0, alldone = 0;
1513 unsigned blksize;
1514 uint32_t reftag;
1515 uint8_t txop, rxop;
1516 int num_bde = 0;
1518 sgpe = scsi_prot_sglist(sc);
1519 sgde = scsi_sglist(sc);
1521 if (!sgpe || !sgde) {
1522 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1523 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
1524 sgpe, sgde);
1525 return 0;
1528 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1529 if (status)
1530 goto out;
1532 /* extract some info from the scsi command */
1533 blksize = lpfc_cmd_blksize(sc);
1534 reftag = scsi_get_lba(sc) & 0xffffffff;
1536 split_offset = 0;
1537 do {
1538 /* setup PDE5 with what we have */
1539 pde5 = (struct lpfc_pde5 *) bpl;
1540 memset(pde5, 0, sizeof(struct lpfc_pde5));
1541 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1543 /* Endianness conversion if necessary for PDE5 */
1544 pde5->word0 = cpu_to_le32(pde5->word0);
1545 pde5->reftag = cpu_to_le32(reftag);
1547 /* advance bpl and increment bde count */
1548 num_bde++;
1549 bpl++;
1550 pde6 = (struct lpfc_pde6 *) bpl;
1552 /* setup PDE6 with the rest of the info */
1553 memset(pde6, 0, sizeof(struct lpfc_pde6));
1554 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1555 bf_set(pde6_optx, pde6, txop);
1556 bf_set(pde6_oprx, pde6, rxop);
1557 bf_set(pde6_ce, pde6, 1);
1558 bf_set(pde6_re, pde6, 1);
1559 bf_set(pde6_ai, pde6, 1);
1560 bf_set(pde6_ae, pde6, 0);
1561 bf_set(pde6_apptagval, pde6, 0);
1563 /* Endianness conversion if necessary for PDE6 */
1564 pde6->word0 = cpu_to_le32(pde6->word0);
1565 pde6->word1 = cpu_to_le32(pde6->word1);
1566 pde6->word2 = cpu_to_le32(pde6->word2);
1568 /* advance bpl and increment bde count */
1569 num_bde++;
1570 bpl++;
1572 /* setup the first BDE that points to protection buffer */
1573 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1574 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
1576 /* must be integer multiple of the DIF block length */
1577 BUG_ON(protgroup_len % 8);
1579 pde7 = (struct lpfc_pde7 *) bpl;
1580 memset(pde7, 0, sizeof(struct lpfc_pde7));
1581 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1583 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1584 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1586 protgrp_blks = protgroup_len / 8;
1587 protgrp_bytes = protgrp_blks * blksize;
1589 /* check if this pde is crossing the 4K boundary; if so split */
1590 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1591 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1592 protgroup_offset += protgroup_remainder;
1593 protgrp_blks = protgroup_remainder / 8;
1594 protgrp_bytes = protgrp_blks * blksize;
1595 } else {
1596 protgroup_offset = 0;
1597 curr_prot++;
1600 num_bde++;
1602 /* setup BDE's for data blocks associated with DIF data */
1603 pgdone = 0;
1604 subtotal = 0; /* total bytes processed for current prot grp */
1605 while (!pgdone) {
1606 if (!sgde) {
1607 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1608 "9065 BLKGRD:%s Invalid data segment\n",
1609 __func__);
1610 return 0;
1612 bpl++;
1613 dataphysaddr = sg_dma_address(sgde) + split_offset;
1614 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1615 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1617 remainder = sg_dma_len(sgde) - split_offset;
1619 if ((subtotal + remainder) <= protgrp_bytes) {
1620 /* we can use this whole buffer */
1621 bpl->tus.f.bdeSize = remainder;
1622 split_offset = 0;
1624 if ((subtotal + remainder) == protgrp_bytes)
1625 pgdone = 1;
1626 } else {
1627 /* must split this buffer with next prot grp */
1628 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1629 split_offset += bpl->tus.f.bdeSize;
1632 subtotal += bpl->tus.f.bdeSize;
1634 if (datadir == DMA_TO_DEVICE)
1635 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1636 else
1637 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1638 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1640 num_bde++;
1641 curr_data++;
1643 if (split_offset)
1644 break;
1646 /* Move to the next s/g segment if possible */
1647 sgde = sg_next(sgde);
1651 if (protgroup_offset) {
1652 /* update the reference tag */
1653 reftag += protgrp_blks;
1654 bpl++;
1655 continue;
1658 /* are we done ? */
1659 if (curr_prot == protcnt) {
1660 alldone = 1;
1661 } else if (curr_prot < protcnt) {
1662 /* advance to next prot buffer */
1663 sgpe = sg_next(sgpe);
1664 bpl++;
1666 /* update the reference tag */
1667 reftag += protgrp_blks;
1668 } else {
1669 /* if we're here, we have a bug */
1670 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1671 "9054 BLKGRD: bug in %s\n", __func__);
1674 } while (!alldone);
1676 out:
1678 return num_bde;
1682 * Given a SCSI command that supports DIF, determine composition of protection
1683 * groups involved in setting up buffer lists
1685 * Returns:
1686 * for DIF (for both read and write)
1687 * */
1688 static int
1689 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
1691 int ret = LPFC_PG_TYPE_INVALID;
1692 unsigned char op = scsi_get_prot_op(sc);
1694 switch (op) {
1695 case SCSI_PROT_READ_STRIP:
1696 case SCSI_PROT_WRITE_INSERT:
1697 ret = LPFC_PG_TYPE_NO_DIF;
1698 break;
1699 case SCSI_PROT_READ_INSERT:
1700 case SCSI_PROT_WRITE_STRIP:
1701 case SCSI_PROT_READ_PASS:
1702 case SCSI_PROT_WRITE_PASS:
1703 ret = LPFC_PG_TYPE_DIF_BUF;
1704 break;
1705 default:
1706 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1707 "9021 Unsupported protection op:%d\n", op);
1708 break;
1711 return ret;
1715 * This is the protection/DIF aware version of
1716 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
1717 * two functions eventually, but for now, it's here
1719 static int
1720 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
1721 struct lpfc_scsi_buf *lpfc_cmd)
1723 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1724 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1725 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1726 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1727 uint32_t num_bde = 0;
1728 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
1729 int prot_group_type = 0;
1730 int diflen, fcpdl;
1731 unsigned blksize;
1734 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
1735 * fcp_rsp regions to the first data bde entry
1737 bpl += 2;
1738 if (scsi_sg_count(scsi_cmnd)) {
1740 * The driver stores the segment count returned from pci_map_sg
1741 * because this a count of dma-mappings used to map the use_sg
1742 * pages. They are not guaranteed to be the same for those
1743 * architectures that implement an IOMMU.
1745 datasegcnt = dma_map_sg(&phba->pcidev->dev,
1746 scsi_sglist(scsi_cmnd),
1747 scsi_sg_count(scsi_cmnd), datadir);
1748 if (unlikely(!datasegcnt))
1749 return 1;
1751 lpfc_cmd->seg_cnt = datasegcnt;
1752 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1753 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1754 "9067 BLKGRD: %s: Too many sg segments"
1755 " from dma_map_sg. Config %d, seg_cnt"
1756 " %d\n",
1757 __func__, phba->cfg_sg_seg_cnt,
1758 lpfc_cmd->seg_cnt);
1759 scsi_dma_unmap(scsi_cmnd);
1760 return 1;
1763 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
1765 switch (prot_group_type) {
1766 case LPFC_PG_TYPE_NO_DIF:
1767 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
1768 datasegcnt);
1769 /* we should have 2 or more entries in buffer list */
1770 if (num_bde < 2)
1771 goto err;
1772 break;
1773 case LPFC_PG_TYPE_DIF_BUF:{
1775 * This type indicates that protection buffers are
1776 * passed to the driver, so that needs to be prepared
1777 * for DMA
1779 protsegcnt = dma_map_sg(&phba->pcidev->dev,
1780 scsi_prot_sglist(scsi_cmnd),
1781 scsi_prot_sg_count(scsi_cmnd), datadir);
1782 if (unlikely(!protsegcnt)) {
1783 scsi_dma_unmap(scsi_cmnd);
1784 return 1;
1787 lpfc_cmd->prot_seg_cnt = protsegcnt;
1788 if (lpfc_cmd->prot_seg_cnt
1789 > phba->cfg_prot_sg_seg_cnt) {
1790 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1791 "9068 BLKGRD: %s: Too many prot sg "
1792 "segments from dma_map_sg. Config %d,"
1793 "prot_seg_cnt %d\n", __func__,
1794 phba->cfg_prot_sg_seg_cnt,
1795 lpfc_cmd->prot_seg_cnt);
1796 dma_unmap_sg(&phba->pcidev->dev,
1797 scsi_prot_sglist(scsi_cmnd),
1798 scsi_prot_sg_count(scsi_cmnd),
1799 datadir);
1800 scsi_dma_unmap(scsi_cmnd);
1801 return 1;
1804 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
1805 datasegcnt, protsegcnt);
1806 /* we should have 3 or more entries in buffer list */
1807 if (num_bde < 3)
1808 goto err;
1809 break;
1811 case LPFC_PG_TYPE_INVALID:
1812 default:
1813 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1814 "9022 Unexpected protection group %i\n",
1815 prot_group_type);
1816 return 1;
1821 * Finish initializing those IOCB fields that are dependent on the
1822 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
1823 * reinitialized since all iocb memory resources are used many times
1824 * for transmit, receive, and continuation bpl's.
1826 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
1827 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
1828 iocb_cmd->ulpBdeCount = 1;
1829 iocb_cmd->ulpLe = 1;
1831 fcpdl = scsi_bufflen(scsi_cmnd);
1833 if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
1835 * We are in DIF Type 1 mode
1836 * Every data block has a 8 byte DIF (trailer)
1837 * attached to it. Must ajust FCP data length
1839 blksize = lpfc_cmd_blksize(scsi_cmnd);
1840 diflen = (fcpdl / blksize) * 8;
1841 fcpdl += diflen;
1843 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
1846 * Due to difference in data length between DIF/non-DIF paths,
1847 * we need to set word 4 of IOCB here
1849 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
1851 return 0;
1852 err:
1853 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1854 "9023 Could not setup all needed BDE's"
1855 "prot_group_type=%d, num_bde=%d\n",
1856 prot_group_type, num_bde);
1857 return 1;
1861 * This function checks for BlockGuard errors detected by
1862 * the HBA. In case of errors, the ASC/ASCQ fields in the
1863 * sense buffer will be set accordingly, paired with
1864 * ILLEGAL_REQUEST to signal to the kernel that the HBA
1865 * detected corruption.
1867 * Returns:
1868 * 0 - No error found
1869 * 1 - BlockGuard error found
1870 * -1 - Internal error (bad profile, ...etc)
1872 static int
1873 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
1874 struct lpfc_iocbq *pIocbOut)
1876 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
1877 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
1878 int ret = 0;
1879 uint32_t bghm = bgf->bghm;
1880 uint32_t bgstat = bgf->bgstat;
1881 uint64_t failing_sector = 0;
1883 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
1884 " 0x%x lba 0x%llx blk cnt 0x%x "
1885 "bgstat=0x%x bghm=0x%x\n",
1886 cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
1887 blk_rq_sectors(cmd->request), bgstat, bghm);
1889 spin_lock(&_dump_buf_lock);
1890 if (!_dump_buf_done) {
1891 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
1892 " Data for %u blocks to debugfs\n",
1893 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1894 lpfc_debug_save_data(phba, cmd);
1896 /* If we have a prot sgl, save the DIF buffer */
1897 if (lpfc_prot_group_type(phba, cmd) ==
1898 LPFC_PG_TYPE_DIF_BUF) {
1899 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
1900 "Saving DIF for %u blocks to debugfs\n",
1901 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
1902 lpfc_debug_save_dif(phba, cmd);
1905 _dump_buf_done = 1;
1907 spin_unlock(&_dump_buf_lock);
1909 if (lpfc_bgs_get_invalid_prof(bgstat)) {
1910 cmd->result = ScsiResult(DID_ERROR, 0);
1911 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
1912 " BlockGuard profile. bgstat:0x%x\n",
1913 bgstat);
1914 ret = (-1);
1915 goto out;
1918 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
1919 cmd->result = ScsiResult(DID_ERROR, 0);
1920 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
1921 "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
1922 bgstat);
1923 ret = (-1);
1924 goto out;
1927 if (lpfc_bgs_get_guard_err(bgstat)) {
1928 ret = 1;
1930 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1931 0x10, 0x1);
1932 cmd->result = DRIVER_SENSE << 24
1933 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1934 phba->bg_guard_err_cnt++;
1935 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1936 "9055 BLKGRD: guard_tag error\n");
1939 if (lpfc_bgs_get_reftag_err(bgstat)) {
1940 ret = 1;
1942 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1943 0x10, 0x3);
1944 cmd->result = DRIVER_SENSE << 24
1945 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1947 phba->bg_reftag_err_cnt++;
1948 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1949 "9056 BLKGRD: ref_tag error\n");
1952 if (lpfc_bgs_get_apptag_err(bgstat)) {
1953 ret = 1;
1955 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
1956 0x10, 0x2);
1957 cmd->result = DRIVER_SENSE << 24
1958 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
1960 phba->bg_apptag_err_cnt++;
1961 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1962 "9061 BLKGRD: app_tag error\n");
1965 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
1967 * setup sense data descriptor 0 per SPC-4 as an information
1968 * field, and put the failing LBA in it.
1969 * This code assumes there was also a guard/app/ref tag error
1970 * indication.
1972 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
1973 cmd->sense_buffer[8] = 0; /* Information descriptor type */
1974 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
1975 cmd->sense_buffer[10] = 0x80; /* Validity bit */
1976 bghm /= cmd->device->sector_size;
1978 failing_sector = scsi_get_lba(cmd);
1979 failing_sector += bghm;
1981 /* Descriptor Information */
1982 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
1985 if (!ret) {
1986 /* No error was reported - problem in FW? */
1987 cmd->result = ScsiResult(DID_ERROR, 0);
1988 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1989 "9057 BLKGRD: no errors reported!\n");
1992 out:
1993 return ret;
1997 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
1998 * @phba: The Hba for which this call is being executed.
1999 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2001 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
2002 * field of @lpfc_cmd for device with SLI-4 interface spec.
2004 * Return codes:
2005 * 1 - Error
2006 * 0 - Success
2008 static int
2009 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2011 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2012 struct scatterlist *sgel = NULL;
2013 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2014 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
2015 struct sli4_sge *first_data_sgl;
2016 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2017 dma_addr_t physaddr;
2018 uint32_t num_bde = 0;
2019 uint32_t dma_len;
2020 uint32_t dma_offset = 0;
2021 int nseg;
2022 struct ulp_bde64 *bde;
2025 * There are three possibilities here - use scatter-gather segment, use
2026 * the single mapping, or neither. Start the lpfc command prep by
2027 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2028 * data bde entry.
2030 if (scsi_sg_count(scsi_cmnd)) {
2032 * The driver stores the segment count returned from pci_map_sg
2033 * because this a count of dma-mappings used to map the use_sg
2034 * pages. They are not guaranteed to be the same for those
2035 * architectures that implement an IOMMU.
2038 nseg = scsi_dma_map(scsi_cmnd);
2039 if (unlikely(!nseg))
2040 return 1;
2041 sgl += 1;
2042 /* clear the last flag in the fcp_rsp map entry */
2043 sgl->word2 = le32_to_cpu(sgl->word2);
2044 bf_set(lpfc_sli4_sge_last, sgl, 0);
2045 sgl->word2 = cpu_to_le32(sgl->word2);
2046 sgl += 1;
2047 first_data_sgl = sgl;
2048 lpfc_cmd->seg_cnt = nseg;
2049 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2050 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
2051 " %s: Too many sg segments from "
2052 "dma_map_sg. Config %d, seg_cnt %d\n",
2053 __func__, phba->cfg_sg_seg_cnt,
2054 lpfc_cmd->seg_cnt);
2055 scsi_dma_unmap(scsi_cmnd);
2056 return 1;
2060 * The driver established a maximum scatter-gather segment count
2061 * during probe that limits the number of sg elements in any
2062 * single scsi command. Just run through the seg_cnt and format
2063 * the sge's.
2064 * When using SLI-3 the driver will try to fit all the BDEs into
2065 * the IOCB. If it can't then the BDEs get added to a BPL as it
2066 * does for SLI-2 mode.
2068 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
2069 physaddr = sg_dma_address(sgel);
2070 dma_len = sg_dma_len(sgel);
2071 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2072 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2073 sgl->word2 = le32_to_cpu(sgl->word2);
2074 if ((num_bde + 1) == nseg)
2075 bf_set(lpfc_sli4_sge_last, sgl, 1);
2076 else
2077 bf_set(lpfc_sli4_sge_last, sgl, 0);
2078 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2079 sgl->word2 = cpu_to_le32(sgl->word2);
2080 sgl->sge_len = cpu_to_le32(dma_len);
2081 dma_offset += dma_len;
2082 sgl++;
2084 /* setup the performance hint (first data BDE) if enabled */
2085 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
2086 bde = (struct ulp_bde64 *)
2087 &(iocb_cmd->unsli3.sli3Words[5]);
2088 bde->addrLow = first_data_sgl->addr_lo;
2089 bde->addrHigh = first_data_sgl->addr_hi;
2090 bde->tus.f.bdeSize =
2091 le32_to_cpu(first_data_sgl->sge_len);
2092 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2093 bde->tus.w = cpu_to_le32(bde->tus.w);
2095 } else {
2096 sgl += 1;
2097 /* clear the last flag in the fcp_rsp map entry */
2098 sgl->word2 = le32_to_cpu(sgl->word2);
2099 bf_set(lpfc_sli4_sge_last, sgl, 1);
2100 sgl->word2 = cpu_to_le32(sgl->word2);
2104 * Finish initializing those IOCB fields that are dependent on the
2105 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
2106 * explicitly reinitialized.
2107 * all iocb memory resources are reused.
2109 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
2112 * Due to difference in data length between DIF/non-DIF paths,
2113 * we need to set word 4 of IOCB here
2115 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
2116 return 0;
2120 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
2121 * @phba: The Hba for which this call is being executed.
2122 * @lpfc_cmd: The scsi buffer which is going to be mapped.
2124 * This routine wraps the actual DMA mapping function pointer from the
2125 * lpfc_hba struct.
2127 * Return codes:
2128 * 1 - Error
2129 * 0 - Success
2131 static inline int
2132 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2134 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
2138 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
2139 * @phba: Pointer to hba context object.
2140 * @vport: Pointer to vport object.
2141 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
2142 * @rsp_iocb: Pointer to response iocb object which reported error.
2144 * This function posts an event when there is a SCSI command reporting
2145 * error from the scsi device.
2147 static void
2148 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
2149 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
2150 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2151 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2152 uint32_t resp_info = fcprsp->rspStatus2;
2153 uint32_t scsi_status = fcprsp->rspStatus3;
2154 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2155 struct lpfc_fast_path_event *fast_path_evt = NULL;
2156 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
2157 unsigned long flags;
2159 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2160 return;
2162 /* If there is queuefull or busy condition send a scsi event */
2163 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
2164 (cmnd->result == SAM_STAT_BUSY)) {
2165 fast_path_evt = lpfc_alloc_fast_evt(phba);
2166 if (!fast_path_evt)
2167 return;
2168 fast_path_evt->un.scsi_evt.event_type =
2169 FC_REG_SCSI_EVENT;
2170 fast_path_evt->un.scsi_evt.subcategory =
2171 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
2172 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
2173 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
2174 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
2175 &pnode->nlp_portname, sizeof(struct lpfc_name));
2176 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
2177 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2178 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
2179 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
2180 fast_path_evt = lpfc_alloc_fast_evt(phba);
2181 if (!fast_path_evt)
2182 return;
2183 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
2184 FC_REG_SCSI_EVENT;
2185 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
2186 LPFC_EVENT_CHECK_COND;
2187 fast_path_evt->un.check_cond_evt.scsi_event.lun =
2188 cmnd->device->lun;
2189 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
2190 &pnode->nlp_portname, sizeof(struct lpfc_name));
2191 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
2192 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2193 fast_path_evt->un.check_cond_evt.sense_key =
2194 cmnd->sense_buffer[2] & 0xf;
2195 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
2196 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
2197 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2198 fcpi_parm &&
2199 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
2200 ((scsi_status == SAM_STAT_GOOD) &&
2201 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
2203 * If status is good or resid does not match with fcp_param and
2204 * there is valid fcpi_parm, then there is a read_check error
2206 fast_path_evt = lpfc_alloc_fast_evt(phba);
2207 if (!fast_path_evt)
2208 return;
2209 fast_path_evt->un.read_check_error.header.event_type =
2210 FC_REG_FABRIC_EVENT;
2211 fast_path_evt->un.read_check_error.header.subcategory =
2212 LPFC_EVENT_FCPRDCHKERR;
2213 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
2214 &pnode->nlp_portname, sizeof(struct lpfc_name));
2215 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
2216 &pnode->nlp_nodename, sizeof(struct lpfc_name));
2217 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
2218 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
2219 fast_path_evt->un.read_check_error.fcpiparam =
2220 fcpi_parm;
2221 } else
2222 return;
2224 fast_path_evt->vport = vport;
2225 spin_lock_irqsave(&phba->hbalock, flags);
2226 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
2227 spin_unlock_irqrestore(&phba->hbalock, flags);
2228 lpfc_worker_wake_up(phba);
2229 return;
2233 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
2234 * @phba: The HBA for which this call is being executed.
2235 * @psb: The scsi buffer which is going to be un-mapped.
2237 * This routine does DMA un-mapping of scatter gather list of scsi command
2238 * field of @lpfc_cmd for device with SLI-3 interface spec.
2240 static void
2241 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
2244 * There are only two special cases to consider. (1) the scsi command
2245 * requested scatter-gather usage or (2) the scsi command allocated
2246 * a request buffer, but did not request use_sg. There is a third
2247 * case, but it does not require resource deallocation.
2249 if (psb->seg_cnt > 0)
2250 scsi_dma_unmap(psb->pCmd);
2251 if (psb->prot_seg_cnt > 0)
2252 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
2253 scsi_prot_sg_count(psb->pCmd),
2254 psb->pCmd->sc_data_direction);
2258 * lpfc_handler_fcp_err - FCP response handler
2259 * @vport: The virtual port for which this call is being executed.
2260 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2261 * @rsp_iocb: The response IOCB which contains FCP error.
2263 * This routine is called to process response IOCB with status field
2264 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
2265 * based upon SCSI and FCP error.
2267 static void
2268 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2269 struct lpfc_iocbq *rsp_iocb)
2271 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
2272 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
2273 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
2274 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
2275 uint32_t resp_info = fcprsp->rspStatus2;
2276 uint32_t scsi_status = fcprsp->rspStatus3;
2277 uint32_t *lp;
2278 uint32_t host_status = DID_OK;
2279 uint32_t rsplen = 0;
2280 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
2284 * If this is a task management command, there is no
2285 * scsi packet associated with this lpfc_cmd. The driver
2286 * consumes it.
2288 if (fcpcmd->fcpCntl2) {
2289 scsi_status = 0;
2290 goto out;
2293 if (resp_info & RSP_LEN_VALID) {
2294 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2295 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
2296 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2297 "2719 Invalid response length: "
2298 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
2299 cmnd->device->id,
2300 cmnd->device->lun, cmnd->cmnd[0],
2301 rsplen);
2302 host_status = DID_ERROR;
2303 goto out;
2305 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
2306 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
2307 "2757 Protocol failure detected during "
2308 "processing of FCP I/O op: "
2309 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
2310 cmnd->device->id,
2311 cmnd->device->lun, cmnd->cmnd[0],
2312 fcprsp->rspInfo3);
2313 host_status = DID_ERROR;
2314 goto out;
2318 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
2319 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
2320 if (snslen > SCSI_SENSE_BUFFERSIZE)
2321 snslen = SCSI_SENSE_BUFFERSIZE;
2323 if (resp_info & RSP_LEN_VALID)
2324 rsplen = be32_to_cpu(fcprsp->rspRspLen);
2325 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
2327 lp = (uint32_t *)cmnd->sense_buffer;
2329 if (!scsi_status && (resp_info & RESID_UNDER))
2330 logit = LOG_FCP;
2332 lpfc_printf_vlog(vport, KERN_WARNING, logit,
2333 "9024 FCP command x%x failed: x%x SNS x%x x%x "
2334 "Data: x%x x%x x%x x%x x%x\n",
2335 cmnd->cmnd[0], scsi_status,
2336 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
2337 be32_to_cpu(fcprsp->rspResId),
2338 be32_to_cpu(fcprsp->rspSnsLen),
2339 be32_to_cpu(fcprsp->rspRspLen),
2340 fcprsp->rspInfo3);
2342 scsi_set_resid(cmnd, 0);
2343 if (resp_info & RESID_UNDER) {
2344 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
2346 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2347 "9025 FCP Read Underrun, expected %d, "
2348 "residual %d Data: x%x x%x x%x\n",
2349 be32_to_cpu(fcpcmd->fcpDl),
2350 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
2351 cmnd->underflow);
2354 * If there is an under run check if under run reported by
2355 * storage array is same as the under run reported by HBA.
2356 * If this is not same, there is a dropped frame.
2358 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
2359 fcpi_parm &&
2360 (scsi_get_resid(cmnd) != fcpi_parm)) {
2361 lpfc_printf_vlog(vport, KERN_WARNING,
2362 LOG_FCP | LOG_FCP_ERROR,
2363 "9026 FCP Read Check Error "
2364 "and Underrun Data: x%x x%x x%x x%x\n",
2365 be32_to_cpu(fcpcmd->fcpDl),
2366 scsi_get_resid(cmnd), fcpi_parm,
2367 cmnd->cmnd[0]);
2368 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
2369 host_status = DID_ERROR;
2372 * The cmnd->underflow is the minimum number of bytes that must
2373 * be transferred for this command. Provided a sense condition
2374 * is not present, make sure the actual amount transferred is at
2375 * least the underflow value or fail.
2377 if (!(resp_info & SNS_LEN_VALID) &&
2378 (scsi_status == SAM_STAT_GOOD) &&
2379 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
2380 < cmnd->underflow)) {
2381 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2382 "9027 FCP command x%x residual "
2383 "underrun converted to error "
2384 "Data: x%x x%x x%x\n",
2385 cmnd->cmnd[0], scsi_bufflen(cmnd),
2386 scsi_get_resid(cmnd), cmnd->underflow);
2387 host_status = DID_ERROR;
2389 } else if (resp_info & RESID_OVER) {
2390 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2391 "9028 FCP command x%x residual overrun error. "
2392 "Data: x%x x%x\n", cmnd->cmnd[0],
2393 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
2394 host_status = DID_ERROR;
2397 * Check SLI validation that all the transfer was actually done
2398 * (fcpi_parm should be zero). Apply check only to reads.
2400 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
2401 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
2402 "9029 FCP Read Check Error Data: "
2403 "x%x x%x x%x x%x x%x\n",
2404 be32_to_cpu(fcpcmd->fcpDl),
2405 be32_to_cpu(fcprsp->rspResId),
2406 fcpi_parm, cmnd->cmnd[0], scsi_status);
2407 switch (scsi_status) {
2408 case SAM_STAT_GOOD:
2409 case SAM_STAT_CHECK_CONDITION:
2410 /* Fabric dropped a data frame. Fail any successful
2411 * command in which we detected dropped frames.
2412 * A status of good or some check conditions could
2413 * be considered a successful command.
2415 host_status = DID_ERROR;
2416 break;
2418 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
2421 out:
2422 cmnd->result = ScsiResult(host_status, scsi_status);
2423 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
2427 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
2428 * @phba: The Hba for which this call is being executed.
2429 * @pIocbIn: The command IOCBQ for the scsi cmnd.
2430 * @pIocbOut: The response IOCBQ for the scsi cmnd.
2432 * This routine assigns scsi command result by looking into response IOCB
2433 * status field appropriately. This routine handles QUEUE FULL condition as
2434 * well by ramping down device queue depth.
2436 static void
2437 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
2438 struct lpfc_iocbq *pIocbOut)
2440 struct lpfc_scsi_buf *lpfc_cmd =
2441 (struct lpfc_scsi_buf *) pIocbIn->context1;
2442 struct lpfc_vport *vport = pIocbIn->vport;
2443 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2444 struct lpfc_nodelist *pnode = rdata->pnode;
2445 struct scsi_cmnd *cmd;
2446 int result;
2447 struct scsi_device *tmp_sdev;
2448 int depth;
2449 unsigned long flags;
2450 struct lpfc_fast_path_event *fast_path_evt;
2451 struct Scsi_Host *shost;
2452 uint32_t queue_depth, scsi_id;
2454 /* Sanity check on return of outstanding command */
2455 if (!(lpfc_cmd->pCmd))
2456 return;
2457 cmd = lpfc_cmd->pCmd;
2458 shost = cmd->device->host;
2460 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
2461 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
2462 /* pick up SLI4 exhange busy status from HBA */
2463 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
2465 if (pnode && NLP_CHK_NODE_ACT(pnode))
2466 atomic_dec(&pnode->cmd_pending);
2468 if (lpfc_cmd->status) {
2469 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
2470 (lpfc_cmd->result & IOERR_DRVR_MASK))
2471 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
2472 else if (lpfc_cmd->status >= IOSTAT_CNT)
2473 lpfc_cmd->status = IOSTAT_DEFAULT;
2475 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2476 "9030 FCP cmd x%x failed <%d/%d> "
2477 "status: x%x result: x%x Data: x%x x%x\n",
2478 cmd->cmnd[0],
2479 cmd->device ? cmd->device->id : 0xffff,
2480 cmd->device ? cmd->device->lun : 0xffff,
2481 lpfc_cmd->status, lpfc_cmd->result,
2482 pIocbOut->iocb.ulpContext,
2483 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
2485 switch (lpfc_cmd->status) {
2486 case IOSTAT_FCP_RSP_ERROR:
2487 /* Call FCP RSP handler to determine result */
2488 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
2489 break;
2490 case IOSTAT_NPORT_BSY:
2491 case IOSTAT_FABRIC_BSY:
2492 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
2493 fast_path_evt = lpfc_alloc_fast_evt(phba);
2494 if (!fast_path_evt)
2495 break;
2496 fast_path_evt->un.fabric_evt.event_type =
2497 FC_REG_FABRIC_EVENT;
2498 fast_path_evt->un.fabric_evt.subcategory =
2499 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
2500 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
2501 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2502 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
2503 &pnode->nlp_portname,
2504 sizeof(struct lpfc_name));
2505 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
2506 &pnode->nlp_nodename,
2507 sizeof(struct lpfc_name));
2509 fast_path_evt->vport = vport;
2510 fast_path_evt->work_evt.evt =
2511 LPFC_EVT_FASTPATH_MGMT_EVT;
2512 spin_lock_irqsave(&phba->hbalock, flags);
2513 list_add_tail(&fast_path_evt->work_evt.evt_listp,
2514 &phba->work_list);
2515 spin_unlock_irqrestore(&phba->hbalock, flags);
2516 lpfc_worker_wake_up(phba);
2517 break;
2518 case IOSTAT_LOCAL_REJECT:
2519 case IOSTAT_REMOTE_STOP:
2520 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
2521 lpfc_cmd->result ==
2522 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
2523 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
2524 lpfc_cmd->result ==
2525 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
2526 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
2527 break;
2529 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
2530 lpfc_cmd->result == IOERR_NO_RESOURCES ||
2531 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
2532 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
2533 cmd->result = ScsiResult(DID_REQUEUE, 0);
2534 break;
2536 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
2537 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
2538 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
2539 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
2541 * This is a response for a BG enabled
2542 * cmd. Parse BG error
2544 lpfc_parse_bg_err(phba, lpfc_cmd,
2545 pIocbOut);
2546 break;
2547 } else {
2548 lpfc_printf_vlog(vport, KERN_WARNING,
2549 LOG_BG,
2550 "9031 non-zero BGSTAT "
2551 "on unprotected cmd\n");
2554 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
2555 && (phba->sli_rev == LPFC_SLI_REV4)
2556 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
2557 /* This IO was aborted by the target, we don't
2558 * know the rxid and because we did not send the
2559 * ABTS we cannot generate and RRQ.
2561 lpfc_set_rrq_active(phba, pnode,
2562 lpfc_cmd->cur_iocbq.sli4_xritag,
2563 0, 0);
2565 /* else: fall through */
2566 default:
2567 cmd->result = ScsiResult(DID_ERROR, 0);
2568 break;
2571 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
2572 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
2573 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
2574 SAM_STAT_BUSY);
2575 } else
2576 cmd->result = ScsiResult(DID_OK, 0);
2578 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
2579 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
2581 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
2582 "0710 Iodone <%d/%d> cmd %p, error "
2583 "x%x SNS x%x x%x Data: x%x x%x\n",
2584 cmd->device->id, cmd->device->lun, cmd,
2585 cmd->result, *lp, *(lp + 3), cmd->retries,
2586 scsi_get_resid(cmd));
2589 lpfc_update_stats(phba, lpfc_cmd);
2590 result = cmd->result;
2591 if (vport->cfg_max_scsicmpl_time &&
2592 time_after(jiffies, lpfc_cmd->start_time +
2593 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
2594 spin_lock_irqsave(shost->host_lock, flags);
2595 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2596 if (pnode->cmd_qdepth >
2597 atomic_read(&pnode->cmd_pending) &&
2598 (atomic_read(&pnode->cmd_pending) >
2599 LPFC_MIN_TGT_QDEPTH) &&
2600 ((cmd->cmnd[0] == READ_10) ||
2601 (cmd->cmnd[0] == WRITE_10)))
2602 pnode->cmd_qdepth =
2603 atomic_read(&pnode->cmd_pending);
2605 pnode->last_change_time = jiffies;
2607 spin_unlock_irqrestore(shost->host_lock, flags);
2608 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
2609 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
2610 time_after(jiffies, pnode->last_change_time +
2611 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
2612 spin_lock_irqsave(shost->host_lock, flags);
2613 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
2614 / 100;
2615 depth = depth ? depth : 1;
2616 pnode->cmd_qdepth += depth;
2617 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
2618 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
2619 pnode->last_change_time = jiffies;
2620 spin_unlock_irqrestore(shost->host_lock, flags);
2624 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
2626 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
2627 queue_depth = cmd->device->queue_depth;
2628 scsi_id = cmd->device->id;
2629 cmd->scsi_done(cmd);
2631 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
2633 * If there is a thread waiting for command completion
2634 * wake up the thread.
2636 spin_lock_irqsave(shost->host_lock, flags);
2637 lpfc_cmd->pCmd = NULL;
2638 if (lpfc_cmd->waitq)
2639 wake_up(lpfc_cmd->waitq);
2640 spin_unlock_irqrestore(shost->host_lock, flags);
2641 lpfc_release_scsi_buf(phba, lpfc_cmd);
2642 return;
2645 if (!result)
2646 lpfc_rampup_queue_depth(vport, queue_depth);
2649 * Check for queue full. If the lun is reporting queue full, then
2650 * back off the lun queue depth to prevent target overloads.
2652 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
2653 NLP_CHK_NODE_ACT(pnode)) {
2654 shost_for_each_device(tmp_sdev, shost) {
2655 if (tmp_sdev->id != scsi_id)
2656 continue;
2657 depth = scsi_track_queue_full(tmp_sdev,
2658 tmp_sdev->queue_depth-1);
2659 if (depth <= 0)
2660 continue;
2661 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
2662 "0711 detected queue full - lun queue "
2663 "depth adjusted to %d.\n", depth);
2664 lpfc_send_sdev_queuedepth_change_event(phba, vport,
2665 pnode,
2666 tmp_sdev->lun,
2667 depth+1, depth);
2672 * If there is a thread waiting for command completion
2673 * wake up the thread.
2675 spin_lock_irqsave(shost->host_lock, flags);
2676 lpfc_cmd->pCmd = NULL;
2677 if (lpfc_cmd->waitq)
2678 wake_up(lpfc_cmd->waitq);
2679 spin_unlock_irqrestore(shost->host_lock, flags);
2681 lpfc_release_scsi_buf(phba, lpfc_cmd);
2685 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
2686 * @data: A pointer to the immediate command data portion of the IOCB.
2687 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
2689 * The routine copies the entire FCP command from @fcp_cmnd to @data while
2690 * byte swapping the data to big endian format for transmission on the wire.
2692 static void
2693 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
2695 int i, j;
2696 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
2697 i += sizeof(uint32_t), j++) {
2698 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
2703 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
2704 * @vport: The virtual port for which this call is being executed.
2705 * @lpfc_cmd: The scsi command which needs to send.
2706 * @pnode: Pointer to lpfc_nodelist.
2708 * This routine initializes fcp_cmnd and iocb data structure from scsi command
2709 * to transfer for device with SLI3 interface spec.
2711 static void
2712 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
2713 struct lpfc_nodelist *pnode)
2715 struct lpfc_hba *phba = vport->phba;
2716 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2717 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2718 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2719 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
2720 int datadir = scsi_cmnd->sc_data_direction;
2721 char tag[2];
2723 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
2724 return;
2726 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
2727 /* clear task management bits */
2728 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
2730 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
2731 &lpfc_cmd->fcp_cmnd->fcp_lun);
2733 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
2735 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
2736 switch (tag[0]) {
2737 case HEAD_OF_QUEUE_TAG:
2738 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
2739 break;
2740 case ORDERED_QUEUE_TAG:
2741 fcp_cmnd->fcpCntl1 = ORDERED_Q;
2742 break;
2743 default:
2744 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
2745 break;
2747 } else
2748 fcp_cmnd->fcpCntl1 = 0;
2751 * There are three possibilities here - use scatter-gather segment, use
2752 * the single mapping, or neither. Start the lpfc command prep by
2753 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
2754 * data bde entry.
2756 if (scsi_sg_count(scsi_cmnd)) {
2757 if (datadir == DMA_TO_DEVICE) {
2758 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
2759 if (phba->sli_rev < LPFC_SLI_REV4) {
2760 iocb_cmd->un.fcpi.fcpi_parm = 0;
2761 iocb_cmd->ulpPU = 0;
2762 } else
2763 iocb_cmd->ulpPU = PARM_READ_CHECK;
2764 fcp_cmnd->fcpCntl3 = WRITE_DATA;
2765 phba->fc4OutputRequests++;
2766 } else {
2767 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
2768 iocb_cmd->ulpPU = PARM_READ_CHECK;
2769 fcp_cmnd->fcpCntl3 = READ_DATA;
2770 phba->fc4InputRequests++;
2772 } else {
2773 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
2774 iocb_cmd->un.fcpi.fcpi_parm = 0;
2775 iocb_cmd->ulpPU = 0;
2776 fcp_cmnd->fcpCntl3 = 0;
2777 phba->fc4ControlRequests++;
2779 if (phba->sli_rev == 3 &&
2780 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
2781 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
2783 * Finish initializing those IOCB fields that are independent
2784 * of the scsi_cmnd request_buffer
2786 piocbq->iocb.ulpContext = pnode->nlp_rpi;
2787 if (phba->sli_rev == LPFC_SLI_REV4)
2788 piocbq->iocb.ulpContext =
2789 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
2790 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
2791 piocbq->iocb.ulpFCP2Rcvy = 1;
2792 else
2793 piocbq->iocb.ulpFCP2Rcvy = 0;
2795 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
2796 piocbq->context1 = lpfc_cmd;
2797 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2798 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2799 piocbq->vport = vport;
2803 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
2804 * @vport: The virtual port for which this call is being executed.
2805 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
2806 * @lun: Logical unit number.
2807 * @task_mgmt_cmd: SCSI task management command.
2809 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
2810 * for device with SLI-3 interface spec.
2812 * Return codes:
2813 * 0 - Error
2814 * 1 - Success
2816 static int
2817 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
2818 struct lpfc_scsi_buf *lpfc_cmd,
2819 unsigned int lun,
2820 uint8_t task_mgmt_cmd)
2822 struct lpfc_iocbq *piocbq;
2823 IOCB_t *piocb;
2824 struct fcp_cmnd *fcp_cmnd;
2825 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
2826 struct lpfc_nodelist *ndlp = rdata->pnode;
2828 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
2829 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
2830 return 0;
2832 piocbq = &(lpfc_cmd->cur_iocbq);
2833 piocbq->vport = vport;
2835 piocb = &piocbq->iocb;
2837 fcp_cmnd = lpfc_cmd->fcp_cmnd;
2838 /* Clear out any old data in the FCP command area */
2839 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
2840 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
2841 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
2842 if (vport->phba->sli_rev == 3 &&
2843 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
2844 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
2845 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
2846 piocb->ulpContext = ndlp->nlp_rpi;
2847 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
2848 piocb->ulpContext =
2849 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
2851 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
2852 piocb->ulpFCP2Rcvy = 1;
2854 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
2856 /* ulpTimeout is only one byte */
2857 if (lpfc_cmd->timeout > 0xff) {
2859 * Do not timeout the command at the firmware level.
2860 * The driver will provide the timeout mechanism.
2862 piocb->ulpTimeout = 0;
2863 } else
2864 piocb->ulpTimeout = lpfc_cmd->timeout;
2866 if (vport->phba->sli_rev == LPFC_SLI_REV4)
2867 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
2869 return 1;
2873 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
2874 * @phba: The hba struct for which this call is being executed.
2875 * @dev_grp: The HBA PCI-Device group number.
2877 * This routine sets up the SCSI interface API function jump table in @phba
2878 * struct.
2879 * Returns: 0 - success, -ENODEV - failure.
2882 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
2885 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
2886 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
2888 switch (dev_grp) {
2889 case LPFC_PCI_DEV_LP:
2890 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
2891 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
2892 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
2893 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
2894 break;
2895 case LPFC_PCI_DEV_OC:
2896 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
2897 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
2898 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
2899 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
2900 break;
2901 default:
2902 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
2903 "1418 Invalid HBA PCI-device group: 0x%x\n",
2904 dev_grp);
2905 return -ENODEV;
2906 break;
2908 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
2909 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
2910 return 0;
2914 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
2915 * @phba: The Hba for which this call is being executed.
2916 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
2917 * @rspiocbq: Pointer to lpfc_iocbq data structure.
2919 * This routine is IOCB completion routine for device reset and target reset
2920 * routine. This routine release scsi buffer associated with lpfc_cmd.
2922 static void
2923 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
2924 struct lpfc_iocbq *cmdiocbq,
2925 struct lpfc_iocbq *rspiocbq)
2927 struct lpfc_scsi_buf *lpfc_cmd =
2928 (struct lpfc_scsi_buf *) cmdiocbq->context1;
2929 if (lpfc_cmd)
2930 lpfc_release_scsi_buf(phba, lpfc_cmd);
2931 return;
2935 * lpfc_info - Info entry point of scsi_host_template data structure
2936 * @host: The scsi host for which this call is being executed.
2938 * This routine provides module information about hba.
2940 * Reutrn code:
2941 * Pointer to char - Success.
2943 const char *
2944 lpfc_info(struct Scsi_Host *host)
2946 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
2947 struct lpfc_hba *phba = vport->phba;
2948 int len;
2949 static char lpfcinfobuf[384];
2951 memset(lpfcinfobuf,0,384);
2952 if (phba && phba->pcidev){
2953 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
2954 len = strlen(lpfcinfobuf);
2955 snprintf(lpfcinfobuf + len,
2956 384-len,
2957 " on PCI bus %02x device %02x irq %d",
2958 phba->pcidev->bus->number,
2959 phba->pcidev->devfn,
2960 phba->pcidev->irq);
2961 len = strlen(lpfcinfobuf);
2962 if (phba->Port[0]) {
2963 snprintf(lpfcinfobuf + len,
2964 384-len,
2965 " port %s",
2966 phba->Port);
2968 len = strlen(lpfcinfobuf);
2969 if (phba->sli4_hba.link_state.logical_speed) {
2970 snprintf(lpfcinfobuf + len,
2971 384-len,
2972 " Logical Link Speed: %d Mbps",
2973 phba->sli4_hba.link_state.logical_speed * 10);
2976 return lpfcinfobuf;
2980 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
2981 * @phba: The Hba for which this call is being executed.
2983 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
2984 * The default value of cfg_poll_tmo is 10 milliseconds.
2986 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
2988 unsigned long poll_tmo_expires =
2989 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
2991 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
2992 mod_timer(&phba->fcp_poll_timer,
2993 poll_tmo_expires);
2997 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
2998 * @phba: The Hba for which this call is being executed.
3000 * This routine starts the fcp_poll_timer of @phba.
3002 void lpfc_poll_start_timer(struct lpfc_hba * phba)
3004 lpfc_poll_rearm_timer(phba);
3008 * lpfc_poll_timeout - Restart polling timer
3009 * @ptr: Map to lpfc_hba data structure pointer.
3011 * This routine restarts fcp_poll timer, when FCP ring polling is enable
3012 * and FCP Ring interrupt is disable.
3015 void lpfc_poll_timeout(unsigned long ptr)
3017 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
3019 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3020 lpfc_sli_handle_fast_ring_event(phba,
3021 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3023 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3024 lpfc_poll_rearm_timer(phba);
3029 * lpfc_queuecommand - scsi_host_template queuecommand entry point
3030 * @cmnd: Pointer to scsi_cmnd data structure.
3031 * @done: Pointer to done routine.
3033 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
3034 * This routine prepares an IOCB from scsi command and provides to firmware.
3035 * The @done callback is invoked after driver finished processing the command.
3037 * Return value :
3038 * 0 - Success
3039 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
3041 static int
3042 lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
3044 struct Scsi_Host *shost = cmnd->device->host;
3045 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3046 struct lpfc_hba *phba = vport->phba;
3047 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3048 struct lpfc_nodelist *ndlp;
3049 struct lpfc_scsi_buf *lpfc_cmd;
3050 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
3051 int err;
3053 err = fc_remote_port_chkready(rport);
3054 if (err) {
3055 cmnd->result = err;
3056 goto out_fail_command;
3058 ndlp = rdata->pnode;
3060 if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
3061 scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
3063 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
3064 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
3065 " op:%02x str=%s without registering for"
3066 " BlockGuard - Rejecting command\n",
3067 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3068 dif_op_str[scsi_get_prot_op(cmnd)]);
3069 goto out_fail_command;
3073 * Catch race where our node has transitioned, but the
3074 * transport is still transitioning.
3076 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
3077 cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
3078 goto out_fail_command;
3080 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
3081 goto out_tgt_busy;
3083 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
3084 if (lpfc_cmd == NULL) {
3085 lpfc_rampdown_queue_depth(phba);
3087 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3088 "0707 driver's buffer pool is empty, "
3089 "IO busied\n");
3090 goto out_host_busy;
3094 * Store the midlayer's command structure for the completion phase
3095 * and complete the command initialization.
3097 lpfc_cmd->pCmd = cmnd;
3098 lpfc_cmd->rdata = rdata;
3099 lpfc_cmd->timeout = 0;
3100 lpfc_cmd->start_time = jiffies;
3101 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
3102 cmnd->scsi_done = done;
3104 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
3105 if (vport->phba->cfg_enable_bg) {
3106 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3107 "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
3108 "str=%s\n",
3109 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3110 dif_op_str[scsi_get_prot_op(cmnd)]);
3111 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3112 "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
3113 "%02x %02x %02x %02x %02x\n",
3114 cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
3115 cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
3116 cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
3117 cmnd->cmnd[9]);
3118 if (cmnd->cmnd[0] == READ_10)
3119 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3120 "9035 BLKGRD: READ @ sector %llu, "
3121 "count %u\n",
3122 (unsigned long long)scsi_get_lba(cmnd),
3123 blk_rq_sectors(cmnd->request));
3124 else if (cmnd->cmnd[0] == WRITE_10)
3125 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3126 "9036 BLKGRD: WRITE @ sector %llu, "
3127 "count %u cmd=%p\n",
3128 (unsigned long long)scsi_get_lba(cmnd),
3129 blk_rq_sectors(cmnd->request),
3130 cmnd);
3133 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3134 } else {
3135 if (vport->phba->cfg_enable_bg) {
3136 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3137 "9038 BLKGRD: rcvd unprotected cmd:"
3138 "%02x op:%02x str=%s\n",
3139 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
3140 dif_op_str[scsi_get_prot_op(cmnd)]);
3141 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3142 "9039 BLKGRD: CDB: %02x %02x %02x "
3143 "%02x %02x %02x %02x %02x %02x %02x\n",
3144 cmnd->cmnd[0], cmnd->cmnd[1],
3145 cmnd->cmnd[2], cmnd->cmnd[3],
3146 cmnd->cmnd[4], cmnd->cmnd[5],
3147 cmnd->cmnd[6], cmnd->cmnd[7],
3148 cmnd->cmnd[8], cmnd->cmnd[9]);
3149 if (cmnd->cmnd[0] == READ_10)
3150 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3151 "9040 dbg: READ @ sector %llu, "
3152 "count %u\n",
3153 (unsigned long long)scsi_get_lba(cmnd),
3154 blk_rq_sectors(cmnd->request));
3155 else if (cmnd->cmnd[0] == WRITE_10)
3156 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3157 "9041 dbg: WRITE @ sector %llu, "
3158 "count %u cmd=%p\n",
3159 (unsigned long long)scsi_get_lba(cmnd),
3160 blk_rq_sectors(cmnd->request), cmnd);
3161 else
3162 lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
3163 "9042 dbg: parser not implemented\n");
3165 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3168 if (err)
3169 goto out_host_busy_free_buf;
3171 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
3173 atomic_inc(&ndlp->cmd_pending);
3174 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
3175 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
3176 if (err) {
3177 atomic_dec(&ndlp->cmd_pending);
3178 goto out_host_busy_free_buf;
3180 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3181 spin_unlock(shost->host_lock);
3182 lpfc_sli_handle_fast_ring_event(phba,
3183 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3185 spin_lock(shost->host_lock);
3186 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3187 lpfc_poll_rearm_timer(phba);
3190 return 0;
3192 out_host_busy_free_buf:
3193 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
3194 lpfc_release_scsi_buf(phba, lpfc_cmd);
3195 out_host_busy:
3196 return SCSI_MLQUEUE_HOST_BUSY;
3198 out_tgt_busy:
3199 return SCSI_MLQUEUE_TARGET_BUSY;
3201 out_fail_command:
3202 done(cmnd);
3203 return 0;
3206 static DEF_SCSI_QCMD(lpfc_queuecommand)
3209 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
3210 * @cmnd: Pointer to scsi_cmnd data structure.
3212 * This routine aborts @cmnd pending in base driver.
3214 * Return code :
3215 * 0x2003 - Error
3216 * 0x2002 - Success
3218 static int
3219 lpfc_abort_handler(struct scsi_cmnd *cmnd)
3221 struct Scsi_Host *shost = cmnd->device->host;
3222 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3223 struct lpfc_hba *phba = vport->phba;
3224 struct lpfc_iocbq *iocb;
3225 struct lpfc_iocbq *abtsiocb;
3226 struct lpfc_scsi_buf *lpfc_cmd;
3227 IOCB_t *cmd, *icmd;
3228 int ret = SUCCESS;
3229 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
3231 ret = fc_block_scsi_eh(cmnd);
3232 if (ret)
3233 return ret;
3234 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
3235 if (!lpfc_cmd) {
3236 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3237 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
3238 "x%x ID %d LUN %d\n",
3239 ret, cmnd->device->id, cmnd->device->lun);
3240 return SUCCESS;
3244 * If pCmd field of the corresponding lpfc_scsi_buf structure
3245 * points to a different SCSI command, then the driver has
3246 * already completed this command, but the midlayer did not
3247 * see the completion before the eh fired. Just return
3248 * SUCCESS.
3250 iocb = &lpfc_cmd->cur_iocbq;
3251 if (lpfc_cmd->pCmd != cmnd)
3252 goto out;
3254 BUG_ON(iocb->context1 != lpfc_cmd);
3256 abtsiocb = lpfc_sli_get_iocbq(phba);
3257 if (abtsiocb == NULL) {
3258 ret = FAILED;
3259 goto out;
3263 * The scsi command can not be in txq and it is in flight because the
3264 * pCmd is still pointig at the SCSI command we have to abort. There
3265 * is no need to search the txcmplq. Just send an abort to the FW.
3268 cmd = &iocb->iocb;
3269 icmd = &abtsiocb->iocb;
3270 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
3271 icmd->un.acxri.abortContextTag = cmd->ulpContext;
3272 if (phba->sli_rev == LPFC_SLI_REV4)
3273 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
3274 else
3275 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
3277 icmd->ulpLe = 1;
3278 icmd->ulpClass = cmd->ulpClass;
3280 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
3281 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
3282 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
3284 if (lpfc_is_link_up(phba))
3285 icmd->ulpCommand = CMD_ABORT_XRI_CN;
3286 else
3287 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
3289 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
3290 abtsiocb->vport = vport;
3291 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
3292 IOCB_ERROR) {
3293 lpfc_sli_release_iocbq(phba, abtsiocb);
3294 ret = FAILED;
3295 goto out;
3298 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3299 lpfc_sli_handle_fast_ring_event(phba,
3300 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3302 lpfc_cmd->waitq = &waitq;
3303 /* Wait for abort to complete */
3304 wait_event_timeout(waitq,
3305 (lpfc_cmd->pCmd != cmnd),
3306 (2*vport->cfg_devloss_tmo*HZ));
3308 spin_lock_irq(shost->host_lock);
3309 lpfc_cmd->waitq = NULL;
3310 spin_unlock_irq(shost->host_lock);
3312 if (lpfc_cmd->pCmd == cmnd) {
3313 ret = FAILED;
3314 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3315 "0748 abort handler timed out waiting "
3316 "for abort to complete: ret %#x, ID %d, "
3317 "LUN %d\n",
3318 ret, cmnd->device->id, cmnd->device->lun);
3321 out:
3322 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3323 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
3324 "LUN %d\n", ret, cmnd->device->id,
3325 cmnd->device->lun);
3326 return ret;
3329 static char *
3330 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
3332 switch (task_mgmt_cmd) {
3333 case FCP_ABORT_TASK_SET:
3334 return "ABORT_TASK_SET";
3335 case FCP_CLEAR_TASK_SET:
3336 return "FCP_CLEAR_TASK_SET";
3337 case FCP_BUS_RESET:
3338 return "FCP_BUS_RESET";
3339 case FCP_LUN_RESET:
3340 return "FCP_LUN_RESET";
3341 case FCP_TARGET_RESET:
3342 return "FCP_TARGET_RESET";
3343 case FCP_CLEAR_ACA:
3344 return "FCP_CLEAR_ACA";
3345 case FCP_TERMINATE_TASK:
3346 return "FCP_TERMINATE_TASK";
3347 default:
3348 return "unknown";
3353 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
3354 * @vport: The virtual port for which this call is being executed.
3355 * @rdata: Pointer to remote port local data
3356 * @tgt_id: Target ID of remote device.
3357 * @lun_id: Lun number for the TMF
3358 * @task_mgmt_cmd: type of TMF to send
3360 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
3361 * a remote port.
3363 * Return Code:
3364 * 0x2003 - Error
3365 * 0x2002 - Success.
3367 static int
3368 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
3369 unsigned tgt_id, unsigned int lun_id,
3370 uint8_t task_mgmt_cmd)
3372 struct lpfc_hba *phba = vport->phba;
3373 struct lpfc_scsi_buf *lpfc_cmd;
3374 struct lpfc_iocbq *iocbq;
3375 struct lpfc_iocbq *iocbqrsp;
3376 struct lpfc_nodelist *pnode = rdata->pnode;
3377 int ret;
3378 int status;
3380 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3381 return FAILED;
3383 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
3384 if (lpfc_cmd == NULL)
3385 return FAILED;
3386 lpfc_cmd->timeout = 60;
3387 lpfc_cmd->rdata = rdata;
3389 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
3390 task_mgmt_cmd);
3391 if (!status) {
3392 lpfc_release_scsi_buf(phba, lpfc_cmd);
3393 return FAILED;
3396 iocbq = &lpfc_cmd->cur_iocbq;
3397 iocbqrsp = lpfc_sli_get_iocbq(phba);
3398 if (iocbqrsp == NULL) {
3399 lpfc_release_scsi_buf(phba, lpfc_cmd);
3400 return FAILED;
3403 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3404 "0702 Issue %s to TGT %d LUN %d "
3405 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
3406 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
3407 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
3408 iocbq->iocb_flag);
3410 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
3411 iocbq, iocbqrsp, lpfc_cmd->timeout);
3412 if (status != IOCB_SUCCESS) {
3413 if (status == IOCB_TIMEDOUT) {
3414 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
3415 ret = TIMEOUT_ERROR;
3416 } else
3417 ret = FAILED;
3418 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3419 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3420 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
3421 "iocb_flag x%x\n",
3422 lpfc_taskmgmt_name(task_mgmt_cmd),
3423 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
3424 iocbqrsp->iocb.un.ulpWord[4],
3425 iocbq->iocb_flag);
3426 } else if (status == IOCB_BUSY)
3427 ret = FAILED;
3428 else
3429 ret = SUCCESS;
3431 lpfc_sli_release_iocbq(phba, iocbqrsp);
3433 if (ret != TIMEOUT_ERROR)
3434 lpfc_release_scsi_buf(phba, lpfc_cmd);
3436 return ret;
3440 * lpfc_chk_tgt_mapped -
3441 * @vport: The virtual port to check on
3442 * @cmnd: Pointer to scsi_cmnd data structure.
3444 * This routine delays until the scsi target (aka rport) for the
3445 * command exists (is present and logged in) or we declare it non-existent.
3447 * Return code :
3448 * 0x2003 - Error
3449 * 0x2002 - Success
3451 static int
3452 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
3454 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3455 struct lpfc_nodelist *pnode;
3456 unsigned long later;
3458 if (!rdata) {
3459 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3460 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
3461 return FAILED;
3463 pnode = rdata->pnode;
3465 * If target is not in a MAPPED state, delay until
3466 * target is rediscovered or devloss timeout expires.
3468 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3469 while (time_after(later, jiffies)) {
3470 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3471 return FAILED;
3472 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
3473 return SUCCESS;
3474 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
3475 rdata = cmnd->device->hostdata;
3476 if (!rdata)
3477 return FAILED;
3478 pnode = rdata->pnode;
3480 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
3481 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3482 return FAILED;
3483 return SUCCESS;
3487 * lpfc_reset_flush_io_context -
3488 * @vport: The virtual port (scsi_host) for the flush context
3489 * @tgt_id: If aborting by Target contect - specifies the target id
3490 * @lun_id: If aborting by Lun context - specifies the lun id
3491 * @context: specifies the context level to flush at.
3493 * After a reset condition via TMF, we need to flush orphaned i/o
3494 * contexts from the adapter. This routine aborts any contexts
3495 * outstanding, then waits for their completions. The wait is
3496 * bounded by devloss_tmo though.
3498 * Return code :
3499 * 0x2003 - Error
3500 * 0x2002 - Success
3502 static int
3503 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
3504 uint64_t lun_id, lpfc_ctx_cmd context)
3506 struct lpfc_hba *phba = vport->phba;
3507 unsigned long later;
3508 int cnt;
3510 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3511 if (cnt)
3512 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
3513 tgt_id, lun_id, context);
3514 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
3515 while (time_after(later, jiffies) && cnt) {
3516 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
3517 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
3519 if (cnt) {
3520 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3521 "0724 I/O flush failure for context %s : cnt x%x\n",
3522 ((context == LPFC_CTX_LUN) ? "LUN" :
3523 ((context == LPFC_CTX_TGT) ? "TGT" :
3524 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
3525 cnt);
3526 return FAILED;
3528 return SUCCESS;
3532 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
3533 * @cmnd: Pointer to scsi_cmnd data structure.
3535 * This routine does a device reset by sending a LUN_RESET task management
3536 * command.
3538 * Return code :
3539 * 0x2003 - Error
3540 * 0x2002 - Success
3542 static int
3543 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
3545 struct Scsi_Host *shost = cmnd->device->host;
3546 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3547 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3548 struct lpfc_nodelist *pnode;
3549 unsigned tgt_id = cmnd->device->id;
3550 unsigned int lun_id = cmnd->device->lun;
3551 struct lpfc_scsi_event_header scsi_event;
3552 int status;
3554 if (!rdata) {
3555 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3556 "0798 Device Reset rport failure: rdata x%p\n", rdata);
3557 return FAILED;
3559 pnode = rdata->pnode;
3560 status = fc_block_scsi_eh(cmnd);
3561 if (status)
3562 return status;
3564 status = lpfc_chk_tgt_mapped(vport, cmnd);
3565 if (status == FAILED) {
3566 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3567 "0721 Device Reset rport failure: rdata x%p\n", rdata);
3568 return FAILED;
3571 scsi_event.event_type = FC_REG_SCSI_EVENT;
3572 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
3573 scsi_event.lun = lun_id;
3574 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3575 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3577 fc_host_post_vendor_event(shost, fc_get_event_number(),
3578 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3580 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3581 FCP_LUN_RESET);
3583 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3584 "0713 SCSI layer issued Device Reset (%d, %d) "
3585 "return x%x\n", tgt_id, lun_id, status);
3588 * We have to clean up i/o as : they may be orphaned by the TMF;
3589 * or if the TMF failed, they may be in an indeterminate state.
3590 * So, continue on.
3591 * We will report success if all the i/o aborts successfully.
3593 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3594 LPFC_CTX_LUN);
3595 return status;
3599 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
3600 * @cmnd: Pointer to scsi_cmnd data structure.
3602 * This routine does a target reset by sending a TARGET_RESET task management
3603 * command.
3605 * Return code :
3606 * 0x2003 - Error
3607 * 0x2002 - Success
3609 static int
3610 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
3612 struct Scsi_Host *shost = cmnd->device->host;
3613 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3614 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
3615 struct lpfc_nodelist *pnode;
3616 unsigned tgt_id = cmnd->device->id;
3617 unsigned int lun_id = cmnd->device->lun;
3618 struct lpfc_scsi_event_header scsi_event;
3619 int status;
3621 if (!rdata) {
3622 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3623 "0799 Target Reset rport failure: rdata x%p\n", rdata);
3624 return FAILED;
3626 pnode = rdata->pnode;
3627 status = fc_block_scsi_eh(cmnd);
3628 if (status)
3629 return status;
3631 status = lpfc_chk_tgt_mapped(vport, cmnd);
3632 if (status == FAILED) {
3633 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3634 "0722 Target Reset rport failure: rdata x%p\n", rdata);
3635 return FAILED;
3638 scsi_event.event_type = FC_REG_SCSI_EVENT;
3639 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
3640 scsi_event.lun = 0;
3641 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
3642 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
3644 fc_host_post_vendor_event(shost, fc_get_event_number(),
3645 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3647 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
3648 FCP_TARGET_RESET);
3650 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3651 "0723 SCSI layer issued Target Reset (%d, %d) "
3652 "return x%x\n", tgt_id, lun_id, status);
3655 * We have to clean up i/o as : they may be orphaned by the TMF;
3656 * or if the TMF failed, they may be in an indeterminate state.
3657 * So, continue on.
3658 * We will report success if all the i/o aborts successfully.
3660 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
3661 LPFC_CTX_TGT);
3662 return status;
3666 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
3667 * @cmnd: Pointer to scsi_cmnd data structure.
3669 * This routine does target reset to all targets on @cmnd->device->host.
3670 * This emulates Parallel SCSI Bus Reset Semantics.
3672 * Return code :
3673 * 0x2003 - Error
3674 * 0x2002 - Success
3676 static int
3677 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
3679 struct Scsi_Host *shost = cmnd->device->host;
3680 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
3681 struct lpfc_nodelist *ndlp = NULL;
3682 struct lpfc_scsi_event_header scsi_event;
3683 int match;
3684 int ret = SUCCESS, status, i;
3686 scsi_event.event_type = FC_REG_SCSI_EVENT;
3687 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
3688 scsi_event.lun = 0;
3689 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
3690 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
3692 fc_host_post_vendor_event(shost, fc_get_event_number(),
3693 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
3695 ret = fc_block_scsi_eh(cmnd);
3696 if (ret)
3697 return ret;
3700 * Since the driver manages a single bus device, reset all
3701 * targets known to the driver. Should any target reset
3702 * fail, this routine returns failure to the midlayer.
3704 for (i = 0; i < LPFC_MAX_TARGET; i++) {
3705 /* Search for mapped node by target ID */
3706 match = 0;
3707 spin_lock_irq(shost->host_lock);
3708 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
3709 if (!NLP_CHK_NODE_ACT(ndlp))
3710 continue;
3711 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
3712 ndlp->nlp_sid == i &&
3713 ndlp->rport) {
3714 match = 1;
3715 break;
3718 spin_unlock_irq(shost->host_lock);
3719 if (!match)
3720 continue;
3722 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
3723 i, 0, FCP_TARGET_RESET);
3725 if (status != SUCCESS) {
3726 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3727 "0700 Bus Reset on target %d failed\n",
3729 ret = FAILED;
3733 * We have to clean up i/o as : they may be orphaned by the TMFs
3734 * above; or if any of the TMFs failed, they may be in an
3735 * indeterminate state.
3736 * We will report success if all the i/o aborts successfully.
3739 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
3740 if (status != SUCCESS)
3741 ret = FAILED;
3743 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3744 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
3745 return ret;
3749 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
3750 * @sdev: Pointer to scsi_device.
3752 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
3753 * globally available list of scsi buffers. This routine also makes sure scsi
3754 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
3755 * of scsi buffer exists for the lifetime of the driver.
3757 * Return codes:
3758 * non-0 - Error
3759 * 0 - Success
3761 static int
3762 lpfc_slave_alloc(struct scsi_device *sdev)
3764 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3765 struct lpfc_hba *phba = vport->phba;
3766 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
3767 uint32_t total = 0;
3768 uint32_t num_to_alloc = 0;
3769 int num_allocated = 0;
3770 uint32_t sdev_cnt;
3772 if (!rport || fc_remote_port_chkready(rport))
3773 return -ENXIO;
3775 sdev->hostdata = rport->dd_data;
3776 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
3779 * Populate the cmds_per_lun count scsi_bufs into this host's globally
3780 * available list of scsi buffers. Don't allocate more than the
3781 * HBA limit conveyed to the midlayer via the host structure. The
3782 * formula accounts for the lun_queue_depth + error handlers + 1
3783 * extra. This list of scsi bufs exists for the lifetime of the driver.
3785 total = phba->total_scsi_bufs;
3786 num_to_alloc = vport->cfg_lun_queue_depth + 2;
3788 /* If allocated buffers are enough do nothing */
3789 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
3790 return 0;
3792 /* Allow some exchanges to be available always to complete discovery */
3793 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
3794 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3795 "0704 At limitation of %d preallocated "
3796 "command buffers\n", total);
3797 return 0;
3798 /* Allow some exchanges to be available always to complete discovery */
3799 } else if (total + num_to_alloc >
3800 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
3801 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3802 "0705 Allocation request of %d "
3803 "command buffers will exceed max of %d. "
3804 "Reducing allocation request to %d.\n",
3805 num_to_alloc, phba->cfg_hba_queue_depth,
3806 (phba->cfg_hba_queue_depth - total));
3807 num_to_alloc = phba->cfg_hba_queue_depth - total;
3809 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
3810 if (num_to_alloc != num_allocated) {
3811 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3812 "0708 Allocation request of %d "
3813 "command buffers did not succeed. "
3814 "Allocated %d buffers.\n",
3815 num_to_alloc, num_allocated);
3817 if (num_allocated > 0)
3818 phba->total_scsi_bufs += num_allocated;
3819 return 0;
3823 * lpfc_slave_configure - scsi_host_template slave_configure entry point
3824 * @sdev: Pointer to scsi_device.
3826 * This routine configures following items
3827 * - Tag command queuing support for @sdev if supported.
3828 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
3830 * Return codes:
3831 * 0 - Success
3833 static int
3834 lpfc_slave_configure(struct scsi_device *sdev)
3836 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3837 struct lpfc_hba *phba = vport->phba;
3839 if (sdev->tagged_supported)
3840 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
3841 else
3842 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
3844 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
3845 lpfc_sli_handle_fast_ring_event(phba,
3846 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
3847 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
3848 lpfc_poll_rearm_timer(phba);
3851 return 0;
3855 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
3856 * @sdev: Pointer to scsi_device.
3858 * This routine sets @sdev hostatdata filed to null.
3860 static void
3861 lpfc_slave_destroy(struct scsi_device *sdev)
3863 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
3864 struct lpfc_hba *phba = vport->phba;
3865 atomic_dec(&phba->sdev_cnt);
3866 sdev->hostdata = NULL;
3867 return;
3871 struct scsi_host_template lpfc_template = {
3872 .module = THIS_MODULE,
3873 .name = LPFC_DRIVER_NAME,
3874 .info = lpfc_info,
3875 .queuecommand = lpfc_queuecommand,
3876 .eh_abort_handler = lpfc_abort_handler,
3877 .eh_device_reset_handler = lpfc_device_reset_handler,
3878 .eh_target_reset_handler = lpfc_target_reset_handler,
3879 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3880 .slave_alloc = lpfc_slave_alloc,
3881 .slave_configure = lpfc_slave_configure,
3882 .slave_destroy = lpfc_slave_destroy,
3883 .scan_finished = lpfc_scan_finished,
3884 .this_id = -1,
3885 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3886 .cmd_per_lun = LPFC_CMD_PER_LUN,
3887 .use_clustering = ENABLE_CLUSTERING,
3888 .shost_attrs = lpfc_hba_attrs,
3889 .max_sectors = 0xFFFF,
3890 .vendor_id = LPFC_NL_VENDOR_ID,
3891 .change_queue_depth = lpfc_change_queue_depth,
3894 struct scsi_host_template lpfc_vport_template = {
3895 .module = THIS_MODULE,
3896 .name = LPFC_DRIVER_NAME,
3897 .info = lpfc_info,
3898 .queuecommand = lpfc_queuecommand,
3899 .eh_abort_handler = lpfc_abort_handler,
3900 .eh_device_reset_handler = lpfc_device_reset_handler,
3901 .eh_target_reset_handler = lpfc_target_reset_handler,
3902 .eh_bus_reset_handler = lpfc_bus_reset_handler,
3903 .slave_alloc = lpfc_slave_alloc,
3904 .slave_configure = lpfc_slave_configure,
3905 .slave_destroy = lpfc_slave_destroy,
3906 .scan_finished = lpfc_scan_finished,
3907 .this_id = -1,
3908 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
3909 .cmd_per_lun = LPFC_CMD_PER_LUN,
3910 .use_clustering = ENABLE_CLUSTERING,
3911 .shost_attrs = lpfc_vport_attrs,
3912 .max_sectors = 0xFFFF,
3913 .change_queue_depth = lpfc_change_queue_depth,