Home | History | Annotate | Line # | Download | only in i2o
dpti.c revision 1.26
      1 /*	$NetBSD: dpti.c,v 1.26 2006/08/30 00:41:46 christos 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.26 2006/08/30 00:41:46 christos 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 dev_type_open(dptiopen);
    146 dev_type_ioctl(dptiioctl);
    147 
    148 const struct cdevsw dpti_cdevsw = {
    149 	dptiopen, nullclose, noread, nowrite, dptiioctl,
    150 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
    151 };
    152 
    153 extern struct cfdriver dpti_cd;
    154 
    155 CFATTACH_DECL(dpti, sizeof(struct dpti_softc),
    156     dpti_match, dpti_attach, NULL, NULL);
    157 
    158 int
    159 dpti_match(struct device *parent, struct cfdata *match, void *aux)
    160 {
    161 	struct iop_attach_args *ia;
    162 	struct iop_softc *iop;
    163 
    164 	ia = aux;
    165 	iop = (struct iop_softc *)parent;
    166 
    167 	if (ia->ia_class != I2O_CLASS_ANY || ia->ia_tid != I2O_TID_IOP)
    168 		return (0);
    169 
    170 	if (le16toh(iop->sc_status.orgid) != I2O_ORG_DPT)
    171 		return (0);
    172 
    173 	return (1);
    174 }
    175 
    176 void
    177 dpti_attach(struct device *parent, struct device *self, void *aux)
    178 {
    179 	struct iop_softc *iop;
    180 	struct dpti_softc *sc;
    181 	struct {
    182 		struct	i2o_param_op_results pr;
    183 		struct	i2o_param_read_results prr;
    184 		struct	i2o_dpt_param_exec_iop_buffers dib;
    185 	} __attribute__ ((__packed__)) param;
    186 	int rv;
    187 
    188 	sc = device_private(self);
    189 	iop = device_private(parent);
    190 
    191 	/*
    192 	 * Tell the world what we are.  The description in the signature
    193 	 * must be no more than 46 bytes long (see dptivar.h).
    194 	 */
    195 	printf(": DPT/Adaptec RAID management interface\n");
    196 	snprintf(dpti_sig.dsDescription, sizeof(dpti_sig.dsDescription),
    197 	    "NetBSD %s I2O OSM", osrelease);
    198 
    199 	rv = iop_field_get_all(iop, I2O_TID_IOP,
    200 	    I2O_DPT_PARAM_EXEC_IOP_BUFFERS, &param,
    201 	    sizeof(param), NULL);
    202 	if (rv != 0)
    203 		return;
    204 
    205 	sc->sc_blinkled = le32toh(param.dib.serialoutputoff) + 8;
    206 }
    207 
    208 int
    209 dptiopen(dev_t dev, int flag, int mode, 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, caddr_t 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, *(caddr_t *)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 		if (securelevel > 1) {
    279 			rv = EPERM;
    280 			break;
    281 		}
    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, *(caddr_t *)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 		if ((rv = lockmgr(&iop->sc_conflock, LK_EXCLUSIVE, NULL)) != 0)
    303 			break;
    304 		rv = iop_reconfigure(iop, 0);
    305 		lockmgr(&iop->sc_conflock, LK_RELEASE, NULL);
    306 		break;
    307 
    308 	default:
    309 		rv = ENOTTY;
    310 		break;
    311 	}
    312 
    313 	return (rv);
    314 }
    315 
    316 int
    317 dpti_blinkled(struct dpti_softc *sc)
    318 {
    319 	struct iop_softc *iop;
    320 	u_int v;
    321 
    322 	iop = (struct iop_softc *)device_parent(&sc->sc_dv);
    323 
    324 	v = bus_space_read_1(iop->sc_iot, iop->sc_ioh, sc->sc_blinkled + 0);
    325 	if (v == 0xbc) {
    326 		v = bus_space_read_1(iop->sc_iot, iop->sc_ioh,
    327 		    sc->sc_blinkled + 1);
    328 		return (v);
    329 	}
    330 
    331 	return (-1);
    332 }
    333 
    334 int
    335 dpti_ctlrinfo(struct dpti_softc *sc, int size, caddr_t data)
    336 {
    337 	struct dpt_ctlrinfo info;
    338 	struct iop_softc *iop;
    339 	int rv, i;
    340 
    341 	iop = (struct iop_softc *)device_parent(&sc->sc_dv);
    342 
    343 	memset(&info, 0, sizeof(info));
    344 
    345 	info.length = sizeof(info) - sizeof(u_int16_t);
    346 	info.drvrHBAnum = device_unit(&sc->sc_dv);
    347 	info.baseAddr = iop->sc_memaddr;
    348 	if ((i = dpti_blinkled(sc)) == -1)
    349 		i = 0;
    350 	info.blinkState = i;
    351 	info.pciBusNum = iop->sc_pcibus;
    352 	info.pciDeviceNum = iop->sc_pcidev;
    353 	info.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
    354 	info.Interrupt = 10;			/* XXX */
    355 
    356 	if (size > sizeof(*data)) {
    357 		memcpy(data, &info, min(sizeof(info), size));
    358 		rv = 0;
    359 	} else
    360 		rv = copyout(&info, *(caddr_t *)data, sizeof(info));
    361 
    362 	return (rv);
    363 }
    364 
    365 int
    366 dpti_sysinfo(struct dpti_softc *sc, int size, caddr_t data)
    367 {
    368 	struct dpt_sysinfo info;
    369 	int rv;
    370 #ifdef __i386__
    371 	int i, j;
    372 #endif
    373 
    374 	memset(&info, 0, sizeof(info));
    375 
    376 #ifdef __i386__
    377 	outb (0x70, 0x12);
    378 	i = inb(0x71);
    379 	j = i >> 4;
    380 	if (i == 0x0f) {
    381 		outb (0x70, 0x19);
    382 		j = inb (0x71);
    383 	}
    384 	info.drive0CMOS = j;
    385 
    386 	j = i & 0x0f;
    387 	if (i == 0x0f) {
    388 		outb (0x70, 0x1a);
    389 		j = inb (0x71);
    390 	}
    391 	info.drive1CMOS = j;
    392 	info.processorFamily = dpti_sig.dsProcessorFamily;
    393 
    394 	/*
    395 	 * Get the conventional memory size from CMOS.
    396 	 */
    397 	outb(0x70, 0x16);
    398 	j = inb(0x71);
    399 	j <<= 8;
    400 	outb(0x70, 0x15);
    401 	j |= inb(0x71);
    402 	info.conventionalMemSize = j;
    403 
    404 	/*
    405 	 * Get the extended memory size from CMOS.
    406 	 */
    407 	outb(0x70, 0x31);
    408 	j = inb(0x71);
    409 	j <<= 8;
    410 	outb(0x70, 0x30);
    411 	j |= inb(0x71);
    412 	info.extendedMemSize = j;
    413 
    414 	switch (cpu_class) {
    415 	case CPUCLASS_386:
    416 		info.processorType = PROC_386;
    417 		break;
    418 	case CPUCLASS_486:
    419 		info.processorType = PROC_486;
    420 		break;
    421 	case CPUCLASS_586:
    422 		info.processorType = PROC_PENTIUM;
    423 		break;
    424 	case CPUCLASS_686:
    425 	default:
    426 		info.processorType = PROC_SEXIUM;
    427 		break;
    428 	}
    429 
    430 	info.flags = SI_CMOS_Valid | SI_BusTypeValid |
    431 	    SI_MemorySizeValid | SI_NO_SmartROM;
    432 #else
    433 	info.flags = SI_BusTypeValid | SI_NO_SmartROM;
    434 #endif
    435 
    436 	info.busType = SI_PCI_BUS;
    437 
    438 	/*
    439 	 * Copy out the info structure to the user.
    440 	 */
    441 	if (size > sizeof(*data)) {
    442 		memcpy(data, &info, min(sizeof(info), size));
    443 		rv = 0;
    444 	} else
    445 		rv = copyout(&info, *(caddr_t *)data, sizeof(info));
    446 
    447 	return (rv);
    448 }
    449 
    450 int
    451 dpti_passthrough(struct dpti_softc *sc, caddr_t data, struct proc *proc)
    452 {
    453 	struct iop_softc *iop;
    454 	struct i2o_msg mh, *mf;
    455 	struct i2o_reply rh;
    456 	struct iop_msg *im;
    457 	struct dpti_ptbuf bufs[IOP_MAX_MSG_XFERS];
    458 	u_int32_t mbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    459 	u_int32_t rbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    460 	int rv, msgsize, repsize, sgoff, i, mapped, nbuf, nfrag, j, sz;
    461 	u_int32_t *p, *pmax;
    462 
    463 	iop = (struct iop_softc *)device_parent(&sc->sc_dv);
    464 	im = NULL;
    465 
    466 	if ((rv = dpti_blinkled(sc)) != -1) {
    467 		if (rv != 0) {
    468 			printf("%s: adapter blinkled = 0x%02x\n",
    469 			    sc->sc_dv.dv_xname, rv);
    470 			return (EIO);
    471 		}
    472 	}
    473 
    474 	/*
    475 	 * Copy in the message frame header and determine the size of the
    476 	 * full message frame.
    477 	 */
    478 	if ((rv = copyin(data, &mh, sizeof(mh))) != 0) {
    479 		DPRINTF(("%s: message copyin failed\n",
    480 		    sc->sc_dv.dv_xname));
    481 		return (rv);
    482 	}
    483 
    484 	msgsize = (mh.msgflags >> 14) & ~3;
    485 	if (msgsize < sizeof(mh) || msgsize >= IOP_MAX_MSG_SIZE) {
    486 		DPRINTF(("%s: bad message frame size\n",
    487 		    sc->sc_dv.dv_xname));
    488 		return (EINVAL);
    489 	}
    490 
    491 	/*
    492 	 * Handle special commands.
    493 	 */
    494 	switch (mh.msgfunc >> 24) {
    495 	case I2O_EXEC_IOP_RESET:
    496 		printf("%s: I2O_EXEC_IOP_RESET not implemented\n",
    497 		    sc->sc_dv.dv_xname);
    498 		return (EOPNOTSUPP);
    499 
    500 	case I2O_EXEC_OUTBOUND_INIT:
    501 		printf("%s: I2O_EXEC_OUTBOUND_INIT not implemented\n",
    502 		    sc->sc_dv.dv_xname);
    503 		return (EOPNOTSUPP);
    504 
    505 	case I2O_EXEC_SYS_TAB_SET:
    506 		printf("%s: I2O_EXEC_SYS_TAB_SET not implemented\n",
    507 		    sc->sc_dv.dv_xname);
    508 		return (EOPNOTSUPP);
    509 
    510 	case I2O_EXEC_STATUS_GET:
    511 		if ((rv = iop_status_get(iop, 0)) == 0)
    512 			rv = copyout(&iop->sc_status, data + msgsize,
    513 			    sizeof(iop->sc_status));
    514 		return (rv);
    515 	}
    516 
    517 	/*
    518 	 * Copy in the full message frame.
    519 	 */
    520 	if ((rv = copyin(data, mbtmp, msgsize)) != 0) {
    521 		DPRINTF(("%s: full message copyin failed\n",
    522 		    sc->sc_dv.dv_xname));
    523 		return (rv);
    524 	}
    525 
    526 	/*
    527 	 * Determine the size of the reply frame, and copy it in.
    528 	 */
    529 	if ((rv = copyin(data + msgsize, &rh, sizeof(rh))) != 0) {
    530 		DPRINTF(("%s: reply copyin failed\n",
    531 		    sc->sc_dv.dv_xname));
    532 		return (rv);
    533 	}
    534 
    535 	repsize = (rh.msgflags >> 14) & ~3;
    536 	if (repsize < sizeof(rh) || repsize >= IOP_MAX_MSG_SIZE) {
    537 		DPRINTF(("%s: bad reply header size\n",
    538 		    sc->sc_dv.dv_xname));
    539 		return (EINVAL);
    540 	}
    541 
    542 	if ((rv = copyin(data + msgsize, rbtmp, repsize)) != 0) {
    543 		DPRINTF(("%s: reply too large\n", sc->sc_dv.dv_xname));
    544 		return (rv);
    545 	}
    546 
    547 	/*
    548 	 * If the message has a scatter gather list, it must be comprised of
    549 	 * simple elements.  If any one transfer contains multiple segments,
    550 	 * we allocate a temporary buffer for it; otherwise, the buffer will
    551 	 * be mapped directly.
    552 	 */
    553 	mapped = 0;
    554 	if ((sgoff = ((mh.msgflags >> 4) & 15)) != 0) {
    555 		if ((sgoff + 2) > (msgsize >> 2)) {
    556 			DPRINTF(("%s: invalid message size fields\n",
    557 			    sc->sc_dv.dv_xname));
    558 			return (EINVAL);
    559 		}
    560 
    561 		memset(bufs, 0, sizeof(bufs));
    562 
    563 		p = mbtmp + sgoff;
    564 		pmax = mbtmp + (msgsize >> 2) - 2;
    565 
    566 		for (nbuf = 0; nbuf < IOP_MAX_MSG_XFERS; nbuf++, p += 2) {
    567 			if (p > pmax) {
    568 				DPRINTF(("%s: invalid SGL (1)\n",
    569 				    sc->sc_dv.dv_xname));
    570 				goto bad;
    571 			}
    572 
    573 			if ((p[0] & 0x30000000) != I2O_SGL_SIMPLE) {
    574 				DPRINTF(("%s: invalid SGL (2)\n",
    575 				    sc->sc_dv.dv_xname));
    576 				goto bad;
    577 			}
    578 
    579 			bufs[nbuf].db_out = (p[0] & I2O_SGL_DATA_OUT) != 0;
    580 			bufs[nbuf].db_ptr = NULL;
    581 
    582 			if ((p[0] & I2O_SGL_END_BUFFER) != 0) {
    583 				if ((p[0] & 0x00ffffff) > IOP_MAX_XFER) {
    584 					DPRINTF(("%s: buffer too large\n",
    585 					    sc->sc_dv.dv_xname));
    586 					goto bad;
    587 				}
    588 
    589 				bufs[nbuf].db_ptr = (caddr_t)p[1];
    590 				bufs[nbuf].db_proc = proc;
    591 				bufs[nbuf].db_size = p[0] & 0x00ffffff;
    592 
    593 				if ((p[0] & I2O_SGL_END) != 0)
    594 					break;
    595 
    596 				continue;
    597 			}
    598 
    599 			/*
    600 			 * The buffer has multiple segments.  Determine the
    601 			 * total size.
    602 			 */
    603 			nfrag = 0;
    604 			sz = 0;
    605 			for (; p <= pmax; p += 2) {
    606 				if (nfrag == DPTI_MAX_SEGS) {
    607 					DPRINTF(("%s: too many segments\n",
    608 					    sc->sc_dv.dv_xname));
    609 					goto bad;
    610 				}
    611 
    612 				bufs[nbuf].db_frags[nfrag].iov_len =
    613 				    p[0] & 0x00ffffff;
    614 				bufs[nbuf].db_frags[nfrag].iov_base =
    615 				    (void *)p[1];
    616 
    617 				sz += p[0] & 0x00ffffff;
    618 				nfrag++;
    619 
    620 				if ((p[0] & I2O_SGL_END) != 0) {
    621 					if ((p[0] & I2O_SGL_END_BUFFER) == 0) {
    622 						DPRINTF((
    623 						    "%s: invalid SGL (3)\n",
    624 						    sc->sc_dv.dv_xname));
    625 						goto bad;
    626 					}
    627 					break;
    628 				}
    629 				if ((p[0] & I2O_SGL_END_BUFFER) != 0)
    630 					break;
    631 			}
    632 			bufs[nbuf].db_nfrag = nfrag;
    633 
    634 			if (p > pmax) {
    635 				DPRINTF(("%s: invalid SGL (4)\n",
    636 				    sc->sc_dv.dv_xname));
    637 				goto bad;
    638 			}
    639 
    640 			if (sz > IOP_MAX_XFER) {
    641 				DPRINTF(("%s: buffer too large\n",
    642 				    sc->sc_dv.dv_xname));
    643 				goto bad;
    644 			}
    645 
    646 			bufs[nbuf].db_size = sz;
    647 			bufs[nbuf].db_ptr = malloc(sz, M_DEVBUF, M_WAITOK);
    648 			if (bufs[nbuf].db_ptr == NULL) {
    649 				DPRINTF(("%s: allocation failure\n",
    650 				    sc->sc_dv.dv_xname));
    651 				rv = ENOMEM;
    652 				goto bad;
    653 			}
    654 
    655 			for (i = 0, sz = 0; i < bufs[nbuf].db_nfrag; i++) {
    656 				rv = copyin(bufs[nbuf].db_frags[i].iov_base,
    657 				    bufs[nbuf].db_ptr + sz,
    658 				    bufs[nbuf].db_frags[i].iov_len);
    659 				if (rv != 0) {
    660 					DPRINTF(("%s: frag copyin\n",
    661 					    sc->sc_dv.dv_xname));
    662 					goto bad;
    663 				}
    664 				sz += bufs[nbuf].db_frags[i].iov_len;
    665 			}
    666 
    667 			if ((p[0] & I2O_SGL_END) != 0)
    668 				break;
    669 		}
    670 
    671 		if (nbuf == IOP_MAX_MSG_XFERS) {
    672 			DPRINTF(("%s: too many transfers\n",
    673 			    sc->sc_dv.dv_xname));
    674 			goto bad;
    675 		}
    676 	} else
    677 		nbuf = -1;
    678 
    679 	/*
    680 	 * Allocate a wrapper, and adjust the message header fields to
    681 	 * indicate that no scatter-gather list is currently present.
    682 	 */
    683 
    684 	im = iop_msg_alloc(iop, IM_WAIT | IM_NOSTATUS);
    685 	im->im_rb = (struct i2o_reply *)rbtmp;
    686 	mf = (struct i2o_msg *)mbtmp;
    687 	mf->msgictx = IOP_ICTX;
    688 	mf->msgtctx = im->im_tctx;
    689 
    690 	if (sgoff != 0)
    691 		mf->msgflags = (mf->msgflags & 0xff0f) | (sgoff << 16);
    692 
    693 	/*
    694 	 * Map the data transfer(s).
    695 	 */
    696 	for (i = 0; i <= nbuf; i++) {
    697 		rv = iop_msg_map(iop, im, mbtmp, bufs[i].db_ptr,
    698 		    bufs[i].db_size, bufs[i].db_out, bufs[i].db_proc);
    699 		if (rv != 0) {
    700 			DPRINTF(("%s: msg_map failed, rv = %d\n",
    701 			    sc->sc_dv.dv_xname, rv));
    702 			goto bad;
    703 		}
    704 		mapped = 1;
    705 	}
    706 
    707 	/*
    708 	 * Start the command and sleep until it completes.
    709 	 */
    710 	if ((rv = iop_msg_post(iop, im, mbtmp, 5*60*1000)) != 0)
    711 		goto bad;
    712 
    713 	/*
    714 	 * Copy out the reply frame.
    715 	 */
    716 	if ((rv = copyout(rbtmp, data + msgsize, repsize)) != 0)
    717 		DPRINTF(("%s: reply copyout() failed\n",
    718 		    sc->sc_dv.dv_xname));
    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(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