atari_scsi.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. 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. instance->hostt->can_queue > 1)
  322. return 1;
  323. if (in_interrupt())
  324. return stdma_try_lock(scsi_falcon_intr, instance);
  325. stdma_lock(scsi_falcon_intr, instance);
  326. return 1;
  327. }
  328. #ifndef MODULE
  329. static int __init atari_scsi_setup(char *str)
  330. {
  331. /* Format of atascsi parameter is:
  332. * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
  333. * Defaults depend on TT or Falcon, determined at run time.
  334. * Negative values mean don't change.
  335. */
  336. int ints[8];
  337. get_options(str, ARRAY_SIZE(ints), ints);
  338. if (ints[0] < 1) {
  339. printk("atari_scsi_setup: no arguments!\n");
  340. return 0;
  341. }
  342. if (ints[0] >= 1)
  343. setup_can_queue = ints[1];
  344. if (ints[0] >= 2)
  345. setup_cmd_per_lun = ints[2];
  346. if (ints[0] >= 3)
  347. setup_sg_tablesize = ints[3];
  348. if (ints[0] >= 4)
  349. setup_hostid = ints[4];
  350. /* ints[5] (use_tagged_queuing) is ignored */
  351. /* ints[6] (use_pdma) is ignored */
  352. if (ints[0] >= 7)
  353. setup_toshiba_delay = ints[7];
  354. return 1;
  355. }
  356. __setup("atascsi=", atari_scsi_setup);
  357. #endif /* !MODULE */
  358. static unsigned long atari_scsi_dma_setup(struct NCR5380_hostdata *hostdata,
  359. void *data, unsigned long count,
  360. int dir)
  361. {
  362. unsigned long addr = virt_to_phys(data);
  363. dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, dir = %d\n",
  364. hostdata->host->host_no, data, addr, count, dir);
  365. if (!IS_A_TT() && !STRAM_ADDR(addr)) {
  366. /* If we have a non-DMAable address on a Falcon, use the dribble
  367. * buffer; 'orig_addr' != 0 in the read case tells the interrupt
  368. * handler to copy data from the dribble buffer to the originally
  369. * wanted address.
  370. */
  371. if (dir)
  372. memcpy(atari_dma_buffer, data, count);
  373. else
  374. atari_dma_orig_addr = data;
  375. addr = atari_dma_phys_buffer;
  376. }
  377. atari_dma_startaddr = addr; /* Needed for calculating residual later. */
  378. /* Cache cleanup stuff: On writes, push any dirty cache out before sending
  379. * it to the peripheral. (Must be done before DMA setup, since at least
  380. * the ST-DMA begins to fill internal buffers right after setup. For
  381. * reads, invalidate any cache, may be altered after DMA without CPU
  382. * knowledge.
  383. *
  384. * ++roman: For the Medusa, there's no need at all for that cache stuff,
  385. * because the hardware does bus snooping (fine!).
  386. */
  387. dma_cache_maintenance(addr, count, dir);
  388. if (IS_A_TT()) {
  389. tt_scsi_dma.dma_ctrl = dir;
  390. SCSI_DMA_WRITE_P(dma_addr, addr);
  391. SCSI_DMA_WRITE_P(dma_cnt, count);
  392. tt_scsi_dma.dma_ctrl = dir | 2;
  393. } else { /* ! IS_A_TT */
  394. /* set address */
  395. SCSI_DMA_SETADR(addr);
  396. /* toggle direction bit to clear FIFO and set DMA direction */
  397. dir <<= 8;
  398. st_dma.dma_mode_status = 0x90 | dir;
  399. st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
  400. st_dma.dma_mode_status = 0x90 | dir;
  401. udelay(40);
  402. /* On writes, round up the transfer length to the next multiple of 512
  403. * (see also comment at atari_dma_xfer_len()). */
  404. st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
  405. udelay(40);
  406. st_dma.dma_mode_status = 0x10 | dir;
  407. udelay(40);
  408. /* need not restore value of dir, only boolean value is tested */
  409. atari_dma_active = 1;
  410. }
  411. return count;
  412. }
  413. static inline int atari_scsi_dma_recv_setup(struct NCR5380_hostdata *hostdata,
  414. unsigned char *data, int count)
  415. {
  416. return atari_scsi_dma_setup(hostdata, data, count, 0);
  417. }
  418. static inline int atari_scsi_dma_send_setup(struct NCR5380_hostdata *hostdata,
  419. unsigned char *data, int count)
  420. {
  421. return atari_scsi_dma_setup(hostdata, data, count, 1);
  422. }
  423. static int atari_scsi_dma_residual(struct NCR5380_hostdata *hostdata)
  424. {
  425. return atari_dma_residual;
  426. }
  427. #define CMD_SURELY_BLOCK_MODE 0
  428. #define CMD_SURELY_BYTE_MODE 1
  429. #define CMD_MODE_UNKNOWN 2
  430. static int falcon_classify_cmd(struct scsi_cmnd *cmd)
  431. {
  432. unsigned char opcode = cmd->cmnd[0];
  433. if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
  434. opcode == READ_BUFFER)
  435. return CMD_SURELY_BYTE_MODE;
  436. else if (opcode == READ_6 || opcode == READ_10 ||
  437. opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
  438. opcode == RECOVER_BUFFERED_DATA) {
  439. /* In case of a sequential-access target (tape), special care is
  440. * needed here: The transfer is block-mode only if the 'fixed' bit is
  441. * set! */
  442. if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
  443. return CMD_SURELY_BYTE_MODE;
  444. else
  445. return CMD_SURELY_BLOCK_MODE;
  446. } else
  447. return CMD_MODE_UNKNOWN;
  448. }
  449. /* This function calculates the number of bytes that can be transferred via
  450. * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
  451. * ST-DMA chip. There are only multiples of 512 bytes possible and max.
  452. * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
  453. * possible on the Falcon, since that would require to program the DMA for
  454. * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
  455. * the overrun problem, so this question is academic :-)
  456. */
  457. static int atari_scsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
  458. struct scsi_cmnd *cmd)
  459. {
  460. int wanted_len = cmd->SCp.this_residual;
  461. int possible_len, limit;
  462. if (wanted_len < DMA_MIN_SIZE)
  463. return 0;
  464. if (IS_A_TT())
  465. /* TT SCSI DMA can transfer arbitrary #bytes */
  466. return wanted_len;
  467. /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
  468. * 255*512 bytes, but this should be enough)
  469. *
  470. * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
  471. * that return a number of bytes which cannot be known beforehand. In this
  472. * case, the given transfer length is an "allocation length". Now it
  473. * can happen that this allocation length is a multiple of 512 bytes and
  474. * the DMA is used. But if not n*512 bytes really arrive, some input data
  475. * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
  476. * between commands that do block transfers and those that do byte
  477. * transfers. But this isn't easy... there are lots of vendor specific
  478. * commands, and the user can issue any command via the
  479. * SCSI_IOCTL_SEND_COMMAND.
  480. *
  481. * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
  482. * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
  483. * and 3), the thing to do is obvious: allow any number of blocks via DMA
  484. * or none. In case 2), we apply some heuristic: Byte mode is assumed if
  485. * the transfer (allocation) length is < 1024, hoping that no cmd. not
  486. * explicitly known as byte mode have such big allocation lengths...
  487. * BTW, all the discussion above applies only to reads. DMA writes are
  488. * unproblematic anyways, since the targets aborts the transfer after
  489. * receiving a sufficient number of bytes.
  490. *
  491. * Another point: If the transfer is from/to an non-ST-RAM address, we
  492. * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
  493. */
  494. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  495. /* Write operation can always use the DMA, but the transfer size must
  496. * be rounded up to the next multiple of 512 (atari_dma_setup() does
  497. * this).
  498. */
  499. possible_len = wanted_len;
  500. } else {
  501. /* Read operations: if the wanted transfer length is not a multiple of
  502. * 512, we cannot use DMA, since the ST-DMA cannot split transfers
  503. * (no interrupt on DMA finished!)
  504. */
  505. if (wanted_len & 0x1ff)
  506. possible_len = 0;
  507. else {
  508. /* Now classify the command (see above) and decide whether it is
  509. * allowed to do DMA at all */
  510. switch (falcon_classify_cmd(cmd)) {
  511. case CMD_SURELY_BLOCK_MODE:
  512. possible_len = wanted_len;
  513. break;
  514. case CMD_SURELY_BYTE_MODE:
  515. possible_len = 0; /* DMA prohibited */
  516. break;
  517. case CMD_MODE_UNKNOWN:
  518. default:
  519. /* For unknown commands assume block transfers if the transfer
  520. * size/allocation length is >= 1024 */
  521. possible_len = (wanted_len < 1024) ? 0 : wanted_len;
  522. break;
  523. }
  524. }
  525. }
  526. /* Last step: apply the hard limit on DMA transfers */
  527. limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
  528. STRAM_BUFFER_SIZE : 255*512;
  529. if (possible_len > limit)
  530. possible_len = limit;
  531. if (possible_len != wanted_len)
  532. dprintk(NDEBUG_DMA, "DMA transfer now %d bytes instead of %d\n",
  533. possible_len, wanted_len);
  534. return possible_len;
  535. }
  536. /* NCR5380 register access functions
  537. *
  538. * There are separate functions for TT and Falcon, because the access
  539. * methods are quite different. The calling macros NCR5380_read and
  540. * NCR5380_write call these functions via function pointers.
  541. */
  542. static u8 atari_scsi_tt_reg_read(unsigned int reg)
  543. {
  544. return tt_scsi_regp[reg * 2];
  545. }
  546. static void atari_scsi_tt_reg_write(unsigned int reg, u8 value)
  547. {
  548. tt_scsi_regp[reg * 2] = value;
  549. }
  550. static u8 atari_scsi_falcon_reg_read(unsigned int reg)
  551. {
  552. unsigned long flags;
  553. u8 result;
  554. reg += 0x88;
  555. local_irq_save(flags);
  556. dma_wd.dma_mode_status = (u_short)reg;
  557. result = (u8)dma_wd.fdc_acces_seccount;
  558. local_irq_restore(flags);
  559. return result;
  560. }
  561. static void atari_scsi_falcon_reg_write(unsigned int reg, u8 value)
  562. {
  563. unsigned long flags;
  564. reg += 0x88;
  565. local_irq_save(flags);
  566. dma_wd.dma_mode_status = (u_short)reg;
  567. dma_wd.fdc_acces_seccount = (u_short)value;
  568. local_irq_restore(flags);
  569. }
  570. #include "NCR5380.c"
  571. static int atari_scsi_host_reset(struct scsi_cmnd *cmd)
  572. {
  573. int rv;
  574. unsigned long flags;
  575. local_irq_save(flags);
  576. /* Abort a maybe active DMA transfer */
  577. if (IS_A_TT()) {
  578. tt_scsi_dma.dma_ctrl = 0;
  579. } else {
  580. if (stdma_is_locked_by(scsi_falcon_intr))
  581. st_dma.dma_mode_status = 0x90;
  582. atari_dma_active = 0;
  583. atari_dma_orig_addr = NULL;
  584. }
  585. rv = NCR5380_host_reset(cmd);
  586. /* The 5380 raises its IRQ line while _RST is active but the ST DMA
  587. * "lock" has been released so this interrupt may end up handled by
  588. * floppy or IDE driver (if one of them holds the lock). The NCR5380
  589. * interrupt flag has been cleared already.
  590. */
  591. local_irq_restore(flags);
  592. return rv;
  593. }
  594. #define DRV_MODULE_NAME "atari_scsi"
  595. #define PFX DRV_MODULE_NAME ": "
  596. static struct scsi_host_template atari_scsi_template = {
  597. .module = THIS_MODULE,
  598. .proc_name = DRV_MODULE_NAME,
  599. .name = "Atari native SCSI",
  600. .info = atari_scsi_info,
  601. .queuecommand = atari_scsi_queue_command,
  602. .eh_abort_handler = atari_scsi_abort,
  603. .eh_host_reset_handler = atari_scsi_host_reset,
  604. .this_id = 7,
  605. .cmd_per_lun = 2,
  606. .use_clustering = DISABLE_CLUSTERING,
  607. .cmd_size = NCR5380_CMD_SIZE,
  608. };
  609. static int __init atari_scsi_probe(struct platform_device *pdev)
  610. {
  611. struct Scsi_Host *instance;
  612. int error;
  613. struct resource *irq;
  614. int host_flags = 0;
  615. irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  616. if (!irq)
  617. return -ENODEV;
  618. if (ATARIHW_PRESENT(TT_SCSI)) {
  619. atari_scsi_reg_read = atari_scsi_tt_reg_read;
  620. atari_scsi_reg_write = atari_scsi_tt_reg_write;
  621. } else {
  622. atari_scsi_reg_read = atari_scsi_falcon_reg_read;
  623. atari_scsi_reg_write = atari_scsi_falcon_reg_write;
  624. }
  625. if (ATARIHW_PRESENT(TT_SCSI)) {
  626. atari_scsi_template.can_queue = 16;
  627. atari_scsi_template.sg_tablesize = SG_ALL;
  628. } else {
  629. atari_scsi_template.can_queue = 1;
  630. atari_scsi_template.sg_tablesize = 1;
  631. }
  632. if (setup_can_queue > 0)
  633. atari_scsi_template.can_queue = setup_can_queue;
  634. if (setup_cmd_per_lun > 0)
  635. atari_scsi_template.cmd_per_lun = setup_cmd_per_lun;
  636. /* Don't increase sg_tablesize on Falcon! */
  637. if (ATARIHW_PRESENT(TT_SCSI) && setup_sg_tablesize > 0)
  638. atari_scsi_template.sg_tablesize = setup_sg_tablesize;
  639. if (setup_hostid >= 0) {
  640. atari_scsi_template.this_id = setup_hostid & 7;
  641. } else {
  642. /* Test if a host id is set in the NVRam */
  643. if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
  644. unsigned char b = nvram_read_byte(16);
  645. /* Arbitration enabled? (for TOS)
  646. * If yes, use configured host ID
  647. */
  648. if (b & 0x80)
  649. atari_scsi_template.this_id = b & 7;
  650. }
  651. }
  652. /* If running on a Falcon and if there's TT-Ram (i.e., more than one
  653. * memory block, since there's always ST-Ram in a Falcon), then
  654. * allocate a STRAM_BUFFER_SIZE byte dribble buffer for transfers
  655. * from/to alternative Ram.
  656. */
  657. if (ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(EXTD_DMA) &&
  658. m68k_realnum_memory > 1) {
  659. atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
  660. if (!atari_dma_buffer) {
  661. pr_err(PFX "can't allocate ST-RAM double buffer\n");
  662. return -ENOMEM;
  663. }
  664. atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
  665. atari_dma_orig_addr = NULL;
  666. }
  667. instance = scsi_host_alloc(&atari_scsi_template,
  668. sizeof(struct NCR5380_hostdata));
  669. if (!instance) {
  670. error = -ENOMEM;
  671. goto fail_alloc;
  672. }
  673. instance->irq = irq->start;
  674. host_flags |= IS_A_TT() ? 0 : FLAG_LATE_DMA_SETUP;
  675. host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
  676. error = NCR5380_init(instance, host_flags);
  677. if (error)
  678. goto fail_init;
  679. if (IS_A_TT()) {
  680. error = request_irq(instance->irq, scsi_tt_intr, 0,
  681. "NCR5380", instance);
  682. if (error) {
  683. pr_err(PFX "request irq %d failed, aborting\n",
  684. instance->irq);
  685. goto fail_irq;
  686. }
  687. tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
  688. tt_scsi_dma.dma_ctrl = 0;
  689. atari_dma_residual = 0;
  690. /* While the read overruns (described by Drew Eckhardt in
  691. * NCR5380.c) never happened on TTs, they do in fact on the
  692. * Medusa (This was the cause why SCSI didn't work right for
  693. * so long there.) Since handling the overruns slows down
  694. * a bit, I turned the #ifdef's into a runtime condition.
  695. *
  696. * In principle it should be sufficient to do max. 1 byte with
  697. * PIO, but there is another problem on the Medusa with the DMA
  698. * rest data register. So read_overruns is currently set
  699. * to 4 to avoid having transfers that aren't a multiple of 4.
  700. * If the rest data bug is fixed, this can be lowered to 1.
  701. */
  702. if (MACH_IS_MEDUSA) {
  703. struct NCR5380_hostdata *hostdata =
  704. shost_priv(instance);
  705. hostdata->read_overruns = 4;
  706. }
  707. } else {
  708. /* Nothing to do for the interrupt: the ST-DMA is initialized
  709. * already.
  710. */
  711. atari_dma_residual = 0;
  712. atari_dma_active = 0;
  713. atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
  714. : 0xff000000);
  715. }
  716. NCR5380_maybe_reset_bus(instance);
  717. error = scsi_add_host(instance, NULL);
  718. if (error)
  719. goto fail_host;
  720. platform_set_drvdata(pdev, instance);
  721. scsi_scan_host(instance);
  722. return 0;
  723. fail_host:
  724. if (IS_A_TT())
  725. free_irq(instance->irq, instance);
  726. fail_irq:
  727. NCR5380_exit(instance);
  728. fail_init:
  729. scsi_host_put(instance);
  730. fail_alloc:
  731. if (atari_dma_buffer)
  732. atari_stram_free(atari_dma_buffer);
  733. return error;
  734. }
  735. static int __exit atari_scsi_remove(struct platform_device *pdev)
  736. {
  737. struct Scsi_Host *instance = platform_get_drvdata(pdev);
  738. scsi_remove_host(instance);
  739. if (IS_A_TT())
  740. free_irq(instance->irq, instance);
  741. NCR5380_exit(instance);
  742. scsi_host_put(instance);
  743. if (atari_dma_buffer)
  744. atari_stram_free(atari_dma_buffer);
  745. return 0;
  746. }
  747. static struct platform_driver atari_scsi_driver = {
  748. .remove = __exit_p(atari_scsi_remove),
  749. .driver = {
  750. .name = DRV_MODULE_NAME,
  751. },
  752. };
  753. module_platform_driver_probe(atari_scsi_driver, atari_scsi_probe);
  754. MODULE_ALIAS("platform:" DRV_MODULE_NAME);
  755. MODULE_LICENSE("GPL");