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