#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fd.c,v 1.53 2025/12/20 10:51:01 skrll Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/device.h>
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/uio.h>
#include <sys/syslog.h>
#include <sys/queue.h>
#include <uvm/uvm_extern.h>
#include <dev/cons.h>
#include <sys/bus.h>
#include <machine/cpu.h>
#include <arc/jazz/fdreg.h>
#include <arc/jazz/fdcvar.h>
#include "ioconf.h"
#include "locators.h"
#define FDUNIT(dev) DISKUNIT(dev)
#define FDTYPE(dev) DISKPART(dev)
static int fdprint(void *, const char *);
struct fd_type {
int sectrac;
int heads;
int seccyl;
int secsize;
int datalen;
int steprate;
int gap1;
int gap2;
int cyls;
int size;
int step;
int rate;
const char *name;
};
const static struct fd_type fd_types[] = {
{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,"1.44MB" },
{ 15,2,30,2,0xff,0xdf,0x1b,0x54,80,2400,1,FDC_500KBPS, "1.2MB" },
{ 9,2,18,2,0xff,0xdf,0x23,0x50,40, 720,2,FDC_300KBPS, "360KB/AT" },
{ 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,1,FDC_250KBPS, "360KB/PC" },
{ 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS, "720KB" },
{ 9,2,18,2,0xff,0xdf,0x23,0x50,80,1440,1,FDC_300KBPS, "720KB/x" },
{ 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS, "360KB/x" },
};
struct fd_softc {
device_t sc_dev;
struct disk sc_dk;
const struct fd_type *sc_deftype;
struct fd_type *sc_type;
struct fd_type sc_type_copy;
struct callout sc_motoron_ch;
struct callout sc_motoroff_ch;
daddr_t sc_blkno;
int sc_bcount;
int sc_opts;
int sc_skip;
int sc_nblks;
int sc_nbytes;
int sc_drive;
int sc_flags;
#define FD_OPEN 0x01
#define FD_MOTOR 0x02
#define FD_MOTOR_WAIT 0x04
int sc_cylin;
TAILQ_ENTRY(fd_softc) sc_drivechain;
int sc_ops;
struct bufq_state *sc_q;
int sc_active;
};
static int fdprobe(device_t, cfdata_t, void *);
static void fdattach(device_t, device_t, void *);
CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc), fdprobe, fdattach, NULL, NULL);
dev_type_open(fdopen);
dev_type_close(fdclose);
dev_type_read(fdread);
dev_type_write(fdwrite);
dev_type_ioctl(fdioctl);
dev_type_strategy(fdstrategy);
const struct bdevsw fd_bdevsw = {
.d_open = fdopen,
.d_close = fdclose,
.d_strategy = fdstrategy,
.d_ioctl = fdioctl,
.d_dump = nodump,
.d_psize = nosize,
.d_discard = nodiscard,
.d_flag = D_DISK
};
const struct cdevsw fd_cdevsw = {
.d_open = fdopen,
.d_close = fdclose,
.d_read = fdread,
.d_write = fdwrite,
.d_ioctl = fdioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_DISK
};
static void fdstart(struct fd_softc *);
struct dkdriver fddkdriver = {
.d_strategy = fdstrategy
};
static bool fd_shutdown(device_t, int);
#if 0
static const struct fd_type *fd_nvtotype(char *, int, int);
#endif
static void fd_set_motor(struct fdc_softc *, int);
static void fd_motor_off(void *);
static void fd_motor_on(void *);
static int fdcresult(struct fdc_softc *);
static void fdcstart(struct fdc_softc *);
static void fdcstatus(device_t, int, const char *);
static void fdctimeout(void *);
static void fdcpseudointr(void *);
static void fdcretry(struct fdc_softc *);
static void fdfinish(struct fd_softc *, struct buf *);
static const struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
static void fd_mountroot_hook(device_t);
struct fdc_attach_args {
int fa_drive;
const struct fd_type *fa_deftype;
};
static int
fdprint(void *aux, const char *fdc)
{
struct fdc_attach_args *fa = aux;
if (fdc == NULL)
aprint_normal(" drive %d", fa->fa_drive);
return QUIET;
}
void
fdcattach(struct fdc_softc *fdc)
{
struct fdc_attach_args fa;
int type;
callout_init(&fdc->sc_timo_ch, 0);
callout_init(&fdc->sc_intr_ch, 0);
fdc->sc_state = DEVIDLE;
TAILQ_INIT(&fdc->sc_drives);
type = 0;
for (fa.fa_drive = 0; fa.fa_drive < 2; fa.fa_drive++) {
fa.fa_deftype = &fd_types[type];
(void)config_found(fdc->sc_dev, (void *)&fa, fdprint,
CFARGS_NONE);
}
}
static int
fdprobe(device_t parent, cfdata_t cf , void *aux)
{
struct fdc_softc *fdc = device_private(parent);
struct fdc_attach_args *fa = aux;
int drive = fa->fa_drive;
bus_space_tag_t iot = fdc->sc_iot;
bus_space_handle_t ioh = fdc->sc_ioh;
int n;
if (cf->cf_loc[FDCCF_DRIVE] != FDCCF_DRIVE_DEFAULT &&
cf->cf_loc[FDCCF_DRIVE] != drive)
return 0;
bus_space_write_1(iot, ioh, FDOUT, drive | FDO_FRST | FDO_MOEN(drive));
delay(250000);
out_fdc(iot, ioh, NE7CMD_RECAL);
out_fdc(iot, ioh, drive);
delay(2000000);
out_fdc(iot, ioh, NE7CMD_SENSEI);
n = fdcresult(fdc);
#ifdef FD_DEBUG
{
int i;
aprint_debug("%s: status", __func__);
for (i = 0; i < n; i++)
aprint_debug(" %x", fdc->sc_status[i]);
aprint_debug("\n");
}
#endif
if (n != 2 || (fdc->sc_status[0] & 0xf8) != 0x20)
return 0;
bus_space_write_1(iot, ioh, FDOUT, FDO_FRST);
return 1;
}
void
fdattach(device_t parent, device_t self, void *aux)
{
struct fdc_softc *fdc = device_private(parent);
struct fd_softc *fd = device_private(self);
struct fdc_attach_args *fa = aux;
const struct fd_type *type = fa->fa_deftype;
int drive = fa->fa_drive;
fd->sc_dev = self;
callout_init(&fd->sc_motoron_ch, 0);
callout_init(&fd->sc_motoroff_ch, 0);
if (type)
printf(": %s, %d cyl, %d head, %d sec\n", type->name,
type->cyls, type->heads, type->sectrac);
else
printf(": density unknown\n");
bufq_alloc(&fd->sc_q, "disksort", BUFQ_SORT_CYLINDER);
fd->sc_cylin = -1;
fd->sc_drive = drive;
fd->sc_deftype = type;
fdc->sc_fd[drive] = fd;
disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
disk_attach(&fd->sc_dk);
mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
if (!pmf_device_register1(self, NULL, NULL, fd_shutdown))
aprint_error_dev(self, "couldn't establish power handler\n");
}
bool
fd_shutdown(device_t self, int howto)
{
struct fd_softc *fd;
fd = device_private(self);
fd_motor_off(fd);
return true;
}
#if 0
static const struct fd_type *
fd_nvtotype(char *fdc, int nvraminfo, int drive)
{
int type;
type = (drive == 0 ? nvraminfo : nvraminfo << 4) & 0xf0;
#if 0
switch (type) {
case NVRAM_DISKETTE_NONE:
return NULL;
case NVRAM_DISKETTE_12M:
return &fd_types[1];
case NVRAM_DISKETTE_TYPE5:
case NVRAM_DISKETTE_TYPE6:
case NVRAM_DISKETTE_144M:
return &fd_types[0];
case NVRAM_DISKETTE_360K:
return &fd_types[3];
case NVRAM_DISKETTE_720K:
return &fd_types[4];
default:
printf("%s: drive %d: unknown device type 0x%x\n",
fdc, drive, type);
return NULL;
}
#else
return &fd_types[0];
#endif
}
#endif
static const struct fd_type *
fd_dev_to_type(struct fd_softc *fd, dev_t dev)
{
int type = FDTYPE(dev);
if (type > __arraycount(fd_types))
return NULL;
return type ? &fd_types[type - 1] : fd->sc_deftype;
}
void
fdstrategy(struct buf *bp)
{
struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev));
int sz;
int s;
if (bp->b_blkno < 0 ||
(bp->b_bcount % FDC_BSIZE) != 0) {
bp->b_error = EINVAL;
goto done;
}
if (bp->b_bcount == 0)
goto done;
sz = howmany(bp->b_bcount, FDC_BSIZE);
if (bp->b_blkno + sz > fd->sc_type->size) {
sz = fd->sc_type->size - bp->b_blkno;
if (sz == 0) {
goto done;
}
if (sz < 0) {
bp->b_error = EINVAL;
goto done;
}
bp->b_bcount = sz << DEV_BSHIFT;
}
bp->b_rawblkno = bp->b_blkno;
bp->b_cylinder =
bp->b_blkno / (FDC_BSIZE / DEV_BSIZE) / fd->sc_type->seccyl;
#ifdef FD_DEBUG
printf("%s: b_blkno %" PRId64 " b_bcount %d blkno %" PRId64
" cylin %d sz %d\n", __func__,
bp->b_blkno, bp->b_bcount, fd->sc_blkno, bp->b_cylinder, sz);
#endif
s = splbio();
bufq_put(fd->sc_q, bp);
callout_stop(&fd->sc_motoroff_ch);
if (fd->sc_active == 0)
fdstart(fd);
#ifdef DIAGNOSTIC
else {
struct fdc_softc *fdc =
device_private(device_parent(fd->sc_dev));
if (fdc->sc_state == DEVIDLE) {
printf("%s: controller inactive\n", __func__);
fdcstart(fdc);
}
}
#endif
splx(s);
return;
done:
bp->b_resid = bp->b_bcount;
biodone(bp);
}
void
fdstart(struct fd_softc *fd)
{
struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
int active = TAILQ_FIRST(&fdc->sc_drives) != 0;
fd->sc_active = 1;
TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
if (!active)
fdcstart(fdc);
}
void
fdfinish(struct fd_softc *fd, struct buf *bp)
{
struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
(void)bufq_get(fd->sc_q);
if (TAILQ_NEXT(fd, sc_drivechain) && ++fd->sc_ops >= 8) {
fd->sc_ops = 0;
TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
if (bufq_peek(fd->sc_q) != NULL)
TAILQ_INSERT_TAIL(&fdc->sc_drives, fd, sc_drivechain);
else
fd->sc_active = 0;
}
bp->b_resid = fd->sc_bcount;
fd->sc_skip = 0;
biodone(bp);
callout_reset(&fd->sc_motoroff_ch, 5 * hz, fd_motor_off, fd);
fdc->sc_state = DEVIDLE;
}
int
fdread(dev_t dev, struct uio *uio, int flags)
{
return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
}
int
fdwrite(dev_t dev, struct uio *uio, int flags)
{
return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
}
void
fd_set_motor(struct fdc_softc *fdc, int reset)
{
struct fd_softc *fd;
u_char status;
int n;
if ((fd = TAILQ_FIRST(&fdc->sc_drives)) != NULL)
status = fd->sc_drive;
else
status = 0;
if (!reset)
status |= FDO_FRST | FDO_FDMAEN;
for (n = 0; n < 4; n++)
if ((fd = fdc->sc_fd[n]) && (fd->sc_flags & FD_MOTOR))
status |= FDO_MOEN(n);
bus_space_write_1(fdc->sc_iot, fdc->sc_ioh, FDOUT, status);
}
void
fd_motor_off(void *arg)
{
struct fd_softc *fd = arg;
struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
int s;
s = splbio();
fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
fd_set_motor(fdc, 0);
splx(s);
}
void
fd_motor_on(void *arg)
{
struct fd_softc *fd = arg;
struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
int s;
s = splbio();
fd->sc_flags &= ~FD_MOTOR_WAIT;
if ((TAILQ_FIRST(&fdc->sc_drives) == fd) &&
(fdc->sc_state == MOTORWAIT))
(void)fdcintr(fdc);
splx(s);
}
int
fdcresult(struct fdc_softc *fdc)
{
bus_space_tag_t iot = fdc->sc_iot;
bus_space_handle_t ioh = fdc->sc_ioh;
u_char i;
int j, n = 0;
for (j = 100000; j; j--) {
i = bus_space_read_1(iot, ioh, FDSTS) &
(NE7_DIO | NE7_RQM | NE7_CB);
if (i == NE7_RQM)
return n;
if (i == (NE7_DIO | NE7_RQM | NE7_CB)) {
if (n >= sizeof(fdc->sc_status)) {
log(LOG_ERR, "%s: overrun\n", __func__);
return -1;
}
fdc->sc_status[n++] =
bus_space_read_1(iot, ioh, FDDATA);
}
delay(10);
}
log(LOG_ERR, "%s: timeout\n", __func__);
return -1;
}
int
out_fdc(bus_space_tag_t iot, bus_space_handle_t ioh, uint8_t x)
{
int i = 100000;
while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_DIO) && i-- > 0);
if (i <= 0)
return -1;
while ((bus_space_read_1(iot, ioh, FDSTS) & NE7_RQM) == 0 && i-- > 0);
if (i <= 0)
return -1;
bus_space_write_1(iot, ioh, FDDATA, x);
return 0;
}
int
fdopen(dev_t dev, int flags, int mode, struct lwp *l)
{
struct fd_softc *fd;
const struct fd_type *type;
fd = device_lookup_private(&fd_cd, FDUNIT(dev));
if (fd == NULL)
return ENXIO;
type = fd_dev_to_type(fd, dev);
if (type == NULL)
return ENXIO;
if ((fd->sc_flags & FD_OPEN) != 0 &&
memcmp(fd->sc_type, type, sizeof(*type)))
return EBUSY;
fd->sc_type_copy = *type;
fd->sc_type = &fd->sc_type_copy;
fd->sc_cylin = -1;
fd->sc_flags |= FD_OPEN;
return 0;
}
int
fdclose(dev_t dev, int flags, int mode, struct lwp *l)
{
struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
fd->sc_flags &= ~FD_OPEN;
return 0;
}
void
fdcstart(struct fdc_softc *fdc)
{
#ifdef DIAGNOSTIC
if (fdc->sc_state != DEVIDLE) {
printf("%s: not idle\n", __func__);
return;
}
#endif
(void)fdcintr(fdc);
}
static void
fdcpstatus(int n, struct fdc_softc *fdc)
{
char bits[64];
switch (n) {
case 0:
printf("\n");
break;
case 2:
snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
printf(" (st0 %s cyl %d)\n", bits, fdc->sc_status[1]);
break;
case 7:
snprintb(bits, sizeof(bits), NE7_ST0BITS, fdc->sc_status[0]);
printf(" (st0 %s", bits);
snprintb(bits, sizeof(bits), NE7_ST1BITS, fdc->sc_status[1]);
printf(" st1 %s", bits);
snprintb(bits, sizeof(bits), NE7_ST2BITS, fdc->sc_status[2]);
printf(" st2 %s", bits);
printf(" cyl %d head %d sec %d)\n",
fdc->sc_status[3], fdc->sc_status[4], fdc->sc_status[5]);
break;
#ifdef DIAGNOSTIC
default:
printf("\nfdcstatus: weird size");
break;
#endif
}
}
void
fdcstatus(device_t dev, int n, const char *s)
{
struct fdc_softc *fdc = device_private(device_parent(dev));
if (n == 0) {
out_fdc(fdc->sc_iot, fdc->sc_ioh, NE7CMD_SENSEI);
(void)fdcresult(fdc);
n = 2;
}
printf("%s: %s", device_xname(dev), s);
fdcpstatus(n, fdc);
}
void
fdctimeout(void *arg)
{
struct fdc_softc *fdc = arg;
struct fd_softc *fd = TAILQ_FIRST(&fdc->sc_drives);
int s;
s = splbio();
#ifdef DEBUG
log(LOG_ERR, "%s: state %d\n", __func__, fdc->sc_state);
#endif
fdcstatus(fd->sc_dev, 0, "timeout");
if (bufq_peek(fd->sc_q) != NULL)
fdc->sc_state++;
else
fdc->sc_state = DEVIDLE;
(void)fdcintr(fdc);
splx(s);
}
void
fdcpseudointr(void *arg)
{
int s;
s = splbio();
(void)fdcintr(arg);
splx(s);
}
int
fdcintr(void *arg)
{
struct fdc_softc *fdc = arg;
#define st0 fdc->sc_status[0]
#define cyl fdc->sc_status[1]
struct fd_softc *fd;
struct buf *bp;
bus_space_tag_t iot = fdc->sc_iot;
bus_space_handle_t ioh = fdc->sc_ioh;
int read, head, sec, i, nblks;
struct fd_type *type;
loop:
fd = TAILQ_FIRST(&fdc->sc_drives);
if (fd == NULL) {
fdc->sc_state = DEVIDLE;
return 1;
}
bp = bufq_peek(fd->sc_q);
if (bp == NULL) {
fd->sc_ops = 0;
TAILQ_REMOVE(&fdc->sc_drives, fd, sc_drivechain);
fd->sc_active = 0;
goto loop;
}
switch (fdc->sc_state) {
case DEVIDLE:
fdc->sc_errors = 0;
fd->sc_skip = 0;
fd->sc_bcount = bp->b_bcount;
fd->sc_blkno = bp->b_blkno / (FDC_BSIZE / DEV_BSIZE);
callout_stop(&fd->sc_motoroff_ch);
if ((fd->sc_flags & FD_MOTOR_WAIT) != 0) {
fdc->sc_state = MOTORWAIT;
return 1;
}
if ((fd->sc_flags & FD_MOTOR) == 0) {
struct fd_softc *ofd = fdc->sc_fd[fd->sc_drive ^ 1];
if (ofd && ofd->sc_flags & FD_MOTOR) {
callout_stop(&ofd->sc_motoroff_ch);
ofd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
}
fd->sc_flags |= FD_MOTOR | FD_MOTOR_WAIT;
fd_set_motor(fdc, 0);
fdc->sc_state = MOTORWAIT;
callout_reset(&fd->sc_motoron_ch, hz / 4,
fd_motor_on, fd);
return 1;
}
fd_set_motor(fdc, 0);
case DOSEEK:
doseek:
if (fd->sc_cylin == bp->b_cylinder)
goto doio;
out_fdc(iot, ioh, NE7CMD_SPECIFY);
out_fdc(iot, ioh, fd->sc_type->steprate);
out_fdc(iot, ioh, 6);
out_fdc(iot, ioh, NE7CMD_SEEK);
out_fdc(iot, ioh, fd->sc_drive);
out_fdc(iot, ioh, bp->b_cylinder * fd->sc_type->step);
fd->sc_cylin = -1;
fdc->sc_state = SEEKWAIT;
iostat_seek(fd->sc_dk.dk_stats);
disk_busy(&fd->sc_dk);
callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
return 1;
case DOIO:
doio:
type = fd->sc_type;
sec = fd->sc_blkno % type->seccyl;
nblks = type->seccyl - sec;
nblks = uimin(nblks, fd->sc_bcount / FDC_BSIZE);
nblks = uimin(nblks, fdc->sc_maxiosize / FDC_BSIZE);
fd->sc_nblks = nblks;
fd->sc_nbytes = nblks * FDC_BSIZE;
head = sec / type->sectrac;
sec -= head * type->sectrac;
#ifdef DIAGNOSTIC
{
int block;
block = (fd->sc_cylin * type->heads + head) *
type->sectrac + sec;
if (block != fd->sc_blkno) {
printf("%s: block %d != blkno %" PRId64
"\n", __func__, block, fd->sc_blkno);
#ifdef DDB
Debugger();
#endif
}
}
#endif
read = (bp->b_flags & B_READ) != 0;
FDCDMA_START(fdc, (uint8_t *)bp->b_data + fd->sc_skip,
fd->sc_nbytes, read);
bus_space_write_1(iot, ioh, FDCTL, type->rate);
#ifdef FD_DEBUG
printf("%s: %s drive %d track %d head %d sec %d nblks %d\n",
__func__, read ? "read" : "write", fd->sc_drive,
fd->sc_cylin, head, sec, nblks);
#endif
if (read)
out_fdc(iot, ioh, NE7CMD_READ);
else
out_fdc(iot, ioh, NE7CMD_WRITE);
out_fdc(iot, ioh, (head << 2) | fd->sc_drive);
out_fdc(iot, ioh, fd->sc_cylin);
out_fdc(iot, ioh, head);
out_fdc(iot, ioh, sec + 1);
out_fdc(iot, ioh, type->secsize);
out_fdc(iot, ioh, type->sectrac);
out_fdc(iot, ioh, type->gap1);
out_fdc(iot, ioh, type->datalen);
fdc->sc_state = IOCOMPLETE;
disk_busy(&fd->sc_dk);
callout_reset(&fdc->sc_timo_ch, 2 * hz, fdctimeout, fdc);
return 1;
case SEEKWAIT:
callout_stop(&fdc->sc_timo_ch);
fdc->sc_state = SEEKCOMPLETE;
callout_reset(&fdc->sc_intr_ch, hz / 50, fdcpseudointr, fdc);
return 1;
case SEEKCOMPLETE:
disk_unbusy(&fd->sc_dk, 0, 0);
out_fdc(iot, ioh, NE7CMD_SENSEI);
if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 ||
cyl != bp->b_cylinder * fd->sc_type->step) {
#ifdef FD_DEBUG
fdcstatus(fd->sc_dev, 2, "seek failed");
#endif
fdcretry(fdc);
goto loop;
}
fd->sc_cylin = bp->b_cylinder;
goto doio;
case IOTIMEDOUT:
FDCDMA_ABORT(fdc);
case SEEKTIMEDOUT:
case RECALTIMEDOUT:
case RESETTIMEDOUT:
fdcretry(fdc);
goto loop;
case IOCOMPLETE:
callout_stop(&fdc->sc_timo_ch);
disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
(bp->b_flags & B_READ));
i = fdcresult(fdc);
if (i != 7 || (st0 & 0xf8) != 0) {
FDCDMA_ABORT(fdc);
#ifdef FD_DEBUG
fdcstatus(fd->sc_dev, 7, bp->b_flags & B_READ ?
"read failed" : "write failed");
printf("blkno %" PRId64 " nblks %d\n",
fd->sc_blkno, fd->sc_nblks);
#endif
fdcretry(fdc);
goto loop;
}
FDCDMA_DONE(fdc);
if (fdc->sc_errors) {
diskerr(bp, "fd", "soft error (corrected)", LOG_PRINTF,
fd->sc_skip / FDC_BSIZE, NULL);
printf("\n");
fdc->sc_errors = 0;
}
fd->sc_blkno += fd->sc_nblks;
fd->sc_skip += fd->sc_nbytes;
fd->sc_bcount -= fd->sc_nbytes;
if (fd->sc_bcount > 0) {
bp->b_cylinder = fd->sc_blkno / fd->sc_type->seccyl;
goto doseek;
}
fdfinish(fd, bp);
goto loop;
case DORESET:
fd_set_motor(fdc, 1);
delay(100);
fd_set_motor(fdc, 0);
fdc->sc_state = RESETCOMPLETE;
callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
return 1;
case RESETCOMPLETE:
callout_stop(&fdc->sc_timo_ch);
for (i = 0; i < 4; i++) {
out_fdc(iot, ioh, NE7CMD_SENSEI);
(void)fdcresult(fdc);
}
case DORECAL:
out_fdc(iot, ioh, NE7CMD_RECAL);
out_fdc(iot, ioh, fd->sc_drive);
fdc->sc_state = RECALWAIT;
callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
return 1;
case RECALWAIT:
callout_stop(&fdc->sc_timo_ch);
fdc->sc_state = RECALCOMPLETE;
callout_reset(&fdc->sc_intr_ch, hz / 30, fdcpseudointr, fdc);
return 1;
case RECALCOMPLETE:
out_fdc(iot, ioh, NE7CMD_SENSEI);
if (fdcresult(fdc) != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
#ifdef FD_DEBUG
fdcstatus(fd->sc_dev, 2, "recalibrate failed");
#endif
fdcretry(fdc);
goto loop;
}
fd->sc_cylin = 0;
goto doseek;
case MOTORWAIT:
if (fd->sc_flags & FD_MOTOR_WAIT)
return 1;
goto doseek;
default:
fdcstatus(fd->sc_dev, 0, "stray interrupt");
return 1;
}
#ifdef DIAGNOSTIC
panic("%s: impossible", __func__);
#endif
#undef st0
#undef cyl
}
void
fdcretry(struct fdc_softc *fdc)
{
struct fd_softc *fd;
struct buf *bp;
fd = TAILQ_FIRST(&fdc->sc_drives);
bp = bufq_peek(fd->sc_q);
switch (fdc->sc_errors) {
case 0:
fdc->sc_state = DOSEEK;
break;
case 1:
case 2:
case 3:
fdc->sc_state = DORECAL;
break;
case 4:
fdc->sc_state = DORESET;
break;
default:
diskerr(bp, "fd", "hard error", LOG_PRINTF,
fd->sc_skip / FDC_BSIZE, (struct disklabel *)NULL);
fdcpstatus(7, fdc);
bp->b_error = EIO;
fdfinish(fd, bp);
}
fdc->sc_errors++;
}
int
fdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
{
struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
struct disklabel buffer;
int error;
switch (cmd) {
case DIOCGDINFO:
memset(&buffer, 0, sizeof(buffer));
buffer.d_secpercyl = fd->sc_type->seccyl;
buffer.d_type = DKTYPE_FLOPPY;
buffer.d_secsize = FDC_BSIZE;
if (readdisklabel(dev, fdstrategy, &buffer, NULL) != NULL)
return EINVAL;
*(struct disklabel *)addr = buffer;
return 0;
case DIOCWLABEL:
if ((flag & FWRITE) == 0)
return EBADF;
return 0;
case DIOCWDINFO:
if ((flag & FWRITE) == 0)
return EBADF;
error = setdisklabel(&buffer, (struct disklabel *)addr,
0, NULL);
if (error)
return error;
error = writedisklabel(dev, fdstrategy, &buffer, NULL);
return error;
default:
return ENOTTY;
}
#ifdef DIAGNOSTIC
panic("%s: impossible", __func__);
#endif
}
void
fd_mountroot_hook(device_t dev)
{
int c;
printf("Insert filesystem floppy and press return.");
cnpollc(true);
for (;;) {
c = cngetc();
if ((c == '\r') || (c == '\n')) {
printf("\n");
break;
}
}
cnpollc(false);
}