Home | History | Annotate | Line # | Download | only in i2o
dpti.c revision 1.17
      1 /*	$NetBSD: dpti.c,v 1.17 2003/12/09 19:51:23 ad 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.17 2003/12/09 19:51:23 ad 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,
    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 = (struct dpti_softc *)self;
    189 	iop = (struct iop_softc *)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 	sprintf(dpti_sig.dsDescription, "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, struct proc *p)
    209 {
    210 
    211 	if (securelevel > 1)
    212 		return (EPERM);
    213 	if (device_lookup(&dpti_cd, minor(dev)) == NULL)
    214 		return (ENXIO);
    215 
    216 	return (0);
    217 }
    218 
    219 int
    220 dptiioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    221 {
    222 	struct iop_softc *iop;
    223 	struct dpti_softc *sc;
    224 	struct ioctl_pt *pt;
    225 	int i, size, rv, linux;
    226 
    227 	sc = device_lookup(&dpti_cd, minor(dev));
    228 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    229 	rv = 0;
    230 
    231 	if (cmd == PTIOCLINUX) {
    232 		pt = (struct ioctl_pt *)data;
    233 		size = IOCPARM_LEN(pt->com);
    234 		cmd = pt->com & 0xffff;
    235 		data = pt->data;
    236 		linux = 1;
    237 	} else {
    238 		size = IOCPARM_LEN(cmd);
    239 		cmd = cmd & 0xffff;
    240 		linux = 0;
    241 	}
    242 
    243 	switch (cmd) {
    244 	case DPT_SIGNATURE:
    245 		if (size > sizeof(dpti_sig))
    246 			size = sizeof(dpti_sig);
    247 		memcpy(data, &dpti_sig, size);
    248 		break;
    249 
    250 	case DPT_CTRLINFO:
    251 		rv = dpti_ctlrinfo(sc, size, data);
    252 		break;
    253 
    254 	case DPT_SYSINFO:
    255 		rv = dpti_sysinfo(sc, size, data);
    256 		break;
    257 
    258 	case DPT_BLINKLED:
    259 		if ((i = dpti_blinkled(sc)) == -1)
    260 			i = 0;
    261 
    262 		if (size == 0) {
    263 			rv = copyout(&i, *(caddr_t *)data, sizeof(i));
    264 			break;
    265 		}
    266 
    267 		*(int *)data = i;
    268 		break;
    269 
    270 	case DPT_TARGET_BUSY:
    271 		/*
    272 		 * XXX This is here to stop linux_machdepioctl() from
    273 		 * whining about an unknown ioctl.
    274 		 */
    275 		rv = EIO;
    276 		break;
    277 
    278 	case DPT_I2OUSRCMD:
    279 		if (sc->sc_nactive++ >= 2)
    280 			tsleep(&sc->sc_nactive, PRIBIO, "dptislp", 0);
    281 
    282 		if (linux)
    283 			rv = dpti_passthrough(sc, data, p);
    284 		else
    285 			rv = dpti_passthrough(sc, *(caddr_t *)data, p);
    286 
    287 		sc->sc_nactive--;
    288 		wakeup_one(&sc->sc_nactive);
    289 		break;
    290 
    291 	case DPT_I2ORESETCMD:
    292 		printf("%s: I2ORESETCMD not implemented\n",
    293 		    sc->sc_dv.dv_xname);
    294 		rv = EOPNOTSUPP;
    295 		break;
    296 
    297 	case DPT_I2ORESCANCMD:
    298 		if ((rv = lockmgr(&iop->sc_conflock, LK_EXCLUSIVE, NULL)) != 0)
    299 			break;
    300 		rv = iop_reconfigure(iop, 0);
    301 		lockmgr(&iop->sc_conflock, LK_RELEASE, NULL);
    302 		break;
    303 
    304 	default:
    305 		rv = ENOTTY;
    306 		break;
    307 	}
    308 
    309 	return (rv);
    310 }
    311 
    312 int
    313 dpti_blinkled(struct dpti_softc *sc)
    314 {
    315 	struct iop_softc *iop;
    316 	u_int v;
    317 
    318 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    319 
    320 	v = bus_space_read_1(iop->sc_iot, iop->sc_ioh, sc->sc_blinkled + 0);
    321 	if (v == 0xbc) {
    322 		v = bus_space_read_1(iop->sc_iot, iop->sc_ioh,
    323 		    sc->sc_blinkled + 1);
    324 		return (v);
    325 	}
    326 
    327 	return (-1);
    328 }
    329 
    330 int
    331 dpti_ctlrinfo(struct dpti_softc *sc, int size, caddr_t data)
    332 {
    333 	struct dpt_ctlrinfo info;
    334 	struct iop_softc *iop;
    335 	int rv, i;
    336 
    337 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    338 
    339 	memset(&info, 0, sizeof(info));
    340 
    341 	info.length = sizeof(info) - sizeof(u_int16_t);
    342 	info.drvrHBAnum = sc->sc_dv.dv_unit;
    343 	info.baseAddr = iop->sc_memaddr;
    344 	if ((i = dpti_blinkled(sc)) == -1)
    345 		i = 0;
    346 	info.blinkState = i;
    347 	info.pciBusNum = iop->sc_pcibus;
    348 	info.pciDeviceNum = iop->sc_pcidev;
    349 	info.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
    350 	info.Interrupt = 10;			/* XXX */
    351 
    352 	if (size > sizeof(*data)) {
    353 		memcpy(data, &info, min(sizeof(info), size));
    354 		rv = 0;
    355 	} else
    356 		rv = copyout(&info, *(caddr_t *)data, sizeof(info));
    357 
    358 	return (rv);
    359 }
    360 
    361 int
    362 dpti_sysinfo(struct dpti_softc *sc, int size, caddr_t data)
    363 {
    364 	struct dpt_sysinfo info;
    365 	int rv;
    366 #ifdef __i386__
    367 	int i, j;
    368 #endif
    369 
    370 	memset(&info, 0, sizeof(info));
    371 
    372 #ifdef __i386__
    373 	outb (0x70, 0x12);
    374 	i = inb(0x71);
    375 	j = i >> 4;
    376 	if (i == 0x0f) {
    377 		outb (0x70, 0x19);
    378 		j = inb (0x71);
    379 	}
    380 	info.drive0CMOS = j;
    381 
    382 	j = i & 0x0f;
    383 	if (i == 0x0f) {
    384 		outb (0x70, 0x1a);
    385 		j = inb (0x71);
    386 	}
    387 	info.drive1CMOS = j;
    388 	info.processorFamily = dpti_sig.dsProcessorFamily;
    389 
    390 	/*
    391 	 * Get the conventional memory size from CMOS.
    392 	 */
    393 	outb(0x70, 0x16);
    394 	j = inb(0x71);
    395 	j <<= 8;
    396 	outb(0x70, 0x15);
    397 	j |= inb(0x71);
    398 	info.conventionalMemSize = j;
    399 
    400 	/*
    401 	 * Get the extended memory size from CMOS.
    402 	 */
    403 	outb(0x70, 0x31);
    404 	j = inb(0x71);
    405 	j <<= 8;
    406 	outb(0x70, 0x30);
    407 	j |= inb(0x71);
    408 	info.extendedMemSize = j;
    409 
    410 	switch (cpu_class) {
    411 	case CPUCLASS_386:
    412 		info.processorType = PROC_386;
    413 		break;
    414 	case CPUCLASS_486:
    415 		info.processorType = PROC_486;
    416 		break;
    417 	case CPUCLASS_586:
    418 		info.processorType = PROC_PENTIUM;
    419 		break;
    420 	case CPUCLASS_686:
    421 	default:
    422 		info.processorType = PROC_SEXIUM;
    423 		break;
    424 	}
    425 
    426 	info.flags = SI_CMOS_Valid | SI_BusTypeValid |
    427 	    SI_MemorySizeValid | SI_NO_SmartROM;
    428 #else
    429 	info.flags = SI_BusTypeValid | SI_NO_SmartROM;
    430 #endif
    431 
    432 	info.busType = SI_PCI_BUS;
    433 
    434 	/*
    435 	 * Copy out the info structure to the user.
    436 	 */
    437 	if (size > sizeof(*data)) {
    438 		memcpy(data, &info, min(sizeof(info), size));
    439 		rv = 0;
    440 	} else
    441 		rv = copyout(&info, *(caddr_t *)data, sizeof(info));
    442 
    443 	return (rv);
    444 }
    445 
    446 int
    447 dpti_passthrough(struct dpti_softc *sc, caddr_t data, struct proc *proc)
    448 {
    449 	struct iop_softc *iop;
    450 	struct i2o_msg mh, *mf;
    451 	struct i2o_reply rh;
    452 	struct iop_msg *im;
    453 	struct dpti_ptbuf bufs[IOP_MAX_MSG_XFERS];
    454 	u_int32_t mbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    455 	u_int32_t rbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    456 	int rv, msgsize, repsize, sgoff, i, mapped, nbuf, nfrag, j, sz;
    457 	u_int32_t *p, *pmax;
    458 
    459 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    460 	im = NULL;
    461 
    462 	if ((rv = dpti_blinkled(sc)) != -1) {
    463 		if (rv != 0) {
    464 			printf("%s: adapter blinkled = 0x%02x\n",
    465 			    sc->sc_dv.dv_xname, rv);
    466 			return (EIO);
    467 		}
    468 	}
    469 
    470 	/*
    471 	 * Copy in the message frame header and determine the size of the
    472 	 * full message frame.
    473 	 */
    474 	if ((rv = copyin(data, &mh, sizeof(mh))) != 0) {
    475 		DPRINTF(("%s: message copyin failed\n",
    476 		    sc->sc_dv.dv_xname));
    477 		return (rv);
    478 	}
    479 
    480 	msgsize = (mh.msgflags >> 14) & ~3;
    481 	if (msgsize < sizeof(mh) || msgsize >= IOP_MAX_MSG_SIZE) {
    482 		DPRINTF(("%s: bad message frame size\n",
    483 		    sc->sc_dv.dv_xname));
    484 		return (EINVAL);
    485 	}
    486 
    487 	/*
    488 	 * Handle special commands.
    489 	 */
    490 	switch (mh.msgfunc >> 24) {
    491 	case I2O_EXEC_IOP_RESET:
    492 		printf("%s: I2O_EXEC_IOP_RESET not implemented\n",
    493 		    sc->sc_dv.dv_xname);
    494 		return (EOPNOTSUPP);
    495 
    496 	case I2O_EXEC_OUTBOUND_INIT:
    497 		printf("%s: I2O_EXEC_OUTBOUND_INIT not implemented\n",
    498 		    sc->sc_dv.dv_xname);
    499 		return (EOPNOTSUPP);
    500 
    501 	case I2O_EXEC_SYS_TAB_SET:
    502 		printf("%s: I2O_EXEC_SYS_TAB_SET not implemented\n",
    503 		    sc->sc_dv.dv_xname);
    504 		return (EOPNOTSUPP);
    505 
    506 	case I2O_EXEC_STATUS_GET:
    507 		if ((rv = iop_status_get(iop, 0)) == 0)
    508 			rv = copyout(&iop->sc_status, data + msgsize,
    509 			    sizeof(iop->sc_status));
    510 		return (rv);
    511 	}
    512 
    513 	/*
    514 	 * Copy in the full message frame.
    515 	 */
    516 	if ((rv = copyin(data, mbtmp, msgsize)) != 0) {
    517 		DPRINTF(("%s: full message copyin failed\n",
    518 		    sc->sc_dv.dv_xname));
    519 		return (rv);
    520 	}
    521 
    522 	/*
    523 	 * Determine the size of the reply frame, and copy it in.
    524 	 */
    525 	if ((rv = copyin(data + msgsize, &rh, sizeof(rh))) != 0) {
    526 		DPRINTF(("%s: reply copyin failed\n",
    527 		    sc->sc_dv.dv_xname));
    528 		return (rv);
    529 	}
    530 
    531 	repsize = (rh.msgflags >> 14) & ~3;
    532 	if (repsize < sizeof(rh) || repsize >= IOP_MAX_MSG_SIZE) {
    533 		DPRINTF(("%s: bad reply header size\n",
    534 		    sc->sc_dv.dv_xname));
    535 		return (EINVAL);
    536 	}
    537 
    538 	if ((rv = copyin(data + msgsize, rbtmp, repsize)) != 0) {
    539 		DPRINTF(("%s: reply too large\n", sc->sc_dv.dv_xname));
    540 		return (rv);
    541 	}
    542 
    543 	/*
    544 	 * If the message has a scatter gather list, it must be comprised of
    545 	 * simple elements.  If any one transfer contains multiple segments,
    546 	 * we allocate a temporary buffer for it; otherwise, the buffer will
    547 	 * be mapped directly.
    548 	 */
    549 	if ((sgoff = ((mh.msgflags >> 4) & 15)) != 0) {
    550 		if ((sgoff + 2) > (msgsize >> 2)) {
    551 			DPRINTF(("%s: invalid message size fields\n",
    552 			    sc->sc_dv.dv_xname));
    553 			return (EINVAL);
    554 		}
    555 
    556 		memset(bufs, 0, sizeof(bufs));
    557 
    558 		p = mbtmp + sgoff;
    559 		pmax = mbtmp + (msgsize >> 2) - 2;
    560 
    561 		for (nbuf = 0; nbuf < IOP_MAX_MSG_XFERS; nbuf++, p += 2) {
    562 			if (p > pmax) {
    563 				DPRINTF(("%s: invalid SGL (1)\n",
    564 				    sc->sc_dv.dv_xname));
    565 				goto bad;
    566 			}
    567 
    568 			if ((p[0] & 0x30000000) != I2O_SGL_SIMPLE) {
    569 				DPRINTF(("%s: invalid SGL (2)\n",
    570 				    sc->sc_dv.dv_xname));
    571 				goto bad;
    572 			}
    573 
    574 			bufs[nbuf].db_out = (p[0] & I2O_SGL_DATA_OUT) != 0;
    575 			bufs[nbuf].db_ptr = NULL;
    576 
    577 			if ((p[0] & I2O_SGL_END_BUFFER) != 0) {
    578 				if ((p[0] & 0x00ffffff) > IOP_MAX_XFER) {
    579 					DPRINTF(("%s: buffer too large\n",
    580 					    sc->sc_dv.dv_xname));
    581 					goto bad;
    582 				}
    583 
    584 				bufs[nbuf].db_ptr = (caddr_t)p[1];
    585 				bufs[nbuf].db_proc = proc;
    586 				bufs[nbuf].db_size = p[0] & 0x00ffffff;
    587 
    588 				if ((p[0] & I2O_SGL_END) != 0)
    589 					break;
    590 
    591 				continue;
    592 			}
    593 
    594 			/*
    595 			 * The buffer has multiple segments.  Determine the
    596 			 * total size.
    597 			 */
    598 			nfrag = 0;
    599 			sz = 0;
    600 			for (; p <= pmax; p += 2) {
    601 				if (nfrag == DPTI_MAX_SEGS) {
    602 					DPRINTF(("%s: too many segments\n",
    603 					    sc->sc_dv.dv_xname));
    604 					goto bad;
    605 				}
    606 
    607 				bufs[nbuf].db_frags[nfrag].iov_len =
    608 				    p[0] & 0x00ffffff;
    609 				bufs[nbuf].db_frags[nfrag].iov_base =
    610 				    (void *)p[1];
    611 
    612 				sz += p[0] & 0x00ffffff;
    613 				nfrag++;
    614 
    615 				if ((p[0] & I2O_SGL_END) != 0) {
    616 					if ((p[0] & I2O_SGL_END_BUFFER) == 0) {
    617 						DPRINTF((
    618 						    "%s: invalid SGL (3)\n",
    619 						    sc->sc_dv.dv_xname));
    620 						goto bad;
    621 					}
    622 					break;
    623 				}
    624 				if ((p[0] & I2O_SGL_END_BUFFER) != 0)
    625 					break;
    626 			}
    627 			bufs[nbuf].db_nfrag = nfrag;
    628 
    629 			if (p > pmax) {
    630 				DPRINTF(("%s: invalid SGL (4)\n",
    631 				    sc->sc_dv.dv_xname));
    632 				goto bad;
    633 			}
    634 
    635 			if (sz > IOP_MAX_XFER) {
    636 				DPRINTF(("%s: buffer too large\n",
    637 				    sc->sc_dv.dv_xname));
    638 				goto bad;
    639 			}
    640 
    641 			bufs[nbuf].db_size = sz;
    642 			bufs[nbuf].db_ptr = malloc(sz, M_DEVBUF, M_WAITOK);
    643 			if (bufs[nbuf].db_ptr == NULL) {
    644 				DPRINTF(("%s: allocation failure\n",
    645 				    sc->sc_dv.dv_xname));
    646 				rv = ENOMEM;
    647 				goto bad;
    648 			}
    649 
    650 			for (i = 0, sz = 0; i < bufs[nbuf].db_nfrag; i++) {
    651 				rv = copyin(bufs[nbuf].db_frags[i].iov_base,
    652 				    bufs[nbuf].db_ptr + sz,
    653 				    bufs[nbuf].db_frags[i].iov_len);
    654 				if (rv != 0) {
    655 					DPRINTF(("%s: frag copyin\n",
    656 					    sc->sc_dv.dv_xname));
    657 					goto bad;
    658 				}
    659 				sz += bufs[nbuf].db_frags[i].iov_len;
    660 			}
    661 
    662 			if ((p[0] & I2O_SGL_END) != 0)
    663 				break;
    664 		}
    665 
    666 		if (nbuf == IOP_MAX_MSG_XFERS) {
    667 			DPRINTF(("%s: too many transfers\n",
    668 			    sc->sc_dv.dv_xname));
    669 			goto bad;
    670 		}
    671 	} else
    672 		nbuf = -1;
    673 
    674 	/*
    675 	 * Allocate a wrapper, and adjust the message header fields to
    676 	 * indicate that no scatter-gather list is currently present.
    677 	 */
    678 	mapped = 0;
    679 
    680 	im = iop_msg_alloc(iop, IM_WAIT | IM_NOSTATUS);
    681 	im->im_rb = (struct i2o_reply *)rbtmp;
    682 	mf = (struct i2o_msg *)mbtmp;
    683 	mf->msgictx = IOP_ICTX;
    684 	mf->msgtctx = im->im_tctx;
    685 
    686 	if (sgoff != 0)
    687 		mf->msgflags = (mf->msgflags & 0xff0f) | (sgoff << 16);
    688 
    689 	/*
    690 	 * Map the data transfer(s).
    691 	 */
    692 	for (i = 0; i <= nbuf; i++) {
    693 		rv = iop_msg_map(iop, im, mbtmp, bufs[i].db_ptr,
    694 		    bufs[i].db_size, bufs[i].db_out, bufs[i].db_proc);
    695 		if (rv != 0) {
    696 			DPRINTF(("%s: msg_map failed, rv = %d\n",
    697 			    sc->sc_dv.dv_xname, rv));
    698 			goto bad;
    699 		}
    700 		mapped = 1;
    701 	}
    702 
    703 	/*
    704 	 * Start the command and sleep until it completes.
    705 	 */
    706 	if ((rv = iop_msg_post(iop, im, mbtmp, 5*60*1000)) != 0)
    707 		goto bad;
    708 
    709 	/*
    710 	 * Copy out the reply frame.
    711 	 */
    712 	if ((rv = copyout(rbtmp, data + msgsize, repsize)) != 0)
    713 		DPRINTF(("%s: reply copyout() failed\n",
    714 		    sc->sc_dv.dv_xname));
    715 
    716  bad:
    717 	/*
    718 	 * Free resources and return to the caller.
    719 	 */
    720 	if (im != NULL) {
    721 		if (mapped)
    722 			iop_msg_unmap(iop, im);
    723 		iop_msg_free(iop, im);
    724 	}
    725 
    726 	for (i = 0; i <= nbuf; i++) {
    727 		if (bufs[i].db_proc != NULL)
    728 			continue;
    729 
    730 		if (!bufs[i].db_out && rv == 0) {
    731 			for (j = 0, sz = 0; j < bufs[i].db_nfrag; j++) {
    732 				rv = copyout(bufs[i].db_ptr + sz,
    733 				    bufs[i].db_frags[j].iov_base,
    734 				    bufs[i].db_frags[j].iov_len);
    735 				if (rv != 0)
    736 					break;
    737 				sz += bufs[i].db_frags[j].iov_len;
    738 			}
    739 		}
    740 
    741 		if (bufs[i].db_ptr != NULL)
    742 			free(bufs[i].db_ptr, M_DEVBUF);
    743 	}
    744 
    745 	return (rv);
    746 }
    747