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