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