aha1542.c 30 KB

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