eata_pio.c 28 KB

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