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