atari_scsi.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:36k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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