eata_pio.c 28 KB

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