Home | History | Annotate | Line # | Download | only in ic
ld_mlx.c revision 1.1.2.2
      1 /*	$NetBSD: ld_mlx.c,v 1.1.2.2 2001/02/11 19:15:32 bouyer 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  * Mylex DAC960 front-end for ld(4) driver.
     41  */
     42 
     43 #include "rnd.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/device.h>
     49 #include <sys/buf.h>
     50 #include <sys/endian.h>
     51 #include <sys/dkio.h>
     52 #include <sys/disk.h>
     53 #if NRND > 0
     54 #include <sys/rnd.h>
     55 #endif
     56 
     57 #include <machine/vmparam.h>
     58 #include <machine/bus.h>
     59 
     60 #include <dev/ldvar.h>
     61 
     62 #include <dev/ic/mlxreg.h>
     63 #include <dev/ic/mlxio.h>
     64 #include <dev/ic/mlxvar.h>
     65 
     66 struct ld_mlx_softc {
     67 	struct	ld_softc sc_ld;
     68 	int	sc_hwunit;
     69 };
     70 
     71 static void	ld_mlx_attach(struct device *, struct device *, void *);
     72 static int	ld_mlx_detach(struct device *, int);
     73 static int	ld_mlx_dobio(struct ld_mlx_softc *, void *, int, int, int,
     74 			     struct buf *);
     75 static int	ld_mlx_dump(struct ld_softc *, void *, int, int);
     76 static void	ld_mlx_handler(struct mlx_ccb *);
     77 static int	ld_mlx_match(struct device *, struct cfdata *, void *);
     78 static int	ld_mlx_start(struct ld_softc *, struct buf *);
     79 
     80 struct cfattach ld_mlx_ca = {
     81 	sizeof(struct ld_mlx_softc),
     82 	ld_mlx_match,
     83 	ld_mlx_attach,
     84 	ld_mlx_detach
     85 };
     86 
     87 static int
     88 ld_mlx_match(struct device *parent, struct cfdata *match, void *aux)
     89 {
     90 
     91 	return (1);
     92 }
     93 
     94 static void
     95 ld_mlx_attach(struct device *parent, struct device *self, void *aux)
     96 {
     97 	struct mlx_attach_args *mlxa;
     98 	struct ld_mlx_softc *sc;
     99 	struct ld_softc *ld;
    100 	struct mlx_softc *mlx;
    101 	struct mlx_sysdrive *ms;
    102 	const char *statestr;
    103 
    104 	sc = (struct ld_mlx_softc *)self;
    105 	ld = &sc->sc_ld;
    106 	mlx = (struct mlx_softc *)parent;
    107 	mlxa = aux;
    108 	ms = &mlx->mlx_sysdrive[mlxa->mlxa_unit];
    109 
    110 	sc->sc_hwunit = mlxa->mlxa_unit;
    111 	ld->sc_maxxfer = MLX_MAX_XFER;
    112 	ld->sc_secsize = MLX_SECTOR_SIZE;
    113 	ld->sc_maxqueuecnt = 1;
    114 	ld->sc_start = ld_mlx_start;
    115 	ld->sc_dump = ld_mlx_dump;
    116 
    117 	/*
    118 	 * Build synthetic geometry.
    119 	 */
    120 	ld->sc_secperunit = ms->ms_size;
    121 
    122 	if (ld->sc_secperunit > 0x200000) {
    123 		ld->sc_nheads = 255;
    124 		ld->sc_nsectors = 63;
    125 		ld->sc_ncylinders = ms->ms_size / (255 * 63);
    126 	} else {
    127 		ld->sc_nheads = 128;
    128 		ld->sc_nsectors = 32;
    129 		ld->sc_ncylinders = ms->ms_size / (128 * 32);
    130 	}
    131 
    132 	/*
    133 	 * Report on current status, and attach to the ld driver proper.
    134 	 */
    135 	switch (ms->ms_state) {
    136 	case MLX_SYSD_ONLINE:
    137 		statestr = "online";
    138 		ld->sc_flags = LDF_ENABLED;
    139 		break;
    140 
    141 	case MLX_SYSD_CRITICAL:
    142 		statestr = "critical";
    143 		ld->sc_flags = LDF_ENABLED;
    144 		break;
    145 
    146 	case MLX_SYSD_OFFLINE:
    147 		statestr = "offline";
    148 		break;
    149 
    150 	default:
    151 		statestr = "state unknown";
    152 		break;
    153 	}
    154 
    155 	if (ms->ms_raidlevel == MLX_SYS_DRV_JBOD)
    156 		printf(": JBOD, %s\n", statestr);
    157 	else
    158 		printf(": RAID%d, %s\n", ms->ms_raidlevel, statestr);
    159 
    160 	ldattach(ld);
    161 }
    162 
    163 static int
    164 ld_mlx_detach(struct device *dv, int flags)
    165 {
    166 	int rv;
    167 
    168 	if ((rv = ldbegindetach((struct ld_softc *)dv, flags)) != 0)
    169 		return (rv);
    170 	ldenddetach((struct ld_softc *)dv);
    171 	mlx_flush((struct mlx_softc *)dv->dv_parent, 1);
    172 
    173 	return (0);
    174 }
    175 
    176 static int
    177 ld_mlx_dobio(struct ld_mlx_softc *sc, void *data, int datasize, int blkno,
    178 	     int dowrite, struct buf *bp)
    179 {
    180 	struct mlx_ccb *mc;
    181 	struct mlx_softc *mlx;
    182 	int s, rv;
    183 	bus_addr_t sgphys;
    184 
    185 	mlx = (struct mlx_softc *)sc->sc_ld.sc_dv.dv_parent;
    186 
    187 	if ((rv = mlx_ccb_alloc(mlx, &mc, bp == NULL)) != 0)
    188 		return (rv);
    189 
    190 	/* Map the data transfer. */
    191 	rv = mlx_ccb_map(mlx, mc, data, datasize,
    192 	    dowrite ? MC_XFER_OUT : MC_XFER_IN);
    193 	if (rv != 0) {
    194 		mlx_ccb_free(mlx, mc);
    195 		return (rv);
    196 	}
    197 
    198 	/* Build the command. */
    199 	sgphys = mlx->mlx_sgls_paddr + (MLX_SGL_SIZE * mc->mc_ident);
    200 	datasize /= MLX_SECTOR_SIZE;
    201 
    202 	if (mlx->mlx_iftype == 2)
    203 		mlx_make_type1(mc,
    204 		    dowrite ? MLX_CMD_WRITESG_OLD : MLX_CMD_READSG_OLD,
    205 		    datasize & 0xff, blkno, sc->sc_hwunit, sgphys,
    206 		    mc->mc_nsgent);
    207 	else
    208 		mlx_make_type5(mc,
    209 		    dowrite ? MLX_CMD_WRITESG : MLX_CMD_READSG,
    210 		    datasize & 0xff,
    211 		    (sc->sc_hwunit << 3) | ((datasize >> 8) & 0x07),
    212 		    blkno, sgphys, mc->mc_nsgent);
    213 
    214 	if (bp == NULL) {
    215 		s = splbio();
    216 		rv = mlx_ccb_poll(mlx, mc, 10000);
    217 		mlx_ccb_unmap(mlx, mc);
    218 		mlx_ccb_free(mlx, mc);
    219 		splx(s);
    220 	} else {
    221  		mc->mc_mx.mx_handler = ld_mlx_handler;
    222 		mc->mc_mx.mx_context = bp;
    223 		mc->mc_mx.mx_dv = &sc->sc_ld.sc_dv;
    224 		mlx_ccb_enqueue(mlx, mc);
    225 		rv = 0;
    226 	}
    227 
    228 	return (rv);
    229 }
    230 
    231 static int
    232 ld_mlx_start(struct ld_softc *ld, struct buf *bp)
    233 {
    234 
    235 	return (ld_mlx_dobio((struct ld_mlx_softc *)ld, bp->b_data,
    236 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
    237 }
    238 
    239 static void
    240 ld_mlx_handler(struct mlx_ccb *mc)
    241 {
    242 	struct buf *bp;
    243 	struct mlx_context *mx;
    244 	struct ld_mlx_softc *sc;
    245 	struct mlx_softc *mlx;
    246 
    247 	mx = &mc->mc_mx;
    248 	bp = mx->mx_context;
    249 	sc = (struct ld_mlx_softc *)mx->mx_dv;
    250 	mlx = (struct mlx_softc *)sc->sc_ld.sc_dv.dv_parent;
    251 
    252 	if (mc->mc_status != MLX_STATUS_OK) {
    253 		bp->b_flags |= B_ERROR;
    254 		bp->b_error = EIO;
    255 		bp->b_resid = bp->b_bcount;
    256 
    257 		if (mc->mc_status == MLX_STATUS_RDWROFFLINE)
    258 			printf("%s: drive offline\n",
    259 			    sc->sc_ld.sc_dv.dv_xname);
    260 		else
    261 			printf("%s: I/O error - %s\n",
    262 			    sc->sc_ld.sc_dv.dv_xname,
    263 			    mlx_ccb_diagnose(mc));
    264 	} else
    265 		bp->b_resid = 0;
    266 
    267 	mlx_ccb_unmap(mlx, mc);
    268 	mlx_ccb_free(mlx, mc);
    269 	lddone(&sc->sc_ld, bp);
    270 }
    271 
    272 static int
    273 ld_mlx_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
    274 {
    275 
    276 	return (ld_mlx_dobio((struct ld_mlx_softc *)ld, data,
    277 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
    278 }
    279