atari_scsi.c 28 KB

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