blob: c422220db0ae62cb466d99ddb81bea76488f4a81 [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. *
4 * Copyright (C) 2004-2005 Emulex. All rights reserved. *
5 * 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
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -050044static inline void
45lpfc_block_requests(struct lpfc_hba * phba)
46{
47 down(&phba->hba_can_block);
48 scsi_block_requests(phba->host);
49}
50
51static inline void
52lpfc_unblock_requests(struct lpfc_hba * phba)
53{
54 scsi_unblock_requests(phba->host);
55 up(&phba->hba_can_block);
56}
57
dea31012005-04-17 16:05:31 -050058/*
59 * This routine allocates a scsi buffer, which contains all the necessary
60 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
61 * contains information to build the IOCB. The DMAable region contains
62 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
63 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
64 * and the BPL BDE is setup in the IOCB.
65 */
66static struct lpfc_scsi_buf *
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040067lpfc_new_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -050068{
69 struct lpfc_scsi_buf *psb;
70 struct ulp_bde64 *bpl;
71 IOCB_t *iocb;
72 dma_addr_t pdma_phys;
James Bottomley604a3e32005-10-29 10:28:33 -050073 uint16_t iotag;
dea31012005-04-17 16:05:31 -050074
75 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
76 if (!psb)
77 return NULL;
78 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
79 psb->scsi_hba = phba;
80
81 /*
82 * Get memory from the pci pool to map the virt space to pci bus space
83 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
84 * struct fcp_rsp and the number of bde's necessary to support the
85 * sg_tablesize.
86 */
87 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
88 &psb->dma_handle);
89 if (!psb->data) {
90 kfree(psb);
91 return NULL;
92 }
93
94 /* Initialize virtual ptrs to dma_buf region. */
95 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
96
James Bottomley604a3e32005-10-29 10:28:33 -050097 /* Allocate iotag for psb->cur_iocbq. */
98 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
99 if (iotag == 0) {
100 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
101 psb->data, psb->dma_handle);
102 kfree (psb);
103 return NULL;
104 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400105 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500106
dea31012005-04-17 16:05:31 -0500107 psb->fcp_cmnd = psb->data;
108 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
109 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
110 sizeof(struct fcp_rsp);
111
112 /* Initialize local short-hand pointers. */
113 bpl = psb->fcp_bpl;
114 pdma_phys = psb->dma_handle;
115
116 /*
117 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
118 * list bdes. Initialize the first two and leave the rest for
119 * queuecommand.
120 */
121 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
122 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
123 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
124 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
125 bpl->tus.w = le32_to_cpu(bpl->tus.w);
126 bpl++;
127
128 /* Setup the physical region for the FCP RSP */
129 pdma_phys += sizeof (struct fcp_cmnd);
130 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
131 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
132 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
133 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
134 bpl->tus.w = le32_to_cpu(bpl->tus.w);
135
136 /*
137 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
138 * initialize it with all known data now.
139 */
140 pdma_phys += (sizeof (struct fcp_rsp));
141 iocb = &psb->cur_iocbq.iocb;
142 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
143 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
144 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
145 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
146 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
147 iocb->ulpBdeCount = 1;
148 iocb->ulpClass = CLASS3;
149
150 return psb;
151}
152
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400153struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500154lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500155{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400156 struct lpfc_scsi_buf * lpfc_cmd = NULL;
157 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500158 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500159
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500160 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400161 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500162 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400163 return lpfc_cmd;
164}
165
166static void
167lpfc_release_scsi_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
168{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500169 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500170 /*
171 * There are only two special cases to consider. (1) the scsi command
172 * requested scatter-gather usage or (2) the scsi command allocated
173 * a request buffer, but did not request use_sg. There is a third
174 * case, but it does not require resource deallocation.
175 */
176 if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
177 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
178 psb->seg_cnt, psb->pCmd->sc_data_direction);
179 } else {
180 if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
181 dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
182 psb->pCmd->request_bufflen,
183 psb->pCmd->sc_data_direction);
184 }
185 }
186
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500187 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400188 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500189 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500190 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500191}
192
193static int
194lpfc_scsi_prep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
195{
196 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
197 struct scatterlist *sgel = NULL;
198 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
199 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
200 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
201 dma_addr_t physaddr;
202 uint32_t i, num_bde = 0;
203 int datadir = scsi_cmnd->sc_data_direction;
204 int dma_error;
205
206 /*
207 * There are three possibilities here - use scatter-gather segment, use
208 * the single mapping, or neither. Start the lpfc command prep by
209 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
210 * data bde entry.
211 */
212 bpl += 2;
213 if (scsi_cmnd->use_sg) {
214 /*
215 * The driver stores the segment count returned from pci_map_sg
216 * because this a count of dma-mappings used to map the use_sg
217 * pages. They are not guaranteed to be the same for those
218 * architectures that implement an IOMMU.
219 */
220 sgel = (struct scatterlist *)scsi_cmnd->request_buffer;
221 lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
222 scsi_cmnd->use_sg, datadir);
223 if (lpfc_cmd->seg_cnt == 0)
224 return 1;
225
226 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
227 printk(KERN_ERR "%s: Too many sg segments from "
228 "dma_map_sg. Config %d, seg_cnt %d",
229 __FUNCTION__, phba->cfg_sg_seg_cnt,
230 lpfc_cmd->seg_cnt);
231 dma_unmap_sg(&phba->pcidev->dev, sgel,
232 lpfc_cmd->seg_cnt, datadir);
233 return 1;
234 }
235
236 /*
237 * The driver established a maximum scatter-gather segment count
238 * during probe that limits the number of sg elements in any
239 * single scsi command. Just run through the seg_cnt and format
240 * the bde's.
241 */
242 for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
243 physaddr = sg_dma_address(sgel);
244 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
245 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
246 bpl->tus.f.bdeSize = sg_dma_len(sgel);
247 if (datadir == DMA_TO_DEVICE)
248 bpl->tus.f.bdeFlags = 0;
249 else
250 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
251 bpl->tus.w = le32_to_cpu(bpl->tus.w);
252 bpl++;
253 sgel++;
254 num_bde++;
255 }
256 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
257 physaddr = dma_map_single(&phba->pcidev->dev,
258 scsi_cmnd->request_buffer,
259 scsi_cmnd->request_bufflen,
260 datadir);
261 dma_error = dma_mapping_error(physaddr);
262 if (dma_error) {
263 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
264 "%d:0718 Unable to dma_map_single "
265 "request_buffer: x%x\n",
266 phba->brd_no, dma_error);
267 return 1;
268 }
269
270 lpfc_cmd->nonsg_phys = physaddr;
271 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
272 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
273 bpl->tus.f.bdeSize = scsi_cmnd->request_bufflen;
274 if (datadir == DMA_TO_DEVICE)
275 bpl->tus.f.bdeFlags = 0;
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400276 else
277 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
dea31012005-04-17 16:05:31 -0500278 bpl->tus.w = le32_to_cpu(bpl->tus.w);
279 num_bde = 1;
280 bpl++;
281 }
282
283 /*
284 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400285 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
286 * reinitialized since all iocb memory resources are used many times
287 * for transmit, receive, and continuation bpl's.
dea31012005-04-17 16:05:31 -0500288 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400289 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500290 iocb_cmd->un.fcpi64.bdl.bdeSize +=
291 (num_bde * sizeof (struct ulp_bde64));
292 iocb_cmd->ulpBdeCount = 1;
293 iocb_cmd->ulpLe = 1;
294 fcp_cmnd->fcpDl = be32_to_cpu(scsi_cmnd->request_bufflen);
295 return 0;
296}
297
298static void
299lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
300{
301 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
302 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
303 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
304 struct lpfc_hba *phba = lpfc_cmd->scsi_hba;
305 uint32_t fcpi_parm = lpfc_cmd->cur_iocbq.iocb.un.fcpi.fcpi_parm;
306 uint32_t resp_info = fcprsp->rspStatus2;
307 uint32_t scsi_status = fcprsp->rspStatus3;
308 uint32_t host_status = DID_OK;
309 uint32_t rsplen = 0;
310
311 /*
312 * If this is a task management command, there is no
313 * scsi packet associated with this lpfc_cmd. The driver
314 * consumes it.
315 */
316 if (fcpcmd->fcpCntl2) {
317 scsi_status = 0;
318 goto out;
319 }
320
321 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
322 "%d:0730 FCP command failed: RSP "
323 "Data: x%x x%x x%x x%x x%x x%x\n",
324 phba->brd_no, resp_info, scsi_status,
325 be32_to_cpu(fcprsp->rspResId),
326 be32_to_cpu(fcprsp->rspSnsLen),
327 be32_to_cpu(fcprsp->rspRspLen),
328 fcprsp->rspInfo3);
329
330 if (resp_info & RSP_LEN_VALID) {
331 rsplen = be32_to_cpu(fcprsp->rspRspLen);
332 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
333 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
334 host_status = DID_ERROR;
335 goto out;
336 }
337 }
338
339 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
340 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
341 if (snslen > SCSI_SENSE_BUFFERSIZE)
342 snslen = SCSI_SENSE_BUFFERSIZE;
343
344 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
345 }
346
347 cmnd->resid = 0;
348 if (resp_info & RESID_UNDER) {
349 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
350
351 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
352 "%d:0716 FCP Read Underrun, expected %d, "
353 "residual %d Data: x%x x%x x%x\n", phba->brd_no,
354 be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
355 fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
356
357 /*
358 * The cmnd->underflow is the minimum number of bytes that must
359 * be transfered for this command. Provided a sense condition
360 * is not present, make sure the actual amount transferred is at
361 * least the underflow value or fail.
362 */
363 if (!(resp_info & SNS_LEN_VALID) &&
364 (scsi_status == SAM_STAT_GOOD) &&
365 (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
366 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
367 "%d:0717 FCP command x%x residual "
368 "underrun converted to error "
369 "Data: x%x x%x x%x\n", phba->brd_no,
370 cmnd->cmnd[0], cmnd->request_bufflen,
371 cmnd->resid, cmnd->underflow);
372
373 host_status = DID_ERROR;
374 }
375 } else if (resp_info & RESID_OVER) {
376 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
377 "%d:0720 FCP command x%x residual "
378 "overrun error. Data: x%x x%x \n",
379 phba->brd_no, cmnd->cmnd[0],
380 cmnd->request_bufflen, cmnd->resid);
381 host_status = DID_ERROR;
382
383 /*
384 * Check SLI validation that all the transfer was actually done
385 * (fcpi_parm should be zero). Apply check only to reads.
386 */
387 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
388 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
389 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
390 "%d:0734 FCP Read Check Error Data: "
391 "x%x x%x x%x x%x\n", phba->brd_no,
392 be32_to_cpu(fcpcmd->fcpDl),
393 be32_to_cpu(fcprsp->rspResId),
394 fcpi_parm, cmnd->cmnd[0]);
395 host_status = DID_ERROR;
396 cmnd->resid = cmnd->request_bufflen;
397 }
398
399 out:
400 cmnd->result = ScsiResult(host_status, scsi_status);
401}
402
403static void
404lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
405 struct lpfc_iocbq *pIocbOut)
406{
407 struct lpfc_scsi_buf *lpfc_cmd =
408 (struct lpfc_scsi_buf *) pIocbIn->context1;
409 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
410 struct lpfc_nodelist *pnode = rdata->pnode;
411 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
dea31012005-04-17 16:05:31 -0500412
413 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
414 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
415
416 if (lpfc_cmd->status) {
417 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
418 (lpfc_cmd->result & IOERR_DRVR_MASK))
419 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
420 else if (lpfc_cmd->status >= IOSTAT_CNT)
421 lpfc_cmd->status = IOSTAT_DEFAULT;
422
423 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
424 "%d:0729 FCP cmd x%x failed <%d/%d> status: "
425 "x%x result: x%x Data: x%x x%x\n",
426 phba->brd_no, cmd->cmnd[0], cmd->device->id,
427 cmd->device->lun, lpfc_cmd->status,
428 lpfc_cmd->result, pIocbOut->iocb.ulpContext,
429 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
430
431 switch (lpfc_cmd->status) {
432 case IOSTAT_FCP_RSP_ERROR:
433 /* Call FCP RSP handler to determine result */
434 lpfc_handle_fcp_err(lpfc_cmd);
435 break;
436 case IOSTAT_NPORT_BSY:
437 case IOSTAT_FABRIC_BSY:
438 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
439 break;
440 default:
441 cmd->result = ScsiResult(DID_ERROR, 0);
442 break;
443 }
444
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400445 if ((pnode == NULL )
446 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
447 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500448 } else {
449 cmd->result = ScsiResult(DID_OK, 0);
450 }
451
452 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
453 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
454
455 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
456 "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
457 "SNS x%x x%x Data: x%x x%x\n",
458 phba->brd_no, cmd->device->id,
459 cmd->device->lun, cmd, cmd->result,
460 *lp, *(lp + 3), cmd->retries, cmd->resid);
461 }
462
dea31012005-04-17 16:05:31 -0500463 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400464
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400465 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500466}
467
468static void
469lpfc_scsi_prep_cmnd(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd,
470 struct lpfc_nodelist *pnode)
471{
472 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
473 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
474 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
475 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
476 int datadir = scsi_cmnd->sc_data_direction;
477
478 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400479 /* clear task management bits */
480 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500481
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400482 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
483 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500484
485 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
486
487 if (scsi_cmnd->device->tagged_supported) {
488 switch (scsi_cmnd->tag) {
489 case HEAD_OF_QUEUE_TAG:
490 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
491 break;
492 case ORDERED_QUEUE_TAG:
493 fcp_cmnd->fcpCntl1 = ORDERED_Q;
494 break;
495 default:
496 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
497 break;
498 }
499 } else
500 fcp_cmnd->fcpCntl1 = 0;
501
502 /*
503 * There are three possibilities here - use scatter-gather segment, use
504 * the single mapping, or neither. Start the lpfc command prep by
505 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
506 * data bde entry.
507 */
508 if (scsi_cmnd->use_sg) {
509 if (datadir == DMA_TO_DEVICE) {
510 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
511 iocb_cmd->un.fcpi.fcpi_parm = 0;
512 iocb_cmd->ulpPU = 0;
513 fcp_cmnd->fcpCntl3 = WRITE_DATA;
514 phba->fc4OutputRequests++;
515 } else {
516 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
517 iocb_cmd->ulpPU = PARM_READ_CHECK;
518 iocb_cmd->un.fcpi.fcpi_parm =
519 scsi_cmnd->request_bufflen;
520 fcp_cmnd->fcpCntl3 = READ_DATA;
521 phba->fc4InputRequests++;
522 }
523 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
524 if (datadir == DMA_TO_DEVICE) {
525 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
526 iocb_cmd->un.fcpi.fcpi_parm = 0;
527 iocb_cmd->ulpPU = 0;
528 fcp_cmnd->fcpCntl3 = WRITE_DATA;
529 phba->fc4OutputRequests++;
530 } else {
531 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
532 iocb_cmd->ulpPU = PARM_READ_CHECK;
533 iocb_cmd->un.fcpi.fcpi_parm =
534 scsi_cmnd->request_bufflen;
535 fcp_cmnd->fcpCntl3 = READ_DATA;
536 phba->fc4InputRequests++;
537 }
538 } else {
539 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
540 iocb_cmd->un.fcpi.fcpi_parm = 0;
541 iocb_cmd->ulpPU = 0;
542 fcp_cmnd->fcpCntl3 = 0;
543 phba->fc4ControlRequests++;
544 }
545
546 /*
547 * Finish initializing those IOCB fields that are independent
548 * of the scsi_cmnd request_buffer
549 */
550 piocbq->iocb.ulpContext = pnode->nlp_rpi;
551 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
552 piocbq->iocb.ulpFCP2Rcvy = 1;
553
554 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
555 piocbq->context1 = lpfc_cmd;
556 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
557 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
558}
559
560static int
561lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
562 struct lpfc_scsi_buf *lpfc_cmd,
563 uint8_t task_mgmt_cmd)
564{
565 struct lpfc_sli *psli;
566 struct lpfc_iocbq *piocbq;
567 IOCB_t *piocb;
568 struct fcp_cmnd *fcp_cmnd;
569 struct scsi_device *scsi_dev = lpfc_cmd->pCmd->device;
570 struct lpfc_rport_data *rdata = scsi_dev->hostdata;
571 struct lpfc_nodelist *ndlp = rdata->pnode;
572
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400573 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea31012005-04-17 16:05:31 -0500574 return 0;
575 }
576
577 psli = &phba->sli;
578 piocbq = &(lpfc_cmd->cur_iocbq);
579 piocb = &piocbq->iocb;
580
581 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400582 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
583 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500584 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
585
586 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
587
588 piocb->ulpContext = ndlp->nlp_rpi;
589 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
590 piocb->ulpFCP2Rcvy = 1;
591 }
592 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
593
594 /* ulpTimeout is only one byte */
595 if (lpfc_cmd->timeout > 0xff) {
596 /*
597 * Do not timeout the command at the firmware level.
598 * The driver will provide the timeout mechanism.
599 */
600 piocb->ulpTimeout = 0;
601 } else {
602 piocb->ulpTimeout = lpfc_cmd->timeout;
603 }
604
605 lpfc_cmd->rdata = rdata;
606
607 switch (task_mgmt_cmd) {
608 case FCP_LUN_RESET:
609 /* Issue LUN Reset to TGT <num> LUN <num> */
610 lpfc_printf_log(phba,
611 KERN_INFO,
612 LOG_FCP,
613 "%d:0703 Issue LUN Reset to TGT %d LUN %d "
614 "Data: x%x x%x\n",
615 phba->brd_no,
616 scsi_dev->id, scsi_dev->lun,
617 ndlp->nlp_rpi, ndlp->nlp_flag);
618
619 break;
620 case FCP_ABORT_TASK_SET:
621 /* Issue Abort Task Set to TGT <num> LUN <num> */
622 lpfc_printf_log(phba,
623 KERN_INFO,
624 LOG_FCP,
625 "%d:0701 Issue Abort Task Set to TGT %d LUN %d "
626 "Data: x%x x%x\n",
627 phba->brd_no,
628 scsi_dev->id, scsi_dev->lun,
629 ndlp->nlp_rpi, ndlp->nlp_flag);
630
631 break;
632 case FCP_TARGET_RESET:
633 /* Issue Target Reset to TGT <num> */
634 lpfc_printf_log(phba,
635 KERN_INFO,
636 LOG_FCP,
637 "%d:0702 Issue Target Reset to TGT %d "
638 "Data: x%x x%x\n",
639 phba->brd_no,
640 scsi_dev->id, ndlp->nlp_rpi,
641 ndlp->nlp_flag);
642 break;
643 }
644
645 return (1);
646}
647
648static int
649lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba)
650{
651 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400652 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500653 int ret;
654
655 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_TARGET_RESET);
656 if (!ret)
657 return FAILED;
658
659 lpfc_cmd->scsi_hba = phba;
660 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400661 iocbqrsp = lpfc_sli_get_iocbq(phba);
662
dea31012005-04-17 16:05:31 -0500663 if (!iocbqrsp)
664 return FAILED;
dea31012005-04-17 16:05:31 -0500665
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400666 ret = lpfc_sli_issue_iocb_wait(phba,
667 &phba->sli.ring[phba->sli.fcp_ring],
668 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500669 if (ret != IOCB_SUCCESS) {
670 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
671 ret = FAILED;
672 } else {
673 ret = SUCCESS;
674 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
675 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
676 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
677 (lpfc_cmd->result & IOERR_DRVR_MASK))
678 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
679 }
680
James Bottomley604a3e32005-10-29 10:28:33 -0500681 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500682 return ret;
683}
684
dea31012005-04-17 16:05:31 -0500685const char *
686lpfc_info(struct Scsi_Host *host)
687{
688 struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata[0];
689 int len;
690 static char lpfcinfobuf[384];
691
692 memset(lpfcinfobuf,0,384);
693 if (phba && phba->pcidev){
694 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
695 len = strlen(lpfcinfobuf);
696 snprintf(lpfcinfobuf + len,
697 384-len,
698 " on PCI bus %02x device %02x irq %d",
699 phba->pcidev->bus->number,
700 phba->pcidev->devfn,
701 phba->pcidev->irq);
702 len = strlen(lpfcinfobuf);
703 if (phba->Port[0]) {
704 snprintf(lpfcinfobuf + len,
705 384-len,
706 " port %s",
707 phba->Port);
708 }
709 }
710 return lpfcinfobuf;
711}
712
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500713static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
714{
715 unsigned long poll_tmo_expires =
716 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
717
718 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
719 mod_timer(&phba->fcp_poll_timer,
720 poll_tmo_expires);
721}
722
723void lpfc_poll_start_timer(struct lpfc_hba * phba)
724{
725 lpfc_poll_rearm_timer(phba);
726}
727
728void lpfc_poll_timeout(unsigned long ptr)
729{
730 struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
731 unsigned long iflag;
732
733 spin_lock_irqsave(phba->host->host_lock, iflag);
734
735 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
736 lpfc_sli_poll_fcp_ring (phba);
737 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
738 lpfc_poll_rearm_timer(phba);
739 }
740
741 spin_unlock_irqrestore(phba->host->host_lock, iflag);
742}
743
dea31012005-04-17 16:05:31 -0500744static int
745lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
746{
747 struct lpfc_hba *phba =
748 (struct lpfc_hba *) cmnd->device->host->hostdata[0];
749 struct lpfc_sli *psli = &phba->sli;
750 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
751 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400752 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400753 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400754 int err;
dea31012005-04-17 16:05:31 -0500755
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400756 err = fc_remote_port_chkready(rport);
757 if (err) {
758 cmnd->result = err;
dea31012005-04-17 16:05:31 -0500759 goto out_fail_command;
760 }
761
762 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400763 * Catch race where our node has transitioned, but the
764 * transport is still transitioning.
dea31012005-04-17 16:05:31 -0500765 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400766 if (!ndlp) {
767 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
768 goto out_fail_command;
dea31012005-04-17 16:05:31 -0500769 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500770 lpfc_cmd = lpfc_get_scsi_buf (phba);
dea31012005-04-17 16:05:31 -0500771 if (lpfc_cmd == NULL) {
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500772 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
773 "%d:0707 driver's buffer pool is empty, "
774 "IO busied\n", phba->brd_no);
dea31012005-04-17 16:05:31 -0500775 goto out_host_busy;
776 }
777
778 /*
779 * Store the midlayer's command structure for the completion phase
780 * and complete the command initialization.
781 */
782 lpfc_cmd->pCmd = cmnd;
783 lpfc_cmd->rdata = rdata;
784 lpfc_cmd->timeout = 0;
785 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
786 cmnd->scsi_done = done;
787
788 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
789 if (err)
790 goto out_host_busy_free_buf;
791
792 lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
793
794 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
795 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
796 if (err)
797 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500798
799 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
800 lpfc_sli_poll_fcp_ring(phba);
801 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
802 lpfc_poll_rearm_timer(phba);
803 }
804
dea31012005-04-17 16:05:31 -0500805 return 0;
806
807 out_host_busy_free_buf:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400808 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500809 out_host_busy:
810 return SCSI_MLQUEUE_HOST_BUSY;
811
812 out_fail_command:
813 done(cmnd);
814 return 0;
815}
816
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500817
dea31012005-04-17 16:05:31 -0500818static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500819lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500820{
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500821 struct Scsi_Host *shost = cmnd->device->host;
822 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
dea31012005-04-17 16:05:31 -0500823 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400824 struct lpfc_iocbq *iocb;
825 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -0500826 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -0500827 IOCB_t *cmd, *icmd;
dea31012005-04-17 16:05:31 -0500828 unsigned int loop_count = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400829 int ret = SUCCESS;
830
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -0500831 lpfc_block_requests(phba);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500832 spin_lock_irq(shost->host_lock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400833
834 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
835 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -0500836
837 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400838 * If pCmd field of the corresponding lpfc_scsi_buf structure
839 * points to a different SCSI command, then the driver has
840 * already completed this command, but the midlayer did not
841 * see the completion before the eh fired. Just return
842 * SUCCESS.
dea31012005-04-17 16:05:31 -0500843 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400844 iocb = &lpfc_cmd->cur_iocbq;
845 if (lpfc_cmd->pCmd != cmnd)
846 goto out;
dea31012005-04-17 16:05:31 -0500847
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400848 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -0500849
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400850 abtsiocb = lpfc_sli_get_iocbq(phba);
851 if (abtsiocb == NULL) {
852 ret = FAILED;
dea31012005-04-17 16:05:31 -0500853 goto out;
854 }
855
dea31012005-04-17 16:05:31 -0500856 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400857 * The scsi command can not be in txq and it is in flight because the
858 * pCmd is still pointig at the SCSI command we have to abort. There
859 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -0500860 */
dea31012005-04-17 16:05:31 -0500861
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400862 cmd = &iocb->iocb;
863 icmd = &abtsiocb->iocb;
864 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
865 icmd->un.acxri.abortContextTag = cmd->ulpContext;
866 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -0500867
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400868 icmd->ulpLe = 1;
869 icmd->ulpClass = cmd->ulpClass;
870 if (phba->hba_state >= LPFC_LINK_UP)
871 icmd->ulpCommand = CMD_ABORT_XRI_CN;
872 else
873 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -0500874
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400875 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
876 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
877 lpfc_sli_release_iocbq(phba, abtsiocb);
878 ret = FAILED;
879 goto out;
880 }
881
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500882 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
883 lpfc_sli_poll_fcp_ring (phba);
884
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400885 /* Wait for abort to complete */
886 while (lpfc_cmd->pCmd == cmnd)
887 {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500888 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
889 lpfc_sli_poll_fcp_ring (phba);
890
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400891 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800892 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT*HZ);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400893 spin_lock_irq(phba->host->host_lock);
894 if (++loop_count
895 > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
dea31012005-04-17 16:05:31 -0500896 break;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400897 }
dea31012005-04-17 16:05:31 -0500898
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400899 if (lpfc_cmd->pCmd == cmnd) {
900 ret = FAILED;
901 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
902 "%d:0748 abort handler timed out waiting for "
903 "abort to complete: ret %#x, ID %d, LUN %d, "
904 "snum %#lx\n",
905 phba->brd_no, ret, cmnd->device->id,
906 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -0500907 }
908
909 out:
910 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500911 "%d:0749 SCSI Layer I/O Abort Request "
912 "Status x%x ID %d LUN %d snum %#lx\n",
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400913 phba->brd_no, ret, cmnd->device->id,
914 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -0500915
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500916 spin_unlock_irq(shost->host_lock);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -0500917 lpfc_unblock_requests(phba);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500918
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400919 return ret;
dea31012005-04-17 16:05:31 -0500920}
921
922static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500923lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500924{
925 struct Scsi_Host *shost = cmnd->device->host;
926 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400927 struct lpfc_scsi_buf *lpfc_cmd;
928 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -0500929 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
930 struct lpfc_nodelist *pnode = rdata->pnode;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -0500931 uint32_t cmd_result = 0, cmd_status = 0;
dea31012005-04-17 16:05:31 -0500932 int ret = FAILED;
933 int cnt, loopcnt;
934
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -0500935 lpfc_block_requests(phba);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500936 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500937 /*
938 * If target is not in a MAPPED state, delay the reset until
939 * target is rediscovered or nodev timeout expires.
940 */
941 while ( 1 ) {
942 if (!pnode)
943 break;
944
945 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
946 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800947 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
dea31012005-04-17 16:05:31 -0500948 spin_lock_irq(phba->host->host_lock);
949 }
950 if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
951 break;
952 }
953
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500954 lpfc_cmd = lpfc_get_scsi_buf (phba);
dea31012005-04-17 16:05:31 -0500955 if (lpfc_cmd == NULL)
956 goto out;
957
958 lpfc_cmd->pCmd = cmnd;
959 lpfc_cmd->timeout = 60;
960 lpfc_cmd->scsi_hba = phba;
961
962 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
963 if (!ret)
964 goto out_free_scsi_buf;
965
966 iocbq = &lpfc_cmd->cur_iocbq;
967
968 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400969 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -0500970 if (iocbqrsp == NULL)
971 goto out_free_scsi_buf;
972
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400973 ret = lpfc_sli_issue_iocb_wait(phba,
974 &phba->sli.ring[phba->sli.fcp_ring],
975 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500976 if (ret == IOCB_SUCCESS)
977 ret = SUCCESS;
978
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -0500979
980 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
981 cmd_status = iocbqrsp->iocb.ulpStatus;
982
983 lpfc_sli_release_iocbq(phba, iocbqrsp);
984 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500985
986 /*
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -0500987 * All outstanding txcmplq I/Os should have been aborted by the device.
dea31012005-04-17 16:05:31 -0500988 * Unfortunately, some targets do not abide by this forcing the driver
989 * to double check.
990 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -0500991 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
992 cmnd->device->id, cmnd->device->lun,
993 LPFC_CTX_LUN);
994 if (cnt)
995 lpfc_sli_abort_iocb(phba,
996 &phba->sli.ring[phba->sli.fcp_ring],
997 cmnd->device->id, cmnd->device->lun,
998 0, LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -0500999 loopcnt = 0;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001000 while(cnt) {
dea31012005-04-17 16:05:31 -05001001 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001002 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001003 spin_lock_irq(phba->host->host_lock);
1004
1005 if (++loopcnt
1006 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1007 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001008
1009 cnt = lpfc_sli_sum_iocb(phba,
1010 &phba->sli.ring[phba->sli.fcp_ring],
1011 cmnd->device->id, cmnd->device->lun,
1012 LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001013 }
1014
1015 if (cnt) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001016 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea31012005-04-17 16:05:31 -05001017 "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1018 phba->brd_no, cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001019 ret = FAILED;
dea31012005-04-17 16:05:31 -05001020 }
1021
dea31012005-04-17 16:05:31 -05001022out_free_scsi_buf:
1023 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1024 "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1025 "Data: x%x x%x x%x\n",
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001026 phba->brd_no, cmnd->device->id,cmnd->device->lun,
1027 ret, cmd_status, cmd_result);
1028
dea31012005-04-17 16:05:31 -05001029out:
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001030 spin_unlock_irq(shost->host_lock);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001031 lpfc_unblock_requests(phba);
dea31012005-04-17 16:05:31 -05001032 return ret;
1033}
1034
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001035static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001036lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001037{
1038 struct Scsi_Host *shost = cmnd->device->host;
1039 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
1040 struct lpfc_nodelist *ndlp = NULL;
1041 int match;
1042 int ret = FAILED, i, err_count = 0;
1043 int cnt, loopcnt;
1044 unsigned int midlayer_id = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001045 struct lpfc_scsi_buf * lpfc_cmd;
dea31012005-04-17 16:05:31 -05001046
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001047 lpfc_block_requests(phba);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001048 spin_lock_irq(shost->host_lock);
1049
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001050 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001051 if (lpfc_cmd == NULL)
1052 goto out;
1053
1054 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1055 lpfc_cmd->timeout = 60;
1056 lpfc_cmd->pCmd = cmnd;
1057 lpfc_cmd->scsi_hba = phba;
1058
1059 /*
1060 * Since the driver manages a single bus device, reset all
1061 * targets known to the driver. Should any target reset
1062 * fail, this routine returns failure to the midlayer.
1063 */
1064 midlayer_id = cmnd->device->id;
1065 for (i = 0; i < MAX_FCP_TARGET; i++) {
1066 /* Search the mapped list for this target ID */
1067 match = 0;
1068 list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1069 if ((i == ndlp->nlp_sid) && ndlp->rport) {
1070 match = 1;
1071 break;
1072 }
1073 }
1074 if (!match)
1075 continue;
1076
1077 lpfc_cmd->pCmd->device->id = i;
1078 lpfc_cmd->pCmd->device->hostdata = ndlp->rport->dd_data;
1079 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
1080 if (ret != SUCCESS) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001081 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea31012005-04-17 16:05:31 -05001082 "%d:0713 Bus Reset on target %d failed\n",
1083 phba->brd_no, i);
1084 err_count++;
1085 }
1086 }
1087
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001088 if (err_count == 0)
1089 ret = SUCCESS;
1090
1091 lpfc_release_scsi_buf(phba, lpfc_cmd);
1092
1093 /*
1094 * All outstanding txcmplq I/Os should have been aborted by
1095 * the targets. Unfortunately, some targets do not abide by
1096 * this forcing the driver to double check.
1097 */
dea31012005-04-17 16:05:31 -05001098 cmnd->device->id = midlayer_id;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001099 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1100 0, 0, LPFC_CTX_HOST);
1101 if (cnt)
1102 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1103 0, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001104 loopcnt = 0;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001105 while(cnt) {
dea31012005-04-17 16:05:31 -05001106 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001107 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001108 spin_lock_irq(phba->host->host_lock);
1109
1110 if (++loopcnt
1111 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1112 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001113
1114 cnt = lpfc_sli_sum_iocb(phba,
1115 &phba->sli.ring[phba->sli.fcp_ring],
1116 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001117 }
1118
1119 if (cnt) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001120 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea31012005-04-17 16:05:31 -05001121 "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1122 phba->brd_no, cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001123 ret = FAILED;
dea31012005-04-17 16:05:31 -05001124 }
1125
dea31012005-04-17 16:05:31 -05001126 lpfc_printf_log(phba,
1127 KERN_ERR,
1128 LOG_FCP,
1129 "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1130 phba->brd_no, ret);
1131out:
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001132 spin_unlock_irq(shost->host_lock);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001133 lpfc_unblock_requests(phba);
dea31012005-04-17 16:05:31 -05001134 return ret;
1135}
1136
1137static int
dea31012005-04-17 16:05:31 -05001138lpfc_slave_alloc(struct scsi_device *sdev)
1139{
1140 struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata[0];
dea31012005-04-17 16:05:31 -05001141 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001142 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001143 uint32_t total = 0, i;
1144 uint32_t num_to_alloc = 0;
1145 unsigned long flags;
dea31012005-04-17 16:05:31 -05001146
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001147 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001148 return -ENXIO;
1149
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001150 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001151
1152 /*
1153 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1154 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001155 * HBA limit conveyed to the midlayer via the host structure. The
1156 * formula accounts for the lun_queue_depth + error handlers + 1
1157 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001158 */
1159 total = phba->total_scsi_bufs;
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001160 num_to_alloc = phba->cfg_lun_queue_depth + 2;
dea31012005-04-17 16:05:31 -05001161 if (total >= phba->cfg_hba_queue_depth) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001162 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1163 "%d:0704 At limitation of %d preallocated "
1164 "command buffers\n", phba->brd_no, total);
dea31012005-04-17 16:05:31 -05001165 return 0;
1166 } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001167 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1168 "%d:0705 Allocation request of %d command "
1169 "buffers will exceed max of %d. Reducing "
1170 "allocation request to %d.\n", phba->brd_no,
1171 num_to_alloc, phba->cfg_hba_queue_depth,
1172 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001173 num_to_alloc = phba->cfg_hba_queue_depth - total;
1174 }
1175
1176 for (i = 0; i < num_to_alloc; i++) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001177 scsi_buf = lpfc_new_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001178 if (!scsi_buf) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001179 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1180 "%d:0706 Failed to allocate command "
1181 "buffer\n", phba->brd_no);
dea31012005-04-17 16:05:31 -05001182 break;
1183 }
1184
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001185 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001186 phba->total_scsi_bufs++;
1187 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001188 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001189 }
1190 return 0;
1191}
1192
1193static int
1194lpfc_slave_configure(struct scsi_device *sdev)
1195{
1196 struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata[0];
1197 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1198
1199 if (sdev->tagged_supported)
1200 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1201 else
1202 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1203
1204 /*
1205 * Initialize the fc transport attributes for the target
1206 * containing this scsi device. Also note that the driver's
1207 * target pointer is stored in the starget_data for the
1208 * driver's sysfs entry point functions.
1209 */
1210 rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
1211
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001212 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1213 lpfc_sli_poll_fcp_ring(phba);
1214 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1215 lpfc_poll_rearm_timer(phba);
1216 }
1217
dea31012005-04-17 16:05:31 -05001218 return 0;
1219}
1220
1221static void
1222lpfc_slave_destroy(struct scsi_device *sdev)
1223{
1224 sdev->hostdata = NULL;
1225 return;
1226}
1227
1228struct scsi_host_template lpfc_template = {
1229 .module = THIS_MODULE,
1230 .name = LPFC_DRIVER_NAME,
1231 .info = lpfc_info,
1232 .queuecommand = lpfc_queuecommand,
1233 .eh_abort_handler = lpfc_abort_handler,
1234 .eh_device_reset_handler= lpfc_reset_lun_handler,
1235 .eh_bus_reset_handler = lpfc_reset_bus_handler,
1236 .slave_alloc = lpfc_slave_alloc,
1237 .slave_configure = lpfc_slave_configure,
1238 .slave_destroy = lpfc_slave_destroy,
1239 .this_id = -1,
1240 .sg_tablesize = LPFC_SG_SEG_CNT,
1241 .cmd_per_lun = LPFC_CMD_PER_LUN,
1242 .use_clustering = ENABLE_CLUSTERING,
1243 .shost_attrs = lpfc_host_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001244 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001245};