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