atari_scsi.c 32 KB

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