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