blob: 17d7dc05149b29a232680a85906b292d848c84a3 [file] [log] [blame]
dea31012005-04-17 16:05:31 -05001/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04003 * Fibre Channel Host Bus Adapters. *
James Smart9413aff2007-04-25 09:53:35 -04004 * Copyright (C) 2004-2007 Emulex. All rights reserved. *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04005 * EMULEX and SLI are trademarks of Emulex. *
dea31012005-04-17 16:05:31 -05006 * www.emulex.com *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -04007 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea31012005-04-17 16:05:31 -05008 * *
9 * This program is free software; you can redistribute it and/or *
James.Smart@Emulex.Comc44ce172005-06-25 10:34:39 -040010 * modify it under the terms of version 2 of the GNU General *
11 * Public License as published by the Free Software Foundation. *
12 * This program is distributed in the hope that it will be useful. *
13 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
14 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
15 * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
16 * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
17 * TO BE LEGALLY INVALID. See the GNU General Public License for *
18 * more details, a copy of which can be found in the file COPYING *
19 * included with this package. *
dea31012005-04-17 16:05:31 -050020 *******************************************************************/
21
dea31012005-04-17 16:05:31 -050022#include <linux/pci.h>
23#include <linux/interrupt.h>
James Smarta90f5682006-08-17 11:58:04 -040024#include <linux/delay.h>
dea31012005-04-17 16:05:31 -050025
26#include <scsi/scsi.h>
27#include <scsi/scsi_device.h>
28#include <scsi/scsi_host.h>
29#include <scsi/scsi_tcq.h>
30#include <scsi/scsi_transport_fc.h>
31
32#include "lpfc_version.h"
33#include "lpfc_hw.h"
34#include "lpfc_sli.h"
35#include "lpfc_disc.h"
36#include "lpfc_scsi.h"
37#include "lpfc.h"
38#include "lpfc_logmsg.h"
39#include "lpfc_crtn.h"
James Smart92d7f7b2007-06-17 19:56:38 -050040#include "lpfc_vport.h"
dea31012005-04-17 16:05:31 -050041
42#define LPFC_RESET_WAIT 2
43#define LPFC_ABORT_WAIT 2
44
dea31012005-04-17 16:05:31 -050045/*
James Smart92d7f7b2007-06-17 19:56:38 -050046 * This function is called with no lock held when there is a resource
47 * error in driver or in firmware.
48 */
49void
50lpfc_adjust_queue_depth(struct lpfc_hba *phba)
51{
52 unsigned long flags;
53
54 spin_lock_irqsave(&phba->hbalock, flags);
55 atomic_inc(&phba->num_rsrc_err);
56 phba->last_rsrc_error_time = jiffies;
57
58 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
59 spin_unlock_irqrestore(&phba->hbalock, flags);
60 return;
61 }
62
63 phba->last_ramp_down_time = jiffies;
64
65 spin_unlock_irqrestore(&phba->hbalock, flags);
66
67 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
68 if ((phba->pport->work_port_events &
69 WORKER_RAMP_DOWN_QUEUE) == 0) {
70 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
71 }
72 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
73
74 spin_lock_irqsave(&phba->hbalock, flags);
75 if (phba->work_wait)
76 wake_up(phba->work_wait);
77 spin_unlock_irqrestore(&phba->hbalock, flags);
78
79 return;
80}
81
82/*
83 * This function is called with no lock held when there is a successful
84 * SCSI command completion.
85 */
86static inline void
James Smart3de2a652007-08-02 11:09:59 -040087lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smart92d7f7b2007-06-17 19:56:38 -050088 struct scsi_device *sdev)
89{
90 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -040091 struct lpfc_hba *phba = vport->phba;
James Smart92d7f7b2007-06-17 19:56:38 -050092 atomic_inc(&phba->num_cmd_success);
93
James Smart3de2a652007-08-02 11:09:59 -040094 if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -050095 return;
James Smart92d7f7b2007-06-17 19:56:38 -050096 spin_lock_irqsave(&phba->hbalock, flags);
97 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
98 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
99 spin_unlock_irqrestore(&phba->hbalock, flags);
100 return;
101 }
James Smart92d7f7b2007-06-17 19:56:38 -0500102 phba->last_ramp_up_time = jiffies;
103 spin_unlock_irqrestore(&phba->hbalock, flags);
104
105 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
106 if ((phba->pport->work_port_events &
107 WORKER_RAMP_UP_QUEUE) == 0) {
108 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
109 }
110 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
111
112 spin_lock_irqsave(&phba->hbalock, flags);
113 if (phba->work_wait)
114 wake_up(phba->work_wait);
115 spin_unlock_irqrestore(&phba->hbalock, flags);
116}
117
118void
119lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
120{
James Smart549e55c2007-08-02 11:09:51 -0400121 struct lpfc_vport **vports;
122 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500123 struct scsi_device *sdev;
124 unsigned long new_queue_depth;
125 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400126 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500127
128 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
129 num_cmd_success = atomic_read(&phba->num_cmd_success);
130
James Smart549e55c2007-08-02 11:09:51 -0400131 vports = lpfc_create_vport_work_array(phba);
132 if (vports != NULL)
133 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
134 shost = lpfc_shost_from_vport(vports[i]);
135 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500136 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400137 sdev->queue_depth * num_rsrc_err /
138 (num_rsrc_err + num_cmd_success);
139 if (!new_queue_depth)
140 new_queue_depth = sdev->queue_depth - 1;
141 else
142 new_queue_depth = sdev->queue_depth -
143 new_queue_depth;
144 if (sdev->ordered_tags)
145 scsi_adjust_queue_depth(sdev,
146 MSG_ORDERED_TAG,
147 new_queue_depth);
148 else
149 scsi_adjust_queue_depth(sdev,
150 MSG_SIMPLE_TAG,
151 new_queue_depth);
152 }
James Smart92d7f7b2007-06-17 19:56:38 -0500153 }
James Smart549e55c2007-08-02 11:09:51 -0400154 lpfc_destroy_vport_work_array(vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500155 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)
169 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
170 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 Smart549e55c2007-08-02 11:09:51 -0400182 lpfc_destroy_vport_work_array(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
205 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
206 if (!psb)
207 return NULL;
208 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
dea31012005-04-17 16:05:31 -0500209
210 /*
211 * Get memory from the pci pool to map the virt space to pci bus space
212 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
213 * struct fcp_rsp and the number of bde's necessary to support the
214 * sg_tablesize.
215 */
216 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
217 &psb->dma_handle);
218 if (!psb->data) {
219 kfree(psb);
220 return NULL;
221 }
222
223 /* Initialize virtual ptrs to dma_buf region. */
224 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
225
James Bottomley604a3e32005-10-29 10:28:33 -0500226 /* Allocate iotag for psb->cur_iocbq. */
227 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
228 if (iotag == 0) {
229 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
230 psb->data, psb->dma_handle);
231 kfree (psb);
232 return NULL;
233 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400234 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500235
dea31012005-04-17 16:05:31 -0500236 psb->fcp_cmnd = psb->data;
237 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
238 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
239 sizeof(struct fcp_rsp);
240
241 /* Initialize local short-hand pointers. */
242 bpl = psb->fcp_bpl;
243 pdma_phys = psb->dma_handle;
244
245 /*
246 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
247 * list bdes. Initialize the first two and leave the rest for
248 * queuecommand.
249 */
250 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
251 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
252 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
253 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
254 bpl->tus.w = le32_to_cpu(bpl->tus.w);
255 bpl++;
256
257 /* Setup the physical region for the FCP RSP */
258 pdma_phys += sizeof (struct fcp_cmnd);
259 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
260 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
261 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
262 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
263 bpl->tus.w = le32_to_cpu(bpl->tus.w);
264
265 /*
266 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
267 * initialize it with all known data now.
268 */
269 pdma_phys += (sizeof (struct fcp_rsp));
270 iocb = &psb->cur_iocbq.iocb;
271 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
272 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
273 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
274 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
275 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
276 iocb->ulpBdeCount = 1;
277 iocb->ulpClass = CLASS3;
278
279 return psb;
280}
281
Adrian Bunk455c53e2006-01-06 20:21:28 +0100282static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500283lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500284{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400285 struct lpfc_scsi_buf * lpfc_cmd = NULL;
286 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500287 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500288
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500289 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400290 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400291 if (lpfc_cmd) {
292 lpfc_cmd->seg_cnt = 0;
293 lpfc_cmd->nonsg_phys = 0;
294 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500295 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400296 return lpfc_cmd;
297}
298
299static void
James Smart92d7f7b2007-06-17 19:56:38 -0500300lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400301{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500302 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500303
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500304 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400305 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500306 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500307 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500308}
309
310static int
James Smart92d7f7b2007-06-17 19:56:38 -0500311lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500312{
313 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
314 struct scatterlist *sgel = NULL;
315 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
316 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
317 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
318 dma_addr_t physaddr;
319 uint32_t i, num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500320 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500321
322 /*
323 * There are three possibilities here - use scatter-gather segment, use
324 * the single mapping, or neither. Start the lpfc command prep by
325 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
326 * data bde entry.
327 */
328 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700329 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500330 /*
331 * The driver stores the segment count returned from pci_map_sg
332 * because this a count of dma-mappings used to map the use_sg
333 * pages. They are not guaranteed to be the same for those
334 * architectures that implement an IOMMU.
335 */
dea31012005-04-17 16:05:31 -0500336
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700337 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
338 scsi_sg_count(scsi_cmnd), datadir);
339 if (unlikely(!nseg))
340 return 1;
341
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500342 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500343 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
344 printk(KERN_ERR "%s: Too many sg segments from "
345 "dma_map_sg. Config %d, seg_cnt %d",
346 __FUNCTION__, phba->cfg_sg_seg_cnt,
347 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500348 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500349 return 1;
350 }
351
352 /*
353 * The driver established a maximum scatter-gather segment count
354 * during probe that limits the number of sg elements in any
355 * single scsi command. Just run through the seg_cnt and format
356 * the bde's.
357 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500358 scsi_for_each_sg(scsi_cmnd, sgel, nseg, i) {
dea31012005-04-17 16:05:31 -0500359 physaddr = sg_dma_address(sgel);
360 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
361 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
362 bpl->tus.f.bdeSize = sg_dma_len(sgel);
363 if (datadir == DMA_TO_DEVICE)
364 bpl->tus.f.bdeFlags = 0;
365 else
366 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
367 bpl->tus.w = le32_to_cpu(bpl->tus.w);
368 bpl++;
dea31012005-04-17 16:05:31 -0500369 num_bde++;
370 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700371 }
dea31012005-04-17 16:05:31 -0500372
373 /*
374 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400375 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
376 * reinitialized since all iocb memory resources are used many times
377 * for transmit, receive, and continuation bpl's.
dea31012005-04-17 16:05:31 -0500378 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400379 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500380 iocb_cmd->un.fcpi64.bdl.bdeSize +=
381 (num_bde * sizeof (struct ulp_bde64));
382 iocb_cmd->ulpBdeCount = 1;
383 iocb_cmd->ulpLe = 1;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500384 fcp_cmnd->fcpDl = be32_to_cpu(scsi_bufflen(scsi_cmnd));
dea31012005-04-17 16:05:31 -0500385 return 0;
386}
387
388static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400389lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
390{
391 /*
392 * There are only two special cases to consider. (1) the scsi command
393 * requested scatter-gather usage or (2) the scsi command allocated
394 * a request buffer, but did not request use_sg. There is a third
395 * case, but it does not require resource deallocation.
396 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500397 if (psb->seg_cnt > 0)
398 scsi_dma_unmap(psb->pCmd);
James Smartbcf4dbf2006-07-06 15:50:08 -0400399}
400
401static void
James Smart2e0fef82007-06-17 19:56:36 -0500402lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
403 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500404{
405 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
406 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
407 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -0400408 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -0500409 uint32_t resp_info = fcprsp->rspStatus2;
410 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500411 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500412 uint32_t host_status = DID_OK;
413 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500414 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500415
416 /*
417 * If this is a task management command, there is no
418 * scsi packet associated with this lpfc_cmd. The driver
419 * consumes it.
420 */
421 if (fcpcmd->fcpCntl2) {
422 scsi_status = 0;
423 goto out;
424 }
425
James Smartc7743952006-12-02 13:34:42 -0500426 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
427 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
428 if (snslen > SCSI_SENSE_BUFFERSIZE)
429 snslen = SCSI_SENSE_BUFFERSIZE;
430
431 if (resp_info & RSP_LEN_VALID)
432 rsplen = be32_to_cpu(fcprsp->rspRspLen);
433 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
434 }
435 lp = (uint32_t *)cmnd->sense_buffer;
436
437 if (!scsi_status && (resp_info & RESID_UNDER))
438 logit = LOG_FCP;
439
James Smarte8b62012007-08-02 11:10:09 -0400440 lpfc_printf_vlog(vport, KERN_WARNING, logit,
441 "0730 FCP command x%x failed: x%x SNS x%x x%x "
442 "Data: x%x x%x x%x x%x x%x\n",
443 cmnd->cmnd[0], scsi_status,
444 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
445 be32_to_cpu(fcprsp->rspResId),
446 be32_to_cpu(fcprsp->rspSnsLen),
447 be32_to_cpu(fcprsp->rspRspLen),
448 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -0500449
450 if (resp_info & RSP_LEN_VALID) {
451 rsplen = be32_to_cpu(fcprsp->rspRspLen);
452 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
453 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
454 host_status = DID_ERROR;
455 goto out;
456 }
457 }
458
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500459 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -0500460 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500461 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -0500462
James Smarte8b62012007-08-02 11:10:09 -0400463 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
464 "0716 FCP Read Underrun, expected %d, "
465 "residual %d Data: x%x x%x x%x\n",
466 be32_to_cpu(fcpcmd->fcpDl),
467 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
468 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500469
470 /*
James Smart7054a602007-04-25 09:52:34 -0400471 * If there is an under run check if under run reported by
472 * storage array is same as the under run reported by HBA.
473 * If this is not same, there is a dropped frame.
474 */
475 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
476 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500477 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -0400478 lpfc_printf_vlog(vport, KERN_WARNING,
479 LOG_FCP | LOG_FCP_ERROR,
480 "0735 FCP Read Check Error "
481 "and Underrun Data: x%x x%x x%x x%x\n",
482 be32_to_cpu(fcpcmd->fcpDl),
483 scsi_get_resid(cmnd), fcpi_parm,
484 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500485 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -0400486 host_status = DID_ERROR;
487 }
488 /*
dea31012005-04-17 16:05:31 -0500489 * The cmnd->underflow is the minimum number of bytes that must
490 * be transfered for this command. Provided a sense condition
491 * is not present, make sure the actual amount transferred is at
492 * least the underflow value or fail.
493 */
494 if (!(resp_info & SNS_LEN_VALID) &&
495 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500496 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
497 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -0400498 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
499 "0717 FCP command x%x residual "
500 "underrun converted to error "
501 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -0400502 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -0400503 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -0500504 host_status = DID_ERROR;
505 }
506 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -0400507 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
508 "0720 FCP command x%x residual overrun error. "
509 "Data: x%x x%x \n", cmnd->cmnd[0],
510 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -0500511 host_status = DID_ERROR;
512
513 /*
514 * Check SLI validation that all the transfer was actually done
515 * (fcpi_parm should be zero). Apply check only to reads.
516 */
517 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
518 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -0400519 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
520 "0734 FCP Read Check Error Data: "
521 "x%x x%x x%x x%x\n",
522 be32_to_cpu(fcpcmd->fcpDl),
523 be32_to_cpu(fcprsp->rspResId),
524 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -0500525 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500526 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -0500527 }
528
529 out:
530 cmnd->result = ScsiResult(host_status, scsi_status);
531}
532
533static void
534lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
535 struct lpfc_iocbq *pIocbOut)
536{
537 struct lpfc_scsi_buf *lpfc_cmd =
538 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500539 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500540 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
541 struct lpfc_nodelist *pnode = rdata->pnode;
542 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500543 int result;
544 struct scsi_device *sdev, *tmp_sdev;
545 int depth = 0;
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) {
612 lpfc_release_scsi_buf(phba, lpfc_cmd);
613 return;
614 }
615
James Smart92d7f7b2007-06-17 19:56:38 -0500616
617 if (!result)
James Smart3de2a652007-08-02 11:09:59 -0400618 lpfc_rampup_queue_depth(vport, sdev);
James Smart92d7f7b2007-06-17 19:56:38 -0500619
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500620 if (!result && pnode != NULL &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500621 ((jiffies - pnode->last_ramp_up_time) >
622 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
623 ((jiffies - pnode->last_q_full_time) >
624 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smart3de2a652007-08-02 11:09:59 -0400625 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500626 shost_for_each_device(tmp_sdev, sdev->host) {
James Smart3de2a652007-08-02 11:09:59 -0400627 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500628 if (tmp_sdev->id != sdev->id)
629 continue;
630 if (tmp_sdev->ordered_tags)
631 scsi_adjust_queue_depth(tmp_sdev,
632 MSG_ORDERED_TAG,
633 tmp_sdev->queue_depth+1);
634 else
635 scsi_adjust_queue_depth(tmp_sdev,
636 MSG_SIMPLE_TAG,
637 tmp_sdev->queue_depth+1);
638
639 pnode->last_ramp_up_time = jiffies;
640 }
641 }
642 }
643
644 /*
645 * Check for queue full. If the lun is reporting queue full, then
646 * back off the lun queue depth to prevent target overloads.
647 */
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500648 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500649 pnode->last_q_full_time = jiffies;
650
651 shost_for_each_device(tmp_sdev, sdev->host) {
652 if (tmp_sdev->id != sdev->id)
653 continue;
654 depth = scsi_track_queue_full(tmp_sdev,
655 tmp_sdev->queue_depth - 1);
656 }
657 /*
James Smart2e0fef82007-06-17 19:56:36 -0500658 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500659 * Modify the returned error code to store
660 * the final depth value set by
661 * scsi_track_queue_full.
662 */
663 if (depth == -1)
664 depth = sdev->host->cmd_per_lun;
665
666 if (depth) {
James Smarte8b62012007-08-02 11:10:09 -0400667 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
668 "0711 detected queue full - lun queue "
669 "depth adjusted to %d.\n", depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500670 }
671 }
672
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400673 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500674}
675
676static void
James Smart2e0fef82007-06-17 19:56:36 -0500677lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
678 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -0500679{
James Smart2e0fef82007-06-17 19:56:36 -0500680 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500681 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
682 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
683 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
684 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
685 int datadir = scsi_cmnd->sc_data_direction;
686
687 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400688 /* clear task management bits */
689 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500690
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400691 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
692 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500693
694 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
695
696 if (scsi_cmnd->device->tagged_supported) {
697 switch (scsi_cmnd->tag) {
698 case HEAD_OF_QUEUE_TAG:
699 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
700 break;
701 case ORDERED_QUEUE_TAG:
702 fcp_cmnd->fcpCntl1 = ORDERED_Q;
703 break;
704 default:
705 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
706 break;
707 }
708 } else
709 fcp_cmnd->fcpCntl1 = 0;
710
711 /*
712 * There are three possibilities here - use scatter-gather segment, use
713 * the single mapping, or neither. Start the lpfc command prep by
714 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
715 * data bde entry.
716 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500717 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500718 if (datadir == DMA_TO_DEVICE) {
719 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
720 iocb_cmd->un.fcpi.fcpi_parm = 0;
721 iocb_cmd->ulpPU = 0;
722 fcp_cmnd->fcpCntl3 = WRITE_DATA;
723 phba->fc4OutputRequests++;
724 } else {
725 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
726 iocb_cmd->ulpPU = PARM_READ_CHECK;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500727 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500728 fcp_cmnd->fcpCntl3 = READ_DATA;
729 phba->fc4InputRequests++;
730 }
731 } else {
732 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
733 iocb_cmd->un.fcpi.fcpi_parm = 0;
734 iocb_cmd->ulpPU = 0;
735 fcp_cmnd->fcpCntl3 = 0;
736 phba->fc4ControlRequests++;
737 }
738
739 /*
740 * Finish initializing those IOCB fields that are independent
741 * of the scsi_cmnd request_buffer
742 */
743 piocbq->iocb.ulpContext = pnode->nlp_rpi;
744 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
745 piocbq->iocb.ulpFCP2Rcvy = 1;
746
747 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
748 piocbq->context1 = lpfc_cmd;
749 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
750 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500751 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -0500752}
753
754static int
James Smart2e0fef82007-06-17 19:56:36 -0500755lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -0500756 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -0400757 unsigned int lun,
dea31012005-04-17 16:05:31 -0500758 uint8_t task_mgmt_cmd)
759{
dea31012005-04-17 16:05:31 -0500760 struct lpfc_iocbq *piocbq;
761 IOCB_t *piocb;
762 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -0400763 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -0500764 struct lpfc_nodelist *ndlp = rdata->pnode;
765
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400766 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea31012005-04-17 16:05:31 -0500767 return 0;
768 }
769
dea31012005-04-17 16:05:31 -0500770 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -0500771 piocbq->vport = vport;
772
dea31012005-04-17 16:05:31 -0500773 piocb = &piocbq->iocb;
774
775 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart420b630d2006-07-06 15:50:16 -0400776 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500777 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
778
779 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
780
781 piocb->ulpContext = ndlp->nlp_rpi;
782 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
783 piocb->ulpFCP2Rcvy = 1;
784 }
785 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
786
787 /* ulpTimeout is only one byte */
788 if (lpfc_cmd->timeout > 0xff) {
789 /*
790 * Do not timeout the command at the firmware level.
791 * The driver will provide the timeout mechanism.
792 */
793 piocb->ulpTimeout = 0;
794 } else {
795 piocb->ulpTimeout = lpfc_cmd->timeout;
796 }
797
James Smart2e0fef82007-06-17 19:56:36 -0500798 return 1;
dea31012005-04-17 16:05:31 -0500799}
800
James Smart7054a602007-04-25 09:52:34 -0400801static void
802lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
803 struct lpfc_iocbq *cmdiocbq,
804 struct lpfc_iocbq *rspiocbq)
805{
806 struct lpfc_scsi_buf *lpfc_cmd =
807 (struct lpfc_scsi_buf *) cmdiocbq->context1;
808 if (lpfc_cmd)
809 lpfc_release_scsi_buf(phba, lpfc_cmd);
810 return;
811}
812
dea31012005-04-17 16:05:31 -0500813static int
James Smart2e0fef82007-06-17 19:56:36 -0500814lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -0400815 unsigned tgt_id, unsigned int lun,
816 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -0500817{
James Smart2e0fef82007-06-17 19:56:36 -0500818 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500819 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400820 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500821 int ret;
822
James Smartf5603512006-12-02 13:35:43 -0500823 if (!rdata->pnode)
824 return FAILED;
825
James Smart0b18ac42006-05-01 21:50:40 -0400826 lpfc_cmd->rdata = rdata;
James Smart2e0fef82007-06-17 19:56:36 -0500827 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -0400828 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -0500829 if (!ret)
830 return FAILED;
831
dea31012005-04-17 16:05:31 -0500832 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400833 iocbqrsp = lpfc_sli_get_iocbq(phba);
834
dea31012005-04-17 16:05:31 -0500835 if (!iocbqrsp)
836 return FAILED;
dea31012005-04-17 16:05:31 -0500837
James Smart0b18ac42006-05-01 21:50:40 -0400838 /* Issue Target Reset to TGT <num> */
James Smarte8b62012007-08-02 11:10:09 -0400839 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
840 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
841 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400842 ret = lpfc_sli_issue_iocb_wait(phba,
843 &phba->sli.ring[phba->sli.fcp_ring],
844 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500845 if (ret != IOCB_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -0400846 if (ret == IOCB_TIMEDOUT)
847 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
dea31012005-04-17 16:05:31 -0500848 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -0500849 } else {
850 ret = SUCCESS;
851 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
852 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
853 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
854 (lpfc_cmd->result & IOERR_DRVR_MASK))
855 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
856 }
857
James Bottomley604a3e32005-10-29 10:28:33 -0500858 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500859 return ret;
860}
861
dea31012005-04-17 16:05:31 -0500862const char *
863lpfc_info(struct Scsi_Host *host)
864{
James Smart2e0fef82007-06-17 19:56:36 -0500865 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
866 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500867 int len;
868 static char lpfcinfobuf[384];
869
870 memset(lpfcinfobuf,0,384);
871 if (phba && phba->pcidev){
872 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
873 len = strlen(lpfcinfobuf);
874 snprintf(lpfcinfobuf + len,
875 384-len,
876 " on PCI bus %02x device %02x irq %d",
877 phba->pcidev->bus->number,
878 phba->pcidev->devfn,
879 phba->pcidev->irq);
880 len = strlen(lpfcinfobuf);
881 if (phba->Port[0]) {
882 snprintf(lpfcinfobuf + len,
883 384-len,
884 " port %s",
885 phba->Port);
886 }
887 }
888 return lpfcinfobuf;
889}
890
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500891static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
892{
893 unsigned long poll_tmo_expires =
894 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
895
896 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
897 mod_timer(&phba->fcp_poll_timer,
898 poll_tmo_expires);
899}
900
901void lpfc_poll_start_timer(struct lpfc_hba * phba)
902{
903 lpfc_poll_rearm_timer(phba);
904}
905
906void lpfc_poll_timeout(unsigned long ptr)
907{
James Smart2e0fef82007-06-17 19:56:36 -0500908 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500909
910 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
911 lpfc_sli_poll_fcp_ring (phba);
912 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
913 lpfc_poll_rearm_timer(phba);
914 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500915}
916
dea31012005-04-17 16:05:31 -0500917static int
918lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
919{
James Smart2e0fef82007-06-17 19:56:36 -0500920 struct Scsi_Host *shost = cmnd->device->host;
921 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
922 struct lpfc_hba *phba = vport->phba;
923 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500924 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
925 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400926 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400927 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400928 int err;
dea31012005-04-17 16:05:31 -0500929
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400930 err = fc_remote_port_chkready(rport);
931 if (err) {
932 cmnd->result = err;
dea31012005-04-17 16:05:31 -0500933 goto out_fail_command;
934 }
935
936 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400937 * Catch race where our node has transitioned, but the
938 * transport is still transitioning.
dea31012005-04-17 16:05:31 -0500939 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400940 if (!ndlp) {
941 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
942 goto out_fail_command;
dea31012005-04-17 16:05:31 -0500943 }
James Smarted957682007-06-17 19:56:37 -0500944 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -0500945 if (lpfc_cmd == NULL) {
James Smart92d7f7b2007-06-17 19:56:38 -0500946 lpfc_adjust_queue_depth(phba);
947
James Smarte8b62012007-08-02 11:10:09 -0400948 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
949 "0707 driver's buffer pool is empty, "
950 "IO busied\n");
dea31012005-04-17 16:05:31 -0500951 goto out_host_busy;
952 }
953
954 /*
955 * Store the midlayer's command structure for the completion phase
956 * and complete the command initialization.
957 */
958 lpfc_cmd->pCmd = cmnd;
959 lpfc_cmd->rdata = rdata;
960 lpfc_cmd->timeout = 0;
961 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
962 cmnd->scsi_done = done;
963
964 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
965 if (err)
966 goto out_host_busy_free_buf;
967
James Smart2e0fef82007-06-17 19:56:36 -0500968 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -0500969
970 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -0500971 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea31012005-04-17 16:05:31 -0500972 if (err)
973 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500974
975 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
976 lpfc_sli_poll_fcp_ring(phba);
977 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
978 lpfc_poll_rearm_timer(phba);
979 }
980
dea31012005-04-17 16:05:31 -0500981 return 0;
982
983 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -0400984 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400985 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500986 out_host_busy:
987 return SCSI_MLQUEUE_HOST_BUSY;
988
989 out_fail_command:
990 done(cmnd);
991 return 0;
992}
993
James Smarta90f5682006-08-17 11:58:04 -0400994static void
995lpfc_block_error_handler(struct scsi_cmnd *cmnd)
996{
997 struct Scsi_Host *shost = cmnd->device->host;
998 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
999
1000 spin_lock_irq(shost->host_lock);
1001 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1002 spin_unlock_irq(shost->host_lock);
1003 msleep(1000);
1004 spin_lock_irq(shost->host_lock);
1005 }
1006 spin_unlock_irq(shost->host_lock);
1007 return;
1008}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001009
dea31012005-04-17 16:05:31 -05001010static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001011lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001012{
James Smart2e0fef82007-06-17 19:56:36 -05001013 struct Scsi_Host *shost = cmnd->device->host;
1014 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1015 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001016 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001017 struct lpfc_iocbq *iocb;
1018 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05001019 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05001020 IOCB_t *cmd, *icmd;
dea31012005-04-17 16:05:31 -05001021 unsigned int loop_count = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001022 int ret = SUCCESS;
1023
James Smarta90f5682006-08-17 11:58:04 -04001024 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001025 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1026 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05001027
1028 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001029 * If pCmd field of the corresponding lpfc_scsi_buf structure
1030 * points to a different SCSI command, then the driver has
1031 * already completed this command, but the midlayer did not
1032 * see the completion before the eh fired. Just return
1033 * SUCCESS.
dea31012005-04-17 16:05:31 -05001034 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001035 iocb = &lpfc_cmd->cur_iocbq;
1036 if (lpfc_cmd->pCmd != cmnd)
1037 goto out;
dea31012005-04-17 16:05:31 -05001038
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001039 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05001040
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001041 abtsiocb = lpfc_sli_get_iocbq(phba);
1042 if (abtsiocb == NULL) {
1043 ret = FAILED;
dea31012005-04-17 16:05:31 -05001044 goto out;
1045 }
1046
dea31012005-04-17 16:05:31 -05001047 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001048 * The scsi command can not be in txq and it is in flight because the
1049 * pCmd is still pointig at the SCSI command we have to abort. There
1050 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05001051 */
dea31012005-04-17 16:05:31 -05001052
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001053 cmd = &iocb->iocb;
1054 icmd = &abtsiocb->iocb;
1055 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1056 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1057 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05001058
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001059 icmd->ulpLe = 1;
1060 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05001061 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001062 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1063 else
1064 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05001065
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001066 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05001067 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001068 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1069 lpfc_sli_release_iocbq(phba, abtsiocb);
1070 ret = FAILED;
1071 goto out;
1072 }
1073
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001074 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1075 lpfc_sli_poll_fcp_ring (phba);
1076
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001077 /* Wait for abort to complete */
1078 while (lpfc_cmd->pCmd == cmnd)
1079 {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001080 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1081 lpfc_sli_poll_fcp_ring (phba);
1082
James Smart2e0fef82007-06-17 19:56:36 -05001083 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT * HZ);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001084 if (++loop_count
James Smart3de2a652007-08-02 11:09:59 -04001085 > (2 * vport->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
dea31012005-04-17 16:05:31 -05001086 break;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001087 }
dea31012005-04-17 16:05:31 -05001088
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001089 if (lpfc_cmd->pCmd == cmnd) {
1090 ret = FAILED;
James Smarte8b62012007-08-02 11:10:09 -04001091 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1092 "0748 abort handler timed out waiting "
1093 "for abort to complete: ret %#x, ID %d, "
1094 "LUN %d, snum %#lx\n",
1095 ret, cmnd->device->id, cmnd->device->lun,
1096 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001097 }
1098
1099 out:
James Smarte8b62012007-08-02 11:10:09 -04001100 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1101 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
1102 "LUN %d snum %#lx\n", ret, cmnd->device->id,
1103 cmnd->device->lun, cmnd->serial_number);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001104 return ret;
dea31012005-04-17 16:05:31 -05001105}
1106
1107static int
James Smart7054a602007-04-25 09:52:34 -04001108lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001109{
James Smart2e0fef82007-06-17 19:56:36 -05001110 struct Scsi_Host *shost = cmnd->device->host;
1111 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1112 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001113 struct lpfc_scsi_buf *lpfc_cmd;
1114 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001115 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1116 struct lpfc_nodelist *pnode = rdata->pnode;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001117 uint32_t cmd_result = 0, cmd_status = 0;
dea31012005-04-17 16:05:31 -05001118 int ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001119 int iocb_status = IOCB_SUCCESS;
dea31012005-04-17 16:05:31 -05001120 int cnt, loopcnt;
1121
James Smarta90f5682006-08-17 11:58:04 -04001122 lpfc_block_error_handler(cmnd);
James Smartf5603512006-12-02 13:35:43 -05001123 loopcnt = 0;
dea31012005-04-17 16:05:31 -05001124 /*
1125 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001126 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001127 */
James Smart92d7f7b2007-06-17 19:56:38 -05001128 while (1) {
dea31012005-04-17 16:05:31 -05001129 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001130 goto out;
dea31012005-04-17 16:05:31 -05001131
1132 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001133 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smartf5603512006-12-02 13:35:43 -05001134 loopcnt++;
1135 rdata = cmnd->device->hostdata;
1136 if (!rdata ||
James Smart3de2a652007-08-02 11:09:59 -04001137 (loopcnt > ((vport->cfg_devloss_tmo * 2) + 1))){
James Smarte8b62012007-08-02 11:10:09 -04001138 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1139 "0721 LUN Reset rport "
1140 "failure: cnt x%x rdata x%p\n",
1141 loopcnt, rdata);
James Smartf5603512006-12-02 13:35:43 -05001142 goto out;
1143 }
1144 pnode = rdata->pnode;
1145 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001146 goto out;
dea31012005-04-17 16:05:31 -05001147 }
James Smartf5603512006-12-02 13:35:43 -05001148 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001149 break;
1150 }
1151
James Smart2e0fef82007-06-17 19:56:36 -05001152 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001153 if (lpfc_cmd == NULL)
1154 goto out;
1155
dea31012005-04-17 16:05:31 -05001156 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001157 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001158
James Smart2e0fef82007-06-17 19:56:36 -05001159 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
James Smart7054a602007-04-25 09:52:34 -04001160 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05001161 if (!ret)
1162 goto out_free_scsi_buf;
1163
1164 iocbq = &lpfc_cmd->cur_iocbq;
1165
1166 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001167 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001168 if (iocbqrsp == NULL)
1169 goto out_free_scsi_buf;
1170
James Smarte8b62012007-08-02 11:10:09 -04001171 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1172 "0703 Issue target reset to TGT %d LUN %d "
1173 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1174 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
James Smart7054a602007-04-25 09:52:34 -04001175 iocb_status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001176 &phba->sli.ring[phba->sli.fcp_ring],
1177 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -05001178
James Smart7054a602007-04-25 09:52:34 -04001179 if (iocb_status == IOCB_TIMEDOUT)
1180 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
1181
1182 if (iocb_status == IOCB_SUCCESS)
1183 ret = SUCCESS;
1184 else
1185 ret = iocb_status;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001186
1187 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1188 cmd_status = iocbqrsp->iocb.ulpStatus;
1189
1190 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001191
1192 /*
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001193 * All outstanding txcmplq I/Os should have been aborted by the device.
dea31012005-04-17 16:05:31 -05001194 * Unfortunately, some targets do not abide by this forcing the driver
1195 * to double check.
1196 */
James Smart51ef4c22007-08-02 11:10:31 -04001197 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001198 LPFC_CTX_LUN);
1199 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04001200 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001201 cmnd->device->id, cmnd->device->lun,
James Smart51ef4c22007-08-02 11:10:31 -04001202 LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001203 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001204 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001205 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001206
1207 if (++loopcnt
James Smart3de2a652007-08-02 11:09:59 -04001208 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001209 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001210
James Smart51ef4c22007-08-02 11:10:31 -04001211 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
1212 cmnd->device->lun, LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001213 }
1214
1215 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001216 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1217 "0719 device reset I/O flush failure: "
1218 "cnt x%x\n", cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001219 ret = FAILED;
dea31012005-04-17 16:05:31 -05001220 }
1221
dea31012005-04-17 16:05:31 -05001222out_free_scsi_buf:
James Smart7054a602007-04-25 09:52:34 -04001223 if (iocb_status != IOCB_TIMEDOUT) {
1224 lpfc_release_scsi_buf(phba, lpfc_cmd);
1225 }
James Smarte8b62012007-08-02 11:10:09 -04001226 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1227 "0713 SCSI layer issued device reset (%d, %d) "
1228 "return x%x status x%x result x%x\n",
1229 cmnd->device->id, cmnd->device->lun, ret,
1230 cmd_status, cmd_result);
dea31012005-04-17 16:05:31 -05001231out:
1232 return ret;
1233}
1234
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001235static int
James Smart7054a602007-04-25 09:52:34 -04001236lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001237{
James Smart2e0fef82007-06-17 19:56:36 -05001238 struct Scsi_Host *shost = cmnd->device->host;
1239 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1240 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001241 struct lpfc_nodelist *ndlp = NULL;
1242 int match;
1243 int ret = FAILED, i, err_count = 0;
1244 int cnt, loopcnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001245 struct lpfc_scsi_buf * lpfc_cmd;
dea31012005-04-17 16:05:31 -05001246
James Smarta90f5682006-08-17 11:58:04 -04001247 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001248
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001249 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001250 if (lpfc_cmd == NULL)
1251 goto out;
1252
1253 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1254 lpfc_cmd->timeout = 60;
dea31012005-04-17 16:05:31 -05001255
1256 /*
1257 * Since the driver manages a single bus device, reset all
1258 * targets known to the driver. Should any target reset
1259 * fail, this routine returns failure to the midlayer.
1260 */
James Smarte17da182006-07-06 15:49:25 -04001261 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001262 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001263 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001264 spin_lock_irq(shost->host_lock);
1265 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart685f0bf2007-04-25 09:53:08 -04001266 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1267 i == ndlp->nlp_sid &&
1268 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001269 match = 1;
1270 break;
1271 }
1272 }
James Smart2e0fef82007-06-17 19:56:36 -05001273 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001274 if (!match)
1275 continue;
1276
James Smart2e0fef82007-06-17 19:56:36 -05001277 ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1278 cmnd->device->lun,
James Smart420b630d2006-07-06 15:50:16 -04001279 ndlp->rport->dd_data);
dea31012005-04-17 16:05:31 -05001280 if (ret != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04001281 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1282 "0700 Bus Reset on target %d failed\n",
1283 i);
dea31012005-04-17 16:05:31 -05001284 err_count++;
James Smart7054a602007-04-25 09:52:34 -04001285 break;
dea31012005-04-17 16:05:31 -05001286 }
1287 }
1288
James Smart7054a602007-04-25 09:52:34 -04001289 if (ret != IOCB_TIMEDOUT)
1290 lpfc_release_scsi_buf(phba, lpfc_cmd);
1291
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001292 if (err_count == 0)
1293 ret = SUCCESS;
James Smart7054a602007-04-25 09:52:34 -04001294 else
1295 ret = FAILED;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001296
1297 /*
1298 * All outstanding txcmplq I/Os should have been aborted by
1299 * the targets. Unfortunately, some targets do not abide by
1300 * this forcing the driver to double check.
1301 */
James Smart51ef4c22007-08-02 11:10:31 -04001302 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001303 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04001304 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1305 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001306 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001307 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001308 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001309
1310 if (++loopcnt
James Smart3de2a652007-08-02 11:09:59 -04001311 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001312 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001313
James Smart51ef4c22007-08-02 11:10:31 -04001314 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001315 }
1316
1317 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001318 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1319 "0715 Bus Reset I/O flush failure: "
1320 "cnt x%x left x%x\n", cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001321 ret = FAILED;
dea31012005-04-17 16:05:31 -05001322 }
1323
James Smarte8b62012007-08-02 11:10:09 -04001324 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1325 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05001326out:
1327 return ret;
1328}
1329
1330static int
dea31012005-04-17 16:05:31 -05001331lpfc_slave_alloc(struct scsi_device *sdev)
1332{
James Smart2e0fef82007-06-17 19:56:36 -05001333 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1334 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001335 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001336 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001337 uint32_t total = 0, i;
1338 uint32_t num_to_alloc = 0;
1339 unsigned long flags;
dea31012005-04-17 16:05:31 -05001340
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001341 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001342 return -ENXIO;
1343
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001344 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001345
1346 /*
1347 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1348 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001349 * HBA limit conveyed to the midlayer via the host structure. The
1350 * formula accounts for the lun_queue_depth + error handlers + 1
1351 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001352 */
1353 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04001354 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05001355
1356 /* Allow some exchanges to be available always to complete discovery */
1357 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001358 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1359 "0704 At limitation of %d preallocated "
1360 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05001361 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001362 /* Allow some exchanges to be available always to complete discovery */
1363 } else if (total + num_to_alloc >
1364 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001365 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1366 "0705 Allocation request of %d "
1367 "command buffers will exceed max of %d. "
1368 "Reducing allocation request to %d.\n",
1369 num_to_alloc, phba->cfg_hba_queue_depth,
1370 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001371 num_to_alloc = phba->cfg_hba_queue_depth - total;
1372 }
1373
1374 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05001375 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05001376 if (!scsi_buf) {
James Smarte8b62012007-08-02 11:10:09 -04001377 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1378 "0706 Failed to allocate "
1379 "command buffer\n");
dea31012005-04-17 16:05:31 -05001380 break;
1381 }
1382
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001383 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001384 phba->total_scsi_bufs++;
1385 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001386 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001387 }
1388 return 0;
1389}
1390
1391static int
1392lpfc_slave_configure(struct scsi_device *sdev)
1393{
James Smart2e0fef82007-06-17 19:56:36 -05001394 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1395 struct lpfc_hba *phba = vport->phba;
1396 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05001397
1398 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04001399 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001400 else
James Smart3de2a652007-08-02 11:09:59 -04001401 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001402
1403 /*
1404 * Initialize the fc transport attributes for the target
1405 * containing this scsi device. Also note that the driver's
1406 * target pointer is stored in the starget_data for the
1407 * driver's sysfs entry point functions.
1408 */
James Smart3de2a652007-08-02 11:09:59 -04001409 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05001410
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001411 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1412 lpfc_sli_poll_fcp_ring(phba);
1413 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1414 lpfc_poll_rearm_timer(phba);
1415 }
1416
dea31012005-04-17 16:05:31 -05001417 return 0;
1418}
1419
1420static void
1421lpfc_slave_destroy(struct scsi_device *sdev)
1422{
1423 sdev->hostdata = NULL;
1424 return;
1425}
1426
James Smart92d7f7b2007-06-17 19:56:38 -05001427
dea31012005-04-17 16:05:31 -05001428struct scsi_host_template lpfc_template = {
1429 .module = THIS_MODULE,
1430 .name = LPFC_DRIVER_NAME,
1431 .info = lpfc_info,
1432 .queuecommand = lpfc_queuecommand,
1433 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04001434 .eh_device_reset_handler= lpfc_device_reset_handler,
1435 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05001436 .slave_alloc = lpfc_slave_alloc,
1437 .slave_configure = lpfc_slave_configure,
1438 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04001439 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05001440 .this_id = -1,
1441 .sg_tablesize = LPFC_SG_SEG_CNT,
1442 .cmd_per_lun = LPFC_CMD_PER_LUN,
1443 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05001444 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001445 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001446};
James Smart3de2a652007-08-02 11:09:59 -04001447
1448struct scsi_host_template lpfc_vport_template = {
1449 .module = THIS_MODULE,
1450 .name = LPFC_DRIVER_NAME,
1451 .info = lpfc_info,
1452 .queuecommand = lpfc_queuecommand,
1453 .eh_abort_handler = lpfc_abort_handler,
1454 .eh_device_reset_handler= lpfc_device_reset_handler,
1455 .eh_bus_reset_handler = lpfc_bus_reset_handler,
1456 .slave_alloc = lpfc_slave_alloc,
1457 .slave_configure = lpfc_slave_configure,
1458 .slave_destroy = lpfc_slave_destroy,
1459 .scan_finished = lpfc_scan_finished,
1460 .this_id = -1,
1461 .sg_tablesize = LPFC_SG_SEG_CNT,
1462 .cmd_per_lun = LPFC_CMD_PER_LUN,
1463 .use_clustering = ENABLE_CLUSTERING,
1464 .shost_attrs = lpfc_vport_attrs,
1465 .max_sectors = 0xFFFF,
1466};