gdth.c 202 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1
/************************************************************************
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4
 * Linux driver for                                                     *  
 * ICP vortex GmbH:    GDT ISA/EISA/PCI Disk Array Controllers          *
 * Intel Corporation:  Storage RAID Controllers                         *
Linus Torvalds's avatar
Linus Torvalds committed
5 6
 *                                                                      *
 * gdth.c                                                               *
Alan Cox's avatar
Alan Cox committed
7
 * Copyright (C) 1995-03 ICP vortex, an Intel company,  Achim Leubner   *
8
 * <achim.leubner@intel.com>                                            *
Linus Torvalds's avatar
Linus Torvalds committed
9
 *                                                                      *
10 11
 * Additions/Fixes: Boji Tony Kannanthanam                              *
 * <boji.t.kannanthanam@intel.com>                                      *
Linus Torvalds's avatar
Linus Torvalds committed
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *                                                                      *
 * This program is free software; you can redistribute it and/or modify *
 * it under the terms of the GNU General Public License as published    *
 * by the Free Software Foundation; either version 2 of the License,    *
 * or (at your option) any later version.                               *
 *                                                                      *
 * This program is distributed in the hope that it will be useful,      *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
 * GNU General Public License for more details.                         *
 *                                                                      *
 * You should have received a copy of the GNU General Public License    *
 * along with this kernel; if not, write to the Free Software           *
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
 *                                                                      *
Alan Cox's avatar
Alan Cox committed
27
 * Tested with Linux 1.2.13, ..., 2.2.20, ..., 2.4.20                   *
Linus Torvalds's avatar
Linus Torvalds committed
28 29
 *                                                                      *
 * $Log: gdth.c,v $
Alan Cox's avatar
Alan Cox committed
30 31 32 33 34 35 36 37
 * Revision 1.62  2003/02/27 15:01:59  achim
 * Dynamic DMA mapping implemented
 * New (character device) IOCTL interface added
 * Other controller related changes made
 *
 * Revision 1.61  2002/11/08 13:09:52  boji
 * Added support for XSCALE based RAID Controllers
 * Fixed SCREENSERVICE initialization in SMP cases
38 39 40 41 42 43 44 45 46 47 48 49
 * Added checks for gdth_polling before GDTH_HA_LOCK
 *
 * Revision 1.60  2002/02/05 09:35:22  achim
 * MODULE_LICENSE only if kernel >= 2.4.11
 *
 * Revision 1.59  2002/01/30 09:46:33  achim
 * Small changes
 *
 * Revision 1.58  2002/01/29 15:30:02  achim
 * Set default value of shared_access to Y
 * New status S_CACHE_RESERV for clustering added
 *
Linus Torvalds's avatar
Linus Torvalds committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
 * Revision 1.57  2001/08/21 11:16:35  achim
 * Bugfix free_irq()
 *
 * Revision 1.56  2001/08/09 11:19:39  achim
 * Scsi_Host_Template changes
 *
 * Revision 1.55  2001/08/09 10:11:28  achim
 * Command HOST_UNFREEZE_IO before cache service init.
 *
 * Revision 1.54  2001/07/20 13:48:12  achim
 * Expand: gdth_analyse_hdrive() removed
 *
 * Revision 1.53  2001/07/17 09:52:49  achim
 * Small OEM related change
 *
 * Revision 1.52  2001/06/19 15:06:20  achim
 * New host command GDT_UNFREEZE_IO added
 *
 * Revision 1.51  2001/05/22 06:42:37  achim
 * PCI: Subdevice ID added
 *
 * Revision 1.50  2001/05/17 13:42:16  achim
 * Support for Intel Storage RAID Controllers added
 *
 * Revision 1.50  2001/05/17 12:12:34  achim
 * Support for Intel Storage RAID Controllers added
 *
Linus Torvalds's avatar
Linus Torvalds committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
 * Revision 1.49  2001/03/15 15:07:17  achim
 * New __setup interface for boot command line options added
 *
 * Revision 1.48  2001/02/06 12:36:28  achim
 * Bugfix Cluster protocol
 *
 * Revision 1.47  2001/01/10 14:42:06  achim
 * New switch shared_access added
 *
 * Revision 1.46  2001/01/09 08:11:35  achim
 * gdth_command() removed
 * meaning of Scsi_Pointer members changed
 *
 * Revision 1.45  2000/11/16 12:02:24  achim
 * Changes for kernel 2.4
 *
 * Revision 1.44  2000/10/11 08:44:10  achim
 * Clustering changes: New flag media_changed added
 *
 * Revision 1.43  2000/09/20 12:59:01  achim
 * DPMEM remap functions for all PCI controller types implemented
 * Small changes for ia64 platform
 *
 * Revision 1.42  2000/07/20 09:04:50  achim
 * Small changes for kernel 2.4
 *
 * Revision 1.41  2000/07/04 14:11:11  achim
 * gdth_analyse_hdrive() added to rescan drives after online expansion
 *
 * Revision 1.40  2000/06/27 11:24:16  achim
 * Changes Clustering, Screenservice
 *
 * Revision 1.39  2000/06/15 13:09:04  achim
 * Changes for gdth_do_cmd()
 *
 * Revision 1.38  2000/06/15 12:08:43  achim
 * Bugfix gdth_sync_event(), service SCREENSERVICE
 * Data direction for command 0xc2 changed to DOU
 *
 * Revision 1.37  2000/05/25 13:50:10  achim
 * New driver parameter virt_ctr added
 *
 * Revision 1.36  2000/05/04 08:50:46  achim
 * Event buffer now in gdth_ha_str
 *
 * Revision 1.35  2000/03/03 10:44:08  achim
 * New event_string only valid for the RP controller family
 *
 * Revision 1.34  2000/03/02 14:55:29  achim
 * New mechanism for async. event handling implemented
 *
 * Revision 1.33  2000/02/21 15:37:37  achim
 * Bugfix Alpha platform + DPMEM above 4GB
 *
 * Revision 1.32  2000/02/14 16:17:37  achim
 * Bugfix sense_buffer[] + raw devices
 *
 * Revision 1.31  2000/02/10 10:29:00  achim
 * Delete sense_buffer[0], if command OK
 *
 * Revision 1.30  1999/11/02 13:42:39  achim
 * ARRAY_DRV_LIST2 implemented
 * Now 255 log. and 100 host drives supported
 *
 * Revision 1.29  1999/10/05 13:28:47  achim
 * GDT_CLUST_RESET added
 *
 * Revision 1.28  1999/08/12 13:44:54  achim
 * MOUNTALL removed
 * Cluster drives -> removeable drives
 *
 * Revision 1.27  1999/06/22 07:22:38  achim
 * Small changes
 *
 * Revision 1.26  1999/06/10 16:09:12  achim
 * Cluster Host Drive support: Bugfixes
 *
 * Revision 1.25  1999/06/01 16:03:56  achim
 * gdth_init_pci(): Manipulate config. space to start RP controller
 *
 * Revision 1.24  1999/05/26 11:53:06  achim
 * Cluster Host Drive support added
 *
Linus Torvalds's avatar
Linus Torvalds committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
 * Revision 1.23  1999/03/26 09:12:31  achim
 * Default value for hdr_channel set to 0
 *
 * Revision 1.22  1999/03/22 16:27:16  achim
 * Bugfix: gdth_store_event() must not be locked with GDTH_LOCK_HA()
 *
 * Revision 1.21  1999/03/16 13:40:34  achim
 * Problems with reserved drives solved
 * gdth_eh_bus_reset() implemented
 *
 * Revision 1.20  1999/03/10 09:08:13  achim
 * Bugfix: Corrections in gdth_direction_tab[] made
 * Bugfix: Increase command timeout (gdth_update_timeout()) NOT in gdth_putq()
 *
 * Revision 1.19  1999/03/05 14:38:16  achim
 * Bugfix: Heads/Sectors mapping for reserved devices possibly wrong
 * -> gdth_eval_mapping() implemented, changes in gdth_bios_param()
 * INIT_RETRIES set to 100s to avoid DEINIT-Timeout for controllers
 * with BIOS disabled and memory test set to Intensive
 * Enhanced /proc support
 *
 * Revision 1.18  1999/02/24 09:54:33  achim
 * Command line parameter hdr_channel implemented
 * Bugfix for EISA controllers + Linux 2.2.x
 *
 * Revision 1.17  1998/12/17 15:58:11  achim
 * Command line parameters implemented
 * Changes for Alpha platforms
 * PCI controller scan changed
 * SMP support improved (spin_lock_irqsave(),...)
 * New async. events, new scan/reserve commands included
 *
 * Revision 1.16  1998/09/28 16:08:46  achim
 * GDT_PCIMPR: DPMEM remapping, if required
 * mdelay() added
 *
 * Revision 1.15  1998/06/03 14:54:06  achim
 * gdth_delay(), gdth_flush() implemented
 * Bugfix: gdth_release() changed
 *
 * Revision 1.14  1998/05/22 10:01:17  achim
 * mj: pcibios_strerror() removed
 * Improved SMP support (if version >= 2.1.95)
 * gdth_halt(): halt_called flag added (if version < 2.1)
 *
 * Revision 1.13  1998/04/16 09:14:57  achim
 * Reserve drives (for raw service) implemented
 * New error handling code enabled
 * Get controller name from board_info() IOCTL
 * Final round of PCI device driver patches by Martin Mares
 *
 * Revision 1.12  1998/03/03 09:32:37  achim
 * Fibre channel controller support added
 *
 * Revision 1.11  1998/01/27 16:19:14  achim
 * SA_SHIRQ added
 * add_timer()/del_timer() instead of GDTH_TIMER
 * scsi_add_timer()/scsi_del_timer() instead of SCSI_TIMER
 * New error handling included
 *
 * Revision 1.10  1997/10/31 12:29:57  achim
 * Read heads/sectors from host drive
 *
 * Revision 1.9  1997/09/04 10:07:25  achim
 * IO-mapping with virt_to_bus(), gdth_readb(), gdth_writeb(), ...
Alan Cox's avatar
Alan Cox committed
225
 * register_reboot_notifier() to get a notify on shutown used
Linus Torvalds's avatar
Linus Torvalds committed
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
 *
 * Revision 1.8  1997/04/02 12:14:30  achim
 * Version 1.00 (see gdth.h), tested with kernel 2.0.29
 *
 * Revision 1.7  1997/03/12 13:33:37  achim
 * gdth_reset() changed, new async. events
 *
 * Revision 1.6  1997/03/04 14:01:11  achim
 * Shutdown routine gdth_halt() implemented
 *
 * Revision 1.5  1997/02/21 09:08:36  achim
 * New controller included (RP, RP1, RP2 series)
 * IOCTL interface implemented
 *
 * Revision 1.4  1996/07/05 12:48:55  achim
 * Function gdth_bios_param() implemented
 * New constant GDTH_MAXC_P_L inserted
 * GDT_WRITE_THR, GDT_EXT_INFO implemented
 * Function gdth_reset() changed
 *
 * Revision 1.3  1996/05/10 09:04:41  achim
 * Small changes for Linux 1.2.13
 *
 * Revision 1.2  1996/05/09 12:45:27  achim
 * Loadable module support implemented
 * /proc support corrections made
 *
 * Revision 1.1  1996/04/11 07:35:57  achim
 * Initial revision
 *
 ************************************************************************/
Alan Cox's avatar
Alan Cox committed
257
#ident "$Id: gdth.c,v 1.62 2003/02/27 15:01:59 achim Exp $" 
Linus Torvalds's avatar
Linus Torvalds committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283

/* All GDT Disk Array Controllers are fully supported by this driver.
 * This includes the PCI/EISA/ISA SCSI Disk Array Controllers and the
 * PCI Fibre Channel Disk Array Controllers. See gdth.h for a complete
 * list of all controller types.
 * 
 * If you have one or more GDT3000/3020 EISA controllers with 
 * controller BIOS disabled, you have to set the IRQ values with the 
 * command line option "gdth=irq1,irq2,...", where the irq1,irq2,... are
 * the IRQ values for the EISA controllers.
 * 
 * After the optional list of IRQ values, other possible 
 * command line options are:
 * disable:Y                    disable driver
 * disable:N                    enable driver
 * reserve_mode:0               reserve no drives for the raw service
 * reserve_mode:1               reserve all not init., removable drives
 * reserve_mode:2               reserve all not init. drives
 * reserve_list:h,b,t,l,h,b,t,l,...     reserve particular drive(s) with 
 *                              h- controller no., b- channel no., 
 *                              t- target ID, l- LUN
 * reverse_scan:Y               reverse scan order for PCI controllers         
 * reverse_scan:N               scan PCI controllers like BIOS
 * max_ids:x                    x - target ID count per channel (1..MAXID)
 * rescan:Y                     rescan all channels/IDs 
 * rescan:N                     use all devices found until now
Linus Torvalds's avatar
Linus Torvalds committed
284 285
 * virt_ctr:Y                   map every channel to a virtual controller 
 * virt_ctr:N                   use multi channel support 
Linus Torvalds's avatar
Linus Torvalds committed
286
 * hdr_channel:x                x - number of virtual bus for host drives
Linus Torvalds's avatar
Linus Torvalds committed
287 288 289 290
 * shared_access:Y              disable driver reserve/release protocol to 
 *                              access a shared resource from several nodes, 
 *                              appropiate controller firmware required
 * shared_access:N              enable driver reserve/release protocol
Linus Torvalds's avatar
Linus Torvalds committed
291
 *
Linus Torvalds's avatar
Linus Torvalds committed
292 293
 * The default values are: "gdth=disable:N,reserve_mode:1,reverse_scan:N,
 *                          max_ids:127,rescan:N,virt_ctr:N,hdr_channel:0,
294
 *                          shared_access:Y".
Linus Torvalds's avatar
Linus Torvalds committed
295 296 297 298 299 300 301 302 303
 * Here is another example: "gdth=reserve_list:0,1,2,0,0,1,3,0,rescan:Y".
 * 
 * When loading the gdth driver as a module, the same options are available. 
 * You can set the IRQs with "IRQ=...". However, the syntax to specify the
 * options changes slightly. You must replace all ',' between options 
 * with ' ' and all ':' with '=' and you must use 
 * '1' in place of 'Y' and '0' in place of 'N'.
 * 
 * Default: "modprobe gdth disable=0 reserve_mode=1 reverse_scan=0
Linus Torvalds's avatar
Linus Torvalds committed
304
 *           max_ids=127 rescan=0 virt_ctr=0 hdr_channel=0 shared_access=0"
Linus Torvalds's avatar
Linus Torvalds committed
305 306 307
 * The other example: "modprobe gdth reserve_list=0,1,2,0,0,1,3,0 rescan=1".
 */

Linus Torvalds's avatar
Linus Torvalds committed
308
/* The meaning of the Scsi_Pointer members in this driver is as follows:
Alan Cox's avatar
Alan Cox committed
309 310 311 312 313 314 315 316 317 318
 * ptr:                     Chaining
 * this_residual:           Command priority
 * buffer:                  phys. DMA sense buffer 
 * dma_handle:              phys. DMA buffer (kernel >= 2.4.0)
 * buffers_residual:        Timeout value
 * Status:                  Command status (gdth_do_cmd()), DMA mem. mappings
 * Message:                 Additional info (gdth_do_cmd()), DMA direction
 * have_data_in:            Flag for gdth_wait_completion()
 * sent_command:            Opcode special command
 * phase:                   Service/parameter/return code special command
Linus Torvalds's avatar
Linus Torvalds committed
319 320
 */

Alan Cox's avatar
Alan Cox committed
321 322 323
/* default: activate /proc and character device IOCTL interface */
#define GDTH_IOCTL_PROC
#define GDTH_IOCTL_CHRDEV
324

Linus Torvalds's avatar
Linus Torvalds committed
325 326 327 328 329 330 331 332 333 334 335
#include <linux/module.h>

#include <linux/version.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/sched.h>
Alan Cox's avatar
Alan Cox committed
336
#include <linux/interrupt.h>
Linus Torvalds's avatar
Linus Torvalds committed
337 338 339 340
#include <linux/in.h>
#include <linux/proc_fs.h>
#include <linux/time.h>
#include <linux/timer.h>
Linus Torvalds's avatar
Linus Torvalds committed
341 342 343 344
#ifdef GDTH_RTC
#include <linux/mc146818rtc.h>
#endif
#if LINUX_VERSION_CODE >= 0x020100
Linus Torvalds's avatar
Linus Torvalds committed
345
#include <linux/reboot.h>
Linus Torvalds's avatar
Linus Torvalds committed
346 347 348
#else
#include <linux/bios32.h>
#endif
Linus Torvalds's avatar
Linus Torvalds committed
349 350 351 352

#include <asm/dma.h>
#include <asm/system.h>
#include <asm/io.h>
Alan Cox's avatar
Alan Cox committed
353
#include <asm/uaccess.h>
Linus Torvalds's avatar
Linus Torvalds committed
354
#if LINUX_VERSION_CODE >= 0x020322
Linus Torvalds's avatar
Linus Torvalds committed
355
#include <linux/spinlock.h>
Linus Torvalds's avatar
Linus Torvalds committed
356 357 358 359 360
#elif LINUX_VERSION_CODE >= 0x02015F
#include <asm/spinlock.h>
#endif

#if LINUX_VERSION_CODE >= 0x010300
Linus Torvalds's avatar
Linus Torvalds committed
361
#include <linux/blk.h>
Linus Torvalds's avatar
Linus Torvalds committed
362 363 364
#else
#include "../block/blk.h"
#endif
Linus Torvalds's avatar
Linus Torvalds committed
365 366
#include "scsi.h"
#include "hosts.h"
Alan Cox's avatar
Alan Cox committed
367 368 369
#if LINUX_VERSION_CODE < 0x020503
#include "sd.h"
#endif
Linus Torvalds's avatar
Linus Torvalds committed
370 371 372 373 374

#include "gdth.h"

static void gdth_delay(int milliseconds);
static void gdth_eval_mapping(ulong32 size, int *cyls, int *heads, int *secs);
Andrew Morton's avatar
Andrew Morton committed
375
static irqreturn_t gdth_interrupt(int irq, void *dev_id, struct pt_regs *regs);
Linus Torvalds's avatar
Linus Torvalds committed
376
static int gdth_sync_event(int hanum,int service,unchar index,Scsi_Cmnd *scp);
Linus Torvalds's avatar
Linus Torvalds committed
377
static int gdth_async_event(int hanum);
Linus Torvalds's avatar
Linus Torvalds committed
378 379 380 381 382 383 384 385 386 387 388 389 390
static void gdth_log_event(gdth_evt_data *dvr, char *buffer);

static void gdth_putq(int hanum,Scsi_Cmnd *scp,unchar priority);
static void gdth_next(int hanum);
static int gdth_fill_raw_cmd(int hanum,Scsi_Cmnd *scp,unchar b);
static int gdth_special_cmd(int hanum,Scsi_Cmnd *scp);
static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, ushort source,
                                      ushort idx, gdth_evt_data *evt);
static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr);
static void gdth_readapp_event(gdth_ha_str *ha, unchar application, 
                               gdth_evt_str *estr);
static void gdth_clear_events(void);

Alan Cox's avatar
Alan Cox committed
391 392
static void gdth_copy_internal_data(int hanum,Scsi_Cmnd *scp,
                                    char *buffer,ushort count);
Linus Torvalds's avatar
Linus Torvalds committed
393 394 395 396 397 398
static int gdth_internal_cache_cmd(int hanum,Scsi_Cmnd *scp);
static int gdth_fill_cache_cmd(int hanum,Scsi_Cmnd *scp,ushort hdrive);

static int gdth_search_eisa(ushort eisa_adr);
static int gdth_search_isa(ulong32 bios_adr);
static int gdth_search_pci(gdth_pci_str *pcistr);
Linus Torvalds's avatar
Linus Torvalds committed
399 400
static void gdth_search_dev(gdth_pci_str *pcistr, ushort *cnt, 
                            ushort vendor, ushort dev);
Linus Torvalds's avatar
Linus Torvalds committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414
static void gdth_sort_pci(gdth_pci_str *pcistr, int cnt);
static int gdth_init_eisa(ushort eisa_adr,gdth_ha_str *ha);
static int gdth_init_isa(ulong32 bios_adr,gdth_ha_str *ha);
static int gdth_init_pci(gdth_pci_str *pcistr,gdth_ha_str *ha);

static void gdth_enable_int(int hanum);
static int gdth_get_status(unchar *pIStatus,int irq);
static int gdth_test_busy(int hanum);
static int gdth_get_cmd_index(int hanum);
static void gdth_release_event(int hanum);
static int gdth_wait(int hanum,int index,ulong32 time);
static int gdth_internal_cmd(int hanum,unchar service,ushort opcode,ulong32 p1,
                             ulong32 p2,ulong32 p3);
static int gdth_search_drives(int hanum);
Linus Torvalds's avatar
Linus Torvalds committed
415
static int gdth_analyse_hdrive(int hanum, ushort hdrive);
Linus Torvalds's avatar
Linus Torvalds committed
416 417 418 419 420 421

static void *gdth_mmap(ulong paddr, ulong size);
static void gdth_munmap(void *addr);

static const char *gdth_ctr_name(int hanum);

Alan Cox's avatar
Alan Cox committed
422 423 424 425 426 427 428
#ifdef GDTH_IOCTL_CHRDEV
static int gdth_open(struct inode *inode, struct file *filep);
static int gdth_close(struct inode *inode, struct file *filep);
static int gdth_ioctl(struct inode *inode, struct file *filep,
                      unsigned int cmd, unsigned long arg);
#endif

Linus Torvalds's avatar
Linus Torvalds committed
429
#if LINUX_VERSION_CODE >= 0x010300
Linus Torvalds's avatar
Linus Torvalds committed
430
static void gdth_flush(int hanum);
Linus Torvalds's avatar
Linus Torvalds committed
431
#if LINUX_VERSION_CODE >= 0x020100
Linus Torvalds's avatar
Linus Torvalds committed
432
static int gdth_halt(struct notifier_block *nb, ulong event, void *buf);
Linus Torvalds's avatar
Linus Torvalds committed
433 434 435 436 437
#else
static int halt_called = FALSE;
void gdth_halt(void);
#endif
#endif
Linus Torvalds's avatar
Linus Torvalds committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508

#ifdef DEBUG_GDTH
static unchar   DebugState = DEBUG_GDTH;

#ifdef __SERIAL__
#define MAX_SERBUF 160
static void ser_init(void);
static void ser_puts(char *str);
static void ser_putc(char c);
static int  ser_printk(const char *fmt, ...);
static char strbuf[MAX_SERBUF+1];
#ifdef __COM2__
#define COM_BASE 0x2f8
#else
#define COM_BASE 0x3f8
#endif
static void ser_init()
{
    unsigned port=COM_BASE;

    outb(0x80,port+3);
    outb(0,port+1);
    /* 19200 Baud, if 9600: outb(12,port) */
    outb(6, port);
    outb(3,port+3);
    outb(0,port+1);
    /*
    ser_putc('I');
    ser_putc(' ');
    */
}

static void ser_puts(char *str)
{
    char *ptr;

    ser_init();
    for (ptr=str;*ptr;++ptr)
        ser_putc(*ptr);
}

static void ser_putc(char c)
{
    unsigned port=COM_BASE;

    while ((inb(port+5) & 0x20)==0);
    outb(c,port);
    if (c==0x0a)
    {
        while ((inb(port+5) & 0x20)==0);
        outb(0x0d,port);
    }
}

static int ser_printk(const char *fmt, ...)
{
    va_list args;
    int i;

    va_start(args,fmt);
    i = vsprintf(strbuf,fmt,args);
    ser_puts(strbuf);
    va_end(args);
    return i;
}

#define TRACE(a)    {if (DebugState==1) {ser_printk a;}}
#define TRACE2(a)   {if (DebugState==1 || DebugState==2) {ser_printk a;}}
#define TRACE3(a)   {if (DebugState!=0) {ser_printk a;}}

#else /* !__SERIAL__ */
Linus Torvalds's avatar
Linus Torvalds committed
509 510 511
#define TRACE(a)    {if (DebugState==1) {printk a;}}
#define TRACE2(a)   {if (DebugState==1 || DebugState==2) {printk a;}}
#define TRACE3(a)   {if (DebugState!=0) {printk a;}}
Linus Torvalds's avatar
Linus Torvalds committed
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535
#endif

#else /* !DEBUG */
#define TRACE(a)
#define TRACE2(a)
#define TRACE3(a)
#endif

#ifdef GDTH_STATISTICS
static ulong32 max_rq=0, max_index=0, max_sg=0;
static ulong32 act_ints=0, act_ios=0, act_stats=0, act_rq=0;
static struct timer_list gdth_timer;
#endif

#define PTR2USHORT(a)   (ushort)(ulong)(a)
#define GDTOFFSOF(a,b)  (size_t)&(((a*)0)->b)   
#define INDEX_OK(i,t)   ((i)<sizeof(t)/sizeof((t)[0]))

#define NUMDATA(a)      ( (gdth_num_str  *)((a)->hostdata))
#define HADATA(a)       (&((gdth_ext_str *)((a)->hostdata))->haext)
#define CMDDATA(a)      (&((gdth_ext_str *)((a)->hostdata))->cmdext)

#define BUS_L2P(a,b)    ((b)>(a)->virt_bus ? (b-1):(b))

Linus Torvalds's avatar
Linus Torvalds committed
536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
#if LINUX_VERSION_CODE < 0x010300
static void *gdth_mmap(ulong paddr, ulong size) 
{
    if (paddr >= high_memory)
        return NULL; 
    else
        return (void *)paddr;
}
static void gdth_munmap(void *addr) 
{
}
inline ulong32 virt_to_phys(volatile void *addr)
{
    return (ulong32)addr;
}
inline void *phys_to_virt(ulong32 addr)
{
    return (void *)addr;
}
#define virt_to_bus             virt_to_phys
#define bus_to_virt             phys_to_virt
#define gdth_readb(addr)        (*(volatile unchar *)(addr))
#define gdth_readw(addr)        (*(volatile ushort *)(addr))
#define gdth_readl(addr)        (*(volatile ulong32 *)(addr))
#define gdth_writeb(b,addr)     (*(volatile unchar *)(addr) = (b))
#define gdth_writew(b,addr)     (*(volatile ushort *)(addr) = (b))
#define gdth_writel(b,addr)     (*(volatile ulong32 *)(addr) = (b))
#define memset_io(a,b,c)        memset((void *)(a),(b),(c))
#define memcpy_fromio(a,b,c)    memcpy((a),(void *)(b),(c))
#define memcpy_toio(a,b,c)      memcpy((void *)(a),(b),(c))

#define PCI_SLOT(devfn)         ((devfn >> 3) & 0x1f)

#elif LINUX_VERSION_CODE < 0x020100
static int remapped = FALSE;
static void *gdth_mmap(ulong paddr, ulong size) 
{
    if ( paddr >= high_memory) {
        remapped = TRUE;
        return vremap(paddr, size);
    } else {
        return (void *)paddr; 
    }
}
static void gdth_munmap(void *addr) 
{
    if (remapped)
        vfree(addr);
    remapped = FALSE;
}
#define gdth_readb(addr)        readb((ulong)(addr))
#define gdth_readw(addr)        readw((ulong)(addr))
#define gdth_readl(addr)        (ulong32)readl((ulong)(addr))
#define gdth_writeb(b,addr)     writeb((b),(ulong)(addr))
#define gdth_writew(b,addr)     writew((b),(ulong)(addr))
#define gdth_writel(b,addr)     writel((ulong32)(b),(ulong)(addr))

#else
Linus Torvalds's avatar
Linus Torvalds committed
594 595 596 597 598 599 600 601 602 603 604 605 606
static void *gdth_mmap(ulong paddr, ulong size) 
{ 
    return ioremap(paddr, size); 
}
static void gdth_munmap(void *addr) 
{
    return iounmap(addr);
}
#define gdth_readb(addr)        readb((ulong)(addr))
#define gdth_readw(addr)        readw((ulong)(addr))
#define gdth_readl(addr)        (ulong32)readl((ulong)(addr))
#define gdth_writeb(b,addr)     writeb((b),(ulong)(addr))
#define gdth_writew(b,addr)     writew((b),(ulong)(addr))
Linus Torvalds's avatar
Linus Torvalds committed
607 608 609
#define gdth_writel(b,addr)     writel((ulong32)(b),(ulong)(addr))
#endif

Linus Torvalds's avatar
Linus Torvalds committed
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624

static unchar   gdth_drq_tab[4] = {5,6,7,7};            /* DRQ table */
static unchar   gdth_irq_tab[6] = {0,10,11,12,14,0};    /* IRQ table */
static unchar   gdth_polling;                           /* polling if TRUE */
static unchar   gdth_from_wait  = FALSE;                /* gdth_wait() */
static int      wait_index,wait_hanum;                  /* gdth_wait() */
static int      gdth_ctr_count  = 0;                    /* controller count */
static int      gdth_ctr_vcount = 0;                    /* virt. ctr. count */
static int      gdth_ctr_released = 0;                  /* gdth_release() */
static struct Scsi_Host *gdth_ctr_tab[MAXHA];           /* controller table */
static struct Scsi_Host *gdth_ctr_vtab[MAXHA*MAXBUS];   /* virt. ctr. table */
static unchar   gdth_write_through = FALSE;             /* write through */
static gdth_evt_str ebuffer[MAX_EVENTS];                /* event buffer */
static int elastidx;
static int eoldidx;
Alan Cox's avatar
Alan Cox committed
625 626 627
#ifdef GDTH_IOCTL_CHRDEV
static int major;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645

#define DIN     1                               /* IN data direction */
#define DOU     2                               /* OUT data direction */
#define DNO     DIN                             /* no data transfer */
#define DUN     DIN                             /* unknown data direction */
static unchar gdth_direction_tab[0x100] = {
    DNO,DNO,DIN,DIN,DOU,DIN,DIN,DOU,DIN,DUN,DOU,DOU,DUN,DUN,DUN,DIN,
    DNO,DIN,DIN,DOU,DIN,DOU,DNO,DNO,DOU,DNO,DIN,DNO,DIN,DOU,DNO,DUN,
    DIN,DUN,DIN,DUN,DOU,DIN,DUN,DUN,DIN,DIN,DOU,DNO,DUN,DIN,DOU,DOU,
    DOU,DOU,DOU,DNO,DIN,DNO,DNO,DIN,DOU,DOU,DOU,DOU,DIN,DOU,DIN,DOU,
    DOU,DOU,DIN,DIN,DIN,DNO,DUN,DNO,DNO,DNO,DUN,DNO,DOU,DIN,DUN,DUN,
    DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DIN,DUN,DUN,DUN,DUN,DUN,
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
    DUN,DUN,DUN,DUN,DUN,DNO,DNO,DUN,DIN,DNO,DOU,DUN,DNO,DUN,DOU,DOU,
    DOU,DOU,DOU,DNO,DUN,DIN,DOU,DIN,DIN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
Linus Torvalds's avatar
Linus Torvalds committed
646
    DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
Linus Torvalds's avatar
Linus Torvalds committed
647 648 649 650 651 652
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN
};

/* __initfunc, __initdata macros */
Linus Torvalds's avatar
Linus Torvalds committed
653 654
#if LINUX_VERSION_CODE >= 0x020322
#define GDTH_INITFUNC(type, func)       type __init func 
Linus Torvalds's avatar
Linus Torvalds committed
655
#include <linux/init.h>
Linus Torvalds's avatar
Linus Torvalds committed
656 657
#elif LINUX_VERSION_CODE >= 0x020126
#define GDTH_INITFUNC(type, func)       __initfunc(type func)
Linus Torvalds's avatar
Linus Torvalds committed
658
#include <linux/init.h>
Linus Torvalds's avatar
Linus Torvalds committed
659 660 661 662 663
#else
#define GDTH_INITFUNC(type, func)       type func
#define __initdata
#define __init
#endif
Linus Torvalds's avatar
Linus Torvalds committed
664

Alan Cox's avatar
Alan Cox committed
665 666 667 668 669 670 671 672 673 674 675
#if LINUX_VERSION_CODE >= 0x020503
#define GDTH_INIT_LOCK_HA(ha)           spin_lock_init(&(ha)->smp_lock)
#define GDTH_LOCK_HA(ha,flags)          spin_lock_irqsave(&(ha)->smp_lock,flags)
#define GDTH_UNLOCK_HA(ha,flags)        spin_unlock_irqrestore(&(ha)->smp_lock,flags)

#define GDTH_LOCK_SCSI_DONE(dev, flags) spin_lock_irqsave(dev->host_lock,flags)
#define GDTH_UNLOCK_SCSI_DONE(dev, flags) spin_unlock_irqrestore(dev->host_lock,flags)
#define GDTH_LOCK_SCSI_DOCMD(dev)       spin_lock_irq(dev->host_lock)
#define GDTH_UNLOCK_SCSI_DOCMD(dev)     spin_unlock_irq(dev->host_lock)

#elif LINUX_VERSION_CODE >= 0x02015F
Linus Torvalds's avatar
Linus Torvalds committed
676 677 678 679
#define GDTH_INIT_LOCK_HA(ha)           spin_lock_init(&(ha)->smp_lock)
#define GDTH_LOCK_HA(ha,flags)          spin_lock_irqsave(&(ha)->smp_lock,flags)
#define GDTH_UNLOCK_HA(ha,flags)        spin_unlock_irqrestore(&(ha)->smp_lock,flags)

Alan Cox's avatar
Alan Cox committed
680 681 682 683 684
#define GDTH_LOCK_SCSI_DONE(flags)      spin_lock_irqsave(&io_request_lock,flags)
#define GDTH_UNLOCK_SCSI_DONE(flags)    spin_unlock_irqrestore(&io_request_lock,flags)
#define GDTH_LOCK_SCSI_DOCMD()          spin_lock_irq(&io_request_lock)
#define GDTH_UNLOCK_SCSI_DOCMD()        spin_unlock_irq(&io_request_lock)

Linus Torvalds's avatar
Linus Torvalds committed
685 686 687 688 689
#else
#define GDTH_INIT_LOCK_HA(ha)           do {} while (0)
#define GDTH_LOCK_HA(ha,flags)          do {save_flags(flags); cli();} while (0)
#define GDTH_UNLOCK_HA(ha,flags)        do {restore_flags(flags);} while (0)

Alan Cox's avatar
Alan Cox committed
690 691 692 693
#define GDTH_LOCK_SCSI_DONE(flags)      do {} while (0)
#define GDTH_UNLOCK_SCSI_DONE(flags)    do {} while (0)
#define GDTH_LOCK_SCSI_DOCMD()          do {} while (0)
#define GDTH_UNLOCK_SCSI_DOCMD()        do {} while (0)
Linus Torvalds's avatar
Linus Torvalds committed
694
#endif
Linus Torvalds's avatar
Linus Torvalds committed
695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

