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