Subject: control tool for amr(4) (update)
To: None <tech-kern@NetBSD.org>
From: Manuel Bouyer <bouyer@antioche.eu.org>
List: tech-kern
Date: 07/02/2006 23:47:13
--Dxnq1zWXvFF0Q93v
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi,
here's a new diff for my changes to amr(4). I reintroduced the double-buffer
for the userland data, as it seems calling bus_dma() on a userland buffer
isn't that easy to do. I also changed amrstat to amrctl, with only
'amrctl stat' implemented for now.

Unless someone objects I'll commit this in 2 or 3 days.

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

--Dxnq1zWXvFF0Q93v
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="diff.amr"

Index: distrib/sets/lists/base/mi
===================================================================
RCS file: /cvsroot/src/distrib/sets/lists/base/mi,v
retrieving revision 1.636
diff -u -r1.636 mi
--- distrib/sets/lists/base/mi	27 Jun 2006 20:39:01 -0000	1.636
+++ distrib/sets/lists/base/mi	2 Jul 2006 21:21:17 -0000
@@ -111,6 +111,7 @@
 ./rescue					base-sys-root
 ./root						base-sys-root
 ./sbin						base-sys-root
+./sbin/amrctl					base-sysutil-root
 ./sbin/atactl					base-sysutil-root
 ./sbin/badsect					base-sysutil-root
 ./sbin/brconfig					base-netutil-root
Index: distrib/sets/lists/comp/mi
===================================================================
RCS file: /cvsroot/src/distrib/sets/lists/comp/mi,v
retrieving revision 1.894
diff -u -r1.894 mi
--- distrib/sets/lists/comp/mi	25 Jun 2006 21:49:14 -0000	1.894
+++ distrib/sets/lists/comp/mi	2 Jul 2006 21:21:21 -0000
@@ -370,6 +370,8 @@
 ./usr/include/dev/ofw/ofw_pci.h			comp-obsolete		obsolete
 ./usr/include/dev/ofw/openfirm.h		comp-obsolete		obsolete
 ./usr/include/dev/ofw/openfirmio.h		comp-c-include
+./usr/include/dev/pci/amrio.h			comp-c-include
+./usr/include/dev/pci/amrreg.h			comp-c-include
 ./usr/include/dev/pci/if_devar.h		comp-obsolete		obsolete
 ./usr/include/dev/pci/if_fxpreg.h		comp-obsolete		obsolete
 ./usr/include/dev/pci/if_fxpvar.h		comp-obsolete		obsolete
Index: distrib/sets/lists/man/mi
===================================================================
RCS file: /cvsroot/src/distrib/sets/lists/man/mi,v
retrieving revision 1.906
diff -u -r1.906 mi
--- distrib/sets/lists/man/mi	30 Jun 2006 23:12:48 -0000	1.906
+++ distrib/sets/lists/man/mi	2 Jul 2006 21:21:23 -0000
@@ -1714,6 +1714,7 @@
 ./usr/share/man/cat8/amiga/installboot.0	man-sysutil-catman	.cat
 ./usr/share/man/cat8/amiga/makedev.0		man-obsolete		obsolete
 ./usr/share/man/cat8/amq.0			man-amd-catman		.cat
+./usr/share/man/cat8/amrctl.0			man-sysutil-man		.cat
 ./usr/share/man/cat8/anvil.0			man-postfix-catman	postfix,.cat
 ./usr/share/man/cat8/apm.0			man-sysutil-catman	.cat
 ./usr/share/man/cat8/apmd.0			man-sysutil-catman	.cat
@@ -4069,6 +4070,7 @@
 ./usr/share/man/man8/amiga/installboot.8	man-sysutil-man		.man
 ./usr/share/man/man8/amiga/makedev.8		man-obsolete		obsolete
 ./usr/share/man/man8/amq.8			man-amd-man		.man
+./usr/share/man/man8/amrctl.8			man-sysutil-man		.man
 ./usr/share/man/man8/anvil.8			man-postfix-man		postfix,.man
 ./usr/share/man/man8/apm.8			man-sysutil-man		.man
 ./usr/share/man/man8/apmd.8			man-sysutil-man		.man