/* LILO and modprobe/insmod parameters */
/* IRQ list for GDT3000/3020 EISA controllers */
static int irq[MAXHA] __initdata = 
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
/* disable driver flag */
static int disable __initdata = 0;
/* reserve flag */
static int reserve_mode = 1;                  
/* reserve list */
static int reserve_list[MAX_RES_ARGS] = 
{0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
/* scan order for PCI controllers */
static int reverse_scan = 0;
/* virtual channel for the host drives */
static int hdr_channel = 0;
/* max. IDs per channel */
static int max_ids = MAXID;
/* rescan all IDs */
static int rescan = 0;
Linus Torvalds's avatar
Linus Torvalds committed
718 719 720
/* map channels to virtual controllers */
static int virt_ctr = 0;
/* shared access */
721
static int shared_access = 1;
Linus Torvalds's avatar
Linus Torvalds committed
722 723

#ifdef MODULE
Linus Torvalds's avatar
Linus Torvalds committed
724
#if LINUX_VERSION_CODE >= 0x02011A
Linus Torvalds's avatar
Linus Torvalds committed
725 726 727 728 729 730 731 732 733
/* parameters for modprobe/insmod */
MODULE_PARM(irq, "i");
MODULE_PARM(disable, "i");
MODULE_PARM(reserve_mode, "i");
MODULE_PARM(reserve_list, "4-" __MODULE_STRING(MAX_RES_ARGS) "i");
MODULE_PARM(reverse_scan, "i");
MODULE_PARM(hdr_channel, "i");
MODULE_PARM(max_ids, "i");
MODULE_PARM(rescan, "i");
Linus Torvalds's avatar
Linus Torvalds committed
734 735
MODULE_PARM(virt_ctr, "i");
MODULE_PARM(shared_access, "i");
Linus Torvalds's avatar
Linus Torvalds committed
736
MODULE_AUTHOR("Achim Leubner");
Alan Cox's avatar
Alan Cox committed
737 738
#endif
#if LINUX_VERSION_CODE >= 0x02040B
Linus Torvalds's avatar
Linus Torvalds committed
739
MODULE_LICENSE("GPL");
Linus Torvalds's avatar
Linus Torvalds committed
740
#endif
Linus Torvalds's avatar
Linus Torvalds committed
741
#endif
Linus Torvalds's avatar
Linus Torvalds committed
742

Alan Cox's avatar
Alan Cox committed
743 744 745
#ifdef GDTH_IOCTL_CHRDEV
/* ioctl interface */
static struct file_operations gdth_fops = {
746 747 748
	.ioctl		= gdth_ioctl,
	.open		= gdth_open,
	.release	= gdth_close,
Alan Cox's avatar
Alan Cox committed
749 750 751
};
#endif

Linus Torvalds's avatar
Linus Torvalds committed
752
/* /proc support */
Linus Torvalds's avatar
Linus Torvalds committed
753
#if LINUX_VERSION_CODE >= 0x010300
Linus Torvalds's avatar
Linus Torvalds committed
754
#include <linux/stat.h> 
Linus Torvalds's avatar
Linus Torvalds committed
755 756 757 758 759 760
#if LINUX_VERSION_CODE < 0x020322
struct proc_dir_entry proc_scsi_gdth = {
    PROC_SCSI_GDTH, 4, "gdth",
    S_IFDIR | S_IRUGO | S_IXUGO, 2
};
#endif
Linus Torvalds's avatar
Linus Torvalds committed
761 762
#include "gdth_proc.h"
#include "gdth_proc.c"
Linus Torvalds's avatar
Linus Torvalds committed
763
#endif
Linus Torvalds's avatar
Linus Torvalds committed
764

Linus Torvalds's avatar
Linus Torvalds committed
765
#if LINUX_VERSION_CODE >= 0x020100
Linus Torvalds's avatar
Linus Torvalds committed
766 767 768 769
/* notifier block to get a notify on system shutdown/halt/reboot */
static struct notifier_block gdth_notifier = {
    gdth_halt, NULL, 0
};
Linus Torvalds's avatar
Linus Torvalds committed
770 771
#endif

Linus Torvalds's avatar
Linus Torvalds committed
772 773 774 775 776 777

static void gdth_delay(int milliseconds)
{
    if (milliseconds == 0) {
        udelay(1);
    } else {
Linus Torvalds's avatar
Linus Torvalds committed
778
#if LINUX_VERSION_CODE >= 0x020168
Linus Torvalds's avatar
Linus Torvalds committed
779
        mdelay(milliseconds);
Linus Torvalds's avatar
Linus Torvalds committed
780 781 782 783 784
#else
        int i;
        for (i = 0; i < milliseconds; ++i) 
            udelay(1000);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
785 786 787 788 789 790 791
    }
}

static void gdth_eval_mapping(ulong32 size, int *cyls, int *heads, int *secs)
{
    *cyls = size /HEADS/SECS;
    if (*cyls <= MAXCYLS) {
Linus Torvalds's avatar
Linus Torvalds committed
792 793 794 795 796 797 798 799 800 801 802 803
        *heads = HEADS;
        *secs = SECS;
    } else {                                        /* too high for 64*32 */
        *cyls = size /MEDHEADS/MEDSECS;
        if (*cyls <= MAXCYLS) {
            *heads = MEDHEADS;
            *secs = MEDSECS;
        } else {                                    /* too high for 127*63 */
            *cyls = size /BIGHEADS/BIGSECS;
            *heads = BIGHEADS;
            *secs = BIGSECS;
        }
Linus Torvalds's avatar
Linus Torvalds committed
804 805 806 807 808
    }
}

/* controller search and initialization functions */

Linus Torvalds's avatar
Linus Torvalds committed
809
GDTH_INITFUNC(static int, gdth_search_eisa(ushort eisa_adr))
Linus Torvalds's avatar
Linus Torvalds committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
{
    ulong32 id;
    
    TRACE(("gdth_search_eisa() adr. %x\n",eisa_adr));
    id = inl(eisa_adr+ID0REG);
    if (id == GDT3A_ID || id == GDT3B_ID) {     /* GDT3000A or GDT3000B */
        if ((inb(eisa_adr+EISAREG) & 8) == 0)   
            return 0;                           /* not EISA configured */
        return 1;
    }
    if (id == GDT3_ID)                          /* GDT3000 */
        return 1;

    return 0;                                   
}


Linus Torvalds's avatar
Linus Torvalds committed
827
GDTH_INITFUNC(static int, gdth_search_isa(ulong32 bios_adr))
Linus Torvalds's avatar
Linus Torvalds committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
{
    void *addr;
    ulong32 id;

    TRACE(("gdth_search_isa() bios adr. %x\n",bios_adr));
    if ((addr = gdth_mmap(bios_adr+BIOS_ID_OFFS, sizeof(ulong32))) != NULL) {
        id = gdth_readl(addr);
        gdth_munmap(addr);
        if (id == GDT2_ID)                          /* GDT2000 */
            return 1;
    }
    return 0;
}


Linus Torvalds's avatar
Linus Torvalds committed
843
GDTH_INITFUNC(static int, gdth_search_pci(gdth_pci_str *pcistr))
Linus Torvalds's avatar
Linus Torvalds committed
844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
{
    ushort device, cnt;
    
    TRACE(("gdth_search_pci()\n"));

    cnt = 0;
    for (device = 0; device <= PCI_DEVICE_ID_VORTEX_GDT6555; ++device)
        gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX, device);
    for (device = PCI_DEVICE_ID_VORTEX_GDT6x17RP; 
         device <= PCI_DEVICE_ID_VORTEX_GDTMAXRP; ++device)
        gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX, device);
    gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_VORTEX, 
                    PCI_DEVICE_ID_VORTEX_GDTNEWRX);
    gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_INTEL,
                    PCI_DEVICE_ID_INTEL_SRC);
Alan Cox's avatar
Alan Cox committed
859 860
    gdth_search_dev(pcistr, &cnt, PCI_VENDOR_ID_INTEL,
                    PCI_DEVICE_ID_INTEL_SRC_XSCALE);
Linus Torvalds's avatar
Linus Torvalds committed
861 862 863
    return cnt;
}

Linus Torvalds's avatar
Linus Torvalds committed
864 865 866 867
#if LINUX_VERSION_CODE >= 0x20363
/* Vortex only makes RAID controllers.
 * We do not really want to specify all 550 ids here, so wildcard match.
 */
Alan Cox's avatar
Alan Cox committed
868 869 870 871 872
static struct pci_device_id gdthtable[] __devinitdata = {
    {PCI_VENDOR_ID_VORTEX,PCI_ANY_ID,PCI_ANY_ID, PCI_ANY_ID},
    {PCI_VENDOR_ID_INTEL,PCI_DEVICE_ID_INTEL_SRC,PCI_ANY_ID,PCI_ANY_ID}, 
    {PCI_VENDOR_ID_INTEL,PCI_DEVICE_ID_INTEL_SRC_XSCALE,PCI_ANY_ID,PCI_ANY_ID}, 
    {0}
Linus Torvalds's avatar
Linus Torvalds committed
873 874 875
};
MODULE_DEVICE_TABLE(pci,gdthtable);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
876 877 878

GDTH_INITFUNC(static void, gdth_search_dev(gdth_pci_str *pcistr, ushort *cnt,
                                           ushort vendor, ushort device))
Linus Torvalds's avatar
Linus Torvalds committed
879
{
Linus Torvalds's avatar
Linus Torvalds committed
880 881
    ulong base0, base1, base2;
#if LINUX_VERSION_CODE >= 0x2015C
Linus Torvalds's avatar
Linus Torvalds committed
882
    struct pci_dev *pdev;
Linus Torvalds's avatar
Linus Torvalds committed
883 884 885 886
#else
    int error;
    ushort idx;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
887
    
Linus Torvalds's avatar
Linus Torvalds committed
888 889
    TRACE(("gdth_search_dev() cnt %d vendor %x device %x\n",
          *cnt, vendor, device));
Linus Torvalds's avatar
Linus Torvalds committed
890

Linus Torvalds's avatar
Linus Torvalds committed
891
#if LINUX_VERSION_CODE >= 0x20363
Linus Torvalds's avatar
Linus Torvalds committed
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
    pdev = NULL;
    while ((pdev = pci_find_device(vendor, device, pdev)) 
           != NULL) {
        if (pci_enable_device(pdev))
            continue;
        if (*cnt >= MAXHA)
            return;
        /* GDT PCI controller found, resources are already in pdev */
        pcistr[*cnt].pdev = pdev;
        pcistr[*cnt].vendor_id = vendor;
        pcistr[*cnt].device_id = device;
        pcistr[*cnt].subdevice_id = pdev->subsystem_device;
        pcistr[*cnt].bus = pdev->bus->number;
        pcistr[*cnt].device_fn = pdev->devfn;
        pcistr[*cnt].irq = pdev->irq;
        base0 = pci_resource_flags(pdev, 0);
        base1 = pci_resource_flags(pdev, 1);
        base2 = pci_resource_flags(pdev, 2);
        if (device <= PCI_DEVICE_ID_VORTEX_GDT6000B ||   /* GDT6000/B */
            device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) {  /* MPR */
            if (!(base0 & IORESOURCE_MEM)) 
Linus Torvalds's avatar
Linus Torvalds committed
913
                continue;
Linus Torvalds's avatar
Linus Torvalds committed
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
            pcistr[*cnt].dpmem = pci_resource_start(pdev, 0);
        } else {                                  /* GDT6110, GDT6120, .. */
            if (!(base0 & IORESOURCE_MEM) ||
                !(base2 & IORESOURCE_MEM) ||
                !(base1 & IORESOURCE_IO)) 
                continue;
            pcistr[*cnt].dpmem = pci_resource_start(pdev, 2);
            pcistr[*cnt].io_mm = pci_resource_start(pdev, 0);
            pcistr[*cnt].io    = pci_resource_start(pdev, 1);
        }
        TRACE2(("Controller found at %d/%d, irq %d, dpmem 0x%lx\n",
                pcistr[*cnt].bus, PCI_SLOT(pcistr[*cnt].device_fn), 
                pcistr[*cnt].irq, pcistr[*cnt].dpmem));
        (*cnt)++;
    }       
Linus Torvalds's avatar
Linus Torvalds committed
929
#elif LINUX_VERSION_CODE >= 0x2015C
Linus Torvalds's avatar
Linus Torvalds committed
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
    pdev = NULL;
    while ((pdev = pci_find_device(vendor, device, pdev)) 
           != NULL) {
        if (*cnt >= MAXHA)
            return;
        /* GDT PCI controller found, resources are already in pdev */
        pcistr[*cnt].pdev = pdev;
        pcistr[*cnt].vendor_id = vendor;
        pcistr[*cnt].device_id = device;
        pcistr[*cnt].bus = pdev->bus->number;
        pcistr[*cnt].device_fn = pdev->devfn;
        pcibios_read_config_word(pcistr[*cnt].bus, pcistr[*cnt].device_fn,
                                 PCI_SUBSYSTEM_ID, &pcistr[*cnt].subdevice_id);
        pcistr[*cnt].irq = pdev->irq;
        base0 = pdev->base_address[0];
        base1 = pdev->base_address[1];
        base2 = pdev->base_address[2];
        if (device <= PCI_DEVICE_ID_VORTEX_GDT6000B ||   /* GDT6000/B */
            device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) {  /* MPR */
            if ((base0 & PCI_BASE_ADDRESS_SPACE) != 
                PCI_BASE_ADDRESS_SPACE_MEMORY)
                continue;
            pcistr[*cnt].dpmem = base0 & PCI_BASE_ADDRESS_MEM_MASK;
        } else {                                  /* GDT6110, GDT6120, .. */
            if ((base0 & PCI_BASE_ADDRESS_SPACE) !=
                PCI_BASE_ADDRESS_SPACE_MEMORY ||
                (base2 & PCI_BASE_ADDRESS_SPACE) !=
                PCI_BASE_ADDRESS_SPACE_MEMORY ||
                (base1 & PCI_BASE_ADDRESS_SPACE) !=
                PCI_BASE_ADDRESS_SPACE_IO)
                continue;
            pcistr[*cnt].dpmem = base2 & PCI_BASE_ADDRESS_MEM_MASK;
            pcistr[*cnt].io_mm = base0 & PCI_BASE_ADDRESS_MEM_MASK;
            pcistr[*cnt].io    = base1 & PCI_BASE_ADDRESS_IO_MASK;
        }
        TRACE2(("Controller found at %d/%d, irq %d, dpmem 0x%lx\n",
                pcistr[*cnt].bus, PCI_SLOT(pcistr[*cnt].device_fn), 
                pcistr[*cnt].irq, pcistr[*cnt].dpmem));
        (*cnt)++;
    }       
Linus Torvalds's avatar
Linus Torvalds committed
970
#else   
Linus Torvalds's avatar
Linus Torvalds committed
971 972 973 974 975 976
    idx = 0;
    while (!pcibios_find_device(vendor, device, idx++,
                                &pcistr[*cnt].bus,&pcistr[*cnt].device_fn)) {
        if (*cnt >= MAXHA)
            return;
        /* GDT PCI ctr. found, now read resources from config space */
Linus Torvalds's avatar
Linus Torvalds committed
977 978 979 980 981
#if LINUX_VERSION_CODE >= 0x010300
#define GDTH_BASEP      (int *)
#else
#define GDTH_BASEP
#endif
Linus Torvalds's avatar
Linus Torvalds committed
982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
        if ((error = pcibios_read_config_dword(pcistr[*cnt].bus,
                                               pcistr[*cnt].device_fn,
                                               PCI_BASE_ADDRESS_0,
                                               GDTH_BASEP&base0)) ||
            (error = pcibios_read_config_dword(pcistr[*cnt].bus,
                                               pcistr[*cnt].device_fn,
                                               PCI_BASE_ADDRESS_1,
                                               GDTH_BASEP&base1)) ||
            (error = pcibios_read_config_dword(pcistr[*cnt].bus,
                                               pcistr[*cnt].device_fn,
                                               PCI_BASE_ADDRESS_2,
                                               GDTH_BASEP&base2)) ||
            (error = pcibios_read_config_word(pcistr[*cnt].bus,
                                              pcistr[*cnt].device_fn,
                                              PCI_SUBSYSTEM_ID,
                                              &pcistr[*cnt].subdevice_id)) ||
            (error = pcibios_read_config_byte(pcistr[*cnt].bus,
                                              pcistr[*cnt].device_fn,
                                              PCI_INTERRUPT_LINE,
                                              &pcistr[*cnt].irq))) {
            printk("GDT-PCI: error %d reading configuration space", error);
            continue;
        }
        pcistr[*cnt].vendor_id = vendor;
        pcistr[*cnt].device_id = device;
        if (device <= PCI_DEVICE_ID_VORTEX_GDT6000B ||   /* GDT6000/B */
            device >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) {  /* MPR */
            if ((base0 & PCI_BASE_ADDRESS_SPACE) !=
                PCI_BASE_ADDRESS_SPACE_MEMORY)
Linus Torvalds's avatar
Linus Torvalds committed
1011
                continue;
Linus Torvalds's avatar
Linus Torvalds committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
            pcistr[*cnt].dpmem = base0 & PCI_BASE_ADDRESS_MEM_MASK;
        } else {                                    /* GDT6110, GDT6120, .. */
            if ((base0 & PCI_BASE_ADDRESS_SPACE) !=
                PCI_BASE_ADDRESS_SPACE_MEMORY ||
                (base2 & PCI_BASE_ADDRESS_SPACE) !=
                PCI_BASE_ADDRESS_SPACE_MEMORY ||
                (base1 & PCI_BASE_ADDRESS_SPACE) !=
                PCI_BASE_ADDRESS_SPACE_IO)
                continue;
            pcistr[*cnt].dpmem = base2 & PCI_BASE_ADDRESS_MEM_MASK;
            pcistr[*cnt].io_mm = base0 & PCI_BASE_ADDRESS_MEM_MASK;
            pcistr[*cnt].io    = base1 & PCI_BASE_ADDRESS_IO_MASK;
Linus Torvalds's avatar
Linus Torvalds committed
1024
        }
Linus Torvalds's avatar
Linus Torvalds committed
1025 1026 1027 1028 1029
        TRACE2(("Controller found at %d/%d, irq %d, dpmem 0x%lx\n",
                pcistr[*cnt].bus, PCI_SLOT(pcistr[*cnt].device_fn), 
                pcistr[*cnt].irq, pcistr[*cnt].dpmem));
        (*cnt)++;
    }
Linus Torvalds's avatar
Linus Torvalds committed
1030
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1031
}   
Linus Torvalds's avatar
Linus Torvalds committed
1032 1033


Linus Torvalds's avatar
Linus Torvalds committed
1034
GDTH_INITFUNC(static void, gdth_sort_pci(gdth_pci_str *pcistr, int cnt))
Linus Torvalds's avatar
Linus Torvalds committed
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071
{    
    gdth_pci_str temp;
    int i, changed;
    
    TRACE(("gdth_sort_pci() cnt %d\n",cnt));
    if (cnt == 0)
        return;

    do {
        changed = FALSE;
        for (i = 0; i < cnt-1; ++i) {
            if (!reverse_scan) {
                if ((pcistr[i].bus > pcistr[i+1].bus) ||
                    (pcistr[i].bus == pcistr[i+1].bus &&
                     PCI_SLOT(pcistr[i].device_fn) > 
                     PCI_SLOT(pcistr[i+1].device_fn))) {
                    temp = pcistr[i];
                    pcistr[i] = pcistr[i+1];
                    pcistr[i+1] = temp;
                    changed = TRUE;
                }
            } else {
                if ((pcistr[i].bus < pcistr[i+1].bus) ||
                    (pcistr[i].bus == pcistr[i+1].bus &&
                     PCI_SLOT(pcistr[i].device_fn) < 
                     PCI_SLOT(pcistr[i+1].device_fn))) {
                    temp = pcistr[i];
                    pcistr[i] = pcistr[i+1];
                    pcistr[i+1] = temp;
                    changed = TRUE;
                }
            }
        }
    } while (changed);
}


Linus Torvalds's avatar
Linus Torvalds committed
1072
GDTH_INITFUNC(static int, gdth_init_eisa(ushort eisa_adr,gdth_ha_str *ha))
Linus Torvalds's avatar
Linus Torvalds committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
{
    ulong32 retries,id;
    unchar prot_ver,eisacf,i,irq_found;

    TRACE(("gdth_init_eisa() adr. %x\n",eisa_adr));
    
    /* disable board interrupts, deinitialize services */
    outb(0xff,eisa_adr+EDOORREG);
    outb(0x00,eisa_adr+EDENABREG);
    outb(0x00,eisa_adr+EINTENABREG);
    
    outb(0xff,eisa_adr+LDOORREG);
    retries = INIT_RETRIES;
    gdth_delay(20);
    while (inb(eisa_adr+EDOORREG) != 0xff) {
        if (--retries == 0) {
            printk("GDT-EISA: Initialization error (DEINIT failed)\n");
            return 0;
        }
        gdth_delay(1);
        TRACE2(("wait for DEINIT: retries=%d\n",retries));
    }
    prot_ver = inb(eisa_adr+MAILBOXREG);
    outb(0xff,eisa_adr+EDOORREG);
    if (prot_ver != PROTOCOL_VERSION) {
        printk("GDT-EISA: Illegal protocol version\n");
        return 0;
    }
    ha->bmic = eisa_adr;
    ha->brd_phys = (ulong32)eisa_adr >> 12;

    outl(0,eisa_adr+MAILBOXREG);
    outl(0,eisa_adr+MAILBOXREG+4);
    outl(0,eisa_adr+MAILBOXREG+8);
    outl(0,eisa_adr+MAILBOXREG+12);

    /* detect IRQ */ 
    if ((id = inl(eisa_adr+ID0REG)) == GDT3_ID) {
Linus Torvalds's avatar
Linus Torvalds committed
1111
        ha->oem_id = OEM_ID_ICP;
Linus Torvalds's avatar
Linus Torvalds committed
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
        ha->type = GDT_EISA;
        ha->stype = id;
        outl(1,eisa_adr+MAILBOXREG+8);
        outb(0xfe,eisa_adr+LDOORREG);
        retries = INIT_RETRIES;
        gdth_delay(20);
        while (inb(eisa_adr+EDOORREG) != 0xfe) {
            if (--retries == 0) {
                printk("GDT-EISA: Initialization error (get IRQ failed)\n");
                return 0;
            }
            gdth_delay(1);
        }
        ha->irq = inb(eisa_adr+MAILBOXREG);
        outb(0xff,eisa_adr+EDOORREG);
        TRACE2(("GDT3000/3020: IRQ=%d\n",ha->irq));
        /* check the result */
        if (ha->irq == 0) {
                TRACE2(("Unknown IRQ, use IRQ table from cmd line !\n"));
                for (i = 0, irq_found = FALSE; 
                     i < MAXHA && irq[i] != 0xff; ++i) {
                if (irq[i]==10 || irq[i]==11 || irq[i]==12 || irq[i]==14) {
                    irq_found = TRUE;
                    break;
                }
                }
            if (irq_found) {
                ha->irq = irq[i];
                irq[i] = 0;
                printk("GDT-EISA: Can not detect controller IRQ,\n");
                printk("Use IRQ setting from command line (IRQ = %d)\n",
                       ha->irq);
            } else {
                printk("GDT-EISA: Initialization error (unknown IRQ), Enable\n");
                printk("the controller BIOS or use command line parameters\n");
                return 0;
            }
        }
    } else {
        eisacf = inb(eisa_adr+EISAREG) & 7;
        if (eisacf > 4)                         /* level triggered */
            eisacf -= 4;
        ha->irq = gdth_irq_tab[eisacf];
Linus Torvalds's avatar
Linus Torvalds committed
1155
        ha->oem_id = OEM_ID_ICP;
Linus Torvalds's avatar
Linus Torvalds committed
1156 1157 1158 1159 1160 1161 1162
        ha->type = GDT_EISA;
        ha->stype = id;
    }
    return 1;
}

       
Linus Torvalds's avatar
Linus Torvalds committed
1163
GDTH_INITFUNC(static int, gdth_init_isa(ulong32 bios_adr,gdth_ha_str *ha))
Linus Torvalds's avatar
Linus Torvalds committed
1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
{
    register gdt2_dpram_str *dp2_ptr;
    int i;
    unchar irq_drq,prot_ver;
    ulong32 retries;

    TRACE(("gdth_init_isa() bios adr. %x\n",bios_adr));

    ha->brd = gdth_mmap(bios_adr, sizeof(gdt2_dpram_str));
    if (ha->brd == NULL) {
        printk("GDT-ISA: Initialization error (DPMEM remap error)\n");
        return 0;
    }
    dp2_ptr = (gdt2_dpram_str *)ha->brd;
    gdth_writeb(1, &dp2_ptr->io.memlock); /* switch off write protection */
    /* reset interface area */
    memset_io((char *)&dp2_ptr->u,0,sizeof(dp2_ptr->u));
    if (gdth_readl(&dp2_ptr->u) != 0) {
        printk("GDT-PCI: Initialization error (DPMEM write error)\n");
        gdth_munmap(ha->brd);
        return 0;
    }

    /* disable board interrupts, read DRQ and IRQ */
    gdth_writeb(0xff, &dp2_ptr->io.irqdel);
    gdth_writeb(0x00, &dp2_ptr->io.irqen);
    gdth_writeb(0x00, &dp2_ptr->u.ic.S_Status);
    gdth_writeb(0x00, &dp2_ptr->u.ic.Cmd_Index);

    irq_drq = gdth_readb(&dp2_ptr->io.rq);
    for (i=0; i<3; ++i) {
        if ((irq_drq & 1)==0)
            break;
        irq_drq >>= 1;
    }
    ha->drq = gdth_drq_tab[i];

    irq_drq = gdth_readb(&dp2_ptr->io.rq) >> 3;
    for (i=1; i<5; ++i) {
        if ((irq_drq & 1)==0)
            break;
        irq_drq >>= 1;
    }
    ha->irq = gdth_irq_tab[i];

    /* deinitialize services */
    gdth_writel(bios_adr, &dp2_ptr->u.ic.S_Info[0]);
    gdth_writeb(0xff, &dp2_ptr->u.ic.S_Cmd_Indx);
    gdth_writeb(0, &dp2_ptr->io.event);
    retries = INIT_RETRIES;
    gdth_delay(20);
    while (gdth_readb(&dp2_ptr->u.ic.S_Status) != 0xff) {
        if (--retries == 0) {
            printk("GDT-ISA: Initialization error (DEINIT failed)\n");
            gdth_munmap(ha->brd);
            return 0;
        }
        gdth_delay(1);
    }
    prot_ver = (unchar)gdth_readl(&dp2_ptr->u.ic.S_Info[0]);
    gdth_writeb(0, &dp2_ptr->u.ic.Status);
    gdth_writeb(0xff, &dp2_ptr->io.irqdel);
    if (prot_ver != PROTOCOL_VERSION) {
        printk("GDT-ISA: Illegal protocol version\n");
        gdth_munmap(ha->brd);
        return 0;
    }

Linus Torvalds's avatar
Linus Torvalds committed
1232
    ha->oem_id = OEM_ID_ICP;
Linus Torvalds's avatar
Linus Torvalds committed
1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
    ha->type = GDT_ISA;
    ha->ic_all_size = sizeof(dp2_ptr->u);
    ha->stype= GDT2_ID;
    ha->brd_phys = bios_adr >> 4;

    /* special request to controller BIOS */
    gdth_writel(0x00, &dp2_ptr->u.ic.S_Info[0]);
    gdth_writel(0x00, &dp2_ptr->u.ic.S_Info[1]);
    gdth_writel(0x01, &dp2_ptr->u.ic.S_Info[2]);
    gdth_writel(0x00, &dp2_ptr->u.ic.S_Info[3]);
    gdth_writeb(0xfe, &dp2_ptr->u.ic.S_Cmd_Indx);
    gdth_writeb(0, &dp2_ptr->io.event);
    retries = INIT_RETRIES;
    gdth_delay(20);
    while (gdth_readb(&dp2_ptr->u.ic.S_Status) != 0xfe) {
        if (--retries == 0) {
            printk("GDT-ISA: Initialization error\n");
            gdth_munmap(ha->brd);
            return 0;
        }
        gdth_delay(1);
    }
    gdth_writeb(0, &dp2_ptr->u.ic.Status);
    gdth_writeb(0xff, &dp2_ptr->io.irqdel);
    return 1;
}


Linus Torvalds's avatar
Linus Torvalds committed
1261
GDTH_INITFUNC(static int, gdth_init_pci(gdth_pci_str *pcistr,gdth_ha_str *ha))
Linus Torvalds's avatar
Linus Torvalds committed
1262 1263 1264 1265 1266 1267
{
    register gdt6_dpram_str *dp6_ptr;
    register gdt6c_dpram_str *dp6c_ptr;
    register gdt6m_dpram_str *dp6m_ptr;
    ulong32 retries;
    unchar prot_ver;
Linus Torvalds's avatar
Linus Torvalds committed
1268
    ushort command;
Linus Torvalds's avatar
Linus Torvalds committed
1269
    int i, found = FALSE;
Linus Torvalds's avatar
Linus Torvalds committed
1270 1271 1272
#if LINUX_VERSION_CODE < 0x2015C
    int rom_addr;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1273 1274 1275

    TRACE(("gdth_init_pci()\n"));

Linus Torvalds's avatar
Linus Torvalds committed
1276 1277 1278 1279
    if (pcistr->vendor_id == PCI_VENDOR_ID_INTEL)
        ha->oem_id = OEM_ID_INTEL;
    else
        ha->oem_id = OEM_ID_ICP;
Linus Torvalds's avatar
Linus Torvalds committed
1280
    ha->brd_phys = (pcistr->bus << 8) | (pcistr->device_fn & 0xf8);
Linus Torvalds's avatar
Linus Torvalds committed
1281 1282 1283
    ha->stype = (ulong32)pcistr->device_id;
    ha->subdevice_id = pcistr->subdevice_id;
    ha->irq = pcistr->irq;
Alan Cox's avatar
Alan Cox committed
1284 1285 1286
#if LINUX_VERSION_CODE >= 0x20400
    ha->pdev = pcistr->pdev;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1287 1288 1289 1290 1291 1292 1293 1294
    
    if (ha->stype <= PCI_DEVICE_ID_VORTEX_GDT6000B) {  /* GDT6000/B */
        TRACE2(("init_pci() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
        ha->brd = gdth_mmap(pcistr->dpmem, sizeof(gdt6_dpram_str));
        if (ha->brd == NULL) {
            printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
            return 0;
        }
Linus Torvalds's avatar
Linus Torvalds committed
1295
        /* check and reset interface area */
Linus Torvalds's avatar
Linus Torvalds committed
1296
        dp6_ptr = (gdt6_dpram_str *)ha->brd;
Linus Torvalds's avatar
Linus Torvalds committed
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339
        gdth_writel(DPMEM_MAGIC, &dp6_ptr->u);
        if (gdth_readl(&dp6_ptr->u) != DPMEM_MAGIC) {
            printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n", 
                   pcistr->dpmem);
            found = FALSE;
            for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
                gdth_munmap(ha->brd);
                ha->brd = gdth_mmap(i, sizeof(ushort)); 
                if (ha->brd == NULL) {
                    printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
                    return 0;
                }
                if (gdth_readw(ha->brd) != 0xffff) {
                    TRACE2(("init_pci_old() address 0x%x busy\n", i));
                    continue;
                }
                gdth_munmap(ha->brd);
#if LINUX_VERSION_CODE >= 0x2015C
                pci_write_config_dword(pcistr->pdev, 
                                       PCI_BASE_ADDRESS_0, i);
#else
                pcibios_write_config_dword(pcistr->bus, pcistr->device_fn,
                                           PCI_BASE_ADDRESS_0, i);
#endif
                ha->brd = gdth_mmap(i, sizeof(gdt6_dpram_str)); 
                if (ha->brd == NULL) {
                    printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
                    return 0;
                }
                dp6_ptr = (gdt6_dpram_str *)ha->brd;
                gdth_writel(DPMEM_MAGIC, &dp6_ptr->u);
                if (gdth_readl(&dp6_ptr->u) == DPMEM_MAGIC) {
                    printk("GDT-PCI: Use free address at 0x%x\n", i);
                    found = TRUE;
                    break;
                }
            }   
            if (!found) {
                printk("GDT-PCI: No free address found!\n");
                gdth_munmap(ha->brd);
                return 0;
            }
        }
Linus Torvalds's avatar
Linus Torvalds committed
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
        memset_io((char *)&dp6_ptr->u,0,sizeof(dp6_ptr->u));
        if (gdth_readl(&dp6_ptr->u) != 0) {
            printk("GDT-PCI: Initialization error (DPMEM write error)\n");
            gdth_munmap(ha->brd);
            return 0;
        }
        
        /* disable board interrupts, deinit services */
        gdth_writeb(0xff, &dp6_ptr->io.irqdel);
        gdth_writeb(0x00, &dp6_ptr->io.irqen);;
        gdth_writeb(0x00, &dp6_ptr->u.ic.S_Status);
        gdth_writeb(0x00, &dp6_ptr->u.ic.Cmd_Index);

        gdth_writel(pcistr->dpmem, &dp6_ptr->u.ic.S_Info[0]);
        gdth_writeb(0xff, &dp6_ptr->u.ic.S_Cmd_Indx);
        gdth_writeb(0, &dp6_ptr->io.event);
        retries = INIT_RETRIES;
        gdth_delay(20);
        while (gdth_readb(&dp6_ptr->u.ic.S_Status) != 0xff) {
            if (--retries == 0) {
                printk("GDT-PCI: Initialization error (DEINIT failed)\n");
                gdth_munmap(ha->brd);
                return 0;
            }
            gdth_delay(1);
        }
        prot_ver = (unchar)gdth_readl(&dp6_ptr->u.ic.S_Info[0]);
        gdth_writeb(0, &dp6_ptr->u.ic.S_Status);
        gdth_writeb(0xff, &dp6_ptr->io.irqdel);
        if (prot_ver != PROTOCOL_VERSION) {
            printk("GDT-PCI: Illegal protocol version\n");
            gdth_munmap(ha->brd);
            return 0;
        }

        ha->type = GDT_PCI;
        ha->ic_all_size = sizeof(dp6_ptr->u);
        
        /* special command to controller BIOS */
        gdth_writel(0x00, &dp6_ptr->u.ic.S_Info[0]);
        gdth_writel(0x00, &dp6_ptr->u.ic.S_Info[1]);
        gdth_writel(0x01, &dp6_ptr->u.ic.S_Info[2]);
        gdth_writel(0x00, &dp6_ptr->u.ic.S_Info[3]);
        gdth_writeb(0xfe, &dp6_ptr->u.ic.S_Cmd_Indx);
        gdth_writeb(0, &dp6_ptr->io.event);
        retries = INIT_RETRIES;
        gdth_delay(20);
        while (gdth_readb(&dp6_ptr->u.ic.S_Status) != 0xfe) {
            if (--retries == 0) {
                printk("GDT-PCI: Initialization error\n");
                gdth_munmap(ha->brd);
                return 0;
            }
            gdth_delay(1);
        }
        gdth_writeb(0, &dp6_ptr->u.ic.S_Status);
        gdth_writeb(0xff, &dp6_ptr->io.irqdel);

    } else if (ha->stype <= PCI_DEVICE_ID_VORTEX_GDT6555) { /* GDT6110, ... */
        ha->plx = (gdt6c_plx_regs *)pcistr->io;
        TRACE2(("init_pci_new() dpmem %lx irq %d\n",
            pcistr->dpmem,ha->irq));
        ha->brd = gdth_mmap(pcistr->dpmem, sizeof(gdt6c_dpram_str));
        if (ha->brd == NULL) {
            printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
            gdth_munmap(ha->brd);
            return 0;
        }
Linus Torvalds's avatar
Linus Torvalds committed
1408
        /* check and reset interface area */
Linus Torvalds's avatar
Linus Torvalds committed
1409
        dp6c_ptr = (gdt6c_dpram_str *)ha->brd;
Linus Torvalds's avatar
Linus Torvalds committed
1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452
        gdth_writel(DPMEM_MAGIC, &dp6c_ptr->u);
        if (gdth_readl(&dp6c_ptr->u) != DPMEM_MAGIC) {
            printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n", 
                   pcistr->dpmem);
            found = FALSE;
            for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
                gdth_munmap(ha->brd);
                ha->brd = gdth_mmap(i, sizeof(ushort)); 
                if (ha->brd == NULL) {
                    printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
                    return 0;
                }
                if (gdth_readw(ha->brd) != 0xffff) {
                    TRACE2(("init_pci_plx() address 0x%x busy\n", i));
                    continue;
                }
                gdth_munmap(ha->brd);
#if LINUX_VERSION_CODE >= 0x2015C
                pci_write_config_dword(pcistr->pdev, 
                                       PCI_BASE_ADDRESS_2, i);
#else
                pcibios_write_config_dword(pcistr->bus, pcistr->device_fn,
                                           PCI_BASE_ADDRESS_2, i);
#endif
                ha->brd = gdth_mmap(i, sizeof(gdt6c_dpram_str)); 
                if (ha->brd == NULL) {
                    printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
                    return 0;
                }
                dp6c_ptr = (gdt6c_dpram_str *)ha->brd;
                gdth_writel(DPMEM_MAGIC, &dp6c_ptr->u);
                if (gdth_readl(&dp6c_ptr->u) == DPMEM_MAGIC) {
                    printk("GDT-PCI: Use free address at 0x%x\n", i);
                    found = TRUE;
                    break;
                }
            }   
            if (!found) {
                printk("GDT-PCI: No free address found!\n");
                gdth_munmap(ha->brd);
                return 0;
            }
        }
