ld_twa.c revision 1.3 1 1.1 wrstuden /* $wasabi: ld_twa.c,v 1.9 2006/02/14 18:44:37 jordanr Exp $ */
2 1.3 christos /* $NetBSD: ld_twa.c,v 1.3 2006/10/12 01:31:31 christos Exp $ */
3 1.1 wrstuden /*-
4 1.1 wrstuden * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 1.1 wrstuden * All rights reserved.
6 1.1 wrstuden *
7 1.1 wrstuden * This code is derived from software contributed to The NetBSD Foundation
8 1.1 wrstuden * by Jordan Rhody of Wasabi Systems, Inc.
9 1.1 wrstuden *
10 1.1 wrstuden * Redistribution and use in source and binary forms, with or without
11 1.1 wrstuden * modification, are permitted provided that the following conditions
12 1.1 wrstuden * are met:
13 1.1 wrstuden * 1. Redistributions of source code must retain the above copyright
14 1.1 wrstuden * notice, this list of conditions and the following disclaimer.
15 1.1 wrstuden * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 wrstuden * notice, this list of conditions and the following disclaimer in the
17 1.1 wrstuden * documentation and/or other materials provided with the distribution.
18 1.1 wrstuden * 3. All advertising materials mentioning features or use of this software
19 1.1 wrstuden * must display the following acknowledgement:
20 1.1 wrstuden * This product includes software developed by the NetBSD
21 1.1 wrstuden * Foundation, Inc. and its contributors.
22 1.1 wrstuden * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 wrstuden * contributors may be used to endorse or promote products derived
24 1.1 wrstuden * from this software without specific prior written permission.
25 1.1 wrstuden *
26 1.1 wrstuden * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 wrstuden * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 wrstuden * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 wrstuden * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 wrstuden * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 wrstuden * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 wrstuden * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 wrstuden * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 wrstuden * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 wrstuden * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 wrstuden * POSSIBILITY OF SUCH DAMAGE.
37 1.1 wrstuden */
38 1.1 wrstuden
39 1.1 wrstuden /*
40 1.1 wrstuden * 3ware "Apache" RAID controller front-end for ld(4) driver.
41 1.1 wrstuden */
42 1.1 wrstuden
43 1.1 wrstuden #include <sys/cdefs.h>
44 1.3 christos __KERNEL_RCSID(0, "$NetBSD: ld_twa.c,v 1.3 2006/10/12 01:31:31 christos Exp $");
45 1.1 wrstuden
46 1.1 wrstuden #include "rnd.h"
47 1.1 wrstuden
48 1.1 wrstuden #include <sys/param.h>
49 1.1 wrstuden #include <sys/systm.h>
50 1.1 wrstuden #include <sys/kernel.h>
51 1.1 wrstuden #include <sys/device.h>
52 1.1 wrstuden #include <sys/buf.h>
53 1.1 wrstuden #include <sys/bufq.h>
54 1.1 wrstuden #include <sys/endian.h>
55 1.1 wrstuden #include <sys/dkio.h>
56 1.1 wrstuden #include <sys/disk.h>
57 1.1 wrstuden #if NRND > 0
58 1.1 wrstuden #include <sys/rnd.h>
59 1.1 wrstuden #endif
60 1.1 wrstuden
61 1.1 wrstuden #include <machine/bus.h>
62 1.1 wrstuden
63 1.1 wrstuden #include <uvm/uvm_extern.h>
64 1.1 wrstuden
65 1.1 wrstuden #include <dev/ldvar.h>
66 1.1 wrstuden
67 1.1 wrstuden #include <dev/scsipi/scsipi_all.h>
68 1.1 wrstuden #include <dev/scsipi/scsipi_disk.h>
69 1.1 wrstuden #include <dev/scsipi/scsipiconf.h>
70 1.1 wrstuden #include <dev/scsipi/scsi_disk.h>
71 1.1 wrstuden
72 1.1 wrstuden
73 1.1 wrstuden #include <dev/pci/pcireg.h>
74 1.1 wrstuden #include <dev/pci/pcivar.h>
75 1.1 wrstuden #include <dev/pci/twareg.h>
76 1.1 wrstuden #include <dev/pci/twavar.h>
77 1.1 wrstuden
78 1.1 wrstuden struct ld_twa_softc {
79 1.1 wrstuden struct ld_softc sc_ld;
80 1.1 wrstuden int sc_hwunit;
81 1.1 wrstuden };
82 1.1 wrstuden
83 1.1 wrstuden static void ld_twa_attach(struct device *, struct device *, void *);
84 1.1 wrstuden static int ld_twa_detach(struct device *, int);
85 1.1 wrstuden static int ld_twa_dobio(struct ld_twa_softc *, void *, int, int,
86 1.1 wrstuden struct buf *);
87 1.1 wrstuden static int ld_twa_dump(struct ld_softc *, void *, int, int);
88 1.1 wrstuden static int ld_twa_flush(struct ld_softc *);
89 1.1 wrstuden static void ld_twa_handler(struct twa_request *);
90 1.1 wrstuden static int ld_twa_match(struct device *, struct cfdata *, void *);
91 1.1 wrstuden static int ld_twa_start(struct ld_softc *, struct buf *);
92 1.1 wrstuden
93 1.1 wrstuden static void ld_twa_adjqparam(struct device *, int);
94 1.1 wrstuden
95 1.1 wrstuden static int ld_twa_scsicmd(struct ld_twa_softc *,
96 1.1 wrstuden struct twa_request *, struct buf *);
97 1.1 wrstuden
98 1.1 wrstuden CFATTACH_DECL(ld_twa, sizeof(struct ld_twa_softc),
99 1.1 wrstuden ld_twa_match, ld_twa_attach, ld_twa_detach, NULL);
100 1.1 wrstuden
101 1.1 wrstuden static const struct twa_callbacks ld_twa_callbacks = {
102 1.1 wrstuden ld_twa_adjqparam,
103 1.1 wrstuden };
104 1.1 wrstuden
105 1.1 wrstuden static int
106 1.3 christos ld_twa_match(struct device *parent __unused, struct cfdata *match __unused,
107 1.3 christos void *aux __unused)
108 1.1 wrstuden {
109 1.1 wrstuden
110 1.1 wrstuden return (1);
111 1.1 wrstuden }
112 1.1 wrstuden
113 1.1 wrstuden static void
114 1.1 wrstuden ld_twa_attach(struct device *parent, struct device *self, void *aux)
115 1.1 wrstuden {
116 1.1 wrstuden struct twa_attach_args *twa_args;
117 1.1 wrstuden struct ld_twa_softc *sc;
118 1.1 wrstuden struct ld_softc *ld;
119 1.1 wrstuden struct twa_softc *twa;
120 1.1 wrstuden
121 1.1 wrstuden sc = (struct ld_twa_softc *)self;
122 1.1 wrstuden ld = &sc->sc_ld;
123 1.1 wrstuden twa = (struct twa_softc *)parent;
124 1.1 wrstuden twa_args = aux;
125 1.1 wrstuden
126 1.1 wrstuden twa_register_callbacks(twa, twa_args->twaa_unit, &ld_twa_callbacks);
127 1.1 wrstuden
128 1.1 wrstuden sc->sc_hwunit = twa_args->twaa_unit;
129 1.1 wrstuden ld->sc_maxxfer = twa_get_maxxfer(twa_get_maxsegs());
130 1.1 wrstuden ld->sc_secperunit = twa->sc_units[sc->sc_hwunit].td_size;
131 1.1 wrstuden ld->sc_flags = LDF_ENABLED;
132 1.1 wrstuden ld->sc_secsize = TWA_SECTOR_SIZE;
133 1.1 wrstuden ld->sc_maxqueuecnt = twa->sc_openings;
134 1.1 wrstuden ld->sc_start = ld_twa_start;
135 1.1 wrstuden ld->sc_dump = ld_twa_dump;
136 1.1 wrstuden ld->sc_flush = ld_twa_flush;
137 1.1 wrstuden ldattach(ld);
138 1.1 wrstuden }
139 1.1 wrstuden
140 1.1 wrstuden static int
141 1.1 wrstuden ld_twa_detach(struct device *self, int flags)
142 1.1 wrstuden {
143 1.1 wrstuden int error;
144 1.1 wrstuden
145 1.1 wrstuden if ((error = ldbegindetach((struct ld_softc *)self, flags)) != 0)
146 1.1 wrstuden return (error);
147 1.1 wrstuden ldenddetach((struct ld_softc *)self);
148 1.1 wrstuden
149 1.1 wrstuden return (0);
150 1.1 wrstuden }
151 1.1 wrstuden
152 1.1 wrstuden static int
153 1.3 christos ld_twa_dobio(struct ld_twa_softc *sc, void *data, int datasize,
154 1.3 christos int blkno __unused, struct buf *bp)
155 1.1 wrstuden {
156 1.1 wrstuden int rv;
157 1.1 wrstuden struct twa_request *tr;
158 1.1 wrstuden struct twa_softc *twa;
159 1.1 wrstuden
160 1.1 wrstuden twa = (struct twa_softc *)sc->sc_ld.sc_dv.dv_parent;
161 1.1 wrstuden
162 1.1 wrstuden if ((tr = twa_get_request(twa, 0)) == NULL) {
163 1.1 wrstuden return (EAGAIN);
164 1.1 wrstuden }
165 1.1 wrstuden if (bp->b_flags & B_READ) {
166 1.1 wrstuden tr->tr_flags = TWA_CMD_DATA_OUT;
167 1.1 wrstuden } else {
168 1.1 wrstuden tr->tr_flags = TWA_CMD_DATA_IN;
169 1.1 wrstuden }
170 1.1 wrstuden
171 1.1 wrstuden tr->tr_data = data;
172 1.1 wrstuden tr->tr_length = datasize;
173 1.1 wrstuden tr->tr_cmd_pkt_type =
174 1.1 wrstuden (TWA_CMD_PKT_TYPE_9K | TWA_CMD_PKT_TYPE_EXTERNAL);
175 1.1 wrstuden
176 1.1 wrstuden tr->tr_command->cmd_hdr.header_desc.size_header = 128;
177 1.1 wrstuden
178 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.command.opcode =
179 1.1 wrstuden TWA_OP_EXECUTE_SCSI_COMMAND;
180 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.unit =
181 1.1 wrstuden sc->sc_hwunit;
182 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.request_id =
183 1.1 wrstuden tr->tr_request_id;
184 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.status = 0;
185 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.sgl_entries = 1;
186 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.sgl_offset = 16;
187 1.1 wrstuden
188 1.1 wrstuden /* offset from end of hdr = max cdb len */
189 1.1 wrstuden ld_twa_scsicmd(sc, tr, bp);
190 1.1 wrstuden
191 1.1 wrstuden tr->tr_callback = ld_twa_handler;
192 1.1 wrstuden tr->tr_ld_sc = sc;
193 1.1 wrstuden
194 1.1 wrstuden tr->bp = bp;
195 1.1 wrstuden
196 1.1 wrstuden rv = twa_map_request(tr);
197 1.1 wrstuden
198 1.1 wrstuden return (rv);
199 1.1 wrstuden }
200 1.1 wrstuden
201 1.1 wrstuden static int
202 1.1 wrstuden ld_twa_start(struct ld_softc *ld, struct buf *bp)
203 1.1 wrstuden {
204 1.1 wrstuden
205 1.1 wrstuden return (ld_twa_dobio((struct ld_twa_softc *)ld, bp->b_data,
206 1.1 wrstuden bp->b_bcount, bp->b_rawblkno, bp));
207 1.1 wrstuden }
208 1.1 wrstuden
209 1.1 wrstuden static void
210 1.1 wrstuden ld_twa_handler(struct twa_request *tr)
211 1.1 wrstuden {
212 1.1 wrstuden uint8_t status;
213 1.1 wrstuden struct buf *bp;
214 1.1 wrstuden struct ld_twa_softc *sc;
215 1.1 wrstuden struct twa_softc *twa;
216 1.1 wrstuden
217 1.1 wrstuden bp = tr->bp;
218 1.1 wrstuden sc = (struct ld_twa_softc *)tr->tr_ld_sc;
219 1.1 wrstuden twa = (struct twa_softc *)sc->sc_ld.sc_dv.dv_parent;
220 1.1 wrstuden
221 1.1 wrstuden status = tr->tr_command->command.cmd_pkt_9k.status;
222 1.1 wrstuden
223 1.1 wrstuden if (status != 0) {
224 1.1 wrstuden bp->b_flags |= B_ERROR;
225 1.1 wrstuden bp->b_error = EIO;
226 1.1 wrstuden bp->b_resid = bp->b_bcount;
227 1.1 wrstuden } else {
228 1.1 wrstuden bp->b_resid = 0;
229 1.1 wrstuden bp->b_error = 0;
230 1.1 wrstuden }
231 1.1 wrstuden twa_release_request(tr);
232 1.1 wrstuden
233 1.1 wrstuden lddone(&sc->sc_ld, bp);
234 1.1 wrstuden }
235 1.1 wrstuden
236 1.1 wrstuden static int
237 1.1 wrstuden ld_twa_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
238 1.1 wrstuden {
239 1.1 wrstuden
240 1.1 wrstuden return (ld_twa_dobio((struct ld_twa_softc *)ld, data,
241 1.1 wrstuden blkcnt * ld->sc_secsize, blkno, NULL));
242 1.1 wrstuden }
243 1.1 wrstuden
244 1.1 wrstuden
245 1.1 wrstuden static int
246 1.1 wrstuden ld_twa_flush(struct ld_softc *ld)
247 1.1 wrstuden {
248 1.1 wrstuden int s, rv = 0;
249 1.1 wrstuden struct twa_request *tr;
250 1.1 wrstuden struct twa_softc *twa = (void *)ld->sc_dv.dv_parent;
251 1.1 wrstuden struct ld_twa_softc *sc = (void *)ld;
252 1.1 wrstuden struct twa_command_generic *generic_cmd;
253 1.1 wrstuden
254 1.1 wrstuden /* Get a request packet. */
255 1.1 wrstuden tr = twa_get_request_wait(twa, 0);
256 1.1 wrstuden KASSERT(tr != NULL);
257 1.1 wrstuden
258 1.1 wrstuden tr->tr_cmd_pkt_type =
259 1.1 wrstuden (TWA_CMD_PKT_TYPE_9K | TWA_CMD_PKT_TYPE_EXTERNAL);
260 1.1 wrstuden
261 1.1 wrstuden tr->tr_callback = twa_request_wait_handler;
262 1.1 wrstuden tr->tr_ld_sc = sc;
263 1.1 wrstuden
264 1.1 wrstuden tr->tr_command->cmd_hdr.header_desc.size_header = 128;
265 1.1 wrstuden
266 1.1 wrstuden generic_cmd = &(tr->tr_command->command.cmd_pkt_7k.generic);
267 1.1 wrstuden generic_cmd->opcode = TWA_OP_FLUSH;
268 1.1 wrstuden generic_cmd->size = 2;
269 1.1 wrstuden generic_cmd->unit = sc->sc_hwunit;
270 1.1 wrstuden generic_cmd->request_id = tr->tr_request_id;
271 1.1 wrstuden generic_cmd->sgl_offset = 0;
272 1.1 wrstuden generic_cmd->host_id = 0;
273 1.1 wrstuden generic_cmd->status = 0;
274 1.1 wrstuden generic_cmd->flags = 0;
275 1.1 wrstuden generic_cmd->count = 0;
276 1.1 wrstuden rv = twa_map_request(tr);
277 1.1 wrstuden s = splbio();
278 1.1 wrstuden while (tr->tr_status != TWA_CMD_COMPLETE)
279 1.1 wrstuden if ((rv = tsleep(tr, PRIBIO, "twaflush", 60 * hz)) != 0)
280 1.1 wrstuden break;
281 1.1 wrstuden twa_release_request(tr);
282 1.1 wrstuden splx(s);
283 1.1 wrstuden
284 1.1 wrstuden return (rv);
285 1.1 wrstuden }
286 1.1 wrstuden
287 1.1 wrstuden static void
288 1.1 wrstuden ld_twa_adjqparam(struct device *self, int openings)
289 1.1 wrstuden {
290 1.1 wrstuden
291 1.1 wrstuden ldadjqparam((struct ld_softc *)self, openings);
292 1.1 wrstuden }
293 1.1 wrstuden
294 1.1 wrstuden
295 1.1 wrstuden static int
296 1.1 wrstuden ld_twa_scsicmd(struct ld_twa_softc *sc,
297 1.1 wrstuden struct twa_request *tr, struct buf *bp)
298 1.1 wrstuden {
299 1.1 wrstuden if (tr->tr_flags == TWA_CMD_DATA_IN) {
300 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.cdb[0] = WRITE_16;
301 1.1 wrstuden } else {
302 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.cdb[0] = READ_16;
303 1.1 wrstuden }
304 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.cdb[1] =
305 1.1 wrstuden (sc->sc_hwunit << 5); /* lun for CDB */
306 1.1 wrstuden
307 1.1 wrstuden _lto8b(htole64(bp->b_rawblkno),
308 1.1 wrstuden &tr->tr_command->command.cmd_pkt_9k.cdb[2]);
309 1.1 wrstuden _lto4b(htole32((bp->b_bcount / TWA_SECTOR_SIZE)),
310 1.1 wrstuden &tr->tr_command->command.cmd_pkt_9k.cdb[10]);
311 1.1 wrstuden
312 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.cdb[14] = 0;
313 1.1 wrstuden tr->tr_command->command.cmd_pkt_9k.cdb[15] = 0;
314 1.1 wrstuden
315 1.1 wrstuden return (0);
316 1.1 wrstuden }
317