Index: etc/MAKEDEV.tmpl
===================================================================
RCS file: /cvsroot/src/etc/MAKEDEV.tmpl,v
retrieving revision 1.53
diff -u -r1.53 MAKEDEV.tmpl
--- etc/MAKEDEV.tmpl	19 Jun 2006 15:44:35 -0000	1.53
+++ etc/MAKEDEV.tmpl	2 Jul 2006 21:21:24 -0000
@@ -223,6 +223,7 @@
 #	ad*	UNIBUS interface to Data Translation A/D converter
 #	agp*	AGP GART devices
 #	altq	ALTQ control interface
+#	amr*	AMI MegaRaid control device
 #	apm	power management device
 #	audio*	audio devices
 #	bell*	OPM bell device (x68k)
@@ -1007,6 +1008,11 @@
 	mkdev radio$unit c %radio_chr% $unit 666
 	;;
 
+amr*)
+	unit=${i#amr}
+	mkdev amr$unit c %amr_chr% $unit
+	;;
+
 apm)
 	mkdev apm	c %apm_chr% 0 644
 	mkdev apmctl	c %apm_chr% 8 644
Index: etc/etc.amd64/MAKEDEV.conf
===================================================================
RCS file: /cvsroot/src/etc/etc.amd64/MAKEDEV.conf,v
retrieving revision 1.6
diff -u -r1.6 MAKEDEV.conf
--- etc/etc.amd64/MAKEDEV.conf	2 Dec 2004 16:44:28 -0000	1.6
+++ etc/etc.amd64/MAKEDEV.conf	2 Jul 2006 21:21:24 -0000
@@ -8,7 +8,7 @@
 	makedev bpf
 	makedev ccd0 md0 ch0 random
 	makedev cgd0 cgd1
-	makedev iop0 mlx0 mly0 dpti0 dpt0 twe0
+	makedev amr0 iop0 mlx0 mly0 dpti0 dpt0 twe0
 	makedev raid0
 	makedev ld0 ld1 ld2 ld3
 	makedev usbs
Index: etc/etc.i386/MAKEDEV.conf
===================================================================
RCS file: /cvsroot/src/etc/etc.i386/MAKEDEV.conf,v
retrieving revision 1.7
diff -u -r1.7 MAKEDEV.conf
--- etc/etc.i386/MAKEDEV.conf	19 Jun 2006 15:44:35 -0000	1.7
+++ etc/etc.i386/MAKEDEV.conf	2 Jul 2006 21:21:24 -0000
@@ -8,7 +8,7 @@
 	makedev bpf
 	makedev ccd0 md0 ch0 random
 	makedev cgd0 cgd1
-	makedev iop0 mlx0 mly0 dpti0 dpt0 twe0 icp0
+	makedev amr0 iop0 mlx0 mly0 dpti0 dpt0 twe0 icp0
 	makedev ed0 ed1
 	makedev raid0
 	makedev ld0 ld1 ld2 ld3
Index: etc/etc.i386/Makefile.inc
===================================================================
RCS file: /cvsroot/src/etc/etc.i386/Makefile.inc,v
retrieving revision 1.44
diff -u -r1.44 Makefile.inc
--- etc/etc.i386/Makefile.inc	8 Jun 2006 22:17:07 -0000	1.44
+++ etc/etc.i386/Makefile.inc	2 Jul 2006 21:21:24 -0000
@@ -5,7 +5,7 @@
 
 KERNEL_SETS=		GENERIC GENERIC_TINY GENERIC_LAPTOP \
 			GENERIC_DIAGNOSTIC GENERIC_PS2TINY GENERIC.MP \
-			GENERIC.MPACPI XEN2_DOM0 XEN2_DOMU XEN3_DOMU
+			GENERIC.MPACPI XEN2_DOM0 XEN2_DOMU XEN3_DOM0 XEN3_DOMU
 
 BUILD_KERNELS=		INSTALL INSTALL_SMALL INSTALL_TINY \
 			INSTALL_LAPTOP INSTALL_PS2 INSTALL_XEN2_DOMU \
Index: sbin/Makefile
===================================================================
RCS file: /cvsroot/src/sbin/Makefile,v
retrieving revision 1.99
diff -u -r1.99 Makefile
--- sbin/Makefile	25 Feb 2006 01:34:16 -0000	1.99
+++ sbin/Makefile	2 Jul 2006 21:21:24 -0000
@@ -6,7 +6,7 @@
 
 .include <bsd.own.mk>
 
