util.c revision 1.7 1 1.7 lukem /* $NetBSD: util.c,v 1.7 2009/04/17 04:03:39 lukem Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.1 ad * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 1.1 ad * All rights reserved.
6 1.1 ad *
7 1.1 ad * This code is derived from software contributed to The NetBSD Foundation
8 1.1 ad * by Andrew Doran.
9 1.1 ad *
10 1.1 ad * Redistribution and use in source and binary forms, with or without
11 1.1 ad * modification, are permitted provided that the following conditions
12 1.1 ad * are met:
13 1.1 ad * 1. Redistributions of source code must retain the above copyright
14 1.1 ad * notice, this list of conditions and the following disclaimer.
15 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 ad * notice, this list of conditions and the following disclaimer in the
17 1.1 ad * documentation and/or other materials provided with the distribution.
18 1.1 ad *
19 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 ad * POSSIBILITY OF SUCH DAMAGE.
30 1.1 ad */
31 1.1 ad
32 1.1 ad /*-
33 1.1 ad * Copyright (c) 1999 Michael Smith
34 1.1 ad * All rights reserved.
35 1.1 ad *
36 1.1 ad * Redistribution and use in source and binary forms, with or without
37 1.1 ad * modification, are permitted provided that the following conditions
38 1.1 ad * are met:
39 1.1 ad * 1. Redistributions of source code must retain the above copyright
40 1.1 ad * notice, this list of conditions and the following disclaimer.
41 1.1 ad * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 ad * notice, this list of conditions and the following disclaimer in the
43 1.1 ad * documentation and/or other materials provided with the distribution.
44 1.1 ad *
45 1.1 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
46 1.1 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 1.1 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 1.1 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
49 1.1 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 1.1 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 1.1 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 1.1 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 1.1 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 1.1 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 1.1 ad * SUCH DAMAGE.
56 1.1 ad */
57 1.1 ad
58 1.1 ad #ifndef lint
59 1.1 ad #include <sys/cdefs.h>
60 1.7 lukem __RCSID("$NetBSD: util.c,v 1.7 2009/04/17 04:03:39 lukem Exp $");
61 1.1 ad #endif /* not lint */
62 1.1 ad
63 1.1 ad #include <sys/types.h>
64 1.1 ad #include <sys/ioctl.h>
65 1.1 ad #include <sys/queue.h>
66 1.1 ad
67 1.1 ad #include <dev/ic/mlxreg.h>
68 1.1 ad #include <dev/ic/mlxio.h>
69 1.1 ad
70 1.1 ad #include <dev/scsipi/scsipi_all.h>
71 1.1 ad
72 1.1 ad #include <stdio.h>
73 1.1 ad #include <stdlib.h>
74 1.2 enami #include <string.h>
75 1.1 ad #include <unistd.h>
76 1.1 ad #include <err.h>
77 1.1 ad
78 1.1 ad #include "extern.h"
79 1.1 ad
80 1.1 ad int
81 1.1 ad mlx_command(struct mlx_usercommand *mu, int bomb)
82 1.1 ad {
83 1.1 ad int rv;
84 1.1 ad
85 1.1 ad if ((rv = ioctl(mlxfd, MLX_COMMAND, mu)) != 0 && bomb)
86 1.1 ad err(EXIT_FAILURE, "cmd 0x%02x failed",
87 1.1 ad mu->mu_command[0]);
88 1.1 ad
89 1.1 ad return (rv);
90 1.1 ad }
91 1.1 ad
92 1.1 ad void
93 1.1 ad mlx_enquiry(struct mlx_enquiry2 *enq)
94 1.1 ad {
95 1.1 ad struct mlx_usercommand mu;
96 1.1 ad struct mlx_enquiry_old meo;
97 1.1 ad
98 1.1 ad memset(&mu, 0, sizeof(mu));
99 1.1 ad
100 1.1 ad mu.mu_datasize = sizeof(*enq);
101 1.1 ad mu.mu_buf = enq;
102 1.1 ad mu.mu_bufptr = 8;
103 1.1 ad mu.mu_bufdir = MU_XFER_IN;
104 1.1 ad mu.mu_command[0] = MLX_CMD_ENQUIRY2;
105 1.1 ad
106 1.1 ad mlx_command(&mu, 1);
107 1.1 ad
108 1.1 ad /*
109 1.1 ad * If we get back a firmware major of 0, this is (probably) an old
110 1.1 ad * controller, so we need to pull the firmware version from the old
111 1.1 ad * enquiry structure.
112 1.1 ad */
113 1.1 ad if (enq->me_firmware_id[0] == 0) {
114 1.1 ad memset(&mu, 0, sizeof(mu));
115 1.1 ad
116 1.1 ad mu.mu_datasize = sizeof(meo);
117 1.1 ad mu.mu_buf = &meo;
118 1.1 ad mu.mu_bufptr = 8;
119 1.1 ad mu.mu_bufdir = MU_XFER_IN;
120 1.1 ad mu.mu_command[0] = MLX_CMD_ENQUIRY_OLD;
121 1.1 ad
122 1.1 ad mlx_command(&mu, 1);
123 1.1 ad
124 1.1 ad enq->me_firmware_id[0] = meo.me_fwmajor;
125 1.1 ad enq->me_firmware_id[1] = meo.me_fwminor;
126 1.4 ad enq->me_firmware_id[2] = 0;
127 1.4 ad enq->me_firmware_id[3] = '0';
128 1.1 ad }
129 1.1 ad }
130 1.1 ad
131 1.1 ad void
132 1.1 ad mlx_configuration(struct mlx_core_cfg *cfg, int wr)
133 1.1 ad {
134 1.1 ad struct mlx_usercommand mu;
135 1.1 ad
136 1.1 ad memset(&mu, 0, sizeof(mu));
137 1.1 ad
138 1.1 ad mu.mu_datasize = sizeof(*cfg);
139 1.1 ad mu.mu_buf = cfg;
140 1.1 ad mu.mu_bufptr = 8;
141 1.1 ad mu.mu_bufdir = (wr ? MU_XFER_OUT : MU_XFER_IN);
142 1.1 ad mu.mu_command[0] = (wr ? MLX_CMD_WRITE_CONFIG : MLX_CMD_READ_CONFIG);
143 1.1 ad
144 1.1 ad mlx_command(&mu, 1);
145 1.1 ad }
146 1.1 ad
147 1.1 ad int
148 1.1 ad mlx_get_device_state(int chan, int targ, struct mlx_phys_drv *pd)
149 1.1 ad {
150 1.1 ad struct mlx_usercommand mu;
151 1.1 ad
152 1.1 ad memset(&mu, 0, sizeof(mu));
153 1.1 ad
154 1.1 ad mu.mu_datasize = sizeof(*pd);
155 1.1 ad mu.mu_buf = pd;
156 1.1 ad mu.mu_bufptr = 8;
157 1.1 ad mu.mu_bufdir = MU_XFER_IN;
158 1.1 ad mu.mu_command[0] = MLX_CMD_DEVICE_STATE;
159 1.1 ad mu.mu_command[2] = chan;
160 1.1 ad mu.mu_command[3] = targ;
161 1.1 ad
162 1.1 ad return (mlx_command(&mu, 0));
163 1.1 ad }
164 1.1 ad
165 1.1 ad int
166 1.1 ad mlx_scsi_inquiry(int chan, int targ, char **vendor, char **device,
167 1.1 ad char **revision)
168 1.1 ad {
169 1.1 ad struct mlx_usercommand mu;
170 1.1 ad static struct {
171 1.1 ad struct mlx_dcdb dcdb;
172 1.1 ad struct scsipi_inquiry_data inq;
173 1.5 perry } __packed dcdb_cmd;
174 1.1 ad struct scsipi_inquiry *inq_cmd;
175 1.1 ad int rv;
176 1.1 ad
177 1.1 ad inq_cmd = (struct scsipi_inquiry *)&dcdb_cmd.dcdb.dcdb_cdb[0];
178 1.1 ad
179 1.1 ad memset(&mu, 0, sizeof(mu));
180 1.1 ad mu.mu_datasize = sizeof(dcdb_cmd);
181 1.1 ad mu.mu_buf = &dcdb_cmd;
182 1.1 ad mu.mu_command[0] = MLX_CMD_DIRECT_CDB;
183 1.1 ad mu.mu_bufdir = MU_XFER_IN | MU_XFER_OUT;
184 1.1 ad
185 1.1 ad memset(&dcdb_cmd, 0, sizeof(dcdb_cmd));
186 1.1 ad dcdb_cmd.dcdb.dcdb_target = (chan << 4) | targ;
187 1.1 ad dcdb_cmd.dcdb.dcdb_flags = MLX_DCDB_DATA_IN | MLX_DCDB_TIMEOUT_10S;
188 1.1 ad dcdb_cmd.dcdb.dcdb_datasize = sizeof(dcdb_cmd.inq);
189 1.1 ad dcdb_cmd.dcdb.dcdb_length = 6;
190 1.1 ad dcdb_cmd.dcdb.dcdb_sense_length = 40;
191 1.1 ad
192 1.1 ad inq_cmd->opcode = INQUIRY;
193 1.1 ad inq_cmd->length = sizeof(dcdb_cmd.inq);
194 1.1 ad
195 1.1 ad if ((rv = mlx_command(&mu, 0)) == 0) {
196 1.1 ad *vendor = &dcdb_cmd.inq.vendor[0];
197 1.1 ad *device = &dcdb_cmd.inq.product[0];
198 1.1 ad *revision = &dcdb_cmd.inq.revision[0];
199 1.1 ad }
200 1.1 ad
201 1.1 ad return (rv);
202 1.1 ad }
203 1.1 ad
204 1.1 ad void
205 1.1 ad mlx_print_phys_drv(struct mlx_phys_drv *pd, int chn, int targ,
206 1.1 ad const char *prefix)
207 1.1 ad {
208 1.7 lukem const char *type;
209 1.7 lukem char *device, *vendor, *revision;
210 1.1 ad
211 1.3 ad switch (pd->pd_flags2 & 0x03) {
212 1.1 ad case MLX_PHYS_DRV_DISK:
213 1.1 ad type = "disk";
214 1.1 ad break;
215 1.1 ad
216 1.1 ad case MLX_PHYS_DRV_SEQUENTIAL:
217 1.1 ad type = "tape";
218 1.1 ad break;
219 1.1 ad
220 1.1 ad case MLX_PHYS_DRV_CDROM:
221 1.1 ad type= "cdrom";
222 1.1 ad break;
223 1.1 ad
224 1.1 ad case MLX_PHYS_DRV_OTHER:
225 1.1 ad default:
226 1.1 ad type = "unknown";
227 1.1 ad break;
228 1.1 ad }
229 1.1 ad
230 1.1 ad printf("%s%s%02d%02d ", prefix, type, chn, targ);
231 1.1 ad
232 1.3 ad switch (pd->pd_status) {
233 1.1 ad case MLX_PHYS_DRV_DEAD:
234 1.1 ad printf(" (dead) ");
235 1.1 ad break;
236 1.1 ad
237 1.1 ad case MLX_PHYS_DRV_WRONLY:
238 1.1 ad printf(" (write-only) ");
239 1.1 ad break;
240 1.1 ad
241 1.1 ad case MLX_PHYS_DRV_ONLINE:
242 1.1 ad printf(" (online) ");
243 1.1 ad break;
244 1.1 ad
245 1.1 ad case MLX_PHYS_DRV_STANDBY:
246 1.1 ad printf(" (standby) ");
247 1.1 ad break;
248 1.1 ad
249 1.1 ad default:
250 1.1 ad printf(" (0x%02x) ", pd->pd_status);
251 1.1 ad break;
252 1.1 ad }
253 1.1 ad
254 1.1 ad printf("\n");
255 1.1 ad if (verbosity == 0)
256 1.1 ad return;
257 1.1 ad
258 1.1 ad printf("%s ", prefix);
259 1.1 ad if (!mlx_scsi_inquiry(chn, targ, &vendor, &device, &revision))
260 1.1 ad printf("'%8.8s' '%16.16s' '%4.4s'", vendor, device, revision);
261 1.1 ad else
262 1.1 ad printf("<IDENTIFY FAILED>");
263 1.1 ad
264 1.1 ad printf(" %dMB ", pd->pd_config_size / 2048);
265 1.1 ad
266 1.1 ad if ((pd->pd_flags2 & MLX_PHYS_DRV_FAST20) != 0)
267 1.1 ad printf(" ultra");
268 1.1 ad else if ((pd->pd_flags2 & MLX_PHYS_DRV_FAST) != 0)
269 1.1 ad printf(" fast");
270 1.1 ad
271 1.1 ad if ((pd->pd_flags2 & MLX_PHYS_DRV_WIDE) != 0)
272 1.1 ad printf(" wide");
273 1.1 ad
274 1.1 ad if ((pd->pd_flags2 & MLX_PHYS_DRV_SYNC) != 0)
275 1.1 ad printf(" sync");
276 1.1 ad
277 1.1 ad if ((pd->pd_flags2 & MLX_PHYS_DRV_TAG) != 0)
278 1.1 ad printf(" tag-enabled");
279 1.1 ad
280 1.1 ad printf("\n");
281 1.1 ad }
282