atari_scsi.c 27 KB

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