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