blob: 0910a9ab76a506014073c7af98ad61df8a2a6100 [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) {
James Smart97eab632008-04-07 10:16:05 -0400172 if (vports[i]->cfg_lun_queue_depth <=
173 sdev->queue_depth)
174 continue;
James Smart549e55c2007-08-02 11:09:51 -0400175 if (sdev->ordered_tags)
176 scsi_adjust_queue_depth(sdev,
177 MSG_ORDERED_TAG,
178 sdev->queue_depth+1);
179 else
180 scsi_adjust_queue_depth(sdev,
181 MSG_SIMPLE_TAG,
182 sdev->queue_depth+1);
183 }
James Smart92d7f7b2007-06-17 19:56:38 -0500184 }
James Smart09372822008-01-11 01:52:54 -0500185 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500186 atomic_set(&phba->num_rsrc_err, 0);
187 atomic_set(&phba->num_cmd_success, 0);
188}
189
190/*
dea31012005-04-17 16:05:31 -0500191 * This routine allocates a scsi buffer, which contains all the necessary
192 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
193 * contains information to build the IOCB. The DMAable region contains
194 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
195 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
196 * and the BPL BDE is setup in the IOCB.
197 */
198static struct lpfc_scsi_buf *
James Smart2e0fef82007-06-17 19:56:36 -0500199lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500200{
James Smart2e0fef82007-06-17 19:56:36 -0500201 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500202 struct lpfc_scsi_buf *psb;
203 struct ulp_bde64 *bpl;
204 IOCB_t *iocb;
205 dma_addr_t pdma_phys;
James Bottomley604a3e32005-10-29 10:28:33 -0500206 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500207
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200208 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
dea31012005-04-17 16:05:31 -0500209 if (!psb)
210 return NULL;
dea31012005-04-17 16:05:31 -0500211
212 /*
213 * Get memory from the pci pool to map the virt space to pci bus space
214 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
215 * struct fcp_rsp and the number of bde's necessary to support the
216 * sg_tablesize.
217 */
218 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
219 &psb->dma_handle);
220 if (!psb->data) {
221 kfree(psb);
222 return NULL;
223 }
224
225 /* Initialize virtual ptrs to dma_buf region. */
226 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
227
James Bottomley604a3e32005-10-29 10:28:33 -0500228 /* Allocate iotag for psb->cur_iocbq. */
229 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
230 if (iotag == 0) {
231 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
232 psb->data, psb->dma_handle);
233 kfree (psb);
234 return NULL;
235 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400236 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500237
dea31012005-04-17 16:05:31 -0500238 psb->fcp_cmnd = psb->data;
239 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
240 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
241 sizeof(struct fcp_rsp);
242
243 /* Initialize local short-hand pointers. */
244 bpl = psb->fcp_bpl;
245 pdma_phys = psb->dma_handle;
246
247 /*
248 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
249 * list bdes. Initialize the first two and leave the rest for
250 * queuecommand.
251 */
252 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
253 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
254 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
255 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
256 bpl->tus.w = le32_to_cpu(bpl->tus.w);
257 bpl++;
258
259 /* Setup the physical region for the FCP RSP */
260 pdma_phys += sizeof (struct fcp_cmnd);
261 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
262 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
263 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
264 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
265 bpl->tus.w = le32_to_cpu(bpl->tus.w);
266
267 /*
268 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
269 * initialize it with all known data now.
270 */
271 pdma_phys += (sizeof (struct fcp_rsp));
272 iocb = &psb->cur_iocbq.iocb;
273 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
274 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
275 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
276 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
277 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
278 iocb->ulpBdeCount = 1;
279 iocb->ulpClass = CLASS3;
280
281 return psb;
282}
283
Adrian Bunk455c53e2006-01-06 20:21:28 +0100284static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500285lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500286{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400287 struct lpfc_scsi_buf * lpfc_cmd = NULL;
288 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500289 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500290
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500291 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400292 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400293 if (lpfc_cmd) {
294 lpfc_cmd->seg_cnt = 0;
295 lpfc_cmd->nonsg_phys = 0;
296 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500297 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400298 return lpfc_cmd;
299}
300
301static void
James Smart92d7f7b2007-06-17 19:56:38 -0500302lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400303{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500304 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500305
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500306 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400307 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500308 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500309 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500310}
311
312static int
James Smart92d7f7b2007-06-17 19:56:38 -0500313lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500314{
315 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
316 struct scatterlist *sgel = NULL;
317 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
318 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
319 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
320 dma_addr_t physaddr;
321 uint32_t i, num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500322 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500323
324 /*
325 * There are three possibilities here - use scatter-gather segment, use
326 * the single mapping, or neither. Start the lpfc command prep by
327 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
328 * data bde entry.
329 */
330 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700331 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500332 /*
333 * The driver stores the segment count returned from pci_map_sg
334 * because this a count of dma-mappings used to map the use_sg
335 * pages. They are not guaranteed to be the same for those
336 * architectures that implement an IOMMU.
337 */
dea31012005-04-17 16:05:31 -0500338
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700339 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
340 scsi_sg_count(scsi_cmnd), datadir);
341 if (unlikely(!nseg))
342 return 1;
343
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500344 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500345 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
346 printk(KERN_ERR "%s: Too many sg segments from "
347 "dma_map_sg. Config %d, seg_cnt %d",
348 __FUNCTION__, phba->cfg_sg_seg_cnt,
349 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500350 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500351 return 1;
352 }
353
354 /*
355 * The driver established a maximum scatter-gather segment count
356 * during probe that limits the number of sg elements in any
357 * single scsi command. Just run through the seg_cnt and format
358 * the bde's.
359 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500360 scsi_for_each_sg(scsi_cmnd, sgel, nseg, i) {
dea31012005-04-17 16:05:31 -0500361 physaddr = sg_dma_address(sgel);
362 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
363 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
364 bpl->tus.f.bdeSize = sg_dma_len(sgel);
365 if (datadir == DMA_TO_DEVICE)
366 bpl->tus.f.bdeFlags = 0;
367 else
368 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
369 bpl->tus.w = le32_to_cpu(bpl->tus.w);
370 bpl++;
dea31012005-04-17 16:05:31 -0500371 num_bde++;
372 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700373 }
dea31012005-04-17 16:05:31 -0500374
375 /*
376 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400377 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
378 * reinitialized since all iocb memory resources are used many times
379 * for transmit, receive, and continuation bpl's.
dea31012005-04-17 16:05:31 -0500380 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400381 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500382 iocb_cmd->un.fcpi64.bdl.bdeSize +=
383 (num_bde * sizeof (struct ulp_bde64));
384 iocb_cmd->ulpBdeCount = 1;
385 iocb_cmd->ulpLe = 1;
James Smart09372822008-01-11 01:52:54 -0500386 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
dea31012005-04-17 16:05:31 -0500387 return 0;
388}
389
390static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400391lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
392{
393 /*
394 * There are only two special cases to consider. (1) the scsi command
395 * requested scatter-gather usage or (2) the scsi command allocated
396 * a request buffer, but did not request use_sg. There is a third
397 * case, but it does not require resource deallocation.
398 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500399 if (psb->seg_cnt > 0)
400 scsi_dma_unmap(psb->pCmd);
James Smartbcf4dbf2006-07-06 15:50:08 -0400401}
402
403static void
James Smart2e0fef82007-06-17 19:56:36 -0500404lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
405 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500406{
407 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
408 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
409 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -0400410 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -0500411 uint32_t resp_info = fcprsp->rspStatus2;
412 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500413 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500414 uint32_t host_status = DID_OK;
415 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500416 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500417
418 /*
419 * If this is a task management command, there is no
420 * scsi packet associated with this lpfc_cmd. The driver
421 * consumes it.
422 */
423 if (fcpcmd->fcpCntl2) {
424 scsi_status = 0;
425 goto out;
426 }
427
James Smartc7743952006-12-02 13:34:42 -0500428 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
429 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
430 if (snslen > SCSI_SENSE_BUFFERSIZE)
431 snslen = SCSI_SENSE_BUFFERSIZE;
432
433 if (resp_info & RSP_LEN_VALID)
434 rsplen = be32_to_cpu(fcprsp->rspRspLen);
435 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
436 }
437 lp = (uint32_t *)cmnd->sense_buffer;
438
439 if (!scsi_status && (resp_info & RESID_UNDER))
440 logit = LOG_FCP;
441
James Smarte8b62012007-08-02 11:10:09 -0400442 lpfc_printf_vlog(vport, KERN_WARNING, logit,
443 "0730 FCP command x%x failed: x%x SNS x%x x%x "
444 "Data: x%x x%x x%x x%x x%x\n",
445 cmnd->cmnd[0], scsi_status,
446 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
447 be32_to_cpu(fcprsp->rspResId),
448 be32_to_cpu(fcprsp->rspSnsLen),
449 be32_to_cpu(fcprsp->rspRspLen),
450 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -0500451
452 if (resp_info & RSP_LEN_VALID) {
453 rsplen = be32_to_cpu(fcprsp->rspRspLen);
454 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
455 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
456 host_status = DID_ERROR;
457 goto out;
458 }
459 }
460
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500461 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -0500462 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500463 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -0500464
James Smarte8b62012007-08-02 11:10:09 -0400465 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
466 "0716 FCP Read Underrun, expected %d, "
467 "residual %d Data: x%x x%x x%x\n",
468 be32_to_cpu(fcpcmd->fcpDl),
469 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
470 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500471
472 /*
James Smart7054a602007-04-25 09:52:34 -0400473 * If there is an under run check if under run reported by
474 * storage array is same as the under run reported by HBA.
475 * If this is not same, there is a dropped frame.
476 */
477 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
478 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500479 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -0400480 lpfc_printf_vlog(vport, KERN_WARNING,
481 LOG_FCP | LOG_FCP_ERROR,
482 "0735 FCP Read Check Error "
483 "and Underrun Data: x%x x%x x%x x%x\n",
484 be32_to_cpu(fcpcmd->fcpDl),
485 scsi_get_resid(cmnd), fcpi_parm,
486 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500487 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -0400488 host_status = DID_ERROR;
489 }
490 /*
dea31012005-04-17 16:05:31 -0500491 * The cmnd->underflow is the minimum number of bytes that must
492 * be transfered for this command. Provided a sense condition
493 * is not present, make sure the actual amount transferred is at
494 * least the underflow value or fail.
495 */
496 if (!(resp_info & SNS_LEN_VALID) &&
497 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500498 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
499 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -0400500 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
501 "0717 FCP command x%x residual "
502 "underrun converted to error "
503 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -0400504 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -0400505 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -0500506 host_status = DID_ERROR;
507 }
508 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -0400509 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
510 "0720 FCP command x%x residual overrun error. "
511 "Data: x%x x%x \n", cmnd->cmnd[0],
512 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -0500513 host_status = DID_ERROR;
514
515 /*
516 * Check SLI validation that all the transfer was actually done
517 * (fcpi_parm should be zero). Apply check only to reads.
518 */
519 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
520 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -0400521 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
522 "0734 FCP Read Check Error Data: "
523 "x%x x%x x%x x%x\n",
524 be32_to_cpu(fcpcmd->fcpDl),
525 be32_to_cpu(fcprsp->rspResId),
526 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -0500527 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500528 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -0500529 }
530
531 out:
532 cmnd->result = ScsiResult(host_status, scsi_status);
533}
534
535static void
536lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
537 struct lpfc_iocbq *pIocbOut)
538{
539 struct lpfc_scsi_buf *lpfc_cmd =
540 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500541 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500542 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
543 struct lpfc_nodelist *pnode = rdata->pnode;
544 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500545 int result;
546 struct scsi_device *sdev, *tmp_sdev;
547 int depth = 0;
James Smartfa61a542008-01-11 01:52:42 -0500548 unsigned long flags;
dea31012005-04-17 16:05:31 -0500549
550 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
551 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
552
553 if (lpfc_cmd->status) {
554 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
555 (lpfc_cmd->result & IOERR_DRVR_MASK))
556 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
557 else if (lpfc_cmd->status >= IOSTAT_CNT)
558 lpfc_cmd->status = IOSTAT_DEFAULT;
559
James Smarte8b62012007-08-02 11:10:09 -0400560 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
561 "0729 FCP cmd x%x failed <%d/%d> "
562 "status: x%x result: x%x Data: x%x x%x\n",
563 cmd->cmnd[0],
564 cmd->device ? cmd->device->id : 0xffff,
565 cmd->device ? cmd->device->lun : 0xffff,
566 lpfc_cmd->status, lpfc_cmd->result,
567 pIocbOut->iocb.ulpContext,
568 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -0500569
570 switch (lpfc_cmd->status) {
571 case IOSTAT_FCP_RSP_ERROR:
572 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500573 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500574 break;
575 case IOSTAT_NPORT_BSY:
576 case IOSTAT_FABRIC_BSY:
577 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
578 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500579 case IOSTAT_LOCAL_REJECT:
580 if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
581 lpfc_cmd->result == IOERR_NO_RESOURCES ||
582 lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
583 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -0400584 break;
585 } /* else: fall through */
dea31012005-04-17 16:05:31 -0500586 default:
587 cmd->result = ScsiResult(DID_ERROR, 0);
588 break;
589 }
590
James Smart58da1ff2008-04-07 10:15:56 -0400591 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400592 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
593 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500594 } else {
595 cmd->result = ScsiResult(DID_OK, 0);
596 }
597
598 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
599 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
600
James Smarte8b62012007-08-02 11:10:09 -0400601 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
602 "0710 Iodone <%d/%d> cmd %p, error "
603 "x%x SNS x%x x%x Data: x%x x%x\n",
604 cmd->device->id, cmd->device->lun, cmd,
605 cmd->result, *lp, *(lp + 3), cmd->retries,
606 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -0500607 }
608
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500609 result = cmd->result;
610 sdev = cmd->device;
James Smart1dcb58e2007-04-25 09:51:30 -0400611 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smart97eab632008-04-07 10:16:05 -0400612 spin_lock_irqsave(sdev->host->host_lock, flags);
613 lpfc_cmd->pCmd = NULL; /* This must be done before scsi_done */
614 spin_unlock_irqrestore(sdev->host->host_lock, flags);
dea31012005-04-17 16:05:31 -0500615 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400616
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500617 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -0500618 /*
619 * If there is a thread waiting for command completion
620 * wake up the thread.
621 */
622 spin_lock_irqsave(sdev->host->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -0500623 if (lpfc_cmd->waitq)
624 wake_up(lpfc_cmd->waitq);
625 spin_unlock_irqrestore(sdev->host->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500626 lpfc_release_scsi_buf(phba, lpfc_cmd);
627 return;
628 }
629
James Smart92d7f7b2007-06-17 19:56:38 -0500630
631 if (!result)
James Smart3de2a652007-08-02 11:09:59 -0400632 lpfc_rampup_queue_depth(vport, sdev);
James Smart92d7f7b2007-06-17 19:56:38 -0500633
James Smart58da1ff2008-04-07 10:15:56 -0400634 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500635 ((jiffies - pnode->last_ramp_up_time) >
636 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
637 ((jiffies - pnode->last_q_full_time) >
638 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smart3de2a652007-08-02 11:09:59 -0400639 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500640 shost_for_each_device(tmp_sdev, sdev->host) {
James Smart3de2a652007-08-02 11:09:59 -0400641 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500642 if (tmp_sdev->id != sdev->id)
643 continue;
644 if (tmp_sdev->ordered_tags)
645 scsi_adjust_queue_depth(tmp_sdev,
646 MSG_ORDERED_TAG,
647 tmp_sdev->queue_depth+1);
648 else
649 scsi_adjust_queue_depth(tmp_sdev,
650 MSG_SIMPLE_TAG,
651 tmp_sdev->queue_depth+1);
652
653 pnode->last_ramp_up_time = jiffies;
654 }
655 }
656 }
657
658 /*
659 * Check for queue full. If the lun is reporting queue full, then
660 * back off the lun queue depth to prevent target overloads.
661 */
James Smart58da1ff2008-04-07 10:15:56 -0400662 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
663 NLP_CHK_NODE_ACT(pnode)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500664 pnode->last_q_full_time = jiffies;
665
666 shost_for_each_device(tmp_sdev, sdev->host) {
667 if (tmp_sdev->id != sdev->id)
668 continue;
669 depth = scsi_track_queue_full(tmp_sdev,
670 tmp_sdev->queue_depth - 1);
671 }
672 /*
James Smart2e0fef82007-06-17 19:56:36 -0500673 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500674 * Modify the returned error code to store
675 * the final depth value set by
676 * scsi_track_queue_full.
677 */
678 if (depth == -1)
679 depth = sdev->host->cmd_per_lun;
680
681 if (depth) {
James Smarte8b62012007-08-02 11:10:09 -0400682 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
683 "0711 detected queue full - lun queue "
684 "depth adjusted to %d.\n", depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500685 }
686 }
687
James Smartfa61a542008-01-11 01:52:42 -0500688 /*
689 * If there is a thread waiting for command completion
690 * wake up the thread.
691 */
692 spin_lock_irqsave(sdev->host->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -0500693 if (lpfc_cmd->waitq)
694 wake_up(lpfc_cmd->waitq);
695 spin_unlock_irqrestore(sdev->host->host_lock, flags);
696
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400697 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500698}
699
700static void
James Smart2e0fef82007-06-17 19:56:36 -0500701lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
702 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -0500703{
James Smart2e0fef82007-06-17 19:56:36 -0500704 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500705 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
706 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
707 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
708 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
709 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -0400710 char tag[2];
dea31012005-04-17 16:05:31 -0500711
James Smart58da1ff2008-04-07 10:15:56 -0400712 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
713 return;
714
dea31012005-04-17 16:05:31 -0500715 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400716 /* clear task management bits */
717 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500718
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400719 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
720 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500721
722 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
723
James Smart7e2b19f2007-10-29 11:00:39 -0400724 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
725 switch (tag[0]) {
dea31012005-04-17 16:05:31 -0500726 case HEAD_OF_QUEUE_TAG:
727 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
728 break;
729 case ORDERED_QUEUE_TAG:
730 fcp_cmnd->fcpCntl1 = ORDERED_Q;
731 break;
732 default:
733 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
734 break;
735 }
736 } else
737 fcp_cmnd->fcpCntl1 = 0;
738
739 /*
740 * There are three possibilities here - use scatter-gather segment, use
741 * the single mapping, or neither. Start the lpfc command prep by
742 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
743 * data bde entry.
744 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500745 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500746 if (datadir == DMA_TO_DEVICE) {
747 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
748 iocb_cmd->un.fcpi.fcpi_parm = 0;
749 iocb_cmd->ulpPU = 0;
750 fcp_cmnd->fcpCntl3 = WRITE_DATA;
751 phba->fc4OutputRequests++;
752 } else {
753 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
754 iocb_cmd->ulpPU = PARM_READ_CHECK;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500755 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500756 fcp_cmnd->fcpCntl3 = READ_DATA;
757 phba->fc4InputRequests++;
758 }
759 } else {
760 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
761 iocb_cmd->un.fcpi.fcpi_parm = 0;
762 iocb_cmd->ulpPU = 0;
763 fcp_cmnd->fcpCntl3 = 0;
764 phba->fc4ControlRequests++;
765 }
766
767 /*
768 * Finish initializing those IOCB fields that are independent
769 * of the scsi_cmnd request_buffer
770 */
771 piocbq->iocb.ulpContext = pnode->nlp_rpi;
772 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
773 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -0500774 else
775 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -0500776
777 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
778 piocbq->context1 = lpfc_cmd;
779 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
780 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500781 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -0500782}
783
784static int
James Smart2e0fef82007-06-17 19:56:36 -0500785lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -0500786 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -0400787 unsigned int lun,
dea31012005-04-17 16:05:31 -0500788 uint8_t task_mgmt_cmd)
789{
dea31012005-04-17 16:05:31 -0500790 struct lpfc_iocbq *piocbq;
791 IOCB_t *piocb;
792 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -0400793 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -0500794 struct lpfc_nodelist *ndlp = rdata->pnode;
795
James Smart58da1ff2008-04-07 10:15:56 -0400796 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
797 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -0500798 return 0;
dea31012005-04-17 16:05:31 -0500799
dea31012005-04-17 16:05:31 -0500800 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -0500801 piocbq->vport = vport;
802
dea31012005-04-17 16:05:31 -0500803 piocb = &piocbq->iocb;
804
805 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart420b630d2006-07-06 15:50:16 -0400806 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500807 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
808
809 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
810
811 piocb->ulpContext = ndlp->nlp_rpi;
812 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
813 piocb->ulpFCP2Rcvy = 1;
814 }
815 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
816
817 /* ulpTimeout is only one byte */
818 if (lpfc_cmd->timeout > 0xff) {
819 /*
820 * Do not timeout the command at the firmware level.
821 * The driver will provide the timeout mechanism.
822 */
823 piocb->ulpTimeout = 0;
824 } else {
825 piocb->ulpTimeout = lpfc_cmd->timeout;
826 }
827
James Smart2e0fef82007-06-17 19:56:36 -0500828 return 1;
dea31012005-04-17 16:05:31 -0500829}
830
James Smart7054a602007-04-25 09:52:34 -0400831static void
832lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
833 struct lpfc_iocbq *cmdiocbq,
834 struct lpfc_iocbq *rspiocbq)
835{
836 struct lpfc_scsi_buf *lpfc_cmd =
837 (struct lpfc_scsi_buf *) cmdiocbq->context1;
838 if (lpfc_cmd)
839 lpfc_release_scsi_buf(phba, lpfc_cmd);
840 return;
841}
842
dea31012005-04-17 16:05:31 -0500843static int
James Smart2e0fef82007-06-17 19:56:36 -0500844lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -0400845 unsigned tgt_id, unsigned int lun,
846 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -0500847{
James Smart2e0fef82007-06-17 19:56:36 -0500848 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500849 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400850 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500851 int ret;
852
James Smart58da1ff2008-04-07 10:15:56 -0400853 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartf5603512006-12-02 13:35:43 -0500854 return FAILED;
855
James Smart0b18ac42006-05-01 21:50:40 -0400856 lpfc_cmd->rdata = rdata;
James Smart2e0fef82007-06-17 19:56:36 -0500857 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -0400858 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -0500859 if (!ret)
860 return FAILED;
861
dea31012005-04-17 16:05:31 -0500862 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400863 iocbqrsp = lpfc_sli_get_iocbq(phba);
864
dea31012005-04-17 16:05:31 -0500865 if (!iocbqrsp)
866 return FAILED;
dea31012005-04-17 16:05:31 -0500867
James Smart0b18ac42006-05-01 21:50:40 -0400868 /* Issue Target Reset to TGT <num> */
James Smarte8b62012007-08-02 11:10:09 -0400869 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
870 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
871 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400872 ret = lpfc_sli_issue_iocb_wait(phba,
873 &phba->sli.ring[phba->sli.fcp_ring],
874 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500875 if (ret != IOCB_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -0400876 if (ret == IOCB_TIMEDOUT)
877 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
dea31012005-04-17 16:05:31 -0500878 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -0500879 } else {
880 ret = SUCCESS;
881 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
882 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
883 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
884 (lpfc_cmd->result & IOERR_DRVR_MASK))
885 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
886 }
887
James Bottomley604a3e32005-10-29 10:28:33 -0500888 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500889 return ret;
890}
891
dea31012005-04-17 16:05:31 -0500892const char *
893lpfc_info(struct Scsi_Host *host)
894{
James Smart2e0fef82007-06-17 19:56:36 -0500895 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
896 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500897 int len;
898 static char lpfcinfobuf[384];
899
900 memset(lpfcinfobuf,0,384);
901 if (phba && phba->pcidev){
902 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
903 len = strlen(lpfcinfobuf);
904 snprintf(lpfcinfobuf + len,
905 384-len,
906 " on PCI bus %02x device %02x irq %d",
907 phba->pcidev->bus->number,
908 phba->pcidev->devfn,
909 phba->pcidev->irq);
910 len = strlen(lpfcinfobuf);
911 if (phba->Port[0]) {
912 snprintf(lpfcinfobuf + len,
913 384-len,
914 " port %s",
915 phba->Port);
916 }
917 }
918 return lpfcinfobuf;
919}
920
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500921static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
922{
923 unsigned long poll_tmo_expires =
924 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
925
926 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
927 mod_timer(&phba->fcp_poll_timer,
928 poll_tmo_expires);
929}
930
931void lpfc_poll_start_timer(struct lpfc_hba * phba)
932{
933 lpfc_poll_rearm_timer(phba);
934}
935
936void lpfc_poll_timeout(unsigned long ptr)
937{
James Smart2e0fef82007-06-17 19:56:36 -0500938 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500939
940 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
941 lpfc_sli_poll_fcp_ring (phba);
942 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
943 lpfc_poll_rearm_timer(phba);
944 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500945}
946
dea31012005-04-17 16:05:31 -0500947static int
948lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
949{
James Smart2e0fef82007-06-17 19:56:36 -0500950 struct Scsi_Host *shost = cmnd->device->host;
951 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
952 struct lpfc_hba *phba = vport->phba;
953 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500954 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
955 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400956 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400957 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400958 int err;
dea31012005-04-17 16:05:31 -0500959
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400960 err = fc_remote_port_chkready(rport);
961 if (err) {
962 cmnd->result = err;
dea31012005-04-17 16:05:31 -0500963 goto out_fail_command;
964 }
965
966 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400967 * Catch race where our node has transitioned, but the
968 * transport is still transitioning.
dea31012005-04-17 16:05:31 -0500969 */
James Smart58da1ff2008-04-07 10:15:56 -0400970 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400971 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
972 goto out_fail_command;
dea31012005-04-17 16:05:31 -0500973 }
James Smarted957682007-06-17 19:56:37 -0500974 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -0500975 if (lpfc_cmd == NULL) {
James Smart92d7f7b2007-06-17 19:56:38 -0500976 lpfc_adjust_queue_depth(phba);
977
James Smarte8b62012007-08-02 11:10:09 -0400978 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
979 "0707 driver's buffer pool is empty, "
980 "IO busied\n");
dea31012005-04-17 16:05:31 -0500981 goto out_host_busy;
982 }
983
984 /*
985 * Store the midlayer's command structure for the completion phase
986 * and complete the command initialization.
987 */
988 lpfc_cmd->pCmd = cmnd;
989 lpfc_cmd->rdata = rdata;
990 lpfc_cmd->timeout = 0;
991 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
992 cmnd->scsi_done = done;
993
994 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
995 if (err)
996 goto out_host_busy_free_buf;
997
James Smart2e0fef82007-06-17 19:56:36 -0500998 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -0500999
1000 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -05001001 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea31012005-04-17 16:05:31 -05001002 if (err)
1003 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001004
1005 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1006 lpfc_sli_poll_fcp_ring(phba);
1007 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1008 lpfc_poll_rearm_timer(phba);
1009 }
1010
dea31012005-04-17 16:05:31 -05001011 return 0;
1012
1013 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04001014 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001015 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001016 out_host_busy:
1017 return SCSI_MLQUEUE_HOST_BUSY;
1018
1019 out_fail_command:
1020 done(cmnd);
1021 return 0;
1022}
1023
James Smarta90f5682006-08-17 11:58:04 -04001024static void
1025lpfc_block_error_handler(struct scsi_cmnd *cmnd)
1026{
1027 struct Scsi_Host *shost = cmnd->device->host;
1028 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1029
1030 spin_lock_irq(shost->host_lock);
1031 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1032 spin_unlock_irq(shost->host_lock);
1033 msleep(1000);
1034 spin_lock_irq(shost->host_lock);
1035 }
1036 spin_unlock_irq(shost->host_lock);
1037 return;
1038}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001039
dea31012005-04-17 16:05:31 -05001040static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001041lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001042{
James Smart2e0fef82007-06-17 19:56:36 -05001043 struct Scsi_Host *shost = cmnd->device->host;
1044 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1045 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001046 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001047 struct lpfc_iocbq *iocb;
1048 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05001049 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05001050 IOCB_t *cmd, *icmd;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001051 int ret = SUCCESS;
James Smartfa61a542008-01-11 01:52:42 -05001052 DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001053
James Smarta90f5682006-08-17 11:58:04 -04001054 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001055 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1056 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05001057
1058 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001059 * If pCmd field of the corresponding lpfc_scsi_buf structure
1060 * points to a different SCSI command, then the driver has
1061 * already completed this command, but the midlayer did not
1062 * see the completion before the eh fired. Just return
1063 * SUCCESS.
dea31012005-04-17 16:05:31 -05001064 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001065 iocb = &lpfc_cmd->cur_iocbq;
1066 if (lpfc_cmd->pCmd != cmnd)
1067 goto out;
dea31012005-04-17 16:05:31 -05001068
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001069 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05001070
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001071 abtsiocb = lpfc_sli_get_iocbq(phba);
1072 if (abtsiocb == NULL) {
1073 ret = FAILED;
dea31012005-04-17 16:05:31 -05001074 goto out;
1075 }
1076
dea31012005-04-17 16:05:31 -05001077 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001078 * The scsi command can not be in txq and it is in flight because the
1079 * pCmd is still pointig at the SCSI command we have to abort. There
1080 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05001081 */
dea31012005-04-17 16:05:31 -05001082
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001083 cmd = &iocb->iocb;
1084 icmd = &abtsiocb->iocb;
1085 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1086 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1087 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05001088
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001089 icmd->ulpLe = 1;
1090 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05001091 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001092 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1093 else
1094 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05001095
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001096 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05001097 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001098 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1099 lpfc_sli_release_iocbq(phba, abtsiocb);
1100 ret = FAILED;
1101 goto out;
1102 }
1103
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001104 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1105 lpfc_sli_poll_fcp_ring (phba);
1106
James Smartfa61a542008-01-11 01:52:42 -05001107 lpfc_cmd->waitq = &waitq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001108 /* Wait for abort to complete */
James Smartfa61a542008-01-11 01:52:42 -05001109 wait_event_timeout(waitq,
1110 (lpfc_cmd->pCmd != cmnd),
1111 (2*vport->cfg_devloss_tmo*HZ));
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001112
James Smartfa61a542008-01-11 01:52:42 -05001113 spin_lock_irq(shost->host_lock);
1114 lpfc_cmd->waitq = NULL;
1115 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001116
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001117 if (lpfc_cmd->pCmd == cmnd) {
1118 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04001119 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1120 "0748 abort handler timed out waiting "
1121 "for abort to complete: ret %#x, ID %d, "
1122 "LUN %d, snum %#lx\n",
1123 ret, cmnd->device->id, cmnd->device->lun,
1124 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001125 }
1126
1127 out:
James Smarte8b62012007-08-02 11:10:09 -04001128 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1129 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
1130 "LUN %d snum %#lx\n", ret, cmnd->device->id,
1131 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001132 return ret;
dea31012005-04-17 16:05:31 -05001133}
1134
1135static int
James Smart7054a602007-04-25 09:52:34 -04001136lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001137{
James Smart2e0fef82007-06-17 19:56:36 -05001138 struct Scsi_Host *shost = cmnd->device->host;
1139 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1140 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001141 struct lpfc_scsi_buf *lpfc_cmd;
1142 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001143 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1144 struct lpfc_nodelist *pnode = rdata->pnode;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001145 uint32_t cmd_result = 0, cmd_status = 0;
dea31012005-04-17 16:05:31 -05001146 int ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001147 int iocb_status = IOCB_SUCCESS;
dea31012005-04-17 16:05:31 -05001148 int cnt, loopcnt;
1149
James Smarta90f5682006-08-17 11:58:04 -04001150 lpfc_block_error_handler(cmnd);
James Smartf5603512006-12-02 13:35:43 -05001151 loopcnt = 0;
dea31012005-04-17 16:05:31 -05001152 /*
1153 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001154 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001155 */
James Smart92d7f7b2007-06-17 19:56:38 -05001156 while (1) {
James Smart58da1ff2008-04-07 10:15:56 -04001157 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smart7054a602007-04-25 09:52:34 -04001158 goto out;
dea31012005-04-17 16:05:31 -05001159
1160 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001161 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smartf5603512006-12-02 13:35:43 -05001162 loopcnt++;
1163 rdata = cmnd->device->hostdata;
1164 if (!rdata ||
James Smart3de2a652007-08-02 11:09:59 -04001165 (loopcnt > ((vport->cfg_devloss_tmo * 2) + 1))){
James Smarte8b62012007-08-02 11:10:09 -04001166 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1167 "0721 LUN Reset rport "
1168 "failure: cnt x%x rdata x%p\n",
1169 loopcnt, rdata);
James Smartf5603512006-12-02 13:35:43 -05001170 goto out;
1171 }
1172 pnode = rdata->pnode;
James Smart58da1ff2008-04-07 10:15:56 -04001173 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smart7054a602007-04-25 09:52:34 -04001174 goto out;
dea31012005-04-17 16:05:31 -05001175 }
James Smartf5603512006-12-02 13:35:43 -05001176 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001177 break;
1178 }
1179
James Smart2e0fef82007-06-17 19:56:36 -05001180 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001181 if (lpfc_cmd == NULL)
1182 goto out;
1183
dea31012005-04-17 16:05:31 -05001184 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001185 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001186
James Smart2e0fef82007-06-17 19:56:36 -05001187 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
James Smart7054a602007-04-25 09:52:34 -04001188 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05001189 if (!ret)
1190 goto out_free_scsi_buf;
1191
1192 iocbq = &lpfc_cmd->cur_iocbq;
1193
1194 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001195 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001196 if (iocbqrsp == NULL)
1197 goto out_free_scsi_buf;
1198
James Smarte8b62012007-08-02 11:10:09 -04001199 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1200 "0703 Issue target reset to TGT %d LUN %d "
1201 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1202 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
James Smart7054a602007-04-25 09:52:34 -04001203 iocb_status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001204 &phba->sli.ring[phba->sli.fcp_ring],
1205 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -05001206
James Smart7054a602007-04-25 09:52:34 -04001207 if (iocb_status == IOCB_TIMEDOUT)
1208 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
1209
1210 if (iocb_status == IOCB_SUCCESS)
1211 ret = SUCCESS;
1212 else
1213 ret = iocb_status;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001214
1215 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1216 cmd_status = iocbqrsp->iocb.ulpStatus;
1217
1218 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001219
1220 /*
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001221 * All outstanding txcmplq I/Os should have been aborted by the device.
dea31012005-04-17 16:05:31 -05001222 * Unfortunately, some targets do not abide by this forcing the driver
1223 * to double check.
1224 */
James Smart51ef4c22007-08-02 11:10:31 -04001225 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001226 LPFC_CTX_LUN);
1227 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04001228 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001229 cmnd->device->id, cmnd->device->lun,
James Smart51ef4c22007-08-02 11:10:31 -04001230 LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001231 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001232 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001233 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001234
1235 if (++loopcnt
James Smart3de2a652007-08-02 11:09:59 -04001236 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001237 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001238
James Smart51ef4c22007-08-02 11:10:31 -04001239 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
1240 cmnd->device->lun, LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001241 }
1242
1243 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001244 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1245 "0719 device reset I/O flush failure: "
1246 "cnt x%x\n", cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001247 ret = FAILED;
dea31012005-04-17 16:05:31 -05001248 }
1249
dea31012005-04-17 16:05:31 -05001250out_free_scsi_buf:
James Smart7054a602007-04-25 09:52:34 -04001251 if (iocb_status != IOCB_TIMEDOUT) {
1252 lpfc_release_scsi_buf(phba, lpfc_cmd);
1253 }
James Smarte8b62012007-08-02 11:10:09 -04001254 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1255 "0713 SCSI layer issued device reset (%d, %d) "
1256 "return x%x status x%x result x%x\n",
1257 cmnd->device->id, cmnd->device->lun, ret,
1258 cmd_status, cmd_result);
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 Smarte47c9092008-02-08 18:49:26 -05001294 if (!NLP_CHK_NODE_ACT(ndlp))
1295 continue;
James Smart685f0bf2007-04-25 09:53:08 -04001296 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1297 i == ndlp->nlp_sid &&
1298 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001299 match = 1;
1300 break;
1301 }
1302 }
James Smart2e0fef82007-06-17 19:56:36 -05001303 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001304 if (!match)
1305 continue;
1306
James Smart2e0fef82007-06-17 19:56:36 -05001307 ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1308 cmnd->device->lun,
James Smart420b630d2006-07-06 15:50:16 -04001309 ndlp->rport->dd_data);
dea31012005-04-17 16:05:31 -05001310 if (ret != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04001311 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1312 "0700 Bus Reset on target %d failed\n",
1313 i);
dea31012005-04-17 16:05:31 -05001314 err_count++;
James Smart7054a602007-04-25 09:52:34 -04001315 break;
dea31012005-04-17 16:05:31 -05001316 }
1317 }
1318
James Smart7054a602007-04-25 09:52:34 -04001319 if (ret != IOCB_TIMEDOUT)
1320 lpfc_release_scsi_buf(phba, lpfc_cmd);
1321
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001322 if (err_count == 0)
1323 ret = SUCCESS;
James Smart7054a602007-04-25 09:52:34 -04001324 else
1325 ret = FAILED;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001326
1327 /*
1328 * All outstanding txcmplq I/Os should have been aborted by
1329 * the targets. Unfortunately, some targets do not abide by
1330 * this forcing the driver to double check.
1331 */
James Smart51ef4c22007-08-02 11:10:31 -04001332 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001333 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04001334 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1335 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
James Smart51ef4c22007-08-02 11:10:31 -04001344 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001345 }
1346
1347 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001348 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1349 "0715 Bus Reset I/O flush failure: "
1350 "cnt x%x left x%x\n", cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001351 ret = FAILED;
dea31012005-04-17 16:05:31 -05001352 }
1353
James Smarte8b62012007-08-02 11:10:09 -04001354 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1355 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05001356out:
1357 return ret;
1358}
1359
1360static int
dea31012005-04-17 16:05:31 -05001361lpfc_slave_alloc(struct scsi_device *sdev)
1362{
James Smart2e0fef82007-06-17 19:56:36 -05001363 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1364 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001365 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001366 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001367 uint32_t total = 0, i;
1368 uint32_t num_to_alloc = 0;
1369 unsigned long flags;
dea31012005-04-17 16:05:31 -05001370
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001371 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001372 return -ENXIO;
1373
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001374 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001375
1376 /*
1377 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1378 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001379 * HBA limit conveyed to the midlayer via the host structure. The
1380 * formula accounts for the lun_queue_depth + error handlers + 1
1381 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001382 */
1383 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04001384 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05001385
1386 /* Allow some exchanges to be available always to complete discovery */
1387 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001388 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1389 "0704 At limitation of %d preallocated "
1390 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05001391 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001392 /* Allow some exchanges to be available always to complete discovery */
1393 } else if (total + num_to_alloc >
1394 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001395 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1396 "0705 Allocation request of %d "
1397 "command buffers will exceed max of %d. "
1398 "Reducing allocation request to %d.\n",
1399 num_to_alloc, phba->cfg_hba_queue_depth,
1400 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001401 num_to_alloc = phba->cfg_hba_queue_depth - total;
1402 }
1403
1404 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05001405 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05001406 if (!scsi_buf) {
James Smarte8b62012007-08-02 11:10:09 -04001407 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1408 "0706 Failed to allocate "
1409 "command buffer\n");
dea31012005-04-17 16:05:31 -05001410 break;
1411 }
1412
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001413 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001414 phba->total_scsi_bufs++;
1415 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001416 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001417 }
1418 return 0;
1419}
1420
1421static int
1422lpfc_slave_configure(struct scsi_device *sdev)
1423{
James Smart2e0fef82007-06-17 19:56:36 -05001424 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1425 struct lpfc_hba *phba = vport->phba;
1426 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05001427
1428 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04001429 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001430 else
James Smart3de2a652007-08-02 11:09:59 -04001431 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001432
1433 /*
1434 * Initialize the fc transport attributes for the target
1435 * containing this scsi device. Also note that the driver's
1436 * target pointer is stored in the starget_data for the
1437 * driver's sysfs entry point functions.
1438 */
James Smart3de2a652007-08-02 11:09:59 -04001439 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05001440
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001441 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1442 lpfc_sli_poll_fcp_ring(phba);
1443 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1444 lpfc_poll_rearm_timer(phba);
1445 }
1446
dea31012005-04-17 16:05:31 -05001447 return 0;
1448}
1449
1450static void
1451lpfc_slave_destroy(struct scsi_device *sdev)
1452{
1453 sdev->hostdata = NULL;
1454 return;
1455}
1456
James Smart92d7f7b2007-06-17 19:56:38 -05001457
dea31012005-04-17 16:05:31 -05001458struct scsi_host_template lpfc_template = {
1459 .module = THIS_MODULE,
1460 .name = LPFC_DRIVER_NAME,
1461 .info = lpfc_info,
1462 .queuecommand = lpfc_queuecommand,
1463 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04001464 .eh_device_reset_handler= lpfc_device_reset_handler,
1465 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05001466 .slave_alloc = lpfc_slave_alloc,
1467 .slave_configure = lpfc_slave_configure,
1468 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04001469 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05001470 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05001471 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05001472 .cmd_per_lun = LPFC_CMD_PER_LUN,
1473 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05001474 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001475 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001476};
James Smart3de2a652007-08-02 11:09:59 -04001477
1478struct scsi_host_template lpfc_vport_template = {
1479 .module = THIS_MODULE,
1480 .name = LPFC_DRIVER_NAME,
1481 .info = lpfc_info,
1482 .queuecommand = lpfc_queuecommand,
1483 .eh_abort_handler = lpfc_abort_handler,
1484 .eh_device_reset_handler= lpfc_device_reset_handler,
1485 .eh_bus_reset_handler = lpfc_bus_reset_handler,
1486 .slave_alloc = lpfc_slave_alloc,
1487 .slave_configure = lpfc_slave_configure,
1488 .slave_destroy = lpfc_slave_destroy,
1489 .scan_finished = lpfc_scan_finished,
1490 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05001491 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04001492 .cmd_per_lun = LPFC_CMD_PER_LUN,
1493 .use_clustering = ENABLE_CLUSTERING,
1494 .shost_attrs = lpfc_vport_attrs,
1495 .max_sectors = 0xFFFF,
1496};