eata_pio.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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, scmd_printk(KERN_DEBUG, cmd,
  333. "eata_pio_queue pid %ld, y %d\n",
  334. cmd->pid, y));
  335. cmd->scsi_done = (void *) done;
  336. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  337. cp->DataOut = 1; /* Output mode */
  338. else
  339. cp->DataIn = 0; /* Input mode */
  340. cp->Interpret = (cmd->device->id == hd->hostid);
  341. cp->cp_datalen = htonl((unsigned long) cmd->request_bufflen);
  342. cp->Auto_Req_Sen = 0;
  343. cp->cp_reqDMA = htonl(0);
  344. cp->reqlen = 0;
  345. cp->cp_id = cmd->device->id;
  346. cp->cp_lun = cmd->device->lun;
  347. cp->cp_dispri = 0;
  348. cp->cp_identify = 1;
  349. memcpy(cp->cp_cdb, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
  350. cp->cp_statDMA = htonl(0);
  351. cp->cp_viraddr = cp;
  352. cp->cmd = cmd;
  353. cmd->host_scribble = (char *) &hd->ccb[y];
  354. if (cmd->use_sg == 0) {
  355. cmd->SCp.buffers_residual = 1;
  356. cmd->SCp.ptr = cmd->request_buffer;
  357. cmd->SCp.this_residual = cmd->request_bufflen;
  358. cmd->SCp.buffer = NULL;
  359. } else {
  360. cmd->SCp.buffer = cmd->request_buffer;
  361. cmd->SCp.buffers_residual = cmd->use_sg;
  362. cmd->SCp.ptr = page_address(cmd->SCp.buffer->page) + cmd->SCp.buffer->offset;
  363. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  364. }
  365. cmd->SCp.Status = (cmd->SCp.this_residual != 0); /* TRUE as long as bytes
  366. * are to transfer */
  367. if (eata_pio_send_command(base, EATA_CMD_PIO_SEND_CP)) {
  368. cmd->result = DID_BUS_BUSY << 16;
  369. scmd_printk(KERN_NOTICE, cmd,
  370. "eata_pio_queue pid %ld, HBA busy, "
  371. "returning DID_BUS_BUSY, done.\n", cmd->pid);
  372. done(cmd);
  373. cp->status = FREE;
  374. return (0);
  375. }
  376. /* FIXME: timeout */
  377. while (!(inb(base + HA_RSTATUS) & HA_SDRQ))
  378. cpu_relax();
  379. outsw(base + HA_RDATA, cp, hd->cplen);
  380. outb(EATA_CMD_PIO_TRUNC, base + HA_WCOMMAND);
  381. for (x = 0; x < hd->cppadlen; x++)
  382. outw(0, base + HA_RDATA);
  383. DBG(DBG_QUEUE, scmd_printk(KERN_DEBUG, cmd,
  384. "Queued base %#.4lx pid: %ld "
  385. "slot %d irq %d\n", (long) sh->base, cmd->pid, y, sh->irq));
  386. return (0);
  387. }
  388. static int eata_pio_abort(struct scsi_cmnd *cmd)
  389. {
  390. uint loop = HZ;
  391. DBG(DBG_ABNORM, scmd_printk(KERN_WARNING, cmd,
  392. "eata_pio_abort called pid: %ld\n",
  393. cmd->pid));
  394. while (inb(cmd->device->host->base + HA_RAUXSTAT) & HA_ABUSY)
  395. if (--loop == 0) {
  396. printk(KERN_WARNING "eata_pio: abort, timeout error.\n");
  397. return FAILED;
  398. }
  399. if (CD(cmd)->status == FREE) {
  400. DBG(DBG_ABNORM, printk(KERN_WARNING "Returning: SCSI_ABORT_NOT_RUNNING\n"));
  401. return FAILED;
  402. }
  403. if (CD(cmd)->status == USED) {
  404. DBG(DBG_ABNORM, printk(KERN_WARNING "Returning: SCSI_ABORT_BUSY\n"));
  405. /* We want to sleep a bit more here */
  406. return FAILED; /* SNOOZE */
  407. }
  408. if (CD(cmd)->status == RESET) {
  409. printk(KERN_WARNING "eata_pio: abort, command reset error.\n");
  410. return FAILED;
  411. }
  412. if (CD(cmd)->status == LOCKED) {
  413. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio: abort, queue slot " "locked.\n"));
  414. return FAILED;
  415. }
  416. panic("eata_pio: abort: invalid slot status\n");
  417. }
  418. static int eata_pio_host_reset(struct scsi_cmnd *cmd)
  419. {
  420. uint x, limit = 0;
  421. unsigned char success = 0;
  422. struct scsi_cmnd *sp;
  423. struct Scsi_Host *host = cmd->device->host;
  424. DBG(DBG_ABNORM, scmd_printk(KERN_WARNING, cmd,
  425. "eata_pio_reset called pid:%ld\n",
  426. cmd->pid));
  427. spin_lock_irq(host->host_lock);
  428. if (HD(cmd)->state == RESET) {
  429. printk(KERN_WARNING "eata_pio_reset: exit, already in reset.\n");
  430. spin_unlock_irq(host->host_lock);
  431. return FAILED;
  432. }
  433. /* force all slots to be free */
  434. for (x = 0; x < cmd->device->host->can_queue; x++) {
  435. if (HD(cmd)->ccb[x].status == FREE)
  436. continue;
  437. sp = HD(cmd)->ccb[x].cmd;
  438. HD(cmd)->ccb[x].status = RESET;
  439. printk(KERN_WARNING "eata_pio_reset: slot %d in reset, pid %ld.\n", x, sp->pid);
  440. if (sp == NULL)
  441. panic("eata_pio_reset: slot %d, sp==NULL.\n", x);
  442. }
  443. /* hard reset the HBA */
  444. outb(EATA_CMD_RESET, (uint) cmd->device->host->base + HA_WCOMMAND);
  445. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: board reset done.\n"));
  446. HD(cmd)->state = RESET;
  447. spin_unlock_irq(host->host_lock);
  448. msleep(3000);
  449. spin_lock_irq(host->host_lock);
  450. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: interrupts disabled, " "loops %d.\n", limit));
  451. for (x = 0; x < cmd->device->host->can_queue; x++) {
  452. /* Skip slots already set free by interrupt */
  453. if (HD(cmd)->ccb[x].status != RESET)
  454. continue;
  455. sp = HD(cmd)->ccb[x].cmd;
  456. sp->result = DID_RESET << 16;
  457. /* This mailbox is terminated */
  458. printk(KERN_WARNING "eata_pio_reset: reset ccb %d.\n", x);
  459. HD(cmd)->ccb[x].status = FREE;
  460. sp->scsi_done(sp);
  461. }
  462. HD(cmd)->state = 0;
  463. spin_unlock_irq(host->host_lock);
  464. if (success) { /* hmmm... */
  465. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: exit, success.\n"));
  466. return SUCCESS;
  467. } else {
  468. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: exit, wakeup.\n"));
  469. return FAILED;
  470. }
  471. }
  472. static char *get_pio_board_data(unsigned long base, uint irq, uint id, unsigned long cplen, unsigned short cppadlen)
  473. {
  474. struct eata_ccb cp;
  475. static char buff[256];
  476. int z;
  477. memset(&cp, 0, sizeof(struct eata_ccb));
  478. memset(buff, 0, sizeof(buff));
  479. cp.DataIn = 1;
  480. cp.Interpret = 1; /* Interpret command */
  481. cp.cp_datalen = htonl(254);
  482. cp.cp_dataDMA = htonl(0);
  483. cp.cp_id = id;
  484. cp.cp_lun = 0;
  485. cp.cp_cdb[0] = INQUIRY;
  486. cp.cp_cdb[1] = 0;
  487. cp.cp_cdb[2] = 0;
  488. cp.cp_cdb[3] = 0;
  489. cp.cp_cdb[4] = 254;
  490. cp.cp_cdb[5] = 0;
  491. if (eata_pio_send_command((uint) base, EATA_CMD_PIO_SEND_CP))
  492. return (NULL);
  493. while (!(inb(base + HA_RSTATUS) & HA_SDRQ));
  494. outsw(base + HA_RDATA, &cp, cplen);
  495. outb(EATA_CMD_PIO_TRUNC, base + HA_WCOMMAND);
  496. for (z = 0; z < cppadlen; z++)
  497. outw(0, base + HA_RDATA);
  498. while (inb(base + HA_RSTATUS) & HA_SBUSY);
  499. if (inb(base + HA_RSTATUS) & HA_SERROR)
  500. return (NULL);
  501. else if (!(inb(base + HA_RSTATUS) & HA_SDRQ))
  502. return (NULL);
  503. else {
  504. insw(base + HA_RDATA, &buff, 127);
  505. while (inb(base + HA_RSTATUS) & HA_SDRQ)
  506. inw(base + HA_RDATA);
  507. return (buff);
  508. }
  509. }
  510. static int get_pio_conf_PIO(u32 base, struct get_conf *buf)
  511. {
  512. unsigned long loop = HZ / 2;
  513. int z;
  514. unsigned short *p;
  515. if (!request_region(base, 9, "eata_pio"))
  516. return 0;
  517. memset(buf, 0, sizeof(struct get_conf));
  518. while (inb(base + HA_RSTATUS) & HA_SBUSY)
  519. if (--loop == 0)
  520. goto fail;
  521. DBG(DBG_PIO && DBG_PROBE, printk(KERN_DEBUG "Issuing PIO READ CONFIG to HBA at %#x\n", base));
  522. eata_pio_send_command(base, EATA_CMD_PIO_READ_CONFIG);
  523. loop = HZ / 2;
  524. for (p = (unsigned short *) buf; (long) p <= ((long) buf + (sizeof(struct get_conf) / 2)); p++) {
  525. while (!(inb(base + HA_RSTATUS) & HA_SDRQ))
  526. if (--loop == 0)
  527. goto fail;
  528. loop = HZ / 2;
  529. *p = inw(base + HA_RDATA);
  530. }
  531. if (inb(base + HA_RSTATUS) & HA_SERROR) {
  532. DBG(DBG_PROBE, printk("eata_dma: get_conf_PIO, error during "
  533. "transfer for HBA at %x\n", base));
  534. goto fail;
  535. }
  536. if (htonl(EATA_SIGNATURE) != buf->signature)
  537. goto fail;
  538. DBG(DBG_PIO && DBG_PROBE, printk(KERN_NOTICE "EATA Controller found "
  539. "at %#4x EATA Level: %x\n",
  540. base, (uint) (buf->version)));
  541. while (inb(base + HA_RSTATUS) & HA_SDRQ)
  542. inw(base + HA_RDATA);
  543. if (!ALLOW_DMA_BOARDS) {
  544. for (z = 0; z < MAXISA; z++)
  545. if (base == ISAbases[z]) {
  546. buf->IRQ = ISAirqs[z];
  547. break;
  548. }
  549. }
  550. return 1;
  551. fail:
  552. release_region(base, 9);
  553. return 0;
  554. }
  555. static void print_pio_config(struct get_conf *gc)
  556. {
  557. printk("Please check values: (read config data)\n");
  558. 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);
  559. 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);
  560. 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);
  561. }
  562. static uint print_selftest(uint base)
  563. {
  564. unsigned char buffer[512];
  565. #ifdef VERBOSE_SETUP
  566. int z;
  567. #endif
  568. printk("eata_pio: executing controller self test & setup...\n");
  569. while (inb(base + HA_RSTATUS) & HA_SBUSY);
  570. outb(EATA_CMD_PIO_SETUPTEST, base + HA_WCOMMAND);
  571. do {
  572. while (inb(base + HA_RSTATUS) & HA_SBUSY)
  573. /* nothing */ ;
  574. if (inb(base + HA_RSTATUS) & HA_SDRQ) {
  575. insw(base + HA_RDATA, &buffer, 256);
  576. #ifdef VERBOSE_SETUP
  577. /* no beeps please... */
  578. for (z = 0; z < 511 && buffer[z]; z++)
  579. if (buffer[z] != 7)
  580. printk("%c", buffer[z]);
  581. #endif
  582. }
  583. } while (inb(base + HA_RSTATUS) & (HA_SBUSY | HA_SDRQ));
  584. return (!(inb(base + HA_RSTATUS) & HA_SERROR));
  585. }
  586. static int register_pio_HBA(long base, struct get_conf *gc)
  587. {
  588. unsigned long size = 0;
  589. char *buff;
  590. unsigned long cplen;
  591. unsigned short cppadlen;
  592. struct Scsi_Host *sh;
  593. hostdata *hd;
  594. DBG(DBG_REGISTER, print_pio_config(gc));
  595. if (gc->DMA_support) {
  596. printk("HBA at %#.4lx supports DMA. Please use EATA-DMA driver.\n", base);
  597. if (!ALLOW_DMA_BOARDS)
  598. return 0;
  599. }
  600. 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) {
  601. printk("HBA at %#lx didn't react on INQUIRY. Sorry.\n", (unsigned long) base);
  602. return 0;
  603. }
  604. if (!print_selftest(base) && !ALLOW_DMA_BOARDS) {
  605. printk("HBA at %#lx failed while performing self test & setup.\n", (unsigned long) base);
  606. return 0;
  607. }
  608. size = sizeof(hostdata) + (sizeof(struct eata_ccb) * ntohs(gc->queuesiz));
  609. sh = scsi_register(&driver_template, size);
  610. if (sh == NULL)
  611. return 0;
  612. if (!reg_IRQ[gc->IRQ]) { /* Interrupt already registered ? */
  613. if (!request_irq(gc->IRQ, do_eata_pio_int_handler, SA_INTERRUPT, "EATA-PIO", sh)) {
  614. reg_IRQ[gc->IRQ]++;
  615. if (!gc->IRQ_TR)
  616. reg_IRQL[gc->IRQ] = 1; /* IRQ is edge triggered */
  617. } else {
  618. printk("Couldn't allocate IRQ %d, Sorry.\n", gc->IRQ);
  619. return 0;
  620. }
  621. } else { /* More than one HBA on this IRQ */
  622. if (reg_IRQL[gc->IRQ]) {
  623. printk("Can't support more than one HBA on this IRQ,\n" " if the IRQ is edge triggered. Sorry.\n");
  624. return 0;
  625. } else
  626. reg_IRQ[gc->IRQ]++;
  627. }
  628. hd = SD(sh);
  629. memset(hd->ccb, 0, (sizeof(struct eata_ccb) * ntohs(gc->queuesiz)));
  630. memset(hd->reads, 0, sizeof(unsigned long) * 26);
  631. strlcpy(SD(sh)->vendor, &buff[8], sizeof(SD(sh)->vendor));
  632. strlcpy(SD(sh)->name, &buff[16], sizeof(SD(sh)->name));
  633. SD(sh)->revision[0] = buff[32];
  634. SD(sh)->revision[1] = buff[33];
  635. SD(sh)->revision[2] = buff[34];
  636. SD(sh)->revision[3] = '.';
  637. SD(sh)->revision[4] = buff[35];
  638. SD(sh)->revision[5] = 0;
  639. switch (ntohl(gc->len)) {
  640. case 0x1c:
  641. SD(sh)->EATA_revision = 'a';
  642. break;
  643. case 0x1e:
  644. SD(sh)->EATA_revision = 'b';
  645. break;
  646. case 0x22:
  647. SD(sh)->EATA_revision = 'c';
  648. break;
  649. case 0x24:
  650. SD(sh)->EATA_revision = 'z';
  651. default:
  652. SD(sh)->EATA_revision = '?';
  653. }
  654. if (ntohl(gc->len) >= 0x22) {
  655. if (gc->is_PCI)
  656. hd->bustype = IS_PCI;
  657. else if (gc->is_EISA)
  658. hd->bustype = IS_EISA;
  659. else
  660. hd->bustype = IS_ISA;
  661. } else {
  662. if (buff[21] == '4')
  663. hd->bustype = IS_PCI;
  664. else if (buff[21] == '2')
  665. hd->bustype = IS_EISA;
  666. else
  667. hd->bustype = IS_ISA;
  668. }
  669. SD(sh)->cplen = cplen;
  670. SD(sh)->cppadlen = cppadlen;
  671. SD(sh)->hostid = gc->scsi_id[3];
  672. SD(sh)->devflags = 1 << gc->scsi_id[3];
  673. SD(sh)->moresupport = gc->MORE_support;
  674. sh->unique_id = base;
  675. sh->base = base;
  676. sh->io_port = base;
  677. sh->n_io_port = 9;
  678. sh->irq = gc->IRQ;
  679. sh->dma_channel = PIO;
  680. sh->this_id = gc->scsi_id[3];
  681. sh->can_queue = 1;
  682. sh->cmd_per_lun = 1;
  683. sh->sg_tablesize = SG_ALL;
  684. hd->channel = 0;
  685. sh->max_id = 8;
  686. sh->max_lun = 8;
  687. if (gc->SECOND)
  688. hd->primary = 0;
  689. else
  690. hd->primary = 1;
  691. sh->unchecked_isa_dma = 0; /* We can only do PIO */
  692. hd->next = NULL; /* build a linked list of all HBAs */
  693. hd->prev = last_HBA;
  694. if (hd->prev != NULL)
  695. SD(hd->prev)->next = sh;
  696. last_HBA = sh;
  697. if (first_HBA == NULL)
  698. first_HBA = sh;
  699. registered_HBAs++;
  700. return (1);
  701. }
  702. static void find_pio_ISA(struct get_conf *buf)
  703. {
  704. int i;
  705. for (i = 0; i < MAXISA; i++) {
  706. if (!ISAbases[i])
  707. continue;
  708. if (!get_pio_conf_PIO(ISAbases[i], buf))
  709. continue;
  710. if (!register_pio_HBA(ISAbases[i], buf))
  711. release_region(ISAbases[i], 9);
  712. else
  713. ISAbases[i] = 0;
  714. }
  715. return;
  716. }
  717. static void find_pio_EISA(struct get_conf *buf)
  718. {
  719. u32 base;
  720. int i;
  721. #ifdef CHECKPAL
  722. u8 pal1, pal2, pal3;
  723. #endif
  724. for (i = 0; i < MAXEISA; i++) {
  725. if (EISAbases[i]) { /* Still a possibility ? */
  726. base = 0x1c88 + (i * 0x1000);
  727. #ifdef CHECKPAL
  728. pal1 = inb((u16) base - 8);
  729. pal2 = inb((u16) base - 7);
  730. pal3 = inb((u16) base - 6);
  731. if (((pal1 == 0x12) && (pal2 == 0x14)) || ((pal1 == 0x38) && (pal2 == 0xa3) && (pal3 == 0x82)) || ((pal1 == 0x06) && (pal2 == 0x94) && (pal3 == 0x24))) {
  732. DBG(DBG_PROBE, printk(KERN_NOTICE "EISA EATA id tags found: " "%x %x %x \n", (int) pal1, (int) pal2, (int) pal3));
  733. #endif
  734. if (get_pio_conf_PIO(base, buf)) {
  735. DBG(DBG_PROBE && DBG_EISA, print_pio_config(buf));
  736. if (buf->IRQ) {
  737. if (!register_pio_HBA(base, buf))
  738. release_region(base, 9);
  739. } else {
  740. printk(KERN_NOTICE "eata_dma: No valid IRQ. HBA " "removed from list\n");
  741. release_region(base, 9);
  742. }
  743. }
  744. /* Nothing found here so we take it from the list */
  745. EISAbases[i] = 0;
  746. #ifdef CHECKPAL
  747. }
  748. #endif
  749. }
  750. }
  751. return;
  752. }
  753. static void find_pio_PCI(struct get_conf *buf)
  754. {
  755. #ifndef CONFIG_PCI
  756. printk("eata_dma: kernel PCI support not enabled. Skipping scan for PCI HBAs.\n");
  757. #else
  758. struct pci_dev *dev = NULL;
  759. u32 base, x;
  760. while ((dev = pci_find_device(PCI_VENDOR_ID_DPT, PCI_DEVICE_ID_DPT, dev)) != NULL) {
  761. DBG(DBG_PROBE && DBG_PCI, printk("eata_pio: find_PCI, HBA at %s\n", pci_name(dev)));
  762. if (pci_enable_device(dev))
  763. continue;
  764. pci_set_master(dev);
  765. base = pci_resource_flags(dev, 0);
  766. if (base & IORESOURCE_MEM) {
  767. printk("eata_pio: invalid base address of device %s\n", pci_name(dev));
  768. continue;
  769. }
  770. base = pci_resource_start(dev, 0);
  771. /* EISA tag there ? */
  772. if ((inb(base) == 0x12) && (inb(base + 1) == 0x14))
  773. continue; /* Jep, it's forced, so move on */
  774. base += 0x10; /* Now, THIS is the real address */
  775. if (base != 0x1f8) {
  776. /* We didn't find it in the primary search */
  777. if (get_pio_conf_PIO(base, buf)) {
  778. if (buf->FORCADR) { /* If the address is forced */
  779. release_region(base, 9);
  780. continue; /* we'll find it later */
  781. }
  782. /* OK. We made it till here, so we can go now
  783. * and register it. We only have to check and
  784. * eventually remove it from the EISA and ISA list
  785. */
  786. if (!register_pio_HBA(base, buf)) {
  787. release_region(base, 9);
  788. continue;
  789. }
  790. if (base < 0x1000) {
  791. for (x = 0; x < MAXISA; ++x) {
  792. if (ISAbases[x] == base) {
  793. ISAbases[x] = 0;
  794. break;
  795. }
  796. }
  797. } else if ((base & 0x0fff) == 0x0c88) {
  798. x = (base >> 12) & 0x0f;
  799. EISAbases[x] = 0;
  800. }
  801. }
  802. #ifdef CHECK_BLINK
  803. else if (check_blink_state(base)) {
  804. printk("eata_pio: HBA is in BLINK state.\n" "Consult your HBAs manual to correct this.\n");
  805. }
  806. #endif
  807. }
  808. }
  809. #endif /* #ifndef CONFIG_PCI */
  810. }
  811. static int eata_pio_detect(struct scsi_host_template *tpnt)
  812. {
  813. struct Scsi_Host *HBA_ptr;
  814. struct get_conf gc;
  815. int i;
  816. find_pio_PCI(&gc);
  817. find_pio_EISA(&gc);
  818. find_pio_ISA(&gc);
  819. for (i = 0; i <= MAXIRQ; i++)
  820. if (reg_IRQ[i])
  821. request_irq(i, do_eata_pio_int_handler, SA_INTERRUPT, "EATA-PIO", NULL);
  822. HBA_ptr = first_HBA;
  823. if (registered_HBAs != 0) {
  824. printk("EATA (Extended Attachment) PIO driver version: %d.%d%s\n"
  825. "(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);
  826. printk("Registered HBAs:\n");
  827. printk("HBA no. Boardtype: Revis: EATA: Bus: BaseIO: IRQ: Ch: ID: Pr:" " QS: SG: CPL:\n");
  828. for (i = 1; i <= registered_HBAs; i++) {
  829. printk("scsi%-2d: %.10s v%s 2.0%c %s %#.4x %2d %d %d %c"
  830. " %2d %2d %2d\n",
  831. HBA_ptr->host_no, SD(HBA_ptr)->name, SD(HBA_ptr)->revision,
  832. SD(HBA_ptr)->EATA_revision, (SD(HBA_ptr)->bustype == 'P') ?
  833. "PCI " : (SD(HBA_ptr)->bustype == 'E') ? "EISA" : "ISA ",
  834. (uint) HBA_ptr->base, HBA_ptr->irq, SD(HBA_ptr)->channel, HBA_ptr->this_id,
  835. SD(HBA_ptr)->primary ? 'Y' : 'N', HBA_ptr->can_queue,
  836. HBA_ptr->sg_tablesize, HBA_ptr->cmd_per_lun);
  837. HBA_ptr = SD(HBA_ptr)->next;
  838. }
  839. }
  840. return (registered_HBAs);
  841. }
  842. static struct scsi_host_template driver_template = {
  843. .proc_name = "eata_pio",
  844. .name = "EATA (Extended Attachment) PIO driver",
  845. .proc_info = eata_pio_proc_info,
  846. .detect = eata_pio_detect,
  847. .release = eata_pio_release,
  848. .queuecommand = eata_pio_queue,
  849. .eh_abort_handler = eata_pio_abort,
  850. .eh_host_reset_handler = eata_pio_host_reset,
  851. .use_clustering = ENABLE_CLUSTERING,
  852. };
  853. MODULE_AUTHOR("Michael Neuffer, Alfred Arnold");
  854. MODULE_DESCRIPTION("EATA SCSI PIO driver");
  855. MODULE_LICENSE("GPL");
  856. #include "scsi_module.c"