dsk.c revision 1.4 1 /* $NetBSD: dsk.c,v 1.4 2011/02/10 13:38: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 };
72
73 static struct dskdv ldskdv[] = {
74 { "pciide", pciide_match, pciide_init },
75 { "siisata", siisata_match, siisata_init },
76 };
77 static int ndskdv = sizeof(ldskdv)/sizeof(ldskdv[0]);
78
79 static int disk_scan(void *);
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(void *self)
94 {
95 struct pcidev *pci = self;
96 struct dskdv *dv;
97 unsigned tag;
98 int n;
99
100 tag = pci->bdf;
101 for (n = 0; n < ndskdv; n++) {
102 dv = &ldskdv[n];
103 if ((*dv->match)(tag, NULL) > 0)
104 goto found;
105 }
106 return 0;
107 found:
108 pci->drv = (*dv->init)(tag, NULL);
109 disk_scan(pci->drv);
110 return 1;
111 }
112
113 static int
114 disk_scan(void *drv)
115 {
116 struct dkdev_ata *l = drv;
117 struct disk *d;
118 int n, ndrive;
119
120 ndrive = 0;
121 for (n = 0; n < 4; n++) {
122 if (l->presense[n] == 0)
123 continue;
124 if (probe_drive(l, n) == 0) {
125 l->presense[n] = 0;
126 continue;
127 }
128 d = &ldisk[ndrive];
129 d->dvops = l;
130 d->unittag = ndrive;
131 snprintf(d->xname, sizeof(d->xname), "wd%d", d->unittag);
132 set_xfermode(l, n);
133 drive_ident(d, l->iobuf);
134 decode_dlabel(d, l->iobuf);
135 ndrive += 1;
136 }
137 return ndrive;
138 }
139
140 int
141 spinwait_unbusy(struct dkdev_ata *l, int n, int milli, const char **err)
142 {
143 struct dvata_chan *chan = &l->chan[n];
144 int sts;
145 const char *msg;
146
147 /*
148 * For best compatibility it is recommended to wait 400ns and
149 * read the alternate status byte four times before the status
150 * is valid.
151 */
152 delay(1);
153 (void)CSR_READ_1(chan->alt);
154 (void)CSR_READ_1(chan->alt);
155 (void)CSR_READ_1(chan->alt);
156 (void)CSR_READ_1(chan->alt);
157
158 sts = CSR_READ_1(chan->cmd + _STS);
159 while (milli-- > 0
160 && sts != 0xff
161 && (sts & (ATA_STS_BUSY|ATA_STS_DRDY)) != ATA_STS_DRDY) {
162 delay(1000);
163 sts = CSR_READ_1(chan->cmd + _STS);
164 }
165
166 msg = NULL;
167 if (sts == 0xff)
168 msg = "returned 0xff";
169 else if (sts & ATA_STS_ERR)
170 msg = "returned ERR";
171 else if (sts & ATA_STS_BUSY)
172 msg = "remains BUSY";
173 else if ((sts & ATA_STS_DRDY) == 0)
174 msg = "no DRDY";
175
176 if (err != NULL)
177 *err = msg;
178 return msg == NULL;
179 }
180
181 int
182 perform_atareset(struct dkdev_ata *l, int n)
183 {
184 struct dvata_chan *chan = &l->chan[n];
185
186 CSR_WRITE_1(chan->ctl, ATA_DREQ);
187 delay(10);
188 CSR_WRITE_1(chan->ctl, ATA_SRST|ATA_DREQ);
189 delay(10);
190 CSR_WRITE_1(chan->ctl, ATA_DREQ);
191
192 return spinwait_unbusy(l, n, 150, NULL);
193 }
194
195 int
196 satapresense(struct dkdev_ata *l, int n)
197 {
198 #define VND_CH(n) (((n&02)<<8)+((n&01)<<7))
199 #define VND_SC(n) (0x100+VND_CH(n))
200 #define VND_SS(n) (0x104+VND_CH(n))
201
202 uint32_t sc = l->bar[5] + VND_SC(n);
203 uint32_t ss = l->bar[5] + VND_SS(n);
204 unsigned val;
205
206 val = (00 << 4) | (03 << 8); /* any speed, no pwrmgt */
207 CSR_WRITE_4(sc, val | 01); /* perform init */
208 delay(50 * 1000);
209 CSR_WRITE_4(sc, val);
210 delay(50 * 1000);
211 val = CSR_READ_4(ss); /* has completed */
212 return ((val & 03) == 03); /* active drive found */
213 }
214
215 static int
216 probe_drive(struct dkdev_ata *l, int n)
217 {
218 struct dvata_chan *chan = &l->chan[n];
219 uint16_t *p;
220 int i;
221
222 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_IDENT);
223 (void)CSR_READ_1(chan->alt);
224 delay(10 * 1000);
225 if (spinwait_unbusy(l, n, 1000, NULL) == 0)
226 return 0;
227
228 p = (uint16_t *)l->iobuf;
229 for (i = 0; i < 512; i += 2) {
230 /* need to have bswap16 */
231 *p++ = iole16toh(chan->cmd + _DAT);
232 }
233 (void)CSR_READ_1(chan->cmd + _STS);
234 return 1;
235 }
236
237 static void
238 drive_ident(struct disk *d, char *ident)
239 {
240 uint16_t *p;
241 uint64_t huge;
242
243 p = (uint16_t *)ident;
244 DPRINTF(("[49]%04x [82]%04x [83]%04x [84]%04x "
245 "[85]%04x [86]%04x [87]%04x [88]%04x\n",
246 p[49], p[82], p[83], p[84],
247 p[85], p[86], p[87], p[88]));
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 for (i = 0; i < dlp->d_npartitions; i += 1) {
336 const char *type;
337 pp = &dlp->d_partitions[i];
338 type = NULL;
339 switch (pp->p_fstype) {
340 case FS_SWAP: /* swap */
341 type = "swap";
342 break;
343 case FS_BSDFFS:
344 type = "ffs";
345 break;
346 case FS_EX2FS:
347 type = "ext2fs";
348 break;
349 }
350 if (type != NULL)
351 printf("%s%c: %s\n", d->xname, i + 'a', type);
352 }
353 }
354
355 static void
356 set_xfermode(struct dkdev_ata *l, int n)
357 {
358 struct dvata_chan *chan = &l->chan[n];
359
360 CSR_WRITE_1(chan->cmd + _FEA, ATA_XFER);
361 CSR_WRITE_1(chan->cmd + _NSECT, XFER_PIO0);
362 CSR_WRITE_1(chan->cmd + _DEV, ATA_DEV_OBS); /* ??? */
363 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_SETF);
364
365 spinwait_unbusy(l, n, 1000, NULL);
366 }
367
368 static int
369 lba_read(struct disk *d, int64_t bno, int bcnt, void *buf)
370 {
371 struct dkdev_ata *l;
372 struct dvata_chan *chan;
373 void (*issue)(struct dvata_chan *, int64_t, int);
374 int n, rdcnt, i, k;
375 uint16_t *p;
376 const char *err;
377 int error;
378
379 l = d->dvops;
380 n = d->unittag;
381 p = (uint16_t *)buf;
382 chan = &l->chan[n];
383 error = 0;
384 for ( ; bcnt > 0; bno += rdcnt, bcnt -= rdcnt) {
385 issue = (bno < (1ULL<<28)) ? issue28 : issue48;
386 rdcnt = (bcnt > 255) ? 255 : bcnt;
387 (*issue)(chan, bno, rdcnt);
388 for (k = 0; k < rdcnt; k++) {
389 if (spinwait_unbusy(l, n, 1000, &err) == 0) {
390 printf("%s blk %lld %s\n", d->xname, bno, err);
391 error = EIO;
392 break;
393 }
394 for (i = 0; i < 512; i += 2) {
395 /* arrives in native order */
396 *p++ = *(uint16_t *)(chan->cmd + _DAT);
397 }
398 /* clear irq if any */
399 (void)CSR_READ_1(chan->cmd + _STS);
400 }
401 }
402 return error;
403 }
404
405 static void
406 issue48(struct dvata_chan *chan, int64_t bno, int nblk)
407 {
408
409 CSR_WRITE_1(chan->cmd + _NSECT, 0); /* always less than 256 */
410 CSR_WRITE_1(chan->cmd + _LBAL, (bno >> 24) & 0xff);
411 CSR_WRITE_1(chan->cmd + _LBAM, (bno >> 32) & 0xff);
412 CSR_WRITE_1(chan->cmd + _LBAH, (bno >> 40) & 0xff);
413 CSR_WRITE_1(chan->cmd + _NSECT, nblk);
414 CSR_WRITE_1(chan->cmd + _LBAL, (bno >> 0) & 0xff);
415 CSR_WRITE_1(chan->cmd + _LBAM, (bno >> 8) & 0xff);
416 CSR_WRITE_1(chan->cmd + _LBAH, (bno >> 16) & 0xff);
417 CSR_WRITE_1(chan->cmd + _DEV, ATA_DEV_LBA);
418 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_READ_EXT);
419 }
420
421 static void
422 issue28(struct dvata_chan *chan, int64_t bno, int nblk)
423 {
424
425 CSR_WRITE_1(chan->cmd + _NSECT, nblk);
426 CSR_WRITE_1(chan->cmd + _LBAL, (bno >> 0) & 0xff);
427 CSR_WRITE_1(chan->cmd + _LBAM, (bno >> 8) & 0xff);
428 CSR_WRITE_1(chan->cmd + _LBAH, (bno >> 16) & 0xff);
429 CSR_WRITE_1(chan->cmd + _DEV, ((bno >> 24) & 0xf) | ATA_DEV_LBA);
430 CSR_WRITE_1(chan->cmd + _CMD, ATA_CMD_READ);
431 }
432
433 static struct disk *
434 lookup_disk(int unit)
435 {
436
437 return &ldisk[unit];
438 }
439
440 int
441 dsk_open(struct open_file *f, ...)
442 {
443 va_list ap;
444 int unit, part;
445 const char *name;
446 struct disk *d;
447 struct disklabel *dlp;
448 struct fs_ops *fs;
449 int error;
450 extern struct btinfo_bootpath bi_path;
451 extern struct btinfo_rootdevice bi_rdev;
452 extern struct fs_ops fs_ffsv2, fs_ffsv1;
453
454 va_start(ap, f);
455 unit = va_arg(ap, int);
456 part = va_arg(ap, int);
457 name = va_arg(ap, const char *);
458 va_end(ap);
459
460 if ((d = lookup_disk(unit)) == NULL)
461 return ENXIO;
462 f->f_devdata = d;
463 if ((dlp = d->dlabel) == NULL || part >= dlp->d_npartitions)
464 return ENXIO;
465 d->part = part;
466
467 snprintf(bi_path.bootpath, sizeof(bi_path.bootpath), name);
468 if (dlp->d_partitions[part].p_fstype == FS_BSDFFS) {
469 if ((error = ffsv2_open(name, f)) == 0) {
470 fs = &fs_ffsv2;
471 goto found;
472 }
473 if (error == EINVAL && (error = ffsv1_open(name, f)) == 0) {
474 fs = &fs_ffsv1;
475 goto found;
476 }
477 return error;
478 }
479 return ENXIO;
480 found:
481 d->fsops = fs;
482 f->f_devdata = d;
483
484 /* build btinfo to identify disk device */
485 snprintf(bi_rdev.devname, sizeof(bi_rdev.devname), "wd");
486 bi_rdev.cookie = d->unittag; /* disk unit number */
487 return 0;
488 }
489
490 int
491 dsk_close(struct open_file *f)
492 {
493 struct disk *d = f->f_devdata;
494 struct fs_ops *fs = d->fsops;
495
496 (*fs->close)(f);
497 d->fsops = NULL;
498 f->f_devdata = NULL;
499 return 0;
500 }
501
502 int
503 dsk_strategy(void *devdata, int rw, daddr_t dblk, size_t size,
504 void *p, size_t *rsize)
505 {
506 struct disk *d = devdata;
507 struct disklabel *dlp;
508 int64_t bno;
509
510 if (size == 0)
511 return 0;
512 if (rw != F_READ)
513 return EOPNOTSUPP;
514
515 bno = dblk;
516 if ((dlp = d->dlabel) != NULL)
517 bno += dlp->d_partitions[d->part].p_offset;
518 (*d->lba_read)(d, bno, size / 512, p);
519 if (rsize != NULL)
520 *rsize = size;
521 return 0;
522 }
523
524 struct fs_ops *
525 dsk_fsops(struct open_file *f)
526 {
527 struct disk *d = f->f_devdata;
528
529 return d->fsops;
530 }
531