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