xref: /netbsd-src/sys/dev/pci/ld_twe.c (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: ld_twe.c,v 1.37 2015/04/13 16:33:25 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2001, 2002, 2003 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; and by Jason R. Thorpe of Wasabi Systems, Inc.
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  * 3ware "Escalade" RAID controller front-end for ld(4) driver.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ld_twe.c,v 1.37 2015/04/13 16:33:25 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 #include <sys/proc.h>
49 
50 #include <sys/bus.h>
51 
52 #include <dev/ldvar.h>
53 
54 #include <dev/pci/twereg.h>
55 #include <dev/pci/twevar.h>
56 
57 struct ld_twe_softc {
58 	struct	ld_softc sc_ld;
59 	int	sc_hwunit;
60 };
61 
62 static void	ld_twe_attach(device_t, device_t, void *);
63 static int	ld_twe_detach(device_t, int);
64 static int	ld_twe_dobio(struct ld_twe_softc *, void *, int, int, int,
65 			     struct buf *);
66 static int	ld_twe_dump(struct ld_softc *, void *, int, int);
67 static int	ld_twe_flush(struct ld_softc *, int);
68 static void	ld_twe_handler(struct twe_ccb *, int);
69 static int	ld_twe_match(device_t, cfdata_t, void *);
70 static int	ld_twe_start(struct ld_softc *, struct buf *);
71 
72 static void	ld_twe_adjqparam(device_t, int);
73 
74 CFATTACH_DECL_NEW(ld_twe, sizeof(struct ld_twe_softc),
75     ld_twe_match, ld_twe_attach, ld_twe_detach, NULL);
76 
77 static const struct twe_callbacks ld_twe_callbacks = {
78 	ld_twe_adjqparam,
79 };
80 
81 static int
82 ld_twe_match(device_t parent, cfdata_t match, void *aux)
83 {
84 
85 	return (1);
86 }
87 
88 static void
89 ld_twe_attach(device_t parent, device_t self, void *aux)
90 {
91 	struct twe_attach_args *twea = aux;
92 	struct ld_twe_softc *sc = device_private(self);
93 	struct ld_softc *ld = &sc->sc_ld;
94 	struct twe_softc *twe = device_private(parent);
95 	struct twe_drive *td = &twe->sc_units[twea->twea_unit];
96 	const char *typestr, *stripestr, *statstr;
97 	char unktype[16], stripebuf[32], unkstat[32];
98 	int error;
99 	uint8_t status;
100 
101 	ld->sc_dv = self;
102 
103 	twe_register_callbacks(twe, twea->twea_unit, &ld_twe_callbacks);
104 
105 	sc->sc_hwunit = twea->twea_unit;
106 	ld->sc_flags = LDF_ENABLED;
107 	ld->sc_maxxfer = twe_get_maxxfer(twe_get_maxsegs());
108 	ld->sc_secperunit = td->td_size;
109 	ld->sc_secsize = TWE_SECTOR_SIZE;
110 	ld->sc_maxqueuecnt = twe->sc_openings;
111 	ld->sc_start = ld_twe_start;
112 	ld->sc_dump = ld_twe_dump;
113 	ld->sc_flush = ld_twe_flush;
114 
115 	typestr = twe_describe_code(twe_table_unittype, td->td_type);
116 	if (typestr == NULL) {
117 		snprintf(unktype, sizeof(unktype), "<0x%02x>", td->td_type);
118 		typestr = unktype;
119 	}
120 	switch (td->td_type) {
121 	case TWE_AD_CONFIG_RAID0:
122 	case TWE_AD_CONFIG_RAID5:
123 	case TWE_AD_CONFIG_RAID10:
124 		stripestr = twe_describe_code(twe_table_stripedepth,
125 		    td->td_stripe);
126 		if (stripestr == NULL)
127 			snprintf(stripebuf, sizeof(stripebuf),
128 			    "<stripe code 0x%02x> ", td->td_stripe);
129 		else
130 			snprintf(stripebuf, sizeof(stripebuf), "%s stripe ",
131 			    stripestr);
132 		break;
133 	default:
134 		stripebuf[0] = '\0';
135 	}
136 
137 	error = twe_param_get_1(twe, TWE_PARAM_UNITINFO + twea->twea_unit,
138 	    TWE_PARAM_UNITINFO_Status, &status);
139 	status &= TWE_PARAM_UNITSTATUS_MASK;
140 	if (error) {
141 		snprintf(unkstat, sizeof(unkstat), "<unknown>");
142 		statstr = unkstat;
143 	} else if ((statstr =
144 		    twe_describe_code(twe_table_unitstate, status)) == NULL) {
145 		snprintf(unkstat, sizeof(unkstat), "<status code 0x%02x>",
146 		    status);
147 		statstr = unkstat;
148 	}
149 
150 	aprint_normal(": %s%s, status: %s\n", stripebuf, typestr, statstr);
151 	ldattach(ld);
152 }
153 
154 static int
155 ld_twe_detach(device_t self, int flags)
156 {
157 	struct ld_twe_softc *sc = device_private(self);
158 	struct ld_softc *ld = &sc->sc_ld;
159 	int rv;
160 
161 	if ((rv = ldbegindetach(ld, flags)) != 0)
162 		return (rv);
163 	ldenddetach(ld);
164 
165 	return (0);
166 }
167 
168 static int
169 ld_twe_dobio(struct ld_twe_softc *sc, void *data, int datasize, int blkno,
170 	     int dowrite, struct buf *bp)
171 {
172 	struct twe_ccb *ccb;
173 	struct twe_cmd *tc;
174 	struct twe_softc *twe;
175 	int s, rv, flags;
176 
177 	twe = device_private(device_parent(sc->sc_ld.sc_dv));
178 
179 	flags = (dowrite ? TWE_CCB_DATA_OUT : TWE_CCB_DATA_IN);
180 	if ((ccb = twe_ccb_alloc(twe, flags)) == NULL)
181 		return (EAGAIN);
182 
183 	ccb->ccb_data = data;
184 	ccb->ccb_datasize = datasize;
185 	tc = ccb->ccb_cmd;
186 
187 	/* Build the command. */
188 	tc->tc_size = 3;
189 	tc->tc_unit = sc->sc_hwunit;
190 	tc->tc_count = htole16(datasize / TWE_SECTOR_SIZE);
191 	tc->tc_args.io.lba = htole32(blkno);
192 
193 	if (dowrite)
194 		tc->tc_opcode = TWE_OP_WRITE | (tc->tc_size << 5);
195 	else
196 		tc->tc_opcode = TWE_OP_READ | (tc->tc_size << 5);
197 
198 	/* Map the data transfer. */
199 	if ((rv = twe_ccb_map(twe, ccb)) != 0) {
200 		twe_ccb_free(twe, ccb);
201 		return (rv);
202 	}
203 
204 	if (bp == NULL) {
205 		/*
206 		 * Polled commands must not sit on the software queue.  Wait
207 		 * up to 2 seconds for the command to complete.
208 		 */
209 		s = splbio();
210 		rv = twe_ccb_poll(twe, ccb, 2000);
211 		twe_ccb_unmap(twe, ccb);
212 		twe_ccb_free(twe, ccb);
213 		splx(s);
214 	} else {
215 		ccb->ccb_tx.tx_handler = ld_twe_handler;
216 		ccb->ccb_tx.tx_context = bp;
217 		ccb->ccb_tx.tx_dv = sc->sc_ld.sc_dv;
218 		twe_ccb_enqueue(twe, ccb);
219 		rv = 0;
220 	}
221 
222 	return (rv);
223 }
224 
225 static int
226 ld_twe_start(struct ld_softc *ld, struct buf *bp)
227 {
228 
229 	return (ld_twe_dobio((struct ld_twe_softc *)ld, bp->b_data,
230 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
231 }
232 
233 static void
234 ld_twe_handler(struct twe_ccb *ccb, int error)
235 {
236 	struct buf *bp;
237 	struct twe_context *tx;
238 	struct ld_twe_softc *sc;
239 	struct twe_softc *twe;
240 
241 	tx = &ccb->ccb_tx;
242 	bp = tx->tx_context;
243 	sc = device_private(tx->tx_dv);
244 	twe = device_private(device_parent(sc->sc_ld.sc_dv));
245 
246 	twe_ccb_unmap(twe, ccb);
247 	twe_ccb_free(twe, ccb);
248 
249 	if (error) {
250 		bp->b_error = error;
251 		bp->b_resid = bp->b_bcount;
252 	} else
253 		bp->b_resid = 0;
254 
255 	lddone(&sc->sc_ld, bp);
256 }
257 
258 static int
259 ld_twe_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
260 {
261 
262 	return (ld_twe_dobio((struct ld_twe_softc *)ld, data,
263 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
264 }
265 
266 static int
267 ld_twe_flush(struct ld_softc *ld, int flags)
268 {
269 	struct ld_twe_softc *sc = (void *) ld;
270 	struct twe_softc *twe = device_private(device_parent(ld->sc_dv));
271 	struct twe_ccb *ccb;
272 	struct twe_cmd *tc;
273 	int s, rv;
274 
275 	ccb = twe_ccb_alloc_wait(twe, 0);
276 	KASSERT(ccb != NULL);
277 
278 	ccb->ccb_data = NULL;
279 	ccb->ccb_datasize = 0;
280 
281 	tc = ccb->ccb_cmd;
282 	tc->tc_size = 2;
283 	tc->tc_opcode = TWE_OP_FLUSH;
284 	tc->tc_unit = sc->sc_hwunit;
285 	tc->tc_count = 0;
286 
287 	if (flags & LDFL_POLL) {
288 		/*
289 		 * Polled commands must not sit on the software queue.  Wait
290 		 * up to 2 seconds for the command to complete.
291 		 */
292 		s = splbio();
293 		rv = twe_ccb_poll(twe, ccb, 2000);
294 		twe_ccb_unmap(twe, ccb);
295 		twe_ccb_free(twe, ccb);
296 		splx(s);
297 	} else {
298 		ccb->ccb_tx.tx_handler = twe_ccb_wait_handler;
299 		ccb->ccb_tx.tx_context = NULL;
300 		ccb->ccb_tx.tx_dv = ld->sc_dv;
301 		twe_ccb_enqueue(twe, ccb);
302 
303 		rv = 0;
304 		s = splbio();
305 		while ((ccb->ccb_flags & TWE_CCB_COMPLETE) == 0)
306 			if ((rv = tsleep(ccb, PRIBIO, "tweflush",
307 			    60 * hz)) != 0)
308 				break;
309 		twe_ccb_free(twe, ccb);
310 		splx(s);
311 	}
312 
313 	return (rv);
314 }
315 
316 static void
317 ld_twe_adjqparam(device_t self, int openings)
318 {
319 	struct ld_twe_softc *sc = device_private(self);
320 	struct ld_softc *ld = &sc->sc_ld;
321 
322 	ldadjqparam(ld, openings);
323 }
324