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