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