blob: a4d8455de4464962235a538c8453b4821aaba9b9 [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
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
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400139struct lpfc_scsi_buf*
140lpfc_sli_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;
dea31012005-04-17 16:05:31 -0500144
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400145 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
146 return lpfc_cmd;
147}
148
149static void
150lpfc_release_scsi_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
151{
dea31012005-04-17 16:05:31 -0500152 /*
153 * There are only two special cases to consider. (1) the scsi command
154 * requested scatter-gather usage or (2) the scsi command allocated
155 * a request buffer, but did not request use_sg. There is a third
156 * case, but it does not require resource deallocation.
157 */
158 if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
159 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
160 psb->seg_cnt, psb->pCmd->sc_data_direction);
161 } else {
162 if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
163 dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
164 psb->pCmd->request_bufflen,
165 psb->pCmd->sc_data_direction);
166 }
167 }
168
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400169 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500170 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
171}
172
173static int
174lpfc_scsi_prep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
175{
176 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
177 struct scatterlist *sgel = NULL;
178 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
179 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
180 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
181 dma_addr_t physaddr;
182 uint32_t i, num_bde = 0;
183 int datadir = scsi_cmnd->sc_data_direction;
184 int dma_error;
185
186 /*
187 * There are three possibilities here - use scatter-gather segment, use
188 * the single mapping, or neither. Start the lpfc command prep by
189 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
190 * data bde entry.
191 */
192 bpl += 2;
193 if (scsi_cmnd->use_sg) {
194 /*
195 * The driver stores the segment count returned from pci_map_sg
196 * because this a count of dma-mappings used to map the use_sg
197 * pages. They are not guaranteed to be the same for those
198 * architectures that implement an IOMMU.
199 */
200 sgel = (struct scatterlist *)scsi_cmnd->request_buffer;
201 lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
202 scsi_cmnd->use_sg, datadir);
203 if (lpfc_cmd->seg_cnt == 0)
204 return 1;
205
206 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
207 printk(KERN_ERR "%s: Too many sg segments from "
208 "dma_map_sg. Config %d, seg_cnt %d",
209 __FUNCTION__, phba->cfg_sg_seg_cnt,
210 lpfc_cmd->seg_cnt);
211 dma_unmap_sg(&phba->pcidev->dev, sgel,
212 lpfc_cmd->seg_cnt, datadir);
213 return 1;
214 }
215
216 /*
217 * The driver established a maximum scatter-gather segment count
218 * during probe that limits the number of sg elements in any
219 * single scsi command. Just run through the seg_cnt and format
220 * the bde's.
221 */
222 for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
223 physaddr = sg_dma_address(sgel);
224 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
225 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
226 bpl->tus.f.bdeSize = sg_dma_len(sgel);
227 if (datadir == DMA_TO_DEVICE)
228 bpl->tus.f.bdeFlags = 0;
229 else
230 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
231 bpl->tus.w = le32_to_cpu(bpl->tus.w);
232 bpl++;
233 sgel++;
234 num_bde++;
235 }
236 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
237 physaddr = dma_map_single(&phba->pcidev->dev,
238 scsi_cmnd->request_buffer,
239 scsi_cmnd->request_bufflen,
240 datadir);
241 dma_error = dma_mapping_error(physaddr);
242 if (dma_error) {
243 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
244 "%d:0718 Unable to dma_map_single "
245 "request_buffer: x%x\n",
246 phba->brd_no, dma_error);
247 return 1;
248 }
249
250 lpfc_cmd->nonsg_phys = physaddr;
251 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
252 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
253 bpl->tus.f.bdeSize = scsi_cmnd->request_bufflen;
254 if (datadir == DMA_TO_DEVICE)
255 bpl->tus.f.bdeFlags = 0;
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400256 else
257 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
dea31012005-04-17 16:05:31 -0500258 bpl->tus.w = le32_to_cpu(bpl->tus.w);
259 num_bde = 1;
260 bpl++;
261 }
262
263 /*
264 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400265 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
266 * reinitialized since all iocb memory resources are used many times
267 * for transmit, receive, and continuation bpl's.
dea31012005-04-17 16:05:31 -0500268 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400269 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500270 iocb_cmd->un.fcpi64.bdl.bdeSize +=
271 (num_bde * sizeof (struct ulp_bde64));
272 iocb_cmd->ulpBdeCount = 1;
273 iocb_cmd->ulpLe = 1;
274 fcp_cmnd->fcpDl = be32_to_cpu(scsi_cmnd->request_bufflen);
275 return 0;
276}
277
278static void
279lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
280{
281 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
282 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
283 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
284 struct lpfc_hba *phba = lpfc_cmd->scsi_hba;
285 uint32_t fcpi_parm = lpfc_cmd->cur_iocbq.iocb.un.fcpi.fcpi_parm;
286 uint32_t resp_info = fcprsp->rspStatus2;
287 uint32_t scsi_status = fcprsp->rspStatus3;
288 uint32_t host_status = DID_OK;
289 uint32_t rsplen = 0;
290
291 /*
292 * If this is a task management command, there is no
293 * scsi packet associated with this lpfc_cmd. The driver
294 * consumes it.
295 */
296 if (fcpcmd->fcpCntl2) {
297 scsi_status = 0;
298 goto out;
299 }
300
301 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
302 "%d:0730 FCP command failed: RSP "
303 "Data: x%x x%x x%x x%x x%x x%x\n",
304 phba->brd_no, resp_info, scsi_status,
305 be32_to_cpu(fcprsp->rspResId),
306 be32_to_cpu(fcprsp->rspSnsLen),
307 be32_to_cpu(fcprsp->rspRspLen),
308 fcprsp->rspInfo3);
309
310 if (resp_info & RSP_LEN_VALID) {
311 rsplen = be32_to_cpu(fcprsp->rspRspLen);
312 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
313 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
314 host_status = DID_ERROR;
315 goto out;
316 }
317 }
318
319 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
320 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
321 if (snslen > SCSI_SENSE_BUFFERSIZE)
322 snslen = SCSI_SENSE_BUFFERSIZE;
323
324 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
325 }
326
327 cmnd->resid = 0;
328 if (resp_info & RESID_UNDER) {
329 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
330
331 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
332 "%d:0716 FCP Read Underrun, expected %d, "
333 "residual %d Data: x%x x%x x%x\n", phba->brd_no,
334 be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
335 fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
336
337 /*
338 * The cmnd->underflow is the minimum number of bytes that must
339 * be transfered for this command. Provided a sense condition
340 * is not present, make sure the actual amount transferred is at
341 * least the underflow value or fail.
342 */
343 if (!(resp_info & SNS_LEN_VALID) &&
344 (scsi_status == SAM_STAT_GOOD) &&
345 (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
346 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
347 "%d:0717 FCP command x%x residual "
348 "underrun converted to error "
349 "Data: x%x x%x x%x\n", phba->brd_no,
350 cmnd->cmnd[0], cmnd->request_bufflen,
351 cmnd->resid, cmnd->underflow);
352
353 host_status = DID_ERROR;
354 }
355 } else if (resp_info & RESID_OVER) {
356 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
357 "%d:0720 FCP command x%x residual "
358 "overrun error. Data: x%x x%x \n",
359 phba->brd_no, cmnd->cmnd[0],
360 cmnd->request_bufflen, cmnd->resid);
361 host_status = DID_ERROR;
362
363 /*
364 * Check SLI validation that all the transfer was actually done
365 * (fcpi_parm should be zero). Apply check only to reads.
366 */
367 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
368 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
369 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
370 "%d:0734 FCP Read Check Error Data: "
371 "x%x x%x x%x x%x\n", phba->brd_no,
372 be32_to_cpu(fcpcmd->fcpDl),
373 be32_to_cpu(fcprsp->rspResId),
374 fcpi_parm, cmnd->cmnd[0]);
375 host_status = DID_ERROR;
376 cmnd->resid = cmnd->request_bufflen;
377 }
378
379 out:
380 cmnd->result = ScsiResult(host_status, scsi_status);
381}
382
383static void
384lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
385 struct lpfc_iocbq *pIocbOut)
386{
387 struct lpfc_scsi_buf *lpfc_cmd =
388 (struct lpfc_scsi_buf *) pIocbIn->context1;
389 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
390 struct lpfc_nodelist *pnode = rdata->pnode;
391 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
392 unsigned long iflag;
393
394 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
395 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
396
397 if (lpfc_cmd->status) {
398 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
399 (lpfc_cmd->result & IOERR_DRVR_MASK))
400 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
401 else if (lpfc_cmd->status >= IOSTAT_CNT)
402 lpfc_cmd->status = IOSTAT_DEFAULT;
403
404 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
405 "%d:0729 FCP cmd x%x failed <%d/%d> status: "
406 "x%x result: x%x Data: x%x x%x\n",
407 phba->brd_no, cmd->cmnd[0], cmd->device->id,
408 cmd->device->lun, lpfc_cmd->status,
409 lpfc_cmd->result, pIocbOut->iocb.ulpContext,
410 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
411
412 switch (lpfc_cmd->status) {
413 case IOSTAT_FCP_RSP_ERROR:
414 /* Call FCP RSP handler to determine result */
415 lpfc_handle_fcp_err(lpfc_cmd);
416 break;
417 case IOSTAT_NPORT_BSY:
418 case IOSTAT_FABRIC_BSY:
419 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
420 break;
421 default:
422 cmd->result = ScsiResult(DID_ERROR, 0);
423 break;
424 }
425
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400426 if ((pnode == NULL )
427 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
428 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500429 } else {
430 cmd->result = ScsiResult(DID_OK, 0);
431 }
432
433 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
434 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
435
436 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
437 "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
438 "SNS x%x x%x Data: x%x x%x\n",
439 phba->brd_no, cmd->device->id,
440 cmd->device->lun, cmd, cmd->result,
441 *lp, *(lp + 3), cmd->retries, cmd->resid);
442 }
443
dea31012005-04-17 16:05:31 -0500444 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400445
446 spin_lock_irqsave(phba->host->host_lock, iflag);
447 lpfc_release_scsi_buf(phba, lpfc_cmd);
448 spin_unlock_irqrestore(phba->host->host_lock, iflag);
dea31012005-04-17 16:05:31 -0500449}
450
451static void
452lpfc_scsi_prep_cmnd(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd,
453 struct lpfc_nodelist *pnode)
454{
455 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
456 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
457 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
458 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
459 int datadir = scsi_cmnd->sc_data_direction;
460
461 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400462 /* clear task management bits */
463 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500464
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400465 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
466 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500467
468 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
469
470 if (scsi_cmnd->device->tagged_supported) {
471 switch (scsi_cmnd->tag) {
472 case HEAD_OF_QUEUE_TAG:
473 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
474 break;
475 case ORDERED_QUEUE_TAG:
476 fcp_cmnd->fcpCntl1 = ORDERED_Q;
477 break;
478 default:
479 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
480 break;
481 }
482 } else
483 fcp_cmnd->fcpCntl1 = 0;
484
485 /*
486 * There are three possibilities here - use scatter-gather segment, use
487 * the single mapping, or neither. Start the lpfc command prep by
488 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
489 * data bde entry.
490 */
491 if (scsi_cmnd->use_sg) {
492 if (datadir == DMA_TO_DEVICE) {
493 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
494 iocb_cmd->un.fcpi.fcpi_parm = 0;
495 iocb_cmd->ulpPU = 0;
496 fcp_cmnd->fcpCntl3 = WRITE_DATA;
497 phba->fc4OutputRequests++;
498 } else {
499 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
500 iocb_cmd->ulpPU = PARM_READ_CHECK;
501 iocb_cmd->un.fcpi.fcpi_parm =
502 scsi_cmnd->request_bufflen;
503 fcp_cmnd->fcpCntl3 = READ_DATA;
504 phba->fc4InputRequests++;
505 }
506 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
507 if (datadir == DMA_TO_DEVICE) {
508 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
509 iocb_cmd->un.fcpi.fcpi_parm = 0;
510 iocb_cmd->ulpPU = 0;
511 fcp_cmnd->fcpCntl3 = WRITE_DATA;
512 phba->fc4OutputRequests++;
513 } else {
514 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
515 iocb_cmd->ulpPU = PARM_READ_CHECK;
516 iocb_cmd->un.fcpi.fcpi_parm =
517 scsi_cmnd->request_bufflen;
518 fcp_cmnd->fcpCntl3 = READ_DATA;
519 phba->fc4InputRequests++;
520 }
521 } else {
522 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
523 iocb_cmd->un.fcpi.fcpi_parm = 0;
524 iocb_cmd->ulpPU = 0;
525 fcp_cmnd->fcpCntl3 = 0;
526 phba->fc4ControlRequests++;
527 }
528
529 /*
530 * Finish initializing those IOCB fields that are independent
531 * of the scsi_cmnd request_buffer
532 */
533 piocbq->iocb.ulpContext = pnode->nlp_rpi;
534 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
535 piocbq->iocb.ulpFCP2Rcvy = 1;
536
537 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
538 piocbq->context1 = lpfc_cmd;
539 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
540 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
541}
542
543static int
544lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
545 struct lpfc_scsi_buf *lpfc_cmd,
546 uint8_t task_mgmt_cmd)
547{
548 struct lpfc_sli *psli;
549 struct lpfc_iocbq *piocbq;
550 IOCB_t *piocb;
551 struct fcp_cmnd *fcp_cmnd;
552 struct scsi_device *scsi_dev = lpfc_cmd->pCmd->device;
553 struct lpfc_rport_data *rdata = scsi_dev->hostdata;
554 struct lpfc_nodelist *ndlp = rdata->pnode;
555
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400556 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea31012005-04-17 16:05:31 -0500557 return 0;
558 }
559
560 psli = &phba->sli;
561 piocbq = &(lpfc_cmd->cur_iocbq);
562 piocb = &piocbq->iocb;
563
564 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400565 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
566 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500567 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
568
569 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
570
571 piocb->ulpContext = ndlp->nlp_rpi;
572 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
573 piocb->ulpFCP2Rcvy = 1;
574 }
575 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
576
577 /* ulpTimeout is only one byte */
578 if (lpfc_cmd->timeout > 0xff) {
579 /*
580 * Do not timeout the command at the firmware level.
581 * The driver will provide the timeout mechanism.
582 */
583 piocb->ulpTimeout = 0;
584 } else {
585 piocb->ulpTimeout = lpfc_cmd->timeout;
586 }
587
588 lpfc_cmd->rdata = rdata;
589
590 switch (task_mgmt_cmd) {
591 case FCP_LUN_RESET:
592 /* Issue LUN Reset to TGT <num> LUN <num> */
593 lpfc_printf_log(phba,
594 KERN_INFO,
595 LOG_FCP,
596 "%d:0703 Issue LUN Reset to TGT %d LUN %d "
597 "Data: x%x x%x\n",
598 phba->brd_no,
599 scsi_dev->id, scsi_dev->lun,
600 ndlp->nlp_rpi, ndlp->nlp_flag);
601
602 break;
603 case FCP_ABORT_TASK_SET:
604 /* Issue Abort Task Set to TGT <num> LUN <num> */
605 lpfc_printf_log(phba,
606 KERN_INFO,
607 LOG_FCP,
608 "%d:0701 Issue Abort Task Set to TGT %d LUN %d "
609 "Data: x%x x%x\n",
610 phba->brd_no,
611 scsi_dev->id, scsi_dev->lun,
612 ndlp->nlp_rpi, ndlp->nlp_flag);
613
614 break;
615 case FCP_TARGET_RESET:
616 /* Issue Target Reset to TGT <num> */
617 lpfc_printf_log(phba,
618 KERN_INFO,
619 LOG_FCP,
620 "%d:0702 Issue Target Reset to TGT %d "
621 "Data: x%x x%x\n",
622 phba->brd_no,
623 scsi_dev->id, ndlp->nlp_rpi,
624 ndlp->nlp_flag);
625 break;
626 }
627
628 return (1);
629}
630
631static int
632lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba)
633{
634 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400635 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500636 int ret;
637
638 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_TARGET_RESET);
639 if (!ret)
640 return FAILED;
641
642 lpfc_cmd->scsi_hba = phba;
643 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400644 iocbqrsp = lpfc_sli_get_iocbq(phba);
645
dea31012005-04-17 16:05:31 -0500646 if (!iocbqrsp)
647 return FAILED;
dea31012005-04-17 16:05:31 -0500648
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400649 ret = lpfc_sli_issue_iocb_wait(phba,
650 &phba->sli.ring[phba->sli.fcp_ring],
651 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500652 if (ret != IOCB_SUCCESS) {
653 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
654 ret = FAILED;
655 } else {
656 ret = SUCCESS;
657 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
658 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
659 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
660 (lpfc_cmd->result & IOERR_DRVR_MASK))
661 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
662 }
663
James Bottomley604a3e32005-10-29 10:28:33 -0500664 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500665 return ret;
666}
667
dea31012005-04-17 16:05:31 -0500668const char *
669lpfc_info(struct Scsi_Host *host)
670{
671 struct lpfc_hba *phba = (struct lpfc_hba *) host->hostdata[0];
672 int len;
673 static char lpfcinfobuf[384];
674
675 memset(lpfcinfobuf,0,384);
676 if (phba && phba->pcidev){
677 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
678 len = strlen(lpfcinfobuf);
679 snprintf(lpfcinfobuf + len,
680 384-len,
681 " on PCI bus %02x device %02x irq %d",
682 phba->pcidev->bus->number,
683 phba->pcidev->devfn,
684 phba->pcidev->irq);
685 len = strlen(lpfcinfobuf);
686 if (phba->Port[0]) {
687 snprintf(lpfcinfobuf + len,
688 384-len,
689 " port %s",
690 phba->Port);
691 }
692 }
693 return lpfcinfobuf;
694}
695
696static int
697lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
698{
699 struct lpfc_hba *phba =
700 (struct lpfc_hba *) cmnd->device->host->hostdata[0];
701 struct lpfc_sli *psli = &phba->sli;
702 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
703 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400704 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400705 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400706 int err;
dea31012005-04-17 16:05:31 -0500707
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400708 err = fc_remote_port_chkready(rport);
709 if (err) {
710 cmnd->result = err;
dea31012005-04-17 16:05:31 -0500711 goto out_fail_command;
712 }
713
714 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400715 * Catch race where our node has transitioned, but the
716 * transport is still transitioning.
dea31012005-04-17 16:05:31 -0500717 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400718 if (!ndlp) {
719 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
720 goto out_fail_command;
dea31012005-04-17 16:05:31 -0500721 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400722 lpfc_cmd = lpfc_sli_get_scsi_buf (phba);
dea31012005-04-17 16:05:31 -0500723 if (lpfc_cmd == NULL) {
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500724 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
725 "%d:0707 driver's buffer pool is empty, "
726 "IO busied\n", phba->brd_no);
dea31012005-04-17 16:05:31 -0500727 goto out_host_busy;
728 }
729
730 /*
731 * Store the midlayer's command structure for the completion phase
732 * and complete the command initialization.
733 */
734 lpfc_cmd->pCmd = cmnd;
735 lpfc_cmd->rdata = rdata;
736 lpfc_cmd->timeout = 0;
737 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
738 cmnd->scsi_done = done;
739
740 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
741 if (err)
742 goto out_host_busy_free_buf;
743
744 lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
745
746 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
747 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
748 if (err)
749 goto out_host_busy_free_buf;
750 return 0;
751
752 out_host_busy_free_buf:
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400753 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500754 cmnd->host_scribble = NULL;
755 out_host_busy:
756 return SCSI_MLQUEUE_HOST_BUSY;
757
758 out_fail_command:
759 done(cmnd);
760 return 0;
761}
762
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500763
dea31012005-04-17 16:05:31 -0500764static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500765lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500766{
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500767 struct Scsi_Host *shost = cmnd->device->host;
768 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
dea31012005-04-17 16:05:31 -0500769 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400770 struct lpfc_iocbq *iocb;
771 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -0500772 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -0500773 IOCB_t *cmd, *icmd;
dea31012005-04-17 16:05:31 -0500774 unsigned int loop_count = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400775 int ret = SUCCESS;
776
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500777 spin_lock_irq(shost->host_lock);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400778
779 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
780 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -0500781
782 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400783 * If pCmd field of the corresponding lpfc_scsi_buf structure
784 * points to a different SCSI command, then the driver has
785 * already completed this command, but the midlayer did not
786 * see the completion before the eh fired. Just return
787 * SUCCESS.
dea31012005-04-17 16:05:31 -0500788 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400789 iocb = &lpfc_cmd->cur_iocbq;
790 if (lpfc_cmd->pCmd != cmnd)
791 goto out;
dea31012005-04-17 16:05:31 -0500792
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400793 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -0500794
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400795 abtsiocb = lpfc_sli_get_iocbq(phba);
796 if (abtsiocb == NULL) {
797 ret = FAILED;
dea31012005-04-17 16:05:31 -0500798 goto out;
799 }
800
dea31012005-04-17 16:05:31 -0500801 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400802 * The scsi command can not be in txq and it is in flight because the
803 * pCmd is still pointig at the SCSI command we have to abort. There
804 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -0500805 */
dea31012005-04-17 16:05:31 -0500806
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400807 cmd = &iocb->iocb;
808 icmd = &abtsiocb->iocb;
809 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
810 icmd->un.acxri.abortContextTag = cmd->ulpContext;
811 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -0500812
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400813 icmd->ulpLe = 1;
814 icmd->ulpClass = cmd->ulpClass;
815 if (phba->hba_state >= LPFC_LINK_UP)
816 icmd->ulpCommand = CMD_ABORT_XRI_CN;
817 else
818 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -0500819
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400820 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
821 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
822 lpfc_sli_release_iocbq(phba, abtsiocb);
823 ret = FAILED;
824 goto out;
825 }
826
827 /* Wait for abort to complete */
828 while (lpfc_cmd->pCmd == cmnd)
829 {
830 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800831 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT*HZ);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400832 spin_lock_irq(phba->host->host_lock);
833 if (++loop_count
834 > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
dea31012005-04-17 16:05:31 -0500835 break;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400836 }
dea31012005-04-17 16:05:31 -0500837
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400838 if (lpfc_cmd->pCmd == cmnd) {
839 ret = FAILED;
840 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
841 "%d:0748 abort handler timed out waiting for "
842 "abort to complete: ret %#x, ID %d, LUN %d, "
843 "snum %#lx\n",
844 phba->brd_no, ret, cmnd->device->id,
845 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -0500846 }
847
848 out:
849 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500850 "%d:0749 SCSI Layer I/O Abort Request "
851 "Status x%x ID %d LUN %d snum %#lx\n",
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400852 phba->brd_no, ret, cmnd->device->id,
853 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -0500854
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500855 spin_unlock_irq(shost->host_lock);
856
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400857 return ret;
dea31012005-04-17 16:05:31 -0500858}
859
860static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500861lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500862{
863 struct Scsi_Host *shost = cmnd->device->host;
864 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400865 struct lpfc_scsi_buf *lpfc_cmd;
866 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -0500867 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
868 struct lpfc_nodelist *pnode = rdata->pnode;
869 int ret = FAILED;
870 int cnt, loopcnt;
871
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500872 spin_lock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500873 /*
874 * If target is not in a MAPPED state, delay the reset until
875 * target is rediscovered or nodev timeout expires.
876 */
877 while ( 1 ) {
878 if (!pnode)
879 break;
880
881 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
882 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800883 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
dea31012005-04-17 16:05:31 -0500884 spin_lock_irq(phba->host->host_lock);
885 }
886 if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
887 break;
888 }
889
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400890 lpfc_cmd = lpfc_sli_get_scsi_buf (phba);
dea31012005-04-17 16:05:31 -0500891 if (lpfc_cmd == NULL)
892 goto out;
893
894 lpfc_cmd->pCmd = cmnd;
895 lpfc_cmd->timeout = 60;
896 lpfc_cmd->scsi_hba = phba;
897
898 ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
899 if (!ret)
900 goto out_free_scsi_buf;
901
902 iocbq = &lpfc_cmd->cur_iocbq;
903
904 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400905 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -0500906 if (iocbqrsp == NULL)
907 goto out_free_scsi_buf;
908
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400909 ret = lpfc_sli_issue_iocb_wait(phba,
910 &phba->sli.ring[phba->sli.fcp_ring],
911 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500912 if (ret == IOCB_SUCCESS)
913 ret = SUCCESS;
914
915 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
916 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
917 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT)
918 if (lpfc_cmd->result & IOERR_DRVR_MASK)
919 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
920
921 /*
922 * All outstanding txcmplq I/Os should have been aborted by the target.
923 * Unfortunately, some targets do not abide by this forcing the driver
924 * to double check.
925 */
926 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
927 cmnd->device->id, cmnd->device->lun, 0,
928 LPFC_CTX_LUN);
929
930 loopcnt = 0;
931 while((cnt = lpfc_sli_sum_iocb(phba,
932 &phba->sli.ring[phba->sli.fcp_ring],
933 cmnd->device->id, cmnd->device->lun,
934 LPFC_CTX_LUN))) {
935 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -0800936 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -0500937 spin_lock_irq(phba->host->host_lock);
938
939 if (++loopcnt
940 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
941 break;
942 }
943
944 if (cnt) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400945 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea31012005-04-17 16:05:31 -0500946 "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
947 phba->brd_no, cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400948 ret = FAILED;
dea31012005-04-17 16:05:31 -0500949 }
950
James Bottomley604a3e32005-10-29 10:28:33 -0500951 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500952
953out_free_scsi_buf:
954 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
955 "%d:0713 SCSI layer issued LUN reset (%d, %d) "
956 "Data: x%x x%x x%x\n",
957 phba->brd_no, lpfc_cmd->pCmd->device->id,
958 lpfc_cmd->pCmd->device->lun, ret, lpfc_cmd->status,
959 lpfc_cmd->result);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400960 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500961out:
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500962 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -0500963 return ret;
964}
965
Jeff Garzik 94d0e7b2005-05-28 07:55:48 -0400966static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500967lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500968{
969 struct Scsi_Host *shost = cmnd->device->host;
970 struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
971 struct lpfc_nodelist *ndlp = NULL;
972 int match;
973 int ret = FAILED, i, err_count = 0;
974 int cnt, loopcnt;
975 unsigned int midlayer_id = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400976 struct lpfc_scsi_buf * lpfc_cmd;
dea31012005-04-17 16:05:31 -0500977
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500978 spin_lock_irq(shost->host_lock);
979
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400980 lpfc_cmd = lpfc_sli_get_scsi_buf (phba);
dea31012005-04-17 16:05:31 -0500981 if (lpfc_cmd == NULL)
982 goto out;
983
984 /* The lpfc_cmd storage is reused. Set all loop invariants. */
985 lpfc_cmd->timeout = 60;
986 lpfc_cmd->pCmd = cmnd;
987 lpfc_cmd->scsi_hba = phba;
988
989 /*
990 * Since the driver manages a single bus device, reset all
991 * targets known to the driver. Should any target reset
992 * fail, this routine returns failure to the midlayer.
993 */
994 midlayer_id = cmnd->device->id;
995 for (i = 0; i < MAX_FCP_TARGET; i++) {
996 /* Search the mapped list for this target ID */
997 match = 0;
998 list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
999 if ((i == ndlp->nlp_sid) && ndlp->rport) {
1000 match = 1;
1001 break;
1002 }
1003 }
1004 if (!match)
1005 continue;
1006
1007 lpfc_cmd->pCmd->device->id = i;
1008 lpfc_cmd->pCmd->device->hostdata = ndlp->rport->dd_data;
1009 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
1010 if (ret != SUCCESS) {
1011 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1012 "%d:0713 Bus Reset on target %d failed\n",
1013 phba->brd_no, i);
1014 err_count++;
1015 }
1016 }
1017
1018 cmnd->device->id = midlayer_id;
1019 loopcnt = 0;
1020 while((cnt = lpfc_sli_sum_iocb(phba,
1021 &phba->sli.ring[phba->sli.fcp_ring],
1022 0, 0, LPFC_CTX_HOST))) {
1023 spin_unlock_irq(phba->host->host_lock);
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001024 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001025 spin_lock_irq(phba->host->host_lock);
1026
1027 if (++loopcnt
1028 > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1029 break;
1030 }
1031
1032 if (cnt) {
1033 /* flush all outstanding commands on the host */
1034 i = lpfc_sli_abort_iocb(phba,
1035 &phba->sli.ring[phba->sli.fcp_ring], 0, 0, 0,
1036 LPFC_CTX_HOST);
1037
1038 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1039 "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1040 phba->brd_no, cnt, i);
1041 }
1042
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001043 if (cnt == 0)
dea31012005-04-17 16:05:31 -05001044 ret = SUCCESS;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001045 else
1046 ret = FAILED;
dea31012005-04-17 16:05:31 -05001047
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001048 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001049 lpfc_printf_log(phba,
1050 KERN_ERR,
1051 LOG_FCP,
1052 "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1053 phba->brd_no, ret);
1054out:
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001055 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001056 return ret;
1057}
1058
1059static int
dea31012005-04-17 16:05:31 -05001060lpfc_slave_alloc(struct scsi_device *sdev)
1061{
1062 struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata[0];
dea31012005-04-17 16:05:31 -05001063 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001064 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001065 uint32_t total = 0, i;
1066 uint32_t num_to_alloc = 0;
1067 unsigned long flags;
dea31012005-04-17 16:05:31 -05001068
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001069 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001070 return -ENXIO;
1071
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001072 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001073
1074 /*
1075 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1076 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001077 * HBA limit conveyed to the midlayer via the host structure. The
1078 * formula accounts for the lun_queue_depth + error handlers + 1
1079 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001080 */
1081 total = phba->total_scsi_bufs;
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001082 num_to_alloc = phba->cfg_lun_queue_depth + 2;
dea31012005-04-17 16:05:31 -05001083 if (total >= phba->cfg_hba_queue_depth) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001084 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1085 "%d:0704 At limitation of %d preallocated "
1086 "command buffers\n", phba->brd_no, total);
dea31012005-04-17 16:05:31 -05001087 return 0;
1088 } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001089 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1090 "%d:0705 Allocation request of %d command "
1091 "buffers will exceed max of %d. Reducing "
1092 "allocation request to %d.\n", phba->brd_no,
1093 num_to_alloc, phba->cfg_hba_queue_depth,
1094 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001095 num_to_alloc = phba->cfg_hba_queue_depth - total;
1096 }
1097
1098 for (i = 0; i < num_to_alloc; i++) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001099 scsi_buf = lpfc_new_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001100 if (!scsi_buf) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001101 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1102 "%d:0706 Failed to allocate command "
1103 "buffer\n", phba->brd_no);
dea31012005-04-17 16:05:31 -05001104 break;
1105 }
1106
1107 spin_lock_irqsave(phba->host->host_lock, flags);
1108 phba->total_scsi_bufs++;
1109 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
1110 spin_unlock_irqrestore(phba->host->host_lock, flags);
1111 }
1112 return 0;
1113}
1114
1115static int
1116lpfc_slave_configure(struct scsi_device *sdev)
1117{
1118 struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata[0];
1119 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1120
1121 if (sdev->tagged_supported)
1122 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1123 else
1124 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1125
1126 /*
1127 * Initialize the fc transport attributes for the target
1128 * containing this scsi device. Also note that the driver's
1129 * target pointer is stored in the starget_data for the
1130 * driver's sysfs entry point functions.
1131 */
1132 rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
1133
1134 return 0;
1135}
1136
1137static void
1138lpfc_slave_destroy(struct scsi_device *sdev)
1139{
1140 sdev->hostdata = NULL;
1141 return;
1142}
1143
1144struct scsi_host_template lpfc_template = {
1145 .module = THIS_MODULE,
1146 .name = LPFC_DRIVER_NAME,
1147 .info = lpfc_info,
1148 .queuecommand = lpfc_queuecommand,
1149 .eh_abort_handler = lpfc_abort_handler,
1150 .eh_device_reset_handler= lpfc_reset_lun_handler,
1151 .eh_bus_reset_handler = lpfc_reset_bus_handler,
1152 .slave_alloc = lpfc_slave_alloc,
1153 .slave_configure = lpfc_slave_configure,
1154 .slave_destroy = lpfc_slave_destroy,
1155 .this_id = -1,
1156 .sg_tablesize = LPFC_SG_SEG_CNT,
1157 .cmd_per_lun = LPFC_CMD_PER_LUN,
1158 .use_clustering = ENABLE_CLUSTERING,
1159 .shost_attrs = lpfc_host_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001160 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001161};