Home | History | Annotate | Line # | Download | only in common
rd.c revision 1.9
      1  1.9     rmind /*	$NetBSD: rd.c,v 1.9 2011/02/08 20:20:14 rmind Exp $	*/
      2  1.1   thorpej 
      3  1.1   thorpej /*
      4  1.9     rmind  * Copyright (c) 1988 University of Utah.
      5  1.1   thorpej  * Copyright (c) 1982, 1990, 1993
      6  1.1   thorpej  *	The Regents of the University of California.  All rights reserved.
      7  1.2       agc  *
      8  1.2       agc  * This code is derived from software contributed to Berkeley by
      9  1.2       agc  * the Systems Programming Group of the University of Utah Computer
     10  1.2       agc  * Science Department.
     11  1.2       agc  *
     12  1.2       agc  * Redistribution and use in source and binary forms, with or without
     13  1.2       agc  * modification, are permitted provided that the following conditions
     14  1.2       agc  * are met:
     15  1.2       agc  * 1. Redistributions of source code must retain the above copyright
     16  1.2       agc  *    notice, this list of conditions and the following disclaimer.
     17  1.2       agc  * 2. Redistributions in binary form must reproduce the above copyright
     18  1.2       agc  *    notice, this list of conditions and the following disclaimer in the
     19  1.2       agc  *    documentation and/or other materials provided with the distribution.
     20  1.2       agc  * 3. Neither the name of the University nor the names of its contributors
     21  1.2       agc  *    may be used to endorse or promote products derived from this software
     22  1.2       agc  *    without specific prior written permission.
     23  1.2       agc  *
     24  1.2       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.2       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.2       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.2       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.2       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.2       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.2       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.2       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.2       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.2       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.2       agc  * SUCH DAMAGE.
     35  1.2       agc  *
     36  1.2       agc  * from: Utah Hdr: rd.c 1.20 92/12/21
     37  1.2       agc  *
     38  1.2       agc  *	@(#)rd.c	8.1 (Berkeley) 7/15/93
     39  1.2       agc  */
     40  1.1   thorpej 
     41  1.1   thorpej /*
     42  1.1   thorpej  * CS80/SS80 disk driver
     43  1.1   thorpej  */
     44  1.1   thorpej #include <sys/param.h>
     45  1.1   thorpej #include <sys/disklabel.h>
     46  1.1   thorpej 
     47  1.3   tsutsui #include <machine/stdarg.h>
     48  1.3   tsutsui 
     49  1.1   thorpej #include <lib/libsa/stand.h>
     50  1.1   thorpej 
     51  1.1   thorpej #include <hp300/dev/rdreg.h>
     52  1.1   thorpej 
     53  1.3   tsutsui #include <hp300/stand/common/conf.h>
     54  1.3   tsutsui #include <hp300/stand/common/hpibvar.h>
     55  1.1   thorpej #include <hp300/stand/common/samachdep.h>
     56  1.1   thorpej 
     57  1.1   thorpej struct	rd_iocmd rd_ioc;
     58  1.1   thorpej struct	rd_rscmd rd_rsc;
     59  1.1   thorpej struct	rd_stat rd_stat;
     60  1.1   thorpej struct	rd_ssmcmd rd_ssmc;
     61  1.1   thorpej 
     62  1.1   thorpej struct	disklabel rdlabel;
     63  1.1   thorpej 
     64  1.1   thorpej struct	rdminilabel {
     65  1.1   thorpej 	u_short	npart;
     66  1.1   thorpej 	u_long	offset[MAXPARTITIONS];
     67  1.1   thorpej };
     68  1.1   thorpej 
     69  1.1   thorpej struct	rd_softc {
     70  1.1   thorpej 	int	sc_ctlr;
     71  1.1   thorpej 	int	sc_unit;
     72  1.1   thorpej 	int	sc_part;
     73  1.1   thorpej 	char	sc_retry;
     74  1.1   thorpej 	char	sc_alive;
     75  1.1   thorpej 	short	sc_type;
     76  1.1   thorpej 	struct	rdminilabel sc_pinfo;
     77  1.3   tsutsui };
     78  1.1   thorpej 
     79  1.1   thorpej #define	RDRETRY		5
     80  1.1   thorpej 
     81  1.1   thorpej struct	rdidentinfo {
     82  1.1   thorpej 	short	ri_hwid;
     83  1.1   thorpej 	short	ri_maxunum;
     84  1.1   thorpej 	int	ri_nblocks;
     85  1.3   tsutsui };
     86  1.3   tsutsui 
     87  1.3   tsutsui static int rdinit(int, int);
     88  1.3   tsutsui static int rdident(int, int);
     89  1.3   tsutsui static void rdreset(int, int);
     90  1.3   tsutsui static int rdgetinfo(struct rd_softc *);
     91  1.3   tsutsui static int rderror(int, int, int);
     92  1.3   tsutsui 
     93  1.3   tsutsui struct rd_softc rd_softc[NHPIB][NRD];
     94  1.3   tsutsui 
     95  1.3   tsutsui struct rdidentinfo rdidentinfo[] = {
     96  1.1   thorpej 	{ RD7946AID,	0,	 108416 },
     97  1.1   thorpej 	{ RD9134DID,	1,	  29088 },
     98  1.1   thorpej 	{ RD9134LID,	1,	   1232 },
     99  1.1   thorpej 	{ RD7912PID,	0,	 128128 },
    100  1.1   thorpej 	{ RD7914PID,	0,	 258048 },
    101  1.1   thorpej 	{ RD7958AID,	0,	 255276 },
    102  1.1   thorpej 	{ RD7957AID,	0,	 159544 },
    103  1.1   thorpej 	{ RD7933HID,	0,	 789958 },
    104  1.1   thorpej 	{ RD9134LID,	1,	  77840 },
    105  1.1   thorpej 	{ RD7936HID,	0,	 600978 },
    106  1.1   thorpej 	{ RD7937HID,	0,	1116102 },
    107  1.1   thorpej 	{ RD7914CTID,	0,	 258048 },
    108  1.1   thorpej 	{ RD7946AID,	0,	 108416 },
    109  1.1   thorpej 	{ RD9134LID,	1,	   1232 },
    110  1.1   thorpej 	{ RD7957BID,	0,	 159894 },
    111  1.1   thorpej 	{ RD7958BID,	0,	 297108 },
    112  1.1   thorpej 	{ RD7959BID,	0,	 594216 },
    113  1.1   thorpej 	{ RD2200AID,	0,	 654948 },
    114  1.1   thorpej 	{ RD2203AID,	0,	1309896 }
    115  1.1   thorpej };
    116  1.1   thorpej int numrdidentinfo = sizeof(rdidentinfo) / sizeof(rdidentinfo[0]);
    117  1.1   thorpej 
    118  1.3   tsutsui int
    119  1.5   tsutsui rdinit(int ctlr, int unit)
    120  1.1   thorpej {
    121  1.3   tsutsui 	struct rd_softc *rs = &rd_softc[ctlr][unit];
    122  1.1   thorpej 
    123  1.1   thorpej 	rs->sc_type = rdident(ctlr, unit);
    124  1.1   thorpej 	if (rs->sc_type < 0)
    125  1.3   tsutsui 		return 0;
    126  1.1   thorpej 	rs->sc_alive = 1;
    127  1.3   tsutsui 	return 1;
    128  1.1   thorpej }
    129  1.1   thorpej 
    130  1.3   tsutsui static void
    131  1.5   tsutsui rdreset(int ctlr, int unit)
    132  1.1   thorpej {
    133  1.7   tsutsui 	uint8_t stat;
    134  1.1   thorpej 
    135  1.1   thorpej 	rd_ssmc.c_unit = C_SUNIT(0);
    136  1.1   thorpej 	rd_ssmc.c_cmd = C_SSM;
    137  1.1   thorpej 	rd_ssmc.c_refm = REF_MASK;
    138  1.1   thorpej 	rd_ssmc.c_fefm = FEF_MASK;
    139  1.1   thorpej 	rd_ssmc.c_aefm = AEF_MASK;
    140  1.1   thorpej 	rd_ssmc.c_iefm = IEF_MASK;
    141  1.7   tsutsui 	hpibsend(ctlr, unit, C_CMD, (uint8_t *)&rd_ssmc, sizeof(rd_ssmc));
    142  1.1   thorpej 	hpibswait(ctlr, unit);
    143  1.1   thorpej 	hpibrecv(ctlr, unit, C_QSTAT, &stat, 1);
    144  1.1   thorpej }
    145  1.1   thorpej 
    146  1.3   tsutsui static int
    147  1.5   tsutsui rdident(int ctlr, int unit)
    148  1.1   thorpej {
    149  1.1   thorpej 	struct rd_describe desc;
    150  1.7   tsutsui 	uint8_t stat, cmd[3];
    151  1.1   thorpej 	char name[7];
    152  1.3   tsutsui 	int id, i;
    153  1.1   thorpej 
    154  1.1   thorpej 	id = hpibid(ctlr, unit);
    155  1.1   thorpej 	if ((id & 0x200) == 0)
    156  1.3   tsutsui 		return -1;
    157  1.1   thorpej 	for (i = 0; i < numrdidentinfo; i++)
    158  1.1   thorpej 		if (id == rdidentinfo[i].ri_hwid)
    159  1.1   thorpej 			break;
    160  1.1   thorpej 	if (i == numrdidentinfo)
    161  1.3   tsutsui 		return -1;
    162  1.1   thorpej 	id = i;
    163  1.1   thorpej 	rdreset(ctlr, unit);
    164  1.1   thorpej 	cmd[0] = C_SUNIT(0);
    165  1.1   thorpej 	cmd[1] = C_SVOL(0);
    166  1.1   thorpej 	cmd[2] = C_DESC;
    167  1.1   thorpej 	hpibsend(ctlr, unit, C_CMD, cmd, sizeof(cmd));
    168  1.7   tsutsui 	hpibrecv(ctlr, unit, C_EXEC, (uint8_t *)&desc, 37);
    169  1.1   thorpej 	hpibrecv(ctlr, unit, C_QSTAT, &stat, sizeof(stat));
    170  1.3   tsutsui 	memset(name, 0, sizeof(name));
    171  1.1   thorpej 	if (!stat) {
    172  1.3   tsutsui 		int n = desc.d_name;
    173  1.1   thorpej 		for (i = 5; i >= 0; i--) {
    174  1.1   thorpej 			name[i] = (n & 0xf) + '0';
    175  1.1   thorpej 			n >>= 4;
    176  1.1   thorpej 		}
    177  1.1   thorpej 	}
    178  1.1   thorpej 	/*
    179  1.1   thorpej 	 * Take care of a couple of anomolies:
    180  1.1   thorpej 	 * 1. 7945A and 7946A both return same HW id
    181  1.1   thorpej 	 * 2. 9122S and 9134D both return same HW id
    182  1.1   thorpej 	 * 3. 9122D and 9134L both return same HW id
    183  1.1   thorpej 	 */
    184  1.1   thorpej 	switch (rdidentinfo[id].ri_hwid) {
    185  1.1   thorpej 	case RD7946AID:
    186  1.3   tsutsui 		if (memcmp(name, "079450", 6) == 0)
    187  1.1   thorpej 			id = RD7945A;
    188  1.1   thorpej 		else
    189  1.1   thorpej 			id = RD7946A;
    190  1.1   thorpej 		break;
    191  1.1   thorpej 
    192  1.1   thorpej 	case RD9134LID:
    193  1.3   tsutsui 		if (memcmp(name, "091340", 6) == 0)
    194  1.1   thorpej 			id = RD9134L;
    195  1.1   thorpej 		else
    196  1.1   thorpej 			id = RD9122D;
    197  1.1   thorpej 		break;
    198  1.1   thorpej 
    199  1.1   thorpej 	case RD9134DID:
    200  1.3   tsutsui 		if (memcmp(name, "091220", 6) == 0)
    201  1.1   thorpej 			id = RD9122S;
    202  1.1   thorpej 		else
    203  1.1   thorpej 			id = RD9134D;
    204  1.1   thorpej 		break;
    205  1.1   thorpej 	}
    206  1.3   tsutsui 	return id;
    207  1.1   thorpej }
    208  1.1   thorpej 
    209  1.1   thorpej char io_buf[MAXBSIZE];
    210  1.1   thorpej 
    211  1.3   tsutsui static int
    212  1.5   tsutsui rdgetinfo(struct rd_softc *rs)
    213  1.1   thorpej {
    214  1.3   tsutsui 	struct rdminilabel *pi = &rs->sc_pinfo;
    215  1.3   tsutsui 	struct disklabel *lp = &rdlabel;
    216  1.3   tsutsui 	char *msg;
    217  1.3   tsutsui 	int err, savepart;
    218  1.1   thorpej 	size_t i;
    219  1.1   thorpej 
    220  1.8  christos 	memset((void *)lp, 0, sizeof *lp);
    221  1.1   thorpej 	lp->d_secsize = DEV_BSIZE;
    222  1.1   thorpej 
    223  1.1   thorpej 	/* Disklabel is always from RAW_PART. */
    224  1.1   thorpej 	savepart = rs->sc_part;
    225  1.1   thorpej 	rs->sc_part = RAW_PART;
    226  1.1   thorpej 	err = rdstrategy(rs, F_READ, LABELSECTOR,
    227  1.1   thorpej 	    lp->d_secsize ? lp->d_secsize : DEV_BSIZE, io_buf, &i);
    228  1.1   thorpej 	rs->sc_part = savepart;
    229  1.1   thorpej 
    230  1.1   thorpej 	if (err) {
    231  1.1   thorpej 		printf("rdgetinfo: rdstrategy error %d\n", err);
    232  1.3   tsutsui 		return 0;
    233  1.1   thorpej 	}
    234  1.1   thorpej 
    235  1.1   thorpej 	msg = getdisklabel(io_buf, lp);
    236  1.1   thorpej 	if (msg) {
    237  1.4   thorpej 		printf("rd(%d,%d,%d): WARNING: %s\n",
    238  1.1   thorpej 		       rs->sc_ctlr, rs->sc_unit, rs->sc_part, msg);
    239  1.1   thorpej 		pi->npart = 3;
    240  1.1   thorpej 		pi->offset[0] = pi->offset[1] = -1;
    241  1.1   thorpej 		pi->offset[2] = 0;
    242  1.1   thorpej 	} else {
    243  1.1   thorpej 		pi->npart = lp->d_npartitions;
    244  1.1   thorpej 		for (i = 0; i < pi->npart; i++)
    245  1.1   thorpej 			pi->offset[i] = lp->d_partitions[i].p_size == 0 ?
    246  1.1   thorpej 				-1 : lp->d_partitions[i].p_offset;
    247  1.1   thorpej 	}
    248  1.3   tsutsui 	return 1;
    249  1.1   thorpej }
    250  1.1   thorpej 
    251  1.3   tsutsui int
    252  1.3   tsutsui rdopen(struct open_file *f, ...)
    253  1.3   tsutsui {
    254  1.3   tsutsui 	va_list ap;
    255  1.1   thorpej 	int ctlr, unit, part;
    256  1.3   tsutsui 	struct rd_softc *rs;
    257  1.3   tsutsui 
    258  1.3   tsutsui 	va_start(ap, f);
    259  1.3   tsutsui 	ctlr = va_arg(ap, int);
    260  1.3   tsutsui 	unit = va_arg(ap, int);
    261  1.3   tsutsui 	part = va_arg(ap, int);
    262  1.3   tsutsui 	va_end(ap);
    263  1.1   thorpej 
    264  1.1   thorpej 	if (ctlr >= NHPIB || hpibalive(ctlr) == 0)
    265  1.3   tsutsui 		return EADAPT;
    266  1.1   thorpej 	if (unit >= NRD)
    267  1.3   tsutsui 		return ECTLR;
    268  1.1   thorpej 	rs = &rd_softc[ctlr][unit];
    269  1.1   thorpej 	rs->sc_part = part;
    270  1.1   thorpej 	rs->sc_unit = unit;
    271  1.1   thorpej 	rs->sc_ctlr = ctlr;
    272  1.1   thorpej 	if (rs->sc_alive == 0) {
    273  1.1   thorpej 		if (rdinit(ctlr, unit) == 0)
    274  1.3   tsutsui 			return ENXIO;
    275  1.1   thorpej 		if (rdgetinfo(rs) == 0)
    276  1.3   tsutsui 			return ERDLAB;
    277  1.1   thorpej 	}
    278  1.1   thorpej 	if (part != RAW_PART &&     /* always allow RAW_PART to be opened */
    279  1.1   thorpej 	    (part >= rs->sc_pinfo.npart || rs->sc_pinfo.offset[part] == -1))
    280  1.3   tsutsui 		return EPART;
    281  1.1   thorpej 	f->f_devdata = (void *)rs;
    282  1.3   tsutsui 	return 0;
    283  1.1   thorpej }
    284  1.1   thorpej 
    285  1.3   tsutsui int
    286  1.5   tsutsui rdclose(struct open_file *f)
    287  1.1   thorpej {
    288  1.1   thorpej 	struct rd_softc *rs = f->f_devdata;
    289  1.1   thorpej 
    290  1.1   thorpej 	/*
    291  1.1   thorpej 	 * Mark the disk `not alive' so that the disklabel
    292  1.1   thorpej 	 * will be re-loaded at next open.
    293  1.1   thorpej 	 */
    294  1.3   tsutsui 	memset(rs, 0, sizeof(struct rd_softc));
    295  1.1   thorpej 	f->f_devdata = NULL;
    296  1.1   thorpej 
    297  1.3   tsutsui 	return 0;
    298  1.1   thorpej }
    299  1.1   thorpej 
    300  1.3   tsutsui int
    301  1.5   tsutsui rdstrategy(void *devdata, int func, daddr_t dblk, size_t size, void *v_buf,
    302  1.5   tsutsui     size_t *rsize)
    303  1.1   thorpej {
    304  1.7   tsutsui 	uint8_t *buf = v_buf;
    305  1.1   thorpej 	struct rd_softc *rs = devdata;
    306  1.3   tsutsui 	int ctlr = rs->sc_ctlr;
    307  1.3   tsutsui 	int unit = rs->sc_unit;
    308  1.1   thorpej 	daddr_t blk;
    309  1.7   tsutsui 	uint8_t stat;
    310  1.1   thorpej 
    311  1.1   thorpej 	if (size == 0)
    312  1.3   tsutsui 		return 0;
    313  1.1   thorpej 
    314  1.1   thorpej 	/*
    315  1.1   thorpej 	 * Don't do partition translation on the `raw partition'.
    316  1.1   thorpej 	 */
    317  1.1   thorpej 	blk = (dblk + ((rs->sc_part == RAW_PART) ? 0 :
    318  1.1   thorpej 	    rs->sc_pinfo.offset[rs->sc_part]));
    319  1.1   thorpej 
    320  1.1   thorpej 	rs->sc_retry = 0;
    321  1.1   thorpej 	rd_ioc.c_unit = C_SUNIT(0);
    322  1.1   thorpej 	rd_ioc.c_volume = C_SVOL(0);
    323  1.1   thorpej 	rd_ioc.c_saddr = C_SADDR;
    324  1.1   thorpej 	rd_ioc.c_hiaddr = 0;
    325  1.1   thorpej 	rd_ioc.c_addr = RDBTOS(blk);
    326  1.1   thorpej 	rd_ioc.c_nop2 = C_NOP;
    327  1.1   thorpej 	rd_ioc.c_slen = C_SLEN;
    328  1.1   thorpej 	rd_ioc.c_len = size;
    329  1.1   thorpej 	rd_ioc.c_cmd = func == F_READ ? C_READ : C_WRITE;
    330  1.1   thorpej retry:
    331  1.7   tsutsui 	hpibsend(ctlr, unit, C_CMD, (uint8_t *)&rd_ioc.c_unit,
    332  1.7   tsutsui 	    sizeof(rd_ioc) - 2);
    333  1.1   thorpej 	hpibswait(ctlr, unit);
    334  1.1   thorpej 	hpibgo(ctlr, unit, C_EXEC, buf, size, func);
    335  1.1   thorpej 	hpibswait(ctlr, unit);
    336  1.1   thorpej 	hpibrecv(ctlr, unit, C_QSTAT, &stat, 1);
    337  1.1   thorpej 	if (stat) {
    338  1.1   thorpej 		if (rderror(ctlr, unit, rs->sc_part) == 0)
    339  1.3   tsutsui 			return EIO;
    340  1.1   thorpej 		if (++rs->sc_retry > RDRETRY)
    341  1.3   tsutsui 			return EIO;
    342  1.1   thorpej 		goto retry;
    343  1.1   thorpej 	}
    344  1.1   thorpej 	*rsize = size;
    345  1.1   thorpej 
    346  1.3   tsutsui 	return 0;
    347  1.1   thorpej }
    348  1.1   thorpej 
    349  1.3   tsutsui static int
    350  1.5   tsutsui rderror(int ctlr, int unit, int part)
    351  1.1   thorpej {
    352  1.7   tsutsui 	uint8_t stat;
    353  1.1   thorpej 
    354  1.1   thorpej 	rd_rsc.c_unit = C_SUNIT(0);
    355  1.1   thorpej 	rd_rsc.c_sram = C_SRAM;
    356  1.1   thorpej 	rd_rsc.c_ram = C_RAM;
    357  1.1   thorpej 	rd_rsc.c_cmd = C_STATUS;
    358  1.7   tsutsui 	hpibsend(ctlr, unit, C_CMD, (uint8_t *)&rd_rsc, sizeof(rd_rsc));
    359  1.7   tsutsui 	hpibrecv(ctlr, unit, C_EXEC, (uint8_t *)&rd_stat, sizeof(rd_stat));
    360  1.1   thorpej 	hpibrecv(ctlr, unit, C_QSTAT, &stat, 1);
    361  1.1   thorpej 	if (stat) {
    362  1.1   thorpej 		printf("rd(%d,%d,0,%d): request status fail %d\n",
    363  1.1   thorpej 		       ctlr, unit, part, stat);
    364  1.3   tsutsui 		return 0;
    365  1.1   thorpej 	}
    366  1.1   thorpej 	printf("rd(%d,%d,0,%d) err: vu 0x%x",
    367  1.1   thorpej 	       ctlr, unit, part, rd_stat.c_vu);
    368  1.1   thorpej 	if ((rd_stat.c_aef & AEF_UD) || (rd_stat.c_ief & (IEF_MD|IEF_RD)))
    369  1.3   tsutsui 		printf(", block %ld", rd_stat.c_blk);
    370  1.1   thorpej 	printf(", R0x%x F0x%x A0x%x I0x%x\n",
    371  1.1   thorpej 	       rd_stat.c_ref, rd_stat.c_fef, rd_stat.c_aef, rd_stat.c_ief);
    372  1.3   tsutsui 	return 1;
    373  1.1   thorpej }
    374