Home | History | Annotate | Line # | Download | only in i2o
dpti.c revision 1.1
      1 /*	$NetBSD: dpti.c,v 1.1 2001/09/21 23:44:20 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Copyright (c) 1996-2000 Distributed Processing Technology Corporation
     41  * Copyright (c) 2000 Adaptec Corporation
     42  * All rights reserved.
     43  *
     44  * TERMS AND CONDITIONS OF USE
     45  *
     46  * Redistribution and use in source form, with or without modification, are
     47  * permitted provided that redistributions of source code must retain the
     48  * above copyright notice, this list of conditions and the following disclaimer.
     49  *
     50  * This software is provided `as is' by Adaptec and any express or implied
     51  * warranties, including, but not limited to, the implied warranties of
     52  * merchantability and fitness for a particular purpose, are disclaimed. In no
     53  * event shall Adaptec be liable for any direct, indirect, incidental, special,
     54  * exemplary or consequential damages (including, but not limited to,
     55  * procurement of substitute goods or services; loss of use, data, or profits;
     56  * or business interruptions) however caused and on any theory of liability,
     57  * whether in contract, strict liability, or tort (including negligence or
     58  * otherwise) arising in any way out of the use of this driver software, even
     59  * if advised of the possibility of such damage.
     60  */
     61 
     62 /*
     63  * Adaptec/DPT I2O control interface.
     64  */
     65 
     66 #include "opt_i2o.h"
     67 
     68 #include <sys/param.h>
     69 #include <sys/systm.h>
     70 #include <sys/kernel.h>
     71 #include <sys/device.h>
     72 #include <sys/queue.h>
     73 #include <sys/proc.h>
     74 #include <sys/endian.h>
     75 #include <sys/malloc.h>
     76 #include <sys/conf.h>
     77 #include <sys/ioctl.h>
     78 
     79 #include <machine/bus.h>
     80 
     81 #include <dev/i2o/i2o.h>
     82 #include <dev/i2o/i2odpt.h>
     83 #include <dev/i2o/iopio.h>
     84 #include <dev/i2o/iopvar.h>
     85 #include <dev/i2o/dptivar.h>
     86 
     87 #include <compat/linux/common/linux_types.h>
     88 #include <compat/linux/common/linux_ioctl.h>
     89 
     90 #ifdef I2ODEBUG
     91 #define	DPRINTF(x)		printf x
     92 #else
     93 #define	DPRINTF(x)
     94 #endif
     95 
     96 static struct dpt_sig dpti_sig = {
     97 	{ 'd', 'P', 't', 'S', 'i', 'G'}, SIG_VERSION, PROC_INTEL,
     98 	PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM, FT_HBADRVR, 0,
     99 	OEM_DPT, OS_FREE_BSD, CAP_ABOVE16MB, DEV_ALL,
    100 	ADF_ALL_SC5,
    101 	0, 0, DPTI_VERSION, DPTI_REVISION, DPTI_SUBREVISION,
    102 	DPTI_MONTH, DPTI_DAY, DPTI_YEAR,
    103 	""
    104 };
    105 
    106 void	dpti_attach(struct device *, struct device *, void *);
    107 int	dpti_blinkled(struct dpti_softc *);
    108 int	dpti_ctlrinfo(struct dpti_softc *, u_long, caddr_t);
    109 int	dpti_match(struct device *, struct cfdata *, void *);
    110 int	dpti_passthrough(struct dpti_softc *, caddr_t, struct proc *);
    111 int	dpti_sysinfo(struct dpti_softc *, u_long, caddr_t);
    112 
    113 cdev_decl(dpti);
    114 
    115 extern struct cfdriver dpti_cd;
    116 
    117 struct cfattach dpti_ca = {
    118 	sizeof(struct dpti_softc), dpti_match, dpti_attach
    119 };
    120 
    121 int
    122 dpti_match(struct device *parent, struct cfdata *match, void *aux)
    123 {
    124 	struct iop_attach_args *ia;
    125 	struct iop_softc *iop;
    126 
    127 	ia = aux;
    128 	iop = (struct iop_softc *)parent;
    129 
    130 	if (ia->ia_class != I2O_CLASS_ANY || ia->ia_tid != I2O_TID_IOP)
    131 		return (0);
    132 
    133 	if (le16toh(iop->sc_status.orgid) != I2O_ORG_DPT)
    134 		return (0);
    135 
    136 	return (1);
    137 }
    138 
    139 void
    140 dpti_attach(struct device *parent, struct device *self, void *aux)
    141 {
    142 	struct iop_softc *iop;
    143 	struct dpti_softc *sc;
    144 	struct {
    145 		struct	i2o_param_op_results pr;
    146 		struct	i2o_param_read_results prr;
    147 		struct	i2o_dpt_param_exec_iop_buffers dib;
    148 	} __attribute__ ((__packed__)) param;
    149 	int rv;
    150 
    151 	sc = (struct dpti_softc *)self;
    152 	iop = (struct iop_softc *)parent;
    153 
    154 	/*
    155 	 * Tell the world what we are.  The description in the signature
    156 	 * must be no more than 46 bytes long (see dptivar.h).
    157 	 */
    158 	printf(": DPT/Adaptec SCSI RAID management interface\n");
    159 	sprintf(dpti_sig.dsDescription, "NetBSD %s I2O OSM", osrelease);
    160 
    161 	rv = iop_field_get_all(iop, I2O_TID_IOP,
    162 	    I2O_DPT_PARAM_EXEC_IOP_BUFFERS, &param,
    163 	    sizeof(param), NULL);
    164 	if (rv != 0)
    165 		return;
    166 
    167 	sc->sc_blinkled = le32toh(param.dib.serialoutputoff) + 8;
    168 }
    169 
    170 int
    171 dptiopen(dev_t dev, int flag, int mode, struct proc *p)
    172 {
    173 
    174 	if (securelevel > 1)
    175 		return (EPERM);
    176 	if (device_lookup(&dpti_cd, minor(dev)) == NULL)
    177 		return (ENXIO);
    178 
    179 	return (0);
    180 }
    181 
    182 int
    183 dpticlose(dev_t dev, int flag, int mode, struct proc *p)
    184 {
    185 
    186 	return (0);
    187 }
    188 
    189 int
    190 dptiioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    191 {
    192 	struct iop_softc *iop;
    193 	struct dpti_softc *sc;
    194 	struct ioctl_pt *pt;
    195 	int i, size;
    196 
    197 	sc = device_lookup(&dpti_cd, minor(dev));
    198 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    199 
    200 	/*
    201 	 * Currently, we only take ioctls passed down from the Linux
    202 	 * emulation layer.
    203 	 */
    204 	if (cmd == PTIOCLINUX) {
    205 		pt = (struct ioctl_pt *)data;
    206 		cmd = pt->com;
    207 		data = pt->data;
    208 	} else
    209 		return (ENOTTY);
    210 
    211 	size = IOCPARM_LEN(cmd);
    212 
    213 	switch (cmd & 0xffff) {
    214 	case DPT_SIGNATURE:
    215 		if (size > sizeof(dpti_sig))
    216 			size = sizeof(dpti_sig);
    217 		memcpy(data, &dpti_sig, size);
    218 		return (0);
    219 
    220 	case DPT_CTRLINFO:
    221 		return (dpti_ctlrinfo(sc, cmd, data));
    222 
    223 	case DPT_SYSINFO:
    224 		return (dpti_sysinfo(sc, cmd, data));
    225 
    226 	case DPT_BLINKLED:
    227 		if ((i = dpti_blinkled(sc)) == -1)
    228 			i = 0;
    229 
    230 		if (size == 0)
    231 			return (copyout(&i, *(caddr_t *)data, sizeof(i)));
    232 
    233 		*(int *)data = i;
    234 		return (0);
    235 
    236 	case DPT_TARGET_BUSY:
    237 		/*
    238 		 * XXX This is here to stop linux_machdepioctl() from
    239 		 * whining about an unknown ioctl.  Really, it should be
    240 		 * implemented.
    241 		 */
    242 		return (EIO);
    243 
    244 	case DPT_I2OUSRCMD:
    245 		return (dpti_passthrough(sc, data, p));
    246 
    247 	case DPT_I2ORESETCMD:
    248 		printf("%s: I2ORESETCMD not implemented\n",
    249 		    sc->sc_dv.dv_xname);
    250 		return (ENOTTY);
    251 
    252 	case DPT_I2ORESCANCMD:
    253 		return (iop_reconfigure(iop, 0));
    254 
    255 	default:
    256 		return (ENOTTY);
    257 	}
    258 }
    259 
    260 int
    261 dpti_blinkled(struct dpti_softc *sc)
    262 {
    263 	struct iop_softc *iop;
    264 	u_int v;
    265 
    266 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    267 
    268 	v = bus_space_read_1(iop->sc_iot, iop->sc_ioh, sc->sc_blinkled + 0);
    269 	if (v == 0xbc) {
    270 		v = bus_space_read_1(iop->sc_iot, iop->sc_ioh,
    271 		    sc->sc_blinkled + 1);
    272 		return (v);
    273 	}
    274 
    275 	return (-1);
    276 }
    277 
    278 int
    279 dpti_ctlrinfo(struct dpti_softc *sc, u_long cmd, caddr_t data)
    280 {
    281 	struct dpt_ctlrinfo CtlrInfo;
    282 	struct iop_softc *iop;
    283 	int rv, i;
    284 
    285 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    286 
    287 	memset(&CtlrInfo, 0, sizeof(CtlrInfo));
    288 
    289 	CtlrInfo.length = sizeof(CtlrInfo) - sizeof(u_int16_t);
    290 	CtlrInfo.drvrHBAnum = sc->sc_dv.dv_unit;
    291 	CtlrInfo.baseAddr = iop->sc_memaddr;
    292 	if ((i = dpti_blinkled(sc)) == -1)
    293 		i = 0;
    294 	CtlrInfo.blinkState = i;
    295 	CtlrInfo.pciBusNum = iop->sc_pcibus;
    296 	CtlrInfo.pciDeviceNum = iop->sc_pcidev;
    297 	CtlrInfo.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
    298 	CtlrInfo.Interrupt = 10;			/* XXX */
    299 
    300 	if (IOCPARM_LEN(cmd) > sizeof(*data)) {
    301 		bcopy(&CtlrInfo, data, sizeof(CtlrInfo));
    302 		rv = 0;
    303 	} else
    304 		rv = copyout(&CtlrInfo, *(caddr_t *)data,
    305 		    sizeof(CtlrInfo));
    306 
    307 	return (rv);
    308 }
    309 
    310 int
    311 dpti_sysinfo(struct dpti_softc *sc, u_long cmd, caddr_t data)
    312 {
    313 
    314 	/*
    315 	 * XXX Not currently implemented, since it involves reading from the
    316 	 * IBM PC CMOS and grovelling around in ISA expansion card space,
    317 	 * which is kind of disgusting.  The DPT utilities don't seem to
    318 	 * mind.  :-)
    319 	 */
    320 	return (EIO);
    321 }
    322 
    323 int
    324 dpti_passthrough(struct dpti_softc *sc, caddr_t data, struct proc *proc)
    325 {
    326 	struct iop_softc *iop;
    327 	struct i2o_msg mh, *mf;
    328 	struct i2o_reply rh;
    329 	struct iop_msg *im;
    330 	struct dpti_ptbuf bufs[IOP_MAX_MSG_XFERS];
    331 	u_int32_t mbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    332 	u_int32_t rbtmp[IOP_MAX_MSG_SIZE / sizeof(u_int32_t)];
    333 	int rv, msgsize, repsize, sgoff, i, mapped, nbuf;
    334 	u_int32_t *p, *pmax;
    335 
    336 	iop = (struct iop_softc *)sc->sc_dv.dv_parent;
    337 
    338 	if ((rv = dpti_blinkled(sc)) != -1) {
    339 		if (rv != 0) {
    340 			printf("%s: adapter blinkled = 0x%02x\n",
    341 			    sc->sc_dv.dv_xname, rv);
    342 			return (EIO);
    343 		}
    344 	}
    345 
    346 	/*
    347 	 * Copy in the message frame header and determine the size of the
    348 	 * full message frame.
    349 	 */
    350 	if ((rv = copyin(data, &mh, sizeof(mh))) != 0) {
    351 		DPRINTF(("%s: message copyin failed\n",
    352 		    sc->sc_dv.dv_xname));
    353 		return (rv);
    354 	}
    355 
    356 	msgsize = (mh.msgflags >> 14) & ~3;
    357 	if (msgsize < sizeof(mh) || msgsize >= IOP_MAX_MSG_SIZE) {
    358 		DPRINTF(("%s: bad message frame size\n",
    359 		    sc->sc_dv.dv_xname));
    360 		return (EINVAL);
    361 	}
    362 
    363 	/*
    364 	 * Handle special commands.
    365 	 *
    366 	 * XXX We'll need to handle all of these later for online firmware
    367 	 * upgrades.  Swish or what, eh?
    368 	 */
    369 	switch (mh.msgfunc >> 24) {
    370 	case I2O_EXEC_IOP_RESET:
    371 		printf("dpti%s: I2O_EXEC_IOP_RESET not implemented\n",
    372 		    sc->sc_dv.dv_xname);
    373 		return (EIO);
    374 
    375 	case I2O_EXEC_OUTBOUND_INIT:
    376 		printf("dpti%s: I2O_EXEC_OUTBOUND_INIT not implemented\n",
    377 		    sc->sc_dv.dv_xname);
    378 		return (EIO);
    379 
    380 	case I2O_EXEC_SYS_TAB_SET:
    381 		printf("dpti%s: I2O_EXEC_SYS_TAB_SET not implemented\n",
    382 		    sc->sc_dv.dv_xname);
    383 		return (EIO);
    384 
    385 	case I2O_EXEC_STATUS_GET:
    386 		iop_status_get(iop, 0);
    387 		rv = copyout(&iop->sc_status, data + msgsize,
    388 		    sizeof(iop->sc_status));
    389 		return (rv);
    390 	}
    391 
    392 	/*
    393 	 * Copy in the full message frame.
    394 	 */
    395 	if ((rv = copyin(data, mbtmp, msgsize)) != 0) {
    396 		DPRINTF(("%s: full message copyin failed\n",
    397 		    sc->sc_dv.dv_xname));
    398 		return (rv);
    399 	}
    400 
    401 	/*
    402 	 * Determine the size of the reply frame, and copy it in.
    403 	 */
    404 	if ((rv = copyin(data + msgsize, &rh, sizeof(rh))) != 0) {
    405 		DPRINTF(("%s: reply copyin failed\n",
    406 		    sc->sc_dv.dv_xname));
    407 		return (rv);
    408 	}
    409 
    410 	repsize = (rh.msgflags >> 14) & ~3;
    411 	if (repsize < sizeof(rh) || repsize >= IOP_MAX_MSG_SIZE) {
    412 		DPRINTF(("%s: bad reply header size\n",
    413 		    sc->sc_dv.dv_xname));
    414 		return (EINVAL);
    415 	}
    416 
    417 	if ((rv = copyin(data + msgsize, rbtmp, repsize)) != 0) {
    418 		DPRINTF(("%s: reply too large\n", sc->sc_dv.dv_xname));
    419 		return (rv);
    420 	}
    421 
    422 	/*
    423 	 * If the message has a scatter gather list, it must be comprised of
    424 	 * simple elements.  If any one transfer contains multiple segments,
    425 	 * we allocate a temporary buffer for it; otherwise, the buffer will
    426 	 * be mapped directly.  XXX Lies!  We don't do scatter-gather yet.
    427 	 */
    428 	if ((sgoff = ((mh.msgflags >> 4) & 15)) != 0) {
    429 		if ((sgoff + 2) > (msgsize >> 2)) {
    430 			DPRINTF(("%s: invalid message size fields\n",
    431 			    sc->sc_dv.dv_xname));
    432 			return (EINVAL);
    433 		}
    434 	} else
    435 		nbuf = -1;
    436 
    437 	if (sgoff != 0) {
    438 		p = mbtmp + sgoff;
    439 		pmax = mbtmp + (msgsize >> 2);
    440 
    441 		for (nbuf = 0; nbuf < IOP_MAX_MSG_XFERS; nbuf++) {
    442 			if (p > pmax - 2) {
    443 				DPRINTF(("%s: invalid SGL (1)\n",
    444 				    sc->sc_dv.dv_xname));
    445 				return (EINVAL);
    446 			}
    447 
    448 			if ((p[0] & 0x30000000) != I2O_SGL_SIMPLE) {
    449 				DPRINTF(("%s: invalid SGL (2)\n",
    450 				    sc->sc_dv.dv_xname));
    451 				return (EINVAL);
    452 			}
    453 
    454 			bufs[nbuf].db_out = (p[0] & I2O_SGL_DATA_OUT) != 0;
    455 
    456 			if ((p[0] & I2O_SGL_END_BUFFER) != 0) {
    457 				if ((p[0] & 0x00ffffff) > IOP_MAX_XFER) {
    458 					DPRINTF(("%s: buffer too large\n",
    459 					    sc->sc_dv.dv_xname));
    460 					return (EINVAL);
    461 				}
    462 
    463 				bufs[nbuf].db_ptr = (caddr_t)p[1];
    464 				bufs[nbuf].db_proc = proc;
    465 				bufs[nbuf].db_size = p[0] & 0x00ffffff;
    466 
    467 				if ((p[0] & I2O_SGL_END) != 0)
    468 					break;
    469 
    470 				p += 2;
    471 				continue;
    472 			}
    473 
    474 			printf("%s: scatter-gather not implemented\n",
    475 			    sc->sc_dv.dv_xname);
    476 			return (EIO);
    477 		}
    478 
    479 		if (nbuf == IOP_MAX_MSG_XFERS) {
    480 			DPRINTF(("%s: too many transfers\n",
    481 			    sc->sc_dv.dv_xname));
    482 			return (EINVAL);
    483 		}
    484 	}
    485 
    486 	/*
    487 	 * Allocate a wrapper, and adjust the message header fields to
    488 	 * indicate that no scatter-gather list is currently present.
    489 	 */
    490 	im = iop_msg_alloc(iop, IM_WAIT | IM_NOSTATUS);
    491 	im->im_rb = (struct i2o_reply *)rbtmp;
    492 	mf = (struct i2o_msg *)mbtmp;
    493 	mf->msgictx = IOP_ICTX;
    494 	mf->msgtctx = im->im_tctx;
    495 	mapped = 0;
    496 
    497 	if (sgoff != 0)
    498 		mf->msgflags = (mf->msgflags & 0xff0f) | (sgoff << 16);
    499 
    500 	/*
    501 	 * Map the data transfer(s).
    502 	 */
    503 	for (i = 0; i <= nbuf; i++) {
    504 		rv = iop_msg_map(iop, im, mbtmp, bufs[i].db_ptr,
    505 		    bufs[i].db_size, bufs[i].db_out, bufs[i].db_proc);
    506 		if (rv != 0) {
    507 			DPRINTF(("%s: msg_map failed\n",
    508 			    sc->sc_dv.dv_xname));
    509 			goto bad;
    510 		}
    511 		mapped = 1;
    512 	}
    513 
    514 	/*
    515 	 * Start the command and sleep until it completes.
    516 	 */
    517 	if (sc->sc_nactive++ >= 2)
    518 		tsleep(&sc->sc_nactive, PRIBIO, "dptislp", 0);
    519 
    520 	if ((rv = iop_msg_post(iop, im, mbtmp, 5*60*1000)) != 0)
    521 		goto bad;
    522 
    523 	/*
    524 	 * Copy out the reply frame.
    525 	 */
    526 	if ((rv = copyout(rbtmp, data + msgsize, repsize)) != 0)
    527 		DPRINTF(("%s: reply copyout() failed\n",
    528 		    sc->sc_dv.dv_xname));
    529 
    530  bad:
    531 	sc->sc_nactive--;
    532 	wakeup_one(&sc->sc_nactive);
    533 
    534 	if (mapped)
    535 		iop_msg_unmap(iop, im);
    536 
    537 	iop_msg_free(iop, im);
    538 	return (rv);
    539 }
    540