eata_pio.c 28 KB

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