blob: 78666c62330e0e805b77e704f4325a125ae1e401 [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 Smart2e0fef82007-06-17 19:56:36 -0500409 struct lpfc_hba *phba = vport->phba;
James Smart7054a602007-04-25 09:52:34 -0400410 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
James Smart92d7f7b2007-06-17 19:56:38 -0500411 uint32_t vpi = vport->vpi;
dea31012005-04-17 16:05:31 -0500412 uint32_t resp_info = fcprsp->rspStatus2;
413 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500414 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500415 uint32_t host_status = DID_OK;
416 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500417 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500418
419 /*
420 * If this is a task management command, there is no
421 * scsi packet associated with this lpfc_cmd. The driver
422 * consumes it.
423 */
424 if (fcpcmd->fcpCntl2) {
425 scsi_status = 0;
426 goto out;
427 }
428
James Smartc7743952006-12-02 13:34:42 -0500429 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
430 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
431 if (snslen > SCSI_SENSE_BUFFERSIZE)
432 snslen = SCSI_SENSE_BUFFERSIZE;
433
434 if (resp_info & RSP_LEN_VALID)
435 rsplen = be32_to_cpu(fcprsp->rspRspLen);
436 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
437 }
438 lp = (uint32_t *)cmnd->sense_buffer;
439
440 if (!scsi_status && (resp_info & RESID_UNDER))
441 logit = LOG_FCP;
442
443 lpfc_printf_log(phba, KERN_WARNING, logit,
James Smart92d7f7b2007-06-17 19:56:38 -0500444 "%d (%d):0730 FCP command x%x failed: x%x SNS x%x x%x "
James Smartc7743952006-12-02 13:34:42 -0500445 "Data: x%x x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500446 phba->brd_no, vpi, cmnd->cmnd[0], scsi_status,
James Smartc7743952006-12-02 13:34:42 -0500447 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
dea31012005-04-17 16:05:31 -0500448 be32_to_cpu(fcprsp->rspResId),
449 be32_to_cpu(fcprsp->rspSnsLen),
450 be32_to_cpu(fcprsp->rspRspLen),
451 fcprsp->rspInfo3);
452
453 if (resp_info & RSP_LEN_VALID) {
454 rsplen = be32_to_cpu(fcprsp->rspRspLen);
455 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
456 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
457 host_status = DID_ERROR;
458 goto out;
459 }
460 }
461
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500462 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -0500463 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500464 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -0500465
466 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500467 "%d (%d):0716 FCP Read Underrun, expected %d, "
468 "residual %d Data: x%x x%x x%x\n",
469 phba->brd_no, vpi, be32_to_cpu(fcpcmd->fcpDl),
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500470 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
James Smart92d7f7b2007-06-17 19:56:38 -0500471 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500472
473 /*
James Smart7054a602007-04-25 09:52:34 -0400474 * If there is an under run check if under run reported by
475 * storage array is same as the under run reported by HBA.
476 * If this is not same, there is a dropped frame.
477 */
478 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
479 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500480 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smart7054a602007-04-25 09:52:34 -0400481 lpfc_printf_log(phba, KERN_WARNING,
James Smart92d7f7b2007-06-17 19:56:38 -0500482 LOG_FCP | LOG_FCP_ERROR,
483 "%d (%d):0735 FCP Read Check Error "
484 "and Underrun Data: x%x x%x x%x x%x\n",
485 phba->brd_no, vpi,
486 be32_to_cpu(fcpcmd->fcpDl),
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500487 scsi_get_resid(cmnd), fcpi_parm,
488 cmnd->cmnd[0]);
489 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -0400490 host_status = DID_ERROR;
491 }
492 /*
dea31012005-04-17 16:05:31 -0500493 * The cmnd->underflow is the minimum number of bytes that must
494 * be transfered for this command. Provided a sense condition
495 * is not present, make sure the actual amount transferred is at
496 * least the underflow value or fail.
497 */
498 if (!(resp_info & SNS_LEN_VALID) &&
499 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500500 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
501 < cmnd->underflow)) {
dea31012005-04-17 16:05:31 -0500502 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500503 "%d (%d):0717 FCP command x%x residual "
dea31012005-04-17 16:05:31 -0500504 "underrun converted to error "
James Smart92d7f7b2007-06-17 19:56:38 -0500505 "Data: x%x x%x x%x\n",
506 phba->brd_no, vpi, cmnd->cmnd[0],
Boaz Harroshd4bd4cd2007-07-03 18:12:35 +0300507 scsi_bufflen(cmnd),
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500508 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -0500509 host_status = DID_ERROR;
510 }
511 } else if (resp_info & RESID_OVER) {
512 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500513 "%d (%d):0720 FCP command x%x residual "
dea31012005-04-17 16:05:31 -0500514 "overrun error. Data: x%x x%x \n",
James Smart92d7f7b2007-06-17 19:56:38 -0500515 phba->brd_no, vpi, cmnd->cmnd[0],
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500516 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -0500517 host_status = DID_ERROR;
518
519 /*
520 * Check SLI validation that all the transfer was actually done
521 * (fcpi_parm should be zero). Apply check only to reads.
522 */
523 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
524 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smartc7743952006-12-02 13:34:42 -0500525 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart92d7f7b2007-06-17 19:56:38 -0500526 "%d (%d):0734 FCP Read Check Error Data: "
527 "x%x x%x x%x x%x\n",
528 phba->brd_no, vpi,
529 be32_to_cpu(fcpcmd->fcpDl),
530 be32_to_cpu(fcprsp->rspResId),
531 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -0500532 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500533 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -0500534 }
535
536 out:
537 cmnd->result = ScsiResult(host_status, scsi_status);
538}
539
540static void
541lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
542 struct lpfc_iocbq *pIocbOut)
543{
544 struct lpfc_scsi_buf *lpfc_cmd =
545 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500546 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500547 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
548 struct lpfc_nodelist *pnode = rdata->pnode;
549 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James Smart92d7f7b2007-06-17 19:56:38 -0500550 uint32_t vpi = (lpfc_cmd->cur_iocbq.vport
551 ? lpfc_cmd->cur_iocbq.vport->vpi
552 : 0);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500553 int result;
554 struct scsi_device *sdev, *tmp_sdev;
555 int depth = 0;
dea31012005-04-17 16:05:31 -0500556
557 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
558 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
559
560 if (lpfc_cmd->status) {
561 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
562 (lpfc_cmd->result & IOERR_DRVR_MASK))
563 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
564 else if (lpfc_cmd->status >= IOSTAT_CNT)
565 lpfc_cmd->status = IOSTAT_DEFAULT;
566
567 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500568 "%d (%d):0729 FCP cmd x%x failed <%d/%d> "
569 "status: x%x result: x%x Data: x%x x%x\n",
570 phba->brd_no, vpi, cmd->cmnd[0],
571 cmd->device ? cmd->device->id : 0xffff,
572 cmd->device ? cmd->device->lun : 0xffff,
573 lpfc_cmd->status, lpfc_cmd->result,
574 pIocbOut->iocb.ulpContext,
dea31012005-04-17 16:05:31 -0500575 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
576
577 switch (lpfc_cmd->status) {
578 case IOSTAT_FCP_RSP_ERROR:
579 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500580 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500581 break;
582 case IOSTAT_NPORT_BSY:
583 case IOSTAT_FABRIC_BSY:
584 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
585 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500586 case IOSTAT_LOCAL_REJECT:
587 if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
588 lpfc_cmd->result == IOERR_NO_RESOURCES ||
589 lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
590 cmd->result = ScsiResult(DID_REQUEUE, 0);
591 break;
592 } /* else: fall through */
dea31012005-04-17 16:05:31 -0500593 default:
594 cmd->result = ScsiResult(DID_ERROR, 0);
595 break;
596 }
597
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400598 if ((pnode == NULL )
599 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
600 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500601 } else {
602 cmd->result = ScsiResult(DID_OK, 0);
603 }
604
605 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
606 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
607
608 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500609 "%d (%d):0710 Iodone <%d/%d> cmd %p, error "
610 "x%x SNS x%x x%x Data: x%x x%x\n",
611 phba->brd_no, vpi, cmd->device->id,
dea31012005-04-17 16:05:31 -0500612 cmd->device->lun, cmd, cmd->result,
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500613 *lp, *(lp + 3), cmd->retries,
614 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -0500615 }
616
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500617 result = cmd->result;
618 sdev = cmd->device;
James Smart1dcb58e2007-04-25 09:51:30 -0400619 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500620 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400621
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500622 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
623 lpfc_release_scsi_buf(phba, lpfc_cmd);
624 return;
625 }
626
James Smart92d7f7b2007-06-17 19:56:38 -0500627
628 if (!result)
James Smart3de2a652007-08-02 11:09:59 -0400629 lpfc_rampup_queue_depth(vport, sdev);
James Smart92d7f7b2007-06-17 19:56:38 -0500630
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500631 if (!result && pnode != NULL &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500632 ((jiffies - pnode->last_ramp_up_time) >
633 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
634 ((jiffies - pnode->last_q_full_time) >
635 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smart3de2a652007-08-02 11:09:59 -0400636 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500637 shost_for_each_device(tmp_sdev, sdev->host) {
James Smart3de2a652007-08-02 11:09:59 -0400638 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500639 if (tmp_sdev->id != sdev->id)
640 continue;
641 if (tmp_sdev->ordered_tags)
642 scsi_adjust_queue_depth(tmp_sdev,
643 MSG_ORDERED_TAG,
644 tmp_sdev->queue_depth+1);
645 else
646 scsi_adjust_queue_depth(tmp_sdev,
647 MSG_SIMPLE_TAG,
648 tmp_sdev->queue_depth+1);
649
650 pnode->last_ramp_up_time = jiffies;
651 }
652 }
653 }
654
655 /*
656 * Check for queue full. If the lun is reporting queue full, then
657 * back off the lun queue depth to prevent target overloads.
658 */
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500659 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500660 pnode->last_q_full_time = jiffies;
661
662 shost_for_each_device(tmp_sdev, sdev->host) {
663 if (tmp_sdev->id != sdev->id)
664 continue;
665 depth = scsi_track_queue_full(tmp_sdev,
666 tmp_sdev->queue_depth - 1);
667 }
668 /*
James Smart2e0fef82007-06-17 19:56:36 -0500669 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500670 * Modify the returned error code to store
671 * the final depth value set by
672 * scsi_track_queue_full.
673 */
674 if (depth == -1)
675 depth = sdev->host->cmd_per_lun;
676
677 if (depth) {
678 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500679 "%d (%d):0711 detected queue full - "
680 "lun queue depth adjusted to %d.\n",
681 phba->brd_no, vpi, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500682 }
683 }
684
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400685 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500686}
687
688static void
James Smart2e0fef82007-06-17 19:56:36 -0500689lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
690 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -0500691{
James Smart2e0fef82007-06-17 19:56:36 -0500692 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500693 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
694 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
695 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
696 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
697 int datadir = scsi_cmnd->sc_data_direction;
698
699 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400700 /* clear task management bits */
701 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500702
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400703 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
704 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500705
706 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
707
708 if (scsi_cmnd->device->tagged_supported) {
709 switch (scsi_cmnd->tag) {
710 case HEAD_OF_QUEUE_TAG:
711 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
712 break;
713 case ORDERED_QUEUE_TAG:
714 fcp_cmnd->fcpCntl1 = ORDERED_Q;
715 break;
716 default:
717 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
718 break;
719 }
720 } else
721 fcp_cmnd->fcpCntl1 = 0;
722
723 /*
724 * There are three possibilities here - use scatter-gather segment, use
725 * the single mapping, or neither. Start the lpfc command prep by
726 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
727 * data bde entry.
728 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500729 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500730 if (datadir == DMA_TO_DEVICE) {
731 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
732 iocb_cmd->un.fcpi.fcpi_parm = 0;
733 iocb_cmd->ulpPU = 0;
734 fcp_cmnd->fcpCntl3 = WRITE_DATA;
735 phba->fc4OutputRequests++;
736 } else {
737 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
738 iocb_cmd->ulpPU = PARM_READ_CHECK;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500739 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500740 fcp_cmnd->fcpCntl3 = READ_DATA;
741 phba->fc4InputRequests++;
742 }
743 } else {
744 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
745 iocb_cmd->un.fcpi.fcpi_parm = 0;
746 iocb_cmd->ulpPU = 0;
747 fcp_cmnd->fcpCntl3 = 0;
748 phba->fc4ControlRequests++;
749 }
750
751 /*
752 * Finish initializing those IOCB fields that are independent
753 * of the scsi_cmnd request_buffer
754 */
755 piocbq->iocb.ulpContext = pnode->nlp_rpi;
756 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
757 piocbq->iocb.ulpFCP2Rcvy = 1;
758
759 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
760 piocbq->context1 = lpfc_cmd;
761 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
762 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500763 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -0500764}
765
766static int
James Smart2e0fef82007-06-17 19:56:36 -0500767lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -0500768 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -0400769 unsigned int lun,
dea31012005-04-17 16:05:31 -0500770 uint8_t task_mgmt_cmd)
771{
dea31012005-04-17 16:05:31 -0500772 struct lpfc_iocbq *piocbq;
773 IOCB_t *piocb;
774 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -0400775 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -0500776 struct lpfc_nodelist *ndlp = rdata->pnode;
777
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400778 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea31012005-04-17 16:05:31 -0500779 return 0;
780 }
781
dea31012005-04-17 16:05:31 -0500782 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -0500783 piocbq->vport = vport;
784
dea31012005-04-17 16:05:31 -0500785 piocb = &piocbq->iocb;
786
787 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart420b630d2006-07-06 15:50:16 -0400788 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500789 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
790
791 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
792
793 piocb->ulpContext = ndlp->nlp_rpi;
794 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
795 piocb->ulpFCP2Rcvy = 1;
796 }
797 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
798
799 /* ulpTimeout is only one byte */
800 if (lpfc_cmd->timeout > 0xff) {
801 /*
802 * Do not timeout the command at the firmware level.
803 * The driver will provide the timeout mechanism.
804 */
805 piocb->ulpTimeout = 0;
806 } else {
807 piocb->ulpTimeout = lpfc_cmd->timeout;
808 }
809
James Smart2e0fef82007-06-17 19:56:36 -0500810 return 1;
dea31012005-04-17 16:05:31 -0500811}
812
James Smart7054a602007-04-25 09:52:34 -0400813static void
814lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
815 struct lpfc_iocbq *cmdiocbq,
816 struct lpfc_iocbq *rspiocbq)
817{
818 struct lpfc_scsi_buf *lpfc_cmd =
819 (struct lpfc_scsi_buf *) cmdiocbq->context1;
820 if (lpfc_cmd)
821 lpfc_release_scsi_buf(phba, lpfc_cmd);
822 return;
823}
824
dea31012005-04-17 16:05:31 -0500825static int
James Smart2e0fef82007-06-17 19:56:36 -0500826lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -0400827 unsigned tgt_id, unsigned int lun,
828 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -0500829{
James Smart2e0fef82007-06-17 19:56:36 -0500830 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500831 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400832 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500833 int ret;
834
James Smartf5603512006-12-02 13:35:43 -0500835 if (!rdata->pnode)
836 return FAILED;
837
James Smart0b18ac42006-05-01 21:50:40 -0400838 lpfc_cmd->rdata = rdata;
James Smart2e0fef82007-06-17 19:56:36 -0500839 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -0400840 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -0500841 if (!ret)
842 return FAILED;
843
dea31012005-04-17 16:05:31 -0500844 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400845 iocbqrsp = lpfc_sli_get_iocbq(phba);
846
dea31012005-04-17 16:05:31 -0500847 if (!iocbqrsp)
848 return FAILED;
dea31012005-04-17 16:05:31 -0500849
James Smart0b18ac42006-05-01 21:50:40 -0400850 /* Issue Target Reset to TGT <num> */
851 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500852 "%d (%d):0702 Issue Target Reset to TGT %d "
James Smart0b18ac42006-05-01 21:50:40 -0400853 "Data: x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500854 phba->brd_no, vport->vpi, tgt_id,
855 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James Smart0b18ac42006-05-01 21:50:40 -0400856
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400857 ret = lpfc_sli_issue_iocb_wait(phba,
858 &phba->sli.ring[phba->sli.fcp_ring],
859 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500860 if (ret != IOCB_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -0400861 if (ret == IOCB_TIMEDOUT)
862 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
dea31012005-04-17 16:05:31 -0500863 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -0500864 } else {
865 ret = SUCCESS;
866 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
867 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
868 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
869 (lpfc_cmd->result & IOERR_DRVR_MASK))
870 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
871 }
872
James Bottomley604a3e32005-10-29 10:28:33 -0500873 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500874 return ret;
875}
876
dea31012005-04-17 16:05:31 -0500877const char *
878lpfc_info(struct Scsi_Host *host)
879{
James Smart2e0fef82007-06-17 19:56:36 -0500880 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
881 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500882 int len;
883 static char lpfcinfobuf[384];
884
885 memset(lpfcinfobuf,0,384);
886 if (phba && phba->pcidev){
887 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
888 len = strlen(lpfcinfobuf);
889 snprintf(lpfcinfobuf + len,
890 384-len,
891 " on PCI bus %02x device %02x irq %d",
892 phba->pcidev->bus->number,
893 phba->pcidev->devfn,
894 phba->pcidev->irq);
895 len = strlen(lpfcinfobuf);
896 if (phba->Port[0]) {
897 snprintf(lpfcinfobuf + len,
898 384-len,
899 " port %s",
900 phba->Port);
901 }
902 }
903 return lpfcinfobuf;
904}
905
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500906static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
907{
908 unsigned long poll_tmo_expires =
909 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
910
911 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
912 mod_timer(&phba->fcp_poll_timer,
913 poll_tmo_expires);
914}
915
916void lpfc_poll_start_timer(struct lpfc_hba * phba)
917{
918 lpfc_poll_rearm_timer(phba);
919}
920
921void lpfc_poll_timeout(unsigned long ptr)
922{
James Smart2e0fef82007-06-17 19:56:36 -0500923 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500924
925 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
926 lpfc_sli_poll_fcp_ring (phba);
927 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
928 lpfc_poll_rearm_timer(phba);
929 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500930}
931
dea31012005-04-17 16:05:31 -0500932static int
933lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
934{
James Smart2e0fef82007-06-17 19:56:36 -0500935 struct Scsi_Host *shost = cmnd->device->host;
936 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
937 struct lpfc_hba *phba = vport->phba;
938 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500939 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
940 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400941 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400942 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400943 int err;
dea31012005-04-17 16:05:31 -0500944
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400945 err = fc_remote_port_chkready(rport);
946 if (err) {
947 cmnd->result = err;
dea31012005-04-17 16:05:31 -0500948 goto out_fail_command;
949 }
950
951 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400952 * Catch race where our node has transitioned, but the
953 * transport is still transitioning.
dea31012005-04-17 16:05:31 -0500954 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400955 if (!ndlp) {
956 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
957 goto out_fail_command;
dea31012005-04-17 16:05:31 -0500958 }
James Smarted957682007-06-17 19:56:37 -0500959 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -0500960 if (lpfc_cmd == NULL) {
James Smart92d7f7b2007-06-17 19:56:38 -0500961 lpfc_adjust_queue_depth(phba);
962
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -0500963 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500964 "%d (%d):0707 driver's buffer pool is empty, "
965 "IO busied\n",
966 phba->brd_no, vport->vpi);
dea31012005-04-17 16:05:31 -0500967 goto out_host_busy;
968 }
969
970 /*
971 * Store the midlayer's command structure for the completion phase
972 * and complete the command initialization.
973 */
974 lpfc_cmd->pCmd = cmnd;
975 lpfc_cmd->rdata = rdata;
976 lpfc_cmd->timeout = 0;
977 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
978 cmnd->scsi_done = done;
979
980 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
981 if (err)
982 goto out_host_busy_free_buf;
983
James Smart2e0fef82007-06-17 19:56:36 -0500984 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -0500985
986 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -0500987 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea31012005-04-17 16:05:31 -0500988 if (err)
989 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500990
991 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
992 lpfc_sli_poll_fcp_ring(phba);
993 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
994 lpfc_poll_rearm_timer(phba);
995 }
996
dea31012005-04-17 16:05:31 -0500997 return 0;
998
999 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04001000 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001001 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001002 out_host_busy:
1003 return SCSI_MLQUEUE_HOST_BUSY;
1004
1005 out_fail_command:
1006 done(cmnd);
1007 return 0;
1008}
1009
James Smarta90f5682006-08-17 11:58:04 -04001010static void
1011lpfc_block_error_handler(struct scsi_cmnd *cmnd)
1012{
1013 struct Scsi_Host *shost = cmnd->device->host;
1014 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1015
1016 spin_lock_irq(shost->host_lock);
1017 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1018 spin_unlock_irq(shost->host_lock);
1019 msleep(1000);
1020 spin_lock_irq(shost->host_lock);
1021 }
1022 spin_unlock_irq(shost->host_lock);
1023 return;
1024}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001025
dea31012005-04-17 16:05:31 -05001026static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001027lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001028{
James Smart2e0fef82007-06-17 19:56:36 -05001029 struct Scsi_Host *shost = cmnd->device->host;
1030 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1031 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001032 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001033 struct lpfc_iocbq *iocb;
1034 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05001035 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05001036 IOCB_t *cmd, *icmd;
dea31012005-04-17 16:05:31 -05001037 unsigned int loop_count = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001038 int ret = SUCCESS;
1039
James Smarta90f5682006-08-17 11:58:04 -04001040 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001041 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1042 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05001043
1044 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001045 * If pCmd field of the corresponding lpfc_scsi_buf structure
1046 * points to a different SCSI command, then the driver has
1047 * already completed this command, but the midlayer did not
1048 * see the completion before the eh fired. Just return
1049 * SUCCESS.
dea31012005-04-17 16:05:31 -05001050 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001051 iocb = &lpfc_cmd->cur_iocbq;
1052 if (lpfc_cmd->pCmd != cmnd)
1053 goto out;
dea31012005-04-17 16:05:31 -05001054
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001055 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05001056
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001057 abtsiocb = lpfc_sli_get_iocbq(phba);
1058 if (abtsiocb == NULL) {
1059 ret = FAILED;
dea31012005-04-17 16:05:31 -05001060 goto out;
1061 }
1062
dea31012005-04-17 16:05:31 -05001063 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001064 * The scsi command can not be in txq and it is in flight because the
1065 * pCmd is still pointig at the SCSI command we have to abort. There
1066 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05001067 */
dea31012005-04-17 16:05:31 -05001068
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001069 cmd = &iocb->iocb;
1070 icmd = &abtsiocb->iocb;
1071 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1072 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1073 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05001074
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001075 icmd->ulpLe = 1;
1076 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05001077 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001078 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1079 else
1080 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05001081
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001082 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05001083 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001084 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1085 lpfc_sli_release_iocbq(phba, abtsiocb);
1086 ret = FAILED;
1087 goto out;
1088 }
1089
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001090 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1091 lpfc_sli_poll_fcp_ring (phba);
1092
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001093 /* Wait for abort to complete */
1094 while (lpfc_cmd->pCmd == cmnd)
1095 {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001096 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1097 lpfc_sli_poll_fcp_ring (phba);
1098
James Smart2e0fef82007-06-17 19:56:36 -05001099 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT * HZ);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001100 if (++loop_count
James Smart3de2a652007-08-02 11:09:59 -04001101 > (2 * vport->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
dea31012005-04-17 16:05:31 -05001102 break;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001103 }
dea31012005-04-17 16:05:31 -05001104
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001105 if (lpfc_cmd->pCmd == cmnd) {
1106 ret = FAILED;
1107 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001108 "%d (%d):0748 abort handler timed out waiting "
1109 "for abort to complete: ret %#x, ID %d, "
1110 "LUN %d, snum %#lx\n",
1111 phba->brd_no, vport->vpi, ret,
1112 cmnd->device->id, cmnd->device->lun,
1113 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001114 }
1115
1116 out:
1117 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001118 "%d (%d):0749 SCSI Layer I/O Abort Request "
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -05001119 "Status x%x ID %d LUN %d snum %#lx\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001120 phba->brd_no, vport->vpi, ret, cmnd->device->id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001121 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001122
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001123 return ret;
dea31012005-04-17 16:05:31 -05001124}
1125
1126static int
James Smart7054a602007-04-25 09:52:34 -04001127lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001128{
James Smart2e0fef82007-06-17 19:56:36 -05001129 struct Scsi_Host *shost = cmnd->device->host;
1130 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1131 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001132 struct lpfc_scsi_buf *lpfc_cmd;
1133 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001134 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1135 struct lpfc_nodelist *pnode = rdata->pnode;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001136 uint32_t cmd_result = 0, cmd_status = 0;
dea31012005-04-17 16:05:31 -05001137 int ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001138 int iocb_status = IOCB_SUCCESS;
dea31012005-04-17 16:05:31 -05001139 int cnt, loopcnt;
1140
James Smarta90f5682006-08-17 11:58:04 -04001141 lpfc_block_error_handler(cmnd);
James Smartf5603512006-12-02 13:35:43 -05001142 loopcnt = 0;
dea31012005-04-17 16:05:31 -05001143 /*
1144 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001145 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001146 */
James Smart92d7f7b2007-06-17 19:56:38 -05001147 while (1) {
dea31012005-04-17 16:05:31 -05001148 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001149 goto out;
dea31012005-04-17 16:05:31 -05001150
1151 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001152 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smartf5603512006-12-02 13:35:43 -05001153 loopcnt++;
1154 rdata = cmnd->device->hostdata;
1155 if (!rdata ||
James Smart3de2a652007-08-02 11:09:59 -04001156 (loopcnt > ((vport->cfg_devloss_tmo * 2) + 1))){
James Smartf5603512006-12-02 13:35:43 -05001157 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001158 "%d (%d):0721 LUN Reset rport "
1159 "failure: cnt x%x rdata x%p\n",
1160 phba->brd_no, vport->vpi,
1161 loopcnt, rdata);
James Smartf5603512006-12-02 13:35:43 -05001162 goto out;
1163 }
1164 pnode = rdata->pnode;
1165 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001166 goto out;
dea31012005-04-17 16:05:31 -05001167 }
James Smartf5603512006-12-02 13:35:43 -05001168 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001169 break;
1170 }
1171
James Smart2e0fef82007-06-17 19:56:36 -05001172 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001173 if (lpfc_cmd == NULL)
1174 goto out;
1175
dea31012005-04-17 16:05:31 -05001176 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001177 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001178
James Smart2e0fef82007-06-17 19:56:36 -05001179 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
James Smart7054a602007-04-25 09:52:34 -04001180 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05001181 if (!ret)
1182 goto out_free_scsi_buf;
1183
1184 iocbq = &lpfc_cmd->cur_iocbq;
1185
1186 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001187 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001188 if (iocbqrsp == NULL)
1189 goto out_free_scsi_buf;
1190
James Smart0b18ac42006-05-01 21:50:40 -04001191 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001192 "%d (%d):0703 Issue target reset to TGT %d LUN %d "
1193 "rpi x%x nlp_flag x%x\n",
1194 phba->brd_no, vport->vpi, cmnd->device->id,
James Smart0b18ac42006-05-01 21:50:40 -04001195 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
1196
James Smart7054a602007-04-25 09:52:34 -04001197 iocb_status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001198 &phba->sli.ring[phba->sli.fcp_ring],
1199 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -05001200
James Smart7054a602007-04-25 09:52:34 -04001201 if (iocb_status == IOCB_TIMEDOUT)
1202 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
1203
1204 if (iocb_status == IOCB_SUCCESS)
1205 ret = SUCCESS;
1206 else
1207 ret = iocb_status;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001208
1209 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1210 cmd_status = iocbqrsp->iocb.ulpStatus;
1211
1212 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001213
1214 /*
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001215 * All outstanding txcmplq I/Os should have been aborted by the device.
dea31012005-04-17 16:05:31 -05001216 * Unfortunately, some targets do not abide by this forcing the driver
1217 * to double check.
1218 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001219 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1220 cmnd->device->id, cmnd->device->lun,
1221 LPFC_CTX_LUN);
1222 if (cnt)
1223 lpfc_sli_abort_iocb(phba,
1224 &phba->sli.ring[phba->sli.fcp_ring],
1225 cmnd->device->id, cmnd->device->lun,
1226 0, LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001227 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001228 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001229 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001230
1231 if (++loopcnt
James Smart3de2a652007-08-02 11:09:59 -04001232 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001233 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001234
1235 cnt = lpfc_sli_sum_iocb(phba,
1236 &phba->sli.ring[phba->sli.fcp_ring],
1237 cmnd->device->id, cmnd->device->lun,
1238 LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001239 }
1240
1241 if (cnt) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001242 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001243 "%d (%d):0719 device reset I/O flush failure: "
1244 "cnt x%x\n",
1245 phba->brd_no, vport->vpi, cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001246 ret = FAILED;
dea31012005-04-17 16:05:31 -05001247 }
1248
dea31012005-04-17 16:05:31 -05001249out_free_scsi_buf:
James Smart7054a602007-04-25 09:52:34 -04001250 if (iocb_status != IOCB_TIMEDOUT) {
1251 lpfc_release_scsi_buf(phba, lpfc_cmd);
1252 }
dea31012005-04-17 16:05:31 -05001253 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001254 "%d (%d):0713 SCSI layer issued device reset (%d, %d) "
James Smart7054a602007-04-25 09:52:34 -04001255 "return x%x status x%x result x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001256 phba->brd_no, vport->vpi, cmnd->device->id,
1257 cmnd->device->lun, ret, cmd_status, cmd_result);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001258
dea31012005-04-17 16:05:31 -05001259out:
1260 return ret;
1261}
1262
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001263static int
James Smart7054a602007-04-25 09:52:34 -04001264lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001265{
James Smart2e0fef82007-06-17 19:56:36 -05001266 struct Scsi_Host *shost = cmnd->device->host;
1267 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1268 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001269 struct lpfc_nodelist *ndlp = NULL;
1270 int match;
1271 int ret = FAILED, i, err_count = 0;
1272 int cnt, loopcnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001273 struct lpfc_scsi_buf * lpfc_cmd;
dea31012005-04-17 16:05:31 -05001274
James Smarta90f5682006-08-17 11:58:04 -04001275 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001276
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001277 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001278 if (lpfc_cmd == NULL)
1279 goto out;
1280
1281 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1282 lpfc_cmd->timeout = 60;
dea31012005-04-17 16:05:31 -05001283
1284 /*
1285 * Since the driver manages a single bus device, reset all
1286 * targets known to the driver. Should any target reset
1287 * fail, this routine returns failure to the midlayer.
1288 */
James Smarte17da182006-07-06 15:49:25 -04001289 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001290 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001291 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001292 spin_lock_irq(shost->host_lock);
1293 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart685f0bf2007-04-25 09:53:08 -04001294 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1295 i == ndlp->nlp_sid &&
1296 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001297 match = 1;
1298 break;
1299 }
1300 }
James Smart2e0fef82007-06-17 19:56:36 -05001301 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001302 if (!match)
1303 continue;
1304
James Smart2e0fef82007-06-17 19:56:36 -05001305 ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1306 cmnd->device->lun,
James Smart420b630d2006-07-06 15:50:16 -04001307 ndlp->rport->dd_data);
dea31012005-04-17 16:05:31 -05001308 if (ret != SUCCESS) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001309 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001310 "%d (%d):0700 Bus Reset on target %d "
1311 "failed\n",
1312 phba->brd_no, vport->vpi, i);
dea31012005-04-17 16:05:31 -05001313 err_count++;
James Smart7054a602007-04-25 09:52:34 -04001314 break;
dea31012005-04-17 16:05:31 -05001315 }
1316 }
1317
James Smart7054a602007-04-25 09:52:34 -04001318 if (ret != IOCB_TIMEDOUT)
1319 lpfc_release_scsi_buf(phba, lpfc_cmd);
1320
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001321 if (err_count == 0)
1322 ret = SUCCESS;
James Smart7054a602007-04-25 09:52:34 -04001323 else
1324 ret = FAILED;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001325
1326 /*
1327 * All outstanding txcmplq I/Os should have been aborted by
1328 * the targets. Unfortunately, some targets do not abide by
1329 * this forcing the driver to double check.
1330 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001331 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1332 0, 0, LPFC_CTX_HOST);
1333 if (cnt)
1334 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1335 0, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001336 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001337 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001338 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001339
1340 if (++loopcnt
James Smart3de2a652007-08-02 11:09:59 -04001341 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001342 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001343
1344 cnt = lpfc_sli_sum_iocb(phba,
1345 &phba->sli.ring[phba->sli.fcp_ring],
1346 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001347 }
1348
1349 if (cnt) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001350 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001351 "%d (%d):0715 Bus Reset I/O flush failure: "
1352 "cnt x%x left x%x\n",
1353 phba->brd_no, vport->vpi, cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001354 ret = FAILED;
dea31012005-04-17 16:05:31 -05001355 }
1356
James Smart92d7f7b2007-06-17 19:56:38 -05001357 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1358 "%d (%d):0714 SCSI layer issued Bus Reset Data: x%x\n",
1359 phba->brd_no, vport->vpi, ret);
dea31012005-04-17 16:05:31 -05001360out:
1361 return ret;
1362}
1363
1364static int
dea31012005-04-17 16:05:31 -05001365lpfc_slave_alloc(struct scsi_device *sdev)
1366{
James Smart2e0fef82007-06-17 19:56:36 -05001367 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1368 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001369 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001370 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001371 uint32_t total = 0, i;
1372 uint32_t num_to_alloc = 0;
1373 unsigned long flags;
dea31012005-04-17 16:05:31 -05001374
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001375 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001376 return -ENXIO;
1377
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001378 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001379
1380 /*
1381 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1382 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001383 * HBA limit conveyed to the midlayer via the host structure. The
1384 * formula accounts for the lun_queue_depth + error handlers + 1
1385 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001386 */
1387 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04001388 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05001389
1390 /* Allow some exchanges to be available always to complete discovery */
1391 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001392 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001393 "%d (%d):0704 At limitation of %d "
1394 "preallocated command buffers\n",
1395 phba->brd_no, vport->vpi, total);
dea31012005-04-17 16:05:31 -05001396 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001397
1398 /* Allow some exchanges to be available always to complete discovery */
1399 } else if (total + num_to_alloc >
1400 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001401 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001402 "%d (%d):0705 Allocation request of %d "
1403 "command buffers will exceed max of %d. "
1404 "Reducing allocation request to %d.\n",
1405 phba->brd_no, vport->vpi, num_to_alloc,
1406 phba->cfg_hba_queue_depth,
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001407 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001408 num_to_alloc = phba->cfg_hba_queue_depth - total;
1409 }
1410
1411 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05001412 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05001413 if (!scsi_buf) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001414 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001415 "%d (%d):0706 Failed to allocate "
1416 "command buffer\n",
1417 phba->brd_no, vport->vpi);
dea31012005-04-17 16:05:31 -05001418 break;
1419 }
1420
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001421 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001422 phba->total_scsi_bufs++;
1423 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001424 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001425 }
1426 return 0;
1427}
1428
1429static int
1430lpfc_slave_configure(struct scsi_device *sdev)
1431{
James Smart2e0fef82007-06-17 19:56:36 -05001432 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1433 struct lpfc_hba *phba = vport->phba;
1434 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05001435
1436 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04001437 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001438 else
James Smart3de2a652007-08-02 11:09:59 -04001439 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001440
1441 /*
1442 * Initialize the fc transport attributes for the target
1443 * containing this scsi device. Also note that the driver's
1444 * target pointer is stored in the starget_data for the
1445 * driver's sysfs entry point functions.
1446 */
James Smart3de2a652007-08-02 11:09:59 -04001447 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05001448
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001449 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1450 lpfc_sli_poll_fcp_ring(phba);
1451 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1452 lpfc_poll_rearm_timer(phba);
1453 }
1454
dea31012005-04-17 16:05:31 -05001455 return 0;
1456}
1457
1458static void
1459lpfc_slave_destroy(struct scsi_device *sdev)
1460{
1461 sdev->hostdata = NULL;
1462 return;
1463}
1464
James Smart92d7f7b2007-06-17 19:56:38 -05001465
dea31012005-04-17 16:05:31 -05001466struct scsi_host_template lpfc_template = {
1467 .module = THIS_MODULE,
1468 .name = LPFC_DRIVER_NAME,
1469 .info = lpfc_info,
1470 .queuecommand = lpfc_queuecommand,
1471 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04001472 .eh_device_reset_handler= lpfc_device_reset_handler,
1473 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05001474 .slave_alloc = lpfc_slave_alloc,
1475 .slave_configure = lpfc_slave_configure,
1476 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04001477 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05001478 .this_id = -1,
1479 .sg_tablesize = LPFC_SG_SEG_CNT,
1480 .cmd_per_lun = LPFC_CMD_PER_LUN,
1481 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05001482 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001483 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001484};
James Smart3de2a652007-08-02 11:09:59 -04001485
1486struct scsi_host_template lpfc_vport_template = {
1487 .module = THIS_MODULE,
1488 .name = LPFC_DRIVER_NAME,
1489 .info = lpfc_info,
1490 .queuecommand = lpfc_queuecommand,
1491 .eh_abort_handler = lpfc_abort_handler,
1492 .eh_device_reset_handler= lpfc_device_reset_handler,
1493 .eh_bus_reset_handler = lpfc_bus_reset_handler,
1494 .slave_alloc = lpfc_slave_alloc,
1495 .slave_configure = lpfc_slave_configure,
1496 .slave_destroy = lpfc_slave_destroy,
1497 .scan_finished = lpfc_scan_finished,
1498 .this_id = -1,
1499 .sg_tablesize = LPFC_SG_SEG_CNT,
1500 .cmd_per_lun = LPFC_CMD_PER_LUN,
1501 .use_clustering = ENABLE_CLUSTERING,
1502 .shost_attrs = lpfc_vport_attrs,
1503 .max_sectors = 0xFFFF,
1504};