ld_twe.c revision 1.7.8.2 1 1.7.8.2 he /* $NetBSD: ld_twe.c,v 1.7.8.2 2001/10/25 17:54:07 he Exp $ */
2 1.7.8.2 he
3 1.7.8.2 he /*-
4 1.7.8.2 he * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.7.8.2 he * All rights reserved.
6 1.7.8.2 he *
7 1.7.8.2 he * This code is derived from software contributed to The NetBSD Foundation
8 1.7.8.2 he * by Andrew Doran.
9 1.7.8.2 he *
10 1.7.8.2 he * Redistribution and use in source and binary forms, with or without
11 1.7.8.2 he * modification, are permitted provided that the following conditions
12 1.7.8.2 he * are met:
13 1.7.8.2 he * 1. Redistributions of source code must retain the above copyright
14 1.7.8.2 he * notice, this list of conditions and the following disclaimer.
15 1.7.8.2 he * 2. Redistributions in binary form must reproduce the above copyright
16 1.7.8.2 he * notice, this list of conditions and the following disclaimer in the
17 1.7.8.2 he * documentation and/or other materials provided with the distribution.
18 1.7.8.2 he * 3. All advertising materials mentioning features or use of this software
19 1.7.8.2 he * must display the following acknowledgement:
20 1.7.8.2 he * This product includes software developed by the NetBSD
21 1.7.8.2 he * Foundation, Inc. and its contributors.
22 1.7.8.2 he * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.7.8.2 he * contributors may be used to endorse or promote products derived
24 1.7.8.2 he * from this software without specific prior written permission.
25 1.7.8.2 he *
26 1.7.8.2 he * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.7.8.2 he * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.7.8.2 he * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.7.8.2 he * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.7.8.2 he * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.7.8.2 he * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.7.8.2 he * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.7.8.2 he * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.7.8.2 he * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.7.8.2 he * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.7.8.2 he * POSSIBILITY OF SUCH DAMAGE.
37 1.7.8.2 he */
38 1.7.8.2 he
39 1.7.8.2 he /*
40 1.7.8.2 he * 3ware "Escalade" RAID controller front-end for ld(4) driver.
41 1.7.8.2 he */
42 1.7.8.2 he
43 1.7.8.2 he #include "rnd.h"
44 1.7.8.2 he
45 1.7.8.2 he #include <sys/param.h>
46 1.7.8.2 he #include <sys/systm.h>
47 1.7.8.2 he #include <sys/kernel.h>
48 1.7.8.2 he #include <sys/device.h>
49 1.7.8.2 he #include <sys/buf.h>
50 1.7.8.2 he #include <sys/endian.h>
51 1.7.8.2 he #include <sys/dkio.h>
52 1.7.8.2 he #include <sys/disk.h>
53 1.7.8.2 he #if NRND > 0
54 1.7.8.2 he #include <sys/rnd.h>
55 1.7.8.2 he #endif
56 1.7.8.2 he
57 1.7.8.2 he #include <machine/bus.h>
58 1.7.8.2 he
59 1.7.8.2 he #include <dev/ldvar.h>
60 1.7.8.2 he
61 1.7.8.2 he #include <dev/pci/twereg.h>
62 1.7.8.2 he #include <dev/pci/twevar.h>
63 1.7.8.2 he
64 1.7.8.2 he struct ld_twe_softc {
65 1.7.8.2 he struct ld_softc sc_ld;
66 1.7.8.2 he int sc_hwunit;
67 1.7.8.2 he };
68 1.7.8.2 he
69 1.7.8.2 he static void ld_twe_attach(struct device *, struct device *, void *);
70 1.7.8.2 he static int ld_twe_dobio(struct ld_twe_softc *, void *, int, int, int,
71 1.7.8.2 he struct buf *);
72 1.7.8.2 he static int ld_twe_dump(struct ld_softc *, void *, int, int);
73 1.7.8.2 he static void ld_twe_handler(struct twe_ccb *, int);
74 1.7.8.2 he static int ld_twe_match(struct device *, struct cfdata *, void *);
75 1.7.8.2 he static int ld_twe_start(struct ld_softc *, struct buf *);
76 1.7.8.2 he
77 1.7.8.2 he struct cfattach ld_twe_ca = {
78 1.7.8.2 he sizeof(struct ld_twe_softc), ld_twe_match, ld_twe_attach
79 1.7.8.2 he };
80 1.7.8.2 he
81 1.7.8.2 he static int
82 1.7.8.2 he ld_twe_match(struct device *parent, struct cfdata *match, void *aux)
83 1.7.8.2 he {
84 1.7.8.2 he
85 1.7.8.2 he return (1);
86 1.7.8.2 he }
87 1.7.8.2 he
88 1.7.8.2 he static void
89 1.7.8.2 he ld_twe_attach(struct device *parent, struct device *self, void *aux)
90 1.7.8.2 he {
91 1.7.8.2 he struct twe_attach_args *twea;
92 1.7.8.2 he struct ld_twe_softc *sc;
93 1.7.8.2 he struct ld_softc *ld;
94 1.7.8.2 he struct twe_softc *twe;
95 1.7.8.2 he
96 1.7.8.2 he sc = (struct ld_twe_softc *)self;
97 1.7.8.2 he ld = &sc->sc_ld;
98 1.7.8.2 he twe = (struct twe_softc *)parent;
99 1.7.8.2 he twea = aux;
100 1.7.8.2 he
101 1.7.8.2 he sc->sc_hwunit = twea->twea_unit;
102 1.7.8.2 he ld->sc_flags = LDF_ENABLED;
103 1.7.8.2 he ld->sc_maxxfer = TWE_MAX_XFER;
104 1.7.8.2 he ld->sc_secperunit = twe->sc_dsize[twea->twea_unit];
105 1.7.8.2 he ld->sc_secsize = TWE_SECTOR_SIZE;
106 1.7.8.2 he ld->sc_maxqueuecnt = (TWE_MAX_QUEUECNT - 1) / twe->sc_nunits;
107 1.7.8.2 he ld->sc_start = ld_twe_start;
108 1.7.8.2 he ld->sc_dump = ld_twe_dump;
109 1.7.8.2 he
110 1.7.8.2 he printf("\n");
111 1.7.8.2 he ldattach(ld);
112 1.7.8.2 he }
113 1.7.8.2 he
114 1.7.8.2 he static int
115 1.7.8.2 he ld_twe_dobio(struct ld_twe_softc *sc, void *data, int datasize, int blkno,
116 1.7.8.2 he int dowrite, struct buf *bp)
117 1.7.8.2 he {
118 1.7.8.2 he struct twe_ccb *ccb;
119 1.7.8.2 he struct twe_cmd *tc;
120 1.7.8.2 he struct twe_softc *twe;
121 1.7.8.2 he int s, rv, flags;
122 1.7.8.2 he
123 1.7.8.2 he twe = (struct twe_softc *)sc->sc_ld.sc_dv.dv_parent;
124 1.7.8.2 he
125 1.7.8.2 he flags = (dowrite ? TWE_CCB_DATA_OUT : TWE_CCB_DATA_IN);
126 1.7.8.2 he if ((rv = twe_ccb_alloc(twe, &ccb, flags)) != 0)
127 1.7.8.2 he return (rv);
128 1.7.8.2 he
129 1.7.8.2 he ccb->ccb_data = data;
130 1.7.8.2 he ccb->ccb_datasize = datasize;
131 1.7.8.2 he tc = ccb->ccb_cmd;
132 1.7.8.2 he
133 1.7.8.2 he /* Build the command. */
134 1.7.8.2 he tc->tc_size = 3;
135 1.7.8.2 he tc->tc_unit = sc->sc_hwunit;
136 1.7.8.2 he tc->tc_count = htole16(datasize / TWE_SECTOR_SIZE);
137 1.7.8.2 he tc->tc_args.io.lba = htole32(blkno);
138 1.7.8.2 he
139 1.7.8.2 he if (dowrite)
140 1.7.8.2 he tc->tc_opcode = TWE_OP_WRITE | (tc->tc_size << 5);
141 1.7.8.2 he else
142 1.7.8.2 he tc->tc_opcode = TWE_OP_READ | (tc->tc_size << 5);
143 1.7.8.2 he
144 1.7.8.2 he /* Map the data transfer. */
145 1.7.8.2 he if ((rv = twe_ccb_map(twe, ccb)) != 0) {
146 1.7.8.2 he twe_ccb_free(twe, ccb);
147 1.7.8.2 he return (rv);
148 1.7.8.2 he }
149 1.7.8.2 he
150 1.7.8.2 he if (bp == NULL) {
151 1.7.8.2 he /*
152 1.7.8.2 he * Polled commands must not sit on the software queue. Wait
153 1.7.8.2 he * up to 2 seconds for the command to complete.
154 1.7.8.2 he */
155 1.7.8.2 he s = splbio();
156 1.7.8.2 he rv = twe_ccb_poll(twe, ccb, 2000);
157 1.7.8.2 he twe_ccb_unmap(twe, ccb);
158 1.7.8.2 he twe_ccb_free(twe, ccb);
159 1.7.8.2 he splx(s);
160 1.7.8.2 he } else {
161 1.7.8.2 he ccb->ccb_tx.tx_handler = ld_twe_handler;
162 1.7.8.2 he ccb->ccb_tx.tx_context = bp;
163 1.7.8.2 he ccb->ccb_tx.tx_dv = (struct device *)sc;
164 1.7.8.2 he twe_ccb_enqueue(twe, ccb);
165 1.7.8.2 he rv = 0;
166 1.7.8.2 he }
167 1.7.8.2 he
168 1.7.8.2 he return (rv);
169 1.7.8.2 he }
170 1.7.8.2 he
171 1.7.8.2 he static int
172 1.7.8.2 he ld_twe_start(struct ld_softc *ld, struct buf *bp)
173 1.7.8.2 he {
174 1.7.8.2 he
175 1.7.8.2 he return (ld_twe_dobio((struct ld_twe_softc *)ld, bp->b_data,
176 1.7.8.2 he bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
177 1.7.8.2 he }
178 1.7.8.2 he
179 1.7.8.2 he static void
180 1.7.8.2 he ld_twe_handler(struct twe_ccb *ccb, int error)
181 1.7.8.2 he {
182 1.7.8.2 he struct buf *bp;
183 1.7.8.2 he struct twe_context *tx;
184 1.7.8.2 he struct ld_twe_softc *sc;
185 1.7.8.2 he struct twe_softc *twe;
186 1.7.8.2 he
187 1.7.8.2 he tx = &ccb->ccb_tx;
188 1.7.8.2 he bp = tx->tx_context;
189 1.7.8.2 he sc = (struct ld_twe_softc *)tx->tx_dv;
190 1.7.8.2 he twe = (struct twe_softc *)sc->sc_ld.sc_dv.dv_parent;
191 1.7.8.2 he
192 1.7.8.2 he twe_ccb_unmap(twe, ccb);
193 1.7.8.2 he twe_ccb_free(twe, ccb);
194 1.7.8.2 he
195 1.7.8.2 he if (error) {
196 1.7.8.2 he bp->b_flags |= B_ERROR;
197 1.7.8.2 he bp->b_error = error;
198 1.7.8.2 he bp->b_resid = bp->b_bcount;
199 1.7.8.2 he } else
200 1.7.8.2 he bp->b_resid = 0;
201 1.7.8.2 he
202 1.7.8.2 he lddone(&sc->sc_ld, bp);
203 1.7.8.2 he }
204 1.7.8.2 he
205 1.7.8.2 he static int
206 1.7.8.2 he ld_twe_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
207 1.7.8.2 he {
208 1.7.8.2 he
209 1.7.8.2 he return (ld_twe_dobio((struct ld_twe_softc *)ld, data,
210 1.7.8.2 he blkcnt * ld->sc_secsize, blkno, 1, NULL));
211 1.7.8.2 he }
212