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