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