xref: /netbsd-src/sys/dev/ic/ld_cac.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: ld_cac.c,v 1.27 2012/02/24 18:04:51 mhitch Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2006 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  * Compaq array controller front-end for ld(4) driver.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ld_cac.c,v 1.27 2012/02/24 18:04:51 mhitch 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/cacreg.h>
55 #include <dev/ic/cacvar.h>
56 
57 struct ld_cac_softc {
58 	struct	ld_softc sc_ld;
59 	kmutex_t *sc_mutex;
60 	int	sc_hwunit;
61 	int	sc_serrcnt;
62 	struct	timeval sc_serrtm;
63 };
64 
65 void	ld_cac_attach(device_t, device_t, void *);
66 void	ld_cac_done(device_t, void *, int);
67 int	ld_cac_dump(struct ld_softc *, void *, int, int);
68 int	ld_cac_match(device_t, cfdata_t, void *);
69 int	ld_cac_start(struct ld_softc *, struct buf *);
70 
71 static const struct	timeval ld_cac_serrintvl = { 60, 0 };
72 
73 CFATTACH_DECL_NEW(ld_cac, sizeof(struct ld_cac_softc),
74     ld_cac_match, ld_cac_attach, NULL, NULL);
75 
76 int
77 ld_cac_match(device_t parent, cfdata_t match, void *aux)
78 {
79 
80 	return (1);
81 }
82 
83 void
84 ld_cac_attach(device_t parent, device_t self, void *aux)
85 {
86 	struct cac_drive_info dinfo;
87 	struct cac_attach_args *caca;
88 	struct ld_cac_softc *sc = device_private(self);
89 	struct cac_softc *cac = device_private(parent);
90 	struct ld_softc *ld = &sc->sc_ld;
91 	const char *type;
92 
93 	caca = aux;
94 	ld->sc_dv = self;
95 	sc->sc_mutex = &cac->sc_mutex;
96 	sc->sc_hwunit = caca->caca_unit;
97 
98 	if (cac_cmd(cac, CAC_CMD_GET_LOG_DRV_INFO, &dinfo, sizeof(dinfo),
99 	    sc->sc_hwunit, 0, CAC_CCB_DATA_IN, NULL)) {
100 		aprint_error(": CMD_GET_LOG_DRV_INFO failed\n");
101 		return;
102 	}
103 
104 	ld->sc_secsize = CAC_GET2(dinfo.secsize);
105 	ld->sc_maxxfer = CAC_MAX_XFER;
106 	ld->sc_maxqueuecnt = (CAC_MAX_CCBS - 1) / cac->sc_nunits;
107 	ld->sc_secperunit = CAC_GET2(dinfo.ncylinders) *
108 	    CAC_GET1(dinfo.nheads) * CAC_GET1(dinfo.nsectors);
109 	ld->sc_start = ld_cac_start;
110 	ld->sc_dump = ld_cac_dump;
111 
112 	switch (CAC_GET1(dinfo.mirror)) {
113 	case 0:
114 		type = "standalone disk or RAID0";
115 		break;
116 	case 1:
117 		type = "RAID4";
118 		break;
119 	case 2:
120 		type = "RAID1";
121 		break;
122 	case 3:
123 		type = "RAID5";
124 		break;
125 	default:
126 		type = "unknown type of";
127 		break;
128 	}
129 
130 	aprint_normal(": %s array\n", type);
131 
132 	/* XXX We should verify this... */
133 	ld->sc_flags = LDF_ENABLED;
134 	ldattach(ld);
135 }
136 
137 int
138 ld_cac_start(struct ld_softc *ld, struct buf *bp)
139 {
140 	int flags, cmd;
141 	struct cac_softc *cac;
142 	struct ld_cac_softc *sc;
143 	struct cac_context cc;
144 
145 	sc = (struct ld_cac_softc *)ld;
146 	cac = device_private(device_parent(ld->sc_dv));
147 
148 	cc.cc_handler = ld_cac_done;
149 	cc.cc_context = bp;
150 	cc.cc_dv = ld->sc_dv;
151 
152 	if ((bp->b_flags & B_READ) == 0) {
153 		cmd = CAC_CMD_WRITE;
154 		flags = CAC_CCB_DATA_OUT;
155 	} else {
156 		cmd = CAC_CMD_READ;
157 		flags = CAC_CCB_DATA_IN;
158 	}
159 
160 	return (cac_cmd(cac, cmd, bp->b_data, bp->b_bcount, sc->sc_hwunit,
161 	    bp->b_rawblkno, flags, &cc));
162 }
163 
164 int
165 ld_cac_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
166 {
167 	struct ld_cac_softc *sc;
168 
169 	sc = (struct ld_cac_softc *)ld;
170 
171 	return (cac_cmd(device_private(device_parent(ld->sc_dv)),
172 	    CAC_CMD_WRITE_MEDIA, data, blkcnt * ld->sc_secsize,
173 	    sc->sc_hwunit, blkno, CAC_CCB_DATA_OUT, NULL));
174 }
175 
176 void
177 ld_cac_done(device_t dv, void *context, int error)
178 {
179 	struct buf *bp;
180 	struct ld_cac_softc *sc;
181 	int rv;
182 
183 	bp = context;
184 	rv = 0;
185 	sc = device_private(dv);
186 
187 	if ((error & CAC_RET_CMD_REJECTED) == CAC_RET_CMD_REJECTED) {
188 		aprint_error_dev(dv, "command rejected\n");
189 		rv = EIO;
190 	}
191 	if (rv == 0 && (error & CAC_RET_INVAL_BLOCK) != 0) {
192 		aprint_error_dev(dv, "invalid request block\n");
193 		rv = EIO;
194 	}
195 	if (rv == 0 && (error & CAC_RET_HARD_ERROR) != 0) {
196 		aprint_error_dev(dv, "hard error\n");
197 		rv = EIO;
198 	}
199 	if (rv == 0 && (error & CAC_RET_SOFT_ERROR) != 0) {
200 		sc->sc_serrcnt++;
201 		if (ratecheck(&sc->sc_serrtm, &ld_cac_serrintvl)) {
202 			sc->sc_serrcnt = 0;
203 			aprint_error_dev(dv,
204 			    "%d soft errors; array may be degraded\n",
205 			    sc->sc_serrcnt);
206 		}
207 	}
208 
209 	if (rv) {
210 		bp->b_error = rv;
211 		bp->b_resid = bp->b_bcount;
212 	} else
213 		bp->b_resid = 0;
214 
215 	mutex_exit(sc->sc_mutex);
216 	lddone(&sc->sc_ld, bp);
217 	mutex_enter(sc->sc_mutex);
218 }
219