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