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