1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
5 * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
6 * Copyright (C) 2004-2016 Emulex. All rights reserved. *
7 * EMULEX and SLI are trademarks of Emulex. *
9 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
11 * This program is free software; you can redistribute it and/or *
12 * modify it under the terms of version 2 of the GNU General *
13 * Public License as published by the Free Software Foundation. *
14 * This program is distributed in the hope that it will be useful. *
15 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
16 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
17 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
18 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
19 * TO BE LEGALLY INVALID. See the GNU General Public License for *
20 * more details, a copy of which can be found in the file COPYING *
21 * included with this package. *
22 *******************************************************************/
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/interrupt.h>
26 #include <linux/export.h>
27 #include <linux/delay.h>
28 #include <asm/unaligned.h>
29 #include <linux/t10-pi.h>
30 #include <linux/crc-t10dif.h>
31 #include <net/checksum.h>
33 #include <scsi/scsi.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_eh.h>
36 #include <scsi/scsi_host.h>
37 #include <scsi/scsi_tcq.h>
38 #include <scsi/scsi_transport_fc.h>
40 #include "lpfc_version.h"
44 #include "lpfc_sli4.h"
46 #include "lpfc_disc.h"
48 #include "lpfc_scsi.h"
49 #include "lpfc_logmsg.h"
50 #include "lpfc_crtn.h"
51 #include "lpfc_vport.h"
53 #define LPFC_RESET_WAIT 2
54 #define LPFC_ABORT_WAIT 2
56 int _dump_buf_done = 1;
58 static char *dif_op_str[] = {
68 struct scsi_dif_tuple {
69 __be16 guard_tag; /* Checksum */
70 __be16 app_tag; /* Opaque storage */
71 __be32 ref_tag; /* Target LBA or indirect LBA */
74 static struct lpfc_rport_data *
75 lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
77 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
79 if (vport->phba->cfg_fof)
80 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
82 return (struct lpfc_rport_data *)sdev->hostdata;
86 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
88 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
90 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
93 lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
96 struct scatterlist *sgde = scsi_sglist(cmnd);
98 if (!_dump_buf_data) {
99 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
100 "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
107 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
108 "9051 BLKGRD: ERROR: data scatterlist is null\n");
112 dst = (void *) _dump_buf_data;
115 memcpy(dst, src, sgde->length);
117 sgde = sg_next(sgde);
122 lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
125 struct scatterlist *sgde = scsi_prot_sglist(cmnd);
127 if (!_dump_buf_dif) {
128 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
129 "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
135 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
136 "9053 BLKGRD: ERROR: prot scatterlist is null\n");
143 memcpy(dst, src, sgde->length);
145 sgde = sg_next(sgde);
149 static inline unsigned
150 lpfc_cmd_blksize(struct scsi_cmnd *sc)
152 return sc->device->sector_size;
155 #define LPFC_CHECK_PROTECT_GUARD 1
156 #define LPFC_CHECK_PROTECT_REF 2
157 static inline unsigned
158 lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
163 static inline unsigned
164 lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
166 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
168 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
174 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
175 * @phba: Pointer to HBA object.
176 * @lpfc_cmd: lpfc scsi command object pointer.
178 * This function is called from the lpfc_prep_task_mgmt_cmd function to
179 * set the last bit in the response sge entry.
182 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
183 struct lpfc_scsi_buf *lpfc_cmd)
185 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
188 sgl->word2 = le32_to_cpu(sgl->word2);
189 bf_set(lpfc_sli4_sge_last, sgl, 1);
190 sgl->word2 = cpu_to_le32(sgl->word2);
195 * lpfc_update_stats - Update statistical data for the command completion
196 * @phba: Pointer to HBA object.
197 * @lpfc_cmd: lpfc scsi command object pointer.
199 * This function is called when there is a command completion and this
200 * function updates the statistical data for the command completion.
203 lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
205 struct lpfc_rport_data *rdata;
206 struct lpfc_nodelist *pnode;
207 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
209 struct Scsi_Host *shost = cmd->device->host;
210 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
211 unsigned long latency;
214 if (!vport->stat_data_enabled ||
215 vport->stat_data_blocked ||
219 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
220 rdata = lpfc_cmd->rdata;
221 pnode = rdata->pnode;
223 spin_lock_irqsave(shost->host_lock, flags);
226 (phba->bucket_type == LPFC_NO_BUCKET)) {
227 spin_unlock_irqrestore(shost->host_lock, flags);
231 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
232 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
234 /* check array subscript bounds */
237 else if (i >= LPFC_MAX_BUCKET_COUNT)
238 i = LPFC_MAX_BUCKET_COUNT - 1;
240 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
241 if (latency <= (phba->bucket_base +
242 ((1<<i)*phba->bucket_step)))
246 pnode->lat_data[i].cmd_count++;
247 spin_unlock_irqrestore(shost->host_lock, flags);
251 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
252 * @phba: The Hba for which this call is being executed.
254 * This routine is called when there is resource error in driver or firmware.
255 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
256 * posts at most 1 event each second. This routine wakes up worker thread of
257 * @phba to process WORKER_RAM_DOWN_EVENT event.
259 * This routine should be called with no lock held.
262 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
266 unsigned long expires;
268 spin_lock_irqsave(&phba->hbalock, flags);
269 atomic_inc(&phba->num_rsrc_err);
270 phba->last_rsrc_error_time = jiffies;
272 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
273 if (time_after(expires, jiffies)) {
274 spin_unlock_irqrestore(&phba->hbalock, flags);
278 phba->last_ramp_down_time = jiffies;
280 spin_unlock_irqrestore(&phba->hbalock, flags);
282 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
283 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
285 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
286 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
289 lpfc_worker_wake_up(phba);
294 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
295 * @phba: The Hba for which this call is being executed.
297 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
298 * thread.This routine reduces queue depth for all scsi device on each vport
299 * associated with @phba.
302 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
304 struct lpfc_vport **vports;
305 struct Scsi_Host *shost;
306 struct scsi_device *sdev;
307 unsigned long new_queue_depth;
308 unsigned long num_rsrc_err, num_cmd_success;
311 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
312 num_cmd_success = atomic_read(&phba->num_cmd_success);
315 * The error and success command counters are global per
316 * driver instance. If another handler has already
317 * operated on this error event, just exit.
319 if (num_rsrc_err == 0)
322 vports = lpfc_create_vport_work_array(phba);
324 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
325 shost = lpfc_shost_from_vport(vports[i]);
326 shost_for_each_device(sdev, shost) {
328 sdev->queue_depth * num_rsrc_err /
329 (num_rsrc_err + num_cmd_success);
330 if (!new_queue_depth)
331 new_queue_depth = sdev->queue_depth - 1;
333 new_queue_depth = sdev->queue_depth -
335 scsi_change_queue_depth(sdev, new_queue_depth);
338 lpfc_destroy_vport_work_array(phba, vports);
339 atomic_set(&phba->num_rsrc_err, 0);
340 atomic_set(&phba->num_cmd_success, 0);
344 * lpfc_scsi_dev_block - set all scsi hosts to block state
345 * @phba: Pointer to HBA context object.
347 * This function walks vport list and set each SCSI host to block state
348 * by invoking fc_remote_port_delete() routine. This function is invoked
349 * with EEH when device's PCI slot has been permanently disabled.
352 lpfc_scsi_dev_block(struct lpfc_hba *phba)
354 struct lpfc_vport **vports;
355 struct Scsi_Host *shost;
356 struct scsi_device *sdev;
357 struct fc_rport *rport;
360 vports = lpfc_create_vport_work_array(phba);
362 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
363 shost = lpfc_shost_from_vport(vports[i]);
364 shost_for_each_device(sdev, shost) {
365 rport = starget_to_rport(scsi_target(sdev));
366 fc_remote_port_delete(rport);
369 lpfc_destroy_vport_work_array(phba, vports);
373 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
374 * @vport: The virtual port for which this call being executed.
375 * @num_to_allocate: The requested number of buffers to allocate.
377 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
378 * the scsi buffer contains all the necessary information needed to initiate
379 * a SCSI I/O. The non-DMAable buffer region contains information to build
380 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
381 * and the initial BPL. In addition to allocating memory, the FCP CMND and
382 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
385 * int - number of scsi buffers that were allocated.
386 * 0 = failure, less than num_to_alloc is a partial failure.
389 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
391 struct lpfc_hba *phba = vport->phba;
392 struct lpfc_scsi_buf *psb;
393 struct ulp_bde64 *bpl;
395 dma_addr_t pdma_phys_fcp_cmd;
396 dma_addr_t pdma_phys_fcp_rsp;
397 dma_addr_t pdma_phys_bpl;
401 bpl_size = phba->cfg_sg_dma_buf_size -
402 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
404 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
405 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
406 num_to_alloc, phba->cfg_sg_dma_buf_size,
407 (int)sizeof(struct fcp_cmnd),
408 (int)sizeof(struct fcp_rsp), bpl_size);
410 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
411 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
416 * Get memory from the pci pool to map the virt space to pci
417 * bus space for an I/O. The DMA buffer includes space for the
418 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
419 * necessary to support the sg_tablesize.
421 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
422 GFP_KERNEL, &psb->dma_handle);
429 /* Allocate iotag for psb->cur_iocbq. */
430 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
432 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
433 psb->data, psb->dma_handle);
437 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
439 psb->fcp_cmnd = psb->data;
440 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
441 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
442 sizeof(struct fcp_rsp);
444 /* Initialize local short-hand pointers. */
446 pdma_phys_fcp_cmd = psb->dma_handle;
447 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
448 pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
449 sizeof(struct fcp_rsp);
452 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
453 * are sg list bdes. Initialize the first two and leave the
454 * rest for queuecommand.
456 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
457 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
458 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
459 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
460 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
462 /* Setup the physical region for the FCP RSP */
463 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
464 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
465 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
466 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
467 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
470 * Since the IOCB for the FCP I/O is built into this
471 * lpfc_scsi_buf, initialize it with all known data now.
473 iocb = &psb->cur_iocbq.iocb;
474 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
475 if ((phba->sli_rev == 3) &&
476 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
477 /* fill in immediate fcp command BDE */
478 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
479 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
480 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
482 iocb->un.fcpi64.bdl.addrHigh = 0;
483 iocb->ulpBdeCount = 0;
485 /* fill in response BDE */
486 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
488 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
489 sizeof(struct fcp_rsp);
490 iocb->unsli3.fcp_ext.rbde.addrLow =
491 putPaddrLow(pdma_phys_fcp_rsp);
492 iocb->unsli3.fcp_ext.rbde.addrHigh =
493 putPaddrHigh(pdma_phys_fcp_rsp);
495 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
496 iocb->un.fcpi64.bdl.bdeSize =
497 (2 * sizeof(struct ulp_bde64));
498 iocb->un.fcpi64.bdl.addrLow =
499 putPaddrLow(pdma_phys_bpl);
500 iocb->un.fcpi64.bdl.addrHigh =
501 putPaddrHigh(pdma_phys_bpl);
502 iocb->ulpBdeCount = 1;
505 iocb->ulpClass = CLASS3;
506 psb->status = IOSTAT_SUCCESS;
507 /* Put it back into the SCSI buffer list */
508 psb->cur_iocbq.context1 = psb;
509 lpfc_release_scsi_buf_s3(phba, psb);
517 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
518 * @vport: pointer to lpfc vport data structure.
520 * This routine is invoked by the vport cleanup for deletions and the cleanup
521 * for an ndlp on removal.
524 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
526 struct lpfc_hba *phba = vport->phba;
527 struct lpfc_scsi_buf *psb, *next_psb;
528 unsigned long iflag = 0;
530 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
532 spin_lock_irqsave(&phba->hbalock, iflag);
533 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
534 list_for_each_entry_safe(psb, next_psb,
535 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
536 if (psb->rdata && psb->rdata->pnode
537 && psb->rdata->pnode->vport == vport)
540 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
541 spin_unlock_irqrestore(&phba->hbalock, iflag);
545 * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
546 * @phba: pointer to lpfc hba data structure.
547 * @axri: pointer to the fcp xri abort wcqe structure.
549 * This routine is invoked by the worker thread to process a SLI4 fast-path
553 lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
554 struct sli4_wcqe_xri_aborted *axri)
556 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
557 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
558 struct lpfc_scsi_buf *psb, *next_psb;
559 unsigned long iflag = 0;
560 struct lpfc_iocbq *iocbq;
562 struct lpfc_nodelist *ndlp;
564 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
566 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
568 spin_lock_irqsave(&phba->hbalock, iflag);
569 spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
570 list_for_each_entry_safe(psb, next_psb,
571 &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
572 if (psb->cur_iocbq.sli4_xritag == xri) {
573 list_del(&psb->list);
575 psb->status = IOSTAT_SUCCESS;
577 &phba->sli4_hba.abts_scsi_buf_list_lock);
578 if (psb->rdata && psb->rdata->pnode)
579 ndlp = psb->rdata->pnode;
583 rrq_empty = list_empty(&phba->active_rrq_list);
584 spin_unlock_irqrestore(&phba->hbalock, iflag);
586 lpfc_set_rrq_active(phba, ndlp,
587 psb->cur_iocbq.sli4_lxritag, rxid, 1);
588 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
590 lpfc_release_scsi_buf_s4(phba, psb);
592 lpfc_worker_wake_up(phba);
596 spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
597 for (i = 1; i <= phba->sli.last_iotag; i++) {
598 iocbq = phba->sli.iocbq_lookup[i];
600 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
601 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
603 if (iocbq->sli4_xritag != xri)
605 psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
607 spin_unlock_irqrestore(&phba->hbalock, iflag);
608 if (!list_empty(&pring->txq))
609 lpfc_worker_wake_up(phba);
613 spin_unlock_irqrestore(&phba->hbalock, iflag);
617 * lpfc_sli4_post_scsi_sgl_list - Post blocks of scsi buffer sgls from a list
618 * @phba: pointer to lpfc hba data structure.
619 * @post_sblist: pointer to the scsi buffer list.
621 * This routine walks a list of scsi buffers that was passed in. It attempts
622 * to construct blocks of scsi buffer sgls which contains contiguous xris and
623 * uses the non-embedded SGL block post mailbox commands to post to the port.
624 * For single SCSI buffer sgl with non-contiguous xri, if any, it shall use
625 * embedded SGL post mailbox command for posting. The @post_sblist passed in
626 * must be local list, thus no lock is needed when manipulate the list.
628 * Returns: 0 = failure, non-zero number of successfully posted buffers.
631 lpfc_sli4_post_scsi_sgl_list(struct lpfc_hba *phba,
632 struct list_head *post_sblist, int sb_count)
634 struct lpfc_scsi_buf *psb, *psb_next;
635 int status, sgl_size;
636 int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
637 dma_addr_t pdma_phys_bpl1;
638 int last_xritag = NO_XRI;
639 LIST_HEAD(prep_sblist);
640 LIST_HEAD(blck_sblist);
641 LIST_HEAD(scsi_sblist);
647 sgl_size = phba->cfg_sg_dma_buf_size -
648 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
650 list_for_each_entry_safe(psb, psb_next, post_sblist, list) {
651 list_del_init(&psb->list);
653 if ((last_xritag != NO_XRI) &&
654 (psb->cur_iocbq.sli4_xritag != last_xritag + 1)) {
655 /* a hole in xri block, form a sgl posting block */
656 list_splice_init(&prep_sblist, &blck_sblist);
657 post_cnt = block_cnt - 1;
658 /* prepare list for next posting block */
659 list_add_tail(&psb->list, &prep_sblist);
662 /* prepare list for next posting block */
663 list_add_tail(&psb->list, &prep_sblist);
664 /* enough sgls for non-embed sgl mbox command */
665 if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
666 list_splice_init(&prep_sblist, &blck_sblist);
667 post_cnt = block_cnt;
672 last_xritag = psb->cur_iocbq.sli4_xritag;
674 /* end of repost sgl list condition for SCSI buffers */
675 if (num_posting == sb_count) {
677 /* last sgl posting block */
678 list_splice_init(&prep_sblist, &blck_sblist);
679 post_cnt = block_cnt;
680 } else if (block_cnt == 1) {
681 /* last single sgl with non-contiguous xri */
682 if (sgl_size > SGL_PAGE_SIZE)
683 pdma_phys_bpl1 = psb->dma_phys_bpl +
687 status = lpfc_sli4_post_sgl(phba,
690 psb->cur_iocbq.sli4_xritag);
692 /* failure, put on abort scsi list */
695 /* success, put on SCSI buffer list */
697 psb->status = IOSTAT_SUCCESS;
700 /* success, put on SCSI buffer sgl list */
701 list_add_tail(&psb->list, &scsi_sblist);
705 /* continue until a nembed page worth of sgls */
709 /* post block of SCSI buffer list sgls */
710 status = lpfc_sli4_post_scsi_sgl_block(phba, &blck_sblist,
713 /* don't reset xirtag due to hole in xri block */
715 last_xritag = NO_XRI;
717 /* reset SCSI buffer post count for next round of posting */
720 /* put posted SCSI buffer-sgl posted on SCSI buffer sgl list */
721 while (!list_empty(&blck_sblist)) {
722 list_remove_head(&blck_sblist, psb,
723 struct lpfc_scsi_buf, list);
725 /* failure, put on abort scsi list */
728 /* success, put on SCSI buffer list */
730 psb->status = IOSTAT_SUCCESS;
733 list_add_tail(&psb->list, &scsi_sblist);
736 /* Push SCSI buffers with sgl posted to the availble list */
737 while (!list_empty(&scsi_sblist)) {
738 list_remove_head(&scsi_sblist, psb,
739 struct lpfc_scsi_buf, list);
740 lpfc_release_scsi_buf_s4(phba, psb);
746 * lpfc_sli4_repost_scsi_sgl_list - Repost all the allocated scsi buffer sgls
747 * @phba: pointer to lpfc hba data structure.
749 * This routine walks the list of scsi buffers that have been allocated and
750 * repost them to the port by using SGL block post. This is needed after a
751 * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
752 * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
753 * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
755 * Returns: 0 = success, non-zero failure.
758 lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
760 LIST_HEAD(post_sblist);
761 int num_posted, rc = 0;
763 /* get all SCSI buffers need to repost to a local list */
764 spin_lock_irq(&phba->scsi_buf_list_get_lock);
765 spin_lock(&phba->scsi_buf_list_put_lock);
766 list_splice_init(&phba->lpfc_scsi_buf_list_get, &post_sblist);
767 list_splice(&phba->lpfc_scsi_buf_list_put, &post_sblist);
768 spin_unlock(&phba->scsi_buf_list_put_lock);
769 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
771 /* post the list of scsi buffer sgls to port if available */
772 if (!list_empty(&post_sblist)) {
773 num_posted = lpfc_sli4_post_scsi_sgl_list(phba, &post_sblist,
774 phba->sli4_hba.scsi_xri_cnt);
775 /* failed to post any scsi buffer, return error */
783 * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
784 * @vport: The virtual port for which this call being executed.
785 * @num_to_allocate: The requested number of buffers to allocate.
787 * This routine allocates scsi buffers for device with SLI-4 interface spec,
788 * the scsi buffer contains all the necessary information needed to initiate
789 * a SCSI I/O. After allocating up to @num_to_allocate SCSI buffers and put
790 * them on a list, it post them to the port by using SGL block post.
793 * int - number of scsi buffers that were allocated and posted.
794 * 0 = failure, less than num_to_alloc is a partial failure.
797 lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
799 struct lpfc_hba *phba = vport->phba;
800 struct lpfc_scsi_buf *psb;
801 struct sli4_sge *sgl;
803 dma_addr_t pdma_phys_fcp_cmd;
804 dma_addr_t pdma_phys_fcp_rsp;
805 dma_addr_t pdma_phys_bpl;
806 uint16_t iotag, lxri = 0;
807 int bcnt, num_posted, sgl_size;
808 LIST_HEAD(prep_sblist);
809 LIST_HEAD(post_sblist);
810 LIST_HEAD(scsi_sblist);
812 sgl_size = phba->cfg_sg_dma_buf_size -
813 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
815 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
816 "9068 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
817 num_to_alloc, phba->cfg_sg_dma_buf_size, sgl_size,
818 (int)sizeof(struct fcp_cmnd),
819 (int)sizeof(struct fcp_rsp));
821 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
822 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
826 * Get memory from the pci pool to map the virt space to
827 * pci bus space for an I/O. The DMA buffer includes space
828 * for the struct fcp_cmnd, struct fcp_rsp and the number
829 * of bde's necessary to support the sg_tablesize.
831 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
832 GFP_KERNEL, &psb->dma_handle);
839 * 4K Page alignment is CRITICAL to BlockGuard, double check
842 if ((phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
843 (((unsigned long)(psb->data) &
844 (unsigned long)(SLI4_PAGE_SIZE - 1)) != 0)) {
845 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
846 "3369 Memory alignment error "
848 (unsigned long)psb->data);
849 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
850 psb->data, psb->dma_handle);
856 lxri = lpfc_sli4_next_xritag(phba);
857 if (lxri == NO_XRI) {
858 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
859 psb->data, psb->dma_handle);
864 /* Allocate iotag for psb->cur_iocbq. */
865 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
867 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
868 psb->data, psb->dma_handle);
870 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
871 "3368 Failed to allocate IOTAG for"
872 " XRI:0x%x\n", lxri);
873 lpfc_sli4_free_xri(phba, lxri);
876 psb->cur_iocbq.sli4_lxritag = lxri;
877 psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
878 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
879 psb->fcp_bpl = psb->data;
880 psb->fcp_cmnd = (psb->data + sgl_size);
881 psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
882 sizeof(struct fcp_cmnd));
884 /* Initialize local short-hand pointers. */
885 sgl = (struct sli4_sge *)psb->fcp_bpl;
886 pdma_phys_bpl = psb->dma_handle;
887 pdma_phys_fcp_cmd = (psb->dma_handle + sgl_size);
888 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
891 * The first two bdes are the FCP_CMD and FCP_RSP.
892 * The balance are sg list bdes. Initialize the
893 * first two and leave the rest for queuecommand.
895 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
896 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
897 sgl->word2 = le32_to_cpu(sgl->word2);
898 bf_set(lpfc_sli4_sge_last, sgl, 0);
899 sgl->word2 = cpu_to_le32(sgl->word2);
900 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
903 /* Setup the physical region for the FCP RSP */
904 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
905 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
906 sgl->word2 = le32_to_cpu(sgl->word2);
907 bf_set(lpfc_sli4_sge_last, sgl, 1);
908 sgl->word2 = cpu_to_le32(sgl->word2);
909 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
912 * Since the IOCB for the FCP I/O is built into this
913 * lpfc_scsi_buf, initialize it with all known data now.
915 iocb = &psb->cur_iocbq.iocb;
916 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
917 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
918 /* setting the BLP size to 2 * sizeof BDE may not be correct.
919 * We are setting the bpl to point to out sgl. An sgl's
920 * entries are 16 bytes, a bpl entries are 12 bytes.
922 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
923 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
924 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
925 iocb->ulpBdeCount = 1;
927 iocb->ulpClass = CLASS3;
928 psb->cur_iocbq.context1 = psb;
929 psb->dma_phys_bpl = pdma_phys_bpl;
931 /* add the scsi buffer to a post list */
932 list_add_tail(&psb->list, &post_sblist);
933 spin_lock_irq(&phba->scsi_buf_list_get_lock);
934 phba->sli4_hba.scsi_xri_cnt++;
935 spin_unlock_irq(&phba->scsi_buf_list_get_lock);
937 lpfc_printf_log(phba, KERN_INFO, LOG_BG | LOG_FCP,
938 "3021 Allocate %d out of %d requested new SCSI "
939 "buffers\n", bcnt, num_to_alloc);
941 /* post the list of scsi buffer sgls to port if available */
942 if (!list_empty(&post_sblist))
943 num_posted = lpfc_sli4_post_scsi_sgl_list(phba,
952 * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
953 * @vport: The virtual port for which this call being executed.
954 * @num_to_allocate: The requested number of buffers to allocate.
956 * This routine wraps the actual SCSI buffer allocator function pointer from
957 * the lpfc_hba struct.
960 * int - number of scsi buffers that were allocated.
961 * 0 = failure, less than num_to_alloc is a partial failure.
964 lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
966 return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
970 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
971 * @phba: The HBA for which this call is being executed.
973 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
974 * and returns to caller.
978 * Pointer to lpfc_scsi_buf - Success
980 static struct lpfc_scsi_buf*
981 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
983 struct lpfc_scsi_buf * lpfc_cmd = NULL;
984 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
985 unsigned long iflag = 0;
987 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
988 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_scsi_buf,
991 spin_lock(&phba->scsi_buf_list_put_lock);
992 list_splice(&phba->lpfc_scsi_buf_list_put,
993 &phba->lpfc_scsi_buf_list_get);
994 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
995 list_remove_head(scsi_buf_list_get, lpfc_cmd,
996 struct lpfc_scsi_buf, list);
997 spin_unlock(&phba->scsi_buf_list_put_lock);
999 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
1001 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
1002 atomic_inc(&ndlp->cmd_pending);
1003 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
1008 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1009 * @phba: The HBA for which this call is being executed.
1011 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1012 * and returns to caller.
1016 * Pointer to lpfc_scsi_buf - Success
1018 static struct lpfc_scsi_buf*
1019 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1021 struct lpfc_scsi_buf *lpfc_cmd, *lpfc_cmd_next;
1022 unsigned long iflag = 0;
1025 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
1026 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1027 &phba->lpfc_scsi_buf_list_get, list) {
1028 if (lpfc_test_rrq_active(phba, ndlp,
1029 lpfc_cmd->cur_iocbq.sli4_lxritag))
1031 list_del_init(&lpfc_cmd->list);
1036 spin_lock(&phba->scsi_buf_list_put_lock);
1037 list_splice(&phba->lpfc_scsi_buf_list_put,
1038 &phba->lpfc_scsi_buf_list_get);
1039 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
1040 spin_unlock(&phba->scsi_buf_list_put_lock);
1041 list_for_each_entry_safe(lpfc_cmd, lpfc_cmd_next,
1042 &phba->lpfc_scsi_buf_list_get, list) {
1043 if (lpfc_test_rrq_active(
1044 phba, ndlp, lpfc_cmd->cur_iocbq.sli4_lxritag))
1046 list_del_init(&lpfc_cmd->list);
1051 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
1055 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
1056 atomic_inc(&ndlp->cmd_pending);
1057 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
1062 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
1063 * @phba: The HBA for which this call is being executed.
1065 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
1066 * and returns to caller.
1070 * Pointer to lpfc_scsi_buf - Success
1072 static struct lpfc_scsi_buf*
1073 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
1075 return phba->lpfc_get_scsi_buf(phba, ndlp);
1079 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
1080 * @phba: The Hba for which this call is being executed.
1081 * @psb: The scsi buffer which is being released.
1083 * This routine releases @psb scsi buffer by adding it to tail of @phba
1084 * lpfc_scsi_buf_list list.
1087 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1089 unsigned long iflag = 0;
1092 psb->nonsg_phys = 0;
1093 psb->prot_seg_cnt = 0;
1095 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1097 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
1098 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1099 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1103 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
1104 * @phba: The Hba for which this call is being executed.
1105 * @psb: The scsi buffer which is being released.
1107 * This routine releases @psb scsi buffer by adding it to tail of @phba
1108 * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
1109 * and cannot be reused for at least RA_TOV amount of time if it was
1113 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1115 unsigned long iflag = 0;
1118 psb->nonsg_phys = 0;
1119 psb->prot_seg_cnt = 0;
1121 if (psb->exch_busy) {
1122 spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
1125 list_add_tail(&psb->list,
1126 &phba->sli4_hba.lpfc_abts_scsi_buf_list);
1127 spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
1131 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
1132 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
1133 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
1134 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
1139 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
1140 * @phba: The Hba for which this call is being executed.
1141 * @psb: The scsi buffer which is being released.
1143 * This routine releases @psb scsi buffer by adding it to tail of @phba
1144 * lpfc_scsi_buf_list list.
1147 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
1149 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
1150 atomic_dec(&psb->ndlp->cmd_pending);
1152 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
1153 phba->lpfc_release_scsi_buf(phba, psb);
1157 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
1158 * @phba: The Hba for which this call is being executed.
1159 * @lpfc_cmd: The scsi buffer which is going to be mapped.
1161 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
1162 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
1163 * through sg elements and format the bde. This routine also initializes all
1164 * IOCB fields which are dependent on scsi command request buffer.
1171 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
1173 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
1174 struct scatterlist *sgel = NULL;
1175 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
1176 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
1177 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
1178 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
1179 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
1180 dma_addr_t physaddr;
1181 uint32_t num_bde = 0;
1182 int nseg, datadir = scsi_cmnd->sc_data_direction;
1185 * There are three possibilities here - use scatter-gather segment, use
1186 * the single mapping, or neither. Start the lpfc command prep by
1187 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
1191 if (scsi_sg_count(scsi_cmnd)) {
1193 * The driver stores the segment count returned from pci_map_sg
1194 * because this a count of dma-mappings used to map the use_sg
1195 * pages. They are not guaranteed to be the same for those
1196 * architectures that implement an IOMMU.
1199 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
1200 scsi_sg_count(scsi_cmnd), datadir);
1201 if (unlikely(!nseg))
1204 lpfc_cmd->seg_cnt = nseg;
1205 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
1206 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1207 "9064 BLKGRD: %s: Too many sg segments from "
1208 "dma_map_sg. Config %d, seg_cnt %d\n",
1209 __func__, phba->cfg_sg_seg_cnt,
1211 lpfc_cmd->seg_cnt = 0;
1212 scsi_dma_unmap(scsi_cmnd);
1217 * The driver established a maximum scatter-gather segment count
1218 * during probe that limits the number of sg elements in any
1219 * single scsi command. Just run through the seg_cnt and format
1221 * When using SLI-3 the driver will try to fit all the BDEs into
1222 * the IOCB. If it can't then the BDEs get added to a BPL as it
1223 * does for SLI-2 mode.
1225 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
1226 physaddr = sg_dma_address(sgel);
1227 if (phba->sli_rev == 3 &&
1228 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1229 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
1230 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
1231 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1232 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
1233 data_bde->addrLow = putPaddrLow(physaddr);
1234 data_bde->addrHigh = putPaddrHigh(physaddr);
1237 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1238 bpl->tus.f.bdeSize = sg_dma_len(sgel);
1239 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1241 le32_to_cpu(putPaddrLow(physaddr));
1243 le32_to_cpu(putPaddrHigh(physaddr));
1250 * Finish initializing those IOCB fields that are dependent on the
1251 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
1252 * explicitly reinitialized and for SLI-3 the extended bde count is
1253 * explicitly reinitialized since all iocb memory resources are reused.
1255 if (phba->sli_rev == 3 &&
1256 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
1257 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
1258 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
1260 * The extended IOCB format can only fit 3 BDE or a BPL.
1261 * This I/O has more than 3 BDE so the 1st data bde will
1262 * be a BPL that is filled in here.
1264 physaddr = lpfc_cmd->dma_handle;
1265 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
1266 data_bde->tus.f.bdeSize = (num_bde *
1267 sizeof(struct ulp_bde64));
1268 physaddr += (sizeof(struct fcp_cmnd) +
1269 sizeof(struct fcp_rsp) +
1270 (2 * sizeof(struct ulp_bde64)));
1271 data_bde->addrHigh = putPaddrHigh(physaddr);
1272 data_bde->addrLow = putPaddrLow(physaddr);
1273 /* ebde count includes the response bde and data bpl */
1274 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
1276 /* ebde count includes the response bde and data bdes */
1277 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1280 iocb_cmd->un.fcpi64.bdl.bdeSize =
1281 ((num_bde + 2) * sizeof(struct ulp_bde64));
1282 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
1284 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
1287 * Due to difference in data length between DIF/non-DIF paths,
1288 * we need to set word 4 of IOCB here
1290 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
1294 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1296 /* Return BG_ERR_INIT if error injection is detected by Initiator */
1297 #define BG_ERR_INIT 0x1
1298 /* Return BG_ERR_TGT if error injection is detected by Target */
1299 #define BG_ERR_TGT 0x2
1300 /* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
1301 #define BG_ERR_SWAP 0x10
1303 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
1306 #define BG_ERR_CHECK 0x20
1309 * lpfc_bg_err_inject - Determine if we should inject an error
1310 * @phba: The Hba for which this call is being executed.
1311 * @sc: The SCSI command to examine
1312 * @reftag: (out) BlockGuard reference tag for transmitted data
1313 * @apptag: (out) BlockGuard application tag for transmitted data
1314 * @new_guard (in) Value to replace CRC with if needed
1316 * Returns BG_ERR_* bit mask or 0 if request ignored
1319 lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1320 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
1322 struct scatterlist *sgpe; /* s/g prot entry */
1323 struct lpfc_scsi_buf *lpfc_cmd = NULL;
1324 struct scsi_dif_tuple *src = NULL;
1325 struct lpfc_nodelist *ndlp;
1326 struct lpfc_rport_data *rdata;
1327 uint32_t op = scsi_get_prot_op(sc);
1334 if (op == SCSI_PROT_NORMAL)
1337 sgpe = scsi_prot_sglist(sc);
1338 lba = scsi_get_lba(sc);
1340 /* First check if we need to match the LBA */
1341 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1342 blksize = lpfc_cmd_blksize(sc);
1343 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1345 /* Make sure we have the right LBA if one is specified */
1346 if ((phba->lpfc_injerr_lba < lba) ||
1347 (phba->lpfc_injerr_lba >= (lba + numblks)))
1350 blockoff = phba->lpfc_injerr_lba - lba;
1351 numblks = sg_dma_len(sgpe) /
1352 sizeof(struct scsi_dif_tuple);
1353 if (numblks < blockoff)
1358 /* Next check if we need to match the remote NPortID or WWPN */
1359 rdata = lpfc_rport_data_from_scsi_device(sc->device);
1360 if (rdata && rdata->pnode) {
1361 ndlp = rdata->pnode;
1363 /* Make sure we have the right NPortID if one is specified */
1364 if (phba->lpfc_injerr_nportid &&
1365 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1369 * Make sure we have the right WWPN if one is specified.
1370 * wwn[0] should be a non-zero NAA in a good WWPN.
1372 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1373 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1374 sizeof(struct lpfc_name)) != 0))
1378 /* Setup a ptr to the protection data if the SCSI host provides it */
1380 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1382 lpfc_cmd = (struct lpfc_scsi_buf *)sc->host_scribble;
1385 /* Should we change the Reference Tag */
1387 if (phba->lpfc_injerr_wref_cnt) {
1389 case SCSI_PROT_WRITE_PASS:
1392 * For WRITE_PASS, force the error
1393 * to be sent on the wire. It should
1394 * be detected by the Target.
1395 * If blockoff != 0 error will be
1396 * inserted in middle of the IO.
1399 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1400 "9076 BLKGRD: Injecting reftag error: "
1401 "write lba x%lx + x%x oldrefTag x%x\n",
1402 (unsigned long)lba, blockoff,
1403 be32_to_cpu(src->ref_tag));
1406 * Save the old ref_tag so we can
1407 * restore it on completion.
1410 lpfc_cmd->prot_data_type =
1412 lpfc_cmd->prot_data_segment =
1414 lpfc_cmd->prot_data =
1417 src->ref_tag = cpu_to_be32(0xDEADBEEF);
1418 phba->lpfc_injerr_wref_cnt--;
1419 if (phba->lpfc_injerr_wref_cnt == 0) {
1420 phba->lpfc_injerr_nportid = 0;
1421 phba->lpfc_injerr_lba =
1422 LPFC_INJERR_LBA_OFF;
1423 memset(&phba->lpfc_injerr_wwpn,
1424 0, sizeof(struct lpfc_name));
1426 rc = BG_ERR_TGT | BG_ERR_CHECK;
1431 case SCSI_PROT_WRITE_INSERT:
1433 * For WRITE_INSERT, force the error
1434 * to be sent on the wire. It should be
1435 * detected by the Target.
1437 /* DEADBEEF will be the reftag on the wire */
1438 *reftag = 0xDEADBEEF;
1439 phba->lpfc_injerr_wref_cnt--;
1440 if (phba->lpfc_injerr_wref_cnt == 0) {
1441 phba->lpfc_injerr_nportid = 0;
1442 phba->lpfc_injerr_lba =
1443 LPFC_INJERR_LBA_OFF;
1444 memset(&phba->lpfc_injerr_wwpn,
1445 0, sizeof(struct lpfc_name));
1447 rc = BG_ERR_TGT | BG_ERR_CHECK;
1449 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1450 "9078 BLKGRD: Injecting reftag error: "
1451 "write lba x%lx\n", (unsigned long)lba);
1453 case SCSI_PROT_WRITE_STRIP:
1455 * For WRITE_STRIP and WRITE_PASS,
1456 * force the error on data
1457 * being copied from SLI-Host to SLI-Port.
1459 *reftag = 0xDEADBEEF;
1460 phba->lpfc_injerr_wref_cnt--;
1461 if (phba->lpfc_injerr_wref_cnt == 0) {
1462 phba->lpfc_injerr_nportid = 0;
1463 phba->lpfc_injerr_lba =
1464 LPFC_INJERR_LBA_OFF;
1465 memset(&phba->lpfc_injerr_wwpn,
1466 0, sizeof(struct lpfc_name));
1470 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1471 "9077 BLKGRD: Injecting reftag error: "
1472 "write lba x%lx\n", (unsigned long)lba);
1476 if (phba->lpfc_injerr_rref_cnt) {
1478 case SCSI_PROT_READ_INSERT:
1479 case SCSI_PROT_READ_STRIP:
1480 case SCSI_PROT_READ_PASS:
1482 * For READ_STRIP and READ_PASS, force the
1483 * error on data being read off the wire. It
1484 * should force an IO error to the driver.
1486 *reftag = 0xDEADBEEF;
1487 phba->lpfc_injerr_rref_cnt--;
1488 if (phba->lpfc_injerr_rref_cnt == 0) {
1489 phba->lpfc_injerr_nportid = 0;
1490 phba->lpfc_injerr_lba =
1491 LPFC_INJERR_LBA_OFF;
1492 memset(&phba->lpfc_injerr_wwpn,
1493 0, sizeof(struct lpfc_name));
1497 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1498 "9079 BLKGRD: Injecting reftag error: "
1499 "read lba x%lx\n", (unsigned long)lba);
1505 /* Should we change the Application Tag */
1507 if (phba->lpfc_injerr_wapp_cnt) {
1509 case SCSI_PROT_WRITE_PASS:
1512 * For WRITE_PASS, force the error
1513 * to be sent on the wire. It should
1514 * be detected by the Target.
1515 * If blockoff != 0 error will be
1516 * inserted in middle of the IO.
1519 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1520 "9080 BLKGRD: Injecting apptag error: "
1521 "write lba x%lx + x%x oldappTag x%x\n",
1522 (unsigned long)lba, blockoff,
1523 be16_to_cpu(src->app_tag));
1526 * Save the old app_tag so we can
1527 * restore it on completion.
1530 lpfc_cmd->prot_data_type =
1532 lpfc_cmd->prot_data_segment =
1534 lpfc_cmd->prot_data =
1537 src->app_tag = cpu_to_be16(0xDEAD);
1538 phba->lpfc_injerr_wapp_cnt--;
1539 if (phba->lpfc_injerr_wapp_cnt == 0) {
1540 phba->lpfc_injerr_nportid = 0;
1541 phba->lpfc_injerr_lba =
1542 LPFC_INJERR_LBA_OFF;
1543 memset(&phba->lpfc_injerr_wwpn,
1544 0, sizeof(struct lpfc_name));
1546 rc = BG_ERR_TGT | BG_ERR_CHECK;
1550 case SCSI_PROT_WRITE_INSERT:
1552 * For WRITE_INSERT, force the
1553 * error to be sent on the wire. It should be
1554 * detected by the Target.
1556 /* DEAD will be the apptag on the wire */
1558 phba->lpfc_injerr_wapp_cnt--;
1559 if (phba->lpfc_injerr_wapp_cnt == 0) {
1560 phba->lpfc_injerr_nportid = 0;
1561 phba->lpfc_injerr_lba =
1562 LPFC_INJERR_LBA_OFF;
1563 memset(&phba->lpfc_injerr_wwpn,
1564 0, sizeof(struct lpfc_name));
1566 rc = BG_ERR_TGT | BG_ERR_CHECK;
1568 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1569 "0813 BLKGRD: Injecting apptag error: "
1570 "write lba x%lx\n", (unsigned long)lba);
1572 case SCSI_PROT_WRITE_STRIP:
1574 * For WRITE_STRIP and WRITE_PASS,
1575 * force the error on data
1576 * being copied from SLI-Host to SLI-Port.
1579 phba->lpfc_injerr_wapp_cnt--;
1580 if (phba->lpfc_injerr_wapp_cnt == 0) {
1581 phba->lpfc_injerr_nportid = 0;
1582 phba->lpfc_injerr_lba =
1583 LPFC_INJERR_LBA_OFF;
1584 memset(&phba->lpfc_injerr_wwpn,
1585 0, sizeof(struct lpfc_name));
1589 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1590 "0812 BLKGRD: Injecting apptag error: "
1591 "write lba x%lx\n", (unsigned long)lba);
1595 if (phba->lpfc_injerr_rapp_cnt) {
1597 case SCSI_PROT_READ_INSERT:
1598 case SCSI_PROT_READ_STRIP:
1599 case SCSI_PROT_READ_PASS:
1601 * For READ_STRIP and READ_PASS, force the
1602 * error on data being read off the wire. It
1603 * should force an IO error to the driver.
1606 phba->lpfc_injerr_rapp_cnt--;
1607 if (phba->lpfc_injerr_rapp_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));
1616 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1617 "0814 BLKGRD: Injecting apptag error: "
1618 "read lba x%lx\n", (unsigned long)lba);
1625 /* Should we change the Guard Tag */
1627 if (phba->lpfc_injerr_wgrd_cnt) {
1629 case SCSI_PROT_WRITE_PASS:
1633 case SCSI_PROT_WRITE_INSERT:
1635 * For WRITE_INSERT, force the
1636 * error to be sent on the wire. It should be
1637 * detected by the Target.
1639 phba->lpfc_injerr_wgrd_cnt--;
1640 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1641 phba->lpfc_injerr_nportid = 0;
1642 phba->lpfc_injerr_lba =
1643 LPFC_INJERR_LBA_OFF;
1644 memset(&phba->lpfc_injerr_wwpn,
1645 0, sizeof(struct lpfc_name));
1648 rc |= BG_ERR_TGT | BG_ERR_SWAP;
1649 /* Signals the caller to swap CRC->CSUM */
1651 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1652 "0817 BLKGRD: Injecting guard error: "
1653 "write lba x%lx\n", (unsigned long)lba);
1655 case SCSI_PROT_WRITE_STRIP:
1657 * For WRITE_STRIP and WRITE_PASS,
1658 * force the error on data
1659 * being copied from SLI-Host to SLI-Port.
1661 phba->lpfc_injerr_wgrd_cnt--;
1662 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1663 phba->lpfc_injerr_nportid = 0;
1664 phba->lpfc_injerr_lba =
1665 LPFC_INJERR_LBA_OFF;
1666 memset(&phba->lpfc_injerr_wwpn,
1667 0, sizeof(struct lpfc_name));
1670 rc = BG_ERR_INIT | BG_ERR_SWAP;
1671 /* Signals the caller to swap CRC->CSUM */
1673 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1674 "0816 BLKGRD: Injecting guard error: "
1675 "write lba x%lx\n", (unsigned long)lba);
1679 if (phba->lpfc_injerr_rgrd_cnt) {
1681 case SCSI_PROT_READ_INSERT:
1682 case SCSI_PROT_READ_STRIP:
1683 case SCSI_PROT_READ_PASS:
1685 * For READ_STRIP and READ_PASS, force the
1686 * error on data being read off the wire. It
1687 * should force an IO error to the driver.
1689 phba->lpfc_injerr_rgrd_cnt--;
1690 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1691 phba->lpfc_injerr_nportid = 0;
1692 phba->lpfc_injerr_lba =
1693 LPFC_INJERR_LBA_OFF;
1694 memset(&phba->lpfc_injerr_wwpn,
1695 0, sizeof(struct lpfc_name));
1698 rc = BG_ERR_INIT | BG_ERR_SWAP;
1699 /* Signals the caller to swap CRC->CSUM */
1701 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1702 "0818 BLKGRD: Injecting guard error: "
1703 "read lba x%lx\n", (unsigned long)lba);
1713 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1714 * the specified SCSI command.
1715 * @phba: The Hba for which this call is being executed.
1716 * @sc: The SCSI command to examine
1717 * @txopt: (out) BlockGuard operation for transmitted data
1718 * @rxopt: (out) BlockGuard operation for received data
1720 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1724 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1725 uint8_t *txop, uint8_t *rxop)
1729 if (lpfc_cmd_guard_csum(sc)) {
1730 switch (scsi_get_prot_op(sc)) {
1731 case SCSI_PROT_READ_INSERT:
1732 case SCSI_PROT_WRITE_STRIP:
1733 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1734 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1737 case SCSI_PROT_READ_STRIP:
1738 case SCSI_PROT_WRITE_INSERT:
1739 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1740 *txop = BG_OP_IN_NODIF_OUT_CRC;
1743 case SCSI_PROT_READ_PASS:
1744 case SCSI_PROT_WRITE_PASS:
1745 *rxop = BG_OP_IN_CRC_OUT_CSUM;
1746 *txop = BG_OP_IN_CSUM_OUT_CRC;
1749 case SCSI_PROT_NORMAL:
1751 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1752 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1753 scsi_get_prot_op(sc));
1759 switch (scsi_get_prot_op(sc)) {
1760 case SCSI_PROT_READ_STRIP:
1761 case SCSI_PROT_WRITE_INSERT:
1762 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1763 *txop = BG_OP_IN_NODIF_OUT_CRC;
1766 case SCSI_PROT_READ_PASS:
1767 case SCSI_PROT_WRITE_PASS:
1768 *rxop = BG_OP_IN_CRC_OUT_CRC;
1769 *txop = BG_OP_IN_CRC_OUT_CRC;
1772 case SCSI_PROT_READ_INSERT:
1773 case SCSI_PROT_WRITE_STRIP:
1774 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1775 *txop = BG_OP_IN_CRC_OUT_NODIF;
1778 case SCSI_PROT_NORMAL:
1780 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1781 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1782 scsi_get_prot_op(sc));
1791 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1793 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1794 * the specified SCSI command in order to force a guard tag error.
1795 * @phba: The Hba for which this call is being executed.
1796 * @sc: The SCSI command to examine
1797 * @txopt: (out) BlockGuard operation for transmitted data
1798 * @rxopt: (out) BlockGuard operation for received data
1800 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1804 lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1805 uint8_t *txop, uint8_t *rxop)
1809 if (lpfc_cmd_guard_csum(sc)) {
1810 switch (scsi_get_prot_op(sc)) {
1811 case SCSI_PROT_READ_INSERT:
1812 case SCSI_PROT_WRITE_STRIP:
1813 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1814 *txop = BG_OP_IN_CRC_OUT_NODIF;
1817 case SCSI_PROT_READ_STRIP:
1818 case SCSI_PROT_WRITE_INSERT:
1819 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1820 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1823 case SCSI_PROT_READ_PASS:
1824 case SCSI_PROT_WRITE_PASS:
1825 *rxop = BG_OP_IN_CSUM_OUT_CRC;
1826 *txop = BG_OP_IN_CRC_OUT_CSUM;
1829 case SCSI_PROT_NORMAL:
1835 switch (scsi_get_prot_op(sc)) {
1836 case SCSI_PROT_READ_STRIP:
1837 case SCSI_PROT_WRITE_INSERT:
1838 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1839 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1842 case SCSI_PROT_READ_PASS:
1843 case SCSI_PROT_WRITE_PASS:
1844 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
1845 *txop = BG_OP_IN_CSUM_OUT_CSUM;
1848 case SCSI_PROT_READ_INSERT:
1849 case SCSI_PROT_WRITE_STRIP:
1850 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1851 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1854 case SCSI_PROT_NORMAL:
1865 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1866 * @phba: The Hba for which this call is being executed.
1867 * @sc: pointer to scsi command we're working on
1868 * @bpl: pointer to buffer list for protection groups
1869 * @datacnt: number of segments of data that have been dma mapped
1871 * This function sets up BPL buffer list for protection groups of
1872 * type LPFC_PG_TYPE_NO_DIF
1874 * This is usually used when the HBA is instructed to generate
1875 * DIFs and insert them into data stream (or strip DIF from
1876 * incoming data stream)
1878 * The buffer list consists of just one protection group described
1880 * +-------------------------+
1881 * start of prot group --> | PDE_5 |
1882 * +-------------------------+
1884 * +-------------------------+
1886 * +-------------------------+
1887 * |more Data BDE's ... (opt)|
1888 * +-------------------------+
1891 * Note: Data s/g buffers have been dma mapped
1893 * Returns the number of BDEs added to the BPL.
1896 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1897 struct ulp_bde64 *bpl, int datasegcnt)
1899 struct scatterlist *sgde = NULL; /* s/g data entry */
1900 struct lpfc_pde5 *pde5 = NULL;
1901 struct lpfc_pde6 *pde6 = NULL;
1902 dma_addr_t physaddr;
1903 int i = 0, num_bde = 0, status;
1904 int datadir = sc->sc_data_direction;
1905 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1908 uint32_t checking = 1;
1912 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1916 /* extract some info from the scsi command for pde*/
1917 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
1919 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1920 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1922 if (rc & BG_ERR_SWAP)
1923 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1924 if (rc & BG_ERR_CHECK)
1929 /* setup PDE5 with what we have */
1930 pde5 = (struct lpfc_pde5 *) bpl;
1931 memset(pde5, 0, sizeof(struct lpfc_pde5));
1932 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1934 /* Endianness conversion if necessary for PDE5 */
1935 pde5->word0 = cpu_to_le32(pde5->word0);
1936 pde5->reftag = cpu_to_le32(reftag);
1938 /* advance bpl and increment bde count */
1941 pde6 = (struct lpfc_pde6 *) bpl;
1943 /* setup PDE6 with the rest of the info */
1944 memset(pde6, 0, sizeof(struct lpfc_pde6));
1945 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1946 bf_set(pde6_optx, pde6, txop);
1947 bf_set(pde6_oprx, pde6, rxop);
1950 * We only need to check the data on READs, for WRITEs
1951 * protection data is automatically generated, not checked.
1953 if (datadir == DMA_FROM_DEVICE) {
1954 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
1955 bf_set(pde6_ce, pde6, checking);
1957 bf_set(pde6_ce, pde6, 0);
1959 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
1960 bf_set(pde6_re, pde6, checking);
1962 bf_set(pde6_re, pde6, 0);
1964 bf_set(pde6_ai, pde6, 1);
1965 bf_set(pde6_ae, pde6, 0);
1966 bf_set(pde6_apptagval, pde6, 0);
1968 /* Endianness conversion if necessary for PDE6 */
1969 pde6->word0 = cpu_to_le32(pde6->word0);
1970 pde6->word1 = cpu_to_le32(pde6->word1);
1971 pde6->word2 = cpu_to_le32(pde6->word2);
1973 /* advance bpl and increment bde count */
1977 /* assumption: caller has already run dma_map_sg on command data */
1978 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1979 physaddr = sg_dma_address(sgde);
1980 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1981 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1982 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1983 if (datadir == DMA_TO_DEVICE)
1984 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1986 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1987 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1997 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1998 * @phba: The Hba for which this call is being executed.
1999 * @sc: pointer to scsi command we're working on
2000 * @bpl: pointer to buffer list for protection groups
2001 * @datacnt: number of segments of data that have been dma mapped
2002 * @protcnt: number of segment of protection data that have been dma mapped
2004 * This function sets up BPL buffer list for protection groups of
2005 * type LPFC_PG_TYPE_DIF
2007 * This is usually used when DIFs are in their own buffers,
2008 * separate from the data. The HBA can then by instructed
2009 * to place the DIFs in the outgoing stream. For read operations,
2010 * The HBA could extract the DIFs and place it in DIF buffers.
2012 * The buffer list for this type consists of one or more of the
2013 * protection groups described below:
2014 * +-------------------------+
2015 * start of first prot group --> | PDE_5 |
2016 * +-------------------------+
2018 * +-------------------------+
2019 * | PDE_7 (Prot BDE) |
2020 * +-------------------------+
2022 * +-------------------------+
2023 * |more Data BDE's ... (opt)|
2024 * +-------------------------+
2025 * start of new prot group --> | PDE_5 |
2026 * +-------------------------+
2028 * +-------------------------+
2030 * Note: It is assumed that both data and protection s/g buffers have been
2033 * Returns the number of BDEs added to the BPL.
2036 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2037 struct ulp_bde64 *bpl, int datacnt, int protcnt)
2039 struct scatterlist *sgde = NULL; /* s/g data entry */
2040 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2041 struct lpfc_pde5 *pde5 = NULL;
2042 struct lpfc_pde6 *pde6 = NULL;
2043 struct lpfc_pde7 *pde7 = NULL;
2044 dma_addr_t dataphysaddr, protphysaddr;
2045 unsigned short curr_data = 0, curr_prot = 0;
2046 unsigned int split_offset;
2047 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2048 unsigned int protgrp_blks, protgrp_bytes;
2049 unsigned int remainder, subtotal;
2051 int datadir = sc->sc_data_direction;
2052 unsigned char pgdone = 0, alldone = 0;
2054 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2057 uint32_t checking = 1;
2062 sgpe = scsi_prot_sglist(sc);
2063 sgde = scsi_sglist(sc);
2065 if (!sgpe || !sgde) {
2066 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2067 "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
2072 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2076 /* extract some info from the scsi command */
2077 blksize = lpfc_cmd_blksize(sc);
2078 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2080 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2081 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2083 if (rc & BG_ERR_SWAP)
2084 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2085 if (rc & BG_ERR_CHECK)
2092 /* Check to see if we ran out of space */
2093 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
2096 /* setup PDE5 with what we have */
2097 pde5 = (struct lpfc_pde5 *) bpl;
2098 memset(pde5, 0, sizeof(struct lpfc_pde5));
2099 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
2101 /* Endianness conversion if necessary for PDE5 */
2102 pde5->word0 = cpu_to_le32(pde5->word0);
2103 pde5->reftag = cpu_to_le32(reftag);
2105 /* advance bpl and increment bde count */
2108 pde6 = (struct lpfc_pde6 *) bpl;
2110 /* setup PDE6 with the rest of the info */
2111 memset(pde6, 0, sizeof(struct lpfc_pde6));
2112 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
2113 bf_set(pde6_optx, pde6, txop);
2114 bf_set(pde6_oprx, pde6, rxop);
2116 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2117 bf_set(pde6_ce, pde6, checking);
2119 bf_set(pde6_ce, pde6, 0);
2121 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2122 bf_set(pde6_re, pde6, checking);
2124 bf_set(pde6_re, pde6, 0);
2126 bf_set(pde6_ai, pde6, 1);
2127 bf_set(pde6_ae, pde6, 0);
2128 bf_set(pde6_apptagval, pde6, 0);
2130 /* Endianness conversion if necessary for PDE6 */
2131 pde6->word0 = cpu_to_le32(pde6->word0);
2132 pde6->word1 = cpu_to_le32(pde6->word1);
2133 pde6->word2 = cpu_to_le32(pde6->word2);
2135 /* advance bpl and increment bde count */
2139 /* setup the first BDE that points to protection buffer */
2140 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2141 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2143 /* must be integer multiple of the DIF block length */
2144 BUG_ON(protgroup_len % 8);
2146 pde7 = (struct lpfc_pde7 *) bpl;
2147 memset(pde7, 0, sizeof(struct lpfc_pde7));
2148 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
2150 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
2151 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
2153 protgrp_blks = protgroup_len / 8;
2154 protgrp_bytes = protgrp_blks * blksize;
2156 /* check if this pde is crossing the 4K boundary; if so split */
2157 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
2158 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
2159 protgroup_offset += protgroup_remainder;
2160 protgrp_blks = protgroup_remainder / 8;
2161 protgrp_bytes = protgrp_blks * blksize;
2163 protgroup_offset = 0;
2169 /* setup BDE's for data blocks associated with DIF data */
2171 subtotal = 0; /* total bytes processed for current prot grp */
2173 /* Check to see if we ran out of space */
2174 if (num_bde >= phba->cfg_total_seg_cnt)
2178 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2179 "9065 BLKGRD:%s Invalid data segment\n",
2184 dataphysaddr = sg_dma_address(sgde) + split_offset;
2185 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
2186 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
2188 remainder = sg_dma_len(sgde) - split_offset;
2190 if ((subtotal + remainder) <= protgrp_bytes) {
2191 /* we can use this whole buffer */
2192 bpl->tus.f.bdeSize = remainder;
2195 if ((subtotal + remainder) == protgrp_bytes)
2198 /* must split this buffer with next prot grp */
2199 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
2200 split_offset += bpl->tus.f.bdeSize;
2203 subtotal += bpl->tus.f.bdeSize;
2205 if (datadir == DMA_TO_DEVICE)
2206 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
2208 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
2209 bpl->tus.w = le32_to_cpu(bpl->tus.w);
2217 /* Move to the next s/g segment if possible */
2218 sgde = sg_next(sgde);
2222 if (protgroup_offset) {
2223 /* update the reference tag */
2224 reftag += protgrp_blks;
2230 if (curr_prot == protcnt) {
2232 } else if (curr_prot < protcnt) {
2233 /* advance to next prot buffer */
2234 sgpe = sg_next(sgpe);
2237 /* update the reference tag */
2238 reftag += protgrp_blks;
2240 /* if we're here, we have a bug */
2241 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2242 "9054 BLKGRD: bug in %s\n", __func__);
2252 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
2253 * @phba: The Hba for which this call is being executed.
2254 * @sc: pointer to scsi command we're working on
2255 * @sgl: pointer to buffer list for protection groups
2256 * @datacnt: number of segments of data that have been dma mapped
2258 * This function sets up SGL buffer list for protection groups of
2259 * type LPFC_PG_TYPE_NO_DIF
2261 * This is usually used when the HBA is instructed to generate
2262 * DIFs and insert them into data stream (or strip DIF from
2263 * incoming data stream)
2265 * The buffer list consists of just one protection group described
2267 * +-------------------------+
2268 * start of prot group --> | DI_SEED |
2269 * +-------------------------+
2271 * +-------------------------+
2272 * |more Data SGE's ... (opt)|
2273 * +-------------------------+
2276 * Note: Data s/g buffers have been dma mapped
2278 * Returns the number of SGEs added to the SGL.
2281 lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2282 struct sli4_sge *sgl, int datasegcnt)
2284 struct scatterlist *sgde = NULL; /* s/g data entry */
2285 struct sli4_sge_diseed *diseed = NULL;
2286 dma_addr_t physaddr;
2287 int i = 0, num_sge = 0, status;
2290 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2293 uint32_t checking = 1;
2295 uint32_t dma_offset = 0;
2297 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2301 /* extract some info from the scsi command for pde*/
2302 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2304 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2305 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2307 if (rc & BG_ERR_SWAP)
2308 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2309 if (rc & BG_ERR_CHECK)
2314 /* setup DISEED with what we have */
2315 diseed = (struct sli4_sge_diseed *) sgl;
2316 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2317 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2319 /* Endianness conversion if necessary */
2320 diseed->ref_tag = cpu_to_le32(reftag);
2321 diseed->ref_tag_tran = diseed->ref_tag;
2324 * We only need to check the data on READs, for WRITEs
2325 * protection data is automatically generated, not checked.
2327 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2328 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
2329 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2331 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2333 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2334 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2336 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2339 /* setup DISEED with the rest of the info */
2340 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2341 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2343 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2344 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2346 /* Endianness conversion if necessary for DISEED */
2347 diseed->word2 = cpu_to_le32(diseed->word2);
2348 diseed->word3 = cpu_to_le32(diseed->word3);
2350 /* advance bpl and increment sge count */
2354 /* assumption: caller has already run dma_map_sg on command data */
2355 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
2356 physaddr = sg_dma_address(sgde);
2357 dma_len = sg_dma_len(sgde);
2358 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2359 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2360 if ((i + 1) == datasegcnt)
2361 bf_set(lpfc_sli4_sge_last, sgl, 1);
2363 bf_set(lpfc_sli4_sge_last, sgl, 0);
2364 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2365 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2367 sgl->sge_len = cpu_to_le32(dma_len);
2368 dma_offset += dma_len;
2379 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2380 * @phba: The Hba for which this call is being executed.
2381 * @sc: pointer to scsi command we're working on
2382 * @sgl: pointer to buffer list for protection groups
2383 * @datacnt: number of segments of data that have been dma mapped
2384 * @protcnt: number of segment of protection data that have been dma mapped
2386 * This function sets up SGL buffer list for protection groups of
2387 * type LPFC_PG_TYPE_DIF
2389 * This is usually used when DIFs are in their own buffers,
2390 * separate from the data. The HBA can then by instructed
2391 * to place the DIFs in the outgoing stream. For read operations,
2392 * The HBA could extract the DIFs and place it in DIF buffers.
2394 * The buffer list for this type consists of one or more of the
2395 * protection groups described below:
2396 * +-------------------------+
2397 * start of first prot group --> | DISEED |
2398 * +-------------------------+
2399 * | DIF (Prot SGE) |
2400 * +-------------------------+
2402 * +-------------------------+
2403 * |more Data SGE's ... (opt)|
2404 * +-------------------------+
2405 * start of new prot group --> | DISEED |
2406 * +-------------------------+
2408 * +-------------------------+
2410 * Note: It is assumed that both data and protection s/g buffers have been
2413 * Returns the number of SGEs added to the SGL.
2416 lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2417 struct sli4_sge *sgl, int datacnt, int protcnt)
2419 struct scatterlist *sgde = NULL; /* s/g data entry */
2420 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2421 struct sli4_sge_diseed *diseed = NULL;
2422 dma_addr_t dataphysaddr, protphysaddr;
2423 unsigned short curr_data = 0, curr_prot = 0;
2424 unsigned int split_offset;
2425 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2426 unsigned int protgrp_blks, protgrp_bytes;
2427 unsigned int remainder, subtotal;
2429 unsigned char pgdone = 0, alldone = 0;
2434 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2437 uint32_t checking = 1;
2438 uint32_t dma_offset = 0;
2441 sgpe = scsi_prot_sglist(sc);
2442 sgde = scsi_sglist(sc);
2444 if (!sgpe || !sgde) {
2445 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2446 "9082 Invalid s/g entry: data=0x%p prot=0x%p\n",
2451 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2455 /* extract some info from the scsi command */
2456 blksize = lpfc_cmd_blksize(sc);
2457 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2459 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2460 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2462 if (rc & BG_ERR_SWAP)
2463 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2464 if (rc & BG_ERR_CHECK)
2471 /* Check to see if we ran out of space */
2472 if (num_sge >= (phba->cfg_total_seg_cnt - 2))
2475 /* setup DISEED with what we have */
2476 diseed = (struct sli4_sge_diseed *) sgl;
2477 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2478 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2480 /* Endianness conversion if necessary */
2481 diseed->ref_tag = cpu_to_le32(reftag);
2482 diseed->ref_tag_tran = diseed->ref_tag;
2484 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
2485 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2488 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2490 * When in this mode, the hardware will replace
2491 * the guard tag from the host with a
2492 * newly generated good CRC for the wire.
2493 * Switch to raw mode here to avoid this
2494 * behavior. What the host sends gets put on the wire.
2496 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2497 txop = BG_OP_RAW_MODE;
2498 rxop = BG_OP_RAW_MODE;
2503 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2504 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2506 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2508 /* setup DISEED with the rest of the info */
2509 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2510 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2512 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2513 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2515 /* Endianness conversion if necessary for DISEED */
2516 diseed->word2 = cpu_to_le32(diseed->word2);
2517 diseed->word3 = cpu_to_le32(diseed->word3);
2519 /* advance sgl and increment bde count */
2523 /* setup the first BDE that points to protection buffer */
2524 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2525 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2527 /* must be integer multiple of the DIF block length */
2528 BUG_ON(protgroup_len % 8);
2530 /* Now setup DIF SGE */
2532 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2533 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2534 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2535 sgl->word2 = cpu_to_le32(sgl->word2);
2537 protgrp_blks = protgroup_len / 8;
2538 protgrp_bytes = protgrp_blks * blksize;
2540 /* check if DIF SGE is crossing the 4K boundary; if so split */
2541 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2542 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2543 protgroup_offset += protgroup_remainder;
2544 protgrp_blks = protgroup_remainder / 8;
2545 protgrp_bytes = protgrp_blks * blksize;
2547 protgroup_offset = 0;
2553 /* setup SGE's for data blocks associated with DIF data */
2555 subtotal = 0; /* total bytes processed for current prot grp */
2557 /* Check to see if we ran out of space */
2558 if (num_sge >= phba->cfg_total_seg_cnt)
2562 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2563 "9086 BLKGRD:%s Invalid data segment\n",
2568 dataphysaddr = sg_dma_address(sgde) + split_offset;
2570 remainder = sg_dma_len(sgde) - split_offset;
2572 if ((subtotal + remainder) <= protgrp_bytes) {
2573 /* we can use this whole buffer */
2574 dma_len = remainder;
2577 if ((subtotal + remainder) == protgrp_bytes)
2580 /* must split this buffer with next prot grp */
2581 dma_len = protgrp_bytes - subtotal;
2582 split_offset += dma_len;
2585 subtotal += dma_len;
2587 sgl->addr_lo = cpu_to_le32(putPaddrLow(dataphysaddr));
2588 sgl->addr_hi = cpu_to_le32(putPaddrHigh(dataphysaddr));
2589 bf_set(lpfc_sli4_sge_last, sgl, 0);
2590 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2591 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2593 sgl->sge_len = cpu_to_le32(dma_len);
2594 dma_offset += dma_len;
2602 /* Move to the next s/g segment if possible */
2603 sgde = sg_next(sgde);
2606 if (protgroup_offset) {
2607 /* update the reference tag */
2608 reftag += protgrp_blks;
2614 if (curr_prot == protcnt) {
2615 bf_set(lpfc_sli4_sge_last, sgl, 1);
2617 } else if (curr_prot < protcnt) {
2618 /* advance to next prot buffer */
2619 sgpe = sg_next(sgpe);
2622 /* update the reference tag */
2623 reftag += protgrp_blks;
2625 /* if we're here, we have a bug */
2626 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2627 "9085 BLKGRD: bug in %s\n", __func__);
2638 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2639 * @phba: The Hba for which this call is being executed.
2640 * @sc: pointer to scsi command we're working on
2642 * Given a SCSI command that supports DIF, determine composition of protection
2643 * groups involved in setting up buffer lists
2645 * Returns: Protection group type (with or without DIF)
2649 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2651 int ret = LPFC_PG_TYPE_INVALID;
2652 unsigned char op = scsi_get_prot_op(sc);
2655 case SCSI_PROT_READ_STRIP:
2656 case SCSI_PROT_WRITE_INSERT:
2657 ret = LPFC_PG_TYPE_NO_DIF;
2659 case SCSI_PROT_READ_INSERT:
2660 case SCSI_PROT_WRITE_STRIP:
2661 case SCSI_PROT_READ_PASS:
2662 case SCSI_PROT_WRITE_PASS:
2663 ret = LPFC_PG_TYPE_DIF_BUF;
2667 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2668 "9021 Unsupported protection op:%d\n",
2676 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2677 * @phba: The Hba for which this call is being executed.
2678 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2680 * Adjust the data length to account for how much data
2681 * is actually on the wire.
2683 * returns the adjusted data length
2686 lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2687 struct lpfc_scsi_buf *lpfc_cmd)
2689 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2692 fcpdl = scsi_bufflen(sc);
2694 /* Check if there is protection data on the wire */
2695 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2696 /* Read check for protection data */
2697 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2701 /* Write check for protection data */
2702 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2707 * If we are in DIF Type 1 mode every data block has a 8 byte
2708 * DIF (trailer) attached to it. Must ajust FCP data length
2709 * to account for the protection data.
2711 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
2717 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2718 * @phba: The Hba for which this call is being executed.
2719 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2721 * This is the protection/DIF aware version of
2722 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2723 * two functions eventually, but for now, it's here
2726 lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2727 struct lpfc_scsi_buf *lpfc_cmd)
2729 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2730 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2731 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
2732 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2733 uint32_t num_bde = 0;
2734 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2735 int prot_group_type = 0;
2739 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2740 * fcp_rsp regions to the first data bde entry
2743 if (scsi_sg_count(scsi_cmnd)) {
2745 * The driver stores the segment count returned from pci_map_sg
2746 * because this a count of dma-mappings used to map the use_sg
2747 * pages. They are not guaranteed to be the same for those
2748 * architectures that implement an IOMMU.
2750 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2751 scsi_sglist(scsi_cmnd),
2752 scsi_sg_count(scsi_cmnd), datadir);
2753 if (unlikely(!datasegcnt))
2756 lpfc_cmd->seg_cnt = datasegcnt;
2758 /* First check if data segment count from SCSI Layer is good */
2759 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
2762 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2764 switch (prot_group_type) {
2765 case LPFC_PG_TYPE_NO_DIF:
2767 /* Here we need to add a PDE5 and PDE6 to the count */
2768 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt)
2771 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2773 /* we should have 2 or more entries in buffer list */
2778 case LPFC_PG_TYPE_DIF_BUF:
2780 * This type indicates that protection buffers are
2781 * passed to the driver, so that needs to be prepared
2784 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2785 scsi_prot_sglist(scsi_cmnd),
2786 scsi_prot_sg_count(scsi_cmnd), datadir);
2787 if (unlikely(!protsegcnt)) {
2788 scsi_dma_unmap(scsi_cmnd);
2792 lpfc_cmd->prot_seg_cnt = protsegcnt;
2795 * There is a minimun of 4 BPLs used for every
2796 * protection data segment.
2798 if ((lpfc_cmd->prot_seg_cnt * 4) >
2799 (phba->cfg_total_seg_cnt - 2))
2802 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2803 datasegcnt, protsegcnt);
2804 /* we should have 3 or more entries in buffer list */
2805 if ((num_bde < 3) ||
2806 (num_bde > phba->cfg_total_seg_cnt))
2810 case LPFC_PG_TYPE_INVALID:
2812 scsi_dma_unmap(scsi_cmnd);
2813 lpfc_cmd->seg_cnt = 0;
2815 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2816 "9022 Unexpected protection group %i\n",
2823 * Finish initializing those IOCB fields that are dependent on the
2824 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2825 * reinitialized since all iocb memory resources are used many times
2826 * for transmit, receive, and continuation bpl's.
2828 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2829 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2830 iocb_cmd->ulpBdeCount = 1;
2831 iocb_cmd->ulpLe = 1;
2833 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
2834 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2837 * Due to difference in data length between DIF/non-DIF paths,
2838 * we need to set word 4 of IOCB here
2840 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2844 if (lpfc_cmd->seg_cnt)
2845 scsi_dma_unmap(scsi_cmnd);
2846 if (lpfc_cmd->prot_seg_cnt)
2847 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2848 scsi_prot_sg_count(scsi_cmnd),
2849 scsi_cmnd->sc_data_direction);
2851 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2852 "9023 Cannot setup S/G List for HBA"
2853 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2854 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2855 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
2856 prot_group_type, num_bde);
2858 lpfc_cmd->seg_cnt = 0;
2859 lpfc_cmd->prot_seg_cnt = 0;
2864 * This function calcuates the T10 DIF guard tag
2865 * on the specified data using a CRC algorithmn
2869 lpfc_bg_crc(uint8_t *data, int count)
2874 crc = crc_t10dif(data, count);
2875 x = cpu_to_be16(crc);
2880 * This function calcuates the T10 DIF guard tag
2881 * on the specified data using a CSUM algorithmn
2882 * using ip_compute_csum.
2885 lpfc_bg_csum(uint8_t *data, int count)
2889 ret = ip_compute_csum(data, count);
2894 * This function examines the protection data to try to determine
2895 * what type of T10-DIF error occurred.
2898 lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
2900 struct scatterlist *sgpe; /* s/g prot entry */
2901 struct scatterlist *sgde; /* s/g data entry */
2902 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2903 struct scsi_dif_tuple *src = NULL;
2904 uint8_t *data_src = NULL;
2906 uint16_t start_app_tag, app_tag;
2907 uint32_t start_ref_tag, ref_tag;
2908 int prot, protsegcnt;
2909 int err_type, len, data_len;
2910 int chk_ref, chk_app, chk_guard;
2914 err_type = BGS_GUARD_ERR_MASK;
2918 /* First check to see if there is protection data to examine */
2919 prot = scsi_get_prot_op(cmd);
2920 if ((prot == SCSI_PROT_READ_STRIP) ||
2921 (prot == SCSI_PROT_WRITE_INSERT) ||
2922 (prot == SCSI_PROT_NORMAL))
2925 /* Currently the driver just supports ref_tag and guard_tag checking */
2930 /* Setup a ptr to the protection data provided by the SCSI host */
2931 sgpe = scsi_prot_sglist(cmd);
2932 protsegcnt = lpfc_cmd->prot_seg_cnt;
2934 if (sgpe && protsegcnt) {
2937 * We will only try to verify guard tag if the segment
2938 * data length is a multiple of the blksize.
2940 sgde = scsi_sglist(cmd);
2941 blksize = lpfc_cmd_blksize(cmd);
2942 data_src = (uint8_t *)sg_virt(sgde);
2943 data_len = sgde->length;
2944 if ((data_len & (blksize - 1)) == 0)
2947 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2948 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
2949 start_app_tag = src->app_tag;
2951 while (src && protsegcnt) {
2955 * First check to see if a protection data
2958 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2959 (src->app_tag == T10_PI_APP_ESCAPE)) {
2964 /* First Guard Tag checking */
2966 guard_tag = src->guard_tag;
2967 if (lpfc_cmd_guard_csum(cmd))
2968 sum = lpfc_bg_csum(data_src,
2971 sum = lpfc_bg_crc(data_src,
2973 if ((guard_tag != sum)) {
2974 err_type = BGS_GUARD_ERR_MASK;
2979 /* Reference Tag checking */
2980 ref_tag = be32_to_cpu(src->ref_tag);
2981 if (chk_ref && (ref_tag != start_ref_tag)) {
2982 err_type = BGS_REFTAG_ERR_MASK;
2987 /* App Tag checking */
2988 app_tag = src->app_tag;
2989 if (chk_app && (app_tag != start_app_tag)) {
2990 err_type = BGS_APPTAG_ERR_MASK;
2994 len -= sizeof(struct scsi_dif_tuple);
2999 data_src += blksize;
3000 data_len -= blksize;
3003 * Are we at the end of the Data segment?
3004 * The data segment is only used for Guard
3007 if (chk_guard && (data_len == 0)) {
3009 sgde = sg_next(sgde);
3013 data_src = (uint8_t *)sg_virt(sgde);
3014 data_len = sgde->length;
3015 if ((data_len & (blksize - 1)) == 0)
3020 /* Goto the next Protection data segment */
3021 sgpe = sg_next(sgpe);
3023 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
3032 if (err_type == BGS_GUARD_ERR_MASK) {
3033 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3035 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3036 SAM_STAT_CHECK_CONDITION;
3037 phba->bg_guard_err_cnt++;
3038 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3039 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
3040 (unsigned long)scsi_get_lba(cmd),
3043 } else if (err_type == BGS_REFTAG_ERR_MASK) {
3044 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3046 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3047 SAM_STAT_CHECK_CONDITION;
3049 phba->bg_reftag_err_cnt++;
3050 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3051 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
3052 (unsigned long)scsi_get_lba(cmd),
3053 ref_tag, start_ref_tag);
3055 } else if (err_type == BGS_APPTAG_ERR_MASK) {
3056 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3058 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3059 SAM_STAT_CHECK_CONDITION;
3061 phba->bg_apptag_err_cnt++;
3062 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3063 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
3064 (unsigned long)scsi_get_lba(cmd),
3065 app_tag, start_app_tag);
3071 * This function checks for BlockGuard errors detected by
3072 * the HBA. In case of errors, the ASC/ASCQ fields in the
3073 * sense buffer will be set accordingly, paired with
3074 * ILLEGAL_REQUEST to signal to the kernel that the HBA
3075 * detected corruption.
3078 * 0 - No error found
3079 * 1 - BlockGuard error found
3080 * -1 - Internal error (bad profile, ...etc)
3083 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
3084 struct lpfc_iocbq *pIocbOut)
3086 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
3087 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
3089 uint32_t bghm = bgf->bghm;
3090 uint32_t bgstat = bgf->bgstat;
3091 uint64_t failing_sector = 0;
3093 spin_lock(&_dump_buf_lock);
3094 if (!_dump_buf_done) {
3095 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
3096 " Data for %u blocks to debugfs\n",
3097 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3098 lpfc_debug_save_data(phba, cmd);
3100 /* If we have a prot sgl, save the DIF buffer */
3101 if (lpfc_prot_group_type(phba, cmd) ==
3102 LPFC_PG_TYPE_DIF_BUF) {
3103 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
3104 "Saving DIF for %u blocks to debugfs\n",
3105 (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
3106 lpfc_debug_save_dif(phba, cmd);
3111 spin_unlock(&_dump_buf_lock);
3113 if (lpfc_bgs_get_invalid_prof(bgstat)) {
3114 cmd->result = DID_ERROR << 16;
3115 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3116 "9072 BLKGRD: Invalid BG Profile in cmd"
3117 " 0x%x lba 0x%llx blk cnt 0x%x "
3118 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3119 (unsigned long long)scsi_get_lba(cmd),
3120 blk_rq_sectors(cmd->request), bgstat, bghm);
3125 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
3126 cmd->result = DID_ERROR << 16;
3127 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3128 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
3129 " 0x%x lba 0x%llx blk cnt 0x%x "
3130 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3131 (unsigned long long)scsi_get_lba(cmd),
3132 blk_rq_sectors(cmd->request), bgstat, bghm);
3137 if (lpfc_bgs_get_guard_err(bgstat)) {
3140 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3142 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3143 SAM_STAT_CHECK_CONDITION;
3144 phba->bg_guard_err_cnt++;
3145 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3146 "9055 BLKGRD: Guard Tag error in cmd"
3147 " 0x%x lba 0x%llx blk cnt 0x%x "
3148 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3149 (unsigned long long)scsi_get_lba(cmd),
3150 blk_rq_sectors(cmd->request), bgstat, bghm);
3153 if (lpfc_bgs_get_reftag_err(bgstat)) {
3156 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3158 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3159 SAM_STAT_CHECK_CONDITION;
3161 phba->bg_reftag_err_cnt++;
3162 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3163 "9056 BLKGRD: Ref Tag error in cmd"
3164 " 0x%x lba 0x%llx blk cnt 0x%x "
3165 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3166 (unsigned long long)scsi_get_lba(cmd),
3167 blk_rq_sectors(cmd->request), bgstat, bghm);
3170 if (lpfc_bgs_get_apptag_err(bgstat)) {
3173 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
3175 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
3176 SAM_STAT_CHECK_CONDITION;
3178 phba->bg_apptag_err_cnt++;
3179 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3180 "9061 BLKGRD: App Tag error in cmd"
3181 " 0x%x lba 0x%llx blk cnt 0x%x "
3182 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3183 (unsigned long long)scsi_get_lba(cmd),
3184 blk_rq_sectors(cmd->request), bgstat, bghm);
3187 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
3189 * setup sense data descriptor 0 per SPC-4 as an information
3190 * field, and put the failing LBA in it.
3191 * This code assumes there was also a guard/app/ref tag error
3194 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
3195 cmd->sense_buffer[8] = 0; /* Information descriptor type */
3196 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
3197 cmd->sense_buffer[10] = 0x80; /* Validity bit */
3199 /* bghm is a "on the wire" FC frame based count */
3200 switch (scsi_get_prot_op(cmd)) {
3201 case SCSI_PROT_READ_INSERT:
3202 case SCSI_PROT_WRITE_STRIP:
3203 bghm /= cmd->device->sector_size;
3205 case SCSI_PROT_READ_STRIP:
3206 case SCSI_PROT_WRITE_INSERT:
3207 case SCSI_PROT_READ_PASS:
3208 case SCSI_PROT_WRITE_PASS:
3209 bghm /= (cmd->device->sector_size +
3210 sizeof(struct scsi_dif_tuple));
3214 failing_sector = scsi_get_lba(cmd);
3215 failing_sector += bghm;
3217 /* Descriptor Information */
3218 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3222 /* No error was reported - problem in FW? */
3223 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3224 "9057 BLKGRD: Unknown error in cmd"
3225 " 0x%x lba 0x%llx blk cnt 0x%x "
3226 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3227 (unsigned long long)scsi_get_lba(cmd),
3228 blk_rq_sectors(cmd->request), bgstat, bghm);
3230 /* Calcuate what type of error it was */
3231 lpfc_calc_bg_err(phba, lpfc_cmd);
3238 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3239 * @phba: The Hba for which this call is being executed.
3240 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3242 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3243 * field of @lpfc_cmd for device with SLI-4 interface spec.
3250 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3252 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3253 struct scatterlist *sgel = NULL;
3254 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3255 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
3256 struct sli4_sge *first_data_sgl;
3257 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3258 dma_addr_t physaddr;
3259 uint32_t num_bde = 0;
3261 uint32_t dma_offset = 0;
3263 struct ulp_bde64 *bde;
3266 * There are three possibilities here - use scatter-gather segment, use
3267 * the single mapping, or neither. Start the lpfc command prep by
3268 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3271 if (scsi_sg_count(scsi_cmnd)) {
3273 * The driver stores the segment count returned from pci_map_sg
3274 * because this a count of dma-mappings used to map the use_sg
3275 * pages. They are not guaranteed to be the same for those
3276 * architectures that implement an IOMMU.
3279 nseg = scsi_dma_map(scsi_cmnd);
3280 if (unlikely(nseg <= 0))
3283 /* clear the last flag in the fcp_rsp map entry */
3284 sgl->word2 = le32_to_cpu(sgl->word2);
3285 bf_set(lpfc_sli4_sge_last, sgl, 0);
3286 sgl->word2 = cpu_to_le32(sgl->word2);
3288 first_data_sgl = sgl;
3289 lpfc_cmd->seg_cnt = nseg;
3290 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3291 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3292 " %s: Too many sg segments from "
3293 "dma_map_sg. Config %d, seg_cnt %d\n",
3294 __func__, phba->cfg_sg_seg_cnt,
3296 lpfc_cmd->seg_cnt = 0;
3297 scsi_dma_unmap(scsi_cmnd);
3302 * The driver established a maximum scatter-gather segment count
3303 * during probe that limits the number of sg elements in any
3304 * single scsi command. Just run through the seg_cnt and format
3306 * When using SLI-3 the driver will try to fit all the BDEs into
3307 * the IOCB. If it can't then the BDEs get added to a BPL as it
3308 * does for SLI-2 mode.
3310 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
3311 physaddr = sg_dma_address(sgel);
3312 dma_len = sg_dma_len(sgel);
3313 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
3314 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
3315 sgl->word2 = le32_to_cpu(sgl->word2);
3316 if ((num_bde + 1) == nseg)
3317 bf_set(lpfc_sli4_sge_last, sgl, 1);
3319 bf_set(lpfc_sli4_sge_last, sgl, 0);
3320 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3321 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
3322 sgl->word2 = cpu_to_le32(sgl->word2);
3323 sgl->sge_len = cpu_to_le32(dma_len);
3324 dma_offset += dma_len;
3328 * Setup the first Payload BDE. For FCoE we just key off
3329 * Performance Hints, for FC we use lpfc_enable_pbde.
3330 * We populate words 13-15 of IOCB/WQE.
3332 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3333 phba->cfg_enable_pbde) {
3334 bde = (struct ulp_bde64 *)
3335 &(iocb_cmd->unsli3.sli3Words[5]);
3336 bde->addrLow = first_data_sgl->addr_lo;
3337 bde->addrHigh = first_data_sgl->addr_hi;
3338 bde->tus.f.bdeSize =
3339 le32_to_cpu(first_data_sgl->sge_len);
3340 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3341 bde->tus.w = cpu_to_le32(bde->tus.w);
3345 /* clear the last flag in the fcp_rsp map entry */
3346 sgl->word2 = le32_to_cpu(sgl->word2);
3347 bf_set(lpfc_sli4_sge_last, sgl, 1);
3348 sgl->word2 = cpu_to_le32(sgl->word2);
3350 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3351 phba->cfg_enable_pbde) {
3352 bde = (struct ulp_bde64 *)
3353 &(iocb_cmd->unsli3.sli3Words[5]);
3354 memset(bde, 0, (sizeof(uint32_t) * 3));
3359 * Finish initializing those IOCB fields that are dependent on the
3360 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3361 * explicitly reinitialized.
3362 * all iocb memory resources are reused.
3364 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3367 * Due to difference in data length between DIF/non-DIF paths,
3368 * we need to set word 4 of IOCB here
3370 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3373 * If the OAS driver feature is enabled and the lun is enabled for
3374 * OAS, set the oas iocb related flags.
3376 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3377 scsi_cmnd->device->hostdata)->oas_enabled) {
3378 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3379 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3380 scsi_cmnd->device->hostdata)->priority;
3386 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3387 * @phba: The Hba for which this call is being executed.
3388 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3390 * This is the protection/DIF aware version of
3391 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3392 * two functions eventually, but for now, it's here
3395 lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3396 struct lpfc_scsi_buf *lpfc_cmd)
3398 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3399 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3400 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->fcp_bpl);
3401 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3402 uint32_t num_sge = 0;
3403 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3404 int prot_group_type = 0;
3408 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3409 * fcp_rsp regions to the first data sge entry
3411 if (scsi_sg_count(scsi_cmnd)) {
3413 * The driver stores the segment count returned from pci_map_sg
3414 * because this a count of dma-mappings used to map the use_sg
3415 * pages. They are not guaranteed to be the same for those
3416 * architectures that implement an IOMMU.
3418 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3419 scsi_sglist(scsi_cmnd),
3420 scsi_sg_count(scsi_cmnd), datadir);
3421 if (unlikely(!datasegcnt))
3425 /* clear the last flag in the fcp_rsp map entry */
3426 sgl->word2 = le32_to_cpu(sgl->word2);
3427 bf_set(lpfc_sli4_sge_last, sgl, 0);
3428 sgl->word2 = cpu_to_le32(sgl->word2);
3431 lpfc_cmd->seg_cnt = datasegcnt;
3433 /* First check if data segment count from SCSI Layer is good */
3434 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt)
3437 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3439 switch (prot_group_type) {
3440 case LPFC_PG_TYPE_NO_DIF:
3441 /* Here we need to add a DISEED to the count */
3442 if ((lpfc_cmd->seg_cnt + 1) > phba->cfg_total_seg_cnt)
3445 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3448 /* we should have 2 or more entries in buffer list */
3453 case LPFC_PG_TYPE_DIF_BUF:
3455 * This type indicates that protection buffers are
3456 * passed to the driver, so that needs to be prepared
3459 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3460 scsi_prot_sglist(scsi_cmnd),
3461 scsi_prot_sg_count(scsi_cmnd), datadir);
3462 if (unlikely(!protsegcnt)) {
3463 scsi_dma_unmap(scsi_cmnd);
3467 lpfc_cmd->prot_seg_cnt = protsegcnt;
3469 * There is a minimun of 3 SGEs used for every
3470 * protection data segment.
3472 if ((lpfc_cmd->prot_seg_cnt * 3) >
3473 (phba->cfg_total_seg_cnt - 2))
3476 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3477 datasegcnt, protsegcnt);
3479 /* we should have 3 or more entries in buffer list */
3480 if ((num_sge < 3) ||
3481 (num_sge > phba->cfg_total_seg_cnt))
3485 case LPFC_PG_TYPE_INVALID:
3487 scsi_dma_unmap(scsi_cmnd);
3488 lpfc_cmd->seg_cnt = 0;
3490 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3491 "9083 Unexpected protection group %i\n",
3497 switch (scsi_get_prot_op(scsi_cmnd)) {
3498 case SCSI_PROT_WRITE_STRIP:
3499 case SCSI_PROT_READ_STRIP:
3500 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3502 case SCSI_PROT_WRITE_INSERT:
3503 case SCSI_PROT_READ_INSERT:
3504 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3506 case SCSI_PROT_WRITE_PASS:
3507 case SCSI_PROT_READ_PASS:
3508 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3512 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3513 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3516 * Due to difference in data length between DIF/non-DIF paths,
3517 * we need to set word 4 of IOCB here
3519 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3522 * If the OAS driver feature is enabled and the lun is enabled for
3523 * OAS, set the oas iocb related flags.
3525 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3526 scsi_cmnd->device->hostdata)->oas_enabled)
3527 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3531 if (lpfc_cmd->seg_cnt)
3532 scsi_dma_unmap(scsi_cmnd);
3533 if (lpfc_cmd->prot_seg_cnt)
3534 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3535 scsi_prot_sg_count(scsi_cmnd),
3536 scsi_cmnd->sc_data_direction);
3538 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3539 "9084 Cannot setup S/G List for HBA"
3540 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3541 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3542 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3543 prot_group_type, num_sge);
3545 lpfc_cmd->seg_cnt = 0;
3546 lpfc_cmd->prot_seg_cnt = 0;
3551 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3552 * @phba: The Hba for which this call is being executed.
3553 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3555 * This routine wraps the actual DMA mapping function pointer from the
3563 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3565 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3569 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3571 * @phba: The Hba for which this call is being executed.
3572 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3574 * This routine wraps the actual DMA mapping function pointer from the
3582 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
3584 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3588 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
3589 * @phba: Pointer to hba context object.
3590 * @vport: Pointer to vport object.
3591 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3592 * @rsp_iocb: Pointer to response iocb object which reported error.
3594 * This function posts an event when there is a SCSI command reporting
3595 * error from the scsi device.
3598 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3599 struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3600 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3601 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3602 uint32_t resp_info = fcprsp->rspStatus2;
3603 uint32_t scsi_status = fcprsp->rspStatus3;
3604 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3605 struct lpfc_fast_path_event *fast_path_evt = NULL;
3606 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3607 unsigned long flags;
3609 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3612 /* If there is queuefull or busy condition send a scsi event */
3613 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3614 (cmnd->result == SAM_STAT_BUSY)) {
3615 fast_path_evt = lpfc_alloc_fast_evt(phba);
3618 fast_path_evt->un.scsi_evt.event_type =
3620 fast_path_evt->un.scsi_evt.subcategory =
3621 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3622 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3623 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3624 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3625 &pnode->nlp_portname, sizeof(struct lpfc_name));
3626 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3627 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3628 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3629 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3630 fast_path_evt = lpfc_alloc_fast_evt(phba);
3633 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3635 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3636 LPFC_EVENT_CHECK_COND;
3637 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3639 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3640 &pnode->nlp_portname, sizeof(struct lpfc_name));
3641 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3642 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3643 fast_path_evt->un.check_cond_evt.sense_key =
3644 cmnd->sense_buffer[2] & 0xf;
3645 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3646 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3647 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3649 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3650 ((scsi_status == SAM_STAT_GOOD) &&
3651 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3653 * If status is good or resid does not match with fcp_param and
3654 * there is valid fcpi_parm, then there is a read_check error
3656 fast_path_evt = lpfc_alloc_fast_evt(phba);
3659 fast_path_evt->un.read_check_error.header.event_type =
3660 FC_REG_FABRIC_EVENT;
3661 fast_path_evt->un.read_check_error.header.subcategory =
3662 LPFC_EVENT_FCPRDCHKERR;
3663 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3664 &pnode->nlp_portname, sizeof(struct lpfc_name));
3665 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3666 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3667 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3668 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3669 fast_path_evt->un.read_check_error.fcpiparam =
3674 fast_path_evt->vport = vport;
3675 spin_lock_irqsave(&phba->hbalock, flags);
3676 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3677 spin_unlock_irqrestore(&phba->hbalock, flags);
3678 lpfc_worker_wake_up(phba);
3683 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3684 * @phba: The HBA for which this call is being executed.
3685 * @psb: The scsi buffer which is going to be un-mapped.
3687 * This routine does DMA un-mapping of scatter gather list of scsi command
3688 * field of @lpfc_cmd for device with SLI-3 interface spec.
3691 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
3694 * There are only two special cases to consider. (1) the scsi command
3695 * requested scatter-gather usage or (2) the scsi command allocated
3696 * a request buffer, but did not request use_sg. There is a third
3697 * case, but it does not require resource deallocation.
3699 if (psb->seg_cnt > 0)
3700 scsi_dma_unmap(psb->pCmd);
3701 if (psb->prot_seg_cnt > 0)
3702 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3703 scsi_prot_sg_count(psb->pCmd),
3704 psb->pCmd->sc_data_direction);
3708 * lpfc_handler_fcp_err - FCP response handler
3709 * @vport: The virtual port for which this call is being executed.
3710 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
3711 * @rsp_iocb: The response IOCB which contains FCP error.
3713 * This routine is called to process response IOCB with status field
3714 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3715 * based upon SCSI and FCP error.
3718 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
3719 struct lpfc_iocbq *rsp_iocb)
3721 struct lpfc_hba *phba = vport->phba;
3722 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3723 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3724 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3725 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3726 uint32_t resp_info = fcprsp->rspStatus2;
3727 uint32_t scsi_status = fcprsp->rspStatus3;
3729 uint32_t host_status = DID_OK;
3730 uint32_t rsplen = 0;
3732 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
3736 * If this is a task management command, there is no
3737 * scsi packet associated with this lpfc_cmd. The driver
3740 if (fcpcmd->fcpCntl2) {
3745 if (resp_info & RSP_LEN_VALID) {
3746 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3747 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
3748 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3749 "2719 Invalid response length: "
3750 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
3752 cmnd->device->lun, cmnd->cmnd[0],
3754 host_status = DID_ERROR;
3757 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3758 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3759 "2757 Protocol failure detected during "
3760 "processing of FCP I/O op: "
3761 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
3763 cmnd->device->lun, cmnd->cmnd[0],
3765 host_status = DID_ERROR;
3770 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3771 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3772 if (snslen > SCSI_SENSE_BUFFERSIZE)
3773 snslen = SCSI_SENSE_BUFFERSIZE;
3775 if (resp_info & RSP_LEN_VALID)
3776 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3777 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3779 lp = (uint32_t *)cmnd->sense_buffer;
3781 /* special handling for under run conditions */
3782 if (!scsi_status && (resp_info & RESID_UNDER)) {
3783 /* don't log under runs if fcp set... */
3784 if (vport->cfg_log_verbose & LOG_FCP)
3785 logit = LOG_FCP_ERROR;
3786 /* unless operator says so */
3787 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3788 logit = LOG_FCP_UNDER;
3791 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3792 "9024 FCP command x%x failed: x%x SNS x%x x%x "
3793 "Data: x%x x%x x%x x%x x%x\n",
3794 cmnd->cmnd[0], scsi_status,
3795 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3796 be32_to_cpu(fcprsp->rspResId),
3797 be32_to_cpu(fcprsp->rspSnsLen),
3798 be32_to_cpu(fcprsp->rspRspLen),
3801 scsi_set_resid(cmnd, 0);
3802 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
3803 if (resp_info & RESID_UNDER) {
3804 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
3806 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
3807 "9025 FCP Underrun, expected %d, "
3808 "residual %d Data: x%x x%x x%x\n",
3810 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3814 * If there is an under run, check if under run reported by
3815 * storage array is same as the under run reported by HBA.
3816 * If this is not same, there is a dropped frame.
3818 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
3819 lpfc_printf_vlog(vport, KERN_WARNING,
3820 LOG_FCP | LOG_FCP_ERROR,
3821 "9026 FCP Read Check Error "
3822 "and Underrun Data: x%x x%x x%x x%x\n",
3824 scsi_get_resid(cmnd), fcpi_parm,
3826 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3827 host_status = DID_ERROR;
3830 * The cmnd->underflow is the minimum number of bytes that must
3831 * be transferred for this command. Provided a sense condition
3832 * is not present, make sure the actual amount transferred is at
3833 * least the underflow value or fail.
3835 if (!(resp_info & SNS_LEN_VALID) &&
3836 (scsi_status == SAM_STAT_GOOD) &&
3837 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3838 < cmnd->underflow)) {
3839 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3840 "9027 FCP command x%x residual "
3841 "underrun converted to error "
3842 "Data: x%x x%x x%x\n",
3843 cmnd->cmnd[0], scsi_bufflen(cmnd),
3844 scsi_get_resid(cmnd), cmnd->underflow);
3845 host_status = DID_ERROR;
3847 } else if (resp_info & RESID_OVER) {
3848 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3849 "9028 FCP command x%x residual overrun error. "
3850 "Data: x%x x%x\n", cmnd->cmnd[0],
3851 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
3852 host_status = DID_ERROR;
3855 * Check SLI validation that all the transfer was actually done
3856 * (fcpi_parm should be zero). Apply check only to reads.
3858 } else if (fcpi_parm) {
3859 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
3860 "9029 FCP %s Check Error xri x%x Data: "
3861 "x%x x%x x%x x%x x%x\n",
3862 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3864 ((phba->sli_rev == LPFC_SLI_REV4) ?
3865 lpfc_cmd->cur_iocbq.sli4_xritag :
3866 rsp_iocb->iocb.ulpContext),
3867 fcpDl, be32_to_cpu(fcprsp->rspResId),
3868 fcpi_parm, cmnd->cmnd[0], scsi_status);
3870 /* There is some issue with the LPe12000 that causes it
3871 * to miscalculate the fcpi_parm and falsely trip this
3872 * recovery logic. Detect this case and don't error when true.
3874 if (fcpi_parm > fcpDl)
3877 switch (scsi_status) {
3879 case SAM_STAT_CHECK_CONDITION:
3880 /* Fabric dropped a data frame. Fail any successful
3881 * command in which we detected dropped frames.
3882 * A status of good or some check conditions could
3883 * be considered a successful command.
3885 host_status = DID_ERROR;
3888 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3892 cmnd->result = host_status << 16 | scsi_status;
3893 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
3897 * lpfc_sli4_scmd_to_wqidx_distr - scsi command to SLI4 WQ index distribution
3898 * @phba: Pointer to HBA context object.
3900 * This routine performs a roundrobin SCSI command to SLI4 FCP WQ index
3901 * distribution. This is called by __lpfc_sli_issue_iocb_s4() with the hbalock
3903 * If scsi-mq is enabled, get the default block layer mapping of software queues
3904 * to hardware queues. This information is saved in request tag.
3906 * Return: index into SLI4 fast-path FCP queue index.
3908 int lpfc_sli4_scmd_to_wqidx_distr(struct lpfc_hba *phba,
3909 struct lpfc_scsi_buf *lpfc_cmd)
3911 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3912 struct lpfc_vector_map_info *cpup;
3917 if (cmnd && shost_use_blk_mq(cmnd->device->host)) {
3918 tag = blk_mq_unique_tag(cmnd->request);
3919 hwq = blk_mq_unique_tag_to_hwq(tag);
3924 if (phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_CPU
3925 && phba->cfg_fcp_io_channel > 1) {
3926 cpu = smp_processor_id();
3927 if (cpu < phba->sli4_hba.num_present_cpu) {
3928 cpup = phba->sli4_hba.cpu_map;
3930 return cpup->channel_id;
3933 chann = atomic_add_return(1, &phba->fcp_qidx);
3934 chann = chann % phba->cfg_fcp_io_channel;
3940 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
3941 * @phba: The Hba for which this call is being executed.
3942 * @pIocbIn: The command IOCBQ for the scsi cmnd.
3943 * @pIocbOut: The response IOCBQ for the scsi cmnd.
3945 * This routine assigns scsi command result by looking into response IOCB
3946 * status field appropriately. This routine handles QUEUE FULL condition as
3947 * well by ramping down device queue depth.
3950 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3951 struct lpfc_iocbq *pIocbOut)
3953 struct lpfc_scsi_buf *lpfc_cmd =
3954 (struct lpfc_scsi_buf *) pIocbIn->context1;
3955 struct lpfc_vport *vport = pIocbIn->vport;
3956 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3957 struct lpfc_nodelist *pnode = rdata->pnode;
3958 struct scsi_cmnd *cmd;
3959 unsigned long flags;
3960 struct lpfc_fast_path_event *fast_path_evt;
3961 struct Scsi_Host *shost;
3962 uint32_t logit = LOG_FCP;
3964 atomic_inc(&phba->fc4ScsiIoCmpls);
3966 /* Sanity check on return of outstanding command */
3967 cmd = lpfc_cmd->pCmd;
3970 shost = cmd->device->host;
3972 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
3973 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
3974 /* pick up SLI4 exhange busy status from HBA */
3975 lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
3977 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3978 if (lpfc_cmd->prot_data_type) {
3979 struct scsi_dif_tuple *src = NULL;
3981 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3983 * Used to restore any changes to protection
3984 * data for error injection.
3986 switch (lpfc_cmd->prot_data_type) {
3987 case LPFC_INJERR_REFTAG:
3989 lpfc_cmd->prot_data;
3991 case LPFC_INJERR_APPTAG:
3993 (uint16_t)lpfc_cmd->prot_data;
3995 case LPFC_INJERR_GUARD:
3997 (uint16_t)lpfc_cmd->prot_data;
4003 lpfc_cmd->prot_data = 0;
4004 lpfc_cmd->prot_data_type = 0;
4005 lpfc_cmd->prot_data_segment = NULL;
4009 if (lpfc_cmd->status) {
4010 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
4011 (lpfc_cmd->result & IOERR_DRVR_MASK))
4012 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
4013 else if (lpfc_cmd->status >= IOSTAT_CNT)
4014 lpfc_cmd->status = IOSTAT_DEFAULT;
4015 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
4016 !lpfc_cmd->fcp_rsp->rspStatus3 &&
4017 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
4018 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
4021 logit = LOG_FCP | LOG_FCP_UNDER;
4022 lpfc_printf_vlog(vport, KERN_WARNING, logit,
4023 "9030 FCP cmd x%x failed <%d/%lld> "
4024 "status: x%x result: x%x "
4025 "sid: x%x did: x%x oxid: x%x "
4028 cmd->device ? cmd->device->id : 0xffff,
4029 cmd->device ? cmd->device->lun : 0xffff,
4030 lpfc_cmd->status, lpfc_cmd->result,
4032 (pnode) ? pnode->nlp_DID : 0,
4033 phba->sli_rev == LPFC_SLI_REV4 ?
4034 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4035 pIocbOut->iocb.ulpContext,
4036 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
4038 switch (lpfc_cmd->status) {
4039 case IOSTAT_FCP_RSP_ERROR:
4040 /* Call FCP RSP handler to determine result */
4041 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
4043 case IOSTAT_NPORT_BSY:
4044 case IOSTAT_FABRIC_BSY:
4045 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
4046 fast_path_evt = lpfc_alloc_fast_evt(phba);
4049 fast_path_evt->un.fabric_evt.event_type =
4050 FC_REG_FABRIC_EVENT;
4051 fast_path_evt->un.fabric_evt.subcategory =
4052 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
4053 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
4054 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4055 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
4056 &pnode->nlp_portname,
4057 sizeof(struct lpfc_name));
4058 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
4059 &pnode->nlp_nodename,
4060 sizeof(struct lpfc_name));
4062 fast_path_evt->vport = vport;
4063 fast_path_evt->work_evt.evt =
4064 LPFC_EVT_FASTPATH_MGMT_EVT;
4065 spin_lock_irqsave(&phba->hbalock, flags);
4066 list_add_tail(&fast_path_evt->work_evt.evt_listp,
4068 spin_unlock_irqrestore(&phba->hbalock, flags);
4069 lpfc_worker_wake_up(phba);
4071 case IOSTAT_LOCAL_REJECT:
4072 case IOSTAT_REMOTE_STOP:
4073 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
4075 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
4076 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
4078 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
4079 cmd->result = DID_NO_CONNECT << 16;
4082 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
4083 lpfc_cmd->result == IOERR_NO_RESOURCES ||
4084 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
4085 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
4086 cmd->result = DID_REQUEUE << 16;
4089 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
4090 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
4091 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
4092 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
4094 * This is a response for a BG enabled
4095 * cmd. Parse BG error
4097 lpfc_parse_bg_err(phba, lpfc_cmd,
4101 lpfc_printf_vlog(vport, KERN_WARNING,
4103 "9031 non-zero BGSTAT "
4104 "on unprotected cmd\n");
4107 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
4108 && (phba->sli_rev == LPFC_SLI_REV4)
4109 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
4110 /* This IO was aborted by the target, we don't
4111 * know the rxid and because we did not send the
4112 * ABTS we cannot generate and RRQ.
4114 lpfc_set_rrq_active(phba, pnode,
4115 lpfc_cmd->cur_iocbq.sli4_lxritag,
4118 /* else: fall through */
4120 cmd->result = DID_ERROR << 16;
4124 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
4125 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
4126 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
4129 cmd->result = DID_OK << 16;
4131 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
4132 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4134 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4135 "0710 Iodone <%d/%llu> cmd %p, error "
4136 "x%x SNS x%x x%x Data: x%x x%x\n",
4137 cmd->device->id, cmd->device->lun, cmd,
4138 cmd->result, *lp, *(lp + 3), cmd->retries,
4139 scsi_get_resid(cmd));
4142 lpfc_update_stats(phba, lpfc_cmd);
4143 if (vport->cfg_max_scsicmpl_time &&
4144 time_after(jiffies, lpfc_cmd->start_time +
4145 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4146 spin_lock_irqsave(shost->host_lock, flags);
4147 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4148 if (pnode->cmd_qdepth >
4149 atomic_read(&pnode->cmd_pending) &&
4150 (atomic_read(&pnode->cmd_pending) >
4151 LPFC_MIN_TGT_QDEPTH) &&
4152 ((cmd->cmnd[0] == READ_10) ||
4153 (cmd->cmnd[0] == WRITE_10)))
4155 atomic_read(&pnode->cmd_pending);
4157 pnode->last_change_time = jiffies;
4159 spin_unlock_irqrestore(shost->host_lock, flags);
4161 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4163 /* If pCmd was set to NULL from abort path, do not call scsi_done */
4164 if (xchg(&lpfc_cmd->pCmd, NULL) == NULL) {
4165 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4166 "0711 FCP cmd already NULL, sid: 0x%06x, "
4167 "did: 0x%06x, oxid: 0x%04x\n",
4169 (pnode) ? pnode->nlp_DID : 0,
4170 phba->sli_rev == LPFC_SLI_REV4 ?
4171 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff);
4175 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4176 cmd->scsi_done(cmd);
4179 * If there is a thread waiting for command completion
4180 * wake up the thread.
4182 spin_lock_irqsave(shost->host_lock, flags);
4183 if (lpfc_cmd->waitq)
4184 wake_up(lpfc_cmd->waitq);
4185 spin_unlock_irqrestore(shost->host_lock, flags);
4187 lpfc_release_scsi_buf(phba, lpfc_cmd);
4191 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4192 * @data: A pointer to the immediate command data portion of the IOCB.
4193 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4195 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4196 * byte swapping the data to big endian format for transmission on the wire.
4199 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4202 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4203 i += sizeof(uint32_t), j++) {
4204 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4209 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
4210 * @vport: The virtual port for which this call is being executed.
4211 * @lpfc_cmd: The scsi command which needs to send.
4212 * @pnode: Pointer to lpfc_nodelist.
4214 * This routine initializes fcp_cmnd and iocb data structure from scsi command
4215 * to transfer for device with SLI3 interface spec.
4218 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
4219 struct lpfc_nodelist *pnode)
4221 struct lpfc_hba *phba = vport->phba;
4222 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4223 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4224 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4225 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4226 int datadir = scsi_cmnd->sc_data_direction;
4231 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4234 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
4235 /* clear task management bits */
4236 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
4238 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4239 &lpfc_cmd->fcp_cmnd->fcp_lun);
4241 ptr = &fcp_cmnd->fcpCdb[0];
4242 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4243 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4244 ptr += scsi_cmnd->cmd_len;
4245 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4248 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4250 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4251 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
4254 * There are three possibilities here - use scatter-gather segment, use
4255 * the single mapping, or neither. Start the lpfc command prep by
4256 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4259 if (scsi_sg_count(scsi_cmnd)) {
4260 if (datadir == DMA_TO_DEVICE) {
4261 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
4262 iocb_cmd->ulpPU = PARM_READ_CHECK;
4263 if (vport->cfg_first_burst_size &&
4264 (pnode->nlp_flag & NLP_FIRSTBURST)) {
4265 fcpdl = scsi_bufflen(scsi_cmnd);
4266 if (fcpdl < vport->cfg_first_burst_size)
4267 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4269 piocbq->iocb.un.fcpi.fcpi_XRdy =
4270 vport->cfg_first_burst_size;
4272 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4273 atomic_inc(&phba->fc4ScsiOutputRequests);
4275 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4276 iocb_cmd->ulpPU = PARM_READ_CHECK;
4277 fcp_cmnd->fcpCntl3 = READ_DATA;
4278 atomic_inc(&phba->fc4ScsiInputRequests);
4281 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4282 iocb_cmd->un.fcpi.fcpi_parm = 0;
4283 iocb_cmd->ulpPU = 0;
4284 fcp_cmnd->fcpCntl3 = 0;
4285 atomic_inc(&phba->fc4ScsiControlRequests);
4287 if (phba->sli_rev == 3 &&
4288 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4289 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
4291 * Finish initializing those IOCB fields that are independent
4292 * of the scsi_cmnd request_buffer
4294 piocbq->iocb.ulpContext = pnode->nlp_rpi;
4296 piocbq->iocb.ulpContext =
4297 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
4298 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4299 piocbq->iocb.ulpFCP2Rcvy = 1;
4301 piocbq->iocb.ulpFCP2Rcvy = 0;
4303 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4304 piocbq->context1 = lpfc_cmd;
4305 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4306 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
4307 piocbq->vport = vport;
4311 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
4312 * @vport: The virtual port for which this call is being executed.
4313 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4314 * @lun: Logical unit number.
4315 * @task_mgmt_cmd: SCSI task management command.
4317 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4318 * for device with SLI-3 interface spec.
4325 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
4326 struct lpfc_scsi_buf *lpfc_cmd,
4328 uint8_t task_mgmt_cmd)
4330 struct lpfc_iocbq *piocbq;
4332 struct fcp_cmnd *fcp_cmnd;
4333 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4334 struct lpfc_nodelist *ndlp = rdata->pnode;
4336 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4337 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
4340 piocbq = &(lpfc_cmd->cur_iocbq);
4341 piocbq->vport = vport;
4343 piocb = &piocbq->iocb;
4345 fcp_cmnd = lpfc_cmd->fcp_cmnd;
4346 /* Clear out any old data in the FCP command area */
4347 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4348 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
4349 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
4350 if (vport->phba->sli_rev == 3 &&
4351 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4352 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
4353 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
4354 piocb->ulpContext = ndlp->nlp_rpi;
4355 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4357 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4359 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
4360 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4362 piocb->un.fcpi.fcpi_parm = 0;
4364 /* ulpTimeout is only one byte */
4365 if (lpfc_cmd->timeout > 0xff) {
4367 * Do not timeout the command at the firmware level.
4368 * The driver will provide the timeout mechanism.
4370 piocb->ulpTimeout = 0;
4372 piocb->ulpTimeout = lpfc_cmd->timeout;
4374 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4375 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
4381 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
4382 * @phba: The hba struct for which this call is being executed.
4383 * @dev_grp: The HBA PCI-Device group number.
4385 * This routine sets up the SCSI interface API function jump table in @phba
4387 * Returns: 0 - success, -ENODEV - failure.
4390 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4393 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4394 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
4397 case LPFC_PCI_DEV_LP:
4398 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
4399 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
4400 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
4401 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
4402 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
4404 case LPFC_PCI_DEV_OC:
4405 phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
4406 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
4407 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
4408 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
4409 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
4412 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4413 "1418 Invalid HBA PCI-device group: 0x%x\n",
4418 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
4419 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4424 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
4425 * @phba: The Hba for which this call is being executed.
4426 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4427 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4429 * This routine is IOCB completion routine for device reset and target reset
4430 * routine. This routine release scsi buffer associated with lpfc_cmd.
4433 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4434 struct lpfc_iocbq *cmdiocbq,
4435 struct lpfc_iocbq *rspiocbq)
4437 struct lpfc_scsi_buf *lpfc_cmd =
4438 (struct lpfc_scsi_buf *) cmdiocbq->context1;
4440 lpfc_release_scsi_buf(phba, lpfc_cmd);
4445 * lpfc_info - Info entry point of scsi_host_template data structure
4446 * @host: The scsi host for which this call is being executed.
4448 * This routine provides module information about hba.
4451 * Pointer to char - Success.
4454 lpfc_info(struct Scsi_Host *host)
4456 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4457 struct lpfc_hba *phba = vport->phba;
4458 int len, link_speed = 0;
4459 static char lpfcinfobuf[384];
4461 memset(lpfcinfobuf,0,384);
4462 if (phba && phba->pcidev){
4463 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
4464 len = strlen(lpfcinfobuf);
4465 snprintf(lpfcinfobuf + len,
4467 " on PCI bus %02x device %02x irq %d",
4468 phba->pcidev->bus->number,
4469 phba->pcidev->devfn,
4471 len = strlen(lpfcinfobuf);
4472 if (phba->Port[0]) {
4473 snprintf(lpfcinfobuf + len,
4478 len = strlen(lpfcinfobuf);
4479 link_speed = lpfc_sli_port_speed_get(phba);
4480 if (link_speed != 0)
4481 snprintf(lpfcinfobuf + len, 384-len,
4482 " Logical Link Speed: %d Mbps", link_speed);
4488 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
4489 * @phba: The Hba for which this call is being executed.
4491 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4492 * The default value of cfg_poll_tmo is 10 milliseconds.
4494 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4496 unsigned long poll_tmo_expires =
4497 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4499 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
4500 mod_timer(&phba->fcp_poll_timer,
4505 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
4506 * @phba: The Hba for which this call is being executed.
4508 * This routine starts the fcp_poll_timer of @phba.
4510 void lpfc_poll_start_timer(struct lpfc_hba * phba)
4512 lpfc_poll_rearm_timer(phba);
4516 * lpfc_poll_timeout - Restart polling timer
4517 * @ptr: Map to lpfc_hba data structure pointer.
4519 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4520 * and FCP Ring interrupt is disable.
4523 void lpfc_poll_timeout(struct timer_list *t)
4525 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
4527 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4528 lpfc_sli_handle_fast_ring_event(phba,
4529 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4531 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4532 lpfc_poll_rearm_timer(phba);
4537 * lpfc_queuecommand - scsi_host_template queuecommand entry point
4538 * @cmnd: Pointer to scsi_cmnd data structure.
4539 * @done: Pointer to done routine.
4541 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4542 * This routine prepares an IOCB from scsi command and provides to firmware.
4543 * The @done callback is invoked after driver finished processing the command.
4547 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4550 lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
4552 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4553 struct lpfc_hba *phba = vport->phba;
4554 struct lpfc_rport_data *rdata;
4555 struct lpfc_nodelist *ndlp;
4556 struct lpfc_scsi_buf *lpfc_cmd;
4557 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
4560 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
4562 /* sanity check on references */
4563 if (unlikely(!rdata) || unlikely(!rport))
4564 goto out_fail_command;
4566 err = fc_remote_port_chkready(rport);
4569 goto out_fail_command;
4571 ndlp = rdata->pnode;
4573 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
4574 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
4576 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4577 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4578 " op:%02x str=%s without registering for"
4579 " BlockGuard - Rejecting command\n",
4580 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4581 dif_op_str[scsi_get_prot_op(cmnd)]);
4582 goto out_fail_command;
4586 * Catch race where our node has transitioned, but the
4587 * transport is still transitioning.
4589 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4591 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4592 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4593 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4594 "3377 Target Queue Full, scsi Id:%d "
4595 "Qdepth:%d Pending command:%d"
4596 " WWNN:%02x:%02x:%02x:%02x:"
4597 "%02x:%02x:%02x:%02x, "
4598 " WWPN:%02x:%02x:%02x:%02x:"
4599 "%02x:%02x:%02x:%02x",
4600 ndlp->nlp_sid, ndlp->cmd_qdepth,
4601 atomic_read(&ndlp->cmd_pending),
4602 ndlp->nlp_nodename.u.wwn[0],
4603 ndlp->nlp_nodename.u.wwn[1],
4604 ndlp->nlp_nodename.u.wwn[2],
4605 ndlp->nlp_nodename.u.wwn[3],
4606 ndlp->nlp_nodename.u.wwn[4],
4607 ndlp->nlp_nodename.u.wwn[5],
4608 ndlp->nlp_nodename.u.wwn[6],
4609 ndlp->nlp_nodename.u.wwn[7],
4610 ndlp->nlp_portname.u.wwn[0],
4611 ndlp->nlp_portname.u.wwn[1],
4612 ndlp->nlp_portname.u.wwn[2],
4613 ndlp->nlp_portname.u.wwn[3],
4614 ndlp->nlp_portname.u.wwn[4],
4615 ndlp->nlp_portname.u.wwn[5],
4616 ndlp->nlp_portname.u.wwn[6],
4617 ndlp->nlp_portname.u.wwn[7]);
4622 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
4623 if (lpfc_cmd == NULL) {
4624 lpfc_rampdown_queue_depth(phba);
4626 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4627 "0707 driver's buffer pool is empty, "
4633 * Store the midlayer's command structure for the completion phase
4634 * and complete the command initialization.
4636 lpfc_cmd->pCmd = cmnd;
4637 lpfc_cmd->rdata = rdata;
4638 lpfc_cmd->ndlp = ndlp;
4639 lpfc_cmd->timeout = 0;
4640 lpfc_cmd->start_time = jiffies;
4641 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4643 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
4644 if (vport->phba->cfg_enable_bg) {
4645 lpfc_printf_vlog(vport,
4646 KERN_INFO, LOG_SCSI_CMD,
4647 "9033 BLKGRD: rcvd %s cmd:x%x "
4648 "sector x%llx cnt %u pt %x\n",
4649 dif_op_str[scsi_get_prot_op(cmnd)],
4651 (unsigned long long)scsi_get_lba(cmnd),
4652 blk_rq_sectors(cmnd->request),
4653 (cmnd->cmnd[1]>>5));
4655 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4657 if (vport->phba->cfg_enable_bg) {
4658 lpfc_printf_vlog(vport,
4659 KERN_INFO, LOG_SCSI_CMD,
4660 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4661 "x%x sector x%llx cnt %u pt %x\n",
4663 (unsigned long long)scsi_get_lba(cmnd),
4664 blk_rq_sectors(cmnd->request),
4665 (cmnd->cmnd[1]>>5));
4667 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4671 goto out_host_busy_free_buf;
4673 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
4675 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4676 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
4678 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4679 "3376 FCP could not issue IOCB err %x"
4680 "FCP cmd x%x <%d/%llu> "
4681 "sid: x%x did: x%x oxid: x%x "
4682 "Data: x%x x%x x%x x%x\n",
4684 cmnd->device ? cmnd->device->id : 0xffff,
4685 cmnd->device ? cmnd->device->lun : (u64) -1,
4686 vport->fc_myDID, ndlp->nlp_DID,
4687 phba->sli_rev == LPFC_SLI_REV4 ?
4688 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4689 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4690 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4691 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4693 (cmnd->request->timeout / 1000));
4695 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4697 atomic_dec(&phba->fc4ScsiOutputRequests);
4700 atomic_dec(&phba->fc4ScsiInputRequests);
4703 atomic_dec(&phba->fc4ScsiControlRequests);
4705 goto out_host_busy_free_buf;
4707 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4708 lpfc_sli_handle_fast_ring_event(phba,
4709 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4711 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4712 lpfc_poll_rearm_timer(phba);
4717 out_host_busy_free_buf:
4718 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4719 lpfc_release_scsi_buf(phba, lpfc_cmd);
4721 return SCSI_MLQUEUE_HOST_BUSY;
4724 return SCSI_MLQUEUE_TARGET_BUSY;
4727 cmnd->scsi_done(cmnd);
4733 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
4734 * @cmnd: Pointer to scsi_cmnd data structure.
4736 * This routine aborts @cmnd pending in base driver.
4743 lpfc_abort_handler(struct scsi_cmnd *cmnd)
4745 struct Scsi_Host *shost = cmnd->device->host;
4746 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4747 struct lpfc_hba *phba = vport->phba;
4748 struct lpfc_iocbq *iocb;
4749 struct lpfc_iocbq *abtsiocb;
4750 struct lpfc_scsi_buf *lpfc_cmd;
4752 int ret = SUCCESS, status = 0;
4753 struct lpfc_sli_ring *pring_s4 = NULL;
4755 unsigned long flags;
4756 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
4758 status = fc_block_scsi_eh(cmnd);
4759 if (status != 0 && status != SUCCESS)
4762 spin_lock_irqsave(&phba->hbalock, flags);
4763 /* driver queued commands are in process of being flushed */
4764 if (phba->hba_flag & HBA_FCP_IOQ_FLUSH) {
4765 spin_unlock_irqrestore(&phba->hbalock, flags);
4766 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4767 "3168 SCSI Layer abort requested I/O has been "
4768 "flushed by LLD.\n");
4772 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
4773 if (!lpfc_cmd || !lpfc_cmd->pCmd) {
4774 spin_unlock_irqrestore(&phba->hbalock, flags);
4775 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4776 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
4777 "x%x ID %d LUN %llu\n",
4778 SUCCESS, cmnd->device->id, cmnd->device->lun);
4782 iocb = &lpfc_cmd->cur_iocbq;
4783 if (phba->sli_rev == LPFC_SLI_REV4) {
4784 if (!(phba->cfg_fof) ||
4785 (!(iocb->iocb_flag & LPFC_IO_FOF))) {
4787 phba->sli4_hba.fcp_wq[iocb->hba_wqidx]->pring;
4789 iocb->hba_wqidx = 0;
4790 pring_s4 = phba->sli4_hba.oas_wq->pring;
4796 spin_lock(&pring_s4->ring_lock);
4798 /* the command is in process of being cancelled */
4799 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4800 if (phba->sli_rev == LPFC_SLI_REV4)
4801 spin_unlock(&pring_s4->ring_lock);
4802 spin_unlock_irqrestore(&phba->hbalock, flags);
4803 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4804 "3169 SCSI Layer abort requested I/O has been "
4805 "cancelled by LLD.\n");
4809 * If pCmd field of the corresponding lpfc_scsi_buf structure
4810 * points to a different SCSI command, then the driver has
4811 * already completed this command, but the midlayer did not
4812 * see the completion before the eh fired. Just return SUCCESS.
4814 if (lpfc_cmd->pCmd != cmnd) {
4815 if (phba->sli_rev == LPFC_SLI_REV4)
4816 spin_unlock(&pring_s4->ring_lock);
4817 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4818 "3170 SCSI Layer abort requested I/O has been "
4819 "completed by LLD.\n");
4823 BUG_ON(iocb->context1 != lpfc_cmd);
4825 /* abort issued in recovery is still in progress */
4826 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4827 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4828 "3389 SCSI Layer I/O Abort Request is pending\n");
4829 if (phba->sli_rev == LPFC_SLI_REV4)
4830 spin_unlock(&pring_s4->ring_lock);
4831 spin_unlock_irqrestore(&phba->hbalock, flags);
4835 abtsiocb = __lpfc_sli_get_iocbq(phba);
4836 if (abtsiocb == NULL) {
4838 if (phba->sli_rev == LPFC_SLI_REV4)
4839 spin_unlock(&pring_s4->ring_lock);
4843 /* Indicate the IO is being aborted by the driver. */
4844 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4847 * The scsi command can not be in txq and it is in flight because the
4848 * pCmd is still pointig at the SCSI command we have to abort. There
4849 * is no need to search the txcmplq. Just send an abort to the FW.
4853 icmd = &abtsiocb->iocb;
4854 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4855 icmd->un.acxri.abortContextTag = cmd->ulpContext;
4856 if (phba->sli_rev == LPFC_SLI_REV4)
4857 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4859 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
4862 icmd->ulpClass = cmd->ulpClass;
4864 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4865 abtsiocb->hba_wqidx = iocb->hba_wqidx;
4866 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
4867 if (iocb->iocb_flag & LPFC_IO_FOF)
4868 abtsiocb->iocb_flag |= LPFC_IO_FOF;
4870 if (lpfc_is_link_up(phba))
4871 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4873 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
4875 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
4876 abtsiocb->vport = vport;
4877 lpfc_cmd->waitq = &waitq;
4878 if (phba->sli_rev == LPFC_SLI_REV4) {
4879 /* Note: both hbalock and ring_lock must be set here */
4880 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4882 spin_unlock(&pring_s4->ring_lock);
4884 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4887 /* no longer need the lock after this point */
4888 spin_unlock_irqrestore(&phba->hbalock, flags);
4891 if (ret_val == IOCB_ERROR) {
4892 if (phba->sli_rev == LPFC_SLI_REV4)
4893 spin_lock_irqsave(&pring_s4->ring_lock, flags);
4895 spin_lock_irqsave(&phba->hbalock, flags);
4896 /* Indicate the IO is not being aborted by the driver. */
4897 iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4898 lpfc_cmd->waitq = NULL;
4899 if (phba->sli_rev == LPFC_SLI_REV4)
4900 spin_unlock_irqrestore(&pring_s4->ring_lock, flags);
4902 spin_unlock_irqrestore(&phba->hbalock, flags);
4903 lpfc_sli_release_iocbq(phba, abtsiocb);
4908 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4909 lpfc_sli_handle_fast_ring_event(phba,
4910 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4913 /* Wait for abort to complete */
4914 wait_event_timeout(waitq,
4915 (lpfc_cmd->pCmd != cmnd),
4916 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
4918 spin_lock_irqsave(shost->host_lock, flags);
4919 lpfc_cmd->waitq = NULL;
4920 spin_unlock_irqrestore(shost->host_lock, flags);
4922 if (lpfc_cmd->pCmd == cmnd) {
4924 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4925 "0748 abort handler timed out waiting "
4926 "for aborting I/O (xri:x%x) to complete: "
4927 "ret %#x, ID %d, LUN %llu\n",
4928 iocb->sli4_xritag, ret,
4929 cmnd->device->id, cmnd->device->lun);
4934 spin_unlock_irqrestore(&phba->hbalock, flags);
4936 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4937 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
4938 "LUN %llu\n", ret, cmnd->device->id,
4944 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4946 switch (task_mgmt_cmd) {
4947 case FCP_ABORT_TASK_SET:
4948 return "ABORT_TASK_SET";
4949 case FCP_CLEAR_TASK_SET:
4950 return "FCP_CLEAR_TASK_SET";
4952 return "FCP_BUS_RESET";
4954 return "FCP_LUN_RESET";
4955 case FCP_TARGET_RESET:
4956 return "FCP_TARGET_RESET";
4958 return "FCP_CLEAR_ACA";
4959 case FCP_TERMINATE_TASK:
4960 return "FCP_TERMINATE_TASK";
4968 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4969 * @vport: The virtual port for which this call is being executed.
4970 * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
4972 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4979 lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd)
4981 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4984 uint8_t rsp_info_code;
4989 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4990 "0703 fcp_rsp is missing\n");
4992 rsp_info = fcprsp->rspStatus2;
4993 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4994 rsp_info_code = fcprsp->rspInfo3;
4997 lpfc_printf_vlog(vport, KERN_INFO,
4999 "0706 fcp_rsp valid 0x%x,"
5000 " rsp len=%d code 0x%x\n",
5002 rsp_len, rsp_info_code);
5004 if ((fcprsp->rspStatus2&RSP_LEN_VALID) && (rsp_len == 8)) {
5005 switch (rsp_info_code) {
5006 case RSP_NO_FAILURE:
5007 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5008 "0715 Task Mgmt No Failure\n");
5011 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
5012 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5013 "0716 Task Mgmt Target "
5016 case RSP_TM_NOT_COMPLETED: /* TM failed */
5017 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5018 "0717 Task Mgmt Target "
5021 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
5022 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5023 "0718 Task Mgmt to invalid "
5034 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5035 * @vport: The virtual port for which this call is being executed.
5036 * @rdata: Pointer to remote port local data
5037 * @tgt_id: Target ID of remote device.
5038 * @lun_id: Lun number for the TMF
5039 * @task_mgmt_cmd: type of TMF to send
5041 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5049 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
5050 unsigned int tgt_id, uint64_t lun_id,
5051 uint8_t task_mgmt_cmd)
5053 struct lpfc_hba *phba = vport->phba;
5054 struct lpfc_scsi_buf *lpfc_cmd;
5055 struct lpfc_iocbq *iocbq;
5056 struct lpfc_iocbq *iocbqrsp;
5057 struct lpfc_rport_data *rdata;
5058 struct lpfc_nodelist *pnode;
5062 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5063 if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
5065 pnode = rdata->pnode;
5067 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode);
5068 if (lpfc_cmd == NULL)
5070 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
5071 lpfc_cmd->rdata = rdata;
5072 lpfc_cmd->pCmd = cmnd;
5073 lpfc_cmd->ndlp = pnode;
5075 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5078 lpfc_release_scsi_buf(phba, lpfc_cmd);
5082 iocbq = &lpfc_cmd->cur_iocbq;
5083 iocbqrsp = lpfc_sli_get_iocbq(phba);
5084 if (iocbqrsp == NULL) {
5085 lpfc_release_scsi_buf(phba, lpfc_cmd);
5088 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
5090 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5091 "0702 Issue %s to TGT %d LUN %llu "
5092 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
5093 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
5094 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5097 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5098 iocbq, iocbqrsp, lpfc_cmd->timeout);
5099 if ((status != IOCB_SUCCESS) ||
5100 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
5101 if (status != IOCB_SUCCESS ||
5102 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
5103 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5104 "0727 TMF %s to TGT %d LUN %llu "
5105 "failed (%d, %d) iocb_flag x%x\n",
5106 lpfc_taskmgmt_name(task_mgmt_cmd),
5108 iocbqrsp->iocb.ulpStatus,
5109 iocbqrsp->iocb.un.ulpWord[4],
5111 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5112 if (status == IOCB_SUCCESS) {
5113 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5114 /* Something in the FCP_RSP was invalid.
5115 * Check conditions */
5116 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5119 } else if (status == IOCB_TIMEDOUT) {
5120 ret = TIMEOUT_ERROR;
5127 lpfc_sli_release_iocbq(phba, iocbqrsp);
5129 if (ret != TIMEOUT_ERROR)
5130 lpfc_release_scsi_buf(phba, lpfc_cmd);
5136 * lpfc_chk_tgt_mapped -
5137 * @vport: The virtual port to check on
5138 * @cmnd: Pointer to scsi_cmnd data structure.
5140 * This routine delays until the scsi target (aka rport) for the
5141 * command exists (is present and logged in) or we declare it non-existent.
5148 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5150 struct lpfc_rport_data *rdata;
5151 struct lpfc_nodelist *pnode;
5152 unsigned long later;
5154 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5156 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5157 "0797 Tgt Map rport failure: rdata x%p\n", rdata);
5160 pnode = rdata->pnode;
5162 * If target is not in a MAPPED state, delay until
5163 * target is rediscovered or devloss timeout expires.
5165 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5166 while (time_after(later, jiffies)) {
5167 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5169 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5171 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5172 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5175 pnode = rdata->pnode;
5177 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5178 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5184 * lpfc_reset_flush_io_context -
5185 * @vport: The virtual port (scsi_host) for the flush context
5186 * @tgt_id: If aborting by Target contect - specifies the target id
5187 * @lun_id: If aborting by Lun context - specifies the lun id
5188 * @context: specifies the context level to flush at.
5190 * After a reset condition via TMF, we need to flush orphaned i/o
5191 * contexts from the adapter. This routine aborts any contexts
5192 * outstanding, then waits for their completions. The wait is
5193 * bounded by devloss_tmo though.
5200 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5201 uint64_t lun_id, lpfc_ctx_cmd context)
5203 struct lpfc_hba *phba = vport->phba;
5204 unsigned long later;
5207 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5209 lpfc_sli_abort_taskmgmt(vport,
5210 &phba->sli.sli3_ring[LPFC_FCP_RING],
5211 tgt_id, lun_id, context);
5212 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5213 while (time_after(later, jiffies) && cnt) {
5214 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5215 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5218 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5219 "0724 I/O flush failure for context %s : cnt x%x\n",
5220 ((context == LPFC_CTX_LUN) ? "LUN" :
5221 ((context == LPFC_CTX_TGT) ? "TGT" :
5222 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5230 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
5231 * @cmnd: Pointer to scsi_cmnd data structure.
5233 * This routine does a device reset by sending a LUN_RESET task management
5241 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
5243 struct Scsi_Host *shost = cmnd->device->host;
5244 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5245 struct lpfc_rport_data *rdata;
5246 struct lpfc_nodelist *pnode;
5247 unsigned tgt_id = cmnd->device->id;
5248 uint64_t lun_id = cmnd->device->lun;
5249 struct lpfc_scsi_event_header scsi_event;
5252 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5253 if (!rdata || !rdata->pnode) {
5254 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5255 "0798 Device Reset rport failure: rdata x%p\n",
5259 pnode = rdata->pnode;
5260 status = fc_block_scsi_eh(cmnd);
5261 if (status != 0 && status != SUCCESS)
5264 status = lpfc_chk_tgt_mapped(vport, cmnd);
5265 if (status == FAILED) {
5266 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5267 "0721 Device Reset rport failure: rdata x%p\n", rdata);
5271 scsi_event.event_type = FC_REG_SCSI_EVENT;
5272 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5273 scsi_event.lun = lun_id;
5274 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5275 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5277 fc_host_post_vendor_event(shost, fc_get_event_number(),
5278 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5280 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
5283 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5284 "0713 SCSI layer issued Device Reset (%d, %llu) "
5285 "return x%x\n", tgt_id, lun_id, status);
5288 * We have to clean up i/o as : they may be orphaned by the TMF;
5289 * or if the TMF failed, they may be in an indeterminate state.
5291 * We will report success if all the i/o aborts successfully.
5293 if (status == SUCCESS)
5294 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5301 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5302 * @cmnd: Pointer to scsi_cmnd data structure.
5304 * This routine does a target reset by sending a TARGET_RESET task management
5312 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5314 struct Scsi_Host *shost = cmnd->device->host;
5315 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5316 struct lpfc_rport_data *rdata;
5317 struct lpfc_nodelist *pnode;
5318 unsigned tgt_id = cmnd->device->id;
5319 uint64_t lun_id = cmnd->device->lun;
5320 struct lpfc_scsi_event_header scsi_event;
5323 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5325 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5326 "0799 Target Reset rport failure: rdata x%p\n", rdata);
5329 pnode = rdata->pnode;
5330 status = fc_block_scsi_eh(cmnd);
5331 if (status != 0 && status != SUCCESS)
5334 status = lpfc_chk_tgt_mapped(vport, cmnd);
5335 if (status == FAILED) {
5336 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5337 "0722 Target Reset rport failure: rdata x%p\n", rdata);
5339 spin_lock_irq(shost->host_lock);
5340 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5341 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5342 spin_unlock_irq(shost->host_lock);
5344 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5346 return FAST_IO_FAIL;
5349 scsi_event.event_type = FC_REG_SCSI_EVENT;
5350 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5352 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5353 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5355 fc_host_post_vendor_event(shost, fc_get_event_number(),
5356 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5358 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
5361 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5362 "0723 SCSI layer issued Target Reset (%d, %llu) "
5363 "return x%x\n", tgt_id, lun_id, status);
5366 * We have to clean up i/o as : they may be orphaned by the TMF;
5367 * or if the TMF failed, they may be in an indeterminate state.
5369 * We will report success if all the i/o aborts successfully.
5371 if (status == SUCCESS)
5372 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5378 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
5379 * @cmnd: Pointer to scsi_cmnd data structure.
5381 * This routine does target reset to all targets on @cmnd->device->host.
5382 * This emulates Parallel SCSI Bus Reset Semantics.
5389 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
5391 struct Scsi_Host *shost = cmnd->device->host;
5392 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5393 struct lpfc_nodelist *ndlp = NULL;
5394 struct lpfc_scsi_event_header scsi_event;
5396 int ret = SUCCESS, status, i;
5398 scsi_event.event_type = FC_REG_SCSI_EVENT;
5399 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5401 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5402 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5404 fc_host_post_vendor_event(shost, fc_get_event_number(),
5405 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5407 status = fc_block_scsi_eh(cmnd);
5408 if (status != 0 && status != SUCCESS)
5412 * Since the driver manages a single bus device, reset all
5413 * targets known to the driver. Should any target reset
5414 * fail, this routine returns failure to the midlayer.
5416 for (i = 0; i < LPFC_MAX_TARGET; i++) {
5417 /* Search for mapped node by target ID */
5419 spin_lock_irq(shost->host_lock);
5420 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5421 if (!NLP_CHK_NODE_ACT(ndlp))
5423 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5424 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5426 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
5427 ndlp->nlp_sid == i &&
5429 ndlp->nlp_type & NLP_FCP_TARGET) {
5434 spin_unlock_irq(shost->host_lock);
5438 status = lpfc_send_taskmgmt(vport, cmnd,
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",
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)
5459 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5460 "0714 SCSI layer issued Bus Reset Data: x%x\n", 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.
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);
5493 rc = lpfc_sli_brdrestart(phba);
5496 rc = lpfc_online(phba);
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);
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.
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));
5529 uint32_t num_to_alloc = 0;
5530 int num_allocated = 0;
5532 struct lpfc_device_data *device_data;
5533 unsigned long flags;
5534 struct lpfc_name target_wwpn;
5536 if (!rport || fc_remote_port_chkready(rport))
5539 if (phba->cfg_fof) {
5542 * Check to see if the device data structure for the lun
5543 * exists. If not, create one.
5546 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5547 spin_lock_irqsave(&phba->devicelock, flags);
5548 device_data = __lpfc_get_device_data(phba,
5550 &vport->fc_portname,
5554 spin_unlock_irqrestore(&phba->devicelock, flags);
5555 device_data = lpfc_create_device_data(phba,
5556 &vport->fc_portname,
5559 phba->cfg_XLanePriority,
5563 spin_lock_irqsave(&phba->devicelock, flags);
5564 list_add_tail(&device_data->listentry, &phba->luns);
5566 device_data->rport_data = rport->dd_data;
5567 device_data->available = true;
5568 spin_unlock_irqrestore(&phba->devicelock, flags);
5569 sdev->hostdata = device_data;
5571 sdev->hostdata = rport->dd_data;
5573 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
5576 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5577 * available list of scsi buffers. Don't allocate more than the
5578 * HBA limit conveyed to the midlayer via the host structure. The
5579 * formula accounts for the lun_queue_depth + error handlers + 1
5580 * extra. This list of scsi bufs exists for the lifetime of the driver.
5582 total = phba->total_scsi_bufs;
5583 num_to_alloc = vport->cfg_lun_queue_depth + 2;
5585 /* If allocated buffers are enough do nothing */
5586 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5589 /* Allow some exchanges to be available always to complete discovery */
5590 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5591 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5592 "0704 At limitation of %d preallocated "
5593 "command buffers\n", total);
5595 /* Allow some exchanges to be available always to complete discovery */
5596 } else if (total + num_to_alloc >
5597 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5598 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5599 "0705 Allocation request of %d "
5600 "command buffers will exceed max of %d. "
5601 "Reducing allocation request to %d.\n",
5602 num_to_alloc, phba->cfg_hba_queue_depth,
5603 (phba->cfg_hba_queue_depth - total));
5604 num_to_alloc = phba->cfg_hba_queue_depth - total;
5606 num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
5607 if (num_to_alloc != num_allocated) {
5608 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5609 "0708 Allocation request of %d "
5610 "command buffers did not succeed. "
5611 "Allocated %d buffers.\n",
5612 num_to_alloc, num_allocated);
5614 if (num_allocated > 0)
5615 phba->total_scsi_bufs += num_allocated;
5620 * lpfc_slave_configure - scsi_host_template slave_configure entry point
5621 * @sdev: Pointer to scsi_device.
5623 * This routine configures following items
5624 * - Tag command queuing support for @sdev if supported.
5625 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5631 lpfc_slave_configure(struct scsi_device *sdev)
5633 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5634 struct lpfc_hba *phba = vport->phba;
5636 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
5638 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5639 lpfc_sli_handle_fast_ring_event(phba,
5640 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
5641 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5642 lpfc_poll_rearm_timer(phba);
5649 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
5650 * @sdev: Pointer to scsi_device.
5652 * This routine sets @sdev hostatdata filed to null.
5655 lpfc_slave_destroy(struct scsi_device *sdev)
5657 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5658 struct lpfc_hba *phba = vport->phba;
5659 unsigned long flags;
5660 struct lpfc_device_data *device_data = sdev->hostdata;
5662 atomic_dec(&phba->sdev_cnt);
5663 if ((phba->cfg_fof) && (device_data)) {
5664 spin_lock_irqsave(&phba->devicelock, flags);
5665 device_data->available = false;
5666 if (!device_data->oas_enabled)
5667 lpfc_delete_device_data(phba, device_data);
5668 spin_unlock_irqrestore(&phba->devicelock, flags);
5670 sdev->hostdata = NULL;
5675 * lpfc_create_device_data - creates and initializes device data structure for OAS
5676 * @pha: Pointer to host bus adapter structure.
5677 * @vport_wwpn: Pointer to vport's wwpn information
5678 * @target_wwpn: Pointer to target's wwpn information
5679 * @lun: Lun on target
5680 * @atomic_create: Flag to indicate if memory should be allocated using the
5681 * GFP_ATOMIC flag or not.
5683 * This routine creates a device data structure which will contain identifying
5684 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5685 * whether or not the corresponding lun is available by the system,
5686 * and pointer to the rport data.
5690 * Pointer to lpfc_device_data - Success
5692 struct lpfc_device_data*
5693 lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5694 struct lpfc_name *target_wwpn, uint64_t lun,
5695 uint32_t pri, bool atomic_create)
5698 struct lpfc_device_data *lun_info;
5701 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5705 /* Attempt to create the device data to contain lun info */
5708 memory_flags = GFP_ATOMIC;
5710 memory_flags = GFP_KERNEL;
5711 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5714 INIT_LIST_HEAD(&lun_info->listentry);
5715 lun_info->rport_data = NULL;
5716 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5717 sizeof(struct lpfc_name));
5718 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5719 sizeof(struct lpfc_name));
5720 lun_info->device_id.lun = lun;
5721 lun_info->oas_enabled = false;
5722 lun_info->priority = pri;
5723 lun_info->available = false;
5728 * lpfc_delete_device_data - frees a device data structure for OAS
5729 * @pha: Pointer to host bus adapter structure.
5730 * @lun_info: Pointer to device data structure to free.
5732 * This routine frees the previously allocated device data structure passed.
5736 lpfc_delete_device_data(struct lpfc_hba *phba,
5737 struct lpfc_device_data *lun_info)
5740 if (unlikely(!phba) || !lun_info ||
5744 if (!list_empty(&lun_info->listentry))
5745 list_del(&lun_info->listentry);
5746 mempool_free(lun_info, phba->device_data_mem_pool);
5751 * __lpfc_get_device_data - returns the device data for the specified lun
5752 * @pha: Pointer to host bus adapter structure.
5753 * @list: Point to list to search.
5754 * @vport_wwpn: Pointer to vport's wwpn information
5755 * @target_wwpn: Pointer to target's wwpn information
5756 * @lun: Lun on target
5758 * This routine searches the list passed for the specified lun's device data.
5759 * This function does not hold locks, it is the responsibility of the caller
5760 * to ensure the proper lock is held before calling the function.
5764 * Pointer to lpfc_device_data - Success
5766 struct lpfc_device_data*
5767 __lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5768 struct lpfc_name *vport_wwpn,
5769 struct lpfc_name *target_wwpn, uint64_t lun)
5772 struct lpfc_device_data *lun_info;
5774 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
5778 /* Check to see if the lun is already enabled for OAS. */
5780 list_for_each_entry(lun_info, list, listentry) {
5781 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5782 sizeof(struct lpfc_name)) == 0) &&
5783 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5784 sizeof(struct lpfc_name)) == 0) &&
5785 (lun_info->device_id.lun == lun))
5793 * lpfc_find_next_oas_lun - searches for the next oas lun
5794 * @pha: Pointer to host bus adapter structure.
5795 * @vport_wwpn: Pointer to vport's wwpn information
5796 * @target_wwpn: Pointer to target's wwpn information
5797 * @starting_lun: Pointer to the lun to start searching for
5798 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5799 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5800 * @found_lun: Pointer to the found lun.
5801 * @found_lun_status: Pointer to status of the found lun.
5803 * This routine searches the luns list for the specified lun
5804 * or the first lun for the vport/target. If the vport wwpn contains
5805 * a zero value then a specific vport is not specified. In this case
5806 * any vport which contains the lun will be considered a match. If the
5807 * target wwpn contains a zero value then a specific target is not specified.
5808 * In this case any target which contains the lun will be considered a
5809 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5810 * are returned. The function will also return the next lun if available.
5811 * If the next lun is not found, starting_lun parameter will be set to
5819 lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5820 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5821 struct lpfc_name *found_vport_wwpn,
5822 struct lpfc_name *found_target_wwpn,
5823 uint64_t *found_lun,
5824 uint32_t *found_lun_status,
5825 uint32_t *found_lun_pri)
5828 unsigned long flags;
5829 struct lpfc_device_data *lun_info;
5830 struct lpfc_device_id *device_id;
5834 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5835 !starting_lun || !found_vport_wwpn ||
5836 !found_target_wwpn || !found_lun || !found_lun_status ||
5837 (*starting_lun == NO_MORE_OAS_LUN) ||
5841 lun = *starting_lun;
5842 *found_lun = NO_MORE_OAS_LUN;
5843 *starting_lun = NO_MORE_OAS_LUN;
5845 /* Search for lun or the lun closet in value */
5847 spin_lock_irqsave(&phba->devicelock, flags);
5848 list_for_each_entry(lun_info, &phba->luns, listentry) {
5849 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5850 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5851 sizeof(struct lpfc_name)) == 0)) &&
5852 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5853 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5854 sizeof(struct lpfc_name)) == 0)) &&
5855 (lun_info->oas_enabled)) {
5856 device_id = &lun_info->device_id;
5858 ((lun == FIND_FIRST_OAS_LUN) ||
5859 (device_id->lun == lun))) {
5860 *found_lun = device_id->lun;
5861 memcpy(found_vport_wwpn,
5862 &device_id->vport_wwpn,
5863 sizeof(struct lpfc_name));
5864 memcpy(found_target_wwpn,
5865 &device_id->target_wwpn,
5866 sizeof(struct lpfc_name));
5867 if (lun_info->available)
5869 OAS_LUN_STATUS_EXISTS;
5871 *found_lun_status = 0;
5872 *found_lun_pri = lun_info->priority;
5873 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5874 memset(vport_wwpn, 0x0,
5875 sizeof(struct lpfc_name));
5876 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5877 memset(target_wwpn, 0x0,
5878 sizeof(struct lpfc_name));
5881 *starting_lun = device_id->lun;
5882 memcpy(vport_wwpn, &device_id->vport_wwpn,
5883 sizeof(struct lpfc_name));
5884 memcpy(target_wwpn, &device_id->target_wwpn,
5885 sizeof(struct lpfc_name));
5890 spin_unlock_irqrestore(&phba->devicelock, flags);
5895 * lpfc_enable_oas_lun - enables a lun for OAS operations
5896 * @pha: Pointer to host bus adapter structure.
5897 * @vport_wwpn: Pointer to vport's wwpn information
5898 * @target_wwpn: Pointer to target's wwpn information
5901 * This routine enables a lun for oas operations. The routines does so by
5902 * doing the following :
5904 * 1) Checks to see if the device data for the lun has been created.
5905 * 2) If found, sets the OAS enabled flag if not set and returns.
5906 * 3) Otherwise, creates a device data structure.
5907 * 4) If successfully created, indicates the device data is for an OAS lun,
5908 * indicates the lun is not available and add to the list of luns.
5915 lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5916 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
5919 struct lpfc_device_data *lun_info;
5920 unsigned long flags;
5922 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5926 spin_lock_irqsave(&phba->devicelock, flags);
5928 /* Check to see if the device data for the lun has been created */
5929 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5932 if (!lun_info->oas_enabled)
5933 lun_info->oas_enabled = true;
5934 lun_info->priority = pri;
5935 spin_unlock_irqrestore(&phba->devicelock, flags);
5939 /* Create an lun info structure and add to list of luns */
5940 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
5943 lun_info->oas_enabled = true;
5944 lun_info->priority = pri;
5945 lun_info->available = false;
5946 list_add_tail(&lun_info->listentry, &phba->luns);
5947 spin_unlock_irqrestore(&phba->devicelock, flags);
5950 spin_unlock_irqrestore(&phba->devicelock, flags);
5955 * lpfc_disable_oas_lun - disables a lun for OAS operations
5956 * @pha: Pointer to host bus adapter structure.
5957 * @vport_wwpn: Pointer to vport's wwpn information
5958 * @target_wwpn: Pointer to target's wwpn information
5961 * This routine disables a lun for oas operations. The routines does so by
5962 * doing the following :
5964 * 1) Checks to see if the device data for the lun is created.
5965 * 2) If present, clears the flag indicating this lun is for OAS.
5966 * 3) If the lun is not available by the system, the device data is
5974 lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5975 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
5978 struct lpfc_device_data *lun_info;
5979 unsigned long flags;
5981 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5985 spin_lock_irqsave(&phba->devicelock, flags);
5987 /* Check to see if the lun is available. */
5988 lun_info = __lpfc_get_device_data(phba,
5989 &phba->luns, vport_wwpn,
5992 lun_info->oas_enabled = false;
5993 lun_info->priority = pri;
5994 if (!lun_info->available)
5995 lpfc_delete_device_data(phba, lun_info);
5996 spin_unlock_irqrestore(&phba->devicelock, flags);
6000 spin_unlock_irqrestore(&phba->devicelock, flags);
6005 lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
6007 return SCSI_MLQUEUE_HOST_BUSY;
6011 lpfc_no_handler(struct scsi_cmnd *cmnd)
6017 lpfc_no_slave(struct scsi_device *sdev)
6022 struct scsi_host_template lpfc_template_nvme = {
6023 .module = THIS_MODULE,
6024 .name = LPFC_DRIVER_NAME,
6025 .proc_name = LPFC_DRIVER_NAME,
6027 .queuecommand = lpfc_no_command,
6028 .eh_abort_handler = lpfc_no_handler,
6029 .eh_device_reset_handler = lpfc_no_handler,
6030 .eh_target_reset_handler = lpfc_no_handler,
6031 .eh_bus_reset_handler = lpfc_no_handler,
6032 .eh_host_reset_handler = lpfc_no_handler,
6033 .slave_alloc = lpfc_no_slave,
6034 .slave_configure = lpfc_no_slave,
6035 .scan_finished = lpfc_scan_finished,
6039 .use_clustering = ENABLE_CLUSTERING,
6040 .shost_attrs = lpfc_hba_attrs,
6041 .max_sectors = 0xFFFF,
6042 .vendor_id = LPFC_NL_VENDOR_ID,
6043 .track_queue_depth = 0,
6046 struct scsi_host_template lpfc_template_no_hr = {
6047 .module = THIS_MODULE,
6048 .name = LPFC_DRIVER_NAME,
6049 .proc_name = LPFC_DRIVER_NAME,
6051 .queuecommand = lpfc_queuecommand,
6052 .eh_timed_out = fc_eh_timed_out,
6053 .eh_abort_handler = lpfc_abort_handler,
6054 .eh_device_reset_handler = lpfc_device_reset_handler,
6055 .eh_target_reset_handler = lpfc_target_reset_handler,
6056 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6057 .slave_alloc = lpfc_slave_alloc,
6058 .slave_configure = lpfc_slave_configure,
6059 .slave_destroy = lpfc_slave_destroy,
6060 .scan_finished = lpfc_scan_finished,
6062 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6063 .cmd_per_lun = LPFC_CMD_PER_LUN,
6064 .use_clustering = ENABLE_CLUSTERING,
6065 .shost_attrs = lpfc_hba_attrs,
6066 .max_sectors = 0xFFFF,
6067 .vendor_id = LPFC_NL_VENDOR_ID,
6068 .change_queue_depth = scsi_change_queue_depth,
6069 .track_queue_depth = 1,
6072 struct scsi_host_template lpfc_template = {
6073 .module = THIS_MODULE,
6074 .name = LPFC_DRIVER_NAME,
6075 .proc_name = LPFC_DRIVER_NAME,
6077 .queuecommand = lpfc_queuecommand,
6078 .eh_timed_out = fc_eh_timed_out,
6079 .eh_abort_handler = lpfc_abort_handler,
6080 .eh_device_reset_handler = lpfc_device_reset_handler,
6081 .eh_target_reset_handler = lpfc_target_reset_handler,
6082 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6083 .eh_host_reset_handler = lpfc_host_reset_handler,
6084 .slave_alloc = lpfc_slave_alloc,
6085 .slave_configure = lpfc_slave_configure,
6086 .slave_destroy = lpfc_slave_destroy,
6087 .scan_finished = lpfc_scan_finished,
6089 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6090 .cmd_per_lun = LPFC_CMD_PER_LUN,
6091 .use_clustering = ENABLE_CLUSTERING,
6092 .shost_attrs = lpfc_hba_attrs,
6093 .max_sectors = 0xFFFF,
6094 .vendor_id = LPFC_NL_VENDOR_ID,
6095 .change_queue_depth = scsi_change_queue_depth,
6096 .track_queue_depth = 1,
6099 struct scsi_host_template lpfc_vport_template = {
6100 .module = THIS_MODULE,
6101 .name = LPFC_DRIVER_NAME,
6102 .proc_name = LPFC_DRIVER_NAME,
6104 .queuecommand = lpfc_queuecommand,
6105 .eh_timed_out = fc_eh_timed_out,
6106 .eh_abort_handler = lpfc_abort_handler,
6107 .eh_device_reset_handler = lpfc_device_reset_handler,
6108 .eh_target_reset_handler = lpfc_target_reset_handler,
6109 .slave_alloc = lpfc_slave_alloc,
6110 .slave_configure = lpfc_slave_configure,
6111 .slave_destroy = lpfc_slave_destroy,
6112 .scan_finished = lpfc_scan_finished,
6114 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6115 .cmd_per_lun = LPFC_CMD_PER_LUN,
6116 .use_clustering = ENABLE_CLUSTERING,
6117 .shost_attrs = lpfc_vport_attrs,
6118 .max_sectors = 0xFFFF,
6119 .change_queue_depth = scsi_change_queue_depth,
6120 .track_queue_depth = 1,