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