xref: /netbsd-src/sys/dev/ic/ld_icp.c (revision e5548b402ae4c44fb816de42c7bba9581ce23ef5)
1 /*	$NetBSD: ld_icp.c,v 1.12 2005/12/11 12:21:27 christos 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 /*
40  * ICP-Vortex "GDT" front-end for ld(4) driver.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: ld_icp.c,v 1.12 2005/12/11 12:21:27 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 <uvm/uvm_extern.h>
62 
63 #include <machine/bus.h>
64 
65 #include <dev/ldvar.h>
66 
67 #include <dev/ic/icpreg.h>
68 #include <dev/ic/icpvar.h>
69 
70 struct ld_icp_softc {
71 	struct	ld_softc sc_ld;
72 	int	sc_hwunit;
73 };
74 
75 void	ld_icp_attach(struct device *, struct device *, void *);
76 int	ld_icp_detach(struct device *, int);
77 int	ld_icp_dobio(struct ld_icp_softc *, void *, int, int, int,
78 		     struct buf *);
79 int	ld_icp_dump(struct ld_softc *, void *, int, int);
80 int	ld_icp_flush(struct ld_softc *);
81 void	ld_icp_intr(struct icp_ccb *);
82 int	ld_icp_match(struct device *, struct cfdata *, void *);
83 int	ld_icp_start(struct ld_softc *, struct buf *);
84 
85 void	ld_icp_adjqparam(struct device *, int);
86 
87 CFATTACH_DECL(ld_icp, sizeof(struct ld_icp_softc),
88     ld_icp_match, ld_icp_attach, ld_icp_detach, NULL);
89 
90 static const struct icp_servicecb ld_icp_servicecb = {
91 	ld_icp_adjqparam,
92 };
93 
94 int
95 ld_icp_match(struct device *parent, struct cfdata *match, void *aux)
96 {
97 	struct icp_attach_args *icpa;
98 
99 	icpa = aux;
100 
101 	return (icpa->icpa_unit < ICPA_UNIT_SCSI);
102 }
103 
104 void
105 ld_icp_attach(struct device *parent, struct device *self, void *aux)
106 {
107 	struct icp_attach_args *icpa;
108 	struct ld_icp_softc *sc;
109 	struct ld_softc *ld;
110 	struct icp_softc *icp;
111 	struct icp_cachedrv *cd;
112 	struct icp_cdevinfo *cdi;
113 	const char *str;
114 	int t;
115 
116 	sc = (struct ld_icp_softc *)self;
117 	ld = &sc->sc_ld;
118 	icp = (struct icp_softc *)parent;
119 	icpa = aux;
120 	cd = &icp->icp_cdr[icpa->icpa_unit];
121 
122 	icp_register_servicecb(icp, icpa->icpa_unit, &ld_icp_servicecb);
123 
124 	sc->sc_hwunit = icpa->icpa_unit;
125 	ld->sc_maxxfer = ICP_MAX_XFER;
126 	ld->sc_secsize = ICP_SECTOR_SIZE;
127 	ld->sc_start = ld_icp_start;
128 	ld->sc_dump = ld_icp_dump;
129 	ld->sc_flush = ld_icp_flush;
130 	ld->sc_secperunit = cd->cd_size;
131 	ld->sc_flags = LDF_ENABLED;
132 	ld->sc_maxqueuecnt = icp->icp_openings;
133 
134 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_CACHE_DRV_INFO,
135 	    sc->sc_hwunit, sizeof(struct icp_cdevinfo))) {
136 		aprint_error(": unable to retrieve device info\n");
137 		ld->sc_flags = LDF_ENABLED;
138 		goto out;
139 	}
140 	cdi = (struct icp_cdevinfo *)icp->icp_scr;
141 
142 	aprint_normal(": <%.8s>, ", cdi->ld_name);
143 	t = le32toh(cdi->ld_dtype) >> 16;
144 
145 	/*
146 	 * Print device type.
147 	 */
148 	if (le32toh(cdi->ld_dcnt) > 1 || le32toh(cdi->ld_slave) != -1)
149 		str = "RAID-1";
150 	else if (t == 0)
151 		str = "JBOD";
152 	else if (t == 1)
153 		str = "RAID-0";
154 	else if (t == 2)
155 		str = "Chain";
156 	else
157 		str = "unknown type";
158 
159 	aprint_normal("type: %s, ", str);
160 
161 	/*
162 	 * Print device status.
163 	 */
164 	if (t > 2)
165 		str = "missing";
166 	else if ((cdi->ld_error & 1) != 0) {
167 		str = "fault";
168 		ld->sc_flags = LDF_ENABLED;
169 	} else if ((cdi->ld_error & 2) != 0)
170 		str = "invalid";
171 	else {
172 		str = "optimal";
173 		ld->sc_flags = LDF_ENABLED;
174 	}
175 
176 	aprint_normal("status: %s\n", str);
177 
178  out:
179 	ldattach(ld);
180 }
181 
182 int
183 ld_icp_detach(struct device *dv, int flags)
184 {
185 	int rv;
186 
187 	if ((rv = ldbegindetach((struct ld_softc *)dv, flags)) != 0)
188 		return (rv);
189 	ldenddetach((struct ld_softc *) dv);
190 
191 	return (0);
192 }
193 
194 int
195 ld_icp_dobio(struct ld_icp_softc *sc, void *data, int datasize, int blkno,
196 	     int dowrite, struct buf *bp)
197 {
198 	struct icp_cachecmd *cc;
199 	struct icp_ccb *ic;
200 	struct icp_softc *icp;
201 	int s, rv;
202 
203 	icp = (struct icp_softc *)sc->sc_ld.sc_dv.dv_parent;
204 
205 	/*
206 	 * Allocate a command control block.
207 	 */
208 	if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL))
209 		return (EAGAIN);
210 
211 	/*
212 	 * Map the data transfer.
213 	 */
214 	cc = &ic->ic_cmd.cmd_packet.cc;
215 	ic->ic_sg = cc->cc_sg;
216 	ic->ic_service = ICP_CACHESERVICE;
217 
218 	rv = icp_ccb_map(icp, ic, data, datasize,
219 	    dowrite ? IC_XFER_OUT : IC_XFER_IN);
220 	if (rv != 0) {
221 		icp_ccb_free(icp, ic);
222 		return (rv);
223 	}
224 
225 	/*
226 	 * Build the command.
227 	 */
228 	ic->ic_cmd.cmd_opcode = htole16((dowrite ? ICP_WRITE : ICP_READ));
229 	cc->cc_deviceno = htole16(sc->sc_hwunit);
230 	cc->cc_blockno = htole32(blkno);
231 	cc->cc_blockcnt = htole32(datasize / ICP_SECTOR_SIZE);
232 	cc->cc_addr = ~0;	/* scatter gather */
233 	cc->cc_nsgent = htole32(ic->ic_nsgent);
234 
235 	ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd +
236 	    ic->ic_nsgent * sizeof(*ic->ic_sg);
237 
238 	/*
239 	 * Fire it off to the controller.
240 	 */
241 	if (bp == NULL) {
242 		s = splbio();
243 		rv = icp_ccb_poll(icp, ic, 10000);
244 		icp_ccb_unmap(icp, ic);
245 		icp_ccb_free(icp, ic);
246 		splx(s);
247 	} else {
248  		ic->ic_intr = ld_icp_intr;
249 		ic->ic_context = bp;
250 		ic->ic_dv = &sc->sc_ld.sc_dv;
251 		icp_ccb_enqueue(icp, ic);
252 	}
253 
254 	return (rv);
255 }
256 
257 int
258 ld_icp_start(struct ld_softc *ld, struct buf *bp)
259 {
260 
261 	return (ld_icp_dobio((struct ld_icp_softc *)ld, bp->b_data,
262 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
263 }
264 
265 int
266 ld_icp_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
267 {
268 
269 	return (ld_icp_dobio((struct ld_icp_softc *)ld, data,
270 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
271 }
272 
273 int
274 ld_icp_flush(struct ld_softc *ld)
275 {
276 	struct ld_icp_softc *sc;
277 	struct icp_softc *icp;
278 	struct icp_cachecmd *cc;
279 	struct icp_ccb *ic;
280 	int rv;
281 
282 	sc = (struct ld_icp_softc *)ld;
283 	icp = (struct icp_softc *)ld->sc_dv.dv_parent;
284 
285 	ic = icp_ccb_alloc_wait(icp);
286 	ic->ic_cmd.cmd_opcode = htole16(ICP_FLUSH);
287 
288 	cc = &ic->ic_cmd.cmd_packet.cc;
289 	cc->cc_deviceno = htole16(sc->sc_hwunit);
290 	cc->cc_blockno = htole32(1);
291 	cc->cc_blockcnt = 0;
292 	cc->cc_addr = 0;
293 	cc->cc_nsgent = 0;
294 
295 	ic->ic_cmdlen = (u_long)&cc->cc_sg - (u_long)&ic->ic_cmd;
296 	ic->ic_service = ICP_CACHESERVICE;
297 
298 	rv = icp_ccb_wait(icp, ic, 30000);
299 	icp_ccb_free(icp, ic);
300 
301 	return (rv);
302 }
303 
304 void
305 ld_icp_intr(struct icp_ccb *ic)
306 {
307 	struct buf *bp;
308 	struct ld_icp_softc *sc;
309 	struct icp_softc *icp;
310 
311 	bp = ic->ic_context;
312 	sc = (struct ld_icp_softc *)ic->ic_dv;
313 	icp = (struct icp_softc *)sc->sc_ld.sc_dv.dv_parent;
314 
315 	if (ic->ic_status != ICP_S_OK) {
316 		printf("%s: request failed; status=0x%04x\n",
317 		    ic->ic_dv->dv_xname, ic->ic_status);
318 		bp->b_flags |= B_ERROR;
319 		bp->b_error = EIO;
320 		bp->b_resid = bp->b_bcount;
321 
322 		icp->icp_evt.size = sizeof(icp->icp_evt.eu.sync);
323 		icp->icp_evt.eu.sync.ionode = icp->icp_dv.dv_unit;
324 		icp->icp_evt.eu.sync.service = icp->icp_service;
325 		icp->icp_evt.eu.sync.status = icp->icp_status;
326 		icp->icp_evt.eu.sync.info = icp->icp_info;
327 		icp->icp_evt.eu.sync.hostdrive = sc->sc_hwunit;
328 		if (icp->icp_status >= 0x8000)
329 			icp_store_event(icp, GDT_ES_SYNC, 0, &icp->icp_evt);
330 		else
331 			icp_store_event(icp, GDT_ES_SYNC, icp->icp_service,
332 			    &icp->icp_evt);
333 	} else
334 		bp->b_resid = 0;
335 
336 	icp_ccb_unmap(icp, ic);
337 	icp_ccb_free(icp, ic);
338 	lddone(&sc->sc_ld, bp);
339 }
340 
341 void
342 ld_icp_adjqparam(struct device *dv, int openings)
343 {
344 
345 	ldadjqparam((struct ld_softc *) dv, openings);
346 }
347