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