Linus Torvalds's avatar
Linus Torvalds committed
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
        memset_io((char *)&dp6c_ptr->u,0,sizeof(dp6c_ptr->u));
        if (gdth_readl(&dp6c_ptr->u) != 0) {
            printk("GDT-PCI: Initialization error (DPMEM write error)\n");
            gdth_munmap(ha->brd);
            return 0;
        }
        
        /* disable board interrupts, deinit services */
        outb(0x00,PTR2USHORT(&ha->plx->control1));
        outb(0xff,PTR2USHORT(&ha->plx->edoor_reg));
        
        gdth_writeb(0x00, &dp6c_ptr->u.ic.S_Status);
        gdth_writeb(0x00, &dp6c_ptr->u.ic.Cmd_Index);

        gdth_writel(pcistr->dpmem, &dp6c_ptr->u.ic.S_Info[0]);
        gdth_writeb(0xff, &dp6c_ptr->u.ic.S_Cmd_Indx);

        outb(1,PTR2USHORT(&ha->plx->ldoor_reg));

        retries = INIT_RETRIES;
        gdth_delay(20);
        while (gdth_readb(&dp6c_ptr->u.ic.S_Status) != 0xff) {
            if (--retries == 0) {
                printk("GDT-PCI: Initialization error (DEINIT failed)\n");
                gdth_munmap(ha->brd);
                return 0;
            }
            gdth_delay(1);
        }
        prot_ver = (unchar)gdth_readl(&dp6c_ptr->u.ic.S_Info[0]);
        gdth_writeb(0, &dp6c_ptr->u.ic.Status);
        if (prot_ver != PROTOCOL_VERSION) {
            printk("GDT-PCI: Illegal protocol version\n");
            gdth_munmap(ha->brd);
            return 0;
        }

        ha->type = GDT_PCINEW;
        ha->ic_all_size = sizeof(dp6c_ptr->u);

        /* special command to controller BIOS */
        gdth_writel(0x00, &dp6c_ptr->u.ic.S_Info[0]);
        gdth_writel(0x00, &dp6c_ptr->u.ic.S_Info[1]);
        gdth_writel(0x01, &dp6c_ptr->u.ic.S_Info[2]);
        gdth_writel(0x00, &dp6c_ptr->u.ic.S_Info[3]);
        gdth_writeb(0xfe, &dp6c_ptr->u.ic.S_Cmd_Indx);
        
        outb(1,PTR2USHORT(&ha->plx->ldoor_reg));

        retries = INIT_RETRIES;
        gdth_delay(20);
        while (gdth_readb(&dp6c_ptr->u.ic.S_Status) != 0xfe) {
            if (--retries == 0) {
                printk("GDT-PCI: Initialization error\n");
                gdth_munmap(ha->brd);
                return 0;
            }
            gdth_delay(1);
        }
        gdth_writeb(0, &dp6c_ptr->u.ic.S_Status);

    } else {                                            /* MPR */
        TRACE2(("init_pci_mpr() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
        ha->brd = gdth_mmap(pcistr->dpmem, sizeof(gdt6m_dpram_str));
        if (ha->brd == NULL) {
            printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
            return 0;
        }

Linus Torvalds's avatar
Linus Torvalds committed
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
        /* manipulate config. space to enable DPMEM, start RP controller */
#if LINUX_VERSION_CODE >= 0x20363
        pci_read_config_word(pcistr->pdev, PCI_COMMAND, &command);
        command |= 6;
        pci_write_config_word(pcistr->pdev, PCI_COMMAND, command);
        if (pci_resource_start(pcistr->pdev, 8) == 1UL)
            pci_resource_start(pcistr->pdev, 8) = 0UL;
        i = 0xFEFF0001UL;
        pci_write_config_dword(pcistr->pdev, PCI_ROM_ADDRESS, i);
        gdth_delay(1);
        pci_write_config_dword(pcistr->pdev, PCI_ROM_ADDRESS,
                               pci_resource_start(pcistr->pdev, 8));
#elif LINUX_VERSION_CODE >= 0x2015C
        pci_read_config_word(pcistr->pdev, PCI_COMMAND, &command);
        command |= 6;
        pci_write_config_word(pcistr->pdev, PCI_COMMAND, command);
        if (pcistr->pdev->rom_address == 1UL)
            pcistr->pdev->rom_address = 0UL;
        i = 0xFEFF0001UL;
        pci_write_config_dword(pcistr->pdev, PCI_ROM_ADDRESS, i);
        gdth_delay(1);
        pci_write_config_dword(pcistr->pdev, PCI_ROM_ADDRESS,
                               pcistr->pdev->rom_address);
#else
        pcibios_read_config_word(pcistr->bus, pcistr->device_fn,
                                 PCI_COMMAND, &command);
        command |= 6;
        pcibios_write_config_word(pcistr->bus, pcistr->device_fn, 
                                  PCI_COMMAND, command);
        pcibios_read_config_dword(pcistr->bus, pcistr->device_fn,
                                  PCI_ROM_ADDRESS, &rom_addr);
        if (rom_addr == 1UL)
            rom_addr = 0UL;
        i = 0xFEFF0001UL;
        pcibios_write_config_dword(pcistr->bus, pcistr->device_fn,
                                   PCI_ROM_ADDRESS, i);
        gdth_delay(1);
        pcibios_write_config_dword(pcistr->bus, pcistr->device_fn,
                                   PCI_ROM_ADDRESS, rom_addr);
#endif
        
Alan Cox's avatar
Alan Cox committed
1563 1564 1565 1566 1567
        /* Ensure that it is safe to access the non HW portions of DPMEM.
         * Aditional check needed for Xscale based RAID controllers */
        while( ((int)gdth_readb(&((gdt6m_dpram_str *)ha->brd)->i960r.sema0_reg) ) & 3 )
            gdth_delay(1);
        
Linus Torvalds's avatar
Linus Torvalds committed
1568 1569 1570 1571
        /* check and reset interface area */
        dp6m_ptr = (gdt6m_dpram_str *)ha->brd;
        gdth_writel(DPMEM_MAGIC, &dp6m_ptr->u);
        if (gdth_readl(&dp6m_ptr->u) != DPMEM_MAGIC) {
Linus Torvalds's avatar
Linus Torvalds committed
1572 1573
            printk("GDT-PCI: Cannot access DPMEM at 0x%lx (shadowed?)\n", 
                   pcistr->dpmem);
Linus Torvalds's avatar
Linus Torvalds committed
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
            found = FALSE;
            for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
                gdth_munmap(ha->brd);
                ha->brd = gdth_mmap(i, sizeof(ushort)); 
                if (ha->brd == NULL) {
                    printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
                    return 0;
                }
                if (gdth_readw(ha->brd) != 0xffff) {
                    TRACE2(("init_pci_mpr() address 0x%x busy\n", i));
                    continue;
                }
                gdth_munmap(ha->brd);
Linus Torvalds's avatar
Linus Torvalds committed
1587
#if LINUX_VERSION_CODE >= 0x2015C
Linus Torvalds's avatar
Linus Torvalds committed
1588 1589
                pci_write_config_dword(pcistr->pdev, 
                                       PCI_BASE_ADDRESS_0, i);
Linus Torvalds's avatar
Linus Torvalds committed
1590 1591 1592 1593
#else
                pcibios_write_config_dword(pcistr->bus, pcistr->device_fn,
                                           PCI_BASE_ADDRESS_0, i);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
                ha->brd = gdth_mmap(i, sizeof(gdt6m_dpram_str)); 
                if (ha->brd == NULL) {
                    printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
                    return 0;
                }
                dp6m_ptr = (gdt6m_dpram_str *)ha->brd;
                gdth_writel(DPMEM_MAGIC, &dp6m_ptr->u);
                if (gdth_readl(&dp6m_ptr->u) == DPMEM_MAGIC) {
                    printk("GDT-PCI: Use free address at 0x%x\n", i);
                    found = TRUE;
                    break;
                }
            }   
            if (!found) {
                printk("GDT-PCI: No free address found!\n");
                gdth_munmap(ha->brd);
                return 0;
            }
        }
        memset_io((char *)&dp6m_ptr->u,0,sizeof(dp6m_ptr->u));
        
        /* disable board interrupts, deinit services */
        gdth_writeb(gdth_readb(&dp6m_ptr->i960r.edoor_en_reg) | 4,
                    &dp6m_ptr->i960r.edoor_en_reg);
        gdth_writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
        gdth_writeb(0x00, &dp6m_ptr->u.ic.S_Status);
        gdth_writeb(0x00, &dp6m_ptr->u.ic.Cmd_Index);

        gdth_writel(pcistr->dpmem, &dp6m_ptr->u.ic.S_Info[0]);
        gdth_writeb(0xff, &dp6m_ptr->u.ic.S_Cmd_Indx);
        gdth_writeb(1, &dp6m_ptr->i960r.ldoor_reg);
        retries = INIT_RETRIES;
        gdth_delay(20);
        while (gdth_readb(&dp6m_ptr->u.ic.S_Status) != 0xff) {
            if (--retries == 0) {
                printk("GDT-PCI: Initialization error (DEINIT failed)\n");
                gdth_munmap(ha->brd);
                return 0;
            }
            gdth_delay(1);
        }
        prot_ver = (unchar)gdth_readl(&dp6m_ptr->u.ic.S_Info[0]);
        gdth_writeb(0, &dp6m_ptr->u.ic.S_Status);
        if (prot_ver != PROTOCOL_VERSION) {
            printk("GDT-PCI: Illegal protocol version\n");
            gdth_munmap(ha->brd);
            return 0;
        }

        ha->type = GDT_PCIMPR;
        ha->ic_all_size = sizeof(dp6m_ptr->u);
        
        /* special command to controller BIOS */
        gdth_writel(0x00, &dp6m_ptr->u.ic.S_Info[0]);
        gdth_writel(0x00, &dp6m_ptr->u.ic.S_Info[1]);
        gdth_writel(0x01, &dp6m_ptr->u.ic.S_Info[2]);
        gdth_writel(0x00, &dp6m_ptr->u.ic.S_Info[3]);
        gdth_writeb(0xfe, &dp6m_ptr->u.ic.S_Cmd_Indx);
        gdth_writeb(1, &dp6m_ptr->i960r.ldoor_reg);
        retries = INIT_RETRIES;
        gdth_delay(20);
        while (gdth_readb(&dp6m_ptr->u.ic.S_Status) != 0xfe) {
            if (--retries == 0) {
                printk("GDT-PCI: Initialization error\n");
                gdth_munmap(ha->brd);
                return 0;
            }
            gdth_delay(1);
        }
        gdth_writeb(0, &dp6m_ptr->u.ic.S_Status);
    }

    return 1;
}


/* controller protocol functions */

Linus Torvalds's avatar
Linus Torvalds committed
1672
GDTH_INITFUNC(static void, gdth_enable_int(int hanum))
Linus Torvalds's avatar
Linus Torvalds committed
1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
{
    gdth_ha_str *ha;
    ulong flags;
    gdt2_dpram_str *dp2_ptr;
    gdt6_dpram_str *dp6_ptr;
    gdt6m_dpram_str *dp6m_ptr;

    TRACE(("gdth_enable_int() hanum %d\n",hanum));
    ha = HADATA(gdth_ctr_tab[hanum]);
    GDTH_LOCK_HA(ha, flags);

    if (ha->type == GDT_EISA) {
        outb(0xff, ha->bmic + EDOORREG);
        outb(0xff, ha->bmic + EDENABREG);
        outb(0x01, ha->bmic + EINTENABREG);
    } else if (ha->type == GDT_ISA) {
        dp2_ptr = (gdt2_dpram_str *)ha->brd;
        gdth_writeb(1, &dp2_ptr->io.irqdel);
        gdth_writeb(0, &dp2_ptr->u.ic.Cmd_Index);
        gdth_writeb(1, &dp2_ptr->io.irqen);
    } else if (ha->type == GDT_PCI) {
        dp6_ptr = (gdt6_dpram_str *)ha->brd;
        gdth_writeb(1, &dp6_ptr->io.irqdel);
        gdth_writeb(0, &dp6_ptr->u.ic.Cmd_Index);
        gdth_writeb(1, &dp6_ptr->io.irqen);
    } else if (ha->type == GDT_PCINEW) {
        outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
        outb(0x03, PTR2USHORT(&ha->plx->control1));
    } else if (ha->type == GDT_PCIMPR) {
        dp6m_ptr = (gdt6m_dpram_str *)ha->brd;
        gdth_writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
        gdth_writeb(gdth_readb(&dp6m_ptr->i960r.edoor_en_reg) & ~4,
                    &dp6m_ptr->i960r.edoor_en_reg);
    }
    GDTH_UNLOCK_HA(ha, flags);
}


static int gdth_get_status(unchar *pIStatus,int irq)
{
    register gdth_ha_str *ha;
    int i;

    TRACE(("gdth_get_status() irq %d ctr_count %d\n",
           irq,gdth_ctr_count));
    
    *pIStatus = 0;
    for (i=0; i<gdth_ctr_count; ++i) {
        ha = HADATA(gdth_ctr_tab[i]);
        if (ha->irq != (unchar)irq)             /* check IRQ */
            continue;
        if (ha->type == GDT_EISA)
            *pIStatus = inb((ushort)ha->bmic + EDOORREG);
        else if (ha->type == GDT_ISA)
            *pIStatus =
                gdth_readb(&((gdt2_dpram_str *)ha->brd)->u.ic.Cmd_Index);
        else if (ha->type == GDT_PCI)
            *pIStatus =
                gdth_readb(&((gdt6_dpram_str *)ha->brd)->u.ic.Cmd_Index);
        else if (ha->type == GDT_PCINEW) 
            *pIStatus = inb(PTR2USHORT(&ha->plx->edoor_reg));
        else if (ha->type == GDT_PCIMPR)
            *pIStatus =
                gdth_readb(&((gdt6m_dpram_str *)ha->brd)->i960r.edoor_reg);
   
        if (*pIStatus)                                  
            return i;                           /* board found */
    }
    return -1;
}
                 
    
static int gdth_test_busy(int hanum)
{
    register gdth_ha_str *ha;
    register int gdtsema0 = 0;

    TRACE(("gdth_test_busy() hanum %d\n",hanum));
    
    ha = HADATA(gdth_ctr_tab[hanum]);
    if (ha->type == GDT_EISA)
        gdtsema0 = (int)inb(ha->bmic + SEMA0REG);
    else if (ha->type == GDT_ISA)
        gdtsema0 = (int)gdth_readb(&((gdt2_dpram_str *)ha->brd)->u.ic.Sema0);
    else if (ha->type == GDT_PCI)
        gdtsema0 = (int)gdth_readb(&((gdt6_dpram_str *)ha->brd)->u.ic.Sema0);
    else if (ha->type == GDT_PCINEW) 
        gdtsema0 = (int)inb(PTR2USHORT(&ha->plx->sema0_reg));
    else if (ha->type == GDT_PCIMPR)
        gdtsema0 = 
            (int)gdth_readb(&((gdt6m_dpram_str *)ha->brd)->i960r.sema0_reg);

    return (gdtsema0 & 1);
}


static int gdth_get_cmd_index(int hanum)
{
    register gdth_ha_str *ha;
    int i;

    TRACE(("gdth_get_cmd_index() hanum %d\n",hanum));

    ha = HADATA(gdth_ctr_tab[hanum]);
    for (i=0; i<GDTH_MAXCMDS; ++i) {
        if (ha->cmd_tab[i].cmnd == UNUSED_CMND) {
            ha->cmd_tab[i].cmnd = ha->pccb->RequestBuffer;
            ha->cmd_tab[i].service = ha->pccb->Service;
            ha->pccb->CommandIndex = (ulong32)i+2;
            return (i+2);
        }
    }
    return 0;
}


static void gdth_set_sema0(int hanum)
{
    register gdth_ha_str *ha;

    TRACE(("gdth_set_sema0() hanum %d\n",hanum));

    ha = HADATA(gdth_ctr_tab[hanum]);
    if (ha->type == GDT_EISA) {
        outb(1, ha->bmic + SEMA0REG);
    } else if (ha->type == GDT_ISA) {
        gdth_writeb(1, &((gdt2_dpram_str *)ha->brd)->u.ic.Sema0);
    } else if (ha->type == GDT_PCI) {
        gdth_writeb(1, &((gdt6_dpram_str *)ha->brd)->u.ic.Sema0);
    } else if (ha->type == GDT_PCINEW) { 
        outb(1, PTR2USHORT(&ha->plx->sema0_reg));
    } else if (ha->type == GDT_PCIMPR) {
        gdth_writeb(1, &((gdt6m_dpram_str *)ha->brd)->i960r.sema0_reg);
    }
}


static void gdth_copy_command(int hanum)
{
    register gdth_ha_str *ha;
    register gdth_cmd_str *cmd_ptr;
    register gdt6m_dpram_str *dp6m_ptr;
    register gdt6c_dpram_str *dp6c_ptr;
    gdt6_dpram_str *dp6_ptr;
    gdt2_dpram_str *dp2_ptr;
    ushort cp_count,dp_offset,cmd_no;
    
    TRACE(("gdth_copy_command() hanum %d\n",hanum));

    ha = HADATA(gdth_ctr_tab[hanum]);
    cp_count = ha->cmd_len;
    dp_offset= ha->cmd_offs_dpmem;
    cmd_no   = ha->cmd_cnt;
    cmd_ptr  = ha->pccb;

    ++ha->cmd_cnt;                                                      
    if (ha->type == GDT_EISA)
        return;                                 /* no DPMEM, no copy */

    /* set cpcount dword aligned */
    if (cp_count & 3)
        cp_count += (4 - (cp_count & 3));

    ha->cmd_offs_dpmem += cp_count;
    
    /* set offset and service, copy command to DPMEM */
    if (ha->type == GDT_ISA) {
        dp2_ptr = (gdt2_dpram_str *)ha->brd;
        gdth_writew(dp_offset + DPMEM_COMMAND_OFFSET, 
                    &dp2_ptr->u.ic.comm_queue[cmd_no].offset);
        gdth_writew((ushort)cmd_ptr->Service, 
                    &dp2_ptr->u.ic.comm_queue[cmd_no].serv_id);
        memcpy_toio(&dp2_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
    } else if (ha->type == GDT_PCI) {
        dp6_ptr = (gdt6_dpram_str *)ha->brd;
        gdth_writew(dp_offset + DPMEM_COMMAND_OFFSET, 
                    &dp6_ptr->u.ic.comm_queue[cmd_no].offset);
        gdth_writew((ushort)cmd_ptr->Service, 
                    &dp6_ptr->u.ic.comm_queue[cmd_no].serv_id);
        memcpy_toio(&dp6_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
    } else if (ha->type == GDT_PCINEW) {
        dp6c_ptr = (gdt6c_dpram_str *)ha->brd;
        gdth_writew(dp_offset + DPMEM_COMMAND_OFFSET, 
                    &dp6c_ptr->u.ic.comm_queue[cmd_no].offset);
        gdth_writew((ushort)cmd_ptr->Service, 
                    &dp6c_ptr->u.ic.comm_queue[cmd_no].serv_id);
        memcpy_toio(&dp6c_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
    } else if (ha->type == GDT_PCIMPR) {
        dp6m_ptr = (gdt6m_dpram_str *)ha->brd;
        gdth_writew(dp_offset + DPMEM_COMMAND_OFFSET, 
                    &dp6m_ptr->u.ic.comm_queue[cmd_no].offset);
        gdth_writew((ushort)cmd_ptr->Service, 
                    &dp6m_ptr->u.ic.comm_queue[cmd_no].serv_id);
        memcpy_toio(&dp6m_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
    }
}


static void gdth_release_event(int hanum)
{
    register gdth_ha_str *ha;

    TRACE(("gdth_release_event() hanum %d\n",hanum));
    ha = HADATA(gdth_ctr_tab[hanum]);

#ifdef GDTH_STATISTICS
    {
        ulong32 i,j;
        for (i=0,j=0; j<GDTH_MAXCMDS; ++j) {
            if (ha->cmd_tab[j].cmnd != UNUSED_CMND)
                ++i;
        }
        if (max_index < i) {
            max_index = i;
            TRACE3(("GDT: max_index = %d\n",(ushort)i));
        }
    }
#endif

    if (ha->pccb->OpCode == GDT_INIT)
        ha->pccb->Service |= 0x80;

    if (ha->type == GDT_EISA) {
        if (ha->pccb->OpCode == GDT_INIT)               /* store DMA buffer */
Alan Cox's avatar
Alan Cox committed
1897
            outl(ha->ccb_phys, ha->bmic + MAILBOXREG);
Linus Torvalds's avatar
Linus Torvalds committed
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968
        outb(ha->pccb->Service, ha->bmic + LDOORREG);
    } else if (ha->type == GDT_ISA) {
        gdth_writeb(0, &((gdt2_dpram_str *)ha->brd)->io.event);
    } else if (ha->type == GDT_PCI) {
        gdth_writeb(0, &((gdt6_dpram_str *)ha->brd)->io.event);
    } else if (ha->type == GDT_PCINEW) { 
        outb(1, PTR2USHORT(&ha->plx->ldoor_reg));
    } else if (ha->type == GDT_PCIMPR) {
        gdth_writeb(1, &((gdt6m_dpram_str *)ha->brd)->i960r.ldoor_reg);
    }
}

    
static int gdth_wait(int hanum,int index,ulong32 time)
{
    gdth_ha_str *ha;
    int answer_found = FALSE;

    TRACE(("gdth_wait() hanum %d index %d time %d\n",hanum,index,time));

    ha = HADATA(gdth_ctr_tab[hanum]);
    if (index == 0)
        return 1;                               /* no wait required */

    gdth_from_wait = TRUE;
    do {
        gdth_interrupt((int)ha->irq,ha,NULL);
        if (wait_hanum==hanum && wait_index==index) {
            answer_found = TRUE;
            break;
        }
        gdth_delay(1);
    } while (--time);
    gdth_from_wait = FALSE;
    
    while (gdth_test_busy(hanum))
        gdth_delay(0);

    return (answer_found);
}


static int gdth_internal_cmd(int hanum,unchar service,ushort opcode,ulong32 p1,
                             ulong32 p2,ulong32 p3)
{
    register gdth_ha_str *ha;
    register gdth_cmd_str *cmd_ptr;
    int retries,index;

    TRACE2(("gdth_internal_cmd() service %d opcode %d\n",service,opcode));

    ha = HADATA(gdth_ctr_tab[hanum]);
    cmd_ptr = ha->pccb;
    memset((char*)cmd_ptr,0,sizeof(gdth_cmd_str));

    /* make command  */
    for (retries = INIT_RETRIES;;) {
        cmd_ptr->Service          = service;
        cmd_ptr->RequestBuffer    = INTERNAL_CMND;
        if (!(index=gdth_get_cmd_index(hanum))) {
            TRACE(("GDT: No free command index found\n"));
            return 0;
        }
        gdth_set_sema0(hanum);
        cmd_ptr->OpCode           = opcode;
        cmd_ptr->BoardNode        = LOCALBOARD;
        if (service == CACHESERVICE) {
            if (opcode == GDT_IOCTL) {
                cmd_ptr->u.ioctl.subfunc = p1;
                cmd_ptr->u.ioctl.channel = p2;
                cmd_ptr->u.ioctl.param_size = (ushort)p3;
Alan Cox's avatar
Alan Cox committed
1969
                cmd_ptr->u.ioctl.p_param = ha->scratch_phys;
Linus Torvalds's avatar
Linus Torvalds committed
1970 1971 1972 1973 1974 1975 1976 1977 1978
            } else {
                cmd_ptr->u.cache.DeviceNo = (ushort)p1;
                cmd_ptr->u.cache.BlockNo  = p2;
            }
        } else if (service == SCSIRAWSERVICE) {
            cmd_ptr->u.raw.direction  = p1;
            cmd_ptr->u.raw.bus        = (unchar)p2;
            cmd_ptr->u.raw.target     = (unchar)p3;
            cmd_ptr->u.raw.lun        = (unchar)(p3 >> 8);
Linus Torvalds's avatar
Linus Torvalds committed
1979 1980 1981 1982 1983 1984
        } else if (service == SCREENSERVICE) {
            if (opcode == GDT_REALTIME) {
                *(ulong32 *)&cmd_ptr->u.screen.su.data[0] = p1;
                *(ulong32 *)&cmd_ptr->u.screen.su.data[4] = p2;
                *(ulong32 *)&cmd_ptr->u.screen.su.data[8] = p3;
            }
Linus Torvalds's avatar
Linus Torvalds committed
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006
        }
        ha->cmd_len          = sizeof(gdth_cmd_str);
        ha->cmd_offs_dpmem   = 0;
        ha->cmd_cnt          = 0;
        gdth_copy_command(hanum);
        gdth_release_event(hanum);
        gdth_delay(20);
        if (!gdth_wait(hanum,index,INIT_TIMEOUT)) {
            printk("GDT: Initialization error (timeout service %d)\n",service);
            return 0;
        }
        if (ha->status != S_BSY || --retries == 0)
            break;
        gdth_delay(1);   
    }   
    
    return (ha->status != S_OK ? 0:1);
}
    

/* search for devices */

Linus Torvalds's avatar
Linus Torvalds committed
2007
GDTH_INITFUNC(static int, gdth_search_drives(int hanum))
Linus Torvalds's avatar
Linus Torvalds committed
2008 2009 2010
{
    register gdth_ha_str *ha;
    ushort cdev_cnt, i;
Linus Torvalds's avatar
Linus Torvalds committed
2011
    ulong32 bus_no, drv_cnt, drv_no, j;
Linus Torvalds's avatar
Linus Torvalds committed
2012 2013 2014 2015
    gdth_getch_str *chn;
    gdth_drlist_str *drl;
    gdth_iochan_str *ioc;
    gdth_raw_iochan_str *iocr;
Linus Torvalds's avatar
Linus Torvalds committed
2016 2017
    gdth_arcdl_str *alst;
    gdth_alist_str *alst2;
Alan Cox's avatar
Alan Cox committed
2018 2019
    gdth_oem_str_ioctl *oemstr;

Linus Torvalds's avatar
Linus Torvalds committed
2020 2021 2022 2023 2024
#ifdef GDTH_RTC
    unchar rtc[12];
    ulong flags;
#endif     
   
Linus Torvalds's avatar
Linus Torvalds committed
2025 2026 2027 2028 2029 2030 2031 2032 2033 2034
    TRACE(("gdth_search_drives() hanum %d\n",hanum));
    ha = HADATA(gdth_ctr_tab[hanum]);

    /* initialize controller services, at first: screen service */
    if (!gdth_internal_cmd(hanum,SCREENSERVICE,GDT_INIT,0,0,0)) {
        printk("GDT: Initialization error screen service (code %d)\n",
               ha->status);
        return 0;
    }
    TRACE2(("gdth_search_drives(): SCREENSERVICE initialized\n"));
Linus Torvalds's avatar
Linus Torvalds committed
2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056

#ifdef GDTH_RTC
    /* read realtime clock info, send to controller */
    /* 1. wait for the falling edge of update flag */
    spin_lock_irqsave(&rtc_lock, flags);
    for (j = 0; j < 1000000; ++j)
        if (CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP)
            break;
    for (j = 0; j < 1000000; ++j)
        if (!(CMOS_READ(RTC_FREQ_SELECT) & RTC_UIP))
            break;
    /* 2. read info */
    do {
        for (j = 0; j < 12; ++j) 
            rtc[j] = CMOS_READ(j);
    } while (rtc[0] != CMOS_READ(0));
    spin_lock_irqrestore(&rtc_lock, flags);
    TRACE2(("gdth_search_drives(): RTC: %x/%x/%x\n",*(ulong32 *)&rtc[0],
            *(ulong32 *)&rtc[4], *(ulong32 *)&rtc[8]));
    /* 3. send to controller firmware */
    gdth_internal_cmd(hanum,SCREENSERVICE,GDT_REALTIME, *(ulong32 *)&rtc[0],
                      *(ulong32 *)&rtc[4], *(ulong32 *)&rtc[8]);
Linus Torvalds's avatar
Linus Torvalds committed
2057 2058 2059 2060
#endif  
 
    /* unfreeze all IOs */
    gdth_internal_cmd(hanum,CACHESERVICE,GDT_UNFREEZE_IO,0,0,0);
Linus Torvalds's avatar
Linus Torvalds committed
2061
 
Linus Torvalds's avatar
Linus Torvalds committed
2062 2063 2064 2065 2066 2067 2068 2069
    /* initialize cache service */
    if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_INIT,LINUX_OS,0,0)) {
        printk("GDT: Initialization error cache service (code %d)\n",
               ha->status);
        return 0;
    }
    TRACE2(("gdth_search_drives(): CACHESERVICE initialized\n"));
    cdev_cnt = (ushort)ha->info;
Linus Torvalds's avatar
Linus Torvalds committed
2070
    ha->fw_vers = ha->service;
Linus Torvalds's avatar
Linus Torvalds committed
2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129

    /* detect number of buses - try new IOCTL */
    iocr = (gdth_raw_iochan_str *)ha->pscratch;
    iocr->hdr.version        = 0xffffffff;
    iocr->hdr.list_entries   = MAXBUS;
    iocr->hdr.first_chan     = 0;
    iocr->hdr.last_chan      = MAXBUS-1;
    iocr->hdr.list_offset    = GDTOFFSOF(gdth_raw_iochan_str, list[0]);
    if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,IOCHAN_RAW_DESC,
                          INVALID_CHANNEL,sizeof(gdth_raw_iochan_str))) {
        TRACE2(("IOCHAN_RAW_DESC supported!\n"));
        ha->bus_cnt = iocr->hdr.chan_count;
        for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
            if (iocr->list[bus_no].proc_id < MAXID)
                ha->bus_id[bus_no] = iocr->list[bus_no].proc_id;
            else
                ha->bus_id[bus_no] = 0xff;
        }
    } else {
        /* old method */
        chn = (gdth_getch_str *)ha->pscratch;
        for (bus_no = 0; bus_no < MAXBUS; ++bus_no) {
            chn->channel_no = bus_no;
            if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
                                   SCSI_CHAN_CNT | L_CTRL_PATTERN,
                                   IO_CHANNEL | INVALID_CHANNEL,
                                   sizeof(gdth_getch_str))) {
                if (bus_no == 0) {
                    printk("GDT: Error detecting channel count (0x%x)\n",
                           ha->status);
                    return 0;
                }
                break;
            }
            if (chn->siop_id < MAXID)
                ha->bus_id[bus_no] = chn->siop_id;
            else
                ha->bus_id[bus_no] = 0xff;
        }       
        ha->bus_cnt = (unchar)bus_no;
    }
    TRACE2(("gdth_search_drives() %d channels\n",ha->bus_cnt));

    /* read cache configuration */
    if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,CACHE_INFO,
                           INVALID_CHANNEL,sizeof(gdth_cinfo_str))) {
        printk("GDT: Initialization error cache service (code %d)\n",
               ha->status);
        return 0;
    }
    ha->cpar = ((gdth_cinfo_str *)ha->pscratch)->cpar;
    TRACE2(("gdth_search_drives() cinfo: vs %x sta %d str %d dw %d b %d\n",
            ha->cpar.version,ha->cpar.state,ha->cpar.strategy,
            ha->cpar.write_back,ha->cpar.block_size));

    /* read board info and features */
    ha->more_proc = FALSE;
    if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,BOARD_INFO,
                          INVALID_CHANNEL,sizeof(gdth_binfo_str))) {
Linus Torvalds's avatar
Linus Torvalds committed
2130 2131
        memcpy(&ha->binfo, (gdth_binfo_str *)ha->pscratch,
               sizeof(gdth_binfo_str));
Linus Torvalds's avatar
Linus Torvalds committed
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
        if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,BOARD_FEATURES,
                              INVALID_CHANNEL,sizeof(gdth_bfeat_str))) {
            TRACE2(("BOARD_INFO/BOARD_FEATURES supported\n"));
            ha->bfeat = *(gdth_bfeat_str *)ha->pscratch;
            ha->more_proc = TRUE;
        }
    } else {
        TRACE2(("BOARD_INFO requires firmware >= 1.10/2.08\n"));
        strcpy(ha->binfo.type_string, gdth_ctr_name(hanum));
    }
    TRACE2(("Controller name: %s\n",ha->binfo.type_string));

    /* read more informations */
    if (ha->more_proc) {
        /* physical drives, channel addresses */
        ioc = (gdth_iochan_str *)ha->pscratch;
        ioc->hdr.version        = 0xffffffff;
        ioc->hdr.list_entries   = MAXBUS;
        ioc->hdr.first_chan     = 0;
        ioc->hdr.last_chan      = MAXBUS-1;
        ioc->hdr.list_offset    = GDTOFFSOF(gdth_iochan_str, list[0]);
        if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,IOCHAN_DESC,
                              INVALID_CHANNEL,sizeof(gdth_iochan_str))) {
            for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
                ha->raw[bus_no].address = ioc->list[bus_no].address;
                ha->raw[bus_no].local_no = ioc->list[bus_no].local_no;
            }
        } else {
            for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
                ha->raw[bus_no].address = IO_CHANNEL;
                ha->raw[bus_no].local_no = bus_no;
            }
        }
        for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no) {
            chn = (gdth_getch_str *)ha->pscratch;
            chn->channel_no = ha->raw[bus_no].local_no;
            if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
                                  SCSI_CHAN_CNT | L_CTRL_PATTERN,
                                  ha->raw[bus_no].address | INVALID_CHANNEL,
                                  sizeof(gdth_getch_str))) {
                ha->raw[bus_no].pdev_cnt = chn->drive_cnt;
                TRACE2(("Channel %d: %d phys. drives\n",
                        bus_no,chn->drive_cnt));
            }
            if (ha->raw[bus_no].pdev_cnt > 0) {
                drl = (gdth_drlist_str *)ha->pscratch;
                drl->sc_no = ha->raw[bus_no].local_no;
                drl->sc_cnt = ha->raw[bus_no].pdev_cnt;
                if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
                                      SCSI_DR_LIST | L_CTRL_PATTERN,
                                      ha->raw[bus_no].address | INVALID_CHANNEL,
                                      sizeof(gdth_drlist_str))) {
                    for (j = 0; j < ha->raw[bus_no].pdev_cnt; ++j) 
                        ha->raw[bus_no].id_list[j] = drl->sc_list[j];
                } else {
                    ha->raw[bus_no].pdev_cnt = 0;
                }
            }
        }

        /* logical drives */
        if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,CACHE_DRV_CNT,
                              INVALID_CHANNEL,sizeof(ulong32))) {
            drv_cnt = *(ulong32 *)ha->pscratch;
            if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,CACHE_DRV_LIST,
                                  INVALID_CHANNEL,drv_cnt * sizeof(ulong32))) {
                for (j = 0; j < drv_cnt; ++j) {
                    drv_no = ((ulong32 *)ha->pscratch)[j];
Linus Torvalds's avatar
Linus Torvalds committed
2200
                    if (drv_no < MAX_LDRIVES) {
Linus Torvalds's avatar
Linus Torvalds committed
2201 2202 2203 2204 2205
                        ha->hdr[drv_no].is_logdrv = TRUE;
                        TRACE2(("Drive %d is log. drive\n",drv_no));
                    }
                }
            }
