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