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