aha1542.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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 int aha1542_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
  346. {
  347. struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
  348. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  349. u8 direction;
  350. u8 target = cmd->device->id;
  351. u8 lun = cmd->device->lun;
  352. unsigned long flags;
  353. int bufflen = scsi_bufflen(cmd);
  354. int mbo;
  355. struct mailbox *mb = aha1542->mb;
  356. struct ccb *ccb = aha1542->ccb;
  357. if (*cmd->cmnd == REQUEST_SENSE) {
  358. /* Don't do the command - we have the sense data already */
  359. cmd->result = 0;
  360. scsi_done(cmd);
  361. return 0;
  362. }
  363. #ifdef DEBUG
  364. {
  365. int i = -1;
  366. if (*cmd->cmnd == READ_10 || *cmd->cmnd == WRITE_10)
  367. i = xscsi2int(cmd->cmnd + 2);
  368. else if (*cmd->cmnd == READ_6 || *cmd->cmnd == WRITE_6)
  369. i = scsi2int(cmd->cmnd + 2);
  370. shost_printk(KERN_DEBUG, sh, "aha1542_queuecommand: dev %d cmd %02x pos %d len %d",
  371. target, *cmd->cmnd, i, bufflen);
  372. print_hex_dump_bytes("command: ", DUMP_PREFIX_NONE, cmd->cmnd, cmd->cmd_len);
  373. }
  374. #endif
  375. if (cmd->sc_data_direction == DMA_TO_DEVICE) {
  376. struct request *rq = scsi_cmd_to_rq(cmd);
  377. void *buf = acmd->data_buffer;
  378. struct req_iterator iter;
  379. struct bio_vec bv;
  380. rq_for_each_segment(bv, rq, iter) {
  381. memcpy_from_bvec(buf, &bv);
  382. buf += bv.bv_len;
  383. }
  384. }
  385. /*
  386. * Use the outgoing mailboxes in a round-robin fashion, because this
  387. * is how the host adapter will scan for them
  388. */
  389. spin_lock_irqsave(sh->host_lock, flags);
  390. mbo = aha1542->aha1542_last_mbo_used + 1;
  391. if (mbo >= AHA1542_MAILBOXES)
  392. mbo = 0;
  393. do {
  394. if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
  395. break;
  396. mbo++;
  397. if (mbo >= AHA1542_MAILBOXES)
  398. mbo = 0;
  399. } while (mbo != aha1542->aha1542_last_mbo_used);
  400. if (mb[mbo].status || aha1542->int_cmds[mbo])
  401. panic("Unable to find empty mailbox for aha1542.\n");
  402. aha1542->int_cmds[mbo] = cmd; /* This will effectively prevent someone else from
  403. * screwing with this cdb.
  404. */
  405. aha1542->aha1542_last_mbo_used = mbo;
  406. #ifdef DEBUG
  407. shost_printk(KERN_DEBUG, sh, "Sending command (%d)...", mbo);
  408. #endif
  409. /* This gets trashed for some reason */
  410. any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
  411. memset(&ccb[mbo], 0, sizeof(struct ccb));
  412. ccb[mbo].cdblen = cmd->cmd_len;
  413. direction = 0;
  414. if (*cmd->cmnd == READ_10 || *cmd->cmnd == READ_6)
  415. direction = 8;
  416. else if (*cmd->cmnd == WRITE_10 || *cmd->cmnd == WRITE_6)
  417. direction = 16;
  418. memcpy(ccb[mbo].cdb, cmd->cmnd, ccb[mbo].cdblen);
  419. ccb[mbo].op = 0; /* SCSI Initiator Command */
  420. any2scsi(ccb[mbo].datalen, bufflen);
  421. if (bufflen)
  422. any2scsi(ccb[mbo].dataptr, acmd->data_buffer_handle);
  423. else
  424. any2scsi(ccb[mbo].dataptr, 0);
  425. ccb[mbo].idlun = (target & 7) << 5 | direction | (lun & 7); /*SCSI Target Id */
  426. ccb[mbo].rsalen = 16;
  427. ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
  428. ccb[mbo].commlinkid = 0;
  429. #ifdef DEBUG
  430. print_hex_dump_bytes("sending: ", DUMP_PREFIX_NONE, &ccb[mbo], sizeof(ccb[mbo]) - 10);
  431. printk("aha1542_queuecommand: now waiting for interrupt ");
  432. #endif
  433. mb[mbo].status = 1;
  434. aha1542_outb(cmd->device->host->io_port, CMD_START_SCSI);
  435. spin_unlock_irqrestore(sh->host_lock, flags);
  436. return 0;
  437. }
  438. /* Initialize mailboxes */
  439. static void setup_mailboxes(struct Scsi_Host *sh)
  440. {
  441. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  442. u8 mb_cmd[5] = { CMD_MBINIT, AHA1542_MAILBOXES, 0, 0, 0};
  443. int i;
  444. for (i = 0; i < AHA1542_MAILBOXES; i++) {
  445. aha1542->mb[i].status = 0;
  446. any2scsi(aha1542->mb[i].ccbptr,
  447. aha1542->ccb_handle + i * sizeof(struct ccb));
  448. aha1542->mb[AHA1542_MAILBOXES + i].status = 0;
  449. }
  450. aha1542_intr_reset(sh->io_port); /* reset interrupts, so they don't block */
  451. any2scsi(mb_cmd + 2, aha1542->mb_handle);
  452. if (aha1542_out(sh->io_port, mb_cmd, 5))
  453. shost_printk(KERN_ERR, sh, "failed setting up mailboxes\n");
  454. aha1542_intr_reset(sh->io_port);
  455. }
  456. static int aha1542_getconfig(struct Scsi_Host *sh)
  457. {
  458. u8 inquiry_result[3];
  459. int i;
  460. i = inb(STATUS(sh->io_port));
  461. if (i & DF) {
  462. i = inb(DATA(sh->io_port));
  463. }
  464. aha1542_outb(sh->io_port, CMD_RETCONF);
  465. aha1542_in(sh->io_port, inquiry_result, 3, 0);
  466. if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
  467. shost_printk(KERN_ERR, sh, "error querying board settings\n");
  468. aha1542_intr_reset(sh->io_port);
  469. switch (inquiry_result[0]) {
  470. case 0x80:
  471. sh->dma_channel = 7;
  472. break;
  473. case 0x40:
  474. sh->dma_channel = 6;
  475. break;
  476. case 0x20:
  477. sh->dma_channel = 5;
  478. break;
  479. case 0x01:
  480. sh->dma_channel = 0;
  481. break;
  482. case 0:
  483. /*
  484. * This means that the adapter, although Adaptec 1542 compatible, doesn't use a DMA channel.
  485. * Currently only aware of the BusLogic BT-445S VL-Bus adapter which needs this.
  486. */
  487. sh->dma_channel = 0xFF;
  488. break;
  489. default:
  490. shost_printk(KERN_ERR, sh, "Unable to determine DMA channel.\n");
  491. return -1;
  492. }
  493. switch (inquiry_result[1]) {
  494. case 0x40:
  495. sh->irq = 15;
  496. break;
  497. case 0x20:
  498. sh->irq = 14;
  499. break;
  500. case 0x8:
  501. sh->irq = 12;
  502. break;
  503. case 0x4:
  504. sh->irq = 11;
  505. break;
  506. case 0x2:
  507. sh->irq = 10;
  508. break;
  509. case 0x1:
  510. sh->irq = 9;
  511. break;
  512. default:
  513. shost_printk(KERN_ERR, sh, "Unable to determine IRQ level.\n");
  514. return -1;
  515. }
  516. sh->this_id = inquiry_result[2] & 7;
  517. return 0;
  518. }
  519. /*
  520. * This function should only be called for 1542C boards - we can detect
  521. * the special firmware settings and unlock the board
  522. */
  523. static int aha1542_mbenable(struct Scsi_Host *sh)
  524. {
  525. static u8 mbenable_cmd[3];
  526. static u8 mbenable_result[2];
  527. int retval;
  528. retval = BIOS_TRANSLATION_6432;
  529. aha1542_outb(sh->io_port, CMD_EXTBIOS);
  530. if (aha1542_in(sh->io_port, mbenable_result, 2, 100))
  531. return retval;
  532. if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 100))
  533. goto fail;
  534. aha1542_intr_reset(sh->io_port);
  535. if ((mbenable_result[0] & 0x08) || mbenable_result[1]) {
  536. mbenable_cmd[0] = CMD_MBENABLE;
  537. mbenable_cmd[1] = 0;
  538. mbenable_cmd[2] = mbenable_result[1];
  539. if ((mbenable_result[0] & 0x08) && (mbenable_result[1] & 0x03))
  540. retval = BIOS_TRANSLATION_25563;
  541. if (aha1542_out(sh->io_port, mbenable_cmd, 3))
  542. goto fail;
  543. }
  544. while (0) {
  545. fail:
  546. shost_printk(KERN_ERR, sh, "Mailbox init failed\n");
  547. }
  548. aha1542_intr_reset(sh->io_port);
  549. return retval;
  550. }
  551. /* Query the board to find out if it is a 1542 or a 1740, or whatever. */
  552. static int aha1542_query(struct Scsi_Host *sh)
  553. {
  554. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  555. u8 inquiry_result[4];
  556. int i;
  557. i = inb(STATUS(sh->io_port));
  558. if (i & DF) {
  559. i = inb(DATA(sh->io_port));
  560. }
  561. aha1542_outb(sh->io_port, CMD_INQUIRY);
  562. aha1542_in(sh->io_port, inquiry_result, 4, 0);
  563. if (!wait_mask(INTRFLAGS(sh->io_port), INTRMASK, HACC, 0, 0))
  564. shost_printk(KERN_ERR, sh, "error querying card type\n");
  565. aha1542_intr_reset(sh->io_port);
  566. aha1542->bios_translation = BIOS_TRANSLATION_6432; /* Default case */
  567. /*
  568. * For an AHA1740 series board, we ignore the board since there is a
  569. * hardware bug which can lead to wrong blocks being returned if the board
  570. * is operating in the 1542 emulation mode. Since there is an extended mode
  571. * driver, we simply ignore the board and let the 1740 driver pick it up.
  572. */
  573. if (inquiry_result[0] == 0x43) {
  574. shost_printk(KERN_INFO, sh, "Emulation mode not supported for AHA-1740 hardware, use aha1740 driver instead.\n");
  575. return 1;
  576. }
  577. /*
  578. * Always call this - boards that do not support extended bios translation
  579. * will ignore the command, and we will set the proper default
  580. */
  581. aha1542->bios_translation = aha1542_mbenable(sh);
  582. return 0;
  583. }
  584. static u8 dma_speed_hw(int dma_speed)
  585. {
  586. switch (dma_speed) {
  587. case 5:
  588. return 0x00;
  589. case 6:
  590. return 0x04;
  591. case 7:
  592. return 0x01;
  593. case 8:
  594. return 0x02;
  595. case 10:
  596. return 0x03;
  597. }
  598. return 0xff; /* invalid */
  599. }
  600. /* Set the Bus on/off-times as not to ruin floppy performance */
  601. static void aha1542_set_bus_times(struct Scsi_Host *sh, int bus_on, int bus_off, int dma_speed)
  602. {
  603. if (bus_on > 0) {
  604. u8 oncmd[] = { CMD_BUSON_TIME, clamp(bus_on, 2, 15) };
  605. aha1542_intr_reset(sh->io_port);
  606. if (aha1542_out(sh->io_port, oncmd, 2))
  607. goto fail;
  608. }
  609. if (bus_off > 0) {
  610. u8 offcmd[] = { CMD_BUSOFF_TIME, clamp(bus_off, 1, 64) };
  611. aha1542_intr_reset(sh->io_port);
  612. if (aha1542_out(sh->io_port, offcmd, 2))
  613. goto fail;
  614. }
  615. if (dma_speed_hw(dma_speed) != 0xff) {
  616. u8 dmacmd[] = { CMD_DMASPEED, dma_speed_hw(dma_speed) };
  617. aha1542_intr_reset(sh->io_port);
  618. if (aha1542_out(sh->io_port, dmacmd, 2))
  619. goto fail;
  620. }
  621. aha1542_intr_reset(sh->io_port);
  622. return;
  623. fail:
  624. shost_printk(KERN_ERR, sh, "setting bus on/off-time failed\n");
  625. aha1542_intr_reset(sh->io_port);
  626. }
  627. /* return non-zero on detection */
  628. static struct Scsi_Host *aha1542_hw_init(struct scsi_host_template *tpnt, struct device *pdev, int indx)
  629. {
  630. unsigned int base_io = io[indx];
  631. struct Scsi_Host *sh;
  632. struct aha1542_hostdata *aha1542;
  633. char dma_info[] = "no DMA";
  634. if (base_io == 0)
  635. return NULL;
  636. if (!request_region(base_io, AHA1542_REGION_SIZE, "aha1542"))
  637. return NULL;
  638. sh = scsi_host_alloc(tpnt, sizeof(struct aha1542_hostdata));
  639. if (!sh)
  640. goto release;
  641. aha1542 = shost_priv(sh);
  642. sh->unique_id = base_io;
  643. sh->io_port = base_io;
  644. sh->n_io_port = AHA1542_REGION_SIZE;
  645. aha1542->aha1542_last_mbi_used = 2 * AHA1542_MAILBOXES - 1;
  646. aha1542->aha1542_last_mbo_used = AHA1542_MAILBOXES - 1;
  647. if (!aha1542_test_port(sh))
  648. goto unregister;
  649. aha1542_set_bus_times(sh, bus_on[indx], bus_off[indx], dma_speed[indx]);
  650. if (aha1542_query(sh))
  651. goto unregister;
  652. if (aha1542_getconfig(sh) == -1)
  653. goto unregister;
  654. if (sh->dma_channel != 0xFF)
  655. snprintf(dma_info, sizeof(dma_info), "DMA %d", sh->dma_channel);
  656. shost_printk(KERN_INFO, sh, "Adaptec AHA-1542 (SCSI-ID %d) at IO 0x%x, IRQ %d, %s\n",
  657. sh->this_id, base_io, sh->irq, dma_info);
  658. if (aha1542->bios_translation == BIOS_TRANSLATION_25563)
  659. shost_printk(KERN_INFO, sh, "Using extended bios translation\n");
  660. if (dma_set_mask_and_coherent(pdev, DMA_BIT_MASK(24)) < 0)
  661. goto unregister;
  662. aha1542->mb = dma_alloc_coherent(pdev,
  663. AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
  664. &aha1542->mb_handle, GFP_KERNEL);
  665. if (!aha1542->mb)
  666. goto unregister;
  667. aha1542->ccb = dma_alloc_coherent(pdev,
  668. AHA1542_MAILBOXES * sizeof(struct ccb),
  669. &aha1542->ccb_handle, GFP_KERNEL);
  670. if (!aha1542->ccb)
  671. goto free_mb;
  672. setup_mailboxes(sh);
  673. if (request_irq(sh->irq, aha1542_interrupt, 0, "aha1542", sh)) {
  674. shost_printk(KERN_ERR, sh, "Unable to allocate IRQ.\n");
  675. goto free_ccb;
  676. }
  677. if (sh->dma_channel != 0xFF) {
  678. if (request_dma(sh->dma_channel, "aha1542")) {
  679. shost_printk(KERN_ERR, sh, "Unable to allocate DMA channel.\n");
  680. goto free_irq;
  681. }
  682. if (sh->dma_channel == 0 || sh->dma_channel >= 5) {
  683. set_dma_mode(sh->dma_channel, DMA_MODE_CASCADE);
  684. enable_dma(sh->dma_channel);
  685. }
  686. }
  687. if (scsi_add_host(sh, pdev))
  688. goto free_dma;
  689. scsi_scan_host(sh);
  690. return sh;
  691. free_dma:
  692. if (sh->dma_channel != 0xff)
  693. free_dma(sh->dma_channel);
  694. free_irq:
  695. free_irq(sh->irq, sh);
  696. free_ccb:
  697. dma_free_coherent(pdev, AHA1542_MAILBOXES * sizeof(struct ccb),
  698. aha1542->ccb, aha1542->ccb_handle);
  699. free_mb:
  700. dma_free_coherent(pdev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
  701. aha1542->mb, aha1542->mb_handle);
  702. unregister:
  703. scsi_host_put(sh);
  704. release:
  705. release_region(base_io, AHA1542_REGION_SIZE);
  706. return NULL;
  707. }
  708. static int aha1542_release(struct Scsi_Host *sh)
  709. {
  710. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  711. struct device *dev = sh->dma_dev;
  712. scsi_remove_host(sh);
  713. if (sh->dma_channel != 0xff)
  714. free_dma(sh->dma_channel);
  715. dma_free_coherent(dev, AHA1542_MAILBOXES * sizeof(struct ccb),
  716. aha1542->ccb, aha1542->ccb_handle);
  717. dma_free_coherent(dev, AHA1542_MAILBOXES * 2 * sizeof(struct mailbox),
  718. aha1542->mb, aha1542->mb_handle);
  719. if (sh->irq)
  720. free_irq(sh->irq, sh);
  721. if (sh->io_port && sh->n_io_port)
  722. release_region(sh->io_port, sh->n_io_port);
  723. scsi_host_put(sh);
  724. return 0;
  725. }
  726. /*
  727. * This is a device reset. This is handled by sending a special command
  728. * to the device.
  729. */
  730. static int aha1542_dev_reset(struct scsi_cmnd *cmd)
  731. {
  732. struct Scsi_Host *sh = cmd->device->host;
  733. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  734. unsigned long flags;
  735. struct mailbox *mb = aha1542->mb;
  736. u8 target = cmd->device->id;
  737. u8 lun = cmd->device->lun;
  738. int mbo;
  739. struct ccb *ccb = aha1542->ccb;
  740. spin_lock_irqsave(sh->host_lock, flags);
  741. mbo = aha1542->aha1542_last_mbo_used + 1;
  742. if (mbo >= AHA1542_MAILBOXES)
  743. mbo = 0;
  744. do {
  745. if (mb[mbo].status == 0 && aha1542->int_cmds[mbo] == NULL)
  746. break;
  747. mbo++;
  748. if (mbo >= AHA1542_MAILBOXES)
  749. mbo = 0;
  750. } while (mbo != aha1542->aha1542_last_mbo_used);
  751. if (mb[mbo].status || aha1542->int_cmds[mbo])
  752. panic("Unable to find empty mailbox for aha1542.\n");
  753. aha1542->int_cmds[mbo] = cmd; /* This will effectively
  754. * prevent someone else from
  755. * screwing with this cdb.
  756. */
  757. aha1542->aha1542_last_mbo_used = mbo;
  758. /* This gets trashed for some reason */
  759. any2scsi(mb[mbo].ccbptr, aha1542->ccb_handle + mbo * sizeof(*ccb));
  760. memset(&ccb[mbo], 0, sizeof(struct ccb));
  761. ccb[mbo].op = 0x81; /* BUS DEVICE RESET */
  762. ccb[mbo].idlun = (target & 7) << 5 | (lun & 7); /*SCSI Target Id */
  763. ccb[mbo].linkptr[0] = ccb[mbo].linkptr[1] = ccb[mbo].linkptr[2] = 0;
  764. ccb[mbo].commlinkid = 0;
  765. /*
  766. * Now tell the 1542 to flush all pending commands for this
  767. * target
  768. */
  769. aha1542_outb(sh->io_port, CMD_START_SCSI);
  770. spin_unlock_irqrestore(sh->host_lock, flags);
  771. scmd_printk(KERN_WARNING, cmd,
  772. "Trying device reset for target\n");
  773. return SUCCESS;
  774. }
  775. static int aha1542_reset(struct scsi_cmnd *cmd, u8 reset_cmd)
  776. {
  777. struct Scsi_Host *sh = cmd->device->host;
  778. struct aha1542_hostdata *aha1542 = shost_priv(sh);
  779. unsigned long flags;
  780. int i;
  781. spin_lock_irqsave(sh->host_lock, flags);
  782. /*
  783. * This does a scsi reset for all devices on the bus.
  784. * In principle, we could also reset the 1542 - should
  785. * we do this? Try this first, and we can add that later
  786. * if it turns out to be useful.
  787. */
  788. outb(reset_cmd, CONTROL(cmd->device->host->io_port));
  789. if (!wait_mask(STATUS(cmd->device->host->io_port),
  790. STATMASK, IDLE, STST | DIAGF | INVDCMD | DF | CDF, 0)) {
  791. spin_unlock_irqrestore(sh->host_lock, flags);
  792. return FAILED;
  793. }
  794. /*
  795. * We need to do this too before the 1542 can interact with
  796. * us again after host reset.
  797. */
  798. if (reset_cmd & HRST)
  799. setup_mailboxes(cmd->device->host);
  800. /*
  801. * Now try to pick up the pieces. For all pending commands,
  802. * free any internal data structures, and basically clear things
  803. * out. We do not try and restart any commands or anything -
  804. * the strategy handler takes care of that crap.
  805. */
  806. shost_printk(KERN_WARNING, cmd->device->host, "Sent BUS RESET to scsi host %d\n", cmd->device->host->host_no);
  807. for (i = 0; i < AHA1542_MAILBOXES; i++) {
  808. if (aha1542->int_cmds[i] != NULL) {
  809. struct scsi_cmnd *tmp_cmd;
  810. tmp_cmd = aha1542->int_cmds[i];
  811. if (tmp_cmd->device->soft_reset) {
  812. /*
  813. * If this device implements the soft reset option,
  814. * then it is still holding onto the command, and
  815. * may yet complete it. In this case, we don't
  816. * flush the data.
  817. */
  818. continue;
  819. }
  820. aha1542_free_cmd(tmp_cmd);
  821. aha1542->int_cmds[i] = NULL;
  822. aha1542->mb[i].status = 0;
  823. }
  824. }
  825. spin_unlock_irqrestore(sh->host_lock, flags);
  826. return SUCCESS;
  827. }
  828. static int aha1542_bus_reset(struct scsi_cmnd *cmd)
  829. {
  830. return aha1542_reset(cmd, SCRST);
  831. }
  832. static int aha1542_host_reset(struct scsi_cmnd *cmd)
  833. {
  834. return aha1542_reset(cmd, HRST | SCRST);
  835. }
  836. static int aha1542_biosparam(struct scsi_device *sdev,
  837. struct block_device *bdev, sector_t capacity, int geom[])
  838. {
  839. struct aha1542_hostdata *aha1542 = shost_priv(sdev->host);
  840. if (capacity >= 0x200000 &&
  841. aha1542->bios_translation == BIOS_TRANSLATION_25563) {
  842. /* Please verify that this is the same as what DOS returns */
  843. geom[0] = 255; /* heads */
  844. geom[1] = 63; /* sectors */
  845. } else {
  846. geom[0] = 64; /* heads */
  847. geom[1] = 32; /* sectors */
  848. }
  849. geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */
  850. return 0;
  851. }
  852. MODULE_LICENSE("GPL");
  853. static int aha1542_init_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  854. {
  855. struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
  856. acmd->data_buffer = dma_alloc_coherent(shost->dma_dev,
  857. SECTOR_SIZE * AHA1542_MAX_SECTORS,
  858. &acmd->data_buffer_handle, GFP_KERNEL);
  859. if (!acmd->data_buffer)
  860. return -ENOMEM;
  861. return 0;
  862. }
  863. static int aha1542_exit_cmd_priv(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
  864. {
  865. struct aha1542_cmd *acmd = scsi_cmd_priv(cmd);
  866. dma_free_coherent(shost->dma_dev, SECTOR_SIZE * AHA1542_MAX_SECTORS,
  867. acmd->data_buffer, acmd->data_buffer_handle);
  868. return 0;
  869. }
  870. static struct scsi_host_template driver_template = {
  871. .module = THIS_MODULE,
  872. .proc_name = "aha1542",
  873. .name = "Adaptec 1542",
  874. .cmd_size = sizeof(struct aha1542_cmd),
  875. .queuecommand = aha1542_queuecommand,
  876. .eh_device_reset_handler= aha1542_dev_reset,
  877. .eh_bus_reset_handler = aha1542_bus_reset,
  878. .eh_host_reset_handler = aha1542_host_reset,
  879. .bios_param = aha1542_biosparam,
  880. .init_cmd_priv = aha1542_init_cmd_priv,
  881. .exit_cmd_priv = aha1542_exit_cmd_priv,
  882. .can_queue = AHA1542_MAILBOXES,
  883. .this_id = 7,
  884. .max_sectors = AHA1542_MAX_SECTORS,
  885. .sg_tablesize = SG_ALL,
  886. };
  887. static int aha1542_isa_match(struct device *pdev, unsigned int ndev)
  888. {
  889. struct Scsi_Host *sh = aha1542_hw_init(&driver_template, pdev, ndev);
  890. if (!sh)
  891. return 0;
  892. dev_set_drvdata(pdev, sh);
  893. return 1;
  894. }
  895. static void aha1542_isa_remove(struct device *pdev,
  896. unsigned int ndev)
  897. {
  898. aha1542_release(dev_get_drvdata(pdev));
  899. dev_set_drvdata(pdev, NULL);
  900. }
  901. static struct isa_driver aha1542_isa_driver = {
  902. .match = aha1542_isa_match,
  903. .remove = aha1542_isa_remove,
  904. .driver = {
  905. .name = "aha1542"
  906. },
  907. };
  908. static int isa_registered;
  909. #ifdef CONFIG_PNP
  910. static const struct pnp_device_id aha1542_pnp_ids[] = {
  911. { .id = "ADP1542" },
  912. { .id = "" }
  913. };
  914. MODULE_DEVICE_TABLE(pnp, aha1542_pnp_ids);
  915. static int aha1542_pnp_probe(struct pnp_dev *pdev, const struct pnp_device_id *id)
  916. {
  917. int indx;
  918. struct Scsi_Host *sh;
  919. for (indx = 0; indx < ARRAY_SIZE(io); indx++) {
  920. if (io[indx])
  921. continue;
  922. if (pnp_activate_dev(pdev) < 0)
  923. continue;
  924. io[indx] = pnp_port_start(pdev, 0);
  925. /*
  926. * The card can be queried for its DMA, we have
  927. * the DMA set up that is enough
  928. */
  929. dev_info(&pdev->dev, "ISAPnP found an AHA1535 at I/O 0x%03X", io[indx]);
  930. }
  931. sh = aha1542_hw_init(&driver_template, &pdev->dev, indx);
  932. if (!sh)
  933. return -ENODEV;
  934. pnp_set_drvdata(pdev, sh);
  935. return 0;
  936. }
  937. static void aha1542_pnp_remove(struct pnp_dev *pdev)
  938. {
  939. aha1542_release(pnp_get_drvdata(pdev));
  940. pnp_set_drvdata(pdev, NULL);
  941. }
  942. static struct pnp_driver aha1542_pnp_driver = {
  943. .name = "aha1542",
  944. .id_table = aha1542_pnp_ids,
  945. .probe = aha1542_pnp_probe,
  946. .remove = aha1542_pnp_remove,
  947. };
  948. static int pnp_registered;
  949. #endif /* CONFIG_PNP */
  950. static int __init aha1542_init(void)
  951. {
  952. int ret = 0;
  953. #ifdef CONFIG_PNP
  954. if (isapnp) {
  955. ret = pnp_register_driver(&aha1542_pnp_driver);
  956. if (!ret)
  957. pnp_registered = 1;
  958. }
  959. #endif
  960. ret = isa_register_driver(&aha1542_isa_driver, MAXBOARDS);
  961. if (!ret)
  962. isa_registered = 1;
  963. #ifdef CONFIG_PNP
  964. if (pnp_registered)
  965. ret = 0;
  966. #endif
  967. if (isa_registered)
  968. ret = 0;
  969. return ret;
  970. }
  971. static void __exit aha1542_exit(void)
  972. {
  973. #ifdef CONFIG_PNP
  974. if (pnp_registered)
  975. pnp_unregister_driver(&aha1542_pnp_driver);
  976. #endif
  977. if (isa_registered)
  978. isa_unregister_driver(&aha1542_isa_driver);
  979. }
  980. module_init(aha1542_init);
  981. module_exit(aha1542_exit);