OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uClinux-2.0.x/] [drivers/] [scsi/] [gdth.c] - Blame information for rev 199

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/************************************************************************
2
 * GDT ISA/EISA/PCI Disk Array Controller driver for Linux              *
3
 *                                                                      *
4
 * gdth.c                                                               *
5
 * Copyright (C) 1995-98 ICP vortex Computersysteme GmbH, Achim Leubner *
6
 *                                                                      *
7
 * <achim@vortex.de>                                                    *
8
 *                                                                      *
9
 * This program is free software; you can redistribute it and/or modify *
10
 * it under the terms of the GNU General Public License as published    *
11
 * by the Free Software Foundation; either version 2 of the License,    *
12
 * or (at your option) any later version.                               *
13
 *                                                                      *
14
 * This program is distributed in the hope that it will be useful,      *
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the         *
17
 * GNU General Public License for more details.                         *
18
 *                                                                      *
19
 * You should have received a copy of the GNU General Public License    *
20
 * along with this kernel; if not, write to the Free Software           *
21
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.            *
22
 *                                                                      *
23
 * Tested with Linux 1.2.13, ..., 2.1.103                               *
24
 *                                                                      *
25
 * $Log: not supported by cvs2svn $
26
 * Revision 1.1.1.1  2001/07/02 17:58:29  simons
27
 * Initial revision
28
 *
29
 * Revision 1.16  1998/09/28 16:08:46  achim
30
 * GDT_PCIMPR: DPMEM remapping, if required
31
 * mdelay() added
32
 *
33
 * Revision 1.15  1998/06/03 14:54:06  achim
34
 * gdth_delay(), gdth_flush() implemented
35
 * Bugfix: gdth_release() changed
36
 *
37
 * Revision 1.14  1998/05/22 10:01:17  achim
38
 * mj: pcibios_strerror() removed
39
 * Improved SMP support (if version >= 2.1.95)
40
 * gdth_halt(): halt_called flag added (if version < 2.1)
41
 *
42
 * Revision 1.13  1998/04/16 09:14:57  achim
43
 * Reserve drives (for raw service) implemented
44
 * New error handling code enabled
45
 * Get controller name from board_info() IOCTL
46
 * Final round of PCI device driver patches by Martin Mares
47
 *
48
 * Revision 1.12  1998/03/03 09:32:37  achim
49
 * Fibre channel controller support added
50
 *
51
 * Revision 1.11  1998/01/27 16:19:14  achim
52
 * SA_SHIRQ added
53
 * add_timer()/del_timer() instead of GDTH_TIMER
54
 * scsi_add_timer()/scsi_del_timer() instead of SCSI_TIMER
55
 * New error handling included
56
 *
57
 * Revision 1.10  1997/10/31 12:29:57  achim
58
 * Read heads/sectors from host drive
59
 *
60
 * Revision 1.9  1997/09/04 10:07:25  achim
61
 * IO-mapping with virt_to_bus(), readb(), writeb(), ...
62
 * register_reboot_notifier() to get a notify on shutdown used
63
 *
64
 * Revision 1.8  1997/04/02 12:14:30  achim
65
 * Version 1.00 (see gdth.h), tested with kernel 2.0.29
66
 *
67
 * Revision 1.7  1997/03/12 13:33:37  achim
68
 * gdth_reset() changed, new async. events
69
 *
70
 * Revision 1.6  1997/03/04 14:01:11  achim
71
 * Shutdown routine gdth_halt() implemented
72
 *
73
 * Revision 1.5  1997/02/21 09:08:36  achim
74
 * New controller included (RP, RP1, RP2 series)
75
 * IOCTL interface implemented
76
 *
77
 * Revision 1.4  1996/07/05 12:48:55  achim
78
 * Function gdth_bios_param() implemented
79
 * New constant GDTH_MAXC_P_L inserted
80
 * GDT_WRITE_THR, GDT_EXT_INFO implemented
81
 * Function gdth_reset() changed
82
 *
83
 * Revision 1.3  1996/05/10 09:04:41  achim
84
 * Small changes for Linux 1.2.13
85
 *
86
 * Revision 1.2  1996/05/09 12:45:27  achim
87
 * Loadable module support implemented
88
 * /proc support corrections made
89
 *
90
 * Revision 1.1  1996/04/11 07:35:57  achim
91
 * Initial revision
92
 *
93
 *
94
 * $Id: gdth.c,v 1.1.1.1 2001-09-10 07:44:33 simons Exp $
95
 ************************************************************************/
96
 
97
#ifdef MODULE
98
#include <linux/module.h>
99
#endif
100
 
101
#include <linux/version.h>
102
#include <linux/kernel.h>
103
#include <linux/head.h>
104
#include <linux/types.h>
105
#include <linux/pci.h>
106
#include <linux/string.h>
107
#include <linux/ioport.h>
108
#include <linux/delay.h>
109
#include <linux/sched.h>
110
#include <linux/in.h>
111
#include <linux/proc_fs.h>
112
#include <linux/time.h>
113
#include <linux/timer.h>
114
#if LINUX_VERSION_CODE >= 0x020100
115
#include <linux/reboot.h>
116
#else
117
#include <linux/bios32.h>
118
#endif
119
 
120
#include <asm/dma.h>
121
#include <asm/system.h>
122
#include <asm/io.h>
123
#if LINUX_VERSION_CODE >= 0x02015F
124
#include <asm/spinlock.h>
125
#endif
126
 
127
#if LINUX_VERSION_CODE >= 0x010300
128
#include <linux/blk.h>
129
#else
130
#include "../block/blk.h"
131
#endif
132
#include "scsi.h"
133
#include "hosts.h"
134
#include "sd.h"
135
 
136
#include "gdth.h"
137
 
138
/****************************************************************************/
139
 
140
/* LILO params: gdth=<IRQ>
141
 *
142
 * Where: <IRQ> is any of the valid IRQs for EISA controllers (10,11,12,14)
143
 * Sets the IRQ of the GDT3000/3020 EISA controller to this value,
144
 * if the IRQ can not automat. detect (controller BIOS disabled)
145
 * See gdth_init_eisa()
146
 *
147
 * You can use the command line gdth=0 to disable the driver
148
 */
149
static unchar irqs[MAXHA] = {0xff};
150
static unchar disable_gdth_scan = FALSE;
151
 
152
/* Reserve drives for raw service: Fill the following structure with the
153
 * appropriate values: Controller number, Channel, Target ID
154
 */
155
static gdth_reserve_str reserve_list[] = {
156
    /* { 0, 1, 4 },             Example: Controller 0, Channel B, ID 4 */
157
    { 0xff, 0xff, 0xff }        /* end of list */
158
};
159
 
160
/****************************************************************************/
161
 
162
#if LINUX_VERSION_CODE >= 0x02015F
163
static void gdth_interrupt(int irq,void *dev_id,struct pt_regs *regs);
164
static void do_gdth_interrupt(int irq,void *dev_id,struct pt_regs *regs);
165
#elif LINUX_VERSION_CODE >= 0x010346
166
static void gdth_interrupt(int irq,void *dev_id,struct pt_regs *regs);
167
#else
168
static void gdth_interrupt(int irq,struct pt_regs *regs);
169
#endif
170
static int gdth_sync_event(int hanum,int service,unchar index,Scsi_Cmnd *scp);
171
static int gdth_async_event(int hanum,int service);
172
 
173
static void gdth_putq(int hanum,Scsi_Cmnd *scp,unchar priority);
174
static void gdth_next(int hanum);
175
static int gdth_fill_raw_cmd(int hanum,Scsi_Cmnd *scp,unchar b);
176
static int gdth_special_cmd(int hanum,Scsi_Cmnd *scp,unchar b);
177
static gdth_evt_str *gdth_store_event(ushort source, ushort idx,
178
                                      gdth_evt_data *evt);
179
static int gdth_read_event(int handle, gdth_evt_str *estr);
180
static void gdth_readapp_event(unchar application, gdth_evt_str *estr);
181
static void gdth_clear_events(void);
182
 
183
static void gdth_copy_internal_data(Scsi_Cmnd *scp,char *buffer,ushort count);
184
static int gdth_internal_cache_cmd(int hanum,Scsi_Cmnd *scp,
185
                                   unchar b,ulong *flags);
186
static int gdth_fill_cache_cmd(int hanum,Scsi_Cmnd *scp,ushort hdrive);
187
 
188
static int gdth_search_eisa(ushort eisa_adr);
189
static int gdth_search_isa(ulong bios_adr);
190
static int gdth_search_pci(ushort device_id,ushort index,gdth_pci_str *pcistr);
191
static int gdth_init_eisa(ushort eisa_adr,gdth_ha_str *ha);
192
static int gdth_init_isa(ulong bios_adr,gdth_ha_str *ha);
193
static int gdth_init_pci(gdth_pci_str *pcistr,gdth_ha_str *ha);
194
 
195
static void gdth_enable_int(int hanum);
196
static int gdth_get_status(unchar *pIStatus,int irq);
197
static int gdth_test_busy(int hanum);
198
static int gdth_get_cmd_index(int hanum);
199
static void gdth_release_event(int hanum);
200
static int gdth_wait(int hanum,int index,ulong time);
201
static int gdth_internal_cmd(int hanum,unchar service,ushort opcode,ulong p1,
202
                             ulong p2,ulong p3);
203
static int gdth_search_drives(int hanum);
204
 
205
static void *gdth_mmap(ulong paddr, ulong size);
206
static void gdth_munmap(void *addr);
207
 
208
static const char *gdth_ctr_name(int hanum);
209
 
210
static void gdth_flush(int hanum);
211
#if LINUX_VERSION_CODE >= 0x020100
212
static int gdth_halt(struct notifier_block *nb, ulong event, void *buf);
213
#else
214
static int halt_called = FALSE;
215
void gdth_halt(void);
216
#endif
217
 
218
#ifdef DEBUG_GDTH
219
static unchar   DebugState = DEBUG_GDTH;
220
extern int sys_syslog(int,char*,int);
221
#define LOGEN           sys_syslog(7,NULL,0)
222
 
223
#ifdef __SERIAL__
224
#define MAX_SERBUF 160
225
static void ser_init(void);
226
static void ser_puts(char *str);
227
static void ser_putc(char c);
228
static int  ser_printk(const char *fmt, ...);
229
static char strbuf[MAX_SERBUF+1];
230
#ifdef __COM2__
231
#define COM_BASE 0x2f8
232
#else
233
#define COM_BASE 0x3f8
234
#endif
235
static void ser_init()
236
{
237
    unsigned port=COM_BASE;
238
 
239
    outb(0x80,port+3);
240
    outb(0,port+1);
241
    /* 19200 Baud, if 9600: outb(12,port) */
242
    outb(6, port);
243
    outb(3,port+3);
244
    outb(0,port+1);
245
    /*
246
    ser_putc('I');
247
    ser_putc(' ');
248
    */
249
}
250
 
251
static void ser_puts(char *str)
252
{
253
    char *ptr;
254
 
255
    ser_init();
256
    for (ptr=str;*ptr;++ptr)
257
        ser_putc(*ptr);
258
}
259
 
260
static void ser_putc(char c)
261
{
262
    unsigned port=COM_BASE;
263
 
264
    while ((inb(port+5) & 0x20)==0);
265
    outb(c,port);
266
    if (c==0x0a)
267
    {
268
        while ((inb(port+5) & 0x20)==0);
269
        outb(0x0d,port);
270
    }
271
}
272
 
273
static int ser_printk(const char *fmt, ...)
274
{
275
    va_list args;
276
    int i;
277
 
278
    va_start(args,fmt);
279
    i = vsprintf(strbuf,fmt,args);
280
    ser_puts(strbuf);
281
    va_end(args);
282
    return i;
283
}
284
 
285
#define TRACE(a)    {if (DebugState==1) {ser_printk a;}}
286
#define TRACE2(a)   {if (DebugState==1 || DebugState==2) {ser_printk a;}}
287
#define TRACE3(a)   {if (DebugState!=0) {ser_printk a;}}
288
 
289
#else /* !__SERIAL__ */
290
#define TRACE(a)    {if (DebugState==1) {LOGEN;printk a;}}
291
#define TRACE2(a)   {if (DebugState==1 || DebugState==2) {LOGEN;printk a;}}
292
#define TRACE3(a)   {if (DebugState!=0) {LOGEN;printk a;}}
293
#endif
294
 
295
#else /* !DEBUG */
296
#define TRACE(a)
297
#define TRACE2(a)
298
#define TRACE3(a)
299
#endif
300
 
301
#ifdef GDTH_STATISTICS
302
static ulong max_rq=0, max_index=0, max_sg=0;
303
static ulong act_ints=0, act_ios=0, act_stats=0, act_rq=0;
304
static struct timer_list gdth_timer;
305
#endif
306
 
307
#define PTR2USHORT(a)   (ushort)(ulong)(a)
308
#define GDTOFFSOF(a,b)  (size_t)&(((a*)0)->b)   
309
#define INDEX_OK(i,t)   ((i)<sizeof(t)/sizeof((t)[0]))
310
 
311
#define NUMDATA(a)      ( (gdth_num_str  *)((a)->hostdata))
312
#define HADATA(a)       (&((gdth_ext_str *)((a)->hostdata))->haext)
313
#define CMDDATA(a)      (&((gdth_ext_str *)((a)->hostdata))->cmdext)
314
#define DMADATA(a)      (&((gdth_ext_str *)((a)->hostdata))->dmaext)
315
 
316
 
317
#if LINUX_VERSION_CODE < 0x010300
318
static void *gdth_mmap(ulong paddr, ulong size)
319
{
320
    if (paddr >= high_memory)
321
        return NULL;
322
    else
323
        return (void *)paddr;
324
}
325
static void gdth_munmap(void *addr)
326
{
327
}
328
inline ulong virt_to_phys(volatile void *addr)
329
{
330
    return (ulong)addr;
331
}
332
inline void *phys_to_virt(ulong addr)
333
{
334
    return (void *)addr;
335
}
336
#define virt_to_bus             virt_to_phys
337
#define bus_to_virt             phys_to_virt
338
#define readb(addr)             (*(volatile unchar *)(addr))
339
#define readw(addr)             (*(volatile ushort *)(addr))
340
#define readl(addr)             (*(volatile ulong *)(addr))
341
#define writeb(b,addr)          (*(volatile unchar *)(addr) = (b))
342
#define writew(b,addr)          (*(volatile ushort *)(addr) = (b))
343
#define writel(b,addr)          (*(volatile ulong *)(addr) = (b))
344
#define memset_io(a,b,c)        memset((void *)(a),(b),(c))
345
#define memcpy_fromio(a,b,c)    memcpy((a),(void *)(b),(c))
346
#define memcpy_toio(a,b,c)      memcpy((void *)(a),(b),(c))
347
 
348
#elif LINUX_VERSION_CODE < 0x020100
349
static int remapped = FALSE;
350
static void *gdth_mmap(ulong paddr, ulong size)
351
{
352
    if ( paddr >= high_memory) {
353
        remapped = TRUE;
354
        return vremap(paddr, size);
355
    } else {
356
        return (void *)paddr;
357
    }
358
}
359
static void gdth_munmap(void *addr)
360
{
361
    if (remapped)
362
        vfree(addr);
363
    remapped = FALSE;
364
}
365
#else
366
static void *gdth_mmap(ulong paddr, ulong size)
367
{
368
    return ioremap(paddr, size);
369
}
370
static void gdth_munmap(void *addr)
371
{
372
    return iounmap(addr);
373
}
374
#endif
375
 
376
 
377
static unchar   gdth_drq_tab[4] = {5,6,7,7};            /* DRQ table */
378
static unchar   gdth_irq_tab[6] = {0,10,11,12,14,0};    /* IRQ table */
379
static unchar   gdth_polling;                           /* polling if TRUE */
380
static unchar   gdth_from_wait  = FALSE;                /* gdth_wait() */
381
static int      wait_index,wait_hanum;                  /* gdth_wait() */
382
static int      gdth_ctr_count  = 0;                    /* controller count */
383
static int      gdth_ctr_vcount = 0;                    /* virt. ctr. count */
384
static int      gdth_ctr_released = 0;                   /* gdth_release() */
385
static struct Scsi_Host *gdth_ctr_tab[MAXHA];           /* controller table */
386
static struct Scsi_Host *gdth_ctr_vtab[MAXHA*MAXBUS];   /* virt. ctr. table */
387
static unchar   gdth_write_through = FALSE;             /* write through */
388
static char *gdth_ioctl_tab[4][MAXHA];                  /* ioctl buffer */
389
static gdth_evt_str ebuffer[MAX_EVENTS];                /* event buffer */
390
static int elastidx;
391
static int eoldidx;
392
 
393
static struct {
394
    Scsi_Cmnd   *cmnd;                          /* pending request */
395
    ushort      service;                        /* service */
396
} gdth_cmd_tab[GDTH_MAXCMDS][MAXHA];            /* table of pend. requests */
397
 
398
#define DIN     1                               /* IN data direction */
399
#define DOU     2                               /* OUT data direction */
400
#define DNO     DIN                             /* no data transfer */
401
#define DUN     DIN                             /* unknown data direction */
402
static unchar gdth_direction_tab[0x100] = {
403
    DNO,DNO,DIN,DIN,DOU,DIN,DIN,DOU,DIN,DUN,DOU,DOU,DUN,DUN,DUN,DIN,
404
    DNO,DIN,DIN,DOU,DIN,DOU,DNO,DNO,DOU,DNO,DIN,DNO,DIN,DOU,DNO,DUN,
405
    DIN,DUN,DIN,DUN,DOU,DIN,DUN,DUN,DIN,DIN,DIN,DUN,DUN,DIN,DIN,DIN,
406
    DIN,DIN,DIN,DNO,DIN,DNO,DNO,DIN,DIN,DIN,DIN,DIN,DIN,DIN,DIN,DIN,
407
    DIN,DIN,DIN,DIN,DIN,DNO,DUN,DNO,DNO,DNO,DUN,DNO,DIN,DIN,DUN,DUN,
408
    DUN,DUN,DUN,DUN,DUN,DIN,DUN,DUN,DUN,DUN,DIN,DUN,DUN,DUN,DUN,DUN,
409
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
410
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
411
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
412
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
413
    DUN,DUN,DUN,DUN,DUN,DNO,DNO,DUN,DIN,DNO,DIN,DUN,DNO,DUN,DIN,DIN,
414
    DIN,DIN,DIN,DNO,DUN,DIN,DIN,DIN,DIN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
415
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
416
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,
417
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DOU,DUN,DUN,DUN,DUN,DUN,
418
    DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN,DUN
419
};
420
 
421
/* __initfunc, __initdata macros */
422
#if LINUX_VERSION_CODE >= 0x020126
423
#include <linux/init.h>
424
#else
425
#define __initfunc(A) A
426
#define __initdata
427
#define __init
428
#endif
429
 
430
/* /proc support */
431
#if LINUX_VERSION_CODE >= 0x010300
432
#include <linux/stat.h> 
433
struct proc_dir_entry proc_scsi_gdth = {
434
    PROC_SCSI_GDTH, 4, "gdth",
435
    S_IFDIR | S_IRUGO | S_IXUGO, 2
436
};
437
#include "gdth_proc.h"
438
#include "gdth_proc.c"
439
#endif
440
 
441
#if LINUX_VERSION_CODE >= 0x020100
442
/* notifier block to get a notify on system shutdown/halt/reboot */
443
static struct notifier_block gdth_notifier = {
444
    gdth_halt, NULL, 0
445
};
446
#endif
447
 
448
static void gdth_delay(int milliseconds)
449
{
450
    if (milliseconds == 0) {
451
        udelay(1);
452
    } else {
453
#if LINUX_VERSION_CODE >= 0x020168
454
        mdelay(milliseconds);
455
#else
456
        int i;
457
        for (i = 0; i < milliseconds; ++i)
458
            udelay(1000);
459
#endif
460
    }
461
}
462
 
463
/* controller search and initialization functions */
464
 
465
__initfunc (static int gdth_search_eisa(ushort eisa_adr))
466
{
467
    ulong id;
468
 
469
    TRACE(("gdth_search_eisa() adr. %x\n",eisa_adr));
470
    id = inl(eisa_adr+ID0REG);
471
    if (id == GDT3A_ID || id == GDT3B_ID) {     /* GDT3000A or GDT3000B */
472
        if ((inb(eisa_adr+EISAREG) & 8) == 0)
473
            return 0;                           /* not EISA configured */
474
        return 1;
475
    }
476
    if (id == GDT3_ID)                          /* GDT3000 */
477
        return 1;
478
 
479
    return 0;
480
}
481
 
482
 
