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