[SCSI] lpfc 8.2.2 : Miscellaneous management and logging mods
[deliverable/linux.git] / drivers / scsi / lpfc / lpfc_scsi.c
CommitLineData
dea3101e 1/*******************************************************************
2 * This file is part of the Emulex Linux Device Driver for *
c44ce173 3 * Fibre Channel Host Bus Adapters. *
9413afff 4 * Copyright (C) 2004-2007 Emulex. All rights reserved. *
c44ce173 5 * EMULEX and SLI are trademarks of Emulex. *
dea3101e 6 * www.emulex.com *
c44ce173 7 * Portions Copyright (C) 2004-2005 Christoph Hellwig *
dea3101e 8 * *
9 * This program is free software; you can redistribute it and/or *
c44ce173
JSEC
10 * 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. *
dea3101e 20 *******************************************************************/
21
dea3101e 22#include <linux/pci.h>
23#include <linux/interrupt.h>
a90f5684 24#include <linux/delay.h>
dea3101e 25
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"
92d7f7b0 40#include "lpfc_vport.h"
dea3101e 41
42#define LPFC_RESET_WAIT 2
43#define LPFC_ABORT_WAIT 2
44
92d7f7b0
JS
45/*
46 * 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
3de2a653 87lpfc_rampup_queue_depth(struct lpfc_vport *vport,
92d7f7b0
JS
88 struct scsi_device *sdev)
89{
90 unsigned long flags;
3de2a653 91 struct lpfc_hba *phba = vport->phba;
92d7f7b0
JS
92 atomic_inc(&phba->num_cmd_success);
93
3de2a653 94 if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
92d7f7b0 95 return;
92d7f7b0
JS
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 }
92d7f7b0
JS
102 phba->last_ramp_up_time = jiffies;
103 spin_unlock_irqrestore(&phba->hbalock, flags);
104
105 spin_lock_irqsave(&phba->pport->work_port_lock, flags);
106 if ((phba->pport->work_port_events &
107 WORKER_RAMP_UP_QUEUE) == 0) {
108 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
109 }
110 spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
111
112 spin_lock_irqsave(&phba->hbalock, flags);
113 if (phba->work_wait)
114 wake_up(phba->work_wait);
115 spin_unlock_irqrestore(&phba->hbalock, flags);
116}
117
118void
119lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
120{
549e55cd
JS
121 struct lpfc_vport **vports;
122 struct Scsi_Host *shost;
92d7f7b0
JS
123 struct scsi_device *sdev;
124 unsigned long new_queue_depth;
125 unsigned long num_rsrc_err, num_cmd_success;
549e55cd 126 int i;
92d7f7b0
JS
127
128 num_rsrc_err = atomic_read(&phba->num_rsrc_err);
129 num_cmd_success = atomic_read(&phba->num_cmd_success);
130
549e55cd
JS
131 vports = lpfc_create_vport_work_array(phba);
132 if (vports != NULL)
133 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
134 shost = lpfc_shost_from_vport(vports[i]);
135 shost_for_each_device(sdev, shost) {
92d7f7b0 136 new_queue_depth =
549e55cd
JS
137 sdev->queue_depth * num_rsrc_err /
138 (num_rsrc_err + num_cmd_success);
139 if (!new_queue_depth)
140 new_queue_depth = sdev->queue_depth - 1;
141 else
142 new_queue_depth = sdev->queue_depth -
143 new_queue_depth;
144 if (sdev->ordered_tags)
145 scsi_adjust_queue_depth(sdev,
146 MSG_ORDERED_TAG,
147 new_queue_depth);
148 else
149 scsi_adjust_queue_depth(sdev,
150 MSG_SIMPLE_TAG,
151 new_queue_depth);
152 }
92d7f7b0 153 }
549e55cd 154 lpfc_destroy_vport_work_array(vports);
92d7f7b0
JS
155 spin_unlock_irq(&phba->hbalock);
156 atomic_set(&phba->num_rsrc_err, 0);
157 atomic_set(&phba->num_cmd_success, 0);
158}
159
160void
161lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
162{
549e55cd
JS
163 struct lpfc_vport **vports;
164 struct Scsi_Host *shost;
92d7f7b0 165 struct scsi_device *sdev;
549e55cd
JS
166 int i;
167
168 vports = lpfc_create_vport_work_array(phba);
169 if (vports != NULL)
170 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
171 shost = lpfc_shost_from_vport(vports[i]);
172 shost_for_each_device(sdev, shost) {
173 if (sdev->ordered_tags)
174 scsi_adjust_queue_depth(sdev,
175 MSG_ORDERED_TAG,
176 sdev->queue_depth+1);
177 else
178 scsi_adjust_queue_depth(sdev,
179 MSG_SIMPLE_TAG,
180 sdev->queue_depth+1);
181 }
92d7f7b0 182 }
549e55cd 183 lpfc_destroy_vport_work_array(vports);
92d7f7b0
JS
184 atomic_set(&phba->num_rsrc_err, 0);
185 atomic_set(&phba->num_cmd_success, 0);
186}
187
dea3101e 188/*
189 * This routine allocates a scsi buffer, which contains all the necessary
190 * information needed to initiate a SCSI I/O. The non-DMAable buffer region
191 * contains information to build the IOCB. The DMAable region contains
192 * memory for the FCP CMND, FCP RSP, and the inital BPL. In addition to
193 * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
194 * and the BPL BDE is setup in the IOCB.
195 */
196static struct lpfc_scsi_buf *
2e0fef85 197lpfc_new_scsi_buf(struct lpfc_vport *vport)
dea3101e 198{
2e0fef85 199 struct lpfc_hba *phba = vport->phba;
dea3101e 200 struct lpfc_scsi_buf *psb;
201 struct ulp_bde64 *bpl;
202 IOCB_t *iocb;
203 dma_addr_t pdma_phys;
604a3e30 204 uint16_t iotag;
dea3101e 205
206 psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
207 if (!psb)
208 return NULL;
209 memset(psb, 0, sizeof (struct lpfc_scsi_buf));
dea3101e 210
211 /*
212 * Get memory from the pci pool to map the virt space to pci bus space
213 * for an I/O. The DMA buffer includes space for the struct fcp_cmnd,
214 * struct fcp_rsp and the number of bde's necessary to support the
215 * sg_tablesize.
216 */
217 psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
218 &psb->dma_handle);
219 if (!psb->data) {
220 kfree(psb);
221 return NULL;
222 }
223
224 /* Initialize virtual ptrs to dma_buf region. */
225 memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
226
604a3e30
JB
227 /* Allocate iotag for psb->cur_iocbq. */
228 iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
229 if (iotag == 0) {
230 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
231 psb->data, psb->dma_handle);
232 kfree (psb);
233 return NULL;
234 }
0bd4ca25 235 psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
604a3e30 236
dea3101e 237 psb->fcp_cmnd = psb->data;
238 psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
239 psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
240 sizeof(struct fcp_rsp);
241
242 /* Initialize local short-hand pointers. */
243 bpl = psb->fcp_bpl;
244 pdma_phys = psb->dma_handle;
245
246 /*
247 * The first two bdes are the FCP_CMD and FCP_RSP. The balance are sg
248 * list bdes. Initialize the first two and leave the rest for
249 * queuecommand.
250 */
251 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
252 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
253 bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
254 bpl->tus.f.bdeFlags = BUFF_USE_CMND;
255 bpl->tus.w = le32_to_cpu(bpl->tus.w);
256 bpl++;
257
258 /* Setup the physical region for the FCP RSP */
259 pdma_phys += sizeof (struct fcp_cmnd);
260 bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
261 bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
262 bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
263 bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
264 bpl->tus.w = le32_to_cpu(bpl->tus.w);
265
266 /*
267 * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
268 * initialize it with all known data now.
269 */
270 pdma_phys += (sizeof (struct fcp_rsp));
271 iocb = &psb->cur_iocbq.iocb;
272 iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
273 iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
274 iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
275 iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
276 iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
277 iocb->ulpBdeCount = 1;
278 iocb->ulpClass = CLASS3;
279
280 return psb;
281}
282
455c53ec 283static struct lpfc_scsi_buf*
875fbdfe 284lpfc_get_scsi_buf(struct lpfc_hba * phba)
dea3101e 285{
0bd4ca25
JSEC
286 struct lpfc_scsi_buf * lpfc_cmd = NULL;
287 struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
875fbdfe 288 unsigned long iflag = 0;
0bd4ca25 289
875fbdfe 290 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 291 list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1dcb58e5
JS
292 if (lpfc_cmd) {
293 lpfc_cmd->seg_cnt = 0;
294 lpfc_cmd->nonsg_phys = 0;
295 }
875fbdfe 296 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
0bd4ca25
JSEC
297 return lpfc_cmd;
298}
dea3101e 299
0bd4ca25 300static void
92d7f7b0 301lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
0bd4ca25 302{
875fbdfe 303 unsigned long iflag = 0;
dea3101e 304
875fbdfe 305 spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
0bd4ca25 306 psb->pCmd = NULL;
dea3101e 307 list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
875fbdfe 308 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
dea3101e 309}
310
311static int
92d7f7b0 312lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
dea3101e 313{
314 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
315 struct scatterlist *sgel = NULL;
316 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
317 struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
318 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
319 dma_addr_t physaddr;
320 uint32_t i, num_bde = 0;
a0b4f78f 321 int nseg, datadir = scsi_cmnd->sc_data_direction;
dea3101e 322
323 /*
324 * There are three possibilities here - use scatter-gather segment, use
325 * the single mapping, or neither. Start the lpfc command prep by
326 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
327 * data bde entry.
328 */
329 bpl += 2;
c59fd9eb 330 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 331 /*
332 * The driver stores the segment count returned from pci_map_sg
333 * because this a count of dma-mappings used to map the use_sg
334 * pages. They are not guaranteed to be the same for those
335 * architectures that implement an IOMMU.
336 */
dea3101e 337
c59fd9eb
FT
338 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
339 scsi_sg_count(scsi_cmnd), datadir);
340 if (unlikely(!nseg))
341 return 1;
342
a0b4f78f 343 lpfc_cmd->seg_cnt = nseg;
dea3101e 344 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
345 printk(KERN_ERR "%s: Too many sg segments from "
346 "dma_map_sg. Config %d, seg_cnt %d",
347 __FUNCTION__, phba->cfg_sg_seg_cnt,
348 lpfc_cmd->seg_cnt);
a0b4f78f 349 scsi_dma_unmap(scsi_cmnd);
dea3101e 350 return 1;
351 }
352
353 /*
354 * The driver established a maximum scatter-gather segment count
355 * during probe that limits the number of sg elements in any
356 * single scsi command. Just run through the seg_cnt and format
357 * the bde's.
358 */
a0b4f78f 359 scsi_for_each_sg(scsi_cmnd, sgel, nseg, i) {
dea3101e 360 physaddr = sg_dma_address(sgel);
361 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
362 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
363 bpl->tus.f.bdeSize = sg_dma_len(sgel);
364 if (datadir == DMA_TO_DEVICE)
365 bpl->tus.f.bdeFlags = 0;
366 else
367 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
368 bpl->tus.w = le32_to_cpu(bpl->tus.w);
369 bpl++;
dea3101e 370 num_bde++;
371 }
c59fd9eb 372 }
dea3101e 373
374 /*
375 * Finish initializing those IOCB fields that are dependent on the
483f05f0
JSEC
376 * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
377 * reinitialized since all iocb memory resources are used many times
378 * for transmit, receive, and continuation bpl's.
dea3101e 379 */
483f05f0 380 iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
dea3101e 381 iocb_cmd->un.fcpi64.bdl.bdeSize +=
382 (num_bde * sizeof (struct ulp_bde64));
383 iocb_cmd->ulpBdeCount = 1;
384 iocb_cmd->ulpLe = 1;
a0b4f78f 385 fcp_cmnd->fcpDl = be32_to_cpu(scsi_bufflen(scsi_cmnd));
dea3101e 386 return 0;
387}
388
bcf4dbfa
JS
389static void
390lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
391{
392 /*
393 * There are only two special cases to consider. (1) the scsi command
394 * requested scatter-gather usage or (2) the scsi command allocated
395 * a request buffer, but did not request use_sg. There is a third
396 * case, but it does not require resource deallocation.
397 */
a0b4f78f
FT
398 if (psb->seg_cnt > 0)
399 scsi_dma_unmap(psb->pCmd);
bcf4dbfa
JS
400}
401
dea3101e 402static void
2e0fef85
JS
403lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
404 struct lpfc_iocbq *rsp_iocb)
dea3101e 405{
406 struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
407 struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
408 struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
7054a606 409 uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
dea3101e 410 uint32_t resp_info = fcprsp->rspStatus2;
411 uint32_t scsi_status = fcprsp->rspStatus3;
c7743956 412 uint32_t *lp;
dea3101e 413 uint32_t host_status = DID_OK;
414 uint32_t rsplen = 0;
c7743956 415 uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
dea3101e 416
417 /*
418 * If this is a task management command, there is no
419 * scsi packet associated with this lpfc_cmd. The driver
420 * consumes it.
421 */
422 if (fcpcmd->fcpCntl2) {
423 scsi_status = 0;
424 goto out;
425 }
426
c7743956
JS
427 if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
428 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
429 if (snslen > SCSI_SENSE_BUFFERSIZE)
430 snslen = SCSI_SENSE_BUFFERSIZE;
431
432 if (resp_info & RSP_LEN_VALID)
433 rsplen = be32_to_cpu(fcprsp->rspRspLen);
434 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
435 }
436 lp = (uint32_t *)cmnd->sense_buffer;
437
438 if (!scsi_status && (resp_info & RESID_UNDER))
439 logit = LOG_FCP;
440
e8b62011
JS
441 lpfc_printf_vlog(vport, KERN_WARNING, logit,
442 "0730 FCP command x%x failed: x%x SNS x%x x%x "
443 "Data: x%x x%x x%x x%x x%x\n",
444 cmnd->cmnd[0], scsi_status,
445 be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
446 be32_to_cpu(fcprsp->rspResId),
447 be32_to_cpu(fcprsp->rspSnsLen),
448 be32_to_cpu(fcprsp->rspRspLen),
449 fcprsp->rspInfo3);
dea3101e 450
451 if (resp_info & RSP_LEN_VALID) {
452 rsplen = be32_to_cpu(fcprsp->rspRspLen);
453 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
454 (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
455 host_status = DID_ERROR;
456 goto out;
457 }
458 }
459
a0b4f78f 460 scsi_set_resid(cmnd, 0);
dea3101e 461 if (resp_info & RESID_UNDER) {
a0b4f78f 462 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
dea3101e 463
e8b62011
JS
464 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
465 "0716 FCP Read Underrun, expected %d, "
466 "residual %d Data: x%x x%x x%x\n",
467 be32_to_cpu(fcpcmd->fcpDl),
468 scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
469 cmnd->underflow);
dea3101e 470
7054a606
JS
471 /*
472 * If there is an under run check if under run reported by
473 * storage array is same as the under run reported by HBA.
474 * If this is not same, there is a dropped frame.
475 */
476 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
477 fcpi_parm &&
a0b4f78f 478 (scsi_get_resid(cmnd) != fcpi_parm)) {
e8b62011
JS
479 lpfc_printf_vlog(vport, KERN_WARNING,
480 LOG_FCP | LOG_FCP_ERROR,
481 "0735 FCP Read Check Error "
482 "and Underrun Data: x%x x%x x%x x%x\n",
483 be32_to_cpu(fcpcmd->fcpDl),
484 scsi_get_resid(cmnd), fcpi_parm,
485 cmnd->cmnd[0]);
a0b4f78f 486 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
7054a606
JS
487 host_status = DID_ERROR;
488 }
dea3101e 489 /*
490 * The cmnd->underflow is the minimum number of bytes that must
491 * be transfered for this command. Provided a sense condition
492 * is not present, make sure the actual amount transferred is at
493 * least the underflow value or fail.
494 */
495 if (!(resp_info & SNS_LEN_VALID) &&
496 (scsi_status == SAM_STAT_GOOD) &&
a0b4f78f
FT
497 (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
498 < cmnd->underflow)) {
e8b62011
JS
499 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
500 "0717 FCP command x%x residual "
501 "underrun converted to error "
502 "Data: x%x x%x x%x\n",
503 cmnd->cmnd[0], cmnd->request_bufflen,
504 scsi_get_resid(cmnd), cmnd->underflow);
dea3101e 505 host_status = DID_ERROR;
506 }
507 } else if (resp_info & RESID_OVER) {
e8b62011
JS
508 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
509 "0720 FCP command x%x residual overrun error. "
510 "Data: x%x x%x \n", cmnd->cmnd[0],
511 scsi_bufflen(cmnd), scsi_get_resid(cmnd));
dea3101e 512 host_status = DID_ERROR;
513
514 /*
515 * Check SLI validation that all the transfer was actually done
516 * (fcpi_parm should be zero). Apply check only to reads.
517 */
518 } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
519 (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
e8b62011
JS
520 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
521 "0734 FCP Read Check Error Data: "
522 "x%x x%x x%x x%x\n",
523 be32_to_cpu(fcpcmd->fcpDl),
524 be32_to_cpu(fcprsp->rspResId),
525 fcpi_parm, cmnd->cmnd[0]);
dea3101e 526 host_status = DID_ERROR;
a0b4f78f 527 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
dea3101e 528 }
529
530 out:
531 cmnd->result = ScsiResult(host_status, scsi_status);
532}
533
534static void
535lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
536 struct lpfc_iocbq *pIocbOut)
537{
538 struct lpfc_scsi_buf *lpfc_cmd =
539 (struct lpfc_scsi_buf *) pIocbIn->context1;
2e0fef85 540 struct lpfc_vport *vport = pIocbIn->vport;
dea3101e 541 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
542 struct lpfc_nodelist *pnode = rdata->pnode;
543 struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
445cf4f4
JSEC
544 int result;
545 struct scsi_device *sdev, *tmp_sdev;
546 int depth = 0;
dea3101e 547
548 lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
549 lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
550
551 if (lpfc_cmd->status) {
552 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
553 (lpfc_cmd->result & IOERR_DRVR_MASK))
554 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
555 else if (lpfc_cmd->status >= IOSTAT_CNT)
556 lpfc_cmd->status = IOSTAT_DEFAULT;
557
e8b62011
JS
558 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
559 "0729 FCP cmd x%x failed <%d/%d> "
560 "status: x%x result: x%x Data: x%x x%x\n",
561 cmd->cmnd[0],
562 cmd->device ? cmd->device->id : 0xffff,
563 cmd->device ? cmd->device->lun : 0xffff,
564 lpfc_cmd->status, lpfc_cmd->result,
565 pIocbOut->iocb.ulpContext,
566 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
dea3101e 567
568 switch (lpfc_cmd->status) {
569 case IOSTAT_FCP_RSP_ERROR:
570 /* Call FCP RSP handler to determine result */
2e0fef85 571 lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
dea3101e 572 break;
573 case IOSTAT_NPORT_BSY:
574 case IOSTAT_FABRIC_BSY:
575 cmd->result = ScsiResult(DID_BUS_BUSY, 0);
576 break;
92d7f7b0
JS
577 case IOSTAT_LOCAL_REJECT:
578 if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
579 lpfc_cmd->result == IOERR_NO_RESOURCES ||
580 lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
581 cmd->result = ScsiResult(DID_REQUEUE, 0);
582 break;
583 } /* else: fall through */
dea3101e 584 default:
585 cmd->result = ScsiResult(DID_ERROR, 0);
586 break;
587 }
588
19a7b4ae
JSEC
589 if ((pnode == NULL )
590 || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
591 cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
dea3101e 592 } else {
593 cmd->result = ScsiResult(DID_OK, 0);
594 }
595
596 if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
597 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
598
e8b62011
JS
599 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
600 "0710 Iodone <%d/%d> cmd %p, error "
601 "x%x SNS x%x x%x Data: x%x x%x\n",
602 cmd->device->id, cmd->device->lun, cmd,
603 cmd->result, *lp, *(lp + 3), cmd->retries,
604 scsi_get_resid(cmd));
dea3101e 605 }
606
445cf4f4
JSEC
607 result = cmd->result;
608 sdev = cmd->device;
1dcb58e5 609 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
0bd4ca25
JSEC
610 cmd->scsi_done(cmd);
611
b808608b
JW
612 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
613 lpfc_release_scsi_buf(phba, lpfc_cmd);
614 return;
615 }
616
92d7f7b0
JS
617
618 if (!result)
3de2a653 619 lpfc_rampup_queue_depth(vport, sdev);
92d7f7b0 620
0c6ac8ef 621 if (!result && pnode != NULL &&
445cf4f4
JSEC
622 ((jiffies - pnode->last_ramp_up_time) >
623 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
624 ((jiffies - pnode->last_q_full_time) >
625 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
3de2a653 626 (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
445cf4f4 627 shost_for_each_device(tmp_sdev, sdev->host) {
3de2a653 628 if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
445cf4f4
JSEC
629 if (tmp_sdev->id != sdev->id)
630 continue;
631 if (tmp_sdev->ordered_tags)
632 scsi_adjust_queue_depth(tmp_sdev,
633 MSG_ORDERED_TAG,
634 tmp_sdev->queue_depth+1);
635 else
636 scsi_adjust_queue_depth(tmp_sdev,
637 MSG_SIMPLE_TAG,
638 tmp_sdev->queue_depth+1);
639
640 pnode->last_ramp_up_time = jiffies;
641 }
642 }
643 }
644
645 /*
646 * Check for queue full. If the lun is reporting queue full, then
647 * back off the lun queue depth to prevent target overloads.
648 */
0c6ac8ef 649 if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
445cf4f4
JSEC
650 pnode->last_q_full_time = jiffies;
651
652 shost_for_each_device(tmp_sdev, sdev->host) {
653 if (tmp_sdev->id != sdev->id)
654 continue;
655 depth = scsi_track_queue_full(tmp_sdev,
656 tmp_sdev->queue_depth - 1);
657 }
658 /*
2e0fef85 659 * The queue depth cannot be lowered any more.
445cf4f4
JSEC
660 * Modify the returned error code to store
661 * the final depth value set by
662 * scsi_track_queue_full.
663 */
664 if (depth == -1)
665 depth = sdev->host->cmd_per_lun;
666
667 if (depth) {
e8b62011
JS
668 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
669 "0711 detected queue full - lun queue "
670 "depth adjusted to %d.\n", depth);
445cf4f4
JSEC
671 }
672 }
673
0bd4ca25 674 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 675}
676
677static void
2e0fef85
JS
678lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
679 struct lpfc_nodelist *pnode)
dea3101e 680{
2e0fef85 681 struct lpfc_hba *phba = vport->phba;
dea3101e 682 struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
683 struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
684 IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
685 struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
686 int datadir = scsi_cmnd->sc_data_direction;
687
688 lpfc_cmd->fcp_rsp->rspSnsLen = 0;
69859dc4
JSEC
689 /* clear task management bits */
690 lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
dea3101e 691
91886523
JSEC
692 int_to_scsilun(lpfc_cmd->pCmd->device->lun,
693 &lpfc_cmd->fcp_cmnd->fcp_lun);
dea3101e 694
695 memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
696
697 if (scsi_cmnd->device->tagged_supported) {
698 switch (scsi_cmnd->tag) {
699 case HEAD_OF_QUEUE_TAG:
700 fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
701 break;
702 case ORDERED_QUEUE_TAG:
703 fcp_cmnd->fcpCntl1 = ORDERED_Q;
704 break;
705 default:
706 fcp_cmnd->fcpCntl1 = SIMPLE_Q;
707 break;
708 }
709 } else
710 fcp_cmnd->fcpCntl1 = 0;
711
712 /*
713 * There are three possibilities here - use scatter-gather segment, use
714 * the single mapping, or neither. Start the lpfc command prep by
715 * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
716 * data bde entry.
717 */
a0b4f78f 718 if (scsi_sg_count(scsi_cmnd)) {
dea3101e 719 if (datadir == DMA_TO_DEVICE) {
720 iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
721 iocb_cmd->un.fcpi.fcpi_parm = 0;
722 iocb_cmd->ulpPU = 0;
723 fcp_cmnd->fcpCntl3 = WRITE_DATA;
724 phba->fc4OutputRequests++;
725 } else {
726 iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
727 iocb_cmd->ulpPU = PARM_READ_CHECK;
a0b4f78f 728 iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
dea3101e 729 fcp_cmnd->fcpCntl3 = READ_DATA;
730 phba->fc4InputRequests++;
731 }
732 } else {
733 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
734 iocb_cmd->un.fcpi.fcpi_parm = 0;
735 iocb_cmd->ulpPU = 0;
736 fcp_cmnd->fcpCntl3 = 0;
737 phba->fc4ControlRequests++;
738 }
739
740 /*
741 * Finish initializing those IOCB fields that are independent
742 * of the scsi_cmnd request_buffer
743 */
744 piocbq->iocb.ulpContext = pnode->nlp_rpi;
745 if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
746 piocbq->iocb.ulpFCP2Rcvy = 1;
747
748 piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
749 piocbq->context1 = lpfc_cmd;
750 piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
751 piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
2e0fef85 752 piocbq->vport = vport;
dea3101e 753}
754
755static int
2e0fef85 756lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
dea3101e 757 struct lpfc_scsi_buf *lpfc_cmd,
420b630d 758 unsigned int lun,
dea3101e 759 uint8_t task_mgmt_cmd)
760{
dea3101e 761 struct lpfc_iocbq *piocbq;
762 IOCB_t *piocb;
763 struct fcp_cmnd *fcp_cmnd;
0b18ac42 764 struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
dea3101e 765 struct lpfc_nodelist *ndlp = rdata->pnode;
766
19a7b4ae 767 if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
dea3101e 768 return 0;
769 }
770
dea3101e 771 piocbq = &(lpfc_cmd->cur_iocbq);
2e0fef85
JS
772 piocbq->vport = vport;
773
dea3101e 774 piocb = &piocbq->iocb;
775
776 fcp_cmnd = lpfc_cmd->fcp_cmnd;
420b630d 777 int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
dea3101e 778 fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
779
780 piocb->ulpCommand = CMD_FCP_ICMND64_CR;
781
782 piocb->ulpContext = ndlp->nlp_rpi;
783 if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
784 piocb->ulpFCP2Rcvy = 1;
785 }
786 piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
787
788 /* ulpTimeout is only one byte */
789 if (lpfc_cmd->timeout > 0xff) {
790 /*
791 * Do not timeout the command at the firmware level.
792 * The driver will provide the timeout mechanism.
793 */
794 piocb->ulpTimeout = 0;
795 } else {
796 piocb->ulpTimeout = lpfc_cmd->timeout;
797 }
798
2e0fef85 799 return 1;
dea3101e 800}
801
7054a606
JS
802static void
803lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
804 struct lpfc_iocbq *cmdiocbq,
805 struct lpfc_iocbq *rspiocbq)
806{
807 struct lpfc_scsi_buf *lpfc_cmd =
808 (struct lpfc_scsi_buf *) cmdiocbq->context1;
809 if (lpfc_cmd)
810 lpfc_release_scsi_buf(phba, lpfc_cmd);
811 return;
812}
813
dea3101e 814static int
2e0fef85 815lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
420b630d
JS
816 unsigned tgt_id, unsigned int lun,
817 struct lpfc_rport_data *rdata)
dea3101e 818{
2e0fef85 819 struct lpfc_hba *phba = vport->phba;
dea3101e 820 struct lpfc_iocbq *iocbq;
0bd4ca25 821 struct lpfc_iocbq *iocbqrsp;
dea3101e 822 int ret;
823
f5603511
JS
824 if (!rdata->pnode)
825 return FAILED;
826
0b18ac42 827 lpfc_cmd->rdata = rdata;
2e0fef85 828 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
420b630d 829 FCP_TARGET_RESET);
dea3101e 830 if (!ret)
831 return FAILED;
832
dea3101e 833 iocbq = &lpfc_cmd->cur_iocbq;
0bd4ca25
JSEC
834 iocbqrsp = lpfc_sli_get_iocbq(phba);
835
dea3101e 836 if (!iocbqrsp)
837 return FAILED;
dea3101e 838
0b18ac42 839 /* Issue Target Reset to TGT <num> */
e8b62011
JS
840 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
841 "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
842 tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
68876920
JSEC
843 ret = lpfc_sli_issue_iocb_wait(phba,
844 &phba->sli.ring[phba->sli.fcp_ring],
845 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea3101e 846 if (ret != IOCB_SUCCESS) {
7054a606
JS
847 if (ret == IOCB_TIMEDOUT)
848 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
dea3101e 849 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
dea3101e 850 } else {
851 ret = SUCCESS;
852 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
853 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
854 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
855 (lpfc_cmd->result & IOERR_DRVR_MASK))
856 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
857 }
858
604a3e30 859 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea3101e 860 return ret;
861}
862
dea3101e 863const char *
864lpfc_info(struct Scsi_Host *host)
865{
2e0fef85
JS
866 struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
867 struct lpfc_hba *phba = vport->phba;
dea3101e 868 int len;
869 static char lpfcinfobuf[384];
870
871 memset(lpfcinfobuf,0,384);
872 if (phba && phba->pcidev){
873 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
874 len = strlen(lpfcinfobuf);
875 snprintf(lpfcinfobuf + len,
876 384-len,
877 " on PCI bus %02x device %02x irq %d",
878 phba->pcidev->bus->number,
879 phba->pcidev->devfn,
880 phba->pcidev->irq);
881 len = strlen(lpfcinfobuf);
882 if (phba->Port[0]) {
883 snprintf(lpfcinfobuf + len,
884 384-len,
885 " port %s",
886 phba->Port);
887 }
888 }
889 return lpfcinfobuf;
890}
891
875fbdfe
JSEC
892static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
893{
894 unsigned long poll_tmo_expires =
895 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
896
897 if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
898 mod_timer(&phba->fcp_poll_timer,
899 poll_tmo_expires);
900}
901
902void lpfc_poll_start_timer(struct lpfc_hba * phba)
903{
904 lpfc_poll_rearm_timer(phba);
905}
906
907void lpfc_poll_timeout(unsigned long ptr)
908{
2e0fef85 909 struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
875fbdfe
JSEC
910
911 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
912 lpfc_sli_poll_fcp_ring (phba);
913 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
914 lpfc_poll_rearm_timer(phba);
915 }
875fbdfe
JSEC
916}
917
dea3101e 918static int
919lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
920{
2e0fef85
JS
921 struct Scsi_Host *shost = cmnd->device->host;
922 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
923 struct lpfc_hba *phba = vport->phba;
924 struct lpfc_sli *psli = &phba->sli;
dea3101e 925 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
926 struct lpfc_nodelist *ndlp = rdata->pnode;
0bd4ca25 927 struct lpfc_scsi_buf *lpfc_cmd;
19a7b4ae 928 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
19a7b4ae 929 int err;
dea3101e 930
19a7b4ae
JSEC
931 err = fc_remote_port_chkready(rport);
932 if (err) {
933 cmnd->result = err;
dea3101e 934 goto out_fail_command;
935 }
936
937 /*
19a7b4ae
JSEC
938 * Catch race where our node has transitioned, but the
939 * transport is still transitioning.
dea3101e 940 */
19a7b4ae
JSEC
941 if (!ndlp) {
942 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
943 goto out_fail_command;
dea3101e 944 }
ed957684 945 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea3101e 946 if (lpfc_cmd == NULL) {
92d7f7b0
JS
947 lpfc_adjust_queue_depth(phba);
948
e8b62011
JS
949 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
950 "0707 driver's buffer pool is empty, "
951 "IO busied\n");
dea3101e 952 goto out_host_busy;
953 }
954
955 /*
956 * Store the midlayer's command structure for the completion phase
957 * and complete the command initialization.
958 */
959 lpfc_cmd->pCmd = cmnd;
960 lpfc_cmd->rdata = rdata;
961 lpfc_cmd->timeout = 0;
962 cmnd->host_scribble = (unsigned char *)lpfc_cmd;
963 cmnd->scsi_done = done;
964
965 err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
966 if (err)
967 goto out_host_busy_free_buf;
968
2e0fef85 969 lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
dea3101e 970
971 err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
92d7f7b0 972 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
dea3101e 973 if (err)
974 goto out_host_busy_free_buf;
875fbdfe
JSEC
975
976 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
977 lpfc_sli_poll_fcp_ring(phba);
978 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
979 lpfc_poll_rearm_timer(phba);
980 }
981
dea3101e 982 return 0;
983
984 out_host_busy_free_buf:
bcf4dbfa 985 lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
0bd4ca25 986 lpfc_release_scsi_buf(phba, lpfc_cmd);
dea3101e 987 out_host_busy:
988 return SCSI_MLQUEUE_HOST_BUSY;
989
990 out_fail_command:
991 done(cmnd);
992 return 0;
993}
994
a90f5684
JS
995static void
996lpfc_block_error_handler(struct scsi_cmnd *cmnd)
997{
998 struct Scsi_Host *shost = cmnd->device->host;
999 struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1000
1001 spin_lock_irq(shost->host_lock);
1002 while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1003 spin_unlock_irq(shost->host_lock);
1004 msleep(1000);
1005 spin_lock_irq(shost->host_lock);
1006 }
1007 spin_unlock_irq(shost->host_lock);
1008 return;
1009}
63c59c3b 1010
dea3101e 1011static int
63c59c3b 1012lpfc_abort_handler(struct scsi_cmnd *cmnd)
dea3101e 1013{
2e0fef85
JS
1014 struct Scsi_Host *shost = cmnd->device->host;
1015 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1016 struct lpfc_hba *phba = vport->phba;
dea3101e 1017 struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
0bd4ca25
JSEC
1018 struct lpfc_iocbq *iocb;
1019 struct lpfc_iocbq *abtsiocb;
dea3101e 1020 struct lpfc_scsi_buf *lpfc_cmd;
dea3101e 1021 IOCB_t *cmd, *icmd;
dea3101e 1022 unsigned int loop_count = 0;
0bd4ca25 1023 int ret = SUCCESS;
dea3101e 1024
a90f5684 1025 lpfc_block_error_handler(cmnd);
0bd4ca25
JSEC
1026 lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1027 BUG_ON(!lpfc_cmd);
dea3101e 1028
0bd4ca25
JSEC
1029 /*
1030 * If pCmd field of the corresponding lpfc_scsi_buf structure
1031 * points to a different SCSI command, then the driver has
1032 * already completed this command, but the midlayer did not
1033 * see the completion before the eh fired. Just return
1034 * SUCCESS.
1035 */
1036 iocb = &lpfc_cmd->cur_iocbq;
1037 if (lpfc_cmd->pCmd != cmnd)
1038 goto out;
dea3101e 1039
0bd4ca25 1040 BUG_ON(iocb->context1 != lpfc_cmd);
dea3101e 1041
0bd4ca25
JSEC
1042 abtsiocb = lpfc_sli_get_iocbq(phba);
1043 if (abtsiocb == NULL) {
1044 ret = FAILED;
dea3101e 1045 goto out;
1046 }
1047
dea3101e 1048 /*
0bd4ca25
JSEC
1049 * The scsi command can not be in txq and it is in flight because the
1050 * pCmd is still pointig at the SCSI command we have to abort. There
1051 * is no need to search the txcmplq. Just send an abort to the FW.
dea3101e 1052 */
dea3101e 1053
0bd4ca25
JSEC
1054 cmd = &iocb->iocb;
1055 icmd = &abtsiocb->iocb;
1056 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1057 icmd->un.acxri.abortContextTag = cmd->ulpContext;
1058 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
dea3101e 1059
0bd4ca25
JSEC
1060 icmd->ulpLe = 1;
1061 icmd->ulpClass = cmd->ulpClass;
2e0fef85 1062 if (lpfc_is_link_up(phba))
0bd4ca25
JSEC
1063 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1064 else
1065 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
dea3101e 1066
0bd4ca25 1067 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
2e0fef85 1068 abtsiocb->vport = vport;
0bd4ca25
JSEC
1069 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1070 lpfc_sli_release_iocbq(phba, abtsiocb);
1071 ret = FAILED;
1072 goto out;
1073 }
dea3101e 1074
875fbdfe
JSEC
1075 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1076 lpfc_sli_poll_fcp_ring (phba);
1077
0bd4ca25
JSEC
1078 /* Wait for abort to complete */
1079 while (lpfc_cmd->pCmd == cmnd)
1080 {
875fbdfe
JSEC
1081 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1082 lpfc_sli_poll_fcp_ring (phba);
1083
2e0fef85 1084 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT * HZ);
0bd4ca25 1085 if (++loop_count
3de2a653 1086 > (2 * vport->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
0bd4ca25
JSEC
1087 break;
1088 }
dea3101e 1089
0bd4ca25
JSEC
1090 if (lpfc_cmd->pCmd == cmnd) {
1091 ret = FAILED;
e8b62011
JS
1092 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1093 "0748 abort handler timed out waiting "
1094 "for abort to complete: ret %#x, ID %d, "
1095 "LUN %d, snum %#lx\n",
1096 ret, cmnd->device->id, cmnd->device->lun,
1097 cmnd->serial_number);
dea3101e 1098 }
1099
1100 out:
e8b62011
JS
1101 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1102 "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
1103 "LUN %d snum %#lx\n", ret, cmnd->device->id,
1104 cmnd->device->lun, cmnd->serial_number);
63c59c3b 1105 return ret;
8fa728a2
JG
1106}
1107
dea3101e 1108static int
7054a606 1109lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
dea3101e 1110{
2e0fef85
JS
1111 struct Scsi_Host *shost = cmnd->device->host;
1112 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1113 struct lpfc_hba *phba = vport->phba;
0bd4ca25
JSEC
1114 struct lpfc_scsi_buf *lpfc_cmd;
1115 struct lpfc_iocbq *iocbq, *iocbqrsp;
dea3101e 1116 struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1117 struct lpfc_nodelist *pnode = rdata->pnode;
6175c02a 1118 uint32_t cmd_result = 0, cmd_status = 0;
dea3101e 1119 int ret = FAILED;
7054a606 1120 int iocb_status = IOCB_SUCCESS;
dea3101e 1121 int cnt, loopcnt;
1122
a90f5684 1123 lpfc_block_error_handler(cmnd);
f5603511 1124 loopcnt = 0;
dea3101e 1125 /*
1126 * If target is not in a MAPPED state, delay the reset until
c01f3208 1127 * target is rediscovered or devloss timeout expires.
dea3101e 1128 */
92d7f7b0 1129 while (1) {
dea3101e 1130 if (!pnode)
7054a606 1131 goto out;
dea3101e 1132
1133 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
a9a3047d 1134 schedule_timeout_uninterruptible(msecs_to_jiffies(500));
f5603511
JS
1135 loopcnt++;
1136 rdata = cmnd->device->hostdata;
1137 if (!rdata ||
3de2a653 1138 (loopcnt > ((vport->cfg_devloss_tmo * 2) + 1))){
e8b62011
JS
1139 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1140 "0721 LUN Reset rport "
1141 "failure: cnt x%x rdata x%p\n",
1142 loopcnt, rdata);
f5603511
JS
1143 goto out;
1144 }
1145 pnode = rdata->pnode;
1146 if (!pnode)
7054a606 1147 goto out;
dea3101e 1148 }
f5603511 1149 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
dea3101e 1150 break;
1151 }
1152
2e0fef85 1153 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea3101e 1154 if (lpfc_cmd == NULL)
1155 goto out;
1156
dea3101e 1157 lpfc_cmd->timeout = 60;
0b18ac42 1158 lpfc_cmd->rdata = rdata;
dea3101e 1159
2e0fef85 1160 ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
7054a606 1161 FCP_TARGET_RESET);
dea3101e 1162 if (!ret)
1163 goto out_free_scsi_buf;
1164
1165 iocbq = &lpfc_cmd->cur_iocbq;
1166
1167 /* get a buffer for this IOCB command response */
0bd4ca25 1168 iocbqrsp = lpfc_sli_get_iocbq(phba);
dea3101e 1169 if (iocbqrsp == NULL)
1170 goto out_free_scsi_buf;
1171
e8b62011
JS
1172 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1173 "0703 Issue target reset to TGT %d LUN %d "
1174 "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1175 cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
7054a606 1176 iocb_status = lpfc_sli_issue_iocb_wait(phba,
68876920
JSEC
1177 &phba->sli.ring[phba->sli.fcp_ring],
1178 iocbq, iocbqrsp, lpfc_cmd->timeout);
dea3101e 1179
7054a606
JS
1180 if (iocb_status == IOCB_TIMEDOUT)
1181 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
1182
1183 if (iocb_status == IOCB_SUCCESS)
1184 ret = SUCCESS;
1185 else
1186 ret = iocb_status;
6175c02a
JSEC
1187
1188 cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1189 cmd_status = iocbqrsp->iocb.ulpStatus;
1190
1191 lpfc_sli_release_iocbq(phba, iocbqrsp);
dea3101e 1192
1193 /*
6175c02a 1194 * All outstanding txcmplq I/Os should have been aborted by the device.
dea3101e 1195 * Unfortunately, some targets do not abide by this forcing the driver
1196 * to double check.
1197 */
6175c02a
JSEC
1198 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1199 cmnd->device->id, cmnd->device->lun,
1200 LPFC_CTX_LUN);
1201 if (cnt)
1202 lpfc_sli_abort_iocb(phba,
1203 &phba->sli.ring[phba->sli.fcp_ring],
1204 cmnd->device->id, cmnd->device->lun,
1205 0, LPFC_CTX_LUN);
dea3101e 1206 loopcnt = 0;
92d7f7b0 1207 while(cnt) {
a9a3047d 1208 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea3101e 1209
1210 if (++loopcnt
3de2a653 1211 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea3101e 1212 break;
6175c02a
JSEC
1213
1214 cnt = lpfc_sli_sum_iocb(phba,
1215 &phba->sli.ring[phba->sli.fcp_ring],
1216 cmnd->device->id, cmnd->device->lun,
1217 LPFC_CTX_LUN);
dea3101e 1218 }
1219
1220 if (cnt) {
e8b62011
JS
1221 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1222 "0719 device reset I/O flush failure: "
1223 "cnt x%x\n", cnt);
0bd4ca25 1224 ret = FAILED;
dea3101e 1225 }
1226
dea3101e 1227out_free_scsi_buf:
7054a606
JS
1228 if (iocb_status != IOCB_TIMEDOUT) {
1229 lpfc_release_scsi_buf(phba, lpfc_cmd);
1230 }
e8b62011
JS
1231 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1232 "0713 SCSI layer issued device reset (%d, %d) "
1233 "return x%x status x%x result x%x\n",
1234 cmnd->device->id, cmnd->device->lun, ret,
1235 cmd_status, cmd_result);
dea3101e 1236out:
1237 return ret;
1238}
1239
94d0e7b8 1240static int
7054a606 1241lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
dea3101e 1242{
2e0fef85
JS
1243 struct Scsi_Host *shost = cmnd->device->host;
1244 struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1245 struct lpfc_hba *phba = vport->phba;
dea3101e 1246 struct lpfc_nodelist *ndlp = NULL;
1247 int match;
1248 int ret = FAILED, i, err_count = 0;
1249 int cnt, loopcnt;
0bd4ca25 1250 struct lpfc_scsi_buf * lpfc_cmd;
dea3101e 1251
a90f5684 1252 lpfc_block_error_handler(cmnd);
63c59c3b 1253
875fbdfe 1254 lpfc_cmd = lpfc_get_scsi_buf(phba);
dea3101e 1255 if (lpfc_cmd == NULL)
1256 goto out;
1257
1258 /* The lpfc_cmd storage is reused. Set all loop invariants. */
1259 lpfc_cmd->timeout = 60;
dea3101e 1260
1261 /*
1262 * Since the driver manages a single bus device, reset all
1263 * targets known to the driver. Should any target reset
1264 * fail, this routine returns failure to the midlayer.
1265 */
e17da18e 1266 for (i = 0; i < LPFC_MAX_TARGET; i++) {
685f0bf7 1267 /* Search for mapped node by target ID */
dea3101e 1268 match = 0;
2e0fef85
JS
1269 spin_lock_irq(shost->host_lock);
1270 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
685f0bf7
JS
1271 if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1272 i == ndlp->nlp_sid &&
1273 ndlp->rport) {
dea3101e 1274 match = 1;
1275 break;
1276 }
1277 }
2e0fef85 1278 spin_unlock_irq(shost->host_lock);
dea3101e 1279 if (!match)
1280 continue;
1281
2e0fef85
JS
1282 ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1283 cmnd->device->lun,
420b630d 1284 ndlp->rport->dd_data);
dea3101e 1285 if (ret != SUCCESS) {
e8b62011
JS
1286 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1287 "0700 Bus Reset on target %d failed\n",
1288 i);
dea3101e 1289 err_count++;
7054a606 1290 break;
dea3101e 1291 }
1292 }
1293
7054a606
JS
1294 if (ret != IOCB_TIMEDOUT)
1295 lpfc_release_scsi_buf(phba, lpfc_cmd);
1296
6175c02a
JSEC
1297 if (err_count == 0)
1298 ret = SUCCESS;
7054a606
JS
1299 else
1300 ret = FAILED;
6175c02a
JSEC
1301
1302 /*
1303 * All outstanding txcmplq I/Os should have been aborted by
1304 * the targets. Unfortunately, some targets do not abide by
1305 * this forcing the driver to double check.
1306 */
6175c02a
JSEC
1307 cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1308 0, 0, LPFC_CTX_HOST);
1309 if (cnt)
1310 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1311 0, 0, 0, LPFC_CTX_HOST);
dea3101e 1312 loopcnt = 0;
92d7f7b0 1313 while(cnt) {
a9a3047d 1314 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
dea3101e 1315
1316 if (++loopcnt
3de2a653 1317 > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
dea3101e 1318 break;
6175c02a
JSEC
1319
1320 cnt = lpfc_sli_sum_iocb(phba,
1321 &phba->sli.ring[phba->sli.fcp_ring],
1322 0, 0, LPFC_CTX_HOST);
dea3101e 1323 }
1324
1325 if (cnt) {
e8b62011
JS
1326 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1327 "0715 Bus Reset I/O flush failure: "
1328 "cnt x%x left x%x\n", cnt, i);
0bd4ca25 1329 ret = FAILED;
6175c02a 1330 }
dea3101e 1331
e8b62011
JS
1332 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1333 "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
dea3101e 1334out:
1335 return ret;
1336}
1337
1338static int
1339lpfc_slave_alloc(struct scsi_device *sdev)
1340{
2e0fef85
JS
1341 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1342 struct lpfc_hba *phba = vport->phba;
dea3101e 1343 struct lpfc_scsi_buf *scsi_buf = NULL;
19a7b4ae 1344 struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
dea3101e 1345 uint32_t total = 0, i;
1346 uint32_t num_to_alloc = 0;
1347 unsigned long flags;
dea3101e 1348
19a7b4ae 1349 if (!rport || fc_remote_port_chkready(rport))
dea3101e 1350 return -ENXIO;
1351
19a7b4ae 1352 sdev->hostdata = rport->dd_data;
dea3101e 1353
1354 /*
1355 * Populate the cmds_per_lun count scsi_bufs into this host's globally
1356 * available list of scsi buffers. Don't allocate more than the
a784efbf
JSEC
1357 * HBA limit conveyed to the midlayer via the host structure. The
1358 * formula accounts for the lun_queue_depth + error handlers + 1
1359 * extra. This list of scsi bufs exists for the lifetime of the driver.
dea3101e 1360 */
1361 total = phba->total_scsi_bufs;
3de2a653 1362 num_to_alloc = vport->cfg_lun_queue_depth + 2;
92d7f7b0
JS
1363
1364 /* Allow some exchanges to be available always to complete discovery */
1365 if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
1366 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1367 "0704 At limitation of %d preallocated "
1368 "command buffers\n", total);
dea3101e 1369 return 0;
92d7f7b0
JS
1370 /* Allow some exchanges to be available always to complete discovery */
1371 } else if (total + num_to_alloc >
1372 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
e8b62011
JS
1373 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1374 "0705 Allocation request of %d "
1375 "command buffers will exceed max of %d. "
1376 "Reducing allocation request to %d.\n",
1377 num_to_alloc, phba->cfg_hba_queue_depth,
1378 (phba->cfg_hba_queue_depth - total));
dea3101e 1379 num_to_alloc = phba->cfg_hba_queue_depth - total;
1380 }
1381
1382 for (i = 0; i < num_to_alloc; i++) {
2e0fef85 1383 scsi_buf = lpfc_new_scsi_buf(vport);
dea3101e 1384 if (!scsi_buf) {
e8b62011
JS
1385 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1386 "0706 Failed to allocate "
1387 "command buffer\n");
dea3101e 1388 break;
1389 }
1390
875fbdfe 1391 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
dea3101e 1392 phba->total_scsi_bufs++;
1393 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
875fbdfe 1394 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
dea3101e 1395 }
1396 return 0;
1397}
1398
1399static int
1400lpfc_slave_configure(struct scsi_device *sdev)
1401{
2e0fef85
JS
1402 struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1403 struct lpfc_hba *phba = vport->phba;
1404 struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
dea3101e 1405
1406 if (sdev->tagged_supported)
3de2a653 1407 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 1408 else
3de2a653 1409 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
dea3101e 1410
1411 /*
1412 * Initialize the fc transport attributes for the target
1413 * containing this scsi device. Also note that the driver's
1414 * target pointer is stored in the starget_data for the
1415 * driver's sysfs entry point functions.
1416 */
3de2a653 1417 rport->dev_loss_tmo = vport->cfg_devloss_tmo;
dea3101e 1418
875fbdfe
JSEC
1419 if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1420 lpfc_sli_poll_fcp_ring(phba);
1421 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1422 lpfc_poll_rearm_timer(phba);
1423 }
1424
dea3101e 1425 return 0;
1426}
1427
1428static void
1429lpfc_slave_destroy(struct scsi_device *sdev)
1430{
1431 sdev->hostdata = NULL;
1432 return;
1433}
1434
92d7f7b0 1435
dea3101e 1436struct scsi_host_template lpfc_template = {
1437 .module = THIS_MODULE,
1438 .name = LPFC_DRIVER_NAME,
1439 .info = lpfc_info,
1440 .queuecommand = lpfc_queuecommand,
1441 .eh_abort_handler = lpfc_abort_handler,
7054a606
JS
1442 .eh_device_reset_handler= lpfc_device_reset_handler,
1443 .eh_bus_reset_handler = lpfc_bus_reset_handler,
dea3101e 1444 .slave_alloc = lpfc_slave_alloc,
1445 .slave_configure = lpfc_slave_configure,
1446 .slave_destroy = lpfc_slave_destroy,
47a8617c 1447 .scan_finished = lpfc_scan_finished,
dea3101e 1448 .this_id = -1,
1449 .sg_tablesize = LPFC_SG_SEG_CNT,
1450 .cmd_per_lun = LPFC_CMD_PER_LUN,
1451 .use_clustering = ENABLE_CLUSTERING,
2e0fef85 1452 .shost_attrs = lpfc_hba_attrs,
564b2960 1453 .max_sectors = 0xFFFF,
dea3101e 1454};
3de2a653
JS
1455
1456struct scsi_host_template lpfc_vport_template = {
1457 .module = THIS_MODULE,
1458 .name = LPFC_DRIVER_NAME,
1459 .info = lpfc_info,
1460 .queuecommand = lpfc_queuecommand,
1461 .eh_abort_handler = lpfc_abort_handler,
1462 .eh_device_reset_handler= lpfc_device_reset_handler,
1463 .eh_bus_reset_handler = lpfc_bus_reset_handler,
1464 .slave_alloc = lpfc_slave_alloc,
1465 .slave_configure = lpfc_slave_configure,
1466 .slave_destroy = lpfc_slave_destroy,
1467 .scan_finished = lpfc_scan_finished,
1468 .this_id = -1,
1469 .sg_tablesize = LPFC_SG_SEG_CNT,
1470 .cmd_per_lun = LPFC_CMD_PER_LUN,
1471 .use_clustering = ENABLE_CLUSTERING,
1472 .shost_attrs = lpfc_vport_attrs,
1473 .max_sectors = 0xFFFF,
1474};
This page took 0.420244 seconds and 5 git commands to generate.