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