PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / scsi / lpfc / lpfc_scsi.c
blobb2ede05a5f0a61bbdeb3ffea4d3d7c263262031d
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;
1016 lxri = lpfc_sli4_next_xritag(phba);
1017 if (lxri == NO_XRI) {
1018 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1019 psb->data, psb->dma_handle);
1020 kfree(psb);
1021 break;
1024 /* Allocate iotag for psb->cur_iocbq. */
1025 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
1026 if (iotag == 0) {
1027 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
1028 psb->data, psb->dma_handle);
1029 kfree(psb);
1030 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1031 "3368 Failed to allocated IOTAG for"
1032 " XRI:0x%x\n", lxri);
1033 lpfc_sli4_free_xri(phba, lxri);
1034 break;
1036 psb->cur_iocbq.sli4_lxritag = lxri;
1037 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
1038 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
1039 psb->fcp_bpl = psb->data;
1040 psb->fcp_cmnd = (psb->data + sgl_size);
1041 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
1042 sizeof(struct fcp_cmnd));
1044 /* Initialize local short-hand pointers. */
1045 sgl = (struct sli4_sge *)psb->fcp_bpl;
1046 pdma_phys_bpl = psb->dma_handle;
1047 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
1048 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
1051 * The first two bdes are the FCP_CMD and FCP_RSP.
1052 * The balance are sg list bdes. Initialize the
1053 * first two and leave the rest for queuecommand.
1055 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
1056 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
1057 sgl->word2 = le32_to_cpu(sgl->word2);
1058 bf_set(lpfc_sli4_sge_last, sgl, 0);
1059 sgl->word2 = cpu_to_le32(sgl->word2);
1060 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
1061 sgl++;
1063 /* Setup the physical region for the FCP RSP */
1064 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
1065 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
1066 sgl->word2 = le32_to_cpu(sgl->word2);
1067 bf_set(lpfc_sli4_sge_last, sgl, 1);
1068 sgl->word2 = cpu_to_le32(sgl->word2);
1069 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
1072 * Since the IOCB for the FCP I/O is built into this
1073 * lpfc_scsi_buf, initialize it with all known data now.
1075 iocb = &psb->cur_iocbq.iocb;
1076 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
1077 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
1078 /* setting the BLP size to 2 * sizeof BDE may not be correct.
1079 * We are setting the bpl to point to out sgl. An sgl's
1080 * entries are 16 bytes, a bpl entries are 12 bytes.
1082 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
1083 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
1084 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
1085 iocb->ulpBdeCount = 1;
1086 iocb->ulpLe = 1;
1087 iocb->ulpClass = CLASS3;
1088 psb->cur_iocbq.context1 = psb;
1089 psb->dma_phys_bpl = pdma_phys_bpl;
1091 /* add the scsi buffer to a post list */
1092 list_add_tail(&psb->list, &post_sblist);
1093 spin_lock_irq(&phba->scsi_buf_list_get_lock);
1094 phba->sli4_hba.scsi_xri_cnt++;
1095 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
1097 lpfc_printf_log(phba, KERN_INFO, LOG_BG,
1098 "3021 Allocate %d out of %d requested new SCSI "
1099 "buffers\n", bcnt, num_to_alloc);
1101 /* post the list of scsi buffer sgls to port if available */
1102 if (!list_empty(&post_sblist))
1103 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
1104 &post_sblist, bcnt);
1105 else
1106 num_posted = 0;
1108 return num_posted;
1112 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
1113 * @vport: The virtual port for which this call being executed.
1114 * @num_to_allocate: The requested number of buffers to allocate.
1116 * This routine wraps the actual SCSI buffer allocator function pointer from
1117 * the lpfc_hba struct.
1119 * Return codes:
1120 * int - number of scsi buffers that were allocated.
1121 * 0 = failure, less than num_to_alloc is a partial failure.
1123 static inline int
1124 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
1126 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
1130 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1131 * @phba: The HBA for which this call is being executed.
1133 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1134 * and returns to caller.
1136 * Return codes:
1137 * NULL - Error
1138 * Pointer to lpfc_scsi_buf - Success
1140 static struct lpfc_scsi_buf*
1141 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1143 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1144 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
1145 unsigned long iflag = 0;
1147 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
1148 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
1149 list);
1150 if (!lpfc_cmd) {
1151 spin_lock(&phba->scsi_buf_list_put_lock);
1152 list_splice(&phba->lpfc_scsi_buf_list_put,
1153 &phba->lpfc_scsi_buf_list_get);
1154 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1155 list_remove_head(scsi_buf_list_get, lpfc_cmd,
1156 struct lpfc_scsi_buf, list);
1157 spin_unlock(&phba->scsi_buf_list_put_lock);
1159 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
1160 return lpfc_cmd;
1163 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1164 * @phba: The HBA for which this call is being executed.
1166 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1167 * and returns to caller.
1169 * Return codes:
1170 * NULL - Error
1171 * Pointer to lpfc_scsi_buf - Success
1173 static struct lpfc_scsi_buf*
1174 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1176 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
1177 unsigned long iflag = 0;
1178 int found = 0;
1180 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
1181 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1182 &phba->lpfc_scsi_buf_list_get, list) {
1183 if (lpfc_test_rrq_active(phba, ndlp,
1184 lpfc_cmd->cur_iocbq.sli4_lxritag))
1185 continue;
1186 list_del(&lpfc_cmd->list);
1187 found = 1;
1188 break;
1190 if (!found) {
1191 spin_lock(&phba->scsi_buf_list_put_lock);
1192 list_splice(&phba->lpfc_scsi_buf_list_put,
1193 &phba->lpfc_scsi_buf_list_get);
1194 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1195 spin_unlock(&phba->scsi_buf_list_put_lock);
1196 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1197 &phba->lpfc_scsi_buf_list_get, list) {
1198 if (lpfc_test_rrq_active(
1199 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1200 continue;
1201 list_del(&lpfc_cmd->list);
1202 found = 1;
1203 break;
1206 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
1207 if (!found)
1208 return NULL;
1209 return lpfc_cmd;
1212 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1213 * @phba: The HBA for which this call is being executed.
1215 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1216 * and returns to caller.
1218 * Return codes:
1219 * NULL - Error
1220 * Pointer to lpfc_scsi_buf - Success
1222 static struct lpfc_scsi_buf*
1223 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1225 return phba->lpfc_get_scsi_buf(phba, ndlp);
1229 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
1230 * @phba: The Hba for which this call is being executed.
1231 * @psb: The scsi buffer which is being released.
1233 * This routine releases @psb scsi buffer by adding it to tail of @phba
1234 * lpfc_scsi_buf_list list.
1236 static void
1237 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1239 unsigned long iflag = 0;
1241 psb->seg_cnt = 0;
1242 psb->nonsg_phys = 0;
1243 psb->prot_seg_cnt = 0;
1245 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1246 psb->pCmd = NULL;
1247 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
1248 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1249 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1253 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1254 * @phba: The Hba for which this call is being executed.
1255 * @psb: The scsi buffer which is being released.
1257 * This routine releases @psb scsi buffer by adding it to tail of @phba
1258 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1259 * and cannot be reused for at least RA_TOV amount of time if it was
1260 * aborted.
1262 static void
1263 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1265 unsigned long iflag = 0;
1267 psb->seg_cnt = 0;
1268 psb->nonsg_phys = 0;
1269 psb->prot_seg_cnt = 0;
1271 if (psb->exch_busy) {
1272 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1273 iflag);
1274 psb->pCmd = NULL;
1275 list_add_tail(&psb->list,
1276 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1277 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1278 iflag);
1279 } else {
1280 psb->pCmd = NULL;
1281 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
1282 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1283 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1284 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1289 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1290 * @phba: The Hba for which this call is being executed.
1291 * @psb: The scsi buffer which is being released.
1293 * This routine releases @psb scsi buffer by adding it to tail of @phba
1294 * lpfc_scsi_buf_list list.
1296 static void
1297 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1300 phba->lpfc_release_scsi_buf(phba, psb);
1304 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
1305 * @phba: The Hba for which this call is being executed.
1306 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1308 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1309 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1310 * through sg elements and format the bdea. This routine also initializes all
1311 * IOCB fields which are dependent on scsi command request buffer.
1313 * Return codes:
1314 * 1 - Error
1315 * 0 - Success
1317 static int
1318 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1320 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1321 struct scatterlist *sgel = NULL;
1322 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1323 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1324 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
1325 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1326 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1327 dma_addr_t physaddr;
1328 uint32_t num_bde = 0;
1329 int nseg, datadir = scsi_cmnd->sc_data_direction;
1332 * There are three possibilities here - use scatter-gather segment, use
1333 * the single mapping, or neither. Start the lpfc command prep by
1334 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1335 * data bde entry.
1337 bpl += 2;
1338 if (scsi_sg_count(scsi_cmnd)) {
1340 * The driver stores the segment count returned from pci_map_sg
1341 * because this a count of dma-mappings used to map the use_sg
1342 * pages. They are not guaranteed to be the same for those
1343 * architectures that implement an IOMMU.
1346 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1347 scsi_sg_count(scsi_cmnd), datadir);
1348 if (unlikely(!nseg))
1349 return 1;
1351 lpfc_cmd->seg_cnt = nseg;
1352 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1353 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1354 "9064 BLKGRD: %s: Too many sg segments from "
1355 "dma_map_sg. Config %d, seg_cnt %d\n",
1356 __func__, phba->cfg_sg_seg_cnt,
1357 lpfc_cmd->seg_cnt);
1358 lpfc_cmd->seg_cnt = 0;
1359 scsi_dma_unmap(scsi_cmnd);
1360 return 1;
1364 * The driver established a maximum scatter-gather segment count
1365 * during probe that limits the number of sg elements in any
1366 * single scsi command. Just run through the seg_cnt and format
1367 * the bde's.
1368 * When using SLI-3 the driver will try to fit all the BDEs into
1369 * the IOCB. If it can't then the BDEs get added to a BPL as it
1370 * does for SLI-2 mode.
1372 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1373 physaddr = sg_dma_address(sgel);
1374 if (phba->sli_rev == 3 &&
1375 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1376 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
1377 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1378 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1379 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1380 data_bde->addrLow = putPaddrLow(physaddr);
1381 data_bde->addrHigh = putPaddrHigh(physaddr);
1382 data_bde++;
1383 } else {
1384 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1385 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1386 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1387 bpl->addrLow =
1388 le32_to_cpu(putPaddrLow(physaddr));
1389 bpl->addrHigh =
1390 le32_to_cpu(putPaddrHigh(physaddr));
1391 bpl++;
1397 * Finish initializing those IOCB fields that are dependent on the
1398 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1399 * explicitly reinitialized and for SLI-3 the extended bde count is
1400 * explicitly reinitialized since all iocb memory resources are reused.
1402 if (phba->sli_rev == 3 &&
1403 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1404 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
1405 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1407 * The extended IOCB format can only fit 3 BDE or a BPL.
1408 * This I/O has more than 3 BDE so the 1st data bde will
1409 * be a BPL that is filled in here.
1411 physaddr = lpfc_cmd->dma_handle;
1412 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1413 data_bde->tus.f.bdeSize = (num_bde *
1414 sizeof(struct ulp_bde64));
1415 physaddr += (sizeof(struct fcp_cmnd) +
1416 sizeof(struct fcp_rsp) +
1417 (2 * sizeof(struct ulp_bde64)));
1418 data_bde->addrHigh = putPaddrHigh(physaddr);
1419 data_bde->addrLow = putPaddrLow(physaddr);
1420 /* ebde count includes the response bde and data bpl */
1421 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1422 } else {
1423 /* ebde count includes the response bde and data bdes */
1424 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1426 } else {
1427 iocb_cmd->un.fcpi64.bdl.bdeSize =
1428 ((num_bde + 2) * sizeof(struct ulp_bde64));
1429 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1431 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1434 * Due to difference in data length between DIF/non-DIF paths,
1435 * we need to set word 4 of IOCB here
1437 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1438 return 0;
1441 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1443 /* Return if if error injection is detected by Initiator */
1444 #define BG_ERR_INIT 0x1
1445 /* Return if if error injection is detected by Target */
1446 #define BG_ERR_TGT 0x2
1447 /* Return if if swapping CSUM<-->CRC is required for error injection */
1448 #define BG_ERR_SWAP 0x10
1449 /* Return if disabling Guard/Ref/App checking is required for error injection */
1450 #define BG_ERR_CHECK 0x20
1453 * lpfc_bg_err_inject - Determine if we should inject an error
1454 * @phba: The Hba for which this call is being executed.
1455 * @sc: The SCSI command to examine
1456 * @reftag: (out) BlockGuard reference tag for transmitted data
1457 * @apptag: (out) BlockGuard application tag for transmitted data
1458 * @new_guard (in) Value to replace CRC with if needed
1460 * Returns BG_ERR_* bit mask or 0 if request ignored
1462 static int
1463 lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1464 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1466 struct scatterlist *sgpe; /* s/g prot entry */
1467 struct scatterlist *sgde; /* s/g data entry */
1468 struct lpfc_scsi_buf *lpfc_cmd = NULL;
1469 struct scsi_dif_tuple *src = NULL;
1470 struct lpfc_nodelist *ndlp;
1471 struct lpfc_rport_data *rdata;
1472 uint32_t op = scsi_get_prot_op(sc);
1473 uint32_t blksize;
1474 uint32_t numblks;
1475 sector_t lba;
1476 int rc = 0;
1477 int blockoff = 0;
1479 if (op == SCSI_PROT_NORMAL)
1480 return 0;
1482 sgpe = scsi_prot_sglist(sc);
1483 sgde = scsi_sglist(sc);
1484 lba = scsi_get_lba(sc);
1486 /* First check if we need to match the LBA */
1487 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1488 blksize = lpfc_cmd_blksize(sc);
1489 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1491 /* Make sure we have the right LBA if one is specified */
1492 if ((phba->lpfc_injerr_lba < lba) ||
1493 (phba->lpfc_injerr_lba >= (lba + numblks)))
1494 return 0;
1495 if (sgpe) {
1496 blockoff = phba->lpfc_injerr_lba - lba;
1497 numblks = sg_dma_len(sgpe) /
1498 sizeof(struct scsi_dif_tuple);
1499 if (numblks < blockoff)
1500 blockoff = numblks;
1504 /* Next check if we need to match the remote NPortID or WWPN */
1505 rdata = sc->device->hostdata;
1506 if (rdata && rdata->pnode) {
1507 ndlp = rdata->pnode;
1509 /* Make sure we have the right NPortID if one is specified */
1510 if (phba->lpfc_injerr_nportid &&
1511 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1512 return 0;
1515 * Make sure we have the right WWPN if one is specified.
1516 * wwn[0] should be a non-zero NAA in a good WWPN.
1518 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1519 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1520 sizeof(struct lpfc_name)) != 0))
1521 return 0;
1524 /* Setup a ptr to the protection data if the SCSI host provides it */
1525 if (sgpe) {
1526 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1527 src += blockoff;
1528 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1531 /* Should we change the Reference Tag */
1532 if (reftag) {
1533 if (phba->lpfc_injerr_wref_cnt) {
1534 switch (op) {
1535 case SCSI_PROT_WRITE_PASS:
1536 if (src) {
1538 * For WRITE_PASS, force the error
1539 * to be sent on the wire. It should
1540 * be detected by the Target.
1541 * If blockoff != 0 error will be
1542 * inserted in middle of the IO.
1545 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1546 "9076 BLKGRD: Injecting reftag error: "
1547 "write lba x%lx + x%x oldrefTag x%x\n",
1548 (unsigned long)lba, blockoff,
1549 be32_to_cpu(src->ref_tag));
1552 * Save the old ref_tag so we can
1553 * restore it on completion.
1555 if (lpfc_cmd) {
1556 lpfc_cmd->prot_data_type =
1557 LPFC_INJERR_REFTAG;
1558 lpfc_cmd->prot_data_segment =
1559 src;
1560 lpfc_cmd->prot_data =
1561 src->ref_tag;
1563 src->ref_tag = cpu_to_be32(0xDEADBEEF);
1564 phba->lpfc_injerr_wref_cnt--;
1565 if (phba->lpfc_injerr_wref_cnt == 0) {
1566 phba->lpfc_injerr_nportid = 0;
1567 phba->lpfc_injerr_lba =
1568 LPFC_INJERR_LBA_OFF;
1569 memset(&phba->lpfc_injerr_wwpn,
1570 0, sizeof(struct lpfc_name));
1572 rc = BG_ERR_TGT | BG_ERR_CHECK;
1574 break;
1576 /* Drop thru */
1577 case SCSI_PROT_WRITE_INSERT:
1579 * For WRITE_INSERT, force the error
1580 * to be sent on the wire. It should be
1581 * detected by the Target.
1583 /* DEADBEEF will be the reftag on the wire */
1584 *reftag = 0xDEADBEEF;
1585 phba->lpfc_injerr_wref_cnt--;
1586 if (phba->lpfc_injerr_wref_cnt == 0) {
1587 phba->lpfc_injerr_nportid = 0;
1588 phba->lpfc_injerr_lba =
1589 LPFC_INJERR_LBA_OFF;
1590 memset(&phba->lpfc_injerr_wwpn,
1591 0, sizeof(struct lpfc_name));
1593 rc = BG_ERR_TGT | BG_ERR_CHECK;
1595 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1596 "9078 BLKGRD: Injecting reftag error: "
1597 "write lba x%lx\n", (unsigned long)lba);
1598 break;
1599 case SCSI_PROT_WRITE_STRIP:
1601 * For WRITE_STRIP and WRITE_PASS,
1602 * force the error on data
1603 * being copied from SLI-Host to SLI-Port.
1605 *reftag = 0xDEADBEEF;
1606 phba->lpfc_injerr_wref_cnt--;
1607 if (phba->lpfc_injerr_wref_cnt == 0) {
1608 phba->lpfc_injerr_nportid = 0;
1609 phba->lpfc_injerr_lba =
1610 LPFC_INJERR_LBA_OFF;
1611 memset(&phba->lpfc_injerr_wwpn,
1612 0, sizeof(struct lpfc_name));
1614 rc = BG_ERR_INIT;
1616 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1617 "9077 BLKGRD: Injecting reftag error: "
1618 "write lba x%lx\n", (unsigned long)lba);
1619 break;
1622 if (phba->lpfc_injerr_rref_cnt) {
1623 switch (op) {
1624 case SCSI_PROT_READ_INSERT:
1625 case SCSI_PROT_READ_STRIP:
1626 case SCSI_PROT_READ_PASS:
1628 * For READ_STRIP and READ_PASS, force the
1629 * error on data being read off the wire. It
1630 * should force an IO error to the driver.
1632 *reftag = 0xDEADBEEF;
1633 phba->lpfc_injerr_rref_cnt--;
1634 if (phba->lpfc_injerr_rref_cnt == 0) {
1635 phba->lpfc_injerr_nportid = 0;
1636 phba->lpfc_injerr_lba =
1637 LPFC_INJERR_LBA_OFF;
1638 memset(&phba->lpfc_injerr_wwpn,
1639 0, sizeof(struct lpfc_name));
1641 rc = BG_ERR_INIT;
1643 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1644 "9079 BLKGRD: Injecting reftag error: "
1645 "read lba x%lx\n", (unsigned long)lba);
1646 break;
1651 /* Should we change the Application Tag */
1652 if (apptag) {
1653 if (phba->lpfc_injerr_wapp_cnt) {
1654 switch (op) {
1655 case SCSI_PROT_WRITE_PASS:
1656 if (src) {
1658 * For WRITE_PASS, force the error
1659 * to be sent on the wire. It should
1660 * be detected by the Target.
1661 * If blockoff != 0 error will be
1662 * inserted in middle of the IO.
1665 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1666 "9080 BLKGRD: Injecting apptag error: "
1667 "write lba x%lx + x%x oldappTag x%x\n",
1668 (unsigned long)lba, blockoff,
1669 be16_to_cpu(src->app_tag));
1672 * Save the old app_tag so we can
1673 * restore it on completion.
1675 if (lpfc_cmd) {
1676 lpfc_cmd->prot_data_type =
1677 LPFC_INJERR_APPTAG;
1678 lpfc_cmd->prot_data_segment =
1679 src;
1680 lpfc_cmd->prot_data =
1681 src->app_tag;
1683 src->app_tag = cpu_to_be16(0xDEAD);
1684 phba->lpfc_injerr_wapp_cnt--;
1685 if (phba->lpfc_injerr_wapp_cnt == 0) {
1686 phba->lpfc_injerr_nportid = 0;
1687 phba->lpfc_injerr_lba =
1688 LPFC_INJERR_LBA_OFF;
1689 memset(&phba->lpfc_injerr_wwpn,
1690 0, sizeof(struct lpfc_name));
1692 rc = BG_ERR_TGT | BG_ERR_CHECK;
1693 break;
1695 /* Drop thru */
1696 case SCSI_PROT_WRITE_INSERT:
1698 * For WRITE_INSERT, force the
1699 * error to be sent on the wire. It should be
1700 * detected by the Target.
1702 /* DEAD will be the apptag on the wire */
1703 *apptag = 0xDEAD;
1704 phba->lpfc_injerr_wapp_cnt--;
1705 if (phba->lpfc_injerr_wapp_cnt == 0) {
1706 phba->lpfc_injerr_nportid = 0;
1707 phba->lpfc_injerr_lba =
1708 LPFC_INJERR_LBA_OFF;
1709 memset(&phba->lpfc_injerr_wwpn,
1710 0, sizeof(struct lpfc_name));
1712 rc = BG_ERR_TGT | BG_ERR_CHECK;
1714 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1715 "0813 BLKGRD: Injecting apptag error: "
1716 "write lba x%lx\n", (unsigned long)lba);
1717 break;
1718 case SCSI_PROT_WRITE_STRIP:
1720 * For WRITE_STRIP and WRITE_PASS,
1721 * force the error on data
1722 * being copied from SLI-Host to SLI-Port.
1724 *apptag = 0xDEAD;
1725 phba->lpfc_injerr_wapp_cnt--;
1726 if (phba->lpfc_injerr_wapp_cnt == 0) {
1727 phba->lpfc_injerr_nportid = 0;
1728 phba->lpfc_injerr_lba =
1729 LPFC_INJERR_LBA_OFF;
1730 memset(&phba->lpfc_injerr_wwpn,
1731 0, sizeof(struct lpfc_name));
1733 rc = BG_ERR_INIT;
1735 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1736 "0812 BLKGRD: Injecting apptag error: "
1737 "write lba x%lx\n", (unsigned long)lba);
1738 break;
1741 if (phba->lpfc_injerr_rapp_cnt) {
1742 switch (op) {
1743 case SCSI_PROT_READ_INSERT:
1744 case SCSI_PROT_READ_STRIP:
1745 case SCSI_PROT_READ_PASS:
1747 * For READ_STRIP and READ_PASS, force the
1748 * error on data being read off the wire. It
1749 * should force an IO error to the driver.
1751 *apptag = 0xDEAD;
1752 phba->lpfc_injerr_rapp_cnt--;
1753 if (phba->lpfc_injerr_rapp_cnt == 0) {
1754 phba->lpfc_injerr_nportid = 0;
1755 phba->lpfc_injerr_lba =
1756 LPFC_INJERR_LBA_OFF;
1757 memset(&phba->lpfc_injerr_wwpn,
1758 0, sizeof(struct lpfc_name));
1760 rc = BG_ERR_INIT;
1762 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1763 "0814 BLKGRD: Injecting apptag error: "
1764 "read lba x%lx\n", (unsigned long)lba);
1765 break;
1771 /* Should we change the Guard Tag */
1772 if (new_guard) {
1773 if (phba->lpfc_injerr_wgrd_cnt) {
1774 switch (op) {
1775 case SCSI_PROT_WRITE_PASS:
1776 rc = BG_ERR_CHECK;
1777 /* Drop thru */
1779 case SCSI_PROT_WRITE_INSERT:
1781 * For WRITE_INSERT, force the
1782 * error to be sent on the wire. It should be
1783 * detected by the Target.
1785 phba->lpfc_injerr_wgrd_cnt--;
1786 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1787 phba->lpfc_injerr_nportid = 0;
1788 phba->lpfc_injerr_lba =
1789 LPFC_INJERR_LBA_OFF;
1790 memset(&phba->lpfc_injerr_wwpn,
1791 0, sizeof(struct lpfc_name));
1794 rc |= BG_ERR_TGT | BG_ERR_SWAP;
1795 /* Signals the caller to swap CRC->CSUM */
1797 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1798 "0817 BLKGRD: Injecting guard error: "
1799 "write lba x%lx\n", (unsigned long)lba);
1800 break;
1801 case SCSI_PROT_WRITE_STRIP:
1803 * For WRITE_STRIP and WRITE_PASS,
1804 * force the error on data
1805 * being copied from SLI-Host to SLI-Port.
1807 phba->lpfc_injerr_wgrd_cnt--;
1808 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1809 phba->lpfc_injerr_nportid = 0;
1810 phba->lpfc_injerr_lba =
1811 LPFC_INJERR_LBA_OFF;
1812 memset(&phba->lpfc_injerr_wwpn,
1813 0, sizeof(struct lpfc_name));
1816 rc = BG_ERR_INIT | BG_ERR_SWAP;
1817 /* Signals the caller to swap CRC->CSUM */
1819 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1820 "0816 BLKGRD: Injecting guard error: "
1821 "write lba x%lx\n", (unsigned long)lba);
1822 break;
1825 if (phba->lpfc_injerr_rgrd_cnt) {
1826 switch (op) {
1827 case SCSI_PROT_READ_INSERT:
1828 case SCSI_PROT_READ_STRIP:
1829 case SCSI_PROT_READ_PASS:
1831 * For READ_STRIP and READ_PASS, force the
1832 * error on data being read off the wire. It
1833 * should force an IO error to the driver.
1835 phba->lpfc_injerr_rgrd_cnt--;
1836 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1837 phba->lpfc_injerr_nportid = 0;
1838 phba->lpfc_injerr_lba =
1839 LPFC_INJERR_LBA_OFF;
1840 memset(&phba->lpfc_injerr_wwpn,
1841 0, sizeof(struct lpfc_name));
1844 rc = BG_ERR_INIT | BG_ERR_SWAP;
1845 /* Signals the caller to swap CRC->CSUM */
1847 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1848 "0818 BLKGRD: Injecting guard error: "
1849 "read lba x%lx\n", (unsigned long)lba);
1854 return rc;
1856 #endif
1859 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1860 * the specified SCSI command.
1861 * @phba: The Hba for which this call is being executed.
1862 * @sc: The SCSI command to examine
1863 * @txopt: (out) BlockGuard operation for transmitted data
1864 * @rxopt: (out) BlockGuard operation for received data
1866 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1869 static int
1870 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1871 uint8_t *txop, uint8_t *rxop)
1873 uint8_t ret = 0;
1875 if (lpfc_cmd_guard_csum(sc)) {
1876 switch (scsi_get_prot_op(sc)) {
1877 case SCSI_PROT_READ_INSERT:
1878 case SCSI_PROT_WRITE_STRIP:
1879 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1880 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1881 break;
1883 case SCSI_PROT_READ_STRIP:
1884 case SCSI_PROT_WRITE_INSERT:
1885 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1886 *txop = BG_OP_IN_NODIF_OUT_CRC;
1887 break;
1889 case SCSI_PROT_READ_PASS:
1890 case SCSI_PROT_WRITE_PASS:
1891 *rxop = BG_OP_IN_CRC_OUT_CSUM;
1892 *txop = BG_OP_IN_CSUM_OUT_CRC;
1893 break;
1895 case SCSI_PROT_NORMAL:
1896 default:
1897 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1898 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1899 scsi_get_prot_op(sc));
1900 ret = 1;
1901 break;
1904 } else {
1905 switch (scsi_get_prot_op(sc)) {
1906 case SCSI_PROT_READ_STRIP:
1907 case SCSI_PROT_WRITE_INSERT:
1908 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1909 *txop = BG_OP_IN_NODIF_OUT_CRC;
1910 break;
1912 case SCSI_PROT_READ_PASS:
1913 case SCSI_PROT_WRITE_PASS:
1914 *rxop = BG_OP_IN_CRC_OUT_CRC;
1915 *txop = BG_OP_IN_CRC_OUT_CRC;
1916 break;
1918 case SCSI_PROT_READ_INSERT:
1919 case SCSI_PROT_WRITE_STRIP:
1920 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1921 *txop = BG_OP_IN_CRC_OUT_NODIF;
1922 break;
1924 case SCSI_PROT_NORMAL:
1925 default:
1926 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1927 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1928 scsi_get_prot_op(sc));
1929 ret = 1;
1930 break;
1934 return ret;
1937 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1939 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1940 * the specified SCSI command in order to force a guard tag error.
1941 * @phba: The Hba for which this call is being executed.
1942 * @sc: The SCSI command to examine
1943 * @txopt: (out) BlockGuard operation for transmitted data
1944 * @rxopt: (out) BlockGuard operation for received data
1946 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1949 static int
1950 lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1951 uint8_t *txop, uint8_t *rxop)
1953 uint8_t ret = 0;
1955 if (lpfc_cmd_guard_csum(sc)) {
1956 switch (scsi_get_prot_op(sc)) {
1957 case SCSI_PROT_READ_INSERT:
1958 case SCSI_PROT_WRITE_STRIP:
1959 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1960 *txop = BG_OP_IN_CRC_OUT_NODIF;
1961 break;
1963 case SCSI_PROT_READ_STRIP:
1964 case SCSI_PROT_WRITE_INSERT:
1965 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1966 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1967 break;
1969 case SCSI_PROT_READ_PASS:
1970 case SCSI_PROT_WRITE_PASS:
1971 *rxop = BG_OP_IN_CSUM_OUT_CRC;
1972 *txop = BG_OP_IN_CRC_OUT_CSUM;
1973 break;
1975 case SCSI_PROT_NORMAL:
1976 default:
1977 break;
1980 } else {
1981 switch (scsi_get_prot_op(sc)) {
1982 case SCSI_PROT_READ_STRIP:
1983 case SCSI_PROT_WRITE_INSERT:
1984 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1985 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1986 break;
1988 case SCSI_PROT_READ_PASS:
1989 case SCSI_PROT_WRITE_PASS:
1990 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
1991 *txop = BG_OP_IN_CSUM_OUT_CSUM;
1992 break;
1994 case SCSI_PROT_READ_INSERT:
1995 case SCSI_PROT_WRITE_STRIP:
1996 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1997 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1998 break;
2000 case SCSI_PROT_NORMAL:
2001 default:
2002 break;
2006 return ret;
2008 #endif
2011 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
2012 * @phba: The Hba for which this call is being executed.
2013 * @sc: pointer to scsi command we're working on
2014 * @bpl: pointer to buffer list for protection groups
2015 * @datacnt: number of segments of data that have been dma mapped
2017 * This function sets up BPL buffer list for protection groups of
2018 * type LPFC_PG_TYPE_NO_DIF
2020 * This is usually used when the HBA is instructed to generate
2021 * DIFs and insert them into data stream (or strip DIF from
2022 * incoming data stream)
2024 * The buffer list consists of just one protection group described
2025 * below:
2026 * +-------------------------+
2027 * start of prot group --> | PDE_5 |
2028 * +-------------------------+
2029 * | PDE_6 |
2030 * +-------------------------+
2031 * | Data BDE |
2032 * +-------------------------+
2033 * |more Data BDE's ... (opt)|
2034 * +-------------------------+
2037 * Note: Data s/g buffers have been dma mapped
2039 * Returns the number of BDEs added to the BPL.
2041 static int
2042 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2043 struct ulp_bde64 *bpl, int datasegcnt)
2045 struct scatterlist *sgde = NULL; /* s/g data entry */
2046 struct lpfc_pde5 *pde5 = NULL;
2047 struct lpfc_pde6 *pde6 = NULL;
2048 dma_addr_t physaddr;
2049 int i = 0, num_bde = 0, status;
2050 int datadir = sc->sc_data_direction;
2051 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2052 uint32_t rc;
2053 #endif
2054 uint32_t checking = 1;
2055 uint32_t reftag;
2056 unsigned blksize;
2057 uint8_t txop, rxop;
2059 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2060 if (status)
2061 goto out;
2063 /* extract some info from the scsi command for pde*/
2064 blksize = lpfc_cmd_blksize(sc);
2065 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2067 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2068 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2069 if (rc) {
2070 if (rc & BG_ERR_SWAP)
2071 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2072 if (rc & BG_ERR_CHECK)
2073 checking = 0;
2075 #endif
2077 /* setup PDE5 with what we have */
2078 pde5 = (struct lpfc_pde5 *) bpl;
2079 memset(pde5, 0, sizeof(struct lpfc_pde5));
2080 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
2082 /* Endianness conversion if necessary for PDE5 */
2083 pde5->word0 = cpu_to_le32(pde5->word0);
2084 pde5->reftag = cpu_to_le32(reftag);
2086 /* advance bpl and increment bde count */
2087 num_bde++;
2088 bpl++;
2089 pde6 = (struct lpfc_pde6 *) bpl;
2091 /* setup PDE6 with the rest of the info */
2092 memset(pde6, 0, sizeof(struct lpfc_pde6));
2093 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2094 bf_set(pde6_optx, pde6, txop);
2095 bf_set(pde6_oprx, pde6, rxop);
2098 * We only need to check the data on READs, for WRITEs
2099 * protection data is automatically generated, not checked.
2101 if (datadir == DMA_FROM_DEVICE) {
2102 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2103 bf_set(pde6_ce, pde6, checking);
2104 else
2105 bf_set(pde6_ce, pde6, 0);
2107 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2108 bf_set(pde6_re, pde6, checking);
2109 else
2110 bf_set(pde6_re, pde6, 0);
2112 bf_set(pde6_ai, pde6, 1);
2113 bf_set(pde6_ae, pde6, 0);
2114 bf_set(pde6_apptagval, pde6, 0);
2116 /* Endianness conversion if necessary for PDE6 */
2117 pde6->word0 = cpu_to_le32(pde6->word0);
2118 pde6->word1 = cpu_to_le32(pde6->word1);
2119 pde6->word2 = cpu_to_le32(pde6->word2);
2121 /* advance bpl and increment bde count */
2122 num_bde++;
2123 bpl++;
2125 /* assumption: caller has already run dma_map_sg on command data */
2126 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2127 physaddr = sg_dma_address(sgde);
2128 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
2129 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
2130 bpl->tus.f.bdeSize = sg_dma_len(sgde);
2131 if (datadir == DMA_TO_DEVICE)
2132 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2133 else
2134 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2135 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2136 bpl++;
2137 num_bde++;
2140 out:
2141 return num_bde;
2145 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
2146 * @phba: The Hba for which this call is being executed.
2147 * @sc: pointer to scsi command we're working on
2148 * @bpl: pointer to buffer list for protection groups
2149 * @datacnt: number of segments of data that have been dma mapped
2150 * @protcnt: number of segment of protection data that have been dma mapped
2152 * This function sets up BPL buffer list for protection groups of
2153 * type LPFC_PG_TYPE_DIF
2155 * This is usually used when DIFs are in their own buffers,
2156 * separate from the data. The HBA can then by instructed
2157 * to place the DIFs in the outgoing stream. For read operations,
2158 * The HBA could extract the DIFs and place it in DIF buffers.
2160 * The buffer list for this type consists of one or more of the
2161 * protection groups described below:
2162 * +-------------------------+
2163 * start of first prot group --> | PDE_5 |
2164 * +-------------------------+
2165 * | PDE_6 |
2166 * +-------------------------+
2167 * | PDE_7 (Prot BDE) |
2168 * +-------------------------+
2169 * | Data BDE |
2170 * +-------------------------+
2171 * |more Data BDE's ... (opt)|
2172 * +-------------------------+
2173 * start of new prot group --> | PDE_5 |
2174 * +-------------------------+
2175 * | ... |
2176 * +-------------------------+
2178 * Note: It is assumed that both data and protection s/g buffers have been
2179 * mapped for DMA
2181 * Returns the number of BDEs added to the BPL.
2183 static int
2184 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2185 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2187 struct scatterlist *sgde = NULL; /* s/g data entry */
2188 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2189 struct lpfc_pde5 *pde5 = NULL;
2190 struct lpfc_pde6 *pde6 = NULL;
2191 struct lpfc_pde7 *pde7 = NULL;
2192 dma_addr_t dataphysaddr, protphysaddr;
2193 unsigned short curr_data = 0, curr_prot = 0;
2194 unsigned int split_offset;
2195 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2196 unsigned int protgrp_blks, protgrp_bytes;
2197 unsigned int remainder, subtotal;
2198 int status;
2199 int datadir = sc->sc_data_direction;
2200 unsigned char pgdone = 0, alldone = 0;
2201 unsigned blksize;
2202 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2203 uint32_t rc;
2204 #endif
2205 uint32_t checking = 1;
2206 uint32_t reftag;
2207 uint8_t txop, rxop;
2208 int num_bde = 0;
2210 sgpe = scsi_prot_sglist(sc);
2211 sgde = scsi_sglist(sc);
2213 if (!sgpe || !sgde) {
2214 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2215 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2216 sgpe, sgde);
2217 return 0;
2220 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2221 if (status)
2222 goto out;
2224 /* extract some info from the scsi command */
2225 blksize = lpfc_cmd_blksize(sc);
2226 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2228 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2229 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2230 if (rc) {
2231 if (rc & BG_ERR_SWAP)
2232 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2233 if (rc & BG_ERR_CHECK)
2234 checking = 0;
2236 #endif
2238 split_offset = 0;
2239 do {
2240 /* Check to see if we ran out of space */
2241 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2242 return num_bde + 3;
2244 /* setup PDE5 with what we have */
2245 pde5 = (struct lpfc_pde5 *) bpl;
2246 memset(pde5, 0, sizeof(struct lpfc_pde5));
2247 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
2249 /* Endianness conversion if necessary for PDE5 */
2250 pde5->word0 = cpu_to_le32(pde5->word0);
2251 pde5->reftag = cpu_to_le32(reftag);
2253 /* advance bpl and increment bde count */
2254 num_bde++;
2255 bpl++;
2256 pde6 = (struct lpfc_pde6 *) bpl;
2258 /* setup PDE6 with the rest of the info */
2259 memset(pde6, 0, sizeof(struct lpfc_pde6));
2260 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2261 bf_set(pde6_optx, pde6, txop);
2262 bf_set(pde6_oprx, pde6, rxop);
2264 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2265 bf_set(pde6_ce, pde6, checking);
2266 else
2267 bf_set(pde6_ce, pde6, 0);
2269 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2270 bf_set(pde6_re, pde6, checking);
2271 else
2272 bf_set(pde6_re, pde6, 0);
2274 bf_set(pde6_ai, pde6, 1);
2275 bf_set(pde6_ae, pde6, 0);
2276 bf_set(pde6_apptagval, pde6, 0);
2278 /* Endianness conversion if necessary for PDE6 */
2279 pde6->word0 = cpu_to_le32(pde6->word0);
2280 pde6->word1 = cpu_to_le32(pde6->word1);
2281 pde6->word2 = cpu_to_le32(pde6->word2);
2283 /* advance bpl and increment bde count */
2284 num_bde++;
2285 bpl++;
2287 /* setup the first BDE that points to protection buffer */
2288 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2289 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2291 /* must be integer multiple of the DIF block length */
2292 BUG_ON(protgroup_len % 8);
2294 pde7 = (struct lpfc_pde7 *) bpl;
2295 memset(pde7, 0, sizeof(struct lpfc_pde7));
2296 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2298 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2299 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
2301 protgrp_blks = protgroup_len / 8;
2302 protgrp_bytes = protgrp_blks * blksize;
2304 /* check if this pde is crossing the 4K boundary; if so split */
2305 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2306 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2307 protgroup_offset += protgroup_remainder;
2308 protgrp_blks = protgroup_remainder / 8;
2309 protgrp_bytes = protgrp_blks * blksize;
2310 } else {
2311 protgroup_offset = 0;
2312 curr_prot++;
2315 num_bde++;
2317 /* setup BDE's for data blocks associated with DIF data */
2318 pgdone = 0;
2319 subtotal = 0; /* total bytes processed for current prot grp */
2320 while (!pgdone) {
2321 /* Check to see if we ran out of space */
2322 if (num_bde >= phba->cfg_total_seg_cnt)
2323 return num_bde + 1;
2325 if (!sgde) {
2326 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2327 "9065 BLKGRD:%s Invalid data segment\n",
2328 __func__);
2329 return 0;
2331 bpl++;
2332 dataphysaddr = sg_dma_address(sgde) + split_offset;
2333 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2334 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2336 remainder = sg_dma_len(sgde) - split_offset;
2338 if ((subtotal + remainder) <= protgrp_bytes) {
2339 /* we can use this whole buffer */
2340 bpl->tus.f.bdeSize = remainder;
2341 split_offset = 0;
2343 if ((subtotal + remainder) == protgrp_bytes)
2344 pgdone = 1;
2345 } else {
2346 /* must split this buffer with next prot grp */
2347 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2348 split_offset += bpl->tus.f.bdeSize;
2351 subtotal += bpl->tus.f.bdeSize;
2353 if (datadir == DMA_TO_DEVICE)
2354 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2355 else
2356 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2357 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2359 num_bde++;
2360 curr_data++;
2362 if (split_offset)
2363 break;
2365 /* Move to the next s/g segment if possible */
2366 sgde = sg_next(sgde);
2370 if (protgroup_offset) {
2371 /* update the reference tag */
2372 reftag += protgrp_blks;
2373 bpl++;
2374 continue;
2377 /* are we done ? */
2378 if (curr_prot == protcnt) {
2379 alldone = 1;
2380 } else if (curr_prot < protcnt) {
2381 /* advance to next prot buffer */
2382 sgpe = sg_next(sgpe);
2383 bpl++;
2385 /* update the reference tag */
2386 reftag += protgrp_blks;
2387 } else {
2388 /* if we're here, we have a bug */
2389 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2390 "9054 BLKGRD: bug in %s\n", __func__);
2393 } while (!alldone);
2394 out:
2396 return num_bde;
2400 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2401 * @phba: The Hba for which this call is being executed.
2402 * @sc: pointer to scsi command we're working on
2403 * @sgl: pointer to buffer list for protection groups
2404 * @datacnt: number of segments of data that have been dma mapped
2406 * This function sets up SGL buffer list for protection groups of
2407 * type LPFC_PG_TYPE_NO_DIF
2409 * This is usually used when the HBA is instructed to generate
2410 * DIFs and insert them into data stream (or strip DIF from
2411 * incoming data stream)
2413 * The buffer list consists of just one protection group described
2414 * below:
2415 * +-------------------------+
2416 * start of prot group --> | DI_SEED |
2417 * +-------------------------+
2418 * | Data SGE |
2419 * +-------------------------+
2420 * |more Data SGE's ... (opt)|
2421 * +-------------------------+
2424 * Note: Data s/g buffers have been dma mapped
2426 * Returns the number of SGEs added to the SGL.
2428 static int
2429 lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2430 struct sli4_sge *sgl, int datasegcnt)
2432 struct scatterlist *sgde = NULL; /* s/g data entry */
2433 struct sli4_sge_diseed *diseed = NULL;
2434 dma_addr_t physaddr;
2435 int i = 0, num_sge = 0, status;
2436 uint32_t reftag;
2437 unsigned blksize;
2438 uint8_t txop, rxop;
2439 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2440 uint32_t rc;
2441 #endif
2442 uint32_t checking = 1;
2443 uint32_t dma_len;
2444 uint32_t dma_offset = 0;
2446 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2447 if (status)
2448 goto out;
2450 /* extract some info from the scsi command for pde*/
2451 blksize = lpfc_cmd_blksize(sc);
2452 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2454 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2455 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2456 if (rc) {
2457 if (rc & BG_ERR_SWAP)
2458 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2459 if (rc & BG_ERR_CHECK)
2460 checking = 0;
2462 #endif
2464 /* setup DISEED with what we have */
2465 diseed = (struct sli4_sge_diseed *) sgl;
2466 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2467 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2469 /* Endianness conversion if necessary */
2470 diseed->ref_tag = cpu_to_le32(reftag);
2471 diseed->ref_tag_tran = diseed->ref_tag;
2474 * We only need to check the data on READs, for WRITEs
2475 * protection data is automatically generated, not checked.
2477 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2478 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2479 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2480 else
2481 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2483 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2484 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2485 else
2486 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2489 /* setup DISEED with the rest of the info */
2490 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2491 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2493 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2494 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2496 /* Endianness conversion if necessary for DISEED */
2497 diseed->word2 = cpu_to_le32(diseed->word2);
2498 diseed->word3 = cpu_to_le32(diseed->word3);
2500 /* advance bpl and increment sge count */
2501 num_sge++;
2502 sgl++;
2504 /* assumption: caller has already run dma_map_sg on command data */
2505 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2506 physaddr = sg_dma_address(sgde);
2507 dma_len = sg_dma_len(sgde);
2508 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2509 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2510 if ((i + 1) == datasegcnt)
2511 bf_set(lpfc_sli4_sge_last, sgl, 1);
2512 else
2513 bf_set(lpfc_sli4_sge_last, sgl, 0);
2514 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2515 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2517 sgl->sge_len = cpu_to_le32(dma_len);
2518 dma_offset += dma_len;
2520 sgl++;
2521 num_sge++;
2524 out:
2525 return num_sge;
2529 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2530 * @phba: The Hba for which this call is being executed.
2531 * @sc: pointer to scsi command we're working on
2532 * @sgl: pointer to buffer list for protection groups
2533 * @datacnt: number of segments of data that have been dma mapped
2534 * @protcnt: number of segment of protection data that have been dma mapped
2536 * This function sets up SGL buffer list for protection groups of
2537 * type LPFC_PG_TYPE_DIF
2539 * This is usually used when DIFs are in their own buffers,
2540 * separate from the data. The HBA can then by instructed
2541 * to place the DIFs in the outgoing stream. For read operations,
2542 * The HBA could extract the DIFs and place it in DIF buffers.
2544 * The buffer list for this type consists of one or more of the
2545 * protection groups described below:
2546 * +-------------------------+
2547 * start of first prot group --> | DISEED |
2548 * +-------------------------+
2549 * | DIF (Prot SGE) |
2550 * +-------------------------+
2551 * | Data SGE |
2552 * +-------------------------+
2553 * |more Data SGE's ... (opt)|
2554 * +-------------------------+
2555 * start of new prot group --> | DISEED |
2556 * +-------------------------+
2557 * | ... |
2558 * +-------------------------+
2560 * Note: It is assumed that both data and protection s/g buffers have been
2561 * mapped for DMA
2563 * Returns the number of SGEs added to the SGL.
2565 static int
2566 lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2567 struct sli4_sge *sgl, int datacnt, int protcnt)
2569 struct scatterlist *sgde = NULL; /* s/g data entry */
2570 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2571 struct sli4_sge_diseed *diseed = NULL;
2572 dma_addr_t dataphysaddr, protphysaddr;
2573 unsigned short curr_data = 0, curr_prot = 0;
2574 unsigned int split_offset;
2575 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2576 unsigned int protgrp_blks, protgrp_bytes;
2577 unsigned int remainder, subtotal;
2578 int status;
2579 unsigned char pgdone = 0, alldone = 0;
2580 unsigned blksize;
2581 uint32_t reftag;
2582 uint8_t txop, rxop;
2583 uint32_t dma_len;
2584 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2585 uint32_t rc;
2586 #endif
2587 uint32_t checking = 1;
2588 uint32_t dma_offset = 0;
2589 int num_sge = 0;
2591 sgpe = scsi_prot_sglist(sc);
2592 sgde = scsi_sglist(sc);
2594 if (!sgpe || !sgde) {
2595 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2596 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2597 sgpe, sgde);
2598 return 0;
2601 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2602 if (status)
2603 goto out;
2605 /* extract some info from the scsi command */
2606 blksize = lpfc_cmd_blksize(sc);
2607 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2609 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2610 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2611 if (rc) {
2612 if (rc & BG_ERR_SWAP)
2613 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2614 if (rc & BG_ERR_CHECK)
2615 checking = 0;
2617 #endif
2619 split_offset = 0;
2620 do {
2621 /* Check to see if we ran out of space */
2622 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2623 return num_sge + 3;
2625 /* setup DISEED with what we have */
2626 diseed = (struct sli4_sge_diseed *) sgl;
2627 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2628 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2630 /* Endianness conversion if necessary */
2631 diseed->ref_tag = cpu_to_le32(reftag);
2632 diseed->ref_tag_tran = diseed->ref_tag;
2634 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
2635 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2637 } else {
2638 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2640 * When in this mode, the hardware will replace
2641 * the guard tag from the host with a
2642 * newly generated good CRC for the wire.
2643 * Switch to raw mode here to avoid this
2644 * behavior. What the host sends gets put on the wire.
2646 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2647 txop = BG_OP_RAW_MODE;
2648 rxop = BG_OP_RAW_MODE;
2653 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2654 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2655 else
2656 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2658 /* setup DISEED with the rest of the info */
2659 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2660 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2662 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2663 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2665 /* Endianness conversion if necessary for DISEED */
2666 diseed->word2 = cpu_to_le32(diseed->word2);
2667 diseed->word3 = cpu_to_le32(diseed->word3);
2669 /* advance sgl and increment bde count */
2670 num_sge++;
2671 sgl++;
2673 /* setup the first BDE that points to protection buffer */
2674 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2675 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2677 /* must be integer multiple of the DIF block length */
2678 BUG_ON(protgroup_len % 8);
2680 /* Now setup DIF SGE */
2681 sgl->word2 = 0;
2682 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2683 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2684 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2685 sgl->word2 = cpu_to_le32(sgl->word2);
2687 protgrp_blks = protgroup_len / 8;
2688 protgrp_bytes = protgrp_blks * blksize;
2690 /* check if DIF SGE is crossing the 4K boundary; if so split */
2691 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2692 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2693 protgroup_offset += protgroup_remainder;
2694 protgrp_blks = protgroup_remainder / 8;
2695 protgrp_bytes = protgrp_blks * blksize;
2696 } else {
2697 protgroup_offset = 0;
2698 curr_prot++;
2701 num_sge++;
2703 /* setup SGE's for data blocks associated with DIF data */
2704 pgdone = 0;
2705 subtotal = 0; /* total bytes processed for current prot grp */
2706 while (!pgdone) {
2707 /* Check to see if we ran out of space */
2708 if (num_sge >= phba->cfg_total_seg_cnt)
2709 return num_sge + 1;
2711 if (!sgde) {
2712 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2713 "9086 BLKGRD:%s Invalid data segment\n",
2714 __func__);
2715 return 0;
2717 sgl++;
2718 dataphysaddr = sg_dma_address(sgde) + split_offset;
2720 remainder = sg_dma_len(sgde) - split_offset;
2722 if ((subtotal + remainder) <= protgrp_bytes) {
2723 /* we can use this whole buffer */
2724 dma_len = remainder;
2725 split_offset = 0;
2727 if ((subtotal + remainder) == protgrp_bytes)
2728 pgdone = 1;
2729 } else {
2730 /* must split this buffer with next prot grp */
2731 dma_len = protgrp_bytes - subtotal;
2732 split_offset += dma_len;
2735 subtotal += dma_len;
2737 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2738 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2739 bf_set(lpfc_sli4_sge_last, sgl, 0);
2740 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2741 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2743 sgl->sge_len = cpu_to_le32(dma_len);
2744 dma_offset += dma_len;
2746 num_sge++;
2747 curr_data++;
2749 if (split_offset)
2750 break;
2752 /* Move to the next s/g segment if possible */
2753 sgde = sg_next(sgde);
2756 if (protgroup_offset) {
2757 /* update the reference tag */
2758 reftag += protgrp_blks;
2759 sgl++;
2760 continue;
2763 /* are we done ? */
2764 if (curr_prot == protcnt) {
2765 bf_set(lpfc_sli4_sge_last, sgl, 1);
2766 alldone = 1;
2767 } else if (curr_prot < protcnt) {
2768 /* advance to next prot buffer */
2769 sgpe = sg_next(sgpe);
2770 sgl++;
2772 /* update the reference tag */
2773 reftag += protgrp_blks;
2774 } else {
2775 /* if we're here, we have a bug */
2776 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2777 "9085 BLKGRD: bug in %s\n", __func__);
2780 } while (!alldone);
2782 out:
2784 return num_sge;
2788 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2789 * @phba: The Hba for which this call is being executed.
2790 * @sc: pointer to scsi command we're working on
2792 * Given a SCSI command that supports DIF, determine composition of protection
2793 * groups involved in setting up buffer lists
2795 * Returns: Protection group type (with or without DIF)
2798 static int
2799 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2801 int ret = LPFC_PG_TYPE_INVALID;
2802 unsigned char op = scsi_get_prot_op(sc);
2804 switch (op) {
2805 case SCSI_PROT_READ_STRIP:
2806 case SCSI_PROT_WRITE_INSERT:
2807 ret = LPFC_PG_TYPE_NO_DIF;
2808 break;
2809 case SCSI_PROT_READ_INSERT:
2810 case SCSI_PROT_WRITE_STRIP:
2811 case SCSI_PROT_READ_PASS:
2812 case SCSI_PROT_WRITE_PASS:
2813 ret = LPFC_PG_TYPE_DIF_BUF;
2814 break;
2815 default:
2816 if (phba)
2817 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2818 "9021 Unsupported protection op:%d\n",
2819 op);
2820 break;
2822 return ret;
2826 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2827 * @phba: The Hba for which this call is being executed.
2828 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2830 * Adjust the data length to account for how much data
2831 * is actually on the wire.
2833 * returns the adjusted data length
2835 static int
2836 lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2837 struct lpfc_scsi_buf *lpfc_cmd)
2839 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2840 int fcpdl;
2842 fcpdl = scsi_bufflen(sc);
2844 /* Check if there is protection data on the wire */
2845 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2846 /* Read check for protection data */
2847 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2848 return fcpdl;
2850 } else {
2851 /* Write check for protection data */
2852 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2853 return fcpdl;
2857 * If we are in DIF Type 1 mode every data block has a 8 byte
2858 * DIF (trailer) attached to it. Must ajust FCP data length
2859 * to account for the protection data.
2861 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
2863 return fcpdl;
2867 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2868 * @phba: The Hba for which this call is being executed.
2869 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2871 * This is the protection/DIF aware version of
2872 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2873 * two functions eventually, but for now, it's here
2875 static int
2876 lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2877 struct lpfc_scsi_buf *lpfc_cmd)
2879 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2880 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2881 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2882 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2883 uint32_t num_bde = 0;
2884 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2885 int prot_group_type = 0;
2886 int fcpdl;
2889 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2890 * fcp_rsp regions to the first data bde entry
2892 bpl += 2;
2893 if (scsi_sg_count(scsi_cmnd)) {
2895 * The driver stores the segment count returned from pci_map_sg
2896 * because this a count of dma-mappings used to map the use_sg
2897 * pages. They are not guaranteed to be the same for those
2898 * architectures that implement an IOMMU.
2900 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2901 scsi_sglist(scsi_cmnd),
2902 scsi_sg_count(scsi_cmnd), datadir);
2903 if (unlikely(!datasegcnt))
2904 return 1;
2906 lpfc_cmd->seg_cnt = datasegcnt;
2908 /* First check if data segment count from SCSI Layer is good */
2909 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2910 goto err;
2912 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2914 switch (prot_group_type) {
2915 case LPFC_PG_TYPE_NO_DIF:
2917 /* Here we need to add a PDE5 and PDE6 to the count */
2918 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2919 goto err;
2921 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2922 datasegcnt);
2923 /* we should have 2 or more entries in buffer list */
2924 if (num_bde < 2)
2925 goto err;
2926 break;
2928 case LPFC_PG_TYPE_DIF_BUF:
2930 * This type indicates that protection buffers are
2931 * passed to the driver, so that needs to be prepared
2932 * for DMA
2934 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2935 scsi_prot_sglist(scsi_cmnd),
2936 scsi_prot_sg_count(scsi_cmnd), datadir);
2937 if (unlikely(!protsegcnt)) {
2938 scsi_dma_unmap(scsi_cmnd);
2939 return 1;
2942 lpfc_cmd->prot_seg_cnt = protsegcnt;
2945 * There is a minimun of 4 BPLs used for every
2946 * protection data segment.
2948 if ((lpfc_cmd->prot_seg_cnt * 4) >
2949 (phba->cfg_total_seg_cnt - 2))
2950 goto err;
2952 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2953 datasegcnt, protsegcnt);
2954 /* we should have 3 or more entries in buffer list */
2955 if ((num_bde < 3) ||
2956 (num_bde > phba->cfg_total_seg_cnt))
2957 goto err;
2958 break;
2960 case LPFC_PG_TYPE_INVALID:
2961 default:
2962 scsi_dma_unmap(scsi_cmnd);
2963 lpfc_cmd->seg_cnt = 0;
2965 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2966 "9022 Unexpected protection group %i\n",
2967 prot_group_type);
2968 return 1;
2973 * Finish initializing those IOCB fields that are dependent on the
2974 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2975 * reinitialized since all iocb memory resources are used many times
2976 * for transmit, receive, and continuation bpl's.
2978 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2979 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2980 iocb_cmd->ulpBdeCount = 1;
2981 iocb_cmd->ulpLe = 1;
2983 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
2984 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2987 * Due to difference in data length between DIF/non-DIF paths,
2988 * we need to set word 4 of IOCB here
2990 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2992 return 0;
2993 err:
2994 if (lpfc_cmd->seg_cnt)
2995 scsi_dma_unmap(scsi_cmnd);
2996 if (lpfc_cmd->prot_seg_cnt)
2997 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2998 scsi_prot_sg_count(scsi_cmnd),
2999 scsi_cmnd->sc_data_direction);
3001 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3002 "9023 Cannot setup S/G List for HBA"
3003 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
3004 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3005 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3006 prot_group_type, num_bde);
3008 lpfc_cmd->seg_cnt = 0;
3009 lpfc_cmd->prot_seg_cnt = 0;
3010 return 1;
3014 * This function calcuates the T10 DIF guard tag
3015 * on the specified data using a CRC algorithmn
3016 * using crc_t10dif.
3018 uint16_t
3019 lpfc_bg_crc(uint8_t *data, int count)
3021 uint16_t crc = 0;
3022 uint16_t x;
3024 crc = crc_t10dif(data, count);
3025 x = cpu_to_be16(crc);
3026 return x;
3030 * This function calcuates the T10 DIF guard tag
3031 * on the specified data using a CSUM algorithmn
3032 * using ip_compute_csum.
3034 uint16_t
3035 lpfc_bg_csum(uint8_t *data, int count)
3037 uint16_t ret;
3039 ret = ip_compute_csum(data, count);
3040 return ret;
3044 * This function examines the protection data to try to determine
3045 * what type of T10-DIF error occurred.
3047 void
3048 lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3050 struct scatterlist *sgpe; /* s/g prot entry */
3051 struct scatterlist *sgde; /* s/g data entry */
3052 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3053 struct scsi_dif_tuple *src = NULL;
3054 uint8_t *data_src = NULL;
3055 uint16_t guard_tag, guard_type;
3056 uint16_t start_app_tag, app_tag;
3057 uint32_t start_ref_tag, ref_tag;
3058 int prot, protsegcnt;
3059 int err_type, len, data_len;
3060 int chk_ref, chk_app, chk_guard;
3061 uint16_t sum;
3062 unsigned blksize;
3064 err_type = BGS_GUARD_ERR_MASK;
3065 sum = 0;
3066 guard_tag = 0;
3068 /* First check to see if there is protection data to examine */
3069 prot = scsi_get_prot_op(cmd);
3070 if ((prot == SCSI_PROT_READ_STRIP) ||
3071 (prot == SCSI_PROT_WRITE_INSERT) ||
3072 (prot == SCSI_PROT_NORMAL))
3073 goto out;
3075 /* Currently the driver just supports ref_tag and guard_tag checking */
3076 chk_ref = 1;
3077 chk_app = 0;
3078 chk_guard = 0;
3080 /* Setup a ptr to the protection data provided by the SCSI host */
3081 sgpe = scsi_prot_sglist(cmd);
3082 protsegcnt = lpfc_cmd->prot_seg_cnt;
3084 if (sgpe && protsegcnt) {
3087 * We will only try to verify guard tag if the segment
3088 * data length is a multiple of the blksize.
3090 sgde = scsi_sglist(cmd);
3091 blksize = lpfc_cmd_blksize(cmd);
3092 data_src = (uint8_t *)sg_virt(sgde);
3093 data_len = sgde->length;
3094 if ((data_len & (blksize - 1)) == 0)
3095 chk_guard = 1;
3096 guard_type = scsi_host_get_guard(cmd->device->host);
3098 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3099 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
3100 start_app_tag = src->app_tag;
3101 len = sgpe->length;
3102 while (src && protsegcnt) {
3103 while (len) {
3106 * First check to see if a protection data
3107 * check is valid
3109 if ((src->ref_tag == 0xffffffff) ||
3110 (src->app_tag == 0xffff)) {
3111 start_ref_tag++;
3112 goto skipit;
3115 /* First Guard Tag checking */
3116 if (chk_guard) {
3117 guard_tag = src->guard_tag;
3118 if (lpfc_cmd_guard_csum(cmd))
3119 sum = lpfc_bg_csum(data_src,
3120 blksize);
3121 else
3122 sum = lpfc_bg_crc(data_src,
3123 blksize);
3124 if ((guard_tag != sum)) {
3125 err_type = BGS_GUARD_ERR_MASK;
3126 goto out;
3130 /* Reference Tag checking */
3131 ref_tag = be32_to_cpu(src->ref_tag);
3132 if (chk_ref && (ref_tag != start_ref_tag)) {
3133 err_type = BGS_REFTAG_ERR_MASK;
3134 goto out;
3136 start_ref_tag++;
3138 /* App Tag checking */
3139 app_tag = src->app_tag;
3140 if (chk_app && (app_tag != start_app_tag)) {
3141 err_type = BGS_APPTAG_ERR_MASK;
3142 goto out;
3144 skipit:
3145 len -= sizeof(struct scsi_dif_tuple);
3146 if (len < 0)
3147 len = 0;
3148 src++;
3150 data_src += blksize;
3151 data_len -= blksize;
3154 * Are we at the end of the Data segment?
3155 * The data segment is only used for Guard
3156 * tag checking.
3158 if (chk_guard && (data_len == 0)) {
3159 chk_guard = 0;
3160 sgde = sg_next(sgde);
3161 if (!sgde)
3162 goto out;
3164 data_src = (uint8_t *)sg_virt(sgde);
3165 data_len = sgde->length;
3166 if ((data_len & (blksize - 1)) == 0)
3167 chk_guard = 1;
3171 /* Goto the next Protection data segment */
3172 sgpe = sg_next(sgpe);
3173 if (sgpe) {
3174 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3175 len = sgpe->length;
3176 } else {
3177 src = NULL;
3179 protsegcnt--;
3182 out:
3183 if (err_type == BGS_GUARD_ERR_MASK) {
3184 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3185 0x10, 0x1);
3186 cmd->result = DRIVER_SENSE << 24
3187 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3188 phba->bg_guard_err_cnt++;
3189 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3190 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3191 (unsigned long)scsi_get_lba(cmd),
3192 sum, guard_tag);
3194 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3195 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3196 0x10, 0x3);
3197 cmd->result = DRIVER_SENSE << 24
3198 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3200 phba->bg_reftag_err_cnt++;
3201 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3202 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3203 (unsigned long)scsi_get_lba(cmd),
3204 ref_tag, start_ref_tag);
3206 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3207 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3208 0x10, 0x2);
3209 cmd->result = DRIVER_SENSE << 24
3210 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3212 phba->bg_apptag_err_cnt++;
3213 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3214 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3215 (unsigned long)scsi_get_lba(cmd),
3216 app_tag, start_app_tag);
3222 * This function checks for BlockGuard errors detected by
3223 * the HBA. In case of errors, the ASC/ASCQ fields in the
3224 * sense buffer will be set accordingly, paired with
3225 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3226 * detected corruption.
3228 * Returns:
3229 * 0 - No error found
3230 * 1 - BlockGuard error found
3231 * -1 - Internal error (bad profile, ...etc)
3233 static int
3234 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3235 struct lpfc_iocbq *pIocbOut)
3237 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3238 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3239 int ret = 0;
3240 uint32_t bghm = bgf->bghm;
3241 uint32_t bgstat = bgf->bgstat;
3242 uint64_t failing_sector = 0;
3244 spin_lock(&_dump_buf_lock);
3245 if (!_dump_buf_done) {
3246 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3247 " Data for %u blocks to debugfs\n",
3248 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3249 lpfc_debug_save_data(phba, cmd);
3251 /* If we have a prot sgl, save the DIF buffer */
3252 if (lpfc_prot_group_type(phba, cmd) ==
3253 LPFC_PG_TYPE_DIF_BUF) {
3254 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3255 "Saving DIF for %u blocks to debugfs\n",
3256 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3257 lpfc_debug_save_dif(phba, cmd);
3260 _dump_buf_done = 1;
3262 spin_unlock(&_dump_buf_lock);
3264 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3265 cmd->result = ScsiResult(DID_ERROR, 0);
3266 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3267 "9072 BLKGRD: Invalid BG Profile in cmd"
3268 " 0x%x lba 0x%llx blk cnt 0x%x "
3269 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3270 (unsigned long long)scsi_get_lba(cmd),
3271 blk_rq_sectors(cmd->request), bgstat, bghm);
3272 ret = (-1);
3273 goto out;
3276 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3277 cmd->result = ScsiResult(DID_ERROR, 0);
3278 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3279 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3280 " 0x%x lba 0x%llx blk cnt 0x%x "
3281 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3282 (unsigned long long)scsi_get_lba(cmd),
3283 blk_rq_sectors(cmd->request), bgstat, bghm);
3284 ret = (-1);
3285 goto out;
3288 if (lpfc_bgs_get_guard_err(bgstat)) {
3289 ret = 1;
3291 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3292 0x10, 0x1);
3293 cmd->result = DRIVER_SENSE << 24
3294 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3295 phba->bg_guard_err_cnt++;
3296 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3297 "9055 BLKGRD: Guard Tag error in cmd"
3298 " 0x%x lba 0x%llx blk cnt 0x%x "
3299 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3300 (unsigned long long)scsi_get_lba(cmd),
3301 blk_rq_sectors(cmd->request), bgstat, bghm);
3304 if (lpfc_bgs_get_reftag_err(bgstat)) {
3305 ret = 1;
3307 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3308 0x10, 0x3);
3309 cmd->result = DRIVER_SENSE << 24
3310 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3312 phba->bg_reftag_err_cnt++;
3313 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3314 "9056 BLKGRD: Ref Tag error in cmd"
3315 " 0x%x lba 0x%llx blk cnt 0x%x "
3316 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3317 (unsigned long long)scsi_get_lba(cmd),
3318 blk_rq_sectors(cmd->request), bgstat, bghm);
3321 if (lpfc_bgs_get_apptag_err(bgstat)) {
3322 ret = 1;
3324 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3325 0x10, 0x2);
3326 cmd->result = DRIVER_SENSE << 24
3327 | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
3329 phba->bg_apptag_err_cnt++;
3330 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3331 "9061 BLKGRD: App Tag error in cmd"
3332 " 0x%x lba 0x%llx blk cnt 0x%x "
3333 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3334 (unsigned long long)scsi_get_lba(cmd),
3335 blk_rq_sectors(cmd->request), bgstat, bghm);
3338 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3340 * setup sense data descriptor 0 per SPC-4 as an information
3341 * field, and put the failing LBA in it.
3342 * This code assumes there was also a guard/app/ref tag error
3343 * indication.
3345 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3346 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3347 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3348 cmd->sense_buffer[10] = 0x80; /* Validity bit */
3350 /* bghm is a "on the wire" FC frame based count */
3351 switch (scsi_get_prot_op(cmd)) {
3352 case SCSI_PROT_READ_INSERT:
3353 case SCSI_PROT_WRITE_STRIP:
3354 bghm /= cmd->device->sector_size;
3355 break;
3356 case SCSI_PROT_READ_STRIP:
3357 case SCSI_PROT_WRITE_INSERT:
3358 case SCSI_PROT_READ_PASS:
3359 case SCSI_PROT_WRITE_PASS:
3360 bghm /= (cmd->device->sector_size +
3361 sizeof(struct scsi_dif_tuple));
3362 break;
3365 failing_sector = scsi_get_lba(cmd);
3366 failing_sector += bghm;
3368 /* Descriptor Information */
3369 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3372 if (!ret) {
3373 /* No error was reported - problem in FW? */
3374 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3375 "9057 BLKGRD: Unknown error in cmd"
3376 " 0x%x lba 0x%llx blk cnt 0x%x "
3377 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3378 (unsigned long long)scsi_get_lba(cmd),
3379 blk_rq_sectors(cmd->request), bgstat, bghm);
3381 /* Calcuate what type of error it was */
3382 lpfc_calc_bg_err(phba, lpfc_cmd);
3384 out:
3385 return ret;
3389 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3390 * @phba: The Hba for which this call is being executed.
3391 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3393 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3394 * field of @lpfc_cmd for device with SLI-4 interface spec.
3396 * Return codes:
3397 * 1 - Error
3398 * 0 - Success
3400 static int
3401 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3403 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3404 struct scatterlist *sgel = NULL;
3405 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3406 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
3407 struct sli4_sge *first_data_sgl;
3408 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3409 dma_addr_t physaddr;
3410 uint32_t num_bde = 0;
3411 uint32_t dma_len;
3412 uint32_t dma_offset = 0;
3413 int nseg;
3414 struct ulp_bde64 *bde;
3417 * There are three possibilities here - use scatter-gather segment, use
3418 * the single mapping, or neither. Start the lpfc command prep by
3419 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3420 * data bde entry.
3422 if (scsi_sg_count(scsi_cmnd)) {
3424 * The driver stores the segment count returned from pci_map_sg
3425 * because this a count of dma-mappings used to map the use_sg
3426 * pages. They are not guaranteed to be the same for those
3427 * architectures that implement an IOMMU.
3430 nseg = scsi_dma_map(scsi_cmnd);
3431 if (unlikely(!nseg))
3432 return 1;
3433 sgl += 1;
3434 /* clear the last flag in the fcp_rsp map entry */
3435 sgl->word2 = le32_to_cpu(sgl->word2);
3436 bf_set(lpfc_sli4_sge_last, sgl, 0);
3437 sgl->word2 = cpu_to_le32(sgl->word2);
3438 sgl += 1;
3439 first_data_sgl = sgl;
3440 lpfc_cmd->seg_cnt = nseg;
3441 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3442 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3443 " %s: Too many sg segments from "
3444 "dma_map_sg. Config %d, seg_cnt %d\n",
3445 __func__, phba->cfg_sg_seg_cnt,
3446 lpfc_cmd->seg_cnt);
3447 lpfc_cmd->seg_cnt = 0;
3448 scsi_dma_unmap(scsi_cmnd);
3449 return 1;
3453 * The driver established a maximum scatter-gather segment count
3454 * during probe that limits the number of sg elements in any
3455 * single scsi command. Just run through the seg_cnt and format
3456 * the sge's.
3457 * When using SLI-3 the driver will try to fit all the BDEs into
3458 * the IOCB. If it can't then the BDEs get added to a BPL as it
3459 * does for SLI-2 mode.
3461 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3462 physaddr = sg_dma_address(sgel);
3463 dma_len = sg_dma_len(sgel);
3464 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3465 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
3466 sgl->word2 = le32_to_cpu(sgl->word2);
3467 if ((num_bde + 1) == nseg)
3468 bf_set(lpfc_sli4_sge_last, sgl, 1);
3469 else
3470 bf_set(lpfc_sli4_sge_last, sgl, 0);
3471 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3472 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
3473 sgl->word2 = cpu_to_le32(sgl->word2);
3474 sgl->sge_len = cpu_to_le32(dma_len);
3475 dma_offset += dma_len;
3476 sgl++;
3478 /* setup the performance hint (first data BDE) if enabled */
3479 if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
3480 bde = (struct ulp_bde64 *)
3481 &(iocb_cmd->unsli3.sli3Words[5]);
3482 bde->addrLow = first_data_sgl->addr_lo;
3483 bde->addrHigh = first_data_sgl->addr_hi;
3484 bde->tus.f.bdeSize =
3485 le32_to_cpu(first_data_sgl->sge_len);
3486 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3487 bde->tus.w = cpu_to_le32(bde->tus.w);
3489 } else {
3490 sgl += 1;
3491 /* clear the last flag in the fcp_rsp map entry */
3492 sgl->word2 = le32_to_cpu(sgl->word2);
3493 bf_set(lpfc_sli4_sge_last, sgl, 1);
3494 sgl->word2 = cpu_to_le32(sgl->word2);
3498 * Finish initializing those IOCB fields that are dependent on the
3499 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3500 * explicitly reinitialized.
3501 * all iocb memory resources are reused.
3503 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3506 * Due to difference in data length between DIF/non-DIF paths,
3507 * we need to set word 4 of IOCB here
3509 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3510 return 0;
3514 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3515 * @phba: The Hba for which this call is being executed.
3516 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3518 * This is the protection/DIF aware version of
3519 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3520 * two functions eventually, but for now, it's here
3522 static int
3523 lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3524 struct lpfc_scsi_buf *lpfc_cmd)
3526 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3527 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3528 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3529 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3530 uint32_t num_sge = 0;
3531 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3532 int prot_group_type = 0;
3533 int fcpdl;
3536 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3537 * fcp_rsp regions to the first data sge entry
3539 if (scsi_sg_count(scsi_cmnd)) {
3541 * The driver stores the segment count returned from pci_map_sg
3542 * because this a count of dma-mappings used to map the use_sg
3543 * pages. They are not guaranteed to be the same for those
3544 * architectures that implement an IOMMU.
3546 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3547 scsi_sglist(scsi_cmnd),
3548 scsi_sg_count(scsi_cmnd), datadir);
3549 if (unlikely(!datasegcnt))
3550 return 1;
3552 sgl += 1;
3553 /* clear the last flag in the fcp_rsp map entry */
3554 sgl->word2 = le32_to_cpu(sgl->word2);
3555 bf_set(lpfc_sli4_sge_last, sgl, 0);
3556 sgl->word2 = cpu_to_le32(sgl->word2);
3558 sgl += 1;
3559 lpfc_cmd->seg_cnt = datasegcnt;
3561 /* First check if data segment count from SCSI Layer is good */
3562 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3563 goto err;
3565 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3567 switch (prot_group_type) {
3568 case LPFC_PG_TYPE_NO_DIF:
3569 /* Here we need to add a DISEED to the count */
3570 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3571 goto err;
3573 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3574 datasegcnt);
3576 /* we should have 2 or more entries in buffer list */
3577 if (num_sge < 2)
3578 goto err;
3579 break;
3581 case LPFC_PG_TYPE_DIF_BUF:
3583 * This type indicates that protection buffers are
3584 * passed to the driver, so that needs to be prepared
3585 * for DMA
3587 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3588 scsi_prot_sglist(scsi_cmnd),
3589 scsi_prot_sg_count(scsi_cmnd), datadir);
3590 if (unlikely(!protsegcnt)) {
3591 scsi_dma_unmap(scsi_cmnd);
3592 return 1;
3595 lpfc_cmd->prot_seg_cnt = protsegcnt;
3597 * There is a minimun of 3 SGEs used for every
3598 * protection data segment.
3600 if ((lpfc_cmd->prot_seg_cnt * 3) >
3601 (phba->cfg_total_seg_cnt - 2))
3602 goto err;
3604 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3605 datasegcnt, protsegcnt);
3607 /* we should have 3 or more entries in buffer list */
3608 if ((num_sge < 3) ||
3609 (num_sge > phba->cfg_total_seg_cnt))
3610 goto err;
3611 break;
3613 case LPFC_PG_TYPE_INVALID:
3614 default:
3615 scsi_dma_unmap(scsi_cmnd);
3616 lpfc_cmd->seg_cnt = 0;
3618 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3619 "9083 Unexpected protection group %i\n",
3620 prot_group_type);
3621 return 1;
3625 switch (scsi_get_prot_op(scsi_cmnd)) {
3626 case SCSI_PROT_WRITE_STRIP:
3627 case SCSI_PROT_READ_STRIP:
3628 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3629 break;
3630 case SCSI_PROT_WRITE_INSERT:
3631 case SCSI_PROT_READ_INSERT:
3632 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3633 break;
3634 case SCSI_PROT_WRITE_PASS:
3635 case SCSI_PROT_READ_PASS:
3636 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3637 break;
3640 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3641 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3644 * Due to difference in data length between DIF/non-DIF paths,
3645 * we need to set word 4 of IOCB here
3647 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3649 return 0;
3650 err:
3651 if (lpfc_cmd->seg_cnt)
3652 scsi_dma_unmap(scsi_cmnd);
3653 if (lpfc_cmd->prot_seg_cnt)
3654 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3655 scsi_prot_sg_count(scsi_cmnd),
3656 scsi_cmnd->sc_data_direction);
3658 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3659 "9084 Cannot setup S/G List for HBA"
3660 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3661 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3662 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3663 prot_group_type, num_sge);
3665 lpfc_cmd->seg_cnt = 0;
3666 lpfc_cmd->prot_seg_cnt = 0;
3667 return 1;
3671 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3672 * @phba: The Hba for which this call is being executed.
3673 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3675 * This routine wraps the actual DMA mapping function pointer from the
3676 * lpfc_hba struct.
3678 * Return codes:
3679 * 1 - Error
3680 * 0 - Success
3682 static inline int
3683 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3685 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3689 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3690 * using BlockGuard.
3691 * @phba: The Hba for which this call is being executed.
3692 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3694 * This routine wraps the actual DMA mapping function pointer from the
3695 * lpfc_hba struct.
3697 * Return codes:
3698 * 1 - Error
3699 * 0 - Success
3701 static inline int
3702 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3704 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3708 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
3709 * @phba: Pointer to hba context object.
3710 * @vport: Pointer to vport object.
3711 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3712 * @rsp_iocb: Pointer to response iocb object which reported error.
3714 * This function posts an event when there is a SCSI command reporting
3715 * error from the scsi device.
3717 static void
3718 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3719 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3720 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3721 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3722 uint32_t resp_info = fcprsp->rspStatus2;
3723 uint32_t scsi_status = fcprsp->rspStatus3;
3724 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3725 struct lpfc_fast_path_event *fast_path_evt = NULL;
3726 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3727 unsigned long flags;
3729 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3730 return;
3732 /* If there is queuefull or busy condition send a scsi event */
3733 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3734 (cmnd->result == SAM_STAT_BUSY)) {
3735 fast_path_evt = lpfc_alloc_fast_evt(phba);
3736 if (!fast_path_evt)
3737 return;
3738 fast_path_evt->un.scsi_evt.event_type =
3739 FC_REG_SCSI_EVENT;
3740 fast_path_evt->un.scsi_evt.subcategory =
3741 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3742 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3743 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3744 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3745 &pnode->nlp_portname, sizeof(struct lpfc_name));
3746 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3747 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3748 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3749 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3750 fast_path_evt = lpfc_alloc_fast_evt(phba);
3751 if (!fast_path_evt)
3752 return;
3753 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3754 FC_REG_SCSI_EVENT;
3755 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3756 LPFC_EVENT_CHECK_COND;
3757 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3758 cmnd->device->lun;
3759 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3760 &pnode->nlp_portname, sizeof(struct lpfc_name));
3761 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3762 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3763 fast_path_evt->un.check_cond_evt.sense_key =
3764 cmnd->sense_buffer[2] & 0xf;
3765 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3766 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3767 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3768 fcpi_parm &&
3769 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3770 ((scsi_status == SAM_STAT_GOOD) &&
3771 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3773 * If status is good or resid does not match with fcp_param and
3774 * there is valid fcpi_parm, then there is a read_check error
3776 fast_path_evt = lpfc_alloc_fast_evt(phba);
3777 if (!fast_path_evt)
3778 return;
3779 fast_path_evt->un.read_check_error.header.event_type =
3780 FC_REG_FABRIC_EVENT;
3781 fast_path_evt->un.read_check_error.header.subcategory =
3782 LPFC_EVENT_FCPRDCHKERR;
3783 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3784 &pnode->nlp_portname, sizeof(struct lpfc_name));
3785 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3786 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3787 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3788 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3789 fast_path_evt->un.read_check_error.fcpiparam =
3790 fcpi_parm;
3791 } else
3792 return;
3794 fast_path_evt->vport = vport;
3795 spin_lock_irqsave(&phba->hbalock, flags);
3796 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3797 spin_unlock_irqrestore(&phba->hbalock, flags);
3798 lpfc_worker_wake_up(phba);
3799 return;
3803 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3804 * @phba: The HBA for which this call is being executed.
3805 * @psb: The scsi buffer which is going to be un-mapped.
3807 * This routine does DMA un-mapping of scatter gather list of scsi command
3808 * field of @lpfc_cmd for device with SLI-3 interface spec.
3810 static void
3811 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
3814 * There are only two special cases to consider. (1) the scsi command
3815 * requested scatter-gather usage or (2) the scsi command allocated
3816 * a request buffer, but did not request use_sg. There is a third
3817 * case, but it does not require resource deallocation.
3819 if (psb->seg_cnt > 0)
3820 scsi_dma_unmap(psb->pCmd);
3821 if (psb->prot_seg_cnt > 0)
3822 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3823 scsi_prot_sg_count(psb->pCmd),
3824 psb->pCmd->sc_data_direction);
3828 * lpfc_handler_fcp_err - FCP response handler
3829 * @vport: The virtual port for which this call is being executed.
3830 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3831 * @rsp_iocb: The response IOCB which contains FCP error.
3833 * This routine is called to process response IOCB with status field
3834 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3835 * based upon SCSI and FCP error.
3837 static void
3838 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3839 struct lpfc_iocbq *rsp_iocb)
3841 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3842 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3843 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3844 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3845 uint32_t resp_info = fcprsp->rspStatus2;
3846 uint32_t scsi_status = fcprsp->rspStatus3;
3847 uint32_t *lp;
3848 uint32_t host_status = DID_OK;
3849 uint32_t rsplen = 0;
3850 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
3854 * If this is a task management command, there is no
3855 * scsi packet associated with this lpfc_cmd. The driver
3856 * consumes it.
3858 if (fcpcmd->fcpCntl2) {
3859 scsi_status = 0;
3860 goto out;
3863 if (resp_info & RSP_LEN_VALID) {
3864 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3865 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
3866 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3867 "2719 Invalid response length: "
3868 "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
3869 cmnd->device->id,
3870 cmnd->device->lun, cmnd->cmnd[0],
3871 rsplen);
3872 host_status = DID_ERROR;
3873 goto out;
3875 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3876 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3877 "2757 Protocol failure detected during "
3878 "processing of FCP I/O op: "
3879 "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
3880 cmnd->device->id,
3881 cmnd->device->lun, cmnd->cmnd[0],
3882 fcprsp->rspInfo3);
3883 host_status = DID_ERROR;
3884 goto out;
3888 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3889 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3890 if (snslen > SCSI_SENSE_BUFFERSIZE)
3891 snslen = SCSI_SENSE_BUFFERSIZE;
3893 if (resp_info & RSP_LEN_VALID)
3894 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3895 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3897 lp = (uint32_t *)cmnd->sense_buffer;
3899 /* special handling for under run conditions */
3900 if (!scsi_status && (resp_info & RESID_UNDER)) {
3901 /* don't log under runs if fcp set... */
3902 if (vport->cfg_log_verbose & LOG_FCP)
3903 logit = LOG_FCP_ERROR;
3904 /* unless operator says so */
3905 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3906 logit = LOG_FCP_UNDER;
3909 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3910 "9024 FCP command x%x failed: x%x SNS x%x x%x "
3911 "Data: x%x x%x x%x x%x x%x\n",
3912 cmnd->cmnd[0], scsi_status,
3913 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3914 be32_to_cpu(fcprsp->rspResId),
3915 be32_to_cpu(fcprsp->rspSnsLen),
3916 be32_to_cpu(fcprsp->rspRspLen),
3917 fcprsp->rspInfo3);
3919 scsi_set_resid(cmnd, 0);
3920 if (resp_info & RESID_UNDER) {
3921 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
3923 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
3924 "9025 FCP Read Underrun, expected %d, "
3925 "residual %d Data: x%x x%x x%x\n",
3926 be32_to_cpu(fcpcmd->fcpDl),
3927 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3928 cmnd->underflow);
3931 * If there is an under run check if under run reported by
3932 * storage array is same as the under run reported by HBA.
3933 * If this is not same, there is a dropped frame.
3935 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3936 fcpi_parm &&
3937 (scsi_get_resid(cmnd) != fcpi_parm)) {
3938 lpfc_printf_vlog(vport, KERN_WARNING,
3939 LOG_FCP | LOG_FCP_ERROR,
3940 "9026 FCP Read Check Error "
3941 "and Underrun Data: x%x x%x x%x x%x\n",
3942 be32_to_cpu(fcpcmd->fcpDl),
3943 scsi_get_resid(cmnd), fcpi_parm,
3944 cmnd->cmnd[0]);
3945 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3946 host_status = DID_ERROR;
3949 * The cmnd->underflow is the minimum number of bytes that must
3950 * be transferred for this command. Provided a sense condition
3951 * is not present, make sure the actual amount transferred is at
3952 * least the underflow value or fail.
3954 if (!(resp_info & SNS_LEN_VALID) &&
3955 (scsi_status == SAM_STAT_GOOD) &&
3956 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3957 < cmnd->underflow)) {
3958 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3959 "9027 FCP command x%x residual "
3960 "underrun converted to error "
3961 "Data: x%x x%x x%x\n",
3962 cmnd->cmnd[0], scsi_bufflen(cmnd),
3963 scsi_get_resid(cmnd), cmnd->underflow);
3964 host_status = DID_ERROR;
3966 } else if (resp_info & RESID_OVER) {
3967 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3968 "9028 FCP command x%x residual overrun error. "
3969 "Data: x%x x%x\n", cmnd->cmnd[0],
3970 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
3971 host_status = DID_ERROR;
3974 * Check SLI validation that all the transfer was actually done
3975 * (fcpi_parm should be zero). Apply check only to reads.
3977 } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
3978 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
3979 "9029 FCP Read Check Error Data: "
3980 "x%x x%x x%x x%x x%x\n",
3981 be32_to_cpu(fcpcmd->fcpDl),
3982 be32_to_cpu(fcprsp->rspResId),
3983 fcpi_parm, cmnd->cmnd[0], scsi_status);
3984 switch (scsi_status) {
3985 case SAM_STAT_GOOD:
3986 case SAM_STAT_CHECK_CONDITION:
3987 /* Fabric dropped a data frame. Fail any successful
3988 * command in which we detected dropped frames.
3989 * A status of good or some check conditions could
3990 * be considered a successful command.
3992 host_status = DID_ERROR;
3993 break;
3995 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3998 out:
3999 cmnd->result = ScsiResult(host_status, scsi_status);
4000 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
4004 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
4005 * @phba: The Hba for which this call is being executed.
4006 * @pIocbIn: The command IOCBQ for the scsi cmnd.
4007 * @pIocbOut: The response IOCBQ for the scsi cmnd.
4009 * This routine assigns scsi command result by looking into response IOCB
4010 * status field appropriately. This routine handles QUEUE FULL condition as
4011 * well by ramping down device queue depth.
4013 static void
4014 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
4015 struct lpfc_iocbq *pIocbOut)
4017 struct lpfc_scsi_buf *lpfc_cmd =
4018 (struct lpfc_scsi_buf *) pIocbIn->context1;
4019 struct lpfc_vport *vport = pIocbIn->vport;
4020 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4021 struct lpfc_nodelist *pnode = rdata->pnode;
4022 struct scsi_cmnd *cmd;
4023 int result;
4024 struct scsi_device *tmp_sdev;
4025 int depth;
4026 unsigned long flags;
4027 struct lpfc_fast_path_event *fast_path_evt;
4028 struct Scsi_Host *shost;
4029 uint32_t queue_depth, scsi_id;
4030 uint32_t logit = LOG_FCP;
4032 /* Sanity check on return of outstanding command */
4033 if (!(lpfc_cmd->pCmd))
4034 return;
4035 cmd = lpfc_cmd->pCmd;
4036 shost = cmd->device->host;
4038 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
4039 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
4040 /* pick up SLI4 exhange busy status from HBA */
4041 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
4043 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4044 if (lpfc_cmd->prot_data_type) {
4045 struct scsi_dif_tuple *src = NULL;
4047 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
4049 * Used to restore any changes to protection
4050 * data for error injection.
4052 switch (lpfc_cmd->prot_data_type) {
4053 case LPFC_INJERR_REFTAG:
4054 src->ref_tag =
4055 lpfc_cmd->prot_data;
4056 break;
4057 case LPFC_INJERR_APPTAG:
4058 src->app_tag =
4059 (uint16_t)lpfc_cmd->prot_data;
4060 break;
4061 case LPFC_INJERR_GUARD:
4062 src->guard_tag =
4063 (uint16_t)lpfc_cmd->prot_data;
4064 break;
4065 default:
4066 break;
4069 lpfc_cmd->prot_data = 0;
4070 lpfc_cmd->prot_data_type = 0;
4071 lpfc_cmd->prot_data_segment = NULL;
4073 #endif
4074 if (pnode && NLP_CHK_NODE_ACT(pnode))
4075 atomic_dec(&pnode->cmd_pending);
4077 if (lpfc_cmd->status) {
4078 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4079 (lpfc_cmd->result & IOERR_DRVR_MASK))
4080 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4081 else if (lpfc_cmd->status >= IOSTAT_CNT)
4082 lpfc_cmd->status = IOSTAT_DEFAULT;
4083 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4084 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4085 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4086 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
4087 logit = 0;
4088 else
4089 logit = LOG_FCP | LOG_FCP_UNDER;
4090 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4091 "9030 FCP cmd x%x failed <%d/%d> "
4092 "status: x%x result: x%x "
4093 "sid: x%x did: x%x oxid: x%x "
4094 "Data: x%x x%x\n",
4095 cmd->cmnd[0],
4096 cmd->device ? cmd->device->id : 0xffff,
4097 cmd->device ? cmd->device->lun : 0xffff,
4098 lpfc_cmd->status, lpfc_cmd->result,
4099 vport->fc_myDID,
4100 (pnode) ? pnode->nlp_DID : 0,
4101 phba->sli_rev == LPFC_SLI_REV4 ?
4102 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4103 pIocbOut->iocb.ulpContext,
4104 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4106 switch (lpfc_cmd->status) {
4107 case IOSTAT_FCP_RSP_ERROR:
4108 /* Call FCP RSP handler to determine result */
4109 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
4110 break;
4111 case IOSTAT_NPORT_BSY:
4112 case IOSTAT_FABRIC_BSY:
4113 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
4114 fast_path_evt = lpfc_alloc_fast_evt(phba);
4115 if (!fast_path_evt)
4116 break;
4117 fast_path_evt->un.fabric_evt.event_type =
4118 FC_REG_FABRIC_EVENT;
4119 fast_path_evt->un.fabric_evt.subcategory =
4120 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4121 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4122 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4123 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4124 &pnode->nlp_portname,
4125 sizeof(struct lpfc_name));
4126 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4127 &pnode->nlp_nodename,
4128 sizeof(struct lpfc_name));
4130 fast_path_evt->vport = vport;
4131 fast_path_evt->work_evt.evt =
4132 LPFC_EVT_FASTPATH_MGMT_EVT;
4133 spin_lock_irqsave(&phba->hbalock, flags);
4134 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4135 &phba->work_list);
4136 spin_unlock_irqrestore(&phba->hbalock, flags);
4137 lpfc_worker_wake_up(phba);
4138 break;
4139 case IOSTAT_LOCAL_REJECT:
4140 case IOSTAT_REMOTE_STOP:
4141 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4142 lpfc_cmd->result ==
4143 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4144 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4145 lpfc_cmd->result ==
4146 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4147 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
4148 break;
4150 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
4151 lpfc_cmd->result == IOERR_NO_RESOURCES ||
4152 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4153 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
4154 cmd->result = ScsiResult(DID_REQUEUE, 0);
4155 break;
4157 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4158 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4159 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4160 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4162 * This is a response for a BG enabled
4163 * cmd. Parse BG error
4165 lpfc_parse_bg_err(phba, lpfc_cmd,
4166 pIocbOut);
4167 break;
4168 } else {
4169 lpfc_printf_vlog(vport, KERN_WARNING,
4170 LOG_BG,
4171 "9031 non-zero BGSTAT "
4172 "on unprotected cmd\n");
4175 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4176 && (phba->sli_rev == LPFC_SLI_REV4)
4177 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4178 /* This IO was aborted by the target, we don't
4179 * know the rxid and because we did not send the
4180 * ABTS we cannot generate and RRQ.
4182 lpfc_set_rrq_active(phba, pnode,
4183 lpfc_cmd->cur_iocbq.sli4_lxritag,
4184 0, 0);
4186 /* else: fall through */
4187 default:
4188 cmd->result = ScsiResult(DID_ERROR, 0);
4189 break;
4192 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
4193 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4194 cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
4195 SAM_STAT_BUSY);
4196 } else
4197 cmd->result = ScsiResult(DID_OK, 0);
4199 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4200 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4202 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4203 "0710 Iodone <%d/%d> cmd %p, error "
4204 "x%x SNS x%x x%x Data: x%x x%x\n",
4205 cmd->device->id, cmd->device->lun, cmd,
4206 cmd->result, *lp, *(lp + 3), cmd->retries,
4207 scsi_get_resid(cmd));
4210 lpfc_update_stats(phba, lpfc_cmd);
4211 result = cmd->result;
4212 if (vport->cfg_max_scsicmpl_time &&
4213 time_after(jiffies, lpfc_cmd->start_time +
4214 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4215 spin_lock_irqsave(shost->host_lock, flags);
4216 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4217 if (pnode->cmd_qdepth >
4218 atomic_read(&pnode->cmd_pending) &&
4219 (atomic_read(&pnode->cmd_pending) >
4220 LPFC_MIN_TGT_QDEPTH) &&
4221 ((cmd->cmnd[0] == READ_10) ||
4222 (cmd->cmnd[0] == WRITE_10)))
4223 pnode->cmd_qdepth =
4224 atomic_read(&pnode->cmd_pending);
4226 pnode->last_change_time = jiffies;
4228 spin_unlock_irqrestore(shost->host_lock, flags);
4229 } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4230 if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
4231 time_after(jiffies, pnode->last_change_time +
4232 msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
4233 spin_lock_irqsave(shost->host_lock, flags);
4234 depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
4235 / 100;
4236 depth = depth ? depth : 1;
4237 pnode->cmd_qdepth += depth;
4238 if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
4239 pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
4240 pnode->last_change_time = jiffies;
4241 spin_unlock_irqrestore(shost->host_lock, flags);
4245 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4247 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4248 queue_depth = cmd->device->queue_depth;
4249 scsi_id = cmd->device->id;
4250 cmd->scsi_done(cmd);
4252 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4253 spin_lock_irqsave(&phba->hbalock, flags);
4254 lpfc_cmd->pCmd = NULL;
4255 spin_unlock_irqrestore(&phba->hbalock, flags);
4258 * If there is a thread waiting for command completion
4259 * wake up the thread.
4261 spin_lock_irqsave(shost->host_lock, flags);
4262 if (lpfc_cmd->waitq)
4263 wake_up(lpfc_cmd->waitq);
4264 spin_unlock_irqrestore(shost->host_lock, flags);
4265 lpfc_release_scsi_buf(phba, lpfc_cmd);
4266 return;
4269 if (!result)
4270 lpfc_rampup_queue_depth(vport, queue_depth);
4273 * Check for queue full. If the lun is reporting queue full, then
4274 * back off the lun queue depth to prevent target overloads.
4276 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
4277 NLP_CHK_NODE_ACT(pnode)) {
4278 shost_for_each_device(tmp_sdev, shost) {
4279 if (tmp_sdev->id != scsi_id)
4280 continue;
4281 depth = scsi_track_queue_full(tmp_sdev,
4282 tmp_sdev->queue_depth-1);
4283 if (depth <= 0)
4284 continue;
4285 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4286 "0711 detected queue full - lun queue "
4287 "depth adjusted to %d.\n", depth);
4288 lpfc_send_sdev_queuedepth_change_event(phba, vport,
4289 pnode,
4290 tmp_sdev->lun,
4291 depth+1, depth);
4295 spin_lock_irqsave(&phba->hbalock, flags);
4296 lpfc_cmd->pCmd = NULL;
4297 spin_unlock_irqrestore(&phba->hbalock, flags);
4300 * If there is a thread waiting for command completion
4301 * wake up the thread.
4303 spin_lock_irqsave(shost->host_lock, flags);
4304 if (lpfc_cmd->waitq)
4305 wake_up(lpfc_cmd->waitq);
4306 spin_unlock_irqrestore(shost->host_lock, flags);
4308 lpfc_release_scsi_buf(phba, lpfc_cmd);
4312 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4313 * @data: A pointer to the immediate command data portion of the IOCB.
4314 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4316 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4317 * byte swapping the data to big endian format for transmission on the wire.
4319 static void
4320 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4322 int i, j;
4323 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4324 i += sizeof(uint32_t), j++) {
4325 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4330 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
4331 * @vport: The virtual port for which this call is being executed.
4332 * @lpfc_cmd: The scsi command which needs to send.
4333 * @pnode: Pointer to lpfc_nodelist.
4335 * This routine initializes fcp_cmnd and iocb data structure from scsi command
4336 * to transfer for device with SLI3 interface spec.
4338 static void
4339 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
4340 struct lpfc_nodelist *pnode)
4342 struct lpfc_hba *phba = vport->phba;
4343 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4344 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4345 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4346 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4347 int datadir = scsi_cmnd->sc_data_direction;
4348 char tag[2];
4349 uint8_t *ptr;
4350 bool sli4;
4351 uint32_t fcpdl;
4353 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4354 return;
4356 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
4357 /* clear task management bits */
4358 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
4360 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4361 &lpfc_cmd->fcp_cmnd->fcp_lun);
4363 ptr = &fcp_cmnd->fcpCdb[0];
4364 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4365 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4366 ptr += scsi_cmnd->cmd_len;
4367 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4370 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
4371 switch (tag[0]) {
4372 case HEAD_OF_QUEUE_TAG:
4373 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
4374 break;
4375 case ORDERED_QUEUE_TAG:
4376 fcp_cmnd->fcpCntl1 = ORDERED_Q;
4377 break;
4378 default:
4379 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4380 break;
4382 } else
4383 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4385 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4388 * There are three possibilities here - use scatter-gather segment, use
4389 * the single mapping, or neither. Start the lpfc command prep by
4390 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4391 * data bde entry.
4393 if (scsi_sg_count(scsi_cmnd)) {
4394 if (datadir == DMA_TO_DEVICE) {
4395 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
4396 iocb_cmd->ulpPU = PARM_READ_CHECK;
4397 if (vport->cfg_first_burst_size &&
4398 (pnode->nlp_flag & NLP_FIRSTBURST)) {
4399 fcpdl = scsi_bufflen(scsi_cmnd);
4400 if (fcpdl < vport->cfg_first_burst_size)
4401 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4402 else
4403 piocbq->iocb.un.fcpi.fcpi_XRdy =
4404 vport->cfg_first_burst_size;
4406 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4407 phba->fc4OutputRequests++;
4408 } else {
4409 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4410 iocb_cmd->ulpPU = PARM_READ_CHECK;
4411 fcp_cmnd->fcpCntl3 = READ_DATA;
4412 phba->fc4InputRequests++;
4414 } else {
4415 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4416 iocb_cmd->un.fcpi.fcpi_parm = 0;
4417 iocb_cmd->ulpPU = 0;
4418 fcp_cmnd->fcpCntl3 = 0;
4419 phba->fc4ControlRequests++;
4421 if (phba->sli_rev == 3 &&
4422 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4423 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
4425 * Finish initializing those IOCB fields that are independent
4426 * of the scsi_cmnd request_buffer
4428 piocbq->iocb.ulpContext = pnode->nlp_rpi;
4429 if (sli4)
4430 piocbq->iocb.ulpContext =
4431 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
4432 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4433 piocbq->iocb.ulpFCP2Rcvy = 1;
4434 else
4435 piocbq->iocb.ulpFCP2Rcvy = 0;
4437 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4438 piocbq->context1 = lpfc_cmd;
4439 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4440 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
4441 piocbq->vport = vport;
4445 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
4446 * @vport: The virtual port for which this call is being executed.
4447 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4448 * @lun: Logical unit number.
4449 * @task_mgmt_cmd: SCSI task management command.
4451 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4452 * for device with SLI-3 interface spec.
4454 * Return codes:
4455 * 0 - Error
4456 * 1 - Success
4458 static int
4459 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
4460 struct lpfc_scsi_buf *lpfc_cmd,
4461 unsigned int lun,
4462 uint8_t task_mgmt_cmd)
4464 struct lpfc_iocbq *piocbq;
4465 IOCB_t *piocb;
4466 struct fcp_cmnd *fcp_cmnd;
4467 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4468 struct lpfc_nodelist *ndlp = rdata->pnode;
4470 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4471 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
4472 return 0;
4474 piocbq = &(lpfc_cmd->cur_iocbq);
4475 piocbq->vport = vport;
4477 piocb = &piocbq->iocb;
4479 fcp_cmnd = lpfc_cmd->fcp_cmnd;
4480 /* Clear out any old data in the FCP command area */
4481 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4482 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
4483 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
4484 if (vport->phba->sli_rev == 3 &&
4485 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4486 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
4487 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
4488 piocb->ulpContext = ndlp->nlp_rpi;
4489 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4490 piocb->ulpContext =
4491 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4493 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
4494 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4496 /* ulpTimeout is only one byte */
4497 if (lpfc_cmd->timeout > 0xff) {
4499 * Do not timeout the command at the firmware level.
4500 * The driver will provide the timeout mechanism.
4502 piocb->ulpTimeout = 0;
4503 } else
4504 piocb->ulpTimeout = lpfc_cmd->timeout;
4506 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4507 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
4509 return 1;
4513 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
4514 * @phba: The hba struct for which this call is being executed.
4515 * @dev_grp: The HBA PCI-Device group number.
4517 * This routine sets up the SCSI interface API function jump table in @phba
4518 * struct.
4519 * Returns: 0 - success, -ENODEV - failure.
4522 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4525 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4526 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
4528 switch (dev_grp) {
4529 case LPFC_PCI_DEV_LP:
4530 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4531 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
4532 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
4533 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
4534 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
4535 break;
4536 case LPFC_PCI_DEV_OC:
4537 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4538 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
4539 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
4540 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
4541 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
4542 break;
4543 default:
4544 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4545 "1418 Invalid HBA PCI-device group: 0x%x\n",
4546 dev_grp);
4547 return -ENODEV;
4548 break;
4550 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
4551 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4552 return 0;
4556 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
4557 * @phba: The Hba for which this call is being executed.
4558 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4559 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4561 * This routine is IOCB completion routine for device reset and target reset
4562 * routine. This routine release scsi buffer associated with lpfc_cmd.
4564 static void
4565 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4566 struct lpfc_iocbq *cmdiocbq,
4567 struct lpfc_iocbq *rspiocbq)
4569 struct lpfc_scsi_buf *lpfc_cmd =
4570 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4571 if (lpfc_cmd)
4572 lpfc_release_scsi_buf(phba, lpfc_cmd);
4573 return;
4577 * lpfc_info - Info entry point of scsi_host_template data structure
4578 * @host: The scsi host for which this call is being executed.
4580 * This routine provides module information about hba.
4582 * Reutrn code:
4583 * Pointer to char - Success.
4585 const char *
4586 lpfc_info(struct Scsi_Host *host)
4588 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4589 struct lpfc_hba *phba = vport->phba;
4590 int len, link_speed = 0;
4591 static char lpfcinfobuf[384];
4593 memset(lpfcinfobuf,0,384);
4594 if (phba && phba->pcidev){
4595 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4596 len = strlen(lpfcinfobuf);
4597 snprintf(lpfcinfobuf + len,
4598 384-len,
4599 " on PCI bus %02x device %02x irq %d",
4600 phba->pcidev->bus->number,
4601 phba->pcidev->devfn,
4602 phba->pcidev->irq);
4603 len = strlen(lpfcinfobuf);
4604 if (phba->Port[0]) {
4605 snprintf(lpfcinfobuf + len,
4606 384-len,
4607 " port %s",
4608 phba->Port);
4610 len = strlen(lpfcinfobuf);
4611 if (phba->sli_rev <= LPFC_SLI_REV3) {
4612 link_speed = lpfc_sli_port_speed_get(phba);
4613 } else {
4614 if (phba->sli4_hba.link_state.logical_speed)
4615 link_speed =
4616 phba->sli4_hba.link_state.logical_speed;
4617 else
4618 link_speed = phba->sli4_hba.link_state.speed;
4620 if (link_speed != 0)
4621 snprintf(lpfcinfobuf + len, 384-len,
4622 " Logical Link Speed: %d Mbps", link_speed);
4624 return lpfcinfobuf;
4628 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
4629 * @phba: The Hba for which this call is being executed.
4631 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4632 * The default value of cfg_poll_tmo is 10 milliseconds.
4634 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4636 unsigned long poll_tmo_expires =
4637 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4639 if (!list_empty(&phba->sli.ring[LPFC_FCP_RING].txcmplq))
4640 mod_timer(&phba->fcp_poll_timer,
4641 poll_tmo_expires);
4645 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
4646 * @phba: The Hba for which this call is being executed.
4648 * This routine starts the fcp_poll_timer of @phba.
4650 void lpfc_poll_start_timer(struct lpfc_hba * phba)
4652 lpfc_poll_rearm_timer(phba);
4656 * lpfc_poll_timeout - Restart polling timer
4657 * @ptr: Map to lpfc_hba data structure pointer.
4659 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4660 * and FCP Ring interrupt is disable.
4663 void lpfc_poll_timeout(unsigned long ptr)
4665 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
4667 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4668 lpfc_sli_handle_fast_ring_event(phba,
4669 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4671 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4672 lpfc_poll_rearm_timer(phba);
4677 * lpfc_queuecommand - scsi_host_template queuecommand entry point
4678 * @cmnd: Pointer to scsi_cmnd data structure.
4679 * @done: Pointer to done routine.
4681 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4682 * This routine prepares an IOCB from scsi command and provides to firmware.
4683 * The @done callback is invoked after driver finished processing the command.
4685 * Return value :
4686 * 0 - Success
4687 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4689 static int
4690 lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
4692 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4693 struct lpfc_hba *phba = vport->phba;
4694 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
4695 struct lpfc_nodelist *ndlp;
4696 struct lpfc_scsi_buf *lpfc_cmd;
4697 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
4698 int err;
4700 err = fc_remote_port_chkready(rport);
4701 if (err) {
4702 cmnd->result = err;
4703 goto out_fail_command;
4705 ndlp = rdata->pnode;
4707 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
4708 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
4710 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4711 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4712 " op:%02x str=%s without registering for"
4713 " BlockGuard - Rejecting command\n",
4714 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4715 dif_op_str[scsi_get_prot_op(cmnd)]);
4716 goto out_fail_command;
4720 * Catch race where our node has transitioned, but the
4721 * transport is still transitioning.
4723 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4724 goto out_tgt_busy;
4725 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
4726 goto out_tgt_busy;
4728 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
4729 if (lpfc_cmd == NULL) {
4730 lpfc_rampdown_queue_depth(phba);
4732 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4733 "0707 driver's buffer pool is empty, "
4734 "IO busied\n");
4735 goto out_host_busy;
4739 * Store the midlayer's command structure for the completion phase
4740 * and complete the command initialization.
4742 lpfc_cmd->pCmd = cmnd;
4743 lpfc_cmd->rdata = rdata;
4744 lpfc_cmd->timeout = 0;
4745 lpfc_cmd->start_time = jiffies;
4746 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4748 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
4749 if (vport->phba->cfg_enable_bg) {
4750 lpfc_printf_vlog(vport,
4751 KERN_INFO, LOG_SCSI_CMD,
4752 "9033 BLKGRD: rcvd %s cmd:x%x "
4753 "sector x%llx cnt %u pt %x\n",
4754 dif_op_str[scsi_get_prot_op(cmnd)],
4755 cmnd->cmnd[0],
4756 (unsigned long long)scsi_get_lba(cmnd),
4757 blk_rq_sectors(cmnd->request),
4758 (cmnd->cmnd[1]>>5));
4760 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4761 } else {
4762 if (vport->phba->cfg_enable_bg) {
4763 lpfc_printf_vlog(vport,
4764 KERN_INFO, LOG_SCSI_CMD,
4765 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4766 "x%x sector x%llx cnt %u pt %x\n",
4767 cmnd->cmnd[0],
4768 (unsigned long long)scsi_get_lba(cmnd),
4769 blk_rq_sectors(cmnd->request),
4770 (cmnd->cmnd[1]>>5));
4772 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4775 if (err)
4776 goto out_host_busy_free_buf;
4778 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
4780 atomic_inc(&ndlp->cmd_pending);
4781 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4782 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
4783 if (err) {
4784 atomic_dec(&ndlp->cmd_pending);
4785 goto out_host_busy_free_buf;
4787 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4788 lpfc_sli_handle_fast_ring_event(phba,
4789 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4791 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4792 lpfc_poll_rearm_timer(phba);
4795 return 0;
4797 out_host_busy_free_buf:
4798 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4799 lpfc_release_scsi_buf(phba, lpfc_cmd);
4800 out_host_busy:
4801 return SCSI_MLQUEUE_HOST_BUSY;
4803 out_tgt_busy:
4804 return SCSI_MLQUEUE_TARGET_BUSY;
4806 out_fail_command:
4807 cmnd->scsi_done(cmnd);
4808 return 0;
4813 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
4814 * @cmnd: Pointer to scsi_cmnd data structure.
4816 * This routine aborts @cmnd pending in base driver.
4818 * Return code :
4819 * 0x2003 - Error
4820 * 0x2002 - Success
4822 static int
4823 lpfc_abort_handler(struct scsi_cmnd *cmnd)
4825 struct Scsi_Host *shost = cmnd->device->host;
4826 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4827 struct lpfc_hba *phba = vport->phba;
4828 struct lpfc_iocbq *iocb;
4829 struct lpfc_iocbq *abtsiocb;
4830 struct lpfc_scsi_buf *lpfc_cmd;
4831 IOCB_t *cmd, *icmd;
4832 int ret = SUCCESS, status = 0;
4833 unsigned long flags;
4834 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
4836 status = fc_block_scsi_eh(cmnd);
4837 if (status != 0 && status != SUCCESS)
4838 return status;
4840 spin_lock_irqsave(&phba->hbalock, flags);
4841 /* driver queued commands are in process of being flushed */
4842 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
4843 spin_unlock_irqrestore(&phba->hbalock, flags);
4844 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4845 "3168 SCSI Layer abort requested I/O has been "
4846 "flushed by LLD.\n");
4847 return FAILED;
4850 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
4851 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
4852 spin_unlock_irqrestore(&phba->hbalock, flags);
4853 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4854 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
4855 "x%x ID %d LUN %d\n",
4856 SUCCESS, cmnd->device->id, cmnd->device->lun);
4857 return SUCCESS;
4860 iocb = &lpfc_cmd->cur_iocbq;
4861 /* the command is in process of being cancelled */
4862 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4863 spin_unlock_irqrestore(&phba->hbalock, flags);
4864 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4865 "3169 SCSI Layer abort requested I/O has been "
4866 "cancelled by LLD.\n");
4867 return FAILED;
4870 * If pCmd field of the corresponding lpfc_scsi_buf structure
4871 * points to a different SCSI command, then the driver has
4872 * already completed this command, but the midlayer did not
4873 * see the completion before the eh fired. Just return SUCCESS.
4875 if (lpfc_cmd->pCmd != cmnd) {
4876 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4877 "3170 SCSI Layer abort requested I/O has been "
4878 "completed by LLD.\n");
4879 goto out_unlock;
4882 BUG_ON(iocb->context1 != lpfc_cmd);
4884 abtsiocb = __lpfc_sli_get_iocbq(phba);
4885 if (abtsiocb == NULL) {
4886 ret = FAILED;
4887 goto out_unlock;
4890 /* Indicate the IO is being aborted by the driver. */
4891 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4894 * The scsi command can not be in txq and it is in flight because the
4895 * pCmd is still pointig at the SCSI command we have to abort. There
4896 * is no need to search the txcmplq. Just send an abort to the FW.
4899 cmd = &iocb->iocb;
4900 icmd = &abtsiocb->iocb;
4901 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4902 icmd->un.acxri.abortContextTag = cmd->ulpContext;
4903 if (phba->sli_rev == LPFC_SLI_REV4)
4904 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4905 else
4906 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
4908 icmd->ulpLe = 1;
4909 icmd->ulpClass = cmd->ulpClass;
4911 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4912 abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
4913 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
4915 if (lpfc_is_link_up(phba))
4916 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4917 else
4918 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
4920 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
4921 abtsiocb->vport = vport;
4922 /* no longer need the lock after this point */
4923 spin_unlock_irqrestore(&phba->hbalock, flags);
4925 if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
4926 IOCB_ERROR) {
4927 lpfc_sli_release_iocbq(phba, abtsiocb);
4928 ret = FAILED;
4929 goto out;
4932 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4933 lpfc_sli_handle_fast_ring_event(phba,
4934 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
4936 lpfc_cmd->waitq = &waitq;
4937 /* Wait for abort to complete */
4938 wait_event_timeout(waitq,
4939 (lpfc_cmd->pCmd != cmnd),
4940 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
4941 lpfc_cmd->waitq = NULL;
4943 if (lpfc_cmd->pCmd == cmnd) {
4944 ret = FAILED;
4945 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4946 "0748 abort handler timed out waiting "
4947 "for abortng I/O (xri:x%x) to complete: "
4948 "ret %#x, ID %d, LUN %d\n",
4949 iocb->sli4_xritag, ret,
4950 cmnd->device->id, cmnd->device->lun);
4952 goto out;
4954 out_unlock:
4955 spin_unlock_irqrestore(&phba->hbalock, flags);
4956 out:
4957 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4958 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
4959 "LUN %d\n", ret, cmnd->device->id,
4960 cmnd->device->lun);
4961 return ret;
4964 static char *
4965 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4967 switch (task_mgmt_cmd) {
4968 case FCP_ABORT_TASK_SET:
4969 return "ABORT_TASK_SET";
4970 case FCP_CLEAR_TASK_SET:
4971 return "FCP_CLEAR_TASK_SET";
4972 case FCP_BUS_RESET:
4973 return "FCP_BUS_RESET";
4974 case FCP_LUN_RESET:
4975 return "FCP_LUN_RESET";
4976 case FCP_TARGET_RESET:
4977 return "FCP_TARGET_RESET";
4978 case FCP_CLEAR_ACA:
4979 return "FCP_CLEAR_ACA";
4980 case FCP_TERMINATE_TASK:
4981 return "FCP_TERMINATE_TASK";
4982 default:
4983 return "unknown";
4989 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4990 * @vport: The virtual port for which this call is being executed.
4991 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4993 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4995 * Return code :
4996 * 0x2003 - Error
4997 * 0x2002 - Success
4999 static int
5000 lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
5002 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
5003 uint32_t rsp_info;
5004 uint32_t rsp_len;
5005 uint8_t rsp_info_code;
5006 int ret = FAILED;
5009 if (fcprsp == NULL)
5010 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5011 "0703 fcp_rsp is missing\n");
5012 else {
5013 rsp_info = fcprsp->rspStatus2;
5014 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
5015 rsp_info_code = fcprsp->rspInfo3;
5018 lpfc_printf_vlog(vport, KERN_INFO,
5019 LOG_FCP,
5020 "0706 fcp_rsp valid 0x%x,"
5021 " rsp len=%d code 0x%x\n",
5022 rsp_info,
5023 rsp_len, rsp_info_code);
5025 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
5026 switch (rsp_info_code) {
5027 case RSP_NO_FAILURE:
5028 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5029 "0715 Task Mgmt No Failure\n");
5030 ret = SUCCESS;
5031 break;
5032 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
5033 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5034 "0716 Task Mgmt Target "
5035 "reject\n");
5036 break;
5037 case RSP_TM_NOT_COMPLETED: /* TM failed */
5038 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5039 "0717 Task Mgmt Target "
5040 "failed TM\n");
5041 break;
5042 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5043 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5044 "0718 Task Mgmt to invalid "
5045 "LUN\n");
5046 break;
5050 return ret;
5055 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5056 * @vport: The virtual port for which this call is being executed.
5057 * @rdata: Pointer to remote port local data
5058 * @tgt_id: Target ID of remote device.
5059 * @lun_id: Lun number for the TMF
5060 * @task_mgmt_cmd: type of TMF to send
5062 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5063 * a remote port.
5065 * Return Code:
5066 * 0x2003 - Error
5067 * 0x2002 - Success.
5069 static int
5070 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
5071 unsigned tgt_id, unsigned int lun_id,
5072 uint8_t task_mgmt_cmd)
5074 struct lpfc_hba *phba = vport->phba;
5075 struct lpfc_scsi_buf *lpfc_cmd;
5076 struct lpfc_iocbq *iocbq;
5077 struct lpfc_iocbq *iocbqrsp;
5078 struct lpfc_nodelist *pnode = rdata->pnode;
5079 int ret;
5080 int status;
5082 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5083 return FAILED;
5085 lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
5086 if (lpfc_cmd == NULL)
5087 return FAILED;
5088 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
5089 lpfc_cmd->rdata = rdata;
5091 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5092 task_mgmt_cmd);
5093 if (!status) {
5094 lpfc_release_scsi_buf(phba, lpfc_cmd);
5095 return FAILED;
5098 iocbq = &lpfc_cmd->cur_iocbq;
5099 iocbqrsp = lpfc_sli_get_iocbq(phba);
5100 if (iocbqrsp == NULL) {
5101 lpfc_release_scsi_buf(phba, lpfc_cmd);
5102 return FAILED;
5104 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
5106 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5107 "0702 Issue %s to TGT %d LUN %d "
5108 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
5109 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
5110 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5111 iocbq->iocb_flag);
5113 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5114 iocbq, iocbqrsp, lpfc_cmd->timeout);
5115 if ((status != IOCB_SUCCESS) ||
5116 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
5117 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5118 "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
5119 "iocb_flag x%x\n",
5120 lpfc_taskmgmt_name(task_mgmt_cmd),
5121 tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
5122 iocbqrsp->iocb.un.ulpWord[4],
5123 iocbq->iocb_flag);
5124 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5125 if (status == IOCB_SUCCESS) {
5126 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5127 /* Something in the FCP_RSP was invalid.
5128 * Check conditions */
5129 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5130 else
5131 ret = FAILED;
5132 } else if (status == IOCB_TIMEDOUT) {
5133 ret = TIMEOUT_ERROR;
5134 } else {
5135 ret = FAILED;
5137 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
5138 } else
5139 ret = SUCCESS;
5141 lpfc_sli_release_iocbq(phba, iocbqrsp);
5143 if (ret != TIMEOUT_ERROR)
5144 lpfc_release_scsi_buf(phba, lpfc_cmd);
5146 return ret;
5150 * lpfc_chk_tgt_mapped -
5151 * @vport: The virtual port to check on
5152 * @cmnd: Pointer to scsi_cmnd data structure.
5154 * This routine delays until the scsi target (aka rport) for the
5155 * command exists (is present and logged in) or we declare it non-existent.
5157 * Return code :
5158 * 0x2003 - Error
5159 * 0x2002 - Success
5161 static int
5162 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5164 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
5165 struct lpfc_nodelist *pnode;
5166 unsigned long later;
5168 if (!rdata) {
5169 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5170 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5171 return FAILED;
5173 pnode = rdata->pnode;
5175 * If target is not in a MAPPED state, delay until
5176 * target is rediscovered or devloss timeout expires.
5178 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5179 while (time_after(later, jiffies)) {
5180 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5181 return FAILED;
5182 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5183 return SUCCESS;
5184 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5185 rdata = cmnd->device->hostdata;
5186 if (!rdata)
5187 return FAILED;
5188 pnode = rdata->pnode;
5190 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5191 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5192 return FAILED;
5193 return SUCCESS;
5197 * lpfc_reset_flush_io_context -
5198 * @vport: The virtual port (scsi_host) for the flush context
5199 * @tgt_id: If aborting by Target contect - specifies the target id
5200 * @lun_id: If aborting by Lun context - specifies the lun id
5201 * @context: specifies the context level to flush at.
5203 * After a reset condition via TMF, we need to flush orphaned i/o
5204 * contexts from the adapter. This routine aborts any contexts
5205 * outstanding, then waits for their completions. The wait is
5206 * bounded by devloss_tmo though.
5208 * Return code :
5209 * 0x2003 - Error
5210 * 0x2002 - Success
5212 static int
5213 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5214 uint64_t lun_id, lpfc_ctx_cmd context)
5216 struct lpfc_hba *phba = vport->phba;
5217 unsigned long later;
5218 int cnt;
5220 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5221 if (cnt)
5222 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
5223 tgt_id, lun_id, context);
5224 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5225 while (time_after(later, jiffies) && cnt) {
5226 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5227 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5229 if (cnt) {
5230 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5231 "0724 I/O flush failure for context %s : cnt x%x\n",
5232 ((context == LPFC_CTX_LUN) ? "LUN" :
5233 ((context == LPFC_CTX_TGT) ? "TGT" :
5234 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5235 cnt);
5236 return FAILED;
5238 return SUCCESS;
5242 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
5243 * @cmnd: Pointer to scsi_cmnd data structure.
5245 * This routine does a device reset by sending a LUN_RESET task management
5246 * command.
5248 * Return code :
5249 * 0x2003 - Error
5250 * 0x2002 - Success
5252 static int
5253 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
5255 struct Scsi_Host *shost = cmnd->device->host;
5256 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5257 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
5258 struct lpfc_nodelist *pnode;
5259 unsigned tgt_id = cmnd->device->id;
5260 unsigned int lun_id = cmnd->device->lun;
5261 struct lpfc_scsi_event_header scsi_event;
5262 int status;
5264 if (!rdata) {
5265 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5266 "0798 Device Reset rport failure: rdata x%p\n", rdata);
5267 return FAILED;
5269 pnode = rdata->pnode;
5270 status = fc_block_scsi_eh(cmnd);
5271 if (status != 0 && status != SUCCESS)
5272 return status;
5274 status = lpfc_chk_tgt_mapped(vport, cmnd);
5275 if (status == FAILED) {
5276 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5277 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5278 return FAILED;
5281 scsi_event.event_type = FC_REG_SCSI_EVENT;
5282 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5283 scsi_event.lun = lun_id;
5284 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5285 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5287 fc_host_post_vendor_event(shost, fc_get_event_number(),
5288 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5290 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5291 FCP_LUN_RESET);
5293 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5294 "0713 SCSI layer issued Device Reset (%d, %d) "
5295 "return x%x\n", tgt_id, lun_id, status);
5298 * We have to clean up i/o as : they may be orphaned by the TMF;
5299 * or if the TMF failed, they may be in an indeterminate state.
5300 * So, continue on.
5301 * We will report success if all the i/o aborts successfully.
5303 if (status == SUCCESS)
5304 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5305 LPFC_CTX_LUN);
5307 return status;
5311 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5312 * @cmnd: Pointer to scsi_cmnd data structure.
5314 * This routine does a target reset by sending a TARGET_RESET task management
5315 * command.
5317 * Return code :
5318 * 0x2003 - Error
5319 * 0x2002 - Success
5321 static int
5322 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5324 struct Scsi_Host *shost = cmnd->device->host;
5325 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5326 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
5327 struct lpfc_nodelist *pnode;
5328 unsigned tgt_id = cmnd->device->id;
5329 unsigned int lun_id = cmnd->device->lun;
5330 struct lpfc_scsi_event_header scsi_event;
5331 int status;
5333 if (!rdata) {
5334 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5335 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5336 return FAILED;
5338 pnode = rdata->pnode;
5339 status = fc_block_scsi_eh(cmnd);
5340 if (status != 0 && status != SUCCESS)
5341 return status;
5343 status = lpfc_chk_tgt_mapped(vport, cmnd);
5344 if (status == FAILED) {
5345 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5346 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5347 return FAILED;
5350 scsi_event.event_type = FC_REG_SCSI_EVENT;
5351 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5352 scsi_event.lun = 0;
5353 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5354 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5356 fc_host_post_vendor_event(shost, fc_get_event_number(),
5357 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5359 status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
5360 FCP_TARGET_RESET);
5362 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5363 "0723 SCSI layer issued Target Reset (%d, %d) "
5364 "return x%x\n", tgt_id, lun_id, status);
5367 * We have to clean up i/o as : they may be orphaned by the TMF;
5368 * or if the TMF failed, they may be in an indeterminate state.
5369 * So, continue on.
5370 * We will report success if all the i/o aborts successfully.
5372 if (status == SUCCESS)
5373 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5374 LPFC_CTX_TGT);
5375 return status;
5379 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
5380 * @cmnd: Pointer to scsi_cmnd data structure.
5382 * This routine does target reset to all targets on @cmnd->device->host.
5383 * This emulates Parallel SCSI Bus Reset Semantics.
5385 * Return code :
5386 * 0x2003 - Error
5387 * 0x2002 - Success
5389 static int
5390 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
5392 struct Scsi_Host *shost = cmnd->device->host;
5393 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5394 struct lpfc_nodelist *ndlp = NULL;
5395 struct lpfc_scsi_event_header scsi_event;
5396 int match;
5397 int ret = SUCCESS, status, i;
5399 scsi_event.event_type = FC_REG_SCSI_EVENT;
5400 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5401 scsi_event.lun = 0;
5402 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5403 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5405 fc_host_post_vendor_event(shost, fc_get_event_number(),
5406 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5408 status = fc_block_scsi_eh(cmnd);
5409 if (status != 0 && status != SUCCESS)
5410 return status;
5413 * Since the driver manages a single bus device, reset all
5414 * targets known to the driver. Should any target reset
5415 * fail, this routine returns failure to the midlayer.
5417 for (i = 0; i < LPFC_MAX_TARGET; i++) {
5418 /* Search for mapped node by target ID */
5419 match = 0;
5420 spin_lock_irq(shost->host_lock);
5421 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5422 if (!NLP_CHK_NODE_ACT(ndlp))
5423 continue;
5424 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5425 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5426 continue;
5427 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
5428 ndlp->nlp_sid == i &&
5429 ndlp->rport) {
5430 match = 1;
5431 break;
5434 spin_unlock_irq(shost->host_lock);
5435 if (!match)
5436 continue;
5438 status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
5439 i, 0, FCP_TARGET_RESET);
5441 if (status != SUCCESS) {
5442 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5443 "0700 Bus Reset on target %d failed\n",
5445 ret = FAILED;
5449 * We have to clean up i/o as : they may be orphaned by the TMFs
5450 * above; or if any of the TMFs failed, they may be in an
5451 * indeterminate state.
5452 * We will report success if all the i/o aborts successfully.
5455 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5456 if (status != SUCCESS)
5457 ret = FAILED;
5459 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5460 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
5461 return ret;
5465 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5466 * @cmnd: Pointer to scsi_cmnd data structure.
5468 * This routine does host reset to the adaptor port. It brings the HBA
5469 * offline, performs a board restart, and then brings the board back online.
5470 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5471 * reject all outstanding SCSI commands to the host and error returned
5472 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5473 * of error handling, it will only return error if resetting of the adapter
5474 * is not successful; in all other cases, will return success.
5476 * Return code :
5477 * 0x2003 - Error
5478 * 0x2002 - Success
5480 static int
5481 lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5483 struct Scsi_Host *shost = cmnd->device->host;
5484 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5485 struct lpfc_hba *phba = vport->phba;
5486 int rc, ret = SUCCESS;
5488 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5489 "3172 SCSI layer issued Host Reset Data:\n");
5491 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5492 lpfc_offline(phba);
5493 rc = lpfc_sli_brdrestart(phba);
5494 if (rc)
5495 ret = FAILED;
5496 rc = lpfc_online(phba);
5497 if (rc)
5498 ret = FAILED;
5499 lpfc_unblock_mgmt_io(phba);
5501 if (ret == FAILED) {
5502 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5503 "3323 Failed host reset, bring it offline\n");
5504 lpfc_sli4_offline_eratt(phba);
5506 return ret;
5510 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
5511 * @sdev: Pointer to scsi_device.
5513 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5514 * globally available list of scsi buffers. This routine also makes sure scsi
5515 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5516 * of scsi buffer exists for the lifetime of the driver.
5518 * Return codes:
5519 * non-0 - Error
5520 * 0 - Success
5522 static int
5523 lpfc_slave_alloc(struct scsi_device *sdev)
5525 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5526 struct lpfc_hba *phba = vport->phba;
5527 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
5528 uint32_t total = 0;
5529 uint32_t num_to_alloc = 0;
5530 int num_allocated = 0;
5531 uint32_t sdev_cnt;
5533 if (!rport || fc_remote_port_chkready(rport))
5534 return -ENXIO;
5536 sdev->hostdata = rport->dd_data;
5537 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
5540 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5541 * available list of scsi buffers. Don't allocate more than the
5542 * HBA limit conveyed to the midlayer via the host structure. The
5543 * formula accounts for the lun_queue_depth + error handlers + 1
5544 * extra. This list of scsi bufs exists for the lifetime of the driver.
5546 total = phba->total_scsi_bufs;
5547 num_to_alloc = vport->cfg_lun_queue_depth + 2;
5549 /* If allocated buffers are enough do nothing */
5550 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5551 return 0;
5553 /* Allow some exchanges to be available always to complete discovery */
5554 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5555 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5556 "0704 At limitation of %d preallocated "
5557 "command buffers\n", total);
5558 return 0;
5559 /* Allow some exchanges to be available always to complete discovery */
5560 } else if (total + num_to_alloc >
5561 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5562 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5563 "0705 Allocation request of %d "
5564 "command buffers will exceed max of %d. "
5565 "Reducing allocation request to %d.\n",
5566 num_to_alloc, phba->cfg_hba_queue_depth,
5567 (phba->cfg_hba_queue_depth - total));
5568 num_to_alloc = phba->cfg_hba_queue_depth - total;
5570 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5571 if (num_to_alloc != num_allocated) {
5572 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5573 "0708 Allocation request of %d "
5574 "command buffers did not succeed. "
5575 "Allocated %d buffers.\n",
5576 num_to_alloc, num_allocated);
5578 if (num_allocated > 0)
5579 phba->total_scsi_bufs += num_allocated;
5580 return 0;
5584 * lpfc_slave_configure - scsi_host_template slave_configure entry point
5585 * @sdev: Pointer to scsi_device.
5587 * This routine configures following items
5588 * - Tag command queuing support for @sdev if supported.
5589 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5591 * Return codes:
5592 * 0 - Success
5594 static int
5595 lpfc_slave_configure(struct scsi_device *sdev)
5597 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5598 struct lpfc_hba *phba = vport->phba;
5600 if (sdev->tagged_supported)
5601 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
5602 else
5603 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
5605 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5606 lpfc_sli_handle_fast_ring_event(phba,
5607 &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
5608 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5609 lpfc_poll_rearm_timer(phba);
5612 return 0;
5616 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
5617 * @sdev: Pointer to scsi_device.
5619 * This routine sets @sdev hostatdata filed to null.
5621 static void
5622 lpfc_slave_destroy(struct scsi_device *sdev)
5624 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5625 struct lpfc_hba *phba = vport->phba;
5626 atomic_dec(&phba->sdev_cnt);
5627 sdev->hostdata = NULL;
5628 return;
5632 struct scsi_host_template lpfc_template = {
5633 .module = THIS_MODULE,
5634 .name = LPFC_DRIVER_NAME,
5635 .info = lpfc_info,
5636 .queuecommand = lpfc_queuecommand,
5637 .eh_abort_handler = lpfc_abort_handler,
5638 .eh_device_reset_handler = lpfc_device_reset_handler,
5639 .eh_target_reset_handler = lpfc_target_reset_handler,
5640 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5641 .eh_host_reset_handler = lpfc_host_reset_handler,
5642 .slave_alloc = lpfc_slave_alloc,
5643 .slave_configure = lpfc_slave_configure,
5644 .slave_destroy = lpfc_slave_destroy,
5645 .scan_finished = lpfc_scan_finished,
5646 .this_id = -1,
5647 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5648 .cmd_per_lun = LPFC_CMD_PER_LUN,
5649 .use_clustering = ENABLE_CLUSTERING,
5650 .shost_attrs = lpfc_hba_attrs,
5651 .max_sectors = 0xFFFF,
5652 .vendor_id = LPFC_NL_VENDOR_ID,
5653 .change_queue_depth = lpfc_change_queue_depth,
5654 .change_queue_type = lpfc_change_queue_type,
5657 struct scsi_host_template lpfc_vport_template = {
5658 .module = THIS_MODULE,
5659 .name = LPFC_DRIVER_NAME,
5660 .info = lpfc_info,
5661 .queuecommand = lpfc_queuecommand,
5662 .eh_abort_handler = lpfc_abort_handler,
5663 .eh_device_reset_handler = lpfc_device_reset_handler,
5664 .eh_target_reset_handler = lpfc_target_reset_handler,
5665 .eh_bus_reset_handler = lpfc_bus_reset_handler,
5666 .slave_alloc = lpfc_slave_alloc,
5667 .slave_configure = lpfc_slave_configure,
5668 .slave_destroy = lpfc_slave_destroy,
5669 .scan_finished = lpfc_scan_finished,
5670 .this_id = -1,
5671 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
5672 .cmd_per_lun = LPFC_CMD_PER_LUN,
5673 .use_clustering = ENABLE_CLUSTERING,
5674 .shost_attrs = lpfc_vport_attrs,
5675 .max_sectors = 0xFFFF,
5676 .change_queue_depth = lpfc_change_queue_depth,
5677 .change_queue_type = lpfc_change_queue_type,