Subject: control tool for amr(4)
To: None <tech-kern@netbsd.org>
From: Manuel Bouyer <bouyer@antioche.eu.org>
List: tech-kern
Date: 06/24/2006 01:17:46
--AhhlLboLdkugWU4S
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi,
attached are diffs to support basic ioctl interface to the amr(4) driver,
and a port of the FreeBSD amrstat tool (diff against netbsd-3).
Works fine on my system:
leandre:/home/bouyer>./amrstat -g
Product                 <PERC 4e/Di>
Firmware                521X
BIOS                    H430
SCSI channels           2
Fibre loops             0
Memory size             256 MB
Battery status          charge done
Logical volume 0        optimal (341.21 GB, RAID5)
Physical drive 0:0      online
Physical drive 0:1      online
Physical drive 0:2      online
Physical drive 0:3      online
Physical drive 0:4      online
Physical drive 0:5      online

I'd like to get this in the tree and pulled up to netbsd-3.
Comments or objections ?

About amrstat.c: it's so small:
leandre:/home/bouyer>size amrstat
   text    data     bss     dec     hex filename
   8910     648    1408   10966    2ad6 amrstat

that adding it to pkgsrc seems overkill to me; it looks like it could
be in base ... 

-- 
Manuel Bouyer <bouyer@antioche.eu.org>
     NetBSD: 26 ans d'experience feront toujours la difference
--

--AhhlLboLdkugWU4S
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="amrstat.c"

/*-
 * Copyright (c) 2002, Pierre David <Pierre.David@crc.u-strasbg.fr>
 * Copyright (c) 2006, Jung-uk Kim <jkim@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice unmodified, this list of conditions, and the following
 *    disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>

#include <machine/param.h>

#define __packed	__attribute__((__packed__))

#include "amrio.h"
#include "amrreg.h"

#define NATTEMPTS	5
#define SLEEPTIME	100000	/* microseconds */

int	nattempts = NATTEMPTS;	/* # of attempts before giving up */
int	sleeptime = SLEEPTIME;	/* between attempts, in ms */

#define AMR_BUFSIZE	1024

int	enq_result = AMR_STATUS_FAILED;
char	enq_buffer[AMR_BUFSIZE];

#define AMR_MAX_NCTRLS	16
#define AMR_MAX_NSDEVS	16

u_int8_t	nschan = 0;

/*
 * Include lookup tables, and a function to match a code to a string.
 *
 * XXX Lookup tables cannot be included, since they require symbols from
 * amrreg.h which need in turn the _KERNEL define.
 */

/* #define AMR_DEFINE_TABLES */
/* #include "amr_tables.h" */

/*
 * Offsets in an amr_user_ioctl.au_cmd [] array See amrio.h
 */

#define MB_COMMAND	0
#define MB_CHANNEL	1
#define MB_PARAM	2
#define MB_PAD		3
#define MB_DRIVE	4

#define FIRMWARE_40LD	1
#define FIRMWARE_8LD	2

static struct {
	char	*product;
	int	signature;
} prodtable[] = {
	{	"Series 431",			AMR_SIG_431	},
	{	"Series 438",			AMR_SIG_438	},
	{	"Series 762",			AMR_SIG_762	},
	{	"Integrated HP NetRAID (T5)",	AMR_SIG_T5	},
	{	"Series 466",			AMR_SIG_466	},
	{	"Series 467",			AMR_SIG_467	},
	{	"Integrated HP NetRAID (T7)",	AMR_SIG_T7	},
	{	"Series 490",			AMR_SIG_490	}
};

static struct {
	int	code;
	char	*ifyes, *ifno;
} proptable[] = {
	{	AMR_DRV_WRITEBACK,
		"writeback",		"write-through"		},
	{	AMR_DRV_READHEAD,
		"read-ahead",		"no-read-ahead"		},
	{	AMR_DRV_ADAPTIVE,
		"adaptative-io",	"no-adaptative-io"	}
};

static struct {
	int	code;
	char	*status;
} statetable[] = {
	{	AMR_DRV_OFFLINE,	"offline"	},
	{	AMR_DRV_DEGRADED,	"degraded"	},
	{	AMR_DRV_OPTIMAL,	"optimal"	},
	{	AMR_DRV_ONLINE,		"online"	},
	{	AMR_DRV_FAILED,		"failed"	},
	{	AMR_DRV_REBUILD,	"rebuild"	},
	{	AMR_DRV_HOTSPARE,	"hotspare"	}
};

