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