atari_scsi.c 27 KB

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