atari_scsi.c 35 KB

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