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