ld_twe.c revision 1.21 1 /* $NetBSD: ld_twe.c,v 1.21 2004/10/28 07:07:41 yamt 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * 3ware "Escalade" RAID controller front-end for ld(4) driver.
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: ld_twe.c,v 1.21 2004/10/28 07:07:41 yamt Exp $");
45
46 #include "rnd.h"
47
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/device.h>
52 #include <sys/buf.h>
53 #include <sys/bufq.h>
54 #include <sys/endian.h>
55 #include <sys/dkio.h>
56 #include <sys/disk.h>
57 #if NRND > 0
58 #include <sys/rnd.h>
59 #endif
60
61 #include <machine/bus.h>
62
63 #include <uvm/uvm_extern.h>
64
65 #include <dev/ldvar.h>
66
67 #include <dev/pci/twereg.h>
68 #include <dev/pci/twevar.h>
69
70 struct ld_twe_softc {
71 struct ld_softc sc_ld;
72 int sc_hwunit;
73 };
74
75 static void ld_twe_attach(struct device *, struct device *, void *);
76 static int ld_twe_detach(struct device *, int);
77 static int ld_twe_dobio(struct ld_twe_softc *, void *, int, int, int,
78 struct buf *);
79 static int ld_twe_dump(struct ld_softc *, void *, int, int);
80 static int ld_twe_flush(struct ld_softc *);
81 static void ld_twe_handler(struct twe_ccb *, int);
82 static int ld_twe_match(struct device *, struct cfdata *, void *);
83 static int ld_twe_start(struct ld_softc *, struct buf *);
84
85 static void ld_twe_adjqparam(struct device *, int);
86
87 CFATTACH_DECL(ld_twe, sizeof(struct ld_twe_softc),
88 ld_twe_match, ld_twe_attach, ld_twe_detach, NULL);
89
90 static const struct twe_callbacks ld_twe_callbacks = {
91 ld_twe_adjqparam,
92 };
93
94 static int
95 ld_twe_match(struct device *parent, struct cfdata *match, void *aux)
96 {
97
98 return (1);
99 }
100
101 static void
102 ld_twe_attach(struct device *parent, struct device *self, void *aux)
103 {
104 struct twe_attach_args *twea;
105 struct ld_twe_softc *sc;
106 struct ld_softc *ld;
107 struct twe_softc *twe;
108 struct twe_drive *td;
109 const char *typestr, *stripestr, *statstr;
110 char unktype[16], stripebuf[32], unkstat[32];
111 int error;
112 uint8_t status;
113
114 sc = (struct ld_twe_softc *)self;
115 ld = &sc->sc_ld;
116 twe = (struct twe_softc *)parent;
117 twea = aux;
118 td = &twe->sc_units[twea->twea_unit];
119
120 twe_register_callbacks(twe, twea->twea_unit, &ld_twe_callbacks);
121
122 sc->sc_hwunit = twea->twea_unit;
123 ld->sc_flags = LDF_ENABLED;
124 ld->sc_maxxfer = twe_get_maxxfer(twe_get_maxsegs());
125 ld->sc_secperunit = td->td_size;
126 ld->sc_secsize = TWE_SECTOR_SIZE;
127 ld->sc_maxqueuecnt = twe->sc_openings;
128 ld->sc_start = ld_twe_start;
129 ld->sc_dump = ld_twe_dump;
130 ld->sc_flush = ld_twe_flush;
131
132 typestr = twe_describe_code(twe_table_unittype, td->td_type);
133 if (typestr == NULL) {
134 snprintf(unktype, sizeof(unktype), "<0x%02x>", td->td_type);
135 typestr = unktype;
136 }
137 switch (td->td_type) {
138 case TWE_AD_CONFIG_RAID0:
139 case TWE_AD_CONFIG_RAID5:
140 case TWE_AD_CONFIG_RAID10:
141 stripestr = twe_describe_code(twe_table_stripedepth,
142 td->td_stripe);
143 if (stripestr == NULL)
144 snprintf(stripebuf, sizeof(stripebuf),
145 "<stripe code 0x%02x> ", td->td_stripe);
146 else
147 snprintf(stripebuf, sizeof(stripebuf), "%s stripe ",
148 stripestr);
149 break;
150 default:
151 stripebuf[0] = '\0';
152 }
153
154 error = twe_param_get_1(twe, TWE_PARAM_UNITINFO + twea->twea_unit,
155 TWE_PARAM_UNITINFO_Status, &status);
156 status &= TWE_PARAM_UNITSTATUS_MASK;
157 if (error) {
158 snprintf(unkstat, sizeof(unkstat), "<unknown>");
159 statstr = unkstat;
160 } else if ((statstr =
161 twe_describe_code(twe_table_unitstate, status)) == NULL) {
162 snprintf(unkstat, sizeof(unkstat), "<status code 0x%02x>",
163 status);
164 statstr = unkstat;
165 }
166
167 printf(": %s%s, status: %s\n", stripebuf, typestr, statstr);
168 ldattach(ld);
169 }
170
171 static int
172 ld_twe_detach(struct device *self, int flags)
173 {
174 int rv;
175
176 if ((rv = ldbegindetach((struct ld_softc *)self, flags)) != 0)
177 return (rv);
178 ldenddetach((struct ld_softc *)self);
179
180 return (0);
181 }
182
183 static int
184 ld_twe_dobio(struct ld_twe_softc *sc, void *data, int datasize, int blkno,
185 int dowrite, struct buf *bp)
186 {
187 struct twe_ccb *ccb;
188 struct twe_cmd *tc;
189 struct twe_softc *twe;
190 int s, rv, flags;
191
192 twe = (struct twe_softc *)sc->sc_ld.sc_dv.dv_parent;
193
194 flags = (dowrite ? TWE_CCB_DATA_OUT : TWE_CCB_DATA_IN);
195 if ((ccb = twe_ccb_alloc(twe, flags)) == NULL)
196 return (EAGAIN);
197
198 ccb->ccb_data = data;
199 ccb->ccb_datasize = datasize;
200 tc = ccb->ccb_cmd;
201
202 /* Build the command. */
203 tc->tc_size = 3;
204 tc->tc_unit = sc->sc_hwunit;
205 tc->tc_count = htole16(datasize / TWE_SECTOR_SIZE);
206 tc->tc_args.io.lba = htole32(blkno);
207
208 if (dowrite)
209 tc->tc_opcode = TWE_OP_WRITE | (tc->tc_size << 5);
210 else
211 tc->tc_opcode = TWE_OP_READ | (tc->tc_size << 5);
212
213 /* Map the data transfer. */
214 if ((rv = twe_ccb_map(twe, ccb)) != 0) {
215 twe_ccb_free(twe, ccb);
216 return (rv);
217 }
218
219 if (bp == NULL) {
220 /*
221 * Polled commands must not sit on the software queue. Wait
222 * up to 2 seconds for the command to complete.
223 */
224 s = splbio();
225 rv = twe_ccb_poll(twe, ccb, 2000);
226 twe_ccb_unmap(twe, ccb);
227 twe_ccb_free(twe, ccb);
228 splx(s);
229 } else {
230 ccb->ccb_tx.tx_handler = ld_twe_handler;
231 ccb->ccb_tx.tx_context = bp;
232 ccb->ccb_tx.tx_dv = (struct device *)sc;
233 twe_ccb_enqueue(twe, ccb);
234 rv = 0;
235 }
236
237 return (rv);
238 }
239
240 static int
241 ld_twe_start(struct ld_softc *ld, struct buf *bp)
242 {
243
244 return (ld_twe_dobio((struct ld_twe_softc *)ld, bp->b_data,
245 bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
246 }
247
248 static void
249 ld_twe_handler(struct twe_ccb *ccb, int error)
250 {
251 struct buf *bp;
252 struct twe_context *tx;
253 struct ld_twe_softc *sc;
254 struct twe_softc *twe;
255
256 tx = &ccb->ccb_tx;
257 bp = tx->tx_context;
258 sc = (struct ld_twe_softc *)tx->tx_dv;
259 twe = (struct twe_softc *)sc->sc_ld.sc_dv.dv_parent;
260
261 twe_ccb_unmap(twe, ccb);
262 twe_ccb_free(twe, ccb);
263
264 if (error) {
265 bp->b_flags |= B_ERROR;
266 bp->b_error = error;
267 bp->b_resid = bp->b_bcount;
268 } else
269 bp->b_resid = 0;
270
271 lddone(&sc->sc_ld, bp);
272 }
273
274 static int
275 ld_twe_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
276 {
277
278 return (ld_twe_dobio((struct ld_twe_softc *)ld, data,
279 blkcnt * ld->sc_secsize, blkno, 1, NULL));
280 }
281
282 static int
283 ld_twe_flush(struct ld_softc *ld)
284 {
285 struct ld_twe_softc *sc = (void *) ld;
286 struct twe_softc *twe = (void *) ld->sc_dv.dv_parent;
287 struct twe_ccb *ccb;
288 struct twe_cmd *tc;
289 int s, rv;
290
291 ccb = twe_ccb_alloc_wait(twe, 0);
292 KASSERT(ccb != NULL);
293
294 ccb->ccb_data = NULL;
295 ccb->ccb_datasize = 0;
296 ccb->ccb_tx.tx_handler = twe_ccb_wait_handler;
297 ccb->ccb_tx.tx_context = NULL;
298 ccb->ccb_tx.tx_dv = &ld->sc_dv;
299
300 tc = ccb->ccb_cmd;
301 tc->tc_size = 2;
302 tc->tc_opcode = TWE_OP_FLUSH;
303 tc->tc_unit = sc->sc_hwunit;
304 tc->tc_count = 0;
305
306 rv = 0;
307 twe_ccb_enqueue(twe, ccb);
308 s = splbio();
309 while ((ccb->ccb_flags & TWE_CCB_COMPLETE) == 0)
310 if ((rv = tsleep(ccb, PRIBIO, "tweflush", 60 * hz)) != 0)
311 break;
312 twe_ccb_free(twe, ccb);
313 splx(s);
314
315 return (rv);
316 }
317
318 static void
319 ld_twe_adjqparam(struct device *self, int openings)
320 {
321
322 ldadjqparam((struct ld_softc *)self, openings);
323 }
324