Linus Torvalds's avatar
Linus Torvalds committed
2206 2207 2208 2209
            alst = (gdth_arcdl_str *)ha->pscratch;
            alst->entries_avail = MAX_LDRIVES;
            alst->first_entry = 0;
            alst->list_offset = GDTOFFSOF(gdth_arcdl_str, list[0]);
Linus Torvalds's avatar
Linus Torvalds committed
2210
            if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
Linus Torvalds's avatar
Linus Torvalds committed
2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223
                                  ARRAY_DRV_LIST2 | LA_CTRL_PATTERN, 
                                  INVALID_CHANNEL, sizeof(gdth_arcdl_str) +
                                  (alst->entries_avail-1) * sizeof(gdth_alist_str))) { 
                for (j = 0; j < alst->entries_init; ++j) {
                    ha->hdr[j].is_arraydrv = alst->list[j].is_arrayd;
                    ha->hdr[j].is_master = alst->list[j].is_master;
                    ha->hdr[j].is_parity = alst->list[j].is_parity;
                    ha->hdr[j].is_hotfix = alst->list[j].is_hotfix;
                    ha->hdr[j].master_no = alst->list[j].cd_handle;
                }
            } else if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
                                         ARRAY_DRV_LIST | LA_CTRL_PATTERN,
                                         0, 35 * sizeof(gdth_alist_str))) {
Linus Torvalds's avatar
Linus Torvalds committed
2224
                for (j = 0; j < 35; ++j) {
Linus Torvalds's avatar
Linus Torvalds committed
2225 2226 2227 2228 2229 2230
                    alst2 = &((gdth_alist_str *)ha->pscratch)[j];
                    ha->hdr[j].is_arraydrv = alst2->is_arrayd;
                    ha->hdr[j].is_master = alst2->is_master;
                    ha->hdr[j].is_parity = alst2->is_parity;
                    ha->hdr[j].is_hotfix = alst2->is_hotfix;
                    ha->hdr[j].master_no = alst2->cd_handle;
Linus Torvalds's avatar
Linus Torvalds committed
2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
                }
            }
        }
    }       
                                  
    /* initialize raw service */
    if (!gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_INIT,0,0,0)) {
        printk("GDT: Initialization error raw service (code %d)\n",
               ha->status);
        return 0;
    }
    TRACE2(("gdth_search_drives(): RAWSERVICE initialized\n"));

    /* set/get features raw service (scatter/gather) */
    if (gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_SET_FEAT,SCATTER_GATHER,
                          0,0)) {
        TRACE2(("gdth_search_drives(): set features RAWSERVICE OK\n"));
        if (gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_GET_FEAT,0,0,0)) {
            TRACE2(("gdth_search_dr(): get feat RAWSERVICE %d\n",
                    ha->info));
            ha->raw_feat = (ushort)ha->info;
        }
    } 

    /* set/get features cache service (equal to raw service) */
    if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_SET_FEAT,0,
                          SCATTER_GATHER,0)) {
        TRACE2(("gdth_search_drives(): set features CACHESERVICE OK\n"));
        if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_GET_FEAT,0,0,0)) {
            TRACE2(("gdth_search_dr(): get feat CACHESERV. %d\n",
                    ha->info));
            ha->cache_feat = (ushort)ha->info;
        }
    }

    /* reserve drives for raw service */
    if (reserve_mode != 0) {
        gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_RESERVE_ALL,
                          reserve_mode == 1 ? 1 : 3, 0, 0);
        TRACE2(("gdth_search_drives(): RESERVE_ALL code %d\n", 
                ha->status));
    }
    for (i = 0; i < MAX_RES_ARGS; i += 4) {
        if (reserve_list[i] == hanum && reserve_list[i+1] < ha->bus_cnt && 
            reserve_list[i+2] < ha->tid_cnt && reserve_list[i+3] < MAXLUN) {
            TRACE2(("gdth_search_drives(): reserve ha %d bus %d id %d lun %d\n",
                    reserve_list[i], reserve_list[i+1],
                    reserve_list[i+2], reserve_list[i+3]));
            if (!gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_RESERVE,0,
                                   reserve_list[i+1], reserve_list[i+2] | 
                                   (reserve_list[i+3] << 8))) {
                printk("GDT: Error raw service (RESERVE, code %d)\n",
                       ha->status);
             }
        }
    }

Alan Cox's avatar
Alan Cox committed
2288 2289 2290 2291 2292 2293 2294 2295 2296 2297
    /* Determine OEM string using IOCTL */
    oemstr = (gdth_oem_str_ioctl *)ha->pscratch;
    oemstr->params.ctl_version = 0x01;
    oemstr->params.buffer_size = sizeof(oemstr->text);
    if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
                          CACHE_READ_OEM_STRING_RECORD,INVALID_CHANNEL,
                          sizeof(gdth_oem_str_ioctl))) {
        TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD OK\n"));
        printk("GDT CTR%d Vendor: %s\n",hanum,oemstr->text.oem_company_name);
        /* Save the Host Drive inquiry data */
2298 2299
        strlcpy(ha->oem_name,oemstr->text.scsi_host_drive_inquiry_vendor_id,
		sizeof(ha->oem_name));
Alan Cox's avatar
Alan Cox committed
2300 2301 2302 2303
    } else {
        /* Old method, based on PCI ID */
        TRACE2(("gdth_search_drives(): CACHE_READ_OEM_STRING_RECORD failed\n"));
        if (ha->oem_id == OEM_ID_INTEL)
2304
            strlcpy(ha->oem_name,"Intel  ", sizeof(ha->oem_name));
Alan Cox's avatar
Alan Cox committed
2305
        else
2306
            strlcpy(ha->oem_name,"ICP    ", sizeof(ha->oem_name));
Alan Cox's avatar
Alan Cox committed
2307 2308
    }

Linus Torvalds's avatar
Linus Torvalds committed
2309 2310 2311 2312 2313 2314 2315
    /* scanning for host drives */
    for (i = 0; i < cdev_cnt; ++i) 
        gdth_analyse_hdrive(hanum,i);
    
    TRACE(("gdth_search_drives() OK\n"));
    return 1;
}
Linus Torvalds's avatar
Linus Torvalds committed
2316

Linus Torvalds's avatar
Linus Torvalds committed
2317 2318 2319 2320
static int gdth_analyse_hdrive(int hanum,ushort hdrive)
{
    register gdth_ha_str *ha;
    int drv_cyls, drv_hds, drv_secs;
Linus Torvalds's avatar
Linus Torvalds committed
2321

Linus Torvalds's avatar
Linus Torvalds committed
2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346
    TRACE(("gdth_analyse_hdrive() hanum %d drive %d\n",hanum,hdrive));
    if (hdrive >= MAX_HDRIVES)
        return 0;
    ha = HADATA(gdth_ctr_tab[hanum]);

    if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_INFO,hdrive,0,0)) 
        return 0;
    ha->hdr[hdrive].present = TRUE;
    ha->hdr[hdrive].size = ha->info;

    /* evaluate mapping (sectors per head, heads per cylinder) */
    ha->hdr[hdrive].size &= ~SECS32;
    if (ha->info2 == 0) {
        gdth_eval_mapping(ha->hdr[hdrive].size,&drv_cyls,&drv_hds,&drv_secs);
    } else {
        drv_hds = ha->info2 & 0xff;
        drv_secs = (ha->info2 >> 8) & 0xff;
        drv_cyls = ha->hdr[hdrive].size /drv_hds/drv_secs;
    }
    ha->hdr[hdrive].heads = (unchar)drv_hds;
    ha->hdr[hdrive].secs  = (unchar)drv_secs;
    /* round size */
    ha->hdr[hdrive].size  = drv_cyls * drv_hds * drv_secs;
    TRACE2(("gdth_search_dr() cdr. %d size %d hds %d scs %d\n",
            hdrive,ha->hdr[hdrive].size,drv_hds,drv_secs));
Linus Torvalds's avatar
Linus Torvalds committed
2347
            
Linus Torvalds's avatar
Linus Torvalds committed
2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367
    /* get informations about device */
    if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_DEVTYPE,hdrive,0,0)) {
        TRACE2(("gdth_search_dr() cache drive %d devtype %d\n",
                hdrive,ha->info));
        ha->hdr[hdrive].devtype = (ushort)ha->info;
    }

    /* cluster info */
    if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_CLUST_INFO,hdrive,0,0)) {
        TRACE2(("gdth_search_dr() cache drive %d cluster info %d\n",
                hdrive,ha->info));
        if (!shared_access)
            ha->hdr[hdrive].cluster_type = (unchar)ha->info;
    }

    /* R/W attributes */
    if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_RW_ATTRIBS,hdrive,0,0)) {
        TRACE2(("gdth_search_dr() cache drive %d r/w attrib. %d\n",
                hdrive,ha->info));
        ha->hdr[hdrive].rw_attribs = (unchar)ha->info;
Linus Torvalds's avatar
Linus Torvalds committed
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388
    }

    return 1;
}


/* command queueing/sending functions */

static void gdth_putq(int hanum,Scsi_Cmnd *scp,unchar priority)
{
    register gdth_ha_str *ha;
    register Scsi_Cmnd *pscp;
    register Scsi_Cmnd *nscp;
    ulong flags;
    unchar b, t;

    TRACE(("gdth_putq() priority %d\n",priority));
    ha = HADATA(gdth_ctr_tab[hanum]);
    GDTH_LOCK_HA(ha, flags);

    scp->SCp.this_residual = (int)priority;
Alan Cox's avatar
Alan Cox committed
2389
#if LINUX_VERSION_CODE >= 0x02053C
2390
    b = virt_ctr ? NUMDATA(scp->device->host)->busnum : scp->device->channel;
2391
    t = scp->device->id;
Alan Cox's avatar
Alan Cox committed
2392 2393 2394 2395
#else
    b = virt_ctr ? NUMDATA(scp->host)->busnum : scp->channel;
    t = scp->target;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2396
#if LINUX_VERSION_CODE >= 0x010300
Linus Torvalds's avatar
Linus Torvalds committed
2397 2398 2399 2400 2401 2402 2403
    if (priority >= DEFAULT_PRI) {
        if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha,b)].lock) ||
            (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock)) {
            TRACE2(("gdth_putq(): locked IO -> update_timeout()\n"));
            scp->SCp.buffers_residual = gdth_update_timeout(hanum, scp, 0);
        }
    }
Linus Torvalds's avatar
Linus Torvalds committed
2404
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437

    if (ha->req_first==NULL) {
        ha->req_first = scp;                    /* queue was empty */
        scp->SCp.ptr = NULL;
    } else {                                    /* queue not empty */
        pscp = ha->req_first;
        nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
        /* priority: 0-highest,..,0xff-lowest */
        while (nscp && (unchar)nscp->SCp.this_residual <= priority) {
            pscp = nscp;
            nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
        }
        pscp->SCp.ptr = (char *)scp;
        scp->SCp.ptr  = (char *)nscp;
    }
    GDTH_UNLOCK_HA(ha, flags);

#ifdef GDTH_STATISTICS
    flags = 0;
    for (nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
        ++flags;
    if (max_rq < flags) {
        max_rq = flags;
        TRACE3(("GDT: max_rq = %d\n",(ushort)max_rq));
    }
#endif
}

static void gdth_next(int hanum)
{
    register gdth_ha_str *ha;
    register Scsi_Cmnd *pscp;
    register Scsi_Cmnd *nscp;
Alan Cox's avatar
Alan Cox committed
2438
    unchar b, t, l, firsttime;
Linus Torvalds's avatar
Linus Torvalds committed
2439
    unchar this_cmd, next_cmd;
2440
    ulong flags = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2441 2442 2443 2444
    int cmd_index;

    TRACE(("gdth_next() hanum %d\n",hanum));
    ha = HADATA(gdth_ctr_tab[hanum]);
2445
    if (!gdth_polling) 
Alan Cox's avatar
Alan Cox committed
2446
        GDTH_LOCK_HA(ha, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2447 2448 2449 2450 2451 2452 2453 2454 2455

    ha->cmd_cnt = ha->cmd_offs_dpmem = 0;
    this_cmd = firsttime = TRUE;
    next_cmd = gdth_polling ? FALSE:TRUE;
    cmd_index = 0;

    for (nscp = pscp = ha->req_first; nscp; nscp = (Scsi_Cmnd *)nscp->SCp.ptr) {
        if (nscp != pscp && nscp != (Scsi_Cmnd *)pscp->SCp.ptr)
            pscp = (Scsi_Cmnd *)pscp->SCp.ptr;
Alan Cox's avatar
Alan Cox committed
2456
#if LINUX_VERSION_CODE >= 0x02053C
2457
        b = virt_ctr ? NUMDATA(nscp->device->host)->busnum : nscp->device->channel;
2458
        t = nscp->device->id;
Alan Cox's avatar
Alan Cox committed
2459 2460 2461 2462 2463 2464
        l = nscp->device->lun;
#else
        b = virt_ctr ? NUMDATA(nscp->host)->busnum : nscp->channel;
        t = nscp->target;
        l = nscp->lun;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483
        if (nscp->SCp.this_residual >= DEFAULT_PRI) {
            if ((b != ha->virt_bus && ha->raw[BUS_L2P(ha,b)].lock) ||
                (b == ha->virt_bus && t < MAX_HDRIVES && ha->hdr[t].lock)) 
                continue;
        }

        if (firsttime) {
            if (gdth_test_busy(hanum)) {        /* controller busy ? */
                TRACE(("gdth_next() controller %d busy !\n",hanum));
                if (!gdth_polling) {
                    GDTH_UNLOCK_HA(ha, flags);
                    return;
                }
                while (gdth_test_busy(hanum))
                    gdth_delay(1);
            }   
            firsttime = FALSE;
        }

Linus Torvalds's avatar
Linus Torvalds committed
2484 2485 2486
#if LINUX_VERSION_CODE >= 0x010300
        if (nscp->done != gdth_scsi_done || nscp->cmnd[0] != 0xff) 
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2487 2488
        {
        if (nscp->SCp.phase == -1) {
Linus Torvalds's avatar
Linus Torvalds committed
2489
            nscp->SCp.phase = CACHESERVICE;           /* default: cache svc. */ 
Linus Torvalds's avatar
Linus Torvalds committed
2490 2491
            if (nscp->cmnd[0] == TEST_UNIT_READY) {
                TRACE2(("TEST_UNIT_READY Bus %d Id %d LUN %d\n", 
Alan Cox's avatar
Alan Cox committed
2492
                        b, t, l));
Linus Torvalds's avatar
Linus Torvalds committed
2493 2494
                /* TEST_UNIT_READY -> set scan mode */
                if ((ha->scan_mode & 0x0f) == 0) {
Alan Cox's avatar
Alan Cox committed
2495
                    if (b == 0 && t == 0 && l == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
2496 2497 2498 2499
                        ha->scan_mode |= 1;
                        TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
                    }
                } else if ((ha->scan_mode & 0x0f) == 1) {
Alan Cox's avatar
Alan Cox committed
2500 2501
                    if (b == 0 && ((t == 0 && l == 1) ||
                         (t == 1 && l == 0))) {
Linus Torvalds's avatar
Linus Torvalds committed
2502 2503 2504
                        nscp->SCp.sent_command = GDT_SCAN_START;
                        nscp->SCp.phase = ((ha->scan_mode & 0x10 ? 1:0) << 8) 
                            | SCSIRAWSERVICE;
Linus Torvalds's avatar
Linus Torvalds committed
2505 2506 2507 2508 2509 2510 2511 2512 2513
                        ha->scan_mode = 0x12;
                        TRACE2(("Scan mode: 0x%x (SCAN_START)\n", 
                                ha->scan_mode));
                    } else {
                        ha->scan_mode &= 0x10;
                        TRACE2(("Scan mode: 0x%x\n", ha->scan_mode));
                    }                   
                } else if (ha->scan_mode == 0x12) {
                    if (b == ha->bus_cnt && t == ha->tid_cnt-1) {
Linus Torvalds's avatar
Linus Torvalds committed
2514 2515
                        nscp->SCp.phase = SCSIRAWSERVICE;
                        nscp->SCp.sent_command = GDT_SCAN_END;
Linus Torvalds's avatar
Linus Torvalds committed
2516 2517 2518 2519 2520 2521
                        ha->scan_mode &= 0x10;
                        TRACE2(("Scan mode: 0x%x (SCAN_END)\n", 
                                ha->scan_mode));
                    }
                }
            }
Linus Torvalds's avatar
Linus Torvalds committed
2522 2523 2524 2525 2526 2527
            if (b == ha->virt_bus && nscp->cmnd[0] != INQUIRY &&
                nscp->cmnd[0] != READ_CAPACITY && nscp->cmnd[0] != MODE_SENSE &&
                (ha->hdr[t].cluster_type & CLUSTER_DRIVE)) {
                /* always GDT_CLUST_INFO! */
                nscp->SCp.sent_command = GDT_CLUST_INFO;
            }
Linus Torvalds's avatar
Linus Torvalds committed
2528 2529 2530
        }
        }

Linus Torvalds's avatar
Linus Torvalds committed
2531 2532 2533 2534 2535 2536 2537
        if (nscp->SCp.sent_command != -1) {
            if ((nscp->SCp.phase & 0xff) == CACHESERVICE) {
                if (!(cmd_index=gdth_fill_cache_cmd(hanum,nscp,t)))
                    this_cmd = FALSE;
                next_cmd = FALSE;
            } else if ((nscp->SCp.phase & 0xff) == SCSIRAWSERVICE) {
                if (!(cmd_index=gdth_fill_raw_cmd(hanum,nscp,BUS_L2P(ha,b))))
Linus Torvalds's avatar
Linus Torvalds committed
2538 2539
                    this_cmd = FALSE;
                next_cmd = FALSE;
Linus Torvalds's avatar
Linus Torvalds committed
2540 2541 2542 2543 2544 2545 2546 2547
            } else {
                memset((char*)nscp->sense_buffer,0,16);
                nscp->sense_buffer[0] = 0x70;
                nscp->sense_buffer[2] = NOT_READY;
                nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
                if (!nscp->SCp.have_data_in)
                    nscp->SCp.have_data_in++;
                else {
2548 2549
                    if (!gdth_polling) 
                        GDTH_UNLOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2550 2551
                    /* io_request_lock already active ! */
                    nscp->scsi_done(nscp);
2552
                    if (!gdth_polling) 
Alan Cox's avatar
Alan Cox committed
2553
                        GDTH_LOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2554
                }
Linus Torvalds's avatar
Linus Torvalds committed
2555 2556 2557
            }
        } else

Linus Torvalds's avatar
Linus Torvalds committed
2558 2559
#if LINUX_VERSION_CODE >= 0x010300
        if (nscp->done == gdth_scsi_done && nscp->cmnd[0] == 0xff) {
Linus Torvalds's avatar
Linus Torvalds committed
2560 2561 2562 2563
            if (!(cmd_index=gdth_special_cmd(hanum,nscp)))
                this_cmd = FALSE;
            next_cmd = FALSE;
        } else
Linus Torvalds's avatar
Linus Torvalds committed
2564
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2565 2566 2567 2568 2569 2570
        if (b != ha->virt_bus) {
            if (ha->raw[BUS_L2P(ha,b)].io_cnt[t] >= GDTH_MAX_RAW ||
                !(cmd_index=gdth_fill_raw_cmd(hanum,nscp,BUS_L2P(ha,b)))) 
                this_cmd = FALSE;
            else 
                ha->raw[BUS_L2P(ha,b)].io_cnt[t]++;
Alan Cox's avatar
Alan Cox committed
2571
        } else if (t >= MAX_HDRIVES || !ha->hdr[t].present || l != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
2572
            TRACE2(("Command 0x%x to bus %d id %d lun %d -> IGNORE\n",
Alan Cox's avatar
Alan Cox committed
2573
                    nscp->cmnd[0], b, t, l));
Linus Torvalds's avatar
Linus Torvalds committed
2574
            nscp->result = DID_BAD_TARGET << 16;
Linus Torvalds's avatar
Linus Torvalds committed
2575 2576 2577
            if (!nscp->SCp.have_data_in)
                nscp->SCp.have_data_in++;
            else {
2578
                if (!gdth_polling) 
Alan Cox's avatar
Alan Cox committed
2579
                    GDTH_UNLOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2580 2581
                /* io_request_lock already active ! */      
                nscp->scsi_done(nscp);
2582
                if (!gdth_polling) 
Alan Cox's avatar
Alan Cox committed
2583
                    GDTH_LOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2584
            }
Linus Torvalds's avatar
Linus Torvalds committed
2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596
        } else {
            switch (nscp->cmnd[0]) {
              case TEST_UNIT_READY:
              case INQUIRY:
              case REQUEST_SENSE:
              case READ_CAPACITY:
              case VERIFY:
              case START_STOP:
              case MODE_SENSE:
                TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
                       nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
                       nscp->cmnd[4],nscp->cmnd[5]));
Linus Torvalds's avatar
Linus Torvalds committed
2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
                if (ha->hdr[t].media_changed && nscp->cmnd[0] != INQUIRY) {
                    /* return UNIT_ATTENTION */
                    TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
                             nscp->cmnd[0], t));
                    ha->hdr[t].media_changed = FALSE;
                    memset((char*)nscp->sense_buffer,0,16);
                    nscp->sense_buffer[0] = 0x70;
                    nscp->sense_buffer[2] = UNIT_ATTENTION;
                    nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
                    if (!nscp->SCp.have_data_in)
                        nscp->SCp.have_data_in++;
                    else {
Alan Cox's avatar
Alan Cox committed
2609 2610
                        if (!gdth_polling) 
                            GDTH_UNLOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2611 2612
                        /* io_request_lock already active ! */      
                        nscp->scsi_done(nscp);
Alan Cox's avatar
Alan Cox committed
2613 2614
                        if (!gdth_polling) 
                            GDTH_LOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2615 2616
                    }
                } else if (gdth_internal_cache_cmd(hanum,nscp)) {
2617
                    if (!gdth_polling) 
Alan Cox's avatar
Alan Cox committed
2618
                        GDTH_UNLOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2619 2620
                    /* io_request_lock already active ! */      
                    nscp->scsi_done(nscp);
2621
                    if (!gdth_polling) 
Alan Cox's avatar
Alan Cox committed
2622
                        GDTH_LOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2623 2624 2625 2626 2627 2628 2629 2630 2631 2632
                }
                break;

              case ALLOW_MEDIUM_REMOVAL:
                TRACE(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
                       nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
                       nscp->cmnd[4],nscp->cmnd[5]));
                if ( (nscp->cmnd[4]&1) && !(ha->hdr[t].devtype&1) ) {
                    TRACE(("Prevent r. nonremov. drive->do nothing\n"));
                    nscp->result = DID_OK << 16;
Linus Torvalds's avatar
Linus Torvalds committed
2633
                    nscp->sense_buffer[0] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2634 2635 2636
                    if (!nscp->SCp.have_data_in)
                        nscp->SCp.have_data_in++;
                    else {
Alan Cox's avatar
Alan Cox committed
2637 2638
                        if (!gdth_polling) 
                            GDTH_UNLOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2639 2640
                        /* io_request_lock already active ! */      
                        nscp->scsi_done(nscp);
Alan Cox's avatar
Alan Cox committed
2641 2642
                        if (!gdth_polling) 
                            GDTH_LOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2643 2644 2645 2646 2647 2648 2649 2650 2651 2652
                    }
                } else {
                    nscp->cmnd[3] = (ha->hdr[t].devtype&1) ? 1:0;
                    TRACE(("Prevent/allow r. %d rem. drive %d\n",
                           nscp->cmnd[4],nscp->cmnd[3]));
                    if (!(cmd_index=gdth_fill_cache_cmd(hanum,nscp,t)))
                        this_cmd = FALSE;
                }
                break;
                
Linus Torvalds's avatar
Linus Torvalds committed
2653 2654 2655 2656 2657 2658 2659 2660
              case RESERVE:
              case RELEASE:
                TRACE2(("cache cmd %s\n",nscp->cmnd[0] == RESERVE ?
                        "RESERVE" : "RELEASE"));
                if (!(cmd_index=gdth_fill_cache_cmd(hanum,nscp,t)))
                    this_cmd = FALSE;
                break;
                
Linus Torvalds's avatar
Linus Torvalds committed
2661 2662 2663 2664
              case READ_6:
              case WRITE_6:
              case READ_10:
              case WRITE_10:
Linus Torvalds's avatar
Linus Torvalds committed
2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676
                if (ha->hdr[t].media_changed) {
                    /* return UNIT_ATTENTION */
                    TRACE2(("cmd 0x%x target %d: UNIT_ATTENTION\n",
                             nscp->cmnd[0], t));
                    ha->hdr[t].media_changed = FALSE;
                    memset((char*)nscp->sense_buffer,0,16);
                    nscp->sense_buffer[0] = 0x70;
                    nscp->sense_buffer[2] = UNIT_ATTENTION;
                    nscp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
                    if (!nscp->SCp.have_data_in)
                        nscp->SCp.have_data_in++;
                    else {
Alan Cox's avatar
Alan Cox committed
2677 2678
                        if (!gdth_polling) 
                            GDTH_UNLOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2679 2680
                        /* io_request_lock already active ! */      
                        nscp->scsi_done(nscp);
Alan Cox's avatar
Alan Cox committed
2681 2682
                        if (!gdth_polling) 
                            GDTH_LOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2683 2684
                    }
                } else if (!(cmd_index=gdth_fill_cache_cmd(hanum,nscp,t)))
Linus Torvalds's avatar
Linus Torvalds committed
2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697
                    this_cmd = FALSE;
                break;

              default:
                TRACE2(("cache cmd %x/%x/%x/%x/%x/%x unknown\n",nscp->cmnd[0],
                        nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
                        nscp->cmnd[4],nscp->cmnd[5]));
                printk("GDT: Unknown SCSI command 0x%x to cache service !\n",
                       nscp->cmnd[0]);
                nscp->result = DID_ABORT << 16;
                if (!nscp->SCp.have_data_in)
                    nscp->SCp.have_data_in++;
                else {
2698
                    if (!gdth_polling) 
Alan Cox's avatar
Alan Cox committed
2699
                        GDTH_UNLOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2700 2701
                    /* io_request_lock already active ! */  
                    nscp->scsi_done(nscp);
2702
                    if (!gdth_polling) 
Alan Cox's avatar
Alan Cox committed
2703
                        GDTH_LOCK_HA(ha,flags);
Linus Torvalds's avatar
Linus Torvalds committed
2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722
                }
                break;
            }
        }

        if (!this_cmd)
            break;
        if (nscp == ha->req_first)
            ha->req_first = pscp = (Scsi_Cmnd *)nscp->SCp.ptr;
        else
            pscp->SCp.ptr = nscp->SCp.ptr;
        if (!next_cmd)
            break;
    }

    if (ha->cmd_cnt > 0) {
        gdth_release_event(hanum);
    }

2723
    if (!gdth_polling) 
Alan Cox's avatar
Alan Cox committed
2724
        GDTH_UNLOCK_HA(ha, flags);
Linus Torvalds's avatar
Linus Torvalds committed
2725 2726 2727 2728 2729 2730 2731 2732

    if (gdth_polling && ha->cmd_cnt > 0) {
        if (!gdth_wait(hanum,cmd_index,POLL_TIMEOUT))
            printk("GDT: Controller %d: Command %d timed out !\n",
                   hanum,cmd_index);
    }
}
    
Alan Cox's avatar
Alan Cox committed
2733 2734
static void gdth_copy_internal_data(int hanum,Scsi_Cmnd *scp,
                                    char *buffer,ushort count)
Linus Torvalds's avatar
Linus Torvalds committed
2735 2736 2737 2738
{
    ushort cpcount,i;
    ushort cpsum,cpnow;
    struct scatterlist *sl;
Alan Cox's avatar
Alan Cox committed
2739 2740 2741
    gdth_ha_str *ha;
    int sgcnt;
    char *address;
Linus Torvalds's avatar
Linus Torvalds committed
2742 2743

    cpcount = count<=(ushort)scp->bufflen ? count:(ushort)scp->bufflen;
Alan Cox's avatar
Alan Cox committed
2744 2745
    ha = HADATA(gdth_ctr_tab[hanum]);

Linus Torvalds's avatar
Linus Torvalds committed
2746 2747
    if (scp->use_sg) {
        sl = (struct scatterlist *)scp->request_buffer;
Alan Cox's avatar
Alan Cox committed
2748 2749 2750 2751
#if LINUX_VERSION_CODE >= 0x020400
        sgcnt = pci_map_sg(ha->pdev,sl,scp->use_sg,PCI_DMA_FROMDEVICE);
        for (i=0,cpsum=0; i<sgcnt; ++i,++sl) {
            cpnow = (ushort)sg_dma_len(sl);
Linus Torvalds's avatar
Linus Torvalds committed
2752 2753 2754 2755 2756
            TRACE(("copy_internal() now %d sum %d count %d %d\n",
                          cpnow,cpsum,cpcount,(ushort)scp->bufflen));
            if (cpsum+cpnow > cpcount) 
                cpnow = cpcount - cpsum;
            cpsum += cpnow;
Alan Cox's avatar
Alan Cox committed
2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775
            address = (char *)phys_to_virt(sg_dma_address(sl));
            memcpy(address,buffer,cpnow);
            if (cpsum == cpcount)
                break;
            buffer += cpnow;
        }
        pci_unmap_sg(ha->pdev,scp->request_buffer,
                     scp->use_sg,PCI_DMA_FROMDEVICE);
#else
        sgcnt = scp->use_sg;
        for (i=0,cpsum=0; i<sgcnt; ++i,++sl) {
            cpnow = (ushort)sl->length;
            TRACE(("copy_internal() now %d sum %d count %d %d\n",
                          cpnow,cpsum,cpcount,(ushort)scp->bufflen));
            if (cpsum+cpnow > cpcount) 
               cpnow = cpcount - cpsum;
            cpsum += cpnow;
            address = (char *)sl->address;
            memcpy(address,buffer,cpnow);
Linus Torvalds's avatar
Linus Torvalds committed
2776 2777 2778 2779
            if (cpsum == cpcount)
                break;
            buffer += cpnow;
        }
Alan Cox's avatar
Alan Cox committed
2780
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
    } else {
        TRACE(("copy_internal() count %d\n",cpcount));
        memcpy((char*)scp->request_buffer,buffer,cpcount);
    }
}

static int gdth_internal_cache_cmd(int hanum,Scsi_Cmnd *scp)
{
    register gdth_ha_str *ha;
    unchar t;
    gdth_inq_data inq;
    gdth_rdcap_data rdc;
    gdth_sense_data sd;
    gdth_modep_data mpd;

    ha = HADATA(gdth_ctr_tab[hanum]);
Alan Cox's avatar
Alan Cox committed
2797
#if LINUX_VERSION_CODE >= 0x02053C
2798
    t  = scp->device->id;
Alan Cox's avatar
Alan Cox committed
2799 2800 2801
#else
    t  = scp->target;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817
    TRACE(("gdth_internal_cache_cmd() cmd 0x%x hdrive %d\n",
           scp->cmnd[0],t));

    switch (scp->cmnd[0]) {
      case TEST_UNIT_READY:
      case VERIFY:
      case START_STOP:
        TRACE2(("Test/Verify/Start hdrive %d\n",t));
        break;

      case INQUIRY:
        TRACE2(("Inquiry hdrive %d devtype %d\n",
                t,ha->hdr[t].devtype));
        inq.type_qual = (ha->hdr[t].devtype&4) ? TYPE_ROM:TYPE_DISK;
        /* you can here set all disks to removable, if you want to do
           a flush using the ALLOW_MEDIUM_REMOVAL command */
Linus Torvalds's avatar
Linus Torvalds committed
2818 2819 2820 2821
        inq.modif_rmb = 0x00;
        if ((ha->hdr[t].devtype & 1) ||
            (ha->hdr[t].cluster_type & CLUSTER_DRIVE))
            inq.modif_rmb = 0x80;
Linus Torvalds's avatar
Linus Torvalds committed
2822 2823 2824
        inq.version   = 2;
        inq.resp_aenc = 2;
        inq.add_length= 32;
Alan Cox's avatar
Alan Cox committed
2825
        strcpy(inq.vendor,ha->oem_name);
Linus Torvalds's avatar
Linus Torvalds committed
2826 2827
        sprintf(inq.product,"Host Drive  #%02d",t);
        strcpy(inq.revision,"   ");
Alan Cox's avatar
Alan Cox committed
2828
        gdth_copy_internal_data(hanum,scp,(char*)&inq,sizeof(gdth_inq_data));
Linus Torvalds's avatar
Linus Torvalds committed
2829 2830 2831 2832 2833 2834 2835 2836 2837
        break;

      case REQUEST_SENSE:
        TRACE2(("Request sense hdrive %d\n",t));
        sd.errorcode = 0x70;
        sd.segno     = 0x00;
        sd.key       = NO_SENSE;
        sd.info      = 0;
        sd.add_length= 0;
Alan Cox's avatar
Alan Cox committed
2838
        gdth_copy_internal_data(hanum,scp,(char*)&sd,sizeof(gdth_sense_data));
Linus Torvalds's avatar
Linus Torvalds committed
2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849
        break;

      case MODE_SENSE:
        TRACE2(("Mode sense hdrive %d\n",t));
        memset((char*)&mpd,0,sizeof(gdth_modep_data));
        mpd.hd.data_length = sizeof(gdth_modep_data);
        mpd.hd.dev_par     = (ha->hdr[t].devtype&2) ? 0x80:0;
        mpd.hd.bd_length   = sizeof(mpd.bd);
        mpd.bd.block_length[0] = (SECTOR_SIZE & 0x00ff0000) >> 16;
        mpd.bd.block_length[1] = (SECTOR_SIZE & 0x0000ff00) >> 8;
        mpd.bd.block_length[2] = (SECTOR_SIZE & 0x000000ff);
Alan Cox's avatar
Alan Cox committed
2850
        gdth_copy_internal_data(hanum,scp,(char*)&mpd,sizeof(gdth_modep_data));
Linus Torvalds's avatar
Linus Torvalds committed
2851 2852 2853 2854 2855 2856
        break;

      case READ_CAPACITY:
        TRACE2(("Read capacity hdrive %d\n",t));
        rdc.last_block_no = ntohl(ha->hdr[t].size-1);
        rdc.block_length  = ntohl(SECTOR_SIZE);
Alan Cox's avatar
Alan Cox committed
2857
        gdth_copy_internal_data(hanum,scp,(char*)&rdc,sizeof(gdth_rdcap_data));
Linus Torvalds's avatar
Linus Torvalds committed
2858 2859 2860 2861 2862 2863
        break;

      default:
        TRACE2(("Internal cache cmd 0x%x unknown\n",scp->cmnd[0]));
        break;
    }
Linus Torvalds's avatar
Linus Torvalds committed
2864

Linus Torvalds's avatar
Linus Torvalds committed
2865
    scp->result = DID_OK << 16;
Linus Torvalds's avatar
Linus Torvalds committed
2866
    scp->sense_buffer[0] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880

    if (!scp->SCp.have_data_in)
        scp->SCp.have_data_in++;
    else 
        return 1;

    return 0;
}
    
