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