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