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