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