static int gdth_fill_cache_cmd(int hanum,Scsi_Cmnd *scp,ushort hdrive)
{
    register gdth_ha_str *ha;
    register gdth_cmd_str *cmdp;
    struct scatterlist *sl;
Linus Torvalds's avatar
Linus Torvalds committed
2881
    ushort i, cnt;
Alan Cox's avatar
Alan Cox committed
2882 2883
    ulong32 no, phys_addr;
    int cmd_index, read_write, sgcnt;
Linus Torvalds's avatar
Linus Torvalds committed
2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904

    ha = HADATA(gdth_ctr_tab[hanum]);
    cmdp = ha->pccb;
    TRACE(("gdth_fill_cache_cmd() cmd 0x%x cmdsize %d hdrive %d\n",
                 scp->cmnd[0],scp->cmd_len,hdrive));

    if (ha->type==GDT_EISA && ha->cmd_cnt>0) 
        return 0;

    cmdp->Service = CACHESERVICE;
    cmdp->RequestBuffer = scp;
    /* search free command index */
    if (!(cmd_index=gdth_get_cmd_index(hanum))) {
        TRACE(("GDT: No free command index found\n"));
        return 0;
    }
    /* if it's the first command, set command semaphore */
    if (ha->cmd_cnt == 0)
        gdth_set_sema0(hanum);

    /* fill command */
Alan Cox's avatar
Alan Cox committed
2905
    read_write = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2906 2907 2908 2909 2910 2911 2912
    if (scp->SCp.sent_command != -1) 
        cmdp->OpCode = scp->SCp.sent_command;   /* special cache cmd. */
    else if (scp->cmnd[0] == RESERVE) 
        cmdp->OpCode = GDT_RESERVE_DRV;
    else if (scp->cmnd[0] == RELEASE)
        cmdp->OpCode = GDT_RELEASE_DRV;
    else if (scp->cmnd[0] == ALLOW_MEDIUM_REMOVAL) {
Linus Torvalds's avatar
Linus Torvalds committed
2913
        if (scp->cmnd[4] & 1)                   /* prevent ? */
Linus Torvalds's avatar
Linus Torvalds committed
2914
            cmdp->OpCode = GDT_MOUNT;
Linus Torvalds's avatar
Linus Torvalds committed
2915
        else if (scp->cmnd[3] & 1)              /* removable drive ? */
Linus Torvalds's avatar
Linus Torvalds committed
2916 2917 2918
            cmdp->OpCode = GDT_UNMOUNT;
        else
            cmdp->OpCode = GDT_FLUSH;
Alan Cox's avatar
Alan Cox committed
2919 2920 2921
    } else if (scp->cmnd[0] == WRITE_6 || scp->cmnd[0] == WRITE_10 ||
               scp->cmnd[0] == WRITE_12) {
        read_write = 1;
Linus Torvalds's avatar
Linus Torvalds committed
2922 2923 2924
        if (gdth_write_through || ((ha->hdr[hdrive].rw_attribs & 1) && 
                                   (ha->cache_feat & GDT_WR_THROUGH)))
            cmdp->OpCode = GDT_WRITE_THR;
Linus Torvalds's avatar
Linus Torvalds committed
2925
        else
Linus Torvalds's avatar
Linus Torvalds committed
2926
            cmdp->OpCode = GDT_WRITE;
Linus Torvalds's avatar
Linus Torvalds committed
2927
    } else {
Alan Cox's avatar
Alan Cox committed
2928
        read_write = 2;
Linus Torvalds's avatar
Linus Torvalds committed
2929
        cmdp->OpCode = GDT_READ;
Linus Torvalds's avatar
Linus Torvalds committed
2930
    }
Linus Torvalds's avatar
Linus Torvalds committed
2931 2932 2933 2934 2935
    
    cmdp->BoardNode        = LOCALBOARD;
    cmdp->u.cache.DeviceNo = hdrive;
    cmdp->u.cache.BlockNo  = 1;
    cmdp->u.cache.sg_canz  = 0;
Linus Torvalds's avatar
Linus Torvalds committed
2936

Linus Torvalds's avatar
Linus Torvalds committed
2937
    if (read_write) {
Linus Torvalds's avatar
Linus Torvalds committed
2938
        if (scp->cmd_len != 6) {
Linus Torvalds's avatar
Linus Torvalds committed
2939 2940 2941 2942
            memcpy(&no, &scp->cmnd[2], sizeof(ulong32));
            cmdp->u.cache.BlockNo = ntohl(no);
            memcpy(&cnt, &scp->cmnd[7], sizeof(ushort));
            cmdp->u.cache.BlockCnt = (ulong32)ntohs(cnt);
Linus Torvalds's avatar
Linus Torvalds committed
2943
        } else {
Linus Torvalds's avatar
Linus Torvalds committed
2944 2945
            memcpy(&no, &scp->cmnd[0], sizeof(ulong32));
            cmdp->u.cache.BlockNo = ntohl(no) & 0x001fffffUL;
Linus Torvalds's avatar
Linus Torvalds committed
2946 2947 2948 2949 2950 2951
            cmdp->u.cache.BlockCnt= scp->cmnd[4]==0 ? 0x100 : scp->cmnd[4];
        }

        if (scp->use_sg) {
            cmdp->u.cache.DestAddr= 0xffffffff;
            sl = (struct scatterlist *)scp->request_buffer;
Alan Cox's avatar
Alan Cox committed
2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963
            sgcnt = scp->use_sg;
#if LINUX_VERSION_CODE >= 0x020400
            scp->SCp.Status = GDTH_MAP_SG;
            scp->SCp.Message = (read_write == 1 ? 
                PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);   
            sgcnt = pci_map_sg(ha->pdev,sl,scp->use_sg,scp->SCp.Message);
            for (i=0; i<sgcnt; ++i,++sl) {
                cmdp->u.cache.sg_lst[i].sg_ptr = sg_dma_address(sl);
                cmdp->u.cache.sg_lst[i].sg_len = sg_dma_len(sl);
            }
#else
            for (i=0; i<sgcnt; ++i,++sl) {
Linus Torvalds's avatar
Linus Torvalds committed
2964 2965 2966
                cmdp->u.cache.sg_lst[i].sg_ptr = virt_to_bus(sl->address);
                cmdp->u.cache.sg_lst[i].sg_len = (ulong32)sl->length;
            }
Alan Cox's avatar
Alan Cox committed
2967
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978
            cmdp->u.cache.sg_canz = (ulong32)i;

#ifdef GDTH_STATISTICS
            if (max_sg < (ulong32)i) {
                max_sg = (ulong32)i;
                TRACE3(("GDT: max_sg = %d\n",i));
            }
#endif
            if (i<GDTH_MAXSG)
                cmdp->u.cache.sg_lst[i].sg_len = 0;
        } else {
Alan Cox's avatar
Alan Cox committed
2979 2980 2981 2982 2983 2984 2985 2986 2987 2988
#if LINUX_VERSION_CODE >= 0x020400
            scp->SCp.Status = GDTH_MAP_SINGLE;
            scp->SCp.Message = (read_write == 1 ? 
                PCI_DMA_TODEVICE : PCI_DMA_FROMDEVICE);   
            phys_addr = pci_map_single(ha->pdev,scp->request_buffer,
                                       scp->request_bufflen,scp->SCp.Message);
            scp->SCp.dma_handle = phys_addr;
#else
            phys_addr = virt_to_bus(scp->request_buffer);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
2989 2990 2991
            if (ha->cache_feat & SCATTER_GATHER) {
                cmdp->u.cache.DestAddr = 0xffffffff;
                cmdp->u.cache.sg_canz = 1;
Alan Cox's avatar
Alan Cox committed
2992
                cmdp->u.cache.sg_lst[0].sg_ptr = phys_addr;
Linus Torvalds's avatar
Linus Torvalds committed
2993 2994 2995
                cmdp->u.cache.sg_lst[0].sg_len = scp->request_bufflen;
                cmdp->u.cache.sg_lst[1].sg_len = 0;
            } else {
Alan Cox's avatar
Alan Cox committed
2996
                cmdp->u.cache.DestAddr  = phys_addr;
Linus Torvalds's avatar
Linus Torvalds committed
2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033
                cmdp->u.cache.sg_canz= 0;
            }
        }
    }
    TRACE(("cache cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
           cmdp->u.cache.DestAddr,cmdp->u.cache.sg_canz,
           cmdp->u.cache.sg_lst[0].sg_ptr,
           cmdp->u.cache.sg_lst[0].sg_len));
    TRACE(("cache cmd: cmd %d blockno. %d, blockcnt %d\n",
           cmdp->OpCode,cmdp->u.cache.BlockNo,cmdp->u.cache.BlockCnt));

    /* evaluate command size, check space */
    ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) +
        (ushort)cmdp->u.cache.sg_canz * sizeof(gdth_sg_str);
    if (ha->cmd_len & 3)
        ha->cmd_len += (4 - (ha->cmd_len & 3));

    if (ha->cmd_cnt > 0) {
        if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
            ha->ic_all_size) {
            TRACE2(("gdth_fill_cache() DPMEM overflow\n"));
            ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
            return 0;
        }
    }

    /* copy command */
    gdth_copy_command(hanum);
    return cmd_index;
}

static int gdth_fill_raw_cmd(int hanum,Scsi_Cmnd *scp,unchar b)
{
    register gdth_ha_str *ha;
    register gdth_cmd_str *cmdp;
    struct scatterlist *sl;
    ushort i;
Alan Cox's avatar
Alan Cox committed
3034 3035
    ulong32 phys_addr, sense_paddr;
    int cmd_index, sgcnt;
Linus Torvalds's avatar
Linus Torvalds committed
3036 3037 3038
    unchar t,l;

    ha = HADATA(gdth_ctr_tab[hanum]);
Alan Cox's avatar
Alan Cox committed
3039
#if LINUX_VERSION_CODE >= 0x02053C
3040 3041
    t = scp->device->id;
    l = scp->device->lun;
Alan Cox's avatar
Alan Cox committed
3042 3043 3044 3045
#else
    t = scp->target;
    l = scp->lun;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064
    cmdp = ha->pccb;
    TRACE(("gdth_fill_raw_cmd() cmd 0x%x bus %d ID %d LUN %d\n",
           scp->cmnd[0],b,t,l));

    if (ha->type==GDT_EISA && ha->cmd_cnt>0) 
        return 0;

    cmdp->Service = SCSIRAWSERVICE;
    cmdp->RequestBuffer = scp;
    /* search free command index */
    if (!(cmd_index=gdth_get_cmd_index(hanum))) {
        TRACE(("GDT: No free command index found\n"));
        return 0;
    }
    /* if it's the first command, set command semaphore */
    if (ha->cmd_cnt == 0)
        gdth_set_sema0(hanum);

    /* fill command */  
Linus Torvalds's avatar
Linus Torvalds committed
3065 3066
    if (scp->SCp.sent_command != -1) {
        cmdp->OpCode           = scp->SCp.sent_command; /* special raw cmd. */
Linus Torvalds's avatar
Linus Torvalds committed
3067 3068 3069 3070 3071 3072 3073 3074
        cmdp->BoardNode        = LOCALBOARD;
        cmdp->u.raw.direction  = (scp->SCp.phase >> 8);
        TRACE2(("special raw cmd 0x%x param 0x%x\n", 
                cmdp->OpCode, cmdp->u.raw.direction));

        /* evaluate command size */
        ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst);
    } else {
Alan Cox's avatar
Alan Cox committed
3075 3076 3077 3078 3079 3080 3081
#if LINUX_VERSION_CODE >= 0x020400
        sense_paddr = pci_map_single(ha->pdev,scp->sense_buffer,
                                     16,PCI_DMA_FROMDEVICE);
        scp->SCp.buffer = (struct scatterlist *)sense_paddr;
#else
        sense_paddr = virt_to_bus(scp->sense_buffer);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
3082 3083 3084 3085 3086 3087 3088 3089 3090 3091
        cmdp->OpCode           = GDT_WRITE;             /* always */
        cmdp->BoardNode        = LOCALBOARD;
        cmdp->u.raw.reserved   = 0;
        cmdp->u.raw.mdisc_time = 0;
        cmdp->u.raw.mcon_time  = 0;
        cmdp->u.raw.clen       = scp->cmd_len;
        cmdp->u.raw.target     = t;
        cmdp->u.raw.lun        = l;
        cmdp->u.raw.bus        = b;
        cmdp->u.raw.priority   = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3092
        cmdp->u.raw.link_p     = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3093 3094
        cmdp->u.raw.sdlen      = scp->request_bufflen;
        cmdp->u.raw.sense_len  = 16;
Alan Cox's avatar
Alan Cox committed
3095
        cmdp->u.raw.sense_data = sense_paddr;
Linus Torvalds's avatar
Linus Torvalds committed
3096
        cmdp->u.raw.direction  = 
Linus Torvalds's avatar
Linus Torvalds committed
3097
            gdth_direction_tab[scp->cmnd[0]]==DOU ? GDTH_DATA_OUT:GDTH_DATA_IN;
Linus Torvalds's avatar
Linus Torvalds committed
3098 3099 3100 3101 3102
        memcpy(cmdp->u.raw.cmd,scp->cmnd,12);

        if (scp->use_sg) {
            cmdp->u.raw.sdata  = 0xffffffff;
            sl = (struct scatterlist *)scp->request_buffer;
Alan Cox's avatar
Alan Cox committed
3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113
            sgcnt = scp->use_sg;
#if LINUX_VERSION_CODE >= 0x020400
            scp->SCp.Status = GDTH_MAP_SG;
            scp->SCp.Message = PCI_DMA_BIDIRECTIONAL; 
            sgcnt = pci_map_sg(ha->pdev,sl,scp->use_sg,scp->SCp.Message);
            for (i=0; i<sgcnt; ++i,++sl) {
                cmdp->u.raw.sg_lst[i].sg_ptr = sg_dma_address(sl);
                cmdp->u.raw.sg_lst[i].sg_len = sg_dma_len(sl);
            }
#else
            for (i=0; i<sgcnt; ++i,++sl) {
Linus Torvalds's avatar
Linus Torvalds committed
3114 3115 3116
                cmdp->u.raw.sg_lst[i].sg_ptr = virt_to_bus(sl->address);
                cmdp->u.raw.sg_lst[i].sg_len = (ulong32)sl->length;
            }
Alan Cox's avatar
Alan Cox committed
3117
#endif
Linus Torvalds's avatar
Linus Torvalds committed
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128
            cmdp->u.raw.sg_ranz = (ulong32)i;

#ifdef GDTH_STATISTICS
            if (max_sg < (ulong32)i) {
                max_sg = (ulong32)i;
                TRACE3(("GDT: max_sg = %d\n",i));
            }
#endif
            if (i<GDTH_MAXSG)
                cmdp->u.raw.sg_lst[i].sg_len = 0;
        } else {
Alan Cox's avatar
Alan Cox committed
3129 3130 3131 3132 3133 3134 3135 3136 3137
#if LINUX_VERSION_CODE >= 0x020400
            scp->SCp.Status = GDTH_MAP_SINGLE;
            scp->SCp.Message = PCI_DMA_BIDIRECTIONAL; 
            phys_addr = pci_map_single(ha->pdev,scp->request_buffer,
                                       scp->request_bufflen,scp->SCp.Message);
            scp->SCp.dma_handle = phys_addr;
#else
            phys_addr = virt_to_bus(scp->request_buffer);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
3138 3139 3140
            if (ha->raw_feat & SCATTER_GATHER) {
                cmdp->u.raw.sdata  = 0xffffffff;
                cmdp->u.raw.sg_ranz= 1;
Alan Cox's avatar
Alan Cox committed
3141
                cmdp->u.raw.sg_lst[0].sg_ptr = phys_addr;
Linus Torvalds's avatar
Linus Torvalds committed
3142 3143 3144
                cmdp->u.raw.sg_lst[0].sg_len = scp->request_bufflen;
                cmdp->u.raw.sg_lst[1].sg_len = 0;
            } else {
Alan Cox's avatar
Alan Cox committed
3145
                cmdp->u.raw.sdata  = phys_addr;
Linus Torvalds's avatar
Linus Torvalds committed
3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
                cmdp->u.raw.sg_ranz= 0;
            }
        }
        TRACE(("raw cmd: addr. %x sganz %x sgptr0 %x sglen0 %x\n",
               cmdp->u.raw.sdata,cmdp->u.raw.sg_ranz,
               cmdp->u.raw.sg_lst[0].sg_ptr,
               cmdp->u.raw.sg_lst[0].sg_len));

        /* evaluate command size */
        ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) +
            (ushort)cmdp->u.raw.sg_ranz * sizeof(gdth_sg_str);
    }
    /* check space */
    if (ha->cmd_len & 3)
        ha->cmd_len += (4 - (ha->cmd_len & 3));

    if (ha->cmd_cnt > 0) {
        if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
            ha->ic_all_size) {
            TRACE2(("gdth_fill_raw() DPMEM overflow\n"));
            ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
            return 0;
        }
    }

    /* copy command */
    gdth_copy_command(hanum);
    return cmd_index;
}

static int gdth_special_cmd(int hanum,Scsi_Cmnd *scp)
{
    register gdth_ha_str *ha;
    register gdth_cmd_str *cmdp;
    int cmd_index;

    ha  = HADATA(gdth_ctr_tab[hanum]);
    cmdp= ha->pccb;
    TRACE2(("gdth_special_cmd(): "));

    if (ha->type==GDT_EISA && ha->cmd_cnt>0) 
        return 0;

    memcpy( cmdp, scp->request_buffer, sizeof(gdth_cmd_str));
    cmdp->RequestBuffer = scp;

    /* search free command index */
    if (!(cmd_index=gdth_get_cmd_index(hanum))) {
        TRACE(("GDT: No free command index found\n"));
        return 0;
    }

    /* if it's the first command, set command semaphore */
    if (ha->cmd_cnt == 0)
       gdth_set_sema0(hanum);

    /* evaluate command size, check space */
    if (cmdp->OpCode == GDT_IOCTL) {
        TRACE2(("IOCTL\n"));
        ha->cmd_len = 
            GDTOFFSOF(gdth_cmd_str,u.ioctl.p_param) + sizeof(ulong32);
    } else if (cmdp->Service == CACHESERVICE) {
        TRACE2(("cache command %d\n",cmdp->OpCode));
        ha->cmd_len = 
            GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) + sizeof(gdth_sg_str);
    } else if (cmdp->Service == SCSIRAWSERVICE) {
        TRACE2(("raw command %d/%d\n",cmdp->OpCode,cmdp->u.raw.cmd[0]));
        ha->cmd_len = 
            GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) + sizeof(gdth_sg_str);
    }

    if (ha->cmd_len & 3)
        ha->cmd_len += (4 - (ha->cmd_len & 3));

    if (ha->cmd_cnt > 0) {
        if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
            ha->ic_all_size) {
            TRACE2(("gdth_special_cmd() DPMEM overflow\n"));
            ha->cmd_tab[cmd_index-2].cmnd = UNUSED_CMND;
            return 0;
        }
    }

    /* copy command */
    gdth_copy_command(hanum);
    return cmd_index;
}    


/* Controller event handling functions */
static gdth_evt_str *gdth_store_event(gdth_ha_str *ha, ushort source, 
                                      ushort idx, gdth_evt_data *evt)
{
    gdth_evt_str *e;
    struct timeval tv;

    /* no GDTH_LOCK_HA() ! */
    TRACE2(("gdth_store_event() source %d idx %d\n", source, idx));
    if (source == 0)                        /* no source -> no event */
        return 0;

    if (ebuffer[elastidx].event_source == source &&
        ebuffer[elastidx].event_idx == idx &&
Linus Torvalds's avatar
Linus Torvalds committed
3249 3250 3251 3252 3253 3254
        ((evt->size != 0 && ebuffer[elastidx].event_data.size != 0 &&
            !memcmp((char *)&ebuffer[elastidx].event_data.eu,
            (char *)&evt->eu, evt->size)) ||
        (evt->size == 0 && ebuffer[elastidx].event_data.size == 0 &&
            !strcmp((char *)&ebuffer[elastidx].event_data.event_string,
            (char *)&evt->event_string)))) { 
Linus Torvalds's avatar
Linus Torvalds committed
3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276
        e = &ebuffer[elastidx];
        do_gettimeofday(&tv);
        e->last_stamp = tv.tv_sec;
        ++e->same_count;
    } else {
        if (ebuffer[elastidx].event_source != 0) {  /* entry not free ? */
            ++elastidx;
            if (elastidx == MAX_EVENTS)
                elastidx = 0;
            if (elastidx == eoldidx) {              /* reached mark ? */
                ++eoldidx;
                if (eoldidx == MAX_EVENTS)
                    eoldidx = 0;
            }
        }
        e = &ebuffer[elastidx];
        e->event_source = source;
        e->event_idx = idx;
        do_gettimeofday(&tv);
        e->first_stamp = e->last_stamp = tv.tv_sec;
        e->same_count = 1;
        e->event_data = *evt;
Linus Torvalds's avatar
Linus Torvalds committed
3277
        e->application = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356
    }
    return e;
}

static int gdth_read_event(gdth_ha_str *ha, int handle, gdth_evt_str *estr)
{
    gdth_evt_str *e;
    int eindex;
    ulong flags;

    TRACE2(("gdth_read_event() handle %d\n", handle));
    GDTH_LOCK_HA(ha, flags);
    if (handle == -1)
        eindex = eoldidx;
    else
        eindex = handle;
    estr->event_source = 0;

    if (eindex >= MAX_EVENTS) {
        GDTH_UNLOCK_HA(ha, flags);
        return eindex;
    }
    e = &ebuffer[eindex];
    if (e->event_source != 0) {
        if (eindex != elastidx) {
            if (++eindex == MAX_EVENTS)
                eindex = 0;
        } else {
            eindex = -1;
        }
        memcpy(estr, e, sizeof(gdth_evt_str));
    }
    GDTH_UNLOCK_HA(ha, flags);
    return eindex;
}

static void gdth_readapp_event(gdth_ha_str *ha,
                               unchar application, gdth_evt_str *estr)
{
    gdth_evt_str *e;
    int eindex;
    ulong flags;
    unchar found = FALSE;

    TRACE2(("gdth_readapp_event() app. %d\n", application));
    GDTH_LOCK_HA(ha, flags);
    eindex = eoldidx;
    for (;;) {
        e = &ebuffer[eindex];
        if (e->event_source == 0)
            break;
        if ((e->application & application) == 0) {
            e->application |= application;
            found = TRUE;
            break;
        }
        if (eindex == elastidx)
            break;
        if (++eindex == MAX_EVENTS)
            eindex = 0;
    }
    if (found)
        memcpy(estr, e, sizeof(gdth_evt_str));
    else
        estr->event_source = 0;
    GDTH_UNLOCK_HA(ha, flags);
}

static void gdth_clear_events()
{
    TRACE(("gdth_clear_events()"));

    eoldidx = elastidx = 0;
    ebuffer[0].event_source = 0;
}


/* SCSI interface functions */

Andrew Morton's avatar
Andrew Morton committed
3357
static irqreturn_t gdth_interrupt(int irq,void *dev_id,struct pt_regs *regs)
Linus Torvalds's avatar
Linus Torvalds committed
3358 3359 3360 3361 3362 3363
{
    register gdth_ha_str *ha;
    gdt6m_dpram_str *dp6m_ptr;
    gdt6_dpram_str *dp6_ptr;
    gdt2_dpram_str *dp2_ptr;
    Scsi_Cmnd *scp;
Linus Torvalds's avatar
Linus Torvalds committed
3364
    int hanum, rval, i;
Linus Torvalds's avatar
Linus Torvalds committed
3365
    unchar IStatus;
Linus Torvalds's avatar
Linus Torvalds committed
3366
    ushort Service;
Linus Torvalds's avatar
Linus Torvalds committed
3367 3368 3369 3370 3371 3372 3373
    ulong flags = 0;

    TRACE(("gdth_interrupt() IRQ %d\n",irq));

    /* if polling and not from gdth_wait() -> return */
    if (gdth_polling) {
        if (!gdth_from_wait) {
Andrew Morton's avatar
Andrew Morton committed
3374
            return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
3375 3376 3377 3378 3379 3380 3381 3382 3383
        }
    }

    if (!gdth_polling)
        GDTH_LOCK_HA((gdth_ha_str *)dev_id,flags);
    wait_index = 0;

    /* search controller */
    if ((hanum = gdth_get_status(&IStatus,irq)) == -1) {
Linus Torvalds's avatar
Linus Torvalds committed
3384
        /* spurious interrupt */
Linus Torvalds's avatar
Linus Torvalds committed
3385 3386
        if (!gdth_polling)
            GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
Andrew Morton's avatar
Andrew Morton committed
3387
        return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397
    }

#ifdef GDTH_STATISTICS
    ++act_ints;
#endif
    
    ha = HADATA(gdth_ctr_tab[hanum]);
    if (ha->type == GDT_EISA) {
        if (IStatus & 0x80) {                       /* error flag */
            IStatus &= ~0x80;
Linus Torvalds's avatar
Linus Torvalds committed
3398 3399
            ha->status = inw(ha->bmic + MAILBOXREG+8);
            TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
Linus Torvalds's avatar
Linus Torvalds committed
3400
        } else                                      /* no error */
Linus Torvalds's avatar
Linus Torvalds committed
3401 3402 3403 3404 3405
            ha->status = S_OK;
        ha->info = inl(ha->bmic + MAILBOXREG+12);
        ha->service = inw(ha->bmic + MAILBOXREG+10);
        ha->info2 = inl(ha->bmic + MAILBOXREG+4);

Linus Torvalds's avatar
Linus Torvalds committed
3406 3407 3408 3409 3410 3411
        outb(0xff, ha->bmic + EDOORREG);            /* acknowledge interrupt */
        outb(0x00, ha->bmic + SEMA1REG);            /* reset status semaphore */
    } else if (ha->type == GDT_ISA) {
        dp2_ptr = (gdt2_dpram_str *)ha->brd;
        if (IStatus & 0x80) {                       /* error flag */
            IStatus &= ~0x80;
Linus Torvalds's avatar
Linus Torvalds committed
3412 3413
            ha->status = gdth_readw(&dp2_ptr->u.ic.Status);
            TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
Linus Torvalds's avatar
Linus Torvalds committed
3414
        } else                                      /* no error */
Linus Torvalds's avatar
Linus Torvalds committed
3415 3416 3417 3418 3419
            ha->status = S_OK;
        ha->info = gdth_readl(&dp2_ptr->u.ic.Info[0]);
        ha->service = gdth_readw(&dp2_ptr->u.ic.Service);
        ha->info2 = gdth_readl(&dp2_ptr->u.ic.Info[1]);

Linus Torvalds's avatar
Linus Torvalds committed
3420 3421 3422 3423 3424 3425 3426
        gdth_writeb(0xff, &dp2_ptr->io.irqdel);     /* acknowledge interrupt */
        gdth_writeb(0, &dp2_ptr->u.ic.Cmd_Index);   /* reset command index */
        gdth_writeb(0, &dp2_ptr->io.Sema1);         /* reset status semaphore */
    } else if (ha->type == GDT_PCI) {
        dp6_ptr = (gdt6_dpram_str *)ha->brd;
        if (IStatus & 0x80) {                       /* error flag */
            IStatus &= ~0x80;
Linus Torvalds's avatar
Linus Torvalds committed
3427 3428
            ha->status = gdth_readw(&dp6_ptr->u.ic.Status);
            TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
Linus Torvalds's avatar
Linus Torvalds committed
3429
        } else                                      /* no error */
Linus Torvalds's avatar
Linus Torvalds committed
3430 3431 3432 3433 3434
            ha->status = S_OK;
        ha->info = gdth_readl(&dp6_ptr->u.ic.Info[0]);
        ha->service = gdth_readw(&dp6_ptr->u.ic.Service);
        ha->info2 = gdth_readl(&dp6_ptr->u.ic.Info[1]);

Linus Torvalds's avatar
Linus Torvalds committed
3435 3436 3437 3438 3439 3440
        gdth_writeb(0xff, &dp6_ptr->io.irqdel);     /* acknowledge interrupt */
        gdth_writeb(0, &dp6_ptr->u.ic.Cmd_Index);   /* reset command index */
        gdth_writeb(0, &dp6_ptr->io.Sema1);         /* reset status semaphore */
    } else if (ha->type == GDT_PCINEW) {
        if (IStatus & 0x80) {                       /* error flag */
            IStatus &= ~0x80;
Linus Torvalds's avatar
Linus Torvalds committed
3441 3442
            ha->status = inw(PTR2USHORT(&ha->plx->status));
            TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
Linus Torvalds's avatar
Linus Torvalds committed
3443
        } else
Linus Torvalds's avatar
Linus Torvalds committed
3444 3445 3446 3447
            ha->status = S_OK;
        ha->info = inl(PTR2USHORT(&ha->plx->info[0]));
        ha->service = inw(PTR2USHORT(&ha->plx->service));
        ha->info2 = inl(PTR2USHORT(&ha->plx->info[1]));
Linus Torvalds's avatar
Linus Torvalds committed
3448 3449 3450 3451 3452 3453 3454

        outb(0xff, PTR2USHORT(&ha->plx->edoor_reg)); 
        outb(0x00, PTR2USHORT(&ha->plx->sema1_reg)); 
    } else if (ha->type == GDT_PCIMPR) {
        dp6m_ptr = (gdt6m_dpram_str *)ha->brd;
        if (IStatus & 0x80) {                       /* error flag */
            IStatus &= ~0x80;
Linus Torvalds's avatar
Linus Torvalds committed
3455 3456
            ha->status = gdth_readw(&dp6m_ptr->i960r.status);
            TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,ha->status));
Linus Torvalds's avatar
Linus Torvalds committed
3457
        } else                                      /* no error */
Linus Torvalds's avatar
Linus Torvalds committed
3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476
            ha->status = S_OK;
        ha->info = gdth_readl(&dp6m_ptr->i960r.info[0]);
        ha->service = gdth_readw(&dp6m_ptr->i960r.service);
        ha->info2 = gdth_readl(&dp6m_ptr->i960r.info[1]);

        /* event string */
        if (IStatus == ASYNCINDEX) {
            if (ha->service != SCREENSERVICE &&
                (ha->fw_vers & 0xff) >= 0x1a) {
                ha->dvr.severity =   
                    gdth_readb(&((gdt6m_dpram_str *)ha->brd)->i960r.severity);
                for (i = 0; i < 256; ++i) {
                    ha->dvr.event_string[i] = gdth_readb
                        (&((gdt6m_dpram_str *)ha->brd)->i960r.evt_str[i]);
                    if (ha->dvr.event_string[i] == 0)
                        break;
                }
            }
        }
Linus Torvalds's avatar
Linus Torvalds committed
3477 3478 3479 3480 3481 3482
        gdth_writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
        gdth_writeb(0, &dp6m_ptr->i960r.sema1_reg);
    } else {
        TRACE2(("gdth_interrupt() unknown controller type\n"));
        if (!gdth_polling)
            GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
Andrew Morton's avatar
Andrew Morton committed
3483
        return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
3484 3485 3486
    }

    TRACE(("gdth_interrupt() index %d stat %d info %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
3487
           IStatus,ha->status,ha->info));
Linus Torvalds's avatar
Linus Torvalds committed
3488 3489 3490 3491 3492 3493 3494 3495

    if (gdth_from_wait) {
        wait_hanum = hanum;
        wait_index = (int)IStatus;
    }

    if (IStatus == ASYNCINDEX) {
        TRACE2(("gdth_interrupt() async. event\n"));
Linus Torvalds's avatar
Linus Torvalds committed
3496
        gdth_async_event(hanum);
Linus Torvalds's avatar
Linus Torvalds committed
3497 3498 3499
        if (!gdth_polling)
            GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
        gdth_next(hanum);
Andrew Morton's avatar
Andrew Morton committed
3500
        return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
3501 3502 3503 3504
    } 

    if (IStatus == SPEZINDEX) {
        TRACE2(("Service unknown or not initialized !\n"));
Linus Torvalds's avatar
Linus Torvalds committed
3505 3506 3507
        ha->dvr.size = sizeof(ha->dvr.eu.driver);
        ha->dvr.eu.driver.ionode = hanum;
        gdth_store_event(ha, ES_DRIVER, 4, &ha->dvr);
Linus Torvalds's avatar
Linus Torvalds committed
3508 3509
        if (!gdth_polling)
            GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
Andrew Morton's avatar
Andrew Morton committed
3510
        return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
3511 3512 3513 3514 3515 3516
    }
    scp     = ha->cmd_tab[IStatus-2].cmnd;
    Service = ha->cmd_tab[IStatus-2].service;
    ha->cmd_tab[IStatus-2].cmnd = UNUSED_CMND;
    if (scp == UNUSED_CMND) {
        TRACE2(("gdth_interrupt() index to unused command (%d)\n",IStatus));
Linus Torvalds's avatar
Linus Torvalds committed
3517 3518 3519 3520
        ha->dvr.size = sizeof(ha->dvr.eu.driver);
        ha->dvr.eu.driver.ionode = hanum;
        ha->dvr.eu.driver.index = IStatus;
        gdth_store_event(ha, ES_DRIVER, 1, &ha->dvr);
Linus Torvalds's avatar
Linus Torvalds committed
3521 3522
        if (!gdth_polling)
            GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
Andrew Morton's avatar
Andrew Morton committed
3523
        return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
3524 3525 3526 3527 3528
    }
    if (scp == INTERNAL_CMND) {
        TRACE(("gdth_interrupt() answer to internal command\n"));
        if (!gdth_polling)
            GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
Andrew Morton's avatar
Andrew Morton committed
3529
        return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
3530 3531 3532 3533 3534 3535 3536 3537 3538
    }

    TRACE(("gdth_interrupt() sync. status\n"));
    rval = gdth_sync_event(hanum,Service,IStatus,scp);
    if (!gdth_polling)
        GDTH_UNLOCK_HA((gdth_ha_str *)dev_id,flags);
    if (rval == 2) {
        gdth_putq(hanum,scp,scp->SCp.this_residual);
    } else if (rval == 1) {
Alan Cox's avatar
Alan Cox committed
3539
#if LINUX_VERSION_CODE >= 0x02053C
3540
        GDTH_LOCK_SCSI_DONE(scp->device->host, flags);
Linus Torvalds's avatar
Linus Torvalds committed
3541
        scp->scsi_done(scp);
Alan Cox's avatar
Alan Cox committed
3542 3543 3544 3545 3546 3547 3548 3549 3550 3551
        GDTH_UNLOCK_SCSI_DONE(scp->device->host, flags);
#elif LINUX_VERSION_CODE >= 0x020503
        GDTH_LOCK_SCSI_DONE(scp->host, flags);
        scp->scsi_done(scp);
        GDTH_UNLOCK_SCSI_DONE(scp->host, flags);
#else
        GDTH_LOCK_SCSI_DONE(flags);
        scp->scsi_done(scp);
        GDTH_UNLOCK_SCSI_DONE(flags);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
3552 3553
    }
    gdth_next(hanum);
Andrew Morton's avatar
Andrew Morton committed
3554
    return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
3555 3556 3557 3558 3559 3560 3561
}

