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

Cache object: 46dfc9f7c5660c97077c9d2a53a37148


[ 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.