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 iocbq->iocb_flag |= LPFC_IO_POLL;
641 ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
642 &phba->sli.ring[phba->sli.fcp_ring],
643 iocbq, SLI_IOCB_HIGH_PRIORITY,
646 if (ret != IOCB_SUCCESS) {
647 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
651 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
652 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
653 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
654 (lpfc_cmd->result & IOERR_DRVR_MASK))
655 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
659 * All outstanding txcmplq I/Os should have been aborted by the target.
660 * Unfortunately, some targets do not abide by this forcing the driver
663 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
664 lpfc_cmd->pCmd->device->id,
665 lpfc_cmd->pCmd->device->lun, 0, LPFC_CTX_TGT);
667 lpfc_sli_release_iocbq(phba, iocbqrsp);
672 lpfc_scsi_cmd_iocb_cleanup (struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
673 struct lpfc_iocbq *pIocbOut)
676 struct lpfc_scsi_buf *lpfc_cmd =
677 (struct lpfc_scsi_buf *) pIocbIn->context1;
679 spin_lock_irqsave(phba->host->host_lock, iflag);
680 lpfc_free_scsi_buf(lpfc_cmd);
681 spin_unlock_irqrestore(phba->host->host_lock, iflag);
685 lpfc_scsi_cmd_iocb_cmpl_aborted(struct lpfc_hba *phba,
686 struct lpfc_iocbq *pIocbIn,
687 struct lpfc_iocbq *pIocbOut)
689 struct scsi_cmnd *ml_cmd =
690 ((struct lpfc_scsi_buf *) pIocbIn->context1)->pCmd;
692 lpfc_scsi_cmd_iocb_cleanup (phba, pIocbIn, pIocbOut);
693 ml_cmd->host_scribble = NULL;
697 lpfc_info(struct Scsi_Host *host)
699 struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata[0];
701 static char lpfcinfobuf[384];
703 memset(lpfcinfobuf,0,384);
704 if (phba && phba->pcidev){
705 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
706 len = strlen(lpfcinfobuf);
707 snprintf(lpfcinfobuf + len,
709 " on PCI bus %02x device %02x irq %d",
710 phba->pcidev->bus->number,
713 len = strlen(lpfcinfobuf);
715 snprintf(lpfcinfobuf + len,
725 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
727 struct lpfc_hba *phba =
728 (struct lpfc_hba *) cmnd->device->host->hostdata[0];
729 struct lpfc_sli *psli = &phba->sli;
730 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
731 struct lpfc_nodelist *ndlp = rdata->pnode;
732 struct lpfc_scsi_buf *lpfc_cmd = NULL;
733 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
734 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
737 err = fc_remote_port_chkready(rport);
740 goto out_fail_command;
744 * Catch race where our node has transitioned, but the
745 * transport is still transitioning.
748 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
749 goto out_fail_command;
752 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
753 if (lpfc_cmd == NULL) {
754 printk(KERN_WARNING "%s: No buffer available - list empty, "
755 "total count %d\n", __FUNCTION__, phba->total_scsi_bufs);
760 * Store the midlayer's command structure for the completion phase
761 * and complete the command initialization.
763 lpfc_cmd->pCmd = cmnd;
764 lpfc_cmd->rdata = rdata;
765 lpfc_cmd->timeout = 0;
766 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
767 cmnd->scsi_done = done;
769 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
771 goto out_host_busy_free_buf;
773 lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
775 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
776 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
778 goto out_host_busy_free_buf;
781 out_host_busy_free_buf:
782 lpfc_free_scsi_buf(lpfc_cmd);
783 cmnd->host_scribble = NULL;
785 return SCSI_MLQUEUE_HOST_BUSY;
793 __lpfc_abort_handler(struct scsi_cmnd *cmnd)
795 struct lpfc_hba *phba =
796 (struct lpfc_hba *)cmnd->device->host->hostdata[0];
797 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
798 struct lpfc_iocbq *iocb, *next_iocb;
799 struct lpfc_iocbq *abtsiocb = NULL;
800 struct lpfc_scsi_buf *lpfc_cmd;
801 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
804 unsigned int id, lun;
805 unsigned int loop_count = 0;
806 int ret = IOCB_SUCCESS;
809 * If the host_scribble data area is NULL, then the driver has already
810 * completed this command, but the midlayer did not see the completion
811 * before the eh fired. Just return SUCCESS.
813 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
817 /* save these now since lpfc_cmd can be freed */
818 id = lpfc_cmd->pCmd->device->id;
819 lun = lpfc_cmd->pCmd->device->lun;
820 snum = lpfc_cmd->pCmd->serial_number;
822 list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
824 if (iocb->context1 != lpfc_cmd)
827 list_del_init(&iocb->list);
829 if (!iocb->iocb_cmpl)
830 lpfc_sli_release_iocbq(phba, iocb);
832 cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
833 cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
834 lpfc_scsi_cmd_iocb_cmpl_aborted(phba, iocb, iocb);
840 list_remove_head(lpfc_iocb_list, abtsiocb, struct lpfc_iocbq, list);
841 if (abtsiocb == NULL)
845 * The scsi command was not in the txq. Check the txcmplq and if it is
846 * found, send an abort to the FW.
848 list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
849 if (iocb->context1 != lpfc_cmd)
852 iocb->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl_aborted;
854 icmd = &abtsiocb->iocb;
855 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
856 icmd->un.acxri.abortContextTag = cmd->ulpContext;
857 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
860 icmd->ulpClass = cmd->ulpClass;
861 if (phba->hba_state >= LPFC_LINK_UP)
862 icmd->ulpCommand = CMD_ABORT_XRI_CN;
864 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
866 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
867 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) ==
869 lpfc_sli_release_iocbq(phba, abtsiocb);
874 /* Wait for abort to complete */
875 while (cmnd->host_scribble)
877 spin_unlock_irq(phba->host->host_lock);
878 set_current_state(TASK_UNINTERRUPTIBLE);
879 schedule_timeout(LPFC_ABORT_WAIT*HZ);
880 spin_lock_irq(phba->host->host_lock);
882 > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
886 if(cmnd->host_scribble) {
887 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
888 "%d:0748 abort handler timed "
889 "out waiting for abort to "
891 "x%x x%x x%x x%lx\n",
892 phba->brd_no, ret, id, lun, snum);
893 cmnd->host_scribble = NULL;
894 iocb->iocb_cmpl = lpfc_scsi_cmd_iocb_cleanup;
902 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
903 "%d:0749 SCSI layer issued abort device "
904 "Data: x%x x%x x%x x%lx\n",
905 phba->brd_no, ret, id, lun, snum);
907 return ret == IOCB_SUCCESS ? SUCCESS : FAILED;
911 lpfc_abort_handler(struct scsi_cmnd *cmnd)
914 spin_lock_irq(cmnd->device->host->host_lock);
915 rc = __lpfc_abort_handler(cmnd);
916 spin_unlock_irq(cmnd->device->host->host_lock);
921 __lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
923 struct Scsi_Host *shost = cmnd->device->host;
924 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
925 struct lpfc_sli *psli = &phba->sli;
926 struct lpfc_scsi_buf *lpfc_cmd = NULL;
927 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
928 struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
929 struct lpfc_iocbq *iocbq, *iocbqrsp = NULL;
930 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
931 struct lpfc_nodelist *pnode = rdata->pnode;
936 * If target is not in a MAPPED state, delay the reset until
937 * target is rediscovered or nodev timeout expires.
943 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
944 spin_unlock_irq(phba->host->host_lock);
945 set_current_state(TASK_UNINTERRUPTIBLE);
946 schedule_timeout( HZ/2);
947 spin_lock_irq(phba->host->host_lock);
949 if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
953 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
954 if (lpfc_cmd == NULL)
957 lpfc_cmd->pCmd = cmnd;
958 lpfc_cmd->timeout = 60;
959 lpfc_cmd->scsi_hba = phba;
961 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
963 goto out_free_scsi_buf;
965 iocbq = &lpfc_cmd->cur_iocbq;
967 /* get a buffer for this IOCB command response */
968 list_remove_head(lpfc_iocb_list, iocbqrsp, struct lpfc_iocbq, list);
969 if (iocbqrsp == NULL)
970 goto out_free_scsi_buf;
972 iocbq->iocb_flag |= LPFC_IO_POLL;
973 iocbq->iocb_cmpl = lpfc_sli_wake_iocb_high_priority;
975 ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
976 &phba->sli.ring[psli->fcp_ring],
977 iocbq, 0, iocbqrsp, 60);
978 if (ret == IOCB_SUCCESS)
981 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
982 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
983 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT)
984 if (lpfc_cmd->result & IOERR_DRVR_MASK)
985 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
988 * All outstanding txcmplq I/Os should have been aborted by the target.
989 * Unfortunately, some targets do not abide by this forcing the driver
992 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
993 cmnd->device->id, cmnd->device->lun, 0,
997 while((cnt = lpfc_sli_sum_iocb(phba,
998 &phba->sli.ring[phba->sli.fcp_ring],
999 cmnd->device->id, cmnd->device->lun,
1001 spin_unlock_irq(phba->host->host_lock);
1002 set_current_state(TASK_UNINTERRUPTIBLE);
1003 schedule_timeout(LPFC_RESET_WAIT*HZ);
1004 spin_lock_irq(phba->host->host_lock);
1007 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1012 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1013 "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1017 lpfc_sli_release_iocbq(phba, iocbqrsp);
1020 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1021 "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1022 "Data: x%x x%x x%x\n",
1023 phba->brd_no, lpfc_cmd->pCmd->device->id,
1024 lpfc_cmd->pCmd->device->lun, ret, lpfc_cmd->status,
1026 lpfc_free_scsi_buf(lpfc_cmd);
1032 lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
1035 spin_lock_irq(cmnd->device->host->host_lock);
1036 rc = __lpfc_reset_lun_handler(cmnd);
1037 spin_unlock_irq(cmnd->device->host->host_lock);
1042 * Note: midlayer calls this function with the host_lock held
1045 __lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
1047 struct Scsi_Host *shost = cmnd->device->host;
1048 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
1049 struct lpfc_nodelist *ndlp = NULL;
1051 int ret = FAILED, i, err_count = 0;
1053 unsigned int midlayer_id = 0;
1054 struct lpfc_scsi_buf * lpfc_cmd = NULL;
1055 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
1057 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1058 if (lpfc_cmd == NULL)
1061 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1062 lpfc_cmd->timeout = 60;
1063 lpfc_cmd->pCmd = cmnd;
1064 lpfc_cmd->scsi_hba = phba;
1067 * Since the driver manages a single bus device, reset all
1068 * targets known to the driver. Should any target reset
1069 * fail, this routine returns failure to the midlayer.
1071 midlayer_id = cmnd->device->id;
1072 for (i = 0; i < MAX_FCP_TARGET; i++) {
1073 /* Search the mapped list for this target ID */
1075 list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1076 if ((i == ndlp->nlp_sid) && ndlp->rport) {
1084 lpfc_cmd->pCmd->device->id = i;
1085 lpfc_cmd->pCmd->device->hostdata = ndlp->rport->dd_data;
1086 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
1087 if (ret != SUCCESS) {
1088 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1089 "%d:0713 Bus Reset on target %d failed\n",
1095 cmnd->device->id = midlayer_id;
1097 while((cnt = lpfc_sli_sum_iocb(phba,
1098 &phba->sli.ring[phba->sli.fcp_ring],
1099 0, 0, LPFC_CTX_HOST))) {
1100 spin_unlock_irq(phba->host->host_lock);
1101 set_current_state(TASK_UNINTERRUPTIBLE);
1102 schedule_timeout(LPFC_RESET_WAIT*HZ);
1103 spin_lock_irq(phba->host->host_lock);
1106 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1111 /* flush all outstanding commands on the host */
1112 i = lpfc_sli_abort_iocb(phba,
1113 &phba->sli.ring[phba->sli.fcp_ring], 0, 0, 0,
1116 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1117 "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1118 phba->brd_no, cnt, i);
1124 lpfc_free_scsi_buf(lpfc_cmd);
1125 lpfc_printf_log(phba,
1128 "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1135 lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
1138 spin_lock_irq(cmnd->device->host->host_lock);
1139 rc = __lpfc_reset_bus_handler(cmnd);
1140 spin_unlock_irq(cmnd->device->host->host_lock);
1145 lpfc_slave_alloc(struct scsi_device *sdev)
1147 struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata[0];
1148 struct lpfc_scsi_buf *scsi_buf = NULL;
1149 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1150 uint32_t total = 0, i;
1151 uint32_t num_to_alloc = 0;
1152 unsigned long flags;
1154 if (!rport || fc_remote_port_chkready(rport))
1157 sdev->hostdata = rport->dd_data;
1160 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1161 * available list of scsi buffers. Don't allocate more than the
1162 * HBA limit conveyed to the midlayer via the host structure. Note
1163 * that this list of scsi bufs exists for the lifetime of the driver.
1165 total = phba->total_scsi_bufs;
1166 num_to_alloc = LPFC_CMD_PER_LUN;
1167 if (total >= phba->cfg_hba_queue_depth) {
1168 printk(KERN_WARNING "%s, At config limitation of "
1169 "%d allocated scsi_bufs\n", __FUNCTION__, total);
1171 } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
1172 num_to_alloc = phba->cfg_hba_queue_depth - total;
1175 for (i = 0; i < num_to_alloc; i++) {
1176 scsi_buf = lpfc_get_scsi_buf(phba);
1178 printk(KERN_ERR "%s, failed to allocate "
1179 "scsi_buf\n", __FUNCTION__);
1183 spin_lock_irqsave(phba->host->host_lock, flags);
1184 phba->total_scsi_bufs++;
1185 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
1186 spin_unlock_irqrestore(phba->host->host_lock, flags);
1192 lpfc_slave_configure(struct scsi_device *sdev)
1194 struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata[0];
1195 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1197 if (sdev->tagged_supported)
1198 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1200 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1203 * Initialize the fc transport attributes for the target
1204 * containing this scsi device. Also note that the driver's
1205 * target pointer is stored in the starget_data for the
1206 * driver's sysfs entry point functions.
1208 rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
1214 lpfc_slave_destroy(struct scsi_device *sdev)
1216 sdev->hostdata = NULL;
1220 struct scsi_host_template lpfc_template = {
1221 .module = THIS_MODULE,
1222 .name = LPFC_DRIVER_NAME,
1224 .queuecommand = lpfc_queuecommand,
1225 .eh_abort_handler = lpfc_abort_handler,
1226 .eh_device_reset_handler= lpfc_reset_lun_handler,
1227 .eh_bus_reset_handler = lpfc_reset_bus_handler,
1228 .slave_alloc = lpfc_slave_alloc,
1229 .slave_configure = lpfc_slave_configure,
1230 .slave_destroy = lpfc_slave_destroy,
1232 .sg_tablesize = LPFC_SG_SEG_CNT,
1233 .cmd_per_lun = LPFC_CMD_PER_LUN,
1234 .use_clustering = ENABLE_CLUSTERING,
1235 .shost_attrs = lpfc_host_attrs,
1236 .max_sectors = 0xFFFF,