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