#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: fdc.c,v 1.52 2023/09/02 05:51:57 jdc Exp $");
#include "opt_ddb.h"
#include "opt_md.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/systm.h>
#include <sys/callout.h>
#include <sys/kernel.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/device.h>
#include <sys/disklabel.h>
#include <sys/disk.h>
#include <sys/fdio.h>
#include <sys/buf.h>
#include <sys/bufq.h>
#include <sys/kmem.h>
#include <sys/proc.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <sys/queue.h>
#include <sys/conf.h>
#include <sys/intr.h>
#include <dev/cons.h>
#include <uvm/uvm_extern.h>
#include <machine/autoconf.h>
#ifdef SUN4
#include <sparc/sparc/auxreg.h>
#include <sparc/dev/fdreg.h>
#include <sparc/dev/fdvar.h>
#elif SUN4U
#include <dev/ebus/ebusreg.h>
#include <dev/ebus/ebusvar.h>
#include <dev/sbus/sbusvar.h>
#include <sparc64/dev/auxioreg.h>
#include <sparc64/dev/auxiovar.h>
#include <sparc64/dev/fdcreg.h>
#include <sparc64/dev/fdcvar.h>
#endif
#include <prop/proplib.h>
#define FDUNIT(dev) (minor(dev) / 8)
#define FDTYPE(dev) (minor(dev) % 8)
#define B_FORMAT B_DEVPRIVATE
#define FD_DEBUG
#ifdef FD_DEBUG
int fdc_debug = 0;
#endif
enum fdc_state {
DEVIDLE = 0,
MOTORWAIT,
DOSEEK,
SEEKWAIT,
SEEKTIMEDOUT,
SEEKCOMPLETE,
DOIO,
IOCOMPLETE,
IOTIMEDOUT,
IOCLEANUPWAIT,
IOCLEANUPTIMEDOUT,
DORESET,
RESETCOMPLETE,
RESETTIMEDOUT,
DORECAL,
RECALWAIT,
RECALTIMEDOUT,
RECALCOMPLETE,
DODSKCHG,
DSKCHGWAIT,
DSKCHGTIMEDOUT,
};
struct fdc_softc {
device_t sc_dev;
bus_space_tag_t sc_bustag;
struct callout sc_timo_ch;
struct callout sc_intr_ch;
struct fd_softc *sc_fd[4];
TAILQ_HEAD(drivehead, fd_softc) sc_drives;
enum fdc_state sc_state;
int sc_flags;
#define FDC_82077 0x01
#define FDC_NEEDHEADSETTLE 0x02
#define FDC_EIS 0x04
#define FDC_NEEDMOTORWAIT 0x08
#define FDC_NOEJECT 0x10
#define FDC_EBUS 0x20
int sc_errors;
int sc_overruns;
int sc_cfg;
struct fdcio sc_io;
#define sc_handle sc_io.fdcio_handle
#define sc_reg_msr sc_io.fdcio_reg_msr
#define sc_reg_fifo sc_io.fdcio_reg_fifo
#define sc_reg_dor sc_io.fdcio_reg_dor
#define sc_reg_dir sc_io.fdcio_reg_dir
#define sc_reg_drs sc_io.fdcio_reg_msr
#define sc_itask sc_io.fdcio_itask
#define sc_istatus sc_io.fdcio_istatus
#define sc_data sc_io.fdcio_data
#define sc_tc sc_io.fdcio_tc
#define sc_nstat sc_io.fdcio_nstat
#define sc_status sc_io.fdcio_status
#define sc_intrcnt sc_io.fdcio_intrcnt
void *sc_sicookie;
};
#ifdef SUN4
extern struct fdcio *fdciop;
#endif
#ifdef SUN4
int fdcmatch_mainbus(device_t, cfdata_t, void*);
int fdcmatch_obio(device_t, cfdata_t, void *);
void fdcattach_mainbus(device_t, device_t, void *);
void fdcattach_obio(device_t, device_t, void *);
#elif SUN4U
int fdcmatch_sbus(device_t, cfdata_t, void *);
int fdcmatch_ebus(device_t, cfdata_t, void *);
void fdcattach_sbus(device_t, device_t, void *);
void fdcattach_ebus(device_t, device_t, void *);
#endif
int fdcattach(struct fdc_softc *, int);
#ifdef SUN4
CFATTACH_DECL_NEW(fdc_mainbus, sizeof(struct fdc_softc),
fdcmatch_mainbus, fdcattach_mainbus, NULL, NULL);
CFATTACH_DECL_NEW(fdc_obio, sizeof(struct fdc_softc),
fdcmatch_obio, fdcattach_obio, NULL, NULL);
#elif SUN4U
CFATTACH_DECL_NEW(fdc_sbus, sizeof(struct fdc_softc),
fdcmatch_sbus, fdcattach_sbus, NULL, NULL);
CFATTACH_DECL_NEW(fdc_ebus, sizeof(struct fdc_softc),
fdcmatch_ebus, fdcattach_ebus, NULL, NULL);
#endif
static struct fd_type *fd_dev_to_type(struct fd_softc *, dev_t);
struct fd_type {
int sectrac;
int heads;
int seccyl;
int secsize;
int datalen;
int steprate;
int gap1;
int gap2;
int cylinders;
int size;
int step;
int rate;
int fillbyte;
int interleave;
const char *name;
};
struct fd_type fd_types[] = {
{ 18,2,36,2,0xff,0xcf,0x1b,0x6c,80,2880,1,FDC_500KBPS,0xf6,1, "1.44MB" },
{ 9,2,18,2,0xff,0xdf,0x2a,0x50,80,1440,1,FDC_250KBPS,0xf6,1, "720KB" },
{ 9,2,18,2,0xff,0xdf,0x2a,0x50,40, 720,2,FDC_250KBPS,0xf6,1, "360KB/x" },
{ 8,2,16,3,0xff,0xdf,0x35,0x74,77,1232,1,FDC_500KBPS,0xf6,1, "1.2MB/NEC" }
};
struct fd_softc {
device_t sc_dev;
struct disk sc_dk;
struct fd_type *sc_deftype;
struct fd_type *sc_type;
struct callout sc_motoron_ch;
struct callout sc_motoroff_ch;
daddr_t sc_blkno;
int sc_bcount;
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;
int sc_opts;
TAILQ_ENTRY(fd_softc) sc_drivechain;
int sc_ops;
struct bufq_state *sc_q;
int sc_active;
};
int fdmatch(device_t, cfdata_t, void *);
void fdattach(device_t, device_t, void *);
bool fdshutdown(device_t, int);
bool fdsuspend(device_t, const pmf_qual_t *);
CFATTACH_DECL_NEW(fd, sizeof(struct fd_softc),
fdmatch, fdattach, NULL, NULL);
extern struct cfdriver fd_cd;
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
};
void fdgetdisklabel(dev_t);
int fd_get_parms(struct fd_softc *);
void fdstrategy(struct buf *);
void fdstart(struct fd_softc *);
int fdprint(void *, const char *);
struct dkdriver fddkdriver = {
.d_strategy = fdstrategy
};
struct fd_type *fd_nvtotype(char *, int, int);
void fd_set_motor(struct fdc_softc *);
void fd_motor_off(void *);
void fd_motor_on(void *);
int fdcresult(struct fdc_softc *);
int fdc_wrfifo(struct fdc_softc *, uint8_t);
void fdcstart(struct fdc_softc *);
void fdcstatus(struct fdc_softc *, const char *);
void fdc_reset(struct fdc_softc *);
int fdc_diskchange(struct fdc_softc *);
void fdctimeout(void *);
void fdcpseudointr(void *);
int fdc_c_hwintr(void *);
void fdchwintr(void);
void fdcswintr(void *);
int fdcstate(struct fdc_softc *);
void fdcretry(struct fdc_softc *);
void fdfinish(struct fd_softc *, struct buf *);
int fdformat(dev_t, struct ne7_fd_formb *, struct proc *);
void fd_do_eject(struct fd_softc *);
void fd_mountroot_hook(device_t );
static int fdconf(struct fdc_softc *);
static void establish_chip_type(
struct fdc_softc *,
bus_space_tag_t,
bus_addr_t,
bus_size_t,
bus_space_handle_t);
static void fd_set_geometry(struct fd_softc *);
#ifdef MEMORY_DISK_HOOKS
int fd_read_md_image(size_t *, void **);
#endif
#ifdef SUN4
#define OBP_FDNAME (CPU_ISSUN4M ? "SUNW,fdtwo" : "fd")
int
fdcmatch_mainbus(device_t parent, cfdata_t match, void *aux)
{
struct mainbus_attach_args *ma = aux;
if (!CPU_ISSUN4C)
return 0;
if (strcmp("fd", ma->ma_name) != 0)
return 0;
return bus_space_probe(ma->ma_bustag,
ma->ma_paddr,
1,
0,
0,
NULL, NULL);
}
int
fdcmatch_obio(device_t parent, cfdata_t match, void *aux)
{
union obio_attach_args *uoba = aux;
struct sbus_attach_args *sa;
if (uoba->uoba_isobio4 != 0)
return 0;
sa = &uoba->uoba_sbus;
if (strcmp("SUNW,fdtwo", sa->sa_name) != 0)
return 0;
return bus_space_probe(sa->sa_bustag,
sbus_bus_addr(sa->sa_bustag,
sa->sa_slot, sa->sa_offset),
1,
0,
0,
NULL, NULL);
}
#elif SUN4U
int
fdcmatch_sbus(device_t parent, cfdata_t match, void *aux)
{
struct sbus_attach_args *sa = aux;
return strcmp("SUNW,fdtwo", sa->sa_name) == 0;
}
int
fdcmatch_ebus(device_t parent, cfdata_t match, void *aux)
{
struct ebus_attach_args *ea = aux;
return strcmp("fdthree", ea->ea_name) == 0;
}
#endif
static void
establish_chip_type(struct fdc_softc *fdc,
bus_space_tag_t tag, bus_addr_t addr, bus_size_t size,
bus_space_handle_t handle)
{
uint8_t v;
if (size < 8)
return;
#ifdef SUN4
if (bus_space_probe(tag, addr,
1,
FDREG77_DOR,
0,
NULL, NULL) == 0) {
return;
}
#endif
v = bus_space_read_1(tag, handle, FDREG77_DOR);
if (v == NE7_RQM) {
bus_space_write_1(tag, handle, FDREG77_DOR, FDC_250KBPS);
v = bus_space_read_1(tag, handle, FDREG77_DOR);
if (v == NE7_RQM) {
return;
}
}
fdc->sc_flags |= FDC_82077;
}
struct fdc_attach_args {
int fa_drive;
struct fd_type *fa_deftype;
};
int
fdprint(void *aux, const char *fdc)
{
register struct fdc_attach_args *fa = aux;
if (!fdc)
aprint_normal(" drive %d", fa->fa_drive);
return QUIET;
}
static int
fdconf(struct fdc_softc *fdc)
{
int vroom;
if (fdc_wrfifo(fdc, NE7CMD_DUMPREG) || fdcresult(fdc) != 10)
return -1;
if ((vroom = fdc->sc_status[7]) == 0)
vroom = 0x64;
if (fdc_wrfifo(fdc, NE7CMD_CFG) != 0)
return -1;
if (fdc_wrfifo(fdc, vroom) != 0)
return -1;
if (fdc_wrfifo(fdc, fdc->sc_cfg) != 0)
return -1;
if (fdc_wrfifo(fdc, 0) != 0)
return -1;
if ((fdc->sc_flags & FDC_82077) != 0) {
if (fdc_wrfifo(fdc, NE7CMD_LOCK | CFG_LOCK) != 0 ||
fdcresult(fdc) != 1) {
#ifdef DEBUG
printf("fdconf: CFGLOCK failed");
#endif
return -1;
}
}
return 0;
#if 0
if (fdc_wrfifo(fdc, NE7CMD_VERSION) == 0 &&
fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x90) {
if (fdc_debug)
printf("[version cmd]");
}
#endif
}
#ifdef SUN4
void
fdcattach_mainbus(device_t parent, device_t self, void *aux)
{
struct fdc_softc *fdc = device_private(self);
struct mainbus_attach_args *ma = aux;
fdc->sc_dev = self;
fdc->sc_bustag = ma->ma_bustag;
if (bus_space_map(
ma->ma_bustag,
ma->ma_paddr,
ma->ma_size,
BUS_SPACE_MAP_LINEAR,
&fdc->sc_handle) != 0) {
aprint_error_dev(self, "cannot map registers\n");
return;
}
establish_chip_type(fdc,
ma->ma_bustag,
ma->ma_paddr,
ma->ma_size,
fdc->sc_handle);
if (fdcattach(fdc, ma->ma_pri) != 0)
bus_space_unmap(ma->ma_bustag, fdc->sc_handle, ma->ma_size);
}
void
fdcattach_obio(device_t parent, device_t self, void *aux)
{
struct fdc_softc *fdc = device_private(self);
union obio_attach_args *uoba = aux;
struct sbus_attach_args *sa = &uoba->uoba_sbus;
if (sa->sa_nintr == 0) {
printf(": no interrupt line configured\n");
return;
}
fdc->sc_dev = self;
fdc->sc_bustag = sa->sa_bustag;
if (sbus_bus_map(sa->sa_bustag,
sa->sa_slot, sa->sa_offset, sa->sa_size,
BUS_SPACE_MAP_LINEAR, &fdc->sc_handle) != 0) {
aprint_error_dev(self, "cannot map control registers\n");
return;
}
establish_chip_type(fdc,
sa->sa_bustag,
sbus_bus_addr(sa->sa_bustag, sa->sa_slot, sa->sa_offset),
sa->sa_size,
fdc->sc_handle);
if (strcmp(prom_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
printf(": no drives attached\n");
return;
}
if (fdcattach(fdc, sa->sa_pri) != 0)
bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
}
#elif SUN4U
void
fdcattach_sbus(device_t parent, device_t self, void *aux)
{
struct fdc_softc *fdc = device_private(self);
struct sbus_attach_args *sa = aux;
if (sa->sa_nintr == 0) {
printf(": no interrupt line configured\n");
return;
}
if (auxio_fd_control(0) != 0) {
printf(": can't attach before auxio\n");
return;
}
fdc->sc_dev = self;
fdc->sc_bustag = sa->sa_bustag;
if (bus_space_map(sa->sa_bustag, BUS_ADDR(sa->sa_slot, sa->sa_offset),
sa->sa_size, 0, &fdc->sc_handle) != 0) {
printf(": cannot map control registers\n");
return;
}
establish_chip_type(fdc,
sa->sa_bustag,
BUS_ADDR(sa->sa_slot, sa->sa_offset),
sa->sa_size,
fdc->sc_handle);
if (strcmp(prom_getpropstring(sa->sa_node, "status"), "disabled") == 0) {
printf(": no drives attached\n");
return;
}
if (prom_getproplen(sa->sa_node, "manual") >= 0)
fdc->sc_flags |= FDC_NOEJECT;
if (fdcattach(fdc, sa->sa_pri) != 0)
bus_space_unmap(sa->sa_bustag, fdc->sc_handle, sa->sa_size);
}
void
fdcattach_ebus(device_t parent, device_t self, void *aux)
{
struct fdc_softc *fdc = device_private(self);
struct ebus_attach_args *ea = aux;
int map_vaddr;
if (ea->ea_nintr == 0) {
printf(": no interrupt line configured\n");
return;
}
if (ea->ea_nreg < 3) {
printf(": expected 3 registers, only got %d\n",
ea->ea_nreg);
return;
}
fdc->sc_dev = self;
fdc->sc_bustag = ea->ea_bustag;
if (ea->ea_nvaddr > 0) {
sparc_promaddr_to_handle(ea->ea_bustag,
ea->ea_vaddr[0], &fdc->sc_handle);
map_vaddr = 1;
} else if (bus_space_map(fdc->sc_bustag,
EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
ea->ea_reg[0].size, 0, &fdc->sc_handle) == 0) {
map_vaddr = 0;
} else {
printf(": can't map control registers\n");
return;
}
establish_chip_type(fdc,
fdc->sc_bustag,
map_vaddr ? ea->ea_vaddr[0] :
EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
ea->ea_reg[0].size,
fdc->sc_handle);
fdc->sc_flags |= FDC_EBUS;
if (prom_getproplen(ea->ea_node, "manual") >= 0)
fdc->sc_flags |= FDC_NOEJECT;
if (fdcattach(fdc, ea->ea_intr[0]) != 0)
if (map_vaddr == 0)
bus_space_unmap(ea->ea_bustag, fdc->sc_handle,
ea->ea_reg[0].size);
}
#endif
int
fdcattach(struct fdc_softc *fdc, int pri)
{
struct fdc_attach_args fa;
int drive_attached;
char code;
callout_init(&fdc->sc_timo_ch, 0);
callout_init(&fdc->sc_intr_ch, 0);
fdc->sc_state = DEVIDLE;
fdc->sc_itask = FDC_ITASK_NONE;
fdc->sc_istatus = FDC_ISTATUS_NONE;
fdc->sc_flags |= FDC_EIS;
TAILQ_INIT(&fdc->sc_drives);
if ((fdc->sc_flags & FDC_82077) != 0) {
fdc->sc_reg_msr = FDREG77_MSR;
fdc->sc_reg_fifo = FDREG77_FIFO;
fdc->sc_reg_dor = FDREG77_DOR;
fdc->sc_reg_dir = FDREG77_DIR;
code = '7';
fdc->sc_flags |= FDC_NEEDMOTORWAIT;
} else {
fdc->sc_reg_msr = FDREG72_MSR;
fdc->sc_reg_fifo = FDREG72_FIFO;
fdc->sc_reg_dor = 0;
code = '2';
}
fdc->sc_cfg = CFG_EIS|CFG_POLL|(8 & CFG_THRHLD_MASK);
if (fdconf(fdc) != 0) {
printf(": no drives attached\n");
return -1;
}
fdc->sc_sicookie = softint_establish(SOFTINT_BIO, fdcswintr, fdc);
if (fdc->sc_sicookie == NULL) {
aprint_normal("\n");
aprint_error_dev(fdc->sc_dev,
"cannot register soft interrupt handler\n");
callout_stop(&fdc->sc_timo_ch);
callout_stop(&fdc->sc_intr_ch);
return -1;
}
#ifdef SUN4
printf(" softpri %d: chip 8207%c\n", IPL_SOFTFDC, code);
#elif SUN4U
printf(" softpri %d: chip 8207%c", IPL_BIO, code);
if (fdc->sc_flags & FDC_NOEJECT)
printf(": manual eject");
printf("\n");
#endif
#ifdef SUN4
fdciop = &fdc->sc_io;
if (bus_intr_establish2(fdc->sc_bustag, pri, 0,
fdc_c_hwintr, fdc, fdchwintr) == NULL) {
#elif SUN4U
if (bus_intr_establish(fdc->sc_bustag, pri, IPL_BIO,
fdc_c_hwintr, fdc) == NULL) {
#endif
aprint_normal("\n");
aprint_error_dev(fdc->sc_dev,
"cannot register interrupt handler\n");
callout_stop(&fdc->sc_timo_ch);
callout_stop(&fdc->sc_intr_ch);
softint_disestablish(fdc->sc_sicookie);
return -1;
}
evcnt_attach_dynamic(&fdc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
device_xname(fdc->sc_dev), "intr");
drive_attached = 0;
for (fa.fa_drive = 0; fa.fa_drive < 4; fa.fa_drive++) {
fa.fa_deftype = NULL;
fa.fa_deftype = &fd_types[0];
if (config_found(fdc->sc_dev, (void *)&fa, fdprint,
CFARGS_NONE) != NULL)
drive_attached = 1;
}
if (drive_attached == 0) {
}
return 0;
}
int
fdmatch(device_t parent, cfdata_t match, void *aux)
{
struct fdc_softc *fdc = device_private(parent);
bus_space_tag_t t = fdc->sc_bustag;
bus_space_handle_t h = fdc->sc_handle;
struct fdc_attach_args *fa = aux;
int drive = fa->fa_drive;
int n, ok;
if (drive > 0)
return 0;
if ((fdc->sc_flags & FDC_82077) != 0) {
bus_space_write_1(t, h, fdc->sc_reg_dor,
drive | FDO_FRST | FDO_MOEN(drive));
delay(250000);
#ifdef SUN4
} else {
auxregbisc(AUXIO4C_FDS, 0);
#endif
}
fdc->sc_nstat = 0;
fdc_wrfifo(fdc, NE7CMD_RECAL);
fdc_wrfifo(fdc, drive);
for (n = 0; n < 10000; n++) {
uint8_t v;
delay(1000);
v = bus_space_read_1(t, h, fdc->sc_reg_msr);
if ((v & (NE7_RQM|NE7_DIO|NE7_CB)) == NE7_RQM) {
delay(100000);
if (fdc_wrfifo(fdc, NE7CMD_SENSEI))
break;
if (fdcresult(fdc) == 1 && fdc->sc_status[0] == 0x80)
continue;
break;
}
}
n = fdc->sc_nstat;
#ifdef FD_DEBUG
if (fdc_debug) {
int i;
printf("fdprobe: %d stati:", n);
for (i = 0; i < n; i++)
printf(" 0x%x", fdc->sc_status[i]);
printf("\n");
}
#endif
ok = (n == 2 && (fdc->sc_status[0] & 0xf8) == 0x20) ? 1 : 0;
if ((fdc->sc_flags & FDC_82077) != 0) {
bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
#ifdef SUN4
} else {
auxregbisc(0, AUXIO4C_FDS);
#endif
}
return ok;
}
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;
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->cylinders, 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;
fdc_wrfifo(fdc, NE7CMD_SPECIFY);
fdc_wrfifo(fdc, type->steprate);
fdc_wrfifo(fdc, 6 | NE7_SPECIFY_NODMA);
disk_init(&fd->sc_dk, device_xname(fd->sc_dev), &fddkdriver);
disk_attach(&fd->sc_dk);
mountroothook_establish(fd_mountroot_hook, fd->sc_dev);
fd_set_geometry(fd);
if (!pmf_device_register1(self, fdsuspend, NULL, fdshutdown))
aprint_error_dev(self, "couldn't establish power handler\n");
}
bool fdshutdown(device_t self, int how)
{
struct fd_softc *fd = device_private(self);
fd_motor_off(fd);
return true;
}
bool fdsuspend(device_t self, const pmf_qual_t *qual)
{
return fdshutdown(self, boothowto);
}
static struct fd_type *
fd_dev_to_type(struct fd_softc *fd, dev_t dev)
{
int type = FDTYPE(dev);
if (type > (sizeof(fd_types) / sizeof(fd_types[0])))
return NULL;
return type ? &fd_types[type - 1] : fd->sc_deftype;
}
void
fdstrategy(struct buf *bp)
{
struct fd_softc *fd;
int sz;
int s;
fd = device_lookup_private(&fd_cd, FDUNIT(bp->b_dev));
if (fd == NULL) {
bp->b_error = EINVAL;
goto done;
}
if (bp->b_blkno < 0 ||
(((bp->b_bcount % FD_BSIZE(fd)) != 0 ||
(bp->b_blkno * DEV_BSIZE) % FD_BSIZE(fd) != 0) &&
(bp->b_flags & B_FORMAT) == 0)) {
bp->b_error = EINVAL;
goto done;
}
if (bp->b_bcount == 0)
goto done;
sz = howmany(bp->b_bcount, DEV_BSIZE);
if (bp->b_blkno + sz > (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)) {
sz = (fd->sc_type->size * DEV_BSIZE) / FD_BSIZE(fd)
- bp->b_blkno;
if (sz == 0) {
bp->b_resid = bp->b_bcount;
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 * DEV_BSIZE) /
(FD_BSIZE(fd) * fd->sc_type->seccyl);
#ifdef FD_DEBUG
if (fdc_debug > 1)
printf("fdstrategy: b_blkno %lld b_bcount %d blkno %lld cylin %d sz %d\n",
(long long)bp->b_blkno, bp->b_bcount,
(long long)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("fdstrategy: controller inactive\n");
fdcstart(fdc);
}
}
#endif
splx(s);
return;
done:
biodone(bp);
}
void
fdstart(struct fd_softc *fd)
{
struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
int active = fdc->sc_drives.tqh_first != 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 (fd->sc_drivechain.tqe_next && ++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;
}
void
fdc_reset(struct fdc_softc *fdc)
{
bus_space_tag_t t = fdc->sc_bustag;
bus_space_handle_t h = fdc->sc_handle;
if ((fdc->sc_flags & FDC_82077) != 0) {
bus_space_write_1(t, h, fdc->sc_reg_dor,
FDO_FDMAEN | FDO_MOEN(0));
}
bus_space_write_1(t, h, fdc->sc_reg_drs, DRS_RESET);
delay(10);
bus_space_write_1(t, h, fdc->sc_reg_drs, 0);
if ((fdc->sc_flags & FDC_82077) != 0) {
bus_space_write_1(t, h, fdc->sc_reg_dor,
FDO_FRST | FDO_FDMAEN | FDO_DS);
}
#ifdef FD_DEBUG
if (fdc_debug)
printf("fdc reset\n");
#endif
}
void
fd_set_motor(struct fdc_softc *fdc)
{
struct fd_softc *fd;
u_char status;
int n;
if ((fdc->sc_flags & FDC_82077) != 0) {
status = FDO_FRST | FDO_FDMAEN;
if ((fd = fdc->sc_drives.tqh_first) != NULL)
status |= fd->sc_drive;
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_bustag, fdc->sc_handle,
fdc->sc_reg_dor, status);
#ifdef SUN4
} else {
for (n = 0; n < 4; n++) {
if ((fd = fdc->sc_fd[n]) != NULL &&
(fd->sc_flags & FD_MOTOR) != 0) {
auxregbisc(AUXIO4C_FDS, 0);
return;
}
}
auxregbisc(0, AUXIO4C_FDS);
#endif
}
}
void
fd_motor_off(void *arg)
{
struct fd_softc *fd = arg;
int s;
s = splbio();
fd->sc_flags &= ~(FD_MOTOR | FD_MOTOR_WAIT);
fd_set_motor(device_private(device_parent(fd->sc_dev)));
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 ((fdc->sc_drives.tqh_first == fd) && (fdc->sc_state == MOTORWAIT))
(void)fdcstate(fdc);
splx(s);
}
int
fdcresult(struct fdc_softc *fdc)
{
bus_space_tag_t t = fdc->sc_bustag;
bus_space_handle_t h = fdc->sc_handle;
int j, n = 0;
for (j = 10000; j; j--) {
uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
v &= (NE7_DIO | NE7_RQM | NE7_CB);
if (v == NE7_RQM)
return fdc->sc_nstat = n;
if (v == (NE7_DIO | NE7_RQM | NE7_CB)) {
if (n >= sizeof(fdc->sc_status)) {
log(LOG_ERR, "fdcresult: overrun\n");
return -1;
}
fdc->sc_status[n++] =
bus_space_read_1(t, h, fdc->sc_reg_fifo);
} else
delay(1);
}
log(LOG_ERR, "fdcresult: timeout\n");
return fdc->sc_nstat = -1;
}
int
fdc_wrfifo(struct fdc_softc *fdc, uint8_t x)
{
bus_space_tag_t t = fdc->sc_bustag;
bus_space_handle_t h = fdc->sc_handle;
int i;
for (i = 100000; i-- > 0;) {
uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_msr);
if ((v & (NE7_DIO|NE7_RQM)) == NE7_RQM) {
bus_space_write_1(t, h, fdc->sc_reg_fifo, x);
return 0;
}
delay(1);
}
return -1;
}
int
fdc_diskchange(struct fdc_softc *fdc)
{
#ifdef SUN4
if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
#endif
bus_space_tag_t t = fdc->sc_bustag;
bus_space_handle_t h = fdc->sc_handle;
uint8_t v = bus_space_read_1(t, h, fdc->sc_reg_dir);
return (v & FDI_DCHG) != 0;
#ifdef SUN4
} else if (CPU_ISSUN4C) {
return (*AUXIO4C_REG & AUXIO4C_FDC) != 0;
}
return 0;
#endif
}
int
fdopen(dev_t dev, int flags, int fmt, struct lwp *l)
{
int pmask;
struct fd_softc *fd;
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 &&
fd->sc_type != type)
return EBUSY;
fd->sc_type = type;
fd->sc_cylin = -1;
fd->sc_flags |= FD_OPEN;
if (fd->sc_dk.dk_openmask == 0)
fdgetdisklabel(dev);
pmask = (1 << DISKPART(dev));
switch (fmt) {
case S_IFCHR:
fd->sc_dk.dk_copenmask |= pmask;
break;
case S_IFBLK:
fd->sc_dk.dk_bopenmask |= pmask;
break;
}
fd->sc_dk.dk_openmask =
fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
return 0;
}
int
fdclose(dev_t dev, int flags, int fmt, struct lwp *l)
{
struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
int pmask = (1 << DISKPART(dev));
fd->sc_flags &= ~FD_OPEN;
fd->sc_opts &= ~(FDOPT_NORETRY|FDOPT_SILENT);
switch (fmt) {
case S_IFCHR:
fd->sc_dk.dk_copenmask &= ~pmask;
break;
case S_IFBLK:
fd->sc_dk.dk_bopenmask &= ~pmask;
break;
}
fd->sc_dk.dk_openmask =
fd->sc_dk.dk_copenmask | fd->sc_dk.dk_bopenmask;
return 0;
}
int
fdread(dev_t dev, struct uio *uio, int flag)
{
return physio(fdstrategy, NULL, dev, B_READ, minphys, uio);
}
int
fdwrite(dev_t dev, struct uio *uio, int flag)
{
return physio(fdstrategy, NULL, dev, B_WRITE, minphys, uio);
}
void
fdcstart(struct fdc_softc *fdc)
{
#ifdef DIAGNOSTIC
if (fdc->sc_state != DEVIDLE) {
printf("fdcstart: not idle\n");
return;
}
#endif
(void)fdcstate(fdc);
}
static void
fdcpstatus(struct fdc_softc *fdc)
{
char bits[64];
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]);
}
void
fdcstatus(struct fdc_softc *fdc, const char *s)
{
struct fd_softc *fd = fdc->sc_drives.tqh_first;
int n;
char bits[64];
n = fdc->sc_nstat;
#if 0
if (n == 0 && (fdc->sc_flags & FDC_82077) != 0) {
fdc_wrfifo(fdc, NE7CMD_SENSEI);
(void)fdcresult(fdc);
n = 2;
}
#endif
printf("%s: %s: state %d",
fd ? device_xname(fd->sc_dev) : "fdc", s, fdc->sc_state);
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:
fdcpstatus(fdc);
break;
#ifdef DIAGNOSTIC
default:
printf(" fdcstatus: weird size: %d\n", n);
break;
#endif
}
}
void
fdctimeout(void *arg)
{
struct fdc_softc *fdc = arg;
struct fd_softc *fd;
int s;
s = splbio();
fd = fdc->sc_drives.tqh_first;
if (fd == NULL) {
aprint_error_dev(fdc->sc_dev, "timeout but no I/O pending: state %d, istatus=%d\n",
fdc->sc_state, fdc->sc_istatus);
fdc->sc_state = DEVIDLE;
goto out;
}
if (bufq_peek(fd->sc_q) != NULL)
fdc->sc_state++;
else
fdc->sc_state = DEVIDLE;
(void)fdcstate(fdc);
out:
splx(s);
}
void
fdcpseudointr(void *arg)
{
struct fdc_softc *fdc = arg;
int s;
s = splbio();
(void)fdcstate(fdc);
splx(s);
}
int
fdc_c_hwintr(void *arg)
{
struct fdc_softc *fdc = arg;
bus_space_tag_t t = fdc->sc_bustag;
bus_space_handle_t h = fdc->sc_handle;
switch (fdc->sc_itask) {
case FDC_ITASK_NONE:
return 0;
case FDC_ITASK_SENSEI:
if (fdc_wrfifo(fdc, NE7CMD_SENSEI) != 0 || fdcresult(fdc) == -1)
fdc->sc_istatus = FDC_ISTATUS_ERROR;
else
fdc->sc_istatus = FDC_ISTATUS_DONE;
softint_schedule(fdc->sc_sicookie);
return 1;
case FDC_ITASK_RESULT:
if (fdcresult(fdc) == -1)
fdc->sc_istatus = FDC_ISTATUS_ERROR;
else
fdc->sc_istatus = FDC_ISTATUS_DONE;
softint_schedule(fdc->sc_sicookie);
return 1;
case FDC_ITASK_DMA:
break;
default:
printf("fdc: stray hard interrupt: itask=%d\n", fdc->sc_itask);
fdc->sc_istatus = FDC_ISTATUS_SPURIOUS;
softint_schedule(fdc->sc_sicookie);
return 1;
}
for (;;) {
uint8_t msr;
msr = bus_space_read_1(t, h, fdc->sc_reg_msr);
if ((msr & NE7_RQM) == 0)
break;
if ((msr & NE7_NDM) == 0) {
fdcresult(fdc);
fdc->sc_istatus = FDC_ISTATUS_DONE;
softint_schedule(fdc->sc_sicookie);
break;
}
if (fdc->sc_tc == 0)
panic("fdc: overrun");
if ((msr & NE7_DIO) != 0)
*fdc->sc_data =
bus_space_read_1(t, h, fdc->sc_reg_fifo);
else
bus_space_write_1(t, h, fdc->sc_reg_fifo,
*fdc->sc_data);
fdc->sc_data++;
if (--fdc->sc_tc == 0) {
FTC_FLIP;
break;
}
}
return 1;
}
void
fdcswintr(void *arg)
{
struct fdc_softc *fdc = arg;
if (fdc->sc_istatus == FDC_ISTATUS_NONE)
return;
switch (fdc->sc_istatus) {
case FDC_ISTATUS_ERROR:
printf("fdc: ierror status: state %d\n", fdc->sc_state);
break;
case FDC_ISTATUS_SPURIOUS:
printf("fdc: spurious interrupt: state %d\n", fdc->sc_state);
break;
}
fdcstate(fdc);
return;
}
int
fdcstate(struct fdc_softc *fdc)
{
#define st0 fdc->sc_status[0]
#define st1 fdc->sc_status[1]
#define cyl fdc->sc_status[1]
#define FDC_WRFIFO(fdc, c) do { \
if (fdc_wrfifo(fdc, (c))) { \
goto xxx; \
} \
} while(0)
struct fd_softc *fd;
struct buf *bp;
int read, head, sec, nblks;
struct fd_type *type;
struct ne7_fd_formb *finfo = NULL;
if (fdc->sc_istatus == FDC_ISTATUS_ERROR) {
if (fdc->sc_state != RESETCOMPLETE &&
fdc->sc_state != RECALWAIT &&
fdc->sc_state != RECALCOMPLETE)
fdc->sc_state = DORESET;
}
fdc->sc_istatus = FDC_ISTATUS_NONE;
fdc->sc_itask = FDC_ITASK_NONE;
loop:
fd = fdc->sc_drives.tqh_first;
if (fd == NULL) {
fdc->sc_state = DEVIDLE;
return 0;
}
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;
}
if (bp->b_flags & B_FORMAT)
finfo = (struct ne7_fd_formb *)bp->b_data;
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 * DEV_BSIZE) / FD_BSIZE(fd);
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);
fdc->sc_state = MOTORWAIT;
if ((fdc->sc_flags & FDC_NEEDMOTORWAIT) != 0) {
callout_reset(&fd->sc_motoron_ch, hz / 4,
fd_motor_on, fd);
} else {
fd->sc_flags &= ~FD_MOTOR_WAIT;
goto loop;
}
return 1;
}
fd_set_motor(fdc);
if (fdc_diskchange(fdc))
goto dodskchg;
case DOSEEK:
doseek:
if ((fdc->sc_flags & FDC_EIS) &&
(bp->b_flags & B_FORMAT) == 0) {
fd->sc_cylin = bp->b_cylinder;
goto doio;
}
if (fd->sc_cylin == bp->b_cylinder)
goto doio;
fd->sc_cylin = -1;
fdc->sc_state = SEEKWAIT;
fdc->sc_nstat = 0;
iostat_seek(fd->sc_dk.dk_stats);
disk_busy(&fd->sc_dk);
callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
FDC_WRFIFO(fdc, NE7CMD_SPECIFY);
FDC_WRFIFO(fdc, fd->sc_type->steprate);
FDC_WRFIFO(fdc, 6 | NE7_SPECIFY_NODMA);
fdc->sc_itask = FDC_ITASK_SENSEI;
FDC_WRFIFO(fdc, NE7CMD_SEEK);
FDC_WRFIFO(fdc, fd->sc_drive);
FDC_WRFIFO(fdc, bp->b_cylinder * fd->sc_type->step);
return 1;
case DODSKCHG:
dodskchg:
disk_busy(&fd->sc_dk);
callout_reset(&fdc->sc_timo_ch, 4 * hz, fdctimeout, fdc);
fd->sc_cylin = -1;
fdc->sc_nstat = 0;
fdc->sc_state = DSKCHGWAIT;
fdc->sc_itask = FDC_ITASK_SENSEI;
FDC_WRFIFO(fdc, NE7CMD_SEEK);
FDC_WRFIFO(fdc, fd->sc_drive);
FDC_WRFIFO(fdc, 1 * fd->sc_type->step);
return 1;
case DSKCHGWAIT:
callout_stop(&fdc->sc_timo_ch);
disk_unbusy(&fd->sc_dk, 0, 0);
if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
cyl != 1 * fd->sc_type->step) {
fdcstatus(fdc, "dskchg seek failed");
fdc->sc_state = DORESET;
} else
fdc->sc_state = DORECAL;
if (fdc_diskchange(fdc)) {
aprint_error_dev(fdc->sc_dev,
"cannot clear disk change status\n");
fdc->sc_state = DORESET;
}
goto loop;
case DOIO:
doio:
if (finfo != NULL)
fd->sc_skip = (char *)&(finfo->fd_formb_cylno(0)) -
(char *)finfo;
type = fd->sc_type;
sec = fd->sc_blkno % type->seccyl;
nblks = type->seccyl - sec;
nblks = uimin(nblks, fd->sc_bcount / FD_BSIZE(fd));
nblks = uimin(nblks, FDC_MAXIOSIZE / FD_BSIZE(fd));
fd->sc_nblks = nblks;
fd->sc_nbytes = finfo ? bp->b_bcount : nblks * FD_BSIZE(fd);
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("fdcintr: block %d != blkno %d\n", block,
(int)fd->sc_blkno);
#ifdef DDB
Debugger();
#endif
}}
#endif
read = bp->b_flags & B_READ;
fdc->sc_data = (char *)bp->b_data + fd->sc_skip;
fdc->sc_tc = fd->sc_nbytes;
bus_space_write_1(fdc->sc_bustag, fdc->sc_handle,
fdc->sc_reg_drs, type->rate);
#ifdef FD_DEBUG
if (fdc_debug > 1)
printf("fdcstate: doio: %s drive %d "
"track %d head %d sec %d nblks %d\n",
finfo ? "format" :
(read ? "read" : "write"),
fd->sc_drive, fd->sc_cylin, head, sec, nblks);
#endif
fdc->sc_state = IOCOMPLETE;
fdc->sc_itask = FDC_ITASK_DMA;
fdc->sc_nstat = 0;
disk_busy(&fd->sc_dk);
callout_reset(&fdc->sc_timo_ch, 3 * hz, fdctimeout, fdc);
if (finfo != NULL) {
FDC_WRFIFO(fdc, NE7CMD_FORMAT);
FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
FDC_WRFIFO(fdc, finfo->fd_formb_secshift);
FDC_WRFIFO(fdc, finfo->fd_formb_nsecs);
FDC_WRFIFO(fdc, finfo->fd_formb_gaplen);
FDC_WRFIFO(fdc, finfo->fd_formb_fillbyte);
} else {
if (read)
FDC_WRFIFO(fdc, NE7CMD_READ);
else
FDC_WRFIFO(fdc, NE7CMD_WRITE);
FDC_WRFIFO(fdc, (head << 2) | fd->sc_drive);
FDC_WRFIFO(fdc, fd->sc_cylin);
FDC_WRFIFO(fdc, head);
FDC_WRFIFO(fdc, sec + 1);
FDC_WRFIFO(fdc, type->secsize);
FDC_WRFIFO(fdc, type->sectrac);
FDC_WRFIFO(fdc, type->gap1);
FDC_WRFIFO(fdc, type->datalen);
}
return 1;
case SEEKWAIT:
callout_stop(&fdc->sc_timo_ch);
fdc->sc_state = SEEKCOMPLETE;
if (fdc->sc_flags & FDC_NEEDHEADSETTLE) {
callout_reset(&fdc->sc_intr_ch, hz / 50,
fdcpseudointr, fdc);
return 1;
}
case SEEKCOMPLETE:
disk_unbusy(&fd->sc_dk, 0, 0);
if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 ||
cyl != bp->b_cylinder * fd->sc_type->step) {
#ifdef FD_DEBUG
if (fdc_debug)
fdcstatus(fdc, "seek failed");
#endif
fdcretry(fdc);
goto loop;
}
fd->sc_cylin = bp->b_cylinder;
goto doio;
case IOTIMEDOUT:
fdc->sc_itask = FDC_ITASK_RESULT;
fdc->sc_state = IOCLEANUPWAIT;
fdc->sc_nstat = 0;
callout_reset(&fdc->sc_timo_ch, hz / 10, fdctimeout, fdc);
FTC_FLIP;
return 1;
case IOCLEANUPTIMEDOUT:
case SEEKTIMEDOUT:
case RECALTIMEDOUT:
case RESETTIMEDOUT:
case DSKCHGTIMEDOUT:
fdcstatus(fdc, "timeout");
fdcretry(fdc);
fdc->sc_state = DORESET;
goto loop;
case IOCLEANUPWAIT:
callout_stop(&fdc->sc_timo_ch);
disk_unbusy(&fd->sc_dk, (bp->b_bcount - bp->b_resid),
(bp->b_flags & B_READ));
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));
if (fdc->sc_nstat != 7 || st1 != 0 ||
((st0 & 0xf8) != 0 &&
((st0 & 0xf8) != 0x20 || (fdc->sc_cfg & CFG_EIS) == 0))) {
#ifdef FD_DEBUG
if (fdc_debug) {
fdcstatus(fdc, bp->b_flags & B_READ ?
"read failed" : "write failed");
printf("blkno %lld nblks %d nstat %d tc %d\n",
(long long)fd->sc_blkno, fd->sc_nblks,
fdc->sc_nstat, fdc->sc_tc);
}
#endif
if (fdc->sc_nstat == 7 &&
(st1 & ST1_OVERRUN) == ST1_OVERRUN) {
int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
if (thr < 15) {
thr++;
fdc->sc_cfg &= ~CFG_THRHLD_MASK;
fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
#ifdef FD_DEBUG
if (fdc_debug)
printf("fdc: %d -> threshold\n",
thr);
#endif
fdconf(fdc);
fdc->sc_overruns = 0;
}
if (++fdc->sc_overruns < 3) {
fdc->sc_state = DOIO;
goto loop;
}
}
fdcretry(fdc);
goto loop;
}
if (fdc->sc_errors) {
diskerr(bp, "fd", "soft error", LOG_PRINTF,
fd->sc_skip / FD_BSIZE(fd),
(struct disklabel *)NULL);
printf("\n");
fdc->sc_errors = 0;
} else {
if (--fdc->sc_overruns < -20) {
int thr = fdc->sc_cfg & CFG_THRHLD_MASK;
if (thr > 0) {
thr--;
fdc->sc_cfg &= ~CFG_THRHLD_MASK;
fdc->sc_cfg |= (thr & CFG_THRHLD_MASK);
#ifdef FD_DEBUG
if (fdc_debug)
printf("fdc: %d -> threshold\n",
thr);
#endif
fdconf(fdc);
}
fdc->sc_overruns = 0;
}
}
fd->sc_blkno += fd->sc_nblks;
fd->sc_skip += fd->sc_nbytes;
fd->sc_bcount -= fd->sc_nbytes;
if (finfo == NULL && 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);
delay(100);
fdc->sc_nstat = 0;
fdc->sc_itask = FDC_ITASK_SENSEI;
fdc->sc_state = RESETCOMPLETE;
callout_reset(&fdc->sc_timo_ch, hz / 2, fdctimeout, fdc);
fdc_reset(fdc);
return 1;
case RESETCOMPLETE:
callout_stop(&fdc->sc_timo_ch);
fdconf(fdc);
case DORECAL:
fdc->sc_state = RECALWAIT;
fdc->sc_itask = FDC_ITASK_SENSEI;
fdc->sc_nstat = 0;
callout_reset(&fdc->sc_timo_ch, 5 * hz, fdctimeout, fdc);
FDC_WRFIFO(fdc, NE7CMD_RECAL);
FDC_WRFIFO(fdc, fd->sc_drive);
return 1;
case RECALWAIT:
callout_stop(&fdc->sc_timo_ch);
fdc->sc_state = RECALCOMPLETE;
if ((fdc->sc_flags & FDC_NEEDHEADSETTLE) != 0) {
callout_reset(&fdc->sc_intr_ch, hz / 30,
fdcpseudointr, fdc);
return 1;
}
case RECALCOMPLETE:
if (fdc->sc_nstat != 2 || (st0 & 0xf8) != 0x20 || cyl != 0) {
#ifdef FD_DEBUG
if (fdc_debug)
fdcstatus(fdc, "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(fdc, "stray interrupt");
return 1;
}
#ifdef DIAGNOSTIC
panic("fdcintr: impossible");
#endif
xxx:
callout_stop(&fdc->sc_timo_ch);
fdcretry(fdc);
fdc->sc_state = DORESET;
goto loop;
#undef st0
#undef st1
#undef cyl
}
void
fdcretry(struct fdc_softc *fdc)
{
struct fd_softc *fd;
struct buf *bp;
int error = EIO;
fd = fdc->sc_drives.tqh_first;
bp = bufq_peek(fd->sc_q);
fdc->sc_overruns = 0;
if (fd->sc_opts & FDOPT_NORETRY)
goto fail;
switch (fdc->sc_errors) {
case 0:
if (fdc->sc_nstat == 7 &&
(fdc->sc_status[0] & 0xd8) == 0x40 &&
(fdc->sc_status[1] & 0x2) == 0x2) {
aprint_error_dev(fdc->sc_dev, "read-only medium\n");
error = EROFS;
goto failsilent;
}
fdc->sc_state =
(fdc->sc_flags & FDC_EIS) ? DOIO : DOSEEK;
break;
case 1: case 2: case 3:
fdc->sc_state = DORECAL;
break;
case 4:
if (fdc->sc_nstat == 7 &&
fdc->sc_status[0] == 0 &&
fdc->sc_status[1] == 0 &&
fdc->sc_status[2] == 0) {
aprint_error_dev(fdc->sc_dev, "no medium?\n");
error = ENODEV;
goto failsilent;
}
fdc->sc_state = DORESET;
break;
default:
fail:
if ((fd->sc_opts & FDOPT_SILENT) == 0) {
diskerr(bp, "fd", "hard error", LOG_PRINTF,
fd->sc_skip / FD_BSIZE(fd),
(struct disklabel *)NULL);
printf("\n");
fdcstatus(fdc, "controller status");
}
failsilent:
bp->b_error = error;
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;
struct fdc_softc *fdc;
struct fdformat_parms *form_parms;
struct fdformat_cmd *form_cmd;
struct ne7_fd_formb *fd_formb;
int il[FD_MAX_NSEC + 1];
int unit;
int i, j;
int error;
unit = FDUNIT(dev);
if (unit >= fd_cd.cd_ndevs)
return ENXIO;
fd = device_lookup_private(&fd_cd, FDUNIT(dev));
fdc = device_private(device_parent(fd->sc_dev));
switch (cmd) {
case DIOCGDINFO:
*(struct disklabel *)addr = *(fd->sc_dk.dk_label);
return 0;
case DIOCWLABEL:
if ((flag & FWRITE) == 0)
return EBADF;
return 0;
case DIOCWDINFO:
if ((flag & FWRITE) == 0)
return EBADF;
error = setdisklabel(fd->sc_dk.dk_label,
(struct disklabel *)addr, 0,
fd->sc_dk.dk_cpulabel);
if (error)
return error;
error = writedisklabel(dev, fdstrategy,
fd->sc_dk.dk_label,
fd->sc_dk.dk_cpulabel);
return error;
case DIOCLOCK:
return 0;
case DIOCEJECT:
if (*(int *)addr == 0) {
int part = DISKPART(dev);
if ((fd->sc_dk.dk_openmask & ~(1 << part)) != 0 ||
fd->sc_dk.dk_bopenmask + fd->sc_dk.dk_copenmask !=
fd->sc_dk.dk_openmask) {
return EBUSY;
}
}
case ODIOCEJECT:
if (fdc->sc_flags & FDC_NOEJECT)
return EINVAL;
fd_do_eject(fd);
return 0;
case FDIOCGETFORMAT:
form_parms = (struct fdformat_parms *)addr;
form_parms->fdformat_version = FDFORMAT_VERSION;
form_parms->nbps = 128 * (1 << fd->sc_type->secsize);
form_parms->ncyl = fd->sc_type->cylinders;
form_parms->nspt = fd->sc_type->sectrac;
form_parms->ntrk = fd->sc_type->heads;
form_parms->stepspercyl = fd->sc_type->step;
form_parms->gaplen = fd->sc_type->gap2;
form_parms->fillbyte = fd->sc_type->fillbyte;
form_parms->interleave = fd->sc_type->interleave;
switch (fd->sc_type->rate) {
case FDC_500KBPS:
form_parms->xfer_rate = 500 * 1024;
break;
case FDC_300KBPS:
form_parms->xfer_rate = 300 * 1024;
break;
case FDC_250KBPS:
form_parms->xfer_rate = 250 * 1024;
break;
default:
return EINVAL;
}
return 0;
case FDIOCSETFORMAT:
if ((flag & FWRITE) == 0)
return EBADF;
form_parms = (struct fdformat_parms *)addr;
if (form_parms->fdformat_version != FDFORMAT_VERSION)
return EINVAL;
i = form_parms->nbps >> 7;
if ((form_parms->nbps & 0x7f) || ffs(i) == 0 ||
i & ~(1 << (ffs(i)-1)))
return EINVAL;
switch (form_parms->xfer_rate) {
case 500 * 1024:
fd->sc_type->rate = FDC_500KBPS;
break;
case 300 * 1024:
fd->sc_type->rate = FDC_300KBPS;
break;
case 250 * 1024:
fd->sc_type->rate = FDC_250KBPS;
break;
default:
return EINVAL;
}
if (form_parms->nspt > FD_MAX_NSEC ||
form_parms->fillbyte > 0xff ||
form_parms->interleave > 0xff)
return EINVAL;
fd->sc_type->sectrac = form_parms->nspt;
if (form_parms->ntrk != 2 && form_parms->ntrk != 1)
return EINVAL;
fd->sc_type->heads = form_parms->ntrk;
fd->sc_type->seccyl = form_parms->nspt * form_parms->ntrk;
fd->sc_type->secsize = ffs(i)-1;
fd->sc_type->gap2 = form_parms->gaplen;
fd->sc_type->cylinders = form_parms->ncyl;
fd->sc_type->size = fd->sc_type->seccyl * form_parms->ncyl *
form_parms->nbps / DEV_BSIZE;
fd->sc_type->step = form_parms->stepspercyl;
fd->sc_type->fillbyte = form_parms->fillbyte;
fd->sc_type->interleave = form_parms->interleave;
return 0;
case FDIOCFORMAT_TRACK:
if((flag & FWRITE) == 0)
return EBADF;
form_cmd = (struct fdformat_cmd *)addr;
if (form_cmd->formatcmd_version != FDFORMAT_VERSION)
return EINVAL;
if (form_cmd->head >= fd->sc_type->heads ||
form_cmd->cylinder >= fd->sc_type->cylinders) {
return EINVAL;
}
fd_formb = kmem_alloc(sizeof(*fd_formb), KM_SLEEP);
fd_formb->head = form_cmd->head;
fd_formb->cyl = form_cmd->cylinder;
fd_formb->transfer_rate = fd->sc_type->rate;
fd_formb->fd_formb_secshift = fd->sc_type->secsize;
fd_formb->fd_formb_nsecs = fd->sc_type->sectrac;
fd_formb->fd_formb_gaplen = fd->sc_type->gap2;
fd_formb->fd_formb_fillbyte = fd->sc_type->fillbyte;
memset(il, 0, sizeof il);
for (j = 0, i = 1; i <= fd_formb->fd_formb_nsecs; i++) {
while (il[(j % fd_formb->fd_formb_nsecs) + 1])
j++;
il[(j % fd_formb->fd_formb_nsecs) + 1] = i;
j += fd->sc_type->interleave;
}
for (i = 0; i < fd_formb->fd_formb_nsecs; i++) {
fd_formb->fd_formb_cylno(i) = form_cmd->cylinder;
fd_formb->fd_formb_headno(i) = form_cmd->head;
fd_formb->fd_formb_secno(i) = il[i + 1];
fd_formb->fd_formb_secsize(i) = fd->sc_type->secsize;
}
error = fdformat(dev, fd_formb, l->l_proc);
kmem_free(fd_formb, sizeof(*fd_formb));
return error;
case FDIOCGETOPTS:
*(int *)addr = fd->sc_opts;
return 0;
case FDIOCSETOPTS:
fd->sc_opts = *(int *)addr;
return 0;
#ifdef FD_DEBUG
case _IO('f', 100):
fdc_wrfifo(fdc, NE7CMD_DUMPREG);
fdcresult(fdc);
printf("fdc: dumpreg(%d regs): <", fdc->sc_nstat);
for (i = 0; i < fdc->sc_nstat; i++)
printf(" 0x%x", fdc->sc_status[i]);
printf(">\n");
return 0;
case _IOW('f', 101, int):
fdc->sc_cfg &= ~CFG_THRHLD_MASK;
fdc->sc_cfg |= (*(int *)addr & CFG_THRHLD_MASK);
fdconf(fdc);
return 0;
case _IO('f', 102):
fdc_wrfifo(fdc, NE7CMD_SENSEI);
fdcresult(fdc);
printf("fdc: sensei(%d regs): <", fdc->sc_nstat);
for (i=0; i< fdc->sc_nstat; i++)
printf(" 0x%x", fdc->sc_status[i]);
printf(">\n");
return 0;
#endif
default:
return ENOTTY;
}
#ifdef DIAGNOSTIC
panic("fdioctl: impossible");
#endif
}
int
fdformat(dev_t dev, struct ne7_fd_formb *finfo, struct proc *p)
{
int rv = 0;
struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
struct fd_type *type = fd->sc_type;
struct buf *bp;
bp = getiobuf(NULL, false);
if (bp == NULL)
return ENOBUFS;
bp->b_vp = NULL;
bp->b_cflags = BC_BUSY;
bp->b_flags = B_PHYS | B_FORMAT;
bp->b_proc = p;
bp->b_dev = dev;
bp->b_blkno = ((finfo->cyl * (type->sectrac * type->heads)
+ finfo->head * type->sectrac) * FD_BSIZE(fd))
/ DEV_BSIZE;
bp->b_bcount = sizeof(struct fd_idfield_data) * finfo->fd_formb_nsecs;
bp->b_data = (void *)finfo;
#ifdef FD_DEBUG
if (fdc_debug) {
int i;
printf("fdformat: blkno 0x%llx count %d\n",
(unsigned long long)bp->b_blkno, bp->b_bcount);
printf("\tcyl:\t%d\n", finfo->cyl);
printf("\thead:\t%d\n", finfo->head);
printf("\tnsecs:\t%d\n", finfo->fd_formb_nsecs);
printf("\tsshft:\t%d\n", finfo->fd_formb_secshift);
printf("\tgaplen:\t%d\n", finfo->fd_formb_gaplen);
printf("\ttrack data:");
for (i = 0; i < finfo->fd_formb_nsecs; i++) {
printf(" [c%d h%d s%d]",
finfo->fd_formb_cylno(i),
finfo->fd_formb_headno(i),
finfo->fd_formb_secno(i) );
if (finfo->fd_formb_secsize(i) != 2)
printf("<sz:%d>", finfo->fd_formb_secsize(i));
}
printf("\n");
}
#endif
fdstrategy(bp);
rv = biowait(bp);
putiobuf(bp);
return rv;
}
void
fdgetdisklabel(dev_t dev)
{
int i;
struct fd_softc *fd = device_lookup_private(&fd_cd, FDUNIT(dev));
struct disklabel *lp = fd->sc_dk.dk_label;
struct cpu_disklabel *clp = fd->sc_dk.dk_cpulabel;
memset(lp, 0, sizeof(struct disklabel));
memset(clp, 0, sizeof(struct cpu_disklabel));
lp->d_type = DKTYPE_FLOPPY;
lp->d_secsize = FD_BSIZE(fd);
lp->d_secpercyl = fd->sc_type->seccyl;
lp->d_nsectors = fd->sc_type->sectrac;
lp->d_ncylinders = fd->sc_type->cylinders;
lp->d_ntracks = fd->sc_type->heads;
lp->d_secperunit = lp->d_secpercyl * lp->d_ncylinders;
lp->d_rpm = 300;
strncpy(lp->d_typename, "floppy disk", sizeof(lp->d_typename));
strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
lp->d_interleave = 1;
lp->d_flags = D_REMOVABLE;
lp->d_partitions[RAW_PART].p_offset = 0;
lp->d_partitions[RAW_PART].p_size = lp->d_secpercyl * lp->d_ncylinders;
lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
lp->d_npartitions = RAW_PART + 1;
lp->d_magic = DISKMAGIC;
lp->d_magic2 = DISKMAGIC;
lp->d_checksum = dkcksum(lp);
if (readdisklabel(dev, fdstrategy, lp, clp) != NULL) {
strncpy(lp->d_packname, "default label",
sizeof(lp->d_packname));
for (i = 0; i < MAXPARTITIONS; ++i) {
lp->d_partitions[i].p_offset = 0;
if (i == RAW_PART) {
lp->d_partitions[i].p_size =
lp->d_secpercyl * lp->d_ncylinders;
lp->d_partitions[i].p_fstype = FS_BSDFFS;
} else {
lp->d_partitions[i].p_size = 0;
lp->d_partitions[i].p_fstype = FS_UNUSED;
}
}
lp->d_npartitions = RAW_PART + 1;
}
}
void
fd_do_eject(struct fd_softc *fd)
{
struct fdc_softc *fdc = device_private(device_parent(fd->sc_dev));
#ifdef SUN4
if (CPU_ISSUN4C) {
auxregbisc(AUXIO4C_FDS, AUXIO4C_FEJ);
delay(10);
auxregbisc(AUXIO4C_FEJ, AUXIO4C_FDS);
return;
}
if (CPU_ISSUN4M && (fdc->sc_flags & FDC_82077) != 0) {
#endif
bus_space_tag_t t = fdc->sc_bustag;
bus_space_handle_t h = fdc->sc_handle;
uint8_t dor = FDO_FRST | FDO_FDMAEN | FDO_MOEN(0);
bus_space_write_1(t, h, fdc->sc_reg_dor, dor | FDO_EJ);
delay(10);
bus_space_write_1(t, h, fdc->sc_reg_dor, FDO_FRST | FDO_DS);
return;
#ifdef SUN4
}
#endif
}
void
fd_mountroot_hook(device_t dev)
{
int c;
fd_do_eject((struct fd_softc *)dev);
printf("Insert filesystem floppy and press return.");
for (;;) {
c = cngetc();
if ((c == '\r') || (c == '\n')) {
printf("\n");
break;
}
}
}
#ifdef MEMORY_DISK_HOOKS
#define FDMICROROOTSIZE ((2*18*80) << DEV_BSHIFT)
int
fd_read_md_image(size_t *sizep, void **addrp)
{
struct buf buf, *bp = &buf;
dev_t dev;
off_t offset;
char *addr;
dev = makedev(54,0);
addr = kmem_alloc(FDMICROROOTSIZE, KM_SLEEP);
*addrp = addr;
if (fdopen(dev, 0, S_IFCHR, NULL))
panic("fd: mountroot: fdopen");
offset = 0;
for (;;) {
bp->b_dev = dev;
bp->b_error = 0;
bp->b_resid = 0;
bp->b_proc = NULL;
bp->b_cflags = BC_BUSY;
bp->b_flags = B_PHYS | B_RAW | B_READ;
bp->b_blkno = btodb(offset);
bp->b_bcount = DEV_BSIZE;
bp->b_data = addr;
fdstrategy(bp);
biowait(bp);
if (bp->b_error)
panic("fd: mountroot: fdread error %d", bp->b_error);
if (bp->b_resid != 0)
break;
addr += DEV_BSIZE;
offset += DEV_BSIZE;
if (offset + DEV_BSIZE > FDMICROROOTSIZE)
break;
}
(void)fdclose(dev, 0, S_IFCHR, NULL);
*sizep = offset;
fd_do_eject(device_lookup_private(&fd_cd, FDUNIT(dev)));
return 0;
}
#endif
static void
fd_set_geometry(struct fd_softc *fd)
{
const struct fd_type *fdt;
fdt = fd->sc_type;
if (fdt == NULL) {
fdt = fd->sc_deftype;
if (fdt == NULL)
return;
}
struct disk_geom *dg = &fd->sc_dk.dk_geom;
memset(dg, 0, sizeof(*dg));
dg->dg_secperunit = fdt->size;
dg->dg_nsectors = fdt->sectrac;
switch (fdt->secsize) {
case 2:
dg->dg_secsize = 512;
break;
case 3:
dg->dg_secsize = 1024;
break;
default:
break;
}
dg->dg_ntracks = fdt->heads;
dg->dg_ncylinders = fdt->cylinders;
disk_set_info(fd->sc_dev, &fd->sc_dk, NULL);
}