xref: /netbsd/sys/dev/pci/ld_twe.c (revision bf9ec67e)
1 /*	$NetBSD: ld_twe.c,v 1.10 2002/05/24 18:10:06 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2001, 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
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 "Escalade" RAID controller front-end for ld(4) driver.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: ld_twe.c,v 1.10 2002/05/24 18:10:06 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/endian.h>
54 #include <sys/dkio.h>
55 #include <sys/disk.h>
56 #if NRND > 0
57 #include <sys/rnd.h>
58 #endif
59 
60 #include <machine/bus.h>
61 
62 #include <uvm/uvm_extern.h>
63 
64 #include <dev/ldvar.h>
65 
66 #include <dev/pci/twereg.h>
67 #include <dev/pci/twevar.h>
68 
69 struct ld_twe_softc {
70 	struct	ld_softc sc_ld;
71 	int	sc_hwunit;
72 };
73 
74 static void	ld_twe_attach(struct device *, struct device *, void *);
75 static int	ld_twe_dobio(struct ld_twe_softc *, void *, int, int, int,
76 			     struct buf *);
77 static int	ld_twe_dump(struct ld_softc *, void *, int, int);
78 static void	ld_twe_handler(struct twe_ccb *, int);
79 static int	ld_twe_match(struct device *, struct cfdata *, void *);
80 static int	ld_twe_start(struct ld_softc *, struct buf *);
81 
82 struct cfattach ld_twe_ca = {
83 	sizeof(struct ld_twe_softc), ld_twe_match, ld_twe_attach
84 };
85 
86 static int
87 ld_twe_match(struct device *parent, struct cfdata *match, void *aux)
88 {
89 
90 	return (1);
91 }
92 
93 static void
94 ld_twe_attach(struct device *parent, struct device *self, void *aux)
95 {
96 	struct twe_attach_args *twea;
97 	struct ld_twe_softc *sc;
98 	struct ld_softc *ld;
99 	struct twe_softc *twe;
100 
101 	sc = (struct ld_twe_softc *)self;
102 	ld = &sc->sc_ld;
103 	twe = (struct twe_softc *)parent;
104 	twea = aux;
105 
106 	sc->sc_hwunit = twea->twea_unit;
107 	ld->sc_flags = LDF_ENABLED;
108 	ld->sc_maxxfer = twe_get_maxxfer(twe_get_maxsegs());
109 	ld->sc_secperunit = twe->sc_dsize[twea->twea_unit];
110 	ld->sc_secsize = TWE_SECTOR_SIZE;
111 	ld->sc_maxqueuecnt = (TWE_MAX_QUEUECNT - 1) / twe->sc_nunits;
112 	ld->sc_start = ld_twe_start;
113 	ld->sc_dump = ld_twe_dump;
114 
115 	printf("\n");
116 	ldattach(ld);
117 }
118 
119 static int
120 ld_twe_dobio(struct ld_twe_softc *sc, void *data, int datasize, int blkno,
121 	     int dowrite, struct buf *bp)
122 {
123 	struct twe_ccb *ccb;
124 	struct twe_cmd *tc;
125 	struct twe_softc *twe;
126 	int s, rv, flags;
127 
128 	twe = (struct twe_softc *)sc->sc_ld.sc_dv.dv_parent;
129 
130 	flags = (dowrite ? TWE_CCB_DATA_OUT : TWE_CCB_DATA_IN);
131 	if ((rv = twe_ccb_alloc(twe, &ccb, flags)) != 0)
132 		return (rv);
133 
134 	ccb->ccb_data = data;
135 	ccb->ccb_datasize = datasize;
136 	tc = ccb->ccb_cmd;
137 
138 	/* Build the command. */
139 	tc->tc_size = 3;
140 	tc->tc_unit = sc->sc_hwunit;
141 	tc->tc_count = htole16(datasize / TWE_SECTOR_SIZE);
142 	tc->tc_args.io.lba = htole32(blkno);
143 
144 	if (dowrite)
145 		tc->tc_opcode = TWE_OP_WRITE | (tc->tc_size << 5);
146 	else
147 		tc->tc_opcode = TWE_OP_READ | (tc->tc_size << 5);
148 
149 	/* Map the data transfer. */
150 	if ((rv = twe_ccb_map(twe, ccb)) != 0) {
151 		twe_ccb_free(twe, ccb);
152 		return (rv);
153 	}
154 
155 	if (bp == NULL) {
156 		/*
157 		 * Polled commands must not sit on the software queue.  Wait
158 		 * up to 2 seconds for the command to complete.
159 		 */
160 		s = splbio();
161 		rv = twe_ccb_poll(twe, ccb, 2000);
162 		twe_ccb_unmap(twe, ccb);
163 		twe_ccb_free(twe, ccb);
164 		splx(s);
165 	} else {
166 		ccb->ccb_tx.tx_handler = ld_twe_handler;
167 		ccb->ccb_tx.tx_context = bp;
168 		ccb->ccb_tx.tx_dv = (struct device *)sc;
169 		twe_ccb_enqueue(twe, ccb);
170 		rv = 0;
171 	}
172 
173 	return (rv);
174 }
175 
176 static int
177 ld_twe_start(struct ld_softc *ld, struct buf *bp)
178 {
179 
180 	return (ld_twe_dobio((struct ld_twe_softc *)ld, bp->b_data,
181 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
182 }
183 
184 static void
185 ld_twe_handler(struct twe_ccb *ccb, int error)
186 {
187 	struct buf *bp;
188 	struct twe_context *tx;
189 	struct ld_twe_softc *sc;
190 	struct twe_softc *twe;
191 
192 	tx = &ccb->ccb_tx;
193 	bp = tx->tx_context;
194 	sc = (struct ld_twe_softc *)tx->tx_dv;
195 	twe = (struct twe_softc *)sc->sc_ld.sc_dv.dv_parent;
196 
197 	twe_ccb_unmap(twe, ccb);
198 	twe_ccb_free(twe, ccb);
199 
200 	if (error) {
201 		bp->b_flags |= B_ERROR;
202 		bp->b_error = error;
203 		bp->b_resid = bp->b_bcount;
204 	} else
205 		bp->b_resid = 0;
206 
207 	lddone(&sc->sc_ld, bp);
208 }
209 
210 static int
211 ld_twe_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
212 {
213 
214 	return (ld_twe_dobio((struct ld_twe_softc *)ld, data,
215 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
216 }
217