xref: /netbsd-src/sys/dev/pci/ld_amr.c (revision cac8e449158efc7261bebc8657cbb0125a2cfdde)
1 /*	$NetBSD: ld_amr.c,v 1.16 2008/04/28 20:23:55 martin 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  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * AMI RAID controller front-end for ld(4) driver.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: ld_amr.c,v 1.16 2008/04/28 20:23:55 martin Exp $");
38 
39 #include "rnd.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/device.h>
45 #include <sys/buf.h>
46 #include <sys/bufq.h>
47 #include <sys/endian.h>
48 #include <sys/dkio.h>
49 #include <sys/disk.h>
50 #if NRND > 0
51 #include <sys/rnd.h>
52 #endif
53 
54 #include <uvm/uvm_extern.h>
55 
56 #include <sys/bus.h>
57 
58 #include <dev/ldvar.h>
59 
60 #include <dev/pci/pcireg.h>
61 #include <dev/pci/pcivar.h>
62 #include <dev/pci/amrreg.h>
63 #include <dev/pci/amrvar.h>
64 
65 struct ld_amr_softc {
66 	struct	ld_softc sc_ld;
67 	int	sc_hwunit;
68 };
69 
70 static int	ld_amr_dobio(struct ld_amr_softc *, void *, int, int, int,
71 			     struct buf *);
72 static int	ld_amr_dump(struct ld_softc *, void *, int, int);
73 static void	ld_amr_handler(struct amr_ccb *);
74 static int	ld_amr_start(struct ld_softc *, struct buf *);
75 
76 static int
77 ld_amr_match(struct device *parent, struct cfdata *match,
78     void *aux)
79 {
80 
81 	return (1);
82 }
83 
84 static void
85 ld_amr_attach(struct device *parent, struct device *self, void *aux)
86 {
87 	struct amr_attach_args *amra;
88 	struct ld_amr_softc *sc;
89 	struct ld_softc *ld;
90 	struct amr_softc *amr;
91 	const char *statestr;
92 	int happy;
93 
94 	sc = (struct ld_amr_softc *)self;
95 	ld = &sc->sc_ld;
96 	amr = (struct amr_softc *)parent;
97 	amra = aux;
98 
99 	sc->sc_hwunit = amra->amra_unit;
100 	ld->sc_maxxfer = amr_max_xfer;
101 	ld->sc_maxqueuecnt = (amr->amr_maxqueuecnt - AMR_NCCB_RESV)
102 	    / amr->amr_numdrives;
103 	ld->sc_secperunit = amr->amr_drive[sc->sc_hwunit].al_size;
104 	ld->sc_secsize = AMR_SECTOR_SIZE;
105 	ld->sc_start = ld_amr_start;
106 	ld->sc_dump = ld_amr_dump;
107 
108 	if (ld->sc_maxqueuecnt > AMR_MAX_CMDS_PU)
109 		ld->sc_maxqueuecnt = AMR_MAX_CMDS_PU;
110 
111 	/*
112 	 * Print status information and attach to the ld driver proper.
113 	 */
114 	statestr = amr_drive_state(amr->amr_drive[sc->sc_hwunit].al_state,
115 	    &happy);
116 	if (happy)
117 		ld->sc_flags = LDF_ENABLED;
118 	aprint_normal(": RAID %d, %s\n",
119 	    amr->amr_drive[sc->sc_hwunit].al_properties & AMR_DRV_RAID_MASK,
120 	    statestr);
121 
122 	ldattach(ld);
123 }
124 
125 CFATTACH_DECL(ld_amr, sizeof(struct ld_amr_softc),
126     ld_amr_match, ld_amr_attach, NULL, NULL);
127 
128 static int
129 ld_amr_dobio(struct ld_amr_softc *sc, void *data, int datasize,
130 	     int blkno, int dowrite, struct buf *bp)
131 {
132 	struct amr_ccb *ac;
133 	struct amr_softc *amr;
134 	struct amr_mailbox_cmd *mb;
135 	int s, rv;
136 
137 	amr = (struct amr_softc *)device_parent(&sc->sc_ld.sc_dv);
138 
139 	if ((rv = amr_ccb_alloc(amr, &ac)) != 0)
140 		return (rv);
141 
142 	mb = &ac->ac_cmd;
143 	mb->mb_command = (dowrite ? AMR_CMD_LWRITE : AMR_CMD_LREAD);
144 	mb->mb_drive = sc->sc_hwunit;
145 	mb->mb_blkcount = htole16(datasize / AMR_SECTOR_SIZE);
146 	mb->mb_lba = htole32(blkno);
147 
148 	rv = amr_ccb_map(amr, ac, data, datasize,
149 	    (dowrite ? AC_XFER_OUT : AC_XFER_IN));
150 	if (rv != 0) {
151 		amr_ccb_free(amr, ac);
152 		return (rv);
153 	}
154 
155 	if (bp == NULL) {
156 		/*
157 		 * Polled commands must not sit on the software queue.  Wait
158 		 * up to 30 seconds for the command to complete.
159 		 */
160 		s = splbio();
161 		rv = amr_ccb_poll(amr, ac, 30000);
162 		splx(s);
163 		amr_ccb_unmap(amr, ac);
164 		amr_ccb_free(amr, ac);
165 	} else {
166 		ac->ac_handler = ld_amr_handler;
167 		ac->ac_context = bp;
168 		ac->ac_dv = (struct device *)sc;
169 		amr_ccb_enqueue(amr, ac);
170 		rv = 0;
171 	}
172 
173 	return (rv);
174 }
175 
176 static int
177 ld_amr_start(struct ld_softc *ld, struct buf *bp)
178 {
179 
180 	return (ld_amr_dobio((struct ld_amr_softc *)ld, bp->b_data,
181 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
182 }
183 
184 static void
185 ld_amr_handler(struct amr_ccb *ac)
186 {
187 	struct buf *bp;
188 	struct ld_amr_softc *sc;
189 	struct amr_softc *amr;
190 
191 	bp = ac->ac_context;
192 	sc = (struct ld_amr_softc *)ac->ac_dv;
193 	amr = (struct amr_softc *)device_parent(&sc->sc_ld.sc_dv);
194 
195 	if (ac->ac_status != AMR_STATUS_SUCCESS) {
196 		printf("%s: cmd status 0x%02x\n", device_xname(&sc->sc_ld.sc_dv),
197 		    ac->ac_status);
198 
199 		bp->b_error = EIO;
200 		bp->b_resid = bp->b_bcount;
201 	} else
202 		bp->b_resid = 0;
203 
204 	amr_ccb_unmap(amr, ac);
205 	amr_ccb_free(amr, ac);
206 	lddone(&sc->sc_ld, bp);
207 }
208 
209 static int
210 ld_amr_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
211 {
212 	struct ld_amr_softc *sc;
213 
214 	sc = (struct ld_amr_softc *)ld;
215 
216 	return (ld_amr_dobio(sc, data, blkcnt * ld->sc_secsize, blkno, 1,
217 	    NULL));
218 }
219