static struct {
	u_int8_t	code;
	char		*status;
} battable[] = {
	{	AMR_BATT_MODULE_MISSING,	"not present"		},
	{	AMR_BATT_LOW_VOLTAGE,		"low voltage"		},
	{	AMR_BATT_TEMP_HIGH,		"high temperature"	},
	{	AMR_BATT_PACK_MISSING,		"pack missing"	},
	{	AMR_BATT_CYCLES_EXCEEDED,	"cycle exceeded"	}
};

static struct {
	u_int8_t	code;
	char		*status;
} bcstatble[] = {
	{	AMR_BATT_CHARGE_DONE,		"charge done"		},
	{	AMR_BATT_CHARGE_INPROG,		"charge in progress"	},
	{	AMR_BATT_CHARGE_FAIL,		"charge failed"		}
};

#define NTAB(tab)	(sizeof tab / sizeof tab [0])

int
amr_ioctl_enquiry(int fd, u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual)
{
	struct amr_user_ioctl am;
	int	r, i;

	am.au_cmd[MB_COMMAND] = cmd;
	am.au_cmd[MB_CHANNEL] = cmdsub;
	am.au_cmd[MB_PARAM] = cmdqual;
	am.au_cmd[MB_PAD] = 0;
	am.au_cmd[MB_DRIVE] = 0;

	am.au_buffer = enq_buffer;
	am.au_length = AMR_BUFSIZE;
	am.au_direction = AMR_IO_READ;
	am.au_status = 0;

	i = 0;
	r = -1;
	while (i < nattempts && r == -1) {
		r = ioctl(fd, AMR_IO_COMMAND, &am);
		if (r == -1) {
			if (errno != EBUSY) {
				perror("ioctl enquiry");
				exit(1);
			} else
				usleep(sleeptime);
		}
		i++;
	}
	return am.au_status;
}

void
usage(char *prog)
{
	fprintf(stderr, "usage: %s [-a num] [-b] "
		"[-c ctlr|-f dev] [-g] [-l vol]\n\t\t"
		"[-p drive|-s bus[:target]] [-t usec] [-v]\n\n\t"
		"-a num\t\tnumber of retries\n\t"
		"-b\t\tbattery status\n\t"
		"-c ctrl\t\tcontroller ID\n\t"
		"-f dev\t\tdevice path\n\t"
		"-g\t\tprint global parameters\n\t"
		"-l vol\t\tlogical volume ID\n\t"
		"-p drive\tphysical drive ID\n\t"
		"-s bus[:target]\tSCSI bus (and optinal target)\n\t"
		"-t usec\t\tsleep time between retries\n\t"
		"-v\t\tverbose output\n",
		prog);
	exit(1);
}

/******************************************************************************
 * Card description
 */

