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