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