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