mmemcard.c revision 1.3 1 1.3 lukem /* $NetBSD: mmemcard.c,v 1.3 2003/07/15 01:31:40 lukem Exp $ */
2 1.1 itohy
3 1.1 itohy /*-
4 1.1 itohy * Copyright (c) 2002 The NetBSD Foundation, Inc.
5 1.1 itohy * All rights reserved.
6 1.1 itohy *
7 1.1 itohy * This code is derived from software contributed to The NetBSD Foundation
8 1.1 itohy * by ITOH Yasufumi.
9 1.1 itohy *
10 1.1 itohy * Redistribution and use in source and binary forms, with or without
11 1.1 itohy * modification, are permitted provided that the following conditions
12 1.1 itohy * are met:
13 1.1 itohy * 1. Redistributions of source code must retain the above copyright
14 1.1 itohy * notice, this list of conditions and the following disclaimer.
15 1.1 itohy * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 itohy * notice, this list of conditions and the following disclaimer in the
17 1.1 itohy * documentation and/or other materials provided with the distribution.
18 1.1 itohy * 3. All advertising materials mentioning features or use of this software
19 1.1 itohy * must display the following acknowledgement:
20 1.1 itohy * This product includes software developed by the NetBSD
21 1.1 itohy * Foundation, Inc. and its contributors.
22 1.1 itohy * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 itohy * contributors may be used to endorse or promote products derived
24 1.1 itohy * from this software without specific prior written permission.
25 1.1 itohy *
26 1.1 itohy * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 itohy * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 itohy * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 itohy * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 itohy * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 itohy * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 itohy * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 itohy * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 itohy * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 itohy * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 itohy * POSSIBILITY OF SUCH DAMAGE.
37 1.1 itohy */
38 1.3 lukem
39 1.3 lukem #include <sys/cdefs.h>
40 1.3 lukem __KERNEL_RCSID(0, "$NetBSD: mmemcard.c,v 1.3 2003/07/15 01:31:40 lukem Exp $");
41 1.1 itohy
42 1.1 itohy #include <sys/param.h>
43 1.1 itohy #include <sys/buf.h>
44 1.1 itohy #include <sys/device.h>
45 1.1 itohy #include <sys/disklabel.h>
46 1.1 itohy #include <sys/disk.h>
47 1.1 itohy #include <sys/kernel.h>
48 1.1 itohy #include <sys/malloc.h>
49 1.1 itohy #include <sys/proc.h>
50 1.1 itohy #include <sys/stat.h>
51 1.1 itohy #include <sys/systm.h>
52 1.1 itohy #include <sys/vnode.h>
53 1.1 itohy #include <sys/conf.h>
54 1.1 itohy
55 1.1 itohy #include <dreamcast/dev/maple/maple.h>
56 1.1 itohy #include <dreamcast/dev/maple/mapleconf.h>
57 1.1 itohy
58 1.1 itohy #define MMEM_MAXACCSIZE 1012 /* (255*4) - 8 = 253*32 / 8 */
59 1.1 itohy
60 1.1 itohy struct mmem_funcdef { /* XXX assuming little-endian structure packing */
61 1.1 itohy unsigned unused : 8,
62 1.1 itohy ra : 4, /* number of access / read */
63 1.1 itohy wa : 4, /* number of access / write */
64 1.1 itohy bb : 8, /* block size / 32 - 1 */
65 1.1 itohy pt : 8; /* number of partition - 1 */
66 1.1 itohy };
67 1.1 itohy
68 1.1 itohy struct mmem_request_read_data {
69 1.1 itohy u_int32_t func_code;
70 1.1 itohy u_int8_t pt;
71 1.1 itohy u_int8_t phase;
72 1.1 itohy u_int16_t block;
73 1.1 itohy };
74 1.1 itohy
75 1.1 itohy struct mmem_response_read_data {
76 1.1 itohy u_int32_t func_code; /* function code (big endian) */
77 1.1 itohy u_int32_t blkno; /* 512byte block number (big endian) */
78 1.1 itohy u_int8_t data[MMEM_MAXACCSIZE];
79 1.1 itohy };
80 1.1 itohy
81 1.1 itohy struct mmem_request_write_data {
82 1.1 itohy u_int32_t func_code;
83 1.1 itohy u_int8_t pt;
84 1.1 itohy u_int8_t phase; /* 0, 1, 2, 3: for each 128 byte */
85 1.1 itohy u_int16_t block;
86 1.1 itohy u_int8_t data[MMEM_MAXACCSIZE];
87 1.1 itohy };
88 1.1 itohy #define MMEM_SIZE_REQW(sc) ((sc)->sc_waccsz + 8)
89 1.1 itohy
90 1.1 itohy struct mmem_request_get_media_info {
91 1.1 itohy u_int32_t func_code;
92 1.1 itohy u_int32_t pt; /* pt (1 byte) and unused 3 bytes */
93 1.1 itohy };
94 1.1 itohy
95 1.1 itohy struct mmem_media_info {
96 1.1 itohy u_int16_t maxblk, minblk;
97 1.1 itohy u_int16_t infpos;
98 1.1 itohy u_int16_t fatpos, fatsz;
99 1.1 itohy u_int16_t dirpos, dirsz;
100 1.1 itohy u_int16_t icon;
101 1.1 itohy u_int16_t datasz;
102 1.1 itohy u_int16_t rsvd[3];
103 1.1 itohy };
104 1.1 itohy
105 1.1 itohy struct mmem_response_media_info {
106 1.1 itohy u_int32_t func_code; /* function code (big endian) */
107 1.1 itohy struct mmem_media_info info;
108 1.1 itohy };
109 1.1 itohy
110 1.1 itohy struct mmem_softc {
111 1.1 itohy struct device sc_dev;
112 1.1 itohy
113 1.1 itohy struct device *sc_parent;
114 1.1 itohy struct maple_unit *sc_unit;
115 1.1 itohy struct maple_devinfo *sc_devinfo;
116 1.1 itohy
117 1.1 itohy enum mmem_stat {
118 1.1 itohy MMEM_INIT, /* during initialization */
119 1.1 itohy MMEM_INIT2, /* during initialization */
120 1.1 itohy MMEM_IDLE, /* init done, not in I/O */
121 1.1 itohy MMEM_READ, /* in read operation */
122 1.1 itohy MMEM_WRITE1, /* in write operation (read and compare) */
123 1.1 itohy MMEM_WRITE2, /* in write operation (write) */
124 1.1 itohy MMEM_DETACH /* detaching */
125 1.1 itohy } sc_stat;
126 1.1 itohy
127 1.1 itohy int sc_npt; /* number of partitions */
128 1.1 itohy int sc_bsize; /* block size */
129 1.1 itohy int sc_wacc; /* number of write access per block */
130 1.1 itohy int sc_waccsz; /* size of a write access */
131 1.1 itohy int sc_racc; /* number of read access per block */
132 1.1 itohy int sc_raccsz; /* size of a read access */
133 1.1 itohy
134 1.1 itohy struct mmem_pt {
135 1.1 itohy int pt_flags;
136 1.1 itohy #define MMEM_PT_OK 1 /* partition is alive */
137 1.1 itohy struct disk pt_dk; /* disk(9) */
138 1.1 itohy struct mmem_media_info pt_info; /* geometry per part */
139 1.1 itohy
140 1.2 itohy char pt_name[16 /* see device.h */ + 4 /* ".255" */];
141 1.1 itohy } *sc_pt;
142 1.1 itohy
143 1.1 itohy /* write request buffer (only one is used at a time) */
144 1.1 itohy union {
145 1.1 itohy struct mmem_request_read_data req_read;
146 1.1 itohy struct mmem_request_write_data req_write;
147 1.1 itohy struct mmem_request_get_media_info req_minfo;
148 1.1 itohy } sc_req;
149 1.1 itohy #define sc_reqr sc_req.req_read
150 1.1 itohy #define sc_reqw sc_req.req_write
151 1.1 itohy #define sc_reqm sc_req.req_minfo
152 1.1 itohy
153 1.1 itohy /* pending buffers */
154 1.1 itohy struct bufq_state sc_q;
155 1.1 itohy
156 1.1 itohy /* current I/O access */
157 1.1 itohy struct buf *sc_bp;
158 1.1 itohy int sc_cnt;
159 1.1 itohy char *sc_iobuf;
160 1.1 itohy int sc_retry;
161 1.1 itohy #define MMEM_MAXRETRY 12
162 1.1 itohy };
163 1.1 itohy
164 1.1 itohy /*
165 1.1 itohy * minor number layout (mmemdetach() depends on this layout):
166 1.1 itohy *
167 1.1 itohy * 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
168 1.1 itohy * |---------------------| |---------------------| |---------|
169 1.1 itohy * unit part disklabel partition
170 1.1 itohy */
171 1.1 itohy #define MMEM_PART(diskunit) ((diskunit) & 0xff)
172 1.1 itohy #define MMEM_UNIT(diskunit) ((diskunit) >> 8)
173 1.1 itohy #define MMEM_DISKMINOR(unit, part, disklabel_partition) \
174 1.1 itohy DISKMINOR(((unit) << 8) | (part), (disklabel_partition))
175 1.1 itohy
176 1.1 itohy static int mmemmatch __P((struct device *, struct cfdata *, void *));
177 1.1 itohy static void mmemattach __P((struct device *, struct device *, void *));
178 1.1 itohy static void mmem_defaultlabel __P((struct mmem_softc *, struct mmem_pt *,
179 1.1 itohy struct disklabel *));
180 1.1 itohy static int mmemdetach __P((struct device *, int));
181 1.1 itohy static void mmem_intr __P((void *, struct maple_response *, int, int));
182 1.1 itohy static void mmem_printerror __P((const char *, int, int, u_int32_t));
183 1.1 itohy static void mmemstart __P((struct mmem_softc *));
184 1.1 itohy static void mmemstart_bp __P((struct mmem_softc *));
185 1.1 itohy static void mmemstart_write2 __P((struct mmem_softc *));
186 1.1 itohy static void mmemdone __P((struct mmem_softc *, struct mmem_pt *, int));
187 1.1 itohy
188 1.1 itohy dev_type_open(mmemopen);
189 1.1 itohy dev_type_close(mmemclose);
190 1.1 itohy dev_type_read(mmemread);
191 1.1 itohy dev_type_write(mmemwrite);
192 1.1 itohy dev_type_ioctl(mmemioctl);
193 1.1 itohy dev_type_strategy(mmemstrategy);
194 1.1 itohy
195 1.1 itohy const struct bdevsw mmem_bdevsw = {
196 1.1 itohy mmemopen, mmemclose, mmemstrategy, mmemioctl, nodump,
197 1.1 itohy nosize, D_DISK
198 1.1 itohy };
199 1.1 itohy
200 1.1 itohy const struct cdevsw mmem_cdevsw = {
201 1.1 itohy mmemopen, mmemclose, mmemread, mmemwrite, mmemioctl,
202 1.1 itohy nostop, notty, nopoll, nommap, nokqfilter, D_DISK
203 1.1 itohy };
204 1.1 itohy
205 1.1 itohy CFATTACH_DECL(mmem, sizeof(struct mmem_softc),
206 1.1 itohy mmemmatch, mmemattach, mmemdetach, NULL);
207 1.1 itohy
208 1.1 itohy extern struct cfdriver mmem_cd;
209 1.1 itohy
210 1.1 itohy struct dkdriver mmemdkdriver = { mmemstrategy };
211 1.1 itohy
212 1.1 itohy static int
213 1.1 itohy mmemmatch(parent, cf, aux)
214 1.1 itohy struct device *parent;
215 1.1 itohy struct cfdata *cf;
216 1.1 itohy void *aux;
217 1.1 itohy {
218 1.1 itohy struct maple_attach_args *ma = aux;
219 1.1 itohy
220 1.1 itohy return (ma->ma_function == MAPLE_FN_MEMCARD ? MAPLE_MATCH_FUNC : 0);
221 1.1 itohy }
222 1.1 itohy
223 1.1 itohy static void
224 1.1 itohy mmemattach(parent, self, aux)
225 1.1 itohy struct device *parent, *self;
226 1.1 itohy void *aux;
227 1.1 itohy {
228 1.1 itohy struct mmem_softc *sc = (void *) self;
229 1.1 itohy struct maple_attach_args *ma = aux;
230 1.1 itohy int i;
231 1.1 itohy union {
232 1.1 itohy u_int32_t v;
233 1.1 itohy struct mmem_funcdef s;
234 1.1 itohy } funcdef;
235 1.1 itohy
236 1.1 itohy sc->sc_parent = parent;
237 1.1 itohy sc->sc_unit = ma->ma_unit;
238 1.1 itohy sc->sc_devinfo = ma->ma_devinfo;
239 1.1 itohy
240 1.1 itohy funcdef.v = maple_get_function_data(ma->ma_devinfo, MAPLE_FN_MEMCARD);
241 1.1 itohy printf(": Memory card\n");
242 1.1 itohy printf("%s: %d part, %d bytes/block, ",
243 1.1 itohy sc->sc_dev.dv_xname,
244 1.1 itohy sc->sc_npt = funcdef.s.pt + 1,
245 1.1 itohy sc->sc_bsize = (funcdef.s.bb + 1) << 5);
246 1.1 itohy if ((sc->sc_wacc = funcdef.s.wa) == 0)
247 1.1 itohy printf("no write, ");
248 1.1 itohy else
249 1.1 itohy printf("%d acc/write, ", sc->sc_wacc);
250 1.1 itohy if ((sc->sc_racc = funcdef.s.ra) == 0)
251 1.1 itohy printf("no read\n");
252 1.1 itohy else
253 1.1 itohy printf("%d acc/read\n", sc->sc_racc);
254 1.1 itohy
255 1.1 itohy /*
256 1.1 itohy * start init sequence
257 1.1 itohy */
258 1.1 itohy sc->sc_stat = MMEM_INIT;
259 1.1 itohy bufq_alloc(&sc->sc_q, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
260 1.1 itohy
261 1.1 itohy /* check consistency */
262 1.1 itohy if (sc->sc_wacc != 0) {
263 1.1 itohy sc->sc_waccsz = sc->sc_bsize / sc->sc_wacc;
264 1.1 itohy if (sc->sc_bsize != sc->sc_waccsz * sc->sc_wacc) {
265 1.1 itohy printf("%s: write access isn't equally divided\n",
266 1.1 itohy sc->sc_dev.dv_xname);
267 1.1 itohy sc->sc_wacc = 0; /* no write */
268 1.1 itohy } else if (sc->sc_waccsz > MMEM_MAXACCSIZE) {
269 1.1 itohy printf("%s: write access size is too large\n",
270 1.1 itohy sc->sc_dev.dv_xname);
271 1.1 itohy sc->sc_wacc = 0; /* no write */
272 1.1 itohy }
273 1.1 itohy }
274 1.1 itohy if (sc->sc_racc != 0) {
275 1.1 itohy sc->sc_raccsz = sc->sc_bsize / sc->sc_racc;
276 1.1 itohy if (sc->sc_bsize != sc->sc_raccsz * sc->sc_racc) {
277 1.1 itohy printf("%s: read access isn't equally divided\n",
278 1.1 itohy sc->sc_dev.dv_xname);
279 1.1 itohy sc->sc_racc = 0; /* no read */
280 1.1 itohy } else if (sc->sc_raccsz > MMEM_MAXACCSIZE) {
281 1.1 itohy printf("%s: read access size is too large\n",
282 1.1 itohy sc->sc_dev.dv_xname);
283 1.1 itohy sc->sc_racc = 0; /* no read */
284 1.1 itohy }
285 1.1 itohy }
286 1.1 itohy if (sc->sc_wacc == 0 && sc->sc_racc == 0) {
287 1.1 itohy printf("%s: device doesn't support read nor write\n",
288 1.1 itohy sc->sc_dev.dv_xname);
289 1.1 itohy return;
290 1.1 itohy }
291 1.1 itohy
292 1.1 itohy /* per-part structure */
293 1.1 itohy sc->sc_pt = malloc(sizeof(struct mmem_pt) * sc->sc_npt, M_DEVBUF,
294 1.1 itohy M_WAITOK|M_ZERO);
295 1.1 itohy
296 1.1 itohy for (i = 0; i < sc->sc_npt; i++) {
297 1.1 itohy sprintf(sc->sc_pt[i].pt_name, "%s.%d", sc->sc_dev.dv_xname, i);
298 1.1 itohy }
299 1.1 itohy
300 1.1 itohy maple_set_callback(parent, sc->sc_unit, MAPLE_FN_MEMCARD,
301 1.1 itohy mmem_intr, sc);
302 1.1 itohy
303 1.1 itohy /*
304 1.1 itohy * get capacity (start from partition 0)
305 1.1 itohy */
306 1.1 itohy sc->sc_reqm.func_code = htonl(MAPLE_FUNC(MAPLE_FN_MEMCARD));
307 1.1 itohy sc->sc_reqm.pt = 0;
308 1.1 itohy maple_command(sc->sc_parent, sc->sc_unit, MAPLE_FN_MEMCARD,
309 1.1 itohy MAPLE_COMMAND_GETMINFO, sizeof sc->sc_reqm / 4, &sc->sc_reqm, 0);
310 1.1 itohy }
311 1.1 itohy
312 1.1 itohy static int
313 1.1 itohy mmemdetach(self, flags)
314 1.1 itohy struct device *self;
315 1.1 itohy int flags;
316 1.1 itohy {
317 1.1 itohy struct mmem_softc *sc = (struct mmem_softc *) self;
318 1.1 itohy struct buf *bp;
319 1.1 itohy int i;
320 1.1 itohy int minor_l, minor_h;
321 1.1 itohy
322 1.1 itohy sc->sc_stat = MMEM_DETACH; /* just in case */
323 1.1 itohy
324 1.1 itohy /*
325 1.1 itohy * kill pending I/O
326 1.1 itohy */
327 1.1 itohy if ((bp = sc->sc_bp) != NULL) {
328 1.1 itohy bp->b_error = EIO;
329 1.1 itohy bp->b_flags |= B_ERROR;
330 1.1 itohy bp->b_resid = bp->b_bcount;
331 1.1 itohy biodone(bp);
332 1.1 itohy }
333 1.1 itohy while ((bp = BUFQ_GET(&sc->sc_q)) != NULL) {
334 1.1 itohy bp->b_error = EIO;
335 1.1 itohy bp->b_flags |= B_ERROR;
336 1.1 itohy bp->b_resid = bp->b_bcount;
337 1.1 itohy biodone(bp);
338 1.1 itohy }
339 1.1 itohy bufq_free(&sc->sc_q);
340 1.1 itohy
341 1.1 itohy /*
342 1.1 itohy * revoke vnodes
343 1.1 itohy */
344 1.1 itohy #ifdef __HAVE_OLD_DISKLABEL
345 1.1 itohy #error This code assumes DISKUNIT() is contiguous in minor number.
346 1.1 itohy #endif
347 1.1 itohy minor_l = MMEM_DISKMINOR(self->dv_unit, 0, 0);
348 1.1 itohy minor_h = MMEM_DISKMINOR(self->dv_unit, sc->sc_npt - 1,
349 1.1 itohy MAXPARTITIONS - 1);
350 1.1 itohy vdevgone(bdevsw_lookup_major(&mmem_bdevsw), minor_l, minor_h, VBLK);
351 1.1 itohy vdevgone(cdevsw_lookup_major(&mmem_cdevsw), minor_l, minor_h, VCHR);
352 1.1 itohy
353 1.1 itohy /*
354 1.1 itohy * free per-partition structure
355 1.1 itohy */
356 1.1 itohy if (sc->sc_pt) {
357 1.1 itohy /*
358 1.1 itohy * detach disks
359 1.1 itohy */
360 1.1 itohy for (i = 0; i < sc->sc_npt; i++) {
361 1.1 itohy if (sc->sc_pt[i].pt_flags & MMEM_PT_OK)
362 1.1 itohy disk_detach(&sc->sc_pt[i].pt_dk);
363 1.1 itohy }
364 1.1 itohy free(sc->sc_pt, M_DEVBUF);
365 1.1 itohy }
366 1.1 itohy
367 1.1 itohy return 0;
368 1.1 itohy }
369 1.1 itohy
370 1.1 itohy /* fake disklabel */
371 1.1 itohy static void
372 1.1 itohy mmem_defaultlabel(sc, pt, d)
373 1.1 itohy struct mmem_softc *sc;
374 1.1 itohy struct mmem_pt *pt;
375 1.1 itohy struct disklabel *d;
376 1.1 itohy {
377 1.1 itohy
378 1.1 itohy bzero(d, sizeof *d);
379 1.1 itohy
380 1.1 itohy #if 0
381 1.1 itohy d->d_type = DTYPE_FLOPPY; /* XXX? */
382 1.1 itohy #endif
383 1.1 itohy strncpy(d->d_typename, sc->sc_devinfo->di_product_name,
384 1.1 itohy sizeof d->d_typename);
385 1.1 itohy strcpy(d->d_packname, "fictitious");
386 1.1 itohy d->d_secsize = sc->sc_bsize;
387 1.1 itohy d->d_ntracks = 1; /* XXX */
388 1.1 itohy d->d_nsectors = d->d_secpercyl = 8; /* XXX */
389 1.1 itohy d->d_secperunit = pt->pt_info.maxblk - pt->pt_info.minblk + 1;
390 1.1 itohy d->d_ncylinders = d->d_secperunit / d->d_secpercyl;
391 1.1 itohy d->d_rpm = 1; /* when 4 acc/write */
392 1.1 itohy
393 1.1 itohy d->d_npartitions = RAW_PART + 1;
394 1.1 itohy d->d_partitions[RAW_PART].p_size = d->d_secperunit;
395 1.1 itohy
396 1.1 itohy d->d_magic = d->d_magic2 = DISKMAGIC;
397 1.1 itohy d->d_checksum = dkcksum(d);
398 1.1 itohy }
399 1.1 itohy
400 1.1 itohy /*
401 1.1 itohy * called back from maple bus driver
402 1.1 itohy */
403 1.1 itohy static void
404 1.1 itohy mmem_intr(dev, response, sz, flags)
405 1.1 itohy void *dev;
406 1.1 itohy struct maple_response *response;
407 1.1 itohy int sz, flags;
408 1.1 itohy {
409 1.1 itohy struct mmem_softc *sc = dev;
410 1.1 itohy struct mmem_response_read_data *r = (void *) response->data;
411 1.1 itohy struct mmem_response_media_info *rm = (void *) response->data;
412 1.1 itohy struct buf *bp;
413 1.1 itohy int part;
414 1.1 itohy struct mmem_pt *pt;
415 1.1 itohy char pbuf[9];
416 1.1 itohy int off;
417 1.1 itohy
418 1.1 itohy switch (sc->sc_stat) {
419 1.1 itohy case MMEM_INIT:
420 1.1 itohy /* checking part geometry */
421 1.1 itohy part = sc->sc_reqm.pt;
422 1.1 itohy pt = &sc->sc_pt[part];
423 1.1 itohy switch ((maple_response_t) response->response_code) {
424 1.1 itohy case MAPLE_RESPONSE_DATATRF:
425 1.1 itohy pt->pt_info = rm->info;
426 1.1 itohy format_bytes(pbuf, sizeof(pbuf),
427 1.1 itohy (u_int64_t)
428 1.1 itohy ((pt->pt_info.maxblk - pt->pt_info.minblk + 1)
429 1.1 itohy * sc->sc_bsize));
430 1.1 itohy printf("%s: %s, blk %d %d, inf %d, fat %d %d, dir %d %d, icon %d, data %d\n",
431 1.1 itohy pt->pt_name,
432 1.1 itohy pbuf,
433 1.1 itohy pt->pt_info.maxblk, pt->pt_info.minblk,
434 1.1 itohy pt->pt_info.infpos,
435 1.1 itohy pt->pt_info.fatpos, pt->pt_info.fatsz,
436 1.1 itohy pt->pt_info.dirpos, pt->pt_info.dirsz,
437 1.1 itohy pt->pt_info.icon,
438 1.1 itohy pt->pt_info.datasz);
439 1.1 itohy
440 1.1 itohy pt->pt_dk.dk_driver = &mmemdkdriver;
441 1.1 itohy pt->pt_dk.dk_name = pt->pt_name;
442 1.1 itohy disk_attach(&pt->pt_dk);
443 1.1 itohy
444 1.1 itohy mmem_defaultlabel(sc, pt, pt->pt_dk.dk_label);
445 1.1 itohy
446 1.1 itohy /* this partition is active */
447 1.1 itohy pt->pt_flags = MMEM_PT_OK;
448 1.1 itohy
449 1.1 itohy break;
450 1.1 itohy default:
451 1.1 itohy printf("%s: init: unexpected response %#x, sz %d\n",
452 1.1 itohy pt->pt_name, ntohl(response->response_code), sz);
453 1.1 itohy break;
454 1.1 itohy }
455 1.1 itohy if (++part == sc->sc_npt) {
456 1.1 itohy #if 1
457 1.1 itohy /*
458 1.1 itohy * XXX Read a block and discard the contents (only to
459 1.1 itohy * turn off the access indicator on Visual Memory).
460 1.1 itohy */
461 1.1 itohy pt = &sc->sc_pt[0];
462 1.1 itohy sc->sc_reqr.func_code =
463 1.1 itohy htonl(MAPLE_FUNC(MAPLE_FN_MEMCARD));
464 1.1 itohy sc->sc_reqr.pt = 0;
465 1.1 itohy sc->sc_reqr.block = htons(pt->pt_info.minblk);
466 1.1 itohy sc->sc_reqr.phase = 0;
467 1.1 itohy maple_command(sc->sc_parent, sc->sc_unit,
468 1.1 itohy MAPLE_FN_MEMCARD, MAPLE_COMMAND_BREAD,
469 1.1 itohy sizeof sc->sc_reqr / 4, &sc->sc_reqr, 0);
470 1.1 itohy sc->sc_stat = MMEM_INIT2;
471 1.1 itohy #else
472 1.1 itohy sc->sc_stat = MMEM_IDLE; /* init done */
473 1.1 itohy #endif
474 1.1 itohy } else {
475 1.1 itohy sc->sc_reqm.pt = part;
476 1.1 itohy maple_command(sc->sc_parent, sc->sc_unit,
477 1.1 itohy MAPLE_FN_MEMCARD, MAPLE_COMMAND_GETMINFO,
478 1.1 itohy sizeof sc->sc_reqm / 4, &sc->sc_reqm, 0);
479 1.1 itohy }
480 1.1 itohy break;
481 1.1 itohy
482 1.1 itohy case MMEM_INIT2:
483 1.1 itohy /* XXX just discard */
484 1.1 itohy sc->sc_stat = MMEM_IDLE; /* init done */
485 1.1 itohy break;
486 1.1 itohy
487 1.1 itohy case MMEM_READ:
488 1.1 itohy bp = sc->sc_bp;
489 1.1 itohy
490 1.1 itohy switch ((maple_response_t) response->response_code) {
491 1.1 itohy case MAPLE_RESPONSE_DATATRF: /* read done */
492 1.1 itohy off = sc->sc_raccsz * sc->sc_reqr.phase;
493 1.1 itohy bcopy(r->data + off, sc->sc_iobuf + off, sc->sc_raccsz);
494 1.1 itohy
495 1.1 itohy if (++sc->sc_reqr.phase == sc->sc_racc) {
496 1.1 itohy /* all phase done */
497 1.1 itohy pt = &sc->sc_pt[sc->sc_reqr.pt];
498 1.1 itohy mmemdone(sc, pt, 0);
499 1.1 itohy } else {
500 1.1 itohy /* go next phase */
501 1.1 itohy maple_command(sc->sc_parent, sc->sc_unit,
502 1.1 itohy MAPLE_FN_MEMCARD, MAPLE_COMMAND_BREAD,
503 1.1 itohy sizeof sc->sc_reqr / 4, &sc->sc_reqr, 0);
504 1.1 itohy }
505 1.1 itohy break;
506 1.1 itohy case MAPLE_RESPONSE_FILEERR:
507 1.1 itohy mmem_printerror(sc->sc_pt[sc->sc_reqr.pt].pt_name,
508 1.1 itohy 1, bp->b_rawblkno,
509 1.1 itohy r->func_code /* XXX */);
510 1.1 itohy mmemstart_bp(sc); /* retry */
511 1.1 itohy break;
512 1.1 itohy default:
513 1.1 itohy printf("%s: read: unexpected response %#x %#x, sz %d\n",
514 1.1 itohy sc->sc_pt[sc->sc_reqr.pt].pt_name,
515 1.1 itohy ntohl(response->response_code),
516 1.1 itohy ntohl(r->func_code), sz);
517 1.1 itohy mmemstart_bp(sc); /* retry */
518 1.1 itohy break;
519 1.1 itohy }
520 1.1 itohy break;
521 1.1 itohy
522 1.1 itohy case MMEM_WRITE1: /* read before write / verify after write */
523 1.1 itohy bp = sc->sc_bp;
524 1.1 itohy
525 1.1 itohy switch ((maple_response_t) response->response_code) {
526 1.1 itohy case MAPLE_RESPONSE_DATATRF: /* read done */
527 1.1 itohy off = sc->sc_raccsz * sc->sc_reqr.phase;
528 1.1 itohy if (bcmp(r->data + off, sc->sc_iobuf + off,
529 1.1 itohy sc->sc_raccsz)) {
530 1.1 itohy /*
531 1.1 itohy * data differ, start writing
532 1.1 itohy */
533 1.1 itohy mmemstart_write2(sc);
534 1.1 itohy } else if (++sc->sc_reqr.phase == sc->sc_racc) {
535 1.1 itohy /*
536 1.1 itohy * all phase done and compared equal
537 1.1 itohy */
538 1.1 itohy pt = &sc->sc_pt[sc->sc_reqr.pt];
539 1.1 itohy mmemdone(sc, pt, 0);
540 1.1 itohy } else {
541 1.1 itohy /* go next phase */
542 1.1 itohy maple_command(sc->sc_parent, sc->sc_unit,
543 1.1 itohy MAPLE_FN_MEMCARD, MAPLE_COMMAND_BREAD,
544 1.1 itohy sizeof sc->sc_reqr / 4, &sc->sc_reqr, 0);
545 1.1 itohy }
546 1.1 itohy break;
547 1.1 itohy case MAPLE_RESPONSE_FILEERR:
548 1.1 itohy mmem_printerror(sc->sc_pt[sc->sc_reqr.pt].pt_name,
549 1.1 itohy 1, bp->b_rawblkno,
550 1.1 itohy r->func_code /* XXX */);
551 1.1 itohy mmemstart_write2(sc); /* start writing */
552 1.1 itohy break;
553 1.1 itohy default:
554 1.1 itohy printf("%s: verify: unexpected response %#x %#x, sz %d\n",
555 1.1 itohy sc->sc_pt[sc->sc_reqr.pt].pt_name,
556 1.1 itohy ntohl(response->response_code),
557 1.1 itohy ntohl(r->func_code), sz);
558 1.1 itohy mmemstart_write2(sc); /* start writing */
559 1.1 itohy break;
560 1.1 itohy }
561 1.1 itohy break;
562 1.1 itohy
563 1.1 itohy case MMEM_WRITE2: /* write */
564 1.1 itohy bp = sc->sc_bp;
565 1.1 itohy
566 1.1 itohy switch ((maple_response_t) response->response_code) {
567 1.1 itohy case MAPLE_RESPONSE_OK: /* write done */
568 1.1 itohy if (sc->sc_reqw.phase == sc->sc_wacc) {
569 1.1 itohy /* all phase done */
570 1.1 itohy mmemstart_bp(sc); /* start verify */
571 1.1 itohy } else if (++sc->sc_reqw.phase == sc->sc_wacc) {
572 1.1 itohy /* check error */
573 1.1 itohy maple_command(sc->sc_parent, sc->sc_unit,
574 1.1 itohy MAPLE_FN_MEMCARD, MAPLE_COMMAND_GETLASTERR,
575 1.1 itohy 2 /* no data */ , &sc->sc_reqw,
576 1.1 itohy MAPLE_FLAG_CMD_PERIODIC_TIMING);
577 1.1 itohy } else {
578 1.1 itohy /* go next phase */
579 1.1 itohy bcopy(sc->sc_iobuf
580 1.1 itohy + sc->sc_waccsz * sc->sc_reqw.phase,
581 1.1 itohy sc->sc_reqw.data, sc->sc_waccsz);
582 1.1 itohy maple_command(sc->sc_parent, sc->sc_unit,
583 1.1 itohy MAPLE_FN_MEMCARD, MAPLE_COMMAND_BWRITE,
584 1.1 itohy MMEM_SIZE_REQW(sc) / 4, &sc->sc_reqw,
585 1.1 itohy MAPLE_FLAG_CMD_PERIODIC_TIMING);
586 1.1 itohy }
587 1.1 itohy break;
588 1.1 itohy case MAPLE_RESPONSE_FILEERR:
589 1.1 itohy mmem_printerror(sc->sc_pt[sc->sc_reqw.pt].pt_name,
590 1.1 itohy 0, bp->b_rawblkno,
591 1.1 itohy r->func_code /* XXX */);
592 1.1 itohy mmemstart_write2(sc); /* retry writing */
593 1.1 itohy break;
594 1.1 itohy default:
595 1.1 itohy printf("%s: write: unexpected response %#x, %#x, sz %d\n",
596 1.1 itohy sc->sc_pt[sc->sc_reqw.pt].pt_name,
597 1.1 itohy ntohl(response->response_code),
598 1.1 itohy ntohl(r->func_code), sz);
599 1.1 itohy mmemstart_write2(sc); /* retry writing */
600 1.1 itohy break;
601 1.1 itohy }
602 1.1 itohy break;
603 1.1 itohy
604 1.1 itohy default:
605 1.1 itohy break;
606 1.1 itohy }
607 1.1 itohy }
608 1.1 itohy
609 1.1 itohy static void
610 1.1 itohy mmem_printerror(head, rd, blk, code)
611 1.1 itohy const char *head;
612 1.1 itohy int rd; /* 1: read, 0: write */
613 1.1 itohy int blk;
614 1.1 itohy u_int32_t code;
615 1.1 itohy {
616 1.1 itohy
617 1.1 itohy printf("%s: error %sing blk %d:", head, rd? "read" : "writ", blk);
618 1.1 itohy NTOHL(code);
619 1.1 itohy if (code & 1)
620 1.1 itohy printf(" PT error");
621 1.1 itohy if (code & 2)
622 1.1 itohy printf(" Phase error");
623 1.1 itohy if (code & 4)
624 1.1 itohy printf(" Block error");
625 1.1 itohy if (code & 010)
626 1.1 itohy printf(" Write error");
627 1.1 itohy if (code & 020)
628 1.1 itohy printf(" Length error");
629 1.1 itohy if (code & 040)
630 1.1 itohy printf(" CRC error");
631 1.1 itohy if (code & ~077)
632 1.1 itohy printf(" Unknown error %#x", code & ~077);
633 1.1 itohy printf("\n");
634 1.1 itohy }
635 1.1 itohy
636 1.1 itohy int
637 1.1 itohy mmemopen(dev, flags, devtype, p)
638 1.1 itohy dev_t dev;
639 1.1 itohy int flags, devtype;
640 1.1 itohy struct proc *p;
641 1.1 itohy {
642 1.1 itohy int diskunit, unit, part, labelpart;
643 1.1 itohy struct mmem_softc *sc;
644 1.1 itohy struct mmem_pt *pt;
645 1.1 itohy
646 1.1 itohy diskunit = DISKUNIT(dev);
647 1.1 itohy unit = MMEM_UNIT(diskunit);
648 1.1 itohy part = MMEM_PART(diskunit);
649 1.1 itohy labelpart = DISKPART(dev);
650 1.1 itohy if ((sc = device_lookup(&mmem_cd, unit)) == NULL
651 1.1 itohy || sc->sc_stat == MMEM_INIT
652 1.1 itohy || sc->sc_stat == MMEM_INIT2
653 1.1 itohy || part >= sc->sc_npt || (pt = &sc->sc_pt[part])->pt_flags == 0)
654 1.1 itohy return ENXIO;
655 1.1 itohy
656 1.1 itohy switch (devtype) {
657 1.1 itohy case S_IFCHR:
658 1.1 itohy pt->pt_dk.dk_copenmask |= (1 << labelpart);
659 1.1 itohy break;
660 1.1 itohy case S_IFBLK:
661 1.1 itohy pt->pt_dk.dk_bopenmask |= (1 << labelpart);
662 1.1 itohy break;
663 1.1 itohy }
664 1.1 itohy
665 1.1 itohy return 0;
666 1.1 itohy }
667 1.1 itohy
668 1.1 itohy int
669 1.1 itohy mmemclose(dev, flags, devtype, p)
670 1.1 itohy dev_t dev;
671 1.1 itohy int flags, devtype;
672 1.1 itohy struct proc *p;
673 1.1 itohy {
674 1.1 itohy int diskunit, unit, part, labelpart;
675 1.1 itohy struct mmem_softc *sc;
676 1.1 itohy struct mmem_pt *pt;
677 1.1 itohy
678 1.1 itohy diskunit = DISKUNIT(dev);
679 1.1 itohy unit = MMEM_UNIT(diskunit);
680 1.1 itohy part = MMEM_PART(diskunit);
681 1.1 itohy sc = mmem_cd.cd_devs[unit];
682 1.1 itohy pt = &sc->sc_pt[part];
683 1.1 itohy labelpart = DISKPART(dev);
684 1.1 itohy
685 1.1 itohy switch (devtype) {
686 1.1 itohy case S_IFCHR:
687 1.1 itohy pt->pt_dk.dk_copenmask &= ~(1 << labelpart);
688 1.1 itohy break;
689 1.1 itohy case S_IFBLK:
690 1.1 itohy pt->pt_dk.dk_bopenmask &= ~(1 << labelpart);
691 1.1 itohy break;
692 1.1 itohy }
693 1.1 itohy
694 1.1 itohy return 0;
695 1.1 itohy }
696 1.1 itohy
697 1.1 itohy void
698 1.1 itohy mmemstrategy(bp)
699 1.1 itohy struct buf *bp;
700 1.1 itohy {
701 1.1 itohy int diskunit, unit, part, labelpart;
702 1.1 itohy struct mmem_softc *sc;
703 1.1 itohy struct mmem_pt *pt;
704 1.1 itohy daddr_t off, nblk, cnt;
705 1.1 itohy
706 1.1 itohy diskunit = DISKUNIT(bp->b_dev);
707 1.1 itohy unit = MMEM_UNIT(diskunit);
708 1.1 itohy part = MMEM_PART(diskunit);
709 1.2 itohy if ((sc = device_lookup(&mmem_cd, unit)) == NULL
710 1.2 itohy || sc->sc_stat == MMEM_INIT
711 1.2 itohy || sc->sc_stat == MMEM_INIT2
712 1.2 itohy || part >= sc->sc_npt || (pt = &sc->sc_pt[part])->pt_flags == 0)
713 1.2 itohy goto inval;
714 1.1 itohy
715 1.1 itohy #if 0
716 1.1 itohy printf("%s: mmemstrategy: blkno %d, count %ld\n",
717 1.1 itohy pt->pt_name, bp->b_blkno, bp->b_bcount);
718 1.1 itohy #endif
719 1.1 itohy
720 1.1 itohy if (bp->b_flags & B_READ) {
721 1.1 itohy if (sc->sc_racc == 0)
722 1.1 itohy goto inval; /* no read */
723 1.1 itohy } else if (sc->sc_wacc == 0) {
724 1.1 itohy bp->b_error = EROFS; /* no write */
725 1.1 itohy goto bad;
726 1.1 itohy }
727 1.1 itohy
728 1.1 itohy if (bp->b_blkno & ~(~(daddr_t)0 >> (DEV_BSHIFT + 1 /* sign bit */))
729 1.1 itohy || (bp->b_bcount % sc->sc_bsize) != 0)
730 1.1 itohy goto inval;
731 1.1 itohy
732 1.1 itohy cnt = howmany(bp->b_bcount, sc->sc_bsize);
733 1.1 itohy if (cnt == 0)
734 1.1 itohy goto done; /* no work */
735 1.1 itohy
736 1.1 itohy off = bp->b_blkno * DEV_BSIZE / sc->sc_bsize;
737 1.1 itohy
738 1.1 itohy /* offset to disklabel partition */
739 1.1 itohy labelpart = DISKPART(bp->b_dev);
740 1.1 itohy if (labelpart == RAW_PART) {
741 1.1 itohy nblk = pt->pt_info.maxblk - pt->pt_info.minblk + 1;
742 1.1 itohy } else {
743 1.1 itohy off +=
744 1.1 itohy nblk = pt->pt_dk.dk_label->d_partitions[labelpart].p_offset;
745 1.1 itohy nblk += pt->pt_dk.dk_label->d_partitions[labelpart].p_size;
746 1.1 itohy }
747 1.1 itohy
748 1.1 itohy /* deal with the EOF condition */
749 1.1 itohy if (off + cnt > nblk) {
750 1.1 itohy if (off >= nblk) {
751 1.1 itohy if (off == nblk)
752 1.1 itohy goto done;
753 1.1 itohy goto inval;
754 1.1 itohy }
755 1.1 itohy cnt = nblk - off;
756 1.1 itohy bp->b_resid = bp->b_bcount - (cnt * sc->sc_bsize);
757 1.1 itohy }
758 1.1 itohy
759 1.1 itohy bp->b_rawblkno = off;
760 1.1 itohy
761 1.1 itohy /* queue this transfer */
762 1.1 itohy BUFQ_PUT(&sc->sc_q, bp);
763 1.1 itohy
764 1.1 itohy if (sc->sc_stat == MMEM_IDLE)
765 1.1 itohy mmemstart(sc);
766 1.1 itohy
767 1.1 itohy return;
768 1.1 itohy
769 1.1 itohy inval: bp->b_error = EINVAL;
770 1.1 itohy bad: bp->b_flags |= B_ERROR;
771 1.1 itohy done: bp->b_resid = bp->b_bcount;
772 1.1 itohy biodone(bp);
773 1.1 itohy }
774 1.1 itohy
775 1.1 itohy /*
776 1.1 itohy * start I/O operations
777 1.1 itohy */
778 1.1 itohy static void
779 1.1 itohy mmemstart(sc)
780 1.1 itohy struct mmem_softc *sc;
781 1.1 itohy {
782 1.1 itohy struct buf *bp;
783 1.1 itohy struct mmem_pt *pt;
784 1.1 itohy int s;
785 1.1 itohy
786 1.1 itohy if ((bp = BUFQ_GET(&sc->sc_q)) == NULL) {
787 1.1 itohy sc->sc_stat = MMEM_IDLE;
788 1.1 itohy maple_enable_unit_ping(sc->sc_parent, sc->sc_unit,
789 1.1 itohy MAPLE_FN_MEMCARD, 1);
790 1.1 itohy return;
791 1.1 itohy }
792 1.1 itohy
793 1.1 itohy sc->sc_bp = bp;
794 1.1 itohy sc->sc_cnt = howmany(bp->b_bcount - bp->b_resid, sc->sc_bsize);
795 1.1 itohy KASSERT(sc->sc_cnt);
796 1.1 itohy sc->sc_iobuf = bp->b_data;
797 1.1 itohy sc->sc_retry = 0;
798 1.1 itohy
799 1.1 itohy pt = &sc->sc_pt[MMEM_PART(DISKUNIT(bp->b_dev))];
800 1.1 itohy s = splbio();
801 1.1 itohy disk_busy(&pt->pt_dk);
802 1.1 itohy splx(s);
803 1.1 itohy
804 1.1 itohy /*
805 1.1 itohy * I/O access will fail if the removal detection (by maple driver)
806 1.1 itohy * occurs before finishing the I/O, so disable it.
807 1.1 itohy * We are sending commands, and the removal detection is still alive.
808 1.1 itohy */
809 1.1 itohy maple_enable_unit_ping(sc->sc_parent, sc->sc_unit, MAPLE_FN_MEMCARD, 0);
810 1.1 itohy
811 1.1 itohy mmemstart_bp(sc);
812 1.1 itohy }
813 1.1 itohy
814 1.1 itohy /*
815 1.1 itohy * start/retry a specified I/O operation
816 1.1 itohy */
817 1.1 itohy static void
818 1.1 itohy mmemstart_bp(sc)
819 1.1 itohy struct mmem_softc *sc;
820 1.1 itohy {
821 1.1 itohy struct buf *bp;
822 1.1 itohy int diskunit, part;
823 1.1 itohy struct mmem_pt *pt;
824 1.1 itohy
825 1.1 itohy bp = sc->sc_bp;
826 1.1 itohy diskunit = DISKUNIT(bp->b_dev);
827 1.1 itohy part = MMEM_PART(diskunit);
828 1.1 itohy pt = &sc->sc_pt[part];
829 1.1 itohy
830 1.1 itohy /* handle retry */
831 1.1 itohy if (sc->sc_retry++ > MMEM_MAXRETRY) {
832 1.1 itohy /* retry count exceeded */
833 1.2 itohy mmemdone(sc, pt, EIO);
834 1.1 itohy return;
835 1.1 itohy }
836 1.1 itohy
837 1.1 itohy /*
838 1.1 itohy * Start the first phase (phase# = 0).
839 1.1 itohy */
840 1.1 itohy /* start read */
841 1.1 itohy sc->sc_stat = (bp->b_flags & B_READ) ? MMEM_READ : MMEM_WRITE1;
842 1.1 itohy sc->sc_reqr.func_code = htonl(MAPLE_FUNC(MAPLE_FN_MEMCARD));
843 1.1 itohy sc->sc_reqr.pt = part;
844 1.1 itohy sc->sc_reqr.block = htons(bp->b_rawblkno);
845 1.1 itohy sc->sc_reqr.phase = 0; /* first phase */
846 1.1 itohy maple_command(sc->sc_parent, sc->sc_unit, MAPLE_FN_MEMCARD,
847 1.1 itohy MAPLE_COMMAND_BREAD, sizeof sc->sc_reqr / 4, &sc->sc_reqr, 0);
848 1.1 itohy }
849 1.1 itohy
850 1.1 itohy static void
851 1.1 itohy mmemstart_write2(sc)
852 1.1 itohy struct mmem_softc *sc;
853 1.1 itohy {
854 1.1 itohy struct buf *bp;
855 1.1 itohy int diskunit, part;
856 1.1 itohy struct mmem_pt *pt;
857 1.1 itohy
858 1.1 itohy bp = sc->sc_bp;
859 1.1 itohy diskunit = DISKUNIT(bp->b_dev);
860 1.1 itohy part = MMEM_PART(diskunit);
861 1.1 itohy pt = &sc->sc_pt[part];
862 1.1 itohy
863 1.1 itohy /* handle retry */
864 1.1 itohy if (sc->sc_retry++ > MMEM_MAXRETRY - 2 /* spare for verify read */) {
865 1.1 itohy /* retry count exceeded */
866 1.2 itohy mmemdone(sc, pt, EIO);
867 1.1 itohy return;
868 1.1 itohy }
869 1.1 itohy
870 1.1 itohy /*
871 1.1 itohy * Start the first phase (phase# = 0).
872 1.1 itohy */
873 1.1 itohy /* start write */
874 1.1 itohy sc->sc_stat = MMEM_WRITE2;
875 1.1 itohy sc->sc_reqw.func_code = htonl(MAPLE_FUNC(MAPLE_FN_MEMCARD));
876 1.1 itohy sc->sc_reqw.pt = part;
877 1.1 itohy sc->sc_reqw.block = htons(bp->b_rawblkno);
878 1.1 itohy sc->sc_reqw.phase = 0; /* first phase */
879 1.1 itohy bcopy(sc->sc_iobuf /* + sc->sc_waccsz * phase */,
880 1.1 itohy sc->sc_reqw.data, sc->sc_waccsz);
881 1.1 itohy maple_command(sc->sc_parent, sc->sc_unit, MAPLE_FN_MEMCARD,
882 1.1 itohy MAPLE_COMMAND_BWRITE, MMEM_SIZE_REQW(sc) / 4, &sc->sc_reqw,
883 1.1 itohy MAPLE_FLAG_CMD_PERIODIC_TIMING);
884 1.1 itohy }
885 1.1 itohy
886 1.1 itohy static void
887 1.1 itohy mmemdone(sc, pt, err)
888 1.1 itohy struct mmem_softc *sc;
889 1.1 itohy struct mmem_pt *pt;
890 1.1 itohy int err;
891 1.1 itohy {
892 1.1 itohy struct buf *bp = sc->sc_bp;
893 1.1 itohy int s;
894 1.1 itohy int bcnt;
895 1.1 itohy
896 1.1 itohy KASSERT(bp);
897 1.1 itohy
898 1.1 itohy if (err) {
899 1.1 itohy bcnt = sc->sc_iobuf - bp->b_data;
900 1.1 itohy bp->b_resid = bp->b_bcount - bcnt;
901 1.1 itohy
902 1.1 itohy /* raise error if no block is read */
903 1.1 itohy if (bcnt == 0) {
904 1.2 itohy bp->b_error = err;
905 1.1 itohy bp->b_flags |= B_ERROR;
906 1.1 itohy }
907 1.1 itohy goto term_xfer;
908 1.1 itohy }
909 1.1 itohy
910 1.1 itohy sc->sc_iobuf += sc->sc_bsize;
911 1.1 itohy if (--sc->sc_cnt == 0) {
912 1.1 itohy term_xfer:
913 1.1 itohy /* terminate current transfer */
914 1.1 itohy sc->sc_bp = NULL;
915 1.1 itohy s = splbio();
916 1.1 itohy disk_unbusy(&pt->pt_dk, sc->sc_iobuf - bp->b_data,
917 1.1 itohy sc->sc_stat == MMEM_READ);
918 1.1 itohy biodone(bp);
919 1.1 itohy splx(s);
920 1.1 itohy
921 1.1 itohy /* go next transfer */
922 1.1 itohy mmemstart(sc);
923 1.1 itohy } else {
924 1.1 itohy /* go next block */
925 1.1 itohy bp->b_rawblkno++;
926 1.1 itohy sc->sc_retry = 0;
927 1.1 itohy mmemstart_bp(sc);
928 1.1 itohy }
929 1.1 itohy }
930 1.1 itohy
931 1.1 itohy int
932 1.1 itohy mmemread(dev, uio, flags)
933 1.1 itohy dev_t dev;
934 1.1 itohy struct uio *uio;
935 1.1 itohy int flags;
936 1.1 itohy {
937 1.1 itohy
938 1.1 itohy return (physio(mmemstrategy, NULL, dev, B_READ, minphys, uio));
939 1.1 itohy }
940 1.1 itohy
941 1.1 itohy int
942 1.1 itohy mmemwrite(dev, uio, flags)
943 1.1 itohy dev_t dev;
944 1.1 itohy struct uio *uio;
945 1.1 itohy int flags;
946 1.1 itohy {
947 1.1 itohy
948 1.1 itohy return (physio(mmemstrategy, NULL, dev, B_WRITE, minphys, uio));
949 1.1 itohy }
950 1.1 itohy
951 1.1 itohy int
952 1.1 itohy mmemioctl(dev, cmd, data, flag, p)
953 1.1 itohy dev_t dev;
954 1.1 itohy u_long cmd;
955 1.1 itohy caddr_t data;
956 1.1 itohy int flag;
957 1.1 itohy struct proc *p;
958 1.1 itohy {
959 1.1 itohy int diskunit, unit, part;
960 1.1 itohy struct mmem_softc *sc;
961 1.1 itohy struct mmem_pt *pt;
962 1.1 itohy
963 1.1 itohy diskunit = DISKUNIT(dev);
964 1.1 itohy unit = MMEM_UNIT(diskunit);
965 1.1 itohy part = MMEM_PART(diskunit);
966 1.1 itohy sc = mmem_cd.cd_devs[unit];
967 1.1 itohy pt = &sc->sc_pt[part];
968 1.1 itohy
969 1.1 itohy switch (cmd) {
970 1.1 itohy case DIOCGDINFO:
971 1.1 itohy *(struct disklabel *)data = *pt->pt_dk.dk_label; /* XXX */
972 1.1 itohy break;
973 1.1 itohy
974 1.1 itohy default:
975 1.1 itohy /* generic maple ioctl */
976 1.1 itohy return maple_unit_ioctl(sc->sc_parent, sc->sc_unit, cmd, data,
977 1.1 itohy flag, p);
978 1.1 itohy }
979 1.1 itohy
980 1.1 itohy return 0;
981 1.1 itohy }
982