blob: 0284ded96bad7c225731379e9bb47a1b85f8fd21 [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"
James Smart92d7f7b2007-06-17 19:56:38 -050040#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050041
42#define LPFC_RESET_WAIT 2
43#define LPFC_ABORT_WAIT 2
44
dea31012005-04-17 16:05:31 -050045/*
James Smart92d7f7b2007-06-17 19:56:38 -050046 * This function is called with no lock held when there is a resource
47 * error in driver or in firmware.
48 */
49void
50lpfc_adjust_queue_depth(struct lpfc_hba *phba)
51{
52 unsigned long flags;
53
54 spin_lock_irqsave(&phba->hbalock, flags);
55 atomic_inc(&phba->num_rsrc_err);
56 phba->last_rsrc_error_time = jiffies;
57
58 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
59 spin_unlock_irqrestore(&phba->hbalock, flags);
60 return;
61 }
62
63 phba->last_ramp_down_time = jiffies;
64
65 spin_unlock_irqrestore(&phba->hbalock, flags);
66
67 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
68 if ((phba->pport->work_port_events &
69 WORKER_RAMP_DOWN_QUEUE) == 0) {
70 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
71 }
72 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
73
74 spin_lock_irqsave(&phba->hbalock, flags);
75 if (phba->work_wait)
76 wake_up(phba->work_wait);
77 spin_unlock_irqrestore(&phba->hbalock, flags);
78
79 return;
80}
81
82/*
83 * This function is called with no lock held when there is a successful
84 * SCSI command completion.
85 */
86static inline void
87lpfc_rampup_queue_depth(struct lpfc_hba *phba,
88 struct scsi_device *sdev)
89{
90 unsigned long flags;
91 atomic_inc(&phba->num_cmd_success);
92
93 if (phba->cfg_lun_queue_depth <= sdev->queue_depth)
94 return;
95
96 spin_lock_irqsave(&phba->hbalock, flags);
97 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
98 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
99 spin_unlock_irqrestore(&phba->hbalock, flags);
100 return;
101 }
102
103 phba->last_ramp_up_time = jiffies;
104 spin_unlock_irqrestore(&phba->hbalock, flags);
105
106 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
107 if ((phba->pport->work_port_events &
108 WORKER_RAMP_UP_QUEUE) == 0) {
109 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
110 }
111 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
112
113 spin_lock_irqsave(&phba->hbalock, flags);
114 if (phba->work_wait)
115 wake_up(phba->work_wait);
116 spin_unlock_irqrestore(&phba->hbalock, flags);
117}
118
119void
120lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
121{
James Smart549e55c2007-08-02 11:09:51 -0400122 struct lpfc_vport **vports;
123 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500124 struct scsi_device *sdev;
125 unsigned long new_queue_depth;
126 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400127 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500128
129 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
130 num_cmd_success = atomic_read(&phba->num_cmd_success);
131
James Smart549e55c2007-08-02 11:09:51 -0400132 vports = lpfc_create_vport_work_array(phba);
133 if (vports != NULL)
134 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
135 shost = lpfc_shost_from_vport(vports[i]);
136 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500137 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400138 sdev->queue_depth * num_rsrc_err /
139 (num_rsrc_err + num_cmd_success);
140 if (!new_queue_depth)
141 new_queue_depth = sdev->queue_depth - 1;
142 else
143 new_queue_depth = sdev->queue_depth -
144 new_queue_depth;
145 if (sdev->ordered_tags)
146 scsi_adjust_queue_depth(sdev,
147 MSG_ORDERED_TAG,
148 new_queue_depth);
149 else
150 scsi_adjust_queue_depth(sdev,
151 MSG_SIMPLE_TAG,
152 new_queue_depth);
153 }
James Smart92d7f7b2007-06-17 19:56:38 -0500154 }
James Smart549e55c2007-08-02 11:09:51 -0400155 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500156 spin_unlock_irq(&phba->hbalock);
157 atomic_set(&phba->num_rsrc_err, 0);
158 atomic_set(&phba->num_cmd_success, 0);
159}
160
161void
162lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
163{
James Smart549e55c2007-08-02 11:09:51 -0400164 struct lpfc_vport **vports;
165 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500166 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400167 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500168
James Smart549e55c2007-08-02 11:09:51 -0400169 vports = lpfc_create_vport_work_array(phba);
170 if (vports != NULL)
171 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
172 shost = lpfc_shost_from_vport(vports[i]);
173 shost_for_each_device(sdev, shost) {
174 if (sdev->ordered_tags)
175 scsi_adjust_queue_depth(sdev,
176 MSG_ORDERED_TAG,
177 sdev->queue_depth+1);
178 else
179 scsi_adjust_queue_depth(sdev,
180 MSG_SIMPLE_TAG,
181 sdev->queue_depth+1);
182 }
James Smart92d7f7b2007-06-17 19:56:38 -0500183 }
James Smart549e55c2007-08-02 11:09:51 -0400184 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500185 atomic_set(&phba->num_rsrc_err, 0);
186 atomic_set(&phba->num_cmd_success, 0);
187}
188
189/*
dea31012005-04-17 16:05:31 -0500190 * This routine allocates a scsi buffer, which contains all the necessary
191 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
192 * contains information to build the IOCB. The DMAable region contains
193 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
194 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
195 * and the BPL BDE is setup in the IOCB.
196 */
197static struct lpfc_scsi_buf *
James Smart2e0fef82007-06-17 19:56:36 -0500198lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500199{
James Smart2e0fef82007-06-17 19:56:36 -0500200 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500201 struct lpfc_scsi_buf *psb;
202 struct ulp_bde64 *bpl;
203 IOCB_t *iocb;
204 dma_addr_t pdma_phys;
James Bottomley604a3e32005-10-29 10:28:33 -0500205 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500206
207 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
208 if (!psb)
209 return NULL;
210 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
dea31012005-04-17 16:05:31 -0500211
212 /*
213 * Get memory from the pci pool to map the virt space to pci bus space
214 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
215 * struct fcp_rsp and the number of bde's necessary to support the
216 * sg_tablesize.
217 */
218 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
219 &psb->dma_handle);
220 if (!psb->data) {
221 kfree(psb);
222 return NULL;
223 }
224
225 /* Initialize virtual ptrs to dma_buf region. */
226 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
227
James Bottomley604a3e32005-10-29 10:28:33 -0500228 /* Allocate iotag for psb->cur_iocbq. */
229 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
230 if (iotag == 0) {
231 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
232 psb->data, psb->dma_handle);
233 kfree (psb);
234 return NULL;
235 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400236 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500237
dea31012005-04-17 16:05:31 -0500238 psb->fcp_cmnd = psb->data;
239 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
240 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
241 sizeof(struct fcp_rsp);
242
243 /* Initialize local short-hand pointers. */
244 bpl = psb->fcp_bpl;
245 pdma_phys = psb->dma_handle;
246
247 /*
248 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
249 * list bdes. Initialize the first two and leave the rest for
250 * queuecommand.
251 */
252 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
253 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
254 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
255 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
256 bpl->tus.w = le32_to_cpu(bpl->tus.w);
257 bpl++;
258
259 /* Setup the physical region for the FCP RSP */
260 pdma_phys += sizeof (struct fcp_cmnd);
261 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
262 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
263 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
264 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
265 bpl->tus.w = le32_to_cpu(bpl->tus.w);
266
267 /*
268 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
269 * initialize it with all known data now.
270 */
271 pdma_phys += (sizeof (struct fcp_rsp));
272 iocb = &psb->cur_iocbq.iocb;
273 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
274 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
275 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
276 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
277 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
278 iocb->ulpBdeCount = 1;
279 iocb->ulpClass = CLASS3;
280
281 return psb;
282}
283
Adrian Bunk455c53e2006-01-06 20:21:28 +0100284static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500285lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500286{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400287 struct lpfc_scsi_buf * lpfc_cmd = NULL;
288 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500289 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500290
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500291 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400292 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400293 if (lpfc_cmd) {
294 lpfc_cmd->seg_cnt = 0;
295 lpfc_cmd->nonsg_phys = 0;
296 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500297 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400298 return lpfc_cmd;
299}
300
301static void
James Smart92d7f7b2007-06-17 19:56:38 -0500302lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400303{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500304 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500305
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500306 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400307 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500308 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500309 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500310}
311
312static int
James Smart92d7f7b2007-06-17 19:56:38 -0500313lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500314{
315 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
316 struct scatterlist *sgel = NULL;
317 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
318 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
319 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
320 dma_addr_t physaddr;
321 uint32_t i, num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500322 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500323
324 /*
325 * There are three possibilities here - use scatter-gather segment, use
326 * the single mapping, or neither. Start the lpfc command prep by
327 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
328 * data bde entry.
329 */
330 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700331 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500332 /*
333 * The driver stores the segment count returned from pci_map_sg
334 * because this a count of dma-mappings used to map the use_sg
335 * pages. They are not guaranteed to be the same for those
336 * architectures that implement an IOMMU.
337 */
dea31012005-04-17 16:05:31 -0500338
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700339 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
340 scsi_sg_count(scsi_cmnd), datadir);
341 if (unlikely(!nseg))
342 return 1;
343
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500344 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500345 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
346 printk(KERN_ERR "%s: Too many sg segments from "
347 "dma_map_sg. Config %d, seg_cnt %d",
348 __FUNCTION__, phba->cfg_sg_seg_cnt,
349 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500350 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500351 return 1;
352 }
353
354 /*
355 * The driver established a maximum scatter-gather segment count
356 * during probe that limits the number of sg elements in any
357 * single scsi command. Just run through the seg_cnt and format
358 * the bde's.
359 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500360 scsi_for_each_sg(scsi_cmnd, sgel, nseg, i) {
dea31012005-04-17 16:05:31 -0500361 physaddr = sg_dma_address(sgel);
362 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
363 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
364 bpl->tus.f.bdeSize = sg_dma_len(sgel);
365 if (datadir == DMA_TO_DEVICE)
366 bpl->tus.f.bdeFlags = 0;
367 else
368 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
369 bpl->tus.w = le32_to_cpu(bpl->tus.w);
370 bpl++;
dea31012005-04-17 16:05:31 -0500371 num_bde++;
372 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700373 }
dea31012005-04-17 16:05:31 -0500374
375 /*
376 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400377 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
378 * reinitialized since all iocb memory resources are used many times
379 * for transmit, receive, and continuation bpl's.
dea31012005-04-17 16:05:31 -0500380 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400381 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500382 iocb_cmd->un.fcpi64.bdl.bdeSize +=
383 (num_bde * sizeof (struct ulp_bde64));
384 iocb_cmd->ulpBdeCount = 1;
385 iocb_cmd->ulpLe = 1;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500386 fcp_cmnd->fcpDl = be32_to_cpu(scsi_bufflen(scsi_cmnd));
dea31012005-04-17 16:05:31 -0500387 return 0;
388}
389
390static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400391lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
392{
393 /*
394 * There are only two special cases to consider. (1) the scsi command
395 * requested scatter-gather usage or (2) the scsi command allocated
396 * a request buffer, but did not request use_sg. There is a third
397 * case, but it does not require resource deallocation.
398 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500399 if (psb->seg_cnt > 0)
400 scsi_dma_unmap(psb->pCmd);
James Smartbcf4dbf2006-07-06 15:50:08 -0400401}
402
403static void
James Smart2e0fef82007-06-17 19:56:36 -0500404lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
405 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500406{
407 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
408 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
409 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart2e0fef82007-06-17 19:56:36 -0500410 struct lpfc_hba *phba = vport->phba;
James Smart7054a602007-04-25 09:52:34 -0400411 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
James Smart92d7f7b2007-06-17 19:56:38 -0500412 uint32_t vpi = vport->vpi;
dea31012005-04-17 16:05:31 -0500413 uint32_t resp_info = fcprsp->rspStatus2;
414 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500415 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500416 uint32_t host_status = DID_OK;
417 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500418 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500419
420 /*
421 * If this is a task management command, there is no
422 * scsi packet associated with this lpfc_cmd. The driver
423 * consumes it.
424 */
425 if (fcpcmd->fcpCntl2) {
426 scsi_status = 0;
427 goto out;
428 }
429
James Smartc7743952006-12-02 13:34:42 -0500430 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
431 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
432 if (snslen > SCSI_SENSE_BUFFERSIZE)
433 snslen = SCSI_SENSE_BUFFERSIZE;
434
435 if (resp_info & RSP_LEN_VALID)
436 rsplen = be32_to_cpu(fcprsp->rspRspLen);
437 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
438 }
439 lp = (uint32_t *)cmnd->sense_buffer;
440
441 if (!scsi_status && (resp_info & RESID_UNDER))
442 logit = LOG_FCP;
443
444 lpfc_printf_log(phba, KERN_WARNING, logit,
James Smart92d7f7b2007-06-17 19:56:38 -0500445 "%d (%d):0730 FCP command x%x failed: x%x SNS x%x x%x "
James Smartc7743952006-12-02 13:34:42 -0500446 "Data: x%x x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500447 phba->brd_no, vpi, cmnd->cmnd[0], scsi_status,
James Smartc7743952006-12-02 13:34:42 -0500448 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
dea31012005-04-17 16:05:31 -0500449 be32_to_cpu(fcprsp->rspResId),
450 be32_to_cpu(fcprsp->rspSnsLen),
451 be32_to_cpu(fcprsp->rspRspLen),
452 fcprsp->rspInfo3);
453
454 if (resp_info & RSP_LEN_VALID) {
455 rsplen = be32_to_cpu(fcprsp->rspRspLen);
456 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
457 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
458 host_status = DID_ERROR;
459 goto out;
460 }
461 }
462
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500463 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -0500464 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500465 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -0500466
467 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500468 "%d (%d):0716 FCP Read Underrun, expected %d, "
469 "residual %d Data: x%x x%x x%x\n",
470 phba->brd_no, vpi, be32_to_cpu(fcpcmd->fcpDl),
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500471 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
James Smart92d7f7b2007-06-17 19:56:38 -0500472 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500473
474 /*
James Smart7054a602007-04-25 09:52:34 -0400475 * If there is an under run check if under run reported by
476 * storage array is same as the under run reported by HBA.
477 * If this is not same, there is a dropped frame.
478 */
479 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
480 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500481 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smart7054a602007-04-25 09:52:34 -0400482 lpfc_printf_log(phba, KERN_WARNING,
James Smart92d7f7b2007-06-17 19:56:38 -0500483 LOG_FCP | LOG_FCP_ERROR,
484 "%d (%d):0735 FCP Read Check Error "
485 "and Underrun Data: x%x x%x x%x x%x\n",
486 phba->brd_no, vpi,
487 be32_to_cpu(fcpcmd->fcpDl),
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500488 scsi_get_resid(cmnd), fcpi_parm,
489 cmnd->cmnd[0]);
490 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -0400491 host_status = DID_ERROR;
492 }
493 /*
dea31012005-04-17 16:05:31 -0500494 * The cmnd->underflow is the minimum number of bytes that must
495 * be transfered for this command. Provided a sense condition
496 * is not present, make sure the actual amount transferred is at
497 * least the underflow value or fail.
498 */
499 if (!(resp_info & SNS_LEN_VALID) &&
500 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500501 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
502 < cmnd->underflow)) {
dea31012005-04-17 16:05:31 -0500503 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500504 "%d (%d):0717 FCP command x%x residual "
dea31012005-04-17 16:05:31 -0500505 "underrun converted to error "
James Smart92d7f7b2007-06-17 19:56:38 -0500506 "Data: x%x x%x x%x\n",
507 phba->brd_no, vpi, cmnd->cmnd[0],
Boaz Harroshd4bd4cd2007-07-03 18:12:35 +0300508 scsi_bufflen(cmnd),
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500509 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -0500510 host_status = DID_ERROR;
511 }
512 } else if (resp_info & RESID_OVER) {
513 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500514 "%d (%d):0720 FCP command x%x residual "
dea31012005-04-17 16:05:31 -0500515 "overrun error. Data: x%x x%x \n",
James Smart92d7f7b2007-06-17 19:56:38 -0500516 phba->brd_no, vpi, cmnd->cmnd[0],
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500517 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -0500518 host_status = DID_ERROR;
519
520 /*
521 * Check SLI validation that all the transfer was actually done
522 * (fcpi_parm should be zero). Apply check only to reads.
523 */
524 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
525 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smartc7743952006-12-02 13:34:42 -0500526 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart92d7f7b2007-06-17 19:56:38 -0500527 "%d (%d):0734 FCP Read Check Error Data: "
528 "x%x x%x x%x x%x\n",
529 phba->brd_no, vpi,
530 be32_to_cpu(fcpcmd->fcpDl),
531 be32_to_cpu(fcprsp->rspResId),
532 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -0500533 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500534 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -0500535 }
536
537 out:
538 cmnd->result = ScsiResult(host_status, scsi_status);
539}
540
541static void
542lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
543 struct lpfc_iocbq *pIocbOut)
544{
545 struct lpfc_scsi_buf *lpfc_cmd =
546 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500547 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500548 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
549 struct lpfc_nodelist *pnode = rdata->pnode;
550 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James Smart92d7f7b2007-06-17 19:56:38 -0500551 uint32_t vpi = (lpfc_cmd->cur_iocbq.vport
552 ? lpfc_cmd->cur_iocbq.vport->vpi
553 : 0);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500554 int result;
555 struct scsi_device *sdev, *tmp_sdev;
556 int depth = 0;
dea31012005-04-17 16:05:31 -0500557
558 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
559 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
560
561 if (lpfc_cmd->status) {
562 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
563 (lpfc_cmd->result & IOERR_DRVR_MASK))
564 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
565 else if (lpfc_cmd->status >= IOSTAT_CNT)
566 lpfc_cmd->status = IOSTAT_DEFAULT;
567
568 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500569 "%d (%d):0729 FCP cmd x%x failed <%d/%d> "
570 "status: x%x result: x%x Data: x%x x%x\n",
571 phba->brd_no, vpi, cmd->cmnd[0],
572 cmd->device ? cmd->device->id : 0xffff,
573 cmd->device ? cmd->device->lun : 0xffff,
574 lpfc_cmd->status, lpfc_cmd->result,
575 pIocbOut->iocb.ulpContext,
dea31012005-04-17 16:05:31 -0500576 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
577
578 switch (lpfc_cmd->status) {
579 case IOSTAT_FCP_RSP_ERROR:
580 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500581 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500582 break;
583 case IOSTAT_NPORT_BSY:
584 case IOSTAT_FABRIC_BSY:
585 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
586 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500587 case IOSTAT_LOCAL_REJECT:
588 if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
589 lpfc_cmd->result == IOERR_NO_RESOURCES ||
590 lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
591 cmd->result = ScsiResult(DID_REQUEUE, 0);
592 break;
593 } /* else: fall through */
dea31012005-04-17 16:05:31 -0500594 default:
595 cmd->result = ScsiResult(DID_ERROR, 0);
596 break;
597 }
598
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400599 if ((pnode == NULL )
600 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
601 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500602 } else {
603 cmd->result = ScsiResult(DID_OK, 0);
604 }
605
606 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
607 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
608
609 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500610 "%d (%d):0710 Iodone <%d/%d> cmd %p, error "
611 "x%x SNS x%x x%x Data: x%x x%x\n",
612 phba->brd_no, vpi, cmd->device->id,
dea31012005-04-17 16:05:31 -0500613 cmd->device->lun, cmd, cmd->result,
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500614 *lp, *(lp + 3), cmd->retries,
615 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -0500616 }
617
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500618 result = cmd->result;
619 sdev = cmd->device;
James Smart1dcb58e2007-04-25 09:51:30 -0400620 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500621 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400622
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500623 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
624 lpfc_release_scsi_buf(phba, lpfc_cmd);
625 return;
626 }
627
James Smart92d7f7b2007-06-17 19:56:38 -0500628
629 if (!result)
630 lpfc_rampup_queue_depth(phba, sdev);
631
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500632 if (!result && pnode != NULL &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500633 ((jiffies - pnode->last_ramp_up_time) >
634 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
635 ((jiffies - pnode->last_q_full_time) >
636 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
637 (phba->cfg_lun_queue_depth > sdev->queue_depth)) {
638 shost_for_each_device(tmp_sdev, sdev->host) {
639 if (phba->cfg_lun_queue_depth > tmp_sdev->queue_depth) {
640 if (tmp_sdev->id != sdev->id)
641 continue;
642 if (tmp_sdev->ordered_tags)
643 scsi_adjust_queue_depth(tmp_sdev,
644 MSG_ORDERED_TAG,
645 tmp_sdev->queue_depth+1);
646 else
647 scsi_adjust_queue_depth(tmp_sdev,
648 MSG_SIMPLE_TAG,
649 tmp_sdev->queue_depth+1);
650
651 pnode->last_ramp_up_time = jiffies;
652 }
653 }
654 }
655
656 /*
657 * Check for queue full. If the lun is reporting queue full, then
658 * back off the lun queue depth to prevent target overloads.
659 */
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500660 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500661 pnode->last_q_full_time = jiffies;
662
663 shost_for_each_device(tmp_sdev, sdev->host) {
664 if (tmp_sdev->id != sdev->id)
665 continue;
666 depth = scsi_track_queue_full(tmp_sdev,
667 tmp_sdev->queue_depth - 1);
668 }
669 /*
James Smart2e0fef82007-06-17 19:56:36 -0500670 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500671 * Modify the returned error code to store
672 * the final depth value set by
673 * scsi_track_queue_full.
674 */
675 if (depth == -1)
676 depth = sdev->host->cmd_per_lun;
677
678 if (depth) {
679 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500680 "%d (%d):0711 detected queue full - "
681 "lun queue depth adjusted to %d.\n",
682 phba->brd_no, vpi, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500683 }
684 }
685
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400686 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500687}
688
689static void
James Smart2e0fef82007-06-17 19:56:36 -0500690lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
691 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -0500692{
James Smart2e0fef82007-06-17 19:56:36 -0500693 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500694 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
695 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
696 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
697 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
698 int datadir = scsi_cmnd->sc_data_direction;
699
700 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400701 /* clear task management bits */
702 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500703
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400704 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
705 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500706
707 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
708
709 if (scsi_cmnd->device->tagged_supported) {
710 switch (scsi_cmnd->tag) {
711 case HEAD_OF_QUEUE_TAG:
712 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
713 break;
714 case ORDERED_QUEUE_TAG:
715 fcp_cmnd->fcpCntl1 = ORDERED_Q;
716 break;
717 default:
718 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
719 break;
720 }
721 } else
722 fcp_cmnd->fcpCntl1 = 0;
723
724 /*
725 * There are three possibilities here - use scatter-gather segment, use
726 * the single mapping, or neither. Start the lpfc command prep by
727 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
728 * data bde entry.
729 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500730 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500731 if (datadir == DMA_TO_DEVICE) {
732 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
733 iocb_cmd->un.fcpi.fcpi_parm = 0;
734 iocb_cmd->ulpPU = 0;
735 fcp_cmnd->fcpCntl3 = WRITE_DATA;
736 phba->fc4OutputRequests++;
737 } else {
738 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
739 iocb_cmd->ulpPU = PARM_READ_CHECK;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500740 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500741 fcp_cmnd->fcpCntl3 = READ_DATA;
742 phba->fc4InputRequests++;
743 }
744 } else {
745 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
746 iocb_cmd->un.fcpi.fcpi_parm = 0;
747 iocb_cmd->ulpPU = 0;
748 fcp_cmnd->fcpCntl3 = 0;
749 phba->fc4ControlRequests++;
750 }
751
752 /*
753 * Finish initializing those IOCB fields that are independent
754 * of the scsi_cmnd request_buffer
755 */
756 piocbq->iocb.ulpContext = pnode->nlp_rpi;
757 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
758 piocbq->iocb.ulpFCP2Rcvy = 1;
759
760 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
761 piocbq->context1 = lpfc_cmd;
762 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
763 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500764 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -0500765}
766
767static int
James Smart2e0fef82007-06-17 19:56:36 -0500768lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -0500769 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -0400770 unsigned int lun,
dea31012005-04-17 16:05:31 -0500771 uint8_t task_mgmt_cmd)
772{
dea31012005-04-17 16:05:31 -0500773 struct lpfc_iocbq *piocbq;
774 IOCB_t *piocb;
775 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -0400776 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -0500777 struct lpfc_nodelist *ndlp = rdata->pnode;
778
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400779 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea31012005-04-17 16:05:31 -0500780 return 0;
781 }
782
dea31012005-04-17 16:05:31 -0500783 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -0500784 piocbq->vport = vport;
785
dea31012005-04-17 16:05:31 -0500786 piocb = &piocbq->iocb;
787
788 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart420b630d2006-07-06 15:50:16 -0400789 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500790 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
791
792 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
793
794 piocb->ulpContext = ndlp->nlp_rpi;
795 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
796 piocb->ulpFCP2Rcvy = 1;
797 }
798 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
799
800 /* ulpTimeout is only one byte */
801 if (lpfc_cmd->timeout > 0xff) {
802 /*
803 * Do not timeout the command at the firmware level.
804 * The driver will provide the timeout mechanism.
805 */
806 piocb->ulpTimeout = 0;
807 } else {
808 piocb->ulpTimeout = lpfc_cmd->timeout;
809 }
810
James Smart2e0fef82007-06-17 19:56:36 -0500811 return 1;
dea31012005-04-17 16:05:31 -0500812}
813
James Smart7054a602007-04-25 09:52:34 -0400814static void
815lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
816 struct lpfc_iocbq *cmdiocbq,
817 struct lpfc_iocbq *rspiocbq)
818{
819 struct lpfc_scsi_buf *lpfc_cmd =
820 (struct lpfc_scsi_buf *) cmdiocbq->context1;
821 if (lpfc_cmd)
822 lpfc_release_scsi_buf(phba, lpfc_cmd);
823 return;
824}
825
dea31012005-04-17 16:05:31 -0500826static int
James Smart2e0fef82007-06-17 19:56:36 -0500827lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -0400828 unsigned tgt_id, unsigned int lun,
829 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -0500830{
James Smart2e0fef82007-06-17 19:56:36 -0500831 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500832 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400833 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500834 int ret;
835
James Smartf5603512006-12-02 13:35:43 -0500836 if (!rdata->pnode)
837 return FAILED;
838
James Smart0b18ac42006-05-01 21:50:40 -0400839 lpfc_cmd->rdata = rdata;
James Smart2e0fef82007-06-17 19:56:36 -0500840 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -0400841 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -0500842 if (!ret)
843 return FAILED;
844
dea31012005-04-17 16:05:31 -0500845 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400846 iocbqrsp = lpfc_sli_get_iocbq(phba);
847
dea31012005-04-17 16:05:31 -0500848 if (!iocbqrsp)
849 return FAILED;
dea31012005-04-17 16:05:31 -0500850
James Smart0b18ac42006-05-01 21:50:40 -0400851 /* Issue Target Reset to TGT <num> */
852 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500853 "%d (%d):0702 Issue Target Reset to TGT %d "
James Smart0b18ac42006-05-01 21:50:40 -0400854 "Data: x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500855 phba->brd_no, vport->vpi, tgt_id,
856 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James Smart0b18ac42006-05-01 21:50:40 -0400857
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400858 ret = lpfc_sli_issue_iocb_wait(phba,
859 &phba->sli.ring[phba->sli.fcp_ring],
860 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500861 if (ret != IOCB_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -0400862 if (ret == IOCB_TIMEDOUT)
863 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
dea31012005-04-17 16:05:31 -0500864 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -0500865 } else {
866 ret = SUCCESS;
867 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
868 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
869 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
870 (lpfc_cmd->result & IOERR_DRVR_MASK))
871 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
872 }
873
James Bottomley604a3e32005-10-29 10:28:33 -0500874 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500875 return ret;
876}
877
dea31012005-04-17 16:05:31 -0500878const char *
879lpfc_info(struct Scsi_Host *host)
880{
James Smart2e0fef82007-06-17 19:56:36 -0500881 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
882 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500883 int len;
884 static char lpfcinfobuf[384];
885
886 memset(lpfcinfobuf,0,384);
887 if (phba && phba->pcidev){
888 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
889 len = strlen(lpfcinfobuf);
890 snprintf(lpfcinfobuf + len,
891 384-len,
892 " on PCI bus %02x device %02x irq %d",
893 phba->pcidev->bus->number,
894 phba->pcidev->devfn,
895 phba->pcidev->irq);
896 len = strlen(lpfcinfobuf);
897 if (phba->Port[0]) {
898 snprintf(lpfcinfobuf + len,
899 384-len,
900 " port %s",
901 phba->Port);
902 }
903 }
904 return lpfcinfobuf;
905}
906
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500907static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
908{
909 unsigned long poll_tmo_expires =
910 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
911
912 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
913 mod_timer(&phba->fcp_poll_timer,
914 poll_tmo_expires);
915}
916
917void lpfc_poll_start_timer(struct lpfc_hba * phba)
918{
919 lpfc_poll_rearm_timer(phba);
920}
921
922void lpfc_poll_timeout(unsigned long ptr)
923{
James Smart2e0fef82007-06-17 19:56:36 -0500924 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500925
926 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
927 lpfc_sli_poll_fcp_ring (phba);
928 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
929 lpfc_poll_rearm_timer(phba);
930 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500931}
932
dea31012005-04-17 16:05:31 -0500933static int
934lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
935{
James Smart2e0fef82007-06-17 19:56:36 -0500936 struct Scsi_Host *shost = cmnd->device->host;
937 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
938 struct lpfc_hba *phba = vport->phba;
939 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500940 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
941 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400942 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400943 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400944 int err;
dea31012005-04-17 16:05:31 -0500945
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400946 err = fc_remote_port_chkready(rport);
947 if (err) {
948 cmnd->result = err;
dea31012005-04-17 16:05:31 -0500949 goto out_fail_command;
950 }
951
952 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400953 * Catch race where our node has transitioned, but the
954 * transport is still transitioning.
dea31012005-04-17 16:05:31 -0500955 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400956 if (!ndlp) {
957 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
958 goto out_fail_command;
dea31012005-04-17 16:05:31 -0500959 }
James Smarted957682007-06-17 19:56:37 -0500960 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -0500961 if (lpfc_cmd == NULL) {
James Smart92d7f7b2007-06-17 19:56:38 -0500962 lpfc_adjust_queue_depth(phba);
963
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500964 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500965 "%d (%d):0707 driver's buffer pool is empty, "
966 "IO busied\n",
967 phba->brd_no, vport->vpi);
dea31012005-04-17 16:05:31 -0500968 goto out_host_busy;
969 }
970
971 /*
972 * Store the midlayer's command structure for the completion phase
973 * and complete the command initialization.
974 */
975 lpfc_cmd->pCmd = cmnd;
976 lpfc_cmd->rdata = rdata;
977 lpfc_cmd->timeout = 0;
978 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
979 cmnd->scsi_done = done;
980
981 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
982 if (err)
983 goto out_host_busy_free_buf;
984
James Smart2e0fef82007-06-17 19:56:36 -0500985 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -0500986
987 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -0500988 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea31012005-04-17 16:05:31 -0500989 if (err)
990 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500991
992 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
993 lpfc_sli_poll_fcp_ring(phba);
994 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
995 lpfc_poll_rearm_timer(phba);
996 }
997
dea31012005-04-17 16:05:31 -0500998 return 0;
999
1000 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04001001 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001002 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001003 out_host_busy:
1004 return SCSI_MLQUEUE_HOST_BUSY;
1005
1006 out_fail_command:
1007 done(cmnd);
1008 return 0;
1009}
1010
James Smarta90f5682006-08-17 11:58:04 -04001011static void
1012lpfc_block_error_handler(struct scsi_cmnd *cmnd)
1013{
1014 struct Scsi_Host *shost = cmnd->device->host;
1015 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1016
1017 spin_lock_irq(shost->host_lock);
1018 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1019 spin_unlock_irq(shost->host_lock);
1020 msleep(1000);
1021 spin_lock_irq(shost->host_lock);
1022 }
1023 spin_unlock_irq(shost->host_lock);
1024 return;
1025}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001026
dea31012005-04-17 16:05:31 -05001027static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001028lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001029{
James Smart2e0fef82007-06-17 19:56:36 -05001030 struct Scsi_Host *shost = cmnd->device->host;
1031 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1032 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001033 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001034 struct lpfc_iocbq *iocb;
1035 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05001036 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05001037 IOCB_t *cmd, *icmd;
dea31012005-04-17 16:05:31 -05001038 unsigned int loop_count = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001039 int ret = SUCCESS;
1040
James Smarta90f5682006-08-17 11:58:04 -04001041 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001042 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1043 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05001044
1045 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001046 * If pCmd field of the corresponding lpfc_scsi_buf structure
1047 * points to a different SCSI command, then the driver has
1048 * already completed this command, but the midlayer did not
1049 * see the completion before the eh fired. Just return
1050 * SUCCESS.
dea31012005-04-17 16:05:31 -05001051 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001052 iocb = &lpfc_cmd->cur_iocbq;
1053 if (lpfc_cmd->pCmd != cmnd)
1054 goto out;
dea31012005-04-17 16:05:31 -05001055
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001056 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05001057
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001058 abtsiocb = lpfc_sli_get_iocbq(phba);
1059 if (abtsiocb == NULL) {
1060 ret = FAILED;
dea31012005-04-17 16:05:31 -05001061 goto out;
1062 }
1063
dea31012005-04-17 16:05:31 -05001064 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001065 * The scsi command can not be in txq and it is in flight because the
1066 * pCmd is still pointig at the SCSI command we have to abort. There
1067 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05001068 */
dea31012005-04-17 16:05:31 -05001069
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001070 cmd = &iocb->iocb;
1071 icmd = &abtsiocb->iocb;
1072 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1073 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1074 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05001075
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001076 icmd->ulpLe = 1;
1077 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05001078 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001079 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1080 else
1081 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05001082
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001083 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05001084 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001085 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1086 lpfc_sli_release_iocbq(phba, abtsiocb);
1087 ret = FAILED;
1088 goto out;
1089 }
1090
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001091 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1092 lpfc_sli_poll_fcp_ring (phba);
1093
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001094 /* Wait for abort to complete */
1095 while (lpfc_cmd->pCmd == cmnd)
1096 {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001097 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1098 lpfc_sli_poll_fcp_ring (phba);
1099
James Smart2e0fef82007-06-17 19:56:36 -05001100 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT * HZ);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001101 if (++loop_count
James Smartc01f3202006-08-18 17:47:08 -04001102 > (2 * phba->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
dea31012005-04-17 16:05:31 -05001103 break;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001104 }
dea31012005-04-17 16:05:31 -05001105
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001106 if (lpfc_cmd->pCmd == cmnd) {
1107 ret = FAILED;
1108 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001109 "%d (%d):0748 abort handler timed out waiting "
1110 "for abort to complete: ret %#x, ID %d, "
1111 "LUN %d, snum %#lx\n",
1112 phba->brd_no, vport->vpi, ret,
1113 cmnd->device->id, cmnd->device->lun,
1114 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001115 }
1116
1117 out:
1118 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001119 "%d (%d):0749 SCSI Layer I/O Abort Request "
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -05001120 "Status x%x ID %d LUN %d snum %#lx\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001121 phba->brd_no, vport->vpi, ret, cmnd->device->id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001122 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001123
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001124 return ret;
dea31012005-04-17 16:05:31 -05001125}
1126
1127static int
James Smart7054a602007-04-25 09:52:34 -04001128lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001129{
James Smart2e0fef82007-06-17 19:56:36 -05001130 struct Scsi_Host *shost = cmnd->device->host;
1131 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1132 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001133 struct lpfc_scsi_buf *lpfc_cmd;
1134 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001135 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1136 struct lpfc_nodelist *pnode = rdata->pnode;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001137 uint32_t cmd_result = 0, cmd_status = 0;
dea31012005-04-17 16:05:31 -05001138 int ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001139 int iocb_status = IOCB_SUCCESS;
dea31012005-04-17 16:05:31 -05001140 int cnt, loopcnt;
1141
James Smarta90f5682006-08-17 11:58:04 -04001142 lpfc_block_error_handler(cmnd);
James Smartf5603512006-12-02 13:35:43 -05001143 loopcnt = 0;
dea31012005-04-17 16:05:31 -05001144 /*
1145 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001146 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001147 */
James Smart92d7f7b2007-06-17 19:56:38 -05001148 while (1) {
dea31012005-04-17 16:05:31 -05001149 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001150 goto out;
dea31012005-04-17 16:05:31 -05001151
1152 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001153 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smartf5603512006-12-02 13:35:43 -05001154 loopcnt++;
1155 rdata = cmnd->device->hostdata;
1156 if (!rdata ||
1157 (loopcnt > ((phba->cfg_devloss_tmo * 2) + 1))) {
1158 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001159 "%d (%d):0721 LUN Reset rport "
1160 "failure: cnt x%x rdata x%p\n",
1161 phba->brd_no, vport->vpi,
1162 loopcnt, rdata);
James Smartf5603512006-12-02 13:35:43 -05001163 goto out;
1164 }
1165 pnode = rdata->pnode;
1166 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001167 goto out;
dea31012005-04-17 16:05:31 -05001168 }
James Smartf5603512006-12-02 13:35:43 -05001169 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001170 break;
1171 }
1172
James Smart2e0fef82007-06-17 19:56:36 -05001173 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001174 if (lpfc_cmd == NULL)
1175 goto out;
1176
dea31012005-04-17 16:05:31 -05001177 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001178 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001179
James Smart2e0fef82007-06-17 19:56:36 -05001180 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
James Smart7054a602007-04-25 09:52:34 -04001181 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05001182 if (!ret)
1183 goto out_free_scsi_buf;
1184
1185 iocbq = &lpfc_cmd->cur_iocbq;
1186
1187 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001188 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001189 if (iocbqrsp == NULL)
1190 goto out_free_scsi_buf;
1191
James Smart0b18ac42006-05-01 21:50:40 -04001192 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001193 "%d (%d):0703 Issue target reset to TGT %d LUN %d "
1194 "rpi x%x nlp_flag x%x\n",
1195 phba->brd_no, vport->vpi, cmnd->device->id,
James Smart0b18ac42006-05-01 21:50:40 -04001196 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
1197
James Smart7054a602007-04-25 09:52:34 -04001198 iocb_status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001199 &phba->sli.ring[phba->sli.fcp_ring],
1200 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -05001201
James Smart7054a602007-04-25 09:52:34 -04001202 if (iocb_status == IOCB_TIMEDOUT)
1203 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
1204
1205 if (iocb_status == IOCB_SUCCESS)
1206 ret = SUCCESS;
1207 else
1208 ret = iocb_status;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001209
1210 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1211 cmd_status = iocbqrsp->iocb.ulpStatus;
1212
1213 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001214
1215 /*
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001216 * All outstanding txcmplq I/Os should have been aborted by the device.
dea31012005-04-17 16:05:31 -05001217 * Unfortunately, some targets do not abide by this forcing the driver
1218 * to double check.
1219 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001220 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1221 cmnd->device->id, cmnd->device->lun,
1222 LPFC_CTX_LUN);
1223 if (cnt)
1224 lpfc_sli_abort_iocb(phba,
1225 &phba->sli.ring[phba->sli.fcp_ring],
1226 cmnd->device->id, cmnd->device->lun,
1227 0, LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001228 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001229 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001230 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001231
1232 if (++loopcnt
James Smartc01f3202006-08-18 17:47:08 -04001233 > (2 * phba->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001234 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001235
1236 cnt = lpfc_sli_sum_iocb(phba,
1237 &phba->sli.ring[phba->sli.fcp_ring],
1238 cmnd->device->id, cmnd->device->lun,
1239 LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001240 }
1241
1242 if (cnt) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001243 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001244 "%d (%d):0719 device reset I/O flush failure: "
1245 "cnt x%x\n",
1246 phba->brd_no, vport->vpi, cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001247 ret = FAILED;
dea31012005-04-17 16:05:31 -05001248 }
1249
dea31012005-04-17 16:05:31 -05001250out_free_scsi_buf:
James Smart7054a602007-04-25 09:52:34 -04001251 if (iocb_status != IOCB_TIMEDOUT) {
1252 lpfc_release_scsi_buf(phba, lpfc_cmd);
1253 }
dea31012005-04-17 16:05:31 -05001254 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001255 "%d (%d):0713 SCSI layer issued device reset (%d, %d) "
James Smart7054a602007-04-25 09:52:34 -04001256 "return x%x status x%x result x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001257 phba->brd_no, vport->vpi, cmnd->device->id,
1258 cmnd->device->lun, ret, cmd_status, cmd_result);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001259
dea31012005-04-17 16:05:31 -05001260out:
1261 return ret;
1262}
1263
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001264static int
James Smart7054a602007-04-25 09:52:34 -04001265lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001266{
James Smart2e0fef82007-06-17 19:56:36 -05001267 struct Scsi_Host *shost = cmnd->device->host;
1268 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1269 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001270 struct lpfc_nodelist *ndlp = NULL;
1271 int match;
1272 int ret = FAILED, i, err_count = 0;
1273 int cnt, loopcnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001274 struct lpfc_scsi_buf * lpfc_cmd;
dea31012005-04-17 16:05:31 -05001275
James Smarta90f5682006-08-17 11:58:04 -04001276 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001277
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001278 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001279 if (lpfc_cmd == NULL)
1280 goto out;
1281
1282 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1283 lpfc_cmd->timeout = 60;
dea31012005-04-17 16:05:31 -05001284
1285 /*
1286 * Since the driver manages a single bus device, reset all
1287 * targets known to the driver. Should any target reset
1288 * fail, this routine returns failure to the midlayer.
1289 */
James Smarte17da182006-07-06 15:49:25 -04001290 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001291 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001292 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001293 spin_lock_irq(shost->host_lock);
1294 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart685f0bf2007-04-25 09:53:08 -04001295 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1296 i == ndlp->nlp_sid &&
1297 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001298 match = 1;
1299 break;
1300 }
1301 }
James Smart2e0fef82007-06-17 19:56:36 -05001302 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001303 if (!match)
1304 continue;
1305
James Smart2e0fef82007-06-17 19:56:36 -05001306 ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1307 cmnd->device->lun,
James Smart420b630d2006-07-06 15:50:16 -04001308 ndlp->rport->dd_data);
dea31012005-04-17 16:05:31 -05001309 if (ret != SUCCESS) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001310 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001311 "%d (%d):0700 Bus Reset on target %d "
1312 "failed\n",
1313 phba->brd_no, vport->vpi, i);
dea31012005-04-17 16:05:31 -05001314 err_count++;
James Smart7054a602007-04-25 09:52:34 -04001315 break;
dea31012005-04-17 16:05:31 -05001316 }
1317 }
1318
James Smart7054a602007-04-25 09:52:34 -04001319 if (ret != IOCB_TIMEDOUT)
1320 lpfc_release_scsi_buf(phba, lpfc_cmd);
1321
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001322 if (err_count == 0)
1323 ret = SUCCESS;
James Smart7054a602007-04-25 09:52:34 -04001324 else
1325 ret = FAILED;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001326
1327 /*
1328 * All outstanding txcmplq I/Os should have been aborted by
1329 * the targets. Unfortunately, some targets do not abide by
1330 * this forcing the driver to double check.
1331 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001332 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1333 0, 0, LPFC_CTX_HOST);
1334 if (cnt)
1335 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1336 0, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001337 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001338 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001339 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001340
1341 if (++loopcnt
James Smartc01f3202006-08-18 17:47:08 -04001342 > (2 * phba->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001343 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001344
1345 cnt = lpfc_sli_sum_iocb(phba,
1346 &phba->sli.ring[phba->sli.fcp_ring],
1347 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001348 }
1349
1350 if (cnt) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001351 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001352 "%d (%d):0715 Bus Reset I/O flush failure: "
1353 "cnt x%x left x%x\n",
1354 phba->brd_no, vport->vpi, cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001355 ret = FAILED;
dea31012005-04-17 16:05:31 -05001356 }
1357
James Smart92d7f7b2007-06-17 19:56:38 -05001358 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1359 "%d (%d):0714 SCSI layer issued Bus Reset Data: x%x\n",
1360 phba->brd_no, vport->vpi, ret);
dea31012005-04-17 16:05:31 -05001361out:
1362 return ret;
1363}
1364
1365static int
dea31012005-04-17 16:05:31 -05001366lpfc_slave_alloc(struct scsi_device *sdev)
1367{
James Smart2e0fef82007-06-17 19:56:36 -05001368 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1369 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001370 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001371 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001372 uint32_t total = 0, i;
1373 uint32_t num_to_alloc = 0;
1374 unsigned long flags;
dea31012005-04-17 16:05:31 -05001375
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001376 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001377 return -ENXIO;
1378
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001379 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001380
1381 /*
1382 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1383 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001384 * HBA limit conveyed to the midlayer via the host structure. The
1385 * formula accounts for the lun_queue_depth + error handlers + 1
1386 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001387 */
1388 total = phba->total_scsi_bufs;
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001389 num_to_alloc = phba->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05001390
1391 /* Allow some exchanges to be available always to complete discovery */
1392 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001393 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001394 "%d (%d):0704 At limitation of %d "
1395 "preallocated command buffers\n",
1396 phba->brd_no, vport->vpi, total);
dea31012005-04-17 16:05:31 -05001397 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001398
1399 /* Allow some exchanges to be available always to complete discovery */
1400 } else if (total + num_to_alloc >
1401 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001402 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001403 "%d (%d):0705 Allocation request of %d "
1404 "command buffers will exceed max of %d. "
1405 "Reducing allocation request to %d.\n",
1406 phba->brd_no, vport->vpi, num_to_alloc,
1407 phba->cfg_hba_queue_depth,
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001408 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001409 num_to_alloc = phba->cfg_hba_queue_depth - total;
1410 }
1411
1412 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05001413 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05001414 if (!scsi_buf) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001415 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001416 "%d (%d):0706 Failed to allocate "
1417 "command buffer\n",
1418 phba->brd_no, vport->vpi);
dea31012005-04-17 16:05:31 -05001419 break;
1420 }
1421
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001422 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001423 phba->total_scsi_bufs++;
1424 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001425 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001426 }
1427 return 0;
1428}
1429
1430static int
1431lpfc_slave_configure(struct scsi_device *sdev)
1432{
James Smart2e0fef82007-06-17 19:56:36 -05001433 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1434 struct lpfc_hba *phba = vport->phba;
1435 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05001436
1437 if (sdev->tagged_supported)
1438 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1439 else
1440 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1441
1442 /*
1443 * Initialize the fc transport attributes for the target
1444 * containing this scsi device. Also note that the driver's
1445 * target pointer is stored in the starget_data for the
1446 * driver's sysfs entry point functions.
1447 */
James Smartc01f3202006-08-18 17:47:08 -04001448 rport->dev_loss_tmo = phba->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05001449
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001450 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1451 lpfc_sli_poll_fcp_ring(phba);
1452 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1453 lpfc_poll_rearm_timer(phba);
1454 }
1455
dea31012005-04-17 16:05:31 -05001456 return 0;
1457}
1458
1459static void
1460lpfc_slave_destroy(struct scsi_device *sdev)
1461{
1462 sdev->hostdata = NULL;
1463 return;
1464}
1465
James Smart92d7f7b2007-06-17 19:56:38 -05001466
dea31012005-04-17 16:05:31 -05001467struct scsi_host_template lpfc_template = {
1468 .module = THIS_MODULE,
1469 .name = LPFC_DRIVER_NAME,
1470 .info = lpfc_info,
1471 .queuecommand = lpfc_queuecommand,
1472 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04001473 .eh_device_reset_handler= lpfc_device_reset_handler,
1474 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05001475 .slave_alloc = lpfc_slave_alloc,
1476 .slave_configure = lpfc_slave_configure,
1477 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04001478 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05001479 .this_id = -1,
1480 .sg_tablesize = LPFC_SG_SEG_CNT,
1481 .cmd_per_lun = LPFC_CMD_PER_LUN,
1482 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05001483 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001484 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001485};