dsk.c revision 1.14 1 /* $NetBSD: dsk.c,v 1.14 2012/01/22 13:16:54 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, 1000, NULL);
190 }
191
192 /* clear idle and standby timers to spin up the drive */
193 void
194 wakeup_drive(struct dkdev_ata *l, int n)
195 {
196 struct dvata_chan *chan = &l->chan[n];
197
198 CSR_WRITE_1(chan->cmd + _NSECT, 0);
199 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_IDLE);
200 (void)CSR_READ_1(chan->alt);
201 delay(10 * 1000);
202 CSR_WRITE_1(chan->cmd + _NSECT, 0);
203 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_STANDBY);
204 (void)CSR_READ_1(chan->alt);
205 delay(10 * 1000);
206 }
207
208 int
209 atachkpwr(struct dkdev_ata *l, int n)
210 {
211 struct dvata_chan *chan = &l->chan[n];
212
213 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_CHKPWR);
214 (void)CSR_READ_1(chan->alt);
215 delay(10 * 1000);
216 return CSR_READ_1(chan->cmd + _NSECT);
217 }
218
219 static int
220 probe_drive(struct dkdev_ata *l, int n)
221 {
222 struct dvata_chan *chan = &l->chan[n];
223 uint16_t *p;
224 int i;
225
226 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_IDENT);
227 (void)CSR_READ_1(chan->alt);
228 delay(10 * 1000);
229 if (spinwait_unbusy(l, n, 1000, NULL) == 0)
230 return 0;
231
232 p = (uint16_t *)l->iobuf;
233 for (i = 0; i < 512; i += 2) {
234 /* need to have bswap16 */
235 *p++ = iole16toh(chan->cmd + _DAT);
236 }
237 (void)CSR_READ_1(chan->cmd + _STS);
238 return 1;
239 }
240
241 static void
242 drive_ident(struct disk *d, char *ident)
243 {
244 uint16_t *p;
245 uint64_t huge;
246
247 p = (uint16_t *)ident;
248 DPRINTF(("[49]%04x [82]%04x [83]%04x [84]%04x "
249 "[85]%04x [86]%04x [87]%04x [88]%04x\n",
250 p[49], p[82], p[83], p[84],
251 p[85], p[86], p[87], p[88]));
252 huge = 0;
253 printf("%s: ", d->xname);
254 printf("<%s> ", mkident((char *)ident + 54, 40));
255 if (p[49] & (1 << 8))
256 printf("DMA ");
257 if (p[49] & (1 << 9)) {
258 printf("LBA ");
259 huge = p[60] | (p[61] << 16);
260 }
261 if ((p[83] & 0xc000) == 0x4000 && (p[83] & (1 << 10))) {
262 printf("LBA48 ");
263 huge = p[100] | (p[101] << 16);
264 huge |= (uint64_t)p[102] << 32;
265 huge |= (uint64_t)p[103] << 48;
266 }
267 huge >>= (1 + 10);
268 printf("%d MB\n", (int)huge);
269
270 memcpy(d->ident, ident, sizeof(d->ident));
271 d->nsect = huge;
272 d->lba_read = lba_read;
273 }
274
275 static char *
276 mkident(char *src, int len)
277 {
278 static char local[40];
279 char *dst, *end, *last;
280
281 if (len > sizeof(local))
282 len = sizeof(local);
283 dst = last = local;
284 end = src + len - 1;
285
286 /* reserve space for '\0' */
287 if (len < 2)
288 goto out;
289 /* skip leading white space */
290 while (*src != '\0' && src < end && *src == ' ')
291 ++src;
292 /* copy string, omitting trailing white space */
293 while (*src != '\0' && src < end) {
294 *dst++ = *src;
295 if (*src++ != ' ')
296 last = dst;
297 }
298 out:
299 *last = '\0';
300 return local;
301 }
302
303 static void
304 decode_dlabel(struct disk *d, char *iobuf)
305 {
306 struct mbr_partition *mp, *bsdp;
307 struct disklabel *dlp;
308 struct partition *pp;
309 char *dp;
310 int i, first, rf_offset;
311
312 bsdp = NULL;
313 (*d->lba_read)(d, 0, 1, iobuf);
314 if (bswap16(*(uint16_t *)(iobuf + MBR_MAGIC_OFFSET)) != MBR_MAGIC)
315 goto skip;
316 mp = (struct mbr_partition *)(iobuf + MBR_PART_OFFSET);
317 for (i = 0; i < MBR_PART_COUNT; i++, mp++) {
318 if (mp->mbrp_type == MBR_PTYPE_NETBSD) {
319 bsdp = mp;
320 break;
321 }
322 }
323 skip:
324 rf_offset = 0;
325 first = (bsdp) ? bswap32(bsdp->mbrp_start) : 0;
326 (*d->lba_read)(d, first + LABELSECTOR, 1, iobuf);
327 dp = iobuf /* + LABELOFFSET */;
328 for (i = 0; i < 512 - sizeof(struct disklabel); i++, dp += 4) {
329 dlp = (struct disklabel *)dp;
330 if (dlp->d_magic == DISKMAGIC && dlp->d_magic2 == DISKMAGIC) {
331 if (dlp->d_partitions[0].p_fstype == FS_RAID) {
332 printf("%s%c: raid\n", d->xname, i + 'a');
333 snprintf(d->xname, sizeof(d->xname), "raid.");
334 rf_offset = dlp->d_partitions[0].p_offset +
335 RF_PROTECTED_SECTORS;
336 (*d->lba_read)(d, rf_offset + LABELSECTOR, 1,
337 iobuf);
338 dp = iobuf /* + LABELOFFSET */;
339 for (i = 0; i < 512 - sizeof(struct disklabel); i++, dp += 4) {
340 dlp = (struct disklabel *)dp;
341 if (dlp->d_magic == DISKMAGIC &&
342 dlp->d_magic2 == DISKMAGIC)
343 goto found;
344 }
345 } else /* Not RAID */
346 goto found;
347 }
348 }
349 d->dlabel = NULL;
350 printf("%s: no disklabel\n", d->xname);
351 return;
352 found:
353 for (i = 0; i < dlp->d_npartitions; i += 1) {
354 const char *type;
355 pp = &dlp->d_partitions[i];
356 pp->p_offset += rf_offset;
357 type = NULL;
358 switch (pp->p_fstype) {
359 case FS_SWAP: /* swap */
360 type = "swap";
361 break;
362 case FS_BSDFFS:
363 type = "ffs";
364 break;
365 case FS_EX2FS:
366 type = "ext2fs";
367 break;
368 }
369 if (type != NULL)
370 printf("%s%c: %s\t(%u)\n", d->xname, i + 'a', type,
371 pp->p_offset);
372 }
373 d->dlabel = allocaligned(sizeof(struct disklabel), 4);
374 memcpy(d->dlabel, dlp, sizeof(struct disklabel));
375 }
376
377 static void
378 set_xfermode(struct dkdev_ata *l, int n)
379 {
380 struct dvata_chan *chan = &l->chan[n];
381
382 CSR_WRITE_1(chan->cmd + _FEA, ATA_XFER);
383 CSR_WRITE_1(chan->cmd + _NSECT, XFER_PIO0);
384 CSR_WRITE_1(chan->cmd + _DEV, ATA_DEV_OBS); /* ??? */
385 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_SETF);
386
387 spinwait_unbusy(l, n, 1000, NULL);
388 }
389
390 static int
391 lba_read(struct disk *d, int64_t bno, int bcnt, void *buf)
392 {
393 struct dkdev_ata *l;
394 struct dvata_chan *chan;
395 void (*issue)(struct dvata_chan *, int64_t, int);
396 int n, rdcnt, i, k;
397 uint16_t *p;
398 const char *err;
399 int error;
400
401 l = d->dvops;
402 n = d->unittag;
403 p = (uint16_t *)buf;
404 chan = &l->chan[n];
405 error = 0;
406 for ( ; bcnt > 0; bno += rdcnt, bcnt -= rdcnt) {
407 issue = (bno < (1ULL<<28)) ? issue28 : issue48;
408 rdcnt = (bcnt > 255) ? 255 : bcnt;
409 (*issue)(chan, bno, rdcnt);
410 for (k = 0; k < rdcnt; k++) {
411 if (spinwait_unbusy(l, n, 1000, &err) == 0) {
412 printf("%s blk %lld %s\n", d->xname, bno, err);
413 error = EIO;
414 break;
415 }
416 for (i = 0; i < 512; i += 2) {
417 /* arrives in native order */
418 *p++ = *(uint16_t *)(chan->cmd + _DAT);
419 }
420 /* clear irq if any */
421 (void)CSR_READ_1(chan->cmd + _STS);
422 }
423 }
424 return error;
425 }
426
427 static void
428 issue48(struct dvata_chan *chan, int64_t bno, int nblk)
429 {
430
431 CSR_WRITE_1(chan->cmd + _NSECT, 0); /* always less than 256 */
432 CSR_WRITE_1(chan->cmd + _LBAL, (bno >> 24) & 0xff);
433 CSR_WRITE_1(chan->cmd + _LBAM, (bno >> 32) & 0xff);
434 CSR_WRITE_1(chan->cmd + _LBAH, (bno >> 40) & 0xff);
435 CSR_WRITE_1(chan->cmd + _NSECT, nblk);
436 CSR_WRITE_1(chan->cmd + _LBAL, (bno >> 0) & 0xff);
437 CSR_WRITE_1(chan->cmd + _LBAM, (bno >> 8) & 0xff);
438 CSR_WRITE_1(chan->cmd + _LBAH, (bno >> 16) & 0xff);
439 CSR_WRITE_1(chan->cmd + _DEV, ATA_DEV_LBA);
440 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_READ_EXT);
441 }
442
443 static void
444 issue28(struct dvata_chan *chan, int64_t bno, int nblk)
445 {
446
447 CSR_WRITE_1(chan->cmd + _NSECT, nblk);
448 CSR_WRITE_1(chan->cmd + _LBAL, (bno >> 0) & 0xff);
449 CSR_WRITE_1(chan->cmd + _LBAM, (bno >> 8) & 0xff);
450 CSR_WRITE_1(chan->cmd + _LBAH, (bno >> 16) & 0xff);
451 CSR_WRITE_1(chan->cmd + _DEV, ((bno >> 24) & 0xf) | ATA_DEV_LBA);
452 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_READ);
453 }
454
455 static struct disk *
456 lookup_disk(int unit)
457 {
458
459 return &ldisk[unit];
460 }
461
462 int
463 dsk_open(struct open_file *f, ...)
464 {
465 va_list ap;
466 int unit, part;
467 const char *name;
468 struct disk *d;
469 struct disklabel *dlp;
470 struct fs_ops *fs;
471 int error;
472 extern struct btinfo_bootpath bi_path;
473 extern struct btinfo_rootdevice bi_rdev;
474 extern struct fs_ops fs_ffsv2, fs_ffsv1;
475
476 va_start(ap, f);
477 unit = va_arg(ap, int);
478 part = va_arg(ap, int);
479 name = va_arg(ap, const char *);
480 va_end(ap);
481
482 if ((d = lookup_disk(unit)) == NULL)
483 return ENXIO;
484 f->f_devdata = d;
485 if ((dlp = d->dlabel) == NULL || part >= dlp->d_npartitions)
486 return ENXIO;
487 d->part = part;
488
489 snprintf(bi_path.bootpath, sizeof(bi_path.bootpath), name);
490 if (dlp->d_partitions[part].p_fstype == FS_BSDFFS) {
491 if ((error = ffsv2_open(name, f)) == 0) {
492 fs = &fs_ffsv2;
493 goto found;
494 }
495 if (error == EINVAL && (error = ffsv1_open(name, f)) == 0) {
496 fs = &fs_ffsv1;
497 goto found;
498 }
499 return error;
500 }
501 return ENXIO;
502 found:
503 d->fsops = fs;
504 f->f_devdata = d;
505
506 /* build btinfo to identify disk device */
507 snprintf(bi_rdev.devname, sizeof(bi_rdev.devname), "wd");
508 bi_rdev.cookie = (d->unittag << 8) | d->part;
509 return 0;
510 }
511
512 int
513 dsk_close(struct open_file *f)
514 {
515 struct disk *d = f->f_devdata;
516 struct fs_ops *fs = d->fsops;
517
518 (*fs->close)(f);
519 d->fsops = NULL;
520 f->f_devdata = NULL;
521 return 0;
522 }
523
524 int
525 dsk_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
526 void *p, size_t *rsize)
527 {
528 struct disk *d = devdata;
529 struct disklabel *dlp;
530 int64_t bno;
531
532 if (size == 0)
533 return 0;
534 if (rw != F_READ)
535 return EOPNOTSUPP;
536
537 bno = dblk;
538 if ((dlp = d->dlabel) != NULL)
539 bno += dlp->d_partitions[d->part].p_offset;
540 (*d->lba_read)(d, bno, size / 512, p);
541 if (rsize != NULL)
542 *rsize = size;
543 return 0;
544 }
545
546 struct fs_ops *
547 dsk_fsops(struct open_file *f)
548 {
549 struct disk *d = f->f_devdata;
550
551 return d->fsops;
552 }
553