blob: 1e88b7a8a4510f9a97d24c2d2faf472b0c8ad57d [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;
James Smart5e9d9b82008-06-14 22:52:53 -040053 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -050054
55 spin_lock_irqsave(&phba->hbalock, flags);
56 atomic_inc(&phba->num_rsrc_err);
57 phba->last_rsrc_error_time = jiffies;
58
59 if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
60 spin_unlock_irqrestore(&phba->hbalock, flags);
61 return;
62 }
63
64 phba->last_ramp_down_time = jiffies;
65
66 spin_unlock_irqrestore(&phba->hbalock, flags);
67
68 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -040069 evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
70 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -050071 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -050072 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
73
James Smart5e9d9b82008-06-14 22:52:53 -040074 if (!evt_posted)
75 lpfc_worker_wake_up(phba);
James Smart92d7f7b2007-06-17 19:56:38 -050076 return;
77}
78
79/*
80 * This function is called with no lock held when there is a successful
81 * SCSI command completion.
82 */
83static inline void
James Smart3de2a652007-08-02 11:09:59 -040084lpfc_rampup_queue_depth(struct lpfc_vport *vport,
James Smart92d7f7b2007-06-17 19:56:38 -050085 struct scsi_device *sdev)
86{
87 unsigned long flags;
James Smart3de2a652007-08-02 11:09:59 -040088 struct lpfc_hba *phba = vport->phba;
James Smart5e9d9b82008-06-14 22:52:53 -040089 uint32_t evt_posted;
James Smart92d7f7b2007-06-17 19:56:38 -050090 atomic_inc(&phba->num_cmd_success);
91
James Smart3de2a652007-08-02 11:09:59 -040092 if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
James Smart92d7f7b2007-06-17 19:56:38 -050093 return;
James Smart92d7f7b2007-06-17 19:56:38 -050094 spin_lock_irqsave(&phba->hbalock, flags);
95 if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
96 ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
97 spin_unlock_irqrestore(&phba->hbalock, flags);
98 return;
99 }
James Smart92d7f7b2007-06-17 19:56:38 -0500100 phba->last_ramp_up_time = jiffies;
101 spin_unlock_irqrestore(&phba->hbalock, flags);
102
103 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
James Smart5e9d9b82008-06-14 22:52:53 -0400104 evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
105 if (!evt_posted)
James Smart92d7f7b2007-06-17 19:56:38 -0500106 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
James Smart92d7f7b2007-06-17 19:56:38 -0500107 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
108
James Smart5e9d9b82008-06-14 22:52:53 -0400109 if (!evt_posted)
110 lpfc_worker_wake_up(phba);
111 return;
James Smart92d7f7b2007-06-17 19:56:38 -0500112}
113
114void
115lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
116{
James Smart549e55c2007-08-02 11:09:51 -0400117 struct lpfc_vport **vports;
118 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500119 struct scsi_device *sdev;
120 unsigned long new_queue_depth;
121 unsigned long num_rsrc_err, num_cmd_success;
James Smart549e55c2007-08-02 11:09:51 -0400122 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500123
124 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
125 num_cmd_success = atomic_read(&phba->num_cmd_success);
126
James Smart549e55c2007-08-02 11:09:51 -0400127 vports = lpfc_create_vport_work_array(phba);
128 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500129 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400130 shost = lpfc_shost_from_vport(vports[i]);
131 shost_for_each_device(sdev, shost) {
James Smart92d7f7b2007-06-17 19:56:38 -0500132 new_queue_depth =
James Smart549e55c2007-08-02 11:09:51 -0400133 sdev->queue_depth * num_rsrc_err /
134 (num_rsrc_err + num_cmd_success);
135 if (!new_queue_depth)
136 new_queue_depth = sdev->queue_depth - 1;
137 else
138 new_queue_depth = sdev->queue_depth -
139 new_queue_depth;
140 if (sdev->ordered_tags)
141 scsi_adjust_queue_depth(sdev,
142 MSG_ORDERED_TAG,
143 new_queue_depth);
144 else
145 scsi_adjust_queue_depth(sdev,
146 MSG_SIMPLE_TAG,
147 new_queue_depth);
148 }
James Smart92d7f7b2007-06-17 19:56:38 -0500149 }
James Smart09372822008-01-11 01:52:54 -0500150 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500151 atomic_set(&phba->num_rsrc_err, 0);
152 atomic_set(&phba->num_cmd_success, 0);
153}
154
155void
156lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
157{
James Smart549e55c2007-08-02 11:09:51 -0400158 struct lpfc_vport **vports;
159 struct Scsi_Host *shost;
James Smart92d7f7b2007-06-17 19:56:38 -0500160 struct scsi_device *sdev;
James Smart549e55c2007-08-02 11:09:51 -0400161 int i;
James Smart92d7f7b2007-06-17 19:56:38 -0500162
James Smart549e55c2007-08-02 11:09:51 -0400163 vports = lpfc_create_vport_work_array(phba);
164 if (vports != NULL)
James Smart09372822008-01-11 01:52:54 -0500165 for(i = 0; i <= phba->max_vpi && vports[i] != NULL; i++) {
James Smart549e55c2007-08-02 11:09:51 -0400166 shost = lpfc_shost_from_vport(vports[i]);
167 shost_for_each_device(sdev, shost) {
James Smart97eab632008-04-07 10:16:05 -0400168 if (vports[i]->cfg_lun_queue_depth <=
169 sdev->queue_depth)
170 continue;
James Smart549e55c2007-08-02 11:09:51 -0400171 if (sdev->ordered_tags)
172 scsi_adjust_queue_depth(sdev,
173 MSG_ORDERED_TAG,
174 sdev->queue_depth+1);
175 else
176 scsi_adjust_queue_depth(sdev,
177 MSG_SIMPLE_TAG,
178 sdev->queue_depth+1);
179 }
James Smart92d7f7b2007-06-17 19:56:38 -0500180 }
James Smart09372822008-01-11 01:52:54 -0500181 lpfc_destroy_vport_work_array(phba, vports);
James Smart92d7f7b2007-06-17 19:56:38 -0500182 atomic_set(&phba->num_rsrc_err, 0);
183 atomic_set(&phba->num_cmd_success, 0);
184}
185
186/*
dea31012005-04-17 16:05:31 -0500187 * This routine allocates a scsi buffer, which contains all the necessary
188 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
189 * contains information to build the IOCB. The DMAable region contains
190 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
191 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
192 * and the BPL BDE is setup in the IOCB.
193 */
194static struct lpfc_scsi_buf *
James Smart2e0fef82007-06-17 19:56:36 -0500195lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500196{
James Smart2e0fef82007-06-17 19:56:36 -0500197 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500198 struct lpfc_scsi_buf *psb;
199 struct ulp_bde64 *bpl;
200 IOCB_t *iocb;
201 dma_addr_t pdma_phys;
James Bottomley604a3e32005-10-29 10:28:33 -0500202 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500203
Mariusz Kozlowskibbfbbbc2007-08-11 10:13:24 +0200204 psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
dea31012005-04-17 16:05:31 -0500205 if (!psb)
206 return NULL;
dea31012005-04-17 16:05:31 -0500207
208 /*
209 * Get memory from the pci pool to map the virt space to pci bus space
210 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
211 * struct fcp_rsp and the number of bde's necessary to support the
212 * sg_tablesize.
213 */
214 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
215 &psb->dma_handle);
216 if (!psb->data) {
217 kfree(psb);
218 return NULL;
219 }
220
221 /* Initialize virtual ptrs to dma_buf region. */
222 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
223
James Bottomley604a3e32005-10-29 10:28:33 -0500224 /* Allocate iotag for psb->cur_iocbq. */
225 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
226 if (iotag == 0) {
227 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
228 psb->data, psb->dma_handle);
229 kfree (psb);
230 return NULL;
231 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400232 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500233
dea31012005-04-17 16:05:31 -0500234 psb->fcp_cmnd = psb->data;
235 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
236 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
237 sizeof(struct fcp_rsp);
238
239 /* Initialize local short-hand pointers. */
240 bpl = psb->fcp_bpl;
241 pdma_phys = psb->dma_handle;
242
243 /*
244 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
245 * list bdes. Initialize the first two and leave the rest for
246 * queuecommand.
247 */
248 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
249 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
250 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
251 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
252 bpl->tus.w = le32_to_cpu(bpl->tus.w);
253 bpl++;
254
255 /* Setup the physical region for the FCP RSP */
256 pdma_phys += sizeof (struct fcp_cmnd);
257 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
258 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
259 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
260 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
261 bpl->tus.w = le32_to_cpu(bpl->tus.w);
262
263 /*
264 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
265 * initialize it with all known data now.
266 */
267 pdma_phys += (sizeof (struct fcp_rsp));
268 iocb = &psb->cur_iocbq.iocb;
269 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
270 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
271 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
272 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
273 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
274 iocb->ulpBdeCount = 1;
275 iocb->ulpClass = CLASS3;
276
277 return psb;
278}
279
Adrian Bunk455c53e2006-01-06 20:21:28 +0100280static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500281lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500282{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400283 struct lpfc_scsi_buf * lpfc_cmd = NULL;
284 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500285 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500286
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500287 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400288 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400289 if (lpfc_cmd) {
290 lpfc_cmd->seg_cnt = 0;
291 lpfc_cmd->nonsg_phys = 0;
292 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500293 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400294 return lpfc_cmd;
295}
296
297static void
James Smart92d7f7b2007-06-17 19:56:38 -0500298lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400299{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500300 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500301
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500302 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400303 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500304 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500305 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500306}
307
308static int
James Smart92d7f7b2007-06-17 19:56:38 -0500309lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500310{
311 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
312 struct scatterlist *sgel = NULL;
313 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
314 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
315 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
316 dma_addr_t physaddr;
317 uint32_t i, num_bde = 0;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500318 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea31012005-04-17 16:05:31 -0500319
320 /*
321 * There are three possibilities here - use scatter-gather segment, use
322 * the single mapping, or neither. Start the lpfc command prep by
323 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
324 * data bde entry.
325 */
326 bpl += 2;
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700327 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500328 /*
329 * The driver stores the segment count returned from pci_map_sg
330 * because this a count of dma-mappings used to map the use_sg
331 * pages. They are not guaranteed to be the same for those
332 * architectures that implement an IOMMU.
333 */
dea31012005-04-17 16:05:31 -0500334
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700335 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
336 scsi_sg_count(scsi_cmnd), datadir);
337 if (unlikely(!nseg))
338 return 1;
339
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500340 lpfc_cmd->seg_cnt = nseg;
dea31012005-04-17 16:05:31 -0500341 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
342 printk(KERN_ERR "%s: Too many sg segments from "
343 "dma_map_sg. Config %d, seg_cnt %d",
344 __FUNCTION__, phba->cfg_sg_seg_cnt,
345 lpfc_cmd->seg_cnt);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500346 scsi_dma_unmap(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500347 return 1;
348 }
349
350 /*
351 * The driver established a maximum scatter-gather segment count
352 * during probe that limits the number of sg elements in any
353 * single scsi command. Just run through the seg_cnt and format
354 * the bde's.
355 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500356 scsi_for_each_sg(scsi_cmnd, sgel, nseg, i) {
dea31012005-04-17 16:05:31 -0500357 physaddr = sg_dma_address(sgel);
358 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
359 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
360 bpl->tus.f.bdeSize = sg_dma_len(sgel);
361 if (datadir == DMA_TO_DEVICE)
362 bpl->tus.f.bdeFlags = 0;
363 else
364 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
365 bpl->tus.w = le32_to_cpu(bpl->tus.w);
366 bpl++;
dea31012005-04-17 16:05:31 -0500367 num_bde++;
368 }
FUJITA Tomonoric59fd9e2007-07-04 06:03:11 -0700369 }
dea31012005-04-17 16:05:31 -0500370
371 /*
372 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400373 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
374 * reinitialized since all iocb memory resources are used many times
375 * for transmit, receive, and continuation bpl's.
dea31012005-04-17 16:05:31 -0500376 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400377 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500378 iocb_cmd->un.fcpi64.bdl.bdeSize +=
379 (num_bde * sizeof (struct ulp_bde64));
380 iocb_cmd->ulpBdeCount = 1;
381 iocb_cmd->ulpLe = 1;
James Smart09372822008-01-11 01:52:54 -0500382 fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
dea31012005-04-17 16:05:31 -0500383 return 0;
384}
385
386static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400387lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
388{
389 /*
390 * There are only two special cases to consider. (1) the scsi command
391 * requested scatter-gather usage or (2) the scsi command allocated
392 * a request buffer, but did not request use_sg. There is a third
393 * case, but it does not require resource deallocation.
394 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500395 if (psb->seg_cnt > 0)
396 scsi_dma_unmap(psb->pCmd);
James Smartbcf4dbf2006-07-06 15:50:08 -0400397}
398
399static void
James Smart2e0fef82007-06-17 19:56:36 -0500400lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
401 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500402{
403 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
404 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
405 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart7054a602007-04-25 09:52:34 -0400406 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea31012005-04-17 16:05:31 -0500407 uint32_t resp_info = fcprsp->rspStatus2;
408 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500409 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500410 uint32_t host_status = DID_OK;
411 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500412 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500413
414 /*
415 * If this is a task management command, there is no
416 * scsi packet associated with this lpfc_cmd. The driver
417 * consumes it.
418 */
419 if (fcpcmd->fcpCntl2) {
420 scsi_status = 0;
421 goto out;
422 }
423
James Smartc7743952006-12-02 13:34:42 -0500424 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
425 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
426 if (snslen > SCSI_SENSE_BUFFERSIZE)
427 snslen = SCSI_SENSE_BUFFERSIZE;
428
429 if (resp_info & RSP_LEN_VALID)
430 rsplen = be32_to_cpu(fcprsp->rspRspLen);
431 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
432 }
433 lp = (uint32_t *)cmnd->sense_buffer;
434
435 if (!scsi_status && (resp_info & RESID_UNDER))
436 logit = LOG_FCP;
437
James Smarte8b62012007-08-02 11:10:09 -0400438 lpfc_printf_vlog(vport, KERN_WARNING, logit,
439 "0730 FCP command x%x failed: x%x SNS x%x x%x "
440 "Data: x%x x%x x%x x%x x%x\n",
441 cmnd->cmnd[0], scsi_status,
442 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
443 be32_to_cpu(fcprsp->rspResId),
444 be32_to_cpu(fcprsp->rspSnsLen),
445 be32_to_cpu(fcprsp->rspRspLen),
446 fcprsp->rspInfo3);
dea31012005-04-17 16:05:31 -0500447
448 if (resp_info & RSP_LEN_VALID) {
449 rsplen = be32_to_cpu(fcprsp->rspRspLen);
450 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
451 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
452 host_status = DID_ERROR;
453 goto out;
454 }
455 }
456
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500457 scsi_set_resid(cmnd, 0);
dea31012005-04-17 16:05:31 -0500458 if (resp_info & RESID_UNDER) {
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500459 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea31012005-04-17 16:05:31 -0500460
James Smarte8b62012007-08-02 11:10:09 -0400461 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
462 "0716 FCP Read Underrun, expected %d, "
463 "residual %d Data: x%x x%x x%x\n",
464 be32_to_cpu(fcpcmd->fcpDl),
465 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
466 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500467
468 /*
James Smart7054a602007-04-25 09:52:34 -0400469 * If there is an under run check if under run reported by
470 * storage array is same as the under run reported by HBA.
471 * If this is not same, there is a dropped frame.
472 */
473 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
474 fcpi_parm &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500475 (scsi_get_resid(cmnd) != fcpi_parm)) {
James Smarte8b62012007-08-02 11:10:09 -0400476 lpfc_printf_vlog(vport, KERN_WARNING,
477 LOG_FCP | LOG_FCP_ERROR,
478 "0735 FCP Read Check Error "
479 "and Underrun Data: x%x x%x x%x x%x\n",
480 be32_to_cpu(fcpcmd->fcpDl),
481 scsi_get_resid(cmnd), fcpi_parm,
482 cmnd->cmnd[0]);
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500483 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
James Smart7054a602007-04-25 09:52:34 -0400484 host_status = DID_ERROR;
485 }
486 /*
dea31012005-04-17 16:05:31 -0500487 * The cmnd->underflow is the minimum number of bytes that must
488 * be transfered for this command. Provided a sense condition
489 * is not present, make sure the actual amount transferred is at
490 * least the underflow value or fail.
491 */
492 if (!(resp_info & SNS_LEN_VALID) &&
493 (scsi_status == SAM_STAT_GOOD) &&
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500494 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
495 < cmnd->underflow)) {
James Smarte8b62012007-08-02 11:10:09 -0400496 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
497 "0717 FCP command x%x residual "
498 "underrun converted to error "
499 "Data: x%x x%x x%x\n",
James Smart66dbfbe2007-08-05 06:08:38 -0400500 cmnd->cmnd[0], scsi_bufflen(cmnd),
James Smarte8b62012007-08-02 11:10:09 -0400501 scsi_get_resid(cmnd), cmnd->underflow);
dea31012005-04-17 16:05:31 -0500502 host_status = DID_ERROR;
503 }
504 } else if (resp_info & RESID_OVER) {
James Smarte8b62012007-08-02 11:10:09 -0400505 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
506 "0720 FCP command x%x residual overrun error. "
507 "Data: x%x x%x \n", cmnd->cmnd[0],
508 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea31012005-04-17 16:05:31 -0500509 host_status = DID_ERROR;
510
511 /*
512 * Check SLI validation that all the transfer was actually done
513 * (fcpi_parm should be zero). Apply check only to reads.
514 */
515 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
516 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smarte8b62012007-08-02 11:10:09 -0400517 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
518 "0734 FCP Read Check Error Data: "
519 "x%x x%x x%x x%x\n",
520 be32_to_cpu(fcpcmd->fcpDl),
521 be32_to_cpu(fcprsp->rspResId),
522 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -0500523 host_status = DID_ERROR;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500524 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea31012005-04-17 16:05:31 -0500525 }
526
527 out:
528 cmnd->result = ScsiResult(host_status, scsi_status);
529}
530
531static void
532lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
533 struct lpfc_iocbq *pIocbOut)
534{
535 struct lpfc_scsi_buf *lpfc_cmd =
536 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500537 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500538 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
539 struct lpfc_nodelist *pnode = rdata->pnode;
540 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500541 int result;
542 struct scsi_device *sdev, *tmp_sdev;
543 int depth = 0;
James Smartfa61a542008-01-11 01:52:42 -0500544 unsigned long flags;
dea31012005-04-17 16:05:31 -0500545
546 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
547 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
548
549 if (lpfc_cmd->status) {
550 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
551 (lpfc_cmd->result & IOERR_DRVR_MASK))
552 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
553 else if (lpfc_cmd->status >= IOSTAT_CNT)
554 lpfc_cmd->status = IOSTAT_DEFAULT;
555
James Smarte8b62012007-08-02 11:10:09 -0400556 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
557 "0729 FCP cmd x%x failed <%d/%d> "
558 "status: x%x result: x%x Data: x%x x%x\n",
559 cmd->cmnd[0],
560 cmd->device ? cmd->device->id : 0xffff,
561 cmd->device ? cmd->device->lun : 0xffff,
562 lpfc_cmd->status, lpfc_cmd->result,
563 pIocbOut->iocb.ulpContext,
564 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea31012005-04-17 16:05:31 -0500565
566 switch (lpfc_cmd->status) {
567 case IOSTAT_FCP_RSP_ERROR:
568 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500569 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500570 break;
571 case IOSTAT_NPORT_BSY:
572 case IOSTAT_FABRIC_BSY:
573 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
574 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500575 case IOSTAT_LOCAL_REJECT:
576 if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
577 lpfc_cmd->result == IOERR_NO_RESOURCES ||
578 lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
579 cmd->result = ScsiResult(DID_REQUEUE, 0);
James Smart58da1ff2008-04-07 10:15:56 -0400580 break;
581 } /* else: fall through */
dea31012005-04-17 16:05:31 -0500582 default:
583 cmd->result = ScsiResult(DID_ERROR, 0);
584 break;
585 }
586
James Smart58da1ff2008-04-07 10:15:56 -0400587 if (!pnode || !NLP_CHK_NODE_ACT(pnode)
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400588 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
589 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500590 } else {
591 cmd->result = ScsiResult(DID_OK, 0);
592 }
593
594 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
595 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
596
James Smarte8b62012007-08-02 11:10:09 -0400597 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
598 "0710 Iodone <%d/%d> cmd %p, error "
599 "x%x SNS x%x x%x Data: x%x x%x\n",
600 cmd->device->id, cmd->device->lun, cmd,
601 cmd->result, *lp, *(lp + 3), cmd->retries,
602 scsi_get_resid(cmd));
dea31012005-04-17 16:05:31 -0500603 }
604
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500605 result = cmd->result;
606 sdev = cmd->device;
James Smart1dcb58e2007-04-25 09:51:30 -0400607 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James Smart97eab632008-04-07 10:16:05 -0400608 spin_lock_irqsave(sdev->host->host_lock, flags);
609 lpfc_cmd->pCmd = NULL; /* This must be done before scsi_done */
610 spin_unlock_irqrestore(sdev->host->host_lock, flags);
dea31012005-04-17 16:05:31 -0500611 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400612
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500613 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
James Smartfa61a542008-01-11 01:52:42 -0500614 /*
615 * If there is a thread waiting for command completion
616 * wake up the thread.
617 */
618 spin_lock_irqsave(sdev->host->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -0500619 if (lpfc_cmd->waitq)
620 wake_up(lpfc_cmd->waitq);
621 spin_unlock_irqrestore(sdev->host->host_lock, flags);
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500622 lpfc_release_scsi_buf(phba, lpfc_cmd);
623 return;
624 }
625
James Smart92d7f7b2007-06-17 19:56:38 -0500626
627 if (!result)
James Smart3de2a652007-08-02 11:09:59 -0400628 lpfc_rampup_queue_depth(vport, sdev);
James Smart92d7f7b2007-06-17 19:56:38 -0500629
James Smart58da1ff2008-04-07 10:15:56 -0400630 if (!result && pnode && NLP_CHK_NODE_ACT(pnode) &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500631 ((jiffies - pnode->last_ramp_up_time) >
632 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
633 ((jiffies - pnode->last_q_full_time) >
634 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
James Smart3de2a652007-08-02 11:09:59 -0400635 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500636 shost_for_each_device(tmp_sdev, sdev->host) {
James Smart3de2a652007-08-02 11:09:59 -0400637 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500638 if (tmp_sdev->id != sdev->id)
639 continue;
640 if (tmp_sdev->ordered_tags)
641 scsi_adjust_queue_depth(tmp_sdev,
642 MSG_ORDERED_TAG,
643 tmp_sdev->queue_depth+1);
644 else
645 scsi_adjust_queue_depth(tmp_sdev,
646 MSG_SIMPLE_TAG,
647 tmp_sdev->queue_depth+1);
648
649 pnode->last_ramp_up_time = jiffies;
650 }
651 }
652 }
653
654 /*
655 * Check for queue full. If the lun is reporting queue full, then
656 * back off the lun queue depth to prevent target overloads.
657 */
James Smart58da1ff2008-04-07 10:15:56 -0400658 if (result == SAM_STAT_TASK_SET_FULL && pnode &&
659 NLP_CHK_NODE_ACT(pnode)) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500660 pnode->last_q_full_time = jiffies;
661
662 shost_for_each_device(tmp_sdev, sdev->host) {
663 if (tmp_sdev->id != sdev->id)
664 continue;
665 depth = scsi_track_queue_full(tmp_sdev,
666 tmp_sdev->queue_depth - 1);
667 }
668 /*
James Smart2e0fef82007-06-17 19:56:36 -0500669 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500670 * Modify the returned error code to store
671 * the final depth value set by
672 * scsi_track_queue_full.
673 */
674 if (depth == -1)
675 depth = sdev->host->cmd_per_lun;
676
677 if (depth) {
James Smarte8b62012007-08-02 11:10:09 -0400678 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
679 "0711 detected queue full - lun queue "
680 "depth adjusted to %d.\n", depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500681 }
682 }
683
James Smartfa61a542008-01-11 01:52:42 -0500684 /*
685 * If there is a thread waiting for command completion
686 * wake up the thread.
687 */
688 spin_lock_irqsave(sdev->host->host_lock, flags);
James Smartfa61a542008-01-11 01:52:42 -0500689 if (lpfc_cmd->waitq)
690 wake_up(lpfc_cmd->waitq);
691 spin_unlock_irqrestore(sdev->host->host_lock, flags);
692
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400693 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500694}
695
696static void
James Smart2e0fef82007-06-17 19:56:36 -0500697lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
698 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -0500699{
James Smart2e0fef82007-06-17 19:56:36 -0500700 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500701 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
702 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
703 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
704 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
705 int datadir = scsi_cmnd->sc_data_direction;
James Smart7e2b19f2007-10-29 11:00:39 -0400706 char tag[2];
dea31012005-04-17 16:05:31 -0500707
James Smart58da1ff2008-04-07 10:15:56 -0400708 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
709 return;
710
dea31012005-04-17 16:05:31 -0500711 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400712 /* clear task management bits */
713 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500714
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400715 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
716 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500717
718 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
719
James Smart7e2b19f2007-10-29 11:00:39 -0400720 if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
721 switch (tag[0]) {
dea31012005-04-17 16:05:31 -0500722 case HEAD_OF_QUEUE_TAG:
723 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
724 break;
725 case ORDERED_QUEUE_TAG:
726 fcp_cmnd->fcpCntl1 = ORDERED_Q;
727 break;
728 default:
729 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
730 break;
731 }
732 } else
733 fcp_cmnd->fcpCntl1 = 0;
734
735 /*
736 * There are three possibilities here - use scatter-gather segment, use
737 * the single mapping, or neither. Start the lpfc command prep by
738 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
739 * data bde entry.
740 */
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500741 if (scsi_sg_count(scsi_cmnd)) {
dea31012005-04-17 16:05:31 -0500742 if (datadir == DMA_TO_DEVICE) {
743 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
744 iocb_cmd->un.fcpi.fcpi_parm = 0;
745 iocb_cmd->ulpPU = 0;
746 fcp_cmnd->fcpCntl3 = WRITE_DATA;
747 phba->fc4OutputRequests++;
748 } else {
749 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
750 iocb_cmd->ulpPU = PARM_READ_CHECK;
FUJITA Tomonoria0b4f782007-06-17 19:56:39 -0500751 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea31012005-04-17 16:05:31 -0500752 fcp_cmnd->fcpCntl3 = READ_DATA;
753 phba->fc4InputRequests++;
754 }
755 } else {
756 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
757 iocb_cmd->un.fcpi.fcpi_parm = 0;
758 iocb_cmd->ulpPU = 0;
759 fcp_cmnd->fcpCntl3 = 0;
760 phba->fc4ControlRequests++;
761 }
762
763 /*
764 * Finish initializing those IOCB fields that are independent
765 * of the scsi_cmnd request_buffer
766 */
767 piocbq->iocb.ulpContext = pnode->nlp_rpi;
768 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
769 piocbq->iocb.ulpFCP2Rcvy = 1;
James Smart09372822008-01-11 01:52:54 -0500770 else
771 piocbq->iocb.ulpFCP2Rcvy = 0;
dea31012005-04-17 16:05:31 -0500772
773 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
774 piocbq->context1 = lpfc_cmd;
775 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
776 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500777 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -0500778}
779
780static int
James Smart2e0fef82007-06-17 19:56:36 -0500781lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -0500782 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -0400783 unsigned int lun,
dea31012005-04-17 16:05:31 -0500784 uint8_t task_mgmt_cmd)
785{
dea31012005-04-17 16:05:31 -0500786 struct lpfc_iocbq *piocbq;
787 IOCB_t *piocb;
788 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -0400789 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -0500790 struct lpfc_nodelist *ndlp = rdata->pnode;
791
James Smart58da1ff2008-04-07 10:15:56 -0400792 if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
793 ndlp->nlp_state != NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -0500794 return 0;
dea31012005-04-17 16:05:31 -0500795
dea31012005-04-17 16:05:31 -0500796 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -0500797 piocbq->vport = vport;
798
dea31012005-04-17 16:05:31 -0500799 piocb = &piocbq->iocb;
800
801 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart420b630d2006-07-06 15:50:16 -0400802 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500803 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
804
805 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
806
807 piocb->ulpContext = ndlp->nlp_rpi;
808 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
809 piocb->ulpFCP2Rcvy = 1;
810 }
811 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
812
813 /* ulpTimeout is only one byte */
814 if (lpfc_cmd->timeout > 0xff) {
815 /*
816 * Do not timeout the command at the firmware level.
817 * The driver will provide the timeout mechanism.
818 */
819 piocb->ulpTimeout = 0;
820 } else {
821 piocb->ulpTimeout = lpfc_cmd->timeout;
822 }
823
James Smart2e0fef82007-06-17 19:56:36 -0500824 return 1;
dea31012005-04-17 16:05:31 -0500825}
826
James Smart7054a602007-04-25 09:52:34 -0400827static void
828lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
829 struct lpfc_iocbq *cmdiocbq,
830 struct lpfc_iocbq *rspiocbq)
831{
832 struct lpfc_scsi_buf *lpfc_cmd =
833 (struct lpfc_scsi_buf *) cmdiocbq->context1;
834 if (lpfc_cmd)
835 lpfc_release_scsi_buf(phba, lpfc_cmd);
836 return;
837}
838
dea31012005-04-17 16:05:31 -0500839static int
James Smart2e0fef82007-06-17 19:56:36 -0500840lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -0400841 unsigned tgt_id, unsigned int lun,
842 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -0500843{
James Smart2e0fef82007-06-17 19:56:36 -0500844 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500845 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400846 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500847 int ret;
James Smart915caaa2008-06-14 22:52:38 -0400848 int status;
dea31012005-04-17 16:05:31 -0500849
James Smart58da1ff2008-04-07 10:15:56 -0400850 if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
James Smartf5603512006-12-02 13:35:43 -0500851 return FAILED;
852
James Smart0b18ac42006-05-01 21:50:40 -0400853 lpfc_cmd->rdata = rdata;
James Smart915caaa2008-06-14 22:52:38 -0400854 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -0400855 FCP_TARGET_RESET);
James Smart915caaa2008-06-14 22:52:38 -0400856 if (!status)
dea31012005-04-17 16:05:31 -0500857 return FAILED;
858
dea31012005-04-17 16:05:31 -0500859 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400860 iocbqrsp = lpfc_sli_get_iocbq(phba);
861
dea31012005-04-17 16:05:31 -0500862 if (!iocbqrsp)
863 return FAILED;
dea31012005-04-17 16:05:31 -0500864
James Smart0b18ac42006-05-01 21:50:40 -0400865 /* Issue Target Reset to TGT <num> */
James Smarte8b62012007-08-02 11:10:09 -0400866 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
867 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
868 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James Smart915caaa2008-06-14 22:52:38 -0400869 status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400870 &phba->sli.ring[phba->sli.fcp_ring],
871 iocbq, iocbqrsp, lpfc_cmd->timeout);
James Smart915caaa2008-06-14 22:52:38 -0400872 if (status != IOCB_SUCCESS) {
873 if (status == IOCB_TIMEDOUT) {
James Smart7054a602007-04-25 09:52:34 -0400874 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smart915caaa2008-06-14 22:52:38 -0400875 ret = TIMEOUT_ERROR;
876 } else
877 ret = FAILED;
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 Smart915caaa2008-06-14 22:52:38 -04001145 unsigned long later;
1146 int ret = SUCCESS;
1147 int status;
1148 int cnt;
dea31012005-04-17 16:05:31 -05001149
James Smarta90f5682006-08-17 11:58:04 -04001150 lpfc_block_error_handler(cmnd);
dea31012005-04-17 16:05:31 -05001151 /*
1152 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001153 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001154 */
James Smart915caaa2008-06-14 22:52:38 -04001155 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1156 while (time_after(later, jiffies)) {
James Smart58da1ff2008-04-07 10:15:56 -04001157 if (!pnode || !NLP_CHK_NODE_ACT(pnode))
James Smart915caaa2008-06-14 22:52:38 -04001158 return FAILED;
James Smartf5603512006-12-02 13:35:43 -05001159 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001160 break;
James Smart915caaa2008-06-14 22:52:38 -04001161 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1162 rdata = cmnd->device->hostdata;
1163 if (!rdata)
1164 break;
1165 pnode = rdata->pnode;
dea31012005-04-17 16:05:31 -05001166 }
James Smart915caaa2008-06-14 22:52:38 -04001167 if (!rdata || pnode->nlp_state != NLP_STE_MAPPED_NODE) {
1168 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1169 "0721 LUN Reset rport "
1170 "failure: msec x%x rdata x%p\n",
1171 jiffies_to_msecs(jiffies - later), rdata);
1172 return FAILED;
1173 }
James Smart2e0fef82007-06-17 19:56:36 -05001174 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001175 if (lpfc_cmd == NULL)
James Smart915caaa2008-06-14 22:52:38 -04001176 return FAILED;
dea31012005-04-17 16:05:31 -05001177 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001178 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001179
James Smart915caaa2008-06-14 22:52:38 -04001180 status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd,
1181 cmnd->device->lun,
1182 FCP_TARGET_RESET);
1183 if (!status) {
1184 lpfc_release_scsi_buf(phba, lpfc_cmd);
1185 return FAILED;
1186 }
dea31012005-04-17 16:05:31 -05001187 iocbq = &lpfc_cmd->cur_iocbq;
1188
1189 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001190 iocbqrsp = lpfc_sli_get_iocbq(phba);
James Smart915caaa2008-06-14 22:52:38 -04001191 if (iocbqrsp == NULL) {
1192 lpfc_release_scsi_buf(phba, lpfc_cmd);
1193 return FAILED;
1194 }
James Smarte8b62012007-08-02 11:10:09 -04001195 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1196 "0703 Issue target reset to TGT %d LUN %d "
1197 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1198 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
James Smart915caaa2008-06-14 22:52:38 -04001199 status = lpfc_sli_issue_iocb_wait(phba,
1200 &phba->sli.ring[phba->sli.fcp_ring],
1201 iocbq, iocbqrsp, lpfc_cmd->timeout);
1202 if (status == IOCB_TIMEDOUT) {
James Smart7054a602007-04-25 09:52:34 -04001203 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
James Smart915caaa2008-06-14 22:52:38 -04001204 ret = TIMEOUT_ERROR;
1205 } else {
1206 if (status != IOCB_SUCCESS)
1207 ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001208 lpfc_release_scsi_buf(phba, lpfc_cmd);
1209 }
James Smarte8b62012007-08-02 11:10:09 -04001210 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1211 "0713 SCSI layer issued device reset (%d, %d) "
1212 "return x%x status x%x result x%x\n",
1213 cmnd->device->id, cmnd->device->lun, ret,
James Smart915caaa2008-06-14 22:52:38 -04001214 iocbqrsp->iocb.ulpStatus,
1215 iocbqrsp->iocb.un.ulpWord[4]);
1216 lpfc_sli_release_iocbq(phba, iocbqrsp);
1217 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id, cmnd->device->lun,
1218 LPFC_CTX_TGT);
1219 if (cnt)
1220 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1221 cmnd->device->id, cmnd->device->lun,
1222 LPFC_CTX_TGT);
1223 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1224 while (time_after(later, jiffies) && cnt) {
1225 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
1226 cnt = lpfc_sli_sum_iocb(vport, cmnd->device->id,
1227 cmnd->device->lun, LPFC_CTX_TGT);
1228 }
1229 if (cnt) {
1230 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1231 "0719 device reset I/O flush failure: "
1232 "cnt x%x\n", cnt);
1233 ret = FAILED;
1234 }
dea31012005-04-17 16:05:31 -05001235 return ret;
1236}
1237
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001238static int
James Smart7054a602007-04-25 09:52:34 -04001239lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001240{
James Smart2e0fef82007-06-17 19:56:36 -05001241 struct Scsi_Host *shost = cmnd->device->host;
1242 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1243 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001244 struct lpfc_nodelist *ndlp = NULL;
1245 int match;
James Smart915caaa2008-06-14 22:52:38 -04001246 int ret = SUCCESS, status, i;
1247 int cnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001248 struct lpfc_scsi_buf * lpfc_cmd;
James Smart915caaa2008-06-14 22:52:38 -04001249 unsigned long later;
dea31012005-04-17 16:05:31 -05001250
James Smarta90f5682006-08-17 11:58:04 -04001251 lpfc_block_error_handler(cmnd);
dea31012005-04-17 16:05:31 -05001252 /*
1253 * Since the driver manages a single bus device, reset all
1254 * targets known to the driver. Should any target reset
1255 * fail, this routine returns failure to the midlayer.
1256 */
James Smarte17da182006-07-06 15:49:25 -04001257 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001258 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001259 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001260 spin_lock_irq(shost->host_lock);
1261 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smarte47c9092008-02-08 18:49:26 -05001262 if (!NLP_CHK_NODE_ACT(ndlp))
1263 continue;
James Smart685f0bf2007-04-25 09:53:08 -04001264 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
James Smart915caaa2008-06-14 22:52:38 -04001265 ndlp->nlp_sid == i &&
James Smart685f0bf2007-04-25 09:53:08 -04001266 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001267 match = 1;
1268 break;
1269 }
1270 }
James Smart2e0fef82007-06-17 19:56:36 -05001271 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001272 if (!match)
1273 continue;
James Smart915caaa2008-06-14 22:52:38 -04001274 lpfc_cmd = lpfc_get_scsi_buf(phba);
1275 if (lpfc_cmd) {
1276 lpfc_cmd->timeout = 60;
1277 status = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1278 cmnd->device->lun,
1279 ndlp->rport->dd_data);
1280 if (status != TIMEOUT_ERROR)
1281 lpfc_release_scsi_buf(phba, lpfc_cmd);
1282 }
1283 if (!lpfc_cmd || status != SUCCESS) {
James Smarte8b62012007-08-02 11:10:09 -04001284 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1285 "0700 Bus Reset on target %d failed\n",
1286 i);
James Smart915caaa2008-06-14 22:52:38 -04001287 ret = FAILED;
dea31012005-04-17 16:05:31 -05001288 }
1289 }
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001290 /*
1291 * All outstanding txcmplq I/Os should have been aborted by
1292 * the targets. Unfortunately, some targets do not abide by
1293 * this forcing the driver to double check.
1294 */
James Smart51ef4c22007-08-02 11:10:31 -04001295 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001296 if (cnt)
James Smart51ef4c22007-08-02 11:10:31 -04001297 lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
1298 0, 0, LPFC_CTX_HOST);
James Smart915caaa2008-06-14 22:52:38 -04001299 later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
1300 while (time_after(later, jiffies) && cnt) {
1301 schedule_timeout_uninterruptible(msecs_to_jiffies(20));
James Smart51ef4c22007-08-02 11:10:31 -04001302 cnt = lpfc_sli_sum_iocb(vport, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001303 }
dea31012005-04-17 16:05:31 -05001304 if (cnt) {
James Smarte8b62012007-08-02 11:10:09 -04001305 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1306 "0715 Bus Reset I/O flush failure: "
1307 "cnt x%x left x%x\n", cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001308 ret = FAILED;
dea31012005-04-17 16:05:31 -05001309 }
James Smarte8b62012007-08-02 11:10:09 -04001310 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1311 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea31012005-04-17 16:05:31 -05001312 return ret;
1313}
1314
1315static int
dea31012005-04-17 16:05:31 -05001316lpfc_slave_alloc(struct scsi_device *sdev)
1317{
James Smart2e0fef82007-06-17 19:56:36 -05001318 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1319 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001320 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001321 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001322 uint32_t total = 0, i;
1323 uint32_t num_to_alloc = 0;
1324 unsigned long flags;
dea31012005-04-17 16:05:31 -05001325
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001326 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001327 return -ENXIO;
1328
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001329 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001330
1331 /*
1332 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1333 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001334 * HBA limit conveyed to the midlayer via the host structure. The
1335 * formula accounts for the lun_queue_depth + error handlers + 1
1336 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001337 */
1338 total = phba->total_scsi_bufs;
James Smart3de2a652007-08-02 11:09:59 -04001339 num_to_alloc = vport->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05001340
1341 /* Allow some exchanges to be available always to complete discovery */
1342 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001343 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1344 "0704 At limitation of %d preallocated "
1345 "command buffers\n", total);
dea31012005-04-17 16:05:31 -05001346 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001347 /* Allow some exchanges to be available always to complete discovery */
1348 } else if (total + num_to_alloc >
1349 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James Smarte8b62012007-08-02 11:10:09 -04001350 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1351 "0705 Allocation request of %d "
1352 "command buffers will exceed max of %d. "
1353 "Reducing allocation request to %d.\n",
1354 num_to_alloc, phba->cfg_hba_queue_depth,
1355 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001356 num_to_alloc = phba->cfg_hba_queue_depth - total;
1357 }
1358
1359 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05001360 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05001361 if (!scsi_buf) {
James Smarte8b62012007-08-02 11:10:09 -04001362 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1363 "0706 Failed to allocate "
1364 "command buffer\n");
dea31012005-04-17 16:05:31 -05001365 break;
1366 }
1367
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001368 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001369 phba->total_scsi_bufs++;
1370 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001371 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001372 }
1373 return 0;
1374}
1375
1376static int
1377lpfc_slave_configure(struct scsi_device *sdev)
1378{
James Smart2e0fef82007-06-17 19:56:36 -05001379 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1380 struct lpfc_hba *phba = vport->phba;
1381 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05001382
1383 if (sdev->tagged_supported)
James Smart3de2a652007-08-02 11:09:59 -04001384 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001385 else
James Smart3de2a652007-08-02 11:09:59 -04001386 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea31012005-04-17 16:05:31 -05001387
1388 /*
1389 * Initialize the fc transport attributes for the target
1390 * containing this scsi device. Also note that the driver's
1391 * target pointer is stored in the starget_data for the
1392 * driver's sysfs entry point functions.
1393 */
James Smart3de2a652007-08-02 11:09:59 -04001394 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05001395
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001396 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1397 lpfc_sli_poll_fcp_ring(phba);
1398 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1399 lpfc_poll_rearm_timer(phba);
1400 }
1401
dea31012005-04-17 16:05:31 -05001402 return 0;
1403}
1404
1405static void
1406lpfc_slave_destroy(struct scsi_device *sdev)
1407{
1408 sdev->hostdata = NULL;
1409 return;
1410}
1411
James Smart92d7f7b2007-06-17 19:56:38 -05001412
dea31012005-04-17 16:05:31 -05001413struct scsi_host_template lpfc_template = {
1414 .module = THIS_MODULE,
1415 .name = LPFC_DRIVER_NAME,
1416 .info = lpfc_info,
1417 .queuecommand = lpfc_queuecommand,
1418 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04001419 .eh_device_reset_handler= lpfc_device_reset_handler,
1420 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05001421 .slave_alloc = lpfc_slave_alloc,
1422 .slave_configure = lpfc_slave_configure,
1423 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04001424 .scan_finished = lpfc_scan_finished,
dea31012005-04-17 16:05:31 -05001425 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05001426 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
dea31012005-04-17 16:05:31 -05001427 .cmd_per_lun = LPFC_CMD_PER_LUN,
1428 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05001429 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001430 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001431};
James Smart3de2a652007-08-02 11:09:59 -04001432
1433struct scsi_host_template lpfc_vport_template = {
1434 .module = THIS_MODULE,
1435 .name = LPFC_DRIVER_NAME,
1436 .info = lpfc_info,
1437 .queuecommand = lpfc_queuecommand,
1438 .eh_abort_handler = lpfc_abort_handler,
1439 .eh_device_reset_handler= lpfc_device_reset_handler,
1440 .eh_bus_reset_handler = lpfc_bus_reset_handler,
1441 .slave_alloc = lpfc_slave_alloc,
1442 .slave_configure = lpfc_slave_configure,
1443 .slave_destroy = lpfc_slave_destroy,
1444 .scan_finished = lpfc_scan_finished,
1445 .this_id = -1,
James Smart83108bd2008-01-11 01:53:09 -05001446 .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
James Smart3de2a652007-08-02 11:09:59 -04001447 .cmd_per_lun = LPFC_CMD_PER_LUN,
1448 .use_clustering = ENABLE_CLUSTERING,
1449 .shost_attrs = lpfc_vport_attrs,
1450 .max_sectors = 0xFFFF,
1451};