xref: /netbsd-src/sys/dev/ic/ld_mlx.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: ld_mlx.c,v 1.8 2004/10/28 07:07:40 yamt 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.8 2004/10/28 07:07:40 yamt 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 <machine/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, 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 	ld->sc_secperunit = ms->ms_size;
117 
118 	/*
119 	 * Report on current status, and attach to the ld driver proper.
120 	 */
121 	switch (ms->ms_state) {
122 	case MLX_SYSD_ONLINE:
123 		statestr = "online";
124 		ld->sc_flags = LDF_ENABLED;
125 		break;
126 
127 	case MLX_SYSD_CRITICAL:
128 		statestr = "critical";
129 		ld->sc_flags = LDF_ENABLED;
130 		break;
131 
132 	case MLX_SYSD_OFFLINE:
133 		statestr = "offline";
134 		break;
135 
136 	default:
137 		statestr = "state unknown";
138 		break;
139 	}
140 
141 	if (ms->ms_raidlevel == MLX_SYS_DRV_JBOD)
142 		printf(": JBOD, %s\n", statestr);
143 	else
144 		printf(": RAID%d, %s\n", ms->ms_raidlevel, statestr);
145 
146 	ldattach(ld);
147 }
148 
149 static int
150 ld_mlx_detach(struct device *dv, int flags)
151 {
152 	int rv;
153 
154 	if ((rv = ldbegindetach((struct ld_softc *)dv, flags)) != 0)
155 		return (rv);
156 	ldenddetach((struct ld_softc *)dv);
157 	mlx_flush((struct mlx_softc *)dv->dv_parent, 1);
158 
159 	return (0);
160 }
161 
162 static int
163 ld_mlx_dobio(struct ld_mlx_softc *sc, void *data, int datasize, int blkno,
164 	     int dowrite, struct buf *bp)
165 {
166 	struct mlx_ccb *mc;
167 	struct mlx_softc *mlx;
168 	int s, rv;
169 	bus_addr_t sgphys;
170 
171 	mlx = (struct mlx_softc *)sc->sc_ld.sc_dv.dv_parent;
172 
173 	if ((rv = mlx_ccb_alloc(mlx, &mc, bp == NULL)) != 0)
174 		return (rv);
175 
176 	/* Map the data transfer. */
177 	rv = mlx_ccb_map(mlx, mc, data, datasize,
178 	    dowrite ? MC_XFER_OUT : MC_XFER_IN);
179 	if (rv != 0) {
180 		mlx_ccb_free(mlx, mc);
181 		return (rv);
182 	}
183 
184 	/* Build the command. */
185 	sgphys = mlx->mlx_sgls_paddr + (MLX_SGL_SIZE * mc->mc_ident);
186 	datasize /= MLX_SECTOR_SIZE;
187 
188 	if (mlx->mlx_ci.ci_iftype <= 2)
189 		mlx_make_type1(mc,
190 		    dowrite ? MLX_CMD_WRITESG_OLD : MLX_CMD_READSG_OLD,
191 		    datasize & 0xff, blkno, sc->sc_hwunit, sgphys,
192 		    mc->mc_nsgent);
193 	else
194 		mlx_make_type5(mc,
195 		    dowrite ? MLX_CMD_WRITESG : MLX_CMD_READSG,
196 		    datasize & 0xff,
197 		    (sc->sc_hwunit << 3) | ((datasize >> 8) & 0x07),
198 		    blkno, sgphys, mc->mc_nsgent);
199 
200 	if (bp == NULL) {
201 		s = splbio();
202 		rv = mlx_ccb_poll(mlx, mc, 10000);
203 		mlx_ccb_unmap(mlx, mc);
204 		mlx_ccb_free(mlx, mc);
205 		splx(s);
206 	} else {
207  		mc->mc_mx.mx_handler = ld_mlx_handler;
208 		mc->mc_mx.mx_context = bp;
209 		mc->mc_mx.mx_dv = &sc->sc_ld.sc_dv;
210 		mlx_ccb_enqueue(mlx, mc);
211 		rv = 0;
212 	}
213 
214 	return (rv);
215 }
216 
217 static int
218 ld_mlx_start(struct ld_softc *ld, struct buf *bp)
219 {
220 
221 	return (ld_mlx_dobio((struct ld_mlx_softc *)ld, bp->b_data,
222 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
223 }
224 
225 static void
226 ld_mlx_handler(struct mlx_ccb *mc)
227 {
228 	struct buf *bp;
229 	struct mlx_context *mx;
230 	struct ld_mlx_softc *sc;
231 	struct mlx_softc *mlx;
232 
233 	mx = &mc->mc_mx;
234 	bp = mx->mx_context;
235 	sc = (struct ld_mlx_softc *)mx->mx_dv;
236 	mlx = (struct mlx_softc *)sc->sc_ld.sc_dv.dv_parent;
237 
238 	if (mc->mc_status != MLX_STATUS_OK) {
239 		bp->b_flags |= B_ERROR;
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