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