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