xref: /netbsd-src/sys/dev/pci/ld_amr.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: ld_amr.c,v 1.1 2002/01/30 14:35:45 ad 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  * 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  * AMI RAID controller front-end for ld(4) driver.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: ld_amr.c,v 1.1 2002/01/30 14:35:45 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/endian.h>
54 #include <sys/dkio.h>
55 #include <sys/disk.h>
56 #if NRND > 0
57 #include <sys/rnd.h>
58 #endif
59 
60 #include <uvm/uvm_extern.h>
61 
62 #include <machine/bus.h>
63 
64 #include <dev/ldvar.h>
65 
66 #include <dev/pci/amrreg.h>
67 #include <dev/pci/amrvar.h>
68 
69 struct ld_amr_softc {
70 	struct	ld_softc sc_ld;
71 	int	sc_hwunit;
72 };
73 
74 void	ld_amr_attach(struct device *, struct device *, void *);
75 int	ld_amr_dobio(struct ld_amr_softc *, void *, int, int, int,
76 		     struct buf *);
77 int	ld_amr_dump(struct ld_softc *, void *, int, int);
78 void	ld_amr_handler(struct amr_ccb *);
79 int	ld_amr_match(struct device *, struct cfdata *, void *);
80 int	ld_amr_start(struct ld_softc *, struct buf *);
81 
82 struct cfattach ld_amr_ca = {
83 	sizeof(struct ld_amr_softc), ld_amr_match, ld_amr_attach
84 };
85 
86 struct {
87 	const char	*ds_descr;
88 	int	ds_enable;
89 } static const ld_amr_dstate[] = {
90 	{ "offline",	0 },
91 	{ "degraded",	LDF_ENABLED },
92 	{ "optimal",	LDF_ENABLED },
93 	{ "online",	LDF_ENABLED },
94 	{ "failed",	0 },
95 	{ "rebuilding",	LDF_ENABLED },
96 	{ "hotspare",	0 },
97 };
98 
99 int
100 ld_amr_match(struct device *parent, struct cfdata *match, void *aux)
101 {
102 
103 	return (1);
104 }
105 
106 void
107 ld_amr_attach(struct device *parent, struct device *self, void *aux)
108 {
109 	struct amr_attach_args *amra;
110 	struct ld_amr_softc *sc;
111 	struct ld_softc *ld;
112 	struct amr_softc *amr;
113 	const char *statestr;
114 	int state;
115 
116 	sc = (struct ld_amr_softc *)self;
117 	ld = &sc->sc_ld;
118 	amr = (struct amr_softc *)parent;
119 	amra = aux;
120 
121 	sc->sc_hwunit = amra->amra_unit;
122 	ld->sc_maxxfer = AMR_MAX_XFER;
123 	ld->sc_maxqueuecnt = amr->amr_maxqueuecnt / amr->amr_numdrives;
124 	ld->sc_secperunit = amr->amr_drive[sc->sc_hwunit].al_size;
125 	ld->sc_secsize = AMR_SECTOR_SIZE;
126 	ld->sc_start = ld_amr_start;
127 	ld->sc_dump = ld_amr_dump;
128 
129 	if (ld->sc_maxqueuecnt > AMR_MAX_CMDS_PU)
130 		ld->sc_maxqueuecnt = AMR_MAX_CMDS_PU;
131 
132 	/*
133 	 * Print status information and attach to the ld driver proper.
134 	 */
135 	state = AMR_DRV_CURSTATE(amr->amr_drive[sc->sc_hwunit].al_state);
136 	if (state >= sizeof(ld_amr_dstate) / sizeof(ld_amr_dstate[0])) {
137 		ld->sc_flags = LDF_ENABLED;
138 		statestr = "status unknown";
139 	} else {
140 		ld->sc_flags = ld_amr_dstate[state].ds_enable;
141 		statestr = ld_amr_dstate[state].ds_descr;
142 	}
143 
144 	printf(": RAID %d, %s\n",
145 	    amr->amr_drive[sc->sc_hwunit].al_properties & AMR_DRV_RAID_MASK,
146 	    statestr);
147 
148 	ldattach(ld);
149 }
150 
151 int
152 ld_amr_dobio(struct ld_amr_softc *sc, void *data, int datasize,
153 	     int blkno, int dowrite, struct buf *bp)
154 {
155 	struct amr_ccb *ac;
156 	struct amr_softc *amr;
157 	struct amr_mailbox *mb;
158 	int s, rv;
159 
160 	amr = (struct amr_softc *)sc->sc_ld.sc_dv.dv_parent;
161 
162 	if ((rv = amr_ccb_alloc(amr, &ac)) != 0)
163 		return (rv);
164 
165 	mb = &ac->ac_mbox;
166 	mb->mb_command = (dowrite ? AMR_CMD_LWRITE : AMR_CMD_LREAD);
167 	mb->mb_drive = sc->sc_hwunit;
168 	mb->mb_blkcount = htole16(datasize / AMR_SECTOR_SIZE);
169 	mb->mb_lba = htole32(blkno);
170 
171 	if ((rv = amr_ccb_map(amr, ac, data, datasize, dowrite)) != 0) {
172 		amr_ccb_free(amr, ac);
173 		return (rv);
174 	}
175 
176 	if (bp == NULL) {
177 		/*
178 		 * Polled commands must not sit on the software queue.  Wait
179 		 * up to 30 seconds for the command to complete.
180 		 */
181 		s = splbio();
182 		rv = amr_ccb_poll(amr, ac, 30000);
183 		splx(s);
184 		amr_ccb_unmap(amr, ac);
185 		amr_ccb_free(amr, ac);
186 	} else {
187 		ac->ac_handler = ld_amr_handler;
188 		ac->ac_context = bp;
189 		ac->ac_dv = (struct device *)sc;
190 		amr_ccb_enqueue(amr, ac);
191 		rv = 0;
192 	}
193 
194 	return (rv);
195 }
196 
197 int
198 ld_amr_start(struct ld_softc *ld, struct buf *bp)
199 {
200 
201 	return (ld_amr_dobio((struct ld_amr_softc *)ld, bp->b_data,
202 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
203 }
204 
205 void
206 ld_amr_handler(struct amr_ccb *ac)
207 {
208 	struct buf *bp;
209 	struct ld_amr_softc *sc;
210 	struct amr_softc *amr;
211 
212 	bp = ac->ac_context;
213 	sc = (struct ld_amr_softc *)ac->ac_dv;
214 	amr = (struct amr_softc *)sc->sc_ld.sc_dv.dv_parent;
215 
216 	if (ac->ac_status != AMR_STATUS_SUCCESS) {
217 		printf("%s: cmd status 0x%02x\n", sc->sc_ld.sc_dv.dv_xname,
218 		    ac->ac_status);
219 
220 		bp->b_flags |= B_ERROR;
221 		bp->b_error = EIO;
222 		bp->b_resid = bp->b_bcount;
223 	} else
224 		bp->b_resid = 0;
225 
226 	amr_ccb_unmap(amr, ac);
227 	amr_ccb_free(amr, ac);
228 	lddone(&sc->sc_ld, bp);
229 }
230 
231 int
232 ld_amr_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
233 {
234 	struct ld_amr_softc *sc;
235 
236 	sc = (struct ld_amr_softc *)ld;
237 
238 	return (ld_amr_dobio(sc, data, blkcnt * ld->sc_secsize, blkno, 1,
239 	    NULL));
240 }
241