Linux-2.6.12-rc2
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / atari_scsi.c
blobaf8adb629b33eba5cbbbb99e000b81710c3731c0
1 /*
2 * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port
4 * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
6 * Loosely based on the work of Robert De Vries' team and added:
7 * - working real DMA
8 * - Falcon support (untested yet!) ++bjoern fixed and now it works
9 * - lots of extensions and bug fixes.
11 * This file is subject to the terms and conditions of the GNU General Public
12 * License. See the file COPYING in the main directory of this archive
13 * for more details.
18 /**************************************************************************/
19 /* */
20 /* Notes for Falcon SCSI: */
21 /* ---------------------- */
22 /* */
23 /* Since the Falcon SCSI uses the ST-DMA chip, that is shared among */
24 /* several device drivers, locking and unlocking the access to this */
25 /* chip is required. But locking is not possible from an interrupt, */
26 /* since it puts the process to sleep if the lock is not available. */
27 /* This prevents "late" locking of the DMA chip, i.e. locking it just */
28 /* before using it, since in case of disconnection-reconnection */
29 /* commands, the DMA is started from the reselection interrupt. */
30 /* */
31 /* Two possible schemes for ST-DMA-locking would be: */
32 /* 1) The lock is taken for each command separately and disconnecting */
33 /* is forbidden (i.e. can_queue = 1). */
34 /* 2) The DMA chip is locked when the first command comes in and */
35 /* released when the last command is finished and all queues are */
36 /* empty. */
37 /* The first alternative would result in bad performance, since the */
38 /* interleaving of commands would not be used. The second is unfair to */
39 /* other drivers using the ST-DMA, because the queues will seldom be */
40 /* totally empty if there is a lot of disk traffic. */
41 /* */
42 /* For this reasons I decided to employ a more elaborate scheme: */
43 /* - First, we give up the lock every time we can (for fairness), this */
44 /* means every time a command finishes and there are no other commands */
45 /* on the disconnected queue. */
46 /* - If there are others waiting to lock the DMA chip, we stop */
47 /* issuing commands, i.e. moving them onto the issue queue. */
48 /* Because of that, the disconnected queue will run empty in a */
49 /* while. Instead we go to sleep on a 'fairness_queue'. */
50 /* - If the lock is released, all processes waiting on the fairness */
51 /* queue will be woken. The first of them tries to re-lock the DMA, */
52 /* the others wait for the first to finish this task. After that, */
53 /* they can all run on and do their commands... */
54 /* This sounds complicated (and it is it :-(), but it seems to be a */
55 /* good compromise between fairness and performance: As long as no one */
56 /* else wants to work with the ST-DMA chip, SCSI can go along as */
57 /* usual. If now someone else comes, this behaviour is changed to a */
58 /* "fairness mode": just already initiated commands are finished and */
59 /* then the lock is released. The other one waiting will probably win */
60 /* the race for locking the DMA, since it was waiting for longer. And */
61 /* after it has finished, SCSI can go ahead again. Finally: I hope I */
62 /* have not produced any deadlock possibilities! */
63 /* */
64 /**************************************************************************/
68 #include <linux/config.h>
69 #include <linux/module.h>
71 #define NDEBUG (0)
73 #define NDEBUG_ABORT 0x800000
74 #define NDEBUG_TAGS 0x1000000
75 #define NDEBUG_MERGING 0x2000000
77 #define AUTOSENSE
78 /* For the Atari version, use only polled IO or REAL_DMA */
79 #define REAL_DMA
80 /* Support tagged queuing? (on devices that are able to... :-) */
81 #define SUPPORT_TAGS
82 #define MAX_TAGS 32
84 #include <linux/types.h>
85 #include <linux/stddef.h>
86 #include <linux/ctype.h>
87 #include <linux/delay.h>
88 #include <linux/mm.h>
89 #include <linux/blkdev.h>
90 #include <linux/sched.h>
91 #include <linux/interrupt.h>
92 #include <linux/init.h>
93 #include <linux/nvram.h>
94 #include <linux/bitops.h>
96 #include <asm/setup.h>
97 #include <asm/atarihw.h>
98 #include <asm/atariints.h>
99 #include <asm/page.h>
100 #include <asm/pgtable.h>
101 #include <asm/irq.h>
102 #include <asm/traps.h>
104 #include "scsi.h"
105 #include <scsi/scsi_host.h>
106 #include "atari_scsi.h"
107 #include "NCR5380.h"
108 #include <asm/atari_stdma.h>
109 #include <asm/atari_stram.h>
110 #include <asm/io.h>
112 #include <linux/stat.h>
114 #define IS_A_TT() ATARIHW_PRESENT(TT_SCSI)
116 #define SCSI_DMA_WRITE_P(elt,val) \
117 do { \
118 unsigned long v = val; \
119 tt_scsi_dma.elt##_lo = v & 0xff; \
120 v >>= 8; \
121 tt_scsi_dma.elt##_lmd = v & 0xff; \
122 v >>= 8; \
123 tt_scsi_dma.elt##_hmd = v & 0xff; \
124 v >>= 8; \
125 tt_scsi_dma.elt##_hi = v & 0xff; \
126 } while(0)
128 #define SCSI_DMA_READ_P(elt) \
129 (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) | \
130 (unsigned long)tt_scsi_dma.elt##_hmd) << 8) | \
131 (unsigned long)tt_scsi_dma.elt##_lmd) << 8) | \
132 (unsigned long)tt_scsi_dma.elt##_lo)
135 static inline void SCSI_DMA_SETADR(unsigned long adr)
137 st_dma.dma_lo = (unsigned char)adr;
138 MFPDELAY();
139 adr >>= 8;
140 st_dma.dma_md = (unsigned char)adr;
141 MFPDELAY();
142 adr >>= 8;
143 st_dma.dma_hi = (unsigned char)adr;
144 MFPDELAY();
147 static inline unsigned long SCSI_DMA_GETADR(void)
149 unsigned long adr;
150 adr = st_dma.dma_lo;
151 MFPDELAY();
152 adr |= (st_dma.dma_md & 0xff) << 8;
153 MFPDELAY();
154 adr |= (st_dma.dma_hi & 0xff) << 16;
155 MFPDELAY();
156 return adr;
159 static inline void ENABLE_IRQ(void)
161 if (IS_A_TT())
162 atari_enable_irq(IRQ_TT_MFP_SCSI);
163 else
164 atari_enable_irq(IRQ_MFP_FSCSI);
167 static inline void DISABLE_IRQ(void)
169 if (IS_A_TT())
170 atari_disable_irq(IRQ_TT_MFP_SCSI);
171 else
172 atari_disable_irq(IRQ_MFP_FSCSI);
176 #define HOSTDATA_DMALEN (((struct NCR5380_hostdata *) \
177 (atari_scsi_host->hostdata))->dma_len)
179 /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
180 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
181 * need ten times the standard value... */
182 #ifndef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
183 #define AFTER_RESET_DELAY (HZ/2)
184 #else
185 #define AFTER_RESET_DELAY (5*HZ/2)
186 #endif
188 /***************************** Prototypes *****************************/
190 #ifdef REAL_DMA
191 static int scsi_dma_is_ignored_buserr( unsigned char dma_stat );
192 static void atari_scsi_fetch_restbytes( void );
193 static long atari_scsi_dma_residual( struct Scsi_Host *instance );
194 static int falcon_classify_cmd( Scsi_Cmnd *cmd );
195 static unsigned long atari_dma_xfer_len( unsigned long wanted_len,
196 Scsi_Cmnd *cmd, int write_flag );
197 #endif
198 static irqreturn_t scsi_tt_intr( int irq, void *dummy, struct pt_regs *fp);
199 static irqreturn_t scsi_falcon_intr( int irq, void *dummy, struct pt_regs *fp);
200 static void falcon_release_lock_if_possible( struct NCR5380_hostdata *
201 hostdata );
202 static void falcon_get_lock( void );
203 #ifdef CONFIG_ATARI_SCSI_RESET_BOOT
204 static void atari_scsi_reset_boot( void );
205 #endif
206 static unsigned char atari_scsi_tt_reg_read( unsigned char reg );
207 static void atari_scsi_tt_reg_write( unsigned char reg, unsigned char value);
208 static unsigned char atari_scsi_falcon_reg_read( unsigned char reg );
209 static void atari_scsi_falcon_reg_write( unsigned char reg, unsigned char value );
211 /************************* End of Prototypes **************************/
214 static struct Scsi_Host *atari_scsi_host = NULL;
215 static unsigned char (*atari_scsi_reg_read)( unsigned char reg );
216 static void (*atari_scsi_reg_write)( unsigned char reg, unsigned char value );
218 #ifdef REAL_DMA
219 static unsigned long atari_dma_residual, atari_dma_startaddr;
220 static short atari_dma_active;
221 /* pointer to the dribble buffer */
222 static char *atari_dma_buffer = NULL;
223 /* precalculated physical address of the dribble buffer */
224 static unsigned long atari_dma_phys_buffer;
225 /* != 0 tells the Falcon int handler to copy data from the dribble buffer */
226 static char *atari_dma_orig_addr;
227 /* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
228 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
229 * cases where requests to physical contiguous buffers have been merged, this
230 * request is <= 4k (one page). So I don't think we have to split transfers
231 * just due to this buffer size...
233 #define STRAM_BUFFER_SIZE (4096)
234 /* mask for address bits that can't be used with the ST-DMA */
235 static unsigned long atari_dma_stram_mask;
236 #define STRAM_ADDR(a) (((a) & atari_dma_stram_mask) == 0)
237 /* number of bytes to cut from a transfer to handle NCR overruns */
238 static int atari_read_overruns = 0;
239 #endif
241 static int setup_can_queue = -1;
242 MODULE_PARM(setup_can_queue, "i");
243 static int setup_cmd_per_lun = -1;
244 MODULE_PARM(setup_cmd_per_lun, "i");
245 static int setup_sg_tablesize = -1;
246 MODULE_PARM(setup_sg_tablesize, "i");
247 #ifdef SUPPORT_TAGS
248 static int setup_use_tagged_queuing = -1;
249 MODULE_PARM(setup_use_tagged_queuing, "i");
250 #endif
251 static int setup_hostid = -1;
252 MODULE_PARM(setup_hostid, "i");
255 #if defined(CONFIG_TT_DMA_EMUL)
256 #include "atari_dma_emul.c"
257 #endif
259 #if defined(REAL_DMA)
261 static int scsi_dma_is_ignored_buserr( unsigned char dma_stat )
263 int i;
264 unsigned long addr = SCSI_DMA_READ_P( dma_addr ), end_addr;
266 if (dma_stat & 0x01) {
268 /* A bus error happens when DMA-ing from the last page of a
269 * physical memory chunk (DMA prefetch!), but that doesn't hurt.
270 * Check for this case:
273 for( i = 0; i < m68k_num_memory; ++i ) {
274 end_addr = m68k_memory[i].addr +
275 m68k_memory[i].size;
276 if (end_addr <= addr && addr <= end_addr + 4)
277 return( 1 );
280 return( 0 );
284 #if 0
285 /* Dead code... wasn't called anyway :-) and causes some trouble, because at
286 * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has
287 * to clear the DMA int pending bit before it allows other level 6 interrupts.
289 static void scsi_dma_buserr (int irq, void *dummy, struct pt_regs *fp)
291 unsigned char dma_stat = tt_scsi_dma.dma_ctrl;
293 /* Don't do anything if a NCR interrupt is pending. Probably it's just
294 * masked... */
295 if (atari_irq_pending( IRQ_TT_MFP_SCSI ))
296 return;
298 printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n",
299 SCSI_DMA_READ_P(dma_addr), dma_stat, SCSI_DMA_READ_P(dma_cnt));
300 if (dma_stat & 0x80) {
301 if (!scsi_dma_is_ignored_buserr( dma_stat ))
302 printk( "SCSI DMA bus error -- bad DMA programming!\n" );
304 else {
305 /* Under normal circumstances we never should get to this point,
306 * since both interrupts are triggered simultaneously and the 5380
307 * int has higher priority. When this irq is handled, that DMA
308 * interrupt is cleared. So a warning message is printed here.
310 printk( "SCSI DMA intr ?? -- this shouldn't happen!\n" );
313 #endif
315 #endif
318 static irqreturn_t scsi_tt_intr (int irq, void *dummy, struct pt_regs *fp)
320 #ifdef REAL_DMA
321 int dma_stat;
323 dma_stat = tt_scsi_dma.dma_ctrl;
325 INT_PRINTK("scsi%d: NCR5380 interrupt, DMA status = %02x\n",
326 atari_scsi_host->host_no, dma_stat & 0xff);
328 /* Look if it was the DMA that has interrupted: First possibility
329 * is that a bus error occurred...
331 if (dma_stat & 0x80) {
332 if (!scsi_dma_is_ignored_buserr( dma_stat )) {
333 printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
334 SCSI_DMA_READ_P(dma_addr));
335 printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
339 /* If the DMA is active but not finished, we have the case
340 * that some other 5380 interrupt occurred within the DMA transfer.
341 * This means we have residual bytes, if the desired end address
342 * is not yet reached. Maybe we have to fetch some bytes from the
343 * rest data register, too. The residual must be calculated from
344 * the address pointer, not the counter register, because only the
345 * addr reg counts bytes not yet written and pending in the rest
346 * data reg!
348 if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
349 atari_dma_residual = HOSTDATA_DMALEN - (SCSI_DMA_READ_P( dma_addr ) -
350 atari_dma_startaddr);
352 DMA_PRINTK("SCSI DMA: There are %ld residual bytes.\n",
353 atari_dma_residual);
355 if ((signed int)atari_dma_residual < 0)
356 atari_dma_residual = 0;
357 if ((dma_stat & 1) == 0) {
358 /* After read operations, we maybe have to
359 transport some rest bytes */
360 atari_scsi_fetch_restbytes();
362 else {
363 /* There seems to be a nasty bug in some SCSI-DMA/NCR
364 combinations: If a target disconnects while a write
365 operation is going on, the address register of the
366 DMA may be a few bytes farer than it actually read.
367 This is probably due to DMA prefetching and a delay
368 between DMA and NCR. Experiments showed that the
369 dma_addr is 9 bytes to high, but this could vary.
370 The problem is, that the residual is thus calculated
371 wrong and the next transfer will start behind where
372 it should. So we round up the residual to the next
373 multiple of a sector size, if it isn't already a
374 multiple and the originally expected transfer size
375 was. The latter condition is there to ensure that
376 the correction is taken only for "real" data
377 transfers and not for, e.g., the parameters of some
378 other command. These shouldn't disconnect anyway.
380 if (atari_dma_residual & 0x1ff) {
381 DMA_PRINTK("SCSI DMA: DMA bug corrected, "
382 "difference %ld bytes\n",
383 512 - (atari_dma_residual & 0x1ff));
384 atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
387 tt_scsi_dma.dma_ctrl = 0;
390 /* If the DMA is finished, fetch the rest bytes and turn it off */
391 if (dma_stat & 0x40) {
392 atari_dma_residual = 0;
393 if ((dma_stat & 1) == 0)
394 atari_scsi_fetch_restbytes();
395 tt_scsi_dma.dma_ctrl = 0;
398 #endif /* REAL_DMA */
400 NCR5380_intr (0, 0, 0);
402 #if 0
403 /* To be sure the int is not masked */
404 atari_enable_irq( IRQ_TT_MFP_SCSI );
405 #endif
406 return IRQ_HANDLED;
410 static irqreturn_t scsi_falcon_intr (int irq, void *dummy, struct pt_regs *fp)
412 #ifdef REAL_DMA
413 int dma_stat;
415 /* Turn off DMA and select sector counter register before
416 * accessing the status register (Atari recommendation!)
418 st_dma.dma_mode_status = 0x90;
419 dma_stat = st_dma.dma_mode_status;
421 /* Bit 0 indicates some error in the DMA process... don't know
422 * what happened exactly (no further docu).
424 if (!(dma_stat & 0x01)) {
425 /* DMA error */
426 printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
429 /* If the DMA was active, but now bit 1 is not clear, it is some
430 * other 5380 interrupt that finishes the DMA transfer. We have to
431 * calculate the number of residual bytes and give a warning if
432 * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
434 if (atari_dma_active && (dma_stat & 0x02)) {
435 unsigned long transferred;
437 transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
438 /* The ST-DMA address is incremented in 2-byte steps, but the
439 * data are written only in 16-byte chunks. If the number of
440 * transferred bytes is not divisible by 16, the remainder is
441 * lost somewhere in outer space.
443 if (transferred & 15)
444 printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
445 "ST-DMA fifo\n", transferred & 15);
447 atari_dma_residual = HOSTDATA_DMALEN - transferred;
448 DMA_PRINTK("SCSI DMA: There are %ld residual bytes.\n",
449 atari_dma_residual);
451 else
452 atari_dma_residual = 0;
453 atari_dma_active = 0;
455 if (atari_dma_orig_addr) {
456 /* If the dribble buffer was used on a read operation, copy the DMA-ed
457 * data to the original destination address.
459 memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
460 HOSTDATA_DMALEN - atari_dma_residual);
461 atari_dma_orig_addr = NULL;
464 #endif /* REAL_DMA */
466 NCR5380_intr (0, 0, 0);
467 return IRQ_HANDLED;
471 #ifdef REAL_DMA
472 static void atari_scsi_fetch_restbytes( void )
474 int nr;
475 char *src, *dst;
476 unsigned long phys_dst;
478 /* fetch rest bytes in the DMA register */
479 phys_dst = SCSI_DMA_READ_P(dma_addr);
480 nr = phys_dst & 3;
481 if (nr) {
482 /* there are 'nr' bytes left for the last long address
483 before the DMA pointer */
484 phys_dst ^= nr;
485 DMA_PRINTK("SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
486 nr, phys_dst);
487 /* The content of the DMA pointer is a physical address! */
488 dst = phys_to_virt(phys_dst);
489 DMA_PRINTK(" = virt addr %p\n", dst);
490 for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
491 *dst++ = *src++;
494 #endif /* REAL_DMA */
497 static int falcon_got_lock = 0;
498 static DECLARE_WAIT_QUEUE_HEAD(falcon_fairness_wait);
499 static int falcon_trying_lock = 0;
500 static DECLARE_WAIT_QUEUE_HEAD(falcon_try_wait);
501 static int falcon_dont_release = 0;
503 /* This function releases the lock on the DMA chip if there is no
504 * connected command and the disconnected queue is empty. On
505 * releasing, instances of falcon_get_lock are awoken, that put
506 * themselves to sleep for fairness. They can now try to get the lock
507 * again (but others waiting longer more probably will win).
510 static void
511 falcon_release_lock_if_possible( struct NCR5380_hostdata * hostdata )
513 unsigned long flags;
515 if (IS_A_TT()) return;
517 local_irq_save(flags);
519 if (falcon_got_lock &&
520 !hostdata->disconnected_queue &&
521 !hostdata->issue_queue &&
522 !hostdata->connected) {
524 if (falcon_dont_release) {
525 #if 0
526 printk("WARNING: Lock release not allowed. Ignored\n");
527 #endif
528 local_irq_restore(flags);
529 return;
531 falcon_got_lock = 0;
532 stdma_release();
533 wake_up( &falcon_fairness_wait );
536 local_irq_restore(flags);
539 /* This function manages the locking of the ST-DMA.
540 * If the DMA isn't locked already for SCSI, it tries to lock it by
541 * calling stdma_lock(). But if the DMA is locked by the SCSI code and
542 * there are other drivers waiting for the chip, we do not issue the
543 * command immediately but wait on 'falcon_fairness_queue'. We will be
544 * waked up when the DMA is unlocked by some SCSI interrupt. After that
545 * we try to get the lock again.
546 * But we must be prepared that more than one instance of
547 * falcon_get_lock() is waiting on the fairness queue. They should not
548 * try all at once to call stdma_lock(), one is enough! For that, the
549 * first one sets 'falcon_trying_lock', others that see that variable
550 * set wait on the queue 'falcon_try_wait'.
551 * Complicated, complicated.... Sigh...
554 static void falcon_get_lock( void )
556 unsigned long flags;
558 if (IS_A_TT()) return;
560 local_irq_save(flags);
562 while( !in_interrupt() && falcon_got_lock && stdma_others_waiting() )
563 sleep_on( &falcon_fairness_wait );
565 while (!falcon_got_lock) {
566 if (in_interrupt())
567 panic( "Falcon SCSI hasn't ST-DMA lock in interrupt" );
568 if (!falcon_trying_lock) {
569 falcon_trying_lock = 1;
570 stdma_lock(scsi_falcon_intr, NULL);
571 falcon_got_lock = 1;
572 falcon_trying_lock = 0;
573 wake_up( &falcon_try_wait );
575 else {
576 sleep_on( &falcon_try_wait );
580 local_irq_restore(flags);
581 if (!falcon_got_lock)
582 panic("Falcon SCSI: someone stole the lock :-(\n");
586 /* This is the wrapper function for NCR5380_queue_command(). It just
587 * tries to get the lock on the ST-DMA (see above) and then calls the
588 * original function.
591 #if 0
592 int atari_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
594 /* falcon_get_lock();
595 * ++guenther: moved to NCR5380_queue_command() to prevent
596 * race condition, see there for an explanation.
598 return( NCR5380_queue_command( cmd, done ) );
600 #endif
603 int atari_scsi_detect (Scsi_Host_Template *host)
605 static int called = 0;
606 struct Scsi_Host *instance;
608 if (!MACH_IS_ATARI ||
609 (!ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(TT_SCSI)) ||
610 called)
611 return( 0 );
613 host->proc_name = "Atari";
615 atari_scsi_reg_read = IS_A_TT() ? atari_scsi_tt_reg_read :
616 atari_scsi_falcon_reg_read;
617 atari_scsi_reg_write = IS_A_TT() ? atari_scsi_tt_reg_write :
618 atari_scsi_falcon_reg_write;
620 /* setup variables */
621 host->can_queue =
622 (setup_can_queue > 0) ? setup_can_queue :
623 IS_A_TT() ? ATARI_TT_CAN_QUEUE : ATARI_FALCON_CAN_QUEUE;
624 host->cmd_per_lun =
625 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun :
626 IS_A_TT() ? ATARI_TT_CMD_PER_LUN : ATARI_FALCON_CMD_PER_LUN;
627 /* Force sg_tablesize to 0 on a Falcon! */
628 host->sg_tablesize =
629 !IS_A_TT() ? ATARI_FALCON_SG_TABLESIZE :
630 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : ATARI_TT_SG_TABLESIZE;
632 if (setup_hostid >= 0)
633 host->this_id = setup_hostid;
634 else {
635 /* use 7 as default */
636 host->this_id = 7;
637 /* Test if a host id is set in the NVRam */
638 if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
639 unsigned char b = nvram_read_byte( 14 );
640 /* Arbitration enabled? (for TOS) If yes, use configured host ID */
641 if (b & 0x80)
642 host->this_id = b & 7;
646 #ifdef SUPPORT_TAGS
647 if (setup_use_tagged_queuing < 0)
648 setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING;
649 #endif
650 #ifdef REAL_DMA
651 /* If running on a Falcon and if there's TT-Ram (i.e., more than one
652 * memory block, since there's always ST-Ram in a Falcon), then allocate a
653 * STRAM_BUFFER_SIZE byte dribble buffer for transfers from/to alternative
654 * Ram.
656 if (MACH_IS_ATARI && ATARIHW_PRESENT(ST_SCSI) &&
657 !ATARIHW_PRESENT(EXTD_DMA) && m68k_num_memory > 1) {
658 atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
659 if (!atari_dma_buffer) {
660 printk( KERN_ERR "atari_scsi_detect: can't allocate ST-RAM "
661 "double buffer\n" );
662 return( 0 );
664 atari_dma_phys_buffer = virt_to_phys( atari_dma_buffer );
665 atari_dma_orig_addr = 0;
667 #endif
668 instance = scsi_register (host, sizeof (struct NCR5380_hostdata));
669 if(instance == NULL)
671 atari_stram_free(atari_dma_buffer);
672 atari_dma_buffer = 0;
673 return 0;
675 atari_scsi_host = instance;
676 /* Set irq to 0, to avoid that the mid-level code disables our interrupt
677 * during queue_command calls. This is completely unnecessary, and even
678 * worse causes bad problems on the Falcon, where the int is shared with
679 * IDE and floppy! */
680 instance->irq = 0;
682 #ifdef CONFIG_ATARI_SCSI_RESET_BOOT
683 atari_scsi_reset_boot();
684 #endif
685 NCR5380_init (instance, 0);
687 if (IS_A_TT()) {
689 /* This int is actually "pseudo-slow", i.e. it acts like a slow
690 * interrupt after having cleared the pending flag for the DMA
691 * interrupt. */
692 if (request_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr, IRQ_TYPE_SLOW,
693 "SCSI NCR5380", scsi_tt_intr)) {
694 printk(KERN_ERR "atari_scsi_detect: cannot allocate irq %d, aborting",IRQ_TT_MFP_SCSI);
695 scsi_unregister(atari_scsi_host);
696 atari_stram_free(atari_dma_buffer);
697 atari_dma_buffer = 0;
698 return 0;
700 tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
701 #ifdef REAL_DMA
702 tt_scsi_dma.dma_ctrl = 0;
703 atari_dma_residual = 0;
704 #ifdef CONFIG_TT_DMA_EMUL
705 if (MACH_IS_HADES) {
706 if (request_irq(IRQ_AUTO_2, hades_dma_emulator,
707 IRQ_TYPE_PRIO, "Hades DMA emulator",
708 hades_dma_emulator)) {
709 printk(KERN_ERR "atari_scsi_detect: cannot allocate irq %d, aborting (MACH_IS_HADES)",IRQ_AUTO_2);
710 free_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr);
711 scsi_unregister(atari_scsi_host);
712 atari_stram_free(atari_dma_buffer);
713 atari_dma_buffer = 0;
714 return 0;
717 #endif
718 if (MACH_IS_MEDUSA || MACH_IS_HADES) {
719 /* While the read overruns (described by Drew Eckhardt in
720 * NCR5380.c) never happened on TTs, they do in fact on the Medusa
721 * (This was the cause why SCSI didn't work right for so long
722 * there.) Since handling the overruns slows down a bit, I turned
723 * the #ifdef's into a runtime condition.
725 * In principle it should be sufficient to do max. 1 byte with
726 * PIO, but there is another problem on the Medusa with the DMA
727 * rest data register. So 'atari_read_overruns' is currently set
728 * to 4 to avoid having transfers that aren't a multiple of 4. If
729 * the rest data bug is fixed, this can be lowered to 1.
731 atari_read_overruns = 4;
733 #endif /*REAL_DMA*/
735 else { /* ! IS_A_TT */
737 /* Nothing to do for the interrupt: the ST-DMA is initialized
738 * already by atari_init_INTS()
741 #ifdef REAL_DMA
742 atari_dma_residual = 0;
743 atari_dma_active = 0;
744 atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
745 : 0xff000000);
746 #endif
749 printk(KERN_INFO "scsi%d: options CAN_QUEUE=%d CMD_PER_LUN=%d SCAT-GAT=%d "
750 #ifdef SUPPORT_TAGS
751 "TAGGED-QUEUING=%s "
752 #endif
753 "HOSTID=%d",
754 instance->host_no, instance->hostt->can_queue,
755 instance->hostt->cmd_per_lun,
756 instance->hostt->sg_tablesize,
757 #ifdef SUPPORT_TAGS
758 setup_use_tagged_queuing ? "yes" : "no",
759 #endif
760 instance->hostt->this_id );
761 NCR5380_print_options (instance);
762 printk ("\n");
764 called = 1;
765 return( 1 );
768 #ifdef MODULE
769 int atari_scsi_release (struct Scsi_Host *sh)
771 if (IS_A_TT())
772 free_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr);
773 if (atari_dma_buffer)
774 atari_stram_free (atari_dma_buffer);
775 return 1;
777 #endif
779 void __init atari_scsi_setup(char *str, int *ints)
781 /* Format of atascsi parameter is:
782 * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
783 * Defaults depend on TT or Falcon, hostid determined at run time.
784 * Negative values mean don't change.
787 if (ints[0] < 1) {
788 printk( "atari_scsi_setup: no arguments!\n" );
789 return;
792 if (ints[0] >= 1) {
793 if (ints[1] > 0)
794 /* no limits on this, just > 0 */
795 setup_can_queue = ints[1];
797 if (ints[0] >= 2) {
798 if (ints[2] > 0)
799 setup_cmd_per_lun = ints[2];
801 if (ints[0] >= 3) {
802 if (ints[3] >= 0) {
803 setup_sg_tablesize = ints[3];
804 /* Must be <= SG_ALL (255) */
805 if (setup_sg_tablesize > SG_ALL)
806 setup_sg_tablesize = SG_ALL;
809 if (ints[0] >= 4) {
810 /* Must be between 0 and 7 */
811 if (ints[4] >= 0 && ints[4] <= 7)
812 setup_hostid = ints[4];
813 else if (ints[4] > 7)
814 printk( "atari_scsi_setup: invalid host ID %d !\n", ints[4] );
816 #ifdef SUPPORT_TAGS
817 if (ints[0] >= 5) {
818 if (ints[5] >= 0)
819 setup_use_tagged_queuing = !!ints[5];
821 #endif
824 int atari_scsi_bus_reset(Scsi_Cmnd *cmd)
826 int rv;
827 struct NCR5380_hostdata *hostdata =
828 (struct NCR5380_hostdata *)cmd->device->host->hostdata;
830 /* For doing the reset, SCSI interrupts must be disabled first,
831 * since the 5380 raises its IRQ line while _RST is active and we
832 * can't disable interrupts completely, since we need the timer.
834 /* And abort a maybe active DMA transfer */
835 if (IS_A_TT()) {
836 atari_turnoff_irq( IRQ_TT_MFP_SCSI );
837 #ifdef REAL_DMA
838 tt_scsi_dma.dma_ctrl = 0;
839 #endif /* REAL_DMA */
841 else {
842 atari_turnoff_irq( IRQ_MFP_FSCSI );
843 #ifdef REAL_DMA
844 st_dma.dma_mode_status = 0x90;
845 atari_dma_active = 0;
846 atari_dma_orig_addr = NULL;
847 #endif /* REAL_DMA */
850 rv = NCR5380_bus_reset(cmd);
852 /* Re-enable ints */
853 if (IS_A_TT()) {
854 atari_turnon_irq( IRQ_TT_MFP_SCSI );
856 else {
857 atari_turnon_irq( IRQ_MFP_FSCSI );
859 if ((rv & SCSI_RESET_ACTION) == SCSI_RESET_SUCCESS)
860 falcon_release_lock_if_possible(hostdata);
862 return( rv );
866 #ifdef CONFIG_ATARI_SCSI_RESET_BOOT
867 static void __init atari_scsi_reset_boot(void)
869 unsigned long end;
872 * Do a SCSI reset to clean up the bus during initialization. No messing
873 * with the queues, interrupts, or locks necessary here.
876 printk( "Atari SCSI: resetting the SCSI bus..." );
878 /* get in phase */
879 NCR5380_write( TARGET_COMMAND_REG,
880 PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
882 /* assert RST */
883 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
884 /* The min. reset hold time is 25us, so 40us should be enough */
885 udelay( 50 );
886 /* reset RST and interrupt */
887 NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
888 NCR5380_read( RESET_PARITY_INTERRUPT_REG );
890 end = jiffies + AFTER_RESET_DELAY;
891 while (time_before(jiffies, end))
892 barrier();
894 printk( " done\n" );
896 #endif
899 const char * atari_scsi_info (struct Scsi_Host *host)
901 /* atari_scsi_detect() is verbose enough... */
902 static const char string[] = "Atari native SCSI";
903 return string;
907 #if defined(REAL_DMA)
909 unsigned long atari_scsi_dma_setup( struct Scsi_Host *instance, void *data,
910 unsigned long count, int dir )
912 unsigned long addr = virt_to_phys( data );
914 DMA_PRINTK("scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
915 "dir = %d\n", instance->host_no, data, addr, count, dir);
917 if (!IS_A_TT() && !STRAM_ADDR(addr)) {
918 /* If we have a non-DMAable address on a Falcon, use the dribble
919 * buffer; 'orig_addr' != 0 in the read case tells the interrupt
920 * handler to copy data from the dribble buffer to the originally
921 * wanted address.
923 if (dir)
924 memcpy( atari_dma_buffer, data, count );
925 else
926 atari_dma_orig_addr = data;
927 addr = atari_dma_phys_buffer;
930 atari_dma_startaddr = addr; /* Needed for calculating residual later. */
932 /* Cache cleanup stuff: On writes, push any dirty cache out before sending
933 * it to the peripheral. (Must be done before DMA setup, since at least
934 * the ST-DMA begins to fill internal buffers right after setup. For
935 * reads, invalidate any cache, may be altered after DMA without CPU
936 * knowledge.
938 * ++roman: For the Medusa, there's no need at all for that cache stuff,
939 * because the hardware does bus snooping (fine!).
941 dma_cache_maintenance( addr, count, dir );
943 if (count == 0)
944 printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n");
946 if (IS_A_TT()) {
947 tt_scsi_dma.dma_ctrl = dir;
948 SCSI_DMA_WRITE_P( dma_addr, addr );
949 SCSI_DMA_WRITE_P( dma_cnt, count );
950 tt_scsi_dma.dma_ctrl = dir | 2;
952 else { /* ! IS_A_TT */
954 /* set address */
955 SCSI_DMA_SETADR( addr );
957 /* toggle direction bit to clear FIFO and set DMA direction */
958 dir <<= 8;
959 st_dma.dma_mode_status = 0x90 | dir;
960 st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
961 st_dma.dma_mode_status = 0x90 | dir;
962 udelay(40);
963 /* On writes, round up the transfer length to the next multiple of 512
964 * (see also comment at atari_dma_xfer_len()). */
965 st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
966 udelay(40);
967 st_dma.dma_mode_status = 0x10 | dir;
968 udelay(40);
969 /* need not restore value of dir, only boolean value is tested */
970 atari_dma_active = 1;
973 return( count );
977 static long atari_scsi_dma_residual( struct Scsi_Host *instance )
979 return( atari_dma_residual );
983 #define CMD_SURELY_BLOCK_MODE 0
984 #define CMD_SURELY_BYTE_MODE 1
985 #define CMD_MODE_UNKNOWN 2
987 static int falcon_classify_cmd( Scsi_Cmnd *cmd )
989 unsigned char opcode = cmd->cmnd[0];
991 if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
992 opcode == READ_BUFFER)
993 return( CMD_SURELY_BYTE_MODE );
994 else if (opcode == READ_6 || opcode == READ_10 ||
995 opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
996 opcode == RECOVER_BUFFERED_DATA) {
997 /* In case of a sequential-access target (tape), special care is
998 * needed here: The transfer is block-mode only if the 'fixed' bit is
999 * set! */
1000 if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
1001 return( CMD_SURELY_BYTE_MODE );
1002 else
1003 return( CMD_SURELY_BLOCK_MODE );
1005 else
1006 return( CMD_MODE_UNKNOWN );
1010 /* This function calculates the number of bytes that can be transferred via
1011 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
1012 * ST-DMA chip. There are only multiples of 512 bytes possible and max.
1013 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
1014 * possible on the Falcon, since that would require to program the DMA for
1015 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
1016 * the overrun problem, so this question is academic :-)
1019 static unsigned long atari_dma_xfer_len( unsigned long wanted_len,
1020 Scsi_Cmnd *cmd,
1021 int write_flag )
1023 unsigned long possible_len, limit;
1024 #ifndef CONFIG_TT_DMA_EMUL
1025 if (MACH_IS_HADES)
1026 /* Hades has no SCSI DMA at all :-( Always force use of PIO */
1027 return( 0 );
1028 #endif
1029 if (IS_A_TT())
1030 /* TT SCSI DMA can transfer arbitrary #bytes */
1031 return( wanted_len );
1033 /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
1034 * 255*512 bytes, but this should be enough)
1036 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
1037 * that return a number of bytes which cannot be known beforehand. In this
1038 * case, the given transfer length is an "allocation length". Now it
1039 * can happen that this allocation length is a multiple of 512 bytes and
1040 * the DMA is used. But if not n*512 bytes really arrive, some input data
1041 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
1042 * between commands that do block transfers and those that do byte
1043 * transfers. But this isn't easy... there are lots of vendor specific
1044 * commands, and the user can issue any command via the
1045 * SCSI_IOCTL_SEND_COMMAND.
1047 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
1048 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
1049 * and 3), the thing to do is obvious: allow any number of blocks via DMA
1050 * or none. In case 2), we apply some heuristic: Byte mode is assumed if
1051 * the transfer (allocation) length is < 1024, hoping that no cmd. not
1052 * explicitly known as byte mode have such big allocation lengths...
1053 * BTW, all the discussion above applies only to reads. DMA writes are
1054 * unproblematic anyways, since the targets aborts the transfer after
1055 * receiving a sufficient number of bytes.
1057 * Another point: If the transfer is from/to an non-ST-RAM address, we
1058 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
1061 if (write_flag) {
1062 /* Write operation can always use the DMA, but the transfer size must
1063 * be rounded up to the next multiple of 512 (atari_dma_setup() does
1064 * this).
1066 possible_len = wanted_len;
1068 else {
1069 /* Read operations: if the wanted transfer length is not a multiple of
1070 * 512, we cannot use DMA, since the ST-DMA cannot split transfers
1071 * (no interrupt on DMA finished!)
1073 if (wanted_len & 0x1ff)
1074 possible_len = 0;
1075 else {
1076 /* Now classify the command (see above) and decide whether it is
1077 * allowed to do DMA at all */
1078 switch( falcon_classify_cmd( cmd )) {
1079 case CMD_SURELY_BLOCK_MODE:
1080 possible_len = wanted_len;
1081 break;
1082 case CMD_SURELY_BYTE_MODE:
1083 possible_len = 0; /* DMA prohibited */
1084 break;
1085 case CMD_MODE_UNKNOWN:
1086 default:
1087 /* For unknown commands assume block transfers if the transfer
1088 * size/allocation length is >= 1024 */
1089 possible_len = (wanted_len < 1024) ? 0 : wanted_len;
1090 break;
1095 /* Last step: apply the hard limit on DMA transfers */
1096 limit = (atari_dma_buffer && !STRAM_ADDR( virt_to_phys(cmd->SCp.ptr) )) ?
1097 STRAM_BUFFER_SIZE : 255*512;
1098 if (possible_len > limit)
1099 possible_len = limit;
1101 if (possible_len != wanted_len)
1102 DMA_PRINTK("Sorry, must cut DMA transfer size to %ld bytes "
1103 "instead of %ld\n", possible_len, wanted_len);
1105 return( possible_len );
1109 #endif /* REAL_DMA */
1112 /* NCR5380 register access functions
1114 * There are separate functions for TT and Falcon, because the access
1115 * methods are quite different. The calling macros NCR5380_read and
1116 * NCR5380_write call these functions via function pointers.
1119 static unsigned char atari_scsi_tt_reg_read( unsigned char reg )
1121 return( tt_scsi_regp[reg * 2] );
1124 static void atari_scsi_tt_reg_write( unsigned char reg, unsigned char value )
1126 tt_scsi_regp[reg * 2] = value;
1129 static unsigned char atari_scsi_falcon_reg_read( unsigned char reg )
1131 dma_wd.dma_mode_status= (u_short)(0x88 + reg);
1132 return( (u_char)dma_wd.fdc_acces_seccount );
1135 static void atari_scsi_falcon_reg_write( unsigned char reg, unsigned char value )
1137 dma_wd.dma_mode_status = (u_short)(0x88 + reg);
1138 dma_wd.fdc_acces_seccount = (u_short)value;
1142 #include "atari_NCR5380.c"
1144 static Scsi_Host_Template driver_template = {
1145 .proc_info = atari_scsi_proc_info,
1146 .name = "Atari native SCSI",
1147 .detect = atari_scsi_detect,
1148 .release = atari_scsi_release,
1149 .info = atari_scsi_info,
1150 .queuecommand = atari_scsi_queue_command,
1151 .eh_abort_handler = atari_scsi_abort,
1152 .eh_bus_reset_handler = atari_scsi_bus_reset,
1153 .can_queue = 0, /* initialized at run-time */
1154 .this_id = 0, /* initialized at run-time */
1155 .sg_tablesize = 0, /* initialized at run-time */
1156 .cmd_per_lun = 0, /* initialized at run-time */
1157 .use_clustering = DISABLE_CLUSTERING
1161 #include "scsi_module.c"
1163 MODULE_LICENSE("GPL");