Home | History | Annotate | Line # | Download | only in dev
md_root.c revision 1.15.8.3
      1 /*	$NetBSD: md_root.c,v 1.15.8.3 2002/06/24 22:04:11 nathanw Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Leo Weppelman.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *      This product includes software developed by Leo Weppelman.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/param.h>
     34 #include <sys/systm.h>
     35 #include <sys/kernel.h>
     36 #include <sys/malloc.h>
     37 #include <sys/buf.h>
     38 #include <sys/lwp.h>
     39 #include <sys/proc.h>
     40 #include <sys/device.h>
     41 #include <sys/ioctl.h>
     42 #include <sys/fcntl.h>
     43 #include <sys/conf.h>
     44 #include <sys/disklabel.h>
     45 #include <sys/disk.h>
     46 #include <sys/dkbad.h>
     47 
     48 #include <dev/cons.h>
     49 #include <dev/md.h>
     50 
     51 #include <atari/atari/device.h>
     52 
     53 /*
     54  * Misc. defines:
     55  */
     56 #define	RAMD_CHUNK	(9 * 512)	/* Chunk-size for auto-load	*/
     57 #define	RAMD_NDEV	3		/* Number of devices configured	*/
     58 
     59 struct   ramd_info {
     60 	u_long	ramd_size;  /* Size of disk in bytes			*/
     61 	u_long	ramd_flag;  /* see defs below				*/
     62 	dev_t	ramd_dev;   /* device to load from			*/
     63 };
     64 
     65 /*
     66  * ramd_flag:
     67  */
     68 #define	RAMD_LOAD	0x01	/* Auto load when first opened	*/
     69 #define	RAMD_LCOMP	0x02	/* Input is compressed		*/
     70 
     71 struct ramd_info rd_info[RAMD_NDEV] = {
     72     {
     73 	1105920,		/*	1Mb in 2160 sectors		*/
     74 	RAMD_LOAD,		/* auto-load this device		*/
     75 	MAKEDISKDEV(2, 0, 2),	/* XXX: This is crap! (720Kb flop)	*/
     76     },
     77     {
     78 	1474560,		/* 1.44Mb in 2880 sectors		*/
     79 	RAMD_LOAD,		/* auto-load this device		*/
     80 	MAKEDISKDEV(2, 0, 2),	/* XXX: This is crap! (720Kb flop)	*/
     81     },
     82     {
     83 	1474560,		/* 1.44Mb in 2880 sectors		*/
     84 	RAMD_LOAD,		/* auto-load this device		*/
     85 	MAKEDISKDEV(2, 0, 3),	/* XXX: This is crap! (1.44Mb flop)	*/
     86     }
     87 };
     88 
     89 struct read_info {
     90     struct buf	*bp;		/* buffer for strategy function		*/
     91     long	nbytes;		/* total number of bytes to read	*/
     92     long	offset;		/* offset in input medium		*/
     93     caddr_t	bufp;		/* current output buffer		*/
     94     caddr_t	ebufp;		/* absolute maximum for bufp		*/
     95     int		chunk;		/* chunk size on input medium		*/
     96     int		media_sz;	/* size of input medium			*/
     97     void	(*strat)(struct buf *);	/* strategy function for read	*/
     98 };
     99 
    100 
    101 static int  loaddisk __P((struct  md_conf *, dev_t ld_dev, struct proc *));
    102 static int  ramd_norm_read __P((struct read_info *));
    103 
    104 #ifdef support_compression
    105 static int  cpy_uncompressed __P((caddr_t, int, struct read_info *));
    106 static int  md_compressed __P((caddr_t, int, struct read_info *));
    107 #endif
    108 
    109 /*
    110  * This is called during autoconfig.
    111  */
    112 void
    113 md_attach_hook(unit, md)
    114 int		unit;
    115 struct md_conf	*md;
    116 {
    117 	if (atari_realconfig && (unit < RAMD_NDEV) && rd_info[unit].ramd_flag) {
    118 		printf ("md%d: %sauto-load on open. Size %ld bytes.\n", unit,
    119 		    rd_info[unit].ramd_flag & RAMD_LCOMP ? "decompress/" : "",
    120 		    rd_info[unit].ramd_size);
    121 		md->md_type = MD_UNCONFIGURED; /* Paranoia... */
    122 	}
    123 }
    124 
    125 void
    126 md_open_hook(unit, md)
    127 int		unit;
    128 struct md_conf	*md;
    129 {
    130 	struct ramd_info *ri;
    131 
    132 	if(unit >= RAMD_NDEV)
    133 		return;
    134 
    135 	ri = &rd_info[unit];
    136 	if (md->md_type != MD_UNCONFIGURED)
    137 		return;	/* Only configure once */
    138 	md->md_addr = malloc(ri->ramd_size, M_DEVBUF, M_WAITOK);
    139 	md->md_size = ri->ramd_size;
    140 	if(md->md_addr == NULL)
    141 		return;
    142 	if(ri->ramd_flag & RAMD_LOAD) {
    143 		if (loaddisk(md, ri->ramd_dev, curproc)) {
    144 			free(md->md_addr, M_DEVBUF);
    145 			md->md_addr = NULL;
    146 			return;
    147 		}
    148 	}
    149 	md->md_type = MD_KMEM_ALLOCATED;
    150 }
    151 
    152 static int
    153 loaddisk(md, ld_dev, proc)
    154 struct md_conf		*md;
    155 dev_t			ld_dev;
    156 struct proc		*proc;
    157 {
    158 	struct buf		buf;
    159 	int			error;
    160 	struct bdevsw		*bdp = &bdevsw[major(ld_dev)];
    161 	struct disklabel	dl;
    162 	struct read_info	rs;
    163 
    164 	/*
    165 	 * Initialize our buffer header:
    166 	 */
    167 	memset(&buf, 0, sizeof(buf));
    168 	buf.b_vnbufs.le_next = NOLIST;
    169 	buf.b_flags = B_BUSY;
    170 	buf.b_dev   = ld_dev;
    171 	buf.b_error = 0;
    172 	buf.b_proc  = proc;
    173 
    174 	/*
    175 	 * Setup read_info:
    176 	 */
    177 	rs.bp       = &buf;
    178 	rs.nbytes   = md->md_size;
    179 	rs.offset   = 0;
    180 	rs.bufp     = md->md_addr;
    181 	rs.ebufp    = md->md_addr + md->md_size;
    182 	rs.chunk    = RAMD_CHUNK;
    183 	rs.media_sz = md->md_size;
    184 	rs.strat    = bdp->d_strategy;
    185 
    186 	/*
    187 	 * Open device and try to get some statistics.
    188 	 */
    189 	if((error = bdp->d_open(ld_dev, FREAD | FNONBLOCK, 0, proc)) != 0)
    190 		return(error);
    191 	if(bdp->d_ioctl(ld_dev, DIOCGDINFO, (caddr_t)&dl, FREAD, proc) == 0) {
    192 		/* Read on a cylinder basis */
    193 		rs.chunk    = dl.d_secsize * dl.d_secpercyl;
    194 		rs.media_sz = dl.d_secperunit * dl.d_secsize;
    195 	}
    196 
    197 #ifdef support_compression
    198 	if(ri->ramd_flag & RAMD_LCOMP)
    199 		error = decompress(cpy_uncompressed, md_compressed, &rs);
    200 	else
    201 #endif /* support_compression */
    202 		error = ramd_norm_read(&rs);
    203 
    204 	bdp->d_close(ld_dev,FREAD | FNONBLOCK, 0, proc);
    205 	return(error);
    206 }
    207 
    208 static int
    209 ramd_norm_read(rsp)
    210 struct read_info	*rsp;
    211 {
    212 	long		bytes_left;
    213 	int		done, error;
    214 	struct buf	*bp;
    215 	int		s;
    216 	int		dotc = 0;
    217 
    218 	bytes_left = rsp->nbytes;
    219 	bp         = rsp->bp;
    220 	error      = 0;
    221 
    222 	while(bytes_left > 0) {
    223 		s = splbio();
    224 		bp->b_flags = B_BUSY | B_PHYS | B_READ;
    225 		splx(s);
    226 		bp->b_blkno  = btodb(rsp->offset);
    227 		bp->b_bcount = rsp->chunk;
    228 		bp->b_data   = rsp->bufp;
    229 
    230 		/* Initiate read */
    231 		(*rsp->strat)(bp);
    232 
    233 		/* Wait for results	*/
    234 		s = splbio();
    235 		while ((bp->b_flags & B_DONE) == 0)
    236 			tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
    237 		if (bp->b_flags & B_ERROR)
    238 			error = (bp->b_error ? bp->b_error : EIO);
    239 		splx(s);
    240 
    241 		/* Dot counter */
    242 		printf(".");
    243 		if(!(++dotc % 40))
    244 			printf("\n");
    245 
    246 		done = bp->b_bcount - bp->b_resid;
    247 		bytes_left   -= done;
    248 		rsp->offset  += done;
    249 		rsp->bufp    += done;
    250 
    251 		if(error || !done)
    252 			break;
    253 
    254 		if((rsp->offset == rsp->media_sz) && (bytes_left != 0)) {
    255 			printf("\nInsert next media and hit any key...");
    256 			cngetc();
    257 			printf("\n");
    258 			rsp->offset = 0;
    259 		}
    260 	}
    261 	printf("\n");
    262 	return(error);
    263 }
    264 
    265 #ifdef support_compression
    266 /*
    267  * Functions supporting uncompression:
    268  */
    269 /*
    270  * Copy from the uncompression buffer to the ramdisk
    271  */
    272 static int
    273 cpy_uncompressed(buf, nbyte, rsp)
    274 caddr_t			buf;
    275 struct read_info	*rsp;
    276 int			nbyte;
    277 {
    278 	if((rsp->bufp + nbyte) >= rsp->ebufp)
    279 		return(0);
    280 	bcopy(buf, rsp->bufp, nbyte);
    281 	rsp->bufp += nbyte;
    282 	return(0);
    283 }
    284 
    285 /*
    286  * Read a maximum of 'nbyte' bytes into 'buf'.
    287  */
    288 static int
    289 md_compressed(buf, nbyte, rsp)
    290 caddr_t			buf;
    291 struct read_info	*rsp;
    292 int			nbyte;
    293 {
    294 	static int	dotc = 0;
    295 	struct buf	*bp;
    296 	       int	nread = 0;
    297 	       int	s;
    298 	       int	done, error;
    299 
    300 
    301 	error  = 0;
    302 	bp     = rsp->bp;
    303 	nbyte &= ~(DEV_BSIZE - 1);
    304 
    305 	while(nbyte > 0) {
    306 		s = splbio();
    307 		bp->b_flags = B_BUSY | B_PHYS | B_READ;
    308 		splx(s);
    309 		bp->b_blkno  = btodb(rsp->offset);
    310 		bp->b_bcount = min(rsp->chunk, nbyte);
    311 		bp->b_data   = buf;
    312 
    313 		/* Initiate read */
    314 		(*rsp->strat)(bp);
    315 
    316 		/* Wait for results	*/
    317 		s = splbio();
    318 		while ((bp->b_flags & B_DONE) == 0)
    319 			tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
    320 		if (bp->b_flags & B_ERROR)
    321 			error = (bp->b_error ? bp->b_error : EIO);
    322 		splx(s);
    323 
    324 		/* Dot counter */
    325 		printf(".");
    326 		if(!(++dotc % 40))
    327 			printf("\n");
    328 
    329 		done = bp->b_bcount - bp->b_resid;
    330 		nbyte        -= done;
    331 		nread        += done;
    332 		rsp->offset  += done;
    333 
    334 		if(error || !done)
    335 			break;
    336 
    337 		if((rsp->offset == rsp->media_sz) && (nbyte != 0)) {
    338 		if(rsp->offset == rsp->media_sz) {
    339 			printf("\nInsert next media and hit any key...");
    340 			if(cngetc() != '\n')
    341 				printf("\n");
    342 			rsp->offset = 0;
    343 		}
    344 	}
    345 	s = splbio();
    346 	splx(s);
    347 	return(nread);
    348 }
    349 #endif /* support_compression */
    350