1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Enterprise Fibre Channel Host Bus Adapters. *
4 * Refer to the README file included with this package for *
5 * driver version and adapter support. *
6 * Copyright (C) 2004 Emulex Corporation. *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of the GNU General Public License *
11 * as published by the Free Software Foundation; either version 2 *
12 * of the License, or (at your option) any later version. *
14 * This program is distributed in the hope that it will be useful, *
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
17 * GNU General Public License for more details, a copy of which *
18 * can be found in the file COPYING included with this package. *
19 *******************************************************************/
22 * $Id: lpfc_scsiport.c 1.208 2004/12/03 11:27:34EST sf_support Exp $
24 #include <linux/version.h>
25 #include <linux/spinlock.h>
26 #include <linux/pci.h>
27 #include <linux/blkdev.h>
28 #include <scsi/scsi.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/scsi_host.h>
31 #include <scsi/scsi_cmnd.h>
32 #include <scsi/scsi_transport_fc.h>
37 #include "lpfc_disc.h"
38 #include "lpfc_scsi.h"
40 #include "lpfc_logmsg.h"
42 #include "lpfc_crtn.h"
44 /* This routine allocates a scsi buffer, which contains all the necessary
45 * information needed to initiate a SCSI I/O. The non-DMAable region of
46 * the buffer contains the area to build the IOCB. The DMAable region contains
47 * the memory for the FCP CMND, FCP RSP, and the inital BPL.
48 * In addition to allocating memeory, the FCP CMND and FCP RSP BDEs are setup
49 * in the BPL and the BPL BDE is setup in the IOCB.
51 struct lpfc_scsi_buf *
52 lpfc_get_scsi_buf(struct lpfc_hba * phba, int gfp_flags)
54 struct lpfc_scsi_buf *psb;
55 struct ulp_bde64 *bpl;
60 psb = mempool_alloc(phba->scsibuf_mem_pool, gfp_flags);
64 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
66 /* Get a SCSI DMA extention for an I/O */
68 * The DMA buffer for struct fcp_cmnd, struct fcp_rsp and BPL use
69 * lpfc_scsi_dma_ext_pool with size LPFC_SCSI_DMA_EXT_SIZE
72 * The size of struct fcp_cmnd = 32 bytes.
73 * The size of struct fcp_rsp = 160 bytes.
74 * The size of struct ulp_bde64 = 12 bytes and driver can only
75 * support LPFC_SCSI_INITIAL_BPL_SIZE (3) S/G segments for scsi data.
76 * One struct ulp_bde64 is used for each of the struct fcp_cmnd and
79 * Total usage for each I/O use 32 + 160 + (2 * 12) +
80 * (4 * 12) = 264 bytes.
83 INIT_LIST_HEAD(&psb->dma_ext.list);
85 psb->dma_ext.virt = pci_pool_alloc(phba->lpfc_scsi_dma_ext_pool,
86 GFP_ATOMIC, &psb->dma_ext.phys);
87 if (!psb->dma_ext.virt) {
88 mempool_free(psb, phba->scsibuf_mem_pool);
92 /* Save virtual ptrs to FCP Command, Response, and BPL */
93 ptr = (uint8_t *) psb->dma_ext.virt;
95 memset(ptr, 0, LPFC_SCSI_DMA_EXT_SIZE);
96 psb->fcp_cmnd = (struct fcp_cmnd *) ptr;
97 ptr += sizeof (struct fcp_cmnd);
98 psb->fcp_rsp = (struct fcp_rsp *) ptr;
99 ptr += (sizeof (struct fcp_rsp));
100 psb->fcp_bpl = (struct ulp_bde64 *) ptr;
101 psb->scsi_hba = phba;
103 /* Since this is for a FCP cmd, the first 2 BDEs in the BPL are always
104 * the FCP CMND and FCP RSP, so lets just set it up right here.
107 /* ptr points to physical address of FCP CMD */
108 pdma_phys = psb->dma_ext.phys;
109 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
110 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
111 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
112 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
113 bpl->tus.w = le32_to_cpu(bpl->tus.w);
117 pdma_phys += sizeof (struct fcp_cmnd);
118 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
119 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
120 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
121 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
122 bpl->tus.w = le32_to_cpu(bpl->tus.w);
125 /* Since the IOCB for the FCP I/O is built into the struct
126 * lpfc_scsi_buf, lets setup what we can right here.
128 pdma_phys += (sizeof (struct fcp_rsp));
129 cmd = &psb->cur_iocbq.iocb;
130 cmd->un.fcpi64.bdl.ulpIoTag32 = 0;
131 cmd->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
132 cmd->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
133 cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
134 cmd->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
135 cmd->ulpBdeCount = 1;
136 cmd->ulpClass = CLASS3;
142 lpfc_free_scsi_buf(struct lpfc_scsi_buf * psb)
144 struct lpfc_hba *phba = psb->scsi_hba;
145 struct lpfc_dmabuf *pbpl, *next_bpl;
148 * There are only two special cases to consider. (1) the scsi command
149 * requested scatter-gather usage or (2) the scsi command allocated
150 * a request buffer, but did not request use_sg. There is a third
151 * case, but it does not require resource deallocation.
154 if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
156 * Since the segment count is nonzero, the scsi command
157 * requested scatter-gather usage and the driver allocated
158 * addition memory buffers to chain BPLs. Traverse this list
159 * and release those resource before freeing the parent
162 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
163 psb->seg_cnt, psb->pCmd->sc_data_direction);
165 list_for_each_entry_safe(pbpl, next_bpl,
166 &psb->dma_ext.list, list) {
167 lpfc_mbuf_free(phba, pbpl->virt, pbpl->phys);
168 list_del(&pbpl->list);
172 if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
174 * Since either the segment count or the use_sg
175 * value is zero, the scsi command did not request
176 * scatter-gather usage and no additional buffers were
177 * required. Just unmap the dma single resource.
179 dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
180 psb->pCmd->request_bufflen,
181 psb->pCmd->sc_data_direction);
186 * Release the pci pool resource and clean up the scsi buffer. Neither
187 * are required now that the IO has completed.
189 pci_pool_free(phba->lpfc_scsi_dma_ext_pool, psb->dma_ext.virt,
191 mempool_free(psb, phba->scsibuf_mem_pool);
195 lpfc_os_prep_io(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
197 struct fcp_cmnd *fcp_cmnd;
198 struct ulp_bde64 *topbpl = NULL;
199 struct ulp_bde64 *bpl;
200 struct lpfc_dmabuf *bmp;
201 struct lpfc_dmabuf *head_bmp;
203 struct scsi_cmnd *cmnd;
204 struct scatterlist *sgel = NULL;
205 struct scatterlist *sgel_begin = NULL;
208 uint32_t num_bmps = 1, num_bde = 0, max_bde;
213 bpl = lpfc_cmd->fcp_bpl;
214 fcp_cmnd = lpfc_cmd->fcp_cmnd;
216 bpl += 2; /* Bump past FCP CMND and FCP RSP */
217 max_bde = LPFC_SCSI_INITIAL_BPL_SIZE - 1;
219 cmnd = lpfc_cmd->pCmd;
220 cmd = &lpfc_cmd->cur_iocbq.iocb;
222 /* These are needed if we chain BPLs */
223 head_bmp = &(lpfc_cmd->dma_ext);
224 use_sg = cmnd->use_sg;
227 * Fill in the FCP CMND
229 memcpy(&fcp_cmnd->fcpCdb[0], cmnd->cmnd, 16);
231 if (cmnd->device->tagged_supported) {
233 case HEAD_OF_QUEUE_TAG:
234 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
236 case ORDERED_QUEUE_TAG:
237 fcp_cmnd->fcpCntl1 = ORDERED_Q;
240 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
244 fcp_cmnd->fcpCntl1 = 0;
247 datadir = cmnd->sc_data_direction;
251 * Get a local pointer to the scatter-gather list. The
252 * scatter-gather list head must be preserved since
253 * sgel is incremented in the loop. The driver must store
254 * the segment count returned from pci_map_sg for calls to
255 * pci_unmap_sg later on because the use_sg field in the
256 * scsi_cmd is a count of physical memory pages, whereas the
257 * seg_cnt is a count of dma-mappings used by the MMIO to
258 * map the use_sg pages. They are not the same in most
259 * cases for those architectures that implement an MMIO.
261 sgel = (struct scatterlist *)cmnd->request_buffer;
263 lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
266 /* return error if we cannot map sg list */
267 if (lpfc_cmd->seg_cnt == 0)
270 /* scatter-gather list case */
271 for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
272 /* Check to see if current BPL is full of BDEs */
273 /* If this is last BDE and there is one left in */
274 /* current BPL, use it. */
275 if (num_bde == max_bde) {
276 bmp = kmalloc(sizeof (struct lpfc_dmabuf),
281 memset(bmp, 0, sizeof (struct lpfc_dmabuf));
283 lpfc_mbuf_alloc(phba, 0, &bmp->phys);
288 max_bde = ((1024 / sizeof(struct ulp_bde64))-3);
289 /* Fill in continuation entry to next bpl */
291 le32_to_cpu(putPaddrHigh(bmp->phys));
293 le32_to_cpu(putPaddrLow(bmp->phys));
294 bpl->tus.f.bdeFlags = BPL64_SIZE_WORD;
297 cmd->un.fcpi64.bdl.bdeSize += (num_bde *
298 sizeof (struct ulp_bde64));
300 topbpl->tus.f.bdeSize = (num_bde *
301 sizeof (struct ulp_bde64));
303 le32_to_cpu(topbpl->tus.w);
306 bpl = (struct ulp_bde64 *) bmp->virt;
307 list_add(&bmp->list, &head_bmp->list);
312 physaddr = sg_dma_address(sgel);
314 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
315 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
316 bpl->tus.f.bdeSize = sg_dma_len(sgel);
317 if (datadir == DMA_TO_DEVICE)
318 bpl->tus.f.bdeFlags = 0;
320 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
321 bpl->tus.w = le32_to_cpu(bpl->tus.w);
327 if (datadir == DMA_TO_DEVICE) {
328 cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
329 fcp_cmnd->fcpCntl3 = WRITE_DATA;
331 phba->fc4OutputRequests++;
333 cmd->ulpCommand = CMD_FCP_IREAD64_CR;
334 cmd->ulpPU = PARM_READ_CHECK;
335 cmd->un.fcpi.fcpi_parm = cmnd->request_bufflen;
336 fcp_cmnd->fcpCntl3 = READ_DATA;
338 phba->fc4InputRequests++;
340 } else if (cmnd->request_buffer && cmnd->request_bufflen) {
341 physaddr = dma_map_single(&phba->pcidev->dev,
342 cmnd->request_buffer,
343 cmnd->request_bufflen,
345 dma_error = dma_mapping_error(physaddr);
347 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
348 "%d:0718 Unable to dma_map_single "
349 "request_buffer: x%x\n",
350 phba->brd_no, dma_error);
354 /* no scatter-gather list case */
355 lpfc_cmd->nonsg_phys = physaddr;
356 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
357 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
358 bpl->tus.f.bdeSize = cmnd->request_bufflen;
359 if (datadir == DMA_TO_DEVICE) {
360 cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
361 fcp_cmnd->fcpCntl3 = WRITE_DATA;
362 bpl->tus.f.bdeFlags = 0;
364 phba->fc4OutputRequests++;
366 cmd->ulpCommand = CMD_FCP_IREAD64_CR;
367 cmd->ulpPU = PARM_READ_CHECK;
368 cmd->un.fcpi.fcpi_parm = cmnd->request_bufflen;
369 fcp_cmnd->fcpCntl3 = READ_DATA;
370 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
372 phba->fc4InputRequests++;
374 bpl->tus.w = le32_to_cpu(bpl->tus.w);
378 cmd->ulpCommand = CMD_FCP_ICMND64_CR;
379 cmd->un.fcpi.fcpi_parm = 0;
380 fcp_cmnd->fcpCntl3 = 0;
382 phba->fc4ControlRequests++;
389 cmd->un.fcpi64.bdl.bdeSize +=
390 (num_bde * sizeof (struct ulp_bde64));
392 topbpl->tus.f.bdeSize = (num_bde * sizeof (struct ulp_bde64));
393 topbpl->tus.w = le32_to_cpu(topbpl->tus.w);
395 cmd->ulpBdeCount = 1;
396 cmd->ulpLe = 1; /* Set the LE bit in the iocb */
398 /* set the Data Length field in the FCP CMND accordingly */
399 fcp_cmnd->fcpDl = be32_to_cpu(cmnd->request_bufflen);
405 * Allocation of a chained BPL failed, unmap the sg list and return
406 * error. This will ultimately cause lpfc_free_scsi_buf to be called
407 * which will handle the rest of the cleanup. Set seg_cnt back to zero
408 * to avoid double unmaps of the sg resources.
410 dma_unmap_sg(&phba->pcidev->dev, sgel_begin, lpfc_cmd->seg_cnt,
412 lpfc_cmd->seg_cnt = 0;
417 lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
419 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
420 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
421 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
422 struct lpfc_hba *phba = lpfc_cmd->scsi_hba;
423 uint32_t fcpi_parm = lpfc_cmd->cur_iocbq.iocb.un.fcpi.fcpi_parm;
424 uint32_t resp_info = fcprsp->rspStatus2;
425 uint32_t scsi_status = fcprsp->rspStatus3;
426 uint32_t host_status = DID_OK;
430 * If this is a task management command, there is no
431 * scsi packet associated with this lpfc_cmd. The driver
434 if (fcpcmd->fcpCntl2) {
439 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
440 "%d:0730 FCP command failed: RSP "
441 "Data: x%x x%x x%x x%x x%x x%x\n",
442 phba->brd_no, resp_info, scsi_status,
443 be32_to_cpu(fcprsp->rspResId),
444 be32_to_cpu(fcprsp->rspSnsLen),
445 be32_to_cpu(fcprsp->rspRspLen),
448 if (resp_info & RSP_LEN_VALID) {
449 rsplen = be32_to_cpu(fcprsp->rspRspLen);
450 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
451 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
452 host_status = DID_ERROR;
457 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
458 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
459 if (snslen > SCSI_SENSE_BUFFERSIZE)
460 snslen = SCSI_SENSE_BUFFERSIZE;
462 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
466 if (resp_info & RESID_UNDER) {
467 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
469 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
470 "%d:0716 FCP Read Underrun, expected %d, "
471 "residual %d Data: x%x x%x x%x\n", phba->brd_no,
472 be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
473 fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
476 * The cmnd->underflow is the minimum number of bytes that must
477 * be transfered for this command. Provided a sense condition is
478 * not present, make sure the actual amount transferred is at
479 * least the underflow value or fail.
481 if (!(resp_info & SNS_LEN_VALID) &&
482 (scsi_status == SAM_STAT_GOOD) &&
483 (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
484 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
485 "%d:0717 FCP command x%x residual "
486 "underrun converted to error "
487 "Data: x%x x%x x%x\n", phba->brd_no,
488 cmnd->cmnd[0], cmnd->request_bufflen,
489 cmnd->resid, cmnd->underflow);
491 host_status = DID_ERROR;
493 } else if (resp_info & RESID_OVER) {
494 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
495 "%d:0720 FCP command x%x residual "
496 "overrun error. Data: x%x x%x \n",
497 phba->brd_no, cmnd->cmnd[0],
498 cmnd->request_bufflen, cmnd->resid);
499 host_status = DID_ERROR;
502 * Check SLI validation that all the transfer was actually done
503 * (fcpi_parm should be zero). Apply check only to reads.
505 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
506 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
507 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
508 "%d:0734 FCP Read Check Error Data: "
509 "x%x x%x x%x x%x\n", phba->brd_no,
510 be32_to_cpu(fcpcmd->fcpDl),
511 be32_to_cpu(fcprsp->rspResId),
512 fcpi_parm, cmnd->cmnd[0]);
513 host_status = DID_ERROR;
514 cmnd->resid = cmnd->request_bufflen;
518 cmnd->result = ScsiResult(host_status, scsi_status);
522 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
523 struct lpfc_iocbq *pIocbOut)
525 struct lpfc_scsi_buf *lpfc_cmd =
526 (struct lpfc_scsi_buf *) pIocbIn->context1;
527 struct lpfc_target *target = lpfc_cmd->target;
528 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
530 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
531 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
533 pci_dma_sync_single_for_cpu(phba->pcidev, lpfc_cmd->dma_ext.phys,
534 LPFC_BPL_SIZE, PCI_DMA_FROMDEVICE);
538 if (lpfc_cmd->status) {
541 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
542 (lpfc_cmd->result & IOERR_DRVR_MASK))
543 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
544 else if (lpfc_cmd->status >= IOSTAT_CNT)
545 lpfc_cmd->status = IOSTAT_DEFAULT;
547 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
548 "%d:0729 FCP cmd x%x failed <%d/%d> status: "
549 "x%x result: x%x Data: x%x x%x\n",
550 phba->brd_no, cmd->cmnd[0], cmd->device->id,
551 cmd->device->lun, lpfc_cmd->status,
552 lpfc_cmd->result, pIocbOut->iocb.ulpContext,
553 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
555 switch (lpfc_cmd->status) {
556 case IOSTAT_FCP_RSP_ERROR:
557 /* Call FCP RSP handler to determine result */
558 lpfc_handle_fcp_err(lpfc_cmd);
560 case IOSTAT_NPORT_BSY:
561 case IOSTAT_FABRIC_BSY:
562 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
565 cmd->result = ScsiResult(DID_ERROR, 0);
570 if(target->pnode->nlp_state != NLP_STE_MAPPED_NODE)
571 cmd->result = ScsiResult(DID_BUS_BUSY,
575 cmd->result = ScsiResult(DID_NO_CONNECT, 0);
578 cmd->result = ScsiResult(DID_OK, 0);
581 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
582 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
584 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
585 "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
586 "SNS x%x x%x Data: x%x x%x\n",
587 phba->brd_no, cmd->device->id,
588 cmd->device->lun, cmd, cmd->result,
589 *lp, *(lp + 3), cmd->retries, cmd->resid);
592 lpfc_free_scsi_buf(lpfc_cmd);
593 cmd->host_scribble = NULL;
598 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
599 struct lpfc_scsi_buf *lpfc_cmd,
600 uint8_t task_mgmt_cmd)
603 struct lpfc_sli *psli;
604 struct lpfc_iocbq *piocbq;
606 struct fcp_cmnd *fcp_cmnd;
607 struct scsi_device *scsi_dev = lpfc_cmd->pCmd->device;
608 struct lpfc_target *target = scsi_dev->hostdata;
609 struct lpfc_nodelist *ndlp = target->pnode;
611 if ((ndlp == 0) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
615 /* allocate an iocb command */
617 piocbq = &(lpfc_cmd->cur_iocbq);
618 piocb = &piocbq->iocb;
621 fcp_cmnd = lpfc_cmd->fcp_cmnd;
622 putLunHigh(fcp_cmnd->fcpLunMsl, 0);
623 putLunLow(fcp_cmnd->fcpLunLsl, lpfc_cmd->pCmd->device->lun)
624 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
627 lpfc_sli_next_iotag(phba, &phba->sli.ring[psli->fcp_ring]);
628 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
630 piocb->ulpContext = ndlp->nlp_rpi;
631 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
632 piocb->ulpFCP2Rcvy = 1;
634 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
636 /* ulpTimeout is only one byte */
637 if (lpfc_cmd->timeout > 0xff) {
639 * Do not timeout the command at the firmware level.
640 * The driver will provide the timeout mechanism.
642 piocb->ulpTimeout = 0;
644 piocb->ulpTimeout = lpfc_cmd->timeout;
647 lpfc_cmd->target = target;
649 switch (task_mgmt_cmd) {
651 /* Issue LUN Reset to TGT <num> LUN <num> */
652 lpfc_printf_log(phba,
655 "%d:0703 Issue LUN Reset to TGT %d LUN %d "
658 scsi_dev->id, scsi_dev->lun,
659 ndlp->nlp_rpi, ndlp->nlp_flag);
662 case FCP_ABORT_TASK_SET:
663 /* Issue Abort Task Set to TGT <num> LUN <num> */
664 lpfc_printf_log(phba,
667 "%d:0701 Issue Abort Task Set to TGT %d LUN %d "
670 scsi_dev->id, scsi_dev->lun,
671 ndlp->nlp_rpi, ndlp->nlp_flag);
674 case FCP_TARGET_RESET:
675 /* Issue Target Reset to TGT <num> */
676 lpfc_printf_log(phba,
679 "%d:0702 Issue Target Reset to TGT %d "
682 scsi_dev->id, ndlp->nlp_rpi,
691 lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba)
693 struct lpfc_iocbq *piocbq, *piocbqrsp;
694 struct lpfc_sli *psli;
697 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_TARGET_RESET);
701 lpfc_cmd->scsi_hba = phba;
703 piocbq = &lpfc_cmd->cur_iocbq;
704 piocbqrsp = mempool_alloc(phba->iocb_mem_pool,
709 /* First flush all outstanding commands on the txq for the target */
710 lpfc_sli_abort_iocb_tgt(phba, &phba->sli.ring[phba->sli.fcp_ring],
711 lpfc_cmd->pCmd->device->id, LPFC_ABORT_TXQ);
713 memset(piocbqrsp, 0, sizeof (struct lpfc_iocbq));
715 piocbq->iocb_flag |= LPFC_IO_POLL;
717 ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
718 &phba->sli.ring[psli->fcp_ring],
719 piocbq, SLI_IOCB_HIGH_PRIORITY,
722 if (ret != IOCB_SUCCESS) {
723 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
725 lpfc_cmd->result = piocbqrsp->iocb.un.ulpWord[4];
726 lpfc_cmd->status = piocbqrsp->iocb.ulpStatus;
727 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
728 (lpfc_cmd->result & IOERR_DRVR_MASK))
729 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
732 /* At this point in time, target reset completion, all outstanding
733 * txcmplq I/Os should have been aborted by the target.
734 * Unfortunately, all targets do not abide by this so we need
735 * to help it out a bit.
737 lpfc_sli_abort_iocb_tgt(phba, &phba->sli.ring[phba->sli.fcp_ring],
738 lpfc_cmd->pCmd->device->id, LPFC_ABORT_ALLQ);
740 /* Done with piocbqrsp, return to free list. */
741 mempool_free(piocbqrsp, phba->iocb_mem_pool);
747 lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
749 struct Scsi_Host *shost = cmnd->device->host;
750 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
751 struct lpfc_target *target;
752 int ret = FAILED, i, err_count = 0;
754 struct scsi_cmnd *lnx_cmd;
755 struct scsi_device *scsi_dev;
756 struct lpfc_scsi_buf * lpfc_cmd;
758 lpfc_cmd = lpfc_get_scsi_buf(phba, GFP_ATOMIC);
762 lnx_cmd = kmalloc(sizeof(struct scsi_cmnd), GFP_ATOMIC);
764 goto out_free_scsi_buf;
766 scsi_dev = kmalloc(sizeof(struct scsi_device), GFP_ATOMIC);
768 goto out_free_lnx_cmd;
770 /* The lpfc_cmd storage is reused. Set all loop invariants. */
771 lpfc_cmd->timeout = cmnd->timeout;
773 lpfc_cmd->timeout = LPFC_DRVR_TIMEOUT;
775 lpfc_cmd->pCmd = lnx_cmd;
776 lpfc_cmd->scsi_hba = phba;
777 lpfc_cmd->pCmd->device = scsi_dev;
778 lpfc_cmd->pCmd->device->channel = 0;
779 lpfc_cmd->pCmd->device->lun = 0;
782 * Since the driver manages a single bus device, reset all
783 * targets known to the driver. Should any target reset
784 * fail, this routine returns failure to the midlayer.
786 for (i = 0; i < MAX_FCP_TARGET; i++) {
787 target = phba->device_queue_hash[i];
791 lpfc_cmd->pCmd->device->id = i;
792 lpfc_cmd->pCmd->device->hostdata = target;
793 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
794 if (ret != IOCB_SUCCESS) {
795 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
796 "%d:0713 Bus Reset on target %d failed\n",
803 while((cnt = lpfc_sli_sum_iocb_host(phba,
804 &phba->sli.ring[phba->sli.fcp_ring]))) {
805 spin_unlock_irq(phba->host->host_lock);
806 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,6)
811 spin_lock_irq(phba->host->host_lock);
813 /* 50ms * 100 = 5 sec
814 * wait upto 5 seconds for all I/Os for this HBA to cmpl
821 /* flush all outstanding commands on the host */
822 i = lpfc_sli_abort_iocb_host(phba,
823 &phba->sli.ring[phba->sli.fcp_ring],
826 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
827 "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
828 phba->brd_no, cnt, i);
836 lpfc_free_scsi_buf(lpfc_cmd);
837 lpfc_printf_log(phba,
840 "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
848 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
850 struct lpfc_hba *phba =
851 (struct lpfc_hba *) cmnd->device->host->hostdata[0];
852 struct lpfc_sli *psli = &phba->sli;
853 struct lpfc_target *targetp = cmnd->device->hostdata;
854 struct lpfc_nodelist *ndlp;
855 struct lpfc_iocbq *piocbq;
856 struct lpfc_scsi_buf *lpfc_cmd;
864 * The target pointer is guaranteed not to be NULL because the driver
865 * only clears the device->hostdata field in lpfc_slave_destroy. This
866 * approach guarantees no further IO calls on this target.
868 ndlp = targetp->pnode;
870 if(phba->fc_flag & FC_UNLOADING) {
871 /* Driver is in process of unloading */
872 cmnd->result = ScsiResult(DID_BAD_TARGET, 0);
875 cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
877 goto out_fail_command;
880 nlp_state = ndlp->nlp_state;
883 * A Fibre Channel is present and functioning only when the node state
884 * is MAPPED. Any other state is a failure.
886 if (nlp_state != NLP_STE_MAPPED_NODE) {
887 if ((nlp_state == NLP_STE_UNMAPPED_NODE) ||
888 (nlp_state == NLP_STE_UNUSED_NODE)) {
889 cmnd->result = ScsiResult(DID_NO_CONNECT, 0);
890 goto out_fail_command;
893 * The device is most likely recovered and the driver
894 * needs a bit more time to finish. Ask the midlayer
900 lpfc_cmd = lpfc_get_scsi_buf(phba, GFP_ATOMIC);
905 * Store the midlayer's command structure for the completion phase
906 * and complete the command initialization.
908 cmnd->scsi_done = done;
909 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
911 lpfc_cmd->target = targetp;
912 lpfc_cmd->timeout = 0;
913 lpfc_cmd->pCmd = cmnd;
914 putLunHigh(lpfc_cmd->fcp_cmnd->fcpLunMsl, lpfc_cmd->pCmd->device->lun);
915 putLunLow(lpfc_cmd->fcp_cmnd->fcpLunLsl, lpfc_cmd->pCmd->device->lun);
917 err = lpfc_os_prep_io(phba, lpfc_cmd);
919 goto out_host_busy_free_buf;
921 piocbq = &(lpfc_cmd->cur_iocbq);
922 piocb = &piocbq->iocb;
923 piocb->ulpTimeout = lpfc_cmd->timeout;
924 piocbq->context1 = lpfc_cmd;
925 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
927 piocbq->iocb.ulpContext = ndlp->nlp_rpi;
928 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
929 piocbq->iocb.ulpFCP2Rcvy = 1;
932 piocbq->iocb.ulpClass = (ndlp->nlp_fcp_info & 0x0f);
934 pci_dma_sync_single_for_device(phba->pcidev, lpfc_cmd->dma_ext.phys,
935 LPFC_SCSI_DMA_EXT_SIZE, PCI_DMA_TODEVICE);
937 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring], piocbq,
940 goto out_host_busy_free_buf;
943 out_host_busy_free_buf:
944 lpfc_free_scsi_buf(lpfc_cmd);
946 targetp->iodonecnt++;
948 return SCSI_MLQUEUE_HOST_BUSY;
951 targetp->iodonecnt++;
958 lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
960 struct Scsi_Host *shost = cmnd->device->host;
961 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
962 struct lpfc_sli *psli = &phba->sli;
963 struct lpfc_scsi_buf *lpfc_cmd;
964 struct lpfc_iocbq *piocbq, *piocbqrsp = NULL;
968 lpfc_cmd = lpfc_get_scsi_buf(phba, GFP_ATOMIC);
972 lpfc_cmd->pCmd = cmnd;
973 lpfc_cmd->timeout = 60; /* set command timeout to 60 seconds */
974 lpfc_cmd->scsi_hba = phba;
976 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
978 goto out_free_scsi_buf;
980 piocbq = &lpfc_cmd->cur_iocbq;
982 /* get a buffer for this IOCB command response */
983 piocbqrsp = mempool_alloc(phba->iocb_mem_pool, GFP_ATOMIC);
985 goto out_free_scsi_buf;
987 /* First flush all outstanding commands on the txq for the lun */
988 lpfc_sli_abort_iocb_lun(phba,
989 &phba->sli.ring[phba->sli.fcp_ring],
991 cmnd->device->lun, LPFC_ABORT_TXQ);
993 memset(piocbqrsp, 0, sizeof (struct lpfc_iocbq));
995 piocbq->iocb_flag |= LPFC_IO_POLL;
996 piocbq->iocb_cmpl = lpfc_sli_wake_iocb_high_priority;
998 ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
999 &phba->sli.ring[psli->fcp_ring],
1002 if (ret == IOCB_SUCCESS)
1005 lpfc_cmd->result = piocbqrsp->iocb.un.ulpWord[4];
1006 lpfc_cmd->status = piocbqrsp->iocb.ulpStatus;
1007 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT)
1008 if (lpfc_cmd->result & IOERR_DRVR_MASK)
1009 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
1011 /* At this point in time, lun reset completion, all outstanding
1012 * txcmplq I/Os should have been aborted by the target.
1013 * Unfortunately, all targets do not abide by this so we need
1014 * to help it out a bit.
1016 lpfc_sli_abort_iocb_lun(phba,
1017 &phba->sli.ring[phba->sli.fcp_ring],
1019 cmnd->device->lun, LPFC_ABORT_ALLQ);
1022 while((cnt = lpfc_sli_sum_iocb_lun(phba,
1023 &phba->sli.ring[phba->sli.fcp_ring],
1025 cmnd->device->lun))) {
1026 spin_unlock_irq(phba->host->host_lock);
1027 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,6)
1032 spin_lock_irq(phba->host->host_lock);
1034 /* 50ms * 100 = 5 sec
1035 * wait upto 5 seconds for all I/Os for this lun to cmpl
1037 if(++loopcnt >= 100)
1042 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1043 "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1047 mempool_free(piocbqrsp, phba->iocb_mem_pool);
1050 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1051 "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1052 "Data: x%x x%x x%x\n",
1053 phba->brd_no, lpfc_cmd->pCmd->device->id,
1054 lpfc_cmd->pCmd->device->lun, ret, lpfc_cmd->status,
1056 lpfc_free_scsi_buf(lpfc_cmd);
1063 lpfc_abort_handler(struct scsi_cmnd *cmnd)
1065 struct lpfc_hba *phba =
1066 (struct lpfc_hba *)cmnd->device->host->hostdata[0];
1067 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
1068 struct lpfc_iocbq *iocb, *next_iocb, *abtsiocbp;
1069 struct lpfc_scsi_buf *lpfc_cmd;
1072 unsigned int id, lun;
1073 int ret = IOCB_ERROR;
1075 /* Returns SUCESS if command aborted, else FAILED */
1077 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1081 /* save these now since lpfc_cmd can be freed */
1082 id = lpfc_cmd->pCmd->device->id;
1083 lun = lpfc_cmd->pCmd->device->lun;
1084 snum = lpfc_cmd->pCmd->serial_number;
1086 /* Search the txq first. */
1087 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
1089 if (iocb->context1 != lpfc_cmd)
1092 list_del_init(&iocb->list);
1094 if (!iocb->iocb_cmpl) {
1095 mempool_free(iocb, phba->iocb_mem_pool);
1098 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
1099 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
1100 (iocb->iocb_cmpl)(phba, iocb, iocb);
1106 abtsiocbp = mempool_alloc(phba->iocb_mem_pool, GFP_ATOMIC);
1109 memset(abtsiocbp, 0, sizeof (struct lpfc_iocbq));
1112 * The scsi command was not in the txq. Check the txcmplq and if it is
1113 * found, send an abort to the FW.
1115 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
1116 if (iocb->context1 != lpfc_cmd)
1119 iocb->iocb_cmpl = NULL;
1121 icmd = &abtsiocbp->iocb;
1122 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1123 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1124 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
1127 icmd->ulpClass = cmd->ulpClass;
1128 icmd->ulpIoTag = lpfc_sli_next_iotag(phba, pring);
1129 if (phba->hba_state >= LPFC_LINK_UP)
1130 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1132 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
1134 /* set up an iotag */
1135 icmd->ulpIoTag = lpfc_sli_next_iotag(phba, pring);
1137 if (lpfc_sli_issue_iocb(phba, pring, abtsiocbp, 0) ==
1139 mempool_free(abtsiocbp, phba->iocb_mem_pool);
1144 * The rsp ring completion will remove IOCB from txcmplq when
1145 * abort is read by HBA.
1152 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1153 "%d:0749 SCSI layer issued abort device "
1154 "Data: x%x x%x x%x x%lx\n",
1155 phba->brd_no, ret, id, lun, snum);
1157 return (ret == IOCB_SUCCESS ? SUCCESS : FAILED);
1160 #if defined(FC_TRANS_VER1) || defined(FC_TRANS_265_BLKPATCH)
1162 lpfc_target_unblock(struct lpfc_hba *phba, struct lpfc_target *targetp)
1164 #if defined(FC_TRANS_VER1)
1166 * This code to be removed once block/unblock and the new
1167 * dicovery state machine are fully debugged.
1169 if (!targetp || !targetp->starget) {
1173 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY | LOG_FCP,
1174 "%d:0262 Cannot unblock scsi target\n", phba->brd_no);
1179 /* Unblock IO to target scsi id <sid> to NPort <nlp_DID> */
1180 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY | LOG_FCP,
1181 "%d:0258 Unblocking IO to Target scsi id x%x "
1182 "NPort pointer x%p\n",
1183 phba->brd_no, targetp->scsi_id, targetp->pnode);
1185 spin_unlock_irq(phba->host->host_lock);
1187 #if defined(FC_TRANS_VER1)
1188 fc_target_unblock(targetp->starget);
1189 #elif defined(FC_TRANS_265_BLKPATCH)
1190 fc_target_unblock(phba->host, targetp->scsi_id,
1191 &targetp->dev_loss_timer);
1193 spin_lock_irq(phba->host->host_lock);
1198 lpfc_target_block(struct lpfc_hba *phba, struct lpfc_target *targetp)
1200 #if defined(FC_TRANS_VER1)
1202 * This code to be removed once block/unblock and the new
1203 * dicovery state machine are fully debugged.
1205 if (!targetp || !targetp->starget) {
1209 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY | LOG_FCP,
1210 "%d:0263 Cannot block scsi target."
1211 " target ptr x%p\n",
1212 phba->brd_no, targetp);
1216 /* Block all IO to target scsi id <sid> to NPort <nlp_DID> */
1217 lpfc_printf_log(phba, KERN_INFO, LOG_DISCOVERY | LOG_FCP,
1218 "%d:0259 Blocking IO to Target scsi id x%x"
1219 " NPort pointer x%p\n",
1220 phba->brd_no, targetp->scsi_id, targetp->pnode);
1222 spin_unlock_irq(phba->host->host_lock);
1223 #if defined(FC_TRANS_VER1)
1224 fc_target_block(targetp->starget);
1225 #elif defined(FC_TRANS_265_BLKPATCH)
1226 fc_target_block(phba->host, targetp->scsi_id, &targetp->dev_loss_timer,
1227 phba->cfg_nodev_tmo);
1229 spin_lock_irq(phba->host->host_lock);
1234 #if defined(FC_TRANS_VER1) || defined(FC_TRANS_265_BLKPATCH)
1236 lpfc_target_remove(struct lpfc_hba *phba, struct lpfc_target *targetp)
1238 struct scsi_device *sdev;
1239 struct Scsi_Host *shost = phba->host;
1241 /* This is only called if scsi target (targetp->starget) is valid */
1242 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY | LOG_FCP,
1243 "%d:0260 Remove Target scsi id x%x\n",
1244 phba->brd_no, targetp->scsi_id);
1246 /* If this target is blocked, we must unblock it first */
1247 if (targetp->blocked)
1248 lpfc_target_unblock(phba, targetp);
1250 /* Remove all associated devices for this target */
1251 if (phba->cfg_scsi_hotplug) {
1253 list_for_each_entry(sdev, &shost->__devices, siblings) {
1254 if (sdev->channel == 0
1255 && sdev->id == targetp->scsi_id) {
1256 spin_unlock_irq(shost->host_lock);
1257 scsi_device_get(sdev);
1258 scsi_remove_device(sdev);
1259 scsi_device_put(sdev);
1260 spin_lock_irq(shost->host_lock);
1270 lpfc_target_add(struct lpfc_hba *phba, struct lpfc_target *targetp)
1272 struct Scsi_Host *shost;
1274 if(!phba->cfg_scsi_hotplug)
1277 /* This is only called if scsi target (targetp->starget) is valid */
1279 lpfc_printf_log(phba, KERN_ERR, LOG_DISCOVERY | LOG_FCP,
1280 "%d:0261 Adding Target scsi id x%x\n",
1281 phba->brd_no, targetp->scsi_id);
1285 #ifdef USE_SCAN_TARGET
1286 lpfc_discq_post_event(phba, targetp, NULL, LPFC_EVT_SCAN);
1289 * The driver discovered a new target. Call the midlayer and get this
1290 * target's luns added into the device list.
1291 * Since we are going to scan the entire host, kick off a timer to
1292 * do this so we can possibly consolidate multiple target scans into
1293 * one scsi host scan.
1295 mod_timer(&phba->fc_scantmo, jiffies + HZ);
1296 phba->fc_flag |= FC_SCSI_SCAN_TMO;