blob: 70255c11d3adde772d88f8288441c8fed324b98e [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 Smarte47c9092008-02-08 18:49:26 -05004 * Copyright (C) 2004-2008 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)
James Smart09372822008-01-11 01:52:54 -0500133 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400134 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 Smart09372822008-01-11 01:52:54 -0500154 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500155 atomic_set(&phba->num_rsrc_err, 0);
156 atomic_set(&phba->num_cmd_success, 0);
157}
158
159void
160lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
161{
James Smart549e55c2007-08-02 11:09:51 -0400162 struct lpfc_vport **vports;
163 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500164 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400165 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500166
James Smart549e55c2007-08-02 11:09:51 -0400167 vports = lpfc_create_vport_work_array(phba);
168 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500169 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400170 shost = lpfc_shost_from_vport(vports[i]);
171 shost_for_each_device(sdev, shost) {
172 if (sdev->ordered_tags)
173 scsi_adjust_queue_depth(sdev,
174 MSG_ORDERED_TAG,
175 sdev->queue_depth+1);
176 else
177 scsi_adjust_queue_depth(sdev,
178 MSG_SIMPLE_TAG,
179 sdev->queue_depth+1);
180 }
James Smart92d7f7b2007-06-17 19:56:38 -0500181 }
James Smart09372822008-01-11 01:52:54 -0500182 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500183 atomic_set(&phba->num_rsrc_err, 0);
184 atomic_set(&phba->num_cmd_success, 0);
185}
186
187/*
dea31012005-04-17 16:05:31 -0500188 * This routine allocates a scsi buffer, which contains all the necessary
189 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
190 * contains information to build the IOCB. The DMAable region contains
191 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
192 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
193 * and the BPL BDE is setup in the IOCB.
194 */
195static struct lpfc_scsi_buf *
James Smart2e0fef82007-06-17 19:56:36 -0500196lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500197{
James Smart2e0fef82007-06-17 19:56:36 -0500198 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500199 struct lpfc_scsi_buf *psb;
200 struct ulp_bde64 *bpl;
201 IOCB_t *iocb;
202 dma_addr_t pdma_phys;
James Bottomley604a3e32005-10-29 10:28:33 -0500203 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500204
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200205 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
dea31012005-04-17 16:05:31 -0500206 if (!psb)
207 return NULL;
dea31012005-04-17 16:05:31 -0500208
209 /*
210 * Get memory from the pci pool to map the virt space to pci bus space
211 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
212 * struct fcp_rsp and the number of bde's necessary to support the
213 * sg_tablesize.
214 */
215 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
216 &psb->dma_handle);
217 if (!psb->data) {
218 kfree(psb);
219 return NULL;
220 }
221
222 /* Initialize virtual ptrs to dma_buf region. */
223 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
224
James Bottomley604a3e32005-10-29 10:28:33 -0500225 /* Allocate iotag for psb->cur_iocbq. */
226 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
227 if (iotag == 0) {
228 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
229 psb->data, psb->dma_handle);
230 kfree (psb);
231 return NULL;
232 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400233 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500234
dea31012005-04-17 16:05:31 -0500235 psb->fcp_cmnd = psb->data;
236 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
237 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
238 sizeof(struct fcp_rsp);
239
240 /* Initialize local short-hand pointers. */
241 bpl = psb->fcp_bpl;
242 pdma_phys = psb->dma_handle;
243
244 /*
245 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
246 * list bdes. Initialize the first two and leave the rest for
247 * queuecommand.
248 */
249 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
250 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
251 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
252 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
253 bpl->tus.w = le32_to_cpu(bpl->tus.w);
254 bpl++;
255
256 /* Setup the physical region for the FCP RSP */
257 pdma_phys += sizeof (struct fcp_cmnd);
258 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
259 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
260 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
261 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
262 bpl->tus.w = le32_to_cpu(bpl->tus.w);
263
264 /*
265 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
266 * initialize it with all known data now.
267 */
268 pdma_phys += (sizeof (struct fcp_rsp));
269 iocb = &psb->cur_iocbq.iocb;
270 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
271 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
272 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
273 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
274 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
275 iocb->ulpBdeCount = 1;
276 iocb->ulpClass = CLASS3;
277
278 return psb;
279}
280
Adrian Bunk455c53e2006-01-06 20:21:28 +0100281static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500282lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500283{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400284 struct lpfc_scsi_buf * lpfc_cmd = NULL;
285 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500286 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500287
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500288 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400289 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400290 if (lpfc_cmd) {
291 lpfc_cmd->seg_cnt = 0;
292 lpfc_cmd->nonsg_phys = 0;
293 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500294 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400295 return lpfc_cmd;
296}
297
298static void
James Smart92d7f7b2007-06-17 19:56:38 -0500299lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400300{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500301 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500302
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500303 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400304 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500305 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500306 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500307}
308
309static int
James Smart92d7f7b2007-06-17 19:56:38 -0500310lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500311{
312 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
313 struct scatterlist *sgel = NULL;
314 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
315 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
316 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
317 dma_addr_t physaddr;
318 uint32_t i, num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500319 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500320
321 /*
322 * There are three possibilities here - use scatter-gather segment, use
323 * the single mapping, or neither. Start the lpfc command prep by
324 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
325 * data bde entry.
326 */
327 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700328 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500329 /*
330 * The driver stores the segment count returned from pci_map_sg
331 * because this a count of dma-mappings used to map the use_sg
332 * pages. They are not guaranteed to be the same for those
333 * architectures that implement an IOMMU.
334 */
dea31012005-04-17 16:05:31 -0500335
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700336 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
337 scsi_sg_count(scsi_cmnd), datadir);
338 if (unlikely(!nseg))
339 return 1;
340
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500341 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500342 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
343 printk(KERN_ERR "%s: Too many sg segments from "
344 "dma_map_sg. Config %d, seg_cnt %d",
345 __FUNCTION__, phba->cfg_sg_seg_cnt,
346 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500347 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500348 return 1;
349 }
350
351 /*
352 * The driver established a maximum scatter-gather segment count
353 * during probe that limits the number of sg elements in any
354 * single scsi command. Just run through the seg_cnt and format
355 * the bde's.
356 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500357 scsi_for_each_sg(scsi_cmnd, sgel, nseg, i) {
dea31012005-04-17 16:05:31 -0500358 physaddr = sg_dma_address(sgel);
359 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
360 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
361 bpl->tus.f.bdeSize = sg_dma_len(sgel);
362 if (datadir == DMA_TO_DEVICE)
363 bpl->tus.f.bdeFlags = 0;
364 else
365 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
366 bpl->tus.w = le32_to_cpu(bpl->tus.w);
367 bpl++;
dea31012005-04-17 16:05:31 -0500368 num_bde++;
369 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700370 }
dea31012005-04-17 16:05:31 -0500371
372 /*
373 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400374 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
375 * reinitialized since all iocb memory resources are used many times
376 * for transmit, receive, and continuation bpl's.
dea31012005-04-17 16:05:31 -0500377 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400378 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500379 iocb_cmd->un.fcpi64.bdl.bdeSize +=
380 (num_bde * sizeof (struct ulp_bde64));
381 iocb_cmd->ulpBdeCount = 1;
382 iocb_cmd->ulpLe = 1;
James Smart09372822008-01-11 01:52:54 -0500383 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
dea31012005-04-17 16:05:31 -0500384 return 0;
385}
386
387static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400388lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
389{
390 /*
391 * There are only two special cases to consider. (1) the scsi command
392 * requested scatter-gather usage or (2) the scsi command allocated
393 * a request buffer, but did not request use_sg. There is a third
394 * case, but it does not require resource deallocation.
395 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500396 if (psb->seg_cnt > 0)
397 scsi_dma_unmap(psb->pCmd);
James Smartbcf4dbf2006-07-06 15:50:08 -0400398}
399
400static void
James Smart2e0fef82007-06-17 19:56:36 -0500401lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
402 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500403{
404 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
405 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
406 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -0400407 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -0500408 uint32_t resp_info = fcprsp->rspStatus2;
409 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500410 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500411 uint32_t host_status = DID_OK;
412 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500413 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500414
415 /*
416 * If this is a task management command, there is no
417 * scsi packet associated with this lpfc_cmd. The driver
418 * consumes it.
419 */
420 if (fcpcmd->fcpCntl2) {
421 scsi_status = 0;
422 goto out;
423 }
424
James Smartc7743952006-12-02 13:34:42 -0500425 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
426 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
427 if (snslen > SCSI_SENSE_BUFFERSIZE)
428 snslen = SCSI_SENSE_BUFFERSIZE;
429
430 if (resp_info & RSP_LEN_VALID)
431 rsplen = be32_to_cpu(fcprsp->rspRspLen);
432 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
433 }
434 lp = (uint32_t *)cmnd->sense_buffer;
435
436 if (!scsi_status && (resp_info & RESID_UNDER))
437 logit = LOG_FCP;
438
James Smarte8b62012007-08-02 11:10:09 -0400439 lpfc_printf_vlog(vport, KERN_WARNING, logit,
440 "0730 FCP command x%x failed: x%x SNS x%x x%x "
441 "Data: x%x x%x x%x x%x x%x\n",
442 cmnd->cmnd[0], scsi_status,
443 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
444 be32_to_cpu(fcprsp->rspResId),
445 be32_to_cpu(fcprsp->rspSnsLen),
446 be32_to_cpu(fcprsp->rspRspLen),
447 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -0500448
449 if (resp_info & RSP_LEN_VALID) {
450 rsplen = be32_to_cpu(fcprsp->rspRspLen);
451 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
452 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
453 host_status = DID_ERROR;
454 goto out;
455 }
456 }
457
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500458 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -0500459 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500460 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -0500461
James Smarte8b62012007-08-02 11:10:09 -0400462 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
463 "0716 FCP Read Underrun, expected %d, "
464 "residual %d Data: x%x x%x x%x\n",
465 be32_to_cpu(fcpcmd->fcpDl),
466 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
467 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500468
469 /*
James Smart7054a602007-04-25 09:52:34 -0400470 * If there is an under run check if under run reported by
471 * storage array is same as the under run reported by HBA.
472 * If this is not same, there is a dropped frame.
473 */
474 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
475 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500476 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -0400477 lpfc_printf_vlog(vport, KERN_WARNING,
478 LOG_FCP | LOG_FCP_ERROR,
479 "0735 FCP Read Check Error "
480 "and Underrun Data: x%x x%x x%x x%x\n",
481 be32_to_cpu(fcpcmd->fcpDl),
482 scsi_get_resid(cmnd), fcpi_parm,
483 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500484 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -0400485 host_status = DID_ERROR;
486 }
487 /*
dea31012005-04-17 16:05:31 -0500488 * The cmnd->underflow is the minimum number of bytes that must
489 * be transfered for this command. Provided a sense condition
490 * is not present, make sure the actual amount transferred is at
491 * least the underflow value or fail.
492 */
493 if (!(resp_info & SNS_LEN_VALID) &&
494 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500495 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
496 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -0400497 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
498 "0717 FCP command x%x residual "
499 "underrun converted to error "
500 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -0400501 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -0400502 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -0500503 host_status = DID_ERROR;
504 }
505 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -0400506 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
507 "0720 FCP command x%x residual overrun error. "
508 "Data: x%x x%x \n", cmnd->cmnd[0],
509 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -0500510 host_status = DID_ERROR;
511
512 /*
513 * Check SLI validation that all the transfer was actually done
514 * (fcpi_parm should be zero). Apply check only to reads.
515 */
516 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
517 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -0400518 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
519 "0734 FCP Read Check Error Data: "
520 "x%x x%x x%x x%x\n",
521 be32_to_cpu(fcpcmd->fcpDl),
522 be32_to_cpu(fcprsp->rspResId),
523 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -0500524 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500525 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -0500526 }
527
528 out:
529 cmnd->result = ScsiResult(host_status, scsi_status);
530}
531
532static void
533lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
534 struct lpfc_iocbq *pIocbOut)
535{
536 struct lpfc_scsi_buf *lpfc_cmd =
537 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500538 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500539 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
540 struct lpfc_nodelist *pnode = rdata->pnode;
541 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500542 int result;
543 struct scsi_device *sdev, *tmp_sdev;
544 int depth = 0;
James Smartfa61a542008-01-11 01:52:42 -0500545 unsigned long flags;
dea31012005-04-17 16:05:31 -0500546
547 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
548 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
549
550 if (lpfc_cmd->status) {
551 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
552 (lpfc_cmd->result & IOERR_DRVR_MASK))
553 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
554 else if (lpfc_cmd->status >= IOSTAT_CNT)
555 lpfc_cmd->status = IOSTAT_DEFAULT;
556
James Smarte8b62012007-08-02 11:10:09 -0400557 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
558 "0729 FCP cmd x%x failed <%d/%d> "
559 "status: x%x result: x%x Data: x%x x%x\n",
560 cmd->cmnd[0],
561 cmd->device ? cmd->device->id : 0xffff,
562 cmd->device ? cmd->device->lun : 0xffff,
563 lpfc_cmd->status, lpfc_cmd->result,
564 pIocbOut->iocb.ulpContext,
565 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -0500566
567 switch (lpfc_cmd->status) {
568 case IOSTAT_FCP_RSP_ERROR:
569 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500570 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500571 break;
572 case IOSTAT_NPORT_BSY:
573 case IOSTAT_FABRIC_BSY:
574 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
575 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500576 case IOSTAT_LOCAL_REJECT:
577 if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
578 lpfc_cmd->result == IOERR_NO_RESOURCES ||
579 lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
580 cmd->result = ScsiResult(DID_REQUEUE, 0);
581 break;
582 } /* else: fall through */
dea31012005-04-17 16:05:31 -0500583 default:
584 cmd->result = ScsiResult(DID_ERROR, 0);
585 break;
586 }
587
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400588 if ((pnode == NULL )
589 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
590 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500591 } else {
592 cmd->result = ScsiResult(DID_OK, 0);
593 }
594
595 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
596 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
597
James Smarte8b62012007-08-02 11:10:09 -0400598 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
599 "0710 Iodone <%d/%d> cmd %p, error "
600 "x%x SNS x%x x%x Data: x%x x%x\n",
601 cmd->device->id, cmd->device->lun, cmd,
602 cmd->result, *lp, *(lp + 3), cmd->retries,
603 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -0500604 }
605
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500606 result = cmd->result;
607 sdev = cmd->device;
James Smart1dcb58e2007-04-25 09:51:30 -0400608 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500609 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400610
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500611 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -0500612 /*
613 * If there is a thread waiting for command completion
614 * wake up the thread.
615 */
616 spin_lock_irqsave(sdev->host->host_lock, flags);
617 lpfc_cmd->pCmd = NULL;
618 if (lpfc_cmd->waitq)
619 wake_up(lpfc_cmd->waitq);
620 spin_unlock_irqrestore(sdev->host->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500621 lpfc_release_scsi_buf(phba, lpfc_cmd);
622 return;
623 }
624
James Smart92d7f7b2007-06-17 19:56:38 -0500625
626 if (!result)
James Smart3de2a652007-08-02 11:09:59 -0400627 lpfc_rampup_queue_depth(vport, sdev);
James Smart92d7f7b2007-06-17 19:56:38 -0500628
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500629 if (!result && pnode != NULL &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500630 ((jiffies - pnode->last_ramp_up_time) >
631 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
632 ((jiffies - pnode->last_q_full_time) >
633 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smart3de2a652007-08-02 11:09:59 -0400634 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500635 shost_for_each_device(tmp_sdev, sdev->host) {
James Smart3de2a652007-08-02 11:09:59 -0400636 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500637 if (tmp_sdev->id != sdev->id)
638 continue;
639 if (tmp_sdev->ordered_tags)
640 scsi_adjust_queue_depth(tmp_sdev,
641 MSG_ORDERED_TAG,
642 tmp_sdev->queue_depth+1);
643 else
644 scsi_adjust_queue_depth(tmp_sdev,
645 MSG_SIMPLE_TAG,
646 tmp_sdev->queue_depth+1);
647
648 pnode->last_ramp_up_time = jiffies;
649 }
650 }
651 }
652
653 /*
654 * Check for queue full. If the lun is reporting queue full, then
655 * back off the lun queue depth to prevent target overloads.
656 */
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500657 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500658 pnode->last_q_full_time = jiffies;
659
660 shost_for_each_device(tmp_sdev, sdev->host) {
661 if (tmp_sdev->id != sdev->id)
662 continue;
663 depth = scsi_track_queue_full(tmp_sdev,
664 tmp_sdev->queue_depth - 1);
665 }
666 /*
James Smart2e0fef82007-06-17 19:56:36 -0500667 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500668 * Modify the returned error code to store
669 * the final depth value set by
670 * scsi_track_queue_full.
671 */
672 if (depth == -1)
673 depth = sdev->host->cmd_per_lun;
674
675 if (depth) {
James Smarte8b62012007-08-02 11:10:09 -0400676 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
677 "0711 detected queue full - lun queue "
678 "depth adjusted to %d.\n", depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500679 }
680 }
681
James Smartfa61a542008-01-11 01:52:42 -0500682 /*
683 * If there is a thread waiting for command completion
684 * wake up the thread.
685 */
686 spin_lock_irqsave(sdev->host->host_lock, flags);
687 lpfc_cmd->pCmd = NULL;
688 if (lpfc_cmd->waitq)
689 wake_up(lpfc_cmd->waitq);
690 spin_unlock_irqrestore(sdev->host->host_lock, flags);
691
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400692 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500693}
694
695static void
James Smart2e0fef82007-06-17 19:56:36 -0500696lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
697 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -0500698{
James Smart2e0fef82007-06-17 19:56:36 -0500699 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500700 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
701 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
702 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
703 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
704 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -0400705 char tag[2];
dea31012005-04-17 16:05:31 -0500706
707 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400708 /* clear task management bits */
709 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500710
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400711 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
712 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500713
714 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
715
James Smart7e2b19f2007-10-29 11:00:39 -0400716 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
717 switch (tag[0]) {
dea31012005-04-17 16:05:31 -0500718 case HEAD_OF_QUEUE_TAG:
719 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
720 break;
721 case ORDERED_QUEUE_TAG:
722 fcp_cmnd->fcpCntl1 = ORDERED_Q;
723 break;
724 default:
725 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
726 break;
727 }
728 } else
729 fcp_cmnd->fcpCntl1 = 0;
730
731 /*
732 * There are three possibilities here - use scatter-gather segment, use
733 * the single mapping, or neither. Start the lpfc command prep by
734 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
735 * data bde entry.
736 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500737 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500738 if (datadir == DMA_TO_DEVICE) {
739 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
740 iocb_cmd->un.fcpi.fcpi_parm = 0;
741 iocb_cmd->ulpPU = 0;
742 fcp_cmnd->fcpCntl3 = WRITE_DATA;
743 phba->fc4OutputRequests++;
744 } else {
745 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
746 iocb_cmd->ulpPU = PARM_READ_CHECK;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500747 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500748 fcp_cmnd->fcpCntl3 = READ_DATA;
749 phba->fc4InputRequests++;
750 }
751 } else {
752 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
753 iocb_cmd->un.fcpi.fcpi_parm = 0;
754 iocb_cmd->ulpPU = 0;
755 fcp_cmnd->fcpCntl3 = 0;
756 phba->fc4ControlRequests++;
757 }
758
759 /*
760 * Finish initializing those IOCB fields that are independent
761 * of the scsi_cmnd request_buffer
762 */
763 piocbq->iocb.ulpContext = pnode->nlp_rpi;
764 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
765 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -0500766 else
767 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -0500768
769 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
770 piocbq->context1 = lpfc_cmd;
771 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
772 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500773 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -0500774}
775
776static int
James Smart2e0fef82007-06-17 19:56:36 -0500777lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -0500778 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -0400779 unsigned int lun,
dea31012005-04-17 16:05:31 -0500780 uint8_t task_mgmt_cmd)
781{
dea31012005-04-17 16:05:31 -0500782 struct lpfc_iocbq *piocbq;
783 IOCB_t *piocb;
784 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -0400785 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -0500786 struct lpfc_nodelist *ndlp = rdata->pnode;
787
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400788 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea31012005-04-17 16:05:31 -0500789 return 0;
790 }
791
dea31012005-04-17 16:05:31 -0500792 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -0500793 piocbq->vport = vport;
794
dea31012005-04-17 16:05:31 -0500795 piocb = &piocbq->iocb;
796
797 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart420b630d2006-07-06 15:50:16 -0400798 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500799 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
800
801 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
802
803 piocb->ulpContext = ndlp->nlp_rpi;
804 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
805 piocb->ulpFCP2Rcvy = 1;
806 }
807 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
808
809 /* ulpTimeout is only one byte */
810 if (lpfc_cmd->timeout > 0xff) {
811 /*
812 * Do not timeout the command at the firmware level.
813 * The driver will provide the timeout mechanism.
814 */
815 piocb->ulpTimeout = 0;
816 } else {
817 piocb->ulpTimeout = lpfc_cmd->timeout;
818 }
819
James Smart2e0fef82007-06-17 19:56:36 -0500820 return 1;
dea31012005-04-17 16:05:31 -0500821}
822
James Smart7054a602007-04-25 09:52:34 -0400823static void
824lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
825 struct lpfc_iocbq *cmdiocbq,
826 struct lpfc_iocbq *rspiocbq)
827{
828 struct lpfc_scsi_buf *lpfc_cmd =
829 (struct lpfc_scsi_buf *) cmdiocbq->context1;
830 if (lpfc_cmd)
831 lpfc_release_scsi_buf(phba, lpfc_cmd);
832 return;
833}
834
dea31012005-04-17 16:05:31 -0500835static int
James Smart2e0fef82007-06-17 19:56:36 -0500836lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -0400837 unsigned tgt_id, unsigned int lun,
838 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -0500839{
James Smart2e0fef82007-06-17 19:56:36 -0500840 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500841 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400842 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500843 int ret;
844
James Smartf5603512006-12-02 13:35:43 -0500845 if (!rdata->pnode)
846 return FAILED;
847
James Smart0b18ac42006-05-01 21:50:40 -0400848 lpfc_cmd->rdata = rdata;
James Smart2e0fef82007-06-17 19:56:36 -0500849 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -0400850 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -0500851 if (!ret)
852 return FAILED;
853
dea31012005-04-17 16:05:31 -0500854 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400855 iocbqrsp = lpfc_sli_get_iocbq(phba);
856
dea31012005-04-17 16:05:31 -0500857 if (!iocbqrsp)
858 return FAILED;
dea31012005-04-17 16:05:31 -0500859
James Smart0b18ac42006-05-01 21:50:40 -0400860 /* Issue Target Reset to TGT <num> */
James Smarte8b62012007-08-02 11:10:09 -0400861 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
862 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
863 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400864 ret = lpfc_sli_issue_iocb_wait(phba,
865 &phba->sli.ring[phba->sli.fcp_ring],
866 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500867 if (ret != IOCB_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -0400868 if (ret == IOCB_TIMEDOUT)
869 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
dea31012005-04-17 16:05:31 -0500870 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -0500871 } else {
872 ret = SUCCESS;
873 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
874 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
875 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
876 (lpfc_cmd->result & IOERR_DRVR_MASK))
877 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
878 }
879
James Bottomley604a3e32005-10-29 10:28:33 -0500880 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500881 return ret;
882}
883
dea31012005-04-17 16:05:31 -0500884const char *
885lpfc_info(struct Scsi_Host *host)
886{
James Smart2e0fef82007-06-17 19:56:36 -0500887 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
888 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500889 int len;
890 static char lpfcinfobuf[384];
891
892 memset(lpfcinfobuf,0,384);
893 if (phba && phba->pcidev){
894 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
895 len = strlen(lpfcinfobuf);
896 snprintf(lpfcinfobuf + len,
897 384-len,
898 " on PCI bus %02x device %02x irq %d",
899 phba->pcidev->bus->number,
900 phba->pcidev->devfn,
901 phba->pcidev->irq);
902 len = strlen(lpfcinfobuf);
903 if (phba->Port[0]) {
904 snprintf(lpfcinfobuf + len,
905 384-len,
906 " port %s",
907 phba->Port);
908 }
909 }
910 return lpfcinfobuf;
911}
912
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500913static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
914{
915 unsigned long poll_tmo_expires =
916 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
917
918 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
919 mod_timer(&phba->fcp_poll_timer,
920 poll_tmo_expires);
921}
922
923void lpfc_poll_start_timer(struct lpfc_hba * phba)
924{
925 lpfc_poll_rearm_timer(phba);
926}
927
928void lpfc_poll_timeout(unsigned long ptr)
929{
James Smart2e0fef82007-06-17 19:56:36 -0500930 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500931
932 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
933 lpfc_sli_poll_fcp_ring (phba);
934 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
935 lpfc_poll_rearm_timer(phba);
936 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500937}
938
dea31012005-04-17 16:05:31 -0500939static int
940lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
941{
James Smart2e0fef82007-06-17 19:56:36 -0500942 struct Scsi_Host *shost = cmnd->device->host;
943 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
944 struct lpfc_hba *phba = vport->phba;
945 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500946 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
947 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400948 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400949 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400950 int err;
dea31012005-04-17 16:05:31 -0500951
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400952 err = fc_remote_port_chkready(rport);
953 if (err) {
954 cmnd->result = err;
dea31012005-04-17 16:05:31 -0500955 goto out_fail_command;
956 }
957
958 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400959 * Catch race where our node has transitioned, but the
960 * transport is still transitioning.
dea31012005-04-17 16:05:31 -0500961 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400962 if (!ndlp) {
963 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
964 goto out_fail_command;
dea31012005-04-17 16:05:31 -0500965 }
James Smarted957682007-06-17 19:56:37 -0500966 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -0500967 if (lpfc_cmd == NULL) {
James Smart92d7f7b2007-06-17 19:56:38 -0500968 lpfc_adjust_queue_depth(phba);
969
James Smarte8b62012007-08-02 11:10:09 -0400970 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
971 "0707 driver's buffer pool is empty, "
972 "IO busied\n");
dea31012005-04-17 16:05:31 -0500973 goto out_host_busy;
974 }
975
976 /*
977 * Store the midlayer's command structure for the completion phase
978 * and complete the command initialization.
979 */
980 lpfc_cmd->pCmd = cmnd;
981 lpfc_cmd->rdata = rdata;
982 lpfc_cmd->timeout = 0;
983 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
984 cmnd->scsi_done = done;
985
986 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
987 if (err)
988 goto out_host_busy_free_buf;
989
James Smart2e0fef82007-06-17 19:56:36 -0500990 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -0500991
992 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -0500993 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea31012005-04-17 16:05:31 -0500994 if (err)
995 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500996
997 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
998 lpfc_sli_poll_fcp_ring(phba);
999 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1000 lpfc_poll_rearm_timer(phba);
1001 }
1002
dea31012005-04-17 16:05:31 -05001003 return 0;
1004
1005 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04001006 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001007 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001008 out_host_busy:
1009 return SCSI_MLQUEUE_HOST_BUSY;
1010
1011 out_fail_command:
1012 done(cmnd);
1013 return 0;
1014}
1015
James Smarta90f5682006-08-17 11:58:04 -04001016static void
1017lpfc_block_error_handler(struct scsi_cmnd *cmnd)
1018{
1019 struct Scsi_Host *shost = cmnd->device->host;
1020 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1021
1022 spin_lock_irq(shost->host_lock);
1023 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1024 spin_unlock_irq(shost->host_lock);
1025 msleep(1000);
1026 spin_lock_irq(shost->host_lock);
1027 }
1028 spin_unlock_irq(shost->host_lock);
1029 return;
1030}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001031
dea31012005-04-17 16:05:31 -05001032static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001033lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001034{
James Smart2e0fef82007-06-17 19:56:36 -05001035 struct Scsi_Host *shost = cmnd->device->host;
1036 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1037 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001038 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001039 struct lpfc_iocbq *iocb;
1040 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05001041 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05001042 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001043 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05001044 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001045
James Smarta90f5682006-08-17 11:58:04 -04001046 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001047 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1048 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05001049
1050 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001051 * If pCmd field of the corresponding lpfc_scsi_buf structure
1052 * points to a different SCSI command, then the driver has
1053 * already completed this command, but the midlayer did not
1054 * see the completion before the eh fired. Just return
1055 * SUCCESS.
dea31012005-04-17 16:05:31 -05001056 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001057 iocb = &lpfc_cmd->cur_iocbq;
1058 if (lpfc_cmd->pCmd != cmnd)
1059 goto out;
dea31012005-04-17 16:05:31 -05001060
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001061 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05001062
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001063 abtsiocb = lpfc_sli_get_iocbq(phba);
1064 if (abtsiocb == NULL) {
1065 ret = FAILED;
dea31012005-04-17 16:05:31 -05001066 goto out;
1067 }
1068
dea31012005-04-17 16:05:31 -05001069 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001070 * The scsi command can not be in txq and it is in flight because the
1071 * pCmd is still pointig at the SCSI command we have to abort. There
1072 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05001073 */
dea31012005-04-17 16:05:31 -05001074
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001075 cmd = &iocb->iocb;
1076 icmd = &abtsiocb->iocb;
1077 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1078 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1079 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05001080
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001081 icmd->ulpLe = 1;
1082 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05001083 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001084 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1085 else
1086 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05001087
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001088 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05001089 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001090 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1091 lpfc_sli_release_iocbq(phba, abtsiocb);
1092 ret = FAILED;
1093 goto out;
1094 }
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 Smartfa61a542008-01-11 01:52:42 -05001099 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001100 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05001101 wait_event_timeout(waitq,
1102 (lpfc_cmd->pCmd != cmnd),
1103 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001104
James Smartfa61a542008-01-11 01:52:42 -05001105 spin_lock_irq(shost->host_lock);
1106 lpfc_cmd->waitq = NULL;
1107 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001108
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001109 if (lpfc_cmd->pCmd == cmnd) {
1110 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04001111 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1112 "0748 abort handler timed out waiting "
1113 "for abort to complete: ret %#x, ID %d, "
1114 "LUN %d, snum %#lx\n",
1115 ret, cmnd->device->id, cmnd->device->lun,
1116 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001117 }
1118
1119 out:
James Smarte8b62012007-08-02 11:10:09 -04001120 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1121 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
1122 "LUN %d snum %#lx\n", ret, cmnd->device->id,
1123 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001124 return ret;
dea31012005-04-17 16:05:31 -05001125}
1126
1127static int
James Smart7054a602007-04-25 09:52:34 -04001128lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001129{
James Smart2e0fef82007-06-17 19:56:36 -05001130 struct Scsi_Host *shost = cmnd->device->host;
1131 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1132 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001133 struct lpfc_scsi_buf *lpfc_cmd;
1134 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001135 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1136 struct lpfc_nodelist *pnode = rdata->pnode;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001137 uint32_t cmd_result = 0, cmd_status = 0;
dea31012005-04-17 16:05:31 -05001138 int ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001139 int iocb_status = IOCB_SUCCESS;
dea31012005-04-17 16:05:31 -05001140 int cnt, loopcnt;
1141
James Smarta90f5682006-08-17 11:58:04 -04001142 lpfc_block_error_handler(cmnd);
James Smartf5603512006-12-02 13:35:43 -05001143 loopcnt = 0;
dea31012005-04-17 16:05:31 -05001144 /*
1145 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001146 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001147 */
James Smart92d7f7b2007-06-17 19:56:38 -05001148 while (1) {
dea31012005-04-17 16:05:31 -05001149 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001150 goto out;
dea31012005-04-17 16:05:31 -05001151
1152 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001153 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smartf5603512006-12-02 13:35:43 -05001154 loopcnt++;
1155 rdata = cmnd->device->hostdata;
1156 if (!rdata ||
James Smart3de2a652007-08-02 11:09:59 -04001157 (loopcnt > ((vport->cfg_devloss_tmo * 2) + 1))){
James Smarte8b62012007-08-02 11:10:09 -04001158 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1159 "0721 LUN Reset rport "
1160 "failure: cnt x%x rdata x%p\n",
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 Smarte8b62012007-08-02 11:10:09 -04001191 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1192 "0703 Issue target reset to TGT %d LUN %d "
1193 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1194 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
James Smart7054a602007-04-25 09:52:34 -04001195 iocb_status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001196 &phba->sli.ring[phba->sli.fcp_ring],
1197 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -05001198
James Smart7054a602007-04-25 09:52:34 -04001199 if (iocb_status == IOCB_TIMEDOUT)
1200 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
1201
1202 if (iocb_status == IOCB_SUCCESS)
1203 ret = SUCCESS;
1204 else
1205 ret = iocb_status;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001206
1207 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1208 cmd_status = iocbqrsp->iocb.ulpStatus;
1209
1210 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001211
1212 /*
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001213 * All outstanding txcmplq I/Os should have been aborted by the device.
dea31012005-04-17 16:05:31 -05001214 * Unfortunately, some targets do not abide by this forcing the driver
1215 * to double check.
1216 */
James Smart51ef4c22007-08-02 11:10:31 -04001217 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001218 LPFC_CTX_LUN);
1219 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04001220 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001221 cmnd->device->id, cmnd->device->lun,
James Smart51ef4c22007-08-02 11:10:31 -04001222 LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001223 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001224 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001225 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001226
1227 if (++loopcnt
James Smart3de2a652007-08-02 11:09:59 -04001228 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001229 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001230
James Smart51ef4c22007-08-02 11:10:31 -04001231 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
1232 cmnd->device->lun, LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001233 }
1234
1235 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001236 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1237 "0719 device reset I/O flush failure: "
1238 "cnt x%x\n", cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001239 ret = FAILED;
dea31012005-04-17 16:05:31 -05001240 }
1241
dea31012005-04-17 16:05:31 -05001242out_free_scsi_buf:
James Smart7054a602007-04-25 09:52:34 -04001243 if (iocb_status != IOCB_TIMEDOUT) {
1244 lpfc_release_scsi_buf(phba, lpfc_cmd);
1245 }
James Smarte8b62012007-08-02 11:10:09 -04001246 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1247 "0713 SCSI layer issued device reset (%d, %d) "
1248 "return x%x status x%x result x%x\n",
1249 cmnd->device->id, cmnd->device->lun, ret,
1250 cmd_status, cmd_result);
dea31012005-04-17 16:05:31 -05001251out:
1252 return ret;
1253}
1254
Jeff Garzik 94d0e7b2005-05-28 07:55:48 -04001255static int
James Smart7054a602007-04-25 09:52:34 -04001256lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001257{
James Smart2e0fef82007-06-17 19:56:36 -05001258 struct Scsi_Host *shost = cmnd->device->host;
1259 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1260 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001261 struct lpfc_nodelist *ndlp = NULL;
1262 int match;
1263 int ret = FAILED, i, err_count = 0;
1264 int cnt, loopcnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001265 struct lpfc_scsi_buf * lpfc_cmd;
dea31012005-04-17 16:05:31 -05001266
James Smarta90f5682006-08-17 11:58:04 -04001267 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001268
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001269 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001270 if (lpfc_cmd == NULL)
1271 goto out;
1272
1273 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1274 lpfc_cmd->timeout = 60;
dea31012005-04-17 16:05:31 -05001275
1276 /*
1277 * Since the driver manages a single bus device, reset all
1278 * targets known to the driver. Should any target reset
1279 * fail, this routine returns failure to the midlayer.
1280 */
James Smarte17da182006-07-06 15:49:25 -04001281 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001282 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001283 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001284 spin_lock_irq(shost->host_lock);
1285 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001286 if (!NLP_CHK_NODE_ACT(ndlp))
1287 continue;
James Smart685f0bf2007-04-25 09:53:08 -04001288 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1289 i == ndlp->nlp_sid &&
1290 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001291 match = 1;
1292 break;
1293 }
1294 }
James Smart2e0fef82007-06-17 19:56:36 -05001295 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001296 if (!match)
1297 continue;
1298
James Smart2e0fef82007-06-17 19:56:36 -05001299 ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1300 cmnd->device->lun,
James Smart420b630d2006-07-06 15:50:16 -04001301 ndlp->rport->dd_data);
dea31012005-04-17 16:05:31 -05001302 if (ret != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04001303 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1304 "0700 Bus Reset on target %d failed\n",
1305 i);
dea31012005-04-17 16:05:31 -05001306 err_count++;
James Smart7054a602007-04-25 09:52:34 -04001307 break;
dea31012005-04-17 16:05:31 -05001308 }
1309 }
1310
James Smart7054a602007-04-25 09:52:34 -04001311 if (ret != IOCB_TIMEDOUT)
1312 lpfc_release_scsi_buf(phba, lpfc_cmd);
1313
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001314 if (err_count == 0)
1315 ret = SUCCESS;
James Smart7054a602007-04-25 09:52:34 -04001316 else
1317 ret = FAILED;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001318
1319 /*
1320 * All outstanding txcmplq I/Os should have been aborted by
1321 * the targets. Unfortunately, some targets do not abide by
1322 * this forcing the driver to double check.
1323 */
James Smart51ef4c22007-08-02 11:10:31 -04001324 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001325 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04001326 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1327 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001328 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001329 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001330 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001331
1332 if (++loopcnt
James Smart3de2a652007-08-02 11:09:59 -04001333 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001334 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001335
James Smart51ef4c22007-08-02 11:10:31 -04001336 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001337 }
1338
1339 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001340 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1341 "0715 Bus Reset I/O flush failure: "
1342 "cnt x%x left x%x\n", cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001343 ret = FAILED;
dea31012005-04-17 16:05:31 -05001344 }
1345
James Smarte8b62012007-08-02 11:10:09 -04001346 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1347 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05001348out:
1349 return ret;
1350}
1351
1352static int
dea31012005-04-17 16:05:31 -05001353lpfc_slave_alloc(struct scsi_device *sdev)
1354{
James Smart2e0fef82007-06-17 19:56:36 -05001355 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1356 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001357 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001358 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001359 uint32_t total = 0, i;
1360 uint32_t num_to_alloc = 0;
1361 unsigned long flags;
dea31012005-04-17 16:05:31 -05001362
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001363 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001364 return -ENXIO;
1365
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001366 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001367
1368 /*
1369 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1370 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001371 * HBA limit conveyed to the midlayer via the host structure. The
1372 * formula accounts for the lun_queue_depth + error handlers + 1
1373 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001374 */
1375 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04001376 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05001377
1378 /* Allow some exchanges to be available always to complete discovery */
1379 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001380 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1381 "0704 At limitation of %d preallocated "
1382 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05001383 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001384 /* Allow some exchanges to be available always to complete discovery */
1385 } else if (total + num_to_alloc >
1386 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001387 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1388 "0705 Allocation request of %d "
1389 "command buffers will exceed max of %d. "
1390 "Reducing allocation request to %d.\n",
1391 num_to_alloc, phba->cfg_hba_queue_depth,
1392 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001393 num_to_alloc = phba->cfg_hba_queue_depth - total;
1394 }
1395
1396 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05001397 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05001398 if (!scsi_buf) {
James Smarte8b62012007-08-02 11:10:09 -04001399 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1400 "0706 Failed to allocate "
1401 "command buffer\n");
dea31012005-04-17 16:05:31 -05001402 break;
1403 }
1404
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001405 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001406 phba->total_scsi_bufs++;
1407 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001408 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001409 }
1410 return 0;
1411}
1412
1413static int
1414lpfc_slave_configure(struct scsi_device *sdev)
1415{
James Smart2e0fef82007-06-17 19:56:36 -05001416 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1417 struct lpfc_hba *phba = vport->phba;
1418 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05001419
1420 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04001421 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001422 else
James Smart3de2a652007-08-02 11:09:59 -04001423 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001424
1425 /*
1426 * Initialize the fc transport attributes for the target
1427 * containing this scsi device. Also note that the driver's
1428 * target pointer is stored in the starget_data for the
1429 * driver's sysfs entry point functions.
1430 */
James Smart3de2a652007-08-02 11:09:59 -04001431 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05001432
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001433 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1434 lpfc_sli_poll_fcp_ring(phba);
1435 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1436 lpfc_poll_rearm_timer(phba);
1437 }
1438
dea31012005-04-17 16:05:31 -05001439 return 0;
1440}
1441
1442static void
1443lpfc_slave_destroy(struct scsi_device *sdev)
1444{
1445 sdev->hostdata = NULL;
1446 return;
1447}
1448
James Smart92d7f7b2007-06-17 19:56:38 -05001449
dea31012005-04-17 16:05:31 -05001450struct scsi_host_template lpfc_template = {
1451 .module = THIS_MODULE,
1452 .name = LPFC_DRIVER_NAME,
1453 .info = lpfc_info,
1454 .queuecommand = lpfc_queuecommand,
1455 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04001456 .eh_device_reset_handler= lpfc_device_reset_handler,
1457 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05001458 .slave_alloc = lpfc_slave_alloc,
1459 .slave_configure = lpfc_slave_configure,
1460 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04001461 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05001462 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05001463 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05001464 .cmd_per_lun = LPFC_CMD_PER_LUN,
1465 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05001466 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001467 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001468};
James Smart3de2a652007-08-02 11:09:59 -04001469
1470struct scsi_host_template lpfc_vport_template = {
1471 .module = THIS_MODULE,
1472 .name = LPFC_DRIVER_NAME,
1473 .info = lpfc_info,
1474 .queuecommand = lpfc_queuecommand,
1475 .eh_abort_handler = lpfc_abort_handler,
1476 .eh_device_reset_handler= lpfc_device_reset_handler,
1477 .eh_bus_reset_handler = lpfc_bus_reset_handler,
1478 .slave_alloc = lpfc_slave_alloc,
1479 .slave_configure = lpfc_slave_configure,
1480 .slave_destroy = lpfc_slave_destroy,
1481 .scan_finished = lpfc_scan_finished,
1482 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05001483 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04001484 .cmd_per_lun = LPFC_CMD_PER_LUN,
1485 .use_clustering = ENABLE_CLUSTERING,
1486 .shost_attrs = lpfc_vport_attrs,
1487 .max_sectors = 0xFFFF,
1488};