Home | History | Annotate | Line # | Download | only in i2o
dpti.c revision 1.21.4.1
      1  1.21.4.1    rpaulo /*	$NetBSD: dpti.c,v 1.21.4.1 2006/09/09 02:50:00 rpaulo 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.3     lukem 
     66       1.3     lukem #include <sys/cdefs.h>
     67  1.21.4.1    rpaulo __KERNEL_RCSID(0, "$NetBSD: dpti.c,v 1.21.4.1 2006/09/09 02:50:00 rpaulo Exp $");
     68       1.1        ad 
     69       1.1        ad #include "opt_i2o.h"
     70       1.1        ad 
     71       1.1        ad #include <sys/param.h>
     72       1.1        ad #include <sys/systm.h>
     73       1.1        ad #include <sys/kernel.h>
     74       1.1        ad #include <sys/device.h>
     75       1.1        ad #include <sys/queue.h>
     76       1.1        ad #include <sys/proc.h>
     77       1.1        ad #include <sys/endian.h>
     78       1.1        ad #include <sys/malloc.h>
     79       1.1        ad #include <sys/conf.h>
     80       1.1        ad #include <sys/ioctl.h>
     81       1.1        ad 
     82       1.2        ad #include <uvm/uvm_extern.h>
     83       1.2        ad 
     84       1.1        ad #include <machine/bus.h>
     85      1.11   thorpej #ifdef __i386__
     86       1.2        ad #include <machine/pio.h>
     87       1.2        ad #endif
     88       1.1        ad 
     89       1.1        ad #include <dev/i2o/i2o.h>
     90       1.1        ad #include <dev/i2o/i2odpt.h>
     91       1.1        ad #include <dev/i2o/iopio.h>
     92       1.1        ad #include <dev/i2o/iopvar.h>
     93       1.1        ad #include <dev/i2o/dptivar.h>
     94       1.1        ad 
     95       1.1        ad #ifdef I2ODEBUG
     96       1.1        ad #define	DPRINTF(x)		printf x
     97       1.1        ad #else
     98       1.1        ad #define	DPRINTF(x)
     99       1.1        ad #endif
    100       1.1        ad 
    101       1.1        ad static struct dpt_sig dpti_sig = {
    102       1.2        ad 	{ 'd', 'P', 't', 'S', 'i', 'G'},
    103       1.2        ad 	SIG_VERSION,
    104      1.11   thorpej #if defined(__i386__)
    105       1.2        ad 	PROC_INTEL,
    106      1.11   thorpej #elif defined(__powerpc__)
    107       1.2        ad 	PROC_POWERPC,
    108      1.11   thorpej #elif defined(__alpha__)
    109       1.2        ad 	PROC_ALPHA,
    110       1.4   thorpej #elif defined(__mips__)
    111       1.2        ad 	PROC_MIPS,
    112      1.11   thorpej #elif defined(__sparc64__)
    113       1.2        ad 	PROC_ULTRASPARC,
    114       1.2        ad #endif
    115      1.11   thorpej #if defined(__i386__)
    116       1.2        ad 	PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
    117       1.2        ad #else
    118       1.2        ad 	0,
    119       1.2        ad #endif
    120       1.2        ad 	FT_HBADRVR,
    121       1.2        ad 	0,
    122       1.2        ad 	OEM_DPT,
    123       1.2        ad 	OS_FREE_BSD,	/* XXX */
    124       1.2        ad 	CAP_ABOVE16MB,
    125       1.2        ad 	DEV_ALL,
    126       1.1        ad 	ADF_ALL_SC5,
    127       1.2        ad 	0,
    128       1.2        ad 	0,
    129       1.2        ad 	DPTI_VERSION,
    130       1.2        ad 	DPTI_REVISION,
    131       1.2        ad 	DPTI_SUBREVISION,
    132       1.2        ad 	DPTI_MONTH,
    133       1.2        ad 	DPTI_DAY,
    134       1.2        ad 	DPTI_YEAR,
    135       1.2        ad 	""		/* Will be filled later */
    136       1.1        ad };
    137       1.1        ad 
    138       1.1        ad void	dpti_attach(struct device *, struct device *, void *);
    139       1.1        ad int	dpti_blinkled(struct dpti_softc *);
    140      1.10        ad int	dpti_ctlrinfo(struct dpti_softc *, int, caddr_t);
    141       1.1        ad int	dpti_match(struct device *, struct cfdata *, void *);
    142       1.1        ad int	dpti_passthrough(struct dpti_softc *, caddr_t, struct proc *);
    143      1.10        ad int	dpti_sysinfo(struct dpti_softc *, int, caddr_t);
    144       1.1        ad 
    145       1.5   gehenna dev_type_open(dptiopen);
    146       1.5   gehenna dev_type_ioctl(dptiioctl);
    147       1.5   gehenna 
    148       1.5   gehenna const struct cdevsw dpti_cdevsw = {
    149       1.5   gehenna 	dptiopen, nullclose, noread, nowrite, dptiioctl,
    150  1.21.4.1    rpaulo 	nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
    151       1.5   gehenna };
    152       1.1        ad 
    153       1.1        ad extern struct cfdriver dpti_cd;
    154       1.1        ad 
    155       1.7   thorpej CFATTACH_DECL(dpti, sizeof(struct dpti_softc),
    156       1.8   thorpej     dpti_match, dpti_attach, NULL, NULL);
    157       1.1        ad 
    158       1.1        ad int
    159       1.1        ad dpti_match(struct device *parent, struct cfdata *match, void *aux)
    160       1.1        ad {
    161       1.1        ad 	struct iop_attach_args *ia;
    162       1.1        ad 	struct iop_softc *iop;
    163       1.1        ad 
    164       1.1        ad 	ia = aux;
    165       1.1        ad 	iop = (struct iop_softc *)parent;
    166       1.1        ad 
    167       1.1        ad 	if (ia->ia_class != I2O_CLASS_ANY || ia->ia_tid != I2O_TID_IOP)
    168       1.1        ad 		return (0);
    169       1.1        ad 
    170       1.1        ad 	if (le16toh(iop->sc_status.orgid) != I2O_ORG_DPT)
    171       1.1        ad 		return (0);
    172       1.1        ad 
    173       1.1        ad 	return (1);
    174       1.1        ad }
    175       1.1        ad 
    176       1.1        ad void
    177       1.1        ad dpti_attach(struct device *parent, struct device *self, void *aux)
    178       1.1        ad {
    179       1.1        ad 	struct iop_softc *iop;
    180       1.1        ad 	struct dpti_softc *sc;
    181       1.1        ad 	struct {
    182       1.1        ad 		struct	i2o_param_op_results pr;
    183       1.1        ad 		struct	i2o_param_read_results prr;
    184       1.1        ad 		struct	i2o_dpt_param_exec_iop_buffers dib;
    185       1.1        ad 	} __attribute__ ((__packed__)) param;
    186       1.1        ad 	int rv;
    187       1.1        ad 
    188  1.21.4.1    rpaulo 	sc = device_private(self);
    189  1.21.4.1    rpaulo 	iop = device_private(parent);
    190       1.1        ad 
    191       1.1        ad 	/*
    192       1.1        ad 	 * Tell the world what we are.  The description in the signature
    193       1.1        ad 	 * must be no more than 46 bytes long (see dptivar.h).
    194       1.1        ad 	 */
    195       1.2        ad 	printf(": DPT/Adaptec RAID management interface\n");
    196      1.18    itojun 	snprintf(dpti_sig.dsDescription, sizeof(dpti_sig.dsDescription),
    197      1.18    itojun 	    "NetBSD %s I2O OSM", osrelease);
    198       1.1        ad 
    199       1.1        ad 	rv = iop_field_get_all(iop, I2O_TID_IOP,
    200       1.1        ad 	    I2O_DPT_PARAM_EXEC_IOP_BUFFERS, &param,
    201       1.1        ad 	    sizeof(param), NULL);
    202       1.1        ad 	if (rv != 0)
    203       1.1        ad 		return;
    204       1.1        ad 
    205       1.1        ad 	sc->sc_blinkled = le32toh(param.dib.serialoutputoff) + 8;
    206       1.1        ad }
    207       1.1        ad 
    208       1.1        ad int
    209      1.21  christos dptiopen(dev_t dev, int flag, int mode, struct lwp *l)
    210       1.1        ad {
    211       1.1        ad 
    212       1.1        ad 	if (device_lookup(&dpti_cd, minor(dev)) == NULL)
    213       1.1        ad 		return (ENXIO);
    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.21  christos dptiioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    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.10        ad 	int i, size, rv, linux;
    225       1.1        ad 
    226       1.1        ad 	sc = device_lookup(&dpti_cd, minor(dev));
    227  1.21.4.1    rpaulo 	iop = (struct iop_softc *)device_parent(&sc->sc_dv);
    228      1.17        ad 	rv = 0;
    229       1.1        ad 
    230       1.1        ad 	if (cmd == PTIOCLINUX) {
    231       1.1        ad 		pt = (struct ioctl_pt *)data;
    232      1.10        ad 		size = IOCPARM_LEN(pt->com);
    233      1.10        ad 		cmd = pt->com & 0xffff;
    234       1.1        ad 		data = pt->data;
    235      1.10        ad 		linux = 1;
    236      1.10        ad 	} else {
    237      1.10        ad 		size = IOCPARM_LEN(cmd);
    238      1.10        ad 		cmd = cmd & 0xffff;
    239      1.10        ad 		linux = 0;
    240      1.10        ad 	}
    241       1.1        ad 
    242      1.10        ad 	switch (cmd) {
    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.17        ad 		break;
    248       1.1        ad 
    249       1.1        ad 	case DPT_CTRLINFO:
    250      1.17        ad 		rv = dpti_ctlrinfo(sc, size, data);
    251      1.17        ad 		break;
    252       1.1        ad 
    253       1.1        ad 	case DPT_SYSINFO:
    254      1.17        ad 		rv = dpti_sysinfo(sc, size, data);
    255      1.17        ad 		break;
    256       1.1        ad 
    257       1.1        ad 	case DPT_BLINKLED:
    258       1.1        ad 		if ((i = dpti_blinkled(sc)) == -1)
    259       1.1        ad 			i = 0;
    260       1.1        ad 
    261      1.17        ad 		if (size == 0) {
    262      1.17        ad 			rv = copyout(&i, *(caddr_t *)data, sizeof(i));
    263      1.17        ad 			break;
    264      1.17        ad 		}
    265       1.1        ad 
    266       1.1        ad 		*(int *)data = i;
    267      1.17        ad 		break;
    268       1.1        ad 
    269       1.1        ad 	case DPT_TARGET_BUSY:
    270       1.1        ad 		/*
    271       1.1        ad 		 * XXX This is here to stop linux_machdepioctl() from
    272      1.10        ad 		 * whining about an unknown ioctl.
    273       1.1        ad 		 */
    274      1.17        ad 		rv = EIO;
    275      1.17        ad 		break;
    276       1.1        ad 
    277       1.1        ad 	case DPT_I2OUSRCMD:
    278  1.21.4.1    rpaulo 		if (securelevel > 1) {
    279  1.21.4.1    rpaulo 			rv = EPERM;
    280  1.21.4.1    rpaulo 			break;
    281  1.21.4.1    rpaulo 		}
    282  1.21.4.1    rpaulo 
    283       1.2        ad 		if (sc->sc_nactive++ >= 2)
    284       1.2        ad 			tsleep(&sc->sc_nactive, PRIBIO, "dptislp", 0);
    285       1.2        ad 
    286      1.10        ad 		if (linux)
    287      1.21  christos 			rv = dpti_passthrough(sc, data, l->l_proc);
    288      1.10        ad 		else
    289      1.21  christos 			rv = dpti_passthrough(sc, *(caddr_t *)data, l->l_proc);
    290       1.2        ad 
    291       1.2        ad 		sc->sc_nactive--;
    292       1.2        ad 		wakeup_one(&sc->sc_nactive);
    293      1.17        ad 		break;
    294       1.1        ad 
    295       1.1        ad 	case DPT_I2ORESETCMD:
    296       1.1        ad 		printf("%s: I2ORESETCMD not implemented\n",
    297       1.1        ad 		    sc->sc_dv.dv_xname);
    298      1.17        ad 		rv = EOPNOTSUPP;
    299      1.17        ad 		break;
    300       1.1        ad 
    301       1.1        ad 	case DPT_I2ORESCANCMD:
    302      1.17        ad 		if ((rv = lockmgr(&iop->sc_conflock, LK_EXCLUSIVE, NULL)) != 0)
    303      1.17        ad 			break;
    304      1.17        ad 		rv = iop_reconfigure(iop, 0);
    305      1.17        ad 		lockmgr(&iop->sc_conflock, LK_RELEASE, NULL);
    306      1.17        ad 		break;
    307       1.1        ad 
    308       1.1        ad 	default:
    309      1.17        ad 		rv = ENOTTY;
    310      1.17        ad 		break;
    311       1.1        ad 	}
    312      1.17        ad 
    313      1.17        ad 	return (rv);
    314       1.1        ad }
    315       1.1        ad 
    316       1.1        ad int
    317       1.1        ad dpti_blinkled(struct dpti_softc *sc)
    318       1.1        ad {
    319       1.1        ad 	struct iop_softc *iop;
    320       1.1        ad 	u_int v;
    321       1.1        ad 
    322  1.21.4.1    rpaulo 	iop = (struct iop_softc *)device_parent(&sc->sc_dv);
    323       1.1        ad 
    324       1.1        ad 	v = bus_space_read_1(iop->sc_iot, iop->sc_ioh, sc->sc_blinkled + 0);
    325       1.1        ad 	if (v == 0xbc) {
    326       1.1        ad 		v = bus_space_read_1(iop->sc_iot, iop->sc_ioh,
    327       1.1        ad 		    sc->sc_blinkled + 1);
    328       1.1        ad 		return (v);
    329       1.1        ad 	}
    330       1.1        ad 
    331       1.1        ad 	return (-1);
    332       1.1        ad }
    333       1.1        ad 
    334       1.1        ad int
    335      1.10        ad dpti_ctlrinfo(struct dpti_softc *sc, int size, caddr_t data)
    336       1.1        ad {
    337       1.2        ad 	struct dpt_ctlrinfo info;
    338       1.1        ad 	struct iop_softc *iop;
    339       1.1        ad 	int rv, i;
    340       1.1        ad 
    341  1.21.4.1    rpaulo 	iop = (struct iop_softc *)device_parent(&sc->sc_dv);
    342       1.1        ad 
    343       1.2        ad 	memset(&info, 0, sizeof(info));
    344       1.1        ad 
    345       1.2        ad 	info.length = sizeof(info) - sizeof(u_int16_t);
    346  1.21.4.1    rpaulo 	info.drvrHBAnum = device_unit(&sc->sc_dv);
    347       1.2        ad 	info.baseAddr = iop->sc_memaddr;
    348       1.1        ad 	if ((i = dpti_blinkled(sc)) == -1)
    349       1.1        ad 		i = 0;
    350       1.2        ad 	info.blinkState = i;
    351       1.2        ad 	info.pciBusNum = iop->sc_pcibus;
    352       1.2        ad 	info.pciDeviceNum = iop->sc_pcidev;
    353       1.2        ad 	info.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
    354       1.2        ad 	info.Interrupt = 10;			/* XXX */
    355       1.1        ad 
    356      1.10        ad 	if (size > sizeof(*data)) {
    357      1.10        ad 		memcpy(data, &info, min(sizeof(info), size));
    358       1.1        ad 		rv = 0;
    359       1.1        ad 	} else
    360       1.2        ad 		rv = copyout(&info, *(caddr_t *)data, sizeof(info));
    361       1.1        ad 
    362       1.1        ad 	return (rv);
    363       1.1        ad }
    364       1.1        ad 
    365       1.1        ad int
    366      1.10        ad dpti_sysinfo(struct dpti_softc *sc, int size, caddr_t data)
    367       1.1        ad {
    368       1.2        ad 	struct dpt_sysinfo info;
    369       1.2        ad 	int rv;
    370      1.11   thorpej #ifdef __i386__
    371       1.2        ad 	int i, j;
    372       1.2        ad #endif
    373       1.2        ad 
    374       1.2        ad 	memset(&info, 0, sizeof(info));
    375       1.2        ad 
    376      1.11   thorpej #ifdef __i386__
    377       1.2        ad 	outb (0x70, 0x12);
    378       1.2        ad 	i = inb(0x71);
    379       1.2        ad 	j = i >> 4;
    380       1.2        ad 	if (i == 0x0f) {
    381       1.2        ad 		outb (0x70, 0x19);
    382       1.2        ad 		j = inb (0x71);
    383       1.2        ad 	}
    384       1.2        ad 	info.drive0CMOS = j;
    385       1.2        ad 
    386       1.2        ad 	j = i & 0x0f;
    387       1.2        ad 	if (i == 0x0f) {
    388       1.2        ad 		outb (0x70, 0x1a);
    389       1.2        ad 		j = inb (0x71);
    390       1.2        ad 	}
    391       1.2        ad 	info.drive1CMOS = j;
    392       1.2        ad 	info.processorFamily = dpti_sig.dsProcessorFamily;
    393       1.2        ad 
    394       1.2        ad 	/*
    395       1.2        ad 	 * Get the conventional memory size from CMOS.
    396       1.2        ad 	 */
    397       1.2        ad 	outb(0x70, 0x16);
    398       1.2        ad 	j = inb(0x71);
    399       1.2        ad 	j <<= 8;
    400       1.2        ad 	outb(0x70, 0x15);
    401       1.2        ad 	j |= inb(0x71);
    402       1.2        ad 	info.conventionalMemSize = j;
    403       1.2        ad 
    404       1.2        ad 	/*
    405       1.2        ad 	 * Get the extended memory size from CMOS.
    406       1.2        ad 	 */
    407       1.2        ad 	outb(0x70, 0x31);
    408       1.2        ad 	j = inb(0x71);
    409       1.2        ad 	j <<= 8;
    410       1.2        ad 	outb(0x70, 0x30);
    411       1.2        ad 	j |= inb(0x71);
    412       1.2        ad 	info.extendedMemSize = j;
    413       1.2        ad 
    414       1.2        ad 	switch (cpu_class) {
    415       1.2        ad 	case CPUCLASS_386:
    416       1.2        ad 		info.processorType = PROC_386;
    417       1.2        ad 		break;
    418       1.2        ad 	case CPUCLASS_486:
    419       1.2        ad 		info.processorType = PROC_486;
    420       1.2        ad 		break;
    421       1.2        ad 	case CPUCLASS_586:
    422       1.2        ad 		info.processorType = PROC_PENTIUM;
    423       1.2        ad 		break;
    424       1.2        ad 	case CPUCLASS_686:
    425       1.2        ad 	default:
    426       1.2        ad 		info.processorType = PROC_SEXIUM;
    427       1.2        ad 		break;
    428       1.2        ad 	}
    429       1.2        ad 
    430       1.2        ad 	info.flags = SI_CMOS_Valid | SI_BusTypeValid |
    431       1.2        ad 	    SI_MemorySizeValid | SI_NO_SmartROM;
    432       1.2        ad #else
    433       1.2        ad 	info.flags = SI_BusTypeValid | SI_NO_SmartROM;
    434       1.2        ad #endif
    435       1.2        ad 
    436       1.2        ad 	info.busType = SI_PCI_BUS;
    437       1.1        ad 
    438       1.1        ad 	/*
    439       1.2        ad 	 * Copy out the info structure to the user.
    440       1.1        ad 	 */
    441      1.10        ad 	if (size > sizeof(*data)) {
    442      1.10        ad 		memcpy(data, &info, min(sizeof(info), size));
    443       1.2        ad 		rv = 0;
    444       1.2        ad 	} else
    445       1.2        ad 		rv = copyout(&info, *(caddr_t *)data, sizeof(info));
    446       1.2        ad 
    447       1.2        ad 	return (rv);
    448       1.1        ad }
    449       1.1        ad 
    450       1.1        ad int
    451       1.1        ad dpti_passthrough(struct dpti_softc *sc, caddr_t data, struct proc *proc)
    452       1.1        ad {
    453       1.1        ad 	struct iop_softc *iop;
    454       1.1        ad 	struct i2o_msg mh, *mf;
    455       1.1        ad 	struct i2o_reply rh;
    456       1.1        ad 	struct iop_msg *im;
    457       1.1        ad 	struct dpti_ptbuf bufs[IOP_MAX_MSG_XFERS];
    458       1.1        ad 	u_int32_t mbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    459       1.1        ad 	u_int32_t rbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    460       1.2        ad 	int rv, msgsize, repsize, sgoff, i, mapped, nbuf, nfrag, j, sz;
    461      1.16    simonb 	u_int32_t *p, *pmax;
    462       1.1        ad 
    463  1.21.4.1    rpaulo 	iop = (struct iop_softc *)device_parent(&sc->sc_dv);
    464       1.2        ad 	im = NULL;
    465       1.1        ad 
    466       1.1        ad 	if ((rv = dpti_blinkled(sc)) != -1) {
    467       1.1        ad 		if (rv != 0) {
    468       1.1        ad 			printf("%s: adapter blinkled = 0x%02x\n",
    469       1.1        ad 			    sc->sc_dv.dv_xname, rv);
    470       1.1        ad 			return (EIO);
    471       1.1        ad 		}
    472       1.1        ad 	}
    473       1.1        ad 
    474       1.1        ad 	/*
    475       1.1        ad 	 * Copy in the message frame header and determine the size of the
    476       1.1        ad 	 * full message frame.
    477       1.1        ad 	 */
    478       1.1        ad 	if ((rv = copyin(data, &mh, sizeof(mh))) != 0) {
    479       1.1        ad 		DPRINTF(("%s: message copyin failed\n",
    480       1.1        ad 		    sc->sc_dv.dv_xname));
    481       1.1        ad 		return (rv);
    482       1.1        ad 	}
    483       1.1        ad 
    484       1.1        ad 	msgsize = (mh.msgflags >> 14) & ~3;
    485       1.1        ad 	if (msgsize < sizeof(mh) || msgsize >= IOP_MAX_MSG_SIZE) {
    486       1.1        ad 		DPRINTF(("%s: bad message frame size\n",
    487       1.1        ad 		    sc->sc_dv.dv_xname));
    488       1.1        ad 		return (EINVAL);
    489       1.1        ad 	}
    490       1.1        ad 
    491       1.1        ad 	/*
    492       1.1        ad 	 * Handle special commands.
    493       1.1        ad 	 */
    494       1.1        ad 	switch (mh.msgfunc >> 24) {
    495       1.1        ad 	case I2O_EXEC_IOP_RESET:
    496       1.2        ad 		printf("%s: I2O_EXEC_IOP_RESET not implemented\n",
    497       1.1        ad 		    sc->sc_dv.dv_xname);
    498       1.2        ad 		return (EOPNOTSUPP);
    499       1.1        ad 
    500       1.1        ad 	case I2O_EXEC_OUTBOUND_INIT:
    501       1.2        ad 		printf("%s: I2O_EXEC_OUTBOUND_INIT not implemented\n",
    502       1.1        ad 		    sc->sc_dv.dv_xname);
    503       1.2        ad 		return (EOPNOTSUPP);
    504       1.1        ad 
    505       1.1        ad 	case I2O_EXEC_SYS_TAB_SET:
    506       1.2        ad 		printf("%s: I2O_EXEC_SYS_TAB_SET not implemented\n",
    507       1.1        ad 		    sc->sc_dv.dv_xname);
    508       1.2        ad 		return (EOPNOTSUPP);
    509       1.1        ad 
    510       1.1        ad 	case I2O_EXEC_STATUS_GET:
    511       1.2        ad 		if ((rv = iop_status_get(iop, 0)) == 0)
    512       1.2        ad 			rv = copyout(&iop->sc_status, data + msgsize,
    513       1.2        ad 			    sizeof(iop->sc_status));
    514       1.1        ad 		return (rv);
    515       1.1        ad 	}
    516       1.1        ad 
    517       1.1        ad 	/*
    518       1.1        ad 	 * Copy in the full message frame.
    519       1.1        ad 	 */
    520       1.1        ad 	if ((rv = copyin(data, mbtmp, msgsize)) != 0) {
    521       1.1        ad 		DPRINTF(("%s: full message copyin failed\n",
    522       1.1        ad 		    sc->sc_dv.dv_xname));
    523       1.1        ad 		return (rv);
    524       1.1        ad 	}
    525       1.1        ad 
    526       1.1        ad 	/*
    527       1.1        ad 	 * Determine the size of the reply frame, and copy it in.
    528       1.1        ad 	 */
    529       1.1        ad 	if ((rv = copyin(data + msgsize, &rh, sizeof(rh))) != 0) {
    530       1.1        ad 		DPRINTF(("%s: reply copyin failed\n",
    531       1.1        ad 		    sc->sc_dv.dv_xname));
    532       1.1        ad 		return (rv);
    533       1.1        ad 	}
    534       1.1        ad 
    535       1.1        ad 	repsize = (rh.msgflags >> 14) & ~3;
    536       1.1        ad 	if (repsize < sizeof(rh) || repsize >= IOP_MAX_MSG_SIZE) {
    537       1.1        ad 		DPRINTF(("%s: bad reply header size\n",
    538       1.1        ad 		    sc->sc_dv.dv_xname));
    539       1.1        ad 		return (EINVAL);
    540       1.1        ad 	}
    541       1.1        ad 
    542       1.1        ad 	if ((rv = copyin(data + msgsize, rbtmp, repsize)) != 0) {
    543       1.1        ad 		DPRINTF(("%s: reply too large\n", sc->sc_dv.dv_xname));
    544       1.1        ad 		return (rv);
    545       1.1        ad 	}
    546       1.1        ad 
    547       1.1        ad 	/*
    548       1.1        ad 	 * If the message has a scatter gather list, it must be comprised of
    549       1.1        ad 	 * simple elements.  If any one transfer contains multiple segments,
    550       1.1        ad 	 * we allocate a temporary buffer for it; otherwise, the buffer will
    551       1.2        ad 	 * be mapped directly.
    552       1.1        ad 	 */
    553      1.20     fredb 	mapped = 0;
    554       1.1        ad 	if ((sgoff = ((mh.msgflags >> 4) & 15)) != 0) {
    555       1.1        ad 		if ((sgoff + 2) > (msgsize >> 2)) {
    556       1.1        ad 			DPRINTF(("%s: invalid message size fields\n",
    557       1.1        ad 			    sc->sc_dv.dv_xname));
    558       1.1        ad 			return (EINVAL);
    559       1.1        ad 		}
    560       1.2        ad 
    561       1.2        ad 		memset(bufs, 0, sizeof(bufs));
    562       1.2        ad 
    563       1.1        ad 		p = mbtmp + sgoff;
    564       1.2        ad 		pmax = mbtmp + (msgsize >> 2) - 2;
    565       1.1        ad 
    566       1.2        ad 		for (nbuf = 0; nbuf < IOP_MAX_MSG_XFERS; nbuf++, p += 2) {
    567       1.2        ad 			if (p > pmax) {
    568       1.1        ad 				DPRINTF(("%s: invalid SGL (1)\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 			if ((p[0] & 0x30000000) != I2O_SGL_SIMPLE) {
    574       1.1        ad 				DPRINTF(("%s: invalid SGL (2)\n",
    575       1.1        ad 				    sc->sc_dv.dv_xname));
    576       1.2        ad 				goto bad;
    577       1.1        ad 			}
    578       1.1        ad 
    579       1.1        ad 			bufs[nbuf].db_out = (p[0] & I2O_SGL_DATA_OUT) != 0;
    580       1.2        ad 			bufs[nbuf].db_ptr = NULL;
    581       1.1        ad 
    582       1.1        ad 			if ((p[0] & I2O_SGL_END_BUFFER) != 0) {
    583       1.1        ad 				if ((p[0] & 0x00ffffff) > IOP_MAX_XFER) {
    584       1.1        ad 					DPRINTF(("%s: buffer too large\n",
    585       1.1        ad 					    sc->sc_dv.dv_xname));
    586       1.2        ad 					goto bad;
    587       1.1        ad 				}
    588       1.1        ad 
    589       1.1        ad 				bufs[nbuf].db_ptr = (caddr_t)p[1];
    590       1.1        ad 				bufs[nbuf].db_proc = proc;
    591       1.1        ad 				bufs[nbuf].db_size = p[0] & 0x00ffffff;
    592       1.1        ad 
    593       1.1        ad 				if ((p[0] & I2O_SGL_END) != 0)
    594       1.1        ad 					break;
    595       1.1        ad 
    596       1.1        ad 				continue;
    597       1.1        ad 			}
    598       1.1        ad 
    599       1.2        ad 			/*
    600       1.2        ad 			 * The buffer has multiple segments.  Determine the
    601       1.2        ad 			 * total size.
    602       1.2        ad 			 */
    603       1.2        ad 			nfrag = 0;
    604       1.2        ad 			sz = 0;
    605      1.16    simonb 			for (; p <= pmax; p += 2) {
    606       1.2        ad 				if (nfrag == DPTI_MAX_SEGS) {
    607       1.2        ad 					DPRINTF(("%s: too many segments\n",
    608       1.2        ad 					    sc->sc_dv.dv_xname));
    609       1.2        ad 					goto bad;
    610       1.2        ad 				}
    611       1.2        ad 
    612       1.2        ad 				bufs[nbuf].db_frags[nfrag].iov_len =
    613       1.2        ad 				    p[0] & 0x00ffffff;
    614      1.19     perry 				bufs[nbuf].db_frags[nfrag].iov_base =
    615       1.2        ad 				    (void *)p[1];
    616       1.2        ad 
    617       1.2        ad 				sz += p[0] & 0x00ffffff;
    618       1.2        ad 				nfrag++;
    619       1.2        ad 
    620       1.2        ad 				if ((p[0] & I2O_SGL_END) != 0) {
    621       1.2        ad 					if ((p[0] & I2O_SGL_END_BUFFER) == 0) {
    622       1.2        ad 						DPRINTF((
    623       1.2        ad 						    "%s: invalid SGL (3)\n",
    624       1.2        ad 						    sc->sc_dv.dv_xname));
    625       1.2        ad 						goto bad;
    626       1.2        ad 					}
    627       1.2        ad 					break;
    628       1.2        ad 				}
    629       1.2        ad 				if ((p[0] & I2O_SGL_END_BUFFER) != 0)
    630       1.2        ad 					break;
    631       1.2        ad 			}
    632       1.2        ad 			bufs[nbuf].db_nfrag = nfrag;
    633       1.2        ad 
    634       1.2        ad 			if (p > pmax) {
    635       1.2        ad 				DPRINTF(("%s: invalid SGL (4)\n",
    636       1.2        ad 				    sc->sc_dv.dv_xname));
    637       1.2        ad 				goto bad;
    638       1.2        ad 			}
    639       1.2        ad 
    640       1.2        ad 			if (sz > IOP_MAX_XFER) {
    641       1.2        ad 				DPRINTF(("%s: buffer too large\n",
    642       1.2        ad 				    sc->sc_dv.dv_xname));
    643       1.2        ad 				goto bad;
    644       1.2        ad 			}
    645       1.2        ad 
    646       1.2        ad 			bufs[nbuf].db_size = sz;
    647       1.2        ad 			bufs[nbuf].db_ptr = malloc(sz, M_DEVBUF, M_WAITOK);
    648       1.2        ad 			if (bufs[nbuf].db_ptr == NULL) {
    649       1.2        ad 				DPRINTF(("%s: allocation failure\n",
    650       1.2        ad 				    sc->sc_dv.dv_xname));
    651       1.2        ad 				rv = ENOMEM;
    652       1.2        ad 				goto bad;
    653       1.2        ad 			}
    654       1.2        ad 
    655       1.2        ad 			for (i = 0, sz = 0; i < bufs[nbuf].db_nfrag; i++) {
    656       1.2        ad 				rv = copyin(bufs[nbuf].db_frags[i].iov_base,
    657       1.2        ad 				    bufs[nbuf].db_ptr + sz,
    658       1.2        ad 				    bufs[nbuf].db_frags[i].iov_len);
    659       1.2        ad 				if (rv != 0) {
    660       1.2        ad 					DPRINTF(("%s: frag copyin\n",
    661       1.2        ad 					    sc->sc_dv.dv_xname));
    662       1.2        ad 					goto bad;
    663       1.2        ad 				}
    664       1.2        ad 				sz += bufs[nbuf].db_frags[i].iov_len;
    665       1.2        ad 			}
    666       1.2        ad 
    667       1.2        ad 			if ((p[0] & I2O_SGL_END) != 0)
    668       1.2        ad 				break;
    669       1.1        ad 		}
    670       1.1        ad 
    671       1.1        ad 		if (nbuf == IOP_MAX_MSG_XFERS) {
    672       1.1        ad 			DPRINTF(("%s: too many transfers\n",
    673       1.1        ad 			    sc->sc_dv.dv_xname));
    674       1.2        ad 			goto bad;
    675       1.1        ad 		}
    676      1.15   mycroft 	} else
    677      1.15   mycroft 		nbuf = -1;
    678       1.1        ad 
    679       1.1        ad 	/*
    680       1.1        ad 	 * Allocate a wrapper, and adjust the message header fields to
    681       1.1        ad 	 * indicate that no scatter-gather list is currently present.
    682       1.1        ad 	 */
    683       1.2        ad 
    684       1.1        ad 	im = iop_msg_alloc(iop, IM_WAIT | IM_NOSTATUS);
    685       1.1        ad 	im->im_rb = (struct i2o_reply *)rbtmp;
    686       1.1        ad 	mf = (struct i2o_msg *)mbtmp;
    687       1.1        ad 	mf->msgictx = IOP_ICTX;
    688       1.1        ad 	mf->msgtctx = im->im_tctx;
    689       1.1        ad 
    690       1.1        ad 	if (sgoff != 0)
    691       1.1        ad 		mf->msgflags = (mf->msgflags & 0xff0f) | (sgoff << 16);
    692       1.1        ad 
    693       1.1        ad 	/*
    694       1.1        ad 	 * Map the data transfer(s).
    695       1.1        ad 	 */
    696       1.1        ad 	for (i = 0; i <= nbuf; i++) {
    697       1.1        ad 		rv = iop_msg_map(iop, im, mbtmp, bufs[i].db_ptr,
    698       1.1        ad 		    bufs[i].db_size, bufs[i].db_out, bufs[i].db_proc);
    699       1.1        ad 		if (rv != 0) {
    700       1.2        ad 			DPRINTF(("%s: msg_map failed, rv = %d\n",
    701       1.2        ad 			    sc->sc_dv.dv_xname, rv));
    702       1.1        ad 			goto bad;
    703       1.1        ad 		}
    704       1.1        ad 		mapped = 1;
    705       1.1        ad 	}
    706       1.1        ad 
    707       1.1        ad 	/*
    708       1.1        ad 	 * Start the command and sleep until it completes.
    709       1.1        ad 	 */
    710       1.1        ad 	if ((rv = iop_msg_post(iop, im, mbtmp, 5*60*1000)) != 0)
    711       1.1        ad 		goto bad;
    712       1.1        ad 
    713       1.1        ad 	/*
    714       1.1        ad 	 * Copy out the reply frame.
    715       1.1        ad 	 */
    716       1.1        ad 	if ((rv = copyout(rbtmp, data + msgsize, repsize)) != 0)
    717       1.1        ad 		DPRINTF(("%s: reply copyout() failed\n",
    718       1.1        ad 		    sc->sc_dv.dv_xname));
    719       1.1        ad 
    720       1.1        ad  bad:
    721       1.2        ad 	/*
    722       1.2        ad 	 * Free resources and return to the caller.
    723       1.2        ad 	 */
    724       1.2        ad 	if (im != NULL) {
    725       1.2        ad 		if (mapped)
    726       1.2        ad 			iop_msg_unmap(iop, im);
    727       1.2        ad 		iop_msg_free(iop, im);
    728       1.2        ad 	}
    729       1.2        ad 
    730       1.2        ad 	for (i = 0; i <= nbuf; i++) {
    731       1.2        ad 		if (bufs[i].db_proc != NULL)
    732       1.2        ad 			continue;
    733       1.2        ad 
    734       1.2        ad 		if (!bufs[i].db_out && rv == 0) {
    735       1.2        ad 			for (j = 0, sz = 0; j < bufs[i].db_nfrag; j++) {
    736       1.2        ad 				rv = copyout(bufs[i].db_ptr + sz,
    737       1.2        ad 				    bufs[i].db_frags[j].iov_base,
    738       1.2        ad 				    bufs[i].db_frags[j].iov_len);
    739       1.2        ad 				if (rv != 0)
    740       1.2        ad 					break;
    741       1.2        ad 				sz += bufs[i].db_frags[j].iov_len;
    742       1.2        ad 			}
    743       1.2        ad 		}
    744       1.1        ad 
    745       1.2        ad 		if (bufs[i].db_ptr != NULL)
    746       1.2        ad 			free(bufs[i].db_ptr, M_DEVBUF);
    747       1.2        ad 	}
    748       1.1        ad 
    749       1.1        ad 	return (rv);
    750       1.1        ad }
    751