interface.c revision 256281
1142425Snectar/*-
2160814Ssimon * Copyright (c) 1999 Michael Smith
3142425Snectar * All rights reserved.
4142425Snectar *
5142425Snectar * Redistribution and use in source and binary forms, with or without
6142425Snectar * modification, are permitted provided that the following conditions
7142425Snectar * are met:
8142425Snectar * 1. Redistributions of source code must retain the above copyright
9142425Snectar *    notice, this list of conditions and the following disclaimer.
10142425Snectar * 2. Redistributions in binary form must reproduce the above copyright
11142425Snectar *    notice, this list of conditions and the following disclaimer in the
12142425Snectar *    documentation and/or other materials provided with the distribution.
13142425Snectar *
14142425Snectar * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15142425Snectar * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16142425Snectar * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17142425Snectar * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18160814Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19142425Snectar * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20142425Snectar * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21142425Snectar * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22142425Snectar * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23142425Snectar * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24142425Snectar * SUCH DAMAGE.
25142425Snectar *
26142425Snectar *	$FreeBSD: stable/10/usr.sbin/mlxcontrol/interface.c 229997 2012-01-12 00:34:33Z ken $
27142425Snectar */
28142425Snectar
29142425Snectar#include <fcntl.h>
30142425Snectar#include <stdio.h>
31142425Snectar#include <stdlib.h>
32142425Snectar#include <unistd.h>
33142425Snectar#include <string.h>
34142425Snectar#include <cam/scsi/scsi_all.h>
35142425Snectar
36142425Snectar#include <dev/mlx/mlxio.h>
37142425Snectar#include <dev/mlx/mlxreg.h>
38142425Snectar
39142425Snectar#include "mlxcontrol.h"
40142425Snectar
41238405Sjkim/********************************************************************************
42142425Snectar * Iterate over all mlx devices, call (func) with each ones' path and (arg)
43142425Snectar */
44142425Snectarvoid
45238405Sjkimmlx_foreach(void (*func)(int unit, void *arg), void *arg)
46238405Sjkim{
47142425Snectar    int		i, fd;
48142425Snectar
49142425Snectar    /* limit total count for sanity */
50142425Snectar    for (i = 0; i < 64; i++) {
51142425Snectar	/* verify we can open it */
52142425Snectar	if ((fd = open(ctrlrpath(i), 0)) >= 0)
53142425Snectar	    close(fd);
54142425Snectar	/* if we can, do */
55142425Snectar	if (fd >= 0) {
56142425Snectar	    func(i, arg);
57160814Ssimon	}
58160814Ssimon    }
59142425Snectar}
60142425Snectar
61142425Snectar/********************************************************************************
62142425Snectar * Open the controller (unit) and give the fd to (func) along with (arg)
63142425Snectar */
64142425Snectarvoid
65142425Snectarmlx_perform(int unit, void (*func)(int fd, void *arg), void *arg)
66142425Snectar{
67142425Snectar    int		fd;
68142425Snectar
69142425Snectar    if ((fd = open(ctrlrpath(unit), 0)) >= 0) {
70142425Snectar	func(fd, arg);
71142425Snectar	close(fd);
72142425Snectar    }
73160814Ssimon}
74142425Snectar
75142425Snectar/********************************************************************************
76142425Snectar * Iterate over all mlxd devices, call (func) with each ones' path and (arg)
77142425Snectar */
78142425Snectarvoid
79142425Snectarmlxd_foreach_ctrlr(int unit, void *arg)
80142425Snectar{
81160814Ssimon    struct mlxd_foreach_action	*ma = (struct mlxd_foreach_action *)arg;
82142425Snectar    int				i, fd, ctrlfd;
83142425Snectar
84142425Snectar    /* Get the device */
85160814Ssimon    if ((ctrlfd = open(ctrlrpath(unit), 0)) < 0)
86160814Ssimon	return;
87160814Ssimon
88160814Ssimon    for (i = -1; ;) {
89160814Ssimon	/* Get the unit number of the next child device */
90160814Ssimon	if (ioctl(ctrlfd, MLX_NEXT_CHILD, &i) < 0) {
91160814Ssimon	    close(ctrlfd);
92160814Ssimon	    return;
93160814Ssimon	}
94160814Ssimon
95	/* check that we can open this unit */
96	if ((fd = open(drivepath(i), 0)) >= 0)
97	    close(fd);
98	/* if we can, do */
99	if (fd >= 0) {
100	    ma->func(i, ma->arg);
101	}
102    }
103}
104
105void
106mlxd_foreach(void (*func)(int unit, void *arg), void *arg)
107{
108    struct mlxd_foreach_action ma;
109
110    ma.func = func;
111    ma.arg = arg;
112    mlx_foreach(mlxd_foreach_ctrlr, &ma);
113}
114
115/********************************************************************************
116 * Find the controller that manages the drive (unit), return controller number
117 * and system drive number on that controller.
118 */
119static struct
120{
121    int		unit;
122    int		ctrlr;
123    int		sysdrive;
124} mlxd_find_ctrlr_param;
125
126static void
127mlxd_find_ctrlr_search(int unit, void *arg)
128{
129    int				i, fd;
130
131    /* Get the device */
132    if ((fd = open(ctrlrpath(unit), 0)) >= 0) {
133	for (i = -1; ;) {
134	    /* Get the unit number of the next child device */
135	    if (ioctl(fd, MLX_NEXT_CHILD, &i) < 0)
136		break;
137
138	    /* is this child the unit we want? */
139	    if (i == mlxd_find_ctrlr_param.unit) {
140		mlxd_find_ctrlr_param.ctrlr = unit;
141		if (ioctl(fd, MLX_GET_SYSDRIVE, &i) == 0)
142		    mlxd_find_ctrlr_param.sysdrive = i;
143	    }
144	}
145	close(fd);
146    }
147}
148
149int
150mlxd_find_ctrlr(int unit, int *ctrlr, int *sysdrive)
151{
152    mlxd_find_ctrlr_param.unit = unit;
153    mlxd_find_ctrlr_param.ctrlr = -1;
154    mlxd_find_ctrlr_param.sysdrive = -1;
155
156    mlx_foreach(mlxd_find_ctrlr_search, NULL);
157    if ((mlxd_find_ctrlr_param.ctrlr != -1) && (mlxd_find_ctrlr_param.sysdrive != -1)) {
158	*ctrlr = mlxd_find_ctrlr_param.ctrlr;
159	*sysdrive = mlxd_find_ctrlr_param.sysdrive;
160	return(0);
161    }
162    return(1);
163}
164
165
166/********************************************************************************
167 * Send a command to the controller on (fd)
168 */
169
170void
171mlx_command(int fd, void *arg)
172{
173    struct mlx_usercommand	*cmd = (struct mlx_usercommand *)arg;
174    int				error;
175
176    error = ioctl(fd, MLX_COMMAND, cmd);
177    if (error != 0)
178	cmd->mu_error = error;
179}
180
181/********************************************************************************
182 * Perform an ENQUIRY2 command and return information related to the controller
183 * (unit)
184 */
185int
186mlx_enquiry(int unit, struct mlx_enquiry2 *enq)
187{
188    struct mlx_usercommand	cmd;
189
190    /* build the command */
191    cmd.mu_datasize = sizeof(*enq);
192    cmd.mu_buf = enq;
193    cmd.mu_bufptr = 8;
194    cmd.mu_command[0] = MLX_CMD_ENQUIRY2;
195
196    /* hand it off for processing */
197    mlx_perform(unit, mlx_command, (void *)&cmd);
198
199    return(cmd.mu_status != 0);
200}
201
202
203/********************************************************************************
204 * Perform a READ CONFIGURATION command and return information related to the controller
205 * (unit)
206 */
207int
208mlx_read_configuration(int unit, struct mlx_core_cfg *cfg)
209{
210    struct mlx_usercommand	cmd;
211
212    /* build the command */
213    cmd.mu_datasize = sizeof(*cfg);
214    cmd.mu_buf = cfg;
215    cmd.mu_bufptr = 8;
216    cmd.mu_command[0] = MLX_CMD_READ_CONFIG;
217
218    /* hand it off for processing */
219    mlx_perform(unit, mlx_command, (void *)&cmd);
220
221    return(cmd.mu_status != 0);
222}
223
224/********************************************************************************
225 * Perform a SCSI INQUIRY command and return pointers to the relevant data.
226 */
227int
228mlx_scsi_inquiry(int unit, int channel, int target, char **vendor, char **device, char **revision)
229{
230    struct mlx_usercommand	cmd;
231    static struct {
232	    struct mlx_dcdb		dcdb;
233	    union {
234		struct scsi_inquiry_data	inq;
235		u_int8_t			pad[SHORT_INQUIRY_LENGTH];
236	    } d;
237    } __attribute__ ((packed))		dcdb_cmd;
238    struct scsi_inquiry		*inq_cmd = (struct scsi_inquiry *)&dcdb_cmd.dcdb.dcdb_cdb[0];
239
240    /* build the command */
241    cmd.mu_datasize = sizeof(dcdb_cmd);
242    cmd.mu_buf = &dcdb_cmd;
243    cmd.mu_command[0] = MLX_CMD_DIRECT_CDB;
244
245    /* build the DCDB */
246    bzero(&dcdb_cmd, sizeof(dcdb_cmd));
247    dcdb_cmd.dcdb.dcdb_channel = channel;
248    dcdb_cmd.dcdb.dcdb_target = target;
249    dcdb_cmd.dcdb.dcdb_flags = MLX_DCDB_DATA_IN | MLX_DCDB_TIMEOUT_10S;
250    dcdb_cmd.dcdb.dcdb_datasize = SHORT_INQUIRY_LENGTH;
251    dcdb_cmd.dcdb.dcdb_cdb_length = 6;
252    dcdb_cmd.dcdb.dcdb_sense_length = SSD_FULL_SIZE;
253
254    /* build the cdb */
255    inq_cmd->opcode = INQUIRY;
256    scsi_ulto2b(SHORT_INQUIRY_LENGTH, inq_cmd->length);
257
258    /* hand it off for processing */
259    mlx_perform(unit, mlx_command, &cmd);
260
261    if (cmd.mu_status == 0) {
262	*vendor = &dcdb_cmd.d.inq.vendor[0];
263	*device = &dcdb_cmd.d.inq.product[0];
264	*revision = &dcdb_cmd.d.inq.revision[0];
265    }
266    return(cmd.mu_status);
267}
268
269/********************************************************************************
270 * Perform a GET DEVICE STATE command and return pointers to the relevant data.
271 */
272int
273mlx_get_device_state(int unit, int channel, int target, struct mlx_phys_drv *drv)
274{
275    struct mlx_usercommand	cmd;
276
277    /* build the command */
278    cmd.mu_datasize = sizeof(*drv);
279    cmd.mu_buf = drv;
280    cmd.mu_bufptr = 8;
281    cmd.mu_command[0] = MLX_CMD_DEVICE_STATE;
282    cmd.mu_command[2] = channel;
283    cmd.mu_command[3] = target;
284
285    /* hand it off for processing */
286    mlx_perform(unit, mlx_command, (void *)&cmd);
287
288    return(cmd.mu_status != 0);
289}
290