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