seagate.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664
  1. /*
  2. * seagate.c Copyright (C) 1992, 1993 Drew Eckhardt
  3. * low level scsi driver for ST01/ST02, Future Domain TMC-885,
  4. * TMC-950 by Drew Eckhardt <drew@colorado.edu>
  5. *
  6. * Note : TMC-880 boards don't work because they have two bits in
  7. * the status register flipped, I'll fix this "RSN"
  8. * [why do I have strong feeling that above message is from 1993? :-)
  9. * pavel@ucw.cz]
  10. *
  11. * This card does all the I/O via memory mapped I/O, so there is no need
  12. * to check or allocate a region of the I/O address space.
  13. */
  14. /* 1996 - to use new read{b,w,l}, write{b,w,l}, and phys_to_virt
  15. * macros, replaced assembler routines with C. There's probably a
  16. * performance hit, but I only have a cdrom and can't tell. Define
  17. * SEAGATE_USE_ASM if you want the old assembler code -- SJT
  18. *
  19. * 1998-jul-29 - created DPRINTK macros and made it work under
  20. * linux 2.1.112, simplified some #defines etc. <pavel@ucw.cz>
  21. *
  22. * Aug 2000 - aeb - deleted seagate_st0x_biosparam(). It would try to
  23. * read the physical disk geometry, a bad mistake. Of course it doesn't
  24. * matter much what geometry one invents, but on large disks it
  25. * returned 256 (or more) heads, causing all kind of failures.
  26. * Of course this means that people might see a different geometry now,
  27. * so boot parameters may be necessary in some cases.
  28. */
  29. /*
  30. * Configuration :
  31. * To use without BIOS -DOVERRIDE=base_address -DCONTROLLER=FD or SEAGATE
  32. * -DIRQ will override the default of 5.
  33. * Note: You can now set these options from the kernel's "command line".
  34. * The syntax is:
  35. *
  36. * st0x=ADDRESS,IRQ (for a Seagate controller)
  37. * or:
  38. * tmc8xx=ADDRESS,IRQ (for a TMC-8xx or TMC-950 controller)
  39. * eg:
  40. * tmc8xx=0xC8000,15
  41. *
  42. * will configure the driver for a TMC-8xx style controller using IRQ 15
  43. * with a base address of 0xC8000.
  44. *
  45. * -DARBITRATE
  46. * Will cause the host adapter to arbitrate for the
  47. * bus for better SCSI-II compatibility, rather than just
  48. * waiting for BUS FREE and then doing its thing. Should
  49. * let us do one command per Lun when I integrate my
  50. * reorganization changes into the distribution sources.
  51. *
  52. * -DDEBUG=65535
  53. * Will activate debug code.
  54. *
  55. * -DFAST or -DFAST32
  56. * Will use blind transfers where possible
  57. *
  58. * -DPARITY
  59. * This will enable parity.
  60. *
  61. * -DSEAGATE_USE_ASM
  62. * Will use older seagate assembly code. should be (very small amount)
  63. * Faster.
  64. *
  65. * -DSLOW_RATE=50
  66. * Will allow compatibility with broken devices that don't
  67. * handshake fast enough (ie, some CD ROM's) for the Seagate
  68. * code.
  69. *
  70. * 50 is some number, It will let you specify a default
  71. * transfer rate if handshaking isn't working correctly.
  72. *
  73. * -DOLDCNTDATASCEME There is a new sceme to set the CONTROL
  74. * and DATA reigsters which complies more closely
  75. * with the SCSI2 standard. This hopefully eliminates
  76. * the need to swap the order these registers are
  77. * 'messed' with. It makes the following two options
  78. * obsolete. To reenable the old sceme define this.
  79. *
  80. * The following to options are patches from the SCSI.HOWTO
  81. *
  82. * -DSWAPSTAT This will swap the definitions for STAT_MSG and STAT_CD.
  83. *
  84. * -DSWAPCNTDATA This will swap the order that seagate.c messes with
  85. * the CONTROL an DATA registers.
  86. */
  87. #include <linux/module.h>
  88. #include <linux/interrupt.h>
  89. #include <linux/spinlock.h>
  90. #include <linux/signal.h>
  91. #include <linux/string.h>
  92. #include <linux/proc_fs.h>
  93. #include <linux/init.h>
  94. #include <linux/delay.h>
  95. #include <linux/blkdev.h>
  96. #include <linux/stat.h>
  97. #include <asm/io.h>
  98. #include <asm/system.h>
  99. #include <asm/uaccess.h>
  100. #include "scsi.h"
  101. #include <scsi/scsi_dbg.h>
  102. #include <scsi/scsi_host.h>
  103. #include "seagate.h"
  104. #include <scsi/scsi_ioctl.h>
  105. #ifdef DEBUG
  106. #define DPRINTK( when, msg... ) do { if ( (DEBUG & (when)) == (when) ) printk( msg ); } while (0)
  107. #else
  108. #define DPRINTK( when, msg... ) do { } while (0)
  109. #endif
  110. #define DANY( msg... ) DPRINTK( 0xffff, msg );
  111. #ifndef IRQ
  112. #define IRQ 5
  113. #endif
  114. #ifdef FAST32
  115. #define FAST
  116. #endif
  117. #undef LINKED /* Linked commands are currently broken! */
  118. #if defined(OVERRIDE) && !defined(CONTROLLER)
  119. #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
  120. #endif
  121. #ifndef __i386__
  122. #undef SEAGATE_USE_ASM
  123. #endif
  124. /*
  125. Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
  126. driver, and Mitsugu Suzuki for information on the ST-01
  127. SCSI host.
  128. */
  129. /*
  130. CONTROL defines
  131. */
  132. #define CMD_RST 0x01
  133. #define CMD_SEL 0x02
  134. #define CMD_BSY 0x04
  135. #define CMD_ATTN 0x08
  136. #define CMD_START_ARB 0x10
  137. #define CMD_EN_PARITY 0x20
  138. #define CMD_INTR 0x40
  139. #define CMD_DRVR_ENABLE 0x80
  140. /*
  141. STATUS
  142. */
  143. #ifdef SWAPSTAT
  144. #define STAT_MSG 0x08
  145. #define STAT_CD 0x02
  146. #else
  147. #define STAT_MSG 0x02
  148. #define STAT_CD 0x08
  149. #endif
  150. #define STAT_BSY 0x01
  151. #define STAT_IO 0x04
  152. #define STAT_REQ 0x10
  153. #define STAT_SEL 0x20
  154. #define STAT_PARITY 0x40
  155. #define STAT_ARB_CMPL 0x80
  156. /*
  157. REQUESTS
  158. */
  159. #define REQ_MASK (STAT_CD | STAT_IO | STAT_MSG)
  160. #define REQ_DATAOUT 0
  161. #define REQ_DATAIN STAT_IO
  162. #define REQ_CMDOUT STAT_CD
  163. #define REQ_STATIN (STAT_CD | STAT_IO)
  164. #define REQ_MSGOUT (STAT_MSG | STAT_CD)
  165. #define REQ_MSGIN (STAT_MSG | STAT_CD | STAT_IO)
  166. extern volatile int seagate_st0x_timeout;
  167. #ifdef PARITY
  168. #define BASE_CMD CMD_EN_PARITY
  169. #else
  170. #define BASE_CMD 0
  171. #endif
  172. /*
  173. Debugging code
  174. */
  175. #define PHASE_BUS_FREE 1
  176. #define PHASE_ARBITRATION 2
  177. #define PHASE_SELECTION 4
  178. #define PHASE_DATAIN 8
  179. #define PHASE_DATAOUT 0x10
  180. #define PHASE_CMDOUT 0x20
  181. #define PHASE_MSGIN 0x40
  182. #define PHASE_MSGOUT 0x80
  183. #define PHASE_STATUSIN 0x100
  184. #define PHASE_ETC (PHASE_DATAIN | PHASE_DATAOUT | PHASE_CMDOUT | PHASE_MSGIN | PHASE_MSGOUT | PHASE_STATUSIN)
  185. #define PRINT_COMMAND 0x200
  186. #define PHASE_EXIT 0x400
  187. #define PHASE_RESELECT 0x800
  188. #define DEBUG_FAST 0x1000
  189. #define DEBUG_SG 0x2000
  190. #define DEBUG_LINKED 0x4000
  191. #define DEBUG_BORKEN 0x8000
  192. /*
  193. * Control options - these are timeouts specified in .01 seconds.
  194. */
  195. /* 30, 20 work */
  196. #define ST0X_BUS_FREE_DELAY 25
  197. #define ST0X_SELECTION_DELAY 25
  198. #define SEAGATE 1 /* these determine the type of the controller */
  199. #define FD 2
  200. #define ST0X_ID_STR "Seagate ST-01/ST-02"
  201. #define FD_ID_STR "TMC-8XX/TMC-950"
  202. static int internal_command (unsigned char target, unsigned char lun,
  203. const void *cmnd,
  204. void *buff, int bufflen, int reselect);
  205. static int incommand; /* set if arbitration has finished
  206. and we are in some command phase. */
  207. static unsigned int base_address = 0; /* Where the card ROM starts, used to
  208. calculate memory mapped register
  209. location. */
  210. static void __iomem *st0x_cr_sr; /* control register write, status
  211. register read. 256 bytes in
  212. length.
  213. Read is status of SCSI BUS, as per
  214. STAT masks. */
  215. static void __iomem *st0x_dr; /* data register, read write 256
  216. bytes in length. */
  217. static volatile int st0x_aborted = 0; /* set when we are aborted, ie by a
  218. time out, etc. */
  219. static unsigned char controller_type = 0; /* set to SEAGATE for ST0x
  220. boards or FD for TMC-8xx
  221. boards */
  222. static int irq = IRQ;
  223. module_param(base_address, uint, 0);
  224. module_param(controller_type, byte, 0);
  225. module_param(irq, int, 0);
  226. MODULE_LICENSE("GPL");
  227. #define retcode(result) (((result) << 16) | (message << 8) | status)
  228. #define STATUS ((u8) readb(st0x_cr_sr))
  229. #define DATA ((u8) readb(st0x_dr))
  230. #define WRITE_CONTROL(d) { writeb((d), st0x_cr_sr); }
  231. #define WRITE_DATA(d) { writeb((d), st0x_dr); }
  232. #ifndef OVERRIDE
  233. static unsigned int seagate_bases[] = {
  234. 0xc8000, 0xca000, 0xcc000,
  235. 0xce000, 0xdc000, 0xde000
  236. };
  237. typedef struct {
  238. const unsigned char *signature;
  239. unsigned offset;
  240. unsigned length;
  241. unsigned char type;
  242. } Signature;
  243. static Signature __initdata signatures[] = {
  244. {"ST01 v1.7 (C) Copyright 1987 Seagate", 15, 37, SEAGATE},
  245. {"SCSI BIOS 2.00 (C) Copyright 1987 Seagate", 15, 40, SEAGATE},
  246. /*
  247. * The following two lines are NOT mistakes. One detects ROM revision
  248. * 3.0.0, the other 3.2. Since seagate has only one type of SCSI adapter,
  249. * and this is not going to change, the "SEAGATE" and "SCSI" together
  250. * are probably "good enough"
  251. */
  252. {"SEAGATE SCSI BIOS ", 16, 17, SEAGATE},
  253. {"SEAGATE SCSI BIOS ", 17, 17, SEAGATE},
  254. /*
  255. * However, future domain makes several incompatible SCSI boards, so specific
  256. * signatures must be used.
  257. */
  258. {"FUTURE DOMAIN CORP. (C) 1986-1989 V5.0C2/14/89", 5, 46, FD},
  259. {"FUTURE DOMAIN CORP. (C) 1986-1989 V6.0A7/28/89", 5, 46, FD},
  260. {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0105/31/90", 5, 47, FD},
  261. {"FUTURE DOMAIN CORP. (C) 1986-1990 V6.0209/18/90", 5, 47, FD},
  262. {"FUTURE DOMAIN CORP. (C) 1986-1990 V7.009/18/90", 5, 46, FD},
  263. {"FUTURE DOMAIN CORP. (C) 1992 V8.00.004/02/92", 5, 44, FD},
  264. {"IBM F1 BIOS V1.1004/30/92", 5, 25, FD},
  265. {"FUTURE DOMAIN TMC-950", 5, 21, FD},
  266. /* Added for 2.2.16 by Matthias_Heidbrink@b.maus.de */
  267. {"IBM F1 V1.2009/22/93", 5, 25, FD},
  268. };
  269. #define NUM_SIGNATURES (sizeof(signatures) / sizeof(Signature))
  270. #endif /* n OVERRIDE */
  271. /*
  272. * hostno stores the hostnumber, as told to us by the init routine.
  273. */
  274. static int hostno = -1;
  275. static void seagate_reconnect_intr (int, void *, struct pt_regs *);
  276. static irqreturn_t do_seagate_reconnect_intr (int, void *, struct pt_regs *);
  277. #ifdef FAST
  278. static int fast = 1;
  279. #else
  280. #define fast 0
  281. #endif
  282. #ifdef SLOW_RATE
  283. /*
  284. * Support for broken devices :
  285. * The Seagate board has a handshaking problem. Namely, a lack
  286. * thereof for slow devices. You can blast 600K/second through
  287. * it if you are polling for each byte, more if you do a blind
  288. * transfer. In the first case, with a fast device, REQ will
  289. * transition high-low or high-low-high before your loop restarts
  290. * and you'll have no problems. In the second case, the board
  291. * will insert wait states for up to 13.2 usecs for REQ to
  292. * transition low->high, and everything will work.
  293. *
  294. * However, there's nothing in the state machine that says
  295. * you *HAVE* to see a high-low-high set of transitions before
  296. * sending the next byte, and slow things like the Trantor CD ROMS
  297. * will break because of this.
  298. *
  299. * So, we need to slow things down, which isn't as simple as it
  300. * seems. We can't slow things down period, because then people
  301. * who don't recompile their kernels will shoot me for ruining
  302. * their performance. We need to do it on a case per case basis.
  303. *
  304. * The best for performance will be to, only for borken devices
  305. * (this is stored on a per-target basis in the scsi_devices array)
  306. *
  307. * Wait for a low->high transition before continuing with that
  308. * transfer. If we timeout, continue anyways. We don't need
  309. * a long timeout, because REQ should only be asserted until the
  310. * corresponding ACK is received and processed.
  311. *
  312. * Note that we can't use the system timer for this, because of
  313. * resolution, and we *really* can't use the timer chip since
  314. * gettimeofday() and the beeper routines use that. So,
  315. * the best thing for us to do will be to calibrate a timing
  316. * loop in the initialization code using the timer chip before
  317. * gettimeofday() can screw with it.
  318. *
  319. * FIXME: this is broken (not borken :-). Empty loop costs less than
  320. * loop with ISA access in it! -- pavel@ucw.cz
  321. */
  322. static int borken_calibration = 0;
  323. static void __init borken_init (void)
  324. {
  325. register int count = 0, start = jiffies + 1, stop = start + 25;
  326. /* FIXME: There may be a better approach, this is a straight port for
  327. now */
  328. preempt_disable();
  329. while (time_before (jiffies, start))
  330. cpu_relax();
  331. for (; time_before (jiffies, stop); ++count)
  332. cpu_relax();
  333. preempt_enable();
  334. /*
  335. * Ok, we now have a count for .25 seconds. Convert to a
  336. * count per second and divide by transfer rate in K. */
  337. borken_calibration = (count * 4) / (SLOW_RATE * 1024);
  338. if (borken_calibration < 1)
  339. borken_calibration = 1;
  340. }
  341. static inline void borken_wait (void)
  342. {
  343. register int count;
  344. for (count = borken_calibration; count && (STATUS & STAT_REQ); --count)
  345. cpu_relax();
  346. #if (DEBUG & DEBUG_BORKEN)
  347. if (count)
  348. printk ("scsi%d : borken timeout\n", hostno);
  349. #endif
  350. }
  351. #endif /* def SLOW_RATE */
  352. /* These beasts only live on ISA, and ISA means 8MHz. Each ULOOP()
  353. * contains at least one ISA access, which takes more than 0.125
  354. * usec. So if we loop 8 times time in usec, we are safe.
  355. */
  356. #define ULOOP( i ) for (clock = i*8;;)
  357. #define TIMEOUT (!(clock--))
  358. int __init seagate_st0x_detect (Scsi_Host_Template * tpnt)
  359. {
  360. struct Scsi_Host *instance;
  361. int i, j;
  362. unsigned long cr, dr;
  363. tpnt->proc_name = "seagate";
  364. /*
  365. * First, we try for the manual override.
  366. */
  367. DANY ("Autodetecting ST0x / TMC-8xx\n");
  368. if (hostno != -1) {
  369. printk (KERN_ERR "seagate_st0x_detect() called twice?!\n");
  370. return 0;
  371. }
  372. /* If the user specified the controller type from the command line,
  373. controller_type will be non-zero, so don't try to detect one */
  374. if (!controller_type) {
  375. #ifdef OVERRIDE
  376. base_address = OVERRIDE;
  377. controller_type = CONTROLLER;
  378. DANY ("Base address overridden to %x, controller type is %s\n",
  379. base_address,
  380. controller_type == SEAGATE ? "SEAGATE" : "FD");
  381. #else /* OVERRIDE */
  382. /*
  383. * To detect this card, we simply look for the signature
  384. * from the BIOS version notice in all the possible locations
  385. * of the ROM's. This has a nice side effect of not trashing
  386. * any register locations that might be used by something else.
  387. *
  388. * XXX - note that we probably should be probing the address
  389. * space for the on-board RAM instead.
  390. */
  391. for (i = 0; i < (sizeof (seagate_bases) / sizeof (unsigned int)); ++i) {
  392. void __iomem *p = ioremap(seagate_bases[i], 0x2000);
  393. if (!p)
  394. continue;
  395. for (j = 0; j < NUM_SIGNATURES; ++j)
  396. if (check_signature(p + signatures[j].offset, signatures[j].signature, signatures[j].length)) {
  397. base_address = seagate_bases[i];
  398. controller_type = signatures[j].type;
  399. break;
  400. }
  401. iounmap(p);
  402. }
  403. #endif /* OVERRIDE */
  404. }
  405. /* (! controller_type) */
  406. tpnt->this_id = (controller_type == SEAGATE) ? 7 : 6;
  407. tpnt->name = (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR;
  408. if (!base_address) {
  409. printk(KERN_INFO "seagate: ST0x/TMC-8xx not detected.\n");
  410. return 0;
  411. }
  412. cr = base_address + (controller_type == SEAGATE ? 0x1a00 : 0x1c00);
  413. dr = cr + 0x200;
  414. st0x_cr_sr = ioremap(cr, 0x100);
  415. st0x_dr = ioremap(dr, 0x100);
  416. DANY("%s detected. Base address = %x, cr = %x, dr = %x\n",
  417. tpnt->name, base_address, cr, dr);
  418. /*
  419. * At all times, we will use IRQ 5. Should also check for IRQ3
  420. * if we lose our first interrupt.
  421. */
  422. instance = scsi_register (tpnt, 0);
  423. if (instance == NULL)
  424. return 0;
  425. hostno = instance->host_no;
  426. if (request_irq (irq, do_seagate_reconnect_intr, SA_INTERRUPT, (controller_type == SEAGATE) ? "seagate" : "tmc-8xx", instance)) {
  427. printk(KERN_ERR "scsi%d : unable to allocate IRQ%d\n", hostno, irq);
  428. return 0;
  429. }
  430. instance->irq = irq;
  431. instance->io_port = base_address;
  432. #ifdef SLOW_RATE
  433. printk(KERN_INFO "Calibrating borken timer... ");
  434. borken_init();
  435. printk(" %d cycles per transfer\n", borken_calibration);
  436. #endif
  437. printk (KERN_INFO "This is one second... ");
  438. {
  439. int clock;
  440. ULOOP (1 * 1000 * 1000) {
  441. STATUS;
  442. if (TIMEOUT)
  443. break;
  444. }
  445. }
  446. printk ("done, %s options:"
  447. #ifdef ARBITRATE
  448. " ARBITRATE"
  449. #endif
  450. #ifdef DEBUG
  451. " DEBUG"
  452. #endif
  453. #ifdef FAST
  454. " FAST"
  455. #ifdef FAST32
  456. "32"
  457. #endif
  458. #endif
  459. #ifdef LINKED
  460. " LINKED"
  461. #endif
  462. #ifdef PARITY
  463. " PARITY"
  464. #endif
  465. #ifdef SEAGATE_USE_ASM
  466. " SEAGATE_USE_ASM"
  467. #endif
  468. #ifdef SLOW_RATE
  469. " SLOW_RATE"
  470. #endif
  471. #ifdef SWAPSTAT
  472. " SWAPSTAT"
  473. #endif
  474. #ifdef SWAPCNTDATA
  475. " SWAPCNTDATA"
  476. #endif
  477. "\n", tpnt->name);
  478. return 1;
  479. }
  480. static const char *seagate_st0x_info (struct Scsi_Host *shpnt)
  481. {
  482. static char buffer[64];
  483. snprintf(buffer, 64, "%s at irq %d, address 0x%05X",
  484. (controller_type == SEAGATE) ? ST0X_ID_STR : FD_ID_STR,
  485. irq, base_address);
  486. return buffer;
  487. }
  488. /*
  489. * These are our saved pointers for the outstanding command that is
  490. * waiting for a reconnect
  491. */
  492. static unsigned char current_target, current_lun;
  493. static unsigned char *current_cmnd, *current_data;
  494. static int current_nobuffs;
  495. static struct scatterlist *current_buffer;
  496. static int current_bufflen;
  497. #ifdef LINKED
  498. /*
  499. * linked_connected indicates whether or not we are currently connected to
  500. * linked_target, linked_lun and in an INFORMATION TRANSFER phase,
  501. * using linked commands.
  502. */
  503. static int linked_connected = 0;
  504. static unsigned char linked_target, linked_lun;
  505. #endif
  506. static void (*done_fn) (Scsi_Cmnd *) = NULL;
  507. static Scsi_Cmnd *SCint = NULL;
  508. /*
  509. * These control whether or not disconnect / reconnect will be attempted,
  510. * or are being attempted.
  511. */
  512. #define NO_RECONNECT 0
  513. #define RECONNECT_NOW 1
  514. #define CAN_RECONNECT 2
  515. /*
  516. * LINKED_RIGHT indicates that we are currently connected to the correct target
  517. * for this command, LINKED_WRONG indicates that we are connected to the wrong
  518. * target. Note that these imply CAN_RECONNECT and require defined(LINKED).
  519. */
  520. #define LINKED_RIGHT 3
  521. #define LINKED_WRONG 4
  522. /*
  523. * This determines if we are expecting to reconnect or not.
  524. */
  525. static int should_reconnect = 0;
  526. /*
  527. * The seagate_reconnect_intr routine is called when a target reselects the
  528. * host adapter. This occurs on the interrupt triggered by the target
  529. * asserting SEL.
  530. */
  531. static irqreturn_t do_seagate_reconnect_intr(int irq, void *dev_id,
  532. struct pt_regs *regs)
  533. {
  534. unsigned long flags;
  535. struct Scsi_Host *dev = dev_id;
  536. spin_lock_irqsave (dev->host_lock, flags);
  537. seagate_reconnect_intr (irq, dev_id, regs);
  538. spin_unlock_irqrestore (dev->host_lock, flags);
  539. return IRQ_HANDLED;
  540. }
  541. static void seagate_reconnect_intr (int irq, void *dev_id, struct pt_regs *regs)
  542. {
  543. int temp;
  544. Scsi_Cmnd *SCtmp;
  545. DPRINTK (PHASE_RESELECT, "scsi%d : seagate_reconnect_intr() called\n", hostno);
  546. if (!should_reconnect)
  547. printk(KERN_WARNING "scsi%d: unexpected interrupt.\n", hostno);
  548. else {
  549. should_reconnect = 0;
  550. DPRINTK (PHASE_RESELECT, "scsi%d : internal_command(%d, %08x, %08x, RECONNECT_NOW\n",
  551. hostno, current_target, current_data, current_bufflen);
  552. temp = internal_command (current_target, current_lun, current_cmnd, current_data, current_bufflen, RECONNECT_NOW);
  553. if (msg_byte(temp) != DISCONNECT) {
  554. if (done_fn) {
  555. DPRINTK(PHASE_RESELECT, "scsi%d : done_fn(%d,%08x)", hostno, hostno, temp);
  556. if (!SCint)
  557. panic ("SCint == NULL in seagate");
  558. SCtmp = SCint;
  559. SCint = NULL;
  560. SCtmp->result = temp;
  561. done_fn(SCtmp);
  562. } else
  563. printk(KERN_ERR "done_fn() not defined.\n");
  564. }
  565. }
  566. }
  567. /*
  568. * The seagate_st0x_queue_command() function provides a queued interface
  569. * to the seagate SCSI driver. Basically, it just passes control onto the
  570. * seagate_command() function, after fixing it so that the done_fn()
  571. * is set to the one passed to the function. We have to be very careful,
  572. * because there are some commands on some devices that do not disconnect,
  573. * and if we simply call the done_fn when the command is done then another
  574. * command is started and queue_command is called again... We end up
  575. * overflowing the kernel stack, and this tends not to be such a good idea.
  576. */
  577. static int recursion_depth = 0;
  578. static int seagate_st0x_queue_command (Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
  579. {
  580. int result, reconnect;
  581. Scsi_Cmnd *SCtmp;
  582. DANY ("seagate: que_command");
  583. done_fn = done;
  584. current_target = SCpnt->device->id;
  585. current_lun = SCpnt->device->lun;
  586. current_cmnd = SCpnt->cmnd;
  587. current_data = (unsigned char *) SCpnt->request_buffer;
  588. current_bufflen = SCpnt->request_bufflen;
  589. SCint = SCpnt;
  590. if (recursion_depth)
  591. return 1;
  592. recursion_depth++;
  593. do {
  594. #ifdef LINKED
  595. /*
  596. * Set linked command bit in control field of SCSI command.
  597. */
  598. current_cmnd[SCpnt->cmd_len] |= 0x01;
  599. if (linked_connected) {
  600. DPRINTK (DEBUG_LINKED, "scsi%d : using linked commands, current I_T_L nexus is ", hostno);
  601. if (linked_target == current_target && linked_lun == current_lun)
  602. {
  603. DPRINTK(DEBUG_LINKED, "correct\n");
  604. reconnect = LINKED_RIGHT;
  605. } else {
  606. DPRINTK(DEBUG_LINKED, "incorrect\n");
  607. reconnect = LINKED_WRONG;
  608. }
  609. } else
  610. #endif /* LINKED */
  611. reconnect = CAN_RECONNECT;
  612. result = internal_command(SCint->device->id, SCint->device->lun, SCint->cmnd,
  613. SCint->request_buffer, SCint->request_bufflen, reconnect);
  614. if (msg_byte(result) == DISCONNECT)
  615. break;
  616. SCtmp = SCint;
  617. SCint = NULL;
  618. SCtmp->result = result;
  619. done_fn(SCtmp);
  620. }
  621. while (SCint);
  622. recursion_depth--;
  623. return 0;
  624. }
  625. static int internal_command (unsigned char target, unsigned char lun,
  626. const void *cmnd, void *buff, int bufflen, int reselect)
  627. {
  628. unsigned char *data = NULL;
  629. struct scatterlist *buffer = NULL;
  630. int clock, temp, nobuffs = 0, done = 0, len = 0;
  631. #ifdef DEBUG
  632. int transfered = 0, phase = 0, newphase;
  633. #endif
  634. register unsigned char status_read;
  635. unsigned char tmp_data, tmp_control, status = 0, message = 0;
  636. unsigned transfersize = 0, underflow = 0;
  637. #ifdef SLOW_RATE
  638. int borken = (int) SCint->device->borken; /* Does the current target require
  639. Very Slow I/O ? */
  640. #endif
  641. incommand = 0;
  642. st0x_aborted = 0;
  643. #if (DEBUG & PRINT_COMMAND)
  644. printk("scsi%d : target = %d, command = ", hostno, target);
  645. __scsi_print_command((unsigned char *) cmnd);
  646. #endif
  647. #if (DEBUG & PHASE_RESELECT)
  648. switch (reselect) {
  649. case RECONNECT_NOW:
  650. printk("scsi%d : reconnecting\n", hostno);
  651. break;
  652. #ifdef LINKED
  653. case LINKED_RIGHT:
  654. printk("scsi%d : connected, can reconnect\n", hostno);
  655. break;
  656. case LINKED_WRONG:
  657. printk("scsi%d : connected to wrong target, can reconnect\n",
  658. hostno);
  659. break;
  660. #endif
  661. case CAN_RECONNECT:
  662. printk("scsi%d : allowed to reconnect\n", hostno);
  663. break;
  664. default:
  665. printk("scsi%d : not allowed to reconnect\n", hostno);
  666. }
  667. #endif
  668. if (target == (controller_type == SEAGATE ? 7 : 6))
  669. return DID_BAD_TARGET;
  670. /*
  671. * We work it differently depending on if this is is "the first time,"
  672. * or a reconnect. If this is a reselect phase, then SEL will
  673. * be asserted, and we must skip selection / arbitration phases.
  674. */
  675. switch (reselect) {
  676. case RECONNECT_NOW:
  677. DPRINTK (PHASE_RESELECT, "scsi%d : phase RESELECT \n", hostno);
  678. /*
  679. * At this point, we should find the logical or of our ID
  680. * and the original target's ID on the BUS, with BSY, SEL,
  681. * and I/O signals asserted.
  682. *
  683. * After ARBITRATION phase is completed, only SEL, BSY,
  684. * and the target ID are asserted. A valid initiator ID
  685. * is not on the bus until IO is asserted, so we must wait
  686. * for that.
  687. */
  688. ULOOP (100 * 1000) {
  689. temp = STATUS;
  690. if ((temp & STAT_IO) && !(temp & STAT_BSY))
  691. break;
  692. if (TIMEOUT) {
  693. DPRINTK (PHASE_RESELECT, "scsi%d : RESELECT timed out while waiting for IO .\n", hostno);
  694. return (DID_BAD_INTR << 16);
  695. }
  696. }
  697. /*
  698. * After I/O is asserted by the target, we can read our ID
  699. * and its ID off of the BUS.
  700. */
  701. if (!((temp = DATA) & (controller_type == SEAGATE ? 0x80 : 0x40))) {
  702. DPRINTK (PHASE_RESELECT, "scsi%d : detected reconnect request to different target.\n\tData bus = %d\n", hostno, temp);
  703. return (DID_BAD_INTR << 16);
  704. }
  705. if (!(temp & (1 << current_target))) {
  706. printk(KERN_WARNING "scsi%d : Unexpected reselect interrupt. Data bus = %d\n", hostno, temp);
  707. return (DID_BAD_INTR << 16);
  708. }
  709. buffer = current_buffer;
  710. cmnd = current_cmnd; /* WDE add */
  711. data = current_data; /* WDE add */
  712. len = current_bufflen; /* WDE add */
  713. nobuffs = current_nobuffs;
  714. /*
  715. * We have determined that we have been selected. At this
  716. * point, we must respond to the reselection by asserting
  717. * BSY ourselves
  718. */
  719. #if 1
  720. WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | CMD_BSY);
  721. #else
  722. WRITE_CONTROL (BASE_CMD | CMD_BSY);
  723. #endif
  724. /*
  725. * The target will drop SEL, and raise BSY, at which time
  726. * we must drop BSY.
  727. */
  728. ULOOP (100 * 1000) {
  729. if (!(STATUS & STAT_SEL))
  730. break;
  731. if (TIMEOUT) {
  732. WRITE_CONTROL (BASE_CMD | CMD_INTR);
  733. DPRINTK (PHASE_RESELECT, "scsi%d : RESELECT timed out while waiting for SEL.\n", hostno);
  734. return (DID_BAD_INTR << 16);
  735. }
  736. }
  737. WRITE_CONTROL (BASE_CMD);
  738. /*
  739. * At this point, we have connected with the target
  740. * and can get on with our lives.
  741. */
  742. break;
  743. case CAN_RECONNECT:
  744. #ifdef LINKED
  745. /*
  746. * This is a bletcherous hack, just as bad as the Unix #!
  747. * interpreter stuff. If it turns out we are using the wrong
  748. * I_T_L nexus, the easiest way to deal with it is to go into
  749. * our INFORMATION TRANSFER PHASE code, send a ABORT
  750. * message on MESSAGE OUT phase, and then loop back to here.
  751. */
  752. connect_loop:
  753. #endif
  754. DPRINTK (PHASE_BUS_FREE, "scsi%d : phase = BUS FREE \n", hostno);
  755. /*
  756. * BUS FREE PHASE
  757. *
  758. * On entry, we make sure that the BUS is in a BUS FREE
  759. * phase, by insuring that both BSY and SEL are low for
  760. * at least one bus settle delay. Several reads help
  761. * eliminate wire glitch.
  762. */
  763. #ifndef ARBITRATE
  764. #error FIXME: this is broken: we may not use jiffies here - we are under cli(). It will hardlock.
  765. clock = jiffies + ST0X_BUS_FREE_DELAY;
  766. while (((STATUS | STATUS | STATUS) & (STAT_BSY | STAT_SEL)) && (!st0x_aborted) && time_before (jiffies, clock))
  767. cpu_relax();
  768. if (time_after (jiffies, clock))
  769. return retcode (DID_BUS_BUSY);
  770. else if (st0x_aborted)
  771. return retcode (st0x_aborted);
  772. #endif
  773. DPRINTK (PHASE_SELECTION, "scsi%d : phase = SELECTION\n", hostno);
  774. clock = jiffies + ST0X_SELECTION_DELAY;
  775. /*
  776. * Arbitration/selection procedure :
  777. * 1. Disable drivers
  778. * 2. Write HOST adapter address bit
  779. * 3. Set start arbitration.
  780. * 4. We get either ARBITRATION COMPLETE or SELECT at this
  781. * point.
  782. * 5. OR our ID and targets on bus.
  783. * 6. Enable SCSI drivers and asserted SEL and ATTN
  784. */
  785. #ifdef ARBITRATE
  786. /* FIXME: verify host lock is always held here */
  787. WRITE_CONTROL(0);
  788. WRITE_DATA((controller_type == SEAGATE) ? 0x80 : 0x40);
  789. WRITE_CONTROL(CMD_START_ARB);
  790. ULOOP (ST0X_SELECTION_DELAY * 10000) {
  791. status_read = STATUS;
  792. if (status_read & STAT_ARB_CMPL)
  793. break;
  794. if (st0x_aborted) /* FIXME: What? We are going to do something even after abort? */
  795. break;
  796. if (TIMEOUT || (status_read & STAT_SEL)) {
  797. printk(KERN_WARNING "scsi%d : arbitration lost or timeout.\n", hostno);
  798. WRITE_CONTROL (BASE_CMD);
  799. return retcode (DID_NO_CONNECT);
  800. }
  801. }
  802. DPRINTK (PHASE_SELECTION, "scsi%d : arbitration complete\n", hostno);
  803. #endif
  804. /*
  805. * When the SCSI device decides that we're gawking at it,
  806. * it will respond by asserting BUSY on the bus.
  807. *
  808. * Note : the Seagate ST-01/02 product manual says that we
  809. * should twiddle the DATA register before the control
  810. * register. However, this does not work reliably so we do
  811. * it the other way around.
  812. *
  813. * Probably could be a problem with arbitration too, we
  814. * really should try this with a SCSI protocol or logic
  815. * analyzer to see what is going on.
  816. */
  817. tmp_data = (unsigned char) ((1 << target) | (controller_type == SEAGATE ? 0x80 : 0x40));
  818. tmp_control = BASE_CMD | CMD_DRVR_ENABLE | CMD_SEL | (reselect ? CMD_ATTN : 0);
  819. /* FIXME: verify host lock is always held here */
  820. #ifdef OLDCNTDATASCEME
  821. #ifdef SWAPCNTDATA
  822. WRITE_CONTROL (tmp_control);
  823. WRITE_DATA (tmp_data);
  824. #else
  825. WRITE_DATA (tmp_data);
  826. WRITE_CONTROL (tmp_control);
  827. #endif
  828. #else
  829. tmp_control ^= CMD_BSY; /* This is guesswork. What used to be in driver */
  830. WRITE_CONTROL (tmp_control); /* could never work: it sent data into control */
  831. WRITE_DATA (tmp_data); /* register and control info into data. Hopefully */
  832. tmp_control ^= CMD_BSY; /* fixed, but order of first two may be wrong. */
  833. WRITE_CONTROL (tmp_control); /* -- pavel@ucw.cz */
  834. #endif
  835. ULOOP (250 * 1000) {
  836. if (st0x_aborted) {
  837. /*
  838. * If we have been aborted, and we have a
  839. * command in progress, IE the target
  840. * still has BSY asserted, then we will
  841. * reset the bus, and notify the midlevel
  842. * driver to expect sense.
  843. */
  844. WRITE_CONTROL (BASE_CMD);
  845. if (STATUS & STAT_BSY) {
  846. printk(KERN_WARNING "scsi%d : BST asserted after we've been aborted.\n", hostno);
  847. seagate_st0x_bus_reset(NULL);
  848. return retcode (DID_RESET);
  849. }
  850. return retcode (st0x_aborted);
  851. }
  852. if (STATUS & STAT_BSY)
  853. break;
  854. if (TIMEOUT) {
  855. DPRINTK (PHASE_SELECTION, "scsi%d : NO CONNECT with target %d, stat = %x \n", hostno, target, STATUS);
  856. return retcode (DID_NO_CONNECT);
  857. }
  858. }
  859. /* Establish current pointers. Take into account scatter / gather */
  860. if ((nobuffs = SCint->use_sg)) {
  861. #if (DEBUG & DEBUG_SG)
  862. {
  863. int i;
  864. printk("scsi%d : scatter gather requested, using %d buffers.\n", hostno, nobuffs);
  865. for (i = 0; i < nobuffs; ++i)
  866. printk("scsi%d : buffer %d address = %p length = %d\n",
  867. hostno, i,
  868. page_address(buffer[i].page) + buffer[i].offset,
  869. buffer[i].length);
  870. }
  871. #endif
  872. buffer = (struct scatterlist *) SCint->buffer;
  873. len = buffer->length;
  874. data = page_address(buffer->page) + buffer->offset;
  875. } else {
  876. DPRINTK (DEBUG_SG, "scsi%d : scatter gather not requested.\n", hostno);
  877. buffer = NULL;
  878. len = SCint->request_bufflen;
  879. data = (unsigned char *) SCint->request_buffer;
  880. }
  881. DPRINTK (PHASE_DATAIN | PHASE_DATAOUT, "scsi%d : len = %d\n",
  882. hostno, len);
  883. break;
  884. #ifdef LINKED
  885. case LINKED_RIGHT:
  886. break;
  887. case LINKED_WRONG:
  888. break;
  889. #endif
  890. } /* end of switch(reselect) */
  891. /*
  892. * There are several conditions under which we wish to send a message :
  893. * 1. When we are allowing disconnect / reconnect, and need to
  894. * establish the I_T_L nexus via an IDENTIFY with the DiscPriv bit
  895. * set.
  896. *
  897. * 2. When we are doing linked commands, are have the wrong I_T_L
  898. * nexus established and want to send an ABORT message.
  899. */
  900. /* GCC does not like an ifdef inside a macro, so do it the hard way. */
  901. #ifdef LINKED
  902. WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT)|| (reselect == LINKED_WRONG))? CMD_ATTN : 0));
  903. #else
  904. WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE | (((reselect == CAN_RECONNECT))? CMD_ATTN : 0));
  905. #endif
  906. /*
  907. * INFORMATION TRANSFER PHASE
  908. *
  909. * The nasty looking read / write inline assembler loops we use for
  910. * DATAIN and DATAOUT phases are approximately 4-5 times as fast as
  911. * the 'C' versions - since we're moving 1024 bytes of data, this
  912. * really adds up.
  913. *
  914. * SJT: The nasty-looking assembler is gone, so it's slower.
  915. *
  916. */
  917. DPRINTK (PHASE_ETC, "scsi%d : phase = INFORMATION TRANSFER\n", hostno);
  918. incommand = 1;
  919. transfersize = SCint->transfersize;
  920. underflow = SCint->underflow;
  921. /*
  922. * Now, we poll the device for status information,
  923. * and handle any requests it makes. Note that since we are unsure
  924. * of how much data will be flowing across the system, etc and
  925. * cannot make reasonable timeouts, that we will instead have the
  926. * midlevel driver handle any timeouts that occur in this phase.
  927. */
  928. while (((status_read = STATUS) & STAT_BSY) && !st0x_aborted && !done) {
  929. #ifdef PARITY
  930. if (status_read & STAT_PARITY) {
  931. printk(KERN_ERR "scsi%d : got parity error\n", hostno);
  932. st0x_aborted = DID_PARITY;
  933. }
  934. #endif
  935. if (status_read & STAT_REQ) {
  936. #if ((DEBUG & PHASE_ETC) == PHASE_ETC)
  937. if ((newphase = (status_read & REQ_MASK)) != phase) {
  938. phase = newphase;
  939. switch (phase) {
  940. case REQ_DATAOUT:
  941. printk ("scsi%d : phase = DATA OUT\n", hostno);
  942. break;
  943. case REQ_DATAIN:
  944. printk ("scsi%d : phase = DATA IN\n", hostno);
  945. break;
  946. case REQ_CMDOUT:
  947. printk
  948. ("scsi%d : phase = COMMAND OUT\n", hostno);
  949. break;
  950. case REQ_STATIN:
  951. printk ("scsi%d : phase = STATUS IN\n", hostno);
  952. break;
  953. case REQ_MSGOUT:
  954. printk
  955. ("scsi%d : phase = MESSAGE OUT\n", hostno);
  956. break;
  957. case REQ_MSGIN:
  958. printk ("scsi%d : phase = MESSAGE IN\n", hostno);
  959. break;
  960. default:
  961. printk ("scsi%d : phase = UNKNOWN\n", hostno);
  962. st0x_aborted = DID_ERROR;
  963. }
  964. }
  965. #endif
  966. switch (status_read & REQ_MASK) {
  967. case REQ_DATAOUT:
  968. /*
  969. * If we are in fast mode, then we simply splat
  970. * the data out in word-sized chunks as fast as
  971. * we can.
  972. */
  973. if (!len) {
  974. #if 0
  975. printk("scsi%d: underflow to target %d lun %d \n", hostno, target, lun);
  976. st0x_aborted = DID_ERROR;
  977. fast = 0;
  978. #endif
  979. break;
  980. }
  981. if (fast && transfersize
  982. && !(len % transfersize)
  983. && (len >= transfersize)
  984. #ifdef FAST32
  985. && !(transfersize % 4)
  986. #endif
  987. ) {
  988. DPRINTK (DEBUG_FAST,
  989. "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
  990. " len = %d, data = %08x\n",
  991. hostno, SCint->underflow,
  992. SCint->transfersize, len,
  993. data);
  994. /* SJT: Start. Fast Write */
  995. #ifdef SEAGATE_USE_ASM
  996. __asm__ ("cld\n\t"
  997. #ifdef FAST32
  998. "shr $2, %%ecx\n\t"
  999. "1:\t"
  1000. "lodsl\n\t"
  1001. "movl %%eax, (%%edi)\n\t"
  1002. #else
  1003. "1:\t"
  1004. "lodsb\n\t"
  1005. "movb %%al, (%%edi)\n\t"
  1006. #endif
  1007. "loop 1b;"
  1008. /* output */ :
  1009. /* input */ :"D" (st0x_dr),
  1010. "S"
  1011. (data),
  1012. "c" (SCint->transfersize)
  1013. /* clobbered */
  1014. : "eax", "ecx",
  1015. "esi");
  1016. #else /* SEAGATE_USE_ASM */
  1017. memcpy_toio(st0x_dr, data, transfersize);
  1018. #endif /* SEAGATE_USE_ASM */
  1019. /* SJT: End */
  1020. len -= transfersize;
  1021. data += transfersize;
  1022. DPRINTK (DEBUG_FAST, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data);
  1023. } else {
  1024. /*
  1025. * We loop as long as we are in a
  1026. * data out phase, there is data to
  1027. * send, and BSY is still active.
  1028. */
  1029. /* SJT: Start. Slow Write. */
  1030. #ifdef SEAGATE_USE_ASM
  1031. int __dummy_1, __dummy_2;
  1032. /*
  1033. * We loop as long as we are in a data out phase, there is data to send,
  1034. * and BSY is still active.
  1035. */
  1036. /* Local variables : len = ecx , data = esi,
  1037. st0x_cr_sr = ebx, st0x_dr = edi
  1038. */
  1039. __asm__ (
  1040. /* Test for any data here at all. */
  1041. "orl %%ecx, %%ecx\n\t"
  1042. "jz 2f\n\t" "cld\n\t"
  1043. /* "movl st0x_cr_sr, %%ebx\n\t" */
  1044. /* "movl st0x_dr, %%edi\n\t" */
  1045. "1:\t"
  1046. "movb (%%ebx), %%al\n\t"
  1047. /* Test for BSY */
  1048. "test $1, %%al\n\t"
  1049. "jz 2f\n\t"
  1050. /* Test for data out phase - STATUS & REQ_MASK should be
  1051. REQ_DATAOUT, which is 0. */
  1052. "test $0xe, %%al\n\t"
  1053. "jnz 2f\n\t"
  1054. /* Test for REQ */
  1055. "test $0x10, %%al\n\t"
  1056. "jz 1b\n\t"
  1057. "lodsb\n\t"
  1058. "movb %%al, (%%edi)\n\t"
  1059. "loop 1b\n\t" "2:\n"
  1060. /* output */ :"=S" (data), "=c" (len),
  1061. "=b"
  1062. (__dummy_1),
  1063. "=D" (__dummy_2)
  1064. /* input */
  1065. : "0" (data), "1" (len),
  1066. "2" (st0x_cr_sr),
  1067. "3" (st0x_dr)
  1068. /* clobbered */
  1069. : "eax");
  1070. #else /* SEAGATE_USE_ASM */
  1071. while (len) {
  1072. unsigned char stat;
  1073. stat = STATUS;
  1074. if (!(stat & STAT_BSY)
  1075. || ((stat & REQ_MASK) !=
  1076. REQ_DATAOUT))
  1077. break;
  1078. if (stat & STAT_REQ) {
  1079. WRITE_DATA (*data++);
  1080. --len;
  1081. }
  1082. }
  1083. #endif /* SEAGATE_USE_ASM */
  1084. /* SJT: End. */
  1085. }
  1086. if (!len && nobuffs) {
  1087. --nobuffs;
  1088. ++buffer;
  1089. len = buffer->length;
  1090. data = page_address(buffer->page) + buffer->offset;
  1091. DPRINTK (DEBUG_SG,
  1092. "scsi%d : next scatter-gather buffer len = %d address = %08x\n",
  1093. hostno, len, data);
  1094. }
  1095. break;
  1096. case REQ_DATAIN:
  1097. #ifdef SLOW_RATE
  1098. if (borken) {
  1099. #if (DEBUG & (PHASE_DATAIN))
  1100. transfered += len;
  1101. #endif
  1102. for (; len && (STATUS & (REQ_MASK | STAT_REQ)) == (REQ_DATAIN | STAT_REQ); --len) {
  1103. *data++ = DATA;
  1104. borken_wait();
  1105. }
  1106. #if (DEBUG & (PHASE_DATAIN))
  1107. transfered -= len;
  1108. #endif
  1109. } else
  1110. #endif
  1111. if (fast && transfersize
  1112. && !(len % transfersize)
  1113. && (len >= transfersize)
  1114. #ifdef FAST32
  1115. && !(transfersize % 4)
  1116. #endif
  1117. ) {
  1118. DPRINTK (DEBUG_FAST,
  1119. "scsi%d : FAST transfer, underflow = %d, transfersize = %d\n"
  1120. " len = %d, data = %08x\n",
  1121. hostno, SCint->underflow,
  1122. SCint->transfersize, len,
  1123. data);
  1124. /* SJT: Start. Fast Read */
  1125. #ifdef SEAGATE_USE_ASM
  1126. __asm__ ("cld\n\t"
  1127. #ifdef FAST32
  1128. "shr $2, %%ecx\n\t"
  1129. "1:\t"
  1130. "movl (%%esi), %%eax\n\t"
  1131. "stosl\n\t"
  1132. #else
  1133. "1:\t"
  1134. "movb (%%esi), %%al\n\t"
  1135. "stosb\n\t"
  1136. #endif
  1137. "loop 1b\n\t"
  1138. /* output */ :
  1139. /* input */ :"S" (st0x_dr),
  1140. "D"
  1141. (data),
  1142. "c" (SCint->transfersize)
  1143. /* clobbered */
  1144. : "eax", "ecx",
  1145. "edi");
  1146. #else /* SEAGATE_USE_ASM */
  1147. memcpy_fromio(data, st0x_dr, len);
  1148. #endif /* SEAGATE_USE_ASM */
  1149. /* SJT: End */
  1150. len -= transfersize;
  1151. data += transfersize;
  1152. #if (DEBUG & PHASE_DATAIN)
  1153. printk ("scsi%d: transfered += %d\n", hostno, transfersize);
  1154. transfered += transfersize;
  1155. #endif
  1156. DPRINTK (DEBUG_FAST, "scsi%d : FAST transfer complete len = %d data = %08x\n", hostno, len, data);
  1157. } else {
  1158. #if (DEBUG & PHASE_DATAIN)
  1159. printk ("scsi%d: transfered += %d\n", hostno, len);
  1160. transfered += len; /* Assume we'll transfer it all, then
  1161. subtract what we *didn't* transfer */
  1162. #endif
  1163. /*
  1164. * We loop as long as we are in a data in phase, there is room to read,
  1165. * and BSY is still active
  1166. */
  1167. /* SJT: Start. */
  1168. #ifdef SEAGATE_USE_ASM
  1169. int __dummy_3, __dummy_4;
  1170. /* Dummy clobbering variables for the new gcc-2.95 */
  1171. /*
  1172. * We loop as long as we are in a data in phase, there is room to read,
  1173. * and BSY is still active
  1174. */
  1175. /* Local variables : ecx = len, edi = data
  1176. esi = st0x_cr_sr, ebx = st0x_dr */
  1177. __asm__ (
  1178. /* Test for room to read */
  1179. "orl %%ecx, %%ecx\n\t"
  1180. "jz 2f\n\t" "cld\n\t"
  1181. /* "movl st0x_cr_sr, %%esi\n\t" */
  1182. /* "movl st0x_dr, %%ebx\n\t" */
  1183. "1:\t"
  1184. "movb (%%esi), %%al\n\t"
  1185. /* Test for BSY */
  1186. "test $1, %%al\n\t"
  1187. "jz 2f\n\t"
  1188. /* Test for data in phase - STATUS & REQ_MASK should be REQ_DATAIN,
  1189. = STAT_IO, which is 4. */
  1190. "movb $0xe, %%ah\n\t"
  1191. "andb %%al, %%ah\n\t"
  1192. "cmpb $0x04, %%ah\n\t"
  1193. "jne 2f\n\t"
  1194. /* Test for REQ */
  1195. "test $0x10, %%al\n\t"
  1196. "jz 1b\n\t"
  1197. "movb (%%ebx), %%al\n\t"
  1198. "stosb\n\t"
  1199. "loop 1b\n\t" "2:\n"
  1200. /* output */ :"=D" (data), "=c" (len),
  1201. "=S"
  1202. (__dummy_3),
  1203. "=b" (__dummy_4)
  1204. /* input */
  1205. : "0" (data), "1" (len),
  1206. "2" (st0x_cr_sr),
  1207. "3" (st0x_dr)
  1208. /* clobbered */
  1209. : "eax");
  1210. #else /* SEAGATE_USE_ASM */
  1211. while (len) {
  1212. unsigned char stat;
  1213. stat = STATUS;
  1214. if (!(stat & STAT_BSY)
  1215. || ((stat & REQ_MASK) !=
  1216. REQ_DATAIN))
  1217. break;
  1218. if (stat & STAT_REQ) {
  1219. *data++ = DATA;
  1220. --len;
  1221. }
  1222. }
  1223. #endif /* SEAGATE_USE_ASM */
  1224. /* SJT: End. */
  1225. #if (DEBUG & PHASE_DATAIN)
  1226. printk ("scsi%d: transfered -= %d\n", hostno, len);
  1227. transfered -= len; /* Since we assumed all of Len got *
  1228. transfered, correct our mistake */
  1229. #endif
  1230. }
  1231. if (!len && nobuffs) {
  1232. --nobuffs;
  1233. ++buffer;
  1234. len = buffer->length;
  1235. data = page_address(buffer->page) + buffer->offset;
  1236. DPRINTK (DEBUG_SG, "scsi%d : next scatter-gather buffer len = %d address = %08x\n", hostno, len, data);
  1237. }
  1238. break;
  1239. case REQ_CMDOUT:
  1240. while (((status_read = STATUS) & STAT_BSY) &&
  1241. ((status_read & REQ_MASK) == REQ_CMDOUT))
  1242. if (status_read & STAT_REQ) {
  1243. WRITE_DATA (*(const unsigned char *) cmnd);
  1244. cmnd = 1 + (const unsigned char *)cmnd;
  1245. #ifdef SLOW_RATE
  1246. if (borken)
  1247. borken_wait ();
  1248. #endif
  1249. }
  1250. break;
  1251. case REQ_STATIN:
  1252. status = DATA;
  1253. break;
  1254. case REQ_MSGOUT:
  1255. /*
  1256. * We can only have sent a MSG OUT if we
  1257. * requested to do this by raising ATTN.
  1258. * So, we must drop ATTN.
  1259. */
  1260. WRITE_CONTROL (BASE_CMD | CMD_DRVR_ENABLE);
  1261. /*
  1262. * If we are reconnecting, then we must
  1263. * send an IDENTIFY message in response
  1264. * to MSGOUT.
  1265. */
  1266. switch (reselect) {
  1267. case CAN_RECONNECT:
  1268. WRITE_DATA (IDENTIFY (1, lun));
  1269. DPRINTK (PHASE_RESELECT | PHASE_MSGOUT, "scsi%d : sent IDENTIFY message.\n", hostno);
  1270. break;
  1271. #ifdef LINKED
  1272. case LINKED_WRONG:
  1273. WRITE_DATA (ABORT);
  1274. linked_connected = 0;
  1275. reselect = CAN_RECONNECT;
  1276. goto connect_loop;
  1277. DPRINTK (PHASE_MSGOUT | DEBUG_LINKED, "scsi%d : sent ABORT message to cancel incorrect I_T_L nexus.\n", hostno);
  1278. #endif /* LINKED */
  1279. DPRINTK (DEBUG_LINKED, "correct\n");
  1280. default:
  1281. WRITE_DATA (NOP);
  1282. printk("scsi%d : target %d requested MSGOUT, sent NOP message.\n", hostno, target);
  1283. }
  1284. break;
  1285. case REQ_MSGIN:
  1286. switch (message = DATA) {
  1287. case DISCONNECT:
  1288. DANY("seagate: deciding to disconnect\n");
  1289. should_reconnect = 1;
  1290. current_data = data; /* WDE add */
  1291. current_buffer = buffer;
  1292. current_bufflen = len; /* WDE add */
  1293. current_nobuffs = nobuffs;
  1294. #ifdef LINKED
  1295. linked_connected = 0;
  1296. #endif
  1297. done = 1;
  1298. DPRINTK ((PHASE_RESELECT | PHASE_MSGIN), "scsi%d : disconnected.\n", hostno);
  1299. break;
  1300. #ifdef LINKED
  1301. case LINKED_CMD_COMPLETE:
  1302. case LINKED_FLG_CMD_COMPLETE:
  1303. #endif
  1304. case COMMAND_COMPLETE:
  1305. /*
  1306. * Note : we should check for underflow here.
  1307. */
  1308. DPRINTK(PHASE_MSGIN, "scsi%d : command complete.\n", hostno);
  1309. done = 1;
  1310. break;
  1311. case ABORT:
  1312. DPRINTK(PHASE_MSGIN, "scsi%d : abort message.\n", hostno);
  1313. done = 1;
  1314. break;
  1315. case SAVE_POINTERS:
  1316. current_buffer = buffer;
  1317. current_bufflen = len; /* WDE add */
  1318. current_data = data; /* WDE mod */
  1319. current_nobuffs = nobuffs;
  1320. DPRINTK (PHASE_MSGIN, "scsi%d : pointers saved.\n", hostno);
  1321. break;
  1322. case RESTORE_POINTERS:
  1323. buffer = current_buffer;
  1324. cmnd = current_cmnd;
  1325. data = current_data; /* WDE mod */
  1326. len = current_bufflen;
  1327. nobuffs = current_nobuffs;
  1328. DPRINTK(PHASE_MSGIN, "scsi%d : pointers restored.\n", hostno);
  1329. break;
  1330. default:
  1331. /*
  1332. * IDENTIFY distinguishes itself
  1333. * from the other messages by
  1334. * setting the high bit.
  1335. *
  1336. * Note : we need to handle at
  1337. * least one outstanding command
  1338. * per LUN, and need to hash the
  1339. * SCSI command for that I_T_L
  1340. * nexus based on the known ID
  1341. * (at this point) and LUN.
  1342. */
  1343. if (message & 0x80) {
  1344. DPRINTK (PHASE_MSGIN, "scsi%d : IDENTIFY message received from id %d, lun %d.\n", hostno, target, message & 7);
  1345. } else {
  1346. /*
  1347. * We should go into a
  1348. * MESSAGE OUT phase, and
  1349. * send a MESSAGE_REJECT
  1350. * if we run into a message
  1351. * that we don't like. The
  1352. * seagate driver needs
  1353. * some serious
  1354. * restructuring first
  1355. * though.
  1356. */
  1357. DPRINTK (PHASE_MSGIN, "scsi%d : unknown message %d from target %d.\n", hostno, message, target);
  1358. }
  1359. }
  1360. break;
  1361. default:
  1362. printk(KERN_ERR "scsi%d : unknown phase.\n", hostno);
  1363. st0x_aborted = DID_ERROR;
  1364. } /* end of switch (status_read & REQ_MASK) */
  1365. #ifdef SLOW_RATE
  1366. /*
  1367. * I really don't care to deal with borken devices in
  1368. * each single byte transfer case (ie, message in,
  1369. * message out, status), so I'll do the wait here if
  1370. * necessary.
  1371. */
  1372. if(borken)
  1373. borken_wait();
  1374. #endif
  1375. } /* if(status_read & STAT_REQ) ends */
  1376. } /* while(((status_read = STATUS)...) ends */
  1377. DPRINTK(PHASE_DATAIN | PHASE_DATAOUT | PHASE_EXIT, "scsi%d : Transfered %d bytes\n", hostno, transfered);
  1378. #if (DEBUG & PHASE_EXIT)
  1379. #if 0 /* Doesn't work for scatter/gather */
  1380. printk("Buffer : \n");
  1381. for(i = 0; i < 20; ++i)
  1382. printk("%02x ", ((unsigned char *) data)[i]); /* WDE mod */
  1383. printk("\n");
  1384. #endif
  1385. printk("scsi%d : status = ", hostno);
  1386. scsi_print_status(status);
  1387. printk(" message = %02x\n", message);
  1388. #endif
  1389. /* We shouldn't reach this until *after* BSY has been deasserted */
  1390. #ifdef LINKED
  1391. else
  1392. {
  1393. /*
  1394. * Fix the message byte so that unsuspecting high level drivers
  1395. * don't puke when they see a LINKED COMMAND message in place of
  1396. * the COMMAND COMPLETE they may be expecting. Shouldn't be
  1397. * necessary, but it's better to be on the safe side.
  1398. *
  1399. * A non LINKED* message byte will indicate that the command
  1400. * completed, and we are now disconnected.
  1401. */
  1402. switch (message) {
  1403. case LINKED_CMD_COMPLETE:
  1404. case LINKED_FLG_CMD_COMPLETE:
  1405. message = COMMAND_COMPLETE;
  1406. linked_target = current_target;
  1407. linked_lun = current_lun;
  1408. linked_connected = 1;
  1409. DPRINTK (DEBUG_LINKED, "scsi%d : keeping I_T_L nexus established for linked command.\n", hostno);
  1410. /* We also will need to adjust status to accommodate intermediate
  1411. conditions. */
  1412. if ((status == INTERMEDIATE_GOOD) || (status == INTERMEDIATE_C_GOOD))
  1413. status = GOOD;
  1414. break;
  1415. /*
  1416. * We should also handle what are "normal" termination
  1417. * messages here (ABORT, BUS_DEVICE_RESET?, and
  1418. * COMMAND_COMPLETE individually, and flake if things
  1419. * aren't right.
  1420. */
  1421. default:
  1422. DPRINTK (DEBUG_LINKED, "scsi%d : closing I_T_L nexus.\n", hostno);
  1423. linked_connected = 0;
  1424. }
  1425. }
  1426. #endif /* LINKED */
  1427. if (should_reconnect) {
  1428. DPRINTK (PHASE_RESELECT, "scsi%d : exiting seagate_st0x_queue_command() with reconnect enabled.\n", hostno);
  1429. WRITE_CONTROL (BASE_CMD | CMD_INTR);
  1430. } else
  1431. WRITE_CONTROL (BASE_CMD);
  1432. return retcode (st0x_aborted);
  1433. } /* end of internal_command */
  1434. static int seagate_st0x_abort (Scsi_Cmnd * SCpnt)
  1435. {
  1436. st0x_aborted = DID_ABORT;
  1437. return SUCCESS;
  1438. }
  1439. #undef ULOOP
  1440. #undef TIMEOUT
  1441. /*
  1442. * the seagate_st0x_reset function resets the SCSI bus
  1443. *
  1444. * May be called with SCpnt = NULL
  1445. */
  1446. static int seagate_st0x_bus_reset(Scsi_Cmnd * SCpnt)
  1447. {
  1448. /* No timeouts - this command is going to fail because it was reset. */
  1449. DANY ("scsi%d: Reseting bus... ", hostno);
  1450. /* assert RESET signal on SCSI bus. */
  1451. WRITE_CONTROL (BASE_CMD | CMD_RST);
  1452. udelay (20 * 1000);
  1453. WRITE_CONTROL (BASE_CMD);
  1454. st0x_aborted = DID_RESET;
  1455. DANY ("done.\n");
  1456. return SUCCESS;
  1457. }
  1458. static int seagate_st0x_release(struct Scsi_Host *shost)
  1459. {
  1460. if (shost->irq)
  1461. free_irq(shost->irq, shost);
  1462. release_region(shost->io_port, shost->n_io_port);
  1463. return 0;
  1464. }
  1465. static Scsi_Host_Template driver_template = {
  1466. .detect = seagate_st0x_detect,
  1467. .release = seagate_st0x_release,
  1468. .info = seagate_st0x_info,
  1469. .queuecommand = seagate_st0x_queue_command,
  1470. .eh_abort_handler = seagate_st0x_abort,
  1471. .eh_bus_reset_handler = seagate_st0x_bus_reset,
  1472. .can_queue = 1,
  1473. .this_id = 7,
  1474. .sg_tablesize = SG_ALL,
  1475. .cmd_per_lun = 1,
  1476. .use_clustering = DISABLE_CLUSTERING,
  1477. };
  1478. #include "scsi_module.c"