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