483
__initfunc (static int gdth_search_isa(ulong bios_adr))
484
{
485
    void *addr;
486
    ulong id;
487
 
488
    TRACE(("gdth_search_isa() bios adr. %lx\n",bios_adr));
489
    if ((addr = gdth_mmap(bios_adr+BIOS_ID_OFFS, sizeof(ulong))) != NULL) {
490
        id = readl(addr);
491
        gdth_munmap(addr);
492
        if (id == GDT2_ID)                          /* GDT2000 */
493
            return 1;
494
    }
495
    return 0;
496
}
497
 
498
 
499
__initfunc (static int gdth_search_pci(ushort device_id,ushort index,gdth_pci_str *pcistr))
500
{
501
    int error;
502
    ulong base0,base1,base2;
503
 
504
    TRACE(("gdth_search_pci() device_id %d, index %d\n",
505
                 device_id,index));
506
 
507
#if LINUX_VERSION_CODE >= 0x20155
508
    if (!pci_present())
509
        return 0;
510
#else
511
    if (!pcibios_present())
512
        return 0;
513
#endif
514
 
515
    if (pcibios_find_device(PCI_VENDOR_ID_VORTEX,device_id,index,
516
                             &pcistr->bus,&pcistr->device_fn))
517
        return 0;
518
 
519
    /* GDT PCI controller found, now read resources from config space */
520
#if LINUX_VERSION_CODE >= 0x20155
521
    {
522
        struct pci_dev *pdev = pci_find_slot(pcistr->bus, pcistr->device_fn);
523
        base0 = pdev->base_address[0];
524
        base1 = pdev->base_address[1];
525
        base2 = pdev->base_address[2];
526
        if ((error = pcibios_read_config_dword(pcistr->bus,pcistr->device_fn,
527
                                               PCI_ROM_ADDRESS,
528
                                               (int *) &pcistr->bios))) {
529
            printk("GDT-PCI: error %d reading configuration space", error);
530
            return -1;
531
        }
532
        pcistr->irq = pdev->irq;
533
    }
534
#else
535
#if LINUX_VERSION_CODE >= 0x010300
536
#define GDTH_BASEP      (int *)
537
#else
538
#define GDTH_BASEP
539
#endif
540
    if ((error = pcibios_read_config_dword(pcistr->bus,pcistr->device_fn,
541
                                           PCI_BASE_ADDRESS_0,
542
                                           GDTH_BASEP&base0)) ||
543
        (error = pcibios_read_config_dword(pcistr->bus,pcistr->device_fn,
544
                                           PCI_BASE_ADDRESS_1,
545
                                           GDTH_BASEP&base1)) ||
546
        (error = pcibios_read_config_dword(pcistr->bus,pcistr->device_fn,
547
                                           PCI_BASE_ADDRESS_2,
548
                                           GDTH_BASEP&base2)) ||
549
        (error = pcibios_read_config_dword(pcistr->bus,pcistr->device_fn,
550
                                           PCI_ROM_ADDRESS,
551
                                           GDTH_BASEP&pcistr->bios)) ||
552
        (error = pcibios_read_config_byte(pcistr->bus,pcistr->device_fn,
553
                                          PCI_INTERRUPT_LINE,&pcistr->irq))) {
554
        printk("GDT-PCI: error %d reading configuration space", error);
555
        return -1;
556
    }
557
#endif
558
 
559
    pcistr->device_id = device_id;
560
    if (device_id <= PCI_DEVICE_ID_VORTEX_GDT6000B ||   /* GDT6000 or GDT6000B */
561
        device_id >= PCI_DEVICE_ID_VORTEX_GDT6x17RP) {  /* MPR */
562
        if ((base0 & PCI_BASE_ADDRESS_SPACE)!=PCI_BASE_ADDRESS_SPACE_MEMORY)
563
            return -1;
564
        pcistr->dpmem = base0 & PCI_BASE_ADDRESS_MEM_MASK;
565
    } else {                                    /* GDT6110, GDT6120, .. */
566
        if ((base0 & PCI_BASE_ADDRESS_SPACE)!=PCI_BASE_ADDRESS_SPACE_MEMORY ||
567
            (base2 & PCI_BASE_ADDRESS_SPACE)!=PCI_BASE_ADDRESS_SPACE_MEMORY ||
568
            (base1 & PCI_BASE_ADDRESS_SPACE)!=PCI_BASE_ADDRESS_SPACE_IO)
569
            return -1;
570
        pcistr->dpmem = base2 & PCI_BASE_ADDRESS_MEM_MASK;
571
        pcistr->io_mm = base0 & PCI_BASE_ADDRESS_MEM_MASK;
572
        pcistr->io    = base1 & PCI_BASE_ADDRESS_IO_MASK;
573
    }
574
    return 1;
575
}
576
 
577
 
578
__initfunc (static int gdth_init_eisa(ushort eisa_adr,gdth_ha_str *ha))
579
{
580
    ulong retries,id;
581
    unchar prot_ver,eisacf,i,irq_found;
582
 
583
    TRACE(("gdth_init_eisa() adr. %x\n",eisa_adr));
584
 
585
    /* disable board interrupts, deinitialize services */
586
    outb(0xff,eisa_adr+EDOORREG);
587
    outb(0x00,eisa_adr+EDENABREG);
588
    outb(0x00,eisa_adr+EINTENABREG);
589
 
590
    outb(0xff,eisa_adr+LDOORREG);
591
    retries = INIT_RETRIES;
592
    gdth_delay(20);
593
    while (inb(eisa_adr+EDOORREG) != 0xff) {
594
        if (--retries == 0) {
595
            printk("GDT-EISA: Initialization error (DEINIT failed)\n");
596
            return 0;
597
        }
598
        gdth_delay(1);
599
        TRACE2(("wait for DEINIT: retries=%ld\n",retries));
600
    }
601
    prot_ver = inb(eisa_adr+MAILBOXREG);
602
    outb(0xff,eisa_adr+EDOORREG);
603
    if (prot_ver != PROTOCOL_VERSION) {
604
        printk("GDT-EISA: Illegal protocol version\n");
605
        return 0;
606
    }
607
    ha->bmic = eisa_adr;
608
    ha->brd_phys = (ulong)eisa_adr >> 12;
609
 
610
    outl(0,eisa_adr+MAILBOXREG);
611
    outl(0,eisa_adr+MAILBOXREG+4);
612
    outl(0,eisa_adr+MAILBOXREG+8);
613
    outl(0,eisa_adr+MAILBOXREG+12);
614
 
615
    /* detect IRQ */
616
    if ((id = inl(eisa_adr+ID0REG)) == GDT3_ID) {
617
        ha->type = GDT_EISA;
618
        ha->stype = id;
619
        outl(1,eisa_adr+MAILBOXREG+8);
620
        outb(0xfe,eisa_adr+LDOORREG);
621
        retries = INIT_RETRIES;
622
        gdth_delay(20);
623
        while (inb(eisa_adr+EDOORREG) != 0xfe) {
624
            if (--retries == 0) {
625
                printk("GDT-EISA: Initialization error (get IRQ failed)\n");
626
                return 0;
627
            }
628
            gdth_delay(1);
629
        }
630
        ha->irq = inb(eisa_adr+MAILBOXREG);
631
        outb(0xff,eisa_adr+EDOORREG);
632
        TRACE2(("GDT3000/3020: IRQ=%d\n",ha->irq));
633
        /* check the result */
634
        if (ha->irq == 0) {
635
            TRACE2(("Unknown IRQ, check IRQ table from cmd line !\n"));
636
            for (i=0,irq_found=FALSE; i<MAXHA && irqs[i]!=0xff; ++i) {
637
                if (irqs[i]!=0) {
638
                    irq_found=TRUE;
639
                    break;
640
                }
641
            }
642
            if (irq_found) {
643
                ha->irq = irqs[i];
644
                irqs[i] = 0;
645
                printk("GDT-EISA: Can not detect controller IRQ,\n");
646
                printk("Use IRQ setting from command line (IRQ = %d)\n",
647
                       ha->irq);
648
            } else {
649
                printk("GDT-EISA: Initialization error (unknown IRQ), Enable\n");
650
                printk("the controller BIOS or use command line parameters\n");
651
                return 0;
652
            }
653
        }
654
    } else {
655
        eisacf = inb(eisa_adr+EISAREG) & 7;
656
        if (eisacf > 4)                         /* level triggered */
657
            eisacf -= 4;
658
        ha->irq = gdth_irq_tab[eisacf];
659
        ha->type = GDT_EISA;
660
        ha->stype= id;
661
    }
662
    return 1;
663
}
664
 
665
 
666
__initfunc (static int gdth_init_isa(ulong bios_adr,gdth_ha_str *ha))
667
{
668
    register gdt2_dpram_str *dp2_ptr;
669
    int i;
670
    unchar irq_drq,prot_ver;
671
    ulong retries;
672
 
673
    TRACE(("gdth_init_isa() bios adr. %lx\n",bios_adr));
674
 
675
    ha->brd = gdth_mmap(bios_adr, sizeof(gdt2_dpram_str));
676
    if (ha->brd == NULL) {
677
        printk("GDT-ISA: Initialization error (DPMEM remap error)\n");
678
        return 0;
679
    }
680
    dp2_ptr = (gdt2_dpram_str *)ha->brd;
681
    writeb(1, &dp2_ptr->io.memlock);                    /* switch off write protection */
682
    /* reset interface area */
683
    memset_io((char *)&dp2_ptr->u,0,sizeof(dp2_ptr->u));
684
    if (readl(&dp2_ptr->u) != 0) {
685
        printk("GDT-PCI: Initialization error (DPMEM write error)\n");
686
        gdth_munmap(ha->brd);
687
        return 0;
688
    }
689
 
690
    /* disable board interrupts, read DRQ and IRQ */
691
    writeb(0xff, &dp2_ptr->io.irqdel);
692
    writeb(0x00, &dp2_ptr->io.irqen);
693
    writeb(0x00, &dp2_ptr->u.ic.S_Status);
694
    writeb(0x00, &dp2_ptr->u.ic.Cmd_Index);
695
 
696
    irq_drq = readb(&dp2_ptr->io.rq);
697
    for (i=0; i<3; ++i) {
698
        if ((irq_drq & 1)==0)
699
            break;
700
        irq_drq >>= 1;
701
    }
702
    ha->drq = gdth_drq_tab[i];
703
 
704
    irq_drq = readb(&dp2_ptr->io.rq) >> 3;
705
    for (i=1; i<5; ++i) {
706
        if ((irq_drq & 1)==0)
707
            break;
708
        irq_drq >>= 1;
709
    }
710
    ha->irq = gdth_irq_tab[i];
711
 
712
    /* deinitialize services */
713
    writel(bios_adr, &dp2_ptr->u.ic.S_Info[0]);
714
    writeb(0xff, &dp2_ptr->u.ic.S_Cmd_Indx);
715
    writeb(0, &dp2_ptr->io.event);
716
    retries = INIT_RETRIES;
717
    gdth_delay(20);
718
    while (readb(&dp2_ptr->u.ic.S_Status) != 0xff) {
719
        if (--retries == 0) {
720
            printk("GDT-ISA: Initialization error (DEINIT failed)\n");
721
            gdth_munmap(ha->brd);
722
            return 0;
723
        }
724
        gdth_delay(1);
725
    }
726
    prot_ver = (unchar)readl(&dp2_ptr->u.ic.S_Info[0]);
727
    writeb(0, &dp2_ptr->u.ic.Status);
728
    writeb(0xff, &dp2_ptr->io.irqdel);
729
    if (prot_ver != PROTOCOL_VERSION) {
730
        printk("GDT-ISA: Illegal protocol version\n");
731
        gdth_munmap(ha->brd);
732
        return 0;
733
    }
734
 
735
    ha->type = GDT_ISA;
736
    ha->ic_all_size = sizeof(dp2_ptr->u);
737
    ha->stype= GDT2_ID;
738
    ha->brd_phys = bios_adr >> 4;
739
 
740
    /* special request to controller BIOS */
741
    writel(0x00, &dp2_ptr->u.ic.S_Info[0]);
742
    writel(0x00, &dp2_ptr->u.ic.S_Info[1]);
743
    writel(0x01, &dp2_ptr->u.ic.S_Info[2]);
744
    writel(0x00, &dp2_ptr->u.ic.S_Info[3]);
745
    writeb(0xfe, &dp2_ptr->u.ic.S_Cmd_Indx);
746
    writeb(0, &dp2_ptr->io.event);
747
    retries = INIT_RETRIES;
748
    gdth_delay(20);
749
    while (readb(&dp2_ptr->u.ic.S_Status) != 0xfe) {
750
        if (--retries == 0) {
751
            printk("GDT-ISA: Initialization error\n");
752
            gdth_munmap(ha->brd);
753
            return 0;
754
        }
755
        gdth_delay(1);
756
    }
757
    writeb(0, &dp2_ptr->u.ic.Status);
758
    writeb(0xff, &dp2_ptr->io.irqdel);
759
    return 1;
760
}
761
 
762
 
763
__initfunc (static int gdth_init_pci(gdth_pci_str *pcistr,gdth_ha_str *ha))
764
{
765
    register gdt6_dpram_str *dp6_ptr;
766
    register gdt6c_dpram_str *dp6c_ptr;
767
    register gdt6m_dpram_str *dp6m_ptr;
768
    ulong retries;
769
    unchar prot_ver;
770
    int i, found = FALSE;
771
 
772
    TRACE(("gdth_init_pci()\n"));
773
 
774
    ha->brd_phys = (pcistr->bus << 8) | (pcistr->device_fn & 0xf8);
775
    ha->stype    = (ulong)pcistr->device_id;
776
    ha->irq      = pcistr->irq;
777
 
778
    if (ha->stype <= PCI_DEVICE_ID_VORTEX_GDT6000B) {   /* GDT6000 or GDT6000B */
779
        TRACE2(("init_pci() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
780
        ha->brd = gdth_mmap(pcistr->dpmem, sizeof(gdt6_dpram_str));
781
        if (ha->brd == NULL) {
782
            printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
783
            return 0;
784
        }
785
        dp6_ptr = (gdt6_dpram_str *)ha->brd;
786
        /* reset interface area */
787
        memset_io((char *)&dp6_ptr->u,0,sizeof(dp6_ptr->u));
788
        if (readl(&dp6_ptr->u) != 0) {
789
            printk("GDT-PCI: Initialization error (DPMEM write error)\n");
790
            gdth_munmap(ha->brd);
791
            return 0;
792
        }
793
 
794
        /* disable board interrupts, deinit services */
795
        writeb(0xff, &dp6_ptr->io.irqdel);
796
        writeb(0x00, &dp6_ptr->io.irqen);;
797
        writeb(0x00, &dp6_ptr->u.ic.S_Status);
798
        writeb(0x00, &dp6_ptr->u.ic.Cmd_Index);
799
 
800
        writel(pcistr->dpmem, &dp6_ptr->u.ic.S_Info[0]);
801
        writeb(0xff, &dp6_ptr->u.ic.S_Cmd_Indx);
802
        writeb(0, &dp6_ptr->io.event);
803
        retries = INIT_RETRIES;
804
        gdth_delay(20);
805
        while (readb(&dp6_ptr->u.ic.S_Status) != 0xff) {
806
            if (--retries == 0) {
807
                printk("GDT-PCI: Initialization error (DEINIT failed)\n");
808
                gdth_munmap(ha->brd);
809
                return 0;
810
            }
811
            gdth_delay(1);
812
        }
813
        prot_ver = (unchar)readl(&dp6_ptr->u.ic.S_Info[0]);
814
        writeb(0, &dp6_ptr->u.ic.S_Status);
815
        writeb(0xff, &dp6_ptr->io.irqdel);
816
        if (prot_ver != PROTOCOL_VERSION) {
817
            printk("GDT-PCI: Illegal protocol version\n");
818
            gdth_munmap(ha->brd);
819
            return 0;
820
        }
821
 
822
        ha->type = GDT_PCI;
823
        ha->ic_all_size = sizeof(dp6_ptr->u);
824
 
825
        /* special command to controller BIOS */
826
        writel(0x00, &dp6_ptr->u.ic.S_Info[0]);
827
        writel(0x00, &dp6_ptr->u.ic.S_Info[1]);
828
        writel(0x01, &dp6_ptr->u.ic.S_Info[2]);
829
        writel(0x00, &dp6_ptr->u.ic.S_Info[3]);
830
        writeb(0xfe, &dp6_ptr->u.ic.S_Cmd_Indx);
831
        writeb(0, &dp6_ptr->io.event);
832
        retries = INIT_RETRIES;
833
        gdth_delay(20);
834
        while (readb(&dp6_ptr->u.ic.S_Status) != 0xfe) {
835
            if (--retries == 0) {
836
                printk("GDT-PCI: Initialization error\n");
837
                gdth_munmap(ha->brd);
838
                return 0;
839
            }
840
            gdth_delay(1);
841
        }
842
        writeb(0, &dp6_ptr->u.ic.S_Status);
843
        writeb(0xff, &dp6_ptr->io.irqdel);
844
 
845
    } else if (ha->stype <= PCI_DEVICE_ID_VORTEX_GDT6555) { /* GDT6110, GDT6120, .. */
846
        ha->plx = (gdt6c_plx_regs *)pcistr->io;
847
        TRACE2(("init_pci_new() dpmem %lx io %lx irq %d\n",
848
                pcistr->dpmem,(ulong)ha->plx,ha->irq));
849
        ha->brd = gdth_mmap(pcistr->dpmem, sizeof(gdt6c_dpram_str));
850
        if (ha->brd == NULL) {
851
            printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
852
            gdth_munmap(ha->brd);
853
            return 0;
854
        }
855
        dp6c_ptr = (gdt6c_dpram_str *)ha->brd;
856
        /* reset interface area */
857
        memset_io((char *)&dp6c_ptr->u,0,sizeof(dp6c_ptr->u));
858
        if (readl(&dp6c_ptr->u) != 0) {
859
            printk("GDT-PCI: Initialization error (DPMEM write error)\n");
860
            gdth_munmap(ha->brd);
861
            return 0;
862
        }
863
 
864
        /* disable board interrupts, deinit services */
865
        outb(0x00,PTR2USHORT(&ha->plx->control1));
866
        outb(0xff,PTR2USHORT(&ha->plx->edoor_reg));
867
 
868
        writeb(0x00, &dp6c_ptr->u.ic.S_Status);
869
        writeb(0x00, &dp6c_ptr->u.ic.Cmd_Index);
870
 
871
        writel(pcistr->dpmem, &dp6c_ptr->u.ic.S_Info[0]);
872
        writeb(0xff, &dp6c_ptr->u.ic.S_Cmd_Indx);
873
 
874
        outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
875
 
876
        retries = INIT_RETRIES;
877
        gdth_delay(20);
878
        while (readb(&dp6c_ptr->u.ic.S_Status) != 0xff) {
879
            if (--retries == 0) {
880
                printk("GDT-PCI: Initialization error (DEINIT failed)\n");
881
                gdth_munmap(ha->brd);
882
                return 0;
883
            }
884
            gdth_delay(1);
885
        }
886
        prot_ver = (unchar)readl(&dp6c_ptr->u.ic.S_Info[0]);
887
        writeb(0, &dp6c_ptr->u.ic.Status);
888
        if (prot_ver != PROTOCOL_VERSION) {
889
            printk("GDT-PCI: Illegal protocol version\n");
890
            gdth_munmap(ha->brd);
891
            return 0;
892
        }
893
 
894
        ha->type = GDT_PCINEW;
895
        ha->ic_all_size = sizeof(dp6c_ptr->u);
896
 
897
        /* special command to controller BIOS */
898
        writel(0x00, &dp6c_ptr->u.ic.S_Info[0]);
899
        writel(0x00, &dp6c_ptr->u.ic.S_Info[1]);
900
        writel(0x01, &dp6c_ptr->u.ic.S_Info[2]);
901
        writel(0x00, &dp6c_ptr->u.ic.S_Info[3]);
902
        writeb(0xfe, &dp6c_ptr->u.ic.S_Cmd_Indx);
903
 
904
        outb(1,PTR2USHORT(&ha->plx->ldoor_reg));
905
 
906
        retries = INIT_RETRIES;
907
        gdth_delay(20);
908
        while (readb(&dp6c_ptr->u.ic.S_Status) != 0xfe) {
909
            if (--retries == 0) {
910
                printk("GDT-PCI: Initialization error\n");
911
                gdth_munmap(ha->brd);
912
                return 0;
913
            }
914
            gdth_delay(1);
915
        }
916
        writeb(0, &dp6c_ptr->u.ic.S_Status);
917
 
918
    } else {                                            /* MPR */
919
        TRACE2(("init_pci_mpr() dpmem %lx irq %d\n",pcistr->dpmem,ha->irq));
920
        ha->brd = gdth_mmap(pcistr->dpmem, sizeof(gdt6m_dpram_str));
921
        if (ha->brd == NULL) {
922
            printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
923
            return 0;
924
        }
925
 
926
        /* check and reset interface area */
927
        dp6m_ptr = (gdt6m_dpram_str *)ha->brd;
928
        writel(DPMEM_MAGIC, &dp6m_ptr->u);
929
        if (readl(&dp6m_ptr->u) != DPMEM_MAGIC) {
930
            printk("GDT-PCI: Cannot access DPMEM at 0x%x (shadowed?)\n",
931
                   (int)ha->brd);
932
            found = FALSE;
933
            for (i = 0xC8000; i < 0xE8000; i += 0x4000) {
934
                pcibios_write_config_dword( pcistr->bus, pcistr->device_fn,
935
                                            PCI_BASE_ADDRESS_0, i );
936
                gdth_munmap( ha->brd );
937
                ha->brd = gdth_mmap(i, sizeof(gdt6m_dpram_str));
938
                if (ha->brd == NULL) {
939
                    printk("GDT-PCI: Initialization error (DPMEM remap error)\n");
940
                    return 0;
941
                }
942
                dp6m_ptr = (gdt6m_dpram_str *)ha->brd;
943
                writel(DPMEM_MAGIC, &dp6m_ptr->u);
944
                if (readl(&dp6m_ptr->u) == DPMEM_MAGIC) {
945
                    printk("GDT-PCI: Use free address at 0x%x\n",
946
                           (int)ha->brd);
947
                    found = TRUE;
948
                    break;
949
                }
950
            }
951
            if (!found) {
952
                printk("GDT-PCI: No free address found!\n");
953
                gdth_munmap( ha->brd );
954
                return 0;
955
            }
956
        }
957
        memset_io((char *)&dp6m_ptr->u,0,sizeof(dp6m_ptr->u));
958
 
959
        /* disable board interrupts, deinit services */
960
        writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) | 4,
961
               &dp6m_ptr->i960r.edoor_en_reg);
962
        writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
963
        writeb(0x00, &dp6m_ptr->u.ic.S_Status);
964
        writeb(0x00, &dp6m_ptr->u.ic.Cmd_Index);
965
 
966
        writel(pcistr->dpmem, &dp6m_ptr->u.ic.S_Info[0]);
967
        writeb(0xff, &dp6m_ptr->u.ic.S_Cmd_Indx);
968
        writeb(1, &dp6m_ptr->i960r.ldoor_reg);
969
        retries = INIT_RETRIES;
970
        gdth_delay(20);
971
        while (readb(&dp6m_ptr->u.ic.S_Status) != 0xff) {
972
            if (--retries == 0) {
973
                printk("GDT-PCI: Initialization error (DEINIT failed)\n");
974
                gdth_munmap(ha->brd);
975
                return 0;
976
            }
977
            gdth_delay(1);
978
        }
979
        prot_ver = (unchar)readl(&dp6m_ptr->u.ic.S_Info[0]);
980
        writeb(0, &dp6m_ptr->u.ic.S_Status);
981
        if (prot_ver != PROTOCOL_VERSION) {
982
            printk("GDT-PCI: Illegal protocol version\n");
983
            gdth_munmap(ha->brd);
984
            return 0;
985
        }
986
 
987
        ha->type = GDT_PCIMPR;
988
        ha->ic_all_size = sizeof(dp6m_ptr->u);
989
 
990
        /* special command to controller BIOS */
991
        writel(0x00, &dp6m_ptr->u.ic.S_Info[0]);
992
        writel(0x00, &dp6m_ptr->u.ic.S_Info[1]);
993
        writel(0x01, &dp6m_ptr->u.ic.S_Info[2]);
994
        writel(0x00, &dp6m_ptr->u.ic.S_Info[3]);
995
        writeb(0xfe, &dp6m_ptr->u.ic.S_Cmd_Indx);
996
        writeb(1, &dp6m_ptr->i960r.ldoor_reg);
997
        retries = INIT_RETRIES;
998
        gdth_delay(20);
999
        while (readb(&dp6m_ptr->u.ic.S_Status) != 0xfe) {
1000
            if (--retries == 0) {
1001
                printk("GDT-PCI: Initialization error\n");
1002
                gdth_munmap(ha->brd);
1003
                return 0;
1004
            }
1005
            gdth_delay(1);
1006
        }
1007
        writeb(0, &dp6m_ptr->u.ic.S_Status);
1008
    }
1009
 
1010
    return 1;
1011
}
1012
 
