eata_pio.c 28 KB

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