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