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