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