eata_pio.c 28 KB

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