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