atari_scsi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. /*
  2. * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port
  3. *
  4. * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
  5. *
  6. * Loosely based on the work of Robert De Vries' team and added:
  7. * - working real DMA
  8. * - Falcon support (untested yet!) ++bjoern fixed and now it works
  9. * - lots of extensions and bug fixes.
  10. *
  11. * This file is subject to the terms and conditions of the GNU General Public
  12. * License. See the file COPYING in the main directory of this archive
  13. * for more details.
  14. *
  15. */
  16. /**************************************************************************/
  17. /* */
  18. /* Notes for Falcon SCSI: */
  19. /* ---------------------- */
  20. /* */
  21. /* Since the Falcon SCSI uses the ST-DMA chip, that is shared among */
  22. /* several device drivers, locking and unlocking the access to this */
  23. /* chip is required. But locking is not possible from an interrupt, */
  24. /* since it puts the process to sleep if the lock is not available. */
  25. /* This prevents "late" locking of the DMA chip, i.e. locking it just */
  26. /* before using it, since in case of disconnection-reconnection */
  27. /* commands, the DMA is started from the reselection interrupt. */
  28. /* */
  29. /* Two possible schemes for ST-DMA-locking would be: */
  30. /* 1) The lock is taken for each command separately and disconnecting */
  31. /* is forbidden (i.e. can_queue = 1). */
  32. /* 2) The DMA chip is locked when the first command comes in and */
  33. /* released when the last command is finished and all queues are */
  34. /* empty. */
  35. /* The first alternative would result in bad performance, since the */
  36. /* interleaving of commands would not be used. The second is unfair to */
  37. /* other drivers using the ST-DMA, because the queues will seldom be */
  38. /* totally empty if there is a lot of disk traffic. */
  39. /* */
  40. /* For this reasons I decided to employ a more elaborate scheme: */
  41. /* - First, we give up the lock every time we can (for fairness), this */
  42. /* means every time a command finishes and there are no other commands */
  43. /* on the disconnected queue. */
  44. /* - If there are others waiting to lock the DMA chip, we stop */
  45. /* issuing commands, i.e. moving them onto the issue queue. */
  46. /* Because of that, the disconnected queue will run empty in a */
  47. /* while. Instead we go to sleep on a 'fairness_queue'. */
  48. /* - If the lock is released, all processes waiting on the fairness */
  49. /* queue will be woken. The first of them tries to re-lock the DMA, */
  50. /* the others wait for the first to finish this task. After that, */
  51. /* they can all run on and do their commands... */
  52. /* This sounds complicated (and it is it :-(), but it seems to be a */
  53. /* good compromise between fairness and performance: As long as no one */
  54. /* else wants to work with the ST-DMA chip, SCSI can go along as */
  55. /* usual. If now someone else comes, this behaviour is changed to a */
  56. /* "fairness mode": just already initiated commands are finished and */
  57. /* then the lock is released. The other one waiting will probably win */
  58. /* the race for locking the DMA, since it was waiting for longer. And */
  59. /* after it has finished, SCSI can go ahead again. Finally: I hope I */
  60. /* have not produced any deadlock possibilities! */
  61. /* */
  62. /**************************************************************************/
  63. #include <linux/module.h>
  64. #define AUTOSENSE
  65. /* For the Atari version, use only polled IO or REAL_DMA */
  66. #define REAL_DMA
  67. /* Support tagged queuing? (on devices that are able to... :-) */
  68. #define SUPPORT_TAGS
  69. #define MAX_TAGS 32
  70. #include <linux/types.h>
  71. #include <linux/stddef.h>
  72. #include <linux/ctype.h>
  73. #include <linux/delay.h>
  74. #include <linux/mm.h>
  75. #include <linux/blkdev.h>
  76. #include <linux/interrupt.h>
  77. #include <linux/init.h>
  78. #include <linux/nvram.h>
  79. #include <linux/bitops.h>
  80. #include <linux/wait.h>
  81. #include <asm/setup.h>
  82. #include <asm/atarihw.h>
  83. #include <asm/atariints.h>
  84. #include <asm/page.h>
  85. #include <asm/pgtable.h>
  86. #include <asm/irq.h>
  87. #include <asm/traps.h>
  88. #include "scsi.h"
  89. #include <scsi/scsi_host.h>
  90. #include "atari_scsi.h"
  91. #include "NCR5380.h"
  92. #include <asm/atari_stdma.h>
  93. #include <asm/atari_stram.h>
  94. #include <asm/io.h>
  95. #include <linux/stat.h>
  96. #define IS_A_TT() ATARIHW_PRESENT(TT_SCSI)
  97. #define SCSI_DMA_WRITE_P(elt,val) \
  98. do { \
  99. unsigned long v = val; \
  100. tt_scsi_dma.elt##_lo = v & 0xff; \
  101. v >>= 8; \
  102. tt_scsi_dma.elt##_lmd = v & 0xff; \
  103. v >>= 8; \
  104. tt_scsi_dma.elt##_hmd = v & 0xff; \
  105. v >>= 8; \
  106. tt_scsi_dma.elt##_hi = v & 0xff; \
  107. } while(0)
  108. #define SCSI_DMA_READ_P(elt) \
  109. (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) | \
  110. (unsigned long)tt_scsi_dma.elt##_hmd) << 8) | \
  111. (unsigned long)tt_scsi_dma.elt##_lmd) << 8) | \
  112. (unsigned long)tt_scsi_dma.elt##_lo)
  113. static inline void SCSI_DMA_SETADR(unsigned long adr)
  114. {
  115. st_dma.dma_lo = (unsigned char)adr;
  116. MFPDELAY();
  117. adr >>= 8;
  118. st_dma.dma_md = (unsigned char)adr;
  119. MFPDELAY();
  120. adr >>= 8;
  121. st_dma.dma_hi = (unsigned char)adr;
  122. MFPDELAY();
  123. }
  124. static inline unsigned long SCSI_DMA_GETADR(void)
  125. {
  126. unsigned long adr;
  127. adr = st_dma.dma_lo;
  128. MFPDELAY();
  129. adr |= (st_dma.dma_md & 0xff) << 8;
  130. MFPDELAY();
  131. adr |= (st_dma.dma_hi & 0xff) << 16;
  132. MFPDELAY();
  133. return adr;
  134. }
  135. static inline void ENABLE_IRQ(void)
  136. {
  137. if (IS_A_TT())
  138. atari_enable_irq(IRQ_TT_MFP_SCSI);
  139. else
  140. atari_enable_irq(IRQ_MFP_FSCSI);
  141. }
  142. static inline void DISABLE_IRQ(void)
  143. {
  144. if (IS_A_TT())
  145. atari_disable_irq(IRQ_TT_MFP_SCSI);
  146. else
  147. atari_disable_irq(IRQ_MFP_FSCSI);
  148. }
  149. #define HOSTDATA_DMALEN (((struct NCR5380_hostdata *) \
  150. (atari_scsi_host->hostdata))->dma_len)
  151. /* Time (in jiffies) to wait after a reset; the SCSI standard calls for 250ms,
  152. * we usually do 0.5s to be on the safe side. But Toshiba CD-ROMs once more
  153. * need ten times the standard value... */
  154. #ifndef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
  155. #define AFTER_RESET_DELAY (HZ/2)
  156. #else
  157. #define AFTER_RESET_DELAY (5*HZ/2)
  158. #endif
  159. /***************************** Prototypes *****************************/
  160. #ifdef REAL_DMA
  161. static int scsi_dma_is_ignored_buserr(unsigned char dma_stat);
  162. static void atari_scsi_fetch_restbytes(void);
  163. static long atari_scsi_dma_residual(struct Scsi_Host *instance);
  164. static int falcon_classify_cmd(Scsi_Cmnd *cmd);
  165. static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
  166. Scsi_Cmnd *cmd, int write_flag);
  167. #endif
  168. static irqreturn_t scsi_tt_intr(int irq, void *dummy);
  169. static irqreturn_t scsi_falcon_intr(int irq, void *dummy);
  170. static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata);
  171. static void falcon_get_lock(void);
  172. #ifdef CONFIG_ATARI_SCSI_RESET_BOOT
  173. static void atari_scsi_reset_boot(void);
  174. #endif
  175. static unsigned char atari_scsi_tt_reg_read(unsigned char reg);
  176. static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value);
  177. static unsigned char atari_scsi_falcon_reg_read(unsigned char reg);
  178. static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value);
  179. /************************* End of Prototypes **************************/
  180. static struct Scsi_Host *atari_scsi_host;
  181. static unsigned char (*atari_scsi_reg_read)(unsigned char reg);
  182. static void (*atari_scsi_reg_write)(unsigned char reg, unsigned char value);
  183. #ifdef REAL_DMA
  184. static unsigned long atari_dma_residual, atari_dma_startaddr;
  185. static short atari_dma_active;
  186. /* pointer to the dribble buffer */
  187. static char *atari_dma_buffer;
  188. /* precalculated physical address of the dribble buffer */
  189. static unsigned long atari_dma_phys_buffer;
  190. /* != 0 tells the Falcon int handler to copy data from the dribble buffer */
  191. static char *atari_dma_orig_addr;
  192. /* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
  193. * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
  194. * cases where requests to physical contiguous buffers have been merged, this
  195. * request is <= 4k (one page). So I don't think we have to split transfers
  196. * just due to this buffer size...
  197. */
  198. #define STRAM_BUFFER_SIZE (4096)
  199. /* mask for address bits that can't be used with the ST-DMA */
  200. static unsigned long atari_dma_stram_mask;
  201. #define STRAM_ADDR(a) (((a) & atari_dma_stram_mask) == 0)
  202. /* number of bytes to cut from a transfer to handle NCR overruns */
  203. static int atari_read_overruns;
  204. #endif
  205. static int setup_can_queue = -1;
  206. module_param(setup_can_queue, int, 0);
  207. static int setup_cmd_per_lun = -1;
  208. module_param(setup_cmd_per_lun, int, 0);
  209. static int setup_sg_tablesize = -1;
  210. module_param(setup_sg_tablesize, int, 0);
  211. #ifdef SUPPORT_TAGS
  212. static int setup_use_tagged_queuing = -1;
  213. module_param(setup_use_tagged_queuing, int, 0);
  214. #endif
  215. static int setup_hostid = -1;
  216. module_param(setup_hostid, int, 0);
  217. #if defined(REAL_DMA)
  218. static int scsi_dma_is_ignored_buserr(unsigned char dma_stat)
  219. {
  220. int i;
  221. unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr;
  222. if (dma_stat & 0x01) {
  223. /* A bus error happens when DMA-ing from the last page of a
  224. * physical memory chunk (DMA prefetch!), but that doesn't hurt.
  225. * Check for this case:
  226. */
  227. for (i = 0; i < m68k_num_memory; ++i) {
  228. end_addr = m68k_memory[i].addr + m68k_memory[i].size;
  229. if (end_addr <= addr && addr <= end_addr + 4)
  230. return 1;
  231. }
  232. }
  233. return 0;
  234. }
  235. #if 0
  236. /* Dead code... wasn't called anyway :-) and causes some trouble, because at
  237. * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has
  238. * to clear the DMA int pending bit before it allows other level 6 interrupts.
  239. */
  240. static void scsi_dma_buserr(int irq, void *dummy)
  241. {
  242. unsigned char dma_stat = tt_scsi_dma.dma_ctrl;
  243. /* Don't do anything if a NCR interrupt is pending. Probably it's just
  244. * masked... */
  245. if (atari_irq_pending(IRQ_TT_MFP_SCSI))
  246. return;
  247. printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n",
  248. SCSI_DMA_READ_P(dma_addr), dma_stat, SCSI_DMA_READ_P(dma_cnt));
  249. if (dma_stat & 0x80) {
  250. if (!scsi_dma_is_ignored_buserr(dma_stat))
  251. printk("SCSI DMA bus error -- bad DMA programming!\n");
  252. } else {
  253. /* Under normal circumstances we never should get to this point,
  254. * since both interrupts are triggered simultaneously and the 5380
  255. * int has higher priority. When this irq is handled, that DMA
  256. * interrupt is cleared. So a warning message is printed here.
  257. */
  258. printk("SCSI DMA intr ?? -- this shouldn't happen!\n");
  259. }
  260. }
  261. #endif
  262. #endif
  263. static irqreturn_t scsi_tt_intr(int irq, void *dummy)
  264. {
  265. #ifdef REAL_DMA
  266. int dma_stat;
  267. dma_stat = tt_scsi_dma.dma_ctrl;
  268. dprintk(NDEBUG_INTR, "scsi%d: NCR5380 interrupt, DMA status = %02x\n",
  269. atari_scsi_host->host_no, dma_stat & 0xff);
  270. /* Look if it was the DMA that has interrupted: First possibility
  271. * is that a bus error occurred...
  272. */
  273. if (dma_stat & 0x80) {
  274. if (!scsi_dma_is_ignored_buserr(dma_stat)) {
  275. printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
  276. SCSI_DMA_READ_P(dma_addr));
  277. printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
  278. }
  279. }
  280. /* If the DMA is active but not finished, we have the case
  281. * that some other 5380 interrupt occurred within the DMA transfer.
  282. * This means we have residual bytes, if the desired end address
  283. * is not yet reached. Maybe we have to fetch some bytes from the
  284. * rest data register, too. The residual must be calculated from
  285. * the address pointer, not the counter register, because only the
  286. * addr reg counts bytes not yet written and pending in the rest
  287. * data reg!
  288. */
  289. if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
  290. atari_dma_residual = HOSTDATA_DMALEN - (SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr);
  291. dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
  292. atari_dma_residual);
  293. if ((signed int)atari_dma_residual < 0)
  294. atari_dma_residual = 0;
  295. if ((dma_stat & 1) == 0) {
  296. /*
  297. * After read operations, we maybe have to
  298. * transport some rest bytes
  299. */
  300. atari_scsi_fetch_restbytes();
  301. } else {
  302. /*
  303. * There seems to be a nasty bug in some SCSI-DMA/NCR
  304. * combinations: If a target disconnects while a write
  305. * operation is going on, the address register of the
  306. * DMA may be a few bytes farer than it actually read.
  307. * This is probably due to DMA prefetching and a delay
  308. * between DMA and NCR. Experiments showed that the
  309. * dma_addr is 9 bytes to high, but this could vary.
  310. * The problem is, that the residual is thus calculated
  311. * wrong and the next transfer will start behind where
  312. * it should. So we round up the residual to the next
  313. * multiple of a sector size, if it isn't already a
  314. * multiple and the originally expected transfer size
  315. * was. The latter condition is there to ensure that
  316. * the correction is taken only for "real" data
  317. * transfers and not for, e.g., the parameters of some
  318. * other command. These shouldn't disconnect anyway.
  319. */
  320. if (atari_dma_residual & 0x1ff) {
  321. dprintk(NDEBUG_DMA, "SCSI DMA: DMA bug corrected, "
  322. "difference %ld bytes\n",
  323. 512 - (atari_dma_residual & 0x1ff));
  324. atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
  325. }
  326. }
  327. tt_scsi_dma.dma_ctrl = 0;
  328. }
  329. /* If the DMA is finished, fetch the rest bytes and turn it off */
  330. if (dma_stat & 0x40) {
  331. atari_dma_residual = 0;
  332. if ((dma_stat & 1) == 0)
  333. atari_scsi_fetch_restbytes();
  334. tt_scsi_dma.dma_ctrl = 0;
  335. }
  336. #endif /* REAL_DMA */
  337. NCR5380_intr(irq, dummy);
  338. #if 0
  339. /* To be sure the int is not masked */
  340. atari_enable_irq(IRQ_TT_MFP_SCSI);
  341. #endif
  342. return IRQ_HANDLED;
  343. }
  344. static irqreturn_t scsi_falcon_intr(int irq, void *dummy)
  345. {
  346. #ifdef REAL_DMA
  347. int dma_stat;
  348. /* Turn off DMA and select sector counter register before
  349. * accessing the status register (Atari recommendation!)
  350. */
  351. st_dma.dma_mode_status = 0x90;
  352. dma_stat = st_dma.dma_mode_status;
  353. /* Bit 0 indicates some error in the DMA process... don't know
  354. * what happened exactly (no further docu).
  355. */
  356. if (!(dma_stat & 0x01)) {
  357. /* DMA error */
  358. printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
  359. }
  360. /* If the DMA was active, but now bit 1 is not clear, it is some
  361. * other 5380 interrupt that finishes the DMA transfer. We have to
  362. * calculate the number of residual bytes and give a warning if
  363. * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
  364. */
  365. if (atari_dma_active && (dma_stat & 0x02)) {
  366. unsigned long transferred;
  367. transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
  368. /* The ST-DMA address is incremented in 2-byte steps, but the
  369. * data are written only in 16-byte chunks. If the number of
  370. * transferred bytes is not divisible by 16, the remainder is
  371. * lost somewhere in outer space.
  372. */
  373. if (transferred & 15)
  374. printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
  375. "ST-DMA fifo\n", transferred & 15);
  376. atari_dma_residual = HOSTDATA_DMALEN - transferred;
  377. dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
  378. atari_dma_residual);
  379. } else
  380. atari_dma_residual = 0;
  381. atari_dma_active = 0;
  382. if (atari_dma_orig_addr) {
  383. /* If the dribble buffer was used on a read operation, copy the DMA-ed
  384. * data to the original destination address.
  385. */
  386. memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
  387. HOSTDATA_DMALEN - atari_dma_residual);
  388. atari_dma_orig_addr = NULL;
  389. }
  390. #endif /* REAL_DMA */
  391. NCR5380_intr(irq, dummy);
  392. return IRQ_HANDLED;
  393. }
  394. #ifdef REAL_DMA
  395. static void atari_scsi_fetch_restbytes(void)
  396. {
  397. int nr;
  398. char *src, *dst;
  399. unsigned long phys_dst;
  400. /* fetch rest bytes in the DMA register */
  401. phys_dst = SCSI_DMA_READ_P(dma_addr);
  402. nr = phys_dst & 3;
  403. if (nr) {
  404. /* there are 'nr' bytes left for the last long address
  405. before the DMA pointer */
  406. phys_dst ^= nr;
  407. dprintk(NDEBUG_DMA, "SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
  408. nr, phys_dst);
  409. /* The content of the DMA pointer is a physical address! */
  410. dst = phys_to_virt(phys_dst);
  411. dprintk(NDEBUG_DMA, " = virt addr %p\n", dst);
  412. for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
  413. *dst++ = *src++;
  414. }
  415. }
  416. #endif /* REAL_DMA */
  417. static int falcon_got_lock = 0;
  418. static DECLARE_WAIT_QUEUE_HEAD(falcon_fairness_wait);
  419. static int falcon_trying_lock = 0;
  420. static DECLARE_WAIT_QUEUE_HEAD(falcon_try_wait);
  421. static int falcon_dont_release = 0;
  422. /* This function releases the lock on the DMA chip if there is no
  423. * connected command and the disconnected queue is empty. On
  424. * releasing, instances of falcon_get_lock are awoken, that put
  425. * themselves to sleep for fairness. They can now try to get the lock
  426. * again (but others waiting longer more probably will win).
  427. */
  428. static void falcon_release_lock_if_possible(struct NCR5380_hostdata *hostdata)
  429. {
  430. unsigned long flags;
  431. if (IS_A_TT())
  432. return;
  433. local_irq_save(flags);
  434. if (falcon_got_lock && !hostdata->disconnected_queue &&
  435. !hostdata->issue_queue && !hostdata->connected) {
  436. if (falcon_dont_release) {
  437. #if 0
  438. printk("WARNING: Lock release not allowed. Ignored\n");
  439. #endif
  440. local_irq_restore(flags);
  441. return;
  442. }
  443. falcon_got_lock = 0;
  444. stdma_release();
  445. wake_up(&falcon_fairness_wait);
  446. }
  447. local_irq_restore(flags);
  448. }
  449. /* This function manages the locking of the ST-DMA.
  450. * If the DMA isn't locked already for SCSI, it tries to lock it by
  451. * calling stdma_lock(). But if the DMA is locked by the SCSI code and
  452. * there are other drivers waiting for the chip, we do not issue the
  453. * command immediately but wait on 'falcon_fairness_queue'. We will be
  454. * waked up when the DMA is unlocked by some SCSI interrupt. After that
  455. * we try to get the lock again.
  456. * But we must be prepared that more than one instance of
  457. * falcon_get_lock() is waiting on the fairness queue. They should not
  458. * try all at once to call stdma_lock(), one is enough! For that, the
  459. * first one sets 'falcon_trying_lock', others that see that variable
  460. * set wait on the queue 'falcon_try_wait'.
  461. * Complicated, complicated.... Sigh...
  462. */
  463. static void falcon_get_lock(void)
  464. {
  465. unsigned long flags;
  466. if (IS_A_TT())
  467. return;
  468. local_irq_save(flags);
  469. wait_event_cmd(falcon_fairness_wait,
  470. in_interrupt() || !falcon_got_lock || !stdma_others_waiting(),
  471. local_irq_restore(flags),
  472. local_irq_save(flags));
  473. while (!falcon_got_lock) {
  474. if (in_irq())
  475. panic("Falcon SCSI hasn't ST-DMA lock in interrupt");
  476. if (!falcon_trying_lock) {
  477. falcon_trying_lock = 1;
  478. stdma_lock(scsi_falcon_intr, NULL);
  479. falcon_got_lock = 1;
  480. falcon_trying_lock = 0;
  481. wake_up(&falcon_try_wait);
  482. } else {
  483. wait_event_cmd(falcon_try_wait,
  484. falcon_got_lock && !falcon_trying_lock,
  485. local_irq_restore(flags),
  486. local_irq_save(flags));
  487. }
  488. }
  489. local_irq_restore(flags);
  490. if (!falcon_got_lock)
  491. panic("Falcon SCSI: someone stole the lock :-(\n");
  492. }
  493. static int __init atari_scsi_detect(struct scsi_host_template *host)
  494. {
  495. static int called = 0;
  496. struct Scsi_Host *instance;
  497. if (!MACH_IS_ATARI ||
  498. (!ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(TT_SCSI)) ||
  499. called)
  500. return 0;
  501. host->proc_name = "Atari";
  502. atari_scsi_reg_read = IS_A_TT() ? atari_scsi_tt_reg_read :
  503. atari_scsi_falcon_reg_read;
  504. atari_scsi_reg_write = IS_A_TT() ? atari_scsi_tt_reg_write :
  505. atari_scsi_falcon_reg_write;
  506. /* setup variables */
  507. host->can_queue =
  508. (setup_can_queue > 0) ? setup_can_queue :
  509. IS_A_TT() ? ATARI_TT_CAN_QUEUE : ATARI_FALCON_CAN_QUEUE;
  510. host->cmd_per_lun =
  511. (setup_cmd_per_lun > 0) ? setup_cmd_per_lun :
  512. IS_A_TT() ? ATARI_TT_CMD_PER_LUN : ATARI_FALCON_CMD_PER_LUN;
  513. /* Force sg_tablesize to 0 on a Falcon! */
  514. host->sg_tablesize =
  515. !IS_A_TT() ? ATARI_FALCON_SG_TABLESIZE :
  516. (setup_sg_tablesize >= 0) ? setup_sg_tablesize : ATARI_TT_SG_TABLESIZE;
  517. if (setup_hostid >= 0)
  518. host->this_id = setup_hostid;
  519. else {
  520. /* use 7 as default */
  521. host->this_id = 7;
  522. /* Test if a host id is set in the NVRam */
  523. if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
  524. unsigned char b = nvram_read_byte( 14 );
  525. /* Arbitration enabled? (for TOS) If yes, use configured host ID */
  526. if (b & 0x80)
  527. host->this_id = b & 7;
  528. }
  529. }
  530. #ifdef SUPPORT_TAGS
  531. if (setup_use_tagged_queuing < 0)
  532. setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING;
  533. #endif
  534. #ifdef REAL_DMA
  535. /* If running on a Falcon and if there's TT-Ram (i.e., more than one
  536. * memory block, since there's always ST-Ram in a Falcon), then allocate a
  537. * STRAM_BUFFER_SIZE byte dribble buffer for transfers from/to alternative
  538. * Ram.
  539. */
  540. if (MACH_IS_ATARI && ATARIHW_PRESENT(ST_SCSI) &&
  541. !ATARIHW_PRESENT(EXTD_DMA) && m68k_num_memory > 1) {
  542. atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
  543. if (!atari_dma_buffer) {
  544. printk(KERN_ERR "atari_scsi_detect: can't allocate ST-RAM "
  545. "double buffer\n");
  546. return 0;
  547. }
  548. atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
  549. atari_dma_orig_addr = 0;
  550. }
  551. #endif
  552. instance = scsi_register(host, sizeof(struct NCR5380_hostdata));
  553. if (instance == NULL) {
  554. atari_stram_free(atari_dma_buffer);
  555. atari_dma_buffer = 0;
  556. return 0;
  557. }
  558. atari_scsi_host = instance;
  559. /*
  560. * Set irq to 0, to avoid that the mid-level code disables our interrupt
  561. * during queue_command calls. This is completely unnecessary, and even
  562. * worse causes bad problems on the Falcon, where the int is shared with
  563. * IDE and floppy!
  564. */
  565. instance->irq = 0;
  566. #ifdef CONFIG_ATARI_SCSI_RESET_BOOT
  567. atari_scsi_reset_boot();
  568. #endif
  569. NCR5380_init(instance, 0);
  570. if (IS_A_TT()) {
  571. /* This int is actually "pseudo-slow", i.e. it acts like a slow
  572. * interrupt after having cleared the pending flag for the DMA
  573. * interrupt. */
  574. if (request_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr, IRQ_TYPE_SLOW,
  575. "SCSI NCR5380", instance)) {
  576. printk(KERN_ERR "atari_scsi_detect: cannot allocate irq %d, aborting",IRQ_TT_MFP_SCSI);
  577. scsi_unregister(atari_scsi_host);
  578. atari_stram_free(atari_dma_buffer);
  579. atari_dma_buffer = 0;
  580. return 0;
  581. }
  582. tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
  583. #ifdef REAL_DMA
  584. tt_scsi_dma.dma_ctrl = 0;
  585. atari_dma_residual = 0;
  586. if (MACH_IS_MEDUSA) {
  587. /* While the read overruns (described by Drew Eckhardt in
  588. * NCR5380.c) never happened on TTs, they do in fact on the Medusa
  589. * (This was the cause why SCSI didn't work right for so long
  590. * there.) Since handling the overruns slows down a bit, I turned
  591. * the #ifdef's into a runtime condition.
  592. *
  593. * In principle it should be sufficient to do max. 1 byte with
  594. * PIO, but there is another problem on the Medusa with the DMA
  595. * rest data register. So 'atari_read_overruns' is currently set
  596. * to 4 to avoid having transfers that aren't a multiple of 4. If
  597. * the rest data bug is fixed, this can be lowered to 1.
  598. */
  599. atari_read_overruns = 4;
  600. }
  601. #endif /*REAL_DMA*/
  602. } else { /* ! IS_A_TT */
  603. /* Nothing to do for the interrupt: the ST-DMA is initialized
  604. * already by atari_init_INTS()
  605. */
  606. #ifdef REAL_DMA
  607. atari_dma_residual = 0;
  608. atari_dma_active = 0;
  609. atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
  610. : 0xff000000);
  611. #endif
  612. }
  613. printk(KERN_INFO "scsi%d: options CAN_QUEUE=%d CMD_PER_LUN=%d SCAT-GAT=%d "
  614. #ifdef SUPPORT_TAGS
  615. "TAGGED-QUEUING=%s "
  616. #endif
  617. "HOSTID=%d",
  618. instance->host_no, instance->hostt->can_queue,
  619. instance->hostt->cmd_per_lun,
  620. instance->hostt->sg_tablesize,
  621. #ifdef SUPPORT_TAGS
  622. setup_use_tagged_queuing ? "yes" : "no",
  623. #endif
  624. instance->hostt->this_id );
  625. NCR5380_print_options(instance);
  626. printk("\n");
  627. called = 1;
  628. return 1;
  629. }
  630. static int atari_scsi_release(struct Scsi_Host *sh)
  631. {
  632. if (IS_A_TT())
  633. free_irq(IRQ_TT_MFP_SCSI, sh);
  634. if (atari_dma_buffer)
  635. atari_stram_free(atari_dma_buffer);
  636. NCR5380_exit(sh);
  637. return 1;
  638. }
  639. #ifndef MODULE
  640. static int __init atari_scsi_setup(char *str)
  641. {
  642. /* Format of atascsi parameter is:
  643. * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
  644. * Defaults depend on TT or Falcon, hostid determined at run time.
  645. * Negative values mean don't change.
  646. */
  647. int ints[6];
  648. get_options(str, ARRAY_SIZE(ints), ints);
  649. if (ints[0] < 1) {
  650. printk("atari_scsi_setup: no arguments!\n");
  651. return 0;
  652. }
  653. if (ints[0] >= 1) {
  654. if (ints[1] > 0)
  655. /* no limits on this, just > 0 */
  656. setup_can_queue = ints[1];
  657. }
  658. if (ints[0] >= 2) {
  659. if (ints[2] > 0)
  660. setup_cmd_per_lun = ints[2];
  661. }
  662. if (ints[0] >= 3) {
  663. if (ints[3] >= 0) {
  664. setup_sg_tablesize = ints[3];
  665. /* Must be <= SG_ALL (255) */
  666. if (setup_sg_tablesize > SG_ALL)
  667. setup_sg_tablesize = SG_ALL;
  668. }
  669. }
  670. if (ints[0] >= 4) {
  671. /* Must be between 0 and 7 */
  672. if (ints[4] >= 0 && ints[4] <= 7)
  673. setup_hostid = ints[4];
  674. else if (ints[4] > 7)
  675. printk("atari_scsi_setup: invalid host ID %d !\n", ints[4]);
  676. }
  677. #ifdef SUPPORT_TAGS
  678. if (ints[0] >= 5) {
  679. if (ints[5] >= 0)
  680. setup_use_tagged_queuing = !!ints[5];
  681. }
  682. #endif
  683. return 1;
  684. }
  685. __setup("atascsi=", atari_scsi_setup);
  686. #endif /* !MODULE */
  687. static int atari_scsi_bus_reset(Scsi_Cmnd *cmd)
  688. {
  689. int rv;
  690. struct NCR5380_hostdata *hostdata =
  691. (struct NCR5380_hostdata *)cmd->device->host->hostdata;
  692. /* For doing the reset, SCSI interrupts must be disabled first,
  693. * since the 5380 raises its IRQ line while _RST is active and we
  694. * can't disable interrupts completely, since we need the timer.
  695. */
  696. /* And abort a maybe active DMA transfer */
  697. if (IS_A_TT()) {
  698. atari_turnoff_irq(IRQ_TT_MFP_SCSI);
  699. #ifdef REAL_DMA
  700. tt_scsi_dma.dma_ctrl = 0;
  701. #endif /* REAL_DMA */
  702. } else {
  703. atari_turnoff_irq(IRQ_MFP_FSCSI);
  704. #ifdef REAL_DMA
  705. st_dma.dma_mode_status = 0x90;
  706. atari_dma_active = 0;
  707. atari_dma_orig_addr = NULL;
  708. #endif /* REAL_DMA */
  709. }
  710. rv = NCR5380_bus_reset(cmd);
  711. /* Re-enable ints */
  712. if (IS_A_TT()) {
  713. atari_turnon_irq(IRQ_TT_MFP_SCSI);
  714. } else {
  715. atari_turnon_irq(IRQ_MFP_FSCSI);
  716. }
  717. if (rv == SUCCESS)
  718. falcon_release_lock_if_possible(hostdata);
  719. return rv;
  720. }
  721. #ifdef CONFIG_ATARI_SCSI_RESET_BOOT
  722. static void __init atari_scsi_reset_boot(void)
  723. {
  724. unsigned long end;
  725. /*
  726. * Do a SCSI reset to clean up the bus during initialization. No messing
  727. * with the queues, interrupts, or locks necessary here.
  728. */
  729. printk("Atari SCSI: resetting the SCSI bus...");
  730. /* get in phase */
  731. NCR5380_write(TARGET_COMMAND_REG,
  732. PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
  733. /* assert RST */
  734. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
  735. /* The min. reset hold time is 25us, so 40us should be enough */
  736. udelay(50);
  737. /* reset RST and interrupt */
  738. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  739. NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  740. end = jiffies + AFTER_RESET_DELAY;
  741. while (time_before(jiffies, end))
  742. barrier();
  743. printk(" done\n");
  744. }
  745. #endif
  746. static const char *atari_scsi_info(struct Scsi_Host *host)
  747. {
  748. /* atari_scsi_detect() is verbose enough... */
  749. static const char string[] = "Atari native SCSI";
  750. return string;
  751. }
  752. #if defined(REAL_DMA)
  753. static unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance,
  754. void *data, unsigned long count,
  755. int dir)
  756. {
  757. unsigned long addr = virt_to_phys(data);
  758. dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
  759. "dir = %d\n", instance->host_no, data, addr, count, dir);
  760. if (!IS_A_TT() && !STRAM_ADDR(addr)) {
  761. /* If we have a non-DMAable address on a Falcon, use the dribble
  762. * buffer; 'orig_addr' != 0 in the read case tells the interrupt
  763. * handler to copy data from the dribble buffer to the originally
  764. * wanted address.
  765. */
  766. if (dir)
  767. memcpy(atari_dma_buffer, data, count);
  768. else
  769. atari_dma_orig_addr = data;
  770. addr = atari_dma_phys_buffer;
  771. }
  772. atari_dma_startaddr = addr; /* Needed for calculating residual later. */
  773. /* Cache cleanup stuff: On writes, push any dirty cache out before sending
  774. * it to the peripheral. (Must be done before DMA setup, since at least
  775. * the ST-DMA begins to fill internal buffers right after setup. For
  776. * reads, invalidate any cache, may be altered after DMA without CPU
  777. * knowledge.
  778. *
  779. * ++roman: For the Medusa, there's no need at all for that cache stuff,
  780. * because the hardware does bus snooping (fine!).
  781. */
  782. dma_cache_maintenance(addr, count, dir);
  783. if (count == 0)
  784. printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n");
  785. if (IS_A_TT()) {
  786. tt_scsi_dma.dma_ctrl = dir;
  787. SCSI_DMA_WRITE_P(dma_addr, addr);
  788. SCSI_DMA_WRITE_P(dma_cnt, count);
  789. tt_scsi_dma.dma_ctrl = dir | 2;
  790. } else { /* ! IS_A_TT */
  791. /* set address */
  792. SCSI_DMA_SETADR(addr);
  793. /* toggle direction bit to clear FIFO and set DMA direction */
  794. dir <<= 8;
  795. st_dma.dma_mode_status = 0x90 | dir;
  796. st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
  797. st_dma.dma_mode_status = 0x90 | dir;
  798. udelay(40);
  799. /* On writes, round up the transfer length to the next multiple of 512
  800. * (see also comment at atari_dma_xfer_len()). */
  801. st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
  802. udelay(40);
  803. st_dma.dma_mode_status = 0x10 | dir;
  804. udelay(40);
  805. /* need not restore value of dir, only boolean value is tested */
  806. atari_dma_active = 1;
  807. }
  808. return count;
  809. }
  810. static long atari_scsi_dma_residual(struct Scsi_Host *instance)
  811. {
  812. return atari_dma_residual;
  813. }
  814. #define CMD_SURELY_BLOCK_MODE 0
  815. #define CMD_SURELY_BYTE_MODE 1
  816. #define CMD_MODE_UNKNOWN 2
  817. static int falcon_classify_cmd(Scsi_Cmnd *cmd)
  818. {
  819. unsigned char opcode = cmd->cmnd[0];
  820. if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
  821. opcode == READ_BUFFER)
  822. return CMD_SURELY_BYTE_MODE;
  823. else if (opcode == READ_6 || opcode == READ_10 ||
  824. opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
  825. opcode == RECOVER_BUFFERED_DATA) {
  826. /* In case of a sequential-access target (tape), special care is
  827. * needed here: The transfer is block-mode only if the 'fixed' bit is
  828. * set! */
  829. if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
  830. return CMD_SURELY_BYTE_MODE;
  831. else
  832. return CMD_SURELY_BLOCK_MODE;
  833. } else
  834. return CMD_MODE_UNKNOWN;
  835. }
  836. /* This function calculates the number of bytes that can be transferred via
  837. * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
  838. * ST-DMA chip. There are only multiples of 512 bytes possible and max.
  839. * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
  840. * possible on the Falcon, since that would require to program the DMA for
  841. * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
  842. * the overrun problem, so this question is academic :-)
  843. */
  844. static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
  845. Scsi_Cmnd *cmd, int write_flag)
  846. {
  847. unsigned long possible_len, limit;
  848. if (IS_A_TT())
  849. /* TT SCSI DMA can transfer arbitrary #bytes */
  850. return wanted_len;
  851. /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
  852. * 255*512 bytes, but this should be enough)
  853. *
  854. * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
  855. * that return a number of bytes which cannot be known beforehand. In this
  856. * case, the given transfer length is an "allocation length". Now it
  857. * can happen that this allocation length is a multiple of 512 bytes and
  858. * the DMA is used. But if not n*512 bytes really arrive, some input data
  859. * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
  860. * between commands that do block transfers and those that do byte
  861. * transfers. But this isn't easy... there are lots of vendor specific
  862. * commands, and the user can issue any command via the
  863. * SCSI_IOCTL_SEND_COMMAND.
  864. *
  865. * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
  866. * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
  867. * and 3), the thing to do is obvious: allow any number of blocks via DMA
  868. * or none. In case 2), we apply some heuristic: Byte mode is assumed if
  869. * the transfer (allocation) length is < 1024, hoping that no cmd. not
  870. * explicitly known as byte mode have such big allocation lengths...
  871. * BTW, all the discussion above applies only to reads. DMA writes are
  872. * unproblematic anyways, since the targets aborts the transfer after
  873. * receiving a sufficient number of bytes.
  874. *
  875. * Another point: If the transfer is from/to an non-ST-RAM address, we
  876. * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
  877. */
  878. if (write_flag) {
  879. /* Write operation can always use the DMA, but the transfer size must
  880. * be rounded up to the next multiple of 512 (atari_dma_setup() does
  881. * this).
  882. */
  883. possible_len = wanted_len;
  884. } else {
  885. /* Read operations: if the wanted transfer length is not a multiple of
  886. * 512, we cannot use DMA, since the ST-DMA cannot split transfers
  887. * (no interrupt on DMA finished!)
  888. */
  889. if (wanted_len & 0x1ff)
  890. possible_len = 0;
  891. else {
  892. /* Now classify the command (see above) and decide whether it is
  893. * allowed to do DMA at all */
  894. switch (falcon_classify_cmd(cmd)) {
  895. case CMD_SURELY_BLOCK_MODE:
  896. possible_len = wanted_len;
  897. break;
  898. case CMD_SURELY_BYTE_MODE:
  899. possible_len = 0; /* DMA prohibited */
  900. break;
  901. case CMD_MODE_UNKNOWN:
  902. default:
  903. /* For unknown commands assume block transfers if the transfer
  904. * size/allocation length is >= 1024 */
  905. possible_len = (wanted_len < 1024) ? 0 : wanted_len;
  906. break;
  907. }
  908. }
  909. }
  910. /* Last step: apply the hard limit on DMA transfers */
  911. limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
  912. STRAM_BUFFER_SIZE : 255*512;
  913. if (possible_len > limit)
  914. possible_len = limit;
  915. if (possible_len != wanted_len)
  916. dprintk(NDEBUG_DMA, "Sorry, must cut DMA transfer size to %ld bytes "
  917. "instead of %ld\n", possible_len, wanted_len);
  918. return possible_len;
  919. }
  920. #endif /* REAL_DMA */
  921. /* NCR5380 register access functions
  922. *
  923. * There are separate functions for TT and Falcon, because the access
  924. * methods are quite different. The calling macros NCR5380_read and
  925. * NCR5380_write call these functions via function pointers.
  926. */
  927. static unsigned char atari_scsi_tt_reg_read(unsigned char reg)
  928. {
  929. return tt_scsi_regp[reg * 2];
  930. }
  931. static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value)
  932. {
  933. tt_scsi_regp[reg * 2] = value;
  934. }
  935. static unsigned char atari_scsi_falcon_reg_read(unsigned char reg)
  936. {
  937. dma_wd.dma_mode_status= (u_short)(0x88 + reg);
  938. return (u_char)dma_wd.fdc_acces_seccount;
  939. }
  940. static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value)
  941. {
  942. dma_wd.dma_mode_status = (u_short)(0x88 + reg);
  943. dma_wd.fdc_acces_seccount = (u_short)value;
  944. }
  945. #include "atari_NCR5380.c"
  946. static struct scsi_host_template driver_template = {
  947. .show_info = atari_scsi_show_info,
  948. .name = "Atari native SCSI",
  949. .detect = atari_scsi_detect,
  950. .release = atari_scsi_release,
  951. .info = atari_scsi_info,
  952. .queuecommand = atari_scsi_queue_command,
  953. .eh_abort_handler = atari_scsi_abort,
  954. .eh_bus_reset_handler = atari_scsi_bus_reset,
  955. .can_queue = 0, /* initialized at run-time */
  956. .this_id = 0, /* initialized at run-time */
  957. .sg_tablesize = 0, /* initialized at run-time */
  958. .cmd_per_lun = 0, /* initialized at run-time */
  959. .use_clustering = DISABLE_CLUSTERING
  960. };
  961. #include "scsi_module.c"
  962. MODULE_LICENSE("GPL");