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