1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2017-2019 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 static char *dif_op_str[] = {
66 struct scsi_dif_tuple {
67 __be16 guard_tag; /* Checksum */
68 __be16 app_tag; /* Opaque storage */
69 __be32 ref_tag; /* Target LBA or indirect LBA */
72 static struct lpfc_rport_data *
73 lpfc_rport_data_from_scsi_device(struct scsi_device *sdev)
75 struct lpfc_vport *vport = (struct lpfc_vport *)sdev->host->hostdata;
77 if (vport->phba->cfg_fof)
78 return ((struct lpfc_device_data *)sdev->hostdata)->rport_data;
80 return (struct lpfc_rport_data *)sdev->hostdata;
84 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
86 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb);
88 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc);
90 static inline unsigned
91 lpfc_cmd_blksize(struct scsi_cmnd *sc)
93 return sc->device->sector_size;
96 #define LPFC_CHECK_PROTECT_GUARD 1
97 #define LPFC_CHECK_PROTECT_REF 2
98 static inline unsigned
99 lpfc_cmd_protect(struct scsi_cmnd *sc, int flag)
104 static inline unsigned
105 lpfc_cmd_guard_csum(struct scsi_cmnd *sc)
107 if (lpfc_prot_group_type(NULL, sc) == LPFC_PG_TYPE_NO_DIF)
109 if (scsi_host_get_guard(sc->device->host) == SHOST_DIX_GUARD_IP)
115 * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
116 * @phba: Pointer to HBA object.
117 * @lpfc_cmd: lpfc scsi command object pointer.
119 * This function is called from the lpfc_prep_task_mgmt_cmd function to
120 * set the last bit in the response sge entry.
123 lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
124 struct lpfc_io_buf *lpfc_cmd)
126 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
129 sgl->word2 = le32_to_cpu(sgl->word2);
130 bf_set(lpfc_sli4_sge_last, sgl, 1);
131 sgl->word2 = cpu_to_le32(sgl->word2);
136 * lpfc_update_stats - Update statistical data for the command completion
137 * @vport: The virtual port on which this call is executing.
138 * @lpfc_cmd: lpfc scsi command object pointer.
140 * This function is called when there is a command completion and this
141 * function updates the statistical data for the command completion.
144 lpfc_update_stats(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
146 struct lpfc_hba *phba = vport->phba;
147 struct lpfc_rport_data *rdata;
148 struct lpfc_nodelist *pnode;
149 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
151 struct Scsi_Host *shost = lpfc_shost_from_vport(vport);
152 unsigned long latency;
155 if (!vport->stat_data_enabled ||
156 vport->stat_data_blocked ||
160 latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
161 rdata = lpfc_cmd->rdata;
162 pnode = rdata->pnode;
164 spin_lock_irqsave(shost->host_lock, flags);
167 (phba->bucket_type == LPFC_NO_BUCKET)) {
168 spin_unlock_irqrestore(shost->host_lock, flags);
172 if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
173 i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
175 /* check array subscript bounds */
178 else if (i >= LPFC_MAX_BUCKET_COUNT)
179 i = LPFC_MAX_BUCKET_COUNT - 1;
181 for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
182 if (latency <= (phba->bucket_base +
183 ((1<<i)*phba->bucket_step)))
187 pnode->lat_data[i].cmd_count++;
188 spin_unlock_irqrestore(shost->host_lock, flags);
192 * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
193 * @phba: The Hba for which this call is being executed.
195 * This routine is called when there is resource error in driver or firmware.
196 * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
197 * posts at most 1 event each second. This routine wakes up worker thread of
198 * @phba to process WORKER_RAM_DOWN_EVENT event.
200 * This routine should be called with no lock held.
203 lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
207 unsigned long expires;
209 spin_lock_irqsave(&phba->hbalock, flags);
210 atomic_inc(&phba->num_rsrc_err);
211 phba->last_rsrc_error_time = jiffies;
213 expires = phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL;
214 if (time_after(expires, jiffies)) {
215 spin_unlock_irqrestore(&phba->hbalock, flags);
219 phba->last_ramp_down_time = jiffies;
221 spin_unlock_irqrestore(&phba->hbalock, flags);
223 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
224 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
226 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
227 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
230 lpfc_worker_wake_up(phba);
235 * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
236 * @phba: The Hba for which this call is being executed.
238 * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
239 * thread.This routine reduces queue depth for all scsi device on each vport
240 * associated with @phba.
243 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
245 struct lpfc_vport **vports;
246 struct Scsi_Host *shost;
247 struct scsi_device *sdev;
248 unsigned long new_queue_depth;
249 unsigned long num_rsrc_err, num_cmd_success;
252 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
253 num_cmd_success = atomic_read(&phba->num_cmd_success);
256 * The error and success command counters are global per
257 * driver instance. If another handler has already
258 * operated on this error event, just exit.
260 if (num_rsrc_err == 0)
263 vports = lpfc_create_vport_work_array(phba);
265 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
266 shost = lpfc_shost_from_vport(vports[i]);
267 shost_for_each_device(sdev, shost) {
269 sdev->queue_depth * num_rsrc_err /
270 (num_rsrc_err + num_cmd_success);
271 if (!new_queue_depth)
272 new_queue_depth = sdev->queue_depth - 1;
274 new_queue_depth = sdev->queue_depth -
276 scsi_change_queue_depth(sdev, new_queue_depth);
279 lpfc_destroy_vport_work_array(phba, vports);
280 atomic_set(&phba->num_rsrc_err, 0);
281 atomic_set(&phba->num_cmd_success, 0);
285 * lpfc_scsi_dev_block - set all scsi hosts to block state
286 * @phba: Pointer to HBA context object.
288 * This function walks vport list and set each SCSI host to block state
289 * by invoking fc_remote_port_delete() routine. This function is invoked
290 * with EEH when device's PCI slot has been permanently disabled.
293 lpfc_scsi_dev_block(struct lpfc_hba *phba)
295 struct lpfc_vport **vports;
296 struct Scsi_Host *shost;
297 struct scsi_device *sdev;
298 struct fc_rport *rport;
301 vports = lpfc_create_vport_work_array(phba);
303 for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
304 shost = lpfc_shost_from_vport(vports[i]);
305 shost_for_each_device(sdev, shost) {
306 rport = starget_to_rport(scsi_target(sdev));
307 fc_remote_port_delete(rport);
310 lpfc_destroy_vport_work_array(phba, vports);
314 * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
315 * @vport: The virtual port for which this call being executed.
316 * @num_to_allocate: The requested number of buffers to allocate.
318 * This routine allocates a scsi buffer for device with SLI-3 interface spec,
319 * the scsi buffer contains all the necessary information needed to initiate
320 * a SCSI I/O. The non-DMAable buffer region contains information to build
321 * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
322 * and the initial BPL. In addition to allocating memory, the FCP CMND and
323 * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
326 * int - number of scsi buffers that were allocated.
327 * 0 = failure, less than num_to_alloc is a partial failure.
330 lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
332 struct lpfc_hba *phba = vport->phba;
333 struct lpfc_io_buf *psb;
334 struct ulp_bde64 *bpl;
336 dma_addr_t pdma_phys_fcp_cmd;
337 dma_addr_t pdma_phys_fcp_rsp;
338 dma_addr_t pdma_phys_sgl;
342 bpl_size = phba->cfg_sg_dma_buf_size -
343 (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
345 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
346 "9067 ALLOC %d scsi_bufs: %d (%d + %d + %d)\n",
347 num_to_alloc, phba->cfg_sg_dma_buf_size,
348 (int)sizeof(struct fcp_cmnd),
349 (int)sizeof(struct fcp_rsp), bpl_size);
351 for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
352 psb = kzalloc(sizeof(struct lpfc_io_buf), GFP_KERNEL);
357 * Get memory from the pci pool to map the virt space to pci
358 * bus space for an I/O. The DMA buffer includes space for the
359 * struct fcp_cmnd, struct fcp_rsp and the number of bde's
360 * necessary to support the sg_tablesize.
362 psb->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
363 GFP_KERNEL, &psb->dma_handle);
370 /* Allocate iotag for psb->cur_iocbq. */
371 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
373 dma_pool_free(phba->lpfc_sg_dma_buf_pool,
374 psb->data, psb->dma_handle);
378 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
380 psb->fcp_cmnd = psb->data;
381 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
382 psb->dma_sgl = psb->data + sizeof(struct fcp_cmnd) +
383 sizeof(struct fcp_rsp);
385 /* Initialize local short-hand pointers. */
386 bpl = (struct ulp_bde64 *)psb->dma_sgl;
387 pdma_phys_fcp_cmd = psb->dma_handle;
388 pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
389 pdma_phys_sgl = psb->dma_handle + sizeof(struct fcp_cmnd) +
390 sizeof(struct fcp_rsp);
393 * The first two bdes are the FCP_CMD and FCP_RSP. The balance
394 * are sg list bdes. Initialize the first two and leave the
395 * rest for queuecommand.
397 bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
398 bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
399 bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
400 bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
401 bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
403 /* Setup the physical region for the FCP RSP */
404 bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
405 bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
406 bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
407 bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
408 bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
411 * Since the IOCB for the FCP I/O is built into this
412 * lpfc_scsi_buf, initialize it with all known data now.
414 iocb = &psb->cur_iocbq.iocb;
415 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
416 if ((phba->sli_rev == 3) &&
417 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
418 /* fill in immediate fcp command BDE */
419 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
420 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
421 iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
423 iocb->un.fcpi64.bdl.addrHigh = 0;
424 iocb->ulpBdeCount = 0;
426 /* fill in response BDE */
427 iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
429 iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
430 sizeof(struct fcp_rsp);
431 iocb->unsli3.fcp_ext.rbde.addrLow =
432 putPaddrLow(pdma_phys_fcp_rsp);
433 iocb->unsli3.fcp_ext.rbde.addrHigh =
434 putPaddrHigh(pdma_phys_fcp_rsp);
436 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
437 iocb->un.fcpi64.bdl.bdeSize =
438 (2 * sizeof(struct ulp_bde64));
439 iocb->un.fcpi64.bdl.addrLow =
440 putPaddrLow(pdma_phys_sgl);
441 iocb->un.fcpi64.bdl.addrHigh =
442 putPaddrHigh(pdma_phys_sgl);
443 iocb->ulpBdeCount = 1;
446 iocb->ulpClass = CLASS3;
447 psb->status = IOSTAT_SUCCESS;
448 /* Put it back into the SCSI buffer list */
449 psb->cur_iocbq.context1 = psb;
450 spin_lock_init(&psb->buf_lock);
451 lpfc_release_scsi_buf_s3(phba, psb);
459 * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
460 * @vport: pointer to lpfc vport data structure.
462 * This routine is invoked by the vport cleanup for deletions and the cleanup
463 * for an ndlp on removal.
466 lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
468 struct lpfc_hba *phba = vport->phba;
469 struct lpfc_io_buf *psb, *next_psb;
470 struct lpfc_sli4_hdw_queue *qp;
471 unsigned long iflag = 0;
474 if (!(vport->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
477 spin_lock_irqsave(&phba->hbalock, iflag);
478 for (idx = 0; idx < phba->cfg_hdw_queue; idx++) {
479 qp = &phba->sli4_hba.hdwq[idx];
481 spin_lock(&qp->abts_io_buf_list_lock);
482 list_for_each_entry_safe(psb, next_psb,
483 &qp->lpfc_abts_io_buf_list, list) {
484 if (psb->cur_iocbq.iocb_flag & LPFC_IO_NVME)
487 if (psb->rdata && psb->rdata->pnode &&
488 psb->rdata->pnode->vport == vport)
491 spin_unlock(&qp->abts_io_buf_list_lock);
493 spin_unlock_irqrestore(&phba->hbalock, iflag);
497 * lpfc_sli4_io_xri_aborted - Fast-path process of fcp xri abort
498 * @phba: pointer to lpfc hba data structure.
499 * @axri: pointer to the fcp xri abort wcqe structure.
501 * This routine is invoked by the worker thread to process a SLI4 fast-path
502 * FCP or NVME aborted xri.
505 lpfc_sli4_io_xri_aborted(struct lpfc_hba *phba,
506 struct sli4_wcqe_xri_aborted *axri, int idx)
508 uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
509 uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
510 struct lpfc_io_buf *psb, *next_psb;
511 struct lpfc_sli4_hdw_queue *qp;
512 unsigned long iflag = 0;
513 struct lpfc_iocbq *iocbq;
515 struct lpfc_nodelist *ndlp;
517 struct lpfc_sli_ring *pring = phba->sli4_hba.els_wq->pring;
519 if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_FCP))
522 qp = &phba->sli4_hba.hdwq[idx];
523 spin_lock_irqsave(&phba->hbalock, iflag);
524 spin_lock(&qp->abts_io_buf_list_lock);
525 list_for_each_entry_safe(psb, next_psb,
526 &qp->lpfc_abts_io_buf_list, list) {
527 if (psb->cur_iocbq.sli4_xritag == xri) {
528 list_del_init(&psb->list);
529 psb->flags &= ~LPFC_SBUF_XBUSY;
530 psb->status = IOSTAT_SUCCESS;
531 if (psb->cur_iocbq.iocb_flag & LPFC_IO_NVME) {
532 qp->abts_nvme_io_bufs--;
533 spin_unlock(&qp->abts_io_buf_list_lock);
534 spin_unlock_irqrestore(&phba->hbalock, iflag);
535 lpfc_sli4_nvme_xri_aborted(phba, axri, psb);
538 qp->abts_scsi_io_bufs--;
539 spin_unlock(&qp->abts_io_buf_list_lock);
541 if (psb->rdata && psb->rdata->pnode)
542 ndlp = psb->rdata->pnode;
546 rrq_empty = list_empty(&phba->active_rrq_list);
547 spin_unlock_irqrestore(&phba->hbalock, iflag);
549 lpfc_set_rrq_active(phba, ndlp,
550 psb->cur_iocbq.sli4_lxritag, rxid, 1);
551 lpfc_sli4_abts_err_handler(phba, ndlp, axri);
553 lpfc_release_scsi_buf_s4(phba, psb);
555 lpfc_worker_wake_up(phba);
559 spin_unlock(&qp->abts_io_buf_list_lock);
560 for (i = 1; i <= phba->sli.last_iotag; i++) {
561 iocbq = phba->sli.iocbq_lookup[i];
563 if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
564 (iocbq->iocb_flag & LPFC_IO_LIBDFC))
566 if (iocbq->sli4_xritag != xri)
568 psb = container_of(iocbq, struct lpfc_io_buf, cur_iocbq);
569 psb->flags &= ~LPFC_SBUF_XBUSY;
570 spin_unlock_irqrestore(&phba->hbalock, iflag);
571 if (!list_empty(&pring->txq))
572 lpfc_worker_wake_up(phba);
576 spin_unlock_irqrestore(&phba->hbalock, iflag);
580 * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
581 * @phba: The HBA for which this call is being executed.
583 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
584 * and returns to caller.
588 * Pointer to lpfc_scsi_buf - Success
590 static struct lpfc_io_buf *
591 lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
592 struct scsi_cmnd *cmnd)
594 struct lpfc_io_buf *lpfc_cmd = NULL;
595 struct list_head *scsi_buf_list_get = &phba->lpfc_scsi_buf_list_get;
596 unsigned long iflag = 0;
598 spin_lock_irqsave(&phba->scsi_buf_list_get_lock, iflag);
599 list_remove_head(scsi_buf_list_get, lpfc_cmd, struct lpfc_io_buf,
602 spin_lock(&phba->scsi_buf_list_put_lock);
603 list_splice(&phba->lpfc_scsi_buf_list_put,
604 &phba->lpfc_scsi_buf_list_get);
605 INIT_LIST_HEAD(&phba->lpfc_scsi_buf_list_put);
606 list_remove_head(scsi_buf_list_get, lpfc_cmd,
607 struct lpfc_io_buf, list);
608 spin_unlock(&phba->scsi_buf_list_put_lock);
610 spin_unlock_irqrestore(&phba->scsi_buf_list_get_lock, iflag);
612 if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_cmd) {
613 atomic_inc(&ndlp->cmd_pending);
614 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
619 * lpfc_get_scsi_buf_s4 - Get a scsi buffer from io_buf_list of the HBA
620 * @phba: The HBA for which this call is being executed.
622 * This routine removes a scsi buffer from head of @hdwq io_buf_list
623 * and returns to caller.
627 * Pointer to lpfc_scsi_buf - Success
629 static struct lpfc_io_buf *
630 lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
631 struct scsi_cmnd *cmnd)
633 struct lpfc_io_buf *lpfc_cmd;
634 struct lpfc_sli4_hdw_queue *qp;
635 struct sli4_sge *sgl;
637 dma_addr_t pdma_phys_fcp_rsp;
638 dma_addr_t pdma_phys_fcp_cmd;
641 struct fcp_cmd_rsp_buf *tmp = NULL;
643 cpu = raw_smp_processor_id();
644 if (cmnd && phba->cfg_fcp_io_sched == LPFC_FCP_SCHED_BY_HDWQ) {
645 tag = blk_mq_unique_tag(cmnd->request);
646 idx = blk_mq_unique_tag_to_hwq(tag);
648 idx = phba->sli4_hba.cpu_map[cpu].hdwq;
651 lpfc_cmd = lpfc_get_io_buf(phba, ndlp, idx,
652 !phba->cfg_xri_rebalancing);
654 qp = &phba->sli4_hba.hdwq[idx];
659 /* Setup key fields in buffer that may have been changed
660 * if other protocols used this buffer.
662 lpfc_cmd->cur_iocbq.iocb_flag = LPFC_IO_FCP;
663 lpfc_cmd->prot_seg_cnt = 0;
664 lpfc_cmd->seg_cnt = 0;
665 lpfc_cmd->timeout = 0;
667 lpfc_cmd->start_time = jiffies;
668 lpfc_cmd->waitq = NULL;
670 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
671 lpfc_cmd->prot_data_type = 0;
673 tmp = lpfc_get_cmd_rsp_buf_per_hdwq(phba, lpfc_cmd);
677 lpfc_cmd->fcp_cmnd = tmp->fcp_cmnd;
678 lpfc_cmd->fcp_rsp = tmp->fcp_rsp;
681 * The first two SGEs are the FCP_CMD and FCP_RSP.
682 * The balance are sg list bdes. Initialize the
683 * first two and leave the rest for queuecommand.
685 sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
686 pdma_phys_fcp_cmd = tmp->fcp_cmd_rsp_dma_handle;
687 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
688 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
689 sgl->word2 = le32_to_cpu(sgl->word2);
690 bf_set(lpfc_sli4_sge_last, sgl, 0);
691 sgl->word2 = cpu_to_le32(sgl->word2);
692 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
695 /* Setup the physical region for the FCP RSP */
696 pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
697 sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
698 sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
699 sgl->word2 = le32_to_cpu(sgl->word2);
700 bf_set(lpfc_sli4_sge_last, sgl, 1);
701 sgl->word2 = cpu_to_le32(sgl->word2);
702 sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
705 * Since the IOCB for the FCP I/O is built into this
706 * lpfc_io_buf, initialize it with all known data now.
708 iocb = &lpfc_cmd->cur_iocbq.iocb;
709 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
710 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
711 /* setting the BLP size to 2 * sizeof BDE may not be correct.
712 * We are setting the bpl to point to out sgl. An sgl's
713 * entries are 16 bytes, a bpl entries are 12 bytes.
715 iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
716 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
717 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
718 iocb->ulpBdeCount = 1;
720 iocb->ulpClass = CLASS3;
722 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
723 atomic_inc(&ndlp->cmd_pending);
724 lpfc_cmd->flags |= LPFC_SBUF_BUMP_QDEPTH;
729 * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
730 * @phba: The HBA for which this call is being executed.
732 * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
733 * and returns to caller.
737 * Pointer to lpfc_scsi_buf - Success
739 static struct lpfc_io_buf*
740 lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
741 struct scsi_cmnd *cmnd)
743 return phba->lpfc_get_scsi_buf(phba, ndlp, cmnd);
747 * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
748 * @phba: The Hba for which this call is being executed.
749 * @psb: The scsi buffer which is being released.
751 * This routine releases @psb scsi buffer by adding it to tail of @phba
752 * lpfc_scsi_buf_list list.
755 lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
757 unsigned long iflag = 0;
760 psb->prot_seg_cnt = 0;
762 spin_lock_irqsave(&phba->scsi_buf_list_put_lock, iflag);
764 psb->cur_iocbq.iocb_flag = LPFC_IO_FCP;
765 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list_put);
766 spin_unlock_irqrestore(&phba->scsi_buf_list_put_lock, iflag);
770 * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
771 * @phba: The Hba for which this call is being executed.
772 * @psb: The scsi buffer which is being released.
774 * This routine releases @psb scsi buffer by adding it to tail of @hdwq
775 * io_buf_list list. For SLI4 XRI's are tied to the scsi buffer
776 * and cannot be reused for at least RA_TOV amount of time if it was
780 lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
782 struct lpfc_sli4_hdw_queue *qp;
783 unsigned long iflag = 0;
786 psb->prot_seg_cnt = 0;
789 if (psb->flags & LPFC_SBUF_XBUSY) {
790 spin_lock_irqsave(&qp->abts_io_buf_list_lock, iflag);
792 list_add_tail(&psb->list, &qp->lpfc_abts_io_buf_list);
793 qp->abts_scsi_io_bufs++;
794 spin_unlock_irqrestore(&qp->abts_io_buf_list_lock, iflag);
796 lpfc_release_io_buf(phba, (struct lpfc_io_buf *)psb, qp);
801 * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
802 * @phba: The Hba for which this call is being executed.
803 * @psb: The scsi buffer which is being released.
805 * This routine releases @psb scsi buffer by adding it to tail of @phba
806 * lpfc_scsi_buf_list list.
809 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
811 if ((psb->flags & LPFC_SBUF_BUMP_QDEPTH) && psb->ndlp)
812 atomic_dec(&psb->ndlp->cmd_pending);
814 psb->flags &= ~LPFC_SBUF_BUMP_QDEPTH;
815 phba->lpfc_release_scsi_buf(phba, psb);
819 * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
820 * @phba: The Hba for which this call is being executed.
821 * @lpfc_cmd: The scsi buffer which is going to be mapped.
823 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
824 * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
825 * through sg elements and format the bde. This routine also initializes all
826 * IOCB fields which are dependent on scsi command request buffer.
833 lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
835 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
836 struct scatterlist *sgel = NULL;
837 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
838 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
839 struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
840 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
841 struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
843 uint32_t num_bde = 0;
844 int nseg, datadir = scsi_cmnd->sc_data_direction;
847 * There are three possibilities here - use scatter-gather segment, use
848 * the single mapping, or neither. Start the lpfc command prep by
849 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
853 if (scsi_sg_count(scsi_cmnd)) {
855 * The driver stores the segment count returned from pci_map_sg
856 * because this a count of dma-mappings used to map the use_sg
857 * pages. They are not guaranteed to be the same for those
858 * architectures that implement an IOMMU.
861 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
862 scsi_sg_count(scsi_cmnd), datadir);
866 lpfc_cmd->seg_cnt = nseg;
867 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
868 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
869 "9064 BLKGRD: %s: Too many sg segments from "
870 "dma_map_sg. Config %d, seg_cnt %d\n",
871 __func__, phba->cfg_sg_seg_cnt,
873 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
874 lpfc_cmd->seg_cnt = 0;
875 scsi_dma_unmap(scsi_cmnd);
880 * The driver established a maximum scatter-gather segment count
881 * during probe that limits the number of sg elements in any
882 * single scsi command. Just run through the seg_cnt and format
884 * When using SLI-3 the driver will try to fit all the BDEs into
885 * the IOCB. If it can't then the BDEs get added to a BPL as it
886 * does for SLI-2 mode.
888 scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
889 physaddr = sg_dma_address(sgel);
890 if (phba->sli_rev == 3 &&
891 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
892 !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
893 nseg <= LPFC_EXT_DATA_BDE_COUNT) {
894 data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
895 data_bde->tus.f.bdeSize = sg_dma_len(sgel);
896 data_bde->addrLow = putPaddrLow(physaddr);
897 data_bde->addrHigh = putPaddrHigh(physaddr);
900 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
901 bpl->tus.f.bdeSize = sg_dma_len(sgel);
902 bpl->tus.w = le32_to_cpu(bpl->tus.w);
904 le32_to_cpu(putPaddrLow(physaddr));
906 le32_to_cpu(putPaddrHigh(physaddr));
913 * Finish initializing those IOCB fields that are dependent on the
914 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
915 * explicitly reinitialized and for SLI-3 the extended bde count is
916 * explicitly reinitialized since all iocb memory resources are reused.
918 if (phba->sli_rev == 3 &&
919 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
920 !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
921 if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
923 * The extended IOCB format can only fit 3 BDE or a BPL.
924 * This I/O has more than 3 BDE so the 1st data bde will
925 * be a BPL that is filled in here.
927 physaddr = lpfc_cmd->dma_handle;
928 data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
929 data_bde->tus.f.bdeSize = (num_bde *
930 sizeof(struct ulp_bde64));
931 physaddr += (sizeof(struct fcp_cmnd) +
932 sizeof(struct fcp_rsp) +
933 (2 * sizeof(struct ulp_bde64)));
934 data_bde->addrHigh = putPaddrHigh(physaddr);
935 data_bde->addrLow = putPaddrLow(physaddr);
936 /* ebde count includes the response bde and data bpl */
937 iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
939 /* ebde count includes the response bde and data bdes */
940 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
943 iocb_cmd->un.fcpi64.bdl.bdeSize =
944 ((num_bde + 2) * sizeof(struct ulp_bde64));
945 iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
947 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
950 * Due to difference in data length between DIF/non-DIF paths,
951 * we need to set word 4 of IOCB here
953 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
957 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
959 /* Return BG_ERR_INIT if error injection is detected by Initiator */
960 #define BG_ERR_INIT 0x1
961 /* Return BG_ERR_TGT if error injection is detected by Target */
962 #define BG_ERR_TGT 0x2
963 /* Return BG_ERR_SWAP if swapping CSUM<-->CRC is required for error injection */
964 #define BG_ERR_SWAP 0x10
966 * Return BG_ERR_CHECK if disabling Guard/Ref/App checking is required for
969 #define BG_ERR_CHECK 0x20
972 * lpfc_bg_err_inject - Determine if we should inject an error
973 * @phba: The Hba for which this call is being executed.
974 * @sc: The SCSI command to examine
975 * @reftag: (out) BlockGuard reference tag for transmitted data
976 * @apptag: (out) BlockGuard application tag for transmitted data
977 * @new_guard (in) Value to replace CRC with if needed
979 * Returns BG_ERR_* bit mask or 0 if request ignored
982 lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
983 uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
985 struct scatterlist *sgpe; /* s/g prot entry */
986 struct lpfc_io_buf *lpfc_cmd = NULL;
987 struct scsi_dif_tuple *src = NULL;
988 struct lpfc_nodelist *ndlp;
989 struct lpfc_rport_data *rdata;
990 uint32_t op = scsi_get_prot_op(sc);
997 if (op == SCSI_PROT_NORMAL)
1000 sgpe = scsi_prot_sglist(sc);
1001 lba = scsi_get_lba(sc);
1003 /* First check if we need to match the LBA */
1004 if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
1005 blksize = lpfc_cmd_blksize(sc);
1006 numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
1008 /* Make sure we have the right LBA if one is specified */
1009 if ((phba->lpfc_injerr_lba < lba) ||
1010 (phba->lpfc_injerr_lba >= (lba + numblks)))
1013 blockoff = phba->lpfc_injerr_lba - lba;
1014 numblks = sg_dma_len(sgpe) /
1015 sizeof(struct scsi_dif_tuple);
1016 if (numblks < blockoff)
1021 /* Next check if we need to match the remote NPortID or WWPN */
1022 rdata = lpfc_rport_data_from_scsi_device(sc->device);
1023 if (rdata && rdata->pnode) {
1024 ndlp = rdata->pnode;
1026 /* Make sure we have the right NPortID if one is specified */
1027 if (phba->lpfc_injerr_nportid &&
1028 (phba->lpfc_injerr_nportid != ndlp->nlp_DID))
1032 * Make sure we have the right WWPN if one is specified.
1033 * wwn[0] should be a non-zero NAA in a good WWPN.
1035 if (phba->lpfc_injerr_wwpn.u.wwn[0] &&
1036 (memcmp(&ndlp->nlp_portname, &phba->lpfc_injerr_wwpn,
1037 sizeof(struct lpfc_name)) != 0))
1041 /* Setup a ptr to the protection data if the SCSI host provides it */
1043 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
1045 lpfc_cmd = (struct lpfc_io_buf *)sc->host_scribble;
1048 /* Should we change the Reference Tag */
1050 if (phba->lpfc_injerr_wref_cnt) {
1052 case SCSI_PROT_WRITE_PASS:
1055 * For WRITE_PASS, force the error
1056 * to be sent on the wire. It should
1057 * be detected by the Target.
1058 * If blockoff != 0 error will be
1059 * inserted in middle of the IO.
1062 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1063 "9076 BLKGRD: Injecting reftag error: "
1064 "write lba x%lx + x%x oldrefTag x%x\n",
1065 (unsigned long)lba, blockoff,
1066 be32_to_cpu(src->ref_tag));
1069 * Save the old ref_tag so we can
1070 * restore it on completion.
1073 lpfc_cmd->prot_data_type =
1075 lpfc_cmd->prot_data_segment =
1077 lpfc_cmd->prot_data =
1080 src->ref_tag = cpu_to_be32(0xDEADBEEF);
1081 phba->lpfc_injerr_wref_cnt--;
1082 if (phba->lpfc_injerr_wref_cnt == 0) {
1083 phba->lpfc_injerr_nportid = 0;
1084 phba->lpfc_injerr_lba =
1085 LPFC_INJERR_LBA_OFF;
1086 memset(&phba->lpfc_injerr_wwpn,
1087 0, sizeof(struct lpfc_name));
1089 rc = BG_ERR_TGT | BG_ERR_CHECK;
1094 case SCSI_PROT_WRITE_INSERT:
1096 * For WRITE_INSERT, force the error
1097 * to be sent on the wire. It should be
1098 * detected by the Target.
1100 /* DEADBEEF will be the reftag on the wire */
1101 *reftag = 0xDEADBEEF;
1102 phba->lpfc_injerr_wref_cnt--;
1103 if (phba->lpfc_injerr_wref_cnt == 0) {
1104 phba->lpfc_injerr_nportid = 0;
1105 phba->lpfc_injerr_lba =
1106 LPFC_INJERR_LBA_OFF;
1107 memset(&phba->lpfc_injerr_wwpn,
1108 0, sizeof(struct lpfc_name));
1110 rc = BG_ERR_TGT | BG_ERR_CHECK;
1112 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1113 "9078 BLKGRD: Injecting reftag error: "
1114 "write lba x%lx\n", (unsigned long)lba);
1116 case SCSI_PROT_WRITE_STRIP:
1118 * For WRITE_STRIP and WRITE_PASS,
1119 * force the error on data
1120 * being copied from SLI-Host to SLI-Port.
1122 *reftag = 0xDEADBEEF;
1123 phba->lpfc_injerr_wref_cnt--;
1124 if (phba->lpfc_injerr_wref_cnt == 0) {
1125 phba->lpfc_injerr_nportid = 0;
1126 phba->lpfc_injerr_lba =
1127 LPFC_INJERR_LBA_OFF;
1128 memset(&phba->lpfc_injerr_wwpn,
1129 0, sizeof(struct lpfc_name));
1133 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1134 "9077 BLKGRD: Injecting reftag error: "
1135 "write lba x%lx\n", (unsigned long)lba);
1139 if (phba->lpfc_injerr_rref_cnt) {
1141 case SCSI_PROT_READ_INSERT:
1142 case SCSI_PROT_READ_STRIP:
1143 case SCSI_PROT_READ_PASS:
1145 * For READ_STRIP and READ_PASS, force the
1146 * error on data being read off the wire. It
1147 * should force an IO error to the driver.
1149 *reftag = 0xDEADBEEF;
1150 phba->lpfc_injerr_rref_cnt--;
1151 if (phba->lpfc_injerr_rref_cnt == 0) {
1152 phba->lpfc_injerr_nportid = 0;
1153 phba->lpfc_injerr_lba =
1154 LPFC_INJERR_LBA_OFF;
1155 memset(&phba->lpfc_injerr_wwpn,
1156 0, sizeof(struct lpfc_name));
1160 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1161 "9079 BLKGRD: Injecting reftag error: "
1162 "read lba x%lx\n", (unsigned long)lba);
1168 /* Should we change the Application Tag */
1170 if (phba->lpfc_injerr_wapp_cnt) {
1172 case SCSI_PROT_WRITE_PASS:
1175 * For WRITE_PASS, force the error
1176 * to be sent on the wire. It should
1177 * be detected by the Target.
1178 * If blockoff != 0 error will be
1179 * inserted in middle of the IO.
1182 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1183 "9080 BLKGRD: Injecting apptag error: "
1184 "write lba x%lx + x%x oldappTag x%x\n",
1185 (unsigned long)lba, blockoff,
1186 be16_to_cpu(src->app_tag));
1189 * Save the old app_tag so we can
1190 * restore it on completion.
1193 lpfc_cmd->prot_data_type =
1195 lpfc_cmd->prot_data_segment =
1197 lpfc_cmd->prot_data =
1200 src->app_tag = cpu_to_be16(0xDEAD);
1201 phba->lpfc_injerr_wapp_cnt--;
1202 if (phba->lpfc_injerr_wapp_cnt == 0) {
1203 phba->lpfc_injerr_nportid = 0;
1204 phba->lpfc_injerr_lba =
1205 LPFC_INJERR_LBA_OFF;
1206 memset(&phba->lpfc_injerr_wwpn,
1207 0, sizeof(struct lpfc_name));
1209 rc = BG_ERR_TGT | BG_ERR_CHECK;
1213 case SCSI_PROT_WRITE_INSERT:
1215 * For WRITE_INSERT, force the
1216 * error to be sent on the wire. It should be
1217 * detected by the Target.
1219 /* DEAD will be the apptag on the wire */
1221 phba->lpfc_injerr_wapp_cnt--;
1222 if (phba->lpfc_injerr_wapp_cnt == 0) {
1223 phba->lpfc_injerr_nportid = 0;
1224 phba->lpfc_injerr_lba =
1225 LPFC_INJERR_LBA_OFF;
1226 memset(&phba->lpfc_injerr_wwpn,
1227 0, sizeof(struct lpfc_name));
1229 rc = BG_ERR_TGT | BG_ERR_CHECK;
1231 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1232 "0813 BLKGRD: Injecting apptag error: "
1233 "write lba x%lx\n", (unsigned long)lba);
1235 case SCSI_PROT_WRITE_STRIP:
1237 * For WRITE_STRIP and WRITE_PASS,
1238 * force the error on data
1239 * being copied from SLI-Host to SLI-Port.
1242 phba->lpfc_injerr_wapp_cnt--;
1243 if (phba->lpfc_injerr_wapp_cnt == 0) {
1244 phba->lpfc_injerr_nportid = 0;
1245 phba->lpfc_injerr_lba =
1246 LPFC_INJERR_LBA_OFF;
1247 memset(&phba->lpfc_injerr_wwpn,
1248 0, sizeof(struct lpfc_name));
1252 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1253 "0812 BLKGRD: Injecting apptag error: "
1254 "write lba x%lx\n", (unsigned long)lba);
1258 if (phba->lpfc_injerr_rapp_cnt) {
1260 case SCSI_PROT_READ_INSERT:
1261 case SCSI_PROT_READ_STRIP:
1262 case SCSI_PROT_READ_PASS:
1264 * For READ_STRIP and READ_PASS, force the
1265 * error on data being read off the wire. It
1266 * should force an IO error to the driver.
1269 phba->lpfc_injerr_rapp_cnt--;
1270 if (phba->lpfc_injerr_rapp_cnt == 0) {
1271 phba->lpfc_injerr_nportid = 0;
1272 phba->lpfc_injerr_lba =
1273 LPFC_INJERR_LBA_OFF;
1274 memset(&phba->lpfc_injerr_wwpn,
1275 0, sizeof(struct lpfc_name));
1279 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1280 "0814 BLKGRD: Injecting apptag error: "
1281 "read lba x%lx\n", (unsigned long)lba);
1288 /* Should we change the Guard Tag */
1290 if (phba->lpfc_injerr_wgrd_cnt) {
1292 case SCSI_PROT_WRITE_PASS:
1296 case SCSI_PROT_WRITE_INSERT:
1298 * For WRITE_INSERT, force the
1299 * error to be sent on the wire. It should be
1300 * detected by the Target.
1302 phba->lpfc_injerr_wgrd_cnt--;
1303 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1304 phba->lpfc_injerr_nportid = 0;
1305 phba->lpfc_injerr_lba =
1306 LPFC_INJERR_LBA_OFF;
1307 memset(&phba->lpfc_injerr_wwpn,
1308 0, sizeof(struct lpfc_name));
1311 rc |= BG_ERR_TGT | BG_ERR_SWAP;
1312 /* Signals the caller to swap CRC->CSUM */
1314 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1315 "0817 BLKGRD: Injecting guard error: "
1316 "write lba x%lx\n", (unsigned long)lba);
1318 case SCSI_PROT_WRITE_STRIP:
1320 * For WRITE_STRIP and WRITE_PASS,
1321 * force the error on data
1322 * being copied from SLI-Host to SLI-Port.
1324 phba->lpfc_injerr_wgrd_cnt--;
1325 if (phba->lpfc_injerr_wgrd_cnt == 0) {
1326 phba->lpfc_injerr_nportid = 0;
1327 phba->lpfc_injerr_lba =
1328 LPFC_INJERR_LBA_OFF;
1329 memset(&phba->lpfc_injerr_wwpn,
1330 0, sizeof(struct lpfc_name));
1333 rc = BG_ERR_INIT | BG_ERR_SWAP;
1334 /* Signals the caller to swap CRC->CSUM */
1336 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1337 "0816 BLKGRD: Injecting guard error: "
1338 "write lba x%lx\n", (unsigned long)lba);
1342 if (phba->lpfc_injerr_rgrd_cnt) {
1344 case SCSI_PROT_READ_INSERT:
1345 case SCSI_PROT_READ_STRIP:
1346 case SCSI_PROT_READ_PASS:
1348 * For READ_STRIP and READ_PASS, force the
1349 * error on data being read off the wire. It
1350 * should force an IO error to the driver.
1352 phba->lpfc_injerr_rgrd_cnt--;
1353 if (phba->lpfc_injerr_rgrd_cnt == 0) {
1354 phba->lpfc_injerr_nportid = 0;
1355 phba->lpfc_injerr_lba =
1356 LPFC_INJERR_LBA_OFF;
1357 memset(&phba->lpfc_injerr_wwpn,
1358 0, sizeof(struct lpfc_name));
1361 rc = BG_ERR_INIT | BG_ERR_SWAP;
1362 /* Signals the caller to swap CRC->CSUM */
1364 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1365 "0818 BLKGRD: Injecting guard error: "
1366 "read lba x%lx\n", (unsigned long)lba);
1376 * lpfc_sc_to_bg_opcodes - Determine the BlockGuard opcodes to be used with
1377 * the specified SCSI command.
1378 * @phba: The Hba for which this call is being executed.
1379 * @sc: The SCSI command to examine
1380 * @txopt: (out) BlockGuard operation for transmitted data
1381 * @rxopt: (out) BlockGuard operation for received data
1383 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1387 lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1388 uint8_t *txop, uint8_t *rxop)
1392 if (lpfc_cmd_guard_csum(sc)) {
1393 switch (scsi_get_prot_op(sc)) {
1394 case SCSI_PROT_READ_INSERT:
1395 case SCSI_PROT_WRITE_STRIP:
1396 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1397 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1400 case SCSI_PROT_READ_STRIP:
1401 case SCSI_PROT_WRITE_INSERT:
1402 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1403 *txop = BG_OP_IN_NODIF_OUT_CRC;
1406 case SCSI_PROT_READ_PASS:
1407 case SCSI_PROT_WRITE_PASS:
1408 *rxop = BG_OP_IN_CRC_OUT_CSUM;
1409 *txop = BG_OP_IN_CSUM_OUT_CRC;
1412 case SCSI_PROT_NORMAL:
1414 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1415 "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
1416 scsi_get_prot_op(sc));
1422 switch (scsi_get_prot_op(sc)) {
1423 case SCSI_PROT_READ_STRIP:
1424 case SCSI_PROT_WRITE_INSERT:
1425 *rxop = BG_OP_IN_CRC_OUT_NODIF;
1426 *txop = BG_OP_IN_NODIF_OUT_CRC;
1429 case SCSI_PROT_READ_PASS:
1430 case SCSI_PROT_WRITE_PASS:
1431 *rxop = BG_OP_IN_CRC_OUT_CRC;
1432 *txop = BG_OP_IN_CRC_OUT_CRC;
1435 case SCSI_PROT_READ_INSERT:
1436 case SCSI_PROT_WRITE_STRIP:
1437 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1438 *txop = BG_OP_IN_CRC_OUT_NODIF;
1441 case SCSI_PROT_NORMAL:
1443 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1444 "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
1445 scsi_get_prot_op(sc));
1454 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1456 * lpfc_bg_err_opcodes - reDetermine the BlockGuard opcodes to be used with
1457 * the specified SCSI command in order to force a guard tag error.
1458 * @phba: The Hba for which this call is being executed.
1459 * @sc: The SCSI command to examine
1460 * @txopt: (out) BlockGuard operation for transmitted data
1461 * @rxopt: (out) BlockGuard operation for received data
1463 * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
1467 lpfc_bg_err_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1468 uint8_t *txop, uint8_t *rxop)
1472 if (lpfc_cmd_guard_csum(sc)) {
1473 switch (scsi_get_prot_op(sc)) {
1474 case SCSI_PROT_READ_INSERT:
1475 case SCSI_PROT_WRITE_STRIP:
1476 *rxop = BG_OP_IN_NODIF_OUT_CRC;
1477 *txop = BG_OP_IN_CRC_OUT_NODIF;
1480 case SCSI_PROT_READ_STRIP:
1481 case SCSI_PROT_WRITE_INSERT:
1482 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1483 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1486 case SCSI_PROT_READ_PASS:
1487 case SCSI_PROT_WRITE_PASS:
1488 *rxop = BG_OP_IN_CSUM_OUT_CRC;
1489 *txop = BG_OP_IN_CRC_OUT_CSUM;
1492 case SCSI_PROT_NORMAL:
1498 switch (scsi_get_prot_op(sc)) {
1499 case SCSI_PROT_READ_STRIP:
1500 case SCSI_PROT_WRITE_INSERT:
1501 *rxop = BG_OP_IN_CSUM_OUT_NODIF;
1502 *txop = BG_OP_IN_NODIF_OUT_CSUM;
1505 case SCSI_PROT_READ_PASS:
1506 case SCSI_PROT_WRITE_PASS:
1507 *rxop = BG_OP_IN_CSUM_OUT_CSUM;
1508 *txop = BG_OP_IN_CSUM_OUT_CSUM;
1511 case SCSI_PROT_READ_INSERT:
1512 case SCSI_PROT_WRITE_STRIP:
1513 *rxop = BG_OP_IN_NODIF_OUT_CSUM;
1514 *txop = BG_OP_IN_CSUM_OUT_NODIF;
1517 case SCSI_PROT_NORMAL:
1528 * lpfc_bg_setup_bpl - Setup BlockGuard BPL with no protection data
1529 * @phba: The Hba for which this call is being executed.
1530 * @sc: pointer to scsi command we're working on
1531 * @bpl: pointer to buffer list for protection groups
1532 * @datacnt: number of segments of data that have been dma mapped
1534 * This function sets up BPL buffer list for protection groups of
1535 * type LPFC_PG_TYPE_NO_DIF
1537 * This is usually used when the HBA is instructed to generate
1538 * DIFs and insert them into data stream (or strip DIF from
1539 * incoming data stream)
1541 * The buffer list consists of just one protection group described
1543 * +-------------------------+
1544 * start of prot group --> | PDE_5 |
1545 * +-------------------------+
1547 * +-------------------------+
1549 * +-------------------------+
1550 * |more Data BDE's ... (opt)|
1551 * +-------------------------+
1554 * Note: Data s/g buffers have been dma mapped
1556 * Returns the number of BDEs added to the BPL.
1559 lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1560 struct ulp_bde64 *bpl, int datasegcnt)
1562 struct scatterlist *sgde = NULL; /* s/g data entry */
1563 struct lpfc_pde5 *pde5 = NULL;
1564 struct lpfc_pde6 *pde6 = NULL;
1565 dma_addr_t physaddr;
1566 int i = 0, num_bde = 0, status;
1567 int datadir = sc->sc_data_direction;
1568 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1571 uint32_t checking = 1;
1575 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1579 /* extract some info from the scsi command for pde*/
1580 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
1582 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1583 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1585 if (rc & BG_ERR_SWAP)
1586 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1587 if (rc & BG_ERR_CHECK)
1592 /* setup PDE5 with what we have */
1593 pde5 = (struct lpfc_pde5 *) bpl;
1594 memset(pde5, 0, sizeof(struct lpfc_pde5));
1595 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1597 /* Endianness conversion if necessary for PDE5 */
1598 pde5->word0 = cpu_to_le32(pde5->word0);
1599 pde5->reftag = cpu_to_le32(reftag);
1601 /* advance bpl and increment bde count */
1604 pde6 = (struct lpfc_pde6 *) bpl;
1606 /* setup PDE6 with the rest of the info */
1607 memset(pde6, 0, sizeof(struct lpfc_pde6));
1608 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1609 bf_set(pde6_optx, pde6, txop);
1610 bf_set(pde6_oprx, pde6, rxop);
1613 * We only need to check the data on READs, for WRITEs
1614 * protection data is automatically generated, not checked.
1616 if (datadir == DMA_FROM_DEVICE) {
1617 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
1618 bf_set(pde6_ce, pde6, checking);
1620 bf_set(pde6_ce, pde6, 0);
1622 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
1623 bf_set(pde6_re, pde6, checking);
1625 bf_set(pde6_re, pde6, 0);
1627 bf_set(pde6_ai, pde6, 1);
1628 bf_set(pde6_ae, pde6, 0);
1629 bf_set(pde6_apptagval, pde6, 0);
1631 /* Endianness conversion if necessary for PDE6 */
1632 pde6->word0 = cpu_to_le32(pde6->word0);
1633 pde6->word1 = cpu_to_le32(pde6->word1);
1634 pde6->word2 = cpu_to_le32(pde6->word2);
1636 /* advance bpl and increment bde count */
1640 /* assumption: caller has already run dma_map_sg on command data */
1641 scsi_for_each_sg(sc, sgde, datasegcnt, i) {
1642 physaddr = sg_dma_address(sgde);
1643 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
1644 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
1645 bpl->tus.f.bdeSize = sg_dma_len(sgde);
1646 if (datadir == DMA_TO_DEVICE)
1647 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1649 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1650 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1660 * lpfc_bg_setup_bpl_prot - Setup BlockGuard BPL with protection data
1661 * @phba: The Hba for which this call is being executed.
1662 * @sc: pointer to scsi command we're working on
1663 * @bpl: pointer to buffer list for protection groups
1664 * @datacnt: number of segments of data that have been dma mapped
1665 * @protcnt: number of segment of protection data that have been dma mapped
1667 * This function sets up BPL buffer list for protection groups of
1668 * type LPFC_PG_TYPE_DIF
1670 * This is usually used when DIFs are in their own buffers,
1671 * separate from the data. The HBA can then by instructed
1672 * to place the DIFs in the outgoing stream. For read operations,
1673 * The HBA could extract the DIFs and place it in DIF buffers.
1675 * The buffer list for this type consists of one or more of the
1676 * protection groups described below:
1677 * +-------------------------+
1678 * start of first prot group --> | PDE_5 |
1679 * +-------------------------+
1681 * +-------------------------+
1682 * | PDE_7 (Prot BDE) |
1683 * +-------------------------+
1685 * +-------------------------+
1686 * |more Data BDE's ... (opt)|
1687 * +-------------------------+
1688 * start of new prot group --> | PDE_5 |
1689 * +-------------------------+
1691 * +-------------------------+
1693 * Note: It is assumed that both data and protection s/g buffers have been
1696 * Returns the number of BDEs added to the BPL.
1699 lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1700 struct ulp_bde64 *bpl, int datacnt, int protcnt)
1702 struct scatterlist *sgde = NULL; /* s/g data entry */
1703 struct scatterlist *sgpe = NULL; /* s/g prot entry */
1704 struct lpfc_pde5 *pde5 = NULL;
1705 struct lpfc_pde6 *pde6 = NULL;
1706 struct lpfc_pde7 *pde7 = NULL;
1707 dma_addr_t dataphysaddr, protphysaddr;
1708 unsigned short curr_data = 0, curr_prot = 0;
1709 unsigned int split_offset;
1710 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
1711 unsigned int protgrp_blks, protgrp_bytes;
1712 unsigned int remainder, subtotal;
1714 int datadir = sc->sc_data_direction;
1715 unsigned char pgdone = 0, alldone = 0;
1717 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1720 uint32_t checking = 1;
1725 sgpe = scsi_prot_sglist(sc);
1726 sgde = scsi_sglist(sc);
1728 if (!sgpe || !sgde) {
1729 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1730 "9020 Invalid s/g entry: data=x%px prot=x%px\n",
1735 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1739 /* extract some info from the scsi command */
1740 blksize = lpfc_cmd_blksize(sc);
1741 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
1743 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1744 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1746 if (rc & BG_ERR_SWAP)
1747 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1748 if (rc & BG_ERR_CHECK)
1755 /* Check to see if we ran out of space */
1756 if (num_bde >= (phba->cfg_total_seg_cnt - 2))
1759 /* setup PDE5 with what we have */
1760 pde5 = (struct lpfc_pde5 *) bpl;
1761 memset(pde5, 0, sizeof(struct lpfc_pde5));
1762 bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
1764 /* Endianness conversion if necessary for PDE5 */
1765 pde5->word0 = cpu_to_le32(pde5->word0);
1766 pde5->reftag = cpu_to_le32(reftag);
1768 /* advance bpl and increment bde count */
1771 pde6 = (struct lpfc_pde6 *) bpl;
1773 /* setup PDE6 with the rest of the info */
1774 memset(pde6, 0, sizeof(struct lpfc_pde6));
1775 bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
1776 bf_set(pde6_optx, pde6, txop);
1777 bf_set(pde6_oprx, pde6, rxop);
1779 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
1780 bf_set(pde6_ce, pde6, checking);
1782 bf_set(pde6_ce, pde6, 0);
1784 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
1785 bf_set(pde6_re, pde6, checking);
1787 bf_set(pde6_re, pde6, 0);
1789 bf_set(pde6_ai, pde6, 1);
1790 bf_set(pde6_ae, pde6, 0);
1791 bf_set(pde6_apptagval, pde6, 0);
1793 /* Endianness conversion if necessary for PDE6 */
1794 pde6->word0 = cpu_to_le32(pde6->word0);
1795 pde6->word1 = cpu_to_le32(pde6->word1);
1796 pde6->word2 = cpu_to_le32(pde6->word2);
1798 /* advance bpl and increment bde count */
1802 /* setup the first BDE that points to protection buffer */
1803 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
1804 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
1806 /* must be integer multiple of the DIF block length */
1807 BUG_ON(protgroup_len % 8);
1809 pde7 = (struct lpfc_pde7 *) bpl;
1810 memset(pde7, 0, sizeof(struct lpfc_pde7));
1811 bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
1813 pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
1814 pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
1816 protgrp_blks = protgroup_len / 8;
1817 protgrp_bytes = protgrp_blks * blksize;
1819 /* check if this pde is crossing the 4K boundary; if so split */
1820 if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
1821 protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
1822 protgroup_offset += protgroup_remainder;
1823 protgrp_blks = protgroup_remainder / 8;
1824 protgrp_bytes = protgrp_blks * blksize;
1826 protgroup_offset = 0;
1832 /* setup BDE's for data blocks associated with DIF data */
1834 subtotal = 0; /* total bytes processed for current prot grp */
1836 /* Check to see if we ran out of space */
1837 if (num_bde >= phba->cfg_total_seg_cnt)
1841 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1842 "9065 BLKGRD:%s Invalid data segment\n",
1847 dataphysaddr = sg_dma_address(sgde) + split_offset;
1848 bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
1849 bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
1851 remainder = sg_dma_len(sgde) - split_offset;
1853 if ((subtotal + remainder) <= protgrp_bytes) {
1854 /* we can use this whole buffer */
1855 bpl->tus.f.bdeSize = remainder;
1858 if ((subtotal + remainder) == protgrp_bytes)
1861 /* must split this buffer with next prot grp */
1862 bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
1863 split_offset += bpl->tus.f.bdeSize;
1866 subtotal += bpl->tus.f.bdeSize;
1868 if (datadir == DMA_TO_DEVICE)
1869 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
1871 bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
1872 bpl->tus.w = le32_to_cpu(bpl->tus.w);
1880 /* Move to the next s/g segment if possible */
1881 sgde = sg_next(sgde);
1885 if (protgroup_offset) {
1886 /* update the reference tag */
1887 reftag += protgrp_blks;
1893 if (curr_prot == protcnt) {
1895 } else if (curr_prot < protcnt) {
1896 /* advance to next prot buffer */
1897 sgpe = sg_next(sgpe);
1900 /* update the reference tag */
1901 reftag += protgrp_blks;
1903 /* if we're here, we have a bug */
1904 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
1905 "9054 BLKGRD: bug in %s\n", __func__);
1915 * lpfc_bg_setup_sgl - Setup BlockGuard SGL with no protection data
1916 * @phba: The Hba for which this call is being executed.
1917 * @sc: pointer to scsi command we're working on
1918 * @sgl: pointer to buffer list for protection groups
1919 * @datacnt: number of segments of data that have been dma mapped
1921 * This function sets up SGL buffer list for protection groups of
1922 * type LPFC_PG_TYPE_NO_DIF
1924 * This is usually used when the HBA is instructed to generate
1925 * DIFs and insert them into data stream (or strip DIF from
1926 * incoming data stream)
1928 * The buffer list consists of just one protection group described
1930 * +-------------------------+
1931 * start of prot group --> | DI_SEED |
1932 * +-------------------------+
1934 * +-------------------------+
1935 * |more Data SGE's ... (opt)|
1936 * +-------------------------+
1939 * Note: Data s/g buffers have been dma mapped
1941 * Returns the number of SGEs added to the SGL.
1944 lpfc_bg_setup_sgl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
1945 struct sli4_sge *sgl, int datasegcnt,
1946 struct lpfc_io_buf *lpfc_cmd)
1948 struct scatterlist *sgde = NULL; /* s/g data entry */
1949 struct sli4_sge_diseed *diseed = NULL;
1950 dma_addr_t physaddr;
1951 int i = 0, num_sge = 0, status;
1954 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1957 uint32_t checking = 1;
1959 uint32_t dma_offset = 0;
1960 struct sli4_hybrid_sgl *sgl_xtra = NULL;
1962 bool lsp_just_set = false;
1964 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
1968 /* extract some info from the scsi command for pde*/
1969 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
1971 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
1972 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
1974 if (rc & BG_ERR_SWAP)
1975 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
1976 if (rc & BG_ERR_CHECK)
1981 /* setup DISEED with what we have */
1982 diseed = (struct sli4_sge_diseed *) sgl;
1983 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
1984 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
1986 /* Endianness conversion if necessary */
1987 diseed->ref_tag = cpu_to_le32(reftag);
1988 diseed->ref_tag_tran = diseed->ref_tag;
1991 * We only need to check the data on READs, for WRITEs
1992 * protection data is automatically generated, not checked.
1994 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
1995 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD))
1996 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
1998 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2000 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2001 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2003 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2006 /* setup DISEED with the rest of the info */
2007 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2008 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2010 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2011 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2013 /* Endianness conversion if necessary for DISEED */
2014 diseed->word2 = cpu_to_le32(diseed->word2);
2015 diseed->word3 = cpu_to_le32(diseed->word3);
2017 /* advance bpl and increment sge count */
2021 /* assumption: caller has already run dma_map_sg on command data */
2022 sgde = scsi_sglist(sc);
2024 for (i = 0; i < datasegcnt; i++) {
2028 /* do we need to expand the segment */
2029 if (!lsp_just_set && !((j + 1) % phba->border_sge_num) &&
2030 ((datasegcnt - 1) != i)) {
2032 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2034 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2036 if (unlikely(!sgl_xtra)) {
2037 lpfc_cmd->seg_cnt = 0;
2040 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2041 sgl_xtra->dma_phys_sgl));
2042 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2043 sgl_xtra->dma_phys_sgl));
2046 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
2049 if (!(bf_get(lpfc_sli4_sge_type, sgl) & LPFC_SGE_TYPE_LSP)) {
2050 if ((datasegcnt - 1) == i)
2051 bf_set(lpfc_sli4_sge_last, sgl, 1);
2052 physaddr = sg_dma_address(sgde);
2053 dma_len = sg_dma_len(sgde);
2054 sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
2055 sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
2057 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2058 sgl->word2 = cpu_to_le32(sgl->word2);
2059 sgl->sge_len = cpu_to_le32(dma_len);
2061 dma_offset += dma_len;
2062 sgde = sg_next(sgde);
2066 lsp_just_set = false;
2069 sgl->word2 = cpu_to_le32(sgl->word2);
2070 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2072 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2075 lsp_just_set = true;
2087 * lpfc_bg_setup_sgl_prot - Setup BlockGuard SGL with protection data
2088 * @phba: The Hba for which this call is being executed.
2089 * @sc: pointer to scsi command we're working on
2090 * @sgl: pointer to buffer list for protection groups
2091 * @datacnt: number of segments of data that have been dma mapped
2092 * @protcnt: number of segment of protection data that have been dma mapped
2094 * This function sets up SGL buffer list for protection groups of
2095 * type LPFC_PG_TYPE_DIF
2097 * This is usually used when DIFs are in their own buffers,
2098 * separate from the data. The HBA can then by instructed
2099 * to place the DIFs in the outgoing stream. For read operations,
2100 * The HBA could extract the DIFs and place it in DIF buffers.
2102 * The buffer list for this type consists of one or more of the
2103 * protection groups described below:
2104 * +-------------------------+
2105 * start of first prot group --> | DISEED |
2106 * +-------------------------+
2107 * | DIF (Prot SGE) |
2108 * +-------------------------+
2110 * +-------------------------+
2111 * |more Data SGE's ... (opt)|
2112 * +-------------------------+
2113 * start of new prot group --> | DISEED |
2114 * +-------------------------+
2116 * +-------------------------+
2118 * Note: It is assumed that both data and protection s/g buffers have been
2121 * Returns the number of SGEs added to the SGL.
2124 lpfc_bg_setup_sgl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
2125 struct sli4_sge *sgl, int datacnt, int protcnt,
2126 struct lpfc_io_buf *lpfc_cmd)
2128 struct scatterlist *sgde = NULL; /* s/g data entry */
2129 struct scatterlist *sgpe = NULL; /* s/g prot entry */
2130 struct sli4_sge_diseed *diseed = NULL;
2131 dma_addr_t dataphysaddr, protphysaddr;
2132 unsigned short curr_data = 0, curr_prot = 0;
2133 unsigned int split_offset;
2134 unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
2135 unsigned int protgrp_blks, protgrp_bytes;
2136 unsigned int remainder, subtotal;
2138 unsigned char pgdone = 0, alldone = 0;
2143 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2146 uint32_t checking = 1;
2147 uint32_t dma_offset = 0;
2148 int num_sge = 0, j = 2;
2149 struct sli4_hybrid_sgl *sgl_xtra = NULL;
2151 sgpe = scsi_prot_sglist(sc);
2152 sgde = scsi_sglist(sc);
2154 if (!sgpe || !sgde) {
2155 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2156 "9082 Invalid s/g entry: data=x%px prot=x%px\n",
2161 status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
2165 /* extract some info from the scsi command */
2166 blksize = lpfc_cmd_blksize(sc);
2167 reftag = (uint32_t)scsi_get_lba(sc); /* Truncate LBA */
2169 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
2170 rc = lpfc_bg_err_inject(phba, sc, &reftag, NULL, 1);
2172 if (rc & BG_ERR_SWAP)
2173 lpfc_bg_err_opcodes(phba, sc, &txop, &rxop);
2174 if (rc & BG_ERR_CHECK)
2181 /* Check to see if we ran out of space */
2182 if ((num_sge >= (phba->cfg_total_seg_cnt - 2)) &&
2186 /* DISEED and DIF have to be together */
2187 if (!((j + 1) % phba->border_sge_num) ||
2188 !((j + 2) % phba->border_sge_num) ||
2189 !((j + 3) % phba->border_sge_num)) {
2193 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_LSP);
2195 sgl_xtra = lpfc_get_sgl_per_hdwq(phba, lpfc_cmd);
2197 if (unlikely(!sgl_xtra)) {
2200 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2201 sgl_xtra->dma_phys_sgl));
2202 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2203 sgl_xtra->dma_phys_sgl));
2206 sgl->word2 = cpu_to_le32(sgl->word2);
2207 sgl->sge_len = cpu_to_le32(phba->cfg_sg_dma_buf_size);
2209 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2213 /* setup DISEED with what we have */
2214 diseed = (struct sli4_sge_diseed *) sgl;
2215 memset(diseed, 0, sizeof(struct sli4_sge_diseed));
2216 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DISEED);
2218 /* Endianness conversion if necessary */
2219 diseed->ref_tag = cpu_to_le32(reftag);
2220 diseed->ref_tag_tran = diseed->ref_tag;
2222 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_GUARD)) {
2223 bf_set(lpfc_sli4_sge_dif_ce, diseed, checking);
2226 bf_set(lpfc_sli4_sge_dif_ce, diseed, 0);
2228 * When in this mode, the hardware will replace
2229 * the guard tag from the host with a
2230 * newly generated good CRC for the wire.
2231 * Switch to raw mode here to avoid this
2232 * behavior. What the host sends gets put on the wire.
2234 if (txop == BG_OP_IN_CRC_OUT_CRC) {
2235 txop = BG_OP_RAW_MODE;
2236 rxop = BG_OP_RAW_MODE;
2241 if (lpfc_cmd_protect(sc, LPFC_CHECK_PROTECT_REF))
2242 bf_set(lpfc_sli4_sge_dif_re, diseed, checking);
2244 bf_set(lpfc_sli4_sge_dif_re, diseed, 0);
2246 /* setup DISEED with the rest of the info */
2247 bf_set(lpfc_sli4_sge_dif_optx, diseed, txop);
2248 bf_set(lpfc_sli4_sge_dif_oprx, diseed, rxop);
2250 bf_set(lpfc_sli4_sge_dif_ai, diseed, 1);
2251 bf_set(lpfc_sli4_sge_dif_me, diseed, 0);
2253 /* Endianness conversion if necessary for DISEED */
2254 diseed->word2 = cpu_to_le32(diseed->word2);
2255 diseed->word3 = cpu_to_le32(diseed->word3);
2257 /* advance sgl and increment bde count */
2263 /* setup the first BDE that points to protection buffer */
2264 protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
2265 protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
2267 /* must be integer multiple of the DIF block length */
2268 BUG_ON(protgroup_len % 8);
2270 /* Now setup DIF SGE */
2272 bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DIF);
2273 sgl->addr_hi = le32_to_cpu(putPaddrHigh(protphysaddr));
2274 sgl->addr_lo = le32_to_cpu(putPaddrLow(protphysaddr));
2275 sgl->word2 = cpu_to_le32(sgl->word2);
2278 protgrp_blks = protgroup_len / 8;
2279 protgrp_bytes = protgrp_blks * blksize;
2281 /* check if DIF SGE is crossing the 4K boundary; if so split */
2282 if ((sgl->addr_lo & 0xfff) + protgroup_len > 0x1000) {
2283 protgroup_remainder = 0x1000 - (sgl->addr_lo & 0xfff);
2284 protgroup_offset += protgroup_remainder;
2285 protgrp_blks = protgroup_remainder / 8;
2286 protgrp_bytes = protgrp_blks * blksize;
2288 protgroup_offset = 0;
2294 /* setup SGE's for data blocks associated with DIF data */
2296 subtotal = 0; /* total bytes processed for current prot grp */
2302 /* Check to see if we ran out of space */
2303 if ((num_sge >= phba->cfg_total_seg_cnt) &&
2308 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2309 "9086 BLKGRD:%s Invalid data segment\n",
2314 if (!((j + 1) % phba->border_sge_num)) {
2318 bf_set(lpfc_sli4_sge_type, sgl,
2321 sgl_xtra = lpfc_get_sgl_per_hdwq(phba,
2324 if (unlikely(!sgl_xtra)) {
2327 sgl->addr_lo = cpu_to_le32(
2328 putPaddrLow(sgl_xtra->dma_phys_sgl));
2329 sgl->addr_hi = cpu_to_le32(
2330 putPaddrHigh(sgl_xtra->dma_phys_sgl));
2333 sgl->word2 = cpu_to_le32(sgl->word2);
2334 sgl->sge_len = cpu_to_le32(
2335 phba->cfg_sg_dma_buf_size);
2337 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
2339 dataphysaddr = sg_dma_address(sgde) +
2342 remainder = sg_dma_len(sgde) - split_offset;
2344 if ((subtotal + remainder) <= protgrp_bytes) {
2345 /* we can use this whole buffer */
2346 dma_len = remainder;
2349 if ((subtotal + remainder) ==
2353 /* must split this buffer with next
2356 dma_len = protgrp_bytes - subtotal;
2357 split_offset += dma_len;
2360 subtotal += dma_len;
2363 sgl->addr_lo = cpu_to_le32(putPaddrLow(
2365 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
2367 bf_set(lpfc_sli4_sge_last, sgl, 0);
2368 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
2369 bf_set(lpfc_sli4_sge_type, sgl,
2370 LPFC_SGE_TYPE_DATA);
2372 sgl->sge_len = cpu_to_le32(dma_len);
2373 dma_offset += dma_len;
2384 /* Move to the next s/g segment if possible */
2385 sgde = sg_next(sgde);
2393 if (protgroup_offset) {
2394 /* update the reference tag */
2395 reftag += protgrp_blks;
2400 if (curr_prot == protcnt) {
2401 /* mark the last SGL */
2403 bf_set(lpfc_sli4_sge_last, sgl, 1);
2405 } else if (curr_prot < protcnt) {
2406 /* advance to next prot buffer */
2407 sgpe = sg_next(sgpe);
2409 /* update the reference tag */
2410 reftag += protgrp_blks;
2412 /* if we're here, we have a bug */
2413 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
2414 "9085 BLKGRD: bug in %s\n", __func__);
2425 * lpfc_prot_group_type - Get prtotection group type of SCSI command
2426 * @phba: The Hba for which this call is being executed.
2427 * @sc: pointer to scsi command we're working on
2429 * Given a SCSI command that supports DIF, determine composition of protection
2430 * groups involved in setting up buffer lists
2432 * Returns: Protection group type (with or without DIF)
2436 lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
2438 int ret = LPFC_PG_TYPE_INVALID;
2439 unsigned char op = scsi_get_prot_op(sc);
2442 case SCSI_PROT_READ_STRIP:
2443 case SCSI_PROT_WRITE_INSERT:
2444 ret = LPFC_PG_TYPE_NO_DIF;
2446 case SCSI_PROT_READ_INSERT:
2447 case SCSI_PROT_WRITE_STRIP:
2448 case SCSI_PROT_READ_PASS:
2449 case SCSI_PROT_WRITE_PASS:
2450 ret = LPFC_PG_TYPE_DIF_BUF;
2454 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2455 "9021 Unsupported protection op:%d\n",
2463 * lpfc_bg_scsi_adjust_dl - Adjust SCSI data length for BlockGuard
2464 * @phba: The Hba for which this call is being executed.
2465 * @lpfc_cmd: The scsi buffer which is going to be adjusted.
2467 * Adjust the data length to account for how much data
2468 * is actually on the wire.
2470 * returns the adjusted data length
2473 lpfc_bg_scsi_adjust_dl(struct lpfc_hba *phba,
2474 struct lpfc_io_buf *lpfc_cmd)
2476 struct scsi_cmnd *sc = lpfc_cmd->pCmd;
2479 fcpdl = scsi_bufflen(sc);
2481 /* Check if there is protection data on the wire */
2482 if (sc->sc_data_direction == DMA_FROM_DEVICE) {
2483 /* Read check for protection data */
2484 if (scsi_get_prot_op(sc) == SCSI_PROT_READ_INSERT)
2488 /* Write check for protection data */
2489 if (scsi_get_prot_op(sc) == SCSI_PROT_WRITE_STRIP)
2494 * If we are in DIF Type 1 mode every data block has a 8 byte
2495 * DIF (trailer) attached to it. Must ajust FCP data length
2496 * to account for the protection data.
2498 fcpdl += (fcpdl / lpfc_cmd_blksize(sc)) * 8;
2504 * lpfc_bg_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
2505 * @phba: The Hba for which this call is being executed.
2506 * @lpfc_cmd: The scsi buffer which is going to be prep'ed.
2508 * This is the protection/DIF aware version of
2509 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
2510 * two functions eventually, but for now, it's here.
2511 * RETURNS 0 - SUCCESS,
2512 * 1 - Failed DMA map, retry.
2513 * 2 - Invalid scsi cmd or prot-type. Do not rety.
2516 lpfc_bg_scsi_prep_dma_buf_s3(struct lpfc_hba *phba,
2517 struct lpfc_io_buf *lpfc_cmd)
2519 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
2520 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
2521 struct ulp_bde64 *bpl = (struct ulp_bde64 *)lpfc_cmd->dma_sgl;
2522 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
2523 uint32_t num_bde = 0;
2524 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
2525 int prot_group_type = 0;
2528 struct lpfc_vport *vport = phba->pport;
2531 * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
2532 * fcp_rsp regions to the first data bde entry
2535 if (scsi_sg_count(scsi_cmnd)) {
2537 * The driver stores the segment count returned from pci_map_sg
2538 * because this a count of dma-mappings used to map the use_sg
2539 * pages. They are not guaranteed to be the same for those
2540 * architectures that implement an IOMMU.
2542 datasegcnt = dma_map_sg(&phba->pcidev->dev,
2543 scsi_sglist(scsi_cmnd),
2544 scsi_sg_count(scsi_cmnd), datadir);
2545 if (unlikely(!datasegcnt))
2548 lpfc_cmd->seg_cnt = datasegcnt;
2550 /* First check if data segment count from SCSI Layer is good */
2551 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
2552 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
2557 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
2559 switch (prot_group_type) {
2560 case LPFC_PG_TYPE_NO_DIF:
2562 /* Here we need to add a PDE5 and PDE6 to the count */
2563 if ((lpfc_cmd->seg_cnt + 2) > phba->cfg_total_seg_cnt) {
2568 num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
2570 /* we should have 2 or more entries in buffer list */
2577 case LPFC_PG_TYPE_DIF_BUF:
2579 * This type indicates that protection buffers are
2580 * passed to the driver, so that needs to be prepared
2583 protsegcnt = dma_map_sg(&phba->pcidev->dev,
2584 scsi_prot_sglist(scsi_cmnd),
2585 scsi_prot_sg_count(scsi_cmnd), datadir);
2586 if (unlikely(!protsegcnt)) {
2587 scsi_dma_unmap(scsi_cmnd);
2591 lpfc_cmd->prot_seg_cnt = protsegcnt;
2594 * There is a minimun of 4 BPLs used for every
2595 * protection data segment.
2597 if ((lpfc_cmd->prot_seg_cnt * 4) >
2598 (phba->cfg_total_seg_cnt - 2)) {
2603 num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
2604 datasegcnt, protsegcnt);
2605 /* we should have 3 or more entries in buffer list */
2606 if ((num_bde < 3) ||
2607 (num_bde > phba->cfg_total_seg_cnt)) {
2613 case LPFC_PG_TYPE_INVALID:
2615 scsi_dma_unmap(scsi_cmnd);
2616 lpfc_cmd->seg_cnt = 0;
2618 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2619 "9022 Unexpected protection group %i\n",
2626 * Finish initializing those IOCB fields that are dependent on the
2627 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
2628 * reinitialized since all iocb memory resources are used many times
2629 * for transmit, receive, and continuation bpl's.
2631 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
2632 iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
2633 iocb_cmd->ulpBdeCount = 1;
2634 iocb_cmd->ulpLe = 1;
2636 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
2637 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
2640 * Due to difference in data length between DIF/non-DIF paths,
2641 * we need to set word 4 of IOCB here
2643 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
2646 * For First burst, we may need to adjust the initial transfer
2649 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
2650 (fcpdl < vport->cfg_first_burst_size))
2651 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
2655 if (lpfc_cmd->seg_cnt)
2656 scsi_dma_unmap(scsi_cmnd);
2657 if (lpfc_cmd->prot_seg_cnt)
2658 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
2659 scsi_prot_sg_count(scsi_cmnd),
2660 scsi_cmnd->sc_data_direction);
2662 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
2663 "9023 Cannot setup S/G List for HBA"
2664 "IO segs %d/%d BPL %d SCSI %d: %d %d\n",
2665 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
2666 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
2667 prot_group_type, num_bde);
2669 lpfc_cmd->seg_cnt = 0;
2670 lpfc_cmd->prot_seg_cnt = 0;
2675 * This function calcuates the T10 DIF guard tag
2676 * on the specified data using a CRC algorithmn
2680 lpfc_bg_crc(uint8_t *data, int count)
2685 crc = crc_t10dif(data, count);
2686 x = cpu_to_be16(crc);
2691 * This function calcuates the T10 DIF guard tag
2692 * on the specified data using a CSUM algorithmn
2693 * using ip_compute_csum.
2696 lpfc_bg_csum(uint8_t *data, int count)
2700 ret = ip_compute_csum(data, count);
2705 * This function examines the protection data to try to determine
2706 * what type of T10-DIF error occurred.
2709 lpfc_calc_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
2711 struct scatterlist *sgpe; /* s/g prot entry */
2712 struct scatterlist *sgde; /* s/g data entry */
2713 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2714 struct scsi_dif_tuple *src = NULL;
2715 uint8_t *data_src = NULL;
2717 uint16_t start_app_tag, app_tag;
2718 uint32_t start_ref_tag, ref_tag;
2719 int prot, protsegcnt;
2720 int err_type, len, data_len;
2721 int chk_ref, chk_app, chk_guard;
2725 err_type = BGS_GUARD_ERR_MASK;
2729 /* First check to see if there is protection data to examine */
2730 prot = scsi_get_prot_op(cmd);
2731 if ((prot == SCSI_PROT_READ_STRIP) ||
2732 (prot == SCSI_PROT_WRITE_INSERT) ||
2733 (prot == SCSI_PROT_NORMAL))
2736 /* Currently the driver just supports ref_tag and guard_tag checking */
2741 /* Setup a ptr to the protection data provided by the SCSI host */
2742 sgpe = scsi_prot_sglist(cmd);
2743 protsegcnt = lpfc_cmd->prot_seg_cnt;
2745 if (sgpe && protsegcnt) {
2748 * We will only try to verify guard tag if the segment
2749 * data length is a multiple of the blksize.
2751 sgde = scsi_sglist(cmd);
2752 blksize = lpfc_cmd_blksize(cmd);
2753 data_src = (uint8_t *)sg_virt(sgde);
2754 data_len = sgde->length;
2755 if ((data_len & (blksize - 1)) == 0)
2758 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2759 start_ref_tag = (uint32_t)scsi_get_lba(cmd); /* Truncate LBA */
2760 start_app_tag = src->app_tag;
2762 while (src && protsegcnt) {
2766 * First check to see if a protection data
2769 if ((src->ref_tag == T10_PI_REF_ESCAPE) ||
2770 (src->app_tag == T10_PI_APP_ESCAPE)) {
2775 /* First Guard Tag checking */
2777 guard_tag = src->guard_tag;
2778 if (lpfc_cmd_guard_csum(cmd))
2779 sum = lpfc_bg_csum(data_src,
2782 sum = lpfc_bg_crc(data_src,
2784 if ((guard_tag != sum)) {
2785 err_type = BGS_GUARD_ERR_MASK;
2790 /* Reference Tag checking */
2791 ref_tag = be32_to_cpu(src->ref_tag);
2792 if (chk_ref && (ref_tag != start_ref_tag)) {
2793 err_type = BGS_REFTAG_ERR_MASK;
2798 /* App Tag checking */
2799 app_tag = src->app_tag;
2800 if (chk_app && (app_tag != start_app_tag)) {
2801 err_type = BGS_APPTAG_ERR_MASK;
2805 len -= sizeof(struct scsi_dif_tuple);
2810 data_src += blksize;
2811 data_len -= blksize;
2814 * Are we at the end of the Data segment?
2815 * The data segment is only used for Guard
2818 if (chk_guard && (data_len == 0)) {
2820 sgde = sg_next(sgde);
2824 data_src = (uint8_t *)sg_virt(sgde);
2825 data_len = sgde->length;
2826 if ((data_len & (blksize - 1)) == 0)
2831 /* Goto the next Protection data segment */
2832 sgpe = sg_next(sgpe);
2834 src = (struct scsi_dif_tuple *)sg_virt(sgpe);
2843 if (err_type == BGS_GUARD_ERR_MASK) {
2844 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2846 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2847 SAM_STAT_CHECK_CONDITION;
2848 phba->bg_guard_err_cnt++;
2849 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2850 "9069 BLKGRD: LBA %lx grd_tag error %x != %x\n",
2851 (unsigned long)scsi_get_lba(cmd),
2854 } else if (err_type == BGS_REFTAG_ERR_MASK) {
2855 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2857 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2858 SAM_STAT_CHECK_CONDITION;
2860 phba->bg_reftag_err_cnt++;
2861 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2862 "9066 BLKGRD: LBA %lx ref_tag error %x != %x\n",
2863 (unsigned long)scsi_get_lba(cmd),
2864 ref_tag, start_ref_tag);
2866 } else if (err_type == BGS_APPTAG_ERR_MASK) {
2867 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2869 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2870 SAM_STAT_CHECK_CONDITION;
2872 phba->bg_apptag_err_cnt++;
2873 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2874 "9041 BLKGRD: LBA %lx app_tag error %x != %x\n",
2875 (unsigned long)scsi_get_lba(cmd),
2876 app_tag, start_app_tag);
2882 * This function checks for BlockGuard errors detected by
2883 * the HBA. In case of errors, the ASC/ASCQ fields in the
2884 * sense buffer will be set accordingly, paired with
2885 * ILLEGAL_REQUEST to signal to the kernel that the HBA
2886 * detected corruption.
2889 * 0 - No error found
2890 * 1 - BlockGuard error found
2891 * -1 - Internal error (bad profile, ...etc)
2894 lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd,
2895 struct lpfc_iocbq *pIocbOut)
2897 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
2898 struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
2900 uint32_t bghm = bgf->bghm;
2901 uint32_t bgstat = bgf->bgstat;
2902 uint64_t failing_sector = 0;
2904 if (lpfc_bgs_get_invalid_prof(bgstat)) {
2905 cmd->result = DID_ERROR << 16;
2906 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2907 "9072 BLKGRD: Invalid BG Profile in cmd"
2908 " 0x%x lba 0x%llx blk cnt 0x%x "
2909 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2910 (unsigned long long)scsi_get_lba(cmd),
2911 blk_rq_sectors(cmd->request), bgstat, bghm);
2916 if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
2917 cmd->result = DID_ERROR << 16;
2918 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2919 "9073 BLKGRD: Invalid BG PDIF Block in cmd"
2920 " 0x%x lba 0x%llx blk cnt 0x%x "
2921 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2922 (unsigned long long)scsi_get_lba(cmd),
2923 blk_rq_sectors(cmd->request), bgstat, bghm);
2928 if (lpfc_bgs_get_guard_err(bgstat)) {
2931 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2933 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2934 SAM_STAT_CHECK_CONDITION;
2935 phba->bg_guard_err_cnt++;
2936 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2937 "9055 BLKGRD: Guard Tag error in cmd"
2938 " 0x%x lba 0x%llx blk cnt 0x%x "
2939 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2940 (unsigned long long)scsi_get_lba(cmd),
2941 blk_rq_sectors(cmd->request), bgstat, bghm);
2944 if (lpfc_bgs_get_reftag_err(bgstat)) {
2947 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2949 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2950 SAM_STAT_CHECK_CONDITION;
2952 phba->bg_reftag_err_cnt++;
2953 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2954 "9056 BLKGRD: Ref Tag error in cmd"
2955 " 0x%x lba 0x%llx blk cnt 0x%x "
2956 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2957 (unsigned long long)scsi_get_lba(cmd),
2958 blk_rq_sectors(cmd->request), bgstat, bghm);
2961 if (lpfc_bgs_get_apptag_err(bgstat)) {
2964 scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
2966 cmd->result = DRIVER_SENSE << 24 | DID_ABORT << 16 |
2967 SAM_STAT_CHECK_CONDITION;
2969 phba->bg_apptag_err_cnt++;
2970 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
2971 "9061 BLKGRD: App Tag error in cmd"
2972 " 0x%x lba 0x%llx blk cnt 0x%x "
2973 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
2974 (unsigned long long)scsi_get_lba(cmd),
2975 blk_rq_sectors(cmd->request), bgstat, bghm);
2978 if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
2980 * setup sense data descriptor 0 per SPC-4 as an information
2981 * field, and put the failing LBA in it.
2982 * This code assumes there was also a guard/app/ref tag error
2985 cmd->sense_buffer[7] = 0xc; /* Additional sense length */
2986 cmd->sense_buffer[8] = 0; /* Information descriptor type */
2987 cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
2988 cmd->sense_buffer[10] = 0x80; /* Validity bit */
2990 /* bghm is a "on the wire" FC frame based count */
2991 switch (scsi_get_prot_op(cmd)) {
2992 case SCSI_PROT_READ_INSERT:
2993 case SCSI_PROT_WRITE_STRIP:
2994 bghm /= cmd->device->sector_size;
2996 case SCSI_PROT_READ_STRIP:
2997 case SCSI_PROT_WRITE_INSERT:
2998 case SCSI_PROT_READ_PASS:
2999 case SCSI_PROT_WRITE_PASS:
3000 bghm /= (cmd->device->sector_size +
3001 sizeof(struct scsi_dif_tuple));
3005 failing_sector = scsi_get_lba(cmd);
3006 failing_sector += bghm;
3008 /* Descriptor Information */
3009 put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
3013 /* No error was reported - problem in FW? */
3014 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_BG,
3015 "9057 BLKGRD: Unknown error in cmd"
3016 " 0x%x lba 0x%llx blk cnt 0x%x "
3017 "bgstat=x%x bghm=x%x\n", cmd->cmnd[0],
3018 (unsigned long long)scsi_get_lba(cmd),
3019 blk_rq_sectors(cmd->request), bgstat, bghm);
3021 /* Calcuate what type of error it was */
3022 lpfc_calc_bg_err(phba, lpfc_cmd);
3029 * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3030 * @phba: The Hba for which this call is being executed.
3031 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3033 * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
3034 * field of @lpfc_cmd for device with SLI-4 interface spec.
3037 * 2 - Error - Do not retry
3042 lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3044 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3045 struct scatterlist *sgel = NULL;
3046 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3047 struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->dma_sgl;
3048 struct sli4_sge *first_data_sgl;
3049 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3050 dma_addr_t physaddr;
3051 uint32_t num_bde = 0;
3053 uint32_t dma_offset = 0;
3055 struct ulp_bde64 *bde;
3056 bool lsp_just_set = false;
3057 struct sli4_hybrid_sgl *sgl_xtra = NULL;
3060 * There are three possibilities here - use scatter-gather segment, use
3061 * the single mapping, or neither. Start the lpfc command prep by
3062 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
3065 if (scsi_sg_count(scsi_cmnd)) {
3067 * The driver stores the segment count returned from pci_map_sg
3068 * because this a count of dma-mappings used to map the use_sg
3069 * pages. They are not guaranteed to be the same for those
3070 * architectures that implement an IOMMU.
3073 nseg = scsi_dma_map(scsi_cmnd);
3074 if (unlikely(nseg <= 0))
3077 /* clear the last flag in the fcp_rsp map entry */
3078 sgl->word2 = le32_to_cpu(sgl->word2);
3079 bf_set(lpfc_sli4_sge_last, sgl, 0);
3080 sgl->word2 = cpu_to_le32(sgl->word2);
3082 first_data_sgl = sgl;
3083 lpfc_cmd->seg_cnt = nseg;
3084 if (!phba->cfg_xpsgl &&
3085 lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
3086 lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
3087 " %s: Too many sg segments from "
3088 "dma_map_sg. Config %d, seg_cnt %d\n",
3089 __func__, phba->cfg_sg_seg_cnt,
3091 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3092 lpfc_cmd->seg_cnt = 0;
3093 scsi_dma_unmap(scsi_cmnd);
3098 * The driver established a maximum scatter-gather segment count
3099 * during probe that limits the number of sg elements in any
3100 * single scsi command. Just run through the seg_cnt and format
3102 * When using SLI-3 the driver will try to fit all the BDEs into
3103 * the IOCB. If it can't then the BDEs get added to a BPL as it
3104 * does for SLI-2 mode.
3107 /* for tracking segment boundaries */
3108 sgel = scsi_sglist(scsi_cmnd);
3110 for (i = 0; i < nseg; i++) {
3112 if ((num_bde + 1) == nseg) {
3113 bf_set(lpfc_sli4_sge_last, sgl, 1);
3114 bf_set(lpfc_sli4_sge_type, sgl,
3115 LPFC_SGE_TYPE_DATA);
3117 bf_set(lpfc_sli4_sge_last, sgl, 0);
3119 /* do we need to expand the segment */
3120 if (!lsp_just_set &&
3121 !((j + 1) % phba->border_sge_num) &&
3122 ((nseg - 1) != i)) {
3124 bf_set(lpfc_sli4_sge_type, sgl,
3127 sgl_xtra = lpfc_get_sgl_per_hdwq(
3130 if (unlikely(!sgl_xtra)) {
3131 lpfc_cmd->seg_cnt = 0;
3132 scsi_dma_unmap(scsi_cmnd);
3135 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3136 sgl_xtra->dma_phys_sgl));
3137 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3138 sgl_xtra->dma_phys_sgl));
3141 bf_set(lpfc_sli4_sge_type, sgl,
3142 LPFC_SGE_TYPE_DATA);
3146 if (!(bf_get(lpfc_sli4_sge_type, sgl) &
3147 LPFC_SGE_TYPE_LSP)) {
3148 if ((nseg - 1) == i)
3149 bf_set(lpfc_sli4_sge_last, sgl, 1);
3151 physaddr = sg_dma_address(sgel);
3152 dma_len = sg_dma_len(sgel);
3153 sgl->addr_lo = cpu_to_le32(putPaddrLow(
3155 sgl->addr_hi = cpu_to_le32(putPaddrHigh(
3158 bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
3159 sgl->word2 = cpu_to_le32(sgl->word2);
3160 sgl->sge_len = cpu_to_le32(dma_len);
3162 dma_offset += dma_len;
3163 sgel = sg_next(sgel);
3166 lsp_just_set = false;
3169 sgl->word2 = cpu_to_le32(sgl->word2);
3170 sgl->sge_len = cpu_to_le32(
3171 phba->cfg_sg_dma_buf_size);
3173 sgl = (struct sli4_sge *)sgl_xtra->dma_sgl;
3176 lsp_just_set = true;
3182 * Setup the first Payload BDE. For FCoE we just key off
3183 * Performance Hints, for FC we use lpfc_enable_pbde.
3184 * We populate words 13-15 of IOCB/WQE.
3186 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3187 phba->cfg_enable_pbde) {
3188 bde = (struct ulp_bde64 *)
3189 &(iocb_cmd->unsli3.sli3Words[5]);
3190 bde->addrLow = first_data_sgl->addr_lo;
3191 bde->addrHigh = first_data_sgl->addr_hi;
3192 bde->tus.f.bdeSize =
3193 le32_to_cpu(first_data_sgl->sge_len);
3194 bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
3195 bde->tus.w = cpu_to_le32(bde->tus.w);
3199 /* clear the last flag in the fcp_rsp map entry */
3200 sgl->word2 = le32_to_cpu(sgl->word2);
3201 bf_set(lpfc_sli4_sge_last, sgl, 1);
3202 sgl->word2 = cpu_to_le32(sgl->word2);
3204 if ((phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) ||
3205 phba->cfg_enable_pbde) {
3206 bde = (struct ulp_bde64 *)
3207 &(iocb_cmd->unsli3.sli3Words[5]);
3208 memset(bde, 0, (sizeof(uint32_t) * 3));
3213 * Finish initializing those IOCB fields that are dependent on the
3214 * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
3215 * explicitly reinitialized.
3216 * all iocb memory resources are reused.
3218 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
3221 * Due to difference in data length between DIF/non-DIF paths,
3222 * we need to set word 4 of IOCB here
3224 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
3227 * If the OAS driver feature is enabled and the lun is enabled for
3228 * OAS, set the oas iocb related flags.
3230 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3231 scsi_cmnd->device->hostdata)->oas_enabled) {
3232 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3233 lpfc_cmd->cur_iocbq.priority = ((struct lpfc_device_data *)
3234 scsi_cmnd->device->hostdata)->priority;
3241 * lpfc_bg_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
3242 * @phba: The Hba for which this call is being executed.
3243 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3245 * This is the protection/DIF aware version of
3246 * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
3247 * two functions eventually, but for now, it's here
3249 * 2 - Error - Do not retry
3254 lpfc_bg_scsi_prep_dma_buf_s4(struct lpfc_hba *phba,
3255 struct lpfc_io_buf *lpfc_cmd)
3257 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
3258 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
3259 struct sli4_sge *sgl = (struct sli4_sge *)(lpfc_cmd->dma_sgl);
3260 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
3261 uint32_t num_sge = 0;
3262 int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
3263 int prot_group_type = 0;
3266 struct lpfc_vport *vport = phba->pport;
3269 * Start the lpfc command prep by bumping the sgl beyond fcp_cmnd
3270 * fcp_rsp regions to the first data sge entry
3272 if (scsi_sg_count(scsi_cmnd)) {
3274 * The driver stores the segment count returned from pci_map_sg
3275 * because this a count of dma-mappings used to map the use_sg
3276 * pages. They are not guaranteed to be the same for those
3277 * architectures that implement an IOMMU.
3279 datasegcnt = dma_map_sg(&phba->pcidev->dev,
3280 scsi_sglist(scsi_cmnd),
3281 scsi_sg_count(scsi_cmnd), datadir);
3282 if (unlikely(!datasegcnt))
3286 /* clear the last flag in the fcp_rsp map entry */
3287 sgl->word2 = le32_to_cpu(sgl->word2);
3288 bf_set(lpfc_sli4_sge_last, sgl, 0);
3289 sgl->word2 = cpu_to_le32(sgl->word2);
3292 lpfc_cmd->seg_cnt = datasegcnt;
3294 /* First check if data segment count from SCSI Layer is good */
3295 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt &&
3297 WARN_ON_ONCE(lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt);
3302 prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
3304 switch (prot_group_type) {
3305 case LPFC_PG_TYPE_NO_DIF:
3306 /* Here we need to add a DISEED to the count */
3307 if (((lpfc_cmd->seg_cnt + 1) >
3308 phba->cfg_total_seg_cnt) &&
3314 num_sge = lpfc_bg_setup_sgl(phba, scsi_cmnd, sgl,
3315 datasegcnt, lpfc_cmd);
3317 /* we should have 2 or more entries in buffer list */
3324 case LPFC_PG_TYPE_DIF_BUF:
3326 * This type indicates that protection buffers are
3327 * passed to the driver, so that needs to be prepared
3330 protsegcnt = dma_map_sg(&phba->pcidev->dev,
3331 scsi_prot_sglist(scsi_cmnd),
3332 scsi_prot_sg_count(scsi_cmnd), datadir);
3333 if (unlikely(!protsegcnt)) {
3334 scsi_dma_unmap(scsi_cmnd);
3338 lpfc_cmd->prot_seg_cnt = protsegcnt;
3340 * There is a minimun of 3 SGEs used for every
3341 * protection data segment.
3343 if (((lpfc_cmd->prot_seg_cnt * 3) >
3344 (phba->cfg_total_seg_cnt - 2)) &&
3350 num_sge = lpfc_bg_setup_sgl_prot(phba, scsi_cmnd, sgl,
3351 datasegcnt, protsegcnt, lpfc_cmd);
3353 /* we should have 3 or more entries in buffer list */
3355 (num_sge > phba->cfg_total_seg_cnt &&
3356 !phba->cfg_xpsgl)) {
3362 case LPFC_PG_TYPE_INVALID:
3364 scsi_dma_unmap(scsi_cmnd);
3365 lpfc_cmd->seg_cnt = 0;
3367 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3368 "9083 Unexpected protection group %i\n",
3374 switch (scsi_get_prot_op(scsi_cmnd)) {
3375 case SCSI_PROT_WRITE_STRIP:
3376 case SCSI_PROT_READ_STRIP:
3377 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_STRIP;
3379 case SCSI_PROT_WRITE_INSERT:
3380 case SCSI_PROT_READ_INSERT:
3381 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_INSERT;
3383 case SCSI_PROT_WRITE_PASS:
3384 case SCSI_PROT_READ_PASS:
3385 lpfc_cmd->cur_iocbq.iocb_flag |= LPFC_IO_DIF_PASS;
3389 fcpdl = lpfc_bg_scsi_adjust_dl(phba, lpfc_cmd);
3390 fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
3393 * Due to difference in data length between DIF/non-DIF paths,
3394 * we need to set word 4 of IOCB here
3396 iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
3399 * For First burst, we may need to adjust the initial transfer
3402 if (iocb_cmd->un.fcpi.fcpi_XRdy &&
3403 (fcpdl < vport->cfg_first_burst_size))
3404 iocb_cmd->un.fcpi.fcpi_XRdy = fcpdl;
3407 * If the OAS driver feature is enabled and the lun is enabled for
3408 * OAS, set the oas iocb related flags.
3410 if ((phba->cfg_fof) && ((struct lpfc_device_data *)
3411 scsi_cmnd->device->hostdata)->oas_enabled)
3412 lpfc_cmd->cur_iocbq.iocb_flag |= (LPFC_IO_OAS | LPFC_IO_FOF);
3416 if (lpfc_cmd->seg_cnt)
3417 scsi_dma_unmap(scsi_cmnd);
3418 if (lpfc_cmd->prot_seg_cnt)
3419 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(scsi_cmnd),
3420 scsi_prot_sg_count(scsi_cmnd),
3421 scsi_cmnd->sc_data_direction);
3423 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
3424 "9084 Cannot setup S/G List for HBA"
3425 "IO segs %d/%d SGL %d SCSI %d: %d %d\n",
3426 lpfc_cmd->seg_cnt, lpfc_cmd->prot_seg_cnt,
3427 phba->cfg_total_seg_cnt, phba->cfg_sg_seg_cnt,
3428 prot_group_type, num_sge);
3430 lpfc_cmd->seg_cnt = 0;
3431 lpfc_cmd->prot_seg_cnt = 0;
3436 * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3437 * @phba: The Hba for which this call is being executed.
3438 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3440 * This routine wraps the actual DMA mapping function pointer from the
3448 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3450 return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
3454 * lpfc_bg_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
3456 * @phba: The Hba for which this call is being executed.
3457 * @lpfc_cmd: The scsi buffer which is going to be mapped.
3459 * This routine wraps the actual DMA mapping function pointer from the
3467 lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *lpfc_cmd)
3469 return phba->lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
3473 * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
3474 * @phba: Pointer to hba context object.
3475 * @vport: Pointer to vport object.
3476 * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
3477 * @rsp_iocb: Pointer to response iocb object which reported error.
3479 * This function posts an event when there is a SCSI command reporting
3480 * error from the scsi device.
3483 lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
3484 struct lpfc_io_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
3485 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3486 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3487 uint32_t resp_info = fcprsp->rspStatus2;
3488 uint32_t scsi_status = fcprsp->rspStatus3;
3489 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3490 struct lpfc_fast_path_event *fast_path_evt = NULL;
3491 struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
3492 unsigned long flags;
3494 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
3497 /* If there is queuefull or busy condition send a scsi event */
3498 if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
3499 (cmnd->result == SAM_STAT_BUSY)) {
3500 fast_path_evt = lpfc_alloc_fast_evt(phba);
3503 fast_path_evt->un.scsi_evt.event_type =
3505 fast_path_evt->un.scsi_evt.subcategory =
3506 (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
3507 LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
3508 fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
3509 memcpy(&fast_path_evt->un.scsi_evt.wwpn,
3510 &pnode->nlp_portname, sizeof(struct lpfc_name));
3511 memcpy(&fast_path_evt->un.scsi_evt.wwnn,
3512 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3513 } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
3514 ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
3515 fast_path_evt = lpfc_alloc_fast_evt(phba);
3518 fast_path_evt->un.check_cond_evt.scsi_event.event_type =
3520 fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
3521 LPFC_EVENT_CHECK_COND;
3522 fast_path_evt->un.check_cond_evt.scsi_event.lun =
3524 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
3525 &pnode->nlp_portname, sizeof(struct lpfc_name));
3526 memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
3527 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3528 fast_path_evt->un.check_cond_evt.sense_key =
3529 cmnd->sense_buffer[2] & 0xf;
3530 fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
3531 fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
3532 } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
3534 ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
3535 ((scsi_status == SAM_STAT_GOOD) &&
3536 !(resp_info & (RESID_UNDER | RESID_OVER))))) {
3538 * If status is good or resid does not match with fcp_param and
3539 * there is valid fcpi_parm, then there is a read_check error
3541 fast_path_evt = lpfc_alloc_fast_evt(phba);
3544 fast_path_evt->un.read_check_error.header.event_type =
3545 FC_REG_FABRIC_EVENT;
3546 fast_path_evt->un.read_check_error.header.subcategory =
3547 LPFC_EVENT_FCPRDCHKERR;
3548 memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
3549 &pnode->nlp_portname, sizeof(struct lpfc_name));
3550 memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
3551 &pnode->nlp_nodename, sizeof(struct lpfc_name));
3552 fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
3553 fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
3554 fast_path_evt->un.read_check_error.fcpiparam =
3559 fast_path_evt->vport = vport;
3560 spin_lock_irqsave(&phba->hbalock, flags);
3561 list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
3562 spin_unlock_irqrestore(&phba->hbalock, flags);
3563 lpfc_worker_wake_up(phba);
3568 * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
3569 * @phba: The HBA for which this call is being executed.
3570 * @psb: The scsi buffer which is going to be un-mapped.
3572 * This routine does DMA un-mapping of scatter gather list of scsi command
3573 * field of @lpfc_cmd for device with SLI-3 interface spec.
3576 lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_io_buf *psb)
3579 * There are only two special cases to consider. (1) the scsi command
3580 * requested scatter-gather usage or (2) the scsi command allocated
3581 * a request buffer, but did not request use_sg. There is a third
3582 * case, but it does not require resource deallocation.
3584 if (psb->seg_cnt > 0)
3585 scsi_dma_unmap(psb->pCmd);
3586 if (psb->prot_seg_cnt > 0)
3587 dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
3588 scsi_prot_sg_count(psb->pCmd),
3589 psb->pCmd->sc_data_direction);
3593 * lpfc_handler_fcp_err - FCP response handler
3594 * @vport: The virtual port for which this call is being executed.
3595 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
3596 * @rsp_iocb: The response IOCB which contains FCP error.
3598 * This routine is called to process response IOCB with status field
3599 * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
3600 * based upon SCSI and FCP error.
3603 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
3604 struct lpfc_iocbq *rsp_iocb)
3606 struct lpfc_hba *phba = vport->phba;
3607 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
3608 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
3609 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
3610 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
3611 uint32_t resp_info = fcprsp->rspStatus2;
3612 uint32_t scsi_status = fcprsp->rspStatus3;
3614 uint32_t host_status = DID_OK;
3615 uint32_t rsplen = 0;
3617 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
3621 * If this is a task management command, there is no
3622 * scsi packet associated with this lpfc_cmd. The driver
3625 if (fcpcmd->fcpCntl2) {
3630 if (resp_info & RSP_LEN_VALID) {
3631 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3632 if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
3633 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3634 "2719 Invalid response length: "
3635 "tgt x%x lun x%llx cmnd x%x rsplen x%x\n",
3637 cmnd->device->lun, cmnd->cmnd[0],
3639 host_status = DID_ERROR;
3642 if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
3643 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
3644 "2757 Protocol failure detected during "
3645 "processing of FCP I/O op: "
3646 "tgt x%x lun x%llx cmnd x%x rspInfo3 x%x\n",
3648 cmnd->device->lun, cmnd->cmnd[0],
3650 host_status = DID_ERROR;
3655 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
3656 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
3657 if (snslen > SCSI_SENSE_BUFFERSIZE)
3658 snslen = SCSI_SENSE_BUFFERSIZE;
3660 if (resp_info & RSP_LEN_VALID)
3661 rsplen = be32_to_cpu(fcprsp->rspRspLen);
3662 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
3664 lp = (uint32_t *)cmnd->sense_buffer;
3666 /* special handling for under run conditions */
3667 if (!scsi_status && (resp_info & RESID_UNDER)) {
3668 /* don't log under runs if fcp set... */
3669 if (vport->cfg_log_verbose & LOG_FCP)
3670 logit = LOG_FCP_ERROR;
3671 /* unless operator says so */
3672 if (vport->cfg_log_verbose & LOG_FCP_UNDER)
3673 logit = LOG_FCP_UNDER;
3676 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3677 "9024 FCP command x%x failed: x%x SNS x%x x%x "
3678 "Data: x%x x%x x%x x%x x%x\n",
3679 cmnd->cmnd[0], scsi_status,
3680 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
3681 be32_to_cpu(fcprsp->rspResId),
3682 be32_to_cpu(fcprsp->rspSnsLen),
3683 be32_to_cpu(fcprsp->rspRspLen),
3686 scsi_set_resid(cmnd, 0);
3687 fcpDl = be32_to_cpu(fcpcmd->fcpDl);
3688 if (resp_info & RESID_UNDER) {
3689 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
3691 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
3692 "9025 FCP Underrun, expected %d, "
3693 "residual %d Data: x%x x%x x%x\n",
3695 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
3699 * If there is an under run, check if under run reported by
3700 * storage array is same as the under run reported by HBA.
3701 * If this is not same, there is a dropped frame.
3703 if (fcpi_parm && (scsi_get_resid(cmnd) != fcpi_parm)) {
3704 lpfc_printf_vlog(vport, KERN_WARNING,
3705 LOG_FCP | LOG_FCP_ERROR,
3706 "9026 FCP Read Check Error "
3707 "and Underrun Data: x%x x%x x%x x%x\n",
3709 scsi_get_resid(cmnd), fcpi_parm,
3711 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3712 host_status = DID_ERROR;
3715 * The cmnd->underflow is the minimum number of bytes that must
3716 * be transferred for this command. Provided a sense condition
3717 * is not present, make sure the actual amount transferred is at
3718 * least the underflow value or fail.
3720 if (!(resp_info & SNS_LEN_VALID) &&
3721 (scsi_status == SAM_STAT_GOOD) &&
3722 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
3723 < cmnd->underflow)) {
3724 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
3725 "9027 FCP command x%x residual "
3726 "underrun converted to error "
3727 "Data: x%x x%x x%x\n",
3728 cmnd->cmnd[0], scsi_bufflen(cmnd),
3729 scsi_get_resid(cmnd), cmnd->underflow);
3730 host_status = DID_ERROR;
3732 } else if (resp_info & RESID_OVER) {
3733 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
3734 "9028 FCP command x%x residual overrun error. "
3735 "Data: x%x x%x\n", cmnd->cmnd[0],
3736 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
3737 host_status = DID_ERROR;
3740 * Check SLI validation that all the transfer was actually done
3741 * (fcpi_parm should be zero). Apply check only to reads.
3743 } else if (fcpi_parm) {
3744 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
3745 "9029 FCP %s Check Error xri x%x Data: "
3746 "x%x x%x x%x x%x x%x\n",
3747 ((cmnd->sc_data_direction == DMA_FROM_DEVICE) ?
3749 ((phba->sli_rev == LPFC_SLI_REV4) ?
3750 lpfc_cmd->cur_iocbq.sli4_xritag :
3751 rsp_iocb->iocb.ulpContext),
3752 fcpDl, be32_to_cpu(fcprsp->rspResId),
3753 fcpi_parm, cmnd->cmnd[0], scsi_status);
3755 /* There is some issue with the LPe12000 that causes it
3756 * to miscalculate the fcpi_parm and falsely trip this
3757 * recovery logic. Detect this case and don't error when true.
3759 if (fcpi_parm > fcpDl)
3762 switch (scsi_status) {
3764 case SAM_STAT_CHECK_CONDITION:
3765 /* Fabric dropped a data frame. Fail any successful
3766 * command in which we detected dropped frames.
3767 * A status of good or some check conditions could
3768 * be considered a successful command.
3770 host_status = DID_ERROR;
3773 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
3777 cmnd->result = host_status << 16 | scsi_status;
3778 lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
3782 * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
3783 * @phba: The Hba for which this call is being executed.
3784 * @pIocbIn: The command IOCBQ for the scsi cmnd.
3785 * @pIocbOut: The response IOCBQ for the scsi cmnd.
3787 * This routine assigns scsi command result by looking into response IOCB
3788 * status field appropriately. This routine handles QUEUE FULL condition as
3789 * well by ramping down device queue depth.
3792 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
3793 struct lpfc_iocbq *pIocbOut)
3795 struct lpfc_io_buf *lpfc_cmd =
3796 (struct lpfc_io_buf *) pIocbIn->context1;
3797 struct lpfc_vport *vport = pIocbIn->vport;
3798 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
3799 struct lpfc_nodelist *pnode = rdata->pnode;
3800 struct scsi_cmnd *cmd;
3801 unsigned long flags;
3802 struct lpfc_fast_path_event *fast_path_evt;
3803 struct Scsi_Host *shost;
3805 uint32_t logit = LOG_FCP;
3806 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3810 /* Guard against abort handler being called at same time */
3811 spin_lock(&lpfc_cmd->buf_lock);
3813 /* Sanity check on return of outstanding command */
3814 cmd = lpfc_cmd->pCmd;
3815 if (!cmd || !phba) {
3816 lpfc_printf_vlog(vport, KERN_ERR, LOG_INIT,
3817 "2621 IO completion: Not an active IO\n");
3818 spin_unlock(&lpfc_cmd->buf_lock);
3822 idx = lpfc_cmd->cur_iocbq.hba_wqidx;
3823 if (phba->sli4_hba.hdwq)
3824 phba->sli4_hba.hdwq[idx].scsi_cstat.io_cmpls++;
3826 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3827 if (unlikely(phba->cpucheck_on & LPFC_CHECK_SCSI_IO)) {
3828 cpu = raw_smp_processor_id();
3829 if (cpu < LPFC_CHECK_CPU_CNT && phba->sli4_hba.hdwq)
3830 phba->sli4_hba.hdwq[idx].cpucheck_cmpl_io[cpu]++;
3833 shost = cmd->device->host;
3835 lpfc_cmd->result = (pIocbOut->iocb.un.ulpWord[4] & IOERR_PARAM_MASK);
3836 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
3837 /* pick up SLI4 exhange busy status from HBA */
3838 if (pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY)
3839 lpfc_cmd->flags |= LPFC_SBUF_XBUSY;
3841 lpfc_cmd->flags &= ~LPFC_SBUF_XBUSY;
3843 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
3844 if (lpfc_cmd->prot_data_type) {
3845 struct scsi_dif_tuple *src = NULL;
3847 src = (struct scsi_dif_tuple *)lpfc_cmd->prot_data_segment;
3849 * Used to restore any changes to protection
3850 * data for error injection.
3852 switch (lpfc_cmd->prot_data_type) {
3853 case LPFC_INJERR_REFTAG:
3855 lpfc_cmd->prot_data;
3857 case LPFC_INJERR_APPTAG:
3859 (uint16_t)lpfc_cmd->prot_data;
3861 case LPFC_INJERR_GUARD:
3863 (uint16_t)lpfc_cmd->prot_data;
3869 lpfc_cmd->prot_data = 0;
3870 lpfc_cmd->prot_data_type = 0;
3871 lpfc_cmd->prot_data_segment = NULL;
3875 if (unlikely(lpfc_cmd->status)) {
3876 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
3877 (lpfc_cmd->result & IOERR_DRVR_MASK))
3878 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
3879 else if (lpfc_cmd->status >= IOSTAT_CNT)
3880 lpfc_cmd->status = IOSTAT_DEFAULT;
3881 if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR &&
3882 !lpfc_cmd->fcp_rsp->rspStatus3 &&
3883 (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER) &&
3884 !(vport->cfg_log_verbose & LOG_FCP_UNDER))
3887 logit = LOG_FCP | LOG_FCP_UNDER;
3888 lpfc_printf_vlog(vport, KERN_WARNING, logit,
3889 "9030 FCP cmd x%x failed <%d/%lld> "
3890 "status: x%x result: x%x "
3891 "sid: x%x did: x%x oxid: x%x "
3894 cmd->device ? cmd->device->id : 0xffff,
3895 cmd->device ? cmd->device->lun : 0xffff,
3896 lpfc_cmd->status, lpfc_cmd->result,
3898 (pnode) ? pnode->nlp_DID : 0,
3899 phba->sli_rev == LPFC_SLI_REV4 ?
3900 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
3901 pIocbOut->iocb.ulpContext,
3902 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
3904 switch (lpfc_cmd->status) {
3905 case IOSTAT_FCP_RSP_ERROR:
3906 /* Call FCP RSP handler to determine result */
3907 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
3909 case IOSTAT_NPORT_BSY:
3910 case IOSTAT_FABRIC_BSY:
3911 cmd->result = DID_TRANSPORT_DISRUPTED << 16;
3912 fast_path_evt = lpfc_alloc_fast_evt(phba);
3915 fast_path_evt->un.fabric_evt.event_type =
3916 FC_REG_FABRIC_EVENT;
3917 fast_path_evt->un.fabric_evt.subcategory =
3918 (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
3919 LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
3920 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
3921 memcpy(&fast_path_evt->un.fabric_evt.wwpn,
3922 &pnode->nlp_portname,
3923 sizeof(struct lpfc_name));
3924 memcpy(&fast_path_evt->un.fabric_evt.wwnn,
3925 &pnode->nlp_nodename,
3926 sizeof(struct lpfc_name));
3928 fast_path_evt->vport = vport;
3929 fast_path_evt->work_evt.evt =
3930 LPFC_EVT_FASTPATH_MGMT_EVT;
3931 spin_lock_irqsave(&phba->hbalock, flags);
3932 list_add_tail(&fast_path_evt->work_evt.evt_listp,
3934 spin_unlock_irqrestore(&phba->hbalock, flags);
3935 lpfc_worker_wake_up(phba);
3937 case IOSTAT_LOCAL_REJECT:
3938 case IOSTAT_REMOTE_STOP:
3939 if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
3941 IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
3942 lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
3944 IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
3945 cmd->result = DID_NO_CONNECT << 16;
3948 if (lpfc_cmd->result == IOERR_INVALID_RPI ||
3949 lpfc_cmd->result == IOERR_NO_RESOURCES ||
3950 lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
3951 lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
3952 cmd->result = DID_REQUEUE << 16;
3955 if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
3956 lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
3957 pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
3958 if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
3960 * This is a response for a BG enabled
3961 * cmd. Parse BG error
3963 lpfc_parse_bg_err(phba, lpfc_cmd,
3967 lpfc_printf_vlog(vport, KERN_WARNING,
3969 "9031 non-zero BGSTAT "
3970 "on unprotected cmd\n");
3973 if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
3974 && (phba->sli_rev == LPFC_SLI_REV4)
3975 && (pnode && NLP_CHK_NODE_ACT(pnode))) {
3976 /* This IO was aborted by the target, we don't
3977 * know the rxid and because we did not send the
3978 * ABTS we cannot generate and RRQ.
3980 lpfc_set_rrq_active(phba, pnode,
3981 lpfc_cmd->cur_iocbq.sli4_lxritag,
3986 cmd->result = DID_ERROR << 16;
3990 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
3991 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
3992 cmd->result = DID_TRANSPORT_DISRUPTED << 16 |
3995 cmd->result = DID_OK << 16;
3997 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
3998 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
4000 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4001 "0710 Iodone <%d/%llu> cmd x%px, error "
4002 "x%x SNS x%x x%x Data: x%x x%x\n",
4003 cmd->device->id, cmd->device->lun, cmd,
4004 cmd->result, *lp, *(lp + 3), cmd->retries,
4005 scsi_get_resid(cmd));
4008 lpfc_update_stats(vport, lpfc_cmd);
4009 if (vport->cfg_max_scsicmpl_time &&
4010 time_after(jiffies, lpfc_cmd->start_time +
4011 msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
4012 spin_lock_irqsave(shost->host_lock, flags);
4013 if (pnode && NLP_CHK_NODE_ACT(pnode)) {
4014 if (pnode->cmd_qdepth >
4015 atomic_read(&pnode->cmd_pending) &&
4016 (atomic_read(&pnode->cmd_pending) >
4017 LPFC_MIN_TGT_QDEPTH) &&
4018 ((cmd->cmnd[0] == READ_10) ||
4019 (cmd->cmnd[0] == WRITE_10)))
4021 atomic_read(&pnode->cmd_pending);
4023 pnode->last_change_time = jiffies;
4025 spin_unlock_irqrestore(shost->host_lock, flags);
4027 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4029 lpfc_cmd->pCmd = NULL;
4030 spin_unlock(&lpfc_cmd->buf_lock);
4032 /* The sdev is not guaranteed to be valid post scsi_done upcall. */
4033 cmd->scsi_done(cmd);
4036 * If there is an abort thread waiting for command completion
4037 * wake up the thread.
4039 spin_lock(&lpfc_cmd->buf_lock);
4040 lpfc_cmd->cur_iocbq.iocb_flag &= ~LPFC_DRIVER_ABORTED;
4041 if (lpfc_cmd->waitq)
4042 wake_up(lpfc_cmd->waitq);
4043 spin_unlock(&lpfc_cmd->buf_lock);
4045 lpfc_release_scsi_buf(phba, lpfc_cmd);
4049 * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
4050 * @data: A pointer to the immediate command data portion of the IOCB.
4051 * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
4053 * The routine copies the entire FCP command from @fcp_cmnd to @data while
4054 * byte swapping the data to big endian format for transmission on the wire.
4057 lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
4060 for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
4061 i += sizeof(uint32_t), j++) {
4062 ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
4067 * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
4068 * @vport: The virtual port for which this call is being executed.
4069 * @lpfc_cmd: The scsi command which needs to send.
4070 * @pnode: Pointer to lpfc_nodelist.
4072 * This routine initializes fcp_cmnd and iocb data structure from scsi command
4073 * to transfer for device with SLI3 interface spec.
4076 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd,
4077 struct lpfc_nodelist *pnode)
4079 struct lpfc_hba *phba = vport->phba;
4080 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
4081 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
4082 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
4083 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
4084 struct lpfc_sli4_hdw_queue *hdwq = NULL;
4085 int datadir = scsi_cmnd->sc_data_direction;
4091 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
4094 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
4095 /* clear task management bits */
4096 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
4098 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
4099 &lpfc_cmd->fcp_cmnd->fcp_lun);
4101 ptr = &fcp_cmnd->fcpCdb[0];
4102 memcpy(ptr, scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
4103 if (scsi_cmnd->cmd_len < LPFC_FCP_CDB_LEN) {
4104 ptr += scsi_cmnd->cmd_len;
4105 memset(ptr, 0, (LPFC_FCP_CDB_LEN - scsi_cmnd->cmd_len));
4108 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
4110 sli4 = (phba->sli_rev == LPFC_SLI_REV4);
4111 piocbq->iocb.un.fcpi.fcpi_XRdy = 0;
4112 idx = lpfc_cmd->hdwq_no;
4113 if (phba->sli4_hba.hdwq)
4114 hdwq = &phba->sli4_hba.hdwq[idx];
4117 * There are three possibilities here - use scatter-gather segment, use
4118 * the single mapping, or neither. Start the lpfc command prep by
4119 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
4122 if (scsi_sg_count(scsi_cmnd)) {
4123 if (datadir == DMA_TO_DEVICE) {
4124 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
4125 iocb_cmd->ulpPU = PARM_READ_CHECK;
4126 if (vport->cfg_first_burst_size &&
4127 (pnode->nlp_flag & NLP_FIRSTBURST)) {
4128 fcpdl = scsi_bufflen(scsi_cmnd);
4129 if (fcpdl < vport->cfg_first_burst_size)
4130 piocbq->iocb.un.fcpi.fcpi_XRdy = fcpdl;
4132 piocbq->iocb.un.fcpi.fcpi_XRdy =
4133 vport->cfg_first_burst_size;
4135 fcp_cmnd->fcpCntl3 = WRITE_DATA;
4137 hdwq->scsi_cstat.output_requests++;
4139 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
4140 iocb_cmd->ulpPU = PARM_READ_CHECK;
4141 fcp_cmnd->fcpCntl3 = READ_DATA;
4143 hdwq->scsi_cstat.input_requests++;
4146 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
4147 iocb_cmd->un.fcpi.fcpi_parm = 0;
4148 iocb_cmd->ulpPU = 0;
4149 fcp_cmnd->fcpCntl3 = 0;
4151 hdwq->scsi_cstat.control_requests++;
4153 if (phba->sli_rev == 3 &&
4154 !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4155 lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
4157 * Finish initializing those IOCB fields that are independent
4158 * of the scsi_cmnd request_buffer
4160 piocbq->iocb.ulpContext = pnode->nlp_rpi;
4162 piocbq->iocb.ulpContext =
4163 phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
4164 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
4165 piocbq->iocb.ulpFCP2Rcvy = 1;
4167 piocbq->iocb.ulpFCP2Rcvy = 0;
4169 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
4170 piocbq->context1 = lpfc_cmd;
4171 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4172 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
4173 piocbq->vport = vport;
4177 * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
4178 * @vport: The virtual port for which this call is being executed.
4179 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
4180 * @lun: Logical unit number.
4181 * @task_mgmt_cmd: SCSI task management command.
4183 * This routine creates FCP information unit corresponding to @task_mgmt_cmd
4184 * for device with SLI-3 interface spec.
4191 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
4192 struct lpfc_io_buf *lpfc_cmd,
4194 uint8_t task_mgmt_cmd)
4196 struct lpfc_iocbq *piocbq;
4198 struct fcp_cmnd *fcp_cmnd;
4199 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
4200 struct lpfc_nodelist *ndlp = rdata->pnode;
4202 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
4203 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
4206 piocbq = &(lpfc_cmd->cur_iocbq);
4207 piocbq->vport = vport;
4209 piocb = &piocbq->iocb;
4211 fcp_cmnd = lpfc_cmd->fcp_cmnd;
4212 /* Clear out any old data in the FCP command area */
4213 memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
4214 int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
4215 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
4216 if (vport->phba->sli_rev == 3 &&
4217 !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
4218 lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
4219 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
4220 piocb->ulpContext = ndlp->nlp_rpi;
4221 if (vport->phba->sli_rev == LPFC_SLI_REV4) {
4223 vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
4225 piocb->ulpFCP2Rcvy = (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) ? 1 : 0;
4226 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
4228 piocb->un.fcpi.fcpi_parm = 0;
4230 /* ulpTimeout is only one byte */
4231 if (lpfc_cmd->timeout > 0xff) {
4233 * Do not timeout the command at the firmware level.
4234 * The driver will provide the timeout mechanism.
4236 piocb->ulpTimeout = 0;
4238 piocb->ulpTimeout = lpfc_cmd->timeout;
4240 if (vport->phba->sli_rev == LPFC_SLI_REV4)
4241 lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
4247 * lpfc_scsi_api_table_setup - Set up scsi api function jump table
4248 * @phba: The hba struct for which this call is being executed.
4249 * @dev_grp: The HBA PCI-Device group number.
4251 * This routine sets up the SCSI interface API function jump table in @phba
4253 * Returns: 0 - success, -ENODEV - failure.
4256 lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
4259 phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
4260 phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
4263 case LPFC_PCI_DEV_LP:
4264 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
4265 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s3;
4266 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
4267 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
4269 case LPFC_PCI_DEV_OC:
4270 phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
4271 phba->lpfc_bg_scsi_prep_dma_buf = lpfc_bg_scsi_prep_dma_buf_s4;
4272 phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
4273 phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
4276 lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
4277 "1418 Invalid HBA PCI-device group: 0x%x\n",
4282 phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
4283 phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
4288 * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
4289 * @phba: The Hba for which this call is being executed.
4290 * @cmdiocbq: Pointer to lpfc_iocbq data structure.
4291 * @rspiocbq: Pointer to lpfc_iocbq data structure.
4293 * This routine is IOCB completion routine for device reset and target reset
4294 * routine. This routine release scsi buffer associated with lpfc_cmd.
4297 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
4298 struct lpfc_iocbq *cmdiocbq,
4299 struct lpfc_iocbq *rspiocbq)
4301 struct lpfc_io_buf *lpfc_cmd =
4302 (struct lpfc_io_buf *) cmdiocbq->context1;
4304 lpfc_release_scsi_buf(phba, lpfc_cmd);
4309 * lpfc_check_pci_resettable - Walks list of devices on pci_dev's bus to check
4310 * if issuing a pci_bus_reset is possibly unsafe
4311 * @phba: lpfc_hba pointer.
4314 * Walks the bus_list to ensure only PCI devices with Emulex
4315 * vendor id, device ids that support hot reset, and only one occurrence
4319 * -EBADSLT, detected invalid device
4323 lpfc_check_pci_resettable(const struct lpfc_hba *phba)
4325 const struct pci_dev *pdev = phba->pcidev;
4326 struct pci_dev *ptr = NULL;
4329 /* Walk the list of devices on the pci_dev's bus */
4330 list_for_each_entry(ptr, &pdev->bus->devices, bus_list) {
4331 /* Check for Emulex Vendor ID */
4332 if (ptr->vendor != PCI_VENDOR_ID_EMULEX) {
4333 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4334 "8346 Non-Emulex vendor found: "
4335 "0x%04x\n", ptr->vendor);
4339 /* Check for valid Emulex Device ID */
4340 switch (ptr->device) {
4341 case PCI_DEVICE_ID_LANCER_FC:
4342 case PCI_DEVICE_ID_LANCER_G6_FC:
4343 case PCI_DEVICE_ID_LANCER_G7_FC:
4346 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4347 "8347 Invalid device found: "
4348 "0x%04x\n", ptr->device);
4352 /* Check for only one function 0 ID to ensure only one HBA on
4355 if (ptr->devfn == 0) {
4356 if (++counter > 1) {
4357 lpfc_printf_log(phba, KERN_INFO, LOG_INIT,
4358 "8348 More than one device on "
4359 "secondary bus found\n");
4369 * lpfc_info - Info entry point of scsi_host_template data structure
4370 * @host: The scsi host for which this call is being executed.
4372 * This routine provides module information about hba.
4375 * Pointer to char - Success.
4378 lpfc_info(struct Scsi_Host *host)
4380 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
4381 struct lpfc_hba *phba = vport->phba;
4383 static char lpfcinfobuf[384];
4384 char tmp[384] = {0};
4386 memset(lpfcinfobuf, 0, sizeof(lpfcinfobuf));
4387 if (phba && phba->pcidev){
4388 /* Model Description */
4389 scnprintf(tmp, sizeof(tmp), phba->ModelDesc);
4390 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4391 sizeof(lpfcinfobuf))
4395 scnprintf(tmp, sizeof(tmp),
4396 " on PCI bus %02x device %02x irq %d",
4397 phba->pcidev->bus->number, phba->pcidev->devfn,
4399 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4400 sizeof(lpfcinfobuf))
4404 if (phba->Port[0]) {
4405 scnprintf(tmp, sizeof(tmp), " port %s", phba->Port);
4406 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4407 sizeof(lpfcinfobuf))
4412 link_speed = lpfc_sli_port_speed_get(phba);
4413 if (link_speed != 0) {
4414 scnprintf(tmp, sizeof(tmp),
4415 " Logical Link Speed: %d Mbps", link_speed);
4416 if (strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf)) >=
4417 sizeof(lpfcinfobuf))
4421 /* PCI resettable */
4422 if (!lpfc_check_pci_resettable(phba)) {
4423 scnprintf(tmp, sizeof(tmp), " PCI resettable");
4424 strlcat(lpfcinfobuf, tmp, sizeof(lpfcinfobuf));
4433 * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
4434 * @phba: The Hba for which this call is being executed.
4436 * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
4437 * The default value of cfg_poll_tmo is 10 milliseconds.
4439 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
4441 unsigned long poll_tmo_expires =
4442 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
4444 if (!list_empty(&phba->sli.sli3_ring[LPFC_FCP_RING].txcmplq))
4445 mod_timer(&phba->fcp_poll_timer,
4450 * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
4451 * @phba: The Hba for which this call is being executed.
4453 * This routine starts the fcp_poll_timer of @phba.
4455 void lpfc_poll_start_timer(struct lpfc_hba * phba)
4457 lpfc_poll_rearm_timer(phba);
4461 * lpfc_poll_timeout - Restart polling timer
4462 * @ptr: Map to lpfc_hba data structure pointer.
4464 * This routine restarts fcp_poll timer, when FCP ring polling is enable
4465 * and FCP Ring interrupt is disable.
4468 void lpfc_poll_timeout(struct timer_list *t)
4470 struct lpfc_hba *phba = from_timer(phba, t, fcp_poll_timer);
4472 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4473 lpfc_sli_handle_fast_ring_event(phba,
4474 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4476 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4477 lpfc_poll_rearm_timer(phba);
4482 * lpfc_queuecommand - scsi_host_template queuecommand entry point
4483 * @cmnd: Pointer to scsi_cmnd data structure.
4484 * @done: Pointer to done routine.
4486 * Driver registers this routine to scsi midlayer to submit a @cmd to process.
4487 * This routine prepares an IOCB from scsi command and provides to firmware.
4488 * The @done callback is invoked after driver finished processing the command.
4492 * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
4495 lpfc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
4497 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4498 struct lpfc_hba *phba = vport->phba;
4499 struct lpfc_rport_data *rdata;
4500 struct lpfc_nodelist *ndlp;
4501 struct lpfc_io_buf *lpfc_cmd;
4502 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
4504 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4508 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
4510 /* sanity check on references */
4511 if (unlikely(!rdata) || unlikely(!rport))
4512 goto out_fail_command;
4514 err = fc_remote_port_chkready(rport);
4517 goto out_fail_command;
4519 ndlp = rdata->pnode;
4521 if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
4522 (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED))) {
4524 lpfc_printf_log(phba, KERN_ERR, LOG_BG,
4525 "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
4526 " op:%02x str=%s without registering for"
4527 " BlockGuard - Rejecting command\n",
4528 cmnd->cmnd[0], scsi_get_prot_op(cmnd),
4529 dif_op_str[scsi_get_prot_op(cmnd)]);
4530 goto out_fail_command;
4534 * Catch race where our node has transitioned, but the
4535 * transport is still transitioning.
4537 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp))
4539 if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
4540 if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) {
4541 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4542 "3377 Target Queue Full, scsi Id:%d "
4543 "Qdepth:%d Pending command:%d"
4544 " WWNN:%02x:%02x:%02x:%02x:"
4545 "%02x:%02x:%02x:%02x, "
4546 " WWPN:%02x:%02x:%02x:%02x:"
4547 "%02x:%02x:%02x:%02x",
4548 ndlp->nlp_sid, ndlp->cmd_qdepth,
4549 atomic_read(&ndlp->cmd_pending),
4550 ndlp->nlp_nodename.u.wwn[0],
4551 ndlp->nlp_nodename.u.wwn[1],
4552 ndlp->nlp_nodename.u.wwn[2],
4553 ndlp->nlp_nodename.u.wwn[3],
4554 ndlp->nlp_nodename.u.wwn[4],
4555 ndlp->nlp_nodename.u.wwn[5],
4556 ndlp->nlp_nodename.u.wwn[6],
4557 ndlp->nlp_nodename.u.wwn[7],
4558 ndlp->nlp_portname.u.wwn[0],
4559 ndlp->nlp_portname.u.wwn[1],
4560 ndlp->nlp_portname.u.wwn[2],
4561 ndlp->nlp_portname.u.wwn[3],
4562 ndlp->nlp_portname.u.wwn[4],
4563 ndlp->nlp_portname.u.wwn[5],
4564 ndlp->nlp_portname.u.wwn[6],
4565 ndlp->nlp_portname.u.wwn[7]);
4570 lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp, cmnd);
4571 if (lpfc_cmd == NULL) {
4572 lpfc_rampdown_queue_depth(phba);
4574 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_ERROR,
4575 "0707 driver's buffer pool is empty, "
4581 * Store the midlayer's command structure for the completion phase
4582 * and complete the command initialization.
4584 lpfc_cmd->pCmd = cmnd;
4585 lpfc_cmd->rdata = rdata;
4586 lpfc_cmd->ndlp = ndlp;
4587 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
4589 if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
4590 if (vport->phba->cfg_enable_bg) {
4591 lpfc_printf_vlog(vport,
4592 KERN_INFO, LOG_SCSI_CMD,
4593 "9033 BLKGRD: rcvd %s cmd:x%x "
4594 "sector x%llx cnt %u pt %x\n",
4595 dif_op_str[scsi_get_prot_op(cmnd)],
4597 (unsigned long long)scsi_get_lba(cmnd),
4598 blk_rq_sectors(cmnd->request),
4599 (cmnd->cmnd[1]>>5));
4601 err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
4603 if (vport->phba->cfg_enable_bg) {
4604 lpfc_printf_vlog(vport,
4605 KERN_INFO, LOG_SCSI_CMD,
4606 "9038 BLKGRD: rcvd PROT_NORMAL cmd: "
4607 "x%x sector x%llx cnt %u pt %x\n",
4609 (unsigned long long)scsi_get_lba(cmnd),
4610 blk_rq_sectors(cmnd->request),
4611 (cmnd->cmnd[1]>>5));
4613 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
4616 if (unlikely(err)) {
4618 cmnd->result = DID_ERROR << 16;
4619 goto out_fail_command_release_buf;
4621 goto out_host_busy_free_buf;
4624 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
4626 #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
4627 if (unlikely(phba->cpucheck_on & LPFC_CHECK_SCSI_IO)) {
4628 cpu = raw_smp_processor_id();
4629 if (cpu < LPFC_CHECK_CPU_CNT) {
4630 struct lpfc_sli4_hdw_queue *hdwq =
4631 &phba->sli4_hba.hdwq[lpfc_cmd->hdwq_no];
4632 hdwq->cpucheck_xmt_io[cpu]++;
4636 err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4637 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
4639 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4640 "3376 FCP could not issue IOCB err %x"
4641 "FCP cmd x%x <%d/%llu> "
4642 "sid: x%x did: x%x oxid: x%x "
4643 "Data: x%x x%x x%x x%x\n",
4645 cmnd->device ? cmnd->device->id : 0xffff,
4646 cmnd->device ? cmnd->device->lun : (u64) -1,
4647 vport->fc_myDID, ndlp->nlp_DID,
4648 phba->sli_rev == LPFC_SLI_REV4 ?
4649 lpfc_cmd->cur_iocbq.sli4_xritag : 0xffff,
4650 lpfc_cmd->cur_iocbq.iocb.ulpContext,
4651 lpfc_cmd->cur_iocbq.iocb.ulpIoTag,
4652 lpfc_cmd->cur_iocbq.iocb.ulpTimeout,
4654 (cmnd->request->timeout / 1000));
4656 goto out_host_busy_free_buf;
4658 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
4659 lpfc_sli_handle_fast_ring_event(phba,
4660 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4662 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4663 lpfc_poll_rearm_timer(phba);
4666 if (phba->cfg_xri_rebalancing)
4667 lpfc_keep_pvt_pool_above_lowwm(phba, lpfc_cmd->hdwq_no);
4671 out_host_busy_free_buf:
4672 idx = lpfc_cmd->hdwq_no;
4673 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
4674 if (phba->sli4_hba.hdwq) {
4675 switch (lpfc_cmd->fcp_cmnd->fcpCntl3) {
4677 phba->sli4_hba.hdwq[idx].scsi_cstat.output_requests--;
4680 phba->sli4_hba.hdwq[idx].scsi_cstat.input_requests--;
4683 phba->sli4_hba.hdwq[idx].scsi_cstat.control_requests--;
4686 lpfc_release_scsi_buf(phba, lpfc_cmd);
4688 return SCSI_MLQUEUE_HOST_BUSY;
4691 return SCSI_MLQUEUE_TARGET_BUSY;
4693 out_fail_command_release_buf:
4694 lpfc_release_scsi_buf(phba, lpfc_cmd);
4697 cmnd->scsi_done(cmnd);
4703 * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
4704 * @cmnd: Pointer to scsi_cmnd data structure.
4706 * This routine aborts @cmnd pending in base driver.
4713 lpfc_abort_handler(struct scsi_cmnd *cmnd)
4715 struct Scsi_Host *shost = cmnd->device->host;
4716 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
4717 struct lpfc_hba *phba = vport->phba;
4718 struct lpfc_iocbq *iocb;
4719 struct lpfc_iocbq *abtsiocb;
4720 struct lpfc_io_buf *lpfc_cmd;
4722 int ret = SUCCESS, status = 0;
4723 struct lpfc_sli_ring *pring_s4 = NULL;
4725 unsigned long flags;
4726 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
4728 status = fc_block_scsi_eh(cmnd);
4729 if (status != 0 && status != SUCCESS)
4732 lpfc_cmd = (struct lpfc_io_buf *)cmnd->host_scribble;
4736 spin_lock_irqsave(&phba->hbalock, flags);
4737 /* driver queued commands are in process of being flushed */
4738 if (phba->hba_flag & HBA_IOQ_FLUSH) {
4739 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4740 "3168 SCSI Layer abort requested I/O has been "
4741 "flushed by LLD.\n");
4746 /* Guard against IO completion being called at same time */
4747 spin_lock(&lpfc_cmd->buf_lock);
4749 if (!lpfc_cmd->pCmd) {
4750 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4751 "2873 SCSI Layer I/O Abort Request IO CMPL Status "
4752 "x%x ID %d LUN %llu\n",
4753 SUCCESS, cmnd->device->id, cmnd->device->lun);
4754 goto out_unlock_buf;
4757 iocb = &lpfc_cmd->cur_iocbq;
4758 if (phba->sli_rev == LPFC_SLI_REV4) {
4759 pring_s4 = phba->sli4_hba.hdwq[iocb->hba_wqidx].io_wq->pring;
4762 goto out_unlock_buf;
4764 spin_lock(&pring_s4->ring_lock);
4766 /* the command is in process of being cancelled */
4767 if (!(iocb->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
4768 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4769 "3169 SCSI Layer abort requested I/O has been "
4770 "cancelled by LLD.\n");
4772 goto out_unlock_ring;
4775 * If pCmd field of the corresponding lpfc_io_buf structure
4776 * points to a different SCSI command, then the driver has
4777 * already completed this command, but the midlayer did not
4778 * see the completion before the eh fired. Just return SUCCESS.
4780 if (lpfc_cmd->pCmd != cmnd) {
4781 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4782 "3170 SCSI Layer abort requested I/O has been "
4783 "completed by LLD.\n");
4784 goto out_unlock_ring;
4787 BUG_ON(iocb->context1 != lpfc_cmd);
4789 /* abort issued in recovery is still in progress */
4790 if (iocb->iocb_flag & LPFC_DRIVER_ABORTED) {
4791 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4792 "3389 SCSI Layer I/O Abort Request is pending\n");
4793 if (phba->sli_rev == LPFC_SLI_REV4)
4794 spin_unlock(&pring_s4->ring_lock);
4795 spin_unlock(&lpfc_cmd->buf_lock);
4796 spin_unlock_irqrestore(&phba->hbalock, flags);
4800 abtsiocb = __lpfc_sli_get_iocbq(phba);
4801 if (abtsiocb == NULL) {
4803 goto out_unlock_ring;
4806 /* Indicate the IO is being aborted by the driver. */
4807 iocb->iocb_flag |= LPFC_DRIVER_ABORTED;
4810 * The scsi command can not be in txq and it is in flight because the
4811 * pCmd is still pointig at the SCSI command we have to abort. There
4812 * is no need to search the txcmplq. Just send an abort to the FW.
4816 icmd = &abtsiocb->iocb;
4817 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
4818 icmd->un.acxri.abortContextTag = cmd->ulpContext;
4819 if (phba->sli_rev == LPFC_SLI_REV4)
4820 icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
4822 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
4825 icmd->ulpClass = cmd->ulpClass;
4827 /* ABTS WQE must go to the same WQ as the WQE to be aborted */
4828 abtsiocb->hba_wqidx = iocb->hba_wqidx;
4829 abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
4830 if (iocb->iocb_flag & LPFC_IO_FOF)
4831 abtsiocb->iocb_flag |= LPFC_IO_FOF;
4833 if (lpfc_is_link_up(phba))
4834 icmd->ulpCommand = CMD_ABORT_XRI_CN;
4836 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
4838 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
4839 abtsiocb->vport = vport;
4840 lpfc_cmd->waitq = &waitq;
4841 if (phba->sli_rev == LPFC_SLI_REV4) {
4842 /* Note: both hbalock and ring_lock must be set here */
4843 ret_val = __lpfc_sli_issue_iocb(phba, pring_s4->ringno,
4845 spin_unlock(&pring_s4->ring_lock);
4847 ret_val = __lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
4851 if (ret_val == IOCB_ERROR) {
4852 /* Indicate the IO is not being aborted by the driver. */
4853 iocb->iocb_flag &= ~LPFC_DRIVER_ABORTED;
4854 lpfc_cmd->waitq = NULL;
4855 spin_unlock(&lpfc_cmd->buf_lock);
4856 spin_unlock_irqrestore(&phba->hbalock, flags);
4857 lpfc_sli_release_iocbq(phba, abtsiocb);
4862 /* no longer need the lock after this point */
4863 spin_unlock(&lpfc_cmd->buf_lock);
4864 spin_unlock_irqrestore(&phba->hbalock, flags);
4866 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
4867 lpfc_sli_handle_fast_ring_event(phba,
4868 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
4871 /* Wait for abort to complete */
4872 wait_event_timeout(waitq,
4873 (lpfc_cmd->pCmd != cmnd),
4874 msecs_to_jiffies(2*vport->cfg_devloss_tmo*1000));
4876 spin_lock(&lpfc_cmd->buf_lock);
4878 if (lpfc_cmd->pCmd == cmnd) {
4880 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
4881 "0748 abort handler timed out waiting "
4882 "for aborting I/O (xri:x%x) to complete: "
4883 "ret %#x, ID %d, LUN %llu\n",
4884 iocb->sli4_xritag, ret,
4885 cmnd->device->id, cmnd->device->lun);
4888 lpfc_cmd->waitq = NULL;
4890 spin_unlock(&lpfc_cmd->buf_lock);
4894 if (phba->sli_rev == LPFC_SLI_REV4)
4895 spin_unlock(&pring_s4->ring_lock);
4897 spin_unlock(&lpfc_cmd->buf_lock);
4899 spin_unlock_irqrestore(&phba->hbalock, flags);
4901 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
4902 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
4903 "LUN %llu\n", ret, cmnd->device->id,
4909 lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
4911 switch (task_mgmt_cmd) {
4912 case FCP_ABORT_TASK_SET:
4913 return "ABORT_TASK_SET";
4914 case FCP_CLEAR_TASK_SET:
4915 return "FCP_CLEAR_TASK_SET";
4917 return "FCP_BUS_RESET";
4919 return "FCP_LUN_RESET";
4920 case FCP_TARGET_RESET:
4921 return "FCP_TARGET_RESET";
4923 return "FCP_CLEAR_ACA";
4924 case FCP_TERMINATE_TASK:
4925 return "FCP_TERMINATE_TASK";
4933 * lpfc_check_fcp_rsp - check the returned fcp_rsp to see if task failed
4934 * @vport: The virtual port for which this call is being executed.
4935 * @lpfc_cmd: Pointer to lpfc_io_buf data structure.
4937 * This routine checks the FCP RSP INFO to see if the tsk mgmt command succeded
4944 lpfc_check_fcp_rsp(struct lpfc_vport *vport, struct lpfc_io_buf *lpfc_cmd)
4946 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
4949 uint8_t rsp_info_code;
4954 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4955 "0703 fcp_rsp is missing\n");
4957 rsp_info = fcprsp->rspStatus2;
4958 rsp_len = be32_to_cpu(fcprsp->rspRspLen);
4959 rsp_info_code = fcprsp->rspInfo3;
4962 lpfc_printf_vlog(vport, KERN_INFO,
4964 "0706 fcp_rsp valid 0x%x,"
4965 " rsp len=%d code 0x%x\n",
4967 rsp_len, rsp_info_code);
4969 /* If FCP_RSP_LEN_VALID bit is one, then the FCP_RSP_LEN
4970 * field specifies the number of valid bytes of FCP_RSP_INFO.
4971 * The FCP_RSP_LEN field shall be set to 0x04 or 0x08
4973 if ((fcprsp->rspStatus2 & RSP_LEN_VALID) &&
4974 ((rsp_len == 8) || (rsp_len == 4))) {
4975 switch (rsp_info_code) {
4976 case RSP_NO_FAILURE:
4977 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4978 "0715 Task Mgmt No Failure\n");
4981 case RSP_TM_NOT_SUPPORTED: /* TM rejected */
4982 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4983 "0716 Task Mgmt Target "
4986 case RSP_TM_NOT_COMPLETED: /* TM failed */
4987 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4988 "0717 Task Mgmt Target "
4991 case RSP_TM_INVALID_LU: /* TM to invalid LU! */
4992 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
4993 "0718 Task Mgmt to invalid "
5004 * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
5005 * @vport: The virtual port for which this call is being executed.
5006 * @rdata: Pointer to remote port local data
5007 * @tgt_id: Target ID of remote device.
5008 * @lun_id: Lun number for the TMF
5009 * @task_mgmt_cmd: type of TMF to send
5011 * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
5019 lpfc_send_taskmgmt(struct lpfc_vport *vport, struct scsi_cmnd *cmnd,
5020 unsigned int tgt_id, uint64_t lun_id,
5021 uint8_t task_mgmt_cmd)
5023 struct lpfc_hba *phba = vport->phba;
5024 struct lpfc_io_buf *lpfc_cmd;
5025 struct lpfc_iocbq *iocbq;
5026 struct lpfc_iocbq *iocbqrsp;
5027 struct lpfc_rport_data *rdata;
5028 struct lpfc_nodelist *pnode;
5032 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5033 if (!rdata || !rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
5035 pnode = rdata->pnode;
5037 lpfc_cmd = lpfc_get_scsi_buf(phba, pnode, NULL);
5038 if (lpfc_cmd == NULL)
5040 lpfc_cmd->timeout = phba->cfg_task_mgmt_tmo;
5041 lpfc_cmd->rdata = rdata;
5042 lpfc_cmd->pCmd = cmnd;
5043 lpfc_cmd->ndlp = pnode;
5045 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
5048 lpfc_release_scsi_buf(phba, lpfc_cmd);
5052 iocbq = &lpfc_cmd->cur_iocbq;
5053 iocbqrsp = lpfc_sli_get_iocbq(phba);
5054 if (iocbqrsp == NULL) {
5055 lpfc_release_scsi_buf(phba, lpfc_cmd);
5058 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
5060 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5061 "0702 Issue %s to TGT %d LUN %llu "
5062 "rpi x%x nlp_flag x%x Data: x%x x%x\n",
5063 lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
5064 pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
5067 status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
5068 iocbq, iocbqrsp, lpfc_cmd->timeout);
5069 if ((status != IOCB_SUCCESS) ||
5070 (iocbqrsp->iocb.ulpStatus != IOSTAT_SUCCESS)) {
5071 if (status != IOCB_SUCCESS ||
5072 iocbqrsp->iocb.ulpStatus != IOSTAT_FCP_RSP_ERROR)
5073 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5074 "0727 TMF %s to TGT %d LUN %llu "
5075 "failed (%d, %d) iocb_flag x%x\n",
5076 lpfc_taskmgmt_name(task_mgmt_cmd),
5078 iocbqrsp->iocb.ulpStatus,
5079 iocbqrsp->iocb.un.ulpWord[4],
5081 /* if ulpStatus != IOCB_SUCCESS, then status == IOCB_SUCCESS */
5082 if (status == IOCB_SUCCESS) {
5083 if (iocbqrsp->iocb.ulpStatus == IOSTAT_FCP_RSP_ERROR)
5084 /* Something in the FCP_RSP was invalid.
5085 * Check conditions */
5086 ret = lpfc_check_fcp_rsp(vport, lpfc_cmd);
5089 } else if (status == IOCB_TIMEDOUT) {
5090 ret = TIMEOUT_ERROR;
5097 lpfc_sli_release_iocbq(phba, iocbqrsp);
5099 if (ret != TIMEOUT_ERROR)
5100 lpfc_release_scsi_buf(phba, lpfc_cmd);
5106 * lpfc_chk_tgt_mapped -
5107 * @vport: The virtual port to check on
5108 * @cmnd: Pointer to scsi_cmnd data structure.
5110 * This routine delays until the scsi target (aka rport) for the
5111 * command exists (is present and logged in) or we declare it non-existent.
5118 lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
5120 struct lpfc_rport_data *rdata;
5121 struct lpfc_nodelist *pnode;
5122 unsigned long later;
5124 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5126 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
5127 "0797 Tgt Map rport failure: rdata x%px\n", rdata);
5130 pnode = rdata->pnode;
5132 * If target is not in a MAPPED state, delay until
5133 * target is rediscovered or devloss timeout expires.
5135 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5136 while (time_after(later, jiffies)) {
5137 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
5139 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
5141 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
5142 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5145 pnode = rdata->pnode;
5147 if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
5148 (pnode->nlp_state != NLP_STE_MAPPED_NODE))
5154 * lpfc_reset_flush_io_context -
5155 * @vport: The virtual port (scsi_host) for the flush context
5156 * @tgt_id: If aborting by Target contect - specifies the target id
5157 * @lun_id: If aborting by Lun context - specifies the lun id
5158 * @context: specifies the context level to flush at.
5160 * After a reset condition via TMF, we need to flush orphaned i/o
5161 * contexts from the adapter. This routine aborts any contexts
5162 * outstanding, then waits for their completions. The wait is
5163 * bounded by devloss_tmo though.
5170 lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
5171 uint64_t lun_id, lpfc_ctx_cmd context)
5173 struct lpfc_hba *phba = vport->phba;
5174 unsigned long later;
5177 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5179 lpfc_sli_abort_taskmgmt(vport,
5180 &phba->sli.sli3_ring[LPFC_FCP_RING],
5181 tgt_id, lun_id, context);
5182 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
5183 while (time_after(later, jiffies) && cnt) {
5184 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
5185 cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
5188 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5189 "0724 I/O flush failure for context %s : cnt x%x\n",
5190 ((context == LPFC_CTX_LUN) ? "LUN" :
5191 ((context == LPFC_CTX_TGT) ? "TGT" :
5192 ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
5200 * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
5201 * @cmnd: Pointer to scsi_cmnd data structure.
5203 * This routine does a device reset by sending a LUN_RESET task management
5211 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
5213 struct Scsi_Host *shost = cmnd->device->host;
5214 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5215 struct lpfc_rport_data *rdata;
5216 struct lpfc_nodelist *pnode;
5217 unsigned tgt_id = cmnd->device->id;
5218 uint64_t lun_id = cmnd->device->lun;
5219 struct lpfc_scsi_event_header scsi_event;
5222 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5223 if (!rdata || !rdata->pnode) {
5224 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5225 "0798 Device Reset rdata failure: rdata x%px\n",
5229 pnode = rdata->pnode;
5230 status = fc_block_scsi_eh(cmnd);
5231 if (status != 0 && status != SUCCESS)
5234 status = lpfc_chk_tgt_mapped(vport, cmnd);
5235 if (status == FAILED) {
5236 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5237 "0721 Device Reset rport failure: rdata x%px\n", rdata);
5241 scsi_event.event_type = FC_REG_SCSI_EVENT;
5242 scsi_event.subcategory = LPFC_EVENT_LUNRESET;
5243 scsi_event.lun = lun_id;
5244 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5245 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5247 fc_host_post_vendor_event(shost, fc_get_event_number(),
5248 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5250 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
5253 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5254 "0713 SCSI layer issued Device Reset (%d, %llu) "
5255 "return x%x\n", tgt_id, lun_id, status);
5258 * We have to clean up i/o as : they may be orphaned by the TMF;
5259 * or if the TMF failed, they may be in an indeterminate state.
5261 * We will report success if all the i/o aborts successfully.
5263 if (status == SUCCESS)
5264 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5271 * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
5272 * @cmnd: Pointer to scsi_cmnd data structure.
5274 * This routine does a target reset by sending a TARGET_RESET task management
5282 lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
5284 struct Scsi_Host *shost = cmnd->device->host;
5285 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5286 struct lpfc_rport_data *rdata;
5287 struct lpfc_nodelist *pnode;
5288 unsigned tgt_id = cmnd->device->id;
5289 uint64_t lun_id = cmnd->device->lun;
5290 struct lpfc_scsi_event_header scsi_event;
5293 rdata = lpfc_rport_data_from_scsi_device(cmnd->device);
5294 if (!rdata || !rdata->pnode) {
5295 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5296 "0799 Target Reset rdata failure: rdata x%px\n",
5300 pnode = rdata->pnode;
5301 status = fc_block_scsi_eh(cmnd);
5302 if (status != 0 && status != SUCCESS)
5305 status = lpfc_chk_tgt_mapped(vport, cmnd);
5306 if (status == FAILED) {
5307 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5308 "0722 Target Reset rport failure: rdata x%px\n", rdata);
5310 spin_lock_irq(shost->host_lock);
5311 pnode->nlp_flag &= ~NLP_NPR_ADISC;
5312 pnode->nlp_fcp_info &= ~NLP_FCP_2_DEVICE;
5313 spin_unlock_irq(shost->host_lock);
5315 lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5317 return FAST_IO_FAIL;
5320 scsi_event.event_type = FC_REG_SCSI_EVENT;
5321 scsi_event.subcategory = LPFC_EVENT_TGTRESET;
5323 memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
5324 memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
5326 fc_host_post_vendor_event(shost, fc_get_event_number(),
5327 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5329 status = lpfc_send_taskmgmt(vport, cmnd, tgt_id, lun_id,
5332 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5333 "0723 SCSI layer issued Target Reset (%d, %llu) "
5334 "return x%x\n", tgt_id, lun_id, status);
5337 * We have to clean up i/o as : they may be orphaned by the TMF;
5338 * or if the TMF failed, they may be in an indeterminate state.
5340 * We will report success if all the i/o aborts successfully.
5342 if (status == SUCCESS)
5343 status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
5349 * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
5350 * @cmnd: Pointer to scsi_cmnd data structure.
5352 * This routine does target reset to all targets on @cmnd->device->host.
5353 * This emulates Parallel SCSI Bus Reset Semantics.
5360 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
5362 struct Scsi_Host *shost = cmnd->device->host;
5363 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5364 struct lpfc_nodelist *ndlp = NULL;
5365 struct lpfc_scsi_event_header scsi_event;
5367 int ret = SUCCESS, status, i;
5369 scsi_event.event_type = FC_REG_SCSI_EVENT;
5370 scsi_event.subcategory = LPFC_EVENT_BUSRESET;
5372 memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
5373 memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
5375 fc_host_post_vendor_event(shost, fc_get_event_number(),
5376 sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
5378 status = fc_block_scsi_eh(cmnd);
5379 if (status != 0 && status != SUCCESS)
5383 * Since the driver manages a single bus device, reset all
5384 * targets known to the driver. Should any target reset
5385 * fail, this routine returns failure to the midlayer.
5387 for (i = 0; i < LPFC_MAX_TARGET; i++) {
5388 /* Search for mapped node by target ID */
5390 spin_lock_irq(shost->host_lock);
5391 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
5392 if (!NLP_CHK_NODE_ACT(ndlp))
5394 if (vport->phba->cfg_fcp2_no_tgt_reset &&
5395 (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE))
5397 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
5398 ndlp->nlp_sid == i &&
5400 ndlp->nlp_type & NLP_FCP_TARGET) {
5405 spin_unlock_irq(shost->host_lock);
5409 status = lpfc_send_taskmgmt(vport, cmnd,
5410 i, 0, FCP_TARGET_RESET);
5412 if (status != SUCCESS) {
5413 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5414 "0700 Bus Reset on target %d failed\n",
5420 * We have to clean up i/o as : they may be orphaned by the TMFs
5421 * above; or if any of the TMFs failed, they may be in an
5422 * indeterminate state.
5423 * We will report success if all the i/o aborts successfully.
5426 status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
5427 if (status != SUCCESS)
5430 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5431 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
5436 * lpfc_host_reset_handler - scsi_host_template eh_host_reset_handler entry pt
5437 * @cmnd: Pointer to scsi_cmnd data structure.
5439 * This routine does host reset to the adaptor port. It brings the HBA
5440 * offline, performs a board restart, and then brings the board back online.
5441 * The lpfc_offline calls lpfc_sli_hba_down which will abort and local
5442 * reject all outstanding SCSI commands to the host and error returned
5443 * back to SCSI mid-level. As this will be SCSI mid-level's last resort
5444 * of error handling, it will only return error if resetting of the adapter
5445 * is not successful; in all other cases, will return success.
5452 lpfc_host_reset_handler(struct scsi_cmnd *cmnd)
5454 struct Scsi_Host *shost = cmnd->device->host;
5455 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
5456 struct lpfc_hba *phba = vport->phba;
5457 int rc, ret = SUCCESS;
5459 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5460 "3172 SCSI layer issued Host Reset Data:\n");
5462 lpfc_offline_prep(phba, LPFC_MBX_WAIT);
5464 rc = lpfc_sli_brdrestart(phba);
5468 rc = lpfc_online(phba);
5472 lpfc_unblock_mgmt_io(phba);
5476 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5477 "3323 Failed host reset\n");
5478 lpfc_unblock_mgmt_io(phba);
5483 * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
5484 * @sdev: Pointer to scsi_device.
5486 * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
5487 * globally available list of scsi buffers. This routine also makes sure scsi
5488 * buffer is not allocated more than HBA limit conveyed to midlayer. This list
5489 * of scsi buffer exists for the lifetime of the driver.
5496 lpfc_slave_alloc(struct scsi_device *sdev)
5498 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5499 struct lpfc_hba *phba = vport->phba;
5500 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
5502 uint32_t num_to_alloc = 0;
5503 int num_allocated = 0;
5505 struct lpfc_device_data *device_data;
5506 unsigned long flags;
5507 struct lpfc_name target_wwpn;
5509 if (!rport || fc_remote_port_chkready(rport))
5512 if (phba->cfg_fof) {
5515 * Check to see if the device data structure for the lun
5516 * exists. If not, create one.
5519 u64_to_wwn(rport->port_name, target_wwpn.u.wwn);
5520 spin_lock_irqsave(&phba->devicelock, flags);
5521 device_data = __lpfc_get_device_data(phba,
5523 &vport->fc_portname,
5527 spin_unlock_irqrestore(&phba->devicelock, flags);
5528 device_data = lpfc_create_device_data(phba,
5529 &vport->fc_portname,
5532 phba->cfg_XLanePriority,
5536 spin_lock_irqsave(&phba->devicelock, flags);
5537 list_add_tail(&device_data->listentry, &phba->luns);
5539 device_data->rport_data = rport->dd_data;
5540 device_data->available = true;
5541 spin_unlock_irqrestore(&phba->devicelock, flags);
5542 sdev->hostdata = device_data;
5544 sdev->hostdata = rport->dd_data;
5546 sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
5548 /* For SLI4, all IO buffers are pre-allocated */
5549 if (phba->sli_rev == LPFC_SLI_REV4)
5552 /* This code path is now ONLY for SLI3 adapters */
5555 * Populate the cmds_per_lun count scsi_bufs into this host's globally
5556 * available list of scsi buffers. Don't allocate more than the
5557 * HBA limit conveyed to the midlayer via the host structure. The
5558 * formula accounts for the lun_queue_depth + error handlers + 1
5559 * extra. This list of scsi bufs exists for the lifetime of the driver.
5561 total = phba->total_scsi_bufs;
5562 num_to_alloc = vport->cfg_lun_queue_depth + 2;
5564 /* If allocated buffers are enough do nothing */
5565 if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
5568 /* Allow some exchanges to be available always to complete discovery */
5569 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5570 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5571 "0704 At limitation of %d preallocated "
5572 "command buffers\n", total);
5574 /* Allow some exchanges to be available always to complete discovery */
5575 } else if (total + num_to_alloc >
5576 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
5577 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
5578 "0705 Allocation request of %d "
5579 "command buffers will exceed max of %d. "
5580 "Reducing allocation request to %d.\n",
5581 num_to_alloc, phba->cfg_hba_queue_depth,
5582 (phba->cfg_hba_queue_depth - total));
5583 num_to_alloc = phba->cfg_hba_queue_depth - total;
5585 num_allocated = lpfc_new_scsi_buf_s3(vport, num_to_alloc);
5586 if (num_to_alloc != num_allocated) {
5587 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
5588 "0708 Allocation request of %d "
5589 "command buffers did not succeed. "
5590 "Allocated %d buffers.\n",
5591 num_to_alloc, num_allocated);
5593 if (num_allocated > 0)
5594 phba->total_scsi_bufs += num_allocated;
5599 * lpfc_slave_configure - scsi_host_template slave_configure entry point
5600 * @sdev: Pointer to scsi_device.
5602 * This routine configures following items
5603 * - Tag command queuing support for @sdev if supported.
5604 * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
5610 lpfc_slave_configure(struct scsi_device *sdev)
5612 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5613 struct lpfc_hba *phba = vport->phba;
5615 scsi_change_queue_depth(sdev, vport->cfg_lun_queue_depth);
5617 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
5618 lpfc_sli_handle_fast_ring_event(phba,
5619 &phba->sli.sli3_ring[LPFC_FCP_RING], HA_R0RE_REQ);
5620 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
5621 lpfc_poll_rearm_timer(phba);
5628 * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
5629 * @sdev: Pointer to scsi_device.
5631 * This routine sets @sdev hostatdata filed to null.
5634 lpfc_slave_destroy(struct scsi_device *sdev)
5636 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
5637 struct lpfc_hba *phba = vport->phba;
5638 unsigned long flags;
5639 struct lpfc_device_data *device_data = sdev->hostdata;
5641 atomic_dec(&phba->sdev_cnt);
5642 if ((phba->cfg_fof) && (device_data)) {
5643 spin_lock_irqsave(&phba->devicelock, flags);
5644 device_data->available = false;
5645 if (!device_data->oas_enabled)
5646 lpfc_delete_device_data(phba, device_data);
5647 spin_unlock_irqrestore(&phba->devicelock, flags);
5649 sdev->hostdata = NULL;
5654 * lpfc_create_device_data - creates and initializes device data structure for OAS
5655 * @pha: Pointer to host bus adapter structure.
5656 * @vport_wwpn: Pointer to vport's wwpn information
5657 * @target_wwpn: Pointer to target's wwpn information
5658 * @lun: Lun on target
5659 * @atomic_create: Flag to indicate if memory should be allocated using the
5660 * GFP_ATOMIC flag or not.
5662 * This routine creates a device data structure which will contain identifying
5663 * information for the device (host wwpn, target wwpn, lun), state of OAS,
5664 * whether or not the corresponding lun is available by the system,
5665 * and pointer to the rport data.
5669 * Pointer to lpfc_device_data - Success
5671 struct lpfc_device_data*
5672 lpfc_create_device_data(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5673 struct lpfc_name *target_wwpn, uint64_t lun,
5674 uint32_t pri, bool atomic_create)
5677 struct lpfc_device_data *lun_info;
5680 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5684 /* Attempt to create the device data to contain lun info */
5687 memory_flags = GFP_ATOMIC;
5689 memory_flags = GFP_KERNEL;
5690 lun_info = mempool_alloc(phba->device_data_mem_pool, memory_flags);
5693 INIT_LIST_HEAD(&lun_info->listentry);
5694 lun_info->rport_data = NULL;
5695 memcpy(&lun_info->device_id.vport_wwpn, vport_wwpn,
5696 sizeof(struct lpfc_name));
5697 memcpy(&lun_info->device_id.target_wwpn, target_wwpn,
5698 sizeof(struct lpfc_name));
5699 lun_info->device_id.lun = lun;
5700 lun_info->oas_enabled = false;
5701 lun_info->priority = pri;
5702 lun_info->available = false;
5707 * lpfc_delete_device_data - frees a device data structure for OAS
5708 * @pha: Pointer to host bus adapter structure.
5709 * @lun_info: Pointer to device data structure to free.
5711 * This routine frees the previously allocated device data structure passed.
5715 lpfc_delete_device_data(struct lpfc_hba *phba,
5716 struct lpfc_device_data *lun_info)
5719 if (unlikely(!phba) || !lun_info ||
5723 if (!list_empty(&lun_info->listentry))
5724 list_del(&lun_info->listentry);
5725 mempool_free(lun_info, phba->device_data_mem_pool);
5730 * __lpfc_get_device_data - returns the device data for the specified lun
5731 * @pha: Pointer to host bus adapter structure.
5732 * @list: Point to list to search.
5733 * @vport_wwpn: Pointer to vport's wwpn information
5734 * @target_wwpn: Pointer to target's wwpn information
5735 * @lun: Lun on target
5737 * This routine searches the list passed for the specified lun's device data.
5738 * This function does not hold locks, it is the responsibility of the caller
5739 * to ensure the proper lock is held before calling the function.
5743 * Pointer to lpfc_device_data - Success
5745 struct lpfc_device_data*
5746 __lpfc_get_device_data(struct lpfc_hba *phba, struct list_head *list,
5747 struct lpfc_name *vport_wwpn,
5748 struct lpfc_name *target_wwpn, uint64_t lun)
5751 struct lpfc_device_data *lun_info;
5753 if (unlikely(!phba) || !list || !vport_wwpn || !target_wwpn ||
5757 /* Check to see if the lun is already enabled for OAS. */
5759 list_for_each_entry(lun_info, list, listentry) {
5760 if ((memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5761 sizeof(struct lpfc_name)) == 0) &&
5762 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5763 sizeof(struct lpfc_name)) == 0) &&
5764 (lun_info->device_id.lun == lun))
5772 * lpfc_find_next_oas_lun - searches for the next oas lun
5773 * @pha: Pointer to host bus adapter structure.
5774 * @vport_wwpn: Pointer to vport's wwpn information
5775 * @target_wwpn: Pointer to target's wwpn information
5776 * @starting_lun: Pointer to the lun to start searching for
5777 * @found_vport_wwpn: Pointer to the found lun's vport wwpn information
5778 * @found_target_wwpn: Pointer to the found lun's target wwpn information
5779 * @found_lun: Pointer to the found lun.
5780 * @found_lun_status: Pointer to status of the found lun.
5782 * This routine searches the luns list for the specified lun
5783 * or the first lun for the vport/target. If the vport wwpn contains
5784 * a zero value then a specific vport is not specified. In this case
5785 * any vport which contains the lun will be considered a match. If the
5786 * target wwpn contains a zero value then a specific target is not specified.
5787 * In this case any target which contains the lun will be considered a
5788 * match. If the lun is found, the lun, vport wwpn, target wwpn and lun status
5789 * are returned. The function will also return the next lun if available.
5790 * If the next lun is not found, starting_lun parameter will be set to
5798 lpfc_find_next_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5799 struct lpfc_name *target_wwpn, uint64_t *starting_lun,
5800 struct lpfc_name *found_vport_wwpn,
5801 struct lpfc_name *found_target_wwpn,
5802 uint64_t *found_lun,
5803 uint32_t *found_lun_status,
5804 uint32_t *found_lun_pri)
5807 unsigned long flags;
5808 struct lpfc_device_data *lun_info;
5809 struct lpfc_device_id *device_id;
5813 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5814 !starting_lun || !found_vport_wwpn ||
5815 !found_target_wwpn || !found_lun || !found_lun_status ||
5816 (*starting_lun == NO_MORE_OAS_LUN) ||
5820 lun = *starting_lun;
5821 *found_lun = NO_MORE_OAS_LUN;
5822 *starting_lun = NO_MORE_OAS_LUN;
5824 /* Search for lun or the lun closet in value */
5826 spin_lock_irqsave(&phba->devicelock, flags);
5827 list_for_each_entry(lun_info, &phba->luns, listentry) {
5828 if (((wwn_to_u64(vport_wwpn->u.wwn) == 0) ||
5829 (memcmp(&lun_info->device_id.vport_wwpn, vport_wwpn,
5830 sizeof(struct lpfc_name)) == 0)) &&
5831 ((wwn_to_u64(target_wwpn->u.wwn) == 0) ||
5832 (memcmp(&lun_info->device_id.target_wwpn, target_wwpn,
5833 sizeof(struct lpfc_name)) == 0)) &&
5834 (lun_info->oas_enabled)) {
5835 device_id = &lun_info->device_id;
5837 ((lun == FIND_FIRST_OAS_LUN) ||
5838 (device_id->lun == lun))) {
5839 *found_lun = device_id->lun;
5840 memcpy(found_vport_wwpn,
5841 &device_id->vport_wwpn,
5842 sizeof(struct lpfc_name));
5843 memcpy(found_target_wwpn,
5844 &device_id->target_wwpn,
5845 sizeof(struct lpfc_name));
5846 if (lun_info->available)
5848 OAS_LUN_STATUS_EXISTS;
5850 *found_lun_status = 0;
5851 *found_lun_pri = lun_info->priority;
5852 if (phba->cfg_oas_flags & OAS_FIND_ANY_VPORT)
5853 memset(vport_wwpn, 0x0,
5854 sizeof(struct lpfc_name));
5855 if (phba->cfg_oas_flags & OAS_FIND_ANY_TARGET)
5856 memset(target_wwpn, 0x0,
5857 sizeof(struct lpfc_name));
5860 *starting_lun = device_id->lun;
5861 memcpy(vport_wwpn, &device_id->vport_wwpn,
5862 sizeof(struct lpfc_name));
5863 memcpy(target_wwpn, &device_id->target_wwpn,
5864 sizeof(struct lpfc_name));
5869 spin_unlock_irqrestore(&phba->devicelock, flags);
5874 * lpfc_enable_oas_lun - enables a lun for OAS operations
5875 * @pha: Pointer to host bus adapter structure.
5876 * @vport_wwpn: Pointer to vport's wwpn information
5877 * @target_wwpn: Pointer to target's wwpn information
5880 * This routine enables a lun for oas operations. The routines does so by
5881 * doing the following :
5883 * 1) Checks to see if the device data for the lun has been created.
5884 * 2) If found, sets the OAS enabled flag if not set and returns.
5885 * 3) Otherwise, creates a device data structure.
5886 * 4) If successfully created, indicates the device data is for an OAS lun,
5887 * indicates the lun is not available and add to the list of luns.
5894 lpfc_enable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5895 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
5898 struct lpfc_device_data *lun_info;
5899 unsigned long flags;
5901 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5905 spin_lock_irqsave(&phba->devicelock, flags);
5907 /* Check to see if the device data for the lun has been created */
5908 lun_info = __lpfc_get_device_data(phba, &phba->luns, vport_wwpn,
5911 if (!lun_info->oas_enabled)
5912 lun_info->oas_enabled = true;
5913 lun_info->priority = pri;
5914 spin_unlock_irqrestore(&phba->devicelock, flags);
5918 /* Create an lun info structure and add to list of luns */
5919 lun_info = lpfc_create_device_data(phba, vport_wwpn, target_wwpn, lun,
5922 lun_info->oas_enabled = true;
5923 lun_info->priority = pri;
5924 lun_info->available = false;
5925 list_add_tail(&lun_info->listentry, &phba->luns);
5926 spin_unlock_irqrestore(&phba->devicelock, flags);
5929 spin_unlock_irqrestore(&phba->devicelock, flags);
5934 * lpfc_disable_oas_lun - disables a lun for OAS operations
5935 * @pha: Pointer to host bus adapter structure.
5936 * @vport_wwpn: Pointer to vport's wwpn information
5937 * @target_wwpn: Pointer to target's wwpn information
5940 * This routine disables a lun for oas operations. The routines does so by
5941 * doing the following :
5943 * 1) Checks to see if the device data for the lun is created.
5944 * 2) If present, clears the flag indicating this lun is for OAS.
5945 * 3) If the lun is not available by the system, the device data is
5953 lpfc_disable_oas_lun(struct lpfc_hba *phba, struct lpfc_name *vport_wwpn,
5954 struct lpfc_name *target_wwpn, uint64_t lun, uint8_t pri)
5957 struct lpfc_device_data *lun_info;
5958 unsigned long flags;
5960 if (unlikely(!phba) || !vport_wwpn || !target_wwpn ||
5964 spin_lock_irqsave(&phba->devicelock, flags);
5966 /* Check to see if the lun is available. */
5967 lun_info = __lpfc_get_device_data(phba,
5968 &phba->luns, vport_wwpn,
5971 lun_info->oas_enabled = false;
5972 lun_info->priority = pri;
5973 if (!lun_info->available)
5974 lpfc_delete_device_data(phba, lun_info);
5975 spin_unlock_irqrestore(&phba->devicelock, flags);
5979 spin_unlock_irqrestore(&phba->devicelock, flags);
5984 lpfc_no_command(struct Scsi_Host *shost, struct scsi_cmnd *cmnd)
5986 return SCSI_MLQUEUE_HOST_BUSY;
5990 lpfc_no_handler(struct scsi_cmnd *cmnd)
5996 lpfc_no_slave(struct scsi_device *sdev)
6001 struct scsi_host_template lpfc_template_nvme = {
6002 .module = THIS_MODULE,
6003 .name = LPFC_DRIVER_NAME,
6004 .proc_name = LPFC_DRIVER_NAME,
6006 .queuecommand = lpfc_no_command,
6007 .eh_abort_handler = lpfc_no_handler,
6008 .eh_device_reset_handler = lpfc_no_handler,
6009 .eh_target_reset_handler = lpfc_no_handler,
6010 .eh_bus_reset_handler = lpfc_no_handler,
6011 .eh_host_reset_handler = lpfc_no_handler,
6012 .slave_alloc = lpfc_no_slave,
6013 .slave_configure = lpfc_no_slave,
6014 .scan_finished = lpfc_scan_finished,
6018 .shost_attrs = lpfc_hba_attrs,
6019 .max_sectors = 0xFFFF,
6020 .vendor_id = LPFC_NL_VENDOR_ID,
6021 .track_queue_depth = 0,
6024 struct scsi_host_template lpfc_template_no_hr = {
6025 .module = THIS_MODULE,
6026 .name = LPFC_DRIVER_NAME,
6027 .proc_name = LPFC_DRIVER_NAME,
6029 .queuecommand = lpfc_queuecommand,
6030 .eh_timed_out = fc_eh_timed_out,
6031 .eh_abort_handler = lpfc_abort_handler,
6032 .eh_device_reset_handler = lpfc_device_reset_handler,
6033 .eh_target_reset_handler = lpfc_target_reset_handler,
6034 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6035 .slave_alloc = lpfc_slave_alloc,
6036 .slave_configure = lpfc_slave_configure,
6037 .slave_destroy = lpfc_slave_destroy,
6038 .scan_finished = lpfc_scan_finished,
6040 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6041 .cmd_per_lun = LPFC_CMD_PER_LUN,
6042 .shost_attrs = lpfc_hba_attrs,
6043 .max_sectors = 0xFFFFFFFF,
6044 .vendor_id = LPFC_NL_VENDOR_ID,
6045 .change_queue_depth = scsi_change_queue_depth,
6046 .track_queue_depth = 1,
6049 struct scsi_host_template lpfc_template = {
6050 .module = THIS_MODULE,
6051 .name = LPFC_DRIVER_NAME,
6052 .proc_name = LPFC_DRIVER_NAME,
6054 .queuecommand = lpfc_queuecommand,
6055 .eh_timed_out = fc_eh_timed_out,
6056 .eh_abort_handler = lpfc_abort_handler,
6057 .eh_device_reset_handler = lpfc_device_reset_handler,
6058 .eh_target_reset_handler = lpfc_target_reset_handler,
6059 .eh_bus_reset_handler = lpfc_bus_reset_handler,
6060 .eh_host_reset_handler = lpfc_host_reset_handler,
6061 .slave_alloc = lpfc_slave_alloc,
6062 .slave_configure = lpfc_slave_configure,
6063 .slave_destroy = lpfc_slave_destroy,
6064 .scan_finished = lpfc_scan_finished,
6066 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6067 .cmd_per_lun = LPFC_CMD_PER_LUN,
6068 .shost_attrs = lpfc_hba_attrs,
6069 .max_sectors = 0xFFFF,
6070 .vendor_id = LPFC_NL_VENDOR_ID,
6071 .change_queue_depth = scsi_change_queue_depth,
6072 .track_queue_depth = 1,
6075 struct scsi_host_template lpfc_vport_template = {
6076 .module = THIS_MODULE,
6077 .name = LPFC_DRIVER_NAME,
6078 .proc_name = LPFC_DRIVER_NAME,
6080 .queuecommand = lpfc_queuecommand,
6081 .eh_timed_out = fc_eh_timed_out,
6082 .eh_abort_handler = lpfc_abort_handler,
6083 .eh_device_reset_handler = lpfc_device_reset_handler,
6084 .eh_target_reset_handler = lpfc_target_reset_handler,
6085 .slave_alloc = lpfc_slave_alloc,
6086 .slave_configure = lpfc_slave_configure,
6087 .slave_destroy = lpfc_slave_destroy,
6088 .scan_finished = lpfc_scan_finished,
6090 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
6091 .cmd_per_lun = LPFC_CMD_PER_LUN,
6092 .shost_attrs = lpfc_vport_attrs,
6093 .max_sectors = 0xFFFF,
6094 .change_queue_depth = scsi_change_queue_depth,
6095 .track_queue_depth = 1,