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: 5.50 $"
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 
print_banner(struct Scsi_Host * shpnt)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 /* no __init, may be called from fdomain_stubs.c */
fdomain_setup(char * str)565 int fdomain_setup( char *str )
566 {
567 	int ints[4];
568 
569 	(void)get_options(str, ARRAY_SIZE(ints), ints);
570 
571 	if (setup_called++ || ints[0] < 2 || ints[0] > 3) {
572 		printk( "scsi: <fdomain>"
573 		" Usage: fdomain=<PORT_BASE>,<IRQ>[,<ADAPTER_ID>]\n" );
574 		printk( "scsi: <fdomain> Bad LILO/INSMOD parameters?\n" );
575 		return 0;
576 	}
577 
578 	port_base       = ints[0] >= 1 ? ints[1] : 0;
579 	interrupt_level = ints[0] >= 2 ? ints[2] : 0;
580 	this_id         = ints[0] >= 3 ? ints[3] : 0;
581 
582 	bios_major = bios_minor = -1; /* Use geometry for BIOS version >= 3.4 */
583 	++setup_called;
584 	return 1;
585 }
586 
587 __setup("fdomain=", fdomain_setup);
588 
589 
do_pause(unsigned amount)590 static void do_pause( unsigned amount )	/* Pause for amount*10 milliseconds */
591 {
592    mdelay(10*amount);
593 }
594 
fdomain_make_bus_idle(void)595 inline static void fdomain_make_bus_idle( void )
596 {
597    outb( 0, SCSI_Cntl_port );
598    outb( 0, SCSI_Mode_Cntl_port );
599    if (chip == tmc18c50 || chip == tmc18c30)
600 	 outb( 0x21 | PARITY_MASK, TMC_Cntl_port ); /* Clear forced intr. */
601    else
602 	 outb( 0x01 | PARITY_MASK, TMC_Cntl_port );
603 }
604 
fdomain_is_valid_port(int port)605 static int fdomain_is_valid_port( int port )
606 {
607 #if DEBUG_DETECT
608    printk( " (%x%x),",
609 	   inb( port + MSB_ID_Code ), inb( port + LSB_ID_Code ) );
610 #endif
611 
612    /* The MCA ID is a unique id for each MCA compatible board.  We
613       are using ISA boards, but Future Domain provides the MCA ID
614       anyway.  We can use this ID to ensure that this is a Future
615       Domain TMC-1660/TMC-1680.
616     */
617 
618    if (inb( port + LSB_ID_Code ) != 0xe9) { /* test for 0x6127 id */
619       if (inb( port + LSB_ID_Code ) != 0x27) return 0;
620       if (inb( port + MSB_ID_Code ) != 0x61) return 0;
621       chip = tmc1800;
622    } else {				    /* test for 0xe960 id */
623       if (inb( port + MSB_ID_Code ) != 0x60) return 0;
624       chip = tmc18c50;
625 
626 				/* Try to toggle 32-bit mode.  This only
627 				   works on an 18c30 chip.  (User reports
628 				   say this works, so we should switch to
629 				   it in the near future.) */
630 
631       outb( 0x80, port + IO_Control );
632       if ((inb( port + Configuration2 ) & 0x80) == 0x80) {
633 	 outb( 0x00, port + IO_Control );
634 	 if ((inb( port + Configuration2 ) & 0x80) == 0x00) {
635 	    chip = tmc18c30;
636 	    FIFO_Size = 0x800;	/* 2k FIFO */
637 	 }
638       }
639 				/* If that failed, we are an 18c50. */
640    }
641 
642    return 1;
643 }
644 
fdomain_test_loopback(void)645 static int fdomain_test_loopback( void )
646 {
647    int i;
648    int result;
649 
650    for (i = 0; i < 255; i++) {
651       outb( i, port_base + Write_Loopback );
652       result = inb( port_base + Read_Loopback );
653       if (i != result)
654 	    return 1;
655    }
656    return 0;
657 }
658 
659 /* fdomain_get_irq assumes that we have a valid MCA ID for a
660    TMC-1660/TMC-1680 Future Domain board.  Now, check to be sure the
661    bios_base matches these ports.  If someone was unlucky enough to have
662    purchased more than one Future Domain board, then they will have to
663    modify this code, as we only detect one board here.  [The one with the
664    lowest bios_base.]
665 
666    Note that this routine is only used for systems without a PCI BIOS32
667    (e.g., ISA bus).  For PCI bus systems, this routine will likely fail
668    unless one of the IRQs listed in the ints array is used by the board.
669    Sometimes it is possible to use the computer's BIOS setup screen to
670    configure a PCI system so that one of these IRQs will be used by the
671    Future Domain card. */
672 
fdomain_get_irq(int base)673 static int fdomain_get_irq( int base )
674 {
675    int options = inb( base + Configuration1 );
676 
677 #if DEBUG_DETECT
678    printk( "scsi: <fdomain> Options = %x\n", options );
679 #endif
680 
681 				/* Check for board with lowest bios_base --
682 				   this isn't valid for the 18c30 or for
683 				   boards on the PCI bus, so just assume we
684 				   have the right board. */
685 
686    if (chip != tmc18c30
687        && !PCI_bus
688        && addresses[ (options & 0xc0) >> 6 ] != bios_base) return 0;
689 
690    return ints[ (options & 0x0e) >> 1 ];
691 }
692 
fdomain_isa_detect(int * irq,int * iobase)693 static int fdomain_isa_detect( int *irq, int *iobase )
694 {
695    int i, j;
696    int base = 0xdeadbeef;
697    int flag = 0;
698 
699 #if DEBUG_DETECT
700    printk( "scsi: <fdomain> fdomain_isa_detect:" );
701 #endif
702 
703 
704    for (i = 0; !bios_base && i < ADDRESS_COUNT; i++) {
705 #if DEBUG_DETECT
706       printk( " %lx(%lx),", addresses[i], bios_base );
707 #endif
708       for (j = 0; !bios_base && j < SIGNATURE_COUNT; j++) {
709 	 if (isa_check_signature(addresses[i] + signatures[j].sig_offset,
710 			     signatures[j].signature,
711 			     signatures[j].sig_length )) {
712 	    bios_major = signatures[j].major_bios_version;
713 	    bios_minor = signatures[j].minor_bios_version;
714 	    PCI_bus    = (signatures[j].flag == 1);
715 	    Quantum    = (signatures[j].flag > 1) ? signatures[j].flag : 0;
716 	    bios_base  = addresses[i];
717 	 }
718       }
719    }
720 
721    if (bios_major == 2) {
722       /* The TMC-1660/TMC-1680 has a RAM area just after the BIOS ROM.
723 	 Assuming the ROM is enabled (otherwise we wouldn't have been
724 	 able to read the ROM signature :-), then the ROM sets up the
725 	 RAM area with some magic numbers, such as a list of port
726 	 base addresses and a list of the disk "geometry" reported to
727 	 DOS (this geometry has nothing to do with physical geometry).
728        */
729 
730       switch (Quantum) {
731       case 2:			/* ISA_200S */
732       case 3:			/* ISA_250MG */
733 	 base = isa_readb(bios_base + 0x1fa2) + (isa_readb(bios_base + 0x1fa3) << 8);
734 	 break;
735       case 4:			/* ISA_200S (another one) */
736 	 base = isa_readb(bios_base + 0x1fa3) + (isa_readb(bios_base + 0x1fa4) << 8);
737 	 break;
738       default:
739 	 base = isa_readb(bios_base + 0x1fcc) + (isa_readb(bios_base + 0x1fcd) << 8);
740 	 break;
741       }
742 
743 #if DEBUG_DETECT
744       printk( " %x,", base );
745 #endif
746 
747       for (flag = 0, i = 0; !flag && i < PORT_COUNT; i++) {
748 	 if (base == ports[i])
749 	       ++flag;
750       }
751 
752       if (flag && fdomain_is_valid_port( base )) {
753 	 *irq    = fdomain_get_irq( base );
754 	 *iobase = base;
755 	 return 1;
756       }
757 
758       /* This is a bad sign.  It usually means that someone patched the
759 	 BIOS signature list (the signatures variable) to contain a BIOS
760 	 signature for a board *OTHER THAN* the TMC-1660/TMC-1680. */
761 
762 #if DEBUG_DETECT
763       printk( " RAM FAILED, " );
764 #endif
765    }
766 
767    /* Anyway, the alternative to finding the address in the RAM is to just
768       search through every possible port address for one that is attached
769       to the Future Domain card.  Don't panic, though, about reading all
770       these random port addresses -- there are rumors that the Future
771       Domain BIOS does something very similar.
772 
773       Do not, however, check ports which the kernel knows are being used by
774       another driver. */
775 
776    for (i = 0; i < PORT_COUNT; i++) {
777       base = ports[i];
778       if (check_region( base, 0x10 )) {
779 #if DEBUG_DETECT
780 	 printk( " (%x inuse),", base );
781 #endif
782 	 continue;
783       }
784 #if DEBUG_DETECT
785       printk( " %x,", base );
786 #endif
787       if ((flag = fdomain_is_valid_port( base ))) break;
788    }
789 
790 #if DEBUG_DETECT
791    if (flag) printk( " SUCCESS\n" );
792    else      printk( " FAILURE\n" );
793 #endif
794 
795    if (!flag) return 0;		/* iobase not found */
796 
797    *irq    = fdomain_get_irq( base );
798    *iobase = base;
799 
800    return 1;			/* success */
801 }
802 
803 /* PCI detection function: int fdomain_pci_bios_detect(int* irq, int*
804    iobase) This function gets the Interrupt Level and I/O base address from
805    the PCI configuration registers. */
806 
807 #ifdef CONFIG_PCI
fdomain_pci_bios_detect(int * irq,int * iobase,struct pci_dev ** ret_pdev)808 static int fdomain_pci_bios_detect( int *irq, int *iobase, struct pci_dev **ret_pdev )
809 {
810    unsigned int     pci_irq;                /* PCI interrupt line */
811    unsigned long    pci_base;               /* PCI I/O base address */
812    struct pci_dev   *pdev = NULL;
813 
814    if (!pci_present()) return 0;
815 
816 #if DEBUG_DETECT
817    /* Tell how to print a list of the known PCI devices from bios32 and
818       list vendor and device IDs being used if in debug mode.  */
819 
820    printk( "scsi: <fdomain> INFO: use lspci -v to see list of PCI devices\n" );
821    printk( "scsi: <fdomain> TMC-3260 detect:"
822 	   " Using Vendor ID: 0x%x and Device ID: 0x%x\n",
823 	   PCI_VENDOR_ID_FD,
824 	   PCI_DEVICE_ID_FD_36C70 );
825 #endif
826 
827    if ((pdev = pci_find_device(PCI_VENDOR_ID_FD,
828 			       PCI_DEVICE_ID_FD_36C70,
829 			       pdev)) == NULL)
830      return 0;
831    if (pci_enable_device(pdev)) return 0;
832 
833 #if DEBUG_DETECT
834    printk( "scsi: <fdomain> TMC-3260 detect:"
835 	   " PCI bus %u, device %u, function %u\n",
836 	   pdev->bus->number,
837 	   PCI_SLOT(pdev->devfn),
838 	   PCI_FUNC(pdev->devfn));
839 #endif
840 
841    /* We now have the appropriate device function for the FD board so we
842       just read the PCI config info from the registers.  */
843 
844    pci_base = pci_resource_start(pdev, 0);
845    pci_irq = pdev->irq;
846 
847    /* Now we have the I/O base address and interrupt from the PCI
848       configuration registers. */
849 
850    *irq    = pci_irq;
851    *iobase = pci_base;
852    *ret_pdev = pdev;
853 
854 #if DEBUG_DETECT
855    printk( "scsi: <fdomain> TMC-3260 detect:"
856 	   " IRQ = %d, I/O base = 0x%x [0x%lx]\n", *irq, *iobase, pci_base );
857 #endif
858 
859    if (!fdomain_is_valid_port( *iobase )) {
860       printk( "scsi: <fdomain>"
861 	      " PCI card detected, but driver not loaded (invalid port)\n" );
862       return 0;
863    }
864 
865 				/* Fill in a few global variables.  Ugh. */
866    bios_major = bios_minor = -1;
867    PCI_bus    = 1;
868    Quantum    = 0;
869    bios_base  = 0;
870 
871    return 1;
872 }
873 #endif
874 
fdomain_16x0_detect(Scsi_Host_Template * tpnt)875 int fdomain_16x0_detect( Scsi_Host_Template *tpnt )
876 {
877    int              retcode;
878    struct Scsi_Host *shpnt;
879    struct pci_dev *pdev = NULL;
880 #if DO_DETECT
881    int i = 0;
882    int j = 0;
883    const int        buflen = 255;
884    Scsi_Cmnd        SCinit;
885    unsigned char    do_inquiry[] =       { INQUIRY, 0, 0, 0, buflen, 0 };
886    unsigned char    do_request_sense[] = { REQUEST_SENSE, 0, 0, 0, buflen, 0 };
887    unsigned char    do_read_capacity[] = { READ_CAPACITY,
888 					   0, 0, 0, 0, 0, 0, 0, 0, 0 };
889    unsigned char    buf[buflen];
890 #endif
891 
892    tpnt->proc_name = "fdomain";
893 
894 #ifdef MODULE
895 	if (fdomain)
896 		fdomain_setup(fdomain);
897 #endif
898 
899    if (setup_called) {
900 #if DEBUG_DETECT
901       printk( "scsi: <fdomain> No BIOS, using port_base = 0x%x, irq = %d\n",
902 	      port_base, interrupt_level );
903 #endif
904       if (!fdomain_is_valid_port( port_base )) {
905 	 printk( "scsi: <fdomain> Cannot locate chip at port base 0x%x\n",
906 		 port_base );
907 	 printk( "scsi: <fdomain> Bad LILO/INSMOD parameters?\n" );
908 	 return 0;
909       }
910    } else {
911       int flag = 0;
912 
913 #ifdef CONFIG_PCI
914 				/* Try PCI detection first */
915       flag = fdomain_pci_bios_detect( &interrupt_level, &port_base, &pdev );
916 #endif
917       if (!flag) {
918 				/* Then try ISA bus detection */
919 	 flag = fdomain_isa_detect( &interrupt_level, &port_base );
920 
921 	 if (!flag) {
922 	    printk( "scsi: <fdomain> Detection failed (no card)\n" );
923 	    return 0;
924 	 }
925       }
926    }
927 
928    SCSI_Mode_Cntl_port   = port_base + SCSI_Mode_Cntl;
929    FIFO_Data_Count_port  = port_base + FIFO_Data_Count;
930    Interrupt_Cntl_port   = port_base + Interrupt_Cntl;
931    Interrupt_Status_port = port_base + Interrupt_Status;
932    Read_FIFO_port        = port_base + Read_FIFO;
933    Read_SCSI_Data_port   = port_base + Read_SCSI_Data;
934    SCSI_Cntl_port        = port_base + SCSI_Cntl;
935    SCSI_Data_NoACK_port  = port_base + SCSI_Data_NoACK;
936    SCSI_Status_port      = port_base + SCSI_Status;
937    TMC_Cntl_port         = port_base + TMC_Cntl;
938    TMC_Status_port       = port_base + TMC_Status;
939    Write_FIFO_port       = port_base + Write_FIFO;
940    Write_SCSI_Data_port  = port_base + Write_SCSI_Data;
941 
942    fdomain_16x0_reset( NULL, 0 );
943 
944    if (fdomain_test_loopback()) {
945       printk( "scsi: <fdomain> Detection failed"
946 	      " (loopback test failed at port base 0x%x)\n", port_base );
947       if (setup_called) {
948 	 printk( "scsi: <fdomain> Bad LILO/INSMOD parameters?\n" );
949       }
950       return 0;
951    }
952 
953    if (this_id) {
954       tpnt->this_id = (this_id & 0x07);
955       adapter_mask  = (1 << tpnt->this_id);
956    } else {
957       if (PCI_bus || (bios_major == 3 && bios_minor >= 2) || bios_major < 0) {
958 	 tpnt->this_id = 7;
959 	 adapter_mask  = 0x80;
960       } else {
961 	 tpnt->this_id = 6;
962 	 adapter_mask  = 0x40;
963       }
964    }
965 
966 				/* Print out a banner here in case we can't
967 				   get resources.  */
968 
969    shpnt = scsi_register( tpnt, 0 );
970    if(shpnt == NULL)
971    	return 0;
972    shpnt->irq = interrupt_level;
973    shpnt->io_port = port_base;
974    scsi_set_pci_device(shpnt, pdev);
975    shpnt->n_io_port = 0x10;
976    print_banner( shpnt );
977 
978 				/* Log IRQ with kernel */
979    if (!interrupt_level) {
980       printk( "scsi: <fdomain>"
981 	      " Card Detected, but driver not loaded (no IRQ)\n" );
982       return 0;
983    } else {
984       /* Register the IRQ with the kernel */
985 
986       retcode = request_irq( interrupt_level,
987 			     do_fdomain_16x0_intr, pdev?SA_SHIRQ:0, "fdomain", shpnt);
988 
989       if (retcode < 0) {
990 	 if (retcode == -EINVAL) {
991 	    printk( "scsi: <fdomain> IRQ %d is bad!\n", interrupt_level );
992 	    printk( "                This shouldn't happen!\n" );
993 	    printk( "                Send mail to faith@acm.org\n" );
994 	 } else if (retcode == -EBUSY) {
995 	    printk( "scsi: <fdomain> IRQ %d is already in use!\n",
996 		    interrupt_level );
997 	    printk( "                Please use another IRQ!\n" );
998 	 } else {
999 	    printk( "scsi: <fdomain> Error getting IRQ %d\n",
1000 		    interrupt_level );
1001 	    printk( "                This shouldn't happen!\n" );
1002 	    printk( "                Send mail to faith@acm.org\n" );
1003 	 }
1004 	 printk( "scsi: <fdomain> Detected, but driver not loaded (IRQ)\n" );
1005 	 return 0;
1006       }
1007    }
1008 
1009 				/* Log I/O ports with kernel */
1010    request_region( port_base, 0x10, "fdomain" );
1011 
1012 #if DO_DETECT
1013 
1014    /* These routines are here because of the way the SCSI bus behaves after
1015       a reset.  This appropriate behavior was not handled correctly by the
1016       higher level SCSI routines when I first wrote this driver.  Now,
1017       however, correct scan routines are part of scsi.c and these routines
1018       are no longer needed.  However, this code is still good for
1019       debugging.  */
1020 
1021    SCinit.request_buffer  = SCinit.buffer = buf;
1022    SCinit.request_bufflen = SCinit.bufflen = sizeof(buf)-1;
1023    SCinit.use_sg          = 0;
1024    SCinit.lun             = 0;
1025 
1026    printk( "scsi: <fdomain> detection routine scanning for devices:\n" );
1027    for (i = 0; i < 8; i++) {
1028       SCinit.target = i;
1029       if (i == tpnt->this_id)	/* Skip host adapter */
1030 	    continue;
1031       memcpy(SCinit.cmnd, do_request_sense, sizeof(do_request_sense));
1032       retcode = fdomain_16x0_command(&SCinit);
1033       if (!retcode) {
1034 	 memcpy(SCinit.cmnd, do_inquiry, sizeof(do_inquiry));
1035 	 retcode = fdomain_16x0_command(&SCinit);
1036 	 if (!retcode) {
1037 	    printk( "     SCSI ID %d: ", i );
1038 	    for (j = 8; j < (buf[4] < 32 ? buf[4] : 32); j++)
1039 		  printk( "%c", buf[j] >= 20 ? buf[j] : ' ' );
1040 	    memcpy(SCinit.cmnd, do_read_capacity, sizeof(do_read_capacity));
1041 	    retcode = fdomain_16x0_command(&SCinit);
1042 	    if (!retcode) {
1043 	       unsigned long blocks, size, capacity;
1044 
1045 	       blocks = (buf[0] << 24) | (buf[1] << 16)
1046 		     | (buf[2] << 8) | buf[3];
1047 	       size = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
1048 	       capacity = +( +(blocks / 1024L) * +(size * 10L)) / 1024L;
1049 
1050 	       printk( "%lu MB (%lu byte blocks)",
1051 		       ((capacity + 5L) / 10L), size );
1052 	    } else {
1053 	       memcpy(SCinit.cmnd, do_request_sense, sizeof(do_request_sense));
1054 	       retcode = fdomain_16x0_command(&SCinit);
1055 	    }
1056 	    printk ("\n" );
1057 	 } else {
1058 	    memcpy(SCinit.cmnd, do_request_sense, sizeof(do_request_sense));
1059 	    retcode = fdomain_16x0_command(&SCinit);
1060 	 }
1061       }
1062    }
1063 #endif
1064 
1065    return 1;			/* Maximum of one adapter will be detected. */
1066 }
1067 
fdomain_16x0_info(struct Scsi_Host * ignore)1068 const char *fdomain_16x0_info( struct Scsi_Host *ignore )
1069 {
1070    static char buffer[128];
1071    char        *pt;
1072 
1073    strcpy( buffer, "Future Domain 16-bit SCSI Driver Version" );
1074    if (strchr( VERSION, ':')) { /* Assume VERSION is an RCS Revision string */
1075       strcat( buffer, strchr( VERSION, ':' ) + 1 );
1076       pt = strrchr( buffer, '$') - 1;
1077       if (!pt)  		/* Stripped RCS Revision string? */
1078 	    pt = buffer + strlen( buffer ) - 1;
1079       if (*pt != ' ')
1080 	    ++pt;
1081       *pt = '\0';
1082    } else {			/* Assume VERSION is a number */
1083       strcat( buffer, " " VERSION );
1084    }
1085 
1086    return buffer;
1087 }
1088 
1089 				/* First pass at /proc information routine. */
1090 /*
1091  * inout : decides on the direction of the dataflow and the meaning of the
1092  *         variables
1093  * buffer: If inout==FALSE data is being written to it else read from it
1094  * *start: If inout==FALSE start of the valid data in the buffer
1095  * offset: If inout==FALSE offset from the beginning of the imaginary file
1096  *         from which we start writing into the buffer
1097  * length: If inout==FALSE max number of bytes to be written into the buffer
1098  *         else number of bytes in the buffer
1099  */
fdomain_16x0_proc_info(char * buffer,char ** start,off_t offset,int length,int hostno,int inout)1100 int fdomain_16x0_proc_info( char *buffer, char **start, off_t offset,
1101 			    int length, int hostno, int inout )
1102 {
1103    const char *info = fdomain_16x0_info( NULL );
1104    int        len;
1105    int        pos;
1106    int        begin;
1107 
1108    if (inout) return(-ENOSYS);
1109 
1110    begin = 0;
1111    strcpy( buffer, info );
1112    strcat( buffer, "\n" );
1113 
1114    pos = len = strlen( buffer );
1115 
1116    if(pos < offset) {
1117       len = 0;
1118       begin = pos;
1119    }
1120 
1121    *start = buffer + (offset - begin);   /* Start of wanted data */
1122    len -= (offset - begin);
1123    if(len > length) len = length;
1124 
1125    return(len);
1126 }
1127 
1128 #if 0
1129 static int fdomain_arbitrate( void )
1130 {
1131    int           status = 0;
1132    unsigned long timeout;
1133 
1134 #if EVERY_ACCESS
1135    printk( "fdomain_arbitrate()\n" );
1136 #endif
1137 
1138    outb( 0x00, SCSI_Cntl_port );              /* Disable data drivers */
1139    outb( adapter_mask, port_base + SCSI_Data_NoACK ); /* Set our id bit */
1140    outb( 0x04 | PARITY_MASK, TMC_Cntl_port ); /* Start arbitration */
1141 
1142    timeout = 500;
1143    do {
1144       status = inb( TMC_Status_port );        /* Read adapter status */
1145       if (status & 0x02)		      /* Arbitration complete */
1146 	    return 0;
1147       mdelay(1);			/* Wait one millisecond */
1148    } while (--timeout);
1149 
1150    /* Make bus idle */
1151    fdomain_make_bus_idle();
1152 
1153 #if EVERY_ACCESS
1154    printk( "Arbitration failed, status = %x\n", status );
1155 #endif
1156 #if ERRORS_ONLY
1157    printk( "scsi: <fdomain> Arbitration failed, status = %x\n", status );
1158 #endif
1159    return 1;
1160 }
1161 #endif
1162 
fdomain_select(int target)1163 static int fdomain_select( int target )
1164 {
1165    int           status;
1166    unsigned long timeout;
1167    static int    flag = 0;
1168 
1169 
1170    outb( 0x82, SCSI_Cntl_port ); /* Bus Enable + Select */
1171    outb( adapter_mask | (1 << target), SCSI_Data_NoACK_port );
1172 
1173    /* Stop arbitration and enable parity */
1174    outb( PARITY_MASK, TMC_Cntl_port );
1175 
1176    timeout = 350;			/* 350 msec */
1177 
1178    do {
1179       status = inb( SCSI_Status_port ); /* Read adapter status */
1180       if (status & 1) {			/* Busy asserted */
1181 	 /* Enable SCSI Bus (on error, should make bus idle with 0) */
1182 	 outb( 0x80, SCSI_Cntl_port );
1183 	 return 0;
1184       }
1185       mdelay(1);			/* wait one msec */
1186    } while (--timeout);
1187    /* Make bus idle */
1188    fdomain_make_bus_idle();
1189 #if EVERY_ACCESS
1190    if (!target) printk( "Selection failed\n" );
1191 #endif
1192 #if ERRORS_ONLY
1193    if (!target) {
1194       if (!flag) /* Skip first failure for all chips. */
1195 	    ++flag;
1196       else
1197 	    printk( "scsi: <fdomain> Selection failed\n" );
1198    }
1199 #endif
1200    return 1;
1201 }
1202 
my_done(int error)1203 void my_done( int error )
1204 {
1205    if (in_command) {
1206       in_command = 0;
1207       outb( 0x00, Interrupt_Cntl_port );
1208       fdomain_make_bus_idle();
1209       current_SC->result = error;
1210       if (current_SC->scsi_done)
1211 	    current_SC->scsi_done( current_SC );
1212       else panic( "scsi: <fdomain> current_SC->scsi_done() == NULL" );
1213    } else {
1214       panic( "scsi: <fdomain> my_done() called outside of command\n" );
1215    }
1216 #if DEBUG_RACE
1217    in_interrupt_flag = 0;
1218 #endif
1219 }
1220 
do_fdomain_16x0_intr(int irq,void * dev_id,struct pt_regs * regs)1221 void do_fdomain_16x0_intr( int irq, void *dev_id, struct pt_regs * regs )
1222 {
1223    unsigned long flags;
1224    int      status;
1225    int      done = 0;
1226    unsigned data_count;
1227 
1228 				/* The fdomain_16x0_intr is only called via
1229 				   the interrupt handler.  The goal of the
1230 				   sti() here is to allow other
1231 				   interruptions while this routine is
1232 				   running. */
1233 
1234    /* Check for other IRQ sources */
1235    if((inb(TMC_Status_port)&0x01)==0)
1236    	return;
1237 
1238    /* It is our IRQ */
1239    outb( 0x00, Interrupt_Cntl_port );
1240 
1241    /* We usually have one spurious interrupt after each command.  Ignore it. */
1242    if (!in_command || !current_SC) {	/* Spurious interrupt */
1243 #if EVERY_ACCESS
1244       printk( "Spurious interrupt, in_command = %d, current_SC = %x\n",
1245 	      in_command, current_SC );
1246 #endif
1247       return;
1248    }
1249 
1250    /* Abort calls my_done, so we do nothing here. */
1251    if (current_SC->SCp.phase & aborted) {
1252 #if DEBUG_ABORT
1253       printk( "scsi: <fdomain> Interrupt after abort, ignoring\n" );
1254 #endif
1255       /*
1256       return; */
1257    }
1258 
1259 #if DEBUG_RACE
1260    ++in_interrupt_flag;
1261 #endif
1262 
1263    if (current_SC->SCp.phase & in_arbitration) {
1264       status = inb( TMC_Status_port );        /* Read adapter status */
1265       if (!(status & 0x02)) {
1266 #if EVERY_ACCESS
1267 	 printk( " AFAIL " );
1268 #endif
1269          spin_lock_irqsave(&io_request_lock, flags);
1270 	 my_done( DID_BUS_BUSY << 16 );
1271          spin_unlock_irqrestore(&io_request_lock, flags);
1272 	 return;
1273       }
1274       current_SC->SCp.phase = in_selection;
1275 
1276       outb( 0x40 | FIFO_COUNT, Interrupt_Cntl_port );
1277 
1278       outb( 0x82, SCSI_Cntl_port ); /* Bus Enable + Select */
1279       outb( adapter_mask | (1 << current_SC->target), SCSI_Data_NoACK_port );
1280 
1281       /* Stop arbitration and enable parity */
1282       outb( 0x10 | PARITY_MASK, TMC_Cntl_port );
1283 #if DEBUG_RACE
1284       in_interrupt_flag = 0;
1285 #endif
1286       return;
1287    } else if (current_SC->SCp.phase & in_selection) {
1288       status = inb( SCSI_Status_port );
1289       if (!(status & 0x01)) {
1290 	 /* Try again, for slow devices */
1291 	 if (fdomain_select( current_SC->target )) {
1292 #if EVERY_ACCESS
1293 	    printk( " SFAIL " );
1294 #endif
1295             spin_lock_irqsave(&io_request_lock, flags);
1296 	    my_done( DID_NO_CONNECT << 16 );
1297             spin_unlock_irqrestore(&io_request_lock, flags);
1298 	    return;
1299 	 } else {
1300 #if EVERY_ACCESS
1301 	    printk( " AltSel " );
1302 #endif
1303 	    /* Stop arbitration and enable parity */
1304 	    outb( 0x10 | PARITY_MASK, TMC_Cntl_port );
1305 	 }
1306       }
1307       current_SC->SCp.phase = in_other;
1308       outb( 0x90 | FIFO_COUNT, Interrupt_Cntl_port );
1309       outb( 0x80, SCSI_Cntl_port );
1310 #if DEBUG_RACE
1311       in_interrupt_flag = 0;
1312 #endif
1313       return;
1314    }
1315 
1316    /* current_SC->SCp.phase == in_other: this is the body of the routine */
1317 
1318    status = inb( SCSI_Status_port );
1319 
1320    if (status & 0x10) {	/* REQ */
1321 
1322       switch (status & 0x0e) {
1323 
1324       case 0x08:		/* COMMAND OUT */
1325 	 outb( current_SC->cmnd[current_SC->SCp.sent_command++],
1326 	       Write_SCSI_Data_port );
1327 #if EVERY_ACCESS
1328 	 printk( "CMD = %x,",
1329 		 current_SC->cmnd[ current_SC->SCp.sent_command - 1] );
1330 #endif
1331 	 break;
1332       case 0x00:		/* DATA OUT -- tmc18c50/tmc18c30 only */
1333 	 if (chip != tmc1800 && !current_SC->SCp.have_data_in) {
1334 	    current_SC->SCp.have_data_in = -1;
1335 	    outb( 0xd0 | PARITY_MASK, TMC_Cntl_port );
1336 	 }
1337 	 break;
1338       case 0x04:		/* DATA IN -- tmc18c50/tmc18c30 only */
1339 	 if (chip != tmc1800 && !current_SC->SCp.have_data_in) {
1340 	    current_SC->SCp.have_data_in = 1;
1341 	    outb( 0x90 | PARITY_MASK, TMC_Cntl_port );
1342 	 }
1343 	 break;
1344       case 0x0c:		/* STATUS IN */
1345 	 current_SC->SCp.Status = inb( Read_SCSI_Data_port );
1346 #if EVERY_ACCESS
1347 	 printk( "Status = %x, ", current_SC->SCp.Status );
1348 #endif
1349 #if ERRORS_ONLY
1350 	 if (current_SC->SCp.Status
1351 	     && current_SC->SCp.Status != 2
1352 	     && current_SC->SCp.Status != 8) {
1353 	    printk( "scsi: <fdomain> target = %d, command = %x, status = %x\n",
1354 		    current_SC->target,
1355 		    current_SC->cmnd[0],
1356 		    current_SC->SCp.Status );
1357 	 }
1358 #endif
1359 	       break;
1360       case 0x0a:		/* MESSAGE OUT */
1361 	 outb( MESSAGE_REJECT, Write_SCSI_Data_port ); /* Reject */
1362 	 break;
1363       case 0x0e:		/* MESSAGE IN */
1364 	 current_SC->SCp.Message = inb( Read_SCSI_Data_port );
1365 #if EVERY_ACCESS
1366 	 printk( "Message = %x, ", current_SC->SCp.Message );
1367 #endif
1368 	 if (!current_SC->SCp.Message) ++done;
1369 #if DEBUG_MESSAGES || EVERY_ACCESS
1370 	 if (current_SC->SCp.Message) {
1371 	    printk( "scsi: <fdomain> message = %x\n",
1372 		    current_SC->SCp.Message );
1373 	 }
1374 #endif
1375 	 break;
1376       }
1377    }
1378 
1379    if (chip == tmc1800
1380        && !current_SC->SCp.have_data_in
1381        && (current_SC->SCp.sent_command
1382 	   >= current_SC->cmd_len)) {
1383 				/* We have to get the FIFO direction
1384 				   correct, so I've made a table based
1385 				   on the SCSI Standard of which commands
1386 				   appear to require a DATA OUT phase.
1387 				 */
1388       /*
1389 	p. 94: Command for all device types
1390 	CHANGE DEFINITION            40 DATA OUT
1391 	COMPARE                      39 DATA OUT
1392 	COPY                         18 DATA OUT
1393 	COPY AND VERIFY              3a DATA OUT
1394 	INQUIRY                      12
1395 	LOG SELECT                   4c DATA OUT
1396 	LOG SENSE                    4d
1397 	MODE SELECT (6)              15 DATA OUT
1398 	MODE SELECT (10)             55 DATA OUT
1399 	MODE SENSE (6)               1a
1400 	MODE SENSE (10)              5a
1401 	READ BUFFER                  3c
1402 	RECEIVE DIAGNOSTIC RESULTS   1c
1403 	REQUEST SENSE                03
1404 	SEND DIAGNOSTIC              1d DATA OUT
1405 	TEST UNIT READY              00
1406 	WRITE BUFFER                 3b DATA OUT
1407 
1408 	p.178: Commands for direct-access devices (not listed on p. 94)
1409 	FORMAT UNIT                  04 DATA OUT
1410 	LOCK-UNLOCK CACHE            36
1411 	PRE-FETCH                    34
1412 	PREVENT-ALLOW MEDIUM REMOVAL 1e
1413 	READ (6)/RECEIVE             08
1414 	READ (10)                    3c
1415 	READ CAPACITY                25
1416 	READ DEFECT DATA (10)        37
1417 	READ LONG                    3e
1418 	REASSIGN BLOCKS              07 DATA OUT
1419 	RELEASE                      17
1420 	RESERVE                      16 DATA OUT
1421 	REZERO UNIT/REWIND           01
1422 	SEARCH DATA EQUAL (10)       31 DATA OUT
1423 	SEARCH DATA HIGH (10)        30 DATA OUT
1424 	SEARCH DATA LOW (10)         32 DATA OUT
1425 	SEEK (6)                     0b
1426 	SEEK (10)                    2b
1427 	SET LIMITS (10)              33
1428 	START STOP UNIT              1b
1429 	SYNCHRONIZE CACHE            35
1430 	VERIFY (10)                  2f
1431 	WRITE (6)/PRINT/SEND         0a DATA OUT
1432 	WRITE (10)/SEND              2a DATA OUT
1433 	WRITE AND VERIFY (10)        2e DATA OUT
1434 	WRITE LONG                   3f DATA OUT
1435 	WRITE SAME                   41 DATA OUT ?
1436 
1437 	p. 261: Commands for sequential-access devices (not previously listed)
1438 	ERASE                        19
1439 	LOAD UNLOAD                  1b
1440 	LOCATE                       2b
1441 	READ BLOCK LIMITS            05
1442 	READ POSITION                34
1443 	READ REVERSE                 0f
1444 	RECOVER BUFFERED DATA        14
1445 	SPACE                        11
1446 	WRITE FILEMARKS              10 ?
1447 
1448 	p. 298: Commands for printer devices (not previously listed)
1449 	****** NOT SUPPORTED BY THIS DRIVER, since 0b is SEEK (6) *****
1450 	SLEW AND PRINT               0b DATA OUT  -- same as seek
1451 	STOP PRINT                   1b
1452 	SYNCHRONIZE BUFFER           10
1453 
1454 	p. 315: Commands for processor devices (not previously listed)
1455 
1456 	p. 321: Commands for write-once devices (not previously listed)
1457 	MEDIUM SCAN                  38
1458 	READ (12)                    a8
1459 	SEARCH DATA EQUAL (12)       b1 DATA OUT
1460 	SEARCH DATA HIGH (12)        b0 DATA OUT
1461 	SEARCH DATA LOW (12)         b2 DATA OUT
1462 	SET LIMITS (12)              b3
1463 	VERIFY (12)                  af
1464 	WRITE (12)                   aa DATA OUT
1465 	WRITE AND VERIFY (12)        ae DATA OUT
1466 
1467 	p. 332: Commands for CD-ROM devices (not previously listed)
1468 	PAUSE/RESUME                 4b
1469 	PLAY AUDIO (10)              45
1470 	PLAY AUDIO (12)              a5
1471 	PLAY AUDIO MSF               47
1472 	PLAY TRACK RELATIVE (10)     49
1473 	PLAY TRACK RELATIVE (12)     a9
1474 	READ HEADER                  44
1475 	READ SUB-CHANNEL             42
1476 	READ TOC                     43
1477 
1478 	p. 370: Commands for scanner devices (not previously listed)
1479 	GET DATA BUFFER STATUS       34
1480 	GET WINDOW                   25
1481 	OBJECT POSITION              31
1482 	SCAN                         1b
1483 	SET WINDOW                   24 DATA OUT
1484 
1485 	p. 391: Commands for optical memory devices (not listed)
1486 	ERASE (10)                   2c
1487 	ERASE (12)                   ac
1488 	MEDIUM SCAN                  38 DATA OUT
1489 	READ DEFECT DATA (12)        b7
1490 	READ GENERATION              29
1491 	READ UPDATED BLOCK           2d
1492 	UPDATE BLOCK                 3d DATA OUT
1493 
1494 	p. 419: Commands for medium changer devices (not listed)
1495 	EXCHANGE MEDIUM              46
1496 	INITIALIZE ELEMENT STATUS    07
1497 	MOVE MEDIUM                  a5
1498 	POSITION TO ELEMENT          2b
1499 	READ ELEMENT STATUS          b8
1500 	REQUEST VOL. ELEMENT ADDRESS b5
1501 	SEND VOLUME TAG              b6 DATA OUT
1502 
1503 	p. 454: Commands for communications devices (not listed previously)
1504 	GET MESSAGE (6)              08
1505 	GET MESSAGE (10)             28
1506 	GET MESSAGE (12)             a8
1507       */
1508 
1509       switch (current_SC->cmnd[0]) {
1510       case CHANGE_DEFINITION: case COMPARE:         case COPY:
1511       case COPY_VERIFY:       case LOG_SELECT:      case MODE_SELECT:
1512       case MODE_SELECT_10:    case SEND_DIAGNOSTIC: case WRITE_BUFFER:
1513 
1514       case FORMAT_UNIT:       case REASSIGN_BLOCKS: case RESERVE:
1515       case SEARCH_EQUAL:      case SEARCH_HIGH:     case SEARCH_LOW:
1516       case WRITE_6:           case WRITE_10:        case WRITE_VERIFY:
1517       case 0x3f:              case 0x41:
1518 
1519       case 0xb1:              case 0xb0:            case 0xb2:
1520       case 0xaa:              case 0xae:
1521 
1522       case 0x24:
1523 
1524       case 0x38:              case 0x3d:
1525 
1526       case 0xb6:
1527 
1528       case 0xea:		/* alternate number for WRITE LONG */
1529 
1530 	 current_SC->SCp.have_data_in = -1;
1531 	 outb( 0xd0 | PARITY_MASK, TMC_Cntl_port );
1532 	 break;
1533 
1534       case 0x00:
1535       default:
1536 
1537 	 current_SC->SCp.have_data_in = 1;
1538 	 outb( 0x90 | PARITY_MASK, TMC_Cntl_port );
1539 	 break;
1540       }
1541    }
1542 
1543    if (current_SC->SCp.have_data_in == -1) { /* DATA OUT */
1544       while ( (data_count = FIFO_Size - inw( FIFO_Data_Count_port )) > 512 ) {
1545 #if EVERY_ACCESS
1546 	 printk( "DC=%d, ", data_count ) ;
1547 #endif
1548 	 if (data_count > current_SC->SCp.this_residual)
1549 	       data_count = current_SC->SCp.this_residual;
1550 	 if (data_count > 0) {
1551 #if EVERY_ACCESS
1552 	    printk( "%d OUT, ", data_count );
1553 #endif
1554 	    if (data_count == 1) {
1555 	       outb( *current_SC->SCp.ptr++, Write_FIFO_port );
1556 	       --current_SC->SCp.this_residual;
1557 	    } else {
1558 	       data_count >>= 1;
1559 	       outsw( Write_FIFO_port, current_SC->SCp.ptr, data_count );
1560 	       current_SC->SCp.ptr += 2 * data_count;
1561 	       current_SC->SCp.this_residual -= 2 * data_count;
1562 	    }
1563 	 }
1564 	 if (!current_SC->SCp.this_residual) {
1565 	    if (current_SC->SCp.buffers_residual) {
1566 	       --current_SC->SCp.buffers_residual;
1567 	       ++current_SC->SCp.buffer;
1568 	       current_SC->SCp.ptr = current_SC->SCp.buffer->address;
1569 	       current_SC->SCp.this_residual = current_SC->SCp.buffer->length;
1570 	    } else
1571 		  break;
1572 	 }
1573       }
1574    }
1575 
1576    if (current_SC->SCp.have_data_in == 1) { /* DATA IN */
1577       while ((data_count = inw( FIFO_Data_Count_port )) > 0) {
1578 #if EVERY_ACCESS
1579 	 printk( "DC=%d, ", data_count );
1580 #endif
1581 	 if (data_count > current_SC->SCp.this_residual)
1582 	       data_count = current_SC->SCp.this_residual;
1583 	 if (data_count) {
1584 #if EVERY_ACCESS
1585 	    printk( "%d IN, ", data_count );
1586 #endif
1587 	    if (data_count == 1) {
1588 	       *current_SC->SCp.ptr++ = inb( Read_FIFO_port );
1589 	       --current_SC->SCp.this_residual;
1590 	    } else {
1591 	       data_count >>= 1; /* Number of words */
1592 	       insw( Read_FIFO_port, current_SC->SCp.ptr, data_count );
1593 	       current_SC->SCp.ptr += 2 * data_count;
1594 	       current_SC->SCp.this_residual -= 2 * data_count;
1595 	    }
1596 	 }
1597 	 if (!current_SC->SCp.this_residual
1598 	     && current_SC->SCp.buffers_residual) {
1599 	    --current_SC->SCp.buffers_residual;
1600 	    ++current_SC->SCp.buffer;
1601 	    current_SC->SCp.ptr = current_SC->SCp.buffer->address;
1602 	    current_SC->SCp.this_residual = current_SC->SCp.buffer->length;
1603 	 }
1604       }
1605    }
1606 
1607    if (done) {
1608 #if EVERY_ACCESS
1609       printk( " ** IN DONE %d ** ", current_SC->SCp.have_data_in );
1610 #endif
1611 
1612 #if ERRORS_ONLY
1613       if (current_SC->cmnd[0] == REQUEST_SENSE && !current_SC->SCp.Status) {
1614 	 if ((unsigned char)(*((char *)current_SC->request_buffer+2)) & 0x0f) {
1615 	    unsigned char key;
1616 	    unsigned char code;
1617 	    unsigned char qualifier;
1618 
1619 	    key = (unsigned char)(*((char *)current_SC->request_buffer + 2))
1620 		  & 0x0f;
1621 	    code = (unsigned char)(*((char *)current_SC->request_buffer + 12));
1622 	    qualifier = (unsigned char)(*((char *)current_SC->request_buffer
1623 					  + 13));
1624 
1625 	    if (key != UNIT_ATTENTION
1626 		&& !(key == NOT_READY
1627 		     && code == 0x04
1628 		     && (!qualifier || qualifier == 0x02 || qualifier == 0x01))
1629 		&& !(key == ILLEGAL_REQUEST && (code == 0x25
1630 						|| code == 0x24
1631 						|| !code)))
1632 
1633 		  printk( "scsi: <fdomain> REQUEST SENSE"
1634 			  " Key = %x, Code = %x, Qualifier = %x\n",
1635 			  key, code, qualifier );
1636 	 }
1637       }
1638 #endif
1639 #if EVERY_ACCESS
1640       printk( "BEFORE MY_DONE. . ." );
1641 #endif
1642       spin_lock_irqsave(&io_request_lock, flags);
1643       my_done( (current_SC->SCp.Status & 0xff)
1644 	       | ((current_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16) );
1645       spin_unlock_irqrestore(&io_request_lock, flags);
1646 #if EVERY_ACCESS
1647       printk( "RETURNING.\n" );
1648 #endif
1649 
1650    } else {
1651       if (current_SC->SCp.phase & disconnect) {
1652 	 outb( 0xd0 | FIFO_COUNT, Interrupt_Cntl_port );
1653 	 outb( 0x00, SCSI_Cntl_port );
1654       } else {
1655 	 outb( 0x90 | FIFO_COUNT, Interrupt_Cntl_port );
1656       }
1657    }
1658 #if DEBUG_RACE
1659    in_interrupt_flag = 0;
1660 #endif
1661    return;
1662 }
1663 
fdomain_16x0_queue(Scsi_Cmnd * SCpnt,void (* done)(Scsi_Cmnd *))1664 int fdomain_16x0_queue( Scsi_Cmnd * SCpnt, void (*done)(Scsi_Cmnd *))
1665 {
1666    if (in_command) {
1667       panic( "scsi: <fdomain> fdomain_16x0_queue() NOT REENTRANT!\n" );
1668    }
1669 #if EVERY_ACCESS
1670    printk( "queue: target = %d cmnd = 0x%02x pieces = %d size = %u\n",
1671 	   SCpnt->target,
1672 	   *(unsigned char *)SCpnt->cmnd,
1673 	   SCpnt->use_sg,
1674 	   SCpnt->request_bufflen );
1675 #endif
1676 
1677    fdomain_make_bus_idle();
1678 
1679    current_SC            = SCpnt; /* Save this for the done function */
1680    current_SC->scsi_done = done;
1681 
1682    /* Initialize static data */
1683 
1684    if (current_SC->use_sg) {
1685       current_SC->SCp.buffer =
1686 	    (struct scatterlist *)current_SC->request_buffer;
1687       current_SC->SCp.ptr              = current_SC->SCp.buffer->address;
1688       current_SC->SCp.this_residual    = current_SC->SCp.buffer->length;
1689       current_SC->SCp.buffers_residual = current_SC->use_sg - 1;
1690    } else {
1691       current_SC->SCp.ptr              = (char *)current_SC->request_buffer;
1692       current_SC->SCp.this_residual    = current_SC->request_bufflen;
1693       current_SC->SCp.buffer           = NULL;
1694       current_SC->SCp.buffers_residual = 0;
1695    }
1696 
1697 
1698    current_SC->SCp.Status              = 0;
1699    current_SC->SCp.Message             = 0;
1700    current_SC->SCp.have_data_in        = 0;
1701    current_SC->SCp.sent_command        = 0;
1702    current_SC->SCp.phase               = in_arbitration;
1703 
1704    /* Start arbitration */
1705    outb( 0x00, Interrupt_Cntl_port );
1706    outb( 0x00, SCSI_Cntl_port );              /* Disable data drivers */
1707    outb( adapter_mask, SCSI_Data_NoACK_port ); /* Set our id bit */
1708    ++in_command;
1709    outb( 0x20, Interrupt_Cntl_port );
1710    outb( 0x14 | PARITY_MASK, TMC_Cntl_port ); /* Start arbitration */
1711 
1712    return 0;
1713 }
1714 
1715 /* The following code, which simulates the old-style command function, was
1716    taken from Tommy Thorn's aha1542.c file.  This code is Copyright (C)
1717    1992 Tommy Thorn. */
1718 
1719 static volatile int internal_done_flag    = 0;
1720 static volatile int internal_done_errcode = 0;
1721 
internal_done(Scsi_Cmnd * SCpnt)1722 static void internal_done( Scsi_Cmnd *SCpnt )
1723 {
1724     internal_done_errcode = SCpnt->result;
1725     ++internal_done_flag;
1726 }
1727 
fdomain_16x0_command(Scsi_Cmnd * SCpnt)1728 int fdomain_16x0_command( Scsi_Cmnd *SCpnt )
1729 {
1730     fdomain_16x0_queue( SCpnt, internal_done );
1731 
1732     while (!internal_done_flag)
1733 	  ;
1734     internal_done_flag = 0;
1735     return internal_done_errcode;
1736 }
1737 
1738 /* End of code derived from Tommy Thorn's work. */
1739 
print_info(Scsi_Cmnd * SCpnt)1740 void print_info( Scsi_Cmnd *SCpnt )
1741 {
1742    unsigned int imr;
1743    unsigned int irr;
1744    unsigned int isr;
1745 
1746    if (!SCpnt || !SCpnt->host) {
1747       printk( "scsi: <fdomain> Cannot provide detailed information\n" );
1748       return;
1749    }
1750 
1751    printk( "%s\n", fdomain_16x0_info( SCpnt->host ) );
1752    print_banner( SCpnt->host );
1753    switch (SCpnt->SCp.phase) {
1754    case in_arbitration: printk( "arbitration " ); break;
1755    case in_selection:   printk( "selection " );   break;
1756    case in_other:       printk( "other " );       break;
1757    default:             printk( "unknown " );     break;
1758    }
1759 
1760    printk( "(%d), target = %d cmnd = 0x%02x pieces = %d size = %u\n",
1761 	   SCpnt->SCp.phase,
1762 	   SCpnt->target,
1763 	   *(unsigned char *)SCpnt->cmnd,
1764 	   SCpnt->use_sg,
1765 	   SCpnt->request_bufflen );
1766    printk( "sent_command = %d, have_data_in = %d, timeout = %d\n",
1767 	   SCpnt->SCp.sent_command,
1768 	   SCpnt->SCp.have_data_in,
1769 	   SCpnt->timeout );
1770 #if DEBUG_RACE
1771    printk( "in_interrupt_flag = %d\n", in_interrupt_flag );
1772 #endif
1773 
1774    imr = (inb( 0x0a1 ) << 8) + inb( 0x21 );
1775    outb( 0x0a, 0xa0 );
1776    irr = inb( 0xa0 ) << 8;
1777    outb( 0x0a, 0x20 );
1778    irr += inb( 0x20 );
1779    outb( 0x0b, 0xa0 );
1780    isr = inb( 0xa0 ) << 8;
1781    outb( 0x0b, 0x20 );
1782    isr += inb( 0x20 );
1783 
1784 				/* Print out interesting information */
1785    printk( "IMR = 0x%04x", imr );
1786    if (imr & (1 << interrupt_level))
1787 	 printk( " (masked)" );
1788    printk( ", IRR = 0x%04x, ISR = 0x%04x\n", irr, isr );
1789 
1790    printk( "SCSI Status      = 0x%02x\n", inb( SCSI_Status_port ) );
1791    printk( "TMC Status       = 0x%02x", inb( TMC_Status_port ) );
1792    if (inb( TMC_Status_port & 1))
1793 	 printk( " (interrupt)" );
1794    printk( "\n" );
1795    printk( "Interrupt Status = 0x%02x", inb( Interrupt_Status_port ) );
1796    if (inb( Interrupt_Status_port ) & 0x08)
1797 	 printk( " (enabled)" );
1798    printk( "\n" );
1799    if (chip == tmc18c50 || chip == tmc18c30) {
1800       printk( "FIFO Status      = 0x%02x\n", inb( port_base + FIFO_Status ) );
1801       printk( "Int. Condition   = 0x%02x\n",
1802 	      inb( port_base + Interrupt_Cond ) );
1803    }
1804    printk( "Configuration 1  = 0x%02x\n", inb( port_base + Configuration1 ) );
1805    if (chip == tmc18c50 || chip == tmc18c30)
1806 	 printk( "Configuration 2  = 0x%02x\n",
1807 		 inb( port_base + Configuration2 ) );
1808 }
1809 
fdomain_16x0_abort(Scsi_Cmnd * SCpnt)1810 int fdomain_16x0_abort( Scsi_Cmnd *SCpnt)
1811 {
1812    unsigned long flags;
1813 #if EVERY_ACCESS || ERRORS_ONLY || DEBUG_ABORT
1814    printk( "scsi: <fdomain> abort " );
1815 #endif
1816 
1817    save_flags( flags );
1818    cli();
1819    if (!in_command) {
1820 #if EVERY_ACCESS || ERRORS_ONLY
1821       printk( " (not in command)\n" );
1822 #endif
1823       restore_flags( flags );
1824       return SCSI_ABORT_NOT_RUNNING;
1825    } else printk( "\n" );
1826 
1827 #if DEBUG_ABORT
1828    print_info( SCpnt );
1829 #endif
1830 
1831    fdomain_make_bus_idle();
1832 
1833    current_SC->SCp.phase |= aborted;
1834 
1835    current_SC->result = DID_ABORT << 16;
1836 
1837    restore_flags( flags );
1838 
1839    /* Aborts are not done well. . . */
1840    my_done( DID_ABORT << 16 );
1841 
1842    return SCSI_ABORT_SUCCESS;
1843 }
1844 
fdomain_16x0_reset(Scsi_Cmnd * SCpnt,unsigned int ignored)1845 int fdomain_16x0_reset( Scsi_Cmnd *SCpnt, unsigned int ignored )
1846 {
1847 #if DEBUG_RESET
1848    static int called_once = 0;
1849 #endif
1850 
1851 #if ERRORS_ONLY
1852    if (SCpnt) printk( "scsi: <fdomain> SCSI Bus Reset\n" );
1853 #endif
1854 
1855 #if DEBUG_RESET
1856    if (called_once) print_info( current_SC );
1857    called_once = 1;
1858 #endif
1859 
1860    outb( 1, SCSI_Cntl_port );
1861    do_pause( 2 );
1862    outb( 0, SCSI_Cntl_port );
1863    do_pause( 115 );
1864    outb( 0, SCSI_Mode_Cntl_port );
1865    outb( PARITY_MASK, TMC_Cntl_port );
1866 
1867    /* Unless this is the very first call (i.e., SCPnt == NULL), everything
1868       is probably hosed at this point.  We will, however, try to keep
1869       things going by informing the high-level code that we need help. */
1870 
1871    return SCSI_RESET_WAKEUP;
1872 }
1873 
1874 #include "sd.h"
1875 #include <scsi/scsi_ioctl.h>
1876 
fdomain_16x0_biosparam(Scsi_Disk * disk,kdev_t dev,int * info_array)1877 int fdomain_16x0_biosparam( Scsi_Disk *disk, kdev_t dev, int *info_array )
1878 {
1879    int              drive;
1880    unsigned char    buf[512 + sizeof (Scsi_Ioctl_Command)];
1881    Scsi_Ioctl_Command *sic = (Scsi_Ioctl_Command *) buf;
1882    int		    size      = disk->capacity;
1883    unsigned char    *data     = sic->data;
1884    unsigned char    do_read[] = { READ_6, 0, 0, 0, 1, 0 };
1885    int              retcode;
1886    unsigned long    offset;
1887    struct drive_info {
1888       unsigned short cylinders;
1889       unsigned char  heads;
1890       unsigned char  sectors;
1891    } i;
1892 
1893    /* NOTES:
1894       The RAM area starts at 0x1f00 from the bios_base address.
1895 
1896       For BIOS Version 2.0:
1897 
1898       The drive parameter table seems to start at 0x1f30.
1899       The first byte's purpose is not known.
1900       Next is the cylinder, head, and sector information.
1901       The last 4 bytes appear to be the drive's size in sectors.
1902       The other bytes in the drive parameter table are unknown.
1903       If anyone figures them out, please send me mail, and I will
1904       update these notes.
1905 
1906       Tape drives do not get placed in this table.
1907 
1908       There is another table at 0x1fea:
1909       If the byte is 0x01, then the SCSI ID is not in use.
1910       If the byte is 0x18 or 0x48, then the SCSI ID is in use,
1911       although tapes don't seem to be in this table.  I haven't
1912       seen any other numbers (in a limited sample).
1913 
1914       0x1f2d is a drive count (i.e., not including tapes)
1915 
1916       The table at 0x1fcc are I/O ports addresses for the various
1917       operations.  I calculate these by hand in this driver code.
1918 
1919 
1920 
1921       For the ISA-200S version of BIOS Version 2.0:
1922 
1923       The drive parameter table starts at 0x1f33.
1924 
1925       WARNING: Assume that the table entry is 25 bytes long.  Someone needs
1926       to check this for the Quantum ISA-200S card.
1927 
1928 
1929 
1930       For BIOS Version 3.2:
1931 
1932       The drive parameter table starts at 0x1f70.  Each entry is
1933       0x0a bytes long.  Heads are one less than we need to report.
1934     */
1935 
1936    if (MAJOR(dev) != SCSI_DISK0_MAJOR) {
1937       printk("scsi: <fdomain> fdomain_16x0_biosparam: too many disks");
1938       return 0;
1939    }
1940    drive = MINOR(dev) >> 4;
1941 
1942    if (bios_major == 2) {
1943       switch (Quantum) {
1944       case 2:			/* ISA_200S */
1945 				/* The value of 25 has never been verified.
1946 				   It should probably be 15. */
1947 	 offset = bios_base + 0x1f33 + drive * 25;
1948 	 break;
1949       case 3:			/* ISA_250MG */
1950 	 offset = bios_base + 0x1f36 + drive * 15;
1951 	 break;
1952       case 4:			/* ISA_200S (another one) */
1953 	 offset = bios_base + 0x1f34 + drive * 15;
1954 	 break;
1955       default:
1956 	 offset = bios_base + 0x1f31 + drive * 25;
1957 	 break;
1958       }
1959       isa_memcpy_fromio( &i, offset, sizeof( struct drive_info ) );
1960       info_array[0] = i.heads;
1961       info_array[1] = i.sectors;
1962       info_array[2] = i.cylinders;
1963    } else if (bios_major == 3
1964 	      && bios_minor >= 0
1965 	      && bios_minor < 4) { /* 3.0 and 3.2 BIOS */
1966       memcpy_fromio( &i, bios_base + 0x1f71 + drive * 10,
1967 		     sizeof( struct drive_info ) );
1968       info_array[0] = i.heads + 1;
1969       info_array[1] = i.sectors;
1970       info_array[2] = i.cylinders;
1971    } else {			/* 3.4 BIOS (and up?) */
1972       /* This algorithm was provided by Future Domain (much thanks!). */
1973 
1974       sic->inlen  = 0;		/* zero bytes out */
1975       sic->outlen = 512;		/* one sector in */
1976       memcpy( data, do_read, sizeof( do_read ) );
1977       retcode = kernel_scsi_ioctl( disk->device,
1978 				   SCSI_IOCTL_SEND_COMMAND,
1979 				   sic );
1980       if (!retcode				    /* SCSI command ok */
1981 	  && data[511] == 0xaa && data[510] == 0x55 /* Partition table valid */
1982 	  && data[0x1c2]) {			    /* Partition type */
1983 
1984 	 /* The partition table layout is as follows:
1985 
1986 	    Start: 0x1b3h
1987 	    Offset: 0 = partition status
1988 		    1 = starting head
1989 		    2 = starting sector and cylinder (word, encoded)
1990 		    4 = partition type
1991 		    5 = ending head
1992 		    6 = ending sector and cylinder (word, encoded)
1993 		    8 = starting absolute sector (double word)
1994 		    c = number of sectors (double word)
1995 	    Signature: 0x1fe = 0x55aa
1996 
1997 	    So, this algorithm assumes:
1998 	    1) the first partition table is in use,
1999 	    2) the data in the first entry is correct, and
2000 	    3) partitions never divide cylinders
2001 
2002 	    Note that (1) may be FALSE for NetBSD (and other BSD flavors),
2003 	    as well as for Linux.  Note also, that Linux doesn't pay any
2004 	    attention to the fields that are used by this algorithm -- it
2005 	    only uses the absolute sector data.  Recent versions of Linux's
2006 	    fdisk(1) will fill this data in correctly, and forthcoming
2007 	    versions will check for consistency.
2008 
2009 	    Checking for a non-zero partition type is not part of the
2010 	    Future Domain algorithm, but it seemed to be a reasonable thing
2011 	    to do, especially in the Linux and BSD worlds. */
2012 
2013 	 info_array[0] = data[0x1c3] + 1;	    /* heads */
2014 	 info_array[1] = data[0x1c4] & 0x3f;	    /* sectors */
2015       } else {
2016 
2017  	 /* Note that this new method guarantees that there will always be
2018 	    less than 1024 cylinders on a platter.  This is good for drives
2019 	    up to approximately 7.85GB (where 1GB = 1024 * 1024 kB). */
2020 
2021 	 if ((unsigned int)size >= 0x7e0000U) {
2022 	    info_array[0] = 0xff; /* heads   = 255 */
2023 	    info_array[1] = 0x3f; /* sectors =  63 */
2024 	 } else if ((unsigned int)size >= 0x200000U) {
2025 	    info_array[0] = 0x80; /* heads   = 128 */
2026 	    info_array[1] = 0x3f; /* sectors =  63 */
2027 	 } else {
2028 	    info_array[0] = 0x40; /* heads   =  64 */
2029 	    info_array[1] = 0x20; /* sectors =  32 */
2030 	 }
2031       }
2032 				/* For both methods, compute the cylinders */
2033       info_array[2] = (unsigned int)size / (info_array[0] * info_array[1] );
2034    }
2035 
2036    return 0;
2037 }
2038 
fdomain_16x0_release(struct Scsi_Host * shpnt)2039 int fdomain_16x0_release(struct Scsi_Host *shpnt)
2040 {
2041 	if (shpnt->irq)
2042 		free_irq(shpnt->irq, shpnt);
2043 	if (shpnt->io_port && shpnt->n_io_port)
2044 		release_region(shpnt->io_port, shpnt->n_io_port);
2045 	return 0;
2046 }
2047 
2048 MODULE_LICENSE("GPL");
2049 
2050 #ifndef PCMCIA
2051 /* Eventually this will go into an include file, but this will be later */
2052 static Scsi_Host_Template driver_template = FDOMAIN_16X0;
2053 
2054 #include "scsi_module.c"
2055 #endif
2056