int
describe_card(int fd, int verbosity, int globalparam)
{
	struct amr_enquiry *ae;
	int	cardtype;

	/*
	 * Try the 40LD firmware interface
	 */

	enq_result = amr_ioctl_enquiry(fd, AMR_CMD_CONFIG,
		AMR_CONFIG_PRODUCT_INFO, 0);
	if (enq_result == AMR_STATUS_SUCCESS) {
		struct amr_prodinfo *ap;

		ap = (struct amr_prodinfo *)enq_buffer;
		nschan = ap->ap_nschan;
		if (globalparam) {
			printf("Product\t\t\t<%.80s>\n", ap->ap_product);
			printf("Firmware\t\t%.16s\n", ap->ap_firmware);
			printf("BIOS\t\t\t%.16s\n", ap->ap_bios);
			printf("SCSI channels\t\t%d\n", ap->ap_nschan);
			printf("Fibre loops\t\t%d\n", ap->ap_fcloops);
			printf("Memory size\t\t%d MB\n", ap->ap_memsize);
			if (verbosity >= 1) {
				printf("Ioctl\t\t\t%d (%s)\n", FIRMWARE_40LD,
				       "40LD");
				printf("Signature\t\t0x%08x\n",
				       ap->ap_signature);
				printf("Configsig\t\t0x%08x\n",
				       ap->ap_configsig);
				printf("Subsystem\t\t0x%04x\n",
				       ap->ap_subsystem);
				printf("Subvendor\t\t0x%04x\n",
				       ap->ap_subvendor);
				printf("Notify counters\t\t%d\n",
				       ap->ap_numnotifyctr);
			}
		}
		return FIRMWARE_40LD;
	}
	/*
	 * Try the 8LD firmware interface
	 */

	enq_result = amr_ioctl_enquiry(fd, AMR_CMD_EXT_ENQUIRY2, 0, 0);
	ae = (struct amr_enquiry *)enq_buffer;
	if (enq_result == AMR_STATUS_SUCCESS) {
		cardtype = ae->ae_signature;
	} else {
		enq_result = amr_ioctl_enquiry(fd, AMR_CMD_ENQUIRY, 0, 0);
		cardtype = 0;
	}

	if (enq_result == AMR_STATUS_SUCCESS) {

		if (globalparam) {
			char   *product = NULL;
			char	bios[100], firmware[100];
			int	i;

			for (i = 0; i < NTAB(prodtable); i++) {
				if (cardtype == prodtable[i].signature) {
					product = prodtable[i].product;
					break;
				}
			}
			if (product == NULL)
				product = "unknown card signature";

			/*
			 * HP NetRaid controllers have a special encoding of
			 * the firmware and BIOS versions. The AMI version
			 * seems to have it as strings whereas the HP version
			 * does it with a leading uppercase character and two
			 * binary numbers.
			 */

			if (ae->ae_adapter.aa_firmware[2] >= 'A' &&
			    ae->ae_adapter.aa_firmware[2] <= 'Z' &&
			    ae->ae_adapter.aa_firmware[1] < ' ' &&
			    ae->ae_adapter.aa_firmware[0] < ' ' &&
			    ae->ae_adapter.aa_bios[2] >= 'A' &&
			    ae->ae_adapter.aa_bios[2] <= 'Z' &&
			    ae->ae_adapter.aa_bios[1] < ' ' &&
			    ae->ae_adapter.aa_bios[0] < ' ') {

				/*
				 * looks like we have an HP NetRaid version
				 * of the MegaRaid
				 */

				if (cardtype == AMR_SIG_438) {
					/*
					 * the AMI 438 is a NetRaid 3si in
					 * HP-land
					 */
					product = "HP NetRaid 3si";
				}
				sprintf(firmware, "%c.%02d.%02d",
					ae->ae_adapter.aa_firmware[2],
					ae->ae_adapter.aa_firmware[1],
					ae->ae_adapter.aa_firmware[0]);
				sprintf(bios, "%c.%02d.%02d",
					ae->ae_adapter.aa_bios[2],
					ae->ae_adapter.aa_bios[1],
					ae->ae_adapter.aa_bios[0]);
			} else {
				sprintf(firmware, "%.4s",
					ae->ae_adapter.aa_firmware);
				sprintf(bios, "%.4s", ae->ae_adapter.aa_bios);
			}

			printf("Ioctl = %d (%s)\n", FIRMWARE_8LD, "8LD");
			printf("Product =\t<%s>\n", product);
			printf("Firmware =\t%s\n", firmware);
			printf("BIOS =\t%s\n", bios);
			/* printf ("SCSI Channels =\t%d\n", ae->ae_nschan); */
			/* printf ("Fibre Loops =\t%d\n", ae->ae_fcloops); */
			printf("Memory size =\t%d MB\n",
			       ae->ae_adapter.aa_memorysize);
			/*
			 * printf ("Notify counters =\t%d\n",
			 * ae->ae_numnotifyctr) ;
			 */
		}
		return FIRMWARE_8LD;
	}
	/*
	 * Neither firmware interface succeeded. Abort.
	 */

	fprintf(stderr, "Firmware interface not supported\n");
	exit(1);

}

char *
describe_property(u_int8_t prop, char *buffer)
{
	int	i;

	strcpy(buffer, "<");
	for (i = 0; i < NTAB(proptable); i++) {
		if (i > 0)
			strcat(buffer, ",");
		if (prop & proptable[i].code)
			strcat(buffer, proptable[i].ifyes);
		else
			strcat(buffer, proptable[i].ifno);
	}
	strcat(buffer, ">");

	return buffer;
}

