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