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