qlogicfas408.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*----------------------------------------------------------------*/
  2. /*
  3. Qlogic linux driver - work in progress. No Warranty express or implied.
  4. Use at your own risk. Support Tort Reform so you won't have to read all
  5. these silly disclaimers.
  6. Copyright 1994, Tom Zerucha.
  7. tz@execpc.com
  8. Additional Code, and much appreciated help by
  9. Michael A. Griffith
  10. grif@cs.ucr.edu
  11. Thanks to Eric Youngdale and Dave Hinds for loadable module and PCMCIA
  12. help respectively, and for suffering through my foolishness during the
  13. debugging process.
  14. Reference Qlogic FAS408 Technical Manual, 53408-510-00A, May 10, 1994
  15. (you can reference it, but it is incomplete and inaccurate in places)
  16. Version 0.46 1/30/97 - kernel 1.2.0+
  17. Functions as standalone, loadable, and PCMCIA driver, the latter from
  18. Dave Hinds' PCMCIA package.
  19. Cleaned up 26/10/2002 by Alan Cox <alan@lxorguk.ukuu.org.uk> as part of the 2.5
  20. SCSI driver cleanup and audit. This driver still needs work on the
  21. following
  22. - Non terminating hardware waits
  23. - Some layering violations with its pcmcia stub
  24. Redistributable under terms of the GNU General Public License
  25. For the avoidance of doubt the "preferred form" of this code is one which
  26. is in an open non patent encumbered format. Where cryptographic key signing
  27. forms part of the process of creating an executable the information
  28. including keys needed to generate an equivalently functional executable
  29. are deemed to be part of the source code.
  30. */
  31. #include <linux/module.h>
  32. #include <linux/blkdev.h> /* to get disk capacity */
  33. #include <linux/kernel.h>
  34. #include <linux/string.h>
  35. #include <linux/init.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/ioport.h>
  38. #include <linux/proc_fs.h>
  39. #include <linux/unistd.h>
  40. #include <linux/spinlock.h>
  41. #include <linux/stat.h>
  42. #include <asm/io.h>
  43. #include <asm/irq.h>
  44. #include <asm/dma.h>
  45. #include "scsi.h"
  46. #include <scsi/scsi_host.h>
  47. #include "qlogicfas408.h"
  48. /*----------------------------------------------------------------*/
  49. static int qlcfg5 = (XTALFREQ << 5); /* 15625/512 */
  50. static int qlcfg6 = SYNCXFRPD;
  51. static int qlcfg7 = SYNCOFFST;
  52. static int qlcfg8 = (SLOWCABLE << 7) | (QL_ENABLE_PARITY << 4);
  53. static int qlcfg9 = ((XTALFREQ + 4) / 5);
  54. static int qlcfgc = (FASTCLK << 3) | (FASTSCSI << 4);
  55. /*----------------------------------------------------------------*/
  56. /*----------------------------------------------------------------*/
  57. /* local functions */
  58. /*----------------------------------------------------------------*/
  59. /* error recovery - reset everything */
  60. static void ql_zap(struct qlogicfas408_priv *priv)
  61. {
  62. int x;
  63. int qbase = priv->qbase;
  64. int int_type = priv->int_type;
  65. x = inb(qbase + 0xd);
  66. REG0;
  67. outb(3, qbase + 3); /* reset SCSI */
  68. outb(2, qbase + 3); /* reset chip */
  69. if (x & 0x80)
  70. REG1;
  71. }
  72. /*
  73. * Do a pseudo-dma tranfer
  74. */
  75. static int ql_pdma(struct qlogicfas408_priv *priv, int phase, char *request, int reqlen)
  76. {
  77. int j;
  78. int qbase = priv->qbase;
  79. j = 0;
  80. if (phase & 1) { /* in */
  81. #if QL_TURBO_PDMA
  82. rtrc(4)
  83. /* empty fifo in large chunks */
  84. if (reqlen >= 128 && (inb(qbase + 8) & 2)) { /* full */
  85. insl(qbase + 4, request, 32);
  86. reqlen -= 128;
  87. request += 128;
  88. }
  89. while (reqlen >= 84 && !(j & 0xc0)) /* 2/3 */
  90. if ((j = inb(qbase + 8)) & 4)
  91. {
  92. insl(qbase + 4, request, 21);
  93. reqlen -= 84;
  94. request += 84;
  95. }
  96. if (reqlen >= 44 && (inb(qbase + 8) & 8)) { /* 1/3 */
  97. insl(qbase + 4, request, 11);
  98. reqlen -= 44;
  99. request += 44;
  100. }
  101. #endif
  102. /* until both empty and int (or until reclen is 0) */
  103. rtrc(7)
  104. j = 0;
  105. while (reqlen && !((j & 0x10) && (j & 0xc0)))
  106. {
  107. /* while bytes to receive and not empty */
  108. j &= 0xc0;
  109. while (reqlen && !((j = inb(qbase + 8)) & 0x10))
  110. {
  111. *request++ = inb(qbase + 4);
  112. reqlen--;
  113. }
  114. if (j & 0x10)
  115. j = inb(qbase + 8);
  116. }
  117. } else { /* out */
  118. #if QL_TURBO_PDMA
  119. rtrc(4)
  120. if (reqlen >= 128 && inb(qbase + 8) & 0x10) { /* empty */
  121. outsl(qbase + 4, request, 32);
  122. reqlen -= 128;
  123. request += 128;
  124. }
  125. while (reqlen >= 84 && !(j & 0xc0)) /* 1/3 */
  126. if (!((j = inb(qbase + 8)) & 8)) {
  127. outsl(qbase + 4, request, 21);
  128. reqlen -= 84;
  129. request += 84;
  130. }
  131. if (reqlen >= 40 && !(inb(qbase + 8) & 4)) { /* 2/3 */
  132. outsl(qbase + 4, request, 10);
  133. reqlen -= 40;
  134. request += 40;
  135. }
  136. #endif
  137. /* until full and int (or until reclen is 0) */
  138. rtrc(7)
  139. j = 0;
  140. while (reqlen && !((j & 2) && (j & 0xc0))) {
  141. /* while bytes to send and not full */
  142. while (reqlen && !((j = inb(qbase + 8)) & 2))
  143. {
  144. outb(*request++, qbase + 4);
  145. reqlen--;
  146. }
  147. if (j & 2)
  148. j = inb(qbase + 8);
  149. }
  150. }
  151. /* maybe return reqlen */
  152. return inb(qbase + 8) & 0xc0;
  153. }
  154. /*
  155. * Wait for interrupt flag (polled - not real hardware interrupt)
  156. */
  157. static int ql_wai(struct qlogicfas408_priv *priv)
  158. {
  159. int k;
  160. int qbase = priv->qbase;
  161. unsigned long i;
  162. k = 0;
  163. i = jiffies + WATCHDOG;
  164. while (time_before(jiffies, i) && !priv->qabort &&
  165. !((k = inb(qbase + 4)) & 0xe0)) {
  166. barrier();
  167. cpu_relax();
  168. }
  169. if (time_after_eq(jiffies, i))
  170. return (DID_TIME_OUT);
  171. if (priv->qabort)
  172. return (priv->qabort == 1 ? DID_ABORT : DID_RESET);
  173. if (k & 0x60)
  174. ql_zap(priv);
  175. if (k & 0x20)
  176. return (DID_PARITY);
  177. if (k & 0x40)
  178. return (DID_ERROR);
  179. return 0;
  180. }
  181. /*
  182. * Initiate scsi command - queueing handler
  183. * caller must hold host lock
  184. */
  185. static void ql_icmd(struct scsi_cmnd *cmd)
  186. {
  187. struct qlogicfas408_priv *priv = get_priv_by_cmd(cmd);
  188. int qbase = priv->qbase;
  189. int int_type = priv->int_type;
  190. unsigned int i;
  191. priv->qabort = 0;
  192. REG0;
  193. /* clearing of interrupts and the fifo is needed */
  194. inb(qbase + 5); /* clear interrupts */
  195. if (inb(qbase + 5)) /* if still interrupting */
  196. outb(2, qbase + 3); /* reset chip */
  197. else if (inb(qbase + 7) & 0x1f)
  198. outb(1, qbase + 3); /* clear fifo */
  199. while (inb(qbase + 5)); /* clear ints */
  200. REG1;
  201. outb(1, qbase + 8); /* set for PIO pseudo DMA */
  202. outb(0, qbase + 0xb); /* disable ints */
  203. inb(qbase + 8); /* clear int bits */
  204. REG0;
  205. outb(0x40, qbase + 0xb); /* enable features */
  206. /* configurables */
  207. outb(qlcfgc, qbase + 0xc);
  208. /* config: no reset interrupt, (initiator) bus id */
  209. outb(0x40 | qlcfg8 | priv->qinitid, qbase + 8);
  210. outb(qlcfg7, qbase + 7);
  211. outb(qlcfg6, qbase + 6);
  212. /**/ outb(qlcfg5, qbase + 5); /* select timer */
  213. outb(qlcfg9 & 7, qbase + 9); /* prescaler */
  214. /* outb(0x99, qbase + 5); */
  215. outb(scmd_id(cmd), qbase + 4);
  216. for (i = 0; i < cmd->cmd_len; i++)
  217. outb(cmd->cmnd[i], qbase + 2);
  218. priv->qlcmd = cmd;
  219. outb(0x41, qbase + 3); /* select and send command */
  220. }
  221. /*
  222. * Process scsi command - usually after interrupt
  223. */
  224. static unsigned int ql_pcmd(struct scsi_cmnd *cmd)
  225. {
  226. unsigned int i, j;
  227. unsigned long k;
  228. unsigned int result; /* ultimate return result */
  229. unsigned int status; /* scsi returned status */
  230. unsigned int message; /* scsi returned message */
  231. unsigned int phase; /* recorded scsi phase */
  232. unsigned int reqlen; /* total length of transfer */
  233. char *buf;
  234. struct qlogicfas408_priv *priv = get_priv_by_cmd(cmd);
  235. int qbase = priv->qbase;
  236. int int_type = priv->int_type;
  237. rtrc(1)
  238. j = inb(qbase + 6);
  239. i = inb(qbase + 5);
  240. if (i == 0x20) {
  241. return (DID_NO_CONNECT << 16);
  242. }
  243. i |= inb(qbase + 5); /* the 0x10 bit can be set after the 0x08 */
  244. if (i != 0x18) {
  245. printk(KERN_ERR "Ql:Bad Interrupt status:%02x\n", i);
  246. ql_zap(priv);
  247. return (DID_BAD_INTR << 16);
  248. }
  249. j &= 7; /* j = inb( qbase + 7 ) >> 5; */
  250. /* correct status is supposed to be step 4 */
  251. /* it sometimes returns step 3 but with 0 bytes left to send */
  252. /* We can try stuffing the FIFO with the max each time, but we will get a
  253. sequence of 3 if any bytes are left (but we do flush the FIFO anyway */
  254. if (j != 3 && j != 4) {
  255. printk(KERN_ERR "Ql:Bad sequence for command %d, int %02X, cmdleft = %d\n",
  256. j, i, inb(qbase + 7) & 0x1f);
  257. ql_zap(priv);
  258. return (DID_ERROR << 16);
  259. }
  260. result = DID_OK;
  261. if (inb(qbase + 7) & 0x1f) /* if some bytes in fifo */
  262. outb(1, qbase + 3); /* clear fifo */
  263. /* note that request_bufflen is the total xfer size when sg is used */
  264. reqlen = scsi_bufflen(cmd);
  265. /* note that it won't work if transfers > 16M are requested */
  266. if (reqlen && !((phase = inb(qbase + 4)) & 6)) { /* data phase */
  267. struct scatterlist *sg;
  268. rtrc(2)
  269. outb(reqlen, qbase); /* low-mid xfer cnt */
  270. outb(reqlen >> 8, qbase + 1); /* low-mid xfer cnt */
  271. outb(reqlen >> 16, qbase + 0xe); /* high xfer cnt */
  272. outb(0x90, qbase + 3); /* command do xfer */
  273. /* PIO pseudo DMA to buffer or sglist */
  274. REG1;
  275. scsi_for_each_sg(cmd, sg, scsi_sg_count(cmd), i) {
  276. if (priv->qabort) {
  277. REG0;
  278. return ((priv->qabort == 1 ?
  279. DID_ABORT : DID_RESET) << 16);
  280. }
  281. buf = sg_virt(sg);
  282. if (ql_pdma(priv, phase, buf, sg->length))
  283. break;
  284. }
  285. REG0;
  286. rtrc(2)
  287. /*
  288. * Wait for irq (split into second state of irq handler
  289. * if this can take time)
  290. */
  291. if ((k = ql_wai(priv)))
  292. return (k << 16);
  293. k = inb(qbase + 5); /* should be 0x10, bus service */
  294. }
  295. /*
  296. * Enter Status (and Message In) Phase
  297. */
  298. k = jiffies + WATCHDOG;
  299. while (time_before(jiffies, k) && !priv->qabort &&
  300. !(inb(qbase + 4) & 6))
  301. cpu_relax(); /* wait for status phase */
  302. if (time_after_eq(jiffies, k)) {
  303. ql_zap(priv);
  304. return (DID_TIME_OUT << 16);
  305. }
  306. /* FIXME: timeout ?? */
  307. while (inb(qbase + 5))
  308. cpu_relax(); /* clear pending ints */
  309. if (priv->qabort)
  310. return ((priv->qabort == 1 ? DID_ABORT : DID_RESET) << 16);
  311. outb(0x11, qbase + 3); /* get status and message */
  312. if ((k = ql_wai(priv)))
  313. return (k << 16);
  314. i = inb(qbase + 5); /* get chip irq stat */
  315. j = inb(qbase + 7) & 0x1f; /* and bytes rec'd */
  316. status = inb(qbase + 2);
  317. message = inb(qbase + 2);
  318. /*
  319. * Should get function complete int if Status and message, else
  320. * bus serv if only status
  321. */
  322. if (!((i == 8 && j == 2) || (i == 0x10 && j == 1))) {
  323. printk(KERN_ERR "Ql:Error during status phase, int=%02X, %d bytes recd\n", i, j);
  324. result = DID_ERROR;
  325. }
  326. outb(0x12, qbase + 3); /* done, disconnect */
  327. rtrc(1)
  328. if ((k = ql_wai(priv)))
  329. return (k << 16);
  330. /*
  331. * Should get bus service interrupt and disconnect interrupt
  332. */
  333. i = inb(qbase + 5); /* should be bus service */
  334. while (!priv->qabort && ((i & 0x20) != 0x20)) {
  335. barrier();
  336. cpu_relax();
  337. i |= inb(qbase + 5);
  338. }
  339. rtrc(0)
  340. if (priv->qabort)
  341. return ((priv->qabort == 1 ? DID_ABORT : DID_RESET) << 16);
  342. return (result << 16) | (message << 8) | (status & STATUS_MASK);
  343. }
  344. /*
  345. * Interrupt handler
  346. */
  347. static void ql_ihandl(void *dev_id)
  348. {
  349. struct scsi_cmnd *icmd;
  350. struct Scsi_Host *host = dev_id;
  351. struct qlogicfas408_priv *priv = get_priv_by_host(host);
  352. int qbase = priv->qbase;
  353. REG0;
  354. if (!(inb(qbase + 4) & 0x80)) /* false alarm? */
  355. return;
  356. if (priv->qlcmd == NULL) { /* no command to process? */
  357. int i;
  358. i = 16;
  359. while (i-- && inb(qbase + 5)); /* maybe also ql_zap() */
  360. return;
  361. }
  362. icmd = priv->qlcmd;
  363. icmd->result = ql_pcmd(icmd);
  364. priv->qlcmd = NULL;
  365. /*
  366. * If result is CHECK CONDITION done calls qcommand to request
  367. * sense
  368. */
  369. (icmd->scsi_done) (icmd);
  370. }
  371. irqreturn_t qlogicfas408_ihandl(int irq, void *dev_id)
  372. {
  373. unsigned long flags;
  374. struct Scsi_Host *host = dev_id;
  375. spin_lock_irqsave(host->host_lock, flags);
  376. ql_ihandl(dev_id);
  377. spin_unlock_irqrestore(host->host_lock, flags);
  378. return IRQ_HANDLED;
  379. }
  380. /*
  381. * Queued command
  382. */
  383. static int qlogicfas408_queuecommand_lck(struct scsi_cmnd *cmd,
  384. void (*done) (struct scsi_cmnd *))
  385. {
  386. struct qlogicfas408_priv *priv = get_priv_by_cmd(cmd);
  387. if (scmd_id(cmd) == priv->qinitid) {
  388. cmd->result = DID_BAD_TARGET << 16;
  389. done(cmd);
  390. return 0;
  391. }
  392. cmd->scsi_done = done;
  393. /* wait for the last command's interrupt to finish */
  394. while (priv->qlcmd != NULL) {
  395. barrier();
  396. cpu_relax();
  397. }
  398. ql_icmd(cmd);
  399. return 0;
  400. }
  401. DEF_SCSI_QCMD(qlogicfas408_queuecommand)
  402. /*
  403. * Return bios parameters
  404. */
  405. int qlogicfas408_biosparam(struct scsi_device *disk, struct block_device *dev,
  406. sector_t capacity, int ip[])
  407. {
  408. /* This should mimic the DOS Qlogic driver's behavior exactly */
  409. ip[0] = 0x40;
  410. ip[1] = 0x20;
  411. ip[2] = (unsigned long) capacity / (ip[0] * ip[1]);
  412. if (ip[2] > 1024) {
  413. ip[0] = 0xff;
  414. ip[1] = 0x3f;
  415. ip[2] = (unsigned long) capacity / (ip[0] * ip[1]);
  416. #if 0
  417. if (ip[2] > 1023)
  418. ip[2] = 1023;
  419. #endif
  420. }
  421. return 0;
  422. }
  423. /*
  424. * Abort a command in progress
  425. */
  426. int qlogicfas408_abort(struct scsi_cmnd *cmd)
  427. {
  428. struct qlogicfas408_priv *priv = get_priv_by_cmd(cmd);
  429. priv->qabort = 1;
  430. ql_zap(priv);
  431. return SUCCESS;
  432. }
  433. /*
  434. * Reset SCSI bus
  435. * FIXME: This function is invoked with cmd = NULL directly by
  436. * the PCMCIA qlogic_stub code. This wants fixing
  437. */
  438. int qlogicfas408_bus_reset(struct scsi_cmnd *cmd)
  439. {
  440. struct qlogicfas408_priv *priv = get_priv_by_cmd(cmd);
  441. unsigned long flags;
  442. priv->qabort = 2;
  443. spin_lock_irqsave(cmd->device->host->host_lock, flags);
  444. ql_zap(priv);
  445. spin_unlock_irqrestore(cmd->device->host->host_lock, flags);
  446. return SUCCESS;
  447. }
  448. /*
  449. * Return info string
  450. */
  451. const char *qlogicfas408_info(struct Scsi_Host *host)
  452. {
  453. struct qlogicfas408_priv *priv = get_priv_by_host(host);
  454. return priv->qinfo;
  455. }
  456. /*
  457. * Get type of chip
  458. */
  459. int qlogicfas408_get_chip_type(int qbase, int int_type)
  460. {
  461. REG1;
  462. return inb(qbase + 0xe) & 0xf8;
  463. }
  464. /*
  465. * Perform initialization tasks
  466. */
  467. void qlogicfas408_setup(int qbase, int id, int int_type)
  468. {
  469. outb(1, qbase + 8); /* set for PIO pseudo DMA */
  470. REG0;
  471. outb(0x40 | qlcfg8 | id, qbase + 8); /* (ini) bus id, disable scsi rst */
  472. outb(qlcfg5, qbase + 5); /* select timer */
  473. outb(qlcfg9, qbase + 9); /* prescaler */
  474. #if QL_RESET_AT_START
  475. outb(3, qbase + 3);
  476. REG1;
  477. /* FIXME: timeout */
  478. while (inb(qbase + 0xf) & 4)
  479. cpu_relax();
  480. REG0;
  481. #endif
  482. }
  483. /*
  484. * Checks if this is a QLogic FAS 408
  485. */
  486. int qlogicfas408_detect(int qbase, int int_type)
  487. {
  488. REG1;
  489. return (((inb(qbase + 0xe) ^ inb(qbase + 0xe)) == 7) &&
  490. ((inb(qbase + 0xe) ^ inb(qbase + 0xe)) == 7));
  491. }
  492. /*
  493. * Disable interrupts
  494. */
  495. void qlogicfas408_disable_ints(struct qlogicfas408_priv *priv)
  496. {
  497. int qbase = priv->qbase;
  498. int int_type = priv->int_type;
  499. REG1;
  500. outb(0, qbase + 0xb); /* disable ints */
  501. }
  502. /*
  503. * Init and exit functions
  504. */
  505. static int __init qlogicfas408_init(void)
  506. {
  507. return 0;
  508. }
  509. static void __exit qlogicfas408_exit(void)
  510. {
  511. }
  512. MODULE_AUTHOR("Tom Zerucha, Michael Griffith");
  513. MODULE_DESCRIPTION("Driver for the Qlogic FAS SCSI controllers");
  514. MODULE_LICENSE("GPL");
  515. module_init(qlogicfas408_init);
  516. module_exit(qlogicfas408_exit);
  517. EXPORT_SYMBOL(qlogicfas408_info);
  518. EXPORT_SYMBOL(qlogicfas408_queuecommand);
  519. EXPORT_SYMBOL(qlogicfas408_abort);
  520. EXPORT_SYMBOL(qlogicfas408_bus_reset);
  521. EXPORT_SYMBOL(qlogicfas408_biosparam);
  522. EXPORT_SYMBOL(qlogicfas408_ihandl);
  523. EXPORT_SYMBOL(qlogicfas408_get_chip_type);
  524. EXPORT_SYMBOL(qlogicfas408_setup);
  525. EXPORT_SYMBOL(qlogicfas408_detect);
  526. EXPORT_SYMBOL(qlogicfas408_disable_ints);