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