Expand PMF_FN_* macros.
[netbsd-mini2440.git] / sys / dev / ic / ld_mlx.c
blobd13d6a8a56c3a49a4a0bbcb34231d86509c05a04
1 /* $NetBSD: ld_mlx.c,v 1.18 2008/09/09 12:45:40 tron Exp $ */
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Mylex DAC960 front-end for ld(4) driver.
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ld_mlx.c,v 1.18 2008/09/09 12:45:40 tron Exp $");
39 #include "rnd.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/device.h>
45 #include <sys/buf.h>
46 #include <sys/bufq.h>
47 #include <sys/endian.h>
48 #include <sys/dkio.h>
49 #include <sys/disk.h>
50 #if NRND > 0
51 #include <sys/rnd.h>
52 #endif
54 #include <machine/vmparam.h>
55 #include <sys/bus.h>
57 #include <dev/ldvar.h>
59 #include <dev/ic/mlxreg.h>
60 #include <dev/ic/mlxio.h>
61 #include <dev/ic/mlxvar.h>
63 struct ld_mlx_softc {
64 struct ld_softc sc_ld;
65 int sc_hwunit;
68 static void ld_mlx_attach(device_t, device_t, void *);
69 static int ld_mlx_detach(device_t, int);
70 static int ld_mlx_dobio(struct ld_mlx_softc *, void *, int, int, int,
71 struct buf *);
72 static int ld_mlx_dump(struct ld_softc *, void *, int, int);
73 static void ld_mlx_handler(struct mlx_ccb *);
74 static int ld_mlx_match(device_t, cfdata_t, void *);
75 static int ld_mlx_start(struct ld_softc *, struct buf *);
77 CFATTACH_DECL_NEW(ld_mlx, sizeof(struct ld_mlx_softc),
78 ld_mlx_match, ld_mlx_attach, ld_mlx_detach, NULL);
80 static int
81 ld_mlx_match(device_t parent, cfdata_t match, void *aux)
84 return (1);
87 static void
88 ld_mlx_attach(device_t parent, device_t self, void *aux)
90 struct mlx_attach_args *mlxa = aux;
91 struct ld_mlx_softc *sc = device_private(self);
92 struct ld_softc *ld = &sc->sc_ld;
93 struct mlx_softc *mlx = device_private(parent);
94 struct mlx_sysdrive *ms = &mlx->mlx_sysdrive[mlxa->mlxa_unit];
95 const char *statestr;
97 ld->sc_dv = self;
99 sc->sc_hwunit = mlxa->mlxa_unit;
100 ld->sc_maxxfer = MLX_MAX_XFER;
101 ld->sc_secsize = MLX_SECTOR_SIZE;
102 ld->sc_maxqueuecnt = 1;
103 ld->sc_start = ld_mlx_start;
104 ld->sc_dump = ld_mlx_dump;
105 ld->sc_secperunit = ms->ms_size;
108 * Report on current status, and attach to the ld driver proper.
110 switch (ms->ms_state) {
111 case MLX_SYSD_ONLINE:
112 statestr = "online";
113 ld->sc_flags = LDF_ENABLED;
114 break;
116 case MLX_SYSD_CRITICAL:
117 statestr = "critical";
118 ld->sc_flags = LDF_ENABLED;
119 break;
121 case MLX_SYSD_OFFLINE:
122 statestr = "offline";
123 break;
125 default:
126 statestr = "state unknown";
127 break;
130 if (ms->ms_raidlevel == MLX_SYS_DRV_JBOD)
131 aprint_normal(": JBOD, %s\n", statestr);
132 else
133 aprint_normal(": RAID%d, %s\n", ms->ms_raidlevel, statestr);
135 ldattach(ld);
138 static int
139 ld_mlx_detach(device_t dv, int flags)
141 struct ld_mlx_softc *sc = device_private(dv);
142 struct ld_softc *ld = &sc->sc_ld;
143 struct mlx_softc *mlx = device_private(device_parent(dv));
144 int rv;
146 if ((rv = ldbegindetach(ld, flags)) != 0)
147 return (rv);
148 ldenddetach(ld);
149 mlx_flush(mlx, 1);
151 return (0);
154 static int
155 ld_mlx_dobio(struct ld_mlx_softc *sc, void *data, int datasize, int blkno,
156 int dowrite, struct buf *bp)
158 struct mlx_ccb *mc;
159 struct mlx_softc *mlx;
160 int s, rv;
161 bus_addr_t sgphys;
163 mlx = device_private(device_parent(sc->sc_ld.sc_dv));
165 if ((rv = mlx_ccb_alloc(mlx, &mc, bp == NULL)) != 0)
166 return (rv);
168 /* Map the data transfer. */
169 rv = mlx_ccb_map(mlx, mc, data, datasize,
170 dowrite ? MC_XFER_OUT : MC_XFER_IN);
171 if (rv != 0) {
172 mlx_ccb_free(mlx, mc);
173 return (rv);
176 /* Build the command. */
177 sgphys = mlx->mlx_sgls_paddr + (MLX_SGL_SIZE * mc->mc_ident);
178 datasize /= MLX_SECTOR_SIZE;
180 if (mlx->mlx_ci.ci_iftype <= 2)
181 mlx_make_type1(mc,
182 dowrite ? MLX_CMD_WRITESG_OLD : MLX_CMD_READSG_OLD,
183 datasize & 0xff, blkno, sc->sc_hwunit, sgphys,
184 mc->mc_nsgent);
185 else
186 mlx_make_type5(mc,
187 dowrite ? MLX_CMD_WRITESG : MLX_CMD_READSG,
188 datasize & 0xff,
189 (sc->sc_hwunit << 3) | ((datasize >> 8) & 0x07),
190 blkno, sgphys, mc->mc_nsgent);
192 if (bp == NULL) {
193 s = splbio();
194 rv = mlx_ccb_poll(mlx, mc, 10000);
195 mlx_ccb_unmap(mlx, mc);
196 mlx_ccb_free(mlx, mc);
197 splx(s);
198 } else {
199 mc->mc_mx.mx_handler = ld_mlx_handler;
200 mc->mc_mx.mx_context = bp;
201 mc->mc_mx.mx_dv = sc->sc_ld.sc_dv;
202 mlx_ccb_enqueue(mlx, mc);
203 rv = 0;
206 return (rv);
209 static int
210 ld_mlx_start(struct ld_softc *ld, struct buf *bp)
213 return (ld_mlx_dobio((struct ld_mlx_softc *)ld, bp->b_data,
214 bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
217 static void
218 ld_mlx_handler(struct mlx_ccb *mc)
220 struct buf *bp;
221 struct mlx_context *mx;
222 struct ld_mlx_softc *sc;
223 struct mlx_softc *mlx;
225 mx = &mc->mc_mx;
226 bp = mx->mx_context;
227 sc = device_private(mx->mx_dv);
228 mlx = device_private(device_parent(sc->sc_ld.sc_dv));
230 if (mc->mc_status != MLX_STATUS_OK) {
231 bp->b_error = EIO;
232 bp->b_resid = bp->b_bcount;
234 if (mc->mc_status == MLX_STATUS_RDWROFFLINE)
235 printf("%s: drive offline\n",
236 device_xname(sc->sc_ld.sc_dv));
237 else
238 printf("%s: I/O error - %s\n",
239 device_xname(sc->sc_ld.sc_dv),
240 mlx_ccb_diagnose(mc));
241 } else
242 bp->b_resid = 0;
244 mlx_ccb_unmap(mlx, mc);
245 mlx_ccb_free(mlx, mc);
246 lddone(&sc->sc_ld, bp);
249 static int
250 ld_mlx_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
253 return (ld_mlx_dobio((struct ld_mlx_softc *)ld, data,
254 blkcnt * ld->sc_secsize, blkno, 1, NULL));