char *
describe_state(int verbosity, u_int8_t state)
{
	int	i;

	if ((AMR_DRV_PREVSTATE(state) == AMR_DRV_CURSTATE(state)) &&
	    (AMR_DRV_CURSTATE(state) == AMR_DRV_OFFLINE) && verbosity == 0)
		return NULL;

	for (i = 0; i < NTAB(statetable); i++)
		if (AMR_DRV_CURSTATE(state) == statetable[i].code)
			return (statetable[i].status);

	return NULL;
}

/******************************************************************************
 * Battery status
 */
void
describe_battery(int fd, int verbosity, int fwint, int bflags, int globalparam)
{
	u_int8_t batt_status;
	int i;

	if (fwint == FIRMWARE_40LD) {
		enq_result = amr_ioctl_enquiry(fd, AMR_CMD_CONFIG,
			AMR_CONFIG_ENQ3, AMR_CONFIG_ENQ3_SOLICITED_FULL);
		if (enq_result == AMR_STATUS_SUCCESS) {
			struct amr_enquiry3 *ae3;

			ae3 = (struct amr_enquiry3 *)enq_buffer;
			if (bflags || globalparam) {
				batt_status = ae3->ae_batterystatus;
				printf("Battery status\t\t");
				for (i = 0; i < NTAB(battable); i++) {
					if (batt_status & battable[i].code)
						printf("%s, ", battable[i].status);
				}
				if (!(batt_status &
				    (AMR_BATT_MODULE_MISSING|AMR_BATT_PACK_MISSING))) {
					for (i = 0; i < NTAB(bcstatble); i++)
						if (bcstatble[i].code ==
						    (batt_status & AMR_BATT_CHARGE_MASK))
							printf("%s", bcstatble[i].status);
				} else
					printf("charge unknown");
				if (verbosity)
					printf(" (0x%02x)", batt_status);
				printf("\n");
			}
		}
	} else if (fwint == FIRMWARE_8LD) {
		/* Nothing to do here. */
		return;
	} else {
		fprintf(stderr, "Firmware interface not supported.\n");
		exit(1);
	}

	return;
}

/******************************************************************************
 * Logical volumes
 */

void
describe_one_volume(int ldrv, int verbosity,
		    u_int32_t size, u_int8_t state, u_int8_t prop)
{
	float	szgb;
	int	raid_level;
	char	propstr[MAXPATHLEN];
	char	*statestr;

	szgb = ((float)size) / (1024 * 1024 * 2);	/* size in GB */

	raid_level = prop & AMR_DRV_RAID_MASK;

	printf("Logical volume %d\t", ldrv);
	statestr = describe_state(verbosity, state);
	printf("%s ", statestr);
	printf("(%.2f GB, RAID%d", szgb, raid_level);
	if (verbosity >= 1) {
		describe_property(prop, propstr);
		printf(" %s", propstr);
	}
	printf(")\n");
}

/******************************************************************************
 * Physical drives
 */

void
describe_one_drive(int pdrv, int verbosity, u_int8_t state)
{
	char	*statestr;

	statestr = describe_state(verbosity, state);
	if (statestr) {
		if (nschan > 0)
			printf("Physical drive %d:%d\t%s\n",
			       pdrv / AMR_MAX_NSDEVS, pdrv % AMR_MAX_NSDEVS,
			       statestr);
		else
			printf("Physical drive %d:\t%s\n", pdrv, statestr);
	}
}

