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
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.
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).
36 * The 14F, 24F, and 34F are supported by this driver.
38 * Places flagged with a triple question-mark are things which are either
39 * unfinished, questionable, or wrong.
42 /* Changes from version 1.11 alpha to 1.12
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.
48 * Caleb Epstein <cae@jpmorgan.com>
51 /* Changes from version 1.9 to 1.11
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.
57 * Fixes from Eric Youngdale to fix a few possible race conditions and
58 * several problems with bit testing operations (insufficient
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.
66 * Use check/snarf_region for 14f, 34f to avoid I/O space address conflicts.
69 /* Changes from version 1.8 to version 1.9
71 * 0.99.11 patches (cae@jpmorgan.com) */
73 /* Changes from version 1.7 to version 1.8
75 * Better error reporting.
78 /* Changes from version 1.6 to version 1.7
80 * Removed CSIR command code.
82 * Better race condition avoidance (xchgb function added).
84 * Set ICM and OGM status to zero at probe (24F)
86 * reset sends soft reset to UltraStor adapter
88 * reset adapter if adapter interrupts with an invalid MSCP address
90 * handle aborted command interrupt (24F)
94 /* Changes from version 1.5 to version 1.6:
96 * Read MSCP address from ICM _before_ clearing the interrupt flag.
97 * This fixes a race condition.
100 /* Changes from version 1.4 to version 1.5:
102 * Abort now calls done when multiple commands are enabled.
104 * Clear busy when aborted command finishes, not when abort is called.
106 * More debugging messages for aborts.
109 /* Changes from version 1.3 to version 1.4:
111 * Enable automatic request of sense data on error (requires newer version
112 * of scsi.c to be useful).
114 * Fix PORT_OVERRIDE for 14F.
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
120 * Boot time test to enable SCSI bus reset (defaults to not allowing reset).
122 * Fix test for OGM busy -- the busy bit is in different places on the 24F.
124 * Release ICM slot by clearing first byte on 24F.
127 #include <linux/module.h>
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>
137 #include <asm/bitops.h>
138 #include <asm/system.h>
141 #define ULTRASTOR_PRIVATE /* Get the private stuff from ultrastor.h */
142 #include <linux/blk.h>
145 #include "ultrastor.h"
147 #include<linux/stat.h>
152 #ifndef ULTRASTOR_DEBUG
153 #define ULTRASTOR_DEBUG (UD_ABORT|UD_CSIR|UD_RESET)
156 #define VERSION "1.12"
158 #define PACKED __attribute__((packed))
159 #define ALIGNED(x) __attribute__((aligned(x)))
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
168 unsigned int address;
169 unsigned int num_bytes;
173 /* MailBox SCSI Command Packet. Basic command structure for communicating
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 *);
199 ultrastor_sg_list sglist[ULTRASTOR_24F_MAX_SG]; /* use larger size for 24F */
203 /* Port addresses (relative to the base address) */
204 #define U14F_PRODUCT_ID(port) ((port) + 0x4)
205 #define CONFIG(port) ((port) + 0x6)
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)
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.
217 This structure also holds port addresses that are not at the same offset
220 This structure holds all data that must be duplicated to support multiple
223 static struct ultrastor_config
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;
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). */
242 #ifdef PRINT_U24F_VERSION
243 volatile int csir_done;
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.) */
250 #if ULTRASTOR_MAX_CMDS == 1
251 unsigned char mscp_busy;
253 unsigned short mscp_free;
255 volatile unsigned char aborted[ULTRASTOR_MAX_CMDS];
256 struct mscp mscp[ULTRASTOR_MAX_CMDS];
259 /* Set this to 1 to reset the SCSI bus on error. */
260 int ultrastor_bus_reset;
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,
269 /* Allowed IRQs for 14f */
270 static const unsigned char interrupt_table_14f[4] = { 15, 14, 11, 10 };
272 /* Allowed DMA channels for 14f (0 indicates reserved) */
273 static const unsigned char dma_channel_table_14f[4] = { 5, 6, 7, 0 };
275 /* Head/sector mappings allowed by 14f */
276 static const struct {
278 unsigned char sectors;
279 } mapping_table[4] = { { 16, 63 }, { 64, 32 }, { 64, 63 }, { 64, 32 } };
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,
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);
293 static inline int find_and_clear_bit_16(unsigned short *field)
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);
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
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
315 static inline unsigned char xchgb(unsigned char reg,
316 volatile unsigned char *mem)
318 __asm__ ("xchgb %0,%1" : "=q" (reg), "=m" (*mem) : "0" (reg));
322 #if ULTRASTOR_DEBUG & (UD_COMMAND | UD_ABORT)
324 static void log_ultrastor_abort(register struct ultrastor_config *config,
327 static char fmt[80] = "abort %d (%x); MSCP free pool: %x;";
333 for (i = 0; i < ULTRASTOR_MAX_CMDS; i++)
336 if (! (config->mscp_free & (1 << i)))
337 fmt[21 + i*2] = '0' + config->mscp[i].target_id;
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);
348 static int ultrastor_14f_detect(Scsi_Host_Template * tpnt)
351 unsigned char in_byte, version_byte = 0;
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;
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;
365 #if (ULTRASTOR_DEBUG & UD_DETECT)
366 printk("US14F: detect: called\n");
369 /* If a 24F has already been configured, don't look for a 14F. */
370 if (config.bios_segment)
374 if(!request_region(PORT_OVERRIDE, 0xc, "ultrastor")) {
375 printk("Ultrastor I/O space already in use\n");
378 config.port_address = PORT_OVERRIDE;
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];
385 #if (ULTRASTOR_DEBUG & UD_DETECT)
386 printk("US14F: detect: testing port address %03X\n", config.port_address);
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);
395 printk("US14F: detect: no adapter at port %03X\n", config.port_address);
399 goto out_release_port;
401 release_region(config.port_address, 0x0c);
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);
412 printk("US14F: detect: no adapter at port %03X\n", config.port_address);
416 goto out_release_port;
418 release_region(config.port_address, 0x0c);
422 version_byte = in_byte;
423 #ifndef PORT_OVERRIDE
426 if (i == ARRAY_SIZE(ultrastor_ports_14f)) {
427 # if (ULTRASTOR_DEBUG & UD_DETECT)
428 printk("US14F: detect: no port address found!\n");
430 /* all ports probed already released - we can just go straight out */
435 #if (ULTRASTOR_DEBUG & UD_DETECT)
436 printk("US14F: detect: adapter found at port address %03X\n",
437 config.port_address);
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));
444 /* All above tests passed, must be the right thing. Get some useful
447 /* Register the I/O space that we use */
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;
464 config.dma_channel = dma_channel_table_14f[config_1.dma_channel];
466 if (!config.bios_segment) {
467 #if (ULTRASTOR_DEBUG & UD_DETECT)
468 printk("US14F: detect: not detected.\n");
470 goto out_release_port;
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");
479 goto out_release_port;
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
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"
495 config.port_address, config.bios_segment, config.interrupt,
496 config.dma_channel, config.ha_scsi_id, config.subversion);
498 tpnt->this_id = config.ha_scsi_id;
499 tpnt->unchecked_isa_dma = (config.subversion != U34F);
501 #if ULTRASTOR_MAX_CMDS > 1
502 config.mscp_free = ~0;
505 if (request_irq(config.interrupt, do_ultrastor_interrupt, 0, "Ultrastor", NULL)) {
506 printk("Unable to allocate IRQ%u for UltraStor controller.\n",
508 goto out_release_port;
510 if (config.dma_channel && request_dma(config.dma_channel,"Ultrastor")) {
511 printk("Unable to allocate DMA channel %u for UltraStor controller.\n",
513 free_irq(config.interrupt, NULL);
514 goto out_release_port;
516 tpnt->sg_tablesize = ULTRASTOR_14F_MAX_SG;
517 printk("UltraStor driver version" VERSION ". Using %d SG lists.\n",
518 ULTRASTOR_14F_MAX_SG);
522 release_region(config.port_address, 0x0c);
526 static int ultrastor_24f_detect(Scsi_Host_Template * tpnt)
529 struct Scsi_Host * shpnt = NULL;
531 #if (ULTRASTOR_DEBUG & UD_DETECT)
532 printk("US24F: detect");
535 /* probe each EISA slot at slot address C80 */
536 for (i = 1; i < 15; i++)
538 unsigned char config_1, config_2;
539 unsigned short addr = (i << 12) | ULTRASTOR_24F_PORT;
541 if (inb(addr) != US24F_PRODUCT_ID_0 &&
542 inb(addr+1) != US24F_PRODUCT_ID_1 &&
543 inb(addr+2) != US24F_PRODUCT_ID_2)
546 config.revision = inb(addr+3);
548 if (! (inb(addr+4) & 1))
550 #if (ULTRASTOR_DEBUG & UD_DETECT)
551 printk("U24F: found disabled card in slot %u\n", i);
555 #if (ULTRASTOR_DEBUG & UD_DETECT)
556 printk("U24F: found card in slot %u\n", i);
558 config_1 = inb(addr + 5);
559 config.bios_segment = bios_segment_table[config_1 & 7];
560 switch(config_1 >> 4)
563 config.interrupt = 15;
566 config.interrupt = 14;
569 config.interrupt = 11;
572 config.interrupt = 10;
575 printk("U24F: invalid IRQ\n");
578 if (request_irq(config.interrupt, do_ultrastor_interrupt, 0, "Ultrastor", NULL))
580 printk("Unable to allocate IRQ%u for UltraStor controller.\n",
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"
599 " H/A SCSI ID: %u\n",
600 config.port_address, config.bios_segment,
601 config.interrupt, config.ha_scsi_id);
603 tpnt->this_id = config.ha_scsi_id;
604 tpnt->unchecked_isa_dma = 0;
605 tpnt->sg_tablesize = ULTRASTOR_24F_MAX_SG;
607 shpnt = scsi_register(tpnt, 0);
609 printk(KERN_WARNING "(ultrastor:) Could not register scsi device. Aborting registration.\n");
610 free_irq(config.interrupt, do_ultrastor_interrupt);
614 shpnt->irq = config.interrupt;
615 shpnt->dma_channel = config.dma_channel;
616 shpnt->io_port = config.port_address;
618 #if ULTRASTOR_MAX_CMDS > 1
619 config.mscp_free = ~0;
621 /* Mark ICM and OGM free */
622 outb(0, addr + 0x16);
623 outb(0, addr + 0x1B);
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",
636 int ultrastor_detect(Scsi_Host_Template * tpnt)
638 tpnt->proc_name = "ultrastor";
639 return ultrastor_14f_detect(tpnt) || ultrastor_24f_detect(tpnt);
642 const char *ultrastor_info(struct Scsi_Host * shpnt)
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,
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);
660 static inline void build_sg_list(register struct mscp *mscp, Scsi_Cmnd *SCpnt)
662 struct scatterlist *sl;
663 long transfer_length = 0;
666 sl = (struct scatterlist *) SCpnt->request_buffer;
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;
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;
681 int ultrastor_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
683 register struct mscp *my_mscp;
684 #if ULTRASTOR_MAX_CMDS > 1
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);
695 /* Has the command been aborted? */
696 if (xchgb(0xff, &config.aborted[mscp_index]) != 0)
698 status = DID_ABORT << 16;
702 my_mscp = &config.mscp[mscp_index];
705 /* This way is faster. */
706 *(unsigned char *)my_mscp = OP_SCSI | (DTD_SCSI << 3);
708 my_mscp->opcode = OP_SCSI;
709 my_mscp->xdir = DTD_SCSI;
710 my_mscp->dcn = FALSE;
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
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;
722 my_mscp->lun = SCpnt->lun;
724 /* Set scatter/gather flag in SCSI command packet */
726 build_sg_list(my_mscp, SCpnt);
728 /* Unset scatter/gather flag in SCSI command packet */
730 my_mscp->transfer_data = virt_to_bus(SCpnt->request_buffer);
731 my_mscp->transfer_data_length = SCpnt->request_bufflen;
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;
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. */
750 while (inb(config.ogm_address - 1) != 0 &&
751 config.aborted[mscp_index] == 0xff) barrier();
755 while ((inb(LCL_DOORBELL_INTR(config.doorbell_address)) &
756 (config.slot ? 2 : 1))
757 && config.aborted[mscp_index] == 0xff) barrier();
759 /* To avoid race conditions, make the code to write to the adapter
760 atomic. This simplifies the abort code. */
765 if (inb(LCL_DOORBELL_INTR(config.doorbell_address)) &
766 (config.slot ? 2 : 1))
768 restore_flags(flags);
772 status = xchgb(0, &config.aborted[mscp_index]);
773 if (status != 0xff) {
774 restore_flags(flags);
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);
785 set_bit(mscp_index, &config.mscp_free);
786 /* If the driver queues commands, call the done proc here. Otherwise
788 #if ULTRASTOR_MAX_CMDS > 1
789 SCpnt->result = status;
797 /* Store pointer in OGM address bytes */
798 outl(virt_to_bus(my_mscp), config.ogm_address);
800 /* Issue OGM interrupt */
802 /* Write OGM command register on 24F */
803 outb(1, config.ogm_address - 1);
804 outb(0x2, LCL_DOORBELL_INTR(config.doorbell_address));
806 outb(0x1, LCL_DOORBELL_INTR(config.doorbell_address));
809 restore_flags(flags);
811 #if (ULTRASTOR_DEBUG & UD_COMMAND)
812 printk("USx4F: queuecommand: returning\n");
818 /* This code must deal with 2 cases:
820 1. The command has not been written to the OGM. In this case, set
821 the abort flag and return.
823 2. The command has been written to the OGM and is stuck somewhere in
826 2a. On a 24F, ask the adapter to abort the command. It will interrupt
829 2b. Call the command's done procedure.
833 int ultrastor_abort(Scsi_Cmnd *SCpnt)
835 #if ULTRASTOR_DEBUG & UD_ABORT
837 unsigned char icm_status = 0, ogm_status = 0;
838 unsigned int icm_addr = 0, ogm_addr = 0;
840 unsigned int mscp_index;
841 unsigned char old_aborted;
842 void (*done)(Scsi_Cmnd *);
845 return SCSI_ABORT_SNOOZE; /* Do not attempt an abort for the 24f */
847 /* Simple consistency checking */
848 if(!SCpnt->host_scribble)
849 return SCSI_ABORT_NOT_RUNNING;
851 mscp_index = ((struct mscp *)SCpnt->host_scribble) - config.mscp;
852 if (mscp_index >= ULTRASTOR_MAX_CMDS)
853 panic("Ux4F aborting invalid MSCP");
855 #if ULTRASTOR_DEBUG & UD_ABORT
858 int port0 = (config.slot << 12) | 0xc80;
863 strcpy(out, "OGM %d:%x ICM %d:%x ports: ");
864 for (i = 0; i < 16; i++)
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] = ' ';
871 out[28 + i * 3] = '\n';
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);
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))
888 printk("Ux4F: abort while completed command pending\n");
889 restore_flags(flags);
891 ultrastor_interrupt(0, NULL, NULL);
892 restore_flags(flags);
893 return SCSI_ABORT_SUCCESS; /* FIXME - is this correct? -ERY */
897 old_aborted = xchgb(DID_ABORT, &config.aborted[mscp_index]);
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;
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)
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);
920 restore_flags(flags);
921 return SCSI_ABORT_PENDING;
924 #if ULTRASTOR_DEBUG & UD_ABORT
925 log_ultrastor_abort(&config, mscp_index);
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. */
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 */
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);
943 if (config.mscp[mscp_index].SCint == 0)
944 return SCSI_ABORT_NOT_RUNNING;
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 */
954 /* Need to set a timeout here in case command never completes. */
955 return SCSI_ABORT_SUCCESS;
958 int ultrastor_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
962 #if (ULTRASTOR_DEBUG & UD_RESET)
963 printk("US14F: reset: called\n");
967 return SCSI_RESET_PUNT; /* Do not attempt a reset for the 24f */
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));
977 outb(0, config.ogm_address - 1);
978 outb(0, config.icm_address - 1);
981 #if ULTRASTOR_MAX_CMDS == 1
982 if (config.mscp_busy && config.mscp->done && config.mscp->SCint)
984 config.mscp->SCint->result = DID_RESET << 16;
985 config.mscp->done(config.mscp->SCint);
987 config.mscp->SCint = 0;
989 for (i = 0; i < ULTRASTOR_MAX_CMDS; i++)
991 if (! (config.mscp_free & (1 << i)) &&
992 config.mscp[i].done && config.mscp[i].SCint)
994 config.mscp[i].SCint->result = DID_RESET << 16;
995 config.mscp[i].done(config.mscp[i].SCint);
996 config.mscp[i].done = 0;
998 config.mscp[i].SCint = 0;
1002 /* FIXME - if the device implements soft resets, then the command
1003 will still be running. ERY */
1005 memset((unsigned char *)config.aborted, 0, sizeof config.aborted);
1006 #if ULTRASTOR_MAX_CMDS == 1
1007 config.mscp_busy = 0;
1009 config.mscp_free = ~0;
1012 restore_flags(flags);
1013 return SCSI_RESET_SUCCESS;
1017 int ultrastor_biosparam(Disk * disk, kdev_t dev, int * dkinfo)
1019 int size = disk->capacity;
1020 unsigned int s = config.heads * config.sectors;
1022 dkinfo[0] = config.heads;
1023 dkinfo[1] = config.sectors;
1024 dkinfo[2] = size / s; /* Ignore partial cylinders */
1026 if (dkinfo[2] > 1024)
1032 static void ultrastor_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1034 unsigned int status;
1035 #if ULTRASTOR_MAX_CMDS > 1
1036 unsigned int mscp_index;
1038 register struct mscp *mscp;
1039 void (*done)(Scsi_Cmnd *);
1042 #if ULTRASTOR_MAX_CMDS == 1
1043 mscp = &config.mscp[0];
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);
1056 /* Clean ICM slot (set ICMINT bit to 0) */
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);
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");
1072 if (icm_status == 3) {
1073 void (*done)(Scsi_Cmnd *) = mscp->done;
1076 mscp->SCint->result = DID_ABORT << 16;
1082 outb(1, SYS_DOORBELL_INTR(config.doorbell_address));
1085 SCtmp = mscp->SCint;
1090 #if ULTRASTOR_DEBUG & (UD_ABORT|UD_INTERRUPT)
1091 printk("MSCP %d (%x): no command\n", mscp_index, (unsigned int) mscp);
1093 #if ULTRASTOR_MAX_CMDS == 1
1094 config.mscp_busy = FALSE;
1096 set_bit(mscp_index, &config.mscp_free);
1098 config.aborted[mscp_index] = 0;
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. */
1108 /* Let the higher levels know that we're done */
1109 switch (mscp->adapter_status)
1112 status = DID_OK << 16;
1114 case 0x01: /* invalid command */
1115 case 0x02: /* invalid parameters */
1116 case 0x03: /* invalid data list */
1118 status = DID_ERROR << 16;
1120 case 0x84: /* SCSI bus abort */
1121 status = DID_ABORT << 16;
1124 status = DID_TIME_OUT << 16;
1128 SCtmp->result = status | mscp->target_status;
1130 SCtmp->host_scribble = 0;
1132 /* Free up mscp block for next command */
1133 #if ULTRASTOR_MAX_CMDS == 1
1134 config.mscp_busy = FALSE;
1136 set_bit(mscp_index, &config.mscp_free);
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]);
1144 config.aborted[mscp_index] = 0;
1149 printk("US14F: interrupt: unexpected interrupt\n");
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");
1159 #if (ULTRASTOR_DEBUG & UD_INTERRUPT)
1160 printk("USx4F: interrupt: returning\n");
1164 static void do_ultrastor_interrupt(int irq, void *dev_id, struct pt_regs *regs)
1166 unsigned long flags;
1168 spin_lock_irqsave(&io_request_lock, flags);
1169 ultrastor_interrupt(irq, dev_id, regs);
1170 spin_unlock_irqrestore(&io_request_lock, flags);
1173 MODULE_LICENSE("GPL");
1175 /* Eventually this will go into an include file, but this will be later */
1176 static Scsi_Host_Template driver_template = ULTRASTOR_14F;
1178 #include "scsi_module.c"