xref: /netbsd-src/sys/dev/pci/ld_amr.c (revision 916bdfa5d4009feda043138aa060ae700ac3e0ec)
1 /*	$NetBSD: ld_amr.c,v 1.25 2016/09/27 03:33:32 pgoyette 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.25 2016/09/27 03:33:32 pgoyette Exp $");
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 #include <sys/buf.h>
44 #include <sys/bufq.h>
45 #include <sys/endian.h>
46 #include <sys/dkio.h>
47 #include <sys/disk.h>
48 #include <sys/module.h>
49 
50 #include <sys/bus.h>
51 
52 #include <dev/ldvar.h>
53 
54 #include <dev/pci/pcireg.h>
55 #include <dev/pci/pcivar.h>
56 #include <dev/pci/amrreg.h>
57 #include <dev/pci/amrvar.h>
58 
59 #include "ioconf.h"
60 
61 struct ld_amr_softc {
62 	struct	ld_softc sc_ld;
63 	int	sc_hwunit;
64 };
65 
66 static int	ld_amr_dobio(struct ld_amr_softc *, void *, int, int, int,
67 			     struct buf *);
68 static int	ld_amr_dump(struct ld_softc *, void *, int, int);
69 static void	ld_amr_handler(struct amr_ccb *);
70 static int	ld_amr_start(struct ld_softc *, struct buf *);
71 
72 static int
ld_amr_match(device_t parent,cfdata_t match,void * aux)73 ld_amr_match(device_t parent, cfdata_t match, void *aux)
74 {
75 	return (1);
76 }
77 
78 static void
ld_amr_attach(device_t parent,device_t self,void * aux)79 ld_amr_attach(device_t parent, device_t self, void *aux)
80 {
81 	struct amr_attach_args *amra = aux;
82 	struct ld_amr_softc *sc = device_private(self);
83 	struct ld_softc *ld = &sc->sc_ld;
84 	struct amr_softc *amr = device_private(parent);
85 	const char *statestr;
86 	int happy;
87 
88 	ld->sc_dv = self;
89 
90 	sc->sc_hwunit = amra->amra_unit;
91 	ld->sc_maxxfer = amr_max_xfer;
92 	ld->sc_maxqueuecnt = (amr->amr_maxqueuecnt - AMR_NCCB_RESV)
93 	    / amr->amr_numdrives;
94 	ld->sc_secperunit = amr->amr_drive[sc->sc_hwunit].al_size;
95 	ld->sc_secsize = AMR_SECTOR_SIZE;
96 	ld->sc_start = ld_amr_start;
97 	ld->sc_dump = ld_amr_dump;
98 
99 	if (ld->sc_maxqueuecnt > AMR_MAX_CMDS_PU)
100 		ld->sc_maxqueuecnt = AMR_MAX_CMDS_PU;
101 
102 	/*
103 	 * Print status information and attach to the ld driver proper.
104 	 */
105 	statestr = amr_drive_state(amr->amr_drive[sc->sc_hwunit].al_state,
106 	    &happy);
107 	if (happy)
108 		ld->sc_flags = LDF_ENABLED;
109 	aprint_normal(": RAID %d, %s\n",
110 	    amr->amr_drive[sc->sc_hwunit].al_properties & AMR_DRV_RAID_MASK,
111 	    statestr);
112 
113 	ldattach(ld, BUFQ_DISK_DEFAULT_STRAT);
114 }
115 
116 CFATTACH_DECL_NEW(ld_amr, sizeof(struct ld_amr_softc),
117     ld_amr_match, ld_amr_attach, NULL, NULL);
118 
119 static int
ld_amr_dobio(struct ld_amr_softc * sc,void * data,int datasize,int blkno,int dowrite,struct buf * bp)120 ld_amr_dobio(struct ld_amr_softc *sc, void *data, int datasize,
121 	     int blkno, int dowrite, struct buf *bp)
122 {
123 	struct amr_ccb *ac;
124 	struct amr_softc *amr;
125 	struct amr_mailbox_cmd *mb;
126 	int rv;
127 
128 	amr = device_private(device_parent(sc->sc_ld.sc_dv));
129 
130 	if ((rv = amr_ccb_alloc(amr, &ac)) != 0)
131 		return (rv);
132 
133 	mb = &ac->ac_cmd;
134 	mb->mb_command = (dowrite ? AMR_CMD_LWRITE : AMR_CMD_LREAD);
135 	mb->mb_drive = sc->sc_hwunit;
136 	mb->mb_blkcount = htole16(datasize / AMR_SECTOR_SIZE);
137 	mb->mb_lba = htole32(blkno);
138 
139 	rv = amr_ccb_map(amr, ac, data, datasize,
140 	    (dowrite ? AC_XFER_OUT : AC_XFER_IN));
141 	if (rv != 0) {
142 		amr_ccb_free(amr, ac);
143 		return (rv);
144 	}
145 
146 	if (bp == NULL) {
147 		/*
148 		 * Polled commands must not sit on the software queue.  Wait
149 		 * up to 30 seconds for the command to complete.
150 		 */
151 		rv = amr_ccb_poll(amr, ac, 30000);
152 		amr_ccb_unmap(amr, ac);
153 		amr_ccb_free(amr, ac);
154 	} else {
155 		ac->ac_handler = ld_amr_handler;
156 		ac->ac_context = bp;
157 		ac->ac_dv = sc->sc_ld.sc_dv;
158 		amr_ccb_enqueue(amr, ac);
159 		rv = 0;
160 	}
161 
162 	return (rv);
163 }
164 
165 static int
ld_amr_start(struct ld_softc * ld,struct buf * bp)166 ld_amr_start(struct ld_softc *ld, struct buf *bp)
167 {
168 	return (ld_amr_dobio((struct ld_amr_softc *)ld, bp->b_data,
169 	    bp->b_bcount, bp->b_rawblkno, (bp->b_flags & B_READ) == 0, bp));
170 }
171 
172 static void
ld_amr_handler(struct amr_ccb * ac)173 ld_amr_handler(struct amr_ccb *ac)
174 {
175 	struct buf *bp;
176 	struct ld_amr_softc *sc;
177 	struct amr_softc *amr;
178 
179 	bp = ac->ac_context;
180 	sc = device_private(ac->ac_dv);
181 	amr = device_private(device_parent(sc->sc_ld.sc_dv));
182 
183 	if (ac->ac_status != AMR_STATUS_SUCCESS) {
184 		printf("%s: cmd status 0x%02x\n", device_xname(sc->sc_ld.sc_dv),
185 		    ac->ac_status);
186 
187 		bp->b_error = EIO;
188 		bp->b_resid = bp->b_bcount;
189 	} else
190 		bp->b_resid = 0;
191 
192 	amr_ccb_unmap(amr, ac);
193 	amr_ccb_free(amr, ac);
194 	lddone(&sc->sc_ld, bp);
195 }
196 
197 static int
ld_amr_dump(struct ld_softc * ld,void * data,int blkno,int blkcnt)198 ld_amr_dump(struct ld_softc *ld, void *data, int blkno, int blkcnt)
199 {
200 	struct ld_amr_softc *sc;
201 
202 	sc = (struct ld_amr_softc *)ld;
203 
204 	return (ld_amr_dobio(sc, data, blkcnt * ld->sc_secsize, blkno, 1,
205 	    NULL));
206 }
207 
208 MODULE(MODULE_CLASS_DRIVER, ld_amr, "ld,amr");
209 
210 #ifdef _MODULE
211 /*
212  * XXX Don't allow ioconf.c to redefine the "struct cfdriver ld_cd"
213  * XXX it will be defined in the common-code module
214  */
215 #undef  CFDRIVER_DECL
216 #define CFDRIVER_DECL(name, class, attr)
217 #include "ioconf.c"
218 #endif
219 
220 static int
ld_amr_modcmd(modcmd_t cmd,void * opaque)221 ld_amr_modcmd(modcmd_t cmd, void *opaque)
222 {
223 #ifdef _MODULE
224 	/*
225 	 * We ignore the cfdriver_vec[] that ioconf provides, since
226 	 * the cfdrivers are attached already.
227 	 */
228 	static struct cfdriver * const no_cfdriver_vec[] = { NULL };
229 #endif
230 	int error = 0;
231 
232 #ifdef _MODULE
233 	switch (cmd) {
234 	case MODULE_CMD_INIT:
235 		error = config_init_component(no_cfdriver_vec,
236 		    cfattach_ioconf_ld_amr, cfdata_ioconf_ld_amr);
237 		break;
238 	case MODULE_CMD_FINI:
239 		error = config_fini_component(no_cfdriver_vec,
240 		    cfattach_ioconf_ld_amr, cfdata_ioconf_ld_amr);
241 		break;
242 	default:
243 		error = ENOTTY;
244 		break;
245 	}
246 #endif
247 
248 	return error;
249 }
250