void
describe_drive(int verbosity, int fwint, int ldrv, int sbus, int sdev)
{
	int	drv, pdrv = -1;

	if (sbus > -1 && sdev > -1)
		pdrv = (sbus * AMR_MAX_NSDEVS) + sdev;
	if (nschan != 0) {
		if (sbus > -1 && sbus >= nschan) {
			fprintf(stderr, "SCSI channel %d does not exist.\n", sbus);
			exit(1);
		} else if (sdev > -1 && sdev >= AMR_MAX_NSDEVS) {
			fprintf(stderr, "SCSI device %d:%d does not exist.\n",
				sbus, sdev);
			exit(1);
		}
	}
	if (fwint == FIRMWARE_40LD) {
		if (enq_result == AMR_STATUS_SUCCESS) {
			struct amr_enquiry3 *ae3;

			ae3 = (struct amr_enquiry3 *)enq_buffer;
			if ((ldrv < 0 && sbus < 0) || ldrv >= 0) {
				if (ldrv >= ae3->ae_numldrives) {
					fprintf(stderr, "Logical volume %d "
						"does not exist.\n", ldrv);
					exit(1);
				}
				if (ldrv < 0) {
					for (drv = 0;
					     drv < ae3->ae_numldrives;
					     drv++)
						describe_one_volume(drv,
						    verbosity,
						    ae3->ae_drivesize[drv],
						    ae3->ae_drivestate[drv],
						    ae3->ae_driveprop[drv]);
				} else {
					describe_one_volume(ldrv,
					    verbosity,
					    ae3->ae_drivesize[ldrv],
					    ae3->ae_drivestate[ldrv],
					    ae3->ae_driveprop[ldrv]);
				}
			}
			if ((ldrv < 0 && sbus < 0) || sbus >= 0) {
				if (pdrv >= AMR_40LD_MAXPHYSDRIVES ||
				    (nschan != 0 && pdrv >= (nschan * AMR_MAX_NSDEVS))) {
					fprintf(stderr, "Physical drive %d "
						"is out of range.\n", pdrv);
					exit(1);
				}
				if (sbus < 0) {
					for (drv = 0;
					     drv < AMR_40LD_MAXPHYSDRIVES;
					     drv++) {
						if (nschan != 0 &&
						    drv >= (nschan * AMR_MAX_NSDEVS))
							break;
						describe_one_drive(drv,
						    verbosity,
						    ae3->ae_pdrivestate[drv]);
					}
				} else if (sdev < 0) {
					for (drv = sbus * AMR_MAX_NSDEVS;
					     drv < ((sbus + 1) * AMR_MAX_NSDEVS);
					     drv++) {
						if (nschan != 0 &&
						    drv >= (nschan * AMR_MAX_NSDEVS))
							break;
						describe_one_drive(drv,
						    verbosity,
						    ae3->ae_pdrivestate[drv]);
					}
				} else {
					if (nschan != 0 &&
					    pdrv < (nschan * AMR_MAX_NSDEVS))
						describe_one_drive(pdrv, 1,
						    ae3->ae_pdrivestate[pdrv]);
				}
			}
		}
	} else if (fwint == FIRMWARE_8LD) {
		/* Nothing to do here. */
		return;
	} else {
		fprintf(stderr, "Firmware interface not supported.\n");
		exit(1);
	}
}

/******************************************************************************
 * Main function
 */

int
main(int argc, char *argv[])
{
	int	i;
	int	fd = -1;
	int	globalparam = 0, verbosity = 0;
	int	bflags = 0, cflags = 0, fflags = 0, sflags = 0;
	int	ctrlno = 0, lvolno = -1, physno = -1;
	int	sbusno = -1, targetno = -1;
	char	filename[MAXPATHLEN];
	char	sdev[MAXPATHLEN];
	char	*pdev;

	extern char *optarg;
	extern int optind;

	/*
	 * Parse arguments
	 */

	while ((i = getopt(argc, argv, "a:bc:f:gl:p:s:t:v")) != -1)
		switch (i) {
		case 'a':
			nattempts = atoi(optarg);
			break;
		case 'b':
			bflags++;
			break;
		case 'c':
			ctrlno = atoi(optarg);
			cflags++;
			break;
		case 'f':
			snprintf(filename, MAXPATHLEN, "%s", optarg);
			filename[MAXPATHLEN - 1] = '\0';
			fflags++;
			break;
		case 'g':
			globalparam = 1;
			break;
		case 'l':
			lvolno = atoi(optarg);
			break;
		case 'p':
			physno = atoi(optarg);
			break;
		case 's':
			snprintf(sdev, MAXPATHLEN, "%s", optarg);
			sdev[MAXPATHLEN - 1] = '\0';
			sflags++;
			break;
		case 't':
			sleeptime = atoi(optarg);
			break;
		case 'v':
			verbosity++;
			break;
		case '?':
		default:
			usage(argv[0]);
		}
	argc -= optind;
	argv += optind;

	if (argc != 0)
		usage(argv[0]);

	if (cflags && fflags)
		usage(argv[0]);
	else if (cflags) {
		snprintf(filename, MAXPATHLEN, "/dev/amr%d", ctrlno);
		filename[MAXPATHLEN - 1] = '\0';
		fd = open(filename, O_RDONLY);
	} else if (fflags) {
		fd = open(filename, O_RDONLY);
	} else {
		for (ctrlno = 0; ctrlno < AMR_MAX_NCTRLS; ctrlno++) {
			snprintf(filename, MAXPATHLEN, "/dev/amr%d", ctrlno);
			filename[MAXPATHLEN - 1] = '\0';
			fd = open(filename, O_RDONLY);
			if (fd != -1)
				break;
		}
	}
	if (fd == -1) {
		perror("open");
		exit(1);
	}
	if (ioctl(fd, AMR_IO_VERSION, &i) == -1) {
		perror("ioctl version");
		exit(1);
	}

	if (sflags) {
		if(physno > -1)
			usage(argv[0]);
		else {
			sbusno = atoi(sdev);
			if ((pdev = index(sdev, ':')))
				targetno = atoi(++pdev);
		}
	} else if (physno > -1) {
		sbusno = physno / AMR_MAX_NSDEVS;
		targetno = physno % AMR_MAX_NSDEVS;
	}
	
	if (globalparam && verbosity >= 1)
		printf("Version\t\t\t%d\n", i);
#if 0
	if (i != 1) {
		fprintf(stderr, "Driver version (%d) not supported\n", i);
		exit(1);
	}
#endif

	i = describe_card(fd, verbosity, globalparam);
	describe_battery(fd, verbosity, i, bflags, globalparam);
	if (!bflags || lvolno > -1 || physno > -1 || sbusno > -1 || targetno > -1)
		describe_drive(verbosity, i, lvolno, sbusno, targetno);

	return 0;
}

