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