upgrade to linux 2.6.9-1.11_FC2
[linux-2.6.git] / drivers / scsi / gdth.c
1 /************************************************************************
2  * Linux driver for                                                     *  
3  * ICP vortex GmbH:    GDT ISA/EISA/PCI Disk Array Controllers          *
4  * Intel Corporation:  Storage RAID Controllers                         *
5  *                                                                      *
6  * gdth.c                                                               *
7  * Copyright (C) 1995-04 ICP vortex GmbH, Achim Leubner                 *
8  * Copyright (C) 2002-04 Intel Corporation                              *
9  * Copyright (C) 2003-04 Adaptec Inc.                                   *
10  * <achim_leubner@adaptec.com>                                          *
11  *                                                                      *
12  * Additions/Fixes:                                                     *
13  * Boji Tony Kannanthanam <boji.t.kannanthanam@intel.com>               *
14  * Johannes Dinner <johannes_dinner@adaptec.com>                        *
15  *                                                                      *
16  * This program is free software; you can redistribute it and/or modify *
17  * it under the terms of the GNU General Public License as published    *
18  * by the Free Software Foundation; either version 2 of the License,    *
19  * or (at your option) any later version.                               *
20  *                                                                      *
21  * This program is distributed in the hope that it will be useful,      *
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
24  * GNU General Public License for more details.                         *
25  *                                                                      *
26  * You should have received a copy of the GNU General Public License    *
27  * along with this kernel; if not, write to the Free Software           *
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
29  *                                                                      *
30  * Linux kernel 2.2.x, 2.4.x, 2.6.x supported                           *
31  *                                                                      *
32  * $Log: gdth.c,v $
33  * Revision 1.73  2004/03/31 13:33:03  achim
34  * Special command 0xfd implemented to detect 64-bit DMA support
35  *
36  * Revision 1.72  2004/03/17 08:56:04  achim
37  * 64-bit DMA only enabled if FW >= x.43
38  *
39  * Revision 1.71  2004/03/05 15:51:29  achim
40  * Screen service: separate message buffer, bugfixes
41  *
42  * Revision 1.70  2004/02/27 12:19:07  achim
43  * Bugfix: Reset bit in config (0xfe) call removed
44  *
45  * Revision 1.69  2004/02/20 09:50:24  achim
46  * Compatibility changes for kernels < 2.4.20
47  * Bugfix screen service command size
48  * pci_set_dma_mask() error handling added
49  *
50  * Revision 1.68  2004/02/19 15:46:54  achim
51  * 64-bit DMA bugfixes
52  * Drive size bugfix for drives > 1TB
53  *
54  * Revision 1.67  2004/01/14 13:11:57  achim
55  * Tool access over /proc no longer supported
56  * Bugfixes IOCTLs
57  *
58  * Revision 1.66  2003/12/19 15:04:06  achim
59  * Bugfixes support for drives > 2TB
60  *
61  * Revision 1.65  2003/12/15 11:21:56  achim
62  * 64-bit DMA support added
63  * Support for drives > 2 TB implemented
64  * Kernels 2.2.x, 2.4.x, 2.6.x supported
65  *
66  * Revision 1.64  2003/09/17 08:30:26  achim
67  * EISA/ISA controller scan disabled
68  * Command line switch probe_eisa_isa added
69  *
70  * Revision 1.63  2003/07/12 14:01:00  Daniele Bellucci <bellucda@tiscali.it>
71  * Minor cleanups in gdth_ioctl.
72  *
73  * Revision 1.62  2003/02/27 15:01:59  achim
74  * Dynamic DMA mapping implemented
75  * New (character device) IOCTL interface added
76  * Other controller related changes made
77  *
78  * Revision 1.61  2002/11/08 13:09:52  boji
79  * Added support for XSCALE based RAID Controllers
80  * Fixed SCREENSERVICE initialization in SMP cases
81  * Added checks for gdth_polling before GDTH_HA_LOCK
82  *
83  * Revision 1.60  2002/02/05 09:35:22  achim
84  * MODULE_LICENSE only if kernel >= 2.4.11
85  *
86  * Revision 1.59  2002/01/30 09:46:33  achim
87  * Small changes
88  *
89  * Revision 1.58  2002/01/29 15:30:02  achim
90  * Set default value of shared_access to Y
91  * New status S_CACHE_RESERV for clustering added
92  *
93  * Revision 1.57  2001/08/21 11:16:35  achim
94  * Bugfix free_irq()
95  *
96  * Revision 1.56  2001/08/09 11:19:39  achim
97  * Scsi_Host_Template changes
98  *
99  * Revision 1.55  2001/08/09 10:11:28  achim
100  * Command HOST_UNFREEZE_IO before cache service init.
101  *
102  * Revision 1.54  2001/07/20 13:48:12  achim
103  * Expand: gdth_analyse_hdrive() removed
104  *
105  * Revision 1.53  2001/07/17 09:52:49  achim
106  * Small OEM related change
107  *
108  * Revision 1.52  2001/06/19 15:06:20  achim
109  * New host command GDT_UNFREEZE_IO added
110  *
111  * Revision 1.51  2001/05/22 06:42:37  achim
112  * PCI: Subdevice ID added
113  *
114  * Revision 1.50  2001/05/17 13:42:16  achim
115  * Support for Intel Storage RAID Controllers added
116  *
117  * Revision 1.50  2001/05/17 12:12:34  achim
118  * Support for Intel Storage RAID Controllers added
119  *
120  * Revision 1.49  2001/03/15 15:07:17  achim
121  * New __setup interface for boot command line options added
122  *
123  * Revision 1.48  2001/02/06 12:36:28  achim
124  * Bugfix Cluster protocol
125  *
126  * Revision 1.47  2001/01/10 14:42:06  achim
127  * New switch shared_access added
128  *
129  * Revision 1.46  2001/01/09 08:11:35  achim
130  * gdth_command() removed
131  * meaning of Scsi_Pointer members changed
132  *
133  * Revision 1.45  2000/11/16 12:02:24  achim
134  * Changes for kernel 2.4
135  *
136  * Revision 1.44  2000/10/11 08:44:10  achim
137  * Clustering changes: New flag media_changed added
138  *
139  * Revision 1.43  2000/09/20 12:59:01  achim
140  * DPMEM remap functions for all PCI controller types implemented
141  * Small changes for ia64 platform
142  *
143  * Revision 1.42  2000/07/20 09:04:50  achim
144  * Small changes for kernel 2.4
145  *
146  * Revision 1.41  2000/07/04 14:11:11  achim
147  * gdth_analyse_hdrive() added to rescan drives after online expansion
148  *
149  * Revision 1.40  2000/06/27 11:24:16  achim
150  * Changes Clustering, Screenservice
151  *
152  * Revision 1.39  2000/06/15 13:09:04  achim
153  * Changes for gdth_do_cmd()
154  *
155  * Revision 1.38  2000/06/15 12:08:43  achim
156  * Bugfix gdth_sync_event(), service SCREENSERVICE
157  * Data direction for command 0xc2 changed to DOU
158  *
159  * Revision 1.37  2000/05/25 13:50:10  achim
160  * New driver parameter virt_ctr added
161  *
162  * Revision 1.36  2000/05/04 08:50:46  achim
163  * Event buffer now in gdth_ha_str
164  *
165  * Revision 1.35  2000/03/03 10:44:08  achim
166  * New event_string only valid for the RP controller family
167  *
168  * Revision 1.34  2000/03/02 14:55:29  achim
169  * New mechanism for async. event handling implemented
170  *
171  * Revision 1.33  2000/02/21 15:37:37  achim
172  * Bugfix Alpha platform + DPMEM above 4GB
173  *
174  * Revision 1.32  2000/02/14 16:17:37  achim
175  * Bugfix sense_buffer[] + raw devices
176  *
177  * Revision 1.31  2000/02/10 10:29:00  achim
178  * Delete sense_buffer[0], if command OK
179  *
180  * Revision 1.30  1999/11/02 13:42:39  achim
181  * ARRAY_DRV_LIST2 implemented
182  * Now 255 log. and 100 host drives supported
183  *
184  * Revision 1.29  1999/10/05 13:28:47  achim
185  * GDT_CLUST_RESET added
186  *
187  * Revision 1.28  1999/08/12 13:44:54  achim
188  * MOUNTALL removed
189  * Cluster drives -> removeable drives
190  *
191  * Revision 1.27  1999/06/22 07:22:38  achim
192  * Small changes
193  *
194  * Revision 1.26  1999/06/10 16:09:12  achim
195  * Cluster Host Drive support: Bugfixes
196  *
197  * Revision 1.25  1999/06/01 16:03:56  achim
198  * gdth_init_pci(): Manipulate config. space to start RP controller
199  *
200  * Revision 1.24  1999/05/26 11:53:06  achim
201  * Cluster Host Drive support added
202  *
203  * Revision 1.23  1999/03/26 09:12:31  achim
204  * Default value for hdr_channel set to 0
205  *
206  * Revision 1.22  1999/03/22 16:27:16  achim
207  * Bugfix: gdth_store_event() must not be locked with GDTH_LOCK_HA()
208  *
209  * Revision 1.21  1999/03/16 13:40:34  achim
210  * Problems with reserved drives solved
211  * gdth_eh_bus_reset() implemented
212  *
213  * Revision 1.20  1999/03/10 09:08:13  achim
214  * Bugfix: Corrections in gdth_direction_tab[] made
215  * Bugfix: Increase command timeout (gdth_update_timeout()) NOT in gdth_putq()
216  *
217  * Revision 1.19  1999/03/05 14:38:16  achim
218  * Bugfix: Heads/Sectors mapping for reserved devices possibly wrong
219  * -> gdth_eval_mapping() implemented, changes in gdth_bios_param()
220  * INIT_RETRIES set to 100s to avoid DEINIT-Timeout for controllers
221  * with BIOS disabled and memory test set to Intensive
222  * Enhanced /proc support
223  *
224  * Revision 1.18  1999/02/24 09:54:33  achim
225  * Command line parameter hdr_channel implemented
226  * Bugfix for EISA controllers + Linux 2.2.x
227  *
228  * Revision 1.17  1998/12/17 15:58:11  achim
229  * Command line parameters implemented
230  * Changes for Alpha platforms
231  * PCI controller scan changed
232  * SMP support improved (spin_lock_irqsave(),...)
233  * New async. events, new scan/reserve commands included
234  *
235  * Revision 1.16  1998/09/28 16:08:46  achim
236  * GDT_PCIMPR: DPMEM remapping, if required
237  * mdelay() added
238  *
239  * Revision 1.15  1998/06/03 14:54:06  achim
240  * gdth_delay(), gdth_flush() implemented
241  * Bugfix: gdth_release() changed
242  *
243  * Revision 1.14  1998/05/22 10:01:17  achim
244  * mj: pcibios_strerror() removed
245  * Improved SMP support (if version >= 2.1.95)
246  * gdth_halt(): halt_called flag added (if version < 2.1)
247  *
248  * Revision 1.13  1998/04/16 09:14:57  achim
249  * Reserve drives (for raw service) implemented
250  * New error handling code enabled
251  * Get controller name from board_info() IOCTL
252  * Final round of PCI device driver patches by Martin Mares
253  *
254  * Revision 1.12  1998/03/03 09:32:37  achim
255  * Fibre channel controller support added
256  *
257  * Revision 1.11  1998/01/27 16:19:14  achim
258  * SA_SHIRQ added
259  * add_timer()/del_timer() instead of GDTH_TIMER
260  * scsi_add_timer()/scsi_del_timer() instead of SCSI_TIMER
261  * New error handling included
262  *
263  * Revision 1.10  1997/10/31 12:29:57  achim
264  * Read heads/sectors from host drive
265  *
266  * Revision 1.9  1997/09/04 10:07:25  achim
267  * IO-mapping with virt_to_bus(), gdth_readb(), gdth_writeb(), ...
268  * register_reboot_notifier() to get a notify on shutown used
269  *
270  * Revision 1.8  1997/04/02 12:14:30  achim
271  * Version 1.00 (see gdth.h), tested with kernel 2.0.29
272  *
273  * Revision 1.7  1997/03/12 13:33:37  achim
274  * gdth_reset() changed, new async. events
275  *
276  * Revision 1.6  1997/03/04 14:01:11  achim
277  * Shutdown routine gdth_halt() implemented
278  *
279  * Revision 1.5  1997/02/21 09:08:36  achim
280  * New controller included (RP, RP1, RP2 series)
281  * IOCTL interface implemented
282  *
283  * Revision 1.4  1996/07/05 12:48:55  achim
284  * Function gdth_bios_param() implemented
285  * New constant GDTH_MAXC_P_L inserted
286  * GDT_WRITE_THR, GDT_EXT_INFO implemented
287  * Function gdth_reset() changed
288  *
289  * Revision 1.3  1996/05/10 09:04:41  achim
290  * Small changes for Linux 1.2.13
291  *
292  * Revision 1.2  1996/05/09 12:45:27  achim
293  * Loadable module support implemented
294  * /proc support corrections made
295  *
296  * Revision 1.1  1996/04/11 07:35:57  achim
297  * Initial revision
298  *
299  ************************************************************************/
300
301 /* All GDT Disk Array Controllers are fully supported by this driver.
302  * This includes the PCI/EISA/ISA SCSI Disk Array Controllers and the
303  * PCI Fibre Channel Disk Array Controllers. See gdth.h for a complete
304  * list of all controller types.
305  * 
306  * If you have one or more GDT3000/3020 EISA controllers with 
307  * controller BIOS disabled, you have to set the IRQ values with the 
308  * command line option "gdth=irq1,irq2,...", where the irq1,irq2,... are
309  * the IRQ values for the EISA controllers.
310  * 
311  * After the optional list of IRQ values, other possible 
312  * command line options are:
313  * disable:Y                    disable driver
314  * disable:N                    enable driver
315  * reserve_mode:0               reserve no drives for the raw service
316  * reserve_mode:1               reserve all not init., removable drives
317  * reserve_mode:2               reserve all not init. drives
318  * reserve_list:h,b,t,l,h,b,t,l,...     reserve particular drive(s) with 
319  *                              h- controller no., b- channel no., 
320  *                              t- target ID, l- LUN
321  * reverse_scan:Y               reverse scan order for PCI controllers         
322  * reverse_scan:N               scan PCI controllers like BIOS
323  * max_ids:x                    x - target ID count per channel (1..MAXID)
324  * rescan:Y                     rescan all channels/IDs 
325  * rescan:N                     use all devices found until now
326  * virt_ctr:Y                   map every channel to a virtual controller 
327  * virt_ctr:N                   use multi channel support 
328  * hdr_channel:x                x - number of virtual bus for host drives
329  * shared_access:Y              disable driver reserve/release protocol to 
330  *                              access a shared resource from several nodes, 
331  *                              appropiate controller firmware required
332  * shared_access:N              enable driver reserve/release protocol
333  * probe_eisa_isa:Y             scan for EISA/ISA controllers
334  * probe_eisa_isa:N             do not scan for EISA/ISA controllers
335  * force_dma32:Y                use only 32 bit DMA mode
336  * force_dma32:N                use 64 bit DMA mode, if supported
337  *
338  * The default values are: "gdth=disable:N,reserve_mode:1,reverse_scan:N,
339  *                          max_ids:127,rescan:N,virt_ctr:N,hdr_channel:0,
340  *                          shared_access:Y,probe_eisa_isa:N,force_dma32:N".
341  * Here is another example: "gdth=reserve_list:0,1,2,0,0,1,3,0,rescan:Y".
342  * 
343  * When loading the gdth driver as a module, the same options are available. 
344  * You can set the IRQs with "IRQ=...". However, the syntax to specify the
345  * options changes slightly. You must replace all ',' between options 
346  * with ' ' and all ':' with '=' and you must use 
347  * '1' in place of 'Y' and '0' in place of 'N'.
348  * 
349  * Default: "modprobe gdth disable=0 reserve_mode=1 reverse_scan=0
350  *           max_ids=127 rescan=0 virt_ctr=0 hdr_channel=0 shared_access=0 
351  *           probe_eisa_isa=0 force_dma32=0"
352  * The other example: "modprobe gdth reserve_list=0,1,2,0,0,1,3,0 rescan=1".
353  */
354
355 /* The meaning of the Scsi_Pointer members in this driver is as follows:
356  * ptr:                     Chaining
357  * this_residual:           Command priority
358  * buffer:                  phys. DMA sense buffer 
359  * dma_handle:              phys. DMA buffer (kernel >= 2.4.0)
360  * buffers_residual:        Timeout value
361  * Status:                  Command status (gdth_do_cmd()), DMA mem. mappings
362  * Message:                 Additional info (gdth_do_cmd()), DMA direction
363  * have_data_in:            Flag for gdth_wait_completion()
364  * sent_command:            Opcode special command
365  * phase:                   Service/parameter/return code special command
366  */
367
368
369 /* interrupt coalescing */
370 /* #define INT_COAL */
371
372 /* statistics */
373 #define GDTH_STATISTICS
374
375 #include <linux/module.h>
376
377 #include <linux/version.h>
378 #include <linux/kernel.h>
379 #include <linux/types.h>
380 #include <linux/pci.h>
381 #include <linux/string.h>
382 #include <linux/ctype.h>
383 #include <linux/ioport.h>
384 #include <linux/delay.h>
385 #include <linux/sched.h>
386 #include <linux/interrupt.h>
387 #include <linux/in.h>
388 #include <linux/proc_fs.h>
389 #include <linux/time.h>
390 #include <linux/timer.h>
391 #ifdef GDTH_RTC
392 #include <linux/mc146818rtc.h>
393 #endif
394 #include <linux/reboot.h>
395
396 #include <asm/dma.h>
397 #include <asm/system.h>
398 #include <asm/io.h>
399 #include <asm/uaccess.h>
400 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
401 #include <linux/spinlock.h>
402 #else
403 #include <asm/spinlock.h>
404 #endif
405 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
406 #include <linux/blkdev.h>
407 #else
408 #include <linux/blk.h>
409 #include "sd.h"
410 #endif
411
412 #include "scsi.h"
413 #include <scsi/scsi_host.h>
414 #include "gdth.h"
415
416 static void gdth_delay(int milliseconds);
417 static void gdth_eval_mapping(ulong32 size, ulong32 *cyls, int *heads, int *secs);
418 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
419 static irqreturn_t gdth_interrupt(int irq, void *dev_id, struct pt_regs *regs);
420 #else
421 static void gdth_interrupt(int irq, void *dev_id, struct pt_regs *regs);
422 #endif
423 static int gdth_sync_event(int hanum,int service,unchar index,Scsi_Cmnd *scp);
424 static int gdth_async_event(int hanum);
425 static void gdth_log_event(gdth_evt_data *dvr, char *buffer);
426
427 static void gdth_putq(int hanum,Scsi_Cmnd *scp,unchar priority);
428 static void gdth_next(int hanum);
429 static int gdth_fill_raw_cmd(int hanum,Scsi_Cmnd *scp,unchar b);
430 static int gdth_special_cmd(int hanum,Scsi_Cmnd *scp);
431 static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, ushort source,
432                                       ushort idx, gdth_evt_data *evt);
433 static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr);
434 static void gdth_readapp_event(gdth_ha_str *ha, unchar application, 
435                                gdth_evt_str *estr);
436 static void gdth_clear_events(void);
437
438 static void gdth_copy_internal_data(int hanum,Scsi_Cmnd *scp,
439                                     char *buffer,ushort count);
440 static int gdth_internal_cache_cmd(int hanum,Scsi_Cmnd *scp);
441 static int gdth_fill_cache_cmd(int hanum,Scsi_Cmnd *scp,ushort hdrive);
442
443 static int gdth_search_eisa(ushort eisa_adr);
444 static int gdth_search_isa(ulong32 bios_adr);
445 static int gdth_search_pci(gdth_pci_str *pcistr);
446 static void gdth_search_dev(gdth_pci_str *pcistr, ushort *cnt, 
447                             ushort vendor, ushort dev);
448 static void gdth_sort_pci(gdth_pci_str *pcistr, int cnt);
449 static int gdth_init_eisa(ushort eisa_adr,gdth_ha_str *ha);
450 static int gdth_init_isa(ulong32 bios_adr,gdth_ha_str *ha);
451 static int gdth_init_pci(gdth_pci_str *pcistr,gdth_ha_str *ha);
452
453 static void gdth_enable_int(int hanum);
454 static int gdth_get_status(unchar *pIStatus,int irq);
455 static int gdth_test_busy(int hanum);
456 static int gdth_get_cmd_index(int hanum);
457 static void gdth_release_event(int hanum);
458 static int gdth_wait(int hanum,int index,ulong32 time);
459 static int gdth_internal_cmd(int hanum,unchar service,ushort opcode,ulong32 p1,
460                              ulong64 p2,ulong64 p3);
461 static int gdth_search_drives(int hanum);
462 static int gdth_analyse_hdrive(int hanum, ushort hdrive);
463
464 static const char *gdth_ctr_name(int hanum);
465
466 static int gdth_open(struct inode *inode, struct file *filep);
467 static int gdth_close(struct inode *inode, struct file *filep);
468 static int gdth_ioctl(struct inode *inode, struct file *filep,
469                       unsigned int cmd, unsigned long arg);
470
471 static void gdth_flush(int hanum);
472 static int gdth_halt(struct notifier_block *nb, ulong event, void *buf);
473
474 #ifdef DEBUG_GDTH
475 static unchar   DebugState = DEBUG_GDTH;
476
477 #ifdef __SERIAL__
478 #define MAX_SERBUF 160
479 static void ser_init(void);
480 static void ser_puts(char *str);
481 static void ser_putc(char c);
482 static int  ser_printk(const char *fmt, ...);
483 static char strbuf[MAX_SERBUF+1];
484 #ifdef __COM2__
485 #define COM_BASE 0x2f8
486 #else
487 #define COM_BASE 0x3f8
488 #endif
489 static void ser_init()
490 {
491     unsigned port=COM_BASE;
492
493     outb(0x80,port+3);
494     outb(0,port+1);
495     /* 19200 Baud, if 9600: outb(12,port) */
496     outb(6, port);
497     outb(3,port+3);
498     outb(0,port+1);
499     /*
500     ser_putc('I');
501     ser_putc(' ');
502     */
503 }
504
505 static void ser_puts(char *str)
506 {
507     char *ptr;
508
509     ser_init();
510     for (ptr=str;*ptr;++ptr)
511         ser_putc(*ptr);
512 }
513
514 static void ser_putc(char c)
515 {
516     unsigned port=COM_BASE;
517
518     while ((inb(port+5) & 0x20)==0);
519     outb(c,port);
520     if (c==0x0a)
521     {
522         while ((inb(port+5) & 0x20)==0);
523         outb(0x0d,port);
524     }
525 }
526
527 static int ser_printk(const char *fmt, ...)
528 {
529     va_list args;
530     int i;
531
532     va_start(args,fmt);
533     i = vsprintf(strbuf,fmt,args);
534     ser_puts(strbuf);
535     va_end(args);
536     return i;
537 }
538
539 #define TRACE(a)    {if (DebugState==1) {ser_printk a;}}
540 #define TRACE2(a)   {if (DebugState==1 || DebugState==2) {ser_printk a;}}
541 #define TRACE3(a)   {if (DebugState!=0) {ser_printk a;}}
542
543 #else /* !__SERIAL__ */
544 #define TRACE(a)    {if (DebugState==1) {printk a;}}
545 #define TRACE2(a)   {if (DebugState==1 || DebugState==2) {printk a;}}
546 #define TRACE3(a)   {if (DebugState!=0) {printk a;}}
547 #endif
548
549 #else /* !DEBUG */
550 #define TRACE(a)
551 #define TRACE2(a)
552 #define TRACE3(a)
553 #endif
554
555 #ifdef GDTH_STATISTICS
556 static ulong32 max_rq=0, max_index=0, max_sg=0;
557 #ifdef INT_COAL
558 static ulong32 max_int_coal=0;
559 #endif
560 static ulong32 act_ints=0, act_ios=0, act_stats=0, act_rq=0;
561 static struct timer_list gdth_timer;
562 #endif
563
564 #define PTR2USHORT(a)   (ushort)(ulong)(a)
565 #define GDTOFFSOF(a,b)  (size_t)&(((a*)0)->b)   
566 #define INDEX_OK(i,t)   ((i)<sizeof(t)/sizeof((t)[0]))
567
568 #define NUMDATA(a)      ( (gdth_num_str  *)((a)->hostdata))
569 #define HADATA(a)       (&((gdth_ext_str *)((a)->hostdata))->haext)
570 #define CMDDATA(a)      (&((gdth_ext_str *)((a)->hostdata))->cmdext)
571
572 #define BUS_L2P(a,b)    ((b)>(a)->virt_bus ? (b-1):(b))
573
574 #define gdth_readb(addr)        readb(addr)
575 #define gdth_readw(addr)        readw(addr)
576 #define gdth_readl(addr)        readl(addr)
577 #define gdth_writeb(b,addr)     writeb((b),(addr))
578 #define gdth_writew(b,addr)     writew((b),(addr))
579 #define gdth_writel(b,addr)     writel((b),(addr))
580
581 static unchar   gdth_drq_tab[4] = {5,6,7,7};            /* DRQ table */
582 static unchar   gdth_irq_tab[6] = {0,10,11,12,14,0};    /* IRQ table */
583 static unchar   gdth_polling;                           /* polling if TRUE */
584 static unchar   gdth_from_wait  = FALSE;                /* gdth_wait() */
585 static int      wait_index,wait_hanum;                  /* gdth_wait() */
586 static int      gdth_ctr_count  = 0;                    /* controller count */
587 static int      gdth_ctr_vcount = 0;                    /* virt. ctr. count */
588 static int      gdth_ctr_released = 0;                  /* gdth_release() */
589 static struct Scsi_Host *gdth_ctr_tab[MAXHA];           /* controller table */
590 static struct Scsi_Host *gdth_ctr_vtab[MAXHA*MAXBUS];   /* virt. ctr. table */
591 static unchar   gdth_write_through = FALSE;             /* write through */
592 static gdth_evt_str ebuffer[MAX_EVENTS];                /* event buffer */
593 static int elastidx;
594 static int eoldidx;
595 static int major;
596
597 #define DIN     1                               /* IN data direction */
598 #define DOU     2                               /* OUT data direction */
599 #define DNO     DIN                             /* no data transfer */
600 #define DUN     DIN                             /* unknown data direction */
601 static unchar gdth_direction_tab[0x100] = {
602     DNO,DNO,DIN,DIN,DOU,DIN,DIN,DOU,DIN,DUN,DOU,DOU,DUN,DUN,DUN,DIN,
603     DNO,DIN,DIN,DOU,DIN,DOU,DNO,DNO,DOU,DNO,DIN,DNO,DIN,DOU,DNO,DUN,
604     DIN,DUN,DIN,DUN,DOU,DIN,DUN,DUN,DIN,DIN,DOU,DNO,DUN,DIN,DOU,DOU,
605     DOU,DOU,DOU,DNO,DIN,DNO,DNO,DIN,DOU,DOU,DOU,DOU,DIN,DOU,DIN,DOU,
606     DOU,DOU,DIN,DIN,DIN,DNO,DUN,DNO,DNO,DNO,DUN,DNO,DOU,DIN,DUN,DUN,
607     DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DIN,DUN,DUN,DUN,DUN,DUN,
608     DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
609     DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
610     DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
611     DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DIN,DUN,
612     DUN,DUN,DUN,DUN,DUN,DNO,DNO,DUN,DIN,DNO,DOU,DUN,DNO,DUN,DOU,DOU,
613     DOU,DOU,DOU,DNO,DUN,DIN,DOU,DIN,DIN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
614     DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
615     DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
616     DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
617     DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN
618 };
619
620 /* __initfunc, __initdata macros */
621 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
622 #define GDTH_INITFUNC(type, func)       type __init func 
623 #include <linux/init.h>
624 #else
625 #define GDTH_INITFUNC(type, func)       __initfunc(type func)
626 #include <linux/init.h>
627 #endif
628
629 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
630 #define GDTH_INIT_LOCK_HA(ha)           spin_lock_init(&(ha)->smp_lock)
631 #define GDTH_LOCK_HA(ha,flags)          spin_lock_irqsave(&(ha)->smp_lock,flags)
632 #define GDTH_UNLOCK_HA(ha,flags)        spin_unlock_irqrestore(&(ha)->smp_lock,flags)
633
634 #define GDTH_LOCK_SCSI_DONE(dev, flags) spin_lock_irqsave(dev->host_lock,flags)
635 #define GDTH_UNLOCK_SCSI_DONE(dev, flags) spin_unlock_irqrestore(dev->host_lock,flags)
636
637 #else
638 #define GDTH_INIT_LOCK_HA(ha)           spin_lock_init(&(ha)->smp_lock)
639 #define GDTH_LOCK_HA(ha,flags)          spin_lock_irqsave(&(ha)->smp_lock,flags)
640 #define GDTH_UNLOCK_HA(ha,flags)        spin_unlock_irqrestore(&(ha)->smp_lock,flags)
641
642 #define GDTH_LOCK_SCSI_DONE(flags)      spin_lock_irqsave(&io_request_lock,flags)
643 #define GDTH_UNLOCK_SCSI_DONE(flags)    spin_unlock_irqrestore(&io_request_lock,flags)
644 #endif
645
646 /* LILO and modprobe/insmod parameters */
647 /* IRQ list for GDT3000/3020 EISA controllers */
648 static int irq[MAXHA] __initdata = 
649 {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
650  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
651 /* disable driver flag */
652 static int disable __initdata = 0;
653 /* reserve flag */
654 static int reserve_mode = 1;                  
655 /* reserve list */
656 static int reserve_list[MAX_RES_ARGS] = 
657 {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
658  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
659  0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
660 /* scan order for PCI controllers */
661 static int reverse_scan = 0;
662 /* virtual channel for the host drives */
663 static int hdr_channel = 0;
664 /* max. IDs per channel */
665 static int max_ids = MAXID;
666 /* rescan all IDs */
667 static int rescan = 0;
668 /* map channels to virtual controllers */
669 static int virt_ctr = 0;
670 /* shared access */
671 static int shared_access = 1;
672 /* enable support for EISA and ISA controllers */
673 static int probe_eisa_isa = 0;
674 /* 64 bit DMA mode, support for drives > 2 TB, if force_dma32 = 0 */
675 static int force_dma32 = 0;
676
677 #ifdef MODULE
678 /* parameters for modprobe/insmod */
679 MODULE_PARM(irq, "i");
680 MODULE_PARM(disable, "i");
681 MODULE_PARM(reserve_mode, "i");
682 MODULE_PARM(reserve_list, "4-" __MODULE_STRING(MAX_RES_ARGS) "i");
683 MODULE_PARM(reverse_scan, "i");
684 MODULE_PARM(hdr_channel, "i");
685 MODULE_PARM(max_ids, "i");
686 MODULE_PARM(rescan, "i");
687 MODULE_PARM(virt_ctr, "i");
688 MODULE_PARM(shared_access, "i");
689 MODULE_PARM(probe_eisa_isa, "i");
690 MODULE_PARM(force_dma32, "i");
691 MODULE_AUTHOR("Achim Leubner");
692 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,11)
693 MODULE_LICENSE("GPL");
694 MODULE_VERSION(GDTH_VERSION_STR);
695 #endif
696 #endif
697
698 /* ioctl interface */
699 static struct file_operations gdth_fops = {
700 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
701     .ioctl   = gdth_ioctl,
702     .open    = gdth_open,
703     .release = gdth_close,
704 #else
705     ioctl:gdth_ioctl,
706     open:gdth_open,
707     release:gdth_close,
708 #endif
709 };
710
711 /* /proc support */
712 #include <linux/stat.h> 
713 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
714 struct proc_dir_entry proc_scsi_gdth = {
715     PROC_SCSI_GDTH, 4, "gdth",
716     S_IFDIR | S_IRUGO | S_IXUGO, 2
717 };
718 #endif
719
720 #include "gdth_proc.h"
721 #include "gdth_proc.c"
722
723 /* notifier block to get a notify on system shutdown/halt/reboot */
724 static struct notifier_block gdth_notifier = {
725     gdth_halt, NULL, 0
726 };
727
728
729 static void gdth_delay(int milliseconds)
730 {
731     if (milliseconds == 0) {
732         udelay(1);
733     } else {
734         mdelay(milliseconds);
735     }
736 }
737
738 static void gdth_eval_mapping(ulong32 size, ulong32 *cyls, int *heads, int *secs)
739 {
740     *cyls = size /HEADS/SECS;
741     if (*cyls <= MAXCYLS) {
742         *heads = HEADS;
743         *secs = SECS;
744     } else {                                        /* too high for 64*32 */
745         *cyls = size /MEDHEADS/MEDSECS;
746         if (*cyls <= MAXCYLS) {
747             *heads = MEDHEADS;
748             *secs = MEDSECS;
749         } else {                                    /* too high for 127*63 */
750             *cyls = size /BIGHEADS/BIGSECS;
751             *heads = BIGHEADS;
752             *secs = BIGSECS;
753         }
754     }
755 }
756
757 /* controller search and initialization functions */
758
759 GDTH_INITFUNC(static int, gdth_search_eisa(ushort eisa_adr))
760 {
761     ulong32 id;
762     
763     TRACE(("gdth_search_eisa() adr. %x\n",eisa_adr));
764     id = inl(eisa_adr+ID0REG);
765     if (id == GDT3A_ID || id == GDT3B_ID) {     /* GDT3000A or GDT3000B */
766         if ((inb(eisa_adr+EISAREG) & 8) == 0)   
767             return 0;                           /* not EISA configured */
768         return 1;
769     }
770     if (id == GDT3_ID)                          /* GDT3000 */
771         return 1;
772
773     return 0;                                   
774 }
775
776
777 GDTH_INITFUNC(static int, gdth_search_isa(ulong32 bios_adr))
778 {
779     void __iomem *addr;
780     ulong32 id;
781
782     TRACE(("gdth_search_isa() bios adr. %x\n",bios_adr));
783     if ((addr = ioremap(bios_adr+BIOS_ID_OFFS, sizeof(ulong32))) != NULL) {
784         id = gdth_readl(addr);
785         iounmap(addr);
786         if (id == GDT2_ID)                          /* GDT2000 */
787             return 1;
788     }
789     return 0;
790 }
791
792
793 GDTH_INITFUNC(static int, gdth_search_pci(gdth_pci_str *pcistr))
794 {
795     ushort device, cnt;
796     
797     TRACE(("gdth_search_pci()\n"));
798
799     cnt = 0;
800     for (device = 0; device <= PCI_DEVICE_ID_VORTEX_GDT6555; ++device)
801         gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX, device);
802     for (device = PCI_DEVICE_ID_VORTEX_GDT6x17RP; 
803          device <= PCI_DEVICE_ID_VORTEX_GDTMAXRP; ++device)
804         gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX, device);
805     gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX, 
806                     PCI_DEVICE_ID_VORTEX_GDTNEWRX);
807     gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX, 
808                     PCI_DEVICE_ID_VORTEX_GDTNEWRX2);
809     gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_INTEL,
810                     PCI_DEVICE_ID_INTEL_SRC);
811     gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_INTEL,
812                     PCI_DEVICE_ID_INTEL_SRC_XSCALE);
813     return cnt;
814 }
815
816 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
817 /* Vortex only makes RAID controllers.
818  * We do not really want to specify all 550 ids here, so wildcard match.
819  */
820 static struct pci_device_id gdthtable[] __devinitdata = {
821     {PCI_VENDOR_ID_VORTEX,PCI_ANY_ID,PCI_ANY_ID, PCI_ANY_ID},
822     {PCI_VENDOR_ID_INTEL,PCI_DEVICE_ID_INTEL_SRC,PCI_ANY_ID,PCI_ANY_ID}, 
823     {PCI_VENDOR_ID_INTEL,PCI_DEVICE_ID_INTEL_SRC_XSCALE,PCI_ANY_ID,PCI_ANY_ID}, 
824     {0}
825 };
826 MODULE_DEVICE_TABLE(pci,gdthtable);
827 #endif
828
829 GDTH_INITFUNC(static void, gdth_search_dev(gdth_pci_str *pcistr, ushort *cnt,
830                                            ushort vendor, ushort device))
831 {
832     ulong base0, base1, base2;
833     struct pci_dev *pdev;
834     
835     TRACE(("gdth_search_dev() cnt %d vendor %x device %x\n",
836           *cnt, vendor, device));
837
838 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
839     pdev = NULL;
840     while ((pdev = pci_find_device(vendor, device, pdev)) 
841            != NULL) {
842         if (pci_enable_device(pdev))
843             continue;
844         if (*cnt >= MAXHA)
845             return;
846         /* GDT PCI controller found, resources are already in pdev */
847         pcistr[*cnt].pdev = pdev;
848         pcistr[*cnt].vendor_id = vendor;
849         pcistr[*cnt].device_id = device;
850         pcistr[*cnt].subdevice_id = pdev->subsystem_device;
851         pcistr[*cnt].bus = pdev->bus->number;
852         pcistr[*cnt].device_fn = pdev->devfn;
853         pcistr[*cnt].irq = pdev->irq;
854         base0 = pci_resource_flags(pdev, 0);
855         base1 = pci_resource_flags(pdev, 1);
856         base2 = pci_resource_flags(pdev, 2);
857         if (device <= PCI_DEVICE_ID_VORTEX_GDT6000B ||   /* GDT6000/B */
858             device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) {  /* MPR */
859             if (!(base0 & IORESOURCE_MEM)) 
860                 continue;
861             pcistr[*cnt].dpmem = pci_resource_start(pdev, 0);
862         } else {                                  /* GDT6110, GDT6120, .. */
863             if (!(base0 & IORESOURCE_MEM) ||
864                 !(base2 & IORESOURCE_MEM) ||
865                 !(base1 & IORESOURCE_IO)) 
866                 continue;
867             pcistr[*cnt].dpmem = pci_resource_start(pdev, 2);
868             pcistr[*cnt].io_mm = pci_resource_start(pdev, 0);
869             pcistr[*cnt].io    = pci_resource_start(pdev, 1);
870         }
871         TRACE2(("Controller found at %d/%d, irq %d, dpmem 0x%lx\n",
872                 pcistr[*cnt].bus, PCI_SLOT(pcistr[*cnt].device_fn), 
873                 pcistr[*cnt].irq, pcistr[*cnt].dpmem));
874         (*cnt)++;
875     }       
876 #else
877     pdev = NULL;
878     while ((pdev = pci_find_device(vendor, device, pdev)) 
879            != NULL) {
880         if (*cnt >= MAXHA)
881             return;
882         /* GDT PCI controller found, resources are already in pdev */
883         pcistr[*cnt].pdev = pdev;
884         pcistr[*cnt].vendor_id = vendor;
885         pcistr[*cnt].device_id = device;
886         pcistr[*cnt].bus = pdev->bus->number;
887         pcistr[*cnt].device_fn = pdev->devfn;
888         pcibios_read_config_word(pcistr[*cnt].bus, pcistr[*cnt].device_fn,
889                                  PCI_SUBSYSTEM_ID, &pcistr[*cnt].subdevice_id);
890         pcistr[*cnt].irq = pdev->irq;
891         base0 = pdev->base_address[0];
892         base1 = pdev->base_address[1];
893         base2 = pdev->base_address[2];
894         if (device <= PCI_DEVICE_ID_VORTEX_GDT6000B ||   /* GDT6000/B */
895             device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) {  /* MPR */
896             if ((base0 & PCI_BASE_ADDRESS_SPACE) != 
897                 PCI_BASE_ADDRESS_SPACE_MEMORY)
898                 continue;
899             pcistr[*cnt].dpmem = base0 & PCI_BASE_ADDRESS_MEM_MASK;
900         } else {                                  /* GDT6110, GDT6120, .. */
901             if ((base0 & PCI_BASE_ADDRESS_SPACE) !=
902                 PCI_BASE_ADDRESS_SPACE_MEMORY ||
903                 (base2 & PCI_BASE_ADDRESS_SPACE) !=
904                 PCI_BASE_ADDRESS_SPACE_MEMORY ||
905                 (base1 & PCI_BASE_ADDRESS_SPACE) !=
906                 PCI_BASE_ADDRESS_SPACE_IO)
907                 continue;
908             pcistr[*cnt].dpmem = base2 & PCI_BASE_ADDRESS_MEM_MASK;
909             pcistr[*cnt].io_mm = base0 & PCI_BASE_ADDRESS_MEM_MASK;
910             pcistr[*cnt].io    = base1 & PCI_BASE_ADDRESS_IO_MASK;
911         }
912         TRACE2(("Controller found at %d/%d, irq %d, dpmem 0x%lx\n",
913                 pcistr[*cnt].bus, PCI_SLOT(pcistr[*cnt].device_fn), 
914                 pcistr[*cnt].irq, pcistr[*cnt].dpmem));
915         (*cnt)++;
916     }       
917 #endif
918 }   
919
920
921 GDTH_INITFUNC(static void, gdth_sort_pci(gdth_pci_str *pcistr, int cnt))
922 {    
923     gdth_pci_str temp;
924     int i, changed;
925     
926     TRACE(("gdth_sort_pci() cnt %d\n",cnt));
927     if (cnt == 0)
928         return;
929
930     do {
931         changed = FALSE;
932         for (i = 0; i < cnt-1; ++i) {
933             if (!reverse_scan) {
934                 if ((pcistr[i].bus > pcistr[i+1].bus) ||
935                     (pcistr[i].bus == pcistr[i+1].bus &&
936                      PCI_SLOT(pcistr[i].device_fn) > 
937                      PCI_SLOT(pcistr[i+1].device_fn))) {
938                     temp = pcistr[i];
939                     pcistr[i] = pcistr[i+1];
940                     pcistr[i+1] = temp;
941                     changed = TRUE;
942                 }
943             } else {
944                 if ((pcistr[i].bus < pcistr[i+1].bus) ||
945                     (pcistr[i].bus == pcistr[i+1].bus &&
946                      PCI_SLOT(pcistr[i].device_fn) < 
947                      PCI_SLOT(pcistr[i+1].device_fn))) {
948                     temp = pcistr[i];
949                     pcistr[i] = pcistr[i+1];
950                     pcistr[i+1] = temp;
951                     changed = TRUE;
952                 }
953             }
954         }
955     } while (changed);
956 }
957
958
959 GDTH_INITFUNC(static int, gdth_init_eisa(ushort eisa_adr,gdth_ha_str *ha))
960 {
961     ulong32 retries,id;
962     unchar prot_ver,eisacf,i,irq_found;
963
964     TRACE(("gdth_init_eisa() adr. %x\n",eisa_adr));
965     
966     /* disable board interrupts, deinitialize services */
967     outb(0xff,eisa_adr+EDOORREG);
968     outb(0x00,eisa_adr+EDENABREG);
969     outb(0x00,eisa_adr+EINTENABREG);
970     
971     outb(0xff,eisa_adr+LDOORREG);
972     retries = INIT_RETRIES;
973     gdth_delay(20);
974     while (inb(eisa_adr+EDOORREG) != 0xff) {
975         if (--retries == 0) {
976             printk("GDT-EISA: Initialization error (DEINIT failed)\n");
977             return 0;
978         }
979         gdth_delay(1);
980         TRACE2(("wait for DEINIT: retries=%d\n",retries));
981     }
982     prot_ver = inb(eisa_adr+MAILBOXREG);
983     outb(0xff,eisa_adr+EDOORREG);
984     if (prot_ver != PROTOCOL_VERSION) {
985         printk("GDT-EISA: Illegal protocol version\n");
986         return 0;
987     }
988     ha->bmic = eisa_adr;
989     ha->brd_phys = (ulong32)eisa_adr >> 12;
990
991     outl(0,eisa_adr+MAILBOXREG);
992     outl(0,eisa_adr+MAILBOXREG+4);
993     outl(0,eisa_adr+MAILBOXREG+8);
994     outl(0,eisa_adr+MAILBOXREG+12);
995
996     /* detect IRQ */ 
997     if ((id = inl(eisa_adr+ID0REG)) == GDT3_ID) {
998         ha->oem_id = OEM_ID_ICP;
999         ha->type = GDT_EISA;
1000         ha->stype = id;
1001         outl(1,eisa_adr+MAILBOXREG+8);
1002         outb(0xfe,eisa_adr+LDOORREG);
1003         retries = INIT_RETRIES;
1004         gdth_delay(20);
1005         while (inb(eisa_adr+EDOORREG) != 0xfe) {
1006             if (--retries == 0) {
1007                 printk("GDT-EISA: Initialization error (get IRQ failed)\n");
1008                 return 0;
1009             }
1010             gdth_delay(1);
1011         }
1012         ha->irq = inb(eisa_adr+MAILBOXREG);
1013         outb(0xff,eisa_adr+EDOORREG);
1014         TRACE2(("GDT3000/3020: IRQ=%d\n",ha->irq));
1015         /* check the result */
1016         if (ha->irq == 0) {
1017                 TRACE2(("Unknown IRQ, use IRQ table from cmd line !\n"));
1018                 for (i = 0, irq_found = FALSE; 
1019                      i < MAXHA && irq[i] != 0xff; ++i) {
1020                 if (irq[i]==10 || irq[i]==11 || irq[i]==12 || irq[i]==14) {
1021                     irq_found = TRUE;
1022                     break;
1023                 }
1024                 }
1025             if (irq_found) {
1026                 ha->irq = irq[i];
1027                 irq[i] = 0;
1028                 printk("GDT-EISA: Can not detect controller IRQ,\n");
1029                 printk("Use IRQ setting from command line (IRQ = %d)\n",
1030                        ha->irq);
1031             } else {
1032                 printk("GDT-EISA: Initialization error (unknown IRQ), Enable\n");
1033                 printk("the controller BIOS or use command line parameters\n");
1034                 return 0;
1035             }
1036         }
1037     } else {
1038         eisacf = inb(eisa_adr+EISAREG) & 7;
1039         if (eisacf > 4)                         /* level triggered */
1040             eisacf -= 4;
1041         ha->irq = gdth_irq_tab[eisacf];
1042         ha->oem_id = OEM_ID_ICP;
1043         ha->type = GDT_EISA;
1044         ha->stype = id;
1045     }
1046
1047     ha->dma64_support = 0;
1048     return 1;
1049 }
1050
1051        
1052 GDTH_INITFUNC(static int, gdth_init_isa(ulong32 bios_adr,gdth_ha_str *ha))
1053 {
1054     register gdt2_dpram_str __iomem *dp2_ptr;
1055     int i;
1056     unchar irq_drq,prot_ver;
1057     ulong32 retries;
1058
1059     TRACE(("gdth_init_isa() bios adr. %x\n",bios_adr));
1060
1061     ha->brd = ioremap(bios_adr, sizeof(gdt2_dpram_str));
1062     if (ha->brd == NULL) {
1063         printk("GDT-ISA: Initialization error (DPMEM remap error)\n");
1064         return 0;
1065     }
1066     dp2_ptr = ha->brd;
1067     gdth_writeb(1, &dp2_ptr->io.memlock); /* switch off write protection */
1068     /* reset interface area */
1069     memset_io(&dp2_ptr->u, 0, sizeof(dp2_ptr->u));
1070     if (gdth_readl(&dp2_ptr->u) != 0) {
1071         printk("GDT-ISA: Initialization error (DPMEM write error)\n");
1072         iounmap(ha->brd);
1073         return 0;
1074     }
1075
1076     /* disable board interrupts, read DRQ and IRQ */
1077     gdth_writeb(0xff, &dp2_ptr->io.irqdel);
1078     gdth_writeb(0x00, &dp2_ptr->io.irqen);
1079     gdth_writeb(0x00, &dp2_ptr->u.ic.S_Status);
1080     gdth_writeb(0x00, &dp2_ptr->u.ic.Cmd_Index);
1081
1082     irq_drq = gdth_readb(&dp2_ptr->io.rq);
1083     for (i=0; i<3; ++i) {
1084         if ((irq_drq & 1)==0)
1085             break;
1086         irq_drq >>= 1;
1087     }
1088     ha->drq = gdth_drq_tab[i];
1089
1090     irq_drq = gdth_readb(&dp2_ptr->io.rq) >> 3;
1091     for (i=1; i<5; ++i) {
1092         if ((irq_drq & 1)==0)
1093             break;
1094         irq_drq >>= 1;
1095     }
1096     ha->irq = gdth_irq_tab[i];
1097
1098     /* deinitialize services */
1099     gdth_writel(bios_adr, &dp2_ptr->u.ic.S_Info[0]);
1100     gdth_writeb(0xff, &dp2_ptr->u.ic.S_Cmd_Indx);
1101     gdth_writeb(0, &dp2_ptr->io.event);
1102     retries = INIT_RETRIES;
1103     gdth_delay(20);
1104     while (gdth_readb(&dp2_ptr->u.ic.S_Status) != 0xff) {
1105         if (--retries == 0) {
1106             printk("GDT-ISA: Initialization error (DEINIT failed)\n");
1107             iounmap(ha->brd);
1108             return 0;
1109         }
1110         gdth_delay(1);
1111     }
1112     prot_ver = (unchar)gdth_readl(&dp2_ptr->u.ic.S_Info[0]);
1113     gdth_writeb(0, &dp2_ptr->u.ic.Status);
1114     gdth_writeb(0xff, &dp2_ptr->io.irqdel);
1115     if (prot_ver != PROTOCOL_VERSION) {
1116         printk("GDT-ISA: Illegal protocol version\n");
1117         iounmap(ha->brd);
1118         return 0;
1119     }
1120
1121     ha->oem_id = OEM_ID_ICP;
1122     ha->type = GDT_ISA;
1123     ha->ic_all_size = sizeof(dp2_ptr->u);
1124     ha->stype= GDT2_ID;
1125     ha->brd_phys = bios_adr >> 4;
1126
1127     /* special request to controller BIOS */
1128     gdth_writel(0x00, &dp2_ptr->u.ic.S_Info[0]);
1129     gdth_writel(0x00, &dp2_ptr->u.ic.S_Info[1]);
1130     gdth_writel(0x01, &dp2_ptr->u.ic.S_Info[2]);
1131     gdth_writel(0x00, &dp2_ptr->u.ic.S_Info[3]);
1132     gdth_writeb(0xfe, &dp2_ptr->u.ic.S_Cmd_Indx);
1133     gdth_writeb(0, &dp2_ptr->io.event);
1134     retries = INIT_RETRIES;
1135     gdth_delay(20);
1136     while (gdth_readb(&dp2_ptr->u.ic.S_Status) != 0xfe) {
1137         if (--retries == 0) {
1138             printk("GDT-ISA: Initialization error\n");
1139             iounmap(ha->brd);
1140             return 0;
1141         }
1142         gdth_delay(1);
1143     }
1144     gdth_writeb(0, &dp2_ptr->u.ic.Status);
1145     gdth_writeb(0xff, &dp2_ptr->io.irqdel);
1146
1147     ha->dma64_support = 0;
1148     return 1;
1149 }
1150
1151
1152 GDTH_INITFUNC(static int, gdth_init_pci(gdth_pci_str *pcistr,gdth_ha_str *ha))
1153 {
1154     register gdt6_dpram_str __iomem *dp6_ptr;
1155     register gdt6c_dpram_str __iomem *dp6c_ptr;
1156     register gdt6m_dpram_str __iomem *dp6m_ptr;
1157     ulong32 retries;
1158     unchar prot_ver;
1159     ushort command;
1160     int i, found = FALSE;
1161
1162     TRACE(("gdth_init_pci()\n"));
1163
1164     if (pcistr->vendor_id == PCI_VENDOR_ID_INTEL)
1165         ha->oem_id = OEM_ID_INTEL;
1166     else
1167         ha->oem_id = OEM_ID_ICP;
1168     ha->brd_phys = (pcistr->bus << 8) | (pcistr->device_fn & 0xf8);
1169     ha->stype = (ulong32)pcistr->device_id;
1170     ha->subdevice_id = pcistr->subdevice_id;
1171     ha->irq = pcistr->irq;
1172 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1173     ha->pdev = pcistr->pdev;
1174 #endif
1175     
1176     if (ha->stype <= PCI_DEVICE_ID_VORTEX_GDT6000B) {  /* GDT6000/B */
1177         TRACE2(("init_pci() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
1178         ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6_dpram_str));
1179         if (ha->brd == NULL) {
1180             printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1181             return 0;
1182         }
1183         /* check and reset interface area */
1184         dp6_ptr = ha->brd;
1185         gdth_writel(DPMEM_MAGIC, &dp6_ptr->u);
1186         if (gdth_readl(&dp6_ptr->u) != DPMEM_MAGIC) {
1187             printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n", 
1188                    pcistr->dpmem);
1189             found = FALSE;
1190             for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1191                 iounmap(ha->brd);
1192                 ha->brd = ioremap(i, sizeof(ushort)); 
1193                 if (ha->brd == NULL) {
1194                     printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1195                     return 0;
1196                 }
1197                 if (gdth_readw(ha->brd) != 0xffff) {
1198                     TRACE2(("init_pci_old() address 0x%x busy\n", i));
1199                     continue;
1200                 }
1201                 iounmap(ha->brd);
1202                 pci_write_config_dword(pcistr->pdev, 
1203                                        PCI_BASE_ADDRESS_0, i);
1204                 ha->brd = ioremap(i, sizeof(gdt6_dpram_str)); 
1205                 if (ha->brd == NULL) {
1206                     printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1207                     return 0;
1208                 }
1209                 dp6_ptr = ha->brd;
1210                 gdth_writel(DPMEM_MAGIC, &dp6_ptr->u);
1211                 if (gdth_readl(&dp6_ptr->u) == DPMEM_MAGIC) {
1212                     printk("GDT-PCI: Use free address at 0x%x\n", i);
1213                     found = TRUE;
1214                     break;
1215                 }
1216             }   
1217             if (!found) {
1218                 printk("GDT-PCI: No free address found!\n");
1219                 iounmap(ha->brd);
1220                 return 0;
1221             }
1222         }
1223         memset_io(&dp6_ptr->u, 0, sizeof(dp6_ptr->u));
1224         if (gdth_readl(&dp6_ptr->u) != 0) {
1225             printk("GDT-PCI: Initialization error (DPMEM write error)\n");
1226             iounmap(ha->brd);
1227             return 0;
1228         }
1229         
1230         /* disable board interrupts, deinit services */
1231         gdth_writeb(0xff, &dp6_ptr->io.irqdel);
1232         gdth_writeb(0x00, &dp6_ptr->io.irqen);
1233         gdth_writeb(0x00, &dp6_ptr->u.ic.S_Status);
1234         gdth_writeb(0x00, &dp6_ptr->u.ic.Cmd_Index);
1235
1236         gdth_writel(pcistr->dpmem, &dp6_ptr->u.ic.S_Info[0]);
1237         gdth_writeb(0xff, &dp6_ptr->u.ic.S_Cmd_Indx);
1238         gdth_writeb(0, &dp6_ptr->io.event);
1239         retries = INIT_RETRIES;
1240         gdth_delay(20);
1241         while (gdth_readb(&dp6_ptr->u.ic.S_Status) != 0xff) {
1242             if (--retries == 0) {
1243                 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1244                 iounmap(ha->brd);
1245                 return 0;
1246             }
1247             gdth_delay(1);
1248         }
1249         prot_ver = (unchar)gdth_readl(&dp6_ptr->u.ic.S_Info[0]);
1250         gdth_writeb(0, &dp6_ptr->u.ic.S_Status);
1251         gdth_writeb(0xff, &dp6_ptr->io.irqdel);
1252         if (prot_ver != PROTOCOL_VERSION) {
1253             printk("GDT-PCI: Illegal protocol version\n");
1254             iounmap(ha->brd);
1255             return 0;
1256         }
1257
1258         ha->type = GDT_PCI;
1259         ha->ic_all_size = sizeof(dp6_ptr->u);
1260         
1261         /* special command to controller BIOS */
1262         gdth_writel(0x00, &dp6_ptr->u.ic.S_Info[0]);
1263         gdth_writel(0x00, &dp6_ptr->u.ic.S_Info[1]);
1264         gdth_writel(0x00, &dp6_ptr->u.ic.S_Info[2]);
1265         gdth_writel(0x00, &dp6_ptr->u.ic.S_Info[3]);
1266         gdth_writeb(0xfe, &dp6_ptr->u.ic.S_Cmd_Indx);
1267         gdth_writeb(0, &dp6_ptr->io.event);
1268         retries = INIT_RETRIES;
1269         gdth_delay(20);
1270         while (gdth_readb(&dp6_ptr->u.ic.S_Status) != 0xfe) {
1271             if (--retries == 0) {
1272                 printk("GDT-PCI: Initialization error\n");
1273                 iounmap(ha->brd);
1274                 return 0;
1275             }
1276             gdth_delay(1);
1277         }
1278         gdth_writeb(0, &dp6_ptr->u.ic.S_Status);
1279         gdth_writeb(0xff, &dp6_ptr->io.irqdel);
1280
1281         ha->dma64_support = 0;
1282
1283     } else if (ha->stype <= PCI_DEVICE_ID_VORTEX_GDT6555) { /* GDT6110, ... */
1284         ha->plx = (gdt6c_plx_regs *)pcistr->io;
1285         TRACE2(("init_pci_new() dpmem %lx irq %d\n",
1286             pcistr->dpmem,ha->irq));
1287         ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6c_dpram_str));
1288         if (ha->brd == NULL) {
1289             printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1290             iounmap(ha->brd);
1291             return 0;
1292         }
1293         /* check and reset interface area */
1294         dp6c_ptr = ha->brd;
1295         gdth_writel(DPMEM_MAGIC, &dp6c_ptr->u);
1296         if (gdth_readl(&dp6c_ptr->u) != DPMEM_MAGIC) {
1297             printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n", 
1298                    pcistr->dpmem);
1299             found = FALSE;
1300             for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1301                 iounmap(ha->brd);
1302                 ha->brd = ioremap(i, sizeof(ushort)); 
1303                 if (ha->brd == NULL) {
1304                     printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1305                     return 0;
1306                 }
1307                 if (gdth_readw(ha->brd) != 0xffff) {
1308                     TRACE2(("init_pci_plx() address 0x%x busy\n", i));
1309                     continue;
1310                 }
1311                 iounmap(ha->brd);
1312                 pci_write_config_dword(pcistr->pdev, 
1313                                        PCI_BASE_ADDRESS_2, i);
1314                 ha->brd = ioremap(i, sizeof(gdt6c_dpram_str)); 
1315                 if (ha->brd == NULL) {
1316                     printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1317                     return 0;
1318                 }
1319                 dp6c_ptr = ha->brd;
1320                 gdth_writel(DPMEM_MAGIC, &dp6c_ptr->u);
1321                 if (gdth_readl(&dp6c_ptr->u) == DPMEM_MAGIC) {
1322                     printk("GDT-PCI: Use free address at 0x%x\n", i);
1323                     found = TRUE;
1324                     break;
1325                 }
1326             }   
1327             if (!found) {
1328                 printk("GDT-PCI: No free address found!\n");
1329                 iounmap(ha->brd);
1330                 return 0;
1331             }
1332         }
1333         memset_io(&dp6c_ptr->u, 0, sizeof(dp6c_ptr->u));
1334         if (gdth_readl(&dp6c_ptr->u) != 0) {
1335             printk("GDT-PCI: Initialization error (DPMEM write error)\n");
1336             iounmap(ha->brd);
1337             return 0;
1338         }
1339         
1340         /* disable board interrupts, deinit services */
1341         outb(0x00,PTR2USHORT(&ha->plx->control1));
1342         outb(0xff,PTR2USHORT(&ha->plx->edoor_reg));
1343         
1344         gdth_writeb(0x00, &dp6c_ptr->u.ic.S_Status);
1345         gdth_writeb(0x00, &dp6c_ptr->u.ic.Cmd_Index);
1346
1347         gdth_writel(pcistr->dpmem, &dp6c_ptr->u.ic.S_Info[0]);
1348         gdth_writeb(0xff, &dp6c_ptr->u.ic.S_Cmd_Indx);
1349
1350         outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
1351
1352         retries = INIT_RETRIES;
1353         gdth_delay(20);
1354         while (gdth_readb(&dp6c_ptr->u.ic.S_Status) != 0xff) {
1355             if (--retries == 0) {
1356                 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1357                 iounmap(ha->brd);
1358                 return 0;
1359             }
1360             gdth_delay(1);
1361         }
1362         prot_ver = (unchar)gdth_readl(&dp6c_ptr->u.ic.S_Info[0]);
1363         gdth_writeb(0, &dp6c_ptr->u.ic.Status);
1364         if (prot_ver != PROTOCOL_VERSION) {
1365             printk("GDT-PCI: Illegal protocol version\n");
1366             iounmap(ha->brd);
1367             return 0;
1368         }
1369
1370         ha->type = GDT_PCINEW;
1371         ha->ic_all_size = sizeof(dp6c_ptr->u);
1372
1373         /* special command to controller BIOS */
1374         gdth_writel(0x00, &dp6c_ptr->u.ic.S_Info[0]);
1375         gdth_writel(0x00, &dp6c_ptr->u.ic.S_Info[1]);
1376         gdth_writel(0x00, &dp6c_ptr->u.ic.S_Info[2]);
1377         gdth_writel(0x00, &dp6c_ptr->u.ic.S_Info[3]);
1378         gdth_writeb(0xfe, &dp6c_ptr->u.ic.S_Cmd_Indx);
1379         
1380         outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
1381
1382         retries = INIT_RETRIES;
1383         gdth_delay(20);
1384         while (gdth_readb(&dp6c_ptr->u.ic.S_Status) != 0xfe) {
1385             if (--retries == 0) {
1386                 printk("GDT-PCI: Initialization error\n");
1387                 iounmap(ha->brd);
1388                 return 0;
1389             }
1390             gdth_delay(1);
1391         }
1392         gdth_writeb(0, &dp6c_ptr->u.ic.S_Status);
1393
1394         ha->dma64_support = 0;
1395
1396     } else {                                            /* MPR */
1397         TRACE2(("init_pci_mpr() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
1398         ha->brd = ioremap(pcistr->dpmem, sizeof(gdt6m_dpram_str));
1399         if (ha->brd == NULL) {
1400             printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1401             return 0;
1402         }
1403
1404         /* manipulate config. space to enable DPMEM, start RP controller */
1405 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
1406         pci_read_config_word(pcistr->pdev, PCI_COMMAND, &command);
1407         command |= 6;
1408         pci_write_config_word(pcistr->pdev, PCI_COMMAND, command);
1409         if (pci_resource_start(pcistr->pdev, 8) == 1UL)
1410             pci_resource_start(pcistr->pdev, 8) = 0UL;
1411         i = 0xFEFF0001UL;
1412         pci_write_config_dword(pcistr->pdev, PCI_ROM_ADDRESS, i);
1413         gdth_delay(1);
1414         pci_write_config_dword(pcistr->pdev, PCI_ROM_ADDRESS,
1415                                pci_resource_start(pcistr->pdev, 8));
1416 #else
1417         pci_read_config_word(pcistr->pdev, PCI_COMMAND, &command);
1418         command |= 6;
1419         pci_write_config_word(pcistr->pdev, PCI_COMMAND, command);
1420         if (pcistr->pdev->rom_address == 1UL)
1421             pcistr->pdev->rom_address = 0UL;
1422         i = 0xFEFF0001UL;
1423         pci_write_config_dword(pcistr->pdev, PCI_ROM_ADDRESS, i);
1424         gdth_delay(1);
1425         pci_write_config_dword(pcistr->pdev, PCI_ROM_ADDRESS,
1426                                pcistr->pdev->rom_address);
1427 #endif
1428         
1429         dp6m_ptr = ha->brd;
1430
1431         /* Ensure that it is safe to access the non HW portions of DPMEM.
1432          * Aditional check needed for Xscale based RAID controllers */
1433         while( ((int)gdth_readb(&dp6m_ptr->i960r.sema0_reg) ) & 3 )
1434             gdth_delay(1);
1435         
1436         /* check and reset interface area */
1437         gdth_writel(DPMEM_MAGIC, &dp6m_ptr->u);
1438         if (gdth_readl(&dp6m_ptr->u) != DPMEM_MAGIC) {
1439             printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n", 
1440                    pcistr->dpmem);
1441             found = FALSE;
1442             for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
1443                 iounmap(ha->brd);
1444                 ha->brd = ioremap(i, sizeof(ushort)); 
1445                 if (ha->brd == NULL) {
1446                     printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1447                     return 0;
1448                 }
1449                 if (gdth_readw(ha->brd) != 0xffff) {
1450                     TRACE2(("init_pci_mpr() address 0x%x busy\n", i));
1451                     continue;
1452                 }
1453                 iounmap(ha->brd);
1454                 pci_write_config_dword(pcistr->pdev, 
1455                                        PCI_BASE_ADDRESS_0, i);
1456                 ha->brd = ioremap(i, sizeof(gdt6m_dpram_str)); 
1457                 if (ha->brd == NULL) {
1458                     printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
1459                     return 0;
1460                 }
1461                 dp6m_ptr = ha->brd;
1462                 gdth_writel(DPMEM_MAGIC, &dp6m_ptr->u);
1463                 if (gdth_readl(&dp6m_ptr->u) == DPMEM_MAGIC) {
1464                     printk("GDT-PCI: Use free address at 0x%x\n", i);
1465                     found = TRUE;
1466                     break;
1467                 }
1468             }   
1469             if (!found) {
1470                 printk("GDT-PCI: No free address found!\n");
1471                 iounmap(ha->brd);
1472                 return 0;
1473             }
1474         }
1475         memset_io(&dp6m_ptr->u, 0, sizeof(dp6m_ptr->u));
1476         
1477         /* disable board interrupts, deinit services */
1478         gdth_writeb(gdth_readb(&dp6m_ptr->i960r.edoor_en_reg) | 4,
1479                     &dp6m_ptr->i960r.edoor_en_reg);
1480         gdth_writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1481         gdth_writeb(0x00, &dp6m_ptr->u.ic.S_Status);
1482         gdth_writeb(0x00, &dp6m_ptr->u.ic.Cmd_Index);
1483
1484         gdth_writel(pcistr->dpmem, &dp6m_ptr->u.ic.S_Info[0]);
1485         gdth_writeb(0xff, &dp6m_ptr->u.ic.S_Cmd_Indx);
1486         gdth_writeb(1, &dp6m_ptr->i960r.ldoor_reg);
1487         retries = INIT_RETRIES;
1488         gdth_delay(20);
1489         while (gdth_readb(&dp6m_ptr->u.ic.S_Status) != 0xff) {
1490             if (--retries == 0) {
1491                 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1492                 iounmap(ha->brd);
1493                 return 0;
1494             }
1495             gdth_delay(1);
1496         }
1497         prot_ver = (unchar)gdth_readl(&dp6m_ptr->u.ic.S_Info[0]);
1498         gdth_writeb(0, &dp6m_ptr->u.ic.S_Status);
1499         if (prot_ver != PROTOCOL_VERSION) {
1500             printk("GDT-PCI: Illegal protocol version\n");
1501             iounmap(ha->brd);
1502             return 0;
1503         }
1504
1505         ha->type = GDT_PCIMPR;
1506         ha->ic_all_size = sizeof(dp6m_ptr->u);
1507         
1508         /* special command to controller BIOS */
1509         gdth_writel(0x00, &dp6m_ptr->u.ic.S_Info[0]);
1510         gdth_writel(0x00, &dp6m_ptr->u.ic.S_Info[1]);
1511         gdth_writel(0x00, &dp6m_ptr->u.ic.S_Info[2]);
1512         gdth_writel(0x00, &dp6m_ptr->u.ic.S_Info[3]);
1513         gdth_writeb(0xfe, &dp6m_ptr->u.ic.S_Cmd_Indx);
1514         gdth_writeb(1, &dp6m_ptr->i960r.ldoor_reg);
1515         retries = INIT_RETRIES;
1516         gdth_delay(20);
1517         while (gdth_readb(&dp6m_ptr->u.ic.S_Status) != 0xfe) {
1518             if (--retries == 0) {
1519                 printk("GDT-PCI: Initialization error\n");
1520                 iounmap(ha->brd);
1521                 return 0;
1522             }
1523             gdth_delay(1);
1524         }
1525         gdth_writeb(0, &dp6m_ptr->u.ic.S_Status);
1526
1527         /* read FW version to detect 64-bit DMA support */
1528         gdth_writeb(0xfd, &dp6m_ptr->u.ic.S_Cmd_Indx);
1529         gdth_writeb(1, &dp6m_ptr->i960r.ldoor_reg);
1530         retries = INIT_RETRIES;
1531         gdth_delay(20);
1532         while (gdth_readb(&dp6m_ptr->u.ic.S_Status) != 0xfd) {
1533             if (--retries == 0) {
1534                 printk("GDT-PCI: Initialization error (DEINIT failed)\n");
1535                 iounmap(ha->brd);
1536                 return 0;
1537             }
1538             gdth_delay(1);
1539         }
1540         prot_ver = (unchar)(gdth_readl(&dp6m_ptr->u.ic.S_Info[0]) >> 16);
1541         gdth_writeb(0, &dp6m_ptr->u.ic.S_Status);
1542         if (prot_ver < 0x2b)      /* FW < x.43: no 64-bit DMA support */
1543             ha->dma64_support = 0;
1544         else 
1545             ha->dma64_support = 1;
1546     }
1547
1548     return 1;
1549 }
1550
1551
1552 /* controller protocol functions */
1553
1554 GDTH_INITFUNC(static void, gdth_enable_int(int hanum))
1555 {
1556     gdth_ha_str *ha;
1557     ulong flags;
1558     gdt2_dpram_str __iomem *dp2_ptr;
1559     gdt6_dpram_str __iomem *dp6_ptr;
1560     gdt6m_dpram_str __iomem *dp6m_ptr;
1561
1562     TRACE(("gdth_enable_int() hanum %d\n",hanum));
1563     ha = HADATA(gdth_ctr_tab[hanum]);
1564     GDTH_LOCK_HA(ha, flags);
1565
1566     if (ha->type == GDT_EISA) {
1567         outb(0xff, ha->bmic + EDOORREG);
1568         outb(0xff, ha->bmic + EDENABREG);
1569         outb(0x01, ha->bmic + EINTENABREG);
1570     } else if (ha->type == GDT_ISA) {
1571         dp2_ptr = ha->brd;
1572         gdth_writeb(1, &dp2_ptr->io.irqdel);
1573         gdth_writeb(0, &dp2_ptr->u.ic.Cmd_Index);
1574         gdth_writeb(1, &dp2_ptr->io.irqen);
1575     } else if (ha->type == GDT_PCI) {
1576         dp6_ptr = ha->brd;
1577         gdth_writeb(1, &dp6_ptr->io.irqdel);
1578         gdth_writeb(0, &dp6_ptr->u.ic.Cmd_Index);
1579         gdth_writeb(1, &dp6_ptr->io.irqen);
1580     } else if (ha->type == GDT_PCINEW) {
1581         outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
1582         outb(0x03, PTR2USHORT(&ha->plx->control1));
1583     } else if (ha->type == GDT_PCIMPR) {
1584         dp6m_ptr = ha->brd;
1585         gdth_writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1586         gdth_writeb(gdth_readb(&dp6m_ptr->i960r.edoor_en_reg) & ~4,
1587                     &dp6m_ptr->i960r.edoor_en_reg);
1588     }
1589     GDTH_UNLOCK_HA(ha, flags);
1590 }
1591
1592
1593 static int gdth_get_status(unchar *pIStatus,int irq)
1594 {
1595     register gdth_ha_str *ha;
1596     int i;
1597
1598     TRACE(("gdth_get_status() irq %d ctr_count %d\n",
1599            irq,gdth_ctr_count));
1600     
1601     *pIStatus = 0;
1602     for (i=0; i<gdth_ctr_count; ++i) {
1603         ha = HADATA(gdth_ctr_tab[i]);
1604         if (ha->irq != (unchar)irq)             /* check IRQ */
1605             continue;
1606         if (ha->type == GDT_EISA)
1607             *pIStatus = inb((ushort)ha->bmic + EDOORREG);
1608         else if (ha->type == GDT_ISA)
1609             *pIStatus =
1610                 gdth_readb(&((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
1611         else if (ha->type == GDT_PCI)
1612             *pIStatus =
1613                 gdth_readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Cmd_Index);
1614         else if (ha->type == GDT_PCINEW) 
1615             *pIStatus = inb(PTR2USHORT(&ha->plx->edoor_reg));
1616         else if (ha->type == GDT_PCIMPR)
1617             *pIStatus =
1618                 gdth_readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.edoor_reg);
1619    
1620         if (*pIStatus)                                  
1621             return i;                           /* board found */
1622     }
1623     return -1;
1624 }
1625                  
1626     
1627 static int gdth_test_busy(int hanum)
1628 {
1629     register gdth_ha_str *ha;
1630     register int gdtsema0 = 0;
1631
1632     TRACE(("gdth_test_busy() hanum %d\n",hanum));
1633     
1634     ha = HADATA(gdth_ctr_tab[hanum]);
1635     if (ha->type == GDT_EISA)
1636         gdtsema0 = (int)inb(ha->bmic + SEMA0REG);
1637     else if (ha->type == GDT_ISA)
1638         gdtsema0 = (int)gdth_readb(&((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
1639     else if (ha->type == GDT_PCI)
1640         gdtsema0 = (int)gdth_readb(&((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
1641     else if (ha->type == GDT_PCINEW) 
1642         gdtsema0 = (int)inb(PTR2USHORT(&ha->plx->sema0_reg));
1643     else if (ha->type == GDT_PCIMPR)
1644         gdtsema0 = 
1645             (int)gdth_readb(&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
1646
1647     return (gdtsema0 & 1);
1648 }
1649
1650
1651 static int gdth_get_cmd_index(int hanum)
1652 {
1653     register gdth_ha_str *ha;
1654     int i;
1655
1656     TRACE(("gdth_get_cmd_index() hanum %d\n",hanum));
1657
1658     ha = HADATA(gdth_ctr_tab[hanum]);
1659     for (i=0; i<GDTH_MAXCMDS; ++i) {
1660         if (ha->cmd_tab[i].cmnd == UNUSED_CMND) {
1661             ha->cmd_tab[i].cmnd = ha->pccb->RequestBuffer;
1662             ha->cmd_tab[i].service = ha->pccb->Service;
1663             ha->pccb->CommandIndex = (ulong32)i+2;
1664             return (i+2);
1665         }
1666     }
1667     return 0;
1668 }
1669
1670
1671 static void gdth_set_sema0(int hanum)
1672 {
1673     register gdth_ha_str *ha;
1674
1675     TRACE(("gdth_set_sema0() hanum %d\n",hanum));
1676
1677     ha = HADATA(gdth_ctr_tab[hanum]);
1678     if (ha->type == GDT_EISA) {
1679         outb(1, ha->bmic + SEMA0REG);
1680     } else if (ha->type == GDT_ISA) {
1681         gdth_writeb(1, &((gdt2_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
1682     } else if (ha->type == GDT_PCI) {
1683         gdth_writeb(1, &((gdt6_dpram_str __iomem *)ha->brd)->u.ic.Sema0);
1684     } else if (ha->type == GDT_PCINEW) { 
1685         outb(1, PTR2USHORT(&ha->plx->sema0_reg));
1686     } else if (ha->type == GDT_PCIMPR) {
1687         gdth_writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.sema0_reg);
1688     }
1689 }
1690
1691
1692 static void gdth_copy_command(int hanum)
1693 {
1694     register gdth_ha_str *ha;
1695     register gdth_cmd_str *cmd_ptr;
1696     register gdt6m_dpram_str __iomem *dp6m_ptr;
1697     register gdt6c_dpram_str __iomem *dp6c_ptr;
1698     gdt6_dpram_str __iomem *dp6_ptr;
1699     gdt2_dpram_str __iomem *dp2_ptr;
1700     ushort cp_count,dp_offset,cmd_no;
1701     
1702     TRACE(("gdth_copy_command() hanum %d\n",hanum));
1703
1704     ha = HADATA(gdth_ctr_tab[hanum]);
1705     cp_count = ha->cmd_len;
1706     dp_offset= ha->cmd_offs_dpmem;
1707     cmd_no   = ha->cmd_cnt;
1708     cmd_ptr  = ha->pccb;
1709
1710     ++ha->cmd_cnt;                                                      
1711     if (ha->type == GDT_EISA)
1712         return;                                 /* no DPMEM, no copy */
1713
1714     /* set cpcount dword aligned */
1715     if (cp_count & 3)
1716         cp_count += (4 - (cp_count & 3));
1717
1718     ha->cmd_offs_dpmem += cp_count;
1719     
1720     /* set offset and service, copy command to DPMEM */
1721     if (ha->type == GDT_ISA) {
1722         dp2_ptr = ha->brd;
1723         gdth_writew(dp_offset + DPMEM_COMMAND_OFFSET, 
1724                     &dp2_ptr->u.ic.comm_queue[cmd_no].offset);
1725         gdth_writew((ushort)cmd_ptr->Service, 
1726                     &dp2_ptr->u.ic.comm_queue[cmd_no].serv_id);
1727         memcpy_toio(&dp2_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1728     } else if (ha->type == GDT_PCI) {
1729         dp6_ptr = ha->brd;
1730         gdth_writew(dp_offset + DPMEM_COMMAND_OFFSET, 
1731                     &dp6_ptr->u.ic.comm_queue[cmd_no].offset);
1732         gdth_writew((ushort)cmd_ptr->Service, 
1733                     &dp6_ptr->u.ic.comm_queue[cmd_no].serv_id);
1734         memcpy_toio(&dp6_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1735     } else if (ha->type == GDT_PCINEW) {
1736         dp6c_ptr = ha->brd;
1737         gdth_writew(dp_offset + DPMEM_COMMAND_OFFSET, 
1738                     &dp6c_ptr->u.ic.comm_queue[cmd_no].offset);
1739         gdth_writew((ushort)cmd_ptr->Service, 
1740                     &dp6c_ptr->u.ic.comm_queue[cmd_no].serv_id);
1741         memcpy_toio(&dp6c_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1742     } else if (ha->type == GDT_PCIMPR) {
1743         dp6m_ptr = ha->brd;
1744         gdth_writew(dp_offset + DPMEM_COMMAND_OFFSET, 
1745                     &dp6m_ptr->u.ic.comm_queue[cmd_no].offset);
1746         gdth_writew((ushort)cmd_ptr->Service, 
1747                     &dp6m_ptr->u.ic.comm_queue[cmd_no].serv_id);
1748         memcpy_toio(&dp6m_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1749     }
1750 }
1751
1752
1753 static void gdth_release_event(int hanum)
1754 {
1755     register gdth_ha_str *ha;
1756
1757     TRACE(("gdth_release_event() hanum %d\n",hanum));
1758     ha = HADATA(gdth_ctr_tab[hanum]);
1759
1760 #ifdef GDTH_STATISTICS
1761     {
1762         ulong32 i,j;
1763         for (i=0,j=0; j<GDTH_MAXCMDS; ++j) {
1764             if (ha->cmd_tab[j].cmnd != UNUSED_CMND)
1765                 ++i;
1766         }
1767         if (max_index < i) {
1768             max_index = i;
1769             TRACE3(("GDT: max_index = %d\n",(ushort)i));
1770         }
1771     }
1772 #endif
1773
1774     if (ha->pccb->OpCode == GDT_INIT)
1775         ha->pccb->Service |= 0x80;
1776
1777     if (ha->type == GDT_EISA) {
1778         if (ha->pccb->OpCode == GDT_INIT)               /* store DMA buffer */
1779             outl(ha->ccb_phys, ha->bmic + MAILBOXREG);
1780         outb(ha->pccb->Service, ha->bmic + LDOORREG);
1781     } else if (ha->type == GDT_ISA) {
1782         gdth_writeb(0, &((gdt2_dpram_str __iomem *)ha->brd)->io.event);
1783     } else if (ha->type == GDT_PCI) {
1784         gdth_writeb(0, &((gdt6_dpram_str __iomem *)ha->brd)->io.event);
1785     } else if (ha->type == GDT_PCINEW) { 
1786         outb(1, PTR2USHORT(&ha->plx->ldoor_reg));
1787     } else if (ha->type == GDT_PCIMPR) {
1788         gdth_writeb(1, &((gdt6m_dpram_str __iomem *)ha->brd)->i960r.ldoor_reg);
1789     }
1790 }
1791
1792     
1793 static int gdth_wait(int hanum,int index,ulong32 time)
1794 {
1795     gdth_ha_str *ha;
1796     int answer_found = FALSE;
1797
1798     TRACE(("gdth_wait() hanum %d index %d time %d\n",hanum,index,time));
1799
1800     ha = HADATA(gdth_ctr_tab[hanum]);
1801     if (index == 0)
1802         return 1;                               /* no wait required */
1803
1804     gdth_from_wait = TRUE;
1805     do {
1806         gdth_interrupt((int)ha->irq,ha,NULL);
1807         if (wait_hanum==hanum && wait_index==index) {
1808             answer_found = TRUE;
1809             break;
1810         }
1811         gdth_delay(1);
1812     } while (--time);
1813     gdth_from_wait = FALSE;
1814     
1815     while (gdth_test_busy(hanum))
1816         gdth_delay(0);
1817
1818     return (answer_found);
1819 }
1820
1821
1822 static int gdth_internal_cmd(int hanum,unchar service,ushort opcode,ulong32 p1,
1823                              ulong64 p2,ulong64 p3)
1824 {
1825     register gdth_ha_str *ha;
1826     register gdth_cmd_str *cmd_ptr;
1827     int retries,index;
1828
1829     TRACE2(("gdth_internal_cmd() service %d opcode %d\n",service,opcode));
1830
1831     ha = HADATA(gdth_ctr_tab[hanum]);
1832     cmd_ptr = ha->pccb;
1833     memset((char*)cmd_ptr,0,sizeof(gdth_cmd_str));
1834
1835     /* make command  */
1836     for (retries = INIT_RETRIES;;) {
1837         cmd_ptr->Service          = service;
1838         cmd_ptr->RequestBuffer    = INTERNAL_CMND;
1839         if (!(index=gdth_get_cmd_index(hanum))) {
1840             TRACE(("GDT: No free command index found\n"));
1841             return 0;
1842         }
1843         gdth_set_sema0(hanum);
1844         cmd_ptr->OpCode           = opcode;
1845         cmd_ptr->BoardNode        = LOCALBOARD;
1846         if (service == CACHESERVICE) {
1847             if (opcode == GDT_IOCTL) {
1848                 cmd_ptr->u.ioctl.subfunc = p1;
1849                 cmd_ptr->u.ioctl.channel = (ulong32)p2;
1850                 cmd_ptr->u.ioctl.param_size = (ushort)p3;
1851                 cmd_ptr->u.ioctl.p_param = ha->scratch_phys;
1852             } else {
1853                 if (ha->cache_feat & GDT_64BIT) {
1854                     cmd_ptr->u.cache64.DeviceNo = (ushort)p1;
1855                     cmd_ptr->u.cache64.BlockNo  = p2;
1856                 } else {
1857                     cmd_ptr->u.cache.DeviceNo = (ushort)p1;
1858                     cmd_ptr->u.cache.BlockNo  = (ulong32)p2;
1859                 }
1860             }
1861         } else if (service == SCSIRAWSERVICE) {
1862             if (ha->raw_feat & GDT_64BIT) {
1863                 cmd_ptr->u.raw64.direction  = p1;
1864                 cmd_ptr->u.raw64.bus        = (unchar)p2;
1865                 cmd_ptr->u.raw64.target     = (unchar)p3;
1866                 cmd_ptr->u.raw64.lun        = (unchar)(p3 >> 8);
1867             } else {
1868                 cmd_ptr->u.raw.direction  = p1;
1869                 cmd_ptr->u.raw.bus        = (unchar)p2;
1870                 cmd_ptr->u.raw.target     = (unchar)p3;
1871                 cmd_ptr->u.raw.lun        = (unchar)(p3 >> 8);
1872             }
1873         } else if (service == SCREENSERVICE) {
1874             if (opcode == GDT_REALTIME) {
1875                 *(ulong32 *)&cmd_ptr->u.screen.su.data[0] = p1;
1876                 *(ulong32 *)&cmd_ptr->u.screen.su.data[4] = (ulong32)p2;
1877                 *(ulong32 *)&cmd_ptr->u.screen.su.data[8] = (ulong32)p3;
1878             }
1879         }
1880         ha->cmd_len          = sizeof(gdth_cmd_str);
1881         ha->cmd_offs_dpmem   = 0;
1882         ha->cmd_cnt          = 0;
1883         gdth_copy_command(hanum);
1884         gdth_release_event(hanum);
1885         gdth_delay(20);
1886         if (!gdth_wait(hanum,index,INIT_TIMEOUT)) {
1887             printk("GDT: Initialization error (timeout service %d)\n",service);
1888             return 0;
1889         }
1890         if (ha->status != S_BSY || --retries == 0)
1891             break;
1892         gdth_delay(1);   
1893     }   
1894     
1895     return (ha->status != S_OK ? 0:1);
1896 }
1897     
1898
1899 /* search for devices */
1900
1901 GDTH_INITFUNC(static int, gdth_search_drives(int hanum))
1902 {
1903     register gdth_ha_str *ha;
1904     ushort cdev_cnt, i;
1905     int ok;
1906     ulong32 bus_no, drv_cnt, drv_no, j;
1907     gdth_getch_str *chn;
1908     gdth_drlist_str *drl;
1909     gdth_iochan_str *ioc;
1910     gdth_raw_iochan_str *iocr;
1911     gdth_arcdl_str *alst;
1912     gdth_alist_str *alst2;
1913     gdth_oem_str_ioctl *oemstr;
1914 #ifdef INT_COAL
1915     gdth_perf_modes *pmod;
1916 #endif
1917
1918 #ifdef GDTH_RTC
1919     unchar rtc[12];
1920     ulong flags;
1921 #endif     
1922    
1923     TRACE(("gdth_search_drives() hanum %d\n",hanum));
1924     ha = HADATA(gdth_ctr_tab[hanum]);
1925     ok = 0;
1926
1927     /* initialize controller services, at first: screen service */
1928     ha->screen_feat = 0;
1929     if (!force_dma32) {
1930         ok = gdth_internal_cmd(hanum,SCREENSERVICE,GDT_X_INIT_SCR,0,0,0);
1931         if (ok)
1932             ha->screen_feat = GDT_64BIT;
1933     }
1934     if (force_dma32 || (!ok && ha->status == (ushort)S_NOFUNC))
1935         ok = gdth_internal_cmd(hanum,SCREENSERVICE,GDT_INIT,0,0,0);
1936     if (!ok) {
1937         printk("GDT-HA %d: Initialization error screen service (code %d)\n",
1938                hanum, ha->status);
1939         return 0;
1940     }
1941     TRACE2(("gdth_search_drives(): SCREENSERVICE initialized\n"));
1942
1943 #ifdef GDTH_RTC
1944     /* read realtime clock info, send to controller */
1945     /* 1. wait for the falling edge of update flag */
1946     spin_lock_irqsave(&rtc_lock, flags);
1947     for (j = 0; j < 1000000; ++j)
1948         if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
1949             break;
1950     for (j = 0; j < 1000000; ++j)
1951         if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
1952             break;
1953     /* 2. read info */
1954     do {
1955         for (j = 0; j < 12; ++j) 
1956             rtc[j] = CMOS_READ(j);
1957     } while (rtc[0] != CMOS_READ(0));
1958     spin_lock_irqrestore(&rtc_lock, flags);
1959     TRACE2(("gdth_search_drives(): RTC: %x/%x/%x\n",*(ulong32 *)&rtc[0],
1960             *(ulong32 *)&rtc[4], *(ulong32 *)&rtc[8]));
1961     /* 3. send to controller firmware */
1962     gdth_internal_cmd(hanum,SCREENSERVICE,GDT_REALTIME, *(ulong32 *)&rtc[0],
1963                       *(ulong32 *)&rtc[4], *(ulong32 *)&rtc[8]);
1964 #endif  
1965  
1966     /* unfreeze all IOs */
1967     gdth_internal_cmd(hanum,CACHESERVICE,GDT_UNFREEZE_IO,0,0,0);
1968  
1969     /* initialize cache service */
1970     ha->cache_feat = 0;
1971     if (!force_dma32) {
1972         ok = gdth_internal_cmd(hanum,CACHESERVICE,GDT_X_INIT_HOST,LINUX_OS,0,0);
1973         if (ok)
1974             ha->cache_feat = GDT_64BIT;
1975     }
1976     if (force_dma32 || (!ok && ha->status == (ushort)S_NOFUNC))
1977         ok = gdth_internal_cmd(hanum,CACHESERVICE,GDT_INIT,LINUX_OS,0,0);
1978     if (!ok) {
1979         printk("GDT-HA %d: Initialization error cache service (code %d)\n",
1980                hanum, ha->status);
1981         return 0;
1982     }
1983     TRACE2(("gdth_search_drives(): CACHESERVICE initialized\n"));
1984     cdev_cnt = (ushort)ha->info;
1985     ha->fw_vers = ha->service;
1986
1987 #ifdef INT_COAL
1988     if (ha->type == GDT_PCIMPR) {
1989         /* set perf. modes */
1990         pmod = (gdth_perf_modes *)ha->pscratch;
1991         pmod->version          = 1;
1992         pmod->st_mode          = 1;    /* enable one status buffer */
1993         *((ulong64 *)&pmod->st_buff_addr1) = ha->coal_stat_phys;
1994         pmod->st_buff_indx1    = COALINDEX;
1995         pmod->st_buff_addr2    = 0;
1996         pmod->st_buff_u_addr2  = 0;
1997         pmod->st_buff_indx2    = 0;
1998         pmod->st_buff_size     = sizeof(gdth_coal_status) * MAXOFFSETS;
1999         pmod->cmd_mode         = 0;    // disable all cmd buffers
2000         pmod->cmd_buff_addr1   = 0;
2001         pmod->cmd_buff_u_addr1 = 0;
2002         pmod->cmd_buff_indx1   = 0;
2003         pmod->cmd_buff_addr2   = 0;
2004         pmod->cmd_buff_u_addr2 = 0;
2005         pmod->cmd_buff_indx2   = 0;
2006         pmod->cmd_buff_size    = 0;
2007         pmod->reserved1        = 0;            
2008         pmod->reserved2        = 0;            
2009         if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,SET_PERF_MODES,
2010                               INVALID_CHANNEL,sizeof(gdth_perf_modes))) {
2011             printk("GDT-HA %d: Interrupt coalescing activated\n", hanum);
2012         }
2013     }
2014 #endif
2015
2016     /* detect number of buses - try new IOCTL */
2017     iocr = (gdth_raw_iochan_str *)ha->pscratch;
2018     iocr->hdr.version        = 0xffffffff;
2019     iocr->hdr.list_entries   = MAXBUS;
2020     iocr->hdr.first_chan     = 0;
2021     iocr->hdr.last_chan      = MAXBUS-1;
2022     iocr->hdr.list_offset    = GDTOFFSOF(gdth_raw_iochan_str, list[0]);
2023     if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,IOCHAN_RAW_DESC,
2024                           INVALID_CHANNEL,sizeof(gdth_raw_iochan_str))) {
2025         TRACE2(("IOCHAN_RAW_DESC supported!\n"));
2026         ha->bus_cnt = iocr->hdr.chan_count;
2027         for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
2028             if (iocr->list[bus_no].proc_id < MAXID)
2029                 ha->bus_id[bus_no] = iocr->list[bus_no].proc_id;
2030             else
2031                 ha->bus_id[bus_no] = 0xff;
2032         }
2033     } else {
2034         /* old method */
2035         chn = (gdth_getch_str *)ha->pscratch;
2036         for (bus_no = 0; bus_no < MAXBUS; ++bus_no) {
2037             chn->channel_no = bus_no;
2038             if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
2039                                    SCSI_CHAN_CNT | L_CTRL_PATTERN,
2040                                    IO_CHANNEL | INVALID_CHANNEL,
2041                                    sizeof(gdth_getch_str))) {
2042                 if (bus_no == 0) {
2043                     printk("GDT-HA %d: Error detecting channel count (0x%x)\n",
2044                            hanum, ha->status);
2045                     return 0;
2046                 }
2047                 break;
2048             }
2049             if (chn->siop_id < MAXID)
2050                 ha->bus_id[bus_no] = chn->siop_id;
2051             else
2052                 ha->bus_id[bus_no] = 0xff;
2053         }       
2054         ha->bus_cnt = (unchar)bus_no;
2055     }
2056     TRACE2(("gdth_search_drives() %d channels\n",ha->bus_cnt));
2057
2058     /* read cache configuration */
2059     if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,CACHE_INFO,
2060                            INVALID_CHANNEL,sizeof(gdth_cinfo_str))) {
2061         printk("GDT-HA %d: Initialization error cache service (code %d)\n",
2062                hanum, ha->status);
2063         return 0;
2064     }
2065     ha->cpar = ((gdth_cinfo_str *)ha->pscratch)->cpar;
2066     TRACE2(("gdth_search_drives() cinfo: vs %x sta %d str %d dw %d b %d\n",
2067             ha->cpar.version,ha->cpar.state,ha->cpar.strategy,
2068             ha->cpar.write_back,ha->cpar.block_size));
2069
2070     /* read board info and features */
2071     ha->more_proc = FALSE;
2072     if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,BOARD_INFO,
2073                           INVALID_CHANNEL,sizeof(gdth_binfo_str))) {
2074         memcpy(&ha->binfo, (gdth_binfo_str *)ha->pscratch,
2075                sizeof(gdth_binfo_str));
2076         if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,BOARD_FEATURES,
2077                               INVALID_CHANNEL,sizeof(gdth_bfeat_str))) {
2078             TRACE2(("BOARD_INFO/BOARD_FEATURES supported\n"));
2079             ha->bfeat = *(gdth_bfeat_str *)ha->pscratch;
2080             ha->more_proc = TRUE;
2081         }
2082     } else {
2083         TRACE2(("BOARD_INFO requires firmware >= 1.10/2.08\n"));
2084         strcpy(ha->binfo.type_string, gdth_ctr_name(hanum));
2085     }
2086     TRACE2(("Controller name: %s\n",ha->binfo.type_string));
2087
2088     /* read more informations */
2089     if (ha->more_proc) {
2090         /* physical drives, channel addresses */
2091         ioc = (gdth_iochan_str *)ha->pscratch;
2092         ioc->hdr.version        = 0xffffffff;
2093         ioc->hdr.list_entries   = MAXBUS;
2094         ioc->hdr.first_chan     = 0;
2095         ioc->hdr.last_chan      = MAXBUS-1;
2096         ioc->hdr.list_offset    = GDTOFFSOF(gdth_iochan_str, list[0]);
2097         if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,IOCHAN_DESC,
2098                               INVALID_CHANNEL,sizeof(gdth_iochan_str))) {
2099             for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
2100                 ha->raw[bus_no].address = ioc->list[bus_no].address;
2101                 ha->raw[bus_no].local_no = ioc->list[bus_no].local_no;
2102             }
2103         } else {
2104             for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
2105                 ha->raw[bus_no].address = IO_CHANNEL;
2106                 ha->raw[bus_no].local_no = bus_no;
2107             }
2108         }
2109         for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
2110             chn = (gdth_getch_str *)ha->pscratch;
2111             chn->channel_no = ha->raw[bus_no].local_no;
2112             if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
2113                                   SCSI_CHAN_CNT | L_CTRL_PATTERN,
2114                                   ha->raw[bus_no].address | INVALID_CHANNEL,
2115                                   sizeof(gdth_getch_str))) {
2116                 ha->raw[bus_no].pdev_cnt = chn->drive_cnt;
2117                 TRACE2(("Channel %d: %d phys. drives\n",
2118                         bus_no,chn->drive_cnt));
2119             }
2120             if (ha->raw[bus_no].pdev_cnt > 0) {
2121                 drl = (gdth_drlist_str *)ha->pscratch;
2122                 drl->sc_no = ha->raw[bus_no].local_no;
2123                 drl->sc_cnt = ha->raw[bus_no].pdev_cnt;
2124                 if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
2125                                       SCSI_DR_LIST | L_CTRL_PATTERN,
2126                                       ha->raw[bus_no].address | INVALID_CHANNEL,
2127                                       sizeof(gdth_drlist_str))) {
2128                     for (j = 0; j < ha->raw[bus_no].pdev_cnt; ++j) 
2129                         ha->raw[bus_no].id_list[j] = drl->sc_list[j];
2130                 } else {
2131                     ha->raw[bus_no].pdev_cnt = 0;
2132                 }
2133             }
2134         }
2135
2136         /* logical drives */
2137         if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,CACHE_DRV_CNT,
2138                               INVALID_CHANNEL,sizeof(ulong32))) {
2139             drv_cnt = *(ulong32 *)ha->pscratch;
2140             if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,CACHE_DRV_LIST,
2141                                   INVALID_CHANNEL,drv_cnt * sizeof(ulong32))) {
2142                 for (j = 0; j < drv_cnt; ++j) {
2143                     drv_no = ((ulong32 *)ha->pscratch)[j];
2144                     if (drv_no < MAX_LDRIVES) {
2145                         ha->hdr[drv_no].is_logdrv = TRUE;
2146                         TRACE2(("Drive %d is log. drive\n",drv_no));
2147                     }
2148                 }
2149             }
2150             alst = (gdth_arcdl_str *)ha->pscratch;
2151             alst->entries_avail = MAX_LDRIVES;
2152             alst->first_entry = 0;
2153             alst->list_offset = GDTOFFSOF(gdth_arcdl_str, list[0]);
2154             if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
2155                                   ARRAY_DRV_LIST2 | LA_CTRL_PATTERN, 
2156                                   INVALID_CHANNEL, sizeof(gdth_arcdl_str) +
2157                                   (alst->entries_avail-1) * sizeof(gdth_alist_str))) { 
2158                 for (j = 0; j < alst->entries_init; ++j) {
2159                     ha->hdr[j].is_arraydrv = alst->list[j].is_arrayd;
2160                     ha->hdr[j].is_master = alst->list[j].is_master;
2161                     ha->hdr[j].is_parity = alst->list[j].is_parity;
2162                     ha->hdr[j].is_hotfix = alst->list[j].is_hotfix;
2163                     ha->hdr[j].master_no = alst->list[j].cd_handle;
2164                 }
2165             } else if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
2166                                          ARRAY_DRV_LIST | LA_CTRL_PATTERN,
2167                                          0, 35 * sizeof(gdth_alist_str))) {
2168                 for (j = 0; j < 35; ++j) {
2169                     alst2 = &((gdth_alist_str *)ha->pscratch)[j];
2170                     ha->hdr[j].is_arraydrv = alst2->is_arrayd;
2171                     ha->hdr[j].is_master = alst2->is_master;
2172                     ha->hdr[j].is_parity = alst2->is_parity;
2173                     ha->hdr[j].is_hotfix = alst2->is_hotfix;
2174                     ha->hdr[j].master_no = alst2->cd_handle;
2175                 }
2176             }
2177         }
2178     }       
2179                                   
2180     /* initialize raw service */
2181     ha->raw_feat = 0;
2182     if (!force_dma32) {
2183         ok = gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_X_INIT_RAW,0,0,0);
2184         if (ok)
2185             ha->raw_feat = GDT_64BIT;
2186     }
2187     if (force_dma32 || (!ok && ha->status == (ushort)S_NOFUNC))
2188         ok = gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_INIT,0,0,0);
2189     if (!ok) {
2190         printk("GDT-HA %d: Initialization error raw service (code %d)\n",
2191                hanum, ha->status);
2192         return 0;
2193     }
2194     TRACE2(("gdth_search_drives(): RAWSERVICE initialized\n"));
2195
2196     /* set/get features raw service (scatter/gather) */
2197     if (gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_SET_FEAT,SCATTER_GATHER,
2198                           0,0)) {
2199         TRACE2(("gdth_search_drives(): set features RAWSERVICE OK\n"));
2200         if (gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_GET_FEAT,0,0,0)) {
2201             TRACE2(("gdth_search_dr(): get feat RAWSERVICE %d\n",
2202                     ha->info));
2203             ha->raw_feat |= (ushort)ha->info;
2204         }
2205     } 
2206
2207     /* set/get features cache service (equal to raw service) */
2208     if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_SET_FEAT,0,
2209                           SCATTER_GATHER,0)) {
2210         TRACE2(("gdth_search_drives(): set features CACHESERVICE OK\n"));
2211         if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_GET_FEAT,0,0,0)) {
2212             TRACE2(("gdth_search_dr(): get feat CACHESERV. %d\n",
2213                     ha->info));
2214             ha->cache_feat |= (ushort)ha->info;
2215         }
2216     }
2217
2218     /* reserve drives for raw service */
2219     if (reserve_mode != 0) {
2220         gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_RESERVE_ALL,
2221                           reserve_mode == 1 ? 1 : 3, 0, 0);
2222         TRACE2(("gdth_search_drives(): RESERVE_ALL code %d\n", 
2223                 ha->status));
2224     }
2225     for (i = 0; i < MAX_RES_ARGS; i += 4) {
2226         if (reserve_list[i] == hanum && reserve_list[i+1] < ha->bus_cnt && 
2227             reserve_list[i+2] < ha->tid_cnt && reserve_list[i+3] < MAXLUN) {
2228             TRACE2(("gdth_search_drives(): reserve ha %d bus %d id %d lun %d\n",
2229                     reserve_list[i], reserve_list[i+1],
2230                     reserve_list[i+2], reserve_list[i+3]));
2231             if (!gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_RESERVE,0,
2232                                    reserve_list[i+1], reserve_list[i+2] | 
2233                                    (reserve_list[i+3] << 8))) {
2234                 printk("GDT-HA %d: Error raw service (RESERVE, code %d)\n",
2235                        hanum, ha->status);
2236              }
2237         }
2238     }
2239
2240     /* Determine OEM string using IOCTL */
2241     oemstr = (gdth_oem_str_ioctl *)ha->pscratch;
2242     oemstr->params.ctl_version = 0x01;
2243     oemstr->params.buffer_size = sizeof(oemstr->text);
2244     if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
2245                           CACHE_READ_OEM_STRING_RECORD,INVALID_CHANNEL,
2246                           sizeof(gdth_oem_str_ioctl))) {
2247         TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD OK\n"));
2248         printk("GDT-HA %d: Vendor: %s Name: %s\n",
2249                hanum,oemstr->text.oem_company_name,ha->binfo.type_string);
2250         /* Save the Host Drive inquiry data */
2251 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2252         strlcpy(ha->oem_name,oemstr->text.scsi_host_drive_inquiry_vendor_id,
2253                 sizeof(ha->oem_name));
2254 #else
2255         strncpy(ha->oem_name,oemstr->text.scsi_host_drive_inquiry_vendor_id,7);
2256         ha->oem_name[7] = '\0';
2257 #endif
2258     } else {
2259         /* Old method, based on PCI ID */
2260         TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD failed\n"));
2261         printk("GDT-HA %d: Name: %s\n",
2262                hanum,ha->binfo.type_string);
2263 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2264         if (ha->oem_id == OEM_ID_INTEL)
2265             strlcpy(ha->oem_name,"Intel  ", sizeof(ha->oem_name));
2266         else
2267             strlcpy(ha->oem_name,"ICP    ", sizeof(ha->oem_name));
2268 #else 
2269         if (ha->oem_id == OEM_ID_INTEL)
2270             strcpy(ha->oem_name,"Intel  ");
2271         else
2272             strcpy(ha->oem_name,"ICP    ");
2273 #endif
2274     }
2275
2276     /* scanning for host drives */
2277     for (i = 0; i < cdev_cnt; ++i) 
2278         gdth_analyse_hdrive(hanum,i);
2279     
2280     TRACE(("gdth_search_drives() OK\n"));
2281     return 1;
2282 }
2283
2284 static int gdth_analyse_hdrive(int hanum,ushort hdrive)
2285 {
2286     register gdth_ha_str *ha;
2287     ulong32 drv_cyls;
2288     int drv_hds, drv_secs;
2289
2290     TRACE(("gdth_analyse_hdrive() hanum %d drive %d\n",hanum,hdrive));
2291     if (hdrive >= MAX_HDRIVES)
2292         return 0;
2293     ha = HADATA(gdth_ctr_tab[hanum]);
2294
2295     if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_INFO,hdrive,0,0)) 
2296         return 0;
2297     ha->hdr[hdrive].present = TRUE;
2298     ha->hdr[hdrive].size = ha->info;
2299    
2300     /* evaluate mapping (sectors per head, heads per cylinder) */
2301     ha->hdr[hdrive].size &= ~SECS32;
2302     if (ha->info2 == 0) {
2303         gdth_eval_mapping(ha->hdr[hdrive].size,&drv_cyls,&drv_hds,&drv_secs);
2304     } else {
2305         drv_hds = ha->info2 & 0xff;
2306         drv_secs = (ha->info2 >> 8) & 0xff;
2307         drv_cyls = (ulong32)ha->hdr[hdrive].size / drv_hds / drv_secs;
2308     }
2309     ha->hdr[hdrive].heads = (unchar)drv_hds;
2310     ha->hdr[hdrive].secs  = (unchar)drv_secs;
2311     /* round size */
2312     ha->hdr[hdrive].size  = drv_cyls * drv_hds * drv_secs;
2313     
2314     if (ha->cache_feat & GDT_64BIT) {
2315         if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_X_INFO,hdrive,0,0)
2316             && ha->info2 != 0) {
2317             ha->hdr[hdrive].size = ((ulong64)ha->info2 << 32) | ha->info;
2318         }
2319     }
2320     TRACE2(("gdth_search_dr() cdr. %d size %d hds %d scs %d\n",
2321             hdrive,ha->hdr[hdrive].size,drv_hds,drv_secs));
2322
2323     /* get informations about device */
2324     if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_DEVTYPE,hdrive,0,0)) {
2325         TRACE2(("gdth_search_dr() cache drive %d devtype %d\n",
2326                 hdrive,ha->info));
2327         ha->hdr[hdrive].devtype = (ushort)ha->info;
2328     }
2329
2330     /* cluster info */
2331     if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_CLUST_INFO,hdrive,0,0)) {
2332         TRACE2(("gdth_search_dr() cache drive %d cluster info %d\n",
2333                 hdrive,ha->info));
2334         if (!shared_access)
2335             ha->hdr[hdrive].cluster_type = (unchar)ha->info;
2336     }
2337
2338     /* R/W attributes */
2339     if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_RW_ATTRIBS,hdrive,0,0)) {
2340         TRACE2(("gdth_search_dr() cache drive %d r/w attrib. %d\n",
2341                 hdrive,ha->info));
2342         ha->hdr[hdrive].rw_attribs = (unchar)ha->info;
2343     }
2344
2345     return 1;
2346 }
2347
2348
2349 /* command queueing/sending functions */
2350
2351 static void gdth_putq(int hanum,Scsi_Cmnd *scp,unchar priority)
2352 {
2353     register gdth_ha_str *ha;
2354     register Scsi_Cmnd *pscp;
2355     register Scsi_Cmnd *nscp;
2356     ulong flags;
2357     unchar b, t;
2358
2359     TRACE(("gdth_putq() priority %d\n",priority));
2360     ha = HADATA(gdth_ctr_tab[hanum]);
2361     GDTH_LOCK_HA(ha, flags);
2362
2363     scp->SCp.this_residual = (int)priority;
2364 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2365     b = virt_ctr ? NUMDATA(scp->device->host)->busnum : scp->device->channel;
2366     t = scp->device->id;
2367 #else
2368     b = virt_ctr ? NUMDATA(scp->host)->busnum : scp->channel;
2369     t = scp->target;
2370 #endif
2371     if (priority >= DEFAULT_PRI) {
2372         if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha,b)].lock) ||
2373             (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock)) {
2374             TRACE2(("gdth_putq(): locked IO -> update_timeout()\n"));
2375             scp->SCp.buffers_residual = gdth_update_timeout(hanum, scp, 0);
2376         }
2377     }
2378
2379     if (ha->req_first==NULL) {
2380         ha->req_first = scp;                    /* queue was empty */
2381         scp->SCp.ptr = NULL;
2382     } else {                                    /* queue not empty */
2383         pscp = ha->req_first;
2384         nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
2385         /* priority: 0-highest,..,0xff-lowest */
2386         while (nscp && (unchar)nscp->SCp.this_residual <= priority) {
2387             pscp = nscp;
2388             nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
2389         }
2390         pscp->SCp.ptr = (char *)scp;
2391         scp->SCp.ptr  = (char *)nscp;
2392     }
2393     GDTH_UNLOCK_HA(ha, flags);
2394
2395 #ifdef GDTH_STATISTICS
2396     flags = 0;
2397     for (nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
2398         ++flags;
2399     if (max_rq < flags) {
2400         max_rq = flags;
2401         TRACE3(("GDT: max_rq = %d\n",(ushort)max_rq));
2402     }
2403 #endif
2404 }
2405
2406 static void gdth_next(int hanum)
2407 {
2408     register gdth_ha_str *ha;
2409     register Scsi_Cmnd *pscp;
2410     register Scsi_Cmnd *nscp;
2411     unchar b, t, l, firsttime;
2412     unchar this_cmd, next_cmd;
2413     ulong flags = 0;
2414     int cmd_index;
2415
2416     TRACE(("gdth_next() hanum %d\n",hanum));
2417     ha = HADATA(gdth_ctr_tab[hanum]);
2418     if (!gdth_polling) 
2419         GDTH_LOCK_HA(ha, flags);
2420
2421     ha->cmd_cnt = ha->cmd_offs_dpmem = 0;
2422     this_cmd = firsttime = TRUE;
2423     next_cmd = gdth_polling ? FALSE:TRUE;
2424     cmd_index = 0;
2425
2426     for (nscp = pscp = ha->req_first; nscp; nscp = (Scsi_Cmnd *)nscp->SCp.ptr) {
2427         if (nscp != pscp && nscp != (Scsi_Cmnd *)pscp->SCp.ptr)
2428             pscp = (Scsi_Cmnd *)pscp->SCp.ptr;
2429 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2430         b = virt_ctr ? NUMDATA(nscp->device->host)->busnum : nscp->device->channel;
2431         t = nscp->device->id;
2432         l = nscp->device->lun;
2433 #else
2434         b = virt_ctr ? NUMDATA(nscp->host)->busnum : nscp->channel;
2435         t = nscp->target;
2436         l = nscp->lun;
2437 #endif
2438         if (nscp->SCp.this_residual >= DEFAULT_PRI) {
2439             if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha,b)].lock) ||
2440                 (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock)) 
2441                 continue;
2442         }
2443
2444         if (firsttime) {
2445             if (gdth_test_busy(hanum)) {        /* controller busy ? */
2446                 TRACE(("gdth_next() controller %d busy !\n",hanum));
2447                 if (!gdth_polling) {
2448                     GDTH_UNLOCK_HA(ha, flags);
2449                     return;
2450                 }
2451                 while (gdth_test_busy(hanum))
2452                     gdth_delay(1);
2453             }   
2454             firsttime = FALSE;
2455         }
2456
2457         if (nscp->done != gdth_scsi_done || nscp->cmnd[0] != 0xff) {        
2458         if (nscp->SCp.phase == -1) {
2459             nscp->SCp.phase = CACHESERVICE;           /* default: cache svc. */ 
2460             if (nscp->cmnd[0] == TEST_UNIT_READY) {
2461                 TRACE2(("TEST_UNIT_READY Bus %d Id %d LUN %d\n", 
2462                         b, t, l));
2463                 /* TEST_UNIT_READY -> set scan mode */
2464                 if ((ha->scan_mode & 0x0f) == 0) {
2465                     if (b == 0 && t == 0 && l == 0) {
2466                         ha->scan_mode |= 1;
2467                         TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
2468                     }
2469                 } else if ((ha->scan_mode & 0x0f) == 1) {
2470                     if (b == 0 && ((t == 0 && l == 1) ||
2471                          (t == 1 && l == 0))) {
2472                         nscp->SCp.sent_command = GDT_SCAN_START;
2473                         nscp->SCp.phase = ((ha->scan_mode & 0x10 ? 1:0) << 8) 
2474                             | SCSIRAWSERVICE;
2475                         ha->scan_mode = 0x12;
2476                         TRACE2(("Scan mode: 0x%x (SCAN_START)\n", 
2477                                 ha->scan_mode));
2478                     } else {
2479                         ha->scan_mode &= 0x10;
2480                         TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
2481                     }                   
2482                 } else if (ha->scan_mode == 0x12) {
2483                     if (b == ha->bus_cnt && t == ha->tid_cnt-1) {
2484                         nscp->SCp.phase = SCSIRAWSERVICE;
2485                         nscp->SCp.sent_command = GDT_SCAN_END;
2486                         ha->scan_mode &= 0x10;
2487                         TRACE2(("Scan mode: 0x%x (SCAN_END)\n", 
2488                                 ha->scan_mode));
2489                     }
2490                 }
2491             }
2492             if (b == ha->virt_bus && nscp->cmnd[0] != INQUIRY &&
2493                 nscp->cmnd[0] != READ_CAPACITY && nscp->cmnd[0] != MODE_SENSE &&
2494                 (ha->hdr[t].cluster_type & CLUSTER_DRIVE)) {
2495                 /* always GDT_CLUST_INFO! */
2496                 nscp->SCp.sent_command = GDT_CLUST_INFO;
2497             }
2498         }
2499         }
2500
2501         if (nscp->SCp.sent_command != -1) {
2502             if ((nscp->SCp.phase & 0xff) == CACHESERVICE) {
2503                 if (!(cmd_index=gdth_fill_cache_cmd(hanum,nscp,t)))
2504                     this_cmd = FALSE;
2505                 next_cmd = FALSE;
2506             } else if ((nscp->SCp.phase & 0xff) == SCSIRAWSERVICE) {
2507                 if (!(cmd_index=gdth_fill_raw_cmd(hanum,nscp,BUS_L2P(ha,b))))
2508                     this_cmd = FALSE;
2509                 next_cmd = FALSE;
2510             } else {
2511                 memset((char*)nscp->sense_buffer,0,16);
2512                 nscp->sense_buffer[0] = 0x70;
2513                 nscp->sense_buffer[2] = NOT_READY;
2514                 nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
2515                 if (!nscp->SCp.have_data_in)
2516                     nscp->SCp.have_data_in++;
2517                 else {
2518                     if (!gdth_polling) 
2519                         GDTH_UNLOCK_HA(ha,flags);
2520                     /* io_request_lock already active ! */
2521                     nscp->scsi_done(nscp);
2522                     if (!gdth_polling) 
2523                         GDTH_LOCK_HA(ha,flags);
2524                 }
2525             }
2526         } else if (nscp->done == gdth_scsi_done && nscp->cmnd[0] == 0xff) {
2527             if (!(cmd_index=gdth_special_cmd(hanum,nscp)))
2528                 this_cmd = FALSE;
2529             next_cmd = FALSE;
2530         } else if (b != ha->virt_bus) {
2531             if (ha->raw[BUS_L2P(ha,b)].io_cnt[t] >= GDTH_MAX_RAW ||
2532                 !(cmd_index=gdth_fill_raw_cmd(hanum,nscp,BUS_L2P(ha,b)))) 
2533                 this_cmd = FALSE;
2534             else 
2535                 ha->raw[BUS_L2P(ha,b)].io_cnt[t]++;
2536         } else if (t >= MAX_HDRIVES || !ha->hdr[t].present || l != 0) {
2537             TRACE2(("Command 0x%x to bus %d id %d lun %d -> IGNORE\n",
2538                     nscp->cmnd[0], b, t, l));
2539             nscp->result = DID_BAD_TARGET << 16;
2540             if (!nscp->SCp.have_data_in)
2541                 nscp->SCp.have_data_in++;
2542             else {
2543                 if (!gdth_polling) 
2544                     GDTH_UNLOCK_HA(ha,flags);
2545                 /* io_request_lock already active ! */      
2546                 nscp->scsi_done(nscp);
2547                 if (!gdth_polling) 
2548                     GDTH_LOCK_HA(ha,flags);
2549             }
2550         } else {
2551             switch (nscp->cmnd[0]) {
2552               case TEST_UNIT_READY:
2553               case INQUIRY:
2554               case REQUEST_SENSE:
2555               case READ_CAPACITY:
2556               case VERIFY:
2557               case START_STOP:
2558               case MODE_SENSE:
2559 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2560               case SERVICE_ACTION_IN:
2561 #endif
2562                 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
2563                        nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2564                        nscp->cmnd[4],nscp->cmnd[5]));
2565                 if (ha->hdr[t].media_changed && nscp->cmnd[0] != INQUIRY) {
2566                     /* return UNIT_ATTENTION */
2567                     TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
2568                              nscp->cmnd[0], t));
2569                     ha->hdr[t].media_changed = FALSE;
2570                     memset((char*)nscp->sense_buffer,0,16);
2571                     nscp->sense_buffer[0] = 0x70;
2572                     nscp->sense_buffer[2] = UNIT_ATTENTION;
2573                     nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
2574                     if (!nscp->SCp.have_data_in)
2575                         nscp->SCp.have_data_in++;
2576                     else {
2577                         if (!gdth_polling) 
2578                             GDTH_UNLOCK_HA(ha,flags);
2579                         /* io_request_lock already active ! */      
2580                         nscp->scsi_done(nscp);
2581                         if (!gdth_polling) 
2582                             GDTH_LOCK_HA(ha,flags);
2583                     }
2584                 } else if (gdth_internal_cache_cmd(hanum,nscp)) {
2585                     if (!gdth_polling) 
2586                         GDTH_UNLOCK_HA(ha,flags);
2587                     /* io_request_lock already active ! */      
2588                     nscp->scsi_done(nscp);
2589                     if (!gdth_polling) 
2590                         GDTH_LOCK_HA(ha,flags);
2591                 }
2592                 break;
2593
2594               case ALLOW_MEDIUM_REMOVAL:
2595                 TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
2596                        nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2597                        nscp->cmnd[4],nscp->cmnd[5]));
2598                 if ( (nscp->cmnd[4]&1) && !(ha->hdr[t].devtype&1) ) {
2599                     TRACE(("Prevent r. nonremov. drive->do nothing\n"));
2600                     nscp->result = DID_OK << 16;
2601                     nscp->sense_buffer[0] = 0;
2602                     if (!nscp->SCp.have_data_in)
2603                         nscp->SCp.have_data_in++;
2604                     else {
2605                         if (!gdth_polling) 
2606                             GDTH_UNLOCK_HA(ha,flags);
2607                         /* io_request_lock already active ! */      
2608                         nscp->scsi_done(nscp);
2609                         if (!gdth_polling) 
2610                             GDTH_LOCK_HA(ha,flags);
2611                     }
2612                 } else {
2613                     nscp->cmnd[3] = (ha->hdr[t].devtype&1) ? 1:0;
2614                     TRACE(("Prevent/allow r. %d rem. drive %d\n",
2615                            nscp->cmnd[4],nscp->cmnd[3]));
2616                     if (!(cmd_index=gdth_fill_cache_cmd(hanum,nscp,t)))
2617                         this_cmd = FALSE;
2618                 }
2619                 break;
2620                 
2621               case RESERVE:
2622               case RELEASE:
2623                 TRACE2(("cache cmd %s\n",nscp->cmnd[0] == RESERVE ?
2624                         "RESERVE" : "RELEASE"));
2625                 if (!(cmd_index=gdth_fill_cache_cmd(hanum,nscp,t)))
2626                     this_cmd = FALSE;
2627                 break;
2628                 
2629               case READ_6:
2630               case WRITE_6:
2631               case READ_10:
2632               case WRITE_10:
2633 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2634               case READ_16:
2635               case WRITE_16:
2636 #endif
2637                 if (ha->hdr[t].media_changed) {
2638                     /* return UNIT_ATTENTION */
2639                     TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
2640                              nscp->cmnd[0], t));
2641                     ha->hdr[t].media_changed = FALSE;
2642                     memset((char*)nscp->sense_buffer,0,16);
2643                     nscp->sense_buffer[0] = 0x70;
2644                     nscp->sense_buffer[2] = UNIT_ATTENTION;
2645                     nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
2646                     if (!nscp->SCp.have_data_in)
2647                         nscp->SCp.have_data_in++;
2648                     else {
2649                         if (!gdth_polling) 
2650                             GDTH_UNLOCK_HA(ha,flags);
2651                         /* io_request_lock already active ! */      
2652                         nscp->scsi_done(nscp);
2653                         if (!gdth_polling) 
2654                             GDTH_LOCK_HA(ha,flags);
2655                     }
2656                 } else if (!(cmd_index=gdth_fill_cache_cmd(hanum,nscp,t)))
2657                     this_cmd = FALSE;
2658                 break;
2659
2660               default:
2661                 TRACE2(("cache cmd %x/%x/%x/%x/%x/%x unknown\n",nscp->cmnd[0],
2662                         nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
2663                         nscp->cmnd[4],nscp->cmnd[5]));
2664                 printk("GDT-HA %d: Unknown SCSI command 0x%x to cache service !\n",
2665                        hanum, nscp->cmnd[0]);
2666                 nscp->result = DID_ABORT << 16;
2667                 if (!nscp->SCp.have_data_in)
2668                     nscp->SCp.have_data_in++;
2669                 else {
2670                     if (!gdth_polling) 
2671                         GDTH_UNLOCK_HA(ha,flags);
2672                     /* io_request_lock already active ! */  
2673                     nscp->scsi_done(nscp);
2674                     if (!gdth_polling) 
2675                         GDTH_LOCK_HA(ha,flags);
2676                 }
2677                 break;
2678             }
2679         }
2680
2681         if (!this_cmd)
2682             break;
2683         if (nscp == ha->req_first)
2684             ha->req_first = pscp = (Scsi_Cmnd *)nscp->SCp.ptr;
2685         else
2686             pscp->SCp.ptr = nscp->SCp.ptr;
2687         if (!next_cmd)
2688             break;
2689     }
2690
2691     if (ha->cmd_cnt > 0) {
2692         gdth_release_event(hanum);
2693     }
2694
2695     if (!gdth_polling) 
2696         GDTH_UNLOCK_HA(ha, flags);
2697
2698     if (gdth_polling && ha->cmd_cnt > 0) {
2699         if (!gdth_wait(hanum,cmd_index,POLL_TIMEOUT))
2700             printk("GDT-HA %d: Command %d timed out !\n",
2701                    hanum,cmd_index);
2702     }
2703 }
2704    
2705 static void gdth_copy_internal_data(int hanum,Scsi_Cmnd *scp,
2706                                     char *buffer,ushort count)
2707 {
2708     ushort cpcount,i;
2709     ushort cpsum,cpnow;
2710     struct scatterlist *sl;
2711     gdth_ha_str *ha;
2712     int sgcnt;
2713     char *address;
2714
2715     cpcount = count<=(ushort)scp->bufflen ? count:(ushort)scp->bufflen;
2716     ha = HADATA(gdth_ctr_tab[hanum]);
2717
2718     if (scp->use_sg) {
2719         sl = (struct scatterlist *)scp->request_buffer;
2720 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,13)
2721         sgcnt = pci_map_sg(ha->pdev,sl,scp->use_sg,PCI_DMA_FROMDEVICE);
2722         for (i=0,cpsum=0; i<sgcnt; ++i,++sl) {
2723             cpnow = (ushort)sg_dma_len(sl);
2724             TRACE(("copy_internal() now %d sum %d count %d %d\n",
2725                           cpnow,cpsum,cpcount,(ushort)scp->bufflen));
2726             if (cpsum+cpnow > cpcount) 
2727                 cpnow = cpcount - cpsum;
2728             cpsum += cpnow;
2729             if (!sl->page) {
2730                 printk("GDT-HA %d: invalid sc/gt element in gdth_copy_internal_data()\n",
2731                        hanum);
2732                 return;
2733             }
2734             address = (char *)(page_address(sl->page) + sl->offset);
2735             memcpy(address,buffer,cpnow);
2736             if (cpsum == cpcount)
2737                 break;
2738             buffer += cpnow;
2739         }
2740         pci_unmap_sg(ha->pdev,scp->request_buffer,
2741                      scp->use_sg,PCI_DMA_FROMDEVICE);
2742 #else
2743         sgcnt = scp->use_sg;
2744         for (i=0,cpsum=0; i<sgcnt; ++i,++sl) {
2745             cpnow = (ushort)sl->length;
2746             TRACE(("copy_internal() now %d sum %d count %d %d\n",
2747                           cpnow,cpsum,cpcount,(ushort)scp->bufflen));
2748             if (cpsum+cpnow > cpcount) 
2749                cpnow = cpcount - cpsum;
2750             cpsum += cpnow;
2751             address = (char *)sl->address;
2752             memcpy(address,buffer,cpnow);
2753             if (cpsum == cpcount)
2754                 break;
2755             buffer += cpnow;
2756         }
2757 #endif
2758     } else {
2759         TRACE(("copy_internal() count %d\n",cpcount));
2760         memcpy((char*)scp->request_buffer,buffer,cpcount);
2761     }
2762 }
2763
2764 static int gdth_internal_cache_cmd(int hanum,Scsi_Cmnd *scp)
2765 {
2766     register gdth_ha_str *ha;
2767     unchar t;
2768     gdth_inq_data inq;
2769     gdth_rdcap_data rdc;
2770     gdth_sense_data sd;
2771     gdth_modep_data mpd;
2772
2773     ha = HADATA(gdth_ctr_tab[hanum]);
2774 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2775     t  = scp->device->id;
2776 #else
2777     t  = scp->target;
2778 #endif
2779     TRACE(("gdth_internal_cache_cmd() cmd 0x%x hdrive %d\n",
2780            scp->cmnd[0],t));
2781
2782     scp->result = DID_OK << 16;
2783     scp->sense_buffer[0] = 0;
2784
2785     switch (scp->cmnd[0]) {
2786       case TEST_UNIT_READY:
2787       case VERIFY:
2788       case START_STOP:
2789         TRACE2(("Test/Verify/Start hdrive %d\n",t));
2790         break;
2791
2792       case INQUIRY:
2793         TRACE2(("Inquiry hdrive %d devtype %d\n",
2794                 t,ha->hdr[t].devtype));
2795         inq.type_qual = (ha->hdr[t].devtype&4) ? TYPE_ROM:TYPE_DISK;
2796         /* you can here set all disks to removable, if you want to do
2797            a flush using the ALLOW_MEDIUM_REMOVAL command */
2798         inq.modif_rmb = 0x00;
2799         if ((ha->hdr[t].devtype & 1) ||
2800             (ha->hdr[t].cluster_type & CLUSTER_DRIVE))
2801             inq.modif_rmb = 0x80;
2802         inq.version   = 2;
2803         inq.resp_aenc = 2;
2804         inq.add_length= 32;
2805         strcpy(inq.vendor,ha->oem_name);
2806         sprintf(inq.product,"Host Drive  #%02d",t);
2807         strcpy(inq.revision,"   ");
2808         gdth_copy_internal_data(hanum,scp,(char*)&inq,sizeof(gdth_inq_data));
2809         break;
2810
2811       case REQUEST_SENSE:
2812         TRACE2(("Request sense hdrive %d\n",t));
2813         sd.errorcode = 0x70;
2814         sd.segno     = 0x00;
2815         sd.key       = NO_SENSE;
2816         sd.info      = 0;
2817         sd.add_length= 0;
2818         gdth_copy_internal_data(hanum,scp,(char*)&sd,sizeof(gdth_sense_data));
2819         break;
2820
2821       case MODE_SENSE:
2822         TRACE2(("Mode sense hdrive %d\n",t));
2823         memset((char*)&mpd,0,sizeof(gdth_modep_data));
2824         mpd.hd.data_length = sizeof(gdth_modep_data);
2825         mpd.hd.dev_par     = (ha->hdr[t].devtype&2) ? 0x80:0;
2826         mpd.hd.bd_length   = sizeof(mpd.bd);
2827         mpd.bd.block_length[0] = (SECTOR_SIZE & 0x00ff0000) >> 16;
2828         mpd.bd.block_length[1] = (SECTOR_SIZE & 0x0000ff00) >> 8;
2829         mpd.bd.block_length[2] = (SECTOR_SIZE & 0x000000ff);
2830         gdth_copy_internal_data(hanum,scp,(char*)&mpd,sizeof(gdth_modep_data));
2831         break;
2832
2833       case READ_CAPACITY:
2834         TRACE2(("Read capacity hdrive %d\n",t));
2835         if (ha->hdr[t].size > (ulong64)0xffffffff)
2836             rdc.last_block_no = 0xffffffff;
2837         else
2838             rdc.last_block_no = cpu_to_be32(ha->hdr[t].size-1);
2839         rdc.block_length  = cpu_to_be32(SECTOR_SIZE);
2840         gdth_copy_internal_data(hanum,scp,(char*)&rdc,sizeof(gdth_rdcap_data));
2841         break;
2842
2843 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2844       case SERVICE_ACTION_IN:
2845         if ((scp->cmnd[1] & 0x1f) == SAI_READ_CAPACITY_16 &&
2846             (ha->cache_feat & GDT_64BIT)) {
2847             gdth_rdcap16_data rdc16;
2848
2849             TRACE2(("Read capacity (16) hdrive %d\n",t));
2850             rdc16.last_block_no = cpu_to_be64(ha->hdr[t].size-1);
2851             rdc16.block_length  = cpu_to_be32(SECTOR_SIZE);
2852             gdth_copy_internal_data(hanum,scp,(char*)&rdc16,sizeof(gdth_rdcap16_data));
2853         } else { 
2854             scp->result = DID_ABORT << 16;
2855         }
2856         break;
2857 #endif
2858
2859       default:
2860         TRACE2(("Internal cache cmd 0x%x unknown\n",scp->cmnd[0]));
2861         break;
2862     }
2863
2864     if (!scp->SCp.have_data_in)
2865         scp->SCp.have_data_in++;
2866     else 
2867         return 1;
2868
2869     return 0;
2870 }
2871     
2872 static int gdth_fill_cache_cmd(int hanum,Scsi_Cmnd *scp,ushort hdrive)
2873 {
2874     register gdth_ha_str *ha;
2875     register gdth_cmd_str *cmdp;
2876     struct scatterlist *sl;
2877     ulong32 cnt, blockcnt;
2878     ulong64 no, blockno;
2879     dma_addr_t phys_addr;
2880     int i, cmd_index, read_write, sgcnt, mode64;
2881 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,13)
2882     struct page *page;
2883     ulong offset;
2884 #endif
2885
2886     ha = HADATA(gdth_ctr_tab[hanum]);
2887     cmdp = ha->pccb;
2888     TRACE(("gdth_fill_cache_cmd() cmd 0x%x cmdsize %d hdrive %d\n",
2889                  scp->cmnd[0],scp->cmd_len,hdrive));
2890
2891     if (ha->type==GDT_EISA && ha->cmd_cnt>0) 
2892         return 0;
2893
2894     mode64 = (ha->cache_feat & GDT_64BIT) ? TRUE : FALSE;
2895     /* test for READ_16, WRITE_16 if !mode64 ? ---
2896        not required, should not occur due to error return on 
2897        READ_CAPACITY_16 */
2898
2899     cmdp->Service = CACHESERVICE;
2900     cmdp->RequestBuffer = scp;
2901     /* search free command index */
2902     if (!(cmd_index=gdth_get_cmd_index(hanum))) {
2903         TRACE(("GDT: No free command index found\n"));
2904         return 0;
2905     }
2906     /* if it's the first command, set command semaphore */
2907     if (ha->cmd_cnt == 0)
2908         gdth_set_sema0(hanum);
2909
2910     /* fill command */
2911     read_write = 0;
2912     if (scp->SCp.sent_command != -1) 
2913         cmdp->OpCode = scp->SCp.sent_command;   /* special cache cmd. */
2914     else if (scp->cmnd[0] == RESERVE) 
2915         cmdp->OpCode = GDT_RESERVE_DRV;
2916     else if (scp->cmnd[0] == RELEASE)
2917         cmdp->OpCode = GDT_RELEASE_DRV;
2918     else if (scp->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
2919         if (scp->cmnd[4] & 1)                   /* prevent ? */
2920             cmdp->OpCode = GDT_MOUNT;
2921         else if (scp->cmnd[3] & 1)              /* removable drive ? */
2922             cmdp->OpCode = GDT_UNMOUNT;
2923         else
2924             cmdp->OpCode = GDT_FLUSH;
2925     } else if (scp->cmnd[0] == WRITE_6 || scp->cmnd[0] == WRITE_10 ||
2926                scp->cmnd[0] == WRITE_12 
2927 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
2928                || scp->cmnd[0] == WRITE_16
2929 #endif
2930     ) {
2931         read_write = 1;
2932         if (gdth_write_through || ((ha->hdr[hdrive].rw_attribs & 1) && 
2933                                    (ha->cache_feat & GDT_WR_THROUGH)))
2934             cmdp->OpCode = GDT_WRITE_THR;
2935         else
2936             cmdp->OpCode = GDT_WRITE;
2937     } else {
2938         read_write = 2;
2939         cmdp->OpCode = GDT_READ;
2940     }
2941
2942     cmdp->BoardNode = LOCALBOARD;
2943     if (mode64) {
2944         cmdp->u.cache64.DeviceNo = hdrive;
2945         cmdp->u.cache64.BlockNo  = 1;
2946         cmdp->u.cache64.sg_canz  = 0;
2947     } else {
2948         cmdp->u.cache.DeviceNo = hdrive;
2949         cmdp->u.cache.BlockNo  = 1;
2950         cmdp->u.cache.sg_canz  = 0;
2951     }
2952
2953     if (read_write) {
2954         if (scp->cmd_len == 16) {
2955             memcpy(&no, &scp->cmnd[2], sizeof(ulong64));
2956             blockno = be64_to_cpu(no);
2957             memcpy(&cnt, &scp->cmnd[10], sizeof(ulong32));
2958             blockcnt = be32_to_cpu(cnt);
2959         } else if (scp->cmd_len == 10) {
2960             memcpy(&no, &scp->cmnd[2], sizeof(ulong32));
2961             blockno = be32_to_cpu(no);
2962             memcpy(&cnt, &scp->cmnd[7], sizeof(ushort));
2963             blockcnt = be16_to_cpu(cnt);
2964         } else {
2965             memcpy(&no, &scp->cmnd[0], sizeof(ulong32));
2966             blockno = be32_to_cpu(no) & 0x001fffffUL;
2967             blockcnt= scp->cmnd[4]==0 ? 0x100 : scp->cmnd[4];
2968         }
2969         if (mode64) {
2970             cmdp->u.cache64.BlockNo = blockno;
2971             cmdp->u.cache64.BlockCnt = blockcnt;
2972         } else {
2973             cmdp->u.cache.BlockNo = (ulong32)blockno;
2974             cmdp->u.cache.BlockCnt = blockcnt;
2975         }
2976
2977         if (scp->use_sg) {
2978             sl = (struct scatterlist *)scp->request_buffer;
2979             sgcnt = scp->use_sg;
2980 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,13)
2981             scp->SCp.Status = GDTH_MAP_SG;
2982             scp->SCp.Message = (read_write == 1 ? 
2983                 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);   
2984             sgcnt = pci_map_sg(ha->pdev,sl,scp->use_sg,scp->SCp.Message);
2985             if (mode64) {
2986                 cmdp->u.cache64.DestAddr= (ulong64)-1;
2987                 cmdp->u.cache64.sg_canz = sgcnt;
2988                 for (i=0; i<sgcnt; ++i,++sl) {
2989                     cmdp->u.cache64.sg_lst[i].sg_ptr = sg_dma_address(sl);
2990 #ifdef GDTH_DMA_STATISTICS
2991                     if (cmdp->u.cache64.sg_lst[i].sg_ptr > (ulong64)0xffffffff)
2992                         ha->dma64_cnt++;
2993                     else
2994                         ha->dma32_cnt++;
2995 #endif
2996                     cmdp->u.cache64.sg_lst[i].sg_len = sg_dma_len(sl);
2997                 }
2998             } else {
2999                 cmdp->u.cache.DestAddr= 0xffffffff;
3000                 cmdp->u.cache.sg_canz = sgcnt;
3001                 for (i=0; i<sgcnt; ++i,++sl) {
3002                     cmdp->u.cache.sg_lst[i].sg_ptr = sg_dma_address(sl);
3003 #ifdef GDTH_DMA_STATISTICS
3004                     ha->dma32_cnt++;
3005 #endif
3006                     cmdp->u.cache.sg_lst[i].sg_len = sg_dma_len(sl);
3007                 }
3008             }
3009 #else
3010             if (mode64) {
3011                 cmdp->u.cache64.DestAddr= (ulong64)-1;
3012                 cmdp->u.cache64.sg_canz = sgcnt;
3013                 for (i=0; i<sgcnt; ++i,++sl) {
3014                     cmdp->u.cache64.sg_lst[i].sg_ptr = virt_to_bus(sl->address);
3015                     cmdp->u.cache64.sg_lst[i].sg_len = (ulong32)sl->length;
3016                 }
3017             } else {
3018                 cmdp->u.cache.DestAddr= 0xffffffff;
3019                 cmdp->u.cache.sg_canz = sgcnt;
3020                 for (i=0; i<sgcnt; ++i,++sl) {
3021                     cmdp->u.cache.sg_lst[i].sg_ptr = virt_to_bus(sl->address);
3022                     cmdp->u.cache.sg_lst[i].sg_len = (ulong32)sl->length;
3023                 }
3024             }
3025 #endif
3026
3027 #ifdef GDTH_STATISTICS
3028             if (max_sg < (ulong32)sgcnt) {
3029                 max_sg = (ulong32)sgcnt;
3030                 TRACE3(("GDT: max_sg = %d\n",max_sg));
3031             }
3032 #endif
3033
3034         } else {
3035 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,13)
3036             scp->SCp.Status = GDTH_MAP_SINGLE;
3037             scp->SCp.Message = (read_write == 1 ? 
3038                 PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);
3039             page = virt_to_page(scp->request_buffer);
3040             offset = (ulong)scp->request_buffer & ~PAGE_MASK;
3041             phys_addr = pci_map_page(ha->pdev,page,offset,
3042                                      scp->request_bufflen,scp->SCp.Message);
3043             scp->SCp.dma_handle = phys_addr;
3044 #else
3045             phys_addr = virt_to_bus(scp->request_buffer);
3046 #endif
3047             if (mode64) {
3048                 if (ha->cache_feat & SCATTER_GATHER) {
3049                     cmdp->u.cache64.DestAddr = (ulong64)-1;
3050                     cmdp->u.cache64.sg_canz = 1;
3051                     cmdp->u.cache64.sg_lst[0].sg_ptr = phys_addr;
3052                     cmdp->u.cache64.sg_lst[0].sg_len = scp->request_bufflen;
3053                     cmdp->u.cache64.sg_lst[1].sg_len = 0;
3054                 } else {
3055                     cmdp->u.cache64.DestAddr  = phys_addr;
3056                     cmdp->u.cache64.sg_canz= 0;
3057                 }
3058             } else {
3059                 if (ha->cache_feat & SCATTER_GATHER) {
3060                     cmdp->u.cache.DestAddr = 0xffffffff;
3061                     cmdp->u.cache.sg_canz = 1;
3062                     cmdp->u.cache.sg_lst[0].sg_ptr = phys_addr;
3063                     cmdp->u.cache.sg_lst[0].sg_len = scp->request_bufflen;
3064                     cmdp->u.cache.sg_lst[1].sg_len = 0;
3065                 } else {
3066                     cmdp->u.cache.DestAddr  = phys_addr;
3067                     cmdp->u.cache.sg_canz= 0;
3068                 }
3069             }
3070         }
3071     }
3072     /* evaluate command size, check space */
3073     if (mode64) {
3074         TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
3075                cmdp->u.cache64.DestAddr,cmdp->u.cache64.sg_canz,
3076                cmdp->u.cache64.sg_lst[0].sg_ptr,
3077                cmdp->u.cache64.sg_lst[0].sg_len));
3078         TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
3079                cmdp->OpCode,cmdp->u.cache64.BlockNo,cmdp->u.cache64.BlockCnt));
3080         ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) +
3081             (ushort)cmdp->u.cache64.sg_canz * sizeof(gdth_sg64_str);
3082     } else {
3083         TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
3084                cmdp->u.cache.DestAddr,cmdp->u.cache.sg_canz,
3085                cmdp->u.cache.sg_lst[0].sg_ptr,
3086                cmdp->u.cache.sg_lst[0].sg_len));
3087         TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
3088                cmdp->OpCode,cmdp->u.cache.BlockNo,cmdp->u.cache.BlockCnt));
3089         ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) +
3090             (ushort)cmdp->u.cache.sg_canz * sizeof(gdth_sg_str);
3091     }
3092     if (ha->cmd_len & 3)
3093         ha->cmd_len += (4 - (ha->cmd_len & 3));
3094
3095     if (ha->cmd_cnt > 0) {
3096         if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
3097             ha->ic_all_size) {
3098             TRACE2(("gdth_fill_cache() DPMEM overflow\n"));
3099             ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
3100             return 0;
3101         }
3102     }
3103
3104     /* copy command */
3105     gdth_copy_command(hanum);
3106     return cmd_index;
3107 }
3108
3109 static int gdth_fill_raw_cmd(int hanum,Scsi_Cmnd *scp,unchar b)
3110 {
3111     register gdth_ha_str *ha;
3112     register gdth_cmd_str *cmdp;
3113     struct scatterlist *sl;
3114     ushort i;
3115     dma_addr_t phys_addr, sense_paddr;
3116     int cmd_index, sgcnt, mode64;
3117     unchar t,l;
3118 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,13)
3119     struct page *page;
3120     ulong offset;
3121 #endif
3122
3123     ha = HADATA(gdth_ctr_tab[hanum]);
3124 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3125     t = scp->device->id;
3126     l = scp->device->lun;
3127 #else
3128     t = scp->target;
3129     l = scp->lun;
3130 #endif
3131     cmdp = ha->pccb;
3132     TRACE(("gdth_fill_raw_cmd() cmd 0x%x bus %d ID %d LUN %d\n",
3133            scp->cmnd[0],b,t,l));
3134
3135     if (ha->type==GDT_EISA && ha->cmd_cnt>0) 
3136         return 0;
3137
3138     mode64 = (ha->raw_feat & GDT_64BIT) ? TRUE : FALSE;
3139
3140     cmdp->Service = SCSIRAWSERVICE;
3141     cmdp->RequestBuffer = scp;
3142     /* search free command index */
3143     if (!(cmd_index=gdth_get_cmd_index(hanum))) {
3144         TRACE(("GDT: No free command index found\n"));
3145         return 0;
3146     }
3147     /* if it's the first command, set command semaphore */
3148     if (ha->cmd_cnt == 0)
3149         gdth_set_sema0(hanum);
3150
3151     /* fill command */  
3152     if (scp->SCp.sent_command != -1) {
3153         cmdp->OpCode           = scp->SCp.sent_command; /* special raw cmd. */
3154         cmdp->BoardNode        = LOCALBOARD;
3155         if (mode64) {
3156             cmdp->u.raw64.direction = (scp->SCp.phase >> 8);
3157             TRACE2(("special raw cmd 0x%x param 0x%x\n", 
3158                     cmdp->OpCode, cmdp->u.raw64.direction));
3159             /* evaluate command size */
3160             ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst);
3161         } else {
3162             cmdp->u.raw.direction  = (scp->SCp.phase >> 8);
3163             TRACE2(("special raw cmd 0x%x param 0x%x\n", 
3164                     cmdp->OpCode, cmdp->u.raw.direction));
3165             /* evaluate command size */
3166             ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst);
3167         }
3168
3169     } else {
3170 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,13)
3171         page = virt_to_page(scp->sense_buffer);
3172         offset = (ulong)scp->sense_buffer & ~PAGE_MASK;
3173         sense_paddr = pci_map_page(ha->pdev,page,offset,
3174                                    16,PCI_DMA_FROMDEVICE);
3175         scp->SCp.buffer = (struct scatterlist *)((ulong32)sense_paddr);
3176         /* high part, if 64bit */
3177         scp->host_scribble = (char *)(ulong32)((ulong64)sense_paddr >> 32);
3178 #else
3179         sense_paddr = virt_to_bus(scp->sense_buffer);
3180 #endif
3181         cmdp->OpCode           = GDT_WRITE;             /* always */
3182         cmdp->BoardNode        = LOCALBOARD;
3183         if (mode64) { 
3184             cmdp->u.raw64.reserved   = 0;
3185             cmdp->u.raw64.mdisc_time = 0;
3186             cmdp->u.raw64.mcon_time  = 0;
3187             cmdp->u.raw64.clen       = scp->cmd_len;
3188             cmdp->u.raw64.target     = t;
3189             cmdp->u.raw64.lun        = l;
3190             cmdp->u.raw64.bus        = b;
3191             cmdp->u.raw64.priority   = 0;
3192             cmdp->u.raw64.sdlen      = scp->request_bufflen;
3193             cmdp->u.raw64.sense_len  = 16;
3194             cmdp->u.raw64.sense_data = sense_paddr;
3195             cmdp->u.raw64.direction  = 
3196                 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
3197             memcpy(cmdp->u.raw64.cmd,scp->cmnd,16);
3198         } else {
3199             cmdp->u.raw.reserved   = 0;
3200             cmdp->u.raw.mdisc_time = 0;
3201             cmdp->u.raw.mcon_time  = 0;
3202             cmdp->u.raw.clen       = scp->cmd_len;
3203             cmdp->u.raw.target     = t;
3204             cmdp->u.raw.lun        = l;
3205             cmdp->u.raw.bus        = b;
3206             cmdp->u.raw.priority   = 0;
3207             cmdp->u.raw.link_p     = 0;
3208             cmdp->u.raw.sdlen      = scp->request_bufflen;
3209             cmdp->u.raw.sense_len  = 16;
3210             cmdp->u.raw.sense_data = sense_paddr;
3211             cmdp->u.raw.direction  = 
3212                 gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
3213             memcpy(cmdp->u.raw.cmd,scp->cmnd,12);
3214         }
3215
3216         if (scp->use_sg) {
3217             sl = (struct scatterlist *)scp->request_buffer;
3218             sgcnt = scp->use_sg;
3219 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,13)
3220             scp->SCp.Status = GDTH_MAP_SG;
3221             scp->SCp.Message = PCI_DMA_BIDIRECTIONAL; 
3222             sgcnt = pci_map_sg(ha->pdev,sl,scp->use_sg,scp->SCp.Message);
3223             if (mode64) {
3224                 cmdp->u.raw64.sdata = (ulong64)-1;
3225                 cmdp->u.raw64.sg_ranz = sgcnt;
3226                 for (i=0; i<sgcnt; ++i,++sl) {
3227                     cmdp->u.raw64.sg_lst[i].sg_ptr = sg_dma_address(sl);
3228 #ifdef GDTH_DMA_STATISTICS
3229                     if (cmdp->u.raw64.sg_lst[i].sg_ptr > (ulong64)0xffffffff)
3230                         ha->dma64_cnt++;
3231                     else
3232                         ha->dma32_cnt++;
3233 #endif
3234                     cmdp->u.raw64.sg_lst[i].sg_len = sg_dma_len(sl);
3235                 }
3236             } else {
3237                 cmdp->u.raw.sdata = 0xffffffff;
3238                 cmdp->u.raw.sg_ranz = sgcnt;
3239                 for (i=0; i<sgcnt; ++i,++sl) {
3240                     cmdp->u.raw.sg_lst[i].sg_ptr = sg_dma_address(sl);
3241 #ifdef GDTH_DMA_STATISTICS
3242                     ha->dma32_cnt++;
3243 #endif
3244                     cmdp->u.raw.sg_lst[i].sg_len = sg_dma_len(sl);
3245                 }
3246             }
3247 #else
3248             if (mode64) {
3249                 cmdp->u.raw64.sdata = (ulong64)-1;
3250                 cmdp->u.raw64.sg_ranz = sgcnt;
3251                 for (i=0; i<sgcnt; ++i,++sl) {
3252                     cmdp->u.raw64.sg_lst[i].sg_ptr = virt_to_bus(sl->address);
3253                     cmdp->u.raw64.sg_lst[i].sg_len = (ulong32)sl->length;
3254                 }
3255             } else {
3256                 cmdp->u.raw.sdata = 0xffffffff;
3257                 cmdp->u.raw.sg_ranz = sgcnt;
3258                 for (i=0; i<sgcnt; ++i,++sl) {
3259                     cmdp->u.raw.sg_lst[i].sg_ptr = virt_to_bus(sl->address);
3260                     cmdp->u.raw.sg_lst[i].sg_len = (ulong32)sl->length;
3261                 }
3262             }
3263 #endif
3264
3265 #ifdef GDTH_STATISTICS
3266             if (max_sg < sgcnt) {
3267                 max_sg = sgcnt;
3268                 TRACE3(("GDT: max_sg = %d\n",sgcnt));
3269             }
3270 #endif
3271
3272         } else {
3273 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,13)
3274             scp->SCp.Status = GDTH_MAP_SINGLE;
3275             scp->SCp.Message = PCI_DMA_BIDIRECTIONAL; 
3276             page = virt_to_page(scp->request_buffer);
3277             offset = (ulong)scp->request_buffer & ~PAGE_MASK;
3278             phys_addr = pci_map_page(ha->pdev,page,offset,
3279                                      scp->request_bufflen,scp->SCp.Message);
3280             scp->SCp.dma_handle = phys_addr;
3281 #else
3282             phys_addr = virt_to_bus(scp->request_buffer);
3283 #endif
3284             if (mode64) {
3285                 if (ha->raw_feat & SCATTER_GATHER) {
3286                     cmdp->u.raw64.sdata  = (ulong64)-1;
3287                     cmdp->u.raw64.sg_ranz= 1;
3288                     cmdp->u.raw64.sg_lst[0].sg_ptr = phys_addr;
3289                     cmdp->u.raw64.sg_lst[0].sg_len = scp->request_bufflen;
3290                     cmdp->u.raw64.sg_lst[1].sg_len = 0;
3291                 } else {
3292                     cmdp->u.raw64.sdata  = phys_addr;
3293                     cmdp->u.raw64.sg_ranz= 0;
3294                 }
3295             } else {
3296                 if (ha->raw_feat & SCATTER_GATHER) {
3297                     cmdp->u.raw.sdata  = 0xffffffff;
3298                     cmdp->u.raw.sg_ranz= 1;
3299                     cmdp->u.raw.sg_lst[0].sg_ptr = phys_addr;
3300                     cmdp->u.raw.sg_lst[0].sg_len = scp->request_bufflen;
3301                     cmdp->u.raw.sg_lst[1].sg_len = 0;
3302                 } else {
3303                     cmdp->u.raw.sdata  = phys_addr;
3304                     cmdp->u.raw.sg_ranz= 0;
3305                 }
3306             }
3307         }
3308         if (mode64) {
3309             TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
3310                    cmdp->u.raw64.sdata,cmdp->u.raw64.sg_ranz,
3311                    cmdp->u.raw64.sg_lst[0].sg_ptr,
3312                    cmdp->u.raw64.sg_lst[0].sg_len));
3313             /* evaluate command size */
3314             ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) +
3315                 (ushort)cmdp->u.raw64.sg_ranz * sizeof(gdth_sg64_str);
3316         } else {
3317             TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
3318                    cmdp->u.raw.sdata,cmdp->u.raw.sg_ranz,
3319                    cmdp->u.raw.sg_lst[0].sg_ptr,
3320                    cmdp->u.raw.sg_lst[0].sg_len));
3321             /* evaluate command size */
3322             ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) +
3323                 (ushort)cmdp->u.raw.sg_ranz * sizeof(gdth_sg_str);
3324         }
3325     }
3326     /* check space */
3327     if (ha->cmd_len & 3)
3328         ha->cmd_len += (4 - (ha->cmd_len & 3));
3329
3330     if (ha->cmd_cnt > 0) {
3331         if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
3332             ha->ic_all_size) {
3333             TRACE2(("gdth_fill_raw() DPMEM overflow\n"));
3334             ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
3335             return 0;
3336         }
3337     }
3338
3339     /* copy command */
3340     gdth_copy_command(hanum);
3341     return cmd_index;
3342 }
3343
3344 static int gdth_special_cmd(int hanum,Scsi_Cmnd *scp)
3345 {
3346     register gdth_ha_str *ha;
3347     register gdth_cmd_str *cmdp;
3348     int cmd_index;
3349
3350     ha  = HADATA(gdth_ctr_tab[hanum]);
3351     cmdp= ha->pccb;
3352     TRACE2(("gdth_special_cmd(): "));
3353
3354     if (ha->type==GDT_EISA && ha->cmd_cnt>0) 
3355         return 0;
3356
3357     memcpy( cmdp, scp->request_buffer, sizeof(gdth_cmd_str));
3358     cmdp->RequestBuffer = scp;
3359
3360     /* search free command index */
3361     if (!(cmd_index=gdth_get_cmd_index(hanum))) {
3362         TRACE(("GDT: No free command index found\n"));
3363         return 0;
3364     }
3365
3366     /* if it's the first command, set command semaphore */
3367     if (ha->cmd_cnt == 0)
3368        gdth_set_sema0(hanum);
3369
3370     /* evaluate command size, check space */
3371     if (cmdp->OpCode == GDT_IOCTL) {
3372         TRACE2(("IOCTL\n"));
3373         ha->cmd_len = 
3374             GDTOFFSOF(gdth_cmd_str,u.ioctl.p_param) + sizeof(ulong64);
3375     } else if (cmdp->Service == CACHESERVICE) {
3376         TRACE2(("cache command %d\n",cmdp->OpCode));
3377         if (ha->cache_feat & GDT_64BIT)
3378             ha->cmd_len = 
3379                 GDTOFFSOF(gdth_cmd_str,u.cache64.sg_lst) + sizeof(gdth_sg64_str);
3380         else
3381             ha->cmd_len = 
3382                 GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) + sizeof(gdth_sg_str);
3383     } else if (cmdp->Service == SCSIRAWSERVICE) {
3384         TRACE2(("raw command %d\n",cmdp->OpCode));
3385         if (ha->raw_feat & GDT_64BIT)
3386             ha->cmd_len = 
3387                 GDTOFFSOF(gdth_cmd_str,u.raw64.sg_lst) + sizeof(gdth_sg64_str);
3388         else
3389             ha->cmd_len = 
3390                 GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) + sizeof(gdth_sg_str);
3391     }
3392
3393     if (ha->cmd_len & 3)
3394         ha->cmd_len += (4 - (ha->cmd_len & 3));
3395
3396     if (ha->cmd_cnt > 0) {
3397         if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
3398             ha->ic_all_size) {
3399             TRACE2(("gdth_special_cmd() DPMEM overflow\n"));
3400             ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
3401             return 0;
3402         }
3403     }
3404
3405     /* copy command */
3406     gdth_copy_command(hanum);
3407     return cmd_index;
3408 }    
3409
3410
3411 /* Controller event handling functions */
3412 static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, ushort source, 
3413                                       ushort idx, gdth_evt_data *evt)
3414 {
3415     gdth_evt_str *e;
3416     struct timeval tv;
3417
3418     /* no GDTH_LOCK_HA() ! */
3419     TRACE2(("gdth_store_event() source %d idx %d\n", source, idx));
3420     if (source == 0)                        /* no source -> no event */
3421         return NULL;
3422
3423     if (ebuffer[elastidx].event_source == source &&
3424         ebuffer[elastidx].event_idx == idx &&
3425         ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
3426             !memcmp((char *)&ebuffer[elastidx].event_data.eu,
3427             (char *)&evt->eu, evt->size)) ||
3428         (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
3429             !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
3430             (char *)&evt->event_string)))) { 
3431         e = &ebuffer[elastidx];
3432         do_gettimeofday(&tv);
3433         e->last_stamp = tv.tv_sec;
3434         ++e->same_count;
3435     } else {
3436         if (ebuffer[elastidx].event_source != 0) {  /* entry not free ? */
3437             ++elastidx;
3438             if (elastidx == MAX_EVENTS)
3439                 elastidx = 0;
3440             if (elastidx == eoldidx) {              /* reached mark ? */
3441                 ++eoldidx;
3442                 if (eoldidx == MAX_EVENTS)
3443                     eoldidx = 0;
3444             }
3445         }
3446         e = &ebuffer[elastidx];
3447         e->event_source = source;
3448         e->event_idx = idx;
3449         do_gettimeofday(&tv);
3450         e->first_stamp = e->last_stamp = tv.tv_sec;
3451         e->same_count = 1;
3452         e->event_data = *evt;
3453         e->application = 0;
3454     }
3455     return e;
3456 }
3457
3458 static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr)
3459 {
3460     gdth_evt_str *e;
3461     int eindex;
3462     ulong flags;
3463
3464     TRACE2(("gdth_read_event() handle %d\n", handle));
3465     GDTH_LOCK_HA(ha, flags);
3466     if (handle == -1)
3467         eindex = eoldidx;
3468     else
3469         eindex = handle;
3470     estr->event_source = 0;
3471
3472     if (eindex >= MAX_EVENTS) {
3473         GDTH_UNLOCK_HA(ha, flags);
3474         return eindex;
3475     }
3476     e = &ebuffer[eindex];
3477     if (e->event_source != 0) {
3478         if (eindex != elastidx) {
3479             if (++eindex == MAX_EVENTS)
3480                 eindex = 0;
3481         } else {
3482             eindex = -1;
3483         }
3484         memcpy(estr, e, sizeof(gdth_evt_str));
3485     }
3486     GDTH_UNLOCK_HA(ha, flags);
3487     return eindex;
3488 }
3489
3490 static void gdth_readapp_event(gdth_ha_str *ha,
3491                                unchar application, gdth_evt_str *estr)
3492 {
3493     gdth_evt_str *e;
3494     int eindex;
3495     ulong flags;
3496     unchar found = FALSE;
3497
3498     TRACE2(("gdth_readapp_event() app. %d\n", application));
3499     GDTH_LOCK_HA(ha, flags);
3500     eindex = eoldidx;
3501     for (;;) {
3502         e = &ebuffer[eindex];
3503         if (e->event_source == 0)
3504             break;
3505         if ((e->application & application) == 0) {
3506             e->application |= application;
3507             found = TRUE;
3508             break;
3509         }
3510         if (eindex == elastidx)
3511             break;
3512         if (++eindex == MAX_EVENTS)
3513             eindex = 0;
3514     }
3515     if (found)
3516         memcpy(estr, e, sizeof(gdth_evt_str));
3517     else
3518         estr->event_source = 0;
3519     GDTH_UNLOCK_HA(ha, flags);
3520 }
3521
3522 static void gdth_clear_events(void)
3523 {
3524     TRACE(("gdth_clear_events()"));
3525
3526     eoldidx = elastidx = 0;
3527     ebuffer[0].event_source = 0;
3528 }
3529
3530
3531 /* SCSI interface functions */
3532
3533 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3534 static irqreturn_t gdth_interrupt(int irq,void *dev_id,struct pt_regs *regs)
3535 #else
3536 static void gdth_interrupt(int irq,void *dev_id,struct pt_regs *regs)
3537 #endif
3538 {
3539     register gdth_ha_str *ha;
3540     gdt6m_dpram_str __iomem *dp6m_ptr = NULL;
3541     gdt6_dpram_str __iomem *dp6_ptr;
3542     gdt2_dpram_str __iomem *dp2_ptr;
3543     Scsi_Cmnd *scp;
3544     int hanum, rval, i;
3545     unchar IStatus;
3546     ushort Service;
3547     ulong flags = 0;
3548 #ifdef INT_COAL
3549     int coalesced = FALSE;
3550     int next = FALSE;
3551     gdth_coal_status *pcs = NULL;
3552     int act_int_coal = 0;       
3553 #endif
3554
3555     TRACE(("gdth_interrupt() IRQ %d\n",irq));
3556
3557     /* if polling and not from gdth_wait() -> return */
3558     if (gdth_polling) {
3559         if (!gdth_from_wait) {
3560 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3561             return IRQ_HANDLED;
3562 #else
3563             return;             
3564 #endif
3565         }
3566     }
3567
3568     if (!gdth_polling)
3569         GDTH_LOCK_HA((gdth_ha_str *)dev_id,flags);
3570     wait_index = 0;
3571
3572     /* search controller */
3573     if ((hanum = gdth_get_status(&IStatus,irq)) == -1) {
3574         /* spurious interrupt */
3575         if (!gdth_polling)
3576             GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
3577 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3578             return IRQ_HANDLED;
3579 #else
3580             return;             
3581 #endif
3582     }
3583     ha = HADATA(gdth_ctr_tab[hanum]);
3584
3585 #ifdef GDTH_STATISTICS
3586     ++act_ints;
3587 #endif
3588
3589 #ifdef INT_COAL
3590     /* See if the fw is returning coalesced status */
3591     if (IStatus == COALINDEX) {
3592         /* Coalesced status.  Setup the initial status 
3593            buffer pointer and flags */
3594         pcs = ha->coal_stat;
3595         coalesced = TRUE;        
3596         next = TRUE;
3597     }
3598
3599     do {
3600         if (coalesced) {
3601             /* For coalesced requests all status
3602                information is found in the status buffer */
3603             IStatus = (unchar)(pcs->status & 0xff);
3604         }
3605 #endif
3606     
3607         if (ha->type == GDT_EISA) {
3608             if (IStatus & 0x80) {                       /* error flag */
3609                 IStatus &= ~0x80;
3610                 ha->status = inw(ha->bmic + MAILBOXREG+8);
3611                 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3612             } else                                      /* no error */
3613                 ha->status = S_OK;
3614             ha->info = inl(ha->bmic + MAILBOXREG+12);
3615             ha->service = inw(ha->bmic + MAILBOXREG+10);
3616             ha->info2 = inl(ha->bmic + MAILBOXREG+4);
3617
3618             outb(0xff, ha->bmic + EDOORREG);    /* acknowledge interrupt */
3619             outb(0x00, ha->bmic + SEMA1REG);    /* reset status semaphore */
3620         } else if (ha->type == GDT_ISA) {
3621             dp2_ptr = ha->brd;
3622             if (IStatus & 0x80) {                       /* error flag */
3623                 IStatus &= ~0x80;
3624                 ha->status = gdth_readw(&dp2_ptr->u.ic.Status);
3625                 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3626             } else                                      /* no error */
3627                 ha->status = S_OK;
3628             ha->info = gdth_readl(&dp2_ptr->u.ic.Info[0]);
3629             ha->service = gdth_readw(&dp2_ptr->u.ic.Service);
3630             ha->info2 = gdth_readl(&dp2_ptr->u.ic.Info[1]);
3631
3632             gdth_writeb(0xff, &dp2_ptr->io.irqdel); /* acknowledge interrupt */
3633             gdth_writeb(0, &dp2_ptr->u.ic.Cmd_Index);/* reset command index */
3634             gdth_writeb(0, &dp2_ptr->io.Sema1);     /* reset status semaphore */
3635         } else if (ha->type == GDT_PCI) {
3636             dp6_ptr = ha->brd;
3637             if (IStatus & 0x80) {                       /* error flag */
3638                 IStatus &= ~0x80;
3639                 ha->status = gdth_readw(&dp6_ptr->u.ic.Status);
3640                 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3641             } else                                      /* no error */
3642                 ha->status = S_OK;
3643             ha->info = gdth_readl(&dp6_ptr->u.ic.Info[0]);
3644             ha->service = gdth_readw(&dp6_ptr->u.ic.Service);
3645             ha->info2 = gdth_readl(&dp6_ptr->u.ic.Info[1]);
3646
3647             gdth_writeb(0xff, &dp6_ptr->io.irqdel); /* acknowledge interrupt */
3648             gdth_writeb(0, &dp6_ptr->u.ic.Cmd_Index);/* reset command index */
3649             gdth_writeb(0, &dp6_ptr->io.Sema1);     /* reset status semaphore */
3650         } else if (ha->type == GDT_PCINEW) {
3651             if (IStatus & 0x80) {                       /* error flag */
3652                 IStatus &= ~0x80;
3653                 ha->status = inw(PTR2USHORT(&ha->plx->status));
3654                 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3655             } else
3656                 ha->status = S_OK;
3657             ha->info = inl(PTR2USHORT(&ha->plx->info[0]));
3658             ha->service = inw(PTR2USHORT(&ha->plx->service));
3659             ha->info2 = inl(PTR2USHORT(&ha->plx->info[1]));
3660
3661             outb(0xff, PTR2USHORT(&ha->plx->edoor_reg)); 
3662             outb(0x00, PTR2USHORT(&ha->plx->sema1_reg)); 
3663         } else if (ha->type == GDT_PCIMPR) {
3664             dp6m_ptr = ha->brd;
3665             if (IStatus & 0x80) {                       /* error flag */
3666                 IStatus &= ~0x80;
3667 #ifdef INT_COAL
3668                 if (coalesced)
3669                     ha->status = pcs->ext_status && 0xffff;
3670                 else 
3671 #endif
3672                     ha->status = gdth_readw(&dp6m_ptr->i960r.status);
3673                 TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
3674             } else                                      /* no error */
3675                 ha->status = S_OK;
3676 #ifdef INT_COAL
3677             /* get information */
3678             if (coalesced) {    
3679                 ha->info = pcs->info0;
3680                 ha->info2 = pcs->info1;
3681                 ha->service = (pcs->ext_status >> 16) && 0xffff;
3682             } else
3683 #endif
3684             {
3685                 ha->info = gdth_readl(&dp6m_ptr->i960r.info[0]);
3686                 ha->service = gdth_readw(&dp6m_ptr->i960r.service);
3687                 ha->info2 = gdth_readl(&dp6m_ptr->i960r.info[1]);
3688             }
3689             /* event string */
3690             if (IStatus == ASYNCINDEX) {
3691                 if (ha->service != SCREENSERVICE &&
3692                     (ha->fw_vers & 0xff) >= 0x1a) {
3693                     ha->dvr.severity = gdth_readb
3694                         (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.severity);
3695                     for (i = 0; i < 256; ++i) {
3696                         ha->dvr.event_string[i] = gdth_readb
3697                             (&((gdt6m_dpram_str __iomem *)ha->brd)->i960r.evt_str[i]);
3698                         if (ha->dvr.event_string[i] == 0)
3699                             break;
3700                     }
3701                 }
3702             }
3703 #ifdef INT_COAL
3704             /* Make sure that non coalesced interrupts get cleared
3705                before being handled by gdth_async_event/gdth_sync_event */
3706             if (!coalesced)
3707 #endif                          
3708             {
3709                 gdth_writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
3710                 gdth_writeb(0, &dp6m_ptr->i960r.sema1_reg);
3711             }
3712         } else {
3713             TRACE2(("gdth_interrupt() unknown controller type\n"));
3714             if (!gdth_polling)
3715                 GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
3716 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3717             return IRQ_HANDLED;
3718 #else
3719             return;             
3720 #endif
3721         }
3722
3723         TRACE(("gdth_interrupt() index %d stat %d info %d\n",
3724                IStatus,ha->status,ha->info));
3725
3726         if (gdth_from_wait) {
3727             wait_hanum = hanum;
3728             wait_index = (int)IStatus;
3729         }
3730
3731         if (IStatus == ASYNCINDEX) {
3732             TRACE2(("gdth_interrupt() async. event\n"));
3733             gdth_async_event(hanum);
3734             if (!gdth_polling)
3735                 GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
3736             gdth_next(hanum);
3737 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3738             return IRQ_HANDLED;
3739 #else
3740             return;             
3741 #endif
3742         } 
3743
3744         if (IStatus == SPEZINDEX) {
3745             TRACE2(("Service unknown or not initialized !\n"));
3746             ha->dvr.size = sizeof(ha->dvr.eu.driver);
3747             ha->dvr.eu.driver.ionode = hanum;
3748             gdth_store_event(ha, ES_DRIVER, 4, &ha->dvr);
3749             if (!gdth_polling)
3750                 GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
3751 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3752             return IRQ_HANDLED;
3753 #else
3754             return;             
3755 #endif
3756         }
3757         scp     = ha->cmd_tab[IStatus-2].cmnd;
3758         Service = ha->cmd_tab[IStatus-2].service;
3759         ha->cmd_tab[IStatus-2].cmnd = UNUSED_CMND;
3760         if (scp == UNUSED_CMND) {
3761             TRACE2(("gdth_interrupt() index to unused command (%d)\n",IStatus));
3762             ha->dvr.size = sizeof(ha->dvr.eu.driver);
3763             ha->dvr.eu.driver.ionode = hanum;
3764             ha->dvr.eu.driver.index = IStatus;
3765             gdth_store_event(ha, ES_DRIVER, 1, &ha->dvr);
3766             if (!gdth_polling)
3767                 GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
3768 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3769             return IRQ_HANDLED;
3770 #else
3771             return;             
3772 #endif
3773         }
3774         if (scp == INTERNAL_CMND) {
3775             TRACE(("gdth_interrupt() answer to internal command\n"));
3776             if (!gdth_polling)
3777                 GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
3778 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3779             return IRQ_HANDLED;
3780 #else
3781             return;             
3782 #endif
3783         }
3784
3785         TRACE(("gdth_interrupt() sync. status\n"));
3786         rval = gdth_sync_event(hanum,Service,IStatus,scp);
3787         if (!gdth_polling)
3788             GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
3789         if (rval == 2) {
3790             gdth_putq(hanum,scp,scp->SCp.this_residual);
3791         } else if (rval == 1) {
3792 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3793             GDTH_LOCK_SCSI_DONE(scp->device->host, flags);
3794             scp->scsi_done(scp);
3795             GDTH_UNLOCK_SCSI_DONE(scp->device->host, flags);
3796 #else
3797             GDTH_LOCK_SCSI_DONE(flags);
3798             scp->scsi_done(scp);
3799             GDTH_UNLOCK_SCSI_DONE(flags);
3800 #endif
3801         }
3802
3803 #ifdef INT_COAL
3804         if (coalesced) {
3805             /* go to the next status in the status buffer */
3806             ++pcs;
3807 #ifdef GDTH_STATISTICS
3808             ++act_int_coal;
3809             if (act_int_coal > max_int_coal) {
3810                 max_int_coal = act_int_coal;
3811                 printk("GDT: max_int_coal = %d\n",(ushort)max_int_coal);
3812             }
3813 #endif      
3814             /* see if there is another status */
3815             if (pcs->status == 0)    
3816                 /* Stop the coalesce loop */
3817                 next = FALSE;
3818         }
3819     } while (next);
3820
3821     /* coalescing only for new GDT_PCIMPR controllers available */      
3822     if (ha->type == GDT_PCIMPR && coalesced) {
3823         gdth_writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
3824         gdth_writeb(0, &dp6m_ptr->i960r.sema1_reg);
3825     }
3826 #endif
3827
3828     gdth_next(hanum);
3829 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3830     return IRQ_HANDLED;
3831 #endif
3832 }
3833
3834 static int gdth_sync_event(int hanum,int service,unchar index,Scsi_Cmnd *scp)
3835 {
3836     register gdth_ha_str *ha;
3837     gdth_msg_str *msg;
3838     gdth_cmd_str *cmdp;
3839     unchar b, t;
3840
3841     ha   = HADATA(gdth_ctr_tab[hanum]);
3842     cmdp = ha->pccb;
3843     TRACE(("gdth_sync_event() serv %d status %d\n",
3844            service,ha->status));
3845
3846     if (service == SCREENSERVICE) {
3847         msg  = ha->pmsg;
3848         TRACE(("len: %d, answer: %d, ext: %d, alen: %d\n",
3849                msg->msg_len,msg->msg_answer,msg->msg_ext,msg->msg_alen));
3850         if (msg->msg_len > MSGLEN+1)
3851             msg->msg_len = MSGLEN+1;
3852         if (msg->msg_len)
3853             if (!(msg->msg_answer && msg->msg_ext)) {
3854                 msg->msg_text[msg->msg_len] = '\0';
3855                 printk("%s",msg->msg_text);
3856             }
3857
3858         if (msg->msg_ext && !msg->msg_answer) {
3859             while (gdth_test_busy(hanum))
3860                 gdth_delay(0);
3861             cmdp->Service       = SCREENSERVICE;
3862             cmdp->RequestBuffer = SCREEN_CMND;
3863             gdth_get_cmd_index(hanum);
3864             gdth_set_sema0(hanum);
3865             cmdp->OpCode        = GDT_READ;
3866             cmdp->BoardNode     = LOCALBOARD;
3867             cmdp->u.screen.reserved  = 0;
3868             cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
3869             cmdp->u.screen.su.msg.msg_addr  = ha->msg_phys;
3870             ha->cmd_offs_dpmem = 0;
3871             ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr) 
3872                 + sizeof(ulong64);
3873             ha->cmd_cnt = 0;
3874             gdth_copy_command(hanum);
3875             gdth_release_event(hanum);
3876             return 0;
3877         }
3878
3879         if (msg->msg_answer && msg->msg_alen) {
3880             /* default answers (getchar() not possible) */
3881             if (msg->msg_alen == 1) {
3882                 msg->msg_alen = 0;
3883                 msg->msg_len = 1;
3884                 msg->msg_text[0] = 0;
3885             } else {
3886                 msg->msg_alen -= 2;
3887                 msg->msg_len = 2;
3888                 msg->msg_text[0] = 1;
3889                 msg->msg_text[1] = 0;
3890             }
3891             msg->msg_ext    = 0;
3892             msg->msg_answer = 0;
3893             while (gdth_test_busy(hanum))
3894                 gdth_delay(0);
3895             cmdp->Service       = SCREENSERVICE;
3896             cmdp->RequestBuffer = SCREEN_CMND;
3897             gdth_get_cmd_index(hanum);
3898             gdth_set_sema0(hanum);
3899             cmdp->OpCode        = GDT_WRITE;
3900             cmdp->BoardNode     = LOCALBOARD;
3901             cmdp->u.screen.reserved  = 0;
3902             cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
3903             cmdp->u.screen.su.msg.msg_addr  = ha->msg_phys;
3904             ha->cmd_offs_dpmem = 0;
3905             ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr) 
3906                 + sizeof(ulong64);
3907             ha->cmd_cnt = 0;
3908             gdth_copy_command(hanum);
3909             gdth_release_event(hanum);
3910             return 0;
3911         }
3912         printk("\n");
3913
3914     } else {
3915 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
3916         b = virt_ctr ? NUMDATA(scp->device->host)->busnum : scp->device->channel;
3917         t = scp->device->id;
3918 #else
3919         b = virt_ctr ? NUMDATA(scp->host)->busnum : scp->channel;
3920         t = scp->target;
3921 #endif
3922         if (scp->SCp.sent_command == -1 && b != ha->virt_bus) {
3923             ha->raw[BUS_L2P(ha,b)].io_cnt[t]--;
3924         }
3925         /* cache or raw service */
3926         if (ha->status == S_BSY) {
3927             TRACE2(("Controller busy -> retry !\n"));
3928             if (scp->SCp.sent_command == GDT_MOUNT)
3929                 scp->SCp.sent_command = GDT_CLUST_INFO;
3930             /* retry */
3931             return 2;
3932         }
3933 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,13)
3934         if (scp->SCp.Status == GDTH_MAP_SG) 
3935             pci_unmap_sg(ha->pdev,scp->request_buffer,
3936                          scp->use_sg,scp->SCp.Message);
3937         else if (scp->SCp.Status == GDTH_MAP_SINGLE) 
3938             pci_unmap_page(ha->pdev,scp->SCp.dma_handle,
3939                            scp->request_bufflen,scp->SCp.Message);
3940         if (scp->SCp.buffer) {
3941             dma_addr_t addr;
3942             addr = (dma_addr_t)(ulong32)scp->SCp.buffer;
3943             if (scp->host_scribble)
3944                 addr += (dma_addr_t)((ulong64)(ulong32)scp->host_scribble << 32);               
3945             pci_unmap_page(ha->pdev,addr,16,PCI_DMA_FROMDEVICE);
3946         }
3947 #endif
3948         if (ha->status == S_OK) {
3949             scp->SCp.Status = S_OK;
3950             scp->SCp.Message = ha->info;
3951             if (scp->SCp.sent_command != -1) {
3952                 TRACE2(("gdth_sync_event(): special cmd 0x%x OK\n",
3953                         scp->SCp.sent_command));
3954                 /* special commands GDT_CLUST_INFO/GDT_MOUNT ? */
3955                 if (scp->SCp.sent_command == GDT_CLUST_INFO) {
3956                     ha->hdr[t].cluster_type = (unchar)ha->info;
3957                     if (!(ha->hdr[t].cluster_type & 
3958                         CLUSTER_MOUNTED)) {
3959                         /* NOT MOUNTED -> MOUNT */
3960                         scp->SCp.sent_command = GDT_MOUNT;
3961                         if (ha->hdr[t].cluster_type & 
3962                             CLUSTER_RESERVED) {
3963                             /* cluster drive RESERVED (on the other node) */
3964                             scp->SCp.phase = -2;      /* reservation conflict */
3965                         }
3966                     } else {
3967                         scp->SCp.sent_command = -1;
3968                     }
3969                 } else {
3970                     if (scp->SCp.sent_command == GDT_MOUNT) {
3971                         ha->hdr[t].cluster_type |= CLUSTER_MOUNTED;
3972                         ha->hdr[t].media_changed = TRUE;
3973                     } else if (scp->SCp.sent_command == GDT_UNMOUNT) {
3974                         ha->hdr[t].cluster_type &= ~CLUSTER_MOUNTED;
3975                         ha->hdr[t].media_changed = TRUE;
3976                     } 
3977                     scp->SCp.sent_command = -1;
3978                 }
3979                 /* retry */
3980                 scp->SCp.this_residual = HIGH_PRI;
3981                 return 2;
3982             } else {
3983                 /* RESERVE/RELEASE ? */
3984                 if (scp->cmnd[0] == RESERVE) {
3985                     ha->hdr[t].cluster_type |= CLUSTER_RESERVED;
3986                 } else if (scp->cmnd[0] == RELEASE) {
3987                     ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
3988                 }           
3989                 scp->result = DID_OK << 16;
3990                 scp->sense_buffer[0] = 0;
3991             }
3992         } else {
3993             scp->SCp.Status = ha->status;
3994             scp->SCp.Message = ha->info;
3995
3996             if (scp->SCp.sent_command != -1) {
3997                 TRACE2(("gdth_sync_event(): special cmd 0x%x error 0x%x\n",
3998                         scp->SCp.sent_command, ha->status));
3999                 if (scp->SCp.sent_command == GDT_SCAN_START ||
4000                     scp->SCp.sent_command == GDT_SCAN_END) {
4001                     scp->SCp.sent_command = -1;
4002                     /* retry */
4003                     scp->SCp.this_residual = HIGH_PRI;
4004                     return 2;
4005                 }
4006                 memset((char*)scp->sense_buffer,0,16);
4007                 scp->sense_buffer[0] = 0x70;
4008                 scp->sense_buffer[2] = NOT_READY;
4009                 scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
4010             } else if (service == CACHESERVICE) {
4011                 if (ha->status == S_CACHE_UNKNOWN &&
4012                     (ha->hdr[t].cluster_type & 
4013                      CLUSTER_RESERVE_STATE) == CLUSTER_RESERVE_STATE) {
4014                     /* bus reset -> force GDT_CLUST_INFO */
4015                     ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
4016                 }
4017                 memset((char*)scp->sense_buffer,0,16);
4018                 if (ha->status == (ushort)S_CACHE_RESERV) {
4019                     scp->result = (DID_OK << 16) | (RESERVATION_CONFLICT << 1);
4020                 } else {
4021                     scp->sense_buffer[0] = 0x70;
4022                     scp->sense_buffer[2] = NOT_READY;
4023                     scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
4024                 }
4025                 if (scp->done != gdth_scsi_done) {
4026                     ha->dvr.size = sizeof(ha->dvr.eu.sync);
4027                     ha->dvr.eu.sync.ionode  = hanum;
4028                     ha->dvr.eu.sync.service = service;
4029                     ha->dvr.eu.sync.status  = ha->status;
4030                     ha->dvr.eu.sync.info    = ha->info;
4031                     ha->dvr.eu.sync.hostdrive = t;
4032                     if (ha->status >= 0x8000)
4033                         gdth_store_event(ha, ES_SYNC, 0, &ha->dvr);
4034                     else
4035                         gdth_store_event(ha, ES_SYNC, service, &ha->dvr);
4036                 }
4037             } else {
4038                 /* sense buffer filled from controller firmware (DMA) */
4039                 if (ha->status != S_RAW_SCSI || ha->info >= 0x100) {
4040                     scp->result = DID_BAD_TARGET << 16;
4041                 } else {
4042                     scp->result = (DID_OK << 16) | ha->info;
4043                 }
4044             }
4045         }
4046         if (!scp->SCp.have_data_in)
4047             scp->SCp.have_data_in++;
4048         else 
4049             return 1;
4050     }
4051
4052     return 0;
4053 }
4054
4055 static char *async_cache_tab[] = {
4056 /* 0*/  "\011\000\002\002\002\004\002\006\004"
4057         "GDT HA %u, service %u, async. status %u/%lu unknown",
4058 /* 1*/  "\011\000\002\002\002\004\002\006\004"
4059         "GDT HA %u, service %u, async. status %u/%lu unknown",
4060 /* 2*/  "\005\000\002\006\004"
4061         "GDT HA %u, Host Drive %lu not ready",
4062 /* 3*/  "\005\000\002\006\004"
4063         "GDT HA %u, Host Drive %lu: REASSIGN not successful and/or data error on reassigned blocks. Drive may crash in the future and should be replaced",
4064 /* 4*/  "\005\000\002\006\004"
4065         "GDT HA %u, mirror update on Host Drive %lu failed",
4066 /* 5*/  "\005\000\002\006\004"
4067         "GDT HA %u, Mirror Drive %lu failed",
4068 /* 6*/  "\005\000\002\006\004"
4069         "GDT HA %u, Mirror Drive %lu: REASSIGN not successful and/or data error on reassigned blocks. Drive may crash in the future and should be replaced",
4070 /* 7*/  "\005\000\002\006\004"
4071         "GDT HA %u, Host Drive %lu write protected",
4072 /* 8*/  "\005\000\002\006\004"
4073         "GDT HA %u, media changed in Host Drive %lu",
4074 /* 9*/  "\005\000\002\006\004"
4075         "GDT HA %u, Host Drive %lu is offline",
4076 /*10*/  "\005\000\002\006\004"
4077         "GDT HA %u, media change of Mirror Drive %lu",
4078 /*11*/  "\005\000\002\006\004"
4079         "GDT HA %u, Mirror Drive %lu is write protected",
4080 /*12*/  "\005\000\002\006\004"
4081         "GDT HA %u, general error on Host Drive %lu. Please check the devices of this drive!",
4082 /*13*/  "\007\000\002\006\002\010\002"
4083         "GDT HA %u, Array Drive %u: Cache Drive %u failed",
4084 /*14*/  "\005\000\002\006\002"
4085         "GDT HA %u, Array Drive %u: FAIL state entered",
4086 /*15*/  "\005\000\002\006\002"
4087         "GDT HA %u, Array Drive %u: error",
4088 /*16*/  "\007\000\002\006\002\010\002"
4089         "GDT HA %u, Array Drive %u: failed drive replaced by Cache Drive %u",
4090 /*17*/  "\005\000\002\006\002"
4091         "GDT HA %u, Array Drive %u: parity build failed",
4092 /*18*/  "\005\000\002\006\002"
4093         "GDT HA %u, Array Drive %u: drive rebuild failed",
4094 /*19*/  "\005\000\002\010\002"
4095         "GDT HA %u, Test of Hot Fix %u failed",
4096 /*20*/  "\005\000\002\006\002"
4097         "GDT HA %u, Array Drive %u: drive build finished successfully",
4098 /*21*/  "\005\000\002\006\002"
4099         "GDT HA %u, Array Drive %u: drive rebuild finished successfully",
4100 /*22*/  "\007\000\002\006\002\010\002"
4101         "GDT HA %u, Array Drive %u: Hot Fix %u activated",
4102 /*23*/  "\005\000\002\006\002"
4103         "GDT HA %u, Host Drive %u: processing of i/o aborted due to serious drive error",
4104 /*24*/  "\005\000\002\010\002"
4105         "GDT HA %u, mirror update on Cache Drive %u completed",
4106 /*25*/  "\005\000\002\010\002"
4107         "GDT HA %u, mirror update on Cache Drive %lu failed",
4108 /*26*/  "\005\000\002\006\002"
4109         "GDT HA %u, Array Drive %u: drive rebuild started",
4110 /*27*/  "\005\000\002\012\001"
4111         "GDT HA %u, Fault bus %u: SHELF OK detected",
4112 /*28*/  "\005\000\002\012\001"
4113         "GDT HA %u, Fault bus %u: SHELF not OK detected",
4114 /*29*/  "\007\000\002\012\001\013\001"
4115         "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug started",
4116 /*30*/  "\007\000\002\012\001\013\001"
4117         "GDT HA %u, Fault bus %u, ID %u: new disk detected",
4118 /*31*/  "\007\000\002\012\001\013\001"
4119         "GDT HA %u, Fault bus %u, ID %u: old disk detected",
4120 /*32*/  "\007\000\002\012\001\013\001"
4121         "GDT HA %u, Fault bus %u, ID %u: plugging an active disk is invalid",
4122 /*33*/  "\007\000\002\012\001\013\001"
4123         "GDT HA %u, Fault bus %u, ID %u: invalid device detected",
4124 /*34*/  "\011\000\002\012\001\013\001\006\004"
4125         "GDT HA %u, Fault bus %u, ID %u: insufficient disk capacity (%lu MB required)",
4126 /*35*/  "\007\000\002\012\001\013\001"
4127         "GDT HA %u, Fault bus %u, ID %u: disk write protected",
4128 /*36*/  "\007\000\002\012\001\013\001"
4129         "GDT HA %u, Fault bus %u, ID %u: disk not available",
4130 /*37*/  "\007\000\002\012\001\006\004"
4131         "GDT HA %u, Fault bus %u: swap detected (%lu)",
4132 /*38*/  "\007\000\002\012\001\013\001"
4133         "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug finished successfully",
4134 /*39*/  "\007\000\002\012\001\013\001"
4135         "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted due to user Hot Plug",
4136 /*40*/  "\007\000\002\012\001\013\001"
4137         "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted",
4138 /*41*/  "\007\000\002\012\001\013\001"
4139         "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug for Hot Fix started",
4140 /*42*/  "\005\000\002\006\002"
4141         "GDT HA %u, Array Drive %u: drive build started",
4142 /*43*/  "\003\000\002"
4143         "GDT HA %u, DRAM parity error detected",
4144 /*44*/  "\005\000\002\006\002"
4145         "GDT HA %u, Mirror Drive %u: update started",
4146 /*45*/  "\007\000\002\006\002\010\002"
4147         "GDT HA %u, Mirror Drive %u: Hot Fix %u activated",
4148 /*46*/  "\005\000\002\006\002"
4149         "GDT HA %u, Array Drive %u: no matching Pool Hot Fix Drive available",
4150 /*47*/  "\005\000\002\006\002"
4151         "GDT HA %u, Array Drive %u: Pool Hot Fix Drive available",
4152 /*48*/  "\005\000\002\006\002"
4153         "GDT HA %u, Mirror Drive %u: no matching Pool Hot Fix Drive available",
4154 /*49*/  "\005\000\002\006\002"
4155         "GDT HA %u, Mirror Drive %u: Pool Hot Fix Drive available",
4156 /*50*/  "\007\000\002\012\001\013\001"
4157         "GDT HA %u, SCSI bus %u, ID %u: IGNORE_WIDE_RESIDUE message received",
4158 /*51*/  "\005\000\002\006\002"
4159         "GDT HA %u, Array Drive %u: expand started",
4160 /*52*/  "\005\000\002\006\002"
4161         "GDT HA %u, Array Drive %u: expand finished successfully",
4162 /*53*/  "\005\000\002\006\002"
4163         "GDT HA %u, Array Drive %u: expand failed",
4164 /*54*/  "\003\000\002"
4165         "GDT HA %u, CPU temperature critical",
4166 /*55*/  "\003\000\002"
4167         "GDT HA %u, CPU temperature OK",
4168 /*56*/  "\005\000\002\006\004"
4169         "GDT HA %u, Host drive %lu created",
4170 /*57*/  "\005\000\002\006\002"
4171         "GDT HA %u, Array Drive %u: expand restarted",
4172 /*58*/  "\005\000\002\006\002"
4173         "GDT HA %u, Array Drive %u: expand stopped",
4174 /*59*/  "\005\000\002\010\002"
4175         "GDT HA %u, Mirror Drive %u: drive build quited",
4176 /*60*/  "\005\000\002\006\002"
4177         "GDT HA %u, Array Drive %u: parity build quited",
4178 /*61*/  "\005\000\002\006\002"
4179         "GDT HA %u, Array Drive %u: drive rebuild quited",
4180 /*62*/  "\005\000\002\006\002"
4181         "GDT HA %u, Array Drive %u: parity verify started",
4182 /*63*/  "\005\000\002\006\002"
4183         "GDT HA %u, Array Drive %u: parity verify done",
4184 /*64*/  "\005\000\002\006\002"
4185         "GDT HA %u, Array Drive %u: parity verify failed",
4186 /*65*/  "\005\000\002\006\002"
4187         "GDT HA %u, Array Drive %u: parity error detected",
4188 /*66*/  "\005\000\002\006\002"
4189         "GDT HA %u, Array Drive %u: parity verify quited",
4190 /*67*/  "\005\000\002\006\002"
4191         "GDT HA %u, Host Drive %u reserved",
4192 /*68*/  "\005\000\002\006\002"
4193         "GDT HA %u, Host Drive %u mounted and released",
4194 /*69*/  "\005\000\002\006\002"
4195         "GDT HA %u, Host Drive %u released",
4196 /*70*/  "\003\000\002"
4197         "GDT HA %u, DRAM error detected and corrected with ECC",
4198 /*71*/  "\003\000\002"
4199         "GDT HA %u, Uncorrectable DRAM error detected with ECC",
4200 /*72*/  "\011\000\002\012\001\013\001\014\001"
4201         "GDT HA %u, SCSI bus %u, ID %u, LUN %u: reassigning block",
4202 /*73*/  "\005\000\002\006\002"
4203         "GDT HA %u, Host drive %u resetted locally",
4204 /*74*/  "\005\000\002\006\002"
4205         "GDT HA %u, Host drive %u resetted remotely",
4206 /*75*/  "\003\000\002"
4207         "GDT HA %u, async. status 75 unknown",
4208 };
4209
4210
4211 static int gdth_async_event(int hanum)
4212 {
4213     gdth_ha_str *ha;
4214     gdth_cmd_str *cmdp;
4215     int cmd_index;
4216
4217     ha  = HADATA(gdth_ctr_tab[hanum]);
4218     cmdp= ha->pccb;
4219     TRACE2(("gdth_async_event() ha %d serv %d\n",
4220             hanum,ha->service));
4221
4222     if (ha->service == SCREENSERVICE) {
4223         if (ha->status == MSG_REQUEST) {
4224             while (gdth_test_busy(hanum))
4225                 gdth_delay(0);
4226             cmdp->Service       = SCREENSERVICE;
4227             cmdp->RequestBuffer = SCREEN_CMND;
4228             cmd_index = gdth_get_cmd_index(hanum);
4229             gdth_set_sema0(hanum);
4230             cmdp->OpCode        = GDT_READ;
4231             cmdp->BoardNode     = LOCALBOARD;
4232             cmdp->u.screen.reserved  = 0;
4233             cmdp->u.screen.su.msg.msg_handle= MSG_INV_HANDLE;
4234             cmdp->u.screen.su.msg.msg_addr  = ha->msg_phys;
4235             ha->cmd_offs_dpmem = 0;
4236             ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr) 
4237                 + sizeof(ulong64);
4238             ha->cmd_cnt = 0;
4239             gdth_copy_command(hanum);
4240             if (ha->type == GDT_EISA)
4241                 printk("[EISA slot %d] ",(ushort)ha->brd_phys);
4242             else if (ha->type == GDT_ISA)
4243                 printk("[DPMEM 0x%4X] ",(ushort)ha->brd_phys);
4244             else 
4245                 printk("[PCI %d/%d] ",(ushort)(ha->brd_phys>>8),
4246                        (ushort)((ha->brd_phys>>3)&0x1f));
4247             gdth_release_event(hanum);
4248         }
4249
4250     } else {
4251         if (ha->type == GDT_PCIMPR && 
4252             (ha->fw_vers & 0xff) >= 0x1a) {
4253             ha->dvr.size = 0;
4254             ha->dvr.eu.async.ionode = hanum;
4255             ha->dvr.eu.async.status  = ha->status;
4256             /* severity and event_string already set! */
4257         } else {        
4258             ha->dvr.size = sizeof(ha->dvr.eu.async);
4259             ha->dvr.eu.async.ionode   = hanum;
4260             ha->dvr.eu.async.service = ha->service;
4261             ha->dvr.eu.async.status  = ha->status;
4262             ha->dvr.eu.async.info    = ha->info;
4263             *(ulong32 *)ha->dvr.eu.async.scsi_coord  = ha->info2;
4264         }
4265         gdth_store_event( ha, ES_ASYNC, ha->service, &ha->dvr );
4266         gdth_log_event( &ha->dvr, NULL );
4267     
4268         /* new host drive from expand? */
4269         if (ha->service == CACHESERVICE && ha->status == 56) {
4270             TRACE2(("gdth_async_event(): new host drive %d created\n",
4271                     (ushort)ha->info));
4272             /* gdth_analyse_hdrive(hanum, (ushort)ha->info); */
4273         }   
4274     }
4275     return 1;
4276 }
4277
4278 static void gdth_log_event(gdth_evt_data *dvr, char *buffer)
4279 {
4280     gdth_stackframe stack;
4281     char *f = NULL;
4282     int i,j;
4283
4284     TRACE2(("gdth_log_event()\n"));
4285     if (dvr->size == 0) {
4286         if (buffer == NULL) {
4287             printk("Adapter %d: %s\n",dvr->eu.async.ionode,dvr->event_string); 
4288         } else {
4289             sprintf(buffer,"Adapter %d: %s\n",
4290                 dvr->eu.async.ionode,dvr->event_string); 
4291         }
4292     } else if (dvr->eu.async.service == CACHESERVICE && 
4293         INDEX_OK(dvr->eu.async.status, async_cache_tab)) {
4294         TRACE2(("GDT: Async. event cache service, event no.: %d\n",
4295                 dvr->eu.async.status));
4296         
4297         f = async_cache_tab[dvr->eu.async.status];
4298         
4299         /* i: parameter to push, j: stack element to fill */
4300         for (j=0,i=1; i < f[0]; i+=2) {
4301             switch (f[i+1]) {
4302               case 4:
4303                 stack.b[j++] = *(ulong32*)&dvr->eu.stream[(int)f[i]];
4304                 break;
4305               case 2:
4306                 stack.b[j++] = *(ushort*)&dvr->eu.stream[(int)f[i]];
4307                 break;
4308               case 1:
4309                 stack.b[j++] = *(unchar*)&dvr->eu.stream[(int)f[i]];
4310                 break;
4311               default:
4312                 break;
4313             }
4314         }
4315         
4316         if (buffer == NULL) {
4317             printk(&f[(int)f[0]],stack); 
4318             printk("\n");
4319         } else {
4320             sprintf(buffer,&f[(int)f[0]],stack); 
4321         }
4322
4323     } else {
4324         if (buffer == NULL) {
4325             printk("GDT HA %u, Unknown async. event service %d event no. %d\n",
4326                    dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
4327         } else {
4328             sprintf(buffer,"GDT HA %u, Unknown async. event service %d event no. %d",
4329                     dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
4330         }
4331     }
4332 }
4333
4334 #ifdef GDTH_STATISTICS
4335 void gdth_timeout(ulong data)
4336 {
4337     ulong32 i;
4338     Scsi_Cmnd *nscp;
4339     gdth_ha_str *ha;
4340     ulong flags;
4341     int hanum = 0;
4342
4343     ha = HADATA(gdth_ctr_tab[hanum]);
4344     GDTH_LOCK_HA(ha, flags);
4345
4346     for (act_stats=0,i=0; i<GDTH_MAXCMDS; ++i) 
4347         if (ha->cmd_tab[i].cmnd != UNUSED_CMND)
4348             ++act_stats;
4349
4350     for (act_rq=0,nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
4351         ++act_rq;
4352
4353     TRACE2(("gdth_to(): ints %d, ios %d, act_stats %d, act_rq %d\n",
4354             act_ints, act_ios, act_stats, act_rq));
4355     act_ints = act_ios = 0;
4356
4357     gdth_timer.expires = jiffies + 30 * HZ;
4358     add_timer(&gdth_timer);
4359     GDTH_UNLOCK_HA(ha, flags);
4360 }
4361 #endif
4362
4363 GDTH_INITFUNC(void, internal_setup(char *str,int *ints))
4364 {
4365     int i, argc;
4366     char *cur_str, *argv;
4367
4368     TRACE2(("internal_setup() str %s ints[0] %d\n", 
4369             str ? str:"NULL", ints ? ints[0]:0));
4370
4371     /* read irq[] from ints[] */
4372     if (ints) {
4373         argc = ints[0];
4374         if (argc > 0) {
4375             if (argc > MAXHA)
4376                 argc = MAXHA;
4377             for (i = 0; i < argc; ++i)
4378                 irq[i] = ints[i+1];
4379         }
4380     }
4381
4382     /* analyse string */
4383     argv = str;
4384     while (argv && (cur_str = strchr(argv, ':'))) {
4385         int val = 0, c = *++cur_str;
4386         
4387         if (c == 'n' || c == 'N')
4388             val = 0;
4389         else if (c == 'y' || c == 'Y')
4390             val = 1;
4391         else
4392             val = (int)simple_strtoul(cur_str, NULL, 0);
4393
4394         if (!strncmp(argv, "disable:", 8))
4395             disable = val;
4396         else if (!strncmp(argv, "reserve_mode:", 13))
4397             reserve_mode = val;
4398         else if (!strncmp(argv, "reverse_scan:", 13))
4399             reverse_scan = val;
4400         else if (!strncmp(argv, "hdr_channel:", 12))
4401             hdr_channel = val;
4402         else if (!strncmp(argv, "max_ids:", 8))
4403             max_ids = val;
4404         else if (!strncmp(argv, "rescan:", 7))
4405             rescan = val;
4406         else if (!strncmp(argv, "virt_ctr:", 9))
4407             virt_ctr = val;
4408         else if (!strncmp(argv, "shared_access:", 14))
4409             shared_access = val;
4410         else if (!strncmp(argv, "probe_eisa_isa:", 15))
4411             probe_eisa_isa = val;
4412         else if (!strncmp(argv, "reserve_list:", 13)) {
4413             reserve_list[0] = val;
4414             for (i = 1; i < MAX_RES_ARGS; i++) {
4415                 cur_str = strchr(cur_str, ',');
4416                 if (!cur_str)
4417                     break;
4418                 if (!isdigit((int)*++cur_str)) {
4419                     --cur_str;          
4420                     break;
4421                 }
4422                 reserve_list[i] = 
4423                     (int)simple_strtoul(cur_str, NULL, 0);
4424             }
4425             if (!cur_str)
4426                 break;
4427             argv = ++cur_str;
4428             continue;
4429         }
4430
4431         if ((argv = strchr(argv, ',')))
4432             ++argv;
4433     }
4434 }
4435
4436 GDTH_INITFUNC(int, option_setup(char *str))
4437 {
4438     int ints[MAXHA];
4439     char *cur = str;
4440     int i = 1;
4441
4442     TRACE2(("option_setup() str %s\n", str ? str:"NULL")); 
4443
4444     while (cur && isdigit(*cur) && i <= MAXHA) {
4445         ints[i++] = simple_strtoul(cur, NULL, 0);
4446         if ((cur = strchr(cur, ',')) != NULL) cur++;
4447     }
4448
4449     ints[0] = i - 1;
4450     internal_setup(cur, ints);
4451     return 1;
4452 }
4453
4454 GDTH_INITFUNC(int, gdth_detect(Scsi_Host_Template *shtp))
4455 {
4456     struct Scsi_Host *shp;
4457     gdth_pci_str pcistr[MAXHA];
4458     gdth_ha_str *ha;
4459     ulong32 isa_bios;
4460     ushort eisa_slot;
4461     int i,hanum,cnt,ctr,err;
4462     unchar b;
4463     
4464  
4465 #ifdef DEBUG_GDTH
4466     printk("GDT: This driver contains debugging information !! Trace level = %d\n",
4467         DebugState);
4468     printk("     Destination of debugging information: ");
4469 #ifdef __SERIAL__
4470 #ifdef __COM2__
4471     printk("Serial port COM2\n");
4472 #else
4473     printk("Serial port COM1\n");
4474 #endif
4475 #else
4476     printk("Console\n");
4477 #endif
4478     gdth_delay(3000);
4479 #endif
4480
4481     TRACE(("gdth_detect()\n"));
4482
4483     if (disable) {
4484         printk("GDT-HA: Controller driver disabled from command line !\n");
4485         return 0;
4486     }
4487
4488     printk("GDT-HA: Storage RAID Controller Driver. Version: %s \n",GDTH_VERSION_STR);
4489     /* initializations */
4490     gdth_polling = TRUE; b = 0;
4491     gdth_clear_events();
4492
4493     /* As default we do not probe for EISA or ISA controllers */
4494     if (probe_eisa_isa) {    
4495         /* scanning for controllers, at first: ISA controller */
4496         for (isa_bios=0xc8000UL; isa_bios<=0xd8000UL; isa_bios+=0x8000UL) {
4497             dma_addr_t scratch_dma_handle;
4498             scratch_dma_handle = 0;
4499
4500             if (gdth_ctr_count >= MAXHA) 
4501                 break;
4502             if (gdth_search_isa(isa_bios)) {        /* controller found */
4503                 shp = scsi_register(shtp,sizeof(gdth_ext_str));
4504                 if (shp == NULL)
4505                     continue;  
4506
4507                 ha = HADATA(shp);
4508                 if (!gdth_init_isa(isa_bios,ha)) {
4509                     scsi_unregister(shp);
4510                     continue;
4511                 }
4512 #ifdef __ia64__
4513                 break;
4514 #else
4515                 /* controller found and initialized */
4516                 printk("Configuring GDT-ISA HA at BIOS 0x%05X IRQ %u DRQ %u\n",
4517                        isa_bios,ha->irq,ha->drq);
4518
4519                 if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT,"gdth",ha)) {
4520                     printk("GDT-ISA: Unable to allocate IRQ\n");
4521                     scsi_unregister(shp);
4522                     continue;
4523                 }
4524                 if (request_dma(ha->drq,"gdth")) {
4525                     printk("GDT-ISA: Unable to allocate DMA channel\n");
4526                     free_irq(ha->irq,ha);
4527                     scsi_unregister(shp);
4528                     continue;
4529                 }
4530                 set_dma_mode(ha->drq,DMA_MODE_CASCADE);
4531                 enable_dma(ha->drq);
4532                 shp->unchecked_isa_dma = 1;
4533                 shp->irq = ha->irq;
4534                 shp->dma_channel = ha->drq;
4535                 hanum = gdth_ctr_count;         
4536                 gdth_ctr_tab[gdth_ctr_count++] = shp;
4537                 gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
4538
4539                 NUMDATA(shp)->hanum = (ushort)hanum;
4540                 NUMDATA(shp)->busnum= 0;
4541
4542                 ha->pccb = CMDDATA(shp);
4543                 ha->ccb_phys = 0L;
4544 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
4545                 ha->pdev = NULL;
4546                 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH, 
4547                                                     &scratch_dma_handle);
4548                 ha->scratch_phys = scratch_dma_handle;
4549                 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str), 
4550                                                 &scratch_dma_handle);
4551                 ha->msg_phys = scratch_dma_handle;
4552 #ifdef INT_COAL
4553                 ha->coal_stat = (gdth_coal_status *)
4554                     pci_alloc_consistent(ha->pdev, sizeof(gdth_coal_status) *
4555                         MAXOFFSETS, &scratch_dma_handle);
4556                 ha->coal_stat_phys = scratch_dma_handle;
4557 #endif
4558 #else
4559                 ha->pscratch = scsi_init_malloc(GDTH_SCRATCH, GFP_ATOMIC | GFP_DMA);
4560                 if (ha->pscratch)
4561                     ha->scratch_phys = virt_to_bus(ha->pscratch);
4562                 ha->pmsg = scsi_init_malloc(sizeof(gdth_msg_str), GFP_ATOMIC | GFP_DMA);
4563                 if (ha->pmsg)
4564                     ha->msg_phys = virt_to_bus(ha->pmsg);
4565 #ifdef INT_COAL
4566                 ha->coal_stat = 
4567                     scsi_init_malloc(sizeof(gdth_coal_status) * MAXOFFSETS, 
4568                                      GFP_ATOMIC | GFP_DMA);
4569                 if (ha->coal_stat)
4570                     ha->coal_stat_phys = virt_to_bus(ha->coal_stat);
4571 #endif
4572 #endif
4573                 ha->scratch_busy = FALSE;
4574                 ha->req_first = NULL;
4575                 ha->tid_cnt = MAX_HDRIVES;
4576                 if (max_ids > 0 && max_ids < ha->tid_cnt)
4577                     ha->tid_cnt = max_ids;
4578                 for (i=0; i<GDTH_MAXCMDS; ++i)
4579                     ha->cmd_tab[i].cmnd = UNUSED_CMND;
4580                 ha->scan_mode = rescan ? 0x10 : 0;
4581
4582                 if (ha->pscratch == NULL || ha->pmsg == NULL || 
4583                     !gdth_search_drives(hanum)) {
4584                     printk("GDT-ISA: Error during device scan\n");
4585                     --gdth_ctr_count;
4586                     --gdth_ctr_vcount;
4587 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
4588 #ifdef INT_COAL
4589                     if (ha->coal_stat)
4590                         pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) *
4591                                             MAXOFFSETS, ha->coal_stat,
4592                                             ha->coal_stat_phys);
4593 #endif
4594                     if (ha->pscratch)
4595                         pci_free_consistent(ha->pdev, GDTH_SCRATCH, 
4596                                             ha->pscratch, ha->scratch_phys);
4597                     if (ha->pmsg)
4598                         pci_free_consistent(ha->pdev, sizeof(gdth_msg_str), 
4599                                             ha->pmsg, ha->msg_phys);
4600 #else
4601 #ifdef INT_COAL
4602                     if (ha->coal_stat)
4603                         scsi_init_free((void *)ha->coal_stat, 
4604                                        sizeof(gdth_coal_status) * MAXOFFSETS);
4605 #endif
4606                     if (ha->pscratch)
4607                         scsi_init_free((void *)ha->pscratch, GDTH_SCRATCH);
4608                     if (ha->pmsg)
4609                         scsi_init_free((void *)ha->pmsg, sizeof(gdth_msg_str));
4610 #endif
4611                     free_irq(ha->irq,ha);
4612                     scsi_unregister(shp);
4613                     continue;
4614                 }
4615                 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4616                     hdr_channel = ha->bus_cnt;
4617                 ha->virt_bus = hdr_channel;
4618
4619 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
4620 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,20) && \
4621     LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
4622                 shp->highmem_io  = 0;
4623 #endif
4624                 if (ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT) 
4625                     shp->max_cmd_len = 16;
4626 #endif
4627                 shp->max_id      = ha->tid_cnt;
4628                 shp->max_lun     = MAXLUN;
4629                 shp->max_channel = virt_ctr ? 0 : ha->bus_cnt;
4630                 if (virt_ctr) {
4631                     virt_ctr = 1;
4632                     /* register addit. SCSI channels as virtual controllers */
4633                     for (b = 1; b < ha->bus_cnt + 1; ++b) {
4634                         shp = scsi_register(shtp,sizeof(gdth_num_str));
4635                         shp->unchecked_isa_dma = 1;
4636                         shp->irq = ha->irq;
4637                         shp->dma_channel = ha->drq;
4638                         gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
4639                         NUMDATA(shp)->hanum = (ushort)hanum;
4640                         NUMDATA(shp)->busnum = b;
4641                     }
4642                 }  
4643
4644                 GDTH_INIT_LOCK_HA(ha);
4645                 gdth_enable_int(hanum);
4646 #endif /* !__ia64__ */
4647             }
4648         }
4649
4650         /* scanning for EISA controllers */
4651         for (eisa_slot=0x1000; eisa_slot<=0x8000; eisa_slot+=0x1000) {
4652             dma_addr_t scratch_dma_handle;
4653             scratch_dma_handle = 0;
4654
4655             if (gdth_ctr_count >= MAXHA) 
4656                 break;
4657             if (gdth_search_eisa(eisa_slot)) {      /* controller found */
4658                 shp = scsi_register(shtp,sizeof(gdth_ext_str));
4659                 if (shp == NULL)
4660                     continue;  
4661
4662                 ha = HADATA(shp);
4663                 if (!gdth_init_eisa(eisa_slot,ha)) {
4664                     scsi_unregister(shp);
4665                     continue;
4666                 }
4667                 /* controller found and initialized */
4668                 printk("Configuring GDT-EISA HA at Slot %d IRQ %u\n",
4669                        eisa_slot>>12,ha->irq);
4670
4671                 if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT,"gdth",ha)) {
4672                     printk("GDT-EISA: Unable to allocate IRQ\n");
4673                     scsi_unregister(shp);
4674                     continue;
4675                 }
4676                 shp->unchecked_isa_dma = 0;
4677                 shp->irq = ha->irq;
4678                 shp->dma_channel = 0xff;
4679                 hanum = gdth_ctr_count;
4680                 gdth_ctr_tab[gdth_ctr_count++] = shp;
4681                 gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
4682
4683                 NUMDATA(shp)->hanum = (ushort)hanum;
4684                 NUMDATA(shp)->busnum= 0;
4685                 TRACE2(("EISA detect Bus 0: hanum %d\n",
4686                         NUMDATA(shp)->hanum));
4687
4688                 ha->pccb = CMDDATA(shp);
4689                 ha->ccb_phys = 0L; 
4690 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
4691                 ha->pdev = NULL;
4692                 ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH, 
4693                                                     &scratch_dma_handle);
4694                 ha->scratch_phys = scratch_dma_handle;
4695                 ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str), 
4696                                                 &scratch_dma_handle);
4697                 ha->msg_phys = scratch_dma_handle;
4698 #ifdef INT_COAL
4699                 ha->coal_stat = (gdth_coal_status *)
4700                     pci_alloc_consistent(ha->pdev, sizeof(gdth_coal_status) *
4701                                          MAXOFFSETS, &scratch_dma_handle);
4702                 ha->coal_stat_phys = scratch_dma_handle;
4703 #endif
4704                 ha->ccb_phys = 
4705                     pci_map_single(ha->pdev,ha->pccb,
4706                                    sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
4707 #else
4708                 ha->pscratch = scsi_init_malloc(GDTH_SCRATCH, GFP_ATOMIC | GFP_DMA);
4709                 if (ha->pscratch)
4710                     ha->scratch_phys = virt_to_bus(ha->pscratch);
4711                 ha->pmsg = scsi_init_malloc(sizeof(gdth_msg_str), GFP_ATOMIC | GFP_DMA);
4712                 if (ha->pmsg)
4713                     ha->msg_phys = virt_to_bus(ha->pmsg);
4714 #ifdef INT_COAL
4715                 ha->coal_stat = 
4716                     scsi_init_malloc(sizeof(gdth_coal_status) * MAXOFFSETS, 
4717                                      GFP_ATOMIC | GFP_DMA);
4718                 if (ha->coal_stat)
4719                     ha->coal_stat_phys = virt_to_bus(ha->coal_stat);
4720 #endif
4721                 ha->ccb_phys = virt_to_bus(ha->pccb);
4722 #endif
4723                 ha->scratch_busy = FALSE;
4724                 ha->req_first = NULL;
4725                 ha->tid_cnt = MAX_HDRIVES;
4726                 if (max_ids > 0 && max_ids < ha->tid_cnt)
4727                     ha->tid_cnt = max_ids;
4728                 for (i=0; i<GDTH_MAXCMDS; ++i)
4729                     ha->cmd_tab[i].cmnd = UNUSED_CMND;
4730                 ha->scan_mode = rescan ? 0x10 : 0;
4731
4732                 if (ha->pscratch == NULL || ha->pmsg == NULL || 
4733                     !gdth_search_drives(hanum)) {
4734                     printk("GDT-EISA: Error during device scan\n");
4735                     --gdth_ctr_count;
4736                     --gdth_ctr_vcount;
4737 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
4738 #ifdef INT_COAL
4739                     if (ha->coal_stat)
4740                         pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) *
4741                                             MAXOFFSETS, ha->coal_stat,
4742                                             ha->coal_stat_phys);
4743 #endif
4744                     if (ha->pscratch)
4745                         pci_free_consistent(ha->pdev, GDTH_SCRATCH, 
4746                                             ha->pscratch, ha->scratch_phys);
4747                     if (ha->pmsg)
4748                         pci_free_consistent(ha->pdev, sizeof(gdth_msg_str), 
4749                                             ha->pmsg, ha->msg_phys);
4750                     if (ha->ccb_phys)
4751                         pci_unmap_single(ha->pdev,ha->ccb_phys,
4752                                         sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
4753 #else
4754 #ifdef INT_COAL
4755                     if (ha->coal_stat)
4756                         scsi_init_free((void *)ha->coal_stat, 
4757                                        sizeof(gdth_coal_status) * MAXOFFSETS);
4758 #endif
4759                     if (ha->pscratch)
4760                         scsi_init_free((void *)ha->pscratch, GDTH_SCRATCH);
4761                     if (ha->pmsg)
4762                         scsi_init_free((void *)ha->pmsg, sizeof(gdth_msg_str));
4763 #endif
4764                     free_irq(ha->irq,ha);
4765                     scsi_unregister(shp);
4766                     continue;
4767                 }
4768                 if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4769                     hdr_channel = ha->bus_cnt;
4770                 ha->virt_bus = hdr_channel;
4771
4772 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
4773 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,20) && \
4774     LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
4775                 shp->highmem_io  = 0;
4776 #endif
4777                 if (ha->cache_feat & ha->raw_feat & ha->screen_feat & GDT_64BIT) 
4778                     shp->max_cmd_len = 16;
4779 #endif
4780                 shp->max_id      = ha->tid_cnt;
4781                 shp->max_lun     = MAXLUN;
4782                 shp->max_channel = virt_ctr ? 0 : ha->bus_cnt;
4783                 if (virt_ctr) {
4784                     virt_ctr = 1;
4785                     /* register addit. SCSI channels as virtual controllers */
4786                     for (b = 1; b < ha->bus_cnt + 1; ++b) {
4787                         shp = scsi_register(shtp,sizeof(gdth_num_str));
4788                         shp->unchecked_isa_dma = 0;
4789                         shp->irq = ha->irq;
4790                         shp->dma_channel = 0xff;
4791                         gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
4792                         NUMDATA(shp)->hanum = (ushort)hanum;
4793                         NUMDATA(shp)->busnum = b;
4794                     }
4795                 }  
4796
4797                 GDTH_INIT_LOCK_HA(ha);
4798                 gdth_enable_int(hanum);
4799             }
4800         }
4801     }
4802
4803     /* scanning for PCI controllers */
4804     cnt = gdth_search_pci(pcistr);
4805     printk("GDT-HA: Found %d PCI Storage RAID Controllers\n",cnt);
4806     gdth_sort_pci(pcistr,cnt);
4807     for (ctr = 0; ctr < cnt; ++ctr) {
4808         dma_addr_t scratch_dma_handle;
4809         scratch_dma_handle = 0;
4810
4811         if (gdth_ctr_count >= MAXHA)
4812             break;
4813         shp = scsi_register(shtp,sizeof(gdth_ext_str));
4814         if (shp == NULL)
4815             continue;  
4816
4817         ha = HADATA(shp);
4818         if (!gdth_init_pci(&pcistr[ctr],ha)) {
4819             scsi_unregister(shp);
4820             continue;
4821         }
4822         /* controller found and initialized */
4823         printk("Configuring GDT-PCI HA at %d/%d IRQ %u\n",
4824                pcistr[ctr].bus,PCI_SLOT(pcistr[ctr].device_fn),ha->irq);
4825
4826         if (request_irq(ha->irq, gdth_interrupt,
4827                         SA_INTERRUPT|SA_SHIRQ, "gdth", ha))
4828         {
4829             printk("GDT-PCI: Unable to allocate IRQ\n");
4830             scsi_unregister(shp);
4831             continue;
4832         }
4833         shp->unchecked_isa_dma = 0;
4834         shp->irq = ha->irq;
4835         shp->dma_channel = 0xff;
4836         hanum = gdth_ctr_count;
4837         gdth_ctr_tab[gdth_ctr_count++] = shp;
4838         gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
4839
4840         NUMDATA(shp)->hanum = (ushort)hanum;
4841         NUMDATA(shp)->busnum= 0;
4842
4843         ha->pccb = CMDDATA(shp);
4844         ha->ccb_phys = 0L;
4845 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
4846         ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH, 
4847                                             &scratch_dma_handle);
4848         ha->scratch_phys = scratch_dma_handle;
4849         ha->pmsg = pci_alloc_consistent(ha->pdev, sizeof(gdth_msg_str), 
4850                                         &scratch_dma_handle);
4851         ha->msg_phys = scratch_dma_handle;
4852 #ifdef INT_COAL
4853         ha->coal_stat = (gdth_coal_status *)
4854             pci_alloc_consistent(ha->pdev, sizeof(gdth_coal_status) *
4855                                  MAXOFFSETS, &scratch_dma_handle);
4856         ha->coal_stat_phys = scratch_dma_handle;
4857 #endif
4858 #else
4859         ha->pscratch = scsi_init_malloc(GDTH_SCRATCH, GFP_ATOMIC | GFP_DMA);
4860         if (ha->pscratch)
4861             ha->scratch_phys = virt_to_bus(ha->pscratch);
4862         ha->pmsg = scsi_init_malloc(sizeof(gdth_msg_str), GFP_ATOMIC | GFP_DMA);
4863         if (ha->pmsg)
4864             ha->msg_phys = virt_to_bus(ha->pmsg);
4865 #ifdef INT_COAL
4866         ha->coal_stat = 
4867             scsi_init_malloc(sizeof(gdth_coal_status) * MAXOFFSETS, 
4868                              GFP_ATOMIC | GFP_DMA);
4869         if (ha->coal_stat)
4870             ha->coal_stat_phys = virt_to_bus(ha->coal_stat);
4871 #endif
4872 #endif
4873         ha->scratch_busy = FALSE;
4874         ha->req_first = NULL;
4875         ha->tid_cnt = pcistr[ctr].device_id >= 0x200 ? MAXID : MAX_HDRIVES;
4876         if (max_ids > 0 && max_ids < ha->tid_cnt)
4877             ha->tid_cnt = max_ids;
4878         for (i=0; i<GDTH_MAXCMDS; ++i)
4879             ha->cmd_tab[i].cmnd = UNUSED_CMND;
4880         ha->scan_mode = rescan ? 0x10 : 0;
4881
4882         err = FALSE;
4883         if (ha->pscratch == NULL || ha->pmsg == NULL || 
4884             !gdth_search_drives(hanum)) {
4885             err = TRUE;
4886         } else {
4887             if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
4888                 hdr_channel = ha->bus_cnt;
4889             ha->virt_bus = hdr_channel;
4890
4891 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
4892 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
4893             scsi_set_device(shp, &pcistr[ctr].pdev->dev);
4894 #else
4895             scsi_set_pci_device(shp, pcistr[ctr].pdev);
4896 #endif
4897             if (!(ha->cache_feat & ha->raw_feat & ha->screen_feat &GDT_64BIT)||
4898                 /* 64-bit DMA only supported from FW >= x.43 */
4899                 (!ha->dma64_support)) {
4900                 if (pci_set_dma_mask(pcistr[ctr].pdev, 0xffffffff)) {
4901                     printk(KERN_WARNING "GDT-PCI %d: Unable to set 32-bit DMA\n", hanum);
4902                     err = TRUE;
4903                 }
4904             } else {
4905                 shp->max_cmd_len = 16;
4906                 if (!pci_set_dma_mask(pcistr[ctr].pdev, 0xffffffffffffffffULL)) {
4907                     printk("GDT-PCI %d: 64-bit DMA enabled\n", hanum);
4908                 } else if (pci_set_dma_mask(pcistr[ctr].pdev, 0xffffffff)) {
4909                     printk(KERN_WARNING "GDT-PCI %d: Unable to set 64/32-bit DMA\n", hanum);
4910                     err = TRUE;
4911                 }
4912             }
4913 #endif
4914         }
4915
4916         if (err) {
4917             printk("GDT-PCI %d: Error during device scan\n", hanum);
4918             --gdth_ctr_count;
4919             --gdth_ctr_vcount;
4920 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
4921 #ifdef INT_COAL
4922             if (ha->coal_stat)
4923                 pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) *
4924                                     MAXOFFSETS, ha->coal_stat,
4925                                     ha->coal_stat_phys);
4926 #endif
4927             if (ha->pscratch)
4928                 pci_free_consistent(ha->pdev, GDTH_SCRATCH, 
4929                                     ha->pscratch, ha->scratch_phys);
4930             if (ha->pmsg)
4931                 pci_free_consistent(ha->pdev, sizeof(gdth_msg_str), 
4932                                     ha->pmsg, ha->msg_phys);
4933 #else
4934 #ifdef INT_COAL
4935             if (ha->coal_stat)
4936                 scsi_init_free((void *)ha->coal_stat, 
4937                                sizeof(gdth_coal_status) * MAXOFFSETS);
4938 #endif
4939             if (ha->pscratch)
4940                 scsi_init_free((void *)ha->pscratch, GDTH_SCRATCH);
4941             if (ha->pmsg)
4942                 scsi_init_free((void *)ha->pmsg, sizeof(gdth_msg_str));
4943 #endif
4944             free_irq(ha->irq,ha);
4945             scsi_unregister(shp);
4946             continue;
4947         }
4948
4949         shp->max_id      = ha->tid_cnt;
4950         shp->max_lun     = MAXLUN;
4951         shp->max_channel = virt_ctr ? 0 : ha->bus_cnt;
4952         if (virt_ctr) {
4953             virt_ctr = 1;
4954             /* register addit. SCSI channels as virtual controllers */
4955             for (b = 1; b < ha->bus_cnt + 1; ++b) {
4956                 shp = scsi_register(shtp,sizeof(gdth_num_str));
4957                 shp->unchecked_isa_dma = 0;
4958                 shp->irq = ha->irq;
4959                 shp->dma_channel = 0xff;
4960                 gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
4961                 NUMDATA(shp)->hanum = (ushort)hanum;
4962                 NUMDATA(shp)->busnum = b;
4963             }
4964         }  
4965
4966
4967         GDTH_INIT_LOCK_HA(ha);
4968         gdth_enable_int(hanum);
4969     }
4970     
4971     TRACE2(("gdth_detect() %d controller detected\n",gdth_ctr_count));
4972     if (gdth_ctr_count > 0) {
4973 #ifdef GDTH_STATISTICS
4974         TRACE2(("gdth_detect(): Initializing timer !\n"));
4975         init_timer(&gdth_timer);
4976         gdth_timer.expires = jiffies + HZ;
4977         gdth_timer.data = 0L;
4978         gdth_timer.function = gdth_timeout;
4979         add_timer(&gdth_timer);
4980 #endif
4981         major = register_chrdev(0,"gdth",&gdth_fops);
4982         register_reboot_notifier(&gdth_notifier);
4983     }
4984     gdth_polling = FALSE;
4985     return gdth_ctr_vcount;
4986 }
4987
4988
4989 int gdth_release(struct Scsi_Host *shp)
4990 {
4991     int hanum;
4992     gdth_ha_str *ha;
4993
4994     TRACE2(("gdth_release()\n"));
4995     if (NUMDATA(shp)->busnum == 0) {
4996         hanum = NUMDATA(shp)->hanum;
4997         ha    = HADATA(gdth_ctr_tab[hanum]);
4998 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
4999         if (ha->sdev) {
5000             scsi_free_host_dev(ha->sdev);
5001             ha->sdev = NULL;
5002         }
5003 #endif
5004         gdth_flush(hanum);
5005
5006         if (shp->irq) {
5007             free_irq(shp->irq,ha);
5008         }
5009 #ifndef __ia64__
5010         if (shp->dma_channel != 0xff) {
5011             free_dma(shp->dma_channel);
5012         }
5013 #endif
5014 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5015 #ifdef INT_COAL
5016         if (ha->coal_stat)
5017             pci_free_consistent(ha->pdev, sizeof(gdth_coal_status) *
5018                                 MAXOFFSETS, ha->coal_stat, ha->coal_stat_phys);
5019 #endif
5020         if (ha->pscratch)
5021             pci_free_consistent(ha->pdev, GDTH_SCRATCH, 
5022                                 ha->pscratch, ha->scratch_phys);
5023         if (ha->pmsg)
5024             pci_free_consistent(ha->pdev, sizeof(gdth_msg_str), 
5025                                 ha->pmsg, ha->msg_phys);
5026         if (ha->ccb_phys)
5027             pci_unmap_single(ha->pdev,ha->ccb_phys,
5028                              sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
5029 #else
5030 #ifdef INT_COAL
5031         if (ha->coal_stat)
5032             scsi_init_free((void *)ha->coal_stat, 
5033                            sizeof(gdth_coal_status) * MAXOFFSETS);
5034 #endif
5035         if (ha->pscratch)
5036             scsi_init_free((void *)ha->pscratch, GDTH_SCRATCH);
5037         if (ha->pmsg)
5038             scsi_init_free((void *)ha->pmsg, sizeof(gdth_msg_str));
5039 #endif
5040         gdth_ctr_released++;
5041         TRACE2(("gdth_release(): HA %d of %d\n", 
5042                 gdth_ctr_released, gdth_ctr_count));
5043
5044         if (gdth_ctr_released == gdth_ctr_count) {
5045 #ifdef GDTH_STATISTICS
5046             del_timer(&gdth_timer);
5047 #endif
5048             unregister_chrdev(major,"gdth");
5049             unregister_reboot_notifier(&gdth_notifier);
5050         }
5051     }
5052
5053     scsi_unregister(shp);
5054     return 0;
5055 }
5056             
5057
5058 static const char *gdth_ctr_name(int hanum)
5059 {
5060     gdth_ha_str *ha;
5061
5062     TRACE2(("gdth_ctr_name()\n"));
5063
5064     ha    = HADATA(gdth_ctr_tab[hanum]);
5065
5066     if (ha->type == GDT_EISA) {
5067         switch (ha->stype) {
5068           case GDT3_ID:
5069             return("GDT3000/3020");
5070           case GDT3A_ID:
5071             return("GDT3000A/3020A/3050A");
5072           case GDT3B_ID:
5073             return("GDT3000B/3010A");
5074         }
5075     } else if (ha->type == GDT_ISA) {
5076         return("GDT2000/2020");
5077     } else if (ha->type == GDT_PCI) {
5078         switch (ha->stype) {
5079           case PCI_DEVICE_ID_VORTEX_GDT60x0:
5080             return("GDT6000/6020/6050");
5081           case PCI_DEVICE_ID_VORTEX_GDT6000B:
5082             return("GDT6000B/6010");
5083         }
5084     } 
5085     /* new controllers (GDT_PCINEW, GDT_PCIMPR, ..) use board_info IOCTL! */
5086
5087     return("");
5088 }
5089
5090 const char *gdth_info(struct Scsi_Host *shp)
5091 {
5092     int hanum;
5093     gdth_ha_str *ha;
5094
5095     TRACE2(("gdth_info()\n"));
5096     hanum = NUMDATA(shp)->hanum;
5097     ha    = HADATA(gdth_ctr_tab[hanum]);
5098
5099     return ((const char *)ha->binfo.type_string);
5100 }
5101
5102 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0)
5103 /* old error handling */
5104 int gdth_abort(Scsi_Cmnd *scp)
5105 {
5106     TRACE2(("gdth_abort() reason %d\n",scp->abort_reason));
5107     return SCSI_ABORT_SNOOZE;
5108 }
5109
5110 int gdth_reset(Scsi_Cmnd *scp, unsigned int reset_flags)
5111 {
5112     TRACE2(("gdth_reset()\n"));
5113     return SCSI_RESET_PUNT;
5114 }
5115 #endif
5116
5117 /* new error handling */
5118 int gdth_eh_abort(Scsi_Cmnd *scp)
5119 {
5120     TRACE2(("gdth_eh_abort()\n"));
5121     return FAILED;
5122 }
5123
5124 int gdth_eh_device_reset(Scsi_Cmnd *scp)
5125 {
5126     TRACE2(("gdth_eh_device_reset()\n"));
5127     return FAILED;
5128 }
5129
5130 int gdth_eh_bus_reset(Scsi_Cmnd *scp)
5131 {
5132     int i, hanum;
5133     gdth_ha_str *ha;
5134     ulong flags;
5135     Scsi_Cmnd *cmnd;
5136     unchar b;
5137
5138     TRACE2(("gdth_eh_bus_reset()\n"));
5139 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5140     hanum = NUMDATA(scp->device->host)->hanum;
5141     b = virt_ctr ? NUMDATA(scp->device->host)->busnum : scp->device->channel;
5142 #else
5143     hanum = NUMDATA(scp->host)->hanum;
5144     b = virt_ctr ? NUMDATA(scp->host)->busnum : scp->channel;
5145 #endif
5146     ha    = HADATA(gdth_ctr_tab[hanum]);
5147
5148     /* clear command tab */
5149     GDTH_LOCK_HA(ha, flags);
5150     for (i = 0; i < GDTH_MAXCMDS; ++i) {
5151         cmnd = ha->cmd_tab[i].cmnd;
5152 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5153         if (!SPECIAL_SCP(cmnd) && cmnd->device->channel == b)
5154 #else
5155         if (!SPECIAL_SCP(cmnd) && cmnd->channel == b)
5156 #endif
5157             ha->cmd_tab[i].cmnd = UNUSED_CMND;
5158     }
5159     GDTH_UNLOCK_HA(ha, flags);
5160
5161     if (b == ha->virt_bus) {
5162         /* host drives */
5163         for (i = 0; i < MAX_HDRIVES; ++i) {
5164             if (ha->hdr[i].present) {
5165                 GDTH_LOCK_HA(ha, flags);
5166                 gdth_polling = TRUE;
5167                 while (gdth_test_busy(hanum))
5168                     gdth_delay(0);
5169                 if (gdth_internal_cmd(hanum, CACHESERVICE, 
5170                                       GDT_CLUST_RESET, i, 0, 0))
5171                     ha->hdr[i].cluster_type &= ~CLUSTER_RESERVED;
5172                 gdth_polling = FALSE;
5173                 GDTH_UNLOCK_HA(ha, flags);
5174             }
5175         }
5176     } else {
5177         /* raw devices */
5178         GDTH_LOCK_HA(ha, flags);
5179         for (i = 0; i < MAXID; ++i)
5180             ha->raw[BUS_L2P(ha,b)].io_cnt[i] = 0;
5181         gdth_polling = TRUE;
5182         while (gdth_test_busy(hanum))
5183             gdth_delay(0);
5184         gdth_internal_cmd(hanum, SCSIRAWSERVICE, GDT_RESET_BUS,
5185                           BUS_L2P(ha,b), 0, 0);
5186         gdth_polling = FALSE;
5187         GDTH_UNLOCK_HA(ha, flags);
5188     }
5189     return SUCCESS;
5190 }
5191
5192 int gdth_eh_host_reset(Scsi_Cmnd *scp)
5193 {
5194     TRACE2(("gdth_eh_host_reset()\n"));
5195     return FAILED;
5196 }
5197
5198
5199 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5200 int gdth_bios_param(struct scsi_device *sdev,struct block_device *bdev,sector_t cap,int *ip)
5201 #else
5202 int gdth_bios_param(Disk *disk,kdev_t dev,int *ip)
5203 #endif
5204 {
5205     unchar b, t;
5206     int hanum;
5207     gdth_ha_str *ha;
5208     struct scsi_device *sd;
5209     unsigned capacity;
5210
5211 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5212     sd = sdev;
5213     capacity = cap;
5214 #else
5215     sd = disk->device;
5216     capacity = disk->capacity;
5217 #endif
5218     hanum = NUMDATA(sd->host)->hanum;
5219     b = virt_ctr ? NUMDATA(sd->host)->busnum : sd->channel;
5220     t = sd->id;
5221     TRACE2(("gdth_bios_param() ha %d bus %d target %d\n", hanum, b, t)); 
5222     ha = HADATA(gdth_ctr_tab[hanum]);
5223
5224     if (b != ha->virt_bus || ha->hdr[t].heads == 0) {
5225         /* raw device or host drive without mapping information */
5226         TRACE2(("Evaluate mapping\n"));
5227         gdth_eval_mapping(capacity,&ip[2],&ip[0],&ip[1]);
5228     } else {
5229         ip[0] = ha->hdr[t].heads;
5230         ip[1] = ha->hdr[t].secs;
5231         ip[2] = capacity / ip[0] / ip[1];
5232     }
5233
5234     TRACE2(("gdth_bios_param(): %d heads, %d secs, %d cyls\n",
5235             ip[0],ip[1],ip[2]));
5236     return 0;
5237 }
5238
5239
5240 int gdth_queuecommand(Scsi_Cmnd *scp,void (*done)(Scsi_Cmnd *))
5241 {
5242     int hanum;
5243     int priority;
5244
5245     TRACE(("gdth_queuecommand() cmd 0x%x\n", scp->cmnd[0]));
5246     
5247     scp->scsi_done = (void *)done;
5248     scp->SCp.have_data_in = 1;
5249     scp->SCp.phase = -1;
5250     scp->SCp.sent_command = -1;
5251     scp->SCp.Status = GDTH_MAP_NONE;
5252     scp->SCp.buffer = (struct scatterlist *)NULL;
5253 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5254     hanum = NUMDATA(scp->device->host)->hanum;
5255 #else
5256     hanum = NUMDATA(scp->host)->hanum;
5257 #endif
5258 #ifdef GDTH_STATISTICS
5259     ++act_ios;
5260 #endif
5261
5262     priority = DEFAULT_PRI;
5263     if (scp->done == gdth_scsi_done)
5264         priority = scp->SCp.this_residual;
5265     gdth_update_timeout(hanum, scp, scp->timeout_per_command * 6);
5266     gdth_putq( hanum, scp, priority );
5267     gdth_next( hanum );
5268     return 0;
5269 }
5270
5271
5272 static int gdth_open(struct inode *inode, struct file *filep)
5273 {
5274 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5275     gdth_ha_str *ha;
5276     int i;
5277
5278     for (i = 0; i < gdth_ctr_count; i++) {
5279         ha = HADATA(gdth_ctr_tab[i]);
5280         if (!ha->sdev)
5281             ha->sdev = scsi_get_host_dev(gdth_ctr_tab[i]);
5282     }
5283 #endif
5284
5285     TRACE(("gdth_open()\n"));
5286     return 0;
5287 }
5288
5289 static int gdth_close(struct inode *inode, struct file *filep)
5290 {
5291     TRACE(("gdth_close()\n"));
5292     return 0;
5293 }
5294
5295 static int ioc_event(void __user *arg)
5296 {
5297     gdth_ioctl_event evt;
5298     gdth_ha_str *ha;
5299     ulong flags;
5300
5301     if (copy_from_user(&evt, arg, sizeof(gdth_ioctl_event)) ||
5302         evt.ionode >= gdth_ctr_count)
5303         return -EFAULT;
5304     ha = HADATA(gdth_ctr_tab[evt.ionode]);
5305
5306     if (evt.erase == 0xff) {
5307         if (evt.event.event_source == ES_TEST)
5308             evt.event.event_data.size=sizeof(evt.event.event_data.eu.test); 
5309         else if (evt.event.event_source == ES_DRIVER)
5310             evt.event.event_data.size=sizeof(evt.event.event_data.eu.driver); 
5311         else if (evt.event.event_source == ES_SYNC)
5312             evt.event.event_data.size=sizeof(evt.event.event_data.eu.sync); 
5313         else
5314             evt.event.event_data.size=sizeof(evt.event.event_data.eu.async);
5315         GDTH_LOCK_HA(ha, flags);
5316         gdth_store_event(ha, evt.event.event_source, evt.event.event_idx,
5317                          &evt.event.event_data);
5318         GDTH_UNLOCK_HA(ha, flags);
5319     } else if (evt.erase == 0xfe) {
5320         gdth_clear_events();
5321     } else if (evt.erase == 0) {
5322         evt.handle = gdth_read_event(ha, evt.handle, &evt.event);
5323     } else {
5324         gdth_readapp_event(ha, evt.erase, &evt.event);
5325     }     
5326     if (copy_to_user(arg, &evt, sizeof(gdth_ioctl_event)))
5327         return -EFAULT;
5328     return 0;
5329 }
5330
5331 static int ioc_lockdrv(void __user *arg)
5332 {
5333     gdth_ioctl_lockdrv ldrv;
5334     unchar i, j;
5335     ulong flags;
5336     gdth_ha_str *ha;
5337
5338     if (copy_from_user(&ldrv, arg, sizeof(gdth_ioctl_lockdrv)) ||
5339         ldrv.ionode >= gdth_ctr_count)
5340         return -EFAULT;
5341     ha = HADATA(gdth_ctr_tab[ldrv.ionode]);
5342  
5343     for (i = 0; i < ldrv.drive_cnt && i < MAX_HDRIVES; ++i) {
5344         j = ldrv.drives[i];
5345         if (j >= MAX_HDRIVES || !ha->hdr[j].present)
5346             continue;
5347         if (ldrv.lock) {
5348             GDTH_LOCK_HA(ha, flags);
5349             ha->hdr[j].lock = 1;
5350             GDTH_UNLOCK_HA(ha, flags);
5351             gdth_wait_completion(ldrv.ionode, ha->bus_cnt, j); 
5352             gdth_stop_timeout(ldrv.ionode, ha->bus_cnt, j); 
5353         } else {
5354             GDTH_LOCK_HA(ha, flags);
5355             ha->hdr[j].lock = 0;
5356             GDTH_UNLOCK_HA(ha, flags);
5357             gdth_start_timeout(ldrv.ionode, ha->bus_cnt, j); 
5358             gdth_next(ldrv.ionode); 
5359         }
5360     } 
5361     return 0;
5362 }
5363
5364 static int ioc_resetdrv(void __user *arg, char *cmnd)
5365 {
5366     gdth_ioctl_reset res;
5367     gdth_cmd_str cmd;
5368     int hanum;
5369     gdth_ha_str *ha;
5370 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5371         Scsi_Request *srp;
5372 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5373         Scsi_Cmnd *scp;
5374 #else
5375         Scsi_Cmnd scp;
5376 #endif
5377
5378     if (copy_from_user(&res, arg, sizeof(gdth_ioctl_reset)) ||
5379         res.ionode >= gdth_ctr_count || res.number >= MAX_HDRIVES)
5380         return -EFAULT;
5381     hanum = res.ionode;
5382     ha = HADATA(gdth_ctr_tab[hanum]);
5383  
5384     if (!ha->hdr[res.number].present)
5385         return 0;
5386     memset(&cmd, 0, sizeof(gdth_cmd_str));
5387     cmd.Service = CACHESERVICE;
5388     cmd.OpCode = GDT_CLUST_RESET;
5389     if (ha->cache_feat & GDT_64BIT)
5390         cmd.u.cache64.DeviceNo = res.number;
5391     else
5392         cmd.u.cache.DeviceNo = res.number;
5393 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5394     srp  = scsi_allocate_request(ha->sdev, GFP_KERNEL);
5395     if (!srp)
5396         return -ENOMEM;
5397     srp->sr_cmd_len = 12;
5398     srp->sr_use_sg = 0;
5399     gdth_do_req(srp, &cmd, cmnd, 30);
5400     res.status = (ushort)srp->sr_command->SCp.Status;
5401     scsi_release_request(srp);
5402 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5403     scp  = scsi_allocate_device(ha->sdev, 1, FALSE);
5404     if (!scp)
5405         return -ENOMEM;
5406     scp->cmd_len = 12;
5407     scp->use_sg = 0;
5408     gdth_do_cmd(scp, &cmd, cmnd, 30);
5409     res.status = (ushort)scp->SCp.Status;
5410     scsi_release_command(scp);
5411 #else
5412     memset(&ha->sdev,0,sizeof(Scsi_Device));
5413     memset(&scp, 0,sizeof(Scsi_Cmnd));
5414     ha->sdev.host = scp.host = gdth_ctr_tab[hanum];
5415     ha->sdev.id = scp.target = ha->sdev.host->this_id;
5416     scp.device = &ha->sdev;
5417     gdth_do_cmd(&scp, &cmd, cmnd, 30);
5418     res.status = (ushort)scp.SCp.Status;
5419 #endif
5420     if (copy_to_user(arg, &res, sizeof(gdth_ioctl_reset)))
5421         return -EFAULT;
5422     return 0;
5423 }
5424
5425 static int ioc_general(void __user *arg, char *cmnd)
5426 {
5427     gdth_ioctl_general gen;
5428     char *buf = NULL;
5429     ulong64 paddr; 
5430     int hanum;
5431         gdth_ha_str *ha; 
5432 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5433         Scsi_Request *srp;
5434 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5435         Scsi_Cmnd *scp;
5436 #else
5437         Scsi_Cmnd scp;
5438 #endif
5439         
5440     if (copy_from_user(&gen, arg, sizeof(gdth_ioctl_general)) ||
5441         gen.ionode >= gdth_ctr_count)
5442         return -EFAULT;
5443     hanum = gen.ionode; 
5444     ha = HADATA(gdth_ctr_tab[hanum]);
5445     if (gen.data_len + gen.sense_len != 0) {
5446         if (!(buf = gdth_ioctl_alloc(hanum, gen.data_len + gen.sense_len, 
5447                                      FALSE, &paddr)))
5448             return -EFAULT;
5449         if (copy_from_user(buf, arg + sizeof(gdth_ioctl_general),  
5450                            gen.data_len + gen.sense_len)) {
5451             gdth_ioctl_free(hanum, gen.data_len+gen.sense_len, buf, paddr);
5452             return -EFAULT;
5453         }
5454
5455         if (gen.command.OpCode == GDT_IOCTL) {
5456             gen.command.u.ioctl.p_param = paddr;
5457         } else if (gen.command.Service == CACHESERVICE) {
5458             if (ha->cache_feat & GDT_64BIT) {
5459                 /* copy elements from 32-bit IOCTL structure */
5460                 gen.command.u.cache64.BlockCnt = gen.command.u.cache.BlockCnt;
5461                 gen.command.u.cache64.BlockNo = gen.command.u.cache.BlockNo;
5462                 gen.command.u.cache64.DeviceNo = gen.command.u.cache.DeviceNo;
5463                 /* addresses */
5464                 if (ha->cache_feat & SCATTER_GATHER) {
5465                     gen.command.u.cache64.DestAddr = (ulong64)-1;
5466                     gen.command.u.cache64.sg_canz = 1;
5467                     gen.command.u.cache64.sg_lst[0].sg_ptr = paddr;
5468                     gen.command.u.cache64.sg_lst[0].sg_len = gen.data_len;
5469                     gen.command.u.cache64.sg_lst[1].sg_len = 0;
5470                 } else {
5471                     gen.command.u.cache64.DestAddr = paddr;
5472                     gen.command.u.cache64.sg_canz = 0;
5473                 }
5474             } else {
5475                 if (ha->cache_feat & SCATTER_GATHER) {
5476                     gen.command.u.cache.DestAddr = 0xffffffff;
5477                     gen.command.u.cache.sg_canz = 1;
5478                     gen.command.u.cache.sg_lst[0].sg_ptr = (ulong32)paddr;
5479                     gen.command.u.cache.sg_lst[0].sg_len = gen.data_len;
5480                     gen.command.u.cache.sg_lst[1].sg_len = 0;
5481                 } else {
5482                     gen.command.u.cache.DestAddr = paddr;
5483                     gen.command.u.cache.sg_canz = 0;
5484                 }
5485             }
5486         } else if (gen.command.Service == SCSIRAWSERVICE) {
5487             if (ha->raw_feat & GDT_64BIT) {
5488                 /* copy elements from 32-bit IOCTL structure */
5489                 char cmd[16];
5490                 gen.command.u.raw64.sense_len = gen.command.u.raw.sense_len;
5491                 gen.command.u.raw64.bus = gen.command.u.raw.bus;
5492                 gen.command.u.raw64.lun = gen.command.u.raw.lun;
5493                 gen.command.u.raw64.target = gen.command.u.raw.target;
5494                 memcpy(cmd, gen.command.u.raw.cmd, 16);
5495                 memcpy(gen.command.u.raw64.cmd, cmd, 16);
5496                 gen.command.u.raw64.clen = gen.command.u.raw.clen;
5497                 gen.command.u.raw64.sdlen = gen.command.u.raw.sdlen;
5498                 gen.command.u.raw64.direction = gen.command.u.raw.direction;
5499                 /* addresses */
5500                 if (ha->raw_feat & SCATTER_GATHER) {
5501                     gen.command.u.raw64.sdata = (ulong64)-1;
5502                     gen.command.u.raw64.sg_ranz = 1;
5503                     gen.command.u.raw64.sg_lst[0].sg_ptr = paddr;
5504                     gen.command.u.raw64.sg_lst[0].sg_len = gen.data_len;
5505                     gen.command.u.raw64.sg_lst[1].sg_len = 0;
5506                 } else {
5507                     gen.command.u.raw64.sdata = paddr;
5508                     gen.command.u.raw64.sg_ranz = 0;
5509                 }
5510                 gen.command.u.raw64.sense_data = paddr + gen.data_len;
5511             } else {
5512                 if (ha->raw_feat & SCATTER_GATHER) {
5513                     gen.command.u.raw.sdata = 0xffffffff;
5514                     gen.command.u.raw.sg_ranz = 1;
5515                     gen.command.u.raw.sg_lst[0].sg_ptr = (ulong32)paddr;
5516                     gen.command.u.raw.sg_lst[0].sg_len = gen.data_len;
5517                     gen.command.u.raw.sg_lst[1].sg_len = 0;
5518                 } else {
5519                     gen.command.u.raw.sdata = paddr;
5520                     gen.command.u.raw.sg_ranz = 0;
5521                 }
5522                 gen.command.u.raw.sense_data = (ulong32)paddr + gen.data_len;
5523             }
5524         } else {
5525             gdth_ioctl_free(hanum, gen.data_len+gen.sense_len, buf, paddr);
5526             return -EFAULT;
5527         }
5528     }
5529
5530 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5531     srp  = scsi_allocate_request(ha->sdev, GFP_KERNEL);
5532     if (!srp)
5533         return -ENOMEM;
5534     srp->sr_cmd_len = 12;
5535     srp->sr_use_sg = 0;
5536     gdth_do_req(srp, &gen.command, cmnd, gen.timeout);
5537     gen.status = srp->sr_command->SCp.Status;
5538     gen.info = srp->sr_command->SCp.Message;
5539     scsi_release_request(srp);
5540 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5541     scp  = scsi_allocate_device(ha->sdev, 1, FALSE);
5542     if (!scp)
5543         return -ENOMEM;
5544     scp->cmd_len = 12;
5545     scp->use_sg = 0;
5546     gdth_do_cmd(scp, &gen.command, cmnd, gen.timeout);
5547     gen.status = scp->SCp.Status;
5548     gen.info = scp->SCp.Message;
5549     scsi_release_command(scp);
5550 #else
5551     memset(&ha->sdev,0,sizeof(Scsi_Device));
5552     memset(&scp, 0,sizeof(Scsi_Cmnd));
5553     ha->sdev.host = scp.host = gdth_ctr_tab[hanum];
5554     ha->sdev.id = scp.target = ha->sdev.host->this_id;
5555     scp.device = &ha->sdev;
5556     gdth_do_cmd(&scp, &gen.command, cmnd, gen.timeout);
5557     gen.status = scp.SCp.Status;
5558     gen.info = scp.SCp.Message;
5559 #endif
5560
5561     if (copy_to_user(arg + sizeof(gdth_ioctl_general), buf, 
5562                      gen.data_len + gen.sense_len)) {
5563         gdth_ioctl_free(hanum, gen.data_len+gen.sense_len, buf, paddr);
5564         return -EFAULT; 
5565     } 
5566     if (copy_to_user(arg, &gen, 
5567         sizeof(gdth_ioctl_general) - sizeof(gdth_cmd_str))) {
5568         gdth_ioctl_free(hanum, gen.data_len+gen.sense_len, buf, paddr);
5569         return -EFAULT;
5570     }
5571     gdth_ioctl_free(hanum, gen.data_len+gen.sense_len, buf, paddr);
5572     return 0;
5573 }
5574  
5575 static int ioc_hdrlist(void __user *arg, char *cmnd)
5576 {
5577     gdth_ioctl_rescan rsc;
5578     gdth_cmd_str cmd;
5579     gdth_ha_str *ha;
5580     unchar i;
5581     int hanum;
5582 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5583     Scsi_Request *srp;
5584 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5585     Scsi_Cmnd *scp;
5586 #else
5587     Scsi_Cmnd scp;
5588 #endif
5589         
5590     if (copy_from_user(&rsc, arg, sizeof(gdth_ioctl_rescan)) ||
5591         rsc.ionode >= gdth_ctr_count)
5592         return -EFAULT;
5593     hanum = rsc.ionode;
5594     ha = HADATA(gdth_ctr_tab[hanum]);
5595     memset(&cmd, 0, sizeof(gdth_cmd_str));
5596    
5597 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5598     srp  = scsi_allocate_request(ha->sdev, GFP_KERNEL);
5599     if (!srp)
5600         return -ENOMEM;
5601     srp->sr_cmd_len = 12;
5602     srp->sr_use_sg = 0;
5603 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5604     scp  = scsi_allocate_device(ha->sdev, 1, FALSE);
5605     if (!scp)
5606         return -ENOMEM;
5607     scp->cmd_len = 12;
5608     scp->use_sg = 0;
5609 #else
5610     memset(&ha->sdev,0,sizeof(Scsi_Device));
5611     memset(&scp, 0,sizeof(Scsi_Cmnd));
5612     ha->sdev.host = scp.host = gdth_ctr_tab[hanum];
5613     ha->sdev.id = scp.target = ha->sdev.host->this_id;
5614     scp.device = &ha->sdev;
5615 #endif
5616
5617     for (i = 0; i < MAX_HDRIVES; ++i) { 
5618         if (!ha->hdr[i].present) {
5619             rsc.hdr_list[i].bus = 0xff; 
5620             continue;
5621         } 
5622         rsc.hdr_list[i].bus = ha->virt_bus;
5623         rsc.hdr_list[i].target = i;
5624         rsc.hdr_list[i].lun = 0;
5625         rsc.hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
5626         if (ha->hdr[i].cluster_type & CLUSTER_DRIVE) { 
5627             cmd.Service = CACHESERVICE;
5628             cmd.OpCode = GDT_CLUST_INFO;
5629             if (ha->cache_feat & GDT_64BIT)
5630                 cmd.u.cache64.DeviceNo = i;
5631             else
5632                 cmd.u.cache.DeviceNo = i;
5633 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5634             gdth_do_req(srp, &cmd, cmnd, 30);
5635             if (srp->sr_command->SCp.Status == S_OK)
5636                 rsc.hdr_list[i].cluster_type = srp->sr_command->SCp.Message;
5637 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5638             gdth_do_cmd(scp, &cmd, cmnd, 30);
5639             if (scp->SCp.Status == S_OK)
5640                 rsc.hdr_list[i].cluster_type = scp->SCp.Message;
5641 #else
5642             gdth_do_cmd(&scp, &cmd, cmnd, 30);
5643             if (scp.SCp.Status == S_OK)
5644                 rsc.hdr_list[i].cluster_type = scp.SCp.Message;
5645 #endif
5646         }
5647     } 
5648 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5649     scsi_release_request(srp);
5650 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5651     scsi_release_command(scp);
5652 #endif       
5653  
5654     if (copy_to_user(arg, &rsc, sizeof(gdth_ioctl_rescan)))
5655         return -EFAULT;
5656     return 0;
5657 }
5658
5659 static int ioc_rescan(void __user *arg, char *cmnd)
5660 {
5661     gdth_ioctl_rescan rsc;
5662     gdth_cmd_str cmd;
5663     ushort i, status, hdr_cnt;
5664     ulong32 info;
5665     int hanum, cyls, hds, secs;
5666     ulong flags;
5667     gdth_ha_str *ha; 
5668 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5669     Scsi_Request *srp;
5670 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5671     Scsi_Cmnd *scp;
5672 #else
5673     Scsi_Cmnd scp;
5674 #endif
5675         
5676     if (copy_from_user(&rsc, arg, sizeof(gdth_ioctl_rescan)) ||
5677         rsc.ionode >= gdth_ctr_count)
5678         return -EFAULT;
5679     hanum = rsc.ionode;
5680     ha = HADATA(gdth_ctr_tab[hanum]);
5681     memset(&cmd, 0, sizeof(gdth_cmd_str));
5682
5683 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5684     srp  = scsi_allocate_request(ha->sdev, GFP_KERNEL);
5685     if (!srp)
5686         return -ENOMEM;
5687     srp->sr_cmd_len = 12;
5688     srp->sr_use_sg = 0;
5689 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5690     scp  = scsi_allocate_device(ha->sdev, 1, FALSE);
5691     if (!scp)
5692         return -ENOMEM;
5693     scp->cmd_len = 12;
5694     scp->use_sg = 0;
5695 #else
5696     memset(&ha->sdev,0,sizeof(Scsi_Device));
5697     memset(&scp, 0,sizeof(Scsi_Cmnd));
5698     ha->sdev.host = scp.host = gdth_ctr_tab[hanum];
5699     ha->sdev.id = scp.target = ha->sdev.host->this_id;
5700     scp.device = &ha->sdev;
5701 #endif
5702      
5703     if (rsc.flag == 0) {
5704         /* old method: re-init. cache service */
5705         cmd.Service = CACHESERVICE;
5706         if (ha->cache_feat & GDT_64BIT) {
5707             cmd.OpCode = GDT_X_INIT_HOST;
5708             cmd.u.cache64.DeviceNo = LINUX_OS;
5709         } else {
5710             cmd.OpCode = GDT_INIT;
5711             cmd.u.cache.DeviceNo = LINUX_OS;
5712         }
5713 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5714         gdth_do_req(srp, &cmd, cmnd, 30);
5715         status = (ushort)srp->sr_command->SCp.Status;
5716         info = (ulong32)srp->sr_command->SCp.Message;
5717 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5718         gdth_do_cmd(scp, &cmd, cmnd, 30);
5719         status = (ushort)scp->SCp.Status;
5720         info = (ulong32)scp->SCp.Message;
5721 #else
5722         gdth_do_cmd(&scp, &cmd, cmnd, 30);
5723         status = (ushort)scp.SCp.Status;
5724         info = (ulong32)scp.SCp.Message;
5725 #endif
5726         i = 0;
5727         hdr_cnt = (status == S_OK ? (ushort)info : 0);
5728     } else {
5729         i = rsc.hdr_no;
5730         hdr_cnt = i + 1;
5731     }
5732     for (; i < hdr_cnt && i < MAX_HDRIVES; ++i) {
5733         cmd.Service = CACHESERVICE;
5734         cmd.OpCode = GDT_INFO;
5735         if (ha->cache_feat & GDT_64BIT) 
5736             cmd.u.cache64.DeviceNo = i;
5737         else 
5738             cmd.u.cache.DeviceNo = i;
5739 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5740         gdth_do_req(srp, &cmd, cmnd, 30);
5741         status = (ushort)srp->sr_command->SCp.Status;
5742         info = (ulong32)srp->sr_command->SCp.Message;
5743 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5744         gdth_do_cmd(scp, &cmd, cmnd, 30);
5745         status = (ushort)scp->SCp.Status;
5746         info = (ulong32)scp->SCp.Message;
5747 #else
5748         gdth_do_cmd(&scp, &cmd, cmnd, 30);
5749         status = (ushort)scp.SCp.Status;
5750         info = (ulong32)scp.SCp.Message;
5751 #endif
5752         GDTH_LOCK_HA(ha, flags);
5753         rsc.hdr_list[i].bus = ha->virt_bus;
5754         rsc.hdr_list[i].target = i;
5755         rsc.hdr_list[i].lun = 0;
5756         if (status != S_OK) {
5757             ha->hdr[i].present = FALSE;
5758         } else {
5759             ha->hdr[i].present = TRUE;
5760             ha->hdr[i].size = info;
5761             /* evaluate mapping */
5762             ha->hdr[i].size &= ~SECS32;
5763             gdth_eval_mapping(ha->hdr[i].size,&cyls,&hds,&secs); 
5764             ha->hdr[i].heads = hds;
5765             ha->hdr[i].secs = secs;
5766             /* round size */
5767             ha->hdr[i].size = cyls * hds * secs;
5768         }
5769         GDTH_UNLOCK_HA(ha, flags);
5770         if (status != S_OK)
5771             continue; 
5772         
5773         /* extended info, if GDT_64BIT, for drives > 2 TB */
5774         /* but we need ha->info2, not yet stored in scp->SCp */
5775
5776         /* devtype, cluster info, R/W attribs */
5777         cmd.Service = CACHESERVICE;
5778         cmd.OpCode = GDT_DEVTYPE;
5779         if (ha->cache_feat & GDT_64BIT) 
5780             cmd.u.cache64.DeviceNo = i;
5781         else
5782             cmd.u.cache.DeviceNo = i;
5783 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5784         gdth_do_req(srp, &cmd, cmnd, 30);
5785         status = (ushort)srp->sr_command->SCp.Status;
5786         info = (ulong32)srp->sr_command->SCp.Message;
5787 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5788         gdth_do_cmd(scp, &cmd, cmnd, 30);
5789         status = (ushort)scp->SCp.Status;
5790         info = (ulong32)scp->SCp.Message;
5791 #else
5792         gdth_do_cmd(&scp, &cmd, cmnd, 30);
5793         status = (ushort)scp.SCp.Status;
5794         info = (ulong32)scp.SCp.Message;
5795 #endif
5796         GDTH_LOCK_HA(ha, flags);
5797         ha->hdr[i].devtype = (status == S_OK ? (ushort)info : 0);
5798         GDTH_UNLOCK_HA(ha, flags);
5799
5800         cmd.Service = CACHESERVICE;
5801         cmd.OpCode = GDT_CLUST_INFO;
5802         if (ha->cache_feat & GDT_64BIT) 
5803             cmd.u.cache64.DeviceNo = i;
5804         else
5805             cmd.u.cache.DeviceNo = i;
5806 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5807         gdth_do_req(srp, &cmd, cmnd, 30);
5808         status = (ushort)srp->sr_command->SCp.Status;
5809         info = (ulong32)srp->sr_command->SCp.Message;
5810 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5811         gdth_do_cmd(scp, &cmd, cmnd, 30);
5812         status = (ushort)scp->SCp.Status;
5813         info = (ulong32)scp->SCp.Message;
5814 #else
5815         gdth_do_cmd(&scp, &cmd, cmnd, 30);
5816         status = (ushort)scp.SCp.Status;
5817         info = (ulong32)scp.SCp.Message;
5818 #endif
5819         GDTH_LOCK_HA(ha, flags);
5820         ha->hdr[i].cluster_type = 
5821             ((status == S_OK && !shared_access) ? (ushort)info : 0);
5822         GDTH_UNLOCK_HA(ha, flags);
5823         rsc.hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
5824
5825         cmd.Service = CACHESERVICE;
5826         cmd.OpCode = GDT_RW_ATTRIBS;
5827         if (ha->cache_feat & GDT_64BIT) 
5828             cmd.u.cache64.DeviceNo = i;
5829         else
5830             cmd.u.cache.DeviceNo = i;
5831 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5832         gdth_do_req(srp, &cmd, cmnd, 30);
5833         status = (ushort)srp->sr_command->SCp.Status;
5834         info = (ulong32)srp->sr_command->SCp.Message;
5835 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5836         gdth_do_cmd(scp, &cmd, cmnd, 30);
5837         status = (ushort)scp->SCp.Status;
5838         info = (ulong32)scp->SCp.Message;
5839 #else
5840         gdth_do_cmd(&scp, &cmd, cmnd, 30);
5841         status = (ushort)scp.SCp.Status;
5842         info = (ulong32)scp.SCp.Message;
5843 #endif
5844         GDTH_LOCK_HA(ha, flags);
5845         ha->hdr[i].rw_attribs = (status == S_OK ? (ushort)info : 0);
5846         GDTH_UNLOCK_HA(ha, flags);
5847     }
5848 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5849     scsi_release_request(srp);
5850 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5851     scsi_release_command(scp);
5852 #endif       
5853  
5854     if (copy_to_user(arg, &rsc, sizeof(gdth_ioctl_rescan)))
5855         return -EFAULT;
5856     return 0;
5857 }
5858   
5859 static int gdth_ioctl(struct inode *inode, struct file *filep,
5860                       unsigned int cmd, unsigned long arg)
5861 {
5862     gdth_ha_str *ha; 
5863 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5864     Scsi_Cmnd *scp;
5865 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
5866     Scsi_Cmnd *scp;
5867 #else
5868     Scsi_Cmnd scp;
5869 #endif
5870     ulong flags;
5871     char cmnd[MAX_COMMAND_SIZE];   
5872     void __user *argp = (void __user *)arg;
5873
5874     memset(cmnd, 0xff, 12);
5875     
5876     TRACE(("gdth_ioctl() cmd 0x%x\n", cmd));
5877  
5878     switch (cmd) {
5879       case GDTIOCTL_CTRCNT:
5880       { 
5881         int cnt = gdth_ctr_count;
5882         if (put_user(cnt, (int __user *)argp))
5883                 return -EFAULT;
5884         break;
5885       }
5886
5887       case GDTIOCTL_DRVERS:
5888       { 
5889         int ver = (GDTH_VERSION<<8) | GDTH_SUBVERSION;
5890         if (put_user(ver, (int __user *)argp))
5891                 return -EFAULT;
5892         break;
5893       }
5894       
5895       case GDTIOCTL_OSVERS:
5896       { 
5897         gdth_ioctl_osvers osv; 
5898
5899         osv.version = (unchar)(LINUX_VERSION_CODE >> 16);
5900         osv.subversion = (unchar)(LINUX_VERSION_CODE >> 8);
5901         osv.revision = (ushort)(LINUX_VERSION_CODE & 0xff);
5902         if (copy_to_user(argp, &osv, sizeof(gdth_ioctl_osvers)))
5903                 return -EFAULT;
5904         break;
5905       }
5906
5907       case GDTIOCTL_CTRTYPE:
5908       { 
5909         gdth_ioctl_ctrtype ctrt;
5910         
5911         if (copy_from_user(&ctrt, argp, sizeof(gdth_ioctl_ctrtype)) ||
5912             ctrt.ionode >= gdth_ctr_count)
5913             return -EFAULT;
5914         ha = HADATA(gdth_ctr_tab[ctrt.ionode]);
5915         if (ha->type == GDT_ISA || ha->type == GDT_EISA) {
5916             ctrt.type = (unchar)((ha->stype>>20) - 0x10);
5917         } else {
5918             if (ha->type != GDT_PCIMPR) {
5919                 ctrt.type = (unchar)((ha->stype<<4) + 6);
5920             } else {
5921                 ctrt.type = 
5922                     (ha->oem_id == OEM_ID_INTEL ? 0xfd : 0xfe);
5923                 if (ha->stype >= 0x300)
5924                     ctrt.ext_type = 0x6000 | ha->subdevice_id;
5925                 else 
5926                     ctrt.ext_type = 0x6000 | ha->stype;
5927             }
5928             ctrt.device_id = ha->stype;
5929             ctrt.sub_device_id = ha->subdevice_id;
5930         }
5931         ctrt.info = ha->brd_phys;
5932         ctrt.oem_id = ha->oem_id;
5933         if (copy_to_user(argp, &ctrt, sizeof(gdth_ioctl_ctrtype)))
5934             return -EFAULT;
5935         break;
5936       }
5937         
5938       case GDTIOCTL_GENERAL:
5939         return ioc_general(argp, cmnd);
5940
5941       case GDTIOCTL_EVENT:
5942         return ioc_event(argp);
5943
5944       case GDTIOCTL_LOCKDRV:
5945         return ioc_lockdrv(argp);
5946
5947       case GDTIOCTL_LOCKCHN:
5948       {
5949         gdth_ioctl_lockchn lchn;
5950         unchar i, j;
5951
5952         if (copy_from_user(&lchn, argp, sizeof(gdth_ioctl_lockchn)) ||
5953             lchn.ionode >= gdth_ctr_count)
5954             return -EFAULT;
5955         ha = HADATA(gdth_ctr_tab[lchn.ionode]);
5956         
5957         i = lchn.channel;
5958         if (i < ha->bus_cnt) {
5959             if (lchn.lock) {
5960                 GDTH_LOCK_HA(ha, flags);
5961                 ha->raw[i].lock = 1;
5962                 GDTH_UNLOCK_HA(ha, flags);
5963                 for (j = 0; j < ha->tid_cnt; ++j) {
5964                     gdth_wait_completion(lchn.ionode, i, j); 
5965                     gdth_stop_timeout(lchn.ionode, i, j); 
5966                 }
5967             } else {
5968                 GDTH_LOCK_HA(ha, flags);
5969                 ha->raw[i].lock = 0;
5970                 GDTH_UNLOCK_HA(ha, flags);
5971                 for (j = 0; j < ha->tid_cnt; ++j) {
5972                     gdth_start_timeout(lchn.ionode, i, j); 
5973                     gdth_next(lchn.ionode); 
5974                 }
5975             }
5976         } 
5977         break;
5978       }
5979
5980       case GDTIOCTL_RESCAN:
5981         return ioc_rescan(argp, cmnd);
5982
5983       case GDTIOCTL_HDRLIST:
5984         return ioc_hdrlist(argp, cmnd);
5985
5986       case GDTIOCTL_RESET_BUS:
5987       {
5988         gdth_ioctl_reset res;
5989         int hanum, rval;
5990
5991         if (copy_from_user(&res, argp, sizeof(gdth_ioctl_reset)) ||
5992             res.ionode >= gdth_ctr_count)
5993             return -EFAULT;
5994         hanum = res.ionode; 
5995         ha = HADATA(gdth_ctr_tab[hanum]);
5996
5997         /* Because we need a Scsi_Cmnd struct., we make a scsi_allocate device also for kernels >=2.6.x */        
5998 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
5999         scp  = scsi_get_command(ha->sdev, GFP_KERNEL);
6000         if (!scp)
6001             return -ENOMEM;
6002         scp->cmd_len = 12;
6003         scp->use_sg = 0;
6004         scp->device->channel = virt_ctr ? 0 : res.number;
6005         rval = gdth_eh_bus_reset(scp);
6006         res.status = (rval == SUCCESS ? S_OK : S_GENERR);
6007         scsi_put_command(scp);
6008 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
6009         scp  = scsi_allocate_device(ha->sdev, 1, FALSE);
6010         if (!scp)
6011             return -ENOMEM;
6012         scp->cmd_len = 12;
6013         scp->use_sg = 0;
6014         scp->channel = virt_ctr ? 0 : res.number;
6015         rval = gdth_eh_bus_reset(scp);
6016         res.status = (rval == SUCCESS ? S_OK : S_GENERR);
6017         scsi_release_command(scp);
6018 #else 
6019         memset(&ha->sdev,0,sizeof(Scsi_Device));
6020         memset(&scp, 0,sizeof(Scsi_Cmnd));
6021         ha->sdev.host = scp.host = gdth_ctr_tab[hanum];
6022         ha->sdev.id = scp.target = ha->sdev.host->this_id;
6023         scp.device = &ha->sdev;
6024         scp.channel = virt_ctr ? 0 : res.number;
6025         rval = gdth_eh_bus_reset(&scp);
6026         res.status = (rval == SUCCESS ? S_OK : S_GENERR);
6027 #endif
6028         if (copy_to_user(argp, &res, sizeof(gdth_ioctl_reset)))
6029             return -EFAULT;
6030         break;
6031       }
6032
6033       case GDTIOCTL_RESET_DRV:
6034         return ioc_resetdrv(argp, cmnd);
6035
6036       default:
6037         break; 
6038     }
6039     return 0;
6040 }
6041
6042
6043 /* flush routine */
6044 static void gdth_flush(int hanum)
6045 {
6046     int             i;
6047     gdth_ha_str     *ha;
6048     gdth_cmd_str    gdtcmd;
6049 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
6050     Scsi_Request    *srp;
6051     Scsi_Device     *sdev;
6052 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
6053     Scsi_Cmnd       *scp;
6054     Scsi_Device     *sdev;
6055 #else
6056     Scsi_Cmnd       scp;
6057     Scsi_Device     sdev;
6058 #endif
6059     char            cmnd[MAX_COMMAND_SIZE];   
6060     memset(cmnd, 0xff, MAX_COMMAND_SIZE);
6061
6062     TRACE2(("gdth_flush() hanum %d\n",hanum));
6063     ha = HADATA(gdth_ctr_tab[hanum]);
6064
6065 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
6066     sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
6067     srp  = scsi_allocate_request(sdev, GFP_KERNEL);
6068     if (!srp)
6069         return;
6070     srp->sr_cmd_len = 12;
6071     srp->sr_use_sg = 0;
6072 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
6073     sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
6074     scp  = scsi_allocate_device(sdev, 1, FALSE);
6075     if (!scp)
6076         return;
6077     scp->cmd_len = 12;
6078     scp->use_sg = 0;
6079 #else
6080     memset(&sdev,0,sizeof(Scsi_Device));
6081     memset(&scp, 0,sizeof(Scsi_Cmnd));
6082     sdev.host = scp.host = gdth_ctr_tab[hanum];
6083     sdev.id = scp.target = sdev.host->this_id;
6084     scp.device = &sdev;
6085 #endif
6086
6087     for (i = 0; i < MAX_HDRIVES; ++i) {
6088         if (ha->hdr[i].present) {
6089             gdtcmd.BoardNode = LOCALBOARD;
6090             gdtcmd.Service = CACHESERVICE;
6091             gdtcmd.OpCode = GDT_FLUSH;
6092             if (ha->cache_feat & GDT_64BIT) { 
6093                 gdtcmd.u.cache64.DeviceNo = i;
6094                 gdtcmd.u.cache64.BlockNo = 1;
6095                 gdtcmd.u.cache64.sg_canz = 0;
6096             } else {
6097                 gdtcmd.u.cache.DeviceNo = i;
6098                 gdtcmd.u.cache.BlockNo = 1;
6099                 gdtcmd.u.cache.sg_canz = 0;
6100             }
6101             TRACE2(("gdth_flush(): flush ha %d drive %d\n", hanum, i));
6102 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
6103             gdth_do_req(srp, &gdtcmd, cmnd, 30);
6104 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
6105             gdth_do_cmd(scp, &gdtcmd, cmnd, 30);
6106 #else
6107             gdth_do_cmd(&scp, &gdtcmd, cmnd, 30);
6108 #endif
6109         }
6110     }
6111 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
6112     scsi_release_request(srp);
6113     scsi_free_host_dev(sdev);
6114 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
6115     scsi_release_command(scp);
6116     scsi_free_host_dev(sdev);
6117 #endif
6118 }
6119
6120 /* shutdown routine */
6121 static int gdth_halt(struct notifier_block *nb, ulong event, void *buf)
6122 {
6123     int             hanum;
6124 #ifndef __alpha__
6125     gdth_cmd_str    gdtcmd;
6126 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
6127     Scsi_Request    *srp;
6128     Scsi_Device     *sdev;
6129 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
6130     Scsi_Cmnd       *scp;
6131     Scsi_Device     *sdev;
6132 #else
6133     Scsi_Cmnd       scp;
6134     Scsi_Device     sdev;
6135 #endif
6136     char            cmnd[MAX_COMMAND_SIZE];   
6137 #endif
6138
6139     TRACE2(("gdth_halt() event %d\n",(int)event));
6140     if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
6141         return NOTIFY_DONE;
6142
6143     printk("GDT-HA: Flushing all host drives .. ");
6144     for (hanum = 0; hanum < gdth_ctr_count; ++hanum) {
6145         gdth_flush(hanum);
6146
6147 #ifndef __alpha__
6148         /* controller reset */
6149         memset(cmnd, 0xff, MAX_COMMAND_SIZE);
6150         gdtcmd.BoardNode = LOCALBOARD;
6151         gdtcmd.Service = CACHESERVICE;
6152         gdtcmd.OpCode = GDT_RESET;
6153         TRACE2(("gdth_halt(): reset controller %d\n", hanum));
6154 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0)
6155         sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
6156         srp  = scsi_allocate_request(sdev, GFP_KERNEL);
6157         if (!srp) {
6158             unregister_reboot_notifier(&gdth_notifier);
6159             return NOTIFY_OK;
6160         }
6161         srp->sr_cmd_len = 12;
6162         srp->sr_use_sg = 0;
6163         gdth_do_req(srp, &gdtcmd, cmnd, 10);
6164         scsi_release_request(srp);
6165         scsi_free_host_dev(sdev);
6166 #elif LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
6167         sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
6168         scp  = scsi_allocate_device(sdev, 1, FALSE);
6169         if (!scp) {
6170             unregister_reboot_notifier(&gdth_notifier);
6171             return NOTIFY_OK;
6172         }
6173         scp->cmd_len = 12;
6174         scp->use_sg = 0;
6175         gdth_do_cmd(scp, &gdtcmd, cmnd, 10);
6176         scsi_release_command(scp);
6177         scsi_free_host_dev(sdev);
6178 #else
6179         memset(&sdev,0,sizeof(Scsi_Device));
6180         memset(&scp, 0,sizeof(Scsi_Cmnd));
6181         sdev.host = scp.host = gdth_ctr_tab[hanum];
6182         sdev.id = scp.target = sdev.host->this_id;
6183         scp.device = &sdev;
6184         gdth_do_cmd(&scp, &gdtcmd, cmnd, 10);
6185 #endif
6186 #endif
6187     }
6188     printk("Done.\n");
6189
6190 #ifdef GDTH_STATISTICS
6191     del_timer(&gdth_timer);
6192 #endif
6193     unregister_reboot_notifier(&gdth_notifier);
6194     return NOTIFY_OK;
6195 }
6196
6197
6198 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,0) && !defined(MODULE)
6199
6200 GDTH_INITFUNC(void, gdth_setup(char *str,int *ints)) 
6201 {    
6202     TRACE2(("gdth_setup() str %s ints[0] %d\n", 
6203             str ? str:"NULL", ints ? ints[0]:0));
6204     internal_setup(str, ints);
6205 }
6206
6207 #else
6208
6209 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,0)
6210 static Scsi_Host_Template driver_template = {
6211         .proc_name              = "gdth", 
6212         .proc_info              = gdth_proc_info,
6213         .name                   = "GDT SCSI Disk Array Controller",
6214         .detect                 = gdth_detect, 
6215         .release                = gdth_release,
6216         .info                   = gdth_info, 
6217         .queuecommand           = gdth_queuecommand,
6218         .eh_abort_handler       = gdth_eh_abort, 
6219         .eh_device_reset_handler = gdth_eh_device_reset,
6220         .eh_bus_reset_handler   = gdth_eh_bus_reset,
6221         .eh_host_reset_handler  = gdth_eh_host_reset,
6222         .bios_param             = gdth_bios_param,
6223         .can_queue              = GDTH_MAXCMDS,
6224         .this_id                = -1,
6225         .sg_tablesize           = GDTH_MAXSG,
6226         .cmd_per_lun            = GDTH_MAXC_P_L,
6227         .unchecked_isa_dma      = 1,
6228         .use_clustering         = ENABLE_CLUSTERING,
6229 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
6230         .use_new_eh_code        = 1,
6231 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,20)
6232         .highmem_io             = 1,
6233 #endif
6234 #endif
6235 };
6236 #else
6237 static Scsi_Host_Template driver_template = GDTH;
6238 #endif
6239
6240 #include "scsi_module.c"
6241 #ifndef MODULE
6242 __setup("gdth=", option_setup);
6243 #endif
6244
6245 #endif