1013
 
1014
/* controller protocol functions */
1015
 
1016
__initfunc (static void gdth_enable_int(int hanum))
1017
{
1018
    gdth_ha_str *ha;
1019
    ulong flags;
1020
    gdt2_dpram_str *dp2_ptr;
1021
    gdt6_dpram_str *dp6_ptr;
1022
    gdt6m_dpram_str *dp6m_ptr;
1023
 
1024
    TRACE(("gdth_enable_int() hanum %d\n",hanum));
1025
    ha = HADATA(gdth_ctr_tab[hanum]);
1026
 
1027
    save_flags(flags);
1028
    cli();
1029
 
1030
    if (ha->type == GDT_EISA) {
1031
        outb(0xff, ha->bmic + EDOORREG);
1032
        outb(0xff, ha->bmic + EDENABREG);
1033
        outb(0x01, ha->bmic + EINTENABREG);
1034
    } else if (ha->type == GDT_ISA) {
1035
        dp2_ptr = (gdt2_dpram_str *)ha->brd;
1036
        writeb(1, &dp2_ptr->io.irqdel);
1037
        writeb(0, &dp2_ptr->u.ic.Cmd_Index);
1038
        writeb(1, &dp2_ptr->io.irqen);
1039
    } else if (ha->type == GDT_PCI) {
1040
        dp6_ptr = (gdt6_dpram_str *)ha->brd;
1041
        writeb(1, &dp6_ptr->io.irqdel);
1042
        writeb(0, &dp6_ptr->u.ic.Cmd_Index);
1043
        writeb(1, &dp6_ptr->io.irqen);
1044
    } else if (ha->type == GDT_PCINEW) {
1045
        outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
1046
        outb(0x03, PTR2USHORT(&ha->plx->control1));
1047
    } else if (ha->type == GDT_PCIMPR) {
1048
        dp6m_ptr = (gdt6m_dpram_str *)ha->brd;
1049
        writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
1050
        writeb(readb(&dp6m_ptr->i960r.edoor_en_reg) & ~4,
1051
               &dp6m_ptr->i960r.edoor_en_reg);
1052
    }
1053
    restore_flags(flags);
1054
}
1055
 
1056
 
1057
static int gdth_get_status(unchar *pIStatus,int irq)
1058
{
1059
    register gdth_ha_str *ha;
1060
    int i;
1061
 
1062
    TRACE(("gdth_get_status() irq %d ctr_count %d\n",
1063
                 irq,gdth_ctr_count));
1064
 
1065
    *pIStatus = 0;
1066
    for (i=0; i<gdth_ctr_count; ++i) {
1067
        ha = HADATA(gdth_ctr_tab[i]);
1068
        if (ha->irq != (unchar)irq)             /* check IRQ */
1069
            continue;
1070
        if (ha->type == GDT_EISA)
1071
            *pIStatus = inb((ushort)ha->bmic + EDOORREG);
1072
        else if (ha->type == GDT_ISA)
1073
            *pIStatus = readb(&((gdt2_dpram_str *)ha->brd)->u.ic.Cmd_Index);
1074
        else if (ha->type == GDT_PCI)
1075
            *pIStatus = readb(&((gdt6_dpram_str *)ha->brd)->u.ic.Cmd_Index);
1076
        else if (ha->type == GDT_PCINEW)
1077
            *pIStatus = inb(PTR2USHORT(&ha->plx->edoor_reg));
1078
        else if (ha->type == GDT_PCIMPR)
1079
            *pIStatus = readb(&((gdt6m_dpram_str *)ha->brd)->i960r.edoor_reg);
1080
 
1081
        if (*pIStatus)
1082
            return i;                           /* board found */
1083
    }
1084
    return -1;
1085
}
1086
 
1087
 
1088
static int gdth_test_busy(int hanum)
1089
{
1090
    register gdth_ha_str *ha;
1091
    register int gdtsema0 = 0;
1092
 
1093
    TRACE(("gdth_test_busy() hanum %d\n",hanum));
1094
 
1095
    ha = HADATA(gdth_ctr_tab[hanum]);
1096
    if (ha->type == GDT_EISA)
1097
        gdtsema0 = (int)inb(ha->bmic + SEMA0REG);
1098
    else if (ha->type == GDT_ISA)
1099
        gdtsema0 = (int)readb(&((gdt2_dpram_str *)ha->brd)->u.ic.Sema0);
1100
    else if (ha->type == GDT_PCI)
1101
        gdtsema0 = (int)readb(&((gdt6_dpram_str *)ha->brd)->u.ic.Sema0);
1102
    else if (ha->type == GDT_PCINEW)
1103
        gdtsema0 = (int)inb(PTR2USHORT(&ha->plx->sema0_reg));
1104
    else if (ha->type == GDT_PCIMPR)
1105
        gdtsema0 = (int)readb(&((gdt6m_dpram_str *)ha->brd)->i960r.sema0_reg);
1106
 
1107
    return (gdtsema0 & 1);
1108
}
1109
 
1110
 
1111
static int gdth_get_cmd_index(int hanum)
1112
{
1113
    register gdth_ha_str *ha;
1114
    int i;
1115
 
1116
    TRACE(("gdth_get_cmd_index() hanum %d\n",hanum));
1117
 
1118
    ha = HADATA(gdth_ctr_tab[hanum]);
1119
    for (i=0; i<GDTH_MAXCMDS; ++i) {
1120
        if (gdth_cmd_tab[i][hanum].cmnd == UNUSED_CMND) {
1121
            gdth_cmd_tab[i][hanum].cmnd = ha->pccb->RequestBuffer;
1122
            gdth_cmd_tab[i][hanum].service = ha->pccb->Service;
1123
            ha->pccb->CommandIndex = (ulong)i+2;
1124
            return (i+2);
1125
        }
1126
    }
1127
    return 0;
1128
}
1129
 
1130
 
1131
static void gdth_set_sema0(int hanum)
1132
{
1133
    register gdth_ha_str *ha;
1134
 
1135
    TRACE(("gdth_set_sema0() hanum %d\n",hanum));
1136
 
1137
    ha = HADATA(gdth_ctr_tab[hanum]);
1138
    if (ha->type == GDT_EISA)
1139
        outb(1, ha->bmic + SEMA0REG);
1140
    else if (ha->type == GDT_ISA)
1141
        writeb(1, &((gdt2_dpram_str *)ha->brd)->u.ic.Sema0);
1142
    else if (ha->type == GDT_PCI)
1143
        writeb(1, &((gdt6_dpram_str *)ha->brd)->u.ic.Sema0);
1144
    else if (ha->type == GDT_PCINEW)
1145
        outb(1, PTR2USHORT(&ha->plx->sema0_reg));
1146
    else if (ha->type == GDT_PCIMPR)
1147
        writeb(1, &((gdt6m_dpram_str *)ha->brd)->i960r.sema0_reg);
1148
 
1149
}
1150
 
1151
 
1152
static void gdth_copy_command(int hanum)
1153
{
1154
    register gdth_ha_str *ha;
1155
    register gdth_cmd_str *cmd_ptr;
1156
    register gdt6m_dpram_str *dp6m_ptr;
1157
    register gdt6c_dpram_str *dp6c_ptr;
1158
    gdt6_dpram_str *dp6_ptr;
1159
    gdt2_dpram_str *dp2_ptr;
1160
    ushort cp_count,dp_offset,cmd_no;
1161
 
1162
    TRACE(("gdth_copy_command() hanum %d\n",hanum));
1163
 
1164
    ha = HADATA(gdth_ctr_tab[hanum]);
1165
    cp_count = ha->cmd_len;
1166
    dp_offset= ha->cmd_offs_dpmem;
1167
    cmd_no   = ha->cmd_cnt;
1168
    cmd_ptr  = ha->pccb;
1169
 
1170
    ++ha->cmd_cnt;
1171
    if (ha->type == GDT_EISA)
1172
        return;                                 /* no DPMEM, no copy */
1173
 
1174
    /* set cpcount dword aligned */
1175
    if (cp_count & 3)
1176
        cp_count += (4 - (cp_count & 3));
1177
 
1178
    ha->cmd_offs_dpmem += cp_count;
1179
 
1180
    /* set offset and service, copy command to DPMEM */
1181
    if (ha->type == GDT_ISA) {
1182
        dp2_ptr = (gdt2_dpram_str *)ha->brd;
1183
        writew(dp_offset + DPMEM_COMMAND_OFFSET,
1184
               &dp2_ptr->u.ic.comm_queue[cmd_no].offset);
1185
        writew((ushort)cmd_ptr->Service,
1186
               &dp2_ptr->u.ic.comm_queue[cmd_no].serv_id);
1187
        memcpy_toio(&dp2_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1188
    } else if (ha->type == GDT_PCI) {
1189
        dp6_ptr = (gdt6_dpram_str *)ha->brd;
1190
        writew(dp_offset + DPMEM_COMMAND_OFFSET,
1191
               &dp6_ptr->u.ic.comm_queue[cmd_no].offset);
1192
        writew((ushort)cmd_ptr->Service,
1193
               &dp6_ptr->u.ic.comm_queue[cmd_no].serv_id);
1194
        memcpy_toio(&dp6_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1195
    } else if (ha->type == GDT_PCINEW) {
1196
        dp6c_ptr = (gdt6c_dpram_str *)ha->brd;
1197
        writew(dp_offset + DPMEM_COMMAND_OFFSET,
1198
               &dp6c_ptr->u.ic.comm_queue[cmd_no].offset);
1199
        writew((ushort)cmd_ptr->Service,
1200
               &dp6c_ptr->u.ic.comm_queue[cmd_no].serv_id);
1201
        memcpy_toio(&dp6c_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1202
    } else if (ha->type == GDT_PCIMPR) {
1203
        dp6m_ptr = (gdt6m_dpram_str *)ha->brd;
1204
        writew(dp_offset + DPMEM_COMMAND_OFFSET,
1205
               &dp6m_ptr->u.ic.comm_queue[cmd_no].offset);
1206
        writew((ushort)cmd_ptr->Service,
1207
               &dp6m_ptr->u.ic.comm_queue[cmd_no].serv_id);
1208
        memcpy_toio(&dp6m_ptr->u.ic.gdt_dpr_cmd[dp_offset],cmd_ptr,cp_count);
1209
    }
1210
}
1211
 
1212
 
1213
static void gdth_release_event(int hanum)
1214
{
1215
    register gdth_ha_str *ha;
1216
 
1217
#ifdef GDTH_STATISTICS
1218
    ulong i,j;
1219
    for (i=0,j=0; j<GDTH_MAXCMDS; ++j) {
1220
        if (gdth_cmd_tab[j][hanum].cmnd != UNUSED_CMND)
1221
            ++i;
1222
    }
1223
    if (max_index < i) {
1224
        max_index = i;
1225
        TRACE3(("GDT: max_index = %d\n",(ushort)i));
1226
    }
1227
#endif
1228
 
1229
    TRACE(("gdth_release_event() hanum %d\n",hanum));
1230
    ha = HADATA(gdth_ctr_tab[hanum]);
1231
 
1232
    if (ha->pccb->OpCode == GDT_INIT)
1233
        ha->pccb->Service |= 0x80;
1234
 
1235
    if (ha->type == GDT_EISA) {
1236
        outb(ha->pccb->Service, ha->bmic + LDOORREG);
1237
        if (ha->pccb->OpCode == GDT_INIT)               /* store DMA buffer */
1238
            outl((ulong)ha->pccb, ha->bmic + MAILBOXREG);
1239
    } else if (ha->type == GDT_ISA)
1240
        writeb(0, &((gdt2_dpram_str *)ha->brd)->io.event);
1241
    else if (ha->type == GDT_PCI)
1242
        writeb(0, &((gdt6_dpram_str *)ha->brd)->io.event);
1243
    else if (ha->type == GDT_PCINEW)
1244
        outb(1, PTR2USHORT(&ha->plx->ldoor_reg));
1245
    else if (ha->type == GDT_PCIMPR)
1246
        writeb(1, &((gdt6m_dpram_str *)ha->brd)->i960r.ldoor_reg);
1247
}
1248
 
1249
 
1250
static int gdth_wait(int hanum,int index,ulong time)
1251
{
1252
    gdth_ha_str *ha;
1253
    int answer_found = FALSE;
1254
 
1255
    TRACE(("gdth_wait() hanum %d index %d time %ld\n",hanum,index,time));
1256
 
1257
    ha = HADATA(gdth_ctr_tab[hanum]);
1258
    if (index == 0)
1259
        return 1;                               /* no wait required */
1260
 
1261
    gdth_from_wait = TRUE;
1262
    do {
1263
#if LINUX_VERSION_CODE >= 0x010346
1264
        gdth_interrupt((int)ha->irq,NULL,NULL);
1265
#else
1266
        gdth_interrupt((int)ha->irq,NULL);
1267
#endif
1268
        if (wait_hanum==hanum && wait_index==index) {
1269
            answer_found = TRUE;
1270
            break;
1271
        }
1272
        gdth_delay(1);
1273
    } while (--time);
1274
    gdth_from_wait = FALSE;
1275
 
1276
    while (gdth_test_busy(hanum))
1277
        gdth_delay(0);
1278
 
1279
    return (answer_found);
1280
}
1281
 
1282
 
1283
static int gdth_internal_cmd(int hanum,unchar service,ushort opcode,ulong p1,
1284
                             ulong p2,ulong p3)
1285
{
1286
    register gdth_ha_str *ha;
1287
    register gdth_cmd_str *cmd_ptr;
1288
    int retries,index;
1289
 
1290
    TRACE2(("gdth_internal_cmd() service %d opcode %d\n",service,opcode));
1291
 
1292
    ha = HADATA(gdth_ctr_tab[hanum]);
1293
    cmd_ptr = ha->pccb;
1294
    memset((char*)cmd_ptr,0,sizeof(gdth_cmd_str));
1295
 
1296
    /* make command  */
1297
    for (retries = INIT_RETRIES;;) {
1298
        cmd_ptr->Service          = service;
1299
        cmd_ptr->RequestBuffer    = INTERNAL_CMND;
1300
        if (!(index=gdth_get_cmd_index(hanum))) {
1301
            TRACE(("GDT: No free command index found\n"));
1302
            return 0;
1303
        }
1304
        gdth_set_sema0(hanum);
1305
        cmd_ptr->OpCode           = opcode;
1306
        cmd_ptr->BoardNode        = LOCALBOARD;
1307
        if (service == CACHESERVICE) {
1308
            if (opcode == GDT_IOCTL) {
1309
                cmd_ptr->u.ioctl.subfunc = p1;
1310
                cmd_ptr->u.ioctl.channel = p2;
1311
                cmd_ptr->u.ioctl.param_size = (ushort)p3;
1312
                cmd_ptr->u.ioctl.p_param = virt_to_bus(ha->pscratch);
1313
            } else {
1314
                cmd_ptr->u.cache.DeviceNo = (ushort)p1;
1315
                cmd_ptr->u.cache.BlockNo  = p2;
1316
            }
1317
        } else if (service == SCSIRAWSERVICE) {
1318
            cmd_ptr->u.raw.direction  = p1;
1319
            cmd_ptr->u.raw.bus        = (unchar)p2;
1320
            cmd_ptr->u.raw.target     = (unchar)p3;
1321
            cmd_ptr->u.raw.lun        = 0;
1322
        }
1323
        ha->cmd_len          = sizeof(gdth_cmd_str);
1324
        ha->cmd_offs_dpmem   = 0;
1325
        ha->cmd_cnt          = 0;
1326
        gdth_copy_command(hanum);
1327
        gdth_release_event(hanum);
1328
        gdth_delay(20);
1329
        if (!gdth_wait(hanum,index,INIT_TIMEOUT)) {
1330
            printk("GDT: Initialization error (timeout service %d)\n",service);
1331
            return 0;
1332
        }
1333
        if (ha->status != S_BSY || --retries == 0)
1334
            break;
1335
        gdth_delay(1);
1336
    }
1337
 
1338
    return (ha->status != S_OK ? 0:1);
1339
}
1340
 
1341
 
1342
/* search for devices */
1343
 
1344
__initfunc (static int gdth_search_drives(int hanum))
1345
{
1346
    register gdth_ha_str *ha;
1347
    ushort cdev_cnt,i;
1348
    unchar b,t,pos_found;
1349
    ulong drv_cyls, drv_hds, drv_secs;
1350
    ulong bus_no;
1351
    gdth_getch_str *chn;
1352
    gdth_iochan_str *ioc;
1353
 
1354
    TRACE(("gdth_search_drives() hanum %d\n",hanum));
1355
    ha = HADATA(gdth_ctr_tab[hanum]);
1356
 
1357
    /* initialize controller services, at first: screen service */
1358
    if (!gdth_internal_cmd(hanum,SCREENSERVICE,GDT_INIT,0,0,0)) {
1359
        printk("GDT: Initialization error screen service (code %d)\n",
1360
               ha->status);
1361
        return 0;
1362
    }
1363
    TRACE2(("gdth_search_drives(): SCREENSERVICE initialized\n"));
1364
 
1365
    /* initialize cache service */
1366
    if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_INIT,LINUX_OS,0,0)) {
1367
        printk("GDT: Initialization error cache service (code %d)\n",
1368
               ha->status);
1369
        return 0;
1370
    }
1371
    TRACE2(("gdth_search_drives(): CACHESERVICE initialized\n"));
1372
    cdev_cnt = (ushort)ha->info;
1373
 
1374
    /* mount all cache devices */
1375
    gdth_internal_cmd(hanum,CACHESERVICE,GDT_MOUNT,0xffff,1,0);
1376
    TRACE2(("gdth_search_drives(): mountall CACHESERVICE OK\n"));
1377
 
1378
    /* initialize cache service after mountall */
1379
    if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_INIT,LINUX_OS,0,0)) {
1380
        printk("GDT: Initialization error cache service (code %d)\n",
1381
               ha->status);
1382
        return 0;
1383
    }