-SUBDIR=	atactl badsect bim brconfig ccdconfig disklabel dkctl dmesg \
+SUBDIR=	amrctl atactl badsect bim brconfig ccdconfig disklabel dkctl dmesg \
 	drvctl edlabel fastboot fdisk fsck fsirand ifconfig init ldconfig \
 	mbrlabel mknod modload modunload mount newbtconf nologin \
 	pdisk ping pppoectl raidctl reboot rcorder rndctl route routed \
Index: sbin/amrctl/Makefile
===================================================================
diff -u /dev/null sbin/amrctl/Makefile
--- /dev/null	2006-07-02 23:23:21.000000000 +0200
+++ sbin/amrctl/Makefile	2006-07-02 19:28:48.000000000 +0200
@@ -0,0 +1,6 @@
+#	$NetBSD: Makefile,v 1.1 2000/11/04 18:53:30 thorpej Exp $
+
+PROG=	amrctl
+MAN=	amrctl.8
+
+.include <bsd.prog.mk>
Index: sbin/amrctl/amrctl.8
===================================================================
diff -u /dev/null sbin/amrctl/amrctl.8
--- /dev/null	2006-07-02 23:23:21.000000000 +0200
+++ sbin/amrctl/amrctl.8	2006-07-02 19:39:22.000000000 +0200
@@ -0,0 +1,103 @@
+.\"	$NetBSD: $
+.\"
+.\" Copyright (c) 2006 Manuel Bouyer.
+.\"
+.\" 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. All advertising materials mentioning features or use of this software
+.\"    must display the following acknowledgement:
+.\"      This product includes software developed by Manuel Bouyer.
+.\" 4. The name of the author may not be used to endorse or promote products
+.\"    derived from this software without specific prior written permission.
+.\"
+.\" 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.
+.\"
+.Dd June 24, 2006
+.Dt AMRCTL 8
+.Os
+.Sh NAME
+.Nm amrctl
+.Nd Control utility for AMI MegaRaid controllers
+.Sh SYNOPSIS
+.Nm
+.Ar stat
+.Op Fl bgv
+.Op Fl a Ar attempts
+.Op Fl f Ar device
+.Op Fl l Ar volno
+.Op Fl p Ar driveno
+.Op Fl s Ar busno
+.Op Fl s Ar busno:driveno
+.Op Fl t Ar microseconds
+.Sh DESCRIPTION
+The 
+.Nm
+queries or controls AMI MegaRaid controllers supported 
+by the
+.Xr amr 4
+driver.
+.Pp
+Only the
+.Ar stat
+subcommand is currently implemented, and reports status of the controller.
+The options for the
+.Ar stat
+subcommand are as follows:
+.Bl -tag -offset indent -width XpXperiodXX
+.It Fl a Ar attempts
+Number of retries for a command before giving up.
+Default 5.
+.It Fl b
+Report battery status.
+.It Fl f Ar device
+Device to use.
+Default to
+.Pa /dev/amr0 .
+.It Fl g
+Report global paramters for the controller.
+.It Fl l Ar volno
+Report status of a logical drive.
+.It Fl p Ar driveno
+Report status of a physical drive.
+.It Fl s Ar busno
+Report status of all physical drives on the specified bus.
+.It Fl s Ar busno:driveno
+Report status of the specified physical drive on the specified bus.
+.It Fl t Ar microseconds
+Delay between retries for a command.
+.It Fl v
+Increase verbosity level by one.
+.El
+.Sh SEE ALSO
+.Xr amr 4
+.Sh HISTORY
+The
+.Nm
+command first appeared in
+.Nx 4.0 .
+.Sh AUTHORS
+The
+.Nm
+command was written by
+.An Pierre David
+.Aq Pierre.David@crc.u-strasbg.fr
+and
+.An Jung-uk Kim
+.Aq jkim@FreeBSD.org
+for
+.Fx .
Index: sbin/amrctl/amrctl.c
===================================================================
diff -u /dev/null sbin/amrctl/amrctl.c
--- /dev/null	2006-07-02 23:40:35.000000000 +0200
+++ sbin/amrctl/amrctl.c	2006-07-02 23:40:40.000000000 +0200
@@ -0,0 +1,683 @@
+/*-
+ * 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 <sys/ioctl.h>
+
+#include <machine/param.h>
+
+#include <dev/pci/amrio.h>
+#include <dev/pci/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" */
+
+int amr_ioctl_enquiry(int, u_int8_t, u_int8_t, u_int8_t);
+void usage(char *);
+int describe_card(int, int, int);
+char * describe_property(u_int8_t, char *);
+const char * describe_state(int, u_int8_t);
+void describe_battery(int, int, int, int, int);
+void describe_one_volume(int, int, u_int32_t, u_int8_t, u_int8_t);
+void describe_one_drive(int, int, u_int8_t);
+void describe_drive(int, int, int, int, int);
+
+/*
+ * 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 {
+	const char	*product;
+	const 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 {
+	const int	code;
+	const 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 {
+	const int	code;
+	const 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 {
+	const u_int8_t	code;
+	const 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 {
+	const u_int8_t	code;
+	const 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 stat [-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) {
+			const 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;
+}
+
+const 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];
+	const 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)
+{
+	const 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, fflags = 0, sflags = 0;
+	int	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
+	 */
+	if (argc < 2)
+		usage(argv[0]);
+	if (strcmp(argv[1], "stat") != 0) /* only stat implemented for now */
+		usage(argv[0]);
+
+	optind = 2;
+	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 '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 (!fflags) {
+		snprintf(filename, MAXPATHLEN, "/dev/amr0");
+	}
+		
+	fd = open(filename, O_RDONLY);
+	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;
+}
Index: sys/conf/majors
===================================================================
RCS file: /cvsroot/src/sys/conf/majors,v
retrieving revision 1.22
diff -u -r1.22 majors
--- sys/conf/majors	14 May 2006 21:25:21 -0000	1.22
+++ sys/conf/majors	2 Jul 2006 21:21:24 -0000
@@ -24,3 +24,4 @@
 device-major	ucycom		char 172		ucycom
 device-major	gpio		char 173		gpio
 device-major	utoppy		char 174		utoppy
