Home | History | Annotate | Line # | Download | only in libsa
bugdev.c revision 1.8
      1  1.8  junyoung /*	$NetBSD: bugdev.c,v 1.8 2005/06/28 21:03:02 junyoung Exp $	*/
      2  1.1     chuck 
      3  1.5        pk /*-
      4  1.5        pk  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  1.1     chuck  * All rights reserved.
      6  1.1     chuck  *
      7  1.5        pk  * This code is derived from software contributed to The NetBSD Foundation
      8  1.5        pk  * by Paul Kranenburg.
      9  1.5        pk  *
     10  1.1     chuck  * Redistribution and use in source and binary forms, with or without
     11  1.1     chuck  * modification, are permitted provided that the following conditions
     12  1.1     chuck  * are met:
     13  1.1     chuck  * 1. Redistributions of source code must retain the above copyright
     14  1.1     chuck  *    notice, this list of conditions and the following disclaimer.
     15  1.1     chuck  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1     chuck  *    notice, this list of conditions and the following disclaimer in the
     17  1.1     chuck  *    documentation and/or other materials provided with the distribution.
     18  1.1     chuck  * 3. All advertising materials mentioning features or use of this software
     19  1.1     chuck  *    must display the following acknowledgement:
     20  1.5        pk  *        This product includes software developed by the NetBSD
     21  1.5        pk  *        Foundation, Inc. and its contributors.
     22  1.5        pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.5        pk  *    contributors may be used to endorse or promote products derived
     24  1.5        pk  *    from this software without specific prior written permission.
     25  1.1     chuck  *
     26  1.5        pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.5        pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.5        pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.5        pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.5        pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.5        pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.5        pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.5        pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.5        pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.5        pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.5        pk  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1     chuck  */
     38  1.1     chuck 
     39  1.1     chuck #include <sys/param.h>
     40  1.1     chuck #include <sys/disklabel.h>
     41  1.1     chuck #include <machine/prom.h>
     42  1.1     chuck 
     43  1.8  junyoung #include <lib/libsa/stand.h>
     44  1.1     chuck #include "libsa.h"
     45  1.1     chuck 
     46  1.7  junyoung void cputobsdlabel(struct disklabel *lp, struct cpu_disklabel *clp);
     47  1.1     chuck 
     48  1.1     chuck int errno;
     49  1.1     chuck 
     50  1.1     chuck struct bugsc_softc {
     51  1.1     chuck 	int	fd;	/* Prom file descriptor */
     52  1.1     chuck 	int	poff;	/* Partition offset */
     53  1.1     chuck 	int	psize;	/* Partition size */
     54  1.1     chuck 	short	ctrl;
     55  1.1     chuck 	short	dev;
     56  1.1     chuck } bugsc_softc[1];
     57  1.1     chuck 
     58  1.1     chuck int
     59  1.7  junyoung devopen(struct open_file *f, const char *fname, char **file)
     60  1.1     chuck {
     61  1.4       scw 	struct bugsc_softc *pp = &bugsc_softc[0];
     62  1.4       scw 	int	error, pn = 0;
     63  1.1     chuck 	char	*dev, *cp;
     64  1.4       scw 	size_t	nrd;
     65  1.4       scw 	static	int iobuf[DEV_BSIZE / sizeof(int)];
     66  1.1     chuck 	struct disklabel sdlabel;
     67  1.1     chuck 
     68  1.1     chuck 	dev = bugargs.arg_start;
     69  1.1     chuck 
     70  1.1     chuck 	/*
     71  1.1     chuck 	 * Extract partition # from boot device string.
     72  1.2       scw 	 * The Bug command line format of this is:
     73  1.2       scw 	 *
     74  1.2       scw 	 *   147-Bug> bo [drive],,[<d>:][kernel_name] [options]
     75  1.2       scw 	 *
     76  1.2       scw 	 * Where:
     77  1.2       scw 	 *       [drive]         The bug LUN number, eg. 0
     78  1.2       scw 	 *       [<d>:]          <d> is partition # ('a' to 'h')
     79  1.2       scw 	 *       [kernel_name]   Eg. netbsd or /netbsd
     80  1.2       scw 	 *       [options]       Eg. -s
     81  1.2       scw 	 *
     82  1.2       scw 	 * At this time, all we need do is scan for a ':', and assume the
     83  1.2       scw 	 * preceding letter is a partition id.
     84  1.1     chuck 	 */
     85  1.2       scw 	for (cp = dev + 1; *cp && cp <= bugargs.arg_end; cp++) {
     86  1.2       scw 		if ( *cp == ':' ) {
     87  1.2       scw 			pn = *(cp - 1) - 'a';
     88  1.2       scw 			break;
     89  1.2       scw 		}
     90  1.2       scw 	}
     91  1.2       scw 
     92  1.2       scw 	if ( pn < 0 || pn >= MAXPARTITIONS ) {
     93  1.2       scw 		printf("Invalid partition number; defaulting to 'a'\n");
     94  1.2       scw 		pn = 0;
     95  1.1     chuck 	}
     96  1.1     chuck 
     97  1.1     chuck 	pp->fd = bugscopen(f);
     98  1.1     chuck 
     99  1.1     chuck 	if (pp->fd < 0) {
    100  1.1     chuck 		printf("Can't open device `%s'\n", dev);
    101  1.7  junyoung 		return ENXIO;
    102  1.1     chuck 	}
    103  1.4       scw 	error = bugscstrategy(pp, F_READ, LABELSECTOR, DEV_BSIZE, iobuf, &nrd);
    104  1.1     chuck 	if (error)
    105  1.7  junyoung 		return error;
    106  1.4       scw 	if (nrd != DEV_BSIZE)
    107  1.7  junyoung 		return EINVAL;
    108  1.1     chuck 
    109  1.4       scw 	/*LINTED*/
    110  1.4       scw 	cputobsdlabel(&sdlabel, (struct cpu_disklabel *)&(iobuf[0]));
    111  1.1     chuck 	pp->poff = sdlabel.d_partitions[pn].p_offset;
    112  1.1     chuck 	pp->psize = sdlabel.d_partitions[pn].p_size;
    113  1.1     chuck 
    114  1.1     chuck 	f->f_dev = devsw;
    115  1.1     chuck 	f->f_devdata = (void *)pp;
    116  1.4       scw 	/*LINTED*/
    117  1.1     chuck 	*file = (char *)fname;
    118  1.7  junyoung 	return 0;
    119  1.1     chuck }
    120  1.1     chuck 
    121  1.1     chuck /* silly block scale factor */
    122  1.1     chuck #define BUG_BLOCK_SIZE 256
    123  1.1     chuck #define BUG_SCALE (512/BUG_BLOCK_SIZE)
    124  1.4       scw /*ARGSUSED*/
    125  1.1     chuck int
    126  1.7  junyoung bugscstrategy(void *devdata, int func, daddr_t dblk, size_t size, void *buf,
    127  1.7  junyoung 	      size_t *rsize)
    128  1.1     chuck {
    129  1.1     chuck 	struct mvmeprom_dskio dio;
    130  1.4       scw 	struct bugsc_softc *pp = (struct bugsc_softc *)devdata;
    131  1.1     chuck 	daddr_t	blk = dblk + pp->poff;
    132  1.1     chuck 
    133  1.1     chuck 	twiddle();
    134  1.1     chuck 
    135  1.1     chuck 	dio.ctrl_lun = pp->ctrl;
    136  1.1     chuck 	dio.dev_lun = pp->dev;
    137  1.1     chuck 	dio.status = 0;
    138  1.1     chuck 	dio.pbuffer = buf;
    139  1.1     chuck 	dio.blk_num = blk * BUG_SCALE;
    140  1.1     chuck 	dio.blk_cnt = size / BUG_BLOCK_SIZE; /* assumed size in bytes */
    141  1.1     chuck 	dio.flag = 0;
    142  1.1     chuck 	dio.addr_mod = 0;
    143  1.1     chuck #ifdef DEBUG
    144  1.1     chuck 	printf("bugscstrategy: size=%d blk=%d buf=%x\n", size, blk, buf);
    145  1.1     chuck 	printf("ctrl %d dev %d\n", dio.ctrl_lun, dio.dev_lun);
    146  1.1     chuck #endif
    147  1.1     chuck 	mvmeprom_diskrd(&dio);
    148  1.1     chuck 
    149  1.1     chuck 	*rsize = dio.blk_cnt * BUG_BLOCK_SIZE;
    150  1.1     chuck #ifdef DEBUG
    151  1.1     chuck printf("rsize %d status %x\n", *rsize, dio.status);
    152  1.1     chuck #endif
    153  1.1     chuck 
    154  1.1     chuck 	if (dio.status)
    155  1.7  junyoung 		return EIO;
    156  1.7  junyoung 	return 0;
    157  1.1     chuck }
    158  1.1     chuck 
    159  1.1     chuck int
    160  1.7  junyoung bugscopen(struct open_file *f)
    161  1.1     chuck {
    162  1.1     chuck #ifdef DEBUG
    163  1.1     chuck 	printf("bugscopen:\n");
    164  1.1     chuck #endif
    165  1.1     chuck 
    166  1.1     chuck 	f->f_devdata = (void *)bugsc_softc;
    167  1.1     chuck 	bugsc_softc[0].ctrl = (short)bugargs.ctrl_lun;
    168  1.1     chuck 	bugsc_softc[0].dev =  (short)bugargs.dev_lun;
    169  1.1     chuck #ifdef DEBUG
    170  1.1     chuck 	printf("using mvmebug ctrl %d dev %d\n",
    171  1.1     chuck 	    bugsc_softc[0].ctrl, bugsc_softc[0].dev);
    172  1.1     chuck #endif
    173  1.7  junyoung 	return 0;
    174  1.1     chuck }
    175  1.1     chuck 
    176  1.4       scw /*ARGSUSED*/
    177  1.1     chuck int
    178  1.7  junyoung bugscclose(struct open_file *f)
    179  1.1     chuck {
    180  1.7  junyoung 	return EIO;
    181  1.1     chuck }
    182  1.1     chuck 
    183  1.4       scw /*ARGSUSED*/
    184  1.1     chuck int
    185  1.7  junyoung bugscioctl(struct open_file *f, u_long cmd, void *data)
    186  1.1     chuck {
    187  1.7  junyoung 	return EIO;
    188  1.1     chuck }
    189  1.1     chuck 
    190  1.1     chuck void
    191  1.7  junyoung cputobsdlabel(struct disklabel *lp, struct cpu_disklabel *clp)
    192  1.1     chuck {
    193  1.1     chuck 	int i;
    194  1.1     chuck 
    195  1.4       scw 	lp->d_magic   = (u_int32_t)clp->magic1;
    196  1.4       scw 	lp->d_type    = (u_int16_t)clp->type;
    197  1.4       scw 	lp->d_subtype = (u_int16_t)clp->subtype;
    198  1.4       scw 
    199  1.6       scw 	memcpy(lp->d_typename, clp->vid_vd, 16);
    200  1.6       scw 	memcpy(lp->d_packname, clp->packname, 16);
    201  1.4       scw 
    202  1.4       scw 	lp->d_secsize        = (u_int32_t)clp->cfg_psm;
    203  1.4       scw 	lp->d_nsectors       = (u_int32_t)clp->cfg_spt;
    204  1.4       scw 	lp->d_ncylinders     = (u_int32_t)clp->cfg_trk; /* trk is num of cyl! */
    205  1.4       scw 	lp->d_ntracks        = (u_int32_t)clp->cfg_hds;
    206  1.4       scw 	lp->d_secpercyl      = (u_int32_t)clp->secpercyl;
    207  1.4       scw 	lp->d_secperunit     = (u_int32_t)clp->secperunit;
    208  1.4       scw 	lp->d_sparespertrack = (u_int16_t)clp->sparespertrack;
    209  1.4       scw 	lp->d_sparespercyl   = (u_int16_t)clp->sparespercyl;
    210  1.4       scw 	lp->d_acylinders     = (u_int32_t)clp->acylinders;
    211  1.4       scw 	lp->d_rpm            = (u_int16_t)clp->rpm;
    212  1.4       scw 	lp->d_interleave     = (u_int16_t)clp->cfg_ilv;
    213  1.4       scw 	lp->d_trackskew      = (u_int16_t)clp->cfg_sof;
    214  1.4       scw 	lp->d_cylskew        = (u_int16_t)clp->cylskew;
    215  1.4       scw 	lp->d_headswitch     = (u_int32_t)clp->headswitch;
    216  1.1     chuck 
    217  1.1     chuck 	/* this silly table is for winchester drives */
    218  1.1     chuck 	switch (clp->cfg_ssr) {
    219  1.1     chuck 	case 0:
    220  1.1     chuck 		lp->d_trkseek = 0;
    221  1.1     chuck 		break;
    222  1.1     chuck 	case 1:
    223  1.1     chuck 		lp->d_trkseek = 6;
    224  1.1     chuck 		break;
    225  1.1     chuck 	case 2:
    226  1.1     chuck 		lp->d_trkseek = 10;
    227  1.1     chuck 		break;
    228  1.1     chuck 	case 3:
    229  1.1     chuck 		lp->d_trkseek = 15;
    230  1.1     chuck 		break;
    231  1.1     chuck 	case 4:
    232  1.1     chuck 		lp->d_trkseek = 20;
    233  1.1     chuck 		break;
    234  1.1     chuck 	default:
    235  1.1     chuck 		lp->d_trkseek = 0;
    236  1.1     chuck 		break;
    237  1.1     chuck 	}
    238  1.4       scw 	lp->d_flags = (u_int32_t)clp->flags;
    239  1.4       scw 
    240  1.1     chuck 	for (i = 0; i < NDDATA; i++)
    241  1.4       scw 		lp->d_drivedata[i] = (u_int32_t)clp->drivedata[i];
    242  1.4       scw 
    243  1.1     chuck 	for (i = 0; i < NSPARE; i++)
    244  1.4       scw 		lp->d_spare[i] = (u_int32_t)clp->spare[i];
    245  1.4       scw 
    246  1.4       scw 	lp->d_magic2      = (u_int32_t)clp->magic2;
    247  1.4       scw 	lp->d_checksum    = (u_int16_t)clp->checksum;
    248  1.4       scw 	lp->d_npartitions = (u_int16_t)clp->partitions;
    249  1.4       scw 	lp->d_bbsize      = (u_int32_t)clp->bbsize;
    250  1.4       scw 	lp->d_sbsize      = (u_int32_t)clp->sbsize;
    251  1.4       scw 
    252  1.6       scw 	memcpy(&(lp->d_partitions[0]), clp->vid_4,
    253  1.7  junyoung 	    sizeof(struct partition) * 4);
    254  1.4       scw 
    255  1.4       scw 	/* CONSTCOND */
    256  1.7  junyoung 	memcpy(&(lp->d_partitions[4]), clp->cfg_4, sizeof(struct partition)
    257  1.1     chuck 		* ((MAXPARTITIONS < 16) ? (MAXPARTITIONS - 4) : 12));
    258  1.1     chuck }
    259