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