[SCSI] lpfc: Fix for "command completion for iotax x?? not found"
[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         iocbq->iocb_flag |= LPFC_IO_POLL;
641         ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
642                      &phba->sli.ring[phba->sli.fcp_ring],
643                      iocbq, SLI_IOCB_HIGH_PRIORITY,
644                      iocbqrsp,
645                      lpfc_cmd->timeout);
646         if (ret != IOCB_SUCCESS) {
647                 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
648                 ret = FAILED;
649         } else {
650                 ret = SUCCESS;
651                 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
652                 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
653                 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
654                         (lpfc_cmd->result & IOERR_DRVR_MASK))
655                                 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
656         }
657
658         /*
659          * All outstanding txcmplq I/Os should have been aborted by the target.
660          * Unfortunately, some targets do not abide by this forcing the driver
661          * to double check.
662          */
663         lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
664                             lpfc_cmd->pCmd->device->id,
665                             lpfc_cmd->pCmd->device->lun, 0, LPFC_CTX_TGT);
666
667         lpfc_sli_release_iocbq(phba, iocbqrsp);
668         return ret;
669 }
670
671 static void
672 lpfc_scsi_cmd_iocb_cleanup (struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
673                             struct lpfc_iocbq *pIocbOut)
674 {
675         unsigned long iflag;
676         struct lpfc_scsi_buf *lpfc_cmd =
677                 (struct lpfc_scsi_buf *) pIocbIn->context1;
678
679         spin_lock_irqsave(phba->host->host_lock, iflag);
680         lpfc_free_scsi_buf(lpfc_cmd);
681         spin_unlock_irqrestore(phba->host->host_lock, iflag);
682 }
683
684 static void
685 lpfc_scsi_cmd_iocb_cmpl_aborted(struct lpfc_hba *phba,
686                                 struct lpfc_iocbq *pIocbIn,
687                                 struct lpfc_iocbq *pIocbOut)
688 {
689         struct scsi_cmnd *ml_cmd =
690                 ((struct lpfc_scsi_buf *) pIocbIn->context1)->pCmd;
691
692         lpfc_scsi_cmd_iocb_cleanup (phba, pIocbIn, pIocbOut);
693         ml_cmd->host_scribble = NULL;
694 }
695
696 const char *
697 lpfc_info(struct Scsi_Host *host)
698 {
699         struct lpfc_hba    *phba = (struct lpfc_hba *) host->hostdata[0];
700         int len;
701         static char  lpfcinfobuf[384];
702
703         memset(lpfcinfobuf,0,384);
704         if (phba && phba->pcidev){
705                 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
706                 len = strlen(lpfcinfobuf);
707                 snprintf(lpfcinfobuf + len,
708                         384-len,
709                         " on PCI bus %02x device %02x irq %d",
710                         phba->pcidev->bus->number,
711                         phba->pcidev->devfn,
712                         phba->pcidev->irq);
713                 len = strlen(lpfcinfobuf);
714                 if (phba->Port[0]) {
715                         snprintf(lpfcinfobuf + len,
716                                  384-len,
717                                  " port %s",
718                                  phba->Port);
719                 }
720         }
721         return lpfcinfobuf;
722 }
723
724 static int
725 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
726 {
727         struct lpfc_hba *phba =
728                 (struct lpfc_hba *) cmnd->device->host->hostdata[0];
729         struct lpfc_sli *psli = &phba->sli;
730         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
731         struct lpfc_nodelist *ndlp = rdata->pnode;
732         struct lpfc_scsi_buf *lpfc_cmd = NULL;
733         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
734         struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
735         int err;
736
737         err = fc_remote_port_chkready(rport);
738         if (err) {
739                 cmnd->result = err;
740                 goto out_fail_command;
741         }
742
743         /*
744          * Catch race where our node has transitioned, but the
745          * transport is still transitioning.
746          */
747         if (!ndlp) {
748                 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
749                 goto out_fail_command;
750         }
751
752         list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
753         if (lpfc_cmd == NULL) {
754                 printk(KERN_WARNING "%s: No buffer available - list empty, "
755                        "total count %d\n", __FUNCTION__, phba->total_scsi_bufs);
756                 goto out_host_busy;
757         }
758
759         /*
760          * Store the midlayer's command structure for the completion phase
761          * and complete the command initialization.
762          */
763         lpfc_cmd->pCmd  = cmnd;
764         lpfc_cmd->rdata = rdata;
765         lpfc_cmd->timeout = 0;
766         cmnd->host_scribble = (unsigned char *)lpfc_cmd;
767         cmnd->scsi_done = done;
768
769         err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
770         if (err)
771                 goto out_host_busy_free_buf;
772
773         lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
774
775         err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
776                                 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
777         if (err)
778                 goto out_host_busy_free_buf;
779         return 0;
780
781  out_host_busy_free_buf:
782         lpfc_free_scsi_buf(lpfc_cmd);
783         cmnd->host_scribble = NULL;
784  out_host_busy:
785         return SCSI_MLQUEUE_HOST_BUSY;
786
787  out_fail_command:
788         done(cmnd);
789         return 0;
790 }
791
792 static int
793 __lpfc_abort_handler(struct scsi_cmnd *cmnd)
794 {
795         struct lpfc_hba *phba =
796                         (struct lpfc_hba *)cmnd->device->host->hostdata[0];
797         struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
798         struct lpfc_iocbq *iocb, *next_iocb;
799         struct lpfc_iocbq *abtsiocb = NULL;
800         struct lpfc_scsi_buf *lpfc_cmd;
801         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
802         IOCB_t *cmd, *icmd;
803         unsigned long snum;
804         unsigned int id, lun;
805         unsigned int loop_count = 0;
806         int ret = IOCB_SUCCESS;
807
808         /*
809          * If the host_scribble data area is NULL, then the driver has already
810          * completed this command, but the midlayer did not see the completion
811          * before the eh fired.  Just return SUCCESS.
812          */
813         lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
814         if (!lpfc_cmd)
815                 return SUCCESS;
816
817         /* save these now since lpfc_cmd can be freed */
818         id   = lpfc_cmd->pCmd->device->id;
819         lun  = lpfc_cmd->pCmd->device->lun;
820         snum = lpfc_cmd->pCmd->serial_number;
821
822         list_for_each_entry_safe(iocb, next_iocb, &pring->txq, list) {
823                 cmd = &iocb->iocb;
824                 if (iocb->context1 != lpfc_cmd)
825                         continue;
826
827                 list_del_init(&iocb->list);
828                 pring->txq_cnt--;
829                 if (!iocb->iocb_cmpl)
830                         lpfc_sli_release_iocbq(phba, iocb);
831                 else {
832                         cmd->ulpStatus = IOSTAT_LOCAL_REJECT;
833                         cmd->un.ulpWord[4] = IOERR_SLI_ABORTED;
834                         lpfc_scsi_cmd_iocb_cmpl_aborted(phba, iocb, iocb);
835                 }
836
837                 goto out;
838         }
839
840         list_remove_head(lpfc_iocb_list, abtsiocb, struct lpfc_iocbq, list);
841         if (abtsiocb == NULL)
842                 return FAILED;
843
844         /*
845          * The scsi command was not in the txq.  Check the txcmplq and if it is
846          * found, send an abort to the FW.
847          */
848         list_for_each_entry_safe(iocb, next_iocb, &pring->txcmplq, list) {
849                 if (iocb->context1 != lpfc_cmd)
850                         continue;
851
852                 iocb->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl_aborted;
853                 cmd = &iocb->iocb;
854                 icmd = &abtsiocb->iocb;
855                 icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
856                 icmd->un.acxri.abortContextTag = cmd->ulpContext;
857                 icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
858
859                 icmd->ulpLe = 1;
860                 icmd->ulpClass = cmd->ulpClass;
861                 if (phba->hba_state >= LPFC_LINK_UP)
862                         icmd->ulpCommand = CMD_ABORT_XRI_CN;
863                 else
864                         icmd->ulpCommand = CMD_CLOSE_XRI_CN;
865
866                 abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
867                 if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) ==
868                                                                 IOCB_ERROR) {
869                         lpfc_sli_release_iocbq(phba, abtsiocb);
870                         ret = IOCB_ERROR;
871                         break;
872                 }
873
874                 /* Wait for abort to complete */
875                 while (cmnd->host_scribble)
876                 {
877                         spin_unlock_irq(phba->host->host_lock);
878                         set_current_state(TASK_UNINTERRUPTIBLE);
879                         schedule_timeout(LPFC_ABORT_WAIT*HZ);
880                         spin_lock_irq(phba->host->host_lock);
881                         if (++loop_count
882                             > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
883                                 break;
884                 }
885
886                 if(cmnd->host_scribble) {
887                         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
888                                         "%d:0748 abort handler timed "
889                                         "out waiting for abort to "
890                                         "complete. Data: "
891                                         "x%x x%x x%x x%lx\n",
892                                         phba->brd_no, ret, id, lun, snum);
893                         cmnd->host_scribble = NULL;
894                         iocb->iocb_cmpl = lpfc_scsi_cmd_iocb_cleanup;
895                         ret = IOCB_ERROR;
896                 }
897
898                 break;
899         }
900
901  out:
902         lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
903                         "%d:0749 SCSI layer issued abort device "
904                         "Data: x%x x%x x%x x%lx\n",
905                         phba->brd_no, ret, id, lun, snum);
906
907         return ret == IOCB_SUCCESS ? SUCCESS : FAILED;
908 }
909
910 static int
911 lpfc_abort_handler(struct scsi_cmnd *cmnd)
912 {
913         int rc;
914         spin_lock_irq(cmnd->device->host->host_lock);
915         rc = __lpfc_abort_handler(cmnd);
916         spin_unlock_irq(cmnd->device->host->host_lock);
917         return rc;
918 }
919
920 static int
921 __lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
922 {
923         struct Scsi_Host *shost = cmnd->device->host;
924         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
925         struct lpfc_sli *psli = &phba->sli;
926         struct lpfc_scsi_buf *lpfc_cmd = NULL;
927         struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
928         struct list_head *lpfc_iocb_list = &phba->lpfc_iocb_list;
929         struct lpfc_iocbq *iocbq, *iocbqrsp = NULL;
930         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
931         struct lpfc_nodelist *pnode = rdata->pnode;
932         int ret = FAILED;
933         int cnt, loopcnt;
934
935         /*
936          * If target is not in a MAPPED state, delay the reset until
937          * target is rediscovered or nodev timeout expires.
938          */
939         while ( 1 ) {
940                 if (!pnode)
941                         break;
942
943                 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
944                         spin_unlock_irq(phba->host->host_lock);
945                         set_current_state(TASK_UNINTERRUPTIBLE);
946                         schedule_timeout( HZ/2);
947                         spin_lock_irq(phba->host->host_lock);
948                 }
949                 if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
950                         break;
951         }
952
953         list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
954         if (lpfc_cmd == NULL)
955                 goto out;
956
957         lpfc_cmd->pCmd = cmnd;
958         lpfc_cmd->timeout = 60;
959         lpfc_cmd->scsi_hba = phba;
960
961         ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
962         if (!ret)
963                 goto out_free_scsi_buf;
964
965         iocbq = &lpfc_cmd->cur_iocbq;
966
967         /* get a buffer for this IOCB command response */
968         list_remove_head(lpfc_iocb_list, iocbqrsp, struct lpfc_iocbq, list);
969         if (iocbqrsp == NULL)
970                 goto out_free_scsi_buf;
971
972         iocbq->iocb_flag |= LPFC_IO_POLL;
973         iocbq->iocb_cmpl = lpfc_sli_wake_iocb_high_priority;
974
975         ret = lpfc_sli_issue_iocb_wait_high_priority(phba,
976                      &phba->sli.ring[psli->fcp_ring],
977                      iocbq, 0, iocbqrsp, 60);
978         if (ret == IOCB_SUCCESS)
979                 ret = SUCCESS;
980
981         lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
982         lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
983         if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT)
984                 if (lpfc_cmd->result & IOERR_DRVR_MASK)
985                         lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
986
987         /*
988          * All outstanding txcmplq I/Os should have been aborted by the target.
989          * Unfortunately, some targets do not abide by this forcing the driver
990          * to double check.
991          */
992         lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
993                             cmnd->device->id, cmnd->device->lun, 0,
994                             LPFC_CTX_LUN);
995
996         loopcnt = 0;
997         while((cnt = lpfc_sli_sum_iocb(phba,
998                                        &phba->sli.ring[phba->sli.fcp_ring],
999                                        cmnd->device->id, cmnd->device->lun,
1000                                        LPFC_CTX_LUN))) {
1001                 spin_unlock_irq(phba->host->host_lock);
1002                 set_current_state(TASK_UNINTERRUPTIBLE);
1003                 schedule_timeout(LPFC_RESET_WAIT*HZ);
1004                 spin_lock_irq(phba->host->host_lock);
1005
1006                 if (++loopcnt
1007                     > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1008                         break;
1009         }
1010
1011         if (cnt) {
1012                 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1013                         "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1014                         phba->brd_no, cnt);
1015         }
1016
1017         lpfc_sli_release_iocbq(phba, iocbqrsp);
1018
1019 out_free_scsi_buf:
1020         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1021                         "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1022                         "Data: x%x x%x x%x\n",
1023                         phba->brd_no, lpfc_cmd->pCmd->device->id,
1024                         lpfc_cmd->pCmd->device->lun, ret, lpfc_cmd->status,
1025                         lpfc_cmd->result);
1026         lpfc_free_scsi_buf(lpfc_cmd);
1027 out:
1028         return ret;
1029 }
1030
1031 static int
1032 lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
1033 {
1034         int rc;
1035         spin_lock_irq(cmnd->device->host->host_lock);
1036         rc = __lpfc_reset_lun_handler(cmnd);
1037         spin_unlock_irq(cmnd->device->host->host_lock);
1038         return rc;
1039 }
1040
1041 /*
1042  * Note: midlayer calls this function with the host_lock held
1043  */
1044 static int
1045 __lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
1046 {
1047         struct Scsi_Host *shost = cmnd->device->host;
1048         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata[0];
1049         struct lpfc_nodelist *ndlp = NULL;
1050         int match;
1051         int ret = FAILED, i, err_count = 0;
1052         int cnt, loopcnt;
1053         unsigned int midlayer_id = 0;
1054         struct lpfc_scsi_buf * lpfc_cmd = NULL;
1055         struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
1056
1057         list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
1058         if (lpfc_cmd == NULL)
1059                 goto out;
1060
1061         /* The lpfc_cmd storage is reused.  Set all loop invariants. */
1062         lpfc_cmd->timeout = 60;
1063         lpfc_cmd->pCmd = cmnd;
1064         lpfc_cmd->scsi_hba = phba;
1065
1066         /*
1067          * Since the driver manages a single bus device, reset all
1068          * targets known to the driver.  Should any target reset
1069          * fail, this routine returns failure to the midlayer.
1070          */
1071         midlayer_id = cmnd->device->id;
1072         for (i = 0; i < MAX_FCP_TARGET; i++) {
1073                 /* Search the mapped list for this target ID */
1074                 match = 0;
1075                 list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1076                         if ((i == ndlp->nlp_sid) && ndlp->rport) {
1077                                 match = 1;
1078                                 break;
1079                         }
1080                 }
1081                 if (!match)
1082                         continue;
1083
1084                 lpfc_cmd->pCmd->device->id = i;
1085                 lpfc_cmd->pCmd->device->hostdata = ndlp->rport->dd_data;
1086                 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba);
1087                 if (ret != SUCCESS) {
1088                         lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1089                                 "%d:0713 Bus Reset on target %d failed\n",
1090                                 phba->brd_no, i);
1091                         err_count++;
1092                 }
1093         }
1094
1095         cmnd->device->id = midlayer_id;
1096         loopcnt = 0;
1097         while((cnt = lpfc_sli_sum_iocb(phba,
1098                                 &phba->sli.ring[phba->sli.fcp_ring],
1099                                 0, 0, LPFC_CTX_HOST))) {
1100                 spin_unlock_irq(phba->host->host_lock);
1101                 set_current_state(TASK_UNINTERRUPTIBLE);
1102                 schedule_timeout(LPFC_RESET_WAIT*HZ);
1103                 spin_lock_irq(phba->host->host_lock);
1104
1105                 if (++loopcnt
1106                     > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1107                         break;
1108         }
1109
1110         if (cnt) {
1111                 /* flush all outstanding commands on the host */
1112                 i = lpfc_sli_abort_iocb(phba,
1113                                 &phba->sli.ring[phba->sli.fcp_ring], 0, 0, 0,
1114                                 LPFC_CTX_HOST);
1115
1116                 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
1117                    "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1118                    phba->brd_no, cnt, i);
1119         }
1120
1121         if (!err_count)
1122                 ret = SUCCESS;
1123
1124         lpfc_free_scsi_buf(lpfc_cmd);
1125         lpfc_printf_log(phba,
1126                         KERN_ERR,
1127                         LOG_FCP,
1128                         "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1129                         phba->brd_no, ret);
1130 out:
1131         return ret;
1132 }
1133
1134 static int
1135 lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
1136 {
1137         int rc;
1138         spin_lock_irq(cmnd->device->host->host_lock);
1139         rc = __lpfc_reset_bus_handler(cmnd);
1140         spin_unlock_irq(cmnd->device->host->host_lock);
1141         return rc;
1142 }
1143
1144 static int
1145 lpfc_slave_alloc(struct scsi_device *sdev)
1146 {
1147         struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata[0];
1148         struct lpfc_scsi_buf *scsi_buf = NULL;
1149         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1150         uint32_t total = 0, i;
1151         uint32_t num_to_alloc = 0;
1152         unsigned long flags;
1153
1154         if (!rport || fc_remote_port_chkready(rport))
1155                 return -ENXIO;
1156
1157         sdev->hostdata = rport->dd_data;
1158
1159         /*
1160          * Populate the cmds_per_lun count scsi_bufs into this host's globally
1161          * available list of scsi buffers.  Don't allocate more than the
1162          * HBA limit conveyed to the midlayer via the host structure.  Note
1163          * that this list of scsi bufs exists for the lifetime of the driver.
1164          */
1165         total = phba->total_scsi_bufs;
1166         num_to_alloc = LPFC_CMD_PER_LUN;
1167         if (total >= phba->cfg_hba_queue_depth) {
1168                 printk(KERN_WARNING "%s, At config limitation of "
1169                        "%d allocated scsi_bufs\n", __FUNCTION__, total);
1170                 return 0;
1171         } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
1172                 num_to_alloc = phba->cfg_hba_queue_depth - total;
1173         }
1174
1175         for (i = 0; i < num_to_alloc; i++) {
1176                 scsi_buf = lpfc_get_scsi_buf(phba);
1177                 if (!scsi_buf) {
1178                         printk(KERN_ERR "%s, failed to allocate "
1179                                "scsi_buf\n", __FUNCTION__);
1180                         break;
1181                 }
1182
1183                 spin_lock_irqsave(phba->host->host_lock, flags);
1184                 phba->total_scsi_bufs++;
1185                 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
1186                 spin_unlock_irqrestore(phba->host->host_lock, flags);
1187         }
1188         return 0;
1189 }
1190
1191 static int
1192 lpfc_slave_configure(struct scsi_device *sdev)
1193 {
1194         struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata[0];
1195         struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1196
1197         if (sdev->tagged_supported)
1198                 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1199         else
1200                 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1201
1202         /*
1203          * Initialize the fc transport attributes for the target
1204          * containing this scsi device.  Also note that the driver's
1205          * target pointer is stored in the starget_data for the
1206          * driver's sysfs entry point functions.
1207          */
1208         rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
1209
1210         return 0;
1211 }
1212
1213 static void
1214 lpfc_slave_destroy(struct scsi_device *sdev)
1215 {
1216         sdev->hostdata = NULL;
1217         return;
1218 }
1219
1220 struct scsi_host_template lpfc_template = {
1221         .module                 = THIS_MODULE,
1222         .name                   = LPFC_DRIVER_NAME,
1223         .info                   = lpfc_info,
1224         .queuecommand           = lpfc_queuecommand,
1225         .eh_abort_handler       = lpfc_abort_handler,
1226         .eh_device_reset_handler= lpfc_reset_lun_handler,
1227         .eh_bus_reset_handler   = lpfc_reset_bus_handler,
1228         .slave_alloc            = lpfc_slave_alloc,
1229         .slave_configure        = lpfc_slave_configure,
1230         .slave_destroy          = lpfc_slave_destroy,
1231         .this_id                = -1,
1232         .sg_tablesize           = LPFC_SG_SEG_CNT,
1233         .cmd_per_lun            = LPFC_CMD_PER_LUN,
1234         .use_clustering         = ENABLE_CLUSTERING,
1235         .shost_attrs            = lpfc_host_attrs,
1236         .max_sectors            = 0xFFFF,
1237 };