1384
    TRACE2(("gdth_search_drives() CACHES. init. after mountall\n"));
1385
    cdev_cnt = (ushort)ha->info;
1386
 
1387
    /* detect number of SCSI buses - try new IOCTL */
1388
    ioc = (gdth_iochan_str *)DMADATA(gdth_ctr_tab[hanum]);
1389
    ioc->version        = -1UL;
1390
    ioc->list_entries   = MAXBUS;
1391
    ioc->first_chan     = 0;
1392
    ioc->last_chan      = MAXBUS-1;
1393
    ioc->list_offset    = GDTOFFSOF(gdth_iochan_str, list[0]);
1394
    if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,GET_IOCHAN_DESC,
1395
                          INVALID_CHANNEL,sizeof(gdth_iochan_str))) {
1396
        TRACE2(("GET_IOCHAN_DESC supported!\n"));
1397
        ha->bus_cnt = ioc->chan_count;
1398
        for (bus_no = 0; bus_no < ha->bus_cnt; ++bus_no)
1399
            if (ioc->list[bus_no].proc_id < MAXID)
1400
                ha->id[bus_no][ioc->list[bus_no].proc_id].type = SIOP_DTYP;
1401
    } else {
1402
        /* old method */
1403
        chn = (gdth_getch_str *)DMADATA(gdth_ctr_tab[hanum]);
1404
        for (bus_no = 0; bus_no < MAXBUS; ++bus_no) {
1405
            chn->channel_no = bus_no;
1406
            if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,
1407
                                   SCSI_CHAN_CNT | L_CTRL_PATTERN,
1408
                                   IO_CHANNEL | INVALID_CHANNEL,
1409
                                   sizeof(gdth_getch_str))) {
1410
                if (bus_no == 0) {
1411
                    printk("GDT: Error detecting SCSI channel count (0x%x)\n",
1412
                           ha->status);
1413
                    return 0;
1414
                }
1415
                break;
1416
            }
1417
            if (chn->siop_id < MAXID)
1418
                ha->id[bus_no][chn->siop_id].type = SIOP_DTYP;
1419
        }
1420
        ha->bus_cnt = (unchar)bus_no;
1421
    }
1422
    TRACE2(("gdth_search_drives() %d SCSI channels\n",ha->bus_cnt));
1423
 
1424
    /* read cache configuration */
1425
    if (!gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,CACHE_INFO,
1426
                           INVALID_CHANNEL,sizeof(gdth_cinfo_str))) {
1427
        printk("GDT: Initialization error cache service (code %d)\n",
1428
               ha->status);
1429
        return 0;
1430
    }
1431
    ha->cpar = ((gdth_cinfo_str *)DMADATA(gdth_ctr_tab[hanum]))->cpar;
1432
    TRACE2(("gdth_search_drives() cinfo: vs %lx sta %d str %d dw %d b %d\n",
1433
            ha->cpar.version,ha->cpar.state,ha->cpar.strategy,
1434
            ha->cpar.write_back,ha->cpar.block_size));
1435
 
1436
    /* read board info, fill ctr_name[] */
1437
    if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_IOCTL,BOARD_INFO,
1438
                          INVALID_CHANNEL,sizeof(gdth_binfo_str))) {
1439
        TRACE2(("BOARD_INFO supported!\n"));
1440
        strcpy(ha->ctr_name, ((gdth_binfo_str *)DMADATA(gdth_ctr_tab[hanum]))->type_string);
1441
    } else {
1442
        strcpy(ha->ctr_name, gdth_ctr_name(hanum));
1443
    }
1444
    TRACE2(("Controller name: %s\n",ha->ctr_name));
1445
 
1446
    /* initialize raw service */
1447
    if (!gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_INIT,0,0,0)) {
1448
        printk("GDT: Initialization error raw service (code %d)\n",
1449
               ha->status);
1450
        return 0;
1451
    }
1452
    TRACE2(("gdth_search_drives(): RAWSERVICE initialized\n"));
1453
 
1454
    /* set/get features raw service (scatter/gather) */
1455
    ha->raw_feat = 0;
1456
    if (gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_SET_FEAT,SCATTER_GATHER,
1457
                          0,0)) {
1458
        TRACE2(("gdth_search_drives(): set features RAWSERVICE OK\n"));
1459
        if (gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_GET_FEAT,0,0,0)) {
1460
            TRACE2(("gdth_search_dr(): get feat RAWSERVICE %ld\n",
1461
                          ha->info));
1462
            ha->raw_feat = (ushort)ha->info;
1463
        }
1464
    }
1465
 
1466
    /* set/get features cache service (equal to raw service) */
1467
    if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_SET_FEAT,0,
1468
                          SCATTER_GATHER,0)) {
1469
        TRACE2(("gdth_search_drives(): set features CACHESERVICE OK\n"));
1470
        if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_GET_FEAT,0,0,0)) {
1471
            TRACE2(("gdth_search_dr(): get feat CACHESERV. %ld\n",
1472
                          ha->info));
1473
            ha->cache_feat = (ushort)ha->info;
1474
        }
1475
    }
1476
 
1477
    /* reserve drives for raw service */
1478
    for (i = 0; reserve_list[i].hanum != 0xff; ++i) {
1479
        if (reserve_list[i].hanum < MAXHA && reserve_list[i].hanum == hanum &&
1480
            reserve_list[i].bus < MAXBUS && reserve_list[i].id < MAXID) {
1481
            TRACE2(("gdth_search_drives(): reserve ha %d bus %d id %d\n",
1482
                    reserve_list[i].hanum, reserve_list[i].bus,
1483
                    reserve_list[i].id));
1484
            if (!gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_RESERVE,0,
1485
                                   reserve_list[i].bus, reserve_list[i].id)) {
1486
                printk("GDT: Error raw service (RESERVE, code %d)\n",
1487
                       ha->status);
1488
            }
1489
        }
1490
    }
1491
 
1492
    /* scanning for raw devices */
1493
    for (b=0; b<ha->bus_cnt; ++b) {
1494
        for (t=0; t<MAXID; ++t) {
1495
            TRACE(("gdth_search_drives() rawd. bus %d id %d\n",b,t));
1496
            if (ha->id[b][t].type != SIOP_DTYP &&
1497
                gdth_internal_cmd(hanum,SCSIRAWSERVICE,GDT_INFO,0,b,t)) {
1498
                ha->id[b][t].type = RAW_DTYP;
1499
            }
1500
        }
1501
    }
1502
 
1503
    /* scanning for cache devices */
1504
    for (i=0; i<cdev_cnt && i<MAX_HDRIVES; ++i) {
1505
        TRACE(("gdth_search_drives() cachedev. %d\n",i));
1506
        if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_INFO,i,0,0)) {
1507
            /* dynamic relation between host drive number and Bus/ID */
1508
            /* search free position */
1509
            pos_found = FALSE;
1510
            for (b=0,t=0; b<ha->bus_cnt; ++b) {
1511
                for (t=0; t<MAXID; ++t) {
1512
                    if (ha->id[b][t].type == EMPTY_DTYP) {
1513
                        pos_found = TRUE;
1514
                        break;
1515
                    }
1516
                }
1517
                if (pos_found)
1518
                    break;
1519
            }
1520
            TRACE(("gdth_search_dr() drive %d free pos at bus/id %d/%d\n",
1521
                         i,b,t));
1522
 
1523
            ha->id[b][t].type      = CACHE_DTYP;
1524
            ha->id[b][t].devtype   = 0;
1525
            ha->id[b][t].size      = ha->info;
1526
            ha->id[b][t].hostdrive = i;
1527
 
1528
            /* evaluate mapping (sectors per head, heads per cylinder) */
1529
            ha->id[b][t].size &= ~SECS32;
1530
            if (ha->info2 == 0) {
1531
                drv_cyls = ha->id[b][t].size /HEADS/SECS;
1532
                if (drv_cyls <= MAXCYLS) {
1533
                    drv_hds = HEADS;
1534
                    drv_secs= SECS;
1535
                } else {                            /* too high for 64*32 */
1536
                    drv_cyls = ha->id[b][t].size /MEDHEADS/MEDSECS;
1537
                    if (drv_cyls <= MAXCYLS) {
1538
                        drv_hds = MEDHEADS;
1539
                        drv_secs= MEDSECS;
1540
                    } else {                        /* too high for 127*63 */
1541
                        drv_cyls = ha->id[b][t].size /BIGHEADS/BIGSECS;
1542
                        drv_hds = BIGHEADS;
1543
                        drv_secs= BIGSECS;
1544
                    }
1545
                }
1546
            } else {
1547
                drv_hds = ha->info2 & 0xff;
1548
                drv_secs = (ha->info2 >> 8) & 0xff;
1549
                drv_cyls = ha->id[b][t].size /drv_hds/drv_secs;
1550
            }
1551
            ha->id[b][t].heads = (unchar)drv_hds;
1552
            ha->id[b][t].secs  = (unchar)drv_secs;
1553
            /* round size */
1554
            ha->id[b][t].size  = drv_cyls * drv_hds * drv_secs;
1555
            TRACE2(("gdth_search_dr() cdr. %d size %ld hds %ld scs %ld\n",
1556
                   i,ha->id[b][t].size,drv_hds,drv_secs));
1557
 
1558
            /* get informations about device */
1559
            if (gdth_internal_cmd(hanum,CACHESERVICE,GDT_DEVTYPE,i,
1560
                                  0,0)) {
1561
                TRACE(("gdth_search_dr() cache drive %d devtype %ld\n",
1562
                       i,ha->info));
1563
                ha->id[b][t].devtype = (ushort)ha->info;
1564
            }
1565
        }
1566
    }
1567
 
1568
    TRACE(("gdth_search_drives() OK\n"));
1569
    return 1;
1570
}
1571
 
1572
 
1573
/* command queueing/sending functions */
1574
 
1575
static void gdth_putq(int hanum,Scsi_Cmnd *scp,unchar priority)
1576
{
1577
    register gdth_ha_str *ha;
1578
    register Scsi_Cmnd *pscp;
1579
    register Scsi_Cmnd *nscp;
1580
    ulong flags;
1581
    unchar b, t;
1582
 
1583
    TRACE(("gdth_putq() priority %d\n",priority));
1584
    save_flags(flags);
1585
    cli();
1586
 
1587
    ha = HADATA(gdth_ctr_tab[hanum]);
1588
    scp->SCp.this_residual = (int)priority;
1589
    gdth_update_timeout(hanum, scp, scp->timeout_per_command * 6);
1590
#if LINUX_VERSION_CODE >= 0x020000
1591
    b = scp->channel;
1592
#else
1593
    b = NUMDATA(nscp->host)->busnum;
1594
#endif
1595
    t = scp->target;
1596
#if LINUX_VERSION_CODE >= 0x010300
1597
    if (priority >= DEFAULT_PRI && ha->id[b][t].lock) {
1598
        TRACE2(("gdth_putq(): locked IO -> update_timeout()\n"));
1599
        scp->SCp.buffers_residual = gdth_update_timeout(hanum, scp, 0);
1600
    }
1601
#endif
1602
 
1603
    if (ha->req_first==NULL) {
1604
        ha->req_first = scp;                    /* queue was empty */
1605
        scp->SCp.ptr = NULL;
1606
    } else {                                    /* queue not empty */
1607
        pscp = ha->req_first;
1608
        nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
1609
        /* priority: 0-highest,..,0xff-lowest */
1610
        while (nscp && (unchar)nscp->SCp.this_residual <= priority) {
1611
            pscp = nscp;
1612
            nscp = (Scsi_Cmnd *)pscp->SCp.ptr;
1613
        }
1614
        pscp->SCp.ptr = (char *)scp;
1615
        scp->SCp.ptr  = (char *)nscp;
1616
    }
1617
    restore_flags(flags);
1618
 
1619
#ifdef GDTH_STATISTICS
1620
    flags = 0;
1621
    for (nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
1622
        ++flags;
1623
    if (max_rq < flags) {
1624
        max_rq = flags;
1625
        TRACE3(("GDT: max_rq = %d\n",(ushort)max_rq));
1626
    }
1627
#endif
1628
}
1629
 
1630
static void gdth_next(int hanum)
1631
{
1632
    register gdth_ha_str *ha;
1633
    register Scsi_Cmnd *pscp;
1634
    register Scsi_Cmnd *nscp;
1635
    unchar b, t, next_cmd, firsttime;
1636
    ushort hdrive;
1637
    ulong flags;
1638
    int cmd_index;
1639
 
1640
    TRACE(("gdth_next() hanum %d\n",hanum));
1641
    save_flags(flags);
1642
    cli();
1643
 
1644
    ha = HADATA(gdth_ctr_tab[hanum]);
1645
    ha->cmd_cnt = ha->cmd_offs_dpmem = 0;
1646
    next_cmd = firsttime = TRUE;
1647
    cmd_index = 0;
1648
 
1649
    for (nscp = pscp = ha->req_first; nscp; nscp = (Scsi_Cmnd *)nscp->SCp.ptr) {
1650
        if (nscp != pscp && nscp != (Scsi_Cmnd *)pscp->SCp.ptr)
1651
            pscp = (Scsi_Cmnd *)pscp->SCp.ptr;
1652
#if LINUX_VERSION_CODE >= 0x020000
1653
        b = nscp->channel;
1654
#else
1655
        b = NUMDATA(nscp->host)->busnum;
1656
#endif
1657
        t = nscp->target;
1658
        if (nscp->SCp.this_residual < DEFAULT_PRI || !ha->id[b][t].lock) {
1659
 
1660
            if (firsttime) {
1661
                if (gdth_test_busy(hanum)) {        /* controller busy ? */
1662
                    TRACE(("gdth_next() controller %d busy !\n",hanum));
1663
                    if (!gdth_polling) {
1664
                        restore_flags(flags);
1665
                        return;
1666
                    }
1667
                    while (gdth_test_busy(hanum))
1668
                        gdth_delay(1);
1669
                }
1670
                firsttime = FALSE;
1671
            }
1672
 
1673
#if LINUX_VERSION_CODE >= 0x010300
1674
            if (nscp->done == gdth_scsi_done) {
1675
                if (!(cmd_index=gdth_special_cmd(hanum,nscp,b)))
1676
                    next_cmd = FALSE;
1677
            } else
1678
#endif
1679
            if (ha->id[b][t].type != CACHE_DTYP) {
1680
                if (!(cmd_index=gdth_fill_raw_cmd(hanum,nscp,b)))
1681
                    next_cmd = FALSE;
1682
            } else {
1683
                hdrive = ha->id[b][t].hostdrive;
1684
                switch (nscp->cmnd[0]) {
1685
                  case TEST_UNIT_READY:
1686
                  case INQUIRY:
1687
                  case REQUEST_SENSE:
1688
                  case READ_CAPACITY:
1689
                  case VERIFY:
1690
                  case START_STOP:
1691
                  case MODE_SENSE:
1692
                    TRACE2(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
1693
                        nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
1694
                        nscp->cmnd[4],nscp->cmnd[5]));
1695
                        gdth_internal_cache_cmd(hanum,nscp,b,&flags);
1696
                    break;
1697
 
1698
                  case ALLOW_MEDIUM_REMOVAL:
1699
                    TRACE2(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
1700
                        nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
1701
                        nscp->cmnd[4],nscp->cmnd[5]));
1702
                    if ( (nscp->cmnd[4]&1) && !(ha->id[b][t].devtype&1) ) {
1703
                        TRACE2(("Prevent r. nonremov. drive->do nothing\n"));
1704
                        nscp->result = DID_OK << 16;
1705
                        restore_flags( flags );
1706
                        if (!nscp->SCp.have_data_in)
1707
                            nscp->SCp.have_data_in++;
1708
                        else
1709
                            nscp->scsi_done(nscp);
1710
                        save_flags( flags );
1711
                        cli();
1712
                    } else {
1713
                        nscp->cmnd[3] = (ha->id[b][t].devtype&1) ? 1:0;
1714
                        TRACE2(("Prevent/allow r. %d rem. drive %d\n",
1715
                            nscp->cmnd[4],nscp->cmnd[3]));
1716
                        if (!(cmd_index=gdth_fill_cache_cmd(hanum,nscp,hdrive)))
1717
                            next_cmd = FALSE;
1718
                    }
1719
                    break;
1720
 
1721
                  case READ_6:
1722
                  case WRITE_6:
1723
                  case READ_10:
1724
                  case WRITE_10:
1725
                    if (!(cmd_index=gdth_fill_cache_cmd(hanum,nscp,hdrive)))
1726
                        next_cmd = FALSE;
1727
                    break;
1728
 
1729
                  default:
1730
                    TRACE2(("cache cmd %x/%x/%x/%x/%x/%x\n",nscp->cmnd[0],
1731
                        nscp->cmnd[1],nscp->cmnd[2],nscp->cmnd[3],
1732
                        nscp->cmnd[4],nscp->cmnd[5]));
1733
                    printk("GDT: Unknown SCSI command 0x%x to cache service !\n",
1734
                       nscp->cmnd[0]);
1735
                    nscp->result = DID_ABORT << 16;
1736
                    restore_flags( flags );
1737
                    if (!nscp->SCp.have_data_in)
1738
                        nscp->SCp.have_data_in++;
1739
                    else
1740
                        nscp->scsi_done(nscp);
1741
                    save_flags( flags );
1742
                    cli();
1743
                    break;
1744
                }
1745
            }
1746
 
1747
            if (!next_cmd)
1748
                break;
1749
            if (nscp == ha->req_first)
1750
                ha->req_first = pscp = (Scsi_Cmnd *)nscp->SCp.ptr;
1751
            else
1752
                pscp->SCp.ptr = nscp->SCp.ptr;
1753
            if (gdth_polling)
1754
                break;
1755
        }
1756
    }
1757
 
1758
    if (ha->cmd_cnt > 0) {
1759
        gdth_release_event(hanum);
1760
    }
1761
 
1762
    restore_flags(flags);
1763
 
1764
    if (gdth_polling && ha->cmd_cnt > 0) {
1765
        if (!gdth_wait(hanum,cmd_index,POLL_TIMEOUT))
1766
            printk("GDT: Controller %d: Command %d timed out !\n",
1767
                   hanum,cmd_index);
1768
    }
1769
}
1770
 
1771
static void gdth_copy_internal_data(Scsi_Cmnd *scp,char *buffer,ushort count)
1772
{
1773
    ushort cpcount,i;
1774
    ushort cpsum,cpnow;
1775
    struct scatterlist *sl;
1776
 
1777
    cpcount = count<=(ushort)scp->bufflen ? count:(ushort)scp->bufflen;
1778
    if (scp->use_sg) {
1779
        sl = (struct scatterlist *)scp->request_buffer;
1780
        for (i=0,cpsum=0; i<scp->use_sg; ++i,++sl) {
1781
            cpnow = (ushort)sl->length;
1782
            TRACE(("copy_internal() now %d sum %d count %d %d\n",
1783
                          cpnow,cpsum,cpcount,(ushort)scp->bufflen));
1784
            if (cpsum+cpnow > cpcount)
1785
                cpnow = cpcount - cpsum;
1786
            cpsum += cpnow;
1787
            memcpy((char*)sl->address,buffer,cpnow);
1788
            if (cpsum == cpcount)
1789
                break;
1790
            buffer += cpnow;
1791
        }
1792
    } else {
1793
        TRACE(("copy_internal() count %d\n",cpcount));
1794
        memcpy((char*)scp->request_buffer,buffer,cpcount);
1795
    }
1796
}
1797
 