--AhhlLboLdkugWU4S
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=diff

Index: conf/majors
===================================================================
RCS file: /cvsroot/src/sys/conf/majors,v
retrieving revision 1.13.8.1
diff -u -r1.13.8.1 majors
--- conf/majors	10 Jun 2005 15:28:49 -0000	1.13.8.1
+++ conf/majors	23 Jun 2006 23:10:23 -0000
@@ -20,3 +20,4 @@
 device-major	dk		char 168 block 168
 device-major	tap		char 169		tap
 device-major	veriexec	char 170		veriexec
+device-major	amr		char 171		amr
Index: dev/pci/amr.c
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/amr.c,v
retrieving revision 1.25.2.1
diff -u -r1.25.2.1 amr.c
--- dev/pci/amr.c	15 Dec 2005 20:08:01 -0000	1.25.2.1
+++ dev/pci/amr.c	23 Jun 2006 23:10:23 -0000
@@ -81,6 +81,7 @@
 #include <sys/proc.h>
 #include <sys/buf.h>
 #include <sys/malloc.h>
+#include <sys/conf.h>
 #include <sys/kthread.h>
 
 #include <uvm/uvm_extern.h>
@@ -92,6 +93,7 @@
 #include <dev/pci/pcivar.h>
 #include <dev/pci/amrreg.h>
 #include <dev/pci/amrvar.h>
+#include <dev/pci/amrio.h>
 
 #include "locators.h"
 
@@ -116,6 +118,11 @@
 int	amr_std_get_work(struct amr_softc *, struct amr_mailbox_resp *);
 int	amr_std_submit(struct amr_softc *, struct amr_ccb *);
 
+dev_type_open(amropen);
+dev_type_close(amrclose);
+dev_type_ioctl(amrioctl);
+
+
 static inline u_int8_t	amr_inb(struct amr_softc *, int);
 static inline u_int32_t	amr_inl(struct amr_softc *, int);
 static inline void	amr_outb(struct amr_softc *, int, u_int8_t);
@@ -124,6 +131,13 @@
 CFATTACH_DECL(amr, sizeof(struct amr_softc),
     amr_match, amr_attach, NULL, NULL);
 
+const struct cdevsw amr_cdevsw = {
+	amropen, amrclose, noread, nowrite, amrioctl,
+	nostop, notty, nopoll, nommap,
+};      
+
+extern struct   cfdriver amr_cd;
+
 #define AT_QUARTZ	0x01	/* `Quartz' chipset */
 #define	AT_SIG		0x02	/* Check for signature */
 
@@ -265,7 +279,7 @@
 	const char *intrstr;
 	pcireg_t reg;
 	int rseg, i, j, size, rv, memreg, ioreg;
-        struct amr_ccb *ac;
+	struct amr_ccb *ac;
 	int help[2];
 	locdesc_t *ldesc = (void *)help; /* XXX */
 
