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