eata_pio.c 28 KB

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