eata_pio.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /************************************************************
  2. * *
  3. * Linux EATA SCSI PIO driver *
  4. * *
  5. * based on the CAM document CAM/89-004 rev. 2.0c, *
  6. * DPT's driver kit, some internal documents and source, *
  7. * and several other Linux scsi drivers and kernel docs. *
  8. * *
  9. * The driver currently: *
  10. * -supports all EATA-PIO boards *
  11. * -only supports DASD devices *
  12. * *
  13. * (c)1993-96 Michael Neuffer, Alfred Arnold *
  14. * neuffer@goofy.zdv.uni-mainz.de *
  15. * a.arnold@kfa-juelich.de *
  16. * *
  17. * Updated 2002 by Alan Cox <alan@lxorguk.ukuu.org.uk> for *
  18. * Linux 2.5.x and the newer locking and error handling *
  19. * *
  20. * This program is free software; you can redistribute it *
  21. * and/or modify it under the terms of the GNU General *
  22. * Public License as published by the Free Software *
  23. * Foundation; either version 2 of the License, or *
  24. * (at your option) any later version. *
  25. * *
  26. * This program is distributed in the hope that it will be *
  27. * useful, but WITHOUT ANY WARRANTY; without even the *
  28. * implied warranty of MERCHANTABILITY or FITNESS FOR A *
  29. * PARTICULAR PURPOSE. See the GNU General Public License *
  30. * for more details. *
  31. * *
  32. * You should have received a copy of the GNU General *
  33. * Public License along with this kernel; if not, write to *
  34. * the Free Software Foundation, Inc., 675 Mass Ave, *
  35. * Cambridge, MA 02139, USA. *
  36. * *
  37. * For the avoidance of doubt the "preferred form" of this *
  38. * code is one which is in an open non patent encumbered *
  39. * format. Where cryptographic key signing forms part of *
  40. * the process of creating an executable the information *
  41. * including keys needed to generate an equivalently *
  42. * functional executable are deemed to be part of the *
  43. * source code are deemed to be part of the source code. *
  44. * *
  45. ************************************************************
  46. * last change: 2002/11/02 OS: Linux 2.5.45 *
  47. ************************************************************/
  48. #include <linux/module.h>
  49. #include <linux/kernel.h>
  50. #include <linux/string.h>
  51. #include <linux/ioport.h>
  52. #include <linux/in.h>
  53. #include <linux/pci.h>
  54. #include <linux/proc_fs.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/blkdev.h>
  57. #include <linux/spinlock.h>
  58. #include <linux/delay.h>
  59. #include <asm/io.h>
  60. #include <scsi/scsi.h>
  61. #include <scsi/scsi_cmnd.h>
  62. #include <scsi/scsi_device.h>
  63. #include <scsi/scsi_host.h>
  64. #include "eata_generic.h"
  65. #include "eata_pio.h"
  66. static unsigned int ISAbases[MAXISA] = {
  67. 0x1F0, 0x170, 0x330, 0x230
  68. };
  69. static unsigned int ISAirqs[MAXISA] = {
  70. 14, 12, 15, 11
  71. };
  72. static unsigned char EISAbases[] = {
  73. 1, 1, 1, 1, 1, 1, 1, 1,
  74. 1, 1, 1, 1, 1, 1, 1, 1
  75. };
  76. static unsigned int registered_HBAs;
  77. static struct Scsi_Host *last_HBA;
  78. static struct Scsi_Host *first_HBA;
  79. static unsigned char reg_IRQ[16];
  80. static unsigned char reg_IRQL[16];
  81. static unsigned long int_counter;
  82. static unsigned long queue_counter;
  83. static struct scsi_host_template driver_template;
  84. /*
  85. * eata_proc_info
  86. * inout : decides on the direction of the dataflow and the meaning of the
  87. * variables
  88. * buffer: If inout==FALSE data is being written to it else read from it
  89. * *start: If inout==FALSE start of the valid data in the buffer
  90. * offset: If inout==FALSE offset from the beginning of the imaginary file
  91. * from which we start writing into the buffer
  92. * length: If inout==FALSE max number of bytes to be written into the buffer
  93. * else number of bytes in the buffer
  94. */
  95. static int eata_pio_proc_info(struct Scsi_Host *shost, char *buffer, char **start, off_t offset,
  96. int length, int rw)
  97. {
  98. int len = 0;
  99. off_t begin = 0, pos = 0;
  100. if (rw)
  101. return -ENOSYS;
  102. len += sprintf(buffer+len, "EATA (Extended Attachment) PIO driver version: "
  103. "%d.%d%s\n",VER_MAJOR, VER_MINOR, VER_SUB);
  104. len += sprintf(buffer + len, "queued commands: %10ld\n"
  105. "processed interrupts:%10ld\n", queue_counter, int_counter);
  106. len += sprintf(buffer + len, "\nscsi%-2d: HBA %.10s\n",
  107. shost->host_no, SD(shost)->name);
  108. len += sprintf(buffer + len, "Firmware revision: v%s\n",
  109. SD(shost)->revision);
  110. len += sprintf(buffer + len, "IO: PIO\n");
  111. len += sprintf(buffer + len, "Base IO : %#.4x\n", (u32) shost->base);
  112. len += sprintf(buffer + len, "Host Bus: %s\n",
  113. (SD(shost)->bustype == 'P')?"PCI ":
  114. (SD(shost)->bustype == 'E')?"EISA":"ISA ");
  115. pos = begin + len;
  116. if (pos < offset) {
  117. len = 0;
  118. begin = pos;
  119. }
  120. if (pos > offset + length)
  121. goto stop_output;
  122. stop_output:
  123. DBG(DBG_PROC, printk("2pos: %ld offset: %ld len: %d\n", pos, offset, len));
  124. *start = buffer + (offset - begin); /* Start of wanted data */
  125. len -= (offset - begin); /* Start slop */
  126. if (len > length)
  127. len = length; /* Ending slop */
  128. DBG(DBG_PROC, printk("3pos: %ld offset: %ld len: %d\n", pos, offset, len));
  129. return len;
  130. }
  131. static int eata_pio_release(struct Scsi_Host *sh)
  132. {
  133. hostdata *hd = SD(sh);
  134. if (sh->irq && reg_IRQ[sh->irq] == 1)
  135. free_irq(sh->irq, NULL);
  136. else
  137. reg_IRQ[sh->irq]--;
  138. if (SD(sh)->channel == 0) {
  139. if (sh->io_port && sh->n_io_port)
  140. release_region(sh->io_port, sh->n_io_port);
  141. }
  142. /* At this point the PCI reference can go */
  143. if (hd->pdev)
  144. pci_dev_put(hd->pdev);
  145. return 1;
  146. }
  147. static void IncStat(struct scsi_pointer *SCp, unsigned int Increment)
  148. {
  149. SCp->ptr += Increment;
  150. if ((SCp->this_residual -= Increment) == 0) {
  151. if ((--SCp->buffers_residual) == 0)
  152. SCp->Status = 0;
  153. else {
  154. SCp->buffer++;
  155. SCp->ptr = sg_virt(SCp->buffer);
  156. SCp->this_residual = SCp->buffer->length;
  157. }
  158. }
  159. }
  160. static irqreturn_t eata_pio_int_handler(int irq, void *dev_id);
  161. static irqreturn_t do_eata_pio_int_handler(int irq, void *dev_id)
  162. {
  163. unsigned long flags;
  164. struct Scsi_Host *dev = dev_id;
  165. irqreturn_t ret;
  166. spin_lock_irqsave(dev->host_lock, flags);
  167. ret = eata_pio_int_handler(irq, dev_id);
  168. spin_unlock_irqrestore(dev->host_lock, flags);
  169. return ret;
  170. }
  171. static irqreturn_t eata_pio_int_handler(int irq, void *dev_id)
  172. {
  173. unsigned int eata_stat = 0xfffff;
  174. struct scsi_cmnd *cmd;
  175. hostdata *hd;
  176. struct eata_ccb *cp;
  177. unsigned long base;
  178. unsigned int x, z;
  179. struct Scsi_Host *sh;
  180. unsigned short zwickel = 0;
  181. unsigned char stat, odd;
  182. irqreturn_t ret = IRQ_NONE;
  183. for (x = 1, sh = first_HBA; x <= registered_HBAs; x++, sh = SD(sh)->prev)
  184. {
  185. if (sh->irq != irq)
  186. continue;
  187. if (inb(sh->base + HA_RSTATUS) & HA_SBUSY)
  188. continue;
  189. int_counter++;
  190. ret = IRQ_HANDLED;
  191. hd = SD(sh);
  192. cp = &hd->ccb[0];
  193. cmd = cp->cmd;
  194. base = cmd->device->host->base;
  195. do {
  196. stat = inb(base + HA_RSTATUS);
  197. if (stat & HA_SDRQ) {
  198. if (cp->DataIn) {
  199. z = 256;
  200. odd = 0;
  201. while ((cmd->SCp.Status) && ((z > 0) || (odd))) {
  202. if (odd) {
  203. *(cmd->SCp.ptr) = zwickel >> 8;
  204. IncStat(&cmd->SCp, 1);
  205. odd = 0;
  206. }
  207. x = min_t(unsigned int, z, cmd->SCp.this_residual / 2);
  208. insw(base + HA_RDATA, cmd->SCp.ptr, x);
  209. z -= x;
  210. IncStat(&cmd->SCp, 2 * x);
  211. if ((z > 0) && (cmd->SCp.this_residual == 1)) {
  212. zwickel = inw(base + HA_RDATA);
  213. *(cmd->SCp.ptr) = zwickel & 0xff;
  214. IncStat(&cmd->SCp, 1);
  215. z--;
  216. odd = 1;
  217. }
  218. }
  219. while (z > 0) {
  220. zwickel = inw(base + HA_RDATA);
  221. z--;
  222. }
  223. } else { /* cp->DataOut */
  224. odd = 0;
  225. z = 256;
  226. while ((cmd->SCp.Status) && ((z > 0) || (odd))) {
  227. if (odd) {
  228. zwickel += *(cmd->SCp.ptr) << 8;
  229. IncStat(&cmd->SCp, 1);
  230. outw(zwickel, base + HA_RDATA);
  231. z--;
  232. odd = 0;
  233. }
  234. x = min_t(unsigned int, z, cmd->SCp.this_residual / 2);
  235. outsw(base + HA_RDATA, cmd->SCp.ptr, x);
  236. z -= x;
  237. IncStat(&cmd->SCp, 2 * x);
  238. if ((z > 0) && (cmd->SCp.this_residual == 1)) {
  239. zwickel = *(cmd->SCp.ptr);
  240. zwickel &= 0xff;
  241. IncStat(&cmd->SCp, 1);
  242. odd = 1;
  243. }
  244. }
  245. while (z > 0 || odd) {
  246. outw(zwickel, base + HA_RDATA);
  247. z--;
  248. odd = 0;
  249. }
  250. }
  251. }
  252. }
  253. while ((stat & HA_SDRQ) || ((stat & HA_SMORE) && hd->moresupport));
  254. /* terminate handler if HBA goes busy again, i.e. transfers
  255. * more data */
  256. if (stat & HA_SBUSY)
  257. break;
  258. /* OK, this is quite stupid, but I haven't found any correct
  259. * way to get HBA&SCSI status so far */
  260. if (!(inb(base + HA_RSTATUS) & HA_SERROR)) {
  261. cmd->result = (DID_OK << 16);
  262. hd->devflags |= (1 << cp->cp_id);
  263. } else if (hd->devflags & (1 << cp->cp_id))
  264. cmd->result = (DID_OK << 16) + 0x02;
  265. else
  266. cmd->result = (DID_NO_CONNECT << 16);
  267. if (cp->status == LOCKED) {
  268. cp->status = FREE;
  269. eata_stat = inb(base + HA_RSTATUS);
  270. printk(KERN_CRIT "eata_pio: int_handler, freeing locked " "queueslot\n");
  271. return ret;
  272. }
  273. #if DBG_INTR2
  274. if (stat != 0x50)
  275. printk(KERN_DEBUG "stat: %#.2x, result: %#.8x\n", stat, cmd->result);
  276. #endif
  277. cp->status = FREE; /* now we can release the slot */
  278. cmd->scsi_done(cmd);
  279. }
  280. return ret;
  281. }
  282. static inline unsigned int eata_pio_send_command(unsigned long base, unsigned char command)
  283. {
  284. unsigned int loop = 50;
  285. while (inb(base + HA_RSTATUS) & HA_SBUSY)
  286. if (--loop == 0)
  287. return 1;
  288. /* Enable interrupts for HBA. It is not the best way to do it at this
  289. * place, but I hope that it doesn't interfere with the IDE driver
  290. * initialization this way */
  291. outb(HA_CTRL_8HEADS, base + HA_CTRLREG);
  292. outb(command, base + HA_WCOMMAND);
  293. return 0;
  294. }
  295. static int eata_pio_queue(struct scsi_cmnd *cmd,
  296. void (*done)(struct scsi_cmnd *))
  297. {
  298. unsigned int x, y;
  299. unsigned long base;
  300. hostdata *hd;
  301. struct Scsi_Host *sh;
  302. struct eata_ccb *cp;
  303. queue_counter++;
  304. hd = HD(cmd);
  305. sh = cmd->device->host;
  306. base = sh->base;
  307. /* use only slot 0, as 2001 can handle only one cmd at a time */
  308. y = x = 0;
  309. if (hd->ccb[y].status != FREE) {
  310. DBG(DBG_QUEUE, printk(KERN_EMERG "can_queue %d, x %d, y %d\n", sh->can_queue, x, y));
  311. #if DEBUG_EATA
  312. panic(KERN_EMERG "eata_pio: run out of queue slots cmdno:%ld " "intrno: %ld\n", queue_counter, int_counter);
  313. #else
  314. panic(KERN_EMERG "eata_pio: run out of queue slots....\n");
  315. #endif
  316. }
  317. cp = &hd->ccb[y];
  318. memset(cp, 0, sizeof(struct eata_ccb));
  319. cp->status = USED; /* claim free slot */
  320. DBG(DBG_QUEUE, scmd_printk(KERN_DEBUG, cmd,
  321. "eata_pio_queue pid %ld, y %d\n",
  322. cmd->serial_number, y));
  323. cmd->scsi_done = (void *) done;
  324. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  325. cp->DataOut = 1; /* Output mode */
  326. else
  327. cp->DataIn = 0; /* Input mode */
  328. cp->Interpret = (cmd->device->id == hd->hostid);
  329. cp->cp_datalen = cpu_to_be32(scsi_bufflen(cmd));
  330. cp->Auto_Req_Sen = 0;
  331. cp->cp_reqDMA = 0;
  332. cp->reqlen = 0;
  333. cp->cp_id = cmd->device->id;
  334. cp->cp_lun = cmd->device->lun;
  335. cp->cp_dispri = 0;
  336. cp->cp_identify = 1;
  337. memcpy(cp->cp_cdb, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
  338. cp->cp_statDMA = 0;
  339. cp->cp_viraddr = cp;
  340. cp->cmd = cmd;
  341. cmd->host_scribble = (char *) &hd->ccb[y];
  342. if (!scsi_bufflen(cmd)) {
  343. cmd->SCp.buffers_residual = 1;
  344. cmd->SCp.ptr = NULL;
  345. cmd->SCp.this_residual = 0;
  346. cmd->SCp.buffer = NULL;
  347. } else {
  348. cmd->SCp.buffer = scsi_sglist(cmd);
  349. cmd->SCp.buffers_residual = scsi_sg_count(cmd);
  350. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  351. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  352. }
  353. cmd->SCp.Status = (cmd->SCp.this_residual != 0); /* TRUE as long as bytes
  354. * are to transfer */
  355. if (eata_pio_send_command(base, EATA_CMD_PIO_SEND_CP)) {
  356. cmd->result = DID_BUS_BUSY << 16;
  357. scmd_printk(KERN_NOTICE, cmd,
  358. "eata_pio_queue pid %ld, HBA busy, "
  359. "returning DID_BUS_BUSY, done.\n", cmd->serial_number);
  360. done(cmd);
  361. cp->status = FREE;
  362. return 0;
  363. }
  364. /* FIXME: timeout */
  365. while (!(inb(base + HA_RSTATUS) & HA_SDRQ))
  366. cpu_relax();
  367. outsw(base + HA_RDATA, cp, hd->cplen);
  368. outb(EATA_CMD_PIO_TRUNC, base + HA_WCOMMAND);
  369. for (x = 0; x < hd->cppadlen; x++)
  370. outw(0, base + HA_RDATA);
  371. DBG(DBG_QUEUE, scmd_printk(KERN_DEBUG, cmd,
  372. "Queued base %#.4lx pid: %ld "
  373. "slot %d irq %d\n", sh->base, cmd->serial_number, y, sh->irq));
  374. return 0;
  375. }
  376. static int eata_pio_abort(struct scsi_cmnd *cmd)
  377. {
  378. unsigned int loop = 100;
  379. DBG(DBG_ABNORM, scmd_printk(KERN_WARNING, cmd,
  380. "eata_pio_abort called pid: %ld\n",
  381. cmd->serial_number));
  382. while (inb(cmd->device->host->base + HA_RAUXSTAT) & HA_ABUSY)
  383. if (--loop == 0) {
  384. printk(KERN_WARNING "eata_pio: abort, timeout error.\n");
  385. return FAILED;
  386. }
  387. if (CD(cmd)->status == FREE) {
  388. DBG(DBG_ABNORM, printk(KERN_WARNING "Returning: SCSI_ABORT_NOT_RUNNING\n"));
  389. return FAILED;
  390. }
  391. if (CD(cmd)->status == USED) {
  392. DBG(DBG_ABNORM, printk(KERN_WARNING "Returning: SCSI_ABORT_BUSY\n"));
  393. /* We want to sleep a bit more here */
  394. return FAILED; /* SNOOZE */
  395. }
  396. if (CD(cmd)->status == RESET) {
  397. printk(KERN_WARNING "eata_pio: abort, command reset error.\n");
  398. return FAILED;
  399. }
  400. if (CD(cmd)->status == LOCKED) {
  401. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio: abort, queue slot " "locked.\n"));
  402. return FAILED;
  403. }
  404. panic("eata_pio: abort: invalid slot status\n");
  405. }
  406. static int eata_pio_host_reset(struct scsi_cmnd *cmd)
  407. {
  408. unsigned int x, limit = 0;
  409. unsigned char success = 0;
  410. struct scsi_cmnd *sp;
  411. struct Scsi_Host *host = cmd->device->host;
  412. DBG(DBG_ABNORM, scmd_printk(KERN_WARNING, cmd,
  413. "eata_pio_reset called pid:%ld\n",
  414. cmd->serial_number));
  415. spin_lock_irq(host->host_lock);
  416. if (HD(cmd)->state == RESET) {
  417. printk(KERN_WARNING "eata_pio_reset: exit, already in reset.\n");
  418. spin_unlock_irq(host->host_lock);
  419. return FAILED;
  420. }
  421. /* force all slots to be free */
  422. for (x = 0; x < cmd->device->host->can_queue; x++) {
  423. if (HD(cmd)->ccb[x].status == FREE)
  424. continue;
  425. sp = HD(cmd)->ccb[x].cmd;
  426. HD(cmd)->ccb[x].status = RESET;
  427. printk(KERN_WARNING "eata_pio_reset: slot %d in reset, pid %ld.\n", x, sp->serial_number);
  428. if (sp == NULL)
  429. panic("eata_pio_reset: slot %d, sp==NULL.\n", x);
  430. }
  431. /* hard reset the HBA */
  432. outb(EATA_CMD_RESET, cmd->device->host->base + HA_WCOMMAND);
  433. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: board reset done.\n"));
  434. HD(cmd)->state = RESET;
  435. spin_unlock_irq(host->host_lock);
  436. msleep(3000);
  437. spin_lock_irq(host->host_lock);
  438. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: interrupts disabled, " "loops %d.\n", limit));
  439. for (x = 0; x < cmd->device->host->can_queue; x++) {
  440. /* Skip slots already set free by interrupt */
  441. if (HD(cmd)->ccb[x].status != RESET)
  442. continue;
  443. sp = HD(cmd)->ccb[x].cmd;
  444. sp->result = DID_RESET << 16;
  445. /* This mailbox is terminated */
  446. printk(KERN_WARNING "eata_pio_reset: reset ccb %d.\n", x);
  447. HD(cmd)->ccb[x].status = FREE;
  448. sp->scsi_done(sp);
  449. }
  450. HD(cmd)->state = 0;
  451. spin_unlock_irq(host->host_lock);
  452. if (success) { /* hmmm... */
  453. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: exit, success.\n"));
  454. return SUCCESS;
  455. } else {
  456. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: exit, wakeup.\n"));
  457. return FAILED;
  458. }
  459. }
  460. static char *get_pio_board_data(unsigned long base, unsigned int irq, unsigned int id, unsigned long cplen, unsigned short cppadlen)
  461. {
  462. struct eata_ccb cp;
  463. static char buff[256];
  464. int z;
  465. memset(&cp, 0, sizeof(struct eata_ccb));
  466. memset(buff, 0, sizeof(buff));
  467. cp.DataIn = 1;
  468. cp.Interpret = 1; /* Interpret command */
  469. cp.cp_datalen = cpu_to_be32(254);
  470. cp.cp_dataDMA = cpu_to_be32(0);
  471. cp.cp_id = id;
  472. cp.cp_lun = 0;
  473. cp.cp_cdb[0] = INQUIRY;
  474. cp.cp_cdb[1] = 0;
  475. cp.cp_cdb[2] = 0;
  476. cp.cp_cdb[3] = 0;
  477. cp.cp_cdb[4] = 254;
  478. cp.cp_cdb[5] = 0;
  479. if (eata_pio_send_command(base, EATA_CMD_PIO_SEND_CP))
  480. return NULL;
  481. while (!(inb(base + HA_RSTATUS) & HA_SDRQ))
  482. cpu_relax();
  483. outsw(base + HA_RDATA, &cp, cplen);
  484. outb(EATA_CMD_PIO_TRUNC, base + HA_WCOMMAND);
  485. for (z = 0; z < cppadlen; z++)
  486. outw(0, base + HA_RDATA);
  487. while (inb(base + HA_RSTATUS) & HA_SBUSY)
  488. cpu_relax();
  489. if (inb(base + HA_RSTATUS) & HA_SERROR)
  490. return NULL;
  491. else if (!(inb(base + HA_RSTATUS) & HA_SDRQ))
  492. return NULL;
  493. else {
  494. insw(base + HA_RDATA, &buff, 127);
  495. while (inb(base + HA_RSTATUS) & HA_SDRQ)
  496. inw(base + HA_RDATA);
  497. return buff;
  498. }
  499. }
  500. static int get_pio_conf_PIO(unsigned long base, struct get_conf *buf)
  501. {
  502. unsigned long loop = HZ / 2;
  503. int z;
  504. unsigned short *p;
  505. if (!request_region(base, 9, "eata_pio"))
  506. return 0;
  507. memset(buf, 0, sizeof(struct get_conf));
  508. while (inb(base + HA_RSTATUS) & HA_SBUSY)
  509. if (--loop == 0)
  510. goto fail;
  511. DBG(DBG_PIO && DBG_PROBE, printk(KERN_DEBUG "Issuing PIO READ CONFIG to HBA at %#lx\n", base));
  512. eata_pio_send_command(base, EATA_CMD_PIO_READ_CONFIG);
  513. loop = 50;
  514. for (p = (unsigned short *) buf; (long) p <= ((long) buf + (sizeof(struct get_conf) / 2)); p++) {
  515. while (!(inb(base + HA_RSTATUS) & HA_SDRQ))
  516. if (--loop == 0)
  517. goto fail;
  518. loop = 50;
  519. *p = inw(base + HA_RDATA);
  520. }
  521. if (inb(base + HA_RSTATUS) & HA_SERROR) {
  522. DBG(DBG_PROBE, printk("eata_dma: get_conf_PIO, error during "
  523. "transfer for HBA at %lx\n", base));
  524. goto fail;
  525. }
  526. if (cpu_to_be32(EATA_SIGNATURE) != buf->signature)
  527. goto fail;
  528. DBG(DBG_PIO && DBG_PROBE, printk(KERN_NOTICE "EATA Controller found "
  529. "at %#4lx EATA Level: %x\n",
  530. base, (unsigned int) (buf->version)));
  531. while (inb(base + HA_RSTATUS) & HA_SDRQ)
  532. inw(base + HA_RDATA);
  533. if (!ALLOW_DMA_BOARDS) {
  534. for (z = 0; z < MAXISA; z++)
  535. if (base == ISAbases[z]) {
  536. buf->IRQ = ISAirqs[z];
  537. break;
  538. }
  539. }
  540. return 1;
  541. fail:
  542. release_region(base, 9);
  543. return 0;
  544. }
  545. static void print_pio_config(struct get_conf *gc)
  546. {
  547. printk("Please check values: (read config data)\n");
  548. printk("LEN: %d ver:%d OCS:%d TAR:%d TRNXFR:%d MORES:%d\n", be32_to_cpu(gc->len), gc->version, gc->OCS_enabled, gc->TAR_support, gc->TRNXFR, gc->MORE_support);
  549. printk("HAAV:%d SCSIID0:%d ID1:%d ID2:%d QUEUE:%d SG:%d SEC:%d\n", gc->HAA_valid, gc->scsi_id[3], gc->scsi_id[2], gc->scsi_id[1], be16_to_cpu(gc->queuesiz), be16_to_cpu(gc->SGsiz), gc->SECOND);
  550. printk("IRQ:%d IRQT:%d FORCADR:%d MCH:%d RIDQ:%d\n", gc->IRQ, gc->IRQ_TR, gc->FORCADR, gc->MAX_CHAN, gc->ID_qest);
  551. }
  552. static unsigned int print_selftest(unsigned int base)
  553. {
  554. unsigned char buffer[512];
  555. #ifdef VERBOSE_SETUP
  556. int z;
  557. #endif
  558. printk("eata_pio: executing controller self test & setup...\n");
  559. while (inb(base + HA_RSTATUS) & HA_SBUSY);
  560. outb(EATA_CMD_PIO_SETUPTEST, base + HA_WCOMMAND);
  561. do {
  562. while (inb(base + HA_RSTATUS) & HA_SBUSY)
  563. /* nothing */ ;
  564. if (inb(base + HA_RSTATUS) & HA_SDRQ) {
  565. insw(base + HA_RDATA, &buffer, 256);
  566. #ifdef VERBOSE_SETUP
  567. /* no beeps please... */
  568. for (z = 0; z < 511 && buffer[z]; z++)
  569. if (buffer[z] != 7)
  570. printk("%c", buffer[z]);
  571. #endif
  572. }
  573. } while (inb(base + HA_RSTATUS) & (HA_SBUSY | HA_SDRQ));
  574. return (!(inb(base + HA_RSTATUS) & HA_SERROR));
  575. }
  576. static int register_pio_HBA(long base, struct get_conf *gc, struct pci_dev *pdev)
  577. {
  578. unsigned long size = 0;
  579. char *buff;
  580. unsigned long cplen;
  581. unsigned short cppadlen;
  582. struct Scsi_Host *sh;
  583. hostdata *hd;
  584. DBG(DBG_REGISTER, print_pio_config(gc));
  585. if (gc->DMA_support) {
  586. printk("HBA at %#.4lx supports DMA. Please use EATA-DMA driver.\n", base);
  587. if (!ALLOW_DMA_BOARDS)
  588. return 0;
  589. }
  590. if ((buff = get_pio_board_data(base, gc->IRQ, gc->scsi_id[3], cplen = (cpu_to_be32(gc->cplen) + 1) / 2, cppadlen = (cpu_to_be16(gc->cppadlen) + 1) / 2)) == NULL) {
  591. printk("HBA at %#lx didn't react on INQUIRY. Sorry.\n", base);
  592. return 0;
  593. }
  594. if (!print_selftest(base) && !ALLOW_DMA_BOARDS) {
  595. printk("HBA at %#lx failed while performing self test & setup.\n", base);
  596. return 0;
  597. }
  598. size = sizeof(hostdata) + (sizeof(struct eata_ccb) * be16_to_cpu(gc->queuesiz));
  599. sh = scsi_register(&driver_template, size);
  600. if (sh == NULL)
  601. return 0;
  602. if (!reg_IRQ[gc->IRQ]) { /* Interrupt already registered ? */
  603. if (!request_irq(gc->IRQ, do_eata_pio_int_handler, IRQF_DISABLED, "EATA-PIO", sh)) {
  604. reg_IRQ[gc->IRQ]++;
  605. if (!gc->IRQ_TR)
  606. reg_IRQL[gc->IRQ] = 1; /* IRQ is edge triggered */
  607. } else {
  608. printk("Couldn't allocate IRQ %d, Sorry.\n", gc->IRQ);
  609. return 0;
  610. }
  611. } else { /* More than one HBA on this IRQ */
  612. if (reg_IRQL[gc->IRQ]) {
  613. printk("Can't support more than one HBA on this IRQ,\n" " if the IRQ is edge triggered. Sorry.\n");
  614. return 0;
  615. } else
  616. reg_IRQ[gc->IRQ]++;
  617. }
  618. hd = SD(sh);
  619. memset(hd->ccb, 0, (sizeof(struct eata_ccb) * be16_to_cpu(gc->queuesiz)));
  620. memset(hd->reads, 0, sizeof(hd->reads));
  621. strlcpy(SD(sh)->vendor, &buff[8], sizeof(SD(sh)->vendor));
  622. strlcpy(SD(sh)->name, &buff[16], sizeof(SD(sh)->name));
  623. SD(sh)->revision[0] = buff[32];
  624. SD(sh)->revision[1] = buff[33];
  625. SD(sh)->revision[2] = buff[34];
  626. SD(sh)->revision[3] = '.';
  627. SD(sh)->revision[4] = buff[35];
  628. SD(sh)->revision[5] = 0;
  629. switch (be32_to_cpu(gc->len)) {
  630. case 0x1c:
  631. SD(sh)->EATA_revision = 'a';
  632. break;
  633. case 0x1e:
  634. SD(sh)->EATA_revision = 'b';
  635. break;
  636. case 0x22:
  637. SD(sh)->EATA_revision = 'c';
  638. break;
  639. case 0x24:
  640. SD(sh)->EATA_revision = 'z';
  641. default:
  642. SD(sh)->EATA_revision = '?';
  643. }
  644. if (be32_to_cpu(gc->len) >= 0x22) {
  645. if (gc->is_PCI)
  646. hd->bustype = IS_PCI;
  647. else if (gc->is_EISA)
  648. hd->bustype = IS_EISA;
  649. else
  650. hd->bustype = IS_ISA;
  651. } else {
  652. if (buff[21] == '4')
  653. hd->bustype = IS_PCI;
  654. else if (buff[21] == '2')
  655. hd->bustype = IS_EISA;
  656. else
  657. hd->bustype = IS_ISA;
  658. }
  659. SD(sh)->cplen = cplen;
  660. SD(sh)->cppadlen = cppadlen;
  661. SD(sh)->hostid = gc->scsi_id[3];
  662. SD(sh)->devflags = 1 << gc->scsi_id[3];
  663. SD(sh)->moresupport = gc->MORE_support;
  664. sh->unique_id = base;
  665. sh->base = base;
  666. sh->io_port = base;
  667. sh->n_io_port = 9;
  668. sh->irq = gc->IRQ;
  669. sh->dma_channel = PIO;
  670. sh->this_id = gc->scsi_id[3];
  671. sh->can_queue = 1;
  672. sh->cmd_per_lun = 1;
  673. sh->sg_tablesize = SG_ALL;
  674. hd->channel = 0;
  675. hd->pdev = pci_dev_get(pdev); /* Keep a PCI reference */
  676. sh->max_id = 8;
  677. sh->max_lun = 8;
  678. if (gc->SECOND)
  679. hd->primary = 0;
  680. else
  681. hd->primary = 1;
  682. hd->next = NULL; /* build a linked list of all HBAs */
  683. hd->prev = last_HBA;
  684. if (hd->prev != NULL)
  685. SD(hd->prev)->next = sh;
  686. last_HBA = sh;
  687. if (first_HBA == NULL)
  688. first_HBA = sh;
  689. registered_HBAs++;
  690. return (1);
  691. }
  692. static void find_pio_ISA(struct get_conf *buf)
  693. {
  694. int i;
  695. for (i = 0; i < MAXISA; i++) {
  696. if (!ISAbases[i])
  697. continue;
  698. if (!get_pio_conf_PIO(ISAbases[i], buf))
  699. continue;
  700. if (!register_pio_HBA(ISAbases[i], buf, NULL))
  701. release_region(ISAbases[i], 9);
  702. else
  703. ISAbases[i] = 0;
  704. }
  705. return;
  706. }
  707. static void find_pio_EISA(struct get_conf *buf)
  708. {
  709. u32 base;
  710. int i;
  711. #ifdef CHECKPAL
  712. u8 pal1, pal2, pal3;
  713. #endif
  714. for (i = 0; i < MAXEISA; i++) {
  715. if (EISAbases[i]) { /* Still a possibility ? */
  716. base = 0x1c88 + (i * 0x1000);
  717. #ifdef CHECKPAL
  718. pal1 = inb((u16) base - 8);
  719. pal2 = inb((u16) base - 7);
  720. pal3 = inb((u16) base - 6);
  721. if (((pal1 == 0x12) && (pal2 == 0x14)) || ((pal1 == 0x38) && (pal2 == 0xa3) && (pal3 == 0x82)) || ((pal1 == 0x06) && (pal2 == 0x94) && (pal3 == 0x24))) {
  722. DBG(DBG_PROBE, printk(KERN_NOTICE "EISA EATA id tags found: " "%x %x %x \n", (int) pal1, (int) pal2, (int) pal3));
  723. #endif
  724. if (get_pio_conf_PIO(base, buf)) {
  725. DBG(DBG_PROBE && DBG_EISA, print_pio_config(buf));
  726. if (buf->IRQ) {
  727. if (!register_pio_HBA(base, buf, NULL))
  728. release_region(base, 9);
  729. } else {
  730. printk(KERN_NOTICE "eata_dma: No valid IRQ. HBA " "removed from list\n");
  731. release_region(base, 9);
  732. }
  733. }
  734. /* Nothing found here so we take it from the list */
  735. EISAbases[i] = 0;
  736. #ifdef CHECKPAL
  737. }
  738. #endif
  739. }
  740. }
  741. return;
  742. }
  743. static void find_pio_PCI(struct get_conf *buf)
  744. {
  745. #ifndef CONFIG_PCI
  746. printk("eata_dma: kernel PCI support not enabled. Skipping scan for PCI HBAs.\n");
  747. #else
  748. struct pci_dev *dev = NULL;
  749. unsigned long base, x;
  750. while ((dev = pci_get_device(PCI_VENDOR_ID_DPT, PCI_DEVICE_ID_DPT, dev)) != NULL) {
  751. DBG(DBG_PROBE && DBG_PCI, printk("eata_pio: find_PCI, HBA at %s\n", pci_name(dev)));
  752. if (pci_enable_device(dev))
  753. continue;
  754. pci_set_master(dev);
  755. base = pci_resource_flags(dev, 0);
  756. if (base & IORESOURCE_MEM) {
  757. printk("eata_pio: invalid base address of device %s\n", pci_name(dev));
  758. continue;
  759. }
  760. base = pci_resource_start(dev, 0);
  761. /* EISA tag there ? */
  762. if ((inb(base) == 0x12) && (inb(base + 1) == 0x14))
  763. continue; /* Jep, it's forced, so move on */
  764. base += 0x10; /* Now, THIS is the real address */
  765. if (base != 0x1f8) {
  766. /* We didn't find it in the primary search */
  767. if (get_pio_conf_PIO(base, buf)) {
  768. if (buf->FORCADR) { /* If the address is forced */
  769. release_region(base, 9);
  770. continue; /* we'll find it later */
  771. }
  772. /* OK. We made it till here, so we can go now
  773. * and register it. We only have to check and
  774. * eventually remove it from the EISA and ISA list
  775. */
  776. if (!register_pio_HBA(base, buf, dev)) {
  777. release_region(base, 9);
  778. continue;
  779. }
  780. if (base < 0x1000) {
  781. for (x = 0; x < MAXISA; ++x) {
  782. if (ISAbases[x] == base) {
  783. ISAbases[x] = 0;
  784. break;
  785. }
  786. }
  787. } else if ((base & 0x0fff) == 0x0c88) {
  788. x = (base >> 12) & 0x0f;
  789. EISAbases[x] = 0;
  790. }
  791. }
  792. #ifdef CHECK_BLINK
  793. else if (check_blink_state(base)) {
  794. printk("eata_pio: HBA is in BLINK state.\n" "Consult your HBAs manual to correct this.\n");
  795. }
  796. #endif
  797. }
  798. }
  799. #endif /* #ifndef CONFIG_PCI */
  800. }
  801. static int eata_pio_detect(struct scsi_host_template *tpnt)
  802. {
  803. struct Scsi_Host *HBA_ptr;
  804. struct get_conf gc;
  805. int i;
  806. find_pio_PCI(&gc);
  807. find_pio_EISA(&gc);
  808. find_pio_ISA(&gc);
  809. for (i = 0; i <= MAXIRQ; i++)
  810. if (reg_IRQ[i])
  811. request_irq(i, do_eata_pio_int_handler, IRQF_DISABLED, "EATA-PIO", NULL);
  812. HBA_ptr = first_HBA;
  813. if (registered_HBAs != 0) {
  814. printk("EATA (Extended Attachment) PIO driver version: %d.%d%s\n"
  815. "(c) 1993-95 Michael Neuffer, neuffer@goofy.zdv.uni-mainz.de\n" " Alfred Arnold, a.arnold@kfa-juelich.de\n" "This release only supports DASD devices (harddisks)\n", VER_MAJOR, VER_MINOR, VER_SUB);
  816. printk("Registered HBAs:\n");
  817. printk("HBA no. Boardtype: Revis: EATA: Bus: BaseIO: IRQ: Ch: ID: Pr:" " QS: SG: CPL:\n");
  818. for (i = 1; i <= registered_HBAs; i++) {
  819. printk("scsi%-2d: %.10s v%s 2.0%c %s %#.4lx %2d %d %d %c"
  820. " %2d %2d %2d\n",
  821. HBA_ptr->host_no, SD(HBA_ptr)->name, SD(HBA_ptr)->revision,
  822. SD(HBA_ptr)->EATA_revision, (SD(HBA_ptr)->bustype == 'P') ?
  823. "PCI " : (SD(HBA_ptr)->bustype == 'E') ? "EISA" : "ISA ",
  824. HBA_ptr->base, HBA_ptr->irq, SD(HBA_ptr)->channel, HBA_ptr->this_id,
  825. SD(HBA_ptr)->primary ? 'Y' : 'N', HBA_ptr->can_queue,
  826. HBA_ptr->sg_tablesize, HBA_ptr->cmd_per_lun);
  827. HBA_ptr = SD(HBA_ptr)->next;
  828. }
  829. }
  830. return (registered_HBAs);
  831. }
  832. static struct scsi_host_template driver_template = {
  833. .proc_name = "eata_pio",
  834. .name = "EATA (Extended Attachment) PIO driver",
  835. .proc_info = eata_pio_proc_info,
  836. .detect = eata_pio_detect,
  837. .release = eata_pio_release,
  838. .queuecommand = eata_pio_queue,
  839. .eh_abort_handler = eata_pio_abort,
  840. .eh_host_reset_handler = eata_pio_host_reset,
  841. .use_clustering = ENABLE_CLUSTERING,
  842. };
  843. MODULE_AUTHOR("Michael Neuffer, Alfred Arnold");
  844. MODULE_DESCRIPTION("EATA SCSI PIO driver");
  845. MODULE_LICENSE("GPL");
  846. #include "scsi_module.c"