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