#ifndef lint
#include <sys/cdefs.h>
__RCSID("$NetBSD: util.c,v 1.7 2009/04/17 04:03:39 lukem Exp $");
#endif
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <dev/ic/mlxreg.h>
#include <dev/ic/mlxio.h>
#include <dev/scsipi/scsipi_all.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <err.h>
#include "extern.h"
int
mlx_command(struct mlx_usercommand *mu, int bomb)
{
int rv;
if ((rv = ioctl(mlxfd, MLX_COMMAND, mu)) != 0 && bomb)
err(EXIT_FAILURE, "cmd 0x%02x failed",
mu->mu_command[0]);
return (rv);
}
void
mlx_enquiry(struct mlx_enquiry2 *enq)
{
struct mlx_usercommand mu;
struct mlx_enquiry_old meo;
memset(&mu, 0, sizeof(mu));
mu.mu_datasize = sizeof(*enq);
mu.mu_buf = enq;
mu.mu_bufptr = 8;
mu.mu_bufdir = MU_XFER_IN;
mu.mu_command[0] = MLX_CMD_ENQUIRY2;
mlx_command(&mu, 1);
if (enq->me_firmware_id[0] == 0) {
memset(&mu, 0, sizeof(mu));
mu.mu_datasize = sizeof(meo);
mu.mu_buf = &meo;
mu.mu_bufptr = 8;
mu.mu_bufdir = MU_XFER_IN;
mu.mu_command[0] = MLX_CMD_ENQUIRY_OLD;
mlx_command(&mu, 1);
enq->me_firmware_id[0] = meo.me_fwmajor;
enq->me_firmware_id[1] = meo.me_fwminor;
enq->me_firmware_id[2] = 0;
enq->me_firmware_id[3] = '0';
}
}
void
mlx_configuration(struct mlx_core_cfg *cfg, int wr)
{
struct mlx_usercommand mu;
memset(&mu, 0, sizeof(mu));
mu.mu_datasize = sizeof(*cfg);
mu.mu_buf = cfg;
mu.mu_bufptr = 8;
mu.mu_bufdir = (wr ? MU_XFER_OUT : MU_XFER_IN);
mu.mu_command[0] = (wr ? MLX_CMD_WRITE_CONFIG : MLX_CMD_READ_CONFIG);
mlx_command(&mu, 1);
}
int
mlx_get_device_state(int chan, int targ, struct mlx_phys_drv *pd)
{
struct mlx_usercommand mu;
memset(&mu, 0, sizeof(mu));
mu.mu_datasize = sizeof(*pd);
mu.mu_buf = pd;
mu.mu_bufptr = 8;
mu.mu_bufdir = MU_XFER_IN;
mu.mu_command[0] = MLX_CMD_DEVICE_STATE;
mu.mu_command[2] = chan;
mu.mu_command[3] = targ;
return (mlx_command(&mu, 0));
}
int
mlx_scsi_inquiry(int chan, int targ, char **vendor, char **device,
char **revision)
{
struct mlx_usercommand mu;
static struct {
struct mlx_dcdb dcdb;
struct scsipi_inquiry_data inq;
} __packed dcdb_cmd;
struct scsipi_inquiry *inq_cmd;
int rv;
inq_cmd = (struct scsipi_inquiry *)&dcdb_cmd.dcdb.dcdb_cdb[0];
memset(&mu, 0, sizeof(mu));
mu.mu_datasize = sizeof(dcdb_cmd);
mu.mu_buf = &dcdb_cmd;
mu.mu_command[0] = MLX_CMD_DIRECT_CDB;
mu.mu_bufdir = MU_XFER_IN | MU_XFER_OUT;
memset(&dcdb_cmd, 0, sizeof(dcdb_cmd));
dcdb_cmd.dcdb.dcdb_target = (chan << 4) | targ;
dcdb_cmd.dcdb.dcdb_flags = MLX_DCDB_DATA_IN | MLX_DCDB_TIMEOUT_10S;
dcdb_cmd.dcdb.dcdb_datasize = sizeof(dcdb_cmd.inq);
dcdb_cmd.dcdb.dcdb_length = 6;
dcdb_cmd.dcdb.dcdb_sense_length = 40;
inq_cmd->opcode = INQUIRY;
inq_cmd->length = sizeof(dcdb_cmd.inq);
if ((rv = mlx_command(&mu, 0)) == 0) {
*vendor = &dcdb_cmd.inq.vendor[0];
*device = &dcdb_cmd.inq.product[0];
*revision = &dcdb_cmd.inq.revision[0];
}
return (rv);
}
void
mlx_print_phys_drv(struct mlx_phys_drv *pd, int chn, int targ,
const char *prefix)
{
const char *type;
char *device, *vendor, *revision;
switch (pd->pd_flags2 & 0x03) {
case MLX_PHYS_DRV_DISK:
type = "disk";
break;
case MLX_PHYS_DRV_SEQUENTIAL:
type = "tape";
break;
case MLX_PHYS_DRV_CDROM:
type= "cdrom";
break;
case MLX_PHYS_DRV_OTHER:
default:
type = "unknown";
break;
}
printf("%s%s%02d%02d ", prefix, type, chn, targ);
switch (pd->pd_status) {
case MLX_PHYS_DRV_DEAD:
printf(" (dead) ");
break;
case MLX_PHYS_DRV_WRONLY:
printf(" (write-only) ");
break;
case MLX_PHYS_DRV_ONLINE:
printf(" (online) ");
break;
case MLX_PHYS_DRV_STANDBY:
printf(" (standby) ");
break;
default:
printf(" (0x%02x) ", pd->pd_status);
break;
}
printf("\n");
if (verbosity == 0)
return;
printf("%s ", prefix);
if (!mlx_scsi_inquiry(chn, targ, &vendor, &device, &revision))
printf("'%8.8s' '%16.16s' '%4.4s'", vendor, device, revision);
else
printf("<IDENTIFY FAILED>");
printf(" %dMB ", pd->pd_config_size / 2048);
if ((pd->pd_flags2 & MLX_PHYS_DRV_FAST20) != 0)
printf(" ultra");
else if ((pd->pd_flags2 & MLX_PHYS_DRV_FAST) != 0)
printf(" fast");
if ((pd->pd_flags2 & MLX_PHYS_DRV_WIDE) != 0)
printf(" wide");
if ((pd->pd_flags2 & MLX_PHYS_DRV_SYNC) != 0)
printf(" sync");
if ((pd->pd_flags2 & MLX_PHYS_DRV_TAG) != 0)
printf(" tag-enabled");
printf("\n");
}