eata_pio.c 28 KB

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