Merge branch 'vendor/LIBARCHIVE'
[dragonfly.git] / usr.sbin / mlxcontrol / config.c
1 /*-
2  * Copyright (c) 1999 Michael Smith
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/usr.sbin/mlxcontrol/config.c,v 1.2.2.1 2000/04/24 19:44:46 msmith Exp $
27  *      $DragonFly: src/usr.sbin/mlxcontrol/config.c,v 1.3 2003/08/08 04:18:46 dillon Exp $
28  */
29
30 #include <fcntl.h>
31 #include <paths.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <err.h>
37
38 #include <dev/raid/mlx/mlxio.h>
39 #include <dev/raid/mlx/mlxreg.h>
40
41 #include "mlxcontrol.h"
42
43 static void     print_span(struct mlx_sys_drv_span *span, int arms);
44 static void     print_sys_drive(struct conf_config *conf, int drvno);
45 static void     print_phys_drive(struct conf_config *conf, int chn, int targ);
46
47 /********************************************************************************
48  * Get the configuration from the selected controller.
49  *
50  * config <controller>
51  *              Print the configuration for <controller>
52  *
53  * XXX update to support adding/deleting drives.
54  */
55
56 int
57 cmd_config(int argc, char *argv[]) 
58 {
59     struct conf_config  conf;
60     int                 unit = 0;       /* XXX */
61     int                 i, j;
62
63     bzero(&conf.cc_cfg, sizeof(conf.cc_cfg));
64     if (mlx_read_configuration(unit, &conf.cc_cfg)) {
65         printf("mlx%d: error submitting READ CONFIGURATION\n", unit);
66     } else {
67
68         printf("# Controller <INSERT DETAILS HERE>\n");
69         printf("#\n# Physical devices connected:\n");
70         for (i = 0; i < 5; i++)
71             for (j = 0; j < 16; j++)
72             print_phys_drive(&conf, i, j);
73         printf("#\n# System Drives defined:\n");
74
75         for (i = 0; i < conf.cc_cfg.cc_num_sys_drives; i++)
76             print_sys_drive(&conf, i);
77     }
78     return(0);
79 }
80
81
82 /********************************************************************************
83  * Print details for the system drive (drvno) in a format that we will be 
84  * able to parse later.
85  *
86  * drive?? <raidlevel> <writemode>
87  *   span? 0x????????-0x???????? ????MB on <disk> [...]
88  *   ...
89  */
90 static void
91 print_span(struct mlx_sys_drv_span *span, int arms)
92 {
93     int         i;
94     
95     printf("0x%08x-0x%08x %uMB on", span->sp_start_lba, span->sp_start_lba + span->sp_nblks, span->sp_nblks / 2048);
96     for (i = 0; i < arms; i++)
97         printf(" disk%02d%02d", span->sp_arm[i] >> 4, span->sp_arm[i] & 0x0f);
98     printf("\n");
99 }
100
101 static void
102 print_sys_drive(struct conf_config *conf, int drvno)
103 {
104     struct mlx_sys_drv  *drv = &conf->cc_cfg.cc_sys_drives[drvno];
105     int                 i;
106
107     printf("drive%02d ", drvno);
108     switch(drv->sd_raidlevel & 0xf) {
109     case MLX_SYS_DRV_RAID0:
110         printf("RAID0");
111         break;
112     case MLX_SYS_DRV_RAID1:
113         printf("RAID1");
114         break;
115     case MLX_SYS_DRV_RAID3:
116         printf("RAID3");
117         break;
118     case MLX_SYS_DRV_RAID5:
119         printf("RAID5");
120         break;
121     case MLX_SYS_DRV_RAID6:
122         printf("RAID6");
123         break;
124     case MLX_SYS_DRV_JBOD:
125         printf("JBOD");
126         break;
127     default:
128         printf("RAID?");
129     }
130     printf(" write%s\n", drv->sd_raidlevel & MLX_SYS_DRV_WRITEBACK ? "back" : "through");
131     
132     for (i = 0; i < drv->sd_valid_spans; i++) {
133         printf("  span%d ", i);
134         print_span(&drv->sd_span[i], drv->sd_valid_arms);
135     }
136 }
137
138 /********************************************************************************
139  * Print details for the physical drive at chn/targ in a format suitable for
140  * human consumption.
141  *
142  * <type>CCTT (<state>) "<vendor>/<model>" 
143  *                       ????MB <features>
144  *
145  */
146 static void
147 print_phys_drive(struct conf_config *conf, int chn, int targ)
148 {
149     struct mlx_phys_drv         *drv = &conf->cc_cfg.cc_phys_drives[chn * 16 + targ];
150  
151     /* if the drive isn't present, don't print it */
152     if (!(drv->pd_flags1 & MLX_PHYS_DRV_PRESENT))
153         return;
154
155     mlx_print_phys_drv(drv, chn, targ, "# ", 1);
156 }
157
158