aha1542.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. /* $Id: aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $
  2. * linux/kernel/aha1542.c
  3. *
  4. * Copyright (C) 1992 Tommy Thorn
  5. * Copyright (C) 1993, 1994, 1995 Eric Youngdale
  6. *
  7. * Modified by Eric Youngdale
  8. * Use request_irq and request_dma to help prevent unexpected conflicts
  9. * Set up on-board DMA controller, such that we do not have to
  10. * have the bios enabled to use the aha1542.
  11. * Modified by David Gentzel
  12. * Don't call request_dma if dma mask is 0 (for BusLogic BT-445S VL-Bus
  13. * controller).
  14. * Modified by Matti Aarnio
  15. * Accept parameters from LILO cmd-line. -- 1-Oct-94
  16. * Modified by Mike McLagan <mike.mclagan@linux.org>
  17. * Recognise extended mode on AHA1542CP, different bit than 1542CF
  18. * 1-Jan-97
  19. * Modified by Bjorn L. Thordarson and Einar Thor Einarsson
  20. * Recognize that DMA0 is valid DMA channel -- 13-Jul-98
  21. * Modified by Chris Faulhaber <jedgar@fxp.org>
  22. * Added module command-line options
  23. * 19-Jul-99
  24. * Modified by Adam Fritzler
  25. * Added proper detection of the AHA-1640 (MCA, now deleted)
  26. */
  27. #include <linux/module.h>
  28. #include <linux/interrupt.h>
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/string.h>
  32. #include <linux/ioport.h>
  33. #include <linux/delay.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/init.h>
  36. #include <linux/spinlock.h>
  37. #include <linux/isapnp.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/slab.h>
  40. #include <asm/dma.h>
  41. #include <asm/io.h>
  42. #include "scsi.h"
  43. #include <scsi/scsi_host.h>
  44. #include "aha1542.h"
  45. #define SCSI_BUF_PA(address) isa_virt_to_bus(address)
  46. #define SCSI_SG_PA(sgent) (isa_page_to_bus(sg_page((sgent))) + (sgent)->offset)
  47. #include <linux/stat.h>
  48. #ifdef DEBUG
  49. #define DEB(x) x
  50. #else
  51. #define DEB(x)
  52. #endif
  53. /*
  54. static const char RCSid[] = "$Header: /usr/src/linux/kernel/blk_drv/scsi/RCS/aha1542.c,v 1.1 1992/07/24 06:27:38 root Exp root $";
  55. */
  56. /* The adaptec can be configured for quite a number of addresses, but
  57. I generally do not want the card poking around at random. We allow
  58. two addresses - this allows people to use the Adaptec with a Midi
  59. card, which also used 0x330 -- can be overridden with LILO! */
  60. #define MAXBOARDS 4 /* Increase this and the sizes of the
  61. arrays below, if you need more.. */
  62. /* Boards 3,4 slots are reserved for ISAPnP scans */
  63. static unsigned int bases[MAXBOARDS] __initdata = {0x330, 0x334, 0, 0};
  64. /* set by aha1542_setup according to the command line; they also may
  65. be marked __initdata, but require zero initializers then */
  66. static int setup_called[MAXBOARDS];
  67. static int setup_buson[MAXBOARDS];
  68. static int setup_busoff[MAXBOARDS];
  69. static int setup_dmaspeed[MAXBOARDS] __initdata = { -1, -1, -1, -1 };
  70. /*
  71. * LILO/Module params: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]
  72. *
  73. * Where: <PORTBASE> is any of the valid AHA addresses:
  74. * 0x130, 0x134, 0x230, 0x234, 0x330, 0x334
  75. * <BUSON> is the time (in microsecs) that AHA spends on the AT-bus
  76. * when transferring data. 1542A power-on default is 11us,
  77. * valid values are in range: 2..15 (decimal)
  78. * <BUSOFF> is the time that AHA spends OFF THE BUS after while
  79. * it is transferring data (not to monopolize the bus).
  80. * Power-on default is 4us, valid range: 1..64 microseconds.
  81. * <DMASPEED> Default is jumper selected (1542A: on the J1),
  82. * but experimenter can alter it with this.
  83. * Valid values: 5, 6, 7, 8, 10 (MB/s)
  84. * Factory default is 5 MB/s.
  85. */
  86. #if defined(MODULE)
  87. static bool isapnp = 0;
  88. static int aha1542[] = {0x330, 11, 4, -1};
  89. module_param_array(aha1542, int, NULL, 0);
  90. module_param(isapnp, bool, 0);
  91. static struct isapnp_device_id id_table[] __initdata = {
  92. {
  93. ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  94. ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1542),
  95. 0
  96. },
  97. {0}
  98. };
  99. MODULE_DEVICE_TABLE(isapnp, id_table);
  100. #else
  101. static int isapnp = 1;
  102. #endif
  103. #define BIOS_TRANSLATION_1632 0 /* Used by some old 1542A boards */
  104. #define BIOS_TRANSLATION_6432 1 /* Default case these days */
  105. #define BIOS_TRANSLATION_25563 2 /* Big disk case */
  106. struct aha1542_hostdata {
  107. /* This will effectively start both of them at the first mailbox */
  108. int bios_translation; /* Mapping bios uses - for compatibility */
  109. int aha1542_last_mbi_used;
  110. int aha1542_last_mbo_used;
  111. Scsi_Cmnd *SCint[AHA1542_MAILBOXES];
  112. struct mailbox mb[2 * AHA1542_MAILBOXES];
  113. struct ccb ccb[AHA1542_MAILBOXES];
  114. };
  115. #define HOSTDATA(host) ((struct aha1542_hostdata *) &host->hostdata)
  116. static DEFINE_SPINLOCK(aha1542_lock);
  117. #define WAITnexttimeout 3000000
  118. static void setup_mailboxes(int base_io, struct Scsi_Host *shpnt);
  119. static int aha1542_restart(struct Scsi_Host *shost);
  120. static void aha1542_intr_handle(struct Scsi_Host *shost);
  121. #define aha1542_intr_reset(base) outb(IRST, CONTROL(base))
  122. #define WAIT(port, mask, allof, noneof) \
  123. { register int WAITbits; \
  124. register int WAITtimeout = WAITnexttimeout; \
  125. while (1) { \
  126. WAITbits = inb(port) & (mask); \
  127. if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
  128. break; \
  129. if (--WAITtimeout == 0) goto fail; \
  130. } \
  131. }
  132. /* Similar to WAIT, except we use the udelay call to regulate the
  133. amount of time we wait. */
  134. #define WAITd(port, mask, allof, noneof, timeout) \
  135. { register int WAITbits; \
  136. register int WAITtimeout = timeout; \
  137. while (1) { \
  138. WAITbits = inb(port) & (mask); \
  139. if ((WAITbits & (allof)) == (allof) && ((WAITbits & (noneof)) == 0)) \
  140. break; \
  141. mdelay(1); \
  142. if (--WAITtimeout == 0) goto fail; \
  143. } \
  144. }
  145. static void aha1542_stat(void)
  146. {
  147. /* int s = inb(STATUS), i = inb(INTRFLAGS);
  148. printk("status=%x intrflags=%x\n", s, i, WAITnexttimeout-WAITtimeout); */
  149. }
  150. /* This is a bit complicated, but we need to make sure that an interrupt
  151. routine does not send something out while we are in the middle of this.
  152. Fortunately, it is only at boot time that multi-byte messages
  153. are ever sent. */
  154. static int aha1542_out(unsigned int base, unchar * cmdp, int len)
  155. {
  156. unsigned long flags = 0;
  157. int got_lock;
  158. if (len == 1) {
  159. got_lock = 0;
  160. while (1 == 1) {
  161. WAIT(STATUS(base), CDF, 0, CDF);
  162. spin_lock_irqsave(&aha1542_lock, flags);
  163. if (inb(STATUS(base)) & CDF) {
  164. spin_unlock_irqrestore(&aha1542_lock, flags);
  165. continue;
  166. }
  167. outb(*cmdp, DATA(base));
  168. spin_unlock_irqrestore(&aha1542_lock, flags);
  169. return 0;
  170. }
  171. } else {
  172. spin_lock_irqsave(&aha1542_lock, flags);
  173. got_lock = 1;
  174. while (len--) {
  175. WAIT(STATUS(base), CDF, 0, CDF);
  176. outb(*cmdp++, DATA(base));
  177. }
  178. spin_unlock_irqrestore(&aha1542_lock, flags);
  179. }
  180. return 0;
  181. fail:
  182. if (got_lock)
  183. spin_unlock_irqrestore(&aha1542_lock, flags);
  184. printk(KERN_ERR "aha1542_out failed(%d): ", len + 1);
  185. aha1542_stat();
  186. return 1;
  187. }
  188. /* Only used at boot time, so we do not need to worry about latency as much
  189. here */
  190. static int __init aha1542_in(unsigned int base, unchar * cmdp, int len)
  191. {
  192. unsigned long flags;
  193. spin_lock_irqsave(&aha1542_lock, flags);
  194. while (len--) {
  195. WAIT(STATUS(base), DF, DF, 0);
  196. *cmdp++ = inb(DATA(base));
  197. }
  198. spin_unlock_irqrestore(&aha1542_lock, flags);
  199. return 0;
  200. fail:
  201. spin_unlock_irqrestore(&aha1542_lock, flags);
  202. printk(KERN_ERR "aha1542_in failed(%d): ", len + 1);
  203. aha1542_stat();
  204. return 1;
  205. }
  206. /* Similar to aha1542_in, except that we wait a very short period of time.
  207. We use this if we know the board is alive and awake, but we are not sure
  208. if the board will respond to the command we are about to send or not */
  209. static int __init aha1542_in1(unsigned int base, unchar * cmdp, int len)
  210. {
  211. unsigned long flags;
  212. spin_lock_irqsave(&aha1542_lock, flags);
  213. while (len--) {
  214. WAITd(STATUS(base), DF, DF, 0, 100);
  215. *cmdp++ = inb(DATA(base));
  216. }
  217. spin_unlock_irqrestore(&aha1542_lock, flags);
  218. return 0;
  219. fail:
  220. spin_unlock_irqrestore(&aha1542_lock, flags);
  221. return 1;
  222. }
  223. static int makecode(unsigned hosterr, unsigned scsierr)
  224. {
  225. switch (hosterr) {
  226. case 0x0:
  227. case 0xa: /* Linked command complete without error and linked normally */
  228. case 0xb: /* Linked command complete without error, interrupt generated */
  229. hosterr = 0;
  230. break;
  231. case 0x11: /* Selection time out-The initiator selection or target
  232. reselection was not complete within the SCSI Time out period */
  233. hosterr = DID_TIME_OUT;
  234. break;
  235. case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
  236. than was allocated by the Data Length field or the sum of the
  237. Scatter / Gather Data Length fields. */
  238. case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
  239. case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
  240. invalid. This usually indicates a software failure. */
  241. case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
  242. This usually indicates a software failure. */
  243. case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
  244. of linked CCB's does not specify the same logical unit number as
  245. the first. */
  246. case 0x18: /* Invalid Target Direction received from Host-The direction of a
  247. Target Mode CCB was invalid. */
  248. case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
  249. received to service data transfer between the same target LUN
  250. and initiator SCSI ID in the same direction. */
  251. case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
  252. length segment or invalid segment list boundaries was received.
  253. A CCB parameter was invalid. */
  254. DEB(printk("Aha1542: %x %x\n", hosterr, scsierr));
  255. hosterr = DID_ERROR; /* Couldn't find any better */
  256. break;
  257. case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
  258. phase sequence was requested by the target. The host adapter
  259. will generate a SCSI Reset Condition, notifying the host with
  260. a SCRD interrupt */
  261. hosterr = DID_RESET;
  262. break;
  263. default:
  264. printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
  265. break;
  266. }
  267. return scsierr | (hosterr << 16);
  268. }
  269. static int __init aha1542_test_port(int bse, struct Scsi_Host *shpnt)
  270. {
  271. unchar inquiry_cmd[] = {CMD_INQUIRY};
  272. unchar inquiry_result[4];
  273. unchar *cmdp;
  274. int len;
  275. volatile int debug = 0;
  276. /* Quick and dirty test for presence of the card. */
  277. if (inb(STATUS(bse)) == 0xff)
  278. return 0;
  279. /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
  280. /* DEB(printk("aha1542_test_port called \n")); */
  281. /* In case some other card was probing here, reset interrupts */
  282. aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
  283. outb(SRST | IRST /*|SCRST */ , CONTROL(bse));
  284. mdelay(20); /* Wait a little bit for things to settle down. */
  285. debug = 1;
  286. /* Expect INIT and IDLE, any of the others are bad */
  287. WAIT(STATUS(bse), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
  288. debug = 2;
  289. /* Shouldn't have generated any interrupts during reset */
  290. if (inb(INTRFLAGS(bse)) & INTRMASK)
  291. goto fail;
  292. /* Perform a host adapter inquiry instead so we do not need to set
  293. up the mailboxes ahead of time */
  294. aha1542_out(bse, inquiry_cmd, 1);
  295. debug = 3;
  296. len = 4;
  297. cmdp = &inquiry_result[0];
  298. while (len--) {
  299. WAIT(STATUS(bse), DF, DF, 0);
  300. *cmdp++ = inb(DATA(bse));
  301. }
  302. debug = 8;
  303. /* Reading port should reset DF */
  304. if (inb(STATUS(bse)) & DF)
  305. goto fail;
  306. debug = 9;
  307. /* When HACC, command is completed, and we're though testing */
  308. WAIT(INTRFLAGS(bse), HACC, HACC, 0);
  309. /* now initialize adapter */
  310. debug = 10;
  311. /* Clear interrupts */
  312. outb(IRST, CONTROL(bse));
  313. debug = 11;
  314. return debug; /* 1 = ok */
  315. fail:
  316. return 0; /* 0 = not ok */
  317. }
  318. /* A quick wrapper for do_aha1542_intr_handle to grab the spin lock */
  319. static irqreturn_t do_aha1542_intr_handle(int dummy, void *dev_id)
  320. {
  321. unsigned long flags;
  322. struct Scsi_Host *shost = dev_id;
  323. spin_lock_irqsave(shost->host_lock, flags);
  324. aha1542_intr_handle(shost);
  325. spin_unlock_irqrestore(shost->host_lock, flags);
  326. return IRQ_HANDLED;
  327. }
  328. /* A "high" level interrupt handler */
  329. static void aha1542_intr_handle(struct Scsi_Host *shost)
  330. {
  331. void (*my_done) (Scsi_Cmnd *) = NULL;
  332. int errstatus, mbi, mbo, mbistatus;
  333. int number_serviced;
  334. unsigned long flags;
  335. Scsi_Cmnd *SCtmp;
  336. int flag;
  337. int needs_restart;
  338. struct mailbox *mb;
  339. struct ccb *ccb;
  340. mb = HOSTDATA(shost)->mb;
  341. ccb = HOSTDATA(shost)->ccb;
  342. #ifdef DEBUG
  343. {
  344. flag = inb(INTRFLAGS(shost->io_port));
  345. printk(KERN_DEBUG "aha1542_intr_handle: ");
  346. if (!(flag & ANYINTR))
  347. printk("no interrupt?");
  348. if (flag & MBIF)
  349. printk("MBIF ");
  350. if (flag & MBOA)
  351. printk("MBOF ");
  352. if (flag & HACC)
  353. printk("HACC ");
  354. if (flag & SCRD)
  355. printk("SCRD ");
  356. printk("status %02x\n", inb(STATUS(shost->io_port)));
  357. };
  358. #endif
  359. number_serviced = 0;
  360. needs_restart = 0;
  361. while (1 == 1) {
  362. flag = inb(INTRFLAGS(shost->io_port));
  363. /* Check for unusual interrupts. If any of these happen, we should
  364. probably do something special, but for now just printing a message
  365. is sufficient. A SCSI reset detected is something that we really
  366. need to deal with in some way. */
  367. if (flag & ~MBIF) {
  368. if (flag & MBOA)
  369. printk("MBOF ");
  370. if (flag & HACC)
  371. printk("HACC ");
  372. if (flag & SCRD) {
  373. needs_restart = 1;
  374. printk("SCRD ");
  375. }
  376. }
  377. aha1542_intr_reset(shost->io_port);
  378. spin_lock_irqsave(&aha1542_lock, flags);
  379. mbi = HOSTDATA(shost)->aha1542_last_mbi_used + 1;
  380. if (mbi >= 2 * AHA1542_MAILBOXES)
  381. mbi = AHA1542_MAILBOXES;
  382. do {
  383. if (mb[mbi].status != 0)
  384. break;
  385. mbi++;
  386. if (mbi >= 2 * AHA1542_MAILBOXES)
  387. mbi = AHA1542_MAILBOXES;
  388. } while (mbi != HOSTDATA(shost)->aha1542_last_mbi_used);
  389. if (mb[mbi].status == 0) {
  390. spin_unlock_irqrestore(&aha1542_lock, flags);
  391. /* Hmm, no mail. Must have read it the last time around */
  392. if (!number_serviced && !needs_restart)
  393. printk(KERN_WARNING "aha1542.c: interrupt received, but no mail.\n");
  394. /* We detected a reset. Restart all pending commands for
  395. devices that use the hard reset option */
  396. if (needs_restart)
  397. aha1542_restart(shost);
  398. return;
  399. };
  400. mbo = (scsi2int(mb[mbi].ccbptr) - (SCSI_BUF_PA(&ccb[0]))) / sizeof(struct ccb);
  401. mbistatus = mb[mbi].status;
  402. mb[mbi].status = 0;
  403. HOSTDATA(shost)->aha1542_last_mbi_used = mbi;
  404. spin_unlock_irqrestore(&aha1542_lock, flags);
  405. #ifdef DEBUG
  406. {
  407. if (ccb[mbo].tarstat | ccb[mbo].hastat)
  408. printk(KERN_DEBUG "aha1542_command: returning %x (status %d)\n",
  409. ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
  410. };
  411. #endif
  412. if (mbistatus == 3)
  413. continue; /* Aborted command not found */
  414. #ifdef DEBUG
  415. printk(KERN_DEBUG "...done %d %d\n", mbo, mbi);
  416. #endif
  417. SCtmp = HOSTDATA(shost)->SCint[mbo];
  418. if (!SCtmp || !SCtmp->scsi_done) {
  419. printk(KERN_WARNING "aha1542_intr_handle: Unexpected interrupt\n");
  420. printk(KERN_WARNING "tarstat=%x, hastat=%x idlun=%x ccb#=%d \n", ccb[mbo].tarstat,
  421. ccb[mbo].hastat, ccb[mbo].idlun, mbo);
  422. return;
  423. }
  424. my_done = SCtmp->scsi_done;
  425. kfree(SCtmp->host_scribble);
  426. SCtmp->host_scribble = NULL;
  427. /* Fetch the sense data, and tuck it away, in the required slot. The
  428. Adaptec automatically fetches it, and there is no guarantee that
  429. we will still have it in the cdb when we come back */
  430. if (ccb[mbo].tarstat == 2)
  431. memcpy(SCtmp->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
  432. SCSI_SENSE_BUFFERSIZE);
  433. /* is there mail :-) */
  434. /* more error checking left out here */
  435. if (mbistatus != 1)
  436. /* This is surely wrong, but I don't know what's right */
  437. errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
  438. else
  439. errstatus = 0;
  440. #ifdef DEBUG
  441. if (errstatus)
  442. printk(KERN_DEBUG "(aha1542 error:%x %x %x) ", errstatus,
  443. ccb[mbo].hastat, ccb[mbo].tarstat);
  444. #endif
  445. if (ccb[mbo].tarstat == 2) {
  446. #ifdef DEBUG
  447. int i;
  448. #endif
  449. DEB(printk("aha1542_intr_handle: sense:"));
  450. #ifdef DEBUG
  451. for (i = 0; i < 12; i++)
  452. printk("%02x ", ccb[mbo].cdb[ccb[mbo].cdblen + i]);
  453. printk("\n");
  454. #endif
  455. /*
  456. DEB(printk("aha1542_intr_handle: buf:"));
  457. for (i = 0; i < bufflen; i++)
  458. printk("%02x ", ((unchar *)buff)[i]);
  459. printk("\n");
  460. */
  461. }
  462. DEB(if (errstatus) printk("aha1542_intr_handle: returning %6x\n", errstatus));
  463. SCtmp->result = errstatus;
  464. HOSTDATA(shost)->SCint[mbo] = NULL; /* This effectively frees up the mailbox slot, as
  465. far as queuecommand is concerned */
  466. my_done(SCtmp);
  467. number_serviced++;
  468. };
  469. }
  470. static int aha1542_queuecommand_lck(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
  471. {
  472. unchar ahacmd = CMD_START_SCSI;
  473. unchar direction;
  474. unchar *cmd = (unchar *) SCpnt->cmnd;
  475. unchar target = SCpnt->device->id;
  476. unchar lun = SCpnt->device->lun;
  477. unsigned long flags;
  478. int bufflen = scsi_bufflen(SCpnt);
  479. int mbo;
  480. struct mailbox *mb;
  481. struct ccb *ccb;
  482. DEB(int i);
  483. mb = HOSTDATA(SCpnt->device->host)->mb;
  484. ccb = HOSTDATA(SCpnt->device->host)->ccb;
  485. DEB(if (target > 1) {
  486. SCpnt->result = DID_TIME_OUT << 16;
  487. done(SCpnt); return 0;
  488. }
  489. );
  490. if (*cmd == REQUEST_SENSE) {
  491. /* Don't do the command - we have the sense data already */
  492. #if 0
  493. /* scsi_request_sense() provides a buffer of size 256,
  494. so there is no reason to expect equality */
  495. if (bufflen != SCSI_SENSE_BUFFERSIZE)
  496. printk(KERN_CRIT "aha1542: Wrong buffer length supplied "
  497. "for request sense (%d)\n", bufflen);
  498. #endif
  499. SCpnt->result = 0;
  500. done(SCpnt);
  501. return 0;
  502. }
  503. #ifdef DEBUG
  504. if (*cmd == READ_10 || *cmd == WRITE_10)
  505. i = xscsi2int(cmd + 2);
  506. else if (*cmd == READ_6 || *cmd == WRITE_6)
  507. i = scsi2int(cmd + 2);
  508. else
  509. i = -1;
  510. if (done)
  511. printk(KERN_DEBUG "aha1542_queuecommand: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
  512. else
  513. printk(KERN_DEBUG "aha1542_command: dev %d cmd %02x pos %d len %d ", target, *cmd, i, bufflen);
  514. aha1542_stat();
  515. printk(KERN_DEBUG "aha1542_queuecommand: dumping scsi cmd:");
  516. for (i = 0; i < SCpnt->cmd_len; i++)
  517. printk("%02x ", cmd[i]);
  518. printk("\n");
  519. if (*cmd == WRITE_10 || *cmd == WRITE_6)
  520. return 0; /* we are still testing, so *don't* write */
  521. #endif
  522. /* Use the outgoing mailboxes in a round-robin fashion, because this
  523. is how the host adapter will scan for them */
  524. spin_lock_irqsave(&aha1542_lock, flags);
  525. mbo = HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used + 1;
  526. if (mbo >= AHA1542_MAILBOXES)
  527. mbo = 0;
  528. do {
  529. if (mb[mbo].status == 0 && HOSTDATA(SCpnt->device->host)->SCint[mbo] == NULL)
  530. break;
  531. mbo++;
  532. if (mbo >= AHA1542_MAILBOXES)
  533. mbo = 0;
  534. } while (mbo != HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used);
  535. if (mb[mbo].status || HOSTDATA(SCpnt->device->host)->SCint[mbo])
  536. panic("Unable to find empty mailbox for aha1542.\n");
  537. HOSTDATA(SCpnt->device->host)->SCint[mbo] = SCpnt; /* This will effectively prevent someone else from
  538. screwing with this cdb. */
  539. HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used = mbo;
  540. spin_unlock_irqrestore(&aha1542_lock, flags);
  541. #ifdef DEBUG
  542. printk(KERN_DEBUG "Sending command (%d %x)...", mbo, done);
  543. #endif
  544. any2scsi(mb[mbo].ccbptr, SCSI_BUF_PA(&ccb[mbo])); /* This gets trashed for some reason */
  545. memset(&ccb[mbo], 0, sizeof(struct ccb));
  546. ccb[mbo].cdblen = SCpnt->cmd_len;
  547. direction = 0;
  548. if (*cmd == READ_10 || *cmd == READ_6)
  549. direction = 8;
  550. else if (*cmd == WRITE_10 || *cmd == WRITE_6)
  551. direction = 16;
  552. memcpy(ccb[mbo].cdb, cmd, ccb[mbo].cdblen);
  553. if (bufflen) {
  554. struct scatterlist *sg;
  555. struct chain *cptr;
  556. #ifdef DEBUG
  557. unsigned char *ptr;
  558. #endif
  559. int i, sg_count = scsi_sg_count(SCpnt);
  560. ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
  561. SCpnt->host_scribble = kmalloc(sizeof(*cptr)*sg_count,
  562. GFP_KERNEL | GFP_DMA);
  563. cptr = (struct chain *) SCpnt->host_scribble;
  564. if (cptr == NULL) {
  565. /* free the claimed mailbox slot */
  566. HOSTDATA(SCpnt->device->host)->SCint[mbo] = NULL;
  567. return SCSI_MLQUEUE_HOST_BUSY;
  568. }
  569. scsi_for_each_sg(SCpnt, sg, sg_count, i) {
  570. any2scsi(cptr[i].dataptr, SCSI_SG_PA(sg));
  571. any2scsi(cptr[i].datalen, sg->length);
  572. };
  573. any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
  574. any2scsi(ccb[mbo].dataptr, SCSI_BUF_PA(cptr));
  575. #ifdef DEBUG
  576. printk("cptr %x: ", cptr);
  577. ptr = (unsigned char *) cptr;
  578. for (i = 0; i < 18; i++)
  579. printk("%02x ", ptr[i]);
  580. #endif
  581. } else {
  582. ccb[mbo].op = 0; /* SCSI Initiator Command */
  583. SCpnt->host_scribble = NULL;
  584. any2scsi(ccb[mbo].datalen, 0);
  585. any2scsi(ccb[mbo].dataptr, 0);
  586. };
  587. ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
  588. ccb[mbo].rsalen = 16;
  589. ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
  590. ccb[mbo].commlinkid = 0;
  591. #ifdef DEBUG
  592. {
  593. int i;
  594. printk(KERN_DEBUG "aha1542_command: sending.. ");
  595. for (i = 0; i < sizeof(ccb[mbo]) - 10; i++)
  596. printk("%02x ", ((unchar *) & ccb[mbo])[i]);
  597. };
  598. #endif
  599. if (done) {
  600. DEB(printk("aha1542_queuecommand: now waiting for interrupt ");
  601. aha1542_stat());
  602. SCpnt->scsi_done = done;
  603. mb[mbo].status = 1;
  604. aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1); /* start scsi command */
  605. DEB(aha1542_stat());
  606. } else
  607. printk("aha1542_queuecommand: done can't be NULL\n");
  608. return 0;
  609. }
  610. static DEF_SCSI_QCMD(aha1542_queuecommand)
  611. /* Initialize mailboxes */
  612. static void setup_mailboxes(int bse, struct Scsi_Host *shpnt)
  613. {
  614. int i;
  615. struct mailbox *mb;
  616. struct ccb *ccb;
  617. unchar cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
  618. mb = HOSTDATA(shpnt)->mb;
  619. ccb = HOSTDATA(shpnt)->ccb;
  620. for (i = 0; i < AHA1542_MAILBOXES; i++) {
  621. mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
  622. any2scsi(mb[i].ccbptr, SCSI_BUF_PA(&ccb[i]));
  623. };
  624. aha1542_intr_reset(bse); /* reset interrupts, so they don't block */
  625. any2scsi((cmd + 2), SCSI_BUF_PA(mb));
  626. aha1542_out(bse, cmd, 5);
  627. WAIT(INTRFLAGS(bse), INTRMASK, HACC, 0);
  628. while (0) {
  629. fail:
  630. printk(KERN_ERR "aha1542_detect: failed setting up mailboxes\n");
  631. }
  632. aha1542_intr_reset(bse);
  633. }
  634. static int __init aha1542_getconfig(int base_io, unsigned char *irq_level, unsigned char *dma_chan, unsigned char *scsi_id)
  635. {
  636. unchar inquiry_cmd[] = {CMD_RETCONF};
  637. unchar inquiry_result[3];
  638. int i;
  639. i = inb(STATUS(base_io));
  640. if (i & DF) {
  641. i = inb(DATA(base_io));
  642. };
  643. aha1542_out(base_io, inquiry_cmd, 1);
  644. aha1542_in(base_io, inquiry_result, 3);
  645. WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
  646. while (0) {
  647. fail:
  648. printk(KERN_ERR "aha1542_detect: query board settings\n");
  649. }
  650. aha1542_intr_reset(base_io);
  651. switch (inquiry_result[0]) {
  652. case 0x80:
  653. *dma_chan = 7;
  654. break;
  655. case 0x40:
  656. *dma_chan = 6;
  657. break;
  658. case 0x20:
  659. *dma_chan = 5;
  660. break;
  661. case 0x01:
  662. *dma_chan = 0;
  663. break;
  664. case 0:
  665. /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
  666. Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
  667. *dma_chan = 0xFF;
  668. break;
  669. default:
  670. printk(KERN_ERR "Unable to determine Adaptec DMA priority. Disabling board\n");
  671. return -1;
  672. };
  673. switch (inquiry_result[1]) {
  674. case 0x40:
  675. *irq_level = 15;
  676. break;
  677. case 0x20:
  678. *irq_level = 14;
  679. break;
  680. case 0x8:
  681. *irq_level = 12;
  682. break;
  683. case 0x4:
  684. *irq_level = 11;
  685. break;
  686. case 0x2:
  687. *irq_level = 10;
  688. break;
  689. case 0x1:
  690. *irq_level = 9;
  691. break;
  692. default:
  693. printk(KERN_ERR "Unable to determine Adaptec IRQ level. Disabling board\n");
  694. return -1;
  695. };
  696. *scsi_id = inquiry_result[2] & 7;
  697. return 0;
  698. }
  699. /* This function should only be called for 1542C boards - we can detect
  700. the special firmware settings and unlock the board */
  701. static int __init aha1542_mbenable(int base)
  702. {
  703. static unchar mbenable_cmd[3];
  704. static unchar mbenable_result[2];
  705. int retval;
  706. retval = BIOS_TRANSLATION_6432;
  707. mbenable_cmd[0] = CMD_EXTBIOS;
  708. aha1542_out(base, mbenable_cmd, 1);
  709. if (aha1542_in1(base, mbenable_result, 2))
  710. return retval;
  711. WAITd(INTRFLAGS(base), INTRMASK, HACC, 0, 100);
  712. aha1542_intr_reset(base);
  713. if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
  714. mbenable_cmd[0] = CMD_MBENABLE;
  715. mbenable_cmd[1] = 0;
  716. mbenable_cmd[2] = mbenable_result[1];
  717. if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
  718. retval = BIOS_TRANSLATION_25563;
  719. aha1542_out(base, mbenable_cmd, 3);
  720. WAIT(INTRFLAGS(base), INTRMASK, HACC, 0);
  721. };
  722. while (0) {
  723. fail:
  724. printk(KERN_ERR "aha1542_mbenable: Mailbox init failed\n");
  725. }
  726. aha1542_intr_reset(base);
  727. return retval;
  728. }
  729. /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
  730. static int __init aha1542_query(int base_io, int *transl)
  731. {
  732. unchar inquiry_cmd[] = {CMD_INQUIRY};
  733. unchar inquiry_result[4];
  734. int i;
  735. i = inb(STATUS(base_io));
  736. if (i & DF) {
  737. i = inb(DATA(base_io));
  738. };
  739. aha1542_out(base_io, inquiry_cmd, 1);
  740. aha1542_in(base_io, inquiry_result, 4);
  741. WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
  742. while (0) {
  743. fail:
  744. printk(KERN_ERR "aha1542_detect: query card type\n");
  745. }
  746. aha1542_intr_reset(base_io);
  747. *transl = BIOS_TRANSLATION_6432; /* Default case */
  748. /* For an AHA1740 series board, we ignore the board since there is a
  749. hardware bug which can lead to wrong blocks being returned if the board
  750. is operating in the 1542 emulation mode. Since there is an extended mode
  751. driver, we simply ignore the board and let the 1740 driver pick it up.
  752. */
  753. if (inquiry_result[0] == 0x43) {
  754. printk(KERN_INFO "aha1542.c: Emulation mode not supported for AHA 174N hardware.\n");
  755. return 1;
  756. };
  757. /* Always call this - boards that do not support extended bios translation
  758. will ignore the command, and we will set the proper default */
  759. *transl = aha1542_mbenable(base_io);
  760. return 0;
  761. }
  762. #ifndef MODULE
  763. static char *setup_str[MAXBOARDS] __initdata;
  764. static int setup_idx = 0;
  765. static void __init aha1542_setup(char *str, int *ints)
  766. {
  767. const char *ahausage = "aha1542: usage: aha1542=<PORTBASE>[,<BUSON>,<BUSOFF>[,<DMASPEED>]]\n";
  768. int setup_portbase;
  769. if (setup_idx >= MAXBOARDS) {
  770. printk(KERN_ERR "aha1542: aha1542_setup called too many times! Bad LILO params ?\n");
  771. printk(KERN_ERR " Entryline 1: %s\n", setup_str[0]);
  772. printk(KERN_ERR " Entryline 2: %s\n", setup_str[1]);
  773. printk(KERN_ERR " This line: %s\n", str);
  774. return;
  775. }
  776. if (ints[0] < 1 || ints[0] > 4) {
  777. printk(KERN_ERR "aha1542: %s\n", str);
  778. printk(ahausage);
  779. printk(KERN_ERR "aha1542: Wrong parameters may cause system malfunction.. We try anyway..\n");
  780. }
  781. setup_called[setup_idx] = ints[0];
  782. setup_str[setup_idx] = str;
  783. setup_portbase = ints[0] >= 1 ? ints[1] : 0; /* Preserve the default value.. */
  784. setup_buson[setup_idx] = ints[0] >= 2 ? ints[2] : 7;
  785. setup_busoff[setup_idx] = ints[0] >= 3 ? ints[3] : 5;
  786. if (ints[0] >= 4)
  787. {
  788. int atbt = -1;
  789. switch (ints[4]) {
  790. case 5:
  791. atbt = 0x00;
  792. break;
  793. case 6:
  794. atbt = 0x04;
  795. break;
  796. case 7:
  797. atbt = 0x01;
  798. break;
  799. case 8:
  800. atbt = 0x02;
  801. break;
  802. case 10:
  803. atbt = 0x03;
  804. break;
  805. default:
  806. printk(KERN_ERR "aha1542: %s\n", str);
  807. printk(ahausage);
  808. printk(KERN_ERR "aha1542: Valid values for DMASPEED are 5-8, 10 MB/s. Using jumper defaults.\n");
  809. break;
  810. }
  811. setup_dmaspeed[setup_idx] = atbt;
  812. }
  813. if (setup_portbase != 0)
  814. bases[setup_idx] = setup_portbase;
  815. ++setup_idx;
  816. }
  817. static int __init do_setup(char *str)
  818. {
  819. int ints[5];
  820. int count=setup_idx;
  821. get_options(str, ARRAY_SIZE(ints), ints);
  822. aha1542_setup(str,ints);
  823. return count<setup_idx;
  824. }
  825. __setup("aha1542=",do_setup);
  826. #endif
  827. /* return non-zero on detection */
  828. static int __init aha1542_detect(struct scsi_host_template * tpnt)
  829. {
  830. unsigned char dma_chan;
  831. unsigned char irq_level;
  832. unsigned char scsi_id;
  833. unsigned long flags;
  834. unsigned int base_io;
  835. int trans;
  836. struct Scsi_Host *shpnt = NULL;
  837. int count = 0;
  838. int indx;
  839. DEB(printk("aha1542_detect: \n"));
  840. tpnt->proc_name = "aha1542";
  841. #ifdef MODULE
  842. bases[0] = aha1542[0];
  843. setup_buson[0] = aha1542[1];
  844. setup_busoff[0] = aha1542[2];
  845. {
  846. int atbt = -1;
  847. switch (aha1542[3]) {
  848. case 5:
  849. atbt = 0x00;
  850. break;
  851. case 6:
  852. atbt = 0x04;
  853. break;
  854. case 7:
  855. atbt = 0x01;
  856. break;
  857. case 8:
  858. atbt = 0x02;
  859. break;
  860. case 10:
  861. atbt = 0x03;
  862. break;
  863. };
  864. setup_dmaspeed[0] = atbt;
  865. }
  866. #endif
  867. /*
  868. * Hunt for ISA Plug'n'Pray Adaptecs (AHA1535)
  869. */
  870. if(isapnp)
  871. {
  872. struct pnp_dev *pdev = NULL;
  873. for(indx = 0; indx < ARRAY_SIZE(bases); indx++) {
  874. if(bases[indx])
  875. continue;
  876. pdev = pnp_find_dev(NULL, ISAPNP_VENDOR('A', 'D', 'P'),
  877. ISAPNP_FUNCTION(0x1542), pdev);
  878. if(pdev==NULL)
  879. break;
  880. /*
  881. * Activate the PnP card
  882. */
  883. if(pnp_device_attach(pdev)<0)
  884. continue;
  885. if(pnp_activate_dev(pdev)<0) {
  886. pnp_device_detach(pdev);
  887. continue;
  888. }
  889. if(!pnp_port_valid(pdev, 0)) {
  890. pnp_device_detach(pdev);
  891. continue;
  892. }
  893. bases[indx] = pnp_port_start(pdev, 0);
  894. /* The card can be queried for its DMA, we have
  895. the DMA set up that is enough */
  896. printk(KERN_INFO "ISAPnP found an AHA1535 at I/O 0x%03X\n", bases[indx]);
  897. }
  898. }
  899. for (indx = 0; indx < ARRAY_SIZE(bases); indx++)
  900. if (bases[indx] != 0 && request_region(bases[indx], 4, "aha1542")) {
  901. shpnt = scsi_register(tpnt,
  902. sizeof(struct aha1542_hostdata));
  903. if(shpnt==NULL) {
  904. release_region(bases[indx], 4);
  905. continue;
  906. }
  907. if (!aha1542_test_port(bases[indx], shpnt))
  908. goto unregister;
  909. base_io = bases[indx];
  910. /* Set the Bus on/off-times as not to ruin floppy performance */
  911. {
  912. unchar oncmd[] = {CMD_BUSON_TIME, 7};
  913. unchar offcmd[] = {CMD_BUSOFF_TIME, 5};
  914. if (setup_called[indx]) {
  915. oncmd[1] = setup_buson[indx];
  916. offcmd[1] = setup_busoff[indx];
  917. }
  918. aha1542_intr_reset(base_io);
  919. aha1542_out(base_io, oncmd, 2);
  920. WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
  921. aha1542_intr_reset(base_io);
  922. aha1542_out(base_io, offcmd, 2);
  923. WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
  924. if (setup_dmaspeed[indx] >= 0) {
  925. unchar dmacmd[] = {CMD_DMASPEED, 0};
  926. dmacmd[1] = setup_dmaspeed[indx];
  927. aha1542_intr_reset(base_io);
  928. aha1542_out(base_io, dmacmd, 2);
  929. WAIT(INTRFLAGS(base_io), INTRMASK, HACC, 0);
  930. }
  931. while (0) {
  932. fail:
  933. printk(KERN_ERR "aha1542_detect: setting bus on/off-time failed\n");
  934. }
  935. aha1542_intr_reset(base_io);
  936. }
  937. if (aha1542_query(base_io, &trans))
  938. goto unregister;
  939. if (aha1542_getconfig(base_io, &irq_level, &dma_chan, &scsi_id) == -1)
  940. goto unregister;
  941. printk(KERN_INFO "Configuring Adaptec (SCSI-ID %d) at IO:%x, IRQ %d", scsi_id, base_io, irq_level);
  942. if (dma_chan != 0xFF)
  943. printk(", DMA priority %d", dma_chan);
  944. printk("\n");
  945. DEB(aha1542_stat());
  946. setup_mailboxes(base_io, shpnt);
  947. DEB(aha1542_stat());
  948. DEB(printk("aha1542_detect: enable interrupt channel %d\n", irq_level));
  949. spin_lock_irqsave(&aha1542_lock, flags);
  950. if (request_irq(irq_level, do_aha1542_intr_handle, 0,
  951. "aha1542", shpnt)) {
  952. printk(KERN_ERR "Unable to allocate IRQ for adaptec controller.\n");
  953. spin_unlock_irqrestore(&aha1542_lock, flags);
  954. goto unregister;
  955. }
  956. if (dma_chan != 0xFF) {
  957. if (request_dma(dma_chan, "aha1542")) {
  958. printk(KERN_ERR "Unable to allocate DMA channel for Adaptec.\n");
  959. free_irq(irq_level, shpnt);
  960. spin_unlock_irqrestore(&aha1542_lock, flags);
  961. goto unregister;
  962. }
  963. if (dma_chan == 0 || dma_chan >= 5) {
  964. set_dma_mode(dma_chan, DMA_MODE_CASCADE);
  965. enable_dma(dma_chan);
  966. }
  967. }
  968. shpnt->this_id = scsi_id;
  969. shpnt->unique_id = base_io;
  970. shpnt->io_port = base_io;
  971. shpnt->n_io_port = 4; /* Number of bytes of I/O space used */
  972. shpnt->dma_channel = dma_chan;
  973. shpnt->irq = irq_level;
  974. HOSTDATA(shpnt)->bios_translation = trans;
  975. if (trans == BIOS_TRANSLATION_25563)
  976. printk(KERN_INFO "aha1542.c: Using extended bios translation\n");
  977. HOSTDATA(shpnt)->aha1542_last_mbi_used = (2 * AHA1542_MAILBOXES - 1);
  978. HOSTDATA(shpnt)->aha1542_last_mbo_used = (AHA1542_MAILBOXES - 1);
  979. memset(HOSTDATA(shpnt)->SCint, 0, sizeof(HOSTDATA(shpnt)->SCint));
  980. spin_unlock_irqrestore(&aha1542_lock, flags);
  981. #if 0
  982. DEB(printk(" *** READ CAPACITY ***\n"));
  983. {
  984. unchar buf[8];
  985. static unchar cmd[] = { READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  986. int i;
  987. for (i = 0; i < sizeof(buf); ++i)
  988. buf[i] = 0x87;
  989. for (i = 0; i < 2; ++i)
  990. if (!aha1542_command(i, cmd, buf, sizeof(buf))) {
  991. printk(KERN_DEBUG "aha_detect: LU %d sector_size %d device_size %d\n",
  992. i, xscsi2int(buf + 4), xscsi2int(buf));
  993. }
  994. }
  995. DEB(printk(" *** NOW RUNNING MY OWN TEST *** \n"));
  996. for (i = 0; i < 4; ++i) {
  997. unsigned char cmd[10];
  998. static buffer[512];
  999. cmd[0] = READ_10;
  1000. cmd[1] = 0;
  1001. xany2scsi(cmd + 2, i);
  1002. cmd[6] = 0;
  1003. cmd[7] = 0;
  1004. cmd[8] = 1;
  1005. cmd[9] = 0;
  1006. aha1542_command(0, cmd, buffer, 512);
  1007. }
  1008. #endif
  1009. count++;
  1010. continue;
  1011. unregister:
  1012. release_region(bases[indx], 4);
  1013. scsi_unregister(shpnt);
  1014. continue;
  1015. };
  1016. return count;
  1017. }
  1018. static int aha1542_release(struct Scsi_Host *shost)
  1019. {
  1020. if (shost->irq)
  1021. free_irq(shost->irq, shost);
  1022. if (shost->dma_channel != 0xff)
  1023. free_dma(shost->dma_channel);
  1024. if (shost->io_port && shost->n_io_port)
  1025. release_region(shost->io_port, shost->n_io_port);
  1026. scsi_unregister(shost);
  1027. return 0;
  1028. }
  1029. static int aha1542_restart(struct Scsi_Host *shost)
  1030. {
  1031. int i;
  1032. int count = 0;
  1033. #if 0
  1034. unchar ahacmd = CMD_START_SCSI;
  1035. #endif
  1036. for (i = 0; i < AHA1542_MAILBOXES; i++)
  1037. if (HOSTDATA(shost)->SCint[i] &&
  1038. !(HOSTDATA(shost)->SCint[i]->device->soft_reset)) {
  1039. #if 0
  1040. HOSTDATA(shost)->mb[i].status = 1; /* Indicate ready to restart... */
  1041. #endif
  1042. count++;
  1043. }
  1044. printk(KERN_DEBUG "Potential to restart %d stalled commands...\n", count);
  1045. #if 0
  1046. /* start scsi command */
  1047. if (count)
  1048. aha1542_out(shost->io_port, &ahacmd, 1);
  1049. #endif
  1050. return 0;
  1051. }
  1052. /*
  1053. * This is a device reset. This is handled by sending a special command
  1054. * to the device.
  1055. */
  1056. static int aha1542_dev_reset(Scsi_Cmnd * SCpnt)
  1057. {
  1058. unsigned long flags;
  1059. struct mailbox *mb;
  1060. unchar target = SCpnt->device->id;
  1061. unchar lun = SCpnt->device->lun;
  1062. int mbo;
  1063. struct ccb *ccb;
  1064. unchar ahacmd = CMD_START_SCSI;
  1065. ccb = HOSTDATA(SCpnt->device->host)->ccb;
  1066. mb = HOSTDATA(SCpnt->device->host)->mb;
  1067. spin_lock_irqsave(&aha1542_lock, flags);
  1068. mbo = HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used + 1;
  1069. if (mbo >= AHA1542_MAILBOXES)
  1070. mbo = 0;
  1071. do {
  1072. if (mb[mbo].status == 0 && HOSTDATA(SCpnt->device->host)->SCint[mbo] == NULL)
  1073. break;
  1074. mbo++;
  1075. if (mbo >= AHA1542_MAILBOXES)
  1076. mbo = 0;
  1077. } while (mbo != HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used);
  1078. if (mb[mbo].status || HOSTDATA(SCpnt->device->host)->SCint[mbo])
  1079. panic("Unable to find empty mailbox for aha1542.\n");
  1080. HOSTDATA(SCpnt->device->host)->SCint[mbo] = SCpnt; /* This will effectively
  1081. prevent someone else from
  1082. screwing with this cdb. */
  1083. HOSTDATA(SCpnt->device->host)->aha1542_last_mbo_used = mbo;
  1084. spin_unlock_irqrestore(&aha1542_lock, flags);
  1085. any2scsi(mb[mbo].ccbptr, SCSI_BUF_PA(&ccb[mbo])); /* This gets trashed for some reason */
  1086. memset(&ccb[mbo], 0, sizeof(struct ccb));
  1087. ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
  1088. ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
  1089. ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
  1090. ccb[mbo].commlinkid = 0;
  1091. /*
  1092. * Now tell the 1542 to flush all pending commands for this
  1093. * target
  1094. */
  1095. aha1542_out(SCpnt->device->host->io_port, &ahacmd, 1);
  1096. scmd_printk(KERN_WARNING, SCpnt,
  1097. "Trying device reset for target\n");
  1098. return SUCCESS;
  1099. #ifdef ERIC_neverdef
  1100. /*
  1101. * With the 1542 we apparently never get an interrupt to
  1102. * acknowledge a device reset being sent. Then again, Leonard
  1103. * says we are doing this wrong in the first place...
  1104. *
  1105. * Take a wait and see attitude. If we get spurious interrupts,
  1106. * then the device reset is doing something sane and useful, and
  1107. * we will wait for the interrupt to post completion.
  1108. */
  1109. printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
  1110. /*
  1111. * Free the command block for all commands running on this
  1112. * target...
  1113. */
  1114. for (i = 0; i < AHA1542_MAILBOXES; i++) {
  1115. if (HOSTDATA(SCpnt->host)->SCint[i] &&
  1116. HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
  1117. Scsi_Cmnd *SCtmp;
  1118. SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
  1119. kfree(SCtmp->host_scribble);
  1120. SCtmp->host_scribble = NULL;
  1121. HOSTDATA(SCpnt->host)->SCint[i] = NULL;
  1122. HOSTDATA(SCpnt->host)->mb[i].status = 0;
  1123. }
  1124. }
  1125. return SUCCESS;
  1126. return FAILED;
  1127. #endif /* ERIC_neverdef */
  1128. }
  1129. static int aha1542_bus_reset(Scsi_Cmnd * SCpnt)
  1130. {
  1131. int i;
  1132. /*
  1133. * This does a scsi reset for all devices on the bus.
  1134. * In principle, we could also reset the 1542 - should
  1135. * we do this? Try this first, and we can add that later
  1136. * if it turns out to be useful.
  1137. */
  1138. outb(SCRST, CONTROL(SCpnt->device->host->io_port));
  1139. /*
  1140. * Wait for the thing to settle down a bit. Unfortunately
  1141. * this is going to basically lock up the machine while we
  1142. * wait for this to complete. To be 100% correct, we need to
  1143. * check for timeout, and if we are doing something like this
  1144. * we are pretty desperate anyways.
  1145. */
  1146. ssleep(4);
  1147. spin_lock_irq(SCpnt->device->host->host_lock);
  1148. WAIT(STATUS(SCpnt->device->host->io_port),
  1149. STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
  1150. /*
  1151. * Now try to pick up the pieces. For all pending commands,
  1152. * free any internal data structures, and basically clear things
  1153. * out. We do not try and restart any commands or anything -
  1154. * the strategy handler takes care of that crap.
  1155. */
  1156. printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
  1157. for (i = 0; i < AHA1542_MAILBOXES; i++) {
  1158. if (HOSTDATA(SCpnt->device->host)->SCint[i] != NULL) {
  1159. Scsi_Cmnd *SCtmp;
  1160. SCtmp = HOSTDATA(SCpnt->device->host)->SCint[i];
  1161. if (SCtmp->device->soft_reset) {
  1162. /*
  1163. * If this device implements the soft reset option,
  1164. * then it is still holding onto the command, and
  1165. * may yet complete it. In this case, we don't
  1166. * flush the data.
  1167. */
  1168. continue;
  1169. }
  1170. kfree(SCtmp->host_scribble);
  1171. SCtmp->host_scribble = NULL;
  1172. HOSTDATA(SCpnt->device->host)->SCint[i] = NULL;
  1173. HOSTDATA(SCpnt->device->host)->mb[i].status = 0;
  1174. }
  1175. }
  1176. spin_unlock_irq(SCpnt->device->host->host_lock);
  1177. return SUCCESS;
  1178. fail:
  1179. spin_unlock_irq(SCpnt->device->host->host_lock);
  1180. return FAILED;
  1181. }
  1182. static int aha1542_host_reset(Scsi_Cmnd * SCpnt)
  1183. {
  1184. int i;
  1185. /*
  1186. * This does a scsi reset for all devices on the bus.
  1187. * In principle, we could also reset the 1542 - should
  1188. * we do this? Try this first, and we can add that later
  1189. * if it turns out to be useful.
  1190. */
  1191. outb(HRST | SCRST, CONTROL(SCpnt->device->host->io_port));
  1192. /*
  1193. * Wait for the thing to settle down a bit. Unfortunately
  1194. * this is going to basically lock up the machine while we
  1195. * wait for this to complete. To be 100% correct, we need to
  1196. * check for timeout, and if we are doing something like this
  1197. * we are pretty desperate anyways.
  1198. */
  1199. ssleep(4);
  1200. spin_lock_irq(SCpnt->device->host->host_lock);
  1201. WAIT(STATUS(SCpnt->device->host->io_port),
  1202. STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
  1203. /*
  1204. * We need to do this too before the 1542 can interact with
  1205. * us again.
  1206. */
  1207. setup_mailboxes(SCpnt->device->host->io_port, SCpnt->device->host);
  1208. /*
  1209. * Now try to pick up the pieces. For all pending commands,
  1210. * free any internal data structures, and basically clear things
  1211. * out. We do not try and restart any commands or anything -
  1212. * the strategy handler takes care of that crap.
  1213. */
  1214. printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->device->host->host_no);
  1215. for (i = 0; i < AHA1542_MAILBOXES; i++) {
  1216. if (HOSTDATA(SCpnt->device->host)->SCint[i] != NULL) {
  1217. Scsi_Cmnd *SCtmp;
  1218. SCtmp = HOSTDATA(SCpnt->device->host)->SCint[i];
  1219. if (SCtmp->device->soft_reset) {
  1220. /*
  1221. * If this device implements the soft reset option,
  1222. * then it is still holding onto the command, and
  1223. * may yet complete it. In this case, we don't
  1224. * flush the data.
  1225. */
  1226. continue;
  1227. }
  1228. kfree(SCtmp->host_scribble);
  1229. SCtmp->host_scribble = NULL;
  1230. HOSTDATA(SCpnt->device->host)->SCint[i] = NULL;
  1231. HOSTDATA(SCpnt->device->host)->mb[i].status = 0;
  1232. }
  1233. }
  1234. spin_unlock_irq(SCpnt->device->host->host_lock);
  1235. return SUCCESS;
  1236. fail:
  1237. spin_unlock_irq(SCpnt->device->host->host_lock);
  1238. return FAILED;
  1239. }
  1240. #if 0
  1241. /*
  1242. * These are the old error handling routines. They are only temporarily
  1243. * here while we play with the new error handling code.
  1244. */
  1245. static int aha1542_old_abort(Scsi_Cmnd * SCpnt)
  1246. {
  1247. #if 0
  1248. unchar ahacmd = CMD_START_SCSI;
  1249. unsigned long flags;
  1250. struct mailbox *mb;
  1251. int mbi, mbo, i;
  1252. printk(KERN_DEBUG "In aha1542_abort: %x %x\n",
  1253. inb(STATUS(SCpnt->host->io_port)),
  1254. inb(INTRFLAGS(SCpnt->host->io_port)));
  1255. spin_lock_irqsave(&aha1542_lock, flags);
  1256. mb = HOSTDATA(SCpnt->host)->mb;
  1257. mbi = HOSTDATA(SCpnt->host)->aha1542_last_mbi_used + 1;
  1258. if (mbi >= 2 * AHA1542_MAILBOXES)
  1259. mbi = AHA1542_MAILBOXES;
  1260. do {
  1261. if (mb[mbi].status != 0)
  1262. break;
  1263. mbi++;
  1264. if (mbi >= 2 * AHA1542_MAILBOXES)
  1265. mbi = AHA1542_MAILBOXES;
  1266. } while (mbi != HOSTDATA(SCpnt->host)->aha1542_last_mbi_used);
  1267. spin_unlock_irqrestore(&aha1542_lock, flags);
  1268. if (mb[mbi].status) {
  1269. printk(KERN_ERR "Lost interrupt discovered on irq %d - attempting to recover\n",
  1270. SCpnt->host->irq);
  1271. aha1542_intr_handle(SCpnt->host, NULL);
  1272. return 0;
  1273. }
  1274. /* OK, no lost interrupt. Try looking to see how many pending commands
  1275. we think we have. */
  1276. for (i = 0; i < AHA1542_MAILBOXES; i++)
  1277. if (HOSTDATA(SCpnt->host)->SCint[i]) {
  1278. if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
  1279. printk(KERN_ERR "Timed out command pending for %s\n",
  1280. SCpnt->request->rq_disk ?
  1281. SCpnt->request->rq_disk->disk_name : "?"
  1282. );
  1283. if (HOSTDATA(SCpnt->host)->mb[i].status) {
  1284. printk(KERN_ERR "OGMB still full - restarting\n");
  1285. aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
  1286. };
  1287. } else
  1288. printk(KERN_ERR "Other pending command %s\n",
  1289. SCpnt->request->rq_disk ?
  1290. SCpnt->request->rq_disk->disk_name : "?"
  1291. );
  1292. }
  1293. #endif
  1294. DEB(printk("aha1542_abort\n"));
  1295. #if 0
  1296. spin_lock_irqsave(&aha1542_lock, flags);
  1297. for (mbo = 0; mbo < AHA1542_MAILBOXES; mbo++) {
  1298. if (SCpnt == HOSTDATA(SCpnt->host)->SCint[mbo]) {
  1299. mb[mbo].status = 2; /* Abort command */
  1300. aha1542_out(SCpnt->host->io_port, &ahacmd, 1); /* start scsi command */
  1301. spin_unlock_irqrestore(&aha1542_lock, flags);
  1302. break;
  1303. }
  1304. }
  1305. if (AHA1542_MAILBOXES == mbo)
  1306. spin_unlock_irqrestore(&aha1542_lock, flags);
  1307. #endif
  1308. return SCSI_ABORT_SNOOZE;
  1309. }
  1310. /* We do not implement a reset function here, but the upper level code
  1311. assumes that it will get some kind of response for the command in
  1312. SCpnt. We must oblige, or the command will hang the scsi system.
  1313. For a first go, we assume that the 1542 notifies us with all of the
  1314. pending commands (it does implement soft reset, after all). */
  1315. static int aha1542_old_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
  1316. {
  1317. unchar ahacmd = CMD_START_SCSI;
  1318. int i;
  1319. /*
  1320. * See if a bus reset was suggested.
  1321. */
  1322. if (reset_flags & SCSI_RESET_SUGGEST_BUS_RESET) {
  1323. /*
  1324. * This does a scsi reset for all devices on the bus.
  1325. * In principle, we could also reset the 1542 - should
  1326. * we do this? Try this first, and we can add that later
  1327. * if it turns out to be useful.
  1328. */
  1329. outb(HRST | SCRST, CONTROL(SCpnt->host->io_port));
  1330. /*
  1331. * Wait for the thing to settle down a bit. Unfortunately
  1332. * this is going to basically lock up the machine while we
  1333. * wait for this to complete. To be 100% correct, we need to
  1334. * check for timeout, and if we are doing something like this
  1335. * we are pretty desperate anyways.
  1336. */
  1337. WAIT(STATUS(SCpnt->host->io_port),
  1338. STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF);
  1339. /*
  1340. * We need to do this too before the 1542 can interact with
  1341. * us again.
  1342. */
  1343. setup_mailboxes(SCpnt->host->io_port, SCpnt->host);
  1344. /*
  1345. * Now try to pick up the pieces. Restart all commands
  1346. * that are currently active on the bus, and reset all of
  1347. * the datastructures. We have some time to kill while
  1348. * things settle down, so print a nice message.
  1349. */
  1350. printk(KERN_WARNING "Sent BUS RESET to scsi host %d\n", SCpnt->host->host_no);
  1351. for (i = 0; i < AHA1542_MAILBOXES; i++)
  1352. if (HOSTDATA(SCpnt->host)->SCint[i] != NULL) {
  1353. Scsi_Cmnd *SCtmp;
  1354. SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
  1355. SCtmp->result = DID_RESET << 16;
  1356. kfree(SCtmp->host_scribble);
  1357. SCtmp->host_scribble = NULL;
  1358. printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
  1359. SCtmp->scsi_done(SCpnt);
  1360. HOSTDATA(SCpnt->host)->SCint[i] = NULL;
  1361. HOSTDATA(SCpnt->host)->mb[i].status = 0;
  1362. }
  1363. /*
  1364. * Now tell the mid-level code what we did here. Since
  1365. * we have restarted all of the outstanding commands,
  1366. * then report SUCCESS.
  1367. */
  1368. return (SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET);
  1369. fail:
  1370. printk(KERN_CRIT "aha1542.c: Unable to perform hard reset.\n");
  1371. printk(KERN_CRIT "Power cycle machine to reset\n");
  1372. return (SCSI_RESET_ERROR | SCSI_RESET_BUS_RESET);
  1373. } else {
  1374. /* This does a selective reset of just the one device */
  1375. /* First locate the ccb for this command */
  1376. for (i = 0; i < AHA1542_MAILBOXES; i++)
  1377. if (HOSTDATA(SCpnt->host)->SCint[i] == SCpnt) {
  1378. HOSTDATA(SCpnt->host)->ccb[i].op = 0x81; /* BUS DEVICE RESET */
  1379. /* Now tell the 1542 to flush all pending commands for this target */
  1380. aha1542_out(SCpnt->host->io_port, &ahacmd, 1);
  1381. /* Here is the tricky part. What to do next. Do we get an interrupt
  1382. for the commands that we aborted with the specified target, or
  1383. do we generate this on our own? Try it without first and see
  1384. what happens */
  1385. printk(KERN_WARNING "Sent BUS DEVICE RESET to target %d\n", SCpnt->target);
  1386. /* If the first does not work, then try the second. I think the
  1387. first option is more likely to be correct. Free the command
  1388. block for all commands running on this target... */
  1389. for (i = 0; i < AHA1542_MAILBOXES; i++)
  1390. if (HOSTDATA(SCpnt->host)->SCint[i] &&
  1391. HOSTDATA(SCpnt->host)->SCint[i]->target == SCpnt->target) {
  1392. Scsi_Cmnd *SCtmp;
  1393. SCtmp = HOSTDATA(SCpnt->host)->SCint[i];
  1394. SCtmp->result = DID_RESET << 16;
  1395. kfree(SCtmp->host_scribble);
  1396. SCtmp->host_scribble = NULL;
  1397. printk(KERN_WARNING "Sending DID_RESET for target %d\n", SCpnt->target);
  1398. SCtmp->scsi_done(SCpnt);
  1399. HOSTDATA(SCpnt->host)->SCint[i] = NULL;
  1400. HOSTDATA(SCpnt->host)->mb[i].status = 0;
  1401. }
  1402. return SCSI_RESET_SUCCESS;
  1403. }
  1404. }
  1405. /* No active command at this time, so this means that each time we got
  1406. some kind of response the last time through. Tell the mid-level code
  1407. to request sense information in order to decide what to do next. */
  1408. return SCSI_RESET_PUNT;
  1409. }
  1410. #endif /* end of big comment block around old_abort + old_reset */
  1411. static int aha1542_biosparam(struct scsi_device *sdev,
  1412. struct block_device *bdev, sector_t capacity, int *ip)
  1413. {
  1414. int translation_algorithm;
  1415. int size = capacity;
  1416. translation_algorithm = HOSTDATA(sdev->host)->bios_translation;
  1417. if ((size >> 11) > 1024 && translation_algorithm == BIOS_TRANSLATION_25563) {
  1418. /* Please verify that this is the same as what DOS returns */
  1419. ip[0] = 255;
  1420. ip[1] = 63;
  1421. ip[2] = size / 255 / 63;
  1422. } else {
  1423. ip[0] = 64;
  1424. ip[1] = 32;
  1425. ip[2] = size >> 11;
  1426. }
  1427. return 0;
  1428. }
  1429. MODULE_LICENSE("GPL");
  1430. static struct scsi_host_template driver_template = {
  1431. .proc_name = "aha1542",
  1432. .name = "Adaptec 1542",
  1433. .detect = aha1542_detect,
  1434. .release = aha1542_release,
  1435. .queuecommand = aha1542_queuecommand,
  1436. .eh_device_reset_handler= aha1542_dev_reset,
  1437. .eh_bus_reset_handler = aha1542_bus_reset,
  1438. .eh_host_reset_handler = aha1542_host_reset,
  1439. .bios_param = aha1542_biosparam,
  1440. .can_queue = AHA1542_MAILBOXES,
  1441. .this_id = 7,
  1442. .sg_tablesize = AHA1542_SCATTER,
  1443. .cmd_per_lun = AHA1542_CMDLUN,
  1444. .unchecked_isa_dma = 1,
  1445. .use_clustering = ENABLE_CLUSTERING,
  1446. };
  1447. #include "scsi_module.c"