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