Home | History | Annotate | Line # | Download | only in boot
wd.c revision 1.4.16.2
      1  1.4.16.2     yamt /*	$NetBSD: wd.c,v 1.4.16.2 2008/03/17 09:14:16 yamt Exp $	*/
      2       1.1      cdi 
      3       1.1      cdi /*-
      4       1.1      cdi  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5       1.1      cdi  * All rights reserved.
      6       1.1      cdi  *
      7       1.1      cdi  * This code is derived from software contributed to The NetBSD Foundation
      8       1.1      cdi  * by Manuel Bouyer.
      9       1.1      cdi  *
     10       1.1      cdi  * Redistribution and use in source and binary forms, with or without
     11       1.1      cdi  * modification, are permitted provided that the following conditions
     12       1.1      cdi  * are met:
     13       1.1      cdi  * 1. Redistributions of source code must retain the above copyright
     14       1.1      cdi  *    notice, this list of conditions and the following disclaimer.
     15       1.1      cdi  * 2. Redistributions in binary form must reproduce the above copyright
     16       1.1      cdi  *    notice, this list of conditions and the following disclaimer in the
     17       1.1      cdi  *    documentation and/or other materials provided with the distribution.
     18       1.1      cdi  * 3. All advertising materials mentioning features or use of this software
     19       1.1      cdi  *    must display the following acknowledgement:
     20       1.1      cdi  *        This product includes software developed by the NetBSD
     21       1.1      cdi  *        Foundation, Inc. and its contributors.
     22       1.1      cdi  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23       1.1      cdi  *    contributors may be used to endorse or promote products derived
     24       1.1      cdi  *    from this software without specific prior written permission.
     25       1.1      cdi  *
     26       1.1      cdi  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27       1.1      cdi  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28       1.1      cdi  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29       1.1      cdi  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30       1.1      cdi  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31       1.1      cdi  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32       1.1      cdi  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33       1.1      cdi  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34       1.1      cdi  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35       1.1      cdi  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36       1.1      cdi  * POSSIBILITY OF SUCH DAMAGE.
     37       1.1      cdi  */
     38       1.1      cdi 
     39       1.1      cdi #include <sys/types.h>
     40       1.1      cdi #include <sys/stdint.h>
     41       1.1      cdi 
     42       1.1      cdi #include <lib/libsa/stand.h>
     43  1.4.16.2     yamt #include <lib/libkern/libkern.h>
     44       1.1      cdi 
     45       1.1      cdi #include <machine/param.h>
     46       1.1      cdi #include <machine/stdarg.h>
     47  1.4.16.1     yamt #include <dev/raidframe/raidframevar.h>		/* For RF_PROTECTED_SECTORS */
     48       1.1      cdi 
     49       1.1      cdi #include "boot.h"
     50       1.1      cdi #include "wdvar.h"
     51       1.1      cdi 
     52       1.1      cdi static int  wd_get_params(struct wd_softc *wd);
     53       1.1      cdi static int  wdgetdisklabel(struct wd_softc *wd);
     54       1.1      cdi static void wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp);
     55       1.1      cdi 
     56       1.1      cdi /*
     57       1.1      cdi  * Get drive parameters through 'device identify' command.
     58       1.1      cdi  */
     59       1.1      cdi int
     60  1.4.16.1     yamt wd_get_params(struct wd_softc *wd)
     61       1.1      cdi {
     62       1.1      cdi 	int error;
     63  1.4.16.1     yamt 	uint8_t buf[DEV_BSIZE];
     64       1.1      cdi 
     65  1.4.16.1     yamt 	if ((error = wdc_exec_identify(wd, buf)) != 0)
     66  1.4.16.1     yamt 		return error;
     67       1.1      cdi 
     68       1.1      cdi 	wd->sc_params = *(struct ataparams *)buf;
     69       1.1      cdi 
     70       1.1      cdi 	/* 48-bit LBA addressing */
     71       1.3       he 	if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0) {
     72       1.1      cdi 		DPRINTF(("Drive supports LBA48.\n"));
     73       1.1      cdi #if defined(_ENABLE_LBA48)
     74       1.1      cdi 		wd->sc_flags |= WDF_LBA48;
     75       1.1      cdi #endif
     76       1.1      cdi 	}
     77       1.1      cdi 
     78       1.1      cdi 	/* Prior to ATA-4, LBA was optional. */
     79       1.1      cdi 	if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0) {
     80       1.1      cdi 		DPRINTF(("Drive supports LBA.\n"));
     81       1.1      cdi 		wd->sc_flags |= WDF_LBA;
     82       1.1      cdi 	}
     83       1.1      cdi 
     84  1.4.16.1     yamt 	return 0;
     85       1.1      cdi }
     86       1.1      cdi 
     87       1.1      cdi /*
     88       1.1      cdi  * Initialize disk label to the default value.
     89       1.1      cdi  */
     90       1.1      cdi void
     91  1.4.16.1     yamt wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
     92       1.1      cdi {
     93  1.4.16.1     yamt 
     94       1.1      cdi 	memset(lp, 0, sizeof(struct disklabel));
     95       1.1      cdi 
     96       1.1      cdi 	lp->d_secsize = DEV_BSIZE;
     97       1.1      cdi 	lp->d_ntracks = wd->sc_params.atap_heads;
     98       1.1      cdi 	lp->d_nsectors = wd->sc_params.atap_sectors;
     99       1.1      cdi 	lp->d_ncylinders = wd->sc_params.atap_cylinders;
    100       1.1      cdi 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    101       1.1      cdi 
    102       1.1      cdi 	if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
    103       1.1      cdi 		lp->d_type = DTYPE_ST506;
    104       1.1      cdi 	else
    105       1.1      cdi 		lp->d_type = DTYPE_ESDI;
    106       1.1      cdi 
    107       1.1      cdi 	strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
    108       1.1      cdi 	strncpy(lp->d_packname, "fictitious", 16);
    109       1.1      cdi 	if (wd->sc_capacity > UINT32_MAX)
    110       1.1      cdi 		lp->d_secperunit = UINT32_MAX;
    111       1.1      cdi 	else
    112       1.1      cdi 		lp->d_secperunit = wd->sc_capacity;
    113       1.1      cdi 	lp->d_rpm = 3600;
    114       1.1      cdi 	lp->d_interleave = 1;
    115       1.1      cdi 	lp->d_flags = 0;
    116       1.1      cdi 
    117       1.1      cdi 	lp->d_partitions[RAW_PART].p_offset = 0;
    118       1.1      cdi 	lp->d_partitions[RAW_PART].p_size =
    119       1.1      cdi 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    120       1.1      cdi 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    121       1.1      cdi 	lp->d_npartitions = MAXPARTITIONS;	/* RAW_PART + 1 ??? */
    122       1.1      cdi 
    123       1.1      cdi 	lp->d_magic = DISKMAGIC;
    124       1.1      cdi 	lp->d_magic2 = DISKMAGIC;
    125       1.1      cdi 	lp->d_checksum = dkcksum(lp);
    126       1.1      cdi }
    127       1.1      cdi 
    128       1.1      cdi /*
    129       1.1      cdi  * Read disk label from the device.
    130       1.1      cdi  */
    131       1.1      cdi int
    132  1.4.16.1     yamt wdgetdisklabel(struct wd_softc *wd)
    133       1.1      cdi {
    134       1.1      cdi 	char *msg;
    135       1.1      cdi 	int sector;
    136       1.1      cdi 	size_t rsize;
    137       1.1      cdi 	struct disklabel *lp;
    138  1.4.16.1     yamt 	uint8_t buf[DEV_BSIZE];
    139       1.1      cdi 
    140       1.1      cdi 	wdgetdefaultlabel(wd, &wd->sc_label);
    141       1.1      cdi 
    142       1.1      cdi 	/*
    143       1.1      cdi 	 * Find NetBSD Partition in DOS partition table.
    144       1.1      cdi 	 */
    145       1.1      cdi 	sector = 0;
    146       1.1      cdi 	if (wdstrategy(wd, F_READ, MBR_BBSECTOR, DEV_BSIZE, buf, &rsize))
    147       1.1      cdi 		return EOFFSET;
    148       1.1      cdi 
    149  1.4.16.1     yamt 	if (*(uint16_t *)&buf[MBR_MAGIC_OFFSET] == MBR_MAGIC) {
    150       1.1      cdi 		int i;
    151       1.1      cdi 		struct mbr_partition *mp;
    152       1.1      cdi 
    153       1.1      cdi 		/*
    154       1.1      cdi 		 * Lookup NetBSD slice. If there is none, go ahead
    155       1.1      cdi 		 * and try to read the disklabel off sector #0.
    156       1.1      cdi 		 */
    157       1.2    lukem 		mp = (struct mbr_partition *)&buf[MBR_PART_OFFSET];
    158       1.2    lukem 		for (i = 0; i < MBR_PART_COUNT; i++) {
    159       1.2    lukem 			if (mp[i].mbrp_type == MBR_PTYPE_NETBSD) {
    160       1.1      cdi 				sector = mp[i].mbrp_start;
    161       1.1      cdi 				break;
    162       1.1      cdi 			}
    163       1.1      cdi 		}
    164       1.1      cdi 	}
    165       1.1      cdi 
    166       1.1      cdi 	if (wdstrategy(wd, F_READ, sector + LABELSECTOR, DEV_BSIZE,
    167  1.4.16.1     yamt 	    buf, &rsize))
    168       1.1      cdi 		return EOFFSET;
    169       1.1      cdi 
    170  1.4.16.1     yamt 	if ((msg = getdisklabel(buf + LABELOFFSET, &wd->sc_label)))
    171       1.1      cdi 		printf("wd%d: getdisklabel: %s\n", wd->sc_unit, msg);
    172       1.1      cdi 
    173       1.1      cdi 	lp = &wd->sc_label;
    174       1.1      cdi 
    175       1.1      cdi 	/* check partition */
    176       1.1      cdi 	if ((wd->sc_part >= lp->d_npartitions) ||
    177  1.4.16.1     yamt 	    (lp->d_partitions[wd->sc_part].p_fstype == FS_UNUSED)) {
    178       1.1      cdi 		DPRINTF(("illegal partition\n"));
    179  1.4.16.1     yamt 		return EPART;
    180       1.1      cdi 	}
    181       1.1      cdi 
    182       1.1      cdi 	DPRINTF(("label info: d_secsize %d, d_nsectors %d, d_ncylinders %d,"
    183  1.4.16.1     yamt 	    "d_ntracks %d, d_secpercyl %d\n",
    184  1.4.16.1     yamt 	    wd->sc_label.d_secsize,
    185  1.4.16.1     yamt 	    wd->sc_label.d_nsectors,
    186  1.4.16.1     yamt 	    wd->sc_label.d_ncylinders,
    187  1.4.16.1     yamt 	    wd->sc_label.d_ntracks,
    188  1.4.16.1     yamt 	    wd->sc_label.d_secpercyl));
    189       1.1      cdi 
    190  1.4.16.1     yamt 	return 0;
    191       1.1      cdi }
    192       1.1      cdi 
    193       1.1      cdi /*
    194       1.1      cdi  * Open device (read drive parameters and disklabel)
    195       1.1      cdi  */
    196       1.1      cdi int
    197       1.1      cdi wdopen(struct open_file *f, ...)
    198       1.1      cdi {
    199       1.1      cdi 	int error;
    200       1.1      cdi 	va_list ap;
    201       1.4  tsutsui 	u_int unit, part;
    202       1.1      cdi 	struct wd_softc *wd;
    203       1.1      cdi 
    204       1.1      cdi 	va_start(ap, f);
    205       1.3       he 	unit = va_arg(ap, u_int);
    206       1.3       he 	part = va_arg(ap, u_int);
    207       1.1      cdi 	va_end(ap);
    208       1.1      cdi 
    209       1.1      cdi 	DPRINTF(("wdopen: %d:%d\n", unit, part));
    210       1.1      cdi 
    211       1.1      cdi 	wd = alloc(sizeof(struct wd_softc));
    212       1.1      cdi 	if (wd == NULL)
    213       1.1      cdi 		return ENOMEM;
    214       1.1      cdi 
    215       1.1      cdi 	memset(wd, 0, sizeof(struct wd_softc));
    216       1.1      cdi 
    217       1.1      cdi 	if (wdc_init(wd, &unit) != 0)
    218       1.1      cdi 		return (ENXIO);
    219       1.1      cdi 
    220       1.1      cdi 	wd->sc_part = part;
    221       1.1      cdi 	wd->sc_unit = unit;
    222       1.1      cdi 
    223  1.4.16.1     yamt 	if ((error = wd_get_params(wd)) != 0)
    224  1.4.16.1     yamt 		return error;
    225       1.1      cdi 
    226  1.4.16.1     yamt 	if ((error = wdgetdisklabel(wd)) != 0)
    227       1.1      cdi 		return error;
    228       1.1      cdi 
    229       1.1      cdi 	f->f_devdata = wd;
    230  1.4.16.1     yamt 	return 0;
    231       1.1      cdi }
    232       1.1      cdi 
    233       1.1      cdi /*
    234       1.1      cdi  * Close device.
    235       1.1      cdi  */
    236       1.1      cdi int
    237       1.1      cdi wdclose(struct open_file *f)
    238       1.1      cdi {
    239  1.4.16.1     yamt 
    240       1.1      cdi 	return 0;
    241       1.1      cdi }
    242       1.1      cdi 
    243       1.1      cdi /*
    244       1.1      cdi  * Read some data.
    245       1.1      cdi  */
    246       1.1      cdi int
    247  1.4.16.2     yamt wdstrategy(void *f, int rw, daddr_t dblk, size_t size, void *p, size_t *rsize)
    248       1.1      cdi {
    249       1.1      cdi 	int i, nsect;
    250       1.1      cdi 	daddr_t blkno;
    251  1.4.16.1     yamt 	struct wd_softc *wd;
    252  1.4.16.1     yamt 	struct partition *pp;
    253  1.4.16.2     yamt 	uint8_t *buf;
    254       1.1      cdi 
    255       1.1      cdi 	if (size == 0)
    256  1.4.16.1     yamt 		return 0;
    257       1.1      cdi 
    258       1.1      cdi 	if (rw != F_READ)
    259       1.1      cdi 		return EOPNOTSUPP;
    260       1.1      cdi 
    261  1.4.16.2     yamt 	buf = p;
    262  1.4.16.1     yamt 	wd = f;
    263  1.4.16.1     yamt 	pp = &wd->sc_label.d_partitions[wd->sc_part];
    264  1.4.16.1     yamt 
    265       1.1      cdi 	nsect = howmany(size, wd->sc_label.d_secsize);
    266  1.4.16.1     yamt 	blkno = dblk + pp->p_offset;
    267  1.4.16.1     yamt 	if (pp->p_fstype == FS_RAID)
    268  1.4.16.1     yamt 		blkno += RF_PROTECTED_SECTORS;
    269       1.1      cdi 
    270       1.1      cdi 	for (i = 0; i < nsect; i++, blkno++) {
    271       1.1      cdi 		int error;
    272       1.1      cdi 
    273  1.4.16.1     yamt 		if ((error = wdc_exec_read(wd, WDCC_READ, blkno, buf)) != 0)
    274  1.4.16.1     yamt 			return error;
    275       1.1      cdi 
    276       1.1      cdi 		buf += wd->sc_label.d_secsize;
    277       1.1      cdi 	}
    278       1.1      cdi 
    279       1.1      cdi 	*rsize = size;
    280  1.4.16.1     yamt 	return 0;
    281       1.1      cdi }
    282