VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / scsi / fd_mcs.c
1 /* fd_mcs.c -- Future Domain MCS 600/700 (or IBM OEM) driver
2  *
3  * FutureDomain MCS-600/700 v0.2 03/11/1998 by ZP Gu (zpg@castle.net)
4  *
5  * This driver is cloned from fdomain.* to specifically support
6  * the Future Domain MCS 600/700 MCA SCSI adapters. Some PS/2s
7  * also equipped with IBM Fast SCSI Adapter/A which is an OEM
8  * of MCS 700.
9  *
10  * This driver also supports Reply SB16/SCSI card (the SCSI part).
11  *
12  * What makes this driver different is that this driver is MCA only
13  * and it supports multiple adapters in the same system, IRQ 
14  * sharing, some driver statistics, and maps highest SCSI id to sda.
15  * All cards are auto-detected.
16  *
17  * Assumptions: TMC-1800/18C50/18C30, BIOS >= 3.4
18  *
19  * LILO command-line options:
20  *   fd_mcs=<FIFO_COUNT>[,<FIFO_SIZE>]
21  *
22  * ********************************************************
23  * Please see Copyrights/Comments in fdomain.* for credits.
24  * Following is from fdomain.c for acknowledgement:
25  *
26  * Created: Sun May  3 18:53:19 1992 by faith@cs.unc.edu
27  * Revised: Wed Oct  2 11:10:55 1996 by r.faith@ieee.org
28  * Author: Rickard E. Faith, faith@cs.unc.edu
29  * Copyright 1992, 1993, 1994, 1995, 1996 Rickard E. Faith
30  *
31  * $Id: fdomain.c,v 5.45 1996/10/02 15:13:06 root Exp $
32
33  * This program is free software; you can redistribute it and/or modify it
34  * under the terms of the GNU General Public License as published by the
35  * Free Software Foundation; either version 2, or (at your option) any
36  * later version.
37
38  * This program is distributed in the hope that it will be useful, but
39  * WITHOUT ANY WARRANTY; without even the implied warranty of
40  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
41  * General Public License for more details.
42
43  * You should have received a copy of the GNU General Public License along
44  * with this program; if not, write to the Free Software Foundation, Inc.,
45  * 675 Mass Ave, Cambridge, MA 02139, USA.
46
47  **************************************************************************
48
49  NOTES ON USER DEFINABLE OPTIONS:
50
51  DEBUG: This turns on the printing of various debug information.
52
53  ENABLE_PARITY: This turns on SCSI parity checking.  With the current
54  driver, all attached devices must support SCSI parity.  If none of your
55  devices support parity, then you can probably get the driver to work by
56  turning this option off.  I have no way of testing this, however, and it
57  would appear that no one ever uses this option.
58
59  FIFO_COUNT: The host adapter has an 8K cache (host adapters based on the
60  18C30 chip have a 2k cache).  When this many 512 byte blocks are filled by
61  the SCSI device, an interrupt will be raised.  Therefore, this could be as
62  low as 0, or as high as 16.  Note, however, that values which are too high
63  or too low seem to prevent any interrupts from occurring, and thereby lock
64  up the machine.  I have found that 2 is a good number, but throughput may
65  be increased by changing this value to values which are close to 2.
66  Please let me know if you try any different values.
67  [*****Now a runtime option*****]
68
69  RESELECTION: This is no longer an option, since I gave up trying to
70  implement it in version 4.x of this driver.  It did not improve
71  performance at all and made the driver unstable (because I never found one
72  of the two race conditions which were introduced by the multiple
73  outstanding command code).  The instability seems a very high price to pay
74  just so that you don't have to wait for the tape to rewind.  If you want
75  this feature implemented, send me patches.  I'll be happy to send a copy
76  of my (broken) driver to anyone who would like to see a copy.
77
78  **************************************************************************/
79
80 #include <linux/module.h>
81 #include <linux/interrupt.h>
82 #include <linux/blkdev.h>
83 #include <linux/errno.h>
84 #include <linux/string.h>
85 #include <linux/ioport.h>
86 #include <linux/proc_fs.h>
87 #include <linux/delay.h>
88 #include <linux/mca.h>
89 #include <linux/spinlock.h>
90 #include <scsi/scsicam.h>
91 #include <linux/mca-legacy.h>
92
93 #include <asm/io.h>
94 #include <asm/system.h>
95
96 #include "scsi.h"
97 #include <scsi/scsi_host.h>
98 #include "fd_mcs.h"
99
100 #define DRIVER_VERSION "v0.2 by ZP Gu<zpg@castle.net>"
101
102 /* START OF USER DEFINABLE OPTIONS */
103
104 #define DEBUG            0      /* Enable debugging output */
105 #define ENABLE_PARITY    1      /* Enable SCSI Parity */
106 #define DO_DETECT        0      /* Do device detection here (see scsi.c) */
107
108 /* END OF USER DEFINABLE OPTIONS */
109
110 #if DEBUG
111 #define EVERY_ACCESS     0      /* Write a line on every scsi access */
112 #define ERRORS_ONLY      1      /* Only write a line if there is an error */
113 #define DEBUG_DETECT     1      /* Debug fd_mcs_detect() */
114 #define DEBUG_MESSAGES   1      /* Debug MESSAGE IN phase */
115 #define DEBUG_ABORT      1      /* Debug abort() routine */
116 #define DEBUG_RESET      1      /* Debug reset() routine */
117 #define DEBUG_RACE       1      /* Debug interrupt-driven race condition */
118 #else
119 #define EVERY_ACCESS     0      /* LEAVE THESE ALONE--CHANGE THE ONES ABOVE */
120 #define ERRORS_ONLY      0
121 #define DEBUG_DETECT     0
122 #define DEBUG_MESSAGES   0
123 #define DEBUG_ABORT      0
124 #define DEBUG_RESET      0
125 #define DEBUG_RACE       0
126 #endif
127
128 /* Errors are reported on the line, so we don't need to report them again */
129 #if EVERY_ACCESS
130 #undef ERRORS_ONLY
131 #define ERRORS_ONLY      0
132 #endif
133
134 #if ENABLE_PARITY
135 #define PARITY_MASK      0x08
136 #else
137 #define PARITY_MASK      0x00
138 #endif
139
140 enum chip_type {
141         unknown = 0x00,
142         tmc1800 = 0x01,
143         tmc18c50 = 0x02,
144         tmc18c30 = 0x03,
145 };
146
147 enum {
148         in_arbitration = 0x02,
149         in_selection = 0x04,
150         in_other = 0x08,
151         disconnect = 0x10,
152         aborted = 0x20,
153         sent_ident = 0x40,
154 };
155
156 enum in_port_type {
157         Read_SCSI_Data = 0,
158         SCSI_Status = 1,
159         TMC_Status = 2,
160         FIFO_Status = 3,        /* tmc18c50/tmc18c30 only */
161         Interrupt_Cond = 4,     /* tmc18c50/tmc18c30 only */
162         LSB_ID_Code = 5,
163         MSB_ID_Code = 6,
164         Read_Loopback = 7,
165         SCSI_Data_NoACK = 8,
166         Interrupt_Status = 9,
167         Configuration1 = 10,
168         Configuration2 = 11,    /* tmc18c50/tmc18c30 only */
169         Read_FIFO = 12,
170         FIFO_Data_Count = 14
171 };
172
173 enum out_port_type {
174         Write_SCSI_Data = 0,
175         SCSI_Cntl = 1,
176         Interrupt_Cntl = 2,
177         SCSI_Mode_Cntl = 3,
178         TMC_Cntl = 4,
179         Memory_Cntl = 5,        /* tmc18c50/tmc18c30 only */
180         Write_Loopback = 7,
181         IO_Control = 11,        /* tmc18c30 only */
182         Write_FIFO = 12
183 };
184
185 struct fd_hostdata {
186         unsigned long _bios_base;
187         int _bios_major;
188         int _bios_minor;
189         volatile int _in_command;
190         Scsi_Cmnd *_current_SC;
191         enum chip_type _chip;
192         int _adapter_mask;
193         int _fifo_count;        /* Number of 512 byte blocks before INTR */
194
195         char _adapter_name[64];
196 #if DEBUG_RACE
197         volatile int _in_interrupt_flag;
198 #endif
199
200         int _SCSI_Mode_Cntl_port;
201         int _FIFO_Data_Count_port;
202         int _Interrupt_Cntl_port;
203         int _Interrupt_Status_port;
204         int _Interrupt_Cond_port;
205         int _Read_FIFO_port;
206         int _Read_SCSI_Data_port;
207         int _SCSI_Cntl_port;
208         int _SCSI_Data_NoACK_port;
209         int _SCSI_Status_port;
210         int _TMC_Cntl_port;
211         int _TMC_Status_port;
212         int _Write_FIFO_port;
213         int _Write_SCSI_Data_port;
214
215         int _FIFO_Size;         /* = 0x2000;  8k FIFO for
216                                    pre-tmc18c30 chips */
217         /* simple stats */
218         int _Bytes_Read;
219         int _Bytes_Written;
220         int _INTR_Processed;
221 };
222
223 #define FD_MAX_HOSTS 3          /* enough? */
224
225 #define HOSTDATA(shpnt) ((struct fd_hostdata *) shpnt->hostdata)
226 #define bios_base             (HOSTDATA(shpnt)->_bios_base)
227 #define bios_major            (HOSTDATA(shpnt)->_bios_major)
228 #define bios_minor            (HOSTDATA(shpnt)->_bios_minor)
229 #define in_command            (HOSTDATA(shpnt)->_in_command)
230 #define current_SC            (HOSTDATA(shpnt)->_current_SC)
231 #define chip                  (HOSTDATA(shpnt)->_chip)
232 #define adapter_mask          (HOSTDATA(shpnt)->_adapter_mask)
233 #define FIFO_COUNT            (HOSTDATA(shpnt)->_fifo_count)
234 #define adapter_name          (HOSTDATA(shpnt)->_adapter_name)
235 #if DEBUG_RACE
236 #define in_interrupt_flag     (HOSTDATA(shpnt)->_in_interrupt_flag)
237 #endif
238 #define SCSI_Mode_Cntl_port   (HOSTDATA(shpnt)->_SCSI_Mode_Cntl_port)
239 #define FIFO_Data_Count_port  (HOSTDATA(shpnt)->_FIFO_Data_Count_port)
240 #define Interrupt_Cntl_port   (HOSTDATA(shpnt)->_Interrupt_Cntl_port)
241 #define Interrupt_Status_port (HOSTDATA(shpnt)->_Interrupt_Status_port)
242 #define Interrupt_Cond_port   (HOSTDATA(shpnt)->_Interrupt_Cond_port)
243 #define Read_FIFO_port        (HOSTDATA(shpnt)->_Read_FIFO_port)
244 #define Read_SCSI_Data_port   (HOSTDATA(shpnt)->_Read_SCSI_Data_port)
245 #define SCSI_Cntl_port        (HOSTDATA(shpnt)->_SCSI_Cntl_port)
246 #define SCSI_Data_NoACK_port  (HOSTDATA(shpnt)->_SCSI_Data_NoACK_port)
247 #define SCSI_Status_port      (HOSTDATA(shpnt)->_SCSI_Status_port)
248 #define TMC_Cntl_port         (HOSTDATA(shpnt)->_TMC_Cntl_port)
249 #define TMC_Status_port       (HOSTDATA(shpnt)->_TMC_Status_port)
250 #define Write_FIFO_port       (HOSTDATA(shpnt)->_Write_FIFO_port)
251 #define Write_SCSI_Data_port  (HOSTDATA(shpnt)->_Write_SCSI_Data_port)
252 #define FIFO_Size             (HOSTDATA(shpnt)->_FIFO_Size)
253 #define Bytes_Read            (HOSTDATA(shpnt)->_Bytes_Read)
254 #define Bytes_Written         (HOSTDATA(shpnt)->_Bytes_Written)
255 #define INTR_Processed        (HOSTDATA(shpnt)->_INTR_Processed)
256
257 struct fd_mcs_adapters_struct {
258         char *name;
259         int id;
260         enum chip_type fd_chip;
261         int fifo_size;
262         int fifo_count;
263 };
264
265 #define REPLY_ID 0x5137
266
267 static struct fd_mcs_adapters_struct fd_mcs_adapters[] = {
268         {"Future Domain SCSI Adapter MCS-700(18C50)",
269          0x60e9,
270          tmc18c50,
271          0x2000,
272          4},
273         {"Future Domain SCSI Adapter MCS-600/700(TMC-1800)",
274          0x6127,
275          tmc1800,
276          0x2000,
277          4},
278         {"Reply Sound Blaster/SCSI Adapter",
279          REPLY_ID,
280          tmc18c30,
281          0x800,
282          2},
283 };
284
285 #define FD_BRDS sizeof(fd_mcs_adapters)/sizeof(struct fd_mcs_adapters_struct)
286
287 static irqreturn_t fd_mcs_intr(int irq, void *dev_id, struct pt_regs *regs);
288
289 static unsigned long addresses[] = { 0xc8000, 0xca000, 0xce000, 0xde000 };
290 static unsigned short ports[] = { 0x140, 0x150, 0x160, 0x170 };
291 static unsigned short ints[] = { 3, 5, 10, 11, 12, 14, 15, 0 };
292
293 /* host information */
294 static int found = 0;
295 static struct Scsi_Host *hosts[FD_MAX_HOSTS + 1] = { NULL };
296
297 static int user_fifo_count = 0;
298 static int user_fifo_size = 0;
299
300 static void fd_mcs_setup(char *str, int *ints)
301 {
302         static int done_setup = 0;
303
304         if (done_setup++ || ints[0] < 1 || ints[0] > 2 || ints[1] < 1 || ints[1] > 16) {
305                 printk("fd_mcs: usage: fd_mcs=FIFO_COUNT, FIFO_SIZE\n");
306         }
307
308         user_fifo_count = ints[0] >= 1 ? ints[1] : 0;
309         user_fifo_size = ints[0] >= 2 ? ints[2] : 0;
310 }
311
312 __setup("fd_mcs=", fd_mcs_setup);
313
314 static void print_banner(struct Scsi_Host *shpnt)
315 {
316         printk("scsi%d <fd_mcs>: ", shpnt->host_no);
317
318         if (bios_base) {
319                 printk("BIOS at 0x%lX", bios_base);
320         } else {
321                 printk("No BIOS");
322         }
323
324         printk(", HostID %d, %s Chip, IRQ %d, IO 0x%lX\n", shpnt->this_id, chip == tmc18c50 ? "TMC-18C50" : (chip == tmc18c30 ? "TMC-18C30" : (chip == tmc1800 ? "TMC-1800" : "Unknown")), shpnt->irq, shpnt->io_port);
325 }
326
327
328 static void do_pause(unsigned amount)
329 {                               /* Pause for amount*10 milliseconds */
330         do {
331                 mdelay(10);
332         } while (--amount);
333 }
334
335 static void fd_mcs_make_bus_idle(struct Scsi_Host *shpnt)
336 {
337         outb(0, SCSI_Cntl_port);
338         outb(0, SCSI_Mode_Cntl_port);
339         if (chip == tmc18c50 || chip == tmc18c30)
340                 outb(0x21 | PARITY_MASK, TMC_Cntl_port);        /* Clear forced intr. */
341         else
342                 outb(0x01 | PARITY_MASK, TMC_Cntl_port);
343 }
344
345 static int fd_mcs_detect(Scsi_Host_Template * tpnt)
346 {
347         int loop;
348         struct Scsi_Host *shpnt;
349
350         /* get id, port, bios, irq */
351         int slot;
352         u_char pos2, pos3, pos4;
353         int id, port, irq;
354         unsigned long bios;
355
356         /* if not MCA machine, return */
357         if (!MCA_bus)
358                 return 0;
359
360         /* changeable? */
361         id = 7;
362
363         for (loop = 0; loop < FD_BRDS; loop++) {
364                 slot = 0;
365                 while (MCA_NOTFOUND != (slot = mca_find_adapter(fd_mcs_adapters[loop].id, slot))) {
366
367                         /* if we get this far, an adapter has been detected and is
368                            enabled */
369
370                         printk(KERN_INFO "scsi  <fd_mcs>: %s at slot %d\n", fd_mcs_adapters[loop].name, slot + 1);
371
372                         pos2 = mca_read_stored_pos(slot, 2);
373                         pos3 = mca_read_stored_pos(slot, 3);
374                         pos4 = mca_read_stored_pos(slot, 4);
375
376                         /* ready for next probe */
377                         slot++;
378
379                         if (fd_mcs_adapters[loop].id == REPLY_ID) {     /* reply card */
380                                 static int reply_irq[] = { 10, 11, 14, 15 };
381
382                                 bios = 0;       /* no bios */
383
384                                 if (pos2 & 0x2)
385                                         port = ports[pos4 & 0x3];
386                                 else
387                                         continue;
388
389                                 /* can't really disable it, same as irq=10 */
390                                 irq = reply_irq[((pos4 >> 2) & 0x1) + 2 * ((pos4 >> 4) & 0x1)];
391                         } else {
392                                 bios = addresses[pos2 >> 6];
393                                 port = ports[(pos2 >> 4) & 0x03];
394                                 irq = ints[(pos2 >> 1) & 0x07];
395                         }
396
397                         if (irq) {
398                                 /* claim the slot */
399                                 mca_set_adapter_name(slot - 1, fd_mcs_adapters[loop].name);
400
401                                 /* check irq/region */
402                                 if (request_irq(irq, fd_mcs_intr, SA_SHIRQ, "fd_mcs", hosts)) {
403                                         printk(KERN_ERR "fd_mcs: interrupt is not available, skipping...\n");
404                                         continue;
405                                 }
406
407                                 /* request I/O region */
408                                 if (request_region(port, 0x10, "fd_mcs")) {
409                                         printk(KERN_ERR "fd_mcs: I/O region is already in use, skipping...\n");
410                                         continue;
411                                 }
412                                 /* register */
413                                 if (!(shpnt = scsi_register(tpnt, sizeof(struct fd_hostdata)))) {
414                                         printk(KERN_ERR "fd_mcs: scsi_register() failed\n");
415                                         release_region(port, 0x10);
416                                         free_irq(irq, hosts);
417                                         continue;
418                                 }
419
420
421                                 /* save name */
422                                 strcpy(adapter_name, fd_mcs_adapters[loop].name);
423
424                                 /* chip/fifo */
425                                 chip = fd_mcs_adapters[loop].fd_chip;
426                                 /* use boot time value if available */
427                                 FIFO_COUNT = user_fifo_count ? user_fifo_count : fd_mcs_adapters[loop].fifo_count;
428                                 FIFO_Size = user_fifo_size ? user_fifo_size : fd_mcs_adapters[loop].fifo_size;
429
430 #ifdef NOT_USED
431                                 /* *************************************************** */
432                                 /* Try to toggle 32-bit mode.  This only
433                                    works on an 18c30 chip.  (User reports
434                                    say this works, so we should switch to
435                                    it in the near future.) */
436                                 outb(0x80, port + IO_Control);
437                                 if ((inb(port + Configuration2) & 0x80) == 0x80) {
438                                         outb(0x00, port + IO_Control);
439                                         if ((inb(port + Configuration2) & 0x80) == 0x00) {
440                                                 chip = tmc18c30;
441                                                 FIFO_Size = 0x800;      /* 2k FIFO */
442
443                                                 printk("FIRST: chip=%s, fifo_size=0x%x\n", (chip == tmc18c30) ? "tmc18c30" : "tmc18c50", FIFO_Size);
444                                         }
445                                 }
446
447                                 /* That should have worked, but appears to
448                                    have problems.  Let's assume it is an
449                                    18c30 if the RAM is disabled. */
450
451                                 if (inb(port + Configuration2) & 0x02) {
452                                         chip = tmc18c30;
453                                         FIFO_Size = 0x800;      /* 2k FIFO */
454
455                                         printk("SECOND: chip=%s, fifo_size=0x%x\n", (chip == tmc18c30) ? "tmc18c30" : "tmc18c50", FIFO_Size);
456                                 }
457                                 /* *************************************************** */
458 #endif
459
460                                 /* IBM/ANSI scsi scan ordering */
461                                 /* Stick this back in when the scsi.c changes are there */
462                                 shpnt->reverse_ordering = 1;
463
464
465                                 /* saving info */
466                                 hosts[found++] = shpnt;
467
468                                 shpnt->this_id = id;
469                                 shpnt->irq = irq;
470                                 shpnt->io_port = port;
471                                 shpnt->n_io_port = 0x10;
472
473                                 /* save */
474                                 bios_base = bios;
475                                 adapter_mask = (1 << id);
476
477                                 /* save more */
478                                 SCSI_Mode_Cntl_port = port + SCSI_Mode_Cntl;
479                                 FIFO_Data_Count_port = port + FIFO_Data_Count;
480                                 Interrupt_Cntl_port = port + Interrupt_Cntl;
481                                 Interrupt_Status_port = port + Interrupt_Status;
482                                 Interrupt_Cond_port = port + Interrupt_Cond;
483                                 Read_FIFO_port = port + Read_FIFO;
484                                 Read_SCSI_Data_port = port + Read_SCSI_Data;
485                                 SCSI_Cntl_port = port + SCSI_Cntl;
486                                 SCSI_Data_NoACK_port = port + SCSI_Data_NoACK;
487                                 SCSI_Status_port = port + SCSI_Status;
488                                 TMC_Cntl_port = port + TMC_Cntl;
489                                 TMC_Status_port = port + TMC_Status;
490                                 Write_FIFO_port = port + Write_FIFO;
491                                 Write_SCSI_Data_port = port + Write_SCSI_Data;
492
493                                 Bytes_Read = 0;
494                                 Bytes_Written = 0;
495                                 INTR_Processed = 0;
496
497                                 /* say something */
498                                 print_banner(shpnt);
499
500                                 /* reset */
501                                 outb(1, SCSI_Cntl_port);
502                                 do_pause(2);
503                                 outb(0, SCSI_Cntl_port);
504                                 do_pause(115);
505                                 outb(0, SCSI_Mode_Cntl_port);
506                                 outb(PARITY_MASK, TMC_Cntl_port);
507                                 /* done reset */
508
509 #if DO_DETECT
510                                 /* scan devices attached */
511                                 {
512                                         const int buflen = 255;
513                                         int i, j, retcode;
514                                         Scsi_Cmnd SCinit;
515                                         unsigned char do_inquiry[] = { INQUIRY, 0, 0, 0, buflen, 0 };
516                                         unsigned char do_request_sense[] = { REQUEST_SENSE,
517                                                 0, 0, 0, buflen, 0
518                                         };
519                                         unsigned char do_read_capacity[] = { READ_CAPACITY,
520                                                 0, 0, 0, 0, 0, 0, 0, 0, 0
521                                         };
522                                         unsigned char buf[buflen];
523
524                                         SCinit.request_buffer = SCinit.buffer = buf;
525                                         SCinit.request_bufflen = SCinit.bufflen = sizeof(buf) - 1;
526                                         SCinit.use_sg = 0;
527                                         SCinit.lun = 0;
528                                         SCinit.host = shpnt;
529
530                                         printk("fd_mcs: detection routine scanning for devices:\n");
531                                         for (i = 0; i < 8; i++) {
532                                                 if (i == shpnt->this_id)        /* Skip host adapter */
533                                                         continue;
534                                                 SCinit.target = i;
535                                                 memcpy(SCinit.cmnd, do_request_sense, sizeof(do_request_sense));
536                                                 retcode = fd_mcs_command(&SCinit);
537                                                 if (!retcode) {
538                                                         memcpy(SCinit.cmnd, do_inquiry, sizeof(do_inquiry));
539                                                         retcode = fd_mcs_command(&SCinit);
540                                                         if (!retcode) {
541                                                                 printk("     SCSI ID %d: ", i);
542                                                                 for (j = 8; j < (buf[4] < 32 ? buf[4] : 32); j++)
543                                                                         printk("%c", buf[j] >= 20 ? buf[j] : ' ');
544                                                                 memcpy(SCinit.cmnd, do_read_capacity, sizeof(do_read_capacity));
545                                                                 retcode = fd_mcs_command(&SCinit);
546                                                                 if (!retcode) {
547                                                                         unsigned long blocks, size, capacity;
548
549                                                                         blocks = (buf[0] << 24) | (buf[1] << 16)
550                                                                             | (buf[2] << 8) | buf[3];
551                                                                         size = (buf[4] << 24) | (buf[5] << 16) | (buf[6] << 8) | buf[7];
552                                                                         capacity = +(+(blocks / 1024L) * +(size * 10L)) / 1024L;
553
554                                                                         printk("%lu MB (%lu byte blocks)\n", ((capacity + 5L) / 10L), size);
555                                                                 }
556                                                         }
557                                                 }
558                                         }
559                                 }
560 #endif
561                         }
562                 }
563
564                 if (found == FD_MAX_HOSTS) {
565                         printk("fd_mcs: detecting reached max=%d host adapters.\n", FD_MAX_HOSTS);
566                         break;
567                 }
568         }
569
570         return found;
571 }
572
573 static const char *fd_mcs_info(struct Scsi_Host *shpnt)
574 {
575         return adapter_name;
576 }
577
578 static int TOTAL_INTR = 0;
579
580 /*
581  * inout : decides on the direction of the dataflow and the meaning of the 
582  *         variables
583  * buffer: If inout==FALSE data is being written to it else read from it
584  * *start: If inout==FALSE start of the valid data in the buffer
585  * offset: If inout==FALSE offset from the beginning of the imaginary file 
586  *         from which we start writing into the buffer
587  * length: If inout==FALSE max number of bytes to be written into the buffer 
588  *         else number of bytes in the buffer
589  */
590 static int fd_mcs_proc_info(struct Scsi_Host *shpnt, char *buffer, char **start, off_t offset, int length, int inout)
591 {
592         int len = 0;
593
594         if (inout)
595                 return (-ENOSYS);
596
597         *start = buffer + offset;
598
599         len += sprintf(buffer + len, "Future Domain MCS-600/700 Driver %s\n", DRIVER_VERSION);
600         len += sprintf(buffer + len, "HOST #%d: %s\n", shpnt->host_no, adapter_name);
601         len += sprintf(buffer + len, "FIFO Size=0x%x, FIFO Count=%d\n", FIFO_Size, FIFO_COUNT);
602         len += sprintf(buffer + len, "DriverCalls=%d, Interrupts=%d, BytesRead=%d, BytesWrite=%d\n\n", TOTAL_INTR, INTR_Processed, Bytes_Read, Bytes_Written);
603
604         if ((len -= offset) <= 0)
605                 return 0;
606         if (len > length)
607                 len = length;
608         return len;
609 }
610
611 static int fd_mcs_select(struct Scsi_Host *shpnt, int target)
612 {
613         int status;
614         unsigned long timeout;
615
616         outb(0x82, SCSI_Cntl_port);     /* Bus Enable + Select */
617         outb(adapter_mask | (1 << target), SCSI_Data_NoACK_port);
618
619         /* Stop arbitration and enable parity */
620         outb(PARITY_MASK, TMC_Cntl_port);
621
622         timeout = 350;          /* 350mS -- because of timeouts
623                                    (was 250mS) */
624
625         do {
626                 status = inb(SCSI_Status_port); /* Read adapter status */
627                 if (status & 1) {       /* Busy asserted */
628                         /* Enable SCSI Bus (on error, should make bus idle with 0) */
629                         outb(0x80, SCSI_Cntl_port);
630                         return 0;
631                 }
632                 udelay(1000);   /* wait one msec */
633         } while (--timeout);
634
635         /* Make bus idle */
636         fd_mcs_make_bus_idle(shpnt);
637 #if EVERY_ACCESS
638         if (!target)
639                 printk("Selection failed\n");
640 #endif
641 #if ERRORS_ONLY
642         if (!target) {
643                 static int flag = 0;
644
645                 if (!flag)      /* Skip first failure for all chips. */
646                         ++flag;
647                 else
648                         printk("fd_mcs: Selection failed\n");
649         }
650 #endif
651         return 1;
652 }
653
654 static void my_done(struct Scsi_Host *shpnt, int error)
655 {
656         if (in_command) {
657                 in_command = 0;
658                 outb(0x00, Interrupt_Cntl_port);
659                 fd_mcs_make_bus_idle(shpnt);
660                 current_SC->result = error;
661                 current_SC->scsi_done(current_SC);
662         } else {
663                 panic("fd_mcs: my_done() called outside of command\n");
664         }
665 #if DEBUG_RACE
666         in_interrupt_flag = 0;
667 #endif
668 }
669
670 /* only my_done needs to be protected  */
671 static irqreturn_t fd_mcs_intr(int irq, void *dev_id, struct pt_regs *regs)
672 {
673         unsigned long flags;
674         int status;
675         int done = 0;
676         unsigned data_count, tmp_count;
677
678         int i = 0;
679         struct Scsi_Host *shpnt;
680
681         TOTAL_INTR++;
682
683         /* search for one adapter-response on shared interrupt */
684         while ((shpnt = hosts[i++])) {
685                 if ((inb(TMC_Status_port)) & 1)
686                         break;
687         }
688
689         /* return if some other device on this IRQ caused the interrupt */
690         if (!shpnt) {
691                 return IRQ_NONE;
692         }
693
694         INTR_Processed++;
695
696         outb(0x00, Interrupt_Cntl_port);
697
698         /* Abort calls my_done, so we do nothing here. */
699         if (current_SC->SCp.phase & aborted) {
700 #if DEBUG_ABORT
701                 printk("Interrupt after abort, ignoring\n");
702 #endif
703                 /* return IRQ_HANDLED; */
704         }
705 #if DEBUG_RACE
706         ++in_interrupt_flag;
707 #endif
708
709         if (current_SC->SCp.phase & in_arbitration) {
710                 status = inb(TMC_Status_port);  /* Read adapter status */
711                 if (!(status & 0x02)) {
712 #if EVERY_ACCESS
713                         printk(" AFAIL ");
714 #endif
715                         spin_lock_irqsave(shpnt->host_lock, flags);
716                         my_done(shpnt, DID_BUS_BUSY << 16);
717                         spin_unlock_irqrestore(shpnt->host_lock, flags);
718                         return IRQ_HANDLED;
719                 }
720                 current_SC->SCp.phase = in_selection;
721
722                 outb(0x40 | FIFO_COUNT, Interrupt_Cntl_port);
723
724                 outb(0x82, SCSI_Cntl_port);     /* Bus Enable + Select */
725                 outb(adapter_mask | (1 << current_SC->device->id), SCSI_Data_NoACK_port);
726
727                 /* Stop arbitration and enable parity */
728                 outb(0x10 | PARITY_MASK, TMC_Cntl_port);
729 #if DEBUG_RACE
730                 in_interrupt_flag = 0;
731 #endif
732                 return IRQ_HANDLED;
733         } else if (current_SC->SCp.phase & in_selection) {
734                 status = inb(SCSI_Status_port);
735                 if (!(status & 0x01)) {
736                         /* Try again, for slow devices */
737                         if (fd_mcs_select(shpnt, current_SC->device->id)) {
738 #if EVERY_ACCESS
739                                 printk(" SFAIL ");
740 #endif
741                                 spin_lock_irqsave(shpnt->host_lock, flags);
742                                 my_done(shpnt, DID_NO_CONNECT << 16);
743                                 spin_unlock_irqrestore(shpnt->host_lock, flags);
744                                 return IRQ_HANDLED;
745                         } else {
746 #if EVERY_ACCESS
747                                 printk(" AltSel ");
748 #endif
749                                 /* Stop arbitration and enable parity */
750                                 outb(0x10 | PARITY_MASK, TMC_Cntl_port);
751                         }
752                 }
753                 current_SC->SCp.phase = in_other;
754                 outb(0x90 | FIFO_COUNT, Interrupt_Cntl_port);
755                 outb(0x80, SCSI_Cntl_port);
756 #if DEBUG_RACE
757                 in_interrupt_flag = 0;
758 #endif
759                 return IRQ_HANDLED;
760         }
761
762         /* current_SC->SCp.phase == in_other: this is the body of the routine */
763
764         status = inb(SCSI_Status_port);
765
766         if (status & 0x10) {    /* REQ */
767
768                 switch (status & 0x0e) {
769
770                 case 0x08:      /* COMMAND OUT */
771                         outb(current_SC->cmnd[current_SC->SCp.sent_command++], Write_SCSI_Data_port);
772 #if EVERY_ACCESS
773                         printk("CMD = %x,", current_SC->cmnd[current_SC->SCp.sent_command - 1]);
774 #endif
775                         break;
776                 case 0x00:      /* DATA OUT -- tmc18c50/tmc18c30 only */
777                         if (chip != tmc1800 && !current_SC->SCp.have_data_in) {
778                                 current_SC->SCp.have_data_in = -1;
779                                 outb(0xd0 | PARITY_MASK, TMC_Cntl_port);
780                         }
781                         break;
782                 case 0x04:      /* DATA IN -- tmc18c50/tmc18c30 only */
783                         if (chip != tmc1800 && !current_SC->SCp.have_data_in) {
784                                 current_SC->SCp.have_data_in = 1;
785                                 outb(0x90 | PARITY_MASK, TMC_Cntl_port);
786                         }
787                         break;
788                 case 0x0c:      /* STATUS IN */
789                         current_SC->SCp.Status = inb(Read_SCSI_Data_port);
790 #if EVERY_ACCESS
791                         printk("Status = %x, ", current_SC->SCp.Status);
792 #endif
793 #if ERRORS_ONLY
794                         if (current_SC->SCp.Status && current_SC->SCp.Status != 2 && current_SC->SCp.Status != 8) {
795                                 printk("ERROR fd_mcs: target = %d, command = %x, status = %x\n", current_SC->device->id, current_SC->cmnd[0], current_SC->SCp.Status);
796                         }
797 #endif
798                         break;
799                 case 0x0a:      /* MESSAGE OUT */
800                         outb(MESSAGE_REJECT, Write_SCSI_Data_port);     /* Reject */
801                         break;
802                 case 0x0e:      /* MESSAGE IN */
803                         current_SC->SCp.Message = inb(Read_SCSI_Data_port);
804 #if EVERY_ACCESS
805                         printk("Message = %x, ", current_SC->SCp.Message);
806 #endif
807                         if (!current_SC->SCp.Message)
808                                 ++done;
809 #if DEBUG_MESSAGES || EVERY_ACCESS
810                         if (current_SC->SCp.Message) {
811                                 printk("fd_mcs: message = %x\n", current_SC->SCp.Message);
812                         }
813 #endif
814                         break;
815                 }
816         }
817
818         if (chip == tmc1800 && !current_SC->SCp.have_data_in && (current_SC->SCp.sent_command >= current_SC->cmd_len)) {
819                 /* We have to get the FIFO direction
820                    correct, so I've made a table based
821                    on the SCSI Standard of which commands
822                    appear to require a DATA OUT phase.
823                  */
824                 /*
825                    p. 94: Command for all device types
826                    CHANGE DEFINITION            40 DATA OUT
827                    COMPARE                      39 DATA OUT
828                    COPY                         18 DATA OUT
829                    COPY AND VERIFY              3a DATA OUT
830                    INQUIRY                      12 
831                    LOG SELECT                   4c DATA OUT
832                    LOG SENSE                    4d
833                    MODE SELECT (6)              15 DATA OUT
834                    MODE SELECT (10)             55 DATA OUT
835                    MODE SENSE (6)               1a
836                    MODE SENSE (10)              5a
837                    READ BUFFER                  3c
838                    RECEIVE DIAGNOSTIC RESULTS   1c
839                    REQUEST SENSE                03
840                    SEND DIAGNOSTIC              1d DATA OUT
841                    TEST UNIT READY              00
842                    WRITE BUFFER                 3b DATA OUT
843
844                    p.178: Commands for direct-access devices (not listed on p. 94)
845                    FORMAT UNIT                  04 DATA OUT
846                    LOCK-UNLOCK CACHE            36
847                    PRE-FETCH                    34
848                    PREVENT-ALLOW MEDIUM REMOVAL 1e
849                    READ (6)/RECEIVE             08
850                    READ (10)                    3c
851                    READ CAPACITY                25
852                    READ DEFECT DATA (10)        37
853                    READ LONG                    3e
854                    REASSIGN BLOCKS              07 DATA OUT
855                    RELEASE                      17
856                    RESERVE                      16 DATA OUT
857                    REZERO UNIT/REWIND           01
858                    SEARCH DATA EQUAL (10)       31 DATA OUT
859                    SEARCH DATA HIGH (10)        30 DATA OUT
860                    SEARCH DATA LOW (10)         32 DATA OUT
861                    SEEK (6)                     0b
862                    SEEK (10)                    2b
863                    SET LIMITS (10)              33
864                    START STOP UNIT              1b
865                    SYNCHRONIZE CACHE            35
866                    VERIFY (10)                  2f
867                    WRITE (6)/PRINT/SEND         0a DATA OUT
868                    WRITE (10)/SEND              2a DATA OUT
869                    WRITE AND VERIFY (10)        2e DATA OUT
870                    WRITE LONG                   3f DATA OUT
871                    WRITE SAME                   41 DATA OUT ?
872
873                    p. 261: Commands for sequential-access devices (not previously listed)
874                    ERASE                        19
875                    LOAD UNLOAD                  1b
876                    LOCATE                       2b
877                    READ BLOCK LIMITS            05
878                    READ POSITION                34
879                    READ REVERSE                 0f
880                    RECOVER BUFFERED DATA        14
881                    SPACE                        11
882                    WRITE FILEMARKS              10 ?
883
884                    p. 298: Commands for printer devices (not previously listed)
885                    ****** NOT SUPPORTED BY THIS DRIVER, since 0b is SEEK (6) *****
886                    SLEW AND PRINT               0b DATA OUT  -- same as seek
887                    STOP PRINT                   1b
888                    SYNCHRONIZE BUFFER           10
889
890                    p. 315: Commands for processor devices (not previously listed)
891
892                    p. 321: Commands for write-once devices (not previously listed)
893                    MEDIUM SCAN                  38
894                    READ (12)                    a8
895                    SEARCH DATA EQUAL (12)       b1 DATA OUT
896                    SEARCH DATA HIGH (12)        b0 DATA OUT
897                    SEARCH DATA LOW (12)         b2 DATA OUT
898                    SET LIMITS (12)              b3
899                    VERIFY (12)                  af
900                    WRITE (12)                   aa DATA OUT
901                    WRITE AND VERIFY (12)        ae DATA OUT
902
903                    p. 332: Commands for CD-ROM devices (not previously listed)
904                    PAUSE/RESUME                 4b
905                    PLAY AUDIO (10)              45
906                    PLAY AUDIO (12)              a5
907                    PLAY AUDIO MSF               47
908                    PLAY TRACK RELATIVE (10)     49
909                    PLAY TRACK RELATIVE (12)     a9
910                    READ HEADER                  44
911                    READ SUB-CHANNEL             42
912                    READ TOC                     43
913
914                    p. 370: Commands for scanner devices (not previously listed)
915                    GET DATA BUFFER STATUS       34
916                    GET WINDOW                   25
917                    OBJECT POSITION              31
918                    SCAN                         1b
919                    SET WINDOW                   24 DATA OUT
920
921                    p. 391: Commands for optical memory devices (not listed)
922                    ERASE (10)                   2c
923                    ERASE (12)                   ac
924                    MEDIUM SCAN                  38 DATA OUT
925                    READ DEFECT DATA (12)        b7
926                    READ GENERATION              29
927                    READ UPDATED BLOCK           2d
928                    UPDATE BLOCK                 3d DATA OUT
929
930                    p. 419: Commands for medium changer devices (not listed)
931                    EXCHANGE MEDIUM              46
932                    INITIALIZE ELEMENT STATUS    07
933                    MOVE MEDIUM                  a5
934                    POSITION TO ELEMENT          2b
935                    READ ELEMENT STATUS          b8
936                    REQUEST VOL. ELEMENT ADDRESS b5
937                    SEND VOLUME TAG              b6 DATA OUT
938
939                    p. 454: Commands for communications devices (not listed previously)
940                    GET MESSAGE (6)              08
941                    GET MESSAGE (10)             28
942                    GET MESSAGE (12)             a8
943                  */
944
945                 switch (current_SC->cmnd[0]) {
946                 case CHANGE_DEFINITION:
947                 case COMPARE:
948                 case COPY:
949                 case COPY_VERIFY:
950                 case LOG_SELECT:
951                 case MODE_SELECT:
952                 case MODE_SELECT_10:
953                 case SEND_DIAGNOSTIC:
954                 case WRITE_BUFFER:
955
956                 case FORMAT_UNIT:
957                 case REASSIGN_BLOCKS:
958                 case RESERVE:
959                 case SEARCH_EQUAL:
960                 case SEARCH_HIGH:
961                 case SEARCH_LOW:
962                 case WRITE_6:
963                 case WRITE_10:
964                 case WRITE_VERIFY:
965                 case 0x3f:
966                 case 0x41:
967
968                 case 0xb1:
969                 case 0xb0:
970                 case 0xb2:
971                 case 0xaa:
972                 case 0xae:
973
974                 case 0x24:
975
976                 case 0x38:
977                 case 0x3d:
978
979                 case 0xb6:
980
981                 case 0xea:      /* alternate number for WRITE LONG */
982
983                         current_SC->SCp.have_data_in = -1;
984                         outb(0xd0 | PARITY_MASK, TMC_Cntl_port);
985                         break;
986
987                 case 0x00:
988                 default:
989
990                         current_SC->SCp.have_data_in = 1;
991                         outb(0x90 | PARITY_MASK, TMC_Cntl_port);
992                         break;
993                 }
994         }
995
996         if (current_SC->SCp.have_data_in == -1) {       /* DATA OUT */
997                 while ((data_count = FIFO_Size - inw(FIFO_Data_Count_port)) > 512) {
998 #if EVERY_ACCESS
999                         printk("DC=%d, ", data_count);
1000 #endif
1001                         if (data_count > current_SC->SCp.this_residual)
1002                                 data_count = current_SC->SCp.this_residual;
1003                         if (data_count > 0) {
1004 #if EVERY_ACCESS
1005                                 printk("%d OUT, ", data_count);
1006 #endif
1007                                 if (data_count == 1) {
1008                                         Bytes_Written++;
1009
1010                                         outb(*current_SC->SCp.ptr++, Write_FIFO_port);
1011                                         --current_SC->SCp.this_residual;
1012                                 } else {
1013                                         data_count >>= 1;
1014                                         tmp_count = data_count << 1;
1015                                         outsw(Write_FIFO_port, current_SC->SCp.ptr, data_count);
1016                                         current_SC->SCp.ptr += tmp_count;
1017                                         Bytes_Written += tmp_count;
1018                                         current_SC->SCp.this_residual -= tmp_count;
1019                                 }
1020                         }
1021                         if (!current_SC->SCp.this_residual) {
1022                                 if (current_SC->SCp.buffers_residual) {
1023                                         --current_SC->SCp.buffers_residual;
1024                                         ++current_SC->SCp.buffer;
1025                                         current_SC->SCp.ptr = page_address(current_SC->SCp.buffer->page) + current_SC->SCp.buffer->offset;
1026                                         current_SC->SCp.this_residual = current_SC->SCp.buffer->length;
1027                                 } else
1028                                         break;
1029                         }
1030                 }
1031         } else if (current_SC->SCp.have_data_in == 1) { /* DATA IN */
1032                 while ((data_count = inw(FIFO_Data_Count_port)) > 0) {
1033 #if EVERY_ACCESS
1034                         printk("DC=%d, ", data_count);
1035 #endif
1036                         if (data_count > current_SC->SCp.this_residual)
1037                                 data_count = current_SC->SCp.this_residual;
1038                         if (data_count) {
1039 #if EVERY_ACCESS
1040                                 printk("%d IN, ", data_count);
1041 #endif
1042                                 if (data_count == 1) {
1043                                         Bytes_Read++;
1044                                         *current_SC->SCp.ptr++ = inb(Read_FIFO_port);
1045                                         --current_SC->SCp.this_residual;
1046                                 } else {
1047                                         data_count >>= 1;       /* Number of words */
1048                                         tmp_count = data_count << 1;
1049                                         insw(Read_FIFO_port, current_SC->SCp.ptr, data_count);
1050                                         current_SC->SCp.ptr += tmp_count;
1051                                         Bytes_Read += tmp_count;
1052                                         current_SC->SCp.this_residual -= tmp_count;
1053                                 }
1054                         }
1055                         if (!current_SC->SCp.this_residual && current_SC->SCp.buffers_residual) {
1056                                 --current_SC->SCp.buffers_residual;
1057                                 ++current_SC->SCp.buffer;
1058                                 current_SC->SCp.ptr = page_address(current_SC->SCp.buffer->page) + current_SC->SCp.buffer->offset;
1059                                 current_SC->SCp.this_residual = current_SC->SCp.buffer->length;
1060                         }
1061                 }
1062         }
1063
1064         if (done) {
1065 #if EVERY_ACCESS
1066                 printk(" ** IN DONE %d ** ", current_SC->SCp.have_data_in);
1067 #endif
1068
1069 #if ERRORS_ONLY
1070                 if (current_SC->cmnd[0] == REQUEST_SENSE && !current_SC->SCp.Status) {
1071                         if ((unsigned char) (*((char *) current_SC->request_buffer + 2)) & 0x0f) {
1072                                 unsigned char key;
1073                                 unsigned char code;
1074                                 unsigned char qualifier;
1075
1076                                 key = (unsigned char) (*((char *) current_SC->request_buffer + 2)) & 0x0f;
1077                                 code = (unsigned char) (*((char *) current_SC->request_buffer + 12));
1078                                 qualifier = (unsigned char) (*((char *) current_SC->request_buffer + 13));
1079
1080                                 if (key != UNIT_ATTENTION && !(key == NOT_READY && code == 0x04 && (!qualifier || qualifier == 0x02 || qualifier == 0x01))
1081                                     && !(key == ILLEGAL_REQUEST && (code == 0x25 || code == 0x24 || !code)))
1082
1083                                         printk("fd_mcs: REQUEST SENSE " "Key = %x, Code = %x, Qualifier = %x\n", key, code, qualifier);
1084                         }
1085                 }
1086 #endif
1087 #if EVERY_ACCESS
1088                 printk("BEFORE MY_DONE. . .");
1089 #endif
1090                 spin_lock_irqsave(shpnt->host_lock, flags);
1091                 my_done(shpnt, (current_SC->SCp.Status & 0xff)
1092                         | ((current_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16));
1093                 spin_unlock_irqrestore(shpnt->host_lock, flags);
1094 #if EVERY_ACCESS
1095                 printk("RETURNING.\n");
1096 #endif
1097
1098         } else {
1099                 if (current_SC->SCp.phase & disconnect) {
1100                         outb(0xd0 | FIFO_COUNT, Interrupt_Cntl_port);
1101                         outb(0x00, SCSI_Cntl_port);
1102                 } else {
1103                         outb(0x90 | FIFO_COUNT, Interrupt_Cntl_port);
1104                 }
1105         }
1106 #if DEBUG_RACE
1107         in_interrupt_flag = 0;
1108 #endif
1109         return IRQ_HANDLED;
1110 }
1111
1112 static int fd_mcs_release(struct Scsi_Host *shpnt)
1113 {
1114         int i, this_host, irq_usage;
1115
1116         release_region(shpnt->io_port, shpnt->n_io_port);
1117
1118         this_host = -1;
1119         irq_usage = 0;
1120         for (i = 0; i < found; i++) {
1121                 if (shpnt == hosts[i])
1122                         this_host = i;
1123                 if (shpnt->irq == hosts[i]->irq)
1124                         irq_usage++;
1125         }
1126
1127         /* only for the last one */
1128         if (1 == irq_usage)
1129                 free_irq(shpnt->irq, hosts);
1130
1131         found--;
1132
1133         for (i = this_host; i < found; i++)
1134                 hosts[i] = hosts[i + 1];
1135
1136         hosts[found] = NULL;
1137
1138         return 0;
1139 }
1140
1141 static int fd_mcs_queue(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
1142 {
1143         struct Scsi_Host *shpnt = SCpnt->device->host;
1144
1145         if (in_command) {
1146                 panic("fd_mcs: fd_mcs_queue() NOT REENTRANT!\n");
1147         }
1148 #if EVERY_ACCESS
1149         printk("queue: target = %d cmnd = 0x%02x pieces = %d size = %u\n", SCpnt->target, *(unsigned char *) SCpnt->cmnd, SCpnt->use_sg, SCpnt->request_bufflen);
1150 #endif
1151
1152         fd_mcs_make_bus_idle(shpnt);
1153
1154         SCpnt->scsi_done = done;        /* Save this for the done function */
1155         current_SC = SCpnt;
1156
1157         /* Initialize static data */
1158
1159         if (current_SC->use_sg) {
1160                 current_SC->SCp.buffer = (struct scatterlist *) current_SC->request_buffer;
1161                 current_SC->SCp.ptr = page_address(current_SC->SCp.buffer->page) + current_SC->SCp.buffer->offset;
1162                 current_SC->SCp.this_residual = current_SC->SCp.buffer->length;
1163                 current_SC->SCp.buffers_residual = current_SC->use_sg - 1;
1164         } else {
1165                 current_SC->SCp.ptr = (char *) current_SC->request_buffer;
1166                 current_SC->SCp.this_residual = current_SC->request_bufflen;
1167                 current_SC->SCp.buffer = NULL;
1168                 current_SC->SCp.buffers_residual = 0;
1169         }
1170
1171
1172         current_SC->SCp.Status = 0;
1173         current_SC->SCp.Message = 0;
1174         current_SC->SCp.have_data_in = 0;
1175         current_SC->SCp.sent_command = 0;
1176         current_SC->SCp.phase = in_arbitration;
1177
1178         /* Start arbitration */
1179         outb(0x00, Interrupt_Cntl_port);
1180         outb(0x00, SCSI_Cntl_port);     /* Disable data drivers */
1181         outb(adapter_mask, SCSI_Data_NoACK_port);       /* Set our id bit */
1182         in_command = 1;
1183         outb(0x20, Interrupt_Cntl_port);
1184         outb(0x14 | PARITY_MASK, TMC_Cntl_port);        /* Start arbitration */
1185
1186         return 0;
1187 }
1188
1189 #if DEBUG_ABORT || DEBUG_RESET
1190 static void fd_mcs_print_info(Scsi_Cmnd * SCpnt)
1191 {
1192         unsigned int imr;
1193         unsigned int irr;
1194         unsigned int isr;
1195         struct Scsi_Host *shpnt = SCpnt->host;
1196
1197         if (!SCpnt || !SCpnt->host) {
1198                 printk("fd_mcs: cannot provide detailed information\n");
1199         }
1200
1201         printk("%s\n", fd_mcs_info(SCpnt->host));
1202         print_banner(SCpnt->host);
1203         switch (SCpnt->SCp.phase) {
1204         case in_arbitration:
1205                 printk("arbitration ");
1206                 break;
1207         case in_selection:
1208                 printk("selection ");
1209                 break;
1210         case in_other:
1211                 printk("other ");
1212                 break;
1213         default:
1214                 printk("unknown ");
1215                 break;
1216         }
1217
1218         printk("(%d), target = %d cmnd = 0x%02x pieces = %d size = %u\n", SCpnt->SCp.phase, SCpnt->device->id, *(unsigned char *) SCpnt->cmnd, SCpnt->use_sg, SCpnt->request_bufflen);
1219         printk("sent_command = %d, have_data_in = %d, timeout = %d\n", SCpnt->SCp.sent_command, SCpnt->SCp.have_data_in, SCpnt->timeout);
1220 #if DEBUG_RACE
1221         printk("in_interrupt_flag = %d\n", in_interrupt_flag);
1222 #endif
1223
1224         imr = (inb(0x0a1) << 8) + inb(0x21);
1225         outb(0x0a, 0xa0);
1226         irr = inb(0xa0) << 8;
1227         outb(0x0a, 0x20);
1228         irr += inb(0x20);
1229         outb(0x0b, 0xa0);
1230         isr = inb(0xa0) << 8;
1231         outb(0x0b, 0x20);
1232         isr += inb(0x20);
1233
1234         /* Print out interesting information */
1235         printk("IMR = 0x%04x", imr);
1236         if (imr & (1 << shpnt->irq))
1237                 printk(" (masked)");
1238         printk(", IRR = 0x%04x, ISR = 0x%04x\n", irr, isr);
1239
1240         printk("SCSI Status      = 0x%02x\n", inb(SCSI_Status_port));
1241         printk("TMC Status       = 0x%02x", inb(TMC_Status_port));
1242         if (inb(TMC_Status_port) & 1)
1243                 printk(" (interrupt)");
1244         printk("\n");
1245         printk("Interrupt Status = 0x%02x", inb(Interrupt_Status_port));
1246         if (inb(Interrupt_Status_port) & 0x08)
1247                 printk(" (enabled)");
1248         printk("\n");
1249         if (chip == tmc18c50 || chip == tmc18c30) {
1250                 printk("FIFO Status      = 0x%02x\n", inb(shpnt->io_port + FIFO_Status));
1251                 printk("Int. Condition   = 0x%02x\n", inb(shpnt->io_port + Interrupt_Cond));
1252         }
1253         printk("Configuration 1  = 0x%02x\n", inb(shpnt->io_port + Configuration1));
1254         if (chip == tmc18c50 || chip == tmc18c30)
1255                 printk("Configuration 2  = 0x%02x\n", inb(shpnt->io_port + Configuration2));
1256 }
1257 #endif
1258
1259 static int fd_mcs_abort(Scsi_Cmnd * SCpnt)
1260 {
1261         struct Scsi_Host *shpnt = SCpnt->device->host;
1262
1263         unsigned long flags;
1264 #if EVERY_ACCESS || ERRORS_ONLY || DEBUG_ABORT
1265         printk("fd_mcs: abort ");
1266 #endif
1267
1268         spin_lock_irqsave(shpnt->host_lock, flags);
1269         if (!in_command) {
1270 #if EVERY_ACCESS || ERRORS_ONLY
1271                 printk(" (not in command)\n");
1272 #endif
1273                 spin_unlock_irqrestore(shpnt->host_lock, flags);
1274                 return FAILED;
1275         } else
1276                 printk("\n");
1277
1278 #if DEBUG_ABORT
1279         fd_mcs_print_info(SCpnt);
1280 #endif
1281
1282         fd_mcs_make_bus_idle(shpnt);
1283
1284         current_SC->SCp.phase |= aborted;
1285
1286         current_SC->result = DID_ABORT << 16;
1287
1288         /* Aborts are not done well. . . */
1289         my_done(shpnt, DID_ABORT << 16);
1290
1291         spin_unlock_irqrestore(shpnt->host_lock, flags);
1292         return SUCCESS;
1293 }
1294
1295 static int fd_mcs_host_reset(Scsi_Cmnd * SCpnt)
1296 {
1297         return FAILED;
1298 }
1299
1300 static int fd_mcs_device_reset(Scsi_Cmnd * SCpnt) 
1301 {
1302         return FAILED;
1303 }
1304
1305 static int fd_mcs_bus_reset(Scsi_Cmnd * SCpnt) {
1306         struct Scsi_Host *shpnt = SCpnt->device->host;
1307
1308 #if DEBUG_RESET
1309         static int called_once = 0;
1310 #endif
1311
1312 #if ERRORS_ONLY
1313         if (SCpnt)
1314                 printk("fd_mcs: SCSI Bus Reset\n");
1315 #endif
1316
1317 #if DEBUG_RESET
1318         if (called_once)
1319                 fd_mcs_print_info(current_SC);
1320         called_once = 1;
1321 #endif
1322
1323         outb(1, SCSI_Cntl_port);
1324         do_pause(2);
1325         outb(0, SCSI_Cntl_port);
1326         do_pause(115);
1327         outb(0, SCSI_Mode_Cntl_port);
1328         outb(PARITY_MASK, TMC_Cntl_port);
1329
1330         /* Unless this is the very first call (i.e., SCPnt == NULL), everything
1331            is probably hosed at this point.  We will, however, try to keep
1332            things going by informing the high-level code that we need help. */
1333                 return SUCCESS;
1334 }
1335
1336 #include <scsi/scsi_ioctl.h>
1337
1338 static int fd_mcs_biosparam(struct scsi_device * disk, struct block_device *bdev,
1339                             sector_t capacity, int *info_array) 
1340 {
1341         unsigned char *p = scsi_bios_ptable(bdev);
1342         int size = capacity;
1343
1344         /* BIOS >= 3.4 for MCA cards */
1345         /* This algorithm was provided by Future Domain (much thanks!). */
1346
1347         if (p && p[65] == 0xaa && p[64] == 0x55 /* Partition table valid */
1348             && p[4]) {  /* Partition type */
1349                 /* The partition table layout is as follows:
1350
1351                    Start: 0x1b3h
1352                    Offset: 0 = partition status
1353                    1 = starting head
1354                    2 = starting sector and cylinder (word, encoded)
1355                    4 = partition type
1356                    5 = ending head
1357                    6 = ending sector and cylinder (word, encoded)
1358                    8 = starting absolute sector (double word)
1359                    c = number of sectors (double word)
1360                    Signature: 0x1fe = 0x55aa
1361
1362                    So, this algorithm assumes:
1363                    1) the first partition table is in use,
1364                    2) the data in the first entry is correct, and
1365                    3) partitions never divide cylinders
1366
1367                    Note that (1) may be FALSE for NetBSD (and other BSD flavors),
1368                    as well as for Linux.  Note also, that Linux doesn't pay any
1369                    attention to the fields that are used by this algorithm -- it
1370                    only uses the absolute sector data.  Recent versions of Linux's
1371                    fdisk(1) will fill this data in correctly, and forthcoming
1372                    versions will check for consistency.
1373
1374                    Checking for a non-zero partition type is not part of the
1375                    Future Domain algorithm, but it seemed to be a reasonable thing
1376                    to do, especially in the Linux and BSD worlds. */
1377
1378                 info_array[0] = p[5] + 1;       /* heads */
1379                 info_array[1] = p[6] & 0x3f;    /* sectors */
1380         } else {
1381                 /* Note that this new method guarantees that there will always be
1382                    less than 1024 cylinders on a platter.  This is good for drives
1383                    up to approximately 7.85GB (where 1GB = 1024 * 1024 kB). */
1384                 if ((unsigned int) size >= 0x7e0000U) 
1385                 {
1386                         info_array[0] = 0xff;   /* heads   = 255 */
1387                         info_array[1] = 0x3f;   /* sectors =  63 */
1388                 } else if ((unsigned int) size >= 0x200000U) {
1389                         info_array[0] = 0x80;   /* heads   = 128 */
1390                         info_array[1] = 0x3f;   /* sectors =  63 */
1391                 } else {
1392                         info_array[0] = 0x40;   /* heads   =  64 */
1393                         info_array[1] = 0x20;   /* sectors =  32 */
1394                 }
1395         }
1396         /* For both methods, compute the cylinders */
1397         info_array[2] = (unsigned int) size / (info_array[0] * info_array[1]);
1398         kfree(p);
1399         return 0;
1400 }
1401
1402 static Scsi_Host_Template driver_template = {
1403         .proc_name                      = "fd_mcs",
1404         .proc_info                      = fd_mcs_proc_info,
1405         .detect                         = fd_mcs_detect,
1406         .release                        = fd_mcs_release,
1407         .info                           = fd_mcs_info,
1408         .queuecommand                   = fd_mcs_queue, 
1409         .eh_abort_handler               = fd_mcs_abort,
1410         .eh_bus_reset_handler           = fd_mcs_bus_reset,
1411         .eh_host_reset_handler          = fd_mcs_host_reset,
1412         .eh_device_reset_handler        = fd_mcs_device_reset,
1413         .bios_param                     = fd_mcs_biosparam,
1414         .can_queue                      = 1,
1415         .this_id                        = 7,
1416         .sg_tablesize                   = 64,
1417         .cmd_per_lun                    = 1,
1418         .use_clustering                 = DISABLE_CLUSTERING,
1419 };
1420 #include "scsi_module.c"