2 * mac_scsi.c -- Device dependent functions for the Macintosh NCR5380 SCSI 3 * port (MacII style machines, no DMA). 9 * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port 11 * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> 13 * Loosely based on the work of Robert De Vries' team and added: 15 * - Falcon support (untested yet!) ++bjoern fixed and now it works 16 * - lots of extensions and bug fixes. 18 * This file is subject to the terms and conditions of the GNU General Public 19 * License. See the file COPYING in the main directory of this archive 25 #include <linux/config.h> 26 #include <linux/module.h> 28 #define NDEBUG_ABORT 0x800000 29 #define NDEBUG_TAGS 0x1000000 30 #define NDEBUG_MERGING 0x2000000 35 /* MSch: Tested the pseudo-DMA code on Atari for the Mac68k port ... */ 36 /* Defining neither PSEUDO_DMA nor REAL_DMA -> PIO transfer, sloooow ! */ 37 /*#define REAL_DMA*//* never supported on NCR5380 Macs */ 39 * Usage: define PSEUDO_DMA to use hardware-handshaked PIO mode (TBI) 40 * undef PSEUDO_DMA to use pure PIO mode 43 /*#define PSEUDO_DMA*//* currently gives trouble on some Macs */ 46 #define EMULATE_PSEUDO_DMA 47 #define DMA_WORKS_RIGHT 51 /* Support tagged queuing? (on devices that are able to... :-) */ 55 #include <linux/types.h> 56 #include <linux/stddef.h> 57 #include <linux/ctype.h> 58 #include <linux/delay.h> 60 #include <linux/blk.h> 61 #include <linux/sched.h> 62 #include <linux/interrupt.h> 64 #include <asm/setup.h> 65 #include <asm/machw.h> 66 #include <asm/macints.h> 67 #include <asm/macintosh.h> 69 #include <asm/pgtable.h> 71 #include <asm/traps.h> 72 #include <asm/bitops.h> 81 #include <linux/stat.h> 83 struct proc_dir_entry proc_scsi_mac
= { 84 PROC_SCSI_MAC
,8,"mac_5380", 85 S_IFDIR
| S_IRUGO
| S_IXUGO
,2 89 * Define RBV_HACK to run the SCSI driver on RBV Macs; undefine to 90 * try getting better interrupt latency 95 #define ENABLE_IRQ() mac_turnon_irq( IRQ_MAC_SCSI ); 96 #define DISABLE_IRQ() mac_turnoff_irq( IRQ_MAC_SCSI ); 98 #define ENABLE_IRQ() mac_enable_irq( IRQ_MAC_SCSI ); 99 #define DISABLE_IRQ() mac_disable_irq( IRQ_MAC_SCSI ); 102 #define HOSTDATA_DMALEN (((struct NCR5380_hostdata *) \ 103 (mac_scsi_host->hostdata))->dma_len) 105 /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms, 106 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more 107 * need ten times the standard value... */ 108 #ifndef CONFIG_MAC_SCSI_TOSHIBA_DELAY 109 #define AFTER_RESET_DELAY (HZ/2) 111 #define AFTER_RESET_DELAY (5*HZ/2) 114 /***************************** Prototypes *****************************/ 117 static intscsi_dma_is_ignored_buserr(unsigned char dma_stat
); 118 static voidmac_scsi_fetch_restbytes(void); 119 static longmac_scsi_dma_residual(struct Scsi_Host
*instance
); 120 static intmac_classify_cmd( Scsi_Cmnd
*cmd
); 121 static unsigned longmac_dma_xfer_len(unsigned long wanted_len
, 122 Scsi_Cmnd
*cmd
,int write_flag
); 125 static intmac_pdma_read(struct Scsi_Host
*instance
,unsigned char*dst
, 127 static intmac_pdma_write(struct Scsi_Host
*instance
,unsigned char*src
, 129 static unsigned longmac_dma_xfer_len(unsigned long wanted_len
, 130 Scsi_Cmnd
*cmd
,int write_flag
); 132 static voidscsi_mac_intr(int irq
,void*dummy
,struct pt_regs
*fp
); 133 static voidmac_scsi_reset_boot(void); 134 static unsigned charmac_scsi_reg_read(unsigned char reg
); 135 static voidmac_scsi_reg_write(unsigned char reg
,unsigned char value
); 137 /************************* End of Prototypes **************************/ 140 static struct Scsi_Host
*mac_scsi_host
= NULL
; 142 static unsigned char(*mac_scsi_reg_read
)(unsigned char reg
); 143 static void(*mac_scsi_reg_write
)(unsigned char reg
,unsigned char value
); 147 static unsigned long mac_dma_residual
, mac_dma_startaddr
; 148 static short mac_dma_active
; 149 /* pointer to the dribble buffer */ 150 static char*mac_dma_buffer
= NULL
; 151 /* precalculated physical address of the dribble buffer */ 152 static unsigned long mac_dma_phys_buffer
; 153 /* != 0 tells the int handler to copy data from the dribble buffer */ 154 static char*mac_dma_orig_addr
; 155 /* size of the dribble buffer; 4k seems enough, since the Falcon cannot use 156 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare 157 * cases where requests to physical contiguous buffers have been merged, this 158 * request is <= 4k (one page). So I don't think we have to split transfers 159 * just due to this buffer size... 161 #define MAC_BUFFER_SIZE (4096) 162 #if 1/* FIXME: is that an issue for Macs?? */ 163 /* mask for address bits that can't be used with the ST-DMA */ 164 static unsigned long mac_dma_stram_mask
; 165 #define STRAM_ADDR(a) (((a) & mac_dma_stram_mask) == 0) 167 /* number of bytes to cut from a transfer to handle NCR overruns */ 168 static int mac_read_overruns
=0; 171 static unsigned long mac_pdma_residual
, mac_pdma_startaddr
, mac_pdma_current
; 172 static short mac_pdma_active
; 173 /* FIXME: is that an issue for Macs?? */ 174 /* mask for address bits that can't be used with the ST-DMA */ 175 static unsigned long mac_dma_stram_mask
; 176 #define STRAM_ADDR(a) (((a) & mac_dma_stram_mask) == 0) 177 static int mac_read_overruns
=0; 179 static int setup_can_queue
= -1; 180 static int setup_cmd_per_lun
= -1; 181 static int setup_sg_tablesize
= -1; 183 static int setup_use_tagged_queuing
= -1; 185 static int setup_hostid
= -1; 188 #if defined(REAL_DMA) 191 static intscsi_dma_is_ignored_buserr(unsigned char dma_stat
) 194 unsigned long addr
=SCSI_DMA_READ_P( dma_addr
), end_addr
; 198 /* A bus error happens when DMA-ing from the last page of a 199 * physical memory chunk (DMA prefetch!), but that doesn't hurt. 200 * Check for this case: 203 for( i
=0; i
< boot_info
.num_memory
; ++i
) { 204 end_addr
= boot_info
.memory
[i
].addr
+ 205 boot_info
.memory
[i
].size
; 206 if(end_addr
<= addr
&& addr
<= end_addr
+4) 214 /* Dead code... wasn't called anyway :-) and causes some trouble, because at 215 * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has 216 * to clear the DMA int pending bit before it allows other level 6 interrupts. 218 static voidscsi_dma_buserr(int irq
,void*dummy
,struct pt_regs
*fp
) 220 unsigned char dma_stat
= tt_scsi_dma
.dma_ctrl
; 222 /* Don't do anything if a NCR interrupt is pending. Probably it's just 224 if(mac_irq_pending( IRQ_MAC_SCSI
)) 227 printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n", 228 SCSI_DMA_READ_P(dma_addr
), dma_stat
,SCSI_DMA_READ_P(dma_cnt
)); 230 if(!scsi_dma_is_ignored_buserr( dma_stat
)) 231 printk("SCSI DMA bus error -- bad DMA programming!\n"); 234 /* Under normal circumstances we never should get to this point, 235 * since both interrupts are triggered simultaneously and the 5380 236 * int has higher priority. When this irq is handled, that DMA 237 * interrupt is cleared. So a warning message is printed here. 239 printk("SCSI DMA intr ?? -- this shouldn't happen!\n"); 246 voidrestore_irq(struct pt_regs
*regs
) 251 flags
= (flags
& ~0x0700) | (regs
->sr
&0x0700); 252 restore_flags(flags
); 255 static int polled_scsi_on
=0; 256 static unsigned char*mac_scsi_regp
= NULL
; 258 voidscsi_mac_polled(void) 262 if(NCR5380_read(BUS_AND_STATUS_REG
)&BASR_IRQ
) 264 printk("SCSI poll\n"); 265 scsi_mac_intr(IRQ_MAC_SCSI
, NULL
, NULL
); 270 static voidscsi_mac_intr(int irq
,void*dummy
,struct pt_regs
*fp
) 276 dma_stat
= mac_scsi_dma
.dma_ctrl
; 278 INT_PRINTK("scsi%d: NCR5380 interrupt, DMA status = %02x\n", 279 mac_scsi_host
->host_no
, dma_stat
&0xff); 281 /* Look if it was the DMA that has interrupted: First possibility 282 * is that a bus error occurred... 285 if(!scsi_dma_is_ignored_buserr( dma_stat
)) { 286 printk(KERN_ERR
"SCSI DMA caused bus error near 0x%08lx\n", 287 SCSI_DMA_READ_P(dma_addr
)); 288 printk(KERN_CRIT
"SCSI DMA bus error -- bad DMA programming!"); 292 /* If the DMA is active but not finished, we have the the case 293 * that some other 5380 interrupt occurred within the DMA transfer. 294 * This means we have residual bytes, if the desired end address 295 * is not yet reached. Maybe we have to fetch some bytes from the 296 * rest data register, too. The residual must be calculated from 297 * the address pointer, not the counter register, because only the 298 * addr reg counts bytes not yet written and pending in the rest 301 if((dma_stat
&0x02) && !(dma_stat
&0x40)) { 302 mac_dma_residual
= HOSTDATA_DMALEN
- (SCSI_DMA_READ_P( dma_addr
) - 305 DMA_PRINTK("SCSI DMA: There are %ld residual bytes.\n", 308 if((signed int)mac_dma_residual
<0) 310 if((dma_stat
&1) ==0) { 311 /* After read operations, we maybe have to 312 transport some rest bytes */ 313 mac_scsi_fetch_restbytes(); 316 /* There seems to be a nasty bug in some SCSI-DMA/NCR 317 combinations: If a target disconnects while a write 318 operation is going on, the address register of the 319 DMA may be a few bytes farer than it actually read. 320 This is probably due to DMA prefetching and a delay 321 between DMA and NCR. Experiments showed that the 322 dma_addr is 9 bytes to high, but this could vary. 323 The problem is, that the residual is thus calculated 324 wrong and the next transfer will start behind where 325 it should. So we round up the residual to the next 326 multiple of a sector size, if it isn't already a 327 multiple and the originally expected transfer size 328 was. The latter condition is there to ensure that 329 the correction is taken only for "real" data 330 transfers and not for, e.g., the parameters of some 331 other command. These shouldn't disconnect anyway. 333 if(mac_dma_residual
&0x1ff) { 334 DMA_PRINTK("SCSI DMA: DMA bug corrected, " 335 "difference %ld bytes\n", 336 512- (mac_dma_residual
&0x1ff)); 337 mac_dma_residual
= (mac_dma_residual
+511) & ~0x1ff; 340 mac_scsi_dma
.dma_ctrl
=0; 343 /* If the DMA is finished, fetch the rest bytes and turn it off */ 345 atari_dma_residual
=0; 346 if((dma_stat
&1) ==0) 347 atari_scsi_fetch_restbytes(); 348 tt_scsi_dma
.dma_ctrl
=0; 354 /* determine if there is any residual for the current transfer */ 355 if(mac_pdma_active
) { 356 /* probably EOP interrupt, signaling i.e. target disconnect. 357 * We must figure out the residual from the source/destination 358 * pointers here ... */ 359 /* Should check bus status here to make sure it wasn't reselect or reset */ 360 mac_pdma_residual
= HOSTDATA_DMALEN
- (mac_pdma_current
- mac_pdma_startaddr
); 366 mac_turnoff_irq( IRQ_MAC_SCSI
); 368 mac_disable_irq( IRQ_MAC_SCSI
); 372 #ifndef RBV_HACK/* interferes with level triggered RBV IRQs ?? */ 376 if( irq
==IRQ_IDX(IRQ_MAC_SCSI
) ) 377 NCR5380_intr(irq
, dummy
, fp
); 379 restore_flags(flags
); 381 /* To be sure the int is not masked */ 383 mac_turnon_irq( IRQ_MAC_SCSI
); 385 mac_enable_irq( IRQ_MAC_SCSI
); 394 static voidmac_scsi_fetch_restbytes(void) 399 /* fetch rest bytes in the DMA register */ 400 dst
= (char*)SCSI_DMA_READ_P( dma_addr
); 401 if((nr
= ((long)dst
&3))) { 402 /* there are 'nr' bytes left for the last long address before the 404 dst
= (char*)( (unsigned long)dst
& ~3); 405 DMA_PRINTK("SCSI DMA: there are %d rest bytes for phys addr 0x%08lx", 407 dst
= (char*)PTOV(dst
);/* The content of the DMA pointer 408 * is a physical address! */ 409 DMA_PRINTK(" = virt addr 0x%08lx\n", (long)dst
); 410 for( src
= (char*)&mac_scsi_dma
.dma_restdata
; nr
>0; --nr
) 416 #if 0/* FIXME : how is the host ID determined on a Mac? */ 417 #define RTC_READ(reg) \ 418 ({ unsigned char __val; \ 419 outb(reg,&tt_rtc.regsel); \ 420 __val = tt_rtc.data; \ 424 #define RTC_WRITE(reg,val) \ 426 outb(reg,&tt_rtc.regsel); \ 427 tt_rtc.data = (val); \ 431 intmac_scsi_detect(Scsi_Host_Template
*host
) 433 static int called
=0; 434 struct Scsi_Host
*instance
; 436 if(!MACH_IS_MAC
|| called
) 439 if(macintosh_config
->scsi_type
!= MAC_SCSI_OLD
) 442 host
->proc_dir
= &proc_scsi_mac
; 444 /* testing: IIfx SCSI without IOP ?? */ 445 if(macintosh_config
->ident
== MAC_MODEL_IIFX
) 446 mac_scsi_regp
= via1_regp
+0x8000; 448 mac_scsi_regp
= via1_regp
+0x10000; 450 #if 0/* maybe if different SCSI versions show up ? */ 451 mac_scsi_reg_read
=IS_A_TT() ? atari_scsi_tt_reg_read
: 452 atari_scsi_falcon_reg_read
; 453 mac_scsi_reg_write
=IS_A_TT() ? atari_scsi_tt_reg_write
: 454 #endif atari_scsi_falcon_reg_write; 456 /* setup variables */ 458 (setup_can_queue
>0) ? setup_can_queue
: 461 (setup_cmd_per_lun
>0) ? setup_cmd_per_lun
: 462 MAC_SCSI_CMD_PER_LUN
; 464 (setup_sg_tablesize
>=0) ? setup_sg_tablesize
: MAC_SCSI_SG_TABLESIZE
; 467 host
->this_id
= setup_hostid
; 469 /* use 7 as default */ 474 if(setup_use_tagged_queuing
<0) 475 setup_use_tagged_queuing
= DEFAULT_USE_TAGGED_QUEUING
; 478 instance
=scsi_register(host
,sizeof(struct NCR5380_hostdata
)); 479 mac_scsi_host
= instance
; 481 /* truncation of machspec bit not critical as instance->irq never used */ 482 #if 0/* Might work; problem was only with Falcon lock */ 483 instance
->irq
= IRQ_MAC_SCSI
; 487 mac_scsi_reset_boot(); 488 NCR5380_init(instance
,0); 490 /* This int is actually "pseudo-slow", i.e. it acts like a slow 491 * interrupt after having cleared the pending flag for the DMA 493 request_irq(IRQ_MAC_SCSI
, scsi_mac_intr
, IRQ_TYPE_SLOW
, 494 "SCSI NCR5380", scsi_mac_intr
); 496 tt_scsi_dma
.dma_ctrl
=0; 497 atari_dma_residual
=0; 500 /* While the read overruns (described by Drew Eckhardt in 501 * NCR5380.c) never happened on TTs, they do in fact on the Medusa 502 * (This was the cause why SCSI didn't work right for so long 503 * there.) Since handling the overruns slows down a bit, I turned 504 * the #ifdef's into a runtime condition. 506 * In principle it should be sufficient to do max. 1 byte with 507 * PIO, but there is another problem on the Medusa with the DMA 508 * rest data register. So 'atari_read_overruns' is currently set 509 * to 4 to avoid having transfers that aren't a multiple of 4. If 510 * the rest data bug is fixed, this can be lowered to 1. 512 mac_read_overruns
=4; 517 mac_pdma_residual
=0; 521 printk(KERN_INFO
"scsi%d: options CAN_QUEUE=%d CMD_PER_LUN=%d SCAT-GAT=%d " 526 instance
->host_no
, instance
->hostt
->can_queue
, 527 instance
->hostt
->cmd_per_lun
, 528 instance
->hostt
->sg_tablesize
, 530 setup_use_tagged_queuing
?"yes":"no", 532 instance
->hostt
->this_id
); 533 NCR5380_print_options(instance
); 541 intmac_scsi_release(struct Scsi_Host
*sh
) 543 free_irq(IRQ_MAC_SCSI
, scsi_mac_intr
); 545 scsi_init_free(mac_dma_buffer
, MAC_BUFFER_SIZE
); 550 voidmac_scsi_setup(char*str
,int*ints
) 552 /* Format of mac5380 parameter is: 553 * mac5380=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags> 554 * Negative values mean don't change. 557 /* Grmbl... the standard parameter parsing can't handle negative numbers 558 * :-( So let's do it ourselves! 561 int i
= ints
[0]+1, fact
; 563 while( str
&& (isdigit(*str
) || *str
=='-') && i
<=10) { 568 ints
[i
++] =simple_strtoul( str
, NULL
,0) * fact
; 569 if((str
=strchr( str
,',')) != NULL
) 575 printk("mac_scsi_setup: no arguments!\n"); 581 /* no limits on this, just > 0 */ 582 setup_can_queue
= ints
[1]; 586 setup_cmd_per_lun
= ints
[2]; 590 setup_sg_tablesize
= ints
[3]; 591 /* Must be <= SG_ALL (255) */ 592 if(setup_sg_tablesize
> SG_ALL
) 593 setup_sg_tablesize
= SG_ALL
; 597 /* Must be between 0 and 7 */ 598 if(ints
[4] >=0&& ints
[4] <=7) 599 setup_hostid
= ints
[4]; 601 printk("mac_scsi_setup: invalid host ID %d !\n", ints
[4] ); 606 setup_use_tagged_queuing
= !!ints
[5]; 611 intmac_scsi_reset( Scsi_Cmnd
*cmd
,unsigned int reset_flags
) 614 struct NCR5380_hostdata
*hostdata
= 615 (struct NCR5380_hostdata
*)cmd
->host
->hostdata
; 617 /* For doing the reset, SCSI interrupts must be disabled first, 618 * since the 5380 raises its IRQ line while _RST is active and we 619 * can't disable interrupts completely, since we need the timer. 621 /* And abort a maybe active DMA transfer */ 622 mac_turnoff_irq( IRQ_MAC_SCSI
); 624 mac_scsi_dma
.dma_ctrl
=0; 630 rv
=NCR5380_reset(cmd
, reset_flags
); 633 mac_turnon_irq( IRQ_MAC_SCSI
); 639 static voidmac_scsi_reset_boot(void) 644 * Do a SCSI reset to clean up the bus during initialization. No messing 645 * with the queues, interrupts, or locks necessary here. 648 printk("Macintosh SCSI: resetting the SCSI bus..."); 650 /* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */ 651 mac_turnoff_irq( IRQ_MAC_SCSI
); 654 NCR5380_write( TARGET_COMMAND_REG
, 655 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG
) )); 658 NCR5380_write( INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_RST
); 659 /* The min. reset hold time is 25us, so 40us should be enough */ 661 /* reset RST and interrupt */ 662 NCR5380_write( INITIATOR_COMMAND_REG
, ICR_BASE
); 663 NCR5380_read( RESET_PARITY_INTERRUPT_REG
); 665 for( end
= jiffies
+ AFTER_RESET_DELAY
;time_before(jiffies
, end
); ) 668 /* switch on SCSI IRQ again */ 669 mac_turnon_irq( IRQ_MAC_SCSI
); 675 const char*mac_scsi_info(struct Scsi_Host
*host
) 677 /* mac_scsi_detect() is verbose enough... */ 678 static const char string
[] ="Macintosh NCR5380 SCSI"; 683 #if defined(REAL_DMA) 685 unsigned longmac_scsi_dma_setup(struct Scsi_Host
*instance
,void*data
, 686 unsigned long count
,int dir
) 688 unsigned long addr
=virt_to_phys( data
); 690 DMA_PRINTK("scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, " 691 "dir = %d\n", instance
->host_no
, data
, addr
, count
, dir
); 693 if(!STRAM_ADDR(addr
)) { 694 /* If we have a non-DMAable address on a Falcon, use the dribble 695 * buffer; 'orig_addr' != 0 in the read case tells the interrupt 696 * handler to copy data from the dribble buffer to the originally 700 memcpy( mac_dma_buffer
, data
, count
); 702 mac_dma_orig_addr
= data
; 703 addr
= mac_dma_phys_buffer
; 706 mac_dma_startaddr
= addr
;/* Needed for calculating residual later. */ 708 /* Cache cleanup stuff: On writes, push any dirty cache out before sending 709 * it to the peripheral. (Must be done before DMA setup, since at least 710 * the ST-DMA begins to fill internal buffers right after setup. For 711 * reads, invalidate any cache, may be altered after DMA without CPU 714 * ++roman: For the Medusa, there's no need at all for that cache stuff, 715 * because the hardware does bus snooping (fine!). 717 dma_cache_maintenance( addr
, count
, dir
); 720 printk(KERN_NOTICE
"SCSI warning: DMA programmed for 0 bytes !\n"); 722 mac_scsi_dma
.dma_ctrl
= dir
; 723 SCSI_DMA_WRITE_P( dma_addr
, addr
); 724 SCSI_DMA_WRITE_P( dma_cnt
, count
); 725 mac_scsi_dma
.dma_ctrl
= dir
|2; 731 static longmac_scsi_dma_residual(struct Scsi_Host
*instance
) 733 return( mac_dma_residual
); 737 #define CMD_SURELY_BLOCK_MODE 0 738 #define CMD_SURELY_BYTE_MODE 1 739 #define CMD_MODE_UNKNOWN 2 741 static intmac_classify_cmd( Scsi_Cmnd
*cmd
) 743 unsigned char opcode
= cmd
->cmnd
[0]; 745 if(opcode
== READ_DEFECT_DATA
|| opcode
== READ_LONG
|| 746 opcode
== READ_BUFFER
) 747 return( CMD_SURELY_BYTE_MODE
); 748 else if(opcode
== READ_6
|| opcode
== READ_10
|| 749 opcode
==0xa8/* READ_12 */|| opcode
== READ_REVERSE
|| 750 opcode
== RECOVER_BUFFERED_DATA
) { 751 /* In case of a sequential-access target (tape), special care is 752 * needed here: The transfer is block-mode only if the 'fixed' bit is 754 if(cmd
->device
->type
== TYPE_TAPE
&& !(cmd
->cmnd
[1] &1)) 755 return( CMD_SURELY_BYTE_MODE
); 757 return( CMD_SURELY_BLOCK_MODE
); 760 return( CMD_MODE_UNKNOWN
); 765 #if defined(REAL_DMA) || defined(PSEUDO_DMA) 766 /* This function calculates the number of bytes that can be transferred via 767 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the 768 * ST-DMA chip. There are only multiples of 512 bytes possible and max. 769 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not 770 * possible on the Falcon, since that would require to program the DMA for 771 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have 772 * the overrun problem, so this question is academic :-) 775 static unsigned longmac_dma_xfer_len(unsigned long wanted_len
, 779 unsigned long possible_len
, limit
; 781 #if defined(REAL_DMA) 783 /* TT SCSI DMA can transfer arbitrary #bytes */ 784 return( wanted_len
); 786 /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max. 787 * 255*512 bytes, but this should be enough) 789 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands 790 * that return a number of bytes which cannot be known beforehand. In this 791 * case, the given transfer length is an "allocation length". Now it 792 * can happen that this allocation length is a multiple of 512 bytes and 793 * the DMA is used. But if not n*512 bytes really arrive, some input data 794 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish 795 * between commands that do block transfers and those that do byte 796 * transfers. But this isn't easy... there are lots of vendor specific 797 * commands, and the user can issue any command via the 798 * SCSI_IOCTL_SEND_COMMAND. 800 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s, 801 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1) 802 * and 3), the thing to do is obvious: allow any number of blocks via DMA 803 * or none. In case 2), we apply some heuristic: Byte mode is assumed if 804 * the transfer (allocation) length is < 1024, hoping that no cmd. not 805 * explicitly known as byte mode have such big allocation lengths... 806 * BTW, all the discussion above applies only to reads. DMA writes are 807 * unproblematic anyways, since the targets aborts the transfer after 808 * receiving a sufficient number of bytes. 810 * Another point: If the transfer is from/to an non-ST-RAM address, we 811 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes. 815 /* Write operation can always use the DMA, but the transfer size must 816 * be rounded up to the next multiple of 512 (atari_dma_setup() does 819 possible_len
= wanted_len
; 822 /* Read operations: if the wanted transfer length is not a multiple of 823 * 512, we cannot use DMA, since the ST-DMA cannot split transfers 824 * (no interrupt on DMA finished!) 826 if(wanted_len
&0x1ff) 829 /* Now classify the command (see above) and decide whether it is 830 * allowed to do DMA at all */ 831 switch(falcon_classify_cmd( cmd
)) { 832 case CMD_SURELY_BLOCK_MODE
: 833 possible_len
= wanted_len
; 835 case CMD_SURELY_BYTE_MODE
: 836 possible_len
=0;/* DMA prohibited */ 838 case CMD_MODE_UNKNOWN
: 840 /* For unknown commands assume block transfers if the transfer 841 * size/allocation length is >= 1024 */ 842 possible_len
= (wanted_len
<1024) ?0: wanted_len
; 848 /* Last step: apply the hard limit on DMA transfers */ 849 limit
= (atari_dma_buffer
&& !STRAM_ADDR(virt_to_phys(cmd
->SCp
.ptr
) )) ? 850 STRAM_BUFFER_SIZE
:255*512; 851 if(possible_len
> limit
) 852 possible_len
= limit
; 854 if(possible_len
!= wanted_len
) 855 DMA_PRINTK("Sorry, must cut DMA transfer size to %ld bytes " 856 "instead of %ld\n", possible_len
, wanted_len
); 859 possible_len
= wanted_len
; 862 return( possible_len
); 864 #endif/* REAL_DMA || PSEUDO_DMA */ 871 /* NCR5380 register access functions 874 static unsigned charmac_scsi_reg_read(unsigned char reg
) 876 return( mac_scsi_regp
[reg
<<4] ); 879 static voidmac_scsi_reg_write(unsigned char reg
,unsigned char value
) 881 mac_scsi_regp
[reg
<<4] = value
; 884 #include"mac_NCR5380.c" 889 * slightly optimized PIO transfer routines, experimental! 890 * command may time out if interrupts are left enabled 893 staticinlineintmac_pdma_read(struct Scsi_Host
*instance
,unsigned char*dst
,int len
) 895 registerunsigned char*d
= dst
; 897 registerunsigned char p
, tmp
; 899 #if (NDEBUG & NDEBUG_PSEUDO_DMA) 900 printk("pdma_read: reading %d bytes to %p\n", len
, dst
); 903 mac_pdma_residual
= len
; 904 if(mac_pdma_active
) { 905 printk("pseudo-DMA already active in pread!\n"); 909 mac_pdma_startaddr
= (unsigned long) dst
; 910 mac_pdma_current
= (unsigned long) dst
; 913 * Get the phase from the bus (sanity check) 914 * Hopefully, the phase bits are valid here ... 916 p
=NCR5380_read(STATUS_REG
) & PHASE_MASK
; 918 PDMA_PRINTK("NCR5380_pread: initial phase mismatch!\n"); 919 NCR_PRINT_PHASE(NDEBUG_ANY
); 924 * The NCR5380 chip will only drive the SCSI bus when the 925 * phase specified in the appropriate bits of the TARGET COMMAND 926 * REGISTER match the STATUS REGISTER 929 #if 0/* done in transfer_dma */ 931 NCR5380_write(TARGET_COMMAND_REG
,PHASE_SR_TO_TCR(p
)); 935 HSH_PRINTK(" read %d ..", i
); 936 /* check if we were interrupted ... */ 937 if(!mac_pdma_active
) { 938 printk("pwrite: interrupt detected!\n"); 943 * Wait for assertion of REQ, after which the phase bits will be 946 while(!((tmp
=NCR5380_read(STATUS_REG
)) & SR_REQ
)) 949 HSH_PRINTK(" REQ .."); 951 /* Check for phase mismatch */ 952 if((tmp
& PHASE_MASK
) != p
) { 954 printk("scsi%d : phase mismatch after interrupt\n", instance
->host_no
); 956 printk("scsi%d : phase mismatch w/o interrupt\n", instance
->host_no
); 957 NCR_PRINT_PHASE(NDEBUG_ANY
); 961 /* Do actual transfer from SCSI bus to memory */ 962 *d
=NCR5380_read(CURRENT_SCSI_DATA_REG
); 969 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| ICR_ASSERT_ACK
); 970 HSH_PRINTK(" ACK .."); 972 /* Wait for REQ to be dropped */ 973 while(NCR5380_read(STATUS_REG
) & SR_REQ
) 975 HSH_PRINTK(" /REQ .."); 978 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
); 979 HSH_PRINTK(" /ACK !\n"); 981 mac_pdma_current
= (unsigned long) d
; 985 #if (NDEBUG & NDEBUG_PSEUDO_DMA) 986 printk("pdma_read: read at %d bytes to %p\n", i
, dst
); 989 if(mac_pdma_residual
) 990 printk("pread: leaving with residual %ld of %ld\n", 991 mac_pdma_residual
, len
); 996 NCR5380_write(MODE_REG
, MR_BASE
); 997 NCR5380_read(RESET_PARITY_INTERRUPT_REG
); 1002 staticinlineintmac_pdma_write(struct Scsi_Host
*instance
,unsigned char*src
,int len
) 1004 registerunsigned char*s
= src
; 1006 registerunsigned char p
, tmp
; 1008 #if (NDEBUG & NDEBUG_PSEUDO_DMA) 1009 printk("pdma_write: writing %d bytes from %p\n", len
, src
); 1012 mac_pdma_residual
= len
; 1013 if(mac_pdma_active
) { 1014 printk("pseudo-DMA already active in pwrite!\n"); 1018 mac_pdma_startaddr
= (unsigned long) src
; 1019 mac_pdma_current
= (unsigned long) src
; 1022 * Get the phase from the bus (sanity check) 1024 p
=NCR5380_read(STATUS_REG
) & PHASE_MASK
; 1026 printk("NCR5380_pwrite: initial phase mismatch!\n"); 1027 NCR_PRINT_PHASE(NDEBUG_ANY
); 1032 * The NCR5380 chip will only drive the SCSI bus when the 1033 * phase specified in the appropriate bits of the TARGET COMMAND 1034 * REGISTER match the STATUS REGISTER 1037 #if 0/* already done in transfer_dma */ 1039 NCR5380_write(TARGET_COMMAND_REG
,PHASE_SR_TO_TCR(p
)); 1043 /* check if we were interrupted ... */ 1044 if(!mac_pdma_active
) { 1045 printk("pwrite: interrupt detected!\n"); 1050 * Wait for assertion of REQ, after which the phase bits will be 1053 while(!((tmp
=NCR5380_read(STATUS_REG
)) & SR_REQ
)); 1055 /* Check for phase mismatch */ 1056 if((tmp
& PHASE_MASK
) != p
) { 1057 if(!mac_pdma_active
) 1058 printk("scsi%d : phase mismatch after interrupt\n", instance
->host_no
); 1060 printk("scsi%d : phase mismatch w/o interrupt\n", instance
->host_no
); 1061 NCR_PRINT_PHASE(NDEBUG_ANY
); 1062 /* should we signal an error here?? */ 1066 /* Do actual transfer to SCSI bus from memory */ 1068 NCR5380_write(OUTPUT_DATA_REG
, *s
); 1072 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| 1075 /* Handshake ... assert ACK */ 1076 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
| 1077 ICR_ASSERT_DATA
| ICR_ASSERT_ACK
); 1079 /* ... wait for REQ to be dropped */ 1080 while(NCR5380_read(STATUS_REG
) & SR_REQ
); 1082 /* and drop ACK (and DATA) ! */ 1083 NCR5380_write(INITIATOR_COMMAND_REG
, ICR_BASE
); 1085 mac_pdma_current
= (unsigned long) s
; 1086 mac_pdma_residual
--; 1089 #if (NDEBUG & NDEBUG_PSEUDO_DMA) 1090 printk("pdma_write: write at %d bytes from %p\n", i
, src
); 1093 if(mac_pdma_residual
) 1094 printk("pwrite: leaving with residual %ld of len %ld\n", 1095 mac_pdma_residual
, len
); 1101 #endif/* PSEUDO_DMA */ 1104 Scsi_Host_Template driver_template
= MAC_SCSI
; 1106 #include"scsi_module.c"