xref: /netbsd-src/sys/dev/pci/ld_amr.c (revision d48f14661dda8638fee055ba15d35bdfb29b9fa8)
1 /*	$NetBSD: ld_amr.c,v 1.9 2006/03/25 04:12:36 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 2002, 2003 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.9 2006/03/25 04:12:36 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/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 <uvm/uvm_extern.h>
62 
63 #include <machine/bus.h>
64 
65 #include <dev/ldvar.h>
66 
67 #include <dev/pci/pcireg.h>
68 #include <dev/pci/pcivar.h>
69 #include <dev/pci/amrreg.h>
70 #include <dev/pci/amrvar.h>
71 
72 struct ld_amr_softc {
73 	struct	ld_softc sc_ld;
74 	int	sc_hwunit;
75 };
76 
77 static int	ld_amr_dobio(struct ld_amr_softc *, void *, int, int, int,
78 			     struct buf *);
79 static int	ld_amr_dump(struct ld_softc *, void *, int, int);
80 static void	ld_amr_handler(struct amr_ccb *);
81 static int	ld_amr_start(struct ld_softc *, struct buf *);
82 
83 static int
84 ld_amr_match(struct device *parent, struct cfdata *match, void *aux)
85 {
86 
87 	return (1);
88 }
89 
90 static void
91 ld_amr_attach(struct device *parent, struct device *self, void *aux)
92 {
93 	struct amr_attach_args *amra;
94 	struct ld_amr_softc *sc;
95 	struct ld_softc *ld;
96 	struct amr_softc *amr;
97 	const char *statestr;
98 	int happy;
99 
100 	sc = (struct ld_amr_softc *)self;
101 	ld = &sc->sc_ld;
102 	amr = (struct amr_softc *)parent;
103 	amra = aux;
104 
105 	sc->sc_hwunit = amra->amra_unit;
106 	ld->sc_maxxfer = amr_max_xfer;
107 	ld->sc_maxqueuecnt = (amr->amr_maxqueuecnt - AMR_NCCB_RESV)
108 	    / amr->amr_numdrives;
109 	ld->sc_secperunit = amr->amr_drive[sc->sc_hwunit].al_size;
110 	ld->sc_secsize = AMR_SECTOR_SIZE;
111 	ld->sc_start = ld_amr_start;
112 	ld->sc_dump = ld_amr_dump;
113 
114 	if (ld->sc_maxqueuecnt > AMR_MAX_CMDS_PU)
115 		ld->sc_maxqueuecnt = AMR_MAX_CMDS_PU;
116 
117 	/*
118 	 * Print status information and attach to the ld driver proper.
119 	 */
120 	statestr = amr_drive_state(amr->amr_drive[sc->sc_hwunit].al_state,
121 	    &happy);
122 	if (happy)
123 		ld->sc_flags = LDF_ENABLED;
124 	aprint_normal(": RAID %d, %s\n",
125 	    amr->amr_drive[sc->sc_hwunit].al_properties & AMR_DRV_RAID_MASK,
126 	    statestr);
127 
128 	ldattach(ld);
129 }
130 
131 CFATTACH_DECL(ld_amr, sizeof(struct ld_amr_softc),
132     ld_amr_match, ld_amr_attach, NULL, NULL);
133 
134 static int
135 ld_amr_dobio(struct ld_amr_softc *sc, void *data, int datasize,
136 	     int blkno, int dowrite, struct buf *bp)
137 {
138 	struct amr_ccb *ac;
139 	struct amr_softc *amr;
140 	struct amr_mailbox_cmd *mb;
141 	int s, rv;
142 
143 	amr = (struct amr_softc *)device_parent(&sc->sc_ld.sc_dv);
144 
145 	if ((rv = amr_ccb_alloc(amr, &ac)) != 0)
146 		return (rv);
147 
148 	mb = &ac->ac_cmd;
149 	mb->mb_command = (dowrite ? AMR_CMD_LWRITE : AMR_CMD_LREAD);
150 	mb->mb_drive = sc->sc_hwunit;
151 	mb->mb_blkcount = htole16(datasize / AMR_SECTOR_SIZE);
152 	mb->mb_lba = htole32(blkno);
153 
154 	if ((rv = amr_ccb_map(amr, ac, data, datasize, dowrite)) != 0) {
155 		amr_ccb_free(amr, ac);
156 		return (rv);
157 	}
158 
159 	if (bp == NULL) {
160 		/*
161 		 * Polled commands must not sit on the software queue.  Wait
162 		 * up to 30 seconds for the command to complete.
163 		 */
164 		s = splbio();
165 		rv = amr_ccb_poll(amr, ac, 30000);
166 		splx(s);
167 		amr_ccb_unmap(amr, ac);
168 		amr_ccb_free(amr, ac);
169 	} else {
170 		ac->ac_handler = ld_amr_handler;
171 		ac->ac_context = bp;
172 		ac->ac_dv = (struct device *)sc;
173 		amr_ccb_enqueue(amr, ac);
174 		rv = 0;
175 	}
176 
177 	return (rv);
178 }
179 
180 static int
181 ld_amr_start(struct ld_softc *ld, struct buf *bp)
182 {
183 
184 	return (ld_amr_dobio((struct ld_amr_softc *)ld, bp->b_data,
185 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
186 }
187 
188 static void
189 ld_amr_handler(struct amr_ccb *ac)
190 {
191 	struct buf *bp;
192 	struct ld_amr_softc *sc;
193 	struct amr_softc *amr;
194 
195 	bp = ac->ac_context;
196 	sc = (struct ld_amr_softc *)ac->ac_dv;
197 	amr = (struct amr_softc *)device_parent(&sc->sc_ld.sc_dv);
198 
199 	if (ac->ac_status != AMR_STATUS_SUCCESS) {
200 		printf("%s: cmd status 0x%02x\n", sc->sc_ld.sc_dv.dv_xname,
201 		    ac->ac_status);
202 
203 		bp->b_flags |= B_ERROR;
204 		bp->b_error = EIO;
205 		bp->b_resid = bp->b_bcount;
206 	} else
207 		bp->b_resid = 0;
208 
209 	amr_ccb_unmap(amr, ac);
210 	amr_ccb_free(amr, ac);
211 	lddone(&sc->sc_ld, bp);
212 }
213 
214 static int
215 ld_amr_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
216 {
217 	struct ld_amr_softc *sc;
218 
219 	sc = (struct ld_amr_softc *)ld;
220 
221 	return (ld_amr_dobio(sc, data, blkcnt * ld->sc_secsize, blkno, 1,
222 	    NULL));
223 }
224