xref: /freebsd/usr.sbin/mlxcontrol/interface.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1b6a7bef2SMike Smith /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
31de7b4b8SPedro F. Giffuni  *
4b6a7bef2SMike Smith  * Copyright (c) 1999 Michael Smith
5b6a7bef2SMike Smith  * All rights reserved.
6b6a7bef2SMike Smith  *
7b6a7bef2SMike Smith  * Redistribution and use in source and binary forms, with or without
8b6a7bef2SMike Smith  * modification, are permitted provided that the following conditions
9b6a7bef2SMike Smith  * are met:
10b6a7bef2SMike Smith  * 1. Redistributions of source code must retain the above copyright
11b6a7bef2SMike Smith  *    notice, this list of conditions and the following disclaimer.
12b6a7bef2SMike Smith  * 2. Redistributions in binary form must reproduce the above copyright
13b6a7bef2SMike Smith  *    notice, this list of conditions and the following disclaimer in the
14b6a7bef2SMike Smith  *    documentation and/or other materials provided with the distribution.
15b6a7bef2SMike Smith  *
16b6a7bef2SMike Smith  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17b6a7bef2SMike Smith  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18b6a7bef2SMike Smith  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19b6a7bef2SMike Smith  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20b6a7bef2SMike Smith  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21b6a7bef2SMike Smith  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22b6a7bef2SMike Smith  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23b6a7bef2SMike Smith  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24b6a7bef2SMike Smith  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25b6a7bef2SMike Smith  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26b6a7bef2SMike Smith  * SUCH DAMAGE.
27b6a7bef2SMike Smith  */
28b6a7bef2SMike Smith 
29b6a7bef2SMike Smith #include <fcntl.h>
30b6a7bef2SMike Smith #include <stdio.h>
31b6a7bef2SMike Smith #include <stdlib.h>
32b6a7bef2SMike Smith #include <unistd.h>
33b6a7bef2SMike Smith #include <string.h>
34b6a7bef2SMike Smith #include <cam/scsi/scsi_all.h>
35b6a7bef2SMike Smith 
36bb441a15SMike Smith #include <dev/mlx/mlxio.h>
37bb441a15SMike Smith #include <dev/mlx/mlxreg.h>
38b6a7bef2SMike Smith 
39b6a7bef2SMike Smith #include "mlxcontrol.h"
40b6a7bef2SMike Smith 
41b6a7bef2SMike Smith /********************************************************************************
42b6a7bef2SMike Smith  * Iterate over all mlx devices, call (func) with each ones' path and (arg)
43b6a7bef2SMike Smith  */
44b6a7bef2SMike Smith void
mlx_foreach(void (* func)(int unit,void * arg),void * arg)45b6a7bef2SMike Smith mlx_foreach(void (*func)(int unit, void *arg), void *arg)
46b6a7bef2SMike Smith {
47b6a7bef2SMike Smith     int		i, fd;
48b6a7bef2SMike Smith 
49b6a7bef2SMike Smith     /* limit total count for sanity */
50b6a7bef2SMike Smith     for (i = 0; i < 64; i++) {
51b6a7bef2SMike Smith 	/* verify we can open it */
52b6a7bef2SMike Smith 	if ((fd = open(ctrlrpath(i), 0)) >= 0)
53b6a7bef2SMike Smith 	    close(fd);
54b6a7bef2SMike Smith 	/* if we can, do */
55b6a7bef2SMike Smith 	if (fd >= 0) {
56b6a7bef2SMike Smith 	    func(i, arg);
57b6a7bef2SMike Smith 	}
58b6a7bef2SMike Smith     }
59b6a7bef2SMike Smith }
60b6a7bef2SMike Smith 
61b6a7bef2SMike Smith /********************************************************************************
62b6a7bef2SMike Smith  * Open the controller (unit) and give the fd to (func) along with (arg)
63b6a7bef2SMike Smith  */
64b6a7bef2SMike Smith void
mlx_perform(int unit,void (* func)(int fd,void * arg),void * arg)65b6a7bef2SMike Smith mlx_perform(int unit, void (*func)(int fd, void *arg), void *arg)
66b6a7bef2SMike Smith {
67b6a7bef2SMike Smith     int		fd;
68b6a7bef2SMike Smith 
69b6a7bef2SMike Smith     if ((fd = open(ctrlrpath(unit), 0)) >= 0) {
70b6a7bef2SMike Smith 	func(fd, arg);
71b6a7bef2SMike Smith 	close(fd);
72b6a7bef2SMike Smith     }
73b6a7bef2SMike Smith }
74b6a7bef2SMike Smith 
75b6a7bef2SMike Smith /********************************************************************************
76b6a7bef2SMike Smith  * Iterate over all mlxd devices, call (func) with each ones' path and (arg)
77b6a7bef2SMike Smith  */
78b6a7bef2SMike Smith void
mlxd_foreach_ctrlr(int unit,void * arg)79b6a7bef2SMike Smith mlxd_foreach_ctrlr(int unit, void *arg)
80b6a7bef2SMike Smith {
81b6a7bef2SMike Smith     struct mlxd_foreach_action	*ma = (struct mlxd_foreach_action *)arg;
8229e6fa3aSStephane E. Potvin     int				i, fd, ctrlfd;
83b6a7bef2SMike Smith 
84b6a7bef2SMike Smith     /* Get the device */
8529e6fa3aSStephane E. Potvin     if ((ctrlfd = open(ctrlrpath(unit), 0)) < 0)
86b6a7bef2SMike Smith 	return;
87b6a7bef2SMike Smith 
88b6a7bef2SMike Smith     for (i = -1; ;) {
89b6a7bef2SMike Smith 	/* Get the unit number of the next child device */
9029e6fa3aSStephane E. Potvin 	if (ioctl(ctrlfd, MLX_NEXT_CHILD, &i) < 0) {
9129e6fa3aSStephane E. Potvin 	    close(ctrlfd);
92b6a7bef2SMike Smith 	    return;
9329e6fa3aSStephane E. Potvin 	}
94b6a7bef2SMike Smith 
95b6a7bef2SMike Smith 	/* check that we can open this unit */
96b6a7bef2SMike Smith 	if ((fd = open(drivepath(i), 0)) >= 0)
97b6a7bef2SMike Smith 	    close(fd);
98b6a7bef2SMike Smith 	/* if we can, do */
99b6a7bef2SMike Smith 	if (fd >= 0) {
100b6a7bef2SMike Smith 	    ma->func(i, ma->arg);
101b6a7bef2SMike Smith 	}
102b6a7bef2SMike Smith     }
103b6a7bef2SMike Smith }
104b6a7bef2SMike Smith 
105b6a7bef2SMike Smith void
mlxd_foreach(void (* func)(int unit,void * arg),void * arg)106b6a7bef2SMike Smith mlxd_foreach(void (*func)(int unit, void *arg), void *arg)
107b6a7bef2SMike Smith {
108b6a7bef2SMike Smith     struct mlxd_foreach_action ma;
109b6a7bef2SMike Smith 
110b6a7bef2SMike Smith     ma.func = func;
111b6a7bef2SMike Smith     ma.arg = arg;
112b6a7bef2SMike Smith     mlx_foreach(mlxd_foreach_ctrlr, &ma);
113b6a7bef2SMike Smith }
114b6a7bef2SMike Smith 
115b6a7bef2SMike Smith /********************************************************************************
116b6a7bef2SMike Smith  * Find the controller that manages the drive (unit), return controller number
117b6a7bef2SMike Smith  * and system drive number on that controller.
118b6a7bef2SMike Smith  */
119b6a7bef2SMike Smith static struct
120b6a7bef2SMike Smith {
121b6a7bef2SMike Smith     int		unit;
122b6a7bef2SMike Smith     int		ctrlr;
123b6a7bef2SMike Smith     int		sysdrive;
124b6a7bef2SMike Smith } mlxd_find_ctrlr_param;
125b6a7bef2SMike Smith 
126b6a7bef2SMike Smith static void
mlxd_find_ctrlr_search(int unit,void * arg)127b6a7bef2SMike Smith mlxd_find_ctrlr_search(int unit, void *arg)
128b6a7bef2SMike Smith {
129b6a7bef2SMike Smith     int				i, fd;
130b6a7bef2SMike Smith 
131b6a7bef2SMike Smith     /* Get the device */
132b6a7bef2SMike Smith     if ((fd = open(ctrlrpath(unit), 0)) >= 0) {
133b6a7bef2SMike Smith 	for (i = -1; ;) {
134b6a7bef2SMike Smith 	    /* Get the unit number of the next child device */
135b6a7bef2SMike Smith 	    if (ioctl(fd, MLX_NEXT_CHILD, &i) < 0)
136b6a7bef2SMike Smith 		break;
137b6a7bef2SMike Smith 
138b6a7bef2SMike Smith 	    /* is this child the unit we want? */
139b6a7bef2SMike Smith 	    if (i == mlxd_find_ctrlr_param.unit) {
140b6a7bef2SMike Smith 		mlxd_find_ctrlr_param.ctrlr = unit;
141b6a7bef2SMike Smith 		if (ioctl(fd, MLX_GET_SYSDRIVE, &i) == 0)
142b6a7bef2SMike Smith 		    mlxd_find_ctrlr_param.sysdrive = i;
143b6a7bef2SMike Smith 	    }
144b6a7bef2SMike Smith 	}
145b6a7bef2SMike Smith 	close(fd);
146b6a7bef2SMike Smith     }
147b6a7bef2SMike Smith }
148b6a7bef2SMike Smith 
149b6a7bef2SMike Smith int
mlxd_find_ctrlr(int unit,int * ctrlr,int * sysdrive)150b6a7bef2SMike Smith mlxd_find_ctrlr(int unit, int *ctrlr, int *sysdrive)
151b6a7bef2SMike Smith {
152b6a7bef2SMike Smith     mlxd_find_ctrlr_param.unit = unit;
153b6a7bef2SMike Smith     mlxd_find_ctrlr_param.ctrlr = -1;
154b6a7bef2SMike Smith     mlxd_find_ctrlr_param.sysdrive = -1;
155b6a7bef2SMike Smith 
156b6a7bef2SMike Smith     mlx_foreach(mlxd_find_ctrlr_search, NULL);
157b6a7bef2SMike Smith     if ((mlxd_find_ctrlr_param.ctrlr != -1) && (mlxd_find_ctrlr_param.sysdrive != -1)) {
158b6a7bef2SMike Smith 	*ctrlr = mlxd_find_ctrlr_param.ctrlr;
159b6a7bef2SMike Smith 	*sysdrive = mlxd_find_ctrlr_param.sysdrive;
160b6a7bef2SMike Smith 	return(0);
161b6a7bef2SMike Smith     }
162b6a7bef2SMike Smith     return(1);
163b6a7bef2SMike Smith }
164b6a7bef2SMike Smith 
165b6a7bef2SMike Smith 
166b6a7bef2SMike Smith /********************************************************************************
167b6a7bef2SMike Smith  * Send a command to the controller on (fd)
168b6a7bef2SMike Smith  */
169b6a7bef2SMike Smith 
170b6a7bef2SMike Smith void
mlx_command(int fd,void * arg)171b6a7bef2SMike Smith mlx_command(int fd, void *arg)
172b6a7bef2SMike Smith {
173b6a7bef2SMike Smith     struct mlx_usercommand	*cmd = (struct mlx_usercommand *)arg;
174b6a7bef2SMike Smith     int				error;
175b6a7bef2SMike Smith 
176b6a7bef2SMike Smith     error = ioctl(fd, MLX_COMMAND, cmd);
177b6a7bef2SMike Smith     if (error != 0)
178b6a7bef2SMike Smith 	cmd->mu_error = error;
179b6a7bef2SMike Smith }
180b6a7bef2SMike Smith 
181b6a7bef2SMike Smith /********************************************************************************
182b6a7bef2SMike Smith  * Perform an ENQUIRY2 command and return information related to the controller
183b6a7bef2SMike Smith  * (unit)
184b6a7bef2SMike Smith  */
185b6a7bef2SMike Smith int
mlx_enquiry(int unit,struct mlx_enquiry2 * enq)186b6a7bef2SMike Smith mlx_enquiry(int unit, struct mlx_enquiry2 *enq)
187b6a7bef2SMike Smith {
188b6a7bef2SMike Smith     struct mlx_usercommand	cmd;
189b6a7bef2SMike Smith 
190b6a7bef2SMike Smith     /* build the command */
191b6a7bef2SMike Smith     cmd.mu_datasize = sizeof(*enq);
192b6a7bef2SMike Smith     cmd.mu_buf = enq;
193b6a7bef2SMike Smith     cmd.mu_bufptr = 8;
194b6a7bef2SMike Smith     cmd.mu_command[0] = MLX_CMD_ENQUIRY2;
195b6a7bef2SMike Smith 
196b6a7bef2SMike Smith     /* hand it off for processing */
197b6a7bef2SMike Smith     mlx_perform(unit, mlx_command, (void *)&cmd);
198b6a7bef2SMike Smith 
199b6a7bef2SMike Smith     return(cmd.mu_status != 0);
200b6a7bef2SMike Smith }
201b6a7bef2SMike Smith 
202b6a7bef2SMike Smith 
203b6a7bef2SMike Smith /********************************************************************************
204b6a7bef2SMike Smith  * Perform a READ CONFIGURATION command and return information related to the controller
205b6a7bef2SMike Smith  * (unit)
206b6a7bef2SMike Smith  */
207b6a7bef2SMike Smith int
mlx_read_configuration(int unit,struct mlx_core_cfg * cfg)208b6a7bef2SMike Smith mlx_read_configuration(int unit, struct mlx_core_cfg *cfg)
209b6a7bef2SMike Smith {
210b6a7bef2SMike Smith     struct mlx_usercommand	cmd;
211b6a7bef2SMike Smith 
212b6a7bef2SMike Smith     /* build the command */
213b6a7bef2SMike Smith     cmd.mu_datasize = sizeof(*cfg);
214b6a7bef2SMike Smith     cmd.mu_buf = cfg;
215b6a7bef2SMike Smith     cmd.mu_bufptr = 8;
216b6a7bef2SMike Smith     cmd.mu_command[0] = MLX_CMD_READ_CONFIG;
217b6a7bef2SMike Smith 
218b6a7bef2SMike Smith     /* hand it off for processing */
219b6a7bef2SMike Smith     mlx_perform(unit, mlx_command, (void *)&cmd);
220b6a7bef2SMike Smith 
221b6a7bef2SMike Smith     return(cmd.mu_status != 0);
222b6a7bef2SMike Smith }
223b6a7bef2SMike Smith 
224b6a7bef2SMike Smith /********************************************************************************
225b6a7bef2SMike Smith  * Perform a SCSI INQUIRY command and return pointers to the relevant data.
226b6a7bef2SMike Smith  */
227b6a7bef2SMike Smith int
mlx_scsi_inquiry(int unit,int channel,int target,char ** vendor,char ** device,char ** revision)228b6a7bef2SMike Smith mlx_scsi_inquiry(int unit, int channel, int target, char **vendor, char **device, char **revision)
229b6a7bef2SMike Smith {
230b6a7bef2SMike Smith     struct mlx_usercommand	cmd;
231b6a7bef2SMike Smith     static struct {
232b6a7bef2SMike Smith 	    struct mlx_dcdb		dcdb;
233b6a7bef2SMike Smith 	    union {
234b6a7bef2SMike Smith 		struct scsi_inquiry_data	inq;
235b6a7bef2SMike Smith 		u_int8_t			pad[SHORT_INQUIRY_LENGTH];
236b6a7bef2SMike Smith 	    } d;
237b6a7bef2SMike Smith     } __attribute__ ((packed))		dcdb_cmd;
238b6a7bef2SMike Smith     struct scsi_inquiry		*inq_cmd = (struct scsi_inquiry *)&dcdb_cmd.dcdb.dcdb_cdb[0];
239b6a7bef2SMike Smith 
240b6a7bef2SMike Smith     /* build the command */
241b6a7bef2SMike Smith     cmd.mu_datasize = sizeof(dcdb_cmd);
242b6a7bef2SMike Smith     cmd.mu_buf = &dcdb_cmd;
243b6a7bef2SMike Smith     cmd.mu_command[0] = MLX_CMD_DIRECT_CDB;
244b6a7bef2SMike Smith 
245b6a7bef2SMike Smith     /* build the DCDB */
246b6a7bef2SMike Smith     bzero(&dcdb_cmd, sizeof(dcdb_cmd));
247b6a7bef2SMike Smith     dcdb_cmd.dcdb.dcdb_channel = channel;
248b6a7bef2SMike Smith     dcdb_cmd.dcdb.dcdb_target = target;
249b6a7bef2SMike Smith     dcdb_cmd.dcdb.dcdb_flags = MLX_DCDB_DATA_IN | MLX_DCDB_TIMEOUT_10S;
250b6a7bef2SMike Smith     dcdb_cmd.dcdb.dcdb_datasize = SHORT_INQUIRY_LENGTH;
251b6a7bef2SMike Smith     dcdb_cmd.dcdb.dcdb_cdb_length = 6;
252b6a7bef2SMike Smith     dcdb_cmd.dcdb.dcdb_sense_length = SSD_FULL_SIZE;
253b6a7bef2SMike Smith 
254b6a7bef2SMike Smith     /* build the cdb */
255b6a7bef2SMike Smith     inq_cmd->opcode = INQUIRY;
256130f4520SKenneth D. Merry     scsi_ulto2b(SHORT_INQUIRY_LENGTH, inq_cmd->length);
257b6a7bef2SMike Smith 
258b6a7bef2SMike Smith     /* hand it off for processing */
259b6a7bef2SMike Smith     mlx_perform(unit, mlx_command, &cmd);
260b6a7bef2SMike Smith 
261b6a7bef2SMike Smith     if (cmd.mu_status == 0) {
262b6a7bef2SMike Smith 	*vendor = &dcdb_cmd.d.inq.vendor[0];
263b6a7bef2SMike Smith 	*device = &dcdb_cmd.d.inq.product[0];
264b6a7bef2SMike Smith 	*revision = &dcdb_cmd.d.inq.revision[0];
265b6a7bef2SMike Smith     }
266b6a7bef2SMike Smith     return(cmd.mu_status);
267b6a7bef2SMike Smith }
268b6a7bef2SMike Smith 
269b6a7bef2SMike Smith /********************************************************************************
270b6a7bef2SMike Smith  * Perform a GET DEVICE STATE command and return pointers to the relevant data.
271b6a7bef2SMike Smith  */
272b6a7bef2SMike Smith int
mlx_get_device_state(int unit,int channel,int target,struct mlx_phys_drv * drv)273b6a7bef2SMike Smith mlx_get_device_state(int unit, int channel, int target, struct mlx_phys_drv *drv)
274b6a7bef2SMike Smith {
275b6a7bef2SMike Smith     struct mlx_usercommand	cmd;
276b6a7bef2SMike Smith 
277b6a7bef2SMike Smith     /* build the command */
278b6a7bef2SMike Smith     cmd.mu_datasize = sizeof(*drv);
279b6a7bef2SMike Smith     cmd.mu_buf = drv;
280b6a7bef2SMike Smith     cmd.mu_bufptr = 8;
281b6a7bef2SMike Smith     cmd.mu_command[0] = MLX_CMD_DEVICE_STATE;
282b6a7bef2SMike Smith     cmd.mu_command[2] = channel;
283b6a7bef2SMike Smith     cmd.mu_command[3] = target;
284b6a7bef2SMike Smith 
285b6a7bef2SMike Smith     /* hand it off for processing */
286b6a7bef2SMike Smith     mlx_perform(unit, mlx_command, (void *)&cmd);
287b6a7bef2SMike Smith 
288b6a7bef2SMike Smith     return(cmd.mu_status != 0);
289b6a7bef2SMike Smith }
290