blob: 9ee2927ad82a84db3c096471e54f75d02d638e3f [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
James Smart3de2a652007-08-02 11:09:59 -040087lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smart92d7f7b2007-06-17 19:56:38 -050088 struct scsi_device *sdev)
89{
90 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -040091 struct lpfc_hba *phba = vport->phba;
James Smart92d7f7b2007-06-17 19:56:38 -050092 atomic_inc(&phba->num_cmd_success);
93
James Smart3de2a652007-08-02 11:09:59 -040094 if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -050095 return;
James Smart92d7f7b2007-06-17 19:56:38 -050096 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 }
James Smart92d7f7b2007-06-17 19:56:38 -0500102 phba->last_ramp_up_time = jiffies;
103 spin_unlock_irqrestore(&phba->hbalock, flags);
104
105 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
106 if ((phba->pport->work_port_events &
107 WORKER_RAMP_UP_QUEUE) == 0) {
108 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
109 }
110 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
111
112 spin_lock_irqsave(&phba->hbalock, flags);
113 if (phba->work_wait)
114 wake_up(phba->work_wait);
115 spin_unlock_irqrestore(&phba->hbalock, flags);
116}
117
118void
119lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
120{
James Smart549e55c2007-08-02 11:09:51 -0400121 struct lpfc_vport **vports;
122 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500123 struct scsi_device *sdev;
124 unsigned long new_queue_depth;
125 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400126 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500127
128 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
129 num_cmd_success = atomic_read(&phba->num_cmd_success);
130
James Smart549e55c2007-08-02 11:09:51 -0400131 vports = lpfc_create_vport_work_array(phba);
132 if (vports != NULL)
133 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
134 shost = lpfc_shost_from_vport(vports[i]);
135 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500136 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400137 sdev->queue_depth * num_rsrc_err /
138 (num_rsrc_err + num_cmd_success);
139 if (!new_queue_depth)
140 new_queue_depth = sdev->queue_depth - 1;
141 else
142 new_queue_depth = sdev->queue_depth -
143 new_queue_depth;
144 if (sdev->ordered_tags)
145 scsi_adjust_queue_depth(sdev,
146 MSG_ORDERED_TAG,
147 new_queue_depth);
148 else
149 scsi_adjust_queue_depth(sdev,
150 MSG_SIMPLE_TAG,
151 new_queue_depth);
152 }
James Smart92d7f7b2007-06-17 19:56:38 -0500153 }
James Smart549e55c2007-08-02 11:09:51 -0400154 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500155 spin_unlock_irq(&phba->hbalock);
156 atomic_set(&phba->num_rsrc_err, 0);
157 atomic_set(&phba->num_cmd_success, 0);
158}
159
160void
161lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
162{
James Smart549e55c2007-08-02 11:09:51 -0400163 struct lpfc_vport **vports;
164 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500165 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400166 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500167
James Smart549e55c2007-08-02 11:09:51 -0400168 vports = lpfc_create_vport_work_array(phba);
169 if (vports != NULL)
170 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
171 shost = lpfc_shost_from_vport(vports[i]);
172 shost_for_each_device(sdev, shost) {
173 if (sdev->ordered_tags)
174 scsi_adjust_queue_depth(sdev,
175 MSG_ORDERED_TAG,
176 sdev->queue_depth+1);
177 else
178 scsi_adjust_queue_depth(sdev,
179 MSG_SIMPLE_TAG,
180 sdev->queue_depth+1);
181 }
James Smart92d7f7b2007-06-17 19:56:38 -0500182 }
James Smart549e55c2007-08-02 11:09:51 -0400183 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500184 atomic_set(&phba->num_rsrc_err, 0);
185 atomic_set(&phba->num_cmd_success, 0);
186}
187
188/*
dea31012005-04-17 16:05:31 -0500189 * This routine allocates a scsi buffer, which contains all the necessary
190 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
191 * contains information to build the IOCB. The DMAable region contains
192 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
193 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
194 * and the BPL BDE is setup in the IOCB.
195 */
196static struct lpfc_scsi_buf *
James Smart2e0fef82007-06-17 19:56:36 -0500197lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500198{
James Smart2e0fef82007-06-17 19:56:36 -0500199 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500200 struct lpfc_scsi_buf *psb;
201 struct ulp_bde64 *bpl;
202 IOCB_t *iocb;
203 dma_addr_t pdma_phys;
James Bottomley604a3e32005-10-29 10:28:33 -0500204 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500205
206 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
207 if (!psb)
208 return NULL;
209 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
dea31012005-04-17 16:05:31 -0500210
211 /*
212 * Get memory from the pci pool to map the virt space to pci bus space
213 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
214 * struct fcp_rsp and the number of bde's necessary to support the
215 * sg_tablesize.
216 */
217 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
218 &psb->dma_handle);
219 if (!psb->data) {
220 kfree(psb);
221 return NULL;
222 }
223
224 /* Initialize virtual ptrs to dma_buf region. */
225 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
226
James Bottomley604a3e32005-10-29 10:28:33 -0500227 /* Allocate iotag for psb->cur_iocbq. */
228 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
229 if (iotag == 0) {
230 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
231 psb->data, psb->dma_handle);
232 kfree (psb);
233 return NULL;
234 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400235 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500236
dea31012005-04-17 16:05:31 -0500237 psb->fcp_cmnd = psb->data;
238 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
239 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
240 sizeof(struct fcp_rsp);
241
242 /* Initialize local short-hand pointers. */
243 bpl = psb->fcp_bpl;
244 pdma_phys = psb->dma_handle;
245
246 /*
247 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
248 * list bdes. Initialize the first two and leave the rest for
249 * queuecommand.
250 */
251 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
252 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
253 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
254 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
255 bpl->tus.w = le32_to_cpu(bpl->tus.w);
256 bpl++;
257
258 /* Setup the physical region for the FCP RSP */
259 pdma_phys += sizeof (struct fcp_cmnd);
260 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
261 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
262 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
263 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
264 bpl->tus.w = le32_to_cpu(bpl->tus.w);
265
266 /*
267 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
268 * initialize it with all known data now.
269 */
270 pdma_phys += (sizeof (struct fcp_rsp));
271 iocb = &psb->cur_iocbq.iocb;
272 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
273 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
274 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
275 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
276 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
277 iocb->ulpBdeCount = 1;
278 iocb->ulpClass = CLASS3;
279
280 return psb;
281}
282
Adrian Bunk455c53e2006-01-06 20:21:28 +0100283static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500284lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500285{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400286 struct lpfc_scsi_buf * lpfc_cmd = NULL;
287 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500288 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500289
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500290 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400291 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400292 if (lpfc_cmd) {
293 lpfc_cmd->seg_cnt = 0;
294 lpfc_cmd->nonsg_phys = 0;
295 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500296 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400297 return lpfc_cmd;
298}
299
300static void
James Smart92d7f7b2007-06-17 19:56:38 -0500301lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400302{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500303 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500304
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500305 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400306 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500307 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500308 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500309}
310
311static int
James Smart92d7f7b2007-06-17 19:56:38 -0500312lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500313{
314 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
315 struct scatterlist *sgel = NULL;
316 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
317 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
318 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
319 dma_addr_t physaddr;
320 uint32_t i, num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500321 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500322
323 /*
324 * There are three possibilities here - use scatter-gather segment, use
325 * the single mapping, or neither. Start the lpfc command prep by
326 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
327 * data bde entry.
328 */
329 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700330 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500331 /*
332 * The driver stores the segment count returned from pci_map_sg
333 * because this a count of dma-mappings used to map the use_sg
334 * pages. They are not guaranteed to be the same for those
335 * architectures that implement an IOMMU.
336 */
dea31012005-04-17 16:05:31 -0500337
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700338 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
339 scsi_sg_count(scsi_cmnd), datadir);
340 if (unlikely(!nseg))
341 return 1;
342
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500343 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500344 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
345 printk(KERN_ERR "%s: Too many sg segments from "
346 "dma_map_sg. Config %d, seg_cnt %d",
347 __FUNCTION__, phba->cfg_sg_seg_cnt,
348 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500349 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500350 return 1;
351 }
352
353 /*
354 * The driver established a maximum scatter-gather segment count
355 * during probe that limits the number of sg elements in any
356 * single scsi command. Just run through the seg_cnt and format
357 * the bde's.
358 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500359 scsi_for_each_sg(scsi_cmnd, sgel, nseg, i) {
dea31012005-04-17 16:05:31 -0500360 physaddr = sg_dma_address(sgel);
361 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
362 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
363 bpl->tus.f.bdeSize = sg_dma_len(sgel);
364 if (datadir == DMA_TO_DEVICE)
365 bpl->tus.f.bdeFlags = 0;
366 else
367 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
368 bpl->tus.w = le32_to_cpu(bpl->tus.w);
369 bpl++;
dea31012005-04-17 16:05:31 -0500370 num_bde++;
371 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700372 }
dea31012005-04-17 16:05:31 -0500373
374 /*
375 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400376 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
377 * reinitialized since all iocb memory resources are used many times
378 * for transmit, receive, and continuation bpl's.
dea31012005-04-17 16:05:31 -0500379 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400380 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500381 iocb_cmd->un.fcpi64.bdl.bdeSize +=
382 (num_bde * sizeof (struct ulp_bde64));
383 iocb_cmd->ulpBdeCount = 1;
384 iocb_cmd->ulpLe = 1;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500385 fcp_cmnd->fcpDl = be32_to_cpu(scsi_bufflen(scsi_cmnd));
dea31012005-04-17 16:05:31 -0500386 return 0;
387}
388
389static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400390lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
391{
392 /*
393 * There are only two special cases to consider. (1) the scsi command
394 * requested scatter-gather usage or (2) the scsi command allocated
395 * a request buffer, but did not request use_sg. There is a third
396 * case, but it does not require resource deallocation.
397 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500398 if (psb->seg_cnt > 0)
399 scsi_dma_unmap(psb->pCmd);
James Smartbcf4dbf2006-07-06 15:50:08 -0400400}
401
402static void
James Smart2e0fef82007-06-17 19:56:36 -0500403lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
404 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500405{
406 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
407 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
408 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -0400409 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -0500410 uint32_t resp_info = fcprsp->rspStatus2;
411 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500412 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500413 uint32_t host_status = DID_OK;
414 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500415 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500416
417 /*
418 * If this is a task management command, there is no
419 * scsi packet associated with this lpfc_cmd. The driver
420 * consumes it.
421 */
422 if (fcpcmd->fcpCntl2) {
423 scsi_status = 0;
424 goto out;
425 }
426
James Smartc7743952006-12-02 13:34:42 -0500427 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
428 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
429 if (snslen > SCSI_SENSE_BUFFERSIZE)
430 snslen = SCSI_SENSE_BUFFERSIZE;
431
432 if (resp_info & RSP_LEN_VALID)
433 rsplen = be32_to_cpu(fcprsp->rspRspLen);
434 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
435 }
436 lp = (uint32_t *)cmnd->sense_buffer;
437
438 if (!scsi_status && (resp_info & RESID_UNDER))
439 logit = LOG_FCP;
440
James Smarte8b62012007-08-02 11:10:09 -0400441 lpfc_printf_vlog(vport, KERN_WARNING, logit,
442 "0730 FCP command x%x failed: x%x SNS x%x x%x "
443 "Data: x%x x%x x%x x%x x%x\n",
444 cmnd->cmnd[0], scsi_status,
445 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
446 be32_to_cpu(fcprsp->rspResId),
447 be32_to_cpu(fcprsp->rspSnsLen),
448 be32_to_cpu(fcprsp->rspRspLen),
449 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -0500450
451 if (resp_info & RSP_LEN_VALID) {
452 rsplen = be32_to_cpu(fcprsp->rspRspLen);
453 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
454 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
455 host_status = DID_ERROR;
456 goto out;
457 }
458 }
459
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500460 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -0500461 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500462 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -0500463
James Smarte8b62012007-08-02 11:10:09 -0400464 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
465 "0716 FCP Read Underrun, expected %d, "
466 "residual %d Data: x%x x%x x%x\n",
467 be32_to_cpu(fcpcmd->fcpDl),
468 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
469 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500470
471 /*
James Smart7054a602007-04-25 09:52:34 -0400472 * If there is an under run check if under run reported by
473 * storage array is same as the under run reported by HBA.
474 * If this is not same, there is a dropped frame.
475 */
476 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
477 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500478 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -0400479 lpfc_printf_vlog(vport, KERN_WARNING,
480 LOG_FCP | LOG_FCP_ERROR,
481 "0735 FCP Read Check Error "
482 "and Underrun Data: x%x x%x x%x x%x\n",
483 be32_to_cpu(fcpcmd->fcpDl),
484 scsi_get_resid(cmnd), fcpi_parm,
485 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500486 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -0400487 host_status = DID_ERROR;
488 }
489 /*
dea31012005-04-17 16:05:31 -0500490 * The cmnd->underflow is the minimum number of bytes that must
491 * be transfered for this command. Provided a sense condition
492 * is not present, make sure the actual amount transferred is at
493 * least the underflow value or fail.
494 */
495 if (!(resp_info & SNS_LEN_VALID) &&
496 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500497 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
498 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -0400499 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
500 "0717 FCP command x%x residual "
501 "underrun converted to error "
502 "Data: x%x x%x x%x\n",
503 cmnd->cmnd[0], cmnd->request_bufflen,
504 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -0500505 host_status = DID_ERROR;
506 }
507 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -0400508 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
509 "0720 FCP command x%x residual overrun error. "
510 "Data: x%x x%x \n", cmnd->cmnd[0],
511 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -0500512 host_status = DID_ERROR;
513
514 /*
515 * Check SLI validation that all the transfer was actually done
516 * (fcpi_parm should be zero). Apply check only to reads.
517 */
518 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
519 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -0400520 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
521 "0734 FCP Read Check Error Data: "
522 "x%x x%x x%x x%x\n",
523 be32_to_cpu(fcpcmd->fcpDl),
524 be32_to_cpu(fcprsp->rspResId),
525 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -0500526 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500527 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -0500528 }
529
530 out:
531 cmnd->result = ScsiResult(host_status, scsi_status);
532}
533
534static void
535lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
536 struct lpfc_iocbq *pIocbOut)
537{
538 struct lpfc_scsi_buf *lpfc_cmd =
539 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500540 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500541 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
542 struct lpfc_nodelist *pnode = rdata->pnode;
543 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500544 int result;
545 struct scsi_device *sdev, *tmp_sdev;
546 int depth = 0;
dea31012005-04-17 16:05:31 -0500547
548 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
549 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
550
551 if (lpfc_cmd->status) {
552 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
553 (lpfc_cmd->result & IOERR_DRVR_MASK))
554 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
555 else if (lpfc_cmd->status >= IOSTAT_CNT)
556 lpfc_cmd->status = IOSTAT_DEFAULT;
557
James Smarte8b62012007-08-02 11:10:09 -0400558 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
559 "0729 FCP cmd x%x failed <%d/%d> "
560 "status: x%x result: x%x Data: x%x x%x\n",
561 cmd->cmnd[0],
562 cmd->device ? cmd->device->id : 0xffff,
563 cmd->device ? cmd->device->lun : 0xffff,
564 lpfc_cmd->status, lpfc_cmd->result,
565 pIocbOut->iocb.ulpContext,
566 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -0500567
568 switch (lpfc_cmd->status) {
569 case IOSTAT_FCP_RSP_ERROR:
570 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500571 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500572 break;
573 case IOSTAT_NPORT_BSY:
574 case IOSTAT_FABRIC_BSY:
575 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
576 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500577 case IOSTAT_LOCAL_REJECT:
578 if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
579 lpfc_cmd->result == IOERR_NO_RESOURCES ||
580 lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
581 cmd->result = ScsiResult(DID_REQUEUE, 0);
582 break;
583 } /* else: fall through */
dea31012005-04-17 16:05:31 -0500584 default:
585 cmd->result = ScsiResult(DID_ERROR, 0);
586 break;
587 }
588
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400589 if ((pnode == NULL )
590 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
591 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500592 } else {
593 cmd->result = ScsiResult(DID_OK, 0);
594 }
595
596 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
597 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
598
James Smarte8b62012007-08-02 11:10:09 -0400599 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
600 "0710 Iodone <%d/%d> cmd %p, error "
601 "x%x SNS x%x x%x Data: x%x x%x\n",
602 cmd->device->id, cmd->device->lun, cmd,
603 cmd->result, *lp, *(lp + 3), cmd->retries,
604 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -0500605 }
606
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500607 result = cmd->result;
608 sdev = cmd->device;
James Smart1dcb58e2007-04-25 09:51:30 -0400609 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500610 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400611
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500612 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
613 lpfc_release_scsi_buf(phba, lpfc_cmd);
614 return;
615 }
616
James Smart92d7f7b2007-06-17 19:56:38 -0500617
618 if (!result)
James Smart3de2a652007-08-02 11:09:59 -0400619 lpfc_rampup_queue_depth(vport, sdev);
James Smart92d7f7b2007-06-17 19:56:38 -0500620
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500621 if (!result && pnode != NULL &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500622 ((jiffies - pnode->last_ramp_up_time) >
623 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
624 ((jiffies - pnode->last_q_full_time) >
625 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smart3de2a652007-08-02 11:09:59 -0400626 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500627 shost_for_each_device(tmp_sdev, sdev->host) {
James Smart3de2a652007-08-02 11:09:59 -0400628 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500629 if (tmp_sdev->id != sdev->id)
630 continue;
631 if (tmp_sdev->ordered_tags)
632 scsi_adjust_queue_depth(tmp_sdev,
633 MSG_ORDERED_TAG,
634 tmp_sdev->queue_depth+1);
635 else
636 scsi_adjust_queue_depth(tmp_sdev,
637 MSG_SIMPLE_TAG,
638 tmp_sdev->queue_depth+1);
639
640 pnode->last_ramp_up_time = jiffies;
641 }
642 }
643 }
644
645 /*
646 * Check for queue full. If the lun is reporting queue full, then
647 * back off the lun queue depth to prevent target overloads.
648 */
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500649 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500650 pnode->last_q_full_time = jiffies;
651
652 shost_for_each_device(tmp_sdev, sdev->host) {
653 if (tmp_sdev->id != sdev->id)
654 continue;
655 depth = scsi_track_queue_full(tmp_sdev,
656 tmp_sdev->queue_depth - 1);
657 }
658 /*
James Smart2e0fef82007-06-17 19:56:36 -0500659 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500660 * Modify the returned error code to store
661 * the final depth value set by
662 * scsi_track_queue_full.
663 */
664 if (depth == -1)
665 depth = sdev->host->cmd_per_lun;
666
667 if (depth) {
James Smarte8b62012007-08-02 11:10:09 -0400668 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
669 "0711 detected queue full - lun queue "
670 "depth adjusted to %d.\n", depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500671 }
672 }
673
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400674 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500675}
676
677static void
James Smart2e0fef82007-06-17 19:56:36 -0500678lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
679 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -0500680{
James Smart2e0fef82007-06-17 19:56:36 -0500681 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500682 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
683 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
684 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
685 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
686 int datadir = scsi_cmnd->sc_data_direction;
687
688 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400689 /* clear task management bits */
690 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500691
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400692 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
693 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500694
695 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
696
697 if (scsi_cmnd->device->tagged_supported) {
698 switch (scsi_cmnd->tag) {
699 case HEAD_OF_QUEUE_TAG:
700 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
701 break;
702 case ORDERED_QUEUE_TAG:
703 fcp_cmnd->fcpCntl1 = ORDERED_Q;
704 break;
705 default:
706 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
707 break;
708 }
709 } else
710 fcp_cmnd->fcpCntl1 = 0;
711
712 /*
713 * There are three possibilities here - use scatter-gather segment, use
714 * the single mapping, or neither. Start the lpfc command prep by
715 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
716 * data bde entry.
717 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500718 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500719 if (datadir == DMA_TO_DEVICE) {
720 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
721 iocb_cmd->un.fcpi.fcpi_parm = 0;
722 iocb_cmd->ulpPU = 0;
723 fcp_cmnd->fcpCntl3 = WRITE_DATA;
724 phba->fc4OutputRequests++;
725 } else {
726 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
727 iocb_cmd->ulpPU = PARM_READ_CHECK;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500728 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500729 fcp_cmnd->fcpCntl3 = READ_DATA;
730 phba->fc4InputRequests++;
731 }
732 } else {
733 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
734 iocb_cmd->un.fcpi.fcpi_parm = 0;
735 iocb_cmd->ulpPU = 0;
736 fcp_cmnd->fcpCntl3 = 0;
737 phba->fc4ControlRequests++;
738 }
739
740 /*
741 * Finish initializing those IOCB fields that are independent
742 * of the scsi_cmnd request_buffer
743 */
744 piocbq->iocb.ulpContext = pnode->nlp_rpi;
745 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
746 piocbq->iocb.ulpFCP2Rcvy = 1;
747
748 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
749 piocbq->context1 = lpfc_cmd;
750 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
751 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500752 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -0500753}
754
755static int
James Smart2e0fef82007-06-17 19:56:36 -0500756lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -0500757 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -0400758 unsigned int lun,
dea31012005-04-17 16:05:31 -0500759 uint8_t task_mgmt_cmd)
760{
dea31012005-04-17 16:05:31 -0500761 struct lpfc_iocbq *piocbq;
762 IOCB_t *piocb;
763 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -0400764 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -0500765 struct lpfc_nodelist *ndlp = rdata->pnode;
766
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400767 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea31012005-04-17 16:05:31 -0500768 return 0;
769 }
770
dea31012005-04-17 16:05:31 -0500771 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -0500772 piocbq->vport = vport;
773
dea31012005-04-17 16:05:31 -0500774 piocb = &piocbq->iocb;
775
776 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart420b630d2006-07-06 15:50:16 -0400777 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500778 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
779
780 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
781
782 piocb->ulpContext = ndlp->nlp_rpi;
783 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
784 piocb->ulpFCP2Rcvy = 1;
785 }
786 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
787
788 /* ulpTimeout is only one byte */
789 if (lpfc_cmd->timeout > 0xff) {
790 /*
791 * Do not timeout the command at the firmware level.
792 * The driver will provide the timeout mechanism.
793 */
794 piocb->ulpTimeout = 0;
795 } else {
796 piocb->ulpTimeout = lpfc_cmd->timeout;
797 }
798
James Smart2e0fef82007-06-17 19:56:36 -0500799 return 1;
dea31012005-04-17 16:05:31 -0500800}
801
James Smart7054a602007-04-25 09:52:34 -0400802static void
803lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
804 struct lpfc_iocbq *cmdiocbq,
805 struct lpfc_iocbq *rspiocbq)
806{
807 struct lpfc_scsi_buf *lpfc_cmd =
808 (struct lpfc_scsi_buf *) cmdiocbq->context1;
809 if (lpfc_cmd)
810 lpfc_release_scsi_buf(phba, lpfc_cmd);
811 return;
812}
813
dea31012005-04-17 16:05:31 -0500814static int
James Smart2e0fef82007-06-17 19:56:36 -0500815lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -0400816 unsigned tgt_id, unsigned int lun,
817 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -0500818{
James Smart2e0fef82007-06-17 19:56:36 -0500819 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500820 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400821 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500822 int ret;
823
James Smartf5603512006-12-02 13:35:43 -0500824 if (!rdata->pnode)
825 return FAILED;
826
James Smart0b18ac42006-05-01 21:50:40 -0400827 lpfc_cmd->rdata = rdata;
James Smart2e0fef82007-06-17 19:56:36 -0500828 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -0400829 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -0500830 if (!ret)
831 return FAILED;
832
dea31012005-04-17 16:05:31 -0500833 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400834 iocbqrsp = lpfc_sli_get_iocbq(phba);
835
dea31012005-04-17 16:05:31 -0500836 if (!iocbqrsp)
837 return FAILED;
dea31012005-04-17 16:05:31 -0500838
James Smart0b18ac42006-05-01 21:50:40 -0400839 /* Issue Target Reset to TGT <num> */
James Smarte8b62012007-08-02 11:10:09 -0400840 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
841 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
842 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400843 ret = lpfc_sli_issue_iocb_wait(phba,
844 &phba->sli.ring[phba->sli.fcp_ring],
845 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500846 if (ret != IOCB_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -0400847 if (ret == IOCB_TIMEDOUT)
848 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
dea31012005-04-17 16:05:31 -0500849 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -0500850 } else {
851 ret = SUCCESS;
852 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
853 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
854 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
855 (lpfc_cmd->result & IOERR_DRVR_MASK))
856 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
857 }
858
James Bottomley604a3e32005-10-29 10:28:33 -0500859 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500860 return ret;
861}
862
dea31012005-04-17 16:05:31 -0500863const char *
864lpfc_info(struct Scsi_Host *host)
865{
James Smart2e0fef82007-06-17 19:56:36 -0500866 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
867 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500868 int len;
869 static char lpfcinfobuf[384];
870
871 memset(lpfcinfobuf,0,384);
872 if (phba && phba->pcidev){
873 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
874 len = strlen(lpfcinfobuf);
875 snprintf(lpfcinfobuf + len,
876 384-len,
877 " on PCI bus %02x device %02x irq %d",
878 phba->pcidev->bus->number,
879 phba->pcidev->devfn,
880 phba->pcidev->irq);
881 len = strlen(lpfcinfobuf);
882 if (phba->Port[0]) {
883 snprintf(lpfcinfobuf + len,
884 384-len,
885 " port %s",
886 phba->Port);
887 }
888 }
889 return lpfcinfobuf;
890}
891
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500892static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
893{
894 unsigned long poll_tmo_expires =
895 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
896
897 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
898 mod_timer(&phba->fcp_poll_timer,
899 poll_tmo_expires);
900}
901
902void lpfc_poll_start_timer(struct lpfc_hba * phba)
903{
904 lpfc_poll_rearm_timer(phba);
905}
906
907void lpfc_poll_timeout(unsigned long ptr)
908{
James Smart2e0fef82007-06-17 19:56:36 -0500909 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500910
911 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
912 lpfc_sli_poll_fcp_ring (phba);
913 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
914 lpfc_poll_rearm_timer(phba);
915 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500916}
917
dea31012005-04-17 16:05:31 -0500918static int
919lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
920{
James Smart2e0fef82007-06-17 19:56:36 -0500921 struct Scsi_Host *shost = cmnd->device->host;
922 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
923 struct lpfc_hba *phba = vport->phba;
924 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500925 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
926 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400927 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400928 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400929 int err;
dea31012005-04-17 16:05:31 -0500930
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400931 err = fc_remote_port_chkready(rport);
932 if (err) {
933 cmnd->result = err;
dea31012005-04-17 16:05:31 -0500934 goto out_fail_command;
935 }
936
937 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400938 * Catch race where our node has transitioned, but the
939 * transport is still transitioning.
dea31012005-04-17 16:05:31 -0500940 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400941 if (!ndlp) {
942 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
943 goto out_fail_command;
dea31012005-04-17 16:05:31 -0500944 }
James Smarted957682007-06-17 19:56:37 -0500945 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -0500946 if (lpfc_cmd == NULL) {
James Smart92d7f7b2007-06-17 19:56:38 -0500947 lpfc_adjust_queue_depth(phba);
948
James Smarte8b62012007-08-02 11:10:09 -0400949 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
950 "0707 driver's buffer pool is empty, "
951 "IO busied\n");
dea31012005-04-17 16:05:31 -0500952 goto out_host_busy;
953 }
954
955 /*
956 * Store the midlayer's command structure for the completion phase
957 * and complete the command initialization.
958 */
959 lpfc_cmd->pCmd = cmnd;
960 lpfc_cmd->rdata = rdata;
961 lpfc_cmd->timeout = 0;
962 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
963 cmnd->scsi_done = done;
964
965 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
966 if (err)
967 goto out_host_busy_free_buf;
968
James Smart2e0fef82007-06-17 19:56:36 -0500969 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -0500970
971 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -0500972 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea31012005-04-17 16:05:31 -0500973 if (err)
974 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500975
976 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
977 lpfc_sli_poll_fcp_ring(phba);
978 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
979 lpfc_poll_rearm_timer(phba);
980 }
981
dea31012005-04-17 16:05:31 -0500982 return 0;
983
984 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -0400985 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400986 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500987 out_host_busy:
988 return SCSI_MLQUEUE_HOST_BUSY;
989
990 out_fail_command:
991 done(cmnd);
992 return 0;
993}
994
James Smarta90f5682006-08-17 11:58:04 -0400995static void
996lpfc_block_error_handler(struct scsi_cmnd *cmnd)
997{
998 struct Scsi_Host *shost = cmnd->device->host;
999 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1000
1001 spin_lock_irq(shost->host_lock);
1002 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1003 spin_unlock_irq(shost->host_lock);
1004 msleep(1000);
1005 spin_lock_irq(shost->host_lock);
1006 }
1007 spin_unlock_irq(shost->host_lock);
1008 return;
1009}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001010
dea31012005-04-17 16:05:31 -05001011static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001012lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001013{
James Smart2e0fef82007-06-17 19:56:36 -05001014 struct Scsi_Host *shost = cmnd->device->host;
1015 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1016 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001017 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001018 struct lpfc_iocbq *iocb;
1019 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05001020 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05001021 IOCB_t *cmd, *icmd;
dea31012005-04-17 16:05:31 -05001022 unsigned int loop_count = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001023 int ret = SUCCESS;
1024
James Smarta90f5682006-08-17 11:58:04 -04001025 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001026 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1027 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05001028
1029 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001030 * If pCmd field of the corresponding lpfc_scsi_buf structure
1031 * points to a different SCSI command, then the driver has
1032 * already completed this command, but the midlayer did not
1033 * see the completion before the eh fired. Just return
1034 * SUCCESS.
dea31012005-04-17 16:05:31 -05001035 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001036 iocb = &lpfc_cmd->cur_iocbq;
1037 if (lpfc_cmd->pCmd != cmnd)
1038 goto out;
dea31012005-04-17 16:05:31 -05001039
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001040 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05001041
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001042 abtsiocb = lpfc_sli_get_iocbq(phba);
1043 if (abtsiocb == NULL) {
1044 ret = FAILED;
dea31012005-04-17 16:05:31 -05001045 goto out;
1046 }
1047
dea31012005-04-17 16:05:31 -05001048 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001049 * The scsi command can not be in txq and it is in flight because the
1050 * pCmd is still pointig at the SCSI command we have to abort. There
1051 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05001052 */
dea31012005-04-17 16:05:31 -05001053
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001054 cmd = &iocb->iocb;
1055 icmd = &abtsiocb->iocb;
1056 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1057 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1058 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05001059
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001060 icmd->ulpLe = 1;
1061 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05001062 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001063 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1064 else
1065 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05001066
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001067 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05001068 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001069 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1070 lpfc_sli_release_iocbq(phba, abtsiocb);
1071 ret = FAILED;
1072 goto out;
1073 }
1074
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001075 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1076 lpfc_sli_poll_fcp_ring (phba);
1077
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001078 /* Wait for abort to complete */
1079 while (lpfc_cmd->pCmd == cmnd)
1080 {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001081 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1082 lpfc_sli_poll_fcp_ring (phba);
1083
James Smart2e0fef82007-06-17 19:56:36 -05001084 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT * HZ);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001085 if (++loop_count
James Smart3de2a652007-08-02 11:09:59 -04001086 > (2 * vport->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
dea31012005-04-17 16:05:31 -05001087 break;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001088 }
dea31012005-04-17 16:05:31 -05001089
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001090 if (lpfc_cmd->pCmd == cmnd) {
1091 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04001092 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1093 "0748 abort handler timed out waiting "
1094 "for abort to complete: ret %#x, ID %d, "
1095 "LUN %d, snum %#lx\n",
1096 ret, cmnd->device->id, cmnd->device->lun,
1097 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001098 }
1099
1100 out:
James Smarte8b62012007-08-02 11:10:09 -04001101 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1102 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
1103 "LUN %d snum %#lx\n", ret, cmnd->device->id,
1104 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001105 return ret;
dea31012005-04-17 16:05:31 -05001106}
1107
1108static int
James Smart7054a602007-04-25 09:52:34 -04001109lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001110{
James Smart2e0fef82007-06-17 19:56:36 -05001111 struct Scsi_Host *shost = cmnd->device->host;
1112 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1113 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001114 struct lpfc_scsi_buf *lpfc_cmd;
1115 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001116 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1117 struct lpfc_nodelist *pnode = rdata->pnode;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001118 uint32_t cmd_result = 0, cmd_status = 0;
dea31012005-04-17 16:05:31 -05001119 int ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001120 int iocb_status = IOCB_SUCCESS;
dea31012005-04-17 16:05:31 -05001121 int cnt, loopcnt;
1122
James Smarta90f5682006-08-17 11:58:04 -04001123 lpfc_block_error_handler(cmnd);
James Smartf5603512006-12-02 13:35:43 -05001124 loopcnt = 0;
dea31012005-04-17 16:05:31 -05001125 /*
1126 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001127 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001128 */
James Smart92d7f7b2007-06-17 19:56:38 -05001129 while (1) {
dea31012005-04-17 16:05:31 -05001130 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001131 goto out;
dea31012005-04-17 16:05:31 -05001132
1133 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001134 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smartf5603512006-12-02 13:35:43 -05001135 loopcnt++;
1136 rdata = cmnd->device->hostdata;
1137 if (!rdata ||
James Smart3de2a652007-08-02 11:09:59 -04001138 (loopcnt > ((vport->cfg_devloss_tmo * 2) + 1))){
James Smarte8b62012007-08-02 11:10:09 -04001139 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1140 "0721 LUN Reset rport "
1141 "failure: cnt x%x rdata x%p\n",
1142 loopcnt, rdata);
James Smartf5603512006-12-02 13:35:43 -05001143 goto out;
1144 }
1145 pnode = rdata->pnode;
1146 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001147 goto out;
dea31012005-04-17 16:05:31 -05001148 }
James Smartf5603512006-12-02 13:35:43 -05001149 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001150 break;
1151 }
1152
James Smart2e0fef82007-06-17 19:56:36 -05001153 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001154 if (lpfc_cmd == NULL)
1155 goto out;
1156
dea31012005-04-17 16:05:31 -05001157 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001158 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001159
James Smart2e0fef82007-06-17 19:56:36 -05001160 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
James Smart7054a602007-04-25 09:52:34 -04001161 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05001162 if (!ret)
1163 goto out_free_scsi_buf;
1164
1165 iocbq = &lpfc_cmd->cur_iocbq;
1166
1167 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001168 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001169 if (iocbqrsp == NULL)
1170 goto out_free_scsi_buf;
1171
James Smarte8b62012007-08-02 11:10:09 -04001172 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1173 "0703 Issue target reset to TGT %d LUN %d "
1174 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1175 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
James Smart7054a602007-04-25 09:52:34 -04001176 iocb_status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001177 &phba->sli.ring[phba->sli.fcp_ring],
1178 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -05001179
James Smart7054a602007-04-25 09:52:34 -04001180 if (iocb_status == IOCB_TIMEDOUT)
1181 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
1182
1183 if (iocb_status == IOCB_SUCCESS)
1184 ret = SUCCESS;
1185 else
1186 ret = iocb_status;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001187
1188 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1189 cmd_status = iocbqrsp->iocb.ulpStatus;
1190
1191 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001192
1193 /*
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001194 * All outstanding txcmplq I/Os should have been aborted by the device.
dea31012005-04-17 16:05:31 -05001195 * Unfortunately, some targets do not abide by this forcing the driver
1196 * to double check.
1197 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001198 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1199 cmnd->device->id, cmnd->device->lun,
1200 LPFC_CTX_LUN);
1201 if (cnt)
1202 lpfc_sli_abort_iocb(phba,
1203 &phba->sli.ring[phba->sli.fcp_ring],
1204 cmnd->device->id, cmnd->device->lun,
1205 0, LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001206 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001207 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001208 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001209
1210 if (++loopcnt
James Smart3de2a652007-08-02 11:09:59 -04001211 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001212 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001213
1214 cnt = lpfc_sli_sum_iocb(phba,
1215 &phba->sli.ring[phba->sli.fcp_ring],
1216 cmnd->device->id, cmnd->device->lun,
1217 LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001218 }
1219
1220 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001221 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1222 "0719 device reset I/O flush failure: "
1223 "cnt x%x\n", cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001224 ret = FAILED;
dea31012005-04-17 16:05:31 -05001225 }
1226
dea31012005-04-17 16:05:31 -05001227out_free_scsi_buf:
James Smart7054a602007-04-25 09:52:34 -04001228 if (iocb_status != IOCB_TIMEDOUT) {
1229 lpfc_release_scsi_buf(phba, lpfc_cmd);
1230 }
James Smarte8b62012007-08-02 11:10:09 -04001231 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1232 "0713 SCSI layer issued device reset (%d, %d) "
1233 "return x%x status x%x result x%x\n",
1234 cmnd->device->id, cmnd->device->lun, ret,
1235 cmd_status, cmd_result);
dea31012005-04-17 16:05:31 -05001236out:
1237 return ret;
1238}
1239
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001240static int
James Smart7054a602007-04-25 09:52:34 -04001241lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001242{
James Smart2e0fef82007-06-17 19:56:36 -05001243 struct Scsi_Host *shost = cmnd->device->host;
1244 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1245 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001246 struct lpfc_nodelist *ndlp = NULL;
1247 int match;
1248 int ret = FAILED, i, err_count = 0;
1249 int cnt, loopcnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001250 struct lpfc_scsi_buf * lpfc_cmd;
dea31012005-04-17 16:05:31 -05001251
James Smarta90f5682006-08-17 11:58:04 -04001252 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001253
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001254 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001255 if (lpfc_cmd == NULL)
1256 goto out;
1257
1258 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1259 lpfc_cmd->timeout = 60;
dea31012005-04-17 16:05:31 -05001260
1261 /*
1262 * Since the driver manages a single bus device, reset all
1263 * targets known to the driver. Should any target reset
1264 * fail, this routine returns failure to the midlayer.
1265 */
James Smarte17da182006-07-06 15:49:25 -04001266 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001267 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001268 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001269 spin_lock_irq(shost->host_lock);
1270 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart685f0bf2007-04-25 09:53:08 -04001271 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1272 i == ndlp->nlp_sid &&
1273 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001274 match = 1;
1275 break;
1276 }
1277 }
James Smart2e0fef82007-06-17 19:56:36 -05001278 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001279 if (!match)
1280 continue;
1281
James Smart2e0fef82007-06-17 19:56:36 -05001282 ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1283 cmnd->device->lun,
James Smart420b630d2006-07-06 15:50:16 -04001284 ndlp->rport->dd_data);
dea31012005-04-17 16:05:31 -05001285 if (ret != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04001286 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1287 "0700 Bus Reset on target %d failed\n",
1288 i);
dea31012005-04-17 16:05:31 -05001289 err_count++;
James Smart7054a602007-04-25 09:52:34 -04001290 break;
dea31012005-04-17 16:05:31 -05001291 }
1292 }
1293
James Smart7054a602007-04-25 09:52:34 -04001294 if (ret != IOCB_TIMEDOUT)
1295 lpfc_release_scsi_buf(phba, lpfc_cmd);
1296
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001297 if (err_count == 0)
1298 ret = SUCCESS;
James Smart7054a602007-04-25 09:52:34 -04001299 else
1300 ret = FAILED;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001301
1302 /*
1303 * All outstanding txcmplq I/Os should have been aborted by
1304 * the targets. Unfortunately, some targets do not abide by
1305 * this forcing the driver to double check.
1306 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001307 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1308 0, 0, LPFC_CTX_HOST);
1309 if (cnt)
1310 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1311 0, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001312 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001313 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001314 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001315
1316 if (++loopcnt
James Smart3de2a652007-08-02 11:09:59 -04001317 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001318 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001319
1320 cnt = lpfc_sli_sum_iocb(phba,
1321 &phba->sli.ring[phba->sli.fcp_ring],
1322 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001323 }
1324
1325 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001326 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1327 "0715 Bus Reset I/O flush failure: "
1328 "cnt x%x left x%x\n", cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001329 ret = FAILED;
dea31012005-04-17 16:05:31 -05001330 }
1331
James Smarte8b62012007-08-02 11:10:09 -04001332 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1333 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05001334out:
1335 return ret;
1336}
1337
1338static int
dea31012005-04-17 16:05:31 -05001339lpfc_slave_alloc(struct scsi_device *sdev)
1340{
James Smart2e0fef82007-06-17 19:56:36 -05001341 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1342 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001343 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001344 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001345 uint32_t total = 0, i;
1346 uint32_t num_to_alloc = 0;
1347 unsigned long flags;
dea31012005-04-17 16:05:31 -05001348
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001349 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001350 return -ENXIO;
1351
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001352 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001353
1354 /*
1355 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1356 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001357 * HBA limit conveyed to the midlayer via the host structure. The
1358 * formula accounts for the lun_queue_depth + error handlers + 1
1359 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001360 */
1361 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04001362 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05001363
1364 /* Allow some exchanges to be available always to complete discovery */
1365 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001366 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1367 "0704 At limitation of %d preallocated "
1368 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05001369 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001370 /* Allow some exchanges to be available always to complete discovery */
1371 } else if (total + num_to_alloc >
1372 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001373 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1374 "0705 Allocation request of %d "
1375 "command buffers will exceed max of %d. "
1376 "Reducing allocation request to %d.\n",
1377 num_to_alloc, phba->cfg_hba_queue_depth,
1378 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001379 num_to_alloc = phba->cfg_hba_queue_depth - total;
1380 }
1381
1382 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05001383 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05001384 if (!scsi_buf) {
James Smarte8b62012007-08-02 11:10:09 -04001385 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1386 "0706 Failed to allocate "
1387 "command buffer\n");
dea31012005-04-17 16:05:31 -05001388 break;
1389 }
1390
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001391 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001392 phba->total_scsi_bufs++;
1393 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001394 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001395 }
1396 return 0;
1397}
1398
1399static int
1400lpfc_slave_configure(struct scsi_device *sdev)
1401{
James Smart2e0fef82007-06-17 19:56:36 -05001402 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1403 struct lpfc_hba *phba = vport->phba;
1404 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05001405
1406 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04001407 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001408 else
James Smart3de2a652007-08-02 11:09:59 -04001409 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001410
1411 /*
1412 * Initialize the fc transport attributes for the target
1413 * containing this scsi device. Also note that the driver's
1414 * target pointer is stored in the starget_data for the
1415 * driver's sysfs entry point functions.
1416 */
James Smart3de2a652007-08-02 11:09:59 -04001417 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05001418
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001419 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1420 lpfc_sli_poll_fcp_ring(phba);
1421 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1422 lpfc_poll_rearm_timer(phba);
1423 }
1424
dea31012005-04-17 16:05:31 -05001425 return 0;
1426}
1427
1428static void
1429lpfc_slave_destroy(struct scsi_device *sdev)
1430{
1431 sdev->hostdata = NULL;
1432 return;
1433}
1434
James Smart92d7f7b2007-06-17 19:56:38 -05001435
dea31012005-04-17 16:05:31 -05001436struct scsi_host_template lpfc_template = {
1437 .module = THIS_MODULE,
1438 .name = LPFC_DRIVER_NAME,
1439 .info = lpfc_info,
1440 .queuecommand = lpfc_queuecommand,
1441 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04001442 .eh_device_reset_handler= lpfc_device_reset_handler,
1443 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05001444 .slave_alloc = lpfc_slave_alloc,
1445 .slave_configure = lpfc_slave_configure,
1446 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04001447 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05001448 .this_id = -1,
1449 .sg_tablesize = LPFC_SG_SEG_CNT,
1450 .cmd_per_lun = LPFC_CMD_PER_LUN,
1451 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05001452 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001453 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001454};
James Smart3de2a652007-08-02 11:09:59 -04001455
1456struct scsi_host_template lpfc_vport_template = {
1457 .module = THIS_MODULE,
1458 .name = LPFC_DRIVER_NAME,
1459 .info = lpfc_info,
1460 .queuecommand = lpfc_queuecommand,
1461 .eh_abort_handler = lpfc_abort_handler,
1462 .eh_device_reset_handler= lpfc_device_reset_handler,
1463 .eh_bus_reset_handler = lpfc_bus_reset_handler,
1464 .slave_alloc = lpfc_slave_alloc,
1465 .slave_configure = lpfc_slave_configure,
1466 .slave_destroy = lpfc_slave_destroy,
1467 .scan_finished = lpfc_scan_finished,
1468 .this_id = -1,
1469 .sg_tablesize = LPFC_SG_SEG_CNT,
1470 .cmd_per_lun = LPFC_CMD_PER_LUN,
1471 .use_clustering = ENABLE_CLUSTERING,
1472 .shost_attrs = lpfc_vport_attrs,
1473 .max_sectors = 0xFFFF,
1474};