x86/xen: resume timer irqs early
[linux/fpc-iii.git] / drivers / scsi / lpfc / lpfc_scsi.c
blobc913e8cc3b269ca82096efd6e356dc8e9e635de9
1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2013 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
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>
27 #include <linux/crc-t10dif.h>
28 #include <net/checksum.h>
30 #include <scsi/scsi.h>
31 #include <scsi/scsi_device.h>
32 #include <scsi/scsi_eh.h>
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_tcq.h>
35 #include <scsi/scsi_transport_fc.h>
37 #include "lpfc_version.h"
38 #include "lpfc_hw4.h"
39 #include "lpfc_hw.h"
40 #include "lpfc_sli.h"
41 #include "lpfc_sli4.h"
42 #include "lpfc_nl.h"
43 #include "lpfc_disc.h"
44 #include "lpfc.h"
45 #include "lpfc_scsi.h"
46 #include "lpfc_logmsg.h"
47 #include "lpfc_crtn.h"
48 #include "lpfc_vport.h"
50 #define LPFC_RESET_WAIT 2
51 #define LPFC_ABORT_WAIT 2
53 int _dump_buf_done = 1;
55 static char *dif_op_str[] = {
56 "PROT_NORMAL",
57 "PROT_READ_INSERT",
58 "PROT_WRITE_STRIP",
59 "PROT_READ_STRIP",
60 "PROT_WRITE_INSERT",
61 "PROT_READ_PASS",
62 "PROT_WRITE_PASS",
65 struct scsi_dif_tuple {
66 __be16 guard_tag; /* Checksum */
67 __be16 app_tag; /* Opaque storage */
68 __be32 ref_tag; /* Target LBA or indirect LBA */
71 static void
72 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
73 static void
74 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
75 static int
76 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
78 static void
79 lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
81 void *src, *dst;
82 struct scatterlist *sgde = scsi_sglist(cmnd);
84 if (!_dump_buf_data) {
85 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
86 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
87 __func__);
88 return;
92 if (!sgde) {
93 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
94 "9051 BLKGRD: ERROR: data scatterlist is null\n");
95 return;
98 dst = (void *) _dump_buf_data;
99 while (sgde) {
100 src = sg_virt(sgde);
101 memcpy(dst, src, sgde->length);
102 dst += sgde->length;
103 sgde = sg_next(sgde);
107 static void
108 lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
110 void *src, *dst;
111 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
113 if (!_dump_buf_dif) {
114 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
115 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
116 __func__);
117 return;
120 if (!sgde) {
121 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
122 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
123 return;
126 dst = _dump_buf_dif;
127 while (sgde) {
128 src = sg_virt(sgde);
129 memcpy(dst, src, sgde->length);
130 dst += sgde->length;
131 sgde = sg_next(sgde);
135 static inline unsigned
136 lpfc_cmd_blksize(struct scsi_cmnd *sc)
138 return sc->device->sector_size;
141 #define LPFC_CHECK_PROTECT_GUARD 1
142 #define LPFC_CHECK_PROTECT_REF 2
143 static inline unsigned
144 lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
146 return 1;
149 static inline unsigned
150 lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
152 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
153 return 0;
154 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
155 return 1;
156 return 0;
160 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
161 * @phba: Pointer to HBA object.
162 * @lpfc_cmd: lpfc scsi command object pointer.
164 * This function is called from the lpfc_prep_task_mgmt_cmd function to
165 * set the last bit in the response sge entry.
167 static void
168 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
169 struct lpfc_scsi_buf *lpfc_cmd)
171 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
172 if (sgl) {
173 sgl += 1;
174 sgl->word2 = le32_to_cpu(sgl->word2);
175 bf_set(lpfc_sli4_sge_last, sgl, 1);
176 sgl->word2 = cpu_to_le32(sgl->word2);
181 * lpfc_update_stats - Update statistical data for the command completion
182 * @phba: Pointer to HBA object.
183 * @lpfc_cmd: lpfc scsi command object pointer.
185 * This function is called when there is a command completion and this
186 * function updates the statistical data for the command completion.
188 static void
189 lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
191 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
192 struct lpfc_nodelist *pnode = rdata->pnode;
193 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
194 unsigned long flags;
195 struct Scsi_Host *shost = cmd->device->host;
196 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
197 unsigned long latency;
198 int i;
200 if (cmd->result)
201 return;
203 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
205 spin_lock_irqsave(shost->host_lock, flags);
206 if (!vport->stat_data_enabled ||
207 vport->stat_data_blocked ||
208 !pnode ||
209 !pnode->lat_data ||
210 (phba->bucket_type == LPFC_NO_BUCKET)) {
211 spin_unlock_irqrestore(shost->host_lock, flags);
212 return;
215 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
216 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
217 phba->bucket_step;
218 /* check array subscript bounds */
219 if (i < 0)
220 i = 0;
221 else if (i >= LPFC_MAX_BUCKET_COUNT)
222 i = LPFC_MAX_BUCKET_COUNT - 1;
223 } else {
224 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
225 if (latency <= (phba->bucket_base +
226 ((1<<i)*phba->bucket_step)))
227 break;
230 pnode->lat_data[i].cmd_count++;
231 spin_unlock_irqrestore(shost->host_lock, flags);
235 * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
236 * @phba: Pointer to HBA context object.
237 * @vport: Pointer to vport object.
238 * @ndlp: Pointer to FC node associated with the target.
239 * @lun: Lun number of the scsi device.
240 * @old_val: Old value of the queue depth.
241 * @new_val: New value of the queue depth.
243 * This function sends an event to the mgmt application indicating
244 * there is a change in the scsi device queue depth.
246 static void
247 lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
248 struct lpfc_vport *vport,
249 struct lpfc_nodelist *ndlp,
250 uint32_t lun,
251 uint32_t old_val,
252 uint32_t new_val)
254 struct lpfc_fast_path_event *fast_path_evt;
255 unsigned long flags;
257 fast_path_evt = lpfc_alloc_fast_evt(phba);
258 if (!fast_path_evt)
259 return;
261 fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
262 FC_REG_SCSI_EVENT;
263 fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
264 LPFC_EVENT_VARQUEDEPTH;
266 /* Report all luns with change in queue depth */
267 fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
268 if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
269 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
270 &ndlp->nlp_portname, sizeof(struct lpfc_name));
271 memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
272 &ndlp->nlp_nodename, sizeof(struct lpfc_name));
275 fast_path_evt->un.queue_depth_evt.oldval = old_val;
276 fast_path_evt->un.queue_depth_evt.newval = new_val;
277 fast_path_evt->vport = vport;
279 fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
280 spin_lock_irqsave(&phba->hbalock, flags);
281 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
282 spin_unlock_irqrestore(&phba->hbalock, flags);
283 lpfc_worker_wake_up(phba);
285 return;
289 * lpfc_change_queue_depth - Alter scsi device queue depth
290 * @sdev: Pointer the scsi device on which to change the queue depth.
291 * @qdepth: New queue depth to set the sdev to.
292 * @reason: The reason for the queue depth change.
294 * This function is called by the midlayer and the LLD to alter the queue
295 * depth for a scsi device. This function sets the queue depth to the new
296 * value and sends an event out to log the queue depth change.
299 lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
301 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
302 struct lpfc_hba *phba = vport->phba;
303 struct lpfc_rport_data *rdata;
304 unsigned long new_queue_depth, old_queue_depth;
306 old_queue_depth = sdev->queue_depth;
307 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
308 new_queue_depth = sdev->queue_depth;
309 rdata = sdev->hostdata;
310 if (rdata)
311 lpfc_send_sdev_queuedepth_change_event(phba, vport,
312 rdata->pnode, sdev->lun,
313 old_queue_depth,
314 new_queue_depth);
315 return sdev->queue_depth;
319 * lpfc_change_queue_type() - Change a device's scsi tag queuing type
320 * @sdev: Pointer the scsi device whose queue depth is to change
321 * @tag_type: Identifier for queue tag type
323 static int
324 lpfc_change_queue_type(struct scsi_device *sdev, int tag_type)
326 if (sdev->tagged_supported) {
327 scsi_set_tag_type(sdev, tag_type);
328 if (tag_type)
329 scsi_activate_tcq(sdev, sdev->queue_depth);
330 else
331 scsi_deactivate_tcq(sdev, sdev->queue_depth);
332 } else
333 tag_type = 0;
335 return tag_type;
339 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
340 * @phba: The Hba for which this call is being executed.
342 * This routine is called when there is resource error in driver or firmware.
343 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
344 * posts at most 1 event each second. This routine wakes up worker thread of
345 * @phba to process WORKER_RAM_DOWN_EVENT event.
347 * This routine should be called with no lock held.
349 void
350 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
352 unsigned long flags;
353 uint32_t evt_posted;
355 spin_lock_irqsave(&phba->hbalock, flags);
356 atomic_inc(&phba->num_rsrc_err);
357 phba->last_rsrc_error_time = jiffies;
359 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
360 spin_unlock_irqrestore(&phba->hbalock, flags);
361 return;
364 phba->last_ramp_down_time = jiffies;
366 spin_unlock_irqrestore(&phba->hbalock, flags);
368 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
369 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
370 if (!evt_posted)
371 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
372 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
374 if (!evt_posted)
375 lpfc_worker_wake_up(phba);
376 return;
380 * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
381 * @phba: The Hba for which this call is being executed.
383 * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
384 * post at most 1 event every 5 minute after last_ramp_up_time or
385 * last_rsrc_error_time. This routine wakes up worker thread of @phba
386 * to process WORKER_RAM_DOWN_EVENT event.
388 * This routine should be called with no lock held.
390 static inline void
391 lpfc_rampup_queue_depth(struct lpfc_vport *vport,
392 uint32_t queue_depth)
394 unsigned long flags;
395 struct lpfc_hba *phba = vport->phba;
396 uint32_t evt_posted;
397 atomic_inc(&phba->num_cmd_success);
399 if (vport->cfg_lun_queue_depth <= queue_depth)
400 return;
401 spin_lock_irqsave(&phba->hbalock, flags);
402 if (time_before(jiffies,
403 phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
404 time_before(jiffies,
405 phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
406 spin_unlock_irqrestore(&phba->hbalock, flags);
407 return;
409 phba->last_ramp_up_time = jiffies;
410 spin_unlock_irqrestore(&phba->hbalock, flags);
412 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
413 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
414 if (!evt_posted)
415 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
416 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
418 if (!evt_posted)
419 lpfc_worker_wake_up(phba);
420 return;
424 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
425 * @phba: The Hba for which this call is being executed.
427 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
428 * thread.This routine reduces queue depth for all scsi device on each vport
429 * associated with @phba.
431 void
432 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
434 struct lpfc_vport **vports;
435 struct Scsi_Host *shost;
436 struct scsi_device *sdev;
437 unsigned long new_queue_depth;
438 unsigned long num_rsrc_err, num_cmd_success;
439 int i;
441 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
442 num_cmd_success = atomic_read(&phba->num_cmd_success);
445 * The error and success command counters are global per
446 * driver instance. If another handler has already
447 * operated on this error event, just exit.
449 if (num_rsrc_err == 0)
450 return;
452 vports = lpfc_create_vport_work_array(phba);
453 if (vports != NULL)
454 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
455 shost = lpfc_shost_from_vport(vports[i]);
456 shost_for_each_device(sdev, shost) {
457 new_queue_depth =
458 sdev->queue_depth * num_rsrc_err /
459 (num_rsrc_err + num_cmd_success);
460 if (!new_queue_depth)
461 new_queue_depth = sdev->queue_depth - 1;
462 else
463 new_queue_depth = sdev->queue_depth -
464 new_queue_depth;
465 lpfc_change_queue_depth(sdev, new_queue_depth,
466 SCSI_QDEPTH_DEFAULT);
469 lpfc_destroy_vport_work_array(phba, vports);
470 atomic_set(&phba->num_rsrc_err, 0);
471 atomic_set(&phba->num_cmd_success, 0);
475 * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
476 * @phba: The Hba for which this call is being executed.
478 * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
479 * thread.This routine increases queue depth for all scsi device on each vport
480 * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
481 * num_cmd_success to zero.
483 void
484 lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
486 struct lpfc_vport **vports;
487 struct Scsi_Host *shost;
488 struct scsi_device *sdev;
489 int i;
491 vports = lpfc_create_vport_work_array(phba);
492 if (vports != NULL)
493 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
494 shost = lpfc_shost_from_vport(vports[i]);
495 shost_for_each_device(sdev, shost) {
496 if (vports[i]->cfg_lun_queue_depth <=
497 sdev->queue_depth)
498 continue;
499 lpfc_change_queue_depth(sdev,
500 sdev->queue_depth+1,
501 SCSI_QDEPTH_RAMP_UP);
504 lpfc_destroy_vport_work_array(phba, vports);
505 atomic_set(&phba->num_rsrc_err, 0);
506 atomic_set(&phba->num_cmd_success, 0);
510 * lpfc_scsi_dev_block - set all scsi hosts to block state
511 * @phba: Pointer to HBA context object.
513 * This function walks vport list and set each SCSI host to block state
514 * by invoking fc_remote_port_delete() routine. This function is invoked
515 * with EEH when device's PCI slot has been permanently disabled.
517 void
518 lpfc_scsi_dev_block(struct lpfc_hba *phba)
520 struct lpfc_vport **vports;
521 struct Scsi_Host *shost;
522 struct scsi_device *sdev;
523 struct fc_rport *rport;
524 int i;
526 vports = lpfc_create_vport_work_array(phba);
527 if (vports != NULL)
528 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
529 shost = lpfc_shost_from_vport(vports[i]);
530 shost_for_each_device(sdev, shost) {
531 rport = starget_to_rport(scsi_target(sdev));
532 fc_remote_port_delete(rport);
535 lpfc_destroy_vport_work_array(phba, vports);
539 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
540 * @vport: The virtual port for which this call being executed.
541 * @num_to_allocate: The requested number of buffers to allocate.
543 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
544 * the scsi buffer contains all the necessary information needed to initiate
545 * a SCSI I/O. The non-DMAable buffer region contains information to build
546 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
547 * and the initial BPL. In addition to allocating memory, the FCP CMND and
548 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
550 * Return codes:
551 * int - number of scsi buffers that were allocated.
552 * 0 = failure, less than num_to_alloc is a partial failure.
554 static int
555 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
557 struct lpfc_hba *phba = vport->phba;
558 struct lpfc_scsi_buf *psb;
559 struct ulp_bde64 *bpl;
560 IOCB_t *iocb;
561 dma_addr_t pdma_phys_fcp_cmd;
562 dma_addr_t pdma_phys_fcp_rsp;
563 dma_addr_t pdma_phys_bpl;
564 uint16_t iotag;
565 int bcnt, bpl_size;
567 bpl_size = phba->cfg_sg_dma_buf_size -
568 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
570 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
571 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
572 num_to_alloc, phba->cfg_sg_dma_buf_size,
573 (int)sizeof(struct fcp_cmnd),
574 (int)sizeof(struct fcp_rsp), bpl_size);
576 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
577 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
578 if (!psb)
579 break;
582 * Get memory from the pci pool to map the virt space to pci
583 * bus space for an I/O. The DMA buffer includes space for the
584 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
585 * necessary to support the sg_tablesize.
587 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
588 GFP_KERNEL, &psb->dma_handle);
589 if (!psb->data) {
590 kfree(psb);
591 break;
594 /* Initialize virtual ptrs to dma_buf region. */
595 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
597 /* Allocate iotag for psb->cur_iocbq. */
598 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
599 if (iotag == 0) {
600 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
601 psb->data, psb->dma_handle);
602 kfree(psb);
603 break;
605 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
607 psb->fcp_cmnd = psb->data;
608 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
609 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
610 sizeof(struct fcp_rsp);
612 /* Initialize local short-hand pointers. */
613 bpl = psb->fcp_bpl;
614 pdma_phys_fcp_cmd = psb->dma_handle;
615 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
616 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
617 sizeof(struct fcp_rsp);
620 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
621 * are sg list bdes. Initialize the first two and leave the
622 * rest for queuecommand.
624 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
625 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
626 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
627 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
628 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
630 /* Setup the physical region for the FCP RSP */
631 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
632 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
633 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
634 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
635 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
638 * Since the IOCB for the FCP I/O is built into this
639 * lpfc_scsi_buf, initialize it with all known data now.
641 iocb = &psb->cur_iocbq.iocb;
642 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
643 if ((phba->sli_rev == 3) &&
644 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
645 /* fill in immediate fcp command BDE */
646 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
647 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
648 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
649 unsli3.fcp_ext.icd);
650 iocb->un.fcpi64.bdl.addrHigh = 0;
651 iocb->ulpBdeCount = 0;
652 iocb->ulpLe = 0;
653 /* fill in response BDE */
654 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
655 BUFF_TYPE_BDE_64;
656 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
657 sizeof(struct fcp_rsp);
658 iocb->unsli3.fcp_ext.rbde.addrLow =
659 putPaddrLow(pdma_phys_fcp_rsp);
660 iocb->unsli3.fcp_ext.rbde.addrHigh =
661 putPaddrHigh(pdma_phys_fcp_rsp);
662 } else {
663 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
664 iocb->un.fcpi64.bdl.bdeSize =
665 (2 * sizeof(struct ulp_bde64));
666 iocb->un.fcpi64.bdl.addrLow =
667 putPaddrLow(pdma_phys_bpl);
668 iocb->un.fcpi64.bdl.addrHigh =
669 putPaddrHigh(pdma_phys_bpl);
670 iocb->ulpBdeCount = 1;
671 iocb->ulpLe = 1;
673 iocb->ulpClass = CLASS3;
674 psb->status = IOSTAT_SUCCESS;
675 /* Put it back into the SCSI buffer list */
676 psb->cur_iocbq.context1 = psb;
677 lpfc_release_scsi_buf_s3(phba, psb);
681 return bcnt;
685 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
686 * @vport: pointer to lpfc vport data structure.
688 * This routine is invoked by the vport cleanup for deletions and the cleanup
689 * for an ndlp on removal.
691 void
692 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
694 struct lpfc_hba *phba = vport->phba;
695 struct lpfc_scsi_buf *psb, *next_psb;
696 unsigned long iflag = 0;
698 spin_lock_irqsave(&phba->hbalock, iflag);
699 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
700 list_for_each_entry_safe(psb, next_psb,
701 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
702 if (psb->rdata && psb->rdata->pnode
703 && psb->rdata->pnode->vport == vport)
704 psb->rdata = NULL;
706 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
707 spin_unlock_irqrestore(&phba->hbalock, iflag);
711 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
712 * @phba: pointer to lpfc hba data structure.
713 * @axri: pointer to the fcp xri abort wcqe structure.
715 * This routine is invoked by the worker thread to process a SLI4 fast-path
716 * FCP aborted xri.
718 void
719 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
720 struct sli4_wcqe_xri_aborted *axri)
722 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
723 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
724 struct lpfc_scsi_buf *psb, *next_psb;
725 unsigned long iflag = 0;
726 struct lpfc_iocbq *iocbq;
727 int i;
728 struct lpfc_nodelist *ndlp;
729 int rrq_empty = 0;
730 struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
732 spin_lock_irqsave(&phba->hbalock, iflag);
733 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
734 list_for_each_entry_safe(psb, next_psb,
735 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
736 if (psb->cur_iocbq.sli4_xritag == xri) {
737 list_del(&psb->list);
738 psb->exch_busy = 0;
739 psb->status = IOSTAT_SUCCESS;
740 spin_unlock(
741 &phba->sli4_hba.abts_scsi_buf_list_lock);
742 if (psb->rdata && psb->rdata->pnode)
743 ndlp = psb->rdata->pnode;
744 else
745 ndlp = NULL;
747 rrq_empty = list_empty(&phba->active_rrq_list);
748 spin_unlock_irqrestore(&phba->hbalock, iflag);
749 if (ndlp) {
750 lpfc_set_rrq_active(phba, ndlp,
751 psb->cur_iocbq.sli4_lxritag, rxid, 1);
752 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
754 lpfc_release_scsi_buf_s4(phba, psb);
755 if (rrq_empty)
756 lpfc_worker_wake_up(phba);
757 return;
760 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
761 for (i = 1; i <= phba->sli.last_iotag; i++) {
762 iocbq = phba->sli.iocbq_lookup[i];
764 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
765 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
766 continue;
767 if (iocbq->sli4_xritag != xri)
768 continue;
769 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
770 psb->exch_busy = 0;
771 spin_unlock_irqrestore(&phba->hbalock, iflag);
772 if (!list_empty(&pring->txq))
773 lpfc_worker_wake_up(phba);
774 return;
777 spin_unlock_irqrestore(&phba->hbalock, iflag);
781 * lpfc_sli4_post_scsi_sgl_list - Psot blocks of scsi buffer sgls from a list
782 * @phba: pointer to lpfc hba data structure.
783 * @post_sblist: pointer to the scsi buffer list.
785 * This routine walks a list of scsi buffers that was passed in. It attempts
786 * to construct blocks of scsi buffer sgls which contains contiguous xris and
787 * uses the non-embedded SGL block post mailbox commands to post to the port.
788 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
789 * embedded SGL post mailbox command for posting. The @post_sblist passed in
790 * must be local list, thus no lock is needed when manipulate the list.
792 * Returns: 0 = failure, non-zero number of successfully posted buffers.
795 lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
796 struct list_head *post_sblist, int sb_count)
798 struct lpfc_scsi_buf *psb, *psb_next;
799 int status, sgl_size;
800 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
801 dma_addr_t pdma_phys_bpl1;
802 int last_xritag = NO_XRI;
803 LIST_HEAD(prep_sblist);
804 LIST_HEAD(blck_sblist);
805 LIST_HEAD(scsi_sblist);
807 /* sanity check */
808 if (sb_count <= 0)
809 return -EINVAL;
811 sgl_size = phba->cfg_sg_dma_buf_size -
812 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
814 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
815 list_del_init(&psb->list);
816 block_cnt++;
817 if ((last_xritag != NO_XRI) &&
818 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
819 /* a hole in xri block, form a sgl posting block */
820 list_splice_init(&prep_sblist, &blck_sblist);
821 post_cnt = block_cnt - 1;
822 /* prepare list for next posting block */
823 list_add_tail(&psb->list, &prep_sblist);
824 block_cnt = 1;
825 } else {
826 /* prepare list for next posting block */
827 list_add_tail(&psb->list, &prep_sblist);
828 /* enough sgls for non-embed sgl mbox command */
829 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
830 list_splice_init(&prep_sblist, &blck_sblist);
831 post_cnt = block_cnt;
832 block_cnt = 0;
835 num_posting++;
836 last_xritag = psb->cur_iocbq.sli4_xritag;
838 /* end of repost sgl list condition for SCSI buffers */
839 if (num_posting == sb_count) {
840 if (post_cnt == 0) {
841 /* last sgl posting block */
842 list_splice_init(&prep_sblist, &blck_sblist);
843 post_cnt = block_cnt;
844 } else if (block_cnt == 1) {
845 /* last single sgl with non-contiguous xri */
846 if (sgl_size > SGL_PAGE_SIZE)
847 pdma_phys_bpl1 = psb->dma_phys_bpl +
848 SGL_PAGE_SIZE;
849 else
850 pdma_phys_bpl1 = 0;
851 status = lpfc_sli4_post_sgl(phba,
852 psb->dma_phys_bpl,
853 pdma_phys_bpl1,
854 psb->cur_iocbq.sli4_xritag);
855 if (status) {
856 /* failure, put on abort scsi list */
857 psb->exch_busy = 1;
858 } else {
859 /* success, put on SCSI buffer list */
860 psb->exch_busy = 0;
861 psb->status = IOSTAT_SUCCESS;
862 num_posted++;
864 /* success, put on SCSI buffer sgl list */
865 list_add_tail(&psb->list, &scsi_sblist);
869 /* continue until a nembed page worth of sgls */
870 if (post_cnt == 0)
871 continue;
873 /* post block of SCSI buffer list sgls */
874 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
875 post_cnt);
877 /* don't reset xirtag due to hole in xri block */
878 if (block_cnt == 0)
879 last_xritag = NO_XRI;
881 /* reset SCSI buffer post count for next round of posting */
882 post_cnt = 0;
884 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
885 while (!list_empty(&blck_sblist)) {
886 list_remove_head(&blck_sblist, psb,
887 struct lpfc_scsi_buf, list);
888 if (status) {
889 /* failure, put on abort scsi list */
890 psb->exch_busy = 1;
891 } else {
892 /* success, put on SCSI buffer list */
893 psb->exch_busy = 0;
894 psb->status = IOSTAT_SUCCESS;
895 num_posted++;
897 list_add_tail(&psb->list, &scsi_sblist);
900 /* Push SCSI buffers with sgl posted to the availble list */
901 while (!list_empty(&scsi_sblist)) {
902 list_remove_head(&scsi_sblist, psb,
903 struct lpfc_scsi_buf, list);
904 lpfc_release_scsi_buf_s4(phba, psb);
906 return num_posted;
910 * lpfc_sli4_repost_scsi_sgl_list - Repsot all the allocated scsi buffer sgls
911 * @phba: pointer to lpfc hba data structure.
913 * This routine walks the list of scsi buffers that have been allocated and
914 * repost them to the port by using SGL block post. This is needed after a
915 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
916 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
917 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
919 * Returns: 0 = success, non-zero failure.
922 lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
924 LIST_HEAD(post_sblist);
925 int num_posted, rc = 0;
927 /* get all SCSI buffers need to repost to a local list */
928 spin_lock_irq(&phba->scsi_buf_list_get_lock);
929 spin_lock(&phba->scsi_buf_list_put_lock);
930 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
931 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
932 spin_unlock(&phba->scsi_buf_list_put_lock);
933 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
935 /* post the list of scsi buffer sgls to port if available */
936 if (!list_empty(&post_sblist)) {
937 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
938 phba->sli4_hba.scsi_xri_cnt);
939 /* failed to post any scsi buffer, return error */
940 if (num_posted == 0)
941 rc = -EIO;
943 return rc;
947 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
948 * @vport: The virtual port for which this call being executed.
949 * @num_to_allocate: The requested number of buffers to allocate.
951 * This routine allocates scsi buffers for device with SLI-4 interface spec,
952 * the scsi buffer contains all the necessary information needed to initiate
953 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
954 * them on a list, it post them to the port by using SGL block post.
956 * Return codes:
957 * int - number of scsi buffers that were allocated and posted.
958 * 0 = failure, less than num_to_alloc is a partial failure.
960 static int
961 lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
963 struct lpfc_hba *phba = vport->phba;
964 struct lpfc_scsi_buf *psb;
965 struct sli4_sge *sgl;
966 IOCB_t *iocb;
967 dma_addr_t pdma_phys_fcp_cmd;
968 dma_addr_t pdma_phys_fcp_rsp;
969 dma_addr_t pdma_phys_bpl;
970 uint16_t iotag, lxri = 0;
971 int bcnt, num_posted, sgl_size;
972 LIST_HEAD(prep_sblist);
973 LIST_HEAD(post_sblist);
974 LIST_HEAD(scsi_sblist);
976 sgl_size = phba->cfg_sg_dma_buf_size -
977 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
979 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
980 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
981 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
982 (int)sizeof(struct fcp_cmnd),
983 (int)sizeof(struct fcp_rsp));
985 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
986 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
987 if (!psb)
988 break;
990 * Get memory from the pci pool to map the virt space to
991 * pci bus space for an I/O. The DMA buffer includes space
992 * for the struct fcp_cmnd, struct fcp_rsp and the number
993 * of bde's necessary to support the sg_tablesize.
995 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
996 GFP_KERNEL, &psb->dma_handle);
997 if (!psb->data) {
998 kfree(psb);
999 break;
1001 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
1004 * 4K Page alignment is CRITICAL to BlockGuard, double check
1005 * to be sure.
1007 if (phba->cfg_enable_bg && (((unsigned long)(psb->data) &
1008 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
1009 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1010 psb->data, psb->dma_handle);
1011 kfree(psb);
1012 break;
1015 /* Allocate iotag for psb->cur_iocbq. */
1016 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
1017 if (iotag == 0) {
1018 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1019 psb->data, psb->dma_handle);
1020 kfree(psb);
1021 break;
1024 lxri = lpfc_sli4_next_xritag(phba);
1025 if (lxri == NO_XRI) {
1026 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1027 psb->data, psb->dma_handle);
1028 kfree(psb);
1029 break;
1031 psb->cur_iocbq.sli4_lxritag = lxri;
1032 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
1033 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
1034 psb->fcp_bpl = psb->data;
1035 psb->fcp_cmnd = (psb->data + sgl_size);
1036 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1037 sizeof(struct fcp_cmnd));
1039 /* Initialize local short-hand pointers. */
1040 sgl = (struct sli4_sge *)psb->fcp_bpl;
1041 pdma_phys_bpl = psb->dma_handle;
1042 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
1043 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1046 * The first two bdes are the FCP_CMD and FCP_RSP.
1047 * The balance are sg list bdes. Initialize the
1048 * first two and leave the rest for queuecommand.
1050 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1051 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
1052 sgl->word2 = le32_to_cpu(sgl->word2);
1053 bf_set(lpfc_sli4_sge_last, sgl, 0);
1054 sgl->word2 = cpu_to_le32(sgl->word2);
1055 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
1056 sgl++;
1058 /* Setup the physical region for the FCP RSP */
1059 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1060 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
1061 sgl->word2 = le32_to_cpu(sgl->word2);
1062 bf_set(lpfc_sli4_sge_last, sgl, 1);
1063 sgl->word2 = cpu_to_le32(sgl->word2);
1064 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
1067 * Since the IOCB for the FCP I/O is built into this
1068 * lpfc_scsi_buf, initialize it with all known data now.
1070 iocb = &psb->cur_iocbq.iocb;
1071 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1072 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1073 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1074 * We are setting the bpl to point to out sgl. An sgl's
1075 * entries are 16 bytes, a bpl entries are 12 bytes.
1077 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1078 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1079 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1080 iocb->ulpBdeCount = 1;
1081 iocb->ulpLe = 1;
1082 iocb->ulpClass = CLASS3;
1083 psb->cur_iocbq.context1 = psb;
1084 psb->dma_phys_bpl = pdma_phys_bpl;
1086 /* add the scsi buffer to a post list */
1087 list_add_tail(&psb->list, &post_sblist);
1088 spin_lock_irq(&phba->scsi_buf_list_get_lock);
1089 phba->sli4_hba.scsi_xri_cnt++;
1090 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
1092 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1093 "3021 Allocate %d out of %d requested new SCSI "
1094 "buffers\n", bcnt, num_to_alloc);
1096 /* post the list of scsi buffer sgls to port if available */
1097 if (!list_empty(&post_sblist))
1098 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1099 &post_sblist, bcnt);
1100 else
1101 num_posted = 0;
1103 return num_posted;
1107 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1108 * @vport: The virtual port for which this call being executed.
1109 * @num_to_allocate: The requested number of buffers to allocate.
1111 * This routine wraps the actual SCSI buffer allocator function pointer from
1112 * the lpfc_hba struct.
1114 * Return codes:
1115 * int - number of scsi buffers that were allocated.
1116 * 0 = failure, less than num_to_alloc is a partial failure.
1118 static inline int
1119 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1121 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1125 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1126 * @phba: The HBA for which this call is being executed.
1128 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1129 * and returns to caller.
1131 * Return codes:
1132 * NULL - Error
1133 * Pointer to lpfc_scsi_buf - Success
1135 static struct lpfc_scsi_buf*
1136 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1138 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1139 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
1140 unsigned long iflag = 0;
1142 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
1143 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1144 list);
1145 if (!lpfc_cmd) {
1146 spin_lock(&phba->scsi_buf_list_put_lock);
1147 list_splice(&phba->lpfc_scsi_buf_list_put,
1148 &phba->lpfc_scsi_buf_list_get);
1149 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1150 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1151 struct lpfc_scsi_buf, list);
1152 spin_unlock(&phba->scsi_buf_list_put_lock);
1154 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
1155 return lpfc_cmd;
1158 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1159 * @phba: The HBA for which this call is being executed.
1161 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1162 * and returns to caller.
1164 * Return codes:
1165 * NULL - Error
1166 * Pointer to lpfc_scsi_buf - Success
1168 static struct lpfc_scsi_buf*
1169 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1171 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
1172 unsigned long iflag = 0;
1173 int found = 0;
1175 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
1176 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1177 &phba->lpfc_scsi_buf_list_get, list) {
1178 if (lpfc_test_rrq_active(phba, ndlp,
1179 lpfc_cmd->cur_iocbq.sli4_lxritag))
1180 continue;
1181 list_del(&lpfc_cmd->list);
1182 found = 1;
1183 break;
1185 if (!found) {
1186 spin_lock(&phba->scsi_buf_list_put_lock);
1187 list_splice(&phba->lpfc_scsi_buf_list_put,
1188 &phba->lpfc_scsi_buf_list_get);
1189 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1190 spin_unlock(&phba->scsi_buf_list_put_lock);
1191 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1192 &phba->lpfc_scsi_buf_list_get, list) {
1193 if (lpfc_test_rrq_active(
1194 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1195 continue;
1196 list_del(&lpfc_cmd->list);
1197 found = 1;
1198 break;
1201 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
1202 if (!found)
1203 return NULL;
1204 return lpfc_cmd;
1207 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1208 * @phba: The HBA for which this call is being executed.
1210 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1211 * and returns to caller.
1213 * Return codes:
1214 * NULL - Error
1215 * Pointer to lpfc_scsi_buf - Success
1217 static struct lpfc_scsi_buf*
1218 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1220 return phba->lpfc_get_scsi_buf(phba, ndlp);
1224 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
1225 * @phba: The Hba for which this call is being executed.
1226 * @psb: The scsi buffer which is being released.
1228 * This routine releases @psb scsi buffer by adding it to tail of @phba
1229 * lpfc_scsi_buf_list list.
1231 static void
1232 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1234 unsigned long iflag = 0;
1236 psb->seg_cnt = 0;
1237 psb->nonsg_phys = 0;
1238 psb->prot_seg_cnt = 0;
1240 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1241 psb->pCmd = NULL;
1242 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
1243 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1244 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1248 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1249 * @phba: The Hba for which this call is being executed.
1250 * @psb: The scsi buffer which is being released.
1252 * This routine releases @psb scsi buffer by adding it to tail of @phba
1253 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1254 * and cannot be reused for at least RA_TOV amount of time if it was
1255 * aborted.
1257 static void
1258 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1260 unsigned long iflag = 0;
1262 psb->seg_cnt = 0;
1263 psb->nonsg_phys = 0;
1264 psb->prot_seg_cnt = 0;
1266 if (psb->exch_busy) {
1267 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1268 iflag);
1269 psb->pCmd = NULL;
1270 list_add_tail(&psb->list,
1271 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1272 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1273 iflag);
1274 } else {
1275 psb->pCmd = NULL;
1276 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
1277 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1278 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1279 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1284 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1285 * @phba: The Hba for which this call is being executed.
1286 * @psb: The scsi buffer which is being released.
1288 * This routine releases @psb scsi buffer by adding it to tail of @phba
1289 * lpfc_scsi_buf_list list.
1291 static void
1292 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1295 phba->lpfc_release_scsi_buf(phba, psb);
1299 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
1300 * @phba: The Hba for which this call is being executed.
1301 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1303 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1304 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1305 * through sg elements and format the bdea. This routine also initializes all
1306 * IOCB fields which are dependent on scsi command request buffer.
1308 * Return codes:
1309 * 1 - Error
1310 * 0 - Success
1312 static int
1313 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1315 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1316 struct scatterlist *sgel = NULL;
1317 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1318 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1319 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
1320 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1321 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1322 dma_addr_t physaddr;
1323 uint32_t num_bde = 0;
1324 int nseg, datadir = scsi_cmnd->sc_data_direction;
1327 * There are three possibilities here - use scatter-gather segment, use
1328 * the single mapping, or neither. Start the lpfc command prep by
1329 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1330 * data bde entry.
1332 bpl += 2;
1333 if (scsi_sg_count(scsi_cmnd)) {
1335 * The driver stores the segment count returned from pci_map_sg
1336 * because this a count of dma-mappings used to map the use_sg
1337 * pages. They are not guaranteed to be the same for those
1338 * architectures that implement an IOMMU.
1341 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1342 scsi_sg_count(scsi_cmnd), datadir);
1343 if (unlikely(!nseg))
1344 return 1;
1346 lpfc_cmd->seg_cnt = nseg;
1347 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1348 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1349 "9064 BLKGRD: %s: Too many sg segments from "
1350 "dma_map_sg. Config %d, seg_cnt %d\n",
1351 __func__, phba->cfg_sg_seg_cnt,
1352 lpfc_cmd->seg_cnt);
1353 lpfc_cmd->seg_cnt = 0;
1354 scsi_dma_unmap(scsi_cmnd);
1355 return 1;
1359 * The driver established a maximum scatter-gather segment count
1360 * during probe that limits the number of sg elements in any
1361 * single scsi command. Just run through the seg_cnt and format
1362 * the bde's.
1363 * When using SLI-3 the driver will try to fit all the BDEs into
1364 * the IOCB. If it can't then the BDEs get added to a BPL as it
1365 * does for SLI-2 mode.
1367 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1368 physaddr = sg_dma_address(sgel);
1369 if (phba->sli_rev == 3 &&
1370 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1371 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
1372 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1373 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1374 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1375 data_bde->addrLow = putPaddrLow(physaddr);
1376 data_bde->addrHigh = putPaddrHigh(physaddr);
1377 data_bde++;
1378 } else {
1379 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1380 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1381 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1382 bpl->addrLow =
1383 le32_to_cpu(putPaddrLow(physaddr));
1384 bpl->addrHigh =
1385 le32_to_cpu(putPaddrHigh(physaddr));
1386 bpl++;
1392 * Finish initializing those IOCB fields that are dependent on the
1393 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1394 * explicitly reinitialized and for SLI-3 the extended bde count is
1395 * explicitly reinitialized since all iocb memory resources are reused.
1397 if (phba->sli_rev == 3 &&
1398 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1399 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
1400 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1402 * The extended IOCB format can only fit 3 BDE or a BPL.
1403 * This I/O has more than 3 BDE so the 1st data bde will
1404 * be a BPL that is filled in here.
1406 physaddr = lpfc_cmd->dma_handle;
1407 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1408 data_bde->tus.f.bdeSize = (num_bde *
1409 sizeof(struct ulp_bde64));
1410 physaddr += (sizeof(struct fcp_cmnd) +
1411 sizeof(struct fcp_rsp) +
1412 (2 * sizeof(struct ulp_bde64)));
1413 data_bde->addrHigh = putPaddrHigh(physaddr);
1414 data_bde->addrLow = putPaddrLow(physaddr);
1415 /* ebde count includes the response bde and data bpl */
1416 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1417 } else {
1418 /* ebde count includes the response bde and data bdes */
1419 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1421 } else {
1422 iocb_cmd->un.fcpi64.bdl.bdeSize =
1423 ((num_bde + 2) * sizeof(struct ulp_bde64));
1424 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1426 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1429 * Due to difference in data length between DIF/non-DIF paths,
1430 * we need to set word 4 of IOCB here
1432 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1433 return 0;
1436 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1438 /* Return if if error injection is detected by Initiator */
1439 #define BG_ERR_INIT 0x1
1440 /* Return if if error injection is detected by Target */
1441 #define BG_ERR_TGT 0x2
1442 /* Return if if swapping CSUM<-->CRC is required for error injection */
1443 #define BG_ERR_SWAP 0x10
1444 /* Return if disabling Guard/Ref/App checking is required for error injection */
1445 #define BG_ERR_CHECK 0x20
1448 * lpfc_bg_err_inject - Determine if we should inject an error
1449 * @phba: The Hba for which this call is being executed.
1450 * @sc: The SCSI command to examine
1451 * @reftag: (out) BlockGuard reference tag for transmitted data
1452 * @apptag: (out) BlockGuard application tag for transmitted data
1453 * @new_guard (in) Value to replace CRC with if needed
1455 * Returns BG_ERR_* bit mask or 0 if request ignored
1457 static int
1458 lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1459 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1461 struct scatterlist *sgpe; /* s/g prot entry */
1462 struct scatterlist *sgde; /* s/g data entry */
1463 struct lpfc_scsi_buf *lpfc_cmd = NULL;
1464 struct scsi_dif_tuple *src = NULL;
1465 struct lpfc_nodelist *ndlp;
1466 struct lpfc_rport_data *rdata;
1467 uint32_t op = scsi_get_prot_op(sc);
1468 uint32_t blksize;
1469 uint32_t numblks;
1470 sector_t lba;
1471 int rc = 0;
1472 int blockoff = 0;
1474 if (op == SCSI_PROT_NORMAL)
1475 return 0;
1477 sgpe = scsi_prot_sglist(sc);
1478 sgde = scsi_sglist(sc);
1479 lba = scsi_get_lba(sc);
1481 /* First check if we need to match the LBA */
1482 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1483 blksize = lpfc_cmd_blksize(sc);
1484 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1486 /* Make sure we have the right LBA if one is specified */
1487 if ((phba->lpfc_injerr_lba < lba) ||
1488 (phba->lpfc_injerr_lba >= (lba + numblks)))
1489 return 0;
1490 if (sgpe) {
1491 blockoff = phba->lpfc_injerr_lba - lba;
1492 numblks = sg_dma_len(sgpe) /
1493 sizeof(struct scsi_dif_tuple);
1494 if (numblks < blockoff)
1495 blockoff = numblks;
1499 /* Next check if we need to match the remote NPortID or WWPN */
1500 rdata = sc->device->hostdata;
1501 if (rdata && rdata->pnode) {
1502 ndlp = rdata->pnode;
1504 /* Make sure we have the right NPortID if one is specified */
1505 if (phba->lpfc_injerr_nportid &&
1506 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1507 return 0;
1510 * Make sure we have the right WWPN if one is specified.
1511 * wwn[0] should be a non-zero NAA in a good WWPN.
1513 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1514 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1515 sizeof(struct lpfc_name)) != 0))
1516 return 0;
1519 /* Setup a ptr to the protection data if the SCSI host provides it */
1520 if (sgpe) {
1521 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1522 src += blockoff;
1523 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1526 /* Should we change the Reference Tag */
1527 if (reftag) {
1528 if (phba->lpfc_injerr_wref_cnt) {
1529 switch (op) {
1530 case SCSI_PROT_WRITE_PASS:
1531 if (src) {
1533 * For WRITE_PASS, force the error
1534 * to be sent on the wire. It should
1535 * be detected by the Target.
1536 * If blockoff != 0 error will be
1537 * inserted in middle of the IO.
1540 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1541 "9076 BLKGRD: Injecting reftag error: "
1542 "write lba x%lx + x%x oldrefTag x%x\n",
1543 (unsigned long)lba, blockoff,
1544 be32_to_cpu(src->ref_tag));
1547 * Save the old ref_tag so we can
1548 * restore it on completion.
1550 if (lpfc_cmd) {
1551 lpfc_cmd->prot_data_type =
1552 LPFC_INJERR_REFTAG;
1553 lpfc_cmd->prot_data_segment =
1554 src;
1555 lpfc_cmd->prot_data =
1556 src->ref_tag;
1558 src->ref_tag = cpu_to_be32(0xDEADBEEF);
1559 phba->lpfc_injerr_wref_cnt--;
1560 if (phba->lpfc_injerr_wref_cnt == 0) {
1561 phba->lpfc_injerr_nportid = 0;
1562 phba->lpfc_injerr_lba =
1563 LPFC_INJERR_LBA_OFF;
1564 memset(&phba->lpfc_injerr_wwpn,
1565 0, sizeof(struct lpfc_name));
1567 rc = BG_ERR_TGT | BG_ERR_CHECK;
1569 break;
1571 /* Drop thru */
1572 case SCSI_PROT_WRITE_INSERT:
1574 * For WRITE_INSERT, force the error
1575 * to be sent on the wire. It should be
1576 * detected by the Target.
1578 /* DEADBEEF will be the reftag on the wire */
1579 *reftag = 0xDEADBEEF;
1580 phba->lpfc_injerr_wref_cnt--;
1581 if (phba->lpfc_injerr_wref_cnt == 0) {
1582 phba->lpfc_injerr_nportid = 0;
1583 phba->lpfc_injerr_lba =
1584 LPFC_INJERR_LBA_OFF;
1585 memset(&phba->lpfc_injerr_wwpn,
1586 0, sizeof(struct lpfc_name));
1588 rc = BG_ERR_TGT | BG_ERR_CHECK;
1590 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1591 "9078 BLKGRD: Injecting reftag error: "
1592 "write lba x%lx\n", (unsigned long)lba);
1593 break;
1594 case SCSI_PROT_WRITE_STRIP:
1596 * For WRITE_STRIP and WRITE_PASS,
1597 * force the error on data
1598 * being copied from SLI-Host to SLI-Port.
1600 *reftag = 0xDEADBEEF;
1601 phba->lpfc_injerr_wref_cnt--;
1602 if (phba->lpfc_injerr_wref_cnt == 0) {
1603 phba->lpfc_injerr_nportid = 0;
1604 phba->lpfc_injerr_lba =
1605 LPFC_INJERR_LBA_OFF;
1606 memset(&phba->lpfc_injerr_wwpn,
1607 0, sizeof(struct lpfc_name));
1609 rc = BG_ERR_INIT;
1611 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1612 "9077 BLKGRD: Injecting reftag error: "
1613 "write lba x%lx\n", (unsigned long)lba);
1614 break;
1617 if (phba->lpfc_injerr_rref_cnt) {
1618 switch (op) {
1619 case SCSI_PROT_READ_INSERT:
1620 case SCSI_PROT_READ_STRIP:
1621 case SCSI_PROT_READ_PASS:
1623 * For READ_STRIP and READ_PASS, force the
1624 * error on data being read off the wire. It
1625 * should force an IO error to the driver.
1627 *reftag = 0xDEADBEEF;
1628 phba->lpfc_injerr_rref_cnt--;
1629 if (phba->lpfc_injerr_rref_cnt == 0) {
1630 phba->lpfc_injerr_nportid = 0;
1631 phba->lpfc_injerr_lba =
1632 LPFC_INJERR_LBA_OFF;
1633 memset(&phba->lpfc_injerr_wwpn,
1634 0, sizeof(struct lpfc_name));
1636 rc = BG_ERR_INIT;
1638 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1639 "9079 BLKGRD: Injecting reftag error: "
1640 "read lba x%lx\n", (unsigned long)lba);
1641 break;
1646 /* Should we change the Application Tag */
1647 if (apptag) {
1648 if (phba->lpfc_injerr_wapp_cnt) {
1649 switch (op) {
1650 case SCSI_PROT_WRITE_PASS:
1651 if (src) {
1653 * For WRITE_PASS, force the error
1654 * to be sent on the wire. It should
1655 * be detected by the Target.
1656 * If blockoff != 0 error will be
1657 * inserted in middle of the IO.
1660 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1661 "9080 BLKGRD: Injecting apptag error: "
1662 "write lba x%lx + x%x oldappTag x%x\n",
1663 (unsigned long)lba, blockoff,
1664 be16_to_cpu(src->app_tag));
1667 * Save the old app_tag so we can
1668 * restore it on completion.
1670 if (lpfc_cmd) {
1671 lpfc_cmd->prot_data_type =
1672 LPFC_INJERR_APPTAG;
1673 lpfc_cmd->prot_data_segment =
1674 src;
1675 lpfc_cmd->prot_data =
1676 src->app_tag;
1678 src->app_tag = cpu_to_be16(0xDEAD);
1679 phba->lpfc_injerr_wapp_cnt--;
1680 if (phba->lpfc_injerr_wapp_cnt == 0) {
1681 phba->lpfc_injerr_nportid = 0;
1682 phba->lpfc_injerr_lba =
1683 LPFC_INJERR_LBA_OFF;
1684 memset(&phba->lpfc_injerr_wwpn,
1685 0, sizeof(struct lpfc_name));
1687 rc = BG_ERR_TGT | BG_ERR_CHECK;
1688 break;
1690 /* Drop thru */
1691 case SCSI_PROT_WRITE_INSERT:
1693 * For WRITE_INSERT, force the
1694 * error to be sent on the wire. It should be
1695 * detected by the Target.
1697 /* DEAD will be the apptag on the wire */
1698 *apptag = 0xDEAD;
1699 phba->lpfc_injerr_wapp_cnt--;
1700 if (phba->lpfc_injerr_wapp_cnt == 0) {
1701 phba->lpfc_injerr_nportid = 0;
1702 phba->lpfc_injerr_lba =
1703 LPFC_INJERR_LBA_OFF;
1704 memset(&phba->lpfc_injerr_wwpn,
1705 0, sizeof(struct lpfc_name));
1707 rc = BG_ERR_TGT | BG_ERR_CHECK;
1709 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1710 "0813 BLKGRD: Injecting apptag error: "
1711 "write lba x%lx\n", (unsigned long)lba);
1712 break;
1713 case SCSI_PROT_WRITE_STRIP:
1715 * For WRITE_STRIP and WRITE_PASS,
1716 * force the error on data
1717 * being copied from SLI-Host to SLI-Port.
1719 *apptag = 0xDEAD;
1720 phba->lpfc_injerr_wapp_cnt--;
1721 if (phba->lpfc_injerr_wapp_cnt == 0) {
1722 phba->lpfc_injerr_nportid = 0;
1723 phba->lpfc_injerr_lba =
1724 LPFC_INJERR_LBA_OFF;
1725 memset(&phba->lpfc_injerr_wwpn,
1726 0, sizeof(struct lpfc_name));
1728 rc = BG_ERR_INIT;
1730 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1731 "0812 BLKGRD: Injecting apptag error: "
1732 "write lba x%lx\n", (unsigned long)lba);
1733 break;
1736 if (phba->lpfc_injerr_rapp_cnt) {
1737 switch (op) {
1738 case SCSI_PROT_READ_INSERT:
1739 case SCSI_PROT_READ_STRIP:
1740 case SCSI_PROT_READ_PASS:
1742 * For READ_STRIP and READ_PASS, force the
1743 * error on data being read off the wire. It
1744 * should force an IO error to the driver.
1746 *apptag = 0xDEAD;
1747 phba->lpfc_injerr_rapp_cnt--;
1748 if (phba->lpfc_injerr_rapp_cnt == 0) {
1749 phba->lpfc_injerr_nportid = 0;
1750 phba->lpfc_injerr_lba =
1751 LPFC_INJERR_LBA_OFF;
1752 memset(&phba->lpfc_injerr_wwpn,
1753 0, sizeof(struct lpfc_name));
1755 rc = BG_ERR_INIT;
1757 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1758 "0814 BLKGRD: Injecting apptag error: "
1759 "read lba x%lx\n", (unsigned long)lba);
1760 break;
1766 /* Should we change the Guard Tag */
1767 if (new_guard) {
1768 if (phba->lpfc_injerr_wgrd_cnt) {
1769 switch (op) {
1770 case SCSI_PROT_WRITE_PASS:
1771 rc = BG_ERR_CHECK;
1772 /* Drop thru */
1774 case SCSI_PROT_WRITE_INSERT:
1776 * For WRITE_INSERT, force the
1777 * error to be sent on the wire. It should be
1778 * detected by the Target.
1780 phba->lpfc_injerr_wgrd_cnt--;
1781 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1782 phba->lpfc_injerr_nportid = 0;
1783 phba->lpfc_injerr_lba =
1784 LPFC_INJERR_LBA_OFF;
1785 memset(&phba->lpfc_injerr_wwpn,
1786 0, sizeof(struct lpfc_name));
1789 rc |= BG_ERR_TGT | BG_ERR_SWAP;
1790 /* Signals the caller to swap CRC->CSUM */
1792 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1793 "0817 BLKGRD: Injecting guard error: "
1794 "write lba x%lx\n", (unsigned long)lba);
1795 break;
1796 case SCSI_PROT_WRITE_STRIP:
1798 * For WRITE_STRIP and WRITE_PASS,
1799 * force the error on data
1800 * being copied from SLI-Host to SLI-Port.
1802 phba->lpfc_injerr_wgrd_cnt--;
1803 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1804 phba->lpfc_injerr_nportid = 0;
1805 phba->lpfc_injerr_lba =
1806 LPFC_INJERR_LBA_OFF;
1807 memset(&phba->lpfc_injerr_wwpn,
1808 0, sizeof(struct lpfc_name));
1811 rc = BG_ERR_INIT | BG_ERR_SWAP;
1812 /* Signals the caller to swap CRC->CSUM */
1814 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1815 "0816 BLKGRD: Injecting guard error: "
1816 "write lba x%lx\n", (unsigned long)lba);
1817 break;
1820 if (phba->lpfc_injerr_rgrd_cnt) {
1821 switch (op) {
1822 case SCSI_PROT_READ_INSERT:
1823 case SCSI_PROT_READ_STRIP:
1824 case SCSI_PROT_READ_PASS:
1826 * For READ_STRIP and READ_PASS, force the
1827 * error on data being read off the wire. It
1828 * should force an IO error to the driver.
1830 phba->lpfc_injerr_rgrd_cnt--;
1831 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1832 phba->lpfc_injerr_nportid = 0;
1833 phba->lpfc_injerr_lba =
1834 LPFC_INJERR_LBA_OFF;
1835 memset(&phba->lpfc_injerr_wwpn,
1836 0, sizeof(struct lpfc_name));
1839 rc = BG_ERR_INIT | BG_ERR_SWAP;
1840 /* Signals the caller to swap CRC->CSUM */
1842 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1843 "0818 BLKGRD: Injecting guard error: "
1844 "read lba x%lx\n", (unsigned long)lba);
1849 return rc;
1851 #endif
1854 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1855 * the specified SCSI command.
1856 * @phba: The Hba for which this call is being executed.
1857 * @sc: The SCSI command to examine
1858 * @txopt: (out) BlockGuard operation for transmitted data
1859 * @rxopt: (out) BlockGuard operation for received data
1861 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1864 static int
1865 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1866 uint8_t *txop, uint8_t *rxop)
1868 uint8_t ret = 0;
1870 if (lpfc_cmd_guard_csum(sc)) {
1871 switch (scsi_get_prot_op(sc)) {
1872 case SCSI_PROT_READ_INSERT:
1873 case SCSI_PROT_WRITE_STRIP:
1874 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1875 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1876 break;
1878 case SCSI_PROT_READ_STRIP:
1879 case SCSI_PROT_WRITE_INSERT:
1880 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1881 *txop = BG_OP_IN_NODIF_OUT_CRC;
1882 break;
1884 case SCSI_PROT_READ_PASS:
1885 case SCSI_PROT_WRITE_PASS:
1886 *rxop = BG_OP_IN_CRC_OUT_CSUM;
1887 *txop = BG_OP_IN_CSUM_OUT_CRC;
1888 break;
1890 case SCSI_PROT_NORMAL:
1891 default:
1892 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1893 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1894 scsi_get_prot_op(sc));
1895 ret = 1;
1896 break;
1899 } else {
1900 switch (scsi_get_prot_op(sc)) {
1901 case SCSI_PROT_READ_STRIP:
1902 case SCSI_PROT_WRITE_INSERT:
1903 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1904 *txop = BG_OP_IN_NODIF_OUT_CRC;
1905 break;
1907 case SCSI_PROT_READ_PASS:
1908 case SCSI_PROT_WRITE_PASS:
1909 *rxop = BG_OP_IN_CRC_OUT_CRC;
1910 *txop = BG_OP_IN_CRC_OUT_CRC;
1911 break;
1913 case SCSI_PROT_READ_INSERT:
1914 case SCSI_PROT_WRITE_STRIP:
1915 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1916 *txop = BG_OP_IN_CRC_OUT_NODIF;
1917 break;
1919 case SCSI_PROT_NORMAL:
1920 default:
1921 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1922 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1923 scsi_get_prot_op(sc));
1924 ret = 1;
1925 break;
1929 return ret;
1932 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1934 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1935 * the specified SCSI command in order to force a guard tag error.
1936 * @phba: The Hba for which this call is being executed.
1937 * @sc: The SCSI command to examine
1938 * @txopt: (out) BlockGuard operation for transmitted data
1939 * @rxopt: (out) BlockGuard operation for received data
1941 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1944 static int
1945 lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1946 uint8_t *txop, uint8_t *rxop)
1948 uint8_t ret = 0;
1950 if (lpfc_cmd_guard_csum(sc)) {
1951 switch (scsi_get_prot_op(sc)) {
1952 case SCSI_PROT_READ_INSERT:
1953 case SCSI_PROT_WRITE_STRIP:
1954 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1955 *txop = BG_OP_IN_CRC_OUT_NODIF;
1956 break;
1958 case SCSI_PROT_READ_STRIP:
1959 case SCSI_PROT_WRITE_INSERT:
1960 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1961 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1962 break;
1964 case SCSI_PROT_READ_PASS:
1965 case SCSI_PROT_WRITE_PASS:
1966 *rxop = BG_OP_IN_CSUM_OUT_CRC;
1967 *txop = BG_OP_IN_CRC_OUT_CSUM;
1968 break;
1970 case SCSI_PROT_NORMAL:
1971 default:
1972 break;
1975 } else {
1976 switch (scsi_get_prot_op(sc)) {
1977 case SCSI_PROT_READ_STRIP:
1978 case SCSI_PROT_WRITE_INSERT:
1979 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1980 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1981 break;
1983 case SCSI_PROT_READ_PASS:
1984 case SCSI_PROT_WRITE_PASS:
1985 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
1986 *txop = BG_OP_IN_CSUM_OUT_CSUM;
1987 break;
1989 case SCSI_PROT_READ_INSERT:
1990 case SCSI_PROT_WRITE_STRIP:
1991 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1992 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1993 break;
1995 case SCSI_PROT_NORMAL:
1996 default:
1997 break;
2001 return ret;
2003 #endif
2006 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
2007 * @phba: The Hba for which this call is being executed.
2008 * @sc: pointer to scsi command we're working on
2009 * @bpl: pointer to buffer list for protection groups
2010 * @datacnt: number of segments of data that have been dma mapped
2012 * This function sets up BPL buffer list for protection groups of
2013 * type LPFC_PG_TYPE_NO_DIF
2015 * This is usually used when the HBA is instructed to generate
2016 * DIFs and insert them into data stream (or strip DIF from
2017 * incoming data stream)
2019 * The buffer list consists of just one protection group described
2020 * below:
2021 * +-------------------------+
2022 * start of prot group --> | PDE_5 |
2023 * +-------------------------+
2024 * | PDE_6 |
2025 * +-------------------------+
2026 * | Data BDE |
2027 * +-------------------------+
2028 * |more Data BDE's ... (opt)|
2029 * +-------------------------+
2032 * Note: Data s/g buffers have been dma mapped
2034 * Returns the number of BDEs added to the BPL.
2036 static int
2037 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2038 struct ulp_bde64 *bpl, int datasegcnt)
2040 struct scatterlist *sgde = NULL; /* s/g data entry */
2041 struct lpfc_pde5 *pde5 = NULL;
2042 struct lpfc_pde6 *pde6 = NULL;
2043 dma_addr_t physaddr;
2044 int i = 0, num_bde = 0, status;
2045 int datadir = sc->sc_data_direction;
2046 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2047 uint32_t rc;
2048 #endif
2049 uint32_t checking = 1;
2050 uint32_t reftag;
2051 unsigned blksize;
2052 uint8_t txop, rxop;
2054 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2055 if (status)
2056 goto out;
2058 /* extract some info from the scsi command for pde*/
2059 blksize = lpfc_cmd_blksize(sc);
2060 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2062 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2063 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2064 if (rc) {
2065 if (rc & BG_ERR_SWAP)
2066 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2067 if (rc & BG_ERR_CHECK)
2068 checking = 0;
2070 #endif
2072 /* setup PDE5 with what we have */
2073 pde5 = (struct lpfc_pde5 *) bpl;
2074 memset(pde5, 0, sizeof(struct lpfc_pde5));
2075 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
2077 /* Endianness conversion if necessary for PDE5 */
2078 pde5->word0 = cpu_to_le32(pde5->word0);
2079 pde5->reftag = cpu_to_le32(reftag);
2081 /* advance bpl and increment bde count */
2082 num_bde++;
2083 bpl++;
2084 pde6 = (struct lpfc_pde6 *) bpl;
2086 /* setup PDE6 with the rest of the info */
2087 memset(pde6, 0, sizeof(struct lpfc_pde6));
2088 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2089 bf_set(pde6_optx, pde6, txop);
2090 bf_set(pde6_oprx, pde6, rxop);
2093 * We only need to check the data on READs, for WRITEs
2094 * protection data is automatically generated, not checked.
2096 if (datadir == DMA_FROM_DEVICE) {
2097 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2098 bf_set(pde6_ce, pde6, checking);
2099 else
2100 bf_set(pde6_ce, pde6, 0);
2102 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2103 bf_set(pde6_re, pde6, checking);
2104 else
2105 bf_set(pde6_re, pde6, 0);
2107 bf_set(pde6_ai, pde6, 1);
2108 bf_set(pde6_ae, pde6, 0);
2109 bf_set(pde6_apptagval, pde6, 0);
2111 /* Endianness conversion if necessary for PDE6 */
2112 pde6->word0 = cpu_to_le32(pde6->word0);
2113 pde6->word1 = cpu_to_le32(pde6->word1);
2114 pde6->word2 = cpu_to_le32(pde6->word2);
2116 /* advance bpl and increment bde count */
2117 num_bde++;
2118 bpl++;
2120 /* assumption: caller has already run dma_map_sg on command data */
2121 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2122 physaddr = sg_dma_address(sgde);
2123 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2124 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2125 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2126 if (datadir == DMA_TO_DEVICE)
2127 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2128 else
2129 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2130 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2131 bpl++;
2132 num_bde++;
2135 out:
2136 return num_bde;
2140 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2141 * @phba: The Hba for which this call is being executed.
2142 * @sc: pointer to scsi command we're working on
2143 * @bpl: pointer to buffer list for protection groups
2144 * @datacnt: number of segments of data that have been dma mapped
2145 * @protcnt: number of segment of protection data that have been dma mapped
2147 * This function sets up BPL buffer list for protection groups of
2148 * type LPFC_PG_TYPE_DIF
2150 * This is usually used when DIFs are in their own buffers,
2151 * separate from the data. The HBA can then by instructed
2152 * to place the DIFs in the outgoing stream. For read operations,
2153 * The HBA could extract the DIFs and place it in DIF buffers.
2155 * The buffer list for this type consists of one or more of the
2156 * protection groups described below:
2157 * +-------------------------+
2158 * start of first prot group --> | PDE_5 |
2159 * +-------------------------+
2160 * | PDE_6 |
2161 * +-------------------------+
2162 * | PDE_7 (Prot BDE) |
2163 * +-------------------------+
2164 * | Data BDE |
2165 * +-------------------------+
2166 * |more Data BDE's ... (opt)|
2167 * +-------------------------+
2168 * start of new prot group --> | PDE_5 |
2169 * +-------------------------+
2170 * | ... |
2171 * +-------------------------+
2173 * Note: It is assumed that both data and protection s/g buffers have been
2174 * mapped for DMA
2176 * Returns the number of BDEs added to the BPL.
2178 static int
2179 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2180 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2182 struct scatterlist *sgde = NULL; /* s/g data entry */
2183 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2184 struct lpfc_pde5 *pde5 = NULL;
2185 struct lpfc_pde6 *pde6 = NULL;
2186 struct lpfc_pde7 *pde7 = NULL;
2187 dma_addr_t dataphysaddr, protphysaddr;
2188 unsigned short curr_data = 0, curr_prot = 0;
2189 unsigned int split_offset;
2190 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2191 unsigned int protgrp_blks, protgrp_bytes;
2192 unsigned int remainder, subtotal;
2193 int status;
2194 int datadir = sc->sc_data_direction;
2195 unsigned char pgdone = 0, alldone = 0;
2196 unsigned blksize;
2197 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2198 uint32_t rc;
2199 #endif
2200 uint32_t checking = 1;
2201 uint32_t reftag;
2202 uint8_t txop, rxop;
2203 int num_bde = 0;
2205 sgpe = scsi_prot_sglist(sc);
2206 sgde = scsi_sglist(sc);
2208 if (!sgpe || !sgde) {
2209 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2210 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2211 sgpe, sgde);
2212 return 0;
2215 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2216 if (status)
2217 goto out;
2219 /* extract some info from the scsi command */
2220 blksize = lpfc_cmd_blksize(sc);
2221 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2223 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2224 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2225 if (rc) {
2226 if (rc & BG_ERR_SWAP)
2227 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2228 if (rc & BG_ERR_CHECK)
2229 checking = 0;
2231 #endif
2233 split_offset = 0;
2234 do {
2235 /* Check to see if we ran out of space */
2236 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2237 return num_bde + 3;
2239 /* setup PDE5 with what we have */
2240 pde5 = (struct lpfc_pde5 *) bpl;
2241 memset(pde5, 0, sizeof(struct lpfc_pde5));
2242 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
2244 /* Endianness conversion if necessary for PDE5 */
2245 pde5->word0 = cpu_to_le32(pde5->word0);
2246 pde5->reftag = cpu_to_le32(reftag);
2248 /* advance bpl and increment bde count */
2249 num_bde++;
2250 bpl++;
2251 pde6 = (struct lpfc_pde6 *) bpl;
2253 /* setup PDE6 with the rest of the info */
2254 memset(pde6, 0, sizeof(struct lpfc_pde6));
2255 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2256 bf_set(pde6_optx, pde6, txop);
2257 bf_set(pde6_oprx, pde6, rxop);
2259 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2260 bf_set(pde6_ce, pde6, checking);
2261 else
2262 bf_set(pde6_ce, pde6, 0);
2264 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2265 bf_set(pde6_re, pde6, checking);
2266 else
2267 bf_set(pde6_re, pde6, 0);
2269 bf_set(pde6_ai, pde6, 1);
2270 bf_set(pde6_ae, pde6, 0);
2271 bf_set(pde6_apptagval, pde6, 0);
2273 /* Endianness conversion if necessary for PDE6 */
2274 pde6->word0 = cpu_to_le32(pde6->word0);
2275 pde6->word1 = cpu_to_le32(pde6->word1);
2276 pde6->word2 = cpu_to_le32(pde6->word2);
2278 /* advance bpl and increment bde count */
2279 num_bde++;
2280 bpl++;
2282 /* setup the first BDE that points to protection buffer */
2283 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2284 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2286 /* must be integer multiple of the DIF block length */
2287 BUG_ON(protgroup_len % 8);
2289 pde7 = (struct lpfc_pde7 *) bpl;
2290 memset(pde7, 0, sizeof(struct lpfc_pde7));
2291 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2293 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2294 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
2296 protgrp_blks = protgroup_len / 8;
2297 protgrp_bytes = protgrp_blks * blksize;
2299 /* check if this pde is crossing the 4K boundary; if so split */
2300 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2301 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2302 protgroup_offset += protgroup_remainder;
2303 protgrp_blks = protgroup_remainder / 8;
2304 protgrp_bytes = protgrp_blks * blksize;
2305 } else {
2306 protgroup_offset = 0;
2307 curr_prot++;
2310 num_bde++;
2312 /* setup BDE's for data blocks associated with DIF data */
2313 pgdone = 0;
2314 subtotal = 0; /* total bytes processed for current prot grp */
2315 while (!pgdone) {
2316 /* Check to see if we ran out of space */
2317 if (num_bde >= phba->cfg_total_seg_cnt)
2318 return num_bde + 1;
2320 if (!sgde) {
2321 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2322 "9065 BLKGRD:%s Invalid data segment\n",
2323 __func__);
2324 return 0;
2326 bpl++;
2327 dataphysaddr = sg_dma_address(sgde) + split_offset;
2328 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2329 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2331 remainder = sg_dma_len(sgde) - split_offset;
2333 if ((subtotal + remainder) <= protgrp_bytes) {
2334 /* we can use this whole buffer */
2335 bpl->tus.f.bdeSize = remainder;
2336 split_offset = 0;
2338 if ((subtotal + remainder) == protgrp_bytes)
2339 pgdone = 1;
2340 } else {
2341 /* must split this buffer with next prot grp */
2342 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2343 split_offset += bpl->tus.f.bdeSize;
2346 subtotal += bpl->tus.f.bdeSize;
2348 if (datadir == DMA_TO_DEVICE)
2349 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2350 else
2351 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2352 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2354 num_bde++;
2355 curr_data++;
2357 if (split_offset)
2358 break;
2360 /* Move to the next s/g segment if possible */
2361 sgde = sg_next(sgde);
2365 if (protgroup_offset) {
2366 /* update the reference tag */
2367 reftag += protgrp_blks;
2368 bpl++;
2369 continue;
2372 /* are we done ? */
2373 if (curr_prot == protcnt) {
2374 alldone = 1;
2375 } else if (curr_prot < protcnt) {
2376 /* advance to next prot buffer */
2377 sgpe = sg_next(sgpe);
2378 bpl++;
2380 /* update the reference tag */
2381 reftag += protgrp_blks;
2382 } else {
2383 /* if we're here, we have a bug */
2384 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2385 "9054 BLKGRD: bug in %s\n", __func__);
2388 } while (!alldone);
2389 out:
2391 return num_bde;
2395 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2396 * @phba: The Hba for which this call is being executed.
2397 * @sc: pointer to scsi command we're working on
2398 * @sgl: pointer to buffer list for protection groups
2399 * @datacnt: number of segments of data that have been dma mapped
2401 * This function sets up SGL buffer list for protection groups of
2402 * type LPFC_PG_TYPE_NO_DIF
2404 * This is usually used when the HBA is instructed to generate
2405 * DIFs and insert them into data stream (or strip DIF from
2406 * incoming data stream)
2408 * The buffer list consists of just one protection group described
2409 * below:
2410 * +-------------------------+
2411 * start of prot group --> | DI_SEED |
2412 * +-------------------------+
2413 * | Data SGE |
2414 * +-------------------------+
2415 * |more Data SGE's ... (opt)|
2416 * +-------------------------+
2419 * Note: Data s/g buffers have been dma mapped
2421 * Returns the number of SGEs added to the SGL.
2423 static int
2424 lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2425 struct sli4_sge *sgl, int datasegcnt)
2427 struct scatterlist *sgde = NULL; /* s/g data entry */
2428 struct sli4_sge_diseed *diseed = NULL;
2429 dma_addr_t physaddr;
2430 int i = 0, num_sge = 0, status;
2431 uint32_t reftag;
2432 unsigned blksize;
2433 uint8_t txop, rxop;
2434 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2435 uint32_t rc;
2436 #endif
2437 uint32_t checking = 1;
2438 uint32_t dma_len;
2439 uint32_t dma_offset = 0;
2441 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2442 if (status)
2443 goto out;
2445 /* extract some info from the scsi command for pde*/
2446 blksize = lpfc_cmd_blksize(sc);
2447 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2449 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2450 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2451 if (rc) {
2452 if (rc & BG_ERR_SWAP)
2453 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2454 if (rc & BG_ERR_CHECK)
2455 checking = 0;
2457 #endif
2459 /* setup DISEED with what we have */
2460 diseed = (struct sli4_sge_diseed *) sgl;
2461 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2462 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2464 /* Endianness conversion if necessary */
2465 diseed->ref_tag = cpu_to_le32(reftag);
2466 diseed->ref_tag_tran = diseed->ref_tag;
2469 * We only need to check the data on READs, for WRITEs
2470 * protection data is automatically generated, not checked.
2472 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2473 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2474 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2475 else
2476 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2478 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2479 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2480 else
2481 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2484 /* setup DISEED with the rest of the info */
2485 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2486 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2488 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2489 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2491 /* Endianness conversion if necessary for DISEED */
2492 diseed->word2 = cpu_to_le32(diseed->word2);
2493 diseed->word3 = cpu_to_le32(diseed->word3);
2495 /* advance bpl and increment sge count */
2496 num_sge++;
2497 sgl++;
2499 /* assumption: caller has already run dma_map_sg on command data */
2500 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2501 physaddr = sg_dma_address(sgde);
2502 dma_len = sg_dma_len(sgde);
2503 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2504 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2505 if ((i + 1) == datasegcnt)
2506 bf_set(lpfc_sli4_sge_last, sgl, 1);
2507 else
2508 bf_set(lpfc_sli4_sge_last, sgl, 0);
2509 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2510 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2512 sgl->sge_len = cpu_to_le32(dma_len);
2513 dma_offset += dma_len;
2515 sgl++;
2516 num_sge++;
2519 out:
2520 return num_sge;
2524 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2525 * @phba: The Hba for which this call is being executed.
2526 * @sc: pointer to scsi command we're working on
2527 * @sgl: pointer to buffer list for protection groups
2528 * @datacnt: number of segments of data that have been dma mapped
2529 * @protcnt: number of segment of protection data that have been dma mapped
2531 * This function sets up SGL buffer list for protection groups of
2532 * type LPFC_PG_TYPE_DIF
2534 * This is usually used when DIFs are in their own buffers,
2535 * separate from the data. The HBA can then by instructed
2536 * to place the DIFs in the outgoing stream. For read operations,
2537 * The HBA could extract the DIFs and place it in DIF buffers.
2539 * The buffer list for this type consists of one or more of the
2540 * protection groups described below:
2541 * +-------------------------+
2542 * start of first prot group --> | DISEED |
2543 * +-------------------------+
2544 * | DIF (Prot SGE) |
2545 * +-------------------------+
2546 * | Data SGE |
2547 * +-------------------------+
2548 * |more Data SGE's ... (opt)|
2549 * +-------------------------+
2550 * start of new prot group --> | DISEED |
2551 * +-------------------------+
2552 * | ... |
2553 * +-------------------------+
2555 * Note: It is assumed that both data and protection s/g buffers have been
2556 * mapped for DMA
2558 * Returns the number of SGEs added to the SGL.
2560 static int
2561 lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2562 struct sli4_sge *sgl, int datacnt, int protcnt)
2564 struct scatterlist *sgde = NULL; /* s/g data entry */
2565 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2566 struct sli4_sge_diseed *diseed = NULL;
2567 dma_addr_t dataphysaddr, protphysaddr;
2568 unsigned short curr_data = 0, curr_prot = 0;
2569 unsigned int split_offset;
2570 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2571 unsigned int protgrp_blks, protgrp_bytes;
2572 unsigned int remainder, subtotal;
2573 int status;
2574 unsigned char pgdone = 0, alldone = 0;
2575 unsigned blksize;
2576 uint32_t reftag;
2577 uint8_t txop, rxop;
2578 uint32_t dma_len;
2579 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2580 uint32_t rc;
2581 #endif
2582 uint32_t checking = 1;
2583 uint32_t dma_offset = 0;
2584 int num_sge = 0;
2586 sgpe = scsi_prot_sglist(sc);
2587 sgde = scsi_sglist(sc);
2589 if (!sgpe || !sgde) {
2590 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2591 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2592 sgpe, sgde);
2593 return 0;
2596 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2597 if (status)
2598 goto out;
2600 /* extract some info from the scsi command */
2601 blksize = lpfc_cmd_blksize(sc);
2602 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2604 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2605 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2606 if (rc) {
2607 if (rc & BG_ERR_SWAP)
2608 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2609 if (rc & BG_ERR_CHECK)
2610 checking = 0;
2612 #endif
2614 split_offset = 0;
2615 do {
2616 /* Check to see if we ran out of space */
2617 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2618 return num_sge + 3;
2620 /* setup DISEED with what we have */
2621 diseed = (struct sli4_sge_diseed *) sgl;
2622 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2623 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2625 /* Endianness conversion if necessary */
2626 diseed->ref_tag = cpu_to_le32(reftag);
2627 diseed->ref_tag_tran = diseed->ref_tag;
2629 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
2630 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2632 } else {
2633 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2635 * When in this mode, the hardware will replace
2636 * the guard tag from the host with a
2637 * newly generated good CRC for the wire.
2638 * Switch to raw mode here to avoid this
2639 * behavior. What the host sends gets put on the wire.
2641 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2642 txop = BG_OP_RAW_MODE;
2643 rxop = BG_OP_RAW_MODE;
2648 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2649 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2650 else
2651 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2653 /* setup DISEED with the rest of the info */
2654 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2655 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2657 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2658 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2660 /* Endianness conversion if necessary for DISEED */
2661 diseed->word2 = cpu_to_le32(diseed->word2);
2662 diseed->word3 = cpu_to_le32(diseed->word3);
2664 /* advance sgl and increment bde count */
2665 num_sge++;
2666 sgl++;
2668 /* setup the first BDE that points to protection buffer */
2669 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2670 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2672 /* must be integer multiple of the DIF block length */
2673 BUG_ON(protgroup_len % 8);
2675 /* Now setup DIF SGE */
2676 sgl->word2 = 0;
2677 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2678 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2679 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2680 sgl->word2 = cpu_to_le32(sgl->word2);
2682 protgrp_blks = protgroup_len / 8;
2683 protgrp_bytes = protgrp_blks * blksize;
2685 /* check if DIF SGE is crossing the 4K boundary; if so split */
2686 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2687 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2688 protgroup_offset += protgroup_remainder;
2689 protgrp_blks = protgroup_remainder / 8;
2690 protgrp_bytes = protgrp_blks * blksize;
2691 } else {
2692 protgroup_offset = 0;
2693 curr_prot++;
2696 num_sge++;
2698 /* setup SGE's for data blocks associated with DIF data */
2699 pgdone = 0;
2700 subtotal = 0; /* total bytes processed for current prot grp */
2701 while (!pgdone) {
2702 /* Check to see if we ran out of space */
2703 if (num_sge >= phba->cfg_total_seg_cnt)
2704 return num_sge + 1;
2706 if (!sgde) {
2707 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2708 "9086 BLKGRD:%s Invalid data segment\n",
2709 __func__);
2710 return 0;
2712 sgl++;
2713 dataphysaddr = sg_dma_address(sgde) + split_offset;
2715 remainder = sg_dma_len(sgde) - split_offset;
2717 if ((subtotal + remainder) <= protgrp_bytes) {
2718 /* we can use this whole buffer */
2719 dma_len = remainder;
2720 split_offset = 0;
2722 if ((subtotal + remainder) == protgrp_bytes)
2723 pgdone = 1;
2724 } else {
2725 /* must split this buffer with next prot grp */
2726 dma_len = protgrp_bytes - subtotal;
2727 split_offset += dma_len;
2730 subtotal += dma_len;
2732 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2733 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2734 bf_set(lpfc_sli4_sge_last, sgl, 0);
2735 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2736 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2738 sgl->sge_len = cpu_to_le32(dma_len);
2739 dma_offset += dma_len;
2741 num_sge++;
2742 curr_data++;
2744 if (split_offset)
2745 break;
2747 /* Move to the next s/g segment if possible */
2748 sgde = sg_next(sgde);
2751 if (protgroup_offset) {
2752 /* update the reference tag */
2753 reftag += protgrp_blks;
2754 sgl++;
2755 continue;
2758 /* are we done ? */
2759 if (curr_prot == protcnt) {
2760 bf_set(lpfc_sli4_sge_last, sgl, 1);
2761 alldone = 1;
2762 } else if (curr_prot < protcnt) {
2763 /* advance to next prot buffer */
2764 sgpe = sg_next(sgpe);
2765 sgl++;
2767 /* update the reference tag */
2768 reftag += protgrp_blks;
2769 } else {
2770 /* if we're here, we have a bug */
2771 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2772 "9085 BLKGRD: bug in %s\n", __func__);
2775 } while (!alldone);
2777 out:
2779 return num_sge;
2783 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2784 * @phba: The Hba for which this call is being executed.
2785 * @sc: pointer to scsi command we're working on
2787 * Given a SCSI command that supports DIF, determine composition of protection
2788 * groups involved in setting up buffer lists
2790 * Returns: Protection group type (with or without DIF)
2793 static int
2794 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2796 int ret = LPFC_PG_TYPE_INVALID;
2797 unsigned char op = scsi_get_prot_op(sc);
2799 switch (op) {
2800 case SCSI_PROT_READ_STRIP:
2801 case SCSI_PROT_WRITE_INSERT:
2802 ret = LPFC_PG_TYPE_NO_DIF;
2803 break;
2804 case SCSI_PROT_READ_INSERT:
2805 case SCSI_PROT_WRITE_STRIP:
2806 case SCSI_PROT_READ_PASS:
2807 case SCSI_PROT_WRITE_PASS:
2808 ret = LPFC_PG_TYPE_DIF_BUF;
2809 break;
2810 default:
2811 if (phba)
2812 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2813 "9021 Unsupported protection op:%d\n",
2814 op);
2815 break;
2817 return ret;
2821 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2822 * @phba: The Hba for which this call is being executed.
2823 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2825 * Adjust the data length to account for how much data
2826 * is actually on the wire.
2828 * returns the adjusted data length
2830 static int
2831 lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2832 struct lpfc_scsi_buf *lpfc_cmd)
2834 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2835 int fcpdl;
2837 fcpdl = scsi_bufflen(sc);
2839 /* Check if there is protection data on the wire */
2840 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2841 /* Read check for protection data */
2842 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2843 return fcpdl;
2845 } else {
2846 /* Write check for protection data */
2847 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2848 return fcpdl;
2852 * If we are in DIF Type 1 mode every data block has a 8 byte
2853 * DIF (trailer) attached to it. Must ajust FCP data length
2854 * to account for the protection data.
2856 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
2858 return fcpdl;
2862 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2863 * @phba: The Hba for which this call is being executed.
2864 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2866 * This is the protection/DIF aware version of
2867 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2868 * two functions eventually, but for now, it's here
2870 static int
2871 lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2872 struct lpfc_scsi_buf *lpfc_cmd)
2874 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2875 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2876 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2877 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2878 uint32_t num_bde = 0;
2879 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2880 int prot_group_type = 0;
2881 int fcpdl;
2884 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2885 * fcp_rsp regions to the first data bde entry
2887 bpl += 2;
2888 if (scsi_sg_count(scsi_cmnd)) {
2890 * The driver stores the segment count returned from pci_map_sg
2891 * because this a count of dma-mappings used to map the use_sg
2892 * pages. They are not guaranteed to be the same for those
2893 * architectures that implement an IOMMU.
2895 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2896 scsi_sglist(scsi_cmnd),
2897 scsi_sg_count(scsi_cmnd), datadir);
2898 if (unlikely(!datasegcnt))
2899 return 1;
2901 lpfc_cmd->seg_cnt = datasegcnt;
2903 /* First check if data segment count from SCSI Layer is good */
2904 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2905 goto err;
2907 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2909 switch (prot_group_type) {
2910 case LPFC_PG_TYPE_NO_DIF:
2912 /* Here we need to add a PDE5 and PDE6 to the count */
2913 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2914 goto err;
2916 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2917 datasegcnt);
2918 /* we should have 2 or more entries in buffer list */
2919 if (num_bde < 2)
2920 goto err;
2921 break;
2923 case LPFC_PG_TYPE_DIF_BUF:
2925 * This type indicates that protection buffers are
2926 * passed to the driver, so that needs to be prepared
2927 * for DMA
2929 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2930 scsi_prot_sglist(scsi_cmnd),
2931 scsi_prot_sg_count(scsi_cmnd), datadir);
2932 if (unlikely(!protsegcnt)) {
2933 scsi_dma_unmap(scsi_cmnd);
2934 return 1;
2937 lpfc_cmd->prot_seg_cnt = protsegcnt;
2940 * There is a minimun of 4 BPLs used for every
2941 * protection data segment.
2943 if ((lpfc_cmd->prot_seg_cnt * 4) >
2944 (phba->cfg_total_seg_cnt - 2))
2945 goto err;
2947 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2948 datasegcnt, protsegcnt);
2949 /* we should have 3 or more entries in buffer list */
2950 if ((num_bde < 3) ||
2951 (num_bde > phba->cfg_total_seg_cnt))
2952 goto err;
2953 break;
2955 case LPFC_PG_TYPE_INVALID:
2956 default:
2957 scsi_dma_unmap(scsi_cmnd);
2958 lpfc_cmd->seg_cnt = 0;
2960 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2961 "9022 Unexpected protection group %i\n",
2962 prot_group_type);
2963 return 1;
2968 * Finish initializing those IOCB fields that are dependent on the
2969 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2970 * reinitialized since all iocb memory resources are used many times
2971 * for transmit, receive, and continuation bpl's.
2973 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2974 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2975 iocb_cmd->ulpBdeCount = 1;
2976 iocb_cmd->ulpLe = 1;
2978 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
2979 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2982 * Due to difference in data length between DIF/non-DIF paths,
2983 * we need to set word 4 of IOCB here
2985 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2987 return 0;
2988 err:
2989 if (lpfc_cmd->seg_cnt)
2990 scsi_dma_unmap(scsi_cmnd);
2991 if (lpfc_cmd->prot_seg_cnt)
2992 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2993 scsi_prot_sg_count(scsi_cmnd),
2994 scsi_cmnd->sc_data_direction);
2996 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2997 "9023 Cannot setup S/G List for HBA"
2998 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2999 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3000 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3001 prot_group_type, num_bde);
3003 lpfc_cmd->seg_cnt = 0;
3004 lpfc_cmd->prot_seg_cnt = 0;
3005 return 1;
3009 * This function calcuates the T10 DIF guard tag
3010 * on the specified data using a CRC algorithmn
3011 * using crc_t10dif.
3013 uint16_t
3014 lpfc_bg_crc(uint8_t *data, int count)
3016 uint16_t crc = 0;
3017 uint16_t x;
3019 crc = crc_t10dif(data, count);
3020 x = cpu_to_be16(crc);
3021 return x;
3025 * This function calcuates the T10 DIF guard tag
3026 * on the specified data using a CSUM algorithmn
3027 * using ip_compute_csum.
3029 uint16_t
3030 lpfc_bg_csum(uint8_t *data, int count)
3032 uint16_t ret;
3034 ret = ip_compute_csum(data, count);
3035 return ret;
3039 * This function examines the protection data to try to determine
3040 * what type of T10-DIF error occurred.
3042 void
3043 lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3045 struct scatterlist *sgpe; /* s/g prot entry */
3046 struct scatterlist *sgde; /* s/g data entry */
3047 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3048 struct scsi_dif_tuple *src = NULL;
3049 uint8_t *data_src = NULL;
3050 uint16_t guard_tag, guard_type;
3051 uint16_t start_app_tag, app_tag;
3052 uint32_t start_ref_tag, ref_tag;
3053 int prot, protsegcnt;
3054 int err_type, len, data_len;
3055 int chk_ref, chk_app, chk_guard;
3056 uint16_t sum;
3057 unsigned blksize;
3059 err_type = BGS_GUARD_ERR_MASK;
3060 sum = 0;
3061 guard_tag = 0;
3063 /* First check to see if there is protection data to examine */
3064 prot = scsi_get_prot_op(cmd);
3065 if ((prot == SCSI_PROT_READ_STRIP) ||
3066 (prot == SCSI_PROT_WRITE_INSERT) ||
3067 (prot == SCSI_PROT_NORMAL))
3068 goto out;
3070 /* Currently the driver just supports ref_tag and guard_tag checking */
3071 chk_ref = 1;
3072 chk_app = 0;
3073 chk_guard = 0;
3075 /* Setup a ptr to the protection data provided by the SCSI host */
3076 sgpe = scsi_prot_sglist(cmd);
3077 protsegcnt = lpfc_cmd->prot_seg_cnt;
3079 if (sgpe && protsegcnt) {
3082 * We will only try to verify guard tag if the segment
3083 * data length is a multiple of the blksize.
3085 sgde = scsi_sglist(cmd);
3086 blksize = lpfc_cmd_blksize(cmd);
3087 data_src = (uint8_t *)sg_virt(sgde);
3088 data_len = sgde->length;
3089 if ((data_len & (blksize - 1)) == 0)
3090 chk_guard = 1;
3091 guard_type = scsi_host_get_guard(cmd->device->host);
3093 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3094 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
3095 start_app_tag = src->app_tag;
3096 len = sgpe->length;
3097 while (src && protsegcnt) {
3098 while (len) {
3101 * First check to see if a protection data
3102 * check is valid
3104 if ((src->ref_tag == 0xffffffff) ||
3105 (src->app_tag == 0xffff)) {
3106 start_ref_tag++;
3107 goto skipit;
3110 /* First Guard Tag checking */
3111 if (chk_guard) {
3112 guard_tag = src->guard_tag;
3113 if (lpfc_cmd_guard_csum(cmd))
3114 sum = lpfc_bg_csum(data_src,
3115 blksize);
3116 else
3117 sum = lpfc_bg_crc(data_src,
3118 blksize);
3119 if ((guard_tag != sum)) {
3120 err_type = BGS_GUARD_ERR_MASK;
3121 goto out;
3125 /* Reference Tag checking */
3126 ref_tag = be32_to_cpu(src->ref_tag);
3127 if (chk_ref && (ref_tag != start_ref_tag)) {
3128 err_type = BGS_REFTAG_ERR_MASK;
3129 goto out;
3131 start_ref_tag++;
3133 /* App Tag checking */
3134 app_tag = src->app_tag;
3135 if (chk_app && (app_tag != start_app_tag)) {
3136 err_type = BGS_APPTAG_ERR_MASK;
3137 goto out;
3139 skipit:
3140 len -= sizeof(struct scsi_dif_tuple);
3141 if (len < 0)
3142 len = 0;
3143 src++;
3145 data_src += blksize;
3146 data_len -= blksize;
3149 * Are we at the end of the Data segment?
3150 * The data segment is only used for Guard
3151 * tag checking.
3153 if (chk_guard && (data_len == 0)) {
3154 chk_guard = 0;
3155 sgde = sg_next(sgde);
3156 if (!sgde)
3157 goto out;
3159 data_src = (uint8_t *)sg_virt(sgde);
3160 data_len = sgde->length;
3161 if ((data_len & (blksize - 1)) == 0)
3162 chk_guard = 1;
3166 /* Goto the next Protection data segment */
3167 sgpe = sg_next(sgpe);
3168 if (sgpe) {
3169 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3170 len = sgpe->length;
3171 } else {
3172 src = NULL;
3174 protsegcnt--;
3177 out:
3178 if (err_type == BGS_GUARD_ERR_MASK) {
3179 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3180 0x10, 0x1);
3181 cmd->result = DRIVER_SENSE << 24
3182 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3183 phba->bg_guard_err_cnt++;
3184 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3185 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3186 (unsigned long)scsi_get_lba(cmd),
3187 sum, guard_tag);
3189 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3190 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3191 0x10, 0x3);
3192 cmd->result = DRIVER_SENSE << 24
3193 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3195 phba->bg_reftag_err_cnt++;
3196 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3197 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3198 (unsigned long)scsi_get_lba(cmd),
3199 ref_tag, start_ref_tag);
3201 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3202 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3203 0x10, 0x2);
3204 cmd->result = DRIVER_SENSE << 24
3205 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3207 phba->bg_apptag_err_cnt++;
3208 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3209 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3210 (unsigned long)scsi_get_lba(cmd),
3211 app_tag, start_app_tag);
3217 * This function checks for BlockGuard errors detected by
3218 * the HBA. In case of errors, the ASC/ASCQ fields in the
3219 * sense buffer will be set accordingly, paired with
3220 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3221 * detected corruption.
3223 * Returns:
3224 * 0 - No error found
3225 * 1 - BlockGuard error found
3226 * -1 - Internal error (bad profile, ...etc)
3228 static int
3229 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3230 struct lpfc_iocbq *pIocbOut)
3232 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3233 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3234 int ret = 0;
3235 uint32_t bghm = bgf->bghm;
3236 uint32_t bgstat = bgf->bgstat;
3237 uint64_t failing_sector = 0;
3239 spin_lock(&_dump_buf_lock);
3240 if (!_dump_buf_done) {
3241 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3242 " Data for %u blocks to debugfs\n",
3243 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3244 lpfc_debug_save_data(phba, cmd);
3246 /* If we have a prot sgl, save the DIF buffer */
3247 if (lpfc_prot_group_type(phba, cmd) ==
3248 LPFC_PG_TYPE_DIF_BUF) {
3249 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3250 "Saving DIF for %u blocks to debugfs\n",
3251 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3252 lpfc_debug_save_dif(phba, cmd);
3255 _dump_buf_done = 1;
3257 spin_unlock(&_dump_buf_lock);
3259 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3260 cmd->result = ScsiResult(DID_ERROR, 0);
3261 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3262 "9072 BLKGRD: Invalid BG Profile in cmd"
3263 " 0x%x lba 0x%llx blk cnt 0x%x "
3264 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3265 (unsigned long long)scsi_get_lba(cmd),
3266 blk_rq_sectors(cmd->request), bgstat, bghm);
3267 ret = (-1);
3268 goto out;
3271 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3272 cmd->result = ScsiResult(DID_ERROR, 0);
3273 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3274 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3275 " 0x%x lba 0x%llx blk cnt 0x%x "
3276 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3277 (unsigned long long)scsi_get_lba(cmd),
3278 blk_rq_sectors(cmd->request), bgstat, bghm);
3279 ret = (-1);
3280 goto out;
3283 if (lpfc_bgs_get_guard_err(bgstat)) {
3284 ret = 1;
3286 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3287 0x10, 0x1);
3288 cmd->result = DRIVER_SENSE << 24
3289 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3290 phba->bg_guard_err_cnt++;
3291 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3292 "9055 BLKGRD: Guard Tag error in cmd"
3293 " 0x%x lba 0x%llx blk cnt 0x%x "
3294 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3295 (unsigned long long)scsi_get_lba(cmd),
3296 blk_rq_sectors(cmd->request), bgstat, bghm);
3299 if (lpfc_bgs_get_reftag_err(bgstat)) {
3300 ret = 1;
3302 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3303 0x10, 0x3);
3304 cmd->result = DRIVER_SENSE << 24
3305 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3307 phba->bg_reftag_err_cnt++;
3308 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3309 "9056 BLKGRD: Ref Tag error in cmd"
3310 " 0x%x lba 0x%llx blk cnt 0x%x "
3311 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3312 (unsigned long long)scsi_get_lba(cmd),
3313 blk_rq_sectors(cmd->request), bgstat, bghm);
3316 if (lpfc_bgs_get_apptag_err(bgstat)) {
3317 ret = 1;
3319 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3320 0x10, 0x2);
3321 cmd->result = DRIVER_SENSE << 24
3322 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3324 phba->bg_apptag_err_cnt++;
3325 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3326 "9061 BLKGRD: App Tag error in cmd"
3327 " 0x%x lba 0x%llx blk cnt 0x%x "
3328 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3329 (unsigned long long)scsi_get_lba(cmd),
3330 blk_rq_sectors(cmd->request), bgstat, bghm);
3333 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3335 * setup sense data descriptor 0 per SPC-4 as an information
3336 * field, and put the failing LBA in it.
3337 * This code assumes there was also a guard/app/ref tag error
3338 * indication.
3340 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3341 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3342 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3343 cmd->sense_buffer[10] = 0x80; /* Validity bit */
3345 /* bghm is a "on the wire" FC frame based count */
3346 switch (scsi_get_prot_op(cmd)) {
3347 case SCSI_PROT_READ_INSERT:
3348 case SCSI_PROT_WRITE_STRIP:
3349 bghm /= cmd->device->sector_size;
3350 break;
3351 case SCSI_PROT_READ_STRIP:
3352 case SCSI_PROT_WRITE_INSERT:
3353 case SCSI_PROT_READ_PASS:
3354 case SCSI_PROT_WRITE_PASS:
3355 bghm /= (cmd->device->sector_size +
3356 sizeof(struct scsi_dif_tuple));
3357 break;
3360 failing_sector = scsi_get_lba(cmd);
3361 failing_sector += bghm;
3363 /* Descriptor Information */
3364 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3367 if (!ret) {
3368 /* No error was reported - problem in FW? */
3369 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3370 "9057 BLKGRD: Unknown error in cmd"
3371 " 0x%x lba 0x%llx blk cnt 0x%x "
3372 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3373 (unsigned long long)scsi_get_lba(cmd),
3374 blk_rq_sectors(cmd->request), bgstat, bghm);
3376 /* Calcuate what type of error it was */
3377 lpfc_calc_bg_err(phba, lpfc_cmd);
3379 out:
3380 return ret;
3384 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3385 * @phba: The Hba for which this call is being executed.
3386 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3388 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3389 * field of @lpfc_cmd for device with SLI-4 interface spec.
3391 * Return codes:
3392 * 1 - Error
3393 * 0 - Success
3395 static int
3396 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3398 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3399 struct scatterlist *sgel = NULL;
3400 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3401 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
3402 struct sli4_sge *first_data_sgl;
3403 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3404 dma_addr_t physaddr;
3405 uint32_t num_bde = 0;
3406 uint32_t dma_len;
3407 uint32_t dma_offset = 0;
3408 int nseg;
3409 struct ulp_bde64 *bde;
3412 * There are three possibilities here - use scatter-gather segment, use
3413 * the single mapping, or neither. Start the lpfc command prep by
3414 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3415 * data bde entry.
3417 if (scsi_sg_count(scsi_cmnd)) {
3419 * The driver stores the segment count returned from pci_map_sg
3420 * because this a count of dma-mappings used to map the use_sg
3421 * pages. They are not guaranteed to be the same for those
3422 * architectures that implement an IOMMU.
3425 nseg = scsi_dma_map(scsi_cmnd);
3426 if (unlikely(!nseg))
3427 return 1;
3428 sgl += 1;
3429 /* clear the last flag in the fcp_rsp map entry */
3430 sgl->word2 = le32_to_cpu(sgl->word2);
3431 bf_set(lpfc_sli4_sge_last, sgl, 0);
3432 sgl->word2 = cpu_to_le32(sgl->word2);
3433 sgl += 1;
3434 first_data_sgl = sgl;
3435 lpfc_cmd->seg_cnt = nseg;
3436 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3437 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3438 " %s: Too many sg segments from "
3439 "dma_map_sg. Config %d, seg_cnt %d\n",
3440 __func__, phba->cfg_sg_seg_cnt,
3441 lpfc_cmd->seg_cnt);
3442 lpfc_cmd->seg_cnt = 0;
3443 scsi_dma_unmap(scsi_cmnd);
3444 return 1;
3448 * The driver established a maximum scatter-gather segment count
3449 * during probe that limits the number of sg elements in any
3450 * single scsi command. Just run through the seg_cnt and format
3451 * the sge's.
3452 * When using SLI-3 the driver will try to fit all the BDEs into
3453 * the IOCB. If it can't then the BDEs get added to a BPL as it
3454 * does for SLI-2 mode.
3456 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3457 physaddr = sg_dma_address(sgel);
3458 dma_len = sg_dma_len(sgel);
3459 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3460 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
3461 sgl->word2 = le32_to_cpu(sgl->word2);
3462 if ((num_bde + 1) == nseg)
3463 bf_set(lpfc_sli4_sge_last, sgl, 1);
3464 else
3465 bf_set(lpfc_sli4_sge_last, sgl, 0);
3466 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3467 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
3468 sgl->word2 = cpu_to_le32(sgl->word2);
3469 sgl->sge_len = cpu_to_le32(dma_len);
3470 dma_offset += dma_len;
3471 sgl++;
3473 /* setup the performance hint (first data BDE) if enabled */
3474 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3475 bde = (struct ulp_bde64 *)
3476 &(iocb_cmd->unsli3.sli3Words[5]);
3477 bde->addrLow = first_data_sgl->addr_lo;
3478 bde->addrHigh = first_data_sgl->addr_hi;
3479 bde->tus.f.bdeSize =
3480 le32_to_cpu(first_data_sgl->sge_len);
3481 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3482 bde->tus.w = cpu_to_le32(bde->tus.w);
3484 } else {
3485 sgl += 1;
3486 /* clear the last flag in the fcp_rsp map entry */
3487 sgl->word2 = le32_to_cpu(sgl->word2);
3488 bf_set(lpfc_sli4_sge_last, sgl, 1);
3489 sgl->word2 = cpu_to_le32(sgl->word2);
3493 * Finish initializing those IOCB fields that are dependent on the
3494 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3495 * explicitly reinitialized.
3496 * all iocb memory resources are reused.
3498 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3501 * Due to difference in data length between DIF/non-DIF paths,
3502 * we need to set word 4 of IOCB here
3504 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3505 return 0;
3509 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3510 * @phba: The Hba for which this call is being executed.
3511 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3513 * This is the protection/DIF aware version of
3514 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3515 * two functions eventually, but for now, it's here
3517 static int
3518 lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3519 struct lpfc_scsi_buf *lpfc_cmd)
3521 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3522 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3523 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3524 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3525 uint32_t num_sge = 0;
3526 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3527 int prot_group_type = 0;
3528 int fcpdl;
3531 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3532 * fcp_rsp regions to the first data sge entry
3534 if (scsi_sg_count(scsi_cmnd)) {
3536 * The driver stores the segment count returned from pci_map_sg
3537 * because this a count of dma-mappings used to map the use_sg
3538 * pages. They are not guaranteed to be the same for those
3539 * architectures that implement an IOMMU.
3541 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3542 scsi_sglist(scsi_cmnd),
3543 scsi_sg_count(scsi_cmnd), datadir);
3544 if (unlikely(!datasegcnt))
3545 return 1;
3547 sgl += 1;
3548 /* clear the last flag in the fcp_rsp map entry */
3549 sgl->word2 = le32_to_cpu(sgl->word2);
3550 bf_set(lpfc_sli4_sge_last, sgl, 0);
3551 sgl->word2 = cpu_to_le32(sgl->word2);
3553 sgl += 1;
3554 lpfc_cmd->seg_cnt = datasegcnt;
3556 /* First check if data segment count from SCSI Layer is good */
3557 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3558 goto err;
3560 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3562 switch (prot_group_type) {
3563 case LPFC_PG_TYPE_NO_DIF:
3564 /* Here we need to add a DISEED to the count */
3565 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3566 goto err;
3568 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3569 datasegcnt);
3571 /* we should have 2 or more entries in buffer list */
3572 if (num_sge < 2)
3573 goto err;
3574 break;
3576 case LPFC_PG_TYPE_DIF_BUF:
3578 * This type indicates that protection buffers are
3579 * passed to the driver, so that needs to be prepared
3580 * for DMA
3582 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3583 scsi_prot_sglist(scsi_cmnd),
3584 scsi_prot_sg_count(scsi_cmnd), datadir);
3585 if (unlikely(!protsegcnt)) {
3586 scsi_dma_unmap(scsi_cmnd);
3587 return 1;
3590 lpfc_cmd->prot_seg_cnt = protsegcnt;
3592 * There is a minimun of 3 SGEs used for every
3593 * protection data segment.
3595 if ((lpfc_cmd->prot_seg_cnt * 3) >
3596 (phba->cfg_total_seg_cnt - 2))
3597 goto err;
3599 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3600 datasegcnt, protsegcnt);
3602 /* we should have 3 or more entries in buffer list */
3603 if ((num_sge < 3) ||
3604 (num_sge > phba->cfg_total_seg_cnt))
3605 goto err;
3606 break;
3608 case LPFC_PG_TYPE_INVALID:
3609 default:
3610 scsi_dma_unmap(scsi_cmnd);
3611 lpfc_cmd->seg_cnt = 0;
3613 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3614 "9083 Unexpected protection group %i\n",
3615 prot_group_type);
3616 return 1;
3620 switch (scsi_get_prot_op(scsi_cmnd)) {
3621 case SCSI_PROT_WRITE_STRIP:
3622 case SCSI_PROT_READ_STRIP:
3623 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3624 break;
3625 case SCSI_PROT_WRITE_INSERT:
3626 case SCSI_PROT_READ_INSERT:
3627 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3628 break;
3629 case SCSI_PROT_WRITE_PASS:
3630 case SCSI_PROT_READ_PASS:
3631 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3632 break;
3635 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3636 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3639 * Due to difference in data length between DIF/non-DIF paths,
3640 * we need to set word 4 of IOCB here
3642 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3644 return 0;
3645 err:
3646 if (lpfc_cmd->seg_cnt)
3647 scsi_dma_unmap(scsi_cmnd);
3648 if (lpfc_cmd->prot_seg_cnt)
3649 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3650 scsi_prot_sg_count(scsi_cmnd),
3651 scsi_cmnd->sc_data_direction);
3653 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3654 "9084 Cannot setup S/G List for HBA"
3655 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3656 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3657 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3658 prot_group_type, num_sge);
3660 lpfc_cmd->seg_cnt = 0;
3661 lpfc_cmd->prot_seg_cnt = 0;
3662 return 1;
3666 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3667 * @phba: The Hba for which this call is being executed.
3668 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3670 * This routine wraps the actual DMA mapping function pointer from the
3671 * lpfc_hba struct.
3673 * Return codes:
3674 * 1 - Error
3675 * 0 - Success
3677 static inline int
3678 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3680 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3684 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3685 * using BlockGuard.
3686 * @phba: The Hba for which this call is being executed.
3687 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3689 * This routine wraps the actual DMA mapping function pointer from the
3690 * lpfc_hba struct.
3692 * Return codes:
3693 * 1 - Error
3694 * 0 - Success
3696 static inline int
3697 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3699 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3703 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
3704 * @phba: Pointer to hba context object.
3705 * @vport: Pointer to vport object.
3706 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3707 * @rsp_iocb: Pointer to response iocb object which reported error.
3709 * This function posts an event when there is a SCSI command reporting
3710 * error from the scsi device.
3712 static void
3713 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3714 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3715 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3716 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3717 uint32_t resp_info = fcprsp->rspStatus2;
3718 uint32_t scsi_status = fcprsp->rspStatus3;
3719 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3720 struct lpfc_fast_path_event *fast_path_evt = NULL;
3721 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3722 unsigned long flags;
3724 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3725 return;
3727 /* If there is queuefull or busy condition send a scsi event */
3728 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3729 (cmnd->result == SAM_STAT_BUSY)) {
3730 fast_path_evt = lpfc_alloc_fast_evt(phba);
3731 if (!fast_path_evt)
3732 return;
3733 fast_path_evt->un.scsi_evt.event_type =
3734 FC_REG_SCSI_EVENT;
3735 fast_path_evt->un.scsi_evt.subcategory =
3736 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3737 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3738 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3739 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3740 &pnode->nlp_portname, sizeof(struct lpfc_name));
3741 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3742 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3743 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3744 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3745 fast_path_evt = lpfc_alloc_fast_evt(phba);
3746 if (!fast_path_evt)
3747 return;
3748 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3749 FC_REG_SCSI_EVENT;
3750 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3751 LPFC_EVENT_CHECK_COND;
3752 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3753 cmnd->device->lun;
3754 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3755 &pnode->nlp_portname, sizeof(struct lpfc_name));
3756 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3757 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3758 fast_path_evt->un.check_cond_evt.sense_key =
3759 cmnd->sense_buffer[2] & 0xf;
3760 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3761 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3762 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3763 fcpi_parm &&
3764 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3765 ((scsi_status == SAM_STAT_GOOD) &&
3766 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3768 * If status is good or resid does not match with fcp_param and
3769 * there is valid fcpi_parm, then there is a read_check error
3771 fast_path_evt = lpfc_alloc_fast_evt(phba);
3772 if (!fast_path_evt)
3773 return;
3774 fast_path_evt->un.read_check_error.header.event_type =
3775 FC_REG_FABRIC_EVENT;
3776 fast_path_evt->un.read_check_error.header.subcategory =
3777 LPFC_EVENT_FCPRDCHKERR;
3778 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3779 &pnode->nlp_portname, sizeof(struct lpfc_name));
3780 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3781 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3782 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3783 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3784 fast_path_evt->un.read_check_error.fcpiparam =
3785 fcpi_parm;
3786 } else
3787 return;
3789 fast_path_evt->vport = vport;
3790 spin_lock_irqsave(&phba->hbalock, flags);
3791 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3792 spin_unlock_irqrestore(&phba->hbalock, flags);
3793 lpfc_worker_wake_up(phba);
3794 return;
3798 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3799 * @phba: The HBA for which this call is being executed.
3800 * @psb: The scsi buffer which is going to be un-mapped.
3802 * This routine does DMA un-mapping of scatter gather list of scsi command
3803 * field of @lpfc_cmd for device with SLI-3 interface spec.
3805 static void
3806 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
3809 * There are only two special cases to consider. (1) the scsi command
3810 * requested scatter-gather usage or (2) the scsi command allocated
3811 * a request buffer, but did not request use_sg. There is a third
3812 * case, but it does not require resource deallocation.
3814 if (psb->seg_cnt > 0)
3815 scsi_dma_unmap(psb->pCmd);
3816 if (psb->prot_seg_cnt > 0)
3817 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3818 scsi_prot_sg_count(psb->pCmd),
3819 psb->pCmd->sc_data_direction);
3823 * lpfc_handler_fcp_err - FCP response handler
3824 * @vport: The virtual port for which this call is being executed.
3825 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3826 * @rsp_iocb: The response IOCB which contains FCP error.
3828 * This routine is called to process response IOCB with status field
3829 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3830 * based upon SCSI and FCP error.
3832 static void
3833 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3834 struct lpfc_iocbq *rsp_iocb)
3836 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3837 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3838 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3839 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3840 uint32_t resp_info = fcprsp->rspStatus2;
3841 uint32_t scsi_status = fcprsp->rspStatus3;
3842 uint32_t *lp;
3843 uint32_t host_status = DID_OK;
3844 uint32_t rsplen = 0;
3845 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
3849 * If this is a task management command, there is no
3850 * scsi packet associated with this lpfc_cmd. The driver
3851 * consumes it.
3853 if (fcpcmd->fcpCntl2) {
3854 scsi_status = 0;
3855 goto out;
3858 if (resp_info & RSP_LEN_VALID) {
3859 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3860 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
3861 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3862 "2719 Invalid response length: "
3863 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3864 cmnd->device->id,
3865 cmnd->device->lun, cmnd->cmnd[0],
3866 rsplen);
3867 host_status = DID_ERROR;
3868 goto out;
3870 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3871 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3872 "2757 Protocol failure detected during "
3873 "processing of FCP I/O op: "
3874 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3875 cmnd->device->id,
3876 cmnd->device->lun, cmnd->cmnd[0],
3877 fcprsp->rspInfo3);
3878 host_status = DID_ERROR;
3879 goto out;
3883 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3884 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3885 if (snslen > SCSI_SENSE_BUFFERSIZE)
3886 snslen = SCSI_SENSE_BUFFERSIZE;
3888 if (resp_info & RSP_LEN_VALID)
3889 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3890 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3892 lp = (uint32_t *)cmnd->sense_buffer;
3894 /* special handling for under run conditions */
3895 if (!scsi_status && (resp_info & RESID_UNDER)) {
3896 /* don't log under runs if fcp set... */
3897 if (vport->cfg_log_verbose & LOG_FCP)
3898 logit = LOG_FCP_ERROR;
3899 /* unless operator says so */
3900 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3901 logit = LOG_FCP_UNDER;
3904 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3905 "9024 FCP command x%x failed: x%x SNS x%x x%x "
3906 "Data: x%x x%x x%x x%x x%x\n",
3907 cmnd->cmnd[0], scsi_status,
3908 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3909 be32_to_cpu(fcprsp->rspResId),
3910 be32_to_cpu(fcprsp->rspSnsLen),
3911 be32_to_cpu(fcprsp->rspRspLen),
3912 fcprsp->rspInfo3);
3914 scsi_set_resid(cmnd, 0);
3915 if (resp_info & RESID_UNDER) {
3916 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
3918 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
3919 "9025 FCP Read Underrun, expected %d, "
3920 "residual %d Data: x%x x%x x%x\n",
3921 be32_to_cpu(fcpcmd->fcpDl),
3922 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3923 cmnd->underflow);
3926 * If there is an under run check if under run reported by
3927 * storage array is same as the under run reported by HBA.
3928 * If this is not same, there is a dropped frame.
3930 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3931 fcpi_parm &&
3932 (scsi_get_resid(cmnd) != fcpi_parm)) {
3933 lpfc_printf_vlog(vport, KERN_WARNING,
3934 LOG_FCP | LOG_FCP_ERROR,
3935 "9026 FCP Read Check Error "
3936 "and Underrun Data: x%x x%x x%x x%x\n",
3937 be32_to_cpu(fcpcmd->fcpDl),
3938 scsi_get_resid(cmnd), fcpi_parm,
3939 cmnd->cmnd[0]);
3940 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3941 host_status = DID_ERROR;
3944 * The cmnd->underflow is the minimum number of bytes that must
3945 * be transferred for this command. Provided a sense condition
3946 * is not present, make sure the actual amount transferred is at
3947 * least the underflow value or fail.
3949 if (!(resp_info & SNS_LEN_VALID) &&
3950 (scsi_status == SAM_STAT_GOOD) &&
3951 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3952 < cmnd->underflow)) {
3953 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3954 "9027 FCP command x%x residual "
3955 "underrun converted to error "
3956 "Data: x%x x%x x%x\n",
3957 cmnd->cmnd[0], scsi_bufflen(cmnd),
3958 scsi_get_resid(cmnd), cmnd->underflow);
3959 host_status = DID_ERROR;
3961 } else if (resp_info & RESID_OVER) {
3962 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3963 "9028 FCP command x%x residual overrun error. "
3964 "Data: x%x x%x\n", cmnd->cmnd[0],
3965 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
3966 host_status = DID_ERROR;
3969 * Check SLI validation that all the transfer was actually done
3970 * (fcpi_parm should be zero). Apply check only to reads.
3972 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
3973 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
3974 "9029 FCP Read Check Error Data: "
3975 "x%x x%x x%x x%x x%x\n",
3976 be32_to_cpu(fcpcmd->fcpDl),
3977 be32_to_cpu(fcprsp->rspResId),
3978 fcpi_parm, cmnd->cmnd[0], scsi_status);
3979 switch (scsi_status) {
3980 case SAM_STAT_GOOD:
3981 case SAM_STAT_CHECK_CONDITION:
3982 /* Fabric dropped a data frame. Fail any successful
3983 * command in which we detected dropped frames.
3984 * A status of good or some check conditions could
3985 * be considered a successful command.
3987 host_status = DID_ERROR;
3988 break;
3990 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3993 out:
3994 cmnd->result = ScsiResult(host_status, scsi_status);
3995 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
3999 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
4000 * @phba: The Hba for which this call is being executed.
4001 * @pIocbIn: The command IOCBQ for the scsi cmnd.
4002 * @pIocbOut: The response IOCBQ for the scsi cmnd.
4004 * This routine assigns scsi command result by looking into response IOCB
4005 * status field appropriately. This routine handles QUEUE FULL condition as
4006 * well by ramping down device queue depth.
4008 static void
4009 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4010 struct lpfc_iocbq *pIocbOut)
4012 struct lpfc_scsi_buf *lpfc_cmd =
4013 (struct lpfc_scsi_buf *) pIocbIn->context1;
4014 struct lpfc_vport *vport = pIocbIn->vport;
4015 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4016 struct lpfc_nodelist *pnode = rdata->pnode;
4017 struct scsi_cmnd *cmd;
4018 int result;
4019 struct scsi_device *tmp_sdev;
4020 int depth;
4021 unsigned long flags;
4022 struct lpfc_fast_path_event *fast_path_evt;
4023 struct Scsi_Host *shost;
4024 uint32_t queue_depth, scsi_id;
4025 uint32_t logit = LOG_FCP;
4027 /* Sanity check on return of outstanding command */
4028 if (!(lpfc_cmd->pCmd))
4029 return;
4030 cmd = lpfc_cmd->pCmd;
4031 shost = cmd->device->host;
4033 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
4034 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
4035 /* pick up SLI4 exhange busy status from HBA */
4036 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4038 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4039 if (lpfc_cmd->prot_data_type) {
4040 struct scsi_dif_tuple *src = NULL;
4042 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4044 * Used to restore any changes to protection
4045 * data for error injection.
4047 switch (lpfc_cmd->prot_data_type) {
4048 case LPFC_INJERR_REFTAG:
4049 src->ref_tag =
4050 lpfc_cmd->prot_data;
4051 break;
4052 case LPFC_INJERR_APPTAG:
4053 src->app_tag =
4054 (uint16_t)lpfc_cmd->prot_data;
4055 break;
4056 case LPFC_INJERR_GUARD:
4057 src->guard_tag =
4058 (uint16_t)lpfc_cmd->prot_data;
4059 break;
4060 default:
4061 break;
4064 lpfc_cmd->prot_data = 0;
4065 lpfc_cmd->prot_data_type = 0;
4066 lpfc_cmd->prot_data_segment = NULL;
4068 #endif
4069 if (pnode && NLP_CHK_NODE_ACT(pnode))
4070 atomic_dec(&pnode->cmd_pending);
4072 if (lpfc_cmd->status) {
4073 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4074 (lpfc_cmd->result & IOERR_DRVR_MASK))
4075 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4076 else if (lpfc_cmd->status >= IOSTAT_CNT)
4077 lpfc_cmd->status = IOSTAT_DEFAULT;
4078 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4079 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4080 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4081 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
4082 logit = 0;
4083 else
4084 logit = LOG_FCP | LOG_FCP_UNDER;
4085 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4086 "9030 FCP cmd x%x failed <%d/%d> "
4087 "status: x%x result: x%x "
4088 "sid: x%x did: x%x oxid: x%x "
4089 "Data: x%x x%x\n",
4090 cmd->cmnd[0],
4091 cmd->device ? cmd->device->id : 0xffff,
4092 cmd->device ? cmd->device->lun : 0xffff,
4093 lpfc_cmd->status, lpfc_cmd->result,
4094 vport->fc_myDID,
4095 (pnode) ? pnode->nlp_DID : 0,
4096 phba->sli_rev == LPFC_SLI_REV4 ?
4097 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4098 pIocbOut->iocb.ulpContext,
4099 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4101 switch (lpfc_cmd->status) {
4102 case IOSTAT_FCP_RSP_ERROR:
4103 /* Call FCP RSP handler to determine result */
4104 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
4105 break;
4106 case IOSTAT_NPORT_BSY:
4107 case IOSTAT_FABRIC_BSY:
4108 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
4109 fast_path_evt = lpfc_alloc_fast_evt(phba);
4110 if (!fast_path_evt)
4111 break;
4112 fast_path_evt->un.fabric_evt.event_type =
4113 FC_REG_FABRIC_EVENT;
4114 fast_path_evt->un.fabric_evt.subcategory =
4115 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4116 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4117 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4118 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4119 &pnode->nlp_portname,
4120 sizeof(struct lpfc_name));
4121 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4122 &pnode->nlp_nodename,
4123 sizeof(struct lpfc_name));
4125 fast_path_evt->vport = vport;
4126 fast_path_evt->work_evt.evt =
4127 LPFC_EVT_FASTPATH_MGMT_EVT;
4128 spin_lock_irqsave(&phba->hbalock, flags);
4129 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4130 &phba->work_list);
4131 spin_unlock_irqrestore(&phba->hbalock, flags);
4132 lpfc_worker_wake_up(phba);
4133 break;
4134 case IOSTAT_LOCAL_REJECT:
4135 case IOSTAT_REMOTE_STOP:
4136 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4137 lpfc_cmd->result ==
4138 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4139 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4140 lpfc_cmd->result ==
4141 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4142 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4143 break;
4145 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
4146 lpfc_cmd->result == IOERR_NO_RESOURCES ||
4147 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4148 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
4149 cmd->result = ScsiResult(DID_REQUEUE, 0);
4150 break;
4152 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4153 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4154 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4155 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4157 * This is a response for a BG enabled
4158 * cmd. Parse BG error
4160 lpfc_parse_bg_err(phba, lpfc_cmd,
4161 pIocbOut);
4162 break;
4163 } else {
4164 lpfc_printf_vlog(vport, KERN_WARNING,
4165 LOG_BG,
4166 "9031 non-zero BGSTAT "
4167 "on unprotected cmd\n");
4170 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4171 && (phba->sli_rev == LPFC_SLI_REV4)
4172 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4173 /* This IO was aborted by the target, we don't
4174 * know the rxid and because we did not send the
4175 * ABTS we cannot generate and RRQ.
4177 lpfc_set_rrq_active(phba, pnode,
4178 lpfc_cmd->cur_iocbq.sli4_lxritag,
4179 0, 0);
4181 /* else: fall through */
4182 default:
4183 cmd->result = ScsiResult(DID_ERROR, 0);
4184 break;
4187 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
4188 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4189 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4190 SAM_STAT_BUSY);
4191 } else
4192 cmd->result = ScsiResult(DID_OK, 0);
4194 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4195 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4197 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4198 "0710 Iodone <%d/%d> cmd %p, error "
4199 "x%x SNS x%x x%x Data: x%x x%x\n",
4200 cmd->device->id, cmd->device->lun, cmd,
4201 cmd->result, *lp, *(lp + 3), cmd->retries,
4202 scsi_get_resid(cmd));
4205 lpfc_update_stats(phba, lpfc_cmd);
4206 result = cmd->result;
4207 if (vport->cfg_max_scsicmpl_time &&
4208 time_after(jiffies, lpfc_cmd->start_time +
4209 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4210 spin_lock_irqsave(shost->host_lock, flags);
4211 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4212 if (pnode->cmd_qdepth >
4213 atomic_read(&pnode->cmd_pending) &&
4214 (atomic_read(&pnode->cmd_pending) >
4215 LPFC_MIN_TGT_QDEPTH) &&
4216 ((cmd->cmnd[0] == READ_10) ||
4217 (cmd->cmnd[0] == WRITE_10)))
4218 pnode->cmd_qdepth =
4219 atomic_read(&pnode->cmd_pending);
4221 pnode->last_change_time = jiffies;
4223 spin_unlock_irqrestore(shost->host_lock, flags);
4224 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4225 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
4226 time_after(jiffies, pnode->last_change_time +
4227 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
4228 spin_lock_irqsave(shost->host_lock, flags);
4229 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4230 / 100;
4231 depth = depth ? depth : 1;
4232 pnode->cmd_qdepth += depth;
4233 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4234 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
4235 pnode->last_change_time = jiffies;
4236 spin_unlock_irqrestore(shost->host_lock, flags);
4240 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4242 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4243 queue_depth = cmd->device->queue_depth;
4244 scsi_id = cmd->device->id;
4245 cmd->scsi_done(cmd);
4247 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4248 spin_lock_irqsave(&phba->hbalock, flags);
4249 lpfc_cmd->pCmd = NULL;
4250 spin_unlock_irqrestore(&phba->hbalock, flags);
4253 * If there is a thread waiting for command completion
4254 * wake up the thread.
4256 spin_lock_irqsave(shost->host_lock, flags);
4257 if (lpfc_cmd->waitq)
4258 wake_up(lpfc_cmd->waitq);
4259 spin_unlock_irqrestore(shost->host_lock, flags);
4260 lpfc_release_scsi_buf(phba, lpfc_cmd);
4261 return;
4264 if (!result)
4265 lpfc_rampup_queue_depth(vport, queue_depth);
4268 * Check for queue full. If the lun is reporting queue full, then
4269 * back off the lun queue depth to prevent target overloads.
4271 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4272 NLP_CHK_NODE_ACT(pnode)) {
4273 shost_for_each_device(tmp_sdev, shost) {
4274 if (tmp_sdev->id != scsi_id)
4275 continue;
4276 depth = scsi_track_queue_full(tmp_sdev,
4277 tmp_sdev->queue_depth-1);
4278 if (depth <= 0)
4279 continue;
4280 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4281 "0711 detected queue full - lun queue "
4282 "depth adjusted to %d.\n", depth);
4283 lpfc_send_sdev_queuedepth_change_event(phba, vport,
4284 pnode,
4285 tmp_sdev->lun,
4286 depth+1, depth);
4290 spin_lock_irqsave(&phba->hbalock, flags);
4291 lpfc_cmd->pCmd = NULL;
4292 spin_unlock_irqrestore(&phba->hbalock, flags);
4295 * If there is a thread waiting for command completion
4296 * wake up the thread.
4298 spin_lock_irqsave(shost->host_lock, flags);
4299 if (lpfc_cmd->waitq)
4300 wake_up(lpfc_cmd->waitq);
4301 spin_unlock_irqrestore(shost->host_lock, flags);
4303 lpfc_release_scsi_buf(phba, lpfc_cmd);
4307 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4308 * @data: A pointer to the immediate command data portion of the IOCB.
4309 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4311 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4312 * byte swapping the data to big endian format for transmission on the wire.
4314 static void
4315 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4317 int i, j;
4318 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4319 i += sizeof(uint32_t), j++) {
4320 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4325 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
4326 * @vport: The virtual port for which this call is being executed.
4327 * @lpfc_cmd: The scsi command which needs to send.
4328 * @pnode: Pointer to lpfc_nodelist.
4330 * This routine initializes fcp_cmnd and iocb data structure from scsi command
4331 * to transfer for device with SLI3 interface spec.
4333 static void
4334 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
4335 struct lpfc_nodelist *pnode)
4337 struct lpfc_hba *phba = vport->phba;
4338 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4339 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4340 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4341 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4342 int datadir = scsi_cmnd->sc_data_direction;
4343 char tag[2];
4344 uint8_t *ptr;
4345 bool sli4;
4346 uint32_t fcpdl;
4348 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4349 return;
4351 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
4352 /* clear task management bits */
4353 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
4355 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4356 &lpfc_cmd->fcp_cmnd->fcp_lun);
4358 ptr = &fcp_cmnd->fcpCdb[0];
4359 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4360 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4361 ptr += scsi_cmnd->cmd_len;
4362 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4365 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4366 switch (tag[0]) {
4367 case HEAD_OF_QUEUE_TAG:
4368 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4369 break;
4370 case ORDERED_QUEUE_TAG:
4371 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4372 break;
4373 default:
4374 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4375 break;
4377 } else
4378 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4380 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4383 * There are three possibilities here - use scatter-gather segment, use
4384 * the single mapping, or neither. Start the lpfc command prep by
4385 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4386 * data bde entry.
4388 if (scsi_sg_count(scsi_cmnd)) {
4389 if (datadir == DMA_TO_DEVICE) {
4390 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
4391 iocb_cmd->ulpPU = PARM_READ_CHECK;
4392 if (vport->cfg_first_burst_size &&
4393 (pnode->nlp_flag & NLP_FIRSTBURST)) {
4394 fcpdl = scsi_bufflen(scsi_cmnd);
4395 if (fcpdl < vport->cfg_first_burst_size)
4396 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4397 else
4398 piocbq->iocb.un.fcpi.fcpi_XRdy =
4399 vport->cfg_first_burst_size;
4401 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4402 phba->fc4OutputRequests++;
4403 } else {
4404 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4405 iocb_cmd->ulpPU = PARM_READ_CHECK;
4406 fcp_cmnd->fcpCntl3 = READ_DATA;
4407 phba->fc4InputRequests++;
4409 } else {
4410 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4411 iocb_cmd->un.fcpi.fcpi_parm = 0;
4412 iocb_cmd->ulpPU = 0;
4413 fcp_cmnd->fcpCntl3 = 0;
4414 phba->fc4ControlRequests++;
4416 if (phba->sli_rev == 3 &&
4417 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4418 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
4420 * Finish initializing those IOCB fields that are independent
4421 * of the scsi_cmnd request_buffer
4423 piocbq->iocb.ulpContext = pnode->nlp_rpi;
4424 if (sli4)
4425 piocbq->iocb.ulpContext =
4426 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
4427 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4428 piocbq->iocb.ulpFCP2Rcvy = 1;
4429 else
4430 piocbq->iocb.ulpFCP2Rcvy = 0;
4432 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4433 piocbq->context1 = lpfc_cmd;
4434 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4435 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
4436 piocbq->vport = vport;
4440 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
4441 * @vport: The virtual port for which this call is being executed.
4442 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4443 * @lun: Logical unit number.
4444 * @task_mgmt_cmd: SCSI task management command.
4446 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4447 * for device with SLI-3 interface spec.
4449 * Return codes:
4450 * 0 - Error
4451 * 1 - Success
4453 static int
4454 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
4455 struct lpfc_scsi_buf *lpfc_cmd,
4456 unsigned int lun,
4457 uint8_t task_mgmt_cmd)
4459 struct lpfc_iocbq *piocbq;
4460 IOCB_t *piocb;
4461 struct fcp_cmnd *fcp_cmnd;
4462 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4463 struct lpfc_nodelist *ndlp = rdata->pnode;
4465 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4466 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
4467 return 0;
4469 piocbq = &(lpfc_cmd->cur_iocbq);
4470 piocbq->vport = vport;
4472 piocb = &piocbq->iocb;
4474 fcp_cmnd = lpfc_cmd->fcp_cmnd;
4475 /* Clear out any old data in the FCP command area */
4476 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4477 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
4478 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
4479 if (vport->phba->sli_rev == 3 &&
4480 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4481 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
4482 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
4483 piocb->ulpContext = ndlp->nlp_rpi;
4484 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4485 piocb->ulpContext =
4486 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4488 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
4489 piocb->ulpFCP2Rcvy = 1;
4491 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4493 /* ulpTimeout is only one byte */
4494 if (lpfc_cmd->timeout > 0xff) {
4496 * Do not timeout the command at the firmware level.
4497 * The driver will provide the timeout mechanism.
4499 piocb->ulpTimeout = 0;
4500 } else
4501 piocb->ulpTimeout = lpfc_cmd->timeout;
4503 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4504 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
4506 return 1;
4510 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
4511 * @phba: The hba struct for which this call is being executed.
4512 * @dev_grp: The HBA PCI-Device group number.
4514 * This routine sets up the SCSI interface API function jump table in @phba
4515 * struct.
4516 * Returns: 0 - success, -ENODEV - failure.
4519 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4522 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4523 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
4525 switch (dev_grp) {
4526 case LPFC_PCI_DEV_LP:
4527 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4528 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
4529 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
4530 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
4531 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
4532 break;
4533 case LPFC_PCI_DEV_OC:
4534 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4535 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
4536 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
4537 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
4538 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
4539 break;
4540 default:
4541 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4542 "1418 Invalid HBA PCI-device group: 0x%x\n",
4543 dev_grp);
4544 return -ENODEV;
4545 break;
4547 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
4548 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4549 return 0;
4553 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
4554 * @phba: The Hba for which this call is being executed.
4555 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4556 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4558 * This routine is IOCB completion routine for device reset and target reset
4559 * routine. This routine release scsi buffer associated with lpfc_cmd.
4561 static void
4562 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4563 struct lpfc_iocbq *cmdiocbq,
4564 struct lpfc_iocbq *rspiocbq)
4566 struct lpfc_scsi_buf *lpfc_cmd =
4567 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4568 if (lpfc_cmd)
4569 lpfc_release_scsi_buf(phba, lpfc_cmd);
4570 return;
4574 * lpfc_info - Info entry point of scsi_host_template data structure
4575 * @host: The scsi host for which this call is being executed.
4577 * This routine provides module information about hba.
4579 * Reutrn code:
4580 * Pointer to char - Success.
4582 const char *
4583 lpfc_info(struct Scsi_Host *host)
4585 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4586 struct lpfc_hba *phba = vport->phba;
4587 int len, link_speed = 0;
4588 static char lpfcinfobuf[384];
4590 memset(lpfcinfobuf,0,384);
4591 if (phba && phba->pcidev){
4592 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4593 len = strlen(lpfcinfobuf);
4594 snprintf(lpfcinfobuf + len,
4595 384-len,
4596 " on PCI bus %02x device %02x irq %d",
4597 phba->pcidev->bus->number,
4598 phba->pcidev->devfn,
4599 phba->pcidev->irq);
4600 len = strlen(lpfcinfobuf);
4601 if (phba->Port[0]) {
4602 snprintf(lpfcinfobuf + len,
4603 384-len,
4604 " port %s",
4605 phba->Port);
4607 len = strlen(lpfcinfobuf);
4608 if (phba->sli_rev <= LPFC_SLI_REV3) {
4609 link_speed = lpfc_sli_port_speed_get(phba);
4610 } else {
4611 if (phba->sli4_hba.link_state.logical_speed)
4612 link_speed =
4613 phba->sli4_hba.link_state.logical_speed;
4614 else
4615 link_speed = phba->sli4_hba.link_state.speed;
4617 if (link_speed != 0)
4618 snprintf(lpfcinfobuf + len, 384-len,
4619 " Logical Link Speed: %d Mbps", link_speed);
4621 return lpfcinfobuf;
4625 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
4626 * @phba: The Hba for which this call is being executed.
4628 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4629 * The default value of cfg_poll_tmo is 10 milliseconds.
4631 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4633 unsigned long poll_tmo_expires =
4634 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4636 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
4637 mod_timer(&phba->fcp_poll_timer,
4638 poll_tmo_expires);
4642 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
4643 * @phba: The Hba for which this call is being executed.
4645 * This routine starts the fcp_poll_timer of @phba.
4647 void lpfc_poll_start_timer(struct lpfc_hba * phba)
4649 lpfc_poll_rearm_timer(phba);
4653 * lpfc_poll_timeout - Restart polling timer
4654 * @ptr: Map to lpfc_hba data structure pointer.
4656 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4657 * and FCP Ring interrupt is disable.
4660 void lpfc_poll_timeout(unsigned long ptr)
4662 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4664 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4665 lpfc_sli_handle_fast_ring_event(phba,
4666 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4668 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4669 lpfc_poll_rearm_timer(phba);
4674 * lpfc_queuecommand - scsi_host_template queuecommand entry point
4675 * @cmnd: Pointer to scsi_cmnd data structure.
4676 * @done: Pointer to done routine.
4678 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4679 * This routine prepares an IOCB from scsi command and provides to firmware.
4680 * The @done callback is invoked after driver finished processing the command.
4682 * Return value :
4683 * 0 - Success
4684 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4686 static int
4687 lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
4689 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4690 struct lpfc_hba *phba = vport->phba;
4691 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4692 struct lpfc_nodelist *ndlp;
4693 struct lpfc_scsi_buf *lpfc_cmd;
4694 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
4695 int err;
4697 err = fc_remote_port_chkready(rport);
4698 if (err) {
4699 cmnd->result = err;
4700 goto out_fail_command;
4702 ndlp = rdata->pnode;
4704 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
4705 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
4707 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4708 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4709 " op:%02x str=%s without registering for"
4710 " BlockGuard - Rejecting command\n",
4711 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4712 dif_op_str[scsi_get_prot_op(cmnd)]);
4713 goto out_fail_command;
4717 * Catch race where our node has transitioned, but the
4718 * transport is still transitioning.
4720 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4721 goto out_tgt_busy;
4722 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
4723 goto out_tgt_busy;
4725 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
4726 if (lpfc_cmd == NULL) {
4727 lpfc_rampdown_queue_depth(phba);
4729 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4730 "0707 driver's buffer pool is empty, "
4731 "IO busied\n");
4732 goto out_host_busy;
4736 * Store the midlayer's command structure for the completion phase
4737 * and complete the command initialization.
4739 lpfc_cmd->pCmd = cmnd;
4740 lpfc_cmd->rdata = rdata;
4741 lpfc_cmd->timeout = 0;
4742 lpfc_cmd->start_time = jiffies;
4743 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4745 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
4746 if (vport->phba->cfg_enable_bg) {
4747 lpfc_printf_vlog(vport,
4748 KERN_INFO, LOG_SCSI_CMD,
4749 "9033 BLKGRD: rcvd %s cmd:x%x "
4750 "sector x%llx cnt %u pt %x\n",
4751 dif_op_str[scsi_get_prot_op(cmnd)],
4752 cmnd->cmnd[0],
4753 (unsigned long long)scsi_get_lba(cmnd),
4754 blk_rq_sectors(cmnd->request),
4755 (cmnd->cmnd[1]>>5));
4757 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4758 } else {
4759 if (vport->phba->cfg_enable_bg) {
4760 lpfc_printf_vlog(vport,
4761 KERN_INFO, LOG_SCSI_CMD,
4762 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4763 "x%x sector x%llx cnt %u pt %x\n",
4764 cmnd->cmnd[0],
4765 (unsigned long long)scsi_get_lba(cmnd),
4766 blk_rq_sectors(cmnd->request),
4767 (cmnd->cmnd[1]>>5));
4769 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4772 if (err)
4773 goto out_host_busy_free_buf;
4775 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
4777 atomic_inc(&ndlp->cmd_pending);
4778 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4779 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
4780 if (err) {
4781 atomic_dec(&ndlp->cmd_pending);
4782 goto out_host_busy_free_buf;
4784 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4785 lpfc_sli_handle_fast_ring_event(phba,
4786 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4788 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4789 lpfc_poll_rearm_timer(phba);
4792 return 0;
4794 out_host_busy_free_buf:
4795 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4796 lpfc_release_scsi_buf(phba, lpfc_cmd);
4797 out_host_busy:
4798 return SCSI_MLQUEUE_HOST_BUSY;
4800 out_tgt_busy:
4801 return SCSI_MLQUEUE_TARGET_BUSY;
4803 out_fail_command:
4804 cmnd->scsi_done(cmnd);
4805 return 0;
4810 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
4811 * @cmnd: Pointer to scsi_cmnd data structure.
4813 * This routine aborts @cmnd pending in base driver.
4815 * Return code :
4816 * 0x2003 - Error
4817 * 0x2002 - Success
4819 static int
4820 lpfc_abort_handler(struct scsi_cmnd *cmnd)
4822 struct Scsi_Host *shost = cmnd->device->host;
4823 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4824 struct lpfc_hba *phba = vport->phba;
4825 struct lpfc_iocbq *iocb;
4826 struct lpfc_iocbq *abtsiocb;
4827 struct lpfc_scsi_buf *lpfc_cmd;
4828 IOCB_t *cmd, *icmd;
4829 int ret = SUCCESS, status = 0;
4830 unsigned long flags;
4831 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
4833 status = fc_block_scsi_eh(cmnd);
4834 if (status != 0 && status != SUCCESS)
4835 return status;
4837 spin_lock_irqsave(&phba->hbalock, flags);
4838 /* driver queued commands are in process of being flushed */
4839 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
4840 spin_unlock_irqrestore(&phba->hbalock, flags);
4841 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4842 "3168 SCSI Layer abort requested I/O has been "
4843 "flushed by LLD.\n");
4844 return FAILED;
4847 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
4848 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
4849 spin_unlock_irqrestore(&phba->hbalock, flags);
4850 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4851 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
4852 "x%x ID %d LUN %d\n",
4853 SUCCESS, cmnd->device->id, cmnd->device->lun);
4854 return SUCCESS;
4857 iocb = &lpfc_cmd->cur_iocbq;
4858 /* the command is in process of being cancelled */
4859 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4860 spin_unlock_irqrestore(&phba->hbalock, flags);
4861 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4862 "3169 SCSI Layer abort requested I/O has been "
4863 "cancelled by LLD.\n");
4864 return FAILED;
4867 * If pCmd field of the corresponding lpfc_scsi_buf structure
4868 * points to a different SCSI command, then the driver has
4869 * already completed this command, but the midlayer did not
4870 * see the completion before the eh fired. Just return SUCCESS.
4872 if (lpfc_cmd->pCmd != cmnd) {
4873 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4874 "3170 SCSI Layer abort requested I/O has been "
4875 "completed by LLD.\n");
4876 goto out_unlock;
4879 BUG_ON(iocb->context1 != lpfc_cmd);
4881 abtsiocb = __lpfc_sli_get_iocbq(phba);
4882 if (abtsiocb == NULL) {
4883 ret = FAILED;
4884 goto out_unlock;
4887 /* Indicate the IO is being aborted by the driver. */
4888 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4891 * The scsi command can not be in txq and it is in flight because the
4892 * pCmd is still pointig at the SCSI command we have to abort. There
4893 * is no need to search the txcmplq. Just send an abort to the FW.
4896 cmd = &iocb->iocb;
4897 icmd = &abtsiocb->iocb;
4898 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4899 icmd->un.acxri.abortContextTag = cmd->ulpContext;
4900 if (phba->sli_rev == LPFC_SLI_REV4)
4901 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4902 else
4903 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
4905 icmd->ulpLe = 1;
4906 icmd->ulpClass = cmd->ulpClass;
4908 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4909 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
4910 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
4912 if (lpfc_is_link_up(phba))
4913 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4914 else
4915 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
4917 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
4918 abtsiocb->vport = vport;
4919 /* no longer need the lock after this point */
4920 spin_unlock_irqrestore(&phba->hbalock, flags);
4922 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4923 IOCB_ERROR) {
4924 lpfc_sli_release_iocbq(phba, abtsiocb);
4925 ret = FAILED;
4926 goto out;
4929 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4930 lpfc_sli_handle_fast_ring_event(phba,
4931 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4933 lpfc_cmd->waitq = &waitq;
4934 /* Wait for abort to complete */
4935 wait_event_timeout(waitq,
4936 (lpfc_cmd->pCmd != cmnd),
4937 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
4938 lpfc_cmd->waitq = NULL;
4940 if (lpfc_cmd->pCmd == cmnd) {
4941 ret = FAILED;
4942 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4943 "0748 abort handler timed out waiting "
4944 "for abortng I/O (xri:x%x) to complete: "
4945 "ret %#x, ID %d, LUN %d\n",
4946 iocb->sli4_xritag, ret,
4947 cmnd->device->id, cmnd->device->lun);
4949 goto out;
4951 out_unlock:
4952 spin_unlock_irqrestore(&phba->hbalock, flags);
4953 out:
4954 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4955 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
4956 "LUN %d\n", ret, cmnd->device->id,
4957 cmnd->device->lun);
4958 return ret;
4961 static char *
4962 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4964 switch (task_mgmt_cmd) {
4965 case FCP_ABORT_TASK_SET:
4966 return "ABORT_TASK_SET";
4967 case FCP_CLEAR_TASK_SET:
4968 return "FCP_CLEAR_TASK_SET";
4969 case FCP_BUS_RESET:
4970 return "FCP_BUS_RESET";
4971 case FCP_LUN_RESET:
4972 return "FCP_LUN_RESET";
4973 case FCP_TARGET_RESET:
4974 return "FCP_TARGET_RESET";
4975 case FCP_CLEAR_ACA:
4976 return "FCP_CLEAR_ACA";
4977 case FCP_TERMINATE_TASK:
4978 return "FCP_TERMINATE_TASK";
4979 default:
4980 return "unknown";
4985 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
4986 * @vport: The virtual port for which this call is being executed.
4987 * @rdata: Pointer to remote port local data
4988 * @tgt_id: Target ID of remote device.
4989 * @lun_id: Lun number for the TMF
4990 * @task_mgmt_cmd: type of TMF to send
4992 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
4993 * a remote port.
4995 * Return Code:
4996 * 0x2003 - Error
4997 * 0x2002 - Success.
4999 static int
5000 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
5001 unsigned tgt_id, unsigned int lun_id,
5002 uint8_t task_mgmt_cmd)
5004 struct lpfc_hba *phba = vport->phba;
5005 struct lpfc_scsi_buf *lpfc_cmd;
5006 struct lpfc_iocbq *iocbq;
5007 struct lpfc_iocbq *iocbqrsp;
5008 struct lpfc_nodelist *pnode = rdata->pnode;
5009 int ret;
5010 int status;
5012 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5013 return FAILED;
5015 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
5016 if (lpfc_cmd == NULL)
5017 return FAILED;
5018 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
5019 lpfc_cmd->rdata = rdata;
5021 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5022 task_mgmt_cmd);
5023 if (!status) {
5024 lpfc_release_scsi_buf(phba, lpfc_cmd);
5025 return FAILED;
5028 iocbq = &lpfc_cmd->cur_iocbq;
5029 iocbqrsp = lpfc_sli_get_iocbq(phba);
5030 if (iocbqrsp == NULL) {
5031 lpfc_release_scsi_buf(phba, lpfc_cmd);
5032 return FAILED;
5034 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
5036 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5037 "0702 Issue %s to TGT %d LUN %d "
5038 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
5039 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
5040 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5041 iocbq->iocb_flag);
5043 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5044 iocbq, iocbqrsp, lpfc_cmd->timeout);
5045 if (status != IOCB_SUCCESS) {
5046 if (status == IOCB_TIMEDOUT) {
5047 ret = TIMEOUT_ERROR;
5048 } else
5049 ret = FAILED;
5050 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5051 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5052 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5053 "iocb_flag x%x\n",
5054 lpfc_taskmgmt_name(task_mgmt_cmd),
5055 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
5056 iocbqrsp->iocb.un.ulpWord[4],
5057 iocbq->iocb_flag);
5058 } else if (status == IOCB_BUSY)
5059 ret = FAILED;
5060 else
5061 ret = SUCCESS;
5063 lpfc_sli_release_iocbq(phba, iocbqrsp);
5065 if (ret != TIMEOUT_ERROR)
5066 lpfc_release_scsi_buf(phba, lpfc_cmd);
5068 return ret;
5072 * lpfc_chk_tgt_mapped -
5073 * @vport: The virtual port to check on
5074 * @cmnd: Pointer to scsi_cmnd data structure.
5076 * This routine delays until the scsi target (aka rport) for the
5077 * command exists (is present and logged in) or we declare it non-existent.
5079 * Return code :
5080 * 0x2003 - Error
5081 * 0x2002 - Success
5083 static int
5084 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5086 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
5087 struct lpfc_nodelist *pnode;
5088 unsigned long later;
5090 if (!rdata) {
5091 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5092 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5093 return FAILED;
5095 pnode = rdata->pnode;
5097 * If target is not in a MAPPED state, delay until
5098 * target is rediscovered or devloss timeout expires.
5100 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5101 while (time_after(later, jiffies)) {
5102 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5103 return FAILED;
5104 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5105 return SUCCESS;
5106 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5107 rdata = cmnd->device->hostdata;
5108 if (!rdata)
5109 return FAILED;
5110 pnode = rdata->pnode;
5112 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5113 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5114 return FAILED;
5115 return SUCCESS;
5119 * lpfc_reset_flush_io_context -
5120 * @vport: The virtual port (scsi_host) for the flush context
5121 * @tgt_id: If aborting by Target contect - specifies the target id
5122 * @lun_id: If aborting by Lun context - specifies the lun id
5123 * @context: specifies the context level to flush at.
5125 * After a reset condition via TMF, we need to flush orphaned i/o
5126 * contexts from the adapter. This routine aborts any contexts
5127 * outstanding, then waits for their completions. The wait is
5128 * bounded by devloss_tmo though.
5130 * Return code :
5131 * 0x2003 - Error
5132 * 0x2002 - Success
5134 static int
5135 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5136 uint64_t lun_id, lpfc_ctx_cmd context)
5138 struct lpfc_hba *phba = vport->phba;
5139 unsigned long later;
5140 int cnt;
5142 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5143 if (cnt)
5144 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5145 tgt_id, lun_id, context);
5146 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5147 while (time_after(later, jiffies) && cnt) {
5148 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5149 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5151 if (cnt) {
5152 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5153 "0724 I/O flush failure for context %s : cnt x%x\n",
5154 ((context == LPFC_CTX_LUN) ? "LUN" :
5155 ((context == LPFC_CTX_TGT) ? "TGT" :
5156 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5157 cnt);
5158 return FAILED;
5160 return SUCCESS;
5164 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
5165 * @cmnd: Pointer to scsi_cmnd data structure.
5167 * This routine does a device reset by sending a LUN_RESET task management
5168 * command.
5170 * Return code :
5171 * 0x2003 - Error
5172 * 0x2002 - Success
5174 static int
5175 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
5177 struct Scsi_Host *shost = cmnd->device->host;
5178 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5179 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
5180 struct lpfc_nodelist *pnode;
5181 unsigned tgt_id = cmnd->device->id;
5182 unsigned int lun_id = cmnd->device->lun;
5183 struct lpfc_scsi_event_header scsi_event;
5184 int status, ret = SUCCESS;
5186 if (!rdata) {
5187 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5188 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5189 return FAILED;
5191 pnode = rdata->pnode;
5192 status = fc_block_scsi_eh(cmnd);
5193 if (status != 0 && status != SUCCESS)
5194 return status;
5196 status = lpfc_chk_tgt_mapped(vport, cmnd);
5197 if (status == FAILED) {
5198 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5199 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5200 return FAILED;
5203 scsi_event.event_type = FC_REG_SCSI_EVENT;
5204 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5205 scsi_event.lun = lun_id;
5206 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5207 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5209 fc_host_post_vendor_event(shost, fc_get_event_number(),
5210 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5212 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5213 FCP_LUN_RESET);
5215 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5216 "0713 SCSI layer issued Device Reset (%d, %d) "
5217 "return x%x\n", tgt_id, lun_id, status);
5220 * We have to clean up i/o as : they may be orphaned by the TMF;
5221 * or if the TMF failed, they may be in an indeterminate state.
5222 * So, continue on.
5223 * We will report success if all the i/o aborts successfully.
5225 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5226 LPFC_CTX_LUN);
5227 return ret;
5231 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5232 * @cmnd: Pointer to scsi_cmnd data structure.
5234 * This routine does a target reset by sending a TARGET_RESET task management
5235 * command.
5237 * Return code :
5238 * 0x2003 - Error
5239 * 0x2002 - Success
5241 static int
5242 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5244 struct Scsi_Host *shost = cmnd->device->host;
5245 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5246 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
5247 struct lpfc_nodelist *pnode;
5248 unsigned tgt_id = cmnd->device->id;
5249 unsigned int lun_id = cmnd->device->lun;
5250 struct lpfc_scsi_event_header scsi_event;
5251 int status, ret = SUCCESS;
5253 if (!rdata) {
5254 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5255 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5256 return FAILED;
5258 pnode = rdata->pnode;
5259 status = fc_block_scsi_eh(cmnd);
5260 if (status != 0 && status != SUCCESS)
5261 return status;
5263 status = lpfc_chk_tgt_mapped(vport, cmnd);
5264 if (status == FAILED) {
5265 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5266 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5267 return FAILED;
5270 scsi_event.event_type = FC_REG_SCSI_EVENT;
5271 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5272 scsi_event.lun = 0;
5273 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5274 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5276 fc_host_post_vendor_event(shost, fc_get_event_number(),
5277 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5279 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5280 FCP_TARGET_RESET);
5282 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5283 "0723 SCSI layer issued Target Reset (%d, %d) "
5284 "return x%x\n", tgt_id, lun_id, status);
5287 * We have to clean up i/o as : they may be orphaned by the TMF;
5288 * or if the TMF failed, they may be in an indeterminate state.
5289 * So, continue on.
5290 * We will report success if all the i/o aborts successfully.
5292 ret = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5293 LPFC_CTX_TGT);
5294 return ret;
5298 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
5299 * @cmnd: Pointer to scsi_cmnd data structure.
5301 * This routine does target reset to all targets on @cmnd->device->host.
5302 * This emulates Parallel SCSI Bus Reset Semantics.
5304 * Return code :
5305 * 0x2003 - Error
5306 * 0x2002 - Success
5308 static int
5309 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
5311 struct Scsi_Host *shost = cmnd->device->host;
5312 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5313 struct lpfc_nodelist *ndlp = NULL;
5314 struct lpfc_scsi_event_header scsi_event;
5315 int match;
5316 int ret = SUCCESS, status, i;
5318 scsi_event.event_type = FC_REG_SCSI_EVENT;
5319 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5320 scsi_event.lun = 0;
5321 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5322 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5324 fc_host_post_vendor_event(shost, fc_get_event_number(),
5325 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5327 status = fc_block_scsi_eh(cmnd);
5328 if (status != 0 && status != SUCCESS)
5329 return status;
5332 * Since the driver manages a single bus device, reset all
5333 * targets known to the driver. Should any target reset
5334 * fail, this routine returns failure to the midlayer.
5336 for (i = 0; i < LPFC_MAX_TARGET; i++) {
5337 /* Search for mapped node by target ID */
5338 match = 0;
5339 spin_lock_irq(shost->host_lock);
5340 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5341 if (!NLP_CHK_NODE_ACT(ndlp))
5342 continue;
5343 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5344 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5345 continue;
5346 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
5347 ndlp->nlp_sid == i &&
5348 ndlp->rport) {
5349 match = 1;
5350 break;
5353 spin_unlock_irq(shost->host_lock);
5354 if (!match)
5355 continue;
5357 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5358 i, 0, FCP_TARGET_RESET);
5360 if (status != SUCCESS) {
5361 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5362 "0700 Bus Reset on target %d failed\n",
5364 ret = FAILED;
5368 * We have to clean up i/o as : they may be orphaned by the TMFs
5369 * above; or if any of the TMFs failed, they may be in an
5370 * indeterminate state.
5371 * We will report success if all the i/o aborts successfully.
5374 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5375 if (status != SUCCESS)
5376 ret = FAILED;
5378 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5379 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
5380 return ret;
5384 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5385 * @cmnd: Pointer to scsi_cmnd data structure.
5387 * This routine does host reset to the adaptor port. It brings the HBA
5388 * offline, performs a board restart, and then brings the board back online.
5389 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5390 * reject all outstanding SCSI commands to the host and error returned
5391 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5392 * of error handling, it will only return error if resetting of the adapter
5393 * is not successful; in all other cases, will return success.
5395 * Return code :
5396 * 0x2003 - Error
5397 * 0x2002 - Success
5399 static int
5400 lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5402 struct Scsi_Host *shost = cmnd->device->host;
5403 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5404 struct lpfc_hba *phba = vport->phba;
5405 int rc, ret = SUCCESS;
5407 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5408 "3172 SCSI layer issued Host Reset Data:\n");
5410 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5411 lpfc_offline(phba);
5412 rc = lpfc_sli_brdrestart(phba);
5413 if (rc)
5414 ret = FAILED;
5415 rc = lpfc_online(phba);
5416 if (rc)
5417 ret = FAILED;
5418 lpfc_unblock_mgmt_io(phba);
5420 if (ret == FAILED) {
5421 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5422 "3323 Failed host reset, bring it offline\n");
5423 lpfc_sli4_offline_eratt(phba);
5425 return ret;
5429 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
5430 * @sdev: Pointer to scsi_device.
5432 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5433 * globally available list of scsi buffers. This routine also makes sure scsi
5434 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5435 * of scsi buffer exists for the lifetime of the driver.
5437 * Return codes:
5438 * non-0 - Error
5439 * 0 - Success
5441 static int
5442 lpfc_slave_alloc(struct scsi_device *sdev)
5444 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5445 struct lpfc_hba *phba = vport->phba;
5446 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
5447 uint32_t total = 0;
5448 uint32_t num_to_alloc = 0;
5449 int num_allocated = 0;
5450 uint32_t sdev_cnt;
5452 if (!rport || fc_remote_port_chkready(rport))
5453 return -ENXIO;
5455 sdev->hostdata = rport->dd_data;
5456 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
5459 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5460 * available list of scsi buffers. Don't allocate more than the
5461 * HBA limit conveyed to the midlayer via the host structure. The
5462 * formula accounts for the lun_queue_depth + error handlers + 1
5463 * extra. This list of scsi bufs exists for the lifetime of the driver.
5465 total = phba->total_scsi_bufs;
5466 num_to_alloc = vport->cfg_lun_queue_depth + 2;
5468 /* If allocated buffers are enough do nothing */
5469 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5470 return 0;
5472 /* Allow some exchanges to be available always to complete discovery */
5473 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5474 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5475 "0704 At limitation of %d preallocated "
5476 "command buffers\n", total);
5477 return 0;
5478 /* Allow some exchanges to be available always to complete discovery */
5479 } else if (total + num_to_alloc >
5480 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5481 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5482 "0705 Allocation request of %d "
5483 "command buffers will exceed max of %d. "
5484 "Reducing allocation request to %d.\n",
5485 num_to_alloc, phba->cfg_hba_queue_depth,
5486 (phba->cfg_hba_queue_depth - total));
5487 num_to_alloc = phba->cfg_hba_queue_depth - total;
5489 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5490 if (num_to_alloc != num_allocated) {
5491 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5492 "0708 Allocation request of %d "
5493 "command buffers did not succeed. "
5494 "Allocated %d buffers.\n",
5495 num_to_alloc, num_allocated);
5497 if (num_allocated > 0)
5498 phba->total_scsi_bufs += num_allocated;
5499 return 0;
5503 * lpfc_slave_configure - scsi_host_template slave_configure entry point
5504 * @sdev: Pointer to scsi_device.
5506 * This routine configures following items
5507 * - Tag command queuing support for @sdev if supported.
5508 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5510 * Return codes:
5511 * 0 - Success
5513 static int
5514 lpfc_slave_configure(struct scsi_device *sdev)
5516 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5517 struct lpfc_hba *phba = vport->phba;
5519 if (sdev->tagged_supported)
5520 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
5521 else
5522 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
5524 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5525 lpfc_sli_handle_fast_ring_event(phba,
5526 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
5527 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5528 lpfc_poll_rearm_timer(phba);
5531 return 0;
5535 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
5536 * @sdev: Pointer to scsi_device.
5538 * This routine sets @sdev hostatdata filed to null.
5540 static void
5541 lpfc_slave_destroy(struct scsi_device *sdev)
5543 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5544 struct lpfc_hba *phba = vport->phba;
5545 atomic_dec(&phba->sdev_cnt);
5546 sdev->hostdata = NULL;
5547 return;
5551 struct scsi_host_template lpfc_template = {
5552 .module = THIS_MODULE,
5553 .name = LPFC_DRIVER_NAME,
5554 .info = lpfc_info,
5555 .queuecommand = lpfc_queuecommand,
5556 .eh_abort_handler = lpfc_abort_handler,
5557 .eh_device_reset_handler = lpfc_device_reset_handler,
5558 .eh_target_reset_handler = lpfc_target_reset_handler,
5559 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5560 .eh_host_reset_handler = lpfc_host_reset_handler,
5561 .slave_alloc = lpfc_slave_alloc,
5562 .slave_configure = lpfc_slave_configure,
5563 .slave_destroy = lpfc_slave_destroy,
5564 .scan_finished = lpfc_scan_finished,
5565 .this_id = -1,
5566 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5567 .cmd_per_lun = LPFC_CMD_PER_LUN,
5568 .use_clustering = ENABLE_CLUSTERING,
5569 .shost_attrs = lpfc_hba_attrs,
5570 .max_sectors = 0xFFFF,
5571 .vendor_id = LPFC_NL_VENDOR_ID,
5572 .change_queue_depth = lpfc_change_queue_depth,
5573 .change_queue_type = lpfc_change_queue_type,
5576 struct scsi_host_template lpfc_vport_template = {
5577 .module = THIS_MODULE,
5578 .name = LPFC_DRIVER_NAME,
5579 .info = lpfc_info,
5580 .queuecommand = lpfc_queuecommand,
5581 .eh_abort_handler = lpfc_abort_handler,
5582 .eh_device_reset_handler = lpfc_device_reset_handler,
5583 .eh_target_reset_handler = lpfc_target_reset_handler,
5584 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5585 .slave_alloc = lpfc_slave_alloc,
5586 .slave_configure = lpfc_slave_configure,
5587 .slave_destroy = lpfc_slave_destroy,
5588 .scan_finished = lpfc_scan_finished,
5589 .this_id = -1,
5590 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5591 .cmd_per_lun = LPFC_CMD_PER_LUN,
5592 .use_clustering = ENABLE_CLUSTERING,
5593 .shost_attrs = lpfc_vport_attrs,
5594 .max_sectors = 0xFFFF,
5595 .change_queue_depth = lpfc_change_queue_depth,
5596 .change_queue_type = lpfc_change_queue_type,