1798
static int gdth_internal_cache_cmd(int hanum,Scsi_Cmnd *scp,
1799
                                   unchar b,ulong *flags)
1800
{
1801
    register gdth_ha_str *ha;
1802
    ushort hdrive;
1803
    unchar t;
1804
    gdth_inq_data inq;
1805
    gdth_rdcap_data rdc;
1806
    gdth_sense_data sd;
1807
    gdth_modep_data mpd;
1808
 
1809
    ha = HADATA(gdth_ctr_tab[hanum]);
1810
    t  = scp->target;
1811
    hdrive = ha->id[b][t].hostdrive;
1812
    TRACE(("gdth_internal_cache_cmd() cmd 0x%x hdrive %d\n",
1813
                 scp->cmnd[0],hdrive));
1814
 
1815
    if (scp->lun !=0)
1816
        scp->result = DID_BAD_TARGET << 16;
1817
    else {
1818
        switch (scp->cmnd[0]) {
1819
          case TEST_UNIT_READY:
1820
          case VERIFY:
1821
          case START_STOP:
1822
            TRACE2(("Test/Verify/Start hdrive %d\n",hdrive));
1823
            break;
1824
 
1825
          case INQUIRY:
1826
            TRACE2(("Inquiry hdrive %d devtype %d\n",
1827
                          hdrive,ha->id[b][t].devtype));
1828
            inq.type_qual = (ha->id[b][t].devtype&4) ? TYPE_ROM:TYPE_DISK;
1829
            /* you can here set all disks to removable, if you want to do
1830
               a flush using the ALLOW_MEDIUM_REMOVAL command */
1831
            inq.modif_rmb = ha->id[b][t].devtype&1 ? 0x80:0x00;
1832
            inq.version   = 2;
1833
            inq.resp_aenc = 2;
1834
            inq.add_length= 32;
1835
            strcpy(inq.vendor,"ICP    ");
1836
            sprintf(inq.product,"Host Drive  #%02d",hdrive);
1837
            strcpy(inq.revision,"   ");
1838
            gdth_copy_internal_data(scp,(char*)&inq,sizeof(gdth_inq_data));
1839
            break;
1840
 
1841
          case REQUEST_SENSE:
1842
            TRACE2(("Request sense hdrive %d\n",hdrive));
1843
            sd.errorcode = 0x70;
1844
            sd.segno     = 0x00;
1845
            sd.key       = NO_SENSE;
1846
            sd.info      = 0;
1847
            sd.add_length= 0;
1848
            gdth_copy_internal_data(scp,(char*)&sd,sizeof(gdth_sense_data));
1849
            break;
1850
 
1851
          case MODE_SENSE:
1852
            TRACE2(("Mode sense hdrive %d\n",hdrive));
1853
            memset((char*)&mpd,0,sizeof(gdth_modep_data));
1854
            mpd.hd.data_length = sizeof(gdth_modep_data);
1855
            mpd.hd.dev_par     = (ha->id[b][t].devtype&2) ? 0x80:0;
1856
            mpd.hd.bd_length   = sizeof(mpd.bd);
1857
            mpd.bd.block_length[0] = (SECTOR_SIZE & 0x00ff0000) >> 16;
1858
            mpd.bd.block_length[1] = (SECTOR_SIZE & 0x0000ff00) >> 8;
1859
            mpd.bd.block_length[2] = (SECTOR_SIZE & 0x000000ff);
1860
            gdth_copy_internal_data(scp,(char*)&mpd,sizeof(gdth_modep_data));
1861
            break;
1862
 
1863
          case READ_CAPACITY:
1864
            TRACE2(("Read capacity hdrive %d\n",hdrive));
1865
            rdc.last_block_no = ntohl(ha->id[b][t].size-1);
1866
            rdc.block_length  = ntohl(SECTOR_SIZE);
1867
            gdth_copy_internal_data(scp,(char*)&rdc,sizeof(gdth_rdcap_data));
1868
            break;
1869
 
1870
          default:
1871
            TRACE2(("Internal cache cmd 0x%x unknown\n",scp->cmnd[0]));
1872
            break;
1873
        }
1874
        scp->result = DID_OK << 16;
1875
    }
1876
 
1877
    restore_flags(*flags);
1878
    if (!scp->SCp.have_data_in)
1879
        scp->SCp.have_data_in++;
1880
    else
1881
        scp->scsi_done(scp);
1882
    save_flags(*flags);
1883
    cli();
1884
    return 1;
1885
}
1886
 
1887
static int gdth_fill_cache_cmd(int hanum,Scsi_Cmnd *scp,ushort hdrive)
1888
{
1889
    register gdth_ha_str *ha;
1890
    register gdth_cmd_str *cmdp;
1891
    struct scatterlist *sl;
1892
    ushort i;
1893
    int cmd_index;
1894
 
1895
    ha = HADATA(gdth_ctr_tab[hanum]);
1896
    cmdp = ha->pccb;
1897
    TRACE(("gdth_fill_cache_cmd() cmd 0x%x cmdsize %d hdrive %d\n",
1898
                 scp->cmnd[0],scp->cmd_len,hdrive));
1899
 
1900
    if (ha->type==GDT_EISA && ha->cmd_cnt>0)
1901
        return 0;
1902
 
1903
    cmdp->Service = CACHESERVICE;
1904
    cmdp->RequestBuffer = scp;
1905
    /* search free command index */
1906
    if (!(cmd_index=gdth_get_cmd_index(hanum))) {
1907
        TRACE(("GDT: No free command index found\n"));
1908
        return 0;
1909
    }
1910
    /* if it's the first command, set command semaphore */
1911
    if (ha->cmd_cnt == 0)
1912
        gdth_set_sema0(hanum);
1913
 
1914
    /* fill command */
1915
    if (scp->cmnd[0]==ALLOW_MEDIUM_REMOVAL) {
1916
        if (scp->cmnd[4] & 1)                   /* prevent ? */
1917
            cmdp->OpCode      = GDT_MOUNT;
1918
        else if (scp->cmnd[3] & 1)              /* removable drive ? */
1919
            cmdp->OpCode      = GDT_UNMOUNT;
1920
        else
1921
            cmdp->OpCode      = GDT_FLUSH;
1922
    } else {
1923
        if (scp->cmnd[0]==WRITE_6 || scp->cmnd[0]==WRITE_10) {
1924
            if (gdth_write_through)
1925
                cmdp->OpCode  = GDT_WRITE_THR;
1926
            else
1927
                cmdp->OpCode  = GDT_WRITE;
1928
        } else {
1929
            cmdp->OpCode      = GDT_READ;
1930
        }
1931
    }
1932
 
1933
    cmdp->BoardNode           = LOCALBOARD;
1934
    cmdp->u.cache.DeviceNo    = hdrive;
1935
 
1936
    if (scp->cmnd[0]==ALLOW_MEDIUM_REMOVAL) {
1937
        cmdp->u.cache.BlockNo = 1;
1938
        cmdp->u.cache.sg_canz = 0;
1939
    } else {
1940
        if (scp->cmd_len != 6) {
1941
            cmdp->u.cache.BlockNo = ntohl(*(ulong*)&scp->cmnd[2]);
1942
            cmdp->u.cache.BlockCnt= (ulong)ntohs(*(ushort*)&scp->cmnd[7]);
1943
        } else {
1944
            cmdp->u.cache.BlockNo = ntohl(*(ulong*)&scp->cmnd[0]) & 0x001fffffUL;
1945
            cmdp->u.cache.BlockCnt= scp->cmnd[4]==0 ? 0x100 : scp->cmnd[4];
1946
        }
1947
 
1948
        if (scp->use_sg) {
1949
            cmdp->u.cache.DestAddr= -1UL;
1950
            sl = (struct scatterlist *)scp->request_buffer;
1951
            for (i=0; i<scp->use_sg; ++i,++sl) {
1952
                cmdp->u.cache.sg_lst[i].sg_ptr = virt_to_bus(sl->address);
1953
                cmdp->u.cache.sg_lst[i].sg_len = (ulong)sl->length;
1954
            }
1955
            cmdp->u.cache.sg_canz = (ulong)i;
1956
 
1957
#ifdef GDTH_STATISTICS
1958
            if (max_sg < (ulong)i) {
1959
                max_sg = (ulong)i;
1960
                TRACE3(("GDT: max_sg = %d\n",i));
1961
            }
1962
#endif
1963
            if (i<GDTH_MAXSG)
1964
                cmdp->u.cache.sg_lst[i].sg_len = 0;
1965
        } else {
1966
            if (ha->cache_feat & SCATTER_GATHER) {
1967
                cmdp->u.cache.DestAddr = -1UL;
1968
                cmdp->u.cache.sg_canz = 1;
1969
                cmdp->u.cache.sg_lst[0].sg_ptr = virt_to_bus(scp->request_buffer);
1970
                cmdp->u.cache.sg_lst[0].sg_len = scp->request_bufflen;
1971
                cmdp->u.cache.sg_lst[1].sg_len = 0;
1972
            } else {
1973
                cmdp->u.cache.DestAddr  = virt_to_bus(scp->request_buffer);
1974
                cmdp->u.cache.sg_canz= 0;
1975
            }
1976
        }
1977
    }
1978
    TRACE(("cache cmd: addr. %lx sganz %lx sgptr0 %lx sglen0 %lx\n",
1979
                  cmdp->u.cache.DestAddr,cmdp->u.cache.sg_canz,
1980
                  cmdp->u.cache.sg_lst[0].sg_ptr,
1981
                  cmdp->u.cache.sg_lst[0].sg_len));
1982
    TRACE(("cache cmd: cmd %d blockno. %ld, blockcnt %ld\n",
1983
                  cmdp->OpCode,cmdp->u.cache.BlockNo,cmdp->u.cache.BlockCnt));
1984
 
1985
    /* evaluate command size, check space */
1986
    ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) +
1987
        (ushort)cmdp->u.cache.sg_canz * sizeof(gdth_sg_str);
1988
    if (ha->cmd_len & 3)
1989
        ha->cmd_len += (4 - (ha->cmd_len & 3));
1990
 
1991
    if (ha->cmd_cnt > 0) {
1992
        if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
1993
            ha->ic_all_size) {
1994
            TRACE2(("gdth_fill_cache() DPMEM overflow\n"));
1995
            gdth_cmd_tab[cmd_index-2][hanum].cmnd = UNUSED_CMND;
1996
            return 0;
1997
        }
1998
    }
1999
 
2000
    /* copy command */
2001
    gdth_copy_command(hanum);
2002
    return cmd_index;
2003
}
2004
 
2005
static int gdth_fill_raw_cmd(int hanum,Scsi_Cmnd *scp,unchar b)
2006
{
2007
    register gdth_ha_str *ha;
2008
    register gdth_cmd_str *cmdp;
2009
    struct scatterlist *sl;
2010
    ushort i;
2011
    int cmd_index;
2012
    unchar t,l;
2013
 
2014
    ha = HADATA(gdth_ctr_tab[hanum]);
2015
    t = scp->target;
2016
    l = scp->lun;
2017
    cmdp = ha->pccb;
2018
    TRACE(("gdth_fill_raw_cmd() cmd 0x%x bus %d ID %d LUN %d\n",
2019
                 scp->cmnd[0],b,t,l));
2020
 
2021
    if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2022
        return 0;
2023
 
2024
    cmdp->Service = SCSIRAWSERVICE;
2025
    cmdp->RequestBuffer = scp;
2026
    /* search free command index */
2027
    if (!(cmd_index=gdth_get_cmd_index(hanum))) {
2028
        TRACE(("GDT: No free command index found\n"));
2029
        return 0;
2030
    }
2031
    /* if it's the first command, set command semaphore */
2032
    if (ha->cmd_cnt == 0)
2033
        gdth_set_sema0(hanum);
2034
 
2035
    /* fill command */
2036
    cmdp->OpCode           = GDT_WRITE;         /* always */
2037
    cmdp->BoardNode        = LOCALBOARD;
2038
    cmdp->u.raw.reserved   = 0;
2039
    cmdp->u.raw.mdisc_time = 0;
2040
    cmdp->u.raw.mcon_time  = 0;
2041
    cmdp->u.raw.clen       = scp->cmd_len;
2042
    cmdp->u.raw.target     = t;
2043
    cmdp->u.raw.lun        = l;
2044
    cmdp->u.raw.bus        = b;
2045
    cmdp->u.raw.priority   = 0;
2046
    cmdp->u.raw.link_p     = NULL;
2047
    cmdp->u.raw.sdlen      = scp->request_bufflen;
2048
    cmdp->u.raw.sense_len  = 16;
2049
    cmdp->u.raw.sense_data = virt_to_bus(scp->sense_buffer);
2050
    cmdp->u.raw.direction  =
2051
        gdth_direction_tab[scp->cmnd[0]]==DOU ? DATA_OUT : DATA_IN;
2052
    memcpy(cmdp->u.raw.cmd,scp->cmnd,12);
2053
 
2054
    if (scp->use_sg) {
2055
        cmdp->u.raw.sdata  = -1UL;
2056
        sl = (struct scatterlist *)scp->request_buffer;
2057
        for (i=0; i<scp->use_sg; ++i,++sl) {
2058
            cmdp->u.raw.sg_lst[i].sg_ptr = virt_to_bus(sl->address);
2059
            cmdp->u.raw.sg_lst[i].sg_len = (ulong)sl->length;
2060
        }
2061
        cmdp->u.raw.sg_ranz = (ulong)i;
2062
 
2063
#ifdef GDTH_STATISTICS
2064
        if (max_sg < (ulong)i) {
2065
            max_sg = (ulong)i;
2066
            TRACE3(("GDT: max_sg = %d\n",i));
2067
        }
2068
#endif
2069
        if (i<GDTH_MAXSG)
2070
            cmdp->u.raw.sg_lst[i].sg_len = 0;
2071
    } else {
2072
        if (ha->raw_feat & SCATTER_GATHER) {
2073
            cmdp->u.raw.sdata  = -1UL;
2074
            cmdp->u.raw.sg_ranz= 1;
2075
            cmdp->u.raw.sg_lst[0].sg_ptr = virt_to_bus(scp->request_buffer);
2076
            cmdp->u.raw.sg_lst[0].sg_len = scp->request_bufflen;
2077
            cmdp->u.raw.sg_lst[1].sg_len = 0;
2078
        } else {
2079
            cmdp->u.raw.sdata  = virt_to_bus(scp->request_buffer);
2080
            cmdp->u.raw.sg_ranz= 0;
2081
        }
2082
    }
2083
    TRACE(("raw cmd: addr. %lx sganz %lx sgptr0 %lx sglen0 %lx\n",
2084
                  cmdp->u.raw.sdata,cmdp->u.raw.sg_ranz,
2085
                  cmdp->u.raw.sg_lst[0].sg_ptr,
2086
                  cmdp->u.raw.sg_lst[0].sg_len));
2087
 
2088
    /* evaluate command size, check space */
2089
    ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) +
2090
        (ushort)cmdp->u.raw.sg_ranz * sizeof(gdth_sg_str);
2091
    if (ha->cmd_len & 3)
2092
        ha->cmd_len += (4 - (ha->cmd_len & 3));
2093
 
2094
    if (ha->cmd_cnt > 0) {
2095
        if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2096
            ha->ic_all_size) {
2097
            TRACE2(("gdth_fill_raw() DPMEM overflow\n"));
2098
            gdth_cmd_tab[cmd_index-2][hanum].cmnd = UNUSED_CMND;
2099
            return 0;
2100
        }
2101
    }
2102
 
2103
    /* copy command */
2104
    gdth_copy_command(hanum);
2105
    return cmd_index;
2106
}
2107
 
2108
static int gdth_special_cmd(int hanum,Scsi_Cmnd *scp,unchar b)
2109
{
2110
    register gdth_ha_str *ha;
2111
    register gdth_cmd_str *cmdp;
2112
    int cmd_index;
2113
 
2114
    ha  = HADATA(gdth_ctr_tab[hanum]);
2115
    cmdp= ha->pccb;
2116
    TRACE2(("gdth_special_cmd(): "));
2117
 
2118
    if (ha->type==GDT_EISA && ha->cmd_cnt>0)
2119
        return 0;
2120
 
2121
    memcpy( cmdp, scp->request_buffer, sizeof(gdth_cmd_str));
2122
    cmdp->RequestBuffer = scp;
2123
 
2124
    /* search free command index */
2125
    if (!(cmd_index=gdth_get_cmd_index(hanum))) {
2126
        TRACE(("GDT: No free command index found\n"));
2127
        return 0;
2128
    }
2129
 
2130
    /* if it's the first command, set command semaphore */
2131
    if (ha->cmd_cnt == 0)
2132
       gdth_set_sema0(hanum);
2133
 
2134
    /* evaluate command size, check space */
2135
    if (cmdp->OpCode == GDT_IOCTL) {
2136
        TRACE2(("IOCTL\n"));
2137
        ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.ioctl.p_param) + sizeof(ulong);
2138
    } else if (cmdp->Service == CACHESERVICE) {
2139
        TRACE2(("cache command %d\n",cmdp->OpCode));
2140
        ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.cache.sg_lst) + sizeof(gdth_sg_str);
2141
    } else if (cmdp->Service == SCSIRAWSERVICE) {
2142
        TRACE2(("raw command %d/%d\n",cmdp->OpCode,cmdp->u.raw.cmd[0]));
2143
        ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.raw.sg_lst) + sizeof(gdth_sg_str);
2144
    }
2145
 
2146
    if (ha->cmd_len & 3)
2147
        ha->cmd_len += (4 - (ha->cmd_len & 3));
2148
 
2149
    if (ha->cmd_cnt > 0) {
2150
        if ((ha->cmd_offs_dpmem + ha->cmd_len + DPMEM_COMMAND_OFFSET) >
2151
            ha->ic_all_size) {
2152
            TRACE2(("gdth_special_cmd() DPMEM overflow\n"));
2153
            gdth_cmd_tab[cmd_index-2][hanum].cmnd = UNUSED_CMND;
2154
            return 0;
2155
        }
2156
    }
2157
 
2158
    /* copy command */
2159
    gdth_copy_command(hanum);
2160
    return cmd_index;
2161
}
2162
 
2163
 
2164
/* Controller event handling functions */
2165
static gdth_evt_str *gdth_store_event(ushort source, ushort idx,
2166
                                      gdth_evt_data *evt)
2167
{
2168
    gdth_evt_str *e;
2169
    ulong flags;
2170
    struct timeval tv;
2171
 
2172
    TRACE2(("gdth_store_event() source %d idx %d\n", source, idx));
2173
    if (source == 0)                        /* no source -> no event */
2174
        return 0;
2175
 
2176
    save_flags(flags);
2177
    cli();
2178
    if (ebuffer[elastidx].event_source == source &&
2179
        ebuffer[elastidx].event_idx == idx &&
2180
        !memcmp((char *)&ebuffer[elastidx].event_data.eu,
2181
            (char *)&evt->eu, evt->size)) {
2182
        e = &ebuffer[elastidx];
2183
        do_gettimeofday(&tv);
2184
        e->last_stamp = tv.tv_sec;
2185
        ++e->same_count;
2186
    } else {
2187
        if (ebuffer[elastidx].event_source != 0) {  /* entry not free ? */
2188
            ++elastidx;
2189
            if (elastidx == MAX_EVENTS)
2190
                elastidx = 0;
2191
            if (elastidx == eoldidx) {              /* reached mark ? */
2192
                ++eoldidx;
2193
                if (eoldidx == MAX_EVENTS)
2194
                    eoldidx = 0;
2195
            }
2196
        }
2197
        e = &ebuffer[elastidx];
2198
        e->event_source = source;
2199
        e->event_idx = idx;
2200
        do_gettimeofday(&tv);
2201
        e->first_stamp = e->last_stamp = tv.tv_sec;
2202
        e->same_count = 1;
2203
        e->event_data = *evt;
2204
    }
2205
    restore_flags(flags);
2206
    return e;
2207
}
2208
 
