arcmsr.c revision 1.8.2.3 1 1.8.2.3 yamt /* $NetBSD: arcmsr.c,v 1.8.2.3 2008/01/21 09:43:35 yamt Exp $ */
2 1.8.2.2 yamt /* $OpenBSD: arc.c,v 1.68 2007/10/27 03:28:27 dlg Exp $ */
3 1.8.2.2 yamt
4 1.8.2.2 yamt /*
5 1.8.2.3 yamt * Copyright (c) 2007 Juan Romero Pardines <xtraeme (at) netbsd.org>
6 1.8.2.2 yamt * Copyright (c) 2006 David Gwynne <dlg (at) openbsd.org>
7 1.8.2.2 yamt *
8 1.8.2.2 yamt * Permission to use, copy, modify, and distribute this software for any
9 1.8.2.2 yamt * purpose with or without fee is hereby granted, provided that the above
10 1.8.2.2 yamt * copyright notice and this permission notice appear in all copies.
11 1.8.2.2 yamt *
12 1.8.2.2 yamt * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13 1.8.2.2 yamt * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14 1.8.2.2 yamt * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15 1.8.2.2 yamt * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16 1.8.2.2 yamt * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 1.8.2.2 yamt * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 1.8.2.2 yamt * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 1.8.2.2 yamt */
20 1.8.2.2 yamt
21 1.8.2.2 yamt #include "bio.h"
22 1.8.2.2 yamt
23 1.8.2.2 yamt #include <sys/cdefs.h>
24 1.8.2.3 yamt __KERNEL_RCSID(0, "$NetBSD: arcmsr.c,v 1.8.2.3 2008/01/21 09:43:35 yamt Exp $");
25 1.8.2.2 yamt
26 1.8.2.2 yamt #include <sys/param.h>
27 1.8.2.2 yamt #include <sys/buf.h>
28 1.8.2.2 yamt #include <sys/kernel.h>
29 1.8.2.2 yamt #include <sys/malloc.h>
30 1.8.2.2 yamt #include <sys/device.h>
31 1.8.2.2 yamt #include <sys/kmem.h>
32 1.8.2.2 yamt #include <sys/kthread.h>
33 1.8.2.2 yamt #include <sys/mutex.h>
34 1.8.2.2 yamt #include <sys/condvar.h>
35 1.8.2.2 yamt #include <sys/rwlock.h>
36 1.8.2.2 yamt
37 1.8.2.2 yamt #if NBIO > 0
38 1.8.2.2 yamt #include <sys/ioctl.h>
39 1.8.2.2 yamt #include <dev/biovar.h>
40 1.8.2.2 yamt #endif
41 1.8.2.2 yamt
42 1.8.2.2 yamt #include <dev/pci/pcireg.h>
43 1.8.2.2 yamt #include <dev/pci/pcivar.h>
44 1.8.2.2 yamt #include <dev/pci/pcidevs.h>
45 1.8.2.2 yamt
46 1.8.2.2 yamt #include <dev/scsipi/scsipi_all.h>
47 1.8.2.2 yamt #include <dev/scsipi/scsi_all.h>
48 1.8.2.2 yamt #include <dev/scsipi/scsiconf.h>
49 1.8.2.2 yamt
50 1.8.2.2 yamt #include <dev/sysmon/sysmonvar.h>
51 1.8.2.2 yamt
52 1.8.2.2 yamt #include <sys/bus.h>
53 1.8.2.2 yamt
54 1.8.2.2 yamt #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
55 1.8.2.2 yamt
56 1.8.2.2 yamt #include <dev/pci/arcmsrvar.h>
57 1.8.2.2 yamt
58 1.8.2.2 yamt /* #define ARC_DEBUG */
59 1.8.2.2 yamt #ifdef ARC_DEBUG
60 1.8.2.2 yamt #define ARC_D_INIT (1<<0)
61 1.8.2.2 yamt #define ARC_D_RW (1<<1)
62 1.8.2.2 yamt #define ARC_D_DB (1<<2)
63 1.8.2.2 yamt
64 1.8.2.2 yamt int arcdebug = 0;
65 1.8.2.2 yamt
66 1.8.2.2 yamt #define DPRINTF(p...) do { if (arcdebug) printf(p); } while (0)
67 1.8.2.2 yamt #define DNPRINTF(n, p...) do { if ((n) & arcdebug) printf(p); } while (0)
68 1.8.2.2 yamt
69 1.8.2.2 yamt #else
70 1.8.2.2 yamt #define DPRINTF(p...) /* p */
71 1.8.2.2 yamt #define DNPRINTF(n, p...) /* n, p */
72 1.8.2.2 yamt #endif
73 1.8.2.2 yamt
74 1.8.2.2 yamt /*
75 1.8.2.2 yamt * the fw header must always equal this.
76 1.8.2.2 yamt */
77 1.8.2.2 yamt static struct arc_fw_hdr arc_fw_hdr = { 0x5e, 0x01, 0x61 };
78 1.8.2.2 yamt
79 1.8.2.2 yamt /*
80 1.8.2.2 yamt * autoconf(9) glue.
81 1.8.2.2 yamt */
82 1.8.2.2 yamt static int arc_match(device_t, struct cfdata *, void *);
83 1.8.2.2 yamt static void arc_attach(device_t, device_t, void *);
84 1.8.2.2 yamt static int arc_detach(device_t, int);
85 1.8.2.2 yamt static void arc_shutdown(void *);
86 1.8.2.2 yamt static int arc_intr(void *);
87 1.8.2.2 yamt static void arc_minphys(struct buf *);
88 1.8.2.2 yamt
89 1.8.2.2 yamt CFATTACH_DECL(arcmsr, sizeof(struct arc_softc),
90 1.8.2.2 yamt arc_match, arc_attach, arc_detach, NULL);
91 1.8.2.2 yamt
92 1.8.2.2 yamt /*
93 1.8.2.2 yamt * bio(4) and sysmon_envsys(9) glue.
94 1.8.2.2 yamt */
95 1.8.2.2 yamt #if NBIO > 0
96 1.8.2.2 yamt static int arc_bioctl(struct device *, u_long, void *);
97 1.8.2.2 yamt static int arc_bio_inq(struct arc_softc *, struct bioc_inq *);
98 1.8.2.2 yamt static int arc_bio_vol(struct arc_softc *, struct bioc_vol *);
99 1.8.2.3 yamt static int arc_bio_disk_volume(struct arc_softc *, struct bioc_disk *);
100 1.8.2.3 yamt static int arc_bio_disk_novol(struct arc_softc *, struct bioc_disk *);
101 1.8.2.3 yamt static void arc_bio_disk_filldata(struct arc_softc *, struct bioc_disk *,
102 1.8.2.3 yamt struct arc_fw_diskinfo *, int);
103 1.8.2.2 yamt static int arc_bio_alarm(struct arc_softc *, struct bioc_alarm *);
104 1.8.2.2 yamt static int arc_bio_alarm_state(struct arc_softc *, struct bioc_alarm *);
105 1.8.2.2 yamt static int arc_bio_getvol(struct arc_softc *, int,
106 1.8.2.2 yamt struct arc_fw_volinfo *);
107 1.8.2.3 yamt static int arc_bio_setstate(struct arc_softc *, struct bioc_setstate *);
108 1.8.2.3 yamt static int arc_bio_volops(struct arc_softc *, struct bioc_volops *);
109 1.8.2.2 yamt static void arc_create_sensors(void *);
110 1.8.2.2 yamt static void arc_refresh_sensors(struct sysmon_envsys *, envsys_data_t *);
111 1.8.2.3 yamt static int arc_fw_parse_status_code(struct arc_softc *, uint8_t *);
112 1.8.2.2 yamt #endif
113 1.8.2.2 yamt
114 1.8.2.2 yamt static int
115 1.8.2.2 yamt arc_match(device_t parent, struct cfdata *match, void *aux)
116 1.8.2.2 yamt {
117 1.8.2.2 yamt struct pci_attach_args *pa = aux;
118 1.8.2.2 yamt
119 1.8.2.2 yamt if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_ARECA) {
120 1.8.2.2 yamt switch (PCI_PRODUCT(pa->pa_id)) {
121 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1110:
122 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1120:
123 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1130:
124 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1160:
125 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1170:
126 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1200:
127 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1202:
128 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1210:
129 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1220:
130 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1230:
131 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1260:
132 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1270:
133 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1280:
134 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1380:
135 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1381:
136 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1680:
137 1.8.2.2 yamt case PCI_PRODUCT_ARECA_ARC1681:
138 1.8.2.2 yamt return 1;
139 1.8.2.2 yamt default:
140 1.8.2.2 yamt break;
141 1.8.2.2 yamt }
142 1.8.2.2 yamt }
143 1.8.2.2 yamt
144 1.8.2.2 yamt return 0;
145 1.8.2.2 yamt }
146 1.8.2.2 yamt
147 1.8.2.2 yamt static void
148 1.8.2.2 yamt arc_attach(device_t parent, device_t self, void *aux)
149 1.8.2.2 yamt {
150 1.8.2.2 yamt struct arc_softc *sc = device_private(self);
151 1.8.2.2 yamt struct pci_attach_args *pa = aux;
152 1.8.2.2 yamt struct scsipi_adapter *adapt = &sc->sc_adapter;
153 1.8.2.2 yamt struct scsipi_channel *chan = &sc->sc_chan;
154 1.8.2.2 yamt
155 1.8.2.2 yamt sc->sc_talking = 0;
156 1.8.2.2 yamt rw_init(&sc->sc_rwlock);
157 1.8.2.2 yamt mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_BIO);
158 1.8.2.2 yamt cv_init(&sc->sc_condvar, "arcdb");
159 1.8.2.2 yamt
160 1.8.2.2 yamt if (arc_map_pci_resources(sc, pa) != 0) {
161 1.8.2.2 yamt /* error message printed by arc_map_pci_resources */
162 1.8.2.2 yamt return;
163 1.8.2.2 yamt }
164 1.8.2.2 yamt
165 1.8.2.2 yamt if (arc_query_firmware(sc) != 0) {
166 1.8.2.2 yamt /* error message printed by arc_query_firmware */
167 1.8.2.2 yamt goto unmap_pci;
168 1.8.2.2 yamt }
169 1.8.2.2 yamt
170 1.8.2.2 yamt if (arc_alloc_ccbs(sc) != 0) {
171 1.8.2.2 yamt /* error message printed by arc_alloc_ccbs */
172 1.8.2.2 yamt goto unmap_pci;
173 1.8.2.2 yamt }
174 1.8.2.2 yamt
175 1.8.2.2 yamt sc->sc_shutdownhook = shutdownhook_establish(arc_shutdown, sc);
176 1.8.2.2 yamt if (sc->sc_shutdownhook == NULL)
177 1.8.2.2 yamt panic("unable to establish arc powerhook");
178 1.8.2.2 yamt
179 1.8.2.2 yamt memset(adapt, 0, sizeof(*adapt));
180 1.8.2.2 yamt adapt->adapt_dev = self;
181 1.8.2.2 yamt adapt->adapt_nchannels = 1;
182 1.8.2.2 yamt adapt->adapt_openings = sc->sc_req_count / ARC_MAX_TARGET;
183 1.8.2.2 yamt adapt->adapt_max_periph = adapt->adapt_openings;
184 1.8.2.2 yamt adapt->adapt_minphys = arc_minphys;
185 1.8.2.2 yamt adapt->adapt_request = arc_scsi_cmd;
186 1.8.2.2 yamt
187 1.8.2.2 yamt memset(chan, 0, sizeof(*chan));
188 1.8.2.2 yamt chan->chan_adapter = adapt;
189 1.8.2.2 yamt chan->chan_bustype = &scsi_bustype;
190 1.8.2.2 yamt chan->chan_nluns = ARC_MAX_LUN;
191 1.8.2.2 yamt chan->chan_ntargets = ARC_MAX_TARGET;
192 1.8.2.2 yamt chan->chan_id = ARC_MAX_TARGET;
193 1.8.2.2 yamt chan->chan_channel = 0;
194 1.8.2.2 yamt chan->chan_flags = SCSIPI_CHAN_NOSETTLE;
195 1.8.2.2 yamt
196 1.8.2.3 yamt /*
197 1.8.2.3 yamt * Save the device_t returned, because we could to attach
198 1.8.2.3 yamt * devices via the management interface.
199 1.8.2.3 yamt */
200 1.8.2.3 yamt sc->sc_scsibus_dv = config_found(self, &sc->sc_chan, scsiprint);
201 1.8.2.2 yamt
202 1.8.2.2 yamt /* enable interrupts */
203 1.8.2.2 yamt arc_write(sc, ARC_REG_INTRMASK,
204 1.8.2.2 yamt ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRSTAT_DOORBELL));
205 1.8.2.2 yamt
206 1.8.2.2 yamt #if NBIO > 0
207 1.8.2.2 yamt /*
208 1.8.2.2 yamt * Register the driver to bio(4) and setup the sensors.
209 1.8.2.2 yamt */
210 1.8.2.2 yamt if (bio_register(self, arc_bioctl) != 0)
211 1.8.2.2 yamt panic("%s: bioctl registration failed\n", device_xname(self));
212 1.8.2.2 yamt
213 1.8.2.2 yamt /*
214 1.8.2.2 yamt * you need to talk to the firmware to get volume info. our firmware
215 1.8.2.2 yamt * interface relies on being able to sleep, so we need to use a thread
216 1.8.2.2 yamt * to do the work.
217 1.8.2.2 yamt */
218 1.8.2.2 yamt if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
219 1.8.2.2 yamt arc_create_sensors, sc, &sc->sc_lwp, "arcmsr_sensors") != 0)
220 1.8.2.2 yamt panic("%s: unable to create a kernel thread for sensors\n",
221 1.8.2.2 yamt device_xname(self));
222 1.8.2.2 yamt #endif
223 1.8.2.2 yamt
224 1.8.2.2 yamt return;
225 1.8.2.2 yamt
226 1.8.2.2 yamt unmap_pci:
227 1.8.2.2 yamt arc_unmap_pci_resources(sc);
228 1.8.2.2 yamt }
229 1.8.2.2 yamt
230 1.8.2.2 yamt static int
231 1.8.2.2 yamt arc_detach(device_t self, int flags)
232 1.8.2.2 yamt {
233 1.8.2.2 yamt struct arc_softc *sc = device_private(self);
234 1.8.2.2 yamt
235 1.8.2.2 yamt shutdownhook_disestablish(sc->sc_shutdownhook);
236 1.8.2.2 yamt
237 1.8.2.2 yamt if (arc_msg0(sc, ARC_REG_INB_MSG0_STOP_BGRB) != 0)
238 1.8.2.2 yamt aprint_error("%s: timeout waiting to stop bg rebuild\n",
239 1.8.2.2 yamt device_xname(&sc->sc_dev));
240 1.8.2.2 yamt
241 1.8.2.2 yamt if (arc_msg0(sc, ARC_REG_INB_MSG0_FLUSH_CACHE) != 0)
242 1.8.2.2 yamt aprint_error("%s: timeout waiting to flush cache\n",
243 1.8.2.2 yamt device_xname(&sc->sc_dev));
244 1.8.2.2 yamt
245 1.8.2.2 yamt return 0;
246 1.8.2.2 yamt }
247 1.8.2.2 yamt
248 1.8.2.2 yamt static void
249 1.8.2.2 yamt arc_shutdown(void *xsc)
250 1.8.2.2 yamt {
251 1.8.2.2 yamt struct arc_softc *sc = xsc;
252 1.8.2.2 yamt
253 1.8.2.2 yamt if (arc_msg0(sc, ARC_REG_INB_MSG0_STOP_BGRB) != 0)
254 1.8.2.2 yamt aprint_error("%s: timeout waiting to stop bg rebuild\n",
255 1.8.2.2 yamt device_xname(&sc->sc_dev));
256 1.8.2.2 yamt
257 1.8.2.2 yamt if (arc_msg0(sc, ARC_REG_INB_MSG0_FLUSH_CACHE) != 0)
258 1.8.2.2 yamt aprint_error("%s: timeout waiting to flush cache\n",
259 1.8.2.2 yamt device_xname(&sc->sc_dev));
260 1.8.2.2 yamt }
261 1.8.2.2 yamt
262 1.8.2.2 yamt static void
263 1.8.2.2 yamt arc_minphys(struct buf *bp)
264 1.8.2.2 yamt {
265 1.8.2.2 yamt if (bp->b_bcount > MAXPHYS)
266 1.8.2.2 yamt bp->b_bcount = MAXPHYS;
267 1.8.2.2 yamt minphys(bp);
268 1.8.2.2 yamt }
269 1.8.2.2 yamt
270 1.8.2.2 yamt static int
271 1.8.2.2 yamt arc_intr(void *arg)
272 1.8.2.2 yamt {
273 1.8.2.2 yamt struct arc_softc *sc = arg;
274 1.8.2.2 yamt struct arc_ccb *ccb = NULL;
275 1.8.2.2 yamt char *kva = ARC_DMA_KVA(sc->sc_requests);
276 1.8.2.2 yamt struct arc_io_cmd *cmd;
277 1.8.2.2 yamt uint32_t reg, intrstat;
278 1.8.2.2 yamt
279 1.8.2.2 yamt mutex_spin_enter(&sc->sc_mutex);
280 1.8.2.2 yamt intrstat = arc_read(sc, ARC_REG_INTRSTAT);
281 1.8.2.2 yamt if (intrstat == 0x0) {
282 1.8.2.2 yamt mutex_spin_exit(&sc->sc_mutex);
283 1.8.2.2 yamt return 0;
284 1.8.2.2 yamt }
285 1.8.2.2 yamt
286 1.8.2.2 yamt intrstat &= ARC_REG_INTRSTAT_POSTQUEUE | ARC_REG_INTRSTAT_DOORBELL;
287 1.8.2.2 yamt arc_write(sc, ARC_REG_INTRSTAT, intrstat);
288 1.8.2.2 yamt
289 1.8.2.2 yamt if (intrstat & ARC_REG_INTRSTAT_DOORBELL) {
290 1.8.2.2 yamt if (sc->sc_talking) {
291 1.8.2.2 yamt arc_write(sc, ARC_REG_INTRMASK,
292 1.8.2.2 yamt ~ARC_REG_INTRMASK_POSTQUEUE);
293 1.8.2.2 yamt cv_broadcast(&sc->sc_condvar);
294 1.8.2.2 yamt } else {
295 1.8.2.2 yamt /* otherwise drop it */
296 1.8.2.2 yamt reg = arc_read(sc, ARC_REG_OUTB_DOORBELL);
297 1.8.2.2 yamt arc_write(sc, ARC_REG_OUTB_DOORBELL, reg);
298 1.8.2.2 yamt if (reg & ARC_REG_OUTB_DOORBELL_WRITE_OK)
299 1.8.2.2 yamt arc_write(sc, ARC_REG_INB_DOORBELL,
300 1.8.2.2 yamt ARC_REG_INB_DOORBELL_READ_OK);
301 1.8.2.2 yamt }
302 1.8.2.2 yamt }
303 1.8.2.2 yamt mutex_spin_exit(&sc->sc_mutex);
304 1.8.2.2 yamt
305 1.8.2.2 yamt while ((reg = arc_pop(sc)) != 0xffffffff) {
306 1.8.2.2 yamt cmd = (struct arc_io_cmd *)(kva +
307 1.8.2.2 yamt ((reg << ARC_REG_REPLY_QUEUE_ADDR_SHIFT) -
308 1.8.2.2 yamt (uint32_t)ARC_DMA_DVA(sc->sc_requests)));
309 1.8.2.2 yamt ccb = &sc->sc_ccbs[htole32(cmd->cmd.context)];
310 1.8.2.2 yamt
311 1.8.2.2 yamt bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
312 1.8.2.2 yamt ccb->ccb_offset, ARC_MAX_IOCMDLEN,
313 1.8.2.2 yamt BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
314 1.8.2.2 yamt
315 1.8.2.2 yamt arc_scsi_cmd_done(sc, ccb, reg);
316 1.8.2.2 yamt }
317 1.8.2.2 yamt
318 1.8.2.3 yamt
319 1.8.2.2 yamt return 1;
320 1.8.2.2 yamt }
321 1.8.2.2 yamt
322 1.8.2.2 yamt void
323 1.8.2.2 yamt arc_scsi_cmd(struct scsipi_channel *chan, scsipi_adapter_req_t req, void *arg)
324 1.8.2.2 yamt {
325 1.8.2.2 yamt struct scsipi_periph *periph;
326 1.8.2.2 yamt struct scsipi_xfer *xs;
327 1.8.2.2 yamt struct scsipi_adapter *adapt = chan->chan_adapter;
328 1.8.2.2 yamt struct arc_softc *sc = device_private(adapt->adapt_dev);
329 1.8.2.2 yamt struct arc_ccb *ccb;
330 1.8.2.2 yamt struct arc_msg_scsicmd *cmd;
331 1.8.2.2 yamt uint32_t reg;
332 1.8.2.2 yamt uint8_t target;
333 1.8.2.2 yamt
334 1.8.2.2 yamt switch (req) {
335 1.8.2.2 yamt case ADAPTER_REQ_GROW_RESOURCES:
336 1.8.2.2 yamt /* Not supported. */
337 1.8.2.2 yamt return;
338 1.8.2.2 yamt case ADAPTER_REQ_SET_XFER_MODE:
339 1.8.2.2 yamt /* Not supported. */
340 1.8.2.2 yamt return;
341 1.8.2.2 yamt case ADAPTER_REQ_RUN_XFER:
342 1.8.2.2 yamt break;
343 1.8.2.2 yamt }
344 1.8.2.2 yamt
345 1.8.2.2 yamt mutex_spin_enter(&sc->sc_mutex);
346 1.8.2.2 yamt
347 1.8.2.2 yamt xs = arg;
348 1.8.2.2 yamt periph = xs->xs_periph;
349 1.8.2.2 yamt target = periph->periph_target;
350 1.8.2.2 yamt
351 1.8.2.2 yamt if (xs->cmdlen > ARC_MSG_CDBLEN) {
352 1.8.2.2 yamt memset(&xs->sense, 0, sizeof(xs->sense));
353 1.8.2.2 yamt xs->sense.scsi_sense.response_code = SSD_RCODE_VALID | 0x70;
354 1.8.2.2 yamt xs->sense.scsi_sense.flags = SKEY_ILLEGAL_REQUEST;
355 1.8.2.2 yamt xs->sense.scsi_sense.asc = 0x20;
356 1.8.2.2 yamt xs->error = XS_SENSE;
357 1.8.2.2 yamt xs->status = SCSI_CHECK;
358 1.8.2.2 yamt mutex_spin_exit(&sc->sc_mutex);
359 1.8.2.2 yamt scsipi_done(xs);
360 1.8.2.2 yamt return;
361 1.8.2.2 yamt }
362 1.8.2.2 yamt
363 1.8.2.2 yamt ccb = arc_get_ccb(sc);
364 1.8.2.2 yamt if (ccb == NULL) {
365 1.8.2.2 yamt xs->error = XS_RESOURCE_SHORTAGE;
366 1.8.2.2 yamt mutex_spin_exit(&sc->sc_mutex);
367 1.8.2.2 yamt scsipi_done(xs);
368 1.8.2.2 yamt return;
369 1.8.2.2 yamt }
370 1.8.2.2 yamt
371 1.8.2.2 yamt ccb->ccb_xs = xs;
372 1.8.2.2 yamt
373 1.8.2.2 yamt if (arc_load_xs(ccb) != 0) {
374 1.8.2.2 yamt xs->error = XS_DRIVER_STUFFUP;
375 1.8.2.2 yamt arc_put_ccb(sc, ccb);
376 1.8.2.2 yamt mutex_spin_exit(&sc->sc_mutex);
377 1.8.2.2 yamt scsipi_done(xs);
378 1.8.2.2 yamt return;
379 1.8.2.2 yamt }
380 1.8.2.2 yamt
381 1.8.2.2 yamt cmd = &ccb->ccb_cmd->cmd;
382 1.8.2.2 yamt reg = ccb->ccb_cmd_post;
383 1.8.2.2 yamt
384 1.8.2.2 yamt /* bus is always 0 */
385 1.8.2.2 yamt cmd->target = target;
386 1.8.2.2 yamt cmd->lun = periph->periph_lun;
387 1.8.2.2 yamt cmd->function = 1; /* XXX magic number */
388 1.8.2.2 yamt
389 1.8.2.2 yamt cmd->cdb_len = xs->cmdlen;
390 1.8.2.2 yamt cmd->sgl_len = ccb->ccb_dmamap->dm_nsegs;
391 1.8.2.2 yamt if (xs->xs_control & XS_CTL_DATA_OUT)
392 1.8.2.2 yamt cmd->flags = ARC_MSG_SCSICMD_FLAG_WRITE;
393 1.8.2.2 yamt if (ccb->ccb_dmamap->dm_nsegs > ARC_SGL_256LEN) {
394 1.8.2.2 yamt cmd->flags |= ARC_MSG_SCSICMD_FLAG_SGL_BSIZE_512;
395 1.8.2.2 yamt reg |= ARC_REG_POST_QUEUE_BIGFRAME;
396 1.8.2.2 yamt }
397 1.8.2.2 yamt
398 1.8.2.2 yamt cmd->context = htole32(ccb->ccb_id);
399 1.8.2.2 yamt cmd->data_len = htole32(xs->datalen);
400 1.8.2.2 yamt
401 1.8.2.2 yamt memcpy(cmd->cdb, xs->cmd, xs->cmdlen);
402 1.8.2.2 yamt
403 1.8.2.2 yamt /* we've built the command, let's put it on the hw */
404 1.8.2.2 yamt bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
405 1.8.2.2 yamt ccb->ccb_offset, ARC_MAX_IOCMDLEN,
406 1.8.2.2 yamt BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
407 1.8.2.2 yamt
408 1.8.2.2 yamt arc_push(sc, reg);
409 1.8.2.2 yamt if (xs->xs_control & XS_CTL_POLL) {
410 1.8.2.2 yamt if (arc_complete(sc, ccb, xs->timeout) != 0) {
411 1.8.2.2 yamt xs->error = XS_DRIVER_STUFFUP;
412 1.8.2.2 yamt mutex_spin_exit(&sc->sc_mutex);
413 1.8.2.2 yamt scsipi_done(xs);
414 1.8.2.2 yamt return;
415 1.8.2.2 yamt }
416 1.8.2.2 yamt }
417 1.8.2.2 yamt
418 1.8.2.2 yamt mutex_spin_exit(&sc->sc_mutex);
419 1.8.2.2 yamt }
420 1.8.2.2 yamt
421 1.8.2.2 yamt int
422 1.8.2.2 yamt arc_load_xs(struct arc_ccb *ccb)
423 1.8.2.2 yamt {
424 1.8.2.2 yamt struct arc_softc *sc = ccb->ccb_sc;
425 1.8.2.2 yamt struct scsipi_xfer *xs = ccb->ccb_xs;
426 1.8.2.2 yamt bus_dmamap_t dmap = ccb->ccb_dmamap;
427 1.8.2.2 yamt struct arc_sge *sgl = ccb->ccb_cmd->sgl, *sge;
428 1.8.2.2 yamt uint64_t addr;
429 1.8.2.2 yamt int i, error;
430 1.8.2.2 yamt
431 1.8.2.2 yamt if (xs->datalen == 0)
432 1.8.2.2 yamt return 0;
433 1.8.2.2 yamt
434 1.8.2.2 yamt error = bus_dmamap_load(sc->sc_dmat, dmap,
435 1.8.2.2 yamt xs->data, xs->datalen, NULL,
436 1.8.2.2 yamt (xs->xs_control & XS_CTL_NOSLEEP) ?
437 1.8.2.2 yamt BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
438 1.8.2.2 yamt if (error != 0) {
439 1.8.2.2 yamt aprint_error("%s: error %d loading dmamap\n",
440 1.8.2.2 yamt device_xname(&sc->sc_dev), error);
441 1.8.2.2 yamt return 1;
442 1.8.2.2 yamt }
443 1.8.2.2 yamt
444 1.8.2.2 yamt for (i = 0; i < dmap->dm_nsegs; i++) {
445 1.8.2.2 yamt sge = &sgl[i];
446 1.8.2.2 yamt
447 1.8.2.2 yamt sge->sg_hdr = htole32(ARC_SGE_64BIT | dmap->dm_segs[i].ds_len);
448 1.8.2.2 yamt addr = dmap->dm_segs[i].ds_addr;
449 1.8.2.2 yamt sge->sg_hi_addr = htole32((uint32_t)(addr >> 32));
450 1.8.2.2 yamt sge->sg_lo_addr = htole32((uint32_t)addr);
451 1.8.2.2 yamt }
452 1.8.2.2 yamt
453 1.8.2.2 yamt bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
454 1.8.2.2 yamt (xs->xs_control & XS_CTL_DATA_IN) ? BUS_DMASYNC_PREREAD :
455 1.8.2.2 yamt BUS_DMASYNC_PREWRITE);
456 1.8.2.2 yamt
457 1.8.2.2 yamt return 0;
458 1.8.2.2 yamt }
459 1.8.2.2 yamt
460 1.8.2.2 yamt void
461 1.8.2.2 yamt arc_scsi_cmd_done(struct arc_softc *sc, struct arc_ccb *ccb, uint32_t reg)
462 1.8.2.2 yamt {
463 1.8.2.2 yamt struct scsipi_xfer *xs = ccb->ccb_xs;
464 1.8.2.2 yamt struct arc_msg_scsicmd *cmd;
465 1.8.2.2 yamt
466 1.8.2.2 yamt if (xs->datalen != 0) {
467 1.8.2.2 yamt bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap, 0,
468 1.8.2.2 yamt ccb->ccb_dmamap->dm_mapsize,
469 1.8.2.2 yamt (xs->xs_control & XS_CTL_DATA_IN) ?
470 1.8.2.2 yamt BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
471 1.8.2.2 yamt bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap);
472 1.8.2.2 yamt }
473 1.8.2.2 yamt
474 1.8.2.2 yamt /* timeout_del */
475 1.8.2.2 yamt xs->status |= XS_STS_DONE;
476 1.8.2.2 yamt
477 1.8.2.2 yamt if (reg & ARC_REG_REPLY_QUEUE_ERR) {
478 1.8.2.2 yamt cmd = &ccb->ccb_cmd->cmd;
479 1.8.2.2 yamt
480 1.8.2.2 yamt switch (cmd->status) {
481 1.8.2.2 yamt case ARC_MSG_STATUS_SELTIMEOUT:
482 1.8.2.2 yamt case ARC_MSG_STATUS_ABORTED:
483 1.8.2.2 yamt case ARC_MSG_STATUS_INIT_FAIL:
484 1.8.2.2 yamt xs->status = SCSI_OK;
485 1.8.2.2 yamt xs->error = XS_SELTIMEOUT;
486 1.8.2.2 yamt break;
487 1.8.2.2 yamt
488 1.8.2.2 yamt case SCSI_CHECK:
489 1.8.2.2 yamt memset(&xs->sense, 0, sizeof(xs->sense));
490 1.8.2.2 yamt memcpy(&xs->sense, cmd->sense_data,
491 1.8.2.2 yamt min(ARC_MSG_SENSELEN, sizeof(xs->sense)));
492 1.8.2.2 yamt xs->sense.scsi_sense.response_code =
493 1.8.2.2 yamt SSD_RCODE_VALID | 0x70;
494 1.8.2.2 yamt xs->status = SCSI_CHECK;
495 1.8.2.2 yamt xs->error = XS_SENSE;
496 1.8.2.2 yamt xs->resid = 0;
497 1.8.2.2 yamt break;
498 1.8.2.2 yamt
499 1.8.2.2 yamt default:
500 1.8.2.2 yamt /* unknown device status */
501 1.8.2.2 yamt xs->error = XS_BUSY; /* try again later? */
502 1.8.2.2 yamt xs->status = SCSI_BUSY;
503 1.8.2.2 yamt break;
504 1.8.2.2 yamt }
505 1.8.2.2 yamt } else {
506 1.8.2.2 yamt xs->status = SCSI_OK;
507 1.8.2.2 yamt xs->error = XS_NOERROR;
508 1.8.2.2 yamt xs->resid = 0;
509 1.8.2.2 yamt }
510 1.8.2.2 yamt
511 1.8.2.2 yamt arc_put_ccb(sc, ccb);
512 1.8.2.2 yamt scsipi_done(xs);
513 1.8.2.2 yamt }
514 1.8.2.2 yamt
515 1.8.2.2 yamt int
516 1.8.2.2 yamt arc_complete(struct arc_softc *sc, struct arc_ccb *nccb, int timeout)
517 1.8.2.2 yamt {
518 1.8.2.2 yamt struct arc_ccb *ccb = NULL;
519 1.8.2.2 yamt char *kva = ARC_DMA_KVA(sc->sc_requests);
520 1.8.2.2 yamt struct arc_io_cmd *cmd;
521 1.8.2.2 yamt uint32_t reg;
522 1.8.2.2 yamt
523 1.8.2.2 yamt do {
524 1.8.2.2 yamt reg = arc_pop(sc);
525 1.8.2.2 yamt if (reg == 0xffffffff) {
526 1.8.2.2 yamt if (timeout-- == 0)
527 1.8.2.2 yamt return 1;
528 1.8.2.2 yamt
529 1.8.2.2 yamt delay(1000);
530 1.8.2.2 yamt continue;
531 1.8.2.2 yamt }
532 1.8.2.2 yamt
533 1.8.2.2 yamt cmd = (struct arc_io_cmd *)(kva +
534 1.8.2.2 yamt ((reg << ARC_REG_REPLY_QUEUE_ADDR_SHIFT) -
535 1.8.2.2 yamt ARC_DMA_DVA(sc->sc_requests)));
536 1.8.2.2 yamt ccb = &sc->sc_ccbs[htole32(cmd->cmd.context)];
537 1.8.2.2 yamt
538 1.8.2.2 yamt bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
539 1.8.2.2 yamt ccb->ccb_offset, ARC_MAX_IOCMDLEN,
540 1.8.2.2 yamt BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
541 1.8.2.2 yamt
542 1.8.2.2 yamt arc_scsi_cmd_done(sc, ccb, reg);
543 1.8.2.2 yamt } while (nccb != ccb);
544 1.8.2.2 yamt
545 1.8.2.2 yamt return 0;
546 1.8.2.2 yamt }
547 1.8.2.2 yamt
548 1.8.2.2 yamt int
549 1.8.2.2 yamt arc_map_pci_resources(struct arc_softc *sc, struct pci_attach_args *pa)
550 1.8.2.2 yamt {
551 1.8.2.2 yamt pcireg_t memtype;
552 1.8.2.2 yamt pci_intr_handle_t ih;
553 1.8.2.2 yamt
554 1.8.2.2 yamt sc->sc_pc = pa->pa_pc;
555 1.8.2.2 yamt sc->sc_tag = pa->pa_tag;
556 1.8.2.2 yamt sc->sc_dmat = pa->pa_dmat;
557 1.8.2.2 yamt
558 1.8.2.2 yamt memtype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, ARC_PCI_BAR);
559 1.8.2.2 yamt if (pci_mapreg_map(pa, ARC_PCI_BAR, memtype, 0, &sc->sc_iot,
560 1.8.2.2 yamt &sc->sc_ioh, NULL, &sc->sc_ios) != 0) {
561 1.8.2.2 yamt aprint_error(": unable to map system interface register\n");
562 1.8.2.2 yamt return 1;
563 1.8.2.2 yamt }
564 1.8.2.2 yamt
565 1.8.2.2 yamt if (pci_intr_map(pa, &ih) != 0) {
566 1.8.2.2 yamt aprint_error(": unable to map interrupt\n");
567 1.8.2.2 yamt goto unmap;
568 1.8.2.2 yamt }
569 1.8.2.2 yamt
570 1.8.2.2 yamt sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
571 1.8.2.2 yamt arc_intr, sc);
572 1.8.2.2 yamt if (sc->sc_ih == NULL) {
573 1.8.2.2 yamt aprint_error(": unable to map interrupt [2]\n");
574 1.8.2.2 yamt goto unmap;
575 1.8.2.2 yamt }
576 1.8.2.2 yamt aprint_normal(": interrupting at %s\n",
577 1.8.2.2 yamt pci_intr_string(pa->pa_pc, ih));
578 1.8.2.2 yamt
579 1.8.2.2 yamt return 0;
580 1.8.2.2 yamt
581 1.8.2.2 yamt unmap:
582 1.8.2.2 yamt bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
583 1.8.2.2 yamt sc->sc_ios = 0;
584 1.8.2.2 yamt return 1;
585 1.8.2.2 yamt }
586 1.8.2.2 yamt
587 1.8.2.2 yamt void
588 1.8.2.2 yamt arc_unmap_pci_resources(struct arc_softc *sc)
589 1.8.2.2 yamt {
590 1.8.2.2 yamt pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
591 1.8.2.2 yamt bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
592 1.8.2.2 yamt sc->sc_ios = 0;
593 1.8.2.2 yamt }
594 1.8.2.2 yamt
595 1.8.2.2 yamt int
596 1.8.2.2 yamt arc_query_firmware(struct arc_softc *sc)
597 1.8.2.2 yamt {
598 1.8.2.2 yamt struct arc_msg_firmware_info fwinfo;
599 1.8.2.2 yamt char string[81]; /* sizeof(vendor)*2+1 */
600 1.8.2.2 yamt
601 1.8.2.2 yamt if (arc_wait_eq(sc, ARC_REG_OUTB_ADDR1, ARC_REG_OUTB_ADDR1_FIRMWARE_OK,
602 1.8.2.2 yamt ARC_REG_OUTB_ADDR1_FIRMWARE_OK) != 0) {
603 1.8.2.2 yamt aprint_debug("%s: timeout waiting for firmware ok\n",
604 1.8.2.2 yamt device_xname(&sc->sc_dev));
605 1.8.2.2 yamt return 1;
606 1.8.2.2 yamt }
607 1.8.2.2 yamt
608 1.8.2.2 yamt if (arc_msg0(sc, ARC_REG_INB_MSG0_GET_CONFIG) != 0) {
609 1.8.2.2 yamt aprint_debug("%s: timeout waiting for get config\n",
610 1.8.2.2 yamt device_xname(&sc->sc_dev));
611 1.8.2.2 yamt return 1;
612 1.8.2.2 yamt }
613 1.8.2.2 yamt
614 1.8.2.2 yamt if (arc_msg0(sc, ARC_REG_INB_MSG0_START_BGRB) != 0) {
615 1.8.2.2 yamt aprint_debug("%s: timeout waiting to start bg rebuild\n",
616 1.8.2.2 yamt device_xname(&sc->sc_dev));
617 1.8.2.2 yamt return 1;
618 1.8.2.2 yamt }
619 1.8.2.2 yamt
620 1.8.2.2 yamt arc_read_region(sc, ARC_REG_MSGBUF, &fwinfo, sizeof(fwinfo));
621 1.8.2.2 yamt
622 1.8.2.2 yamt DNPRINTF(ARC_D_INIT, "%s: signature: 0x%08x\n",
623 1.8.2.2 yamt device_xname(&sc->sc_dev), htole32(fwinfo.signature));
624 1.8.2.2 yamt
625 1.8.2.2 yamt if (htole32(fwinfo.signature) != ARC_FWINFO_SIGNATURE_GET_CONFIG) {
626 1.8.2.2 yamt aprint_error("%s: invalid firmware info from iop\n",
627 1.8.2.2 yamt device_xname(&sc->sc_dev));
628 1.8.2.2 yamt return 1;
629 1.8.2.2 yamt }
630 1.8.2.2 yamt
631 1.8.2.2 yamt DNPRINTF(ARC_D_INIT, "%s: request_len: %d\n",
632 1.8.2.2 yamt device_xname(&sc->sc_dev),
633 1.8.2.2 yamt htole32(fwinfo.request_len));
634 1.8.2.2 yamt DNPRINTF(ARC_D_INIT, "%s: queue_len: %d\n",
635 1.8.2.2 yamt device_xname(&sc->sc_dev),
636 1.8.2.2 yamt htole32(fwinfo.queue_len));
637 1.8.2.2 yamt DNPRINTF(ARC_D_INIT, "%s: sdram_size: %d\n",
638 1.8.2.2 yamt device_xname(&sc->sc_dev),
639 1.8.2.2 yamt htole32(fwinfo.sdram_size));
640 1.8.2.2 yamt DNPRINTF(ARC_D_INIT, "%s: sata_ports: %d\n",
641 1.8.2.2 yamt device_xname(&sc->sc_dev),
642 1.8.2.2 yamt htole32(fwinfo.sata_ports));
643 1.8.2.2 yamt
644 1.8.2.2 yamt scsipi_strvis(string, 81, fwinfo.vendor, sizeof(fwinfo.vendor));
645 1.8.2.2 yamt DNPRINTF(ARC_D_INIT, "%s: vendor: \"%s\"\n",
646 1.8.2.2 yamt device_xname(&sc->sc_dev), string);
647 1.8.2.2 yamt
648 1.8.2.2 yamt scsipi_strvis(string, 17, fwinfo.model, sizeof(fwinfo.model));
649 1.8.2.2 yamt aprint_normal("%s: Areca %s Host Adapter RAID controller\n",
650 1.8.2.2 yamt device_xname(&sc->sc_dev), string);
651 1.8.2.2 yamt
652 1.8.2.2 yamt scsipi_strvis(string, 33, fwinfo.fw_version, sizeof(fwinfo.fw_version));
653 1.8.2.2 yamt DNPRINTF(ARC_D_INIT, "%s: version: \"%s\"\n",
654 1.8.2.2 yamt device_xname(&sc->sc_dev), string);
655 1.8.2.2 yamt
656 1.8.2.3 yamt aprint_normal("%s: %d ports, %dMB SDRAM, firmware <%s>\n",
657 1.8.2.3 yamt device_xname(&sc->sc_dev), htole32(fwinfo.sata_ports),
658 1.8.2.3 yamt htole32(fwinfo.sdram_size), string);
659 1.8.2.3 yamt
660 1.8.2.3 yamt /* save the number of max disks for future use */
661 1.8.2.3 yamt sc->sc_maxdisks = htole32(fwinfo.sata_ports);
662 1.8.2.3 yamt
663 1.8.2.2 yamt if (htole32(fwinfo.request_len) != ARC_MAX_IOCMDLEN) {
664 1.8.2.2 yamt aprint_error("%s: unexpected request frame size (%d != %d)\n",
665 1.8.2.2 yamt device_xname(&sc->sc_dev),
666 1.8.2.2 yamt htole32(fwinfo.request_len), ARC_MAX_IOCMDLEN);
667 1.8.2.2 yamt return 1;
668 1.8.2.2 yamt }
669 1.8.2.2 yamt
670 1.8.2.2 yamt sc->sc_req_count = htole32(fwinfo.queue_len);
671 1.8.2.2 yamt
672 1.8.2.2 yamt return 0;
673 1.8.2.2 yamt }
674 1.8.2.2 yamt
675 1.8.2.2 yamt #if NBIO > 0
676 1.8.2.2 yamt static int
677 1.8.2.2 yamt arc_bioctl(struct device *self, u_long cmd, void *addr)
678 1.8.2.2 yamt {
679 1.8.2.2 yamt struct arc_softc *sc = device_private(self);
680 1.8.2.2 yamt int error = 0;
681 1.8.2.2 yamt
682 1.8.2.2 yamt switch (cmd) {
683 1.8.2.2 yamt case BIOCINQ:
684 1.8.2.2 yamt error = arc_bio_inq(sc, (struct bioc_inq *)addr);
685 1.8.2.2 yamt break;
686 1.8.2.2 yamt
687 1.8.2.2 yamt case BIOCVOL:
688 1.8.2.2 yamt error = arc_bio_vol(sc, (struct bioc_vol *)addr);
689 1.8.2.2 yamt break;
690 1.8.2.2 yamt
691 1.8.2.2 yamt case BIOCDISK:
692 1.8.2.3 yamt error = arc_bio_disk_volume(sc, (struct bioc_disk *)addr);
693 1.8.2.3 yamt break;
694 1.8.2.3 yamt
695 1.8.2.3 yamt case BIOCDISK_NOVOL:
696 1.8.2.3 yamt error = arc_bio_disk_novol(sc, (struct bioc_disk *)addr);
697 1.8.2.2 yamt break;
698 1.8.2.2 yamt
699 1.8.2.2 yamt case BIOCALARM:
700 1.8.2.2 yamt error = arc_bio_alarm(sc, (struct bioc_alarm *)addr);
701 1.8.2.2 yamt break;
702 1.8.2.2 yamt
703 1.8.2.3 yamt case BIOCSETSTATE:
704 1.8.2.3 yamt error = arc_bio_setstate(sc, (struct bioc_setstate *)addr);
705 1.8.2.3 yamt break;
706 1.8.2.3 yamt
707 1.8.2.3 yamt case BIOCVOLOPS:
708 1.8.2.3 yamt error = arc_bio_volops(sc, (struct bioc_volops *)addr);
709 1.8.2.3 yamt break;
710 1.8.2.3 yamt
711 1.8.2.2 yamt default:
712 1.8.2.2 yamt error = ENOTTY;
713 1.8.2.2 yamt break;
714 1.8.2.2 yamt }
715 1.8.2.2 yamt
716 1.8.2.2 yamt return error;
717 1.8.2.2 yamt }
718 1.8.2.2 yamt
719 1.8.2.2 yamt static int
720 1.8.2.3 yamt arc_fw_parse_status_code(struct arc_softc *sc, uint8_t *reply)
721 1.8.2.3 yamt {
722 1.8.2.3 yamt switch (*reply) {
723 1.8.2.3 yamt case ARC_FW_CMD_RAIDINVAL:
724 1.8.2.3 yamt printf("%s: firmware error (invalid raid set)\n",
725 1.8.2.3 yamt device_xname(&sc->sc_dev));
726 1.8.2.3 yamt return EINVAL;
727 1.8.2.3 yamt case ARC_FW_CMD_VOLINVAL:
728 1.8.2.3 yamt printf("%s: firmware error (invalid volume set)\n",
729 1.8.2.3 yamt device_xname(&sc->sc_dev));
730 1.8.2.3 yamt return EINVAL;
731 1.8.2.3 yamt case ARC_FW_CMD_NORAID:
732 1.8.2.3 yamt printf("%s: firmware error (unexistent raid set)\n",
733 1.8.2.3 yamt device_xname(&sc->sc_dev));
734 1.8.2.3 yamt return ENODEV;
735 1.8.2.3 yamt case ARC_FW_CMD_NOVOLUME:
736 1.8.2.3 yamt printf("%s: firmware error (unexistent volume set)\n",
737 1.8.2.3 yamt device_xname(&sc->sc_dev));
738 1.8.2.3 yamt return ENODEV;
739 1.8.2.3 yamt case ARC_FW_CMD_NOPHYSDRV:
740 1.8.2.3 yamt printf("%s: firmware error (unexistent physical drive)\n",
741 1.8.2.3 yamt device_xname(&sc->sc_dev));
742 1.8.2.3 yamt return ENODEV;
743 1.8.2.3 yamt case ARC_FW_CMD_PARAM_ERR:
744 1.8.2.3 yamt printf("%s: firmware error (parameter error)\n",
745 1.8.2.3 yamt device_xname(&sc->sc_dev));
746 1.8.2.3 yamt return EINVAL;
747 1.8.2.3 yamt case ARC_FW_CMD_UNSUPPORTED:
748 1.8.2.3 yamt printf("%s: firmware error (unsupported command)\n",
749 1.8.2.3 yamt device_xname(&sc->sc_dev));
750 1.8.2.3 yamt return EOPNOTSUPP;
751 1.8.2.3 yamt case ARC_FW_CMD_DISKCFG_CHGD:
752 1.8.2.3 yamt printf("%s: firmware error (disk configuration changed)\n",
753 1.8.2.3 yamt device_xname(&sc->sc_dev));
754 1.8.2.3 yamt return EINVAL;
755 1.8.2.3 yamt case ARC_FW_CMD_PASS_INVAL:
756 1.8.2.3 yamt printf("%s: firmware error (invalid password)\n",
757 1.8.2.3 yamt device_xname(&sc->sc_dev));
758 1.8.2.3 yamt return EINVAL;
759 1.8.2.3 yamt case ARC_FW_CMD_NODISKSPACE:
760 1.8.2.3 yamt printf("%s: firmware error (no disk space available)\n",
761 1.8.2.3 yamt device_xname(&sc->sc_dev));
762 1.8.2.3 yamt return EOPNOTSUPP;
763 1.8.2.3 yamt case ARC_FW_CMD_CHECKSUM_ERR:
764 1.8.2.3 yamt printf("%s: firmware error (checksum error)\n",
765 1.8.2.3 yamt device_xname(&sc->sc_dev));
766 1.8.2.3 yamt return EINVAL;
767 1.8.2.3 yamt case ARC_FW_CMD_PASS_REQD:
768 1.8.2.3 yamt printf("%s: firmware error (password required)\n",
769 1.8.2.3 yamt device_xname(&sc->sc_dev));
770 1.8.2.3 yamt return EPERM;
771 1.8.2.3 yamt case ARC_FW_CMD_OK:
772 1.8.2.3 yamt default:
773 1.8.2.3 yamt return 0;
774 1.8.2.3 yamt }
775 1.8.2.3 yamt }
776 1.8.2.3 yamt
777 1.8.2.3 yamt static int
778 1.8.2.2 yamt arc_bio_alarm(struct arc_softc *sc, struct bioc_alarm *ba)
779 1.8.2.2 yamt {
780 1.8.2.2 yamt uint8_t request[2], reply[1];
781 1.8.2.2 yamt size_t len;
782 1.8.2.2 yamt int error = 0;
783 1.8.2.2 yamt
784 1.8.2.2 yamt switch (ba->ba_opcode) {
785 1.8.2.2 yamt case BIOC_SAENABLE:
786 1.8.2.2 yamt case BIOC_SADISABLE:
787 1.8.2.2 yamt request[0] = ARC_FW_SET_ALARM;
788 1.8.2.2 yamt request[1] = (ba->ba_opcode == BIOC_SAENABLE) ?
789 1.8.2.2 yamt ARC_FW_SET_ALARM_ENABLE : ARC_FW_SET_ALARM_DISABLE;
790 1.8.2.2 yamt len = sizeof(request);
791 1.8.2.2 yamt
792 1.8.2.2 yamt break;
793 1.8.2.2 yamt
794 1.8.2.2 yamt case BIOC_SASILENCE:
795 1.8.2.2 yamt request[0] = ARC_FW_MUTE_ALARM;
796 1.8.2.2 yamt len = 1;
797 1.8.2.2 yamt
798 1.8.2.2 yamt break;
799 1.8.2.2 yamt
800 1.8.2.2 yamt case BIOC_GASTATUS:
801 1.8.2.2 yamt /* system info is too big/ugly to deal with here */
802 1.8.2.2 yamt return arc_bio_alarm_state(sc, ba);
803 1.8.2.2 yamt
804 1.8.2.2 yamt default:
805 1.8.2.2 yamt return EOPNOTSUPP;
806 1.8.2.2 yamt }
807 1.8.2.2 yamt
808 1.8.2.2 yamt error = arc_msgbuf(sc, request, len, reply, sizeof(reply));
809 1.8.2.2 yamt if (error != 0)
810 1.8.2.2 yamt return error;
811 1.8.2.2 yamt
812 1.8.2.3 yamt return arc_fw_parse_status_code(sc, &reply[0]);
813 1.8.2.2 yamt }
814 1.8.2.2 yamt
815 1.8.2.2 yamt static int
816 1.8.2.2 yamt arc_bio_alarm_state(struct arc_softc *sc, struct bioc_alarm *ba)
817 1.8.2.2 yamt {
818 1.8.2.2 yamt struct arc_fw_sysinfo *sysinfo;
819 1.8.2.3 yamt uint8_t request;
820 1.8.2.2 yamt int error = 0;
821 1.8.2.2 yamt
822 1.8.2.2 yamt sysinfo = kmem_zalloc(sizeof(struct arc_fw_sysinfo), KM_SLEEP);
823 1.8.2.2 yamt
824 1.8.2.2 yamt request = ARC_FW_SYSINFO;
825 1.8.2.2 yamt error = arc_msgbuf(sc, &request, sizeof(request),
826 1.8.2.2 yamt sysinfo, sizeof(struct arc_fw_sysinfo));
827 1.8.2.2 yamt
828 1.8.2.2 yamt if (error != 0)
829 1.8.2.2 yamt goto out;
830 1.8.2.2 yamt
831 1.8.2.2 yamt ba->ba_status = sysinfo->alarm;
832 1.8.2.2 yamt
833 1.8.2.2 yamt out:
834 1.8.2.2 yamt kmem_free(sysinfo, sizeof(*sysinfo));
835 1.8.2.2 yamt return error;
836 1.8.2.2 yamt }
837 1.8.2.2 yamt
838 1.8.2.3 yamt static int
839 1.8.2.3 yamt arc_bio_volops(struct arc_softc *sc, struct bioc_volops *bc)
840 1.8.2.3 yamt {
841 1.8.2.3 yamt /* to create a raid set */
842 1.8.2.3 yamt struct req_craidset {
843 1.8.2.3 yamt uint8_t cmdcode;
844 1.8.2.3 yamt uint32_t devmask;
845 1.8.2.3 yamt uint8_t raidset_name[16];
846 1.8.2.3 yamt } __packed;
847 1.8.2.3 yamt
848 1.8.2.3 yamt /* to create a volume set */
849 1.8.2.3 yamt struct req_cvolset {
850 1.8.2.3 yamt uint8_t cmdcode;
851 1.8.2.3 yamt uint8_t raidset;
852 1.8.2.3 yamt uint8_t volset_name[16];
853 1.8.2.3 yamt uint64_t capacity;
854 1.8.2.3 yamt uint8_t raidlevel;
855 1.8.2.3 yamt uint8_t stripe;
856 1.8.2.3 yamt uint8_t scsi_chan;
857 1.8.2.3 yamt uint8_t scsi_target;
858 1.8.2.3 yamt uint8_t scsi_lun;
859 1.8.2.3 yamt uint8_t tagqueue;
860 1.8.2.3 yamt uint8_t cache;
861 1.8.2.3 yamt uint8_t speed;
862 1.8.2.3 yamt uint8_t quick_init;
863 1.8.2.3 yamt } __packed;
864 1.8.2.3 yamt
865 1.8.2.3 yamt struct scsibus_softc *scsibus_sc = NULL;
866 1.8.2.3 yamt struct req_craidset req_craidset;
867 1.8.2.3 yamt struct req_cvolset req_cvolset;
868 1.8.2.3 yamt uint8_t request[2];
869 1.8.2.3 yamt uint8_t reply[1];
870 1.8.2.3 yamt int error = 0;
871 1.8.2.3 yamt
872 1.8.2.3 yamt switch (bc->bc_opcode) {
873 1.8.2.3 yamt case BIOC_VCREATE_VOLUME:
874 1.8.2.3 yamt {
875 1.8.2.3 yamt /*
876 1.8.2.3 yamt * Zero out the structs so that we use some defaults
877 1.8.2.3 yamt * in raid and volume sets.
878 1.8.2.3 yamt */
879 1.8.2.3 yamt memset(&req_craidset, 0, sizeof(req_craidset));
880 1.8.2.3 yamt memset(&req_cvolset, 0, sizeof(req_cvolset));
881 1.8.2.3 yamt
882 1.8.2.3 yamt /*
883 1.8.2.3 yamt * Firstly we have to create the raid set and
884 1.8.2.3 yamt * use the default name for all them.
885 1.8.2.3 yamt */
886 1.8.2.3 yamt req_craidset.cmdcode = ARC_FW_CREATE_RAIDSET;
887 1.8.2.3 yamt req_craidset.devmask = bc->bc_devmask;
888 1.8.2.3 yamt error = arc_msgbuf(sc, &req_craidset, sizeof(req_craidset),
889 1.8.2.3 yamt reply, sizeof(reply));
890 1.8.2.3 yamt if (error != 0)
891 1.8.2.3 yamt return error;
892 1.8.2.3 yamt
893 1.8.2.3 yamt error = arc_fw_parse_status_code(sc, &reply[0]);
894 1.8.2.3 yamt if (error) {
895 1.8.2.3 yamt printf("%s: create raidset%d failed\n",
896 1.8.2.3 yamt device_xname(&sc->sc_dev), bc->bc_volid);
897 1.8.2.3 yamt return error;
898 1.8.2.3 yamt }
899 1.8.2.3 yamt
900 1.8.2.3 yamt /*
901 1.8.2.3 yamt * At this point the raid set was created, so it's
902 1.8.2.3 yamt * time to create the volume set.
903 1.8.2.3 yamt */
904 1.8.2.3 yamt req_cvolset.cmdcode = ARC_FW_CREATE_VOLUME;
905 1.8.2.3 yamt req_cvolset.raidset = bc->bc_volid;
906 1.8.2.3 yamt req_cvolset.capacity = bc->bc_size * ARC_BLOCKSIZE;
907 1.8.2.3 yamt
908 1.8.2.3 yamt /*
909 1.8.2.3 yamt * Set the RAID level.
910 1.8.2.3 yamt */
911 1.8.2.3 yamt switch (bc->bc_level) {
912 1.8.2.3 yamt case 0:
913 1.8.2.3 yamt case 1:
914 1.8.2.3 yamt req_cvolset.raidlevel = bc->bc_level;
915 1.8.2.3 yamt break;
916 1.8.2.3 yamt case 3:
917 1.8.2.3 yamt req_cvolset.raidlevel = ARC_FW_VOL_RAIDLEVEL_3;
918 1.8.2.3 yamt break;
919 1.8.2.3 yamt case 5:
920 1.8.2.3 yamt req_cvolset.raidlevel = ARC_FW_VOL_RAIDLEVEL_5;
921 1.8.2.3 yamt break;
922 1.8.2.3 yamt case 6:
923 1.8.2.3 yamt req_cvolset.raidlevel = ARC_FW_VOL_RAIDLEVEL_6;
924 1.8.2.3 yamt break;
925 1.8.2.3 yamt default:
926 1.8.2.3 yamt return EOPNOTSUPP;
927 1.8.2.3 yamt }
928 1.8.2.3 yamt
929 1.8.2.3 yamt /*
930 1.8.2.3 yamt * Set the stripe size.
931 1.8.2.3 yamt */
932 1.8.2.3 yamt switch (bc->bc_stripe) {
933 1.8.2.3 yamt case 4:
934 1.8.2.3 yamt req_cvolset.stripe = 0;
935 1.8.2.3 yamt break;
936 1.8.2.3 yamt case 8:
937 1.8.2.3 yamt req_cvolset.stripe = 1;
938 1.8.2.3 yamt break;
939 1.8.2.3 yamt case 16:
940 1.8.2.3 yamt req_cvolset.stripe = 2;
941 1.8.2.3 yamt break;
942 1.8.2.3 yamt case 32:
943 1.8.2.3 yamt req_cvolset.stripe = 3;
944 1.8.2.3 yamt break;
945 1.8.2.3 yamt case 64:
946 1.8.2.3 yamt req_cvolset.stripe = 4;
947 1.8.2.3 yamt break;
948 1.8.2.3 yamt case 128:
949 1.8.2.3 yamt req_cvolset.stripe = 5;
950 1.8.2.3 yamt break;
951 1.8.2.3 yamt default:
952 1.8.2.3 yamt req_cvolset.stripe = 4; /* by default 64K */
953 1.8.2.3 yamt break;
954 1.8.2.3 yamt }
955 1.8.2.3 yamt
956 1.8.2.3 yamt req_cvolset.scsi_chan = bc->bc_channel;
957 1.8.2.3 yamt req_cvolset.scsi_target = bc->bc_target;
958 1.8.2.3 yamt req_cvolset.scsi_lun = bc->bc_lun;
959 1.8.2.3 yamt req_cvolset.tagqueue = 1; /* always enabled */
960 1.8.2.3 yamt req_cvolset.cache = 1; /* always enabled */
961 1.8.2.3 yamt req_cvolset.speed = 4; /* always max speed */
962 1.8.2.3 yamt
963 1.8.2.3 yamt error = arc_msgbuf(sc, &req_cvolset, sizeof(req_cvolset),
964 1.8.2.3 yamt reply, sizeof(reply));
965 1.8.2.3 yamt if (error != 0)
966 1.8.2.3 yamt return error;
967 1.8.2.3 yamt
968 1.8.2.3 yamt error = arc_fw_parse_status_code(sc, &reply[0]);
969 1.8.2.3 yamt if (error) {
970 1.8.2.3 yamt printf("%s: create volumeset%d failed\n",
971 1.8.2.3 yamt device_xname(&sc->sc_dev), bc->bc_volid);
972 1.8.2.3 yamt return error;
973 1.8.2.3 yamt }
974 1.8.2.3 yamt
975 1.8.2.3 yamt /*
976 1.8.2.3 yamt * Do a rescan on the bus to attach the device associated
977 1.8.2.3 yamt * with the new volume.
978 1.8.2.3 yamt */
979 1.8.2.3 yamt scsibus_sc = device_private(sc->sc_scsibus_dv);
980 1.8.2.3 yamt (void)scsi_probe_bus(scsibus_sc, bc->bc_target, bc->bc_lun);
981 1.8.2.3 yamt
982 1.8.2.3 yamt break;
983 1.8.2.3 yamt }
984 1.8.2.3 yamt case BIOC_VREMOVE_VOLUME:
985 1.8.2.3 yamt {
986 1.8.2.3 yamt /*
987 1.8.2.3 yamt * Remove the volume set specified in bc_volid.
988 1.8.2.3 yamt */
989 1.8.2.3 yamt request[0] = ARC_FW_DELETE_VOLUME;
990 1.8.2.3 yamt request[1] = bc->bc_volid;
991 1.8.2.3 yamt error = arc_msgbuf(sc, request, sizeof(request),
992 1.8.2.3 yamt reply, sizeof(reply));
993 1.8.2.3 yamt if (error != 0)
994 1.8.2.3 yamt return error;
995 1.8.2.3 yamt
996 1.8.2.3 yamt error = arc_fw_parse_status_code(sc, &reply[0]);
997 1.8.2.3 yamt if (error) {
998 1.8.2.3 yamt printf("%s: delete volumeset%d failed\n",
999 1.8.2.3 yamt device_xname(&sc->sc_dev), bc->bc_volid);
1000 1.8.2.3 yamt return error;
1001 1.8.2.3 yamt }
1002 1.8.2.3 yamt
1003 1.8.2.3 yamt /*
1004 1.8.2.3 yamt * Detach the sd(4) device associated with the volume,
1005 1.8.2.3 yamt * but if there's an error don't make it a priority.
1006 1.8.2.3 yamt */
1007 1.8.2.3 yamt error = scsipi_target_detach(&sc->sc_chan, bc->bc_target,
1008 1.8.2.3 yamt bc->bc_lun, 0);
1009 1.8.2.3 yamt if (error)
1010 1.8.2.3 yamt printf("%s: couldn't detach sd device for volume %d "
1011 1.8.2.3 yamt "at %u:%u.%u (error=%d)\n",
1012 1.8.2.3 yamt device_xname(&sc->sc_dev), bc->bc_volid,
1013 1.8.2.3 yamt bc->bc_channel, bc->bc_target, bc->bc_lun, error);
1014 1.8.2.3 yamt
1015 1.8.2.3 yamt /*
1016 1.8.2.3 yamt * and remove the raid set specified in bc_volid,
1017 1.8.2.3 yamt * we only care about volumes.
1018 1.8.2.3 yamt */
1019 1.8.2.3 yamt request[0] = ARC_FW_DELETE_RAIDSET;
1020 1.8.2.3 yamt request[1] = bc->bc_volid;
1021 1.8.2.3 yamt error = arc_msgbuf(sc, request, sizeof(request),
1022 1.8.2.3 yamt reply, sizeof(reply));
1023 1.8.2.3 yamt if (error != 0)
1024 1.8.2.3 yamt return error;
1025 1.8.2.3 yamt
1026 1.8.2.3 yamt error = arc_fw_parse_status_code(sc, &reply[0]);
1027 1.8.2.3 yamt if (error) {
1028 1.8.2.3 yamt printf("%s: delete raidset%d failed\n",
1029 1.8.2.3 yamt device_xname(&sc->sc_dev), bc->bc_volid);
1030 1.8.2.3 yamt return error;
1031 1.8.2.3 yamt }
1032 1.8.2.3 yamt
1033 1.8.2.3 yamt break;
1034 1.8.2.3 yamt }
1035 1.8.2.3 yamt default:
1036 1.8.2.3 yamt return EOPNOTSUPP;
1037 1.8.2.3 yamt }
1038 1.8.2.3 yamt
1039 1.8.2.3 yamt return error;
1040 1.8.2.3 yamt }
1041 1.8.2.3 yamt
1042 1.8.2.3 yamt static int
1043 1.8.2.3 yamt arc_bio_setstate(struct arc_softc *sc, struct bioc_setstate *bs)
1044 1.8.2.3 yamt {
1045 1.8.2.3 yamt /* for a hotspare disk */
1046 1.8.2.3 yamt struct request_hs {
1047 1.8.2.3 yamt uint8_t cmdcode;
1048 1.8.2.3 yamt uint32_t devmask;
1049 1.8.2.3 yamt } __packed;
1050 1.8.2.3 yamt
1051 1.8.2.3 yamt /* for a pass-through disk */
1052 1.8.2.3 yamt struct request_pt {
1053 1.8.2.3 yamt uint8_t cmdcode;
1054 1.8.2.3 yamt uint8_t devid;
1055 1.8.2.3 yamt uint8_t scsi_chan;
1056 1.8.2.3 yamt uint8_t scsi_id;
1057 1.8.2.3 yamt uint8_t scsi_lun;
1058 1.8.2.3 yamt uint8_t tagged_queue;
1059 1.8.2.3 yamt uint8_t cache_mode;
1060 1.8.2.3 yamt uint8_t max_speed;
1061 1.8.2.3 yamt } __packed;
1062 1.8.2.3 yamt
1063 1.8.2.3 yamt struct scsibus_softc *scsibus_sc = NULL;
1064 1.8.2.3 yamt struct request_hs req_hs; /* to add/remove hotspare */
1065 1.8.2.3 yamt struct request_pt req_pt; /* to add a pass-through */
1066 1.8.2.3 yamt uint8_t req_gen[2];
1067 1.8.2.3 yamt uint8_t reply[1];
1068 1.8.2.3 yamt int error = 0;
1069 1.8.2.3 yamt
1070 1.8.2.3 yamt switch (bs->bs_status) {
1071 1.8.2.3 yamt case BIOC_SSHOTSPARE:
1072 1.8.2.3 yamt {
1073 1.8.2.3 yamt req_hs.cmdcode = ARC_FW_CREATE_HOTSPARE;
1074 1.8.2.3 yamt req_hs.devmask = (1 << bs->bs_target);
1075 1.8.2.3 yamt goto hotspare;
1076 1.8.2.3 yamt }
1077 1.8.2.3 yamt case BIOC_SSDELHOTSPARE:
1078 1.8.2.3 yamt {
1079 1.8.2.3 yamt req_hs.cmdcode = ARC_FW_DELETE_HOTSPARE;
1080 1.8.2.3 yamt req_hs.devmask = (1 << bs->bs_target);
1081 1.8.2.3 yamt goto hotspare;
1082 1.8.2.3 yamt }
1083 1.8.2.3 yamt case BIOC_SSPASSTHRU:
1084 1.8.2.3 yamt {
1085 1.8.2.3 yamt req_pt.cmdcode = ARC_FW_CREATE_PASSTHRU;
1086 1.8.2.3 yamt req_pt.devid = bs->bs_other_id; /* this wants device# */
1087 1.8.2.3 yamt req_pt.scsi_chan = bs->bs_channel;
1088 1.8.2.3 yamt req_pt.scsi_id = bs->bs_target;
1089 1.8.2.3 yamt req_pt.scsi_lun = bs->bs_lun;
1090 1.8.2.3 yamt req_pt.tagged_queue = 1; /* always enabled */
1091 1.8.2.3 yamt req_pt.cache_mode = 1; /* always enabled */
1092 1.8.2.3 yamt req_pt.max_speed = 4; /* always max speed */
1093 1.8.2.3 yamt
1094 1.8.2.3 yamt error = arc_msgbuf(sc, &req_pt, sizeof(req_pt),
1095 1.8.2.3 yamt reply, sizeof(reply));
1096 1.8.2.3 yamt if (error != 0)
1097 1.8.2.3 yamt return error;
1098 1.8.2.3 yamt
1099 1.8.2.3 yamt /*
1100 1.8.2.3 yamt * Do a rescan on the bus to attach the new device
1101 1.8.2.3 yamt * associated with the pass-through disk.
1102 1.8.2.3 yamt */
1103 1.8.2.3 yamt scsibus_sc = device_private(sc->sc_scsibus_dv);
1104 1.8.2.3 yamt (void)scsi_probe_bus(scsibus_sc, bs->bs_target, bs->bs_lun);
1105 1.8.2.3 yamt
1106 1.8.2.3 yamt goto out;
1107 1.8.2.3 yamt }
1108 1.8.2.3 yamt case BIOC_SSDELPASSTHRU:
1109 1.8.2.3 yamt {
1110 1.8.2.3 yamt req_gen[0] = ARC_FW_DELETE_PASSTHRU;
1111 1.8.2.3 yamt req_gen[1] = bs->bs_target;
1112 1.8.2.3 yamt error = arc_msgbuf(sc, &req_gen, sizeof(req_gen),
1113 1.8.2.3 yamt reply, sizeof(reply));
1114 1.8.2.3 yamt if (error != 0)
1115 1.8.2.3 yamt return error;
1116 1.8.2.3 yamt
1117 1.8.2.3 yamt /*
1118 1.8.2.3 yamt * Detach the sd device associated with this pass-through disk.
1119 1.8.2.3 yamt */
1120 1.8.2.3 yamt error = scsipi_target_detach(&sc->sc_chan, bs->bs_target,
1121 1.8.2.3 yamt bs->bs_lun, 0);
1122 1.8.2.3 yamt if (error)
1123 1.8.2.3 yamt printf("%s: couldn't detach sd device for the "
1124 1.8.2.3 yamt "pass-through disk at %u:%u.%u (error=%d)\n",
1125 1.8.2.3 yamt device_xname(&sc->sc_dev),
1126 1.8.2.3 yamt bs->bs_channel, bs->bs_target, bs->bs_lun, error);
1127 1.8.2.3 yamt
1128 1.8.2.3 yamt goto out;
1129 1.8.2.3 yamt }
1130 1.8.2.3 yamt case BIOC_SSCHECKSTART_VOL:
1131 1.8.2.3 yamt {
1132 1.8.2.3 yamt req_gen[0] = ARC_FW_START_CHECKVOL;
1133 1.8.2.3 yamt req_gen[1] = bs->bs_volid;
1134 1.8.2.3 yamt error = arc_msgbuf(sc, &req_gen, sizeof(req_gen),
1135 1.8.2.3 yamt reply, sizeof(reply));
1136 1.8.2.3 yamt if (error != 0)
1137 1.8.2.3 yamt return error;
1138 1.8.2.3 yamt
1139 1.8.2.3 yamt goto out;
1140 1.8.2.3 yamt }
1141 1.8.2.3 yamt case BIOC_SSCHECKSTOP_VOL:
1142 1.8.2.3 yamt {
1143 1.8.2.3 yamt uint8_t req = ARC_FW_STOP_CHECKVOL;
1144 1.8.2.3 yamt error = arc_msgbuf(sc, &req, 1, reply, sizeof(reply));
1145 1.8.2.3 yamt if (error != 0)
1146 1.8.2.3 yamt return error;
1147 1.8.2.3 yamt
1148 1.8.2.3 yamt goto out;
1149 1.8.2.3 yamt }
1150 1.8.2.3 yamt default:
1151 1.8.2.3 yamt return EOPNOTSUPP;
1152 1.8.2.3 yamt }
1153 1.8.2.3 yamt
1154 1.8.2.3 yamt hotspare:
1155 1.8.2.3 yamt error = arc_msgbuf(sc, &req_hs, sizeof(req_hs),
1156 1.8.2.3 yamt reply, sizeof(reply));
1157 1.8.2.3 yamt if (error != 0)
1158 1.8.2.3 yamt return error;
1159 1.8.2.3 yamt
1160 1.8.2.3 yamt out:
1161 1.8.2.3 yamt return arc_fw_parse_status_code(sc, &reply[0]);
1162 1.8.2.3 yamt }
1163 1.8.2.2 yamt
1164 1.8.2.2 yamt static int
1165 1.8.2.2 yamt arc_bio_inq(struct arc_softc *sc, struct bioc_inq *bi)
1166 1.8.2.2 yamt {
1167 1.8.2.2 yamt uint8_t request[2];
1168 1.8.2.2 yamt struct arc_fw_sysinfo *sysinfo;
1169 1.8.2.3 yamt struct arc_fw_raidinfo *raidinfo;
1170 1.8.2.3 yamt int maxraidset, nvols = 0, i;
1171 1.8.2.2 yamt int error = 0;
1172 1.8.2.2 yamt
1173 1.8.2.2 yamt sysinfo = kmem_zalloc(sizeof(struct arc_fw_sysinfo), KM_SLEEP);
1174 1.8.2.3 yamt raidinfo = kmem_zalloc(sizeof(struct arc_fw_raidinfo), KM_SLEEP);
1175 1.8.2.2 yamt
1176 1.8.2.2 yamt request[0] = ARC_FW_SYSINFO;
1177 1.8.2.2 yamt error = arc_msgbuf(sc, request, 1, sysinfo,
1178 1.8.2.2 yamt sizeof(struct arc_fw_sysinfo));
1179 1.8.2.2 yamt if (error != 0)
1180 1.8.2.2 yamt goto out;
1181 1.8.2.2 yamt
1182 1.8.2.3 yamt maxraidset = sysinfo->max_raid_set;
1183 1.8.2.2 yamt
1184 1.8.2.3 yamt request[0] = ARC_FW_RAIDINFO;
1185 1.8.2.3 yamt for (i = 0; i < maxraidset; i++) {
1186 1.8.2.2 yamt request[1] = i;
1187 1.8.2.3 yamt error = arc_msgbuf(sc, request, sizeof(request), raidinfo,
1188 1.8.2.3 yamt sizeof(struct arc_fw_raidinfo));
1189 1.8.2.2 yamt if (error != 0)
1190 1.8.2.2 yamt goto out;
1191 1.8.2.2 yamt
1192 1.8.2.3 yamt if (raidinfo->volumes)
1193 1.8.2.2 yamt nvols++;
1194 1.8.2.2 yamt }
1195 1.8.2.2 yamt
1196 1.8.2.2 yamt strlcpy(bi->bi_dev, device_xname(&sc->sc_dev), sizeof(bi->bi_dev));
1197 1.8.2.2 yamt bi->bi_novol = nvols;
1198 1.8.2.3 yamt bi->bi_nodisk = sc->sc_maxdisks;
1199 1.8.2.3 yamt
1200 1.8.2.2 yamt out:
1201 1.8.2.3 yamt kmem_free(raidinfo, sizeof(*raidinfo));
1202 1.8.2.2 yamt kmem_free(sysinfo, sizeof(*sysinfo));
1203 1.8.2.2 yamt return error;
1204 1.8.2.2 yamt }
1205 1.8.2.2 yamt
1206 1.8.2.2 yamt static int
1207 1.8.2.2 yamt arc_bio_getvol(struct arc_softc *sc, int vol, struct arc_fw_volinfo *volinfo)
1208 1.8.2.2 yamt {
1209 1.8.2.2 yamt uint8_t request[2];
1210 1.8.2.2 yamt struct arc_fw_sysinfo *sysinfo;
1211 1.8.2.2 yamt int error = 0;
1212 1.8.2.2 yamt int maxvols, nvols = 0, i;
1213 1.8.2.2 yamt
1214 1.8.2.2 yamt sysinfo = kmem_zalloc(sizeof(struct arc_fw_sysinfo), KM_SLEEP);
1215 1.8.2.2 yamt
1216 1.8.2.2 yamt request[0] = ARC_FW_SYSINFO;
1217 1.8.2.2 yamt error = arc_msgbuf(sc, request, 1, sysinfo,
1218 1.8.2.2 yamt sizeof(struct arc_fw_sysinfo));
1219 1.8.2.2 yamt if (error != 0)
1220 1.8.2.2 yamt goto out;
1221 1.8.2.2 yamt
1222 1.8.2.2 yamt maxvols = sysinfo->max_volume_set;
1223 1.8.2.2 yamt
1224 1.8.2.2 yamt request[0] = ARC_FW_VOLINFO;
1225 1.8.2.2 yamt for (i = 0; i < maxvols; i++) {
1226 1.8.2.2 yamt request[1] = i;
1227 1.8.2.2 yamt error = arc_msgbuf(sc, request, sizeof(request), volinfo,
1228 1.8.2.2 yamt sizeof(struct arc_fw_volinfo));
1229 1.8.2.2 yamt if (error != 0)
1230 1.8.2.2 yamt goto out;
1231 1.8.2.2 yamt
1232 1.8.2.3 yamt if (volinfo->capacity == 0 && volinfo->capacity2 == 0)
1233 1.8.2.2 yamt continue;
1234 1.8.2.2 yamt
1235 1.8.2.2 yamt if (nvols == vol)
1236 1.8.2.2 yamt break;
1237 1.8.2.2 yamt
1238 1.8.2.2 yamt nvols++;
1239 1.8.2.2 yamt }
1240 1.8.2.2 yamt
1241 1.8.2.2 yamt if (nvols != vol ||
1242 1.8.2.3 yamt (volinfo->capacity == 0 && volinfo->capacity2 == 0)) {
1243 1.8.2.2 yamt error = ENODEV;
1244 1.8.2.2 yamt goto out;
1245 1.8.2.2 yamt }
1246 1.8.2.2 yamt
1247 1.8.2.2 yamt out:
1248 1.8.2.2 yamt kmem_free(sysinfo, sizeof(*sysinfo));
1249 1.8.2.2 yamt return error;
1250 1.8.2.2 yamt }
1251 1.8.2.2 yamt
1252 1.8.2.2 yamt static int
1253 1.8.2.2 yamt arc_bio_vol(struct arc_softc *sc, struct bioc_vol *bv)
1254 1.8.2.2 yamt {
1255 1.8.2.2 yamt struct arc_fw_volinfo *volinfo;
1256 1.8.2.2 yamt uint64_t blocks;
1257 1.8.2.2 yamt uint32_t status;
1258 1.8.2.2 yamt int error = 0;
1259 1.8.2.2 yamt
1260 1.8.2.2 yamt volinfo = kmem_zalloc(sizeof(struct arc_fw_volinfo), KM_SLEEP);
1261 1.8.2.2 yamt
1262 1.8.2.2 yamt error = arc_bio_getvol(sc, bv->bv_volid, volinfo);
1263 1.8.2.2 yamt if (error != 0)
1264 1.8.2.2 yamt goto out;
1265 1.8.2.2 yamt
1266 1.8.2.2 yamt bv->bv_percent = -1;
1267 1.8.2.2 yamt bv->bv_seconds = 0;
1268 1.8.2.2 yamt
1269 1.8.2.2 yamt status = htole32(volinfo->volume_status);
1270 1.8.2.2 yamt if (status == 0x0) {
1271 1.8.2.2 yamt if (htole32(volinfo->fail_mask) == 0x0)
1272 1.8.2.2 yamt bv->bv_status = BIOC_SVONLINE;
1273 1.8.2.2 yamt else
1274 1.8.2.2 yamt bv->bv_status = BIOC_SVDEGRADED;
1275 1.8.2.2 yamt } else if (status & ARC_FW_VOL_STATUS_NEED_REGEN) {
1276 1.8.2.2 yamt bv->bv_status = BIOC_SVDEGRADED;
1277 1.8.2.2 yamt } else if (status & ARC_FW_VOL_STATUS_FAILED) {
1278 1.8.2.2 yamt bv->bv_status = BIOC_SVOFFLINE;
1279 1.8.2.2 yamt } else if (status & ARC_FW_VOL_STATUS_INITTING) {
1280 1.8.2.2 yamt bv->bv_status = BIOC_SVBUILDING;
1281 1.8.2.3 yamt bv->bv_percent = htole32(volinfo->progress);
1282 1.8.2.2 yamt } else if (status & ARC_FW_VOL_STATUS_REBUILDING) {
1283 1.8.2.2 yamt bv->bv_status = BIOC_SVREBUILD;
1284 1.8.2.3 yamt bv->bv_percent = htole32(volinfo->progress);
1285 1.8.2.2 yamt } else if (status & ARC_FW_VOL_STATUS_MIGRATING) {
1286 1.8.2.2 yamt bv->bv_status = BIOC_SVMIGRATING;
1287 1.8.2.3 yamt bv->bv_percent = htole32(volinfo->progress);
1288 1.8.2.3 yamt } else if (status & ARC_FW_VOL_STATUS_CHECKING) {
1289 1.8.2.3 yamt bv->bv_status = BIOC_SVCHECKING;
1290 1.8.2.3 yamt bv->bv_percent = htole32(volinfo->progress);
1291 1.8.2.2 yamt }
1292 1.8.2.2 yamt
1293 1.8.2.2 yamt blocks = (uint64_t)htole32(volinfo->capacity2) << 32;
1294 1.8.2.2 yamt blocks += (uint64_t)htole32(volinfo->capacity);
1295 1.8.2.2 yamt bv->bv_size = blocks * ARC_BLOCKSIZE; /* XXX */
1296 1.8.2.2 yamt
1297 1.8.2.2 yamt switch (volinfo->raid_level) {
1298 1.8.2.2 yamt case ARC_FW_VOL_RAIDLEVEL_0:
1299 1.8.2.2 yamt bv->bv_level = 0;
1300 1.8.2.2 yamt break;
1301 1.8.2.2 yamt case ARC_FW_VOL_RAIDLEVEL_1:
1302 1.8.2.2 yamt bv->bv_level = 1;
1303 1.8.2.2 yamt break;
1304 1.8.2.2 yamt case ARC_FW_VOL_RAIDLEVEL_3:
1305 1.8.2.2 yamt bv->bv_level = 3;
1306 1.8.2.2 yamt break;
1307 1.8.2.2 yamt case ARC_FW_VOL_RAIDLEVEL_5:
1308 1.8.2.2 yamt bv->bv_level = 5;
1309 1.8.2.2 yamt break;
1310 1.8.2.2 yamt case ARC_FW_VOL_RAIDLEVEL_6:
1311 1.8.2.2 yamt bv->bv_level = 6;
1312 1.8.2.2 yamt break;
1313 1.8.2.2 yamt case ARC_FW_VOL_RAIDLEVEL_PASSTHRU:
1314 1.8.2.3 yamt bv->bv_level = BIOC_SVOL_PASSTHRU;
1315 1.8.2.3 yamt break;
1316 1.8.2.2 yamt default:
1317 1.8.2.2 yamt bv->bv_level = -1;
1318 1.8.2.2 yamt break;
1319 1.8.2.2 yamt }
1320 1.8.2.2 yamt
1321 1.8.2.2 yamt bv->bv_nodisk = volinfo->member_disks;
1322 1.8.2.3 yamt bv->bv_stripe_size = volinfo->stripe_size / 2;
1323 1.8.2.3 yamt snprintf(bv->bv_dev, sizeof(bv->bv_dev), "sd%d", bv->bv_volid);
1324 1.8.2.3 yamt scsipi_strvis(bv->bv_vendor, sizeof(bv->bv_vendor), volinfo->set_name,
1325 1.8.2.3 yamt sizeof(volinfo->set_name));
1326 1.8.2.2 yamt
1327 1.8.2.2 yamt out:
1328 1.8.2.2 yamt kmem_free(volinfo, sizeof(*volinfo));
1329 1.8.2.2 yamt return error;
1330 1.8.2.2 yamt }
1331 1.8.2.2 yamt
1332 1.8.2.2 yamt static int
1333 1.8.2.3 yamt arc_bio_disk_novol(struct arc_softc *sc, struct bioc_disk *bd)
1334 1.8.2.2 yamt {
1335 1.8.2.2 yamt struct arc_fw_diskinfo *diskinfo;
1336 1.8.2.3 yamt uint8_t request[2];
1337 1.8.2.2 yamt int error = 0;
1338 1.8.2.3 yamt
1339 1.8.2.3 yamt diskinfo = kmem_zalloc(sizeof(struct arc_fw_diskinfo), KM_SLEEP);
1340 1.8.2.3 yamt
1341 1.8.2.3 yamt if (bd->bd_diskid > sc->sc_maxdisks) {
1342 1.8.2.3 yamt error = ENODEV;
1343 1.8.2.3 yamt goto out;
1344 1.8.2.3 yamt }
1345 1.8.2.3 yamt
1346 1.8.2.3 yamt request[0] = ARC_FW_DISKINFO;
1347 1.8.2.3 yamt request[1] = bd->bd_diskid;
1348 1.8.2.3 yamt error = arc_msgbuf(sc, request, sizeof(request),
1349 1.8.2.3 yamt diskinfo, sizeof(struct arc_fw_diskinfo));
1350 1.8.2.3 yamt if (error != 0)
1351 1.8.2.3 yamt return error;
1352 1.8.2.3 yamt
1353 1.8.2.3 yamt /* skip disks with no capacity */
1354 1.8.2.3 yamt if (htole32(diskinfo->capacity) == 0 &&
1355 1.8.2.3 yamt htole32(diskinfo->capacity2) == 0)
1356 1.8.2.3 yamt goto out;
1357 1.8.2.3 yamt
1358 1.8.2.3 yamt bd->bd_disknovol = true;
1359 1.8.2.3 yamt arc_bio_disk_filldata(sc, bd, diskinfo, bd->bd_diskid);
1360 1.8.2.3 yamt
1361 1.8.2.3 yamt out:
1362 1.8.2.3 yamt kmem_free(diskinfo, sizeof(*diskinfo));
1363 1.8.2.3 yamt return error;
1364 1.8.2.3 yamt }
1365 1.8.2.3 yamt
1366 1.8.2.3 yamt static void
1367 1.8.2.3 yamt arc_bio_disk_filldata(struct arc_softc *sc, struct bioc_disk *bd,
1368 1.8.2.3 yamt struct arc_fw_diskinfo *diskinfo, int diskid)
1369 1.8.2.3 yamt {
1370 1.8.2.2 yamt uint64_t blocks;
1371 1.8.2.2 yamt char model[81];
1372 1.8.2.2 yamt char serial[41];
1373 1.8.2.2 yamt char rev[17];
1374 1.8.2.2 yamt
1375 1.8.2.3 yamt switch (htole32(diskinfo->device_state)) {
1376 1.8.2.3 yamt case ARC_FW_DISK_PASSTHRU:
1377 1.8.2.3 yamt bd->bd_status = BIOC_SDPASSTHRU;
1378 1.8.2.3 yamt break;
1379 1.8.2.3 yamt case ARC_FW_DISK_RAIDMEMBER:
1380 1.8.2.3 yamt bd->bd_status = BIOC_SDONLINE;
1381 1.8.2.3 yamt break;
1382 1.8.2.3 yamt case ARC_FW_DISK_HOTSPARE:
1383 1.8.2.3 yamt bd->bd_status = BIOC_SDHOTSPARE;
1384 1.8.2.3 yamt break;
1385 1.8.2.3 yamt case ARC_FW_DISK_UNUSED:
1386 1.8.2.3 yamt bd->bd_status = BIOC_SDUNUSED;
1387 1.8.2.3 yamt break;
1388 1.8.2.3 yamt default:
1389 1.8.2.3 yamt printf("%s: unknown disk device_state: 0x%x\n", __func__,
1390 1.8.2.3 yamt htole32(diskinfo->device_state));
1391 1.8.2.3 yamt bd->bd_status = BIOC_SDINVALID;
1392 1.8.2.3 yamt return;
1393 1.8.2.3 yamt }
1394 1.8.2.3 yamt
1395 1.8.2.3 yamt blocks = (uint64_t)htole32(diskinfo->capacity2) << 32;
1396 1.8.2.3 yamt blocks += (uint64_t)htole32(diskinfo->capacity);
1397 1.8.2.3 yamt bd->bd_size = blocks * ARC_BLOCKSIZE; /* XXX */
1398 1.8.2.3 yamt
1399 1.8.2.3 yamt scsipi_strvis(model, 81, diskinfo->model, sizeof(diskinfo->model));
1400 1.8.2.3 yamt scsipi_strvis(serial, 41, diskinfo->serial, sizeof(diskinfo->serial));
1401 1.8.2.3 yamt scsipi_strvis(rev, 17, diskinfo->firmware_rev,
1402 1.8.2.3 yamt sizeof(diskinfo->firmware_rev));
1403 1.8.2.3 yamt
1404 1.8.2.3 yamt snprintf(bd->bd_vendor, sizeof(bd->bd_vendor), "%s %s", model, rev);
1405 1.8.2.3 yamt strlcpy(bd->bd_serial, serial, sizeof(bd->bd_serial));
1406 1.8.2.3 yamt
1407 1.8.2.3 yamt #if 0
1408 1.8.2.3 yamt bd->bd_channel = diskinfo->scsi_attr.channel;
1409 1.8.2.3 yamt bd->bd_target = diskinfo->scsi_attr.target;
1410 1.8.2.3 yamt bd->bd_lun = diskinfo->scsi_attr.lun;
1411 1.8.2.3 yamt #endif
1412 1.8.2.3 yamt
1413 1.8.2.3 yamt /*
1414 1.8.2.3 yamt * the firwmare doesnt seem to fill scsi_attr in, so fake it with
1415 1.8.2.3 yamt * the diskid.
1416 1.8.2.3 yamt */
1417 1.8.2.3 yamt bd->bd_channel = 0;
1418 1.8.2.3 yamt bd->bd_target = diskid;
1419 1.8.2.3 yamt bd->bd_lun = 0;
1420 1.8.2.3 yamt }
1421 1.8.2.3 yamt
1422 1.8.2.3 yamt static int
1423 1.8.2.3 yamt arc_bio_disk_volume(struct arc_softc *sc, struct bioc_disk *bd)
1424 1.8.2.3 yamt {
1425 1.8.2.3 yamt uint8_t request[2];
1426 1.8.2.3 yamt struct arc_fw_raidinfo *raidinfo;
1427 1.8.2.3 yamt struct arc_fw_volinfo *volinfo;
1428 1.8.2.3 yamt struct arc_fw_diskinfo *diskinfo;
1429 1.8.2.3 yamt int error = 0;
1430 1.8.2.3 yamt
1431 1.8.2.2 yamt volinfo = kmem_zalloc(sizeof(struct arc_fw_volinfo), KM_SLEEP);
1432 1.8.2.2 yamt raidinfo = kmem_zalloc(sizeof(struct arc_fw_raidinfo), KM_SLEEP);
1433 1.8.2.2 yamt diskinfo = kmem_zalloc(sizeof(struct arc_fw_diskinfo), KM_SLEEP);
1434 1.8.2.2 yamt
1435 1.8.2.2 yamt error = arc_bio_getvol(sc, bd->bd_volid, volinfo);
1436 1.8.2.2 yamt if (error != 0)
1437 1.8.2.2 yamt goto out;
1438 1.8.2.2 yamt
1439 1.8.2.2 yamt request[0] = ARC_FW_RAIDINFO;
1440 1.8.2.2 yamt request[1] = volinfo->raid_set_number;
1441 1.8.2.2 yamt
1442 1.8.2.2 yamt error = arc_msgbuf(sc, request, sizeof(request), raidinfo,
1443 1.8.2.2 yamt sizeof(struct arc_fw_raidinfo));
1444 1.8.2.2 yamt if (error != 0)
1445 1.8.2.2 yamt goto out;
1446 1.8.2.2 yamt
1447 1.8.2.2 yamt if (bd->bd_diskid > raidinfo->member_devices) {
1448 1.8.2.2 yamt error = ENODEV;
1449 1.8.2.2 yamt goto out;
1450 1.8.2.2 yamt }
1451 1.8.2.2 yamt
1452 1.8.2.2 yamt request[0] = ARC_FW_DISKINFO;
1453 1.8.2.2 yamt request[1] = raidinfo->device_array[bd->bd_diskid];
1454 1.8.2.2 yamt error = arc_msgbuf(sc, request, sizeof(request), diskinfo,
1455 1.8.2.2 yamt sizeof(struct arc_fw_diskinfo));
1456 1.8.2.2 yamt if (error != 0)
1457 1.8.2.2 yamt goto out;
1458 1.8.2.2 yamt
1459 1.8.2.3 yamt /* now fill our bio disk with data from the firmware */
1460 1.8.2.3 yamt arc_bio_disk_filldata(sc, bd, diskinfo,
1461 1.8.2.3 yamt raidinfo->device_array[bd->bd_diskid]);
1462 1.8.2.2 yamt
1463 1.8.2.2 yamt out:
1464 1.8.2.2 yamt kmem_free(raidinfo, sizeof(*raidinfo));
1465 1.8.2.2 yamt kmem_free(volinfo, sizeof(*volinfo));
1466 1.8.2.3 yamt kmem_free(diskinfo, sizeof(*diskinfo));
1467 1.8.2.2 yamt return error;
1468 1.8.2.2 yamt }
1469 1.8.2.2 yamt #endif /* NBIO > 0 */
1470 1.8.2.2 yamt
1471 1.8.2.2 yamt uint8_t
1472 1.8.2.2 yamt arc_msg_cksum(void *cmd, uint16_t len)
1473 1.8.2.2 yamt {
1474 1.8.2.2 yamt uint8_t *buf = cmd;
1475 1.8.2.2 yamt uint8_t cksum;
1476 1.8.2.2 yamt int i;
1477 1.8.2.2 yamt
1478 1.8.2.2 yamt cksum = (uint8_t)(len >> 8) + (uint8_t)len;
1479 1.8.2.2 yamt for (i = 0; i < len; i++)
1480 1.8.2.2 yamt cksum += buf[i];
1481 1.8.2.2 yamt
1482 1.8.2.2 yamt return cksum;
1483 1.8.2.2 yamt }
1484 1.8.2.2 yamt
1485 1.8.2.2 yamt
1486 1.8.2.2 yamt int
1487 1.8.2.2 yamt arc_msgbuf(struct arc_softc *sc, void *wptr, size_t wbuflen, void *rptr,
1488 1.8.2.2 yamt size_t rbuflen)
1489 1.8.2.2 yamt {
1490 1.8.2.2 yamt uint8_t rwbuf[ARC_REG_IOC_RWBUF_MAXLEN];
1491 1.8.2.2 yamt uint8_t *wbuf, *rbuf;
1492 1.8.2.2 yamt int wlen, wdone = 0, rlen, rdone = 0;
1493 1.8.2.2 yamt struct arc_fw_bufhdr *bufhdr;
1494 1.8.2.2 yamt uint32_t reg, rwlen;
1495 1.8.2.2 yamt int error = 0;
1496 1.8.2.2 yamt #ifdef ARC_DEBUG
1497 1.8.2.2 yamt int i;
1498 1.8.2.2 yamt #endif
1499 1.8.2.2 yamt
1500 1.8.2.2 yamt wbuf = rbuf = NULL;
1501 1.8.2.2 yamt
1502 1.8.2.2 yamt DNPRINTF(ARC_D_DB, "%s: arc_msgbuf wbuflen: %d rbuflen: %d\n",
1503 1.8.2.2 yamt device_xname(&sc->sc_dev), wbuflen, rbuflen);
1504 1.8.2.2 yamt
1505 1.8.2.2 yamt wlen = sizeof(struct arc_fw_bufhdr) + wbuflen + 1; /* 1 for cksum */
1506 1.8.2.2 yamt wbuf = kmem_alloc(wlen, KM_SLEEP);
1507 1.8.2.2 yamt
1508 1.8.2.2 yamt rlen = sizeof(struct arc_fw_bufhdr) + rbuflen + 1; /* 1 for cksum */
1509 1.8.2.2 yamt rbuf = kmem_alloc(rlen, KM_SLEEP);
1510 1.8.2.2 yamt
1511 1.8.2.2 yamt DNPRINTF(ARC_D_DB, "%s: arc_msgbuf wlen: %d rlen: %d\n",
1512 1.8.2.2 yamt device_xname(&sc->sc_dev), wlen, rlen);
1513 1.8.2.2 yamt
1514 1.8.2.2 yamt bufhdr = (struct arc_fw_bufhdr *)wbuf;
1515 1.8.2.2 yamt bufhdr->hdr = arc_fw_hdr;
1516 1.8.2.2 yamt bufhdr->len = htole16(wbuflen);
1517 1.8.2.2 yamt memcpy(wbuf + sizeof(struct arc_fw_bufhdr), wptr, wbuflen);
1518 1.8.2.2 yamt wbuf[wlen - 1] = arc_msg_cksum(wptr, wbuflen);
1519 1.8.2.2 yamt
1520 1.8.2.2 yamt arc_lock(sc);
1521 1.8.2.2 yamt if (arc_read(sc, ARC_REG_OUTB_DOORBELL) != 0) {
1522 1.8.2.2 yamt error = EBUSY;
1523 1.8.2.2 yamt goto out;
1524 1.8.2.2 yamt }
1525 1.8.2.2 yamt
1526 1.8.2.2 yamt reg = ARC_REG_OUTB_DOORBELL_READ_OK;
1527 1.8.2.2 yamt
1528 1.8.2.2 yamt do {
1529 1.8.2.2 yamt if ((reg & ARC_REG_OUTB_DOORBELL_READ_OK) && wdone < wlen) {
1530 1.8.2.2 yamt memset(rwbuf, 0, sizeof(rwbuf));
1531 1.8.2.2 yamt rwlen = (wlen - wdone) % sizeof(rwbuf);
1532 1.8.2.2 yamt memcpy(rwbuf, &wbuf[wdone], rwlen);
1533 1.8.2.2 yamt
1534 1.8.2.2 yamt #ifdef ARC_DEBUG
1535 1.8.2.2 yamt if (arcdebug & ARC_D_DB) {
1536 1.8.2.2 yamt printf("%s: write %d:",
1537 1.8.2.2 yamt device_xname(&sc->sc_dev), rwlen);
1538 1.8.2.2 yamt for (i = 0; i < rwlen; i++)
1539 1.8.2.2 yamt printf(" 0x%02x", rwbuf[i]);
1540 1.8.2.2 yamt printf("\n");
1541 1.8.2.2 yamt }
1542 1.8.2.2 yamt #endif
1543 1.8.2.2 yamt
1544 1.8.2.2 yamt /* copy the chunk to the hw */
1545 1.8.2.2 yamt arc_write(sc, ARC_REG_IOC_WBUF_LEN, rwlen);
1546 1.8.2.2 yamt arc_write_region(sc, ARC_REG_IOC_WBUF, rwbuf,
1547 1.8.2.2 yamt sizeof(rwbuf));
1548 1.8.2.2 yamt
1549 1.8.2.2 yamt /* say we have a buffer for the hw */
1550 1.8.2.2 yamt arc_write(sc, ARC_REG_INB_DOORBELL,
1551 1.8.2.2 yamt ARC_REG_INB_DOORBELL_WRITE_OK);
1552 1.8.2.2 yamt
1553 1.8.2.2 yamt wdone += rwlen;
1554 1.8.2.2 yamt }
1555 1.8.2.2 yamt
1556 1.8.2.2 yamt while ((reg = arc_read(sc, ARC_REG_OUTB_DOORBELL)) == 0)
1557 1.8.2.2 yamt arc_wait(sc);
1558 1.8.2.3 yamt
1559 1.8.2.2 yamt arc_write(sc, ARC_REG_OUTB_DOORBELL, reg);
1560 1.8.2.2 yamt
1561 1.8.2.2 yamt DNPRINTF(ARC_D_DB, "%s: reg: 0x%08x\n",
1562 1.8.2.2 yamt device_xname(&sc->sc_dev), reg);
1563 1.8.2.2 yamt
1564 1.8.2.2 yamt if ((reg & ARC_REG_OUTB_DOORBELL_WRITE_OK) && rdone < rlen) {
1565 1.8.2.2 yamt rwlen = arc_read(sc, ARC_REG_IOC_RBUF_LEN);
1566 1.8.2.2 yamt if (rwlen > sizeof(rwbuf)) {
1567 1.8.2.2 yamt DNPRINTF(ARC_D_DB, "%s: rwlen too big\n",
1568 1.8.2.2 yamt device_xname(&sc->sc_dev));
1569 1.8.2.2 yamt error = EIO;
1570 1.8.2.2 yamt goto out;
1571 1.8.2.2 yamt }
1572 1.8.2.2 yamt
1573 1.8.2.2 yamt arc_read_region(sc, ARC_REG_IOC_RBUF, rwbuf,
1574 1.8.2.2 yamt sizeof(rwbuf));
1575 1.8.2.2 yamt
1576 1.8.2.2 yamt arc_write(sc, ARC_REG_INB_DOORBELL,
1577 1.8.2.2 yamt ARC_REG_INB_DOORBELL_READ_OK);
1578 1.8.2.2 yamt
1579 1.8.2.2 yamt #ifdef ARC_DEBUG
1580 1.8.2.2 yamt printf("%s: len: %d+%d=%d/%d\n",
1581 1.8.2.2 yamt device_xname(&sc->sc_dev),
1582 1.8.2.2 yamt rwlen, rdone, rwlen + rdone, rlen);
1583 1.8.2.2 yamt if (arcdebug & ARC_D_DB) {
1584 1.8.2.2 yamt printf("%s: read:",
1585 1.8.2.2 yamt device_xname(&sc->sc_dev));
1586 1.8.2.2 yamt for (i = 0; i < rwlen; i++)
1587 1.8.2.2 yamt printf(" 0x%02x", rwbuf[i]);
1588 1.8.2.2 yamt printf("\n");
1589 1.8.2.2 yamt }
1590 1.8.2.2 yamt #endif
1591 1.8.2.2 yamt
1592 1.8.2.2 yamt if ((rdone + rwlen) > rlen) {
1593 1.8.2.2 yamt DNPRINTF(ARC_D_DB, "%s: rwbuf too big\n",
1594 1.8.2.2 yamt device_xname(&sc->sc_dev));
1595 1.8.2.2 yamt error = EIO;
1596 1.8.2.2 yamt goto out;
1597 1.8.2.2 yamt }
1598 1.8.2.2 yamt
1599 1.8.2.2 yamt memcpy(&rbuf[rdone], rwbuf, rwlen);
1600 1.8.2.2 yamt rdone += rwlen;
1601 1.8.2.2 yamt }
1602 1.8.2.2 yamt } while (rdone != rlen);
1603 1.8.2.2 yamt
1604 1.8.2.2 yamt bufhdr = (struct arc_fw_bufhdr *)rbuf;
1605 1.8.2.2 yamt if (memcmp(&bufhdr->hdr, &arc_fw_hdr, sizeof(bufhdr->hdr)) != 0 ||
1606 1.8.2.2 yamt bufhdr->len != htole16(rbuflen)) {
1607 1.8.2.2 yamt DNPRINTF(ARC_D_DB, "%s: rbuf hdr is wrong\n",
1608 1.8.2.2 yamt device_xname(&sc->sc_dev));
1609 1.8.2.2 yamt error = EIO;
1610 1.8.2.2 yamt goto out;
1611 1.8.2.2 yamt }
1612 1.8.2.2 yamt
1613 1.8.2.2 yamt memcpy(rptr, rbuf + sizeof(struct arc_fw_bufhdr), rbuflen);
1614 1.8.2.2 yamt
1615 1.8.2.2 yamt if (rbuf[rlen - 1] != arc_msg_cksum(rptr, rbuflen)) {
1616 1.8.2.2 yamt DNPRINTF(ARC_D_DB, "%s: invalid cksum\n",
1617 1.8.2.2 yamt device_xname(&sc->sc_dev));
1618 1.8.2.2 yamt error = EIO;
1619 1.8.2.2 yamt goto out;
1620 1.8.2.2 yamt }
1621 1.8.2.2 yamt
1622 1.8.2.2 yamt out:
1623 1.8.2.2 yamt arc_unlock(sc);
1624 1.8.2.2 yamt kmem_free(wbuf, wlen);
1625 1.8.2.2 yamt kmem_free(rbuf, rlen);
1626 1.8.2.2 yamt
1627 1.8.2.2 yamt return error;
1628 1.8.2.2 yamt }
1629 1.8.2.2 yamt
1630 1.8.2.2 yamt void
1631 1.8.2.2 yamt arc_lock(struct arc_softc *sc)
1632 1.8.2.2 yamt {
1633 1.8.2.2 yamt rw_enter(&sc->sc_rwlock, RW_WRITER);
1634 1.8.2.2 yamt mutex_spin_enter(&sc->sc_mutex);
1635 1.8.2.2 yamt arc_write(sc, ARC_REG_INTRMASK, ~ARC_REG_INTRMASK_POSTQUEUE);
1636 1.8.2.2 yamt sc->sc_talking = 1;
1637 1.8.2.2 yamt }
1638 1.8.2.2 yamt
1639 1.8.2.2 yamt void
1640 1.8.2.2 yamt arc_unlock(struct arc_softc *sc)
1641 1.8.2.2 yamt {
1642 1.8.2.2 yamt KASSERT(mutex_owned(&sc->sc_mutex));
1643 1.8.2.2 yamt
1644 1.8.2.2 yamt arc_write(sc, ARC_REG_INTRMASK,
1645 1.8.2.2 yamt ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRMASK_DOORBELL));
1646 1.8.2.3 yamt sc->sc_talking = 0;
1647 1.8.2.2 yamt mutex_spin_exit(&sc->sc_mutex);
1648 1.8.2.2 yamt rw_exit(&sc->sc_rwlock);
1649 1.8.2.2 yamt }
1650 1.8.2.2 yamt
1651 1.8.2.2 yamt void
1652 1.8.2.2 yamt arc_wait(struct arc_softc *sc)
1653 1.8.2.2 yamt {
1654 1.8.2.2 yamt KASSERT(mutex_owned(&sc->sc_mutex));
1655 1.8.2.2 yamt
1656 1.8.2.2 yamt arc_write(sc, ARC_REG_INTRMASK,
1657 1.8.2.2 yamt ~(ARC_REG_INTRMASK_POSTQUEUE|ARC_REG_INTRMASK_DOORBELL));
1658 1.8.2.3 yamt if (cv_timedwait(&sc->sc_condvar, &sc->sc_mutex, hz) == EWOULDBLOCK)
1659 1.8.2.2 yamt arc_write(sc, ARC_REG_INTRMASK, ~ARC_REG_INTRMASK_POSTQUEUE);
1660 1.8.2.2 yamt }
1661 1.8.2.2 yamt
1662 1.8.2.2 yamt #if NBIO > 0
1663 1.8.2.2 yamt static void
1664 1.8.2.2 yamt arc_create_sensors(void *arg)
1665 1.8.2.2 yamt {
1666 1.8.2.2 yamt struct arc_softc *sc = arg;
1667 1.8.2.2 yamt struct bioc_inq bi;
1668 1.8.2.2 yamt struct bioc_vol bv;
1669 1.8.2.2 yamt int i;
1670 1.8.2.2 yamt size_t slen;
1671 1.8.2.2 yamt
1672 1.8.2.2 yamt memset(&bi, 0, sizeof(bi));
1673 1.8.2.2 yamt if (arc_bio_inq(sc, &bi) != 0) {
1674 1.8.2.2 yamt aprint_error("%s: unable to query firmware for sensor info\n",
1675 1.8.2.2 yamt device_xname(&sc->sc_dev));
1676 1.8.2.2 yamt kthread_exit(0);
1677 1.8.2.2 yamt }
1678 1.8.2.2 yamt
1679 1.8.2.2 yamt sc->sc_nsensors = bi.bi_novol;
1680 1.8.2.2 yamt /*
1681 1.8.2.2 yamt * There's no point to continue if there are no drives connected...
1682 1.8.2.2 yamt */
1683 1.8.2.2 yamt if (!sc->sc_nsensors)
1684 1.8.2.2 yamt kthread_exit(0);
1685 1.8.2.2 yamt
1686 1.8.2.2 yamt sc->sc_sme = sysmon_envsys_create();
1687 1.8.2.2 yamt slen = sizeof(envsys_data_t) * sc->sc_nsensors;
1688 1.8.2.2 yamt sc->sc_sensors = kmem_zalloc(slen, KM_SLEEP);
1689 1.8.2.2 yamt
1690 1.8.2.2 yamt for (i = 0; i < sc->sc_nsensors; i++) {
1691 1.8.2.2 yamt memset(&bv, 0, sizeof(bv));
1692 1.8.2.2 yamt bv.bv_volid = i;
1693 1.8.2.2 yamt if (arc_bio_vol(sc, &bv) != 0)
1694 1.8.2.2 yamt goto bad;
1695 1.8.2.2 yamt
1696 1.8.2.2 yamt sc->sc_sensors[i].units = ENVSYS_DRIVE;
1697 1.8.2.2 yamt sc->sc_sensors[i].monitor = true;
1698 1.8.2.2 yamt sc->sc_sensors[i].flags = ENVSYS_FMONSTCHANGED;
1699 1.8.2.3 yamt snprintf(sc->sc_sensors[i].desc, sizeof(sc->sc_sensors[i].desc),
1700 1.8.2.3 yamt "RAID volume %s", bv.bv_dev);
1701 1.8.2.2 yamt if (sysmon_envsys_sensor_attach(sc->sc_sme, &sc->sc_sensors[i]))
1702 1.8.2.2 yamt goto bad;
1703 1.8.2.2 yamt }
1704 1.8.2.2 yamt
1705 1.8.2.2 yamt sc->sc_sme->sme_name = device_xname(&sc->sc_dev);
1706 1.8.2.2 yamt sc->sc_sme->sme_cookie = sc;
1707 1.8.2.2 yamt sc->sc_sme->sme_refresh = arc_refresh_sensors;
1708 1.8.2.2 yamt if (sysmon_envsys_register(sc->sc_sme)) {
1709 1.8.2.2 yamt aprint_debug("%s: unable to register with sysmon\n",
1710 1.8.2.2 yamt device_xname(&sc->sc_dev));
1711 1.8.2.2 yamt goto bad;
1712 1.8.2.2 yamt }
1713 1.8.2.2 yamt kthread_exit(0);
1714 1.8.2.2 yamt
1715 1.8.2.2 yamt bad:
1716 1.8.2.2 yamt kmem_free(sc->sc_sensors, slen);
1717 1.8.2.2 yamt sysmon_envsys_destroy(sc->sc_sme);
1718 1.8.2.2 yamt kthread_exit(0);
1719 1.8.2.2 yamt }
1720 1.8.2.2 yamt
1721 1.8.2.2 yamt static void
1722 1.8.2.2 yamt arc_refresh_sensors(struct sysmon_envsys *sme, envsys_data_t *edata)
1723 1.8.2.2 yamt {
1724 1.8.2.2 yamt struct arc_softc *sc = sme->sme_cookie;
1725 1.8.2.2 yamt struct bioc_vol bv;
1726 1.8.2.2 yamt
1727 1.8.2.2 yamt memset(&bv, 0, sizeof(bv));
1728 1.8.2.2 yamt bv.bv_volid = edata->sensor;
1729 1.8.2.2 yamt
1730 1.8.2.2 yamt if (arc_bio_vol(sc, &bv)) {
1731 1.8.2.2 yamt edata->value_cur = ENVSYS_DRIVE_EMPTY;
1732 1.8.2.2 yamt edata->state = ENVSYS_SINVALID;
1733 1.8.2.2 yamt return;
1734 1.8.2.2 yamt }
1735 1.8.2.2 yamt
1736 1.8.2.2 yamt switch (bv.bv_status) {
1737 1.8.2.2 yamt case BIOC_SVOFFLINE:
1738 1.8.2.2 yamt edata->value_cur = ENVSYS_DRIVE_FAIL;
1739 1.8.2.2 yamt edata->state = ENVSYS_SCRITICAL;
1740 1.8.2.2 yamt break;
1741 1.8.2.2 yamt case BIOC_SVDEGRADED:
1742 1.8.2.2 yamt edata->value_cur = ENVSYS_DRIVE_PFAIL;
1743 1.8.2.2 yamt edata->state = ENVSYS_SCRITICAL;
1744 1.8.2.2 yamt break;
1745 1.8.2.2 yamt case BIOC_SVBUILDING:
1746 1.8.2.2 yamt edata->value_cur = ENVSYS_DRIVE_REBUILD;
1747 1.8.2.2 yamt edata->state = ENVSYS_SVALID;
1748 1.8.2.2 yamt break;
1749 1.8.2.2 yamt case BIOC_SVMIGRATING:
1750 1.8.2.2 yamt edata->value_cur = ENVSYS_DRIVE_MIGRATING;
1751 1.8.2.2 yamt edata->state = ENVSYS_SVALID;
1752 1.8.2.2 yamt break;
1753 1.8.2.2 yamt case BIOC_SVSCRUB:
1754 1.8.2.2 yamt case BIOC_SVONLINE:
1755 1.8.2.2 yamt edata->value_cur = ENVSYS_DRIVE_ONLINE;
1756 1.8.2.2 yamt edata->state = ENVSYS_SVALID;
1757 1.8.2.2 yamt break;
1758 1.8.2.2 yamt case BIOC_SVINVALID:
1759 1.8.2.2 yamt /* FALLTRHOUGH */
1760 1.8.2.2 yamt default:
1761 1.8.2.2 yamt edata->value_cur = ENVSYS_DRIVE_EMPTY; /* unknown state */
1762 1.8.2.2 yamt edata->state = ENVSYS_SINVALID;
1763 1.8.2.2 yamt break;
1764 1.8.2.2 yamt }
1765 1.8.2.2 yamt }
1766 1.8.2.2 yamt #endif /* NBIO > 0 */
1767 1.8.2.2 yamt
1768 1.8.2.2 yamt uint32_t
1769 1.8.2.2 yamt arc_read(struct arc_softc *sc, bus_size_t r)
1770 1.8.2.2 yamt {
1771 1.8.2.2 yamt uint32_t v;
1772 1.8.2.2 yamt
1773 1.8.2.2 yamt bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
1774 1.8.2.2 yamt BUS_SPACE_BARRIER_READ);
1775 1.8.2.2 yamt v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, r);
1776 1.8.2.2 yamt
1777 1.8.2.2 yamt DNPRINTF(ARC_D_RW, "%s: arc_read 0x%lx 0x%08x\n",
1778 1.8.2.2 yamt device_xname(&sc->sc_dev), r, v);
1779 1.8.2.2 yamt
1780 1.8.2.2 yamt return v;
1781 1.8.2.2 yamt }
1782 1.8.2.2 yamt
1783 1.8.2.2 yamt void
1784 1.8.2.2 yamt arc_read_region(struct arc_softc *sc, bus_size_t r, void *buf, size_t len)
1785 1.8.2.2 yamt {
1786 1.8.2.2 yamt bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, len,
1787 1.8.2.2 yamt BUS_SPACE_BARRIER_READ);
1788 1.8.2.2 yamt bus_space_read_region_4(sc->sc_iot, sc->sc_ioh, r,
1789 1.8.2.2 yamt (uint32_t *)buf, len >> 2);
1790 1.8.2.2 yamt }
1791 1.8.2.2 yamt
1792 1.8.2.2 yamt void
1793 1.8.2.2 yamt arc_write(struct arc_softc *sc, bus_size_t r, uint32_t v)
1794 1.8.2.2 yamt {
1795 1.8.2.2 yamt DNPRINTF(ARC_D_RW, "%s: arc_write 0x%lx 0x%08x\n",
1796 1.8.2.2 yamt device_xname(&sc->sc_dev), r, v);
1797 1.8.2.2 yamt
1798 1.8.2.2 yamt bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
1799 1.8.2.2 yamt bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
1800 1.8.2.2 yamt BUS_SPACE_BARRIER_WRITE);
1801 1.8.2.2 yamt }
1802 1.8.2.2 yamt
1803 1.8.2.2 yamt void
1804 1.8.2.2 yamt arc_write_region(struct arc_softc *sc, bus_size_t r, void *buf, size_t len)
1805 1.8.2.2 yamt {
1806 1.8.2.2 yamt bus_space_write_region_4(sc->sc_iot, sc->sc_ioh, r,
1807 1.8.2.2 yamt (const uint32_t *)buf, len >> 2);
1808 1.8.2.2 yamt bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, len,
1809 1.8.2.2 yamt BUS_SPACE_BARRIER_WRITE);
1810 1.8.2.2 yamt }
1811 1.8.2.2 yamt
1812 1.8.2.2 yamt int
1813 1.8.2.2 yamt arc_wait_eq(struct arc_softc *sc, bus_size_t r, uint32_t mask,
1814 1.8.2.2 yamt uint32_t target)
1815 1.8.2.2 yamt {
1816 1.8.2.2 yamt int i;
1817 1.8.2.2 yamt
1818 1.8.2.2 yamt DNPRINTF(ARC_D_RW, "%s: arc_wait_eq 0x%lx 0x%08x 0x%08x\n",
1819 1.8.2.2 yamt device_xname(&sc->sc_dev), r, mask, target);
1820 1.8.2.2 yamt
1821 1.8.2.2 yamt for (i = 0; i < 10000; i++) {
1822 1.8.2.2 yamt if ((arc_read(sc, r) & mask) == target)
1823 1.8.2.2 yamt return 0;
1824 1.8.2.2 yamt delay(1000);
1825 1.8.2.2 yamt }
1826 1.8.2.2 yamt
1827 1.8.2.2 yamt return 1;
1828 1.8.2.2 yamt }
1829 1.8.2.2 yamt
1830 1.8.2.2 yamt int
1831 1.8.2.2 yamt arc_wait_ne(struct arc_softc *sc, bus_size_t r, uint32_t mask,
1832 1.8.2.2 yamt uint32_t target)
1833 1.8.2.2 yamt {
1834 1.8.2.2 yamt int i;
1835 1.8.2.2 yamt
1836 1.8.2.2 yamt DNPRINTF(ARC_D_RW, "%s: arc_wait_ne 0x%lx 0x%08x 0x%08x\n",
1837 1.8.2.2 yamt device_xname(&sc->sc_dev), r, mask, target);
1838 1.8.2.2 yamt
1839 1.8.2.2 yamt for (i = 0; i < 10000; i++) {
1840 1.8.2.2 yamt if ((arc_read(sc, r) & mask) != target)
1841 1.8.2.2 yamt return 0;
1842 1.8.2.2 yamt delay(1000);
1843 1.8.2.2 yamt }
1844 1.8.2.2 yamt
1845 1.8.2.2 yamt return 1;
1846 1.8.2.2 yamt }
1847 1.8.2.2 yamt
1848 1.8.2.2 yamt int
1849 1.8.2.2 yamt arc_msg0(struct arc_softc *sc, uint32_t m)
1850 1.8.2.2 yamt {
1851 1.8.2.2 yamt /* post message */
1852 1.8.2.2 yamt arc_write(sc, ARC_REG_INB_MSG0, m);
1853 1.8.2.2 yamt /* wait for the fw to do it */
1854 1.8.2.2 yamt if (arc_wait_eq(sc, ARC_REG_INTRSTAT, ARC_REG_INTRSTAT_MSG0,
1855 1.8.2.2 yamt ARC_REG_INTRSTAT_MSG0) != 0)
1856 1.8.2.2 yamt return 1;
1857 1.8.2.2 yamt
1858 1.8.2.2 yamt /* ack it */
1859 1.8.2.2 yamt arc_write(sc, ARC_REG_INTRSTAT, ARC_REG_INTRSTAT_MSG0);
1860 1.8.2.2 yamt
1861 1.8.2.2 yamt return 0;
1862 1.8.2.2 yamt }
1863 1.8.2.2 yamt
1864 1.8.2.2 yamt struct arc_dmamem *
1865 1.8.2.2 yamt arc_dmamem_alloc(struct arc_softc *sc, size_t size)
1866 1.8.2.2 yamt {
1867 1.8.2.2 yamt struct arc_dmamem *adm;
1868 1.8.2.2 yamt int nsegs;
1869 1.8.2.2 yamt
1870 1.8.2.2 yamt adm = kmem_zalloc(sizeof(*adm), KM_NOSLEEP);
1871 1.8.2.2 yamt if (adm == NULL)
1872 1.8.2.2 yamt return NULL;
1873 1.8.2.2 yamt
1874 1.8.2.2 yamt adm->adm_size = size;
1875 1.8.2.2 yamt
1876 1.8.2.2 yamt if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1877 1.8.2.2 yamt BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &adm->adm_map) != 0)
1878 1.8.2.2 yamt goto admfree;
1879 1.8.2.2 yamt
1880 1.8.2.2 yamt if (bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &adm->adm_seg,
1881 1.8.2.2 yamt 1, &nsegs, BUS_DMA_NOWAIT) != 0)
1882 1.8.2.2 yamt goto destroy;
1883 1.8.2.2 yamt
1884 1.8.2.2 yamt if (bus_dmamem_map(sc->sc_dmat, &adm->adm_seg, nsegs, size,
1885 1.8.2.2 yamt &adm->adm_kva, BUS_DMA_NOWAIT|BUS_DMA_COHERENT) != 0)
1886 1.8.2.2 yamt goto free;
1887 1.8.2.2 yamt
1888 1.8.2.2 yamt if (bus_dmamap_load(sc->sc_dmat, adm->adm_map, adm->adm_kva, size,
1889 1.8.2.2 yamt NULL, BUS_DMA_NOWAIT) != 0)
1890 1.8.2.2 yamt goto unmap;
1891 1.8.2.2 yamt
1892 1.8.2.2 yamt memset(adm->adm_kva, 0, size);
1893 1.8.2.2 yamt
1894 1.8.2.2 yamt return adm;
1895 1.8.2.2 yamt
1896 1.8.2.2 yamt unmap:
1897 1.8.2.2 yamt bus_dmamem_unmap(sc->sc_dmat, adm->adm_kva, size);
1898 1.8.2.2 yamt free:
1899 1.8.2.2 yamt bus_dmamem_free(sc->sc_dmat, &adm->adm_seg, 1);
1900 1.8.2.2 yamt destroy:
1901 1.8.2.2 yamt bus_dmamap_destroy(sc->sc_dmat, adm->adm_map);
1902 1.8.2.2 yamt admfree:
1903 1.8.2.2 yamt kmem_free(adm, sizeof(*adm));
1904 1.8.2.2 yamt
1905 1.8.2.2 yamt return NULL;
1906 1.8.2.2 yamt }
1907 1.8.2.2 yamt
1908 1.8.2.2 yamt void
1909 1.8.2.2 yamt arc_dmamem_free(struct arc_softc *sc, struct arc_dmamem *adm)
1910 1.8.2.2 yamt {
1911 1.8.2.2 yamt bus_dmamap_unload(sc->sc_dmat, adm->adm_map);
1912 1.8.2.2 yamt bus_dmamem_unmap(sc->sc_dmat, adm->adm_kva, adm->adm_size);
1913 1.8.2.2 yamt bus_dmamem_free(sc->sc_dmat, &adm->adm_seg, 1);
1914 1.8.2.2 yamt bus_dmamap_destroy(sc->sc_dmat, adm->adm_map);
1915 1.8.2.2 yamt kmem_free(adm, sizeof(*adm));
1916 1.8.2.2 yamt }
1917 1.8.2.2 yamt
1918 1.8.2.2 yamt int
1919 1.8.2.2 yamt arc_alloc_ccbs(struct arc_softc *sc)
1920 1.8.2.2 yamt {
1921 1.8.2.2 yamt struct arc_ccb *ccb;
1922 1.8.2.2 yamt uint8_t *cmd;
1923 1.8.2.2 yamt int i;
1924 1.8.2.2 yamt size_t ccbslen;
1925 1.8.2.2 yamt
1926 1.8.2.2 yamt TAILQ_INIT(&sc->sc_ccb_free);
1927 1.8.2.2 yamt
1928 1.8.2.2 yamt ccbslen = sizeof(struct arc_ccb) * sc->sc_req_count;
1929 1.8.2.2 yamt sc->sc_ccbs = kmem_zalloc(ccbslen, KM_SLEEP);
1930 1.8.2.2 yamt
1931 1.8.2.2 yamt sc->sc_requests = arc_dmamem_alloc(sc,
1932 1.8.2.2 yamt ARC_MAX_IOCMDLEN * sc->sc_req_count);
1933 1.8.2.2 yamt if (sc->sc_requests == NULL) {
1934 1.8.2.2 yamt aprint_error("%s: unable to allocate ccb dmamem\n",
1935 1.8.2.2 yamt device_xname(&sc->sc_dev));
1936 1.8.2.2 yamt goto free_ccbs;
1937 1.8.2.2 yamt }
1938 1.8.2.2 yamt cmd = ARC_DMA_KVA(sc->sc_requests);
1939 1.8.2.2 yamt
1940 1.8.2.2 yamt for (i = 0; i < sc->sc_req_count; i++) {
1941 1.8.2.2 yamt ccb = &sc->sc_ccbs[i];
1942 1.8.2.2 yamt
1943 1.8.2.2 yamt if (bus_dmamap_create(sc->sc_dmat, MAXPHYS, ARC_SGL_MAXLEN,
1944 1.8.2.2 yamt MAXPHYS, 0, 0, &ccb->ccb_dmamap) != 0) {
1945 1.8.2.2 yamt aprint_error("%s: unable to create dmamap for ccb %d\n",
1946 1.8.2.2 yamt device_xname(&sc->sc_dev), i);
1947 1.8.2.2 yamt goto free_maps;
1948 1.8.2.2 yamt }
1949 1.8.2.2 yamt
1950 1.8.2.2 yamt ccb->ccb_sc = sc;
1951 1.8.2.2 yamt ccb->ccb_id = i;
1952 1.8.2.2 yamt ccb->ccb_offset = ARC_MAX_IOCMDLEN * i;
1953 1.8.2.2 yamt
1954 1.8.2.2 yamt ccb->ccb_cmd = (struct arc_io_cmd *)&cmd[ccb->ccb_offset];
1955 1.8.2.2 yamt ccb->ccb_cmd_post = (ARC_DMA_DVA(sc->sc_requests) +
1956 1.8.2.2 yamt ccb->ccb_offset) >> ARC_REG_POST_QUEUE_ADDR_SHIFT;
1957 1.8.2.2 yamt
1958 1.8.2.2 yamt arc_put_ccb(sc, ccb);
1959 1.8.2.2 yamt }
1960 1.8.2.2 yamt
1961 1.8.2.2 yamt return 0;
1962 1.8.2.2 yamt
1963 1.8.2.2 yamt free_maps:
1964 1.8.2.2 yamt while ((ccb = arc_get_ccb(sc)) != NULL)
1965 1.8.2.2 yamt bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
1966 1.8.2.2 yamt arc_dmamem_free(sc, sc->sc_requests);
1967 1.8.2.2 yamt
1968 1.8.2.2 yamt free_ccbs:
1969 1.8.2.2 yamt kmem_free(sc->sc_ccbs, ccbslen);
1970 1.8.2.2 yamt
1971 1.8.2.2 yamt return 1;
1972 1.8.2.2 yamt }
1973 1.8.2.2 yamt
1974 1.8.2.2 yamt struct arc_ccb *
1975 1.8.2.2 yamt arc_get_ccb(struct arc_softc *sc)
1976 1.8.2.2 yamt {
1977 1.8.2.2 yamt struct arc_ccb *ccb;
1978 1.8.2.2 yamt
1979 1.8.2.2 yamt ccb = TAILQ_FIRST(&sc->sc_ccb_free);
1980 1.8.2.2 yamt if (ccb != NULL)
1981 1.8.2.2 yamt TAILQ_REMOVE(&sc->sc_ccb_free, ccb, ccb_link);
1982 1.8.2.2 yamt
1983 1.8.2.2 yamt return ccb;
1984 1.8.2.2 yamt }
1985 1.8.2.2 yamt
1986 1.8.2.2 yamt void
1987 1.8.2.2 yamt arc_put_ccb(struct arc_softc *sc, struct arc_ccb *ccb)
1988 1.8.2.2 yamt {
1989 1.8.2.2 yamt ccb->ccb_xs = NULL;
1990 1.8.2.2 yamt memset(ccb->ccb_cmd, 0, ARC_MAX_IOCMDLEN);
1991 1.8.2.2 yamt TAILQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_link);
1992 1.8.2.2 yamt }
1993