atari_scsi.c 35 KB

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