static int gdth_sync_event(int hanum,int service,unchar index,Scsi_Cmnd *scp)
{
    register gdth_ha_str *ha;
    gdth_msg_str *msg;
    gdth_cmd_str *cmdp;
Alan Cox's avatar
Alan Cox committed
3562
    unchar b, t;
Linus Torvalds's avatar
Linus Torvalds committed
3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589

    ha   = HADATA(gdth_ctr_tab[hanum]);
    cmdp = ha->pccb;
    TRACE(("gdth_sync_event() serv %d status %d\n",
           service,ha->status));

    if (service == SCREENSERVICE) {
        msg  = (gdth_msg_str *)ha->pscratch;
        ha->scratch_busy = FALSE;
        TRACE(("len: %d, answer: %d, ext: %d, alen: %d\n",
               msg->msg_len,msg->msg_answer,msg->msg_ext,msg->msg_alen));
        if (msg->msg_len)
            if (!(msg->msg_answer && msg->msg_ext)) {
                msg->msg_text[msg->msg_len] = '\0';
                printk("%s",msg->msg_text);
            }

        if (msg->msg_ext && !msg->msg_answer) {
            while (gdth_test_busy(hanum))
                gdth_delay(0);
            cmdp->Service       = SCREENSERVICE;
            cmdp->RequestBuffer = SCREEN_CMND;
            gdth_get_cmd_index(hanum);
            gdth_set_sema0(hanum);
            cmdp->OpCode        = GDT_READ;
            cmdp->BoardNode     = LOCALBOARD;
            cmdp->u.screen.reserved  = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3590
            cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
Alan Cox's avatar
Alan Cox committed
3591
            cmdp->u.screen.su.msg.msg_addr  = ha->scratch_phys;
Linus Torvalds's avatar
Linus Torvalds committed
3592 3593
            ha->scratch_busy = TRUE;
            ha->cmd_offs_dpmem = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3594
            ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr) 
Linus Torvalds's avatar
Linus Torvalds committed
3595 3596 3597 3598 3599 3600 3601 3602
                + sizeof(ulong32);
            ha->cmd_cnt = 0;
            gdth_copy_command(hanum);
            gdth_release_event(hanum);
            return 0;
        }

        if (msg->msg_answer && msg->msg_alen) {
Linus Torvalds's avatar
Linus Torvalds committed
3603 3604 3605 3606 3607
            /* default answers (getchar() not possible) */
            if (msg->msg_alen == 1) {
                msg->msg_alen = 0;
                msg->msg_len = 1;
                msg->msg_text[0] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3608
            } else {
Linus Torvalds's avatar
Linus Torvalds committed
3609 3610 3611 3612
                msg->msg_alen -= 2;
                msg->msg_len = 2;
                msg->msg_text[0] = 1;
                msg->msg_text[1] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3613
            }
Linus Torvalds's avatar
Linus Torvalds committed
3614 3615
            msg->msg_ext    = 0;
            msg->msg_answer = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3616 3617 3618 3619 3620 3621 3622 3623 3624
            while (gdth_test_busy(hanum))
                gdth_delay(0);
            cmdp->Service       = SCREENSERVICE;
            cmdp->RequestBuffer = SCREEN_CMND;
            gdth_get_cmd_index(hanum);
            gdth_set_sema0(hanum);
            cmdp->OpCode        = GDT_WRITE;
            cmdp->BoardNode     = LOCALBOARD;
            cmdp->u.screen.reserved  = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3625
            cmdp->u.screen.su.msg.msg_handle= msg->msg_handle;
Alan Cox's avatar
Alan Cox committed
3626
            cmdp->u.screen.su.msg.msg_addr  = ha->scratch_phys;
Linus Torvalds's avatar
Linus Torvalds committed
3627 3628
            ha->scratch_busy = TRUE;
            ha->cmd_offs_dpmem = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3629
            ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr) 
Linus Torvalds's avatar
Linus Torvalds committed
3630 3631 3632 3633 3634 3635 3636 3637 3638
                + sizeof(ulong32);
            ha->cmd_cnt = 0;
            gdth_copy_command(hanum);
            gdth_release_event(hanum);
            return 0;
        }
        printk("\n");

    } else {
Alan Cox's avatar
Alan Cox committed
3639
#if LINUX_VERSION_CODE >= 0x02053C
3640
        b = virt_ctr ? NUMDATA(scp->device->host)->busnum : scp->device->channel;
Alan Cox's avatar
Alan Cox committed
3641 3642 3643 3644 3645
        t = scp->device->id;
#else
        b = virt_ctr ? NUMDATA(scp->host)->busnum : scp->channel;
        t = scp->target;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
3646
        if (scp->SCp.sent_command == -1 && b != ha->virt_bus) {
Alan Cox's avatar
Alan Cox committed
3647
            ha->raw[BUS_L2P(ha,b)].io_cnt[t]--;
Linus Torvalds's avatar
Linus Torvalds committed
3648 3649
        }
        /* cache or raw service */
Alan Cox's avatar
Alan Cox committed
3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663
        if (ha->status == S_BSY) {
            TRACE2(("Controller busy -> retry !\n"));
            if (scp->SCp.sent_command == GDT_MOUNT)
                scp->SCp.sent_command = GDT_CLUST_INFO;
            /* retry */
            return 2;
        }
#if LINUX_VERSION_CODE >= 0x020400
        if (scp->SCp.Status == GDTH_MAP_SG) 
            pci_unmap_sg(ha->pdev,scp->request_buffer,
                         scp->use_sg,scp->SCp.Message);
        else if (scp->SCp.Status == GDTH_MAP_SINGLE) 
            pci_unmap_single(ha->pdev,scp->SCp.dma_handle,
                         scp->request_bufflen,scp->SCp.Message);
3664
        if (scp->SCp.buffer) 
Andrew Morton's avatar
Andrew Morton committed
3665
            pci_unmap_single(ha->pdev,(dma_addr_t)scp->SCp.buffer,
3666
						16,PCI_DMA_FROMDEVICE);
Alan Cox's avatar
Alan Cox committed
3667
#endif
Linus Torvalds's avatar
Linus Torvalds committed
3668
        if (ha->status == S_OK) {
Linus Torvalds's avatar
Linus Torvalds committed
3669 3670 3671
            scp->SCp.Status = S_OK;
            scp->SCp.Message = ha->info;
            if (scp->SCp.sent_command != -1) {
Linus Torvalds's avatar
Linus Torvalds committed
3672
                TRACE2(("gdth_sync_event(): special cmd 0x%x OK\n",
Linus Torvalds's avatar
Linus Torvalds committed
3673 3674 3675
                        scp->SCp.sent_command));
                /* special commands GDT_CLUST_INFO/GDT_MOUNT ? */
                if (scp->SCp.sent_command == GDT_CLUST_INFO) {
Alan Cox's avatar
Alan Cox committed
3676 3677
                    ha->hdr[t].cluster_type = (unchar)ha->info;
                    if (!(ha->hdr[t].cluster_type & 
Linus Torvalds's avatar
Linus Torvalds committed
3678 3679 3680
                        CLUSTER_MOUNTED)) {
                        /* NOT MOUNTED -> MOUNT */
                        scp->SCp.sent_command = GDT_MOUNT;
Alan Cox's avatar
Alan Cox committed
3681
                        if (ha->hdr[t].cluster_type & 
Linus Torvalds's avatar
Linus Torvalds committed
3682 3683 3684 3685 3686 3687 3688 3689 3690
                            CLUSTER_RESERVED) {
                            /* cluster drive RESERVED (on the other node) */
                            scp->SCp.phase = -2;      /* reservation conflict */
                        }
                    } else {
                        scp->SCp.sent_command = -1;
                    }
                } else {
                    if (scp->SCp.sent_command == GDT_MOUNT) {
Alan Cox's avatar
Alan Cox committed
3691 3692
                        ha->hdr[t].cluster_type |= CLUSTER_MOUNTED;
                        ha->hdr[t].media_changed = TRUE;
Linus Torvalds's avatar
Linus Torvalds committed
3693
                    } else if (scp->SCp.sent_command == GDT_UNMOUNT) {
Alan Cox's avatar
Alan Cox committed
3694 3695
                        ha->hdr[t].cluster_type &= ~CLUSTER_MOUNTED;
                        ha->hdr[t].media_changed = TRUE;
Linus Torvalds's avatar
Linus Torvalds committed
3696 3697 3698 3699
                    } 
                    scp->SCp.sent_command = -1;
                }
                /* retry */
Linus Torvalds's avatar
Linus Torvalds committed
3700 3701
                scp->SCp.this_residual = HIGH_PRI;
                return 2;
Linus Torvalds's avatar
Linus Torvalds committed
3702 3703 3704
            } else {
                /* RESERVE/RELEASE ? */
                if (scp->cmnd[0] == RESERVE) {
Alan Cox's avatar
Alan Cox committed
3705
                    ha->hdr[t].cluster_type |= CLUSTER_RESERVED;
Linus Torvalds's avatar
Linus Torvalds committed
3706
                } else if (scp->cmnd[0] == RELEASE) {
Alan Cox's avatar
Alan Cox committed
3707
                    ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
Linus Torvalds's avatar
Linus Torvalds committed
3708 3709 3710
                }           
                scp->result = DID_OK << 16;
                scp->sense_buffer[0] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3711 3712
            }
        } else {
Linus Torvalds's avatar
Linus Torvalds committed
3713 3714 3715 3716
            scp->SCp.Status = ha->status;
            scp->SCp.Message = ha->info;

            if (scp->SCp.sent_command != -1) {
Linus Torvalds's avatar
Linus Torvalds committed
3717
                TRACE2(("gdth_sync_event(): special cmd 0x%x error 0x%x\n",
Linus Torvalds's avatar
Linus Torvalds committed
3718 3719 3720 3721 3722 3723 3724 3725
                        scp->SCp.sent_command, ha->status));
                if (scp->SCp.sent_command == GDT_SCAN_START ||
                    scp->SCp.sent_command == GDT_SCAN_END) {
                    scp->SCp.sent_command = -1;
                    /* retry */
                    scp->SCp.this_residual = HIGH_PRI;
                    return 2;
                }
Linus Torvalds's avatar
Linus Torvalds committed
3726 3727 3728 3729
                memset((char*)scp->sense_buffer,0,16);
                scp->sense_buffer[0] = 0x70;
                scp->sense_buffer[2] = NOT_READY;
                scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
Linus Torvalds's avatar
Linus Torvalds committed
3730 3731
            } else if (service == CACHESERVICE) {
                if (ha->status == S_CACHE_UNKNOWN &&
Alan Cox's avatar
Alan Cox committed
3732
                    (ha->hdr[t].cluster_type & 
Linus Torvalds's avatar
Linus Torvalds committed
3733 3734
                     CLUSTER_RESERVE_STATE) == CLUSTER_RESERVE_STATE) {
                    /* bus reset -> force GDT_CLUST_INFO */
Alan Cox's avatar
Alan Cox committed
3735
                    ha->hdr[t].cluster_type &= ~CLUSTER_RESERVED;
Linus Torvalds's avatar
Linus Torvalds committed
3736 3737
                }
                memset((char*)scp->sense_buffer,0,16);
3738 3739 3740 3741 3742 3743 3744
                if (ha->status == (ushort)S_CACHE_RESERV) {
                    scp->result = (DID_OK << 16) | (RESERVATION_CONFLICT << 1);
                } else {
                    scp->sense_buffer[0] = 0x70;
                    scp->sense_buffer[2] = NOT_READY;
                    scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
                }
Linus Torvalds's avatar
Linus Torvalds committed
3745
#if LINUX_VERSION_CODE >= 0x010300
Linus Torvalds's avatar
Linus Torvalds committed
3746
                if (scp->done != gdth_scsi_done)
Linus Torvalds's avatar
Linus Torvalds committed
3747
#endif  
Linus Torvalds's avatar
Linus Torvalds committed
3748
                {
Linus Torvalds's avatar
Linus Torvalds committed
3749 3750 3751 3752 3753
                    ha->dvr.size = sizeof(ha->dvr.eu.sync);
                    ha->dvr.eu.sync.ionode  = hanum;
                    ha->dvr.eu.sync.service = service;
                    ha->dvr.eu.sync.status  = ha->status;
                    ha->dvr.eu.sync.info    = ha->info;
Alan Cox's avatar
Alan Cox committed
3754
                    ha->dvr.eu.sync.hostdrive = t;
Linus Torvalds's avatar
Linus Torvalds committed
3755
                    if (ha->status >= 0x8000)
Linus Torvalds's avatar
Linus Torvalds committed
3756
                        gdth_store_event(ha, ES_SYNC, 0, &ha->dvr);
Linus Torvalds's avatar
Linus Torvalds committed
3757
                    else
Linus Torvalds's avatar
Linus Torvalds committed
3758
                        gdth_store_event(ha, ES_SYNC, service, &ha->dvr);
Linus Torvalds's avatar
Linus Torvalds committed
3759 3760
                }
            } else {
Linus Torvalds's avatar
Linus Torvalds committed
3761 3762
                /* sense buffer filled from controller firmware (DMA) */
                if (ha->status != S_RAW_SCSI || ha->info >= 0x100) {
Linus Torvalds's avatar
Linus Torvalds committed
3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924
                    scp->result = DID_BAD_TARGET << 16;
                } else {
                    scp->result = (DID_OK << 16) | ha->info;
                }
            }
        }
        if (!scp->SCp.have_data_in)
            scp->SCp.have_data_in++;
        else 
            return 1;
    }

    return 0;
}

static char *async_cache_tab[] = {
/* 0*/  "\011\000\002\002\002\004\002\006\004"
        "GDT HA %u, service %u, async. status %u/%lu unknown",
/* 1*/  "\011\000\002\002\002\004\002\006\004"
        "GDT HA %u, service %u, async. status %u/%lu unknown",
/* 2*/  "\005\000\002\006\004"
        "GDT HA %u, Host Drive %lu not ready",
/* 3*/  "\005\000\002\006\004"
        "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",
/* 4*/  "\005\000\002\006\004"
        "GDT HA %u, mirror update on Host Drive %lu failed",
/* 5*/  "\005\000\002\006\004"
        "GDT HA %u, Mirror Drive %lu failed",
/* 6*/  "\005\000\002\006\004"
        "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",
/* 7*/  "\005\000\002\006\004"
        "GDT HA %u, Host Drive %lu write protected",
/* 8*/  "\005\000\002\006\004"
        "GDT HA %u, media changed in Host Drive %lu",
/* 9*/  "\005\000\002\006\004"
        "GDT HA %u, Host Drive %lu is offline",
/*10*/  "\005\000\002\006\004"
        "GDT HA %u, media change of Mirror Drive %lu",
/*11*/  "\005\000\002\006\004"
        "GDT HA %u, Mirror Drive %lu is write protected",
/*12*/  "\005\000\002\006\004"
        "GDT HA %u, general error on Host Drive %lu. Please check the devices of this drive!",
/*13*/  "\007\000\002\006\002\010\002"
        "GDT HA %u, Array Drive %u: Cache Drive %u failed",
/*14*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: FAIL state entered",
/*15*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: error",
/*16*/  "\007\000\002\006\002\010\002"
        "GDT HA %u, Array Drive %u: failed drive replaced by Cache Drive %u",
/*17*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: parity build failed",
/*18*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: drive rebuild failed",
/*19*/  "\005\000\002\010\002"
        "GDT HA %u, Test of Hot Fix %u failed",
/*20*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: drive build finished successfully",
/*21*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: drive rebuild finished successfully",
/*22*/  "\007\000\002\006\002\010\002"
        "GDT HA %u, Array Drive %u: Hot Fix %u activated",
/*23*/  "\005\000\002\006\002"
        "GDT HA %u, Host Drive %u: processing of i/o aborted due to serious drive error",
/*24*/  "\005\000\002\010\002"
        "GDT HA %u, mirror update on Cache Drive %u completed",
/*25*/  "\005\000\002\010\002"
        "GDT HA %u, mirror update on Cache Drive %lu failed",
/*26*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: drive rebuild started",
/*27*/  "\005\000\002\012\001"
        "GDT HA %u, Fault bus %u: SHELF OK detected",
/*28*/  "\005\000\002\012\001"
        "GDT HA %u, Fault bus %u: SHELF not OK detected",
/*29*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug started",
/*30*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: new disk detected",
/*31*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: old disk detected",
/*32*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: plugging an active disk is illegal",
/*33*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: illegal device detected",
/*34*/  "\011\000\002\012\001\013\001\006\004"
        "GDT HA %u, Fault bus %u, ID %u: insufficient disk capacity (%lu MB required)",
/*35*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: disk write protected",
/*36*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: disk not available",
/*37*/  "\007\000\002\012\001\006\004"
        "GDT HA %u, Fault bus %u: swap detected (%lu)",
/*38*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug finished successfully",
/*39*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted due to user Hot Plug",
/*40*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted",
/*41*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug for Hot Fix started",
/*42*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: drive build started",
/*43*/  "\003\000\002"
        "GDT HA %u, DRAM parity error detected",
/*44*/  "\005\000\002\006\002"
        "GDT HA %u, Mirror Drive %u: update started",
/*45*/  "\007\000\002\006\002\010\002"
        "GDT HA %u, Mirror Drive %u: Hot Fix %u activated",
/*46*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: no matching Pool Hot Fix Drive available",
/*47*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: Pool Hot Fix Drive available",
/*48*/  "\005\000\002\006\002"
        "GDT HA %u, Mirror Drive %u: no matching Pool Hot Fix Drive available",
/*49*/  "\005\000\002\006\002"
        "GDT HA %u, Mirror Drive %u: Pool Hot Fix Drive available",
/*50*/  "\007\000\002\012\001\013\001"
        "GDT HA %u, SCSI bus %u, ID %u: IGNORE_WIDE_RESIDUE message received",
/*51*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: expand started",
/*52*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: expand finished successfully",
/*53*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: expand failed",
/*54*/  "\003\000\002"
        "GDT HA %u, CPU temperature critical",
/*55*/  "\003\000\002"
        "GDT HA %u, CPU temperature OK",
/*56*/  "\005\000\002\006\004"
        "GDT HA %u, Host drive %lu created",
/*57*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: expand restarted",
/*58*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: expand stopped",
/*59*/  "\005\000\002\010\002"
        "GDT HA %u, Mirror Drive %u: drive build quited",
/*60*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: parity build quited",
/*61*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: drive rebuild quited",
/*62*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: parity verify started",
/*63*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: parity verify done",
/*64*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: parity verify failed",
/*65*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: parity error detected",
/*66*/  "\005\000\002\006\002"
        "GDT HA %u, Array Drive %u: parity verify quited",
/*67*/  "\005\000\002\006\002"
        "GDT HA %u, Host Drive %u reserved",
/*68*/  "\005\000\002\006\002"
        "GDT HA %u, Host Drive %u mounted and released",
/*69*/  "\005\000\002\006\002"
        "GDT HA %u, Host Drive %u released",
/*70*/  "\003\000\002"
        "GDT HA %u, DRAM error detected and corrected with ECC",
/*71*/  "\003\000\002"
        "GDT HA %u, Uncorrectable DRAM error detected with ECC",
/*72*/  "\011\000\002\012\001\013\001\014\001"
        "GDT HA %u, SCSI bus %u, ID %u, LUN %u: reassigning block",
Linus Torvalds's avatar
Linus Torvalds committed
3925 3926 3927 3928 3929 3930
/*73*/  "\005\000\002\006\002"
        "GDT HA %u, Host drive %u resetted locally",
/*74*/  "\005\000\002\006\002"
        "GDT HA %u, Host drive %u resetted remotely",
/*75*/  "\003\000\002"
        "GDT HA %u, async. status 75 unknown",
Linus Torvalds's avatar
Linus Torvalds committed
3931 3932 3933
};


Linus Torvalds's avatar
Linus Torvalds committed
3934
static int gdth_async_event(int hanum)
Linus Torvalds's avatar
Linus Torvalds committed
3935 3936 3937 3938 3939 3940 3941 3942 3943 3944
{
    gdth_ha_str *ha;
    gdth_msg_str *msg;
    gdth_cmd_str *cmdp;
    int cmd_index;

    ha  = HADATA(gdth_ctr_tab[hanum]);
    cmdp= ha->pccb;
    msg = (gdth_msg_str *)ha->pscratch;
    TRACE2(("gdth_async_event() ha %d serv %d\n",
Linus Torvalds's avatar
Linus Torvalds committed
3945
            hanum,ha->service));
Linus Torvalds's avatar
Linus Torvalds committed
3946

Linus Torvalds's avatar
Linus Torvalds committed
3947
    if (ha->service == SCREENSERVICE) {
Linus Torvalds's avatar
Linus Torvalds committed
3948 3949 3950 3951 3952 3953 3954 3955 3956 3957
        if (ha->status == MSG_REQUEST) {
            while (gdth_test_busy(hanum))
                gdth_delay(0);
            cmdp->Service       = SCREENSERVICE;
            cmdp->RequestBuffer = SCREEN_CMND;
            cmd_index = gdth_get_cmd_index(hanum);
            gdth_set_sema0(hanum);
            cmdp->OpCode        = GDT_READ;
            cmdp->BoardNode     = LOCALBOARD;
            cmdp->u.screen.reserved  = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3958
            cmdp->u.screen.su.msg.msg_handle= MSG_INV_HANDLE;
Alan Cox's avatar
Alan Cox committed
3959
            cmdp->u.screen.su.msg.msg_addr  = ha->scratch_phys;
Linus Torvalds's avatar
Linus Torvalds committed
3960 3961
            ha->scratch_busy = TRUE;
            ha->cmd_offs_dpmem = 0;
Linus Torvalds's avatar
Linus Torvalds committed
3962
            ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.su.msg.msg_addr) 
Linus Torvalds's avatar
Linus Torvalds committed
3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976
                + sizeof(ulong32);
            ha->cmd_cnt = 0;
            gdth_copy_command(hanum);
            if (ha->type == GDT_EISA)
                printk("[EISA slot %d] ",(ushort)ha->brd_phys);
            else if (ha->type == GDT_ISA)
                printk("[DPMEM 0x%4X] ",(ushort)ha->brd_phys);
            else 
                printk("[PCI %d/%d] ",(ushort)(ha->brd_phys>>8),
                       (ushort)((ha->brd_phys>>3)&0x1f));
            gdth_release_event(hanum);
        }

    } else {
Linus Torvalds's avatar
Linus Torvalds committed
3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997
        if (ha->type == GDT_PCIMPR && 
            (ha->fw_vers & 0xff) >= 0x1a) {
            ha->dvr.size = 0;
            ha->dvr.eu.async.ionode = hanum;
            ha->dvr.eu.async.status  = ha->status;
            /* severity and event_string already set! */
        } else {        
            ha->dvr.size = sizeof(ha->dvr.eu.async);
            ha->dvr.eu.async.ionode   = hanum;
            ha->dvr.eu.async.service = ha->service;
            ha->dvr.eu.async.status  = ha->status;
            ha->dvr.eu.async.info    = ha->info;
            *(ulong32 *)ha->dvr.eu.async.scsi_coord  = ha->info2;
        }
        gdth_store_event( ha, ES_ASYNC, ha->service, &ha->dvr );
        gdth_log_event( &ha->dvr, NULL );
    
        /* new host drive from expand? */
        if (ha->service == CACHESERVICE && ha->status == 56) {
            TRACE2(("gdth_async_event(): new host drive %d created\n",
                    (ushort)ha->info));
Linus Torvalds's avatar
Linus Torvalds committed
3998
            /* gdth_analyse_hdrive(hanum, (ushort)ha->info); */
Linus Torvalds's avatar
Linus Torvalds committed
3999
        }   
Linus Torvalds's avatar
Linus Torvalds committed
4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010
    }
    return 1;
}

static void gdth_log_event(gdth_evt_data *dvr, char *buffer)
{
    gdth_stackframe stack;
    char *f = NULL;
    int i,j;

    TRACE2(("gdth_log_event()\n"));
Linus Torvalds's avatar
Linus Torvalds committed
4011 4012 4013 4014 4015 4016 4017 4018
    if (dvr->size == 0) {
        if (buffer == NULL) {
            printk("Adapter %d: %s\n",dvr->eu.async.ionode,dvr->event_string); 
        } else {
            sprintf(buffer,"Adapter %d: %s\n",
                dvr->eu.async.ionode,dvr->event_string); 
        }
    } else if (dvr->eu.async.service == CACHESERVICE && 
Linus Torvalds's avatar
Linus Torvalds committed
4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088
        INDEX_OK(dvr->eu.async.status, async_cache_tab)) {
        TRACE2(("GDT: Async. event cache service, event no.: %d\n",
                dvr->eu.async.status));
        
        f = async_cache_tab[dvr->eu.async.status];
        
        /* i: parameter to push, j: stack element to fill */
        for (j=0,i=1; i < f[0]; i+=2) {
            switch (f[i+1]) {
              case 4:
                stack.b[j++] = *(ulong32*)&dvr->eu.stream[(int)f[i]];
                break;
              case 2:
                stack.b[j++] = *(ushort*)&dvr->eu.stream[(int)f[i]];
                break;
              case 1:
                stack.b[j++] = *(unchar*)&dvr->eu.stream[(int)f[i]];
                break;
              default:
                break;
            }
        }
        
        if (buffer == NULL) {
            printk(&f[(int)f[0]],stack); 
            printk("\n");
        } else {
            sprintf(buffer,&f[(int)f[0]],stack); 
        }

    } else {
        if (buffer == NULL) {
            printk("GDT HA %u, Unknown async. event service %d event no. %d\n",
                   dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
        } else {
            sprintf(buffer,"GDT HA %u, Unknown async. event service %d event no. %d",
                    dvr->eu.async.ionode,dvr->eu.async.service,dvr->eu.async.status);
        }
    }
}

#ifdef GDTH_STATISTICS
void gdth_timeout(ulong data)
{
    ulong32 i;
    Scsi_Cmnd *nscp;
    gdth_ha_str *ha;
    ulong flags;
    int hanum = 0;

    ha = HADATA(gdth_ctr_tab[hanum]);
    GDTH_LOCK_HA(ha, flags);

    for (act_stats=0,i=0; i<GDTH_MAXCMDS; ++i) 
        if (ha->cmd_tab[i].cmnd != UNUSED_CMND)
            ++act_stats;

    for (act_rq=0,nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
        ++act_rq;

    TRACE2(("gdth_to(): ints %d, ios %d, act_stats %d, act_rq %d\n",
            act_ints, act_ios, act_stats, act_rq));
    act_ints = act_ios = 0;

    gdth_timer.expires = jiffies + 30 * HZ;
    add_timer(&gdth_timer);
    GDTH_UNLOCK_HA(ha, flags);
}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118
GDTH_INITFUNC(void, internal_setup(char *str,int *ints))
{
    int i, argc;
    char *cur_str, *argv;

    TRACE2(("internal_setup() str %s ints[0] %d\n", 
            str ? str:"NULL", ints ? ints[0]:0));

    /* read irq[] from ints[] */
    if (ints) {
        argc = ints[0];
        if (argc > 0) {
            if (argc > MAXHA)
                argc = MAXHA;
            for (i = 0; i < argc; ++i)
                irq[i] = ints[i+1];
        }
    }

    /* analyse string */
    argv = str;
    while (argv && (cur_str = strchr(argv, ':'))) {
        int val = 0, c = *++cur_str;
        
        if (c == 'n' || c == 'N')
            val = 0;
        else if (c == 'y' || c == 'Y')
            val = 1;
        else
            val = (int)simple_strtoul(cur_str, NULL, 0);
Linus Torvalds's avatar
Linus Torvalds committed
4119

Linus Torvalds's avatar
Linus Torvalds committed
4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178
        if (!strncmp(argv, "disable:", 8))
            disable = val;
        else if (!strncmp(argv, "reserve_mode:", 13))
            reserve_mode = val;
        else if (!strncmp(argv, "reverse_scan:", 13))
            reverse_scan = val;
        else if (!strncmp(argv, "hdr_channel:", 12))
            hdr_channel = val;
        else if (!strncmp(argv, "max_ids:", 8))
            max_ids = val;
        else if (!strncmp(argv, "rescan:", 7))
            rescan = val;
        else if (!strncmp(argv, "virt_ctr:", 9))
            virt_ctr = val;
        else if (!strncmp(argv, "shared_access:", 14))
            shared_access = val;
        else if (!strncmp(argv, "reserve_list:", 13)) {
            reserve_list[0] = val;
            for (i = 1; i < MAX_RES_ARGS; i++) {
                cur_str = strchr(cur_str, ',');
                if (!cur_str)
                    break;
                if (!isdigit((int)*++cur_str)) {
                    --cur_str;          
                    break;
                }
                reserve_list[i] = 
                    (int)simple_strtoul(cur_str, NULL, 0);
            }
            if (!cur_str)
                break;
            argv = ++cur_str;
            continue;
        }

        if ((argv = strchr(argv, ',')))
            ++argv;
    }
}

GDTH_INITFUNC(int, option_setup(char *str))
{
    int ints[MAXHA];
    char *cur = str;
    int i = 1;

    TRACE2(("option_setup() str %s\n", str ? str:"NULL")); 

    while (cur && isdigit(*cur) && i <= MAXHA) {
        ints[i++] = simple_strtoul(cur, NULL, 0);
        if ((cur = strchr(cur, ',')) != NULL) cur++;
    }

    ints[0] = i - 1;
    internal_setup(cur, ints);
    return 1;
}

GDTH_INITFUNC(int, gdth_detect(Scsi_Host_Template *shtp))
Linus Torvalds's avatar
Linus Torvalds committed
4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210
{
    struct Scsi_Host *shp;
    gdth_ha_str *ha;
    ulong32 isa_bios;
    ushort eisa_slot;
    int i,hanum,cnt,ctr;
    unchar b;
    
 
#ifdef DEBUG_GDTH
    printk("GDT: This driver contains debugging information !! Trace level = %d\n",
        DebugState);
    printk("     Destination of debugging information: ");
#ifdef __SERIAL__
#ifdef __COM2__
    printk("Serial port COM2\n");
#else
    printk("Serial port COM1\n");
#endif
#else
    printk("Console\n");
#endif
    gdth_delay(3000);
#endif

    TRACE(("gdth_detect()\n"));

    if (disable) {
        printk("GDT: Controller driver disabled from command line !\n");
        return 0;
    }

4211
    printk("GDT: Storage RAID Controller Driver. Version: %s \n",GDTH_VERSION_STR);
Linus Torvalds's avatar
Linus Torvalds committed
4212 4213 4214 4215 4216 4217
    /* initializations */
    gdth_polling = TRUE; b = 0;
    gdth_clear_events();

    /* scanning for controllers, at first: ISA controller */
    for (isa_bios=0xc8000UL; isa_bios<=0xd8000UL; isa_bios+=0x8000UL) {
4218 4219 4220

	dma_addr_t scratch_dma_handle;

Linus Torvalds's avatar
Linus Torvalds committed
4221 4222 4223 4224
        if (gdth_ctr_count >= MAXHA) 
            break;
        if (gdth_search_isa(isa_bios)) {        /* controller found */
            shp = scsi_register(shtp,sizeof(gdth_ext_str));
Alan Cox's avatar
Alan Cox committed
4225 4226
            if (shp == NULL)
                continue;  
4227

Linus Torvalds's avatar
Linus Torvalds committed
4228 4229 4230 4231 4232
            ha = HADATA(shp);
            if (!gdth_init_isa(isa_bios,ha)) {
                scsi_unregister(shp);
                continue;
            }
Linus Torvalds's avatar
Linus Torvalds committed
4233 4234 4235
#ifdef __ia64__
            break;
#else
Linus Torvalds's avatar
Linus Torvalds committed
4236 4237 4238 4239
            /* controller found and initialized */
            printk("Configuring GDT-ISA HA at BIOS 0x%05X IRQ %u DRQ %u\n",
                   isa_bios,ha->irq,ha->drq);

Linus Torvalds's avatar
Linus Torvalds committed
4240
#if LINUX_VERSION_CODE >= 0x010346 
Linus Torvalds's avatar
Linus Torvalds committed
4241
            if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT,"gdth",ha))
Linus Torvalds's avatar
Linus Torvalds committed
4242 4243 4244
#else
            if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT,"gdth")) 
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4245 4246 4247 4248 4249 4250 4251
            {
                printk("GDT-ISA: Unable to allocate IRQ\n");
                scsi_unregister(shp);
                continue;
            }
            if (request_dma(ha->drq,"gdth")) {
                printk("GDT-ISA: Unable to allocate DMA channel\n");
Linus Torvalds's avatar
Linus Torvalds committed
4252
#if LINUX_VERSION_CODE >= 0x010346 
Linus Torvalds's avatar
Linus Torvalds committed
4253
                free_irq(ha->irq,ha);
Linus Torvalds's avatar
Linus Torvalds committed
4254 4255 4256
#else
                free_irq(ha->irq);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272
                scsi_unregister(shp);
                continue;
            }
            set_dma_mode(ha->drq,DMA_MODE_CASCADE);
            enable_dma(ha->drq);
            shp->unchecked_isa_dma = 1;
            shp->irq = ha->irq;
            shp->dma_channel = ha->drq;
            hanum = gdth_ctr_count;         
            gdth_ctr_tab[gdth_ctr_count++] = shp;
            gdth_ctr_vtab[gdth_ctr_vcount++] = shp;

            NUMDATA(shp)->hanum = (ushort)hanum;
            NUMDATA(shp)->busnum= 0;

            ha->pccb = CMDDATA(shp);
Alan Cox's avatar
Alan Cox committed
4273 4274 4275 4276
            ha->ccb_phys = 0L;
#if LINUX_VERSION_CODE >= 0x020400
            ha->pdev = NULL;
            ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH, 
4277 4278
						&scratch_dma_handle);
            ha->scratch_phys = (ulong32)scratch_dma_handle;
Linus Torvalds's avatar
Linus Torvalds committed
4279 4280
#else
            ha->pscratch = scsi_init_malloc(GDTH_SCRATCH, GFP_ATOMIC | GFP_DMA);
Alan Cox's avatar
Alan Cox committed
4281 4282
            if (ha->pscratch)
                ha->scratch_phys = virt_to_bus(ha->pscratch);
Linus Torvalds's avatar
Linus Torvalds committed
4283
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296
            ha->scratch_busy = FALSE;
            ha->req_first = NULL;
            ha->tid_cnt = MAX_HDRIVES;
            if (max_ids > 0 && max_ids < ha->tid_cnt)
                ha->tid_cnt = max_ids;
            for (i=0; i<GDTH_MAXCMDS; ++i)
                ha->cmd_tab[i].cmnd = UNUSED_CMND;
            ha->scan_mode = rescan ? 0x10 : 0;

            if (ha->pscratch == NULL || !gdth_search_drives(hanum)) {
                printk("GDT-ISA: Error during device scan\n");
                --gdth_ctr_count;
                --gdth_ctr_vcount;
Alan Cox's avatar
Alan Cox committed
4297 4298 4299 4300
                if (ha->pscratch != NULL) {
#if LINUX_VERSION_CODE >= 0x020400
                    pci_free_consistent(ha->pdev, GDTH_SCRATCH, 
                                        ha->pscratch, ha->scratch_phys);
Linus Torvalds's avatar
Linus Torvalds committed
4301 4302 4303
#else
                    scsi_init_free((void *)ha->pscratch, GDTH_SCRATCH);
#endif
Alan Cox's avatar
Alan Cox committed
4304
                }
Linus Torvalds's avatar
Linus Torvalds committed
4305
#if LINUX_VERSION_CODE >= 0x010346 
Linus Torvalds's avatar
Linus Torvalds committed
4306
                free_irq(ha->irq,ha);
Linus Torvalds's avatar
Linus Torvalds committed
4307 4308 4309
#else
                free_irq(ha->irq);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4310 4311 4312 4313 4314 4315 4316
                scsi_unregister(shp);
                continue;
            }
            if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
                hdr_channel = ha->bus_cnt;
            ha->virt_bus = hdr_channel;

Linus Torvalds's avatar
Linus Torvalds committed
4317
#if LINUX_VERSION_CODE >= 0x020000
Linus Torvalds's avatar
Linus Torvalds committed
4318 4319
            shp->max_id      = ha->tid_cnt;
            shp->max_lun     = MAXLUN;