2209
static int gdth_read_event(int handle, gdth_evt_str *estr)
2210
{
2211
    gdth_evt_str *e;
2212
    int eindex;
2213
    ulong flags;
2214
 
2215
    TRACE2(("gdth_read_event() handle %d\n", handle));
2216
    save_flags(flags);
2217
    cli();
2218
    if (handle == -1)
2219
        eindex = eoldidx;
2220
    else
2221
        eindex = handle;
2222
    estr->event_source = 0;
2223
 
2224
    if (eindex >= MAX_EVENTS) {
2225
        restore_flags(flags);
2226
        return eindex;
2227
    }
2228
    e = &ebuffer[eindex];
2229
    if (e->event_source != 0) {
2230
        if (eindex != elastidx) {
2231
            if (++eindex == MAX_EVENTS)
2232
                eindex = 0;
2233
        } else {
2234
            eindex = -1;
2235
        }
2236
        memcpy(estr, e, sizeof(gdth_evt_str));
2237
    }
2238
    restore_flags(flags);
2239
    return eindex;
2240
}
2241
 
2242
static void gdth_readapp_event(unchar application, gdth_evt_str *estr)
2243
{
2244
    gdth_evt_str *e;
2245
    int eindex;
2246
    ulong flags;
2247
    unchar found = FALSE;
2248
 
2249
    TRACE2(("gdth_readapp_event() app. %d\n", application));
2250
    save_flags(flags);
2251
    cli();
2252
    eindex = eoldidx;
2253
    for (;;) {
2254
        e = &ebuffer[eindex];
2255
        if (e->event_source == 0)
2256
            break;
2257
        if ((e->application & application) == 0) {
2258
            e->application |= application;
2259
            found = TRUE;
2260
            break;
2261
        }
2262
        if (eindex == elastidx)
2263
            break;
2264
        if (++eindex == MAX_EVENTS)
2265
            eindex = 0;
2266
    }
2267
    if (found)
2268
        memcpy(estr, e, sizeof(gdth_evt_str));
2269
    else
2270
        estr->event_source = 0;
2271
    restore_flags(flags);
2272
}
2273
 
2274
static void gdth_clear_events()
2275
{
2276
    ulong flags;
2277
 
2278
    TRACE(("gdth_clear_events()"));
2279
    save_flags(flags);
2280
    cli();
2281
 
2282
    eoldidx = elastidx = 0;
2283
    ebuffer[0].event_source = 0;
2284
    restore_flags(flags);
2285
}
2286
 
2287
 
2288
/* SCSI interface functions */
2289
 
2290
#if LINUX_VERSION_CODE >= 0x02015F
2291
static void do_gdth_interrupt(int irq,void *dev_id,struct pt_regs *regs)
2292
{
2293
    ulong flags;
2294
 
2295
    spin_lock_irqsave(&io_request_lock, flags);
2296
    gdth_interrupt(irq, dev_id, regs);
2297
    spin_unlock_irqrestore(&io_request_lock, flags);
2298
}
2299
#endif
2300
 
2301
#if LINUX_VERSION_CODE >= 0x010346
2302
static void gdth_interrupt(int irq,void *dev_id,struct pt_regs *regs)
2303
#else
2304
static void gdth_interrupt(int irq,struct pt_regs *regs)
2305
#endif
2306
{
2307
    register gdth_ha_str *ha;
2308
    gdt6m_dpram_str *dp6m_ptr;
2309
    gdt6_dpram_str *dp6_ptr;
2310
    gdt2_dpram_str *dp2_ptr;
2311
    Scsi_Cmnd *scp;
2312
    int hanum;
2313
    unchar IStatus;
2314
    ushort CmdStatus, Service = 0;
2315
    ulong InfoBytes, InfoBytes2 = 0;
2316
    gdth_evt_data dvr;
2317
 
2318
    TRACE(("gdth_interrupt() IRQ %d\n",irq));
2319
 
2320
    /* if polling and not from gdth_wait() -> return */
2321
    if (gdth_polling) {
2322
        if (!gdth_from_wait) {
2323
            return;
2324
        }
2325
    }
2326
 
2327
    wait_index = 0;
2328
 
2329
    /* search controller */
2330
    if ((hanum = gdth_get_status(&IStatus,irq)) == -1) {
2331
        /*
2332
        TRACE2(("gdth_interrupt(): Spurious interrupt received\n"));
2333
        */
2334
        return;
2335
    }
2336
 
2337
#ifdef GDTH_STATISTICS
2338
    ++act_ints;
2339
#endif
2340
 
2341
    ha = HADATA(gdth_ctr_tab[hanum]);
2342
    if (ha->type == GDT_EISA) {
2343
        if (IStatus & 0x80) {                   /* error flag */
2344
            IStatus &= ~0x80;
2345
            CmdStatus = inw(ha->bmic + MAILBOXREG+8);
2346
            TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,CmdStatus));
2347
            if (IStatus == ASYNCINDEX) {        /* async. event ? */
2348
                Service = inw(ha->bmic + MAILBOXREG+10);
2349
                InfoBytes2 = inl(ha->bmic + MAILBOXREG+4);
2350
            }
2351
        } else                                  /* no error */
2352
            CmdStatus = S_OK;
2353
        InfoBytes = inl(ha->bmic + MAILBOXREG+12);
2354
        if (gdth_polling)                       /* init. -> more info */
2355
            InfoBytes2 = inl(ha->bmic + MAILBOXREG+4);
2356
        outb(0xff, ha->bmic + EDOORREG);    /* acknowledge interrupt */
2357
        outb(0x00, ha->bmic + SEMA1REG);    /* reset status semaphore */
2358
    } else if (ha->type == GDT_ISA) {
2359
        dp2_ptr = (gdt2_dpram_str *)ha->brd;
2360
        if (IStatus & 0x80) {                   /* error flag */
2361
            IStatus &= ~0x80;
2362
            CmdStatus = readw(&dp2_ptr->u.ic.Status);
2363
            TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,CmdStatus));
2364
            if (IStatus == ASYNCINDEX) {        /* async. event ? */
2365
                Service = readw(&dp2_ptr->u.ic.Service);
2366
                InfoBytes2 = readl(&dp2_ptr->u.ic.Info[1]);
2367
            }
2368
        } else                                  /* no error */
2369
            CmdStatus = S_OK;
2370
        InfoBytes = readl(&dp2_ptr->u.ic.Info[0]);
2371
        if (gdth_polling)                       /* init. -> more info */
2372
            InfoBytes2 = readl(&dp2_ptr->u.ic.Info[1]);
2373
        writeb(0xff, &dp2_ptr->io.irqdel);              /* acknowledge interrupt */
2374
        writeb(0, &dp2_ptr->u.ic.Cmd_Index);            /* reset command index */
2375
        writeb(0, &dp2_ptr->io.Sema1);                 /* reset status semaphore */
2376
    } else if (ha->type == GDT_PCI) {
2377
        dp6_ptr = (gdt6_dpram_str *)ha->brd;
2378
        if (IStatus & 0x80) {                   /* error flag */
2379
            IStatus &= ~0x80;
2380
            CmdStatus = readw(&dp6_ptr->u.ic.Status);
2381
            TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,CmdStatus));
2382
            if (IStatus == ASYNCINDEX) {        /* async. event ? */
2383
                Service = readw(&dp6_ptr->u.ic.Service);
2384
                InfoBytes2 = readl(&dp6_ptr->u.ic.Info[1]);
2385
            }
2386
        } else                                  /* no error */
2387
            CmdStatus = S_OK;
2388
        InfoBytes = readl(&dp6_ptr->u.ic.Info[0]);
2389
        if (gdth_polling)                       /* init. -> more info */
2390
            InfoBytes2 = readl(&dp6_ptr->u.ic.Info[1]);
2391
        writeb(0xff, &dp6_ptr->io.irqdel);              /* acknowledge interrupt */
2392
        writeb(0, &dp6_ptr->u.ic.Cmd_Index);            /* reset command index */
2393
        writeb(0, &dp6_ptr->io.Sema1);                 /* reset status semaphore */
2394
    } else if (ha->type == GDT_PCINEW) {
2395
        if (IStatus & 0x80) {                   /* error flag */
2396
            IStatus &= ~0x80;
2397
            CmdStatus = inw(PTR2USHORT(&ha->plx->status));
2398
            TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,CmdStatus));
2399
            if (IStatus == ASYNCINDEX) {        /* async. event ? */
2400
                Service = inw(PTR2USHORT(&ha->plx->service));
2401
                InfoBytes2 = inl(PTR2USHORT(&ha->plx->info[1]));
2402
            }
2403
        } else
2404
            CmdStatus = S_OK;
2405
 
2406
        InfoBytes = inl(PTR2USHORT(&ha->plx->info[0]));
2407
        if (gdth_polling)                       /* init. -> more info */
2408
            InfoBytes2 = inl(PTR2USHORT(&ha->plx->info[1]));
2409
        outb(0xff, PTR2USHORT(&ha->plx->edoor_reg));
2410
        outb(0x00, PTR2USHORT(&ha->plx->sema1_reg));
2411
    } else if (ha->type == GDT_PCIMPR) {
2412
        dp6m_ptr = (gdt6m_dpram_str *)ha->brd;
2413
        if (IStatus & 0x80) {                   /* error flag */
2414
            IStatus &= ~0x80;
2415
            CmdStatus = readw(&dp6m_ptr->i960r.status);
2416
            TRACE2(("gdth_interrupt() error %d/%d\n",IStatus,CmdStatus));
2417
            if (IStatus == ASYNCINDEX) {        /* async. event ? */
2418
                Service = readw(&dp6m_ptr->i960r.service);
2419
                InfoBytes2 = readl(&dp6m_ptr->i960r.info[1]);
2420
            }
2421
        } else                                  /* no error */
2422
            CmdStatus = S_OK;
2423
        InfoBytes = readl(&dp6m_ptr->i960r.info[0]);
2424
        if (gdth_polling)                       /* init. -> more info */
2425
            InfoBytes2 = readl(&dp6m_ptr->i960r.info[1]);
2426
        writeb(0xff, &dp6m_ptr->i960r.edoor_reg);
2427
        writeb(0, &dp6m_ptr->i960r.sema1_reg);
2428
    } else {
2429
        TRACE2(("gdth_interrupt() unknown controller type\n"));
2430
        return;
2431
    }
2432
 
2433
    TRACE(("gdth_interrupt() index %d stat %d info %ld\n",
2434
                 IStatus,CmdStatus,InfoBytes));
2435
    ha->status = CmdStatus;
2436
    ha->info   = InfoBytes;
2437
    ha->info2  = InfoBytes2;
2438
 
2439
    if (gdth_from_wait) {
2440
        wait_hanum = hanum;
2441
        wait_index = (int)IStatus;
2442
    }
2443
 
2444
    if (IStatus == ASYNCINDEX) {
2445
        TRACE2(("gdth_interrupt() async. event\n"));
2446
        gdth_async_event(hanum,Service);
2447
    } else {
2448
        if (IStatus == SPEZINDEX) {
2449
            TRACE2(("Service unknown or not initialized !\n"));
2450
            dvr.size = sizeof(dvr.eu.driver);
2451
            dvr.eu.driver.ionode = hanum;
2452
            gdth_store_event(ES_DRIVER, 4, &dvr);
2453
            return;
2454
        }
2455
        scp     = gdth_cmd_tab[IStatus-2][hanum].cmnd;
2456
        Service = gdth_cmd_tab[IStatus-2][hanum].service;
2457
        gdth_cmd_tab[IStatus-2][hanum].cmnd = UNUSED_CMND;
2458
        if (scp == UNUSED_CMND) {
2459
            TRACE2(("gdth_interrupt() index to unused command (%d)\n",IStatus));
2460
            dvr.size = sizeof(dvr.eu.driver);
2461
            dvr.eu.driver.ionode = hanum;
2462
            dvr.eu.driver.index = IStatus;
2463
            gdth_store_event(ES_DRIVER, 1, &dvr);
2464
            return;
2465
        }
2466
        if (scp == INTERNAL_CMND) {
2467
            TRACE(("gdth_interrupt() answer to internal command\n"));
2468
            return;
2469
        }
2470
        TRACE(("gdth_interrupt() sync. status\n"));
2471
        gdth_sync_event(hanum,Service,IStatus,scp);
2472
    }
2473
    gdth_next(hanum);
2474
}
2475
 
2476
static int gdth_sync_event(int hanum,int service,unchar index,Scsi_Cmnd *scp)
2477
{
2478
    register gdth_ha_str *ha;
2479
    gdth_msg_str *msg;
2480
    gdth_cmd_str *cmdp;
2481
    char c='\r';
2482
    ushort i;
2483
    gdth_evt_data dvr;
2484
 
2485
    ha   = HADATA(gdth_ctr_tab[hanum]);
2486
    cmdp = ha->pccb;
2487
    TRACE(("gdth_sync_event() scp %lx serv %d status %d\n",
2488
                 (ulong)scp,service,ha->status));
2489
 
2490
    if (service == SCREENSERVICE) {
2491
        msg  = (gdth_msg_str *)ha->pscratch;
2492
        TRACE(("len: %ld, answer: %d, ext: %d, alen: %ld\n",
2493
                     msg->msg_len,msg->msg_answer,msg->msg_ext,msg->msg_alen));
2494
        if (msg->msg_len)
2495
            if (!(msg->msg_answer && msg->msg_ext)) {
2496
                msg->msg_text[msg->msg_len] = '\0';
2497
                printk("%s",msg->msg_text);
2498
            }
2499
 
2500
        if (msg->msg_ext && !msg->msg_answer) {
2501
            while (gdth_test_busy(hanum))
2502
                gdth_delay(0);
2503
            cmdp->Service       = SCREENSERVICE;
2504
            cmdp->RequestBuffer = SCREEN_CMND;
2505
            gdth_get_cmd_index(hanum);
2506
            gdth_set_sema0(hanum);
2507
            cmdp->OpCode        = GDT_READ;
2508
            cmdp->BoardNode     = LOCALBOARD;
2509
            cmdp->u.screen.reserved  = 0;
2510
            cmdp->u.screen.msg_handle= msg->msg_handle;
2511
            cmdp->u.screen.msg_addr  = (ulong)msg;
2512
            ha->cmd_offs_dpmem = 0;
2513
            ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.msg_addr)
2514
                + sizeof(ulong);
2515
            ha->cmd_cnt = 0;
2516
            gdth_copy_command(hanum);
2517
            gdth_release_event(hanum);
2518
            return 1;
2519
        }
2520
 
2521
        if (msg->msg_answer && msg->msg_alen) {
2522
            for (i=0; i<msg->msg_alen && i<MSGLEN; ++i) {
2523
                /* getchar() ?? */
2524
                /* .. */
2525
                if (c == '\r')
2526
                    break;
2527
                msg->msg_text[i] = c;
2528
            }
2529
            msg->msg_alen -= i;
2530
            if (c!='\r' && msg->msg_alen!=0) {
2531
                msg->msg_answer = 1;
2532
                msg->msg_ext    = 1;
2533
            } else {
2534
                msg->msg_ext    = 0;
2535
                msg->msg_answer = 0;
2536
            }
2537
            msg->msg_len = i;
2538
            while (gdth_test_busy(hanum))
2539
                gdth_delay(0);
2540
            cmdp->Service       = SCREENSERVICE;
2541
            cmdp->RequestBuffer = SCREEN_CMND;
2542
            gdth_get_cmd_index(hanum);
2543
            gdth_set_sema0(hanum);
2544
            cmdp->OpCode        = GDT_WRITE;
2545
            cmdp->BoardNode     = LOCALBOARD;
2546
            cmdp->u.screen.reserved  = 0;
2547
            cmdp->u.screen.msg_handle= msg->msg_handle;
2548
            cmdp->u.screen.msg_addr  = (ulong)msg;
2549
            ha->cmd_offs_dpmem = 0;
2550
            ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.msg_addr)
2551
                + sizeof(ulong);
2552
            ha->cmd_cnt = 0;
2553
            gdth_copy_command(hanum);
2554
            gdth_release_event(hanum);
2555
            return 1;
2556
        }
2557
        printk("\n");
2558
 
2559
    } else {
2560
        scp->SCp.Message = (int)ha->status;
2561
        /* cache or raw service */
2562
        if (ha->status == S_OK) {
2563
            scp->result = DID_OK << 16;
2564
        } else if (ha->status == S_BSY) {
2565
            TRACE2(("Controller busy -> retry !\n"));
2566
            gdth_putq(hanum,scp,scp->SCp.this_residual);
2567
            return 1;
2568
        } else {
2569
            if (service == CACHESERVICE) {
2570
                memset((char*)scp->sense_buffer,0,16);
2571
                scp->sense_buffer[0] = 0x70;
2572
                scp->sense_buffer[2] = NOT_READY;
2573
                scp->result = (DID_OK << 16) | (CHECK_CONDITION << 1);
2574
 
2575
                if (scp->done != gdth_scsi_done) {
2576
                    dvr.size = sizeof(dvr.eu.sync);
2577
                    dvr.eu.sync.ionode  = hanum;
2578
                    dvr.eu.sync.service = service;
2579
                    dvr.eu.sync.status  = ha->status;
2580
                    dvr.eu.sync.info    = ha->info;
2581
                    dvr.eu.sync.hostdrive =
2582
#if LINUX_VERSION_CODE >= 0x020000
2583
                        ha->id[scp->channel][scp->target].hostdrive;
2584
#else
2585
                        ha->id[NUMDATA(scp->host)->busnum][scp->target].hostdrive;
2586
#endif
2587
                    if (ha->status >= 0x8000)
2588
                        gdth_store_event(ES_SYNC, 0, &dvr);
2589
                    else
2590
                        gdth_store_event(ES_SYNC, service, &dvr);
2591
                }
2592
            } else {
2593
                if (ha->status!=S_RAW_SCSI || ha->status==S_RAW_ILL || ha->info>=0x100) {
2594
                    scp->result = DID_BAD_TARGET << 16;
2595
                } else {
2596
                    scp->result = (DID_OK << 16) | ha->info;
2597
                }
2598
            }
2599
        }
2600
        if (!scp->SCp.have_data_in)
2601
            scp->SCp.have_data_in++;
2602
        else
2603
            scp->scsi_done(scp);
2604
    }
2605
 
2606
    return 1;
2607
}
2608
 
