Home | History | Annotate | Line # | Download | only in amrctl
      1  1.13    rillig /*	$NetBSD: amrctl.c,v 1.13 2024/11/03 10:43:26 rillig 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.13    rillig __RCSID("$NetBSD: amrctl.c,v 1.13 2024/11/03 10:43:26 rillig 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.11     sevan 		"[-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 		"-f dev\t\tdevice path\n\t"
    200   1.1    bouyer 		"-g\t\tprint global parameters\n\t"
    201   1.1    bouyer 		"-l vol\t\tlogical volume ID\n\t"
    202   1.1    bouyer 		"-p drive\tphysical drive ID\n\t"
    203   1.1    bouyer 		"-s bus[:target]\tSCSI bus (and optinal target)\n\t"
    204   1.1    bouyer 		"-t usec\t\tsleep time between retries\n\t"
    205   1.1    bouyer 		"-v\t\tverbose output\n",
    206   1.1    bouyer 		prog);
    207   1.1    bouyer 	exit(1);
    208   1.1    bouyer }
    209   1.1    bouyer 
    210   1.1    bouyer /******************************************************************************
    211   1.1    bouyer  * Card description
    212   1.1    bouyer  */
    213   1.1    bouyer 
    214   1.3     joerg static int
    215   1.1    bouyer describe_card(int fd, int verbosity, int globalparam)
    216   1.1    bouyer {
    217   1.1    bouyer 	struct amr_enquiry *ae;
    218   1.2     lukem 	uint32_t	cardtype;
    219   1.1    bouyer 
    220   1.1    bouyer 	/*
    221   1.1    bouyer 	 * Try the 40LD firmware interface
    222   1.1    bouyer 	 */
    223   1.1    bouyer 
    224   1.1    bouyer 	enq_result = amr_ioctl_enquiry(fd, AMR_CMD_CONFIG,
    225   1.1    bouyer 		AMR_CONFIG_PRODUCT_INFO, 0);
    226   1.1    bouyer 	if (enq_result == AMR_STATUS_SUCCESS) {
    227   1.1    bouyer 		struct amr_prodinfo *ap;
    228   1.1    bouyer 
    229   1.1    bouyer 		ap = (struct amr_prodinfo *)enq_buffer;
    230   1.1    bouyer 		nschan = ap->ap_nschan;
    231   1.1    bouyer 		if (globalparam) {
    232   1.1    bouyer 			printf("Product\t\t\t<%.80s>\n", ap->ap_product);
    233   1.1    bouyer 			printf("Firmware\t\t%.16s\n", ap->ap_firmware);
    234   1.1    bouyer 			printf("BIOS\t\t\t%.16s\n", ap->ap_bios);
    235   1.1    bouyer 			printf("SCSI channels\t\t%d\n", ap->ap_nschan);
    236   1.1    bouyer 			printf("Fibre loops\t\t%d\n", ap->ap_fcloops);
    237   1.1    bouyer 			printf("Memory size\t\t%d MB\n", ap->ap_memsize);
    238   1.1    bouyer 			if (verbosity >= 1) {
    239   1.1    bouyer 				printf("Ioctl\t\t\t%d (%s)\n", FIRMWARE_40LD,
    240   1.1    bouyer 				       "40LD");
    241   1.1    bouyer 				printf("Signature\t\t0x%08x\n",
    242   1.1    bouyer 				       ap->ap_signature);
    243   1.1    bouyer 				printf("Configsig\t\t0x%08x\n",
    244   1.1    bouyer 				       ap->ap_configsig);
    245   1.1    bouyer 				printf("Subsystem\t\t0x%04x\n",
    246   1.1    bouyer 				       ap->ap_subsystem);
    247   1.1    bouyer 				printf("Subvendor\t\t0x%04x\n",
    248   1.1    bouyer 				       ap->ap_subvendor);
    249   1.1    bouyer 				printf("Notify counters\t\t%d\n",
    250   1.1    bouyer 				       ap->ap_numnotifyctr);
    251   1.1    bouyer 			}
    252   1.1    bouyer 		}
    253   1.1    bouyer 		return FIRMWARE_40LD;
    254   1.1    bouyer 	}
    255   1.1    bouyer 	/*
    256   1.1    bouyer 	 * Try the 8LD firmware interface
    257   1.1    bouyer 	 */
    258   1.1    bouyer 
    259   1.1    bouyer 	enq_result = amr_ioctl_enquiry(fd, AMR_CMD_EXT_ENQUIRY2, 0, 0);
    260   1.1    bouyer 	ae = (struct amr_enquiry *)enq_buffer;
    261   1.1    bouyer 	if (enq_result == AMR_STATUS_SUCCESS) {
    262   1.1    bouyer 		cardtype = ae->ae_signature;
    263   1.1    bouyer 	} else {
    264   1.1    bouyer 		enq_result = amr_ioctl_enquiry(fd, AMR_CMD_ENQUIRY, 0, 0);
    265   1.1    bouyer 		cardtype = 0;
    266   1.1    bouyer 	}
    267   1.1    bouyer 
    268   1.1    bouyer 	if (enq_result == AMR_STATUS_SUCCESS) {
    269   1.1    bouyer 
    270   1.1    bouyer 		if (globalparam) {
    271   1.1    bouyer 			const char   *product = NULL;
    272   1.1    bouyer 			char	bios[100], firmware[100];
    273   1.2     lukem 			size_t	i;
    274   1.1    bouyer 
    275   1.5  jakllsch 			for (i = 0; i < __arraycount(prodtable); i++) {
    276   1.1    bouyer 				if (cardtype == prodtable[i].signature) {
    277   1.1    bouyer 					product = prodtable[i].product;
    278   1.1    bouyer 					break;
    279   1.1    bouyer 				}
    280   1.1    bouyer 			}
    281   1.1    bouyer 			if (product == NULL)
    282   1.1    bouyer 				product = "unknown card signature";
    283   1.1    bouyer 
    284   1.1    bouyer 			/*
    285   1.1    bouyer 			 * HP NetRaid controllers have a special encoding of
    286   1.1    bouyer 			 * the firmware and BIOS versions. The AMI version
    287   1.1    bouyer 			 * seems to have it as strings whereas the HP version
    288   1.1    bouyer 			 * does it with a leading uppercase character and two
    289   1.1    bouyer 			 * binary numbers.
    290   1.1    bouyer 			 */
    291   1.1    bouyer 
    292   1.1    bouyer 			if (ae->ae_adapter.aa_firmware[2] >= 'A' &&
    293   1.1    bouyer 			    ae->ae_adapter.aa_firmware[2] <= 'Z' &&
    294   1.1    bouyer 			    ae->ae_adapter.aa_firmware[1] < ' ' &&
    295   1.1    bouyer 			    ae->ae_adapter.aa_firmware[0] < ' ' &&
    296   1.1    bouyer 			    ae->ae_adapter.aa_bios[2] >= 'A' &&
    297   1.1    bouyer 			    ae->ae_adapter.aa_bios[2] <= 'Z' &&
    298   1.1    bouyer 			    ae->ae_adapter.aa_bios[1] < ' ' &&
    299   1.1    bouyer 			    ae->ae_adapter.aa_bios[0] < ' ') {
    300   1.1    bouyer 
    301   1.1    bouyer 				/*
    302   1.1    bouyer 				 * looks like we have an HP NetRaid version
    303   1.1    bouyer 				 * of the MegaRaid
    304   1.1    bouyer 				 */
    305   1.1    bouyer 
    306   1.1    bouyer 				if (cardtype == AMR_SIG_438) {
    307   1.1    bouyer 					/*
    308   1.1    bouyer 					 * the AMI 438 is a NetRaid 3si in
    309   1.1    bouyer 					 * HP-land
    310   1.1    bouyer 					 */
    311   1.1    bouyer 					product = "HP NetRaid 3si";
    312   1.1    bouyer 				}
    313  1.10  dholland 				snprintf(firmware, sizeof(firmware),
    314  1.10  dholland 					"%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.10  dholland 				snprintf(bios, sizeof(bios),
    319  1.10  dholland 					"%c.%02d.%02d",
    320   1.1    bouyer 					ae->ae_adapter.aa_bios[2],
    321   1.1    bouyer 					ae->ae_adapter.aa_bios[1],
    322   1.1    bouyer 					ae->ae_adapter.aa_bios[0]);
    323   1.1    bouyer 			} else {
    324  1.10  dholland 				snprintf(firmware, sizeof(firmware), "%.4s",
    325   1.1    bouyer 					ae->ae_adapter.aa_firmware);
    326  1.10  dholland 				snprintf(bios, sizeof(bios), "%.4s",
    327  1.10  dholland 					ae->ae_adapter.aa_bios);
    328   1.1    bouyer 			}
    329   1.1    bouyer 
    330   1.1    bouyer 			printf("Ioctl = %d (%s)\n", FIRMWARE_8LD, "8LD");
    331   1.1    bouyer 			printf("Product =\t<%s>\n", product);
    332   1.1    bouyer 			printf("Firmware =\t%s\n", firmware);
    333   1.1    bouyer 			printf("BIOS =\t%s\n", bios);
    334   1.1    bouyer 			/* printf ("SCSI Channels =\t%d\n", ae->ae_nschan); */
    335   1.1    bouyer 			/* printf ("Fibre Loops =\t%d\n", ae->ae_fcloops); */
    336   1.1    bouyer 			printf("Memory size =\t%d MB\n",
    337   1.1    bouyer 			       ae->ae_adapter.aa_memorysize);
    338   1.1    bouyer 			/*
    339   1.1    bouyer 			 * printf ("Notify counters =\t%d\n",
    340   1.1    bouyer 			 * ae->ae_numnotifyctr) ;
    341   1.1    bouyer 			 */
    342   1.1    bouyer 		}
    343   1.1    bouyer 		return FIRMWARE_8LD;
    344   1.1    bouyer 	}
    345   1.1    bouyer 	/*
    346   1.1    bouyer 	 * Neither firmware interface succeeded. Abort.
    347   1.1    bouyer 	 */
    348   1.1    bouyer 
    349   1.1    bouyer 	fprintf(stderr, "Firmware interface not supported\n");
    350   1.1    bouyer 	exit(1);
    351   1.1    bouyer 
    352   1.1    bouyer }
    353   1.1    bouyer 
    354   1.3     joerg static char *
    355   1.7  jakllsch describe_property(uint8_t prop, char *buffer)
    356   1.1    bouyer {
    357   1.2     lukem 	size_t	i;
    358   1.1    bouyer 
    359   1.1    bouyer 	strcpy(buffer, "<");
    360   1.5  jakllsch 	for (i = 0; i < __arraycount(proptable); i++) {
    361   1.1    bouyer 		if (i > 0)
    362   1.1    bouyer 			strcat(buffer, ",");
    363   1.1    bouyer 		if (prop & proptable[i].code)
    364   1.1    bouyer 			strcat(buffer, proptable[i].ifyes);
    365   1.1    bouyer 		else
    366   1.1    bouyer 			strcat(buffer, proptable[i].ifno);
    367   1.1    bouyer 	}
    368   1.1    bouyer 	strcat(buffer, ">");
    369   1.1    bouyer 
    370   1.1    bouyer 	return buffer;
    371   1.1    bouyer }
    372   1.1    bouyer 
    373   1.3     joerg static const char *
    374   1.7  jakllsch describe_state(int verbosity, uint8_t state)
    375   1.1    bouyer {
    376   1.2     lukem 	size_t	i;
    377   1.1    bouyer 
    378   1.1    bouyer 	if ((AMR_DRV_PREVSTATE(state) == AMR_DRV_CURSTATE(state)) &&
    379   1.1    bouyer 	    (AMR_DRV_CURSTATE(state) == AMR_DRV_OFFLINE) && verbosity == 0)
    380   1.1    bouyer 		return NULL;
    381   1.1    bouyer 
    382   1.5  jakllsch 	for (i = 0; i < __arraycount(statetable); i++)
    383   1.1    bouyer 		if (AMR_DRV_CURSTATE(state) == statetable[i].code)
    384   1.1    bouyer 			return (statetable[i].status);
    385   1.1    bouyer 
    386   1.1    bouyer 	return NULL;
    387   1.1    bouyer }
    388   1.1    bouyer 
    389   1.1    bouyer /******************************************************************************
    390   1.1    bouyer  * Battery status
    391   1.1    bouyer  */
    392   1.3     joerg static void
    393   1.1    bouyer describe_battery(int fd, int verbosity, int fwint, int bflags, int globalparam)
    394   1.1    bouyer {
    395   1.7  jakllsch 	uint8_t batt_status;
    396   1.2     lukem 	size_t i;
    397   1.1    bouyer 
    398   1.1    bouyer 	if (fwint == FIRMWARE_40LD) {
    399   1.1    bouyer 		enq_result = amr_ioctl_enquiry(fd, AMR_CMD_CONFIG,
    400   1.1    bouyer 			AMR_CONFIG_ENQ3, AMR_CONFIG_ENQ3_SOLICITED_FULL);
    401   1.1    bouyer 		if (enq_result == AMR_STATUS_SUCCESS) {
    402   1.1    bouyer 			struct amr_enquiry3 *ae3;
    403   1.1    bouyer 
    404   1.1    bouyer 			ae3 = (struct amr_enquiry3 *)enq_buffer;
    405   1.1    bouyer 			if (bflags || globalparam) {
    406   1.1    bouyer 				batt_status = ae3->ae_batterystatus;
    407   1.1    bouyer 				printf("Battery status\t\t");
    408   1.5  jakllsch 				for (i = 0; i < __arraycount(battable); i++) {
    409   1.1    bouyer 					if (batt_status & battable[i].code)
    410   1.1    bouyer 						printf("%s, ", battable[i].status);
    411   1.1    bouyer 				}
    412   1.1    bouyer 				if (!(batt_status &
    413   1.1    bouyer 				    (AMR_BATT_MODULE_MISSING|AMR_BATT_PACK_MISSING))) {
    414   1.5  jakllsch 					for (i = 0;
    415   1.5  jakllsch 					     i < __arraycount(bcstatble); i++)
    416   1.1    bouyer 						if (bcstatble[i].code ==
    417   1.1    bouyer 						    (batt_status & AMR_BATT_CHARGE_MASK))
    418   1.1    bouyer 							printf("%s", bcstatble[i].status);
    419   1.1    bouyer 				} else
    420   1.1    bouyer 					printf("charge unknown");
    421   1.1    bouyer 				if (verbosity)
    422   1.1    bouyer 					printf(" (0x%02x)", batt_status);
    423   1.1    bouyer 				printf("\n");
    424   1.1    bouyer 			}
    425   1.1    bouyer 		}
    426   1.1    bouyer 	} else if (fwint == FIRMWARE_8LD) {
    427   1.1    bouyer 		/* Nothing to do here. */
    428   1.1    bouyer 		return;
    429   1.1    bouyer 	} else {
    430   1.1    bouyer 		fprintf(stderr, "Firmware interface not supported.\n");
    431   1.1    bouyer 		exit(1);
    432   1.1    bouyer 	}
    433   1.1    bouyer 
    434   1.1    bouyer 	return;
    435   1.1    bouyer }
    436   1.1    bouyer 
    437   1.1    bouyer /******************************************************************************
    438   1.1    bouyer  * Logical volumes
    439   1.1    bouyer  */
    440   1.1    bouyer 
    441   1.3     joerg static void
    442   1.1    bouyer describe_one_volume(int ldrv, int verbosity,
    443   1.7  jakllsch 		    uint32_t size, uint8_t state, uint8_t prop)
    444   1.1    bouyer {
    445   1.1    bouyer 	float	szgb;
    446   1.1    bouyer 	int	raid_level;
    447   1.1    bouyer 	char	propstr[MAXPATHLEN];
    448   1.1    bouyer 	const char *statestr;
    449   1.1    bouyer 
    450   1.1    bouyer 	szgb = ((float)size) / (1024 * 1024 * 2);	/* size in GB */
    451   1.1    bouyer 
    452   1.1    bouyer 	raid_level = prop & AMR_DRV_RAID_MASK;
    453   1.1    bouyer 
    454   1.1    bouyer 	printf("Logical volume %d\t", ldrv);
    455   1.1    bouyer 	statestr = describe_state(verbosity, state);
    456  1.12       mrg 	if (statestr)
    457  1.12       mrg 		printf("%s ", statestr);
    458   1.1    bouyer 	printf("(%.2f GB, RAID%d", szgb, raid_level);
    459   1.1    bouyer 	if (verbosity >= 1) {
    460   1.1    bouyer 		describe_property(prop, propstr);
    461   1.1    bouyer 		printf(" %s", propstr);
    462   1.1    bouyer 	}
    463   1.1    bouyer 	printf(")\n");
    464   1.1    bouyer }
    465   1.1    bouyer 
    466   1.1    bouyer /******************************************************************************
    467   1.1    bouyer  * Physical drives
    468   1.1    bouyer  */
    469   1.1    bouyer 
    470   1.3     joerg static void
    471   1.7  jakllsch describe_one_drive(int pdrv, int verbosity, uint8_t state)
    472   1.1    bouyer {
    473   1.1    bouyer 	const char *statestr;
    474   1.1    bouyer 
    475   1.1    bouyer 	statestr = describe_state(verbosity, state);
    476   1.1    bouyer 	if (statestr) {
    477   1.1    bouyer 		if (nschan > 0)
    478   1.1    bouyer 			printf("Physical drive %d:%d\t%s\n",
    479   1.1    bouyer 			       pdrv / AMR_MAX_NSDEVS, pdrv % AMR_MAX_NSDEVS,
    480   1.1    bouyer 			       statestr);
    481   1.1    bouyer 		else
    482   1.1    bouyer 			printf("Physical drive %d:\t%s\n", pdrv, statestr);
    483   1.1    bouyer 	}
    484   1.1    bouyer }
    485   1.1    bouyer 
    486   1.3     joerg static void
    487   1.1    bouyer describe_drive(int verbosity, int fwint, int ldrv, int sbus, int sdev)
    488   1.1    bouyer {
    489   1.1    bouyer 	int	drv, pdrv = -1;
    490   1.1    bouyer 
    491   1.1    bouyer 	if (sbus > -1 && sdev > -1)
    492   1.1    bouyer 		pdrv = (sbus * AMR_MAX_NSDEVS) + sdev;
    493   1.1    bouyer 	if (nschan != 0) {
    494   1.1    bouyer 		if (sbus > -1 && sbus >= nschan) {
    495   1.1    bouyer 			fprintf(stderr, "SCSI channel %d does not exist.\n", sbus);
    496   1.1    bouyer 			exit(1);
    497   1.1    bouyer 		} else if (sdev > -1 && sdev >= AMR_MAX_NSDEVS) {
    498   1.1    bouyer 			fprintf(stderr, "SCSI device %d:%d does not exist.\n",
    499   1.1    bouyer 				sbus, sdev);
    500   1.1    bouyer 			exit(1);
    501   1.1    bouyer 		}
    502   1.1    bouyer 	}
    503   1.1    bouyer 	if (fwint == FIRMWARE_40LD) {
    504   1.1    bouyer 		if (enq_result == AMR_STATUS_SUCCESS) {
    505   1.1    bouyer 			struct amr_enquiry3 *ae3;
    506   1.1    bouyer 
    507   1.1    bouyer 			ae3 = (struct amr_enquiry3 *)enq_buffer;
    508   1.1    bouyer 			if ((ldrv < 0 && sbus < 0) || ldrv >= 0) {
    509   1.1    bouyer 				if (ldrv >= ae3->ae_numldrives) {
    510   1.1    bouyer 					fprintf(stderr, "Logical volume %d "
    511   1.1    bouyer 						"does not exist.\n", ldrv);
    512   1.1    bouyer 					exit(1);
    513   1.1    bouyer 				}
    514   1.1    bouyer 				if (ldrv < 0) {
    515   1.1    bouyer 					for (drv = 0;
    516   1.1    bouyer 					     drv < ae3->ae_numldrives;
    517   1.1    bouyer 					     drv++)
    518   1.1    bouyer 						describe_one_volume(drv,
    519   1.1    bouyer 						    verbosity,
    520   1.1    bouyer 						    ae3->ae_drivesize[drv],
    521   1.1    bouyer 						    ae3->ae_drivestate[drv],
    522   1.1    bouyer 						    ae3->ae_driveprop[drv]);
    523   1.1    bouyer 				} else {
    524   1.1    bouyer 					describe_one_volume(ldrv,
    525   1.1    bouyer 					    verbosity,
    526   1.1    bouyer 					    ae3->ae_drivesize[ldrv],
    527   1.1    bouyer 					    ae3->ae_drivestate[ldrv],
    528   1.1    bouyer 					    ae3->ae_driveprop[ldrv]);
    529   1.1    bouyer 				}
    530   1.1    bouyer 			}
    531   1.1    bouyer 			if ((ldrv < 0 && sbus < 0) || sbus >= 0) {
    532   1.1    bouyer 				if (pdrv >= AMR_40LD_MAXPHYSDRIVES ||
    533   1.1    bouyer 				    (nschan != 0 && pdrv >= (nschan * AMR_MAX_NSDEVS))) {
    534   1.1    bouyer 					fprintf(stderr, "Physical drive %d "
    535   1.1    bouyer 						"is out of range.\n", pdrv);
    536   1.1    bouyer 					exit(1);
    537   1.1    bouyer 				}
    538   1.1    bouyer 				if (sbus < 0) {
    539   1.1    bouyer 					for (drv = 0;
    540   1.1    bouyer 					     drv < AMR_40LD_MAXPHYSDRIVES;
    541   1.1    bouyer 					     drv++) {
    542   1.1    bouyer 						if (nschan != 0 &&
    543   1.1    bouyer 						    drv >= (nschan * AMR_MAX_NSDEVS))
    544   1.1    bouyer 							break;
    545   1.1    bouyer 						describe_one_drive(drv,
    546   1.1    bouyer 						    verbosity,
    547   1.1    bouyer 						    ae3->ae_pdrivestate[drv]);
    548   1.1    bouyer 					}
    549   1.1    bouyer 				} else if (sdev < 0) {
    550   1.1    bouyer 					for (drv = sbus * AMR_MAX_NSDEVS;
    551   1.1    bouyer 					     drv < ((sbus + 1) * AMR_MAX_NSDEVS);
    552   1.1    bouyer 					     drv++) {
    553   1.1    bouyer 						if (nschan != 0 &&
    554   1.1    bouyer 						    drv >= (nschan * AMR_MAX_NSDEVS))
    555   1.1    bouyer 							break;
    556   1.1    bouyer 						describe_one_drive(drv,
    557   1.1    bouyer 						    verbosity,
    558   1.1    bouyer 						    ae3->ae_pdrivestate[drv]);
    559   1.1    bouyer 					}
    560   1.1    bouyer 				} else {
    561   1.1    bouyer 					if (nschan != 0 &&
    562   1.1    bouyer 					    pdrv < (nschan * AMR_MAX_NSDEVS))
    563   1.1    bouyer 						describe_one_drive(pdrv, 1,
    564   1.1    bouyer 						    ae3->ae_pdrivestate[pdrv]);
    565   1.1    bouyer 				}
    566   1.1    bouyer 			}
    567   1.1    bouyer 		}
    568   1.1    bouyer 	} else if (fwint == FIRMWARE_8LD) {
    569   1.1    bouyer 		/* Nothing to do here. */
    570   1.1    bouyer 		return;
    571   1.1    bouyer 	} else {
    572   1.1    bouyer 		fprintf(stderr, "Firmware interface not supported.\n");
    573   1.1    bouyer 		exit(1);
    574   1.1    bouyer 	}
    575   1.1    bouyer }
    576   1.1    bouyer 
    577   1.1    bouyer /******************************************************************************
    578   1.1    bouyer  * Main function
    579   1.1    bouyer  */
    580   1.1    bouyer 
    581   1.1    bouyer int
    582   1.1    bouyer main(int argc, char *argv[])
    583   1.1    bouyer {
    584   1.1    bouyer 	int	i;
    585   1.1    bouyer 	int	fd = -1;
    586   1.1    bouyer 	int	globalparam = 0, verbosity = 0;
    587   1.1    bouyer 	int	bflags = 0, fflags = 0, sflags = 0;
    588   1.1    bouyer 	int	lvolno = -1, physno = -1;
    589   1.1    bouyer 	int	sbusno = -1, targetno = -1;
    590   1.1    bouyer 	char	filename[MAXPATHLEN];
    591   1.1    bouyer 	char	sdev[MAXPATHLEN];
    592   1.1    bouyer 	char	*pdev;
    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.11     sevan 	while ((i = getopt(argc, argv, "a:b: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