xref: /netbsd-src/sys/dev/i2o/dpti.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: dpti.c,v 1.35 2007/10/19 11:59:43 ad 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.35 2007/10/19 11:59:43 ad 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 		    sc->sc_dv.dv_xname);
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 			printf("%s: adapter blinkled = 0x%02x\n",
468 			    sc->sc_dv.dv_xname, rv);
469 			return (EIO);
470 		}
471 	}
472 
473 	/*
474 	 * Copy in the message frame header and determine the size of the
475 	 * full message frame.
476 	 */
477 	if ((rv = copyin(data, &mh, sizeof(mh))) != 0) {
478 		DPRINTF(("%s: message copyin failed\n",
479 		    sc->sc_dv.dv_xname));
480 		return (rv);
481 	}
482 
483 	msgsize = (mh.msgflags >> 14) & ~3;
484 	if (msgsize < sizeof(mh) || msgsize >= IOP_MAX_MSG_SIZE) {
485 		DPRINTF(("%s: bad message frame size\n",
486 		    sc->sc_dv.dv_xname));
487 		return (EINVAL);
488 	}
489 
490 	/*
491 	 * Handle special commands.
492 	 */
493 	switch (mh.msgfunc >> 24) {
494 	case I2O_EXEC_IOP_RESET:
495 		printf("%s: I2O_EXEC_IOP_RESET not implemented\n",
496 		    sc->sc_dv.dv_xname);
497 		return (EOPNOTSUPP);
498 
499 	case I2O_EXEC_OUTBOUND_INIT:
500 		printf("%s: I2O_EXEC_OUTBOUND_INIT not implemented\n",
501 		    sc->sc_dv.dv_xname);
502 		return (EOPNOTSUPP);
503 
504 	case I2O_EXEC_SYS_TAB_SET:
505 		printf("%s: I2O_EXEC_SYS_TAB_SET not implemented\n",
506 		    sc->sc_dv.dv_xname);
507 		return (EOPNOTSUPP);
508 
509 	case I2O_EXEC_STATUS_GET:
510 		if ((rv = iop_status_get(iop, 0)) == 0)
511 			rv = copyout(&iop->sc_status, (char *)data + msgsize,
512 			    sizeof(iop->sc_status));
513 		return (rv);
514 	}
515 
516 	/*
517 	 * Copy in the full message frame.
518 	 */
519 	if ((rv = copyin(data, mbtmp, msgsize)) != 0) {
520 		DPRINTF(("%s: full message copyin failed\n",
521 		    sc->sc_dv.dv_xname));
522 		return (rv);
523 	}
524 
525 	/*
526 	 * Determine the size of the reply frame, and copy it in.
527 	 */
528 	if ((rv = copyin((char *)data + msgsize, &rh, sizeof(rh))) != 0) {
529 		DPRINTF(("%s: reply copyin failed\n",
530 		    sc->sc_dv.dv_xname));
531 		return (rv);
532 	}
533 
534 	repsize = (rh.msgflags >> 14) & ~3;
535 	if (repsize < sizeof(rh) || repsize >= IOP_MAX_MSG_SIZE) {
536 		DPRINTF(("%s: bad reply header size\n",
537 		    sc->sc_dv.dv_xname));
538 		return (EINVAL);
539 	}
540 
541 	if ((rv = copyin((char *)data + msgsize, rbtmp, repsize)) != 0) {
542 		DPRINTF(("%s: reply too large\n", sc->sc_dv.dv_xname));
543 		return (rv);
544 	}
545 
546 	/*
547 	 * If the message has a scatter gather list, it must be comprised of
548 	 * simple elements.  If any one transfer contains multiple segments,
549 	 * we allocate a temporary buffer for it; otherwise, the buffer will
550 	 * be mapped directly.
551 	 */
552 	mapped = 0;
553 	if ((sgoff = ((mh.msgflags >> 4) & 15)) != 0) {
554 		if ((sgoff + 2) > (msgsize >> 2)) {
555 			DPRINTF(("%s: invalid message size fields\n",
556 			    sc->sc_dv.dv_xname));
557 			return (EINVAL);
558 		}
559 
560 		memset(bufs, 0, sizeof(bufs));
561 
562 		p = mbtmp + sgoff;
563 		pmax = mbtmp + (msgsize >> 2) - 2;
564 
565 		for (nbuf = 0; nbuf < IOP_MAX_MSG_XFERS; nbuf++, p += 2) {
566 			if (p > pmax) {
567 				DPRINTF(("%s: invalid SGL (1)\n",
568 				    sc->sc_dv.dv_xname));
569 				goto bad;
570 			}
571 
572 			if ((p[0] & 0x30000000) != I2O_SGL_SIMPLE) {
573 				DPRINTF(("%s: invalid SGL (2)\n",
574 				    sc->sc_dv.dv_xname));
575 				goto bad;
576 			}
577 
578 			bufs[nbuf].db_out = (p[0] & I2O_SGL_DATA_OUT) != 0;
579 			bufs[nbuf].db_ptr = NULL;
580 
581 			if ((p[0] & I2O_SGL_END_BUFFER) != 0) {
582 				if ((p[0] & 0x00ffffff) > IOP_MAX_XFER) {
583 					DPRINTF(("%s: buffer too large\n",
584 					    sc->sc_dv.dv_xname));
585 					goto bad;
586 				}
587 
588 				bufs[nbuf].db_ptr = (void *)p[1];
589 				bufs[nbuf].db_proc = proc;
590 				bufs[nbuf].db_size = p[0] & 0x00ffffff;
591 
592 				if ((p[0] & I2O_SGL_END) != 0)
593 					break;
594 
595 				continue;
596 			}
597 
598 			/*
599 			 * The buffer has multiple segments.  Determine the
600 			 * total size.
601 			 */
602 			nfrag = 0;
603 			sz = 0;
604 			for (; p <= pmax; p += 2) {
605 				if (nfrag == DPTI_MAX_SEGS) {
606 					DPRINTF(("%s: too many segments\n",
607 					    sc->sc_dv.dv_xname));
608 					goto bad;
609 				}
610 
611 				bufs[nbuf].db_frags[nfrag].iov_len =
612 				    p[0] & 0x00ffffff;
613 				bufs[nbuf].db_frags[nfrag].iov_base =
614 				    (void *)p[1];
615 
616 				sz += p[0] & 0x00ffffff;
617 				nfrag++;
618 
619 				if ((p[0] & I2O_SGL_END) != 0) {
620 					if ((p[0] & I2O_SGL_END_BUFFER) == 0) {
621 						DPRINTF((
622 						    "%s: invalid SGL (3)\n",
623 						    sc->sc_dv.dv_xname));
624 						goto bad;
625 					}
626 					break;
627 				}
628 				if ((p[0] & I2O_SGL_END_BUFFER) != 0)
629 					break;
630 			}
631 			bufs[nbuf].db_nfrag = nfrag;
632 
633 			if (p > pmax) {
634 				DPRINTF(("%s: invalid SGL (4)\n",
635 				    sc->sc_dv.dv_xname));
636 				goto bad;
637 			}
638 
639 			if (sz > IOP_MAX_XFER) {
640 				DPRINTF(("%s: buffer too large\n",
641 				    sc->sc_dv.dv_xname));
642 				goto bad;
643 			}
644 
645 			bufs[nbuf].db_size = sz;
646 			bufs[nbuf].db_ptr = malloc(sz, M_DEVBUF, M_WAITOK);
647 			if (bufs[nbuf].db_ptr == NULL) {
648 				DPRINTF(("%s: allocation failure\n",
649 				    sc->sc_dv.dv_xname));
650 				rv = ENOMEM;
651 				goto bad;
652 			}
653 
654 			for (i = 0, sz = 0; i < bufs[nbuf].db_nfrag; i++) {
655 				rv = copyin(bufs[nbuf].db_frags[i].iov_base,
656 				    (char *)bufs[nbuf].db_ptr + sz,
657 				    bufs[nbuf].db_frags[i].iov_len);
658 				if (rv != 0) {
659 					DPRINTF(("%s: frag copyin\n",
660 					    sc->sc_dv.dv_xname));
661 					goto bad;
662 				}
663 				sz += bufs[nbuf].db_frags[i].iov_len;
664 			}
665 
666 			if ((p[0] & I2O_SGL_END) != 0)
667 				break;
668 		}
669 
670 		if (nbuf == IOP_MAX_MSG_XFERS) {
671 			DPRINTF(("%s: too many transfers\n",
672 			    sc->sc_dv.dv_xname));
673 			goto bad;
674 		}
675 	} else
676 		nbuf = -1;
677 
678 	/*
679 	 * Allocate a wrapper, and adjust the message header fields to
680 	 * indicate that no scatter-gather list is currently present.
681 	 */
682 
683 	im = iop_msg_alloc(iop, IM_WAIT | IM_NOSTATUS);
684 	im->im_rb = (struct i2o_reply *)rbtmp;
685 	mf = (struct i2o_msg *)mbtmp;
686 	mf->msgictx = IOP_ICTX;
687 	mf->msgtctx = im->im_tctx;
688 
689 	if (sgoff != 0)
690 		mf->msgflags = (mf->msgflags & 0xff0f) | (sgoff << 16);
691 
692 	/*
693 	 * Map the data transfer(s).
694 	 */
695 	for (i = 0; i <= nbuf; i++) {
696 		rv = iop_msg_map(iop, im, mbtmp, bufs[i].db_ptr,
697 		    bufs[i].db_size, bufs[i].db_out, bufs[i].db_proc);
698 		if (rv != 0) {
699 			DPRINTF(("%s: msg_map failed, rv = %d\n",
700 			    sc->sc_dv.dv_xname, rv));
701 			goto bad;
702 		}
703 		mapped = 1;
704 	}
705 
706 	/*
707 	 * Start the command and sleep until it completes.
708 	 */
709 	if ((rv = iop_msg_post(iop, im, mbtmp, 5*60*1000)) != 0)
710 		goto bad;
711 
712 	/*
713 	 * Copy out the reply frame.
714 	 */
715 	if ((rv = copyout(rbtmp, (char *)data + msgsize, repsize)) != 0) {
716 		DPRINTF(("%s: reply copyout() failed\n",
717 		    sc->sc_dv.dv_xname));
718 	}
719 
720  bad:
721 	/*
722 	 * Free resources and return to the caller.
723 	 */
724 	if (im != NULL) {
725 		if (mapped)
726 			iop_msg_unmap(iop, im);
727 		iop_msg_free(iop, im);
728 	}
729 
730 	for (i = 0; i <= nbuf; i++) {
731 		if (bufs[i].db_proc != NULL)
732 			continue;
733 
734 		if (!bufs[i].db_out && rv == 0) {
735 			for (j = 0, sz = 0; j < bufs[i].db_nfrag; j++) {
736 				rv = copyout((char *)bufs[i].db_ptr + sz,
737 				    bufs[i].db_frags[j].iov_base,
738 				    bufs[i].db_frags[j].iov_len);
739 				if (rv != 0)
740 					break;
741 				sz += bufs[i].db_frags[j].iov_len;
742 			}
743 		}
744 
745 		if (bufs[i].db_ptr != NULL)
746 			free(bufs[i].db_ptr, M_DEVBUF);
747 	}
748 
749 	return (rv);
750 }
751