2609
static char *async_cache_tab[] = {
2610
/* 0*/  "\011\000\002\002\002\004\002\006\004"
2611
        "GDT HA %u, service %u, async. status %u/%lu unknown",
2612
/* 1*/  "\011\000\002\002\002\004\002\006\004"
2613
        "GDT HA %u, service %u, async. status %u/%lu unknown",
2614
/* 2*/  "\005\000\002\006\004"
2615
        "GDT HA %u, Host Drive %lu not ready",
2616
/* 3*/  "\005\000\002\006\004"
2617
        "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",
2618
/* 4*/  "\005\000\002\006\004"
2619
        "GDT HA %u, mirror update on Host Drive %lu failed",
2620
/* 5*/  "\005\000\002\006\004"
2621
        "GDT HA %u, Mirror Drive %lu failed",
2622
/* 6*/  "\005\000\002\006\004"
2623
        "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",
2624
/* 7*/  "\005\000\002\006\004"
2625
        "GDT HA %u, Host Drive %lu write protected",
2626
/* 8*/  "\005\000\002\006\004"
2627
        "GDT HA %u, media changed in Host Drive %lu",
2628
/* 9*/  "\005\000\002\006\004"
2629
        "GDT HA %u, Host Drive %lu is offline",
2630
/*10*/  "\005\000\002\006\004"
2631
        "GDT HA %u, media change of Mirror Drive %lu",
2632
/*11*/  "\005\000\002\006\004"
2633
        "GDT HA %u, Mirror Drive %lu is write protected",
2634
/*12*/  "\005\000\002\006\004"
2635
        "GDT HA %u, general error on Host Drive %lu. Please check the devices of this drive!",
2636
/*13*/  "\007\000\002\006\002\010\002"
2637
        "GDT HA %u, Array Drive %u: Cache Drive %u failed",
2638
/*14*/  "\005\000\002\006\002"
2639
        "GDT HA %u, Array Drive %u: FAIL state entered",
2640
/*15*/  "\005\000\002\006\002"
2641
        "GDT HA %u, Array Drive %u: error",
2642
/*16*/  "\007\000\002\006\002\010\002"
2643
        "GDT HA %u, Array Drive %u: failed drive replaced by Cache Drive %u",
2644
/*17*/  "\005\000\002\006\002"
2645
        "GDT HA %u, Array Drive %u: parity build failed",
2646
/*18*/  "\005\000\002\006\002"
2647
        "GDT HA %u, Array Drive %u: drive rebuild failed",
2648
/*19*/  "\007\000\002\010\002"
2649
        "GDT HA %u, Test of Hot Fix %u failed",
2650
/*20*/  "\005\000\002\006\002"
2651
        "GDT HA %u, Array Drive %u: drive build finished successfully",
2652
/*21*/  "\005\000\002\006\002"
2653
        "GDT HA %u, Array Drive %u: drive rebuild finished successfully",
2654
/*22*/  "\007\000\002\006\002\010\002"
2655
        "GDT HA %u, Array Drive %u: Hot Fix %u activated",
2656
/*23*/  "\005\000\002\006\002"
2657
        "GDT HA %u, Host Drive %u: processing of i/o aborted due to serious drive error",
2658
/*24*/  "\005\000\002\010\002"
2659
        "GDT HA %u, mirror update on Cache Drive %u completed",
2660
/*25*/  "\005\000\002\010\002"
2661
        "GDT HA %u, mirror update on Cache Drive %lu failed",
2662
/*26*/  "\005\000\002\006\002"
2663
        "GDT HA %u, Array Drive %u: drive rebuild started",
2664
/*27*/  "\005\000\002\012\001"
2665
        "GDT HA %u, Fault bus %u: SHELF OK detected",
2666
/*28*/  "\005\000\002\012\001"
2667
        "GDT HA %u, Fault bus %u: SHELF not OK detected",
2668
/*29*/  "\007\000\002\012\001\013\001"
2669
        "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug started",
2670
/*30*/  "\007\000\002\012\001\013\001"
2671
        "GDT HA %u, Fault bus %u, ID %u: new disk detected",
2672
/*31*/  "\007\000\002\012\001\013\001"
2673
        "GDT HA %u, Fault bus %u, ID %u: old disk detected",
2674
/*32*/  "\007\000\002\012\001\013\001"
2675
        "GDT HA %u, Fault bus %u, ID %u: plugging an active disk is illegal",
2676
/*33*/  "\007\000\002\012\001\013\001"
2677
        "GDT HA %u, Fault bus %u, ID %u: illegal device detected",
2678
/*34*/  "\011\000\002\012\001\013\001\006\004"
2679
        "GDT HA %u, Fault bus %u, ID %u: insufficient disk capacity (%lu MB required)",
2680
/*35*/  "\007\000\002\012\001\013\001"
2681
        "GDT HA %u, Fault bus %u, ID %u: disk write protected",
2682
/*36*/  "\007\000\002\012\001\013\001"
2683
        "GDT HA %u, Fault bus %u, ID %u: disk not available",
2684
/*37*/  "\007\000\002\012\001\006\004"
2685
        "GDT HA %u, Fault bus %u: swap detected (%lu)",
2686
/*38*/  "\007\000\002\012\001\013\001"
2687
        "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug finished successfully",
2688
/*39*/  "\007\000\002\012\001\013\001"
2689
        "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted due to user Hot Plug",
2690
/*40*/  "\007\000\002\012\001\013\001"
2691
        "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug aborted",
2692
/*41*/  "\007\000\002\012\001\013\001"
2693
        "GDT HA %u, Fault bus %u, ID %u: Auto Hot Plug for Hot Fix started",
2694
/*42*/  "\005\000\002\006\002"
2695
        "GDT HA %u, Array Drive %u: drive build started",
2696
/*43*/  "\003\000\002"
2697
        "GDT HA %u, DRAM parity error detected",
2698
/*44*/  "\005\000\002\006\002"
2699
        "GDT HA %u, Mirror Drive %u: update started",
2700
/*45*/  "\007\000\002\006\002\010\002"
2701
        "GDT HA %u, Mirror Drive %u: Hot Fix %u activated",
2702
/*46*/  "\005\000\002\006\002"
2703
        "GDT HA %u, Array Drive %u: no matching Pool Hot Fix Drive available",
2704
/*47*/  "\005\000\002\006\002"
2705
        "GDT HA %u, Array Drive %u: Pool Hot Fix Drive available",
2706
/*48*/  "\005\000\002\006\002"
2707
        "GDT HA %u, Mirror Drive %u: no matching Pool Hot Fix Drive available",
2708
/*49*/  "\005\000\002\006\002"
2709
        "GDT HA %u, Mirror Drive %u: Pool Hot Fix Drive available",
2710
/*50*/  "\007\000\002\012\001\013\001"
2711
        "GDT HA %u, SCSI bus %u, ID %u: IGNORE_WIDE_RESIDUE message received",
2712
/*51*/  "\005\000\002\006\002"
2713
        "GDT HA %u, Array Drive %u: expand started",
2714
/*52*/  "\005\000\002\006\002"
2715
        "GDT HA %u, Array Drive %u: expand finished successfully",
2716
/*53*/  "\005\000\002\006\002"
2717
        "GDT HA %u, Array Drive %u: expand failed",
2718
/*54*/  "\003\000\002"
2719
        "GDT HA %u, CPU temperature critical",
2720
/*55*/  "\003\000\002"
2721
        "GDT HA %u, CPU temperature OK",
2722
/*56*/  "\005\000\002\006\004"
2723
        "GDT HA %u, Host drive %lu created",
2724
/*57*/  "\005\000\002\006\002"
2725
        "GDT HA %u, Array Drive %u: expand restarted",
2726
/*58*/  "\005\000\002\006\002"
2727
        "GDT HA %u, Array Drive %u: expand stopped",
2728
};
2729
 
2730
 
2731
static int gdth_async_event(int hanum,int service)
2732
{
2733
    gdth_stackframe stack;
2734
    gdth_evt_data dvr;
2735
    char *f = NULL;
2736
    int i,j;
2737
    gdth_ha_str *ha;
2738
    gdth_msg_str *msg;
2739
    gdth_cmd_str *cmdp;
2740
    int cmd_index;
2741
 
2742
    ha  = HADATA(gdth_ctr_tab[hanum]);
2743
    cmdp= ha->pccb;
2744
    msg = (gdth_msg_str *)ha->pscratch;
2745
    TRACE2(("gdth_async_event() ha %d serv %d\n",
2746
                 hanum,service));
2747
 
2748
    if (service == SCREENSERVICE) {
2749
        if (ha->status == MSG_REQUEST) {
2750
            while (gdth_test_busy(hanum))
2751
                gdth_delay(0);
2752
            cmdp->Service       = SCREENSERVICE;
2753
            cmdp->RequestBuffer = SCREEN_CMND;
2754
            cmd_index = gdth_get_cmd_index(hanum);
2755
            gdth_set_sema0(hanum);
2756
            cmdp->OpCode        = GDT_READ;
2757
            cmdp->BoardNode     = LOCALBOARD;
2758
            cmdp->u.screen.reserved  = 0;
2759
            cmdp->u.screen.msg_handle= MSG_INV_HANDLE;
2760
            cmdp->u.screen.msg_addr  = (ulong)msg;
2761
            ha->cmd_offs_dpmem = 0;
2762
            ha->cmd_len = GDTOFFSOF(gdth_cmd_str,u.screen.msg_addr)
2763
                + sizeof(ulong);
2764
            ha->cmd_cnt = 0;
2765
            gdth_copy_command(hanum);
2766
            if (ha->type == GDT_EISA)
2767
                printk("[EISA slot %d] ",(ushort)ha->brd_phys);
2768
            else if (ha->type == GDT_ISA)
2769
                printk("[DPMEM 0x%4X] ",(ushort)ha->brd_phys);
2770
            else
2771
                printk("[PCI %d/%d] ",(ushort)(ha->brd_phys>>8),
2772
                       (ushort)((ha->brd_phys>>3)&0x1f));
2773
            gdth_release_event(hanum);
2774
        }
2775
 
2776
    } else {
2777
        dvr.size = sizeof(dvr.eu.async);
2778
        dvr.eu.async.ionode   = hanum;
2779
        dvr.eu.async.service = service;
2780
        dvr.eu.async.status  = ha->status;
2781
        dvr.eu.async.info    = ha->info;
2782
        *(ulong *)dvr.eu.async.scsi_coord  = ha->info2;
2783
        gdth_store_event(ES_ASYNC, service, &dvr);
2784
 
2785
        if (service==CACHESERVICE && INDEX_OK(ha->status,async_cache_tab)) {
2786
            TRACE2(("GDT: Async. event cache service, event no.: %d\n",
2787
                ha->status));
2788
 
2789
            f = async_cache_tab[ha->status];
2790
 
2791
            /* i: parameter to push, j: stack element to fill */
2792
            for (j=0,i=1; i < f[0]; i+=2) {
2793
                switch (f[i+1]) {
2794
                  case 4:
2795
                    stack.b[j++] = *(ulong*)&dvr.eu.stream[(int)f[i]];
2796
                    break;
2797
                  case 2:
2798
                    stack.b[j++] = *(ushort*)&dvr.eu.stream[(int)f[i]];
2799
                    break;
2800
                  case 1:
2801
                    stack.b[j++] = *(unchar*)&dvr.eu.stream[(int)f[i]];
2802
                    break;
2803
                  default:
2804
                    break;
2805
                }
2806
            }
2807
 
2808
            printk(&f[f[0]],stack); printk("\n");
2809
 
2810
        } else {
2811
            printk("GDT: Unknown async. event service %d event no. %d\n",
2812
                service,ha->status);
2813
        }
2814
    }
2815
    return 1;
2816
}
2817
 
2818
 
2819
#ifdef GDTH_STATISTICS
2820
void gdth_timeout(ulong data)
2821
{
2822
    ulong flags,i;
2823
    Scsi_Cmnd *nscp;
2824
    gdth_ha_str *ha;
2825
    int hanum = 0;
2826
 
2827
    save_flags(flags);
2828
    cli();
2829
 
2830
    for (act_stats=0,i=0; i<GDTH_MAXCMDS; ++i)
2831
        if (gdth_cmd_tab[i][hanum].cmnd != UNUSED_CMND)
2832
            ++act_stats;
2833
 
2834
    ha = HADATA(gdth_ctr_tab[hanum]);
2835
    for (act_rq=0,nscp=ha->req_first; nscp; nscp=(Scsi_Cmnd*)nscp->SCp.ptr)
2836
        ++act_rq;
2837
 
2838
    TRACE2(("gdth_to(): ints %ld, ios %ld, act_stats %ld, act_rq %ld\n",
2839
            act_ints, act_ios, act_stats, act_rq));
2840
    act_ints = act_ios = 0;
2841
 
2842
    gdth_timer.expires = jiffies + 30 * HZ;
2843
    add_timer(&gdth_timer);
2844
    restore_flags(flags);
2845
}
2846
#endif
2847
 
2848
 
2849
__initfunc (int gdth_detect(Scsi_Host_Template *shtp))
2850
{
2851
    struct Scsi_Host *shp;
2852
    gdth_ha_str *ha;
2853
    unsigned long flags;
2854
    ulong isa_bios;
2855
    ushort eisa_slot,device_id,index;
2856
    gdth_pci_str pcistr;
2857
    int i,j,hanum;
2858
    unchar b;
2859
 
2860
 
2861
#ifdef DEBUG_GDTH
2862
    printk("GDT: This driver contains debugging information !! Trace level = %d\n",
2863
        DebugState);
2864
    printk("     Destination of debugging information: ");
2865
#ifdef __SERIAL__
2866
#ifdef __COM2__
2867
    printk("Serial port COM2\n");
2868
#else
2869
    printk("Serial port COM1\n");
2870
#endif
2871
#else
2872
    printk("Console\n");
2873
#endif
2874
    gdth_delay(3000);
2875
#endif
2876
 
2877
    TRACE(("gdth_detect()\n"));
2878
 
2879
    if (disable_gdth_scan) {
2880
        printk("GDT: Controller driver disabled from command line !\n");
2881
        return 0;
2882
    }
2883
 
2884
    /* initializations */
2885
    gdth_polling = TRUE; b = 0;
2886
    for (i=0; i<GDTH_MAXCMDS; ++i)
2887
        for (j=0; j<MAXHA; ++j)
2888
            gdth_cmd_tab[i][j].cmnd = UNUSED_CMND;
2889
    for (i=0; i<4; ++i)
2890
        for (j=0; j<MAXHA; ++j)
2891
            gdth_ioctl_tab[i][j] = NULL;
2892
    gdth_clear_events();
2893
 
2894
    /* scanning for controllers, at first: ISA controller */
2895
    for (isa_bios=0xc8000UL; isa_bios<=0xd8000UL; isa_bios+=0x8000UL) {
2896
        if (gdth_search_isa(isa_bios)) {        /* controller found */
2897
            shp = scsi_register(shtp,sizeof(gdth_ext_str));
2898
            ha = HADATA(shp);
2899
            if (!gdth_init_isa(isa_bios,ha)) {
2900
                scsi_unregister(shp);
2901
                continue;
2902
            }
2903
            /* controller found and initialized */
2904
            printk("Configuring GDT-ISA HA at BIOS 0x%05lX IRQ %u DRQ %u\n",
2905
                   isa_bios,ha->irq,ha->drq);
2906
 
2907
            save_flags(flags);
2908
            cli();
2909
#if LINUX_VERSION_CODE >= 0x02015F 
2910
            if (request_irq(ha->irq,do_gdth_interrupt,SA_INTERRUPT,"gdth",NULL))
2911
#elif LINUX_VERSION_CODE >= 0x010346 
2912
            if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT,"gdth",NULL))
2913
#else
2914
            if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT,"gdth"))
2915
#endif
2916
            {
2917
                printk("GDT-ISA: Unable to allocate IRQ\n");
2918
                restore_flags(flags);
2919
                scsi_unregister(shp);
2920
                continue;
2921
            }
2922
            if (request_dma(ha->drq,"gdth")) {
2923
                printk("GDT-ISA: Unable to allocate DMA channel\n");
2924
#if LINUX_VERSION_CODE >= 0x010346 
2925
                free_irq(ha->irq,NULL);
2926
#else
2927
                free_irq(ha->irq);
2928
#endif
2929
                restore_flags(flags);
2930
                scsi_unregister(shp);
2931
                continue;
2932
            }
2933
            set_dma_mode(ha->drq,DMA_MODE_CASCADE);
2934
            enable_dma(ha->drq);
2935
            shp->unchecked_isa_dma = 1;
2936
            shp->irq = ha->irq;
2937
            shp->dma_channel = ha->drq;
2938
            for (i=0; i<MAXID; ++i) {
2939
                if (ha->id[0][i].type==SIOP_DTYP) {
2940
                    shp->this_id = i;
2941
                    break;
2942
                }
2943
            }
2944
            hanum = gdth_ctr_count;
2945
            gdth_ctr_tab[gdth_ctr_count++] = shp;
2946
            gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
2947
 
2948
            NUMDATA(shp)->hanum = (ushort)hanum;
2949
            NUMDATA(shp)->busnum= 0;
2950
 
2951
            ha->pccb = CMDDATA(shp);
2952
            ha->pscratch = DMADATA(shp);
2953
            ha->req_first = NULL;
2954
            for (i=0; i<MAXBUS; ++i) {
2955
                for (j=0; j<MAXID; ++j) {
2956
                    ha->id[i][j].type = EMPTY_DTYP;
2957
                    ha->id[i][j].lock = 0;
2958
                    ha->id[i][j].heads = 0;
2959
                }
2960
            }
2961
            restore_flags(flags);
2962
 
2963
            if (!gdth_search_drives(hanum)) {
2964
                printk("GDT-ISA: Error during device scan\n");
2965
                --gdth_ctr_count;
2966
                --gdth_ctr_vcount;
2967
                save_flags(flags);
2968
                cli();
2969
#if LINUX_VERSION_CODE >= 0x010346 
2970
                free_irq(ha->irq,NULL);
2971
#else
2972
                free_irq(ha->irq);
2973
#endif
2974
                restore_flags(flags);
2975
                scsi_unregister(shp);
2976
                continue;
2977
            }
2978
 
2979
#if LINUX_VERSION_CODE >= 0x020000
2980
            shp->max_id      = 8;
2981
            shp->max_lun     = MAXLUN;
2982
            shp->max_channel = ha->bus_cnt - 1;
2983
#else
2984
            /* register addit. SCSI channels as virtual controllers */
2985
            for (b=1; b<ha->bus_cnt; ++b) {
2986
                shp = scsi_register(shtp,sizeof(gdth_num_str));
2987
                shp->unchecked_isa_dma = 1;
2988
                shp->irq = ha->irq;
2989
                shp->dma_channel = ha->drq;
2990
                for (i=0; i<MAXID; ++i) {
2991
                    if (ha->id[b][i].type==SIOP_DTYP) {
2992
                        shp->this_id = i;
2993
                        break;
2994
                    }
2995
                }
2996
                gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
2997
                NUMDATA(shp)->hanum = (ushort)hanum;
2998
                NUMDATA(shp)->busnum = b;
2999
            }
3000
#endif
3001
 
3002
            gdth_enable_int(hanum);
3003
        }
3004
    }
3005
 
3006
    /* scanning for EISA controllers */
3007
    for (eisa_slot=0x1000; eisa_slot<=0x8000; eisa_slot+=0x1000) {
3008
        if (gdth_search_eisa(eisa_slot)) {      /* controller found */
3009
            shp = scsi_register(shtp,sizeof(gdth_ext_str));
3010
            ha = HADATA(shp);
3011
            if (!gdth_init_eisa(eisa_slot,ha)) {
3012
                scsi_unregister(shp);
3013
                continue;
3014
            }
3015
            /* controller found and initialized */
3016
            printk("Configuring GDT-EISA HA at Slot %d IRQ %u\n",
3017
                   eisa_slot>>12,ha->irq);
3018
 
3019
            save_flags(flags);
3020
            cli();
3021
#if LINUX_VERSION_CODE >= 0x02015F 
3022
            if (request_irq(ha->irq,do_gdth_interrupt,SA_INTERRUPT,"gdth",NULL))
3023
#elif LINUX_VERSION_CODE >= 0x010346 
3024
            if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT,"gdth",NULL))
3025
#else
3026
            if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT,"gdth"))
3027
#endif
3028
            {
3029
                printk("GDT-EISA: Unable to allocate IRQ\n");
3030
                restore_flags(flags);
3031
                scsi_unregister(shp);
3032
                continue;
3033
            }
3034
            shp->unchecked_isa_dma = 0;
3035
            shp->irq = ha->irq;
3036
            shp->dma_channel = 0xff;
3037
            for (i=0; i<MAXID; ++i) {
3038
                if (ha->id[0][i].type==SIOP_DTYP) {
3039
                    shp->this_id = i;
3040
                    break;
3041
                }
3042
            }
3043
            hanum = gdth_ctr_count;
3044
            gdth_ctr_tab[gdth_ctr_count++] = shp;
3045
            gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
3046
 
3047
            NUMDATA(shp)->hanum = (ushort)hanum;
3048
            NUMDATA(shp)->busnum= 0;
3049
            TRACE2(("EISA detect Bus 0: shp %lx hanum %d\n",
3050
                          (ulong)shp,NUMDATA(shp)->hanum));
3051
 
3052
            ha->pccb = CMDDATA(shp);
3053
            ha->pscratch = DMADATA(shp);
3054
            ha->req_first = NULL;
3055
            for (i=0; i<MAXBUS; ++i) {
3056
                for (j=0; j<MAXID; ++j) {
3057
                    ha->id[i][j].type = EMPTY_DTYP;
3058
                    ha->id[i][j].lock = 0;
3059
                    ha->id[i][j].heads = 0;
3060
                }
3061
            }
3062
            restore_flags(flags);
3063
 
3064
            if (!gdth_search_drives(hanum)) {
3065
                printk("GDT-EISA: Error during device scan\n");
3066
                --gdth_ctr_count;
3067
                --gdth_ctr_vcount;
3068
                save_flags(flags);
3069
                cli();
3070
#if LINUX_VERSION_CODE >= 0x010346 
3071
                free_irq(ha->irq,NULL);
3072
#else
3073
                free_irq(ha->irq);
3074
#endif
3075
                restore_flags(flags);
3076
                scsi_unregister(shp);
3077
                continue;
3078
            }
3079
 
3080
#if LINUX_VERSION_CODE >= 0x020000
3081
            shp->max_id      = 8;
3082
            shp->max_lun     = MAXLUN;
3083
            shp->max_channel = ha->bus_cnt - 1;
3084
#else
3085
            /* register addit. SCSI channels as virtual controllers */
3086
            for (b=1; b<ha->bus_cnt; ++b) {
3087
                shp = scsi_register(shtp,sizeof(gdth_num_str));
3088
                shp->unchecked_isa_dma = 0;
3089
                shp->irq = ha->irq;
3090
                shp->dma_channel = 0xff;
3091
                for (i=0; i<MAXID; ++i) {
3092
                    if (ha->id[b][i].type==SIOP_DTYP) {
3093
                        shp->this_id = i;
3094
                        break;
3095
                    }
3096
                }
3097
                gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
3098
                NUMDATA(shp)->hanum = (ushort)hanum;
3099
                NUMDATA(shp)->busnum = b;
3100
                TRACE2(("EISA detect Bus %d: shp %lx hanum %d\n",
3101
                              NUMDATA(shp)->busnum,(ulong)shp,
3102
                              NUMDATA(shp)->hanum));
3103
            }
