Home | History | Annotate | Line # | Download | only in i2o
dpti.c revision 1.2
      1  1.2  ad /*	$NetBSD: dpti.c,v 1.2 2001/09/27 18:43:37 ad Exp $	*/
      2  1.1  ad 
      3  1.1  ad /*-
      4  1.1  ad  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  1.1  ad  * All rights reserved.
      6  1.1  ad  *
      7  1.1  ad  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  ad  * by Andrew Doran.
      9  1.1  ad  *
     10  1.1  ad  * Redistribution and use in source and binary forms, with or without
     11  1.1  ad  * modification, are permitted provided that the following conditions
     12  1.1  ad  * are met:
     13  1.1  ad  * 1. Redistributions of source code must retain the above copyright
     14  1.1  ad  *    notice, this list of conditions and the following disclaimer.
     15  1.1  ad  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  ad  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  ad  *    documentation and/or other materials provided with the distribution.
     18  1.1  ad  * 3. All advertising materials mentioning features or use of this software
     19  1.1  ad  *    must display the following acknowledgement:
     20  1.1  ad  *        This product includes software developed by the NetBSD
     21  1.1  ad  *        Foundation, Inc. and its contributors.
     22  1.1  ad  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  ad  *    contributors may be used to endorse or promote products derived
     24  1.1  ad  *    from this software without specific prior written permission.
     25  1.1  ad  *
     26  1.1  ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  ad  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  ad  */
     38  1.1  ad 
     39  1.1  ad /*
     40  1.1  ad  * Copyright (c) 1996-2000 Distributed Processing Technology Corporation
     41  1.1  ad  * Copyright (c) 2000 Adaptec Corporation
     42  1.1  ad  * All rights reserved.
     43  1.1  ad  *
     44  1.1  ad  * TERMS AND CONDITIONS OF USE
     45  1.1  ad  *
     46  1.1  ad  * Redistribution and use in source form, with or without modification, are
     47  1.1  ad  * permitted provided that redistributions of source code must retain the
     48  1.1  ad  * above copyright notice, this list of conditions and the following disclaimer.
     49  1.1  ad  *
     50  1.1  ad  * This software is provided `as is' by Adaptec and any express or implied
     51  1.1  ad  * warranties, including, but not limited to, the implied warranties of
     52  1.1  ad  * merchantability and fitness for a particular purpose, are disclaimed. In no
     53  1.1  ad  * event shall Adaptec be liable for any direct, indirect, incidental, special,
     54  1.1  ad  * exemplary or consequential damages (including, but not limited to,
     55  1.1  ad  * procurement of substitute goods or services; loss of use, data, or profits;
     56  1.1  ad  * or business interruptions) however caused and on any theory of liability,
     57  1.1  ad  * whether in contract, strict liability, or tort (including negligence or
     58  1.1  ad  * otherwise) arising in any way out of the use of this driver software, even
     59  1.1  ad  * if advised of the possibility of such damage.
     60  1.1  ad  */
     61  1.1  ad 
     62  1.1  ad /*
     63  1.1  ad  * Adaptec/DPT I2O control interface.
     64  1.1  ad  */
     65  1.1  ad 
     66  1.1  ad #include "opt_i2o.h"
     67  1.1  ad 
     68  1.1  ad #include <sys/param.h>
     69  1.1  ad #include <sys/systm.h>
     70  1.1  ad #include <sys/kernel.h>
     71  1.1  ad #include <sys/device.h>
     72  1.1  ad #include <sys/queue.h>
     73  1.1  ad #include <sys/proc.h>
     74  1.1  ad #include <sys/endian.h>
     75  1.1  ad #include <sys/malloc.h>
     76  1.1  ad #include <sys/conf.h>
     77  1.1  ad #include <sys/ioctl.h>
     78  1.1  ad 
     79  1.2  ad #include <uvm/uvm_extern.h>
     80  1.2  ad 
     81  1.1  ad #include <machine/bus.h>
     82  1.2  ad #ifdef i386
     83  1.2  ad #include <machine/pio.h>
     84  1.2  ad #endif
     85  1.1  ad 
     86  1.1  ad #include <dev/i2o/i2o.h>
     87  1.1  ad #include <dev/i2o/i2odpt.h>
     88  1.1  ad #include <dev/i2o/iopio.h>
     89  1.1  ad #include <dev/i2o/iopvar.h>
     90  1.1  ad #include <dev/i2o/dptivar.h>
     91  1.1  ad 
     92  1.1  ad #ifdef I2ODEBUG
     93  1.1  ad #define	DPRINTF(x)		printf x
     94  1.1  ad #else
     95  1.1  ad #define	DPRINTF(x)
     96  1.1  ad #endif
     97  1.1  ad 
     98  1.1  ad static struct dpt_sig dpti_sig = {
     99  1.2  ad 	{ 'd', 'P', 't', 'S', 'i', 'G'},
    100  1.2  ad 	SIG_VERSION,
    101  1.2  ad #if defined(i386)
    102  1.2  ad 	PROC_INTEL,
    103  1.2  ad #elif defined(powerpc)
    104  1.2  ad 	PROC_POWERPC,
    105  1.2  ad #elif defined(alpha)
    106  1.2  ad 	PROC_ALPHA,
    107  1.2  ad #elif defined(mips)
    108  1.2  ad 	PROC_MIPS,
    109  1.2  ad #elif defined(sparc64)
    110  1.2  ad 	PROC_ULTRASPARC,
    111  1.2  ad #endif
    112  1.2  ad #if defined(i386)
    113  1.2  ad 	PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
    114  1.2  ad #else
    115  1.2  ad 	0,
    116  1.2  ad #endif
    117  1.2  ad 	FT_HBADRVR,
    118  1.2  ad 	0,
    119  1.2  ad 	OEM_DPT,
    120  1.2  ad 	OS_FREE_BSD,	/* XXX */
    121  1.2  ad 	CAP_ABOVE16MB,
    122  1.2  ad 	DEV_ALL,
    123  1.1  ad 	ADF_ALL_SC5,
    124  1.2  ad 	0,
    125  1.2  ad 	0,
    126  1.2  ad 	DPTI_VERSION,
    127  1.2  ad 	DPTI_REVISION,
    128  1.2  ad 	DPTI_SUBREVISION,
    129  1.2  ad 	DPTI_MONTH,
    130  1.2  ad 	DPTI_DAY,
    131  1.2  ad 	DPTI_YEAR,
    132  1.2  ad 	""		/* Will be filled later */
    133  1.1  ad };
    134  1.1  ad 
    135  1.1  ad void	dpti_attach(struct device *, struct device *, void *);
    136  1.1  ad int	dpti_blinkled(struct dpti_softc *);
    137  1.1  ad int	dpti_ctlrinfo(struct dpti_softc *, u_long, caddr_t);
    138  1.1  ad int	dpti_match(struct device *, struct cfdata *, void *);
    139  1.1  ad int	dpti_passthrough(struct dpti_softc *, caddr_t, struct proc *);
    140  1.1  ad int	dpti_sysinfo(struct dpti_softc *, u_long, caddr_t);
    141  1.1  ad 
    142  1.1  ad cdev_decl(dpti);
    143  1.1  ad 
    144  1.1  ad extern struct cfdriver dpti_cd;
    145  1.1  ad 
    146  1.1  ad struct cfattach dpti_ca = {
    147  1.1  ad 	sizeof(struct dpti_softc), dpti_match, dpti_attach
    148  1.1  ad };
    149  1.1  ad 
    150  1.1  ad int
    151  1.1  ad dpti_match(struct device *parent, struct cfdata *match, void *aux)
    152  1.1  ad {
    153  1.1  ad 	struct iop_attach_args *ia;
    154  1.1  ad 	struct iop_softc *iop;
    155  1.1  ad 
    156  1.1  ad 	ia = aux;
    157  1.1  ad 	iop = (struct iop_softc *)parent;
    158  1.1  ad 
    159  1.1  ad 	if (ia->ia_class != I2O_CLASS_ANY || ia->ia_tid != I2O_TID_IOP)
    160  1.1  ad 		return (0);
    161  1.1  ad 
    162  1.1  ad 	if (le16toh(iop->sc_status.orgid) != I2O_ORG_DPT)
    163  1.1  ad 		return (0);
    164  1.1  ad 
    165  1.1  ad 	return (1);
    166  1.1  ad }
    167  1.1  ad 
    168  1.1  ad void
    169  1.1  ad dpti_attach(struct device *parent, struct device *self, void *aux)
    170  1.1  ad {
    171  1.1  ad 	struct iop_softc *iop;
    172  1.1  ad 	struct dpti_softc *sc;
    173  1.1  ad 	struct {
    174  1.1  ad 		struct	i2o_param_op_results pr;
    175  1.1  ad 		struct	i2o_param_read_results prr;
    176  1.1  ad 		struct	i2o_dpt_param_exec_iop_buffers dib;
    177  1.1  ad 	} __attribute__ ((__packed__)) param;
    178  1.1  ad 	int rv;
    179  1.1  ad 
    180  1.1  ad 	sc = (struct dpti_softc *)self;
    181  1.1  ad 	iop = (struct iop_softc *)parent;
    182  1.1  ad 
    183  1.1  ad 	/*
    184  1.1  ad 	 * Tell the world what we are.  The description in the signature
    185  1.1  ad 	 * must be no more than 46 bytes long (see dptivar.h).
    186  1.1  ad 	 */
    187  1.2  ad 	printf(": DPT/Adaptec RAID management interface\n");
    188  1.1  ad 	sprintf(dpti_sig.dsDescription, "NetBSD %s I2O OSM", osrelease);
    189  1.1  ad 
    190  1.1  ad 	rv = iop_field_get_all(iop, I2O_TID_IOP,
    191  1.1  ad 	    I2O_DPT_PARAM_EXEC_IOP_BUFFERS, &param,
    192  1.1  ad 	    sizeof(param), NULL);
    193  1.1  ad 	if (rv != 0)
    194  1.1  ad 		return;
    195  1.1  ad 
    196  1.1  ad 	sc->sc_blinkled = le32toh(param.dib.serialoutputoff) + 8;
    197  1.1  ad }
    198  1.1  ad 
    199  1.1  ad int
    200  1.1  ad dptiopen(dev_t dev, int flag, int mode, struct proc *p)
    201  1.1  ad {
    202  1.1  ad 
    203  1.1  ad 	if (securelevel > 1)
    204  1.1  ad 		return (EPERM);
    205  1.1  ad 	if (device_lookup(&dpti_cd, minor(dev)) == NULL)
    206  1.1  ad 		return (ENXIO);
    207  1.1  ad 
    208  1.1  ad 	return (0);
    209  1.1  ad }
    210  1.1  ad 
    211  1.1  ad int
    212  1.1  ad dpticlose(dev_t dev, int flag, int mode, struct proc *p)
    213  1.1  ad {
    214  1.1  ad 
    215  1.1  ad 	return (0);
    216  1.1  ad }
    217  1.1  ad 
    218  1.1  ad int
    219  1.1  ad dptiioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    220  1.1  ad {
    221  1.1  ad 	struct iop_softc *iop;
    222  1.1  ad 	struct dpti_softc *sc;
    223  1.1  ad 	struct ioctl_pt *pt;
    224  1.2  ad 	int i, size, rv;
    225  1.1  ad 
    226  1.1  ad 	sc = device_lookup(&dpti_cd, minor(dev));
    227  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    228  1.1  ad 
    229  1.1  ad 	/*
    230  1.1  ad 	 * Currently, we only take ioctls passed down from the Linux
    231  1.1  ad 	 * emulation layer.
    232  1.1  ad 	 */
    233  1.1  ad 	if (cmd == PTIOCLINUX) {
    234  1.1  ad 		pt = (struct ioctl_pt *)data;
    235  1.1  ad 		cmd = pt->com;
    236  1.1  ad 		data = pt->data;
    237  1.1  ad 	} else
    238  1.1  ad 		return (ENOTTY);
    239  1.1  ad 
    240  1.1  ad 	size = IOCPARM_LEN(cmd);
    241  1.1  ad 
    242  1.1  ad 	switch (cmd & 0xffff) {
    243  1.1  ad 	case DPT_SIGNATURE:
    244  1.1  ad 		if (size > sizeof(dpti_sig))
    245  1.1  ad 			size = sizeof(dpti_sig);
    246  1.1  ad 		memcpy(data, &dpti_sig, size);
    247  1.1  ad 		return (0);
    248  1.1  ad 
    249  1.1  ad 	case DPT_CTRLINFO:
    250  1.1  ad 		return (dpti_ctlrinfo(sc, cmd, data));
    251  1.1  ad 
    252  1.1  ad 	case DPT_SYSINFO:
    253  1.1  ad 		return (dpti_sysinfo(sc, cmd, data));
    254  1.1  ad 
    255  1.1  ad 	case DPT_BLINKLED:
    256  1.1  ad 		if ((i = dpti_blinkled(sc)) == -1)
    257  1.1  ad 			i = 0;
    258  1.1  ad 
    259  1.1  ad 		if (size == 0)
    260  1.1  ad 			return (copyout(&i, *(caddr_t *)data, sizeof(i)));
    261  1.1  ad 
    262  1.1  ad 		*(int *)data = i;
    263  1.1  ad 		return (0);
    264  1.1  ad 
    265  1.1  ad 	case DPT_TARGET_BUSY:
    266  1.1  ad 		/*
    267  1.1  ad 		 * XXX This is here to stop linux_machdepioctl() from
    268  1.1  ad 		 * whining about an unknown ioctl.  Really, it should be
    269  1.1  ad 		 * implemented.
    270  1.1  ad 		 */
    271  1.1  ad 		return (EIO);
    272  1.1  ad 
    273  1.1  ad 	case DPT_I2OUSRCMD:
    274  1.2  ad 		if (sc->sc_nactive++ >= 2)
    275  1.2  ad 			tsleep(&sc->sc_nactive, PRIBIO, "dptislp", 0);
    276  1.2  ad 
    277  1.2  ad 		rv = dpti_passthrough(sc, data, p);
    278  1.2  ad 
    279  1.2  ad 		sc->sc_nactive--;
    280  1.2  ad 		wakeup_one(&sc->sc_nactive);
    281  1.2  ad 		return (rv);
    282  1.1  ad 
    283  1.1  ad 	case DPT_I2ORESETCMD:
    284  1.1  ad 		printf("%s: I2ORESETCMD not implemented\n",
    285  1.1  ad 		    sc->sc_dv.dv_xname);
    286  1.2  ad 		return (EOPNOTSUPP);
    287  1.1  ad 
    288  1.1  ad 	case DPT_I2ORESCANCMD:
    289  1.1  ad 		return (iop_reconfigure(iop, 0));
    290  1.1  ad 
    291  1.1  ad 	default:
    292  1.1  ad 		return (ENOTTY);
    293  1.1  ad 	}
    294  1.1  ad }
    295  1.1  ad 
    296  1.1  ad int
    297  1.1  ad dpti_blinkled(struct dpti_softc *sc)
    298  1.1  ad {
    299  1.1  ad 	struct iop_softc *iop;
    300  1.1  ad 	u_int v;
    301  1.1  ad 
    302  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    303  1.1  ad 
    304  1.1  ad 	v = bus_space_read_1(iop->sc_iot, iop->sc_ioh, sc->sc_blinkled + 0);
    305  1.1  ad 	if (v == 0xbc) {
    306  1.1  ad 		v = bus_space_read_1(iop->sc_iot, iop->sc_ioh,
    307  1.1  ad 		    sc->sc_blinkled + 1);
    308  1.1  ad 		return (v);
    309  1.1  ad 	}
    310  1.1  ad 
    311  1.1  ad 	return (-1);
    312  1.1  ad }
    313  1.1  ad 
    314  1.1  ad int
    315  1.1  ad dpti_ctlrinfo(struct dpti_softc *sc, u_long cmd, caddr_t data)
    316  1.1  ad {
    317  1.2  ad 	struct dpt_ctlrinfo info;
    318  1.1  ad 	struct iop_softc *iop;
    319  1.1  ad 	int rv, i;
    320  1.1  ad 
    321  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    322  1.1  ad 
    323  1.2  ad 	memset(&info, 0, sizeof(info));
    324  1.1  ad 
    325  1.2  ad 	info.length = sizeof(info) - sizeof(u_int16_t);
    326  1.2  ad 	info.drvrHBAnum = sc->sc_dv.dv_unit;
    327  1.2  ad 	info.baseAddr = iop->sc_memaddr;
    328  1.1  ad 	if ((i = dpti_blinkled(sc)) == -1)
    329  1.1  ad 		i = 0;
    330  1.2  ad 	info.blinkState = i;
    331  1.2  ad 	info.pciBusNum = iop->sc_pcibus;
    332  1.2  ad 	info.pciDeviceNum = iop->sc_pcidev;
    333  1.2  ad 	info.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
    334  1.2  ad 	info.Interrupt = 10;			/* XXX */
    335  1.1  ad 
    336  1.1  ad 	if (IOCPARM_LEN(cmd) > sizeof(*data)) {
    337  1.2  ad 		memcpy(data, &info, min(sizeof(info), IOCPARM_LEN(cmd)));
    338  1.1  ad 		rv = 0;
    339  1.1  ad 	} else
    340  1.2  ad 		rv = copyout(&info, *(caddr_t *)data, sizeof(info));
    341  1.1  ad 
    342  1.1  ad 	return (rv);
    343  1.1  ad }
    344  1.1  ad 
    345  1.1  ad int
    346  1.1  ad dpti_sysinfo(struct dpti_softc *sc, u_long cmd, caddr_t data)
    347  1.1  ad {
    348  1.2  ad 	struct dpt_sysinfo info;
    349  1.2  ad 	int rv;
    350  1.2  ad #ifdef i386
    351  1.2  ad 	int i, j;
    352  1.2  ad #endif
    353  1.2  ad 
    354  1.2  ad 	memset(&info, 0, sizeof(info));
    355  1.2  ad 
    356  1.2  ad #ifdef i386
    357  1.2  ad 	outb (0x70, 0x12);
    358  1.2  ad 	i = inb(0x71);
    359  1.2  ad 	j = i >> 4;
    360  1.2  ad 	if (i == 0x0f) {
    361  1.2  ad 		outb (0x70, 0x19);
    362  1.2  ad 		j = inb (0x71);
    363  1.2  ad 	}
    364  1.2  ad 	info.drive0CMOS = j;
    365  1.2  ad 
    366  1.2  ad 	j = i & 0x0f;
    367  1.2  ad 	if (i == 0x0f) {
    368  1.2  ad 		outb (0x70, 0x1a);
    369  1.2  ad 		j = inb (0x71);
    370  1.2  ad 	}
    371  1.2  ad 	info.drive1CMOS = j;
    372  1.2  ad 	info.processorFamily = dpti_sig.dsProcessorFamily;
    373  1.2  ad 
    374  1.2  ad 	/*
    375  1.2  ad 	 * Get the conventional memory size from CMOS.
    376  1.2  ad 	 */
    377  1.2  ad 	outb(0x70, 0x16);
    378  1.2  ad 	j = inb(0x71);
    379  1.2  ad 	j <<= 8;
    380  1.2  ad 	outb(0x70, 0x15);
    381  1.2  ad 	j |= inb(0x71);
    382  1.2  ad 	info.conventionalMemSize = j;
    383  1.2  ad 
    384  1.2  ad 	/*
    385  1.2  ad 	 * Get the extended memory size from CMOS.
    386  1.2  ad 	 */
    387  1.2  ad 	outb(0x70, 0x31);
    388  1.2  ad 	j = inb(0x71);
    389  1.2  ad 	j <<= 8;
    390  1.2  ad 	outb(0x70, 0x30);
    391  1.2  ad 	j |= inb(0x71);
    392  1.2  ad 	info.extendedMemSize = j;
    393  1.2  ad 
    394  1.2  ad 	switch (cpu_class) {
    395  1.2  ad 	case CPUCLASS_386:
    396  1.2  ad 		info.processorType = PROC_386;
    397  1.2  ad 		break;
    398  1.2  ad 	case CPUCLASS_486:
    399  1.2  ad 		info.processorType = PROC_486;
    400  1.2  ad 		break;
    401  1.2  ad 	case CPUCLASS_586:
    402  1.2  ad 		info.processorType = PROC_PENTIUM;
    403  1.2  ad 		break;
    404  1.2  ad 	case CPUCLASS_686:
    405  1.2  ad 	default:
    406  1.2  ad 		info.processorType = PROC_SEXIUM;
    407  1.2  ad 		break;
    408  1.2  ad 	}
    409  1.2  ad 
    410  1.2  ad 	info.flags = SI_CMOS_Valid | SI_BusTypeValid |
    411  1.2  ad 	    SI_MemorySizeValid | SI_NO_SmartROM;
    412  1.2  ad #else
    413  1.2  ad 	info.flags = SI_BusTypeValid | SI_NO_SmartROM;
    414  1.2  ad #endif
    415  1.2  ad 
    416  1.2  ad 	info.busType = SI_PCI_BUS;
    417  1.1  ad 
    418  1.1  ad 	/*
    419  1.2  ad 	 * Copy out the info structure to the user.
    420  1.1  ad 	 */
    421  1.2  ad 	if (IOCPARM_LEN(cmd) > sizeof(*data)) {
    422  1.2  ad 		memcpy(data, &info, min(sizeof(info), IOCPARM_LEN(cmd)));
    423  1.2  ad 		rv = 0;
    424  1.2  ad 	} else
    425  1.2  ad 		rv = copyout(&info, *(caddr_t *)data, sizeof(info));
    426  1.2  ad 
    427  1.2  ad 	return (rv);
    428  1.1  ad }
    429  1.1  ad 
    430  1.1  ad int
    431  1.1  ad dpti_passthrough(struct dpti_softc *sc, caddr_t data, struct proc *proc)
    432  1.1  ad {
    433  1.1  ad 	struct iop_softc *iop;
    434  1.1  ad 	struct i2o_msg mh, *mf;
    435  1.1  ad 	struct i2o_reply rh;
    436  1.1  ad 	struct iop_msg *im;
    437  1.1  ad 	struct dpti_ptbuf bufs[IOP_MAX_MSG_XFERS];
    438  1.1  ad 	u_int32_t mbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    439  1.1  ad 	u_int32_t rbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    440  1.2  ad 	int rv, msgsize, repsize, sgoff, i, mapped, nbuf, nfrag, j, sz;
    441  1.2  ad 	u_int32_t *p, *pmax, *pstart;
    442  1.1  ad 
    443  1.1  ad 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    444  1.2  ad 	im = NULL;
    445  1.1  ad 
    446  1.1  ad 	if ((rv = dpti_blinkled(sc)) != -1) {
    447  1.1  ad 		if (rv != 0) {
    448  1.1  ad 			printf("%s: adapter blinkled = 0x%02x\n",
    449  1.1  ad 			    sc->sc_dv.dv_xname, rv);
    450  1.1  ad 			return (EIO);
    451  1.1  ad 		}
    452  1.1  ad 	}
    453  1.1  ad 
    454  1.1  ad 	/*
    455  1.1  ad 	 * Copy in the message frame header and determine the size of the
    456  1.1  ad 	 * full message frame.
    457  1.1  ad 	 */
    458  1.1  ad 	if ((rv = copyin(data, &mh, sizeof(mh))) != 0) {
    459  1.1  ad 		DPRINTF(("%s: message copyin failed\n",
    460  1.1  ad 		    sc->sc_dv.dv_xname));
    461  1.1  ad 		return (rv);
    462  1.1  ad 	}
    463  1.1  ad 
    464  1.1  ad 	msgsize = (mh.msgflags >> 14) & ~3;
    465  1.1  ad 	if (msgsize < sizeof(mh) || msgsize >= IOP_MAX_MSG_SIZE) {
    466  1.1  ad 		DPRINTF(("%s: bad message frame size\n",
    467  1.1  ad 		    sc->sc_dv.dv_xname));
    468  1.1  ad 		return (EINVAL);
    469  1.1  ad 	}
    470  1.1  ad 
    471  1.1  ad 	/*
    472  1.1  ad 	 * Handle special commands.
    473  1.1  ad 	 */
    474  1.1  ad 	switch (mh.msgfunc >> 24) {
    475  1.1  ad 	case I2O_EXEC_IOP_RESET:
    476  1.2  ad 		printf("%s: I2O_EXEC_IOP_RESET not implemented\n",
    477  1.1  ad 		    sc->sc_dv.dv_xname);
    478  1.2  ad 		return (EOPNOTSUPP);
    479  1.1  ad 
    480  1.1  ad 	case I2O_EXEC_OUTBOUND_INIT:
    481  1.2  ad 		printf("%s: I2O_EXEC_OUTBOUND_INIT not implemented\n",
    482  1.1  ad 		    sc->sc_dv.dv_xname);
    483  1.2  ad 		return (EOPNOTSUPP);
    484  1.1  ad 
    485  1.1  ad 	case I2O_EXEC_SYS_TAB_SET:
    486  1.2  ad 		printf("%s: I2O_EXEC_SYS_TAB_SET not implemented\n",
    487  1.1  ad 		    sc->sc_dv.dv_xname);
    488  1.2  ad 		return (EOPNOTSUPP);
    489  1.1  ad 
    490  1.1  ad 	case I2O_EXEC_STATUS_GET:
    491  1.2  ad 		if ((rv = iop_status_get(iop, 0)) == 0)
    492  1.2  ad 			rv = copyout(&iop->sc_status, data + msgsize,
    493  1.2  ad 			    sizeof(iop->sc_status));
    494  1.1  ad 		return (rv);
    495  1.1  ad 	}
    496  1.1  ad 
    497  1.1  ad 	/*
    498  1.1  ad 	 * Copy in the full message frame.
    499  1.1  ad 	 */
    500  1.1  ad 	if ((rv = copyin(data, mbtmp, msgsize)) != 0) {
    501  1.1  ad 		DPRINTF(("%s: full message copyin failed\n",
    502  1.1  ad 		    sc->sc_dv.dv_xname));
    503  1.1  ad 		return (rv);
    504  1.1  ad 	}
    505  1.1  ad 
    506  1.1  ad 	/*
    507  1.1  ad 	 * Determine the size of the reply frame, and copy it in.
    508  1.1  ad 	 */
    509  1.1  ad 	if ((rv = copyin(data + msgsize, &rh, sizeof(rh))) != 0) {
    510  1.1  ad 		DPRINTF(("%s: reply copyin failed\n",
    511  1.1  ad 		    sc->sc_dv.dv_xname));
    512  1.1  ad 		return (rv);
    513  1.1  ad 	}
    514  1.1  ad 
    515  1.1  ad 	repsize = (rh.msgflags >> 14) & ~3;
    516  1.1  ad 	if (repsize < sizeof(rh) || repsize >= IOP_MAX_MSG_SIZE) {
    517  1.1  ad 		DPRINTF(("%s: bad reply header size\n",
    518  1.1  ad 		    sc->sc_dv.dv_xname));
    519  1.1  ad 		return (EINVAL);
    520  1.1  ad 	}
    521  1.1  ad 
    522  1.1  ad 	if ((rv = copyin(data + msgsize, rbtmp, repsize)) != 0) {
    523  1.1  ad 		DPRINTF(("%s: reply too large\n", sc->sc_dv.dv_xname));
    524  1.1  ad 		return (rv);
    525  1.1  ad 	}
    526  1.1  ad 
    527  1.1  ad 	/*
    528  1.1  ad 	 * If the message has a scatter gather list, it must be comprised of
    529  1.1  ad 	 * simple elements.  If any one transfer contains multiple segments,
    530  1.1  ad 	 * we allocate a temporary buffer for it; otherwise, the buffer will
    531  1.2  ad 	 * be mapped directly.
    532  1.1  ad 	 */
    533  1.1  ad 	if ((sgoff = ((mh.msgflags >> 4) & 15)) != 0) {
    534  1.1  ad 		if ((sgoff + 2) > (msgsize >> 2)) {
    535  1.1  ad 			DPRINTF(("%s: invalid message size fields\n",
    536  1.1  ad 			    sc->sc_dv.dv_xname));
    537  1.1  ad 			return (EINVAL);
    538  1.1  ad 		}
    539  1.2  ad 
    540  1.2  ad 		memset(bufs, 0, sizeof(bufs));
    541  1.1  ad 	} else
    542  1.1  ad 		nbuf = -1;
    543  1.1  ad 
    544  1.2  ad 	rv = EINVAL;
    545  1.2  ad 
    546  1.1  ad 	if (sgoff != 0) {
    547  1.1  ad 		p = mbtmp + sgoff;
    548  1.2  ad 		pmax = mbtmp + (msgsize >> 2) - 2;
    549  1.1  ad 
    550  1.2  ad 		for (nbuf = 0; nbuf < IOP_MAX_MSG_XFERS; nbuf++, p += 2) {
    551  1.2  ad 			if (p > pmax) {
    552  1.1  ad 				DPRINTF(("%s: invalid SGL (1)\n",
    553  1.1  ad 				    sc->sc_dv.dv_xname));
    554  1.2  ad 				goto bad;
    555  1.1  ad 			}
    556  1.1  ad 
    557  1.1  ad 			if ((p[0] & 0x30000000) != I2O_SGL_SIMPLE) {
    558  1.1  ad 				DPRINTF(("%s: invalid SGL (2)\n",
    559  1.1  ad 				    sc->sc_dv.dv_xname));
    560  1.2  ad 				goto bad;
    561  1.1  ad 			}
    562  1.1  ad 
    563  1.1  ad 			bufs[nbuf].db_out = (p[0] & I2O_SGL_DATA_OUT) != 0;
    564  1.2  ad 			bufs[nbuf].db_ptr = NULL;
    565  1.1  ad 
    566  1.1  ad 			if ((p[0] & I2O_SGL_END_BUFFER) != 0) {
    567  1.1  ad 				if ((p[0] & 0x00ffffff) > IOP_MAX_XFER) {
    568  1.1  ad 					DPRINTF(("%s: buffer too large\n",
    569  1.1  ad 					    sc->sc_dv.dv_xname));
    570  1.2  ad 					goto bad;
    571  1.1  ad 				}
    572  1.1  ad 
    573  1.1  ad 				bufs[nbuf].db_ptr = (caddr_t)p[1];
    574  1.1  ad 				bufs[nbuf].db_proc = proc;
    575  1.1  ad 				bufs[nbuf].db_size = p[0] & 0x00ffffff;
    576  1.1  ad 
    577  1.1  ad 				if ((p[0] & I2O_SGL_END) != 0)
    578  1.1  ad 					break;
    579  1.1  ad 
    580  1.1  ad 				continue;
    581  1.1  ad 			}
    582  1.1  ad 
    583  1.2  ad 			/*
    584  1.2  ad 			 * The buffer has multiple segments.  Determine the
    585  1.2  ad 			 * total size.
    586  1.2  ad 			 */
    587  1.2  ad 			nfrag = 0;
    588  1.2  ad 			sz = 0;
    589  1.2  ad 			for (pstart = p; p <= pmax; p += 2) {
    590  1.2  ad 				if (nfrag == DPTI_MAX_SEGS) {
    591  1.2  ad 					DPRINTF(("%s: too many segments\n",
    592  1.2  ad 					    sc->sc_dv.dv_xname));
    593  1.2  ad 					goto bad;
    594  1.2  ad 				}
    595  1.2  ad 
    596  1.2  ad 				bufs[nbuf].db_frags[nfrag].iov_len =
    597  1.2  ad 				    p[0] & 0x00ffffff;
    598  1.2  ad 				bufs[nbuf].db_frags[nfrag].iov_base =
    599  1.2  ad 				    (void *)p[1];
    600  1.2  ad 
    601  1.2  ad 				sz += p[0] & 0x00ffffff;
    602  1.2  ad 				nfrag++;
    603  1.2  ad 
    604  1.2  ad 				if ((p[0] & I2O_SGL_END) != 0) {
    605  1.2  ad 					if ((p[0] & I2O_SGL_END_BUFFER) == 0) {
    606  1.2  ad 						DPRINTF((
    607  1.2  ad 						    "%s: invalid SGL (3)\n",
    608  1.2  ad 						    sc->sc_dv.dv_xname));
    609  1.2  ad 						goto bad;
    610  1.2  ad 					}
    611  1.2  ad 					break;
    612  1.2  ad 				}
    613  1.2  ad 				if ((p[0] & I2O_SGL_END_BUFFER) != 0)
    614  1.2  ad 					break;
    615  1.2  ad 			}
    616  1.2  ad 			bufs[nbuf].db_nfrag = nfrag;
    617  1.2  ad 
    618  1.2  ad 			if (p > pmax) {
    619  1.2  ad 				DPRINTF(("%s: invalid SGL (4)\n",
    620  1.2  ad 				    sc->sc_dv.dv_xname));
    621  1.2  ad 				goto bad;
    622  1.2  ad 			}
    623  1.2  ad 
    624  1.2  ad 			if (sz > IOP_MAX_XFER) {
    625  1.2  ad 				DPRINTF(("%s: buffer too large\n",
    626  1.2  ad 				    sc->sc_dv.dv_xname));
    627  1.2  ad 				goto bad;
    628  1.2  ad 			}
    629  1.2  ad 
    630  1.2  ad 			bufs[nbuf].db_size = sz;
    631  1.2  ad 			bufs[nbuf].db_ptr = malloc(sz, M_DEVBUF, M_WAITOK);
    632  1.2  ad 			if (bufs[nbuf].db_ptr == NULL) {
    633  1.2  ad 				DPRINTF(("%s: allocation failure\n",
    634  1.2  ad 				    sc->sc_dv.dv_xname));
    635  1.2  ad 				rv = ENOMEM;
    636  1.2  ad 				goto bad;
    637  1.2  ad 			}
    638  1.2  ad 
    639  1.2  ad 			for (i = 0, sz = 0; i < bufs[nbuf].db_nfrag; i++) {
    640  1.2  ad 				rv = copyin(bufs[nbuf].db_frags[i].iov_base,
    641  1.2  ad 				    bufs[nbuf].db_ptr + sz,
    642  1.2  ad 				    bufs[nbuf].db_frags[i].iov_len);
    643  1.2  ad 				if (rv != 0) {
    644  1.2  ad 					DPRINTF(("%s: frag copyin\n",
    645  1.2  ad 					    sc->sc_dv.dv_xname));
    646  1.2  ad 					goto bad;
    647  1.2  ad 				}
    648  1.2  ad 				sz += bufs[nbuf].db_frags[i].iov_len;
    649  1.2  ad 			}
    650  1.2  ad 
    651  1.2  ad 			if ((p[0] & I2O_SGL_END) != 0)
    652  1.2  ad 				break;
    653  1.1  ad 		}
    654  1.1  ad 
    655  1.1  ad 		if (nbuf == IOP_MAX_MSG_XFERS) {
    656  1.1  ad 			DPRINTF(("%s: too many transfers\n",
    657  1.1  ad 			    sc->sc_dv.dv_xname));
    658  1.2  ad 			goto bad;
    659  1.1  ad 		}
    660  1.1  ad 	}
    661  1.1  ad 
    662  1.1  ad 	/*
    663  1.1  ad 	 * Allocate a wrapper, and adjust the message header fields to
    664  1.1  ad 	 * indicate that no scatter-gather list is currently present.
    665  1.1  ad 	 */
    666  1.2  ad 	mapped = 0;
    667  1.2  ad 
    668  1.1  ad 	im = iop_msg_alloc(iop, IM_WAIT | IM_NOSTATUS);
    669  1.1  ad 	im->im_rb = (struct i2o_reply *)rbtmp;
    670  1.1  ad 	mf = (struct i2o_msg *)mbtmp;
    671  1.1  ad 	mf->msgictx = IOP_ICTX;
    672  1.1  ad 	mf->msgtctx = im->im_tctx;
    673  1.1  ad 
    674  1.1  ad 	if (sgoff != 0)
    675  1.1  ad 		mf->msgflags = (mf->msgflags & 0xff0f) | (sgoff << 16);
    676  1.1  ad 
    677  1.1  ad 	/*
    678  1.1  ad 	 * Map the data transfer(s).
    679  1.1  ad 	 */
    680  1.1  ad 	for (i = 0; i <= nbuf; i++) {
    681  1.1  ad 		rv = iop_msg_map(iop, im, mbtmp, bufs[i].db_ptr,
    682  1.1  ad 		    bufs[i].db_size, bufs[i].db_out, bufs[i].db_proc);
    683  1.1  ad 		if (rv != 0) {
    684  1.2  ad 			DPRINTF(("%s: msg_map failed, rv = %d\n",
    685  1.2  ad 			    sc->sc_dv.dv_xname, rv));
    686  1.1  ad 			goto bad;
    687  1.1  ad 		}
    688  1.1  ad 		mapped = 1;
    689  1.1  ad 	}
    690  1.1  ad 
    691  1.1  ad 	/*
    692  1.1  ad 	 * Start the command and sleep until it completes.
    693  1.1  ad 	 */
    694  1.1  ad 	if ((rv = iop_msg_post(iop, im, mbtmp, 5*60*1000)) != 0)
    695  1.1  ad 		goto bad;
    696  1.1  ad 
    697  1.1  ad 	/*
    698  1.1  ad 	 * Copy out the reply frame.
    699  1.1  ad 	 */
    700  1.1  ad 	if ((rv = copyout(rbtmp, data + msgsize, repsize)) != 0)
    701  1.1  ad 		DPRINTF(("%s: reply copyout() failed\n",
    702  1.1  ad 		    sc->sc_dv.dv_xname));
    703  1.1  ad 
    704  1.1  ad  bad:
    705  1.2  ad 	/*
    706  1.2  ad 	 * Free resources and return to the caller.
    707  1.2  ad 	 */
    708  1.2  ad 	if (im != NULL) {
    709  1.2  ad 		if (mapped)
    710  1.2  ad 			iop_msg_unmap(iop, im);
    711  1.2  ad 		iop_msg_free(iop, im);
    712  1.2  ad 	}
    713  1.2  ad 
    714  1.2  ad 	for (i = 0; i <= nbuf; i++) {
    715  1.2  ad 		if (bufs[i].db_proc != NULL)
    716  1.2  ad 			continue;
    717  1.2  ad 
    718  1.2  ad 		if (!bufs[i].db_out && rv == 0) {
    719  1.2  ad 			for (j = 0, sz = 0; j < bufs[i].db_nfrag; j++) {
    720  1.2  ad 				rv = copyout(bufs[i].db_ptr + sz,
    721  1.2  ad 				    bufs[i].db_frags[j].iov_base,
    722  1.2  ad 				    bufs[i].db_frags[j].iov_len);
    723  1.2  ad 				if (rv != 0)
    724  1.2  ad 					break;
    725  1.2  ad 				sz += bufs[i].db_frags[j].iov_len;
    726  1.2  ad 			}
    727  1.2  ad 		}
    728  1.1  ad 
    729  1.2  ad 		if (bufs[i].db_ptr != NULL)
    730  1.2  ad 			free(bufs[i].db_ptr, M_DEVBUF);
    731  1.2  ad 	}
    732  1.1  ad 
    733  1.1  ad 	return (rv);
    734  1.1  ad }
    735