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