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