Home | History | Annotate | Line # | Download | only in amrctl
amrctl.c revision 1.9
      1  1.9  jakllsch /*	$NetBSD: amrctl.c,v 1.9 2012/05/18 14:01:34 jakllsch Exp $	*/
      2  1.8  jakllsch 
      3  1.1    bouyer /*-
      4  1.1    bouyer  * Copyright (c) 2002, Pierre David <Pierre.David (at) crc.u-strasbg.fr>
      5  1.1    bouyer  * Copyright (c) 2006, Jung-uk Kim <jkim (at) FreeBSD.org>
      6  1.1    bouyer  * All rights reserved.
      7  1.1    bouyer  *
      8  1.1    bouyer  * Redistribution and use in source and binary forms, with or without
      9  1.1    bouyer  * modification, are permitted provided that the following conditions
     10  1.1    bouyer  * are met:
     11  1.1    bouyer  * 1. Redistributions of source code must retain the above copyright
     12  1.1    bouyer  *    notice unmodified, this list of conditions, and the following
     13  1.1    bouyer  *    disclaimer.
     14  1.1    bouyer  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1    bouyer  *    notice, this list of conditions and the following disclaimer in the
     16  1.1    bouyer  *    documentation and/or other materials provided with the distribution.
     17  1.1    bouyer  *
     18  1.1    bouyer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.1    bouyer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.1    bouyer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.1    bouyer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.1    bouyer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.1    bouyer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.1    bouyer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.1    bouyer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.1    bouyer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.1    bouyer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1    bouyer  */
     29  1.1    bouyer 
     30  1.1    bouyer #include <sys/cdefs.h>
     31  1.8  jakllsch #ifndef lint
     32  1.9  jakllsch __RCSID("$NetBSD: amrctl.c,v 1.9 2012/05/18 14:01:34 jakllsch Exp $");
     33  1.8  jakllsch #endif
     34  1.1    bouyer 
     35  1.1    bouyer #include <stdio.h>
     36  1.1    bouyer #include <stdlib.h>
     37  1.1    bouyer #include <string.h>
     38  1.1    bouyer #include <fcntl.h>
     39  1.6  jakllsch #include <err.h>
     40  1.1    bouyer #include <errno.h>
     41  1.1    bouyer #include <unistd.h>
     42  1.1    bouyer 
     43  1.1    bouyer #include <sys/ioctl.h>
     44  1.1    bouyer 
     45  1.1    bouyer #include <machine/param.h>
     46  1.1    bouyer 
     47  1.1    bouyer #include <dev/pci/amrio.h>
     48  1.1    bouyer #include <dev/pci/amrreg.h>
     49  1.1    bouyer 
     50  1.1    bouyer #define NATTEMPTS	5
     51  1.1    bouyer #define SLEEPTIME	100000	/* microseconds */
     52  1.1    bouyer 
     53  1.3     joerg static int	nattempts = NATTEMPTS;	/* # of attempts before giving up */
     54  1.3     joerg static int	sleeptime = SLEEPTIME;	/* between attempts, in ms */
     55  1.1    bouyer 
     56  1.1    bouyer #define AMR_BUFSIZE	1024
     57  1.1    bouyer 
     58  1.3     joerg static int	enq_result = AMR_STATUS_FAILED;
     59  1.3     joerg static char	enq_buffer[AMR_BUFSIZE];
     60  1.1    bouyer 
     61  1.1    bouyer #define AMR_MAX_NCTRLS	16
     62  1.1    bouyer #define AMR_MAX_NSDEVS	16
     63  1.1    bouyer 
     64  1.7  jakllsch static uint8_t	nschan = 0;
     65  1.1    bouyer 
     66  1.1    bouyer /*
     67  1.1    bouyer  * Include lookup tables, and a function to match a code to a string.
     68  1.1    bouyer  *
     69  1.1    bouyer  * XXX Lookup tables cannot be included, since they require symbols from
     70  1.1    bouyer  * amrreg.h which need in turn the _KERNEL define.
     71  1.1    bouyer  */
     72  1.1    bouyer 
     73  1.1    bouyer /* #define AMR_DEFINE_TABLES */
     74  1.1    bouyer /* #include "amr_tables.h" */
     75  1.1    bouyer 
     76  1.7  jakllsch static int amr_ioctl_enquiry(int, uint8_t, uint8_t, uint8_t);
     77  1.3     joerg __dead static void usage(const char *);
     78  1.3     joerg static int describe_card(int, int, int);
     79  1.7  jakllsch static char * describe_property(uint8_t, char *);
     80  1.7  jakllsch static const char * describe_state(int, uint8_t);
     81  1.3     joerg static void describe_battery(int, int, int, int, int);
     82  1.7  jakllsch static void describe_one_volume(int, int, uint32_t, uint8_t, uint8_t);
     83  1.7  jakllsch static void describe_one_drive(int, int, uint8_t);
     84  1.3     joerg static void describe_drive(int, int, int, int, int);
     85  1.1    bouyer 
     86  1.1    bouyer /*
     87  1.1    bouyer  * Offsets in an amr_user_ioctl.au_cmd [] array See amrio.h
     88  1.1    bouyer  */
     89  1.1    bouyer 
     90  1.1    bouyer #define MB_COMMAND	0
     91  1.1    bouyer #define MB_CHANNEL	1
     92  1.1    bouyer #define MB_PARAM	2
     93  1.1    bouyer #define MB_PAD		3
     94  1.1    bouyer #define MB_DRIVE	4
     95  1.1    bouyer 
     96  1.1    bouyer #define FIRMWARE_40LD	1
     97  1.1    bouyer #define FIRMWARE_8LD	2
     98  1.1    bouyer 
     99  1.4  jakllsch static const struct {
    100  1.1    bouyer 	const char	*product;
    101  1.2     lukem 	const uint32_t	signature;
    102  1.1    bouyer } prodtable[] = {
    103  1.1    bouyer 	{	"Series 431",			AMR_SIG_431	},
    104  1.1    bouyer 	{	"Series 438",			AMR_SIG_438	},
    105  1.1    bouyer 	{	"Series 762",			AMR_SIG_762	},
    106  1.1    bouyer 	{	"Integrated HP NetRAID (T5)",	AMR_SIG_T5	},
    107  1.1    bouyer 	{	"Series 466",			AMR_SIG_466	},
    108  1.1    bouyer 	{	"Series 467",			AMR_SIG_467	},
    109  1.1    bouyer 	{	"Integrated HP NetRAID (T7)",	AMR_SIG_T7	},
    110  1.1    bouyer 	{	"Series 490",			AMR_SIG_490	}
    111  1.1    bouyer };
    112  1.1    bouyer 
    113  1.4  jakllsch static const struct {
    114  1.1    bouyer 	const int	code;
    115  1.1    bouyer 	const char	*ifyes, *ifno;
    116  1.1    bouyer } proptable[] = {
    117  1.1    bouyer 	{	AMR_DRV_WRITEBACK,
    118  1.1    bouyer 		"writeback",		"write-through"		},
    119  1.1    bouyer 	{	AMR_DRV_READHEAD,
    120  1.1    bouyer 		"read-ahead",		"no-read-ahead"		},
    121  1.1    bouyer 	{	AMR_DRV_ADAPTIVE,
    122  1.1    bouyer 		"adaptative-io",	"no-adaptative-io"	}
    123  1.1    bouyer };
    124  1.1    bouyer 
    125  1.4  jakllsch static const struct {
    126  1.1    bouyer 	const int	code;
    127  1.1    bouyer 	const char	*status;
    128  1.1    bouyer } statetable[] = {
    129  1.1    bouyer 	{	AMR_DRV_OFFLINE,	"offline"	},
    130  1.1    bouyer 	{	AMR_DRV_DEGRADED,	"degraded"	},
    131  1.1    bouyer 	{	AMR_DRV_OPTIMAL,	"optimal"	},
    132  1.1    bouyer 	{	AMR_DRV_ONLINE,		"online"	},
    133  1.1    bouyer 	{	AMR_DRV_FAILED,		"failed"	},
    134  1.1    bouyer 	{	AMR_DRV_REBUILD,	"rebuild"	},
    135  1.1    bouyer 	{	AMR_DRV_HOTSPARE,	"hotspare"	}
    136  1.1    bouyer };
    137  1.1    bouyer 
    138  1.4  jakllsch static const struct {
    139  1.7  jakllsch 	const uint8_t	code;
    140  1.1    bouyer 	const char		*status;
    141  1.1    bouyer } battable[] = {
    142  1.1    bouyer 	{	AMR_BATT_MODULE_MISSING,	"not present"		},
    143  1.1    bouyer 	{	AMR_BATT_LOW_VOLTAGE,		"low voltage"		},
    144  1.1    bouyer 	{	AMR_BATT_TEMP_HIGH,		"high temperature"	},
    145  1.1    bouyer 	{	AMR_BATT_PACK_MISSING,		"pack missing"	},
    146  1.1    bouyer 	{	AMR_BATT_CYCLES_EXCEEDED,	"cycle exceeded"	}
    147  1.1    bouyer };
    148  1.1    bouyer 
    149  1.4  jakllsch static const struct {
    150  1.7  jakllsch 	const uint8_t	code;
    151  1.1    bouyer 	const char		*status;
    152  1.1    bouyer } bcstatble[] = {
    153  1.1    bouyer 	{	AMR_BATT_CHARGE_DONE,		"charge done"		},
    154  1.1    bouyer 	{	AMR_BATT_CHARGE_INPROG,		"charge in progress"	},
    155  1.1    bouyer 	{	AMR_BATT_CHARGE_FAIL,		"charge failed"		}
    156  1.1    bouyer };
    157  1.1    bouyer 
    158  1.3     joerg static int
    159  1.7  jakllsch amr_ioctl_enquiry(int fd, uint8_t cmd, uint8_t cmdsub, uint8_t cmdqual)
    160  1.1    bouyer {
    161  1.1    bouyer 	struct amr_user_ioctl am;
    162  1.1    bouyer 	int	r, i;
    163  1.1    bouyer 
    164  1.1    bouyer 	am.au_cmd[MB_COMMAND] = cmd;
    165  1.1    bouyer 	am.au_cmd[MB_CHANNEL] = cmdsub;
    166  1.1    bouyer 	am.au_cmd[MB_PARAM] = cmdqual;
    167  1.1    bouyer 	am.au_cmd[MB_PAD] = 0;
    168  1.1    bouyer 	am.au_cmd[MB_DRIVE] = 0;
    169  1.1    bouyer 
    170  1.1    bouyer 	am.au_buffer = enq_buffer;
    171  1.1    bouyer 	am.au_length = AMR_BUFSIZE;
    172  1.1    bouyer 	am.au_direction = AMR_IO_READ;
    173  1.1    bouyer 	am.au_status = 0;
    174  1.1    bouyer 
    175  1.1    bouyer 	i = 0;
    176  1.1    bouyer 	r = -1;
    177  1.1    bouyer 	while (i < nattempts && r == -1) {
    178  1.1    bouyer 		r = ioctl(fd, AMR_IO_COMMAND, &am);
    179  1.1    bouyer 		if (r == -1) {
    180  1.1    bouyer 			if (errno != EBUSY) {
    181  1.9  jakllsch 				warn("ioctl enquiry");
    182  1.9  jakllsch 				return -1;
    183  1.1    bouyer 			} else
    184  1.1    bouyer 				usleep(sleeptime);
    185  1.1    bouyer 		}
    186  1.1    bouyer 		i++;
    187  1.1    bouyer 	}
    188  1.1    bouyer 	return am.au_status;
    189  1.1    bouyer }
    190  1.1    bouyer 
    191  1.3     joerg static void
    192  1.3     joerg usage(const char *prog)
    193  1.1    bouyer {
    194  1.1    bouyer 	fprintf(stderr, "usage: %s stat [-a num] [-b] "
    195  1.1    bouyer 		"[-c ctlr|-f dev] [-g] [-l vol]\n\t\t"
    196  1.1    bouyer 		"[-p drive|-s bus[:target]] [-t usec] [-v]\n\n\t"
    197  1.1    bouyer 		"-a num\t\tnumber of retries\n\t"
    198  1.1    bouyer 		"-b\t\tbattery status\n\t"
    199  1.1    bouyer 		"-c ctrl\t\tcontroller ID\n\t"
    200  1.1    bouyer 		"-f dev\t\tdevice path\n\t"
    201  1.1    bouyer 		"-g\t\tprint global parameters\n\t"
    202  1.1    bouyer 		"-l vol\t\tlogical volume ID\n\t"
    203  1.1    bouyer 		"-p drive\tphysical drive ID\n\t"
    204  1.1    bouyer 		"-s bus[:target]\tSCSI bus (and optinal target)\n\t"
    205  1.1    bouyer 		"-t usec\t\tsleep time between retries\n\t"
    206  1.1    bouyer 		"-v\t\tverbose output\n",
    207  1.1    bouyer 		prog);
    208  1.1    bouyer 	exit(1);
    209  1.1    bouyer }
    210  1.1    bouyer 
    211  1.1    bouyer /******************************************************************************
    212  1.1    bouyer  * Card description
    213  1.1    bouyer  */
    214  1.1    bouyer 
    215  1.3     joerg static int
    216  1.1    bouyer describe_card(int fd, int verbosity, int globalparam)
    217  1.1    bouyer {
    218  1.1    bouyer 	struct amr_enquiry *ae;
    219  1.2     lukem 	uint32_t	cardtype;
    220  1.1    bouyer 
    221  1.1    bouyer 	/*
    222  1.1    bouyer 	 * Try the 40LD firmware interface
    223  1.1    bouyer 	 */
    224  1.1    bouyer 
    225  1.1    bouyer 	enq_result = amr_ioctl_enquiry(fd, AMR_CMD_CONFIG,
    226  1.1    bouyer 		AMR_CONFIG_PRODUCT_INFO, 0);
    227  1.1    bouyer 	if (enq_result == AMR_STATUS_SUCCESS) {
    228  1.1    bouyer 		struct amr_prodinfo *ap;
    229  1.1    bouyer 
    230  1.1    bouyer 		ap = (struct amr_prodinfo *)enq_buffer;
    231  1.1    bouyer 		nschan = ap->ap_nschan;
    232  1.1    bouyer 		if (globalparam) {
    233  1.1    bouyer 			printf("Product\t\t\t<%.80s>\n", ap->ap_product);
    234  1.1    bouyer 			printf("Firmware\t\t%.16s\n", ap->ap_firmware);
    235  1.1    bouyer 			printf("BIOS\t\t\t%.16s\n", ap->ap_bios);
    236  1.1    bouyer 			printf("SCSI channels\t\t%d\n", ap->ap_nschan);
    237  1.1    bouyer 			printf("Fibre loops\t\t%d\n", ap->ap_fcloops);
    238  1.1    bouyer 			printf("Memory size\t\t%d MB\n", ap->ap_memsize);
    239  1.1    bouyer 			if (verbosity >= 1) {
    240  1.1    bouyer 				printf("Ioctl\t\t\t%d (%s)\n", FIRMWARE_40LD,
    241  1.1    bouyer 				       "40LD");
    242  1.1    bouyer 				printf("Signature\t\t0x%08x\n",
    243  1.1    bouyer 				       ap->ap_signature);
    244  1.1    bouyer 				printf("Configsig\t\t0x%08x\n",
    245  1.1    bouyer 				       ap->ap_configsig);
    246  1.1    bouyer 				printf("Subsystem\t\t0x%04x\n",
    247  1.1    bouyer 				       ap->ap_subsystem);
    248  1.1    bouyer 				printf("Subvendor\t\t0x%04x\n",
    249  1.1    bouyer 				       ap->ap_subvendor);
    250  1.1    bouyer 				printf("Notify counters\t\t%d\n",
    251  1.1    bouyer 				       ap->ap_numnotifyctr);
    252  1.1    bouyer 			}
    253  1.1    bouyer 		}
    254  1.1    bouyer 		return FIRMWARE_40LD;
    255  1.1    bouyer 	}
    256  1.1    bouyer 	/*
    257  1.1    bouyer 	 * Try the 8LD firmware interface
    258  1.1    bouyer 	 */
    259  1.1    bouyer 
    260  1.1    bouyer 	enq_result = amr_ioctl_enquiry(fd, AMR_CMD_EXT_ENQUIRY2, 0, 0);
    261  1.1    bouyer 	ae = (struct amr_enquiry *)enq_buffer;
    262  1.1    bouyer 	if (enq_result == AMR_STATUS_SUCCESS) {
    263  1.1    bouyer 		cardtype = ae->ae_signature;
    264  1.1    bouyer 	} else {
    265  1.1    bouyer 		enq_result = amr_ioctl_enquiry(fd, AMR_CMD_ENQUIRY, 0, 0);
    266  1.1    bouyer 		cardtype = 0;
    267  1.1    bouyer 	}
    268  1.1    bouyer 
    269  1.1    bouyer 	if (enq_result == AMR_STATUS_SUCCESS) {
    270  1.1    bouyer 
    271  1.1    bouyer 		if (globalparam) {
    272  1.1    bouyer 			const char   *product = NULL;
    273  1.1    bouyer 			char	bios[100], firmware[100];
    274  1.2     lukem 			size_t	i;
    275  1.1    bouyer 
    276  1.5  jakllsch 			for (i = 0; i < __arraycount(prodtable); i++) {
    277  1.1    bouyer 				if (cardtype == prodtable[i].signature) {
    278  1.1    bouyer 					product = prodtable[i].product;
    279  1.1    bouyer 					break;
    280  1.1    bouyer 				}
    281  1.1    bouyer 			}
    282  1.1    bouyer 			if (product == NULL)
    283  1.1    bouyer 				product = "unknown card signature";
    284  1.1    bouyer 
    285  1.1    bouyer 			/*
    286  1.1    bouyer 			 * HP NetRaid controllers have a special encoding of
    287  1.1    bouyer 			 * the firmware and BIOS versions. The AMI version
    288  1.1    bouyer 			 * seems to have it as strings whereas the HP version
    289  1.1    bouyer 			 * does it with a leading uppercase character and two
    290  1.1    bouyer 			 * binary numbers.
    291  1.1    bouyer 			 */
    292  1.1    bouyer 
    293  1.1    bouyer 			if (ae->ae_adapter.aa_firmware[2] >= 'A' &&
    294  1.1    bouyer 			    ae->ae_adapter.aa_firmware[2] <= 'Z' &&
    295  1.1    bouyer 			    ae->ae_adapter.aa_firmware[1] < ' ' &&
    296  1.1    bouyer 			    ae->ae_adapter.aa_firmware[0] < ' ' &&
    297  1.1    bouyer 			    ae->ae_adapter.aa_bios[2] >= 'A' &&
    298  1.1    bouyer 			    ae->ae_adapter.aa_bios[2] <= 'Z' &&
    299  1.1    bouyer 			    ae->ae_adapter.aa_bios[1] < ' ' &&
    300  1.1    bouyer 			    ae->ae_adapter.aa_bios[0] < ' ') {
    301  1.1    bouyer 
    302  1.1    bouyer 				/*
    303  1.1    bouyer 				 * looks like we have an HP NetRaid version
    304  1.1    bouyer 				 * of the MegaRaid
    305  1.1    bouyer 				 */
    306  1.1    bouyer 
    307  1.1    bouyer 				if (cardtype == AMR_SIG_438) {
    308  1.1    bouyer 					/*
    309  1.1    bouyer 					 * the AMI 438 is a NetRaid 3si in
    310  1.1    bouyer 					 * HP-land
    311  1.1    bouyer 					 */
    312  1.1    bouyer 					product = "HP NetRaid 3si";
    313  1.1    bouyer 				}
    314  1.1    bouyer 				sprintf(firmware, "%c.%02d.%02d",
    315  1.1    bouyer 					ae->ae_adapter.aa_firmware[2],
    316  1.1    bouyer 					ae->ae_adapter.aa_firmware[1],
    317  1.1    bouyer 					ae->ae_adapter.aa_firmware[0]);
    318  1.1    bouyer 				sprintf(bios, "%c.%02d.%02d",
    319  1.1    bouyer 					ae->ae_adapter.aa_bios[2],
    320  1.1    bouyer 					ae->ae_adapter.aa_bios[1],
    321  1.1    bouyer 					ae->ae_adapter.aa_bios[0]);
    322  1.1    bouyer 			} else {
    323  1.1    bouyer 				sprintf(firmware, "%.4s",
    324  1.1    bouyer 					ae->ae_adapter.aa_firmware);
    325  1.1    bouyer 				sprintf(bios, "%.4s", ae->ae_adapter.aa_bios);
    326  1.1    bouyer 			}
    327  1.1    bouyer 
    328  1.1    bouyer 			printf("Ioctl = %d (%s)\n", FIRMWARE_8LD, "8LD");
    329  1.1    bouyer 			printf("Product =\t<%s>\n", product);
    330  1.1    bouyer 			printf("Firmware =\t%s\n", firmware);
    331  1.1    bouyer 			printf("BIOS =\t%s\n", bios);
    332  1.1    bouyer 			/* printf ("SCSI Channels =\t%d\n", ae->ae_nschan); */
    333  1.1    bouyer 			/* printf ("Fibre Loops =\t%d\n", ae->ae_fcloops); */
    334  1.1    bouyer 			printf("Memory size =\t%d MB\n",
    335  1.1    bouyer 			       ae->ae_adapter.aa_memorysize);
    336  1.1    bouyer 			/*
    337  1.1    bouyer 			 * printf ("Notify counters =\t%d\n",
    338  1.1    bouyer 			 * ae->ae_numnotifyctr) ;
    339  1.1    bouyer 			 */
    340  1.1    bouyer 		}
    341  1.1    bouyer 		return FIRMWARE_8LD;
    342  1.1    bouyer 	}
    343  1.1    bouyer 	/*
    344  1.1    bouyer 	 * Neither firmware interface succeeded. Abort.
    345  1.1    bouyer 	 */
    346  1.1    bouyer 
    347  1.1    bouyer 	fprintf(stderr, "Firmware interface not supported\n");
    348  1.1    bouyer 	exit(1);
    349  1.1    bouyer 
    350  1.1    bouyer }
    351  1.1    bouyer 
    352  1.3     joerg static char *
    353  1.7  jakllsch describe_property(uint8_t prop, char *buffer)
    354  1.1    bouyer {
    355  1.2     lukem 	size_t	i;
    356  1.1    bouyer 
    357  1.1    bouyer 	strcpy(buffer, "<");
    358  1.5  jakllsch 	for (i = 0; i < __arraycount(proptable); i++) {
    359  1.1    bouyer 		if (i > 0)
    360  1.1    bouyer 			strcat(buffer, ",");
    361  1.1    bouyer 		if (prop & proptable[i].code)
    362  1.1    bouyer 			strcat(buffer, proptable[i].ifyes);
    363  1.1    bouyer 		else
    364  1.1    bouyer 			strcat(buffer, proptable[i].ifno);
    365  1.1    bouyer 	}
    366  1.1    bouyer 	strcat(buffer, ">");
    367  1.1    bouyer 
    368  1.1    bouyer 	return buffer;
    369  1.1    bouyer }
    370  1.1    bouyer 
    371  1.3     joerg static const char *
    372  1.7  jakllsch describe_state(int verbosity, uint8_t state)
    373  1.1    bouyer {
    374  1.2     lukem 	size_t	i;
    375  1.1    bouyer 
    376  1.1    bouyer 	if ((AMR_DRV_PREVSTATE(state) == AMR_DRV_CURSTATE(state)) &&
    377  1.1    bouyer 	    (AMR_DRV_CURSTATE(state) == AMR_DRV_OFFLINE) && verbosity == 0)
    378  1.1    bouyer 		return NULL;
    379  1.1    bouyer 
    380  1.5  jakllsch 	for (i = 0; i < __arraycount(statetable); i++)
    381  1.1    bouyer 		if (AMR_DRV_CURSTATE(state) == statetable[i].code)
    382  1.1    bouyer 			return (statetable[i].status);
    383  1.1    bouyer 
    384  1.1    bouyer 	return NULL;
    385  1.1    bouyer }
    386  1.1    bouyer 
    387  1.1    bouyer /******************************************************************************
    388  1.1    bouyer  * Battery status
    389  1.1    bouyer  */
    390  1.3     joerg static void
    391  1.1    bouyer describe_battery(int fd, int verbosity, int fwint, int bflags, int globalparam)
    392  1.1    bouyer {
    393  1.7  jakllsch 	uint8_t batt_status;
    394  1.2     lukem 	size_t i;
    395  1.1    bouyer 
    396  1.1    bouyer 	if (fwint == FIRMWARE_40LD) {
    397  1.1    bouyer 		enq_result = amr_ioctl_enquiry(fd, AMR_CMD_CONFIG,
    398  1.1    bouyer 			AMR_CONFIG_ENQ3, AMR_CONFIG_ENQ3_SOLICITED_FULL);
    399  1.1    bouyer 		if (enq_result == AMR_STATUS_SUCCESS) {
    400  1.1    bouyer 			struct amr_enquiry3 *ae3;
    401  1.1    bouyer 
    402  1.1    bouyer 			ae3 = (struct amr_enquiry3 *)enq_buffer;
    403  1.1    bouyer 			if (bflags || globalparam) {
    404  1.1    bouyer 				batt_status = ae3->ae_batterystatus;
    405  1.1    bouyer 				printf("Battery status\t\t");
    406  1.5  jakllsch 				for (i = 0; i < __arraycount(battable); i++) {
    407  1.1    bouyer 					if (batt_status & battable[i].code)
    408  1.1    bouyer 						printf("%s, ", battable[i].status);
    409  1.1    bouyer 				}
    410  1.1    bouyer 				if (!(batt_status &
    411  1.1    bouyer 				    (AMR_BATT_MODULE_MISSING|AMR_BATT_PACK_MISSING))) {
    412  1.5  jakllsch 					for (i = 0;
    413  1.5  jakllsch 					     i < __arraycount(bcstatble); i++)
    414  1.1    bouyer 						if (bcstatble[i].code ==
    415  1.1    bouyer 						    (batt_status & AMR_BATT_CHARGE_MASK))
    416  1.1    bouyer 							printf("%s", bcstatble[i].status);
    417  1.1    bouyer 				} else
    418  1.1    bouyer 					printf("charge unknown");
    419  1.1    bouyer 				if (verbosity)
    420  1.1    bouyer 					printf(" (0x%02x)", batt_status);
    421  1.1    bouyer 				printf("\n");
    422  1.1    bouyer 			}
    423  1.1    bouyer 		}
    424  1.1    bouyer 	} else if (fwint == FIRMWARE_8LD) {
    425  1.1    bouyer 		/* Nothing to do here. */
    426  1.1    bouyer 		return;
    427  1.1    bouyer 	} else {
    428  1.1    bouyer 		fprintf(stderr, "Firmware interface not supported.\n");
    429  1.1    bouyer 		exit(1);
    430  1.1    bouyer 	}
    431  1.1    bouyer 
    432  1.1    bouyer 	return;
    433  1.1    bouyer }
    434  1.1    bouyer 
    435  1.1    bouyer /******************************************************************************
    436  1.1    bouyer  * Logical volumes
    437  1.1    bouyer  */
    438  1.1    bouyer 
    439  1.3     joerg static void
    440  1.1    bouyer describe_one_volume(int ldrv, int verbosity,
    441  1.7  jakllsch 		    uint32_t size, uint8_t state, uint8_t prop)
    442  1.1    bouyer {
    443  1.1    bouyer 	float	szgb;
    444  1.1    bouyer 	int	raid_level;
    445  1.1    bouyer 	char	propstr[MAXPATHLEN];
    446  1.1    bouyer 	const char *statestr;
    447  1.1    bouyer 
    448  1.1    bouyer 	szgb = ((float)size) / (1024 * 1024 * 2);	/* size in GB */
    449  1.1    bouyer 
    450  1.1    bouyer 	raid_level = prop & AMR_DRV_RAID_MASK;
    451  1.1    bouyer 
    452  1.1    bouyer 	printf("Logical volume %d\t", ldrv);
    453  1.1    bouyer 	statestr = describe_state(verbosity, state);
    454  1.1    bouyer 	printf("%s ", statestr);
    455  1.1    bouyer 	printf("(%.2f GB, RAID%d", szgb, raid_level);
    456  1.1    bouyer 	if (verbosity >= 1) {
    457  1.1    bouyer 		describe_property(prop, propstr);
    458  1.1    bouyer 		printf(" %s", propstr);
    459  1.1    bouyer 	}
    460  1.1    bouyer 	printf(")\n");
    461  1.1    bouyer }
    462  1.1    bouyer 
    463  1.1    bouyer /******************************************************************************
    464  1.1    bouyer  * Physical drives
    465  1.1    bouyer  */
    466  1.1    bouyer 
    467  1.3     joerg static void
    468  1.7  jakllsch describe_one_drive(int pdrv, int verbosity, uint8_t state)
    469  1.1    bouyer {
    470  1.1    bouyer 	const char *statestr;
    471  1.1    bouyer 
    472  1.1    bouyer 	statestr = describe_state(verbosity, state);
    473  1.1    bouyer 	if (statestr) {
    474  1.1    bouyer 		if (nschan > 0)
    475  1.1    bouyer 			printf("Physical drive %d:%d\t%s\n",
    476  1.1    bouyer 			       pdrv / AMR_MAX_NSDEVS, pdrv % AMR_MAX_NSDEVS,
    477  1.1    bouyer 			       statestr);
    478  1.1    bouyer 		else
    479  1.1    bouyer 			printf("Physical drive %d:\t%s\n", pdrv, statestr);
    480  1.1    bouyer 	}
    481  1.1    bouyer }
    482  1.1    bouyer 
    483  1.3     joerg static void
    484  1.1    bouyer describe_drive(int verbosity, int fwint, int ldrv, int sbus, int sdev)
    485  1.1    bouyer {
    486  1.1    bouyer 	int	drv, pdrv = -1;
    487  1.1    bouyer 
    488  1.1    bouyer 	if (sbus > -1 && sdev > -1)
    489  1.1    bouyer 		pdrv = (sbus * AMR_MAX_NSDEVS) + sdev;
    490  1.1    bouyer 	if (nschan != 0) {
    491  1.1    bouyer 		if (sbus > -1 && sbus >= nschan) {
    492  1.1    bouyer 			fprintf(stderr, "SCSI channel %d does not exist.\n", sbus);
    493  1.1    bouyer 			exit(1);
    494  1.1    bouyer 		} else if (sdev > -1 && sdev >= AMR_MAX_NSDEVS) {
    495  1.1    bouyer 			fprintf(stderr, "SCSI device %d:%d does not exist.\n",
    496  1.1    bouyer 				sbus, sdev);
    497  1.1    bouyer 			exit(1);
    498  1.1    bouyer 		}
    499  1.1    bouyer 	}
    500  1.1    bouyer 	if (fwint == FIRMWARE_40LD) {
    501  1.1    bouyer 		if (enq_result == AMR_STATUS_SUCCESS) {
    502  1.1    bouyer 			struct amr_enquiry3 *ae3;
    503  1.1    bouyer 
    504  1.1    bouyer 			ae3 = (struct amr_enquiry3 *)enq_buffer;
    505  1.1    bouyer 			if ((ldrv < 0 && sbus < 0) || ldrv >= 0) {
    506  1.1    bouyer 				if (ldrv >= ae3->ae_numldrives) {
    507  1.1    bouyer 					fprintf(stderr, "Logical volume %d "
    508  1.1    bouyer 						"does not exist.\n", ldrv);
    509  1.1    bouyer 					exit(1);
    510  1.1    bouyer 				}
    511  1.1    bouyer 				if (ldrv < 0) {
    512  1.1    bouyer 					for (drv = 0;
    513  1.1    bouyer 					     drv < ae3->ae_numldrives;
    514  1.1    bouyer 					     drv++)
    515  1.1    bouyer 						describe_one_volume(drv,
    516  1.1    bouyer 						    verbosity,
    517  1.1    bouyer 						    ae3->ae_drivesize[drv],
    518  1.1    bouyer 						    ae3->ae_drivestate[drv],
    519  1.1    bouyer 						    ae3->ae_driveprop[drv]);
    520  1.1    bouyer 				} else {
    521  1.1    bouyer 					describe_one_volume(ldrv,
    522  1.1    bouyer 					    verbosity,
    523  1.1    bouyer 					    ae3->ae_drivesize[ldrv],
    524  1.1    bouyer 					    ae3->ae_drivestate[ldrv],
    525  1.1    bouyer 					    ae3->ae_driveprop[ldrv]);
    526  1.1    bouyer 				}
    527  1.1    bouyer 			}
    528  1.1    bouyer 			if ((ldrv < 0 && sbus < 0) || sbus >= 0) {
    529  1.1    bouyer 				if (pdrv >= AMR_40LD_MAXPHYSDRIVES ||
    530  1.1    bouyer 				    (nschan != 0 && pdrv >= (nschan * AMR_MAX_NSDEVS))) {
    531  1.1    bouyer 					fprintf(stderr, "Physical drive %d "
    532  1.1    bouyer 						"is out of range.\n", pdrv);
    533  1.1    bouyer 					exit(1);
    534  1.1    bouyer 				}
    535  1.1    bouyer 				if (sbus < 0) {
    536  1.1    bouyer 					for (drv = 0;
    537  1.1    bouyer 					     drv < AMR_40LD_MAXPHYSDRIVES;
    538  1.1    bouyer 					     drv++) {
    539  1.1    bouyer 						if (nschan != 0 &&
    540  1.1    bouyer 						    drv >= (nschan * AMR_MAX_NSDEVS))
    541  1.1    bouyer 							break;
    542  1.1    bouyer 						describe_one_drive(drv,
    543  1.1    bouyer 						    verbosity,
    544  1.1    bouyer 						    ae3->ae_pdrivestate[drv]);
    545  1.1    bouyer 					}
    546  1.1    bouyer 				} else if (sdev < 0) {
    547  1.1    bouyer 					for (drv = sbus * AMR_MAX_NSDEVS;
    548  1.1    bouyer 					     drv < ((sbus + 1) * AMR_MAX_NSDEVS);
    549  1.1    bouyer 					     drv++) {
    550  1.1    bouyer 						if (nschan != 0 &&
    551  1.1    bouyer 						    drv >= (nschan * AMR_MAX_NSDEVS))
    552  1.1    bouyer 							break;
    553  1.1    bouyer 						describe_one_drive(drv,
    554  1.1    bouyer 						    verbosity,
    555  1.1    bouyer 						    ae3->ae_pdrivestate[drv]);
    556  1.1    bouyer 					}
    557  1.1    bouyer 				} else {
    558  1.1    bouyer 					if (nschan != 0 &&
    559  1.1    bouyer 					    pdrv < (nschan * AMR_MAX_NSDEVS))
    560  1.1    bouyer 						describe_one_drive(pdrv, 1,
    561  1.1    bouyer 						    ae3->ae_pdrivestate[pdrv]);
    562  1.1    bouyer 				}
    563  1.1    bouyer 			}
    564  1.1    bouyer 		}
    565  1.1    bouyer 	} else if (fwint == FIRMWARE_8LD) {
    566  1.1    bouyer 		/* Nothing to do here. */
    567  1.1    bouyer 		return;
    568  1.1    bouyer 	} else {
    569  1.1    bouyer 		fprintf(stderr, "Firmware interface not supported.\n");
    570  1.1    bouyer 		exit(1);
    571  1.1    bouyer 	}
    572  1.1    bouyer }
    573  1.1    bouyer 
    574  1.1    bouyer /******************************************************************************
    575  1.1    bouyer  * Main function
    576  1.1    bouyer  */
    577  1.1    bouyer 
    578  1.1    bouyer int
    579  1.1    bouyer main(int argc, char *argv[])
    580  1.1    bouyer {
    581  1.1    bouyer 	int	i;
    582  1.1    bouyer 	int	fd = -1;
    583  1.1    bouyer 	int	globalparam = 0, verbosity = 0;
    584  1.1    bouyer 	int	bflags = 0, fflags = 0, sflags = 0;
    585  1.1    bouyer 	int	lvolno = -1, physno = -1;
    586  1.1    bouyer 	int	sbusno = -1, targetno = -1;
    587  1.1    bouyer 	char	filename[MAXPATHLEN];
    588  1.1    bouyer 	char	sdev[MAXPATHLEN];
    589  1.1    bouyer 	char	*pdev;
    590  1.1    bouyer 
    591  1.1    bouyer 	extern char *optarg;
    592  1.1    bouyer 	extern int optind;
    593  1.1    bouyer 
    594  1.1    bouyer 	/*
    595  1.1    bouyer 	 * Parse arguments
    596  1.1    bouyer 	 */
    597  1.1    bouyer 	if (argc < 2)
    598  1.1    bouyer 		usage(argv[0]);
    599  1.1    bouyer 	if (strcmp(argv[1], "stat") != 0) /* only stat implemented for now */
    600  1.1    bouyer 		usage(argv[0]);
    601  1.1    bouyer 
    602  1.1    bouyer 	optind = 2;
    603  1.1    bouyer 	while ((i = getopt(argc, argv, "a:bc:f:gl:p:s:t:v")) != -1)
    604  1.1    bouyer 		switch (i) {
    605  1.1    bouyer 		case 'a':
    606  1.1    bouyer 			nattempts = atoi(optarg);
    607  1.1    bouyer 			break;
    608  1.1    bouyer 		case 'b':
    609  1.1    bouyer 			bflags++;
    610  1.1    bouyer 			break;
    611  1.1    bouyer 		case 'f':
    612  1.1    bouyer 			snprintf(filename, MAXPATHLEN, "%s", optarg);
    613  1.1    bouyer 			filename[MAXPATHLEN - 1] = '\0';
    614  1.1    bouyer 			fflags++;
    615  1.1    bouyer 			break;
    616  1.1    bouyer 		case 'g':
    617  1.1    bouyer 			globalparam = 1;
    618  1.1    bouyer 			break;
    619  1.1    bouyer 		case 'l':
    620  1.1    bouyer 			lvolno = atoi(optarg);
    621  1.1    bouyer 			break;
    622  1.1    bouyer 		case 'p':
    623  1.1    bouyer 			physno = atoi(optarg);
    624  1.1    bouyer 			break;
    625  1.1    bouyer 		case 's':
    626  1.1    bouyer 			snprintf(sdev, MAXPATHLEN, "%s", optarg);
    627  1.1    bouyer 			sdev[MAXPATHLEN - 1] = '\0';
    628  1.1    bouyer 			sflags++;
    629  1.1    bouyer 			break;
    630  1.1    bouyer 		case 't':
    631  1.1    bouyer 			sleeptime = atoi(optarg);
    632  1.1    bouyer 			break;
    633  1.1    bouyer 		case 'v':
    634  1.1    bouyer 			verbosity++;
    635  1.1    bouyer 			break;
    636  1.1    bouyer 		case '?':
    637  1.1    bouyer 		default:
    638  1.1    bouyer 			usage(argv[0]);
    639  1.1    bouyer 		}
    640  1.1    bouyer 	argc -= optind;
    641  1.1    bouyer 	argv += optind;
    642  1.1    bouyer 
    643  1.1    bouyer 	if (argc != 0)
    644  1.1    bouyer 		usage(argv[0]);
    645  1.1    bouyer 
    646  1.1    bouyer 	if (!fflags) {
    647  1.1    bouyer 		snprintf(filename, MAXPATHLEN, "/dev/amr0");
    648  1.1    bouyer 	}
    649  1.1    bouyer 
    650  1.1    bouyer 	fd = open(filename, O_RDONLY);
    651  1.1    bouyer 	if (fd == -1) {
    652  1.6  jakllsch 		err(EXIT_FAILURE, "open");
    653  1.1    bouyer 	}
    654  1.1    bouyer 	if (ioctl(fd, AMR_IO_VERSION, &i) == -1) {
    655  1.6  jakllsch 		err(EXIT_FAILURE, "ioctl version");
    656  1.1    bouyer 	}
    657  1.1    bouyer 
    658  1.1    bouyer 	if (sflags) {
    659  1.1    bouyer 		if(physno > -1)
    660  1.1    bouyer 			usage(argv[0]);
    661  1.1    bouyer 		else {
    662  1.1    bouyer 			sbusno = atoi(sdev);
    663  1.1    bouyer 			if ((pdev = index(sdev, ':')))
    664  1.1    bouyer 				targetno = atoi(++pdev);
    665  1.1    bouyer 		}
    666  1.1    bouyer 	} else if (physno > -1) {
    667  1.1    bouyer 		sbusno = physno / AMR_MAX_NSDEVS;
    668  1.1    bouyer 		targetno = physno % AMR_MAX_NSDEVS;
    669  1.1    bouyer 	}
    670  1.1    bouyer 
    671  1.1    bouyer 	if (globalparam && verbosity >= 1)
    672  1.1    bouyer 		printf("Version\t\t\t%d\n", i);
    673  1.1    bouyer #if 0
    674  1.1    bouyer 	if (i != 1) {
    675  1.1    bouyer 		fprintf(stderr, "Driver version (%d) not supported\n", i);
    676  1.1    bouyer 		exit(1);
    677  1.1    bouyer 	}
    678  1.1    bouyer #endif
    679  1.1    bouyer 
    680  1.1    bouyer 	i = describe_card(fd, verbosity, globalparam);
    681  1.1    bouyer 	describe_battery(fd, verbosity, i, bflags, globalparam);
    682  1.1    bouyer 	if (!bflags || lvolno > -1 || physno > -1 || sbusno > -1 || targetno > -1)
    683  1.1    bouyer 		describe_drive(verbosity, i, lvolno, sbusno, targetno);
    684  1.1    bouyer 
    685  1.1    bouyer 	return 0;
    686  1.1    bouyer }
    687