merge by hand (fix up qla_os.c merge error)
[linux-2.6.git] / drivers / scsi / qla2xxx / qla_os.c
1 /*
2  *                  QLOGIC LINUX SOFTWARE
3  *
4  * QLogic ISP2x00 device driver for Linux 2.6.x
5  * Copyright (C) 2003-2004 QLogic Corporation
6  * (www.qlogic.com)
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2, or (at your option) any
11  * later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  */
19 #include "qla_def.h"
20
21 #include <linux/moduleparam.h>
22 #include <linux/vmalloc.h>
23 #include <linux/smp_lock.h>
24 #include <linux/delay.h>
25
26 #include <scsi/scsi_tcq.h>
27 #include <scsi/scsicam.h>
28 #include <scsi/scsi_transport.h>
29 #include <scsi/scsi_transport_fc.h>
30
31 /*
32  * Driver version
33  */
34 char qla2x00_version_str[40];
35
36 /*
37  * SRB allocation cache
38  */
39 static kmem_cache_t *srb_cachep;
40
41 /*
42  * Ioctl related information.
43  */
44 static int num_hosts;
45
46 int ql2xlogintimeout = 20;
47 module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
48 MODULE_PARM_DESC(ql2xlogintimeout,
49                 "Login timeout value in seconds.");
50
51 int qlport_down_retry = 30;
52 module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
53 MODULE_PARM_DESC(qlport_down_retry,
54                 "Maximum number of command retries to a port that returns"
55                 "a PORT-DOWN status.");
56
57 int ql2xplogiabsentdevice;
58 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
59 MODULE_PARM_DESC(ql2xplogiabsentdevice,
60                 "Option to enable PLOGI to devices that are not present after "
61                 "a Fabric scan.  This is needed for several broken switches."
62                 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
63
64 int ql2xenablezio = 0;
65 module_param(ql2xenablezio, int, S_IRUGO|S_IRUSR);
66 MODULE_PARM_DESC(ql2xenablezio,
67                 "Option to enable ZIO:If 1 then enable it otherwise" 
68                 " use the default set in the NVRAM."
69                 " Default is 0 : disabled");
70
71 int ql2xintrdelaytimer = 10;
72 module_param(ql2xintrdelaytimer, int, S_IRUGO|S_IRUSR);
73 MODULE_PARM_DESC(ql2xintrdelaytimer,
74                 "ZIO: Waiting time for Firmware before it generates an "
75                 "interrupt to the host to notify completion of request.");
76
77 int ql2xloginretrycount = 0;
78 module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
79 MODULE_PARM_DESC(ql2xloginretrycount,
80                 "Specify an alternate value for the NVRAM login retry count.");
81
82 static void qla2x00_free_device(scsi_qla_host_t *);
83
84 static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
85
86 /*
87  * SCSI host template entry points 
88  */
89 static int qla2xxx_slave_configure(struct scsi_device * device);
90 static int qla2xxx_slave_alloc(struct scsi_device *);
91 static void qla2xxx_slave_destroy(struct scsi_device *);
92 static int qla2x00_queuecommand(struct scsi_cmnd *cmd,
93                 void (*fn)(struct scsi_cmnd *));
94 static int qla2xxx_eh_abort(struct scsi_cmnd *);
95 static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
96 static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
97 static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
98 static int qla2x00_loop_reset(scsi_qla_host_t *ha);
99 static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
100
101 static struct scsi_host_template qla2x00_driver_template = {
102         .module                 = THIS_MODULE,
103         .name                   = "qla2xxx",
104         .queuecommand           = qla2x00_queuecommand,
105
106         .eh_abort_handler       = qla2xxx_eh_abort,
107         .eh_device_reset_handler = qla2xxx_eh_device_reset,
108         .eh_bus_reset_handler   = qla2xxx_eh_bus_reset,
109         .eh_host_reset_handler  = qla2xxx_eh_host_reset,
110
111         .slave_configure        = qla2xxx_slave_configure,
112
113         .slave_alloc            = qla2xxx_slave_alloc,
114         .slave_destroy          = qla2xxx_slave_destroy,
115         .this_id                = -1,
116         .cmd_per_lun            = 3,
117         .use_clustering         = ENABLE_CLUSTERING,
118         .sg_tablesize           = SG_ALL,
119
120         /*
121          * The RISC allows for each command to transfer (2^32-1) bytes of data,
122          * which equates to 0x800000 sectors.
123          */
124         .max_sectors            = 0xFFFF,
125 };
126
127 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
128
129 /* TODO Convert to inlines
130  *
131  * Timer routines
132  */
133 #define WATCH_INTERVAL          1       /* number of seconds */
134
135 static void qla2x00_timer(scsi_qla_host_t *);
136
137 static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
138     void *, unsigned long);
139 static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
140 static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
141
142 static inline void
143 qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
144 {
145         init_timer(&ha->timer);
146         ha->timer.expires = jiffies + interval * HZ;
147         ha->timer.data = (unsigned long)ha;
148         ha->timer.function = (void (*)(unsigned long))func;
149         add_timer(&ha->timer);
150         ha->timer_active = 1;
151 }
152
153 static inline void
154 qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
155 {
156         mod_timer(&ha->timer, jiffies + interval * HZ);
157 }
158
159 static __inline__ void
160 qla2x00_stop_timer(scsi_qla_host_t *ha)
161 {
162         del_timer_sync(&ha->timer);
163         ha->timer_active = 0;
164 }
165
166 static int qla2x00_do_dpc(void *data);
167
168 static void qla2x00_rst_aen(scsi_qla_host_t *);
169
170 static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
171 static void qla2x00_mem_free(scsi_qla_host_t *ha);
172 static int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
173 static void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
174 static srb_t *qla2x00_get_new_sp(scsi_qla_host_t *);
175 static void qla2x00_sp_free_dma(scsi_qla_host_t *, srb_t *);
176 void qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *);
177
178 /* -------------------------------------------------------------------------- */
179
180 static char *
181 qla2x00_get_pci_info_str(struct scsi_qla_host *ha, char *str)
182 {
183         static char *pci_bus_modes[] = {
184                 "33", "66", "100", "133",
185         };
186         uint16_t pci_bus;
187
188         strcpy(str, "PCI");
189         pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
190         if (pci_bus) {
191                 strcat(str, "-X (");
192                 strcat(str, pci_bus_modes[pci_bus]);
193         } else {
194                 pci_bus = (ha->pci_attr & BIT_8) >> 8;
195                 strcat(str, " (");
196                 strcat(str, pci_bus_modes[pci_bus]);
197         }
198         strcat(str, " MHz)");
199
200         return (str);
201 }
202
203 char *
204 qla2x00_get_fw_version_str(struct scsi_qla_host *ha, char *str)
205 {
206         char un_str[10];
207         
208         sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
209             ha->fw_minor_version,
210             ha->fw_subminor_version);
211
212         if (ha->fw_attributes & BIT_9) {
213                 strcat(str, "FLX");
214                 return (str);
215         }
216
217         switch (ha->fw_attributes & 0xFF) {
218         case 0x7:
219                 strcat(str, "EF");
220                 break;
221         case 0x17:
222                 strcat(str, "TP");
223                 break;
224         case 0x37:
225                 strcat(str, "IP");
226                 break;
227         case 0x77:
228                 strcat(str, "VI");
229                 break;
230         default:
231                 sprintf(un_str, "(%x)", ha->fw_attributes);
232                 strcat(str, un_str);
233                 break;
234         }
235         if (ha->fw_attributes & 0x100)
236                 strcat(str, "X");
237
238         return (str);
239 }
240
241 /**************************************************************************
242 * qla2x00_queuecommand
243 *
244 * Description:
245 *     Queue a command to the controller.
246 *
247 * Input:
248 *     cmd - pointer to Scsi cmd structure
249 *     fn - pointer to Scsi done function
250 *
251 * Returns:
252 *   0 - Always
253 *
254 * Note:
255 * The mid-level driver tries to ensures that queuecommand never gets invoked
256 * concurrently with itself or the interrupt handler (although the
257 * interrupt handler may call this routine as part of request-completion
258 * handling).
259 **************************************************************************/
260 static int
261 qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
262 {
263         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
264         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
265         srb_t *sp;
266         int rval;
267
268         if (!fcport) {
269                 cmd->result = DID_NO_CONNECT << 16;
270                 goto qc_fail_command;
271         }
272
273         if (atomic_read(&fcport->state) != FCS_ONLINE) {
274                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
275                     atomic_read(&ha->loop_state) == LOOP_DEAD) {
276                         cmd->result = DID_NO_CONNECT << 16;
277                         goto qc_fail_command;
278                 }
279                 goto qc_host_busy;
280         }
281
282         spin_unlock_irq(ha->host->host_lock);
283
284         /* Allocate a command packet from the "sp" pool. */
285         if ((sp = qla2x00_get_new_sp(ha)) == NULL) {
286                 goto qc_host_busy_lock;
287         }
288
289         sp->ha = ha;
290         sp->fcport = fcport;
291         sp->cmd = cmd;
292         sp->flags = 0;
293
294         CMD_SP(cmd) = (void *)sp;
295         cmd->scsi_done = done;
296
297         rval = qla2x00_start_scsi(sp);
298         if (rval != QLA_SUCCESS)
299                 goto qc_host_busy_free_sp;
300
301         /* Manage unprocessed RIO/ZIO commands in response queue. */
302         if (ha->flags.online && ha->flags.process_response_queue &&
303             ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
304                 unsigned long flags;
305
306                 spin_lock_irqsave(&ha->hardware_lock, flags);
307                 qla2x00_process_response_queue(ha);
308                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
309         }
310
311         spin_lock_irq(ha->host->host_lock);
312
313         return 0;
314
315 qc_host_busy_free_sp:
316         qla2x00_sp_free_dma(ha, sp);
317         CMD_SP(cmd) = NULL;
318         mempool_free(sp, ha->srb_mempool);
319
320 qc_host_busy_lock:
321         spin_lock_irq(ha->host->host_lock);
322
323 qc_host_busy:
324         return SCSI_MLQUEUE_HOST_BUSY;
325
326 qc_fail_command:
327         done(cmd);
328
329         return 0;
330 }
331
332 /*
333  * qla2x00_eh_wait_on_command
334  *    Waits for the command to be returned by the Firmware for some
335  *    max time.
336  *
337  * Input:
338  *    ha = actual ha whose done queue will contain the command
339  *            returned by firmware.
340  *    cmd = Scsi Command to wait on.
341  *    flag = Abort/Reset(Bus or Device Reset)
342  *
343  * Return:
344  *    Not Found : 0
345  *    Found : 1
346  */
347 static int
348 qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
349 {
350 #define ABORT_POLLING_PERIOD    HZ
351 #define ABORT_WAIT_ITER         ((10 * HZ) / (ABORT_POLLING_PERIOD))
352         unsigned long wait_iter = ABORT_WAIT_ITER;
353         int ret = QLA_SUCCESS;
354
355         while (CMD_SP(cmd)) {
356                 set_current_state(TASK_UNINTERRUPTIBLE);
357                 schedule_timeout(ABORT_POLLING_PERIOD);
358
359                 if (--wait_iter)
360                         break;
361         }
362         if (CMD_SP(cmd))
363                 ret = QLA_FUNCTION_FAILED;
364
365         return ret;
366 }
367
368 /*
369  * qla2x00_wait_for_hba_online
370  *    Wait till the HBA is online after going through 
371  *    <= MAX_RETRIES_OF_ISP_ABORT  or
372  *    finally HBA is disabled ie marked offline
373  *
374  * Input:
375  *     ha - pointer to host adapter structure
376  * 
377  * Note:    
378  *    Does context switching-Release SPIN_LOCK
379  *    (if any) before calling this routine.
380  *
381  * Return:
382  *    Success (Adapter is online) : 0
383  *    Failed  (Adapter is offline/disabled) : 1
384  */
385 static int 
386 qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
387 {
388         int      return_status;
389         unsigned long wait_online;
390
391         wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ); 
392         while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
393             test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
394             test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
395             ha->dpc_active) && time_before(jiffies, wait_online)) {
396
397                 msleep(1000);
398         }
399         if (ha->flags.online) 
400                 return_status = QLA_SUCCESS; 
401         else
402                 return_status = QLA_FUNCTION_FAILED;
403
404         DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
405
406         return (return_status);
407 }
408
409 /*
410  * qla2x00_wait_for_loop_ready
411  *    Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
412  *    to be in LOOP_READY state.         
413  * Input:
414  *     ha - pointer to host adapter structure
415  * 
416  * Note:    
417  *    Does context switching-Release SPIN_LOCK
418  *    (if any) before calling this routine.
419  *    
420  *
421  * Return:
422  *    Success (LOOP_READY) : 0
423  *    Failed  (LOOP_NOT_READY) : 1
424  */
425 static inline int 
426 qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
427 {
428         int      return_status = QLA_SUCCESS;
429         unsigned long loop_timeout ;
430
431         /* wait for 5 min at the max for loop to be ready */
432         loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ); 
433
434         while ((!atomic_read(&ha->loop_down_timer) &&
435             atomic_read(&ha->loop_state) == LOOP_DOWN) ||
436             atomic_read(&ha->loop_state) != LOOP_READY) {
437                 msleep(1000);
438                 if (time_after_eq(jiffies, loop_timeout)) {
439                         return_status = QLA_FUNCTION_FAILED;
440                         break;
441                 }
442         }
443         return (return_status); 
444 }
445
446 /**************************************************************************
447 * qla2xxx_eh_abort
448 *
449 * Description:
450 *    The abort function will abort the specified command.
451 *
452 * Input:
453 *    cmd = Linux SCSI command packet to be aborted.
454 *
455 * Returns:
456 *    Either SUCCESS or FAILED.
457 *
458 * Note:
459 **************************************************************************/
460 int
461 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
462 {
463         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
464         srb_t *sp;
465         int ret, i;
466         unsigned int id, lun;
467         unsigned long serial;
468         unsigned long flags;
469
470         if (!CMD_SP(cmd))
471                 return FAILED;
472
473         ret = FAILED;
474
475         id = cmd->device->id;
476         lun = cmd->device->lun;
477         serial = cmd->serial_number;
478
479         /* Check active list for command command. */
480         spin_lock_irqsave(&ha->hardware_lock, flags);
481         for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
482                 sp = ha->outstanding_cmds[i];
483
484                 if (sp == NULL)
485                         continue;
486
487                 if (sp->cmd != cmd)
488                         continue;
489
490                 DEBUG2(printk("%s(%ld): aborting sp %p from RISC. pid=%ld "
491                     "sp->state=%x\n", __func__, ha->host_no, sp, serial,
492                     sp->state));
493                 DEBUG3(qla2x00_print_scsi_cmd(cmd);)
494
495                 spin_unlock_irqrestore(&ha->hardware_lock, flags);
496                 if (qla2x00_abort_command(ha, sp)) {
497                         DEBUG2(printk("%s(%ld): abort_command "
498                             "mbx failed.\n", __func__, ha->host_no));
499                 } else {
500                         DEBUG3(printk("%s(%ld): abort_command "
501                             "mbx success.\n", __func__, ha->host_no));
502                         ret = SUCCESS;
503                 }
504                 spin_lock_irqsave(&ha->hardware_lock, flags);
505
506                 break;
507         }
508         spin_unlock_irqrestore(&ha->hardware_lock, flags);
509
510         /* Wait for the command to be returned. */
511         if (ret == SUCCESS) {
512                 if (qla2x00_eh_wait_on_command(ha, cmd) != QLA_SUCCESS) {
513                         qla_printk(KERN_ERR, ha, 
514                             "scsi(%ld:%d:%d): Abort handler timed out -- %lx "
515                             "%x.\n", ha->host_no, id, lun, serial, ret);
516                 }
517         }
518
519         qla_printk(KERN_INFO, ha, 
520             "scsi(%ld:%d:%d): Abort command issued -- %lx %x.\n", ha->host_no,
521             id, lun, serial, ret);
522
523         return ret;
524 }
525
526 /**************************************************************************
527 * qla2x00_eh_wait_for_pending_target_commands
528 *
529 * Description:
530 *    Waits for all the commands to come back from the specified target.
531 *
532 * Input:
533 *    ha - pointer to scsi_qla_host structure.
534 *    t  - target        
535 * Returns:
536 *    Either SUCCESS or FAILED.
537 *
538 * Note:
539 **************************************************************************/
540 static int
541 qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
542 {
543         int     cnt;
544         int     status;
545         srb_t           *sp;
546         struct scsi_cmnd *cmd;
547         unsigned long flags;
548
549         status = 0;
550
551         /*
552          * Waiting for all commands for the designated target in the active
553          * array
554          */
555         for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
556                 spin_lock_irqsave(&ha->hardware_lock, flags);
557                 sp = ha->outstanding_cmds[cnt];
558                 if (sp) {
559                         cmd = sp->cmd;
560                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
561                         if (cmd->device->id == t) {
562                                 if (!qla2x00_eh_wait_on_command(ha, cmd)) {
563                                         status = 1;
564                                         break;
565                                 }
566                         }
567                 } else {
568                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
569                 }
570         }
571         return (status);
572 }
573
574
575 /**************************************************************************
576 * qla2xxx_eh_device_reset
577 *
578 * Description:
579 *    The device reset function will reset the target and abort any
580 *    executing commands.
581 *
582 *    NOTE: The use of SP is undefined within this context.  Do *NOT*
583 *          attempt to use this value, even if you determine it is 
584 *          non-null.
585 *
586 * Input:
587 *    cmd = Linux SCSI command packet of the command that cause the
588 *          bus device reset.
589 *
590 * Returns:
591 *    SUCCESS/FAILURE (defined as macro in scsi.h).
592 *
593 **************************************************************************/
594 int
595 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
596 {
597         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
598         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
599         srb_t *sp;
600         int ret;
601         unsigned int id, lun;
602         unsigned long serial;
603
604         ret = FAILED;
605
606         id = cmd->device->id;
607         lun = cmd->device->lun;
608         serial = cmd->serial_number;
609
610         sp = (srb_t *) CMD_SP(cmd);
611         if (!sp || !fcport)
612                 return ret;
613
614         qla_printk(KERN_INFO, ha,
615             "scsi(%ld:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, id, lun);
616
617         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
618                 goto eh_dev_reset_done;
619
620         if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
621                 if (qla2x00_device_reset(ha, fcport) == 0)
622                         ret = SUCCESS;
623
624 #if defined(LOGOUT_AFTER_DEVICE_RESET)
625                 if (ret == SUCCESS) {
626                         if (fcport->flags & FC_FABRIC_DEVICE) {
627                                 qla2x00_fabric_logout(ha, fcport->loop_id);
628                                 qla2x00_mark_device_lost(ha, fcport);
629                         }
630                 }
631 #endif
632         } else {
633                 DEBUG2(printk(KERN_INFO
634                     "%s failed: loop not ready\n",__func__);)
635         }
636
637         if (ret == FAILED) {
638                 DEBUG3(printk("%s(%ld): device reset failed\n",
639                     __func__, ha->host_no));
640                 qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
641                     __func__);
642
643                 goto eh_dev_reset_done;
644         }
645
646         /*
647          * If we are coming down the EH path, wait for all commands to
648          * complete for the device.
649          */
650         if (cmd->device->host->eh_active) {
651                 if (qla2x00_eh_wait_for_pending_target_commands(ha, id))
652                         ret = FAILED;
653
654                 if (ret == FAILED) {
655                         DEBUG3(printk("%s(%ld): failed while waiting for "
656                             "commands\n", __func__, ha->host_no));
657                         qla_printk(KERN_INFO, ha,
658                             "%s: failed while waiting for commands\n",
659                             __func__); 
660
661                         goto eh_dev_reset_done;
662                 }
663         }
664
665         qla_printk(KERN_INFO, ha,
666             "scsi(%ld:%d:%d): DEVICE RESET SUCCEEDED.\n", ha->host_no, id, lun);
667
668 eh_dev_reset_done:
669         return ret;
670 }
671
672 /**************************************************************************
673 * qla2x00_eh_wait_for_pending_commands
674 *
675 * Description:
676 *    Waits for all the commands to come back from the specified host.
677 *
678 * Input:
679 *    ha - pointer to scsi_qla_host structure.
680 *
681 * Returns:
682 *    1 : SUCCESS
683 *    0 : FAILED
684 *
685 * Note:
686 **************************************************************************/
687 static int
688 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
689 {
690         int     cnt;
691         int     status;
692         srb_t           *sp;
693         struct scsi_cmnd *cmd;
694         unsigned long flags;
695
696         status = 1;
697
698         /*
699          * Waiting for all commands for the designated target in the active
700          * array
701          */
702         for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
703                 spin_lock_irqsave(&ha->hardware_lock, flags);
704                 sp = ha->outstanding_cmds[cnt];
705                 if (sp) {
706                         cmd = sp->cmd;
707                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
708                         status = qla2x00_eh_wait_on_command(ha, cmd);
709                         if (status == 0)
710                                 break;
711                 }
712                 else {
713                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
714                 }
715         }
716         return (status);
717 }
718
719
720 /**************************************************************************
721 * qla2xxx_eh_bus_reset
722 *
723 * Description:
724 *    The bus reset function will reset the bus and abort any executing
725 *    commands.
726 *
727 * Input:
728 *    cmd = Linux SCSI command packet of the command that cause the
729 *          bus reset.
730 *
731 * Returns:
732 *    SUCCESS/FAILURE (defined as macro in scsi.h).
733 *
734 **************************************************************************/
735 int
736 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
737 {
738         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
739         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
740         srb_t *sp;
741         int ret;
742         unsigned int id, lun;
743         unsigned long serial;
744
745         ret = FAILED;
746
747         id = cmd->device->id;
748         lun = cmd->device->lun;
749         serial = cmd->serial_number;
750
751         sp = (srb_t *) CMD_SP(cmd);
752         if (!sp || !fcport)
753                 return ret;
754
755         qla_printk(KERN_INFO, ha,
756             "scsi(%ld:%d:%d): LOOP RESET ISSUED.\n", ha->host_no, id, lun);
757
758         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
759                 DEBUG2(printk("%s failed:board disabled\n",__func__));
760                 goto eh_bus_reset_done;
761         }
762
763         if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
764                 if (qla2x00_loop_reset(ha) == QLA_SUCCESS)
765                         ret = SUCCESS;
766         }
767         if (ret == FAILED)
768                 goto eh_bus_reset_done;
769
770         /* Waiting for our command in done_queue to be returned to OS.*/
771         if (cmd->device->host->eh_active)
772                 if (!qla2x00_eh_wait_for_pending_commands(ha))
773                         ret = FAILED;
774
775 eh_bus_reset_done:
776         qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
777             (ret == FAILED) ? "failed" : "succeded");
778
779         return ret;
780 }
781
782 /**************************************************************************
783 * qla2xxx_eh_host_reset
784 *
785 * Description:
786 *    The reset function will reset the Adapter.
787 *
788 * Input:
789 *      cmd = Linux SCSI command packet of the command that cause the
790 *            adapter reset.
791 *
792 * Returns:
793 *      Either SUCCESS or FAILED.
794 *
795 * Note:
796 **************************************************************************/
797 int
798 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
799 {
800         scsi_qla_host_t *ha = to_qla_host(cmd->device->host);
801         fc_port_t *fcport = (struct fc_port *) cmd->device->hostdata;
802         srb_t *sp;
803         int ret;
804         unsigned int id, lun;
805         unsigned long serial;
806
807         ret = FAILED;
808
809         id = cmd->device->id;
810         lun = cmd->device->lun;
811         serial = cmd->serial_number;
812
813         sp = (srb_t *) CMD_SP(cmd);
814         if (!sp || !fcport)
815                 return ret;
816
817         qla_printk(KERN_INFO, ha,
818             "scsi(%ld:%d:%d): ADAPTER RESET ISSUED.\n", ha->host_no, id, lun);
819
820         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
821                 goto eh_host_reset_lock;
822
823         /*
824          * Fixme-may be dpc thread is active and processing
825          * loop_resync,so wait a while for it to 
826          * be completed and then issue big hammer.Otherwise
827          * it may cause I/O failure as big hammer marks the
828          * devices as lost kicking of the port_down_timer
829          * while dpc is stuck for the mailbox to complete.
830          */
831         qla2x00_wait_for_loop_ready(ha);
832         set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
833         if (qla2x00_abort_isp(ha)) {
834                 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
835                 /* failed. schedule dpc to try */
836                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
837
838                 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
839                         goto eh_host_reset_lock;
840         } 
841         clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
842
843         /* Waiting for our command in done_queue to be returned to OS.*/
844         if (qla2x00_eh_wait_for_pending_commands(ha))
845                 ret = SUCCESS;
846
847 eh_host_reset_lock:
848         qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
849             (ret == FAILED) ? "failed" : "succeded");
850
851         return ret;
852 }
853
854 /*
855 * qla2x00_loop_reset
856 *      Issue loop reset.
857 *
858 * Input:
859 *      ha = adapter block pointer.
860 *
861 * Returns:
862 *      0 = success
863 */
864 static int
865 qla2x00_loop_reset(scsi_qla_host_t *ha)
866 {
867         int status = QLA_SUCCESS;
868         struct fc_port *fcport;
869
870         if (ha->flags.enable_lip_reset) {
871                 status = qla2x00_lip_reset(ha);
872         }
873
874         if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
875                 list_for_each_entry(fcport, &ha->fcports, list) {
876                         if (fcport->port_type != FCT_TARGET)
877                                 continue;
878
879                         status = qla2x00_target_reset(ha, fcport);
880                         if (status != QLA_SUCCESS)
881                                 break;
882                 }
883         }
884
885         if (status == QLA_SUCCESS &&
886                 ((!ha->flags.enable_target_reset && 
887                   !ha->flags.enable_lip_reset) ||
888                 ha->flags.enable_lip_full_login)) {
889
890                 status = qla2x00_full_login_lip(ha);
891         }
892
893         /* Issue marker command only when we are going to start the I/O */
894         ha->marker_needed = 1;
895
896         if (status) {
897                 /* Empty */
898                 DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
899                                 __func__,
900                                 ha->host_no);)
901         } else {
902                 /* Empty */
903                 DEBUG3(printk("%s(%ld): exiting normally.\n",
904                                 __func__,
905                                 ha->host_no);)
906         }
907
908         return(status);
909 }
910
911 /*
912  * qla2x00_device_reset
913  *      Issue bus device reset message to the target.
914  *
915  * Input:
916  *      ha = adapter block pointer.
917  *      t = SCSI ID.
918  *      TARGET_QUEUE_LOCK must be released.
919  *      ADAPTER_STATE_LOCK must be released.
920  *
921  * Context:
922  *      Kernel context.
923  */
924 static int
925 qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
926 {
927         /* Abort Target command will clear Reservation */
928         return qla2x00_abort_target(reset_fcport);
929 }
930
931 static int
932 qla2xxx_slave_alloc(struct scsi_device *sdev)
933 {
934         scsi_qla_host_t *ha = to_qla_host(sdev->host);
935         struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
936         fc_port_t *fcport;
937         int found;
938
939         if (!rport)
940                 return -ENXIO;
941
942         found = 0;
943         list_for_each_entry(fcport, &ha->fcports, list) {
944                 if (rport->port_name ==
945                     be64_to_cpu(*(uint64_t *)fcport->port_name)) {
946                         found++;
947                         break;
948                 }
949         }
950         if (!found)
951                 return -ENXIO;
952
953         sdev->hostdata = fcport;
954
955         return 0;
956 }
957
958 static int
959 qla2xxx_slave_configure(struct scsi_device *sdev)
960 {
961         scsi_qla_host_t *ha = to_qla_host(sdev->host);
962         struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
963
964         if (sdev->tagged_supported)
965                 scsi_activate_tcq(sdev, 32);
966         else
967                 scsi_deactivate_tcq(sdev, 32);
968
969         rport->dev_loss_tmo = ha->port_down_retry_count + 5;
970
971         return 0;
972 }
973
974 static void
975 qla2xxx_slave_destroy(struct scsi_device *sdev)
976 {
977         sdev->hostdata = NULL;
978 }
979
980 /**
981  * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
982  * @ha: HA context
983  *
984  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
985  * supported addressing method.
986  */
987 static void
988 qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
989 {
990         /* Assume 32bit DMA address */
991         ha->flags.enable_64bit_addressing = 0;
992         ha->calc_request_entries = qla2x00_calc_iocbs_32;
993         ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_32;
994
995         /*
996          * Given the two variants pci_set_dma_mask(), allow the compiler to
997          * assist in setting the proper dma mask.
998          */
999         if (sizeof(dma_addr_t) > 4) {
1000                 if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1001                         ha->flags.enable_64bit_addressing = 1;
1002                         ha->calc_request_entries = qla2x00_calc_iocbs_64;
1003                         ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_64;
1004
1005                         if (pci_set_consistent_dma_mask(ha->pdev,
1006                             DMA_64BIT_MASK)) {
1007                                 qla_printk(KERN_DEBUG, ha, 
1008                                     "Failed to set 64 bit PCI consistent mask; "
1009                                     "using 32 bit.\n");
1010                                 pci_set_consistent_dma_mask(ha->pdev,
1011                                     DMA_32BIT_MASK);
1012                         }
1013                 } else {
1014                         qla_printk(KERN_DEBUG, ha,
1015                             "Failed to set 64 bit PCI DMA mask, falling back "
1016                             "to 32 bit MASK.\n");
1017                         pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1018                 }
1019         } else {
1020                 pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1021         }
1022 }
1023
1024 static int
1025 qla2x00_iospace_config(scsi_qla_host_t *ha)
1026 {
1027         unsigned long   pio, pio_len, pio_flags;
1028         unsigned long   mmio, mmio_len, mmio_flags;
1029
1030         /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1031         pio = pci_resource_start(ha->pdev, 0);
1032         pio_len = pci_resource_len(ha->pdev, 0);
1033         pio_flags = pci_resource_flags(ha->pdev, 0);
1034         if (pio_flags & IORESOURCE_IO) {
1035                 if (pio_len < MIN_IOBASE_LEN) {
1036                         qla_printk(KERN_WARNING, ha,
1037                             "Invalid PCI I/O region size (%s)...\n",
1038                                 pci_name(ha->pdev));
1039                         pio = 0;
1040                 }
1041         } else {
1042                 qla_printk(KERN_WARNING, ha,
1043                     "region #0 not a PIO resource (%s)...\n",
1044                     pci_name(ha->pdev));
1045                 pio = 0;
1046         }
1047
1048         /* Use MMIO operations for all accesses. */
1049         mmio = pci_resource_start(ha->pdev, 1);
1050         mmio_len = pci_resource_len(ha->pdev, 1);
1051         mmio_flags = pci_resource_flags(ha->pdev, 1);
1052
1053         if (!(mmio_flags & IORESOURCE_MEM)) {
1054                 qla_printk(KERN_ERR, ha,
1055                     "region #0 not an MMIO resource (%s), aborting\n",
1056                     pci_name(ha->pdev));
1057                 goto iospace_error_exit;
1058         }
1059         if (mmio_len < MIN_IOBASE_LEN) {
1060                 qla_printk(KERN_ERR, ha,
1061                     "Invalid PCI mem region size (%s), aborting\n",
1062                         pci_name(ha->pdev));
1063                 goto iospace_error_exit;
1064         }
1065
1066         if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1067                 qla_printk(KERN_WARNING, ha,
1068                     "Failed to reserve PIO/MMIO regions (%s)\n",
1069                     pci_name(ha->pdev));
1070
1071                 goto iospace_error_exit;
1072         }
1073
1074         ha->pio_address = pio;
1075         ha->pio_length = pio_len;
1076         ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1077         if (!ha->iobase) {
1078                 qla_printk(KERN_ERR, ha,
1079                     "cannot remap MMIO (%s), aborting\n", pci_name(ha->pdev));
1080
1081                 goto iospace_error_exit;
1082         }
1083
1084         return (0);
1085
1086 iospace_error_exit:
1087         return (-ENOMEM);
1088 }
1089
1090 /*
1091  * PCI driver interface
1092  */
1093 int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1094 {
1095         int     ret;
1096         device_reg_t __iomem *reg;
1097         struct Scsi_Host *host;
1098         scsi_qla_host_t *ha;
1099         unsigned long   flags = 0;
1100         unsigned long   wait_switch = 0;
1101         char pci_info[20];
1102         char fw_str[30];
1103         fc_port_t *fcport;
1104
1105         if (pci_enable_device(pdev))
1106                 return -1;
1107
1108         host = scsi_host_alloc(&qla2x00_driver_template,
1109             sizeof(scsi_qla_host_t));
1110         if (host == NULL) {
1111                 printk(KERN_WARNING
1112                     "qla2xxx: Couldn't allocate host from scsi layer!\n");
1113                 goto probe_disable_device;
1114         }
1115
1116         /* Clear our data area */
1117         ha = (scsi_qla_host_t *)host->hostdata;
1118         memset(ha, 0, sizeof(scsi_qla_host_t));
1119
1120         ha->pdev = pdev;
1121         ha->host = host;
1122         ha->host_no = host->host_no;
1123         ha->brd_info = brd_info;
1124         sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1125
1126         /* Configure PCI I/O space */
1127         ret = qla2x00_iospace_config(ha);
1128         if (ret != 0) {
1129                 goto probe_alloc_failed;
1130         }
1131
1132         /* Sanitize the information from PCI BIOS. */
1133         host->irq = pdev->irq;
1134
1135         qla_printk(KERN_INFO, ha,
1136             "Found an %s, irq %d, iobase 0x%p\n", ha->brd_info->isp_name,
1137             host->irq, ha->iobase);
1138
1139         spin_lock_init(&ha->hardware_lock);
1140
1141         ha->prev_topology = 0;
1142         ha->ports = MAX_BUSES;
1143
1144         if (IS_QLA2100(ha)) {
1145                 host->max_id = MAX_TARGETS_2100;
1146                 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1147                 ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1148                 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1149                 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1150                 host->sg_tablesize = 32;
1151         } else if (IS_QLA2200(ha)) {
1152                 host->max_id = MAX_TARGETS_2200;
1153                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1154                 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1155                 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1156                 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1157         } else /*if (IS_QLA2300(ha))*/ {
1158                 host->max_id = MAX_TARGETS_2200;
1159                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1160                 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1161                 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1162                 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1163         }
1164         host->can_queue = ha->request_q_length + 128;
1165
1166         /* load the F/W, read paramaters, and init the H/W */
1167         ha->instance = num_hosts;
1168
1169         init_MUTEX(&ha->mbx_cmd_sem);
1170         init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1171
1172         INIT_LIST_HEAD(&ha->list);
1173         INIT_LIST_HEAD(&ha->fcports);
1174         INIT_LIST_HEAD(&ha->rscn_fcports);
1175
1176         /*
1177          * These locks are used to prevent more than one CPU
1178          * from modifying the queue at the same time. The
1179          * higher level "host_lock" will reduce most
1180          * contention for these locks.
1181          */
1182         spin_lock_init(&ha->mbx_reg_lock);
1183
1184         ha->dpc_pid = -1;
1185         init_completion(&ha->dpc_inited);
1186         init_completion(&ha->dpc_exited);
1187
1188         qla2x00_config_dma_addressing(ha);
1189         if (qla2x00_mem_alloc(ha)) {
1190                 qla_printk(KERN_WARNING, ha,
1191                     "[ERROR] Failed to allocate memory for adapter\n");
1192
1193                 goto probe_alloc_failed;
1194         }
1195
1196         pci_set_drvdata(pdev, ha);
1197         host->this_id = 255;
1198         host->cmd_per_lun = 3;
1199         host->unique_id = ha->instance;
1200         host->max_cmd_len = MAX_CMDSZ;
1201         host->max_channel = ha->ports - 1;
1202         host->max_lun = MAX_LUNS;
1203         host->transportt = qla2xxx_transport_template;
1204         if (scsi_add_host(host, &pdev->dev))
1205                 goto probe_alloc_failed;
1206
1207         qla2x00_alloc_sysfs_attr(ha);
1208
1209         if (qla2x00_initialize_adapter(ha) &&
1210             !(ha->device_flags & DFLG_NO_CABLE)) {
1211
1212                 qla_printk(KERN_WARNING, ha,
1213                     "Failed to initialize adapter\n");
1214
1215                 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
1216                     "Adapter flags %x.\n",
1217                     ha->host_no, ha->device_flags));
1218
1219                 goto probe_failed;
1220         }
1221
1222         qla2x00_init_host_attr(ha);
1223
1224         /*
1225          * Startup the kernel thread for this host adapter
1226          */
1227         ha->dpc_should_die = 0;
1228         ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
1229         if (ha->dpc_pid < 0) {
1230                 qla_printk(KERN_WARNING, ha,
1231                     "Unable to start DPC thread!\n");
1232
1233                 goto probe_failed;
1234         }
1235         wait_for_completion(&ha->dpc_inited);
1236
1237         if (IS_QLA2100(ha) || IS_QLA2200(ha))
1238                 ret = request_irq(host->irq, qla2100_intr_handler,
1239                     SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1240         else
1241                 ret = request_irq(host->irq, qla2300_intr_handler,
1242                     SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
1243         if (ret != 0) {
1244                 qla_printk(KERN_WARNING, ha,
1245                     "Failed to reserve interrupt %d already in use.\n",
1246                     host->irq);
1247                 goto probe_failed;
1248         }
1249
1250         /* Initialized the timer */
1251         qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
1252
1253         DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
1254             ha->host_no, ha));
1255
1256         reg = ha->iobase;
1257
1258         /* Disable ISP interrupts. */
1259         qla2x00_disable_intrs(ha);
1260
1261         /* Ensure mailbox registers are free. */
1262         spin_lock_irqsave(&ha->hardware_lock, flags);
1263         WRT_REG_WORD(&reg->semaphore, 0);
1264         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
1265         WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
1266
1267         /* Enable proper parity */
1268         if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
1269                 if (IS_QLA2300(ha))
1270                         /* SRAM parity */
1271                         WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x1));
1272                 else
1273                         /* SRAM, Instruction RAM and GP RAM parity */
1274                         WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x7));
1275         }
1276         spin_unlock_irqrestore(&ha->hardware_lock, flags);
1277
1278         /* Enable chip interrupts. */
1279         qla2x00_enable_intrs(ha);
1280
1281         /* v2.19.5b6 */
1282         /*
1283          * Wait around max loop_reset_delay secs for the devices to come
1284          * on-line. We don't want Linux scanning before we are ready.
1285          *
1286          */
1287         for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
1288             time_before(jiffies,wait_switch) &&
1289              !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
1290              && (ha->device_flags & SWITCH_FOUND) ;) {
1291
1292                 qla2x00_check_fabric_devices(ha);
1293
1294                 msleep(10);
1295         }
1296
1297         ha->flags.init_done = 1;
1298         num_hosts++;
1299
1300         qla_printk(KERN_INFO, ha, "\n"
1301             " QLogic Fibre Channel HBA Driver: %s\n"
1302             "  QLogic %s - %s\n"
1303             "  %s: %s @ %s hdma%c, host#=%ld, fw=%s\n", qla2x00_version_str,
1304             ha->model_number, ha->model_desc ? ha->model_desc: "",
1305             ha->brd_info->isp_name, qla2x00_get_pci_info_str(ha, pci_info),
1306             pci_name(ha->pdev), ha->flags.enable_64bit_addressing ? '+': '-',
1307             ha->host_no, qla2x00_get_fw_version_str(ha, fw_str));
1308
1309         /* Go with fc_rport registration. */
1310         list_for_each_entry(fcport, &ha->fcports, list)
1311                 qla2x00_reg_remote_port(ha, fcport);
1312
1313         return 0;
1314
1315 probe_failed:
1316         fc_remove_host(ha->host);
1317
1318         scsi_remove_host(host);
1319
1320 probe_alloc_failed:
1321         qla2x00_free_device(ha);
1322
1323         scsi_host_put(host);
1324
1325 probe_disable_device:
1326         pci_disable_device(pdev);
1327
1328         return -1;
1329 }
1330 EXPORT_SYMBOL_GPL(qla2x00_probe_one);
1331
1332 void qla2x00_remove_one(struct pci_dev *pdev)
1333 {
1334         scsi_qla_host_t *ha;
1335
1336         ha = pci_get_drvdata(pdev);
1337
1338         qla2x00_free_sysfs_attr(ha);
1339
1340         fc_remove_host(ha->host);
1341
1342         scsi_remove_host(ha->host);
1343
1344         qla2x00_free_device(ha);
1345
1346         scsi_host_put(ha->host);
1347
1348         pci_set_drvdata(pdev, NULL);
1349 }
1350 EXPORT_SYMBOL_GPL(qla2x00_remove_one);
1351
1352 static void
1353 qla2x00_free_device(scsi_qla_host_t *ha)
1354 {
1355         int ret;
1356
1357         /* Abort any outstanding IO descriptors. */
1358         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
1359                 qla2x00_cancel_io_descriptors(ha);
1360
1361         /* turn-off interrupts on the card */
1362         if (ha->interrupts_on)
1363                 qla2x00_disable_intrs(ha);
1364
1365         /* Disable timer */
1366         if (ha->timer_active)
1367                 qla2x00_stop_timer(ha);
1368
1369         /* Kill the kernel thread for this host */
1370         if (ha->dpc_pid >= 0) {
1371                 ha->dpc_should_die = 1;
1372                 wmb();
1373                 ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
1374                 if (ret) {
1375                         qla_printk(KERN_ERR, ha,
1376                             "Unable to signal DPC thread -- (%d)\n", ret);
1377
1378                         /* TODO: SOMETHING MORE??? */
1379                 } else {
1380                         wait_for_completion(&ha->dpc_exited);
1381                 }
1382         }
1383
1384         qla2x00_mem_free(ha);
1385
1386
1387         ha->flags.online = 0;
1388
1389         /* Detach interrupts */
1390         if (ha->pdev->irq)
1391                 free_irq(ha->pdev->irq, ha);
1392
1393         /* release io space registers  */
1394         if (ha->iobase)
1395                 iounmap(ha->iobase);
1396         pci_release_regions(ha->pdev);
1397
1398         pci_disable_device(ha->pdev);
1399 }
1400
1401 /*
1402  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
1403  *
1404  * Input: ha = adapter block pointer.  fcport = port structure pointer.
1405  *
1406  * Return: None.
1407  *
1408  * Context:
1409  */
1410 void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
1411     int do_login)
1412 {
1413         if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1414                 fc_remote_port_block(fcport->rport);
1415         /* 
1416          * We may need to retry the login, so don't change the state of the
1417          * port but do the retries.
1418          */
1419         if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
1420                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1421
1422         if (!do_login)
1423                 return;
1424
1425         if (fcport->login_retry == 0) {
1426                 fcport->login_retry = ha->login_retry_count;
1427                 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1428
1429                 DEBUG(printk("scsi(%ld): Port login retry: "
1430                     "%02x%02x%02x%02x%02x%02x%02x%02x, "
1431                     "id = 0x%04x retry cnt=%d\n",
1432                     ha->host_no,
1433                     fcport->port_name[0],
1434                     fcport->port_name[1],
1435                     fcport->port_name[2],
1436                     fcport->port_name[3],
1437                     fcport->port_name[4],
1438                     fcport->port_name[5],
1439                     fcport->port_name[6],
1440                     fcport->port_name[7],
1441                     fcport->loop_id,
1442                     fcport->login_retry));
1443         }
1444 }
1445
1446 /*
1447  * qla2x00_mark_all_devices_lost
1448  *      Updates fcport state when device goes offline.
1449  *
1450  * Input:
1451  *      ha = adapter block pointer.
1452  *      fcport = port structure pointer.
1453  *
1454  * Return:
1455  *      None.
1456  *
1457  * Context:
1458  */
1459 void
1460 qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha) 
1461 {
1462         fc_port_t *fcport;
1463
1464         list_for_each_entry(fcport, &ha->fcports, list) {
1465                 if (fcport->port_type != FCT_TARGET)
1466                         continue;
1467
1468                 /*
1469                  * No point in marking the device as lost, if the device is
1470                  * already DEAD.
1471                  */
1472                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
1473                         continue;
1474                 if (atomic_read(&fcport->state) == FCS_ONLINE && fcport->rport)
1475                         fc_remote_port_block(fcport->rport);
1476                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
1477         }
1478 }
1479
1480 /*
1481 * qla2x00_mem_alloc
1482 *      Allocates adapter memory.
1483 *
1484 * Returns:
1485 *      0  = success.
1486 *      1  = failure.
1487 */
1488 static uint8_t
1489 qla2x00_mem_alloc(scsi_qla_host_t *ha)
1490 {
1491         char    name[16];
1492         uint8_t   status = 1;
1493         int     retry= 10;
1494
1495         do {
1496                 /*
1497                  * This will loop only once if everything goes well, else some
1498                  * number of retries will be performed to get around a kernel
1499                  * bug where available mem is not allocated until after a
1500                  * little delay and a retry.
1501                  */
1502                 ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
1503                     (ha->request_q_length + 1) * sizeof(request_t),
1504                     &ha->request_dma, GFP_KERNEL);
1505                 if (ha->request_ring == NULL) {
1506                         qla_printk(KERN_WARNING, ha,
1507                             "Memory Allocation failed - request_ring\n");
1508
1509                         qla2x00_mem_free(ha);
1510                         msleep(100);
1511
1512                         continue;
1513                 }
1514
1515                 ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
1516                     (ha->response_q_length + 1) * sizeof(response_t),
1517                     &ha->response_dma, GFP_KERNEL);
1518                 if (ha->response_ring == NULL) {
1519                         qla_printk(KERN_WARNING, ha,
1520                             "Memory Allocation failed - response_ring\n");
1521
1522                         qla2x00_mem_free(ha);
1523                         msleep(100);
1524
1525                         continue;
1526                 }
1527
1528                 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
1529                     &ha->gid_list_dma, GFP_KERNEL);
1530                 if (ha->gid_list == NULL) {
1531                         qla_printk(KERN_WARNING, ha,
1532                             "Memory Allocation failed - gid_list\n");
1533
1534                         qla2x00_mem_free(ha);
1535                         msleep(100);
1536
1537                         continue;
1538                 }
1539
1540                 ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
1541                     sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
1542                 if (ha->rlc_rsp == NULL) {
1543                         qla_printk(KERN_WARNING, ha,
1544                                 "Memory Allocation failed - rlc");
1545
1546                         qla2x00_mem_free(ha);
1547                         msleep(100);
1548
1549                         continue;
1550                 }
1551
1552                 snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
1553                 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
1554                     DMA_POOL_SIZE, 8, 0);
1555                 if (ha->s_dma_pool == NULL) {
1556                         qla_printk(KERN_WARNING, ha,
1557                             "Memory Allocation failed - s_dma_pool\n");
1558
1559                         qla2x00_mem_free(ha);
1560                         msleep(100);
1561
1562                         continue;
1563                 }
1564
1565                 /* get consistent memory allocated for init control block */
1566                 ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1567                     &ha->init_cb_dma);
1568                 if (ha->init_cb == NULL) {
1569                         qla_printk(KERN_WARNING, ha,
1570                             "Memory Allocation failed - init_cb\n");
1571
1572                         qla2x00_mem_free(ha);
1573                         msleep(100);
1574
1575                         continue;
1576                 }
1577                 memset(ha->init_cb, 0, sizeof(init_cb_t));
1578
1579                 /* Get consistent memory allocated for Get Port Database cmd */
1580                 ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1581                     &ha->iodesc_pd_dma);
1582                 if (ha->iodesc_pd == NULL) {
1583                         /* error */
1584                         qla_printk(KERN_WARNING, ha,
1585                             "Memory Allocation failed - iodesc_pd\n");
1586
1587                         qla2x00_mem_free(ha);
1588                         msleep(100);
1589
1590                         continue;
1591                 }
1592                 memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
1593
1594                 /* Allocate ioctl related memory. */
1595                 if (qla2x00_alloc_ioctl_mem(ha)) {
1596                         qla_printk(KERN_WARNING, ha,
1597                             "Memory Allocation failed - ioctl_mem\n");
1598
1599                         qla2x00_mem_free(ha);
1600                         msleep(100);
1601
1602                         continue;
1603                 }
1604
1605                 if (qla2x00_allocate_sp_pool(ha)) {
1606                         qla_printk(KERN_WARNING, ha,
1607                             "Memory Allocation failed - "
1608                             "qla2x00_allocate_sp_pool()\n");
1609
1610                         qla2x00_mem_free(ha);
1611                         msleep(100);
1612
1613                         continue;
1614                 }
1615
1616                 /* Allocate memory for SNS commands */
1617                 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
1618                         /* Get consistent memory allocated for SNS commands */
1619                         ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
1620                             sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
1621                             GFP_KERNEL);
1622                         if (ha->sns_cmd == NULL) {
1623                                 /* error */
1624                                 qla_printk(KERN_WARNING, ha,
1625                                     "Memory Allocation failed - sns_cmd\n");
1626
1627                                 qla2x00_mem_free(ha);
1628                                 msleep(100);
1629
1630                                 continue;
1631                         }
1632                         memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
1633                 } else {
1634                         /* Get consistent memory allocated for MS IOCB */
1635                         ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
1636                             &ha->ms_iocb_dma);
1637                         if (ha->ms_iocb == NULL) {
1638                                 /* error */
1639                                 qla_printk(KERN_WARNING, ha,
1640                                     "Memory Allocation failed - ms_iocb\n");
1641
1642                                 qla2x00_mem_free(ha);
1643                                 msleep(100);
1644
1645                                 continue;
1646                         }
1647                         memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
1648
1649                         /*
1650                          * Get consistent memory allocated for CT SNS
1651                          * commands
1652                          */
1653                         ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
1654                             sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
1655                             GFP_KERNEL);
1656                         if (ha->ct_sns == NULL) {
1657                                 /* error */
1658                                 qla_printk(KERN_WARNING, ha,
1659                                     "Memory Allocation failed - ct_sns\n");
1660
1661                                 qla2x00_mem_free(ha);
1662                                 msleep(100);
1663
1664                                 continue;
1665                         }
1666                         memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
1667                 }
1668
1669                 /* Done all allocations without any error. */
1670                 status = 0;
1671
1672         } while (retry-- && status != 0);
1673
1674         if (status) {
1675                 printk(KERN_WARNING
1676                         "%s(): **** FAILED ****\n", __func__);
1677         }
1678
1679         return(status);
1680 }
1681
1682 /*
1683 * qla2x00_mem_free
1684 *      Frees all adapter allocated memory.
1685 *
1686 * Input:
1687 *      ha = adapter block pointer.
1688 */
1689 static void
1690 qla2x00_mem_free(scsi_qla_host_t *ha)
1691 {
1692         struct list_head        *fcpl, *fcptemp;
1693         fc_port_t       *fcport;
1694         unsigned long   wtime;/* max wait time if mbx cmd is busy. */
1695
1696         if (ha == NULL) {
1697                 /* error */
1698                 DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
1699                 return;
1700         }
1701
1702         /* Make sure all other threads are stopped. */
1703         wtime = 60 * HZ;
1704         while (ha->dpc_wait && wtime) {
1705                 set_current_state(TASK_INTERRUPTIBLE);
1706                 wtime = schedule_timeout(wtime);
1707         }
1708
1709         /* free ioctl memory */
1710         qla2x00_free_ioctl_mem(ha);
1711
1712         /* free sp pool */
1713         qla2x00_free_sp_pool(ha);
1714
1715         if (ha->sns_cmd)
1716                 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
1717                     ha->sns_cmd, ha->sns_cmd_dma);
1718
1719         if (ha->ct_sns)
1720                 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
1721                     ha->ct_sns, ha->ct_sns_dma);
1722
1723         if (ha->ms_iocb)
1724                 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
1725
1726         if (ha->iodesc_pd)
1727                 dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
1728
1729         if (ha->init_cb)
1730                 dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
1731
1732         if (ha->s_dma_pool)
1733                 dma_pool_destroy(ha->s_dma_pool);
1734
1735         if (ha->rlc_rsp)
1736                 dma_free_coherent(&ha->pdev->dev,
1737                     sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
1738                     ha->rlc_rsp_dma);
1739
1740         if (ha->gid_list)
1741                 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
1742                     ha->gid_list_dma);
1743
1744         if (ha->response_ring)
1745                 dma_free_coherent(&ha->pdev->dev,
1746                     (ha->response_q_length + 1) * sizeof(response_t),
1747                     ha->response_ring, ha->response_dma);
1748
1749         if (ha->request_ring)
1750                 dma_free_coherent(&ha->pdev->dev,
1751                     (ha->request_q_length + 1) * sizeof(request_t),
1752                     ha->request_ring, ha->request_dma);
1753
1754         ha->sns_cmd = NULL;
1755         ha->sns_cmd_dma = 0;
1756         ha->ct_sns = NULL;
1757         ha->ct_sns_dma = 0;
1758         ha->ms_iocb = NULL;
1759         ha->ms_iocb_dma = 0;
1760         ha->iodesc_pd = NULL;
1761         ha->iodesc_pd_dma = 0;
1762         ha->init_cb = NULL;
1763         ha->init_cb_dma = 0;
1764
1765         ha->s_dma_pool = NULL;
1766
1767         ha->rlc_rsp = NULL;
1768         ha->rlc_rsp_dma = 0;
1769         ha->gid_list = NULL;
1770         ha->gid_list_dma = 0;
1771
1772         ha->response_ring = NULL;
1773         ha->response_dma = 0;
1774         ha->request_ring = NULL;
1775         ha->request_dma = 0;
1776
1777         list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
1778                 fcport = list_entry(fcpl, fc_port_t, list);
1779
1780                 /* fc ports */
1781                 list_del_init(&fcport->list);
1782                 kfree(fcport);
1783         }
1784         INIT_LIST_HEAD(&ha->fcports);
1785
1786         if (ha->fw_dump)
1787                 free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
1788
1789         if (ha->fw_dump_buffer)
1790                 vfree(ha->fw_dump_buffer);
1791
1792         ha->fw_dump = NULL;
1793         ha->fw_dump_reading = 0;
1794         ha->fw_dump_buffer = NULL;
1795 }
1796
1797 /*
1798  * qla2x00_allocate_sp_pool
1799  *       This routine is called during initialization to allocate
1800  *       memory for local srb_t.
1801  *
1802  * Input:
1803  *       ha   = adapter block pointer.
1804  *
1805  * Context:
1806  *      Kernel context.
1807  * 
1808  * Note: Sets the ref_count for non Null sp to one.
1809  */
1810 static int
1811 qla2x00_allocate_sp_pool(scsi_qla_host_t *ha) 
1812 {
1813         int      rval;
1814
1815         rval = QLA_SUCCESS;
1816         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
1817             mempool_free_slab, srb_cachep);
1818         if (ha->srb_mempool == NULL) {
1819                 qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
1820                 rval = QLA_FUNCTION_FAILED;
1821         }
1822         return (rval);
1823 }
1824
1825 /*
1826  *  This routine frees all adapter allocated memory.
1827  *  
1828  */
1829 static void
1830 qla2x00_free_sp_pool( scsi_qla_host_t *ha) 
1831 {
1832         if (ha->srb_mempool) {
1833                 mempool_destroy(ha->srb_mempool);
1834                 ha->srb_mempool = NULL;
1835         }
1836 }
1837
1838 /**************************************************************************
1839 * qla2x00_do_dpc
1840 *   This kernel thread is a task that is schedule by the interrupt handler
1841 *   to perform the background processing for interrupts.
1842 *
1843 * Notes:
1844 * This task always run in the context of a kernel thread.  It
1845 * is kick-off by the driver's detect code and starts up
1846 * up one per adapter. It immediately goes to sleep and waits for
1847 * some fibre event.  When either the interrupt handler or
1848 * the timer routine detects a event it will one of the task
1849 * bits then wake us up.
1850 **************************************************************************/
1851 static int
1852 qla2x00_do_dpc(void *data)
1853 {
1854         DECLARE_MUTEX_LOCKED(sem);
1855         scsi_qla_host_t *ha;
1856         fc_port_t       *fcport;
1857         uint8_t         status;
1858         uint16_t        next_loopid;
1859
1860         ha = (scsi_qla_host_t *)data;
1861
1862         lock_kernel();
1863
1864         daemonize("%s_dpc", ha->host_str);
1865         allow_signal(SIGHUP);
1866
1867         ha->dpc_wait = &sem;
1868
1869         set_user_nice(current, -20);
1870
1871         unlock_kernel();
1872
1873         complete(&ha->dpc_inited);
1874
1875         while (1) {
1876                 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
1877
1878                 if (down_interruptible(&sem))
1879                         break;
1880
1881                 if (ha->dpc_should_die)
1882                         break;
1883
1884                 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
1885
1886                 /* Initialization not yet finished. Don't do anything yet. */
1887                 if (!ha->flags.init_done || ha->dpc_active)
1888                         continue;
1889
1890                 DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
1891
1892                 ha->dpc_active = 1;
1893
1894                 if (ha->flags.mbox_busy) {
1895                         ha->dpc_active = 0;
1896                         continue;
1897                 }
1898
1899                 if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
1900
1901                         DEBUG(printk("scsi(%ld): dpc: sched "
1902                             "qla2x00_abort_isp ha = %p\n",
1903                             ha->host_no, ha));
1904                         if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
1905                             &ha->dpc_flags))) {
1906
1907                                 if (qla2x00_abort_isp(ha)) {
1908                                         /* failed. retry later */
1909                                         set_bit(ISP_ABORT_NEEDED,
1910                                             &ha->dpc_flags);
1911                                 }
1912                                 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
1913                         }
1914                         DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
1915                             ha->host_no));
1916                 }
1917
1918                 if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
1919                     (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
1920
1921                         DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
1922                             ha->host_no));
1923
1924                         qla2x00_rst_aen(ha);
1925                         clear_bit(RESET_ACTIVE, &ha->dpc_flags);
1926                 }
1927
1928                 /* Retry each device up to login retry count */
1929                 if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
1930                     !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
1931                     atomic_read(&ha->loop_state) != LOOP_DOWN) {
1932
1933                         DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
1934                             ha->host_no));
1935
1936                         next_loopid = 0;
1937                         list_for_each_entry(fcport, &ha->fcports, list) {
1938                                 if (fcport->port_type != FCT_TARGET)
1939                                         continue;
1940
1941                                 /*
1942                                  * If the port is not ONLINE then try to login
1943                                  * to it if we haven't run out of retries.
1944                                  */
1945                                 if (atomic_read(&fcport->state) != FCS_ONLINE &&
1946                                     fcport->login_retry) {
1947
1948                                         fcport->login_retry--;
1949                                         if (fcport->flags & FCF_FABRIC_DEVICE) {
1950                                                 if (fcport->flags &
1951                                                     FCF_TAPE_PRESENT)
1952                                                         qla2x00_fabric_logout(
1953                                                             ha,
1954                                                             fcport->loop_id);
1955                                                 status = qla2x00_fabric_login(
1956                                                     ha, fcport, &next_loopid);
1957                                         } else
1958                                                 status =
1959                                                     qla2x00_local_device_login(
1960                                                         ha, fcport->loop_id);
1961
1962                                         if (status == QLA_SUCCESS) {
1963                                                 fcport->old_loop_id = fcport->loop_id;
1964
1965                                                 DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
1966                                                     ha->host_no, fcport->loop_id));
1967                                                 
1968                                                 fcport->port_login_retry_count =
1969                                                     ha->port_down_retry_count * PORT_RETRY_TIME;
1970                                                 atomic_set(&fcport->state, FCS_ONLINE);
1971                                                 atomic_set(&fcport->port_down_timer,
1972                                                     ha->port_down_retry_count * PORT_RETRY_TIME);
1973
1974                                                 fcport->login_retry = 0;
1975                                         } else if (status == 1) {
1976                                                 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
1977                                                 /* retry the login again */
1978                                                 DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
1979                                                     ha->host_no,
1980                                                     fcport->login_retry, fcport->loop_id));
1981                                         } else {
1982                                                 fcport->login_retry = 0;
1983                                         }
1984                                 }
1985                                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
1986                                         break;
1987                         }
1988                         DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
1989                             ha->host_no));
1990                 }
1991
1992                 if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
1993                     atomic_read(&ha->loop_state) != LOOP_DOWN) {
1994
1995                         clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
1996                         DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
1997                             ha->host_no));
1998                                 
1999                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
2000
2001                         DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
2002                             ha->host_no));
2003                 }
2004
2005                 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
2006
2007                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
2008                             ha->host_no));
2009
2010                         if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
2011                             &ha->dpc_flags))) {
2012
2013                                 qla2x00_loop_resync(ha);
2014
2015                                 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
2016                         }
2017
2018                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
2019                             ha->host_no));
2020                 }
2021
2022                 if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
2023
2024                         DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
2025                             ha->host_no));
2026
2027                         qla2x00_rescan_fcports(ha);
2028
2029                         DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
2030                             "end.\n",
2031                             ha->host_no));
2032                 }
2033
2034                 if (!ha->interrupts_on)
2035                         qla2x00_enable_intrs(ha);
2036
2037                 ha->dpc_active = 0;
2038         } /* End of while(1) */
2039
2040         DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
2041
2042         /*
2043          * Make sure that nobody tries to wake us up again.
2044          */
2045         ha->dpc_wait = NULL;
2046         ha->dpc_active = 0;
2047
2048         complete_and_exit(&ha->dpc_exited, 0);
2049 }
2050
2051 /*
2052 *  qla2x00_rst_aen
2053 *      Processes asynchronous reset.
2054 *
2055 * Input:
2056 *      ha  = adapter block pointer.
2057 */
2058 static void
2059 qla2x00_rst_aen(scsi_qla_host_t *ha) 
2060 {
2061         if (ha->flags.online && !ha->flags.reset_active &&
2062             !atomic_read(&ha->loop_down_timer) &&
2063             !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
2064                 do {
2065                         clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
2066
2067                         /*
2068                          * Issue marker command only when we are going to start
2069                          * the I/O.
2070                          */
2071                         ha->marker_needed = 1;
2072                 } while (!atomic_read(&ha->loop_down_timer) &&
2073                     (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
2074         }
2075 }
2076
2077
2078 /*
2079  * This routine will allocate SP from the free queue
2080  * input:
2081  *        scsi_qla_host_t *
2082  * output:
2083  *        srb_t * or NULL
2084  */
2085 static srb_t *
2086 qla2x00_get_new_sp(scsi_qla_host_t *ha)
2087 {
2088         srb_t *sp;
2089
2090         sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
2091         if (sp)
2092                 atomic_set(&sp->ref_count, 1);
2093         return (sp);
2094 }
2095
2096 static void
2097 qla2x00_sp_free_dma(scsi_qla_host_t *ha, srb_t *sp)
2098 {
2099         struct scsi_cmnd *cmd = sp->cmd;
2100
2101         if (sp->flags & SRB_DMA_VALID) {
2102                 if (cmd->use_sg) {
2103                         dma_unmap_sg(&ha->pdev->dev, cmd->request_buffer,
2104                             cmd->use_sg, cmd->sc_data_direction);
2105                 } else if (cmd->request_bufflen) {
2106                         dma_unmap_single(&ha->pdev->dev, sp->dma_handle,
2107                             cmd->request_bufflen, cmd->sc_data_direction);
2108                 }
2109                 sp->flags &= ~SRB_DMA_VALID;
2110         }
2111 }
2112
2113 void
2114 qla2x00_sp_compl(scsi_qla_host_t *ha, srb_t *sp)
2115 {
2116         struct scsi_cmnd *cmd = sp->cmd;
2117
2118         qla2x00_sp_free_dma(ha, sp);
2119
2120         CMD_SP(cmd) = NULL;
2121         mempool_free(sp, ha->srb_mempool);
2122
2123         cmd->scsi_done(cmd);
2124 }
2125
2126 /**************************************************************************
2127 *   qla2x00_timer
2128 *
2129 * Description:
2130 *   One second timer
2131 *
2132 * Context: Interrupt
2133 ***************************************************************************/
2134 static void
2135 qla2x00_timer(scsi_qla_host_t *ha)
2136 {
2137         unsigned long   cpu_flags = 0;
2138         fc_port_t       *fcport;
2139         int             start_dpc = 0;
2140         int             index;
2141         srb_t           *sp;
2142         int             t;
2143
2144         /*
2145          * Ports - Port down timer.
2146          *
2147          * Whenever, a port is in the LOST state we start decrementing its port
2148          * down timer every second until it reaches zero. Once  it reaches zero
2149          * the port it marked DEAD. 
2150          */
2151         t = 0;
2152         list_for_each_entry(fcport, &ha->fcports, list) {
2153                 if (fcport->port_type != FCT_TARGET)
2154                         continue;
2155
2156                 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
2157
2158                         if (atomic_read(&fcport->port_down_timer) == 0)
2159                                 continue;
2160
2161                         if (atomic_dec_and_test(&fcport->port_down_timer) != 0) 
2162                                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
2163                         
2164                         DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
2165                             "%d remainning\n",
2166                             ha->host_no,
2167                             t, atomic_read(&fcport->port_down_timer)));
2168                 }
2169                 t++;
2170         } /* End of for fcport  */
2171
2172
2173         /* Loop down handler. */
2174         if (atomic_read(&ha->loop_down_timer) > 0 &&
2175             !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
2176
2177                 if (atomic_read(&ha->loop_down_timer) ==
2178                     ha->loop_down_abort_time) {
2179
2180                         DEBUG(printk("scsi(%ld): Loop Down - aborting the "
2181                             "queues before time expire\n",
2182                             ha->host_no));
2183
2184                         if (!IS_QLA2100(ha) && ha->link_down_timeout)
2185                                 atomic_set(&ha->loop_state, LOOP_DEAD); 
2186
2187                         /* Schedule an ISP abort to return any tape commands. */
2188                         spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
2189                         for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
2190                             index++) {
2191                                 fc_port_t *sfcp;
2192
2193                                 sp = ha->outstanding_cmds[index];
2194                                 if (!sp)
2195                                         continue;
2196                                 sfcp = sp->fcport;
2197                                 if (!(sfcp->flags & FCF_TAPE_PRESENT))
2198                                         continue;
2199
2200                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2201                                 break;
2202                         }
2203                         spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
2204
2205                         set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
2206                         start_dpc++;
2207                 }
2208
2209                 /* if the loop has been down for 4 minutes, reinit adapter */
2210                 if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
2211                         DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
2212                             "restarting queues.\n",
2213                             ha->host_no));
2214
2215                         set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
2216                         start_dpc++;
2217
2218                         if (!(ha->device_flags & DFLG_NO_CABLE)) {
2219                                 DEBUG(printk("scsi(%ld): Loop down - "
2220                                     "aborting ISP.\n",
2221                                     ha->host_no));
2222                                 qla_printk(KERN_WARNING, ha,
2223                                     "Loop down - aborting ISP.\n");
2224
2225                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
2226                         }
2227                 }
2228                 DEBUG3(printk("scsi(%ld): Loop Down - seconds remainning %d\n",
2229                     ha->host_no,
2230                     atomic_read(&ha->loop_down_timer)));
2231         }
2232
2233         /* Schedule the DPC routine if needed */
2234         if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
2235             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
2236             start_dpc ||
2237             test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
2238             test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) ||
2239             test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
2240             ha->dpc_wait && !ha->dpc_active) {
2241
2242                 up(ha->dpc_wait);
2243         }
2244
2245         qla2x00_restart_timer(ha, WATCH_INTERVAL);
2246 }
2247
2248 /* XXX(hch): crude hack to emulate a down_timeout() */
2249 int
2250 qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
2251 {
2252         const unsigned int step = HZ/10;
2253
2254         do {
2255                 if (!down_trylock(sema))
2256                         return 0;
2257                 set_current_state(TASK_INTERRUPTIBLE);
2258                 if (schedule_timeout(step))
2259                         break;
2260         } while ((timeout -= step) > 0);
2261
2262         return -ETIMEDOUT;
2263 }
2264
2265 /**
2266  * qla2x00_module_init - Module initialization.
2267  **/
2268 static int __init
2269 qla2x00_module_init(void)
2270 {
2271         /* Allocate cache for SRBs. */
2272         srb_cachep = kmem_cache_create("qla2xxx_srbs", sizeof(srb_t), 0,
2273             SLAB_HWCACHE_ALIGN, NULL, NULL);
2274         if (srb_cachep == NULL) {
2275                 printk(KERN_ERR
2276                     "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
2277                 return -ENOMEM;
2278         }
2279
2280         /* Derive version string. */
2281         strcpy(qla2x00_version_str, QLA2XXX_VERSION);
2282 #if DEBUG_QLA2100
2283         strcat(qla2x00_version_str, "-debug");
2284 #endif
2285         qla2xxx_transport_template =
2286             fc_attach_transport(&qla2xxx_transport_functions);
2287         if (!qla2xxx_transport_template)
2288                 return -ENODEV;
2289
2290         printk(KERN_INFO "QLogic Fibre Channel HBA Driver\n");
2291         return 0;
2292 }
2293
2294 /**
2295  * qla2x00_module_exit - Module cleanup.
2296  **/
2297 static void __exit
2298 qla2x00_module_exit(void)
2299 {
2300         kmem_cache_destroy(srb_cachep);
2301         fc_release_transport(qla2xxx_transport_template);
2302 }
2303
2304 module_init(qla2x00_module_init);
2305 module_exit(qla2x00_module_exit);
2306
2307 MODULE_AUTHOR("QLogic Corporation");
2308 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
2309 MODULE_LICENSE("GPL");
2310 MODULE_VERSION(QLA2XXX_VERSION);