upgrade to linux 2.6.9-1.11_FC2
[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 char srb_cachep_name[16];
40 kmem_cache_t *srb_cachep;
41
42 /*
43  * Stats for all adpaters.
44  */
45 struct _qla2x00stats qla2x00_stats;
46
47 /*
48  * Ioctl related information.
49  */
50 int num_hosts;
51 int apiHBAInstance;
52
53 /*
54  * Module parameter information and variables
55  */
56 int ql2xmaxqdepth;
57 module_param(ql2xmaxqdepth, int, S_IRUGO|S_IWUSR);
58 MODULE_PARM_DESC(ql2xmaxqdepth,
59                 "Maximum queue depth to report for target devices.");
60
61 int ql2xlogintimeout = 20;
62 module_param(ql2xlogintimeout, int, S_IRUGO|S_IRUSR);
63 MODULE_PARM_DESC(ql2xlogintimeout,
64                 "Login timeout value in seconds.");
65
66 int qlport_down_retry;
67 module_param(qlport_down_retry, int, S_IRUGO|S_IRUSR);
68 MODULE_PARM_DESC(qlport_down_retry,
69                 "Maximum number of command retries to a port that returns"
70                 "a PORT-DOWN status.");
71
72 int ql2xretrycount = 20;
73 module_param(ql2xretrycount, int, S_IRUGO|S_IWUSR);
74 MODULE_PARM_DESC(ql2xretrycount,
75                 "Maximum number of mid-layer retries allowed for a command.  "
76                 "Default value is 20, ");
77
78 int displayConfig;
79 module_param(displayConfig, int, S_IRUGO|S_IWUSR);
80 MODULE_PARM_DESC(displayConfig,
81                 "If 1 then display the configuration used in /etc/modprobe.conf.");
82
83 int ql2xplogiabsentdevice;
84 module_param(ql2xplogiabsentdevice, int, S_IRUGO|S_IWUSR);
85 MODULE_PARM_DESC(ql2xplogiabsentdevice,
86                 "Option to enable PLOGI to devices that are not present after "
87                 "a Fabric scan.  This is needed for several broken switches."
88                 "Default is 0 - no PLOGI. 1 - perfom PLOGI.");
89
90 int ql2xenablezio = 0;
91 module_param(ql2xenablezio, int, S_IRUGO|S_IRUSR);
92 MODULE_PARM_DESC(ql2xenablezio,
93                 "Option to enable ZIO:If 1 then enable it otherwise" 
94                 " use the default set in the NVRAM."
95                 " Default is 0 : disabled");
96
97 int ql2xintrdelaytimer = 10;
98 module_param(ql2xintrdelaytimer, int, S_IRUGO|S_IRUSR);
99 MODULE_PARM_DESC(ql2xintrdelaytimer,
100                 "ZIO: Waiting time for Firmware before it generates an "
101                 "interrupt to the host to notify completion of request.");
102
103 int ConfigRequired;
104 module_param(ConfigRequired, int, S_IRUGO|S_IRUSR);
105 MODULE_PARM_DESC(ConfigRequired,
106                 "If 1, then only configured devices passed in through the"
107                 "ql2xopts parameter will be presented to the OS");
108
109 int Bind = BIND_BY_PORT_NAME;
110 module_param(Bind, int, S_IRUGO|S_IRUSR);
111 MODULE_PARM_DESC(Bind,
112                 "Target persistent binding method: "
113                 "0 by Portname (default); 1 by PortID; 2 by Nodename. ");
114
115 int ql2xsuspendcount = SUSPEND_COUNT;
116 module_param(ql2xsuspendcount, int, S_IRUGO|S_IWUSR);
117 MODULE_PARM_DESC(ql2xsuspendcount,
118                 "Number of 6-second suspend iterations to perform while a "
119                 "target returns a <NOT READY> status.  Default is 10 "
120                 "iterations.");
121
122 int ql2xdoinitscan = 1;
123 module_param(ql2xdoinitscan, int, S_IRUGO|S_IWUSR);
124 MODULE_PARM_DESC(ql2xdoinitscan,
125                 "Signal mid-layer to perform scan after driver load: 0 -- no "
126                 "signal sent to mid-layer.");
127
128 int ql2xloginretrycount = 0;
129 module_param(ql2xloginretrycount, int, S_IRUGO|S_IRUSR);
130 MODULE_PARM_DESC(ql2xloginretrycount,
131                 "Specify an alternate value for the NVRAM login retry count.");
132
133 /*
134  * Proc structures and functions
135  */
136 struct info_str {
137         char    *buffer;
138         int     length;
139         off_t   offset;
140         int     pos;
141 };
142
143 static void copy_mem_info(struct info_str *, char *, int);
144 static int copy_info(struct info_str *, char *, ...);
145
146 static void qla2x00_free_device(scsi_qla_host_t *);
147
148 static void qla2x00_config_dma_addressing(scsi_qla_host_t *ha);
149
150 /*
151  * SCSI host template entry points 
152  */
153 static int qla2xxx_slave_configure(struct scsi_device * device);
154 static int qla2xxx_eh_abort(struct scsi_cmnd *);
155 static int qla2xxx_eh_device_reset(struct scsi_cmnd *);
156 static int qla2xxx_eh_bus_reset(struct scsi_cmnd *);
157 static int qla2xxx_eh_host_reset(struct scsi_cmnd *);
158 static int qla2x00_loop_reset(scsi_qla_host_t *ha);
159 static int qla2x00_device_reset(scsi_qla_host_t *, fc_port_t *);
160
161 static int qla2x00_proc_info(struct Scsi_Host *, char *, char **,
162     off_t, int, int);
163
164 static struct scsi_host_template qla2x00_driver_template = {
165         .module                 = THIS_MODULE,
166         .name                   = "qla2xxx",
167         .proc_name              = "qla2xxx",
168         .proc_info              = qla2x00_proc_info,
169         .queuecommand           = qla2x00_queuecommand,
170
171         .eh_abort_handler       = qla2xxx_eh_abort,
172         .eh_device_reset_handler = qla2xxx_eh_device_reset,
173         .eh_bus_reset_handler   = qla2xxx_eh_bus_reset,
174         .eh_host_reset_handler  = qla2xxx_eh_host_reset,
175
176         .slave_configure        = qla2xxx_slave_configure,
177
178         .this_id                = -1,
179         .cmd_per_lun            = 3,
180         .use_clustering         = ENABLE_CLUSTERING,
181         .sg_tablesize           = SG_ALL,
182
183         /*
184          * The RISC allows for each command to transfer (2^32-1) bytes of data,
185          * which equates to 0x800000 sectors.
186          */
187         .max_sectors            = 0xFFFF,
188 };
189
190 static struct scsi_transport_template *qla2xxx_transport_template = NULL;
191
192 static void qla2x00_display_fc_names(scsi_qla_host_t *);
193
194 /* TODO Convert to inlines
195  *
196  * Timer routines
197  */
198 #define WATCH_INTERVAL          1       /* number of seconds */
199
200 static void qla2x00_timer(scsi_qla_host_t *);
201
202 static __inline__ void qla2x00_start_timer(scsi_qla_host_t *,
203     void *, unsigned long);
204 static __inline__ void qla2x00_restart_timer(scsi_qla_host_t *, unsigned long);
205 static __inline__ void qla2x00_stop_timer(scsi_qla_host_t *);
206
207 static inline void
208 qla2x00_start_timer(scsi_qla_host_t *ha, void *func, unsigned long interval)
209 {
210         init_timer(&ha->timer);
211         ha->timer.expires = jiffies + interval * HZ;
212         ha->timer.data = (unsigned long)ha;
213         ha->timer.function = (void (*)(unsigned long))func;
214         add_timer(&ha->timer);
215         ha->timer_active = 1;
216 }
217
218 static inline void
219 qla2x00_restart_timer(scsi_qla_host_t *ha, unsigned long interval)
220 {
221         mod_timer(&ha->timer, jiffies + interval * HZ);
222 }
223
224 static __inline__ void
225 qla2x00_stop_timer(scsi_qla_host_t *ha)
226 {
227         del_timer_sync(&ha->timer);
228         ha->timer_active = 0;
229 }
230
231 void qla2x00_cmd_timeout(srb_t *);
232
233 static __inline__ void qla2x00_callback(scsi_qla_host_t *, struct scsi_cmnd *);
234 static __inline__ void sp_put(struct scsi_qla_host * ha, srb_t *sp);
235 static __inline__ void sp_get(struct scsi_qla_host * ha, srb_t *sp);
236 static __inline__ void
237 qla2x00_delete_from_done_queue(scsi_qla_host_t *, srb_t *); 
238
239 /*
240 * qla2x00_callback
241 *      Returns the completed SCSI command to LINUX.
242 *
243 * Input:
244 *       ha -- Host adapter structure
245 *       cmd -- SCSI mid-level command structure.
246 * Returns:
247 *      None
248 * Note:From failover point of view we always get the sp
249 *      from vis_ha pool in queuecommand.So when we put it 
250 *      back to the pool it has to be the vis_ha.         
251 *      So rely on struct scsi_cmnd to get the vis_ha and not on sp.                     
252 */
253 static inline void
254 qla2x00_callback(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
255 {
256         srb_t *sp = (srb_t *) CMD_SP(cmd);
257         scsi_qla_host_t *vis_ha;
258         os_lun_t *lq;
259         int got_sense;
260         unsigned long   cpu_flags = 0;
261
262         cmd->host_scribble = (unsigned char *) NULL;
263         vis_ha = (scsi_qla_host_t *) cmd->device->host->hostdata;
264
265         if (sp == NULL) {
266                 qla_printk(KERN_INFO, ha,
267                         "%s(): **** CMD derives a NULL SP\n",
268                         __func__);
269                 DEBUG2(BUG();)
270                 return;
271         }
272
273         /*
274          * If command status is not DID_BUS_BUSY then go ahead and freed sp.
275          */
276         /*
277          * Cancel command timeout
278          */
279         qla2x00_delete_timer_from_cmd(sp);
280
281         /*
282          * Put SP back in the free queue
283          */
284         sp->cmd   = NULL;
285         CMD_SP(cmd) = NULL;
286         lq = sp->lun_queue;
287         got_sense = (sp->flags & SRB_GOT_SENSE)? 1: 0;
288         add_to_free_queue(vis_ha, sp);
289
290         if (host_byte(cmd->result) == DID_OK) {
291                 /* device ok */
292                 ha->total_bytes += cmd->bufflen;
293                 if (!got_sense) {
294                         /* If lun was suspended then clear retry count */
295                         spin_lock_irqsave(&lq->q_lock, cpu_flags);
296                         if (!test_bit(LUN_EXEC_DELAYED, &lq->q_flag))
297                                 lq->q_state = LUN_STATE_READY;
298                         spin_unlock_irqrestore(&lq->q_lock, cpu_flags);
299                 }
300         } else if (host_byte(cmd->result) == DID_ERROR) {
301                 /* device error */
302                 ha->total_dev_errs++;
303         }
304
305         /* Call the mid-level driver interrupt handler */
306         (*(cmd)->scsi_done)(cmd);
307 }
308
309 /**************************************************************************
310 * sp_put
311 *
312 * Description:
313 *   Decrement reference count and call the callback if we're the last
314 *   owner of the specified sp. Will get the host_lock before calling
315 *   the callback.
316 *
317 * Input:
318 *   ha - pointer to the scsi_qla_host_t where the callback is to occur.
319 *   sp - pointer to srb_t structure to use.
320 *
321 * Returns:
322 *
323 **************************************************************************/
324 static inline void
325 sp_put(struct scsi_qla_host * ha, srb_t *sp)
326 {
327         if (atomic_read(&sp->ref_count) == 0) {
328                 qla_printk(KERN_INFO, ha,
329                         "%s(): **** SP->ref_count not zero\n",
330                         __func__);
331                 DEBUG2(BUG();)
332
333                 return;
334         }
335
336         if (!atomic_dec_and_test(&sp->ref_count)) {
337                 return;
338         }
339
340         qla2x00_callback(ha, sp->cmd);
341 }
342
343 /**************************************************************************
344 * sp_get
345 *
346 * Description:
347 *   Increment reference count of the specified sp.
348 *
349 * Input:
350 *   sp - pointer to srb_t structure to use.
351 *
352 * Returns:
353 *
354 **************************************************************************/
355 static inline void
356 sp_get(struct scsi_qla_host * ha, srb_t *sp)
357 {
358         atomic_inc(&sp->ref_count);
359
360         if (atomic_read(&sp->ref_count) > 2) {
361                 qla_printk(KERN_INFO, ha,
362                         "%s(): **** SP->ref_count greater than two\n",
363                         __func__);
364                 DEBUG2(BUG();)
365
366                 return;
367         }
368 }
369
370 static inline void 
371 qla2x00_delete_from_done_queue(scsi_qla_host_t *dest_ha, srb_t *sp) 
372 {
373         /* remove command from done list */
374         list_del_init(&sp->list);
375         dest_ha->done_q_cnt--;
376         sp->state = SRB_NO_QUEUE_STATE;
377
378         if (sp->flags & SRB_DMA_VALID) {
379                 sp->flags &= ~SRB_DMA_VALID;
380
381                 /* Release memory used for this I/O */
382                 if (sp->cmd->use_sg) {
383                         pci_unmap_sg(dest_ha->pdev, sp->cmd->request_buffer,
384                             sp->cmd->use_sg, sp->cmd->sc_data_direction);
385                 } else if (sp->cmd->request_bufflen) {
386                         pci_unmap_page(dest_ha->pdev, sp->dma_handle,
387                             sp->cmd->request_bufflen,
388                             sp->cmd->sc_data_direction);
389                 }
390         }
391 }
392
393 static int qla2x00_do_dpc(void *data);
394
395 static void qla2x00_rst_aen(scsi_qla_host_t *);
396
397 static uint8_t qla2x00_mem_alloc(scsi_qla_host_t *);
398 static void qla2x00_mem_free(scsi_qla_host_t *ha);
399 int qla2x00_allocate_sp_pool( scsi_qla_host_t *ha);
400 void qla2x00_free_sp_pool(scsi_qla_host_t *ha);
401
402 static ssize_t qla2x00_sysfs_read_fw_dump(struct kobject *, char *, loff_t,
403     size_t);
404 static ssize_t qla2x00_sysfs_write_fw_dump(struct kobject *, char *, loff_t,
405     size_t);
406 static struct bin_attribute sysfs_fw_dump_attr = {
407         .attr = {
408                 .name = "fw_dump",
409                 .mode = S_IRUSR | S_IWUSR,
410                 .owner = THIS_MODULE,
411         },
412         .size = 0,
413         .read = qla2x00_sysfs_read_fw_dump,
414         .write = qla2x00_sysfs_write_fw_dump,
415 };
416 static ssize_t qla2x00_sysfs_read_nvram(struct kobject *, char *, loff_t,
417     size_t);
418 static ssize_t qla2x00_sysfs_write_nvram(struct kobject *, char *, loff_t,
419     size_t);
420 static struct bin_attribute sysfs_nvram_attr = {
421         .attr = {
422                 .name = "nvram",
423                 .mode = S_IRUSR | S_IWUSR,
424                 .owner = THIS_MODULE,
425         },
426         .size = sizeof(nvram_t),
427         .read = qla2x00_sysfs_read_nvram,
428         .write = qla2x00_sysfs_write_nvram,
429 };
430
431
432 int
433 qla2x00_set_info(char *buffer, int length, struct Scsi_Host *shost)
434 {
435         return (-ENOSYS);  /* Currently this is a no-op */
436 }
437
438 /* -------------------------------------------------------------------------- */
439
440
441 /* SysFS attributes. */
442 static ssize_t qla2x00_sysfs_read_fw_dump(struct kobject *kobj, char *buf,
443     loff_t off, size_t count)
444 {
445         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
446             struct device, kobj)));
447
448         if (ha->fw_dump_reading == 0)
449                 return 0;
450         if (off > ha->fw_dump_buffer_len)
451                 return 0;
452         if (off + count > ha->fw_dump_buffer_len)
453                 count = ha->fw_dump_buffer_len - off;
454
455         memcpy(buf, &ha->fw_dump_buffer[off], count);
456
457         return (count);
458 }
459
460 static ssize_t qla2x00_sysfs_write_fw_dump(struct kobject *kobj, char *buf,
461     loff_t off, size_t count)
462 {
463         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
464             struct device, kobj)));
465         int reading;
466         uint32_t dump_size;
467
468         if (off != 0)
469                 return (0);
470
471         reading = simple_strtol(buf, NULL, 10);
472         switch (reading) {
473         case 0:
474                 if (ha->fw_dump_reading == 1) {
475                         qla_printk(KERN_INFO, ha,
476                             "Firmware dump cleared on (%ld).\n",
477                             ha->host_no);
478
479                         vfree(ha->fw_dump_buffer);
480                         free_pages((unsigned long)ha->fw_dump,
481                             ha->fw_dump_order);
482
483                         ha->fw_dump_reading = 0;
484                         ha->fw_dump_buffer = NULL;
485                         ha->fw_dump = NULL;
486                 }
487                 break;
488         case 1:
489                 if (ha->fw_dump != NULL && !ha->fw_dump_reading) {
490                         ha->fw_dump_reading = 1;
491
492                         dump_size = FW_DUMP_SIZE_1M;
493                         if (ha->fw_memory_size < 0x20000) 
494                                 dump_size = FW_DUMP_SIZE_128K;
495                         else if (ha->fw_memory_size < 0x80000) 
496                                 dump_size = FW_DUMP_SIZE_512K;
497                         ha->fw_dump_buffer = (char *)vmalloc(dump_size);
498                         if (ha->fw_dump_buffer == NULL) {
499                                 qla_printk(KERN_WARNING, ha,
500                                     "Unable to allocate memory for firmware "
501                                     "dump buffer (%d).\n", dump_size);
502
503                                 ha->fw_dump_reading = 0;
504                                 return (count);
505                         }
506                         qla_printk(KERN_INFO, ha,
507                             "Firmware dump ready for read on (%ld).\n",
508                             ha->host_no);
509                         memset(ha->fw_dump_buffer, 0, dump_size);
510                         if (IS_QLA2100(ha) || IS_QLA2200(ha))
511                                 qla2100_ascii_fw_dump(ha);
512                         else
513                                 qla2300_ascii_fw_dump(ha);
514                         ha->fw_dump_buffer_len = strlen(ha->fw_dump_buffer);
515                 }
516                 break;
517         }
518         return (count);
519 }
520
521 static ssize_t qla2x00_sysfs_read_nvram(struct kobject *kobj, char *buf,
522     loff_t off, size_t count)
523 {
524         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
525             struct device, kobj)));
526         uint16_t        *witer;
527         unsigned long   flags;
528         uint16_t        cnt;
529
530         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != sizeof(nvram_t))
531                 return 0;
532
533         /* Read NVRAM. */
534         spin_lock_irqsave(&ha->hardware_lock, flags);
535         qla2x00_lock_nvram_access(ha);
536         witer = (uint16_t *)buf;
537         for (cnt = 0; cnt < count / 2; cnt++) {
538                 *witer = cpu_to_le16(qla2x00_get_nvram_word(ha,
539                     cnt+ha->nvram_base));
540                 witer++;
541         }
542         qla2x00_unlock_nvram_access(ha);
543         spin_unlock_irqrestore(&ha->hardware_lock, flags);
544
545         return (count);
546 }
547
548 static ssize_t qla2x00_sysfs_write_nvram(struct kobject *kobj, char *buf,
549     loff_t off, size_t count)
550 {
551         struct scsi_qla_host *ha = to_qla_host(dev_to_shost(container_of(kobj,
552             struct device, kobj)));
553         uint8_t         *iter;
554         uint16_t        *witer;
555         unsigned long   flags;
556         uint16_t        cnt;
557         uint8_t         chksum;
558
559         if (!capable(CAP_SYS_ADMIN) || off != 0 || count != sizeof(nvram_t))
560                 return 0;
561
562         /* Checksum NVRAM. */
563         iter = (uint8_t *)buf;
564         chksum = 0;
565         for (cnt = 0; cnt < count - 1; cnt++)
566                 chksum += *iter++;
567         chksum = ~chksum + 1;
568         *iter = chksum;
569
570         /* Write NVRAM. */
571         spin_lock_irqsave(&ha->hardware_lock, flags);
572         qla2x00_lock_nvram_access(ha);
573         witer = (uint16_t *)buf;
574         for (cnt = 0; cnt < count / 2; cnt++) {
575                 qla2x00_write_nvram_word(ha, cnt+ha->nvram_base,
576                     cpu_to_le16(*witer));
577                 witer++;
578         }
579         qla2x00_unlock_nvram_access(ha);
580         spin_unlock_irqrestore(&ha->hardware_lock, flags);
581
582         return (count);
583 }
584
585 /* -------------------------------------------------------------------------- */
586 char *
587 qla2x00_get_pci_info_str(struct scsi_qla_host *ha, char *str)
588 {
589         static char *pci_bus_modes[] = {
590                 "33", "66", "100", "133",
591         };
592         uint16_t pci_bus;
593
594         strcpy(str, "PCI");
595         pci_bus = (ha->pci_attr & (BIT_9 | BIT_10)) >> 9;
596         if (pci_bus) {
597                 strcat(str, "-X (");
598                 strcat(str, pci_bus_modes[pci_bus]);
599         } else {
600                 pci_bus = (ha->pci_attr & BIT_8) >> 8;
601                 strcat(str, " (");
602                 strcat(str, pci_bus_modes[pci_bus]);
603         }
604         strcat(str, " MHz)");
605
606         return (str);
607 }
608
609 char *
610 qla2x00_get_fw_version_str(struct scsi_qla_host *ha, char *str)
611 {
612         char un_str[10];
613         
614         sprintf(str, "%d.%02d.%02d ", ha->fw_major_version,
615             ha->fw_minor_version,
616             ha->fw_subminor_version);
617
618         if (ha->fw_attributes & BIT_9) {
619                 strcat(str, "FLX");
620                 return (str);
621         }
622
623         switch (ha->fw_attributes & 0xFF) {
624         case 0x7:
625                 strcat(str, "EF");
626                 break;
627         case 0x17:
628                 strcat(str, "TP");
629                 break;
630         case 0x37:
631                 strcat(str, "IP");
632                 break;
633         case 0x77:
634                 strcat(str, "VI");
635                 break;
636         default:
637                 sprintf(un_str, "(%x)", ha->fw_attributes);
638                 strcat(str, un_str);
639                 break;
640         }
641         if (ha->fw_attributes & 0x100)
642                 strcat(str, "X");
643
644         return (str);
645 }
646
647 /**************************************************************************
648 * qla2x00_queuecommand
649 *
650 * Description:
651 *     Queue a command to the controller.
652 *
653 * Input:
654 *     cmd - pointer to Scsi cmd structure
655 *     fn - pointer to Scsi done function
656 *
657 * Returns:
658 *   0 - Always
659 *
660 * Note:
661 * The mid-level driver tries to ensures that queuecommand never gets invoked
662 * concurrently with itself or the interrupt handler (although the
663 * interrupt handler may call this routine as part of request-completion
664 * handling).
665 **************************************************************************/
666 int
667 qla2x00_queuecommand(struct scsi_cmnd *cmd, void (*fn)(struct scsi_cmnd *))
668 {
669         fc_port_t       *fcport;
670         os_lun_t        *lq;
671         os_tgt_t        *tq;
672         scsi_qla_host_t *ha, *ha2;
673         srb_t           *sp;
674         struct Scsi_Host *host;
675         unsigned int    b, t, l;
676         unsigned long   handle;
677         int             was_empty;
678
679
680         host = cmd->device->host;
681         ha = (scsi_qla_host_t *) host->hostdata;
682         was_empty = 1;
683
684         cmd->scsi_done = fn;
685
686         spin_unlock_irq(ha->host->host_lock);
687
688         /*
689          * Allocate a command packet from the "sp" pool.  If we cant get back
690          * one then let scsi layer come back later.
691          */
692         if ((sp = qla2x00_get_new_sp(ha)) == NULL) {
693                 qla_printk(KERN_WARNING, ha,
694                     "Couldn't allocate memory for sp - retried.\n");
695
696                 spin_lock_irq(ha->host->host_lock);
697
698                 return (1);
699         }
700
701         sp->cmd = cmd;
702         CMD_SP(cmd) = (void *)sp;
703
704         sp->flags = 0;
705         if (CMD_RESID_LEN(cmd) & SRB_IOCTL) {
706                 /* Need to set sp->flags */
707                 sp->flags |= SRB_IOCTL;
708                 CMD_RESID_LEN(cmd) = 0; /* Clear it since no more use. */
709         }
710
711         sp->fo_retry_cnt = 0;
712         sp->err_id = 0;
713
714         /* Generate LU queue on bus, target, LUN */
715         b = cmd->device->channel;
716         t = cmd->device->id;
717         l = cmd->device->lun;
718
719         /*
720          * Start Command Timer. Typically it will be 2 seconds less than what
721          * is requested by the Host such that we can return the IO before
722          * aborts are called.
723          */
724         if ((cmd->timeout_per_command / HZ) > QLA_CMD_TIMER_DELTA)
725                 qla2x00_add_timer_to_cmd(sp,
726                     (cmd->timeout_per_command / HZ) - QLA_CMD_TIMER_DELTA);
727         else
728                 qla2x00_add_timer_to_cmd(sp, cmd->timeout_per_command / HZ);
729
730         if (l >= ha->max_luns) {
731                 cmd->result = DID_NO_CONNECT << 16;
732                 sp->err_id = SRB_ERR_PORT;
733
734                 spin_lock_irq(ha->host->host_lock);
735
736                 sp_put(ha, sp);
737
738                 return (0);
739         }
740
741         if ((tq = (os_tgt_t *) TGT_Q(ha, t)) != NULL &&
742             (lq = (os_lun_t *) LUN_Q(ha, t, l)) != NULL) {
743                 fcport = lq->fclun->fcport;
744                 ha2 = fcport->ha;
745         } else {
746                 lq = NULL;
747                 fcport = NULL;
748                 ha2 = ha;
749         }
750
751         /* Set an invalid handle until we issue the command to ISP */
752         /* then we will set the real handle value.                 */
753         handle = INVALID_HANDLE;
754         cmd->host_scribble = (unsigned char *)handle;
755
756         /* Bookkeeping information */
757         sp->r_start = jiffies;       /* time the request was received */
758         sp->u_start = 0;
759
760         /* Setup device queue pointers. */
761         sp->tgt_queue = tq;
762         sp->lun_queue = lq;
763
764         /*
765          * NOTE : q is NULL
766          *
767          * 1. When device is added from persistent binding but has not been
768          *    discovered yet.The state of loopid == PORT_AVAIL.
769          * 2. When device is never found on the bus.(loopid == UNUSED)
770          *
771          * IF Device Queue is not created, or device is not in a valid state
772          * and link down error reporting is enabled, reject IO.
773          */
774         if (fcport == NULL) {
775                 DEBUG3(printk("scsi(%ld:%2d:%2d): port unavailable\n",
776                     ha->host_no,t,l));
777
778                 cmd->result = DID_NO_CONNECT << 16;
779                 sp->err_id = SRB_ERR_PORT;
780
781                 spin_lock_irq(ha->host->host_lock);
782
783                 sp_put(ha, sp);
784
785                 return (0);
786         }
787
788         /* Only modify the allowed count if the target is a *non* tape device */
789         if ((fcport->flags & FCF_TAPE_PRESENT) == 0) {
790                 sp->flags &= ~SRB_TAPE;
791                 if (cmd->allowed < ql2xretrycount) {
792                         cmd->allowed = ql2xretrycount;
793                 }
794         } else
795                 sp->flags |= SRB_TAPE;
796
797
798         DEBUG5(printk("scsi(%ld:%2d:%2d): (queuecmd) queue sp = %p, "
799             "flags=0x%x fo retry=%d, pid=%ld\n",
800             ha->host_no, t, l, sp, sp->flags, sp->fo_retry_cnt,
801             cmd->serial_number));
802         DEBUG5(qla2x00_print_scsi_cmd(cmd));
803
804         sp->fclun = lq->fclun;
805         sp->ha = ha2;
806
807         if (cmd->sc_data_direction == DMA_BIDIRECTIONAL &&
808             cmd->request_bufflen != 0) {
809
810                 DEBUG2(printk(KERN_WARNING
811                     "scsi(%ld): Incorrect data direction - transfer "
812                     "length=%d, direction=%d, pid=%ld, opcode=%x\n",
813                     ha->host_no, cmd->request_bufflen, cmd->sc_data_direction,
814                     cmd->serial_number, cmd->cmnd[0]));
815         }
816
817         /* Final pre-check :
818          *
819          *      Either PORT_DOWN_TIMER OR LINK_DOWN_TIMER Expired.
820          */
821         if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
822             atomic_read(&ha2->loop_state) == LOOP_DEAD) {
823                 /*
824                  * Add the command to the done-queue for later failover
825                  * processing.
826                  */
827                 cmd->result = DID_NO_CONNECT << 16;
828                 if (atomic_read(&ha2->loop_state) == LOOP_DOWN) 
829                         sp->err_id = SRB_ERR_LOOP;
830                 else
831                         sp->err_id = SRB_ERR_PORT;
832
833                 add_to_done_queue(ha, sp);
834                 qla2x00_done(ha);
835
836                 spin_lock_irq(ha->host->host_lock);
837                 return (0);
838         }
839         if (tq && test_bit(TQF_SUSPENDED, &tq->flags) &&
840             (sp->flags & SRB_TAPE) == 0) {
841                 /* If target suspended put incoming I/O in retry_q. */
842                 qla2x00_extend_timeout(sp->cmd, 10);
843                 add_to_scsi_retry_queue(ha, sp);
844         } else
845                 was_empty = add_to_pending_queue(ha, sp);
846
847         if ((IS_QLA2100(ha) || IS_QLA2200(ha)) && ha->flags.online) {
848                 if (ha->response_ring_ptr->signature != RESPONSE_PROCESSED) {
849                         unsigned long flags;
850
851                         spin_lock_irqsave(&ha->hardware_lock, flags);   
852                         qla2x00_process_response_queue(ha);
853                         spin_unlock_irqrestore(&ha->hardware_lock, flags);
854                 }
855         }
856
857         /* We submit to the hardware if:
858          *
859          *      1) we're on the cpu the irq's arrive on or
860          *      2) there are very few io's outstanding.
861          *
862          * In all other cases we'll let an irq pick up our IO and submit it
863          * to the controller to improve affinity.
864          */
865         if (smp_processor_id() == ha->last_irq_cpu || was_empty)
866                 qla2x00_next(ha);
867
868         spin_lock_irq(ha->host->host_lock);
869
870         return (0);
871 }
872
873 /*
874  * qla2x00_eh_wait_on_command
875  *    Waits for the command to be returned by the Firmware for some
876  *    max time.
877  *
878  * Input:
879  *    ha = actual ha whose done queue will contain the command
880  *            returned by firmware.
881  *    cmd = Scsi Command to wait on.
882  *    flag = Abort/Reset(Bus or Device Reset)
883  *
884  * Return:
885  *    Not Found : 0
886  *    Found : 1
887  */
888 static int
889 qla2x00_eh_wait_on_command(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
890 {
891 #define ABORT_POLLING_PERIOD    HZ
892 #define ABORT_WAIT_TIME         ((10 * HZ) / (ABORT_POLLING_PERIOD))
893
894         int             found = 0;
895         int             done = 0;
896         srb_t           *rp = NULL;
897         struct list_head *list, *temp;
898         u_long          max_wait_time = ABORT_WAIT_TIME;
899
900         do {
901                 /* Check on done queue */
902                 spin_lock(&ha->list_lock);
903                 list_for_each_safe(list, temp, &ha->done_queue) {
904                         rp = list_entry(list, srb_t, list);
905
906                         /*
907                          * Found command. Just exit and wait for the cmd sent
908                          * to OS.
909                         */
910                         if (cmd == rp->cmd) {
911                                 found++;
912                                 DEBUG3(printk("%s: found in done queue.\n",
913                                     __func__);)
914                                 break;
915                         }
916                 }
917                 spin_unlock(&ha->list_lock);
918
919                 /* Complete the cmd right away. */
920                 if (found) { 
921                         qla2x00_delete_from_done_queue(ha, rp);
922                         sp_put(ha, rp);
923                         done++;
924                         break;
925                 }
926
927                 spin_unlock_irq(ha->host->host_lock);
928
929                 set_current_state(TASK_UNINTERRUPTIBLE);
930                 schedule_timeout(ABORT_POLLING_PERIOD);
931
932                 spin_lock_irq(ha->host->host_lock);
933
934         } while ((max_wait_time--));
935
936         if (done)
937                 DEBUG2(printk(KERN_INFO "%s: found cmd=%p.\n", __func__, cmd));
938
939         return (done);
940 }
941
942 /*
943  * qla2x00_wait_for_hba_online
944  *    Wait till the HBA is online after going through 
945  *    <= MAX_RETRIES_OF_ISP_ABORT  or
946  *    finally HBA is disabled ie marked offline
947  *
948  * Input:
949  *     ha - pointer to host adapter structure
950  * 
951  * Note:    
952  *    Does context switching-Release SPIN_LOCK
953  *    (if any) before calling this routine.
954  *
955  * Return:
956  *    Success (Adapter is online) : 0
957  *    Failed  (Adapter is offline/disabled) : 1
958  */
959 int 
960 qla2x00_wait_for_hba_online(scsi_qla_host_t *ha)
961 {
962         int      return_status;
963         unsigned long wait_online;
964
965         wait_online = jiffies + (MAX_LOOP_TIMEOUT * HZ); 
966         while (((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) ||
967             test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
968             test_bit(ISP_ABORT_RETRY, &ha->dpc_flags) ||
969             ha->dpc_active) && time_before(jiffies, wait_online)) {
970
971                 msleep(1000);
972         }
973         if (ha->flags.online) 
974                 return_status = QLA_SUCCESS; 
975         else
976                 return_status = QLA_FUNCTION_FAILED;
977
978         DEBUG2(printk("%s return_status=%d\n",__func__,return_status));
979
980         return (return_status);
981 }
982
983 /*
984  * qla2x00_wait_for_loop_ready
985  *    Wait for MAX_LOOP_TIMEOUT(5 min) value for loop
986  *    to be in LOOP_READY state.         
987  * Input:
988  *     ha - pointer to host adapter structure
989  * 
990  * Note:    
991  *    Does context switching-Release SPIN_LOCK
992  *    (if any) before calling this routine.
993  *    
994  *
995  * Return:
996  *    Success (LOOP_READY) : 0
997  *    Failed  (LOOP_NOT_READY) : 1
998  */
999 static inline int 
1000 qla2x00_wait_for_loop_ready(scsi_qla_host_t *ha)
1001 {
1002         int      return_status = QLA_SUCCESS;
1003         unsigned long loop_timeout ;
1004
1005         /* wait for 5 min at the max for loop to be ready */
1006         loop_timeout = jiffies + (MAX_LOOP_TIMEOUT * HZ); 
1007
1008         while ((!atomic_read(&ha->loop_down_timer) &&
1009             atomic_read(&ha->loop_state) == LOOP_DOWN) ||
1010             test_bit(CFG_ACTIVE, &ha->cfg_flags) ||
1011             atomic_read(&ha->loop_state) != LOOP_READY) {
1012                 msleep(1000);
1013                 if (time_after_eq(jiffies, loop_timeout)) {
1014                         return_status = QLA_FUNCTION_FAILED;
1015                         break;
1016                 }
1017         }
1018         return (return_status); 
1019 }
1020
1021 /**************************************************************************
1022 * qla2xxx_eh_abort
1023 *
1024 * Description:
1025 *    The abort function will abort the specified command.
1026 *
1027 * Input:
1028 *    cmd = Linux SCSI command packet to be aborted.
1029 *
1030 * Returns:
1031 *    Either SUCCESS or FAILED.
1032 *
1033 * Note:
1034 **************************************************************************/
1035 int
1036 qla2xxx_eh_abort(struct scsi_cmnd *cmd)
1037 {
1038         int             i;
1039         int             return_status = FAILED;
1040         os_lun_t        *q;
1041         scsi_qla_host_t *ha;
1042         scsi_qla_host_t *vis_ha;
1043         srb_t           *sp;
1044         srb_t           *rp;
1045         struct list_head *list, *temp;
1046         struct Scsi_Host *host;
1047         uint8_t         found = 0;
1048         unsigned int    b, t, l;
1049
1050         /* Get the SCSI request ptr */
1051         sp = (srb_t *) CMD_SP(cmd);
1052
1053         /*
1054          * If sp is NULL, command is already returned.
1055          * sp is NULLED just before we call back scsi_done
1056          *
1057          */
1058         if ((sp == NULL)) {
1059                 /* no action - we don't have command */
1060                 qla_printk(KERN_INFO, to_qla_host(cmd->device->host),
1061                     "qla2xxx_eh_abort: cmd already done sp=%p\n", sp);
1062                 DEBUG(printk("qla2xxx_eh_abort: cmd already done sp=%p\n", sp);)
1063                 return SUCCESS;
1064         }
1065         if (sp) {
1066                 DEBUG(printk("qla2xxx_eh_abort: refcount %i \n",
1067                     atomic_read(&sp->ref_count));)
1068         }
1069
1070         vis_ha = (scsi_qla_host_t *) cmd->device->host->hostdata;
1071         ha = (scsi_qla_host_t *)cmd->device->host->hostdata;
1072
1073         host = ha->host;
1074
1075         /* Generate LU queue on bus, target, LUN */
1076         b = cmd->device->channel;
1077         t = cmd->device->id;
1078         l = cmd->device->lun;
1079         q = GET_LU_Q(vis_ha, t, l);
1080
1081         qla_printk(KERN_INFO, ha, 
1082             "%s scsi(%ld:%d:%d:%d): cmd_timeout_in_sec=0x%x.\n", __func__,
1083             ha->host_no, (int)b, (int)t, (int)l,
1084             cmd->timeout_per_command / HZ);
1085
1086         /*
1087          * if no LUN queue then something is very wrong!!!
1088          */
1089         if (q == NULL) {
1090                 qla_printk(KERN_WARNING, ha,
1091                         "qla2x00: (%x:%x:%x) No LUN queue.\n", b, t, l);
1092
1093                 /* no action - we don't have command */
1094                 return FAILED;
1095         }
1096
1097         DEBUG2(printk("scsi(%ld): ABORTing cmd=%p sp=%p jiffies = 0x%lx, "
1098             "timeout=%x, dpc_flags=%lx, vis_ha->dpc_flags=%lx q->flag=%lx\n",
1099             ha->host_no, cmd, sp, jiffies, cmd->timeout_per_command / HZ,
1100             ha->dpc_flags, vis_ha->dpc_flags, q->q_flag));
1101         DEBUG2(qla2x00_print_scsi_cmd(cmd));
1102
1103         spin_unlock_irq(ha->host->host_lock);
1104         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
1105                 DEBUG2(printk("%s failed:board disabled\n", __func__);)
1106                 spin_lock_irq(ha->host->host_lock);
1107                 return FAILED;
1108         }
1109         spin_lock_irq(ha->host->host_lock);
1110
1111         /* Search done queue */
1112         spin_lock(&ha->list_lock);
1113         list_for_each_safe(list, temp, &ha->done_queue) {
1114                 rp = list_entry(list, srb_t, list);
1115
1116                 if (cmd != rp->cmd)
1117                         continue;
1118
1119                 /*
1120                  * Found command.Remove it from done list.
1121                  * And proceed to post completion to scsi mid layer.
1122                  */
1123                 return_status = SUCCESS;
1124                 found++;
1125                 qla2x00_delete_from_done_queue(ha, sp);
1126
1127                 break;
1128         } /* list_for_each_safe() */
1129         spin_unlock(&ha->list_lock);
1130
1131         /*
1132          * Return immediately if the aborted command was already in the done
1133          * queue
1134          */
1135         if (found) {
1136                 qla_printk(KERN_INFO, ha,
1137                     "qla2xxx_eh_abort: Returning completed command=%p sp=%p\n",
1138                     cmd, sp);
1139                 sp_put(ha, sp);
1140                 return (return_status);
1141         }
1142         
1143
1144         /*
1145          * See if this command is in the retry queue
1146          */
1147         DEBUG3(printk("qla2xxx_eh_abort: searching sp %p in retry "
1148                     "queue.\n", sp);)
1149
1150         spin_lock(&ha->list_lock);
1151         list_for_each_safe(list, temp, &ha->retry_queue) {
1152                 rp = list_entry(list, srb_t, list);
1153
1154                 if (cmd != rp->cmd)
1155                         continue;
1156
1157
1158                 DEBUG2(printk("qla2xxx_eh_abort: found "
1159                     "in retry queue. SP=%p\n", sp);)
1160
1161                 __del_from_retry_queue(ha, rp);
1162                 cmd->result = DID_ABORT << 16;
1163                 __add_to_done_queue(ha, rp);
1164
1165                 return_status = SUCCESS;
1166                 found++;
1167
1168                 break;
1169
1170         } 
1171         spin_unlock(&ha->list_lock);
1172
1173
1174         /*
1175          * Our SP pointer points at the command we want to remove from the
1176          * pending queue providing we haven't already sent it to the adapter.
1177          */
1178         if (!found) {
1179                 DEBUG3(printk("qla2xxx_eh_abort: searching sp %p "
1180                     "in pending queue.\n", sp);)
1181
1182                 spin_lock(&vis_ha->list_lock);
1183                 list_for_each_safe(list, temp, &vis_ha->pending_queue) {
1184                         rp = list_entry(list, srb_t, list);
1185
1186                         if (rp->cmd != cmd)
1187                                 continue;
1188
1189                         /* Remove srb from LUN queue. */
1190                         rp->flags |=  SRB_ABORTED;
1191
1192                         DEBUG2(printk("qla2xxx_eh_abort: Cmd in pending queue."
1193                             " serial_number %ld.\n",
1194                             sp->cmd->serial_number);)
1195
1196                         __del_from_pending_queue(vis_ha, rp);
1197                         cmd->result = DID_ABORT << 16;
1198
1199                         __add_to_done_queue(vis_ha, rp);
1200
1201                         return_status = SUCCESS;
1202
1203                         found++;
1204                         break;
1205                 } /* list_for_each_safe() */
1206                 spin_unlock(&vis_ha->list_lock);
1207         } /*End of if !found */
1208
1209         if (!found) {  /* find the command in our active list */
1210                 DEBUG3(printk("qla2xxx_eh_abort: searching sp %p "
1211                     "in outstanding queue.\n", sp);)
1212
1213                 spin_lock(&ha->hardware_lock);
1214                 for (i = 1; i < MAX_OUTSTANDING_COMMANDS; i++) {
1215                         sp = ha->outstanding_cmds[i];
1216
1217                         if (sp == NULL)
1218                                 continue;
1219
1220                         if (sp->cmd != cmd)
1221                                 continue;
1222
1223                         DEBUG2(printk("qla2xxx_eh_abort(%ld): aborting sp %p "
1224                             "from RISC. pid=%ld sp->state=%x q->q_flag=%lx\n",
1225                             ha->host_no, sp, sp->cmd->serial_number,
1226                             sp->state, q->q_flag);)
1227                         DEBUG(qla2x00_print_scsi_cmd(cmd);)
1228
1229                         /* Get a reference to the sp and drop the lock.*/
1230                         sp_get(ha, sp);
1231
1232                         spin_unlock(&ha->hardware_lock);
1233                         spin_unlock_irq(ha->host->host_lock);
1234
1235                         if (qla2x00_abort_command(ha, sp)) {
1236                                 DEBUG2(printk("qla2xxx_eh_abort: abort_command "
1237                                     "mbx failed.\n");)
1238                                 return_status = FAILED;
1239                         } else {
1240                                 DEBUG3(printk("qla2xxx_eh_abort: abort_command "
1241                                     " mbx success.\n");)
1242                                 return_status = SUCCESS;
1243                         }
1244
1245                         sp_put(ha,sp);
1246
1247                         spin_lock_irq(ha->host->host_lock);
1248                         spin_lock(&ha->hardware_lock);
1249
1250                         /*
1251                          * Regardless of mailbox command status, go check on
1252                          * done queue just in case the sp is already done.
1253                          */
1254                         break;
1255
1256                 }/*End of for loop */
1257                 spin_unlock(&ha->hardware_lock);
1258
1259         } /*End of if !found */
1260
1261         /* Waiting for our command in done_queue to be returned to OS.*/
1262         if (qla2x00_eh_wait_on_command(ha, cmd) != 0) {
1263                 DEBUG2(printk("qla2xxx_eh_abort: cmd returned back to OS.\n");)
1264                 return_status = SUCCESS;
1265         }
1266
1267         if (return_status == FAILED) {
1268                 qla_printk(KERN_INFO, ha, 
1269                         "qla2xxx_eh_abort Exiting: status=Failed\n");
1270                 return FAILED;
1271         }
1272
1273         DEBUG2(printk("qla2xxx_eh_abort: Exiting. return_status=0x%x.\n",
1274             return_status));
1275
1276         return return_status;
1277 }
1278
1279 /**************************************************************************
1280 * qla2x00_eh_wait_for_pending_target_commands
1281 *
1282 * Description:
1283 *    Waits for all the commands to come back from the specified target.
1284 *
1285 * Input:
1286 *    ha - pointer to scsi_qla_host structure.
1287 *    t  - target        
1288 * Returns:
1289 *    Either SUCCESS or FAILED.
1290 *
1291 * Note:
1292 **************************************************************************/
1293 int
1294 qla2x00_eh_wait_for_pending_target_commands(scsi_qla_host_t *ha, unsigned int t)
1295 {
1296         int     cnt;
1297         int     status;
1298         srb_t           *sp;
1299         struct scsi_cmnd *cmd;
1300
1301         status = 0;
1302
1303         /*
1304          * Waiting for all commands for the designated target in the active
1305          * array
1306          */
1307         for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
1308                 spin_lock(&ha->hardware_lock);
1309                 sp = ha->outstanding_cmds[cnt];
1310                 if (sp) {
1311                         cmd = sp->cmd;
1312                         spin_unlock(&ha->hardware_lock);
1313                         if (cmd->device->id == t) {
1314                                 if (!qla2x00_eh_wait_on_command(ha, cmd)) {
1315                                         status = 1;
1316                                         break;
1317                                 }
1318                         }
1319                 }
1320                 else {
1321                         spin_unlock(&ha->hardware_lock);
1322                 }
1323         }
1324         return (status);
1325 }
1326
1327
1328 /**************************************************************************
1329 * qla2xxx_eh_device_reset
1330 *
1331 * Description:
1332 *    The device reset function will reset the target and abort any
1333 *    executing commands.
1334 *
1335 *    NOTE: The use of SP is undefined within this context.  Do *NOT*
1336 *          attempt to use this value, even if you determine it is 
1337 *          non-null.
1338 *
1339 * Input:
1340 *    cmd = Linux SCSI command packet of the command that cause the
1341 *          bus device reset.
1342 *
1343 * Returns:
1344 *    SUCCESS/FAILURE (defined as macro in scsi.h).
1345 *
1346 **************************************************************************/
1347 int
1348 qla2xxx_eh_device_reset(struct scsi_cmnd *cmd)
1349 {
1350         int             return_status;
1351         unsigned int    b, t, l;
1352         scsi_qla_host_t *ha;
1353         os_tgt_t        *tq;
1354         os_lun_t        *lq;
1355         fc_port_t       *fcport_to_reset;
1356         srb_t           *rp;
1357         struct list_head *list, *temp;
1358
1359         return_status = FAILED;
1360         if (cmd == NULL) {
1361                 printk(KERN_INFO
1362                     "%s(): **** SCSI mid-layer passing in NULL cmd\n",
1363                     __func__);
1364
1365                 return (return_status);
1366         }
1367
1368         b = cmd->device->channel;
1369         t = cmd->device->id;
1370         l = cmd->device->lun;
1371         ha = (scsi_qla_host_t *)cmd->device->host->hostdata;
1372
1373         tq = TGT_Q(ha, t);
1374         if (tq == NULL) {
1375                 qla_printk(KERN_INFO, ha,
1376                     "%s(): **** CMD derives a NULL TGT_Q\n", __func__);
1377
1378                 return (return_status);
1379         }
1380         lq = (os_lun_t *)LUN_Q(ha, t, l);
1381         if (lq == NULL) {
1382                 printk(KERN_INFO
1383                     "%s(): **** CMD derives a NULL LUN_Q\n", __func__);
1384
1385                 return (return_status);
1386         }
1387         fcport_to_reset = lq->fclun->fcport;
1388
1389         /* If we are coming in from the back-door, stall I/O until complete. */
1390         if (!cmd->device->host->eh_active)
1391                 set_bit(TQF_SUSPENDED, &tq->flags);
1392
1393         qla_printk(KERN_INFO, ha,
1394             "scsi(%ld:%d:%d:%d): DEVICE RESET ISSUED.\n", ha->host_no, b, t, l);
1395
1396         DEBUG2(printk(KERN_INFO
1397             "scsi(%ld): DEVICE_RESET cmd=%p jiffies = 0x%lx, timeout=%x, "
1398             "dpc_flags=%lx, status=%x allowed=%d cmd.state=%x\n",
1399             ha->host_no, cmd, jiffies, cmd->timeout_per_command / HZ,
1400             ha->dpc_flags, cmd->result, cmd->allowed, cmd->state));
1401
1402         /* Clear commands from the retry queue. */
1403         spin_lock(&ha->list_lock);
1404         list_for_each_safe(list, temp, &ha->retry_queue) {
1405                 rp = list_entry(list, srb_t, list);
1406  
1407                 if (t != rp->cmd->device->id) 
1408                         continue;
1409  
1410                 DEBUG2(printk(KERN_INFO
1411                     "qla2xxx_eh_reset: found in retry queue. SP=%p\n", rp));
1412  
1413                 __del_from_retry_queue(ha, rp);
1414                 rp->cmd->result = DID_RESET << 16;
1415                 __add_to_done_queue(ha, rp);
1416         }
1417         spin_unlock(&ha->list_lock);
1418
1419         spin_unlock_irq(ha->host->host_lock);
1420
1421         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
1422                 DEBUG2(printk(KERN_INFO
1423                     "%s failed:board disabled\n",__func__));
1424
1425                 spin_lock_irq(ha->host->host_lock);
1426                 goto eh_dev_reset_done;
1427         }
1428
1429         if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
1430                 if (qla2x00_device_reset(ha, fcport_to_reset) == 0) {
1431                         return_status = SUCCESS;
1432                 }
1433
1434 #if defined(LOGOUT_AFTER_DEVICE_RESET)
1435                 if (return_status == SUCCESS) {
1436                         if (fcport_to_reset->flags & FC_FABRIC_DEVICE) {
1437                                 qla2x00_fabric_logout(ha,
1438                                     fcport_to_reset->loop_id);
1439                                 qla2x00_mark_device_lost(ha, fcport_to_reset);
1440                         }
1441                 }
1442 #endif
1443         } else {
1444                 DEBUG2(printk(KERN_INFO
1445                     "%s failed: loop not ready\n",__func__);)
1446         }
1447
1448         spin_lock_irq(ha->host->host_lock);
1449
1450         if (return_status == FAILED) {
1451                 DEBUG3(printk("%s(%ld): device reset failed\n",
1452                     __func__, ha->host_no));
1453                 qla_printk(KERN_INFO, ha, "%s: device reset failed\n",
1454                     __func__);
1455
1456                 goto eh_dev_reset_done;
1457         }
1458
1459         /*
1460          * If we are coming down the EH path, wait for all commands to
1461          * complete for the device.
1462          */
1463         if (cmd->device->host->eh_active) {
1464                 if (qla2x00_eh_wait_for_pending_target_commands(ha, t))
1465                         return_status = FAILED;
1466
1467                 if (return_status == FAILED) {
1468                         DEBUG3(printk("%s(%ld): failed while waiting for "
1469                             "commands\n", __func__, ha->host_no));
1470                         qla_printk(KERN_INFO, ha,
1471                             "%s: failed while waiting for commands\n",
1472                             __func__); 
1473
1474                         goto eh_dev_reset_done;
1475                 }
1476         }
1477
1478         qla_printk(KERN_INFO, ha,
1479             "scsi(%ld:%d:%d:%d): DEVICE RESET SUCCEEDED.\n",
1480             ha->host_no, b, t, l);
1481
1482 eh_dev_reset_done:
1483
1484         if (!cmd->device->host->eh_active)
1485                 clear_bit(TQF_SUSPENDED, &tq->flags);
1486
1487         return (return_status);
1488 }
1489
1490 /**************************************************************************
1491 * qla2x00_eh_wait_for_pending_commands
1492 *
1493 * Description:
1494 *    Waits for all the commands to come back from the specified host.
1495 *
1496 * Input:
1497 *    ha - pointer to scsi_qla_host structure.
1498 *
1499 * Returns:
1500 *    1 : SUCCESS
1501 *    0 : FAILED
1502 *
1503 * Note:
1504 **************************************************************************/
1505 int
1506 qla2x00_eh_wait_for_pending_commands(scsi_qla_host_t *ha)
1507 {
1508         int     cnt;
1509         int     status;
1510         srb_t           *sp;
1511         struct scsi_cmnd *cmd;
1512
1513         status = 1;
1514
1515         /*
1516          * Waiting for all commands for the designated target in the active
1517          * array
1518          */
1519         for (cnt = 1; cnt < MAX_OUTSTANDING_COMMANDS; cnt++) {
1520                 spin_lock(&ha->hardware_lock);
1521                 sp = ha->outstanding_cmds[cnt];
1522                 if (sp) {
1523                         cmd = sp->cmd;
1524                         spin_unlock(&ha->hardware_lock);
1525                         status = qla2x00_eh_wait_on_command(ha, cmd);
1526                         if (status == 0)
1527                                 break;
1528                 }
1529                 else {
1530                         spin_unlock(&ha->hardware_lock);
1531                 }
1532         }
1533         return (status);
1534 }
1535
1536
1537 /**************************************************************************
1538 * qla2xxx_eh_bus_reset
1539 *
1540 * Description:
1541 *    The bus reset function will reset the bus and abort any executing
1542 *    commands.
1543 *
1544 * Input:
1545 *    cmd = Linux SCSI command packet of the command that cause the
1546 *          bus reset.
1547 *
1548 * Returns:
1549 *    SUCCESS/FAILURE (defined as macro in scsi.h).
1550 *
1551 **************************************************************************/
1552 int
1553 qla2xxx_eh_bus_reset(struct scsi_cmnd *cmd)
1554 {
1555         scsi_qla_host_t *ha;
1556         srb_t *sp;
1557         int rval = FAILED;
1558
1559         ha = (scsi_qla_host_t *) cmd->device->host->hostdata;
1560         sp = (srb_t *) CMD_SP(cmd);
1561
1562         qla_printk(KERN_INFO, ha,
1563             "scsi(%ld:%d:%d:%d): LOOP RESET ISSUED.\n", ha->host_no,
1564             cmd->device->channel, cmd->device->id, cmd->device->lun);
1565
1566         spin_unlock_irq(ha->host->host_lock);
1567
1568         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS) {
1569                 DEBUG2(printk("%s failed:board disabled\n",__func__));
1570                 spin_lock_irq(ha->host->host_lock);
1571                 return FAILED;
1572         }
1573
1574         if (qla2x00_wait_for_loop_ready(ha) == QLA_SUCCESS) {
1575                 if (qla2x00_loop_reset(ha) == QLA_SUCCESS) 
1576                         rval = SUCCESS;
1577         }
1578
1579         spin_lock_irq(ha->host->host_lock);
1580         if (rval == FAILED)
1581                 goto out;
1582
1583         /* Waiting for our command in done_queue to be returned to OS.*/
1584         if (cmd->device->host->eh_active)
1585                 if (!qla2x00_eh_wait_for_pending_commands(ha))
1586                         rval = FAILED;
1587
1588  out:
1589         qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
1590                         (rval == FAILED) ? "failed" : "succeded");
1591
1592         return rval;
1593 }
1594
1595 /**************************************************************************
1596 * qla2xxx_eh_host_reset
1597 *
1598 * Description:
1599 *    The reset function will reset the Adapter.
1600 *
1601 * Input:
1602 *      cmd = Linux SCSI command packet of the command that cause the
1603 *            adapter reset.
1604 *
1605 * Returns:
1606 *      Either SUCCESS or FAILED.
1607 *
1608 * Note:
1609 **************************************************************************/
1610 int
1611 qla2xxx_eh_host_reset(struct scsi_cmnd *cmd)
1612 {
1613         scsi_qla_host_t *ha = (scsi_qla_host_t *)cmd->device->host->hostdata;
1614         int             rval = SUCCESS;
1615
1616         /* Display which one we're actually resetting for debug. */
1617         DEBUG(printk("qla2xxx_eh_host_reset:Resetting scsi(%ld).\n",
1618             ha->host_no));
1619
1620         /*
1621          *  Now issue reset.
1622          */
1623         qla_printk(KERN_INFO, ha,
1624             "scsi(%ld:%d:%d:%d): ADAPTER RESET issued.\n", ha->host_no,
1625             cmd->device->channel, cmd->device->id, cmd->device->lun);
1626
1627         spin_unlock_irq(ha->host->host_lock);
1628
1629         if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
1630                 goto board_disabled;
1631
1632         /*
1633          * Fixme-may be dpc thread is active and processing
1634          * loop_resync,so wait a while for it to 
1635          * be completed and then issue big hammer.Otherwise
1636          * it may cause I/O failure as big hammer marks the
1637          * devices as lost kicking of the port_down_timer
1638          * while dpc is stuck for the mailbox to complete.
1639          */
1640         /* Blocking call-Does context switching if loop is Not Ready */
1641         qla2x00_wait_for_loop_ready(ha);
1642         set_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
1643         if (qla2x00_abort_isp(ha)) {
1644                 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
1645                 /* failed. schedule dpc to try */
1646                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
1647
1648                 if (qla2x00_wait_for_hba_online(ha) != QLA_SUCCESS)
1649                         goto board_disabled;
1650         } 
1651
1652         clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
1653
1654         spin_lock_irq(ha->host->host_lock);
1655         if (rval == FAILED)
1656                 goto out;
1657
1658         /* Waiting for our command in done_queue to be returned to OS.*/
1659         if (!qla2x00_eh_wait_for_pending_commands(ha))
1660                 rval = FAILED;
1661
1662  out:
1663         qla_printk(KERN_INFO, ha, "%s: reset %s\n", __func__,
1664                         (rval == FAILED) ? "failed" : "succeded");
1665
1666         return rval;
1667
1668  board_disabled:
1669         spin_lock_irq(ha->host->host_lock);
1670
1671         qla_printk(KERN_INFO, ha, "%s: failed:board disabled\n", __func__);
1672         return FAILED;
1673 }
1674
1675
1676 /*
1677 * qla2x00_loop_reset
1678 *      Issue loop reset.
1679 *
1680 * Input:
1681 *      ha = adapter block pointer.
1682 *
1683 * Returns:
1684 *      0 = success
1685 */
1686 static int
1687 qla2x00_loop_reset(scsi_qla_host_t *ha)
1688 {
1689         int status = QLA_SUCCESS;
1690         uint16_t t;
1691         os_tgt_t        *tq;
1692
1693         if (ha->flags.enable_lip_reset) {
1694                 status = qla2x00_lip_reset(ha);
1695         }
1696
1697         if (status == QLA_SUCCESS && ha->flags.enable_target_reset) {
1698                 for (t = 0; t < MAX_FIBRE_DEVICES; t++) {
1699                         if ((tq = TGT_Q(ha, t)) == NULL)
1700                                 continue;
1701
1702                         if (tq->fcport == NULL)
1703                                 continue;
1704
1705                         status = qla2x00_target_reset(ha, 0, t);
1706                         if (status != QLA_SUCCESS) {
1707                                 break;
1708                         }
1709                 }
1710         }
1711
1712         if (status == QLA_SUCCESS &&
1713                 ((!ha->flags.enable_target_reset && 
1714                   !ha->flags.enable_lip_reset) ||
1715                 ha->flags.enable_lip_full_login)) {
1716
1717                 status = qla2x00_full_login_lip(ha);
1718         }
1719
1720         /* Issue marker command only when we are going to start the I/O */
1721         ha->marker_needed = 1;
1722
1723         if (status) {
1724                 /* Empty */
1725                 DEBUG2_3(printk("%s(%ld): **** FAILED ****\n",
1726                                 __func__,
1727                                 ha->host_no);)
1728         } else {
1729                 /* Empty */
1730                 DEBUG3(printk("%s(%ld): exiting normally.\n",
1731                                 __func__,
1732                                 ha->host_no);)
1733         }
1734
1735         return(status);
1736 }
1737
1738 /*
1739  * qla2x00_device_reset
1740  *      Issue bus device reset message to the target.
1741  *
1742  * Input:
1743  *      ha = adapter block pointer.
1744  *      t = SCSI ID.
1745  *      TARGET_QUEUE_LOCK must be released.
1746  *      ADAPTER_STATE_LOCK must be released.
1747  *
1748  * Context:
1749  *      Kernel context.
1750  */
1751 static int
1752 qla2x00_device_reset(scsi_qla_host_t *ha, fc_port_t *reset_fcport)
1753 {
1754         /* Abort Target command will clear Reservation */
1755         return qla2x00_abort_target(reset_fcport);
1756 }
1757
1758 /**************************************************************************
1759 * qla2xxx_slave_configure
1760 *
1761 * Description:
1762 **************************************************************************/
1763 int
1764 qla2xxx_slave_configure(struct scsi_device *sdev)
1765 {
1766         scsi_qla_host_t *ha = to_qla_host(sdev->host);
1767         int queue_depth;
1768
1769         if (IS_QLA2100(ha) || IS_QLA2200(ha))
1770                 queue_depth = 16;
1771         else
1772                 queue_depth = 32;
1773
1774         if (sdev->tagged_supported) {
1775                 if (ql2xmaxqdepth != 0 && ql2xmaxqdepth <= 0xffffU)
1776                         queue_depth = ql2xmaxqdepth;
1777
1778                 ql2xmaxqdepth = queue_depth;
1779
1780                 scsi_activate_tcq(sdev, queue_depth);
1781
1782                 qla_printk(KERN_INFO, ha,
1783                     "scsi(%d:%d:%d:%d): Enabled tagged queuing, queue "
1784                     "depth %d.\n",
1785                     sdev->host->host_no, sdev->channel, sdev->id, sdev->lun,
1786                     sdev->queue_depth);
1787         } else {
1788                  scsi_adjust_queue_depth(sdev, 0 /* TCQ off */,
1789                      sdev->host->hostt->cmd_per_lun /* 3 */);
1790         }
1791
1792         return (0);
1793 }
1794
1795 /**
1796  * qla2x00_config_dma_addressing() - Configure OS DMA addressing method.
1797  * @ha: HA context
1798  *
1799  * At exit, the @ha's flags.enable_64bit_addressing set to indicated
1800  * supported addressing method.
1801  */
1802 static void
1803 qla2x00_config_dma_addressing(scsi_qla_host_t *ha)
1804 {
1805         /* Assume 32bit DMA address */
1806         ha->flags.enable_64bit_addressing = 0;
1807         ha->calc_request_entries = qla2x00_calc_iocbs_32;
1808         ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_32;
1809
1810         /*
1811          * Given the two variants pci_set_dma_mask(), allow the compiler to
1812          * assist in setting the proper dma mask.
1813          */
1814         if (sizeof(dma_addr_t) > 4) {
1815                 if (pci_set_dma_mask(ha->pdev, DMA_64BIT_MASK) == 0) {
1816                         ha->flags.enable_64bit_addressing = 1;
1817                         ha->calc_request_entries = qla2x00_calc_iocbs_64;
1818                         ha->build_scsi_iocbs = qla2x00_build_scsi_iocbs_64;
1819
1820                         if (pci_set_consistent_dma_mask(ha->pdev,
1821                             DMA_64BIT_MASK)) {
1822                                 qla_printk(KERN_DEBUG, ha, 
1823                                     "Failed to set 64 bit PCI consistent mask; "
1824                                     "using 32 bit.\n");
1825                                 pci_set_consistent_dma_mask(ha->pdev,
1826                                     DMA_32BIT_MASK);
1827                         }
1828                 } else {
1829                         qla_printk(KERN_DEBUG, ha,
1830                             "Failed to set 64 bit PCI DMA mask, falling back "
1831                             "to 32 bit MASK.\n");
1832                         pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1833                 }
1834         } else {
1835                 pci_set_dma_mask(ha->pdev, DMA_32BIT_MASK);
1836         }
1837 }
1838
1839 static int
1840 qla2x00_iospace_config(scsi_qla_host_t *ha)
1841 {
1842         unsigned long   pio, pio_len, pio_flags;
1843         unsigned long   mmio, mmio_len, mmio_flags;
1844
1845         /* We only need PIO for Flash operations on ISP2312 v2 chips. */
1846         pio = pci_resource_start(ha->pdev, 0);
1847         pio_len = pci_resource_len(ha->pdev, 0);
1848         pio_flags = pci_resource_flags(ha->pdev, 0);
1849         if (pio_flags & IORESOURCE_IO) {
1850                 if (pio_len < MIN_IOBASE_LEN) {
1851                         qla_printk(KERN_WARNING, ha,
1852                             "Invalid PCI I/O region size (%s)...\n",
1853                             ha->pdev->slot_name);
1854                         pio = 0;
1855                 }
1856         } else {
1857                 qla_printk(KERN_WARNING, ha,
1858                     "region #0 not a PIO resource (%s)...\n",
1859                     ha->pdev->slot_name);
1860                 pio = 0;
1861         }
1862
1863         /* Use MMIO operations for all accesses. */
1864         mmio = pci_resource_start(ha->pdev, 1);
1865         mmio_len = pci_resource_len(ha->pdev, 1);
1866         mmio_flags = pci_resource_flags(ha->pdev, 1);
1867
1868         if (!(mmio_flags & IORESOURCE_MEM)) {
1869                 qla_printk(KERN_ERR, ha,
1870                     "region #0 not an MMIO resource (%s), aborting\n",
1871                     ha->pdev->slot_name);
1872                 goto iospace_error_exit;
1873         }
1874         if (mmio_len < MIN_IOBASE_LEN) {
1875                 qla_printk(KERN_ERR, ha,
1876                     "Invalid PCI mem region size (%s), aborting\n",
1877                     ha->pdev->slot_name);
1878                 goto iospace_error_exit;
1879         }
1880
1881         if (pci_request_regions(ha->pdev, ha->brd_info->drv_name)) {
1882                 qla_printk(KERN_WARNING, ha,
1883                     "Failed to reserve PIO/MMIO regions (%s)\n", 
1884                     ha->pdev->slot_name);
1885
1886                 goto iospace_error_exit;
1887         }
1888
1889         ha->pio_address = pio;
1890         ha->pio_length = pio_len;
1891         ha->iobase = ioremap(mmio, MIN_IOBASE_LEN);
1892         if (!ha->iobase) {
1893                 qla_printk(KERN_ERR, ha,
1894                     "cannot remap MMIO (%s), aborting\n", ha->pdev->slot_name);
1895
1896                 goto iospace_error_exit;
1897         }
1898
1899         return (0);
1900
1901 iospace_error_exit:
1902         return (-ENOMEM);
1903 }
1904
1905 /*
1906  * PCI driver interface
1907  */
1908 int qla2x00_probe_one(struct pci_dev *pdev, struct qla_board_info *brd_info)
1909 {
1910         int     ret;
1911         device_reg_t __iomem *reg;
1912         struct Scsi_Host *host;
1913         scsi_qla_host_t *ha;
1914         unsigned long   flags = 0;
1915         unsigned long   wait_switch = 0;
1916         char pci_info[20];
1917         char fw_str[30];
1918
1919         if (pci_enable_device(pdev))
1920                 return -1;
1921
1922         host = scsi_host_alloc(&qla2x00_driver_template,
1923             sizeof(scsi_qla_host_t));
1924         if (host == NULL) {
1925                 printk(KERN_WARNING
1926                     "qla2xxx: Couldn't allocate host from scsi layer!\n");
1927                 goto probe_disable_device;
1928         }
1929
1930         /* Clear our data area */
1931         ha = (scsi_qla_host_t *)host->hostdata;
1932         memset(ha, 0, sizeof(scsi_qla_host_t));
1933
1934         ha->pdev = pdev;
1935         ha->host = host;
1936         ha->host_no = host->host_no;
1937         ha->brd_info = brd_info;
1938         sprintf(ha->host_str, "%s_%ld", ha->brd_info->drv_name, ha->host_no);
1939
1940         /* Configure PCI I/O space */
1941         ret = qla2x00_iospace_config(ha);
1942         if (ret != 0) {
1943                 goto probe_failed;
1944         }
1945
1946         /* Sanitize the information from PCI BIOS. */
1947         host->irq = pdev->irq;
1948
1949         qla_printk(KERN_INFO, ha,
1950             "Found an %s, irq %d, iobase 0x%p\n", ha->brd_info->isp_name,
1951             host->irq, ha->iobase);
1952
1953         spin_lock_init(&ha->hardware_lock);
1954
1955         /* 4.23 Initialize /proc/scsi/qla2x00 counters */
1956         ha->actthreads = 0;
1957         ha->qthreads   = 0;
1958         ha->total_isr_cnt = 0;
1959         ha->total_isp_aborts = 0;
1960         ha->total_lip_cnt = 0;
1961         ha->total_dev_errs = 0;
1962         ha->total_ios = 0;
1963         ha->total_bytes = 0;
1964
1965         ha->prev_topology = 0;
1966         ha->ports = MAX_BUSES;
1967
1968         if (IS_QLA2100(ha)) {
1969                 ha->max_targets = MAX_TARGETS_2100;
1970                 ha->mbx_count = MAILBOX_REGISTER_COUNT_2100;
1971                 ha->request_q_length = REQUEST_ENTRY_CNT_2100;
1972                 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1973                 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1974                 host->sg_tablesize = 32;
1975         } else if (IS_QLA2200(ha)) {
1976                 ha->max_targets = MAX_TARGETS_2200;
1977                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1978                 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1979                 ha->response_q_length = RESPONSE_ENTRY_CNT_2100;
1980                 ha->last_loop_id = SNS_LAST_LOOP_ID_2100;
1981         } else /*if (IS_QLA2300(ha))*/ {
1982                 ha->max_targets = MAX_TARGETS_2200;
1983                 ha->mbx_count = MAILBOX_REGISTER_COUNT;
1984                 ha->request_q_length = REQUEST_ENTRY_CNT_2200;
1985                 ha->response_q_length = RESPONSE_ENTRY_CNT_2300;
1986                 ha->last_loop_id = SNS_LAST_LOOP_ID_2300;
1987         }
1988         host->can_queue = ha->request_q_length + 128;
1989
1990         /* load the F/W, read paramaters, and init the H/W */
1991         ha->instance = num_hosts;
1992
1993         init_MUTEX(&ha->mbx_cmd_sem);
1994         init_MUTEX_LOCKED(&ha->mbx_intr_sem);
1995
1996         INIT_LIST_HEAD(&ha->list);
1997         INIT_LIST_HEAD(&ha->fcports);
1998         INIT_LIST_HEAD(&ha->rscn_fcports);
1999         INIT_LIST_HEAD(&ha->done_queue);
2000         INIT_LIST_HEAD(&ha->retry_queue);
2001         INIT_LIST_HEAD(&ha->scsi_retry_queue);
2002         INIT_LIST_HEAD(&ha->pending_queue);
2003
2004         /*
2005          * These locks are used to prevent more than one CPU
2006          * from modifying the queue at the same time. The
2007          * higher level "host_lock" will reduce most
2008          * contention for these locks.
2009          */
2010         spin_lock_init(&ha->mbx_reg_lock);
2011         spin_lock_init(&ha->list_lock);
2012
2013         ha->dpc_pid = -1;
2014         init_completion(&ha->dpc_inited);
2015         init_completion(&ha->dpc_exited);
2016
2017         qla2x00_config_dma_addressing(ha);
2018         if (qla2x00_mem_alloc(ha)) {
2019                 qla_printk(KERN_WARNING, ha,
2020                     "[ERROR] Failed to allocate memory for adapter\n");
2021
2022                 goto probe_failed;
2023         }
2024
2025         if (qla2x00_initialize_adapter(ha) &&
2026             !(ha->device_flags & DFLG_NO_CABLE)) {
2027
2028                 qla_printk(KERN_WARNING, ha,
2029                     "Failed to initialize adapter\n");
2030
2031                 DEBUG2(printk("scsi(%ld): Failed to initialize adapter - "
2032                     "Adapter flags %x.\n",
2033                     ha->host_no, ha->device_flags));
2034
2035                 goto probe_failed;
2036         }
2037
2038         /*
2039          * Startup the kernel thread for this host adapter
2040          */
2041         ha->dpc_should_die = 0;
2042         ha->dpc_pid = kernel_thread(qla2x00_do_dpc, ha, 0);
2043         if (ha->dpc_pid < 0) {
2044                 qla_printk(KERN_WARNING, ha,
2045                     "Unable to start DPC thread!\n");
2046
2047                 goto probe_failed;
2048         }
2049         wait_for_completion(&ha->dpc_inited);
2050
2051         host->this_id = 255;
2052         host->cmd_per_lun = 3;
2053         host->max_cmd_len = MAX_CMDSZ;
2054         host->max_channel = ha->ports - 1;
2055         host->max_lun = ha->max_luns;
2056         BUG_ON(qla2xxx_transport_template == NULL);
2057         host->transportt = qla2xxx_transport_template;
2058         host->unique_id = ha->instance;
2059         host->max_id = ha->max_targets;
2060
2061         if (IS_QLA2100(ha) || IS_QLA2200(ha))
2062                 ret = request_irq(host->irq, qla2100_intr_handler,
2063                     SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
2064         else
2065                 ret = request_irq(host->irq, qla2300_intr_handler,
2066                     SA_INTERRUPT|SA_SHIRQ, ha->brd_info->drv_name, ha);
2067         if (ret != 0) {
2068                 qla_printk(KERN_WARNING, ha,
2069                     "Failed to reserve interrupt %d already in use.\n",
2070                     host->irq);
2071                 goto probe_failed;
2072         }
2073
2074         /* Initialized the timer */
2075         qla2x00_start_timer(ha, qla2x00_timer, WATCH_INTERVAL);
2076
2077         DEBUG2(printk("DEBUG: detect hba %ld at address = %p\n",
2078             ha->host_no, ha));
2079
2080         reg = ha->iobase;
2081
2082         /* Disable ISP interrupts. */
2083         qla2x00_disable_intrs(ha);
2084
2085         /* Ensure mailbox registers are free. */
2086         spin_lock_irqsave(&ha->hardware_lock, flags);
2087         WRT_REG_WORD(&reg->semaphore, 0);
2088         WRT_REG_WORD(&reg->hccr, HCCR_CLR_RISC_INT);
2089         WRT_REG_WORD(&reg->hccr, HCCR_CLR_HOST_INT);
2090
2091         /* Enable proper parity */
2092         if (!IS_QLA2100(ha) && !IS_QLA2200(ha)) {
2093                 if (IS_QLA2300(ha))
2094                         /* SRAM parity */
2095                         WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x1));
2096                 else
2097                         /* SRAM, Instruction RAM and GP RAM parity */
2098                         WRT_REG_WORD(&reg->hccr, (HCCR_ENABLE_PARITY + 0x7));
2099         }
2100         spin_unlock_irqrestore(&ha->hardware_lock, flags);
2101
2102         /* Enable chip interrupts. */
2103         qla2x00_enable_intrs(ha);
2104
2105         /* v2.19.5b6 */
2106         /*
2107          * Wait around max loop_reset_delay secs for the devices to come
2108          * on-line. We don't want Linux scanning before we are ready.
2109          *
2110          */
2111         for (wait_switch = jiffies + (ha->loop_reset_delay * HZ);
2112             time_before(jiffies,wait_switch) &&
2113              !(ha->device_flags & (DFLG_NO_CABLE | DFLG_FABRIC_DEVICES))
2114              && (ha->device_flags & SWITCH_FOUND) ;) {
2115
2116                 qla2x00_check_fabric_devices(ha);
2117
2118                 msleep(10);
2119         }
2120
2121         pci_set_drvdata(pdev, ha);
2122         ha->flags.init_done = 1;
2123         num_hosts++;
2124
2125         /* List the target we have found */
2126         if (displayConfig) {
2127                 qla2x00_display_fc_names(ha);
2128         }
2129
2130         if (scsi_add_host(host, &pdev->dev))
2131                 goto probe_failed;
2132
2133         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_fw_dump_attr);
2134         sysfs_create_bin_file(&host->shost_gendev.kobj, &sysfs_nvram_attr);
2135
2136         qla_printk(KERN_INFO, ha, "\n"
2137             " QLogic Fibre Channel HBA Driver: %s\n"
2138             "  QLogic %s - %s\n"
2139             "  %s: %s @ %s hdma%c, host#=%ld, fw=%s\n", qla2x00_version_str,
2140             ha->model_number, ha->model_desc ? ha->model_desc: "",
2141             ha->brd_info->isp_name, qla2x00_get_pci_info_str(ha, pci_info),
2142             pci_name(ha->pdev), ha->flags.enable_64bit_addressing ? '+': '-',
2143             ha->host_no, qla2x00_get_fw_version_str(ha, fw_str));
2144
2145         if (ql2xdoinitscan)
2146                 scsi_scan_host(host);
2147
2148         return 0;
2149
2150 probe_failed:
2151         qla2x00_free_device(ha);
2152
2153         scsi_host_put(host);
2154
2155 probe_disable_device:
2156         pci_disable_device(pdev);
2157
2158         return -1;
2159 }
2160 EXPORT_SYMBOL_GPL(qla2x00_probe_one);
2161
2162 void qla2x00_remove_one(struct pci_dev *pdev)
2163 {
2164         scsi_qla_host_t *ha;
2165
2166         ha = pci_get_drvdata(pdev);
2167
2168         sysfs_remove_bin_file(&ha->host->shost_gendev.kobj,
2169             &sysfs_fw_dump_attr);
2170         sysfs_remove_bin_file(&ha->host->shost_gendev.kobj, &sysfs_nvram_attr);
2171
2172         scsi_remove_host(ha->host);
2173
2174         qla2x00_free_device(ha);
2175
2176         scsi_host_put(ha->host);
2177
2178         pci_set_drvdata(pdev, NULL);
2179 }
2180 EXPORT_SYMBOL_GPL(qla2x00_remove_one);
2181
2182 static void
2183 qla2x00_free_device(scsi_qla_host_t *ha)
2184 {
2185         int ret;
2186
2187         /* Abort any outstanding IO descriptors. */
2188         if (!IS_QLA2100(ha) && !IS_QLA2200(ha))
2189                 qla2x00_cancel_io_descriptors(ha);
2190
2191         /* turn-off interrupts on the card */
2192         if (ha->interrupts_on)
2193                 qla2x00_disable_intrs(ha);
2194
2195         /* Disable timer */
2196         if (ha->timer_active)
2197                 qla2x00_stop_timer(ha);
2198
2199         /* Kill the kernel thread for this host */
2200         if (ha->dpc_pid >= 0) {
2201                 ha->dpc_should_die = 1;
2202                 wmb();
2203                 ret = kill_proc(ha->dpc_pid, SIGHUP, 1);
2204                 if (ret) {
2205                         qla_printk(KERN_ERR, ha,
2206                             "Unable to signal DPC thread -- (%d)\n", ret);
2207
2208                         /* TODO: SOMETHING MORE??? */
2209                 } else {
2210                         wait_for_completion(&ha->dpc_exited);
2211                 }
2212         }
2213
2214         qla2x00_mem_free(ha);
2215
2216
2217         ha->flags.online = 0;
2218
2219         /* Detach interrupts */
2220         if (ha->pdev->irq)
2221                 free_irq(ha->pdev->irq, ha);
2222
2223         /* release io space registers  */
2224         if (ha->iobase)
2225                 iounmap(ha->iobase);
2226         pci_release_regions(ha->pdev);
2227
2228         pci_disable_device(ha->pdev);
2229 }
2230
2231
2232 /*
2233  * The following support functions are adopted to handle
2234  * the re-entrant qla2x00_proc_info correctly.
2235  */
2236 static void
2237 copy_mem_info(struct info_str *info, char *data, int len)
2238 {
2239         if (info->pos + len > info->offset + info->length)
2240                 len = info->offset + info->length - info->pos;
2241
2242         if (info->pos + len < info->offset) {
2243                 info->pos += len;
2244                 return;
2245         }
2246  
2247         if (info->pos < info->offset) {
2248                 off_t partial;
2249  
2250                 partial = info->offset - info->pos;
2251                 data += partial;
2252                 info->pos += partial;
2253                 len  -= partial;
2254         }
2255  
2256         if (len > 0) {
2257                 memcpy(info->buffer, data, len);
2258                 info->pos += len;
2259                 info->buffer += len;
2260         }
2261 }
2262
2263 static int
2264 copy_info(struct info_str *info, char *fmt, ...)
2265 {
2266         va_list args;
2267         char buf[256];
2268         int len;
2269  
2270         va_start(args, fmt);
2271         len = vsprintf(buf, fmt, args);
2272         va_end(args);
2273  
2274         copy_mem_info(info, buf, len);
2275
2276         return (len);
2277 }
2278
2279 /*************************************************************************
2280 * qla2x00_proc_info
2281 *
2282 * Description:
2283 *   Return information to handle /proc support for the driver.
2284 *
2285 * inout : decides the direction of the dataflow and the meaning of the
2286 *         variables
2287 * buffer: If inout==0 data is being written to it else read from it
2288 *         (ptr to a page buffer)
2289 * *start: If inout==0 start of the valid data in the buffer
2290 * offset: If inout==0 starting offset from the beginning of all
2291 *         possible data to return.
2292 * length: If inout==0 max number of bytes to be written into the buffer
2293 *         else number of bytes in "buffer"
2294 * Returns:
2295 *         < 0:  error. errno value.
2296 *         >= 0: sizeof data returned.
2297 *************************************************************************/
2298 int
2299 qla2x00_proc_info(struct Scsi_Host *shost, char *buffer,
2300     char **start, off_t offset, int length, int inout)
2301 {
2302         struct info_str info;
2303         int             retval = -EINVAL;
2304         os_lun_t        *up;
2305         os_tgt_t        *tq;
2306         unsigned int    t, l;
2307         uint32_t        tmp_sn;
2308         uint32_t        *flags;
2309         uint8_t         *loop_state;
2310         scsi_qla_host_t *ha;
2311         char fw_info[30];
2312  
2313         DEBUG3(printk(KERN_INFO
2314             "Entering proc_info buff_in=%p, offset=0x%lx, length=0x%x\n",
2315             buffer, offset, length);)
2316
2317         ha = (scsi_qla_host_t *) shost->hostdata;
2318
2319         if (inout) {
2320                 /* Has data been written to the file? */
2321                 DEBUG3(printk(
2322                     "%s: has data been written to the file. \n",
2323                     __func__);)
2324
2325                 return (qla2x00_set_info(buffer, length, shost));
2326         }
2327
2328         if (start) {
2329                 *start = buffer;
2330         }
2331
2332         info.buffer = buffer;
2333         info.length = length;
2334         info.offset = offset;
2335         info.pos    = 0;
2336
2337         /* start building the print buffer */
2338         copy_info(&info,
2339             "QLogic PCI to Fibre Channel Host Adapter for %s:\n"
2340             "        Firmware version %s, ",
2341             ha->model_number, qla2x00_get_fw_version_str(ha, fw_info));
2342
2343         copy_info(&info, "Driver version %s\n", qla2x00_version_str);
2344
2345         copy_info(&info, "Entry address = %p\n", qla2x00_set_info);
2346
2347         tmp_sn = ((ha->serial0 & 0x1f) << 16) | (ha->serial2 << 8) | 
2348             ha->serial1;
2349         copy_info(&info, "ISP: %s, Serial# %c%05d\n",
2350             ha->brd_info->isp_name, ('A' + tmp_sn/100000), (tmp_sn%100000));
2351
2352         copy_info(&info,
2353             "Request Queue = 0x%llx, Response Queue = 0x%llx\n",
2354                 (unsigned long long)ha->request_dma,
2355                 (unsigned long long)ha->response_dma);
2356
2357         copy_info(&info,
2358             "Request Queue count = %d, Response Queue count = %d\n",
2359             ha->request_q_length, ha->response_q_length);
2360
2361         copy_info(&info,
2362             "Total number of active commands = %ld\n",
2363             ha->actthreads);
2364
2365         copy_info(&info,
2366             "Total number of interrupts = %ld\n",
2367             (long)ha->total_isr_cnt);
2368
2369         copy_info(&info,
2370             "    Device queue depth = 0x%x\n",
2371             (ql2xmaxqdepth == 0) ? 16 : ql2xmaxqdepth);
2372
2373         copy_info(&info,
2374             "Number of free request entries = %d\n", ha->req_q_cnt);
2375
2376         copy_info(&info,
2377             "Number of mailbox timeouts = %ld\n", ha->total_mbx_timeout);
2378
2379         copy_info(&info,
2380             "Number of ISP aborts = %ld\n", ha->total_isp_aborts);
2381
2382         copy_info(&info,
2383             "Number of loop resyncs = %ld\n", ha->total_loop_resync);
2384
2385         copy_info(&info,
2386             "Number of retries for empty slots = %ld\n",
2387             qla2x00_stats.outarray_full);
2388
2389         copy_info(&info,
2390             "Number of reqs in pending_q= %ld, retry_q= %d, "
2391             "done_q= %ld, scsi_retry_q= %d\n",
2392             ha->qthreads, ha->retry_q_cnt,
2393             ha->done_q_cnt, ha->scsi_retry_q_cnt);
2394
2395
2396         flags = (uint32_t *) &ha->flags;
2397
2398         if (atomic_read(&ha->loop_state) == LOOP_DOWN) {
2399                 loop_state = "DOWN";
2400         } else if (atomic_read(&ha->loop_state) == LOOP_UP) {
2401                 loop_state = "UP";
2402         } else if (atomic_read(&ha->loop_state) == LOOP_READY) {
2403                 loop_state = "READY";
2404         } else if (atomic_read(&ha->loop_state) == LOOP_TIMEOUT) {
2405                 loop_state = "TIMEOUT";
2406         } else if (atomic_read(&ha->loop_state) == LOOP_UPDATE) {
2407                 loop_state = "UPDATE";
2408         } else {
2409                 loop_state = "UNKNOWN";
2410         }
2411
2412         copy_info(&info, 
2413             "Host adapter:loop state = <%s>, flags = 0x%lx\n",
2414             loop_state , *flags);
2415
2416         copy_info(&info, "Dpc flags = 0x%lx\n", ha->dpc_flags);
2417
2418         copy_info(&info, "MBX flags = 0x%x\n", ha->mbx_flags);
2419
2420         copy_info(&info, "Link down Timeout = %3.3d\n",
2421             ha->link_down_timeout);
2422
2423         copy_info(&info, "Port down retry = %3.3d\n",
2424             ha->port_down_retry_count);
2425
2426         copy_info(&info, "Login retry count = %3.3d\n",
2427             ha->login_retry_count);
2428
2429         copy_info(&info,
2430             "Commands retried with dropped frame(s) = %d\n",
2431             ha->dropped_frame_error_cnt);
2432
2433         copy_info(&info,
2434             "Product ID = %04x %04x %04x %04x\n", ha->product_id[0],
2435             ha->product_id[1], ha->product_id[2], ha->product_id[3]);
2436
2437         copy_info(&info, "\n");
2438
2439         /* 2.25 node/port display to proc */
2440         /* Display the node name for adapter */
2441         copy_info(&info, "\nSCSI Device Information:\n");
2442         copy_info(&info,
2443             "scsi-qla%d-adapter-node="
2444             "%02x%02x%02x%02x%02x%02x%02x%02x;\n",
2445             (int)ha->instance,
2446             ha->init_cb->node_name[0],
2447             ha->init_cb->node_name[1],
2448             ha->init_cb->node_name[2],
2449             ha->init_cb->node_name[3],
2450             ha->init_cb->node_name[4],
2451             ha->init_cb->node_name[5],
2452             ha->init_cb->node_name[6],
2453             ha->init_cb->node_name[7]);
2454
2455         /* display the port name for adapter */
2456         copy_info(&info,
2457             "scsi-qla%d-adapter-port="
2458             "%02x%02x%02x%02x%02x%02x%02x%02x;\n",
2459             (int)ha->instance,
2460             ha->init_cb->port_name[0],
2461             ha->init_cb->port_name[1],
2462             ha->init_cb->port_name[2],
2463             ha->init_cb->port_name[3],
2464             ha->init_cb->port_name[4],
2465             ha->init_cb->port_name[5],
2466             ha->init_cb->port_name[6],
2467             ha->init_cb->port_name[7]);
2468
2469         /* Print out device port names */
2470         for (t = 0; t < MAX_FIBRE_DEVICES; t++) {
2471                 if ((tq = TGT_Q(ha, t)) == NULL)
2472                         continue;
2473
2474                 copy_info(&info,
2475                     "scsi-qla%d-target-%d="
2476                     "%02x%02x%02x%02x%02x%02x%02x%02x;\n",
2477                     (int)ha->instance, t,
2478                     tq->port_name[0], tq->port_name[1],
2479                     tq->port_name[2], tq->port_name[3],
2480                     tq->port_name[4], tq->port_name[5],
2481                     tq->port_name[6], tq->port_name[7]);
2482         }
2483
2484         copy_info(&info, "\nSCSI LUN Information:\n");
2485         copy_info(&info,
2486             "(Id:Lun)  * - indicates lun is not registered with the OS.\n");
2487
2488         /* scan for all equipment stats */
2489         for (t = 0; t < MAX_FIBRE_DEVICES; t++) {
2490                 /* scan all luns */
2491                 for (l = 0; l < ha->max_luns; l++) {
2492                         up = (os_lun_t *) GET_LU_Q(ha, t, l);
2493
2494                         if (up == NULL) {
2495                                 continue;
2496                         }
2497                         if (up->fclun == NULL) {
2498                                 continue;
2499                         }
2500
2501                         copy_info(&info,
2502                             "(%2d:%2d): Total reqs %ld,",
2503                             t,l,up->io_cnt);
2504
2505                         copy_info(&info,
2506                             " Pending reqs %ld,",
2507                             up->out_cnt);
2508
2509                         if (up->io_cnt < 4) {
2510                                 copy_info(&info,
2511                                     " flags 0x%x*,",
2512                                     (int)up->q_flag);
2513                         } else {
2514                                 copy_info(&info,
2515                                     " flags 0x%x,",
2516                                     (int)up->q_flag);
2517                         }
2518
2519                         copy_info(&info, 
2520                             " %ld:%d:%02x %02x",
2521                             up->fclun->fcport->ha->instance,
2522                             up->fclun->fcport->cur_path,
2523                             up->fclun->fcport->loop_id,
2524                             up->fclun->device_type);
2525
2526                         copy_info(&info, "\n");
2527
2528                         if (info.pos >= info.offset + info.length) {
2529                                 /* No need to continue */
2530                                 goto profile_stop;
2531                         }
2532                 }
2533
2534                 if (info.pos >= info.offset + info.length) {
2535                         /* No need to continue */
2536                         break;
2537                 }
2538         }
2539
2540 profile_stop:
2541
2542         retval = info.pos > info.offset ? info.pos - info.offset : 0;
2543
2544         DEBUG3(printk(KERN_INFO 
2545             "Exiting proc_info: info.pos=%d, offset=0x%lx, "
2546             "length=0x%x\n", info.pos, offset, length);)
2547
2548         return (retval);
2549 }
2550
2551 /*
2552 * qla2x00_display_fc_names
2553 *      This routine will the node names of the different devices found
2554 *      after port inquiry.
2555 *
2556 * Input:
2557 *      cmd = SCSI command structure
2558 *
2559 * Returns:
2560 *      None.
2561 */
2562 static void
2563 qla2x00_display_fc_names(scsi_qla_host_t *ha) 
2564 {
2565         uint16_t        tgt;
2566         os_tgt_t        *tq;
2567
2568         /* Display the node name for adapter */
2569         qla_printk(KERN_INFO, ha,
2570             "scsi-qla%d-adapter-node=%02x%02x%02x%02x%02x%02x%02x%02x\\;\n",
2571             (int)ha->instance,
2572             ha->init_cb->node_name[0],
2573             ha->init_cb->node_name[1],
2574             ha->init_cb->node_name[2],
2575             ha->init_cb->node_name[3],
2576             ha->init_cb->node_name[4],
2577             ha->init_cb->node_name[5],
2578             ha->init_cb->node_name[6],
2579             ha->init_cb->node_name[7]);
2580
2581         /* display the port name for adapter */
2582         qla_printk(KERN_INFO, ha,
2583             "scsi-qla%d-adapter-port=%02x%02x%02x%02x%02x%02x%02x%02x\\;\n",
2584             (int)ha->instance,
2585             ha->init_cb->port_name[0],
2586             ha->init_cb->port_name[1],
2587             ha->init_cb->port_name[2],
2588             ha->init_cb->port_name[3],
2589             ha->init_cb->port_name[4],
2590             ha->init_cb->port_name[5],
2591             ha->init_cb->port_name[6],
2592             ha->init_cb->port_name[7]);
2593
2594         /* Print out device port names */
2595         for (tgt = 0; tgt < MAX_TARGETS; tgt++) {
2596                 if ((tq = ha->otgt[tgt]) == NULL)
2597                         continue;
2598
2599                 if (tq->fcport == NULL)
2600                         continue;
2601
2602                 switch (ha->binding_type) {
2603                         case BIND_BY_PORT_NAME:
2604                                 qla_printk(KERN_INFO, ha,
2605                                     "scsi-qla%d-tgt-%d-di-0-port="
2606                                     "%02x%02x%02x%02x%02x%02x%02x%02x\\;\n",
2607                                     (int)ha->instance, 
2608                                     tgt,
2609                                     tq->port_name[0], 
2610                                     tq->port_name[1],
2611                                     tq->port_name[2], 
2612                                     tq->port_name[3],
2613                                     tq->port_name[4], 
2614                                     tq->port_name[5],
2615                                     tq->port_name[6], 
2616                                     tq->port_name[7]);
2617
2618                                 break;
2619
2620                         case BIND_BY_PORT_ID:
2621                                 qla_printk(KERN_INFO, ha,
2622                                     "scsi-qla%d-tgt-%d-di-0-pid="
2623                                     "%02x%02x%02x\\;\n",
2624                                     (int)ha->instance,
2625                                     tgt,
2626                                     tq->d_id.b.domain,
2627                                     tq->d_id.b.area,
2628                                     tq->d_id.b.al_pa);
2629                                 break;
2630                 }
2631
2632 #if VSA
2633                 qla_printk(KERN_INFO, ha,
2634                     "scsi-qla%d-target-%d-vsa=01;\n", (int)ha->instance, tgt);
2635 #endif
2636         }
2637 }
2638
2639 /*
2640  *  qla2x00_suspend_lun
2641  *      Suspend lun and start port down timer
2642  *
2643  * Input:
2644  *      ha = visable adapter block pointer.
2645  *  lq = lun queue
2646  *  cp = Scsi command pointer 
2647  *  time = time in seconds
2648  *  count = number of times to let time expire
2649  *  delay_lun = non-zero, if lun should be delayed rather than suspended
2650  *
2651  * Return:
2652  *     QLA_SUCCESS  -- suspended lun 
2653  *     QLA_FUNCTION_FAILED  -- Didn't suspend lun
2654  *
2655  * Context:
2656  *      Interrupt context.
2657  */
2658 int
2659 __qla2x00_suspend_lun(scsi_qla_host_t *ha,
2660                 os_lun_t *lq, int time, int count, int delay_lun)
2661 {
2662         int     rval;
2663         srb_t *sp;
2664         struct list_head *list, *temp;
2665         unsigned long flags;
2666
2667         rval = QLA_SUCCESS;
2668
2669         /* if the lun_q is already suspended then don't do it again */
2670         if (lq->q_state == LUN_STATE_READY ||lq->q_state == LUN_STATE_RUN) {
2671
2672                 spin_lock_irqsave(&lq->q_lock, flags);
2673                 if (lq->q_state == LUN_STATE_READY) {
2674                         lq->q_max = count;
2675                         lq->q_count = 0;
2676                 }
2677                 /* Set the suspend time usually 6 secs */
2678                 atomic_set(&lq->q_timer, time);
2679
2680                 /* now suspend the lun */
2681                 lq->q_state = LUN_STATE_WAIT;
2682
2683                 if (delay_lun) {
2684                         set_bit(LUN_EXEC_DELAYED, &lq->q_flag);
2685                         DEBUG(printk(KERN_INFO
2686                             "scsi(%ld): Delay lun execution for %d secs, "
2687                             "count=%d, max count=%d, state=%d\n",
2688                             ha->host_no,
2689                             time,
2690                             lq->q_count, lq->q_max, lq->q_state));
2691                 } else {
2692                         DEBUG(printk(KERN_INFO
2693                             "scsi(%ld): Suspend lun for %d secs, count=%d, "
2694                             "max count=%d, state=%d\n",
2695                             ha->host_no,
2696                             time,
2697                             lq->q_count, lq->q_max, lq->q_state));
2698                 }
2699                 spin_unlock_irqrestore(&lq->q_lock, flags);
2700
2701                 /*
2702                  * Remove all pending commands from request queue and  put them
2703                  * in the scsi_retry queue.
2704                  */
2705                 spin_lock_irqsave(&ha->list_lock, flags);
2706                 list_for_each_safe(list, temp, &ha->pending_queue) {
2707                         sp = list_entry(list, srb_t, list);
2708                         if (sp->lun_queue != lq)
2709                                 continue;
2710
2711                         __del_from_pending_queue(ha, sp);
2712
2713                         if (sp->cmd->allowed < count)
2714                                 sp->cmd->allowed = count;
2715                         __add_to_scsi_retry_queue(ha, sp);
2716
2717                 } /* list_for_each_safe */
2718                 spin_unlock_irqrestore(&ha->list_lock, flags);
2719                 rval = QLA_SUCCESS;
2720         } else {
2721                 rval = QLA_FUNCTION_FAILED;
2722         }
2723
2724         return (rval);
2725 }
2726
2727 /*
2728  * qla2x00_mark_device_lost Updates fcport state when device goes offline.
2729  *
2730  * Input: ha = adapter block pointer.  fcport = port structure pointer.
2731  *
2732  * Return: None.
2733  *
2734  * Context:
2735  */
2736 void qla2x00_mark_device_lost(scsi_qla_host_t *ha, fc_port_t *fcport,
2737     int do_login)
2738 {
2739         /* 
2740          * We may need to retry the login, so don't change the state of the
2741          * port but do the retries.
2742          */
2743         if (atomic_read(&fcport->state) != FCS_DEVICE_DEAD)
2744                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2745
2746         if (!do_login)
2747                 return;
2748
2749         if (fcport->login_retry == 0) {
2750                 fcport->login_retry = ha->login_retry_count;
2751                 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
2752
2753                 DEBUG(printk("scsi(%ld): Port login retry: "
2754                     "%02x%02x%02x%02x%02x%02x%02x%02x, "
2755                     "id = 0x%04x retry cnt=%d\n",
2756                     ha->host_no,
2757                     fcport->port_name[0],
2758                     fcport->port_name[1],
2759                     fcport->port_name[2],
2760                     fcport->port_name[3],
2761                     fcport->port_name[4],
2762                     fcport->port_name[5],
2763                     fcport->port_name[6],
2764                     fcport->port_name[7],
2765                     fcport->loop_id,
2766                     fcport->login_retry));
2767         }
2768 }
2769
2770 /*
2771  * qla2x00_mark_all_devices_lost
2772  *      Updates fcport state when device goes offline.
2773  *
2774  * Input:
2775  *      ha = adapter block pointer.
2776  *      fcport = port structure pointer.
2777  *
2778  * Return:
2779  *      None.
2780  *
2781  * Context:
2782  */
2783 void
2784 qla2x00_mark_all_devices_lost(scsi_qla_host_t *ha) 
2785 {
2786         fc_port_t *fcport;
2787
2788         list_for_each_entry(fcport, &ha->fcports, list) {
2789                 if (fcport->port_type != FCT_TARGET)
2790                         continue;
2791
2792                 /*
2793                  * No point in marking the device as lost, if the device is
2794                  * already DEAD.
2795                  */
2796                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD)
2797                         continue;
2798
2799                 atomic_set(&fcport->state, FCS_DEVICE_LOST);
2800         }
2801 }
2802
2803 /*
2804 * qla2x00_mem_alloc
2805 *      Allocates adapter memory.
2806 *
2807 * Returns:
2808 *      0  = success.
2809 *      1  = failure.
2810 */
2811 static uint8_t
2812 qla2x00_mem_alloc(scsi_qla_host_t *ha)
2813 {
2814         char    name[16];
2815         uint8_t   status = 1;
2816         int     retry= 10;
2817
2818         do {
2819                 /*
2820                  * This will loop only once if everything goes well, else some
2821                  * number of retries will be performed to get around a kernel
2822                  * bug where available mem is not allocated until after a
2823                  * little delay and a retry.
2824                  */
2825                 ha->request_ring = dma_alloc_coherent(&ha->pdev->dev,
2826                     (ha->request_q_length + 1) * sizeof(request_t),
2827                     &ha->request_dma, GFP_KERNEL);
2828                 if (ha->request_ring == NULL) {
2829                         qla_printk(KERN_WARNING, ha,
2830                             "Memory Allocation failed - request_ring\n");
2831
2832                         qla2x00_mem_free(ha);
2833                         msleep(100);
2834
2835                         continue;
2836                 }
2837
2838                 ha->response_ring = dma_alloc_coherent(&ha->pdev->dev,
2839                     (ha->response_q_length + 1) * sizeof(response_t),
2840                     &ha->response_dma, GFP_KERNEL);
2841                 if (ha->response_ring == NULL) {
2842                         qla_printk(KERN_WARNING, ha,
2843                             "Memory Allocation failed - response_ring\n");
2844
2845                         qla2x00_mem_free(ha);
2846                         msleep(100);
2847
2848                         continue;
2849                 }
2850
2851                 ha->gid_list = dma_alloc_coherent(&ha->pdev->dev, GID_LIST_SIZE,
2852                     &ha->gid_list_dma, GFP_KERNEL);
2853                 if (ha->gid_list == NULL) {
2854                         qla_printk(KERN_WARNING, ha,
2855                             "Memory Allocation failed - gid_list\n");
2856
2857                         qla2x00_mem_free(ha);
2858                         msleep(100);
2859
2860                         continue;
2861                 }
2862
2863                 ha->rlc_rsp = dma_alloc_coherent(&ha->pdev->dev,
2864                     sizeof(rpt_lun_cmd_rsp_t), &ha->rlc_rsp_dma, GFP_KERNEL);
2865                 if (ha->rlc_rsp == NULL) {
2866                         qla_printk(KERN_WARNING, ha,
2867                                 "Memory Allocation failed - rlc");
2868
2869                         qla2x00_mem_free(ha);
2870                         msleep(100);
2871
2872                         continue;
2873                 }
2874
2875                 snprintf(name, sizeof(name), "qla2xxx_%ld", ha->host_no);
2876                 ha->s_dma_pool = dma_pool_create(name, &ha->pdev->dev,
2877                     DMA_POOL_SIZE, 8, 0);
2878                 if (ha->s_dma_pool == NULL) {
2879                         qla_printk(KERN_WARNING, ha,
2880                             "Memory Allocation failed - s_dma_pool\n");
2881
2882                         qla2x00_mem_free(ha);
2883                         msleep(100);
2884
2885                         continue;
2886                 }
2887
2888                 /* get consistent memory allocated for init control block */
2889                 ha->init_cb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2890                     &ha->init_cb_dma);
2891                 if (ha->init_cb == NULL) {
2892                         qla_printk(KERN_WARNING, ha,
2893                             "Memory Allocation failed - init_cb\n");
2894
2895                         qla2x00_mem_free(ha);
2896                         msleep(100);
2897
2898                         continue;
2899                 }
2900                 memset(ha->init_cb, 0, sizeof(init_cb_t));
2901
2902                 /* Get consistent memory allocated for Get Port Database cmd */
2903                 ha->iodesc_pd = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2904                     &ha->iodesc_pd_dma);
2905                 if (ha->iodesc_pd == NULL) {
2906                         /* error */
2907                         qla_printk(KERN_WARNING, ha,
2908                             "Memory Allocation failed - iodesc_pd\n");
2909
2910                         qla2x00_mem_free(ha);
2911                         msleep(100);
2912
2913                         continue;
2914                 }
2915                 memset(ha->iodesc_pd, 0, PORT_DATABASE_SIZE);
2916
2917                 /* Allocate ioctl related memory. */
2918                 if (qla2x00_alloc_ioctl_mem(ha)) {
2919                         qla_printk(KERN_WARNING, ha,
2920                             "Memory Allocation failed - ioctl_mem\n");
2921
2922                         qla2x00_mem_free(ha);
2923                         msleep(100);
2924
2925                         continue;
2926                 }
2927
2928                 if (qla2x00_allocate_sp_pool(ha)) {
2929                         qla_printk(KERN_WARNING, ha,
2930                             "Memory Allocation failed - "
2931                             "qla2x00_allocate_sp_pool()\n");
2932
2933                         qla2x00_mem_free(ha);
2934                         msleep(100);
2935
2936                         continue;
2937                 }
2938
2939                 /* Allocate memory for SNS commands */
2940                 if (IS_QLA2100(ha) || IS_QLA2200(ha)) {
2941                         /* Get consistent memory allocated for SNS commands */
2942                         ha->sns_cmd = dma_alloc_coherent(&ha->pdev->dev,
2943                             sizeof(struct sns_cmd_pkt), &ha->sns_cmd_dma,
2944                             GFP_KERNEL);
2945                         if (ha->sns_cmd == NULL) {
2946                                 /* error */
2947                                 qla_printk(KERN_WARNING, ha,
2948                                     "Memory Allocation failed - sns_cmd\n");
2949
2950                                 qla2x00_mem_free(ha);
2951                                 msleep(100);
2952
2953                                 continue;
2954                         }
2955                         memset(ha->sns_cmd, 0, sizeof(struct sns_cmd_pkt));
2956                 } else {
2957                         /* Get consistent memory allocated for MS IOCB */
2958                         ha->ms_iocb = dma_pool_alloc(ha->s_dma_pool, GFP_KERNEL,
2959                             &ha->ms_iocb_dma);
2960                         if (ha->ms_iocb == NULL) {
2961                                 /* error */
2962                                 qla_printk(KERN_WARNING, ha,
2963                                     "Memory Allocation failed - ms_iocb\n");
2964
2965                                 qla2x00_mem_free(ha);
2966                                 msleep(100);
2967
2968                                 continue;
2969                         }
2970                         memset(ha->ms_iocb, 0, sizeof(ms_iocb_entry_t));
2971
2972                         /*
2973                          * Get consistent memory allocated for CT SNS
2974                          * commands
2975                          */
2976                         ha->ct_sns = dma_alloc_coherent(&ha->pdev->dev,
2977                             sizeof(struct ct_sns_pkt), &ha->ct_sns_dma,
2978                             GFP_KERNEL);
2979                         if (ha->ct_sns == NULL) {
2980                                 /* error */
2981                                 qla_printk(KERN_WARNING, ha,
2982                                     "Memory Allocation failed - ct_sns\n");
2983
2984                                 qla2x00_mem_free(ha);
2985                                 msleep(100);
2986
2987                                 continue;
2988                         }
2989                         memset(ha->ct_sns, 0, sizeof(struct ct_sns_pkt));
2990                 }
2991
2992                 /* Done all allocations without any error. */
2993                 status = 0;
2994
2995         } while (retry-- && status != 0);
2996
2997         if (status) {
2998                 printk(KERN_WARNING
2999                         "%s(): **** FAILED ****\n", __func__);
3000         }
3001
3002         return(status);
3003 }
3004
3005 /*
3006 * qla2x00_mem_free
3007 *      Frees all adapter allocated memory.
3008 *
3009 * Input:
3010 *      ha = adapter block pointer.
3011 */
3012 static void
3013 qla2x00_mem_free(scsi_qla_host_t *ha)
3014 {
3015         uint32_t        t;
3016         struct list_head        *fcpl, *fcptemp;
3017         fc_port_t       *fcport;
3018         struct list_head        *fcll, *fcltemp;
3019         fc_lun_t        *fclun;
3020         unsigned long   wtime;/* max wait time if mbx cmd is busy. */
3021
3022         if (ha == NULL) {
3023                 /* error */
3024                 DEBUG2(printk("%s(): ERROR invalid ha pointer.\n", __func__));
3025                 return;
3026         }
3027
3028         /* Free the target queues */
3029         for (t = 0; t < MAX_TARGETS; t++) {
3030                 qla2x00_tgt_free(ha, t);
3031         }
3032
3033         /* Make sure all other threads are stopped. */
3034         wtime = 60 * HZ;
3035         while (ha->dpc_wait && wtime) {
3036                 set_current_state(TASK_INTERRUPTIBLE);
3037                 wtime = schedule_timeout(wtime);
3038         }
3039
3040         /* free ioctl memory */
3041         qla2x00_free_ioctl_mem(ha);
3042
3043         /* free sp pool */
3044         qla2x00_free_sp_pool(ha);
3045
3046         if (ha->sns_cmd)
3047                 dma_free_coherent(&ha->pdev->dev, sizeof(struct sns_cmd_pkt),
3048                     ha->sns_cmd, ha->sns_cmd_dma);
3049
3050         if (ha->ct_sns)
3051                 dma_free_coherent(&ha->pdev->dev, sizeof(struct ct_sns_pkt),
3052                     ha->ct_sns, ha->ct_sns_dma);
3053
3054         if (ha->ms_iocb)
3055                 dma_pool_free(ha->s_dma_pool, ha->ms_iocb, ha->ms_iocb_dma);
3056
3057         if (ha->iodesc_pd)
3058                 dma_pool_free(ha->s_dma_pool, ha->iodesc_pd, ha->iodesc_pd_dma);
3059
3060         if (ha->init_cb)
3061                 dma_pool_free(ha->s_dma_pool, ha->init_cb, ha->init_cb_dma);
3062
3063         if (ha->s_dma_pool)
3064                 dma_pool_destroy(ha->s_dma_pool);
3065
3066         if (ha->rlc_rsp)
3067                 dma_free_coherent(&ha->pdev->dev,
3068                     sizeof(rpt_lun_cmd_rsp_t), ha->rlc_rsp,
3069                     ha->rlc_rsp_dma);
3070
3071         if (ha->gid_list)
3072                 dma_free_coherent(&ha->pdev->dev, GID_LIST_SIZE, ha->gid_list,
3073                     ha->gid_list_dma);
3074
3075         if (ha->response_ring)
3076                 dma_free_coherent(&ha->pdev->dev,
3077                     (ha->response_q_length + 1) * sizeof(response_t),
3078                     ha->response_ring, ha->response_dma);
3079
3080         if (ha->request_ring)
3081                 dma_free_coherent(&ha->pdev->dev,
3082                     (ha->request_q_length + 1) * sizeof(request_t),
3083                     ha->request_ring, ha->request_dma);
3084
3085         ha->sns_cmd = NULL;
3086         ha->sns_cmd_dma = 0;
3087         ha->ct_sns = NULL;
3088         ha->ct_sns_dma = 0;
3089         ha->ms_iocb = NULL;
3090         ha->ms_iocb_dma = 0;
3091         ha->iodesc_pd = NULL;
3092         ha->iodesc_pd_dma = 0;
3093         ha->init_cb = NULL;
3094         ha->init_cb_dma = 0;
3095
3096         ha->s_dma_pool = NULL;
3097
3098         ha->rlc_rsp = NULL;
3099         ha->rlc_rsp_dma = 0;
3100         ha->gid_list = NULL;
3101         ha->gid_list_dma = 0;
3102
3103         ha->response_ring = NULL;
3104         ha->response_dma = 0;
3105         ha->request_ring = NULL;
3106         ha->request_dma = 0;
3107
3108         list_for_each_safe(fcpl, fcptemp, &ha->fcports) {
3109                 fcport = list_entry(fcpl, fc_port_t, list);
3110
3111                 /* fc luns */
3112                 list_for_each_safe(fcll, fcltemp, &fcport->fcluns) {
3113                         fclun = list_entry(fcll, fc_lun_t, list);
3114
3115                         list_del_init(&fclun->list);
3116                         kfree(fclun);
3117                 }
3118
3119                 /* fc ports */
3120                 list_del_init(&fcport->list);
3121                 kfree(fcport);
3122         }
3123         INIT_LIST_HEAD(&ha->fcports);
3124
3125         if (ha->fw_dump)
3126                 free_pages((unsigned long)ha->fw_dump, ha->fw_dump_order);
3127
3128         if (ha->fw_dump_buffer)
3129                 vfree(ha->fw_dump_buffer);
3130
3131         ha->fw_dump = NULL;
3132         ha->fw_dump_reading = 0;
3133         ha->fw_dump_buffer = NULL;
3134 }
3135
3136 /*
3137  * qla2x00_allocate_sp_pool
3138  *       This routine is called during initialization to allocate
3139  *       memory for local srb_t.
3140  *
3141  * Input:
3142  *       ha   = adapter block pointer.
3143  *
3144  * Context:
3145  *      Kernel context.
3146  * 
3147  * Note: Sets the ref_count for non Null sp to one.
3148  */
3149 int
3150 qla2x00_allocate_sp_pool(scsi_qla_host_t *ha) 
3151 {
3152         int      rval;
3153
3154         rval = QLA_SUCCESS;
3155         ha->srb_mempool = mempool_create(SRB_MIN_REQ, mempool_alloc_slab,
3156             mempool_free_slab, srb_cachep);
3157         if (ha->srb_mempool == NULL) {
3158                 qla_printk(KERN_INFO, ha, "Unable to allocate SRB mempool.\n");
3159                 rval = QLA_FUNCTION_FAILED;
3160         }
3161         return (rval);
3162 }
3163
3164 /*
3165  *  This routine frees all adapter allocated memory.
3166  *  
3167  */
3168 void
3169 qla2x00_free_sp_pool( scsi_qla_host_t *ha) 
3170 {
3171         if (ha->srb_mempool) {
3172                 mempool_destroy(ha->srb_mempool);
3173                 ha->srb_mempool = NULL;
3174         }
3175 }
3176
3177 /**************************************************************************
3178 * qla2x00_do_dpc
3179 *   This kernel thread is a task that is schedule by the interrupt handler
3180 *   to perform the background processing for interrupts.
3181 *
3182 * Notes:
3183 * This task always run in the context of a kernel thread.  It
3184 * is kick-off by the driver's detect code and starts up
3185 * up one per adapter. It immediately goes to sleep and waits for
3186 * some fibre event.  When either the interrupt handler or
3187 * the timer routine detects a event it will one of the task
3188 * bits then wake us up.
3189 **************************************************************************/
3190 static int
3191 qla2x00_do_dpc(void *data)
3192 {
3193         DECLARE_MUTEX_LOCKED(sem);
3194         scsi_qla_host_t *ha;
3195         fc_port_t       *fcport;
3196         os_lun_t        *q;
3197         srb_t           *sp;
3198         uint8_t         status;
3199         unsigned long   flags = 0;
3200         struct list_head *list, *templist;
3201         int     dead_cnt, online_cnt;
3202         int     retry_cmds = 0;
3203         uint16_t        next_loopid;
3204         int t;
3205         os_tgt_t *tq;
3206
3207         ha = (scsi_qla_host_t *)data;
3208
3209         lock_kernel();
3210
3211         daemonize("%s_dpc", ha->host_str);
3212         allow_signal(SIGHUP);
3213
3214         ha->dpc_wait = &sem;
3215
3216         set_user_nice(current, -20);
3217
3218         unlock_kernel();
3219
3220         complete(&ha->dpc_inited);
3221
3222         while (1) {
3223                 DEBUG3(printk("qla2x00: DPC handler sleeping\n"));
3224
3225                 if (down_interruptible(&sem))
3226                         break;
3227
3228                 if (ha->dpc_should_die)
3229                         break;
3230
3231                 DEBUG3(printk("qla2x00: DPC handler waking up\n"));
3232
3233                 /* Initialization not yet finished. Don't do anything yet. */
3234                 if (!ha->flags.init_done || ha->dpc_active)
3235                         continue;
3236
3237                 DEBUG3(printk("scsi(%ld): DPC handler\n", ha->host_no));
3238
3239                 ha->dpc_active = 1;
3240
3241                 if (!list_empty(&ha->done_queue))
3242                         qla2x00_done(ha);
3243
3244                 /* Process commands in retry queue */
3245                 if (test_and_clear_bit(PORT_RESTART_NEEDED, &ha->dpc_flags)) {
3246                         DEBUG(printk("scsi(%ld): DPC checking retry_q. "
3247                             "total=%d\n",
3248                             ha->host_no, ha->retry_q_cnt));
3249
3250                         spin_lock_irqsave(&ha->list_lock, flags);
3251                         dead_cnt = online_cnt = 0;
3252                         list_for_each_safe(list, templist, &ha->retry_queue) {
3253                                 sp = list_entry(list, srb_t, list);
3254                                 q = sp->lun_queue;
3255                                 DEBUG3(printk("scsi(%ld): pid=%ld sp=%p, "
3256                                     "spflags=0x%x, q_flag= 0x%lx\n",
3257                                     ha->host_no, sp->cmd->serial_number, sp,
3258                                     sp->flags, q->q_flag));
3259
3260                                 if (q == NULL)
3261                                         continue;
3262                                 fcport = q->fclun->fcport;
3263
3264                                 if (atomic_read(&fcport->state) ==
3265                                     FCS_DEVICE_DEAD ||
3266                                     atomic_read(&fcport->ha->loop_state) == LOOP_DEAD) {
3267
3268                                         __del_from_retry_queue(ha, sp);
3269                                         sp->cmd->result = DID_NO_CONNECT << 16;
3270                                         if (atomic_read(&fcport->ha->loop_state) ==
3271                                             LOOP_DOWN) 
3272                                                 sp->err_id = SRB_ERR_LOOP;
3273                                         else
3274                                                 sp->err_id = SRB_ERR_PORT;
3275                                         sp->cmd->host_scribble =
3276                                             (unsigned char *) NULL;
3277                                         __add_to_done_queue(ha, sp);
3278                                         dead_cnt++;
3279                                 } else if (atomic_read(&fcport->state) !=
3280                                     FCS_DEVICE_LOST) {
3281
3282                                         __del_from_retry_queue(ha, sp);
3283                                         sp->cmd->result = DID_BUS_BUSY << 16;
3284                                         sp->cmd->host_scribble =
3285                                             (unsigned char *) NULL;
3286                                         __add_to_done_queue(ha, sp);
3287                                         online_cnt++;
3288                                 }
3289                         } /* list_for_each_safe() */
3290                         spin_unlock_irqrestore(&ha->list_lock, flags);
3291
3292                         DEBUG(printk("scsi(%ld): done processing retry queue "
3293                             "- dead=%d, online=%d\n ",
3294                             ha->host_no, dead_cnt, online_cnt));
3295                 }
3296
3297                 /* Process commands in scsi retry queue */
3298                 if (test_and_clear_bit(SCSI_RESTART_NEEDED, &ha->dpc_flags)) {
3299                         /*
3300                          * Any requests we want to delay for some period is put
3301                          * in the scsi retry queue with a delay added. The
3302                          * timer will schedule a "scsi_restart_needed" every 
3303                          * second as long as there are requests in the scsi
3304                          * queue. 
3305                          */
3306                         DEBUG(printk("scsi(%ld): DPC checking scsi "
3307                             "retry_q.total=%d\n",
3308                             ha->host_no, ha->scsi_retry_q_cnt));
3309
3310                         online_cnt = 0;
3311                         spin_lock_irqsave(&ha->list_lock, flags);
3312                         list_for_each_safe(list, templist,
3313                             &ha->scsi_retry_queue) {
3314
3315                                 sp = list_entry(list, srb_t, list);
3316                                 q = sp->lun_queue;
3317                                 tq = sp->tgt_queue;
3318
3319                                 DEBUG3(printk("scsi(%ld): scsi_retry_q: "
3320                                     "pid=%ld sp=%p, spflags=0x%x, "
3321                                     "q_flag= 0x%lx,q_state=%d\n",
3322                                     ha->host_no, sp->cmd->serial_number,
3323                                     sp, sp->flags, q->q_flag, q->q_state));
3324
3325                                 /* Was this lun suspended */
3326                                 if (q->q_state != LUN_STATE_WAIT) {
3327                                         online_cnt++;
3328                                         __del_from_scsi_retry_queue(ha, sp);
3329
3330                                         if (test_bit(TQF_RETRY_CMDS,
3331                                             &tq->flags)) {
3332                                                 qla2x00_extend_timeout(sp->cmd,
3333                                                     (sp->cmd->timeout_per_command / HZ) - QLA_CMD_TIMER_DELTA);
3334                                                 __add_to_pending_queue(ha, sp);
3335                                                 retry_cmds++;
3336                                         } else
3337                                                 __add_to_retry_queue(ha, sp);
3338                                 }
3339
3340                                 /* Was this command suspended for N secs */
3341                                 if (sp->delay != 0) {
3342                                         sp->delay--;
3343                                         if (sp->delay == 0) {
3344                                                 online_cnt++;
3345                                                 __del_from_scsi_retry_queue(
3346                                                     ha, sp);
3347                                                 __add_to_retry_queue(ha,sp);
3348                                         }
3349                                 }
3350                         }
3351                         spin_unlock_irqrestore(&ha->list_lock, flags);
3352
3353                         /* Clear all Target Unsuspended bits */
3354                         for (t = 0; t < ha->max_targets; t++) {
3355                                 if ((tq = ha->otgt[t]) == NULL)
3356                                         continue;
3357
3358                                 if (test_bit(TQF_RETRY_CMDS, &tq->flags))
3359                                         clear_bit(TQF_RETRY_CMDS, &tq->flags);
3360                         }
3361                         if (retry_cmds)
3362                                 qla2x00_next(ha);
3363
3364                         DEBUG(if (online_cnt > 0))
3365                         DEBUG(printk("scsi(%ld): dpc() found scsi reqs to "
3366                             "restart= %d\n",
3367                             ha->host_no, online_cnt));
3368                 }
3369
3370                 if (ha->flags.mbox_busy) {
3371                         if (!list_empty(&ha->done_queue))
3372                                 qla2x00_done(ha);
3373
3374                         ha->dpc_active = 0;
3375                         continue;
3376                 }
3377
3378                 if (test_and_clear_bit(ISP_ABORT_NEEDED, &ha->dpc_flags)) {
3379
3380                         DEBUG(printk("scsi(%ld): dpc: sched "
3381                             "qla2x00_abort_isp ha = %p\n",
3382                             ha->host_no, ha));
3383                         if (!(test_and_set_bit(ABORT_ISP_ACTIVE,
3384                             &ha->dpc_flags))) {
3385
3386                                 if (qla2x00_abort_isp(ha)) {
3387                                         /* failed. retry later */
3388                                         set_bit(ISP_ABORT_NEEDED,
3389                                             &ha->dpc_flags);
3390                                 }
3391                                 clear_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags);
3392                         }
3393                         DEBUG(printk("scsi(%ld): dpc: qla2x00_abort_isp end\n",
3394                             ha->host_no));
3395                 }
3396
3397                 if (test_and_clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags) &&
3398                     (!(test_and_set_bit(RESET_ACTIVE, &ha->dpc_flags)))) {
3399
3400                         DEBUG(printk("scsi(%ld): qla2x00_reset_marker()\n",
3401                             ha->host_no));
3402
3403                         qla2x00_rst_aen(ha);
3404                         clear_bit(RESET_ACTIVE, &ha->dpc_flags);
3405                 }
3406
3407                 /* Retry each device up to login retry count */
3408                 if ((test_and_clear_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
3409                     !test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) &&
3410                     atomic_read(&ha->loop_state) != LOOP_DOWN) {
3411
3412                         DEBUG(printk("scsi(%ld): qla2x00_port_login()\n",
3413                             ha->host_no));
3414
3415                         next_loopid = 0;
3416                         list_for_each_entry(fcport, &ha->fcports, list) {
3417                                 if (fcport->port_type != FCT_TARGET)
3418                                         continue;
3419
3420                                 /*
3421                                  * If the port is not ONLINE then try to login
3422                                  * to it if we haven't run out of retries.
3423                                  */
3424                                 if (atomic_read(&fcport->state) != FCS_ONLINE &&
3425                                     fcport->login_retry) {
3426
3427                                         fcport->login_retry--;
3428                                         if (fcport->flags & FCF_FABRIC_DEVICE) {
3429                                                 if (fcport->flags &
3430                                                     FCF_TAPE_PRESENT)
3431                                                         qla2x00_fabric_logout(
3432                                                             ha,
3433                                                             fcport->loop_id);
3434                                                 status = qla2x00_fabric_login(
3435                                                     ha, fcport, &next_loopid);
3436                                         } else
3437                                                 status =
3438                                                     qla2x00_local_device_login(
3439                                                         ha, fcport->loop_id);
3440
3441                                         if (status == QLA_SUCCESS) {
3442                                                 fcport->old_loop_id = fcport->loop_id;
3443
3444                                                 DEBUG(printk("scsi(%ld): port login OK: logged in ID 0x%x\n",
3445                                                     ha->host_no, fcport->loop_id));
3446                                                 
3447                                                 fcport->port_login_retry_count =
3448                                                     ha->port_down_retry_count * PORT_RETRY_TIME;
3449                                                 atomic_set(&fcport->state, FCS_ONLINE);
3450                                                 atomic_set(&fcport->port_down_timer,
3451                                                     ha->port_down_retry_count * PORT_RETRY_TIME);
3452
3453                                                 fcport->login_retry = 0;
3454                                         } else if (status == 1) {
3455                                                 set_bit(RELOGIN_NEEDED, &ha->dpc_flags);
3456                                                 /* retry the login again */
3457                                                 DEBUG(printk("scsi(%ld): Retrying %d login again loop_id 0x%x\n",
3458                                                     ha->host_no,
3459                                                     fcport->login_retry, fcport->loop_id));
3460                                         } else {
3461                                                 fcport->login_retry = 0;
3462                                         }
3463                                 }
3464                                 if (test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags))
3465                                         break;
3466                         }
3467                         DEBUG(printk("scsi(%ld): qla2x00_port_login - end\n",
3468                             ha->host_no));
3469                 }
3470
3471                 if ((test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags)) &&
3472                     atomic_read(&ha->loop_state) != LOOP_DOWN) {
3473
3474                         clear_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags);
3475                         DEBUG(printk("scsi(%ld): qla2x00_login_retry()\n",
3476                             ha->host_no));
3477                                 
3478                         set_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags);
3479
3480                         DEBUG(printk("scsi(%ld): qla2x00_login_retry - end\n",
3481                             ha->host_no));
3482                 }
3483
3484                 if (test_and_clear_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags)) {
3485
3486                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync()\n",
3487                             ha->host_no));
3488
3489                         if (!(test_and_set_bit(LOOP_RESYNC_ACTIVE,
3490                             &ha->dpc_flags))) {
3491
3492                                 qla2x00_loop_resync(ha);
3493
3494                                 clear_bit(LOOP_RESYNC_ACTIVE, &ha->dpc_flags);
3495                         }
3496
3497                         DEBUG(printk("scsi(%ld): qla2x00_loop_resync - end\n",
3498                             ha->host_no));
3499                 }
3500
3501
3502                 if (test_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags)) {
3503                         DEBUG(printk("scsi(%ld): qla2x00_restart_queues()\n",
3504                             ha->host_no));
3505
3506                         qla2x00_restart_queues(ha, 0);
3507
3508                         DEBUG(printk("scsi(%ld): qla2x00_restart_queues - end\n",
3509                             ha->host_no));
3510                 }
3511
3512                 if (test_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags)) {
3513
3514                         DEBUG(printk("scsi(%ld): qla2x00_abort_queues()\n",
3515                             ha->host_no));
3516                                 
3517                         qla2x00_abort_queues(ha, 0);
3518
3519                         DEBUG(printk("scsi(%ld): qla2x00_abort_queues - end\n",
3520                             ha->host_no));
3521                 }
3522
3523                 if (test_and_clear_bit(FCPORT_RESCAN_NEEDED, &ha->dpc_flags)) {
3524
3525                         DEBUG(printk("scsi(%ld): Rescan flagged fcports...\n",
3526                             ha->host_no));
3527
3528                         qla2x00_rescan_fcports(ha);
3529
3530                         DEBUG(printk("scsi(%ld): Rescan flagged fcports..."
3531                             "end.\n",
3532                             ha->host_no));
3533                 }
3534
3535
3536                 if (!ha->interrupts_on)
3537                         qla2x00_enable_intrs(ha);
3538
3539                 if (!list_empty(&ha->done_queue))
3540                         qla2x00_done(ha);
3541
3542                 ha->dpc_active = 0;
3543         } /* End of while(1) */
3544
3545         DEBUG(printk("scsi(%ld): DPC handler exiting\n", ha->host_no));
3546
3547         /*
3548          * Make sure that nobody tries to wake us up again.
3549          */
3550         ha->dpc_wait = NULL;
3551         ha->dpc_active = 0;
3552
3553         complete_and_exit(&ha->dpc_exited, 0);
3554 }
3555
3556 /*
3557  *  qla2x00_abort_queues
3558  *      Abort all commands on queues on device
3559  *
3560  * Input:
3561  *      ha = adapter block pointer.
3562  *
3563  * Context:
3564  *      Interrupt context.
3565  */
3566 void
3567 qla2x00_abort_queues(scsi_qla_host_t *ha, uint8_t doneqflg) 
3568 {
3569
3570         srb_t       *sp;
3571         struct list_head *list, *temp;
3572         unsigned long flags;
3573
3574         clear_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
3575
3576         /* Return all commands device queues. */
3577         spin_lock_irqsave(&ha->list_lock,flags);
3578         list_for_each_safe(list, temp, &ha->pending_queue) {
3579                 sp = list_entry(list, srb_t, list);
3580
3581                 if (sp->flags & SRB_ABORTED)
3582                         continue;
3583
3584                 /* Remove srb from LUN queue. */
3585                 __del_from_pending_queue(ha, sp);
3586
3587                 /* Set ending status. */
3588                 sp->cmd->result = DID_BUS_BUSY << 16;
3589
3590                 __add_to_done_queue(ha, sp);
3591         }
3592         spin_unlock_irqrestore(&ha->list_lock, flags);
3593 }
3594
3595 /*
3596 *  qla2x00_rst_aen
3597 *      Processes asynchronous reset.
3598 *
3599 * Input:
3600 *      ha  = adapter block pointer.
3601 */
3602 static void
3603 qla2x00_rst_aen(scsi_qla_host_t *ha) 
3604 {
3605         if (ha->flags.online && !ha->flags.reset_active &&
3606             !atomic_read(&ha->loop_down_timer) &&
3607             !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags))) {
3608                 do {
3609                         clear_bit(RESET_MARKER_NEEDED, &ha->dpc_flags);
3610
3611                         /*
3612                          * Issue marker command only when we are going to start
3613                          * the I/O.
3614                          */
3615                         ha->marker_needed = 1;
3616                 } while (!atomic_read(&ha->loop_down_timer) &&
3617                     (test_bit(RESET_MARKER_NEEDED, &ha->dpc_flags)));
3618         }
3619 }
3620
3621
3622 /*
3623  * This routine will allocate SP from the free queue
3624  * input:
3625  *        scsi_qla_host_t *
3626  * output:
3627  *        srb_t * or NULL
3628  */
3629 srb_t *
3630 qla2x00_get_new_sp(scsi_qla_host_t *ha)
3631 {
3632         srb_t *sp;
3633
3634         sp = mempool_alloc(ha->srb_mempool, GFP_ATOMIC);
3635         if (sp)
3636                 atomic_set(&sp->ref_count, 1);
3637         return (sp);
3638 }
3639
3640 /**************************************************************************
3641 *   qla2x00_timer
3642 *
3643 * Description:
3644 *   One second timer
3645 *
3646 * Context: Interrupt
3647 ***************************************************************************/
3648 static void
3649 qla2x00_timer(scsi_qla_host_t *ha)
3650 {
3651         int             t,l;
3652         unsigned long   cpu_flags = 0;
3653         fc_port_t       *fcport;
3654         os_lun_t *lq;
3655         os_tgt_t *tq;
3656         int             start_dpc = 0;
3657         int             index;
3658         srb_t           *sp;
3659
3660         /*
3661          * We try and restart any request in the retry queue every second.
3662          */
3663         if (!list_empty(&ha->retry_queue)) {
3664                 set_bit(PORT_RESTART_NEEDED, &ha->dpc_flags);
3665                 start_dpc++;
3666         }
3667
3668         /*
3669          * We try and restart any request in the scsi_retry queue every second.
3670          */
3671         if (!list_empty(&ha->scsi_retry_queue)) {
3672                 set_bit(SCSI_RESTART_NEEDED, &ha->dpc_flags);
3673                 start_dpc++;
3674         }
3675
3676         /*
3677          * Ports - Port down timer.
3678          *
3679          * Whenever, a port is in the LOST state we start decrementing its port
3680          * down timer every second until it reaches zero. Once  it reaches zero
3681          * the port it marked DEAD. 
3682          */
3683         t = 0;
3684         list_for_each_entry(fcport, &ha->fcports, list) {
3685                 if (fcport->port_type != FCT_TARGET)
3686                         continue;
3687
3688                 if (atomic_read(&fcport->state) == FCS_DEVICE_LOST) {
3689
3690                         if (atomic_read(&fcport->port_down_timer) == 0)
3691                                 continue;
3692
3693                         if (atomic_dec_and_test(&fcport->port_down_timer) != 0) 
3694                                 atomic_set(&fcport->state, FCS_DEVICE_DEAD);
3695                         
3696                         DEBUG(printk("scsi(%ld): fcport-%d - port retry count: "
3697                             "%d remainning\n",
3698                             ha->host_no,
3699                             t, atomic_read(&fcport->port_down_timer)));
3700                 }
3701                 t++;
3702         } /* End of for fcport  */
3703
3704         /*
3705          * LUNS - lun suspend timer.
3706          *
3707          * Whenever, a lun is suspended the timer starts decrementing its
3708          * suspend timer every second until it reaches zero. Once  it reaches
3709          * zero the lun retry count is decremented. 
3710          */
3711
3712         /*
3713          * FIXME(dg) - Need to convert this linear search of luns into a search
3714          * of a list of suspended luns.
3715          */
3716         for (t = 0; t < ha->max_targets; t++) {
3717                 if ((tq = ha->otgt[t]) == NULL)
3718                         continue;
3719
3720                 for (l = 0; l < ha->max_luns; l++) {
3721                         if ((lq = (os_lun_t *) tq->olun[l]) == NULL)
3722                                 continue;
3723
3724                         spin_lock_irqsave(&lq->q_lock, cpu_flags);
3725                         if (lq->q_state == LUN_STATE_WAIT &&
3726                                 atomic_read(&lq->q_timer) != 0) {
3727
3728                                 if (atomic_dec_and_test(&lq->q_timer) != 0) {
3729                                         /*
3730                                          * A delay should immediately
3731                                          * transition to a READY state
3732                                          */
3733                                         if (test_and_clear_bit(LUN_EXEC_DELAYED,
3734                                             &lq->q_flag)) {
3735                                                 lq->q_state = LUN_STATE_READY;
3736                                         }
3737                                         else {
3738                                                 lq->q_count++;
3739                                                 if (lq->q_count == lq->q_max)
3740                                                         lq->q_state =
3741                                                             LUN_STATE_TIMEOUT;
3742                                                 else
3743                                                         lq->q_state =
3744                                                             LUN_STATE_RUN;
3745                                         }
3746                                 }
3747                                 DEBUG3(printk("scsi(%ld): lun%d - timer %d, "
3748                                     "count=%d, max=%d, state=%d\n",
3749                                     ha->host_no,
3750                                     l,
3751                                     atomic_read(&lq->q_timer),
3752                                     lq->q_count, lq->q_max, lq->q_state));
3753                         }
3754                         spin_unlock_irqrestore(&lq->q_lock, cpu_flags);
3755                 } /* End of for luns  */
3756         } /* End of for targets  */
3757
3758         /* Loop down handler. */
3759         if (atomic_read(&ha->loop_down_timer) > 0 &&
3760             !(test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) && ha->flags.online) {
3761
3762                 if (atomic_read(&ha->loop_down_timer) ==
3763                     ha->loop_down_abort_time) {
3764
3765                         DEBUG(printk("scsi(%ld): Loop Down - aborting the "
3766                             "queues before time expire\n",
3767                             ha->host_no));
3768
3769                         if (!IS_QLA2100(ha) && ha->link_down_timeout)
3770                                 atomic_set(&ha->loop_state, LOOP_DEAD); 
3771
3772                         /* Schedule an ISP abort to return any tape commands. */
3773                         spin_lock_irqsave(&ha->hardware_lock, cpu_flags);
3774                         for (index = 1; index < MAX_OUTSTANDING_COMMANDS;
3775                             index++) {
3776                                 sp = ha->outstanding_cmds[index];
3777                                 if (!sp)
3778                                         continue;
3779                                 if (!(sp->fclun->fcport->flags &
3780                                     FCF_TAPE_PRESENT))
3781                                         continue;
3782
3783                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3784                                 break;
3785                         }
3786                         spin_unlock_irqrestore(&ha->hardware_lock, cpu_flags);
3787
3788                         set_bit(ABORT_QUEUES_NEEDED, &ha->dpc_flags);
3789                         start_dpc++;
3790                 }
3791
3792                 /* if the loop has been down for 4 minutes, reinit adapter */
3793                 if (atomic_dec_and_test(&ha->loop_down_timer) != 0) {
3794                         DEBUG(printk("scsi(%ld): Loop down exceed 4 mins - "
3795                             "restarting queues.\n",
3796                             ha->host_no));
3797
3798                         set_bit(RESTART_QUEUES_NEEDED, &ha->dpc_flags);
3799                         start_dpc++;
3800
3801                         if (!(ha->device_flags & DFLG_NO_CABLE)) {
3802                                 DEBUG(printk("scsi(%ld): Loop down - "
3803                                     "aborting ISP.\n",
3804                                     ha->host_no));
3805                                 qla_printk(KERN_WARNING, ha,
3806                                     "Loop down - aborting ISP.\n");
3807
3808                                 set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
3809                         }
3810                 }
3811                 DEBUG3(printk("scsi(%ld): Loop Down - seconds remainning %d\n",
3812                     ha->host_no,
3813                     atomic_read(&ha->loop_down_timer)));
3814         }
3815
3816         /*
3817          * Done Q Handler -- dgFIXME This handler will kick off doneq if we
3818          * haven't process it in 2 seconds.
3819          */
3820         if (!list_empty(&ha->done_queue))
3821                 qla2x00_done(ha);
3822
3823
3824         /* Schedule the DPC routine if needed */
3825         if ((test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
3826             test_bit(LOOP_RESYNC_NEEDED, &ha->dpc_flags) ||
3827             start_dpc ||
3828             test_bit(LOGIN_RETRY_NEEDED, &ha->dpc_flags) ||
3829             test_bit(RELOGIN_NEEDED, &ha->dpc_flags)) &&
3830             ha->dpc_wait && !ha->dpc_active) {
3831
3832                 up(ha->dpc_wait);
3833         }
3834
3835         qla2x00_restart_timer(ha, WATCH_INTERVAL);
3836 }
3837
3838 /*
3839  * qla2x00_extend_timeout
3840  *      This routine will extend the timeout to the specified value.
3841  *
3842  * Input:
3843  *      cmd = SCSI command structure
3844  *
3845  * Returns:
3846  *      None.
3847  */
3848 void 
3849 qla2x00_extend_timeout(struct scsi_cmnd *cmd, int timeout) 
3850 {
3851         srb_t *sp = (srb_t *) CMD_SP(cmd);
3852         u_long our_jiffies = (timeout * HZ) + jiffies;
3853
3854         sp->ext_history= 0; 
3855         sp->e_start = jiffies;
3856         if (cmd->eh_timeout.function) {
3857                 mod_timer(&cmd->eh_timeout,our_jiffies);
3858                 sp->ext_history |= 1;
3859         }
3860         if (sp->timer.function != NULL) {
3861                 /* 
3862                  * Our internal timer should timeout before the midlayer has a
3863                  * chance begin the abort process
3864                  */
3865                 mod_timer(&sp->timer,our_jiffies - (QLA_CMD_TIMER_DELTA * HZ));
3866
3867                 sp->ext_history |= 2;
3868         }
3869 }
3870
3871 /**************************************************************************
3872 *   qla2x00_cmd_timeout
3873 *
3874 * Description:
3875 *       Handles the command if it times out in any state.
3876 *
3877 * Input:
3878 *     sp - pointer to validate
3879 *
3880 * Returns:
3881 * None.
3882 * Note:Need to add the support for if( sp->state == SRB_FAILOVER_STATE).
3883 **************************************************************************/
3884 void
3885 qla2x00_cmd_timeout(srb_t *sp)
3886 {
3887         int t, l;
3888         int processed;
3889         scsi_qla_host_t *vis_ha, *dest_ha;
3890         struct scsi_cmnd *cmd;
3891         unsigned long flags, cpu_flags;
3892         fc_port_t *fcport;
3893
3894         cmd = sp->cmd;
3895         vis_ha = (scsi_qla_host_t *)cmd->device->host->hostdata;
3896
3897         DEBUG3(printk("cmd_timeout: Entering sp->state = %x\n", sp->state));
3898
3899         t = cmd->device->id;
3900         l = cmd->device->lun;
3901         fcport = sp->fclun->fcport;
3902         dest_ha = sp->ha;
3903
3904         /*
3905          * If IO is found either in retry Queue 
3906          *    OR in Lun Queue
3907          * Return this IO back to host
3908          */
3909         spin_lock_irqsave(&vis_ha->list_lock, flags);
3910         processed = 0;
3911         if (sp->state == SRB_PENDING_STATE) {
3912                 __del_from_pending_queue(vis_ha, sp);
3913                 DEBUG2(printk("scsi(%ld): Found in Pending queue pid %ld, "
3914                     "State = %x., fcport state=%d jiffies=%lx\n",
3915                     vis_ha->host_no, cmd->serial_number, sp->state,
3916                     atomic_read(&fcport->state), jiffies));
3917
3918                 /*
3919                  * If FC_DEVICE is marked as dead return the cmd with
3920                  * DID_NO_CONNECT status.  Otherwise set the host_byte to
3921                  * DID_BUS_BUSY to let the OS  retry this cmd.
3922                  */
3923                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
3924                     atomic_read(&fcport->ha->loop_state) == LOOP_DEAD) {
3925                         cmd->result = DID_NO_CONNECT << 16;
3926                         if (atomic_read(&fcport->ha->loop_state) == LOOP_DOWN) 
3927                                 sp->err_id = SRB_ERR_LOOP;
3928                         else
3929                                 sp->err_id = SRB_ERR_PORT;
3930                 } else {
3931                         cmd->result = DID_BUS_BUSY << 16;
3932                 }
3933                 __add_to_done_queue(vis_ha, sp);
3934                 processed++;
3935         } 
3936         spin_unlock_irqrestore(&vis_ha->list_lock, flags);
3937
3938         if (processed) {
3939                 qla2x00_done(vis_ha);
3940                 return;
3941         }
3942
3943         spin_lock_irqsave(&dest_ha->list_lock, flags);
3944         if ((sp->state == SRB_RETRY_STATE) ||
3945             (sp->state == SRB_SCSI_RETRY_STATE)) {
3946
3947                 DEBUG2(printk("scsi(%ld): Found in (Scsi) Retry queue or "
3948                     "failover Q pid %ld, State = %x., fcport state=%d "
3949                     "jiffies=%lx retried=%d\n",
3950                     dest_ha->host_no, cmd->serial_number, sp->state,
3951                     atomic_read(&fcport->state), jiffies, cmd->retries));
3952
3953                 if ((sp->state == SRB_RETRY_STATE)) {
3954                         __del_from_retry_queue(dest_ha, sp);
3955                 } else if ((sp->state == SRB_SCSI_RETRY_STATE)) {
3956                         __del_from_scsi_retry_queue(dest_ha, sp);
3957                 } 
3958
3959                 /*
3960                  * If FC_DEVICE is marked as dead return the cmd with
3961                  * DID_NO_CONNECT status.  Otherwise set the host_byte to
3962                  * DID_BUS_BUSY to let the OS  retry this cmd.
3963                  */
3964                 if ((atomic_read(&fcport->state) == FCS_DEVICE_DEAD) ||
3965                     atomic_read(&dest_ha->loop_state) == LOOP_DEAD) {
3966                         qla2x00_extend_timeout(cmd, EXTEND_CMD_TIMEOUT);
3967                         cmd->result = DID_NO_CONNECT << 16;
3968                         if (atomic_read(&dest_ha->loop_state) == LOOP_DOWN) 
3969                                 sp->err_id = SRB_ERR_LOOP;
3970                         else
3971                                 sp->err_id = SRB_ERR_PORT;
3972                 } else {
3973                         cmd->result = DID_BUS_BUSY << 16;
3974                 }
3975
3976                 __add_to_done_queue(dest_ha, sp);
3977                 processed++;
3978         } 
3979         spin_unlock_irqrestore(&dest_ha->list_lock, flags);
3980
3981         if (processed) {
3982                 qla2x00_done(dest_ha);
3983
3984                  return;
3985         }
3986
3987         spin_lock_irqsave(&dest_ha->list_lock, cpu_flags);
3988         if (sp->state == SRB_DONE_STATE) {
3989                 /* IO in done_q  -- leave it */
3990                 DEBUG(printk("scsi(%ld): Found in Done queue pid %ld sp=%p.\n",
3991                     dest_ha->host_no, cmd->serial_number, sp));
3992         } else if (sp->state == SRB_SUSPENDED_STATE) {
3993                 DEBUG(printk("scsi(%ld): Found SP %p in suspended state  "
3994                     "- pid %ld:\n",
3995                     dest_ha->host_no, sp, cmd->serial_number));
3996                 DEBUG(qla2x00_dump_buffer((uint8_t *)sp, sizeof(srb_t));)
3997         } else if (sp->state == SRB_ACTIVE_STATE) {
3998                 /*
3999                  * IO is with ISP find the command in our active list.
4000                  */
4001                 spin_unlock_irqrestore(&dest_ha->list_lock, cpu_flags);
4002                 spin_lock_irqsave(&dest_ha->hardware_lock, flags);
4003                 if (sp == dest_ha->outstanding_cmds[
4004                     (unsigned long)sp->cmd->host_scribble]) {
4005
4006                         DEBUG(printk("cmd_timeout: Found in ISP \n"));
4007
4008                         if (sp->flags & SRB_TAPE) {
4009                                 /*
4010                                  * We cannot allow the midlayer error handler
4011                                  * to wakeup and begin the abort process.
4012                                  * Extend the timer so that the firmware can
4013                                  * properly return the IOCB.
4014                                  */
4015                                 DEBUG(printk("cmd_timeout: Extending timeout "
4016                                     "of FCP2 tape command!\n"));
4017                                 qla2x00_extend_timeout(sp->cmd,
4018                                     EXTEND_CMD_TIMEOUT);
4019                         }
4020                         sp->state = SRB_ACTIVE_TIMEOUT_STATE;
4021                         spin_unlock_irqrestore(&dest_ha->hardware_lock, flags);
4022                 } else {
4023                         spin_unlock_irqrestore(&dest_ha->hardware_lock, flags);
4024                         printk(KERN_INFO 
4025                                 "qla_cmd_timeout: State indicates it is with "
4026                                 "ISP, But not in active array\n");
4027                 }
4028                 spin_lock_irqsave(&dest_ha->list_lock, cpu_flags);
4029         } else if (sp->state == SRB_ACTIVE_TIMEOUT_STATE) {
4030                 DEBUG(printk("qla2100%ld: Found in Active timeout state"
4031                                 "pid %ld, State = %x., \n",
4032                                 dest_ha->host_no,
4033                                 sp->cmd->serial_number, sp->state);)
4034         } else {
4035                 /* EMPTY */
4036                 DEBUG2(printk("cmd_timeout%ld: LOST command state = "
4037                                 "0x%x, sp=%p\n",
4038                                 vis_ha->host_no, sp->state,sp);)
4039
4040                 qla_printk(KERN_INFO, vis_ha,
4041                         "cmd_timeout: LOST command state = 0x%x\n", sp->state);
4042         }
4043         spin_unlock_irqrestore(&dest_ha->list_lock, cpu_flags);
4044
4045         DEBUG3(printk("cmd_timeout: Leaving\n");)
4046 }
4047
4048 /**************************************************************************
4049 * qla2x00_done
4050 *      Process completed commands.
4051 *
4052 * Input:
4053 *      old_ha           = adapter block pointer.
4054 *
4055 **************************************************************************/
4056 void
4057 qla2x00_done(scsi_qla_host_t *old_ha)
4058 {
4059         os_lun_t        *lq;
4060         struct scsi_cmnd *cmd;
4061         unsigned long   flags = 0;
4062         scsi_qla_host_t *ha;
4063         scsi_qla_host_t *vis_ha;
4064         int     send_marker_once = 0;
4065         srb_t           *sp, *sptemp;
4066         LIST_HEAD(local_sp_list);
4067
4068         /*
4069          * Get into local queue such that we do not wind up calling done queue
4070          * tasklet for the same IOs from DPC or any other place.
4071          */
4072         spin_lock_irqsave(&old_ha->list_lock, flags);
4073         list_splice_init(&old_ha->done_queue, &local_sp_list);
4074         spin_unlock_irqrestore(&old_ha->list_lock, flags);
4075
4076         /*
4077          * All done commands are in the local queue, now do the call back.
4078          */
4079         list_for_each_entry_safe(sp, sptemp, &local_sp_list, list) {
4080                 old_ha->done_q_cnt--;
4081                 sp->state = SRB_NO_QUEUE_STATE;
4082
4083                 /* remove command from local list */
4084                 list_del_init(&sp->list);
4085
4086                 cmd = sp->cmd;
4087                 if (cmd == NULL)
4088                         continue;
4089
4090                 vis_ha = (scsi_qla_host_t *)cmd->device->host->hostdata;
4091                 lq = sp->lun_queue;
4092                 ha = sp->ha;
4093
4094                 if (sp->flags & SRB_DMA_VALID) {
4095                         sp->flags &= ~SRB_DMA_VALID;
4096
4097                         /* Release memory used for this I/O */
4098                         if (cmd->use_sg) {
4099                                 pci_unmap_sg(ha->pdev, cmd->request_buffer,
4100                                     cmd->use_sg, cmd->sc_data_direction);
4101                         } else if (cmd->request_bufflen) {
4102                                 pci_unmap_page(ha->pdev, sp->dma_handle,
4103                                     cmd->request_bufflen,
4104                                     cmd->sc_data_direction);
4105                         }
4106                 }
4107
4108
4109                 switch (host_byte(cmd->result)) {
4110                         case DID_OK:
4111                         case DID_ERROR:
4112                                 break;
4113
4114                         case DID_RESET:
4115                                 /*
4116                                  * Set marker needed, so we don't have to
4117                                  * send multiple markers
4118                                  */
4119                                 if (!send_marker_once) {
4120                                         ha->marker_needed = 1;
4121                                         send_marker_once++;
4122                                 }
4123
4124                                 /*
4125                                  * WORKAROUND
4126                                  *
4127                                  * A backdoor device-reset requires different
4128                                  * error handling.  This code differentiates
4129                                  * between normal error handling and the
4130                                  * backdoor method.
4131                                  *
4132                                  */
4133                                 if (ha->host->eh_active != EH_ACTIVE)
4134                                         cmd->result = DID_BUS_BUSY << 16;
4135                                 break;
4136
4137
4138                         case DID_ABORT:
4139                                 sp->flags &= ~SRB_ABORT_PENDING;
4140                                 sp->flags |= SRB_ABORTED;
4141
4142                                 if (sp->flags & SRB_TIMEOUT)
4143                                         cmd->result = DID_TIME_OUT << 16;
4144
4145                                 break;
4146
4147                         default:
4148                                 DEBUG2(printk("scsi(%ld:%d:%d) %s: did_error "
4149                                     "= %d, comp-scsi= 0x%x-0x%x.\n",
4150                                     vis_ha->host_no,
4151                                     cmd->device->id, cmd->device->lun,
4152                                     __func__,
4153                                     host_byte(cmd->result),
4154                                     CMD_COMPL_STATUS(cmd),
4155                                     CMD_SCSI_STATUS(cmd)));
4156                                 break;
4157                 }
4158
4159                 /*
4160                  * Call the mid-level driver interrupt handler -- via sp_put()
4161                  */
4162                 sp_put(ha, sp);
4163         } /* end of while */
4164 }
4165
4166 /*
4167  * qla2x00_process_response_queue_in_zio_mode
4168  *      Process response queue completion as fast as possible
4169  *      to achieve Zero Interrupt Opertions-ZIO
4170  *
4171  * Input:
4172  *      ha = adapter block pointer.
4173  *
4174  * Context:
4175  *      Kernel context.
4176  */
4177 static inline void
4178 qla2x00_process_response_queue_in_zio_mode(scsi_qla_host_t *ha)
4179 {
4180         unsigned long flags;
4181
4182         /* Check for unprocessed commands in response queue. */
4183         if (!ha->flags.process_response_queue)
4184                 return;
4185         if (!ha->flags.online)
4186                 return;
4187         if (ha->response_ring_ptr->signature == RESPONSE_PROCESSED)
4188                 return;
4189         
4190         spin_lock_irqsave(&ha->hardware_lock,flags);
4191         qla2x00_process_response_queue(ha);
4192         spin_unlock_irqrestore(&ha->hardware_lock, flags);
4193 }
4194
4195 /*
4196  * qla2x00_next
4197  *      Retrieve and process next job in the LUN queue.
4198  *
4199  * Input:
4200  *      tq = SCSI target queue pointer.
4201  *      lq = SCSI LUN queue pointer.
4202  *      TGT_LOCK must be already obtained.
4203  *
4204  * Output:
4205  *      Releases TGT_LOCK upon exit.
4206  *
4207  * Context:
4208  *      Kernel/Interrupt context.
4209  * 
4210  * Note: This routine will always try to start I/O from visible HBA.
4211  */
4212 void
4213 qla2x00_next(scsi_qla_host_t *vis_ha) 
4214 {
4215         int             rval;
4216         unsigned long   flags;
4217         scsi_qla_host_t *dest_ha;
4218         fc_port_t       *fcport;
4219         srb_t           *sp, *sptemp;
4220         LIST_HEAD(local_sp_list);
4221
4222         dest_ha = NULL;
4223
4224         spin_lock_irqsave(&vis_ha->list_lock, flags);
4225         list_splice_init(&vis_ha->pending_queue, &local_sp_list);
4226         vis_ha->qthreads = 0;
4227         spin_unlock_irqrestore(&vis_ha->list_lock, flags);
4228
4229         list_for_each_entry_safe(sp, sptemp, &local_sp_list, list) {
4230                 list_del_init(&sp->list);
4231                 sp->state = SRB_NO_QUEUE_STATE;
4232
4233                 fcport = sp->fclun->fcport;
4234                 dest_ha = fcport->ha;
4235
4236                 /* If device is dead then send request back to OS */
4237                 if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD) {
4238                         sp->cmd->result = DID_NO_CONNECT << 16;
4239                         if (atomic_read(&dest_ha->loop_state) == LOOP_DOWN) 
4240                                 sp->err_id = SRB_ERR_LOOP;
4241                         else
4242                                 sp->err_id = SRB_ERR_PORT;
4243
4244                         DEBUG3(printk("scsi(%ld): loop/port is down - pid=%ld, "
4245                             "sp=%p err_id=%d loopid=0x%x queued to dest HBA "
4246                             "scsi%ld.\n", dest_ha->host_no,
4247                             sp->cmd->serial_number, sp, sp->err_id,
4248                             fcport->loop_id, dest_ha->host_no));
4249                         /* 
4250                          * Initiate a failover - done routine will initiate.
4251                          */
4252                         add_to_done_queue(vis_ha, sp);
4253
4254                         continue;
4255                 }
4256
4257                 /*
4258                  * SCSI Kluge: Whenever, we need to wait for an event such as
4259                  * loop down (i.e. loop_down_timer ) or port down (i.e.  LUN
4260                  * request qeueue is suspended) then we will recycle new
4261                  * commands back to the SCSI layer.  We do this because this is
4262                  * normally a temporary condition and we don't want the
4263                  * mid-level scsi.c driver to get upset and start aborting
4264                  * commands.  The timeout value is extracted from the command
4265                  * minus 1-second and put on a retry queue (watchdog). Once the
4266                  * command timeout it is returned to the mid-level with a BUSY
4267                  * status, so the mid-level will retry it. This process
4268                  * continues until the LOOP DOWN time expires or the condition
4269                  * goes away.
4270                  */
4271                 if (!(sp->flags & (SRB_IOCTL | SRB_TAPE)) &&
4272                     (atomic_read(&fcport->state) != FCS_ONLINE ||
4273                         test_bit(ABORT_ISP_ACTIVE, &dest_ha->dpc_flags) ||
4274                         atomic_read(&dest_ha->loop_state) != LOOP_READY)) {
4275
4276                         DEBUG3(printk("scsi(%ld): port=(0x%x) retry_q(%d) "
4277                             "loop state = %d, loop counter = 0x%x dpc flags "
4278                             "= 0x%lx\n",
4279                             dest_ha->host_no,
4280                             fcport->loop_id,
4281                             atomic_read(&fcport->state),
4282                             atomic_read(&dest_ha->loop_state),
4283                             atomic_read(&dest_ha->loop_down_timer),
4284                             dest_ha->dpc_flags));
4285
4286                         qla2x00_extend_timeout(sp->cmd, EXTEND_CMD_TIMEOUT);
4287                         add_to_retry_queue(vis_ha, sp);
4288
4289                         continue;
4290                 } 
4291
4292                 /*
4293                  * If this request's lun is suspended then put the request on
4294                  * the  scsi_retry queue. 
4295                  */
4296                 if (!(sp->flags & (SRB_IOCTL | SRB_TAPE)) &&
4297                     sp->lun_queue->q_state == LUN_STATE_WAIT) {
4298                         DEBUG3(printk("scsi(%ld): lun wait state - pid=%ld, "
4299                             "opcode=%d, allowed=%d, retries=%d\n",
4300                             dest_ha->host_no,
4301                             sp->cmd->serial_number,
4302                             sp->cmd->cmnd[0],
4303                             sp->cmd->allowed,
4304                             sp->cmd->retries));
4305                                 
4306                         add_to_scsi_retry_queue(vis_ha, sp);
4307
4308                         continue;
4309                 }
4310
4311                 sp->lun_queue->io_cnt++;
4312
4313                 rval = qla2x00_start_scsi(sp);
4314                 if (rval != QLA_SUCCESS) {
4315                         /* Place request back on top of device queue */
4316                         /* add to the top of queue */
4317                         add_to_pending_queue_head(vis_ha, sp);
4318
4319                         sp->lun_queue->io_cnt--;
4320                 }
4321         }
4322
4323         if (!IS_QLA2100(vis_ha) && !IS_QLA2200(vis_ha)) {
4324                 /* Process response_queue if ZIO support is enabled*/ 
4325                 qla2x00_process_response_queue_in_zio_mode(vis_ha);
4326
4327         }
4328 }
4329
4330
4331 /**************************************************************************
4332 *   qla2x00_check_tgt_status
4333 *
4334 * Description:
4335 *     Checks to see if the target or loop is down.
4336 *
4337 * Input:
4338 *     cmd - pointer to Scsi cmd structure
4339 *
4340 * Returns:
4341 *   1 - if target is present
4342 *   0 - if target is not present
4343 *
4344 **************************************************************************/
4345 int
4346 qla2x00_check_tgt_status(scsi_qla_host_t *ha, struct scsi_cmnd *cmd)
4347 {
4348         os_lun_t        *lq;
4349         unsigned int    b, t, l;
4350         fc_port_t       *fcport;
4351
4352         /* Generate LU queue on bus, target, LUN */
4353         b = cmd->device->channel;
4354         t = cmd->device->id;
4355         l = cmd->device->lun;
4356
4357         if ((lq = GET_LU_Q(ha,t,l)) == NULL) {
4358                 return (QLA_FUNCTION_FAILED);
4359         }
4360
4361         fcport = lq->fclun->fcport;
4362
4363         if (TGT_Q(ha, t) == NULL ||
4364             l >= ha->max_luns ||
4365             atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
4366             atomic_read(&ha->loop_state) == LOOP_DEAD ||
4367             (!atomic_read(&ha->loop_down_timer) &&
4368                 atomic_read(&ha->loop_state) == LOOP_DOWN) ||
4369             test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags) ||
4370             test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
4371             atomic_read(&ha->loop_state) != LOOP_READY) {
4372
4373                 DEBUG(printk(KERN_INFO
4374                     "scsi(%ld:%2d:%2d:%2d): %s connection is down\n",
4375                     ha->host_no,
4376                     b, t, l,
4377                     __func__));
4378
4379                 cmd->result = DID_NO_CONNECT << 16;
4380                 return (QLA_FUNCTION_FAILED);
4381         }
4382         return (QLA_SUCCESS);
4383 }
4384
4385 /**************************************************************************
4386 *   qla2x00_check_port_status
4387 *
4388 * Description:
4389 *     Checks to see if the port or loop is down.
4390 *
4391 * Input:
4392 *     fcport - pointer to fc_port_t structure.
4393 *
4394 * Returns:
4395 *   1 - if port is present
4396 *   0 - if port is not present
4397 *
4398 **************************************************************************/
4399 int
4400 qla2x00_check_port_status(scsi_qla_host_t *ha, fc_port_t *fcport)
4401 {
4402         if (fcport == NULL) {
4403                 return (QLA_FUNCTION_FAILED);
4404         }
4405
4406         if (atomic_read(&fcport->state) == FCS_DEVICE_DEAD ||
4407             atomic_read(&ha->loop_state) == LOOP_DEAD) {
4408                 return (QLA_FUNCTION_FAILED);
4409         }
4410
4411         if ((atomic_read(&fcport->state) != FCS_ONLINE) || 
4412             (!atomic_read(&ha->loop_down_timer) &&
4413                 atomic_read(&ha->loop_state) == LOOP_DOWN) ||
4414             (test_bit(ABORT_ISP_ACTIVE, &ha->dpc_flags)) ||
4415             test_bit(CFG_ACTIVE, &ha->cfg_flags) ||
4416             test_bit(ISP_ABORT_NEEDED, &ha->dpc_flags) ||
4417             atomic_read(&ha->loop_state) != LOOP_READY) {
4418
4419                 DEBUG(printk(KERN_INFO
4420                     "scsi(%ld): Connection is down. fcport=%p.\n",
4421                     ha->host_no, fcport));
4422
4423                 return (QLA_BUSY);
4424         }
4425
4426         return (QLA_SUCCESS);
4427 }
4428
4429 /* XXX(hch): crude hack to emulate a down_timeout() */
4430 int
4431 qla2x00_down_timeout(struct semaphore *sema, unsigned long timeout)
4432 {
4433         const unsigned int step = HZ/10;
4434
4435         do {
4436                 if (!down_trylock(sema))
4437                         return 0;
4438                 set_current_state(TASK_INTERRUPTIBLE);
4439                 if (schedule_timeout(step))
4440                         break;
4441         } while ((timeout -= step) > 0);
4442
4443         return -ETIMEDOUT;
4444 }
4445
4446 static void
4447 qla2xxx_get_port_id(struct scsi_target *starget)
4448 {
4449         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
4450         scsi_qla_host_t *ha = to_qla_host(shost);
4451         struct fc_port *fc;
4452
4453         list_for_each_entry(fc, &ha->fcports, list) {
4454                 if (fc->os_target_id == starget->id) {
4455                         fc_starget_port_id(starget) = fc->d_id.b.domain << 16 |
4456                                 fc->d_id.b.area << 8 | 
4457                                 fc->d_id.b.al_pa;
4458                         return;
4459                 }
4460         }
4461         fc_starget_port_id(starget) = -1;
4462 }
4463
4464 static void
4465 qla2xxx_get_port_name(struct scsi_target *starget)
4466 {
4467         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
4468         scsi_qla_host_t *ha = to_qla_host(shost);
4469         struct fc_port *fc;
4470
4471         list_for_each_entry(fc, &ha->fcports, list) {
4472                 if (fc->os_target_id == starget->id) {
4473                         fc_starget_port_name(starget) =
4474                                 __be64_to_cpu(*(uint64_t *)fc->port_name);
4475                         return;
4476                 }
4477         }
4478         fc_starget_port_name(starget) = -1;
4479 }
4480
4481 static void
4482 qla2xxx_get_node_name(struct scsi_target *starget)
4483 {
4484         struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
4485         scsi_qla_host_t *ha = to_qla_host(shost);
4486         struct fc_port *fc;
4487
4488         list_for_each_entry(fc, &ha->fcports, list) {
4489                 if (fc->os_target_id == starget->id) {
4490                         fc_starget_node_name(starget) =
4491                                 __be64_to_cpu(*(uint64_t *)fc->node_name);
4492                         return;
4493                 }
4494         }
4495         fc_starget_node_name(starget) = -1;
4496 }
4497
4498 static struct fc_function_template qla2xxx_transport_functions = {
4499         .get_starget_port_id = qla2xxx_get_port_id,
4500         .show_starget_port_id = 1,
4501         .get_starget_port_name = qla2xxx_get_port_name,
4502         .show_starget_port_name = 1,
4503         .get_starget_node_name = qla2xxx_get_node_name,
4504         .show_starget_node_name = 1,
4505 };
4506
4507 /**
4508  * qla2x00_module_init - Module initialization.
4509  **/
4510 static int __init
4511 qla2x00_module_init(void)
4512 {
4513         /* Allocate cache for SRBs. */
4514         sprintf(srb_cachep_name, "qla2xxx_srbs");
4515         srb_cachep = kmem_cache_create(srb_cachep_name, sizeof(srb_t), 0,
4516             SLAB_HWCACHE_ALIGN, NULL, NULL);
4517         if (srb_cachep == NULL) {
4518                 printk(KERN_ERR
4519                     "qla2xxx: Unable to allocate SRB cache...Failing load!\n");
4520                 return -ENOMEM;
4521         }
4522
4523         /* Derive version string. */
4524         strcpy(qla2x00_version_str, QLA2XXX_VERSION);
4525 #if DEBUG_QLA2100
4526         strcat(qla2x00_version_str, "-debug");
4527 #endif
4528
4529         qla2xxx_transport_template = fc_attach_transport(&qla2xxx_transport_functions);
4530         if (!qla2xxx_transport_template)
4531                 return -ENODEV;
4532
4533         printk(KERN_INFO
4534             "QLogic Fibre Channel HBA Driver (%p)\n", qla2x00_set_info);
4535
4536         return 0;
4537 }
4538
4539 /**
4540  * qla2x00_module_exit - Module cleanup.
4541  **/
4542 static void __exit
4543 qla2x00_module_exit(void)
4544 {
4545         /* Free SRBs cache. */
4546         if (srb_cachep != NULL) {
4547                 if (kmem_cache_destroy(srb_cachep) != 0) {
4548                         printk(KERN_ERR
4549                             "qla2xxx: Unable to free SRB cache...Memory pools "
4550                             "still active?\n");
4551                 }
4552                 srb_cachep = NULL;
4553         }
4554
4555         fc_release_transport(qla2xxx_transport_template);
4556 }
4557
4558 module_init(qla2x00_module_init);
4559 module_exit(qla2x00_module_exit);
4560
4561 MODULE_AUTHOR("QLogic Corporation");
4562 MODULE_DESCRIPTION("QLogic Fibre Channel HBA Driver");
4563 MODULE_LICENSE("GPL");
4564 MODULE_VERSION(QLA2XXX_VERSION);