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