[SCSI] Block I/O while SG reset operation in progress - lpfc portion
[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-2006 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 static struct lpfc_scsi_buf*
140 lpfc_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         unsigned long iflag = 0;
145
146         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
147         list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
148         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
149         return  lpfc_cmd;
150 }
151
152 static void
153 lpfc_release_scsi_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
154 {
155         unsigned long iflag = 0;
156         /*
157          * There are only two special cases to consider.  (1) the scsi command
158          * requested scatter-gather usage or (2) the scsi command allocated
159          * a request buffer, but did not request use_sg.  There is a third
160          * case, but it does not require resource deallocation.
161          */
162         if ((psb->seg_cnt > 0) && (psb->pCmd->use_sg)) {
163                 dma_unmap_sg(&phba->pcidev->dev, psb->pCmd->request_buffer,
164                                 psb->seg_cnt, psb->pCmd->sc_data_direction);
165         } else {
166                  if ((psb->nonsg_phys) && (psb->pCmd->request_bufflen)) {
167                         dma_unmap_single(&phba->pcidev->dev, psb->nonsg_phys,
168                                                 psb->pCmd->request_bufflen,
169                                                 psb->pCmd->sc_data_direction);
170                  }
171         }
172
173         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
174         psb->pCmd = NULL;
175         list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
176         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
177 }
178
179 static int
180 lpfc_scsi_prep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd)
181 {
182         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
183         struct scatterlist *sgel = NULL;
184         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
185         struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
186         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
187         dma_addr_t physaddr;
188         uint32_t i, num_bde = 0;
189         int datadir = scsi_cmnd->sc_data_direction;
190         int dma_error;
191
192         /*
193          * There are three possibilities here - use scatter-gather segment, use
194          * the single mapping, or neither.  Start the lpfc command prep by
195          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
196          * data bde entry.
197          */
198         bpl += 2;
199         if (scsi_cmnd->use_sg) {
200                 /*
201                  * The driver stores the segment count returned from pci_map_sg
202                  * because this a count of dma-mappings used to map the use_sg
203                  * pages.  They are not guaranteed to be the same for those
204                  * architectures that implement an IOMMU.
205                  */
206                 sgel = (struct scatterlist *)scsi_cmnd->request_buffer;
207                 lpfc_cmd->seg_cnt = dma_map_sg(&phba->pcidev->dev, sgel,
208                                                 scsi_cmnd->use_sg, datadir);
209                 if (lpfc_cmd->seg_cnt == 0)
210                         return 1;
211
212                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
213                         printk(KERN_ERR "%s: Too many sg segments from "
214                                "dma_map_sg.  Config %d, seg_cnt %d",
215                                __FUNCTION__, phba->cfg_sg_seg_cnt,
216                                lpfc_cmd->seg_cnt);
217                         dma_unmap_sg(&phba->pcidev->dev, sgel,
218                                      lpfc_cmd->seg_cnt, datadir);
219                         return 1;
220                 }
221
222                 /*
223                  * The driver established a maximum scatter-gather segment count
224                  * during probe that limits the number of sg elements in any
225                  * single scsi command.  Just run through the seg_cnt and format
226                  * the bde's.
227                  */
228                 for (i = 0; i < lpfc_cmd->seg_cnt; i++) {
229                         physaddr = sg_dma_address(sgel);
230                         bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
231                         bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
232                         bpl->tus.f.bdeSize = sg_dma_len(sgel);
233                         if (datadir == DMA_TO_DEVICE)
234                                 bpl->tus.f.bdeFlags = 0;
235                         else
236                                 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
237                         bpl->tus.w = le32_to_cpu(bpl->tus.w);
238                         bpl++;
239                         sgel++;
240                         num_bde++;
241                 }
242         } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
243                 physaddr = dma_map_single(&phba->pcidev->dev,
244                                           scsi_cmnd->request_buffer,
245                                           scsi_cmnd->request_bufflen,
246                                           datadir);
247                 dma_error = dma_mapping_error(physaddr);
248                 if (dma_error) {
249                         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
250                                 "%d:0718 Unable to dma_map_single "
251                                 "request_buffer: x%x\n",
252                                 phba->brd_no, dma_error);
253                         return 1;
254                 }
255
256                 lpfc_cmd->nonsg_phys = physaddr;
257                 bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
258                 bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
259                 bpl->tus.f.bdeSize = scsi_cmnd->request_bufflen;
260                 if (datadir == DMA_TO_DEVICE)
261                         bpl->tus.f.bdeFlags = 0;
262                 else
263                         bpl->tus.f.bdeFlags = BUFF_USE_RCV;
264                 bpl->tus.w = le32_to_cpu(bpl->tus.w);
265                 num_bde = 1;
266                 bpl++;
267         }
268
269         /*
270          * Finish initializing those IOCB fields that are dependent on the
271          * scsi_cmnd request_buffer.  Note that the bdeSize is explicitly
272          * reinitialized since all iocb memory resources are used many times
273          * for transmit, receive, and continuation bpl's.
274          */
275         iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
276         iocb_cmd->un.fcpi64.bdl.bdeSize +=
277                 (num_bde * sizeof (struct ulp_bde64));
278         iocb_cmd->ulpBdeCount = 1;
279         iocb_cmd->ulpLe = 1;
280         fcp_cmnd->fcpDl = be32_to_cpu(scsi_cmnd->request_bufflen);
281         return 0;
282 }
283
284 static void
285 lpfc_handle_fcp_err(struct lpfc_scsi_buf *lpfc_cmd)
286 {
287         struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
288         struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
289         struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
290         struct lpfc_hba *phba = lpfc_cmd->scsi_hba;
291         uint32_t fcpi_parm = lpfc_cmd->cur_iocbq.iocb.un.fcpi.fcpi_parm;
292         uint32_t resp_info = fcprsp->rspStatus2;
293         uint32_t scsi_status = fcprsp->rspStatus3;
294         uint32_t host_status = DID_OK;
295         uint32_t rsplen = 0;
296
297         /*
298          *  If this is a task management command, there is no
299          *  scsi packet associated with this lpfc_cmd.  The driver
300          *  consumes it.
301          */
302         if (fcpcmd->fcpCntl2) {
303                 scsi_status = 0;
304                 goto out;
305         }
306
307         lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
308                         "%d:0730 FCP command failed: RSP "
309                         "Data: x%x x%x x%x x%x x%x x%x\n",
310                         phba->brd_no, resp_info, scsi_status,
311                         be32_to_cpu(fcprsp->rspResId),
312                         be32_to_cpu(fcprsp->rspSnsLen),
313                         be32_to_cpu(fcprsp->rspRspLen),
314                         fcprsp->rspInfo3);
315
316         if (resp_info & RSP_LEN_VALID) {
317                 rsplen = be32_to_cpu(fcprsp->rspRspLen);
318                 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
319                     (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
320                         host_status = DID_ERROR;
321                         goto out;
322                 }
323         }
324
325         if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
326                 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
327                 if (snslen > SCSI_SENSE_BUFFERSIZE)
328                         snslen = SCSI_SENSE_BUFFERSIZE;
329
330                 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
331         }
332
333         cmnd->resid = 0;
334         if (resp_info & RESID_UNDER) {
335                 cmnd->resid = be32_to_cpu(fcprsp->rspResId);
336
337                 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
338                                 "%d:0716 FCP Read Underrun, expected %d, "
339                                 "residual %d Data: x%x x%x x%x\n", phba->brd_no,
340                                 be32_to_cpu(fcpcmd->fcpDl), cmnd->resid,
341                                 fcpi_parm, cmnd->cmnd[0], cmnd->underflow);
342
343                 /*
344                  * The cmnd->underflow is the minimum number of bytes that must
345                  * be transfered for this command.  Provided a sense condition
346                  * is not present, make sure the actual amount transferred is at
347                  * least the underflow value or fail.
348                  */
349                 if (!(resp_info & SNS_LEN_VALID) &&
350                     (scsi_status == SAM_STAT_GOOD) &&
351                     (cmnd->request_bufflen - cmnd->resid) < cmnd->underflow) {
352                         lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
353                                         "%d:0717 FCP command x%x residual "
354                                         "underrun converted to error "
355                                         "Data: x%x x%x x%x\n", phba->brd_no,
356                                         cmnd->cmnd[0], cmnd->request_bufflen,
357                                         cmnd->resid, cmnd->underflow);
358
359                         host_status = DID_ERROR;
360                 }
361         } else if (resp_info & RESID_OVER) {
362                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
363                                 "%d:0720 FCP command x%x residual "
364                                 "overrun error. Data: x%x x%x \n",
365                                 phba->brd_no, cmnd->cmnd[0],
366                                 cmnd->request_bufflen, cmnd->resid);
367                 host_status = DID_ERROR;
368
369         /*
370          * Check SLI validation that all the transfer was actually done
371          * (fcpi_parm should be zero). Apply check only to reads.
372          */
373         } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
374                         (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
375                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
376                         "%d:0734 FCP Read Check Error Data: "
377                         "x%x x%x x%x x%x\n", phba->brd_no,
378                         be32_to_cpu(fcpcmd->fcpDl),
379                         be32_to_cpu(fcprsp->rspResId),
380                         fcpi_parm, cmnd->cmnd[0]);
381                 host_status = DID_ERROR;
382                 cmnd->resid = cmnd->request_bufflen;
383         }
384
385  out:
386         cmnd->result = ScsiResult(host_status, scsi_status);
387 }
388
389 static void
390 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
391                         struct lpfc_iocbq *pIocbOut)
392 {
393         struct lpfc_scsi_buf *lpfc_cmd =
394                 (struct lpfc_scsi_buf *) pIocbIn->context1;
395         struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
396         struct lpfc_nodelist *pnode = rdata->pnode;
397         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
398         int result;
399         struct scsi_device *sdev, *tmp_sdev;
400         int depth = 0;
401
402         lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
403         lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
404
405         if (lpfc_cmd->status) {
406                 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
407                     (lpfc_cmd->result & IOERR_DRVR_MASK))
408                         lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
409                 else if (lpfc_cmd->status >= IOSTAT_CNT)
410                         lpfc_cmd->status = IOSTAT_DEFAULT;
411
412                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
413                                 "%d:0729 FCP cmd x%x failed <%d/%d> status: "
414                                 "x%x result: x%x Data: x%x x%x\n",
415                                 phba->brd_no, cmd->cmnd[0], cmd->device->id,
416                                 cmd->device->lun, lpfc_cmd->status,
417                                 lpfc_cmd->result, pIocbOut->iocb.ulpContext,
418                                 lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
419
420                 switch (lpfc_cmd->status) {
421                 case IOSTAT_FCP_RSP_ERROR:
422                         /* Call FCP RSP handler to determine result */
423                         lpfc_handle_fcp_err(lpfc_cmd);
424                         break;
425                 case IOSTAT_NPORT_BSY:
426                 case IOSTAT_FABRIC_BSY:
427                         cmd->result = ScsiResult(DID_BUS_BUSY, 0);
428                         break;
429                 default:
430                         cmd->result = ScsiResult(DID_ERROR, 0);
431                         break;
432                 }
433
434                 if ((pnode == NULL )
435                     || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
436                         cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
437         } else {
438                 cmd->result = ScsiResult(DID_OK, 0);
439         }
440
441         if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
442                 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
443
444                 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
445                                 "%d:0710 Iodone <%d/%d> cmd %p, error x%x "
446                                 "SNS x%x x%x Data: x%x x%x\n",
447                                 phba->brd_no, cmd->device->id,
448                                 cmd->device->lun, cmd, cmd->result,
449                                 *lp, *(lp + 3), cmd->retries, cmd->resid);
450         }
451
452         result = cmd->result;
453         sdev = cmd->device;
454         cmd->scsi_done(cmd);
455
456         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
457                 lpfc_release_scsi_buf(phba, lpfc_cmd);
458                 return;
459         }
460
461         if (!result && pnode != NULL &&
462            ((jiffies - pnode->last_ramp_up_time) >
463                 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
464            ((jiffies - pnode->last_q_full_time) >
465                 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
466            (phba->cfg_lun_queue_depth > sdev->queue_depth)) {
467                 shost_for_each_device(tmp_sdev, sdev->host) {
468                         if (phba->cfg_lun_queue_depth > tmp_sdev->queue_depth) {
469                                 if (tmp_sdev->id != sdev->id)
470                                         continue;
471                                 if (tmp_sdev->ordered_tags)
472                                         scsi_adjust_queue_depth(tmp_sdev,
473                                                 MSG_ORDERED_TAG,
474                                                 tmp_sdev->queue_depth+1);
475                                 else
476                                         scsi_adjust_queue_depth(tmp_sdev,
477                                                 MSG_SIMPLE_TAG,
478                                                 tmp_sdev->queue_depth+1);
479
480                                 pnode->last_ramp_up_time = jiffies;
481                         }
482                 }
483         }
484
485         /*
486          * Check for queue full.  If the lun is reporting queue full, then
487          * back off the lun queue depth to prevent target overloads.
488          */
489         if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
490                 pnode->last_q_full_time = jiffies;
491
492                 shost_for_each_device(tmp_sdev, sdev->host) {
493                         if (tmp_sdev->id != sdev->id)
494                                 continue;
495                         depth = scsi_track_queue_full(tmp_sdev,
496                                         tmp_sdev->queue_depth - 1);
497                 }
498                 /*
499                  * The queue depth cannot be lowered any more.
500                  * Modify the returned error code to store
501                  * the final depth value set by
502                  * scsi_track_queue_full.
503                  */
504                 if (depth == -1)
505                         depth = sdev->host->cmd_per_lun;
506
507                 if (depth) {
508                         lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
509                                 "%d:0711 detected queue full - lun queue depth "
510                                 " adjusted to %d.\n", phba->brd_no, depth);
511                 }
512         }
513
514         lpfc_release_scsi_buf(phba, lpfc_cmd);
515 }
516
517 static void
518 lpfc_scsi_prep_cmnd(struct lpfc_hba * phba, struct lpfc_scsi_buf * lpfc_cmd,
519                         struct lpfc_nodelist *pnode)
520 {
521         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
522         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
523         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
524         struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
525         int datadir = scsi_cmnd->sc_data_direction;
526
527         lpfc_cmd->fcp_rsp->rspSnsLen = 0;
528         /* clear task management bits */
529         lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
530
531         int_to_scsilun(lpfc_cmd->pCmd->device->lun,
532                         &lpfc_cmd->fcp_cmnd->fcp_lun);
533
534         memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
535
536         if (scsi_cmnd->device->tagged_supported) {
537                 switch (scsi_cmnd->tag) {
538                 case HEAD_OF_QUEUE_TAG:
539                         fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
540                         break;
541                 case ORDERED_QUEUE_TAG:
542                         fcp_cmnd->fcpCntl1 = ORDERED_Q;
543                         break;
544                 default:
545                         fcp_cmnd->fcpCntl1 = SIMPLE_Q;
546                         break;
547                 }
548         } else
549                 fcp_cmnd->fcpCntl1 = 0;
550
551         /*
552          * There are three possibilities here - use scatter-gather segment, use
553          * the single mapping, or neither.  Start the lpfc command prep by
554          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
555          * data bde entry.
556          */
557         if (scsi_cmnd->use_sg) {
558                 if (datadir == DMA_TO_DEVICE) {
559                         iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
560                         iocb_cmd->un.fcpi.fcpi_parm = 0;
561                         iocb_cmd->ulpPU = 0;
562                         fcp_cmnd->fcpCntl3 = WRITE_DATA;
563                         phba->fc4OutputRequests++;
564                 } else {
565                         iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
566                         iocb_cmd->ulpPU = PARM_READ_CHECK;
567                         iocb_cmd->un.fcpi.fcpi_parm =
568                                 scsi_cmnd->request_bufflen;
569                         fcp_cmnd->fcpCntl3 = READ_DATA;
570                         phba->fc4InputRequests++;
571                 }
572         } else if (scsi_cmnd->request_buffer && scsi_cmnd->request_bufflen) {
573                 if (datadir == DMA_TO_DEVICE) {
574                         iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
575                         iocb_cmd->un.fcpi.fcpi_parm = 0;
576                         iocb_cmd->ulpPU = 0;
577                         fcp_cmnd->fcpCntl3 = WRITE_DATA;
578                         phba->fc4OutputRequests++;
579                 } else {
580                         iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
581                         iocb_cmd->ulpPU = PARM_READ_CHECK;
582                         iocb_cmd->un.fcpi.fcpi_parm =
583                                 scsi_cmnd->request_bufflen;
584                         fcp_cmnd->fcpCntl3 = READ_DATA;
585                         phba->fc4InputRequests++;
586                 }
587         } else {
588                 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
589                 iocb_cmd->un.fcpi.fcpi_parm = 0;
590                 iocb_cmd->ulpPU = 0;
591                 fcp_cmnd->fcpCntl3 = 0;
592                 phba->fc4ControlRequests++;
593         }
594
595         /*
596          * Finish initializing those IOCB fields that are independent
597          * of the scsi_cmnd request_buffer
598          */
599         piocbq->iocb.ulpContext = pnode->nlp_rpi;
600         if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
601                 piocbq->iocb.ulpFCP2Rcvy = 1;
602
603         piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
604         piocbq->context1  = lpfc_cmd;
605         piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
606         piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
607 }
608
609 static int
610 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_hba *phba,
611                              struct lpfc_scsi_buf *lpfc_cmd,
612                              uint8_t task_mgmt_cmd)
613 {
614         struct lpfc_sli *psli;
615         struct lpfc_iocbq *piocbq;
616         IOCB_t *piocb;
617         struct fcp_cmnd *fcp_cmnd;
618         struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
619         struct lpfc_nodelist *ndlp = rdata->pnode;
620
621         if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
622                 return 0;
623         }
624
625         psli = &phba->sli;
626         piocbq = &(lpfc_cmd->cur_iocbq);
627         piocb = &piocbq->iocb;
628
629         fcp_cmnd = lpfc_cmd->fcp_cmnd;
630         int_to_scsilun(lpfc_cmd->pCmd->device->lun,
631                         &lpfc_cmd->fcp_cmnd->fcp_lun);
632         fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
633
634         piocb->ulpCommand = CMD_FCP_ICMND64_CR;
635
636         piocb->ulpContext = ndlp->nlp_rpi;
637         if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
638                 piocb->ulpFCP2Rcvy = 1;
639         }
640         piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
641
642         /* ulpTimeout is only one byte */
643         if (lpfc_cmd->timeout > 0xff) {
644                 /*
645                  * Do not timeout the command at the firmware level.
646                  * The driver will provide the timeout mechanism.
647                  */
648                 piocb->ulpTimeout = 0;
649         } else {
650                 piocb->ulpTimeout = lpfc_cmd->timeout;
651         }
652
653         return (1);
654 }
655
656 static int
657 lpfc_scsi_tgt_reset(struct lpfc_scsi_buf * lpfc_cmd, struct lpfc_hba * phba,
658                     unsigned  tgt_id, struct lpfc_rport_data *rdata)
659 {
660         struct lpfc_iocbq *iocbq;
661         struct lpfc_iocbq *iocbqrsp;
662         int ret;
663
664         lpfc_cmd->rdata = rdata;
665         ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_TARGET_RESET);
666         if (!ret)
667                 return FAILED;
668
669         lpfc_cmd->scsi_hba = phba;
670         iocbq = &lpfc_cmd->cur_iocbq;
671         iocbqrsp = lpfc_sli_get_iocbq(phba);
672
673         if (!iocbqrsp)
674                 return FAILED;
675
676         /* Issue Target Reset to TGT <num> */
677         lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
678                         "%d:0702 Issue Target Reset to TGT %d "
679                         "Data: x%x x%x\n",
680                         phba->brd_no, tgt_id, rdata->pnode->nlp_rpi,
681                         rdata->pnode->nlp_flag);
682
683         ret = lpfc_sli_issue_iocb_wait(phba,
684                                        &phba->sli.ring[phba->sli.fcp_ring],
685                                        iocbq, iocbqrsp, lpfc_cmd->timeout);
686         if (ret != IOCB_SUCCESS) {
687                 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
688                 ret = FAILED;
689         } else {
690                 ret = SUCCESS;
691                 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
692                 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
693                 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
694                         (lpfc_cmd->result & IOERR_DRVR_MASK))
695                                 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
696         }
697
698         lpfc_sli_release_iocbq(phba, iocbqrsp);
699         return ret;
700 }
701
702 const char *
703 lpfc_info(struct Scsi_Host *host)
704 {
705         struct lpfc_hba    *phba = (struct lpfc_hba *) host->hostdata;
706         int len;
707         static char  lpfcinfobuf[384];
708
709         memset(lpfcinfobuf,0,384);
710         if (phba && phba->pcidev){
711                 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
712                 len = strlen(lpfcinfobuf);
713                 snprintf(lpfcinfobuf + len,
714                         384-len,
715                         " on PCI bus %02x device %02x irq %d",
716                         phba->pcidev->bus->number,
717                         phba->pcidev->devfn,
718                         phba->pcidev->irq);
719                 len = strlen(lpfcinfobuf);
720                 if (phba->Port[0]) {
721                         snprintf(lpfcinfobuf + len,
722                                  384-len,
723                                  " port %s",
724                                  phba->Port);
725                 }
726         }
727         return lpfcinfobuf;
728 }
729
730 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
731 {
732         unsigned long  poll_tmo_expires =
733                 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
734
735         if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
736                 mod_timer(&phba->fcp_poll_timer,
737                           poll_tmo_expires);
738 }
739
740 void lpfc_poll_start_timer(struct lpfc_hba * phba)
741 {
742         lpfc_poll_rearm_timer(phba);
743 }
744
745 void lpfc_poll_timeout(unsigned long ptr)
746 {
747         struct lpfc_hba *phba = (struct lpfc_hba *)ptr;
748         unsigned long iflag;
749
750         spin_lock_irqsave(phba->host->host_lock, iflag);
751
752         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
753                 lpfc_sli_poll_fcp_ring (phba);
754                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
755                         lpfc_poll_rearm_timer(phba);
756         }
757
758         spin_unlock_irqrestore(phba->host->host_lock, iflag);
759 }
760
761 static int
762 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
763 {
764         struct lpfc_hba *phba =
765                 (struct lpfc_hba *) cmnd->device->host->hostdata;
766         struct lpfc_sli *psli = &phba->sli;
767         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
768         struct lpfc_nodelist *ndlp = rdata->pnode;
769         struct lpfc_scsi_buf *lpfc_cmd;
770         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
771         int err;
772
773         err = fc_remote_port_chkready(rport);
774         if (err) {
775                 cmnd->result = err;
776                 goto out_fail_command;
777         }
778
779         /*
780          * Catch race where our node has transitioned, but the
781          * transport is still transitioning.
782          */
783         if (!ndlp) {
784                 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
785                 goto out_fail_command;
786         }
787         lpfc_cmd = lpfc_get_scsi_buf (phba);
788         if (lpfc_cmd == NULL) {
789                 lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
790                                 "%d:0707 driver's buffer pool is empty, "
791                                 "IO busied\n", phba->brd_no);
792                 goto out_host_busy;
793         }
794
795         /*
796          * Store the midlayer's command structure for the completion phase
797          * and complete the command initialization.
798          */
799         lpfc_cmd->pCmd  = cmnd;
800         lpfc_cmd->rdata = rdata;
801         lpfc_cmd->timeout = 0;
802         cmnd->host_scribble = (unsigned char *)lpfc_cmd;
803         cmnd->scsi_done = done;
804
805         err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
806         if (err)
807                 goto out_host_busy_free_buf;
808
809         lpfc_scsi_prep_cmnd(phba, lpfc_cmd, ndlp);
810
811         err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
812                                 &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
813         if (err)
814                 goto out_host_busy_free_buf;
815
816         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
817                 lpfc_sli_poll_fcp_ring(phba);
818                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
819                         lpfc_poll_rearm_timer(phba);
820         }
821
822         return 0;
823
824  out_host_busy_free_buf:
825         lpfc_release_scsi_buf(phba, lpfc_cmd);
826  out_host_busy:
827         return SCSI_MLQUEUE_HOST_BUSY;
828
829  out_fail_command:
830         done(cmnd);
831         return 0;
832 }
833
834
835 static int
836 lpfc_abort_handler(struct scsi_cmnd *cmnd)
837 {
838         struct Scsi_Host *shost = cmnd->device->host;
839         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
840         struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
841         struct lpfc_iocbq *iocb;
842         struct lpfc_iocbq *abtsiocb;
843         struct lpfc_scsi_buf *lpfc_cmd;
844         IOCB_t *cmd, *icmd;
845         unsigned int loop_count = 0;
846         int ret = SUCCESS;
847
848         spin_lock_irq(shost->host_lock);
849
850         lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
851         BUG_ON(!lpfc_cmd);
852
853         /*
854          * If pCmd field of the corresponding lpfc_scsi_buf structure
855          * points to a different SCSI command, then the driver has
856          * already completed this command, but the midlayer did not
857          * see the completion before the eh fired.  Just return
858          * SUCCESS.
859          */
860         iocb = &lpfc_cmd->cur_iocbq;
861         if (lpfc_cmd->pCmd != cmnd)
862                 goto out;
863
864         BUG_ON(iocb->context1 != lpfc_cmd);
865
866         abtsiocb = lpfc_sli_get_iocbq(phba);
867         if (abtsiocb == NULL) {
868                 ret = FAILED;
869                 goto out;
870         }
871
872         /*
873          * The scsi command can not be in txq and it is in flight because the
874          * pCmd is still pointig at the SCSI command we have to abort. There
875          * is no need to search the txcmplq. Just send an abort to the FW.
876          */
877
878         cmd = &iocb->iocb;
879         icmd = &abtsiocb->iocb;
880         icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
881         icmd->un.acxri.abortContextTag = cmd->ulpContext;
882         icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
883
884         icmd->ulpLe = 1;
885         icmd->ulpClass = cmd->ulpClass;
886         if (phba->hba_state >= LPFC_LINK_UP)
887                 icmd->ulpCommand = CMD_ABORT_XRI_CN;
888         else
889                 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
890
891         abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
892         if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
893                 lpfc_sli_release_iocbq(phba, abtsiocb);
894                 ret = FAILED;
895                 goto out;
896         }
897
898         if (phba->cfg_poll & DISABLE_FCP_RING_INT)
899                 lpfc_sli_poll_fcp_ring (phba);
900
901         /* Wait for abort to complete */
902         while (lpfc_cmd->pCmd == cmnd)
903         {
904                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
905                         lpfc_sli_poll_fcp_ring (phba);
906
907                 spin_unlock_irq(phba->host->host_lock);
908                         schedule_timeout_uninterruptible(LPFC_ABORT_WAIT*HZ);
909                 spin_lock_irq(phba->host->host_lock);
910                 if (++loop_count
911                     > (2 * phba->cfg_nodev_tmo)/LPFC_ABORT_WAIT)
912                         break;
913         }
914
915         if (lpfc_cmd->pCmd == cmnd) {
916                 ret = FAILED;
917                 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
918                                 "%d:0748 abort handler timed out waiting for "
919                                 "abort to complete: ret %#x, ID %d, LUN %d, "
920                                 "snum %#lx\n",
921                                 phba->brd_no,  ret, cmnd->device->id,
922                                 cmnd->device->lun, cmnd->serial_number);
923         }
924
925  out:
926         lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
927                         "%d:0749 SCSI Layer I/O Abort Request "
928                         "Status x%x ID %d LUN %d snum %#lx\n",
929                         phba->brd_no, ret, cmnd->device->id,
930                         cmnd->device->lun, cmnd->serial_number);
931
932         spin_unlock_irq(shost->host_lock);
933
934         return ret;
935 }
936
937 static int
938 lpfc_reset_lun_handler(struct scsi_cmnd *cmnd)
939 {
940         struct Scsi_Host *shost = cmnd->device->host;
941         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
942         struct lpfc_scsi_buf *lpfc_cmd;
943         struct lpfc_iocbq *iocbq, *iocbqrsp;
944         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
945         struct lpfc_nodelist *pnode = rdata->pnode;
946         uint32_t cmd_result = 0, cmd_status = 0;
947         int ret = FAILED;
948         int cnt, loopcnt;
949
950         spin_lock_irq(shost->host_lock);
951         /*
952          * If target is not in a MAPPED state, delay the reset until
953          * target is rediscovered or nodev timeout expires.
954          */
955         while ( 1 ) {
956                 if (!pnode)
957                         break;
958
959                 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
960                         spin_unlock_irq(phba->host->host_lock);
961                         schedule_timeout_uninterruptible(msecs_to_jiffies(500));
962                         spin_lock_irq(phba->host->host_lock);
963                 }
964                 if ((pnode) && (pnode->nlp_state == NLP_STE_MAPPED_NODE))
965                         break;
966         }
967
968         lpfc_cmd = lpfc_get_scsi_buf (phba);
969         if (lpfc_cmd == NULL)
970                 goto out;
971
972         lpfc_cmd->pCmd = cmnd;
973         lpfc_cmd->timeout = 60;
974         lpfc_cmd->scsi_hba = phba;
975         lpfc_cmd->rdata = rdata;
976
977         ret = lpfc_scsi_prep_task_mgmt_cmd(phba, lpfc_cmd, FCP_LUN_RESET);
978         if (!ret)
979                 goto out_free_scsi_buf;
980
981         iocbq = &lpfc_cmd->cur_iocbq;
982
983         /* get a buffer for this IOCB command response */
984         iocbqrsp = lpfc_sli_get_iocbq(phba);
985         if (iocbqrsp == NULL)
986                 goto out_free_scsi_buf;
987
988         lpfc_printf_log(phba, KERN_INFO, LOG_FCP,
989                         "%d:0703 Issue LUN Reset to TGT %d LUN %d "
990                         "Data: x%x x%x\n", phba->brd_no, cmnd->device->id,
991                         cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
992
993         ret = lpfc_sli_issue_iocb_wait(phba,
994                                        &phba->sli.ring[phba->sli.fcp_ring],
995                                        iocbq, iocbqrsp, lpfc_cmd->timeout);
996         if (ret == IOCB_SUCCESS)
997                 ret = SUCCESS;
998
999
1000         cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1001         cmd_status = iocbqrsp->iocb.ulpStatus;
1002
1003         lpfc_sli_release_iocbq(phba, iocbqrsp);
1004         lpfc_release_scsi_buf(phba, lpfc_cmd);
1005
1006         /*
1007          * All outstanding txcmplq I/Os should have been aborted by the device.
1008          * Unfortunately, some targets do not abide by this forcing the driver
1009          * to double check.
1010          */
1011         cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1012                                 cmnd->device->id, cmnd->device->lun,
1013                                 LPFC_CTX_LUN);
1014         if (cnt)
1015                 lpfc_sli_abort_iocb(phba,
1016                                     &phba->sli.ring[phba->sli.fcp_ring],
1017                                     cmnd->device->id, cmnd->device->lun,
1018                                     0, LPFC_CTX_LUN);
1019         loopcnt = 0;
1020         while(cnt) {
1021                 spin_unlock_irq(phba->host->host_lock);
1022                 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
1023                 spin_lock_irq(phba->host->host_lock);
1024
1025                 if (++loopcnt
1026                     > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1027                         break;
1028
1029                 cnt = lpfc_sli_sum_iocb(phba,
1030                                         &phba->sli.ring[phba->sli.fcp_ring],
1031                                         cmnd->device->id, cmnd->device->lun,
1032                                         LPFC_CTX_LUN);
1033         }
1034
1035         if (cnt) {
1036                 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1037                         "%d:0719 LUN Reset I/O flush failure: cnt x%x\n",
1038                         phba->brd_no, cnt);
1039                 ret = FAILED;
1040         }
1041
1042 out_free_scsi_buf:
1043         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1044                         "%d:0713 SCSI layer issued LUN reset (%d, %d) "
1045                         "Data: x%x x%x x%x\n",
1046                         phba->brd_no, cmnd->device->id,cmnd->device->lun,
1047                         ret, cmd_status, cmd_result);
1048
1049 out:
1050         spin_unlock_irq(shost->host_lock);
1051         return ret;
1052 }
1053
1054 static int
1055 lpfc_reset_bus_handler(struct scsi_cmnd *cmnd)
1056 {
1057         struct Scsi_Host *shost = cmnd->device->host;
1058         struct lpfc_hba *phba = (struct lpfc_hba *)shost->hostdata;
1059         struct lpfc_nodelist *ndlp = NULL;
1060         int match;
1061         int ret = FAILED, i, err_count = 0;
1062         int cnt, loopcnt;
1063         struct lpfc_scsi_buf * lpfc_cmd;
1064
1065         spin_lock_irq(shost->host_lock);
1066
1067         lpfc_cmd = lpfc_get_scsi_buf(phba);
1068         if (lpfc_cmd == NULL)
1069                 goto out;
1070
1071         /* The lpfc_cmd storage is reused.  Set all loop invariants. */
1072         lpfc_cmd->timeout = 60;
1073         lpfc_cmd->pCmd = cmnd;
1074         lpfc_cmd->scsi_hba = phba;
1075
1076         /*
1077          * Since the driver manages a single bus device, reset all
1078          * targets known to the driver.  Should any target reset
1079          * fail, this routine returns failure to the midlayer.
1080          */
1081         for (i = 0; i < MAX_FCP_TARGET; i++) {
1082                 /* Search the mapped list for this target ID */
1083                 match = 0;
1084                 list_for_each_entry(ndlp, &phba->fc_nlpmap_list, nlp_listp) {
1085                         if ((i == ndlp->nlp_sid) && ndlp->rport) {
1086                                 match = 1;
1087                                 break;
1088                         }
1089                 }
1090                 if (!match)
1091                         continue;
1092
1093                 ret = lpfc_scsi_tgt_reset(lpfc_cmd, phba,
1094                                           i, ndlp->rport->dd_data);
1095                 if (ret != SUCCESS) {
1096                         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1097                                 "%d:0713 Bus Reset on target %d failed\n",
1098                                 phba->brd_no, i);
1099                         err_count++;
1100                 }
1101         }
1102
1103         if (err_count == 0)
1104                 ret = SUCCESS;
1105
1106         lpfc_release_scsi_buf(phba, lpfc_cmd);
1107
1108         /*
1109          * All outstanding txcmplq I/Os should have been aborted by
1110          * the targets.  Unfortunately, some targets do not abide by
1111          * this forcing the driver to double check.
1112          */
1113         cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1114                                 0, 0, LPFC_CTX_HOST);
1115         if (cnt)
1116                 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1117                                     0, 0, 0, LPFC_CTX_HOST);
1118         loopcnt = 0;
1119         while(cnt) {
1120                 spin_unlock_irq(phba->host->host_lock);
1121                 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
1122                 spin_lock_irq(phba->host->host_lock);
1123
1124                 if (++loopcnt
1125                     > (2 * phba->cfg_nodev_tmo)/LPFC_RESET_WAIT)
1126                         break;
1127
1128                 cnt = lpfc_sli_sum_iocb(phba,
1129                                         &phba->sli.ring[phba->sli.fcp_ring],
1130                                         0, 0, LPFC_CTX_HOST);
1131         }
1132
1133         if (cnt) {
1134                 lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1135                    "%d:0715 Bus Reset I/O flush failure: cnt x%x left x%x\n",
1136                    phba->brd_no, cnt, i);
1137                 ret = FAILED;
1138         }
1139
1140         lpfc_printf_log(phba,
1141                         KERN_ERR,
1142                         LOG_FCP,
1143                         "%d:0714 SCSI layer issued Bus Reset Data: x%x\n",
1144                         phba->brd_no, ret);
1145 out:
1146         spin_unlock_irq(shost->host_lock);
1147         return ret;
1148 }
1149
1150 static int
1151 lpfc_slave_alloc(struct scsi_device *sdev)
1152 {
1153         struct lpfc_hba *phba = (struct lpfc_hba *)sdev->host->hostdata;
1154         struct lpfc_scsi_buf *scsi_buf = NULL;
1155         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1156         uint32_t total = 0, i;
1157         uint32_t num_to_alloc = 0;
1158         unsigned long flags;
1159
1160         if (!rport || fc_remote_port_chkready(rport))
1161                 return -ENXIO;
1162
1163         sdev->hostdata = rport->dd_data;
1164
1165         /*
1166          * Populate the cmds_per_lun count scsi_bufs into this host's globally
1167          * available list of scsi buffers.  Don't allocate more than the
1168          * HBA limit conveyed to the midlayer via the host structure.  The
1169          * formula accounts for the lun_queue_depth + error handlers + 1
1170          * extra.  This list of scsi bufs exists for the lifetime of the driver.
1171          */
1172         total = phba->total_scsi_bufs;
1173         num_to_alloc = phba->cfg_lun_queue_depth + 2;
1174         if (total >= phba->cfg_hba_queue_depth) {
1175                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1176                                 "%d:0704 At limitation of %d preallocated "
1177                                 "command buffers\n", phba->brd_no, total);
1178                 return 0;
1179         } else if (total + num_to_alloc > phba->cfg_hba_queue_depth) {
1180                 lpfc_printf_log(phba, KERN_WARNING, LOG_FCP,
1181                                 "%d:0705 Allocation request of %d command "
1182                                 "buffers will exceed max of %d.  Reducing "
1183                                 "allocation request to %d.\n", phba->brd_no,
1184                                 num_to_alloc, phba->cfg_hba_queue_depth,
1185                                 (phba->cfg_hba_queue_depth - total));
1186                 num_to_alloc = phba->cfg_hba_queue_depth - total;
1187         }
1188
1189         for (i = 0; i < num_to_alloc; i++) {
1190                 scsi_buf = lpfc_new_scsi_buf(phba);
1191                 if (!scsi_buf) {
1192                         lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
1193                                         "%d:0706 Failed to allocate command "
1194                                         "buffer\n", phba->brd_no);
1195                         break;
1196                 }
1197
1198                 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
1199                 phba->total_scsi_bufs++;
1200                 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
1201                 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
1202         }
1203         return 0;
1204 }
1205
1206 static int
1207 lpfc_slave_configure(struct scsi_device *sdev)
1208 {
1209         struct lpfc_hba *phba = (struct lpfc_hba *) sdev->host->hostdata;
1210         struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
1211
1212         if (sdev->tagged_supported)
1213                 scsi_activate_tcq(sdev, phba->cfg_lun_queue_depth);
1214         else
1215                 scsi_deactivate_tcq(sdev, phba->cfg_lun_queue_depth);
1216
1217         /*
1218          * Initialize the fc transport attributes for the target
1219          * containing this scsi device.  Also note that the driver's
1220          * target pointer is stored in the starget_data for the
1221          * driver's sysfs entry point functions.
1222          */
1223         rport->dev_loss_tmo = phba->cfg_nodev_tmo + 5;
1224
1225         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1226                 lpfc_sli_poll_fcp_ring(phba);
1227                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1228                         lpfc_poll_rearm_timer(phba);
1229         }
1230
1231         return 0;
1232 }
1233
1234 static void
1235 lpfc_slave_destroy(struct scsi_device *sdev)
1236 {
1237         sdev->hostdata = NULL;
1238         return;
1239 }
1240
1241 struct scsi_host_template lpfc_template = {
1242         .module                 = THIS_MODULE,
1243         .name                   = LPFC_DRIVER_NAME,
1244         .info                   = lpfc_info,
1245         .queuecommand           = lpfc_queuecommand,
1246         .eh_abort_handler       = lpfc_abort_handler,
1247         .eh_device_reset_handler= lpfc_reset_lun_handler,
1248         .eh_bus_reset_handler   = lpfc_reset_bus_handler,
1249         .slave_alloc            = lpfc_slave_alloc,
1250         .slave_configure        = lpfc_slave_configure,
1251         .slave_destroy          = lpfc_slave_destroy,
1252         .this_id                = -1,
1253         .sg_tablesize           = LPFC_SG_SEG_CNT,
1254         .cmd_per_lun            = LPFC_CMD_PER_LUN,
1255         .use_clustering         = ENABLE_CLUSTERING,
1256         .shost_attrs            = lpfc_host_attrs,
1257         .max_sectors            = 0xFFFF,
1258 };