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