Home | History | Annotate | Line # | Download | only in altboot
dsk.c revision 1.2
      1 /* $NetBSD: dsk.c,v 1.2 2011/01/23 01:32:08 nisimura Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Tohru Nishimura.
      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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * assumptions;
     34  * - up to 4 IDE/SATA drives.
     35  * - a single (master) drive in each IDE channel.
     36  * - all drives are up and spinning.
     37  */
     38 
     39 #include <sys/types.h>
     40 
     41 #include <lib/libsa/stand.h>
     42 #include <lib/libsa/ufs.h>
     43 
     44 #include <sys/disklabel.h>
     45 #include <sys/bootblock.h>
     46 
     47 #include <machine/bootinfo.h>
     48 #include <machine/stdarg.h>
     49 
     50 #include "globals.h"
     51 
     52 /*
     53  * - no vtophys() translation, vaddr_t == paddr_t.
     54  */
     55 #define CSR_READ_4(r)		in32rb(r)
     56 #define CSR_WRITE_4(r,v)	out32rb(r,v)
     57 #define CSR_READ_1(r)		*(volatile uint8_t *)(r)
     58 #define CSR_WRITE_1(r,v)	*(volatile uint8_t *)(r)=(v)
     59 
     60 #define DSK_DECL(xxx) \
     61     int xxx ## _match(unsigned, void *); \
     62     void * xxx ## _init(unsigned, void *)
     63 
     64 DSK_DECL(pciide);
     65 DSK_DECL(siisata);
     66 
     67 struct dskdv {
     68 	char *name;
     69 	int (*match)(unsigned, void *);
     70 	void *(*init)(unsigned, void *);
     71 	void *priv;
     72 };
     73 
     74 static struct dskdv ldskdv[] = {
     75 	{ "pciide", pciide_match, pciide_init, },
     76 	{ "siisata", siisata_match, siisata_init, },
     77 };
     78 static int ndskdv = sizeof(ldskdv)/sizeof(ldskdv[0]);
     79 
     80 static int probe_drive(struct dkdev_ata *, int);
     81 static void drive_ident(struct disk *, char *);
     82 static char *mkident(char *, int);
     83 static void set_xfermode(struct dkdev_ata *, int);
     84 static void decode_dlabel(struct disk *, char *);
     85 static int lba_read(struct disk *, int64_t, int, void *);
     86 static void issue48(struct dvata_chan *, int64_t, int);
     87 static void issue28(struct dvata_chan *, int64_t, int);
     88 static struct disk *lookup_disk(int);
     89 
     90 static struct disk ldisk[4];
     91 
     92 int
     93 dskdv_init(unsigned tag, void **cookie)
     94 {
     95 	struct dskdv *dv;
     96 	int n;
     97 
     98 	for (n = 0; n < ndskdv; n++) {
     99 		dv = &ldskdv[n];
    100 		if ((*dv->match)(tag, NULL) > 0)
    101 			goto found;
    102 	}
    103 	return 0;
    104   found:
    105 	dv->priv = (*dv->init)(tag, NULL);
    106 	*cookie = dv;
    107 	return 1;
    108 }
    109 
    110 int
    111 disk_scan(void *cookie)
    112 {
    113 	struct dskdv *dv = cookie;
    114 	struct dkdev_ata *l = dv->priv;
    115 	struct disk *d;
    116 	int n, ndrive;
    117 
    118 	ndrive = 0;
    119 	for (n = 0; n < 4; n++) {
    120 		if (l->presense[n] == 0)
    121 			continue;
    122 		if (probe_drive(l, n) == 0) {
    123 			l->presense[n] = 0;
    124 			continue;
    125 		}
    126 		d = &ldisk[ndrive];
    127 		d->dvops = l;
    128 		d->unittag = ndrive;
    129 		snprintf(d->xname, sizeof(d->xname), "wd%d", d->unittag);
    130 		set_xfermode(l, n);
    131 		drive_ident(d, l->iobuf);
    132 		decode_dlabel(d, l->iobuf);
    133 		ndrive += 1;
    134 	}
    135 	return ndrive;
    136 }
    137 
    138 int
    139 spinwait_unbusy(struct dkdev_ata *l, int n, int milli, const char **err)
    140 {
    141 	struct dvata_chan *chan = &l->chan[n];
    142 	int sts;
    143 	const char *msg;
    144 
    145 	/*
    146 	 * For best compatibility it is recommended to wait 400ns and
    147 	 * read the alternate status byte four times before the status
    148 	 * is valid.
    149 	 */
    150 	delay(1);
    151 	(void)CSR_READ_1(chan->alt);
    152 	(void)CSR_READ_1(chan->alt);
    153 	(void)CSR_READ_1(chan->alt);
    154 	(void)CSR_READ_1(chan->alt);
    155 
    156 	sts = CSR_READ_1(chan->cmd + _STS);
    157 	while (milli-- > 0
    158 	    && sts != 0xff
    159 	    && (sts & (ATA_STS_BUSY|ATA_STS_DRDY)) != ATA_STS_DRDY) {
    160 		delay(1000);
    161 		sts = CSR_READ_1(chan->cmd + _STS);
    162 	}
    163 
    164 	msg = NULL;
    165 	if (sts == 0xff)
    166 		msg = "returned 0xff";
    167 	else if (sts & ATA_STS_ERR)
    168 		msg = "returned ERR";
    169 	else if (sts & ATA_STS_BUSY)
    170 		msg = "remains BUSY";
    171 	else if ((sts & ATA_STS_DRDY) == 0)
    172 		msg = "no DRDY";
    173 
    174 	if (err != NULL)
    175 		*err = msg;
    176 	return msg == NULL;
    177 }
    178 
    179 int
    180 perform_atareset(struct dkdev_ata *l, int n)
    181 {
    182 	struct dvata_chan *chan = &l->chan[n];
    183 
    184 	CSR_WRITE_1(chan->ctl, ATA_DREQ);
    185 	delay(10);
    186 	CSR_WRITE_1(chan->ctl, ATA_SRST|ATA_DREQ);
    187 	delay(10);
    188 	CSR_WRITE_1(chan->ctl, ATA_DREQ);
    189 
    190 	return spinwait_unbusy(l, n, 150, NULL);
    191 }
    192 
    193 int
    194 satapresense(struct dkdev_ata *l, int n)
    195 {
    196 #define VND_CH(n) (((n&02)<<8)+((n&01)<<7))
    197 #define VND_SC(n) (0x100+VND_CH(n))
    198 #define VND_SS(n) (0x104+VND_CH(n))
    199 
    200 	uint32_t sc = l->bar[5] + VND_SC(n);
    201 	uint32_t ss = l->bar[5] + VND_SS(n);
    202 	unsigned val;
    203 
    204 	val = (00 << 4) | (03 << 8);	/* any speed, no pwrmgt */
    205 	CSR_WRITE_4(sc, val | 01);	/* perform init */
    206 	delay(50 * 1000);
    207 	CSR_WRITE_4(sc, val);
    208 	delay(50 * 1000);
    209 	val = CSR_READ_4(ss);		/* has completed */
    210 	return ((val & 03) == 03);	/* active drive found */
    211 }
    212 
    213 static int
    214 probe_drive(struct dkdev_ata *l, int n)
    215 {
    216 	struct dvata_chan *chan = &l->chan[n];
    217 	uint16_t *p;
    218 	int i;
    219 
    220 	CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_IDENT);
    221 	(void)CSR_READ_1(chan->alt);
    222 	delay(10 * 1000);
    223 	if (spinwait_unbusy(l, n, 1000, NULL) == 0)
    224 		return 0;
    225 
    226 	p = (uint16_t *)l->iobuf;
    227 	for (i = 0; i < 512; i += 2) {
    228 		/* need to have bswap16 */
    229 		*p++ = iole16toh(chan->cmd + _DAT);
    230 	}
    231 	(void)CSR_READ_1(chan->cmd + _STS);
    232 	return 1;
    233 }
    234 
    235 static void
    236 drive_ident(struct disk *d, char *ident)
    237 {
    238 	uint16_t *p;
    239 	uint64_t huge;
    240 
    241 	p = (uint16_t *)ident;
    242 #if 1
    243         printf("[49]%04x [82]%04x [83]%04x [84]%04x "
    244 	       "[85]%04x [86]%04x [87]%04x [88]%04x\n",
    245 		p[49], p[82], p[83], p[84],
    246 		p[85], p[86], p[87], p[88]);
    247 #endif
    248 	huge = 0;
    249 	printf("%s: ", d->xname);
    250 	printf("<%s> ", mkident((char *)ident + 54, 40));
    251 	if (p[49] & (1 << 8))
    252 		printf("DMA ");
    253 	if (p[49] & (1 << 9)) {
    254 		printf("LBA ");
    255 		huge = p[60] | (p[61] << 16);
    256 	}
    257 	if ((p[83] & 0xc000) == 0x4000 && (p[83] & (1 << 10))) {
    258 		printf("LBA48 ");
    259 		huge = p[100] | (p[101] << 16);
    260 		huge |= (uint64_t)p[102] << 32;
    261 		huge |= (uint64_t)p[103] << 48;
    262 	}
    263 	huge >>= (1 + 10);
    264 	printf("%d MB\n", (int)huge);
    265 
    266 	memcpy(d->ident, ident, sizeof(d->ident));
    267 	d->nsect = huge;
    268 	d->lba_read = lba_read;
    269 }
    270 
    271 static char *
    272 mkident(char *src, int len)
    273 {
    274 	static char local[40];
    275 	char *dst, *end, *last;
    276 
    277 	if (len > sizeof(local))
    278 		len = sizeof(local);
    279 	dst = last = local;
    280 	end = src + len - 1;
    281 
    282 	/* reserve space for '\0' */
    283 	if (len < 2)
    284 		goto out;
    285 	/* skip leading white space */
    286 	while (*src != '\0' && src < end && *src == ' ')
    287 		++src;
    288 	/* copy string, omitting trailing white space */
    289 	while (*src != '\0' && src < end) {
    290 		*dst++ = *src;
    291 		if (*src++ != ' ')
    292 			last = dst;
    293 	}
    294  out:
    295 	*last = '\0';
    296 	return local;
    297 }
    298 
    299 static void
    300 decode_dlabel(struct disk *d, char *iobuf)
    301 {
    302         struct mbr_partition *mp, *bsdp;
    303 	struct disklabel *dlp;
    304 	struct partition *pp;
    305 	char *dp;
    306 	int i, first;
    307 
    308 	bsdp = NULL;
    309 	(*d->lba_read)(d, 0, 1, iobuf);
    310 	if (bswap16(*(uint16_t *)(iobuf + MBR_MAGIC_OFFSET)) != MBR_MAGIC)
    311 		goto skip;
    312 	mp = (struct mbr_partition *)(iobuf + MBR_PART_OFFSET);
    313 	for (i = 0; i < MBR_PART_COUNT; i++, mp++) {
    314 		if (mp->mbrp_type == MBR_PTYPE_NETBSD) {
    315 			bsdp = mp;
    316 			break;
    317 		}
    318 	}
    319   skip:
    320 	first = (bsdp) ? bswap32(bsdp->mbrp_start) : 0;
    321 	(*d->lba_read)(d, first + LABELSECTOR, 1, iobuf);
    322 	dp = iobuf /* + LABELOFFSET */;
    323 	for (i = 0; i < 512 - sizeof(struct disklabel); i++, dp += 4) {
    324 		dlp = (struct disklabel *)dp;
    325 		if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC) {
    326 			goto found;
    327 		}
    328 	}
    329 	d->dlabel = NULL;
    330 	printf("%s: no disklabel\n", d->xname);
    331 	return;
    332   found:
    333 	d->dlabel = allocaligned(sizeof(struct disklabel), 4);
    334 	memcpy(d->dlabel, dlp, sizeof(struct disklabel));
    335 #if 1
    336 	for (i = 0; i < dlp->d_npartitions; i += 1) {
    337 		const char *type;
    338 		pp = &dlp->d_partitions[i];
    339 		type = NULL;
    340 		switch (pp->p_fstype) {
    341 		case FS_SWAP: /* swap */
    342 			type = "swap";
    343 			break;
    344 		case FS_BSDFFS:
    345 			type = "ffs";
    346 			break;
    347 		case FS_EX2FS:
    348 			type = "ext2fs";
    349 			break;
    350 		}
    351 		if (type != NULL)
    352 			printf("%s%c: %s\n", d->xname, i + 'a', type);
    353 	}
    354 #endif
    355 }
    356 
    357 static void
    358 set_xfermode(struct dkdev_ata *l, int n)
    359 {
    360 	struct dvata_chan *chan = &l->chan[n];
    361 
    362 	CSR_WRITE_1(chan->cmd + _FEA, ATA_XFER);
    363 	CSR_WRITE_1(chan->cmd + _NSECT, XFER_PIO0);
    364 	CSR_WRITE_1(chan->cmd + _DEV, ATA_DEV_OBS); /* ??? */
    365 	CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_SETF);
    366 
    367 	spinwait_unbusy(l, n, 1000, NULL);
    368 }
    369 
    370 static int
    371 lba_read(struct disk *d, int64_t bno, int bcnt, void *buf)
    372 {
    373 	struct dkdev_ata *l;
    374 	struct dvata_chan *chan;
    375 	void (*issue)(struct dvata_chan *, int64_t, int);
    376 	int n, rdcnt, i, k;
    377 	uint16_t *p;
    378 	const char *err;
    379 	int error;
    380 
    381 	l = d->dvops;
    382 	n = d->unittag;
    383 	p = (uint16_t *)buf;
    384 	chan = &l->chan[n];
    385 	error = 0;
    386 	for ( ; bcnt > 0; bno += rdcnt, bcnt -= rdcnt) {
    387 		issue = (bno < (1ULL<<28)) ? issue28 : issue48;
    388 		rdcnt = (bcnt > 255) ? 255 : bcnt;
    389 		(*issue)(chan, bno, rdcnt);
    390 		for (k = 0; k < rdcnt; k++) {
    391 			if (spinwait_unbusy(l, n, 1000, &err) == 0) {
    392 				printf("%s blk %lld %s\n", d->xname, bno, err);
    393 				error = EIO;
    394 				break;
    395 			}
    396 			for (i = 0; i < 512; i += 2) {
    397 				/* arrives in native order */
    398 				*p++ = *(uint16_t *)(chan->cmd + _DAT);
    399 			}
    400 			/* clear irq if any */
    401 			(void)CSR_READ_1(chan->cmd + _STS);
    402 		}
    403 	}
    404 	return error;
    405 }
    406 
    407 static void
    408 issue48(struct dvata_chan *chan, int64_t bno, int nblk)
    409 {
    410 
    411 	CSR_WRITE_1(chan->cmd + _NSECT, 0); /* always less than 256 */
    412 	CSR_WRITE_1(chan->cmd + _LBAL, (bno >> 24) & 0xff);
    413 	CSR_WRITE_1(chan->cmd + _LBAM, (bno >> 32) & 0xff);
    414 	CSR_WRITE_1(chan->cmd + _LBAH, (bno >> 40) & 0xff);
    415 	CSR_WRITE_1(chan->cmd + _NSECT, nblk);
    416 	CSR_WRITE_1(chan->cmd + _LBAL, (bno >>  0) & 0xff);
    417 	CSR_WRITE_1(chan->cmd + _LBAM, (bno >>  8) & 0xff);
    418 	CSR_WRITE_1(chan->cmd + _LBAH, (bno >> 16) & 0xff);
    419 	CSR_WRITE_1(chan->cmd + _DEV, ATA_DEV_LBA);
    420 	CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_READ_EXT);
    421 }
    422 
    423 static void
    424 issue28(struct dvata_chan *chan, int64_t bno, int nblk)
    425 {
    426 
    427 	CSR_WRITE_1(chan->cmd + _NSECT, nblk);
    428 	CSR_WRITE_1(chan->cmd + _LBAL, (bno >>  0) & 0xff);
    429 	CSR_WRITE_1(chan->cmd + _LBAM, (bno >>  8) & 0xff);
    430 	CSR_WRITE_1(chan->cmd + _LBAH, (bno >> 16) & 0xff);
    431 	CSR_WRITE_1(chan->cmd + _DEV, ((bno >> 24) & 0xf) | ATA_DEV_LBA);
    432 	CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_READ);
    433 }
    434 
    435 static struct disk *
    436 lookup_disk(int unit)
    437 {
    438 
    439 	return &ldisk[unit];
    440 }
    441 
    442 int
    443 dsk_open(struct open_file *f, ...)
    444 {
    445 	va_list ap;
    446 	int unit, part;
    447 	const char *name;
    448 	struct disk *d;
    449 	struct disklabel *dlp;
    450 	struct fs_ops *fs;
    451 	int error;
    452 	extern struct btinfo_bootpath bi_path;
    453 	extern struct btinfo_rootdevice bi_rdev;
    454 	extern struct fs_ops fs_ffsv2, fs_ffsv1;
    455 
    456 	va_start(ap, f);
    457 	unit = va_arg(ap, int);
    458 	part = va_arg(ap, int);
    459 	name = va_arg(ap, const char *);
    460 	va_end(ap);
    461 
    462 	if ((d = lookup_disk(unit)) == NULL)
    463 		return ENXIO;
    464 	f->f_devdata = d;
    465 	if ((dlp = d->dlabel) == NULL || part >= dlp->d_npartitions)
    466 		return ENXIO;
    467 	d->part = part;
    468 
    469 	snprintf(bi_path.bootpath, sizeof(bi_path.bootpath), name);
    470 	if (dlp->d_partitions[part].p_fstype == FS_BSDFFS) {
    471 		if ((error = ffsv2_open(name, f)) == 0) {
    472 			fs = &fs_ffsv2;
    473 			goto found;
    474 		}
    475 		if (error == EINVAL && (error = ffsv1_open(name, f)) == 0) {
    476 			fs = &fs_ffsv1;
    477 			goto found;
    478 		}
    479 		return error;
    480 	}
    481 	return ENXIO;
    482   found:
    483 #if 0
    484 printf("dsk_open found %s\n", fsmod);
    485 #endif
    486 	d->fsops = fs;
    487 	f->f_devdata = d;
    488 
    489 	/* build btinfo to identify disk device */
    490 	snprintf(bi_rdev.devname, sizeof(bi_rdev.devname), "wd");
    491 	bi_rdev.cookie = d->unittag; /* disk unit number */
    492 	return 0;
    493 }
    494 
    495 int
    496 dsk_close(struct open_file *f)
    497 {
    498 	struct disk *d = f->f_devdata;
    499 	struct fs_ops *fs = d->fsops;
    500 
    501 	(*fs->close)(f);
    502 	d->fsops = NULL;
    503 	f->f_devdata = NULL;
    504 	return 0;
    505 }
    506 
    507 int
    508 dsk_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
    509 	void *p, size_t *rsize)
    510 {
    511 	struct disk *d = devdata;
    512 	struct disklabel *dlp;
    513 	int64_t bno;
    514 
    515 #if 0
    516 printf("%s %lld %d\n", d->xname, dblk, size);
    517 #endif
    518 	if (size == 0)
    519 		return 0;
    520 	if (rw != F_READ)
    521 		return EOPNOTSUPP;
    522 
    523 	bno = dblk;
    524 	if ((dlp = d->dlabel) != NULL)
    525 		bno += dlp->d_partitions[d->part].p_offset;
    526 	(*d->lba_read)(d, bno, size / 512, p);
    527 	if (rsize != NULL)
    528 		*rsize = size;
    529 	return 0;
    530 }
    531 
    532 struct fs_ops *
    533 dsk_fsops(struct open_file *f)
    534 {
    535 	struct disk *d = f->f_devdata;
    536 
    537 	return d->fsops;
    538 }
    539