Home | History | Annotate | Line # | Download | only in mlxctl
config.c revision 1.3
      1 /*	$NetBSD: config.c,v 1.3 2007/02/21 20:53:59 hubertf Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*-
     40  * Copyright (c) 1999 Michael Smith
     41  * All rights reserved.
     42  *
     43  * Redistribution and use in source and binary forms, with or without
     44  * modification, are permitted provided that the following conditions
     45  * are met:
     46  * 1. Redistributions of source code must retain the above copyright
     47  *    notice, this list of conditions and the following disclaimer.
     48  * 2. Redistributions in binary form must reproduce the above copyright
     49  *    notice, this list of conditions and the following disclaimer in the
     50  *    documentation and/or other materials provided with the distribution.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  * SUCH DAMAGE.
     63  *
     64  * from FreeBSD: config.c,v 1.2 2000/04/11 23:04:17 msmith Exp
     65  */
     66 
     67 #ifndef lint
     68 #include <sys/cdefs.h>
     69 __RCSID("$NetBSD: config.c,v 1.3 2007/02/21 20:53:59 hubertf Exp $");
     70 #endif /* not lint */
     71 
     72 #include <sys/types.h>
     73 #include <sys/param.h>
     74 #include <sys/ioctl.h>
     75 #include <sys/queue.h>
     76 
     77 #include <dev/ic/mlxreg.h>
     78 #include <dev/ic/mlxio.h>
     79 
     80 #include <err.h>
     81 #include <fcntl.h>
     82 #include <stdio.h>
     83 #include <stdlib.h>
     84 #include <string.h>
     85 #include <unistd.h>
     86 #include <getopt.h>
     87 
     88 #include "extern.h"
     89 
     90 struct conf_phys_drv {
     91 	TAILQ_ENTRY(conf_phys_drv)	pd_link;
     92 	int				pd_bus;
     93 	int				pd_target;
     94 	struct mlx_phys_drv		pd_drv;
     95 };
     96 
     97 struct conf_span {
     98 	TAILQ_ENTRY(conf_span)		s_link;
     99 	struct conf_phys_drv		*s_drvs[8];
    100 	struct mlx_sys_drv_span		s_span;
    101 };
    102 
    103 struct conf_sys_drv {
    104 	TAILQ_ENTRY(conf_sys_drv)	sd_link;
    105 	struct conf_span		*sd_spans[4];
    106 	struct mlx_sys_drv		sd_drv;
    107 };
    108 
    109 struct conf_config {
    110 	TAILQ_HEAD(,conf_phys_drv)	cc_phys_drvs;
    111 	TAILQ_HEAD(,conf_span)		cc_spans;
    112 	TAILQ_HEAD(,conf_sys_drv)	cc_sys_drvs;
    113 	struct conf_sys_drv		*cc_drives[32];
    114 	struct mlx_core_cfg		cc_cfg;
    115 };
    116 
    117 static void	print_span(struct mlx_sys_drv_span *, int);
    118 static void	print_sys_drive(struct conf_config *, int);
    119 static void	print_phys_drive(struct conf_config *, int, int);
    120 
    121 /*
    122  * Get the configuration from the selected controller.
    123  *
    124  * config <controller>
    125  *		Print the configuration for <controller>
    126  *
    127  * XXX update to support adding/deleting drives.
    128  */
    129 int
    130 cmd_config(char **argv)
    131 {
    132 	char hostname[MAXHOSTNAMELEN];
    133 	struct conf_config conf;
    134 	int i, j;
    135 
    136 	if (ci.ci_firmware_id[0] < 3) {
    137 		warnx("action not supported by this firmware version");
    138 		return (1);
    139 	}
    140 
    141 	memset(&conf.cc_cfg, 0, sizeof(conf.cc_cfg));
    142 	mlx_configuration(&conf.cc_cfg, 0);
    143 
    144 	gethostname(hostname, sizeof(hostname));
    145 	printf("# controller %s on %s\n", mlxname, hostname);
    146 
    147 	printf("#\n# physical devices connected:\n");
    148 	for (i = 0; i < 5; i++)
    149 		for (j = 0; j < 16; j++)
    150 			print_phys_drive(&conf, i, j);
    151 
    152 	printf("#\n# system drives defined:\n");
    153 	for (i = 0; i < conf.cc_cfg.cc_num_sys_drives; i++)
    154 		print_sys_drive(&conf, i);
    155 
    156 	return(0);
    157 }
    158 
    159 /*
    160  * Print details for the system drive (drvno) in a format that we will be
    161  * able to parse later.
    162  *
    163  * drive?? <raidlevel> <writemode>
    164  *   span? 0x????????-0x???????? ????blks on <disk> [...]
    165  *   ...
    166  */
    167 static void
    168 print_span(struct mlx_sys_drv_span *span, int arms)
    169 {
    170 	int i;
    171 
    172 	printf("0x%08x-0x%08x %u blks on", span->sp_start_lba,
    173 	    span->sp_start_lba + span->sp_nblks, span->sp_nblks);
    174 
    175 	for (i = 0; i < arms; i++)
    176 		printf(" disk%02d%02d", span->sp_arm[i] >> 4,
    177 		    span->sp_arm[i] & 0x0f);
    178 
    179 	printf("\n");
    180 }
    181 
    182 static void
    183 print_sys_drive(struct conf_config *conf, int drvno)
    184 {
    185 	struct mlx_sys_drv *drv;
    186 	int i;
    187 
    188 	drv = &conf->cc_cfg.cc_sys_drives[drvno];
    189 
    190 	printf("drive%02d ", drvno);
    191 
    192 	switch(drv->sd_raidlevel & 0xf) {
    193 	case MLX_SYS_DRV_RAID0:
    194 		printf("RAID0");
    195 		break;
    196 	 case MLX_SYS_DRV_RAID1:
    197 		printf("RAID1");
    198 		break;
    199 	case MLX_SYS_DRV_RAID3:
    200 		printf("RAID3");
    201 		break;
    202 	case MLX_SYS_DRV_RAID5:
    203 		printf("RAID5");
    204 		break;
    205 	case MLX_SYS_DRV_RAID6:
    206 		printf("RAID6");
    207 		break;
    208 	case MLX_SYS_DRV_JBOD:
    209 		printf("JBOD");
    210 		break;
    211 	default:
    212 		printf("RAID?");
    213 		break;
    214 	}
    215 
    216 	printf(" write%s\n",
    217 	    drv->sd_raidlevel & MLX_SYS_DRV_WRITEBACK ? "back" : "through");
    218 
    219 	for (i = 0; i < drv->sd_valid_spans; i++) {
    220 		printf("  span%d ", i);
    221 		print_span(&drv->sd_span[i], drv->sd_valid_arms);
    222 	}
    223 }
    224 
    225 /*
    226  * Print details for the physical drive at chn/targ in a format suitable for
    227  * human consumption.
    228  */
    229 static void
    230 print_phys_drive(struct conf_config *conf, int chn, int targ)
    231 {
    232 	struct mlx_phys_drv *pd;
    233 
    234 	pd = &conf->cc_cfg.cc_phys_drives[chn * 16 + targ];
    235 
    236 	/* If the drive isn't present, don't print it. */
    237 	if ((pd->pd_flags1 & MLX_PHYS_DRV_PRESENT) == 0)
    238 		return;
    239 
    240 	mlx_print_phys_drv(pd, chn, targ, "# ");
    241 }
    242