xref: /netbsd-src/sys/dev/ic/ld_icp.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: ld_icp.c,v 1.26 2012/10/27 17:18:21 chs 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * ICP-Vortex "GDT" front-end for ld(4) driver.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ld_icp.c,v 1.26 2012/10/27 17:18:21 chs Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/buf.h>
44 #include <sys/bufq.h>
45 #include <sys/endian.h>
46 #include <sys/dkio.h>
47 #include <sys/disk.h>
48 #include <sys/rnd.h>
49 
50 #include <sys/bus.h>
51 
52 #include <dev/ldvar.h>
53 
54 #include <dev/ic/icpreg.h>
55 #include <dev/ic/icpvar.h>
56 
57 struct ld_icp_softc {
58 	struct	ld_softc sc_ld;
59 	int	sc_hwunit;
60 };
61 
62 void	ld_icp_attach(device_t, device_t, void *);
63 int	ld_icp_detach(device_t, int);
64 int	ld_icp_dobio(struct ld_icp_softc *, void *, int, int, int,
65 		     struct buf *);
66 int	ld_icp_dump(struct ld_softc *, void *, int, int);
67 int	ld_icp_flush(struct ld_softc *, int);
68 void	ld_icp_intr(struct icp_ccb *);
69 int	ld_icp_match(device_t, cfdata_t, void *);
70 int	ld_icp_start(struct ld_softc *, struct buf *);
71 
72 void	ld_icp_adjqparam(device_t, int);
73 
74 CFATTACH_DECL_NEW(ld_icp, sizeof(struct ld_icp_softc),
75     ld_icp_match, ld_icp_attach, ld_icp_detach, NULL);
76 
77 static const struct icp_servicecb ld_icp_servicecb = {
78 	ld_icp_adjqparam,
79 };
80 
81 int
82 ld_icp_match(device_t parent, cfdata_t match, void *aux)
83 {
84 	struct icp_attach_args *icpa;
85 
86 	icpa = aux;
87 
88 	return (icpa->icpa_unit < ICPA_UNIT_SCSI);
89 }
90 
91 void
92 ld_icp_attach(device_t parent, device_t self, void *aux)
93 {
94 	struct icp_attach_args *icpa = aux;
95 	struct ld_icp_softc *sc = device_private(self);
96 	struct ld_softc *ld = &sc->sc_ld;
97 	struct icp_softc *icp = device_private(parent);
98 	struct icp_cachedrv *cd = &icp->icp_cdr[icpa->icpa_unit];
99 	struct icp_cdevinfo *cdi;
100 	const char *str;
101 	int t;
102 
103 	ld->sc_dv = self;
104 
105 	icp_register_servicecb(icp, icpa->icpa_unit, &ld_icp_servicecb);
106 
107 	sc->sc_hwunit = icpa->icpa_unit;
108 	ld->sc_maxxfer = ICP_MAX_XFER;
109 	ld->sc_secsize = ICP_SECTOR_SIZE;
110 	ld->sc_start = ld_icp_start;
111 	ld->sc_dump = ld_icp_dump;
112 	ld->sc_flush = ld_icp_flush;
113 	ld->sc_secperunit = cd->cd_size;
114 	ld->sc_flags = LDF_ENABLED;
115 	ld->sc_maxqueuecnt = icp->icp_openings;
116 
117 	if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_CACHE_DRV_INFO,
118 	    sc->sc_hwunit, sizeof(struct icp_cdevinfo))) {
119 		aprint_error(": unable to retrieve device info\n");
120 		ld->sc_flags = LDF_ENABLED;
121 		goto out;
122 	}
123 	cdi = (struct icp_cdevinfo *)icp->icp_scr;
124 
125 	aprint_normal(": <%.8s>, ", cdi->ld_name);
126 	t = le32toh(cdi->ld_dtype) >> 16;
127 
128 	/*
129 	 * Print device type.
130 	 */
131 	if (le32toh(cdi->ld_dcnt) > 1 || le32toh(cdi->ld_slave) != -1)
132 		str = "RAID-1";
133 	else if (t == 0)
134 		str = "JBOD";
135 	else if (t == 1)
136 		str = "RAID-0";
137 	else if (t == 2)
138 		str = "Chain";
139 	else
140 		str = "unknown type";
141 
142 	aprint_normal("type: %s, ", str);
143 
144 	/*
145 	 * Print device status.
146 	 */
147 	if (t > 2)
148 		str = "missing";
149 	else if ((cdi->ld_error & 1) != 0) {
150 		str = "fault";
151 		ld->sc_flags = LDF_ENABLED;
152 	} else if ((cdi->ld_error & 2) != 0)
153 		str = "invalid";
154 	else {
155 		str = "optimal";
156 		ld->sc_flags = LDF_ENABLED;
157 	}
158 
159 	aprint_normal("status: %s\n", str);
160 
161  out:
162 	ldattach(ld);
163 }
164 
165 int
166 ld_icp_detach(device_t dv, int flags)
167 {
168 	int rv;
169 
170 	if ((rv = ldbegindetach((struct ld_softc *)dv, flags)) != 0)
171 		return (rv);
172 	ldenddetach((struct ld_softc *) dv);
173 
174 	return (0);
175 }
176 
177 int
178 ld_icp_dobio(struct ld_icp_softc *sc, void *data, int datasize, int blkno,
179 	     int dowrite, struct buf *bp)
180 {
181 	struct icp_cachecmd *cc;
182 	struct icp_ccb *ic;
183 	struct icp_softc *icp;
184 	int s, rv;
185 
186 	icp = device_private(device_parent(sc->sc_ld.sc_dv));
187 
188 	/*
189 	 * Allocate a command control block.
190 	 */
191 	if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL))
192 		return (EAGAIN);
193 
194 	/*
195 	 * Map the data transfer.
196 	 */
197 	cc = &ic->ic_cmd.cmd_packet.cc;
198 	ic->ic_sg = cc->cc_sg;
199 	ic->ic_service = ICP_CACHESERVICE;
200 
201 	rv = icp_ccb_map(icp, ic, data, datasize,
202 	    dowrite ? IC_XFER_OUT : IC_XFER_IN);
203 	if (rv != 0) {
204 		icp_ccb_free(icp, ic);
205 		return (rv);
206 	}
207 
208 	/*
209 	 * Build the command.
210 	 */
211 	ic->ic_cmd.cmd_opcode = htole16((dowrite ? ICP_WRITE : ICP_READ));
212 	cc->cc_deviceno = htole16(sc->sc_hwunit);
213 	cc->cc_blockno = htole32(blkno);
214 	cc->cc_blockcnt = htole32(datasize / ICP_SECTOR_SIZE);
215 	cc->cc_addr = ~0;	/* scatter gather */
216 	cc->cc_nsgent = htole32(ic->ic_nsgent);
217 
218 	ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd +
219 	    ic->ic_nsgent * sizeof(*ic->ic_sg);
220 
221 	/*
222 	 * Fire it off to the controller.
223 	 */
224 	if (bp == NULL) {
225 		s = splbio();
226 		rv = icp_ccb_poll(icp, ic, 10000);
227 		icp_ccb_unmap(icp, ic);
228 		icp_ccb_free(icp, ic);
229 		splx(s);
230 	} else {
231  		ic->ic_intr = ld_icp_intr;
232 		ic->ic_context = bp;
233 		ic->ic_dv = sc->sc_ld.sc_dv;
234 		icp_ccb_enqueue(icp, ic);
235 	}
236 
237 	return (rv);
238 }
239 
240 int
241 ld_icp_start(struct ld_softc *ld, struct buf *bp)
242 {
243 
244 	return (ld_icp_dobio((struct ld_icp_softc *)ld, bp->b_data,
245 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
246 }
247 
248 int
249 ld_icp_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
250 {
251 
252 	return (ld_icp_dobio((struct ld_icp_softc *)ld, data,
253 	    blkcnt * ld->sc_secsize, blkno, 1, NULL));
254 }
255 
256 int
257 ld_icp_flush(struct ld_softc *ld, int flags)
258 {
259 	struct ld_icp_softc *sc;
260 	struct icp_softc *icp;
261 	struct icp_cachecmd *cc;
262 	struct icp_ccb *ic;
263 	int rv;
264 
265 	sc = (struct ld_icp_softc *)ld;
266 	icp = device_private(device_parent(ld->sc_dv));
267 
268 	ic = icp_ccb_alloc_wait(icp);
269 	ic->ic_cmd.cmd_opcode = htole16(ICP_FLUSH);
270 
271 	cc = &ic->ic_cmd.cmd_packet.cc;
272 	cc->cc_deviceno = htole16(sc->sc_hwunit);
273 	cc->cc_blockno = htole32(1);
274 	cc->cc_blockcnt = 0;
275 	cc->cc_addr = 0;
276 	cc->cc_nsgent = 0;
277 
278 	ic->ic_cmdlen = (u_long)&cc->cc_sg - (u_long)&ic->ic_cmd;
279 	ic->ic_service = ICP_CACHESERVICE;
280 
281 	rv = icp_ccb_wait(icp, ic, 30000);
282 	icp_ccb_free(icp, ic);
283 
284 	return (rv);
285 }
286 
287 void
288 ld_icp_intr(struct icp_ccb *ic)
289 {
290 	struct buf *bp;
291 	struct ld_icp_softc *sc;
292 	struct icp_softc *icp;
293 
294 	bp = ic->ic_context;
295 	sc = device_private(ic->ic_dv);
296 	icp = device_private(device_parent(sc->sc_ld.sc_dv));
297 
298 	if (ic->ic_status != ICP_S_OK) {
299 		aprint_error_dev(ic->ic_dv, "request failed; status=0x%04x\n",
300 		    ic->ic_status);
301 		bp->b_error = EIO;
302 		bp->b_resid = bp->b_bcount;
303 
304 		icp->icp_evt.size = sizeof(icp->icp_evt.eu.sync);
305 		icp->icp_evt.eu.sync.ionode = device_unit(icp->icp_dv);
306 		icp->icp_evt.eu.sync.service = icp->icp_service;
307 		icp->icp_evt.eu.sync.status = icp->icp_status;
308 		icp->icp_evt.eu.sync.info = icp->icp_info;
309 		icp->icp_evt.eu.sync.hostdrive = sc->sc_hwunit;
310 		if (icp->icp_status >= 0x8000)
311 			icp_store_event(icp, GDT_ES_SYNC, 0, &icp->icp_evt);
312 		else
313 			icp_store_event(icp, GDT_ES_SYNC, icp->icp_service,
314 			    &icp->icp_evt);
315 	} else
316 		bp->b_resid = 0;
317 
318 	icp_ccb_unmap(icp, ic);
319 	icp_ccb_free(icp, ic);
320 	lddone(&sc->sc_ld, bp);
321 }
322 
323 void
324 ld_icp_adjqparam(device_t dv, int openings)
325 {
326 
327 	ldadjqparam((struct ld_softc *) dv, openings);
328 }
329