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