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