1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2005 Emulex. All rights reserved. *
5 * EMULEX and SLI are trademarks of Emulex. *
7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
9 * This program is free software; you can redistribute it and/or *
10 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
20 *******************************************************************/
22 #include <linux/pci.h>
23 #include <linux/interrupt.h>
25 #include <scsi/scsi.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_host.h>
28 #include <scsi/scsi_tcq.h>
29 #include <scsi/scsi_transport_fc.h>
31 #include "lpfc_version.h"
34 #include "lpfc_disc.h"
35 #include "lpfc_scsi.h"
37 #include "lpfc_logmsg.h"
38 #include "lpfc_crtn.h"
40 #define LPFC_RESET_WAIT 2
41 #define LPFC_ABORT_WAIT 2
45 * This routine allocates a scsi buffer, which contains all the necessary
46 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
47 * contains information to build the IOCB. The DMAable region contains
48 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
49 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
50 * and the BPL BDE is setup in the IOCB.
52 static struct lpfc_scsi_buf *
53 lpfc_get_scsi_buf(struct lpfc_hba * phba)
55 struct lpfc_scsi_buf *psb;
56 struct ulp_bde64 *bpl;
60 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
63 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
67 * Get memory from the pci pool to map the virt space to pci bus space
68 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
69 * struct fcp_rsp and the number of bde's necessary to support the
72 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
79 /* Initialize virtual ptrs to dma_buf region. */
80 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
82 psb->fcp_cmnd = psb->data;
83 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
84 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
85 sizeof(struct fcp_rsp);
87 /* Initialize local short-hand pointers. */
89 pdma_phys = psb->dma_handle;
92 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
93 * list bdes. Initialize the first two and leave the rest for
96 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
97 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
98 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
99 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
100 bpl->tus.w = le32_to_cpu(bpl->tus.w);
103 /* Setup the physical region for the FCP RSP */
104 pdma_phys += sizeof (struct fcp_cmnd);
105 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
106 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
107 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
108 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
109 bpl->tus.w = le32_to_cpu(bpl->tus.w);
112 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
113 * initialize it with all known data now.
115 pdma_phys += (sizeof (struct fcp_rsp));
116 iocb = &psb->cur_iocbq.iocb;
117 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
118 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
119 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
120 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
121 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
122 iocb->ulpBdeCount = 1;
123 iocb->ulpClass = CLASS3;
129 lpfc_free_scsi_buf(struct lpfc_scsi_buf * psb)
131 struct lpfc_hba *phba = psb->scsi_hba;
134 * There are only two special cases to consider. (1) the scsi command
135 * requested scatter-gather usage or (2) the scsi command allocated
136 * a request buffer, but did not request use_sg. There is a third
137 * case, but it does not require resource deallocation.
139 if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
140 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
141 psb->seg_cnt, psb->pCmd->sc_data_direction);
143 if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
144 dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
145 psb->pCmd->request_bufflen,
146 psb->pCmd->sc_data_direction);
150 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
154 lpfc_scsi_prep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
156 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
157 struct scatterlist *sgel = NULL;
158 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
159 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
160 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
162 uint32_t i, num_bde = 0;
163 int datadir = scsi_cmnd->sc_data_direction;
167 * There are three possibilities here - use scatter-gather segment, use
168 * the single mapping, or neither. Start the lpfc command prep by
169 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
173 if (scsi_cmnd->use_sg) {
175 * The driver stores the segment count returned from pci_map_sg
176 * because this a count of dma-mappings used to map the use_sg
177 * pages. They are not guaranteed to be the same for those
178 * architectures that implement an IOMMU.
180 sgel = (struct scatterlist *)scsi_cmnd->request_buffer;
181 lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
182 scsi_cmnd->use_sg, datadir);
183 if (lpfc_cmd->seg_cnt == 0)
186 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
187 printk(KERN_ERR "%s: Too many sg segments from "
188 "dma_map_sg. Config %d, seg_cnt %d",
189 __FUNCTION__, phba->cfg_sg_seg_cnt,
191 dma_unmap_sg(&phba->pcidev->dev, sgel,
192 lpfc_cmd->seg_cnt, datadir);
197 * The driver established a maximum scatter-gather segment count
198 * during probe that limits the number of sg elements in any
199 * single scsi command. Just run through the seg_cnt and format
202 for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
203 physaddr = sg_dma_address(sgel);
204 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
205 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
206 bpl->tus.f.bdeSize = sg_dma_len(sgel);
207 if (datadir == DMA_TO_DEVICE)
208 bpl->tus.f.bdeFlags = 0;
210 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
211 bpl->tus.w = le32_to_cpu(bpl->tus.w);
216 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
217 physaddr = dma_map_single(&phba->pcidev->dev,
218 scsi_cmnd->request_buffer,
219 scsi_cmnd->request_bufflen,
221 dma_error = dma_mapping_error(physaddr);
223 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
224 "%d:0718 Unable to dma_map_single "
225 "request_buffer: x%x\n",
226 phba->brd_no, dma_error);
230 lpfc_cmd->nonsg_phys = physaddr;
231 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
232 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
233 bpl->tus.f.bdeSize = scsi_cmnd->request_bufflen;
234 if (datadir == DMA_TO_DEVICE)
235 bpl->tus.f.bdeFlags = 0;
237 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
238 bpl->tus.w = le32_to_cpu(bpl->tus.w);
244 * Finish initializing those IOCB fields that are dependent on the
245 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
246 * reinitialized since all iocb memory resources are used many times
247 * for transmit, receive, and continuation bpl's.
249 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
250 iocb_cmd->un.fcpi64.bdl.bdeSize +=
251 (num_bde * sizeof (struct ulp_bde64));
252 iocb_cmd->ulpBdeCount = 1;
254 fcp_cmnd->fcpDl = be32_to_cpu(scsi_cmnd->request_bufflen);
259 lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
261 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
262 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
263 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
264 struct lpfc_hba *phba = lpfc_cmd->scsi_hba;
265 uint32_t fcpi_parm = lpfc_cmd->cur_iocbq.iocb.un.fcpi.fcpi_parm;
266 uint32_t resp_info = fcprsp->rspStatus2;
267 uint32_t scsi_status = fcprsp->rspStatus3;
268 uint32_t host_status = DID_OK;
272 * If this is a task management command, there is no
273 * scsi packet associated with this lpfc_cmd. The driver
276 if (fcpcmd->fcpCntl2) {
281 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
282 "%d:0730 FCP command failed: RSP "
283 "Data: x%x x%x x%x x%x x%x x%x\n",
284 phba->brd_no, resp_info, scsi_status,
285 be32_to_cpu(fcprsp->rspResId),
286 be32_to_cpu(fcprsp->rspSnsLen),
287 be32_to_cpu(fcprsp->rspRspLen),
290 if (resp_info & RSP_LEN_VALID) {
291 rsplen = be32_to_cpu(fcprsp->rspRspLen);
292 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
293 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
294 host_status = DID_ERROR;
299 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
300 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
301 if (snslen > SCSI_SENSE_BUFFERSIZE)
302 snslen = SCSI_SENSE_BUFFERSIZE;
304 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
308 if (resp_info & RESID_UNDER) {
309 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
311 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
312 "%d:0716 FCP Read Underrun, expected %d, "
313 "residual %d Data: x%x x%x x%x\n", phba->brd_no,
314 be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
315 fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
318 * The cmnd->underflow is the minimum number of bytes that must
319 * be transfered for this command. Provided a sense condition
320 * is not present, make sure the actual amount transferred is at
321 * least the underflow value or fail.
323 if (!(resp_info & SNS_LEN_VALID) &&
324 (scsi_status == SAM_STAT_GOOD) &&
325 (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
326 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
327 "%d:0717 FCP command x%x residual "
328 "underrun converted to error "
329 "Data: x%x x%x x%x\n", phba->brd_no,
330 cmnd->cmnd[0], cmnd->request_bufflen,
331 cmnd->resid, cmnd->underflow);
333 host_status = DID_ERROR;
335 } else if (resp_info & RESID_OVER) {
336 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
337 "%d:0720 FCP command x%x residual "
338 "overrun error. Data: x%x x%x \n",
339 phba->brd_no, cmnd->cmnd[0],
340 cmnd->request_bufflen, cmnd->resid);
341 host_status = DID_ERROR;
344 * Check SLI validation that all the transfer was actually done
345 * (fcpi_parm should be zero). Apply check only to reads.
347 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
348 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
349 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
350 "%d:0734 FCP Read Check Error Data: "
351 "x%x x%x x%x x%x\n", phba->brd_no,
352 be32_to_cpu(fcpcmd->fcpDl),
353 be32_to_cpu(fcprsp->rspResId),
354 fcpi_parm, cmnd->cmnd[0]);
355 host_status = DID_ERROR;
356 cmnd->resid = cmnd->request_bufflen;
360 cmnd->result = ScsiResult(host_status, scsi_status);
364 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
365 struct lpfc_iocbq *pIocbOut)
367 struct lpfc_scsi_buf *lpfc_cmd =
368 (struct lpfc_scsi_buf *) pIocbIn->context1;
369 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
370 struct lpfc_nodelist *pnode = rdata->pnode;
371 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
374 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
375 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
377 if (lpfc_cmd->status) {
378 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
379 (lpfc_cmd->result & IOERR_DRVR_MASK))
380 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
381 else if (lpfc_cmd->status >= IOSTAT_CNT)
382 lpfc_cmd->status = IOSTAT_DEFAULT;
384 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
385 "%d:0729 FCP cmd x%x failed <%d/%d> status: "
386 "x%x result: x%x Data: x%x x%x\n",
387 phba->brd_no, cmd->cmnd[0], cmd->device->id,
388 cmd->device->lun, lpfc_cmd->status,
389 lpfc_cmd->result, pIocbOut->iocb.ulpContext,
390 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
392 switch (lpfc_cmd->status) {
393 case IOSTAT_FCP_RSP_ERROR:
394 /* Call FCP RSP handler to determine result */
395 lpfc_handle_fcp_err(lpfc_cmd);
397 case IOSTAT_NPORT_BSY:
398 case IOSTAT_FABRIC_BSY:
399 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
402 cmd->result = ScsiResult(DID_ERROR, 0);
407 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
408 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
410 cmd->result = ScsiResult(DID_OK, 0);
413 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
414 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
416 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
417 "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
418 "SNS x%x x%x Data: x%x x%x\n",
419 phba->brd_no, cmd->device->id,
420 cmd->device->lun, cmd, cmd->result,
421 *lp, *(lp + 3), cmd->retries, cmd->resid);
424 spin_lock_irqsave(phba->host->host_lock, iflag);
425 lpfc_free_scsi_buf(lpfc_cmd);
426 cmd->host_scribble = NULL;
427 spin_unlock_irqrestore(phba->host->host_lock, iflag);
433 lpfc_scsi_prep_cmnd(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd,
434 struct lpfc_nodelist *pnode)
436 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
437 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
438 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
439 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
440 int datadir = scsi_cmnd->sc_data_direction;
442 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
443 /* clear task management bits */
444 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
446 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
447 &lpfc_cmd->fcp_cmnd->fcp_lun);
449 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
451 if (scsi_cmnd->device->tagged_supported) {
452 switch (scsi_cmnd->tag) {
453 case HEAD_OF_QUEUE_TAG:
454 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
456 case ORDERED_QUEUE_TAG:
457 fcp_cmnd->fcpCntl1 = ORDERED_Q;
460 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
464 fcp_cmnd->fcpCntl1 = 0;
467 * There are three possibilities here - use scatter-gather segment, use
468 * the single mapping, or neither. Start the lpfc command prep by
469 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
472 if (scsi_cmnd->use_sg) {
473 if (datadir == DMA_TO_DEVICE) {
474 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
475 iocb_cmd->un.fcpi.fcpi_parm = 0;
477 fcp_cmnd->fcpCntl3 = WRITE_DATA;
478 phba->fc4OutputRequests++;
480 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
481 iocb_cmd->ulpPU = PARM_READ_CHECK;
482 iocb_cmd->un.fcpi.fcpi_parm =
483 scsi_cmnd->request_bufflen;
484 fcp_cmnd->fcpCntl3 = READ_DATA;
485 phba->fc4InputRequests++;
487 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
488 if (datadir == DMA_TO_DEVICE) {
489 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
490 iocb_cmd->un.fcpi.fcpi_parm = 0;
492 fcp_cmnd->fcpCntl3 = WRITE_DATA;
493 phba->fc4OutputRequests++;
495 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
496 iocb_cmd->ulpPU = PARM_READ_CHECK;
497 iocb_cmd->un.fcpi.fcpi_parm =
498 scsi_cmnd->request_bufflen;
499 fcp_cmnd->fcpCntl3 = READ_DATA;
500 phba->fc4InputRequests++;
503 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
504 iocb_cmd->un.fcpi.fcpi_parm = 0;
506 fcp_cmnd->fcpCntl3 = 0;
507 phba->fc4ControlRequests++;
511 * Finish initializing those IOCB fields that are independent
512 * of the scsi_cmnd request_buffer
514 piocbq->iocb.ulpContext = pnode->nlp_rpi;
515 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
516 piocbq->iocb.ulpFCP2Rcvy = 1;
518 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
519 piocbq->context1 = lpfc_cmd;
520 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
521 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
525 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
526 struct lpfc_scsi_buf *lpfc_cmd,
527 uint8_t task_mgmt_cmd)
529 struct lpfc_sli *psli;
530 struct lpfc_iocbq *piocbq;
532 struct fcp_cmnd *fcp_cmnd;
533 struct scsi_device *scsi_dev = lpfc_cmd->pCmd->device;
534 struct lpfc_rport_data *rdata = scsi_dev->hostdata;
535 struct lpfc_nodelist *ndlp = rdata->pnode;
537 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
542 piocbq = &(lpfc_cmd->cur_iocbq);
543 piocb = &piocbq->iocb;
545 fcp_cmnd = lpfc_cmd->fcp_cmnd;
546 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
547 &lpfc_cmd->fcp_cmnd->fcp_lun);
548 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
550 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
552 piocb->ulpContext = ndlp->nlp_rpi;
553 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
554 piocb->ulpFCP2Rcvy = 1;
556 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
558 /* ulpTimeout is only one byte */
559 if (lpfc_cmd->timeout > 0xff) {
561 * Do not timeout the command at the firmware level.
562 * The driver will provide the timeout mechanism.
564 piocb->ulpTimeout = 0;
566 piocb->ulpTimeout = lpfc_cmd->timeout;
569 lpfc_cmd->rdata = rdata;
571 switch (task_mgmt_cmd) {
573 /* Issue LUN Reset to TGT <num> LUN <num> */
574 lpfc_printf_log(phba,
577 "%d:0703 Issue LUN Reset to TGT %d LUN %d "
580 scsi_dev->id, scsi_dev->lun,
581 ndlp->nlp_rpi, ndlp->nlp_flag);
584 case FCP_ABORT_TASK_SET:
585 /* Issue Abort Task Set to TGT <num> LUN <num> */
586 lpfc_printf_log(phba,
589 "%d:0701 Issue Abort Task Set to TGT %d LUN %d "
592 scsi_dev->id, scsi_dev->lun,
593 ndlp->nlp_rpi, ndlp->nlp_flag);
596 case FCP_TARGET_RESET:
597 /* Issue Target Reset to TGT <num> */
598 lpfc_printf_log(phba,
601 "%d:0702 Issue Target Reset to TGT %d "
604 scsi_dev->id, ndlp->nlp_rpi,
613 lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba)
615 struct lpfc_iocbq *iocbq;
616 struct lpfc_iocbq *iocbqrsp = NULL;
617 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
620 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_TARGET_RESET);
624 lpfc_cmd->scsi_hba = phba;
625 iocbq = &lpfc_cmd->cur_iocbq;
626 list_remove_head(lpfc_iocb_list, iocbqrsp, struct lpfc_iocbq, list);
629 memset(iocbqrsp, 0, sizeof (struct lpfc_iocbq));
631 iocbq->iocb_flag |= LPFC_IO_POLL;
632 ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
633 &phba->sli.ring[phba->sli.fcp_ring],
634 iocbq, SLI_IOCB_HIGH_PRIORITY,
637 if (ret != IOCB_SUCCESS) {
638 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
642 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
643 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
644 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
645 (lpfc_cmd->result & IOERR_DRVR_MASK))
646 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
650 * All outstanding txcmplq I/Os should have been aborted by the target.
651 * Unfortunately, some targets do not abide by this forcing the driver
654 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
655 lpfc_cmd->pCmd->device->id,
656 lpfc_cmd->pCmd->device->lun, 0, LPFC_CTX_TGT);
658 /* Return response IOCB to free list. */
659 list_add_tail(&iocbqrsp->list, lpfc_iocb_list);
664 lpfc_scsi_cmd_iocb_cleanup (struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
665 struct lpfc_iocbq *pIocbOut)
668 struct lpfc_scsi_buf *lpfc_cmd =
669 (struct lpfc_scsi_buf *) pIocbIn->context1;
671 spin_lock_irqsave(phba->host->host_lock, iflag);
672 lpfc_free_scsi_buf(lpfc_cmd);
673 spin_unlock_irqrestore(phba->host->host_lock, iflag);
677 lpfc_scsi_cmd_iocb_cmpl_aborted(struct lpfc_hba *phba,
678 struct lpfc_iocbq *pIocbIn,
679 struct lpfc_iocbq *pIocbOut)
681 struct scsi_cmnd *ml_cmd =
682 ((struct lpfc_scsi_buf *) pIocbIn->context1)->pCmd;
684 lpfc_scsi_cmd_iocb_cleanup (phba, pIocbIn, pIocbOut);
685 ml_cmd->host_scribble = NULL;
689 lpfc_info(struct Scsi_Host *host)
691 struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata[0];
693 static char lpfcinfobuf[384];
695 memset(lpfcinfobuf,0,384);
696 if (phba && phba->pcidev){
697 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
698 len = strlen(lpfcinfobuf);
699 snprintf(lpfcinfobuf + len,
701 " on PCI bus %02x device %02x irq %d",
702 phba->pcidev->bus->number,
705 len = strlen(lpfcinfobuf);
707 snprintf(lpfcinfobuf + len,
717 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
719 struct lpfc_hba *phba =
720 (struct lpfc_hba *) cmnd->device->host->hostdata[0];
721 struct lpfc_sli *psli = &phba->sli;
722 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
723 struct lpfc_nodelist *ndlp = rdata->pnode;
724 struct lpfc_scsi_buf *lpfc_cmd = NULL;
725 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
726 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
729 err = fc_remote_port_chkready(rport);
732 goto out_fail_command;
736 * Catch race where our node has transitioned, but the
737 * transport is still transitioning.
740 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
741 goto out_fail_command;
744 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
745 if (lpfc_cmd == NULL) {
746 printk(KERN_WARNING "%s: No buffer available - list empty, "
747 "total count %d\n", __FUNCTION__, phba->total_scsi_bufs);
752 * Store the midlayer's command structure for the completion phase
753 * and complete the command initialization.
755 lpfc_cmd->pCmd = cmnd;
756 lpfc_cmd->rdata = rdata;
757 lpfc_cmd->timeout = 0;
758 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
759 cmnd->scsi_done = done;
761 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
763 goto out_host_busy_free_buf;
765 lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
767 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
768 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
770 goto out_host_busy_free_buf;
773 out_host_busy_free_buf:
774 lpfc_free_scsi_buf(lpfc_cmd);
775 cmnd->host_scribble = NULL;
777 return SCSI_MLQUEUE_HOST_BUSY;
785 __lpfc_abort_handler(struct scsi_cmnd *cmnd)
787 struct lpfc_hba *phba =
788 (struct lpfc_hba *)cmnd->device->host->hostdata[0];
789 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
790 struct lpfc_iocbq *iocb, *next_iocb;
791 struct lpfc_iocbq *abtsiocb = NULL;
792 struct lpfc_scsi_buf *lpfc_cmd;
793 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
796 unsigned int id, lun;
797 unsigned int loop_count = 0;
798 int ret = IOCB_SUCCESS;
801 * If the host_scribble data area is NULL, then the driver has already
802 * completed this command, but the midlayer did not see the completion
803 * before the eh fired. Just return SUCCESS.
805 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
809 /* save these now since lpfc_cmd can be freed */
810 id = lpfc_cmd->pCmd->device->id;
811 lun = lpfc_cmd->pCmd->device->lun;
812 snum = lpfc_cmd->pCmd->serial_number;
814 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
816 if (iocb->context1 != lpfc_cmd)
819 list_del_init(&iocb->list);
821 if (!iocb->iocb_cmpl) {
822 list_add_tail(&iocb->list, lpfc_iocb_list);
825 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
826 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
827 lpfc_scsi_cmd_iocb_cmpl_aborted(phba, iocb, iocb);
833 list_remove_head(lpfc_iocb_list, abtsiocb, struct lpfc_iocbq, list);
834 if (abtsiocb == NULL)
837 memset(abtsiocb, 0, sizeof (struct lpfc_iocbq));
840 * The scsi command was not in the txq. Check the txcmplq and if it is
841 * found, send an abort to the FW.
843 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
844 if (iocb->context1 != lpfc_cmd)
847 iocb->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl_aborted;
849 icmd = &abtsiocb->iocb;
850 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
851 icmd->un.acxri.abortContextTag = cmd->ulpContext;
852 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
855 icmd->ulpClass = cmd->ulpClass;
856 if (phba->hba_state >= LPFC_LINK_UP)
857 icmd->ulpCommand = CMD_ABORT_XRI_CN;
859 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
861 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
862 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) ==
864 list_add_tail(&abtsiocb->list, lpfc_iocb_list);
869 /* Wait for abort to complete */
870 while (cmnd->host_scribble)
872 spin_unlock_irq(phba->host->host_lock);
873 set_current_state(TASK_UNINTERRUPTIBLE);
874 schedule_timeout(LPFC_ABORT_WAIT*HZ);
875 spin_lock_irq(phba->host->host_lock);
877 > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
881 if(cmnd->host_scribble) {
882 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
883 "%d:0748 abort handler timed "
884 "out waiting for abort to "
886 "x%x x%x x%x x%lx\n",
887 phba->brd_no, ret, id, lun, snum);
888 cmnd->host_scribble = NULL;
889 iocb->iocb_cmpl = lpfc_scsi_cmd_iocb_cleanup;
897 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
898 "%d:0749 SCSI layer issued abort device "
899 "Data: x%x x%x x%x x%lx\n",
900 phba->brd_no, ret, id, lun, snum);
902 return ret == IOCB_SUCCESS ? SUCCESS : FAILED;
906 lpfc_abort_handler(struct scsi_cmnd *cmnd)
909 spin_lock_irq(cmnd->device->host->host_lock);
910 rc = __lpfc_abort_handler(cmnd);
911 spin_unlock_irq(cmnd->device->host->host_lock);
916 __lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
918 struct Scsi_Host *shost = cmnd->device->host;
919 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
920 struct lpfc_sli *psli = &phba->sli;
921 struct lpfc_scsi_buf *lpfc_cmd = NULL;
922 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
923 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
924 struct lpfc_iocbq *iocbq, *iocbqrsp = NULL;
925 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
926 struct lpfc_nodelist *pnode = rdata->pnode;
931 * If target is not in a MAPPED state, delay the reset until
932 * target is rediscovered or nodev timeout expires.
938 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
939 spin_unlock_irq(phba->host->host_lock);
940 set_current_state(TASK_UNINTERRUPTIBLE);
941 schedule_timeout( HZ/2);
942 spin_lock_irq(phba->host->host_lock);
944 if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
948 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
949 if (lpfc_cmd == NULL)
952 lpfc_cmd->pCmd = cmnd;
953 lpfc_cmd->timeout = 60;
954 lpfc_cmd->scsi_hba = phba;
956 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
958 goto out_free_scsi_buf;
960 iocbq = &lpfc_cmd->cur_iocbq;
962 /* get a buffer for this IOCB command response */
963 list_remove_head(lpfc_iocb_list, iocbqrsp, struct lpfc_iocbq, list);
964 if (iocbqrsp == NULL)
965 goto out_free_scsi_buf;
967 memset(iocbqrsp, 0, sizeof (struct lpfc_iocbq));
969 iocbq->iocb_flag |= LPFC_IO_POLL;
970 iocbq->iocb_cmpl = lpfc_sli_wake_iocb_high_priority;
972 ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
973 &phba->sli.ring[psli->fcp_ring],
974 iocbq, 0, iocbqrsp, 60);
975 if (ret == IOCB_SUCCESS)
978 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
979 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
980 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT)
981 if (lpfc_cmd->result & IOERR_DRVR_MASK)
982 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
985 * All outstanding txcmplq I/Os should have been aborted by the target.
986 * Unfortunately, some targets do not abide by this forcing the driver
989 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
990 cmnd->device->id, cmnd->device->lun, 0,
994 while((cnt = lpfc_sli_sum_iocb(phba,
995 &phba->sli.ring[phba->sli.fcp_ring],
996 cmnd->device->id, cmnd->device->lun,
998 spin_unlock_irq(phba->host->host_lock);
999 set_current_state(TASK_UNINTERRUPTIBLE);
1000 schedule_timeout(LPFC_RESET_WAIT*HZ);
1001 spin_lock_irq(phba->host->host_lock);
1004 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1009 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1010 "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1014 list_add_tail(&iocbqrsp->list, lpfc_iocb_list);
1017 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1018 "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1019 "Data: x%x x%x x%x\n",
1020 phba->brd_no, lpfc_cmd->pCmd->device->id,
1021 lpfc_cmd->pCmd->device->lun, ret, lpfc_cmd->status,
1023 lpfc_free_scsi_buf(lpfc_cmd);
1029 lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
1032 spin_lock_irq(cmnd->device->host->host_lock);
1033 rc = __lpfc_reset_lun_handler(cmnd);
1034 spin_unlock_irq(cmnd->device->host->host_lock);
1039 * Note: midlayer calls this function with the host_lock held
1042 __lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
1044 struct Scsi_Host *shost = cmnd->device->host;
1045 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
1046 struct lpfc_nodelist *ndlp = NULL;
1048 int ret = FAILED, i, err_count = 0;
1050 unsigned int midlayer_id = 0;
1051 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1052 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
1054 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1055 if (lpfc_cmd == NULL)
1058 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1059 lpfc_cmd->timeout = 60;
1060 lpfc_cmd->pCmd = cmnd;
1061 lpfc_cmd->scsi_hba = phba;
1064 * Since the driver manages a single bus device, reset all
1065 * targets known to the driver. Should any target reset
1066 * fail, this routine returns failure to the midlayer.
1068 midlayer_id = cmnd->device->id;
1069 for (i = 0; i < MAX_FCP_TARGET; i++) {
1070 /* Search the mapped list for this target ID */
1072 list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1073 if ((i == ndlp->nlp_sid) && ndlp->rport) {
1081 lpfc_cmd->pCmd->device->id = i;
1082 lpfc_cmd->pCmd->device->hostdata = ndlp->rport->dd_data;
1083 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
1084 if (ret != SUCCESS) {
1085 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1086 "%d:0713 Bus Reset on target %d failed\n",
1092 cmnd->device->id = midlayer_id;
1094 while((cnt = lpfc_sli_sum_iocb(phba,
1095 &phba->sli.ring[phba->sli.fcp_ring],
1096 0, 0, LPFC_CTX_HOST))) {
1097 spin_unlock_irq(phba->host->host_lock);
1098 set_current_state(TASK_UNINTERRUPTIBLE);
1099 schedule_timeout(LPFC_RESET_WAIT*HZ);
1100 spin_lock_irq(phba->host->host_lock);
1103 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1108 /* flush all outstanding commands on the host */
1109 i = lpfc_sli_abort_iocb(phba,
1110 &phba->sli.ring[phba->sli.fcp_ring], 0, 0, 0,
1113 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1114 "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1115 phba->brd_no, cnt, i);
1121 lpfc_free_scsi_buf(lpfc_cmd);
1122 lpfc_printf_log(phba,
1125 "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1132 lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
1135 spin_lock_irq(cmnd->device->host->host_lock);
1136 rc = __lpfc_reset_bus_handler(cmnd);
1137 spin_unlock_irq(cmnd->device->host->host_lock);
1142 lpfc_slave_alloc(struct scsi_device *sdev)
1144 struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata[0];
1145 struct lpfc_scsi_buf *scsi_buf = NULL;
1146 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1147 uint32_t total = 0, i;
1148 uint32_t num_to_alloc = 0;
1149 unsigned long flags;
1151 if (!rport || fc_remote_port_chkready(rport))
1154 sdev->hostdata = rport->dd_data;
1157 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1158 * available list of scsi buffers. Don't allocate more than the
1159 * HBA limit conveyed to the midlayer via the host structure. Note
1160 * that this list of scsi bufs exists for the lifetime of the driver.
1162 total = phba->total_scsi_bufs;
1163 num_to_alloc = LPFC_CMD_PER_LUN;
1164 if (total >= phba->cfg_hba_queue_depth) {
1165 printk(KERN_WARNING "%s, At config limitation of "
1166 "%d allocated scsi_bufs\n", __FUNCTION__, total);
1168 } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
1169 num_to_alloc = phba->cfg_hba_queue_depth - total;
1172 for (i = 0; i < num_to_alloc; i++) {
1173 scsi_buf = lpfc_get_scsi_buf(phba);
1175 printk(KERN_ERR "%s, failed to allocate "
1176 "scsi_buf\n", __FUNCTION__);
1180 spin_lock_irqsave(phba->host->host_lock, flags);
1181 phba->total_scsi_bufs++;
1182 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
1183 spin_unlock_irqrestore(phba->host->host_lock, flags);
1189 lpfc_slave_configure(struct scsi_device *sdev)
1191 struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata[0];
1192 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1194 if (sdev->tagged_supported)
1195 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1197 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1200 * Initialize the fc transport attributes for the target
1201 * containing this scsi device. Also note that the driver's
1202 * target pointer is stored in the starget_data for the
1203 * driver's sysfs entry point functions.
1205 rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
1211 lpfc_slave_destroy(struct scsi_device *sdev)
1213 sdev->hostdata = NULL;
1217 struct scsi_host_template lpfc_template = {
1218 .module = THIS_MODULE,
1219 .name = LPFC_DRIVER_NAME,
1221 .queuecommand = lpfc_queuecommand,
1222 .eh_abort_handler = lpfc_abort_handler,
1223 .eh_device_reset_handler= lpfc_reset_lun_handler,
1224 .eh_bus_reset_handler = lpfc_reset_bus_handler,
1225 .slave_alloc = lpfc_slave_alloc,
1226 .slave_configure = lpfc_slave_configure,
1227 .slave_destroy = lpfc_slave_destroy,
1229 .sg_tablesize = LPFC_SG_SEG_CNT,
1230 .cmd_per_lun = LPFC_CMD_PER_LUN,
1231 .use_clustering = ENABLE_CLUSTERING,
1232 .shost_attrs = lpfc_host_attrs,
1233 .max_sectors = 0xFFFF,