seagate.c 46 KB

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