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