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