xref: /netbsd-src/sys/dev/ic/ld_mlx.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: ld_mlx.c,v 1.19 2009/01/16 04:20:28 mhitch 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.19 2009/01/16 04:20:28 mhitch Exp $");
38 
39 #include "rnd.h"
40 
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
53 
54 #include <machine/vmparam.h>
55 #include <sys/bus.h>
56 
57 #include <dev/ldvar.h>
58 
59 #include <dev/ic/mlxreg.h>
60 #include <dev/ic/mlxio.h>
61 #include <dev/ic/mlxvar.h>
62 
63 struct ld_mlx_softc {
64 	struct	ld_softc sc_ld;
65 	int	sc_hwunit;
66 };
67 
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 *);
76 
77 CFATTACH_DECL_NEW(ld_mlx, sizeof(struct ld_mlx_softc),
78     ld_mlx_match, ld_mlx_attach, ld_mlx_detach, NULL);
79 
80 static int
81 ld_mlx_match(device_t parent, cfdata_t match, void *aux)
82 {
83 
84 	return (1);
85 }
86 
87 static void
88 ld_mlx_attach(device_t parent, device_t self, void *aux)
89 {
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;
96 
97 	ld->sc_dv = self;
98 
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;
106 
107 	/*
108 	 * Report on current status, and attach to the ld driver proper.
109 	 */
110 	switch (ms->ms_state) {
111 	case MLX_SYSD_ONLINE:
112 		statestr = "online";
113 		ld->sc_flags = LDF_ENABLED;
114 		break;
115 
116 	case MLX_SYSD_CRITICAL:
117 		statestr = "critical";
118 		ld->sc_flags = LDF_ENABLED;
119 		break;
120 
121 	case MLX_SYSD_OFFLINE:
122 		statestr = "offline";
123 		break;
124 
125 	default:
126 		statestr = "state unknown";
127 		break;
128 	}
129 
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);
134 
135 	ldattach(ld);
136 }
137 
138 static int
139 ld_mlx_detach(device_t dv, int flags)
140 {
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;
145 
146 	if ((rv = ldbegindetach(ld, flags)) != 0)
147 		return (rv);
148 	ldenddetach(ld);
149 	mlx_flush(mlx, 1);
150 
151 	return (0);
152 }
153 
154 static int
155 ld_mlx_dobio(struct ld_mlx_softc *sc, void *data, int datasize, int blkno,
156 	     int dowrite, struct buf *bp)
157 {
158 	struct mlx_ccb *mc;
159 	struct mlx_softc *mlx;
160 	int s, rv;
161 	bus_addr_t sgphys;
162 
163 	mlx = device_private(device_parent(sc->sc_ld.sc_dv));
164 
165 	if ((rv = mlx_ccb_alloc(mlx, &mc, bp == NULL)) != 0)
166 		return (rv);
167 
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);
174 	}
175 
176 	/* Build the command. */
177 	sgphys = mlx->mlx_sgls_paddr + (MLX_SGL_SIZE * mc->mc_ident);
178 	datasize /= MLX_SECTOR_SIZE;
179 
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);
191 
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;
204 	}
205 
206 	return (rv);
207 }
208 
209 static int
210 ld_mlx_start(struct ld_softc *ld, struct buf *bp)
211 {
212 
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));
215 }
216 
217 static void
218 ld_mlx_handler(struct mlx_ccb *mc)
219 {
220 	struct buf *bp;
221 	struct mlx_context *mx;
222 	struct ld_mlx_softc *sc;
223 	struct mlx_softc *mlx;
224 
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));
229 
230 	if (mc->mc_status != MLX_STATUS_OK) {
231 		bp->b_error = EIO;
232 		bp->b_resid = bp->b_bcount;
233 
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;
243 
244 	mlx_ccb_unmap(mlx, mc);
245 	mlx_ccb_free(mlx, mc);
246 	lddone(&sc->sc_ld, bp);
247 }
248 
249 static int
250 ld_mlx_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
251 {
252 
253 	return (ld_mlx_dobio((struct ld_mlx_softc *)ld, data,
254 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
255 }
256