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