blob: d45616e70c67ba1479e85ba8b4d9038e2b56e11d [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -05004 * Copyright (C) 2004-2006 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * 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. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/pci.h>
23#include <linux/interrupt.h>
24
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>
30
31#include "lpfc_version.h"
32#include "lpfc_hw.h"
33#include "lpfc_sli.h"
34#include "lpfc_disc.h"
35#include "lpfc_scsi.h"
36#include "lpfc.h"
37#include "lpfc_logmsg.h"
38#include "lpfc_crtn.h"
39
40#define LPFC_RESET_WAIT 2
41#define LPFC_ABORT_WAIT 2
42
dea31012005-04-17 16:05:31 -050043
44/*
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.
51 */
52static struct lpfc_scsi_buf *
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040053lpfc_new_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -050054{
55 struct lpfc_scsi_buf *psb;
56 struct ulp_bde64 *bpl;
57 IOCB_t *iocb;
58 dma_addr_t pdma_phys;
James Bottomley604a3e32005-10-29 10:28:33 -050059 uint16_t iotag;
dea31012005-04-17 16:05:31 -050060
61 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
62 if (!psb)
63 return NULL;
64 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
65 psb->scsi_hba = phba;
66
67 /*
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
71 * sg_tablesize.
72 */
73 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
74 &psb->dma_handle);
75 if (!psb->data) {
76 kfree(psb);
77 return NULL;
78 }
79
80 /* Initialize virtual ptrs to dma_buf region. */
81 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
82
James Bottomley604a3e32005-10-29 10:28:33 -050083 /* Allocate iotag for psb->cur_iocbq. */
84 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
85 if (iotag == 0) {
86 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
87 psb->data, psb->dma_handle);
88 kfree (psb);
89 return NULL;
90 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040091 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -050092
dea31012005-04-17 16:05:31 -050093 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);
97
98 /* Initialize local short-hand pointers. */
99 bpl = psb->fcp_bpl;
100 pdma_phys = psb->dma_handle;
101
102 /*
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
105 * queuecommand.
106 */
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);
112 bpl++;
113
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);
121
122 /*
123 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
124 * initialize it with all known data now.
125 */
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;
135
136 return psb;
137}
138
Adrian Bunk455c53e2006-01-06 20:21:28 +0100139static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500140lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500141{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400142 struct lpfc_scsi_buf * lpfc_cmd = NULL;
143 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500144 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500145
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500146 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400147 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500148 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400149 return lpfc_cmd;
150}
151
152static void
153lpfc_release_scsi_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
154{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500155 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500156
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500157 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400158 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500159 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500160 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500161}
162
163static int
164lpfc_scsi_prep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
165{
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;
171 dma_addr_t physaddr;
172 uint32_t i, num_bde = 0;
173 int datadir = scsi_cmnd->sc_data_direction;
174 int dma_error;
175
176 /*
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
180 * data bde entry.
181 */
182 bpl += 2;
183 if (scsi_cmnd->use_sg) {
184 /*
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.
189 */
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)
194 return 1;
195
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,
200 lpfc_cmd->seg_cnt);
201 dma_unmap_sg(&phba->pcidev->dev, sgel,
202 lpfc_cmd->seg_cnt, datadir);
203 return 1;
204 }
205
206 /*
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
210 * the bde's.
211 */
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;
219 else
220 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
221 bpl->tus.w = le32_to_cpu(bpl->tus.w);
222 bpl++;
223 sgel++;
224 num_bde++;
225 }
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,
230 datadir);
231 dma_error = dma_mapping_error(physaddr);
232 if (dma_error) {
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);
237 return 1;
238 }
239
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;
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400246 else
247 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
dea31012005-04-17 16:05:31 -0500248 bpl->tus.w = le32_to_cpu(bpl->tus.w);
249 num_bde = 1;
250 bpl++;
251 }
252
253 /*
254 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400255 * 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.
dea31012005-04-17 16:05:31 -0500258 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400259 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500260 iocb_cmd->un.fcpi64.bdl.bdeSize +=
261 (num_bde * sizeof (struct ulp_bde64));
262 iocb_cmd->ulpBdeCount = 1;
263 iocb_cmd->ulpLe = 1;
264 fcp_cmnd->fcpDl = be32_to_cpu(scsi_cmnd->request_bufflen);
265 return 0;
266}
267
268static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400269lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
270{
271 /*
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.
276 */
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);
280 } else {
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);
285 }
286 }
287}
288
289static void
dea31012005-04-17 16:05:31 -0500290lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
291{
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;
300 uint32_t rsplen = 0;
301
302 /*
303 * If this is a task management command, there is no
304 * scsi packet associated with this lpfc_cmd. The driver
305 * consumes it.
306 */
307 if (fcpcmd->fcpCntl2) {
308 scsi_status = 0;
309 goto out;
310 }
311
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),
319 fcprsp->rspInfo3);
320
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;
326 goto out;
327 }
328 }
329
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;
334
335 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
336 }
337
338 cmnd->resid = 0;
339 if (resp_info & RESID_UNDER) {
340 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
341
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);
347
348 /*
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.
353 */
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);
363
364 host_status = DID_ERROR;
365 }
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;
373
374 /*
375 * Check SLI validation that all the transfer was actually done
376 * (fcpi_parm should be zero). Apply check only to reads.
377 */
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;
388 }
389
390 out:
391 cmnd->result = ScsiResult(host_status, scsi_status);
392}
393
394static void
395lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
396 struct lpfc_iocbq *pIocbOut)
397{
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;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500403 int result;
404 struct scsi_device *sdev, *tmp_sdev;
405 int depth = 0;
dea31012005-04-17 16:05:31 -0500406
407 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
408 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
409
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;
416
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);
424
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);
429 break;
430 case IOSTAT_NPORT_BSY:
431 case IOSTAT_FABRIC_BSY:
432 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
433 break;
434 default:
435 cmd->result = ScsiResult(DID_ERROR, 0);
436 break;
437 }
438
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400439 if ((pnode == NULL )
440 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
441 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500442 } else {
443 cmd->result = ScsiResult(DID_OK, 0);
444 }
445
446 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
447 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
448
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);
455 }
456
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500457 result = cmd->result;
458 sdev = cmd->device;
dea31012005-04-17 16:05:31 -0500459 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400460
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500461 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartbcf4dbf2006-07-06 15:50:08 -0400462 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500463 lpfc_release_scsi_buf(phba, lpfc_cmd);
464 return;
465 }
466
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500467 if (!result && pnode != NULL &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500468 ((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)
476 continue;
477 if (tmp_sdev->ordered_tags)
478 scsi_adjust_queue_depth(tmp_sdev,
479 MSG_ORDERED_TAG,
480 tmp_sdev->queue_depth+1);
481 else
482 scsi_adjust_queue_depth(tmp_sdev,
483 MSG_SIMPLE_TAG,
484 tmp_sdev->queue_depth+1);
485
486 pnode->last_ramp_up_time = jiffies;
487 }
488 }
489 }
490
491 /*
492 * Check for queue full. If the lun is reporting queue full, then
493 * back off the lun queue depth to prevent target overloads.
494 */
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500495 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500496 pnode->last_q_full_time = jiffies;
497
498 shost_for_each_device(tmp_sdev, sdev->host) {
499 if (tmp_sdev->id != sdev->id)
500 continue;
501 depth = scsi_track_queue_full(tmp_sdev,
502 tmp_sdev->queue_depth - 1);
503 }
504 /*
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.
509 */
510 if (depth == -1)
511 depth = sdev->host->cmd_per_lun;
512
513 if (depth) {
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);
517 }
518 }
519
James Smartbcf4dbf2006-07-06 15:50:08 -0400520 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400521 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500522}
523
524static void
525lpfc_scsi_prep_cmnd(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd,
526 struct lpfc_nodelist *pnode)
527{
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;
533
534 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400535 /* clear task management bits */
536 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500537
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400538 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
539 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500540
541 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
542
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;
547 break;
548 case ORDERED_QUEUE_TAG:
549 fcp_cmnd->fcpCntl1 = ORDERED_Q;
550 break;
551 default:
552 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
553 break;
554 }
555 } else
556 fcp_cmnd->fcpCntl1 = 0;
557
558 /*
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
562 * data bde entry.
563 */
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;
568 iocb_cmd->ulpPU = 0;
569 fcp_cmnd->fcpCntl3 = WRITE_DATA;
570 phba->fc4OutputRequests++;
571 } else {
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++;
578 }
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;
583 iocb_cmd->ulpPU = 0;
584 fcp_cmnd->fcpCntl3 = WRITE_DATA;
585 phba->fc4OutputRequests++;
586 } else {
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++;
593 }
594 } else {
595 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
596 iocb_cmd->un.fcpi.fcpi_parm = 0;
597 iocb_cmd->ulpPU = 0;
598 fcp_cmnd->fcpCntl3 = 0;
599 phba->fc4ControlRequests++;
600 }
601
602 /*
603 * Finish initializing those IOCB fields that are independent
604 * of the scsi_cmnd request_buffer
605 */
606 piocbq->iocb.ulpContext = pnode->nlp_rpi;
607 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
608 piocbq->iocb.ulpFCP2Rcvy = 1;
609
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;
614}
615
616static int
617lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
618 struct lpfc_scsi_buf *lpfc_cmd,
619 uint8_t task_mgmt_cmd)
620{
621 struct lpfc_sli *psli;
622 struct lpfc_iocbq *piocbq;
623 IOCB_t *piocb;
624 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -0400625 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -0500626 struct lpfc_nodelist *ndlp = rdata->pnode;
627
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400628 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea31012005-04-17 16:05:31 -0500629 return 0;
630 }
631
632 psli = &phba->sli;
633 piocbq = &(lpfc_cmd->cur_iocbq);
634 piocb = &piocbq->iocb;
635
636 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400637 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
638 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500639 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
640
641 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
642
643 piocb->ulpContext = ndlp->nlp_rpi;
644 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
645 piocb->ulpFCP2Rcvy = 1;
646 }
647 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
648
649 /* ulpTimeout is only one byte */
650 if (lpfc_cmd->timeout > 0xff) {
651 /*
652 * Do not timeout the command at the firmware level.
653 * The driver will provide the timeout mechanism.
654 */
655 piocb->ulpTimeout = 0;
656 } else {
657 piocb->ulpTimeout = lpfc_cmd->timeout;
658 }
659
dea31012005-04-17 16:05:31 -0500660 return (1);
661}
662
663static int
James Smart0b18ac42006-05-01 21:50:40 -0400664lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba,
665 unsigned tgt_id, struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -0500666{
667 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400668 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500669 int ret;
670
James Smart0b18ac42006-05-01 21:50:40 -0400671 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -0500672 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_TARGET_RESET);
673 if (!ret)
674 return FAILED;
675
676 lpfc_cmd->scsi_hba = phba;
677 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400678 iocbqrsp = lpfc_sli_get_iocbq(phba);
679
dea31012005-04-17 16:05:31 -0500680 if (!iocbqrsp)
681 return FAILED;
dea31012005-04-17 16:05:31 -0500682
James Smart0b18ac42006-05-01 21:50:40 -0400683 /* Issue Target Reset to TGT <num> */
684 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
685 "%d:0702 Issue Target Reset to TGT %d "
686 "Data: x%x x%x\n",
687 phba->brd_no, tgt_id, rdata->pnode->nlp_rpi,
688 rdata->pnode->nlp_flag);
689
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400690 ret = lpfc_sli_issue_iocb_wait(phba,
691 &phba->sli.ring[phba->sli.fcp_ring],
692 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500693 if (ret != IOCB_SUCCESS) {
694 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
695 ret = FAILED;
696 } else {
697 ret = SUCCESS;
698 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
699 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
700 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
701 (lpfc_cmd->result & IOERR_DRVR_MASK))
702 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
703 }
704
James Bottomley604a3e32005-10-29 10:28:33 -0500705 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500706 return ret;
707}
708
dea31012005-04-17 16:05:31 -0500709const char *
710lpfc_info(struct Scsi_Host *host)
711{
Jamie Wellnitz7f0b5b12006-02-28 19:25:24 -0500712 struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata;
dea31012005-04-17 16:05:31 -0500713 int len;
714 static char lpfcinfobuf[384];
715
716 memset(lpfcinfobuf,0,384);
717 if (phba && phba->pcidev){
718 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
719 len = strlen(lpfcinfobuf);
720 snprintf(lpfcinfobuf + len,
721 384-len,
722 " on PCI bus %02x device %02x irq %d",
723 phba->pcidev->bus->number,
724 phba->pcidev->devfn,
725 phba->pcidev->irq);
726 len = strlen(lpfcinfobuf);
727 if (phba->Port[0]) {
728 snprintf(lpfcinfobuf + len,
729 384-len,
730 " port %s",
731 phba->Port);
732 }
733 }
734 return lpfcinfobuf;
735}
736
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500737static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
738{
739 unsigned long poll_tmo_expires =
740 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
741
742 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
743 mod_timer(&phba->fcp_poll_timer,
744 poll_tmo_expires);
745}
746
747void lpfc_poll_start_timer(struct lpfc_hba * phba)
748{
749 lpfc_poll_rearm_timer(phba);
750}
751
752void lpfc_poll_timeout(unsigned long ptr)
753{
754 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
755 unsigned long iflag;
756
757 spin_lock_irqsave(phba->host->host_lock, iflag);
758
759 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
760 lpfc_sli_poll_fcp_ring (phba);
761 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
762 lpfc_poll_rearm_timer(phba);
763 }
764
765 spin_unlock_irqrestore(phba->host->host_lock, iflag);
766}
767
dea31012005-04-17 16:05:31 -0500768static int
769lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
770{
771 struct lpfc_hba *phba =
Jamie Wellnitz7f0b5b12006-02-28 19:25:24 -0500772 (struct lpfc_hba *) cmnd->device->host->hostdata;
dea31012005-04-17 16:05:31 -0500773 struct lpfc_sli *psli = &phba->sli;
774 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
775 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400776 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400777 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400778 int err;
dea31012005-04-17 16:05:31 -0500779
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400780 err = fc_remote_port_chkready(rport);
781 if (err) {
782 cmnd->result = err;
dea31012005-04-17 16:05:31 -0500783 goto out_fail_command;
784 }
785
786 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400787 * Catch race where our node has transitioned, but the
788 * transport is still transitioning.
dea31012005-04-17 16:05:31 -0500789 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400790 if (!ndlp) {
791 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
792 goto out_fail_command;
dea31012005-04-17 16:05:31 -0500793 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500794 lpfc_cmd = lpfc_get_scsi_buf (phba);
dea31012005-04-17 16:05:31 -0500795 if (lpfc_cmd == NULL) {
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500796 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
797 "%d:0707 driver's buffer pool is empty, "
798 "IO busied\n", phba->brd_no);
dea31012005-04-17 16:05:31 -0500799 goto out_host_busy;
800 }
801
802 /*
803 * Store the midlayer's command structure for the completion phase
804 * and complete the command initialization.
805 */
806 lpfc_cmd->pCmd = cmnd;
807 lpfc_cmd->rdata = rdata;
808 lpfc_cmd->timeout = 0;
809 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
810 cmnd->scsi_done = done;
811
812 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
813 if (err)
814 goto out_host_busy_free_buf;
815
816 lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
817
818 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
819 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
820 if (err)
821 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500822
823 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
824 lpfc_sli_poll_fcp_ring(phba);
825 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
826 lpfc_poll_rearm_timer(phba);
827 }
828
dea31012005-04-17 16:05:31 -0500829 return 0;
830
831 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -0400832 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400833 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500834 out_host_busy:
835 return SCSI_MLQUEUE_HOST_BUSY;
836
837 out_fail_command:
838 done(cmnd);
839 return 0;
840}
841
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500842
dea31012005-04-17 16:05:31 -0500843static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500844lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500845{
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500846 struct Scsi_Host *shost = cmnd->device->host;
Jamie Wellnitz7f0b5b12006-02-28 19:25:24 -0500847 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
dea31012005-04-17 16:05:31 -0500848 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400849 struct lpfc_iocbq *iocb;
850 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -0500851 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -0500852 IOCB_t *cmd, *icmd;
dea31012005-04-17 16:05:31 -0500853 unsigned int loop_count = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400854 int ret = SUCCESS;
855
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500856 spin_lock_irq(shost->host_lock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400857
858 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
859 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -0500860
861 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400862 * If pCmd field of the corresponding lpfc_scsi_buf structure
863 * points to a different SCSI command, then the driver has
864 * already completed this command, but the midlayer did not
865 * see the completion before the eh fired. Just return
866 * SUCCESS.
dea31012005-04-17 16:05:31 -0500867 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400868 iocb = &lpfc_cmd->cur_iocbq;
869 if (lpfc_cmd->pCmd != cmnd)
870 goto out;
dea31012005-04-17 16:05:31 -0500871
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400872 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -0500873
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400874 abtsiocb = lpfc_sli_get_iocbq(phba);
875 if (abtsiocb == NULL) {
876 ret = FAILED;
dea31012005-04-17 16:05:31 -0500877 goto out;
878 }
879
dea31012005-04-17 16:05:31 -0500880 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400881 * The scsi command can not be in txq and it is in flight because the
882 * pCmd is still pointig at the SCSI command we have to abort. There
883 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -0500884 */
dea31012005-04-17 16:05:31 -0500885
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400886 cmd = &iocb->iocb;
887 icmd = &abtsiocb->iocb;
888 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
889 icmd->un.acxri.abortContextTag = cmd->ulpContext;
890 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -0500891
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400892 icmd->ulpLe = 1;
893 icmd->ulpClass = cmd->ulpClass;
894 if (phba->hba_state >= LPFC_LINK_UP)
895 icmd->ulpCommand = CMD_ABORT_XRI_CN;
896 else
897 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -0500898
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400899 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
900 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
901 lpfc_sli_release_iocbq(phba, abtsiocb);
902 ret = FAILED;
903 goto out;
904 }
905
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500906 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
907 lpfc_sli_poll_fcp_ring (phba);
908
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400909 /* Wait for abort to complete */
910 while (lpfc_cmd->pCmd == cmnd)
911 {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500912 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
913 lpfc_sli_poll_fcp_ring (phba);
914
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400915 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800916 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT*HZ);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400917 spin_lock_irq(phba->host->host_lock);
918 if (++loop_count
919 > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
dea31012005-04-17 16:05:31 -0500920 break;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400921 }
dea31012005-04-17 16:05:31 -0500922
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400923 if (lpfc_cmd->pCmd == cmnd) {
924 ret = FAILED;
925 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
926 "%d:0748 abort handler timed out waiting for "
927 "abort to complete: ret %#x, ID %d, LUN %d, "
928 "snum %#lx\n",
929 phba->brd_no, ret, cmnd->device->id,
930 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -0500931 }
932
933 out:
934 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500935 "%d:0749 SCSI Layer I/O Abort Request "
936 "Status x%x ID %d LUN %d snum %#lx\n",
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400937 phba->brd_no, ret, cmnd->device->id,
938 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -0500939
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500940 spin_unlock_irq(shost->host_lock);
941
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400942 return ret;
dea31012005-04-17 16:05:31 -0500943}
944
945static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500946lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500947{
948 struct Scsi_Host *shost = cmnd->device->host;
Jamie Wellnitz7f0b5b12006-02-28 19:25:24 -0500949 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400950 struct lpfc_scsi_buf *lpfc_cmd;
951 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -0500952 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
953 struct lpfc_nodelist *pnode = rdata->pnode;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -0500954 uint32_t cmd_result = 0, cmd_status = 0;
dea31012005-04-17 16:05:31 -0500955 int ret = FAILED;
956 int cnt, loopcnt;
957
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500958 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500959 /*
960 * If target is not in a MAPPED state, delay the reset until
961 * target is rediscovered or nodev timeout expires.
962 */
963 while ( 1 ) {
964 if (!pnode)
965 break;
966
967 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
968 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800969 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
dea31012005-04-17 16:05:31 -0500970 spin_lock_irq(phba->host->host_lock);
971 }
972 if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
973 break;
974 }
975
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500976 lpfc_cmd = lpfc_get_scsi_buf (phba);
dea31012005-04-17 16:05:31 -0500977 if (lpfc_cmd == NULL)
978 goto out;
979
980 lpfc_cmd->pCmd = cmnd;
981 lpfc_cmd->timeout = 60;
982 lpfc_cmd->scsi_hba = phba;
James Smart0b18ac42006-05-01 21:50:40 -0400983 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -0500984
985 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
986 if (!ret)
987 goto out_free_scsi_buf;
988
989 iocbq = &lpfc_cmd->cur_iocbq;
990
991 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400992 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -0500993 if (iocbqrsp == NULL)
994 goto out_free_scsi_buf;
995
James Smart0b18ac42006-05-01 21:50:40 -0400996 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
997 "%d:0703 Issue LUN Reset to TGT %d LUN %d "
998 "Data: x%x x%x\n", phba->brd_no, cmnd->device->id,
999 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
1000
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001001 ret = lpfc_sli_issue_iocb_wait(phba,
1002 &phba->sli.ring[phba->sli.fcp_ring],
1003 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -05001004 if (ret == IOCB_SUCCESS)
1005 ret = SUCCESS;
1006
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001007
1008 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1009 cmd_status = iocbqrsp->iocb.ulpStatus;
1010
1011 lpfc_sli_release_iocbq(phba, iocbqrsp);
1012 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001013
1014 /*
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001015 * All outstanding txcmplq I/Os should have been aborted by the device.
dea31012005-04-17 16:05:31 -05001016 * Unfortunately, some targets do not abide by this forcing the driver
1017 * to double check.
1018 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001019 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1020 cmnd->device->id, cmnd->device->lun,
1021 LPFC_CTX_LUN);
1022 if (cnt)
1023 lpfc_sli_abort_iocb(phba,
1024 &phba->sli.ring[phba->sli.fcp_ring],
1025 cmnd->device->id, cmnd->device->lun,
1026 0, LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001027 loopcnt = 0;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001028 while(cnt) {
dea31012005-04-17 16:05:31 -05001029 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001030 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001031 spin_lock_irq(phba->host->host_lock);
1032
1033 if (++loopcnt
1034 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1035 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001036
1037 cnt = lpfc_sli_sum_iocb(phba,
1038 &phba->sli.ring[phba->sli.fcp_ring],
1039 cmnd->device->id, cmnd->device->lun,
1040 LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001041 }
1042
1043 if (cnt) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001044 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea31012005-04-17 16:05:31 -05001045 "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1046 phba->brd_no, cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001047 ret = FAILED;
dea31012005-04-17 16:05:31 -05001048 }
1049
dea31012005-04-17 16:05:31 -05001050out_free_scsi_buf:
1051 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1052 "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1053 "Data: x%x x%x x%x\n",
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001054 phba->brd_no, cmnd->device->id,cmnd->device->lun,
1055 ret, cmd_status, cmd_result);
1056
dea31012005-04-17 16:05:31 -05001057out:
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001058 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001059 return ret;
1060}
1061
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001062static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001063lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001064{
1065 struct Scsi_Host *shost = cmnd->device->host;
Jamie Wellnitz7f0b5b12006-02-28 19:25:24 -05001066 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
dea31012005-04-17 16:05:31 -05001067 struct lpfc_nodelist *ndlp = NULL;
1068 int match;
1069 int ret = FAILED, i, err_count = 0;
1070 int cnt, loopcnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001071 struct lpfc_scsi_buf * lpfc_cmd;
dea31012005-04-17 16:05:31 -05001072
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001073 spin_lock_irq(shost->host_lock);
1074
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001075 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001076 if (lpfc_cmd == NULL)
1077 goto out;
1078
1079 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1080 lpfc_cmd->timeout = 60;
1081 lpfc_cmd->pCmd = cmnd;
1082 lpfc_cmd->scsi_hba = phba;
1083
1084 /*
1085 * Since the driver manages a single bus device, reset all
1086 * targets known to the driver. Should any target reset
1087 * fail, this routine returns failure to the midlayer.
1088 */
James Smarte17da182006-07-06 15:49:25 -04001089 for (i = 0; i < LPFC_MAX_TARGET; i++) {
dea31012005-04-17 16:05:31 -05001090 /* Search the mapped list for this target ID */
1091 match = 0;
1092 list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1093 if ((i == ndlp->nlp_sid) && ndlp->rport) {
1094 match = 1;
1095 break;
1096 }
1097 }
1098 if (!match)
1099 continue;
1100
James Smart0b18ac42006-05-01 21:50:40 -04001101 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba,
1102 i, ndlp->rport->dd_data);
dea31012005-04-17 16:05:31 -05001103 if (ret != SUCCESS) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001104 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea31012005-04-17 16:05:31 -05001105 "%d:0713 Bus Reset on target %d failed\n",
1106 phba->brd_no, i);
1107 err_count++;
1108 }
1109 }
1110
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001111 if (err_count == 0)
1112 ret = SUCCESS;
1113
1114 lpfc_release_scsi_buf(phba, lpfc_cmd);
1115
1116 /*
1117 * All outstanding txcmplq I/Os should have been aborted by
1118 * the targets. Unfortunately, some targets do not abide by
1119 * this forcing the driver to double check.
1120 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001121 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1122 0, 0, LPFC_CTX_HOST);
1123 if (cnt)
1124 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1125 0, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001126 loopcnt = 0;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001127 while(cnt) {
dea31012005-04-17 16:05:31 -05001128 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001129 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001130 spin_lock_irq(phba->host->host_lock);
1131
1132 if (++loopcnt
1133 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1134 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001135
1136 cnt = lpfc_sli_sum_iocb(phba,
1137 &phba->sli.ring[phba->sli.fcp_ring],
1138 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001139 }
1140
1141 if (cnt) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001142 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea31012005-04-17 16:05:31 -05001143 "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1144 phba->brd_no, cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001145 ret = FAILED;
dea31012005-04-17 16:05:31 -05001146 }
1147
dea31012005-04-17 16:05:31 -05001148 lpfc_printf_log(phba,
1149 KERN_ERR,
1150 LOG_FCP,
1151 "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1152 phba->brd_no, ret);
1153out:
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001154 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001155 return ret;
1156}
1157
1158static int
dea31012005-04-17 16:05:31 -05001159lpfc_slave_alloc(struct scsi_device *sdev)
1160{
Jamie Wellnitz7f0b5b12006-02-28 19:25:24 -05001161 struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata;
dea31012005-04-17 16:05:31 -05001162 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001163 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001164 uint32_t total = 0, i;
1165 uint32_t num_to_alloc = 0;
1166 unsigned long flags;
dea31012005-04-17 16:05:31 -05001167
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001168 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001169 return -ENXIO;
1170
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001171 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001172
1173 /*
1174 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1175 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001176 * HBA limit conveyed to the midlayer via the host structure. The
1177 * formula accounts for the lun_queue_depth + error handlers + 1
1178 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001179 */
1180 total = phba->total_scsi_bufs;
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001181 num_to_alloc = phba->cfg_lun_queue_depth + 2;
dea31012005-04-17 16:05:31 -05001182 if (total >= phba->cfg_hba_queue_depth) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001183 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1184 "%d:0704 At limitation of %d preallocated "
1185 "command buffers\n", phba->brd_no, total);
dea31012005-04-17 16:05:31 -05001186 return 0;
1187 } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001188 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1189 "%d:0705 Allocation request of %d command "
1190 "buffers will exceed max of %d. Reducing "
1191 "allocation request to %d.\n", phba->brd_no,
1192 num_to_alloc, phba->cfg_hba_queue_depth,
1193 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001194 num_to_alloc = phba->cfg_hba_queue_depth - total;
1195 }
1196
1197 for (i = 0; i < num_to_alloc; i++) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001198 scsi_buf = lpfc_new_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001199 if (!scsi_buf) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001200 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1201 "%d:0706 Failed to allocate command "
1202 "buffer\n", phba->brd_no);
dea31012005-04-17 16:05:31 -05001203 break;
1204 }
1205
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001206 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001207 phba->total_scsi_bufs++;
1208 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001209 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001210 }
1211 return 0;
1212}
1213
1214static int
1215lpfc_slave_configure(struct scsi_device *sdev)
1216{
Jamie Wellnitz7f0b5b12006-02-28 19:25:24 -05001217 struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata;
dea31012005-04-17 16:05:31 -05001218 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1219
1220 if (sdev->tagged_supported)
1221 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1222 else
1223 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1224
1225 /*
1226 * Initialize the fc transport attributes for the target
1227 * containing this scsi device. Also note that the driver's
1228 * target pointer is stored in the starget_data for the
1229 * driver's sysfs entry point functions.
1230 */
1231 rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
1232
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001233 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1234 lpfc_sli_poll_fcp_ring(phba);
1235 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1236 lpfc_poll_rearm_timer(phba);
1237 }
1238
dea31012005-04-17 16:05:31 -05001239 return 0;
1240}
1241
1242static void
1243lpfc_slave_destroy(struct scsi_device *sdev)
1244{
1245 sdev->hostdata = NULL;
1246 return;
1247}
1248
1249struct scsi_host_template lpfc_template = {
1250 .module = THIS_MODULE,
1251 .name = LPFC_DRIVER_NAME,
1252 .info = lpfc_info,
1253 .queuecommand = lpfc_queuecommand,
1254 .eh_abort_handler = lpfc_abort_handler,
1255 .eh_device_reset_handler= lpfc_reset_lun_handler,
1256 .eh_bus_reset_handler = lpfc_reset_bus_handler,
1257 .slave_alloc = lpfc_slave_alloc,
1258 .slave_configure = lpfc_slave_configure,
1259 .slave_destroy = lpfc_slave_destroy,
1260 .this_id = -1,
1261 .sg_tablesize = LPFC_SG_SEG_CNT,
1262 .cmd_per_lun = LPFC_CMD_PER_LUN,
1263 .use_clustering = ENABLE_CLUSTERING,
1264 .shost_attrs = lpfc_host_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001265 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001266};