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