Home | History | Annotate | Line # | Download | only in i2o
dpti.c revision 1.3.14.1
      1 /*	$NetBSD: dpti.c,v 1.3.14.1 2002/12/12 23:34:45 he Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 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.3.14.1 2002/12/12 23:34:45 he Exp $");
     68 
     69 #include "opt_i2o.h"
     70 
     71 #include <sys/param.h>
     72 #include <sys/systm.h>
     73 #include <sys/kernel.h>
     74 #include <sys/device.h>
     75 #include <sys/queue.h>
     76 #include <sys/proc.h>
     77 #include <sys/endian.h>
     78 #include <sys/malloc.h>
     79 #include <sys/conf.h>
     80 #include <sys/ioctl.h>
     81 
     82 #include <uvm/uvm_extern.h>
     83 
     84 #include <machine/bus.h>
     85 #ifdef i386
     86 #include <machine/pio.h>
     87 #endif
     88 
     89 #include <dev/i2o/i2o.h>
     90 #include <dev/i2o/i2odpt.h>
     91 #include <dev/i2o/iopio.h>
     92 #include <dev/i2o/iopvar.h>
     93 #include <dev/i2o/dptivar.h>
     94 
     95 #ifdef I2ODEBUG
     96 #define	DPRINTF(x)		printf x
     97 #else
     98 #define	DPRINTF(x)
     99 #endif
    100 
    101 static struct dpt_sig dpti_sig = {
    102 	{ 'd', 'P', 't', 'S', 'i', 'G'},
    103 	SIG_VERSION,
    104 #if defined(i386)
    105 	PROC_INTEL,
    106 #elif defined(powerpc)
    107 	PROC_POWERPC,
    108 #elif defined(alpha)
    109 	PROC_ALPHA,
    110 #elif defined(mips)
    111 	PROC_MIPS,
    112 #elif defined(sparc64)
    113 	PROC_ULTRASPARC,
    114 #endif
    115 #if defined(i386)
    116 	PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
    117 #else
    118 	0,
    119 #endif
    120 	FT_HBADRVR,
    121 	0,
    122 	OEM_DPT,
    123 	OS_FREE_BSD,	/* XXX */
    124 	CAP_ABOVE16MB,
    125 	DEV_ALL,
    126 	ADF_ALL_SC5,
    127 	0,
    128 	0,
    129 	DPTI_VERSION,
    130 	DPTI_REVISION,
    131 	DPTI_SUBREVISION,
    132 	DPTI_MONTH,
    133 	DPTI_DAY,
    134 	DPTI_YEAR,
    135 	""		/* Will be filled later */
    136 };
    137 
    138 void	dpti_attach(struct device *, struct device *, void *);
    139 int	dpti_blinkled(struct dpti_softc *);
    140 int	dpti_ctlrinfo(struct dpti_softc *, int, caddr_t);
    141 int	dpti_match(struct device *, struct cfdata *, void *);
    142 int	dpti_passthrough(struct dpti_softc *, caddr_t, struct proc *);
    143 int	dpti_sysinfo(struct dpti_softc *, int, caddr_t);
    144 
    145 cdev_decl(dpti);
    146 
    147 extern struct cfdriver dpti_cd;
    148 
    149 struct cfattach dpti_ca = {
    150 	sizeof(struct dpti_softc), dpti_match, dpti_attach
    151 };
    152 
    153 int
    154 dpti_match(struct device *parent, struct cfdata *match, void *aux)
    155 {
    156 	struct iop_attach_args *ia;
    157 	struct iop_softc *iop;
    158 
    159 	ia = aux;
    160 	iop = (struct iop_softc *)parent;
    161 
    162 	if (ia->ia_class != I2O_CLASS_ANY || ia->ia_tid != I2O_TID_IOP)
    163 		return (0);
    164 
    165 	if (le16toh(iop->sc_status.orgid) != I2O_ORG_DPT)
    166 		return (0);
    167 
    168 	return (1);
    169 }
    170 
    171 void
    172 dpti_attach(struct device *parent, struct device *self, void *aux)
    173 {
    174 	struct iop_softc *iop;
    175 	struct dpti_softc *sc;
    176 	struct {
    177 		struct	i2o_param_op_results pr;
    178 		struct	i2o_param_read_results prr;
    179 		struct	i2o_dpt_param_exec_iop_buffers dib;
    180 	} __attribute__ ((__packed__)) param;
    181 	int rv;
    182 
    183 	sc = (struct dpti_softc *)self;
    184 	iop = (struct iop_softc *)parent;
    185 
    186 	/*
    187 	 * Tell the world what we are.  The description in the signature
    188 	 * must be no more than 46 bytes long (see dptivar.h).
    189 	 */
    190 	printf(": DPT/Adaptec RAID management interface\n");
    191 	sprintf(dpti_sig.dsDescription, "NetBSD %s I2O OSM", osrelease);
    192 
    193 	rv = iop_field_get_all(iop, I2O_TID_IOP,
    194 	    I2O_DPT_PARAM_EXEC_IOP_BUFFERS, &param,
    195 	    sizeof(param), NULL);
    196 	if (rv != 0)
    197 		return;
    198 
    199 	sc->sc_blinkled = le32toh(param.dib.serialoutputoff) + 8;
    200 }
    201 
    202 int
    203 dptiopen(dev_t dev, int flag, int mode, struct proc *p)
    204 {
    205 
    206 	if (securelevel > 1)
    207 		return (EPERM);
    208 	if (device_lookup(&dpti_cd, minor(dev)) == NULL)
    209 		return (ENXIO);
    210 
    211 	return (0);
    212 }
    213 
    214 int
    215 dpticlose(dev_t dev, int flag, int mode, struct proc *p)
    216 {
    217 
    218 	return (0);
    219 }
    220 
    221 int
    222 dptiioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    223 {
    224 	struct iop_softc *iop;
    225 	struct dpti_softc *sc;
    226 	struct ioctl_pt *pt;
    227 	int i, size, rv, linux;
    228 
    229 	sc = device_lookup(&dpti_cd, minor(dev));
    230 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    231 
    232 	if (cmd == PTIOCLINUX) {
    233 		pt = (struct ioctl_pt *)data;
    234 		size = IOCPARM_LEN(pt->com);
    235 		cmd = pt->com & 0xffff;
    236 		data = pt->data;
    237 		linux = 1;
    238 	} else {
    239 		size = IOCPARM_LEN(cmd);
    240 		cmd = cmd & 0xffff;
    241 		linux = 0;
    242 	}
    243 
    244 	switch (cmd) {
    245 	case DPT_SIGNATURE:
    246 		if (size > sizeof(dpti_sig))
    247 			size = sizeof(dpti_sig);
    248 		memcpy(data, &dpti_sig, size);
    249 		return (0);
    250 
    251 	case DPT_CTRLINFO:
    252 		return (dpti_ctlrinfo(sc, size, data));
    253 
    254 	case DPT_SYSINFO:
    255 		return (dpti_sysinfo(sc, size, data));
    256 
    257 	case DPT_BLINKLED:
    258 		if ((i = dpti_blinkled(sc)) == -1)
    259 			i = 0;
    260 
    261 		if (size == 0)
    262 			return (copyout(&i, *(caddr_t *)data, sizeof(i)));
    263 
    264 		*(int *)data = i;
    265 		return (0);
    266 
    267 	case DPT_TARGET_BUSY:
    268 		/*
    269 		 * XXX This is here to stop linux_machdepioctl() from
    270 		 * whining about an unknown ioctl.
    271 		 */
    272 		return (EIO);
    273 
    274 	case DPT_I2OUSRCMD:
    275 		if (sc->sc_nactive++ >= 2)
    276 			tsleep(&sc->sc_nactive, PRIBIO, "dptislp", 0);
    277 
    278 		if (linux)
    279 			rv = dpti_passthrough(sc, data, p);
    280 		else
    281 			rv = dpti_passthrough(sc, *(caddr_t *)data, p);
    282 
    283 		sc->sc_nactive--;
    284 		wakeup_one(&sc->sc_nactive);
    285 		return (rv);
    286 
    287 	case DPT_I2ORESETCMD:
    288 		printf("%s: I2ORESETCMD not implemented\n",
    289 		    sc->sc_dv.dv_xname);
    290 		return (EOPNOTSUPP);
    291 
    292 	case DPT_I2ORESCANCMD:
    293 		return (iop_reconfigure(iop, 0));
    294 
    295 	default:
    296 		return (ENOTTY);
    297 	}
    298 }
    299 
    300 int
    301 dpti_blinkled(struct dpti_softc *sc)
    302 {
    303 	struct iop_softc *iop;
    304 	u_int v;
    305 
    306 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    307 
    308 	v = bus_space_read_1(iop->sc_iot, iop->sc_ioh, sc->sc_blinkled + 0);
    309 	if (v == 0xbc) {
    310 		v = bus_space_read_1(iop->sc_iot, iop->sc_ioh,
    311 		    sc->sc_blinkled + 1);
    312 		return (v);
    313 	}
    314 
    315 	return (-1);
    316 }
    317 
    318 int
    319 dpti_ctlrinfo(struct dpti_softc *sc, int size, caddr_t data)
    320 {
    321 	struct dpt_ctlrinfo info;
    322 	struct iop_softc *iop;
    323 	int rv, i;
    324 
    325 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    326 
    327 	memset(&info, 0, sizeof(info));
    328 
    329 	info.length = sizeof(info) - sizeof(u_int16_t);
    330 	info.drvrHBAnum = sc->sc_dv.dv_unit;
    331 	info.baseAddr = iop->sc_memaddr;
    332 	if ((i = dpti_blinkled(sc)) == -1)
    333 		i = 0;
    334 	info.blinkState = i;
    335 	info.pciBusNum = iop->sc_pcibus;
    336 	info.pciDeviceNum = iop->sc_pcidev;
    337 	info.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
    338 	info.Interrupt = 10;			/* XXX */
    339 
    340 	if (size > sizeof(*data)) {
    341 		memcpy(data, &info, min(sizeof(info), size));
    342 		rv = 0;
    343 	} else
    344 		rv = copyout(&info, *(caddr_t *)data, sizeof(info));
    345 
    346 	return (rv);
    347 }
    348 
    349 int
    350 dpti_sysinfo(struct dpti_softc *sc, int size, caddr_t data)
    351 {
    352 	struct dpt_sysinfo info;
    353 	int rv;
    354 #ifdef i386
    355 	int i, j;
    356 #endif
    357 
    358 	memset(&info, 0, sizeof(info));
    359 
    360 #ifdef i386
    361 	outb (0x70, 0x12);
    362 	i = inb(0x71);
    363 	j = i >> 4;
    364 	if (i == 0x0f) {
    365 		outb (0x70, 0x19);
    366 		j = inb (0x71);
    367 	}
    368 	info.drive0CMOS = j;
    369 
    370 	j = i & 0x0f;
    371 	if (i == 0x0f) {
    372 		outb (0x70, 0x1a);
    373 		j = inb (0x71);
    374 	}
    375 	info.drive1CMOS = j;
    376 	info.processorFamily = dpti_sig.dsProcessorFamily;
    377 
    378 	/*
    379 	 * Get the conventional memory size from CMOS.
    380 	 */
    381 	outb(0x70, 0x16);
    382 	j = inb(0x71);
    383 	j <<= 8;
    384 	outb(0x70, 0x15);
    385 	j |= inb(0x71);
    386 	info.conventionalMemSize = j;
    387 
    388 	/*
    389 	 * Get the extended memory size from CMOS.
    390 	 */
    391 	outb(0x70, 0x31);
    392 	j = inb(0x71);
    393 	j <<= 8;
    394 	outb(0x70, 0x30);
    395 	j |= inb(0x71);
    396 	info.extendedMemSize = j;
    397 
    398 	switch (cpu_class) {
    399 	case CPUCLASS_386:
    400 		info.processorType = PROC_386;
    401 		break;
    402 	case CPUCLASS_486:
    403 		info.processorType = PROC_486;
    404 		break;
    405 	case CPUCLASS_586:
    406 		info.processorType = PROC_PENTIUM;
    407 		break;
    408 	case CPUCLASS_686:
    409 	default:
    410 		info.processorType = PROC_SEXIUM;
    411 		break;
    412 	}
    413 
    414 	info.flags = SI_CMOS_Valid | SI_BusTypeValid |
    415 	    SI_MemorySizeValid | SI_NO_SmartROM;
    416 #else
    417 	info.flags = SI_BusTypeValid | SI_NO_SmartROM;
    418 #endif
    419 
    420 	info.busType = SI_PCI_BUS;
    421 
    422 	/*
    423 	 * Copy out the info structure to the user.
    424 	 */
    425 	if (size > sizeof(*data)) {
    426 		memcpy(data, &info, min(sizeof(info), size));
    427 		rv = 0;
    428 	} else
    429 		rv = copyout(&info, *(caddr_t *)data, sizeof(info));
    430 
    431 	return (rv);
    432 }
    433 
    434 int
    435 dpti_passthrough(struct dpti_softc *sc, caddr_t data, struct proc *proc)
    436 {
    437 	struct iop_softc *iop;
    438 	struct i2o_msg mh, *mf;
    439 	struct i2o_reply rh;
    440 	struct iop_msg *im;
    441 	struct dpti_ptbuf bufs[IOP_MAX_MSG_XFERS];
    442 	u_int32_t mbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    443 	u_int32_t rbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    444 	int rv, msgsize, repsize, sgoff, i, mapped, nbuf, nfrag, j, sz;
    445 	u_int32_t *p, *pmax, *pstart;
    446 
    447 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    448 	im = NULL;
    449 
    450 	if ((rv = dpti_blinkled(sc)) != -1) {
    451 		if (rv != 0) {
    452 			printf("%s: adapter blinkled = 0x%02x\n",
    453 			    sc->sc_dv.dv_xname, rv);
    454 			return (EIO);
    455 		}
    456 	}
    457 
    458 	/*
    459 	 * Copy in the message frame header and determine the size of the
    460 	 * full message frame.
    461 	 */
    462 	if ((rv = copyin(data, &mh, sizeof(mh))) != 0) {
    463 		DPRINTF(("%s: message copyin failed\n",
    464 		    sc->sc_dv.dv_xname));
    465 		return (rv);
    466 	}
    467 
    468 	msgsize = (mh.msgflags >> 14) & ~3;
    469 	if (msgsize < sizeof(mh) || msgsize >= IOP_MAX_MSG_SIZE) {
    470 		DPRINTF(("%s: bad message frame size\n",
    471 		    sc->sc_dv.dv_xname));
    472 		return (EINVAL);
    473 	}
    474 
    475 	/*
    476 	 * Handle special commands.
    477 	 */
    478 	switch (mh.msgfunc >> 24) {
    479 	case I2O_EXEC_IOP_RESET:
    480 		printf("%s: I2O_EXEC_IOP_RESET not implemented\n",
    481 		    sc->sc_dv.dv_xname);
    482 		return (EOPNOTSUPP);
    483 
    484 	case I2O_EXEC_OUTBOUND_INIT:
    485 		printf("%s: I2O_EXEC_OUTBOUND_INIT not implemented\n",
    486 		    sc->sc_dv.dv_xname);
    487 		return (EOPNOTSUPP);
    488 
    489 	case I2O_EXEC_SYS_TAB_SET:
    490 		printf("%s: I2O_EXEC_SYS_TAB_SET not implemented\n",
    491 		    sc->sc_dv.dv_xname);
    492 		return (EOPNOTSUPP);
    493 
    494 	case I2O_EXEC_STATUS_GET:
    495 		if ((rv = iop_status_get(iop, 0)) == 0)
    496 			rv = copyout(&iop->sc_status, data + msgsize,
    497 			    sizeof(iop->sc_status));
    498 		return (rv);
    499 	}
    500 
    501 	/*
    502 	 * Copy in the full message frame.
    503 	 */
    504 	if ((rv = copyin(data, mbtmp, msgsize)) != 0) {
    505 		DPRINTF(("%s: full message copyin failed\n",
    506 		    sc->sc_dv.dv_xname));
    507 		return (rv);
    508 	}
    509 
    510 	/*
    511 	 * Determine the size of the reply frame, and copy it in.
    512 	 */
    513 	if ((rv = copyin(data + msgsize, &rh, sizeof(rh))) != 0) {
    514 		DPRINTF(("%s: reply copyin failed\n",
    515 		    sc->sc_dv.dv_xname));
    516 		return (rv);
    517 	}
    518 
    519 	repsize = (rh.msgflags >> 14) & ~3;
    520 	if (repsize < sizeof(rh) || repsize >= IOP_MAX_MSG_SIZE) {
    521 		DPRINTF(("%s: bad reply header size\n",
    522 		    sc->sc_dv.dv_xname));
    523 		return (EINVAL);
    524 	}
    525 
    526 	if ((rv = copyin(data + msgsize, rbtmp, repsize)) != 0) {
    527 		DPRINTF(("%s: reply too large\n", sc->sc_dv.dv_xname));
    528 		return (rv);
    529 	}
    530 
    531 	/*
    532 	 * If the message has a scatter gather list, it must be comprised of
    533 	 * simple elements.  If any one transfer contains multiple segments,
    534 	 * we allocate a temporary buffer for it; otherwise, the buffer will
    535 	 * be mapped directly.
    536 	 */
    537 	if ((sgoff = ((mh.msgflags >> 4) & 15)) != 0) {
    538 		if ((sgoff + 2) > (msgsize >> 2)) {
    539 			DPRINTF(("%s: invalid message size fields\n",
    540 			    sc->sc_dv.dv_xname));
    541 			return (EINVAL);
    542 		}
    543 
    544 		memset(bufs, 0, sizeof(bufs));
    545 	} else
    546 		nbuf = -1;
    547 
    548 	rv = EINVAL;
    549 
    550 	if (sgoff != 0) {
    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 				    sc->sc_dv.dv_xname));
    558 				goto bad;
    559 			}
    560 
    561 			if ((p[0] & 0x30000000) != I2O_SGL_SIMPLE) {
    562 				DPRINTF(("%s: invalid SGL (2)\n",
    563 				    sc->sc_dv.dv_xname));
    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 					    sc->sc_dv.dv_xname));
    574 					goto bad;
    575 				}
    576 
    577 				bufs[nbuf].db_ptr = (caddr_t)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 (pstart = p; p <= pmax; p += 2) {
    594 				if (nfrag == DPTI_MAX_SEGS) {
    595 					DPRINTF(("%s: too many segments\n",
    596 					    sc->sc_dv.dv_xname));
    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 						    sc->sc_dv.dv_xname));
    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 				    sc->sc_dv.dv_xname));
    625 				goto bad;
    626 			}
    627 
    628 			if (sz > IOP_MAX_XFER) {
    629 				DPRINTF(("%s: buffer too large\n",
    630 				    sc->sc_dv.dv_xname));
    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 				    sc->sc_dv.dv_xname));
    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 				    bufs[nbuf].db_ptr + sz,
    646 				    bufs[nbuf].db_frags[i].iov_len);
    647 				if (rv != 0) {
    648 					DPRINTF(("%s: frag copyin\n",
    649 					    sc->sc_dv.dv_xname));
    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 			    sc->sc_dv.dv_xname));
    662 			goto bad;
    663 		}
    664 	}
    665 
    666 	/*
    667 	 * Allocate a wrapper, and adjust the message header fields to
    668 	 * indicate that no scatter-gather list is currently present.
    669 	 */
    670 	mapped = 0;
    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 			    sc->sc_dv.dv_xname, 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, data + msgsize, repsize)) != 0)
    705 		DPRINTF(("%s: reply copyout() failed\n",
    706 		    sc->sc_dv.dv_xname));
    707 
    708  bad:
    709 	/*
    710 	 * Free resources and return to the caller.
    711 	 */
    712 	if (im != NULL) {
    713 		if (mapped)
    714 			iop_msg_unmap(iop, im);
    715 		iop_msg_free(iop, im);
    716 	}
    717 
    718 	for (i = 0; i <= nbuf; i++) {
    719 		if (bufs[i].db_proc != NULL)
    720 			continue;
    721 
    722 		if (!bufs[i].db_out && rv == 0) {
    723 			for (j = 0, sz = 0; j < bufs[i].db_nfrag; j++) {
    724 				rv = copyout(bufs[i].db_ptr + sz,
    725 				    bufs[i].db_frags[j].iov_base,
    726 				    bufs[i].db_frags[j].iov_len);
    727 				if (rv != 0)
    728 					break;
    729 				sz += bufs[i].db_frags[j].iov_len;
    730 			}
    731 		}
    732 
    733 		if (bufs[i].db_ptr != NULL)
    734 			free(bufs[i].db_ptr, M_DEVBUF);
    735 	}
    736 
    737 	return (rv);
    738 }
    739