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
340enum chip_type {
341   unknown          = 0x00,
342   tmc1800          = 0x01,
343   tmc18c50         = 0x02,
344   tmc18c30         = 0x03,
345};
346
347enum {
348   in_arbitration   = 0x02,
349   in_selection     = 0x04,
350   in_other         = 0x08,
351   disconnect       = 0x10,
352   aborted          = 0x20,
353   sent_ident       = 0x40,
354};
355
356enum 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
373enum 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
385static int               port_base         = 0;
386static unsigned long     bios_base         = 0;
387static int               bios_major        = 0;
388static int               bios_minor        = 0;
389static int               PCI_bus           = 0;
390static int               Quantum           = 0;	/* Quantum board variant */
391static int               interrupt_level   = 0;
392static volatile int      in_command        = 0;
393static Scsi_Cmnd         *current_SC       = NULL;
394static enum chip_type    chip              = unknown;
395static int               adapter_mask      = 0;
396static int               this_id           = 0;
397static int               setup_called      = 0;
398
399#if DEBUG_RACE
400static volatile int      in_interrupt_flag = 0;
401#endif
402
403static int               SCSI_Mode_Cntl_port;
404static int               FIFO_Data_Count_port;
405static int               Interrupt_Cntl_port;
406static int               Interrupt_Status_port;
407static int               Read_FIFO_port;
408static int               Read_SCSI_Data_port;
409static int               SCSI_Cntl_port;
410static int               SCSI_Data_NoACK_port;
411static int               SCSI_Status_port;
412static int               TMC_Cntl_port;
413static int               TMC_Status_port;
414static int               Write_FIFO_port;
415static int               Write_SCSI_Data_port;
416
417static int               FIFO_Size = 0x2000; /* 8k FIFO for
418						pre-tmc18c30 chips */
419
420extern 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				*/
428static char * fdomain = NULL;
429MODULE_PARM(fdomain, "s");
430#endif
431
432static 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
443static unsigned short ports[] = { 0x140, 0x150, 0x160, 0x170 };
444#define PORT_COUNT (sizeof( ports ) / sizeof( unsigned short ))
445
446static 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
480struct 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
525static 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
564static 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
589static void do_pause( unsigned amount )	/* Pause for amount*10 milliseconds */
590{
591   mdelay(10*amount);
592}
593
594inline 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
604static 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
644static 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
672static 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
692static 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
807static 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
874int 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
1067const 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 */
1099int 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
1128static int fdomain_select( int target )
1129{
1130   int           status;
1131   unsigned long timeout;
1132   static int    flag = 0;
1133
1134
1135   outb( 0x82, SCSI_Cntl_port ); /* Bus Enable + Select */
1136   outb( adapter_mask | (1 << target), SCSI_Data_NoACK_port );
1137
1138   /* Stop arbitration and enable parity */
1139   outb( PARITY_MASK, TMC_Cntl_port );
1140
1141   timeout = 350;			/* 350 msec */
1142
1143   do {
1144      status = inb( SCSI_Status_port ); /* Read adapter status */
1145      if (status & 1) {			/* Busy asserted */
1146	 /* Enable SCSI Bus (on error, should make bus idle with 0) */
1147	 outb( 0x80, SCSI_Cntl_port );
1148	 return 0;
1149      }
1150      mdelay(1);			/* wait one msec */
1151   } while (--timeout);
1152   /* Make bus idle */
1153   fdomain_make_bus_idle();
1154#if EVERY_ACCESS
1155   if (!target) printk( "Selection failed\n" );
1156#endif
1157#if ERRORS_ONLY
1158   if (!target) {
1159      if (!flag) /* Skip first failure for all chips. */
1160	    ++flag;
1161      else
1162	    printk( "scsi: <fdomain> Selection failed\n" );
1163   }
1164#endif
1165   return 1;
1166}
1167
1168void my_done( int error )
1169{
1170   if (in_command) {
1171      in_command = 0;
1172      outb( 0x00, Interrupt_Cntl_port );
1173      fdomain_make_bus_idle();
1174      current_SC->result = error;
1175      if (current_SC->scsi_done)
1176	    current_SC->scsi_done( current_SC );
1177      else panic( "scsi: <fdomain> current_SC->scsi_done() == NULL" );
1178   } else {
1179      panic( "scsi: <fdomain> my_done() called outside of command\n" );
1180   }
1181#if DEBUG_RACE
1182   in_interrupt_flag = 0;
1183#endif
1184}
1185
1186void do_fdomain_16x0_intr( int irq, void *dev_id, struct pt_regs * regs )
1187{
1188   unsigned long flags;
1189   int      status;
1190   int      done = 0;
1191   unsigned data_count;
1192
1193				/* The fdomain_16x0_intr is only called via
1194				   the interrupt handler.  The goal of the
1195				   sti() here is to allow other
1196				   interruptions while this routine is
1197				   running. */
1198
1199   /* Check for other IRQ sources */
1200   if((inb(TMC_Status_port)&0x01)==0)
1201   	return;
1202
1203   /* It is our IRQ */
1204   outb( 0x00, Interrupt_Cntl_port );
1205
1206   /* We usually have one spurious interrupt after each command.  Ignore it. */
1207   if (!in_command || !current_SC) {	/* Spurious interrupt */
1208#if EVERY_ACCESS
1209      printk( "Spurious interrupt, in_command = %d, current_SC = %x\n",
1210	      in_command, current_SC );
1211#endif
1212      return;
1213   }
1214
1215   /* Abort calls my_done, so we do nothing here. */
1216   if (current_SC->SCp.phase & aborted) {
1217#if DEBUG_ABORT
1218      printk( "scsi: <fdomain> Interrupt after abort, ignoring\n" );
1219#endif
1220      /*
1221      return; */
1222   }
1223
1224#if DEBUG_RACE
1225   ++in_interrupt_flag;
1226#endif
1227
1228   if (current_SC->SCp.phase & in_arbitration) {
1229      status = inb( TMC_Status_port );        /* Read adapter status */
1230      if (!(status & 0x02)) {
1231#if EVERY_ACCESS
1232	 printk( " AFAIL " );
1233#endif
1234         spin_lock_irqsave(&io_request_lock, flags);
1235	 my_done( DID_BUS_BUSY << 16 );
1236         spin_unlock_irqrestore(&io_request_lock, flags);
1237	 return;
1238      }
1239      current_SC->SCp.phase = in_selection;
1240
1241      outb( 0x40 | FIFO_COUNT, Interrupt_Cntl_port );
1242
1243      outb( 0x82, SCSI_Cntl_port ); /* Bus Enable + Select */
1244      outb( adapter_mask | (1 << current_SC->target), SCSI_Data_NoACK_port );
1245
1246      /* Stop arbitration and enable parity */
1247      outb( 0x10 | PARITY_MASK, TMC_Cntl_port );
1248#if DEBUG_RACE
1249      in_interrupt_flag = 0;
1250#endif
1251      return;
1252   } else if (current_SC->SCp.phase & in_selection) {
1253      status = inb( SCSI_Status_port );
1254      if (!(status & 0x01)) {
1255	 /* Try again, for slow devices */
1256	 if (fdomain_select( current_SC->target )) {
1257#if EVERY_ACCESS
1258	    printk( " SFAIL " );
1259#endif
1260            spin_lock_irqsave(&io_request_lock, flags);
1261	    my_done( DID_NO_CONNECT << 16 );
1262            spin_unlock_irqrestore(&io_request_lock, flags);
1263	    return;
1264	 } else {
1265#if EVERY_ACCESS
1266	    printk( " AltSel " );
1267#endif
1268	    /* Stop arbitration and enable parity */
1269	    outb( 0x10 | PARITY_MASK, TMC_Cntl_port );
1270	 }
1271      }
1272      current_SC->SCp.phase = in_other;
1273      outb( 0x90 | FIFO_COUNT, Interrupt_Cntl_port );
1274      outb( 0x80, SCSI_Cntl_port );
1275#if DEBUG_RACE
1276      in_interrupt_flag = 0;
1277#endif
1278      return;
1279   }
1280
1281   /* current_SC->SCp.phase == in_other: this is the body of the routine */
1282
1283   status = inb( SCSI_Status_port );
1284
1285   if (status & 0x10) {	/* REQ */
1286
1287      switch (status & 0x0e) {
1288
1289      case 0x08:		/* COMMAND OUT */
1290	 outb( current_SC->cmnd[current_SC->SCp.sent_command++],
1291	       Write_SCSI_Data_port );
1292#if EVERY_ACCESS
1293	 printk( "CMD = %x,",
1294		 current_SC->cmnd[ current_SC->SCp.sent_command - 1] );
1295#endif
1296	 break;
1297      case 0x00:		/* DATA OUT -- tmc18c50/tmc18c30 only */
1298	 if (chip != tmc1800 && !current_SC->SCp.have_data_in) {
1299	    current_SC->SCp.have_data_in = -1;
1300	    outb( 0xd0 | PARITY_MASK, TMC_Cntl_port );
1301	 }
1302	 break;
1303      case 0x04:		/* DATA IN -- tmc18c50/tmc18c30 only */
1304	 if (chip != tmc1800 && !current_SC->SCp.have_data_in) {
1305	    current_SC->SCp.have_data_in = 1;
1306	    outb( 0x90 | PARITY_MASK, TMC_Cntl_port );
1307	 }
1308	 break;
1309      case 0x0c:		/* STATUS IN */
1310	 current_SC->SCp.Status = inb( Read_SCSI_Data_port );
1311#if EVERY_ACCESS
1312	 printk( "Status = %x, ", current_SC->SCp.Status );
1313#endif
1314#if ERRORS_ONLY
1315	 if (current_SC->SCp.Status
1316	     && current_SC->SCp.Status != 2
1317	     && current_SC->SCp.Status != 8) {
1318	    printk( "scsi: <fdomain> target = %d, command = %x, status = %x\n",
1319		    current_SC->target,
1320		    current_SC->cmnd[0],
1321		    current_SC->SCp.Status );
1322	 }
1323#endif
1324	       break;
1325      case 0x0a:		/* MESSAGE OUT */
1326	 outb( MESSAGE_REJECT, Write_SCSI_Data_port ); /* Reject */
1327	 break;
1328      case 0x0e:		/* MESSAGE IN */
1329	 current_SC->SCp.Message = inb( Read_SCSI_Data_port );
1330#if EVERY_ACCESS
1331	 printk( "Message = %x, ", current_SC->SCp.Message );
1332#endif
1333	 if (!current_SC->SCp.Message) ++done;
1334#if DEBUG_MESSAGES || EVERY_ACCESS
1335	 if (current_SC->SCp.Message) {
1336	    printk( "scsi: <fdomain> message = %x\n",
1337		    current_SC->SCp.Message );
1338	 }
1339#endif
1340	 break;
1341      }
1342   }
1343
1344   if (chip == tmc1800
1345       && !current_SC->SCp.have_data_in
1346       && (current_SC->SCp.sent_command
1347	   >= current_SC->cmd_len)) {
1348				/* We have to get the FIFO direction
1349				   correct, so I've made a table based
1350				   on the SCSI Standard of which commands
1351				   appear to require a DATA OUT phase.
1352				 */
1353      /*
1354	p. 94: Command for all device types
1355	CHANGE DEFINITION            40 DATA OUT
1356	COMPARE                      39 DATA OUT
1357	COPY                         18 DATA OUT
1358	COPY AND VERIFY              3a DATA OUT
1359	INQUIRY                      12
1360	LOG SELECT                   4c DATA OUT
1361	LOG SENSE                    4d
1362	MODE SELECT (6)              15 DATA OUT
1363	MODE SELECT (10)             55 DATA OUT
1364	MODE SENSE (6)               1a
1365	MODE SENSE (10)              5a
1366	READ BUFFER                  3c
1367	RECEIVE DIAGNOSTIC RESULTS   1c
1368	REQUEST SENSE                03
1369	SEND DIAGNOSTIC              1d DATA OUT
1370	TEST UNIT READY              00
1371	WRITE BUFFER                 3b DATA OUT
1372
1373	p.178: Commands for direct-access devices (not listed on p. 94)
1374	FORMAT UNIT                  04 DATA OUT
1375	LOCK-UNLOCK CACHE            36
1376	PRE-FETCH                    34
1377	PREVENT-ALLOW MEDIUM REMOVAL 1e
1378	READ (6)/RECEIVE             08
1379	READ (10)                    3c
1380	READ CAPACITY                25
1381	READ DEFECT DATA (10)        37
1382	READ LONG                    3e
1383	REASSIGN BLOCKS              07 DATA OUT
1384	RELEASE                      17
1385	RESERVE                      16 DATA OUT
1386	REZERO UNIT/REWIND           01
1387	SEARCH DATA EQUAL (10)       31 DATA OUT
1388	SEARCH DATA HIGH (10)        30 DATA OUT
1389	SEARCH DATA LOW (10)         32 DATA OUT
1390	SEEK (6)                     0b
1391	SEEK (10)                    2b
1392	SET LIMITS (10)              33
1393	START STOP UNIT              1b
1394	SYNCHRONIZE CACHE            35
1395	VERIFY (10)                  2f
1396	WRITE (6)/PRINT/SEND         0a DATA OUT
1397	WRITE (10)/SEND              2a DATA OUT
1398	WRITE AND VERIFY (10)        2e DATA OUT
1399	WRITE LONG                   3f DATA OUT
1400	WRITE SAME                   41 DATA OUT ?
1401
1402	p. 261: Commands for sequential-access devices (not previously listed)
1403	ERASE                        19
1404	LOAD UNLOAD                  1b
1405	LOCATE                       2b
1406	READ BLOCK LIMITS            05
1407	READ POSITION                34
1408	READ REVERSE                 0f
1409	RECOVER BUFFERED DATA        14
1410	SPACE                        11
1411	WRITE FILEMARKS              10 ?
1412
1413	p. 298: Commands for printer devices (not previously listed)
1414	****** NOT SUPPORTED BY THIS DRIVER, since 0b is SEEK (6) *****
1415	SLEW AND PRINT               0b DATA OUT  -- same as seek
1416	STOP PRINT                   1b
1417	SYNCHRONIZE BUFFER           10
1418
1419	p. 315: Commands for processor devices (not previously listed)
1420
1421	p. 321: Commands for write-once devices (not previously listed)
1422	MEDIUM SCAN                  38
1423	READ (12)                    a8
1424	SEARCH DATA EQUAL (12)       b1 DATA OUT
1425	SEARCH DATA HIGH (12)        b0 DATA OUT
1426	SEARCH DATA LOW (12)         b2 DATA OUT
1427	SET LIMITS (12)              b3
1428	VERIFY (12)                  af
1429	WRITE (12)                   aa DATA OUT
1430	WRITE AND VERIFY (12)        ae DATA OUT
1431
1432	p. 332: Commands for CD-ROM devices (not previously listed)
1433	PAUSE/RESUME                 4b
1434	PLAY AUDIO (10)              45
1435	PLAY AUDIO (12)              a5
1436	PLAY AUDIO MSF               47
1437	PLAY TRACK RELATIVE (10)     49
1438	PLAY TRACK RELATIVE (12)     a9
1439	READ HEADER                  44
1440	READ SUB-CHANNEL             42
1441	READ TOC                     43
1442
1443	p. 370: Commands for scanner devices (not previously listed)
1444	GET DATA BUFFER STATUS       34
1445	GET WINDOW                   25
1446	OBJECT POSITION              31
1447	SCAN                         1b
1448	SET WINDOW                   24 DATA OUT
1449
1450	p. 391: Commands for optical memory devices (not listed)
1451	ERASE (10)                   2c
1452	ERASE (12)                   ac
1453	MEDIUM SCAN                  38 DATA OUT
1454	READ DEFECT DATA (12)        b7
1455	READ GENERATION              29
1456	READ UPDATED BLOCK           2d
1457	UPDATE BLOCK                 3d DATA OUT
1458
1459	p. 419: Commands for medium changer devices (not listed)
1460	EXCHANGE MEDIUM              46
1461	INITIALIZE ELEMENT STATUS    07
1462	MOVE MEDIUM                  a5
1463	POSITION TO ELEMENT          2b
1464	READ ELEMENT STATUS          b8
1465	REQUEST VOL. ELEMENT ADDRESS b5
1466	SEND VOLUME TAG              b6 DATA OUT
1467
1468	p. 454: Commands for communications devices (not listed previously)
1469	GET MESSAGE (6)              08
1470	GET MESSAGE (10)             28
1471	GET MESSAGE (12)             a8
1472      */
1473
1474      switch (current_SC->cmnd[0]) {
1475      case CHANGE_DEFINITION: case COMPARE:         case COPY:
1476      case COPY_VERIFY:       case LOG_SELECT:      case MODE_SELECT:
1477      case MODE_SELECT_10:    case SEND_DIAGNOSTIC: case WRITE_BUFFER:
1478
1479      case FORMAT_UNIT:       case REASSIGN_BLOCKS: case RESERVE:
1480      case SEARCH_EQUAL:      case SEARCH_HIGH:     case SEARCH_LOW:
1481      case WRITE_6:           case WRITE_10:        case WRITE_VERIFY:
1482      case 0x3f:              case 0x41:
1483
1484      case 0xb1:              case 0xb0:            case 0xb2:
1485      case 0xaa:              case 0xae:
1486
1487      case 0x24:
1488
1489      case 0x38:              case 0x3d:
1490
1491      case 0xb6:
1492
1493      case 0xea:		/* alternate number for WRITE LONG */
1494
1495	 current_SC->SCp.have_data_in = -1;
1496	 outb( 0xd0 | PARITY_MASK, TMC_Cntl_port );
1497	 break;
1498
1499      case 0x00:
1500      default:
1501
1502	 current_SC->SCp.have_data_in = 1;
1503	 outb( 0x90 | PARITY_MASK, TMC_Cntl_port );
1504	 break;
1505      }
1506   }
1507
1508   if (current_SC->SCp.have_data_in == -1) { /* DATA OUT */
1509      while ( (data_count = FIFO_Size - inw( FIFO_Data_Count_port )) > 512 ) {
1510#if EVERY_ACCESS
1511	 printk( "DC=%d, ", data_count ) ;
1512#endif
1513	 if (data_count > current_SC->SCp.this_residual)
1514	       data_count = current_SC->SCp.this_residual;
1515	 if (data_count > 0) {
1516#if EVERY_ACCESS
1517	    printk( "%d OUT, ", data_count );
1518#endif
1519	    if (data_count == 1) {
1520	       outb( *current_SC->SCp.ptr++, Write_FIFO_port );
1521	       --current_SC->SCp.this_residual;
1522	    } else {
1523	       data_count >>= 1;
1524	       outsw( Write_FIFO_port, current_SC->SCp.ptr, data_count );
1525	       current_SC->SCp.ptr += 2 * data_count;
1526	       current_SC->SCp.this_residual -= 2 * data_count;
1527	    }
1528	 }
1529	 if (!current_SC->SCp.this_residual) {
1530	    if (current_SC->SCp.buffers_residual) {
1531	       --current_SC->SCp.buffers_residual;
1532	       ++current_SC->SCp.buffer;
1533	       current_SC->SCp.ptr = current_SC->SCp.buffer->address;
1534	       current_SC->SCp.this_residual = current_SC->SCp.buffer->length;
1535	    } else
1536		  break;
1537	 }
1538      }
1539   }
1540
1541   if (current_SC->SCp.have_data_in == 1) { /* DATA IN */
1542      while ((data_count = inw( FIFO_Data_Count_port )) > 0) {
1543#if EVERY_ACCESS
1544	 printk( "DC=%d, ", data_count );
1545#endif
1546	 if (data_count > current_SC->SCp.this_residual)
1547	       data_count = current_SC->SCp.this_residual;
1548	 if (data_count) {
1549#if EVERY_ACCESS
1550	    printk( "%d IN, ", data_count );
1551#endif
1552	    if (data_count == 1) {
1553	       *current_SC->SCp.ptr++ = inb( Read_FIFO_port );
1554	       --current_SC->SCp.this_residual;
1555	    } else {
1556	       data_count >>= 1; /* Number of words */
1557	       insw( Read_FIFO_port, current_SC->SCp.ptr, data_count );
1558	       current_SC->SCp.ptr += 2 * data_count;
1559	       current_SC->SCp.this_residual -= 2 * data_count;
1560	    }
1561	 }
1562	 if (!current_SC->SCp.this_residual
1563	     && current_SC->SCp.buffers_residual) {
1564	    --current_SC->SCp.buffers_residual;
1565	    ++current_SC->SCp.buffer;
1566	    current_SC->SCp.ptr = current_SC->SCp.buffer->address;
1567	    current_SC->SCp.this_residual = current_SC->SCp.buffer->length;
1568	 }
1569      }
1570   }
1571
1572   if (done) {
1573#if EVERY_ACCESS
1574      printk( " ** IN DONE %d ** ", current_SC->SCp.have_data_in );
1575#endif
1576
1577#if ERRORS_ONLY
1578      if (current_SC->cmnd[0] == REQUEST_SENSE && !current_SC->SCp.Status) {
1579	 if ((unsigned char)(*((char *)current_SC->request_buffer+2)) & 0x0f) {
1580	    unsigned char key;
1581	    unsigned char code;
1582	    unsigned char qualifier;
1583
1584	    key = (unsigned char)(*((char *)current_SC->request_buffer + 2))
1585		  & 0x0f;
1586	    code = (unsigned char)(*((char *)current_SC->request_buffer + 12));
1587	    qualifier = (unsigned char)(*((char *)current_SC->request_buffer
1588					  + 13));
1589
1590	    if (key != UNIT_ATTENTION
1591		&& !(key == NOT_READY
1592		     && code == 0x04
1593		     && (!qualifier || qualifier == 0x02 || qualifier == 0x01))
1594		&& !(key == ILLEGAL_REQUEST && (code == 0x25
1595						|| code == 0x24
1596						|| !code)))
1597
1598		  printk( "scsi: <fdomain> REQUEST SENSE"
1599			  " Key = %x, Code = %x, Qualifier = %x\n",
1600			  key, code, qualifier );
1601	 }
1602      }
1603#endif
1604#if EVERY_ACCESS
1605      printk( "BEFORE MY_DONE. . ." );
1606#endif
1607      spin_lock_irqsave(&io_request_lock, flags);
1608      my_done( (current_SC->SCp.Status & 0xff)
1609	       | ((current_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16) );
1610      spin_unlock_irqrestore(&io_request_lock, flags);
1611#if EVERY_ACCESS
1612      printk( "RETURNING.\n" );
1613#endif
1614
1615   } else {
1616      if (current_SC->SCp.phase & disconnect) {
1617	 outb( 0xd0 | FIFO_COUNT, Interrupt_Cntl_port );
1618	 outb( 0x00, SCSI_Cntl_port );
1619      } else {
1620	 outb( 0x90 | FIFO_COUNT, Interrupt_Cntl_port );
1621      }
1622   }
1623#if DEBUG_RACE
1624   in_interrupt_flag = 0;
1625#endif
1626   return;
1627}
1628
1629int fdomain_16x0_queue( Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
1630{
1631   if (in_command) {
1632      panic( "scsi: <fdomain> fdomain_16x0_queue() NOT REENTRANT!\n" );
1633   }
1634#if EVERY_ACCESS
1635   printk( "queue: target = %d cmnd = 0x%02x pieces = %d size = %u\n",
1636	   SCpnt->target,
1637	   *(unsigned char *)SCpnt->cmnd,
1638	   SCpnt->use_sg,
1639	   SCpnt->request_bufflen );
1640#endif
1641
1642   fdomain_make_bus_idle();
1643
1644   current_SC            = SCpnt; /* Save this for the done function */
1645   current_SC->scsi_done = done;
1646
1647   /* Initialize static data */
1648
1649   if (current_SC->use_sg) {
1650      current_SC->SCp.buffer =
1651	    (struct scatterlist *)current_SC->request_buffer;
1652      current_SC->SCp.ptr              = current_SC->SCp.buffer->address;
1653      current_SC->SCp.this_residual    = current_SC->SCp.buffer->length;
1654      current_SC->SCp.buffers_residual = current_SC->use_sg - 1;
1655   } else {
1656      current_SC->SCp.ptr              = (char *)current_SC->request_buffer;
1657      current_SC->SCp.this_residual    = current_SC->request_bufflen;
1658      current_SC->SCp.buffer           = NULL;
1659      current_SC->SCp.buffers_residual = 0;
1660   }
1661
1662
1663   current_SC->SCp.Status              = 0;
1664   current_SC->SCp.Message             = 0;
1665   current_SC->SCp.have_data_in        = 0;
1666   current_SC->SCp.sent_command        = 0;
1667   current_SC->SCp.phase               = in_arbitration;
1668
1669   /* Start arbitration */
1670   outb( 0x00, Interrupt_Cntl_port );
1671   outb( 0x00, SCSI_Cntl_port );              /* Disable data drivers */
1672   outb( adapter_mask, SCSI_Data_NoACK_port ); /* Set our id bit */
1673   ++in_command;
1674   outb( 0x20, Interrupt_Cntl_port );
1675   outb( 0x14 | PARITY_MASK, TMC_Cntl_port ); /* Start arbitration */
1676
1677   return 0;
1678}
1679
1680/* The following code, which simulates the old-style command function, was
1681   taken from Tommy Thorn's aha1542.c file.  This code is Copyright (C)
1682   1992 Tommy Thorn. */
1683
1684static volatile int internal_done_flag    = 0;
1685static volatile int internal_done_errcode = 0;
1686
1687static void internal_done( Scsi_Cmnd *SCpnt )
1688{
1689    internal_done_errcode = SCpnt->result;
1690    ++internal_done_flag;
1691}
1692
1693int fdomain_16x0_command( Scsi_Cmnd *SCpnt )
1694{
1695    fdomain_16x0_queue( SCpnt, internal_done );
1696
1697    while (!internal_done_flag)
1698	  ;
1699    internal_done_flag = 0;
1700    return internal_done_errcode;
1701}
1702
1703/* End of code derived from Tommy Thorn's work. */
1704
1705void print_info( Scsi_Cmnd *SCpnt )
1706{
1707   unsigned int imr;
1708   unsigned int irr;
1709   unsigned int isr;
1710
1711   if (!SCpnt || !SCpnt->host) {
1712      printk( "scsi: <fdomain> Cannot provide detailed information\n" );
1713      return;
1714   }
1715
1716   printk( "%s\n", fdomain_16x0_info( SCpnt->host ) );
1717   print_banner( SCpnt->host );
1718   switch (SCpnt->SCp.phase) {
1719   case in_arbitration: printk( "arbitration " ); break;
1720   case in_selection:   printk( "selection " );   break;
1721   case in_other:       printk( "other " );       break;
1722   default:             printk( "unknown " );     break;
1723   }
1724
1725   printk( "(%d), target = %d cmnd = 0x%02x pieces = %d size = %u\n",
1726	   SCpnt->SCp.phase,
1727	   SCpnt->target,
1728	   *(unsigned char *)SCpnt->cmnd,
1729	   SCpnt->use_sg,
1730	   SCpnt->request_bufflen );
1731   printk( "sent_command = %d, have_data_in = %d, timeout = %d\n",
1732	   SCpnt->SCp.sent_command,
1733	   SCpnt->SCp.have_data_in,
1734	   SCpnt->timeout );
1735#if DEBUG_RACE
1736   printk( "in_interrupt_flag = %d\n", in_interrupt_flag );
1737#endif
1738
1739   imr = (inb( 0x0a1 ) << 8) + inb( 0x21 );
1740   outb( 0x0a, 0xa0 );
1741   irr = inb( 0xa0 ) << 8;
1742   outb( 0x0a, 0x20 );
1743   irr += inb( 0x20 );
1744   outb( 0x0b, 0xa0 );
1745   isr = inb( 0xa0 ) << 8;
1746   outb( 0x0b, 0x20 );
1747   isr += inb( 0x20 );
1748
1749				/* Print out interesting information */
1750   printk( "IMR = 0x%04x", imr );
1751   if (imr & (1 << interrupt_level))
1752	 printk( " (masked)" );
1753   printk( ", IRR = 0x%04x, ISR = 0x%04x\n", irr, isr );
1754
1755   printk( "SCSI Status      = 0x%02x\n", inb( SCSI_Status_port ) );
1756   printk( "TMC Status       = 0x%02x", inb( TMC_Status_port ) );
1757   if (inb( TMC_Status_port & 1))
1758	 printk( " (interrupt)" );
1759   printk( "\n" );
1760   printk( "Interrupt Status = 0x%02x", inb( Interrupt_Status_port ) );
1761   if (inb( Interrupt_Status_port ) & 0x08)
1762	 printk( " (enabled)" );
1763   printk( "\n" );
1764   if (chip == tmc18c50 || chip == tmc18c30) {
1765      printk( "FIFO Status      = 0x%02x\n", inb( port_base + FIFO_Status ) );
1766      printk( "Int. Condition   = 0x%02x\n",
1767	      inb( port_base + Interrupt_Cond ) );
1768   }
1769   printk( "Configuration 1  = 0x%02x\n", inb( port_base + Configuration1 ) );
1770   if (chip == tmc18c50 || chip == tmc18c30)
1771	 printk( "Configuration 2  = 0x%02x\n",
1772		 inb( port_base + Configuration2 ) );
1773}
1774
1775int fdomain_16x0_abort( Scsi_Cmnd *SCpnt)
1776{
1777   unsigned long flags;
1778#if EVERY_ACCESS || ERRORS_ONLY || DEBUG_ABORT
1779   printk( "scsi: <fdomain> abort " );
1780#endif
1781
1782   save_flags( flags );
1783   cli();
1784   if (!in_command) {
1785#if EVERY_ACCESS || ERRORS_ONLY
1786      printk( " (not in command)\n" );
1787#endif
1788      restore_flags( flags );
1789      return SCSI_ABORT_NOT_RUNNING;
1790   } else printk( "\n" );
1791
1792#if DEBUG_ABORT
1793   print_info( SCpnt );
1794#endif
1795
1796   fdomain_make_bus_idle();
1797
1798   current_SC->SCp.phase |= aborted;
1799
1800   current_SC->result = DID_ABORT << 16;
1801
1802   restore_flags( flags );
1803
1804   /* Aborts are not done well. . . */
1805   my_done( DID_ABORT << 16 );
1806
1807   return SCSI_ABORT_SUCCESS;
1808}
1809
1810int fdomain_16x0_reset( Scsi_Cmnd *SCpnt, unsigned int ignored )
1811{
1812#if DEBUG_RESET
1813   static int called_once = 0;
1814#endif
1815
1816#if ERRORS_ONLY
1817   if (SCpnt) printk( "scsi: <fdomain> SCSI Bus Reset\n" );
1818#endif
1819
1820#if DEBUG_RESET
1821   if (called_once) print_info( current_SC );
1822   called_once = 1;
1823#endif
1824
1825   outb( 1, SCSI_Cntl_port );
1826   do_pause( 2 );
1827   outb( 0, SCSI_Cntl_port );
1828   do_pause( 115 );
1829   outb( 0, SCSI_Mode_Cntl_port );
1830   outb( PARITY_MASK, TMC_Cntl_port );
1831
1832   /* Unless this is the very first call (i.e., SCPnt == NULL), everything
1833      is probably hosed at this point.  We will, however, try to keep
1834      things going by informing the high-level code that we need help. */
1835
1836   return SCSI_RESET_WAKEUP;
1837}
1838
1839#include "sd.h"
1840#include <scsi/scsi_ioctl.h>
1841
1842int fdomain_16x0_biosparam( Scsi_Disk *disk, kdev_t dev, int *info_array )
1843{
1844   int              drive;
1845   unsigned char    buf[512 + sizeof (Scsi_Ioctl_Command)];
1846   Scsi_Ioctl_Command *sic = (Scsi_Ioctl_Command *) buf;
1847   int		    size      = disk->capacity;
1848   unsigned char    *data     = sic->data;
1849   unsigned char    do_read[] = { READ_6, 0, 0, 0, 1, 0 };
1850   int              retcode;
1851   unsigned long    offset;
1852   struct drive_info {
1853      unsigned short cylinders;
1854      unsigned char  heads;
1855      unsigned char  sectors;
1856   } i;
1857
1858   /* NOTES:
1859      The RAM area starts at 0x1f00 from the bios_base address.
1860
1861      For BIOS Version 2.0:
1862
1863      The drive parameter table seems to start at 0x1f30.
1864      The first byte's purpose is not known.
1865      Next is the cylinder, head, and sector information.
1866      The last 4 bytes appear to be the drive's size in sectors.
1867      The other bytes in the drive parameter table are unknown.
1868      If anyone figures them out, please send me mail, and I will
1869      update these notes.
1870
1871      Tape drives do not get placed in this table.
1872
1873      There is another table at 0x1fea:
1874      If the byte is 0x01, then the SCSI ID is not in use.
1875      If the byte is 0x18 or 0x48, then the SCSI ID is in use,
1876      although tapes don't seem to be in this table.  I haven't
1877      seen any other numbers (in a limited sample).
1878
1879      0x1f2d is a drive count (i.e., not including tapes)
1880
1881      The table at 0x1fcc are I/O ports addresses for the various
1882      operations.  I calculate these by hand in this driver code.
1883
1884
1885
1886      For the ISA-200S version of BIOS Version 2.0:
1887
1888      The drive parameter table starts at 0x1f33.
1889
1890      WARNING: Assume that the table entry is 25 bytes long.  Someone needs
1891      to check this for the Quantum ISA-200S card.
1892
1893
1894
1895      For BIOS Version 3.2:
1896
1897      The drive parameter table starts at 0x1f70.  Each entry is
1898      0x0a bytes long.  Heads are one less than we need to report.
1899    */
1900
1901   if (MAJOR(dev) != SCSI_DISK0_MAJOR) {
1902      printk("scsi: <fdomain> fdomain_16x0_biosparam: too many disks");
1903      return 0;
1904   }
1905   drive = MINOR(dev) >> 4;
1906
1907   if (bios_major == 2) {
1908      switch (Quantum) {
1909      case 2:			/* ISA_200S */
1910				/* The value of 25 has never been verified.
1911				   It should probably be 15. */
1912	 offset = bios_base + 0x1f33 + drive * 25;
1913	 break;
1914      case 3:			/* ISA_250MG */
1915	 offset = bios_base + 0x1f36 + drive * 15;
1916	 break;
1917      case 4:			/* ISA_200S (another one) */
1918	 offset = bios_base + 0x1f34 + drive * 15;
1919	 break;
1920      default:
1921	 offset = bios_base + 0x1f31 + drive * 25;
1922	 break;
1923      }
1924      isa_memcpy_fromio( &i, offset, sizeof( struct drive_info ) );
1925      info_array[0] = i.heads;
1926      info_array[1] = i.sectors;
1927      info_array[2] = i.cylinders;
1928   } else if (bios_major == 3
1929	      && bios_minor >= 0
1930	      && bios_minor < 4) { /* 3.0 and 3.2 BIOS */
1931      memcpy_fromio( &i, bios_base + 0x1f71 + drive * 10,
1932		     sizeof( struct drive_info ) );
1933      info_array[0] = i.heads + 1;
1934      info_array[1] = i.sectors;
1935      info_array[2] = i.cylinders;
1936   } else {			/* 3.4 BIOS (and up?) */
1937      /* This algorithm was provided by Future Domain (much thanks!). */
1938
1939      sic->inlen  = 0;		/* zero bytes out */
1940      sic->outlen = 512;		/* one sector in */
1941      memcpy( data, do_read, sizeof( do_read ) );
1942      retcode = kernel_scsi_ioctl( disk->device,
1943				   SCSI_IOCTL_SEND_COMMAND,
1944				   sic );
1945      if (!retcode				    /* SCSI command ok */
1946	  && data[511] == 0xaa && data[510] == 0x55 /* Partition table valid */
1947	  && data[0x1c2]) {			    /* Partition type */
1948
1949	 /* The partition table layout is as follows:
1950
1951	    Start: 0x1b3h
1952	    Offset: 0 = partition status
1953		    1 = starting head
1954		    2 = starting sector and cylinder (word, encoded)
1955		    4 = partition type
1956		    5 = ending head
1957		    6 = ending sector and cylinder (word, encoded)
1958		    8 = starting absolute sector (double word)
1959		    c = number of sectors (double word)
1960	    Signature: 0x1fe = 0x55aa
1961
1962	    So, this algorithm assumes:
1963	    1) the first partition table is in use,
1964	    2) the data in the first entry is correct, and
1965	    3) partitions never divide cylinders
1966
1967	    Note that (1) may be FALSE for NetBSD (and other BSD flavors),
1968	    as well as for Linux.  Note also, that Linux doesn't pay any
1969	    attention to the fields that are used by this algorithm -- it
1970	    only uses the absolute sector data.  Recent versions of Linux's
1971	    fdisk(1) will fill this data in correctly, and forthcoming
1972	    versions will check for consistency.
1973
1974	    Checking for a non-zero partition type is not part of the
1975	    Future Domain algorithm, but it seemed to be a reasonable thing
1976	    to do, especially in the Linux and BSD worlds. */
1977
1978	 info_array[0] = data[0x1c3] + 1;	    /* heads */
1979	 info_array[1] = data[0x1c4] & 0x3f;	    /* sectors */
1980      } else {
1981
1982 	 /* Note that this new method guarantees that there will always be
1983	    less than 1024 cylinders on a platter.  This is good for drives
1984	    up to approximately 7.85GB (where 1GB = 1024 * 1024 kB). */
1985
1986	 if ((unsigned int)size >= 0x7e0000U) {
1987	    info_array[0] = 0xff; /* heads   = 255 */
1988	    info_array[1] = 0x3f; /* sectors =  63 */
1989	 } else if ((unsigned int)size >= 0x200000U) {
1990	    info_array[0] = 0x80; /* heads   = 128 */
1991	    info_array[1] = 0x3f; /* sectors =  63 */
1992	 } else {
1993	    info_array[0] = 0x40; /* heads   =  64 */
1994	    info_array[1] = 0x20; /* sectors =  32 */
1995	 }
1996      }
1997				/* For both methods, compute the cylinders */
1998      info_array[2] = (unsigned int)size / (info_array[0] * info_array[1] );
1999   }
2000
2001   return 0;
2002}
2003
2004int fdomain_16x0_release(struct Scsi_Host *shpnt)
2005{
2006	if (shpnt->irq)
2007		free_irq(shpnt->irq, shpnt);
2008	if (shpnt->io_port && shpnt->n_io_port)
2009		release_region(shpnt->io_port, shpnt->n_io_port);
2010	return 0;
2011}
2012
2013MODULE_LICENSE("GPL");
2014
2015/* Eventually this will go into an include file, but this will be later */
2016static Scsi_Host_Template driver_template = FDOMAIN_16X0;
2017
2018#include "scsi_module.c"
2019