xref: /netbsd/sys/dev/ic/ld_mlx.c (revision c4a72b64)
1 /*	$NetBSD: ld_mlx.c,v 1.7 2002/10/02 16:33:34 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Mylex DAC960 front-end for ld(4) driver.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: ld_mlx.c,v 1.7 2002/10/02 16:33:34 thorpej Exp $");
45 
46 #include "rnd.h"
47 
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/device.h>
52 #include <sys/buf.h>
53 #include <sys/endian.h>
54 #include <sys/dkio.h>
55 #include <sys/disk.h>
56 #if NRND > 0
57 #include <sys/rnd.h>
58 #endif
59 
60 #include <machine/vmparam.h>
61 #include <machine/bus.h>
62 
63 #include <dev/ldvar.h>
64 
65 #include <dev/ic/mlxreg.h>
66 #include <dev/ic/mlxio.h>
67 #include <dev/ic/mlxvar.h>
68 
69 struct ld_mlx_softc {
70 	struct	ld_softc sc_ld;
71 	int	sc_hwunit;
72 };
73 
74 static void	ld_mlx_attach(struct device *, struct device *, void *);
75 static int	ld_mlx_detach(struct device *, int);
76 static int	ld_mlx_dobio(struct ld_mlx_softc *, void *, int, int, int,
77 			     struct buf *);
78 static int	ld_mlx_dump(struct ld_softc *, void *, int, int);
79 static void	ld_mlx_handler(struct mlx_ccb *);
80 static int	ld_mlx_match(struct device *, struct cfdata *, void *);
81 static int	ld_mlx_start(struct ld_softc *, struct buf *);
82 
83 CFATTACH_DECL(ld_mlx, sizeof(struct ld_mlx_softc),
84     ld_mlx_match, ld_mlx_attach, ld_mlx_detach, NULL);
85 
86 static int
87 ld_mlx_match(struct device *parent, struct cfdata *match, void *aux)
88 {
89 
90 	return (1);
91 }
92 
93 static void
94 ld_mlx_attach(struct device *parent, struct device *self, void *aux)
95 {
96 	struct mlx_attach_args *mlxa;
97 	struct ld_mlx_softc *sc;
98 	struct ld_softc *ld;
99 	struct mlx_softc *mlx;
100 	struct mlx_sysdrive *ms;
101 	const char *statestr;
102 
103 	sc = (struct ld_mlx_softc *)self;
104 	ld = &sc->sc_ld;
105 	mlx = (struct mlx_softc *)parent;
106 	mlxa = aux;
107 	ms = &mlx->mlx_sysdrive[mlxa->mlxa_unit];
108 
109 	sc->sc_hwunit = mlxa->mlxa_unit;
110 	ld->sc_maxxfer = MLX_MAX_XFER;
111 	ld->sc_secsize = MLX_SECTOR_SIZE;
112 	ld->sc_maxqueuecnt = 1;
113 	ld->sc_start = ld_mlx_start;
114 	ld->sc_dump = ld_mlx_dump;
115 	ld->sc_secperunit = ms->ms_size;
116 
117 	/*
118 	 * Report on current status, and attach to the ld driver proper.
119 	 */
120 	switch (ms->ms_state) {
121 	case MLX_SYSD_ONLINE:
122 		statestr = "online";
123 		ld->sc_flags = LDF_ENABLED;
124 		break;
125 
126 	case MLX_SYSD_CRITICAL:
127 		statestr = "critical";
128 		ld->sc_flags = LDF_ENABLED;
129 		break;
130 
131 	case MLX_SYSD_OFFLINE:
132 		statestr = "offline";
133 		break;
134 
135 	default:
136 		statestr = "state unknown";
137 		break;
138 	}
139 
140 	if (ms->ms_raidlevel == MLX_SYS_DRV_JBOD)
141 		printf(": JBOD, %s\n", statestr);
142 	else
143 		printf(": RAID%d, %s\n", ms->ms_raidlevel, statestr);
144 
145 	ldattach(ld);
146 }
147 
148 static int
149 ld_mlx_detach(struct device *dv, int flags)
150 {
151 	int rv;
152 
153 	if ((rv = ldbegindetach((struct ld_softc *)dv, flags)) != 0)
154 		return (rv);
155 	ldenddetach((struct ld_softc *)dv);
156 	mlx_flush((struct mlx_softc *)dv->dv_parent, 1);
157 
158 	return (0);
159 }
160 
161 static int
162 ld_mlx_dobio(struct ld_mlx_softc *sc, void *data, int datasize, int blkno,
163 	     int dowrite, struct buf *bp)
164 {
165 	struct mlx_ccb *mc;
166 	struct mlx_softc *mlx;
167 	int s, rv;
168 	bus_addr_t sgphys;
169 
170 	mlx = (struct mlx_softc *)sc->sc_ld.sc_dv.dv_parent;
171 
172 	if ((rv = mlx_ccb_alloc(mlx, &mc, bp == NULL)) != 0)
173 		return (rv);
174 
175 	/* Map the data transfer. */
176 	rv = mlx_ccb_map(mlx, mc, data, datasize,
177 	    dowrite ? MC_XFER_OUT : MC_XFER_IN);
178 	if (rv != 0) {
179 		mlx_ccb_free(mlx, mc);
180 		return (rv);
181 	}
182 
183 	/* Build the command. */
184 	sgphys = mlx->mlx_sgls_paddr + (MLX_SGL_SIZE * mc->mc_ident);
185 	datasize /= MLX_SECTOR_SIZE;
186 
187 	if (mlx->mlx_ci.ci_iftype <= 2)
188 		mlx_make_type1(mc,
189 		    dowrite ? MLX_CMD_WRITESG_OLD : MLX_CMD_READSG_OLD,
190 		    datasize & 0xff, blkno, sc->sc_hwunit, sgphys,
191 		    mc->mc_nsgent);
192 	else
193 		mlx_make_type5(mc,
194 		    dowrite ? MLX_CMD_WRITESG : MLX_CMD_READSG,
195 		    datasize & 0xff,
196 		    (sc->sc_hwunit << 3) | ((datasize >> 8) & 0x07),
197 		    blkno, sgphys, mc->mc_nsgent);
198 
199 	if (bp == NULL) {
200 		s = splbio();
201 		rv = mlx_ccb_poll(mlx, mc, 10000);
202 		mlx_ccb_unmap(mlx, mc);
203 		mlx_ccb_free(mlx, mc);
204 		splx(s);
205 	} else {
206  		mc->mc_mx.mx_handler = ld_mlx_handler;
207 		mc->mc_mx.mx_context = bp;
208 		mc->mc_mx.mx_dv = &sc->sc_ld.sc_dv;
209 		mlx_ccb_enqueue(mlx, mc);
210 		rv = 0;
211 	}
212 
213 	return (rv);
214 }
215 
216 static int
217 ld_mlx_start(struct ld_softc *ld, struct buf *bp)
218 {
219 
220 	return (ld_mlx_dobio((struct ld_mlx_softc *)ld, bp->b_data,
221 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
222 }
223 
224 static void
225 ld_mlx_handler(struct mlx_ccb *mc)
226 {
227 	struct buf *bp;
228 	struct mlx_context *mx;
229 	struct ld_mlx_softc *sc;
230 	struct mlx_softc *mlx;
231 
232 	mx = &mc->mc_mx;
233 	bp = mx->mx_context;
234 	sc = (struct ld_mlx_softc *)mx->mx_dv;
235 	mlx = (struct mlx_softc *)sc->sc_ld.sc_dv.dv_parent;
236 
237 	if (mc->mc_status != MLX_STATUS_OK) {
238 		bp->b_flags |= B_ERROR;
239 		bp->b_error = EIO;
240 		bp->b_resid = bp->b_bcount;
241 
242 		if (mc->mc_status == MLX_STATUS_RDWROFFLINE)
243 			printf("%s: drive offline\n",
244 			    sc->sc_ld.sc_dv.dv_xname);
245 		else
246 			printf("%s: I/O error - %s\n",
247 			    sc->sc_ld.sc_dv.dv_xname,
248 			    mlx_ccb_diagnose(mc));
249 	} else
250 		bp->b_resid = 0;
251 
252 	mlx_ccb_unmap(mlx, mc);
253 	mlx_ccb_free(mlx, mc);
254 	lddone(&sc->sc_ld, bp);
255 }
256 
257 static int
258 ld_mlx_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
259 {
260 
261 	return (ld_mlx_dobio((struct ld_mlx_softc *)ld, data,
262 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
263 }
264