The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/i386/isa/wt.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 
    2 /*
    3  * Streamer tape driver for 386bsd and FreeBSD.
    4  * Supports Archive and Wangtek compatible QIC-02/QIC-36 boards.
    5  *
    6  * Copyright (C) 1993 by:
    7  *      Sergey Ryzhkov       <sir@kiae.su>
    8  *      Serge Vakulenko      <vak@zebub.msk.su>
    9  *
   10  * This software is distributed with NO WARRANTIES, not even the implied
   11  * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   12  *
   13  * Authors grant any other persons or organisations permission to use
   14  * or modify this software as long as this message is kept with the software,
   15  * all derivative works or modified versions.
   16  *
   17  * This driver is derived from the old 386bsd Wangtek streamer tape driver,
   18  * made by Robert Baron at CMU, based on Intel sources.
   19  * Authors thank Robert Baron, CMU and Intel and retain here
   20  * the original CMU copyright notice.
   21  *
   22  * Version 1.3, Thu Nov 11 12:09:13 MSK 1993
   23  * $FreeBSD$
   24  *
   25  */
   26 
   27 /*
   28  * Code for MTERASE added by John Lind (john@starfire.mn.org) 95/09/02.
   29  * This was very easy due to the excellent structure and clear coding
   30  * of the original driver.
   31  */
   32 
   33 /*
   34  * Copyright (c) 1989 Carnegie-Mellon University.
   35  * All rights reserved.
   36  *
   37  * Authors: Robert Baron
   38  *
   39  * Permission to use, copy, modify and distribute this software and
   40  * its documentation is hereby granted, provided that both the copyright
   41  * notice and this permission notice appear in all copies of the
   42  * software, derivative works or modified versions, and any portions
   43  * thereof, and that both notices appear in supporting documentation.
   44  *
   45  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
   46  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
   47  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
   48  *
   49  * Carnegie Mellon requests users of this software to return to
   50  *
   51  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
   52  *  School of Computer Science
   53  *  Carnegie Mellon University
   54  *  Pittsburgh PA 15213-3890
   55  *
   56  * any improvements or extensions that they make and grant Carnegie the
   57  * rights to redistribute these changes.
   58  */
   59 
   60 #include "wt.h"
   61 
   62 #include <sys/param.h>
   63 #include <sys/systm.h>
   64 #include <sys/kernel.h>
   65 #include <sys/buf.h>
   66 #include <sys/fcntl.h>
   67 #include <sys/malloc.h>
   68 #include <sys/mtio.h>
   69 #include <sys/conf.h>
   70 
   71 #include <machine/clock.h>
   72 
   73 #include <i386/isa/isa_device.h>
   74 #include <i386/isa/wtreg.h>
   75 
   76 
   77 /*
   78  * Uncomment this to enable internal device tracing.
   79  */
   80 #define TRACE(s)                /* printf s */
   81 
   82 #define WTPRI                   (PZERO+10)      /* sleep priority */
   83 
   84 /*
   85  * Wangtek controller ports
   86  */
   87 #define WT_CTLPORT(base)        ((base)+0)      /* control, write only */
   88 #define WT_STATPORT(base)       ((base)+0)      /* status, read only */
   89 #define WT_CMDPORT(base)        ((base)+1)      /* command, write only */
   90 #define WT_DATAPORT(base)       ((base)+1)      /* data, read only */
   91 #define WT_NPORT                2               /* 2 i/o ports */
   92 
   93 /* status port bits */
   94 #define WT_BUSY                 0x01            /* not ready bit define */
   95 #define WT_NOEXCEP              0x02            /* no exception bit define */
   96 #define WT_RESETMASK            0x07            /* to check after reset */
   97 #define WT_RESETVAL             0x05            /* state after reset */
   98 
   99 /* control port bits */
  100 #define WT_ONLINE               0x01            /* device selected */
  101 #define WT_RESET                0x02            /* reset command */
  102 #define WT_REQUEST              0x04            /* request command */
  103 #define WT_IEN                  0x08            /* enable dma */
  104 
  105 /*
  106  * Archive controller ports
  107  */
  108 #define AV_DATAPORT(base)       ((base)+0)      /* data, read only */
  109 #define AV_CMDPORT(base)        ((base)+0)      /* command, write only */
  110 #define AV_STATPORT(base)       ((base)+1)      /* status, read only */
  111 #define AV_CTLPORT(base)        ((base)+1)      /* control, write only */
  112 #define AV_SDMAPORT(base)       ((base)+2)      /* start dma */
  113 #define AV_RDMAPORT(base)       ((base)+3)      /* reset dma */
  114 #define AV_NPORT                4               /* 4 i/o ports */
  115 
  116 /* status port bits */
  117 #define AV_BUSY                 0x40            /* not ready bit define */
  118 #define AV_NOEXCEP              0x20            /* no exception bit define */
  119 #define AV_RESETMASK            0xf8            /* to check after reset */
  120 #define AV_RESETVAL             0x50            /* state after reset */
  121 
  122 /* control port bits */
  123 #define AV_RESET                0x80            /* reset command */
  124 #define AV_REQUEST              0x40            /* request command */
  125 #define AV_IEN                  0x20            /* enable interrupts */
  126 
  127 enum wttype {
  128         UNKNOWN = 0,                    /* unknown type, driver disabled */
  129         ARCHIVE,                        /* Archive Viper SC499, SC402 etc */
  130         WANGTEK                         /* Wangtek */
  131 };
  132 
  133 typedef struct {
  134         unsigned short err;             /* code for error encountered */
  135         unsigned short ercnt;           /* number of error blocks */
  136         unsigned short urcnt;           /* number of underruns */
  137 } wtstatus_t;
  138 
  139 typedef struct {
  140         enum wttype type;               /* type of controller */
  141         unsigned unit;                  /* unit number */
  142         unsigned port;                  /* base i/o port */
  143         unsigned chan;                  /* dma channel number, 1..3 */
  144         unsigned flags;                 /* state of tape drive */
  145         unsigned dens;                  /* tape density */
  146         int bsize;                      /* tape block size */
  147         void *buf;                      /* internal i/o buffer */
  148 
  149         void *dmavaddr;                 /* virtual address of dma i/o buffer */
  150         unsigned dmatotal;              /* size of i/o buffer */
  151         unsigned dmaflags;              /* i/o direction, ISADMA_READ or ISADMA_WRITE */
  152         unsigned dmacount;              /* resulting length of dma i/o */
  153 
  154         wtstatus_t error;               /* status of controller */
  155 
  156         unsigned short DATAPORT, CMDPORT, STATPORT, CTLPORT, SDMAPORT, RDMAPORT;
  157         unsigned char BUSY, NOEXCEP, RESETMASK, RESETVAL;
  158         unsigned char ONLINE, RESET, REQUEST, IEN;
  159 } wtinfo_t;
  160 
  161 static wtinfo_t wttab[NWT];                    /* tape info by unit number */
  162 
  163 static int wtwait (wtinfo_t *t, int catch, char *msg);
  164 static int wtcmd (wtinfo_t *t, int cmd);
  165 static int wtstart (wtinfo_t *t, unsigned mode, void *vaddr, unsigned len);
  166 static void wtdma (wtinfo_t *t);
  167 static timeout_t wtimer;
  168 static void wtclock (wtinfo_t *t);
  169 static int wtreset (wtinfo_t *t);
  170 static int wtsense (wtinfo_t *t, int verb, int ignor);
  171 static int wtstatus (wtinfo_t *t);
  172 static ointhand2_t wtintr;
  173 static void wtrewind (wtinfo_t *t);
  174 static int wtreadfm (wtinfo_t *t);
  175 static int wtwritefm (wtinfo_t *t);
  176 static int wtpoll (wtinfo_t *t, int mask, int bits);
  177 
  178 static  d_open_t        wtopen;
  179 static  d_close_t       wtclose;
  180 static  d_ioctl_t       wtioctl;
  181 static  d_strategy_t    wtstrategy;
  182 
  183 #define CDEV_MAJOR 10
  184 
  185 static struct cdevsw wt_cdevsw = {
  186         /* open */      wtopen,
  187         /* close */     wtclose,
  188         /* read */      physread,
  189         /* write */     physwrite,
  190         /* ioctl */     wtioctl,
  191         /* poll */      nopoll,
  192         /* mmap */      nommap,
  193         /* strategy */  wtstrategy,
  194         /* name */      "wt",
  195         /* maj */       CDEV_MAJOR,
  196         /* dump */      nodump,
  197         /* psize */     nopsize,
  198         /* flags */     0,
  199         /* bmaj */      -1
  200 };
  201 
  202 
  203 /*
  204  * Probe for the presence of the device.
  205  */
  206 static int 
  207 wtprobe (struct isa_device *id)
  208 {
  209         wtinfo_t *t = wttab + id->id_unit;
  210         static int once;
  211 
  212         if (!once++)
  213                 cdevsw_add(&wt_cdevsw);
  214 
  215         t->unit = id->id_unit;
  216         t->chan = id->id_drq;
  217         t->port = id->id_iobase;
  218         if (t->chan<1 || t->chan>3) {
  219                 printf ("wt%d: Bad drq=%d, should be 1..3\n", t->unit, t->chan);
  220                 return (0);
  221         }
  222 
  223         /* Try Wangtek. */
  224         t->type = WANGTEK;
  225         t->CTLPORT = WT_CTLPORT (t->port);  t->STATPORT = WT_STATPORT (t->port);
  226         t->CMDPORT = WT_CMDPORT (t->port);  t->DATAPORT = WT_DATAPORT (t->port);
  227         t->SDMAPORT = 0;                    t->RDMAPORT = 0;
  228         t->BUSY = WT_BUSY;                  t->NOEXCEP = WT_NOEXCEP;
  229         t->RESETMASK = WT_RESETMASK;        t->RESETVAL = WT_RESETVAL;
  230         t->ONLINE = WT_ONLINE;              t->RESET = WT_RESET;
  231         t->REQUEST = WT_REQUEST;            t->IEN = WT_IEN;
  232         if (wtreset (t))
  233                 return (WT_NPORT);
  234 
  235         /* Try Archive. */
  236         t->type = ARCHIVE;
  237         t->CTLPORT = AV_CTLPORT (t->port);  t->STATPORT = AV_STATPORT (t->port);
  238         t->CMDPORT = AV_CMDPORT (t->port);  t->DATAPORT = AV_DATAPORT (t->port);
  239         t->SDMAPORT = AV_SDMAPORT (t->port); t->RDMAPORT = AV_RDMAPORT (t->port);
  240         t->BUSY = AV_BUSY;                  t->NOEXCEP = AV_NOEXCEP;
  241         t->RESETMASK = AV_RESETMASK;        t->RESETVAL = AV_RESETVAL;
  242         t->ONLINE = 0;                      t->RESET = AV_RESET;
  243         t->REQUEST = AV_REQUEST;            t->IEN = AV_IEN;
  244         if (wtreset (t))
  245                 return (AV_NPORT);
  246 
  247         /* Tape controller not found. */
  248         t->type = UNKNOWN;
  249         return (0);
  250 }
  251 
  252 /*
  253  * Device is found, configure it.
  254  */
  255 static int
  256 wtattach (struct isa_device *id)
  257 {
  258         wtinfo_t *t = wttab + id->id_unit;
  259 
  260         id->id_ointr = wtintr;
  261         if (t->type == ARCHIVE) {
  262                 printf ("wt%d: type <Archive>\n", t->unit);
  263                 outb (t->RDMAPORT, 0);          /* reset dma */
  264         } else
  265                 printf ("wt%d: type <Wangtek>\n", t->unit);
  266         t->flags = TPSTART;                     /* tape is rewound */
  267         t->dens = -1;                           /* unknown density */
  268         isa_dmainit(t->chan, 1024);
  269 
  270         make_dev(&wt_cdevsw, id->id_unit, 0, 0, 0600, "rwt%d", id->id_unit);
  271         return (1);
  272 }
  273 
  274 struct isa_driver wtdriver = { wtprobe, wtattach, "wt", };
  275 
  276 /*
  277  * Open routine, called on every device open.
  278  */
  279 static int
  280 wtopen (dev_t dev, int flag, int fmt, struct proc *p)
  281 {
  282         int u = minor (dev) & T_UNIT;
  283         wtinfo_t *t = wttab + u;
  284         int error;
  285 
  286         if (u >= NWT || t->type == UNKNOWN)
  287                 return (ENXIO);
  288 
  289         /* Check that device is not in use */
  290         if (t->flags & TPINUSE)
  291                 return (EBUSY);
  292 
  293         /* If the tape is in rewound state, check the status and set density. */
  294         if (t->flags & TPSTART) {
  295                 /* If rewind is going on, wait */
  296                 error = wtwait (t, PCATCH, "wtrew");
  297                 if (error)
  298                         return (error);
  299 
  300                 /* Check the controller status */
  301                 if (! wtsense (t, 0, (flag & FWRITE) ? 0 : TP_WRP)) {
  302                         /* Bad status, reset the controller */
  303                         if (! wtreset (t))
  304                                 return (EIO);
  305                         if (! wtsense (t, 1, (flag & FWRITE) ? 0 : TP_WRP))
  306                                 return (EIO);
  307                 }
  308 
  309                 /* Set up tape density. */
  310                 if (t->dens != (minor (dev) & WT_DENSEL)) {
  311                         int d = 0;
  312 
  313                         switch (minor (dev) & WT_DENSEL) {
  314                         case WT_DENSDFLT: default: break; /* default density */
  315                         case WT_QIC11:  d = QIC_FMT11;  break; /* minor 010 */
  316                         case WT_QIC24:  d = QIC_FMT24;  break; /* minor 020 */
  317                         case WT_QIC120: d = QIC_FMT120; break; /* minor 030 */
  318                         case WT_QIC150: d = QIC_FMT150; break; /* minor 040 */
  319                         case WT_QIC300: d = QIC_FMT300; break; /* minor 050 */
  320                         case WT_QIC600: d = QIC_FMT600; break; /* minor 060 */
  321                         }
  322                         if (d) {
  323                                 /* Change tape density. */
  324                                 if (! wtcmd (t, d))
  325                                         return (EIO);
  326                                 if (! wtsense (t, 1, TP_WRP | TP_ILL))
  327                                         return (EIO);
  328 
  329                                 /* Check the status of the controller. */
  330                                 if (t->error.err & TP_ILL) {
  331                                         printf ("wt%d: invalid tape density\n", t->unit);
  332                                         return (ENODEV);
  333                                 }
  334                         }
  335                         t->dens = minor (dev) & WT_DENSEL;
  336                 }
  337                 t->flags &= ~TPSTART;
  338         } else if (t->dens != (minor (dev) & WT_DENSEL))
  339                 return (ENXIO);
  340 
  341         t->bsize = (minor (dev) & WT_BSIZE) ? 1024 : 512;
  342         t->buf = malloc (t->bsize, M_TEMP, M_WAITOK);
  343         if (! t->buf)
  344                 return (EAGAIN);
  345 
  346         if (isa_dma_acquire(t->chan))
  347                 return(EBUSY);
  348 
  349         t->flags = TPINUSE;
  350 
  351         if (flag & FREAD)
  352                 t->flags |= TPREAD;
  353         if (flag & FWRITE)
  354                 t->flags |= TPWRITE;
  355         return (0);
  356 }
  357 
  358 /*
  359  * Close routine, called on last device close.
  360  */
  361 static int
  362 wtclose (dev_t dev, int flags, int fmt, struct proc *p)
  363 {
  364         int u = minor (dev) & T_UNIT;
  365         wtinfo_t *t = wttab + u;
  366 
  367         if (u >= NWT || t->type == UNKNOWN)
  368                 return (ENXIO);
  369 
  370         /* If rewind is pending, do nothing */
  371         if (t->flags & TPREW)
  372                 goto done;
  373 
  374         /* If seek forward is pending and no rewind on close, do nothing */
  375         if (t->flags & TPRMARK) {
  376                 if (minor (dev) & T_NOREWIND)
  377                         goto done;
  378 
  379                 /* If read file mark is going on, wait */
  380                 wtwait (t, 0, "wtrfm");
  381         }
  382 
  383         if (t->flags & TPWANY)
  384                 /* Tape was written.  Write file mark. */
  385                 wtwritefm (t);
  386 
  387         if (! (minor (dev) & T_NOREWIND)) {
  388                 /* Rewind tape to beginning of tape. */
  389                 /* Don't wait until rewind, though. */
  390                 wtrewind (t);
  391                 goto done;
  392         }
  393         if ((t->flags & TPRANY) && ! (t->flags & (TPVOL | TPWANY)))
  394                 /* Space forward to after next file mark if no writing done. */
  395                 /* Don't wait for completion. */
  396                 wtreadfm (t);
  397 done:
  398         t->flags &= TPREW | TPRMARK | TPSTART | TPTIMER;
  399         free (t->buf, M_TEMP);
  400         isa_dma_release(t->chan);
  401         return (0);
  402 }
  403 
  404 /*
  405  * Ioctl routine.  Compatible with BSD ioctls.
  406  * There are two possible ioctls:
  407  * ioctl (int fd, MTIOCGET, struct mtget *buf)  -- get status
  408  * ioctl (int fd, MTIOCTOP, struct mtop *buf)   -- do BSD-like op
  409  */
  410 static int
  411 wtioctl (dev_t dev, u_long cmd, caddr_t arg, int flags, struct proc *p)
  412 {
  413         int u = minor (dev) & T_UNIT;
  414         wtinfo_t *t = wttab + u;
  415         int error, count, op;
  416 
  417         if (u >= NWT || t->type == UNKNOWN)
  418                 return (ENXIO);
  419 
  420         switch (cmd) {
  421         default:
  422                 return (EINVAL);
  423         case MTIOCIEOT:         /* ignore EOT errors */
  424         case MTIOCEEOT:         /* enable EOT errors */
  425                 return (0);
  426         case MTIOCGET:
  427                 ((struct mtget*)arg)->mt_type =
  428                         t->type == ARCHIVE ? MT_ISVIPER1 : 0x11;
  429                 ((struct mtget*)arg)->mt_dsreg = t->flags;      /* status */
  430                 ((struct mtget*)arg)->mt_erreg = t->error.err;  /* errors */
  431                 ((struct mtget*)arg)->mt_resid = 0;
  432                 ((struct mtget*)arg)->mt_fileno = 0;            /* file */
  433                 ((struct mtget*)arg)->mt_blkno = 0;             /* block */
  434                 return (0);
  435         case MTIOCTOP:
  436                 break;
  437         }
  438         switch ((short) ((struct mtop*)arg)->mt_op) {
  439         default:
  440         case MTFSR:             /* forward space record */
  441         case MTBSR:             /* backward space record */
  442         case MTBSF:             /* backward space file */
  443                 break;
  444         case MTNOP:             /* no operation, sets status only */
  445         case MTCACHE:           /* enable controller cache */
  446         case MTNOCACHE:         /* disable controller cache */
  447                 return (0);
  448         case MTREW:             /* rewind */
  449         case MTOFFL:            /* rewind and put the drive offline */
  450                 if (t->flags & TPREW)   /* rewind is running */
  451                         return (0);
  452                 error = wtwait (t, PCATCH, "wtorew");
  453                 if (error)
  454                         return (error);
  455                 wtrewind (t);
  456                 return (0);
  457         case MTFSF:             /* forward space file */
  458                 for (count=((struct mtop*)arg)->mt_count; count>0; --count) {
  459                         error = wtwait (t, PCATCH, "wtorfm");
  460                         if (error)
  461                                 return (error);
  462                         error = wtreadfm (t);
  463                         if (error)
  464                                 return (error);
  465                 }
  466                 return (0);
  467         case MTWEOF:            /* write an end-of-file record */
  468                 if (! (t->flags & TPWRITE) || (t->flags & TPWP))
  469                         return (EACCES);
  470                 error = wtwait (t, PCATCH, "wtowfm");
  471                 if (error)
  472                         return (error);
  473                 error = wtwritefm (t);
  474                 if (error)
  475                         return (error);
  476                 return (0);
  477         case MTRETENS:          /* re-tension tape */
  478                 error = wtwait (t, PCATCH, "wtretens");
  479                 if (error)
  480                         return (error);
  481                 op = QIC_RETENS;
  482                 goto erase_retens;
  483                 
  484         case MTERASE:           /* erase to EOM */
  485                 if (! (t->flags & TPWRITE) || (t->flags & TPWP))
  486                         return (EACCES);
  487                 error = wtwait (t, PCATCH, "wterase");
  488                 if (error)
  489                         return (error);
  490                 op = QIC_ERASE;
  491         erase_retens:
  492                 /* ERASE and RETENS operations work like REWIND. */
  493                 /* Simulate the rewind operation here. */
  494                 t->flags &= ~(TPRO | TPWO | TPVOL);
  495                 if (! wtcmd (t, op))
  496                         return (EIO);
  497                 t->flags |= TPSTART | TPREW;
  498                 t->flags |= TPWANY;
  499                 wtclock (t);
  500                 return (0);
  501         }
  502         return (EINVAL);
  503 }
  504 
  505 /*
  506  * Strategy routine.
  507  */
  508 static void
  509 wtstrategy (struct buf *bp)
  510 {
  511         int u = minor (bp->b_dev) & T_UNIT;
  512         wtinfo_t *t = wttab + u;
  513         int s;
  514 
  515         bp->b_resid = bp->b_bcount;
  516         if (u >= NWT || t->type == UNKNOWN) {
  517                 bp->b_error = ENXIO;
  518                 goto err2xit;
  519         }
  520 
  521         /* at file marks and end of tape, we just return '0 bytes available' */
  522         if (t->flags & TPVOL)
  523                 goto xit;
  524 
  525         if (bp->b_bcount % t->bsize != 0) {
  526                 bp->b_error = EINVAL;
  527                 goto err2xit;
  528         }
  529 
  530         if (bp->b_flags & B_READ) {
  531                 /* Check read access and no previous write to this tape. */
  532                 if (! (t->flags & TPREAD) || (t->flags & TPWANY))
  533                         goto errxit;
  534 
  535                 /* For now, we assume that all data will be copied out */
  536                 /* If read command outstanding, just skip down */
  537                 if (! (t->flags & TPRO)) {
  538                         if (! wtsense (t, 1, TP_WRP))   /* clear status */
  539                                 goto errxit;
  540                         if (! wtcmd (t, QIC_RDDATA)) {  /* sed read mode */
  541                                 wtsense (t, 1, TP_WRP);
  542                                 goto errxit;
  543                         }
  544                         t->flags |= TPRO | TPRANY;
  545                 }
  546         } else {
  547                 /* Check write access and write protection. */
  548                 /* No previous read from this tape allowed. */
  549                 if (! (t->flags & TPWRITE) || (t->flags & (TPWP | TPRANY)))
  550                         goto errxit;
  551 
  552                 /* If write command outstanding, just skip down */
  553                 if (! (t->flags & TPWO)) {
  554                         if (! wtsense (t, 1, 0))        /* clear status */
  555                                 goto errxit;
  556                         if (! wtcmd (t, QIC_WRTDATA)) { /* set write mode */
  557                                 wtsense (t, 1, 0);
  558                                 goto errxit;
  559                         }
  560                         t->flags |= TPWO | TPWANY;
  561                 }
  562         }
  563 
  564         if (! bp->b_bcount)
  565                 goto xit;
  566 
  567         t->flags &= ~TPEXCEP;
  568         s = splbio ();
  569         if (wtstart (t, bp->b_flags & B_READ ? ISADMA_READ : ISADMA_WRITE, bp->b_data, bp->b_bcount)) {
  570                 wtwait (t, 0, (bp->b_flags & B_READ) ? "wtread" : "wtwrite");
  571                 bp->b_resid -= t->dmacount;
  572         }
  573         splx (s);
  574 
  575         if (t->flags & TPEXCEP) {
  576 errxit:         bp->b_error = EIO;
  577 err2xit:        bp->b_flags |= B_ERROR;
  578         }
  579 xit:    biodone (bp);
  580         return;
  581 }
  582 
  583 /*
  584  * Interrupt routine.
  585  */
  586 static void
  587 wtintr (int u)
  588 {
  589         wtinfo_t *t = wttab + u;
  590         unsigned char s;
  591 
  592         if (u >= NWT || t->type == UNKNOWN) {
  593                 TRACE (("wtintr() -- device not configured\n"));
  594                 return;
  595         }
  596 
  597         s = inb (t->STATPORT);                  /* get status */
  598         TRACE (("wtintr() status=0x%x -- ", s));
  599         if ((s & (t->BUSY | t->NOEXCEP)) == (t->BUSY | t->NOEXCEP)) {
  600                 TRACE (("busy\n"));
  601                 return;                         /* device is busy */
  602         }
  603 
  604         /*
  605          * Check if rewind finished.
  606          */
  607         if (t->flags & TPREW) {
  608                 TRACE (((s & (t->BUSY | t->NOEXCEP)) == (t->BUSY | t->NOEXCEP) ?
  609                         "rewind busy?\n" : "rewind finished\n"));
  610                 t->flags &= ~TPREW;             /* Rewind finished. */
  611                 wtsense (t, 1, TP_WRP);
  612                 wakeup ((caddr_t)t);
  613                 return;
  614         }
  615 
  616         /*
  617          * Check if writing/reading of file mark finished.
  618          */
  619         if (t->flags & (TPRMARK | TPWMARK)) {
  620                 TRACE (((s & (t->BUSY | t->NOEXCEP)) == (t->BUSY | t->NOEXCEP) ?
  621                         "marker r/w busy?\n" : "marker r/w finished\n"));
  622                 if (! (s & t->NOEXCEP))         /* operation failed */
  623                         wtsense (t, 1, (t->flags & TPRMARK) ? TP_WRP : 0);
  624                 t->flags &= ~(TPRMARK | TPWMARK); /* operation finished */
  625                 wakeup ((caddr_t)t);
  626                 return;
  627         }
  628 
  629         /*
  630          * Do we started any i/o?  If no, just return.
  631          */
  632         if (! (t->flags & TPACTIVE)) {
  633                 TRACE (("unexpected interrupt\n"));
  634                 return;
  635         }
  636 
  637         /*
  638          * Clean up dma.
  639          */
  640         if ((t->dmaflags & ISADMA_READ) && (t->dmatotal - t->dmacount) < t->bsize) {
  641                 /* If reading short block, copy the internal buffer
  642                  * to the user memory. */
  643                 isa_dmadone (t->dmaflags, t->buf, t->bsize, t->chan);
  644                 bcopy (t->buf, t->dmavaddr, t->dmatotal - t->dmacount);
  645         } else
  646                 isa_dmadone (t->dmaflags, t->dmavaddr, t->bsize, t->chan);
  647 
  648         t->flags &= ~TPACTIVE;
  649         t->dmacount += t->bsize;
  650         t->dmavaddr = (char *)t->dmavaddr + t->bsize;
  651 
  652         /*
  653          * On exception, check for end of file and end of volume.
  654          */
  655         if (! (s & t->NOEXCEP)) {
  656                 TRACE (("i/o exception\n"));
  657                 wtsense (t, 1, (t->dmaflags & ISADMA_READ) ? TP_WRP : 0);
  658                 if (t->error.err & (TP_EOM | TP_FIL))
  659                         t->flags |= TPVOL;      /* end of file */
  660                 else
  661                         t->flags |= TPEXCEP;    /* i/o error */
  662                 wakeup ((caddr_t)t);
  663                 return;
  664         }
  665 
  666         if (t->dmacount < t->dmatotal) {        /* continue i/o */
  667                 wtdma (t);
  668                 TRACE (("continue i/o, %d\n", t->dmacount));
  669                 return;
  670         }
  671         if (t->dmacount > t->dmatotal)          /* short last block */
  672                 t->dmacount = t->dmatotal;
  673         wakeup ((caddr_t)t);    /* wake up user level */
  674         TRACE (("i/o finished, %d\n", t->dmacount));
  675 }
  676 
  677 /* start the rewind operation */
  678 static void
  679 wtrewind (wtinfo_t *t)
  680 {
  681         int rwmode = (t->flags & (TPRO | TPWO));
  682 
  683         t->flags &= ~(TPRO | TPWO | TPVOL);
  684         /*
  685          * Wangtek strictly follows QIC-02 standard:
  686          * clearing ONLINE in read/write modes causes rewind.
  687          * REWIND command is not allowed in read/write mode
  688          * and gives `illegal command' error.
  689          */
  690         if (t->type==WANGTEK && rwmode) {
  691                 outb (t->CTLPORT, 0);
  692         } else if (! wtcmd (t, QIC_REWIND))
  693                 return;
  694         t->flags |= TPSTART | TPREW;
  695         wtclock (t);
  696 }
  697 
  698 /* start the `read marker' operation */
  699 static int
  700 wtreadfm (wtinfo_t *t)
  701 {
  702         t->flags &= ~(TPRO | TPWO | TPVOL);
  703         if (! wtcmd (t, QIC_READFM)) {
  704                 wtsense (t, 1, TP_WRP);
  705                 return (EIO);
  706         }
  707         t->flags |= TPRMARK | TPRANY;
  708         wtclock (t);
  709         /* Don't wait for completion here. */
  710         return (0);
  711 }
  712 
  713 /* write marker to the tape */
  714 static int
  715 wtwritefm (wtinfo_t *t)
  716 {
  717         tsleep ((caddr_t)wtwritefm, WTPRI, "wtwfm", hz); /* timeout: 1 second */
  718         t->flags &= ~(TPRO | TPWO);
  719         if (! wtcmd (t, QIC_WRITEFM)) {
  720                 wtsense (t, 1, 0);
  721                 return (EIO);
  722         }
  723         t->flags |= TPWMARK | TPWANY;
  724         wtclock (t);
  725         return (wtwait (t, 0, "wtwfm"));
  726 }
  727 
  728 /* while controller status & mask == bits continue waiting */
  729 static int
  730 wtpoll (wtinfo_t *t, int mask, int bits)
  731 {
  732         int s, i;
  733 
  734         /* Poll status port, waiting for specified bits. */
  735         for (i=0; i<1000; ++i) {                        /* up to 1 msec */
  736                 s = inb (t->STATPORT);
  737                 if ((s & mask) != bits)
  738                         return (s);
  739                 DELAY (1);
  740         }
  741         for (i=0; i<100; ++i) {                         /* up to 10 msec */
  742                 s = inb (t->STATPORT);
  743                 if ((s & mask) != bits)
  744                         return (s);
  745                 DELAY (100);
  746         }
  747         for (;;) {                                      /* forever */
  748                 s = inb (t->STATPORT);
  749                 if ((s & mask) != bits)
  750                         return (s);
  751                 tsleep ((caddr_t)wtpoll, WTPRI, "wtpoll", 1); /* timeout: 1 tick */
  752         }
  753 }
  754 
  755 /* execute QIC command */
  756 static int
  757 wtcmd (wtinfo_t *t, int cmd)
  758 {
  759         int s, x;
  760 
  761         TRACE (("wtcmd() cmd=0x%x\n", cmd));
  762         x = splbio();
  763         s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
  764         if (! (s & t->NOEXCEP)) {                       /* error */
  765                 splx(x);
  766                 return (0);
  767         }
  768 
  769         outb (t->CMDPORT, cmd);                         /* output the command */
  770 
  771         outb (t->CTLPORT, t->REQUEST | t->ONLINE);      /* set request */
  772         wtpoll (t, t->BUSY, t->BUSY);                   /* wait for ready */
  773         outb (t->CTLPORT, t->IEN | t->ONLINE);          /* reset request */
  774         wtpoll (t, t->BUSY, 0);                         /* wait for not ready */
  775         splx(x);
  776         return (1);
  777 }
  778 
  779 /* wait for the end of i/o, seeking marker or rewind operation */
  780 static int
  781 wtwait (wtinfo_t *t, int catch, char *msg)
  782 {
  783         int error;
  784 
  785         TRACE (("wtwait() `%s'\n", msg));
  786         while (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)) {
  787                 error = tsleep ((caddr_t)t, WTPRI | catch, msg, 0);
  788                 if (error)
  789                         return (error);
  790         }
  791         return (0);
  792 }
  793 
  794 /* initialize dma for the i/o operation */
  795 static void
  796 wtdma (wtinfo_t *t)
  797 {
  798         t->flags |= TPACTIVE;
  799         wtclock (t);
  800 
  801         if (t->type == ARCHIVE)
  802                 outb (t->SDMAPORT, 0);          /* set dma */
  803 
  804         if ((t->dmaflags & ISADMA_READ) && (t->dmatotal - t->dmacount) < t->bsize)
  805                 /* Reading short block.  Do it through the internal buffer. */
  806                 isa_dmastart (t->dmaflags, t->buf, t->bsize, t->chan);
  807         else
  808                 isa_dmastart (t->dmaflags, t->dmavaddr, t->bsize, t->chan);
  809 }
  810 
  811 /* start i/o operation */
  812 static int
  813 wtstart (wtinfo_t *t, unsigned flags, void *vaddr, unsigned len)
  814 {
  815         int s, x;
  816 
  817         TRACE (("wtstart()\n"));
  818         x = splbio();
  819         s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
  820         if (! (s & t->NOEXCEP)) {
  821                 t->flags |= TPEXCEP;            /* error */
  822                 splx(x);
  823                 return (0);
  824         }
  825         t->flags &= ~TPEXCEP;                   /* clear exception flag */
  826         t->dmavaddr = vaddr;
  827         t->dmatotal = len;
  828         t->dmacount = 0;
  829         t->dmaflags = flags;
  830         wtdma (t);
  831         splx(x);
  832         return (1);
  833 }
  834 
  835 /* start timer */
  836 static void
  837 wtclock (wtinfo_t *t)
  838 {
  839         if (! (t->flags & TPTIMER)) {
  840                 t->flags |= TPTIMER;
  841                 /* Some controllers seem to lose dma interrupts too often.
  842                  * To make the tape stream we need 1 tick timeout. */
  843                 timeout (wtimer, (caddr_t)t, (t->flags & TPACTIVE) ? 1 : hz);
  844         }
  845 }
  846 
  847 /*
  848  * Simulate an interrupt periodically while i/o is going.
  849  * This is necessary in case interrupts get eaten due to
  850  * multiple devices on a single IRQ line.
  851  */
  852 static void
  853 wtimer (void *xt)
  854 {
  855         wtinfo_t *t = (wtinfo_t *)xt;
  856         int s;
  857 
  858         t->flags &= ~TPTIMER;
  859         if (! (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK)))
  860                 return;
  861 
  862         /* If i/o going, simulate interrupt. */
  863         s = splbio ();
  864         if ((inb (t->STATPORT) & (t->BUSY | t->NOEXCEP)) != (t->BUSY | t->NOEXCEP)) {
  865                 TRACE (("wtimer() -- "));
  866                 wtintr (t->unit);
  867         }
  868         splx (s);
  869 
  870         /* Restart timer if i/o pending. */
  871         if (t->flags & (TPACTIVE | TPREW | TPRMARK | TPWMARK))
  872                 wtclock (t);
  873 }
  874 
  875 /* reset the controller */
  876 static int
  877 wtreset (wtinfo_t *t)
  878 {
  879         /* Perform QIC-02 and QIC-36 compatible reset sequence. */
  880         /* Thanks to Mikael Hybsch <micke@dynas.se>. */
  881         int s, i;
  882 
  883         outb (t->CTLPORT, t->RESET | t->ONLINE); /* send reset */
  884         DELAY (30);
  885         outb (t->CTLPORT, t->ONLINE);           /* turn off reset */
  886         DELAY (30);
  887 
  888         /* Read the controller status. */
  889         s = inb (t->STATPORT);
  890         if (s == 0xff)                          /* no port at this address? */
  891                 return (0);
  892 
  893         /* Wait 3 sec for reset to complete. Needed for QIC-36 boards? */
  894         for (i=0; i<3000; ++i) {
  895                 if (! (s & t->BUSY) || ! (s & t->NOEXCEP))
  896                         break;
  897                 DELAY (1000);
  898                 s = inb (t->STATPORT);
  899         }
  900         return ((s & t->RESETMASK) == t->RESETVAL);
  901 }
  902 
  903 /* get controller status information */
  904 /* return 0 if user i/o request should receive an i/o error code */
  905 static int
  906 wtsense (wtinfo_t *t, int verb, int ignor)
  907 {
  908         char *msg = 0;
  909         int err;
  910 
  911         TRACE (("wtsense() ignor=0x%x\n", ignor));
  912         t->flags &= ~(TPRO | TPWO);
  913         if (! wtstatus (t))
  914                 return (0);
  915         if (! (t->error.err & TP_ST0))
  916                 t->error.err &= ~TP_ST0MASK;
  917         if (! (t->error.err & TP_ST1))
  918                 t->error.err &= ~TP_ST1MASK;
  919         t->error.err &= ~ignor;         /* ignore certain errors */
  920         err = t->error.err & (TP_FIL | TP_BNL | TP_UDA | TP_EOM | TP_WRP |
  921                 TP_USL | TP_CNI | TP_MBD | TP_NDT | TP_ILL);
  922         if (! err)
  923                 return (1);
  924         if (! verb)
  925                 return (0);
  926 
  927         /* lifted from tdriver.c from Wangtek */
  928         if      (err & TP_USL)  msg = "Drive not online";
  929         else if (err & TP_CNI)  msg = "No cartridge";
  930         else if ((err & TP_WRP) && !(t->flags & TPWP)) {
  931                 msg = "Tape is write protected";
  932                 t->flags |= TPWP;
  933         }
  934         else if (err & TP_FIL)  msg = 0 /*"Filemark detected"*/;
  935         else if (err & TP_EOM)  msg = 0 /*"End of tape"*/;
  936         else if (err & TP_BNL)  msg = "Block not located";
  937         else if (err & TP_UDA)  msg = "Unrecoverable data error";
  938         else if (err & TP_NDT)  msg = "No data detected";
  939         else if (err & TP_ILL)  msg = "Illegal command";
  940         if (msg)
  941                 printf ("wt%d: %s\n", t->unit, msg);
  942         return (0);
  943 }
  944 
  945 /* get controller status information */
  946 static int
  947 wtstatus (wtinfo_t *t)
  948 {
  949         char *p;
  950         int x;
  951 
  952         x = splbio();
  953         wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP); /* ready? */
  954         outb (t->CMDPORT, QIC_RDSTAT);  /* send `read status' command */
  955 
  956         outb (t->CTLPORT, t->REQUEST | t->ONLINE);      /* set request */
  957         wtpoll (t, t->BUSY, t->BUSY);                   /* wait for ready */
  958         outb (t->CTLPORT, t->ONLINE);                   /* reset request */
  959         wtpoll (t, t->BUSY, 0);                         /* wait for not ready */
  960 
  961         p = (char*) &t->error;
  962         while (p < (char*)&t->error + 6) {
  963                 int s = wtpoll (t, t->BUSY | t->NOEXCEP, t->BUSY | t->NOEXCEP);
  964                 if (! (s & t->NOEXCEP)) {               /* error */
  965                         splx(x);
  966                         return (0);
  967                 }
  968 
  969                 *p++ = inb (t->DATAPORT);               /* read status byte */
  970 
  971                 outb (t->CTLPORT, t->REQUEST | t->ONLINE); /* set request */
  972                 wtpoll (t, t->BUSY, 0);                 /* wait for not ready */
  973                 DELAY(20);
  974                 outb (t->CTLPORT, t->ONLINE);           /* unset request */
  975         }
  976         splx(x);
  977         return (1);
  978 }

Cache object: 6731b35d9398b7751087e3608a44a96e


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.