dpti.c revision 1.46 1 1.46 dholland /* $NetBSD: dpti.c,v 1.46 2014/03/16 05:20:27 dholland Exp $ */
2 1.1 ad
3 1.1 ad /*-
4 1.32 ad * Copyright (c) 2001, 2007 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) 1996-2000 Distributed Processing Technology Corporation
34 1.1 ad * Copyright (c) 2000 Adaptec Corporation
35 1.1 ad * All rights reserved.
36 1.1 ad *
37 1.1 ad * TERMS AND CONDITIONS OF USE
38 1.1 ad *
39 1.1 ad * Redistribution and use in source form, with or without modification, are
40 1.1 ad * permitted provided that redistributions of source code must retain the
41 1.1 ad * above copyright notice, this list of conditions and the following disclaimer.
42 1.1 ad *
43 1.1 ad * This software is provided `as is' by Adaptec and any express or implied
44 1.1 ad * warranties, including, but not limited to, the implied warranties of
45 1.1 ad * merchantability and fitness for a particular purpose, are disclaimed. In no
46 1.1 ad * event shall Adaptec be liable for any direct, indirect, incidental, special,
47 1.1 ad * exemplary or consequential damages (including, but not limited to,
48 1.1 ad * procurement of substitute goods or services; loss of use, data, or profits;
49 1.1 ad * or business interruptions) however caused and on any theory of liability,
50 1.1 ad * whether in contract, strict liability, or tort (including negligence or
51 1.1 ad * otherwise) arising in any way out of the use of this driver software, even
52 1.1 ad * if advised of the possibility of such damage.
53 1.1 ad */
54 1.1 ad
55 1.1 ad /*
56 1.1 ad * Adaptec/DPT I2O control interface.
57 1.1 ad */
58 1.3 lukem
59 1.3 lukem #include <sys/cdefs.h>
60 1.46 dholland __KERNEL_RCSID(0, "$NetBSD: dpti.c,v 1.46 2014/03/16 05:20:27 dholland Exp $");
61 1.1 ad
62 1.1 ad #include <sys/param.h>
63 1.1 ad #include <sys/systm.h>
64 1.1 ad #include <sys/kernel.h>
65 1.1 ad #include <sys/device.h>
66 1.1 ad #include <sys/queue.h>
67 1.1 ad #include <sys/proc.h>
68 1.1 ad #include <sys/endian.h>
69 1.1 ad #include <sys/malloc.h>
70 1.1 ad #include <sys/conf.h>
71 1.1 ad #include <sys/ioctl.h>
72 1.29 elad #include <sys/kauth.h>
73 1.1 ad
74 1.35 ad #include <sys/bus.h>
75 1.11 thorpej #ifdef __i386__
76 1.2 ad #include <machine/pio.h>
77 1.38 ad #include <machine/cputypes.h>
78 1.2 ad #endif
79 1.1 ad
80 1.1 ad #include <dev/i2o/i2o.h>
81 1.1 ad #include <dev/i2o/i2odpt.h>
82 1.1 ad #include <dev/i2o/iopio.h>
83 1.1 ad #include <dev/i2o/iopvar.h>
84 1.1 ad #include <dev/i2o/dptivar.h>
85 1.1 ad
86 1.1 ad #ifdef I2ODEBUG
87 1.1 ad #define DPRINTF(x) printf x
88 1.1 ad #else
89 1.1 ad #define DPRINTF(x)
90 1.1 ad #endif
91 1.1 ad
92 1.1 ad static struct dpt_sig dpti_sig = {
93 1.2 ad { 'd', 'P', 't', 'S', 'i', 'G'},
94 1.2 ad SIG_VERSION,
95 1.11 thorpej #if defined(__i386__)
96 1.2 ad PROC_INTEL,
97 1.11 thorpej #elif defined(__powerpc__)
98 1.2 ad PROC_POWERPC,
99 1.11 thorpej #elif defined(__alpha__)
100 1.2 ad PROC_ALPHA,
101 1.4 thorpej #elif defined(__mips__)
102 1.2 ad PROC_MIPS,
103 1.11 thorpej #elif defined(__sparc64__)
104 1.2 ad PROC_ULTRASPARC,
105 1.2 ad #endif
106 1.11 thorpej #if defined(__i386__)
107 1.2 ad PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
108 1.2 ad #else
109 1.2 ad 0,
110 1.2 ad #endif
111 1.2 ad FT_HBADRVR,
112 1.2 ad 0,
113 1.2 ad OEM_DPT,
114 1.2 ad OS_FREE_BSD, /* XXX */
115 1.2 ad CAP_ABOVE16MB,
116 1.2 ad DEV_ALL,
117 1.1 ad ADF_ALL_SC5,
118 1.2 ad 0,
119 1.2 ad 0,
120 1.2 ad DPTI_VERSION,
121 1.2 ad DPTI_REVISION,
122 1.2 ad DPTI_SUBREVISION,
123 1.2 ad DPTI_MONTH,
124 1.2 ad DPTI_DAY,
125 1.2 ad DPTI_YEAR,
126 1.2 ad "" /* Will be filled later */
127 1.1 ad };
128 1.1 ad
129 1.42 cegger void dpti_attach(device_t, device_t, void *);
130 1.1 ad int dpti_blinkled(struct dpti_softc *);
131 1.33 christos int dpti_ctlrinfo(struct dpti_softc *, int, void *);
132 1.42 cegger int dpti_match(device_t, cfdata_t, void *);
133 1.33 christos int dpti_passthrough(struct dpti_softc *, void *, struct proc *);
134 1.33 christos int dpti_sysinfo(struct dpti_softc *, int, void *);
135 1.1 ad
136 1.5 gehenna dev_type_open(dptiopen);
137 1.5 gehenna dev_type_ioctl(dptiioctl);
138 1.5 gehenna
139 1.5 gehenna const struct cdevsw dpti_cdevsw = {
140 1.46 dholland .d_open = dptiopen,
141 1.46 dholland .d_close = nullclose,
142 1.46 dholland .d_read = noread,
143 1.46 dholland .d_write = nowrite,
144 1.46 dholland .d_ioctl = dptiioctl,
145 1.46 dholland .d_stop = nostop,
146 1.46 dholland .d_tty = notty,
147 1.46 dholland .d_poll = nopoll,
148 1.46 dholland .d_mmap = nommap,
149 1.46 dholland .d_kqfilter = nokqfilter,
150 1.46 dholland .d_flag = D_OTHER,
151 1.5 gehenna };
152 1.1 ad
153 1.1 ad extern struct cfdriver dpti_cd;
154 1.1 ad
155 1.45 chs CFATTACH_DECL_NEW(dpti, sizeof(struct dpti_softc),
156 1.8 thorpej dpti_match, dpti_attach, NULL, NULL);
157 1.1 ad
158 1.1 ad int
159 1.42 cegger dpti_match(device_t parent, cfdata_t match, void *aux)
160 1.1 ad {
161 1.1 ad struct iop_attach_args *ia;
162 1.1 ad struct iop_softc *iop;
163 1.1 ad
164 1.1 ad ia = aux;
165 1.45 chs iop = device_private(parent);
166 1.1 ad
167 1.1 ad if (ia->ia_class != I2O_CLASS_ANY || ia->ia_tid != I2O_TID_IOP)
168 1.1 ad return (0);
169 1.1 ad
170 1.1 ad if (le16toh(iop->sc_status.orgid) != I2O_ORG_DPT)
171 1.1 ad return (0);
172 1.1 ad
173 1.1 ad return (1);
174 1.1 ad }
175 1.1 ad
176 1.1 ad void
177 1.42 cegger dpti_attach(device_t parent, device_t self, void *aux)
178 1.1 ad {
179 1.1 ad struct iop_softc *iop;
180 1.1 ad struct dpti_softc *sc;
181 1.1 ad struct {
182 1.1 ad struct i2o_param_op_results pr;
183 1.1 ad struct i2o_param_read_results prr;
184 1.1 ad struct i2o_dpt_param_exec_iop_buffers dib;
185 1.40 gmcgarry } __packed param;
186 1.1 ad int rv;
187 1.1 ad
188 1.24 thorpej sc = device_private(self);
189 1.45 chs sc->sc_dev = self;
190 1.24 thorpej iop = device_private(parent);
191 1.1 ad
192 1.1 ad /*
193 1.1 ad * Tell the world what we are. The description in the signature
194 1.1 ad * must be no more than 46 bytes long (see dptivar.h).
195 1.1 ad */
196 1.2 ad printf(": DPT/Adaptec RAID management interface\n");
197 1.18 itojun snprintf(dpti_sig.dsDescription, sizeof(dpti_sig.dsDescription),
198 1.18 itojun "NetBSD %s I2O OSM", osrelease);
199 1.1 ad
200 1.1 ad rv = iop_field_get_all(iop, I2O_TID_IOP,
201 1.1 ad I2O_DPT_PARAM_EXEC_IOP_BUFFERS, ¶m,
202 1.1 ad sizeof(param), NULL);
203 1.1 ad if (rv != 0)
204 1.1 ad return;
205 1.1 ad
206 1.1 ad sc->sc_blinkled = le32toh(param.dib.serialoutputoff) + 8;
207 1.1 ad }
208 1.1 ad
209 1.1 ad int
210 1.30 christos dptiopen(dev_t dev, int flag, int mode,
211 1.30 christos struct lwp *l)
212 1.1 ad {
213 1.1 ad
214 1.1 ad if (device_lookup(&dpti_cd, minor(dev)) == NULL)
215 1.1 ad return (ENXIO);
216 1.1 ad
217 1.1 ad return (0);
218 1.1 ad }
219 1.1 ad
220 1.1 ad int
221 1.33 christos dptiioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
222 1.1 ad {
223 1.1 ad struct iop_softc *iop;
224 1.1 ad struct dpti_softc *sc;
225 1.1 ad struct ioctl_pt *pt;
226 1.10 ad int i, size, rv, linux;
227 1.1 ad
228 1.39 tsutsui sc = device_lookup_private(&dpti_cd, minor(dev));
229 1.45 chs iop = device_private(device_parent(sc->sc_dev));
230 1.17 ad rv = 0;
231 1.1 ad
232 1.1 ad if (cmd == PTIOCLINUX) {
233 1.1 ad pt = (struct ioctl_pt *)data;
234 1.10 ad size = IOCPARM_LEN(pt->com);
235 1.10 ad cmd = pt->com & 0xffff;
236 1.1 ad data = pt->data;
237 1.10 ad linux = 1;
238 1.10 ad } else {
239 1.10 ad size = IOCPARM_LEN(cmd);
240 1.10 ad cmd = cmd & 0xffff;
241 1.10 ad linux = 0;
242 1.10 ad }
243 1.1 ad
244 1.10 ad switch (cmd) {
245 1.1 ad case DPT_SIGNATURE:
246 1.1 ad if (size > sizeof(dpti_sig))
247 1.1 ad size = sizeof(dpti_sig);
248 1.1 ad memcpy(data, &dpti_sig, size);
249 1.17 ad break;
250 1.1 ad
251 1.1 ad case DPT_CTRLINFO:
252 1.17 ad rv = dpti_ctlrinfo(sc, size, data);
253 1.17 ad break;
254 1.1 ad
255 1.1 ad case DPT_SYSINFO:
256 1.17 ad rv = dpti_sysinfo(sc, size, data);
257 1.17 ad break;
258 1.1 ad
259 1.1 ad case DPT_BLINKLED:
260 1.1 ad if ((i = dpti_blinkled(sc)) == -1)
261 1.1 ad i = 0;
262 1.1 ad
263 1.17 ad if (size == 0) {
264 1.33 christos rv = copyout(&i, *(void **)data, sizeof(i));
265 1.17 ad break;
266 1.17 ad }
267 1.1 ad
268 1.1 ad *(int *)data = i;
269 1.17 ad break;
270 1.1 ad
271 1.1 ad case DPT_TARGET_BUSY:
272 1.1 ad /*
273 1.1 ad * XXX This is here to stop linux_machdepioctl() from
274 1.10 ad * whining about an unknown ioctl.
275 1.1 ad */
276 1.17 ad rv = EIO;
277 1.17 ad break;
278 1.1 ad
279 1.1 ad case DPT_I2OUSRCMD:
280 1.31 elad rv = kauth_authorize_device_passthru(l->l_cred, dev,
281 1.31 elad KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_ALL, data);
282 1.29 elad if (rv)
283 1.25 christos break;
284 1.25 christos
285 1.44 rmind mutex_enter(&iop->sc_conflock);
286 1.44 rmind if (linux) {
287 1.21 christos rv = dpti_passthrough(sc, data, l->l_proc);
288 1.44 rmind } else {
289 1.33 christos rv = dpti_passthrough(sc, *(void **)data, l->l_proc);
290 1.44 rmind }
291 1.44 rmind mutex_exit(&iop->sc_conflock);
292 1.17 ad break;
293 1.1 ad
294 1.1 ad case DPT_I2ORESETCMD:
295 1.1 ad printf("%s: I2ORESETCMD not implemented\n",
296 1.45 chs device_xname(sc->sc_dev));
297 1.17 ad rv = EOPNOTSUPP;
298 1.17 ad break;
299 1.1 ad
300 1.1 ad case DPT_I2ORESCANCMD:
301 1.32 ad mutex_enter(&iop->sc_conflock);
302 1.17 ad rv = iop_reconfigure(iop, 0);
303 1.32 ad mutex_exit(&iop->sc_conflock);
304 1.17 ad break;
305 1.1 ad
306 1.1 ad default:
307 1.17 ad rv = ENOTTY;
308 1.17 ad break;
309 1.1 ad }
310 1.17 ad
311 1.17 ad return (rv);
312 1.1 ad }
313 1.1 ad
314 1.1 ad int
315 1.1 ad dpti_blinkled(struct dpti_softc *sc)
316 1.1 ad {
317 1.1 ad struct iop_softc *iop;
318 1.1 ad u_int v;
319 1.1 ad
320 1.45 chs iop = device_private(device_parent(sc->sc_dev));
321 1.1 ad
322 1.1 ad v = bus_space_read_1(iop->sc_iot, iop->sc_ioh, sc->sc_blinkled + 0);
323 1.1 ad if (v == 0xbc) {
324 1.1 ad v = bus_space_read_1(iop->sc_iot, iop->sc_ioh,
325 1.1 ad sc->sc_blinkled + 1);
326 1.1 ad return (v);
327 1.1 ad }
328 1.1 ad
329 1.1 ad return (-1);
330 1.1 ad }
331 1.1 ad
332 1.1 ad int
333 1.33 christos dpti_ctlrinfo(struct dpti_softc *sc, int size, void *data)
334 1.1 ad {
335 1.2 ad struct dpt_ctlrinfo info;
336 1.1 ad struct iop_softc *iop;
337 1.1 ad int rv, i;
338 1.1 ad
339 1.45 chs iop = device_private(device_parent(sc->sc_dev));
340 1.1 ad
341 1.2 ad memset(&info, 0, sizeof(info));
342 1.1 ad
343 1.2 ad info.length = sizeof(info) - sizeof(u_int16_t);
344 1.45 chs info.drvrHBAnum = device_unit(sc->sc_dev);
345 1.2 ad info.baseAddr = iop->sc_memaddr;
346 1.1 ad if ((i = dpti_blinkled(sc)) == -1)
347 1.1 ad i = 0;
348 1.2 ad info.blinkState = i;
349 1.2 ad info.pciBusNum = iop->sc_pcibus;
350 1.2 ad info.pciDeviceNum = iop->sc_pcidev;
351 1.2 ad info.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
352 1.2 ad info.Interrupt = 10; /* XXX */
353 1.1 ad
354 1.33 christos if (size > sizeof(char)) {
355 1.10 ad memcpy(data, &info, min(sizeof(info), size));
356 1.1 ad rv = 0;
357 1.1 ad } else
358 1.33 christos rv = copyout(&info, *(void **)data, sizeof(info));
359 1.1 ad
360 1.1 ad return (rv);
361 1.1 ad }
362 1.1 ad
363 1.1 ad int
364 1.33 christos dpti_sysinfo(struct dpti_softc *sc, int size, void *data)
365 1.1 ad {
366 1.2 ad struct dpt_sysinfo info;
367 1.2 ad int rv;
368 1.11 thorpej #ifdef __i386__
369 1.2 ad int i, j;
370 1.2 ad #endif
371 1.2 ad
372 1.2 ad memset(&info, 0, sizeof(info));
373 1.2 ad
374 1.11 thorpej #ifdef __i386__
375 1.2 ad outb (0x70, 0x12);
376 1.2 ad i = inb(0x71);
377 1.2 ad j = i >> 4;
378 1.2 ad if (i == 0x0f) {
379 1.2 ad outb (0x70, 0x19);
380 1.2 ad j = inb (0x71);
381 1.2 ad }
382 1.2 ad info.drive0CMOS = j;
383 1.2 ad
384 1.2 ad j = i & 0x0f;
385 1.2 ad if (i == 0x0f) {
386 1.2 ad outb (0x70, 0x1a);
387 1.2 ad j = inb (0x71);
388 1.2 ad }
389 1.2 ad info.drive1CMOS = j;
390 1.2 ad info.processorFamily = dpti_sig.dsProcessorFamily;
391 1.2 ad
392 1.2 ad /*
393 1.2 ad * Get the conventional memory size from CMOS.
394 1.2 ad */
395 1.2 ad outb(0x70, 0x16);
396 1.2 ad j = inb(0x71);
397 1.2 ad j <<= 8;
398 1.2 ad outb(0x70, 0x15);
399 1.2 ad j |= inb(0x71);
400 1.2 ad info.conventionalMemSize = j;
401 1.2 ad
402 1.2 ad /*
403 1.2 ad * Get the extended memory size from CMOS.
404 1.2 ad */
405 1.2 ad outb(0x70, 0x31);
406 1.2 ad j = inb(0x71);
407 1.2 ad j <<= 8;
408 1.2 ad outb(0x70, 0x30);
409 1.2 ad j |= inb(0x71);
410 1.2 ad info.extendedMemSize = j;
411 1.2 ad
412 1.2 ad switch (cpu_class) {
413 1.2 ad case CPUCLASS_386:
414 1.2 ad info.processorType = PROC_386;
415 1.2 ad break;
416 1.2 ad case CPUCLASS_486:
417 1.2 ad info.processorType = PROC_486;
418 1.2 ad break;
419 1.2 ad case CPUCLASS_586:
420 1.2 ad info.processorType = PROC_PENTIUM;
421 1.2 ad break;
422 1.2 ad case CPUCLASS_686:
423 1.2 ad default:
424 1.2 ad info.processorType = PROC_SEXIUM;
425 1.2 ad break;
426 1.2 ad }
427 1.2 ad
428 1.2 ad info.flags = SI_CMOS_Valid | SI_BusTypeValid |
429 1.2 ad SI_MemorySizeValid | SI_NO_SmartROM;
430 1.2 ad #else
431 1.2 ad info.flags = SI_BusTypeValid | SI_NO_SmartROM;
432 1.2 ad #endif
433 1.2 ad
434 1.2 ad info.busType = SI_PCI_BUS;
435 1.1 ad
436 1.1 ad /*
437 1.2 ad * Copy out the info structure to the user.
438 1.1 ad */
439 1.33 christos if (size > sizeof(char)) {
440 1.10 ad memcpy(data, &info, min(sizeof(info), size));
441 1.2 ad rv = 0;
442 1.2 ad } else
443 1.33 christos rv = copyout(&info, *(void **)data, sizeof(info));
444 1.2 ad
445 1.2 ad return (rv);
446 1.1 ad }
447 1.1 ad
448 1.1 ad int
449 1.33 christos dpti_passthrough(struct dpti_softc *sc, void *data, struct proc *proc)
450 1.1 ad {
451 1.1 ad struct iop_softc *iop;
452 1.1 ad struct i2o_msg mh, *mf;
453 1.1 ad struct i2o_reply rh;
454 1.1 ad struct iop_msg *im;
455 1.1 ad struct dpti_ptbuf bufs[IOP_MAX_MSG_XFERS];
456 1.1 ad u_int32_t mbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
457 1.1 ad u_int32_t rbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
458 1.2 ad int rv, msgsize, repsize, sgoff, i, mapped, nbuf, nfrag, j, sz;
459 1.16 simonb u_int32_t *p, *pmax;
460 1.1 ad
461 1.45 chs iop = device_private(device_parent(sc->sc_dev));
462 1.2 ad im = NULL;
463 1.1 ad
464 1.1 ad if ((rv = dpti_blinkled(sc)) != -1) {
465 1.1 ad if (rv != 0) {
466 1.45 chs aprint_error_dev(sc->sc_dev, "adapter blinkled = 0x%02x\n", rv);
467 1.1 ad return (EIO);
468 1.1 ad }
469 1.1 ad }
470 1.1 ad
471 1.1 ad /*
472 1.1 ad * Copy in the message frame header and determine the size of the
473 1.1 ad * full message frame.
474 1.1 ad */
475 1.1 ad if ((rv = copyin(data, &mh, sizeof(mh))) != 0) {
476 1.1 ad DPRINTF(("%s: message copyin failed\n",
477 1.45 chs device_xname(sc->sc_dev)));
478 1.1 ad return (rv);
479 1.1 ad }
480 1.1 ad
481 1.1 ad msgsize = (mh.msgflags >> 14) & ~3;
482 1.1 ad if (msgsize < sizeof(mh) || msgsize >= IOP_MAX_MSG_SIZE) {
483 1.1 ad DPRINTF(("%s: bad message frame size\n",
484 1.45 chs device_xname(sc->sc_dev)));
485 1.1 ad return (EINVAL);
486 1.1 ad }
487 1.1 ad
488 1.1 ad /*
489 1.1 ad * Handle special commands.
490 1.1 ad */
491 1.1 ad switch (mh.msgfunc >> 24) {
492 1.1 ad case I2O_EXEC_IOP_RESET:
493 1.2 ad printf("%s: I2O_EXEC_IOP_RESET not implemented\n",
494 1.45 chs device_xname(sc->sc_dev));
495 1.2 ad return (EOPNOTSUPP);
496 1.1 ad
497 1.1 ad case I2O_EXEC_OUTBOUND_INIT:
498 1.2 ad printf("%s: I2O_EXEC_OUTBOUND_INIT not implemented\n",
499 1.45 chs device_xname(sc->sc_dev));
500 1.2 ad return (EOPNOTSUPP);
501 1.1 ad
502 1.1 ad case I2O_EXEC_SYS_TAB_SET:
503 1.2 ad printf("%s: I2O_EXEC_SYS_TAB_SET not implemented\n",
504 1.45 chs device_xname(sc->sc_dev));
505 1.2 ad return (EOPNOTSUPP);
506 1.1 ad
507 1.1 ad case I2O_EXEC_STATUS_GET:
508 1.2 ad if ((rv = iop_status_get(iop, 0)) == 0)
509 1.33 christos rv = copyout(&iop->sc_status, (char *)data + msgsize,
510 1.2 ad sizeof(iop->sc_status));
511 1.1 ad return (rv);
512 1.1 ad }
513 1.1 ad
514 1.1 ad /*
515 1.1 ad * Copy in the full message frame.
516 1.1 ad */
517 1.1 ad if ((rv = copyin(data, mbtmp, msgsize)) != 0) {
518 1.1 ad DPRINTF(("%s: full message copyin failed\n",
519 1.45 chs device_xname(sc->sc_dev)));
520 1.1 ad return (rv);
521 1.1 ad }
522 1.1 ad
523 1.1 ad /*
524 1.1 ad * Determine the size of the reply frame, and copy it in.
525 1.1 ad */
526 1.33 christos if ((rv = copyin((char *)data + msgsize, &rh, sizeof(rh))) != 0) {
527 1.1 ad DPRINTF(("%s: reply copyin failed\n",
528 1.45 chs device_xname(sc->sc_dev)));
529 1.1 ad return (rv);
530 1.1 ad }
531 1.1 ad
532 1.1 ad repsize = (rh.msgflags >> 14) & ~3;
533 1.1 ad if (repsize < sizeof(rh) || repsize >= IOP_MAX_MSG_SIZE) {
534 1.1 ad DPRINTF(("%s: bad reply header size\n",
535 1.45 chs device_xname(sc->sc_dev)));
536 1.1 ad return (EINVAL);
537 1.1 ad }
538 1.1 ad
539 1.33 christos if ((rv = copyin((char *)data + msgsize, rbtmp, repsize)) != 0) {
540 1.45 chs DPRINTF(("%s: reply too large\n", device_xname(sc->sc_dev)));
541 1.1 ad return (rv);
542 1.1 ad }
543 1.1 ad
544 1.1 ad /*
545 1.1 ad * If the message has a scatter gather list, it must be comprised of
546 1.1 ad * simple elements. If any one transfer contains multiple segments,
547 1.1 ad * we allocate a temporary buffer for it; otherwise, the buffer will
548 1.2 ad * be mapped directly.
549 1.1 ad */
550 1.20 fredb mapped = 0;
551 1.1 ad if ((sgoff = ((mh.msgflags >> 4) & 15)) != 0) {
552 1.1 ad if ((sgoff + 2) > (msgsize >> 2)) {
553 1.1 ad DPRINTF(("%s: invalid message size fields\n",
554 1.45 chs device_xname(sc->sc_dev)));
555 1.1 ad return (EINVAL);
556 1.1 ad }
557 1.2 ad
558 1.2 ad memset(bufs, 0, sizeof(bufs));
559 1.2 ad
560 1.1 ad p = mbtmp + sgoff;
561 1.2 ad pmax = mbtmp + (msgsize >> 2) - 2;
562 1.1 ad
563 1.2 ad for (nbuf = 0; nbuf < IOP_MAX_MSG_XFERS; nbuf++, p += 2) {
564 1.2 ad if (p > pmax) {
565 1.1 ad DPRINTF(("%s: invalid SGL (1)\n",
566 1.45 chs device_xname(sc->sc_dev)));
567 1.2 ad goto bad;
568 1.1 ad }
569 1.1 ad
570 1.1 ad if ((p[0] & 0x30000000) != I2O_SGL_SIMPLE) {
571 1.1 ad DPRINTF(("%s: invalid SGL (2)\n",
572 1.45 chs device_xname(sc->sc_dev)));
573 1.2 ad goto bad;
574 1.1 ad }
575 1.1 ad
576 1.1 ad bufs[nbuf].db_out = (p[0] & I2O_SGL_DATA_OUT) != 0;
577 1.2 ad bufs[nbuf].db_ptr = NULL;
578 1.1 ad
579 1.1 ad if ((p[0] & I2O_SGL_END_BUFFER) != 0) {
580 1.1 ad if ((p[0] & 0x00ffffff) > IOP_MAX_XFER) {
581 1.1 ad DPRINTF(("%s: buffer too large\n",
582 1.45 chs device_xname(sc->sc_dev)));
583 1.2 ad goto bad;
584 1.1 ad }
585 1.1 ad
586 1.33 christos bufs[nbuf].db_ptr = (void *)p[1];
587 1.1 ad bufs[nbuf].db_proc = proc;
588 1.1 ad bufs[nbuf].db_size = p[0] & 0x00ffffff;
589 1.1 ad
590 1.1 ad if ((p[0] & I2O_SGL_END) != 0)
591 1.1 ad break;
592 1.1 ad
593 1.1 ad continue;
594 1.1 ad }
595 1.1 ad
596 1.2 ad /*
597 1.2 ad * The buffer has multiple segments. Determine the
598 1.2 ad * total size.
599 1.2 ad */
600 1.2 ad nfrag = 0;
601 1.2 ad sz = 0;
602 1.16 simonb for (; p <= pmax; p += 2) {
603 1.2 ad if (nfrag == DPTI_MAX_SEGS) {
604 1.2 ad DPRINTF(("%s: too many segments\n",
605 1.45 chs device_xname(sc->sc_dev)));
606 1.2 ad goto bad;
607 1.2 ad }
608 1.2 ad
609 1.2 ad bufs[nbuf].db_frags[nfrag].iov_len =
610 1.2 ad p[0] & 0x00ffffff;
611 1.19 perry bufs[nbuf].db_frags[nfrag].iov_base =
612 1.2 ad (void *)p[1];
613 1.2 ad
614 1.2 ad sz += p[0] & 0x00ffffff;
615 1.2 ad nfrag++;
616 1.2 ad
617 1.2 ad if ((p[0] & I2O_SGL_END) != 0) {
618 1.2 ad if ((p[0] & I2O_SGL_END_BUFFER) == 0) {
619 1.2 ad DPRINTF((
620 1.2 ad "%s: invalid SGL (3)\n",
621 1.45 chs device_xname(sc->sc_dev)));
622 1.2 ad goto bad;
623 1.2 ad }
624 1.2 ad break;
625 1.2 ad }
626 1.2 ad if ((p[0] & I2O_SGL_END_BUFFER) != 0)
627 1.2 ad break;
628 1.2 ad }
629 1.2 ad bufs[nbuf].db_nfrag = nfrag;
630 1.2 ad
631 1.2 ad if (p > pmax) {
632 1.2 ad DPRINTF(("%s: invalid SGL (4)\n",
633 1.45 chs device_xname(sc->sc_dev)));
634 1.2 ad goto bad;
635 1.2 ad }
636 1.2 ad
637 1.2 ad if (sz > IOP_MAX_XFER) {
638 1.2 ad DPRINTF(("%s: buffer too large\n",
639 1.45 chs device_xname(sc->sc_dev)));
640 1.2 ad goto bad;
641 1.2 ad }
642 1.2 ad
643 1.2 ad bufs[nbuf].db_size = sz;
644 1.2 ad bufs[nbuf].db_ptr = malloc(sz, M_DEVBUF, M_WAITOK);
645 1.2 ad if (bufs[nbuf].db_ptr == NULL) {
646 1.2 ad DPRINTF(("%s: allocation failure\n",
647 1.45 chs device_xname(sc->sc_dev)));
648 1.2 ad rv = ENOMEM;
649 1.2 ad goto bad;
650 1.2 ad }
651 1.2 ad
652 1.2 ad for (i = 0, sz = 0; i < bufs[nbuf].db_nfrag; i++) {
653 1.2 ad rv = copyin(bufs[nbuf].db_frags[i].iov_base,
654 1.33 christos (char *)bufs[nbuf].db_ptr + sz,
655 1.2 ad bufs[nbuf].db_frags[i].iov_len);
656 1.2 ad if (rv != 0) {
657 1.2 ad DPRINTF(("%s: frag copyin\n",
658 1.45 chs device_xname(sc->sc_dev)));
659 1.2 ad goto bad;
660 1.2 ad }
661 1.2 ad sz += bufs[nbuf].db_frags[i].iov_len;
662 1.2 ad }
663 1.2 ad
664 1.2 ad if ((p[0] & I2O_SGL_END) != 0)
665 1.2 ad break;
666 1.1 ad }
667 1.1 ad
668 1.1 ad if (nbuf == IOP_MAX_MSG_XFERS) {
669 1.1 ad DPRINTF(("%s: too many transfers\n",
670 1.45 chs device_xname(sc->sc_dev)));
671 1.2 ad goto bad;
672 1.1 ad }
673 1.15 mycroft } else
674 1.15 mycroft nbuf = -1;
675 1.1 ad
676 1.1 ad /*
677 1.1 ad * Allocate a wrapper, and adjust the message header fields to
678 1.1 ad * indicate that no scatter-gather list is currently present.
679 1.1 ad */
680 1.2 ad
681 1.1 ad im = iop_msg_alloc(iop, IM_WAIT | IM_NOSTATUS);
682 1.1 ad im->im_rb = (struct i2o_reply *)rbtmp;
683 1.1 ad mf = (struct i2o_msg *)mbtmp;
684 1.1 ad mf->msgictx = IOP_ICTX;
685 1.1 ad mf->msgtctx = im->im_tctx;
686 1.1 ad
687 1.1 ad if (sgoff != 0)
688 1.1 ad mf->msgflags = (mf->msgflags & 0xff0f) | (sgoff << 16);
689 1.1 ad
690 1.1 ad /*
691 1.1 ad * Map the data transfer(s).
692 1.1 ad */
693 1.1 ad for (i = 0; i <= nbuf; i++) {
694 1.1 ad rv = iop_msg_map(iop, im, mbtmp, bufs[i].db_ptr,
695 1.1 ad bufs[i].db_size, bufs[i].db_out, bufs[i].db_proc);
696 1.1 ad if (rv != 0) {
697 1.2 ad DPRINTF(("%s: msg_map failed, rv = %d\n",
698 1.45 chs device_xname(sc->sc_dev), rv));
699 1.1 ad goto bad;
700 1.1 ad }
701 1.1 ad mapped = 1;
702 1.1 ad }
703 1.1 ad
704 1.1 ad /*
705 1.1 ad * Start the command and sleep until it completes.
706 1.1 ad */
707 1.1 ad if ((rv = iop_msg_post(iop, im, mbtmp, 5*60*1000)) != 0)
708 1.1 ad goto bad;
709 1.1 ad
710 1.1 ad /*
711 1.1 ad * Copy out the reply frame.
712 1.1 ad */
713 1.33 christos if ((rv = copyout(rbtmp, (char *)data + msgsize, repsize)) != 0) {
714 1.1 ad DPRINTF(("%s: reply copyout() failed\n",
715 1.45 chs device_xname(sc->sc_dev)));
716 1.27 christos }
717 1.1 ad
718 1.1 ad bad:
719 1.2 ad /*
720 1.2 ad * Free resources and return to the caller.
721 1.2 ad */
722 1.2 ad if (im != NULL) {
723 1.2 ad if (mapped)
724 1.2 ad iop_msg_unmap(iop, im);
725 1.2 ad iop_msg_free(iop, im);
726 1.2 ad }
727 1.2 ad
728 1.2 ad for (i = 0; i <= nbuf; i++) {
729 1.2 ad if (bufs[i].db_proc != NULL)
730 1.2 ad continue;
731 1.2 ad
732 1.2 ad if (!bufs[i].db_out && rv == 0) {
733 1.2 ad for (j = 0, sz = 0; j < bufs[i].db_nfrag; j++) {
734 1.33 christos rv = copyout((char *)bufs[i].db_ptr + sz,
735 1.2 ad bufs[i].db_frags[j].iov_base,
736 1.2 ad bufs[i].db_frags[j].iov_len);
737 1.2 ad if (rv != 0)
738 1.2 ad break;
739 1.2 ad sz += bufs[i].db_frags[j].iov_len;
740 1.2 ad }
741 1.2 ad }
742 1.1 ad
743 1.2 ad if (bufs[i].db_ptr != NULL)
744 1.2 ad free(bufs[i].db_ptr, M_DEVBUF);
745 1.2 ad }
746 1.1 ad
747 1.1 ad return (rv);
748 1.1 ad }
749