xref: /netbsd-src/usr.sbin/mlxctl/config.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: config.c,v 1.4 2007/03/26 23:08:29 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.4 2007/03/26 23:08:29 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 
87 #include "extern.h"
88 
89 struct conf_phys_drv {
90 	TAILQ_ENTRY(conf_phys_drv)	pd_link;
91 	int				pd_bus;
92 	int				pd_target;
93 	struct mlx_phys_drv		pd_drv;
94 };
95 
96 struct conf_span {
97 	TAILQ_ENTRY(conf_span)		s_link;
98 	struct conf_phys_drv		*s_drvs[8];
99 	struct mlx_sys_drv_span		s_span;
100 };
101 
102 struct conf_sys_drv {
103 	TAILQ_ENTRY(conf_sys_drv)	sd_link;
104 	struct conf_span		*sd_spans[4];
105 	struct mlx_sys_drv		sd_drv;
106 };
107 
108 struct conf_config {
109 	TAILQ_HEAD(,conf_phys_drv)	cc_phys_drvs;
110 	TAILQ_HEAD(,conf_span)		cc_spans;
111 	TAILQ_HEAD(,conf_sys_drv)	cc_sys_drvs;
112 	struct conf_sys_drv		*cc_drives[32];
113 	struct mlx_core_cfg		cc_cfg;
114 };
115 
116 static void	print_span(struct mlx_sys_drv_span *, int);
117 static void	print_sys_drive(struct conf_config *, int);
118 static void	print_phys_drive(struct conf_config *, int, int);
119 
120 /*
121  * Get the configuration from the selected controller.
122  *
123  * config <controller>
124  *		Print the configuration for <controller>
125  *
126  * XXX update to support adding/deleting drives.
127  */
128 int
129 cmd_config(char **argv)
130 {
131 	char hostname[MAXHOSTNAMELEN];
132 	struct conf_config conf;
133 	int i, j;
134 
135 	if (ci.ci_firmware_id[0] < 3) {
136 		warnx("action not supported by this firmware version");
137 		return (1);
138 	}
139 
140 	memset(&conf.cc_cfg, 0, sizeof(conf.cc_cfg));
141 	mlx_configuration(&conf.cc_cfg, 0);
142 
143 	gethostname(hostname, sizeof(hostname));
144 	printf("# controller %s on %s\n", mlxname, hostname);
145 
146 	printf("#\n# physical devices connected:\n");
147 	for (i = 0; i < 5; i++)
148 		for (j = 0; j < 16; j++)
149 			print_phys_drive(&conf, i, j);
150 
151 	printf("#\n# system drives defined:\n");
152 	for (i = 0; i < conf.cc_cfg.cc_num_sys_drives; i++)
153 		print_sys_drive(&conf, i);
154 
155 	return(0);
156 }
157 
158 /*
159  * Print details for the system drive (drvno) in a format that we will be
160  * able to parse later.
161  *
162  * drive?? <raidlevel> <writemode>
163  *   span? 0x????????-0x???????? ????blks on <disk> [...]
164  *   ...
165  */
166 static void
167 print_span(struct mlx_sys_drv_span *span, int arms)
168 {
169 	int i;
170 
171 	printf("0x%08x-0x%08x %u blks on", span->sp_start_lba,
172 	    span->sp_start_lba + span->sp_nblks, span->sp_nblks);
173 
174 	for (i = 0; i < arms; i++)
175 		printf(" disk%02d%02d", span->sp_arm[i] >> 4,
176 		    span->sp_arm[i] & 0x0f);
177 
178 	printf("\n");
179 }
180 
181 static void
182 print_sys_drive(struct conf_config *conf, int drvno)
183 {
184 	struct mlx_sys_drv *drv;
185 	int i;
186 
187 	drv = &conf->cc_cfg.cc_sys_drives[drvno];
188 
189 	printf("drive%02d ", drvno);
190 
191 	switch(drv->sd_raidlevel & 0xf) {
192 	case MLX_SYS_DRV_RAID0:
193 		printf("RAID0");
194 		break;
195 	 case MLX_SYS_DRV_RAID1:
196 		printf("RAID1");
197 		break;
198 	case MLX_SYS_DRV_RAID3:
199 		printf("RAID3");
200 		break;
201 	case MLX_SYS_DRV_RAID5:
202 		printf("RAID5");
203 		break;
204 	case MLX_SYS_DRV_RAID6:
205 		printf("RAID6");
206 		break;
207 	case MLX_SYS_DRV_JBOD:
208 		printf("JBOD");
209 		break;
210 	default:
211 		printf("RAID?");
212 		break;
213 	}
214 
215 	printf(" write%s\n",
216 	    drv->sd_raidlevel & MLX_SYS_DRV_WRITEBACK ? "back" : "through");
217 
218 	for (i = 0; i < drv->sd_valid_spans; i++) {
219 		printf("  span%d ", i);
220 		print_span(&drv->sd_span[i], drv->sd_valid_arms);
221 	}
222 }
223 
224 /*
225  * Print details for the physical drive at chn/targ in a format suitable for
226  * human consumption.
227  */
228 static void
229 print_phys_drive(struct conf_config *conf, int chn, int targ)
230 {
231 	struct mlx_phys_drv *pd;
232 
233 	pd = &conf->cc_cfg.cc_phys_drives[chn * 16 + targ];
234 
235 	/* If the drive isn't present, don't print it. */
236 	if ((pd->pd_flags1 & MLX_PHYS_DRV_PRESENT) == 0)
237 		return;
238 
239 	mlx_print_phys_drv(pd, chn, targ, "# ");
240 }
241