1/* 2 * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port 3 * 4 * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> 5 * 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. 10 * 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. 14 * 15 */ 16 17 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/**************************************************************************/ 65 66 67 68#include <linux/module.h> 69 70#define NDEBUG (0) 71 72#define NDEBUG_ABORT 0x00100000 73#define NDEBUG_TAGS 0x00200000 74#define NDEBUG_MERGING 0x00400000 75 76#define AUTOSENSE 77/* For the Atari version, use only polled IO or REAL_DMA */ 78#define REAL_DMA 79/* Support tagged queuing? (on devices that are able to... :-) */ 80#define SUPPORT_TAGS 81#define MAX_TAGS 32 82 83#include <linux/types.h> 84#include <linux/stddef.h> 85#include <linux/ctype.h> 86#include <linux/delay.h> 87#include <linux/mm.h> 88#include <linux/blkdev.h> 89#include <linux/interrupt.h> 90#include <linux/init.h> 91#include <linux/nvram.h> 92#include <linux/bitops.h> 93 94#include <asm/setup.h> 95#include <asm/atarihw.h> 96#include <asm/atariints.h> 97#include <asm/page.h> 98#include <asm/pgtable.h> 99#include <asm/irq.h> 100#include <asm/traps.h> 101 102#include "scsi.h" 103#include <scsi/scsi_host.h> 104#include "atari_scsi.h" 105#include "NCR5380.h" 106#include <asm/atari_stdma.h> 107#include <asm/atari_stram.h> 108#include <asm/io.h> 109 110#include <linux/stat.h> 111 112#define IS_A_TT() ATARIHW_PRESENT(TT_SCSI) 113 114#define SCSI_DMA_WRITE_P(elt,val) \ 115 do { \ 116 unsigned long v = val; \ 117 tt_scsi_dma.elt##_lo = v & 0xff; \ 118 v >>= 8; \ 119 tt_scsi_dma.elt##_lmd = v & 0xff; \ 120 v >>= 8; \ 121 tt_scsi_dma.elt##_hmd = v & 0xff; \ 122 v >>= 8; \ 123 tt_scsi_dma.elt##_hi = v & 0xff; \ 124 } while(0) 125 126#define SCSI_DMA_READ_P(elt) \ 127 (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) | \ 128 (unsigned long)tt_scsi_dma.elt##_hmd) << 8) | \ 129 (unsigned long)tt_scsi_dma.elt##_lmd) << 8) | \ 130 (unsigned long)tt_scsi_dma.elt##_lo) 131 132 133static inline void SCSI_DMA_SETADR(unsigned long adr) 134{ 135 st_dma.dma_lo = (unsigned char)adr; 136 MFPDELAY(); 137 adr >>= 8; 138 st_dma.dma_md = (unsigned char)adr; 139 MFPDELAY(); 140 adr >>= 8; 141 st_dma.dma_hi = (unsigned char)adr; 142 MFPDELAY(); 143} 144 145static inline unsigned long SCSI_DMA_GETADR(void) 146{ 147 unsigned long adr; 148 adr = st_dma.dma_lo; 149 MFPDELAY(); 150 adr |= (st_dma.dma_md & 0xff) << 8; 151 MFPDELAY(); 152 adr |= (st_dma.dma_hi & 0xff) << 16; 153 MFPDELAY(); 154 return adr; 155} 156 157static inline void ENABLE_IRQ(void) 158{ 159 if (IS_A_TT()) 160 atari_enable_irq(IRQ_TT_MFP_SCSI); 161 else 162 atari_enable_irq(IRQ_MFP_FSCSI); 163} 164 165static inline void DISABLE_IRQ(void) 166{ 167 if (IS_A_TT()) 168 atari_disable_irq(IRQ_TT_MFP_SCSI); 169 else 170 atari_disable_irq(IRQ_MFP_FSCSI); 171} 172 173 174#define HOSTDATA_DMALEN (((struct NCR5380_hostdata *) \ 175 (atari_scsi_host->hostdata))->dma_len) 176 177/* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms, 178 * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more 179 * need ten times the standard value... */ 180#ifndef CONFIG_ATARI_SCSI_TOSHIBA_DELAY 181#define AFTER_RESET_DELAY (HZ/2) 182#else 183#define AFTER_RESET_DELAY (5*HZ/2) 184#endif 185 186/***************************** Prototypes *****************************/ 187 188#ifdef REAL_DMA 189static int scsi_dma_is_ignored_buserr(unsigned char dma_stat); 190static void atari_scsi_fetch_restbytes(void); 191static long atari_scsi_dma_residual(struct Scsi_Host *instance); 192static int falcon_classify_cmd(Scsi_Cmnd *cmd); 193static unsigned long atari_dma_xfer_len(unsigned long wanted_len, 194 Scsi_Cmnd *cmd, int write_flag); 195#endif 196static irqreturn_t scsi_tt_intr(int irq, void *dummy); 197static irqreturn_t scsi_falcon_intr(int irq, void *dummy); 198static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata); 199static void falcon_get_lock(void); 200#ifdef CONFIG_ATARI_SCSI_RESET_BOOT 201static void atari_scsi_reset_boot(void); 202#endif 203static unsigned char atari_scsi_tt_reg_read(unsigned char reg); 204static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value); 205static unsigned char atari_scsi_falcon_reg_read(unsigned char reg); 206static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value); 207 208/************************* End of Prototypes **************************/ 209 210 211static struct Scsi_Host *atari_scsi_host; 212static unsigned char (*atari_scsi_reg_read)(unsigned char reg); 213static void (*atari_scsi_reg_write)(unsigned char reg, unsigned char value); 214 215#ifdef REAL_DMA 216static unsigned long atari_dma_residual, atari_dma_startaddr; 217static short atari_dma_active; 218/* pointer to the dribble buffer */ 219static char *atari_dma_buffer; 220/* precalculated physical address of the dribble buffer */ 221static unsigned long atari_dma_phys_buffer; 222/* != 0 tells the Falcon int handler to copy data from the dribble buffer */ 223static char *atari_dma_orig_addr; 224/* size of the dribble buffer; 4k seems enough, since the Falcon cannot use 225 * scatter-gather anyway, so most transfers are 1024 byte only. In the rare 226 * cases where requests to physical contiguous buffers have been merged, this 227 * request is <= 4k (one page). So I don't think we have to split transfers 228 * just due to this buffer size... 229 */ 230#define STRAM_BUFFER_SIZE (4096) 231/* mask for address bits that can't be used with the ST-DMA */ 232static unsigned long atari_dma_stram_mask; 233#define STRAM_ADDR(a) (((a) & atari_dma_stram_mask) == 0) 234/* number of bytes to cut from a transfer to handle NCR overruns */ 235static int atari_read_overruns; 236#endif 237 238static int setup_can_queue = -1; 239module_param(setup_can_queue, int, 0); 240static int setup_cmd_per_lun = -1; 241module_param(setup_cmd_per_lun, int, 0); 242static int setup_sg_tablesize = -1; 243module_param(setup_sg_tablesize, int, 0); 244#ifdef SUPPORT_TAGS 245static int setup_use_tagged_queuing = -1; 246module_param(setup_use_tagged_queuing, int, 0); 247#endif 248static int setup_hostid = -1; 249module_param(setup_hostid, int, 0); 250 251 252#if defined(CONFIG_TT_DMA_EMUL) 253#include "atari_dma_emul.c" 254#endif 255 256#if defined(REAL_DMA) 257 258static int scsi_dma_is_ignored_buserr(unsigned char dma_stat) 259{ 260 int i; 261 unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr; 262 263 if (dma_stat & 0x01) { 264 265 /* A bus error happens when DMA-ing from the last page of a 266 * physical memory chunk (DMA prefetch!), but that doesn't hurt. 267 * Check for this case: 268 */ 269 270 for (i = 0; i < m68k_num_memory; ++i) { 271 end_addr = m68k_memory[i].addr + m68k_memory[i].size; 272 if (end_addr <= addr && addr <= end_addr + 4) 273 return 1; 274 } 275 } 276 return 0; 277} 278 279 280 281#endif 282 283 284static irqreturn_t scsi_tt_intr(int irq, void *dummy) 285{ 286#ifdef REAL_DMA 287 int dma_stat; 288 289 dma_stat = tt_scsi_dma.dma_ctrl; 290 291 INT_PRINTK("scsi%d: NCR5380 interrupt, DMA status = %02x\n", 292 atari_scsi_host->host_no, dma_stat & 0xff); 293 294 /* Look if it was the DMA that has interrupted: First possibility 295 * is that a bus error occurred... 296 */ 297 if (dma_stat & 0x80) { 298 if (!scsi_dma_is_ignored_buserr(dma_stat)) { 299 printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n", 300 SCSI_DMA_READ_P(dma_addr)); 301 printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!"); 302 } 303 } 304 305 /* If the DMA is active but not finished, we have the case 306 * that some other 5380 interrupt occurred within the DMA transfer. 307 * This means we have residual bytes, if the desired end address 308 * is not yet reached. Maybe we have to fetch some bytes from the 309 * rest data register, too. The residual must be calculated from 310 * the address pointer, not the counter register, because only the 311 * addr reg counts bytes not yet written and pending in the rest 312 * data reg! 313 */ 314 if ((dma_stat & 0x02) && !(dma_stat & 0x40)) { 315 atari_dma_residual = HOSTDATA_DMALEN - (SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr); 316 317 DMA_PRINTK("SCSI DMA: There are %ld residual bytes.\n", 318 atari_dma_residual); 319 320 if ((signed int)atari_dma_residual < 0) 321 atari_dma_residual = 0; 322 if ((dma_stat & 1) == 0) { 323 /* 324 * After read operations, we maybe have to 325 * transport some rest bytes 326 */ 327 atari_scsi_fetch_restbytes(); 328 } else { 329 /* 330 * There seems to be a nasty bug in some SCSI-DMA/NCR 331 * combinations: If a target disconnects while a write 332 * operation is going on, the address register of the 333 * DMA may be a few bytes farer than it actually read. 334 * This is probably due to DMA prefetching and a delay 335 * between DMA and NCR. Experiments showed that the 336 * dma_addr is 9 bytes to high, but this could vary. 337 * The problem is, that the residual is thus calculated 338 * wrong and the next transfer will start behind where 339 * it should. So we round up the residual to the next 340 * multiple of a sector size, if it isn't already a 341 * multiple and the originally expected transfer size 342 * was. The latter condition is there to ensure that 343 * the correction is taken only for "real" data 344 * transfers and not for, e.g., the parameters of some 345 * other command. These shouldn't disconnect anyway. 346 */ 347 if (atari_dma_residual & 0x1ff) { 348 DMA_PRINTK("SCSI DMA: DMA bug corrected, " 349 "difference %ld bytes\n", 350 512 - (atari_dma_residual & 0x1ff)); 351 atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff; 352 } 353 } 354 tt_scsi_dma.dma_ctrl = 0; 355 } 356 357 /* If the DMA is finished, fetch the rest bytes and turn it off */ 358 if (dma_stat & 0x40) { 359 atari_dma_residual = 0; 360 if ((dma_stat & 1) == 0) 361 atari_scsi_fetch_restbytes(); 362 tt_scsi_dma.dma_ctrl = 0; 363 } 364 365#endif /* REAL_DMA */ 366 367 NCR5380_intr(0, 0); 368 369 return IRQ_HANDLED; 370} 371 372 373static irqreturn_t scsi_falcon_intr(int irq, void *dummy) 374{ 375#ifdef REAL_DMA 376 int dma_stat; 377 378 /* Turn off DMA and select sector counter register before 379 * accessing the status register (Atari recommendation!) 380 */ 381 st_dma.dma_mode_status = 0x90; 382 dma_stat = st_dma.dma_mode_status; 383 384 /* Bit 0 indicates some error in the DMA process... don't know 385 * what happened exactly (no further docu). 386 */ 387 if (!(dma_stat & 0x01)) { 388 /* DMA error */ 389 printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR()); 390 } 391 392 /* If the DMA was active, but now bit 1 is not clear, it is some 393 * other 5380 interrupt that finishes the DMA transfer. We have to 394 * calculate the number of residual bytes and give a warning if 395 * bytes are stuck in the ST-DMA fifo (there's no way to reach them!) 396 */ 397 if (atari_dma_active && (dma_stat & 0x02)) { 398 unsigned long transferred; 399 400 transferred = SCSI_DMA_GETADR() - atari_dma_startaddr; 401 /* The ST-DMA address is incremented in 2-byte steps, but the 402 * data are written only in 16-byte chunks. If the number of 403 * transferred bytes is not divisible by 16, the remainder is 404 * lost somewhere in outer space. 405 */ 406 if (transferred & 15) 407 printk(KERN_ERR "SCSI DMA error: %ld bytes lost in " 408 "ST-DMA fifo\n", transferred & 15); 409 410 atari_dma_residual = HOSTDATA_DMALEN - transferred; 411 DMA_PRINTK("SCSI DMA: There are %ld residual bytes.\n", 412 atari_dma_residual); 413 } else 414 atari_dma_residual = 0; 415 atari_dma_active = 0; 416 417 if (atari_dma_orig_addr) { 418 /* If the dribble buffer was used on a read operation, copy the DMA-ed 419 * data to the original destination address. 420 */ 421 memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr), 422 HOSTDATA_DMALEN - atari_dma_residual); 423 atari_dma_orig_addr = NULL; 424 } 425 426#endif /* REAL_DMA */ 427 428 NCR5380_intr(0, 0); 429 return IRQ_HANDLED; 430} 431 432 433#ifdef REAL_DMA 434static void atari_scsi_fetch_restbytes(void) 435{ 436 int nr; 437 char *src, *dst; 438 unsigned long phys_dst; 439 440 /* fetch rest bytes in the DMA register */ 441 phys_dst = SCSI_DMA_READ_P(dma_addr); 442 nr = phys_dst & 3; 443 if (nr) { 444 /* there are 'nr' bytes left for the last long address 445 before the DMA pointer */ 446 phys_dst ^= nr; 447 DMA_PRINTK("SCSI DMA: there are %d rest bytes for phys addr 0x%08lx", 448 nr, phys_dst); 449 /* The content of the DMA pointer is a physical address! */ 450 dst = phys_to_virt(phys_dst); 451 DMA_PRINTK(" = virt addr %p\n", dst); 452 for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr) 453 *dst++ = *src++; 454 } 455} 456#endif /* REAL_DMA */ 457 458 459static int falcon_got_lock = 0; 460static DECLARE_WAIT_QUEUE_HEAD(falcon_fairness_wait); 461static int falcon_trying_lock = 0; 462static DECLARE_WAIT_QUEUE_HEAD(falcon_try_wait); 463static int falcon_dont_release = 0; 464 465/* This function releases the lock on the DMA chip if there is no 466 * connected command and the disconnected queue is empty. On 467 * releasing, instances of falcon_get_lock are awoken, that put 468 * themselves to sleep for fairness. They can now try to get the lock 469 * again (but others waiting longer more probably will win). 470 */ 471 472static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata) 473{ 474 unsigned long flags; 475 476 if (IS_A_TT()) 477 return; 478 479 local_irq_save(flags); 480 481 if (falcon_got_lock && !hostdata->disconnected_queue && 482 !hostdata->issue_queue && !hostdata->connected) { 483 484 if (falcon_dont_release) { 485 local_irq_restore(flags); 486 return; 487 } 488 falcon_got_lock = 0; 489 stdma_release(); 490 wake_up(&falcon_fairness_wait); 491 } 492 493 local_irq_restore(flags); 494} 495 496/* This function manages the locking of the ST-DMA. 497 * If the DMA isn't locked already for SCSI, it tries to lock it by 498 * calling stdma_lock(). But if the DMA is locked by the SCSI code and 499 * there are other drivers waiting for the chip, we do not issue the 500 * command immediately but wait on 'falcon_fairness_queue'. We will be 501 * waked up when the DMA is unlocked by some SCSI interrupt. After that 502 * we try to get the lock again. 503 * But we must be prepared that more than one instance of 504 * falcon_get_lock() is waiting on the fairness queue. They should not 505 * try all at once to call stdma_lock(), one is enough! For that, the 506 * first one sets 'falcon_trying_lock', others that see that variable 507 * set wait on the queue 'falcon_try_wait'. 508 * Complicated, complicated.... Sigh... 509 */ 510 511static void falcon_get_lock(void) 512{ 513 unsigned long flags; 514 515 if (IS_A_TT()) 516 return; 517 518 local_irq_save(flags); 519 520 while (!in_irq() && falcon_got_lock && stdma_others_waiting()) 521 sleep_on(&falcon_fairness_wait); 522 523 while (!falcon_got_lock) { 524 if (in_irq()) 525 panic("Falcon SCSI hasn't ST-DMA lock in interrupt"); 526 if (!falcon_trying_lock) { 527 falcon_trying_lock = 1; 528 stdma_lock(scsi_falcon_intr, NULL); 529 falcon_got_lock = 1; 530 falcon_trying_lock = 0; 531 wake_up(&falcon_try_wait); 532 } else { 533 sleep_on(&falcon_try_wait); 534 } 535 } 536 537 local_irq_restore(flags); 538 if (!falcon_got_lock) 539 panic("Falcon SCSI: someone stole the lock :-(\n"); 540} 541 542 543/* This is the wrapper function for NCR5380_queue_command(). It just 544 * tries to get the lock on the ST-DMA (see above) and then calls the 545 * original function. 546 */ 547 548 549 550int atari_scsi_detect(struct scsi_host_template *host) 551{ 552 static int called = 0; 553 struct Scsi_Host *instance; 554 555 if (!MACH_IS_ATARI || 556 (!ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(TT_SCSI)) || 557 called) 558 return 0; 559 560 host->proc_name = "Atari"; 561 562 atari_scsi_reg_read = IS_A_TT() ? atari_scsi_tt_reg_read : 563 atari_scsi_falcon_reg_read; 564 atari_scsi_reg_write = IS_A_TT() ? atari_scsi_tt_reg_write : 565 atari_scsi_falcon_reg_write; 566 567 /* setup variables */ 568 host->can_queue = 569 (setup_can_queue > 0) ? setup_can_queue : 570 IS_A_TT() ? ATARI_TT_CAN_QUEUE : ATARI_FALCON_CAN_QUEUE; 571 host->cmd_per_lun = 572 (setup_cmd_per_lun > 0) ? setup_cmd_per_lun : 573 IS_A_TT() ? ATARI_TT_CMD_PER_LUN : ATARI_FALCON_CMD_PER_LUN; 574 /* Force sg_tablesize to 0 on a Falcon! */ 575 host->sg_tablesize = 576 !IS_A_TT() ? ATARI_FALCON_SG_TABLESIZE : 577 (setup_sg_tablesize >= 0) ? setup_sg_tablesize : ATARI_TT_SG_TABLESIZE; 578 579 if (setup_hostid >= 0) 580 host->this_id = setup_hostid; 581 else { 582 /* use 7 as default */ 583 host->this_id = 7; 584 /* Test if a host id is set in the NVRam */ 585 if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) { 586 unsigned char b = nvram_read_byte( 14 ); 587 /* Arbitration enabled? (for TOS) If yes, use configured host ID */ 588 if (b & 0x80) 589 host->this_id = b & 7; 590 } 591 } 592 593#ifdef SUPPORT_TAGS 594 if (setup_use_tagged_queuing < 0) 595 setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING; 596#endif 597#ifdef REAL_DMA 598 /* If running on a Falcon and if there's TT-Ram (i.e., more than one 599 * memory block, since there's always ST-Ram in a Falcon), then allocate a 600 * STRAM_BUFFER_SIZE byte dribble buffer for transfers from/to alternative 601 * Ram. 602 */ 603 if (MACH_IS_ATARI && ATARIHW_PRESENT(ST_SCSI) && 604 !ATARIHW_PRESENT(EXTD_DMA) && m68k_num_memory > 1) { 605 atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI"); 606 if (!atari_dma_buffer) { 607 printk(KERN_ERR "atari_scsi_detect: can't allocate ST-RAM " 608 "double buffer\n"); 609 return 0; 610 } 611 atari_dma_phys_buffer = virt_to_phys(atari_dma_buffer); 612 atari_dma_orig_addr = 0; 613 } 614#endif 615 instance = scsi_register(host, sizeof(struct NCR5380_hostdata)); 616 if (instance == NULL) { 617 atari_stram_free(atari_dma_buffer); 618 atari_dma_buffer = 0; 619 return 0; 620 } 621 atari_scsi_host = instance; 622 /* 623 * Set irq to 0, to avoid that the mid-level code disables our interrupt 624 * during queue_command calls. This is completely unnecessary, and even 625 * worse causes bad problems on the Falcon, where the int is shared with 626 * IDE and floppy! 627 */ 628 instance->irq = 0; 629 630#ifdef CONFIG_ATARI_SCSI_RESET_BOOT 631 atari_scsi_reset_boot(); 632#endif 633 NCR5380_init(instance, 0); 634 635 if (IS_A_TT()) { 636 637 /* This int is actually "pseudo-slow", i.e. it acts like a slow 638 * interrupt after having cleared the pending flag for the DMA 639 * interrupt. */ 640 if (request_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr, IRQ_TYPE_SLOW, 641 "SCSI NCR5380", scsi_tt_intr)) { 642 printk(KERN_ERR "atari_scsi_detect: cannot allocate irq %d, aborting",IRQ_TT_MFP_SCSI); 643 scsi_unregister(atari_scsi_host); 644 atari_stram_free(atari_dma_buffer); 645 atari_dma_buffer = 0; 646 return 0; 647 } 648 tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */ 649#ifdef REAL_DMA 650 tt_scsi_dma.dma_ctrl = 0; 651 atari_dma_residual = 0; 652#ifdef CONFIG_TT_DMA_EMUL 653 if (MACH_IS_HADES) { 654 if (request_irq(IRQ_AUTO_2, hades_dma_emulator, 655 IRQ_TYPE_PRIO, "Hades DMA emulator", 656 hades_dma_emulator)) { 657 printk(KERN_ERR "atari_scsi_detect: cannot allocate irq %d, aborting (MACH_IS_HADES)",IRQ_AUTO_2); 658 free_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr); 659 scsi_unregister(atari_scsi_host); 660 atari_stram_free(atari_dma_buffer); 661 atari_dma_buffer = 0; 662 return 0; 663 } 664 } 665#endif 666 if (MACH_IS_MEDUSA || MACH_IS_HADES) { 667 /* While the read overruns (described by Drew Eckhardt in 668 * NCR5380.c) never happened on TTs, they do in fact on the Medusa 669 * (This was the cause why SCSI didn't work right for so long 670 * there.) Since handling the overruns slows down a bit, I turned 671 * the #ifdef's into a runtime condition. 672 * 673 * In principle it should be sufficient to do max. 1 byte with 674 * PIO, but there is another problem on the Medusa with the DMA 675 * rest data register. So 'atari_read_overruns' is currently set 676 * to 4 to avoid having transfers that aren't a multiple of 4. If 677 * the rest data bug is fixed, this can be lowered to 1. 678 */ 679 atari_read_overruns = 4; 680 } 681#endif /*REAL_DMA*/ 682 } else { /* ! IS_A_TT */ 683 684 /* Nothing to do for the interrupt: the ST-DMA is initialized 685 * already by atari_init_INTS() 686 */ 687 688#ifdef REAL_DMA 689 atari_dma_residual = 0; 690 atari_dma_active = 0; 691 atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000 692 : 0xff000000); 693#endif 694 } 695 696 printk(KERN_INFO "scsi%d: options CAN_QUEUE=%d CMD_PER_LUN=%d SCAT-GAT=%d " 697#ifdef SUPPORT_TAGS 698 "TAGGED-QUEUING=%s " 699#endif 700 "HOSTID=%d", 701 instance->host_no, instance->hostt->can_queue, 702 instance->hostt->cmd_per_lun, 703 instance->hostt->sg_tablesize, 704#ifdef SUPPORT_TAGS 705 setup_use_tagged_queuing ? "yes" : "no", 706#endif 707 instance->hostt->this_id ); 708 NCR5380_print_options(instance); 709 printk("\n"); 710 711 called = 1; 712 return 1; 713} 714 715int atari_scsi_release(struct Scsi_Host *sh) 716{ 717 if (IS_A_TT()) 718 free_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr); 719 if (atari_dma_buffer) 720 atari_stram_free(atari_dma_buffer); 721 return 1; 722} 723 724void __init atari_scsi_setup(char *str, int *ints) 725{ 726 /* Format of atascsi parameter is: 727 * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags> 728 * Defaults depend on TT or Falcon, hostid determined at run time. 729 * Negative values mean don't change. 730 */ 731 732 if (ints[0] < 1) { 733 printk("atari_scsi_setup: no arguments!\n"); 734 return; 735 } 736 737 if (ints[0] >= 1) { 738 if (ints[1] > 0) 739 /* no limits on this, just > 0 */ 740 setup_can_queue = ints[1]; 741 } 742 if (ints[0] >= 2) { 743 if (ints[2] > 0) 744 setup_cmd_per_lun = ints[2]; 745 } 746 if (ints[0] >= 3) { 747 if (ints[3] >= 0) { 748 setup_sg_tablesize = ints[3]; 749 /* Must be <= SG_ALL (255) */ 750 if (setup_sg_tablesize > SG_ALL) 751 setup_sg_tablesize = SG_ALL; 752 } 753 } 754 if (ints[0] >= 4) { 755 /* Must be between 0 and 7 */ 756 if (ints[4] >= 0 && ints[4] <= 7) 757 setup_hostid = ints[4]; 758 else if (ints[4] > 7) 759 printk("atari_scsi_setup: invalid host ID %d !\n", ints[4]); 760 } 761#ifdef SUPPORT_TAGS 762 if (ints[0] >= 5) { 763 if (ints[5] >= 0) 764 setup_use_tagged_queuing = !!ints[5]; 765 } 766#endif 767} 768 769int atari_scsi_bus_reset(Scsi_Cmnd *cmd) 770{ 771 int rv; 772 struct NCR5380_hostdata *hostdata = 773 (struct NCR5380_hostdata *)cmd->device->host->hostdata; 774 775 /* For doing the reset, SCSI interrupts must be disabled first, 776 * since the 5380 raises its IRQ line while _RST is active and we 777 * can't disable interrupts completely, since we need the timer. 778 */ 779 /* And abort a maybe active DMA transfer */ 780 if (IS_A_TT()) { 781 atari_turnoff_irq(IRQ_TT_MFP_SCSI); 782#ifdef REAL_DMA 783 tt_scsi_dma.dma_ctrl = 0; 784#endif /* REAL_DMA */ 785 } else { 786 atari_turnoff_irq(IRQ_MFP_FSCSI); 787#ifdef REAL_DMA 788 st_dma.dma_mode_status = 0x90; 789 atari_dma_active = 0; 790 atari_dma_orig_addr = NULL; 791#endif /* REAL_DMA */ 792 } 793 794 rv = NCR5380_bus_reset(cmd); 795 796 /* Re-enable ints */ 797 if (IS_A_TT()) { 798 atari_turnon_irq(IRQ_TT_MFP_SCSI); 799 } else { 800 atari_turnon_irq(IRQ_MFP_FSCSI); 801 } 802 if ((rv & SCSI_RESET_ACTION) == SCSI_RESET_SUCCESS) 803 falcon_release_lock_if_possible(hostdata); 804 805 return rv; 806} 807 808 809#ifdef CONFIG_ATARI_SCSI_RESET_BOOT 810static void __init atari_scsi_reset_boot(void) 811{ 812 unsigned long end; 813 814 /* 815 * Do a SCSI reset to clean up the bus during initialization. No messing 816 * with the queues, interrupts, or locks necessary here. 817 */ 818 819 printk("Atari SCSI: resetting the SCSI bus..."); 820 821 /* get in phase */ 822 NCR5380_write(TARGET_COMMAND_REG, 823 PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG))); 824 825 /* assert RST */ 826 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST); 827 /* The min. reset hold time is 25us, so 40us should be enough */ 828 udelay(50); 829 /* reset RST and interrupt */ 830 NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); 831 NCR5380_read(RESET_PARITY_INTERRUPT_REG); 832 833 end = jiffies + AFTER_RESET_DELAY; 834 while (time_before(jiffies, end)) 835 barrier(); 836 837 printk(" done\n"); 838} 839#endif 840 841 842const char *atari_scsi_info(struct Scsi_Host *host) 843{ 844 /* atari_scsi_detect() is verbose enough... */ 845 static const char string[] = "Atari native SCSI"; 846 return string; 847} 848 849 850#if defined(REAL_DMA) 851 852unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance, void *data, 853 unsigned long count, int dir) 854{ 855 unsigned long addr = virt_to_phys(data); 856 857 DMA_PRINTK("scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, " 858 "dir = %d\n", instance->host_no, data, addr, count, dir); 859 860 if (!IS_A_TT() && !STRAM_ADDR(addr)) { 861 /* If we have a non-DMAable address on a Falcon, use the dribble 862 * buffer; 'orig_addr' != 0 in the read case tells the interrupt 863 * handler to copy data from the dribble buffer to the originally 864 * wanted address. 865 */ 866 if (dir) 867 memcpy(atari_dma_buffer, data, count); 868 else 869 atari_dma_orig_addr = data; 870 addr = atari_dma_phys_buffer; 871 } 872 873 atari_dma_startaddr = addr; /* Needed for calculating residual later. */ 874 875 /* Cache cleanup stuff: On writes, push any dirty cache out before sending 876 * it to the peripheral. (Must be done before DMA setup, since at least 877 * the ST-DMA begins to fill internal buffers right after setup. For 878 * reads, invalidate any cache, may be altered after DMA without CPU 879 * knowledge. 880 * 881 * ++roman: For the Medusa, there's no need at all for that cache stuff, 882 * because the hardware does bus snooping (fine!). 883 */ 884 dma_cache_maintenance(addr, count, dir); 885 886 if (count == 0) 887 printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n"); 888 889 if (IS_A_TT()) { 890 tt_scsi_dma.dma_ctrl = dir; 891 SCSI_DMA_WRITE_P(dma_addr, addr); 892 SCSI_DMA_WRITE_P(dma_cnt, count); 893 tt_scsi_dma.dma_ctrl = dir | 2; 894 } else { /* ! IS_A_TT */ 895 896 /* set address */ 897 SCSI_DMA_SETADR(addr); 898 899 /* toggle direction bit to clear FIFO and set DMA direction */ 900 dir <<= 8; 901 st_dma.dma_mode_status = 0x90 | dir; 902 st_dma.dma_mode_status = 0x90 | (dir ^ 0x100); 903 st_dma.dma_mode_status = 0x90 | dir; 904 udelay(40); 905 /* On writes, round up the transfer length to the next multiple of 512 906 * (see also comment at atari_dma_xfer_len()). */ 907 st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9; 908 udelay(40); 909 st_dma.dma_mode_status = 0x10 | dir; 910 udelay(40); 911 /* need not restore value of dir, only boolean value is tested */ 912 atari_dma_active = 1; 913 } 914 915 return count; 916} 917 918 919static long atari_scsi_dma_residual(struct Scsi_Host *instance) 920{ 921 return atari_dma_residual; 922} 923 924 925#define CMD_SURELY_BLOCK_MODE 0 926#define CMD_SURELY_BYTE_MODE 1 927#define CMD_MODE_UNKNOWN 2 928 929static int falcon_classify_cmd(Scsi_Cmnd *cmd) 930{ 931 unsigned char opcode = cmd->cmnd[0]; 932 933 if (opcode == READ_DEFECT_DATA || opcode == READ_LONG || 934 opcode == READ_BUFFER) 935 return CMD_SURELY_BYTE_MODE; 936 else if (opcode == READ_6 || opcode == READ_10 || 937 opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE || 938 opcode == RECOVER_BUFFERED_DATA) { 939 /* In case of a sequential-access target (tape), special care is 940 * needed here: The transfer is block-mode only if the 'fixed' bit is 941 * set! */ 942 if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1)) 943 return CMD_SURELY_BYTE_MODE; 944 else 945 return CMD_SURELY_BLOCK_MODE; 946 } else 947 return CMD_MODE_UNKNOWN; 948} 949 950 951/* This function calculates the number of bytes that can be transferred via 952 * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the 953 * ST-DMA chip. There are only multiples of 512 bytes possible and max. 954 * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not 955 * possible on the Falcon, since that would require to program the DMA for 956 * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have 957 * the overrun problem, so this question is academic :-) 958 */ 959 960static unsigned long atari_dma_xfer_len(unsigned long wanted_len, 961 Scsi_Cmnd *cmd, int write_flag) 962{ 963 unsigned long possible_len, limit; 964#ifndef CONFIG_TT_DMA_EMUL 965 if (MACH_IS_HADES) 966 /* Hades has no SCSI DMA at all :-( Always force use of PIO */ 967 return 0; 968#endif 969 if (IS_A_TT()) 970 /* TT SCSI DMA can transfer arbitrary #bytes */ 971 return wanted_len; 972 973 /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max. 974 * 255*512 bytes, but this should be enough) 975 * 976 * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands 977 * that return a number of bytes which cannot be known beforehand. In this 978 * case, the given transfer length is an "allocation length". Now it 979 * can happen that this allocation length is a multiple of 512 bytes and 980 * the DMA is used. But if not n*512 bytes really arrive, some input data 981 * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish 982 * between commands that do block transfers and those that do byte 983 * transfers. But this isn't easy... there are lots of vendor specific 984 * commands, and the user can issue any command via the 985 * SCSI_IOCTL_SEND_COMMAND. 986 * 987 * The solution: We classify SCSI commands in 1) surely block-mode cmd.s, 988 * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1) 989 * and 3), the thing to do is obvious: allow any number of blocks via DMA 990 * or none. In case 2), we apply some heuristic: Byte mode is assumed if 991 * the transfer (allocation) length is < 1024, hoping that no cmd. not 992 * explicitly known as byte mode have such big allocation lengths... 993 * BTW, all the discussion above applies only to reads. DMA writes are 994 * unproblematic anyways, since the targets aborts the transfer after 995 * receiving a sufficient number of bytes. 996 * 997 * Another point: If the transfer is from/to an non-ST-RAM address, we 998 * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes. 999 */ 1000 1001 if (write_flag) { 1002 /* Write operation can always use the DMA, but the transfer size must 1003 * be rounded up to the next multiple of 512 (atari_dma_setup() does 1004 * this). 1005 */ 1006 possible_len = wanted_len; 1007 } else { 1008 /* Read operations: if the wanted transfer length is not a multiple of 1009 * 512, we cannot use DMA, since the ST-DMA cannot split transfers 1010 * (no interrupt on DMA finished!) 1011 */ 1012 if (wanted_len & 0x1ff) 1013 possible_len = 0; 1014 else { 1015 /* Now classify the command (see above) and decide whether it is 1016 * allowed to do DMA at all */ 1017 switch (falcon_classify_cmd(cmd)) { 1018 case CMD_SURELY_BLOCK_MODE: 1019 possible_len = wanted_len; 1020 break; 1021 case CMD_SURELY_BYTE_MODE: 1022 possible_len = 0; /* DMA prohibited */ 1023 break; 1024 case CMD_MODE_UNKNOWN: 1025 default: 1026 /* For unknown commands assume block transfers if the transfer 1027 * size/allocation length is >= 1024 */ 1028 possible_len = (wanted_len < 1024) ? 0 : wanted_len; 1029 break; 1030 } 1031 } 1032 } 1033 1034 /* Last step: apply the hard limit on DMA transfers */ 1035 limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ? 1036 STRAM_BUFFER_SIZE : 255*512; 1037 if (possible_len > limit) 1038 possible_len = limit; 1039 1040 if (possible_len != wanted_len) 1041 DMA_PRINTK("Sorry, must cut DMA transfer size to %ld bytes " 1042 "instead of %ld\n", possible_len, wanted_len); 1043 1044 return possible_len; 1045} 1046 1047 1048#endif /* REAL_DMA */ 1049 1050 1051/* NCR5380 register access functions 1052 * 1053 * There are separate functions for TT and Falcon, because the access 1054 * methods are quite different. The calling macros NCR5380_read and 1055 * NCR5380_write call these functions via function pointers. 1056 */ 1057 1058static unsigned char atari_scsi_tt_reg_read(unsigned char reg) 1059{ 1060 return tt_scsi_regp[reg * 2]; 1061} 1062 1063static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value) 1064{ 1065 tt_scsi_regp[reg * 2] = value; 1066} 1067 1068static unsigned char atari_scsi_falcon_reg_read(unsigned char reg) 1069{ 1070 dma_wd.dma_mode_status= (u_short)(0x88 + reg); 1071 return (u_char)dma_wd.fdc_acces_seccount; 1072} 1073 1074static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value) 1075{ 1076 dma_wd.dma_mode_status = (u_short)(0x88 + reg); 1077 dma_wd.fdc_acces_seccount = (u_short)value; 1078} 1079 1080 1081#include "atari_NCR5380.c" 1082 1083static struct scsi_host_template driver_template = { 1084 .proc_info = atari_scsi_proc_info, 1085 .name = "Atari native SCSI", 1086 .detect = atari_scsi_detect, 1087 .release = atari_scsi_release, 1088 .info = atari_scsi_info, 1089 .queuecommand = atari_scsi_queue_command, 1090 .eh_abort_handler = atari_scsi_abort, 1091 .eh_bus_reset_handler = atari_scsi_bus_reset, 1092 .can_queue = 0, /* initialized at run-time */ 1093 .this_id = 0, /* initialized at run-time */ 1094 .sg_tablesize = 0, /* initialized at run-time */ 1095 .cmd_per_lun = 0, /* initialized at run-time */ 1096 .use_clustering = DISABLE_CLUSTERING 1097}; 1098 1099 1100#include "scsi_module.c" 1101 1102MODULE_LICENSE("GPL"); 1103