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