3104
#endif
3105
 
3106
            gdth_enable_int(hanum);
3107
        }
3108
    }
3109
 
3110
    /* scanning for PCI controllers */
3111
    for (device_id = 0; device_id <= PCI_DEVICE_ID_VORTEX_GDTMAXRP; ++device_id) {
3112
        if (device_id > PCI_DEVICE_ID_VORTEX_GDT6555 &&
3113
            device_id < PCI_DEVICE_ID_VORTEX_GDT6x17RP)
3114
            continue;
3115
        for (index = 0; ; ++index) {
3116
            if (!gdth_search_pci(device_id,index,&pcistr))
3117
                break;                          /* next device_id */
3118
            shp = scsi_register(shtp,sizeof(gdth_ext_str));
3119
            ha = HADATA(shp);
3120
            if (!gdth_init_pci(&pcistr,ha)) {
3121
                scsi_unregister(shp);
3122
                continue;
3123
            }
3124
            /* controller found and initialized */
3125
            printk("Configuring GDT-PCI HA at %d/%d IRQ %u\n",
3126
                   pcistr.bus,pcistr.device_fn>>3,ha->irq);
3127
 
3128
            save_flags(flags);
3129
            cli();
3130
#if LINUX_VERSION_CODE >= 0x02015F 
3131
            if (request_irq(ha->irq,do_gdth_interrupt,SA_INTERRUPT|SA_SHIRQ,"gdth",NULL))
3132
#elif LINUX_VERSION_CODE >= 0x010346 
3133
            if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT|SA_SHIRQ,"gdth",NULL))
3134
#else
3135
            if (request_irq(ha->irq,gdth_interrupt,SA_INTERRUPT|SA_SHIRQ,"gdth"))
3136
#endif
3137
            {
3138
                printk("GDT-PCI: Unable to allocate IRQ\n");
3139
                restore_flags(flags);
3140
                scsi_unregister(shp);
3141
                continue;
3142
            }
3143
            shp->unchecked_isa_dma = 0;
3144
            shp->irq = ha->irq;
3145
            shp->dma_channel = 0xff;
3146
            for (i=0; i<MAXID; ++i) {
3147
                if (ha->id[0][i].type==SIOP_DTYP) {
3148
                    shp->this_id = i;
3149
                    break;
3150
                }
3151
            }
3152
            hanum = gdth_ctr_count;
3153
            gdth_ctr_tab[gdth_ctr_count++] = shp;
3154
            gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
3155
 
3156
            NUMDATA(shp)->hanum = (ushort)hanum;
3157
            NUMDATA(shp)->busnum= 0;
3158
 
3159
            ha->pccb = CMDDATA(shp);
3160
            ha->pscratch = DMADATA(shp);
3161
            ha->req_first = NULL;
3162
            for (i=0; i<MAXBUS; ++i) {
3163
                for (j=0; j<MAXID; ++j) {
3164
                    ha->id[i][j].type = EMPTY_DTYP;
3165
                    ha->id[i][j].lock = 0;
3166
                    ha->id[i][j].heads = 0;
3167
                }
3168
            }
3169
            restore_flags(flags);
3170
 
3171
            if (!gdth_search_drives(hanum)) {
3172
                printk("GDT-PCI: Error during device scan\n");
3173
                --gdth_ctr_count;
3174
                --gdth_ctr_vcount;
3175
                save_flags(flags);
3176
                cli();
3177
#if LINUX_VERSION_CODE >= 0x010346 
3178
                free_irq(ha->irq,NULL);
3179
#else
3180
                free_irq(ha->irq);
3181
#endif
3182
                restore_flags(flags);
3183
                scsi_unregister(shp);
3184
                continue;
3185
            }
3186
 
3187
#if LINUX_VERSION_CODE >= 0x020000
3188
            shp->max_id      = MAXID;
3189
            shp->max_lun     = MAXLUN;
3190
            shp->max_channel = ha->bus_cnt - 1;
3191
#else
3192
            /* register addit. SCSI channels as virtual controllers */
3193
            for (b=1; b<ha->bus_cnt; ++b) {
3194
                shp = scsi_register(shtp,sizeof(gdth_num_str));
3195
                shp->unchecked_isa_dma = 0;
3196
                shp->irq = ha->irq;
3197
                shp->dma_channel = 0xff;
3198
                for (i=0; i<MAXID; ++i) {
3199
                    if (ha->id[b][i].type==SIOP_DTYP) {
3200
                        shp->this_id = i;
3201
                        break;
3202
                    }
3203
                }
3204
                gdth_ctr_vtab[gdth_ctr_vcount++] = shp;
3205
                NUMDATA(shp)->hanum = (ushort)hanum;
3206
                NUMDATA(shp)->busnum = b;
3207
            }
3208
#endif
3209
 
3210
            gdth_enable_int(hanum);
3211
        }
3212
    }
3213
 
3214
    TRACE2(("gdth_detect() %d controller detected\n",gdth_ctr_count));
3215
    if (gdth_ctr_count > 0) {
3216
#ifdef GDTH_STATISTICS
3217
        TRACE2(("gdth_detect(): Initializing timer !\n"));
3218
        init_timer(&gdth_timer);
3219
        gdth_timer.expires = jiffies + HZ;
3220
        gdth_timer.data = 0L;
3221
        gdth_timer.function = gdth_timeout;
3222
        add_timer(&gdth_timer);
3223
#endif
3224
#if LINUX_VERSION_CODE >= 0x020100
3225
        register_reboot_notifier(&gdth_notifier);
3226
#endif
3227
    }
3228
    gdth_polling = FALSE;
3229
    return gdth_ctr_vcount;
3230
}
3231
 
3232
 
3233
int gdth_release(struct Scsi_Host *shp)
3234
{
3235
    unsigned long flags;
3236
 
3237
    TRACE2(("gdth_release()\n"));
3238
 
3239
    if (NUMDATA(shp)->busnum == 0) {
3240
        gdth_flush(NUMDATA(shp)->hanum);
3241
 
3242
        save_flags(flags);
3243
        cli();
3244
        if (shp->irq) {
3245
#if LINUX_VERSION_CODE >= 0x010346
3246
            free_irq(shp->irq,NULL);
3247
#else
3248
            free_irq(shp->irq);
3249
#endif
3250
        }
3251
        if (shp->dma_channel != 0xff) {
3252
            free_dma(shp->dma_channel);
3253
        }
3254
        restore_flags(flags);
3255
        gdth_ctr_released++;
3256
        TRACE2(("gdth_release(): HA %d of %d\n",
3257
                gdth_ctr_released, gdth_ctr_count));
3258
 
3259
        if (gdth_ctr_released == gdth_ctr_count) {
3260
#ifdef GDTH_STATISTICS
3261
            del_timer(&gdth_timer);
3262
#endif
3263
#if LINUX_VERSION_CODE >= 0x020100
3264
            unregister_reboot_notifier(&gdth_notifier);
3265
#endif
3266
        }
3267
    }
3268
 
3269
    scsi_unregister(shp);
3270
    return 0;
3271
}
3272
 
3273
 
3274
static const char *gdth_ctr_name(int hanum)
3275
{
3276
    gdth_ha_str *ha;
3277
 
3278
    TRACE2(("gdth_ctr_name()\n"));
3279
 
3280
    ha    = HADATA(gdth_ctr_tab[hanum]);
3281
 
3282
    if (ha->type == GDT_EISA) {
3283
        switch (ha->stype) {
3284
          case GDT3_ID:
3285
            return("GDT3000/3020");
3286
          case GDT3A_ID:
3287
            return("GDT3000A/3020A/3050A");
3288
          case GDT3B_ID:
3289
            return("GDT3000B/3010A");
3290
        }
3291
    } else if (ha->type == GDT_ISA) {
3292
        return("GDT2000/2020");
3293
    } else if (ha->type == GDT_PCI) {
3294
        switch (ha->stype) {
3295
          case PCI_DEVICE_ID_VORTEX_GDT60x0:
3296
            return("GDT6000/6020/6050");
3297
          case PCI_DEVICE_ID_VORTEX_GDT6000B:
3298
            return("GDT6000B/6010");
3299
        }
3300
    }
3301
    /* new controllers (GDT_PCINEW, GDT_PCIMPR, ..) use board_info IOCTL! */
3302
 
3303
    return("");
3304
}
3305
 
3306
const char *gdth_info(struct Scsi_Host *shp)
3307
{
3308
    int hanum;
3309
    gdth_ha_str *ha;
3310
 
3311
    TRACE2(("gdth_info()\n"));
3312
    hanum = NUMDATA(shp)->hanum;
3313
    ha    = HADATA(gdth_ctr_tab[hanum]);
3314
 
3315
    return ((const char *)ha->ctr_name);
3316
}
3317
 
3318
/* old error handling */
3319
int gdth_abort(Scsi_Cmnd *scp)
3320
{
3321
    TRACE2(("gdth_abort() reason %d\n",scp->abort_reason));
3322
    return SCSI_ABORT_SNOOZE;
3323
}
3324
 
3325
#if LINUX_VERSION_CODE >= 0x010346
3326
int gdth_reset(Scsi_Cmnd *scp, unsigned int reset_flags)
3327
#else
3328
int gdth_reset(Scsi_Cmnd *scp)
3329
#endif
3330
{
3331
    TRACE2(("gdth_reset()\n"));
3332
    return SCSI_RESET_PUNT;
3333
}
3334
 
3335
#if LINUX_VERSION_CODE >= 0x02015F
3336
/* new error handling */
3337
int gdth_eh_abort(Scsi_Cmnd *scp)
3338
{
3339
    TRACE2(("gdth_eh_abort()\n"));
3340
    return FAILED;
3341
}
3342
 
3343
int gdth_eh_device_reset(Scsi_Cmnd *scp)
3344
{
3345
    TRACE2(("gdth_eh_device_reset()\n"));
3346
    return FAILED;
3347
}
3348
 
3349
int gdth_eh_bus_reset(Scsi_Cmnd *scp)
3350
{
3351
    TRACE2(("gdth_eh_bus_reset()\n"));
3352
    return FAILED;
3353
}
3354
 
3355
int gdth_eh_host_reset(Scsi_Cmnd *scp)
3356
{
3357
    TRACE2(("gdth_eh_host_reset()\n"));
3358
    return FAILED;
3359
}
3360
#endif
3361
 
3362
#if LINUX_VERSION_CODE >= 0x010300
3363
int gdth_bios_param(Disk *disk,kdev_t dev,int *ip)
3364
#else
3365
int gdth_bios_param(Disk *disk,int dev,int *ip)
3366
#endif
3367
{
3368
    unchar b, t;
3369
    int hanum;
3370
    gdth_ha_str *ha;
3371
    int drv_hds, drv_secs;
3372
 
3373
    hanum = NUMDATA(disk->device->host)->hanum;
3374
    b = disk->device->channel;
3375
    t = disk->device->id;
3376
    TRACE2(("gdth_bios_param() ha %d bus %d target %d\n", hanum, b, t));
3377
    ha = HADATA(gdth_ctr_tab[hanum]);
3378
 
3379
    if (ha->id[b][t].heads == 0) {
3380
        /* raw device: evaluate mapping (sectors per head, heads per cylinder) */
3381
        if (disk->capacity /HEADS/SECS <= MAXCYLS) {
3382
            drv_hds = HEADS;
3383
            drv_secs= SECS;
3384
        } else if (disk->capacity /MEDHEADS/MEDSECS <= MAXCYLS) {
3385
            drv_hds = MEDHEADS;
3386
            drv_secs= MEDSECS;
3387
        } else {
3388
            drv_hds = BIGHEADS;
3389
            drv_secs= BIGSECS;
3390
        }
3391
        ha->id[b][t].heads = drv_hds;
3392
        ha->id[b][t].secs = drv_secs;
3393
        TRACE2(("gdth_bios_param(): raw device -> params evaluated\n"));
3394
    }
3395
 
3396
    ip[0] = ha->id[b][t].heads;
3397
    ip[1] = ha->id[b][t].secs;
3398
    ip[2] = disk->capacity / ip[0] / ip[1];
3399
 
3400
    TRACE2(("gdth_bios_param(): %d heads, %d secs, %d cyls\n",
3401
            ip[0],ip[1],ip[2]));
3402
    return 0;
3403
}
3404
 
3405
 
3406
static void internal_done(Scsi_Cmnd *scp)
3407
{
3408
    scp->SCp.sent_command++;
3409
}
3410
 
3411
int gdth_command(Scsi_Cmnd *scp)
3412
{
3413
    TRACE2(("gdth_command()\n"));
3414
 
3415
    scp->SCp.sent_command = 0;
3416
    gdth_queuecommand(scp,internal_done);
3417
 
3418
    while (!scp->SCp.sent_command)
3419
        barrier();
3420
    return scp->result;
3421
}
3422
 
3423
 
3424
int gdth_queuecommand(Scsi_Cmnd *scp,void (*done)(Scsi_Cmnd *))
3425
{
3426
    int hanum;
3427
    int priority;
3428
 
3429
    TRACE(("gdth_queuecommand() cmd 0x%x id %d lun %d\n",
3430
                  scp->cmnd[0],scp->target,scp->lun));
3431
 
3432
    scp->scsi_done = (void *)done;
3433
    scp->SCp.have_data_in = 1;
3434
    hanum = NUMDATA(scp->host)->hanum;
3435
#ifdef GDTH_STATISTICS
3436
    ++act_ios;
3437
#endif
3438
 
3439
    priority = DEFAULT_PRI;
3440
#if LINUX_VERSION_CODE >= 0x010300
3441
    if (scp->done == gdth_scsi_done)
3442
        priority = scp->SCp.this_residual;
3443
#endif
3444
    gdth_putq( hanum, scp, priority );
3445
    gdth_next( hanum );
3446
    return 0;
3447
}
3448
 
3449
/* flush routine */
3450
static void gdth_flush(int hanum)
3451
{
3452
    int             i, j;
3453
    gdth_ha_str     *ha;
3454
    Scsi_Cmnd       scp;
3455
    Scsi_Device     sdev;
3456
    gdth_cmd_str    gdtcmd;
3457
    char            cmnd[12];
3458
 
3459
    TRACE2(("gdth_flush() hanum %d\n",hanum));
3460
    ha = HADATA(gdth_ctr_tab[hanum]);
3461
    memset(&sdev,0,sizeof(Scsi_Device));
3462
    memset(&scp, 0,sizeof(Scsi_Cmnd));
3463
    sdev.host = gdth_ctr_tab[hanum];
3464
    sdev.id = sdev.host->this_id;
3465
    scp.cmd_len = 12;
3466
    scp.host = gdth_ctr_tab[hanum];
3467
    scp.target = sdev.host->this_id;
3468
    scp.device = &sdev;
3469
    scp.use_sg = 0;
3470
 
3471
    for (i = 0; i < MAXBUS; ++i) {
3472
        for (j = 0; j < MAXID; ++j) {
3473
            if (ha->id[i][j].type == CACHE_DTYP) {
3474
                gdtcmd.BoardNode = LOCALBOARD;
3475
                gdtcmd.Service = CACHESERVICE;
3476
                gdtcmd.OpCode = GDT_FLUSH;
3477
                gdtcmd.u.cache.DeviceNo = ha->id[i][j].hostdrive;
3478
                gdtcmd.u.cache.BlockNo = 1;
3479
                gdtcmd.u.cache.sg_canz = 0;
3480
                TRACE2(("gdth_flush(): flush ha %d drive %d\n",
3481
                        hanum, ha->id[i][j].hostdrive));
3482
                {
3483
                    struct semaphore sem = MUTEX_LOCKED;
3484
                    scp.request.rq_status = RQ_SCSI_BUSY;
3485
                    scp.request.sem = &sem;
3486
                    scp.SCp.this_residual = IOCTL_PRI;
3487
                    scsi_do_cmd(&scp, cmnd, &gdtcmd,
3488
                                sizeof(gdth_cmd_str), gdth_scsi_done,
3489
                                30*HZ, 1);
3490
                    down(&sem);
3491
                }
3492
            }
3493
        }
3494
    }
3495
}
3496
 
3497
/* shutdown routine */
3498
#if LINUX_VERSION_CODE >= 0x020100
3499
static int gdth_halt(struct notifier_block *nb, ulong event, void *buf)
3500
#else
3501
void gdth_halt(void)
3502
#endif
3503
{
3504
    int             hanum;
3505
    Scsi_Cmnd       scp;
3506
    Scsi_Device     sdev;
3507
    gdth_cmd_str    gdtcmd;
3508
    char            cmnd[12];
3509
 
3510
#if LINUX_VERSION_CODE >= 0x020100
3511
    TRACE2(("gdth_halt() event %d\n",event));
3512
    if (event != SYS_RESTART && event != SYS_HALT && event != SYS_POWER_OFF)
3513
        return NOTIFY_DONE;
3514
#else
3515
    TRACE2(("gdth_halt()\n"));
3516
    if (halt_called) {
3517
        TRACE2(("already called\n"));
3518
        return;
3519
    }
3520
    halt_called = TRUE;
3521
#endif
3522
    printk("GDT: Flushing all host drives .. ");
3523
    for (hanum = 0; hanum < gdth_ctr_count; ++hanum) {
3524
        gdth_flush(hanum);
3525
 
3526
        /* controller reset */
3527
        memset(&sdev,0,sizeof(Scsi_Device));
3528
        memset(&scp, 0,sizeof(Scsi_Cmnd));
3529
        sdev.host = gdth_ctr_tab[hanum];
3530
        sdev.id = sdev.host->this_id;
3531
        scp.cmd_len = 12;
3532
        scp.host = gdth_ctr_tab[hanum];
3533
        scp.target = sdev.host->this_id;
3534
        scp.device = &sdev;
3535
        scp.use_sg = 0;
3536
 
3537
        gdtcmd.BoardNode = LOCALBOARD;
3538
        gdtcmd.Service = CACHESERVICE;
3539
        gdtcmd.OpCode = GDT_RESET;
3540
        TRACE2(("gdth_halt(): reset controller %d\n", hanum));
3541
        {
3542
            struct semaphore sem = MUTEX_LOCKED;
3543
            scp.request.rq_status = RQ_SCSI_BUSY;
3544
            scp.request.sem = &sem;
3545
            scp.SCp.this_residual = IOCTL_PRI;
3546
            scsi_do_cmd(&scp, cmnd, &gdtcmd,
3547
                sizeof(gdth_cmd_str), gdth_scsi_done,
3548
                10*HZ, 1);
3549
            down(&sem);
3550
        }
3551
    }
3552
    printk("Done.\n");
3553
 
3554
#ifdef GDTH_STATISTICS
3555
    del_timer(&gdth_timer);
3556
#endif
3557
#if LINUX_VERSION_CODE >= 0x020100
3558
    unregister_reboot_notifier(&gdth_notifier);
3559
    return NOTIFY_OK;
3560
#endif
3561
}
3562
 
3563
 
3564
/* called from init/main.c */
3565
__initfunc (void gdth_setup(char *str,int *ints))
3566
{
3567
    static size_t setup_idx = 0;
3568
 
3569
    TRACE2(("gdth_setup() str %s ints[0] %d ints[1] %d\n",
3570
                  str ? str:"NULL", ints[0],
3571
                  ints[0] ? ints[1]:0));
3572
 
3573
    if (setup_idx >= MAXHA) {
3574
        printk("GDT: gdth_setup() called too many times. Bad LILO params ?\n");
3575
        return;
3576
    }
3577
    if (ints[0] != 1) {
3578
        printk("GDT: Illegal command line !\n");
3579
        printk("Usage: gdth=<IRQ>\n");
3580
        printk("Where: <IRQ>: valid EISA controller IRQ (10,11,12,14)\n");
3581
        printk("              or 0 to disable controller driver\n");
3582
        return;
3583
    }
3584
    if (ints[1] == 10 || ints[1] == 11 || ints[1] == 12 || ints[1] == 14) {
3585
        irqs[setup_idx++] = ints[1];
3586
        irqs[setup_idx]   = 0xff;
3587
        return;
3588
    }
3589
    if (ints[1] == 0) {
3590
        disable_gdth_scan = TRUE;
3591
        return;
3592
    }
3593
    printk("GDT: Invalid IRQ (%d) specified\n",ints[1]);
3594
}
3595
 
3596
 
3597
#ifdef MODULE
3598
Scsi_Host_Template driver_template = GDTH;
3599
#include "scsi_module.c"
3600
#endif
3601
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.