+device-major	amr		char 175		amr
Index: sys/dev/pci/Makefile
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/Makefile,v
retrieving revision 1.11
diff -u -r1.11 Makefile
--- sys/dev/pci/Makefile	6 Dec 2005 11:53:56 -0000	1.11
+++ sys/dev/pci/Makefile	2 Jul 2006 21:21:24 -0000
@@ -5,7 +5,7 @@
 INCSDIR= /usr/include/dev/pci
 
 # Only install includes which are used by userland
-INCS=	if_lmc.h mlyio.h mlyreg.h \
+INCS=	amrreg.h amrio.h if_lmc.h mlyio.h mlyreg.h \
 	pcidevs.h pcidevs_data.h pciio.h pcireg.h \
 	tgareg.h twereg.h tweio.h
 
Index: sys/dev/pci/amr.c
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/amr.c,v
retrieving revision 1.35
diff -u -r1.35 amr.c
--- sys/dev/pci/amr.c	7 Jun 2006 22:33:36 -0000	1.35
+++ sys/dev/pci/amr.c	2 Jul 2006 21:21:24 -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"
 
@@ -115,9 +117,20 @@
 static int	amr_std_get_work(struct amr_softc *, struct amr_mailbox_resp *);
 static int	amr_std_submit(struct amr_softc *, struct amr_ccb *);
 
+static dev_type_open(amropen);
+static dev_type_close(amrclose);
+static dev_type_ioctl(amrioctl);
+
 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 */
 
@@ -260,7 +273,7 @@
 	const char *intrstr;
 	pcireg_t reg;
 	int rseg, i, j, size, rv, memreg, ioreg;
-        struct amr_ccb *ac;
+	struct amr_ccb *ac;
 	int locs[AMRCF_NLOCS];
 
 	aprint_naive(": RAID controller\n");
@@ -719,7 +732,7 @@
 static 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;
