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