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