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