xref: /dragonfly/usr.sbin/mlxcontrol/interface.c (revision 36a3d1d6)
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/interface.c,v 1.2.2.1 2000/04/24 19:44:46 msmith Exp $
27  *	$DragonFly: src/usr.sbin/mlxcontrol/interface.c,v 1.3 2003/08/08 04:18:46 dillon Exp $
28  */
29 
30 #include <fcntl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <unistd.h>
34 #include <string.h>
35 #include <cam/scsi/scsi_all.h>
36 
37 #include <dev/raid/mlx/mlxio.h>
38 #include <dev/raid/mlx/mlxreg.h>
39 
40 #include "mlxcontrol.h"
41 
42 /********************************************************************************
43  * Iterate over all mlx devices, call (func) with each ones' path and (arg)
44  */
45 void
46 mlx_foreach(void (*func)(int unit, void *arg), void *arg)
47 {
48     int		i, fd;
49 
50     /* limit total count for sanity */
51     for (i = 0; i < 64; i++) {
52 	/* verify we can open it */
53 	if ((fd = open(ctrlrpath(i), 0)) >= 0)
54 	    close(fd);
55 	/* if we can, do */
56 	if (fd >= 0) {
57 	    func(i, arg);
58 	}
59     }
60 }
61 
62 /********************************************************************************
63  * Open the controller (unit) and give the fd to (func) along with (arg)
64  */
65 void
66 mlx_perform(int unit, void (*func)(int fd, void *arg), void *arg)
67 {
68     int		fd;
69 
70     if ((fd = open(ctrlrpath(unit), 0)) >= 0) {
71 	func(fd, arg);
72 	close(fd);
73     }
74 }
75 
76 /********************************************************************************
77  * Iterate over all mlxd devices, call (func) with each ones' path and (arg)
78  */
79 void
80 mlxd_foreach_ctrlr(int unit, void *arg)
81 {
82     struct mlxd_foreach_action	*ma = (struct mlxd_foreach_action *)arg;
83     int				i, fd;
84 
85     /* Get the device */
86     if ((fd = open(ctrlrpath(unit), 0)) < 0)
87 	return;
88 
89     for (i = -1; ;) {
90 	/* Get the unit number of the next child device */
91 	if (ioctl(fd, MLX_NEXT_CHILD, &i) < 0)
92 	    return;
93 
94 	/* check that we can open this unit */
95 	if ((fd = open(drivepath(i), 0)) >= 0)
96 	    close(fd);
97 	/* if we can, do */
98 	if (fd >= 0) {
99 	    ma->func(i, ma->arg);
100 	}
101     }
102 }
103 
104 void
105 mlxd_foreach(void (*func)(int unit, void *arg), void *arg)
106 {
107     struct mlxd_foreach_action ma;
108 
109     ma.func = func;
110     ma.arg = arg;
111     mlx_foreach(mlxd_foreach_ctrlr, &ma);
112 }
113 
114 /********************************************************************************
115  * Find the controller that manages the drive (unit), return controller number
116  * and system drive number on that controller.
117  */
118 static struct
119 {
120     int		unit;
121     int		ctrlr;
122     int		sysdrive;
123 } mlxd_find_ctrlr_param;
124 
125 static void
126 mlxd_find_ctrlr_search(int unit, void *arg)
127 {
128     int				i, fd;
129 
130     /* Get the device */
131     if ((fd = open(ctrlrpath(unit), 0)) >= 0) {
132 	for (i = -1; ;) {
133 	    /* Get the unit number of the next child device */
134 	    if (ioctl(fd, MLX_NEXT_CHILD, &i) < 0)
135 		break;
136 
137 	    /* is this child the unit we want? */
138 	    if (i == mlxd_find_ctrlr_param.unit) {
139 		mlxd_find_ctrlr_param.ctrlr = unit;
140 		if (ioctl(fd, MLX_GET_SYSDRIVE, &i) == 0)
141 		    mlxd_find_ctrlr_param.sysdrive = i;
142 	    }
143 	}
144 	close(fd);
145     }
146 }
147 
148 int
149 mlxd_find_ctrlr(int unit, int *ctrlr, int *sysdrive)
150 {
151     mlxd_find_ctrlr_param.unit = unit;
152     mlxd_find_ctrlr_param.ctrlr = -1;
153     mlxd_find_ctrlr_param.sysdrive = -1;
154 
155     mlx_foreach(mlxd_find_ctrlr_search, NULL);
156     if ((mlxd_find_ctrlr_param.ctrlr != -1) && (mlxd_find_ctrlr_param.sysdrive != -1)) {
157 	*ctrlr = mlxd_find_ctrlr_param.ctrlr;
158 	*sysdrive = mlxd_find_ctrlr_param.sysdrive;
159 	return(0);
160     }
161     return(1);
162 }
163 
164 
165 /********************************************************************************
166  * Send a command to the controller on (fd)
167  */
168 
169 void
170 mlx_command(int fd, void *arg)
171 {
172     struct mlx_usercommand	*cmd = (struct mlx_usercommand *)arg;
173     int				error;
174 
175     error = ioctl(fd, MLX_COMMAND, cmd);
176     if (error != 0)
177 	cmd->mu_error = error;
178 }
179 
180 /********************************************************************************
181  * Perform an ENQUIRY2 command and return information related to the controller
182  * (unit)
183  */
184 int
185 mlx_enquiry(int unit, struct mlx_enquiry2 *enq)
186 {
187     struct mlx_usercommand	cmd;
188 
189     /* build the command */
190     cmd.mu_datasize = sizeof(*enq);
191     cmd.mu_buf = enq;
192     cmd.mu_bufptr = 8;
193     cmd.mu_command[0] = MLX_CMD_ENQUIRY2;
194 
195     /* hand it off for processing */
196     mlx_perform(unit, mlx_command, (void *)&cmd);
197 
198     return(cmd.mu_status != 0);
199 }
200 
201 
202 /********************************************************************************
203  * Perform a READ CONFIGURATION command and return information related to the controller
204  * (unit)
205  */
206 int
207 mlx_read_configuration(int unit, struct mlx_core_cfg *cfg)
208 {
209     struct mlx_usercommand	cmd;
210 
211     /* build the command */
212     cmd.mu_datasize = sizeof(*cfg);
213     cmd.mu_buf = cfg;
214     cmd.mu_bufptr = 8;
215     cmd.mu_command[0] = MLX_CMD_READ_CONFIG;
216 
217     /* hand it off for processing */
218     mlx_perform(unit, mlx_command, (void *)&cmd);
219 
220     return(cmd.mu_status != 0);
221 }
222 
223 /********************************************************************************
224  * Perform a SCSI INQUIRY command and return pointers to the relevant data.
225  */
226 int
227 mlx_scsi_inquiry(int unit, int channel, int target, char **vendor, char **device, char **revision)
228 {
229     struct mlx_usercommand	cmd;
230     static struct {
231 	    struct mlx_dcdb		dcdb;
232 	    union {
233 		struct scsi_inquiry_data	inq;
234 		u_int8_t			pad[SHORT_INQUIRY_LENGTH];
235 	    } d;
236     } __attribute__ ((packed))		dcdb_cmd;
237     struct scsi_inquiry		*inq_cmd = (struct scsi_inquiry *)&dcdb_cmd.dcdb.dcdb_cdb[0];
238 
239     /* build the command */
240     cmd.mu_datasize = sizeof(dcdb_cmd);
241     cmd.mu_buf = &dcdb_cmd;
242     cmd.mu_command[0] = MLX_CMD_DIRECT_CDB;
243 
244     /* build the DCDB */
245     bzero(&dcdb_cmd, sizeof(dcdb_cmd));
246     dcdb_cmd.dcdb.dcdb_channel = channel;
247     dcdb_cmd.dcdb.dcdb_target = target;
248     dcdb_cmd.dcdb.dcdb_flags = MLX_DCDB_DATA_IN | MLX_DCDB_TIMEOUT_10S;
249     dcdb_cmd.dcdb.dcdb_datasize = SHORT_INQUIRY_LENGTH;
250     dcdb_cmd.dcdb.dcdb_cdb_length = 6;
251     dcdb_cmd.dcdb.dcdb_sense_length = SSD_FULL_SIZE;
252 
253     /* build the cdb */
254     inq_cmd->opcode = INQUIRY;
255     inq_cmd->length = SHORT_INQUIRY_LENGTH;
256 
257     /* hand it off for processing */
258     mlx_perform(unit, mlx_command, &cmd);
259 
260     if (cmd.mu_status == 0) {
261 	*vendor = &dcdb_cmd.d.inq.vendor[0];
262 	*device = &dcdb_cmd.d.inq.product[0];
263 	*revision = &dcdb_cmd.d.inq.revision[0];
264     }
265     return(cmd.mu_status);
266 }
267 
268 /********************************************************************************
269  * Perform a GET DEVICE STATE command and return pointers to the relevant data.
270  */
271 int
272 mlx_get_device_state(int unit, int channel, int target, struct mlx_phys_drv *drv)
273 {
274     struct mlx_usercommand	cmd;
275 
276     /* build the command */
277     cmd.mu_datasize = sizeof(*drv);
278     cmd.mu_buf = drv;
279     cmd.mu_bufptr = 8;
280     cmd.mu_command[0] = MLX_CMD_DEVICE_STATE;
281     cmd.mu_command[2] = channel;
282     cmd.mu_command[3] = target;
283 
284     /* hand it off for processing */
285     mlx_perform(unit, mlx_command, (void *)&cmd);
286 
287     return(cmd.mu_status != 0);
288 }
289