blob: af8f8968bfba9193b0fd4b2dc8cf49401f6f011c [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
87lpfc_rampup_queue_depth(struct lpfc_hba *phba,
88 struct scsi_device *sdev)
89{
90 unsigned long flags;
91 atomic_inc(&phba->num_cmd_success);
92
93 if (phba->cfg_lun_queue_depth <= sdev->queue_depth)
94 return;
95
96 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 }
102
103 phba->last_ramp_up_time = jiffies;
104 spin_unlock_irqrestore(&phba->hbalock, flags);
105
106 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
107 if ((phba->pport->work_port_events &
108 WORKER_RAMP_UP_QUEUE) == 0) {
109 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
110 }
111 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
112
113 spin_lock_irqsave(&phba->hbalock, flags);
114 if (phba->work_wait)
115 wake_up(phba->work_wait);
116 spin_unlock_irqrestore(&phba->hbalock, flags);
117}
118
119void
120lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
121{
122 struct lpfc_vport *vport;
123 struct Scsi_Host *host;
124 struct scsi_device *sdev;
125 unsigned long new_queue_depth;
126 unsigned long num_rsrc_err, num_cmd_success;
127
128 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
129 num_cmd_success = atomic_read(&phba->num_cmd_success);
130
131 spin_lock_irq(&phba->hbalock);
132 list_for_each_entry(vport, &phba->port_list, listentry) {
133 host = lpfc_shost_from_vport(vport);
134 if (!scsi_host_get(host))
135 continue;
136
137 spin_unlock_irq(&phba->hbalock);
138
139 shost_for_each_device(sdev, host) {
140 new_queue_depth = sdev->queue_depth * num_rsrc_err /
141 (num_rsrc_err + num_cmd_success);
142 if (!new_queue_depth)
143 new_queue_depth = sdev->queue_depth - 1;
144 else
145 new_queue_depth =
146 sdev->queue_depth - new_queue_depth;
147
148 if (sdev->ordered_tags)
149 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG,
150 new_queue_depth);
151 else
152 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG,
153 new_queue_depth);
154 }
155 spin_lock_irq(&phba->hbalock);
156 scsi_host_put(host);
157 }
158 spin_unlock_irq(&phba->hbalock);
159 atomic_set(&phba->num_rsrc_err, 0);
160 atomic_set(&phba->num_cmd_success, 0);
161}
162
163void
164lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
165{
166 struct lpfc_vport *vport;
167 struct Scsi_Host *host;
168 struct scsi_device *sdev;
169
170 spin_lock_irq(&phba->hbalock);
171 list_for_each_entry(vport, &phba->port_list, listentry) {
172 host = lpfc_shost_from_vport(vport);
173 if (!scsi_host_get(host))
174 continue;
175
176 spin_unlock_irq(&phba->hbalock);
177 shost_for_each_device(sdev, host) {
178 if (sdev->ordered_tags)
179 scsi_adjust_queue_depth(sdev, MSG_ORDERED_TAG,
180 sdev->queue_depth+1);
181 else
182 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG,
183 sdev->queue_depth+1);
184 }
185 spin_lock_irq(&phba->hbalock);
186 scsi_host_put(host);
187 }
188 spin_unlock_irq(&phba->hbalock);
189 atomic_set(&phba->num_rsrc_err, 0);
190 atomic_set(&phba->num_cmd_success, 0);
191}
192
193/*
dea31012005-04-17 16:05:31 -0500194 * This routine allocates a scsi buffer, which contains all the necessary
195 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
196 * contains information to build the IOCB. The DMAable region contains
197 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
198 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
199 * and the BPL BDE is setup in the IOCB.
200 */
201static struct lpfc_scsi_buf *
James Smart2e0fef82007-06-17 19:56:36 -0500202lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea31012005-04-17 16:05:31 -0500203{
James Smart2e0fef82007-06-17 19:56:36 -0500204 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500205 struct lpfc_scsi_buf *psb;
206 struct ulp_bde64 *bpl;
207 IOCB_t *iocb;
208 dma_addr_t pdma_phys;
James Bottomley604a3e32005-10-29 10:28:33 -0500209 uint16_t iotag;
dea31012005-04-17 16:05:31 -0500210
211 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
212 if (!psb)
213 return NULL;
214 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
dea31012005-04-17 16:05:31 -0500215
216 /*
217 * Get memory from the pci pool to map the virt space to pci bus space
218 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
219 * struct fcp_rsp and the number of bde's necessary to support the
220 * sg_tablesize.
221 */
222 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
223 &psb->dma_handle);
224 if (!psb->data) {
225 kfree(psb);
226 return NULL;
227 }
228
229 /* Initialize virtual ptrs to dma_buf region. */
230 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
231
James Bottomley604a3e32005-10-29 10:28:33 -0500232 /* Allocate iotag for psb->cur_iocbq. */
233 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
234 if (iotag == 0) {
235 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
236 psb->data, psb->dma_handle);
237 kfree (psb);
238 return NULL;
239 }
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400240 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
James Bottomley604a3e32005-10-29 10:28:33 -0500241
dea31012005-04-17 16:05:31 -0500242 psb->fcp_cmnd = psb->data;
243 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
244 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
245 sizeof(struct fcp_rsp);
246
247 /* Initialize local short-hand pointers. */
248 bpl = psb->fcp_bpl;
249 pdma_phys = psb->dma_handle;
250
251 /*
252 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
253 * list bdes. Initialize the first two and leave the rest for
254 * queuecommand.
255 */
256 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
257 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
258 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
259 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
260 bpl->tus.w = le32_to_cpu(bpl->tus.w);
261 bpl++;
262
263 /* Setup the physical region for the FCP RSP */
264 pdma_phys += sizeof (struct fcp_cmnd);
265 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
266 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
267 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
268 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
269 bpl->tus.w = le32_to_cpu(bpl->tus.w);
270
271 /*
272 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
273 * initialize it with all known data now.
274 */
275 pdma_phys += (sizeof (struct fcp_rsp));
276 iocb = &psb->cur_iocbq.iocb;
277 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
278 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
279 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
280 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
281 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
282 iocb->ulpBdeCount = 1;
283 iocb->ulpClass = CLASS3;
284
285 return psb;
286}
287
Adrian Bunk455c53e2006-01-06 20:21:28 +0100288static struct lpfc_scsi_buf*
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500289lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea31012005-04-17 16:05:31 -0500290{
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400291 struct lpfc_scsi_buf * lpfc_cmd = NULL;
292 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500293 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500294
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500295 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400296 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
James Smart1dcb58e2007-04-25 09:51:30 -0400297 if (lpfc_cmd) {
298 lpfc_cmd->seg_cnt = 0;
299 lpfc_cmd->nonsg_phys = 0;
300 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500301 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400302 return lpfc_cmd;
303}
304
305static void
James Smart92d7f7b2007-06-17 19:56:38 -0500306lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400307{
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500308 unsigned long iflag = 0;
dea31012005-04-17 16:05:31 -0500309
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500310 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400311 psb->pCmd = NULL;
dea31012005-04-17 16:05:31 -0500312 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500313 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea31012005-04-17 16:05:31 -0500314}
315
316static int
James Smart92d7f7b2007-06-17 19:56:38 -0500317lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea31012005-04-17 16:05:31 -0500318{
319 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
320 struct scatterlist *sgel = NULL;
321 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
322 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
323 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
James Smart92d7f7b2007-06-17 19:56:38 -0500324 uint32_t vpi = (lpfc_cmd->cur_iocbq.vport
325 ? lpfc_cmd->cur_iocbq.vport->vpi
326 : 0);
dea31012005-04-17 16:05:31 -0500327 dma_addr_t physaddr;
328 uint32_t i, num_bde = 0;
329 int datadir = scsi_cmnd->sc_data_direction;
330 int dma_error;
331
332 /*
333 * There are three possibilities here - use scatter-gather segment, use
334 * the single mapping, or neither. Start the lpfc command prep by
335 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
336 * data bde entry.
337 */
338 bpl += 2;
339 if (scsi_cmnd->use_sg) {
340 /*
341 * The driver stores the segment count returned from pci_map_sg
342 * because this a count of dma-mappings used to map the use_sg
343 * pages. They are not guaranteed to be the same for those
344 * architectures that implement an IOMMU.
345 */
346 sgel = (struct scatterlist *)scsi_cmnd->request_buffer;
347 lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
348 scsi_cmnd->use_sg, datadir);
349 if (lpfc_cmd->seg_cnt == 0)
350 return 1;
351
352 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
353 printk(KERN_ERR "%s: Too many sg segments from "
354 "dma_map_sg. Config %d, seg_cnt %d",
355 __FUNCTION__, phba->cfg_sg_seg_cnt,
356 lpfc_cmd->seg_cnt);
357 dma_unmap_sg(&phba->pcidev->dev, sgel,
358 lpfc_cmd->seg_cnt, datadir);
359 return 1;
360 }
361
362 /*
363 * The driver established a maximum scatter-gather segment count
364 * during probe that limits the number of sg elements in any
365 * single scsi command. Just run through the seg_cnt and format
366 * the bde's.
367 */
368 for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
369 physaddr = sg_dma_address(sgel);
370 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
371 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
372 bpl->tus.f.bdeSize = sg_dma_len(sgel);
373 if (datadir == DMA_TO_DEVICE)
374 bpl->tus.f.bdeFlags = 0;
375 else
376 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
377 bpl->tus.w = le32_to_cpu(bpl->tus.w);
378 bpl++;
379 sgel++;
380 num_bde++;
381 }
382 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
383 physaddr = dma_map_single(&phba->pcidev->dev,
384 scsi_cmnd->request_buffer,
385 scsi_cmnd->request_bufflen,
386 datadir);
387 dma_error = dma_mapping_error(physaddr);
388 if (dma_error) {
389 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500390 "%d (%d):0718 Unable to dma_map_single "
391 "request_buffer: x%x\n",
392 phba->brd_no, vpi, dma_error);
dea31012005-04-17 16:05:31 -0500393 return 1;
394 }
395
396 lpfc_cmd->nonsg_phys = physaddr;
397 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
398 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
399 bpl->tus.f.bdeSize = scsi_cmnd->request_bufflen;
400 if (datadir == DMA_TO_DEVICE)
401 bpl->tus.f.bdeFlags = 0;
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400402 else
403 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
dea31012005-04-17 16:05:31 -0500404 bpl->tus.w = le32_to_cpu(bpl->tus.w);
405 num_bde = 1;
406 bpl++;
407 }
408
409 /*
410 * Finish initializing those IOCB fields that are dependent on the
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400411 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
412 * reinitialized since all iocb memory resources are used many times
413 * for transmit, receive, and continuation bpl's.
dea31012005-04-17 16:05:31 -0500414 */
James.Smart@Emulex.Com483f05f2005-08-10 15:02:45 -0400415 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea31012005-04-17 16:05:31 -0500416 iocb_cmd->un.fcpi64.bdl.bdeSize +=
417 (num_bde * sizeof (struct ulp_bde64));
418 iocb_cmd->ulpBdeCount = 1;
419 iocb_cmd->ulpLe = 1;
420 fcp_cmnd->fcpDl = be32_to_cpu(scsi_cmnd->request_bufflen);
421 return 0;
422}
423
424static void
James Smartbcf4dbf2006-07-06 15:50:08 -0400425lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
426{
427 /*
428 * There are only two special cases to consider. (1) the scsi command
429 * requested scatter-gather usage or (2) the scsi command allocated
430 * a request buffer, but did not request use_sg. There is a third
431 * case, but it does not require resource deallocation.
432 */
433 if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
434 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
435 psb->seg_cnt, psb->pCmd->sc_data_direction);
436 } else {
437 if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
438 dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
439 psb->pCmd->request_bufflen,
440 psb->pCmd->sc_data_direction);
441 }
442 }
443}
444
445static void
James Smart2e0fef82007-06-17 19:56:36 -0500446lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
447 struct lpfc_iocbq *rsp_iocb)
dea31012005-04-17 16:05:31 -0500448{
449 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
450 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
451 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
James Smart2e0fef82007-06-17 19:56:36 -0500452 struct lpfc_hba *phba = vport->phba;
James Smart7054a602007-04-25 09:52:34 -0400453 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
James Smart92d7f7b2007-06-17 19:56:38 -0500454 uint32_t vpi = vport->vpi;
dea31012005-04-17 16:05:31 -0500455 uint32_t resp_info = fcprsp->rspStatus2;
456 uint32_t scsi_status = fcprsp->rspStatus3;
James Smartc7743952006-12-02 13:34:42 -0500457 uint32_t *lp;
dea31012005-04-17 16:05:31 -0500458 uint32_t host_status = DID_OK;
459 uint32_t rsplen = 0;
James Smartc7743952006-12-02 13:34:42 -0500460 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea31012005-04-17 16:05:31 -0500461
462 /*
463 * If this is a task management command, there is no
464 * scsi packet associated with this lpfc_cmd. The driver
465 * consumes it.
466 */
467 if (fcpcmd->fcpCntl2) {
468 scsi_status = 0;
469 goto out;
470 }
471
James Smartc7743952006-12-02 13:34:42 -0500472 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
473 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
474 if (snslen > SCSI_SENSE_BUFFERSIZE)
475 snslen = SCSI_SENSE_BUFFERSIZE;
476
477 if (resp_info & RSP_LEN_VALID)
478 rsplen = be32_to_cpu(fcprsp->rspRspLen);
479 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
480 }
481 lp = (uint32_t *)cmnd->sense_buffer;
482
483 if (!scsi_status && (resp_info & RESID_UNDER))
484 logit = LOG_FCP;
485
486 lpfc_printf_log(phba, KERN_WARNING, logit,
James Smart92d7f7b2007-06-17 19:56:38 -0500487 "%d (%d):0730 FCP command x%x failed: x%x SNS x%x x%x "
James Smartc7743952006-12-02 13:34:42 -0500488 "Data: x%x x%x x%x x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500489 phba->brd_no, vpi, cmnd->cmnd[0], scsi_status,
James Smartc7743952006-12-02 13:34:42 -0500490 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
dea31012005-04-17 16:05:31 -0500491 be32_to_cpu(fcprsp->rspResId),
492 be32_to_cpu(fcprsp->rspSnsLen),
493 be32_to_cpu(fcprsp->rspRspLen),
494 fcprsp->rspInfo3);
495
496 if (resp_info & RSP_LEN_VALID) {
497 rsplen = be32_to_cpu(fcprsp->rspRspLen);
498 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
499 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
500 host_status = DID_ERROR;
501 goto out;
502 }
503 }
504
dea31012005-04-17 16:05:31 -0500505 cmnd->resid = 0;
506 if (resp_info & RESID_UNDER) {
507 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
508
509 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500510 "%d (%d):0716 FCP Read Underrun, expected %d, "
511 "residual %d Data: x%x x%x x%x\n",
512 phba->brd_no, vpi, be32_to_cpu(fcpcmd->fcpDl),
513 cmnd->resid, fcpi_parm, cmnd->cmnd[0],
514 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500515
516 /*
James Smart7054a602007-04-25 09:52:34 -0400517 * If there is an under run check if under run reported by
518 * storage array is same as the under run reported by HBA.
519 * If this is not same, there is a dropped frame.
520 */
521 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
522 fcpi_parm &&
523 (cmnd->resid != fcpi_parm)) {
524 lpfc_printf_log(phba, KERN_WARNING,
James Smart92d7f7b2007-06-17 19:56:38 -0500525 LOG_FCP | LOG_FCP_ERROR,
526 "%d (%d):0735 FCP Read Check Error "
527 "and Underrun Data: x%x x%x x%x x%x\n",
528 phba->brd_no, vpi,
529 be32_to_cpu(fcpcmd->fcpDl),
530 cmnd->resid, fcpi_parm, cmnd->cmnd[0]);
James Smart7054a602007-04-25 09:52:34 -0400531 cmnd->resid = cmnd->request_bufflen;
532 host_status = DID_ERROR;
533 }
534 /*
dea31012005-04-17 16:05:31 -0500535 * The cmnd->underflow is the minimum number of bytes that must
536 * be transfered for this command. Provided a sense condition
537 * is not present, make sure the actual amount transferred is at
538 * least the underflow value or fail.
539 */
540 if (!(resp_info & SNS_LEN_VALID) &&
541 (scsi_status == SAM_STAT_GOOD) &&
542 (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
543 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500544 "%d (%d):0717 FCP command x%x residual "
dea31012005-04-17 16:05:31 -0500545 "underrun converted to error "
James Smart92d7f7b2007-06-17 19:56:38 -0500546 "Data: x%x x%x x%x\n",
547 phba->brd_no, vpi, cmnd->cmnd[0],
548 cmnd->request_bufflen, cmnd->resid,
549 cmnd->underflow);
dea31012005-04-17 16:05:31 -0500550
551 host_status = DID_ERROR;
552 }
553 } else if (resp_info & RESID_OVER) {
554 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500555 "%d (%d):0720 FCP command x%x residual "
dea31012005-04-17 16:05:31 -0500556 "overrun error. Data: x%x x%x \n",
James Smart92d7f7b2007-06-17 19:56:38 -0500557 phba->brd_no, vpi, cmnd->cmnd[0],
dea31012005-04-17 16:05:31 -0500558 cmnd->request_bufflen, cmnd->resid);
559 host_status = DID_ERROR;
560
561 /*
562 * Check SLI validation that all the transfer was actually done
563 * (fcpi_parm should be zero). Apply check only to reads.
564 */
565 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
566 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
James Smartc7743952006-12-02 13:34:42 -0500567 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
James Smart92d7f7b2007-06-17 19:56:38 -0500568 "%d (%d):0734 FCP Read Check Error Data: "
569 "x%x x%x x%x x%x\n",
570 phba->brd_no, vpi,
571 be32_to_cpu(fcpcmd->fcpDl),
572 be32_to_cpu(fcprsp->rspResId),
573 fcpi_parm, cmnd->cmnd[0]);
dea31012005-04-17 16:05:31 -0500574 host_status = DID_ERROR;
575 cmnd->resid = cmnd->request_bufflen;
576 }
577
578 out:
579 cmnd->result = ScsiResult(host_status, scsi_status);
580}
581
582static void
583lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
584 struct lpfc_iocbq *pIocbOut)
585{
586 struct lpfc_scsi_buf *lpfc_cmd =
587 (struct lpfc_scsi_buf *) pIocbIn->context1;
James Smart2e0fef82007-06-17 19:56:36 -0500588 struct lpfc_vport *vport = pIocbIn->vport;
dea31012005-04-17 16:05:31 -0500589 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
590 struct lpfc_nodelist *pnode = rdata->pnode;
591 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
James Smart92d7f7b2007-06-17 19:56:38 -0500592 uint32_t vpi = (lpfc_cmd->cur_iocbq.vport
593 ? lpfc_cmd->cur_iocbq.vport->vpi
594 : 0);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500595 int result;
596 struct scsi_device *sdev, *tmp_sdev;
597 int depth = 0;
dea31012005-04-17 16:05:31 -0500598
599 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
600 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
601
602 if (lpfc_cmd->status) {
603 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
604 (lpfc_cmd->result & IOERR_DRVR_MASK))
605 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
606 else if (lpfc_cmd->status >= IOSTAT_CNT)
607 lpfc_cmd->status = IOSTAT_DEFAULT;
608
609 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500610 "%d (%d):0729 FCP cmd x%x failed <%d/%d> "
611 "status: x%x result: x%x Data: x%x x%x\n",
612 phba->brd_no, vpi, cmd->cmnd[0],
613 cmd->device ? cmd->device->id : 0xffff,
614 cmd->device ? cmd->device->lun : 0xffff,
615 lpfc_cmd->status, lpfc_cmd->result,
616 pIocbOut->iocb.ulpContext,
dea31012005-04-17 16:05:31 -0500617 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
618
619 switch (lpfc_cmd->status) {
620 case IOSTAT_FCP_RSP_ERROR:
621 /* Call FCP RSP handler to determine result */
James Smart2e0fef82007-06-17 19:56:36 -0500622 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea31012005-04-17 16:05:31 -0500623 break;
624 case IOSTAT_NPORT_BSY:
625 case IOSTAT_FABRIC_BSY:
626 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
627 break;
James Smart92d7f7b2007-06-17 19:56:38 -0500628 case IOSTAT_LOCAL_REJECT:
629 if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
630 lpfc_cmd->result == IOERR_NO_RESOURCES ||
631 lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
632 cmd->result = ScsiResult(DID_REQUEUE, 0);
633 break;
634 } /* else: fall through */
dea31012005-04-17 16:05:31 -0500635 default:
636 cmd->result = ScsiResult(DID_ERROR, 0);
637 break;
638 }
639
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400640 if ((pnode == NULL )
641 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
642 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea31012005-04-17 16:05:31 -0500643 } else {
644 cmd->result = ScsiResult(DID_OK, 0);
645 }
646
647 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
648 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
649
650 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500651 "%d (%d):0710 Iodone <%d/%d> cmd %p, error "
652 "x%x SNS x%x x%x Data: x%x x%x\n",
653 phba->brd_no, vpi, cmd->device->id,
dea31012005-04-17 16:05:31 -0500654 cmd->device->lun, cmd, cmd->result,
655 *lp, *(lp + 3), cmd->retries, cmd->resid);
656 }
657
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500658 result = cmd->result;
659 sdev = cmd->device;
James Smart1dcb58e2007-04-25 09:51:30 -0400660 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500661 cmd->scsi_done(cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400662
Jamie Wellnitzb8086082006-02-28 22:33:12 -0500663 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
664 lpfc_release_scsi_buf(phba, lpfc_cmd);
665 return;
666 }
667
James Smart92d7f7b2007-06-17 19:56:38 -0500668
669 if (!result)
670 lpfc_rampup_queue_depth(phba, sdev);
671
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500672 if (!result && pnode != NULL &&
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500673 ((jiffies - pnode->last_ramp_up_time) >
674 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
675 ((jiffies - pnode->last_q_full_time) >
676 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
677 (phba->cfg_lun_queue_depth > sdev->queue_depth)) {
678 shost_for_each_device(tmp_sdev, sdev->host) {
679 if (phba->cfg_lun_queue_depth > tmp_sdev->queue_depth) {
680 if (tmp_sdev->id != sdev->id)
681 continue;
682 if (tmp_sdev->ordered_tags)
683 scsi_adjust_queue_depth(tmp_sdev,
684 MSG_ORDERED_TAG,
685 tmp_sdev->queue_depth+1);
686 else
687 scsi_adjust_queue_depth(tmp_sdev,
688 MSG_SIMPLE_TAG,
689 tmp_sdev->queue_depth+1);
690
691 pnode->last_ramp_up_time = jiffies;
692 }
693 }
694 }
695
696 /*
697 * Check for queue full. If the lun is reporting queue full, then
698 * back off the lun queue depth to prevent target overloads.
699 */
Jamie Wellnitz0c6ac8e2006-02-28 19:25:36 -0500700 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500701 pnode->last_q_full_time = jiffies;
702
703 shost_for_each_device(tmp_sdev, sdev->host) {
704 if (tmp_sdev->id != sdev->id)
705 continue;
706 depth = scsi_track_queue_full(tmp_sdev,
707 tmp_sdev->queue_depth - 1);
708 }
709 /*
James Smart2e0fef82007-06-17 19:56:36 -0500710 * The queue depth cannot be lowered any more.
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500711 * Modify the returned error code to store
712 * the final depth value set by
713 * scsi_track_queue_full.
714 */
715 if (depth == -1)
716 depth = sdev->host->cmd_per_lun;
717
718 if (depth) {
719 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500720 "%d (%d):0711 detected queue full - "
721 "lun queue depth adjusted to %d.\n",
722 phba->brd_no, vpi, depth);
James.Smart@Emulex.Com445cf4f2005-11-28 11:42:38 -0500723 }
724 }
725
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400726 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -0500727}
728
729static void
James Smart2e0fef82007-06-17 19:56:36 -0500730lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
731 struct lpfc_nodelist *pnode)
dea31012005-04-17 16:05:31 -0500732{
James Smart2e0fef82007-06-17 19:56:36 -0500733 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500734 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
735 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
736 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
737 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
738 int datadir = scsi_cmnd->sc_data_direction;
739
740 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
James.Smart@Emulex.Com69859dc2005-08-10 15:02:37 -0400741 /* clear task management bits */
742 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea31012005-04-17 16:05:31 -0500743
James.Smart@Emulex.Com91886522005-08-10 15:03:09 -0400744 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
745 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500746
747 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
748
749 if (scsi_cmnd->device->tagged_supported) {
750 switch (scsi_cmnd->tag) {
751 case HEAD_OF_QUEUE_TAG:
752 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
753 break;
754 case ORDERED_QUEUE_TAG:
755 fcp_cmnd->fcpCntl1 = ORDERED_Q;
756 break;
757 default:
758 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
759 break;
760 }
761 } else
762 fcp_cmnd->fcpCntl1 = 0;
763
764 /*
765 * There are three possibilities here - use scatter-gather segment, use
766 * the single mapping, or neither. Start the lpfc command prep by
767 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
768 * data bde entry.
769 */
770 if (scsi_cmnd->use_sg) {
771 if (datadir == DMA_TO_DEVICE) {
772 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
773 iocb_cmd->un.fcpi.fcpi_parm = 0;
774 iocb_cmd->ulpPU = 0;
775 fcp_cmnd->fcpCntl3 = WRITE_DATA;
776 phba->fc4OutputRequests++;
777 } else {
778 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
779 iocb_cmd->ulpPU = PARM_READ_CHECK;
780 iocb_cmd->un.fcpi.fcpi_parm =
781 scsi_cmnd->request_bufflen;
782 fcp_cmnd->fcpCntl3 = READ_DATA;
783 phba->fc4InputRequests++;
784 }
785 } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
786 if (datadir == DMA_TO_DEVICE) {
787 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
788 iocb_cmd->un.fcpi.fcpi_parm = 0;
789 iocb_cmd->ulpPU = 0;
790 fcp_cmnd->fcpCntl3 = WRITE_DATA;
791 phba->fc4OutputRequests++;
792 } else {
793 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
794 iocb_cmd->ulpPU = PARM_READ_CHECK;
795 iocb_cmd->un.fcpi.fcpi_parm =
796 scsi_cmnd->request_bufflen;
797 fcp_cmnd->fcpCntl3 = READ_DATA;
798 phba->fc4InputRequests++;
799 }
800 } else {
801 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
802 iocb_cmd->un.fcpi.fcpi_parm = 0;
803 iocb_cmd->ulpPU = 0;
804 fcp_cmnd->fcpCntl3 = 0;
805 phba->fc4ControlRequests++;
806 }
807
808 /*
809 * Finish initializing those IOCB fields that are independent
810 * of the scsi_cmnd request_buffer
811 */
812 piocbq->iocb.ulpContext = pnode->nlp_rpi;
813 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
814 piocbq->iocb.ulpFCP2Rcvy = 1;
815
816 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
817 piocbq->context1 = lpfc_cmd;
818 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
819 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
James Smart2e0fef82007-06-17 19:56:36 -0500820 piocbq->vport = vport;
dea31012005-04-17 16:05:31 -0500821}
822
823static int
James Smart2e0fef82007-06-17 19:56:36 -0500824lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea31012005-04-17 16:05:31 -0500825 struct lpfc_scsi_buf *lpfc_cmd,
James Smart420b630d2006-07-06 15:50:16 -0400826 unsigned int lun,
dea31012005-04-17 16:05:31 -0500827 uint8_t task_mgmt_cmd)
828{
dea31012005-04-17 16:05:31 -0500829 struct lpfc_iocbq *piocbq;
830 IOCB_t *piocb;
831 struct fcp_cmnd *fcp_cmnd;
James Smart0b18ac42006-05-01 21:50:40 -0400832 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea31012005-04-17 16:05:31 -0500833 struct lpfc_nodelist *ndlp = rdata->pnode;
834
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400835 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea31012005-04-17 16:05:31 -0500836 return 0;
837 }
838
dea31012005-04-17 16:05:31 -0500839 piocbq = &(lpfc_cmd->cur_iocbq);
James Smart2e0fef82007-06-17 19:56:36 -0500840 piocbq->vport = vport;
841
dea31012005-04-17 16:05:31 -0500842 piocb = &piocbq->iocb;
843
844 fcp_cmnd = lpfc_cmd->fcp_cmnd;
James Smart420b630d2006-07-06 15:50:16 -0400845 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
dea31012005-04-17 16:05:31 -0500846 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
847
848 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
849
850 piocb->ulpContext = ndlp->nlp_rpi;
851 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
852 piocb->ulpFCP2Rcvy = 1;
853 }
854 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
855
856 /* ulpTimeout is only one byte */
857 if (lpfc_cmd->timeout > 0xff) {
858 /*
859 * Do not timeout the command at the firmware level.
860 * The driver will provide the timeout mechanism.
861 */
862 piocb->ulpTimeout = 0;
863 } else {
864 piocb->ulpTimeout = lpfc_cmd->timeout;
865 }
866
James Smart2e0fef82007-06-17 19:56:36 -0500867 return 1;
dea31012005-04-17 16:05:31 -0500868}
869
James Smart7054a602007-04-25 09:52:34 -0400870static void
871lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
872 struct lpfc_iocbq *cmdiocbq,
873 struct lpfc_iocbq *rspiocbq)
874{
875 struct lpfc_scsi_buf *lpfc_cmd =
876 (struct lpfc_scsi_buf *) cmdiocbq->context1;
877 if (lpfc_cmd)
878 lpfc_release_scsi_buf(phba, lpfc_cmd);
879 return;
880}
881
dea31012005-04-17 16:05:31 -0500882static int
James Smart2e0fef82007-06-17 19:56:36 -0500883lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
James Smart420b630d2006-07-06 15:50:16 -0400884 unsigned tgt_id, unsigned int lun,
885 struct lpfc_rport_data *rdata)
dea31012005-04-17 16:05:31 -0500886{
James Smart2e0fef82007-06-17 19:56:36 -0500887 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500888 struct lpfc_iocbq *iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400889 struct lpfc_iocbq *iocbqrsp;
dea31012005-04-17 16:05:31 -0500890 int ret;
891
James Smartf5603512006-12-02 13:35:43 -0500892 if (!rdata->pnode)
893 return FAILED;
894
James Smart0b18ac42006-05-01 21:50:40 -0400895 lpfc_cmd->rdata = rdata;
James Smart2e0fef82007-06-17 19:56:36 -0500896 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
James Smart420b630d2006-07-06 15:50:16 -0400897 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -0500898 if (!ret)
899 return FAILED;
900
dea31012005-04-17 16:05:31 -0500901 iocbq = &lpfc_cmd->cur_iocbq;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400902 iocbqrsp = lpfc_sli_get_iocbq(phba);
903
dea31012005-04-17 16:05:31 -0500904 if (!iocbqrsp)
905 return FAILED;
dea31012005-04-17 16:05:31 -0500906
James Smart0b18ac42006-05-01 21:50:40 -0400907 /* Issue Target Reset to TGT <num> */
908 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -0500909 "%d (%d):0702 Issue Target Reset to TGT %d "
James Smart0b18ac42006-05-01 21:50:40 -0400910 "Data: x%x x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -0500911 phba->brd_no, vport->vpi, tgt_id,
912 rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
James Smart0b18ac42006-05-01 21:50:40 -0400913
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -0400914 ret = lpfc_sli_issue_iocb_wait(phba,
915 &phba->sli.ring[phba->sli.fcp_ring],
916 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -0500917 if (ret != IOCB_SUCCESS) {
James Smart7054a602007-04-25 09:52:34 -0400918 if (ret == IOCB_TIMEDOUT)
919 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
dea31012005-04-17 16:05:31 -0500920 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea31012005-04-17 16:05:31 -0500921 } else {
922 ret = SUCCESS;
923 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
924 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
925 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
926 (lpfc_cmd->result & IOERR_DRVR_MASK))
927 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
928 }
929
James Bottomley604a3e32005-10-29 10:28:33 -0500930 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -0500931 return ret;
932}
933
dea31012005-04-17 16:05:31 -0500934const char *
935lpfc_info(struct Scsi_Host *host)
936{
James Smart2e0fef82007-06-17 19:56:36 -0500937 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
938 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -0500939 int len;
940 static char lpfcinfobuf[384];
941
942 memset(lpfcinfobuf,0,384);
943 if (phba && phba->pcidev){
944 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
945 len = strlen(lpfcinfobuf);
946 snprintf(lpfcinfobuf + len,
947 384-len,
948 " on PCI bus %02x device %02x irq %d",
949 phba->pcidev->bus->number,
950 phba->pcidev->devfn,
951 phba->pcidev->irq);
952 len = strlen(lpfcinfobuf);
953 if (phba->Port[0]) {
954 snprintf(lpfcinfobuf + len,
955 384-len,
956 " port %s",
957 phba->Port);
958 }
959 }
960 return lpfcinfobuf;
961}
962
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500963static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
964{
965 unsigned long poll_tmo_expires =
966 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
967
968 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
969 mod_timer(&phba->fcp_poll_timer,
970 poll_tmo_expires);
971}
972
973void lpfc_poll_start_timer(struct lpfc_hba * phba)
974{
975 lpfc_poll_rearm_timer(phba);
976}
977
978void lpfc_poll_timeout(unsigned long ptr)
979{
James Smart2e0fef82007-06-17 19:56:36 -0500980 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500981
982 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
983 lpfc_sli_poll_fcp_ring (phba);
984 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
985 lpfc_poll_rearm_timer(phba);
986 }
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -0500987}
988
dea31012005-04-17 16:05:31 -0500989static int
990lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
991{
James Smart2e0fef82007-06-17 19:56:36 -0500992 struct Scsi_Host *shost = cmnd->device->host;
993 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
994 struct lpfc_hba *phba = vport->phba;
995 struct lpfc_sli *psli = &phba->sli;
dea31012005-04-17 16:05:31 -0500996 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
997 struct lpfc_nodelist *ndlp = rdata->pnode;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -0400998 struct lpfc_scsi_buf *lpfc_cmd;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -0400999 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001000 int err;
dea31012005-04-17 16:05:31 -05001001
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001002 err = fc_remote_port_chkready(rport);
1003 if (err) {
1004 cmnd->result = err;
dea31012005-04-17 16:05:31 -05001005 goto out_fail_command;
1006 }
1007
1008 /*
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001009 * Catch race where our node has transitioned, but the
1010 * transport is still transitioning.
dea31012005-04-17 16:05:31 -05001011 */
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001012 if (!ndlp) {
1013 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
1014 goto out_fail_command;
dea31012005-04-17 16:05:31 -05001015 }
James Smarted957682007-06-17 19:56:37 -05001016 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001017 if (lpfc_cmd == NULL) {
James Smart92d7f7b2007-06-17 19:56:38 -05001018 lpfc_adjust_queue_depth(phba);
1019
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -05001020 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001021 "%d (%d):0707 driver's buffer pool is empty, "
1022 "IO busied\n",
1023 phba->brd_no, vport->vpi);
dea31012005-04-17 16:05:31 -05001024 goto out_host_busy;
1025 }
1026
1027 /*
1028 * Store the midlayer's command structure for the completion phase
1029 * and complete the command initialization.
1030 */
1031 lpfc_cmd->pCmd = cmnd;
1032 lpfc_cmd->rdata = rdata;
1033 lpfc_cmd->timeout = 0;
1034 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
1035 cmnd->scsi_done = done;
1036
1037 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
1038 if (err)
1039 goto out_host_busy_free_buf;
1040
James Smart2e0fef82007-06-17 19:56:36 -05001041 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea31012005-04-17 16:05:31 -05001042
1043 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
James Smart92d7f7b2007-06-17 19:56:38 -05001044 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea31012005-04-17 16:05:31 -05001045 if (err)
1046 goto out_host_busy_free_buf;
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001047
1048 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1049 lpfc_sli_poll_fcp_ring(phba);
1050 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1051 lpfc_poll_rearm_timer(phba);
1052 }
1053
dea31012005-04-17 16:05:31 -05001054 return 0;
1055
1056 out_host_busy_free_buf:
James Smartbcf4dbf2006-07-06 15:50:08 -04001057 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001058 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea31012005-04-17 16:05:31 -05001059 out_host_busy:
1060 return SCSI_MLQUEUE_HOST_BUSY;
1061
1062 out_fail_command:
1063 done(cmnd);
1064 return 0;
1065}
1066
James Smarta90f5682006-08-17 11:58:04 -04001067static void
1068lpfc_block_error_handler(struct scsi_cmnd *cmnd)
1069{
1070 struct Scsi_Host *shost = cmnd->device->host;
1071 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1072
1073 spin_lock_irq(shost->host_lock);
1074 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1075 spin_unlock_irq(shost->host_lock);
1076 msleep(1000);
1077 spin_lock_irq(shost->host_lock);
1078 }
1079 spin_unlock_irq(shost->host_lock);
1080 return;
1081}
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001082
dea31012005-04-17 16:05:31 -05001083static int
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001084lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001085{
James Smart2e0fef82007-06-17 19:56:36 -05001086 struct Scsi_Host *shost = cmnd->device->host;
1087 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1088 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001089 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001090 struct lpfc_iocbq *iocb;
1091 struct lpfc_iocbq *abtsiocb;
dea31012005-04-17 16:05:31 -05001092 struct lpfc_scsi_buf *lpfc_cmd;
dea31012005-04-17 16:05:31 -05001093 IOCB_t *cmd, *icmd;
dea31012005-04-17 16:05:31 -05001094 unsigned int loop_count = 0;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001095 int ret = SUCCESS;
1096
James Smarta90f5682006-08-17 11:58:04 -04001097 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001098 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1099 BUG_ON(!lpfc_cmd);
dea31012005-04-17 16:05:31 -05001100
1101 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001102 * If pCmd field of the corresponding lpfc_scsi_buf structure
1103 * points to a different SCSI command, then the driver has
1104 * already completed this command, but the midlayer did not
1105 * see the completion before the eh fired. Just return
1106 * SUCCESS.
dea31012005-04-17 16:05:31 -05001107 */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001108 iocb = &lpfc_cmd->cur_iocbq;
1109 if (lpfc_cmd->pCmd != cmnd)
1110 goto out;
dea31012005-04-17 16:05:31 -05001111
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001112 BUG_ON(iocb->context1 != lpfc_cmd);
dea31012005-04-17 16:05:31 -05001113
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001114 abtsiocb = lpfc_sli_get_iocbq(phba);
1115 if (abtsiocb == NULL) {
1116 ret = FAILED;
dea31012005-04-17 16:05:31 -05001117 goto out;
1118 }
1119
dea31012005-04-17 16:05:31 -05001120 /*
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001121 * The scsi command can not be in txq and it is in flight because the
1122 * pCmd is still pointig at the SCSI command we have to abort. There
1123 * is no need to search the txcmplq. Just send an abort to the FW.
dea31012005-04-17 16:05:31 -05001124 */
dea31012005-04-17 16:05:31 -05001125
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001126 cmd = &iocb->iocb;
1127 icmd = &abtsiocb->iocb;
1128 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1129 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1130 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea31012005-04-17 16:05:31 -05001131
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001132 icmd->ulpLe = 1;
1133 icmd->ulpClass = cmd->ulpClass;
James Smart2e0fef82007-06-17 19:56:36 -05001134 if (lpfc_is_link_up(phba))
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001135 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1136 else
1137 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea31012005-04-17 16:05:31 -05001138
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001139 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
James Smart2e0fef82007-06-17 19:56:36 -05001140 abtsiocb->vport = vport;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001141 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1142 lpfc_sli_release_iocbq(phba, abtsiocb);
1143 ret = FAILED;
1144 goto out;
1145 }
1146
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001147 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1148 lpfc_sli_poll_fcp_ring (phba);
1149
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001150 /* Wait for abort to complete */
1151 while (lpfc_cmd->pCmd == cmnd)
1152 {
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001153 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1154 lpfc_sli_poll_fcp_ring (phba);
1155
James Smart2e0fef82007-06-17 19:56:36 -05001156 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT * HZ);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001157 if (++loop_count
James Smartc01f3202006-08-18 17:47:08 -04001158 > (2 * phba->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
dea31012005-04-17 16:05:31 -05001159 break;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001160 }
dea31012005-04-17 16:05:31 -05001161
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001162 if (lpfc_cmd->pCmd == cmnd) {
1163 ret = FAILED;
1164 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001165 "%d (%d):0748 abort handler timed out waiting "
1166 "for abort to complete: ret %#x, ID %d, "
1167 "LUN %d, snum %#lx\n",
1168 phba->brd_no, vport->vpi, ret,
1169 cmnd->device->id, cmnd->device->lun,
1170 cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001171 }
1172
1173 out:
1174 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001175 "%d (%d):0749 SCSI Layer I/O Abort Request "
James.Smart@Emulex.Com1de933f2005-11-28 11:41:15 -05001176 "Status x%x ID %d LUN %d snum %#lx\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001177 phba->brd_no, vport->vpi, ret, cmnd->device->id,
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001178 cmnd->device->lun, cmnd->serial_number);
dea31012005-04-17 16:05:31 -05001179
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001180 return ret;
dea31012005-04-17 16:05:31 -05001181}
1182
1183static int
James Smart7054a602007-04-25 09:52:34 -04001184lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001185{
James Smart2e0fef82007-06-17 19:56:36 -05001186 struct Scsi_Host *shost = cmnd->device->host;
1187 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1188 struct lpfc_hba *phba = vport->phba;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001189 struct lpfc_scsi_buf *lpfc_cmd;
1190 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea31012005-04-17 16:05:31 -05001191 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1192 struct lpfc_nodelist *pnode = rdata->pnode;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001193 uint32_t cmd_result = 0, cmd_status = 0;
dea31012005-04-17 16:05:31 -05001194 int ret = FAILED;
James Smart7054a602007-04-25 09:52:34 -04001195 int iocb_status = IOCB_SUCCESS;
dea31012005-04-17 16:05:31 -05001196 int cnt, loopcnt;
1197
James Smarta90f5682006-08-17 11:58:04 -04001198 lpfc_block_error_handler(cmnd);
James Smartf5603512006-12-02 13:35:43 -05001199 loopcnt = 0;
dea31012005-04-17 16:05:31 -05001200 /*
1201 * If target is not in a MAPPED state, delay the reset until
James Smartc01f3202006-08-18 17:47:08 -04001202 * target is rediscovered or devloss timeout expires.
dea31012005-04-17 16:05:31 -05001203 */
James Smart92d7f7b2007-06-17 19:56:38 -05001204 while (1) {
dea31012005-04-17 16:05:31 -05001205 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001206 goto out;
dea31012005-04-17 16:05:31 -05001207
1208 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001209 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
James Smartf5603512006-12-02 13:35:43 -05001210 loopcnt++;
1211 rdata = cmnd->device->hostdata;
1212 if (!rdata ||
1213 (loopcnt > ((phba->cfg_devloss_tmo * 2) + 1))) {
1214 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001215 "%d (%d):0721 LUN Reset rport "
1216 "failure: cnt x%x rdata x%p\n",
1217 phba->brd_no, vport->vpi,
1218 loopcnt, rdata);
James Smartf5603512006-12-02 13:35:43 -05001219 goto out;
1220 }
1221 pnode = rdata->pnode;
1222 if (!pnode)
James Smart7054a602007-04-25 09:52:34 -04001223 goto out;
dea31012005-04-17 16:05:31 -05001224 }
James Smartf5603512006-12-02 13:35:43 -05001225 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea31012005-04-17 16:05:31 -05001226 break;
1227 }
1228
James Smart2e0fef82007-06-17 19:56:36 -05001229 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001230 if (lpfc_cmd == NULL)
1231 goto out;
1232
dea31012005-04-17 16:05:31 -05001233 lpfc_cmd->timeout = 60;
James Smart0b18ac42006-05-01 21:50:40 -04001234 lpfc_cmd->rdata = rdata;
dea31012005-04-17 16:05:31 -05001235
James Smart2e0fef82007-06-17 19:56:36 -05001236 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
James Smart7054a602007-04-25 09:52:34 -04001237 FCP_TARGET_RESET);
dea31012005-04-17 16:05:31 -05001238 if (!ret)
1239 goto out_free_scsi_buf;
1240
1241 iocbq = &lpfc_cmd->cur_iocbq;
1242
1243 /* get a buffer for this IOCB command response */
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001244 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea31012005-04-17 16:05:31 -05001245 if (iocbqrsp == NULL)
1246 goto out_free_scsi_buf;
1247
James Smart0b18ac42006-05-01 21:50:40 -04001248 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001249 "%d (%d):0703 Issue target reset to TGT %d LUN %d "
1250 "rpi x%x nlp_flag x%x\n",
1251 phba->brd_no, vport->vpi, cmnd->device->id,
James Smart0b18ac42006-05-01 21:50:40 -04001252 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
1253
James Smart7054a602007-04-25 09:52:34 -04001254 iocb_status = lpfc_sli_issue_iocb_wait(phba,
James.Smart@Emulex.Com68876922005-10-28 20:29:47 -04001255 &phba->sli.ring[phba->sli.fcp_ring],
1256 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea31012005-04-17 16:05:31 -05001257
James Smart7054a602007-04-25 09:52:34 -04001258 if (iocb_status == IOCB_TIMEDOUT)
1259 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
1260
1261 if (iocb_status == IOCB_SUCCESS)
1262 ret = SUCCESS;
1263 else
1264 ret = iocb_status;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001265
1266 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1267 cmd_status = iocbqrsp->iocb.ulpStatus;
1268
1269 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea31012005-04-17 16:05:31 -05001270
1271 /*
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001272 * All outstanding txcmplq I/Os should have been aborted by the device.
dea31012005-04-17 16:05:31 -05001273 * Unfortunately, some targets do not abide by this forcing the driver
1274 * to double check.
1275 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001276 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1277 cmnd->device->id, cmnd->device->lun,
1278 LPFC_CTX_LUN);
1279 if (cnt)
1280 lpfc_sli_abort_iocb(phba,
1281 &phba->sli.ring[phba->sli.fcp_ring],
1282 cmnd->device->id, cmnd->device->lun,
1283 0, LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001284 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001285 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001286 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001287
1288 if (++loopcnt
James Smartc01f3202006-08-18 17:47:08 -04001289 > (2 * phba->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001290 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001291
1292 cnt = lpfc_sli_sum_iocb(phba,
1293 &phba->sli.ring[phba->sli.fcp_ring],
1294 cmnd->device->id, cmnd->device->lun,
1295 LPFC_CTX_LUN);
dea31012005-04-17 16:05:31 -05001296 }
1297
1298 if (cnt) {
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001299 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001300 "%d (%d):0719 device reset I/O flush failure: "
1301 "cnt x%x\n",
1302 phba->brd_no, vport->vpi, cnt);
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001303 ret = FAILED;
dea31012005-04-17 16:05:31 -05001304 }
1305
dea31012005-04-17 16:05:31 -05001306out_free_scsi_buf:
James Smart7054a602007-04-25 09:52:34 -04001307 if (iocb_status != IOCB_TIMEDOUT) {
1308 lpfc_release_scsi_buf(phba, lpfc_cmd);
1309 }
dea31012005-04-17 16:05:31 -05001310 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001311 "%d (%d):0713 SCSI layer issued device reset (%d, %d) "
James Smart7054a602007-04-25 09:52:34 -04001312 "return x%x status x%x result x%x\n",
James Smart92d7f7b2007-06-17 19:56:38 -05001313 phba->brd_no, vport->vpi, cmnd->device->id,
1314 cmnd->device->lun, ret, cmd_status, cmd_result);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001315
dea31012005-04-17 16:05:31 -05001316out:
1317 return ret;
1318}
1319
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04001320static int
James Smart7054a602007-04-25 09:52:34 -04001321lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea31012005-04-17 16:05:31 -05001322{
James Smart2e0fef82007-06-17 19:56:36 -05001323 struct Scsi_Host *shost = cmnd->device->host;
1324 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1325 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001326 struct lpfc_nodelist *ndlp = NULL;
1327 int match;
1328 int ret = FAILED, i, err_count = 0;
1329 int cnt, loopcnt;
James.Smart@Emulex.Com0bd4ca22005-10-28 20:30:02 -04001330 struct lpfc_scsi_buf * lpfc_cmd;
dea31012005-04-17 16:05:31 -05001331
James Smarta90f5682006-08-17 11:58:04 -04001332 lpfc_block_error_handler(cmnd);
James.Smart@Emulex.Com63c59c32005-11-28 11:41:53 -05001333
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001334 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea31012005-04-17 16:05:31 -05001335 if (lpfc_cmd == NULL)
1336 goto out;
1337
1338 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1339 lpfc_cmd->timeout = 60;
dea31012005-04-17 16:05:31 -05001340
1341 /*
1342 * Since the driver manages a single bus device, reset all
1343 * targets known to the driver. Should any target reset
1344 * fail, this routine returns failure to the midlayer.
1345 */
James Smarte17da182006-07-06 15:49:25 -04001346 for (i = 0; i < LPFC_MAX_TARGET; i++) {
James Smart685f0bf2007-04-25 09:53:08 -04001347 /* Search for mapped node by target ID */
dea31012005-04-17 16:05:31 -05001348 match = 0;
James Smart2e0fef82007-06-17 19:56:36 -05001349 spin_lock_irq(shost->host_lock);
1350 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
James Smart685f0bf2007-04-25 09:53:08 -04001351 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1352 i == ndlp->nlp_sid &&
1353 ndlp->rport) {
dea31012005-04-17 16:05:31 -05001354 match = 1;
1355 break;
1356 }
1357 }
James Smart2e0fef82007-06-17 19:56:36 -05001358 spin_unlock_irq(shost->host_lock);
dea31012005-04-17 16:05:31 -05001359 if (!match)
1360 continue;
1361
James Smart2e0fef82007-06-17 19:56:36 -05001362 ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1363 cmnd->device->lun,
James Smart420b630d2006-07-06 15:50:16 -04001364 ndlp->rport->dd_data);
dea31012005-04-17 16:05:31 -05001365 if (ret != SUCCESS) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001366 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001367 "%d (%d):0700 Bus Reset on target %d "
1368 "failed\n",
1369 phba->brd_no, vport->vpi, i);
dea31012005-04-17 16:05:31 -05001370 err_count++;
James Smart7054a602007-04-25 09:52:34 -04001371 break;
dea31012005-04-17 16:05:31 -05001372 }
1373 }
1374
James Smart7054a602007-04-25 09:52:34 -04001375 if (ret != IOCB_TIMEDOUT)
1376 lpfc_release_scsi_buf(phba, lpfc_cmd);
1377
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001378 if (err_count == 0)
1379 ret = SUCCESS;
James Smart7054a602007-04-25 09:52:34 -04001380 else
1381 ret = FAILED;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001382
1383 /*
1384 * All outstanding txcmplq I/Os should have been aborted by
1385 * the targets. Unfortunately, some targets do not abide by
1386 * this forcing the driver to double check.
1387 */
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001388 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1389 0, 0, LPFC_CTX_HOST);
1390 if (cnt)
1391 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1392 0, 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001393 loopcnt = 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001394 while(cnt) {
Nishanth Aravamudana9a30472005-11-07 01:01:20 -08001395 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea31012005-04-17 16:05:31 -05001396
1397 if (++loopcnt
James Smartc01f3202006-08-18 17:47:08 -04001398 > (2 * phba->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea31012005-04-17 16:05:31 -05001399 break;
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001400
1401 cnt = lpfc_sli_sum_iocb(phba,
1402 &phba->sli.ring[phba->sli.fcp_ring],
1403 0, 0, LPFC_CTX_HOST);
dea31012005-04-17 16:05:31 -05001404 }
1405
1406 if (cnt) {
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001407 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001408 "%d (%d):0715 Bus Reset I/O flush failure: "
1409 "cnt x%x left x%x\n",
1410 phba->brd_no, vport->vpi, cnt, i);
James.Smart@Emulex.Com6175c022005-11-28 11:42:05 -05001411 ret = FAILED;
dea31012005-04-17 16:05:31 -05001412 }
1413
James Smart92d7f7b2007-06-17 19:56:38 -05001414 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1415 "%d (%d):0714 SCSI layer issued Bus Reset Data: x%x\n",
1416 phba->brd_no, vport->vpi, ret);
dea31012005-04-17 16:05:31 -05001417out:
1418 return ret;
1419}
1420
1421static int
dea31012005-04-17 16:05:31 -05001422lpfc_slave_alloc(struct scsi_device *sdev)
1423{
James Smart2e0fef82007-06-17 19:56:36 -05001424 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1425 struct lpfc_hba *phba = vport->phba;
dea31012005-04-17 16:05:31 -05001426 struct lpfc_scsi_buf *scsi_buf = NULL;
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001427 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea31012005-04-17 16:05:31 -05001428 uint32_t total = 0, i;
1429 uint32_t num_to_alloc = 0;
1430 unsigned long flags;
dea31012005-04-17 16:05:31 -05001431
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001432 if (!rport || fc_remote_port_chkready(rport))
dea31012005-04-17 16:05:31 -05001433 return -ENXIO;
1434
James.Smart@Emulex.Com19a7b4a2005-10-18 12:03:35 -04001435 sdev->hostdata = rport->dd_data;
dea31012005-04-17 16:05:31 -05001436
1437 /*
1438 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1439 * available list of scsi buffers. Don't allocate more than the
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001440 * HBA limit conveyed to the midlayer via the host structure. The
1441 * formula accounts for the lun_queue_depth + error handlers + 1
1442 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea31012005-04-17 16:05:31 -05001443 */
1444 total = phba->total_scsi_bufs;
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001445 num_to_alloc = phba->cfg_lun_queue_depth + 2;
James Smart92d7f7b2007-06-17 19:56:38 -05001446
1447 /* Allow some exchanges to be available always to complete discovery */
1448 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001449 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001450 "%d (%d):0704 At limitation of %d "
1451 "preallocated command buffers\n",
1452 phba->brd_no, vport->vpi, total);
dea31012005-04-17 16:05:31 -05001453 return 0;
James Smart92d7f7b2007-06-17 19:56:38 -05001454
1455 /* Allow some exchanges to be available always to complete discovery */
1456 } else if (total + num_to_alloc >
1457 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001458 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001459 "%d (%d):0705 Allocation request of %d "
1460 "command buffers will exceed max of %d. "
1461 "Reducing allocation request to %d.\n",
1462 phba->brd_no, vport->vpi, num_to_alloc,
1463 phba->cfg_hba_queue_depth,
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001464 (phba->cfg_hba_queue_depth - total));
dea31012005-04-17 16:05:31 -05001465 num_to_alloc = phba->cfg_hba_queue_depth - total;
1466 }
1467
1468 for (i = 0; i < num_to_alloc; i++) {
James Smart2e0fef82007-06-17 19:56:36 -05001469 scsi_buf = lpfc_new_scsi_buf(vport);
dea31012005-04-17 16:05:31 -05001470 if (!scsi_buf) {
James.Smart@Emulex.Coma784efb2005-10-28 20:29:51 -04001471 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
James Smart92d7f7b2007-06-17 19:56:38 -05001472 "%d (%d):0706 Failed to allocate "
1473 "command buffer\n",
1474 phba->brd_no, vport->vpi);
dea31012005-04-17 16:05:31 -05001475 break;
1476 }
1477
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001478 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001479 phba->total_scsi_bufs++;
1480 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001481 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea31012005-04-17 16:05:31 -05001482 }
1483 return 0;
1484}
1485
1486static int
1487lpfc_slave_configure(struct scsi_device *sdev)
1488{
James Smart2e0fef82007-06-17 19:56:36 -05001489 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1490 struct lpfc_hba *phba = vport->phba;
1491 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea31012005-04-17 16:05:31 -05001492
1493 if (sdev->tagged_supported)
1494 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1495 else
1496 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1497
1498 /*
1499 * Initialize the fc transport attributes for the target
1500 * containing this scsi device. Also note that the driver's
1501 * target pointer is stored in the starget_data for the
1502 * driver's sysfs entry point functions.
1503 */
James Smartc01f3202006-08-18 17:47:08 -04001504 rport->dev_loss_tmo = phba->cfg_devloss_tmo;
dea31012005-04-17 16:05:31 -05001505
James.Smart@Emulex.Com875fbdf2005-11-29 16:32:13 -05001506 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1507 lpfc_sli_poll_fcp_ring(phba);
1508 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1509 lpfc_poll_rearm_timer(phba);
1510 }
1511
dea31012005-04-17 16:05:31 -05001512 return 0;
1513}
1514
1515static void
1516lpfc_slave_destroy(struct scsi_device *sdev)
1517{
1518 sdev->hostdata = NULL;
1519 return;
1520}
1521
James Smart92d7f7b2007-06-17 19:56:38 -05001522
dea31012005-04-17 16:05:31 -05001523struct scsi_host_template lpfc_template = {
1524 .module = THIS_MODULE,
1525 .name = LPFC_DRIVER_NAME,
1526 .info = lpfc_info,
1527 .queuecommand = lpfc_queuecommand,
1528 .eh_abort_handler = lpfc_abort_handler,
James Smart7054a602007-04-25 09:52:34 -04001529 .eh_device_reset_handler= lpfc_device_reset_handler,
1530 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea31012005-04-17 16:05:31 -05001531 .slave_alloc = lpfc_slave_alloc,
1532 .slave_configure = lpfc_slave_configure,
1533 .slave_destroy = lpfc_slave_destroy,
James Smart47a86172007-04-25 09:53:22 -04001534 .scan_finished = lpfc_scan_finished,
1535 .scan_start = lpfc_scan_start,
dea31012005-04-17 16:05:31 -05001536 .this_id = -1,
1537 .sg_tablesize = LPFC_SG_SEG_CNT,
1538 .cmd_per_lun = LPFC_CMD_PER_LUN,
1539 .use_clustering = ENABLE_CLUSTERING,
James Smart2e0fef82007-06-17 19:56:36 -05001540 .shost_attrs = lpfc_hba_attrs,
James.Smart@Emulex.Com564b2962005-06-25 10:34:17 -04001541 .max_sectors = 0xFFFF,
dea31012005-04-17 16:05:31 -05001542};