1 /* $NetBSD: ld_twe.c,v 1.40 2017/02/27 21:32:33 jdolecek 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.40 2017/02/27 21:32:33 jdolecek 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 #include <sys/module.h>
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 #include "ioconf.h"
58
59 struct ld_twe_softc {
60 struct ld_softc sc_ld;
61 int sc_hwunit;
62 };
63
64 static void ld_twe_attach(device_t, device_t, void *);
65 static int ld_twe_detach(device_t, int);
66 static int ld_twe_dobio(struct ld_twe_softc *, void *, int, int, int,
67 struct buf *);
68 static int ld_twe_dump(struct ld_softc *, void *, int, int);
69 static int ld_twe_flush(struct ld_softc *, bool);
70 static int ld_twe_ioctl(struct ld_softc *, u_long, void *, int32_t, bool);
71 static void ld_twe_handler(struct twe_ccb *, int);
72 static int ld_twe_match(device_t, cfdata_t, void *);
73 static int ld_twe_start(struct ld_softc *, struct buf *);
74
75 static void ld_twe_adjqparam(device_t, int);
76
77 CFATTACH_DECL_NEW(ld_twe, sizeof(struct ld_twe_softc),
78 ld_twe_match, ld_twe_attach, ld_twe_detach, NULL);
79
80 static const struct twe_callbacks ld_twe_callbacks = {
81 ld_twe_adjqparam,
82 };
83
84 static int
ld_twe_match(device_t parent,cfdata_t match,void * aux)85 ld_twe_match(device_t parent, cfdata_t match, void *aux)
86 {
87
88 return (1);
89 }
90
91 static void
ld_twe_attach(device_t parent,device_t self,void * aux)92 ld_twe_attach(device_t parent, device_t self, void *aux)
93 {
94 struct twe_attach_args *twea = aux;
95 struct ld_twe_softc *sc = device_private(self);
96 struct ld_softc *ld = &sc->sc_ld;
97 struct twe_softc *twe = device_private(parent);
98 struct twe_drive *td = &twe->sc_units[twea->twea_unit];
99 const char *typestr, *stripestr, *statstr;
100 char unktype[16], stripebuf[32], unkstat[32];
101 int error;
102 uint8_t status;
103
104 ld->sc_dv = self;
105
106 twe_register_callbacks(twe, twea->twea_unit, &ld_twe_callbacks);
107
108 sc->sc_hwunit = twea->twea_unit;
109 ld->sc_flags = LDF_ENABLED;
110 ld->sc_maxxfer = twe_get_maxxfer(twe_get_maxsegs());
111 ld->sc_secperunit = td->td_size;
112 ld->sc_secsize = TWE_SECTOR_SIZE;
113 ld->sc_maxqueuecnt = twe->sc_openings;
114 ld->sc_start = ld_twe_start;
115 ld->sc_dump = ld_twe_dump;
116 ld->sc_ioctl = ld_twe_ioctl;
117
118 typestr = twe_describe_code(twe_table_unittype, td->td_type);
119 if (typestr == NULL) {
120 snprintf(unktype, sizeof(unktype), "<0x%02x>", td->td_type);
121 typestr = unktype;
122 }
123 switch (td->td_type) {
124 case TWE_AD_CONFIG_RAID0:
125 case TWE_AD_CONFIG_RAID5:
126 case TWE_AD_CONFIG_RAID10:
127 stripestr = twe_describe_code(twe_table_stripedepth,
128 td->td_stripe);
129 if (stripestr == NULL)
130 snprintf(stripebuf, sizeof(stripebuf),
131 "<stripe code 0x%02x> ", td->td_stripe);
132 else
133 snprintf(stripebuf, sizeof(stripebuf), "%s stripe ",
134 stripestr);
135 break;
136 default:
137 stripebuf[0] = '\0';
138 }
139
140 error = twe_param_get_1(twe, TWE_PARAM_UNITINFO + twea->twea_unit,
141 TWE_PARAM_UNITINFO_Status, &status);
142 status &= TWE_PARAM_UNITSTATUS_MASK;
143 if (error) {
144 snprintf(unkstat, sizeof(unkstat), "<unknown>");
145 statstr = unkstat;
146 } else if ((statstr =
147 twe_describe_code(twe_table_unitstate, status)) == NULL) {
148 snprintf(unkstat, sizeof(unkstat), "<status code 0x%02x>",
149 status);
150 statstr = unkstat;
151 }
152
153 aprint_normal(": %s%s, status: %s\n", stripebuf, typestr, statstr);
154 ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
155 }
156
157 static int
ld_twe_detach(device_t self,int flags)158 ld_twe_detach(device_t self, int flags)
159 {
160 struct ld_twe_softc *sc = device_private(self);
161 struct ld_softc *ld = &sc->sc_ld;
162 int rv;
163
164 if ((rv = ldbegindetach(ld, flags)) != 0)
165 return (rv);
166 ldenddetach(ld);
167
168 return (0);
169 }
170
171 static int
ld_twe_dobio(struct ld_twe_softc * sc,void * data,int datasize,int blkno,int dowrite,struct buf * bp)172 ld_twe_dobio(struct ld_twe_softc *sc, void *data, int datasize, int blkno,
173 int dowrite, struct buf *bp)
174 {
175 struct twe_ccb *ccb;
176 struct twe_cmd *tc;
177 struct twe_softc *twe;
178 int s, rv, flags;
179
180 twe = device_private(device_parent(sc->sc_ld.sc_dv));
181
182 flags = (dowrite ? TWE_CCB_DATA_OUT : TWE_CCB_DATA_IN);
183 if ((ccb = twe_ccb_alloc(twe, flags)) == NULL)
184 return (EAGAIN);
185
186 ccb->ccb_data = data;
187 ccb->ccb_datasize = datasize;
188 tc = ccb->ccb_cmd;
189
190 /* Build the command. */
191 tc->tc_size = 3;
192 tc->tc_unit = sc->sc_hwunit;
193 tc->tc_count = htole16(datasize / TWE_SECTOR_SIZE);
194 tc->tc_args.io.lba = htole32(blkno);
195
196 if (dowrite)
197 tc->tc_opcode = TWE_OP_WRITE | (tc->tc_size << 5);
198 else
199 tc->tc_opcode = TWE_OP_READ | (tc->tc_size << 5);
200
201 /* Map the data transfer. */
202 if ((rv = twe_ccb_map(twe, ccb)) != 0) {
203 twe_ccb_free(twe, ccb);
204 return (rv);
205 }
206
207 if (bp == NULL) {
208 /*
209 * Polled commands must not sit on the software queue. Wait
210 * up to 2 seconds for the command to complete.
211 */
212 s = splbio();
213 rv = twe_ccb_poll(twe, ccb, 2000);
214 twe_ccb_unmap(twe, ccb);
215 twe_ccb_free(twe, ccb);
216 splx(s);
217 } else {
218 ccb->ccb_tx.tx_handler = ld_twe_handler;
219 ccb->ccb_tx.tx_context = bp;
220 ccb->ccb_tx.tx_dv = sc->sc_ld.sc_dv;
221 twe_ccb_enqueue(twe, ccb);
222 rv = 0;
223 }
224
225 return (rv);
226 }
227
228 static int
ld_twe_start(struct ld_softc * ld,struct buf * bp)229 ld_twe_start(struct ld_softc *ld, struct buf *bp)
230 {
231
232 return (ld_twe_dobio((struct ld_twe_softc *)ld, bp->b_data,
233 bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
234 }
235
236 static void
ld_twe_handler(struct twe_ccb * ccb,int error)237 ld_twe_handler(struct twe_ccb *ccb, int error)
238 {
239 struct buf *bp;
240 struct twe_context *tx;
241 struct ld_twe_softc *sc;
242 struct twe_softc *twe;
243
244 tx = &ccb->ccb_tx;
245 bp = tx->tx_context;
246 sc = device_private(tx->tx_dv);
247 twe = device_private(device_parent(sc->sc_ld.sc_dv));
248
249 twe_ccb_unmap(twe, ccb);
250 twe_ccb_free(twe, ccb);
251
252 if (error) {
253 bp->b_error = error;
254 bp->b_resid = bp->b_bcount;
255 } else
256 bp->b_resid = 0;
257
258 lddone(&sc->sc_ld, bp);
259 }
260
261 static int
ld_twe_dump(struct ld_softc * ld,void * data,int blkno,int blkcnt)262 ld_twe_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
263 {
264
265 return (ld_twe_dobio((struct ld_twe_softc *)ld, data,
266 blkcnt * ld->sc_secsize, blkno, 1, NULL));
267 }
268
269 static int
ld_twe_flush(struct ld_softc * ld,bool poll)270 ld_twe_flush(struct ld_softc *ld, bool poll)
271 {
272 struct ld_twe_softc *sc = (void *) ld;
273 struct twe_softc *twe = device_private(device_parent(ld->sc_dv));
274 struct twe_ccb *ccb;
275 struct twe_cmd *tc;
276 int s, rv;
277
278 ccb = twe_ccb_alloc_wait(twe, 0);
279 KASSERT(ccb != NULL);
280
281 ccb->ccb_data = NULL;
282 ccb->ccb_datasize = 0;
283
284 tc = ccb->ccb_cmd;
285 tc->tc_size = 2;
286 tc->tc_opcode = TWE_OP_FLUSH;
287 tc->tc_unit = sc->sc_hwunit;
288 tc->tc_count = 0;
289
290 if (poll) {
291 /*
292 * Polled commands must not sit on the software queue. Wait
293 * up to 2 seconds for the command to complete.
294 */
295 s = splbio();
296 rv = twe_ccb_poll(twe, ccb, 2000);
297 twe_ccb_unmap(twe, ccb);
298 twe_ccb_free(twe, ccb);
299 splx(s);
300 } else {
301 ccb->ccb_tx.tx_handler = twe_ccb_wait_handler;
302 ccb->ccb_tx.tx_context = NULL;
303 ccb->ccb_tx.tx_dv = ld->sc_dv;
304 twe_ccb_enqueue(twe, ccb);
305
306 rv = 0;
307 s = splbio();
308 while ((ccb->ccb_flags & TWE_CCB_COMPLETE) == 0)
309 if ((rv = tsleep(ccb, PRIBIO, "tweflush",
310 60 * hz)) != 0)
311 break;
312 twe_ccb_free(twe, ccb);
313 splx(s);
314 }
315
316 return (rv);
317 }
318
319 static int
ld_twe_ioctl(struct ld_softc * ld,u_long cmd,void * addr,int32_t flag,bool poll)320 ld_twe_ioctl(struct ld_softc *ld, u_long cmd, void *addr, int32_t flag, bool poll)
321 {
322 int error;
323
324 switch (cmd) {
325 case DIOCCACHESYNC:
326 error = ld_twe_flush(ld, poll);
327 break;
328
329 default:
330 error = EPASSTHROUGH;
331 break;
332 }
333
334 return error;
335 }
336
337 static void
ld_twe_adjqparam(device_t self,int openings)338 ld_twe_adjqparam(device_t self, int openings)
339 {
340 struct ld_twe_softc *sc = device_private(self);
341 struct ld_softc *ld = &sc->sc_ld;
342
343 ldadjqparam(ld, openings);
344 }
345
346 MODULE(MODULE_CLASS_DRIVER, ld_twe, "ld,twe");
347
348 #ifdef _MODULE
349 /*
350 * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
351 * XXX it will be defined in the common-code module
352 */
353 #undef CFDRIVER_DECL
354 #define CFDRIVER_DECL(name, class, attr)
355 #include "ioconf.c"
356 #endif
357
358 static int
ld_twe_modcmd(modcmd_t cmd,void * opaque)359 ld_twe_modcmd(modcmd_t cmd, void *opaque)
360 {
361 #ifdef _MODULE
362 /*
363 * We ignore the cfdriver_vec[] that ioconf provides, since
364 * the cfdrivers are attached already.
365 */
366 static struct cfdriver * const no_cfdriver_vec[] = { NULL };
367 #endif
368 int error = 0;
369
370 #ifdef _MODULE
371 switch (cmd) {
372 case MODULE_CMD_INIT:
373 error = config_init_component(no_cfdriver_vec,
374 cfattach_ioconf_ld_twe, cfdata_ioconf_ld_twe);
375 break;
376 case MODULE_CMD_FINI:
377 error = config_fini_component(no_cfdriver_vec,
378 cfattach_ioconf_ld_twe, cfdata_ioconf_ld_twe);
379 break;
380 default:
381 error = ENOTTY;
382 break;
383 }
384 #endif
385
386 return error;
387 }
388