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