atari_scsi.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127
  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. /* This is the wrapper function for NCR5380_queue_command(). It just
  492. * tries to get the lock on the ST-DMA (see above) and then calls the
  493. * original function.
  494. */
  495. #if 0
  496. int atari_queue_command(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
  497. {
  498. /* falcon_get_lock();
  499. * ++guenther: moved to NCR5380_queue_command() to prevent
  500. * race condition, see there for an explanation.
  501. */
  502. return NCR5380_queue_command(cmd, done);
  503. }
  504. #endif
  505. int __init atari_scsi_detect(struct scsi_host_template *host)
  506. {
  507. static int called = 0;
  508. struct Scsi_Host *instance;
  509. if (!MACH_IS_ATARI ||
  510. (!ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(TT_SCSI)) ||
  511. called)
  512. return 0;
  513. host->proc_name = "Atari";
  514. atari_scsi_reg_read = IS_A_TT() ? atari_scsi_tt_reg_read :
  515. atari_scsi_falcon_reg_read;
  516. atari_scsi_reg_write = IS_A_TT() ? atari_scsi_tt_reg_write :
  517. atari_scsi_falcon_reg_write;
  518. /* setup variables */
  519. host->can_queue =
  520. (setup_can_queue > 0) ? setup_can_queue :
  521. IS_A_TT() ? ATARI_TT_CAN_QUEUE : ATARI_FALCON_CAN_QUEUE;
  522. host->cmd_per_lun =
  523. (setup_cmd_per_lun > 0) ? setup_cmd_per_lun :
  524. IS_A_TT() ? ATARI_TT_CMD_PER_LUN : ATARI_FALCON_CMD_PER_LUN;
  525. /* Force sg_tablesize to 0 on a Falcon! */
  526. host->sg_tablesize =
  527. !IS_A_TT() ? ATARI_FALCON_SG_TABLESIZE :
  528. (setup_sg_tablesize >= 0) ? setup_sg_tablesize : ATARI_TT_SG_TABLESIZE;
  529. if (setup_hostid >= 0)
  530. host->this_id = setup_hostid;
  531. else {
  532. /* use 7 as default */
  533. host->this_id = 7;
  534. /* Test if a host id is set in the NVRam */
  535. if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) {
  536. unsigned char b = nvram_read_byte( 14 );
  537. /* Arbitration enabled? (for TOS) If yes, use configured host ID */
  538. if (b & 0x80)
  539. host->this_id = b & 7;
  540. }
  541. }
  542. #ifdef SUPPORT_TAGS
  543. if (setup_use_tagged_queuing < 0)
  544. setup_use_tagged_queuing = DEFAULT_USE_TAGGED_QUEUING;
  545. #endif
  546. #ifdef REAL_DMA
  547. /* If running on a Falcon and if there's TT-Ram (i.e., more than one
  548. * memory block, since there's always ST-Ram in a Falcon), then allocate a
  549. * STRAM_BUFFER_SIZE byte dribble buffer for transfers from/to alternative
  550. * Ram.
  551. */
  552. if (MACH_IS_ATARI && ATARIHW_PRESENT(ST_SCSI) &&
  553. !ATARIHW_PRESENT(EXTD_DMA) && m68k_num_memory > 1) {
  554. atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
  555. if (!atari_dma_buffer) {
  556. printk(KERN_ERR "atari_scsi_detect: can't allocate ST-RAM "
  557. "double buffer\n");
  558. return 0;
  559. }
  560. atari_dma_phys_buffer = virt_to_phys(atari_dma_buffer);
  561. atari_dma_orig_addr = 0;
  562. }
  563. #endif
  564. instance = scsi_register(host, sizeof(struct NCR5380_hostdata));
  565. if (instance == NULL) {
  566. atari_stram_free(atari_dma_buffer);
  567. atari_dma_buffer = 0;
  568. return 0;
  569. }
  570. atari_scsi_host = instance;
  571. /*
  572. * Set irq to 0, to avoid that the mid-level code disables our interrupt
  573. * during queue_command calls. This is completely unnecessary, and even
  574. * worse causes bad problems on the Falcon, where the int is shared with
  575. * IDE and floppy!
  576. */
  577. instance->irq = 0;
  578. #ifdef CONFIG_ATARI_SCSI_RESET_BOOT
  579. atari_scsi_reset_boot();
  580. #endif
  581. NCR5380_init(instance, 0);
  582. if (IS_A_TT()) {
  583. /* This int is actually "pseudo-slow", i.e. it acts like a slow
  584. * interrupt after having cleared the pending flag for the DMA
  585. * interrupt. */
  586. if (request_irq(IRQ_TT_MFP_SCSI, scsi_tt_intr, IRQ_TYPE_SLOW,
  587. "SCSI NCR5380", instance)) {
  588. printk(KERN_ERR "atari_scsi_detect: cannot allocate irq %d, aborting",IRQ_TT_MFP_SCSI);
  589. scsi_unregister(atari_scsi_host);
  590. atari_stram_free(atari_dma_buffer);
  591. atari_dma_buffer = 0;
  592. return 0;
  593. }
  594. tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
  595. #ifdef REAL_DMA
  596. tt_scsi_dma.dma_ctrl = 0;
  597. atari_dma_residual = 0;
  598. if (MACH_IS_MEDUSA) {
  599. /* While the read overruns (described by Drew Eckhardt in
  600. * NCR5380.c) never happened on TTs, they do in fact on the Medusa
  601. * (This was the cause why SCSI didn't work right for so long
  602. * there.) Since handling the overruns slows down a bit, I turned
  603. * the #ifdef's into a runtime condition.
  604. *
  605. * In principle it should be sufficient to do max. 1 byte with
  606. * PIO, but there is another problem on the Medusa with the DMA
  607. * rest data register. So 'atari_read_overruns' is currently set
  608. * to 4 to avoid having transfers that aren't a multiple of 4. If
  609. * the rest data bug is fixed, this can be lowered to 1.
  610. */
  611. atari_read_overruns = 4;
  612. }
  613. #endif /*REAL_DMA*/
  614. } else { /* ! IS_A_TT */
  615. /* Nothing to do for the interrupt: the ST-DMA is initialized
  616. * already by atari_init_INTS()
  617. */
  618. #ifdef REAL_DMA
  619. atari_dma_residual = 0;
  620. atari_dma_active = 0;
  621. atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
  622. : 0xff000000);
  623. #endif
  624. }
  625. printk(KERN_INFO "scsi%d: options CAN_QUEUE=%d CMD_PER_LUN=%d SCAT-GAT=%d "
  626. #ifdef SUPPORT_TAGS
  627. "TAGGED-QUEUING=%s "
  628. #endif
  629. "HOSTID=%d",
  630. instance->host_no, instance->hostt->can_queue,
  631. instance->hostt->cmd_per_lun,
  632. instance->hostt->sg_tablesize,
  633. #ifdef SUPPORT_TAGS
  634. setup_use_tagged_queuing ? "yes" : "no",
  635. #endif
  636. instance->hostt->this_id );
  637. NCR5380_print_options(instance);
  638. printk("\n");
  639. called = 1;
  640. return 1;
  641. }
  642. int atari_scsi_release(struct Scsi_Host *sh)
  643. {
  644. if (IS_A_TT())
  645. free_irq(IRQ_TT_MFP_SCSI, sh);
  646. if (atari_dma_buffer)
  647. atari_stram_free(atari_dma_buffer);
  648. return 1;
  649. }
  650. void __init atari_scsi_setup(char *str, int *ints)
  651. {
  652. /* Format of atascsi parameter is:
  653. * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
  654. * Defaults depend on TT or Falcon, hostid determined at run time.
  655. * Negative values mean don't change.
  656. */
  657. if (ints[0] < 1) {
  658. printk("atari_scsi_setup: no arguments!\n");
  659. return;
  660. }
  661. if (ints[0] >= 1) {
  662. if (ints[1] > 0)
  663. /* no limits on this, just > 0 */
  664. setup_can_queue = ints[1];
  665. }
  666. if (ints[0] >= 2) {
  667. if (ints[2] > 0)
  668. setup_cmd_per_lun = ints[2];
  669. }
  670. if (ints[0] >= 3) {
  671. if (ints[3] >= 0) {
  672. setup_sg_tablesize = ints[3];
  673. /* Must be <= SG_ALL (255) */
  674. if (setup_sg_tablesize > SG_ALL)
  675. setup_sg_tablesize = SG_ALL;
  676. }
  677. }
  678. if (ints[0] >= 4) {
  679. /* Must be between 0 and 7 */
  680. if (ints[4] >= 0 && ints[4] <= 7)
  681. setup_hostid = ints[4];
  682. else if (ints[4] > 7)
  683. printk("atari_scsi_setup: invalid host ID %d !\n", ints[4]);
  684. }
  685. #ifdef SUPPORT_TAGS
  686. if (ints[0] >= 5) {
  687. if (ints[5] >= 0)
  688. setup_use_tagged_queuing = !!ints[5];
  689. }
  690. #endif
  691. }
  692. int atari_scsi_bus_reset(Scsi_Cmnd *cmd)
  693. {
  694. int rv;
  695. struct NCR5380_hostdata *hostdata =
  696. (struct NCR5380_hostdata *)cmd->device->host->hostdata;
  697. /* For doing the reset, SCSI interrupts must be disabled first,
  698. * since the 5380 raises its IRQ line while _RST is active and we
  699. * can't disable interrupts completely, since we need the timer.
  700. */
  701. /* And abort a maybe active DMA transfer */
  702. if (IS_A_TT()) {
  703. atari_turnoff_irq(IRQ_TT_MFP_SCSI);
  704. #ifdef REAL_DMA
  705. tt_scsi_dma.dma_ctrl = 0;
  706. #endif /* REAL_DMA */
  707. } else {
  708. atari_turnoff_irq(IRQ_MFP_FSCSI);
  709. #ifdef REAL_DMA
  710. st_dma.dma_mode_status = 0x90;
  711. atari_dma_active = 0;
  712. atari_dma_orig_addr = NULL;
  713. #endif /* REAL_DMA */
  714. }
  715. rv = NCR5380_bus_reset(cmd);
  716. /* Re-enable ints */
  717. if (IS_A_TT()) {
  718. atari_turnon_irq(IRQ_TT_MFP_SCSI);
  719. } else {
  720. atari_turnon_irq(IRQ_MFP_FSCSI);
  721. }
  722. if ((rv & SCSI_RESET_ACTION) == SCSI_RESET_SUCCESS)
  723. falcon_release_lock_if_possible(hostdata);
  724. return rv;
  725. }
  726. #ifdef CONFIG_ATARI_SCSI_RESET_BOOT
  727. static void __init atari_scsi_reset_boot(void)
  728. {
  729. unsigned long end;
  730. /*
  731. * Do a SCSI reset to clean up the bus during initialization. No messing
  732. * with the queues, interrupts, or locks necessary here.
  733. */
  734. printk("Atari SCSI: resetting the SCSI bus...");
  735. /* get in phase */
  736. NCR5380_write(TARGET_COMMAND_REG,
  737. PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG)));
  738. /* assert RST */
  739. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST);
  740. /* The min. reset hold time is 25us, so 40us should be enough */
  741. udelay(50);
  742. /* reset RST and interrupt */
  743. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  744. NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  745. end = jiffies + AFTER_RESET_DELAY;
  746. while (time_before(jiffies, end))
  747. barrier();
  748. printk(" done\n");
  749. }
  750. #endif
  751. const char *atari_scsi_info(struct Scsi_Host *host)
  752. {
  753. /* atari_scsi_detect() is verbose enough... */
  754. static const char string[] = "Atari native SCSI";
  755. return string;
  756. }
  757. #if defined(REAL_DMA)
  758. unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance, void *data,
  759. unsigned long count, int dir)
  760. {
  761. unsigned long addr = virt_to_phys(data);
  762. DMA_PRINTK("scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, "
  763. "dir = %d\n", instance->host_no, data, addr, count, dir);
  764. if (!IS_A_TT() && !STRAM_ADDR(addr)) {
  765. /* If we have a non-DMAable address on a Falcon, use the dribble
  766. * buffer; 'orig_addr' != 0 in the read case tells the interrupt
  767. * handler to copy data from the dribble buffer to the originally
  768. * wanted address.
  769. */
  770. if (dir)
  771. memcpy(atari_dma_buffer, data, count);
  772. else
  773. atari_dma_orig_addr = data;
  774. addr = atari_dma_phys_buffer;
  775. }
  776. atari_dma_startaddr = addr; /* Needed for calculating residual later. */
  777. /* Cache cleanup stuff: On writes, push any dirty cache out before sending
  778. * it to the peripheral. (Must be done before DMA setup, since at least
  779. * the ST-DMA begins to fill internal buffers right after setup. For
  780. * reads, invalidate any cache, may be altered after DMA without CPU
  781. * knowledge.
  782. *
  783. * ++roman: For the Medusa, there's no need at all for that cache stuff,
  784. * because the hardware does bus snooping (fine!).
  785. */
  786. dma_cache_maintenance(addr, count, dir);
  787. if (count == 0)
  788. printk(KERN_NOTICE "SCSI warning: DMA programmed for 0 bytes !\n");
  789. if (IS_A_TT()) {
  790. tt_scsi_dma.dma_ctrl = dir;
  791. SCSI_DMA_WRITE_P(dma_addr, addr);
  792. SCSI_DMA_WRITE_P(dma_cnt, count);
  793. tt_scsi_dma.dma_ctrl = dir | 2;
  794. } else { /* ! IS_A_TT */
  795. /* set address */
  796. SCSI_DMA_SETADR(addr);
  797. /* toggle direction bit to clear FIFO and set DMA direction */
  798. dir <<= 8;
  799. st_dma.dma_mode_status = 0x90 | dir;
  800. st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
  801. st_dma.dma_mode_status = 0x90 | dir;
  802. udelay(40);
  803. /* On writes, round up the transfer length to the next multiple of 512
  804. * (see also comment at atari_dma_xfer_len()). */
  805. st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
  806. udelay(40);
  807. st_dma.dma_mode_status = 0x10 | dir;
  808. udelay(40);
  809. /* need not restore value of dir, only boolean value is tested */
  810. atari_dma_active = 1;
  811. }
  812. return count;
  813. }
  814. static long atari_scsi_dma_residual(struct Scsi_Host *instance)
  815. {
  816. return atari_dma_residual;
  817. }
  818. #define CMD_SURELY_BLOCK_MODE 0
  819. #define CMD_SURELY_BYTE_MODE 1
  820. #define CMD_MODE_UNKNOWN 2
  821. static int falcon_classify_cmd(Scsi_Cmnd *cmd)
  822. {
  823. unsigned char opcode = cmd->cmnd[0];
  824. if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
  825. opcode == READ_BUFFER)
  826. return CMD_SURELY_BYTE_MODE;
  827. else if (opcode == READ_6 || opcode == READ_10 ||
  828. opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
  829. opcode == RECOVER_BUFFERED_DATA) {
  830. /* In case of a sequential-access target (tape), special care is
  831. * needed here: The transfer is block-mode only if the 'fixed' bit is
  832. * set! */
  833. if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
  834. return CMD_SURELY_BYTE_MODE;
  835. else
  836. return CMD_SURELY_BLOCK_MODE;
  837. } else
  838. return CMD_MODE_UNKNOWN;
  839. }
  840. /* This function calculates the number of bytes that can be transferred via
  841. * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
  842. * ST-DMA chip. There are only multiples of 512 bytes possible and max.
  843. * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
  844. * possible on the Falcon, since that would require to program the DMA for
  845. * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
  846. * the overrun problem, so this question is academic :-)
  847. */
  848. static unsigned long atari_dma_xfer_len(unsigned long wanted_len,
  849. Scsi_Cmnd *cmd, int write_flag)
  850. {
  851. unsigned long possible_len, limit;
  852. if (IS_A_TT())
  853. /* TT SCSI DMA can transfer arbitrary #bytes */
  854. return wanted_len;
  855. /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
  856. * 255*512 bytes, but this should be enough)
  857. *
  858. * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
  859. * that return a number of bytes which cannot be known beforehand. In this
  860. * case, the given transfer length is an "allocation length". Now it
  861. * can happen that this allocation length is a multiple of 512 bytes and
  862. * the DMA is used. But if not n*512 bytes really arrive, some input data
  863. * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
  864. * between commands that do block transfers and those that do byte
  865. * transfers. But this isn't easy... there are lots of vendor specific
  866. * commands, and the user can issue any command via the
  867. * SCSI_IOCTL_SEND_COMMAND.
  868. *
  869. * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
  870. * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
  871. * and 3), the thing to do is obvious: allow any number of blocks via DMA
  872. * or none. In case 2), we apply some heuristic: Byte mode is assumed if
  873. * the transfer (allocation) length is < 1024, hoping that no cmd. not
  874. * explicitly known as byte mode have such big allocation lengths...
  875. * BTW, all the discussion above applies only to reads. DMA writes are
  876. * unproblematic anyways, since the targets aborts the transfer after
  877. * receiving a sufficient number of bytes.
  878. *
  879. * Another point: If the transfer is from/to an non-ST-RAM address, we
  880. * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
  881. */
  882. if (write_flag) {
  883. /* Write operation can always use the DMA, but the transfer size must
  884. * be rounded up to the next multiple of 512 (atari_dma_setup() does
  885. * this).
  886. */
  887. possible_len = wanted_len;
  888. } else {
  889. /* Read operations: if the wanted transfer length is not a multiple of
  890. * 512, we cannot use DMA, since the ST-DMA cannot split transfers
  891. * (no interrupt on DMA finished!)
  892. */
  893. if (wanted_len & 0x1ff)
  894. possible_len = 0;
  895. else {
  896. /* Now classify the command (see above) and decide whether it is
  897. * allowed to do DMA at all */
  898. switch (falcon_classify_cmd(cmd)) {
  899. case CMD_SURELY_BLOCK_MODE:
  900. possible_len = wanted_len;
  901. break;
  902. case CMD_SURELY_BYTE_MODE:
  903. possible_len = 0; /* DMA prohibited */
  904. break;
  905. case CMD_MODE_UNKNOWN:
  906. default:
  907. /* For unknown commands assume block transfers if the transfer
  908. * size/allocation length is >= 1024 */
  909. possible_len = (wanted_len < 1024) ? 0 : wanted_len;
  910. break;
  911. }
  912. }
  913. }
  914. /* Last step: apply the hard limit on DMA transfers */
  915. limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ?
  916. STRAM_BUFFER_SIZE : 255*512;
  917. if (possible_len > limit)
  918. possible_len = limit;
  919. if (possible_len != wanted_len)
  920. DMA_PRINTK("Sorry, must cut DMA transfer size to %ld bytes "
  921. "instead of %ld\n", possible_len, wanted_len);
  922. return possible_len;
  923. }
  924. #endif /* REAL_DMA */
  925. /* NCR5380 register access functions
  926. *
  927. * There are separate functions for TT and Falcon, because the access
  928. * methods are quite different. The calling macros NCR5380_read and
  929. * NCR5380_write call these functions via function pointers.
  930. */
  931. static unsigned char atari_scsi_tt_reg_read(unsigned char reg)
  932. {
  933. return tt_scsi_regp[reg * 2];
  934. }
  935. static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value)
  936. {
  937. tt_scsi_regp[reg * 2] = value;
  938. }
  939. static unsigned char atari_scsi_falcon_reg_read(unsigned char reg)
  940. {
  941. dma_wd.dma_mode_status= (u_short)(0x88 + reg);
  942. return (u_char)dma_wd.fdc_acces_seccount;
  943. }
  944. static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value)
  945. {
  946. dma_wd.dma_mode_status = (u_short)(0x88 + reg);
  947. dma_wd.fdc_acces_seccount = (u_short)value;
  948. }
  949. #include "atari_NCR5380.c"
  950. static struct scsi_host_template driver_template = {
  951. .proc_info = atari_scsi_proc_info,
  952. .name = "Atari native SCSI",
  953. .detect = atari_scsi_detect,
  954. .release = atari_scsi_release,
  955. .info = atari_scsi_info,
  956. .queuecommand = atari_scsi_queue_command,
  957. .eh_abort_handler = atari_scsi_abort,
  958. .eh_bus_reset_handler = atari_scsi_bus_reset,
  959. .can_queue = 0, /* initialized at run-time */
  960. .this_id = 0, /* initialized at run-time */
  961. .sg_tablesize = 0, /* initialized at run-time */
  962. .cmd_per_lun = 0, /* initialized at run-time */
  963. .use_clustering = DISABLE_CLUSTERING
  964. };
  965. #include "scsi_module.c"
  966. MODULE_LICENSE("GPL");