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