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