@@ -730,7 +744,7 @@
 void
 amr_shutdown(void *cookie)
 {
-        extern struct cfdriver amr_cd;
+	extern struct cfdriver amr_cd;
 	struct amr_softc *amr;
 	struct amr_ccb *ac;
 	int i, rv, s;
@@ -1316,3 +1330,110 @@
 		printf("%08x ", ((u_int32_t *)&ac->ac_cmd)[i]);
 	printf("\n");
 }
+
+int
+amropen(dev_t dev, int flag, int mode, struct proc *p)
+{
+	struct amr_softc *amr;
+	 
+	if ((amr = device_lookup(&amr_cd, minor(dev))) == NULL)
+		return (ENXIO);
+	if ((amr->amr_flags & AMRF_OPEN) != 0)
+		return (EBUSY);
+							  
+	amr->amr_flags |= AMRF_OPEN;
+	return (0);
+}
+
+int
+amrclose(dev_t dev, int flag, int mode, struct proc *p)
+{
+	struct amr_softc *amr;
+
+	amr = device_lookup(&amr_cd, minor(dev));
+	amr->amr_flags &= ~AMRF_OPEN;
+	return (0);
+}
+
+int
+amrioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
+{
+	struct amr_softc *amr;
+	struct amr_user_ioctl *au;
+	int *version;
+	struct amr_ccb *ac;
+	struct amr_mailbox_ioctl *mbi;
+	void *dp = NULL, *au_buffer;
+	unsigned long au_length;
+	unsigned char*au_cmd;
+	int error, s;
+
+	if (securelevel >= 2)
+		return (EPERM);
+
+	amr = device_lookup(&amr_cd, minor(dev));
+
+	/* This should be compatible with the FreeBSD interface */
+
+	switch(cmd) {
+	case AMR_IO_VERSION:
+		version = (void *)data;
+		*version = AMR_IO_VERSION_NUMBER;
+		return 0;
+	case AMR_IO_COMMAND:
+		au = (void *)data;
+		au_cmd = au->au_cmd;
+		au_buffer = au->au_buffer;
+		au_length = au->au_length;
+		break;
+	default:
+		return EINVAL;
+	}
+
+	if (au_cmd[0] == AMR_CMD_PASS) {
+		/* not yet */
+		return EOPNOTSUPP;
+	}
+
+	if (au_length != 0 && au_cmd[0] != 0x06) {
+		dp = malloc(au_length, M_DEVBUF, M_WAITOK|M_ZERO);
+		if (dp == NULL)
+			return ENOMEM;
+		if ((error = copyin(au_buffer, dp, au_length)) != 0) {
+			free(dp, M_DEVBUF);
+			return (error);
+		}
+	}
+	/* direct command to controller */
+	s = splbio();
+	while (amr_ccb_alloc(amr, &ac) != 0) {
+		error = tsleep(NULL, PRIBIO | PCATCH, "armmbx", hz);
+		if (error == EINTR)
+			goto out;
+	}
+			
+	mbi = (struct amr_mailbox_ioctl *)&ac->ac_cmd;
+	/* copy pertinent mailbox items */
+	mbi->mb_command = au_cmd[0];
+	mbi->mb_channel = au_cmd[1];
+	mbi->mb_param = au_cmd[2];
+	mbi->mb_pad[0] = au_cmd[3];
+	mbi->mb_drive = au_cmd[4];
+	/* build the command */
+	error = amr_ccb_map(amr, ac, dp, au_length, 0);
+	if (error)
+		goto out;
+	error = amr_ccb_wait(amr, ac);
+	amr_ccb_unmap(amr, ac);
+	amr_ccb_free(amr, ac);
+	if (error)
+		goto out;
+	if (au_length != 0) {
+		error = copyout(dp, au_buffer, au_length);
+	}
+out:
+	splx(s);
+	if (dp)
+		free(dp, M_DEVBUF);
+	return error;
+}
Index: dev/pci/amrio.h
===================================================================
RCS file: dev/pci/amrio.h
diff -N dev/pci/amrio.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ dev/pci/amrio.h	23 Jun 2006 23:10:23 -0000
@@ -0,0 +1,124 @@
+/* $NetBSD: $ */
+
+/*-
+ * Copyright (c) 1999 Michael Smith
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Copyright (c) 2002 Eric Moore
+ * Copyright (c) 2002 LSI Logic Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The party using or redistributing the source code and binary forms
+ *    agrees to the disclaimer below and the terms and conditions set forth
+ *    herein.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ *
+ *	FreeBSD: src/sys/dev/amr/amrio.h,v 1.7 2005/12/14 03:26:49 scottl Exp
+ */
+
+/*
+ * ioctl interface
+ */
+
+#include <sys/ioccom.h>
+#include <sys/param.h>
+
+/*
+ * Fetch the driver's interface version.
+ */
+#define AMR_IO_VERSION_NUMBER	153
+#define AMR_IO_VERSION	_IOR('A', 0x200, int)
+
+/*
+ * Pass a command from userspace through to the adapter.
+ *
+ * Note that in order to be code-compatible with the Linux
+ * interface where possible, the formatting of the au_cmd field is
+ * somewhat Interesting.
+ *
+ * For normal commands, the layout is (fields from struct amr_mailbox_ioctl):
+ *
+ * 0		mb_command
+ * 1		mb_channel
+ * 2		mb_param
+ * 3		mb_pad[0]
+ * 4		mb_drive
+ *
+ * For SCSI passthrough commands, the layout is:
+ *
+ * 0		AMR_CMD_PASS	(0x3)
+ * 1		reserved, 0
+ * 2		cdb length
+ * 3		cdb data
+ * 3+cdb_len	passthrough control byte (timeout, ars, islogical)
+ * 4+cdb_len	reserved, 0
+ * 5+cdb_len	channel
+ * 6+cdb_len	target
+ */
+
+struct amr_user_ioctl {
+    unsigned char	au_cmd[32];	/* command text from userspace */
+    void		*au_buffer;	/* data buffer in userspace */
+    unsigned long	au_length;	/* data buffer size (0 == no data) */
+    int			au_direction;	/* data transfer direction */
+#define AMR_IO_NODATA	0
+#define AMR_IO_READ	1
+#define AMR_IO_WRITE	2
+    int			au_status;	/* command status returned by adapter */
+};
+
+#define AMR_IO_COMMAND	_IOWR('A', 0x201, struct amr_user_ioctl)
+
+#if 0 /* defined(__amd64__) || defined(__ia64__) */
+
+struct amr_user_ioctl32 {
+    unsigned char	au_cmd[32];	/* command text from userspace */
+    u_int32_t		au_buffer;	/* 32-bit pointer to uspace buf */
+    u_int32_t		au_length;	/* length of the uspace buffer */
+    int32_t		au_direction;	/* data transfer direction */
+    int32_t		au_status;	/* command status returned by adapter */
+};
+
+#	define AMR_IO_COMMAND32	_IOWR('A', 0x201, struct amr_user_ioctl32)
+#endif
Index: dev/pci/amrreg.h
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/amrreg.h,v
retrieving revision 1.2
diff -u -r1.2 amrreg.h
--- dev/pci/amrreg.h	4 May 2003 16:15:36 -0000	1.2
+++ dev/pci/amrreg.h	23 Jun 2006 23:10:23 -0000
@@ -319,6 +319,7 @@
 	u_int32_t	ae_drivesize[AMR_40LD_MAXDRIVES];	/* logical drive size */
 	u_int8_t	ae_driveprop[AMR_40LD_MAXDRIVES];	/* logical drive properties */
 	u_int8_t	ae_drivestate[AMR_40LD_MAXDRIVES];	/* physical drive state */
+	u_int8_t	ae_pdrivestate[AMR_40LD_MAXPHYSDRIVES]; /* physical drive state */
 	u_int16_t	ae_driveformat[AMR_40LD_MAXPHYSDRIVES];
 	u_int8_t	ae_targxfer[80];			/* physical drive transfer rates */
 
Index: dev/pci/amrvar.h
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/amrvar.h,v
retrieving revision 1.4
diff -u -r1.4 amrvar.h
--- dev/pci/amrvar.h	13 Sep 2004 12:55:48 -0000	1.4
+++ dev/pci/amrvar.h	23 Jun 2006 23:10:23 -0000
@@ -107,6 +107,7 @@
 
 /* General flags. */
 #define	AMRF_THREAD_EXIT	0x00010000
+#define	AMRF_OPEN		0x00020000
 
 /*
  * Command control block.

--AhhlLboLdkugWU4S--