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