File: /usr/src/linux/drivers/scsi/ultrastor.c

1     /*
2      *	ultrastor.c	Copyright (C) 1992 David B. Gentzel
3      *	Low-level SCSI driver for UltraStor 14F, 24F, and 34F
4      *	by David B. Gentzel, Whitfield Software Services, Carnegie, PA
5      *	    (gentzel@nova.enet.dec.com)
6      *  scatter/gather added by Scott Taylor (n217cg@tamuts.tamu.edu)
7      *  24F and multiple command support by John F. Carr (jfc@athena.mit.edu)
8      *    John's work modified by Caleb Epstein (cae@jpmorgan.com) and 
9      *    Eric Youngdale (ericy@cais.com).
10      *	Thanks to UltraStor for providing the necessary documentation
11      */
12     
13     /*
14      * TODO:
15      *	1. Find out why scatter/gather is limited to 16 requests per command.
16      *         This is fixed, at least on the 24F, as of version 1.12 - CAE.
17      *	2. Look at command linking (mscp.command_link and
18      *	   mscp.command_link_id).  (Does not work with many disks, 
19      *				and no performance increase.  ERY).
20      *	3. Allow multiple adapters.
21      */
22     
23     /*
24      * NOTES:
25      *    The UltraStor 14F, 24F, and 34F are a family of intelligent, high
26      *    performance SCSI-2 host adapters.  They all support command queueing
27      *    and scatter/gather I/O.  Some of them can also emulate the standard
28      *    WD1003 interface for use with OS's which don't support SCSI.  Here
29      *    is the scoop on the various models:
30      *	14F - ISA first-party DMA HA with floppy support and WD1003 emulation.
31      *	14N - ISA HA with floppy support.  I think that this is a non-DMA
32      *	      HA.  Nothing further known.
33      *	24F - EISA Bus Master HA with floppy support and WD1003 emulation.
34      *	34F - VL-Bus Bus Master HA with floppy support (no WD1003 emulation).
35      *
36      *    The 14F, 24F, and 34F are supported by this driver.
37      *
38      *    Places flagged with a triple question-mark are things which are either
39      *    unfinished, questionable, or wrong.
40      */
41     
42     /* Changes from version 1.11 alpha to 1.12
43      *
44      * Increased the size of the scatter-gather list to 33 entries for
45      * the 24F adapter (it was 16).  I don't have the specs for the 14F
46      * or the 34F, so they may support larger s-g lists as well.
47      *
48      * Caleb Epstein <cae@jpmorgan.com>
49      */
50     
51     /* Changes from version 1.9 to 1.11
52      *
53      * Patches to bring this driver up to speed with the default kernel
54      * driver which supports only the 14F and 34F adapters.  This version
55      * should compile cleanly into 0.99.13, 0.99.12 and probably 0.99.11.
56      *
57      * Fixes from Eric Youngdale to fix a few possible race conditions and
58      * several problems with bit testing operations (insufficient
59      * parentheses).
60      *
61      * Removed the ultrastor_abort() and ultrastor_reset() functions
62      * (enclosed them in #if 0 / #endif).  These functions, at least on
63      * the 24F, cause the SCSI bus to do odd things and generally lead to
64      * kernel panics and machine hangs.  This is like the Adaptec code.
65      *
66      * Use check/snarf_region for 14f, 34f to avoid I/O space address conflicts.
67      */
68     
69     /* Changes from version 1.8 to version 1.9
70      *
71      *  0.99.11 patches (cae@jpmorgan.com) */
72     
73     /* Changes from version 1.7 to version 1.8
74      *
75      * Better error reporting.
76      */
77     
78     /* Changes from version 1.6 to version 1.7
79      *
80      * Removed CSIR command code.
81      *
82      * Better race condition avoidance (xchgb function added).
83      *
84      * Set ICM and OGM status to zero at probe (24F)
85      *
86      * reset sends soft reset to UltraStor adapter
87      *
88      * reset adapter if adapter interrupts with an invalid MSCP address
89      *
90      * handle aborted command interrupt (24F)
91      *
92      */
93     
94     /* Changes from version 1.5 to version 1.6:
95      *
96      * Read MSCP address from ICM _before_ clearing the interrupt flag.
97      * This fixes a race condition.
98      */
99     
100     /* Changes from version 1.4 to version 1.5:
101      *
102      * Abort now calls done when multiple commands are enabled.
103      *
104      * Clear busy when aborted command finishes, not when abort is called.
105      *
106      * More debugging messages for aborts.
107      */
108     
109     /* Changes from version 1.3 to version 1.4:
110      *
111      * Enable automatic request of sense data on error (requires newer version
112      * of scsi.c to be useful).
113      *
114      * Fix PORT_OVERRIDE for 14F.
115      *
116      * Fix abort and reset to work properly (config.aborted wasn't cleared
117      * after it was tested, so after a command abort no further commands would
118      * work).
119      *
120      * Boot time test to enable SCSI bus reset (defaults to not allowing reset).
121      *
122      * Fix test for OGM busy -- the busy bit is in different places on the 24F.
123      *
124      * Release ICM slot by clearing first byte on 24F.
125      */
126     
127     #ifdef MODULE
128     #include <linux/module.h>
129     #endif
130     
131     #include <linux/stddef.h>
132     #include <linux/string.h>
133     #include <linux/sched.h>
134     #include <linux/kernel.h>
135     #include <linux/ioport.h>
136     #include <linux/proc_fs.h>
137     #include <linux/spinlock.h>
138     #include <asm/io.h>
139     #include <asm/bitops.h>
140     #include <asm/system.h>
141     #include <asm/dma.h>
142     
143     #define ULTRASTOR_PRIVATE	/* Get the private stuff from ultrastor.h */
144     #include <linux/blk.h>
145     #include "scsi.h"
146     #include "hosts.h"
147     #include "ultrastor.h"
148     #include "sd.h"
149     #include<linux/stat.h>
150     
151     #define FALSE 0
152     #define TRUE 1
153     
154     #ifndef ULTRASTOR_DEBUG
155     #define ULTRASTOR_DEBUG (UD_ABORT|UD_CSIR|UD_RESET)
156     #endif
157     
158     #define VERSION "1.12"
159     
160     #define PACKED		__attribute__((packed))
161     #define ALIGNED(x)	__attribute__((aligned(x)))
162     
163     
164     /* The 14F uses an array of 4-byte ints for its scatter/gather list.
165        The data can be unaligned, but need not be.  It's easier to give
166        the list normal alignment since it doesn't need to fit into a
167        packed structure.  */
168     
169     typedef struct {
170       unsigned int address;
171       unsigned int num_bytes;
172     } ultrastor_sg_list;
173     
174     
175     /* MailBox SCSI Command Packet.  Basic command structure for communicating
176        with controller. */
177     struct mscp {
178       unsigned char opcode: 3;		/* type of command */
179       unsigned char xdir: 2;		/* data transfer direction */
180       unsigned char dcn: 1;		/* disable disconnect */
181       unsigned char ca: 1;		/* use cache (if available) */
182       unsigned char sg: 1;		/* scatter/gather operation */
183       unsigned char target_id: 3;		/* target SCSI id */
184       unsigned char ch_no: 2;		/* SCSI channel (always 0 for 14f) */
185       unsigned char lun: 3;		/* logical unit number */
186       unsigned int transfer_data PACKED;	/* transfer data pointer */
187       unsigned int transfer_data_length PACKED;	/* length in bytes */
188       unsigned int command_link PACKED;	/* for linking command chains */
189       unsigned char scsi_command_link_id;	/* identifies command in chain */
190       unsigned char number_of_sg_list;	/* (if sg is set) 8 bytes per list */
191       unsigned char length_of_sense_byte;
192       unsigned char length_of_scsi_cdbs;	/* 6, 10, or 12 */
193       unsigned char scsi_cdbs[12];	/* SCSI commands */
194       unsigned char adapter_status;	/* non-zero indicates HA error */
195       unsigned char target_status;	/* non-zero indicates target error */
196       unsigned int sense_data PACKED;
197       /* The following fields are for software only.  They are included in
198          the MSCP structure because they are associated with SCSI requests.  */
199       void (*done)(Scsi_Cmnd *);
200       Scsi_Cmnd *SCint;
201       ultrastor_sg_list sglist[ULTRASTOR_24F_MAX_SG]; /* use larger size for 24F */
202     };
203     
204     
205     /* Port addresses (relative to the base address) */
206     #define U14F_PRODUCT_ID(port) ((port) + 0x4)
207     #define CONFIG(port) ((port) + 0x6)
208     
209     /* Port addresses relative to the doorbell base address.  */
210     #define LCL_DOORBELL_MASK(port) ((port) + 0x0)
211     #define LCL_DOORBELL_INTR(port) ((port) + 0x1)
212     #define SYS_DOORBELL_MASK(port) ((port) + 0x2)
213     #define SYS_DOORBELL_INTR(port) ((port) + 0x3)
214     
215     
216     /* Used to store configuration info read from config i/o registers.  Most of
217        this is not used yet, but might as well save it.
218        
219        This structure also holds port addresses that are not at the same offset
220        on the 14F and 24F.
221        
222        This structure holds all data that must be duplicated to support multiple
223        adapters.  */
224     
225     static struct ultrastor_config
226     {
227       unsigned short port_address;		/* base address of card */
228       unsigned short doorbell_address;	/* base address of doorbell CSRs */
229       unsigned short ogm_address;		/* base address of OGM */
230       unsigned short icm_address;		/* base address of ICM */
231       const void *bios_segment;
232       unsigned char interrupt: 4;
233       unsigned char dma_channel: 3;
234       unsigned char bios_drive_number: 1;
235       unsigned char heads;
236       unsigned char sectors;
237       unsigned char ha_scsi_id: 3;
238       unsigned char subversion: 4;
239       unsigned char revision;
240       /* The slot number is used to distinguish the 24F (slot != 0) from
241          the 14F and 34F (slot == 0). */
242       unsigned char slot;
243     
244     #ifdef PRINT_U24F_VERSION
245       volatile int csir_done;
246     #endif
247     
248       /* A pool of MSCP structures for this adapter, and a bitmask of
249          busy structures.  (If ULTRASTOR_14F_MAX_CMDS == 1, a 1 byte
250          busy flag is used instead.)  */
251     
252     #if ULTRASTOR_MAX_CMDS == 1
253       unsigned char mscp_busy;
254     #else
255       unsigned short mscp_free;
256     #endif
257       volatile unsigned char aborted[ULTRASTOR_MAX_CMDS];
258       struct mscp mscp[ULTRASTOR_MAX_CMDS];
259     } config = {0};
260     
261     /* Set this to 1 to reset the SCSI bus on error.  */
262     int ultrastor_bus_reset;
263     
264     
265     /* Allowed BIOS base addresses (NULL indicates reserved) */
266     static const void *const bios_segment_table[8] = {
267       NULL,	     (void *)0xC4000, (void *)0xC8000, (void *)0xCC000,
268       (void *)0xD0000, (void *)0xD4000, (void *)0xD8000, (void *)0xDC000,
269     };
270     
271     /* Allowed IRQs for 14f */
272     static const unsigned char interrupt_table_14f[4] = { 15, 14, 11, 10 };
273     
274     /* Allowed DMA channels for 14f (0 indicates reserved) */
275     static const unsigned char dma_channel_table_14f[4] = { 5, 6, 7, 0 };
276     
277     /* Head/sector mappings allowed by 14f */
278     static const struct {
279       unsigned char heads;
280       unsigned char sectors;
281     } mapping_table[4] = { { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 } };
282     
283     #ifndef PORT_OVERRIDE
284     /* ??? A probe of address 0x310 screws up NE2000 cards */
285     static const unsigned short ultrastor_ports_14f[] = {
286       0x330, 0x340, /*0x310,*/ 0x230, 0x240, 0x210, 0x130, 0x140,
287     };
288     #endif
289     
290     static void ultrastor_interrupt(int, void *, struct pt_regs *);
291     static void do_ultrastor_interrupt(int, void *, struct pt_regs *);
292     static inline void build_sg_list(struct mscp *, Scsi_Cmnd *SCpnt);
293     
294     
295     static inline int find_and_clear_bit_16(unsigned short *field)
296     {
297       int rv;
298       unsigned long flags;
299     
300       save_flags(flags);
301       cli();
302       if (*field == 0) panic("No free mscp");
303       asm("xorl %0,%0\n0:\tbsfw %1,%w0\n\tbtr %0,%1\n\tjnc 0b"
304           : "=&r" (rv), "=m" (*field) : "1" (*field));
305       restore_flags(flags);
306       return rv;
307     }
308     
309     /* This has been re-implemented with the help of Richard Earnshaw,
310        <rwe@pegasus.esprit.ec.org> and works with gcc-2.5.8 and gcc-2.6.0.
311        The instability noted by jfc below appears to be a bug in
312        gcc-2.5.x when compiling w/o optimization.  --Caleb
313     
314        This asm is fragile: it doesn't work without the casts and it may
315        not work without optimization.  Maybe I should add a swap builtin
316        to gcc.  --jfc  */
317     static inline unsigned char xchgb(unsigned char reg,
318     				  volatile unsigned char *mem)
319     {
320       __asm__ ("xchgb %0,%1" : "=q" (reg), "=m" (*mem) : "0" (reg));
321       return reg;
322     }
323     
324     #if ULTRASTOR_DEBUG & (UD_COMMAND | UD_ABORT)
325     
326     static void log_ultrastor_abort(register struct ultrastor_config *config,
327     				int command)
328     {
329       static char fmt[80] = "abort %d (%x); MSCP free pool: %x;";
330       register int i;
331       unsigned long flags;
332       save_flags(flags);
333       cli();
334     
335       for (i = 0; i < ULTRASTOR_MAX_CMDS; i++)
336         {
337           fmt[20 + i*2] = ' ';
338           if (! (config->mscp_free & (1 << i)))
339     	fmt[21 + i*2] = '0' + config->mscp[i].target_id;
340           else
341     	fmt[21 + i*2] = '-';
342         }
343       fmt[20 + ULTRASTOR_MAX_CMDS * 2] = '\n';
344       fmt[21 + ULTRASTOR_MAX_CMDS * 2] = 0;
345       printk(fmt, command, &config->mscp[command], config->mscp_free);
346       restore_flags(flags);
347     }
348     #endif
349     
350     static int ultrastor_14f_detect(Scsi_Host_Template * tpnt)
351     {
352         size_t i;
353         unsigned char in_byte, version_byte = 0;
354         struct config_1 {
355           unsigned char bios_segment: 3;
356           unsigned char removable_disks_as_fixed: 1;
357           unsigned char interrupt: 2;
358         unsigned char dma_channel: 2;
359         } config_1;
360         struct config_2 {
361           unsigned char ha_scsi_id: 3;
362           unsigned char mapping_mode: 2;
363           unsigned char bios_drive_number: 1;
364           unsigned char tfr_port: 2;
365         } config_2;
366     
367     #if (ULTRASTOR_DEBUG & UD_DETECT)
368         printk("US14F: detect: called\n");
369     #endif
370     
371         /* If a 24F has already been configured, don't look for a 14F.  */
372         if (config.bios_segment)
373     	return FALSE;
374     
375     #ifdef PORT_OVERRIDE
376         if(check_region(PORT_OVERRIDE, 0xc)) {
377           printk("Ultrastor I/O space already in use\n");
378           return FALSE;
379         };
380         config.port_address = PORT_OVERRIDE;
381     #else
382         for (i = 0; i < ARRAY_SIZE(ultrastor_ports_14f); i++) {
383           if(check_region(ultrastor_ports_14f[i], 0x0c)) continue;
384           config.port_address = ultrastor_ports_14f[i];
385     #endif
386     
387     #if (ULTRASTOR_DEBUG & UD_DETECT)
388     	printk("US14F: detect: testing port address %03X\n", config.port_address);
389     #endif
390     
391     	in_byte = inb(U14F_PRODUCT_ID(config.port_address));
392     	if (in_byte != US14F_PRODUCT_ID_0) {
393     #if (ULTRASTOR_DEBUG & UD_DETECT)
394     # ifdef PORT_OVERRIDE
395     	    printk("US14F: detect: wrong product ID 0 - %02X\n", in_byte);
396     # else
397     	    printk("US14F: detect: no adapter at port %03X\n", config.port_address);
398     # endif
399     #endif
400     #ifdef PORT_OVERRIDE
401     	    return FALSE;
402     #else
403     	    continue;
404     #endif
405     	}
406     	in_byte = inb(U14F_PRODUCT_ID(config.port_address) + 1);
407     	/* Only upper nibble is significant for Product ID 1 */
408     	if ((in_byte & 0xF0) != US14F_PRODUCT_ID_1) {
409     #if (ULTRASTOR_DEBUG & UD_DETECT)
410     # ifdef PORT_OVERRIDE
411     	    printk("US14F: detect: wrong product ID 1 - %02X\n", in_byte);
412     # else
413     	    printk("US14F: detect: no adapter at port %03X\n", config.port_address);
414     # endif
415     #endif
416     #ifdef PORT_OVERRIDE
417     	    return FALSE;
418     #else
419     	    continue;
420     #endif
421     	}
422     	version_byte = in_byte;
423     #ifndef PORT_OVERRIDE
424     	break;
425         }
426         if (i == ARRAY_SIZE(ultrastor_ports_14f)) {
427     # if (ULTRASTOR_DEBUG & UD_DETECT)
428     	printk("US14F: detect: no port address found!\n");
429     # endif
430     	return FALSE;
431         }
432     #endif
433     
434     #if (ULTRASTOR_DEBUG & UD_DETECT)
435         printk("US14F: detect: adapter found at port address %03X\n",
436     	   config.port_address);
437     #endif
438     
439         /* Set local doorbell mask to disallow bus reset unless
440            ultrastor_bus_reset is true.  */
441         outb(ultrastor_bus_reset ? 0xc2 : 0x82, LCL_DOORBELL_MASK(config.port_address));
442     
443         /* All above tests passed, must be the right thing.  Get some useful
444            info. */
445     
446         request_region(config.port_address, 0x0c,"ultrastor"); 
447         /* Register the I/O space that we use */
448     
449         *(char *)&config_1 = inb(CONFIG(config.port_address + 0));
450         *(char *)&config_2 = inb(CONFIG(config.port_address + 1));
451         config.bios_segment = bios_segment_table[config_1.bios_segment];
452         config.doorbell_address = config.port_address;
453         config.ogm_address = config.port_address + 0x8;
454         config.icm_address = config.port_address + 0xC;
455         config.interrupt = interrupt_table_14f[config_1.interrupt];
456         config.ha_scsi_id = config_2.ha_scsi_id;
457         config.heads = mapping_table[config_2.mapping_mode].heads;
458         config.sectors = mapping_table[config_2.mapping_mode].sectors;
459         config.bios_drive_number = config_2.bios_drive_number;
460         config.subversion = (version_byte & 0x0F);
461         if (config.subversion == U34F)
462     	config.dma_channel = 0;
463         else
464     	config.dma_channel = dma_channel_table_14f[config_1.dma_channel];
465     
466         if (!config.bios_segment) {
467     #if (ULTRASTOR_DEBUG & UD_DETECT)
468     	printk("US14F: detect: not detected.\n");
469     #endif
470     	return FALSE;
471         }
472     
473         /* Final consistency check, verify previous info. */
474         if (config.subversion != U34F)
475     	if (!config.dma_channel || !(config_2.tfr_port & 0x2)) {
476     #if (ULTRASTOR_DEBUG & UD_DETECT)
477     	    printk("US14F: detect: consistency check failed\n");
478     #endif
479     	    return FALSE;
480     	}
481     
482         /* If we were TRULY paranoid, we could issue a host adapter inquiry
483            command here and verify the data returned.  But frankly, I'm
484            exhausted! */
485     
486         /* Finally!  Now I'm satisfied... */
487     #if (ULTRASTOR_DEBUG & UD_DETECT)
488         printk("US14F: detect: detect succeeded\n"
489     	   "  Port address: %03X\n"
490     	   "  BIOS segment: %05X\n"
491     	   "  Interrupt: %u\n"
492     	   "  DMA channel: %u\n"
493     	   "  H/A SCSI ID: %u\n"
494     	   "  Subversion: %u\n",
495     	   config.port_address, config.bios_segment, config.interrupt,
496     	   config.dma_channel, config.ha_scsi_id, config.subversion);
497     #endif
498         tpnt->this_id = config.ha_scsi_id;
499         tpnt->unchecked_isa_dma = (config.subversion != U34F);
500     
501     #if ULTRASTOR_MAX_CMDS > 1
502         config.mscp_free = ~0;
503     #endif
504     
505         if (request_irq(config.interrupt, do_ultrastor_interrupt, 0, "Ultrastor", NULL)) {
506     	printk("Unable to allocate IRQ%u for UltraStor controller.\n",
507     	       config.interrupt);
508     	return FALSE;
509         }
510         if (config.dma_channel && request_dma(config.dma_channel,"Ultrastor")) {
511     	printk("Unable to allocate DMA channel %u for UltraStor controller.\n",
512     	       config.dma_channel);
513     	free_irq(config.interrupt, NULL);
514     	return FALSE;
515         }
516         tpnt->sg_tablesize = ULTRASTOR_14F_MAX_SG;
517         printk("UltraStor driver version" VERSION ".  Using %d SG lists.\n",
518     	   ULTRASTOR_14F_MAX_SG);
519     
520         return TRUE;
521     }
522     
523     static int ultrastor_24f_detect(Scsi_Host_Template * tpnt)
524     {
525       register int i;
526       struct Scsi_Host * shpnt = NULL;
527     
528     #if (ULTRASTOR_DEBUG & UD_DETECT)
529       printk("US24F: detect");
530     #endif
531     
532       /* probe each EISA slot at slot address C80 */
533       for (i = 1; i < 15; i++)
534         {
535           unsigned char config_1, config_2;
536           unsigned short addr = (i << 12) | ULTRASTOR_24F_PORT;
537     
538           if (inb(addr) != US24F_PRODUCT_ID_0 &&
539     	  inb(addr+1) != US24F_PRODUCT_ID_1 &&
540     	  inb(addr+2) != US24F_PRODUCT_ID_2)
541     	continue;
542     
543           config.revision = inb(addr+3);
544           config.slot = i;
545           if (! (inb(addr+4) & 1))
546     	{
547     #if (ULTRASTOR_DEBUG & UD_DETECT)
548     	  printk("U24F: found disabled card in slot %u\n", i);
549     #endif
550     	  continue;
551     	}
552     #if (ULTRASTOR_DEBUG & UD_DETECT)
553           printk("U24F: found card in slot %u\n", i);
554     #endif
555           config_1 = inb(addr + 5);
556           config.bios_segment = bios_segment_table[config_1 & 7];
557           switch(config_1 >> 4)
558     	{
559     	case 1:
560     	  config.interrupt = 15;
561     	  break;
562     	case 2:
563     	  config.interrupt = 14;
564     	  break;
565     	case 4:
566     	  config.interrupt = 11;
567     	  break;
568     	case 8:
569     	  config.interrupt = 10;
570     	  break;
571     	default:
572     	  printk("U24F: invalid IRQ\n");
573     	  return FALSE;
574     	}
575           if (request_irq(config.interrupt, do_ultrastor_interrupt, 0, "Ultrastor", NULL))
576     	{
577     	  printk("Unable to allocate IRQ%u for UltraStor controller.\n",
578     		 config.interrupt);
579     	  return FALSE;
580     	}
581           /* BIOS addr set */
582           /* base port set */
583           config.port_address = addr;
584           config.doorbell_address = addr + 12;
585           config.ogm_address = addr + 0x17;
586           config.icm_address = addr + 0x1C;
587           config_2 = inb(addr + 7);
588           config.ha_scsi_id = config_2 & 7;
589           config.heads = mapping_table[(config_2 >> 3) & 3].heads;
590           config.sectors = mapping_table[(config_2 >> 3) & 3].sectors;
591     #if (ULTRASTOR_DEBUG & UD_DETECT)
592           printk("US24F: detect: detect succeeded\n"
593     	     "  Port address: %03X\n"
594     	     "  BIOS segment: %05X\n"
595     	     "  Interrupt: %u\n"
596     	     "  H/A SCSI ID: %u\n",
597     	     config.port_address, config.bios_segment,
598     	     config.interrupt, config.ha_scsi_id);
599     #endif
600           tpnt->this_id = config.ha_scsi_id;
601           tpnt->unchecked_isa_dma = 0;
602           tpnt->sg_tablesize = ULTRASTOR_24F_MAX_SG;
603     
604           shpnt = scsi_register(tpnt, 0);
605           if (!shpnt) {
606                  printk(KERN_WARNING "(ultrastor:) Could not register scsi device. Aborting registration.\n");
607                  free_irq(config.interrupt, do_ultrastor_interrupt);
608                  return FALSE;
609           }
610     
611           shpnt->irq = config.interrupt;
612           shpnt->dma_channel = config.dma_channel;
613           shpnt->io_port = config.port_address;
614     
615     #if ULTRASTOR_MAX_CMDS > 1
616           config.mscp_free = ~0;
617     #endif
618           /* Mark ICM and OGM free */
619           outb(0, addr + 0x16);
620           outb(0, addr + 0x1B);
621     
622           /* Set local doorbell mask to disallow bus reset unless
623     	 ultrastor_bus_reset is true.  */
624           outb(ultrastor_bus_reset ? 0xc2 : 0x82, LCL_DOORBELL_MASK(addr+12));
625           outb(0x02, SYS_DOORBELL_MASK(addr+12));
626           printk("UltraStor driver version " VERSION ".  Using %d SG lists.\n",
627     	     tpnt->sg_tablesize);
628           return TRUE;
629         }
630       return FALSE;
631     }
632     
633     int ultrastor_detect(Scsi_Host_Template * tpnt)
634     {
635         tpnt->proc_name = "ultrastor";
636       return ultrastor_14f_detect(tpnt) || ultrastor_24f_detect(tpnt);
637     }
638     
639     const char *ultrastor_info(struct Scsi_Host * shpnt)
640     {
641         static char buf[64];
642     
643         if (config.slot)
644           sprintf(buf, "UltraStor 24F SCSI @ Slot %u IRQ%u",
645     	      config.slot, config.interrupt);
646         else if (config.subversion)
647           sprintf(buf, "UltraStor 34F SCSI @ Port %03X BIOS %05X IRQ%u",
648     	      config.port_address, (int)config.bios_segment,
649     	      config.interrupt);
650         else
651           sprintf(buf, "UltraStor 14F SCSI @ Port %03X BIOS %05X IRQ%u DMA%u",
652     	      config.port_address, (int)config.bios_segment,
653     	      config.interrupt, config.dma_channel);
654         return buf;
655     }
656     
657     static inline void build_sg_list(register struct mscp *mscp, Scsi_Cmnd *SCpnt)
658     {
659     	struct scatterlist *sl;
660     	long transfer_length = 0;
661     	int i, max;
662     
663     	sl = (struct scatterlist *) SCpnt->request_buffer;
664     	max = SCpnt->use_sg;
665     	for (i = 0; i < max; i++) {
666     		mscp->sglist[i].address = virt_to_bus(sl[i].address);
667     		mscp->sglist[i].num_bytes = sl[i].length;
668     		transfer_length += sl[i].length;
669     	}
670     	mscp->number_of_sg_list = max;
671     	mscp->transfer_data = virt_to_bus(mscp->sglist);
672     	/* ??? May not be necessary.  Docs are unclear as to whether transfer
673     	   length field is ignored or whether it should be set to the total
674     	   number of bytes of the transfer.  */
675     	mscp->transfer_data_length = transfer_length;
676     }
677     
678     int ultrastor_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
679     {
680         register struct mscp *my_mscp;
681     #if ULTRASTOR_MAX_CMDS > 1
682         int mscp_index;
683     #endif
684         unsigned int status;
685         unsigned long flags;
686     
687         /* Next test is for debugging; "can't happen" */
688         if ((config.mscp_free & ((1U << ULTRASTOR_MAX_CMDS) - 1)) == 0)
689     	panic("ultrastor_queuecommand: no free MSCP\n");
690         mscp_index = find_and_clear_bit_16(&config.mscp_free);
691     
692         /* Has the command been aborted?  */
693         if (xchgb(0xff, &config.aborted[mscp_index]) != 0)
694           {
695     	status = DID_ABORT << 16;
696     	goto aborted;
697           }
698     
699         my_mscp = &config.mscp[mscp_index];
700     
701     #if 1
702         /* This way is faster.  */
703         *(unsigned char *)my_mscp = OP_SCSI | (DTD_SCSI << 3);
704     #else
705         my_mscp->opcode = OP_SCSI;
706         my_mscp->xdir = DTD_SCSI;
707         my_mscp->dcn = FALSE;
708     #endif
709         /* Tape drives don't work properly if the cache is used.  The SCSI
710            READ command for a tape doesn't have a block offset, and the adapter
711            incorrectly assumes that all reads from the tape read the same
712            blocks.  Results will depend on read buffer size and other disk
713            activity. 
714     
715            ???  Which other device types should never use the cache?   */
716         my_mscp->ca = SCpnt->device->type != TYPE_TAPE;
717         my_mscp->target_id = SCpnt->target;
718         my_mscp->ch_no = 0;
719         my_mscp->lun = SCpnt->lun;
720         if (SCpnt->use_sg) {
721     	/* Set scatter/gather flag in SCSI command packet */
722     	my_mscp->sg = TRUE;
723     	build_sg_list(my_mscp, SCpnt);
724         } else {
725     	/* Unset scatter/gather flag in SCSI command packet */
726     	my_mscp->sg = FALSE;
727     	my_mscp->transfer_data = virt_to_bus(SCpnt->request_buffer);
728     	my_mscp->transfer_data_length = SCpnt->request_bufflen;
729         }
730         my_mscp->command_link = 0;		/*???*/
731         my_mscp->scsi_command_link_id = 0;	/*???*/
732         my_mscp->length_of_sense_byte = sizeof SCpnt->sense_buffer;
733         my_mscp->length_of_scsi_cdbs = SCpnt->cmd_len;
734         memcpy(my_mscp->scsi_cdbs, SCpnt->cmnd, my_mscp->length_of_scsi_cdbs);
735         my_mscp->adapter_status = 0;
736         my_mscp->target_status = 0;
737         my_mscp->sense_data = virt_to_bus(&SCpnt->sense_buffer);
738         my_mscp->done = done;
739         my_mscp->SCint = SCpnt;
740         SCpnt->host_scribble = (unsigned char *)my_mscp;
741     
742         /* Find free OGM slot.  On 24F, look for OGM status byte == 0.
743            On 14F and 34F, wait for local interrupt pending flag to clear.  */
744     
745       retry:
746         if (config.slot)
747     	while (inb(config.ogm_address - 1) != 0 &&
748     	       config.aborted[mscp_index] == 0xff) barrier();
749     
750         /* else??? */
751     
752         while ((inb(LCL_DOORBELL_INTR(config.doorbell_address)) & 
753     	    (config.slot ? 2 : 1)) 
754     	   && config.aborted[mscp_index] == 0xff) barrier();
755     
756         /* To avoid race conditions, make the code to write to the adapter
757            atomic.  This simplifies the abort code.  */
758     
759         save_flags(flags);
760         cli();
761     
762         if (inb(LCL_DOORBELL_INTR(config.doorbell_address)) &
763     	(config.slot ? 2 : 1))
764           {
765           restore_flags(flags);
766           goto retry;
767           }
768     
769         status = xchgb(0, &config.aborted[mscp_index]);
770         if (status != 0xff) {
771     	restore_flags(flags);
772     
773     #if ULTRASTOR_DEBUG & (UD_COMMAND | UD_ABORT)
774     	printk("USx4F: queuecommand: aborted\n");
775     #if ULTRASTOR_MAX_CMDS > 1
776     	log_ultrastor_abort(&config, mscp_index);
777     #endif
778     #endif
779     	status <<= 16;
780     
781           aborted:
782     	set_bit(mscp_index, &config.mscp_free);
783     	/* If the driver queues commands, call the done proc here.  Otherwise
784     	   return an error.  */
785     #if ULTRASTOR_MAX_CMDS > 1
786     	SCpnt->result = status;
787     	done(SCpnt);
788     	return 0;
789     #else
790     	return status;
791     #endif
792         }
793     
794         /* Store pointer in OGM address bytes */
795         outl(virt_to_bus(my_mscp), config.ogm_address);
796     
797         /* Issue OGM interrupt */
798         if (config.slot) {
799     	/* Write OGM command register on 24F */
800     	outb(1, config.ogm_address - 1);
801     	outb(0x2, LCL_DOORBELL_INTR(config.doorbell_address));
802         } else {
803     	outb(0x1, LCL_DOORBELL_INTR(config.doorbell_address));
804         }
805     
806         restore_flags(flags);
807     
808     #if (ULTRASTOR_DEBUG & UD_COMMAND)
809         printk("USx4F: queuecommand: returning\n");
810     #endif
811     
812         return 0;
813     }
814     
815     /* This code must deal with 2 cases:
816     
817        1. The command has not been written to the OGM.  In this case, set
818        the abort flag and return.
819     
820        2. The command has been written to the OGM and is stuck somewhere in
821        the adapter.
822     
823        2a.  On a 24F, ask the adapter to abort the command.  It will interrupt
824        when it does.
825     
826        2b.  Call the command's done procedure.
827     
828      */
829     
830     int ultrastor_abort(Scsi_Cmnd *SCpnt)
831     {
832     #if ULTRASTOR_DEBUG & UD_ABORT
833         char out[108];
834         unsigned char icm_status = 0, ogm_status = 0;
835         unsigned int icm_addr = 0, ogm_addr = 0;
836     #endif
837         unsigned int mscp_index;
838         unsigned char old_aborted;
839         void (*done)(Scsi_Cmnd *);
840     
841         if(config.slot) 
842           return SCSI_ABORT_SNOOZE;  /* Do not attempt an abort for the 24f */
843     
844         /* Simple consistency checking */
845         if(!SCpnt->host_scribble)
846           return SCSI_ABORT_NOT_RUNNING;
847     
848         mscp_index = ((struct mscp *)SCpnt->host_scribble) - config.mscp;
849         if (mscp_index >= ULTRASTOR_MAX_CMDS)
850     	panic("Ux4F aborting invalid MSCP");
851     
852     #if ULTRASTOR_DEBUG & UD_ABORT
853         if (config.slot)
854           {
855     	int port0 = (config.slot << 12) | 0xc80;
856     	int i;
857     	unsigned long flags;
858     	save_flags(flags);
859     	cli();
860     	strcpy(out, "OGM %d:%x ICM %d:%x ports:  ");
861     	for (i = 0; i < 16; i++)
862     	  {
863     	    unsigned char p = inb(port0 + i);
864     	    out[28 + i * 3] = "0123456789abcdef"[p >> 4];
865     	    out[29 + i * 3] = "0123456789abcdef"[p & 15];
866     	    out[30 + i * 3] = ' ';
867     	  }
868     	out[28 + i * 3] = '\n';
869     	out[29 + i * 3] = 0;
870     	ogm_status = inb(port0 + 22);
871     	ogm_addr = (unsigned int)bus_to_virt(inl(port0 + 23));
872     	icm_status = inb(port0 + 27);
873     	icm_addr = (unsigned int)bus_to_virt(inl(port0 + 28));
874     	restore_flags(flags);
875           }
876     
877         /* First check to see if an interrupt is pending.  I suspect the SiS
878            chipset loses interrupts.  (I also suspect is mangles data, but
879            one bug at a time... */
880         if (config.slot ? inb(config.icm_address - 1) == 2 :
881     	(inb(SYS_DOORBELL_INTR(config.doorbell_address)) & 1))
882           {
883     	unsigned long flags;
884     	save_flags(flags);
885     	printk("Ux4F: abort while completed command pending\n");
886     	restore_flags(flags);
887     	cli();
888     	ultrastor_interrupt(0, NULL, NULL);
889     	restore_flags(flags);
890     	return SCSI_ABORT_SUCCESS;  /* FIXME - is this correct? -ERY */
891           }
892     #endif
893     
894         old_aborted = xchgb(DID_ABORT, &config.aborted[mscp_index]);
895     
896         /* aborted == 0xff is the signal that queuecommand has not yet sent
897            the command.  It will notice the new abort flag and fail.  */
898         if (old_aborted == 0xff)
899     	return SCSI_ABORT_SUCCESS;
900     
901         /* On 24F, send an abort MSCP request.  The adapter will interrupt
902            and the interrupt handler will call done.  */
903         if (config.slot && inb(config.ogm_address - 1) == 0)
904           {
905     	unsigned long flags;
906     
907     	save_flags(flags);
908     	cli();
909     	outl(virt_to_bus(&config.mscp[mscp_index]), config.ogm_address);
910     	inb(0xc80);	/* delay */
911     	outb(0x80, config.ogm_address - 1);
912     	outb(0x2, LCL_DOORBELL_INTR(config.doorbell_address));
913     #if ULTRASTOR_DEBUG & UD_ABORT
914     	log_ultrastor_abort(&config, mscp_index);
915     	printk(out, ogm_status, ogm_addr, icm_status, icm_addr);
916     #endif
917     	restore_flags(flags);
918     	return SCSI_ABORT_PENDING;
919           }
920     
921     #if ULTRASTOR_DEBUG & UD_ABORT
922         log_ultrastor_abort(&config, mscp_index);
923     #endif
924     
925         /* Can't request a graceful abort.  Either this is not a 24F or
926            the OGM is busy.  Don't free the command -- the adapter might
927            still be using it.  Setting SCint = 0 causes the interrupt
928            handler to ignore the command.  */
929     
930         /* FIXME - devices that implement soft resets will still be running
931            the command after a bus reset.  We would probably rather leave
932            the command in the queue.  The upper level code will automatically
933            leave the command in the active state instead of requeueing it. ERY */
934     
935     #if ULTRASTOR_DEBUG & UD_ABORT
936         if (config.mscp[mscp_index].SCint != SCpnt)
937     	printk("abort: command mismatch, %p != %p\n",
938     	       config.mscp[mscp_index].SCint, SCpnt);
939     #endif
940         if (config.mscp[mscp_index].SCint == 0)
941     	return SCSI_ABORT_NOT_RUNNING;
942     
943         if (config.mscp[mscp_index].SCint != SCpnt) panic("Bad abort");
944         config.mscp[mscp_index].SCint = 0;
945         done = config.mscp[mscp_index].done;
946         config.mscp[mscp_index].done = 0;
947         SCpnt->result = DID_ABORT << 16;
948         /* I worry about reentrancy in scsi.c  */
949         done(SCpnt);
950     
951         /* Need to set a timeout here in case command never completes.  */
952         return SCSI_ABORT_SUCCESS;
953     }
954     
955     int ultrastor_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
956     {
957         unsigned long flags;
958         register int i;
959     #if (ULTRASTOR_DEBUG & UD_RESET)
960         printk("US14F: reset: called\n");
961     #endif
962     
963         if(config.slot)
964           return SCSI_RESET_PUNT;  /* Do not attempt a reset for the 24f */
965     
966         save_flags(flags);
967         cli();
968     
969         /* Reset the adapter and SCSI bus.  The SCSI bus reset can be
970            inhibited by clearing ultrastor_bus_reset before probe.  */
971         outb(0xc0, LCL_DOORBELL_INTR(config.doorbell_address));
972         if (config.slot)
973           {
974     	outb(0, config.ogm_address - 1);
975     	outb(0, config.icm_address - 1);
976           }
977     
978     #if ULTRASTOR_MAX_CMDS == 1
979         if (config.mscp_busy && config.mscp->done && config.mscp->SCint)
980           {
981     	config.mscp->SCint->result = DID_RESET << 16;
982     	config.mscp->done(config.mscp->SCint);
983           }
984         config.mscp->SCint = 0;
985     #else
986         for (i = 0; i < ULTRASTOR_MAX_CMDS; i++)
987           {
988     	if (! (config.mscp_free & (1 << i)) &&
989     	    config.mscp[i].done && config.mscp[i].SCint)
990     	  {
991     	    config.mscp[i].SCint->result = DID_RESET << 16;
992     	    config.mscp[i].done(config.mscp[i].SCint);
993     	    config.mscp[i].done = 0;
994     	  }
995     	config.mscp[i].SCint = 0;
996           }
997     #endif
998     
999         /* FIXME - if the device implements soft resets, then the command
1000            will still be running.  ERY */
1001     
1002         memset((unsigned char *)config.aborted, 0, sizeof config.aborted);
1003     #if ULTRASTOR_MAX_CMDS == 1
1004         config.mscp_busy = 0;
1005     #else
1006         config.mscp_free = ~0;
1007     #endif
1008     
1009         restore_flags(flags);
1010         return SCSI_RESET_SUCCESS;
1011     
1012     }
1013     
1014     int ultrastor_biosparam(Disk * disk, kdev_t dev, int * dkinfo)
1015     {
1016         int size = disk->capacity;
1017         unsigned int s = config.heads * config.sectors;
1018     
1019         dkinfo[0] = config.heads;
1020         dkinfo[1] = config.sectors;
1021         dkinfo[2] = size / s;	/* Ignore partial cylinders */
1022     #if 0
1023         if (dkinfo[2] > 1024)
1024     	dkinfo[2] = 1024;
1025     #endif
1026         return 0;
1027     }
1028     
1029     static void ultrastor_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1030     {
1031         unsigned int status;
1032     #if ULTRASTOR_MAX_CMDS > 1
1033         unsigned int mscp_index;
1034     #endif
1035         register struct mscp *mscp;
1036         void (*done)(Scsi_Cmnd *);
1037         Scsi_Cmnd *SCtmp;
1038     
1039     #if ULTRASTOR_MAX_CMDS == 1
1040         mscp = &config.mscp[0];
1041     #else
1042         mscp = (struct mscp *)bus_to_virt(inl(config.icm_address));
1043         mscp_index = mscp - config.mscp;
1044         if (mscp_index >= ULTRASTOR_MAX_CMDS) {
1045     	printk("Ux4F interrupt: bad MSCP address %x\n", (unsigned int) mscp);
1046     	/* A command has been lost.  Reset and report an error
1047     	   for all commands.  */
1048     	ultrastor_reset(NULL, 0);
1049     	return;
1050         }
1051     #endif
1052     
1053         /* Clean ICM slot (set ICMINT bit to 0) */
1054         if (config.slot) {
1055     	unsigned char icm_status = inb(config.icm_address - 1);
1056     #if ULTRASTOR_DEBUG & (UD_INTERRUPT|UD_ERROR|UD_ABORT)
1057     	if (icm_status != 1 && icm_status != 2)
1058     	    printk("US24F: ICM status %x for MSCP %d (%x)\n", icm_status,
1059     		   mscp_index, (unsigned int) mscp);
1060     #endif
1061     	/* The manual says clear interrupt then write 0 to ICM status.
1062     	   This seems backwards, but I'll do it anyway.  --jfc */
1063     	outb(2, SYS_DOORBELL_INTR(config.doorbell_address));
1064     	outb(0, config.icm_address - 1);
1065     	if (icm_status == 4) {
1066     	    printk("UltraStor abort command failed\n");
1067     	    return;
1068     	}
1069     	if (icm_status == 3) {
1070     	    void (*done)(Scsi_Cmnd *) = mscp->done;
1071     	    if (done) {
1072     		mscp->done = 0;
1073     		mscp->SCint->result = DID_ABORT << 16;
1074     		done(mscp->SCint);
1075     	    }
1076     	    return;
1077     	}
1078         } else {
1079     	outb(1, SYS_DOORBELL_INTR(config.doorbell_address));
1080         }
1081     
1082         SCtmp = mscp->SCint;
1083         mscp->SCint = NULL;
1084     
1085         if (SCtmp == 0)
1086           {
1087     #if ULTRASTOR_DEBUG & (UD_ABORT|UD_INTERRUPT)
1088     	printk("MSCP %d (%x): no command\n", mscp_index, (unsigned int) mscp);
1089     #endif	
1090     #if ULTRASTOR_MAX_CMDS == 1
1091     	config.mscp_busy = FALSE;
1092     #else
1093     	set_bit(mscp_index, &config.mscp_free);
1094     #endif
1095     	config.aborted[mscp_index] = 0;
1096     	return;
1097           }
1098     
1099         /* Save done locally and zero before calling.  This is needed as
1100            once we call done, we may get another command queued before this
1101            interrupt service routine can return. */
1102         done = mscp->done;
1103         mscp->done = 0;
1104     
1105         /* Let the higher levels know that we're done */
1106         switch (mscp->adapter_status)
1107           {
1108           case 0:
1109     	status = DID_OK << 16;
1110     	break;
1111           case 0x01:	/* invalid command */
1112           case 0x02:	/* invalid parameters */
1113           case 0x03:	/* invalid data list */
1114           default:
1115     	status = DID_ERROR << 16;
1116     	break;
1117           case 0x84:	/* SCSI bus abort */
1118     	status = DID_ABORT << 16;
1119     	break;
1120           case 0x91:
1121     	status = DID_TIME_OUT << 16;
1122     	break;
1123           }
1124     
1125         SCtmp->result = status | mscp->target_status;
1126     
1127         SCtmp->host_scribble = 0;
1128     
1129         /* Free up mscp block for next command */
1130     #if ULTRASTOR_MAX_CMDS == 1
1131         config.mscp_busy = FALSE;
1132     #else
1133         set_bit(mscp_index, &config.mscp_free);
1134     #endif
1135     
1136     #if ULTRASTOR_DEBUG & (UD_ABORT|UD_INTERRUPT)
1137         if (config.aborted[mscp_index])
1138     	printk("Ux4 interrupt: MSCP %d (%x) aborted = %d\n",
1139     	       mscp_index, (unsigned int) mscp, config.aborted[mscp_index]);
1140     #endif
1141         config.aborted[mscp_index] = 0;
1142     
1143         if (done)
1144     	done(SCtmp);
1145         else
1146     	printk("US14F: interrupt: unexpected interrupt\n");
1147     
1148         if (config.slot ? inb(config.icm_address - 1) :
1149            (inb(SYS_DOORBELL_INTR(config.doorbell_address)) & 1))
1150     #if (ULTRASTOR_DEBUG & UD_MULTI_CMD)
1151           printk("Ux4F: multiple commands completed\n");
1152     #else
1153           ;
1154     #endif
1155     
1156     #if (ULTRASTOR_DEBUG & UD_INTERRUPT)
1157         printk("USx4F: interrupt: returning\n");
1158     #endif
1159     }
1160     
1161     static void do_ultrastor_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1162     {
1163         unsigned long flags;
1164     
1165         spin_lock_irqsave(&io_request_lock, flags);
1166         ultrastor_interrupt(irq, dev_id, regs);
1167         spin_unlock_irqrestore(&io_request_lock, flags);
1168     }
1169     
1170     /* Eventually this will go into an include file, but this will be later */
1171     static Scsi_Host_Template driver_template = ULTRASTOR_14F;
1172     
1173     #include "scsi_module.c"
1174