eata_pio.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:28k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  *  This program is free software; you can redistribute it  *
  18.  *  and/or modify it under the terms of the GNU General     *
  19.  *  Public License as published by the Free Software        *
  20.  *  Foundation; either version 2 of the License, or         *
  21.  *  (at your option) any later version.                     *
  22.  *                                                          *
  23.  *  This program is distributed in the hope that it will be *
  24.  *  useful, but WITHOUT ANY WARRANTY; without even the      *
  25.  *  implied warranty of MERCHANTABILITY or FITNESS FOR A    *
  26.  *  PARTICULAR PURPOSE.  See the GNU General Public License *
  27.  *  for more details.                                       *
  28.  *                                                          *
  29.  *  You should have received a copy of the GNU General      *
  30.  *  Public License along with this kernel; if not, write to *
  31.  *  the Free Software Foundation, Inc., 675 Mass Ave,       *
  32.  *  Cambridge, MA 02139, USA.                               *
  33.  *                                                          *
  34.  ************************************************************
  35.  *  last change: 96/07/16                  OS: Linux 2.0.8  *
  36.  ************************************************************/
  37. /* Look in eata_pio.h for configuration information */
  38. #include <linux/module.h>
  39.  
  40. #include <linux/kernel.h>
  41. #include <linux/sched.h>
  42. #include <linux/string.h>
  43. #include <linux/ioport.h>
  44. #include <linux/slab.h>
  45. #include <linux/in.h>
  46. #include <linux/pci.h>
  47. #include <linux/proc_fs.h>
  48. #include <asm/io.h>
  49. #include "eata_pio.h"
  50. #include "eata_dma_proc.h"
  51. #include "scsi.h"
  52. #include "sd.h"
  53. #include <linux/stat.h>
  54. #include <linux/config.h> /* for CONFIG_PCI */
  55. #include <linux/blk.h>
  56. #include <linux/spinlock.h>
  57. static uint ISAbases[MAXISA] =
  58. {0x1F0, 0x170, 0x330, 0x230};
  59. static uint ISAirqs[MAXISA] =
  60. {14,12,15,11};
  61. static unchar EISAbases[] =
  62. {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
  63. static uint registered_HBAs = 0;
  64. static struct Scsi_Host *last_HBA = NULL;
  65. static struct Scsi_Host *first_HBA = NULL;
  66. static unchar reg_IRQ[] =
  67. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  68. static unchar reg_IRQL[] =
  69. {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  70. static ulong int_counter = 0;
  71. static ulong queue_counter = 0;
  72. #include "eata_pio_proc.c"
  73.  
  74. #ifdef MODULE
  75. int eata_pio_release(struct Scsi_Host *sh)
  76. {
  77.     if (sh->irq && reg_IRQ[sh->irq] == 1) free_irq(sh->irq, NULL);
  78.     else reg_IRQ[sh->irq]--;
  79.     if (SD(sh)->channel == 0) {
  80. if (sh->io_port && sh->n_io_port)
  81.     release_region(sh->io_port, sh->n_io_port);
  82.     }
  83.     return(TRUE);
  84. }
  85. #endif
  86. void IncStat(Scsi_Pointer *SCp, uint Increment)
  87. {
  88.     SCp->ptr+=Increment; 
  89.     if ((SCp->this_residual-=Increment)==0)
  90.     {
  91. if ((--SCp->buffers_residual)==0) SCp->Status=FALSE;
  92. else
  93. {
  94.     SCp->buffer++;
  95.     SCp->ptr=SCp->buffer->address;
  96.     SCp->this_residual=SCp->buffer->length;
  97. }
  98.     }
  99. }
  100. void eata_pio_int_handler(int irq, void *dev_id, struct pt_regs * regs);
  101. void do_eata_pio_int_handler(int irq, void *dev_id, struct pt_regs * regs)
  102. {
  103.     unsigned long flags;
  104.     spin_lock_irqsave(&io_request_lock, flags);
  105.     eata_pio_int_handler(irq, dev_id, regs);
  106.     spin_unlock_irqrestore(&io_request_lock, flags);
  107. }
  108. void eata_pio_int_handler(int irq, void *dev_id, struct pt_regs * regs)
  109. {
  110.     uint eata_stat = 0xfffff;
  111.     Scsi_Cmnd *cmd;
  112.     hostdata *hd;
  113.     struct eata_ccb *cp;
  114.     uint base;
  115.     ulong flags;
  116.     uint x,z;
  117.     struct Scsi_Host *sh;
  118.     ushort zwickel=0;
  119.     unchar stat,odd;
  120.     
  121.     save_flags(flags);
  122.     cli();
  123.     
  124.     for (x = 1, sh = first_HBA; x <= registered_HBAs; x++, sh = SD(sh)->prev) {
  125. if (sh->irq != irq)
  126.     continue;
  127. if (inb((uint)sh->base + HA_RSTATUS) & HA_SBUSY)
  128.     continue;
  129. int_counter++;
  130. hd=SD(sh);
  131. cp = &hd->ccb[0];
  132. cmd = cp->cmd;
  133. base = (uint) cmd->host->base;
  134. do
  135. {
  136.     stat=inb(base+HA_RSTATUS);
  137.     if (stat&HA_SDRQ) {
  138. if (cp->DataIn)
  139. {
  140.     z=256; odd=FALSE;
  141.     while ((cmd->SCp.Status)&&((z>0)||(odd)))
  142.     {
  143. if (odd) 
  144.     *(cmd->SCp.ptr)=zwickel>>8; 
  145.     IncStat(&cmd->SCp,1);
  146.     odd=FALSE;
  147. }
  148. x=min_t(unsigned int,z,cmd->SCp.this_residual/2);
  149. insw(base+HA_RDATA,cmd->SCp.ptr,x);
  150. z-=x; 
  151. IncStat(&cmd->SCp,2*x);
  152. if ((z>0)&&(cmd->SCp.this_residual==1))
  153. {
  154.     zwickel=inw(base+HA_RDATA); 
  155.     *(cmd->SCp.ptr)=zwickel&0xff;
  156.     IncStat(&cmd->SCp,1); z--; 
  157.     odd=TRUE;
  158. }
  159.     }
  160.     while (z>0) {
  161. zwickel=inw(base+HA_RDATA); 
  162. z--;
  163.     } 
  164. }
  165. else /* cp->DataOut */
  166. {
  167.     odd=FALSE; z=256;
  168.     while ((cmd->SCp.Status)&&((z>0)||(odd)))
  169.     {
  170. if (odd)
  171. {
  172.     zwickel+=*(cmd->SCp.ptr)<<8; 
  173.     IncStat(&cmd->SCp,1);
  174.     outw(zwickel,base+HA_RDATA); 
  175.     z--; 
  176.     odd=FALSE; 
  177. }
  178. x=min_t(unsigned int,z,cmd->SCp.this_residual/2);
  179. outsw(base+HA_RDATA,cmd->SCp.ptr,x);
  180. z-=x; 
  181. IncStat(&cmd->SCp,2*x);
  182. if ((z>0)&&(cmd->SCp.this_residual==1))
  183. {
  184.     zwickel=*(cmd->SCp.ptr); 
  185.     zwickel&=0xff;
  186.     IncStat(&cmd->SCp,1); 
  187.     odd=TRUE;
  188. }  
  189.     }
  190.     while (z>0||odd) {
  191. outw(zwickel,base+HA_RDATA); 
  192. z--; 
  193. odd=FALSE;
  194.     }
  195. }
  196.     }
  197. }
  198. while ((stat&HA_SDRQ)||((stat&HA_SMORE)&&hd->moresupport));
  199. /* terminate handler if HBA goes busy again, i.e. transfers
  200.  * more data */
  201. if (stat&HA_SBUSY) break;
  202. /* OK, this is quite stupid, but I haven't found any correct
  203.  * way to get HBA&SCSI status so far */
  204. if (!(inb(base+HA_RSTATUS)&HA_SERROR))
  205. {
  206.     cmd->result=(DID_OK<<16); 
  207.     hd->devflags|=(1<<cp->cp_id);
  208. }
  209. else if (hd->devflags&1<<cp->cp_id) 
  210.     cmd->result=(DID_OK<<16)+0x02;
  211. else cmd->result=(DID_NO_CONNECT<<16);
  212. if (cp->status == LOCKED) {
  213.     cp->status = FREE;
  214.     eata_stat = inb(base + HA_RSTATUS);
  215.     printk(KERN_NOTICE "eata_pio: int_handler, freeing locked "
  216.                    "queueslotn");
  217.     DBG(DBG_INTR&&DBG_DELAY,DELAY(1));
  218.     restore_flags(flags);
  219.     return;
  220. }
  221. #if DBG_INTR2
  222. if (stat != 0x50) 
  223.     printk(KERN_DEBUG "stat: %#.2x, result: %#.8xn", stat, 
  224.                    cmd->result); 
  225. DBG(DBG_INTR&&DBG_DELAY,DELAY(1));
  226. #endif
  227. cp->status = FREE;   /* now we can release the slot  */
  228. restore_flags(flags);
  229. cmd->scsi_done(cmd);
  230. save_flags(flags);
  231. cli();
  232.     }
  233.     restore_flags(flags);
  234.     
  235.     return;
  236. }
  237. inline uint eata_pio_send_command(uint base, unchar command)
  238. {
  239.     uint loop = HZ/2;
  240.     
  241.     while (inb(base + HA_RSTATUS) & HA_SBUSY)
  242. if (--loop == 0)
  243.     return(TRUE);
  244.     /* Enable interrupts for HBA.  It is not the best way to do it at this
  245.      * place, but I hope that it doesn't interfere with the IDE driver 
  246.      * initialization this way */
  247.     outb(HA_CTRL_8HEADS,base+HA_CTRLREG);
  248.     
  249.     outb(command, base + HA_WCOMMAND);
  250.     return(FALSE);
  251. }
  252. int eata_pio_queue(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
  253. {
  254.     uint x, y;
  255.     long flags;
  256.     uint base;
  257.     
  258.     hostdata *hd;
  259.     struct Scsi_Host *sh;
  260.     struct eata_ccb *cp;
  261.     
  262.     save_flags(flags);
  263.     cli();
  264.     
  265.     queue_counter++;
  266.     
  267.     hd = HD(cmd);
  268.     sh = cmd->host;
  269.     base = (uint) sh->base;
  270.     
  271.     /* use only slot 0, as 2001 can handle only one cmd at a time */
  272.     
  273.     y = x = 0;
  274.     
  275.     if (hd->ccb[y].status!=FREE) { 
  276. DBG(DBG_QUEUE, printk(KERN_EMERG "can_queue %d, x %d, y %dn",
  277.                               sh->can_queue,x,y));
  278. #if DEBUG_EATA
  279. panic(KERN_EMERG "eata_pio: run out of queue slots cmdno:%ld "
  280.               "intrno: %ldn", queue_counter, int_counter);
  281. #else
  282. panic(KERN_EMERG "eata_pio: run out of queue slots....n");
  283. #endif
  284.     }
  285.     
  286.     cp = &hd->ccb[y];
  287.     
  288.     memset(cp, 0, sizeof(struct eata_ccb));
  289.     memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
  290.     cp->status = USED;      /* claim free slot */
  291.     DBG(DBG_QUEUE, printk(KERN_DEBUG "eata_pio_queue pid %ld, target: %x, lun:"
  292.                           " %x, y %dn", cmd->pid, cmd->target, cmd->lun, y));
  293.     DBG(DBG_QUEUE && DBG_DELAY, DELAY(1));
  294.     
  295.     cmd->scsi_done = (void *)done;
  296.     
  297.     switch (cmd->cmnd[0]) {
  298.     case CHANGE_DEFINITION: case COMPARE:         case COPY:
  299.     case COPY_VERIFY:       case LOG_SELECT:      case MODE_SELECT:
  300.     case MODE_SELECT_10:    case SEND_DIAGNOSTIC: case WRITE_BUFFER:
  301.     case FORMAT_UNIT:       case REASSIGN_BLOCKS: case RESERVE:
  302.     case SEARCH_EQUAL:      case SEARCH_HIGH:     case SEARCH_LOW:
  303.     case WRITE_6:           case WRITE_10:        case WRITE_VERIFY:
  304.     case UPDATE_BLOCK:      case WRITE_LONG:      case WRITE_SAME:      
  305.     case SEARCH_HIGH_12:    case SEARCH_EQUAL_12: case SEARCH_LOW_12:
  306.     case WRITE_12:          case WRITE_VERIFY_12: case SET_WINDOW: 
  307.     case MEDIUM_SCAN:       case SEND_VOLUME_TAG:            
  308.     case 0xea:      /* alternate number for WRITE LONG */
  309. cp->DataOut = TRUE; /* Output mode */
  310. break;
  311.     case TEST_UNIT_READY:
  312.     default:
  313. cp->DataIn = TRUE;  /* Input mode  */
  314.     }
  315.     
  316.     cp->Interpret = (cmd->target == hd->hostid);
  317.     cp->cp_datalen = htonl((ulong)cmd->request_bufflen);
  318.     cp->Auto_Req_Sen = FALSE;
  319.     cp->cp_reqDMA = htonl(0);
  320.     cp->reqlen = 0;
  321.     
  322.     cp->cp_id = cmd->target;
  323.     cp->cp_lun = cmd->lun;
  324.     cp->cp_dispri = FALSE;
  325.     cp->cp_identify = TRUE;
  326.     memcpy(cp->cp_cdb, cmd->cmnd, COMMAND_SIZE(*cmd->cmnd));
  327.     
  328.     cp->cp_statDMA = htonl(0);
  329.     
  330.     cp->cp_viraddr = cp;
  331.     cp->cmd = cmd;
  332.     cmd->host_scribble = (char *)&hd->ccb[y];   
  333.     
  334.     if (cmd->use_sg == 0)
  335.     { 
  336. cmd->SCp.buffers_residual=1;
  337. cmd->SCp.ptr = cmd->request_buffer;
  338. cmd->SCp.this_residual = cmd->request_bufflen;
  339. cmd->SCp.buffer = NULL;
  340.     } else {
  341. cmd->SCp.buffer = cmd->request_buffer;
  342. cmd->SCp.buffers_residual = cmd->use_sg;
  343. cmd->SCp.ptr = cmd->SCp.buffer->address;
  344. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  345.     }
  346.     cmd->SCp.Status = (cmd->SCp.this_residual != 0);  /* TRUE as long as bytes 
  347.                                                        * are to transfer */ 
  348.     
  349.     if (eata_pio_send_command(base, EATA_CMD_PIO_SEND_CP)) 
  350.     {
  351. cmd->result = DID_BUS_BUSY << 16;
  352. printk(KERN_NOTICE "eata_pio_queue target %d, pid %ld, HBA busy, "
  353.                "returning DID_BUS_BUSY, done.n", cmd->target, cmd->pid);
  354.         done(cmd);
  355.         cp->status = FREE;      
  356.         restore_flags(flags);
  357. return (0);
  358.     }
  359.     while (!(inb(base + HA_RSTATUS) & HA_SDRQ));
  360.     outsw(base + HA_RDATA, cp, hd->cplen);
  361.     outb(EATA_CMD_PIO_TRUNC, base + HA_WCOMMAND);
  362.     for (x = 0; x < hd->cppadlen; x++) outw(0, base + HA_RDATA);
  363.     
  364.     DBG(DBG_QUEUE,printk(KERN_DEBUG "Queued base %#.4lx pid: %ld target: %x "
  365.                          "lun: %x slot %d irq %dn", (long)sh->base, cmd->pid, 
  366.  cmd->target, cmd->lun, y, sh->irq));
  367.     DBG(DBG_QUEUE && DBG_DELAY, DELAY(1));
  368.     
  369.     restore_flags(flags);
  370.     return (0);
  371. }
  372. int eata_pio_abort(Scsi_Cmnd * cmd)
  373. {
  374.     ulong flags;
  375.     uint loop = HZ;
  376.     
  377.     save_flags(flags);
  378.     cli();
  379.     
  380.     DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_abort called pid: %ld "
  381.                            "target: %x lun: %x reason %xn", cmd->pid, 
  382.                            cmd->target, cmd->lun, cmd->abort_reason));
  383.     DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
  384.     
  385.     
  386.     while (inb((uint)(cmd->host->base) + HA_RAUXSTAT) & HA_ABUSY)
  387. if (--loop == 0) {
  388.     printk(KERN_WARNING "eata_pio: abort, timeout error.n");
  389.     restore_flags(flags);
  390.     DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
  391.     return (SCSI_ABORT_ERROR);
  392. }
  393.     if (CD(cmd)->status == FREE) {
  394. DBG(DBG_ABNORM, printk(KERN_WARNING "Returning: SCSI_ABORT_NOT_RUNNINGn")); 
  395. restore_flags(flags);
  396. return (SCSI_ABORT_NOT_RUNNING);
  397.     }
  398.     if (CD(cmd)->status == USED) {
  399. DBG(DBG_ABNORM, printk(KERN_WARNING "Returning: SCSI_ABORT_BUSYn"));
  400. restore_flags(flags);
  401. return (SCSI_ABORT_BUSY);  /* SNOOZE */ 
  402.     }
  403.     if (CD(cmd)->status == RESET) {
  404. restore_flags(flags);
  405. printk(KERN_WARNING "eata_pio: abort, command reset error.n");
  406. DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
  407. return (SCSI_ABORT_ERROR);
  408.     }
  409.     if (CD(cmd)->status == LOCKED) {
  410. restore_flags(flags);
  411. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio: abort, queue slot "
  412.                                "locked.n"));
  413. DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
  414. return (SCSI_ABORT_NOT_RUNNING);
  415.     }
  416.     restore_flags(flags);
  417.     panic("eata_pio: abort: invalid slot statusn");
  418. }
  419. int eata_pio_reset(Scsi_Cmnd * cmd, unsigned int dummy)
  420. {
  421.     uint x, time, limit = 0;
  422.     ulong flags;
  423.     unchar success = FALSE;
  424.     Scsi_Cmnd *sp; 
  425.     
  426.     save_flags(flags);
  427.     cli();
  428.     DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset called pid:%ld target:"
  429.                            " %x lun: %x reason %xn", cmd->pid, cmd->target, 
  430.                            cmd->lun, cmd->abort_reason));
  431.     if (HD(cmd)->state == RESET) {
  432. printk(KERN_WARNING "eata_pio_reset: exit, already in reset.n");
  433. restore_flags(flags);
  434. DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
  435. return (SCSI_RESET_ERROR);
  436.     }
  437.     
  438.     /* force all slots to be free */
  439.     
  440.     for (x = 0; x < cmd->host->can_queue; x++) {
  441. if (HD(cmd)->ccb[x].status == FREE) 
  442.     continue;
  443. sp = HD(cmd)->ccb[x].cmd;
  444. HD(cmd)->ccb[x].status = RESET;
  445. printk(KERN_WARNING "eata_pio_reset: slot %d in reset, pid %ld.n", x,
  446.                sp->pid);
  447. DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
  448. if (sp == NULL)
  449.     panic("eata_pio_reset: slot %d, sp==NULL.n", x);
  450. DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
  451.     }
  452.     
  453.     /* hard reset the HBA  */
  454.     outb(EATA_CMD_RESET, (uint) cmd->host->base+HA_WCOMMAND);
  455.     
  456.     DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: board reset done.n"));
  457.     HD(cmd)->state = RESET;
  458.     
  459.     time = jiffies;
  460.     while (time_before(jiffies, time + 3 * HZ) && limit++ < 10000000);
  461.     
  462.     DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: interrupts disabled, "
  463.                            "loops %d.n", limit));
  464.     DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
  465.     
  466.     for (x = 0; x < cmd->host->can_queue; x++) {
  467. /* Skip slots already set free by interrupt */
  468. if (HD(cmd)->ccb[x].status != RESET)
  469.     continue;
  470. sp = HD(cmd)->ccb[x].cmd;
  471. sp->result = DID_RESET << 16;
  472. /* This mailbox is terminated */
  473. printk(KERN_WARNING "eata_pio_reset: reset ccb %d.n",x);
  474. HD(cmd)->ccb[x].status = FREE;
  475. restore_flags(flags);
  476. sp->scsi_done(sp);
  477. cli();
  478.     }
  479.     
  480.     HD(cmd)->state = FALSE;
  481.     restore_flags(flags);
  482.     
  483.     if (success) { /* hmmm... */
  484. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: exit, success.n"));
  485. DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
  486. return (SCSI_RESET_SUCCESS);
  487.     } else {
  488. DBG(DBG_ABNORM, printk(KERN_WARNING "eata_pio_reset: exit, wakeup.n"));
  489. DBG(DBG_ABNORM && DBG_DELAY, DELAY(1));
  490. return (SCSI_RESET_PUNT);
  491.     }
  492. }
  493. char * get_pio_board_data(ulong base, uint irq, uint id, ulong cplen, ushort cppadlen)
  494. {
  495.     struct eata_ccb cp;
  496.     static char buff[256];
  497.     int z;
  498.     
  499.     memset(&cp, 0, sizeof(struct eata_ccb));
  500.     memset(buff, 0, sizeof(buff));
  501.     
  502.     cp.DataIn = TRUE;     
  503.     cp.Interpret = TRUE;   /* Interpret command */
  504.     
  505.     cp.cp_datalen = htonl(254);  
  506.     cp.cp_dataDMA = htonl(0);
  507.     
  508.     cp.cp_id = id;
  509.     cp.cp_lun = 0;
  510.     
  511.     cp.cp_cdb[0] = INQUIRY;
  512.     cp.cp_cdb[1] = 0;
  513.     cp.cp_cdb[2] = 0;
  514.     cp.cp_cdb[3] = 0;
  515.     cp.cp_cdb[4] = 254;
  516.     cp.cp_cdb[5] = 0;
  517.     
  518.     if (eata_pio_send_command((uint) base, EATA_CMD_PIO_SEND_CP)) 
  519.         return (NULL);
  520.     while (!(inb(base + HA_RSTATUS) & HA_SDRQ));
  521.     outsw(base + HA_RDATA, &cp, cplen);
  522.     outb(EATA_CMD_PIO_TRUNC, base + HA_WCOMMAND);
  523.     for (z = 0; z < cppadlen; z++) outw(0, base + HA_RDATA);
  524.     
  525.     while (inb(base + HA_RSTATUS) & HA_SBUSY);
  526.     if (inb(base + HA_RSTATUS) & HA_SERROR)
  527. return (NULL);
  528.     else if (!(inb(base + HA_RSTATUS) & HA_SDRQ))
  529. return (NULL);
  530.     else
  531.     {
  532. insw(base+HA_RDATA, &buff, 127);
  533. while (inb(base + HA_RSTATUS)&HA_SDRQ) inw(base + HA_RDATA);
  534. return (buff);
  535.     }
  536. }
  537. int get_pio_conf_PIO(u32 base, struct get_conf *buf)
  538. {
  539.     ulong loop = HZ/2;
  540.     int z;
  541.     ushort *p;
  542.     
  543.     if(check_region(base, 9))  
  544. return (FALSE);
  545.     
  546.     memset(buf, 0, sizeof(struct get_conf));
  547.     
  548.     while (inb(base + HA_RSTATUS) & HA_SBUSY)
  549. if (--loop == 0) 
  550.     return (FALSE);
  551.     
  552.     DBG(DBG_PIO && DBG_PROBE,
  553. printk(KERN_DEBUG "Issuing PIO READ CONFIG to HBA at %#xn", base));
  554.     eata_pio_send_command(base, EATA_CMD_PIO_READ_CONFIG);
  555.     loop = HZ/2;
  556.     for (p = (ushort *) buf; 
  557.  (long)p <= ((long)buf + (sizeof(struct get_conf) / 2)); p++) {
  558. while (!(inb(base + HA_RSTATUS) & HA_SDRQ))
  559.     if (--loop == 0)
  560. return (FALSE);
  561. loop = HZ/2;
  562. *p = inw(base + HA_RDATA);
  563.     }
  564.     if (!(inb(base + HA_RSTATUS) & HA_SERROR)) {            /* Error ? */
  565. if (htonl(EATA_SIGNATURE) == buf->signature) {
  566.     DBG(DBG_PIO&&DBG_PROBE, printk(KERN_NOTICE "EATA Controller found "
  567.                                            "at %#4x EATA Level: %xn", base, 
  568.    (uint) (buf->version)));
  569.     
  570.     while (inb(base + HA_RSTATUS) & HA_SDRQ) 
  571. inw(base + HA_RDATA);
  572.     if(ALLOW_DMA_BOARDS == FALSE) {
  573. for (z = 0; z < MAXISA; z++)
  574.     if (base == ISAbases[z]) {
  575. buf->IRQ = ISAirqs[z]; 
  576. break;
  577.     }
  578.     }
  579.     return (TRUE);
  580.     } else {
  581. DBG(DBG_PROBE, printk("eata_dma: get_conf_PIO, error during transfer "
  582.       "for HBA at %xn", base));
  583.     }
  584.     return (FALSE);
  585. }
  586. void print_pio_config(struct get_conf *gc)
  587. {
  588.     printk("Please check values: (read config data)n");
  589.     printk("LEN: %d ver:%d OCS:%d TAR:%d TRNXFR:%d MORES:%dn",
  590.    (uint) ntohl(gc->len), gc->version,
  591.    gc->OCS_enabled, gc->TAR_support, gc->TRNXFR, gc->MORE_support);
  592.     printk("HAAV:%d SCSIID0:%d ID1:%d ID2:%d QUEUE:%d SG:%d SEC:%dn",
  593.    gc->HAA_valid, gc->scsi_id[3], gc->scsi_id[2],
  594.    gc->scsi_id[1], ntohs(gc->queuesiz), ntohs(gc->SGsiz), gc->SECOND);
  595.     printk("IRQ:%d IRQT:%d FORCADR:%d MCH:%d RIDQ:%dn",
  596.    gc->IRQ, gc->IRQ_TR, gc->FORCADR, 
  597.    gc->MAX_CHAN, gc->ID_qest);
  598.     DBG(DPT_DEBUG, DELAY(14));
  599. }
  600. static uint print_selftest(uint base)
  601. {
  602.     unchar buffer[512];
  603. #ifdef VERBOSE_SETUP
  604.     int z;
  605. #endif
  606.     
  607.     printk("eata_pio: executing controller self test & setup...n");
  608.     while (inb(base + HA_RSTATUS) & HA_SBUSY);
  609.     outb(EATA_CMD_PIO_SETUPTEST, base + HA_WCOMMAND);
  610.     do {
  611. while (inb(base + HA_RSTATUS) & HA_SBUSY)
  612.     /* nothing */ ;
  613. if (inb(base + HA_RSTATUS) & HA_SDRQ)
  614. {
  615.     insw(base + HA_RDATA, &buffer, 256);
  616. #ifdef VERBOSE_SETUP
  617.     /* no beeps please... */
  618.     for (z = 0; z < 511 && buffer[z]; z++)
  619. if (buffer[z] != 7) printk("%c", buffer[z]);
  620. #endif
  621. }
  622.     } while (inb(base+HA_RSTATUS) & (HA_SBUSY|HA_SDRQ));
  623.     
  624.     return (!(inb(base+HA_RSTATUS) & HA_SERROR)); 
  625. }
  626. int register_pio_HBA(long base, struct get_conf *gc, Scsi_Host_Template * tpnt)
  627. {
  628.     ulong size = 0;
  629.     char *buff;
  630.     ulong cplen;
  631.     ushort cppadlen;
  632.     struct Scsi_Host *sh;
  633.     hostdata *hd;
  634.     
  635.     DBG(DBG_REGISTER, print_pio_config(gc));
  636.     
  637.     if (gc->DMA_support == TRUE) {
  638. printk("HBA at %#.4lx supports DMA. Please use EATA-DMA driver.n",base);
  639. if(ALLOW_DMA_BOARDS == FALSE)
  640.     return (FALSE);
  641.     }
  642.     
  643.     if ((buff = get_pio_board_data((uint)base, gc->IRQ, gc->scsi_id[3], 
  644.        cplen   =(htonl(gc->cplen   )+1)/2, 
  645.        cppadlen=(htons(gc->cppadlen)+1)/2)) == NULL)
  646.     {
  647. printk("HBA at %#lx didn't react on INQUIRY. Sorry.n", (ulong) base);
  648. return (FALSE);
  649.     }
  650.     
  651.     if (print_selftest(base) == FALSE && ALLOW_DMA_BOARDS == FALSE)
  652.     {
  653. printk("HBA at %#lx failed while performing self test & setup.n", 
  654.        (ulong) base);
  655. return (FALSE);
  656.     }
  657.     
  658.     if (!reg_IRQ[gc->IRQ]) {    /* Interrupt already registered ? */
  659. if (!request_irq(gc->IRQ, do_eata_pio_int_handler, SA_INTERRUPT, 
  660.  "EATA-PIO", NULL)){
  661.     reg_IRQ[gc->IRQ]++;
  662.     if (!gc->IRQ_TR)
  663. reg_IRQL[gc->IRQ] = TRUE;   /* IRQ is edge triggered */
  664. } else {
  665.     printk("Couldn't allocate IRQ %d, Sorry.n", gc->IRQ);
  666.     return (FALSE);
  667. }
  668.     } else {            /* More than one HBA on this IRQ */
  669. if (reg_IRQL[gc->IRQ] == TRUE) {
  670.     printk("Can't support more than one HBA on this IRQ,n"
  671.    "  if the IRQ is edge triggered. Sorry.n");
  672.     return (FALSE);
  673. } else
  674.     reg_IRQ[gc->IRQ]++;
  675.     }
  676.     
  677.     request_region(base, 8, "eata_pio");
  678.     
  679.     size = sizeof(hostdata) + (sizeof(struct eata_ccb) * ntohs(gc->queuesiz));
  680.     
  681.     sh = scsi_register(tpnt, size);
  682.     if(sh == NULL)
  683.     {
  684.      release_region(base, 8);
  685.      return FALSE;
  686.     }
  687.     
  688.     hd = SD(sh);                   
  689.     
  690.     memset(hd->ccb, 0, (sizeof(struct eata_ccb) * ntohs(gc->queuesiz)));
  691.     memset(hd->reads, 0, sizeof(ulong) * 26); 
  692.     
  693.     strncpy(SD(sh)->vendor, &buff[8], 8);
  694.     SD(sh)->vendor[8] = 0;
  695.     strncpy(SD(sh)->name, &buff[16], 17);
  696.     SD(sh)->name[17] = 0;
  697.     SD(sh)->revision[0] = buff[32];
  698.     SD(sh)->revision[1] = buff[33];
  699.     SD(sh)->revision[2] = buff[34];
  700.     SD(sh)->revision[3] = '.';
  701.     SD(sh)->revision[4] = buff[35];
  702.     SD(sh)->revision[5] = 0;
  703.     switch (ntohl(gc->len)) {
  704.     case 0x1c:
  705. SD(sh)->EATA_revision = 'a';
  706. break;
  707.     case 0x1e:
  708. SD(sh)->EATA_revision = 'b';
  709. break;
  710.     case 0x22:
  711. SD(sh)->EATA_revision = 'c';
  712. break;
  713.     case 0x24:
  714. SD(sh)->EATA_revision = 'z';
  715.     default:
  716. SD(sh)->EATA_revision = '?';
  717.     }
  718.     if(ntohl(gc->len) >= 0x22) {
  719. if (gc->is_PCI == TRUE)
  720.     hd->bustype = IS_PCI;
  721. else if (gc->is_EISA == TRUE)
  722.     hd->bustype = IS_EISA;
  723. else
  724.     hd->bustype = IS_ISA;
  725.     } else {
  726. if (buff[21] == '4')
  727.     hd->bustype = IS_PCI;
  728. else if (buff[21] == '2')
  729.     hd->bustype = IS_EISA;
  730. else
  731.     hd->bustype = IS_ISA;
  732.     }
  733.   
  734.     SD(sh)->cplen=cplen;
  735.     SD(sh)->cppadlen=cppadlen;
  736.     SD(sh)->hostid=gc->scsi_id[3];
  737.     SD(sh)->devflags=1<<gc->scsi_id[3];
  738.     SD(sh)->moresupport=gc->MORE_support;
  739.     sh->unique_id = base;
  740.     sh->base = base;
  741.     sh->io_port = base;
  742.     sh->n_io_port = 8;
  743.     sh->irq = gc->IRQ;
  744.     sh->dma_channel = PIO;
  745.     sh->this_id = gc->scsi_id[3];
  746.     sh->can_queue = 1;
  747.     sh->cmd_per_lun = 1;
  748.     sh->sg_tablesize = SG_ALL;
  749.     
  750.     hd->channel = 0;
  751.     
  752.     sh->max_id = 8;
  753.     sh->max_lun = 8;
  754.     if (gc->SECOND)
  755. hd->primary = FALSE;
  756.     else
  757. hd->primary = TRUE;
  758.     
  759.     sh->unchecked_isa_dma = FALSE; /* We can only do PIO */
  760.     
  761.     hd->next = NULL;    /* build a linked list of all HBAs */
  762.     hd->prev = last_HBA;
  763.     if(hd->prev != NULL)
  764. SD(hd->prev)->next = sh;
  765.     last_HBA = sh;
  766.     if (first_HBA == NULL)
  767. first_HBA = sh;
  768.     registered_HBAs++;
  769.     return (1);
  770. }
  771. void find_pio_ISA(struct get_conf *buf, Scsi_Host_Template * tpnt)
  772. {
  773.     int i;
  774.     
  775.     for (i = 0; i < MAXISA; i++) {  
  776. if (ISAbases[i]) {  
  777.     if (get_pio_conf_PIO(ISAbases[i], buf) == TRUE){
  778. register_pio_HBA(ISAbases[i], buf, tpnt);
  779.     }
  780.     ISAbases[i] = 0;
  781. }
  782.     }
  783.     return;
  784. }
  785. void find_pio_EISA(struct get_conf *buf, Scsi_Host_Template * tpnt)
  786. {
  787.     u32 base;
  788.     int i;
  789. #if CHECKPAL
  790.     u8 pal1, pal2, pal3;
  791. #endif
  792.     for (i = 0; i < MAXEISA; i++) {
  793. if (EISAbases[i] == TRUE) { /* Still a possibility ?          */
  794.     base = 0x1c88 + (i * 0x1000);
  795. #if CHECKPAL
  796.     pal1 = inb((u16)base - 8);
  797.     pal2 = inb((u16)base - 7);
  798.     pal3 = inb((u16)base - 6);
  799.     if (((pal1 == 0x12) && (pal2 == 0x14)) ||
  800. ((pal1 == 0x38) && (pal2 == 0xa3) && (pal3 == 0x82)) ||
  801. ((pal1 == 0x06) && (pal2 == 0x94) && (pal3 == 0x24))) {
  802. DBG(DBG_PROBE, printk(KERN_NOTICE "EISA EATA id tags found: "
  803.                                       "%x %x %x n",
  804.       (int)pal1, (int)pal2, (int)pal3));
  805. #endif
  806. if (get_pio_conf_PIO(base, buf) == TRUE) {
  807.     DBG(DBG_PROBE && DBG_EISA, print_pio_config(buf));
  808.     if (buf->IRQ) {
  809. register_pio_HBA(base, buf, tpnt);
  810.     } else
  811. printk(KERN_NOTICE "eata_dma: No valid IRQ. HBA "
  812.                                "removed from listn");
  813. }
  814. /* Nothing found here so we take it from the list */
  815. EISAbases[i] = 0;
  816. #if CHECKPAL
  817.     }
  818. #endif
  819. }
  820.     }
  821.     return;
  822. }
  823. void find_pio_PCI(struct get_conf *buf, Scsi_Host_Template * tpnt)
  824. {
  825. #ifndef CONFIG_PCI
  826.     printk("eata_dma: kernel PCI support not enabled. Skipping scan for PCI HBAs.n");
  827. #else
  828.     struct pci_dev *dev = NULL; 
  829.     u32 base, x;
  830.     while ((dev = pci_find_device(PCI_VENDOR_ID_DPT, PCI_DEVICE_ID_DPT, dev)) != NULL) {
  831.     DBG(DBG_PROBE && DBG_PCI, 
  832. printk("eata_pio: find_PCI, HBA at %sn", dev->name));
  833.     if (pci_enable_device(dev))
  834.      continue;
  835.     pci_set_master(dev);
  836.     base = pci_resource_flags(dev, 0);
  837.     if (base & IORESOURCE_MEM) {
  838. printk("eata_pio: invalid base address of device %sn", dev->name);
  839. continue;
  840.     }
  841.     base = pci_resource_start(dev, 0);
  842.             /* EISA tag there ? */
  843.     if ((inb(base) == 0x12) && (inb(base + 1) == 0x14))
  844. continue;   /* Jep, it's forced, so move on  */
  845.     base += 0x10;   /* Now, THIS is the real address */
  846.     if (base != 0x1f8) {
  847. /* We didn't find it in the primary search */
  848. if (get_pio_conf_PIO(base, buf) == TRUE) {
  849.     if (buf->FORCADR)   /* If the address is forced */
  850. continue;       /* we'll find it later      */
  851.     
  852.     /* OK. We made it till here, so we can go now  
  853.      * and register it. We  only have to check and 
  854.      * eventually remove it from the EISA and ISA list 
  855.      */
  856.     
  857.     register_pio_HBA(base, buf, tpnt);
  858.     
  859.     if (base < 0x1000) {
  860. for (x = 0; x < MAXISA; ++x) {
  861.     if (ISAbases[x] == base) {
  862. ISAbases[x] = 0;
  863. break;
  864.     }
  865. }
  866.     } else if ((base & 0x0fff) == 0x0c88) {
  867. x = (base >> 12) & 0x0f;
  868. EISAbases[x] = 0;
  869.     }
  870. #if CHECK_BLINK
  871. else if (check_blink_state(base) == TRUE) {
  872.     printk("eata_pio: HBA is in BLINK state.n"
  873.    "Consult your HBAs manual to correct this.n");
  874. }
  875. #endif
  876.     }
  877. }
  878. #endif /* #ifndef CONFIG_PCI */
  879. }
  880. int eata_pio_detect(Scsi_Host_Template * tpnt)
  881. {
  882.     struct Scsi_Host *HBA_ptr;
  883.     struct get_conf gc;
  884.     int i;
  885.     
  886.     DBG((DBG_PROBE && DBG_DELAY) || DPT_DEBUG,
  887. printk("Using lots of delays to let you read the debugging outputn"));
  888.     
  889.     tpnt->proc_name = "eata_pio";
  890.     find_pio_PCI(&gc, tpnt);
  891.     find_pio_EISA(&gc, tpnt);
  892.     find_pio_ISA(&gc, tpnt);
  893.     
  894.     for (i = 0; i <= MAXIRQ; i++)
  895. if (reg_IRQ[i])
  896.     request_irq(i, do_eata_pio_int_handler, SA_INTERRUPT, "EATA-PIO", NULL);
  897.     
  898.     HBA_ptr = first_HBA;
  899.   
  900.     if (registered_HBAs != 0) {
  901. printk("EATA (Extended Attachment) PIO driver version: %d.%d%sn"
  902.        "(c) 1993-95 Michael Neuffer, neuffer@goofy.zdv.uni-mainz.den"
  903.        "            Alfred Arnold,   a.arnold@kfa-juelich.den"
  904.        "This release only supports DASD devices (harddisks)n",
  905.        VER_MAJOR, VER_MINOR, VER_SUB);
  906. printk("Registered HBAs:n");
  907. printk("HBA no. Boardtype: Revis: EATA: Bus: BaseIO: IRQ: Ch: ID: Pr:"
  908.                " QS: SG: CPL:n");
  909. for (i = 1; i <= registered_HBAs; i++) {
  910.     printk("scsi%-2d: %.10s v%s 2.0%c  %s %#.4x   %2d   %d   %d   %c"
  911.                    "  %2d  %2d  %2dn", 
  912.    HBA_ptr->host_no, SD(HBA_ptr)->name, SD(HBA_ptr)->revision,
  913.    SD(HBA_ptr)->EATA_revision, (SD(HBA_ptr)->bustype == 'P')?
  914.    "PCI ":(SD(HBA_ptr)->bustype == 'E')?"EISA":"ISA ",
  915.    (uint) HBA_ptr->base, HBA_ptr->irq, SD(HBA_ptr)->channel, 
  916.                    HBA_ptr->this_id, (SD(HBA_ptr)->primary == TRUE)?'Y':'N', 
  917.    HBA_ptr->can_queue, HBA_ptr->sg_tablesize, 
  918.                    HBA_ptr->cmd_per_lun);
  919.     HBA_ptr = SD(HBA_ptr)->next;
  920. }
  921.     }
  922.     DBG(DPT_DEBUG,DELAY(12));
  923.     
  924.     return (registered_HBAs);
  925. }
  926. /* Eventually this will go into an include file, but this will be later */
  927. static Scsi_Host_Template driver_template = EATA_PIO;
  928. #include "scsi_module.c"
  929. MODULE_LICENSE("GPL");
  930. /*
  931.  * Overrides for Emacs so that we almost follow Linus's tabbing style.
  932.  * Emacs will notice this stuff at the end of the file and automatically
  933.  * adjust the settings for this buffer only.  This must remain at the end
  934.  * of the file.
  935.  * ---------------------------------------------------------------------------
  936.  * Local variables:
  937.  * c-indent-level: 4
  938.  * c-brace-imaginary-offset: 0
  939.  * c-brace-offset: -4
  940.  * c-argdecl-indent: 4
  941.  * c-label-offset: -4
  942.  * c-continued-statement-offset: 4
  943.  * c-continued-brace-offset: 0
  944.  * indent-tabs-mode: nil
  945.  * tab-width: 8
  946.  * End:
  947.  */