Linus Torvalds's avatar
Linus Torvalds committed
4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336
            shp->max_channel = virt_ctr ? 0 : ha->bus_cnt;
            if (virt_ctr)  
#endif
            {
                virt_ctr = 1;
                /* register addit. SCSI channels as virtual controllers */
                for (b = 1; b < ha->bus_cnt + 1; ++b) {
                    shp = scsi_register(shtp,sizeof(gdth_num_str));
                    shp->unchecked_isa_dma = 1;
                    shp->irq = ha->irq;
                    shp->dma_channel = ha->drq;
                    gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
                    NUMDATA(shp)->hanum = (ushort)hanum;
                    NUMDATA(shp)->busnum = b;
                }
            }  

Linus Torvalds's avatar
Linus Torvalds committed
4337 4338
            GDTH_INIT_LOCK_HA(ha);
            gdth_enable_int(hanum);
Linus Torvalds's avatar
Linus Torvalds committed
4339
#endif /* !__ia64__ */
Linus Torvalds's avatar
Linus Torvalds committed
4340 4341 4342 4343 4344
        }
    }

    /* scanning for EISA controllers */
    for (eisa_slot=0x1000; eisa_slot<=0x8000; eisa_slot+=0x1000) {
4345 4346 4347

	dma_addr_t scratch_dma_handle;

Linus Torvalds's avatar
Linus Torvalds committed
4348 4349 4350 4351
        if (gdth_ctr_count >= MAXHA) 
            break;
        if (gdth_search_eisa(eisa_slot)) {      /* controller found */
            shp = scsi_register(shtp,sizeof(gdth_ext_str));
Alan Cox's avatar
Alan Cox committed
4352 4353
            if (shp == NULL)
                continue;  
4354

Linus Torvalds's avatar
Linus Torvalds committed
4355 4356 4357 4358 4359 4360 4361 4362 4363
            ha = HADATA(shp);
            if (!gdth_init_eisa(eisa_slot,ha)) {
                scsi_unregister(shp);
                continue;
            }
            /* controller found and initialized */
            printk("Configuring GDT-EISA HA at Slot %d IRQ %u\n",
                   eisa_slot>>12,ha->irq);

Linus Torvalds's avatar
Linus Torvalds committed
4364
#if LINUX_VERSION_CODE >= 0x010346 
Linus Torvalds's avatar
Linus Torvalds committed
4365
            if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT,"gdth",ha))
Linus Torvalds's avatar
Linus Torvalds committed
4366 4367 4368
#else
            if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT,"gdth")) 
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386
            {
                printk("GDT-EISA: Unable to allocate IRQ\n");
                scsi_unregister(shp);
                continue;
            }
            shp->unchecked_isa_dma = 0;
            shp->irq = ha->irq;
            shp->dma_channel = 0xff;
            hanum = gdth_ctr_count;
            gdth_ctr_tab[gdth_ctr_count++] = shp;
            gdth_ctr_vtab[gdth_ctr_vcount++] = shp;

            NUMDATA(shp)->hanum = (ushort)hanum;
            NUMDATA(shp)->busnum= 0;
            TRACE2(("EISA detect Bus 0: hanum %d\n",
                    NUMDATA(shp)->hanum));

            ha->pccb = CMDDATA(shp);
Alan Cox's avatar
Alan Cox committed
4387 4388 4389 4390
            ha->ccb_phys = 0L; 
#if LINUX_VERSION_CODE >= 0x020400
            ha->pdev = NULL;
            ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH, 
4391 4392
						&scratch_dma_handle);
            ha->scratch_phys = (ulong32) scratch_dma_handle;
Alan Cox's avatar
Alan Cox committed
4393 4394 4395
            ha->ccb_phys = 
                pci_map_single(ha->pdev,ha->pccb,
                               sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
Linus Torvalds's avatar
Linus Torvalds committed
4396 4397
#else
            ha->pscratch = scsi_init_malloc(GDTH_SCRATCH, GFP_ATOMIC | GFP_DMA);
Alan Cox's avatar
Alan Cox committed
4398 4399 4400
            if (ha->pscratch)
                ha->scratch_phys = virt_to_bus(ha->pscratch);
            ha->ccb_phys = virt_to_bus(ha->pccb);
Linus Torvalds's avatar
Linus Torvalds committed
4401
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414
            ha->scratch_busy = FALSE;
            ha->req_first = NULL;
            ha->tid_cnt = MAX_HDRIVES;
            if (max_ids > 0 && max_ids < ha->tid_cnt)
                ha->tid_cnt = max_ids;
            for (i=0; i<GDTH_MAXCMDS; ++i)
                ha->cmd_tab[i].cmnd = UNUSED_CMND;
            ha->scan_mode = rescan ? 0x10 : 0;

            if (ha->pscratch == NULL || !gdth_search_drives(hanum)) {
                printk("GDT-EISA: Error during device scan\n");
                --gdth_ctr_count;
                --gdth_ctr_vcount;
Alan Cox's avatar
Alan Cox committed
4415 4416 4417 4418 4419 4420
                if (ha->pscratch != NULL) {
#if LINUX_VERSION_CODE >= 0x020400
                    pci_free_consistent(ha->pdev, GDTH_SCRATCH, 
                                        ha->pscratch, ha->scratch_phys);
                    pci_unmap_single(ha->pdev,ha->ccb_phys,
                                    sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
Linus Torvalds's avatar
Linus Torvalds committed
4421 4422 4423
#else
                    scsi_init_free((void *)ha->pscratch, GDTH_SCRATCH);
#endif
Alan Cox's avatar
Alan Cox committed
4424
                }
Linus Torvalds's avatar
Linus Torvalds committed
4425
#if LINUX_VERSION_CODE >= 0x010346 
Linus Torvalds's avatar
Linus Torvalds committed
4426
                free_irq(ha->irq,ha);
Linus Torvalds's avatar
Linus Torvalds committed
4427 4428 4429
#else
                free_irq(ha->irq);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4430 4431 4432 4433 4434 4435 4436
                scsi_unregister(shp);
                continue;
            }
            if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
                hdr_channel = ha->bus_cnt;
            ha->virt_bus = hdr_channel;

Linus Torvalds's avatar
Linus Torvalds committed
4437
#if LINUX_VERSION_CODE >= 0x020000
Linus Torvalds's avatar
Linus Torvalds committed
4438 4439
            shp->max_id      = ha->tid_cnt;
            shp->max_lun     = MAXLUN;
Linus Torvalds's avatar
Linus Torvalds committed
4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456
            shp->max_channel = virt_ctr ? 0 : ha->bus_cnt;
            if (virt_ctr)  
#endif
            {
                virt_ctr = 1;
                /* register addit. SCSI channels as virtual controllers */
                for (b = 1; b < ha->bus_cnt + 1; ++b) {
                    shp = scsi_register(shtp,sizeof(gdth_num_str));
                    shp->unchecked_isa_dma = 0;
                    shp->irq = ha->irq;
                    shp->dma_channel = 0xff;
                    gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
                    NUMDATA(shp)->hanum = (ushort)hanum;
                    NUMDATA(shp)->busnum = b;
                }
            }  

Linus Torvalds's avatar
Linus Torvalds committed
4457 4458 4459 4460 4461 4462
            GDTH_INIT_LOCK_HA(ha);
            gdth_enable_int(hanum);
        }
    }

    /* scanning for PCI controllers */
4463
#if LINUX_VERSION_CODE < 0x2015C
Linus Torvalds's avatar
Linus Torvalds committed
4464 4465
    if (pcibios_present())
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4466 4467 4468 4469
    {
        gdth_pci_str pcistr[MAXHA];

        cnt = gdth_search_pci(pcistr);
Alan Cox's avatar
Alan Cox committed
4470
        printk("GDT: Found %d PCI Storage RAID Controllers\n",cnt);
Linus Torvalds's avatar
Linus Torvalds committed
4471 4472
        gdth_sort_pci(pcistr,cnt);
        for (ctr = 0; ctr < cnt; ++ctr) {
4473 4474 4475

	    dma_addr_t scratch_dma_handle;

Linus Torvalds's avatar
Linus Torvalds committed
4476 4477 4478
            if (gdth_ctr_count >= MAXHA)
                break;
            shp = scsi_register(shtp,sizeof(gdth_ext_str));
Alan Cox's avatar
Alan Cox committed
4479 4480
            if (shp == NULL)
                continue;  
4481

Linus Torvalds's avatar
Linus Torvalds committed
4482 4483 4484 4485 4486 4487
            ha = HADATA(shp);
            if (!gdth_init_pci(&pcistr[ctr],ha)) {
                scsi_unregister(shp);
                continue;
            }
            /* controller found and initialized */
4488 4489
            printk("GDT CTR%d: Configuring GDT-PCI HA at %d/%d IRQ %u\n",
                   ctr,pcistr[ctr].bus,PCI_SLOT(pcistr[ctr].device_fn),ha->irq);
Linus Torvalds's avatar
Linus Torvalds committed
4490

Linus Torvalds's avatar
Linus Torvalds committed
4491
#if LINUX_VERSION_CODE >= 0x010346 
Linus Torvalds's avatar
Linus Torvalds committed
4492 4493
            if (request_irq(ha->irq, gdth_interrupt,
                            SA_INTERRUPT|SA_SHIRQ, "gdth", ha))
Linus Torvalds's avatar
Linus Torvalds committed
4494 4495 4496 4497
#else
            if (request_irq(ha->irq, gdth_interrupt,
                            SA_INTERRUPT|SA_SHIRQ, "gdth")) 
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513
            {
                printk("GDT-PCI: Unable to allocate IRQ\n");
                scsi_unregister(shp);
                continue;
            }
            shp->unchecked_isa_dma = 0;
            shp->irq = ha->irq;
            shp->dma_channel = 0xff;
            hanum = gdth_ctr_count;
            gdth_ctr_tab[gdth_ctr_count++] = shp;
            gdth_ctr_vtab[gdth_ctr_vcount++] = shp;

            NUMDATA(shp)->hanum = (ushort)hanum;
            NUMDATA(shp)->busnum= 0;

            ha->pccb = CMDDATA(shp);
Alan Cox's avatar
Alan Cox committed
4514 4515 4516
            ha->ccb_phys = 0L;
#if LINUX_VERSION_CODE >= 0x020400
            ha->pscratch = pci_alloc_consistent(ha->pdev, GDTH_SCRATCH, 
4517 4518
						&scratch_dma_handle);
            ha->scratch_phys = (ulong32)scratch_dma_handle;
Linus Torvalds's avatar
Linus Torvalds committed
4519 4520
#else
            ha->pscratch = scsi_init_malloc(GDTH_SCRATCH, GFP_ATOMIC | GFP_DMA);
Alan Cox's avatar
Alan Cox committed
4521 4522
            if (ha->pscratch)
                ha->scratch_phys = virt_to_bus(ha->pscratch);
Linus Torvalds's avatar
Linus Torvalds committed
4523
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536
            ha->scratch_busy = FALSE;
            ha->req_first = NULL;
            ha->tid_cnt = pcistr[ctr].device_id >= 0x200 ? MAXID : MAX_HDRIVES;
            if (max_ids > 0 && max_ids < ha->tid_cnt)
                ha->tid_cnt = max_ids;
            for (i=0; i<GDTH_MAXCMDS; ++i)
                ha->cmd_tab[i].cmnd = UNUSED_CMND;
            ha->scan_mode = rescan ? 0x10 : 0;

            if (ha->pscratch == NULL || !gdth_search_drives(hanum)) {
                printk("GDT-PCI: Error during device scan\n");
                --gdth_ctr_count;
                --gdth_ctr_vcount;
Alan Cox's avatar
Alan Cox committed
4537 4538 4539 4540
                if (ha->pscratch != NULL) {
#if LINUX_VERSION_CODE >= 0x020400
                    pci_free_consistent(ha->pdev, GDTH_SCRATCH, 
                                        ha->pscratch, ha->scratch_phys);
Linus Torvalds's avatar
Linus Torvalds committed
4541 4542 4543
#else
                    scsi_init_free((void *)ha->pscratch, GDTH_SCRATCH);
#endif
Alan Cox's avatar
Alan Cox committed
4544
                }
Linus Torvalds's avatar
Linus Torvalds committed
4545
#if LINUX_VERSION_CODE >= 0x010346 
Linus Torvalds's avatar
Linus Torvalds committed
4546
                free_irq(ha->irq,ha);
Linus Torvalds's avatar
Linus Torvalds committed
4547 4548 4549
#else
                free_irq(ha->irq);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4550 4551 4552 4553 4554 4555 4556
                scsi_unregister(shp);
                continue;
            }
            if (hdr_channel < 0 || hdr_channel > ha->bus_cnt)
                hdr_channel = ha->bus_cnt;
            ha->virt_bus = hdr_channel;

Linus Torvalds's avatar
Linus Torvalds committed
4557
#if LINUX_VERSION_CODE >= 0x020000
Linus Torvalds's avatar
Linus Torvalds committed
4558 4559
            shp->max_id      = ha->tid_cnt;
            shp->max_lun     = MAXLUN;
Linus Torvalds's avatar
Linus Torvalds committed
4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576
            shp->max_channel = virt_ctr ? 0 : ha->bus_cnt;
            if (virt_ctr)  
#endif
            {
                virt_ctr = 1;
                /* register addit. SCSI channels as virtual controllers */
                for (b = 1; b < ha->bus_cnt + 1; ++b) {
                    shp = scsi_register(shtp,sizeof(gdth_num_str));
                    shp->unchecked_isa_dma = 0;
                    shp->irq = ha->irq;
                    shp->dma_channel = 0xff;
                    gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
                    NUMDATA(shp)->hanum = (ushort)hanum;
                    NUMDATA(shp)->busnum = b;
                }
            }  

Linus Torvalds's avatar
Linus Torvalds committed
4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591
            GDTH_INIT_LOCK_HA(ha);
            gdth_enable_int(hanum);
        }
    }

    TRACE2(("gdth_detect() %d controller detected\n",gdth_ctr_count));
    if (gdth_ctr_count > 0) {
#ifdef GDTH_STATISTICS
        TRACE2(("gdth_detect(): Initializing timer !\n"));
        init_timer(&gdth_timer);
        gdth_timer.expires = jiffies + HZ;
        gdth_timer.data = 0L;
        gdth_timer.function = gdth_timeout;
        add_timer(&gdth_timer);
#endif
Alan Cox's avatar
Alan Cox committed
4592 4593 4594
#ifdef GDTH_IOCTL_CHRDEV
        major = register_chrdev(0,"gdth",&gdth_fops);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4595
#if LINUX_VERSION_CODE >= 0x020100
Linus Torvalds's avatar
Linus Torvalds committed
4596
        register_reboot_notifier(&gdth_notifier);
Linus Torvalds's avatar
Linus Torvalds committed
4597
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612
    }
    gdth_polling = FALSE;
    return gdth_ctr_vcount;
}


int gdth_release(struct Scsi_Host *shp)
{
    int hanum;
    gdth_ha_str *ha;

    TRACE2(("gdth_release()\n"));
    if (NUMDATA(shp)->busnum == 0) {
        hanum = NUMDATA(shp)->hanum;
        ha    = HADATA(gdth_ctr_tab[hanum]);
Linus Torvalds's avatar
Linus Torvalds committed
4613
#if LINUX_VERSION_CODE >= 0x010300
Linus Torvalds's avatar
Linus Torvalds committed
4614
        gdth_flush(hanum);
Linus Torvalds's avatar
Linus Torvalds committed
4615
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4616 4617

        if (shp->irq) {
Linus Torvalds's avatar
Linus Torvalds committed
4618
#if LINUX_VERSION_CODE >= 0x010346
Linus Torvalds's avatar
Linus Torvalds committed
4619
            free_irq(shp->irq,ha);
Linus Torvalds's avatar
Linus Torvalds committed
4620 4621 4622
#else
            free_irq(shp->irq);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4623
        }
Linus Torvalds's avatar
Linus Torvalds committed
4624
#ifndef __ia64__
Linus Torvalds's avatar
Linus Torvalds committed
4625 4626 4627
        if (shp->dma_channel != 0xff) {
            free_dma(shp->dma_channel);
        }
Linus Torvalds's avatar
Linus Torvalds committed
4628
#endif
Alan Cox's avatar
Alan Cox committed
4629 4630 4631 4632 4633 4634
#if LINUX_VERSION_CODE >= 0x020400
        pci_free_consistent(ha->pdev, GDTH_SCRATCH, 
            ha->pscratch, ha->scratch_phys);
        if (ha->ccb_phys)
            pci_unmap_single(ha->pdev,ha->ccb_phys,
                             sizeof(gdth_cmd_str),PCI_DMA_BIDIRECTIONAL);
Linus Torvalds's avatar
Linus Torvalds committed
4635 4636 4637
#else
        scsi_init_free((void *)ha->pscratch, GDTH_SCRATCH);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4638 4639 4640 4641 4642 4643 4644 4645
        gdth_ctr_released++;
        TRACE2(("gdth_release(): HA %d of %d\n", 
                gdth_ctr_released, gdth_ctr_count));

        if (gdth_ctr_released == gdth_ctr_count) {
#ifdef GDTH_STATISTICS
            del_timer(&gdth_timer);
#endif
Alan Cox's avatar
Alan Cox committed
4646 4647 4648
#ifdef GDTH_IOCTL_CHRDEV
            unregister_chrdev(major,"gdth");
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4649
#if LINUX_VERSION_CODE >= 0x020100
Linus Torvalds's avatar
Linus Torvalds committed
4650
            unregister_reboot_notifier(&gdth_notifier);
Linus Torvalds's avatar
Linus Torvalds committed
4651
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710
        }
    }

    scsi_unregister(shp);
    return 0;
}
            

static const char *gdth_ctr_name(int hanum)
{
    gdth_ha_str *ha;

    TRACE2(("gdth_ctr_name()\n"));

    ha    = HADATA(gdth_ctr_tab[hanum]);

    if (ha->type == GDT_EISA) {
        switch (ha->stype) {
          case GDT3_ID:
            return("GDT3000/3020");
          case GDT3A_ID:
            return("GDT3000A/3020A/3050A");
          case GDT3B_ID:
            return("GDT3000B/3010A");
        }
    } else if (ha->type == GDT_ISA) {
        return("GDT2000/2020");
    } else if (ha->type == GDT_PCI) {
        switch (ha->stype) {
          case PCI_DEVICE_ID_VORTEX_GDT60x0:
            return("GDT6000/6020/6050");
          case PCI_DEVICE_ID_VORTEX_GDT6000B:
            return("GDT6000B/6010");
        }
    } 
    /* new controllers (GDT_PCINEW, GDT_PCIMPR, ..) use board_info IOCTL! */

    return("");
}

const char *gdth_info(struct Scsi_Host *shp)
{
    int hanum;
    gdth_ha_str *ha;

    TRACE2(("gdth_info()\n"));
    hanum = NUMDATA(shp)->hanum;
    ha    = HADATA(gdth_ctr_tab[hanum]);

    return ((const char *)ha->binfo.type_string);
}

/* old error handling */
int gdth_abort(Scsi_Cmnd *scp)
{
    TRACE2(("gdth_abort() reason %d\n",scp->abort_reason));
    return SCSI_ABORT_SNOOZE;
}

Linus Torvalds's avatar
Linus Torvalds committed
4711
#if LINUX_VERSION_CODE >= 0x010346
Linus Torvalds's avatar
Linus Torvalds committed
4712
int gdth_reset(Scsi_Cmnd *scp, unsigned int reset_flags)
Linus Torvalds's avatar
Linus Torvalds committed
4713 4714 4715
#else
int gdth_reset(Scsi_Cmnd *scp)
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4716 4717 4718 4719 4720
{
    TRACE2(("gdth_reset()\n"));
    return SCSI_RESET_PUNT;
}

Linus Torvalds's avatar
Linus Torvalds committed
4721
#if LINUX_VERSION_CODE >= 0x02015F
Linus Torvalds's avatar
Linus Torvalds committed
4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740
/* new error handling */
int gdth_eh_abort(Scsi_Cmnd *scp)
{
    TRACE2(("gdth_eh_abort()\n"));
    return FAILED;
}

int gdth_eh_device_reset(Scsi_Cmnd *scp)
{
    TRACE2(("gdth_eh_device_reset()\n"));
    return FAILED;
}

int gdth_eh_bus_reset(Scsi_Cmnd *scp)
{
    int i, hanum;
    gdth_ha_str *ha;
    ulong flags;
    Scsi_Cmnd *cmnd;
Linus Torvalds's avatar
Linus Torvalds committed
4741
    unchar b;
Linus Torvalds's avatar
Linus Torvalds committed
4742 4743

    TRACE2(("gdth_eh_bus_reset()\n"));
Alan Cox's avatar
Alan Cox committed
4744
#if LINUX_VERSION_CODE >= 0x02053C
4745 4746
    hanum = NUMDATA(scp->device->host)->hanum;
    b = virt_ctr ? NUMDATA(scp->device->host)->busnum : scp->device->channel;
Alan Cox's avatar
Alan Cox committed
4747 4748 4749 4750
#else
    hanum = NUMDATA(scp->host)->hanum;
    b = virt_ctr ? NUMDATA(scp->host)->busnum : scp->channel;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4751 4752
    ha    = HADATA(gdth_ctr_tab[hanum]);

Linus Torvalds's avatar
Linus Torvalds committed
4753
    /* clear command tab */
Linus Torvalds's avatar
Linus Torvalds committed
4754 4755 4756
    GDTH_LOCK_HA(ha, flags);
    for (i = 0; i < GDTH_MAXCMDS; ++i) {
        cmnd = ha->cmd_tab[i].cmnd;
Alan Cox's avatar
Alan Cox committed
4757
#if LINUX_VERSION_CODE >= 0x02053C
4758
        if (!SPECIAL_SCP(cmnd) && cmnd->device->channel == b)
Alan Cox's avatar
Alan Cox committed
4759 4760 4761
#else
        if (!SPECIAL_SCP(cmnd) && cmnd->channel == b)
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4762 4763 4764
            ha->cmd_tab[i].cmnd = UNUSED_CMND;
    }
    GDTH_UNLOCK_HA(ha, flags);
Linus Torvalds's avatar
Linus Torvalds committed
4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793

    if (b == ha->virt_bus) {
        /* host drives */
        for (i = 0; i < MAX_HDRIVES; ++i) {
            if (ha->hdr[i].present) {
                GDTH_LOCK_HA(ha, flags);
                gdth_polling = TRUE;
                while (gdth_test_busy(hanum))
                    gdth_delay(0);
                if (gdth_internal_cmd(hanum, CACHESERVICE, 
                                      GDT_CLUST_RESET, i, 0, 0))
                    ha->hdr[i].cluster_type &= ~CLUSTER_RESERVED;
                gdth_polling = FALSE;
                GDTH_UNLOCK_HA(ha, flags);
            }
        }
    } else {
        /* raw devices */
        GDTH_LOCK_HA(ha, flags);
        for (i = 0; i < MAXID; ++i)
            ha->raw[BUS_L2P(ha,b)].io_cnt[i] = 0;
        gdth_polling = TRUE;
        while (gdth_test_busy(hanum))
            gdth_delay(0);
        gdth_internal_cmd(hanum, SCSIRAWSERVICE, GDT_RESET_BUS,
                          BUS_L2P(ha,b), 0, 0);
        gdth_polling = FALSE;
        GDTH_UNLOCK_HA(ha, flags);
    }
Linus Torvalds's avatar
Linus Torvalds committed
4794 4795 4796 4797 4798 4799 4800 4801
    return SUCCESS;
}

int gdth_eh_host_reset(Scsi_Cmnd *scp)
{
    TRACE2(("gdth_eh_host_reset()\n"));
    return FAILED;
}
Linus Torvalds's avatar
Linus Torvalds committed
4802
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4803

Alan Cox's avatar
Alan Cox committed
4804 4805 4806 4807 4808 4809 4810 4811

#if LINUX_VERSION_CODE >= 0x020503
int gdth_bios_param(struct scsi_device *sdev,struct block_device *bdev,sector_t cap,int *ip)
#elif LINUX_VERSION_CODE >= 0x010300
int gdth_bios_param(Disk *disk,kdev_t dev,int *ip)
#else
int gdth_bios_param(Disk *disk,int dev,int *ip)
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4812
{
Linus Torvalds's avatar
Linus Torvalds committed
4813
    unchar b, t;
Linus Torvalds's avatar
Linus Torvalds committed
4814 4815
    int hanum;
    gdth_ha_str *ha;
Alan Cox's avatar
Alan Cox committed
4816 4817
    struct scsi_device *sd;
    unsigned capacity;
Linus Torvalds's avatar
Linus Torvalds committed
4818

Alan Cox's avatar
Alan Cox committed
4819 4820 4821 4822 4823 4824 4825 4826 4827 4828
#if LINUX_VERSION_CODE >= 0x020503
    sd = sdev;
    capacity = cap;
#else
    sd = disk->device;
    capacity = disk->capacity;
#endif
    hanum = NUMDATA(sd->host)->hanum;
    b = virt_ctr ? NUMDATA(sd->host)->busnum : sd->channel;
    t = sd->id;
Linus Torvalds's avatar
Linus Torvalds committed
4829
    TRACE2(("gdth_bios_param() ha %d bus %d target %d\n", hanum, b, t)); 
Linus Torvalds's avatar
Linus Torvalds committed
4830 4831
    ha = HADATA(gdth_ctr_tab[hanum]);

Linus Torvalds's avatar
Linus Torvalds committed
4832
    if (b != ha->virt_bus || ha->hdr[t].heads == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
4833
        /* raw device or host drive without mapping information */
Linus Torvalds's avatar
Linus Torvalds committed
4834
        TRACE2(("Evaluate mapping\n"));
4835
        gdth_eval_mapping(capacity,&ip[2],&ip[0],&ip[1]);
Linus Torvalds's avatar
Linus Torvalds committed
4836
    } else {
Linus Torvalds's avatar
Linus Torvalds committed
4837 4838
        ip[0] = ha->hdr[t].heads;
        ip[1] = ha->hdr[t].secs;
4839
        ip[2] = capacity / ip[0] / ip[1];
Linus Torvalds's avatar
Linus Torvalds committed
4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852
    }

    TRACE2(("gdth_bios_param(): %d heads, %d secs, %d cyls\n",
            ip[0],ip[1],ip[2]));
    return 0;
}


int gdth_queuecommand(Scsi_Cmnd *scp,void (*done)(Scsi_Cmnd *))
{
    int hanum;
    int priority;

Alan Cox's avatar
Alan Cox committed
4853
    TRACE(("gdth_queuecommand() cmd 0x%x\n", scp->cmnd[0]));
Linus Torvalds's avatar
Linus Torvalds committed
4854 4855 4856 4857
    
    scp->scsi_done = (void *)done;
    scp->SCp.have_data_in = 1;
    scp->SCp.phase = -1;
Linus Torvalds's avatar
Linus Torvalds committed
4858
    scp->SCp.sent_command = -1;
Alan Cox's avatar
Alan Cox committed
4859 4860 4861
    scp->SCp.Status = GDTH_MAP_NONE;
    scp->SCp.buffer = (struct scatterlist *)NULL;
#if LINUX_VERSION_CODE >= 0x02053C
4862
    hanum = NUMDATA(scp->device->host)->hanum;
Alan Cox's avatar
Alan Cox committed
4863 4864 4865
#else
    hanum = NUMDATA(scp->host)->hanum;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4866 4867 4868 4869 4870
#ifdef GDTH_STATISTICS
    ++act_ios;
#endif

    priority = DEFAULT_PRI;
Linus Torvalds's avatar
Linus Torvalds committed
4871
#if LINUX_VERSION_CODE >= 0x010300
Linus Torvalds's avatar
Linus Torvalds committed
4872 4873
    if (scp->done == gdth_scsi_done)
        priority = scp->SCp.this_residual;
Linus Torvalds's avatar
Linus Torvalds committed
4874
#endif
Linus Torvalds's avatar
Linus Torvalds committed
4875 4876 4877 4878 4879 4880
    gdth_update_timeout(hanum, scp, scp->timeout_per_command * 6);
    gdth_putq( hanum, scp, priority );
    gdth_next( hanum );
    return 0;
}

Alan Cox's avatar
Alan Cox committed
4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893
#ifdef GDTH_IOCTL_CHRDEV
static int gdth_open(struct inode *inode, struct file *filep)
{
    TRACE(("gdth_open()\n"));
    return 0;
}

static int gdth_close(struct inode *inode, struct file *filep)
{
    TRACE(("gdth_close()\n"));
    return 0;
}

4894
static int ioc_event(unsigned long arg)
Alan Cox's avatar
Alan Cox committed
4895
{
4896 4897 4898
        gdth_ioctl_event evt;
        gdth_ha_str *ha;
        ulong flags;
Alan Cox's avatar
Alan Cox committed
4899

4900 4901 4902 4903
        if (copy_from_user(&evt, (char *)arg, sizeof(gdth_ioctl_event)) ||
            evt.ionode >= gdth_ctr_count)
            return -EFAULT;
        ha = HADATA(gdth_ctr_tab[evt.ionode]);
Alan Cox's avatar
Alan Cox committed
4904

4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928
        if (evt.erase == 0xff) {
            if (evt.event.event_source == ES_TEST)
                evt.event.event_data.size=sizeof(evt.event.event_data.eu.test); 
            else if (evt.event.event_source == ES_DRIVER)
                evt.event.event_data.size=sizeof(evt.event.event_data.eu.driver); 
            else if (evt.event.event_source == ES_SYNC)
                evt.event.event_data.size=sizeof(evt.event.event_data.eu.sync); 
            else
                evt.event.event_data.size=sizeof(evt.event.event_data.eu.async);
            GDTH_LOCK_HA(ha, flags);
            gdth_store_event(ha, evt.event.event_source, evt.event.event_idx,
                             &evt.event.event_data);
            GDTH_UNLOCK_HA(ha, flags);
        } else if (evt.erase == 0xfe) {
            gdth_clear_events();
        } else if (evt.erase == 0) {
            evt.handle = gdth_read_event(ha, evt.handle, &evt.event);
        } else {
            gdth_readapp_event(ha, evt.erase, &evt.event);
        }     
        if (copy_to_user((char *)arg, &evt, sizeof(gdth_ioctl_event)))
            return -EFAULT;
        return 0;
}
Alan Cox's avatar
Alan Cox committed
4929

4930 4931 4932 4933 4934 4935 4936 4937 4938
static int ioc_lockdrv(unsigned long arg)
{
        gdth_ioctl_lockdrv ldrv;
        unchar i, j;
        ulong flags;
        gdth_ha_str *ha;

        if (copy_from_user(&ldrv, (char *)arg, sizeof(gdth_ioctl_lockdrv)) ||
            ldrv.ionode >= gdth_ctr_count)
Alan Cox's avatar
Alan Cox committed
4939
            return -EFAULT;
4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951
        ha = HADATA(gdth_ctr_tab[ldrv.ionode]);
 
        for (i = 0; i < ldrv.drive_cnt && i < MAX_HDRIVES; ++i) {
            j = ldrv.drives[i];
            if (j >= MAX_HDRIVES || !ha->hdr[j].present)
                continue;
            if (ldrv.lock) {
                GDTH_LOCK_HA(ha, flags);
                ha->hdr[j].lock = 1;
                GDTH_UNLOCK_HA(ha, flags);
                gdth_wait_completion(ldrv.ionode, ha->bus_cnt, j); 
                gdth_stop_timeout(ldrv.ionode, ha->bus_cnt, j); 
Alan Cox's avatar
Alan Cox committed
4952
            } else {
4953 4954 4955 4956 4957
                GDTH_LOCK_HA(ha, flags);
                ha->hdr[j].lock = 0;
                GDTH_UNLOCK_HA(ha, flags);
                gdth_start_timeout(ldrv.ionode, ha->bus_cnt, j); 
                gdth_next(ldrv.ionode); 
Alan Cox's avatar
Alan Cox committed
4958
            }
4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981
        } 
        return 0;
}

static int ioc_resetdrv(unsigned long arg, char *cmnd)
{
        gdth_ioctl_reset res;
        gdth_cmd_str cmd;
        int hanum;
        gdth_ha_str *ha;
#if LINUX_VERSION_CODE >= 0x020503
	Scsi_Request *srp;
	Scsi_Device *sdev;
#elif LINUX_VERSION_CODE >= 0x020322
	Scsi_Cmnd *scp;
	Scsi_Device *sdev;
#else
	Scsi_Cmnd scp;
	Scsi_Device sdev;
#endif

        if (copy_from_user(&res, (char *)arg, sizeof(gdth_ioctl_reset)) ||
            res.ionode >= gdth_ctr_count || res.number >= MAX_HDRIVES)
Alan Cox's avatar
Alan Cox committed
4982
            return -EFAULT;
4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028
        hanum = res.ionode;
        ha = HADATA(gdth_ctr_tab[hanum]);
 
        if (!ha->hdr[res.number].present)
            return 0;
        cmd.Service = CACHESERVICE;
        cmd.OpCode = GDT_CLUST_RESET;
        cmd.u.cache.DeviceNo = res.number;
#if LINUX_VERSION_CODE >= 0x020503
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
        srp  = scsi_allocate_request(sdev);
        if (!srp)
            return -ENOMEM;
        srp->sr_cmd_len = 12;
        srp->sr_use_sg = 0;
        gdth_do_req(srp, &cmd, cmnd, 30);
        res.status = (ushort)srp->sr_command->SCp.Status;
        scsi_release_request(srp);
        scsi_free_host_dev(sdev);
#elif LINUX_VERSION_CODE >= 0x020322
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
        scp  = scsi_allocate_device(sdev, 1, FALSE);
        if (!scp)
            return -ENOMEM;
        scp->cmd_len = 12;
        scp->use_sg = 0;
        gdth_do_cmd(scp, &cmd, cmnd, 30);
        res.status = (ushort)scp->SCp.Status;
        scsi_release_command(scp);
        scsi_free_host_dev(sdev);
#else
        memset(&sdev,0,sizeof(Scsi_Device));
        memset(&scp, 0,sizeof(Scsi_Cmnd));
        sdev.host = scp.host = gdth_ctr_tab[hanum];
        sdev.id = scp.target = sdev.host->this_id;
        scp.device = &sdev;
        gdth_do_cmd(&scp, &cmd, cmnd, 30);
        res.status = (ushort)scp.SCp.Status;
#endif
        if (copy_to_user((char *)arg, &res, sizeof(gdth_ioctl_reset)))
            return -EFAULT;
        return 0;
}

