1 /*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
3 * Fibre Channel Host Bus Adapters. *
4 * Copyright (C) 2004-2006 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_new_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);
91 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
93 psb->fcp_cmnd = psb->data;
94 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
95 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
96 sizeof(struct fcp_rsp);
98 /* Initialize local short-hand pointers. */
100 pdma_phys = psb->dma_handle;
103 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
104 * list bdes. Initialize the first two and leave the rest for
107 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
108 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
109 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
110 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
111 bpl->tus.w = le32_to_cpu(bpl->tus.w);
114 /* Setup the physical region for the FCP RSP */
115 pdma_phys += sizeof (struct fcp_cmnd);
116 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
117 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
118 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
119 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
120 bpl->tus.w = le32_to_cpu(bpl->tus.w);
123 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
124 * initialize it with all known data now.
126 pdma_phys += (sizeof (struct fcp_rsp));
127 iocb = &psb->cur_iocbq.iocb;
128 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
129 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
130 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
131 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
132 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
133 iocb->ulpBdeCount = 1;
134 iocb->ulpClass = CLASS3;
139 static struct lpfc_scsi_buf*
140 lpfc_get_scsi_buf(struct lpfc_hba * phba)
142 struct lpfc_scsi_buf * lpfc_cmd = NULL;
143 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
144 unsigned long iflag = 0;
146 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
147 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
148 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
153 lpfc_release_scsi_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
155 unsigned long iflag = 0;
157 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
159 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
160 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
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_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
272 * There are only two special cases to consider. (1) the scsi command
273 * requested scatter-gather usage or (2) the scsi command allocated
274 * a request buffer, but did not request use_sg. There is a third
275 * case, but it does not require resource deallocation.
277 if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
278 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
279 psb->seg_cnt, psb->pCmd->sc_data_direction);
281 if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
282 dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
283 psb->pCmd->request_bufflen,
284 psb->pCmd->sc_data_direction);
290 lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
292 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
293 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
294 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
295 struct lpfc_hba *phba = lpfc_cmd->scsi_hba;
296 uint32_t fcpi_parm = lpfc_cmd->cur_iocbq.iocb.un.fcpi.fcpi_parm;
297 uint32_t resp_info = fcprsp->rspStatus2;
298 uint32_t scsi_status = fcprsp->rspStatus3;
299 uint32_t host_status = DID_OK;
303 * If this is a task management command, there is no
304 * scsi packet associated with this lpfc_cmd. The driver
307 if (fcpcmd->fcpCntl2) {
312 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
313 "%d:0730 FCP command failed: RSP "
314 "Data: x%x x%x x%x x%x x%x x%x\n",
315 phba->brd_no, resp_info, scsi_status,
316 be32_to_cpu(fcprsp->rspResId),
317 be32_to_cpu(fcprsp->rspSnsLen),
318 be32_to_cpu(fcprsp->rspRspLen),
321 if (resp_info & RSP_LEN_VALID) {
322 rsplen = be32_to_cpu(fcprsp->rspRspLen);
323 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
324 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
325 host_status = DID_ERROR;
330 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
331 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
332 if (snslen > SCSI_SENSE_BUFFERSIZE)
333 snslen = SCSI_SENSE_BUFFERSIZE;
335 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
339 if (resp_info & RESID_UNDER) {
340 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
342 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
343 "%d:0716 FCP Read Underrun, expected %d, "
344 "residual %d Data: x%x x%x x%x\n", phba->brd_no,
345 be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
346 fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
349 * The cmnd->underflow is the minimum number of bytes that must
350 * be transfered for this command. Provided a sense condition
351 * is not present, make sure the actual amount transferred is at
352 * least the underflow value or fail.
354 if (!(resp_info & SNS_LEN_VALID) &&
355 (scsi_status == SAM_STAT_GOOD) &&
356 (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
357 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
358 "%d:0717 FCP command x%x residual "
359 "underrun converted to error "
360 "Data: x%x x%x x%x\n", phba->brd_no,
361 cmnd->cmnd[0], cmnd->request_bufflen,
362 cmnd->resid, cmnd->underflow);
364 host_status = DID_ERROR;
366 } else if (resp_info & RESID_OVER) {
367 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
368 "%d:0720 FCP command x%x residual "
369 "overrun error. Data: x%x x%x \n",
370 phba->brd_no, cmnd->cmnd[0],
371 cmnd->request_bufflen, cmnd->resid);
372 host_status = DID_ERROR;
375 * Check SLI validation that all the transfer was actually done
376 * (fcpi_parm should be zero). Apply check only to reads.
378 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
379 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
380 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
381 "%d:0734 FCP Read Check Error Data: "
382 "x%x x%x x%x x%x\n", phba->brd_no,
383 be32_to_cpu(fcpcmd->fcpDl),
384 be32_to_cpu(fcprsp->rspResId),
385 fcpi_parm, cmnd->cmnd[0]);
386 host_status = DID_ERROR;
387 cmnd->resid = cmnd->request_bufflen;
391 cmnd->result = ScsiResult(host_status, scsi_status);
395 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
396 struct lpfc_iocbq *pIocbOut)
398 struct lpfc_scsi_buf *lpfc_cmd =
399 (struct lpfc_scsi_buf *) pIocbIn->context1;
400 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
401 struct lpfc_nodelist *pnode = rdata->pnode;
402 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
404 struct scsi_device *sdev, *tmp_sdev;
407 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
408 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
410 if (lpfc_cmd->status) {
411 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
412 (lpfc_cmd->result & IOERR_DRVR_MASK))
413 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
414 else if (lpfc_cmd->status >= IOSTAT_CNT)
415 lpfc_cmd->status = IOSTAT_DEFAULT;
417 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
418 "%d:0729 FCP cmd x%x failed <%d/%d> status: "
419 "x%x result: x%x Data: x%x x%x\n",
420 phba->brd_no, cmd->cmnd[0], cmd->device->id,
421 cmd->device->lun, lpfc_cmd->status,
422 lpfc_cmd->result, pIocbOut->iocb.ulpContext,
423 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
425 switch (lpfc_cmd->status) {
426 case IOSTAT_FCP_RSP_ERROR:
427 /* Call FCP RSP handler to determine result */
428 lpfc_handle_fcp_err(lpfc_cmd);
430 case IOSTAT_NPORT_BSY:
431 case IOSTAT_FABRIC_BSY:
432 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
435 cmd->result = ScsiResult(DID_ERROR, 0);
440 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
441 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
443 cmd->result = ScsiResult(DID_OK, 0);
446 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
447 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
449 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
450 "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
451 "SNS x%x x%x Data: x%x x%x\n",
452 phba->brd_no, cmd->device->id,
453 cmd->device->lun, cmd, cmd->result,
454 *lp, *(lp + 3), cmd->retries, cmd->resid);
457 result = cmd->result;
461 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
462 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
463 lpfc_release_scsi_buf(phba, lpfc_cmd);
467 if (!result && pnode != NULL &&
468 ((jiffies - pnode->last_ramp_up_time) >
469 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
470 ((jiffies - pnode->last_q_full_time) >
471 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
472 (phba->cfg_lun_queue_depth > sdev->queue_depth)) {
473 shost_for_each_device(tmp_sdev, sdev->host) {
474 if (phba->cfg_lun_queue_depth > tmp_sdev->queue_depth) {
475 if (tmp_sdev->id != sdev->id)
477 if (tmp_sdev->ordered_tags)
478 scsi_adjust_queue_depth(tmp_sdev,
480 tmp_sdev->queue_depth+1);
482 scsi_adjust_queue_depth(tmp_sdev,
484 tmp_sdev->queue_depth+1);
486 pnode->last_ramp_up_time = jiffies;
492 * Check for queue full. If the lun is reporting queue full, then
493 * back off the lun queue depth to prevent target overloads.
495 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
496 pnode->last_q_full_time = jiffies;
498 shost_for_each_device(tmp_sdev, sdev->host) {
499 if (tmp_sdev->id != sdev->id)
501 depth = scsi_track_queue_full(tmp_sdev,
502 tmp_sdev->queue_depth - 1);
505 * The queue depth cannot be lowered any more.
506 * Modify the returned error code to store
507 * the final depth value set by
508 * scsi_track_queue_full.
511 depth = sdev->host->cmd_per_lun;
514 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
515 "%d:0711 detected queue full - lun queue depth "
516 " adjusted to %d.\n", phba->brd_no, depth);
520 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
521 lpfc_release_scsi_buf(phba, lpfc_cmd);
525 lpfc_scsi_prep_cmnd(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd,
526 struct lpfc_nodelist *pnode)
528 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
529 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
530 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
531 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
532 int datadir = scsi_cmnd->sc_data_direction;
534 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
535 /* clear task management bits */
536 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
538 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
539 &lpfc_cmd->fcp_cmnd->fcp_lun);
541 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
543 if (scsi_cmnd->device->tagged_supported) {
544 switch (scsi_cmnd->tag) {
545 case HEAD_OF_QUEUE_TAG:
546 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
548 case ORDERED_QUEUE_TAG:
549 fcp_cmnd->fcpCntl1 = ORDERED_Q;
552 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
556 fcp_cmnd->fcpCntl1 = 0;
559 * There are three possibilities here - use scatter-gather segment, use
560 * the single mapping, or neither. Start the lpfc command prep by
561 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
564 if (scsi_cmnd->use_sg) {
565 if (datadir == DMA_TO_DEVICE) {
566 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
567 iocb_cmd->un.fcpi.fcpi_parm = 0;
569 fcp_cmnd->fcpCntl3 = WRITE_DATA;
570 phba->fc4OutputRequests++;
572 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
573 iocb_cmd->ulpPU = PARM_READ_CHECK;
574 iocb_cmd->un.fcpi.fcpi_parm =
575 scsi_cmnd->request_bufflen;
576 fcp_cmnd->fcpCntl3 = READ_DATA;
577 phba->fc4InputRequests++;
579 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
580 if (datadir == DMA_TO_DEVICE) {
581 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
582 iocb_cmd->un.fcpi.fcpi_parm = 0;
584 fcp_cmnd->fcpCntl3 = WRITE_DATA;
585 phba->fc4OutputRequests++;
587 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
588 iocb_cmd->ulpPU = PARM_READ_CHECK;
589 iocb_cmd->un.fcpi.fcpi_parm =
590 scsi_cmnd->request_bufflen;
591 fcp_cmnd->fcpCntl3 = READ_DATA;
592 phba->fc4InputRequests++;
595 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
596 iocb_cmd->un.fcpi.fcpi_parm = 0;
598 fcp_cmnd->fcpCntl3 = 0;
599 phba->fc4ControlRequests++;
603 * Finish initializing those IOCB fields that are independent
604 * of the scsi_cmnd request_buffer
606 piocbq->iocb.ulpContext = pnode->nlp_rpi;
607 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
608 piocbq->iocb.ulpFCP2Rcvy = 1;
610 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
611 piocbq->context1 = lpfc_cmd;
612 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
613 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
617 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
618 struct lpfc_scsi_buf *lpfc_cmd,
620 uint8_t task_mgmt_cmd)
622 struct lpfc_sli *psli;
623 struct lpfc_iocbq *piocbq;
625 struct fcp_cmnd *fcp_cmnd;
626 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
627 struct lpfc_nodelist *ndlp = rdata->pnode;
629 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
634 piocbq = &(lpfc_cmd->cur_iocbq);
635 piocb = &piocbq->iocb;
637 fcp_cmnd = lpfc_cmd->fcp_cmnd;
638 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
639 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
641 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
643 piocb->ulpContext = ndlp->nlp_rpi;
644 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
645 piocb->ulpFCP2Rcvy = 1;
647 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
649 /* ulpTimeout is only one byte */
650 if (lpfc_cmd->timeout > 0xff) {
652 * Do not timeout the command at the firmware level.
653 * The driver will provide the timeout mechanism.
655 piocb->ulpTimeout = 0;
657 piocb->ulpTimeout = lpfc_cmd->timeout;
664 lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba,
665 unsigned tgt_id, unsigned int lun,
666 struct lpfc_rport_data *rdata)
668 struct lpfc_iocbq *iocbq;
669 struct lpfc_iocbq *iocbqrsp;
672 lpfc_cmd->rdata = rdata;
673 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, lun,
678 lpfc_cmd->scsi_hba = phba;
679 iocbq = &lpfc_cmd->cur_iocbq;
680 iocbqrsp = lpfc_sli_get_iocbq(phba);
685 /* Issue Target Reset to TGT <num> */
686 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
687 "%d:0702 Issue Target Reset to TGT %d "
689 phba->brd_no, tgt_id, rdata->pnode->nlp_rpi,
690 rdata->pnode->nlp_flag);
692 ret = lpfc_sli_issue_iocb_wait(phba,
693 &phba->sli.ring[phba->sli.fcp_ring],
694 iocbq, iocbqrsp, lpfc_cmd->timeout);
695 if (ret != IOCB_SUCCESS) {
696 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
700 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
701 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
702 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
703 (lpfc_cmd->result & IOERR_DRVR_MASK))
704 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
707 lpfc_sli_release_iocbq(phba, iocbqrsp);
712 lpfc_info(struct Scsi_Host *host)
714 struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata;
716 static char lpfcinfobuf[384];
718 memset(lpfcinfobuf,0,384);
719 if (phba && phba->pcidev){
720 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
721 len = strlen(lpfcinfobuf);
722 snprintf(lpfcinfobuf + len,
724 " on PCI bus %02x device %02x irq %d",
725 phba->pcidev->bus->number,
728 len = strlen(lpfcinfobuf);
730 snprintf(lpfcinfobuf + len,
739 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
741 unsigned long poll_tmo_expires =
742 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
744 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
745 mod_timer(&phba->fcp_poll_timer,
749 void lpfc_poll_start_timer(struct lpfc_hba * phba)
751 lpfc_poll_rearm_timer(phba);
754 void lpfc_poll_timeout(unsigned long ptr)
756 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
759 spin_lock_irqsave(phba->host->host_lock, iflag);
761 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
762 lpfc_sli_poll_fcp_ring (phba);
763 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
764 lpfc_poll_rearm_timer(phba);
767 spin_unlock_irqrestore(phba->host->host_lock, iflag);
771 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
773 struct lpfc_hba *phba =
774 (struct lpfc_hba *) cmnd->device->host->hostdata;
775 struct lpfc_sli *psli = &phba->sli;
776 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
777 struct lpfc_nodelist *ndlp = rdata->pnode;
778 struct lpfc_scsi_buf *lpfc_cmd;
779 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
782 err = fc_remote_port_chkready(rport);
785 goto out_fail_command;
789 * Catch race where our node has transitioned, but the
790 * transport is still transitioning.
793 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
794 goto out_fail_command;
796 lpfc_cmd = lpfc_get_scsi_buf (phba);
797 if (lpfc_cmd == NULL) {
798 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
799 "%d:0707 driver's buffer pool is empty, "
800 "IO busied\n", phba->brd_no);
805 * Store the midlayer's command structure for the completion phase
806 * and complete the command initialization.
808 lpfc_cmd->pCmd = cmnd;
809 lpfc_cmd->rdata = rdata;
810 lpfc_cmd->timeout = 0;
811 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
812 cmnd->scsi_done = done;
814 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
816 goto out_host_busy_free_buf;
818 lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
820 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
821 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
823 goto out_host_busy_free_buf;
825 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
826 lpfc_sli_poll_fcp_ring(phba);
827 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
828 lpfc_poll_rearm_timer(phba);
833 out_host_busy_free_buf:
834 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
835 lpfc_release_scsi_buf(phba, lpfc_cmd);
837 return SCSI_MLQUEUE_HOST_BUSY;
846 lpfc_abort_handler(struct scsi_cmnd *cmnd)
848 struct Scsi_Host *shost = cmnd->device->host;
849 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
850 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
851 struct lpfc_iocbq *iocb;
852 struct lpfc_iocbq *abtsiocb;
853 struct lpfc_scsi_buf *lpfc_cmd;
855 unsigned int loop_count = 0;
858 spin_lock_irq(shost->host_lock);
860 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
864 * If pCmd field of the corresponding lpfc_scsi_buf structure
865 * points to a different SCSI command, then the driver has
866 * already completed this command, but the midlayer did not
867 * see the completion before the eh fired. Just return
870 iocb = &lpfc_cmd->cur_iocbq;
871 if (lpfc_cmd->pCmd != cmnd)
874 BUG_ON(iocb->context1 != lpfc_cmd);
876 abtsiocb = lpfc_sli_get_iocbq(phba);
877 if (abtsiocb == NULL) {
883 * The scsi command can not be in txq and it is in flight because the
884 * pCmd is still pointig at the SCSI command we have to abort. There
885 * is no need to search the txcmplq. Just send an abort to the FW.
889 icmd = &abtsiocb->iocb;
890 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
891 icmd->un.acxri.abortContextTag = cmd->ulpContext;
892 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
895 icmd->ulpClass = cmd->ulpClass;
896 if (phba->hba_state >= LPFC_LINK_UP)
897 icmd->ulpCommand = CMD_ABORT_XRI_CN;
899 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
901 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
902 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
903 lpfc_sli_release_iocbq(phba, abtsiocb);
908 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
909 lpfc_sli_poll_fcp_ring (phba);
911 /* Wait for abort to complete */
912 while (lpfc_cmd->pCmd == cmnd)
914 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
915 lpfc_sli_poll_fcp_ring (phba);
917 spin_unlock_irq(phba->host->host_lock);
918 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT*HZ);
919 spin_lock_irq(phba->host->host_lock);
921 > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
925 if (lpfc_cmd->pCmd == cmnd) {
927 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
928 "%d:0748 abort handler timed out waiting for "
929 "abort to complete: ret %#x, ID %d, LUN %d, "
931 phba->brd_no, ret, cmnd->device->id,
932 cmnd->device->lun, cmnd->serial_number);
936 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
937 "%d:0749 SCSI Layer I/O Abort Request "
938 "Status x%x ID %d LUN %d snum %#lx\n",
939 phba->brd_no, ret, cmnd->device->id,
940 cmnd->device->lun, cmnd->serial_number);
942 spin_unlock_irq(shost->host_lock);
948 lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
950 struct Scsi_Host *shost = cmnd->device->host;
951 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
952 struct lpfc_scsi_buf *lpfc_cmd;
953 struct lpfc_iocbq *iocbq, *iocbqrsp;
954 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
955 struct lpfc_nodelist *pnode = rdata->pnode;
956 uint32_t cmd_result = 0, cmd_status = 0;
960 spin_lock_irq(shost->host_lock);
962 * If target is not in a MAPPED state, delay the reset until
963 * target is rediscovered or nodev timeout expires.
969 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
970 spin_unlock_irq(phba->host->host_lock);
971 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
972 spin_lock_irq(phba->host->host_lock);
974 if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
978 lpfc_cmd = lpfc_get_scsi_buf (phba);
979 if (lpfc_cmd == NULL)
982 lpfc_cmd->timeout = 60;
983 lpfc_cmd->scsi_hba = phba;
984 lpfc_cmd->rdata = rdata;
986 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, cmnd->device->lun,
989 goto out_free_scsi_buf;
991 iocbq = &lpfc_cmd->cur_iocbq;
993 /* get a buffer for this IOCB command response */
994 iocbqrsp = lpfc_sli_get_iocbq(phba);
995 if (iocbqrsp == NULL)
996 goto out_free_scsi_buf;
998 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
999 "%d:0703 Issue LUN Reset to TGT %d LUN %d "
1000 "Data: x%x x%x\n", phba->brd_no, cmnd->device->id,
1001 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
1003 ret = lpfc_sli_issue_iocb_wait(phba,
1004 &phba->sli.ring[phba->sli.fcp_ring],
1005 iocbq, iocbqrsp, lpfc_cmd->timeout);
1006 if (ret == IOCB_SUCCESS)
1010 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1011 cmd_status = iocbqrsp->iocb.ulpStatus;
1013 lpfc_sli_release_iocbq(phba, iocbqrsp);
1016 * All outstanding txcmplq I/Os should have been aborted by the device.
1017 * Unfortunately, some targets do not abide by this forcing the driver
1020 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1021 cmnd->device->id, cmnd->device->lun,
1024 lpfc_sli_abort_iocb(phba,
1025 &phba->sli.ring[phba->sli.fcp_ring],
1026 cmnd->device->id, cmnd->device->lun,
1030 spin_unlock_irq(phba->host->host_lock);
1031 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
1032 spin_lock_irq(phba->host->host_lock);
1035 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1038 cnt = lpfc_sli_sum_iocb(phba,
1039 &phba->sli.ring[phba->sli.fcp_ring],
1040 cmnd->device->id, cmnd->device->lun,
1045 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1046 "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1052 lpfc_release_scsi_buf(phba, lpfc_cmd);
1054 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1055 "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1056 "Data: x%x x%x x%x\n",
1057 phba->brd_no, cmnd->device->id,cmnd->device->lun,
1058 ret, cmd_status, cmd_result);
1061 spin_unlock_irq(shost->host_lock);
1066 lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
1068 struct Scsi_Host *shost = cmnd->device->host;
1069 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
1070 struct lpfc_nodelist *ndlp = NULL;
1072 int ret = FAILED, i, err_count = 0;
1074 struct lpfc_scsi_buf * lpfc_cmd;
1076 spin_lock_irq(shost->host_lock);
1078 lpfc_cmd = lpfc_get_scsi_buf(phba);
1079 if (lpfc_cmd == NULL)
1082 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1083 lpfc_cmd->timeout = 60;
1084 lpfc_cmd->scsi_hba = phba;
1087 * Since the driver manages a single bus device, reset all
1088 * targets known to the driver. Should any target reset
1089 * fail, this routine returns failure to the midlayer.
1091 for (i = 0; i < LPFC_MAX_TARGET; i++) {
1092 /* Search the mapped list for this target ID */
1094 list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1095 if ((i == ndlp->nlp_sid) && ndlp->rport) {
1103 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba, i, cmnd->device->lun,
1104 ndlp->rport->dd_data);
1105 if (ret != SUCCESS) {
1106 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1107 "%d:0713 Bus Reset on target %d failed\n",
1116 lpfc_release_scsi_buf(phba, lpfc_cmd);
1119 * All outstanding txcmplq I/Os should have been aborted by
1120 * the targets. Unfortunately, some targets do not abide by
1121 * this forcing the driver to double check.
1123 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1124 0, 0, LPFC_CTX_HOST);
1126 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1127 0, 0, 0, LPFC_CTX_HOST);
1130 spin_unlock_irq(phba->host->host_lock);
1131 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
1132 spin_lock_irq(phba->host->host_lock);
1135 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1138 cnt = lpfc_sli_sum_iocb(phba,
1139 &phba->sli.ring[phba->sli.fcp_ring],
1140 0, 0, LPFC_CTX_HOST);
1144 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1145 "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1146 phba->brd_no, cnt, i);
1150 lpfc_printf_log(phba,
1153 "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1156 spin_unlock_irq(shost->host_lock);
1161 lpfc_slave_alloc(struct scsi_device *sdev)
1163 struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata;
1164 struct lpfc_scsi_buf *scsi_buf = NULL;
1165 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1166 uint32_t total = 0, i;
1167 uint32_t num_to_alloc = 0;
1168 unsigned long flags;
1170 if (!rport || fc_remote_port_chkready(rport))
1173 sdev->hostdata = rport->dd_data;
1176 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1177 * available list of scsi buffers. Don't allocate more than the
1178 * HBA limit conveyed to the midlayer via the host structure. The
1179 * formula accounts for the lun_queue_depth + error handlers + 1
1180 * extra. This list of scsi bufs exists for the lifetime of the driver.
1182 total = phba->total_scsi_bufs;
1183 num_to_alloc = phba->cfg_lun_queue_depth + 2;
1184 if (total >= phba->cfg_hba_queue_depth) {
1185 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1186 "%d:0704 At limitation of %d preallocated "
1187 "command buffers\n", phba->brd_no, total);
1189 } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
1190 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1191 "%d:0705 Allocation request of %d command "
1192 "buffers will exceed max of %d. Reducing "
1193 "allocation request to %d.\n", phba->brd_no,
1194 num_to_alloc, phba->cfg_hba_queue_depth,
1195 (phba->cfg_hba_queue_depth - total));
1196 num_to_alloc = phba->cfg_hba_queue_depth - total;
1199 for (i = 0; i < num_to_alloc; i++) {
1200 scsi_buf = lpfc_new_scsi_buf(phba);
1202 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1203 "%d:0706 Failed to allocate command "
1204 "buffer\n", phba->brd_no);
1208 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
1209 phba->total_scsi_bufs++;
1210 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
1211 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
1217 lpfc_slave_configure(struct scsi_device *sdev)
1219 struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata;
1220 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1222 if (sdev->tagged_supported)
1223 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1225 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1228 * Initialize the fc transport attributes for the target
1229 * containing this scsi device. Also note that the driver's
1230 * target pointer is stored in the starget_data for the
1231 * driver's sysfs entry point functions.
1233 rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
1235 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1236 lpfc_sli_poll_fcp_ring(phba);
1237 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1238 lpfc_poll_rearm_timer(phba);
1245 lpfc_slave_destroy(struct scsi_device *sdev)
1247 sdev->hostdata = NULL;
1251 struct scsi_host_template lpfc_template = {
1252 .module = THIS_MODULE,
1253 .name = LPFC_DRIVER_NAME,
1255 .queuecommand = lpfc_queuecommand,
1256 .eh_abort_handler = lpfc_abort_handler,
1257 .eh_device_reset_handler= lpfc_reset_lun_handler,
1258 .eh_bus_reset_handler = lpfc_reset_bus_handler,
1259 .slave_alloc = lpfc_slave_alloc,
1260 .slave_configure = lpfc_slave_configure,
1261 .slave_destroy = lpfc_slave_destroy,
1263 .sg_tablesize = LPFC_SG_SEG_CNT,
1264 .cmd_per_lun = LPFC_CMD_PER_LUN,
1265 .use_clustering = ENABLE_CLUSTERING,
1266 .shost_attrs = lpfc_host_attrs,
1267 .max_sectors = 0xFFFF,