@@ -872,7 +885,7 @@
 		ac->ac_cmd.mb_command = AMR_CMD_ENQUIRY;
 
 		rv = amr_ccb_map(amr, ac, amr->amr_enqbuf,
-		    AMR_ENQUIRY_BUFSIZE, 0);
+		    AMR_ENQUIRY_BUFSIZE, AC_XFER_IN);
 		if (rv != 0) {
 			printf("%s: ccb_map failed (%d)\n",
  			    amr->amr_dv.dv_xname, rv);
@@ -948,7 +961,7 @@
 	mb[2] = cmdsub;
 	mb[3] = cmdqual;
 
-	rv = amr_ccb_map(amr, ac, sbuf, AMR_ENQUIRY_BUFSIZE, 0);
+	rv = amr_ccb_map(amr, ac, sbuf, AMR_ENQUIRY_BUFSIZE, AC_XFER_IN);
 	if (rv == 0) {
 		rv = amr_ccb_poll(amr, ac, 2000);
 		amr_ccb_unmap(amr, ac);
@@ -1027,12 +1040,13 @@
  */
 int
 amr_ccb_map(struct amr_softc *amr, struct amr_ccb *ac, void *data, int size,
-	    int out)
+	    int tflag)
 {
 	struct amr_sgentry *sge;
 	struct amr_mailbox_cmd *mb;
 	int nsegs, i, rv, sgloff;
 	bus_dmamap_t xfer;
+	int dmaflag = 0;
 
 	xfer = ac->ac_xfer_map;
 
@@ -1043,9 +1057,14 @@
 
 	mb = &ac->ac_cmd;
 	ac->ac_xfer_size = size;
-	ac->ac_flags |= (out ? AC_XFER_OUT : AC_XFER_IN);
+	ac->ac_flags |= (tflag & (AC_XFER_OUT | AC_XFER_IN));
 	sgloff = AMR_SGL_SIZE * ac->ac_ident;
 
+	if (tflag & AC_XFER_OUT)
+		dmaflag |= BUS_DMASYNC_PREWRITE;
+	if (tflag & AC_XFER_IN)
+		dmaflag |= BUS_DMASYNC_PREREAD;
+
 	/* We don't need to use a scatter/gather list for just 1 segment. */
 	nsegs = xfer->dm_nsegs;
 	if (nsegs == 1) {
@@ -1063,8 +1082,7 @@
 		}
 	}
 
-	bus_dmamap_sync(amr->amr_dmat, xfer, 0, ac->ac_xfer_size,
-	    out ? BUS_DMASYNC_PREWRITE : BUS_DMASYNC_PREREAD);
+	bus_dmamap_sync(amr->amr_dmat, xfer, 0, ac->ac_xfer_size, dmaflag);
 
 	if ((ac->ac_flags & AC_NOSGL) == 0)
 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap, sgloff,
@@ -1079,14 +1097,19 @@
 void
 amr_ccb_unmap(struct amr_softc *amr, struct amr_ccb *ac)
 {
+	int dmaflag = 0;
+
+	if (ac->ac_flags & AC_XFER_IN)
+		dmaflag |= BUS_DMASYNC_POSTREAD;
+	if (ac->ac_flags & AC_XFER_OUT)
+		dmaflag |= BUS_DMASYNC_POSTWRITE;
 
 	if ((ac->ac_flags & AC_NOSGL) == 0)
 		bus_dmamap_sync(amr->amr_dmat, amr->amr_dmamap,
 		    AMR_SGL_SIZE * ac->ac_ident, AMR_SGL_SIZE,
 		    BUS_DMASYNC_POSTWRITE);
 	bus_dmamap_sync(amr->amr_dmat, ac->ac_xfer_map, 0, ac->ac_xfer_size,
-	    (ac->ac_flags & AC_XFER_IN) != 0 ?
-	    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
+	    dmaflag);
 	bus_dmamap_unload(amr->amr_dmat, ac->ac_xfer_map);
 }
 
@@ -1305,3 +1328,106 @@
 		printf("%08x ", ((u_int32_t *)&ac->ac_cmd)[i]);
 	printf("\n");
 }
