aha1542.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /*
  2. * Driver for Adaptec AHA-1542 SCSI host adapters
  3. *
  4. * Copyright (C) 1992 Tommy Thorn
  5. * Copyright (C) 1993, 1994, 1995 Eric Youngdale
  6. * Copyright (C) 2015 Ondrej Zary
  7. */
  8. #include <linux/module.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/kernel.h>
  11. #include <linux/types.h>
  12. #include <linux/string.h>
  13. #include <linux/delay.h>
  14. #include <linux/init.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/isa.h>
  17. #include <linux/pnp.h>
  18. #include <linux/slab.h>
  19. #include <linux/io.h>
  20. #include <asm/dma.h>
  21. #include <scsi/scsi_cmnd.h>
  22. #include <scsi/scsi_device.h>
  23. #include <scsi/scsi_host.h>
  24. #include "aha1542.h"
  25. #define MAXBOARDS 4
  26. static bool isapnp = 1;
  27. module_param(isapnp, bool, 0);
  28. MODULE_PARM_DESC(isapnp, "enable PnP support (default=1)");
  29. static int io[MAXBOARDS] = { 0x330, 0x334, 0, 0 };
  30. module_param_array(io, int, NULL, 0);
  31. MODULE_PARM_DESC(io, "base IO address of controller (0x130,0x134,0x230,0x234,0x330,0x334, default=0x330,0x334)");
  32. /* time AHA spends on the AT-bus during data transfer */
  33. static int bus_on[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 11us */
  34. module_param_array(bus_on, int, NULL, 0);
  35. MODULE_PARM_DESC(bus_on, "bus on time [us] (2-15, default=-1 [HW default: 11])");
  36. /* time AHA spends off the bus (not to monopolize it) during data transfer */
  37. static int bus_off[MAXBOARDS] = { -1, -1, -1, -1 }; /* power-on default: 4us */
  38. module_param_array(bus_off, int, NULL, 0);
  39. MODULE_PARM_DESC(bus_off, "bus off time [us] (1-64, default=-1 [HW default: 4])");
  40. /* default is jumper selected (J1 on 1542A), factory default = 5 MB/s */
  41. static int dma_speed[MAXBOARDS] = { -1, -1, -1, -1 };
  42. module_param_array(dma_speed, int, NULL, 0);
  43. MODULE_PARM_DESC(dma_speed, "DMA speed [MB/s] (5,6,7,8,10, default=-1 [by jumper])");
  44. #define BIOS_TRANSLATION_6432 1 /* Default case these days */
  45. #define BIOS_TRANSLATION_25563 2 /* Big disk case */
  46. struct aha1542_hostdata {
  47. /* This will effectively start both of them at the first mailbox */
  48. int bios_translation; /* Mapping bios uses - for compatibility */
  49. int aha1542_last_mbi_used;
  50. int aha1542_last_mbo_used;
  51. struct scsi_cmnd *int_cmds[AHA1542_MAILBOXES];
  52. struct mailbox mb[2 * AHA1542_MAILBOXES];
  53. struct ccb ccb[AHA1542_MAILBOXES];
  54. };
  55. static inline void aha1542_intr_reset(u16 base)
  56. {
  57. outb(IRST, CONTROL(base));
  58. }
  59. static inline bool wait_mask(u16 port, u8 mask, u8 allof, u8 noneof, int timeout)
  60. {
  61. bool delayed = true;
  62. if (timeout == 0) {
  63. timeout = 3000000;
  64. delayed = false;
  65. }
  66. while (1) {
  67. u8 bits = inb(port) & mask;
  68. if ((bits & allof) == allof && ((bits & noneof) == 0))
  69. break;
  70. if (delayed)
  71. mdelay(1);
  72. if (--timeout == 0)
  73. return false;
  74. }
  75. return true;
  76. }
  77. static int aha1542_outb(unsigned int base, u8 val)
  78. {
  79. if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
  80. return 1;
  81. outb(val, DATA(base));
  82. return 0;
  83. }
  84. static int aha1542_out(unsigned int base, u8 *buf, int len)
  85. {
  86. while (len--) {
  87. if (!wait_mask(STATUS(base), CDF, 0, CDF, 0))
  88. return 1;
  89. outb(*buf++, DATA(base));
  90. }
  91. if (!wait_mask(INTRFLAGS(base), INTRMASK, HACC, 0, 0))
  92. return 1;
  93. return 0;
  94. }
  95. /* Only used at boot time, so we do not need to worry about latency as much
  96. here */
  97. static int aha1542_in(unsigned int base, u8 *buf, int len, int timeout)
  98. {
  99. while (len--) {
  100. if (!wait_mask(STATUS(base), DF, DF, 0, timeout))
  101. return 1;
  102. *buf++ = inb(DATA(base));
  103. }
  104. return 0;
  105. }
  106. static int makecode(unsigned hosterr, unsigned scsierr)
  107. {
  108. switch (hosterr) {
  109. case 0x0:
  110. case 0xa: /* Linked command complete without error and linked normally */
  111. case 0xb: /* Linked command complete without error, interrupt generated */
  112. hosterr = 0;
  113. break;
  114. case 0x11: /* Selection time out-The initiator selection or target
  115. reselection was not complete within the SCSI Time out period */
  116. hosterr = DID_TIME_OUT;
  117. break;
  118. case 0x12: /* Data overrun/underrun-The target attempted to transfer more data
  119. than was allocated by the Data Length field or the sum of the
  120. Scatter / Gather Data Length fields. */
  121. case 0x13: /* Unexpected bus free-The target dropped the SCSI BSY at an unexpected time. */
  122. case 0x15: /* MBO command was not 00, 01 or 02-The first byte of the CB was
  123. invalid. This usually indicates a software failure. */
  124. case 0x16: /* Invalid CCB Operation Code-The first byte of the CCB was invalid.
  125. This usually indicates a software failure. */
  126. case 0x17: /* Linked CCB does not have the same LUN-A subsequent CCB of a set
  127. of linked CCB's does not specify the same logical unit number as
  128. the first. */
  129. case 0x18: /* Invalid Target Direction received from Host-The direction of a
  130. Target Mode CCB was invalid. */
  131. case 0x19: /* Duplicate CCB Received in Target Mode-More than once CCB was
  132. received to service data transfer between the same target LUN
  133. and initiator SCSI ID in the same direction. */
  134. case 0x1a: /* Invalid CCB or Segment List Parameter-A segment list with a zero
  135. length segment or invalid segment list boundaries was received.
  136. A CCB parameter was invalid. */
  137. #ifdef DEBUG
  138. printk("Aha1542: %x %x\n", hosterr, scsierr);
  139. #endif
  140. hosterr = DID_ERROR; /* Couldn't find any better */
  141. break;
  142. case 0x14: /* Target bus phase sequence failure-An invalid bus phase or bus
  143. phase sequence was requested by the target. The host adapter
  144. will generate a SCSI Reset Condition, notifying the host with
  145. a SCRD interrupt */
  146. hosterr = DID_RESET;
  147. break;
  148. default:
  149. printk(KERN_ERR "aha1542: makecode: unknown hoststatus %x\n", hosterr);
  150. break;
  151. }
  152. return scsierr | (hosterr << 16);
  153. }
  154. static int aha1542_test_port(struct Scsi_Host *sh)
  155. {
  156. u8 inquiry_result[4];
  157. int i;
  158. /* Quick and dirty test for presence of the card. */
  159. if (inb(STATUS(sh->io_port)) == 0xff)
  160. return 0;
  161. /* Reset the adapter. I ought to make a hard reset, but it's not really necessary */
  162. /* In case some other card was probing here, reset interrupts */
  163. aha1542_intr_reset(sh->io_port); /* reset interrupts, so they don't block */
  164. outb(SRST | IRST /*|SCRST */ , CONTROL(sh->io_port));
  165. mdelay(20); /* Wait a little bit for things to settle down. */
  166. /* Expect INIT and IDLE, any of the others are bad */
  167. if (!wait_mask(STATUS(sh->io_port), STATMASK, INIT | IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0))
  168. return 0;
  169. /* Shouldn't have generated any interrupts during reset */
  170. if (inb(INTRFLAGS(sh->io_port)) & INTRMASK)
  171. return 0;
  172. /* Perform a host adapter inquiry instead so we do not need to set
  173. up the mailboxes ahead of time */
  174. aha1542_outb(sh->io_port, CMD_INQUIRY);
  175. for (i = 0; i < 4; i++) {
  176. if (!wait_mask(STATUS(sh->io_port), DF, DF, 0, 0))
  177. return 0;
  178. inquiry_result[i] = inb(DATA(sh->io_port));
  179. }
  180. /* Reading port should reset DF */
  181. if (inb(STATUS(sh->io_port)) & DF)
  182. return 0;
  183. /* When HACC, command is completed, and we're though testing */
  184. if (!wait_mask(INTRFLAGS(sh->io_port), HACC, HACC, 0, 0))
  185. return 0;
  186. /* Clear interrupts */
  187. outb(IRST, CONTROL(sh->io_port));
  188. return 1;
  189. }
  190. static irqreturn_t aha1542_interrupt(int irq, void *dev_id)
  191. {
  192. struct Scsi_Host *sh = dev_id;
  193. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  194. void (*my_done)(struct scsi_cmnd *) = NULL;
  195. int errstatus, mbi, mbo, mbistatus;
  196. int number_serviced;
  197. unsigned long flags;
  198. struct scsi_cmnd *tmp_cmd;
  199. int flag;
  200. struct mailbox *mb = aha1542->mb;
  201. struct ccb *ccb = aha1542->ccb;
  202. #ifdef DEBUG
  203. {
  204. flag = inb(INTRFLAGS(sh->io_port));
  205. shost_printk(KERN_DEBUG, sh, "aha1542_intr_handle: ");
  206. if (!(flag & ANYINTR))
  207. printk("no interrupt?");
  208. if (flag & MBIF)
  209. printk("MBIF ");
  210. if (flag & MBOA)
  211. printk("MBOF ");
  212. if (flag & HACC)
  213. printk("HACC ");
  214. if (flag & SCRD)
  215. printk("SCRD ");
  216. printk("status %02x\n", inb(STATUS(sh->io_port)));
  217. };
  218. #endif
  219. number_serviced = 0;
  220. spin_lock_irqsave(sh->host_lock, flags);
  221. while (1) {
  222. flag = inb(INTRFLAGS(sh->io_port));
  223. /* Check for unusual interrupts. If any of these happen, we should
  224. probably do something special, but for now just printing a message
  225. is sufficient. A SCSI reset detected is something that we really
  226. need to deal with in some way. */
  227. if (flag & ~MBIF) {
  228. if (flag & MBOA)
  229. printk("MBOF ");
  230. if (flag & HACC)
  231. printk("HACC ");
  232. if (flag & SCRD)
  233. printk("SCRD ");
  234. }
  235. aha1542_intr_reset(sh->io_port);
  236. mbi = aha1542->aha1542_last_mbi_used + 1;
  237. if (mbi >= 2 * AHA1542_MAILBOXES)
  238. mbi = AHA1542_MAILBOXES;
  239. do {
  240. if (mb[mbi].status != 0)
  241. break;
  242. mbi++;
  243. if (mbi >= 2 * AHA1542_MAILBOXES)
  244. mbi = AHA1542_MAILBOXES;
  245. } while (mbi != aha1542->aha1542_last_mbi_used);
  246. if (mb[mbi].status == 0) {
  247. spin_unlock_irqrestore(sh->host_lock, flags);
  248. /* Hmm, no mail. Must have read it the last time around */
  249. if (!number_serviced)
  250. shost_printk(KERN_WARNING, sh, "interrupt received, but no mail.\n");
  251. return IRQ_HANDLED;
  252. };
  253. mbo = (scsi2int(mb[mbi].ccbptr) - (isa_virt_to_bus(&ccb[0]))) / sizeof(struct ccb);
  254. mbistatus = mb[mbi].status;
  255. mb[mbi].status = 0;
  256. aha1542->aha1542_last_mbi_used = mbi;
  257. #ifdef DEBUG
  258. if (ccb[mbo].tarstat | ccb[mbo].hastat)
  259. shost_printk(KERN_DEBUG, sh, "aha1542_command: returning %x (status %d)\n",
  260. ccb[mbo].tarstat + ((int) ccb[mbo].hastat << 16), mb[mbi].status);
  261. #endif
  262. if (mbistatus == 3)
  263. continue; /* Aborted command not found */
  264. #ifdef DEBUG
  265. shost_printk(KERN_DEBUG, sh, "...done %d %d\n", mbo, mbi);
  266. #endif
  267. tmp_cmd = aha1542->int_cmds[mbo];
  268. if (!tmp_cmd || !tmp_cmd->scsi_done) {
  269. spin_unlock_irqrestore(sh->host_lock, flags);
  270. shost_printk(KERN_WARNING, sh, "Unexpected interrupt\n");
  271. shost_printk(KERN_WARNING, sh, "tarstat=%x, hastat=%x idlun=%x ccb#=%d\n", ccb[mbo].tarstat,
  272. ccb[mbo].hastat, ccb[mbo].idlun, mbo);
  273. return IRQ_HANDLED;
  274. }
  275. my_done = tmp_cmd->scsi_done;
  276. kfree(tmp_cmd->host_scribble);
  277. tmp_cmd->host_scribble = NULL;
  278. /* Fetch the sense data, and tuck it away, in the required slot. The
  279. Adaptec automatically fetches it, and there is no guarantee that
  280. we will still have it in the cdb when we come back */
  281. if (ccb[mbo].tarstat == 2)
  282. memcpy(tmp_cmd->sense_buffer, &ccb[mbo].cdb[ccb[mbo].cdblen],
  283. SCSI_SENSE_BUFFERSIZE);
  284. /* is there mail :-) */
  285. /* more error checking left out here */
  286. if (mbistatus != 1)
  287. /* This is surely wrong, but I don't know what's right */
  288. errstatus = makecode(ccb[mbo].hastat, ccb[mbo].tarstat);
  289. else
  290. errstatus = 0;
  291. #ifdef DEBUG
  292. if (errstatus)
  293. shost_printk(KERN_DEBUG, sh, "(aha1542 error:%x %x %x) ", errstatus,
  294. ccb[mbo].hastat, ccb[mbo].tarstat);
  295. if (ccb[mbo].tarstat == 2)
  296. print_hex_dump_bytes("sense: ", DUMP_PREFIX_NONE, &ccb[mbo].cdb[ccb[mbo].cdblen], 12);
  297. if (errstatus)
  298. printk("aha1542_intr_handle: returning %6x\n", errstatus);
  299. #endif
  300. tmp_cmd->result = errstatus;
  301. aha1542->int_cmds[mbo] = NULL; /* This effectively frees up the mailbox slot, as
  302. far as queuecommand is concerned */
  303. my_done(tmp_cmd);
  304. number_serviced++;
  305. };
  306. }
  307. static int aha1542_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
  308. {
  309. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  310. u8 direction;
  311. u8 target = cmd->device->id;
  312. u8 lun = cmd->device->lun;
  313. unsigned long flags;
  314. int bufflen = scsi_bufflen(cmd);
  315. int mbo, sg_count;
  316. struct mailbox *mb = aha1542->mb;
  317. struct ccb *ccb = aha1542->ccb;
  318. struct chain *cptr;
  319. if (*cmd->cmnd == REQUEST_SENSE) {
  320. /* Don't do the command - we have the sense data already */
  321. cmd->result = 0;
  322. cmd->scsi_done(cmd);
  323. return 0;
  324. }
  325. #ifdef DEBUG
  326. {
  327. int i = -1;
  328. if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
  329. i = xscsi2int(cmd->cmnd + 2);
  330. else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
  331. i = scsi2int(cmd->cmnd + 2);
  332. shost_printk(KERN_DEBUG, sh, "aha1542_queuecommand: dev %d cmd %02x pos %d len %d",
  333. target, *cmd->cmnd, i, bufflen);
  334. print_hex_dump_bytes("command: ", DUMP_PREFIX_NONE, cmd->cmnd, cmd->cmd_len);
  335. }
  336. #endif
  337. if (bufflen) { /* allocate memory before taking host_lock */
  338. sg_count = scsi_sg_count(cmd);
  339. cptr = kmalloc(sizeof(*cptr) * sg_count, GFP_KERNEL | GFP_DMA);
  340. if (!cptr)
  341. return SCSI_MLQUEUE_HOST_BUSY;
  342. } else {
  343. sg_count = 0;
  344. cptr = NULL;
  345. }
  346. /* Use the outgoing mailboxes in a round-robin fashion, because this
  347. is how the host adapter will scan for them */
  348. spin_lock_irqsave(sh->host_lock, flags);
  349. mbo = aha1542->aha1542_last_mbo_used + 1;
  350. if (mbo >= AHA1542_MAILBOXES)
  351. mbo = 0;
  352. do {
  353. if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
  354. break;
  355. mbo++;
  356. if (mbo >= AHA1542_MAILBOXES)
  357. mbo = 0;
  358. } while (mbo != aha1542->aha1542_last_mbo_used);
  359. if (mb[mbo].status || aha1542->int_cmds[mbo])
  360. panic("Unable to find empty mailbox for aha1542.\n");
  361. aha1542->int_cmds[mbo] = cmd; /* This will effectively prevent someone else from
  362. screwing with this cdb. */
  363. aha1542->aha1542_last_mbo_used = mbo;
  364. #ifdef DEBUG
  365. shost_printk(KERN_DEBUG, sh, "Sending command (%d %p)...", mbo, cmd->scsi_done);
  366. #endif
  367. any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */
  368. memset(&ccb[mbo], 0, sizeof(struct ccb));
  369. ccb[mbo].cdblen = cmd->cmd_len;
  370. direction = 0;
  371. if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
  372. direction = 8;
  373. else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
  374. direction = 16;
  375. memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
  376. if (bufflen) {
  377. struct scatterlist *sg;
  378. int i;
  379. ccb[mbo].op = 2; /* SCSI Initiator Command w/scatter-gather */
  380. cmd->host_scribble = (void *)cptr;
  381. scsi_for_each_sg(cmd, sg, sg_count, i) {
  382. any2scsi(cptr[i].dataptr, isa_page_to_bus(sg_page(sg))
  383. + sg->offset);
  384. any2scsi(cptr[i].datalen, sg->length);
  385. };
  386. any2scsi(ccb[mbo].datalen, sg_count * sizeof(struct chain));
  387. any2scsi(ccb[mbo].dataptr, isa_virt_to_bus(cptr));
  388. #ifdef DEBUG
  389. shost_printk(KERN_DEBUG, sh, "cptr %p: ", cptr);
  390. print_hex_dump_bytes("cptr: ", DUMP_PREFIX_NONE, cptr, 18);
  391. #endif
  392. } else {
  393. ccb[mbo].op = 0; /* SCSI Initiator Command */
  394. cmd->host_scribble = NULL;
  395. any2scsi(ccb[mbo].datalen, 0);
  396. any2scsi(ccb[mbo].dataptr, 0);
  397. };
  398. ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
  399. ccb[mbo].rsalen = 16;
  400. ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
  401. ccb[mbo].commlinkid = 0;
  402. #ifdef DEBUG
  403. print_hex_dump_bytes("sending: ", DUMP_PREFIX_NONE, &ccb[mbo], sizeof(ccb[mbo]) - 10);
  404. printk("aha1542_queuecommand: now waiting for interrupt ");
  405. #endif
  406. mb[mbo].status = 1;
  407. aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
  408. spin_unlock_irqrestore(sh->host_lock, flags);
  409. return 0;
  410. }
  411. /* Initialize mailboxes */
  412. static void setup_mailboxes(struct Scsi_Host *sh)
  413. {
  414. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  415. int i;
  416. struct mailbox *mb = aha1542->mb;
  417. struct ccb *ccb = aha1542->ccb;
  418. u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
  419. for (i = 0; i < AHA1542_MAILBOXES; i++) {
  420. mb[i].status = mb[AHA1542_MAILBOXES + i].status = 0;
  421. any2scsi(mb[i].ccbptr, isa_virt_to_bus(&ccb[i]));
  422. };
  423. aha1542_intr_reset(sh->io_port); /* reset interrupts, so they don't block */
  424. any2scsi((mb_cmd + 2), isa_virt_to_bus(mb));
  425. if (aha1542_out(sh->io_port, mb_cmd, 5))
  426. shost_printk(KERN_ERR, sh, "failed setting up mailboxes\n");
  427. aha1542_intr_reset(sh->io_port);
  428. }
  429. static int aha1542_getconfig(struct Scsi_Host *sh)
  430. {
  431. u8 inquiry_result[3];
  432. int i;
  433. i = inb(STATUS(sh->io_port));
  434. if (i & DF) {
  435. i = inb(DATA(sh->io_port));
  436. };
  437. aha1542_outb(sh->io_port, CMD_RETCONF);
  438. aha1542_in(sh->io_port, inquiry_result, 3, 0);
  439. if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
  440. shost_printk(KERN_ERR, sh, "error querying board settings\n");
  441. aha1542_intr_reset(sh->io_port);
  442. switch (inquiry_result[0]) {
  443. case 0x80:
  444. sh->dma_channel = 7;
  445. break;
  446. case 0x40:
  447. sh->dma_channel = 6;
  448. break;
  449. case 0x20:
  450. sh->dma_channel = 5;
  451. break;
  452. case 0x01:
  453. sh->dma_channel = 0;
  454. break;
  455. case 0:
  456. /* This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
  457. Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this. */
  458. sh->dma_channel = 0xFF;
  459. break;
  460. default:
  461. shost_printk(KERN_ERR, sh, "Unable to determine DMA channel.\n");
  462. return -1;
  463. };
  464. switch (inquiry_result[1]) {
  465. case 0x40:
  466. sh->irq = 15;
  467. break;
  468. case 0x20:
  469. sh->irq = 14;
  470. break;
  471. case 0x8:
  472. sh->irq = 12;
  473. break;
  474. case 0x4:
  475. sh->irq = 11;
  476. break;
  477. case 0x2:
  478. sh->irq = 10;
  479. break;
  480. case 0x1:
  481. sh->irq = 9;
  482. break;
  483. default:
  484. shost_printk(KERN_ERR, sh, "Unable to determine IRQ level.\n");
  485. return -1;
  486. };
  487. sh->this_id = inquiry_result[2] & 7;
  488. return 0;
  489. }
  490. /* This function should only be called for 1542C boards - we can detect
  491. the special firmware settings and unlock the board */
  492. static int aha1542_mbenable(struct Scsi_Host *sh)
  493. {
  494. static u8 mbenable_cmd[3];
  495. static u8 mbenable_result[2];
  496. int retval;
  497. retval = BIOS_TRANSLATION_6432;
  498. aha1542_outb(sh->io_port, CMD_EXTBIOS);
  499. if (aha1542_in(sh->io_port, mbenable_result, 2, 100))
  500. return retval;
  501. if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 100))
  502. goto fail;
  503. aha1542_intr_reset(sh->io_port);
  504. if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
  505. mbenable_cmd[0] = CMD_MBENABLE;
  506. mbenable_cmd[1] = 0;
  507. mbenable_cmd[2] = mbenable_result[1];
  508. if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
  509. retval = BIOS_TRANSLATION_25563;
  510. if (aha1542_out(sh->io_port, mbenable_cmd, 3))
  511. goto fail;
  512. };
  513. while (0) {
  514. fail:
  515. shost_printk(KERN_ERR, sh, "Mailbox init failed\n");
  516. }
  517. aha1542_intr_reset(sh->io_port);
  518. return retval;
  519. }
  520. /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
  521. static int aha1542_query(struct Scsi_Host *sh)
  522. {
  523. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  524. u8 inquiry_result[4];
  525. int i;
  526. i = inb(STATUS(sh->io_port));
  527. if (i & DF) {
  528. i = inb(DATA(sh->io_port));
  529. };
  530. aha1542_outb(sh->io_port, CMD_INQUIRY);
  531. aha1542_in(sh->io_port, inquiry_result, 4, 0);
  532. if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
  533. shost_printk(KERN_ERR, sh, "error querying card type\n");
  534. aha1542_intr_reset(sh->io_port);
  535. aha1542->bios_translation = BIOS_TRANSLATION_6432; /* Default case */
  536. /* For an AHA1740 series board, we ignore the board since there is a
  537. hardware bug which can lead to wrong blocks being returned if the board
  538. is operating in the 1542 emulation mode. Since there is an extended mode
  539. driver, we simply ignore the board and let the 1740 driver pick it up.
  540. */
  541. if (inquiry_result[0] == 0x43) {
  542. shost_printk(KERN_INFO, sh, "Emulation mode not supported for AHA-1740 hardware, use aha1740 driver instead.\n");
  543. return 1;
  544. };
  545. /* Always call this - boards that do not support extended bios translation
  546. will ignore the command, and we will set the proper default */
  547. aha1542->bios_translation = aha1542_mbenable(sh);
  548. return 0;
  549. }
  550. static u8 dma_speed_hw(int dma_speed)
  551. {
  552. switch (dma_speed) {
  553. case 5:
  554. return 0x00;
  555. case 6:
  556. return 0x04;
  557. case 7:
  558. return 0x01;
  559. case 8:
  560. return 0x02;
  561. case 10:
  562. return 0x03;
  563. }
  564. return 0xff; /* invalid */
  565. }
  566. /* Set the Bus on/off-times as not to ruin floppy performance */
  567. static void aha1542_set_bus_times(struct Scsi_Host *sh, int bus_on, int bus_off, int dma_speed)
  568. {
  569. if (bus_on > 0) {
  570. u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on, 2, 15) };
  571. aha1542_intr_reset(sh->io_port);
  572. if (aha1542_out(sh->io_port, oncmd, 2))
  573. goto fail;
  574. }
  575. if (bus_off > 0) {
  576. u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off, 1, 64) };
  577. aha1542_intr_reset(sh->io_port);
  578. if (aha1542_out(sh->io_port, offcmd, 2))
  579. goto fail;
  580. }
  581. if (dma_speed_hw(dma_speed) != 0xff) {
  582. u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed) };
  583. aha1542_intr_reset(sh->io_port);
  584. if (aha1542_out(sh->io_port, dmacmd, 2))
  585. goto fail;
  586. }
  587. aha1542_intr_reset(sh->io_port);
  588. return;
  589. fail:
  590. shost_printk(KERN_ERR, sh, "setting bus on/off-time failed\n");
  591. aha1542_intr_reset(sh->io_port);
  592. }
  593. /* return non-zero on detection */
  594. static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
  595. {
  596. unsigned int base_io = io[indx];
  597. struct Scsi_Host *sh;
  598. struct aha1542_hostdata *aha1542;
  599. char dma_info[] = "no DMA";
  600. if (base_io == 0)
  601. return NULL;
  602. if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
  603. return NULL;
  604. sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
  605. if (!sh)
  606. goto release;
  607. aha1542 = shost_priv(sh);
  608. sh->unique_id = base_io;
  609. sh->io_port = base_io;
  610. sh->n_io_port = AHA1542_REGION_SIZE;
  611. aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
  612. aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
  613. if (!aha1542_test_port(sh))
  614. goto unregister;
  615. aha1542_set_bus_times(sh, bus_on[indx], bus_off[indx], dma_speed[indx]);
  616. if (aha1542_query(sh))
  617. goto unregister;
  618. if (aha1542_getconfig(sh) == -1)
  619. goto unregister;
  620. if (sh->dma_channel != 0xFF)
  621. snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel);
  622. shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n",
  623. sh->this_id, base_io, sh->irq, dma_info);
  624. if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
  625. shost_printk(KERN_INFO, sh, "Using extended bios translation\n");
  626. setup_mailboxes(sh);
  627. if (request_irq(sh->irq, aha1542_interrupt, 0, "aha1542", sh)) {
  628. shost_printk(KERN_ERR, sh, "Unable to allocate IRQ.\n");
  629. goto unregister;
  630. }
  631. if (sh->dma_channel != 0xFF) {
  632. if (request_dma(sh->dma_channel, "aha1542")) {
  633. shost_printk(KERN_ERR, sh, "Unable to allocate DMA channel.\n");
  634. goto free_irq;
  635. }
  636. if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
  637. set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
  638. enable_dma(sh->dma_channel);
  639. }
  640. }
  641. if (scsi_add_host(sh, pdev))
  642. goto free_dma;
  643. scsi_scan_host(sh);
  644. return sh;
  645. free_dma:
  646. if (sh->dma_channel != 0xff)
  647. free_dma(sh->dma_channel);
  648. free_irq:
  649. free_irq(sh->irq, sh);
  650. unregister:
  651. scsi_host_put(sh);
  652. release:
  653. release_region(base_io, AHA1542_REGION_SIZE);
  654. return NULL;
  655. }
  656. static int aha1542_release(struct Scsi_Host *sh)
  657. {
  658. scsi_remove_host(sh);
  659. if (sh->dma_channel != 0xff)
  660. free_dma(sh->dma_channel);
  661. if (sh->irq)
  662. free_irq(sh->irq, sh);
  663. if (sh->io_port && sh->n_io_port)
  664. release_region(sh->io_port, sh->n_io_port);
  665. scsi_host_put(sh);
  666. return 0;
  667. }
  668. /*
  669. * This is a device reset. This is handled by sending a special command
  670. * to the device.
  671. */
  672. static int aha1542_dev_reset(struct scsi_cmnd *cmd)
  673. {
  674. struct Scsi_Host *sh = cmd->device->host;
  675. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  676. unsigned long flags;
  677. struct mailbox *mb = aha1542->mb;
  678. u8 target = cmd->device->id;
  679. u8 lun = cmd->device->lun;
  680. int mbo;
  681. struct ccb *ccb = aha1542->ccb;
  682. spin_lock_irqsave(sh->host_lock, flags);
  683. mbo = aha1542->aha1542_last_mbo_used + 1;
  684. if (mbo >= AHA1542_MAILBOXES)
  685. mbo = 0;
  686. do {
  687. if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
  688. break;
  689. mbo++;
  690. if (mbo >= AHA1542_MAILBOXES)
  691. mbo = 0;
  692. } while (mbo != aha1542->aha1542_last_mbo_used);
  693. if (mb[mbo].status || aha1542->int_cmds[mbo])
  694. panic("Unable to find empty mailbox for aha1542.\n");
  695. aha1542->int_cmds[mbo] = cmd; /* This will effectively
  696. prevent someone else from
  697. screwing with this cdb. */
  698. aha1542->aha1542_last_mbo_used = mbo;
  699. any2scsi(mb[mbo].ccbptr, isa_virt_to_bus(&ccb[mbo])); /* This gets trashed for some reason */
  700. memset(&ccb[mbo], 0, sizeof(struct ccb));
  701. ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
  702. ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
  703. ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
  704. ccb[mbo].commlinkid = 0;
  705. /*
  706. * Now tell the 1542 to flush all pending commands for this
  707. * target
  708. */
  709. aha1542_outb(sh->io_port, CMD_START_SCSI);
  710. spin_unlock_irqrestore(sh->host_lock, flags);
  711. scmd_printk(KERN_WARNING, cmd,
  712. "Trying device reset for target\n");
  713. return SUCCESS;
  714. }
  715. static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
  716. {
  717. struct Scsi_Host *sh = cmd->device->host;
  718. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  719. unsigned long flags;
  720. int i;
  721. spin_lock_irqsave(sh->host_lock, flags);
  722. /*
  723. * This does a scsi reset for all devices on the bus.
  724. * In principle, we could also reset the 1542 - should
  725. * we do this? Try this first, and we can add that later
  726. * if it turns out to be useful.
  727. */
  728. outb(reset_cmd, CONTROL(cmd->device->host->io_port));
  729. if (!wait_mask(STATUS(cmd->device->host->io_port),
  730. STATMASK, IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
  731. spin_unlock_irqrestore(sh->host_lock, flags);
  732. return FAILED;
  733. }
  734. /*
  735. * We need to do this too before the 1542 can interact with
  736. * us again after host reset.
  737. */
  738. if (reset_cmd & HRST)
  739. setup_mailboxes(cmd->device->host);
  740. /*
  741. * Now try to pick up the pieces. For all pending commands,
  742. * free any internal data structures, and basically clear things
  743. * out. We do not try and restart any commands or anything -
  744. * the strategy handler takes care of that crap.
  745. */
  746. shost_printk(KERN_WARNING, cmd->device->host, "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
  747. for (i = 0; i < AHA1542_MAILBOXES; i++) {
  748. if (aha1542->int_cmds[i] != NULL) {
  749. struct scsi_cmnd *tmp_cmd;
  750. tmp_cmd = aha1542->int_cmds[i];
  751. if (tmp_cmd->device->soft_reset) {
  752. /*
  753. * If this device implements the soft reset option,
  754. * then it is still holding onto the command, and
  755. * may yet complete it. In this case, we don't
  756. * flush the data.
  757. */
  758. continue;
  759. }
  760. kfree(tmp_cmd->host_scribble);
  761. tmp_cmd->host_scribble = NULL;
  762. aha1542->int_cmds[i] = NULL;
  763. aha1542->mb[i].status = 0;
  764. }
  765. }
  766. spin_unlock_irqrestore(sh->host_lock, flags);
  767. return SUCCESS;
  768. }
  769. static int aha1542_bus_reset(struct scsi_cmnd *cmd)
  770. {
  771. return aha1542_reset(cmd, SCRST);
  772. }
  773. static int aha1542_host_reset(struct scsi_cmnd *cmd)
  774. {
  775. return aha1542_reset(cmd, HRST | SCRST);
  776. }
  777. static int aha1542_biosparam(struct scsi_device *sdev,
  778. struct block_device *bdev, sector_t capacity, int geom[])
  779. {
  780. struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
  781. if (capacity >= 0x200000 &&
  782. aha1542->bios_translation == BIOS_TRANSLATION_25563) {
  783. /* Please verify that this is the same as what DOS returns */
  784. geom[0] = 255; /* heads */
  785. geom[1] = 63; /* sectors */
  786. } else {
  787. geom[0] = 64; /* heads */
  788. geom[1] = 32; /* sectors */
  789. }
  790. geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */
  791. return 0;
  792. }
  793. MODULE_LICENSE("GPL");
  794. static struct scsi_host_template driver_template = {
  795. .module = THIS_MODULE,
  796. .proc_name = "aha1542",
  797. .name = "Adaptec 1542",
  798. .queuecommand = aha1542_queuecommand,
  799. .eh_device_reset_handler= aha1542_dev_reset,
  800. .eh_bus_reset_handler = aha1542_bus_reset,
  801. .eh_host_reset_handler = aha1542_host_reset,
  802. .bios_param = aha1542_biosparam,
  803. .can_queue = AHA1542_MAILBOXES,
  804. .this_id = 7,
  805. .sg_tablesize = 16,
  806. .unchecked_isa_dma = 1,
  807. .use_clustering = ENABLE_CLUSTERING,
  808. };
  809. static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
  810. {
  811. struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
  812. if (!sh)
  813. return 0;
  814. dev_set_drvdata(pdev, sh);
  815. return 1;
  816. }
  817. static int aha1542_isa_remove(struct device *pdev,
  818. unsigned int ndev)
  819. {
  820. aha1542_release(dev_get_drvdata(pdev));
  821. dev_set_drvdata(pdev, NULL);
  822. return 0;
  823. }
  824. static struct isa_driver aha1542_isa_driver = {
  825. .match = aha1542_isa_match,
  826. .remove = aha1542_isa_remove,
  827. .driver = {
  828. .name = "aha1542"
  829. },
  830. };
  831. static int isa_registered;
  832. #ifdef CONFIG_PNP
  833. static struct pnp_device_id aha1542_pnp_ids[] = {
  834. { .id = "ADP1542" },
  835. { .id = "" }
  836. };
  837. MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
  838. static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
  839. {
  840. int indx;
  841. struct Scsi_Host *sh;
  842. for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
  843. if (io[indx])
  844. continue;
  845. if (pnp_activate_dev(pdev) < 0)
  846. continue;
  847. io[indx] = pnp_port_start(pdev, 0);
  848. /* The card can be queried for its DMA, we have
  849. the DMA set up that is enough */
  850. dev_info(&pdev->dev, "ISAPnP found an AHA1535 at I/O 0x%03X", io[indx]);
  851. }
  852. sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
  853. if (!sh)
  854. return -ENODEV;
  855. pnp_set_drvdata(pdev, sh);
  856. return 0;
  857. }
  858. static void aha1542_pnp_remove(struct pnp_dev *pdev)
  859. {
  860. aha1542_release(pnp_get_drvdata(pdev));
  861. pnp_set_drvdata(pdev, NULL);
  862. }
  863. static struct pnp_driver aha1542_pnp_driver = {
  864. .name = "aha1542",
  865. .id_table = aha1542_pnp_ids,
  866. .probe = aha1542_pnp_probe,
  867. .remove = aha1542_pnp_remove,
  868. };
  869. static int pnp_registered;
  870. #endif /* CONFIG_PNP */
  871. static int __init aha1542_init(void)
  872. {
  873. int ret = 0;
  874. #ifdef CONFIG_PNP
  875. if (isapnp) {
  876. ret = pnp_register_driver(&aha1542_pnp_driver);
  877. if (!ret)
  878. pnp_registered = 1;
  879. }
  880. #endif
  881. ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
  882. if (!ret)
  883. isa_registered = 1;
  884. #ifdef CONFIG_PNP
  885. if (pnp_registered)
  886. ret = 0;
  887. #endif
  888. if (isa_registered)
  889. ret = 0;
  890. return ret;
  891. }
  892. static void __exit aha1542_exit(void)
  893. {
  894. #ifdef CONFIG_PNP
  895. if (pnp_registered)
  896. pnp_unregister_driver(&aha1542_pnp_driver);
  897. #endif
  898. if (isa_registered)
  899. isa_unregister_driver(&aha1542_isa_driver);
  900. }
  901. module_init(aha1542_init);
  902. module_exit(aha1542_exit);