1/* $NetBSD: ld_icp.c,v 1.29 2016/09/27 03:33:32 pgoyette 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.29 2016/09/27 03:33:32 pgoyette 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/module.h>
49#include <sys/bus.h>
50
51#include <dev/ldvar.h>
52
53#include <dev/ic/icpreg.h>
54#include <dev/ic/icpvar.h>
55
56#include "ioconf.h"
57
58struct ld_icp_softc {
59 struct ld_softc sc_ld;
60 int sc_hwunit;
61};
62
63void ld_icp_attach(device_t, device_t, void *);
64int ld_icp_detach(device_t, int);
65int ld_icp_dobio(struct ld_icp_softc *, void *, int, int, int,
66 struct buf *);
67int ld_icp_dump(struct ld_softc *, void *, int, int);
68int ld_icp_flush(struct ld_softc *, int);
69void ld_icp_intr(struct icp_ccb *);
70int ld_icp_match(device_t, cfdata_t, void *);
71int ld_icp_start(struct ld_softc *, struct buf *);
72
73void ld_icp_adjqparam(device_t, int);
74
75CFATTACH_DECL_NEW(ld_icp, sizeof(struct ld_icp_softc),
76 ld_icp_match, ld_icp_attach, ld_icp_detach, NULL);
77
78static const struct icp_servicecb ld_icp_servicecb = {
79 ld_icp_adjqparam,
80};
81
82int
83ld_icp_match(device_t parent, cfdata_t match, void *aux)
84{
85 struct icp_attach_args *icpa;
86
87 icpa = aux;
88
89 return (icpa->icpa_unit < ICPA_UNIT_SCSI);
90}
91
92void
93ld_icp_attach(device_t parent, device_t self, void *aux)
94{
95 struct icp_attach_args *icpa = aux;
96 struct ld_icp_softc *sc = device_private(self);
97 struct ld_softc *ld = &sc->sc_ld;
98 struct icp_softc *icp = device_private(parent);
99 struct icp_cachedrv *cd = &icp->icp_cdr[icpa->icpa_unit];
100 struct icp_cdevinfo *cdi;
101 const char *str;
102 int t;
103
104 ld->sc_dv = self;
105
106 icp_register_servicecb(icp, icpa->icpa_unit, &ld_icp_servicecb);
107
108 sc->sc_hwunit = icpa->icpa_unit;
109 ld->sc_maxxfer = ICP_MAX_XFER;
110 ld->sc_secsize = ICP_SECTOR_SIZE;
111 ld->sc_start = ld_icp_start;
112 ld->sc_dump = ld_icp_dump;
113 ld->sc_flush = ld_icp_flush;
114 ld->sc_secperunit = cd->cd_size;
115 ld->sc_flags = LDF_ENABLED;
116 ld->sc_maxqueuecnt = icp->icp_openings;
117
118 if (!icp_cmd(icp, ICP_CACHESERVICE, ICP_IOCTL, ICP_CACHE_DRV_INFO,
119 sc->sc_hwunit, sizeof(struct icp_cdevinfo))) {
120 aprint_error(": unable to retrieve device info\n");
121 ld->sc_flags = LDF_ENABLED;
122 goto out;
123 }
124 cdi = (struct icp_cdevinfo *)icp->icp_scr;
125
126 aprint_normal(": <%.8s>, ", cdi->ld_name);
127 t = le32toh(cdi->ld_dtype) >> 16;
128
129 /*
130 * Print device type.
131 */
132 if (le32toh(cdi->ld_dcnt) > 1 || le32toh(cdi->ld_slave) != -1)
133 str = "RAID-1";
134 else if (t == 0)
135 str = "JBOD";
136 else if (t == 1)
137 str = "RAID-0";
138 else if (t == 2)
139 str = "Chain";
140 else
141 str = "unknown type";
142
143 aprint_normal("type: %s, ", str);
144
145 /*
146 * Print device status.
147 */
148 if (t > 2)
149 str = "missing";
150 else if ((cdi->ld_error & 1) != 0) {
151 str = "fault";
152 ld->sc_flags = LDF_ENABLED;
153 } else if ((cdi->ld_error & 2) != 0)
154 str = "invalid";
155 else {
156 str = "optimal";
157 ld->sc_flags = LDF_ENABLED;
158 }
159
160 aprint_normal("status: %s\n", str);
161
162 out:
163 ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
164}
165
166int
167ld_icp_detach(device_t dv, int flags)
168{
169 int rv;
170
171 if ((rv = ldbegindetach((struct ld_softc *)dv, flags)) != 0)
172 return (rv);
173 ldenddetach((struct ld_softc *) dv);
174
175 return (0);
176}
177
178int
179ld_icp_dobio(struct ld_icp_softc *sc, void *data, int datasize, int blkno,
180 int dowrite, struct buf *bp)
181{
182 struct icp_cachecmd *cc;
183 struct icp_ccb *ic;
184 struct icp_softc *icp;
185 int s, rv;
186
187 icp = device_private(device_parent(sc->sc_ld.sc_dv));
188
189 /*
190 * Allocate a command control block.
191 */
192 if (__predict_false((ic = icp_ccb_alloc(icp)) == NULL))
193 return (EAGAIN);
194
195 /*
196 * Map the data transfer.
197 */
198 cc = &ic->ic_cmd.cmd_packet.cc;
199 ic->ic_sg = cc->cc_sg;
200 ic->ic_service = ICP_CACHESERVICE;
201
202 rv = icp_ccb_map(icp, ic, data, datasize,
203 dowrite ? IC_XFER_OUT : IC_XFER_IN);
204 if (rv != 0) {
205 icp_ccb_free(icp, ic);
206 return (rv);
207 }
208
209 /*
210 * Build the command.
211 */
212 ic->ic_cmd.cmd_opcode = htole16((dowrite ? ICP_WRITE : ICP_READ));
213 cc->cc_deviceno = htole16(sc->sc_hwunit);
214 cc->cc_blockno = htole32(blkno);
215 cc->cc_blockcnt = htole32(datasize / ICP_SECTOR_SIZE);
216 cc->cc_addr = ~0; /* scatter gather */
217 cc->cc_nsgent = htole32(ic->ic_nsgent);
218
219 ic->ic_cmdlen = (u_long)ic->ic_sg - (u_long)&ic->ic_cmd +
220 ic->ic_nsgent * sizeof(*ic->ic_sg);
221
222 /*
223 * Fire it off to the controller.
224 */
225 if (bp == NULL) {
226 s = splbio();
227 rv = icp_ccb_poll(icp, ic, 10000);
228 icp_ccb_unmap(icp, ic);
229 icp_ccb_free(icp, ic);
230 splx(s);
231 } else {
232 ic->ic_intr = ld_icp_intr;
233 ic->ic_context = bp;
234 ic->ic_dv = sc->sc_ld.sc_dv;
235 icp_ccb_enqueue(icp, ic);
236 }
237
238 return (rv);
239}
240
241int
242ld_icp_start(struct ld_softc *ld, struct buf *bp)
243{
244
245 return (ld_icp_dobio((struct ld_icp_softc *)ld, bp->b_data,
246 bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
247}
248
249int
250ld_icp_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
251{
252
253 return (ld_icp_dobio((struct ld_icp_softc *)ld, data,
254 blkcnt * ld->sc_secsize, blkno, 1, NULL));
255}
256
257int
258ld_icp_flush(struct ld_softc *ld, int flags)
259{
260 struct ld_icp_softc *sc;
261 struct icp_softc *icp;
262 struct icp_cachecmd *cc;
263 struct icp_ccb *ic;
264 int rv;
265
266 sc = (struct ld_icp_softc *)ld;
267 icp = device_private(device_parent(ld->sc_dv));
268
269 ic = icp_ccb_alloc_wait(icp);
270 ic->ic_cmd.cmd_opcode = htole16(ICP_FLUSH);
271
272 cc = &ic->ic_cmd.cmd_packet.cc;
273 cc->cc_deviceno = htole16(sc->sc_hwunit);
274 cc->cc_blockno = htole32(1);
275 cc->cc_blockcnt = 0;
276 cc->cc_addr = 0;
277 cc->cc_nsgent = 0;
278
279 ic->ic_cmdlen = (u_long)&cc->cc_sg - (u_long)&ic->ic_cmd;
280 ic->ic_service = ICP_CACHESERVICE;
281
282 rv = icp_ccb_wait(icp, ic, 30000);
283 icp_ccb_free(icp, ic);
284
285 return (rv);
286}
287
288void
289ld_icp_intr(struct icp_ccb *ic)
290{
291 struct buf *bp;
292 struct ld_icp_softc *sc;
293 struct icp_softc *icp;
294
295 bp = ic->ic_context;
296 sc = device_private(ic->ic_dv);
297 icp = device_private(device_parent(sc->sc_ld.sc_dv));
298
299 if (ic->ic_status != ICP_S_OK) {
300 aprint_error_dev(ic->ic_dv, "request failed; status=0x%04x\n",
301 ic->ic_status);
302 bp->b_error = EIO;
303 bp->b_resid = bp->b_bcount;
304
305 icp->icp_evt.size = sizeof(icp->icp_evt.eu.sync);
306 icp->icp_evt.eu.sync.ionode = device_unit(icp->icp_dv);
307 icp->icp_evt.eu.sync.service = icp->icp_service;
308 icp->icp_evt.eu.sync.status = icp->icp_status;
309 icp->icp_evt.eu.sync.info = icp->icp_info;
310 icp->icp_evt.eu.sync.hostdrive = sc->sc_hwunit;
311 if (icp->icp_status >= 0x8000)
312 icp_store_event(icp, GDT_ES_SYNC, 0, &icp->icp_evt);
313 else
314 icp_store_event(icp, GDT_ES_SYNC, icp->icp_service,
315 &icp->icp_evt);
316 } else
317 bp->b_resid = 0;
318
319 icp_ccb_unmap(icp, ic);
320 icp_ccb_free(icp, ic);
321 lddone(&sc->sc_ld, bp);
322}
323
324void
325ld_icp_adjqparam(device_t dv, int openings)
326{
327
328 ldadjqparam((struct ld_softc *) dv, openings);
329}
330
331MODULE(MODULE_CLASS_DRIVER, ld_icp, "ld"); /* no icp module yet */
332
333#ifdef _MODULE
334/*
335 * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
336 * XXX it will be defined in the common-code module
337 */
338#undef CFDRIVER_DECL
339#define CFDRIVER_DECL(name, class, attr)
340#include "ioconf.c"
341#endif
342
343static int
344ld_icp_modcmd(modcmd_t cmd, void *opaque)
345{
346#ifdef _MODULE
347 /*
348 * We ignore the cfdriver_vec[] that ioconf provides, since
349 * the cfdrivers are attached already.
350 */
351 static struct cfdriver * const no_cfdriver_vec[] = { NULL };
352#endif
353 int error = 0;
354
355#ifdef _MODULE
356 switch (cmd) {
357 case MODULE_CMD_INIT:
358 error = config_init_component(no_cfdriver_vec,
359 cfattach_ioconf_ld_icp, cfdata_ioconf_ld_icp);
360 break;
361 case MODULE_CMD_FINI:
362 error = config_fini_component(no_cfdriver_vec,
363 cfattach_ioconf_ld_icp, cfdata_ioconf_ld_icp);
364 break;
365 default:
366 error = ENOTTY;
367 break;
368 }
369#endif
370
371 return error;
372}
373