make oldconfig will rebuild these...
[linux-2.4.21-pre4.git] / drivers / scsi / fdomain.c
1 /* fdomain.c -- Future Domain TMC-16x0 SCSI driver
2  * Created: Sun May  3 18:53:19 1992 by faith@cs.unc.edu
3  * Revised: Mon Dec 28 21:59:02 1998 by faith@acm.org
4  * Author: Rickard E. Faith, faith@cs.unc.edu
5  * Copyright 1992-1996, 1998 Rickard E. Faith (faith@acm.org)
6  * Shared IRQ supported added 7/7/2001  Alan Cox <alan@redhat.com>
7
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License as published by the
10  * Free Software Foundation; either version 2, or (at your option) any
11  * later version.
12
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 675 Mass Ave, Cambridge, MA 02139, USA.
21
22  **************************************************************************
23
24  SUMMARY:
25
26  Future Domain BIOS versions supported for autodetect:
27     2.0, 3.0, 3.2, 3.4 (1.0), 3.5 (2.0), 3.6, 3.61
28  Chips are supported:
29     TMC-1800, TMC-18C50, TMC-18C30, TMC-36C70
30  Boards supported:
31     Future Domain TMC-1650, TMC-1660, TMC-1670, TMC-1680, TMC-1610M/MER/MEX
32     Future Domain TMC-3260 (PCI)
33     Quantum ISA-200S, ISA-250MG
34     Adaptec AHA-2920A (PCI) [BUT *NOT* AHA-2920C -- use aic7xxx instead]
35     IBM ?
36  LILO/INSMOD command-line options:
37     fdomain=<PORT_BASE>,<IRQ>[,<ADAPTER_ID>]
38
39
40     
41  NOTE:
42
43  The Adaptec AHA-2920C has an Adaptec AIC-7850 chip on it.
44  Use the aic7xxx driver for this board.
45        
46  The Adaptec AHA-2920A has a Future Domain chip on it, so this is the right
47  driver for that card.  Unfortunately, the boxes will probably just say
48  "2920", so you'll have to look on the card for a Future Domain logo, or a
49  letter after the 2920.
50
51  
52  
53  THANKS:
54
55  Thanks to Adaptec for providing PCI boards for testing.  This finally
56  enabled me to test the PCI detection and correct it for PCI boards that do
57  not have a BIOS at a standard ISA location.  For PCI boards, LILO/INSMOD
58  command-line options should no longer be needed.  --RF 18Nov98
59
60
61  
62  DESCRIPTION:
63  
64  This is the Linux low-level SCSI driver for Future Domain TMC-1660/1680
65  TMC-1650/1670, and TMC-3260 SCSI host adapters.  The 1650 and 1670 have a
66  25-pin external connector, whereas the 1660 and 1680 have a SCSI-2 50-pin
67  high-density external connector.  The 1670 and 1680 have floppy disk
68  controllers built in.  The TMC-3260 is a PCI bus card.
69
70  Future Domain's older boards are based on the TMC-1800 chip, and this
71  driver was originally written for a TMC-1680 board with the TMC-1800 chip.
72  More recently, boards are being produced with the TMC-18C50 and TMC-18C30
73  chips.  The latest and greatest board may not work with this driver.  If
74  you have to patch this driver so that it will recognize your board's BIOS
75  signature, then the driver may fail to function after the board is
76  detected.
77
78  Please note that the drive ordering that Future Domain implemented in BIOS
79  versions 3.4 and 3.5 is the opposite of the order (currently) used by the
80  rest of the SCSI industry.  If you have BIOS version 3.4 or 3.5, and have
81  more then one drive, then the drive ordering will be the reverse of that
82  which you see under DOS.  For example, under DOS SCSI ID 0 will be D: and
83  SCSI ID 1 will be C: (the boot device).  Under Linux, SCSI ID 0 will be
84  /dev/sda and SCSI ID 1 will be /dev/sdb.  The Linux ordering is consistent
85  with that provided by all the other SCSI drivers for Linux.  If you want
86  this changed, you will probably have to patch the higher level SCSI code.
87  If you do so, please send me patches that are protected by #ifdefs.
88
89  If you have a TMC-8xx or TMC-9xx board, then this is not the driver for
90  your board.  Please refer to the Seagate driver for more information and
91  possible support.
92
93  
94  
95  HISTORY:
96
97  Linux       Driver      Driver
98  Version     Version     Date         Support/Notes
99
100              0.0          3 May 1992  V2.0 BIOS; 1800 chip
101  0.97        1.9         28 Jul 1992
102  0.98.6      3.1         27 Nov 1992
103  0.99        3.2          9 Dec 1992
104
105  0.99.3      3.3         10 Jan 1993  V3.0 BIOS
106  0.99.5      3.5         18 Feb 1993
107  0.99.10     3.6         15 May 1993  V3.2 BIOS; 18C50 chip
108  0.99.11     3.17         3 Jul 1993  (now under RCS)
109  0.99.12     3.18        13 Aug 1993
110  0.99.14     5.6         31 Oct 1993  (reselection code removed)
111
112  0.99.15     5.9         23 Jan 1994  V3.4 BIOS (preliminary)
113  1.0.8/1.1.1 5.15         1 Apr 1994  V3.4 BIOS; 18C30 chip (preliminary)
114  1.0.9/1.1.3 5.16         7 Apr 1994  V3.4 BIOS; 18C30 chip
115  1.1.38      5.18        30 Jul 1994  36C70 chip (PCI version of 18C30)
116  1.1.62      5.20         2 Nov 1994  V3.5 BIOS
117  1.1.73      5.22         7 Dec 1994  Quantum ISA-200S board; V2.0 BIOS
118
119  1.1.82      5.26        14 Jan 1995  V3.5 BIOS; TMC-1610M/MER/MEX board
120  1.2.10      5.28         5 Jun 1995  Quantum ISA-250MG board; V2.0, V2.01 BIOS
121  1.3.4       5.31        23 Jun 1995  PCI BIOS-32 detection (preliminary)
122  1.3.7       5.33         4 Jul 1995  PCI BIOS-32 detection
123  1.3.28      5.36        17 Sep 1995  V3.61 BIOS; LILO command-line support
124  1.3.34      5.39        12 Oct 1995  V3.60 BIOS; /proc
125  1.3.72      5.39         8 Feb 1996  Adaptec AHA-2920 board
126  1.3.85      5.41         4 Apr 1996
127  2.0.12      5.44         8 Aug 1996  Use ID 7 for all PCI cards
128  2.1.1       5.45         2 Oct 1996  Update ROM accesses for 2.1.x
129  2.1.97      5.46        23 Apr 1998  Rewritten PCI detection routines [mj]
130  2.1.11x     5.47         9 Aug 1998  Touched for 8 SCSI disk majors support
131              5.48        18 Nov 1998  BIOS no longer needed for PCI detection
132  2.2.0       5.50        28 Dec 1998  Support insmod parameters
133  
134
135  REFERENCES USED:
136
137  "TMC-1800 SCSI Chip Specification (FDC-1800T)", Future Domain Corporation,
138  1990.
139
140  "Technical Reference Manual: 18C50 SCSI Host Adapter Chip", Future Domain
141  Corporation, January 1992.
142
143  "LXT SCSI Products: Specifications and OEM Technical Manual (Revision
144  B/September 1991)", Maxtor Corporation, 1991.
145
146  "7213S product Manual (Revision P3)", Maxtor Corporation, 1992.
147
148  "Draft Proposed American National Standard: Small Computer System
149  Interface - 2 (SCSI-2)", Global Engineering Documents. (X3T9.2/86-109,
150  revision 10h, October 17, 1991)
151
152  Private communications, Drew Eckhardt (drew@cs.colorado.edu) and Eric
153  Youngdale (ericy@cais.com), 1992.
154
155  Private communication, Tuong Le (Future Domain Engineering department),
156  1994. (Disk geometry computations for Future Domain BIOS version 3.4, and
157  TMC-18C30 detection.)
158
159  Hogan, Thom. The Programmer's PC Sourcebook. Microsoft Press, 1988. Page
160  60 (2.39: Disk Partition Table Layout).
161
162  "18C30 Technical Reference Manual", Future Domain Corporation, 1993, page
163  6-1.
164
165
166  
167  NOTES ON REFERENCES:
168
169  The Maxtor manuals were free.  Maxtor telephone technical support is
170  great!
171
172  The Future Domain manuals were $25 and $35.  They document the chip, not
173  the TMC-16x0 boards, so some information I had to guess at.  In 1992,
174  Future Domain sold DOS BIOS source for $250 and the UN*X driver source was
175  $750, but these required a non-disclosure agreement, so even if I could
176  have afforded them, they would *not* have been useful for writing this
177  publically distributable driver.  Future Domain technical support has
178  provided some information on the phone and have sent a few useful FAXs.
179  They have been much more helpful since they started to recognize that the
180  word "Linux" refers to an operating system :-).
181
182  
183
184  ALPHA TESTERS:
185
186  There are many other alpha testers that come and go as the driver
187  develops.  The people listed here were most helpful in times of greatest
188  need (mostly early on -- I've probably left out a few worthy people in
189  more recent times):
190
191  Todd Carrico (todd@wutc.wustl.edu), Dan Poirier (poirier@cs.unc.edu ), Ken
192  Corey (kenc@sol.acs.unt.edu), C. de Bruin (bruin@bruin@sterbbs.nl), Sakari
193  Aaltonen (sakaria@vipunen.hit.fi), John Rice (rice@xanth.cs.odu.edu), Brad
194  Yearwood (brad@optilink.com), and Ray Toy (toy@soho.crd.ge.com).
195
196  Special thanks to Tien-Wan Yang (twyang@cs.uh.edu), who graciously lent me
197  his 18C50-based card for debugging.  He is the sole reason that this
198  driver works with the 18C50 chip.
199
200  Thanks to Dave Newman (dnewman@crl.com) for providing initial patches for
201  the version 3.4 BIOS.
202
203  Thanks to James T. McKinley (mckinley@msupa.pa.msu.edu) for providing
204  patches that support the TMC-3260, a PCI bus card with the 36C70 chip.
205  The 36C70 chip appears to be "completely compatible" with the 18C30 chip.
206
207  Thanks to Eric Kasten (tigger@petroglyph.cl.msu.edu) for providing the
208  patch for the version 3.5 BIOS.
209
210  Thanks for Stephen Henson (shenson@nyx10.cs.du.edu) for providing the
211  patch for the Quantum ISA-200S SCSI adapter.
212  
213  Thanks to Adam Bowen for the signature to the 1610M/MER/MEX scsi cards, to
214  Martin Andrews (andrewm@ccfadm.eeg.ccf.org) for the signature to some
215  random TMC-1680 repackaged by IBM; and to Mintak Ng (mintak@panix.com) for
216  the version 3.61 BIOS signature.
217
218  Thanks for Mark Singer (elf@netcom.com) and Richard Simpson
219  (rsimpson@ewrcsdra.demon.co.uk) for more Quantum signatures and detective
220  work on the Quantum RAM layout.
221
222  Special thanks to James T. McKinley (mckinley@msupa.pa.msu.edu) for
223  providing patches for proper PCI BIOS32-mediated detection of the TMC-3260
224  card (a PCI bus card with the 36C70 chip).  Please send James PCI-related
225  bug reports.
226
227  Thanks to Tom Cavin (tec@usa1.com) for preliminary command-line option
228  patches.
229
230  New PCI detection code written by Martin Mares <mj@atrey.karlin.mff.cuni.cz>
231
232  Insmod parameter code based on patches from Daniel Graham
233  <graham@balance.uoregon.edu>. 
234  
235  All of the alpha testers deserve much thanks.
236
237
238
239  NOTES ON USER DEFINABLE OPTIONS:
240
241  DEBUG: This turns on the printing of various debug information.
242
243  ENABLE_PARITY: This turns on SCSI parity checking.  With the current
244  driver, all attached devices must support SCSI parity.  If none of your
245  devices support parity, then you can probably get the driver to work by
246  turning this option off.  I have no way of testing this, however, and it
247  would appear that no one ever uses this option.
248
249  FIFO_COUNT: The host adapter has an 8K cache (host adapters based on the
250  18C30 chip have a 2k cache).  When this many 512 byte blocks are filled by
251  the SCSI device, an interrupt will be raised.  Therefore, this could be as
252  low as 0, or as high as 16.  Note, however, that values which are too high
253  or too low seem to prevent any interrupts from occurring, and thereby lock
254  up the machine.  I have found that 2 is a good number, but throughput may
255  be increased by changing this value to values which are close to 2.
256  Please let me know if you try any different values.
257
258  DO_DETECT: This activates some old scan code which was needed before the
259  high level drivers got fixed.  If you are having trouble with the driver,
260  turning this on should not hurt, and might help.  Please let me know if
261  this is the case, since this code will be removed from future drivers.
262
263  RESELECTION: This is no longer an option, since I gave up trying to
264  implement it in version 4.x of this driver.  It did not improve
265  performance at all and made the driver unstable (because I never found one
266  of the two race conditions which were introduced by the multiple
267  outstanding command code).  The instability seems a very high price to pay
268  just so that you don't have to wait for the tape to rewind.  If you want
269  this feature implemented, send me patches.  I'll be happy to send a copy
270  of my (broken) driver to anyone who would like to see a copy.
271
272  **************************************************************************/
273
274 #include <linux/module.h>
275
276 #ifdef PCMCIA
277 #undef MODULE
278 #endif
279
280 #include <linux/init.h>
281 #include <linux/sched.h>
282 #include <asm/io.h>
283 #include <linux/blk.h>
284 #include "scsi.h"
285 #include "hosts.h"
286 #include "fdomain.h"
287 #include <asm/system.h>
288 #include <linux/spinlock.h>
289 #include <linux/errno.h>
290 #include <linux/string.h>
291 #include <linux/ioport.h>
292 #include <linux/proc_fs.h>
293 #include <linux/pci.h>
294 #include <linux/stat.h>
295 #include <linux/delay.h>
296
297 #include <linux/config.h>       /* for CONFIG_PCI */
298   
299 #define VERSION          "$Revision: 1.1.1.1 $"
300
301 /* START OF USER DEFINABLE OPTIONS */
302
303 #define DEBUG            1      /* Enable debugging output */
304 #define ENABLE_PARITY    1      /* Enable SCSI Parity */
305 #define FIFO_COUNT       2      /* Number of 512 byte blocks before INTR */
306 #define DO_DETECT        0      /* Do device detection here (see scsi.c) */
307
308 /* END OF USER DEFINABLE OPTIONS */
309
310 #if DEBUG
311 #define EVERY_ACCESS     0      /* Write a line on every scsi access */
312 #define ERRORS_ONLY      1      /* Only write a line if there is an error */
313 #define DEBUG_DETECT     0      /* Debug fdomain_16x0_detect() */
314 #define DEBUG_MESSAGES   1      /* Debug MESSAGE IN phase */
315 #define DEBUG_ABORT      1      /* Debug abort() routine */
316 #define DEBUG_RESET      1      /* Debug reset() routine */
317 #define DEBUG_RACE       1      /* Debug interrupt-driven race condition */
318 #else
319 #define EVERY_ACCESS     0      /* LEAVE THESE ALONE--CHANGE THE ONES ABOVE */
320 #define ERRORS_ONLY      0
321 #define DEBUG_DETECT     0
322 #define DEBUG_MESSAGES   0
323 #define DEBUG_ABORT      0
324 #define DEBUG_RESET      0
325 #define DEBUG_RACE       0
326 #endif
327
328 /* Errors are reported on the line, so we don't need to report them again */
329 #if EVERY_ACCESS
330 #undef ERRORS_ONLY
331 #define ERRORS_ONLY      0
332 #endif
333
334 #if ENABLE_PARITY
335 #define PARITY_MASK      0x08
336 #else
337 #define PARITY_MASK      0x00
338 #endif
339
340 enum chip_type {
341    unknown          = 0x00,
342    tmc1800          = 0x01,
343    tmc18c50         = 0x02,
344    tmc18c30         = 0x03,
345 };
346
347 enum {
348    in_arbitration   = 0x02,
349    in_selection     = 0x04,
350    in_other         = 0x08,
351    disconnect       = 0x10,
352    aborted          = 0x20,
353    sent_ident       = 0x40,
354 };
355
356 enum in_port_type {
357    Read_SCSI_Data   =  0,
358    SCSI_Status      =  1,
359    TMC_Status       =  2,
360    FIFO_Status      =  3,       /* tmc18c50/tmc18c30 only */
361    Interrupt_Cond   =  4,       /* tmc18c50/tmc18c30 only */
362    LSB_ID_Code      =  5,
363    MSB_ID_Code      =  6,
364    Read_Loopback    =  7,
365    SCSI_Data_NoACK  =  8,
366    Interrupt_Status =  9,
367    Configuration1   = 10,
368    Configuration2   = 11,       /* tmc18c50/tmc18c30 only */
369    Read_FIFO        = 12,
370    FIFO_Data_Count  = 14
371 };
372
373 enum out_port_type {
374    Write_SCSI_Data  =  0,
375    SCSI_Cntl        =  1,
376    Interrupt_Cntl   =  2,
377    SCSI_Mode_Cntl   =  3,
378    TMC_Cntl         =  4,
379    Memory_Cntl      =  5,       /* tmc18c50/tmc18c30 only */
380    Write_Loopback   =  7,
381    IO_Control       = 11,       /* tmc18c30 only */
382    Write_FIFO       = 12
383 };
384
385 static int               port_base         = 0;
386 static unsigned long     bios_base         = 0;
387 static int               bios_major        = 0;
388 static int               bios_minor        = 0;
389 static int               PCI_bus           = 0;
390 static int               Quantum           = 0; /* Quantum board variant */
391 static int               interrupt_level   = 0;
392 static volatile int      in_command        = 0;
393 static Scsi_Cmnd         *current_SC       = NULL;
394 static enum chip_type    chip              = unknown;
395 static int               adapter_mask      = 0;
396 static int               this_id           = 0;
397 static int               setup_called      = 0;
398
399 #if DEBUG_RACE
400 static volatile int      in_interrupt_flag = 0;
401 #endif
402
403 static int               SCSI_Mode_Cntl_port;
404 static int               FIFO_Data_Count_port;
405 static int               Interrupt_Cntl_port;
406 static int               Interrupt_Status_port;
407 static int               Read_FIFO_port;
408 static int               Read_SCSI_Data_port;
409 static int               SCSI_Cntl_port;
410 static int               SCSI_Data_NoACK_port;
411 static int               SCSI_Status_port;
412 static int               TMC_Cntl_port;
413 static int               TMC_Status_port;
414 static int               Write_FIFO_port;
415 static int               Write_SCSI_Data_port;
416
417 static int               FIFO_Size = 0x2000; /* 8k FIFO for
418                                                 pre-tmc18c30 chips */
419
420 extern void              do_fdomain_16x0_intr( int irq, void *dev_id,
421                                             struct pt_regs * regs );
422
423 #ifdef MODULE
424                                 /* Allow insmod parameters to be like LILO
425                                    parameters.  For example:
426                                    insmod fdomain fdomain=0x140,11
427                                 */
428 static char * fdomain = NULL;
429 MODULE_PARM(fdomain, "s");
430 #endif
431
432 static unsigned long addresses[] = {
433    0xc8000,
434    0xca000,
435    0xce000,
436    0xde000,
437    0xcc000,             /* Extra addresses for PCI boards */
438    0xd0000,
439    0xe0000,
440 };
441 #define ADDRESS_COUNT (sizeof( addresses ) / sizeof( unsigned ))
442                        
443 static unsigned short ports[] = { 0x140, 0x150, 0x160, 0x170 };
444 #define PORT_COUNT (sizeof( ports ) / sizeof( unsigned short ))
445
446 static unsigned short ints[] = { 3, 5, 10, 11, 12, 14, 15, 0 };
447
448 /*
449
450   READ THIS BEFORE YOU ADD A SIGNATURE!
451
452   READING THIS SHORT NOTE CAN SAVE YOU LOTS OF TIME!
453
454   READ EVERY WORD, ESPECIALLY THE WORD *NOT*
455
456   This driver works *ONLY* for Future Domain cards using the TMC-1800,
457   TMC-18C50, or TMC-18C30 chip.  This includes models TMC-1650, 1660, 1670,
458   and 1680.  These are all 16-bit cards.
459
460   The following BIOS signature signatures are for boards which do *NOT*
461   work with this driver (these TMC-8xx and TMC-9xx boards may work with the
462   Seagate driver):
463
464   FUTURE DOMAIN CORP. (C) 1986-1988 V4.0I 03/16/88
465   FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89
466   FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89
467   FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90
468   FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90
469   FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90
470   FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92
471
472   (The cards which do *NOT* work are all 8-bit cards -- although some of
473   them have a 16-bit form-factor, the upper 8-bits are used only for IRQs
474   and are *NOT* used for data.  You can tell the difference by following
475   the tracings on the circuit board -- if only the IRQ lines are involved,
476   you have a "8-bit" card, and should *NOT* use this driver.)
477
478 */
479
480 struct signature {
481    const char *signature;
482    int  sig_offset;
483    int  sig_length;
484    int  major_bios_version;
485    int  minor_bios_version;
486    int  flag; /* 1 == PCI_bus, 2 == ISA_200S, 3 == ISA_250MG, 4 == ISA_200S */
487 } signatures[] = {
488    /*          1         2         3         4         5         6 */
489    /* 123456789012345678901234567890123456789012345678901234567890 */
490    { "FUTURE DOMAIN CORP. (C) 1986-1990 1800-V2.07/28/89",  5, 50,  2,  0, 0 },
491    { "FUTURE DOMAIN CORP. (C) 1986-1990 1800-V1.07/28/89",  5, 50,  2,  0, 0 },
492    { "FUTURE DOMAIN CORP. (C) 1986-1990 1800-V2.07/28/89", 72, 50,  2,  0, 2 },
493    { "FUTURE DOMAIN CORP. (C) 1986-1990 1800-V2.0",        73, 43,  2,  0, 3 },
494    { "FUTURE DOMAIN CORP. (C) 1991 1800-V2.0.",            72, 39,  2,  0, 4 },
495    { "FUTURE DOMAIN CORP. (C) 1992 V3.00.004/02/92",        5, 44,  3,  0, 0 },
496    { "FUTURE DOMAIN TMC-18XX (C) 1993 V3.203/12/93",        5, 44,  3,  2, 0 },
497    { "IBM F1 P2 BIOS v1.0104/29/93",                        5, 28,  3, -1, 0 },
498    { "Future Domain Corp. V1.0008/18/93",                   5, 33,  3,  4, 0 },
499    { "Future Domain Corp. V1.0008/18/93",                  26, 33,  3,  4, 1 },
500    { "Adaptec AHA-2920 PCI-SCSI Card",                     42, 31,  3, -1, 1 },
501    { "IBM F1 P264/32",                                      5, 14,  3, -1, 1 },
502                                 /* This next signature may not be a 3.5 bios */
503    { "Future Domain Corp. V2.0108/18/93",                   5, 33,  3,  5, 0 },
504    { "FUTURE DOMAIN CORP.  V3.5008/18/93",                  5, 34,  3,  5, 0 },
505    { "FUTURE DOMAIN 18c30/18c50/1800 (C) 1994 V3.5",        5, 44,  3,  5, 0 },
506    { "FUTURE DOMAIN CORP.  V3.6008/18/93",                  5, 34,  3,  6, 0 },
507    { "FUTURE DOMAIN CORP.  V3.6108/18/93",                  5, 34,  3,  6, 0 },
508    { "FUTURE DOMAIN TMC-18XX",                              5, 22, -1, -1, 0 },
509
510    /* READ NOTICE ABOVE *BEFORE* YOU WASTE YOUR TIME ADDING A SIGNATURE
511     Also, fix the disk geometry code for your signature and send your
512     changes for faith@cs.unc.edu.  Above all, do *NOT* change any old
513     signatures!
514
515     Note that the last line will match a "generic" 18XX bios.  Because
516     Future Domain has changed the host SCSI ID and/or the location of the
517     geometry information in the on-board RAM area for each of the first
518     three BIOS's, it is still important to enter a fully qualified
519     signature in the table for any new BIOS's (after the host SCSI ID and
520     geometry location are verified). */
521 };
522
523 #define SIGNATURE_COUNT (sizeof( signatures ) / sizeof( struct signature ))
524
525 static void print_banner( struct Scsi_Host *shpnt )
526 {
527    if (!shpnt) return;          /* This won't ever happen */
528
529    if (bios_major < 0 && bios_minor < 0) {
530       printk( "scsi%d: <fdomain> No BIOS; using scsi id %d\n",
531               shpnt->host_no, shpnt->this_id );
532    } else {
533       printk( "scsi%d: <fdomain> BIOS version ", shpnt->host_no );
534
535       if (bios_major >= 0) printk( "%d.", bios_major );
536       else                 printk( "?." );
537
538       if (bios_minor >= 0) printk( "%d", bios_minor );
539       else                 printk( "?." );
540    
541       printk( " at 0x%lx using scsi id %d\n",
542               bios_base, shpnt->this_id );
543    }
544
545                                 /* If this driver works for later FD PCI
546                                    boards, we will have to modify banner
547                                    for additional PCI cards, but for now if
548                                    it's PCI it's a TMC-3260 - JTM */
549    printk( "scsi%d: <fdomain> %s chip at 0x%x irq ",
550            shpnt->host_no,
551            chip == tmc1800 ? "TMC-1800"
552            : (chip == tmc18c50 ? "TMC-18C50"
553               : (chip == tmc18c30 ?
554                  (PCI_bus ? "TMC-36C70 (PCI bus)" : "TMC-18C30")
555                  : "Unknown")),
556            port_base );
557
558    if (interrupt_level) printk( "%d", interrupt_level );
559    else                 printk( "<none>" );
560
561    printk( "\n" );
562 }
563
564 static int __init fdomain_setup( char *str )
565 {
566         int ints[4];
567
568         (void)get_options(str, ARRAY_SIZE(ints), ints);
569
570         if (setup_called++ || ints[0] < 2 || ints[0] > 3) {
571                 printk( "scsi: <fdomain>"
572                 " Usage: fdomain=<PORT_BASE>,<IRQ>[,<ADAPTER_ID>]\n" );
573                 printk( "scsi: <fdomain> Bad LILO/INSMOD parameters?\n" );
574                 return 0;
575         }
576
577         port_base       = ints[0] >= 1 ? ints[1] : 0;
578         interrupt_level = ints[0] >= 2 ? ints[2] : 0;
579         this_id         = ints[0] >= 3 ? ints[3] : 0;
580    
581         bios_major = bios_minor = -1; /* Use geometry for BIOS version >= 3.4 */
582         ++setup_called;
583         return 1;
584 }
585
586 __setup("fdomain=", fdomain_setup);
587
588
589 static void do_pause( unsigned amount ) /* Pause for amount*10 milliseconds */
590 {
591    mdelay(10*amount);
592 }
593
594 inline static void fdomain_make_bus_idle( void )
595 {
596    outb( 0, SCSI_Cntl_port );
597    outb( 0, SCSI_Mode_Cntl_port );
598    if (chip == tmc18c50 || chip == tmc18c30)
599          outb( 0x21 | PARITY_MASK, TMC_Cntl_port ); /* Clear forced intr. */
600    else
601          outb( 0x01 | PARITY_MASK, TMC_Cntl_port );
602 }
603
604 static int fdomain_is_valid_port( int port )
605 {
606 #if DEBUG_DETECT 
607    printk( " (%x%x),",
608            inb( port + MSB_ID_Code ), inb( port + LSB_ID_Code ) );
609 #endif
610
611    /* The MCA ID is a unique id for each MCA compatible board.  We
612       are using ISA boards, but Future Domain provides the MCA ID
613       anyway.  We can use this ID to ensure that this is a Future
614       Domain TMC-1660/TMC-1680.
615     */
616
617    if (inb( port + LSB_ID_Code ) != 0xe9) { /* test for 0x6127 id */
618       if (inb( port + LSB_ID_Code ) != 0x27) return 0;
619       if (inb( port + MSB_ID_Code ) != 0x61) return 0;
620       chip = tmc1800;
621    } else {                                 /* test for 0xe960 id */
622       if (inb( port + MSB_ID_Code ) != 0x60) return 0;
623       chip = tmc18c50;
624
625                                 /* Try to toggle 32-bit mode.  This only
626                                    works on an 18c30 chip.  (User reports
627                                    say this works, so we should switch to
628                                    it in the near future.) */
629
630       outb( 0x80, port + IO_Control );
631       if ((inb( port + Configuration2 ) & 0x80) == 0x80) {
632          outb( 0x00, port + IO_Control );
633          if ((inb( port + Configuration2 ) & 0x80) == 0x00) {
634             chip = tmc18c30;
635             FIFO_Size = 0x800;  /* 2k FIFO */
636          }
637       }
638                                 /* If that failed, we are an 18c50. */
639    }
640
641    return 1;
642 }
643
644 static int fdomain_test_loopback( void )
645 {
646    int i;
647    int result;
648
649    for (i = 0; i < 255; i++) {
650       outb( i, port_base + Write_Loopback );
651       result = inb( port_base + Read_Loopback );
652       if (i != result)
653             return 1;
654    }
655    return 0;
656 }
657
658 /* fdomain_get_irq assumes that we have a valid MCA ID for a
659    TMC-1660/TMC-1680 Future Domain board.  Now, check to be sure the
660    bios_base matches these ports.  If someone was unlucky enough to have
661    purchased more than one Future Domain board, then they will have to
662    modify this code, as we only detect one board here.  [The one with the
663    lowest bios_base.]
664
665    Note that this routine is only used for systems without a PCI BIOS32
666    (e.g., ISA bus).  For PCI bus systems, this routine will likely fail
667    unless one of the IRQs listed in the ints array is used by the board.
668    Sometimes it is possible to use the computer's BIOS setup screen to
669    configure a PCI system so that one of these IRQs will be used by the
670    Future Domain card. */
671
672 static int fdomain_get_irq( int base )
673 {
674    int options = inb( base + Configuration1 );
675
676 #if DEBUG_DETECT
677    printk( "scsi: <fdomain> Options = %x\n", options );
678 #endif
679    
680                                 /* Check for board with lowest bios_base --
681                                    this isn't valid for the 18c30 or for
682                                    boards on the PCI bus, so just assume we
683                                    have the right board. */
684
685    if (chip != tmc18c30
686        && !PCI_bus
687        && addresses[ (options & 0xc0) >> 6 ] != bios_base) return 0;
688
689    return ints[ (options & 0x0e) >> 1 ];
690 }
691
692 static int fdomain_isa_detect( int *irq, int *iobase )
693 {
694    int i, j;
695    int base = 0xdeadbeef;
696    int flag = 0;
697
698 #if DEBUG_DETECT
699    printk( "scsi: <fdomain> fdomain_isa_detect:" );
700 #endif
701
702
703    for (i = 0; !bios_base && i < ADDRESS_COUNT; i++) {
704 #if DEBUG_DETECT
705       printk( " %lx(%lx),", addresses[i], bios_base );
706 #endif
707       for (j = 0; !bios_base && j < SIGNATURE_COUNT; j++) {
708          if (isa_check_signature(addresses[i] + signatures[j].sig_offset,
709                              signatures[j].signature,
710                              signatures[j].sig_length )) {
711             bios_major = signatures[j].major_bios_version;
712             bios_minor = signatures[j].minor_bios_version;
713             PCI_bus    = (signatures[j].flag == 1);
714             Quantum    = (signatures[j].flag > 1) ? signatures[j].flag : 0;
715             bios_base  = addresses[i];
716          }
717       }
718    }
719    
720    if (bios_major == 2) {
721       /* The TMC-1660/TMC-1680 has a RAM area just after the BIOS ROM.
722          Assuming the ROM is enabled (otherwise we wouldn't have been
723          able to read the ROM signature :-), then the ROM sets up the
724          RAM area with some magic numbers, such as a list of port
725          base addresses and a list of the disk "geometry" reported to
726          DOS (this geometry has nothing to do with physical geometry).
727        */
728
729       switch (Quantum) {
730       case 2:                   /* ISA_200S */
731       case 3:                   /* ISA_250MG */
732          base = isa_readb(bios_base + 0x1fa2) + (isa_readb(bios_base + 0x1fa3) << 8);
733          break;
734       case 4:                   /* ISA_200S (another one) */
735          base = isa_readb(bios_base + 0x1fa3) + (isa_readb(bios_base + 0x1fa4) << 8);
736          break;
737       default:
738          base = isa_readb(bios_base + 0x1fcc) + (isa_readb(bios_base + 0x1fcd) << 8);
739          break;
740       }
741    
742 #if DEBUG_DETECT
743       printk( " %x,", base );
744 #endif
745
746       for (flag = 0, i = 0; !flag && i < PORT_COUNT; i++) {
747          if (base == ports[i])
748                ++flag;
749       }
750
751       if (flag && fdomain_is_valid_port( base )) {
752          *irq    = fdomain_get_irq( base );
753          *iobase = base;
754          return 1;
755       }
756       
757       /* This is a bad sign.  It usually means that someone patched the
758          BIOS signature list (the signatures variable) to contain a BIOS
759          signature for a board *OTHER THAN* the TMC-1660/TMC-1680. */
760       
761 #if DEBUG_DETECT
762       printk( " RAM FAILED, " );
763 #endif
764    }
765
766    /* Anyway, the alternative to finding the address in the RAM is to just
767       search through every possible port address for one that is attached
768       to the Future Domain card.  Don't panic, though, about reading all
769       these random port addresses -- there are rumors that the Future
770       Domain BIOS does something very similar.
771
772       Do not, however, check ports which the kernel knows are being used by
773       another driver. */
774
775    for (i = 0; i < PORT_COUNT; i++) {
776       base = ports[i];
777       if (check_region( base, 0x10 )) {
778 #if DEBUG_DETECT
779          printk( " (%x inuse),", base );
780 #endif
781          continue;
782       }
783 #if DEBUG_DETECT
784       printk( " %x,", base );
785 #endif
786       if ((flag = fdomain_is_valid_port( base ))) break;
787    }
788
789 #if DEBUG_DETECT
790    if (flag) printk( " SUCCESS\n" );
791    else      printk( " FAILURE\n" );
792 #endif
793
794    if (!flag) return 0;         /* iobase not found */
795
796    *irq    = fdomain_get_irq( base );
797    *iobase = base;
798
799    return 1;                    /* success */
800 }
801
802 /* PCI detection function: int fdomain_pci_bios_detect(int* irq, int*
803    iobase) This function gets the Interrupt Level and I/O base address from
804    the PCI configuration registers. */
805
806 #ifdef CONFIG_PCI
807 static int fdomain_pci_bios_detect( int *irq, int *iobase, struct pci_dev **ret_pdev )
808 {
809    unsigned int     pci_irq;                /* PCI interrupt line */
810    unsigned long    pci_base;               /* PCI I/O base address */
811    struct pci_dev   *pdev = NULL;
812
813    if (!pci_present()) return 0;
814
815 #if DEBUG_DETECT
816    /* Tell how to print a list of the known PCI devices from bios32 and
817       list vendor and device IDs being used if in debug mode.  */
818       
819    printk( "scsi: <fdomain> INFO: use lspci -v to see list of PCI devices\n" );
820    printk( "scsi: <fdomain> TMC-3260 detect:"
821            " Using Vendor ID: 0x%x and Device ID: 0x%x\n",
822            PCI_VENDOR_ID_FD, 
823            PCI_DEVICE_ID_FD_36C70 );
824 #endif 
825
826    if ((pdev = pci_find_device(PCI_VENDOR_ID_FD,
827                                PCI_DEVICE_ID_FD_36C70,
828                                pdev)) == NULL)
829      return 0;
830    if (pci_enable_device(pdev)) return 0;
831        
832 #if DEBUG_DETECT
833    printk( "scsi: <fdomain> TMC-3260 detect:"
834            " PCI bus %u, device %u, function %u\n",
835            pdev->bus->number,
836            PCI_SLOT(pdev->devfn),
837            PCI_FUNC(pdev->devfn));
838 #endif
839
840    /* We now have the appropriate device function for the FD board so we
841       just read the PCI config info from the registers.  */
842
843    pci_base = pci_resource_start(pdev, 0);
844    pci_irq = pdev->irq;
845
846    /* Now we have the I/O base address and interrupt from the PCI
847       configuration registers. */
848
849    *irq    = pci_irq;
850    *iobase = pci_base;
851    *ret_pdev = pdev;
852
853 #if DEBUG_DETECT
854    printk( "scsi: <fdomain> TMC-3260 detect:"
855            " IRQ = %d, I/O base = 0x%x [0x%lx]\n", *irq, *iobase, pci_base );
856 #endif
857
858    if (!fdomain_is_valid_port( *iobase )) {
859       printk( "scsi: <fdomain>"
860               " PCI card detected, but driver not loaded (invalid port)\n" );
861       return 0;
862    }
863
864                                 /* Fill in a few global variables.  Ugh. */
865    bios_major = bios_minor = -1;
866    PCI_bus    = 1;
867    Quantum    = 0;
868    bios_base  = 0;
869    
870    return 1;
871 }
872 #endif
873
874 int fdomain_16x0_detect( Scsi_Host_Template *tpnt )
875 {
876    int              retcode;
877    struct Scsi_Host *shpnt;
878    struct pci_dev *pdev = NULL;
879 #if DO_DETECT
880    int i = 0;
881    int j = 0;
882    const int        buflen = 255;
883    Scsi_Cmnd        SCinit;
884    unsigned char    do_inquiry[] =       { INQUIRY, 0, 0, 0, buflen, 0 };
885    unsigned char    do_request_sense[] = { REQUEST_SENSE, 0, 0, 0, buflen, 0 };
886    unsigned char    do_read_capacity[] = { READ_CAPACITY,
887                                            0, 0, 0, 0, 0, 0, 0, 0, 0 };
888    unsigned char    buf[buflen];
889 #endif
890
891    tpnt->proc_name = "fdomain";
892
893 #ifdef MODULE
894         if (fdomain)
895                 fdomain_setup(fdomain);
896 #endif
897    
898    if (setup_called) {
899 #if DEBUG_DETECT
900       printk( "scsi: <fdomain> No BIOS, using port_base = 0x%x, irq = %d\n",
901               port_base, interrupt_level );
902 #endif
903       if (!fdomain_is_valid_port( port_base )) {
904          printk( "scsi: <fdomain> Cannot locate chip at port base 0x%x\n",
905                  port_base );
906          printk( "scsi: <fdomain> Bad LILO/INSMOD parameters?\n" );
907          return 0;
908       }
909    } else {
910       int flag = 0;
911
912 #ifdef CONFIG_PCI
913                                 /* Try PCI detection first */
914       flag = fdomain_pci_bios_detect( &interrupt_level, &port_base, &pdev );
915 #endif
916       if (!flag) {
917                                 /* Then try ISA bus detection */
918          flag = fdomain_isa_detect( &interrupt_level, &port_base );
919
920          if (!flag) {
921             printk( "scsi: <fdomain> Detection failed (no card)\n" );
922             return 0;
923          }
924       }
925    }
926
927    SCSI_Mode_Cntl_port   = port_base + SCSI_Mode_Cntl;
928    FIFO_Data_Count_port  = port_base + FIFO_Data_Count;
929    Interrupt_Cntl_port   = port_base + Interrupt_Cntl;
930    Interrupt_Status_port = port_base + Interrupt_Status;
931    Read_FIFO_port        = port_base + Read_FIFO;
932    Read_SCSI_Data_port   = port_base + Read_SCSI_Data;
933    SCSI_Cntl_port        = port_base + SCSI_Cntl;
934    SCSI_Data_NoACK_port  = port_base + SCSI_Data_NoACK;
935    SCSI_Status_port      = port_base + SCSI_Status;
936    TMC_Cntl_port         = port_base + TMC_Cntl;
937    TMC_Status_port       = port_base + TMC_Status;
938    Write_FIFO_port       = port_base + Write_FIFO;
939    Write_SCSI_Data_port  = port_base + Write_SCSI_Data;
940
941    fdomain_16x0_reset( NULL, 0 );
942
943    if (fdomain_test_loopback()) {
944       printk( "scsi: <fdomain> Detection failed"
945               " (loopback test failed at port base 0x%x)\n", port_base );
946       if (setup_called) {
947          printk( "scsi: <fdomain> Bad LILO/INSMOD parameters?\n" );
948       }
949       return 0;
950    }
951
952    if (this_id) {
953       tpnt->this_id = (this_id & 0x07);
954       adapter_mask  = (1 << tpnt->this_id);
955    } else {
956       if (PCI_bus || (bios_major == 3 && bios_minor >= 2) || bios_major < 0) {
957          tpnt->this_id = 7;
958          adapter_mask  = 0x80;
959       } else {
960          tpnt->this_id = 6;
961          adapter_mask  = 0x40;
962       }
963    }
964
965                                 /* Print out a banner here in case we can't
966                                    get resources.  */
967
968    shpnt = scsi_register( tpnt, 0 );
969    if(shpnt == NULL)
970         return 0;
971    shpnt->irq = interrupt_level;
972    shpnt->io_port = port_base;
973    scsi_set_pci_device(shpnt, pdev);
974    shpnt->n_io_port = 0x10;
975    print_banner( shpnt );
976
977                                 /* Log IRQ with kernel */   
978    if (!interrupt_level) {
979       printk( "scsi: <fdomain>"
980               " Card Detected, but driver not loaded (no IRQ)\n" );
981       return 0;
982    } else {
983       /* Register the IRQ with the kernel */
984
985       retcode = request_irq( interrupt_level,
986                              do_fdomain_16x0_intr, pdev?SA_SHIRQ:0, "fdomain", shpnt);
987
988       if (retcode < 0) {
989          if (retcode == -EINVAL) {
990             printk( "scsi: <fdomain> IRQ %d is bad!\n", interrupt_level );
991             printk( "                This shouldn't happen!\n" );
992             printk( "                Send mail to faith@acm.org\n" );
993          } else if (retcode == -EBUSY) {
994             printk( "scsi: <fdomain> IRQ %d is already in use!\n",
995                     interrupt_level );
996             printk( "                Please use another IRQ!\n" );
997          } else {
998             printk( "scsi: <fdomain> Error getting IRQ %d\n",
999                     interrupt_level );
1000             printk( "                This shouldn't happen!\n" );
1001             printk( "                Send mail to faith@acm.org\n" );
1002          }
1003          printk( "scsi: <fdomain> Detected, but driver not loaded (IRQ)\n" );
1004          return 0;
1005       }
1006    }
1007
1008                                 /* Log I/O ports with kernel */
1009    request_region( port_base, 0x10, "fdomain" );
1010
1011 #if DO_DETECT
1012
1013    /* These routines are here because of the way the SCSI bus behaves after
1014       a reset.  This appropriate behavior was not handled correctly by the
1015       higher level SCSI routines when I first wrote this driver.  Now,
1016       however, correct scan routines are part of scsi.c and these routines
1017       are no longer needed.  However, this code is still good for
1018       debugging.  */
1019
1020    SCinit.request_buffer  = SCinit.buffer = buf;
1021    SCinit.request_bufflen = SCinit.bufflen = sizeof(buf)-1;
1022    SCinit.use_sg          = 0;
1023    SCinit.lun             = 0;
1024
1025    printk( "scsi: <fdomain> detection routine scanning for devices:\n" );
1026    for (i = 0; i < 8; i++) {
1027       SCinit.target = i;
1028       if (i == tpnt->this_id)   /* Skip host adapter */
1029             continue;
1030       memcpy(SCinit.cmnd, do_request_sense, sizeof(do_request_sense));
1031       retcode = fdomain_16x0_command(&SCinit);
1032       if (!retcode) {
1033          memcpy(SCinit.cmnd, do_inquiry, sizeof(do_inquiry));
1034          retcode = fdomain_16x0_command(&SCinit);
1035          if (!retcode) {
1036             printk( "     SCSI ID %d: ", i );
1037             for (j = 8; j < (buf[4] < 32 ? buf[4] : 32); j++)
1038                   printk( "%c", buf[j] >= 20 ? buf[j] : ' ' );
1039             memcpy(SCinit.cmnd, do_read_capacity, sizeof(do_read_capacity));
1040             retcode = fdomain_16x0_command(&SCinit);
1041             if (!retcode) {
1042                unsigned long blocks, size, capacity;
1043                
1044                blocks = (buf[0] << 24) | (buf[1] << 16)
1045                      | (buf[2] << 8) | buf[3];
1046                size = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
1047                capacity = +( +(blocks / 1024L) * +(size * 10L)) / 1024L;
1048                
1049                printk( "%lu MB (%lu byte blocks)",
1050                        ((capacity + 5L) / 10L), size );
1051             } else {
1052                memcpy(SCinit.cmnd, do_request_sense, sizeof(do_request_sense));
1053                retcode = fdomain_16x0_command(&SCinit);
1054             }
1055             printk ("\n" );
1056          } else {
1057             memcpy(SCinit.cmnd, do_request_sense, sizeof(do_request_sense));
1058             retcode = fdomain_16x0_command(&SCinit);
1059          }
1060       }
1061    }
1062 #endif
1063
1064    return 1;                    /* Maximum of one adapter will be detected. */
1065 }
1066
1067 const char *fdomain_16x0_info( struct Scsi_Host *ignore )
1068 {
1069    static char buffer[128];
1070    char        *pt;
1071    
1072    strcpy( buffer, "Future Domain 16-bit SCSI Driver Version" );
1073    if (strchr( VERSION, ':')) { /* Assume VERSION is an RCS Revision string */
1074       strcat( buffer, strchr( VERSION, ':' ) + 1 );
1075       pt = strrchr( buffer, '$') - 1;
1076       if (!pt)                  /* Stripped RCS Revision string? */
1077             pt = buffer + strlen( buffer ) - 1;
1078       if (*pt != ' ')
1079             ++pt;
1080       *pt = '\0';
1081    } else {                     /* Assume VERSION is a number */
1082       strcat( buffer, " " VERSION );
1083    }
1084       
1085    return buffer;
1086 }
1087
1088                                 /* First pass at /proc information routine. */
1089 /*
1090  * inout : decides on the direction of the dataflow and the meaning of the 
1091  *         variables
1092  * buffer: If inout==FALSE data is being written to it else read from it
1093  * *start: If inout==FALSE start of the valid data in the buffer
1094  * offset: If inout==FALSE offset from the beginning of the imaginary file 
1095  *         from which we start writing into the buffer
1096  * length: If inout==FALSE max number of bytes to be written into the buffer 
1097  *         else number of bytes in the buffer
1098  */
1099 int fdomain_16x0_proc_info( char *buffer, char **start, off_t offset,
1100                             int length, int hostno, int inout )
1101 {
1102    const char *info = fdomain_16x0_info( NULL );
1103    int        len;
1104    int        pos;
1105    int        begin;
1106
1107    if (inout) return(-ENOSYS);
1108     
1109    begin = 0;
1110    strcpy( buffer, info );
1111    strcat( buffer, "\n" );
1112
1113    pos = len = strlen( buffer );
1114
1115    if(pos < offset) {
1116       len = 0;
1117       begin = pos;
1118    }
1119     
1120    *start = buffer + (offset - begin);   /* Start of wanted data */
1121    len -= (offset - begin);
1122    if(len > length) len = length;
1123    
1124    return(len);
1125 }
1126    
1127 #if 0
1128 static int fdomain_arbitrate( void )
1129 {
1130    int           status = 0;
1131    unsigned long timeout;
1132
1133 #if EVERY_ACCESS
1134    printk( "fdomain_arbitrate()\n" );
1135 #endif
1136    
1137    outb( 0x00, SCSI_Cntl_port );              /* Disable data drivers */
1138    outb( adapter_mask, port_base + SCSI_Data_NoACK ); /* Set our id bit */
1139    outb( 0x04 | PARITY_MASK, TMC_Cntl_port ); /* Start arbitration */
1140
1141    timeout = 500;
1142    do {
1143       status = inb( TMC_Status_port );        /* Read adapter status */
1144       if (status & 0x02)                      /* Arbitration complete */
1145             return 0;
1146       mdelay(1);                        /* Wait one millisecond */
1147    } while (--timeout);
1148
1149    /* Make bus idle */
1150    fdomain_make_bus_idle();
1151
1152 #if EVERY_ACCESS
1153    printk( "Arbitration failed, status = %x\n", status );
1154 #endif
1155 #if ERRORS_ONLY
1156    printk( "scsi: <fdomain> Arbitration failed, status = %x\n", status );
1157 #endif
1158    return 1;
1159 }
1160 #endif
1161
1162 static int fdomain_select( int target )
1163 {
1164    int           status;
1165    unsigned long timeout;
1166    static int    flag = 0;
1167
1168
1169    outb( 0x82, SCSI_Cntl_port ); /* Bus Enable + Select */
1170    outb( adapter_mask | (1 << target), SCSI_Data_NoACK_port );
1171
1172    /* Stop arbitration and enable parity */
1173    outb( PARITY_MASK, TMC_Cntl_port ); 
1174
1175    timeout = 350;                       /* 350 msec */
1176
1177    do {
1178       status = inb( SCSI_Status_port ); /* Read adapter status */
1179       if (status & 1) {                 /* Busy asserted */
1180          /* Enable SCSI Bus (on error, should make bus idle with 0) */
1181          outb( 0x80, SCSI_Cntl_port );
1182          return 0;
1183       }
1184       mdelay(1);                        /* wait one msec */
1185    } while (--timeout);
1186    /* Make bus idle */
1187    fdomain_make_bus_idle();
1188 #if EVERY_ACCESS
1189    if (!target) printk( "Selection failed\n" );
1190 #endif
1191 #if ERRORS_ONLY
1192    if (!target) {
1193       if (!flag) /* Skip first failure for all chips. */
1194             ++flag;
1195       else
1196             printk( "scsi: <fdomain> Selection failed\n" );
1197    }
1198 #endif
1199    return 1;
1200 }
1201
1202 void my_done( int error )
1203 {
1204    if (in_command) {
1205       in_command = 0;
1206       outb( 0x00, Interrupt_Cntl_port );
1207       fdomain_make_bus_idle();
1208       current_SC->result = error;
1209       if (current_SC->scsi_done)
1210             current_SC->scsi_done( current_SC );
1211       else panic( "scsi: <fdomain> current_SC->scsi_done() == NULL" );
1212    } else {
1213       panic( "scsi: <fdomain> my_done() called outside of command\n" );
1214    }
1215 #if DEBUG_RACE
1216    in_interrupt_flag = 0;
1217 #endif
1218 }
1219
1220 void do_fdomain_16x0_intr( int irq, void *dev_id, struct pt_regs * regs )
1221 {
1222    unsigned long flags;
1223    int      status;
1224    int      done = 0;
1225    unsigned data_count;
1226
1227                                 /* The fdomain_16x0_intr is only called via
1228                                    the interrupt handler.  The goal of the
1229                                    sti() here is to allow other
1230                                    interruptions while this routine is
1231                                    running. */
1232
1233    /* Check for other IRQ sources */
1234    if((inb(TMC_Status_port)&0x01)==0)   
1235         return;
1236
1237    /* It is our IRQ */          
1238    outb( 0x00, Interrupt_Cntl_port );
1239
1240    /* We usually have one spurious interrupt after each command.  Ignore it. */
1241    if (!in_command || !current_SC) {    /* Spurious interrupt */
1242 #if EVERY_ACCESS
1243       printk( "Spurious interrupt, in_command = %d, current_SC = %x\n",
1244               in_command, current_SC );
1245 #endif
1246       return;
1247    }
1248
1249    /* Abort calls my_done, so we do nothing here. */
1250    if (current_SC->SCp.phase & aborted) {
1251 #if DEBUG_ABORT
1252       printk( "scsi: <fdomain> Interrupt after abort, ignoring\n" );
1253 #endif
1254       /*
1255       return; */
1256    }
1257
1258 #if DEBUG_RACE
1259    ++in_interrupt_flag;
1260 #endif
1261
1262    if (current_SC->SCp.phase & in_arbitration) {
1263       status = inb( TMC_Status_port );        /* Read adapter status */
1264       if (!(status & 0x02)) {
1265 #if EVERY_ACCESS
1266          printk( " AFAIL " );
1267 #endif
1268          spin_lock_irqsave(&io_request_lock, flags);
1269          my_done( DID_BUS_BUSY << 16 );
1270          spin_unlock_irqrestore(&io_request_lock, flags);
1271          return;
1272       }
1273       current_SC->SCp.phase = in_selection;
1274       
1275       outb( 0x40 | FIFO_COUNT, Interrupt_Cntl_port );
1276
1277       outb( 0x82, SCSI_Cntl_port ); /* Bus Enable + Select */
1278       outb( adapter_mask | (1 << current_SC->target), SCSI_Data_NoACK_port );
1279       
1280       /* Stop arbitration and enable parity */
1281       outb( 0x10 | PARITY_MASK, TMC_Cntl_port );
1282 #if DEBUG_RACE
1283       in_interrupt_flag = 0;
1284 #endif
1285       return;
1286    } else if (current_SC->SCp.phase & in_selection) {
1287       status = inb( SCSI_Status_port );
1288       if (!(status & 0x01)) {
1289          /* Try again, for slow devices */
1290          if (fdomain_select( current_SC->target )) {
1291 #if EVERY_ACCESS
1292             printk( " SFAIL " );
1293 #endif
1294             spin_lock_irqsave(&io_request_lock, flags);
1295             my_done( DID_NO_CONNECT << 16 );
1296             spin_unlock_irqrestore(&io_request_lock, flags);
1297             return;
1298          } else {
1299 #if EVERY_ACCESS
1300             printk( " AltSel " );
1301 #endif
1302             /* Stop arbitration and enable parity */
1303             outb( 0x10 | PARITY_MASK, TMC_Cntl_port );
1304          }
1305       }
1306       current_SC->SCp.phase = in_other;
1307       outb( 0x90 | FIFO_COUNT, Interrupt_Cntl_port );
1308       outb( 0x80, SCSI_Cntl_port );
1309 #if DEBUG_RACE
1310       in_interrupt_flag = 0;
1311 #endif
1312       return;
1313    }
1314    
1315    /* current_SC->SCp.phase == in_other: this is the body of the routine */
1316    
1317    status = inb( SCSI_Status_port );
1318    
1319    if (status & 0x10) { /* REQ */
1320       
1321       switch (status & 0x0e) {
1322        
1323       case 0x08:                /* COMMAND OUT */
1324          outb( current_SC->cmnd[current_SC->SCp.sent_command++],
1325                Write_SCSI_Data_port );
1326 #if EVERY_ACCESS
1327          printk( "CMD = %x,",
1328                  current_SC->cmnd[ current_SC->SCp.sent_command - 1] );
1329 #endif
1330          break;
1331       case 0x00:                /* DATA OUT -- tmc18c50/tmc18c30 only */
1332          if (chip != tmc1800 && !current_SC->SCp.have_data_in) {
1333             current_SC->SCp.have_data_in = -1;
1334             outb( 0xd0 | PARITY_MASK, TMC_Cntl_port );
1335          }
1336          break;
1337       case 0x04:                /* DATA IN -- tmc18c50/tmc18c30 only */
1338          if (chip != tmc1800 && !current_SC->SCp.have_data_in) {
1339             current_SC->SCp.have_data_in = 1;
1340             outb( 0x90 | PARITY_MASK, TMC_Cntl_port );
1341          }
1342          break;
1343       case 0x0c:                /* STATUS IN */
1344          current_SC->SCp.Status = inb( Read_SCSI_Data_port );
1345 #if EVERY_ACCESS
1346          printk( "Status = %x, ", current_SC->SCp.Status );
1347 #endif
1348 #if ERRORS_ONLY
1349          if (current_SC->SCp.Status
1350              && current_SC->SCp.Status != 2
1351              && current_SC->SCp.Status != 8) {
1352             printk( "scsi: <fdomain> target = %d, command = %x, status = %x\n",
1353                     current_SC->target,
1354                     current_SC->cmnd[0],
1355                     current_SC->SCp.Status );
1356          }
1357 #endif
1358                break;
1359       case 0x0a:                /* MESSAGE OUT */
1360          outb( MESSAGE_REJECT, Write_SCSI_Data_port ); /* Reject */
1361          break;
1362       case 0x0e:                /* MESSAGE IN */
1363          current_SC->SCp.Message = inb( Read_SCSI_Data_port );
1364 #if EVERY_ACCESS
1365          printk( "Message = %x, ", current_SC->SCp.Message );
1366 #endif
1367          if (!current_SC->SCp.Message) ++done;
1368 #if DEBUG_MESSAGES || EVERY_ACCESS
1369          if (current_SC->SCp.Message) {
1370             printk( "scsi: <fdomain> message = %x\n",
1371                     current_SC->SCp.Message );
1372          }
1373 #endif
1374          break;
1375       }
1376    }
1377
1378    if (chip == tmc1800
1379        && !current_SC->SCp.have_data_in
1380        && (current_SC->SCp.sent_command
1381            >= current_SC->cmd_len)) {
1382                                 /* We have to get the FIFO direction
1383                                    correct, so I've made a table based
1384                                    on the SCSI Standard of which commands
1385                                    appear to require a DATA OUT phase.
1386                                  */
1387       /*
1388         p. 94: Command for all device types
1389         CHANGE DEFINITION            40 DATA OUT
1390         COMPARE                      39 DATA OUT
1391         COPY                         18 DATA OUT
1392         COPY AND VERIFY              3a DATA OUT
1393         INQUIRY                      12 
1394         LOG SELECT                   4c DATA OUT
1395         LOG SENSE                    4d
1396         MODE SELECT (6)              15 DATA OUT
1397         MODE SELECT (10)             55 DATA OUT
1398         MODE SENSE (6)               1a
1399         MODE SENSE (10)              5a
1400         READ BUFFER                  3c
1401         RECEIVE DIAGNOSTIC RESULTS   1c
1402         REQUEST SENSE                03
1403         SEND DIAGNOSTIC              1d DATA OUT
1404         TEST UNIT READY              00
1405         WRITE BUFFER                 3b DATA OUT
1406
1407         p.178: Commands for direct-access devices (not listed on p. 94)
1408         FORMAT UNIT                  04 DATA OUT
1409         LOCK-UNLOCK CACHE            36
1410         PRE-FETCH                    34
1411         PREVENT-ALLOW MEDIUM REMOVAL 1e
1412         READ (6)/RECEIVE             08
1413         READ (10)                    3c
1414         READ CAPACITY                25
1415         READ DEFECT DATA (10)        37
1416         READ LONG                    3e
1417         REASSIGN BLOCKS              07 DATA OUT
1418         RELEASE                      17
1419         RESERVE                      16 DATA OUT
1420         REZERO UNIT/REWIND           01
1421         SEARCH DATA EQUAL (10)       31 DATA OUT
1422         SEARCH DATA HIGH (10)        30 DATA OUT
1423         SEARCH DATA LOW (10)         32 DATA OUT
1424         SEEK (6)                     0b
1425         SEEK (10)                    2b
1426         SET LIMITS (10)              33
1427         START STOP UNIT              1b
1428         SYNCHRONIZE CACHE            35
1429         VERIFY (10)                  2f
1430         WRITE (6)/PRINT/SEND         0a DATA OUT
1431         WRITE (10)/SEND              2a DATA OUT
1432         WRITE AND VERIFY (10)        2e DATA OUT
1433         WRITE LONG                   3f DATA OUT
1434         WRITE SAME                   41 DATA OUT ?
1435
1436         p. 261: Commands for sequential-access devices (not previously listed)
1437         ERASE                        19
1438         LOAD UNLOAD                  1b
1439         LOCATE                       2b
1440         READ BLOCK LIMITS            05
1441         READ POSITION                34
1442         READ REVERSE                 0f
1443         RECOVER BUFFERED DATA        14
1444         SPACE                        11
1445         WRITE FILEMARKS              10 ?
1446
1447         p. 298: Commands for printer devices (not previously listed)
1448         ****** NOT SUPPORTED BY THIS DRIVER, since 0b is SEEK (6) *****
1449         SLEW AND PRINT               0b DATA OUT  -- same as seek
1450         STOP PRINT                   1b
1451         SYNCHRONIZE BUFFER           10
1452
1453         p. 315: Commands for processor devices (not previously listed)
1454         
1455         p. 321: Commands for write-once devices (not previously listed)
1456         MEDIUM SCAN                  38
1457         READ (12)                    a8
1458         SEARCH DATA EQUAL (12)       b1 DATA OUT
1459         SEARCH DATA HIGH (12)        b0 DATA OUT
1460         SEARCH DATA LOW (12)         b2 DATA OUT
1461         SET LIMITS (12)              b3
1462         VERIFY (12)                  af
1463         WRITE (12)                   aa DATA OUT
1464         WRITE AND VERIFY (12)        ae DATA OUT
1465
1466         p. 332: Commands for CD-ROM devices (not previously listed)
1467         PAUSE/RESUME                 4b
1468         PLAY AUDIO (10)              45
1469         PLAY AUDIO (12)              a5
1470         PLAY AUDIO MSF               47
1471         PLAY TRACK RELATIVE (10)     49
1472         PLAY TRACK RELATIVE (12)     a9
1473         READ HEADER                  44
1474         READ SUB-CHANNEL             42
1475         READ TOC                     43
1476
1477         p. 370: Commands for scanner devices (not previously listed)
1478         GET DATA BUFFER STATUS       34
1479         GET WINDOW                   25
1480         OBJECT POSITION              31
1481         SCAN                         1b
1482         SET WINDOW                   24 DATA OUT
1483
1484         p. 391: Commands for optical memory devices (not listed)
1485         ERASE (10)                   2c
1486         ERASE (12)                   ac
1487         MEDIUM SCAN                  38 DATA OUT
1488         READ DEFECT DATA (12)        b7
1489         READ GENERATION              29
1490         READ UPDATED BLOCK           2d
1491         UPDATE BLOCK                 3d DATA OUT
1492
1493         p. 419: Commands for medium changer devices (not listed)
1494         EXCHANGE MEDIUM              46
1495         INITIALIZE ELEMENT STATUS    07
1496         MOVE MEDIUM                  a5
1497         POSITION TO ELEMENT          2b
1498         READ ELEMENT STATUS          b8
1499         REQUEST VOL. ELEMENT ADDRESS b5
1500         SEND VOLUME TAG              b6 DATA OUT
1501
1502         p. 454: Commands for communications devices (not listed previously)
1503         GET MESSAGE (6)              08
1504         GET MESSAGE (10)             28
1505         GET MESSAGE (12)             a8
1506       */
1507         
1508       switch (current_SC->cmnd[0]) {
1509       case CHANGE_DEFINITION: case COMPARE:         case COPY:
1510       case COPY_VERIFY:       case LOG_SELECT:      case MODE_SELECT:
1511       case MODE_SELECT_10:    case SEND_DIAGNOSTIC: case WRITE_BUFFER:
1512
1513       case FORMAT_UNIT:       case REASSIGN_BLOCKS: case RESERVE:
1514       case SEARCH_EQUAL:      case SEARCH_HIGH:     case SEARCH_LOW:
1515       case WRITE_6:           case WRITE_10:        case WRITE_VERIFY:
1516       case 0x3f:              case 0x41:
1517
1518       case 0xb1:              case 0xb0:            case 0xb2:
1519       case 0xaa:              case 0xae:
1520
1521       case 0x24:
1522
1523       case 0x38:              case 0x3d:
1524
1525       case 0xb6:
1526          
1527       case 0xea:                /* alternate number for WRITE LONG */
1528          
1529          current_SC->SCp.have_data_in = -1;
1530          outb( 0xd0 | PARITY_MASK, TMC_Cntl_port );
1531          break;
1532
1533       case 0x00:
1534       default:
1535          
1536          current_SC->SCp.have_data_in = 1;
1537          outb( 0x90 | PARITY_MASK, TMC_Cntl_port );
1538          break;
1539       }
1540    }
1541
1542    if (current_SC->SCp.have_data_in == -1) { /* DATA OUT */
1543       while ( (data_count = FIFO_Size - inw( FIFO_Data_Count_port )) > 512 ) {
1544 #if EVERY_ACCESS
1545          printk( "DC=%d, ", data_count ) ;
1546 #endif
1547          if (data_count > current_SC->SCp.this_residual)
1548                data_count = current_SC->SCp.this_residual;
1549          if (data_count > 0) {
1550 #if EVERY_ACCESS
1551             printk( "%d OUT, ", data_count );
1552 #endif
1553             if (data_count == 1) {
1554                outb( *current_SC->SCp.ptr++, Write_FIFO_port );
1555                --current_SC->SCp.this_residual;
1556             } else {
1557                data_count >>= 1;
1558                outsw( Write_FIFO_port, current_SC->SCp.ptr, data_count );
1559                current_SC->SCp.ptr += 2 * data_count;
1560                current_SC->SCp.this_residual -= 2 * data_count;
1561             }
1562          }
1563          if (!current_SC->SCp.this_residual) {
1564             if (current_SC->SCp.buffers_residual) {
1565                --current_SC->SCp.buffers_residual;
1566                ++current_SC->SCp.buffer;
1567                current_SC->SCp.ptr = current_SC->SCp.buffer->address;
1568                current_SC->SCp.this_residual = current_SC->SCp.buffer->length;
1569             } else
1570                   break;
1571          }
1572       }
1573    }
1574    
1575    if (current_SC->SCp.have_data_in == 1) { /* DATA IN */
1576       while ((data_count = inw( FIFO_Data_Count_port )) > 0) {
1577 #if EVERY_ACCESS
1578          printk( "DC=%d, ", data_count );
1579 #endif
1580          if (data_count > current_SC->SCp.this_residual)
1581                data_count = current_SC->SCp.this_residual;
1582          if (data_count) {
1583 #if EVERY_ACCESS
1584             printk( "%d IN, ", data_count );
1585 #endif
1586             if (data_count == 1) {
1587                *current_SC->SCp.ptr++ = inb( Read_FIFO_port );
1588                --current_SC->SCp.this_residual;
1589             } else {
1590                data_count >>= 1; /* Number of words */
1591                insw( Read_FIFO_port, current_SC->SCp.ptr, data_count );
1592                current_SC->SCp.ptr += 2 * data_count;
1593                current_SC->SCp.this_residual -= 2 * data_count;
1594             }
1595          }
1596          if (!current_SC->SCp.this_residual
1597              && current_SC->SCp.buffers_residual) {
1598             --current_SC->SCp.buffers_residual;
1599             ++current_SC->SCp.buffer;
1600             current_SC->SCp.ptr = current_SC->SCp.buffer->address;
1601             current_SC->SCp.this_residual = current_SC->SCp.buffer->length;
1602          }
1603       }
1604    }
1605    
1606    if (done) {
1607 #if EVERY_ACCESS
1608       printk( " ** IN DONE %d ** ", current_SC->SCp.have_data_in );
1609 #endif
1610
1611 #if ERRORS_ONLY
1612       if (current_SC->cmnd[0] == REQUEST_SENSE && !current_SC->SCp.Status) {
1613          if ((unsigned char)(*((char *)current_SC->request_buffer+2)) & 0x0f) {
1614             unsigned char key;
1615             unsigned char code;
1616             unsigned char qualifier;
1617
1618             key = (unsigned char)(*((char *)current_SC->request_buffer + 2))
1619                   & 0x0f;
1620             code = (unsigned char)(*((char *)current_SC->request_buffer + 12));
1621             qualifier = (unsigned char)(*((char *)current_SC->request_buffer
1622                                           + 13));
1623
1624             if (key != UNIT_ATTENTION
1625                 && !(key == NOT_READY
1626                      && code == 0x04
1627                      && (!qualifier || qualifier == 0x02 || qualifier == 0x01))
1628                 && !(key == ILLEGAL_REQUEST && (code == 0x25
1629                                                 || code == 0x24
1630                                                 || !code)))
1631                   
1632                   printk( "scsi: <fdomain> REQUEST SENSE"
1633                           " Key = %x, Code = %x, Qualifier = %x\n",
1634                           key, code, qualifier );
1635          }
1636       }
1637 #endif
1638 #if EVERY_ACCESS
1639       printk( "BEFORE MY_DONE. . ." );
1640 #endif
1641       spin_lock_irqsave(&io_request_lock, flags);
1642       my_done( (current_SC->SCp.Status & 0xff)
1643                | ((current_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16) );
1644       spin_unlock_irqrestore(&io_request_lock, flags);
1645 #if EVERY_ACCESS
1646       printk( "RETURNING.\n" );
1647 #endif
1648       
1649    } else {
1650       if (current_SC->SCp.phase & disconnect) {
1651          outb( 0xd0 | FIFO_COUNT, Interrupt_Cntl_port );
1652          outb( 0x00, SCSI_Cntl_port );
1653       } else {
1654          outb( 0x90 | FIFO_COUNT, Interrupt_Cntl_port );
1655       }
1656    }
1657 #if DEBUG_RACE
1658    in_interrupt_flag = 0;
1659 #endif
1660    return;
1661 }
1662
1663 int fdomain_16x0_queue( Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
1664 {
1665    if (in_command) {
1666       panic( "scsi: <fdomain> fdomain_16x0_queue() NOT REENTRANT!\n" );
1667    }
1668 #if EVERY_ACCESS
1669    printk( "queue: target = %d cmnd = 0x%02x pieces = %d size = %u\n",
1670            SCpnt->target,
1671            *(unsigned char *)SCpnt->cmnd,
1672            SCpnt->use_sg,
1673            SCpnt->request_bufflen );
1674 #endif
1675
1676    fdomain_make_bus_idle();
1677
1678    current_SC            = SCpnt; /* Save this for the done function */
1679    current_SC->scsi_done = done;
1680
1681    /* Initialize static data */
1682
1683    if (current_SC->use_sg) {
1684       current_SC->SCp.buffer =
1685             (struct scatterlist *)current_SC->request_buffer;
1686       current_SC->SCp.ptr              = current_SC->SCp.buffer->address;
1687       current_SC->SCp.this_residual    = current_SC->SCp.buffer->length;
1688       current_SC->SCp.buffers_residual = current_SC->use_sg - 1;
1689    } else {
1690       current_SC->SCp.ptr              = (char *)current_SC->request_buffer;
1691       current_SC->SCp.this_residual    = current_SC->request_bufflen;
1692       current_SC->SCp.buffer           = NULL;
1693       current_SC->SCp.buffers_residual = 0;
1694    }
1695          
1696    
1697    current_SC->SCp.Status              = 0;
1698    current_SC->SCp.Message             = 0;
1699    current_SC->SCp.have_data_in        = 0;
1700    current_SC->SCp.sent_command        = 0;
1701    current_SC->SCp.phase               = in_arbitration;
1702
1703    /* Start arbitration */
1704    outb( 0x00, Interrupt_Cntl_port );
1705    outb( 0x00, SCSI_Cntl_port );              /* Disable data drivers */
1706    outb( adapter_mask, SCSI_Data_NoACK_port ); /* Set our id bit */
1707    ++in_command;
1708    outb( 0x20, Interrupt_Cntl_port );
1709    outb( 0x14 | PARITY_MASK, TMC_Cntl_port ); /* Start arbitration */
1710
1711    return 0;
1712 }
1713
1714 /* The following code, which simulates the old-style command function, was
1715    taken from Tommy Thorn's aha1542.c file.  This code is Copyright (C)
1716    1992 Tommy Thorn. */
1717
1718 static volatile int internal_done_flag    = 0;
1719 static volatile int internal_done_errcode = 0;
1720
1721 static void internal_done( Scsi_Cmnd *SCpnt )
1722 {
1723     internal_done_errcode = SCpnt->result;
1724     ++internal_done_flag;
1725 }
1726
1727 int fdomain_16x0_command( Scsi_Cmnd *SCpnt )
1728 {
1729     fdomain_16x0_queue( SCpnt, internal_done );
1730
1731     while (!internal_done_flag)
1732           ;
1733     internal_done_flag = 0;
1734     return internal_done_errcode;
1735 }
1736
1737 /* End of code derived from Tommy Thorn's work. */
1738
1739 void print_info( Scsi_Cmnd *SCpnt )
1740 {
1741    unsigned int imr;
1742    unsigned int irr;
1743    unsigned int isr;
1744
1745    if (!SCpnt || !SCpnt->host) {
1746       printk( "scsi: <fdomain> Cannot provide detailed information\n" );
1747       return;
1748    }
1749    
1750    printk( "%s\n", fdomain_16x0_info( SCpnt->host ) );
1751    print_banner( SCpnt->host );
1752    switch (SCpnt->SCp.phase) {
1753    case in_arbitration: printk( "arbitration " ); break;
1754    case in_selection:   printk( "selection " );   break;
1755    case in_other:       printk( "other " );       break;
1756    default:             printk( "unknown " );     break;
1757    }
1758
1759    printk( "(%d), target = %d cmnd = 0x%02x pieces = %d size = %u\n",
1760            SCpnt->SCp.phase,
1761            SCpnt->target,
1762            *(unsigned char *)SCpnt->cmnd,
1763            SCpnt->use_sg,
1764            SCpnt->request_bufflen );
1765    printk( "sent_command = %d, have_data_in = %d, timeout = %d\n",
1766            SCpnt->SCp.sent_command,
1767            SCpnt->SCp.have_data_in,
1768            SCpnt->timeout );
1769 #if DEBUG_RACE
1770    printk( "in_interrupt_flag = %d\n", in_interrupt_flag );
1771 #endif
1772
1773    imr = (inb( 0x0a1 ) << 8) + inb( 0x21 );
1774    outb( 0x0a, 0xa0 );
1775    irr = inb( 0xa0 ) << 8;
1776    outb( 0x0a, 0x20 );
1777    irr += inb( 0x20 );
1778    outb( 0x0b, 0xa0 );
1779    isr = inb( 0xa0 ) << 8;
1780    outb( 0x0b, 0x20 );
1781    isr += inb( 0x20 );
1782
1783                                 /* Print out interesting information */
1784    printk( "IMR = 0x%04x", imr );
1785    if (imr & (1 << interrupt_level))
1786          printk( " (masked)" );
1787    printk( ", IRR = 0x%04x, ISR = 0x%04x\n", irr, isr );
1788
1789    printk( "SCSI Status      = 0x%02x\n", inb( SCSI_Status_port ) );
1790    printk( "TMC Status       = 0x%02x", inb( TMC_Status_port ) );
1791    if (inb( TMC_Status_port & 1))
1792          printk( " (interrupt)" );
1793    printk( "\n" );
1794    printk( "Interrupt Status = 0x%02x", inb( Interrupt_Status_port ) );
1795    if (inb( Interrupt_Status_port ) & 0x08)
1796          printk( " (enabled)" );
1797    printk( "\n" );
1798    if (chip == tmc18c50 || chip == tmc18c30) {
1799       printk( "FIFO Status      = 0x%02x\n", inb( port_base + FIFO_Status ) );
1800       printk( "Int. Condition   = 0x%02x\n",
1801               inb( port_base + Interrupt_Cond ) );
1802    }
1803    printk( "Configuration 1  = 0x%02x\n", inb( port_base + Configuration1 ) );
1804    if (chip == tmc18c50 || chip == tmc18c30)
1805          printk( "Configuration 2  = 0x%02x\n",
1806                  inb( port_base + Configuration2 ) );
1807 }
1808
1809 int fdomain_16x0_abort( Scsi_Cmnd *SCpnt)
1810 {
1811    unsigned long flags;
1812 #if EVERY_ACCESS || ERRORS_ONLY || DEBUG_ABORT
1813    printk( "scsi: <fdomain> abort " );
1814 #endif
1815
1816    save_flags( flags );
1817    cli();
1818    if (!in_command) {
1819 #if EVERY_ACCESS || ERRORS_ONLY
1820       printk( " (not in command)\n" );
1821 #endif
1822       restore_flags( flags );
1823       return SCSI_ABORT_NOT_RUNNING;
1824    } else printk( "\n" );
1825
1826 #if DEBUG_ABORT
1827    print_info( SCpnt );
1828 #endif
1829
1830    fdomain_make_bus_idle();
1831
1832    current_SC->SCp.phase |= aborted;
1833
1834    current_SC->result = DID_ABORT << 16;
1835
1836    restore_flags( flags );
1837    
1838    /* Aborts are not done well. . . */
1839    my_done( DID_ABORT << 16 );
1840
1841    return SCSI_ABORT_SUCCESS;
1842 }
1843
1844 int fdomain_16x0_reset( Scsi_Cmnd *SCpnt, unsigned int ignored )
1845 {
1846 #if DEBUG_RESET
1847    static int called_once = 0;
1848 #endif
1849
1850 #if ERRORS_ONLY
1851    if (SCpnt) printk( "scsi: <fdomain> SCSI Bus Reset\n" );
1852 #endif
1853
1854 #if DEBUG_RESET
1855    if (called_once) print_info( current_SC );
1856    called_once = 1;
1857 #endif
1858    
1859    outb( 1, SCSI_Cntl_port );
1860    do_pause( 2 );
1861    outb( 0, SCSI_Cntl_port );
1862    do_pause( 115 );
1863    outb( 0, SCSI_Mode_Cntl_port );
1864    outb( PARITY_MASK, TMC_Cntl_port );
1865
1866    /* Unless this is the very first call (i.e., SCPnt == NULL), everything
1867       is probably hosed at this point.  We will, however, try to keep
1868       things going by informing the high-level code that we need help. */
1869
1870    return SCSI_RESET_WAKEUP;
1871 }
1872
1873 #include "sd.h"
1874 #include <scsi/scsi_ioctl.h>
1875
1876 int fdomain_16x0_biosparam( Scsi_Disk *disk, kdev_t dev, int *info_array )
1877 {
1878    int              drive;
1879    unsigned char    buf[512 + sizeof (Scsi_Ioctl_Command)];
1880    Scsi_Ioctl_Command *sic = (Scsi_Ioctl_Command *) buf;
1881    int              size      = disk->capacity;
1882    unsigned char    *data     = sic->data;
1883    unsigned char    do_read[] = { READ_6, 0, 0, 0, 1, 0 };
1884    int              retcode;
1885    unsigned long    offset;
1886    struct drive_info {
1887       unsigned short cylinders;
1888       unsigned char  heads;
1889       unsigned char  sectors;
1890    } i;
1891    
1892    /* NOTES:
1893       The RAM area starts at 0x1f00 from the bios_base address.
1894
1895       For BIOS Version 2.0:
1896       
1897       The drive parameter table seems to start at 0x1f30.
1898       The first byte's purpose is not known.
1899       Next is the cylinder, head, and sector information.
1900       The last 4 bytes appear to be the drive's size in sectors.
1901       The other bytes in the drive parameter table are unknown.
1902       If anyone figures them out, please send me mail, and I will
1903       update these notes.
1904
1905       Tape drives do not get placed in this table.
1906
1907       There is another table at 0x1fea:
1908       If the byte is 0x01, then the SCSI ID is not in use.
1909       If the byte is 0x18 or 0x48, then the SCSI ID is in use,
1910       although tapes don't seem to be in this table.  I haven't
1911       seen any other numbers (in a limited sample).
1912
1913       0x1f2d is a drive count (i.e., not including tapes)
1914
1915       The table at 0x1fcc are I/O ports addresses for the various
1916       operations.  I calculate these by hand in this driver code.
1917
1918       
1919       
1920       For the ISA-200S version of BIOS Version 2.0:
1921
1922       The drive parameter table starts at 0x1f33.
1923
1924       WARNING: Assume that the table entry is 25 bytes long.  Someone needs
1925       to check this for the Quantum ISA-200S card.
1926
1927       
1928       
1929       For BIOS Version 3.2:
1930
1931       The drive parameter table starts at 0x1f70.  Each entry is
1932       0x0a bytes long.  Heads are one less than we need to report.
1933     */
1934
1935    if (MAJOR(dev) != SCSI_DISK0_MAJOR) {
1936       printk("scsi: <fdomain> fdomain_16x0_biosparam: too many disks");
1937       return 0;
1938    }
1939    drive = MINOR(dev) >> 4;
1940
1941    if (bios_major == 2) {
1942       switch (Quantum) {
1943       case 2:                   /* ISA_200S */
1944                                 /* The value of 25 has never been verified.
1945                                    It should probably be 15. */
1946          offset = bios_base + 0x1f33 + drive * 25;
1947          break;
1948       case 3:                   /* ISA_250MG */
1949          offset = bios_base + 0x1f36 + drive * 15;
1950          break;
1951       case 4:                   /* ISA_200S (another one) */
1952          offset = bios_base + 0x1f34 + drive * 15;
1953          break;
1954       default:
1955          offset = bios_base + 0x1f31 + drive * 25;
1956          break;
1957       }
1958       isa_memcpy_fromio( &i, offset, sizeof( struct drive_info ) );
1959       info_array[0] = i.heads;
1960       info_array[1] = i.sectors;
1961       info_array[2] = i.cylinders;
1962    } else if (bios_major == 3
1963               && bios_minor >= 0
1964               && bios_minor < 4) { /* 3.0 and 3.2 BIOS */
1965       memcpy_fromio( &i, bios_base + 0x1f71 + drive * 10,
1966                      sizeof( struct drive_info ) );
1967       info_array[0] = i.heads + 1;
1968       info_array[1] = i.sectors;
1969       info_array[2] = i.cylinders;
1970    } else {                     /* 3.4 BIOS (and up?) */
1971       /* This algorithm was provided by Future Domain (much thanks!). */
1972
1973       sic->inlen  = 0;          /* zero bytes out */
1974       sic->outlen = 512;                /* one sector in */
1975       memcpy( data, do_read, sizeof( do_read ) );
1976       retcode = kernel_scsi_ioctl( disk->device,
1977                                    SCSI_IOCTL_SEND_COMMAND,
1978                                    sic );
1979       if (!retcode                                  /* SCSI command ok */
1980           && data[511] == 0xaa && data[510] == 0x55 /* Partition table valid */
1981           && data[0x1c2]) {                         /* Partition type */
1982
1983          /* The partition table layout is as follows:
1984
1985             Start: 0x1b3h
1986             Offset: 0 = partition status
1987                     1 = starting head
1988                     2 = starting sector and cylinder (word, encoded)
1989                     4 = partition type
1990                     5 = ending head
1991                     6 = ending sector and cylinder (word, encoded)
1992                     8 = starting absolute sector (double word)
1993                     c = number of sectors (double word)
1994             Signature: 0x1fe = 0x55aa
1995
1996             So, this algorithm assumes:
1997             1) the first partition table is in use,
1998             2) the data in the first entry is correct, and
1999             3) partitions never divide cylinders
2000
2001             Note that (1) may be FALSE for NetBSD (and other BSD flavors),
2002             as well as for Linux.  Note also, that Linux doesn't pay any
2003             attention to the fields that are used by this algorithm -- it
2004             only uses the absolute sector data.  Recent versions of Linux's
2005             fdisk(1) will fill this data in correctly, and forthcoming
2006             versions will check for consistency.
2007
2008             Checking for a non-zero partition type is not part of the
2009             Future Domain algorithm, but it seemed to be a reasonable thing
2010             to do, especially in the Linux and BSD worlds. */
2011
2012          info_array[0] = data[0x1c3] + 1;           /* heads */
2013          info_array[1] = data[0x1c4] & 0x3f;        /* sectors */
2014       } else {
2015
2016          /* Note that this new method guarantees that there will always be
2017             less than 1024 cylinders on a platter.  This is good for drives
2018             up to approximately 7.85GB (where 1GB = 1024 * 1024 kB). */
2019
2020          if ((unsigned int)size >= 0x7e0000U) {
2021             info_array[0] = 0xff; /* heads   = 255 */
2022             info_array[1] = 0x3f; /* sectors =  63 */
2023          } else if ((unsigned int)size >= 0x200000U) {
2024             info_array[0] = 0x80; /* heads   = 128 */
2025             info_array[1] = 0x3f; /* sectors =  63 */
2026          } else {
2027             info_array[0] = 0x40; /* heads   =  64 */
2028             info_array[1] = 0x20; /* sectors =  32 */
2029          }
2030       }
2031                                 /* For both methods, compute the cylinders */
2032       info_array[2] = (unsigned int)size / (info_array[0] * info_array[1] );
2033    }
2034    
2035    return 0;
2036 }
2037
2038 int fdomain_16x0_release(struct Scsi_Host *shpnt)
2039 {
2040         if (shpnt->irq)
2041                 free_irq(shpnt->irq, shpnt);
2042         if (shpnt->io_port && shpnt->n_io_port)
2043                 release_region(shpnt->io_port, shpnt->n_io_port);
2044         return 0;
2045 }
2046
2047 MODULE_LICENSE("GPL");
2048
2049 /* Eventually this will go into an include file, but this will be later */
2050 static Scsi_Host_Template driver_template = FDOMAIN_16X0;
2051
2052 #include "scsi_module.c"