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