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