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