Home | History | Annotate | Line # | Download | only in dev
md_root.c revision 1.15.8.1
      1 /*	$NetBSD: md_root.c,v 1.15.8.1 2001/11/17 23:18:08 scw 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	2		/* 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 	1105920,		/*	1Mb in 2160 sectors		*/
     79 	RAMD_LOAD,		/* auto-load this device		*/
     80 	MAKEDISKDEV(2, 0, 3),	/* XXX: This is crap! (1.44Mb flop)	*/
     81     }
     82 };
     83 
     84 struct read_info {
     85     struct buf	*bp;		/* buffer for strategy function		*/
     86     long	nbytes;		/* total number of bytes to read	*/
     87     long	offset;		/* offset in input medium		*/
     88     caddr_t	bufp;		/* current output buffer		*/
     89     caddr_t	ebufp;		/* absolute maximum for bufp		*/
     90     int		chunk;		/* chunk size on input medium		*/
     91     int		media_sz;	/* size of input medium			*/
     92     void	(*strat)(struct buf *);	/* strategy function for read	*/
     93 };
     94 
     95 
     96 static int  loaddisk __P((struct  md_conf *, dev_t ld_dev, struct proc *));
     97 static int  ramd_norm_read __P((struct read_info *));
     98 
     99 #ifdef support_compression
    100 static int  cpy_uncompressed __P((caddr_t, int, struct read_info *));
    101 static int  md_compressed __P((caddr_t, int, struct read_info *));
    102 #endif
    103 
    104 /*
    105  * This is called during autoconfig.
    106  */
    107 void
    108 md_attach_hook(unit, md)
    109 int		unit;
    110 struct md_conf	*md;
    111 {
    112 	if (atari_realconfig && (unit < RAMD_NDEV) && rd_info[unit].ramd_flag) {
    113 		printf ("md%d: %sauto-load on open. Size %ld bytes.\n", unit,
    114 		    rd_info[unit].ramd_flag & RAMD_LCOMP ? "decompress/" : "",
    115 		    rd_info[unit].ramd_size);
    116 		md->md_type = MD_UNCONFIGURED; /* Paranoia... */
    117 	}
    118 }
    119 
    120 void
    121 md_open_hook(unit, md)
    122 int		unit;
    123 struct md_conf	*md;
    124 {
    125 	struct ramd_info *ri;
    126 
    127 	if(unit >= RAMD_NDEV)
    128 		return;
    129 
    130 	ri = &rd_info[unit];
    131 	if (md->md_type != MD_UNCONFIGURED)
    132 		return;	/* Only configure once */
    133 	md->md_addr = malloc(ri->ramd_size, M_DEVBUF, M_WAITOK);
    134 	md->md_size = ri->ramd_size;
    135 	if(md->md_addr == NULL)
    136 		return;
    137 	if(ri->ramd_flag & RAMD_LOAD) {
    138 		if (loaddisk(md, ri->ramd_dev, curproc->l_proc)) {
    139 			free(md->md_addr, M_DEVBUF);
    140 			md->md_addr = NULL;
    141 			return;
    142 		}
    143 	}
    144 	md->md_type = MD_KMEM_ALLOCATED;
    145 }
    146 
    147 static int
    148 loaddisk(md, ld_dev, proc)
    149 struct md_conf		*md;
    150 dev_t			ld_dev;
    151 struct proc		*proc;
    152 {
    153 	struct buf		buf;
    154 	int			error;
    155 	struct bdevsw		*bdp = &bdevsw[major(ld_dev)];
    156 	struct disklabel	dl;
    157 	struct read_info	rs;
    158 
    159 	/*
    160 	 * Initialize our buffer header:
    161 	 */
    162 	memset(&buf, 0, sizeof(buf));
    163 	buf.b_vnbufs.le_next = NOLIST;
    164 	buf.b_flags = B_BUSY;
    165 	buf.b_dev   = ld_dev;
    166 	buf.b_error = 0;
    167 	buf.b_proc  = proc;
    168 
    169 	/*
    170 	 * Setup read_info:
    171 	 */
    172 	rs.bp       = &buf;
    173 	rs.nbytes   = md->md_size;
    174 	rs.offset   = 0;
    175 	rs.bufp     = md->md_addr;
    176 	rs.ebufp    = md->md_addr + md->md_size;
    177 	rs.chunk    = RAMD_CHUNK;
    178 	rs.media_sz = md->md_size;
    179 	rs.strat    = bdp->d_strategy;
    180 
    181 	/*
    182 	 * Open device and try to get some statistics.
    183 	 */
    184 	if((error = bdp->d_open(ld_dev, FREAD | FNONBLOCK, 0, proc)) != 0)
    185 		return(error);
    186 	if(bdp->d_ioctl(ld_dev, DIOCGDINFO, (caddr_t)&dl, FREAD, proc) == 0) {
    187 		/* Read on a cylinder basis */
    188 		rs.chunk    = dl.d_secsize * dl.d_secpercyl;
    189 		rs.media_sz = dl.d_secperunit * dl.d_secsize;
    190 	}
    191 
    192 #ifdef support_compression
    193 	if(ri->ramd_flag & RAMD_LCOMP)
    194 		error = decompress(cpy_uncompressed, md_compressed, &rs);
    195 	else
    196 #endif /* support_compression */
    197 		error = ramd_norm_read(&rs);
    198 
    199 	bdp->d_close(ld_dev,FREAD | FNONBLOCK, 0, proc);
    200 	return(error);
    201 }
    202 
    203 static int
    204 ramd_norm_read(rsp)
    205 struct read_info	*rsp;
    206 {
    207 	long		bytes_left;
    208 	int		done, error;
    209 	struct buf	*bp;
    210 	int		s;
    211 	int		dotc = 0;
    212 
    213 	bytes_left = rsp->nbytes;
    214 	bp         = rsp->bp;
    215 	error      = 0;
    216 
    217 	while(bytes_left > 0) {
    218 		s = splbio();
    219 		bp->b_flags = B_BUSY | B_PHYS | B_READ;
    220 		splx(s);
    221 		bp->b_blkno  = btodb(rsp->offset);
    222 		bp->b_bcount = rsp->chunk;
    223 		bp->b_data   = rsp->bufp;
    224 
    225 		/* Initiate read */
    226 		(*rsp->strat)(bp);
    227 
    228 		/* Wait for results	*/
    229 		s = splbio();
    230 		while ((bp->b_flags & B_DONE) == 0)
    231 			tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
    232 		if (bp->b_flags & B_ERROR)
    233 			error = (bp->b_error ? bp->b_error : EIO);
    234 		splx(s);
    235 
    236 		/* Dot counter */
    237 		printf(".");
    238 		if(!(++dotc % 40))
    239 			printf("\n");
    240 
    241 		done = bp->b_bcount - bp->b_resid;
    242 		bytes_left   -= done;
    243 		rsp->offset  += done;
    244 		rsp->bufp    += done;
    245 
    246 		if(error || !done)
    247 			break;
    248 
    249 		if((rsp->offset == rsp->media_sz) && (bytes_left != 0)) {
    250 			printf("\nInsert next media and hit any key...");
    251 			cngetc();
    252 			printf("\n");
    253 			rsp->offset = 0;
    254 		}
    255 	}
    256 	printf("\n");
    257 	return(error);
    258 }
    259 
    260 #ifdef support_compression
    261 /*
    262  * Functions supporting uncompression:
    263  */
    264 /*
    265  * Copy from the uncompression buffer to the ramdisk
    266  */
    267 static int
    268 cpy_uncompressed(buf, nbyte, rsp)
    269 caddr_t			buf;
    270 struct read_info	*rsp;
    271 int			nbyte;
    272 {
    273 	if((rsp->bufp + nbyte) >= rsp->ebufp)
    274 		return(0);
    275 	bcopy(buf, rsp->bufp, nbyte);
    276 	rsp->bufp += nbyte;
    277 	return(0);
    278 }
    279 
    280 /*
    281  * Read a maximum of 'nbyte' bytes into 'buf'.
    282  */
    283 static int
    284 md_compressed(buf, nbyte, rsp)
    285 caddr_t			buf;
    286 struct read_info	*rsp;
    287 int			nbyte;
    288 {
    289 	static int	dotc = 0;
    290 	struct buf	*bp;
    291 	       int	nread = 0;
    292 	       int	s;
    293 	       int	done, error;
    294 
    295 
    296 	error  = 0;
    297 	bp     = rsp->bp;
    298 	nbyte &= ~(DEV_BSIZE - 1);
    299 
    300 	while(nbyte > 0) {
    301 		s = splbio();
    302 		bp->b_flags = B_BUSY | B_PHYS | B_READ;
    303 		splx(s);
    304 		bp->b_blkno  = btodb(rsp->offset);
    305 		bp->b_bcount = min(rsp->chunk, nbyte);
    306 		bp->b_data   = buf;
    307 
    308 		/* Initiate read */
    309 		(*rsp->strat)(bp);
    310 
    311 		/* Wait for results	*/
    312 		s = splbio();
    313 		while ((bp->b_flags & B_DONE) == 0)
    314 			tsleep((caddr_t) bp, PRIBIO + 1, "ramd_norm_read", 0);
    315 		if (bp->b_flags & B_ERROR)
    316 			error = (bp->b_error ? bp->b_error : EIO);
    317 		splx(s);
    318 
    319 		/* Dot counter */
    320 		printf(".");
    321 		if(!(++dotc % 40))
    322 			printf("\n");
    323 
    324 		done = bp->b_bcount - bp->b_resid;
    325 		nbyte        -= done;
    326 		nread        += done;
    327 		rsp->offset  += done;
    328 
    329 		if(error || !done)
    330 			break;
    331 
    332 		if((rsp->offset == rsp->media_sz) && (nbyte != 0)) {
    333 		if(rsp->offset == rsp->media_sz) {
    334 			printf("\nInsert next media and hit any key...");
    335 			if(cngetc() != '\n')
    336 				printf("\n");
    337 			rsp->offset = 0;
    338 		}
    339 	}
    340 	s = splbio();
    341 	splx(s);
    342 	return(nread);
    343 }
    344 #endif /* support_compression */
    345