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