static int ioc_general(unsigned long arg, char *cmnd)
{
Alan Cox's avatar
Alan Cox committed
5029 5030 5031 5032
        gdth_ioctl_general gen;
        char *buf = NULL;
        ulong32 paddr; 
        int hanum;
5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043
	gdth_ha_str *ha; 
#if LINUX_VERSION_CODE >= 0x020503
	Scsi_Request *srp;
	Scsi_Device *sdev;
#elif LINUX_VERSION_CODE >= 0x020322
	Scsi_Cmnd *scp;
	Scsi_Device *sdev;
#else
	Scsi_Cmnd scp;
	Scsi_Device sdev;
#endif
Alan Cox's avatar
Alan Cox committed
5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137
        
        if (copy_from_user(&gen, (char *)arg, sizeof(gdth_ioctl_general)) ||
            gen.ionode >= gdth_ctr_count)
            return -EFAULT;
        hanum = gen.ionode; 
        ha = HADATA(gdth_ctr_tab[hanum]);
        if (gen.data_len + gen.sense_len != 0) {
            if (!(buf = gdth_ioctl_alloc(hanum, gen.data_len + gen.sense_len, 
                                         FALSE, &paddr)))
                return -EFAULT;
            if (copy_from_user(buf, (char *)arg + sizeof(gdth_ioctl_general),  
                               gen.data_len + gen.sense_len)) {
                gdth_ioctl_free(hanum, gen.data_len+gen.sense_len, buf, paddr);
                return -EFAULT;
            }

            if (gen.command.OpCode == GDT_IOCTL) {
                gen.command.u.ioctl.p_param = paddr;
            } else if (gen.command.Service == CACHESERVICE) {
                if (ha->cache_feat & SCATTER_GATHER) {
                    gen.command.u.cache.DestAddr = 0xffffffff;
                    gen.command.u.cache.sg_canz = 1;
                    gen.command.u.cache.sg_lst[0].sg_ptr = paddr;
                    gen.command.u.cache.sg_lst[0].sg_len = gen.data_len;
                    gen.command.u.cache.sg_lst[1].sg_len = 0;
                } else {
                    gen.command.u.cache.DestAddr = paddr;
                    gen.command.u.cache.sg_canz = 0;
                }
            } else if (gen.command.Service == SCSIRAWSERVICE) {
                if (ha->raw_feat & SCATTER_GATHER) {
                    gen.command.u.raw.sdata = 0xffffffff;
                    gen.command.u.raw.sg_ranz = 1;
                    gen.command.u.raw.sg_lst[0].sg_ptr = paddr;
                    gen.command.u.raw.sg_lst[0].sg_len = gen.data_len;
                    gen.command.u.raw.sg_lst[1].sg_len = 0;
                } else {
                    gen.command.u.raw.sdata = paddr;
                    gen.command.u.raw.sg_ranz = 0;
                }
                gen.command.u.raw.sense_data = paddr + gen.data_len;

            } else {
                gdth_ioctl_free(hanum, gen.data_len+gen.sense_len, buf, paddr);
                return -EFAULT;
            }
        }

#if LINUX_VERSION_CODE >= 0x020503
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
        srp  = scsi_allocate_request(sdev);
        if (!srp)
            return -ENOMEM;
        srp->sr_cmd_len = 12;
        srp->sr_use_sg = 0;
        gdth_do_req(srp, &gen.command, cmnd, gen.timeout);
        gen.status = srp->sr_command->SCp.Status;
        gen.info = srp->sr_command->SCp.Message;
        scsi_release_request(srp);
        scsi_free_host_dev(sdev);
#elif LINUX_VERSION_CODE >= 0x020322
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
        scp  = scsi_allocate_device(sdev, 1, FALSE);
        if (!scp)
            return -ENOMEM;
        scp->cmd_len = 12;
        scp->use_sg = 0;
        gdth_do_cmd(scp, &gen.command, cmnd, gen.timeout);
        gen.status = scp->SCp.Status;
        gen.info = scp->SCp.Message;
        scsi_release_command(scp);
        scsi_free_host_dev(sdev);
#else
        memset(&sdev,0,sizeof(Scsi_Device));
        memset(&scp, 0,sizeof(Scsi_Cmnd));
        sdev.host = scp.host = gdth_ctr_tab[hanum];
        sdev.id = scp.target = sdev.host->this_id;
        scp.device = &sdev;
        gdth_do_cmd(&scp, &gen.command, cmnd, gen.timeout);
        gen.status = scp.SCp.Status;
        gen.info = scp.SCp.Message;
#endif

        if (copy_to_user((char *)arg + sizeof(gdth_ioctl_general), buf, 
                         gen.data_len + gen.sense_len)) {
            gdth_ioctl_free(hanum, gen.data_len+gen.sense_len, buf, paddr);
            return -EFAULT; 
        } 
        if (copy_to_user((char *)arg, &gen, 
            sizeof(gdth_ioctl_general) - sizeof(gdth_cmd_str))) {
            gdth_ioctl_free(hanum, gen.data_len+gen.sense_len, buf, paddr);
            return -EFAULT;
        }
        gdth_ioctl_free(hanum, gen.data_len+gen.sense_len, buf, paddr);
5138 5139
        return 0;
}
Alan Cox's avatar
Alan Cox committed
5140
 
5141 5142 5143 5144
static int ioc_hdrlist(unsigned long arg, char *cmnd)
{
        gdth_ioctl_rescan rsc;
        gdth_cmd_str cmd;
Alan Cox's avatar
Alan Cox committed
5145
        gdth_ha_str *ha;
5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160
        unchar i;
        int hanum;
#if LINUX_VERSION_CODE >= 0x020503
	Scsi_Request *srp;
	Scsi_Device *sdev;
#elif LINUX_VERSION_CODE >= 0x020322
	Scsi_Cmnd *scp;
	Scsi_Device *sdev;
#else
	Scsi_Cmnd scp;
	Scsi_Device sdev;
#endif
        
        if (copy_from_user(&rsc, (char *)arg, sizeof(gdth_ioctl_rescan)) ||
            rsc.ionode >= gdth_ctr_count)
Alan Cox's avatar
Alan Cox committed
5161
            return -EFAULT;
5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185
        hanum = rsc.ionode;
        ha = HADATA(gdth_ctr_tab[hanum]);
   
#if LINUX_VERSION_CODE >= 0x020503
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
        srp  = scsi_allocate_request(sdev);
        if (!srp)
            return -ENOMEM;
        srp->sr_cmd_len = 12;
        srp->sr_use_sg = 0;
#elif LINUX_VERSION_CODE >= 0x020322
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
        scp  = scsi_allocate_device(sdev, 1, FALSE);
        if (!scp)
            return -ENOMEM;
        scp->cmd_len = 12;
        scp->use_sg = 0;
#else
        memset(&sdev,0,sizeof(Scsi_Device));
        memset(&scp, 0,sizeof(Scsi_Cmnd));
        sdev.host = scp.host = gdth_ctr_tab[hanum];
        sdev.id = scp.target = sdev.host->this_id;
        scp.device = &sdev;
#endif
Alan Cox's avatar
Alan Cox committed
5186

5187 5188 5189
        for (i = 0; i < MAX_HDRIVES; ++i) { 
            if (!ha->hdr[i].present) {
                rsc.hdr_list[i].bus = 0xff; 
Alan Cox's avatar
Alan Cox committed
5190
                continue;
5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212
            } 
            rsc.hdr_list[i].bus = ha->virt_bus;
            rsc.hdr_list[i].target = i;
            rsc.hdr_list[i].lun = 0;
            rsc.hdr_list[i].cluster_type = ha->hdr[i].cluster_type;
            if (ha->hdr[i].cluster_type & CLUSTER_DRIVE) { 
                cmd.Service = CACHESERVICE;
                cmd.OpCode = GDT_CLUST_INFO;
                cmd.u.cache.DeviceNo = i;
#if LINUX_VERSION_CODE >= 0x020503
                gdth_do_req(srp, &cmd, cmnd, 30);
                if (srp->sr_command->SCp.Status == S_OK)
                    rsc.hdr_list[i].cluster_type = srp->sr_command->SCp.Message;
#elif LINUX_VERSION_CODE >= 0x020322
                gdth_do_cmd(scp, &cmd, cmnd, 30);
                if (scp->SCp.Status == S_OK)
                    rsc.hdr_list[i].cluster_type = scp->SCp.Message;
#else
                gdth_do_cmd(&scp, &cmd, cmnd, 30);
                if (scp.SCp.Status == S_OK)
                    rsc.hdr_list[i].cluster_type = scp.SCp.Message;
#endif
Alan Cox's avatar
Alan Cox committed
5213 5214
            }
        } 
5215 5216 5217 5218 5219 5220 5221 5222 5223
#if LINUX_VERSION_CODE >= 0x020503
        scsi_release_request(srp);
        scsi_free_host_dev(sdev);
#elif LINUX_VERSION_CODE >= 0x020322
        scsi_release_command(scp);
        scsi_free_host_dev(sdev);
#endif       
 
        if (copy_to_user((char *)arg, &rsc, sizeof(gdth_ioctl_rescan)))
Alan Cox's avatar
Alan Cox committed
5224
            return -EFAULT;
5225 5226
        return 0;
}
Alan Cox's avatar
Alan Cox committed
5227

5228 5229
static int ioc_rescan(unsigned long arg, char *cmnd)
{
Alan Cox's avatar
Alan Cox committed
5230 5231 5232 5233 5234
        gdth_ioctl_rescan rsc;
        gdth_cmd_str cmd;
        ushort i, status, hdr_cnt;
        ulong32 info;
        int hanum, cyls, hds, secs;
5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246
	ulong flags;
	gdth_ha_str *ha; 
#if LINUX_VERSION_CODE >= 0x020503
	Scsi_Request *srp;
	Scsi_Device *sdev;
#elif LINUX_VERSION_CODE >= 0x020322
	Scsi_Cmnd *scp;
	Scsi_Device *sdev;
#else
	Scsi_Cmnd scp;
	Scsi_Device sdev;
#endif
Alan Cox's avatar
Alan Cox committed
5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410
        
        if (copy_from_user(&rsc, (char *)arg, sizeof(gdth_ioctl_rescan)) ||
            rsc.ionode >= gdth_ctr_count)
            return -EFAULT;
        hanum = rsc.ionode;
        ha = HADATA(gdth_ctr_tab[hanum]);

#if LINUX_VERSION_CODE >= 0x020503
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
        srp  = scsi_allocate_request(sdev);
        if (!srp)
            return -ENOMEM;
        srp->sr_cmd_len = 12;
        srp->sr_use_sg = 0;
#elif LINUX_VERSION_CODE >= 0x020322
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
        scp  = scsi_allocate_device(sdev, 1, FALSE);
        if (!scp)
            return -ENOMEM;
        scp->cmd_len = 12;
        scp->use_sg = 0;
#else
        memset(&sdev,0,sizeof(Scsi_Device));
        memset(&scp, 0,sizeof(Scsi_Cmnd));
        sdev.host = scp.host = gdth_ctr_tab[hanum];
        sdev.id = scp.target = sdev.host->this_id;
        scp.device = &sdev;
#endif
     
        if (rsc.flag == 0) {
            /* old method: re-init. cache service */
            cmd.Service = CACHESERVICE;
            cmd.OpCode = GDT_INIT;
            cmd.u.cache.DeviceNo = LINUX_OS;
#if LINUX_VERSION_CODE >= 0x020503
            gdth_do_req(srp, &cmd, cmnd, 30);
            status = (ushort)srp->sr_command->SCp.Status;
            info = (ulong32)srp->sr_command->SCp.Message;
#elif LINUX_VERSION_CODE >= 0x020322
            gdth_do_cmd(scp, &cmd, cmnd, 30);
            status = (ushort)scp->SCp.Status;
            info = (ulong32)scp->SCp.Message;
#else
            gdth_do_cmd(&scp, &cmd, cmnd, 30);
            status = (ushort)scp.SCp.Status;
            info = (ulong32)scp.SCp.Message;
#endif
            i = 0;
            hdr_cnt = (status == S_OK ? (ushort)info : 0);
        } else {
            i = rsc.hdr_no;
            hdr_cnt = i + 1;
        }
        for (; i < hdr_cnt && i < MAX_HDRIVES; ++i) {
            cmd.Service = CACHESERVICE;
            cmd.OpCode = GDT_INFO;
            cmd.u.cache.DeviceNo = i;
#if LINUX_VERSION_CODE >= 0x020503
            gdth_do_req(srp, &cmd, cmnd, 30);
            status = (ushort)srp->sr_command->SCp.Status;
            info = (ulong32)srp->sr_command->SCp.Message;
#elif LINUX_VERSION_CODE >= 0x020322
            gdth_do_cmd(scp, &cmd, cmnd, 30);
            status = (ushort)scp->SCp.Status;
            info = (ulong32)scp->SCp.Message;
#else
            gdth_do_cmd(&scp, &cmd, cmnd, 30);
            status = (ushort)scp.SCp.Status;
            info = (ulong32)scp.SCp.Message;
#endif
            GDTH_LOCK_HA(ha, flags);
            rsc.hdr_list[i].bus = ha->virt_bus;
            rsc.hdr_list[i].target = i;
            rsc.hdr_list[i].lun = 0;
            if (status != S_OK) {
                ha->hdr[i].present = FALSE;
            } else {
                ha->hdr[i].present = TRUE;
                ha->hdr[i].size = info;
                /* evaluate mapping */
                ha->hdr[i].size &= ~SECS32;
                gdth_eval_mapping(ha->hdr[i].size,&cyls,&hds,&secs); 
                ha->hdr[i].heads = hds;
                ha->hdr[i].secs = secs;
                /* round size */
                ha->hdr[i].size = cyls * hds * secs;
            }
            GDTH_UNLOCK_HA(ha, flags);
            if (status != S_OK)
                continue; 

            /* devtype, cluster info, R/W attribs */
            cmd.Service = CACHESERVICE;
            cmd.OpCode = GDT_DEVTYPE;
            cmd.u.cache.DeviceNo = i;
#if LINUX_VERSION_CODE >= 0x020503
            gdth_do_req(srp, &cmd, cmnd, 30);
            status = (ushort)srp->sr_command->SCp.Status;
            info = (ulong32)srp->sr_command->SCp.Message;
#elif LINUX_VERSION_CODE >= 0x020322
            gdth_do_cmd(scp, &cmd, cmnd, 30);
            status = (ushort)scp->SCp.Status;
            info = (ulong32)scp->SCp.Message;
#else
            gdth_do_cmd(&scp, &cmd, cmnd, 30);
            status = (ushort)scp.SCp.Status;
            info = (ulong32)scp.SCp.Message;
#endif
            GDTH_LOCK_HA(ha, flags);
            ha->hdr[i].devtype = (status == S_OK ? (ushort)info : 0);
            GDTH_UNLOCK_HA(ha, flags);

            cmd.Service = CACHESERVICE;
            cmd.OpCode = GDT_CLUST_INFO;
            cmd.u.cache.DeviceNo = i;
#if LINUX_VERSION_CODE >= 0x020503
            gdth_do_req(srp, &cmd, cmnd, 30);
            status = (ushort)srp->sr_command->SCp.Status;
            info = (ulong32)srp->sr_command->SCp.Message;
#elif LINUX_VERSION_CODE >= 0x020322
            gdth_do_cmd(scp, &cmd, cmnd, 30);
            status = (ushort)scp->SCp.Status;
            info = (ulong32)scp->SCp.Message;
#else
            gdth_do_cmd(&scp, &cmd, cmnd, 30);
            status = (ushort)scp.SCp.Status;
            info = (ulong32)scp.SCp.Message;
#endif
            GDTH_LOCK_HA(ha, flags);
            ha->hdr[i].cluster_type = 
                ((status == S_OK && !shared_access) ? (ushort)info : 0);
            GDTH_UNLOCK_HA(ha, flags);
            rsc.hdr_list[i].cluster_type = ha->hdr[i].cluster_type;

            cmd.Service = CACHESERVICE;
            cmd.OpCode = GDT_RW_ATTRIBS;
            cmd.u.cache.DeviceNo = i;
#if LINUX_VERSION_CODE >= 0x020503
            gdth_do_req(srp, &cmd, cmnd, 30);
            status = (ushort)srp->sr_command->SCp.Status;
            info = (ulong32)srp->sr_command->SCp.Message;
#elif LINUX_VERSION_CODE >= 0x020322
            gdth_do_cmd(scp, &cmd, cmnd, 30);
            status = (ushort)scp->SCp.Status;
            info = (ulong32)scp->SCp.Message;
#else
            gdth_do_cmd(&scp, &cmd, cmnd, 30);
            status = (ushort)scp.SCp.Status;
            info = (ulong32)scp.SCp.Message;
#endif
            GDTH_LOCK_HA(ha, flags);
            ha->hdr[i].rw_attribs = (status == S_OK ? (ushort)info : 0);
            GDTH_UNLOCK_HA(ha, flags);
        }
#if LINUX_VERSION_CODE >= 0x020503
        scsi_release_request(srp);
        scsi_free_host_dev(sdev);
#elif LINUX_VERSION_CODE >= 0x020322
        scsi_release_command(scp);
        scsi_free_host_dev(sdev);
#endif       
 
        if (copy_to_user((char *)arg, &rsc, sizeof(gdth_ioctl_rescan)))
            return -EFAULT;
5411 5412
        return 0;
}
Alan Cox's avatar
Alan Cox committed
5413
  
5414 5415 5416 5417
static int gdth_ioctl(struct inode *inode, struct file *filep,
                      unsigned int cmd, unsigned long arg)
{
    gdth_ha_str *ha; 
Alan Cox's avatar
Alan Cox committed
5418
#if LINUX_VERSION_CODE >= 0x020503
5419 5420
    Scsi_Cmnd *scp;
    Scsi_Device *sdev;
Alan Cox's avatar
Alan Cox committed
5421
#elif LINUX_VERSION_CODE >= 0x020322
5422 5423
    Scsi_Cmnd *scp;
    Scsi_Device *sdev;
Alan Cox's avatar
Alan Cox committed
5424
#else
5425 5426
    Scsi_Cmnd scp;
    Scsi_Device sdev;
Alan Cox's avatar
Alan Cox committed
5427
#endif
5428 5429
    ulong flags;
    char cmnd[MAX_COMMAND_SIZE];   
Alan Cox's avatar
Alan Cox committed
5430

5431 5432 5433
    memset(cmnd, 0xff, 12);
    
    TRACE(("gdth_ioctl() cmd 0x%x\n", cmd));
Alan Cox's avatar
Alan Cox committed
5434
 
5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487
    switch (cmd) {
      case GDTIOCTL_CTRCNT:
      { 
        int cnt = gdth_ctr_count;
        put_user(cnt, (int *)arg);
        break;
      }

      case GDTIOCTL_DRVERS:
      { 
        int ver = (GDTH_VERSION<<8) | GDTH_SUBVERSION;
        put_user(ver, (int *)arg);
        break;
      }
      
      case GDTIOCTL_OSVERS:
      { 
        gdth_ioctl_osvers osv; 

        osv.version = (unchar)(LINUX_VERSION_CODE >> 16);
        osv.subversion = (unchar)(LINUX_VERSION_CODE >> 8);
        osv.revision = (ushort)(LINUX_VERSION_CODE & 0xff);
        copy_to_user((char *)arg, &osv, sizeof(gdth_ioctl_osvers));
        break;
      }

      case GDTIOCTL_CTRTYPE:
      { 
        gdth_ioctl_ctrtype ctrt;
        
        if (copy_from_user(&ctrt, (char *)arg, sizeof(gdth_ioctl_ctrtype)) ||
            ctrt.ionode >= gdth_ctr_count)
            return -EFAULT;
        ha = HADATA(gdth_ctr_tab[ctrt.ionode]);
        if (ha->type == GDT_ISA || ha->type == GDT_EISA) {
            ctrt.type = (unchar)((ha->stype>>20) - 0x10);
        } else {
            if (ha->type != GDT_PCIMPR) {
                ctrt.type = (unchar)((ha->stype<<4) + 6);
            } else {
                ctrt.type = 
                    (ha->oem_id == OEM_ID_INTEL ? 0xfd : 0xfe);
                if (ha->stype >= 0x300)
                    ctrt.ext_type = 0x6000 | ha->subdevice_id;
                else 
                    ctrt.ext_type = 0x6000 | ha->stype;
            }
            ctrt.device_id = ha->stype;
            ctrt.sub_device_id = ha->subdevice_id;
        }
        ctrt.info = ha->brd_phys;
        ctrt.oem_id = ha->oem_id;
        if (copy_to_user((char *)arg, &ctrt, sizeof(gdth_ioctl_ctrtype)))
Alan Cox's avatar
Alan Cox committed
5488 5489 5490
            return -EFAULT;
        break;
      }
5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538
        
      case GDTIOCTL_GENERAL:
	return ioc_general(arg, cmnd);

      case GDTIOCTL_EVENT:
	return ioc_event(arg);

      case GDTIOCTL_LOCKDRV:
	return ioc_lockdrv(arg);

      case GDTIOCTL_LOCKCHN:
      {
        gdth_ioctl_lockchn lchn;
        unchar i, j;

        if (copy_from_user(&lchn, (char *)arg, sizeof(gdth_ioctl_lockchn)) ||
            lchn.ionode >= gdth_ctr_count)
            return -EFAULT;
        ha = HADATA(gdth_ctr_tab[lchn.ionode]);
        
        i = lchn.channel;
        if (i < ha->bus_cnt) {
            if (lchn.lock) {
                GDTH_LOCK_HA(ha, flags);
                ha->raw[i].lock = 1;
                GDTH_UNLOCK_HA(ha, flags);
                for (j = 0; j < ha->tid_cnt; ++j) {
                    gdth_wait_completion(lchn.ionode, i, j); 
                    gdth_stop_timeout(lchn.ionode, i, j); 
                }
            } else {
                GDTH_LOCK_HA(ha, flags);
                ha->raw[i].lock = 0;
                GDTH_UNLOCK_HA(ha, flags);
                for (j = 0; j < ha->tid_cnt; ++j) {
                    gdth_start_timeout(lchn.ionode, i, j); 
                    gdth_next(lchn.ionode); 
                }
            }
        } 
        break;
      }

      case GDTIOCTL_RESCAN:
	return ioc_rescan(arg, cmnd);

      case GDTIOCTL_HDRLIST:
	return ioc_hdrlist(arg, cmnd);
Alan Cox's avatar
Alan Cox committed
5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596

      case GDTIOCTL_RESET_BUS:
      {
        gdth_ioctl_reset res;
        int hanum, rval;

        if (copy_from_user(&res, (char *)arg, sizeof(gdth_ioctl_reset)) ||
            res.ionode >= gdth_ctr_count)
            return -EFAULT;
        hanum = res.ionode; 

        /* Because we need a Scsi_Cmnd struct., we make a scsi_allocate device also for kernels >=2.5.x */        
#if LINUX_VERSION_CODE >= 0x02053C
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
        scp  = scsi_get_command(sdev, GFP_KERNEL);
        if (!scp)
            return -ENOMEM;
        scp->cmd_len = 12;
        scp->use_sg = 0;
        scp->device->channel = virt_ctr ? 0 : res.number;
        rval = gdth_eh_bus_reset(scp);
        res.status = (rval == SUCCESS ? S_OK : S_GENERR);
        scsi_put_command(scp);
        scsi_free_host_dev(sdev);
#elif LINUX_VERSION_CODE >= 0x020322
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
#if LINUX_VERSION_CODE >= 0x020503
        scp  = scsi_allocate_device(sdev, 1);
#else
        scp  = scsi_allocate_device(sdev, 1, FALSE);
#endif
        if (!scp)
            return -ENOMEM;
        scp->cmd_len = 12;
        scp->use_sg = 0;
        scp->channel = virt_ctr ? 0 : res.number;
        rval = gdth_eh_bus_reset(scp);
        res.status = (rval == SUCCESS ? S_OK : S_GENERR);
        scsi_release_command(scp);
        scsi_free_host_dev(sdev);
#elif LINUX_VERSION_CODE >= 0x02015F
        memset(&sdev,0,sizeof(Scsi_Device));
        memset(&scp, 0,sizeof(Scsi_Cmnd));
        sdev.host = scp.host = gdth_ctr_tab[hanum];
        sdev.id = scp.target = sdev.host->this_id;
        scp.device = &sdev;
        scp.channel = virt_ctr ? 0 : res.number;
        rval = gdth_eh_bus_reset(&scp);
        res.status = (rval == SUCCESS ? S_OK : S_GENERR);
#else
        res.status = S_OK;
#endif
        if (copy_to_user((char *)arg, &res, sizeof(gdth_ioctl_reset)))
            return -EFAULT;
        break;
      }

      case GDTIOCTL_RESET_DRV:
5597
	return ioc_resetdrv(arg, cmnd);
Alan Cox's avatar
Alan Cox committed
5598 5599 5600 5601 5602 5603 5604 5605

      default:
        break; 
    }
    return 0;
}
#endif

Linus Torvalds's avatar
Linus Torvalds committed
5606
#if LINUX_VERSION_CODE >= 0x010300
Linus Torvalds's avatar
Linus Torvalds committed
5607 5608 5609 5610 5611 5612
/* flush routine */
static void gdth_flush(int hanum)
{
    int             i;
    gdth_ha_str     *ha;
    gdth_cmd_str    gdtcmd;
Alan Cox's avatar
Alan Cox committed
5613 5614 5615 5616
#if LINUX_VERSION_CODE >= 0x020503
    Scsi_Request    *srp;
    Scsi_Device     *sdev;
#elif LINUX_VERSION_CODE >= 0x020322
Linus Torvalds's avatar
Linus Torvalds committed
5617 5618 5619 5620 5621 5622 5623
    Scsi_Cmnd       *scp;
    Scsi_Device     *sdev;
#else
    Scsi_Cmnd       scp;
    Scsi_Device     sdev;
#endif
    char            cmnd[MAX_COMMAND_SIZE];   
Linus Torvalds's avatar
Linus Torvalds committed
5624
    memset(cmnd, 0xff, MAX_COMMAND_SIZE);
Linus Torvalds's avatar
Linus Torvalds committed
5625 5626 5627 5628

    TRACE2(("gdth_flush() hanum %d\n",hanum));
    ha = HADATA(gdth_ctr_tab[hanum]);

Alan Cox's avatar
Alan Cox committed
5629
#if LINUX_VERSION_CODE >= 0x020503
Linus Torvalds's avatar
Linus Torvalds committed
5630
    sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
Alan Cox's avatar
Alan Cox committed
5631 5632 5633 5634 5635 5636 5637 5638 5639 5640
    srp  = scsi_allocate_request(sdev);
    if (!srp)
        return;
    srp->sr_cmd_len = 12;
    srp->sr_use_sg = 0;
#elif LINUX_VERSION_CODE >= 0x020322
    sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
    scp  = scsi_allocate_device(sdev, 1, FALSE);
    if (!scp)
        return;
Linus Torvalds's avatar
Linus Torvalds committed
5641 5642 5643 5644 5645 5646 5647 5648 5649
    scp->cmd_len = 12;
    scp->use_sg = 0;
#else
    memset(&sdev,0,sizeof(Scsi_Device));
    memset(&scp, 0,sizeof(Scsi_Cmnd));
    sdev.host = scp.host = gdth_ctr_tab[hanum];
    sdev.id = scp.target = sdev.host->this_id;
    scp.device = &sdev;
#endif
Linus Torvalds's avatar
Linus Torvalds committed
5650

Linus Torvalds's avatar
Linus Torvalds committed
5651 5652 5653 5654 5655 5656 5657 5658 5659
    for (i = 0; i < MAX_HDRIVES; ++i) {
        if (ha->hdr[i].present) {
            gdtcmd.BoardNode = LOCALBOARD;
            gdtcmd.Service = CACHESERVICE;
            gdtcmd.OpCode = GDT_FLUSH;
            gdtcmd.u.cache.DeviceNo = i;
            gdtcmd.u.cache.BlockNo = 1;
            gdtcmd.u.cache.sg_canz = 0;
            TRACE2(("gdth_flush(): flush ha %d drive %d\n", hanum, i));
Alan Cox's avatar
Alan Cox committed
5660 5661 5662
#if LINUX_VERSION_CODE >= 0x020503
            gdth_do_req(srp, &gdtcmd, cmnd, 30);
#elif LINUX_VERSION_CODE >= 0x020322
Linus Torvalds's avatar
Linus Torvalds committed
5663 5664 5665 5666
            gdth_do_cmd(scp, &gdtcmd, cmnd, 30);
#else
            gdth_do_cmd(&scp, &gdtcmd, cmnd, 30);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
5667 5668
        }
    }
Alan Cox's avatar
Alan Cox committed
5669 5670 5671 5672 5673
#if LINUX_VERSION_CODE >= 0x020503
    scsi_release_request(srp);
    scsi_free_host_dev(sdev);
#elif LINUX_VERSION_CODE >= 0x020322
    scsi_release_command(scp);
Linus Torvalds's avatar
Linus Torvalds committed
5674
    scsi_free_host_dev(sdev);
Linus Torvalds's avatar
Linus Torvalds committed
5675
#endif
Linus Torvalds's avatar
Linus Torvalds committed
5676 5677 5678
}

/* shutdown routine */
Linus Torvalds's avatar
Linus Torvalds committed
5679
#if LINUX_VERSION_CODE >= 0x020100
Linus Torvalds's avatar
Linus Torvalds committed
5680
static int gdth_halt(struct notifier_block *nb, ulong event, void *buf)
Linus Torvalds's avatar
Linus Torvalds committed
5681 5682 5683
#else
void gdth_halt(void)
#endif
Linus Torvalds's avatar
Linus Torvalds committed
5684 5685 5686 5687
{
    int             hanum;
#ifndef __alpha__
    gdth_cmd_str    gdtcmd;
Alan Cox's avatar
Alan Cox committed
5688 5689 5690 5691
#if LINUX_VERSION_CODE >= 0x020503
    Scsi_Request    *srp;
    Scsi_Device     *sdev;
#elif LINUX_VERSION_CODE >= 0x020322
Linus Torvalds's avatar
Linus Torvalds committed
5692 5693 5694 5695 5696 5697 5698
    Scsi_Cmnd       *scp;
    Scsi_Device     *sdev;
#else
    Scsi_Cmnd       scp;
    Scsi_Device     sdev;
#endif
    char            cmnd[MAX_COMMAND_SIZE];   
Linus Torvalds's avatar
Linus Torvalds committed
5699 5700
#endif

Linus Torvalds's avatar
Linus Torvalds committed
5701 5702
#if LINUX_VERSION_CODE >= 0x020100
    TRACE2(("gdth_halt() event %d\n",(int)event));
Linus Torvalds's avatar
Linus Torvalds committed
5703 5704
    if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
        return NOTIFY_DONE;
Linus Torvalds's avatar
Linus Torvalds committed
5705 5706 5707 5708 5709 5710 5711 5712
#else
    TRACE2(("gdth_halt()\n"));
    if (halt_called) {
        TRACE2(("already called\n"));
        return;
    }
    halt_called = TRUE;
#endif
Alan Cox's avatar
Alan Cox committed
5713

Linus Torvalds's avatar
Linus Torvalds committed
5714 5715 5716 5717 5718 5719
    printk("GDT: Flushing all host drives .. ");
    for (hanum = 0; hanum < gdth_ctr_count; ++hanum) {
        gdth_flush(hanum);

#ifndef __alpha__
        /* controller reset */
Linus Torvalds's avatar
Linus Torvalds committed
5720
        memset(cmnd, 0xff, MAX_COMMAND_SIZE);
Alan Cox's avatar
Alan Cox committed
5721 5722 5723 5724 5725
        gdtcmd.BoardNode = LOCALBOARD;
        gdtcmd.Service = CACHESERVICE;
        gdtcmd.OpCode = GDT_RESET;
        TRACE2(("gdth_halt(): reset controller %d\n", hanum));
#if LINUX_VERSION_CODE >= 0x020503
Linus Torvalds's avatar
Linus Torvalds committed
5726
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
Alan Cox's avatar
Alan Cox committed
5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751
        srp  = scsi_allocate_request(sdev);
        if (!srp) {
#if LINUX_VERSION_CODE >= 0x020100
            unregister_reboot_notifier(&gdth_notifier);
            return NOTIFY_OK;
#else
            return;
#endif
        }
        srp->sr_cmd_len = 12;
        srp->sr_use_sg = 0;
        gdth_do_req(srp, &gdtcmd, cmnd, 10);
        scsi_release_request(srp);
        scsi_free_host_dev(sdev);
#elif LINUX_VERSION_CODE >= 0x020322
        sdev = scsi_get_host_dev(gdth_ctr_tab[hanum]);
        scp  = scsi_allocate_device(sdev, 1, FALSE);
        if (!scp) {
#if LINUX_VERSION_CODE >= 0x020100
            unregister_reboot_notifier(&gdth_notifier);
            return NOTIFY_OK;
#else
            return;
#endif
        }
Linus Torvalds's avatar
Linus Torvalds committed
5752 5753
        scp->cmd_len = 12;
        scp->use_sg = 0;
Alan Cox's avatar
Alan Cox committed
5754 5755 5756
        gdth_do_cmd(scp, &gdtcmd, cmnd, 10);
        scsi_release_command(scp);
        scsi_free_host_dev(sdev);
Linus Torvalds's avatar
Linus Torvalds committed
5757 5758 5759 5760 5761 5762 5763 5764
#else
        memset(&sdev,0,sizeof(Scsi_Device));
        memset(&scp, 0,sizeof(Scsi_Cmnd));
        sdev.host = scp.host = gdth_ctr_tab[hanum];
        sdev.id = scp.target = sdev.host->this_id;
        scp.device = &sdev;
        gdth_do_cmd(&scp, &gdtcmd, cmnd, 10);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
5765 5766 5767 5768 5769 5770 5771
#endif
    }
    printk("Done.\n");

#ifdef GDTH_STATISTICS
    del_timer(&gdth_timer);
#endif
Linus Torvalds's avatar
Linus Torvalds committed
5772
#if LINUX_VERSION_CODE >= 0x020100
Linus Torvalds's avatar
Linus Torvalds committed
5773 5774
    unregister_reboot_notifier(&gdth_notifier);
    return NOTIFY_OK;
Linus Torvalds's avatar
Linus Torvalds committed
5775
#endif
Linus Torvalds's avatar
Linus Torvalds committed
5776
}
Linus Torvalds's avatar
Linus Torvalds committed
5777
#endif
Linus Torvalds's avatar
Linus Torvalds committed
5778 5779


Linus Torvalds's avatar
Linus Torvalds committed
5780 5781 5782 5783
#if LINUX_VERSION_CODE < 0x020400 && !defined(MODULE)

GDTH_INITFUNC(void, gdth_setup(char *str,int *ints)) 
{    
Linus Torvalds's avatar
Linus Torvalds committed
5784 5785
    TRACE2(("gdth_setup() str %s ints[0] %d\n", 
            str ? str:"NULL", ints ? ints[0]:0));
Linus Torvalds's avatar
Linus Torvalds committed
5786
    internal_setup(str, ints);
Linus Torvalds's avatar
Linus Torvalds committed
5787 5788
}

Linus Torvalds's avatar
Linus Torvalds committed
5789
#else
Linus Torvalds's avatar
Linus Torvalds committed
5790

5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818
static Scsi_Host_Template driver_template = {
#if LINUX_VERSION_CODE >= 0x02015F
	.proc_name		= "gdth", 
#else
	.proc_dir		= &proc_scsi_gdth,
#endif
	.proc_info		= gdth_proc_info,
	.name			= "GDT SCSI Disk Array Controller",
	.detect			= gdth_detect, 
	.release		= gdth_release,
	.info			= gdth_info, 
	.queuecommand		= gdth_queuecommand,
	.eh_abort_handler	= gdth_eh_abort, 
	.eh_device_reset_handler = gdth_eh_device_reset,
	.eh_bus_reset_handler	= gdth_eh_bus_reset,
	.eh_host_reset_handler	= gdth_eh_host_reset,
	.bios_param		= gdth_bios_param,
	.can_queue		= GDTH_MAXCMDS,
	.this_id		= -1,
	.sg_tablesize		= GDTH_MAXSG,
	.cmd_per_lun		= GDTH_MAXC_P_L,
	.unchecked_isa_dma	= 1,
	.use_clustering		= ENABLE_CLUSTERING,
#if LINUX_VERSION_CODE < 0x020501
	.use_new_eh_code	= 1,
#endif
};

Linus Torvalds's avatar
Linus Torvalds committed
5819
#include "scsi_module.c"
Linus Torvalds's avatar
Linus Torvalds committed
5820 5821 5822 5823 5824
#ifndef MODULE
__setup("gdth=", option_setup);
#endif

#endif