+
+static int
+amropen(dev_t dev, int flag, int mode, struct lwp *l)
+{
+	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);
+}
+
+static int
+amrclose(dev_t dev, int flag, int mode, struct lwp *l)
+{
+	struct amr_softc *amr;
+
+	amr = device_lookup(&amr_cd, minor(dev));
+	amr->amr_flags &= ~AMRF_OPEN;
+	return (0);
+}
+
+static int
+amrioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
+{
+	struct amr_softc *amr;
+	struct amr_user_ioctl *au;
+	struct amr_ccb *ac;
+	struct amr_mailbox_ioctl *mbi;
+	unsigned long au_length;
+	uint8_t *au_cmd;
+	int error;
+	void *dp = NULL, *au_buffer;
+
+	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:
+		*(int *)data = AMR_IO_VERSION_NUMBER;
+		return 0;
+	case AMR_IO_COMMAND:
+		au = (struct amr_user_ioctl *)data;
+		au_cmd = au->au_cmd;
+		au_buffer = au->au_buffer;
+		au_length = au->au_length;
+		break;
+	default:
+		return ENOTTY;
+	}
+
+	if (au_cmd[0] == AMR_CMD_PASS) {
+		/* not yet */
+		return EOPNOTSUPP;
+	}
+
+	if (au_length <= 0 || au_length > MAXPHYS || au_cmd[0] == 0x06)
+		return (EINVAL);
+
+	/*
+	 * allocate kernel memory for data, doing I/O directly to user
+	 * buffer isn't that easy.
+	 */
+	dp = malloc(au_length, M_DEVBUF, M_WAITOK|M_ZERO);
+	if (dp == NULL)
+		return ENOMEM;
+	if ((error = copyin(au_buffer, dp, au_length)) != 0)
+		goto out;
+
+	/* direct command to controller */
+	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;
+	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];
+	error = amr_ccb_map(amr, ac, dp, (int)au_length,
+	    AC_XFER_IN | AC_XFER_OUT);
+	if (error == 0) {
+		error = amr_ccb_wait(amr, ac);
+		amr_ccb_unmap(amr, ac);
+		if (error == 0)
+			error = copyout(dp, au_buffer, au_length);
+
+	}
+	amr_ccb_free(amr, ac);
+out:
+	free(dp, M_DEVBUF);
+	return (error);
+}
Index: sys/dev/pci/amrio.h
===================================================================
RCS file: sys/dev/pci/amrio.h
diff -N sys/dev/pci/amrio.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ sys/dev/pci/amrio.h	2 Jul 2006 21:21:24 -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: sys/dev/pci/amrreg.h
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/amrreg.h,v
retrieving revision 1.2
diff -u -r1.2 amrreg.h
--- sys/dev/pci/amrreg.h	4 May 2003 16:15:36 -0000	1.2
+++ sys/dev/pci/amrreg.h	2 Jul 2006 21:21:24 -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: sys/dev/pci/amrvar.h
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/amrvar.h,v
retrieving revision 1.5
diff -u -r1.5 amrvar.h
--- sys/dev/pci/amrvar.h	11 Dec 2005 12:22:48 -0000	1.5
+++ sys/dev/pci/amrvar.h	2 Jul 2006 21:21:24 -0000
@@ -107,6 +107,7 @@
 
 /* General flags. */
 #define	AMRF_THREAD_EXIT	0x00010000
+#define	AMRF_OPEN		0x00020000
 
 /*
  * Command control block.
Index: sys/dev/pci/ld_amr.c
===================================================================
RCS file: /cvsroot/src/sys/dev/pci/ld_amr.c,v
retrieving revision 1.9
diff -u -r1.9 ld_amr.c
--- sys/dev/pci/ld_amr.c	25 Mar 2006 04:12:36 -0000	1.9
+++ sys/dev/pci/ld_amr.c	2 Jul 2006 21:21:24 -0000
@@ -151,7 +151,9 @@
 	mb->mb_blkcount = htole16(datasize / AMR_SECTOR_SIZE);
 	mb->mb_lba = htole32(blkno);
 
-	if ((rv = amr_ccb_map(amr, ac, data, datasize, dowrite)) != 0) {
+	rv = amr_ccb_map(amr, ac, data, datasize,
+	    (dowrite ? AC_XFER_OUT : AC_XFER_IN));
+	if (rv != 0) {
 		amr_ccb_free(amr, ac);
 		return (rv);
 	}

--Dxnq1zWXvFF0Q93v--