xref: /netbsd/sys/dev/ic/ld_aac.c (revision c4a72b64)
1 /*	$NetBSD: ld_aac.c,v 1.5 2002/10/02 16:33:33 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 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 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: ld_aac.c,v 1.5 2002/10/02 16:33:33 thorpej Exp $");
41 
42 #include "rnd.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/kernel.h>
47 #include <sys/device.h>
48 #include <sys/buf.h>
49 #include <sys/endian.h>
50 #include <sys/dkio.h>
51 #include <sys/disk.h>
52 #if NRND > 0
53 #include <sys/rnd.h>
54 #endif
55 
56 #include <machine/bus.h>
57 
58 #include <uvm/uvm_extern.h>
59 
60 #include <dev/ldvar.h>
61 
62 #include <dev/ic/aacreg.h>
63 #include <dev/ic/aacvar.h>
64 
65 struct ld_aac_softc {
66 	struct	ld_softc sc_ld;
67 	int	sc_hwunit;
68 };
69 
70 static void	ld_aac_attach(struct device *, struct device *, void *);
71 static void	ld_aac_intr(struct aac_ccb *);
72 static int	ld_aac_dobio(struct ld_aac_softc *, void *, int, int, int,
73 			     struct buf *);
74 static int	ld_aac_dump(struct ld_softc *, void *, int, int);
75 static int	ld_aac_match(struct device *, struct cfdata *, void *);
76 static int	ld_aac_start(struct ld_softc *, struct buf *);
77 
78 CFATTACH_DECL(ld_aac, sizeof(struct ld_aac_softc),
79     ld_aac_match, ld_aac_attach, NULL, NULL);
80 
81 static int
82 ld_aac_match(struct device *parent, struct cfdata *match, void *aux)
83 {
84 
85 	return (1);
86 }
87 
88 static void
89 ld_aac_attach(struct device *parent, struct device *self, void *aux)
90 {
91 	struct aac_attach_args *aaca;
92 	struct aac_drive *hdr;
93 	struct ld_aac_softc *sc;
94 	struct ld_softc *ld;
95 	struct aac_softc *aac;
96 
97 	aaca = aux;
98 	aac = (struct aac_softc *)parent;
99 	sc = (struct ld_aac_softc *)self;
100 	ld = &sc->sc_ld;
101 	hdr = &aac->sc_hdr[aaca->aaca_unit];
102 
103 	sc->sc_hwunit = aaca->aaca_unit;
104 	ld->sc_flags = LDF_ENABLED;
105 	ld->sc_maxxfer = AAC_MAX_XFER;
106 	ld->sc_secperunit = hdr->hd_size;
107 	ld->sc_secsize = AAC_SECTOR_SIZE;
108 	ld->sc_maxqueuecnt = (AAC_NCCBS - AAC_NCCBS_RESERVE) / aac->sc_nunits;
109 	ld->sc_start = ld_aac_start;
110 	ld->sc_dump = ld_aac_dump;
111 
112 	printf(": %s\n",
113 	    aac_describe_code(aac_container_types, hdr->hd_devtype));
114 	ldattach(ld);
115 }
116 
117 static int
118 ld_aac_dobio(struct ld_aac_softc *sc, void *data, int datasize, int blkno,
119 	     int dowrite, struct buf *bp)
120 {
121 	struct aac_blockread_response *brr;
122 	struct aac_blockwrite_response *bwr;
123 	struct aac_ccb *ac;
124 	struct aac_softc *aac;
125 	struct aac_blockread *br;
126 	struct aac_blockwrite *bw;
127 	struct aac_sg_entry *sge;
128 	struct aac_sg_table *sgt;
129 	struct aac_fib *fib;
130 	bus_dmamap_t xfer;
131 	u_int32_t status;
132 	u_int16_t size;
133 	int s, rv, i;
134 
135 	aac = (struct aac_softc *)sc->sc_ld.sc_dv.dv_parent;
136 
137 	/*
138 	 * Allocate a command control block and map the data transfer.
139 	 */
140 	ac = aac_ccb_alloc(aac, (dowrite ? AAC_CCB_DATA_OUT : AAC_CCB_DATA_IN));
141 	ac->ac_data = data;
142 	ac->ac_datalen = datasize;
143 
144 	if ((rv = aac_ccb_map(aac, ac)) != 0) {
145 		aac_ccb_free(aac, ac);
146 		return (rv);
147 	}
148 
149 	/*
150 	 * Build the command.
151 	 */
152 	fib = ac->ac_fib;
153 
154         fib->Header.XferState = htole32(AAC_FIBSTATE_HOSTOWNED |
155 	    AAC_FIBSTATE_INITIALISED | AAC_FIBSTATE_FROMHOST |
156 	    AAC_FIBSTATE_REXPECTED | AAC_FIBSTATE_NORM);
157 	fib->Header.Command = htole16(ContainerCommand);
158 
159 	if (dowrite) {
160 		bw = (struct aac_blockwrite *)&fib->data[0];
161 		bw->Command = htole32(VM_CtBlockWrite);
162 		bw->ContainerId = htole32(sc->sc_hwunit);
163 		bw->BlockNumber = htole32(blkno);
164 		bw->ByteCount = htole32(datasize);
165 		bw->Stable = htole32(CUNSTABLE); /* XXX what's appropriate here? */
166 
167 		size = sizeof(struct aac_blockwrite);
168 		sgt = &bw->SgMap;
169 	} else {
170 		br = (struct aac_blockread *)&fib->data[0];
171 		br->Command = htole32(VM_CtBlockRead);
172 		br->ContainerId = htole32(sc->sc_hwunit);
173 		br->BlockNumber = htole32(blkno);
174 		br->ByteCount = htole32(datasize);
175 
176 		size = sizeof(struct aac_blockread);
177 		sgt = &br->SgMap;
178 	}
179 
180 	xfer = ac->ac_dmamap_xfer;
181 	sgt->SgCount = xfer->dm_nsegs;
182 	sge = sgt->SgEntry;
183 
184 	for (i = 0; i < xfer->dm_nsegs; i++, sge++) {
185 		sge->SgAddress = htole32(xfer->dm_segs[i].ds_addr);
186 		sge->SgByteCount = htole32(xfer->dm_segs[i].ds_len);
187 		AAC_DPRINTF(AAC_D_IO,
188 		    ("#%d va %p pa %lx len %x\n", i, data,
189 		    (u_long)xfer->dm_segs[i].ds_addr,
190 		    xfer->dm_segs[i].ds_len));
191 	}
192 
193 	size += xfer->dm_nsegs * sizeof(struct aac_sg_entry);
194 	size = htole16(sizeof(fib->Header) + size);
195 	fib->Header.Size = htole16(size);
196 
197 	if (bp == NULL) {
198 		/*
199 		 * Polled commands must not sit on the software queue.  Wait
200 		 * up to 30 seconds for the command to complete.
201 		 */
202 		s = splbio();
203 		rv = aac_ccb_poll(aac, ac, 30000);
204 		aac_ccb_unmap(aac, ac);
205 		aac_ccb_free(aac, ac);
206 		splx(s);
207 
208 		if (rv == 0) {
209 			if (dowrite) {
210 				bwr = (struct aac_blockwrite_response *)
211 				    &ac->ac_fib->data[0];
212 				status = le32toh(bwr->Status);
213 			} else {
214 				brr = (struct aac_blockread_response *)
215 				    &ac->ac_fib->data[0];
216 				status = le32toh(brr->Status);
217 			}
218 
219 			if (status != ST_OK) {
220 				printf("%s: I/O error: %s\n",
221 				    sc->sc_ld.sc_dv.dv_xname,
222 				    aac_describe_code(aac_command_status_table,
223 				    status));
224 				rv = EIO;
225 			}
226 		}
227 	} else {
228 		ac->ac_device = (struct device *)sc;
229 		ac->ac_context = bp;
230 		ac->ac_intr = ld_aac_intr;
231 		aac_ccb_enqueue(aac, ac);
232 		rv = 0;
233 	}
234 
235 	return (rv);
236 }
237 
238 static int
239 ld_aac_start(struct ld_softc *ld, struct buf *bp)
240 {
241 
242 	return (ld_aac_dobio((struct ld_aac_softc *)ld, bp->b_data,
243 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
244 }
245 
246 static void
247 ld_aac_intr(struct aac_ccb *ac)
248 {
249 	struct aac_blockread_response *brr;
250 	struct aac_blockwrite_response *bwr;
251 	struct ld_aac_softc *sc;
252 	struct aac_softc *aac;
253 	struct buf *bp;
254 	u_int32_t status;
255 
256 	bp = ac->ac_context;
257 	sc = (struct ld_aac_softc *)ac->ac_device;
258 	aac = (struct aac_softc *)sc->sc_ld.sc_dv.dv_parent;
259 
260 	if ((bp->b_flags & B_READ) != 0) {
261 		brr = (struct aac_blockread_response *)&ac->ac_fib->data[0];
262 		status = le32toh(brr->Status);
263 	} else {
264 		bwr = (struct aac_blockwrite_response *)&ac->ac_fib->data[0];
265 		status = le32toh(bwr->Status);
266 	}
267 
268 	aac_ccb_unmap(aac, ac);
269 	aac_ccb_free(aac, ac);
270 
271 	if (status != ST_OK) {
272 		bp->b_flags |= B_ERROR;
273 		bp->b_error = EIO;
274 		bp->b_resid = bp->b_bcount;
275 
276 		printf("%s: I/O error: %s\n", sc->sc_ld.sc_dv.dv_xname,
277 		    aac_describe_code(aac_command_status_table, status));
278 	} else
279 		bp->b_resid = 0;
280 
281 	lddone(&sc->sc_ld, bp);
282 }
283 
284 static int
285 ld_aac_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
286 {
287 
288 	return (ld_aac_dobio((struct ld_aac_softc *)ld, data,
289 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
290 }
291