9ee2927ad82a84db3c096471e54f75d02d638e3f
[linux-2.6.git] / drivers / scsi / lpfc / lpfc_scsi.c
1 /*******************************************************************
2  * This file is part of the Emulex Linux Device Driver for         *
3  * Fibre Channel Host Bus Adapters.                                *
4  * Copyright (C) 2004-2007 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 #include <linux/delay.h>
25
26 #include <scsi/scsi.h>
27 #include <scsi/scsi_device.h>
28 #include <scsi/scsi_host.h>
29 #include <scsi/scsi_tcq.h>
30 #include <scsi/scsi_transport_fc.h>
31
32 #include "lpfc_version.h"
33 #include "lpfc_hw.h"
34 #include "lpfc_sli.h"
35 #include "lpfc_disc.h"
36 #include "lpfc_scsi.h"
37 #include "lpfc.h"
38 #include "lpfc_logmsg.h"
39 #include "lpfc_crtn.h"
40 #include "lpfc_vport.h"
41
42 #define LPFC_RESET_WAIT  2
43 #define LPFC_ABORT_WAIT  2
44
45 /*
46  * This function is called with no lock held when there is a resource
47  * error in driver or in firmware.
48  */
49 void
50 lpfc_adjust_queue_depth(struct lpfc_hba *phba)
51 {
52         unsigned long flags;
53
54         spin_lock_irqsave(&phba->hbalock, flags);
55         atomic_inc(&phba->num_rsrc_err);
56         phba->last_rsrc_error_time = jiffies;
57
58         if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
59                 spin_unlock_irqrestore(&phba->hbalock, flags);
60                 return;
61         }
62
63         phba->last_ramp_down_time = jiffies;
64
65         spin_unlock_irqrestore(&phba->hbalock, flags);
66
67         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
68         if ((phba->pport->work_port_events &
69                 WORKER_RAMP_DOWN_QUEUE) == 0) {
70                 phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
71         }
72         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
73
74         spin_lock_irqsave(&phba->hbalock, flags);
75         if (phba->work_wait)
76                 wake_up(phba->work_wait);
77         spin_unlock_irqrestore(&phba->hbalock, flags);
78
79         return;
80 }
81
82 /*
83  * This function is called with no lock held when there is a successful
84  * SCSI command completion.
85  */
86 static inline void
87 lpfc_rampup_queue_depth(struct lpfc_vport  *vport,
88                         struct scsi_device *sdev)
89 {
90         unsigned long flags;
91         struct lpfc_hba *phba = vport->phba;
92         atomic_inc(&phba->num_cmd_success);
93
94         if (vport->cfg_lun_queue_depth <= sdev->queue_depth)
95                 return;
96         spin_lock_irqsave(&phba->hbalock, flags);
97         if (((phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) > jiffies) ||
98          ((phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL ) > jiffies)) {
99                 spin_unlock_irqrestore(&phba->hbalock, flags);
100                 return;
101         }
102         phba->last_ramp_up_time = jiffies;
103         spin_unlock_irqrestore(&phba->hbalock, flags);
104
105         spin_lock_irqsave(&phba->pport->work_port_lock, flags);
106         if ((phba->pport->work_port_events &
107                 WORKER_RAMP_UP_QUEUE) == 0) {
108                 phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
109         }
110         spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
111
112         spin_lock_irqsave(&phba->hbalock, flags);
113         if (phba->work_wait)
114                 wake_up(phba->work_wait);
115         spin_unlock_irqrestore(&phba->hbalock, flags);
116 }
117
118 void
119 lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
120 {
121         struct lpfc_vport **vports;
122         struct Scsi_Host  *shost;
123         struct scsi_device *sdev;
124         unsigned long new_queue_depth;
125         unsigned long num_rsrc_err, num_cmd_success;
126         int i;
127
128         num_rsrc_err = atomic_read(&phba->num_rsrc_err);
129         num_cmd_success = atomic_read(&phba->num_cmd_success);
130
131         vports = lpfc_create_vport_work_array(phba);
132         if (vports != NULL)
133                 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
134                         shost = lpfc_shost_from_vport(vports[i]);
135                         shost_for_each_device(sdev, shost) {
136                                 new_queue_depth =
137                                         sdev->queue_depth * num_rsrc_err /
138                                         (num_rsrc_err + num_cmd_success);
139                                 if (!new_queue_depth)
140                                         new_queue_depth = sdev->queue_depth - 1;
141                                 else
142                                         new_queue_depth = sdev->queue_depth -
143                                                                 new_queue_depth;
144                                 if (sdev->ordered_tags)
145                                         scsi_adjust_queue_depth(sdev,
146                                                         MSG_ORDERED_TAG,
147                                                         new_queue_depth);
148                                 else
149                                         scsi_adjust_queue_depth(sdev,
150                                                         MSG_SIMPLE_TAG,
151                                                         new_queue_depth);
152                         }
153                 }
154         lpfc_destroy_vport_work_array(vports);
155         spin_unlock_irq(&phba->hbalock);
156         atomic_set(&phba->num_rsrc_err, 0);
157         atomic_set(&phba->num_cmd_success, 0);
158 }
159
160 void
161 lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
162 {
163         struct lpfc_vport **vports;
164         struct Scsi_Host  *shost;
165         struct scsi_device *sdev;
166         int i;
167
168         vports = lpfc_create_vport_work_array(phba);
169         if (vports != NULL)
170                 for(i = 0; i < LPFC_MAX_VPORTS && vports[i] != NULL; i++) {
171                         shost = lpfc_shost_from_vport(vports[i]);
172                         shost_for_each_device(sdev, shost) {
173                                 if (sdev->ordered_tags)
174                                         scsi_adjust_queue_depth(sdev,
175                                                         MSG_ORDERED_TAG,
176                                                         sdev->queue_depth+1);
177                                 else
178                                         scsi_adjust_queue_depth(sdev,
179                                                         MSG_SIMPLE_TAG,
180                                                         sdev->queue_depth+1);
181                         }
182                 }
183         lpfc_destroy_vport_work_array(vports);
184         atomic_set(&phba->num_rsrc_err, 0);
185         atomic_set(&phba->num_cmd_success, 0);
186 }
187
188 /*
189  * This routine allocates a scsi buffer, which contains all the necessary
190  * information needed to initiate a SCSI I/O.  The non-DMAable buffer region
191  * contains information to build the IOCB.  The DMAable region contains
192  * memory for the FCP CMND, FCP RSP, and the inital BPL.  In addition to
193  * allocating memeory, the FCP CMND and FCP RSP BDEs are setup in the BPL
194  * and the BPL BDE is setup in the IOCB.
195  */
196 static struct lpfc_scsi_buf *
197 lpfc_new_scsi_buf(struct lpfc_vport *vport)
198 {
199         struct lpfc_hba *phba = vport->phba;
200         struct lpfc_scsi_buf *psb;
201         struct ulp_bde64 *bpl;
202         IOCB_t *iocb;
203         dma_addr_t pdma_phys;
204         uint16_t iotag;
205
206         psb = kmalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
207         if (!psb)
208                 return NULL;
209         memset(psb, 0, sizeof (struct lpfc_scsi_buf));
210
211         /*
212          * Get memory from the pci pool to map the virt space to pci bus space
213          * for an I/O.  The DMA buffer includes space for the struct fcp_cmnd,
214          * struct fcp_rsp and the number of bde's necessary to support the
215          * sg_tablesize.
216          */
217         psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool, GFP_KERNEL,
218                                                         &psb->dma_handle);
219         if (!psb->data) {
220                 kfree(psb);
221                 return NULL;
222         }
223
224         /* Initialize virtual ptrs to dma_buf region. */
225         memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
226
227         /* Allocate iotag for psb->cur_iocbq. */
228         iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
229         if (iotag == 0) {
230                 pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
231                               psb->data, psb->dma_handle);
232                 kfree (psb);
233                 return NULL;
234         }
235         psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
236
237         psb->fcp_cmnd = psb->data;
238         psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
239         psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
240                                                         sizeof(struct fcp_rsp);
241
242         /* Initialize local short-hand pointers. */
243         bpl = psb->fcp_bpl;
244         pdma_phys = psb->dma_handle;
245
246         /*
247          * The first two bdes are the FCP_CMD and FCP_RSP.  The balance are sg
248          * list bdes.  Initialize the first two and leave the rest for
249          * queuecommand.
250          */
251         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
252         bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
253         bpl->tus.f.bdeSize = sizeof (struct fcp_cmnd);
254         bpl->tus.f.bdeFlags = BUFF_USE_CMND;
255         bpl->tus.w = le32_to_cpu(bpl->tus.w);
256         bpl++;
257
258         /* Setup the physical region for the FCP RSP */
259         pdma_phys += sizeof (struct fcp_cmnd);
260         bpl->addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys));
261         bpl->addrLow = le32_to_cpu(putPaddrLow(pdma_phys));
262         bpl->tus.f.bdeSize = sizeof (struct fcp_rsp);
263         bpl->tus.f.bdeFlags = (BUFF_USE_CMND | BUFF_USE_RCV);
264         bpl->tus.w = le32_to_cpu(bpl->tus.w);
265
266         /*
267          * Since the IOCB for the FCP I/O is built into this lpfc_scsi_buf,
268          * initialize it with all known data now.
269          */
270         pdma_phys += (sizeof (struct fcp_rsp));
271         iocb = &psb->cur_iocbq.iocb;
272         iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
273         iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys);
274         iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys);
275         iocb->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
276         iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDL;
277         iocb->ulpBdeCount = 1;
278         iocb->ulpClass = CLASS3;
279
280         return psb;
281 }
282
283 static struct lpfc_scsi_buf*
284 lpfc_get_scsi_buf(struct lpfc_hba * phba)
285 {
286         struct  lpfc_scsi_buf * lpfc_cmd = NULL;
287         struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
288         unsigned long iflag = 0;
289
290         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
291         list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
292         if (lpfc_cmd) {
293                 lpfc_cmd->seg_cnt = 0;
294                 lpfc_cmd->nonsg_phys = 0;
295         }
296         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
297         return  lpfc_cmd;
298 }
299
300 static void
301 lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
302 {
303         unsigned long iflag = 0;
304
305         spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
306         psb->pCmd = NULL;
307         list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
308         spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
309 }
310
311 static int
312 lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
313 {
314         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
315         struct scatterlist *sgel = NULL;
316         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
317         struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
318         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
319         dma_addr_t physaddr;
320         uint32_t i, num_bde = 0;
321         int nseg, datadir = scsi_cmnd->sc_data_direction;
322
323         /*
324          * There are three possibilities here - use scatter-gather segment, use
325          * the single mapping, or neither.  Start the lpfc command prep by
326          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
327          * data bde entry.
328          */
329         bpl += 2;
330         if (scsi_sg_count(scsi_cmnd)) {
331                 /*
332                  * The driver stores the segment count returned from pci_map_sg
333                  * because this a count of dma-mappings used to map the use_sg
334                  * pages.  They are not guaranteed to be the same for those
335                  * architectures that implement an IOMMU.
336                  */
337
338                 nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
339                                   scsi_sg_count(scsi_cmnd), datadir);
340                 if (unlikely(!nseg))
341                         return 1;
342
343                 lpfc_cmd->seg_cnt = nseg;
344                 if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
345                         printk(KERN_ERR "%s: Too many sg segments from "
346                                "dma_map_sg.  Config %d, seg_cnt %d",
347                                __FUNCTION__, phba->cfg_sg_seg_cnt,
348                                lpfc_cmd->seg_cnt);
349                         scsi_dma_unmap(scsi_cmnd);
350                         return 1;
351                 }
352
353                 /*
354                  * The driver established a maximum scatter-gather segment count
355                  * during probe that limits the number of sg elements in any
356                  * single scsi command.  Just run through the seg_cnt and format
357                  * the bde's.
358                  */
359                 scsi_for_each_sg(scsi_cmnd, sgel, nseg, i) {
360                         physaddr = sg_dma_address(sgel);
361                         bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
362                         bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
363                         bpl->tus.f.bdeSize = sg_dma_len(sgel);
364                         if (datadir == DMA_TO_DEVICE)
365                                 bpl->tus.f.bdeFlags = 0;
366                         else
367                                 bpl->tus.f.bdeFlags = BUFF_USE_RCV;
368                         bpl->tus.w = le32_to_cpu(bpl->tus.w);
369                         bpl++;
370                         num_bde++;
371                 }
372         }
373
374         /*
375          * Finish initializing those IOCB fields that are dependent on the
376          * scsi_cmnd request_buffer.  Note that the bdeSize is explicitly
377          * reinitialized since all iocb memory resources are used many times
378          * for transmit, receive, and continuation bpl's.
379          */
380         iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof (struct ulp_bde64));
381         iocb_cmd->un.fcpi64.bdl.bdeSize +=
382                 (num_bde * sizeof (struct ulp_bde64));
383         iocb_cmd->ulpBdeCount = 1;
384         iocb_cmd->ulpLe = 1;
385         fcp_cmnd->fcpDl = be32_to_cpu(scsi_bufflen(scsi_cmnd));
386         return 0;
387 }
388
389 static void
390 lpfc_scsi_unprep_dma_buf(struct lpfc_hba * phba, struct lpfc_scsi_buf * psb)
391 {
392         /*
393          * There are only two special cases to consider.  (1) the scsi command
394          * requested scatter-gather usage or (2) the scsi command allocated
395          * a request buffer, but did not request use_sg.  There is a third
396          * case, but it does not require resource deallocation.
397          */
398         if (psb->seg_cnt > 0)
399                 scsi_dma_unmap(psb->pCmd);
400 }
401
402 static void
403 lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
404                     struct lpfc_iocbq *rsp_iocb)
405 {
406         struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
407         struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
408         struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
409         uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
410         uint32_t resp_info = fcprsp->rspStatus2;
411         uint32_t scsi_status = fcprsp->rspStatus3;
412         uint32_t *lp;
413         uint32_t host_status = DID_OK;
414         uint32_t rsplen = 0;
415         uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
416
417         /*
418          *  If this is a task management command, there is no
419          *  scsi packet associated with this lpfc_cmd.  The driver
420          *  consumes it.
421          */
422         if (fcpcmd->fcpCntl2) {
423                 scsi_status = 0;
424                 goto out;
425         }
426
427         if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
428                 uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
429                 if (snslen > SCSI_SENSE_BUFFERSIZE)
430                         snslen = SCSI_SENSE_BUFFERSIZE;
431
432                 if (resp_info & RSP_LEN_VALID)
433                   rsplen = be32_to_cpu(fcprsp->rspRspLen);
434                 memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
435         }
436         lp = (uint32_t *)cmnd->sense_buffer;
437
438         if (!scsi_status && (resp_info & RESID_UNDER))
439                 logit = LOG_FCP;
440
441         lpfc_printf_vlog(vport, KERN_WARNING, logit,
442                          "0730 FCP command x%x failed: x%x SNS x%x x%x "
443                          "Data: x%x x%x x%x x%x x%x\n",
444                          cmnd->cmnd[0], scsi_status,
445                          be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
446                          be32_to_cpu(fcprsp->rspResId),
447                          be32_to_cpu(fcprsp->rspSnsLen),
448                          be32_to_cpu(fcprsp->rspRspLen),
449                          fcprsp->rspInfo3);
450
451         if (resp_info & RSP_LEN_VALID) {
452                 rsplen = be32_to_cpu(fcprsp->rspRspLen);
453                 if ((rsplen != 0 && rsplen != 4 && rsplen != 8) ||
454                     (fcprsp->rspInfo3 != RSP_NO_FAILURE)) {
455                         host_status = DID_ERROR;
456                         goto out;
457                 }
458         }
459
460         scsi_set_resid(cmnd, 0);
461         if (resp_info & RESID_UNDER) {
462                 scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
463
464                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
465                                  "0716 FCP Read Underrun, expected %d, "
466                                  "residual %d Data: x%x x%x x%x\n",
467                                  be32_to_cpu(fcpcmd->fcpDl),
468                                  scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
469                                  cmnd->underflow);
470
471                 /*
472                  * If there is an under run check if under run reported by
473                  * storage array is same as the under run reported by HBA.
474                  * If this is not same, there is a dropped frame.
475                  */
476                 if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
477                         fcpi_parm &&
478                         (scsi_get_resid(cmnd) != fcpi_parm)) {
479                         lpfc_printf_vlog(vport, KERN_WARNING,
480                                          LOG_FCP | LOG_FCP_ERROR,
481                                          "0735 FCP Read Check Error "
482                                          "and Underrun Data: x%x x%x x%x x%x\n",
483                                          be32_to_cpu(fcpcmd->fcpDl),
484                                          scsi_get_resid(cmnd), fcpi_parm,
485                                          cmnd->cmnd[0]);
486                         scsi_set_resid(cmnd, scsi_bufflen(cmnd));
487                         host_status = DID_ERROR;
488                 }
489                 /*
490                  * The cmnd->underflow is the minimum number of bytes that must
491                  * be transfered for this command.  Provided a sense condition
492                  * is not present, make sure the actual amount transferred is at
493                  * least the underflow value or fail.
494                  */
495                 if (!(resp_info & SNS_LEN_VALID) &&
496                     (scsi_status == SAM_STAT_GOOD) &&
497                     (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
498                      < cmnd->underflow)) {
499                         lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
500                                          "0717 FCP command x%x residual "
501                                          "underrun converted to error "
502                                          "Data: x%x x%x x%x\n",
503                                          cmnd->cmnd[0], cmnd->request_bufflen,
504                                          scsi_get_resid(cmnd), cmnd->underflow);
505                         host_status = DID_ERROR;
506                 }
507         } else if (resp_info & RESID_OVER) {
508                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
509                                  "0720 FCP command x%x residual overrun error. "
510                                  "Data: x%x x%x \n", cmnd->cmnd[0],
511                                  scsi_bufflen(cmnd), scsi_get_resid(cmnd));
512                 host_status = DID_ERROR;
513
514         /*
515          * Check SLI validation that all the transfer was actually done
516          * (fcpi_parm should be zero). Apply check only to reads.
517          */
518         } else if ((scsi_status == SAM_STAT_GOOD) && fcpi_parm &&
519                         (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
520                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
521                                  "0734 FCP Read Check Error Data: "
522                                  "x%x x%x x%x x%x\n",
523                                  be32_to_cpu(fcpcmd->fcpDl),
524                                  be32_to_cpu(fcprsp->rspResId),
525                                  fcpi_parm, cmnd->cmnd[0]);
526                 host_status = DID_ERROR;
527                 scsi_set_resid(cmnd, scsi_bufflen(cmnd));
528         }
529
530  out:
531         cmnd->result = ScsiResult(host_status, scsi_status);
532 }
533
534 static void
535 lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
536                         struct lpfc_iocbq *pIocbOut)
537 {
538         struct lpfc_scsi_buf *lpfc_cmd =
539                 (struct lpfc_scsi_buf *) pIocbIn->context1;
540         struct lpfc_vport      *vport = pIocbIn->vport;
541         struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
542         struct lpfc_nodelist *pnode = rdata->pnode;
543         struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
544         int result;
545         struct scsi_device *sdev, *tmp_sdev;
546         int depth = 0;
547
548         lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
549         lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
550
551         if (lpfc_cmd->status) {
552                 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
553                     (lpfc_cmd->result & IOERR_DRVR_MASK))
554                         lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
555                 else if (lpfc_cmd->status >= IOSTAT_CNT)
556                         lpfc_cmd->status = IOSTAT_DEFAULT;
557
558                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
559                                  "0729 FCP cmd x%x failed <%d/%d> "
560                                  "status: x%x result: x%x Data: x%x x%x\n",
561                                  cmd->cmnd[0],
562                                  cmd->device ? cmd->device->id : 0xffff,
563                                  cmd->device ? cmd->device->lun : 0xffff,
564                                  lpfc_cmd->status, lpfc_cmd->result,
565                                  pIocbOut->iocb.ulpContext,
566                                  lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
567
568                 switch (lpfc_cmd->status) {
569                 case IOSTAT_FCP_RSP_ERROR:
570                         /* Call FCP RSP handler to determine result */
571                         lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
572                         break;
573                 case IOSTAT_NPORT_BSY:
574                 case IOSTAT_FABRIC_BSY:
575                         cmd->result = ScsiResult(DID_BUS_BUSY, 0);
576                         break;
577                 case IOSTAT_LOCAL_REJECT:
578                         if (lpfc_cmd->result == RJT_UNAVAIL_PERM ||
579                             lpfc_cmd->result == IOERR_NO_RESOURCES ||
580                             lpfc_cmd->result == RJT_LOGIN_REQUIRED) {
581                                 cmd->result = ScsiResult(DID_REQUEUE, 0);
582                         break;
583                 } /* else: fall through */
584                 default:
585                         cmd->result = ScsiResult(DID_ERROR, 0);
586                         break;
587                 }
588
589                 if ((pnode == NULL )
590                     || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
591                         cmd->result = ScsiResult(DID_BUS_BUSY, SAM_STAT_BUSY);
592         } else {
593                 cmd->result = ScsiResult(DID_OK, 0);
594         }
595
596         if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
597                 uint32_t *lp = (uint32_t *)cmd->sense_buffer;
598
599                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
600                                  "0710 Iodone <%d/%d> cmd %p, error "
601                                  "x%x SNS x%x x%x Data: x%x x%x\n",
602                                  cmd->device->id, cmd->device->lun, cmd,
603                                  cmd->result, *lp, *(lp + 3), cmd->retries,
604                                  scsi_get_resid(cmd));
605         }
606
607         result = cmd->result;
608         sdev = cmd->device;
609         lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
610         cmd->scsi_done(cmd);
611
612         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
613                 lpfc_release_scsi_buf(phba, lpfc_cmd);
614                 return;
615         }
616
617
618         if (!result)
619                 lpfc_rampup_queue_depth(vport, sdev);
620
621         if (!result && pnode != NULL &&
622            ((jiffies - pnode->last_ramp_up_time) >
623                 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
624            ((jiffies - pnode->last_q_full_time) >
625                 LPFC_Q_RAMP_UP_INTERVAL * HZ) &&
626            (vport->cfg_lun_queue_depth > sdev->queue_depth)) {
627                 shost_for_each_device(tmp_sdev, sdev->host) {
628                         if (vport->cfg_lun_queue_depth > tmp_sdev->queue_depth){
629                                 if (tmp_sdev->id != sdev->id)
630                                         continue;
631                                 if (tmp_sdev->ordered_tags)
632                                         scsi_adjust_queue_depth(tmp_sdev,
633                                                 MSG_ORDERED_TAG,
634                                                 tmp_sdev->queue_depth+1);
635                                 else
636                                         scsi_adjust_queue_depth(tmp_sdev,
637                                                 MSG_SIMPLE_TAG,
638                                                 tmp_sdev->queue_depth+1);
639
640                                 pnode->last_ramp_up_time = jiffies;
641                         }
642                 }
643         }
644
645         /*
646          * Check for queue full.  If the lun is reporting queue full, then
647          * back off the lun queue depth to prevent target overloads.
648          */
649         if (result == SAM_STAT_TASK_SET_FULL && pnode != NULL) {
650                 pnode->last_q_full_time = jiffies;
651
652                 shost_for_each_device(tmp_sdev, sdev->host) {
653                         if (tmp_sdev->id != sdev->id)
654                                 continue;
655                         depth = scsi_track_queue_full(tmp_sdev,
656                                         tmp_sdev->queue_depth - 1);
657                 }
658                 /*
659                  * The queue depth cannot be lowered any more.
660                  * Modify the returned error code to store
661                  * the final depth value set by
662                  * scsi_track_queue_full.
663                  */
664                 if (depth == -1)
665                         depth = sdev->host->cmd_per_lun;
666
667                 if (depth) {
668                         lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
669                                          "0711 detected queue full - lun queue "
670                                          "depth adjusted to %d.\n", depth);
671                 }
672         }
673
674         lpfc_release_scsi_buf(phba, lpfc_cmd);
675 }
676
677 static void
678 lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
679                     struct lpfc_nodelist *pnode)
680 {
681         struct lpfc_hba *phba = vport->phba;
682         struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
683         struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
684         IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
685         struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
686         int datadir = scsi_cmnd->sc_data_direction;
687
688         lpfc_cmd->fcp_rsp->rspSnsLen = 0;
689         /* clear task management bits */
690         lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
691
692         int_to_scsilun(lpfc_cmd->pCmd->device->lun,
693                         &lpfc_cmd->fcp_cmnd->fcp_lun);
694
695         memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
696
697         if (scsi_cmnd->device->tagged_supported) {
698                 switch (scsi_cmnd->tag) {
699                 case HEAD_OF_QUEUE_TAG:
700                         fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
701                         break;
702                 case ORDERED_QUEUE_TAG:
703                         fcp_cmnd->fcpCntl1 = ORDERED_Q;
704                         break;
705                 default:
706                         fcp_cmnd->fcpCntl1 = SIMPLE_Q;
707                         break;
708                 }
709         } else
710                 fcp_cmnd->fcpCntl1 = 0;
711
712         /*
713          * There are three possibilities here - use scatter-gather segment, use
714          * the single mapping, or neither.  Start the lpfc command prep by
715          * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
716          * data bde entry.
717          */
718         if (scsi_sg_count(scsi_cmnd)) {
719                 if (datadir == DMA_TO_DEVICE) {
720                         iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
721                         iocb_cmd->un.fcpi.fcpi_parm = 0;
722                         iocb_cmd->ulpPU = 0;
723                         fcp_cmnd->fcpCntl3 = WRITE_DATA;
724                         phba->fc4OutputRequests++;
725                 } else {
726                         iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
727                         iocb_cmd->ulpPU = PARM_READ_CHECK;
728                         iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
729                         fcp_cmnd->fcpCntl3 = READ_DATA;
730                         phba->fc4InputRequests++;
731                 }
732         } else {
733                 iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
734                 iocb_cmd->un.fcpi.fcpi_parm = 0;
735                 iocb_cmd->ulpPU = 0;
736                 fcp_cmnd->fcpCntl3 = 0;
737                 phba->fc4ControlRequests++;
738         }
739
740         /*
741          * Finish initializing those IOCB fields that are independent
742          * of the scsi_cmnd request_buffer
743          */
744         piocbq->iocb.ulpContext = pnode->nlp_rpi;
745         if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
746                 piocbq->iocb.ulpFCP2Rcvy = 1;
747
748         piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
749         piocbq->context1  = lpfc_cmd;
750         piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
751         piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
752         piocbq->vport = vport;
753 }
754
755 static int
756 lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
757                              struct lpfc_scsi_buf *lpfc_cmd,
758                              unsigned int lun,
759                              uint8_t task_mgmt_cmd)
760 {
761         struct lpfc_iocbq *piocbq;
762         IOCB_t *piocb;
763         struct fcp_cmnd *fcp_cmnd;
764         struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
765         struct lpfc_nodelist *ndlp = rdata->pnode;
766
767         if ((ndlp == NULL) || (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
768                 return 0;
769         }
770
771         piocbq = &(lpfc_cmd->cur_iocbq);
772         piocbq->vport = vport;
773
774         piocb = &piocbq->iocb;
775
776         fcp_cmnd = lpfc_cmd->fcp_cmnd;
777         int_to_scsilun(lun, &lpfc_cmd->fcp_cmnd->fcp_lun);
778         fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
779
780         piocb->ulpCommand = CMD_FCP_ICMND64_CR;
781
782         piocb->ulpContext = ndlp->nlp_rpi;
783         if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
784                 piocb->ulpFCP2Rcvy = 1;
785         }
786         piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
787
788         /* ulpTimeout is only one byte */
789         if (lpfc_cmd->timeout > 0xff) {
790                 /*
791                  * Do not timeout the command at the firmware level.
792                  * The driver will provide the timeout mechanism.
793                  */
794                 piocb->ulpTimeout = 0;
795         } else {
796                 piocb->ulpTimeout = lpfc_cmd->timeout;
797         }
798
799         return 1;
800 }
801
802 static void
803 lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
804                         struct lpfc_iocbq *cmdiocbq,
805                         struct lpfc_iocbq *rspiocbq)
806 {
807         struct lpfc_scsi_buf *lpfc_cmd =
808                 (struct lpfc_scsi_buf *) cmdiocbq->context1;
809         if (lpfc_cmd)
810                 lpfc_release_scsi_buf(phba, lpfc_cmd);
811         return;
812 }
813
814 static int
815 lpfc_scsi_tgt_reset(struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_vport *vport,
816                     unsigned  tgt_id, unsigned int lun,
817                     struct lpfc_rport_data *rdata)
818 {
819         struct lpfc_hba   *phba = vport->phba;
820         struct lpfc_iocbq *iocbq;
821         struct lpfc_iocbq *iocbqrsp;
822         int ret;
823
824         if (!rdata->pnode)
825                 return FAILED;
826
827         lpfc_cmd->rdata = rdata;
828         ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun,
829                                            FCP_TARGET_RESET);
830         if (!ret)
831                 return FAILED;
832
833         iocbq = &lpfc_cmd->cur_iocbq;
834         iocbqrsp = lpfc_sli_get_iocbq(phba);
835
836         if (!iocbqrsp)
837                 return FAILED;
838
839         /* Issue Target Reset to TGT <num> */
840         lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
841                          "0702 Issue Target Reset to TGT %d Data: x%x x%x\n",
842                          tgt_id, rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
843         ret = lpfc_sli_issue_iocb_wait(phba,
844                                        &phba->sli.ring[phba->sli.fcp_ring],
845                                        iocbq, iocbqrsp, lpfc_cmd->timeout);
846         if (ret != IOCB_SUCCESS) {
847                 if (ret == IOCB_TIMEDOUT)
848                         iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
849                 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
850         } else {
851                 ret = SUCCESS;
852                 lpfc_cmd->result = iocbqrsp->iocb.un.ulpWord[4];
853                 lpfc_cmd->status = iocbqrsp->iocb.ulpStatus;
854                 if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
855                         (lpfc_cmd->result & IOERR_DRVR_MASK))
856                                 lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
857         }
858
859         lpfc_sli_release_iocbq(phba, iocbqrsp);
860         return ret;
861 }
862
863 const char *
864 lpfc_info(struct Scsi_Host *host)
865 {
866         struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
867         struct lpfc_hba   *phba = vport->phba;
868         int len;
869         static char  lpfcinfobuf[384];
870
871         memset(lpfcinfobuf,0,384);
872         if (phba && phba->pcidev){
873                 strncpy(lpfcinfobuf, phba->ModelDesc, 256);
874                 len = strlen(lpfcinfobuf);
875                 snprintf(lpfcinfobuf + len,
876                         384-len,
877                         " on PCI bus %02x device %02x irq %d",
878                         phba->pcidev->bus->number,
879                         phba->pcidev->devfn,
880                         phba->pcidev->irq);
881                 len = strlen(lpfcinfobuf);
882                 if (phba->Port[0]) {
883                         snprintf(lpfcinfobuf + len,
884                                  384-len,
885                                  " port %s",
886                                  phba->Port);
887                 }
888         }
889         return lpfcinfobuf;
890 }
891
892 static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
893 {
894         unsigned long  poll_tmo_expires =
895                 (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
896
897         if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
898                 mod_timer(&phba->fcp_poll_timer,
899                           poll_tmo_expires);
900 }
901
902 void lpfc_poll_start_timer(struct lpfc_hba * phba)
903 {
904         lpfc_poll_rearm_timer(phba);
905 }
906
907 void lpfc_poll_timeout(unsigned long ptr)
908 {
909         struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
910
911         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
912                 lpfc_sli_poll_fcp_ring (phba);
913                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
914                         lpfc_poll_rearm_timer(phba);
915         }
916 }
917
918 static int
919 lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
920 {
921         struct Scsi_Host  *shost = cmnd->device->host;
922         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
923         struct lpfc_hba   *phba = vport->phba;
924         struct lpfc_sli   *psli = &phba->sli;
925         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
926         struct lpfc_nodelist *ndlp = rdata->pnode;
927         struct lpfc_scsi_buf *lpfc_cmd;
928         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
929         int err;
930
931         err = fc_remote_port_chkready(rport);
932         if (err) {
933                 cmnd->result = err;
934                 goto out_fail_command;
935         }
936
937         /*
938          * Catch race where our node has transitioned, but the
939          * transport is still transitioning.
940          */
941         if (!ndlp) {
942                 cmnd->result = ScsiResult(DID_BUS_BUSY, 0);
943                 goto out_fail_command;
944         }
945         lpfc_cmd = lpfc_get_scsi_buf(phba);
946         if (lpfc_cmd == NULL) {
947                 lpfc_adjust_queue_depth(phba);
948
949                 lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
950                                  "0707 driver's buffer pool is empty, "
951                                  "IO busied\n");
952                 goto out_host_busy;
953         }
954
955         /*
956          * Store the midlayer's command structure for the completion phase
957          * and complete the command initialization.
958          */
959         lpfc_cmd->pCmd  = cmnd;
960         lpfc_cmd->rdata = rdata;
961         lpfc_cmd->timeout = 0;
962         cmnd->host_scribble = (unsigned char *)lpfc_cmd;
963         cmnd->scsi_done = done;
964
965         err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
966         if (err)
967                 goto out_host_busy_free_buf;
968
969         lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
970
971         err = lpfc_sli_issue_iocb(phba, &phba->sli.ring[psli->fcp_ring],
972                                   &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
973         if (err)
974                 goto out_host_busy_free_buf;
975
976         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
977                 lpfc_sli_poll_fcp_ring(phba);
978                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
979                         lpfc_poll_rearm_timer(phba);
980         }
981
982         return 0;
983
984  out_host_busy_free_buf:
985         lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
986         lpfc_release_scsi_buf(phba, lpfc_cmd);
987  out_host_busy:
988         return SCSI_MLQUEUE_HOST_BUSY;
989
990  out_fail_command:
991         done(cmnd);
992         return 0;
993 }
994
995 static void
996 lpfc_block_error_handler(struct scsi_cmnd *cmnd)
997 {
998         struct Scsi_Host *shost = cmnd->device->host;
999         struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
1000
1001         spin_lock_irq(shost->host_lock);
1002         while (rport->port_state == FC_PORTSTATE_BLOCKED) {
1003                 spin_unlock_irq(shost->host_lock);
1004                 msleep(1000);
1005                 spin_lock_irq(shost->host_lock);
1006         }
1007         spin_unlock_irq(shost->host_lock);
1008         return;
1009 }
1010
1011 static int
1012 lpfc_abort_handler(struct scsi_cmnd *cmnd)
1013 {
1014         struct Scsi_Host  *shost = cmnd->device->host;
1015         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1016         struct lpfc_hba   *phba = vport->phba;
1017         struct lpfc_sli_ring *pring = &phba->sli.ring[phba->sli.fcp_ring];
1018         struct lpfc_iocbq *iocb;
1019         struct lpfc_iocbq *abtsiocb;
1020         struct lpfc_scsi_buf *lpfc_cmd;
1021         IOCB_t *cmd, *icmd;
1022         unsigned int loop_count = 0;
1023         int ret = SUCCESS;
1024
1025         lpfc_block_error_handler(cmnd);
1026         lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
1027         BUG_ON(!lpfc_cmd);
1028
1029         /*
1030          * If pCmd field of the corresponding lpfc_scsi_buf structure
1031          * points to a different SCSI command, then the driver has
1032          * already completed this command, but the midlayer did not
1033          * see the completion before the eh fired.  Just return
1034          * SUCCESS.
1035          */
1036         iocb = &lpfc_cmd->cur_iocbq;
1037         if (lpfc_cmd->pCmd != cmnd)
1038                 goto out;
1039
1040         BUG_ON(iocb->context1 != lpfc_cmd);
1041
1042         abtsiocb = lpfc_sli_get_iocbq(phba);
1043         if (abtsiocb == NULL) {
1044                 ret = FAILED;
1045                 goto out;
1046         }
1047
1048         /*
1049          * The scsi command can not be in txq and it is in flight because the
1050          * pCmd is still pointig at the SCSI command we have to abort. There
1051          * is no need to search the txcmplq. Just send an abort to the FW.
1052          */
1053
1054         cmd = &iocb->iocb;
1055         icmd = &abtsiocb->iocb;
1056         icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
1057         icmd->un.acxri.abortContextTag = cmd->ulpContext;
1058         icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
1059
1060         icmd->ulpLe = 1;
1061         icmd->ulpClass = cmd->ulpClass;
1062         if (lpfc_is_link_up(phba))
1063                 icmd->ulpCommand = CMD_ABORT_XRI_CN;
1064         else
1065                 icmd->ulpCommand = CMD_CLOSE_XRI_CN;
1066
1067         abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
1068         abtsiocb->vport = vport;
1069         if (lpfc_sli_issue_iocb(phba, pring, abtsiocb, 0) == IOCB_ERROR) {
1070                 lpfc_sli_release_iocbq(phba, abtsiocb);
1071                 ret = FAILED;
1072                 goto out;
1073         }
1074
1075         if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1076                 lpfc_sli_poll_fcp_ring (phba);
1077
1078         /* Wait for abort to complete */
1079         while (lpfc_cmd->pCmd == cmnd)
1080         {
1081                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1082                         lpfc_sli_poll_fcp_ring (phba);
1083
1084                 schedule_timeout_uninterruptible(LPFC_ABORT_WAIT * HZ);
1085                 if (++loop_count
1086                     > (2 * vport->cfg_devloss_tmo)/LPFC_ABORT_WAIT)
1087                         break;
1088         }
1089
1090         if (lpfc_cmd->pCmd == cmnd) {
1091                 ret = FAILED;
1092                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1093                                  "0748 abort handler timed out waiting "
1094                                  "for abort to complete: ret %#x, ID %d, "
1095                                  "LUN %d, snum %#lx\n",
1096                                  ret, cmnd->device->id, cmnd->device->lun,
1097                                  cmnd->serial_number);
1098         }
1099
1100  out:
1101         lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1102                          "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
1103                          "LUN %d snum %#lx\n", ret, cmnd->device->id,
1104                          cmnd->device->lun, cmnd->serial_number);
1105         return ret;
1106 }
1107
1108 static int
1109 lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
1110 {
1111         struct Scsi_Host  *shost = cmnd->device->host;
1112         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1113         struct lpfc_hba   *phba = vport->phba;
1114         struct lpfc_scsi_buf *lpfc_cmd;
1115         struct lpfc_iocbq *iocbq, *iocbqrsp;
1116         struct lpfc_rport_data *rdata = cmnd->device->hostdata;
1117         struct lpfc_nodelist *pnode = rdata->pnode;
1118         uint32_t cmd_result = 0, cmd_status = 0;
1119         int ret = FAILED;
1120         int iocb_status = IOCB_SUCCESS;
1121         int cnt, loopcnt;
1122
1123         lpfc_block_error_handler(cmnd);
1124         loopcnt = 0;
1125         /*
1126          * If target is not in a MAPPED state, delay the reset until
1127          * target is rediscovered or devloss timeout expires.
1128          */
1129         while (1) {
1130                 if (!pnode)
1131                         goto out;
1132
1133                 if (pnode->nlp_state != NLP_STE_MAPPED_NODE) {
1134                         schedule_timeout_uninterruptible(msecs_to_jiffies(500));
1135                         loopcnt++;
1136                         rdata = cmnd->device->hostdata;
1137                         if (!rdata ||
1138                                 (loopcnt > ((vport->cfg_devloss_tmo * 2) + 1))){
1139                                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1140                                                  "0721 LUN Reset rport "
1141                                                  "failure: cnt x%x rdata x%p\n",
1142                                                  loopcnt, rdata);
1143                                 goto out;
1144                         }
1145                         pnode = rdata->pnode;
1146                         if (!pnode)
1147                                 goto out;
1148                 }
1149                 if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
1150                         break;
1151         }
1152
1153         lpfc_cmd = lpfc_get_scsi_buf(phba);
1154         if (lpfc_cmd == NULL)
1155                 goto out;
1156
1157         lpfc_cmd->timeout = 60;
1158         lpfc_cmd->rdata = rdata;
1159
1160         ret = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, cmnd->device->lun,
1161                                            FCP_TARGET_RESET);
1162         if (!ret)
1163                 goto out_free_scsi_buf;
1164
1165         iocbq = &lpfc_cmd->cur_iocbq;
1166
1167         /* get a buffer for this IOCB command response */
1168         iocbqrsp = lpfc_sli_get_iocbq(phba);
1169         if (iocbqrsp == NULL)
1170                 goto out_free_scsi_buf;
1171
1172         lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
1173                          "0703 Issue target reset to TGT %d LUN %d "
1174                          "rpi x%x nlp_flag x%x\n", cmnd->device->id,
1175                          cmnd->device->lun, pnode->nlp_rpi, pnode->nlp_flag);
1176         iocb_status = lpfc_sli_issue_iocb_wait(phba,
1177                                        &phba->sli.ring[phba->sli.fcp_ring],
1178                                        iocbq, iocbqrsp, lpfc_cmd->timeout);
1179
1180         if (iocb_status == IOCB_TIMEDOUT)
1181                 iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
1182
1183         if (iocb_status == IOCB_SUCCESS)
1184                 ret = SUCCESS;
1185         else
1186                 ret = iocb_status;
1187
1188         cmd_result = iocbqrsp->iocb.un.ulpWord[4];
1189         cmd_status = iocbqrsp->iocb.ulpStatus;
1190
1191         lpfc_sli_release_iocbq(phba, iocbqrsp);
1192
1193         /*
1194          * All outstanding txcmplq I/Os should have been aborted by the device.
1195          * Unfortunately, some targets do not abide by this forcing the driver
1196          * to double check.
1197          */
1198         cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1199                                 cmnd->device->id, cmnd->device->lun,
1200                                 LPFC_CTX_LUN);
1201         if (cnt)
1202                 lpfc_sli_abort_iocb(phba,
1203                                     &phba->sli.ring[phba->sli.fcp_ring],
1204                                     cmnd->device->id, cmnd->device->lun,
1205                                     0, LPFC_CTX_LUN);
1206         loopcnt = 0;
1207         while(cnt) {
1208                 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
1209
1210                 if (++loopcnt
1211                     > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
1212                         break;
1213
1214                 cnt = lpfc_sli_sum_iocb(phba,
1215                                         &phba->sli.ring[phba->sli.fcp_ring],
1216                                         cmnd->device->id, cmnd->device->lun,
1217                                         LPFC_CTX_LUN);
1218         }
1219
1220         if (cnt) {
1221                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1222                                  "0719 device reset I/O flush failure: "
1223                                  "cnt x%x\n", cnt);
1224                 ret = FAILED;
1225         }
1226
1227 out_free_scsi_buf:
1228         if (iocb_status != IOCB_TIMEDOUT) {
1229                 lpfc_release_scsi_buf(phba, lpfc_cmd);
1230         }
1231         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1232                          "0713 SCSI layer issued device reset (%d, %d) "
1233                          "return x%x status x%x result x%x\n",
1234                          cmnd->device->id, cmnd->device->lun, ret,
1235                          cmd_status, cmd_result);
1236 out:
1237         return ret;
1238 }
1239
1240 static int
1241 lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
1242 {
1243         struct Scsi_Host  *shost = cmnd->device->host;
1244         struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
1245         struct lpfc_hba   *phba = vport->phba;
1246         struct lpfc_nodelist *ndlp = NULL;
1247         int match;
1248         int ret = FAILED, i, err_count = 0;
1249         int cnt, loopcnt;
1250         struct lpfc_scsi_buf * lpfc_cmd;
1251
1252         lpfc_block_error_handler(cmnd);
1253
1254         lpfc_cmd = lpfc_get_scsi_buf(phba);
1255         if (lpfc_cmd == NULL)
1256                 goto out;
1257
1258         /* The lpfc_cmd storage is reused.  Set all loop invariants. */
1259         lpfc_cmd->timeout = 60;
1260
1261         /*
1262          * Since the driver manages a single bus device, reset all
1263          * targets known to the driver.  Should any target reset
1264          * fail, this routine returns failure to the midlayer.
1265          */
1266         for (i = 0; i < LPFC_MAX_TARGET; i++) {
1267                 /* Search for mapped node by target ID */
1268                 match = 0;
1269                 spin_lock_irq(shost->host_lock);
1270                 list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
1271                         if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
1272                             i == ndlp->nlp_sid &&
1273                             ndlp->rport) {
1274                                 match = 1;
1275                                 break;
1276                         }
1277                 }
1278                 spin_unlock_irq(shost->host_lock);
1279                 if (!match)
1280                         continue;
1281
1282                 ret = lpfc_scsi_tgt_reset(lpfc_cmd, vport, i,
1283                                           cmnd->device->lun,
1284                                           ndlp->rport->dd_data);
1285                 if (ret != SUCCESS) {
1286                         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1287                                          "0700 Bus Reset on target %d failed\n",
1288                                          i);
1289                         err_count++;
1290                         break;
1291                 }
1292         }
1293
1294         if (ret != IOCB_TIMEDOUT)
1295                 lpfc_release_scsi_buf(phba, lpfc_cmd);
1296
1297         if (err_count == 0)
1298                 ret = SUCCESS;
1299         else
1300                 ret = FAILED;
1301
1302         /*
1303          * All outstanding txcmplq I/Os should have been aborted by
1304          * the targets.  Unfortunately, some targets do not abide by
1305          * this forcing the driver to double check.
1306          */
1307         cnt = lpfc_sli_sum_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1308                                 0, 0, LPFC_CTX_HOST);
1309         if (cnt)
1310                 lpfc_sli_abort_iocb(phba, &phba->sli.ring[phba->sli.fcp_ring],
1311                                     0, 0, 0, LPFC_CTX_HOST);
1312         loopcnt = 0;
1313         while(cnt) {
1314                 schedule_timeout_uninterruptible(LPFC_RESET_WAIT*HZ);
1315
1316                 if (++loopcnt
1317                     > (2 * vport->cfg_devloss_tmo)/LPFC_RESET_WAIT)
1318                         break;
1319
1320                 cnt = lpfc_sli_sum_iocb(phba,
1321                                         &phba->sli.ring[phba->sli.fcp_ring],
1322                                         0, 0, LPFC_CTX_HOST);
1323         }
1324
1325         if (cnt) {
1326                 lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1327                                  "0715 Bus Reset I/O flush failure: "
1328                                  "cnt x%x left x%x\n", cnt, i);
1329                 ret = FAILED;
1330         }
1331
1332         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1333                          "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
1334 out:
1335         return ret;
1336 }
1337
1338 static int
1339 lpfc_slave_alloc(struct scsi_device *sdev)
1340 {
1341         struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1342         struct lpfc_hba   *phba = vport->phba;
1343         struct lpfc_scsi_buf *scsi_buf = NULL;
1344         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
1345         uint32_t total = 0, i;
1346         uint32_t num_to_alloc = 0;
1347         unsigned long flags;
1348
1349         if (!rport || fc_remote_port_chkready(rport))
1350                 return -ENXIO;
1351
1352         sdev->hostdata = rport->dd_data;
1353
1354         /*
1355          * Populate the cmds_per_lun count scsi_bufs into this host's globally
1356          * available list of scsi buffers.  Don't allocate more than the
1357          * HBA limit conveyed to the midlayer via the host structure.  The
1358          * formula accounts for the lun_queue_depth + error handlers + 1
1359          * extra.  This list of scsi bufs exists for the lifetime of the driver.
1360          */
1361         total = phba->total_scsi_bufs;
1362         num_to_alloc = vport->cfg_lun_queue_depth + 2;
1363
1364         /* Allow some exchanges to be available always to complete discovery */
1365         if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
1366                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1367                                  "0704 At limitation of %d preallocated "
1368                                  "command buffers\n", total);
1369                 return 0;
1370         /* Allow some exchanges to be available always to complete discovery */
1371         } else if (total + num_to_alloc >
1372                 phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
1373                 lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
1374                                  "0705 Allocation request of %d "
1375                                  "command buffers will exceed max of %d.  "
1376                                  "Reducing allocation request to %d.\n",
1377                                  num_to_alloc, phba->cfg_hba_queue_depth,
1378                                  (phba->cfg_hba_queue_depth - total));
1379                 num_to_alloc = phba->cfg_hba_queue_depth - total;
1380         }
1381
1382         for (i = 0; i < num_to_alloc; i++) {
1383                 scsi_buf = lpfc_new_scsi_buf(vport);
1384                 if (!scsi_buf) {
1385                         lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
1386                                          "0706 Failed to allocate "
1387                                          "command buffer\n");
1388                         break;
1389                 }
1390
1391                 spin_lock_irqsave(&phba->scsi_buf_list_lock, flags);
1392                 phba->total_scsi_bufs++;
1393                 list_add_tail(&scsi_buf->list, &phba->lpfc_scsi_buf_list);
1394                 spin_unlock_irqrestore(&phba->scsi_buf_list_lock, flags);
1395         }
1396         return 0;
1397 }
1398
1399 static int
1400 lpfc_slave_configure(struct scsi_device *sdev)
1401 {
1402         struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
1403         struct lpfc_hba   *phba = vport->phba;
1404         struct fc_rport   *rport = starget_to_rport(sdev->sdev_target);
1405
1406         if (sdev->tagged_supported)
1407                 scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
1408         else
1409                 scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
1410
1411         /*
1412          * Initialize the fc transport attributes for the target
1413          * containing this scsi device.  Also note that the driver's
1414          * target pointer is stored in the starget_data for the
1415          * driver's sysfs entry point functions.
1416          */
1417         rport->dev_loss_tmo = vport->cfg_devloss_tmo;
1418
1419         if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
1420                 lpfc_sli_poll_fcp_ring(phba);
1421                 if (phba->cfg_poll & DISABLE_FCP_RING_INT)
1422                         lpfc_poll_rearm_timer(phba);
1423         }
1424
1425         return 0;
1426 }
1427
1428 static void
1429 lpfc_slave_destroy(struct scsi_device *sdev)
1430 {
1431         sdev->hostdata = NULL;
1432         return;
1433 }
1434
1435
1436 struct scsi_host_template lpfc_template = {
1437         .module                 = THIS_MODULE,
1438         .name                   = LPFC_DRIVER_NAME,
1439         .info                   = lpfc_info,
1440         .queuecommand           = lpfc_queuecommand,
1441         .eh_abort_handler       = lpfc_abort_handler,
1442         .eh_device_reset_handler= lpfc_device_reset_handler,
1443         .eh_bus_reset_handler   = lpfc_bus_reset_handler,
1444         .slave_alloc            = lpfc_slave_alloc,
1445         .slave_configure        = lpfc_slave_configure,
1446         .slave_destroy          = lpfc_slave_destroy,
1447         .scan_finished          = lpfc_scan_finished,
1448         .this_id                = -1,
1449         .sg_tablesize           = LPFC_SG_SEG_CNT,
1450         .cmd_per_lun            = LPFC_CMD_PER_LUN,
1451         .use_clustering         = ENABLE_CLUSTERING,
1452         .shost_attrs            = lpfc_hba_attrs,
1453         .max_sectors            = 0xFFFF,
1454 };
1455
1456 struct scsi_host_template lpfc_vport_template = {
1457         .module                 = THIS_MODULE,
1458         .name                   = LPFC_DRIVER_NAME,
1459         .info                   = lpfc_info,
1460         .queuecommand           = lpfc_queuecommand,
1461         .eh_abort_handler       = lpfc_abort_handler,
1462         .eh_device_reset_handler= lpfc_device_reset_handler,
1463         .eh_bus_reset_handler   = lpfc_bus_reset_handler,
1464         .slave_alloc            = lpfc_slave_alloc,
1465         .slave_configure        = lpfc_slave_configure,
1466         .slave_destroy          = lpfc_slave_destroy,
1467         .scan_finished          = lpfc_scan_finished,
1468         .this_id                = -1,
1469         .sg_tablesize           = LPFC_SG_SEG_CNT,
1470         .cmd_per_lun            = LPFC_CMD_PER_LUN,
1471         .use_clustering         = ENABLE_CLUSTERING,
1472         .shost_attrs            = lpfc_vport_attrs,
1473         .max_sectors            = 0xFFFF,
1474 };