xref: /netbsd-src/sys/dev/ic/ld_icp.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: ld_icp.c,v 1.18 2007/10/19 11:59:55 ad 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.18 2007/10/19 11:59:55 ad 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 <sys/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,
96     void *aux)
97 {
98 	struct icp_attach_args *icpa;
99 
100 	icpa = aux;
101 
102 	return (icpa->icpa_unit < ICPA_UNIT_SCSI);
103 }
104 
105 void
106 ld_icp_attach(struct device *parent, struct device *self, void *aux)
107 {
108 	struct icp_attach_args *icpa;
109 	struct ld_icp_softc *sc;
110 	struct ld_softc *ld;
111 	struct icp_softc *icp;
112 	struct icp_cachedrv *cd;
113 	struct icp_cdevinfo *cdi;
114 	const char *str;
115 	int t;
116 
117 	sc = (struct ld_icp_softc *)self;
118 	ld = &sc->sc_ld;
119 	icp = (struct icp_softc *)parent;
120 	icpa = aux;
121 	cd = &icp->icp_cdr[icpa->icpa_unit];
122 
123 	icp_register_servicecb(icp, icpa->icpa_unit, &ld_icp_servicecb);
124 
125 	sc->sc_hwunit = icpa->icpa_unit;
126 	ld->sc_maxxfer = ICP_MAX_XFER;
127 	ld->sc_secsize = ICP_SECTOR_SIZE;
128 	ld->sc_start = ld_icp_start;
129 	ld->sc_dump = ld_icp_dump;
130 	ld->sc_flush = ld_icp_flush;
131 	ld->sc_secperunit = cd->cd_size;
132 	ld->sc_flags = LDF_ENABLED;
133 	ld->sc_maxqueuecnt = icp->icp_openings;
134 
135 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_CACHE_DRV_INFO,
136 	    sc->sc_hwunit, sizeof(struct icp_cdevinfo))) {
137 		aprint_error(": unable to retrieve device info\n");
138 		ld->sc_flags = LDF_ENABLED;
139 		goto out;
140 	}
141 	cdi = (struct icp_cdevinfo *)icp->icp_scr;
142 
143 	aprint_normal(": <%.8s>, ", cdi->ld_name);
144 	t = le32toh(cdi->ld_dtype) >> 16;
145 
146 	/*
147 	 * Print device type.
148 	 */
149 	if (le32toh(cdi->ld_dcnt) > 1 || le32toh(cdi->ld_slave) != -1)
150 		str = "RAID-1";
151 	else if (t == 0)
152 		str = "JBOD";
153 	else if (t == 1)
154 		str = "RAID-0";
155 	else if (t == 2)
156 		str = "Chain";
157 	else
158 		str = "unknown type";
159 
160 	aprint_normal("type: %s, ", str);
161 
162 	/*
163 	 * Print device status.
164 	 */
165 	if (t > 2)
166 		str = "missing";
167 	else if ((cdi->ld_error & 1) != 0) {
168 		str = "fault";
169 		ld->sc_flags = LDF_ENABLED;
170 	} else if ((cdi->ld_error & 2) != 0)
171 		str = "invalid";
172 	else {
173 		str = "optimal";
174 		ld->sc_flags = LDF_ENABLED;
175 	}
176 
177 	aprint_normal("status: %s\n", str);
178 
179  out:
180 	ldattach(ld);
181 }
182 
183 int
184 ld_icp_detach(struct device *dv, int flags)
185 {
186 	int rv;
187 
188 	if ((rv = ldbegindetach((struct ld_softc *)dv, flags)) != 0)
189 		return (rv);
190 	ldenddetach((struct ld_softc *) dv);
191 
192 	return (0);
193 }
194 
195 int
196 ld_icp_dobio(struct ld_icp_softc *sc, void *data, int datasize, int blkno,
197 	     int dowrite, struct buf *bp)
198 {
199 	struct icp_cachecmd *cc;
200 	struct icp_ccb *ic;
201 	struct icp_softc *icp;
202 	int s, rv;
203 
204 	icp = (struct icp_softc *)device_parent(&sc->sc_ld.sc_dv);
205 
206 	/*
207 	 * Allocate a command control block.
208 	 */
209 	if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL))
210 		return (EAGAIN);
211 
212 	/*
213 	 * Map the data transfer.
214 	 */
215 	cc = &ic->ic_cmd.cmd_packet.cc;
216 	ic->ic_sg = cc->cc_sg;
217 	ic->ic_service = ICP_CACHESERVICE;
218 
219 	rv = icp_ccb_map(icp, ic, data, datasize,
220 	    dowrite ? IC_XFER_OUT : IC_XFER_IN);
221 	if (rv != 0) {
222 		icp_ccb_free(icp, ic);
223 		return (rv);
224 	}
225 
226 	/*
227 	 * Build the command.
228 	 */
229 	ic->ic_cmd.cmd_opcode = htole16((dowrite ? ICP_WRITE : ICP_READ));
230 	cc->cc_deviceno = htole16(sc->sc_hwunit);
231 	cc->cc_blockno = htole32(blkno);
232 	cc->cc_blockcnt = htole32(datasize / ICP_SECTOR_SIZE);
233 	cc->cc_addr = ~0;	/* scatter gather */
234 	cc->cc_nsgent = htole32(ic->ic_nsgent);
235 
236 	ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd +
237 	    ic->ic_nsgent * sizeof(*ic->ic_sg);
238 
239 	/*
240 	 * Fire it off to the controller.
241 	 */
242 	if (bp == NULL) {
243 		s = splbio();
244 		rv = icp_ccb_poll(icp, ic, 10000);
245 		icp_ccb_unmap(icp, ic);
246 		icp_ccb_free(icp, ic);
247 		splx(s);
248 	} else {
249  		ic->ic_intr = ld_icp_intr;
250 		ic->ic_context = bp;
251 		ic->ic_dv = &sc->sc_ld.sc_dv;
252 		icp_ccb_enqueue(icp, ic);
253 	}
254 
255 	return (rv);
256 }
257 
258 int
259 ld_icp_start(struct ld_softc *ld, struct buf *bp)
260 {
261 
262 	return (ld_icp_dobio((struct ld_icp_softc *)ld, bp->b_data,
263 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
264 }
265 
266 int
267 ld_icp_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
268 {
269 
270 	return (ld_icp_dobio((struct ld_icp_softc *)ld, data,
271 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
272 }
273 
274 int
275 ld_icp_flush(struct ld_softc *ld)
276 {
277 	struct ld_icp_softc *sc;
278 	struct icp_softc *icp;
279 	struct icp_cachecmd *cc;
280 	struct icp_ccb *ic;
281 	int rv;
282 
283 	sc = (struct ld_icp_softc *)ld;
284 	icp = (struct icp_softc *)device_parent(&ld->sc_dv);
285 
286 	ic = icp_ccb_alloc_wait(icp);
287 	ic->ic_cmd.cmd_opcode = htole16(ICP_FLUSH);
288 
289 	cc = &ic->ic_cmd.cmd_packet.cc;
290 	cc->cc_deviceno = htole16(sc->sc_hwunit);
291 	cc->cc_blockno = htole32(1);
292 	cc->cc_blockcnt = 0;
293 	cc->cc_addr = 0;
294 	cc->cc_nsgent = 0;
295 
296 	ic->ic_cmdlen = (u_long)&cc->cc_sg - (u_long)&ic->ic_cmd;
297 	ic->ic_service = ICP_CACHESERVICE;
298 
299 	rv = icp_ccb_wait(icp, ic, 30000);
300 	icp_ccb_free(icp, ic);
301 
302 	return (rv);
303 }
304 
305 void
306 ld_icp_intr(struct icp_ccb *ic)
307 {
308 	struct buf *bp;
309 	struct ld_icp_softc *sc;
310 	struct icp_softc *icp;
311 
312 	bp = ic->ic_context;
313 	sc = (struct ld_icp_softc *)ic->ic_dv;
314 	icp = (struct icp_softc *)device_parent(&sc->sc_ld.sc_dv);
315 
316 	if (ic->ic_status != ICP_S_OK) {
317 		printf("%s: request failed; status=0x%04x\n",
318 		    ic->ic_dv->dv_xname, ic->ic_status);
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 = device_unit(&icp->icp_dv);
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