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