Home | History | Annotate | Line # | Download | only in qbus
rf.c revision 1.31
      1 /*	$NetBSD: rf.c,v 1.31 2015/01/02 19:42:07 christos Exp $	*/
      2 /*
      3  * Copyright (c) 2002 Jochen Kunz.
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  * 3. The name of Jochen Kunz may not be used to endorse or promote
     15  *    products derived from this software without specific prior
     16  *    written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY JOCHEN KUNZ
     19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL JOCHEN KUNZ
     22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     28  * POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 /*
     32 TODO:
     33 - Better LBN bound checking, block padding for SD disks.
     34 - Formatting / "Set Density"
     35 - Better error handling / detailed error reason reporting.
     36 */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: rf.c,v 1.31 2015/01/02 19:42:07 christos Exp $");
     40 
     41 /* autoconfig stuff */
     42 #include <sys/param.h>
     43 #include <sys/device.h>
     44 #include <sys/conf.h>
     45 #include "locators.h"
     46 #include "ioconf.h"
     47 
     48 /* bus_space / bus_dma */
     49 #include <sys/bus.h>
     50 
     51 /* UniBus / QBus specific stuff */
     52 #include <dev/qbus/ubavar.h>
     53 
     54 /* disk interface */
     55 #include <sys/types.h>
     56 #include <sys/disklabel.h>
     57 #include <sys/disk.h>
     58 
     59 /* general system data and functions */
     60 #include <sys/systm.h>
     61 #include <sys/ioctl.h>
     62 #include <sys/ioccom.h>
     63 
     64 /* physio / buffer handling */
     65 #include <sys/buf.h>
     66 #include <sys/bufq.h>
     67 
     68 /* tsleep / sleep / wakeup */
     69 #include <sys/proc.h>
     70 /* hz for above */
     71 #include <sys/kernel.h>
     72 
     73 /* bitdefinitions for RX211 */
     74 #include <dev/qbus/rfreg.h>
     75 
     76 
     77 #define	RFS_DENS	0x0001		/* single or double density */
     78 #define	RFS_AD		0x0002		/* density auto detect */
     79 #define	RFS_NOTINIT	0x0000		/* not initialized */
     80 #define	RFS_PROBING	0x0010		/* density detect / verify started */
     81 #define	RFS_FBUF	0x0020		/* Fill Buffer */
     82 #define	RFS_EBUF	0x0030		/* Empty Buffer */
     83 #define	RFS_WSEC	0x0040		/* Write Sector */
     84 #define	RFS_RSEC	0x0050		/* Read Sector */
     85 #define	RFS_SMD		0x0060		/* Set Media Density */
     86 #define	RFS_RSTAT	0x0070		/* Read Status */
     87 #define	RFS_WDDS	0x0080		/* Write Deleted Data Sector */
     88 #define	RFS_REC		0x0090		/* Read Error Code */
     89 #define	RFS_IDLE	0x00a0		/* controller is idle */
     90 #define	RFS_CMDS	0x00f0		/* command mask */
     91 #define	RFS_OPEN_A	0x0100		/* partition a open */
     92 #define	RFS_OPEN_B	0x0200		/* partition b open */
     93 #define	RFS_OPEN_C	0x0400		/* partition c open */
     94 #define	RFS_OPEN_MASK	0x0f00		/* mask for open partitions */
     95 #define RFS_OPEN_SHIFT	8		/* to shift 1 to get RFS_OPEN_A */
     96 #define	RFS_SETCMD(rf, state)	((rf) = ((rf) & ~RFS_CMDS) | (state))
     97 
     98 
     99 
    100 /* autoconfig stuff */
    101 static int rfc_match(device_t, cfdata_t, void *);
    102 static void rfc_attach(device_t, device_t, void *);
    103 static int rf_match(device_t, cfdata_t, void *);
    104 static void rf_attach(device_t, device_t, void *);
    105 static int rf_print(void *, const char *);
    106 
    107 /* device interface functions / interface to disk(9) */
    108 dev_type_open(rfopen);
    109 dev_type_close(rfclose);
    110 dev_type_read(rfread);
    111 dev_type_write(rfwrite);
    112 dev_type_ioctl(rfioctl);
    113 dev_type_strategy(rfstrategy);
    114 dev_type_dump(rfdump);
    115 dev_type_size(rfsize);
    116 
    117 
    118 /* Entries in block and character major device number switch table. */
    119 const struct bdevsw rf_bdevsw = {
    120 	.d_open = rfopen,
    121 	.d_close = rfclose,
    122 	.d_strategy = rfstrategy,
    123 	.d_ioctl = rfioctl,
    124 	.d_dump = rfdump,
    125 	.d_psize = rfsize,
    126 	.d_discard = nodiscard,
    127 	.d_flag = D_DISK
    128 };
    129 
    130 const struct cdevsw rf_cdevsw = {
    131 	.d_open = rfopen,
    132 	.d_close = rfclose,
    133 	.d_read = rfread,
    134 	.d_write = rfwrite,
    135 	.d_ioctl = rfioctl,
    136 	.d_stop = nostop,
    137 	.d_tty = notty,
    138 	.d_poll = nopoll,
    139 	.d_mmap = nommap,
    140 	.d_kqfilter = nokqfilter,
    141 	.d_discard = nodiscard,
    142 	.d_flag = D_DISK
    143 };
    144 
    145 
    146 
    147 struct rfc_softc {
    148 	device_t sc_dev;		/* common device data */
    149 	device_t sc_childs[2];		/* child devices */
    150 	struct evcnt sc_intr_count;	/* Interrupt counter for statistics */
    151 	struct buf *sc_curbuf;		/* buf that is currently in work */
    152 	bus_space_tag_t sc_iot;		/* bus_space I/O tag */
    153 	bus_space_handle_t sc_ioh;	/* bus_space I/O handle */
    154 	bus_dma_tag_t sc_dmat;		/* bus_dma DMA tag */
    155 	bus_dmamap_t sc_dmam;		/* bus_dma DMA map */
    156 	void *sc_bufidx;		/* current position in buffer data */
    157 	int sc_curchild;		/* child whos bufq is in work */
    158 	int sc_bytesleft;		/* bytes left to transfer */
    159 	u_int8_t type;			/* controller type, 1 or 2 */
    160 };
    161 
    162 
    163 
    164 CFATTACH_DECL_NEW(
    165 	rfc,
    166 	sizeof(struct rfc_softc),
    167 	rfc_match,
    168 	rfc_attach,
    169 	NULL,
    170 	NULL
    171 );
    172 
    173 
    174 
    175 struct rf_softc {
    176 	device_t sc_dev;		/* common device data */
    177 	struct disk sc_disk;		/* common disk device data */
    178 	struct rfc_softc *sc_rfc;	/* our parent */
    179 	struct bufq_state *sc_bufq;	/* queue of pending transfers */
    180 	int sc_state;			/* state of drive */
    181 	u_int8_t sc_dnum;		/* drive number, 0 or 1 */
    182 };
    183 
    184 
    185 
    186 CFATTACH_DECL_NEW(
    187 	rf,
    188 	sizeof(struct rf_softc),
    189 	rf_match,
    190 	rf_attach,
    191 	NULL,
    192 	NULL
    193 );
    194 
    195 
    196 
    197 struct rfc_attach_args {
    198 	u_int8_t type;		/* controller type, 1 or 2 */
    199 	u_int8_t dnum;		/* drive number, 0 or 1 */
    200 };
    201 
    202 
    203 
    204 const struct dkdriver rfdkdriver = {
    205 	rfstrategy
    206 };
    207 
    208 
    209 
    210 /* helper functions */
    211 int rfc_sendcmd(struct rfc_softc *, int, int, int);
    212 struct rf_softc* get_new_buf( struct rfc_softc *);
    213 static void rfc_intr(void *);
    214 
    215 
    216 
    217 /*
    218  * Issue a reset command to the controller and look for the bits in
    219  * RX2CS and RX2ES.
    220  * RX2CS_RX02 and / or RX2CS_DD can be set,
    221  * RX2ES has to be set, all other bits must be 0
    222  */
    223 int
    224 rfc_match(device_t parent, cfdata_t match, void *aux)
    225 {
    226 	struct uba_attach_args *ua = aux;
    227 	int i;
    228 
    229 	/* Issue reset command. */
    230 	bus_space_write_2(ua->ua_iot, ua->ua_ioh, RX2CS, RX2CS_INIT);
    231 	/* Wait for the controller to become ready, that is when
    232 	 * RX2CS_DONE, RX2ES_RDY and RX2ES_ID are set. */
    233 	for (i = 0 ; i < 20 ; i++) {
    234 		if ((bus_space_read_2(ua->ua_iot, ua->ua_ioh, RX2CS)
    235 		    & RX2CS_DONE) != 0
    236 		    && (bus_space_read_2(ua->ua_iot, ua->ua_ioh, RX2ES)
    237 		    & (RX2ES_RDY | RX2ES_ID)) != 0)
    238 			break;
    239 		DELAY(100000);	/* wait 100ms */
    240 	}
    241 	/*
    242 	 * Give up if the timeout has elapsed
    243 	 * and the controller is not ready.
    244 	 */
    245 	if (i >= 20)
    246 		return(0);
    247 	/*
    248 	 * Issue a Read Status command with interrupt enabled.
    249 	 * The uba(4) driver wants to catch the interrupt to get the
    250 	 * interrupt vector and level of the device
    251 	 */
    252 	bus_space_write_2(ua->ua_iot, ua->ua_ioh, RX2CS,
    253 	    RX2CS_RSTAT | RX2CS_IE);
    254 	/*
    255 	 * Wait for command to finish, ignore errors and
    256 	 * abort if the controller does not respond within the timeout
    257 	 */
    258 	for (i = 0 ; i < 20 ; i++) {
    259 		if ((bus_space_read_2(ua->ua_iot, ua->ua_ioh, RX2CS)
    260 		    & (RX2CS_DONE | RX2CS_IE)) != 0
    261 		    && (bus_space_read_2(ua->ua_iot, ua->ua_ioh, RX2ES)
    262 		    & RX2ES_RDY) != 0 )
    263 			return(1);
    264 		DELAY(100000);	/* wait 100ms */
    265 	}
    266 	return(0);
    267 }
    268 
    269 
    270 
    271 /* #define RX02_PROBE 1 */
    272 #ifdef RX02_PROBE
    273 /*
    274  * Probe the density of an inserted floppy disk.
    275  * This is done by reading a sector from disk.
    276  * Return -1 on error, 0 on SD and 1 on DD.
    277  */
    278 int rfcprobedens(struct rfc_softc *, int);
    279 int
    280 rfcprobedens(struct rfc_softc *rfc_sc, int dnum)
    281 {
    282 	int dens_flag;
    283 	int i;
    284 
    285 	dens_flag = 0;
    286 	do {
    287 		bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS,
    288 		    RX2CS_RSEC | (dens_flag == 0 ? 0 : RX2CS_DD)
    289 		    | (dnum == 0 ? 0 : RX2CS_US));
    290 		/*
    291 		 * Transfer request set?
    292 		 * Wait 50us, the controller needs this time to setle
    293 		 */
    294 		DELAY(50);
    295 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    296 		    & RX2CS_TR) == 0) {
    297 			printf("%s: did not respond to Read Sector CMD(1)\n",
    298 			    device_xname(rfc_sc->sc_dev));
    299 			return(-1);
    300 		}
    301 		bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2SA, 1);
    302 		/* Wait 50us, the controller needs this time to setle */
    303 		DELAY(50);
    304 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    305 		    & RX2CS_TR) == 0) {
    306 			printf("%s: did not respond to Read Sector CMD(2)\n",
    307 			    device_xname(rfc_sc->sc_dev));
    308 			return(-1);
    309 		}
    310 		bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2TA, 1);
    311 		/* Wait for the command to finish */
    312 		for (i = 0 ; i < 200 ; i++) {
    313 			if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh,
    314 			    RX2CS) & RX2CS_DONE) != 0)
    315 				break;
    316 			DELAY(10000);	/* wait 10ms */
    317 		}
    318 		if (i >= 200) {
    319 			printf("%s: did not respond to Read Sector CMD(3)\n",
    320 			    device_xname(rfc_sc->sc_dev));
    321 			return(-1);
    322 		}
    323 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    324 		    & RX2CS_ERR) == 0)
    325 			return(dens_flag);
    326 	} while (rfc_sc->type == 2 && dens_flag++ == 0);
    327 	return(-1);
    328 }
    329 #endif /* RX02_PROBE */
    330 
    331 
    332 
    333 void
    334 rfc_attach(device_t parent, device_t self, void *aux)
    335 {
    336 	struct rfc_softc *rfc_sc = device_private(self);
    337 	struct uba_attach_args *ua = aux;
    338 	struct rfc_attach_args rfc_aa;
    339 	int i;
    340 
    341 	rfc_sc->sc_dev = self;
    342 	rfc_sc->sc_iot = ua->ua_iot;
    343 	rfc_sc->sc_ioh = ua->ua_ioh;
    344 	rfc_sc->sc_dmat = ua->ua_dmat;
    345 	rfc_sc->sc_curbuf = NULL;
    346 	/* Tell the QBus busdriver about our interrupt handler. */
    347 	uba_intr_establish(ua->ua_icookie, ua->ua_cvec, rfc_intr, rfc_sc,
    348 	    &rfc_sc->sc_intr_count);
    349 	/* Attach to the interrupt counter, see evcnt(9) */
    350 	evcnt_attach_dynamic(&rfc_sc->sc_intr_count, EVCNT_TYPE_INTR,
    351 	    ua->ua_evcnt, device_xname(rfc_sc->sc_dev), "intr");
    352 	/* get a bus_dma(9) handle */
    353 	i = bus_dmamap_create(rfc_sc->sc_dmat, RX2_BYTE_DD, 1, RX2_BYTE_DD, 0,
    354 	    BUS_DMA_ALLOCNOW, &rfc_sc->sc_dmam);
    355 	if (i != 0) {
    356 		printf("rfc_attach: Error creating bus dma map: %d\n", i);
    357 		return;
    358 	}
    359 
    360 	/* Issue reset command. */
    361 	bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS, RX2CS_INIT);
    362 	/*
    363 	 * Wait for the controller to become ready, that is when
    364 	 * RX2CS_DONE, RX2ES_RDY and RX2ES_ID are set.
    365 	 */
    366 	for (i = 0 ; i < 20 ; i++) {
    367 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    368 		    & RX2CS_DONE) != 0
    369 		    && (bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2ES)
    370 		    & (RX2ES_RDY | RX2ES_ID)) != 0)
    371 			break;
    372 		DELAY(100000);	/* wait 100ms */
    373 	}
    374 	/*
    375 	 * Give up if the timeout has elapsed
    376 	 * and the controller is not ready.
    377 	 */
    378 	if (i >= 20) {
    379 		printf(": did not respond to INIT CMD\n");
    380 		return;
    381 	}
    382 	/* Is ths a RX01 or a RX02? */
    383 	if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    384 	    & RX2CS_RX02) != 0) {
    385 		rfc_sc->type = 2;
    386 		rfc_aa.type = 2;
    387 	} else {
    388 		rfc_sc->type = 1;
    389 		rfc_aa.type = 1;
    390 	}
    391 	printf(": RX0%d\n", rfc_sc->type);
    392 
    393 #ifndef RX02_PROBE
    394 	/*
    395 	 * Bouth disk drievs and the controller are one physical unit.
    396 	 * If we found the controller, there will be bouth disk drievs.
    397 	 * So attach them.
    398 	 */
    399 	rfc_aa.dnum = 0;
    400 	rfc_sc->sc_childs[0] = config_found(rfc_sc->sc_dev, &rfc_aa, rf_print);
    401 	rfc_aa.dnum = 1;
    402 	rfc_sc->sc_childs[1] = config_found(rfc_sc->sc_dev, &rfc_aa, rf_print);
    403 #else /* RX02_PROBE */
    404 	/*
    405 	 * There are clones of the DEC RX system with standard shugart
    406 	 * interface. In this case we can not be sure that there are
    407 	 * bouth disk drievs. So we want to do a detection of attached
    408 	 * drives. This is done by reading a sector from disk. This means
    409 	 * that there must be a formatted disk in the drive at boot time.
    410 	 * This is bad, but I did not find another way to detect the
    411 	 * (non)existence of a floppy drive.
    412 	 */
    413 	if (rfcprobedens(rfc_sc, 0) >= 0) {
    414 		rfc_aa.dnum = 0;
    415 		rfc_sc->sc_childs[0] = config_found(rfc_sc->sc_dev, &rfc_aa,
    416 		    rf_print);
    417 	} else
    418 		rfc_sc->sc_childs[0] = NULL;
    419 	if (rfcprobedens(rfc_sc, 1) >= 0) {
    420 		rfc_aa.dnum = 1;
    421 		rfc_sc->sc_childs[1] = config_found(rfc_sc->sc_dev, &rfc_aa,
    422 		    rf_print);
    423 	} else
    424 		rfc_sc->sc_childs[1] = NULL;
    425 #endif /* RX02_PROBE */
    426 	return;
    427 }
    428 
    429 
    430 
    431 int
    432 rf_match(device_t parent, cfdata_t match, void *aux)
    433 {
    434 	struct rfc_attach_args *rfc_aa = aux;
    435 
    436 	/*
    437 	 * Only attach if the locator is wildcarded or
    438 	 * if the specified locator addresses the current device.
    439 	 */
    440 	if (match->cf_loc[RFCCF_DRIVE] == RFCCF_DRIVE_DEFAULT ||
    441 	    match->cf_loc[RFCCF_DRIVE] == rfc_aa->dnum)
    442 		return(1);
    443 	return(0);
    444 }
    445 
    446 
    447 
    448 void
    449 rf_attach(device_t parent, device_t self, void *aux)
    450 {
    451 	struct rf_softc *rf_sc = device_private(self);
    452 	struct rfc_softc *rfc_sc = device_private(parent);
    453 	struct rfc_attach_args *rfc_aa = (struct rfc_attach_args *)aux;
    454 	struct disklabel *dl;
    455 
    456 	rf_sc->sc_dev = self;
    457 	rf_sc->sc_rfc = rfc_sc;
    458 	rf_sc->sc_dnum = rfc_aa->dnum;
    459 	rf_sc->sc_state = 0;
    460 	disk_init(&rf_sc->sc_disk, device_xname(rf_sc->sc_dev), &rfdkdriver);
    461 	disk_attach(&rf_sc->sc_disk);
    462 	dl = rf_sc->sc_disk.dk_label;
    463 	dl->d_type = DKTYPE_FLOPPY;		/* drive type */
    464 	dl->d_magic = DISKMAGIC;		/* the magic number */
    465 	dl->d_magic2 = DISKMAGIC;
    466 	dl->d_typename[0] = 'R';
    467 	dl->d_typename[1] = 'X';
    468 	dl->d_typename[2] = '0';
    469 	dl->d_typename[3] = rfc_sc->type == 1 ? '1' : '2';	/* type name */
    470 	dl->d_typename[4] = '\0';
    471 	dl->d_secsize = DEV_BSIZE;		/* bytes per sector */
    472 	/*
    473 	 * Fill in some values to have a initialized data structure. Some
    474 	 * values will be reset by rfopen() depending on the actual density.
    475 	 */
    476 	dl->d_nsectors = RX2_SECTORS;		/* sectors per track */
    477 	dl->d_ntracks = 1;								/* tracks per cylinder */
    478 	dl->d_ncylinders = RX2_TRACKS;		/* cylinders per unit */
    479 	dl->d_secpercyl = RX2_SECTORS;		/* sectors per cylinder */
    480 	dl->d_secperunit = RX2_SECTORS * RX2_TRACKS;	/* sectors per unit */
    481 	dl->d_rpm = 360;			/* rotational speed */
    482 	dl->d_interleave = 1;			/* hardware sector interleave */
    483 	/* number of partitions in following */
    484 	dl->d_npartitions = MAXPARTITIONS;
    485 	dl->d_bbsize = 0;		/* size of boot area at sn0, bytes */
    486 	dl->d_sbsize = 0;		/* max size of fs superblock, bytes */
    487 	/* number of sectors in partition */
    488 	dl->d_partitions[0].p_size = 501;
    489 	dl->d_partitions[0].p_offset = 0;	/* starting sector */
    490 	dl->d_partitions[0].p_fsize = 0;	/* fs basic fragment size */
    491 	dl->d_partitions[0].p_fstype = 0;	/* fs type */
    492 	dl->d_partitions[0].p_frag = 0;		/* fs fragments per block */
    493 	dl->d_partitions[1].p_size = RX2_SECTORS * RX2_TRACKS / 2;
    494 	dl->d_partitions[1].p_offset = 0;	/* starting sector */
    495 	dl->d_partitions[1].p_fsize = 0;	/* fs basic fragment size */
    496 	dl->d_partitions[1].p_fstype = 0;	/* fs type */
    497 	dl->d_partitions[1].p_frag = 0;		/* fs fragments per block */
    498 	dl->d_partitions[2].p_size = RX2_SECTORS * RX2_TRACKS;
    499 	dl->d_partitions[2].p_offset = 0;	/* starting sector */
    500 	dl->d_partitions[2].p_fsize = 0;	/* fs basic fragment size */
    501 	dl->d_partitions[2].p_fstype = 0;	/* fs type */
    502 	dl->d_partitions[2].p_frag = 0;		/* fs fragments per block */
    503 	bufq_alloc(&rf_sc->sc_bufq, "disksort", BUFQ_SORT_CYLINDER);
    504 	printf("\n");
    505 	return;
    506 }
    507 
    508 
    509 
    510 int
    511 rf_print(void *aux, const char *name)
    512 {
    513 	struct rfc_attach_args *rfc_aa = aux;
    514 
    515 	if (name != NULL)
    516 		aprint_normal("RX0%d at %s", rfc_aa->type, name);
    517 	aprint_normal(" drive %d", rfc_aa->dnum);
    518 	return(UNCONF);
    519 }
    520 
    521 
    522 
    523 /* Send a command to the controller */
    524 int
    525 rfc_sendcmd(struct rfc_softc *rfc_sc, int cmd, int data1, int data2)
    526 {
    527 
    528 	/* Write command to CSR. */
    529 	bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS, cmd);
    530 	/* Wait 50us, the controller needs this time to setle. */
    531 	DELAY(50);
    532 	/* Write parameter 1 to DBR */
    533 	if ((cmd & RX2CS_FC) != RX2CS_RSTAT) {
    534 		/* Transfer request set? */
    535 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    536 		    & RX2CS_TR) == 0) {
    537 			printf("%s: did not respond to CMD %x (1)\n",
    538 			    device_xname(rfc_sc->sc_dev), cmd);
    539 			return(-1);
    540 		}
    541 		bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2DB,
    542 		    data1);
    543 	}
    544 	/* Write parameter 2 to DBR */
    545 	if ((cmd & RX2CS_FC) <= RX2CS_RSEC || (cmd & RX2CS_FC) == RX2CS_WDDS) {
    546 		/* Wait 50us, the controller needs this time to setle. */
    547 		DELAY(50);
    548 		/* Transfer request set? */
    549 		if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2CS)
    550 		    & RX2CS_TR) == 0) {
    551 			printf("%s: did not respond to CMD %x (2)\n",
    552 			    device_xname(rfc_sc->sc_dev), cmd);
    553 			return(-1);
    554 		}
    555 		bus_space_write_2(rfc_sc->sc_iot, rfc_sc->sc_ioh, RX2DB,
    556 		    data2);
    557 	}
    558 	return(1);
    559 }
    560 
    561 
    562 
    563 void
    564 rfstrategy(struct buf *buf)
    565 {
    566 	struct rf_softc *rf_sc;
    567 	struct rfc_softc *rfc_sc;
    568 	int s;
    569 
    570 	if ((rf_sc = device_lookup_private(&rf_cd, DISKUNIT(buf->b_dev))) == NULL) {
    571 		buf->b_error = ENXIO;
    572 		biodone(buf);
    573 		return;
    574 	}
    575 	rfc_sc = rf_sc->sc_rfc;
    576 	/* We are going to operate on a non-open dev? PANIC! */
    577 	if ((rf_sc->sc_state & (1 << (DISKPART(buf->b_dev) + RFS_OPEN_SHIFT)))
    578 	    == 0)
    579 		panic("rfstrategy: can not operate on non-open drive %s "
    580 		    "partition %"PRIu32, device_xname(rf_sc->sc_dev),
    581 		    DISKPART(buf->b_dev));
    582 	if (buf->b_bcount == 0) {
    583 		biodone(buf);
    584 		return;
    585 	}
    586 	/*
    587 	 * bufq_put() operates on b_rawblkno. rfstrategy() gets
    588 	 * only b_blkno that is partition relative. As a floppy does not
    589 	 * have partitions b_rawblkno == b_blkno.
    590 	 */
    591 	buf->b_rawblkno = buf->b_blkno;
    592 	/*
    593 	 * from sys/kern/subr_disk.c:
    594 	 * Seek sort for disks.  We depend on the driver which calls us using
    595 	 * b_resid as the current cylinder number.
    596 	 */
    597 	s = splbio();
    598 	if (rfc_sc->sc_curbuf == NULL) {
    599 		rfc_sc->sc_curchild = rf_sc->sc_dnum;
    600 		rfc_sc->sc_curbuf = buf;
    601 		rfc_sc->sc_bufidx = buf->b_data;
    602 		rfc_sc->sc_bytesleft = buf->b_bcount;
    603 		rfc_intr(rfc_sc);
    604 	} else {
    605 		buf->b_resid = buf->b_blkno / RX2_SECTORS;
    606 		bufq_put(rf_sc->sc_bufq, buf);
    607 		buf->b_resid = 0;
    608 	}
    609 	splx(s);
    610 }
    611 
    612 /*
    613  * Look if there is another buffer in the bufferqueue of this drive
    614  * and start to process it if there is one.
    615  * If the bufferqueue is empty, look at the bufferqueue of the other drive
    616  * that is attached to this controller.
    617  * Start procesing the bufferqueue of the other drive if it isn't empty.
    618  * Return a pointer to the softc structure of the drive that is now
    619  * ready to process a buffer or NULL if there is no buffer in either queues.
    620  */
    621 struct rf_softc*
    622 get_new_buf( struct rfc_softc *rfc_sc)
    623 {
    624 	struct rf_softc *rf_sc;
    625 	struct rf_softc *other_drive;
    626 
    627 	rf_sc = device_private(rfc_sc->sc_childs[rfc_sc->sc_curchild]);
    628 	rfc_sc->sc_curbuf = bufq_get(rf_sc->sc_bufq);
    629 	if (rfc_sc->sc_curbuf != NULL) {
    630 		rfc_sc->sc_bufidx = rfc_sc->sc_curbuf->b_data;
    631 		rfc_sc->sc_bytesleft = rfc_sc->sc_curbuf->b_bcount;
    632 	} else {
    633 		RFS_SETCMD(rf_sc->sc_state, RFS_IDLE);
    634 		other_drive = device_private(
    635 		    rfc_sc->sc_childs[ rfc_sc->sc_curchild == 0 ? 1 : 0]);
    636 		if (other_drive != NULL
    637 		    && bufq_peek(other_drive->sc_bufq) != NULL) {
    638 			rfc_sc->sc_curchild = rfc_sc->sc_curchild == 0 ? 1 : 0;
    639 			rf_sc = other_drive;
    640 			rfc_sc->sc_curbuf = bufq_get(rf_sc->sc_bufq);
    641 			rfc_sc->sc_bufidx = rfc_sc->sc_curbuf->b_data;
    642 			rfc_sc->sc_bytesleft = rfc_sc->sc_curbuf->b_bcount;
    643 		} else
    644 			return(NULL);
    645 	}
    646 	return(rf_sc);
    647 }
    648 
    649 
    650 
    651 void
    652 rfc_intr(void *intarg)
    653 {
    654 	struct rfc_softc *rfc_sc = intarg;
    655 	struct rf_softc *rf_sc;
    656 	int i;
    657 
    658 	rf_sc = device_private(rfc_sc->sc_childs[rfc_sc->sc_curchild]);
    659 	for (;;) {
    660 		/*
    661 		 * First clean up from previous command...
    662 		 */
    663 		switch (rf_sc->sc_state & RFS_CMDS) {
    664 		case RFS_PROBING:	/* density detect / verify started */
    665 			disk_unbusy(&rf_sc->sc_disk, 0, 1);
    666 			if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh,
    667 			    RX2CS) & RX2CS_ERR) == 0) {
    668 				RFS_SETCMD(rf_sc->sc_state, RFS_IDLE);
    669 				wakeup(rf_sc);
    670 			} else {
    671 				if (rfc_sc->type == 2
    672 				    && (rf_sc->sc_state & RFS_DENS) == 0
    673 				    && (rf_sc->sc_state & RFS_AD) != 0) {
    674 					/* retry at DD */
    675 					rf_sc->sc_state |= RFS_DENS;
    676 					disk_busy(&rf_sc->sc_disk);
    677 					if (rfc_sendcmd(rfc_sc, RX2CS_RSEC
    678 					    | RX2CS_IE | RX2CS_DD |
    679 					    (rf_sc->sc_dnum == 0 ? 0 :
    680 					    RX2CS_US), 1, 1) < 0) {
    681 						disk_unbusy(&rf_sc->sc_disk,
    682 						    0, 1);
    683 						RFS_SETCMD(rf_sc->sc_state,
    684 						    RFS_NOTINIT);
    685 						wakeup(rf_sc);
    686 					}
    687 				} else {
    688 					printf("%s: density error.\n",
    689 					    device_xname(rf_sc->sc_dev));
    690 					RFS_SETCMD(rf_sc->sc_state,RFS_NOTINIT);
    691 					wakeup(rf_sc);
    692 				}
    693 			}
    694 			return;
    695 		case RFS_IDLE:	/* controller is idle */
    696 			if (rfc_sc->sc_curbuf->b_bcount
    697 			    % ((rf_sc->sc_state & RFS_DENS) == 0
    698 			    ? RX2_BYTE_SD : RX2_BYTE_DD) != 0) {
    699 				/*
    700 				 * can only handle blocks that are a multiple
    701 				 * of the physical block size
    702 				 */
    703 				rfc_sc->sc_curbuf->b_error = EIO;
    704 			}
    705 			RFS_SETCMD(rf_sc->sc_state, (rfc_sc->sc_curbuf->b_flags
    706 			    & B_READ) != 0 ? RFS_RSEC : RFS_FBUF);
    707 			break;
    708 		case RFS_RSEC:	/* Read Sector */
    709 			disk_unbusy(&rf_sc->sc_disk, 0, 1);
    710 			/* check for errors */
    711 			if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh,
    712 			    RX2CS) & RX2CS_ERR) != 0) {
    713 				/* should do more verbose error reporting */
    714 				printf("rfc_intr: Error reading secotr: %x\n",
    715 				    bus_space_read_2(rfc_sc->sc_iot,
    716 				    rfc_sc->sc_ioh, RX2ES) );
    717 				rfc_sc->sc_curbuf->b_error = EIO;
    718 			}
    719 			RFS_SETCMD(rf_sc->sc_state, RFS_EBUF);
    720 			break;
    721 		case RFS_WSEC:	/* Write Sector */
    722 			i = (rf_sc->sc_state & RFS_DENS) == 0
    723 				? RX2_BYTE_SD : RX2_BYTE_DD;
    724 			disk_unbusy(&rf_sc->sc_disk, i, 0);
    725 			/* check for errors */
    726 			if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh,
    727 			    RX2CS) & RX2CS_ERR) != 0) {
    728 				/* should do more verbose error reporting */
    729 				printf("rfc_intr: Error writing secotr: %x\n",
    730 				    bus_space_read_2(rfc_sc->sc_iot,
    731 				    rfc_sc->sc_ioh, RX2ES) );
    732 				rfc_sc->sc_curbuf->b_error = EIO;
    733 				break;
    734 			}
    735 			if (rfc_sc->sc_bytesleft > i) {
    736 				rfc_sc->sc_bytesleft -= i;
    737 				rfc_sc->sc_bufidx =
    738 				    (char *)rfc_sc->sc_bufidx + i;
    739 			} else {
    740 				biodone(rfc_sc->sc_curbuf);
    741 				rf_sc = get_new_buf( rfc_sc);
    742 				if (rf_sc == NULL)
    743 					return;
    744 			}
    745 			RFS_SETCMD(rf_sc->sc_state,
    746 			    (rfc_sc->sc_curbuf->b_flags & B_READ) != 0
    747 			    ? RFS_RSEC : RFS_FBUF);
    748 			break;
    749 		case RFS_FBUF:	/* Fill Buffer */
    750 			disk_unbusy(&rf_sc->sc_disk, 0, 0);
    751 			bus_dmamap_unload(rfc_sc->sc_dmat, rfc_sc->sc_dmam);
    752 			/* check for errors */
    753 			if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh,
    754 			    RX2CS) & RX2CS_ERR) != 0) {
    755 				/* should do more verbose error reporting */
    756 				printf("rfc_intr: Error while DMA: %x\n",
    757 				    bus_space_read_2(rfc_sc->sc_iot,
    758 				    rfc_sc->sc_ioh, RX2ES));
    759 				rfc_sc->sc_curbuf->b_error = EIO;
    760 			}
    761 			RFS_SETCMD(rf_sc->sc_state, RFS_WSEC);
    762 			break;
    763 		case RFS_EBUF:	/* Empty Buffer */
    764 			i = (rf_sc->sc_state & RFS_DENS) == 0
    765 			    ? RX2_BYTE_SD : RX2_BYTE_DD;
    766 			disk_unbusy(&rf_sc->sc_disk, i, 1);
    767 			bus_dmamap_unload(rfc_sc->sc_dmat, rfc_sc->sc_dmam);
    768 			/* check for errors */
    769 			if ((bus_space_read_2(rfc_sc->sc_iot, rfc_sc->sc_ioh,
    770 			    RX2CS) & RX2CS_ERR) != 0) {
    771 				/* should do more verbose error reporting */
    772 				printf("rfc_intr: Error while DMA: %x\n",
    773 				    bus_space_read_2(rfc_sc->sc_iot,
    774 				    rfc_sc->sc_ioh, RX2ES));
    775 				rfc_sc->sc_curbuf->b_error = EIO;
    776 				break;
    777 			}
    778 			if (rfc_sc->sc_bytesleft > i) {
    779 				rfc_sc->sc_bytesleft -= i;
    780 				rfc_sc->sc_bufidx =
    781 				    (char *)rfc_sc->sc_bufidx + i;
    782 			} else {
    783 				biodone(rfc_sc->sc_curbuf);
    784 				rf_sc = get_new_buf( rfc_sc);
    785 				if (rf_sc == NULL)
    786 					return;
    787 			}
    788 			RFS_SETCMD(rf_sc->sc_state,
    789 			    (rfc_sc->sc_curbuf->b_flags & B_READ) != 0
    790 			    ? RFS_RSEC : RFS_FBUF);
    791 			break;
    792 		case RFS_NOTINIT: /* Device is not open */
    793 		case RFS_SMD:	/* Set Media Density */
    794 		case RFS_RSTAT:	/* Read Status */
    795 		case RFS_WDDS:	/* Write Deleted Data Sector */
    796 		case RFS_REC:	/* Read Error Code */
    797 		default:
    798 			panic("Impossible state in rfc_intr(1): 0x%x\n",
    799 			    rf_sc->sc_state & RFS_CMDS);
    800 		}
    801 
    802 		if (rfc_sc->sc_curbuf->b_error != 0) {
    803 			/*
    804 			 * An error occurred while processing this buffer.
    805 			 * Finish it and try to get a new buffer to process.
    806 			 * Return if there are no buffers in the queues.
    807 			 * This loops until the queues are empty or a new
    808 			 * action was successfully scheduled.
    809 			 */
    810 			rfc_sc->sc_curbuf->b_resid = rfc_sc->sc_bytesleft;
    811 			rfc_sc->sc_curbuf->b_error = EIO;
    812 			biodone(rfc_sc->sc_curbuf);
    813 			rf_sc = get_new_buf( rfc_sc);
    814 			if (rf_sc == NULL)
    815 				return;
    816 			continue;
    817 		}
    818 
    819 		/*
    820 		 * ... then initiate next command.
    821 		 */
    822 		switch (rf_sc->sc_state & RFS_CMDS) {
    823 		case RFS_EBUF:	/* Empty Buffer */
    824 			i = bus_dmamap_load(rfc_sc->sc_dmat, rfc_sc->sc_dmam,
    825 			    rfc_sc->sc_bufidx, (rf_sc->sc_state & RFS_DENS) == 0
    826 			    ? RX2_BYTE_SD : RX2_BYTE_DD,
    827 			    rfc_sc->sc_curbuf->b_proc, BUS_DMA_NOWAIT);
    828 			if (i != 0) {
    829 				printf("rfc_intr: Error loading dmamap: %d\n",
    830 				i);
    831 				rfc_sc->sc_curbuf->b_error = EIO;
    832 				break;
    833 			}
    834 			disk_busy(&rf_sc->sc_disk);
    835 			if (rfc_sendcmd(rfc_sc, RX2CS_EBUF | RX2CS_IE
    836 			    | ((rf_sc->sc_state & RFS_DENS) == 0 ? 0 : RX2CS_DD)
    837 			    | (rf_sc->sc_dnum == 0 ? 0 : RX2CS_US)
    838 			    | ((rfc_sc->sc_dmam->dm_segs[0].ds_addr
    839 			    & 0x30000) >>4), ((rf_sc->sc_state & RFS_DENS) == 0
    840 			    ? RX2_BYTE_SD : RX2_BYTE_DD) / 2,
    841 			    rfc_sc->sc_dmam->dm_segs[0].ds_addr & 0xffff) < 0) {
    842 				disk_unbusy(&rf_sc->sc_disk, 0, 1);
    843 				rfc_sc->sc_curbuf->b_error = EIO;
    844 				bus_dmamap_unload(rfc_sc->sc_dmat,
    845 				rfc_sc->sc_dmam);
    846 			}
    847 			break;
    848 		case RFS_FBUF:	/* Fill Buffer */
    849 			i = bus_dmamap_load(rfc_sc->sc_dmat, rfc_sc->sc_dmam,
    850 			    rfc_sc->sc_bufidx, (rf_sc->sc_state & RFS_DENS) == 0
    851 			    ? RX2_BYTE_SD : RX2_BYTE_DD,
    852 			    rfc_sc->sc_curbuf->b_proc, BUS_DMA_NOWAIT);
    853 			if (i != 0) {
    854 				printf("rfc_intr: Error loading dmamap: %d\n",
    855 				    i);
    856 				rfc_sc->sc_curbuf->b_error = EIO;
    857 				break;
    858 			}
    859 			disk_busy(&rf_sc->sc_disk);
    860 			if (rfc_sendcmd(rfc_sc, RX2CS_FBUF | RX2CS_IE
    861 			    | ((rf_sc->sc_state & RFS_DENS) == 0 ? 0 : RX2CS_DD)
    862 			    | (rf_sc->sc_dnum == 0 ? 0 : RX2CS_US)
    863 			    | ((rfc_sc->sc_dmam->dm_segs[0].ds_addr
    864 			    & 0x30000)>>4), ((rf_sc->sc_state & RFS_DENS) == 0
    865 			    ? RX2_BYTE_SD : RX2_BYTE_DD) / 2,
    866 			    rfc_sc->sc_dmam->dm_segs[0].ds_addr & 0xffff) < 0) {
    867 				disk_unbusy(&rf_sc->sc_disk, 0, 0);
    868 				rfc_sc->sc_curbuf->b_error = EIO;
    869 				bus_dmamap_unload(rfc_sc->sc_dmat,
    870 				    rfc_sc->sc_dmam);
    871 			}
    872 			break;
    873 		case RFS_WSEC:	/* Write Sector */
    874 			i = (rfc_sc->sc_curbuf->b_bcount - rfc_sc->sc_bytesleft
    875 			    + rfc_sc->sc_curbuf->b_blkno * DEV_BSIZE) /
    876 			    ((rf_sc->sc_state & RFS_DENS) == 0
    877 			    ? RX2_BYTE_SD : RX2_BYTE_DD);
    878 			if (i > RX2_TRACKS * RX2_SECTORS) {
    879 				rfc_sc->sc_curbuf->b_error = EIO;
    880 				break;
    881 			}
    882 			disk_busy(&rf_sc->sc_disk);
    883 			if (rfc_sendcmd(rfc_sc, RX2CS_WSEC | RX2CS_IE
    884 			    | (rf_sc->sc_dnum == 0 ? 0 : RX2CS_US)
    885 			    | ((rf_sc->sc_state& RFS_DENS) == 0 ? 0 : RX2CS_DD),
    886 			    i % RX2_SECTORS + 1, i / RX2_SECTORS) < 0) {
    887 				disk_unbusy(&rf_sc->sc_disk, 0, 0);
    888 				rfc_sc->sc_curbuf->b_error = EIO;
    889 			}
    890 			break;
    891 		case RFS_RSEC:	/* Read Sector */
    892 			i = (rfc_sc->sc_curbuf->b_bcount - rfc_sc->sc_bytesleft
    893 			    + rfc_sc->sc_curbuf->b_blkno * DEV_BSIZE) /
    894 			    ((rf_sc->sc_state & RFS_DENS) == 0
    895 			    ? RX2_BYTE_SD : RX2_BYTE_DD);
    896 			if (i > RX2_TRACKS * RX2_SECTORS) {
    897 				rfc_sc->sc_curbuf->b_error = EIO;
    898 				break;
    899 			}
    900 			disk_busy(&rf_sc->sc_disk);
    901 			if (rfc_sendcmd(rfc_sc, RX2CS_RSEC | RX2CS_IE
    902 			    | (rf_sc->sc_dnum == 0 ? 0 : RX2CS_US)
    903 			    | ((rf_sc->sc_state& RFS_DENS) == 0 ? 0 : RX2CS_DD),
    904 			    i % RX2_SECTORS + 1, i / RX2_SECTORS) < 0) {
    905 				disk_unbusy(&rf_sc->sc_disk, 0, 1);
    906 				rfc_sc->sc_curbuf->b_error = EIO;
    907 			}
    908 			break;
    909 		case RFS_NOTINIT: /* Device is not open */
    910 		case RFS_PROBING: /* density detect / verify started */
    911 		case RFS_IDLE:	/* controller is idle */
    912 		case RFS_SMD:	/* Set Media Density */
    913 		case RFS_RSTAT:	/* Read Status */
    914 		case RFS_WDDS:	/* Write Deleted Data Sector */
    915 		case RFS_REC:	/* Read Error Code */
    916 		default:
    917 			panic("Impossible state in rfc_intr(2): 0x%x\n",
    918 			    rf_sc->sc_state & RFS_CMDS);
    919 		}
    920 
    921 		if (rfc_sc->sc_curbuf->b_error != 0) {
    922 			/*
    923 			 * An error occurred while processing this buffer.
    924 			 * Finish it and try to get a new buffer to process.
    925 			 * Return if there are no buffers in the queues.
    926 			 * This loops until the queues are empty or a new
    927 			 * action was successfully scheduled.
    928 			 */
    929 			rfc_sc->sc_curbuf->b_resid = rfc_sc->sc_bytesleft;
    930 			rfc_sc->sc_curbuf->b_error = EIO;
    931 			biodone(rfc_sc->sc_curbuf);
    932 			rf_sc = get_new_buf( rfc_sc);
    933 			if (rf_sc == NULL)
    934 				return;
    935 			continue;
    936 		}
    937 		break;
    938 	}
    939 	return;
    940 }
    941 
    942 
    943 
    944 int
    945 rfdump(dev_t dev, daddr_t blkno, void *va, size_t size)
    946 {
    947 
    948 	/* A 0.5MB floppy is much to small to take a system dump... */
    949 	return(ENXIO);
    950 }
    951 
    952 
    953 
    954 int
    955 rfsize(dev_t dev)
    956 {
    957 
    958 	return(-1);
    959 }
    960 
    961 
    962 
    963 int
    964 rfopen(dev_t dev, int oflags, int devtype, struct lwp *l)
    965 {
    966 	struct rf_softc *rf_sc;
    967 	struct rfc_softc *rfc_sc;
    968 	struct disklabel *dl;
    969 
    970 	if ((rf_sc = device_lookup_private(&rf_cd, DISKUNIT(dev))) == NULL)
    971 		return ENXIO;
    972 
    973 	rfc_sc = rf_sc->sc_rfc;
    974 	dl = rf_sc->sc_disk.dk_label;
    975 	switch (DISKPART(dev)) {
    976 		case 0:			/* Part. a is single density. */
    977 			/* opening in single and double density is senseless */
    978 			if ((rf_sc->sc_state & RFS_OPEN_B) != 0 )
    979 				return(ENXIO);
    980 			rf_sc->sc_state &= ~RFS_DENS;
    981 			rf_sc->sc_state &= ~RFS_AD;
    982 			rf_sc->sc_state |= RFS_OPEN_A;
    983 		break;
    984 		case 1:			/* Part. b is double density. */
    985 			/*
    986 			 * Opening a single density only drive in double
    987 			 * density or simultaneous opening in single and
    988 			 * double density is senseless.
    989 			 */
    990 			if (rfc_sc->type == 1
    991 			    || (rf_sc->sc_state & RFS_OPEN_A) != 0 )
    992 				return(ENXIO);
    993 			rf_sc->sc_state |= RFS_DENS;
    994 			rf_sc->sc_state &= ~RFS_AD;
    995 			rf_sc->sc_state |= RFS_OPEN_B;
    996 		break;
    997 		case 2:			/* Part. c is auto density. */
    998 			rf_sc->sc_state |= RFS_AD;
    999 			rf_sc->sc_state |= RFS_OPEN_C;
   1000 		break;
   1001 		default:
   1002 			return(ENXIO);
   1003 		break;
   1004 	}
   1005 	if ((rf_sc->sc_state & RFS_CMDS) == RFS_NOTINIT) {
   1006 		rfc_sc->sc_curchild = rf_sc->sc_dnum;
   1007 		/*
   1008 		 * Controller is idle and density is not detected.
   1009 		 * Start a density probe by issuing a read sector command
   1010 		 * and sleep until the density probe finished.
   1011 		 * Due to this it is imposible to open unformatted media.
   1012 		 * As the RX02/02 is not able to format its own media,
   1013 		 * media must be purchased preformatted. fsck DEC makreting!
   1014 		 */
   1015 		RFS_SETCMD(rf_sc->sc_state, RFS_PROBING);
   1016 		disk_busy(&rf_sc->sc_disk);
   1017 		if (rfc_sendcmd(rfc_sc, RX2CS_RSEC | RX2CS_IE
   1018 		    | (rf_sc->sc_dnum == 0 ? 0 : RX2CS_US)
   1019 		    | ((rf_sc->sc_state & RFS_DENS) == 0 ? 0 : RX2CS_DD),
   1020 		    1, 1) < 0) {
   1021 			rf_sc->sc_state = 0;
   1022 			return(ENXIO);
   1023 		}
   1024 		/* wait max. 2 sec for density probe to finish */
   1025 		if (tsleep(rf_sc, PRIBIO | PCATCH, "density probe", 2 * hz)
   1026 		    != 0 || (rf_sc->sc_state & RFS_CMDS) == RFS_NOTINIT) {
   1027 			/* timeout elapsed and / or something went wrong */
   1028 			rf_sc->sc_state = 0;
   1029 			return(ENXIO);
   1030 		}
   1031 	}
   1032 	/* disklabel. We use different fake geometries for SD and DD. */
   1033 	if ((rf_sc->sc_state & RFS_DENS) == 0) {
   1034 		dl->d_nsectors = 10;		/* sectors per track */
   1035 		dl->d_secpercyl = 10;		/* sectors per cylinder */
   1036 		dl->d_ncylinders = 50;		/* cylinders per unit */
   1037 		dl->d_secperunit = 501; /* sectors per unit */
   1038 		/* number of sectors in partition */
   1039 		dl->d_partitions[2].p_size = 500;
   1040 	} else {
   1041 		dl->d_nsectors = RX2_SECTORS / 2;  /* sectors per track */
   1042 		dl->d_secpercyl = RX2_SECTORS / 2; /* sectors per cylinder */
   1043 		dl->d_ncylinders = RX2_TRACKS;	   /* cylinders per unit */
   1044 		/* sectors per unit */
   1045 		dl->d_secperunit = RX2_SECTORS * RX2_TRACKS / 2;
   1046 		/* number of sectors in partition */
   1047 		dl->d_partitions[2].p_size = RX2_SECTORS * RX2_TRACKS / 2;
   1048 	}
   1049 	return(0);
   1050 }
   1051 
   1052 
   1053 
   1054 int
   1055 rfclose(dev_t dev, int fflag, int devtype, struct lwp *l)
   1056 {
   1057 	struct rf_softc *rf_sc = device_lookup_private(&rf_cd, DISKUNIT(dev));
   1058 
   1059 	if ((rf_sc->sc_state & 1 << (DISKPART(dev) + RFS_OPEN_SHIFT)) == 0)
   1060 		panic("rfclose: can not close non-open drive %s "
   1061 		    "partition %"PRIu32, device_xname(rf_sc->sc_dev), DISKPART(dev));
   1062 	else
   1063 		rf_sc->sc_state &= ~(1 << (DISKPART(dev) + RFS_OPEN_SHIFT));
   1064 	if ((rf_sc->sc_state & RFS_OPEN_MASK) == 0)
   1065 		rf_sc->sc_state = 0;
   1066 	return(0);
   1067 }
   1068 
   1069 
   1070 
   1071 int
   1072 rfread(dev_t dev, struct uio *uio, int ioflag)
   1073 {
   1074 
   1075 	return(physio(rfstrategy, NULL, dev, B_READ, minphys, uio));
   1076 }
   1077 
   1078 
   1079 
   1080 int
   1081 rfwrite(dev_t dev, struct uio *uio, int ioflag)
   1082 {
   1083 
   1084 	return(physio(rfstrategy, NULL, dev, B_WRITE, minphys, uio));
   1085 }
   1086 
   1087 
   1088 
   1089 int
   1090 rfioctl(dev_t dev, u_long cmd, void *data, int fflag, struct lwp *l)
   1091 {
   1092 	struct rf_softc *rf_sc = device_lookup_private(&rf_cd, DISKUNIT(dev));
   1093 	int error;
   1094 
   1095 	/* We are going to operate on a non-open dev? PANIC! */
   1096 	if ((rf_sc->sc_state & 1 << (DISKPART(dev) + RFS_OPEN_SHIFT)) == 0)
   1097 		panic("rfioctl: can not operate on non-open drive %s "
   1098 		    "partition %"PRIu32, device_xname(rf_sc->sc_dev), DISKPART(dev));
   1099 	error = disk_ioctl(&rf_sc->sc_disk, dev, cmd, data, fflag, l);
   1100 	if (error != EPASSTHROUGH)
   1101 		return error;
   1102 
   1103 	switch (cmd) {
   1104 	/* get and set disklabel; DIOCGPART used internally */
   1105 	case DIOCSDINFO: /* set */
   1106 		return(0);
   1107 	case DIOCWDINFO: /* set, update disk */
   1108 		return(0);
   1109 	/* do format operation, read or write */
   1110 	case DIOCRFORMAT:
   1111 	break;
   1112 	case DIOCWFORMAT:
   1113 	break;
   1114 
   1115 	case DIOCSSTEP: /* set step rate */
   1116 	break;
   1117 	case DIOCSRETRIES: /* set # of retries */
   1118 	break;
   1119 	case DIOCKLABEL: /* keep/drop label on close? */
   1120 	break;
   1121 	case DIOCWLABEL: /* write en/disable label */
   1122 	break;
   1123 
   1124 /*	case DIOCSBAD: / * set kernel dkbad */
   1125 	break; /* */
   1126 	case DIOCEJECT: /* eject removable disk */
   1127 	break;
   1128 	case ODIOCEJECT: /* eject removable disk */
   1129 	break;
   1130 	case DIOCLOCK: /* lock/unlock pack */
   1131 	break;
   1132 
   1133 	/* get default label, clear label */
   1134 	case DIOCGDEFLABEL:
   1135 	break;
   1136 	case DIOCCLRLABEL:
   1137 	break;
   1138 	default:
   1139 		return(ENOTTY);
   1140 	}
   1141 
   1142 	return(ENOTTY);
   1143 }
   1144