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