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