blob: 90c88733a4f5fd69263cd0c66761ba2b32bebf55 [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. *
James Smart9413aff2007-04-25 09:53:35 -04004 * Copyright (C) 2004-2007 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/pci.h>
23#include <linux/interrupt.h>
James Smarta90f5682006-08-17 11:58:04 -040024#include <linux/delay.h>
dea31012005-04-17 16:05:31 -050025
26#include <scsi/scsi.h>
27#include <scsi/scsi_device.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_tcq.h>
30#include <scsi/scsi_transport_fc.h>
31
32#include "lpfc_version.h"
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
35#include "lpfc_disc.h"
36#include "lpfc_scsi.h"
37#include "lpfc.h"
38#include "lpfc_logmsg.h"
39#include "lpfc_crtn.h"
40
41#define LPFC_RESET_WAIT 2
42#define LPFC_ABORT_WAIT 2
43
dea31012005-04-17 16:05:31 -050044/*
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 Smart2e0fef82007-06-17 19:56:36 -050053lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -050054{
James Smart2e0fef82007-06-17 19:56:36 -050055 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -050056 struct lpfc_scsi_buf *psb;
57 struct ulp_bde64 *bpl;
58 IOCB_t *iocb;
59 dma_addr_t pdma_phys;
James Bottomley604a3e32005-10-29 10:28:33 -050060 uint16_t iotag;
dea31012005-04-17 16:05:31 -050061
62 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
63 if (!psb)
64 return NULL;
65 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
dea31012005-04-17 16:05:31 -050066
67 /*
68 * Get memory from the pci pool to map the virt space to pci bus space
69 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
70 * struct fcp_rsp and the number of bde's necessary to support the
71 * sg_tablesize.
72 */
73 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
74 &psb->dma_handle);
75 if (!psb->data) {
76 kfree(psb);
77 return NULL;
78 }
79
80 /* Initialize virtual ptrs to dma_buf region. */
81 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
82
James Bottomley604a3e32005-10-29 10:28:33 -050083 /* Allocate iotag for psb->cur_iocbq. */
84 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
85 if (iotag == 0) {
86 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
87 psb->data, psb->dma_handle);
88 kfree (psb);
89 return NULL;
90 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -040091 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -050092
dea31012005-04-17 16:05:31 -050093 psb->fcp_cmnd = psb->data;
94 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
95 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
96 sizeof(struct fcp_rsp);
97
98 /* Initialize local short-hand pointers. */
99 bpl = psb->fcp_bpl;
100 pdma_phys = psb->dma_handle;
101
102 /*
103 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
104 * list bdes. Initialize the first two and leave the rest for
105 * queuecommand.
106 */
107 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
108 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
109 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
110 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
111 bpl->tus.w = le32_to_cpu(bpl->tus.w);
112 bpl++;
113
114 /* Setup the physical region for the FCP RSP */
115 pdma_phys += sizeof (struct fcp_cmnd);
116 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
117 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
118 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
119 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
120 bpl->tus.w = le32_to_cpu(bpl->tus.w);
121
122 /*
123 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
124 * initialize it with all known data now.
125 */
126 pdma_phys += (sizeof (struct fcp_rsp));
127 iocb = &psb->cur_iocbq.iocb;
128 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
129 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
130 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
131 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
132 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
133 iocb->ulpBdeCount = 1;
134 iocb->ulpClass = CLASS3;
135
136 return psb;
137}
138
Adrian Bunk455c53e2006-01-06 20:21:28 +0100139static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500140lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500141{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400142 struct lpfc_scsi_buf * lpfc_cmd = NULL;
143 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500144 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500145
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500146 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400147 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400148 if (lpfc_cmd) {
149 lpfc_cmd->seg_cnt = 0;
150 lpfc_cmd->nonsg_phys = 0;
151 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500152 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400153 return lpfc_cmd;
154}
155
156static void
157lpfc_release_scsi_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
158{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500159 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500160
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500161 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400162 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500163 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500164 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500165}
166
167static int
168lpfc_scsi_prep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
169{
170 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
171 struct scatterlist *sgel = NULL;
172 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
173 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
174 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
175 dma_addr_t physaddr;
176 uint32_t i, num_bde = 0;
177 int datadir = scsi_cmnd->sc_data_direction;
178 int dma_error;
179
180 /*
181 * There are three possibilities here - use scatter-gather segment, use
182 * the single mapping, or neither. Start the lpfc command prep by
183 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
184 * data bde entry.
185 */
186 bpl += 2;
187 if (scsi_cmnd->use_sg) {
188 /*
189 * The driver stores the segment count returned from pci_map_sg
190 * because this a count of dma-mappings used to map the use_sg
191 * pages. They are not guaranteed to be the same for those
192 * architectures that implement an IOMMU.
193 */
194 sgel = (struct scatterlist *)scsi_cmnd->request_buffer;
195 lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
196 scsi_cmnd->use_sg, datadir);
197 if (lpfc_cmd->seg_cnt == 0)
198 return 1;
199
200 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
201 printk(KERN_ERR "%s: Too many sg segments from "
202 "dma_map_sg. Config %d, seg_cnt %d",
203 __FUNCTION__, phba->cfg_sg_seg_cnt,
204 lpfc_cmd->seg_cnt);
205 dma_unmap_sg(&phba->pcidev->dev, sgel,
206 lpfc_cmd->seg_cnt, datadir);
207 return 1;
208 }
209
210 /*
211 * The driver established a maximum scatter-gather segment count
212 * during probe that limits the number of sg elements in any
213 * single scsi command. Just run through the seg_cnt and format
214 * the bde's.
215 */
216 for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
217 physaddr = sg_dma_address(sgel);
218 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
219 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
220 bpl->tus.f.bdeSize = sg_dma_len(sgel);
221 if (datadir == DMA_TO_DEVICE)
222 bpl->tus.f.bdeFlags = 0;
223 else
224 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
225 bpl->tus.w = le32_to_cpu(bpl->tus.w);
226 bpl++;
227 sgel++;
228 num_bde++;
229 }
230 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
231 physaddr = dma_map_single(&phba->pcidev->dev,
232 scsi_cmnd->request_buffer,
233 scsi_cmnd->request_bufflen,
234 datadir);
235 dma_error = dma_mapping_error(physaddr);
236 if (dma_error) {
237 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
238 "%d:0718 Unable to dma_map_single "
239 "request_buffer: x%x\n",
240 phba->brd_no, dma_error);
241 return 1;
242 }
243
244 lpfc_cmd->nonsg_phys = physaddr;
245 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
246 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
247 bpl->tus.f.bdeSize = scsi_cmnd->request_bufflen;
248 if (datadir == DMA_TO_DEVICE)
249 bpl->tus.f.bdeFlags = 0;
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400250 else
251 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
dea31012005-04-17 16:05:31 -0500252 bpl->tus.w = le32_to_cpu(bpl->tus.w);
253 num_bde = 1;
254 bpl++;
255 }
256
257 /*
258 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400259 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
260 * reinitialized since all iocb memory resources are used many times
261 * for transmit, receive, and continuation bpl's.
dea31012005-04-17 16:05:31 -0500262 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400263 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500264 iocb_cmd->un.fcpi64.bdl.bdeSize +=
265 (num_bde * sizeof (struct ulp_bde64));
266 iocb_cmd->ulpBdeCount = 1;
267 iocb_cmd->ulpLe = 1;
268 fcp_cmnd->fcpDl = be32_to_cpu(scsi_cmnd->request_bufflen);
269 return 0;
270}
271
272static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400273lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
274{
275 /*
276 * There are only two special cases to consider. (1) the scsi command
277 * requested scatter-gather usage or (2) the scsi command allocated
278 * a request buffer, but did not request use_sg. There is a third
279 * case, but it does not require resource deallocation.
280 */
281 if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
282 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
283 psb->seg_cnt, psb->pCmd->sc_data_direction);
284 } else {
285 if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
286 dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
287 psb->pCmd->request_bufflen,
288 psb->pCmd->sc_data_direction);
289 }
290 }
291}
292
293static void
James Smart2e0fef82007-06-17 19:56:36 -0500294lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
295 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500296{
297 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
298 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
299 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart2e0fef82007-06-17 19:56:36 -0500300 struct lpfc_hba *phba = vport->phba;
James Smart7054a602007-04-25 09:52:34 -0400301 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -0500302 uint32_t resp_info = fcprsp->rspStatus2;
303 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500304 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500305 uint32_t host_status = DID_OK;
306 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500307 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500308
309 /*
310 * If this is a task management command, there is no
311 * scsi packet associated with this lpfc_cmd. The driver
312 * consumes it.
313 */
314 if (fcpcmd->fcpCntl2) {
315 scsi_status = 0;
316 goto out;
317 }
318
James Smartc7743952006-12-02 13:34:42 -0500319 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 if (resp_info & RSP_LEN_VALID)
325 rsplen = be32_to_cpu(fcprsp->rspRspLen);
326 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
327 }
328 lp = (uint32_t *)cmnd->sense_buffer;
329
330 if (!scsi_status && (resp_info & RESID_UNDER))
331 logit = LOG_FCP;
332
333 lpfc_printf_log(phba, KERN_WARNING, logit,
334 "%d:0730 FCP command x%x failed: x%x SNS x%x x%x "
335 "Data: x%x x%x x%x x%x x%x\n",
336 phba->brd_no, cmnd->cmnd[0], scsi_status,
337 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
dea31012005-04-17 16:05:31 -0500338 be32_to_cpu(fcprsp->rspResId),
339 be32_to_cpu(fcprsp->rspSnsLen),
340 be32_to_cpu(fcprsp->rspRspLen),
341 fcprsp->rspInfo3);
342
343 if (resp_info & RSP_LEN_VALID) {
344 rsplen = be32_to_cpu(fcprsp->rspRspLen);
345 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
346 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
347 host_status = DID_ERROR;
348 goto out;
349 }
350 }
351
dea31012005-04-17 16:05:31 -0500352 cmnd->resid = 0;
353 if (resp_info & RESID_UNDER) {
354 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
355
356 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
357 "%d:0716 FCP Read Underrun, expected %d, "
358 "residual %d Data: x%x x%x x%x\n", phba->brd_no,
359 be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
360 fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
361
362 /*
James Smart7054a602007-04-25 09:52:34 -0400363 * If there is an under run check if under run reported by
364 * storage array is same as the under run reported by HBA.
365 * If this is not same, there is a dropped frame.
366 */
367 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
368 fcpi_parm &&
369 (cmnd->resid != fcpi_parm)) {
370 lpfc_printf_log(phba, KERN_WARNING,
371 LOG_FCP | LOG_FCP_ERROR,
372 "%d:0735 FCP Read Check Error and Underrun "
373 "Data: x%x x%x x%x x%x\n", phba->brd_no,
374 be32_to_cpu(fcpcmd->fcpDl),
375 cmnd->resid,
376 fcpi_parm, cmnd->cmnd[0]);
377 cmnd->resid = cmnd->request_bufflen;
378 host_status = DID_ERROR;
379 }
380 /*
dea31012005-04-17 16:05:31 -0500381 * The cmnd->underflow is the minimum number of bytes that must
382 * be transfered for this command. Provided a sense condition
383 * is not present, make sure the actual amount transferred is at
384 * least the underflow value or fail.
385 */
386 if (!(resp_info & SNS_LEN_VALID) &&
387 (scsi_status == SAM_STAT_GOOD) &&
388 (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
389 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
390 "%d:0717 FCP command x%x residual "
391 "underrun converted to error "
392 "Data: x%x x%x x%x\n", phba->brd_no,
393 cmnd->cmnd[0], cmnd->request_bufflen,
394 cmnd->resid, cmnd->underflow);
395
396 host_status = DID_ERROR;
397 }
398 } else if (resp_info & RESID_OVER) {
399 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
400 "%d:0720 FCP command x%x residual "
401 "overrun error. Data: x%x x%x \n",
402 phba->brd_no, cmnd->cmnd[0],
403 cmnd->request_bufflen, cmnd->resid);
404 host_status = DID_ERROR;
405
406 /*
407 * Check SLI validation that all the transfer was actually done
408 * (fcpi_parm should be zero). Apply check only to reads.
409 */
410 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
411 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smartc7743952006-12-02 13:34:42 -0500412 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
dea31012005-04-17 16:05:31 -0500413 "%d:0734 FCP Read Check Error Data: "
414 "x%x x%x x%x x%x\n", phba->brd_no,
415 be32_to_cpu(fcpcmd->fcpDl),
416 be32_to_cpu(fcprsp->rspResId),
417 fcpi_parm, cmnd->cmnd[0]);
418 host_status = DID_ERROR;
419 cmnd->resid = cmnd->request_bufflen;
420 }
421
422 out:
423 cmnd->result = ScsiResult(host_status, scsi_status);
424}
425
426static void
427lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
428 struct lpfc_iocbq *pIocbOut)
429{
430 struct lpfc_scsi_buf *lpfc_cmd =
431 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500432 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500433 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
434 struct lpfc_nodelist *pnode = rdata->pnode;
435 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500436 int result;
437 struct scsi_device *sdev, *tmp_sdev;
438 int depth = 0;
dea31012005-04-17 16:05:31 -0500439
440 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
441 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
442
443 if (lpfc_cmd->status) {
444 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
445 (lpfc_cmd->result & IOERR_DRVR_MASK))
446 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
447 else if (lpfc_cmd->status >= IOSTAT_CNT)
448 lpfc_cmd->status = IOSTAT_DEFAULT;
449
450 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
451 "%d:0729 FCP cmd x%x failed <%d/%d> status: "
452 "x%x result: x%x Data: x%x x%x\n",
453 phba->brd_no, cmd->cmnd[0], cmd->device->id,
454 cmd->device->lun, lpfc_cmd->status,
455 lpfc_cmd->result, pIocbOut->iocb.ulpContext,
456 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
457
458 switch (lpfc_cmd->status) {
459 case IOSTAT_FCP_RSP_ERROR:
460 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500461 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500462 break;
463 case IOSTAT_NPORT_BSY:
464 case IOSTAT_FABRIC_BSY:
465 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
466 break;
467 default:
468 cmd->result = ScsiResult(DID_ERROR, 0);
469 break;
470 }
471
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400472 if ((pnode == NULL )
473 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
474 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500475 } else {
476 cmd->result = ScsiResult(DID_OK, 0);
477 }
478
479 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
480 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
481
482 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
483 "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
484 "SNS x%x x%x Data: x%x x%x\n",
485 phba->brd_no, cmd->device->id,
486 cmd->device->lun, cmd, cmd->result,
487 *lp, *(lp + 3), cmd->retries, cmd->resid);
488 }
489
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500490 result = cmd->result;
491 sdev = cmd->device;
James Smart1dcb58e2007-04-25 09:51:30 -0400492 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500493 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400494
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500495 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
496 lpfc_release_scsi_buf(phba, lpfc_cmd);
497 return;
498 }
499
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500500 if (!result && pnode != NULL &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500501 ((jiffies - pnode->last_ramp_up_time) >
502 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
503 ((jiffies - pnode->last_q_full_time) >
504 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
505 (phba->cfg_lun_queue_depth > sdev->queue_depth)) {
506 shost_for_each_device(tmp_sdev, sdev->host) {
507 if (phba->cfg_lun_queue_depth > tmp_sdev->queue_depth) {
508 if (tmp_sdev->id != sdev->id)
509 continue;
510 if (tmp_sdev->ordered_tags)
511 scsi_adjust_queue_depth(tmp_sdev,
512 MSG_ORDERED_TAG,
513 tmp_sdev->queue_depth+1);
514 else
515 scsi_adjust_queue_depth(tmp_sdev,
516 MSG_SIMPLE_TAG,
517 tmp_sdev->queue_depth+1);
518
519 pnode->last_ramp_up_time = jiffies;
520 }
521 }
522 }
523
524 /*
525 * Check for queue full. If the lun is reporting queue full, then
526 * back off the lun queue depth to prevent target overloads.
527 */
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500528 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500529 pnode->last_q_full_time = jiffies;
530
531 shost_for_each_device(tmp_sdev, sdev->host) {
532 if (tmp_sdev->id != sdev->id)
533 continue;
534 depth = scsi_track_queue_full(tmp_sdev,
535 tmp_sdev->queue_depth - 1);
536 }
537 /*
James Smart2e0fef82007-06-17 19:56:36 -0500538 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500539 * Modify the returned error code to store
540 * the final depth value set by
541 * scsi_track_queue_full.
542 */
543 if (depth == -1)
544 depth = sdev->host->cmd_per_lun;
545
546 if (depth) {
547 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
548 "%d:0711 detected queue full - lun queue depth "
549 " adjusted to %d.\n", phba->brd_no, depth);
550 }
551 }
552
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400553 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500554}
555
556static void
James Smart2e0fef82007-06-17 19:56:36 -0500557lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
558 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -0500559{
James Smart2e0fef82007-06-17 19:56:36 -0500560 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500561 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
562 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
563 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
564 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
565 int datadir = scsi_cmnd->sc_data_direction;
566
567 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400568 /* clear task management bits */
569 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500570
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400571 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
572 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500573
574 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
575
576 if (scsi_cmnd->device->tagged_supported) {
577 switch (scsi_cmnd->tag) {
578 case HEAD_OF_QUEUE_TAG:
579 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
580 break;
581 case ORDERED_QUEUE_TAG:
582 fcp_cmnd->fcpCntl1 = ORDERED_Q;
583 break;
584 default:
585 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
586 break;
587 }
588 } else
589 fcp_cmnd->fcpCntl1 = 0;
590
591 /*
592 * There are three possibilities here - use scatter-gather segment, use
593 * the single mapping, or neither. Start the lpfc command prep by
594 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
595 * data bde entry.
596 */
597 if (scsi_cmnd->use_sg) {
598 if (datadir == DMA_TO_DEVICE) {
599 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
600 iocb_cmd->un.fcpi.fcpi_parm = 0;
601 iocb_cmd->ulpPU = 0;
602 fcp_cmnd->fcpCntl3 = WRITE_DATA;
603 phba->fc4OutputRequests++;
604 } else {
605 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
606 iocb_cmd->ulpPU = PARM_READ_CHECK;
607 iocb_cmd->un.fcpi.fcpi_parm =
608 scsi_cmnd->request_bufflen;
609 fcp_cmnd->fcpCntl3 = READ_DATA;
610 phba->fc4InputRequests++;
611 }
612 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
613 if (datadir == DMA_TO_DEVICE) {
614 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
615 iocb_cmd->un.fcpi.fcpi_parm = 0;
616 iocb_cmd->ulpPU = 0;
617 fcp_cmnd->fcpCntl3 = WRITE_DATA;
618 phba->fc4OutputRequests++;
619 } else {
620 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
621 iocb_cmd->ulpPU = PARM_READ_CHECK;
622 iocb_cmd->un.fcpi.fcpi_parm =
623 scsi_cmnd->request_bufflen;
624 fcp_cmnd->fcpCntl3 = READ_DATA;
625 phba->fc4InputRequests++;
626 }
627 } else {
628 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
629 iocb_cmd->un.fcpi.fcpi_parm = 0;
630 iocb_cmd->ulpPU = 0;
631 fcp_cmnd->fcpCntl3 = 0;
632 phba->fc4ControlRequests++;
633 }
634
635 /*
636 * Finish initializing those IOCB fields that are independent
637 * of the scsi_cmnd request_buffer
638 */
639 piocbq->iocb.ulpContext = pnode->nlp_rpi;
640 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
641 piocbq->iocb.ulpFCP2Rcvy = 1;
642
643 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
644 piocbq->context1 = lpfc_cmd;
645 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
646 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500647 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -0500648}
649
650static int
James Smart2e0fef82007-06-17 19:56:36 -0500651lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -0500652 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -0400653 unsigned int lun,
dea31012005-04-17 16:05:31 -0500654 uint8_t task_mgmt_cmd)
655{
dea31012005-04-17 16:05:31 -0500656 struct lpfc_iocbq *piocbq;
657 IOCB_t *piocb;
658 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -0400659 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -0500660 struct lpfc_nodelist *ndlp = rdata->pnode;
661
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400662 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea31012005-04-17 16:05:31 -0500663 return 0;
664 }
665
dea31012005-04-17 16:05:31 -0500666 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -0500667 piocbq->vport = vport;
668
dea31012005-04-17 16:05:31 -0500669 piocb = &piocbq->iocb;
670
671 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart420b630d2006-07-06 15:50:16 -0400672 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500673 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
674
675 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
676
677 piocb->ulpContext = ndlp->nlp_rpi;
678 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
679 piocb->ulpFCP2Rcvy = 1;
680 }
681 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
682
683 /* ulpTimeout is only one byte */
684 if (lpfc_cmd->timeout > 0xff) {
685 /*
686 * Do not timeout the command at the firmware level.
687 * The driver will provide the timeout mechanism.
688 */
689 piocb->ulpTimeout = 0;
690 } else {
691 piocb->ulpTimeout = lpfc_cmd->timeout;
692 }
693
James Smart2e0fef82007-06-17 19:56:36 -0500694 return 1;
dea31012005-04-17 16:05:31 -0500695}
696
James Smart7054a602007-04-25 09:52:34 -0400697static void
698lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
699 struct lpfc_iocbq *cmdiocbq,
700 struct lpfc_iocbq *rspiocbq)
701{
702 struct lpfc_scsi_buf *lpfc_cmd =
703 (struct lpfc_scsi_buf *) cmdiocbq->context1;
704 if (lpfc_cmd)
705 lpfc_release_scsi_buf(phba, lpfc_cmd);
706 return;
707}
708
dea31012005-04-17 16:05:31 -0500709static int
James Smart2e0fef82007-06-17 19:56:36 -0500710lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -0400711 unsigned tgt_id, unsigned int lun,
712 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -0500713{
James Smart2e0fef82007-06-17 19:56:36 -0500714 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500715 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400716 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500717 int ret;
718
James Smartf5603512006-12-02 13:35:43 -0500719 if (!rdata->pnode)
720 return FAILED;
721
James Smart0b18ac42006-05-01 21:50:40 -0400722 lpfc_cmd->rdata = rdata;
James Smart2e0fef82007-06-17 19:56:36 -0500723 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -0400724 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -0500725 if (!ret)
726 return FAILED;
727
dea31012005-04-17 16:05:31 -0500728 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400729 iocbqrsp = lpfc_sli_get_iocbq(phba);
730
dea31012005-04-17 16:05:31 -0500731 if (!iocbqrsp)
732 return FAILED;
dea31012005-04-17 16:05:31 -0500733
James Smart0b18ac42006-05-01 21:50:40 -0400734 /* Issue Target Reset to TGT <num> */
735 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
736 "%d:0702 Issue Target Reset to TGT %d "
737 "Data: x%x x%x\n",
738 phba->brd_no, tgt_id, rdata->pnode->nlp_rpi,
739 rdata->pnode->nlp_flag);
740
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400741 ret = lpfc_sli_issue_iocb_wait(phba,
742 &phba->sli.ring[phba->sli.fcp_ring],
743 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500744 if (ret != IOCB_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -0400745 if (ret == IOCB_TIMEDOUT)
746 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
dea31012005-04-17 16:05:31 -0500747 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -0500748 } else {
749 ret = SUCCESS;
750 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
751 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
752 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
753 (lpfc_cmd->result & IOERR_DRVR_MASK))
754 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
755 }
756
James Bottomley604a3e32005-10-29 10:28:33 -0500757 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500758 return ret;
759}
760
dea31012005-04-17 16:05:31 -0500761const char *
762lpfc_info(struct Scsi_Host *host)
763{
James Smart2e0fef82007-06-17 19:56:36 -0500764 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
765 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500766 int len;
767 static char lpfcinfobuf[384];
768
769 memset(lpfcinfobuf,0,384);
770 if (phba && phba->pcidev){
771 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
772 len = strlen(lpfcinfobuf);
773 snprintf(lpfcinfobuf + len,
774 384-len,
775 " on PCI bus %02x device %02x irq %d",
776 phba->pcidev->bus->number,
777 phba->pcidev->devfn,
778 phba->pcidev->irq);
779 len = strlen(lpfcinfobuf);
780 if (phba->Port[0]) {
781 snprintf(lpfcinfobuf + len,
782 384-len,
783 " port %s",
784 phba->Port);
785 }
786 }
787 return lpfcinfobuf;
788}
789
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500790static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
791{
792 unsigned long poll_tmo_expires =
793 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
794
795 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
796 mod_timer(&phba->fcp_poll_timer,
797 poll_tmo_expires);
798}
799
800void lpfc_poll_start_timer(struct lpfc_hba * phba)
801{
802 lpfc_poll_rearm_timer(phba);
803}
804
805void lpfc_poll_timeout(unsigned long ptr)
806{
James Smart2e0fef82007-06-17 19:56:36 -0500807 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500808
809 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
810 lpfc_sli_poll_fcp_ring (phba);
811 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
812 lpfc_poll_rearm_timer(phba);
813 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500814}
815
dea31012005-04-17 16:05:31 -0500816static int
817lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
818{
James Smart2e0fef82007-06-17 19:56:36 -0500819 struct Scsi_Host *shost = cmnd->device->host;
820 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
821 struct lpfc_hba *phba = vport->phba;
822 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500823 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
824 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400825 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400826 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400827 int err;
dea31012005-04-17 16:05:31 -0500828
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400829 err = fc_remote_port_chkready(rport);
830 if (err) {
831 cmnd->result = err;
dea31012005-04-17 16:05:31 -0500832 goto out_fail_command;
833 }
834
835 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400836 * Catch race where our node has transitioned, but the
837 * transport is still transitioning.
dea31012005-04-17 16:05:31 -0500838 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400839 if (!ndlp) {
840 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
841 goto out_fail_command;
dea31012005-04-17 16:05:31 -0500842 }
James Smarted957682007-06-17 19:56:37 -0500843 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -0500844 if (lpfc_cmd == NULL) {
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500845 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
846 "%d:0707 driver's buffer pool is empty, "
847 "IO busied\n", phba->brd_no);
dea31012005-04-17 16:05:31 -0500848 goto out_host_busy;
849 }
850
851 /*
852 * Store the midlayer's command structure for the completion phase
853 * and complete the command initialization.
854 */
855 lpfc_cmd->pCmd = cmnd;
856 lpfc_cmd->rdata = rdata;
857 lpfc_cmd->timeout = 0;
858 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
859 cmnd->scsi_done = done;
860
861 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
862 if (err)
863 goto out_host_busy_free_buf;
864
James Smart2e0fef82007-06-17 19:56:36 -0500865 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -0500866
867 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
868 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
869 if (err)
870 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500871
872 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
873 lpfc_sli_poll_fcp_ring(phba);
874 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
875 lpfc_poll_rearm_timer(phba);
876 }
877
dea31012005-04-17 16:05:31 -0500878 return 0;
879
880 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -0400881 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400882 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500883 out_host_busy:
884 return SCSI_MLQUEUE_HOST_BUSY;
885
886 out_fail_command:
887 done(cmnd);
888 return 0;
889}
890
James Smarta90f5682006-08-17 11:58:04 -0400891static void
892lpfc_block_error_handler(struct scsi_cmnd *cmnd)
893{
894 struct Scsi_Host *shost = cmnd->device->host;
895 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
896
897 spin_lock_irq(shost->host_lock);
898 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
899 spin_unlock_irq(shost->host_lock);
900 msleep(1000);
901 spin_lock_irq(shost->host_lock);
902 }
903 spin_unlock_irq(shost->host_lock);
904 return;
905}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500906
dea31012005-04-17 16:05:31 -0500907static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -0500908lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -0500909{
James Smart2e0fef82007-06-17 19:56:36 -0500910 struct Scsi_Host *shost = cmnd->device->host;
911 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
912 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500913 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400914 struct lpfc_iocbq *iocb;
915 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -0500916 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -0500917 IOCB_t *cmd, *icmd;
dea31012005-04-17 16:05:31 -0500918 unsigned int loop_count = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400919 int ret = SUCCESS;
920
James Smarta90f5682006-08-17 11:58:04 -0400921 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400922 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
923 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -0500924
925 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400926 * If pCmd field of the corresponding lpfc_scsi_buf structure
927 * points to a different SCSI command, then the driver has
928 * already completed this command, but the midlayer did not
929 * see the completion before the eh fired. Just return
930 * SUCCESS.
dea31012005-04-17 16:05:31 -0500931 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400932 iocb = &lpfc_cmd->cur_iocbq;
933 if (lpfc_cmd->pCmd != cmnd)
934 goto out;
dea31012005-04-17 16:05:31 -0500935
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400936 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -0500937
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400938 abtsiocb = lpfc_sli_get_iocbq(phba);
939 if (abtsiocb == NULL) {
940 ret = FAILED;
dea31012005-04-17 16:05:31 -0500941 goto out;
942 }
943
dea31012005-04-17 16:05:31 -0500944 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400945 * The scsi command can not be in txq and it is in flight because the
946 * pCmd is still pointig at the SCSI command we have to abort. There
947 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -0500948 */
dea31012005-04-17 16:05:31 -0500949
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400950 cmd = &iocb->iocb;
951 icmd = &abtsiocb->iocb;
952 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
953 icmd->un.acxri.abortContextTag = cmd->ulpContext;
954 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -0500955
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400956 icmd->ulpLe = 1;
957 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -0500958 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400959 icmd->ulpCommand = CMD_ABORT_XRI_CN;
960 else
961 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -0500962
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400963 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -0500964 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400965 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
966 lpfc_sli_release_iocbq(phba, abtsiocb);
967 ret = FAILED;
968 goto out;
969 }
970
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500971 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
972 lpfc_sli_poll_fcp_ring (phba);
973
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400974 /* Wait for abort to complete */
975 while (lpfc_cmd->pCmd == cmnd)
976 {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500977 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
978 lpfc_sli_poll_fcp_ring (phba);
979
James Smart2e0fef82007-06-17 19:56:36 -0500980 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT * HZ);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400981 if (++loop_count
James Smartc01f3202006-08-18 17:47:08 -0400982 > (2 * phba->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
dea31012005-04-17 16:05:31 -0500983 break;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400984 }
dea31012005-04-17 16:05:31 -0500985
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400986 if (lpfc_cmd->pCmd == cmnd) {
987 ret = FAILED;
988 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
989 "%d:0748 abort handler timed out waiting for "
990 "abort to complete: ret %#x, ID %d, LUN %d, "
991 "snum %#lx\n",
992 phba->brd_no, ret, cmnd->device->id,
993 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -0500994 }
995
996 out:
997 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500998 "%d:0749 SCSI Layer I/O Abort Request "
999 "Status x%x ID %d LUN %d snum %#lx\n",
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001000 phba->brd_no, ret, cmnd->device->id,
1001 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001002
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001003 return ret;
dea31012005-04-17 16:05:31 -05001004}
1005
1006static int
James Smart7054a602007-04-25 09:52:34 -04001007lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001008{
James Smart2e0fef82007-06-17 19:56:36 -05001009 struct Scsi_Host *shost = cmnd->device->host;
1010 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1011 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001012 struct lpfc_scsi_buf *lpfc_cmd;
1013 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001014 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1015 struct lpfc_nodelist *pnode = rdata->pnode;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001016 uint32_t cmd_result = 0, cmd_status = 0;
dea31012005-04-17 16:05:31 -05001017 int ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001018 int iocb_status = IOCB_SUCCESS;
dea31012005-04-17 16:05:31 -05001019 int cnt, loopcnt;
1020
James Smarta90f5682006-08-17 11:58:04 -04001021 lpfc_block_error_handler(cmnd);
James Smartf5603512006-12-02 13:35:43 -05001022 loopcnt = 0;
dea31012005-04-17 16:05:31 -05001023 /*
1024 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001025 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001026 */
1027 while ( 1 ) {
1028 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001029 goto out;
dea31012005-04-17 16:05:31 -05001030
1031 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001032 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smartf5603512006-12-02 13:35:43 -05001033 loopcnt++;
1034 rdata = cmnd->device->hostdata;
1035 if (!rdata ||
1036 (loopcnt > ((phba->cfg_devloss_tmo * 2) + 1))) {
1037 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1038 "%d:0721 LUN Reset rport failure:"
1039 " cnt x%x rdata x%p\n",
1040 phba->brd_no, loopcnt, rdata);
1041 goto out;
1042 }
1043 pnode = rdata->pnode;
1044 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001045 goto out;
dea31012005-04-17 16:05:31 -05001046 }
James Smartf5603512006-12-02 13:35:43 -05001047 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001048 break;
1049 }
1050
James Smart2e0fef82007-06-17 19:56:36 -05001051 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001052 if (lpfc_cmd == NULL)
1053 goto out;
1054
dea31012005-04-17 16:05:31 -05001055 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001056 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001057
James Smart2e0fef82007-06-17 19:56:36 -05001058 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
James Smart7054a602007-04-25 09:52:34 -04001059 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05001060 if (!ret)
1061 goto out_free_scsi_buf;
1062
1063 iocbq = &lpfc_cmd->cur_iocbq;
1064
1065 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001066 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001067 if (iocbqrsp == NULL)
1068 goto out_free_scsi_buf;
1069
James Smart0b18ac42006-05-01 21:50:40 -04001070 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart7054a602007-04-25 09:52:34 -04001071 "%d:0703 Issue target reset to TGT %d LUN %d rpi x%x "
1072 "nlp_flag x%x\n", phba->brd_no, cmnd->device->id,
James Smart0b18ac42006-05-01 21:50:40 -04001073 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
1074
James Smart7054a602007-04-25 09:52:34 -04001075 iocb_status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001076 &phba->sli.ring[phba->sli.fcp_ring],
1077 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -05001078
James Smart7054a602007-04-25 09:52:34 -04001079 if (iocb_status == IOCB_TIMEDOUT)
1080 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
1081
1082 if (iocb_status == IOCB_SUCCESS)
1083 ret = SUCCESS;
1084 else
1085 ret = iocb_status;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001086
1087 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1088 cmd_status = iocbqrsp->iocb.ulpStatus;
1089
1090 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001091
1092 /*
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001093 * All outstanding txcmplq I/Os should have been aborted by the device.
dea31012005-04-17 16:05:31 -05001094 * Unfortunately, some targets do not abide by this forcing the driver
1095 * to double check.
1096 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001097 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1098 cmnd->device->id, cmnd->device->lun,
1099 LPFC_CTX_LUN);
1100 if (cnt)
1101 lpfc_sli_abort_iocb(phba,
1102 &phba->sli.ring[phba->sli.fcp_ring],
1103 cmnd->device->id, cmnd->device->lun,
1104 0, LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001105 loopcnt = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001106 while (cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001107 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001108
1109 if (++loopcnt
James Smartc01f3202006-08-18 17:47:08 -04001110 > (2 * phba->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001111 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001112
1113 cnt = lpfc_sli_sum_iocb(phba,
1114 &phba->sli.ring[phba->sli.fcp_ring],
1115 cmnd->device->id, cmnd->device->lun,
1116 LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001117 }
1118
1119 if (cnt) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001120 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart7054a602007-04-25 09:52:34 -04001121 "%d:0719 device reset I/O flush failure: cnt x%x\n",
dea31012005-04-17 16:05:31 -05001122 phba->brd_no, cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001123 ret = FAILED;
dea31012005-04-17 16:05:31 -05001124 }
1125
dea31012005-04-17 16:05:31 -05001126out_free_scsi_buf:
James Smart7054a602007-04-25 09:52:34 -04001127 if (iocb_status != IOCB_TIMEDOUT) {
1128 lpfc_release_scsi_buf(phba, lpfc_cmd);
1129 }
dea31012005-04-17 16:05:31 -05001130 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart7054a602007-04-25 09:52:34 -04001131 "%d:0713 SCSI layer issued device reset (%d, %d) "
1132 "return x%x status x%x result x%x\n",
1133 phba->brd_no, cmnd->device->id, cmnd->device->lun,
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001134 ret, cmd_status, cmd_result);
1135
dea31012005-04-17 16:05:31 -05001136out:
1137 return ret;
1138}
1139
Jeff Garzik 94d0e7b2005-05-28 07:55:48 -04001140static int
James Smart7054a602007-04-25 09:52:34 -04001141lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001142{
James Smart2e0fef82007-06-17 19:56:36 -05001143 struct Scsi_Host *shost = cmnd->device->host;
1144 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1145 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001146 struct lpfc_nodelist *ndlp = NULL;
1147 int match;
1148 int ret = FAILED, i, err_count = 0;
1149 int cnt, loopcnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001150 struct lpfc_scsi_buf * lpfc_cmd;
dea31012005-04-17 16:05:31 -05001151
James Smarta90f5682006-08-17 11:58:04 -04001152 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001153
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001154 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001155 if (lpfc_cmd == NULL)
1156 goto out;
1157
1158 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1159 lpfc_cmd->timeout = 60;
dea31012005-04-17 16:05:31 -05001160
1161 /*
1162 * Since the driver manages a single bus device, reset all
1163 * targets known to the driver. Should any target reset
1164 * fail, this routine returns failure to the midlayer.
1165 */
James Smarte17da182006-07-06 15:49:25 -04001166 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001167 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001168 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001169 spin_lock_irq(shost->host_lock);
1170 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart685f0bf2007-04-25 09:53:08 -04001171 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1172 i == ndlp->nlp_sid &&
1173 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001174 match = 1;
1175 break;
1176 }
1177 }
James Smart2e0fef82007-06-17 19:56:36 -05001178 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001179 if (!match)
1180 continue;
1181
James Smart2e0fef82007-06-17 19:56:36 -05001182 ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1183 cmnd->device->lun,
James Smart420b630d2006-07-06 15:50:16 -04001184 ndlp->rport->dd_data);
dea31012005-04-17 16:05:31 -05001185 if (ret != SUCCESS) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001186 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smartdca94792006-08-01 07:34:08 -04001187 "%d:0700 Bus Reset on target %d failed\n",
dea31012005-04-17 16:05:31 -05001188 phba->brd_no, i);
1189 err_count++;
James Smart7054a602007-04-25 09:52:34 -04001190 break;
dea31012005-04-17 16:05:31 -05001191 }
1192 }
1193
James Smart7054a602007-04-25 09:52:34 -04001194 if (ret != IOCB_TIMEDOUT)
1195 lpfc_release_scsi_buf(phba, lpfc_cmd);
1196
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001197 if (err_count == 0)
1198 ret = SUCCESS;
James Smart7054a602007-04-25 09:52:34 -04001199 else
1200 ret = FAILED;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001201
1202 /*
1203 * All outstanding txcmplq I/Os should have been aborted by
1204 * the targets. Unfortunately, some targets do not abide by
1205 * this forcing the driver to double check.
1206 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001207 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1208 0, 0, LPFC_CTX_HOST);
1209 if (cnt)
1210 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1211 0, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001212 loopcnt = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001213 while (cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001214 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001215
1216 if (++loopcnt
James Smartc01f3202006-08-18 17:47:08 -04001217 > (2 * phba->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001218 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001219
1220 cnt = lpfc_sli_sum_iocb(phba,
1221 &phba->sli.ring[phba->sli.fcp_ring],
1222 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001223 }
1224
1225 if (cnt) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001226 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
dea31012005-04-17 16:05:31 -05001227 "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1228 phba->brd_no, cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001229 ret = FAILED;
dea31012005-04-17 16:05:31 -05001230 }
1231
dea31012005-04-17 16:05:31 -05001232 lpfc_printf_log(phba,
1233 KERN_ERR,
1234 LOG_FCP,
1235 "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1236 phba->brd_no, ret);
1237out:
1238 return ret;
1239}
1240
1241static int
dea31012005-04-17 16:05:31 -05001242lpfc_slave_alloc(struct scsi_device *sdev)
1243{
James Smart2e0fef82007-06-17 19:56:36 -05001244 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1245 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001246 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001247 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001248 uint32_t total = 0, i;
1249 uint32_t num_to_alloc = 0;
1250 unsigned long flags;
dea31012005-04-17 16:05:31 -05001251
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001252 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001253 return -ENXIO;
1254
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001255 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001256
1257 /*
1258 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1259 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001260 * HBA limit conveyed to the midlayer via the host structure. The
1261 * formula accounts for the lun_queue_depth + error handlers + 1
1262 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001263 */
1264 total = phba->total_scsi_bufs;
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001265 num_to_alloc = phba->cfg_lun_queue_depth + 2;
dea31012005-04-17 16:05:31 -05001266 if (total >= phba->cfg_hba_queue_depth) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001267 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1268 "%d:0704 At limitation of %d preallocated "
1269 "command buffers\n", phba->brd_no, total);
dea31012005-04-17 16:05:31 -05001270 return 0;
1271 } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001272 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1273 "%d:0705 Allocation request of %d command "
1274 "buffers will exceed max of %d. Reducing "
1275 "allocation request to %d.\n", phba->brd_no,
1276 num_to_alloc, phba->cfg_hba_queue_depth,
1277 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001278 num_to_alloc = phba->cfg_hba_queue_depth - total;
1279 }
1280
1281 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05001282 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05001283 if (!scsi_buf) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001284 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1285 "%d:0706 Failed to allocate command "
1286 "buffer\n", phba->brd_no);
dea31012005-04-17 16:05:31 -05001287 break;
1288 }
1289
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001290 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001291 phba->total_scsi_bufs++;
1292 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001293 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001294 }
1295 return 0;
1296}
1297
1298static int
1299lpfc_slave_configure(struct scsi_device *sdev)
1300{
James Smart2e0fef82007-06-17 19:56:36 -05001301 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1302 struct lpfc_hba *phba = vport->phba;
1303 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05001304
1305 if (sdev->tagged_supported)
1306 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1307 else
1308 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1309
1310 /*
1311 * Initialize the fc transport attributes for the target
1312 * containing this scsi device. Also note that the driver's
1313 * target pointer is stored in the starget_data for the
1314 * driver's sysfs entry point functions.
1315 */
James Smartc01f3202006-08-18 17:47:08 -04001316 rport->dev_loss_tmo = phba->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05001317
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001318 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1319 lpfc_sli_poll_fcp_ring(phba);
1320 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1321 lpfc_poll_rearm_timer(phba);
1322 }
1323
dea31012005-04-17 16:05:31 -05001324 return 0;
1325}
1326
1327static void
1328lpfc_slave_destroy(struct scsi_device *sdev)
1329{
1330 sdev->hostdata = NULL;
1331 return;
1332}
1333
1334struct scsi_host_template lpfc_template = {
1335 .module = THIS_MODULE,
1336 .name = LPFC_DRIVER_NAME,
1337 .info = lpfc_info,
1338 .queuecommand = lpfc_queuecommand,
1339 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04001340 .eh_device_reset_handler= lpfc_device_reset_handler,
1341 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05001342 .slave_alloc = lpfc_slave_alloc,
1343 .slave_configure = lpfc_slave_configure,
1344 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04001345 .scan_finished = lpfc_scan_finished,
1346 .scan_start = lpfc_scan_start,
dea31012005-04-17 16:05:31 -05001347 .this_id = -1,
1348 .sg_tablesize = LPFC_SG_SEG_CNT,
1349 .cmd_per_lun = LPFC_CMD_PER_LUN,
1350 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05001351 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001352 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001353};