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;
61 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
64 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
68 * Get memory from the pci pool to map the virt space to pci bus space
69 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
70 * struct fcp_rsp and the number of bde's necessary to support the
73 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
80 /* Initialize virtual ptrs to dma_buf region. */
81 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
83 /* Allocate iotag for psb->cur_iocbq. */
84 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
86 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
87 psb->data, psb->dma_handle);
92 psb->fcp_cmnd = psb->data;
93 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
94 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
95 sizeof(struct fcp_rsp);
97 /* Initialize local short-hand pointers. */
99 pdma_phys = psb->dma_handle;
102 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
103 * list bdes. Initialize the first two and leave the rest for
106 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
107 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
108 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
109 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
110 bpl->tus.w = le32_to_cpu(bpl->tus.w);
113 /* Setup the physical region for the FCP RSP */
114 pdma_phys += sizeof (struct fcp_cmnd);
115 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
116 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
117 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
118 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
119 bpl->tus.w = le32_to_cpu(bpl->tus.w);
122 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
123 * initialize it with all known data now.
125 pdma_phys += (sizeof (struct fcp_rsp));
126 iocb = &psb->cur_iocbq.iocb;
127 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
128 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
129 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
130 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
131 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
132 iocb->ulpBdeCount = 1;
133 iocb->ulpClass = CLASS3;
139 lpfc_free_scsi_buf(struct lpfc_scsi_buf * psb)
141 struct lpfc_hba *phba = psb->scsi_hba;
144 * There are only two special cases to consider. (1) the scsi command
145 * requested scatter-gather usage or (2) the scsi command allocated
146 * a request buffer, but did not request use_sg. There is a third
147 * case, but it does not require resource deallocation.
149 if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
150 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
151 psb->seg_cnt, psb->pCmd->sc_data_direction);
153 if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
154 dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
155 psb->pCmd->request_bufflen,
156 psb->pCmd->sc_data_direction);
160 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
164 lpfc_scsi_prep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
166 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
167 struct scatterlist *sgel = NULL;
168 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
169 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
170 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
172 uint32_t i, num_bde = 0;
173 int datadir = scsi_cmnd->sc_data_direction;
177 * There are three possibilities here - use scatter-gather segment, use
178 * the single mapping, or neither. Start the lpfc command prep by
179 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
183 if (scsi_cmnd->use_sg) {
185 * The driver stores the segment count returned from pci_map_sg
186 * because this a count of dma-mappings used to map the use_sg
187 * pages. They are not guaranteed to be the same for those
188 * architectures that implement an IOMMU.
190 sgel = (struct scatterlist *)scsi_cmnd->request_buffer;
191 lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
192 scsi_cmnd->use_sg, datadir);
193 if (lpfc_cmd->seg_cnt == 0)
196 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
197 printk(KERN_ERR "%s: Too many sg segments from "
198 "dma_map_sg. Config %d, seg_cnt %d",
199 __FUNCTION__, phba->cfg_sg_seg_cnt,
201 dma_unmap_sg(&phba->pcidev->dev, sgel,
202 lpfc_cmd->seg_cnt, datadir);
207 * The driver established a maximum scatter-gather segment count
208 * during probe that limits the number of sg elements in any
209 * single scsi command. Just run through the seg_cnt and format
212 for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
213 physaddr = sg_dma_address(sgel);
214 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
215 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
216 bpl->tus.f.bdeSize = sg_dma_len(sgel);
217 if (datadir == DMA_TO_DEVICE)
218 bpl->tus.f.bdeFlags = 0;
220 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
221 bpl->tus.w = le32_to_cpu(bpl->tus.w);
226 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
227 physaddr = dma_map_single(&phba->pcidev->dev,
228 scsi_cmnd->request_buffer,
229 scsi_cmnd->request_bufflen,
231 dma_error = dma_mapping_error(physaddr);
233 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
234 "%d:0718 Unable to dma_map_single "
235 "request_buffer: x%x\n",
236 phba->brd_no, dma_error);
240 lpfc_cmd->nonsg_phys = physaddr;
241 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
242 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
243 bpl->tus.f.bdeSize = scsi_cmnd->request_bufflen;
244 if (datadir == DMA_TO_DEVICE)
245 bpl->tus.f.bdeFlags = 0;
247 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
248 bpl->tus.w = le32_to_cpu(bpl->tus.w);
254 * Finish initializing those IOCB fields that are dependent on the
255 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
256 * reinitialized since all iocb memory resources are used many times
257 * for transmit, receive, and continuation bpl's.
259 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
260 iocb_cmd->un.fcpi64.bdl.bdeSize +=
261 (num_bde * sizeof (struct ulp_bde64));
262 iocb_cmd->ulpBdeCount = 1;
264 fcp_cmnd->fcpDl = be32_to_cpu(scsi_cmnd->request_bufflen);
269 lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
271 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
272 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
273 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
274 struct lpfc_hba *phba = lpfc_cmd->scsi_hba;
275 uint32_t fcpi_parm = lpfc_cmd->cur_iocbq.iocb.un.fcpi.fcpi_parm;
276 uint32_t resp_info = fcprsp->rspStatus2;
277 uint32_t scsi_status = fcprsp->rspStatus3;
278 uint32_t host_status = DID_OK;
282 * If this is a task management command, there is no
283 * scsi packet associated with this lpfc_cmd. The driver
286 if (fcpcmd->fcpCntl2) {
291 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
292 "%d:0730 FCP command failed: RSP "
293 "Data: x%x x%x x%x x%x x%x x%x\n",
294 phba->brd_no, resp_info, scsi_status,
295 be32_to_cpu(fcprsp->rspResId),
296 be32_to_cpu(fcprsp->rspSnsLen),
297 be32_to_cpu(fcprsp->rspRspLen),
300 if (resp_info & RSP_LEN_VALID) {
301 rsplen = be32_to_cpu(fcprsp->rspRspLen);
302 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
303 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
304 host_status = DID_ERROR;
309 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
310 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
311 if (snslen > SCSI_SENSE_BUFFERSIZE)
312 snslen = SCSI_SENSE_BUFFERSIZE;
314 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
318 if (resp_info & RESID_UNDER) {
319 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
321 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
322 "%d:0716 FCP Read Underrun, expected %d, "
323 "residual %d Data: x%x x%x x%x\n", phba->brd_no,
324 be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
325 fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
328 * The cmnd->underflow is the minimum number of bytes that must
329 * be transfered for this command. Provided a sense condition
330 * is not present, make sure the actual amount transferred is at
331 * least the underflow value or fail.
333 if (!(resp_info & SNS_LEN_VALID) &&
334 (scsi_status == SAM_STAT_GOOD) &&
335 (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
336 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
337 "%d:0717 FCP command x%x residual "
338 "underrun converted to error "
339 "Data: x%x x%x x%x\n", phba->brd_no,
340 cmnd->cmnd[0], cmnd->request_bufflen,
341 cmnd->resid, cmnd->underflow);
343 host_status = DID_ERROR;
345 } else if (resp_info & RESID_OVER) {
346 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
347 "%d:0720 FCP command x%x residual "
348 "overrun error. Data: x%x x%x \n",
349 phba->brd_no, cmnd->cmnd[0],
350 cmnd->request_bufflen, cmnd->resid);
351 host_status = DID_ERROR;
354 * Check SLI validation that all the transfer was actually done
355 * (fcpi_parm should be zero). Apply check only to reads.
357 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
358 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
359 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
360 "%d:0734 FCP Read Check Error Data: "
361 "x%x x%x x%x x%x\n", phba->brd_no,
362 be32_to_cpu(fcpcmd->fcpDl),
363 be32_to_cpu(fcprsp->rspResId),
364 fcpi_parm, cmnd->cmnd[0]);
365 host_status = DID_ERROR;
366 cmnd->resid = cmnd->request_bufflen;
370 cmnd->result = ScsiResult(host_status, scsi_status);
374 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
375 struct lpfc_iocbq *pIocbOut)
377 struct lpfc_scsi_buf *lpfc_cmd =
378 (struct lpfc_scsi_buf *) pIocbIn->context1;
379 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
380 struct lpfc_nodelist *pnode = rdata->pnode;
381 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
384 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
385 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
387 if (lpfc_cmd->status) {
388 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
389 (lpfc_cmd->result & IOERR_DRVR_MASK))
390 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
391 else if (lpfc_cmd->status >= IOSTAT_CNT)
392 lpfc_cmd->status = IOSTAT_DEFAULT;
394 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
395 "%d:0729 FCP cmd x%x failed <%d/%d> status: "
396 "x%x result: x%x Data: x%x x%x\n",
397 phba->brd_no, cmd->cmnd[0], cmd->device->id,
398 cmd->device->lun, lpfc_cmd->status,
399 lpfc_cmd->result, pIocbOut->iocb.ulpContext,
400 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
402 switch (lpfc_cmd->status) {
403 case IOSTAT_FCP_RSP_ERROR:
404 /* Call FCP RSP handler to determine result */
405 lpfc_handle_fcp_err(lpfc_cmd);
407 case IOSTAT_NPORT_BSY:
408 case IOSTAT_FABRIC_BSY:
409 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
412 cmd->result = ScsiResult(DID_ERROR, 0);
417 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
418 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
420 cmd->result = ScsiResult(DID_OK, 0);
423 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
424 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
426 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
427 "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
428 "SNS x%x x%x Data: x%x x%x\n",
429 phba->brd_no, cmd->device->id,
430 cmd->device->lun, cmd, cmd->result,
431 *lp, *(lp + 3), cmd->retries, cmd->resid);
434 spin_lock_irqsave(phba->host->host_lock, iflag);
435 lpfc_free_scsi_buf(lpfc_cmd);
436 cmd->host_scribble = NULL;
437 spin_unlock_irqrestore(phba->host->host_lock, iflag);
443 lpfc_scsi_prep_cmnd(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd,
444 struct lpfc_nodelist *pnode)
446 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
447 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
448 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
449 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
450 int datadir = scsi_cmnd->sc_data_direction;
452 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
453 /* clear task management bits */
454 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
456 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
457 &lpfc_cmd->fcp_cmnd->fcp_lun);
459 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
461 if (scsi_cmnd->device->tagged_supported) {
462 switch (scsi_cmnd->tag) {
463 case HEAD_OF_QUEUE_TAG:
464 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
466 case ORDERED_QUEUE_TAG:
467 fcp_cmnd->fcpCntl1 = ORDERED_Q;
470 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
474 fcp_cmnd->fcpCntl1 = 0;
477 * There are three possibilities here - use scatter-gather segment, use
478 * the single mapping, or neither. Start the lpfc command prep by
479 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
482 if (scsi_cmnd->use_sg) {
483 if (datadir == DMA_TO_DEVICE) {
484 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
485 iocb_cmd->un.fcpi.fcpi_parm = 0;
487 fcp_cmnd->fcpCntl3 = WRITE_DATA;
488 phba->fc4OutputRequests++;
490 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
491 iocb_cmd->ulpPU = PARM_READ_CHECK;
492 iocb_cmd->un.fcpi.fcpi_parm =
493 scsi_cmnd->request_bufflen;
494 fcp_cmnd->fcpCntl3 = READ_DATA;
495 phba->fc4InputRequests++;
497 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
498 if (datadir == DMA_TO_DEVICE) {
499 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
500 iocb_cmd->un.fcpi.fcpi_parm = 0;
502 fcp_cmnd->fcpCntl3 = WRITE_DATA;
503 phba->fc4OutputRequests++;
505 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
506 iocb_cmd->ulpPU = PARM_READ_CHECK;
507 iocb_cmd->un.fcpi.fcpi_parm =
508 scsi_cmnd->request_bufflen;
509 fcp_cmnd->fcpCntl3 = READ_DATA;
510 phba->fc4InputRequests++;
513 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
514 iocb_cmd->un.fcpi.fcpi_parm = 0;
516 fcp_cmnd->fcpCntl3 = 0;
517 phba->fc4ControlRequests++;
521 * Finish initializing those IOCB fields that are independent
522 * of the scsi_cmnd request_buffer
524 piocbq->iocb.ulpContext = pnode->nlp_rpi;
525 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
526 piocbq->iocb.ulpFCP2Rcvy = 1;
528 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
529 piocbq->context1 = lpfc_cmd;
530 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
531 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
535 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
536 struct lpfc_scsi_buf *lpfc_cmd,
537 uint8_t task_mgmt_cmd)
539 struct lpfc_sli *psli;
540 struct lpfc_iocbq *piocbq;
542 struct fcp_cmnd *fcp_cmnd;
543 struct scsi_device *scsi_dev = lpfc_cmd->pCmd->device;
544 struct lpfc_rport_data *rdata = scsi_dev->hostdata;
545 struct lpfc_nodelist *ndlp = rdata->pnode;
547 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
552 piocbq = &(lpfc_cmd->cur_iocbq);
553 piocb = &piocbq->iocb;
555 fcp_cmnd = lpfc_cmd->fcp_cmnd;
556 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
557 &lpfc_cmd->fcp_cmnd->fcp_lun);
558 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
560 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
562 piocb->ulpContext = ndlp->nlp_rpi;
563 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
564 piocb->ulpFCP2Rcvy = 1;
566 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
568 /* ulpTimeout is only one byte */
569 if (lpfc_cmd->timeout > 0xff) {
571 * Do not timeout the command at the firmware level.
572 * The driver will provide the timeout mechanism.
574 piocb->ulpTimeout = 0;
576 piocb->ulpTimeout = lpfc_cmd->timeout;
579 lpfc_cmd->rdata = rdata;
581 switch (task_mgmt_cmd) {
583 /* Issue LUN Reset to TGT <num> LUN <num> */
584 lpfc_printf_log(phba,
587 "%d:0703 Issue LUN Reset to TGT %d LUN %d "
590 scsi_dev->id, scsi_dev->lun,
591 ndlp->nlp_rpi, ndlp->nlp_flag);
594 case FCP_ABORT_TASK_SET:
595 /* Issue Abort Task Set to TGT <num> LUN <num> */
596 lpfc_printf_log(phba,
599 "%d:0701 Issue Abort Task Set to TGT %d LUN %d "
602 scsi_dev->id, scsi_dev->lun,
603 ndlp->nlp_rpi, ndlp->nlp_flag);
606 case FCP_TARGET_RESET:
607 /* Issue Target Reset to TGT <num> */
608 lpfc_printf_log(phba,
611 "%d:0702 Issue Target Reset to TGT %d "
614 scsi_dev->id, ndlp->nlp_rpi,
623 lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba)
625 struct lpfc_iocbq *iocbq;
626 struct lpfc_iocbq *iocbqrsp = NULL;
627 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
630 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_TARGET_RESET);
634 lpfc_cmd->scsi_hba = phba;
635 iocbq = &lpfc_cmd->cur_iocbq;
636 list_remove_head(lpfc_iocb_list, iocbqrsp, struct lpfc_iocbq, list);
640 ret = lpfc_sli_issue_iocb_wait(phba,
641 &phba->sli.ring[phba->sli.fcp_ring],
642 iocbq, iocbqrsp, lpfc_cmd->timeout);
643 if (ret != IOCB_SUCCESS) {
644 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
648 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
649 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
650 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
651 (lpfc_cmd->result & IOERR_DRVR_MASK))
652 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
656 * All outstanding txcmplq I/Os should have been aborted by the target.
657 * Unfortunately, some targets do not abide by this forcing the driver
660 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
661 lpfc_cmd->pCmd->device->id,
662 lpfc_cmd->pCmd->device->lun, 0, LPFC_CTX_TGT);
664 lpfc_sli_release_iocbq(phba, iocbqrsp);
669 lpfc_scsi_cmd_iocb_cleanup (struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
670 struct lpfc_iocbq *pIocbOut)
673 struct lpfc_scsi_buf *lpfc_cmd =
674 (struct lpfc_scsi_buf *) pIocbIn->context1;
676 spin_lock_irqsave(phba->host->host_lock, iflag);
677 lpfc_free_scsi_buf(lpfc_cmd);
678 spin_unlock_irqrestore(phba->host->host_lock, iflag);
682 lpfc_scsi_cmd_iocb_cmpl_aborted(struct lpfc_hba *phba,
683 struct lpfc_iocbq *pIocbIn,
684 struct lpfc_iocbq *pIocbOut)
686 struct scsi_cmnd *ml_cmd =
687 ((struct lpfc_scsi_buf *) pIocbIn->context1)->pCmd;
689 lpfc_scsi_cmd_iocb_cleanup (phba, pIocbIn, pIocbOut);
690 ml_cmd->host_scribble = NULL;
694 lpfc_info(struct Scsi_Host *host)
696 struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata[0];
698 static char lpfcinfobuf[384];
700 memset(lpfcinfobuf,0,384);
701 if (phba && phba->pcidev){
702 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
703 len = strlen(lpfcinfobuf);
704 snprintf(lpfcinfobuf + len,
706 " on PCI bus %02x device %02x irq %d",
707 phba->pcidev->bus->number,
710 len = strlen(lpfcinfobuf);
712 snprintf(lpfcinfobuf + len,
722 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
724 struct lpfc_hba *phba =
725 (struct lpfc_hba *) cmnd->device->host->hostdata[0];
726 struct lpfc_sli *psli = &phba->sli;
727 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
728 struct lpfc_nodelist *ndlp = rdata->pnode;
729 struct lpfc_scsi_buf *lpfc_cmd = NULL;
730 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
731 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
734 err = fc_remote_port_chkready(rport);
737 goto out_fail_command;
741 * Catch race where our node has transitioned, but the
742 * transport is still transitioning.
745 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
746 goto out_fail_command;
749 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
750 if (lpfc_cmd == NULL) {
751 printk(KERN_WARNING "%s: No buffer available - list empty, "
752 "total count %d\n", __FUNCTION__, phba->total_scsi_bufs);
757 * Store the midlayer's command structure for the completion phase
758 * and complete the command initialization.
760 lpfc_cmd->pCmd = cmnd;
761 lpfc_cmd->rdata = rdata;
762 lpfc_cmd->timeout = 0;
763 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
764 cmnd->scsi_done = done;
766 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
768 goto out_host_busy_free_buf;
770 lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
772 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
773 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
775 goto out_host_busy_free_buf;
778 out_host_busy_free_buf:
779 lpfc_free_scsi_buf(lpfc_cmd);
780 cmnd->host_scribble = NULL;
782 return SCSI_MLQUEUE_HOST_BUSY;
790 __lpfc_abort_handler(struct scsi_cmnd *cmnd)
792 struct lpfc_hba *phba =
793 (struct lpfc_hba *)cmnd->device->host->hostdata[0];
794 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
795 struct lpfc_iocbq *iocb, *next_iocb;
796 struct lpfc_iocbq *abtsiocb = NULL;
797 struct lpfc_scsi_buf *lpfc_cmd;
798 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
801 unsigned int id, lun;
802 unsigned int loop_count = 0;
803 int ret = IOCB_SUCCESS;
806 * If the host_scribble data area is NULL, then the driver has already
807 * completed this command, but the midlayer did not see the completion
808 * before the eh fired. Just return SUCCESS.
810 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
814 /* save these now since lpfc_cmd can be freed */
815 id = lpfc_cmd->pCmd->device->id;
816 lun = lpfc_cmd->pCmd->device->lun;
817 snum = lpfc_cmd->pCmd->serial_number;
819 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
821 if (iocb->context1 != lpfc_cmd)
824 list_del_init(&iocb->list);
826 if (!iocb->iocb_cmpl)
827 lpfc_sli_release_iocbq(phba, iocb);
829 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
830 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
831 lpfc_scsi_cmd_iocb_cmpl_aborted(phba, iocb, iocb);
837 list_remove_head(lpfc_iocb_list, abtsiocb, struct lpfc_iocbq, list);
838 if (abtsiocb == NULL)
842 * The scsi command was not in the txq. Check the txcmplq and if it is
843 * found, send an abort to the FW.
845 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
846 if (iocb->context1 != lpfc_cmd)
849 iocb->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl_aborted;
851 icmd = &abtsiocb->iocb;
852 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
853 icmd->un.acxri.abortContextTag = cmd->ulpContext;
854 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
857 icmd->ulpClass = cmd->ulpClass;
858 if (phba->hba_state >= LPFC_LINK_UP)
859 icmd->ulpCommand = CMD_ABORT_XRI_CN;
861 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
863 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
864 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) ==
866 lpfc_sli_release_iocbq(phba, abtsiocb);
871 /* Wait for abort to complete */
872 while (cmnd->host_scribble)
874 spin_unlock_irq(phba->host->host_lock);
875 set_current_state(TASK_UNINTERRUPTIBLE);
876 schedule_timeout(LPFC_ABORT_WAIT*HZ);
877 spin_lock_irq(phba->host->host_lock);
879 > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
883 if(cmnd->host_scribble) {
884 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
885 "%d:0748 abort handler timed "
886 "out waiting for abort to "
888 "x%x x%x x%x x%lx\n",
889 phba->brd_no, ret, id, lun, snum);
890 cmnd->host_scribble = NULL;
891 iocb->iocb_cmpl = lpfc_scsi_cmd_iocb_cleanup;
899 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
900 "%d:0749 SCSI layer issued abort device "
901 "Data: x%x x%x x%x x%lx\n",
902 phba->brd_no, ret, id, lun, snum);
904 return ret == IOCB_SUCCESS ? SUCCESS : FAILED;
908 lpfc_abort_handler(struct scsi_cmnd *cmnd)
911 spin_lock_irq(cmnd->device->host->host_lock);
912 rc = __lpfc_abort_handler(cmnd);
913 spin_unlock_irq(cmnd->device->host->host_lock);
918 __lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
920 struct Scsi_Host *shost = cmnd->device->host;
921 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
922 struct lpfc_scsi_buf *lpfc_cmd = NULL;
923 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
924 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
925 struct lpfc_iocbq *iocbq, *iocbqrsp = NULL;
926 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
927 struct lpfc_nodelist *pnode = rdata->pnode;
932 * If target is not in a MAPPED state, delay the reset until
933 * target is rediscovered or nodev timeout expires.
939 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
940 spin_unlock_irq(phba->host->host_lock);
941 set_current_state(TASK_UNINTERRUPTIBLE);
942 schedule_timeout( HZ/2);
943 spin_lock_irq(phba->host->host_lock);
945 if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
949 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
950 if (lpfc_cmd == NULL)
953 lpfc_cmd->pCmd = cmnd;
954 lpfc_cmd->timeout = 60;
955 lpfc_cmd->scsi_hba = phba;
957 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
959 goto out_free_scsi_buf;
961 iocbq = &lpfc_cmd->cur_iocbq;
963 /* get a buffer for this IOCB command response */
964 list_remove_head(lpfc_iocb_list, iocbqrsp, struct lpfc_iocbq, list);
965 if (iocbqrsp == NULL)
966 goto out_free_scsi_buf;
968 ret = lpfc_sli_issue_iocb_wait(phba,
969 &phba->sli.ring[phba->sli.fcp_ring],
970 iocbq, iocbqrsp, lpfc_cmd->timeout);
971 if (ret == IOCB_SUCCESS)
974 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
975 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
976 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT)
977 if (lpfc_cmd->result & IOERR_DRVR_MASK)
978 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
981 * All outstanding txcmplq I/Os should have been aborted by the target.
982 * Unfortunately, some targets do not abide by this forcing the driver
985 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
986 cmnd->device->id, cmnd->device->lun, 0,
990 while((cnt = lpfc_sli_sum_iocb(phba,
991 &phba->sli.ring[phba->sli.fcp_ring],
992 cmnd->device->id, cmnd->device->lun,
994 spin_unlock_irq(phba->host->host_lock);
995 set_current_state(TASK_UNINTERRUPTIBLE);
996 schedule_timeout(LPFC_RESET_WAIT*HZ);
997 spin_lock_irq(phba->host->host_lock);
1000 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1005 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1006 "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1010 lpfc_sli_release_iocbq(phba, iocbqrsp);
1013 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1014 "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1015 "Data: x%x x%x x%x\n",
1016 phba->brd_no, lpfc_cmd->pCmd->device->id,
1017 lpfc_cmd->pCmd->device->lun, ret, lpfc_cmd->status,
1019 lpfc_free_scsi_buf(lpfc_cmd);
1025 lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
1028 spin_lock_irq(cmnd->device->host->host_lock);
1029 rc = __lpfc_reset_lun_handler(cmnd);
1030 spin_unlock_irq(cmnd->device->host->host_lock);
1035 * Note: midlayer calls this function with the host_lock held
1038 __lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
1040 struct Scsi_Host *shost = cmnd->device->host;
1041 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
1042 struct lpfc_nodelist *ndlp = NULL;
1044 int ret = FAILED, i, err_count = 0;
1046 unsigned int midlayer_id = 0;
1047 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1048 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
1050 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1051 if (lpfc_cmd == NULL)
1054 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1055 lpfc_cmd->timeout = 60;
1056 lpfc_cmd->pCmd = cmnd;
1057 lpfc_cmd->scsi_hba = phba;
1060 * Since the driver manages a single bus device, reset all
1061 * targets known to the driver. Should any target reset
1062 * fail, this routine returns failure to the midlayer.
1064 midlayer_id = cmnd->device->id;
1065 for (i = 0; i < MAX_FCP_TARGET; i++) {
1066 /* Search the mapped list for this target ID */
1068 list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1069 if ((i == ndlp->nlp_sid) && ndlp->rport) {
1077 lpfc_cmd->pCmd->device->id = i;
1078 lpfc_cmd->pCmd->device->hostdata = ndlp->rport->dd_data;
1079 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
1080 if (ret != SUCCESS) {
1081 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1082 "%d:0713 Bus Reset on target %d failed\n",
1088 cmnd->device->id = midlayer_id;
1090 while((cnt = lpfc_sli_sum_iocb(phba,
1091 &phba->sli.ring[phba->sli.fcp_ring],
1092 0, 0, LPFC_CTX_HOST))) {
1093 spin_unlock_irq(phba->host->host_lock);
1094 set_current_state(TASK_UNINTERRUPTIBLE);
1095 schedule_timeout(LPFC_RESET_WAIT*HZ);
1096 spin_lock_irq(phba->host->host_lock);
1099 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1104 /* flush all outstanding commands on the host */
1105 i = lpfc_sli_abort_iocb(phba,
1106 &phba->sli.ring[phba->sli.fcp_ring], 0, 0, 0,
1109 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1110 "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1111 phba->brd_no, cnt, i);
1117 lpfc_free_scsi_buf(lpfc_cmd);
1118 lpfc_printf_log(phba,
1121 "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1128 lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
1131 spin_lock_irq(cmnd->device->host->host_lock);
1132 rc = __lpfc_reset_bus_handler(cmnd);
1133 spin_unlock_irq(cmnd->device->host->host_lock);
1138 lpfc_slave_alloc(struct scsi_device *sdev)
1140 struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata[0];
1141 struct lpfc_scsi_buf *scsi_buf = NULL;
1142 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1143 uint32_t total = 0, i;
1144 uint32_t num_to_alloc = 0;
1145 unsigned long flags;
1147 if (!rport || fc_remote_port_chkready(rport))
1150 sdev->hostdata = rport->dd_data;
1153 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1154 * available list of scsi buffers. Don't allocate more than the
1155 * HBA limit conveyed to the midlayer via the host structure. Note
1156 * that this list of scsi bufs exists for the lifetime of the driver.
1158 total = phba->total_scsi_bufs;
1159 num_to_alloc = LPFC_CMD_PER_LUN;
1160 if (total >= phba->cfg_hba_queue_depth) {
1161 printk(KERN_WARNING "%s, At config limitation of "
1162 "%d allocated scsi_bufs\n", __FUNCTION__, total);
1164 } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
1165 num_to_alloc = phba->cfg_hba_queue_depth - total;
1168 for (i = 0; i < num_to_alloc; i++) {
1169 scsi_buf = lpfc_get_scsi_buf(phba);
1171 printk(KERN_ERR "%s, failed to allocate "
1172 "scsi_buf\n", __FUNCTION__);
1176 spin_lock_irqsave(phba->host->host_lock, flags);
1177 phba->total_scsi_bufs++;
1178 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
1179 spin_unlock_irqrestore(phba->host->host_lock, flags);
1185 lpfc_slave_configure(struct scsi_device *sdev)
1187 struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata[0];
1188 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1190 if (sdev->tagged_supported)
1191 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1193 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1196 * Initialize the fc transport attributes for the target
1197 * containing this scsi device. Also note that the driver's
1198 * target pointer is stored in the starget_data for the
1199 * driver's sysfs entry point functions.
1201 rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
1207 lpfc_slave_destroy(struct scsi_device *sdev)
1209 sdev->hostdata = NULL;
1213 struct scsi_host_template lpfc_template = {
1214 .module = THIS_MODULE,
1215 .name = LPFC_DRIVER_NAME,
1217 .queuecommand = lpfc_queuecommand,
1218 .eh_abort_handler = lpfc_abort_handler,
1219 .eh_device_reset_handler= lpfc_reset_lun_handler,
1220 .eh_bus_reset_handler = lpfc_reset_bus_handler,
1221 .slave_alloc = lpfc_slave_alloc,
1222 .slave_configure = lpfc_slave_configure,
1223 .slave_destroy = lpfc_slave_destroy,
1225 .sg_tablesize = LPFC_SG_SEG_CNT,
1226 .cmd_per_lun = LPFC_CMD_PER_LUN,
1227 .use_clustering = ENABLE_CLUSTERING,
1228 .shost_attrs = lpfc_host_attrs,
1229 .max_sectors = 0xFFFF,