#include <sys/param.h>
#include <sys/systm.h>
#include <sys/module.h>
#include <sys/bus.h>
#include <sys/malloc.h>
#include <sys/thread2.h>
#include <machine/clock.h>
#include <bus/cam/cam.h>
#include <bus/cam/cam_ccb.h>
#include <bus/cam/cam_sim.h>
#include <bus/cam/cam_xpt.h>
#include <bus/cam/cam_xpt_sim.h>
#include <bus/cam/cam_xpt_periph.h>
#include <bus/cam/cam_debug.h>
#include <bus/cam/cam_periph.h>
#include <bus/cam/scsi/scsi_all.h>
#include <bus/cam/scsi/scsi_message.h>
#include <bus/cam/scsi/scsi_da.h>
#include <sys/kernel.h>
#include "opt_vpo.h"
#include <bus/ppbus/ppbconf.h>
#include "vpoio.h"
#include "ppbus_if.h"
struct vpo_sense {
struct scsi_sense cmd;
unsigned int stat;
unsigned int count;
};
struct vpo_data {
unsigned short vpo_unit;
int vpo_stat;
int vpo_count;
int vpo_error;
int vpo_isplus;
struct cam_sim *sim;
struct vpo_sense vpo_sense;
struct vpoio_data vpo_io;
};
#define DEVTOSOFTC(dev) \
((struct vpo_data *)device_get_softc(dev))
static void vpo_action(struct cam_sim *sim, union ccb *ccb);
static void vpo_poll(struct cam_sim *sim);
static void vpo_cam_rescan_callback(struct cam_periph *periph,
union ccb *ccb);
static void vpo_cam_rescan(struct vpo_data *vpo);
static int
vpo_probe(device_t dev)
{
struct vpo_data *vpo;
int error;
vpo = DEVTOSOFTC(dev);
bzero(vpo, sizeof(struct vpo_data));
vpo->vpo_unit = device_get_unit(dev);
vpoio_set_unit(&vpo->vpo_io, vpo->vpo_unit);
if ((error = vpoio_probe(dev, &vpo->vpo_io)) == 0) {
vpo->vpo_isplus = 0;
device_set_desc(dev,
"Iomega VPI0 Parallel to SCSI interface");
} else if ((error = imm_probe(dev, &vpo->vpo_io)) == 0) {
vpo->vpo_isplus = 1;
device_set_desc(dev,
"Iomega Matchmaker Parallel to SCSI interface");
} else {
return (error);
}
return (0);
}
static int
vpo_attach(device_t dev)
{
struct vpo_data *vpo = DEVTOSOFTC(dev);
struct cam_devq *devq;
int error;
if (vpo->vpo_isplus) {
if ((error = imm_attach(&vpo->vpo_io)))
return (error);
} else {
if ((error = vpoio_attach(&vpo->vpo_io)))
return (error);
}
devq = cam_simq_alloc(1);
if (devq == NULL)
return (ENXIO);
vpo->sim = cam_sim_alloc(vpo_action, vpo_poll, "vpo", vpo,
device_get_unit(dev), &sim_mplock,
1, 0, devq);
cam_simq_release(devq);
if (vpo->sim == NULL) {
return (ENXIO);
}
if (xpt_bus_register(vpo->sim, 0) != CAM_SUCCESS) {
cam_sim_free(vpo->sim);
return (ENXIO);
}
vpo_cam_rescan(vpo);
return (0);
}
static void
vpo_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
{
xpt_free_ccb(&ccb->ccb_h);
}
static void
vpo_cam_rescan(struct vpo_data *vpo)
{
struct cam_path *path;
union ccb *ccb;
ccb = xpt_alloc_ccb();
if (xpt_create_path(&path, xpt_periph, cam_sim_path(vpo->sim), 0, 0)
!= CAM_REQ_CMP) {
return;
}
xpt_setup_ccb(&ccb->ccb_h, path, 5);
ccb->ccb_h.func_code = XPT_SCAN_BUS;
ccb->ccb_h.cbfcnp = vpo_cam_rescan_callback;
ccb->crcn.flags = CAM_FLAG_NONE;
xpt_action(ccb);
}
static void
vpo_intr(struct vpo_data *vpo, struct ccb_scsiio *csio)
{
int error;
#ifdef VP0_DEBUG
int i;
#endif
crit_enter();
if (vpo->vpo_isplus) {
error = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
csio->ccb_h.target_id,
(char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
(char *)csio->data_ptr, csio->dxfer_len,
&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
} else {
error = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
csio->ccb_h.target_id,
(char *)&csio->cdb_io.cdb_bytes, csio->cdb_len,
(char *)csio->data_ptr, csio->dxfer_len,
&vpo->vpo_stat, &vpo->vpo_count, &vpo->vpo_error);
}
#ifdef VP0_DEBUG
kprintf("vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
error, vpo->vpo_stat, vpo->vpo_count, vpo->vpo_error);
for (i=0; i<csio->cdb_len; i++)
kprintf("%x ", ((char *)&csio->cdb_io.cdb_bytes)[i]);
kprintf("\n");
#endif
if (error) {
csio->ccb_h.status = CAM_CMD_TIMEOUT;
goto error;
}
if (vpo->vpo_error) {
if (vpo->vpo_error != VP0_ESELECT_TIMEOUT)
kprintf("vpo%d: VP0 error/timeout (%d)\n",
vpo->vpo_unit, vpo->vpo_error);
csio->ccb_h.status = CAM_CMD_TIMEOUT;
goto error;
}
if (vpo->vpo_stat != SCSI_STATUS_OK) {
csio->scsi_status = vpo->vpo_stat;
if ((vpo->vpo_stat & SCSI_STATUS_CHECK_COND) != 0) {
vpo->vpo_sense.cmd.opcode = REQUEST_SENSE;
vpo->vpo_sense.cmd.length = csio->sense_len;
vpo->vpo_sense.cmd.control = 0;
if (vpo->vpo_isplus) {
error = imm_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
csio->ccb_h.target_id,
(char *)&vpo->vpo_sense.cmd,
sizeof(vpo->vpo_sense.cmd),
(char *)&csio->sense_data, csio->sense_len,
&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
&vpo->vpo_error);
} else {
error = vpoio_do_scsi(&vpo->vpo_io, VP0_INITIATOR,
csio->ccb_h.target_id,
(char *)&vpo->vpo_sense.cmd,
sizeof(vpo->vpo_sense.cmd),
(char *)&csio->sense_data, csio->sense_len,
&vpo->vpo_sense.stat, &vpo->vpo_sense.count,
&vpo->vpo_error);
}
#ifdef VP0_DEBUG
kprintf("(sense) vpo_do_scsi = %d, status = 0x%x, count = %d, vpo_error = %d\n",
error, vpo->vpo_sense.stat, vpo->vpo_sense.count, vpo->vpo_error);
#endif
if (error == 0 && vpo->vpo_sense.stat == SCSI_STATUS_OK) {
csio->ccb_h.status = CAM_AUTOSNS_VALID | CAM_SCSI_STATUS_ERROR;
csio->sense_resid = csio->sense_len - vpo->vpo_sense.count;
#ifdef VP0_DEBUG
kprintf("(sense) ");
for (i=0; i<vpo->vpo_sense.count; i++)
kprintf("%x ", ((char *)&csio->sense_data)[i]);
kprintf("\n");
#endif
} else {
csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
}
} else {
csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
}
goto error;
}
csio->resid = csio->dxfer_len - vpo->vpo_count;
csio->ccb_h.status = CAM_REQ_CMP;
error:
crit_exit();
return;
}
static void
vpo_action(struct cam_sim *sim, union ccb *ccb)
{
struct vpo_data *vpo = (struct vpo_data *)sim->softc;
switch (ccb->ccb_h.func_code) {
case XPT_SCSI_IO:
{
struct ccb_scsiio *csio;
csio = &ccb->csio;
#ifdef VP0_DEBUG
kprintf("vpo%d: XPT_SCSI_IO (0x%x) request\n",
vpo->vpo_unit, csio->cdb_io.cdb_bytes[0]);
#endif
vpo_intr(vpo, csio);
xpt_done(ccb);
break;
}
case XPT_CALC_GEOMETRY:
{
struct ccb_calc_geometry *ccg;
ccg = &ccb->ccg;
#ifdef VP0_DEBUG
kprintf("vpo%d: XPT_CALC_GEOMETRY (bs=%d,vs=%ju,c=%d,h=%d,spt=%d) request\n",
vpo->vpo_unit,
ccg->block_size,
(uintmax_t)ccg->volume_size,
ccg->cylinders,
ccg->heads,
ccg->secs_per_track);
#endif
ccg->heads = 64;
ccg->secs_per_track = 32;
ccg->cylinders = ccg->volume_size /
(ccg->heads * ccg->secs_per_track);
ccb->ccb_h.status = CAM_REQ_CMP;
xpt_done(ccb);
break;
}
case XPT_RESET_BUS:
{
#ifdef VP0_DEBUG
kprintf("vpo%d: XPT_RESET_BUS request\n", vpo->vpo_unit);
#endif
if (vpo->vpo_isplus) {
if (imm_reset_bus(&vpo->vpo_io)) {
ccb->ccb_h.status = CAM_REQ_CMP_ERR;
xpt_done(ccb);
return;
}
} else {
if (vpoio_reset_bus(&vpo->vpo_io)) {
ccb->ccb_h.status = CAM_REQ_CMP_ERR;
xpt_done(ccb);
return;
}
}
ccb->ccb_h.status = CAM_REQ_CMP;
xpt_done(ccb);
break;
}
case XPT_PATH_INQ:
{
struct ccb_pathinq *cpi = &ccb->cpi;
#ifdef VP0_DEBUG
kprintf("vpo%d: XPT_PATH_INQ request\n", vpo->vpo_unit);
#endif
cpi->version_num = 1;
cpi->hba_inquiry = 0;
cpi->target_sprt = 0;
cpi->hba_misc = 0;
cpi->hba_eng_cnt = 0;
cpi->max_target = 7;
cpi->max_lun = 0;
cpi->initiator_id = VP0_INITIATOR;
cpi->bus_id = sim->bus_id;
cpi->base_transfer_speed = 93;
strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
strncpy(cpi->hba_vid, "Iomega", HBA_IDLEN);
strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
cpi->unit_number = sim->unit_number;
cpi->ccb_h.status = CAM_REQ_CMP;
xpt_done(ccb);
break;
}
default:
ccb->ccb_h.status = CAM_REQ_INVALID;
xpt_done(ccb);
break;
}
return;
}
static void
vpo_poll(struct cam_sim *sim)
{
return;
}
static devclass_t vpo_devclass;
static device_method_t vpo_methods[] = {
DEVMETHOD(device_identify, bus_generic_identify),
DEVMETHOD(device_probe, vpo_probe),
DEVMETHOD(device_attach, vpo_attach),
DEVMETHOD_END
};
static driver_t vpo_driver = {
"vpo",
vpo_methods,
sizeof(struct vpo_data),
};
DRIVER_MODULE(vpo, ppbus, vpo_driver, vpo_devclass, NULL, NULL);