1 /* $NetBSD: arcmsr.c,v 1.21 2008/06/24 10:14:41 gmcgarry Exp $ */
2 /* $OpenBSD: arc.c,v 1.68 2007/10/27 03:28:27 dlg Exp $ */
5 * Copyright (c) 2007, 2008 Juan Romero Pardines <xtraeme@netbsd.org>
6 * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
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.
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.
23 #include <sys/cdefs.h>
24 __KERNEL_RCSID(0, "$NetBSD: arcmsr.c,v 1.21 2008/06/24 10:14:41 gmcgarry Exp $");
26 #include <sys/param.h>
28 #include <sys/kernel.h>
29 #include <sys/malloc.h>
30 #include <sys/device.h>
32 #include <sys/kthread.h>
33 #include <sys/mutex.h>
34 #include <sys/condvar.h>
35 #include <sys/rwlock.h>
38 #include <sys/ioctl.h>
39 #include <dev/biovar.h>
42 #include <dev/pci/pcireg.h>
43 #include <dev/pci/pcivar.h>
44 #include <dev/pci/pcidevs.h>
46 #include <dev/scsipi/scsipi_all.h>
47 #include <dev/scsipi/scsi_all.h>
48 #include <dev/scsipi/scsiconf.h>
50 #include <dev/sysmon/sysmonvar.h>
54 #include <uvm/uvm_extern.h> /* for PAGE_SIZE */
56 #include <dev/pci/arcmsrvar.h>
58 /* #define ARC_DEBUG */
60 #define ARC_D_INIT (1<<0)
61 #define ARC_D_RW (1<<1)
62 #define ARC_D_DB (1<<2)
66 #define DPRINTF(p...) do { if (arcdebug) printf(p); } while (0)
67 #define DNPRINTF(n, p...) do { if ((n) & arcdebug) printf(p); } while (0)
70 #define DPRINTF(p, ...) /* p */
71 #define DNPRINTF(n, p, ...) /* n, p */
75 * the fw header must always equal this.
77 static struct arc_fw_hdr arc_fw_hdr
= { 0x5e, 0x01, 0x61 };
82 static int arc_match(device_t
, cfdata_t
, void *);
83 static void arc_attach(device_t
, device_t
, void *);
84 static int arc_detach(device_t
, int);
85 static bool arc_shutdown(device_t
, int);
86 static int arc_intr(void *);
87 static void arc_minphys(struct buf
*);
89 CFATTACH_DECL_NEW(arcmsr
, sizeof(struct arc_softc
),
90 arc_match
, arc_attach
, arc_detach
, NULL
);
93 * bio(4) and sysmon_envsys(9) glue.
96 static int arc_bioctl(device_t
, u_long
, void *);
97 static int arc_bio_inq(struct arc_softc
*, struct bioc_inq
*);
98 static int arc_bio_vol(struct arc_softc
*, struct bioc_vol
*);
99 static int arc_bio_disk_volume(struct arc_softc
*, struct bioc_disk
*);
100 static int arc_bio_disk_novol(struct arc_softc
*, struct bioc_disk
*);
101 static void arc_bio_disk_filldata(struct arc_softc
*, struct bioc_disk
*,
102 struct arc_fw_diskinfo
*, int);
103 static int arc_bio_alarm(struct arc_softc
*, struct bioc_alarm
*);
104 static int arc_bio_alarm_state(struct arc_softc
*, struct bioc_alarm
*);
105 static int arc_bio_getvol(struct arc_softc
*, int,
106 struct arc_fw_volinfo
*);
107 static int arc_bio_setstate(struct arc_softc
*, struct bioc_setstate
*);
108 static int arc_bio_volops(struct arc_softc
*, struct bioc_volops
*);
109 static void arc_create_sensors(void *);
110 static void arc_refresh_sensors(struct sysmon_envsys
*, envsys_data_t
*);
111 static int arc_fw_parse_status_code(struct arc_softc
*, uint8_t *);
115 arc_match(device_t parent
, cfdata_t match
, void *aux
)
117 struct pci_attach_args
*pa
= aux
;
119 if (PCI_VENDOR(pa
->pa_id
) == PCI_VENDOR_ARECA
) {
120 switch (PCI_PRODUCT(pa
->pa_id
)) {
121 case PCI_PRODUCT_ARECA_ARC1110
:
122 case PCI_PRODUCT_ARECA_ARC1120
:
123 case PCI_PRODUCT_ARECA_ARC1130
:
124 case PCI_PRODUCT_ARECA_ARC1160
:
125 case PCI_PRODUCT_ARECA_ARC1170
:
126 case PCI_PRODUCT_ARECA_ARC1200
:
127 case PCI_PRODUCT_ARECA_ARC1202
:
128 case PCI_PRODUCT_ARECA_ARC1210
:
129 case PCI_PRODUCT_ARECA_ARC1220
:
130 case PCI_PRODUCT_ARECA_ARC1230
:
131 case PCI_PRODUCT_ARECA_ARC1260
:
132 case PCI_PRODUCT_ARECA_ARC1270
:
133 case PCI_PRODUCT_ARECA_ARC1280
:
134 case PCI_PRODUCT_ARECA_ARC1380
:
135 case PCI_PRODUCT_ARECA_ARC1381
:
136 case PCI_PRODUCT_ARECA_ARC1680
:
137 case PCI_PRODUCT_ARECA_ARC1681
:
148 arc_attach(device_t parent
, device_t self
, void *aux
)
150 struct arc_softc
*sc
= device_private(self
);
151 struct pci_attach_args
*pa
= aux
;
152 struct scsipi_adapter
*adapt
= &sc
->sc_adapter
;
153 struct scsipi_channel
*chan
= &sc
->sc_chan
;
157 rw_init(&sc
->sc_rwlock
);
158 mutex_init(&sc
->sc_mutex
, MUTEX_DEFAULT
, IPL_BIO
);
159 cv_init(&sc
->sc_condvar
, "arcdb");
161 if (arc_map_pci_resources(self
, pa
) != 0) {
162 /* error message printed by arc_map_pci_resources */
166 if (arc_query_firmware(self
) != 0) {
167 /* error message printed by arc_query_firmware */
171 if (arc_alloc_ccbs(self
) != 0) {
172 /* error message printed by arc_alloc_ccbs */
176 if (!pmf_device_register1(self
, NULL
, NULL
, arc_shutdown
))
177 panic("%s: couldn't establish shutdown handler\n",
180 memset(adapt
, 0, sizeof(*adapt
));
181 adapt
->adapt_dev
= self
;
182 adapt
->adapt_nchannels
= 1;
183 adapt
->adapt_openings
= sc
->sc_req_count
/ ARC_MAX_TARGET
;
184 adapt
->adapt_max_periph
= adapt
->adapt_openings
;
185 adapt
->adapt_minphys
= arc_minphys
;
186 adapt
->adapt_request
= arc_scsi_cmd
;
188 memset(chan
, 0, sizeof(*chan
));
189 chan
->chan_adapter
= adapt
;
190 chan
->chan_bustype
= &scsi_bustype
;
191 chan
->chan_nluns
= ARC_MAX_LUN
;
192 chan
->chan_ntargets
= ARC_MAX_TARGET
;
193 chan
->chan_id
= ARC_MAX_TARGET
;
194 chan
->chan_flags
= SCSIPI_CHAN_NOSETTLE
;
197 * Save the device_t returned, because we could to attach
198 * devices via the management interface.
200 sc
->sc_scsibus_dv
= config_found(self
, &sc
->sc_chan
, scsiprint
);
202 /* enable interrupts */
203 arc_write(sc
, ARC_REG_INTRMASK
,
204 ~(ARC_REG_INTRMASK_POSTQUEUE
|ARC_REG_INTRSTAT_DOORBELL
));
208 * Register the driver to bio(4) and setup the sensors.
210 if (bio_register(self
, arc_bioctl
) != 0)
211 panic("%s: bioctl registration failed\n", device_xname(self
));
214 * you need to talk to the firmware to get volume info. our firmware
215 * interface relies on being able to sleep, so we need to use a thread
218 if (kthread_create(PRI_NONE
, KTHREAD_MPSAFE
, NULL
,
219 arc_create_sensors
, sc
, &sc
->sc_lwp
, "arcmsr_sensors") != 0)
220 panic("%s: unable to create a kernel thread for sensors\n",
227 arc_unmap_pci_resources(sc
);
231 arc_detach(device_t self
, int flags
)
233 struct arc_softc
*sc
= device_private(self
);
235 if (arc_msg0(sc
, ARC_REG_INB_MSG0_STOP_BGRB
) != 0)
236 aprint_error_dev(self
, "timeout waiting to stop bg rebuild\n");
238 if (arc_msg0(sc
, ARC_REG_INB_MSG0_FLUSH_CACHE
) != 0)
239 aprint_error_dev(self
, "timeout waiting to flush cache\n");
245 arc_shutdown(device_t self
, int how
)
247 struct arc_softc
*sc
= device_private(self
);
249 if (arc_msg0(sc
, ARC_REG_INB_MSG0_STOP_BGRB
) != 0)
250 aprint_error_dev(self
, "timeout waiting to stop bg rebuild\n");
252 if (arc_msg0(sc
, ARC_REG_INB_MSG0_FLUSH_CACHE
) != 0)
253 aprint_error_dev(self
, "timeout waiting to flush cache\n");
259 arc_minphys(struct buf
*bp
)
261 if (bp
->b_bcount
> MAXPHYS
)
262 bp
->b_bcount
= MAXPHYS
;
269 struct arc_softc
*sc
= arg
;
270 struct arc_ccb
*ccb
= NULL
;
271 char *kva
= ARC_DMA_KVA(sc
->sc_requests
);
272 struct arc_io_cmd
*cmd
;
273 uint32_t reg
, intrstat
;
275 mutex_spin_enter(&sc
->sc_mutex
);
276 intrstat
= arc_read(sc
, ARC_REG_INTRSTAT
);
277 if (intrstat
== 0x0) {
278 mutex_spin_exit(&sc
->sc_mutex
);
282 intrstat
&= ARC_REG_INTRSTAT_POSTQUEUE
| ARC_REG_INTRSTAT_DOORBELL
;
283 arc_write(sc
, ARC_REG_INTRSTAT
, intrstat
);
285 if (intrstat
& ARC_REG_INTRSTAT_DOORBELL
) {
286 if (sc
->sc_talking
) {
287 arc_write(sc
, ARC_REG_INTRMASK
,
288 ~ARC_REG_INTRMASK_POSTQUEUE
);
289 cv_broadcast(&sc
->sc_condvar
);
291 /* otherwise drop it */
292 reg
= arc_read(sc
, ARC_REG_OUTB_DOORBELL
);
293 arc_write(sc
, ARC_REG_OUTB_DOORBELL
, reg
);
294 if (reg
& ARC_REG_OUTB_DOORBELL_WRITE_OK
)
295 arc_write(sc
, ARC_REG_INB_DOORBELL
,
296 ARC_REG_INB_DOORBELL_READ_OK
);
299 mutex_spin_exit(&sc
->sc_mutex
);
301 while ((reg
= arc_pop(sc
)) != 0xffffffff) {
302 cmd
= (struct arc_io_cmd
*)(kva
+
303 ((reg
<< ARC_REG_REPLY_QUEUE_ADDR_SHIFT
) -
304 (uint32_t)ARC_DMA_DVA(sc
->sc_requests
)));
305 ccb
= &sc
->sc_ccbs
[htole32(cmd
->cmd
.context
)];
307 bus_dmamap_sync(sc
->sc_dmat
, ARC_DMA_MAP(sc
->sc_requests
),
308 ccb
->ccb_offset
, ARC_MAX_IOCMDLEN
,
309 BUS_DMASYNC_PREREAD
| BUS_DMASYNC_PREWRITE
);
311 arc_scsi_cmd_done(sc
, ccb
, reg
);
319 arc_scsi_cmd(struct scsipi_channel
*chan
, scsipi_adapter_req_t req
, void *arg
)
321 struct scsipi_periph
*periph
;
322 struct scsipi_xfer
*xs
;
323 struct scsipi_adapter
*adapt
= chan
->chan_adapter
;
324 struct arc_softc
*sc
= device_private(adapt
->adapt_dev
);
326 struct arc_msg_scsicmd
*cmd
;
331 case ADAPTER_REQ_GROW_RESOURCES
:
334 case ADAPTER_REQ_SET_XFER_MODE
:
337 case ADAPTER_REQ_RUN_XFER
:
341 mutex_spin_enter(&sc
->sc_mutex
);
344 periph
= xs
->xs_periph
;
345 target
= periph
->periph_target
;
347 if (xs
->cmdlen
> ARC_MSG_CDBLEN
) {
348 memset(&xs
->sense
, 0, sizeof(xs
->sense
));
349 xs
->sense
.scsi_sense
.response_code
= SSD_RCODE_VALID
| 0x70;
350 xs
->sense
.scsi_sense
.flags
= SKEY_ILLEGAL_REQUEST
;
351 xs
->sense
.scsi_sense
.asc
= 0x20;
352 xs
->error
= XS_SENSE
;
353 xs
->status
= SCSI_CHECK
;
354 mutex_spin_exit(&sc
->sc_mutex
);
359 ccb
= arc_get_ccb(sc
);
361 xs
->error
= XS_RESOURCE_SHORTAGE
;
362 mutex_spin_exit(&sc
->sc_mutex
);
369 if (arc_load_xs(ccb
) != 0) {
370 xs
->error
= XS_DRIVER_STUFFUP
;
371 arc_put_ccb(sc
, ccb
);
372 mutex_spin_exit(&sc
->sc_mutex
);
377 cmd
= &ccb
->ccb_cmd
->cmd
;
378 reg
= ccb
->ccb_cmd_post
;
380 /* bus is always 0 */
381 cmd
->target
= target
;
382 cmd
->lun
= periph
->periph_lun
;
383 cmd
->function
= 1; /* XXX magic number */
385 cmd
->cdb_len
= xs
->cmdlen
;
386 cmd
->sgl_len
= ccb
->ccb_dmamap
->dm_nsegs
;
387 if (xs
->xs_control
& XS_CTL_DATA_OUT
)
388 cmd
->flags
= ARC_MSG_SCSICMD_FLAG_WRITE
;
389 if (ccb
->ccb_dmamap
->dm_nsegs
> ARC_SGL_256LEN
) {
390 cmd
->flags
|= ARC_MSG_SCSICMD_FLAG_SGL_BSIZE_512
;
391 reg
|= ARC_REG_POST_QUEUE_BIGFRAME
;
394 cmd
->context
= htole32(ccb
->ccb_id
);
395 cmd
->data_len
= htole32(xs
->datalen
);
397 memcpy(cmd
->cdb
, xs
->cmd
, xs
->cmdlen
);
399 /* we've built the command, let's put it on the hw */
400 bus_dmamap_sync(sc
->sc_dmat
, ARC_DMA_MAP(sc
->sc_requests
),
401 ccb
->ccb_offset
, ARC_MAX_IOCMDLEN
,
402 BUS_DMASYNC_PREREAD
| BUS_DMASYNC_PREWRITE
);
405 if (xs
->xs_control
& XS_CTL_POLL
) {
406 if (arc_complete(sc
, ccb
, xs
->timeout
) != 0) {
407 xs
->error
= XS_DRIVER_STUFFUP
;
408 mutex_spin_exit(&sc
->sc_mutex
);
414 mutex_spin_exit(&sc
->sc_mutex
);
418 arc_load_xs(struct arc_ccb
*ccb
)
420 struct arc_softc
*sc
= ccb
->ccb_sc
;
421 struct scsipi_xfer
*xs
= ccb
->ccb_xs
;
422 bus_dmamap_t dmap
= ccb
->ccb_dmamap
;
423 struct arc_sge
*sgl
= ccb
->ccb_cmd
->sgl
, *sge
;
427 if (xs
->datalen
== 0)
430 error
= bus_dmamap_load(sc
->sc_dmat
, dmap
,
431 xs
->data
, xs
->datalen
, NULL
,
432 (xs
->xs_control
& XS_CTL_NOSLEEP
) ?
433 BUS_DMA_NOWAIT
: BUS_DMA_WAITOK
);
435 aprint_error("%s: error %d loading dmamap\n",
436 device_xname(sc
->sc_dev
), error
);
440 for (i
= 0; i
< dmap
->dm_nsegs
; i
++) {
443 sge
->sg_hdr
= htole32(ARC_SGE_64BIT
| dmap
->dm_segs
[i
].ds_len
);
444 addr
= dmap
->dm_segs
[i
].ds_addr
;
445 sge
->sg_hi_addr
= htole32((uint32_t)(addr
>> 32));
446 sge
->sg_lo_addr
= htole32((uint32_t)addr
);
449 bus_dmamap_sync(sc
->sc_dmat
, dmap
, 0, dmap
->dm_mapsize
,
450 (xs
->xs_control
& XS_CTL_DATA_IN
) ? BUS_DMASYNC_PREREAD
:
451 BUS_DMASYNC_PREWRITE
);
457 arc_scsi_cmd_done(struct arc_softc
*sc
, struct arc_ccb
*ccb
, uint32_t reg
)
459 struct scsipi_xfer
*xs
= ccb
->ccb_xs
;
460 struct arc_msg_scsicmd
*cmd
;
462 if (xs
->datalen
!= 0) {
463 bus_dmamap_sync(sc
->sc_dmat
, ccb
->ccb_dmamap
, 0,
464 ccb
->ccb_dmamap
->dm_mapsize
,
465 (xs
->xs_control
& XS_CTL_DATA_IN
) ?
466 BUS_DMASYNC_POSTREAD
: BUS_DMASYNC_POSTWRITE
);
467 bus_dmamap_unload(sc
->sc_dmat
, ccb
->ccb_dmamap
);
471 xs
->status
|= XS_STS_DONE
;
473 if (reg
& ARC_REG_REPLY_QUEUE_ERR
) {
474 cmd
= &ccb
->ccb_cmd
->cmd
;
476 switch (cmd
->status
) {
477 case ARC_MSG_STATUS_SELTIMEOUT
:
478 case ARC_MSG_STATUS_ABORTED
:
479 case ARC_MSG_STATUS_INIT_FAIL
:
480 xs
->status
= SCSI_OK
;
481 xs
->error
= XS_SELTIMEOUT
;
485 memset(&xs
->sense
, 0, sizeof(xs
->sense
));
486 memcpy(&xs
->sense
, cmd
->sense_data
,
487 min(ARC_MSG_SENSELEN
, sizeof(xs
->sense
)));
488 xs
->sense
.scsi_sense
.response_code
=
489 SSD_RCODE_VALID
| 0x70;
490 xs
->status
= SCSI_CHECK
;
491 xs
->error
= XS_SENSE
;
496 /* unknown device status */
497 xs
->error
= XS_BUSY
; /* try again later? */
498 xs
->status
= SCSI_BUSY
;
502 xs
->status
= SCSI_OK
;
503 xs
->error
= XS_NOERROR
;
507 arc_put_ccb(sc
, ccb
);
512 arc_complete(struct arc_softc
*sc
, struct arc_ccb
*nccb
, int timeout
)
514 struct arc_ccb
*ccb
= NULL
;
515 char *kva
= ARC_DMA_KVA(sc
->sc_requests
);
516 struct arc_io_cmd
*cmd
;
521 if (reg
== 0xffffffff) {
529 cmd
= (struct arc_io_cmd
*)(kva
+
530 ((reg
<< ARC_REG_REPLY_QUEUE_ADDR_SHIFT
) -
531 ARC_DMA_DVA(sc
->sc_requests
)));
532 ccb
= &sc
->sc_ccbs
[htole32(cmd
->cmd
.context
)];
534 bus_dmamap_sync(sc
->sc_dmat
, ARC_DMA_MAP(sc
->sc_requests
),
535 ccb
->ccb_offset
, ARC_MAX_IOCMDLEN
,
536 BUS_DMASYNC_PREREAD
| BUS_DMASYNC_PREWRITE
);
538 arc_scsi_cmd_done(sc
, ccb
, reg
);
539 } while (nccb
!= ccb
);
545 arc_map_pci_resources(device_t self
, struct pci_attach_args
*pa
)
547 struct arc_softc
*sc
= device_private(self
);
549 pci_intr_handle_t ih
;
551 sc
->sc_pc
= pa
->pa_pc
;
552 sc
->sc_tag
= pa
->pa_tag
;
553 sc
->sc_dmat
= pa
->pa_dmat
;
555 memtype
= pci_mapreg_type(sc
->sc_pc
, sc
->sc_tag
, ARC_PCI_BAR
);
556 if (pci_mapreg_map(pa
, ARC_PCI_BAR
, memtype
, 0, &sc
->sc_iot
,
557 &sc
->sc_ioh
, NULL
, &sc
->sc_ios
) != 0) {
558 aprint_error(": unable to map system interface register\n");
562 if (pci_intr_map(pa
, &ih
) != 0) {
563 aprint_error(": unable to map interrupt\n");
567 sc
->sc_ih
= pci_intr_establish(pa
->pa_pc
, ih
, IPL_BIO
,
569 if (sc
->sc_ih
== NULL
) {
570 aprint_error(": unable to map interrupt [2]\n");
575 aprint_normal_dev(self
, "interrupting at %s\n",
576 pci_intr_string(pa
->pa_pc
, ih
));
581 bus_space_unmap(sc
->sc_iot
, sc
->sc_ioh
, sc
->sc_ios
);
587 arc_unmap_pci_resources(struct arc_softc
*sc
)
589 pci_intr_disestablish(sc
->sc_pc
, sc
->sc_ih
);
590 bus_space_unmap(sc
->sc_iot
, sc
->sc_ioh
, sc
->sc_ios
);
595 arc_query_firmware(device_t self
)
597 struct arc_softc
*sc
= device_private(self
);
598 struct arc_msg_firmware_info fwinfo
;
599 char string
[81]; /* sizeof(vendor)*2+1 */
601 if (arc_wait_eq(sc
, ARC_REG_OUTB_ADDR1
, ARC_REG_OUTB_ADDR1_FIRMWARE_OK
,
602 ARC_REG_OUTB_ADDR1_FIRMWARE_OK
) != 0) {
603 aprint_debug_dev(self
, "timeout waiting for firmware ok\n");
607 if (arc_msg0(sc
, ARC_REG_INB_MSG0_GET_CONFIG
) != 0) {
608 aprint_debug_dev(self
, "timeout waiting for get config\n");
612 if (arc_msg0(sc
, ARC_REG_INB_MSG0_START_BGRB
) != 0) {
613 aprint_debug_dev(self
, "timeout waiting to start bg rebuild\n");
617 arc_read_region(sc
, ARC_REG_MSGBUF
, &fwinfo
, sizeof(fwinfo
));
619 DNPRINTF(ARC_D_INIT
, "%s: signature: 0x%08x\n",
620 device_xname(self
), htole32(fwinfo
.signature
));
622 if (htole32(fwinfo
.signature
) != ARC_FWINFO_SIGNATURE_GET_CONFIG
) {
623 aprint_error_dev(self
, "invalid firmware info from iop\n");
627 DNPRINTF(ARC_D_INIT
, "%s: request_len: %d\n",
628 device_xname(self
), htole32(fwinfo
.request_len
));
629 DNPRINTF(ARC_D_INIT
, "%s: queue_len: %d\n",
630 device_xname(self
), htole32(fwinfo
.queue_len
));
631 DNPRINTF(ARC_D_INIT
, "%s: sdram_size: %d\n",
632 device_xname(self
), htole32(fwinfo
.sdram_size
));
633 DNPRINTF(ARC_D_INIT
, "%s: sata_ports: %d\n",
634 device_xname(self
), htole32(fwinfo
.sata_ports
));
636 scsipi_strvis(string
, 81, fwinfo
.vendor
, sizeof(fwinfo
.vendor
));
637 DNPRINTF(ARC_D_INIT
, "%s: vendor: \"%s\"\n",
638 device_xname(self
), string
);
640 scsipi_strvis(string
, 17, fwinfo
.model
, sizeof(fwinfo
.model
));
641 aprint_normal_dev(self
, "Areca %s Host Adapter RAID controller\n",
644 scsipi_strvis(string
, 33, fwinfo
.fw_version
, sizeof(fwinfo
.fw_version
));
645 DNPRINTF(ARC_D_INIT
, "%s: version: \"%s\"\n",
646 device_xname(self
), string
);
648 aprint_normal_dev(self
, "%d ports, %dMB SDRAM, firmware <%s>\n",
649 htole32(fwinfo
.sata_ports
), htole32(fwinfo
.sdram_size
), string
);
651 if (htole32(fwinfo
.request_len
) != ARC_MAX_IOCMDLEN
) {
652 aprint_error_dev(self
,
653 "unexpected request frame size (%d != %d)\n",
654 htole32(fwinfo
.request_len
), ARC_MAX_IOCMDLEN
);
658 sc
->sc_req_count
= htole32(fwinfo
.queue_len
);
665 arc_bioctl(device_t self
, u_long cmd
, void *addr
)
667 struct arc_softc
*sc
= device_private(self
);
672 error
= arc_bio_inq(sc
, (struct bioc_inq
*)addr
);
676 error
= arc_bio_vol(sc
, (struct bioc_vol
*)addr
);
680 error
= arc_bio_disk_volume(sc
, (struct bioc_disk
*)addr
);
684 error
= arc_bio_disk_novol(sc
, (struct bioc_disk
*)addr
);
688 error
= arc_bio_alarm(sc
, (struct bioc_alarm
*)addr
);
692 error
= arc_bio_setstate(sc
, (struct bioc_setstate
*)addr
);
696 error
= arc_bio_volops(sc
, (struct bioc_volops
*)addr
);
708 arc_fw_parse_status_code(struct arc_softc
*sc
, uint8_t *reply
)
711 case ARC_FW_CMD_RAIDINVAL
:
712 printf("%s: firmware error (invalid raid set)\n",
713 device_xname(sc
->sc_dev
));
715 case ARC_FW_CMD_VOLINVAL
:
716 printf("%s: firmware error (invalid volume set)\n",
717 device_xname(sc
->sc_dev
));
719 case ARC_FW_CMD_NORAID
:
720 printf("%s: firmware error (unexistent raid set)\n",
721 device_xname(sc
->sc_dev
));
723 case ARC_FW_CMD_NOVOLUME
:
724 printf("%s: firmware error (unexistent volume set)\n",
725 device_xname(sc
->sc_dev
));
727 case ARC_FW_CMD_NOPHYSDRV
:
728 printf("%s: firmware error (unexistent physical drive)\n",
729 device_xname(sc
->sc_dev
));
731 case ARC_FW_CMD_PARAM_ERR
:
732 printf("%s: firmware error (parameter error)\n",
733 device_xname(sc
->sc_dev
));
735 case ARC_FW_CMD_UNSUPPORTED
:
736 printf("%s: firmware error (unsupported command)\n",
737 device_xname(sc
->sc_dev
));
739 case ARC_FW_CMD_DISKCFG_CHGD
:
740 printf("%s: firmware error (disk configuration changed)\n",
741 device_xname(sc
->sc_dev
));
743 case ARC_FW_CMD_PASS_INVAL
:
744 printf("%s: firmware error (invalid password)\n",
745 device_xname(sc
->sc_dev
));
747 case ARC_FW_CMD_NODISKSPACE
:
748 printf("%s: firmware error (no disk space available)\n",
749 device_xname(sc
->sc_dev
));
751 case ARC_FW_CMD_CHECKSUM_ERR
:
752 printf("%s: firmware error (checksum error)\n",
753 device_xname(sc
->sc_dev
));
755 case ARC_FW_CMD_PASS_REQD
:
756 printf("%s: firmware error (password required)\n",
757 device_xname(sc
->sc_dev
));
766 arc_bio_alarm(struct arc_softc
*sc
, struct bioc_alarm
*ba
)
768 uint8_t request
[2], reply
[1];
772 switch (ba
->ba_opcode
) {
775 request
[0] = ARC_FW_SET_ALARM
;
776 request
[1] = (ba
->ba_opcode
== BIOC_SAENABLE
) ?
777 ARC_FW_SET_ALARM_ENABLE
: ARC_FW_SET_ALARM_DISABLE
;
778 len
= sizeof(request
);
783 request
[0] = ARC_FW_MUTE_ALARM
;
789 /* system info is too big/ugly to deal with here */
790 return arc_bio_alarm_state(sc
, ba
);
796 error
= arc_msgbuf(sc
, request
, len
, reply
, sizeof(reply
));
800 return arc_fw_parse_status_code(sc
, &reply
[0]);
804 arc_bio_alarm_state(struct arc_softc
*sc
, struct bioc_alarm
*ba
)
806 struct arc_fw_sysinfo
*sysinfo
;
810 sysinfo
= kmem_zalloc(sizeof(*sysinfo
), KM_SLEEP
);
812 request
= ARC_FW_SYSINFO
;
813 error
= arc_msgbuf(sc
, &request
, sizeof(request
),
814 sysinfo
, sizeof(struct arc_fw_sysinfo
));
819 ba
->ba_status
= sysinfo
->alarm
;
822 kmem_free(sysinfo
, sizeof(*sysinfo
));
827 arc_bio_volops(struct arc_softc
*sc
, struct bioc_volops
*bc
)
829 /* to create a raid set */
830 struct req_craidset
{
833 uint8_t raidset_name
[16];
836 /* to create a volume set */
840 uint8_t volset_name
[16];
853 struct scsibus_softc
*scsibus_sc
= NULL
;
854 struct req_craidset req_craidset
;
855 struct req_cvolset req_cvolset
;
860 switch (bc
->bc_opcode
) {
861 case BIOC_VCREATE_VOLUME
:
864 * Zero out the structs so that we use some defaults
865 * in raid and volume sets.
867 memset(&req_craidset
, 0, sizeof(req_craidset
));
868 memset(&req_cvolset
, 0, sizeof(req_cvolset
));
871 * Firstly we have to create the raid set and
872 * use the default name for all them.
874 req_craidset
.cmdcode
= ARC_FW_CREATE_RAIDSET
;
875 req_craidset
.devmask
= bc
->bc_devmask
;
876 error
= arc_msgbuf(sc
, &req_craidset
, sizeof(req_craidset
),
877 reply
, sizeof(reply
));
881 error
= arc_fw_parse_status_code(sc
, &reply
[0]);
883 printf("%s: create raidset%d failed\n",
884 device_xname(sc
->sc_dev
), bc
->bc_volid
);
889 * At this point the raid set was created, so it's
890 * time to create the volume set.
892 req_cvolset
.cmdcode
= ARC_FW_CREATE_VOLUME
;
893 req_cvolset
.raidset
= bc
->bc_volid
;
894 req_cvolset
.capacity
= bc
->bc_size
* ARC_BLOCKSIZE
;
897 * Set the RAID level.
899 switch (bc
->bc_level
) {
902 req_cvolset
.raidlevel
= bc
->bc_level
;
904 case BIOC_SVOL_RAID10
:
905 req_cvolset
.raidlevel
= 1;
908 req_cvolset
.raidlevel
= ARC_FW_VOL_RAIDLEVEL_3
;
911 req_cvolset
.raidlevel
= ARC_FW_VOL_RAIDLEVEL_5
;
914 req_cvolset
.raidlevel
= ARC_FW_VOL_RAIDLEVEL_6
;
921 * Set the stripe size.
923 switch (bc
->bc_stripe
) {
925 req_cvolset
.stripe
= 0;
928 req_cvolset
.stripe
= 1;
931 req_cvolset
.stripe
= 2;
934 req_cvolset
.stripe
= 3;
937 req_cvolset
.stripe
= 4;
940 req_cvolset
.stripe
= 5;
943 req_cvolset
.stripe
= 4; /* by default 64K */
947 req_cvolset
.scsi_chan
= bc
->bc_channel
;
948 req_cvolset
.scsi_target
= bc
->bc_target
;
949 req_cvolset
.scsi_lun
= bc
->bc_lun
;
950 req_cvolset
.tagqueue
= 1; /* always enabled */
951 req_cvolset
.cache
= 1; /* always enabled */
952 req_cvolset
.speed
= 4; /* always max speed */
954 /* RAID 1 and 1+0 levels need foreground initialization */
955 if (bc
->bc_level
== 1 || bc
->bc_level
== BIOC_SVOL_RAID10
)
956 req_cvolset
.quick_init
= 1; /* foreground init */
958 error
= arc_msgbuf(sc
, &req_cvolset
, sizeof(req_cvolset
),
959 reply
, sizeof(reply
));
963 error
= arc_fw_parse_status_code(sc
, &reply
[0]);
965 printf("%s: create volumeset%d failed\n",
966 device_xname(sc
->sc_dev
), bc
->bc_volid
);
971 * If we are creating a RAID 1 or RAID 1+0 volume,
972 * the volume will be created immediately but it won't
973 * be available until the initialization is done... so
974 * don't bother attaching the sd(4) device.
976 if (bc
->bc_level
== 1 || bc
->bc_level
== BIOC_SVOL_RAID10
)
980 * Do a rescan on the bus to attach the device associated
981 * with the new volume.
983 scsibus_sc
= device_private(sc
->sc_scsibus_dv
);
984 (void)scsi_probe_bus(scsibus_sc
, bc
->bc_target
, bc
->bc_lun
);
988 case BIOC_VREMOVE_VOLUME
:
991 * Remove the volume set specified in bc_volid.
993 request
[0] = ARC_FW_DELETE_VOLUME
;
994 request
[1] = bc
->bc_volid
;
995 error
= arc_msgbuf(sc
, request
, sizeof(request
),
996 reply
, sizeof(reply
));
1000 error
= arc_fw_parse_status_code(sc
, &reply
[0]);
1002 printf("%s: delete volumeset%d failed\n",
1003 device_xname(sc
->sc_dev
), bc
->bc_volid
);
1008 * Detach the sd(4) device associated with the volume,
1009 * but if there's an error don't make it a priority.
1011 error
= scsipi_target_detach(&sc
->sc_chan
, bc
->bc_target
,
1014 printf("%s: couldn't detach sd device for volume %d "
1015 "at %u:%u.%u (error=%d)\n",
1016 device_xname(sc
->sc_dev
), bc
->bc_volid
,
1017 bc
->bc_channel
, bc
->bc_target
, bc
->bc_lun
, error
);
1020 * and remove the raid set specified in bc_volid,
1021 * we only care about volumes.
1023 request
[0] = ARC_FW_DELETE_RAIDSET
;
1024 request
[1] = bc
->bc_volid
;
1025 error
= arc_msgbuf(sc
, request
, sizeof(request
),
1026 reply
, sizeof(reply
));
1030 error
= arc_fw_parse_status_code(sc
, &reply
[0]);
1032 printf("%s: delete raidset%d failed\n",
1033 device_xname(sc
->sc_dev
), bc
->bc_volid
);
1047 arc_bio_setstate(struct arc_softc
*sc
, struct bioc_setstate
*bs
)
1049 /* for a hotspare disk */
1055 /* for a pass-through disk */
1062 uint8_t tagged_queue
;
1067 struct scsibus_softc
*scsibus_sc
= NULL
;
1068 struct request_hs req_hs
; /* to add/remove hotspare */
1069 struct request_pt req_pt
; /* to add a pass-through */
1074 switch (bs
->bs_status
) {
1075 case BIOC_SSHOTSPARE
:
1077 req_hs
.cmdcode
= ARC_FW_CREATE_HOTSPARE
;
1078 req_hs
.devmask
= (1 << bs
->bs_target
);
1081 case BIOC_SSDELHOTSPARE
:
1083 req_hs
.cmdcode
= ARC_FW_DELETE_HOTSPARE
;
1084 req_hs
.devmask
= (1 << bs
->bs_target
);
1087 case BIOC_SSPASSTHRU
:
1089 req_pt
.cmdcode
= ARC_FW_CREATE_PASSTHRU
;
1090 req_pt
.devid
= bs
->bs_other_id
; /* this wants device# */
1091 req_pt
.scsi_chan
= bs
->bs_channel
;
1092 req_pt
.scsi_id
= bs
->bs_target
;
1093 req_pt
.scsi_lun
= bs
->bs_lun
;
1094 req_pt
.tagged_queue
= 1; /* always enabled */
1095 req_pt
.cache_mode
= 1; /* always enabled */
1096 req_pt
.max_speed
= 4; /* always max speed */
1098 error
= arc_msgbuf(sc
, &req_pt
, sizeof(req_pt
),
1099 reply
, sizeof(reply
));
1104 * Do a rescan on the bus to attach the new device
1105 * associated with the pass-through disk.
1107 scsibus_sc
= device_private(sc
->sc_scsibus_dv
);
1108 (void)scsi_probe_bus(scsibus_sc
, bs
->bs_target
, bs
->bs_lun
);
1112 case BIOC_SSDELPASSTHRU
:
1114 req_gen
[0] = ARC_FW_DELETE_PASSTHRU
;
1115 req_gen
[1] = bs
->bs_target
;
1116 error
= arc_msgbuf(sc
, &req_gen
, sizeof(req_gen
),
1117 reply
, sizeof(reply
));
1122 * Detach the sd device associated with this pass-through disk.
1124 error
= scsipi_target_detach(&sc
->sc_chan
, bs
->bs_target
,
1127 printf("%s: couldn't detach sd device for the "
1128 "pass-through disk at %u:%u.%u (error=%d)\n",
1129 device_xname(sc
->sc_dev
),
1130 bs
->bs_channel
, bs
->bs_target
, bs
->bs_lun
, error
);
1134 case BIOC_SSCHECKSTART_VOL
:
1136 req_gen
[0] = ARC_FW_START_CHECKVOL
;
1137 req_gen
[1] = bs
->bs_volid
;
1138 error
= arc_msgbuf(sc
, &req_gen
, sizeof(req_gen
),
1139 reply
, sizeof(reply
));
1145 case BIOC_SSCHECKSTOP_VOL
:
1147 uint8_t req
= ARC_FW_STOP_CHECKVOL
;
1148 error
= arc_msgbuf(sc
, &req
, 1, reply
, sizeof(reply
));
1159 error
= arc_msgbuf(sc
, &req_hs
, sizeof(req_hs
),
1160 reply
, sizeof(reply
));
1165 return arc_fw_parse_status_code(sc
, &reply
[0]);
1169 arc_bio_inq(struct arc_softc
*sc
, struct bioc_inq
*bi
)
1172 struct arc_fw_sysinfo
*sysinfo
= NULL
;
1173 struct arc_fw_raidinfo
*raidinfo
;
1177 raidinfo
= kmem_zalloc(sizeof(*raidinfo
), KM_SLEEP
);
1179 if (!sc
->sc_maxraidset
|| !sc
->sc_maxvolset
|| !sc
->sc_cchans
) {
1180 sysinfo
= kmem_zalloc(sizeof(*sysinfo
), KM_SLEEP
);
1182 request
[0] = ARC_FW_SYSINFO
;
1183 error
= arc_msgbuf(sc
, request
, 1, sysinfo
,
1184 sizeof(struct arc_fw_sysinfo
));
1188 sc
->sc_maxraidset
= sysinfo
->max_raid_set
;
1189 sc
->sc_maxvolset
= sysinfo
->max_volume_set
;
1190 sc
->sc_cchans
= sysinfo
->ide_channels
;
1193 request
[0] = ARC_FW_RAIDINFO
;
1194 for (i
= 0; i
< sc
->sc_maxraidset
; i
++) {
1196 error
= arc_msgbuf(sc
, request
, sizeof(request
), raidinfo
,
1197 sizeof(struct arc_fw_raidinfo
));
1201 nvols
+= raidinfo
->volumes
;
1204 strlcpy(bi
->bi_dev
, device_xname(sc
->sc_dev
), sizeof(bi
->bi_dev
));
1205 bi
->bi_novol
= nvols
;
1206 bi
->bi_nodisk
= sc
->sc_cchans
;
1210 kmem_free(sysinfo
, sizeof(*sysinfo
));
1211 kmem_free(raidinfo
, sizeof(*raidinfo
));
1216 arc_bio_getvol(struct arc_softc
*sc
, int vol
, struct arc_fw_volinfo
*volinfo
)
1222 request
[0] = ARC_FW_VOLINFO
;
1223 for (i
= 0; i
< sc
->sc_maxvolset
; i
++) {
1225 error
= arc_msgbuf(sc
, request
, sizeof(request
), volinfo
,
1226 sizeof(struct arc_fw_volinfo
));
1230 if (volinfo
->capacity
== 0 && volinfo
->capacity2
== 0)
1240 (volinfo
->capacity
== 0 && volinfo
->capacity2
== 0)) {
1250 arc_bio_vol(struct arc_softc
*sc
, struct bioc_vol
*bv
)
1252 struct arc_fw_volinfo
*volinfo
;
1257 volinfo
= kmem_zalloc(sizeof(*volinfo
), KM_SLEEP
);
1259 error
= arc_bio_getvol(sc
, bv
->bv_volid
, volinfo
);
1263 bv
->bv_percent
= -1;
1266 status
= htole32(volinfo
->volume_status
);
1267 if (status
== 0x0) {
1268 if (htole32(volinfo
->fail_mask
) == 0x0)
1269 bv
->bv_status
= BIOC_SVONLINE
;
1271 bv
->bv_status
= BIOC_SVDEGRADED
;
1272 } else if (status
& ARC_FW_VOL_STATUS_NEED_REGEN
) {
1273 bv
->bv_status
= BIOC_SVDEGRADED
;
1274 } else if (status
& ARC_FW_VOL_STATUS_FAILED
) {
1275 bv
->bv_status
= BIOC_SVOFFLINE
;
1276 } else if (status
& ARC_FW_VOL_STATUS_INITTING
) {
1277 bv
->bv_status
= BIOC_SVBUILDING
;
1278 bv
->bv_percent
= htole32(volinfo
->progress
);
1279 } else if (status
& ARC_FW_VOL_STATUS_REBUILDING
) {
1280 bv
->bv_status
= BIOC_SVREBUILD
;
1281 bv
->bv_percent
= htole32(volinfo
->progress
);
1282 } else if (status
& ARC_FW_VOL_STATUS_MIGRATING
) {
1283 bv
->bv_status
= BIOC_SVMIGRATING
;
1284 bv
->bv_percent
= htole32(volinfo
->progress
);
1285 } else if (status
& ARC_FW_VOL_STATUS_CHECKING
) {
1286 bv
->bv_status
= BIOC_SVCHECKING
;
1287 bv
->bv_percent
= htole32(volinfo
->progress
);
1288 } else if (status
& ARC_FW_VOL_STATUS_NEED_INIT
) {
1289 bv
->bv_status
= BIOC_SVOFFLINE
;
1291 printf("%s: volume %d status 0x%x\n",
1292 device_xname(sc
->sc_dev
), bv
->bv_volid
, status
);
1295 blocks
= (uint64_t)htole32(volinfo
->capacity2
) << 32;
1296 blocks
+= (uint64_t)htole32(volinfo
->capacity
);
1297 bv
->bv_size
= blocks
* ARC_BLOCKSIZE
; /* XXX */
1299 switch (volinfo
->raid_level
) {
1300 case ARC_FW_VOL_RAIDLEVEL_0
:
1303 case ARC_FW_VOL_RAIDLEVEL_1
:
1304 if (volinfo
->member_disks
> 2)
1305 bv
->bv_level
= BIOC_SVOL_RAID10
;
1309 case ARC_FW_VOL_RAIDLEVEL_3
:
1312 case ARC_FW_VOL_RAIDLEVEL_5
:
1315 case ARC_FW_VOL_RAIDLEVEL_6
:
1318 case ARC_FW_VOL_RAIDLEVEL_PASSTHRU
:
1319 bv
->bv_level
= BIOC_SVOL_PASSTHRU
;
1326 bv
->bv_nodisk
= volinfo
->member_disks
;
1327 bv
->bv_stripe_size
= volinfo
->stripe_size
/ 2;
1328 snprintf(bv
->bv_dev
, sizeof(bv
->bv_dev
), "sd%d", bv
->bv_volid
);
1329 scsipi_strvis(bv
->bv_vendor
, sizeof(bv
->bv_vendor
), volinfo
->set_name
,
1330 sizeof(volinfo
->set_name
));
1333 kmem_free(volinfo
, sizeof(*volinfo
));
1338 arc_bio_disk_novol(struct arc_softc
*sc
, struct bioc_disk
*bd
)
1340 struct arc_fw_diskinfo
*diskinfo
;
1344 diskinfo
= kmem_zalloc(sizeof(*diskinfo
), KM_SLEEP
);
1346 if (bd
->bd_diskid
>= sc
->sc_cchans
) {
1351 request
[0] = ARC_FW_DISKINFO
;
1352 request
[1] = bd
->bd_diskid
;
1353 error
= arc_msgbuf(sc
, request
, sizeof(request
),
1354 diskinfo
, sizeof(struct arc_fw_diskinfo
));
1358 /* skip disks with no capacity */
1359 if (htole32(diskinfo
->capacity
) == 0 &&
1360 htole32(diskinfo
->capacity2
) == 0)
1363 bd
->bd_disknovol
= true;
1364 arc_bio_disk_filldata(sc
, bd
, diskinfo
, bd
->bd_diskid
);
1367 kmem_free(diskinfo
, sizeof(*diskinfo
));
1372 arc_bio_disk_filldata(struct arc_softc
*sc
, struct bioc_disk
*bd
,
1373 struct arc_fw_diskinfo
*diskinfo
, int diskid
)
1380 /* Ignore bit zero for now, we don't know what it means */
1381 diskinfo
->device_state
&= ~0x1;
1383 switch (diskinfo
->device_state
) {
1384 case ARC_FW_DISK_FAILED
:
1385 bd
->bd_status
= BIOC_SDFAILED
;
1387 case ARC_FW_DISK_PASSTHRU
:
1388 bd
->bd_status
= BIOC_SDPASSTHRU
;
1390 case ARC_FW_DISK_NORMAL
:
1391 bd
->bd_status
= BIOC_SDONLINE
;
1393 case ARC_FW_DISK_HOTSPARE
:
1394 bd
->bd_status
= BIOC_SDHOTSPARE
;
1396 case ARC_FW_DISK_UNUSED
:
1397 bd
->bd_status
= BIOC_SDUNUSED
;
1400 /* disk has been disconnected */
1401 bd
->bd_status
= BIOC_SDOFFLINE
;
1405 strlcpy(bd
->bd_vendor
, "disk missing", sizeof(bd
->bd_vendor
));
1408 printf("%s: unknown disk device_state: 0x%x\n", __func__
,
1409 diskinfo
->device_state
);
1410 bd
->bd_status
= BIOC_SDINVALID
;
1414 blocks
= (uint64_t)htole32(diskinfo
->capacity2
) << 32;
1415 blocks
+= (uint64_t)htole32(diskinfo
->capacity
);
1416 bd
->bd_size
= blocks
* ARC_BLOCKSIZE
; /* XXX */
1418 scsipi_strvis(model
, 81, diskinfo
->model
, sizeof(diskinfo
->model
));
1419 scsipi_strvis(serial
, 41, diskinfo
->serial
, sizeof(diskinfo
->serial
));
1420 scsipi_strvis(rev
, 17, diskinfo
->firmware_rev
,
1421 sizeof(diskinfo
->firmware_rev
));
1423 snprintf(bd
->bd_vendor
, sizeof(bd
->bd_vendor
), "%s %s", model
, rev
);
1424 strlcpy(bd
->bd_serial
, serial
, sizeof(bd
->bd_serial
));
1427 bd
->bd_channel
= diskinfo
->scsi_attr
.channel
;
1428 bd
->bd_target
= diskinfo
->scsi_attr
.target
;
1429 bd
->bd_lun
= diskinfo
->scsi_attr
.lun
;
1433 * the firwmare doesnt seem to fill scsi_attr in, so fake it with
1437 bd
->bd_target
= diskid
;
1442 arc_bio_disk_volume(struct arc_softc
*sc
, struct bioc_disk
*bd
)
1444 struct arc_fw_raidinfo
*raidinfo
;
1445 struct arc_fw_volinfo
*volinfo
;
1446 struct arc_fw_diskinfo
*diskinfo
;
1450 volinfo
= kmem_zalloc(sizeof(*volinfo
), KM_SLEEP
);
1451 raidinfo
= kmem_zalloc(sizeof(*raidinfo
), KM_SLEEP
);
1452 diskinfo
= kmem_zalloc(sizeof(*diskinfo
), KM_SLEEP
);
1454 error
= arc_bio_getvol(sc
, bd
->bd_volid
, volinfo
);
1458 request
[0] = ARC_FW_RAIDINFO
;
1459 request
[1] = volinfo
->raid_set_number
;
1461 error
= arc_msgbuf(sc
, request
, sizeof(request
), raidinfo
,
1462 sizeof(struct arc_fw_raidinfo
));
1466 if (bd
->bd_diskid
>= sc
->sc_cchans
||
1467 bd
->bd_diskid
>= raidinfo
->member_devices
) {
1472 if (raidinfo
->device_array
[bd
->bd_diskid
] == 0xff) {
1474 * The disk has been disconnected, mark it offline
1475 * and put it on another bus.
1480 bd
->bd_status
= BIOC_SDOFFLINE
;
1481 strlcpy(bd
->bd_vendor
, "disk missing", sizeof(bd
->bd_vendor
));
1485 request
[0] = ARC_FW_DISKINFO
;
1486 request
[1] = raidinfo
->device_array
[bd
->bd_diskid
];
1487 error
= arc_msgbuf(sc
, request
, sizeof(request
), diskinfo
,
1488 sizeof(struct arc_fw_diskinfo
));
1492 /* now fill our bio disk with data from the firmware */
1493 arc_bio_disk_filldata(sc
, bd
, diskinfo
,
1494 raidinfo
->device_array
[bd
->bd_diskid
]);
1497 kmem_free(raidinfo
, sizeof(*raidinfo
));
1498 kmem_free(volinfo
, sizeof(*volinfo
));
1499 kmem_free(diskinfo
, sizeof(*diskinfo
));
1502 #endif /* NBIO > 0 */
1505 arc_msg_cksum(void *cmd
, uint16_t len
)
1511 cksum
= (uint8_t)(len
>> 8) + (uint8_t)len
;
1512 for (i
= 0; i
< len
; i
++)
1520 arc_msgbuf(struct arc_softc
*sc
, void *wptr
, size_t wbuflen
, void *rptr
,
1523 uint8_t rwbuf
[ARC_REG_IOC_RWBUF_MAXLEN
];
1524 uint8_t *wbuf
, *rbuf
;
1525 int wlen
, wdone
= 0, rlen
, rdone
= 0;
1526 struct arc_fw_bufhdr
*bufhdr
;
1527 uint32_t reg
, rwlen
;
1535 DNPRINTF(ARC_D_DB
, "%s: arc_msgbuf wbuflen: %d rbuflen: %d\n",
1536 device_xname(sc
->sc_dev
), wbuflen
, rbuflen
);
1538 wlen
= sizeof(struct arc_fw_bufhdr
) + wbuflen
+ 1; /* 1 for cksum */
1539 wbuf
= kmem_alloc(wlen
, KM_SLEEP
);
1541 rlen
= sizeof(struct arc_fw_bufhdr
) + rbuflen
+ 1; /* 1 for cksum */
1542 rbuf
= kmem_alloc(rlen
, KM_SLEEP
);
1544 DNPRINTF(ARC_D_DB
, "%s: arc_msgbuf wlen: %d rlen: %d\n",
1545 device_xname(sc
->sc_dev
), wlen
, rlen
);
1547 bufhdr
= (struct arc_fw_bufhdr
*)wbuf
;
1548 bufhdr
->hdr
= arc_fw_hdr
;
1549 bufhdr
->len
= htole16(wbuflen
);
1550 memcpy(wbuf
+ sizeof(struct arc_fw_bufhdr
), wptr
, wbuflen
);
1551 wbuf
[wlen
- 1] = arc_msg_cksum(wptr
, wbuflen
);
1554 if (arc_read(sc
, ARC_REG_OUTB_DOORBELL
) != 0) {
1559 reg
= ARC_REG_OUTB_DOORBELL_READ_OK
;
1562 if ((reg
& ARC_REG_OUTB_DOORBELL_READ_OK
) && wdone
< wlen
) {
1563 memset(rwbuf
, 0, sizeof(rwbuf
));
1564 rwlen
= (wlen
- wdone
) % sizeof(rwbuf
);
1565 memcpy(rwbuf
, &wbuf
[wdone
], rwlen
);
1568 if (arcdebug
& ARC_D_DB
) {
1569 printf("%s: write %d:",
1570 device_xname(sc
->sc_dev
), rwlen
);
1571 for (i
= 0; i
< rwlen
; i
++)
1572 printf(" 0x%02x", rwbuf
[i
]);
1577 /* copy the chunk to the hw */
1578 arc_write(sc
, ARC_REG_IOC_WBUF_LEN
, rwlen
);
1579 arc_write_region(sc
, ARC_REG_IOC_WBUF
, rwbuf
,
1582 /* say we have a buffer for the hw */
1583 arc_write(sc
, ARC_REG_INB_DOORBELL
,
1584 ARC_REG_INB_DOORBELL_WRITE_OK
);
1589 while ((reg
= arc_read(sc
, ARC_REG_OUTB_DOORBELL
)) == 0)
1592 arc_write(sc
, ARC_REG_OUTB_DOORBELL
, reg
);
1594 DNPRINTF(ARC_D_DB
, "%s: reg: 0x%08x\n",
1595 device_xname(sc
->sc_dev
), reg
);
1597 if ((reg
& ARC_REG_OUTB_DOORBELL_WRITE_OK
) && rdone
< rlen
) {
1598 rwlen
= arc_read(sc
, ARC_REG_IOC_RBUF_LEN
);
1599 if (rwlen
> sizeof(rwbuf
)) {
1600 DNPRINTF(ARC_D_DB
, "%s: rwlen too big\n",
1601 device_xname(sc
->sc_dev
));
1606 arc_read_region(sc
, ARC_REG_IOC_RBUF
, rwbuf
,
1609 arc_write(sc
, ARC_REG_INB_DOORBELL
,
1610 ARC_REG_INB_DOORBELL_READ_OK
);
1613 printf("%s: len: %d+%d=%d/%d\n",
1614 device_xname(sc
->sc_dev
),
1615 rwlen
, rdone
, rwlen
+ rdone
, rlen
);
1616 if (arcdebug
& ARC_D_DB
) {
1618 device_xname(sc
->sc_dev
));
1619 for (i
= 0; i
< rwlen
; i
++)
1620 printf(" 0x%02x", rwbuf
[i
]);
1625 if ((rdone
+ rwlen
) > rlen
) {
1626 DNPRINTF(ARC_D_DB
, "%s: rwbuf too big\n",
1627 device_xname(sc
->sc_dev
));
1632 memcpy(&rbuf
[rdone
], rwbuf
, rwlen
);
1635 } while (rdone
!= rlen
);
1637 bufhdr
= (struct arc_fw_bufhdr
*)rbuf
;
1638 if (memcmp(&bufhdr
->hdr
, &arc_fw_hdr
, sizeof(bufhdr
->hdr
)) != 0 ||
1639 bufhdr
->len
!= htole16(rbuflen
)) {
1640 DNPRINTF(ARC_D_DB
, "%s: rbuf hdr is wrong\n",
1641 device_xname(sc
->sc_dev
));
1646 memcpy(rptr
, rbuf
+ sizeof(struct arc_fw_bufhdr
), rbuflen
);
1648 if (rbuf
[rlen
- 1] != arc_msg_cksum(rptr
, rbuflen
)) {
1649 DNPRINTF(ARC_D_DB
, "%s: invalid cksum\n",
1650 device_xname(sc
->sc_dev
));
1657 kmem_free(wbuf
, wlen
);
1658 kmem_free(rbuf
, rlen
);
1664 arc_lock(struct arc_softc
*sc
)
1666 rw_enter(&sc
->sc_rwlock
, RW_WRITER
);
1667 mutex_spin_enter(&sc
->sc_mutex
);
1668 arc_write(sc
, ARC_REG_INTRMASK
, ~ARC_REG_INTRMASK_POSTQUEUE
);
1673 arc_unlock(struct arc_softc
*sc
)
1675 KASSERT(mutex_owned(&sc
->sc_mutex
));
1677 arc_write(sc
, ARC_REG_INTRMASK
,
1678 ~(ARC_REG_INTRMASK_POSTQUEUE
|ARC_REG_INTRMASK_DOORBELL
));
1680 mutex_spin_exit(&sc
->sc_mutex
);
1681 rw_exit(&sc
->sc_rwlock
);
1685 arc_wait(struct arc_softc
*sc
)
1687 KASSERT(mutex_owned(&sc
->sc_mutex
));
1689 arc_write(sc
, ARC_REG_INTRMASK
,
1690 ~(ARC_REG_INTRMASK_POSTQUEUE
|ARC_REG_INTRMASK_DOORBELL
));
1691 if (cv_timedwait(&sc
->sc_condvar
, &sc
->sc_mutex
, hz
) == EWOULDBLOCK
)
1692 arc_write(sc
, ARC_REG_INTRMASK
, ~ARC_REG_INTRMASK_POSTQUEUE
);
1697 arc_create_sensors(void *arg
)
1699 struct arc_softc
*sc
= arg
;
1703 size_t slen
, count
= 0;
1705 memset(&bi
, 0, sizeof(bi
));
1706 if (arc_bio_inq(sc
, &bi
) != 0) {
1707 aprint_error("%s: unable to query firmware for sensor info\n",
1708 device_xname(sc
->sc_dev
));
1712 /* There's no point to continue if there are no volumes */
1716 for (i
= 0; i
< bi
.bi_novol
; i
++) {
1717 memset(&bv
, 0, sizeof(bv
));
1719 if (arc_bio_vol(sc
, &bv
) != 0)
1722 /* Skip passthrough volumes */
1723 if (bv
.bv_level
== BIOC_SVOL_PASSTHRU
)
1726 /* new volume found */
1728 /* new disk in a volume found */
1729 sc
->sc_nsensors
+= bv
.bv_nodisk
;
1732 /* No valid volumes */
1733 if (!sc
->sc_nsensors
)
1736 sc
->sc_sme
= sysmon_envsys_create();
1737 slen
= sizeof(envsys_data_t
) * sc
->sc_nsensors
;
1738 sc
->sc_sensors
= kmem_zalloc(slen
, KM_SLEEP
);
1740 /* Attach sensors for volumes and disks */
1741 for (i
= 0; i
< bi
.bi_novol
; i
++) {
1742 memset(&bv
, 0, sizeof(bv
));
1744 if (arc_bio_vol(sc
, &bv
) != 0)
1747 sc
->sc_sensors
[count
].units
= ENVSYS_DRIVE
;
1748 sc
->sc_sensors
[count
].monitor
= true;
1749 sc
->sc_sensors
[count
].flags
= ENVSYS_FMONSTCHANGED
;
1751 /* Skip passthrough volumes */
1752 if (bv
.bv_level
== BIOC_SVOL_PASSTHRU
)
1755 if (bv
.bv_level
== BIOC_SVOL_RAID10
)
1756 snprintf(sc
->sc_sensors
[count
].desc
,
1757 sizeof(sc
->sc_sensors
[count
].desc
),
1758 "RAID 1+0 volume%d (%s)", i
, bv
.bv_dev
);
1760 snprintf(sc
->sc_sensors
[count
].desc
,
1761 sizeof(sc
->sc_sensors
[count
].desc
),
1762 "RAID %d volume%d (%s)", bv
.bv_level
, i
,
1765 sc
->sc_sensors
[count
].value_max
= i
;
1767 if (sysmon_envsys_sensor_attach(sc
->sc_sme
,
1768 &sc
->sc_sensors
[count
]))
1773 /* Attach disk sensors for this volume */
1774 for (j
= 0; j
< bv
.bv_nodisk
; j
++) {
1775 sc
->sc_sensors
[count
].units
= ENVSYS_DRIVE
;
1776 sc
->sc_sensors
[count
].monitor
= true;
1777 sc
->sc_sensors
[count
].flags
= ENVSYS_FMONSTCHANGED
;
1779 snprintf(sc
->sc_sensors
[count
].desc
,
1780 sizeof(sc
->sc_sensors
[count
].desc
),
1781 "disk%d volume%d (%s)", j
, i
, bv
.bv_dev
);
1782 sc
->sc_sensors
[count
].value_max
= i
;
1783 sc
->sc_sensors
[count
].value_avg
= j
+ 10;
1785 if (sysmon_envsys_sensor_attach(sc
->sc_sme
,
1786 &sc
->sc_sensors
[count
]))
1794 * Register our envsys driver with the framework now that the
1795 * sensors were all attached.
1797 sc
->sc_sme
->sme_name
= device_xname(sc
->sc_dev
);
1798 sc
->sc_sme
->sme_cookie
= sc
;
1799 sc
->sc_sme
->sme_refresh
= arc_refresh_sensors
;
1801 if (sysmon_envsys_register(sc
->sc_sme
)) {
1802 aprint_debug("%s: unable to register with sysmon\n",
1803 device_xname(sc
->sc_dev
));
1809 kmem_free(sc
->sc_sensors
, slen
);
1810 sysmon_envsys_destroy(sc
->sc_sme
);
1815 arc_refresh_sensors(struct sysmon_envsys
*sme
, envsys_data_t
*edata
)
1817 struct arc_softc
*sc
= sme
->sme_cookie
;
1819 struct bioc_disk bd
;
1822 if (edata
->units
!= ENVSYS_DRIVE
)
1825 memset(&bv
, 0, sizeof(bv
));
1826 bv
.bv_volid
= edata
->value_max
;
1828 if (arc_bio_vol(sc
, &bv
)) {
1829 edata
->value_cur
= ENVSYS_DRIVE_EMPTY
;
1830 edata
->state
= ENVSYS_SINVALID
;
1834 /* Current sensor is handling a disk volume member */
1835 if (edata
->value_avg
) {
1836 memset(&bd
, 0, sizeof(bd
));
1837 bd
.bd_volid
= edata
->value_max
;
1838 bd
.bd_diskid
= edata
->value_avg
- 10;
1840 if (arc_bio_disk_volume(sc
, &bd
)) {
1841 edata
->value_cur
= ENVSYS_DRIVE_OFFLINE
;
1842 edata
->state
= ENVSYS_SCRITICAL
;
1846 switch (bd
.bd_status
) {
1848 edata
->value_cur
= ENVSYS_DRIVE_ONLINE
;
1849 edata
->state
= ENVSYS_SVALID
;
1851 case BIOC_SDOFFLINE
:
1852 edata
->value_cur
= ENVSYS_DRIVE_OFFLINE
;
1853 edata
->state
= ENVSYS_SCRITICAL
;
1856 edata
->value_cur
= ENVSYS_DRIVE_FAIL
;
1857 edata
->state
= ENVSYS_SCRITICAL
;
1864 /* Current sensor is handling a volume */
1865 switch (bv
.bv_status
) {
1866 case BIOC_SVOFFLINE
:
1867 edata
->value_cur
= ENVSYS_DRIVE_OFFLINE
;
1868 edata
->state
= ENVSYS_SCRITICAL
;
1870 case BIOC_SVDEGRADED
:
1871 edata
->value_cur
= ENVSYS_DRIVE_PFAIL
;
1872 edata
->state
= ENVSYS_SCRITICAL
;
1874 case BIOC_SVBUILDING
:
1875 edata
->value_cur
= ENVSYS_DRIVE_BUILD
;
1876 edata
->state
= ENVSYS_SVALID
;
1878 case BIOC_SVMIGRATING
:
1879 edata
->value_cur
= ENVSYS_DRIVE_MIGRATING
;
1880 edata
->state
= ENVSYS_SVALID
;
1882 case BIOC_SVCHECKING
:
1883 edata
->value_cur
= ENVSYS_DRIVE_CHECK
;
1884 edata
->state
= ENVSYS_SVALID
;
1886 case BIOC_SVREBUILD
:
1887 edata
->value_cur
= ENVSYS_DRIVE_REBUILD
;
1888 edata
->state
= ENVSYS_SCRITICAL
;
1892 edata
->value_cur
= ENVSYS_DRIVE_ONLINE
;
1893 edata
->state
= ENVSYS_SVALID
;
1895 case BIOC_SVINVALID
:
1898 edata
->value_cur
= ENVSYS_DRIVE_EMPTY
; /* unknown state */
1899 edata
->state
= ENVSYS_SINVALID
;
1903 #endif /* NBIO > 0 */
1906 arc_read(struct arc_softc
*sc
, bus_size_t r
)
1910 bus_space_barrier(sc
->sc_iot
, sc
->sc_ioh
, r
, 4,
1911 BUS_SPACE_BARRIER_READ
);
1912 v
= bus_space_read_4(sc
->sc_iot
, sc
->sc_ioh
, r
);
1914 DNPRINTF(ARC_D_RW
, "%s: arc_read 0x%lx 0x%08x\n",
1915 device_xname(sc
->sc_dev
), r
, v
);
1921 arc_read_region(struct arc_softc
*sc
, bus_size_t r
, void *buf
, size_t len
)
1923 bus_space_barrier(sc
->sc_iot
, sc
->sc_ioh
, r
, len
,
1924 BUS_SPACE_BARRIER_READ
);
1925 bus_space_read_region_4(sc
->sc_iot
, sc
->sc_ioh
, r
,
1926 (uint32_t *)buf
, len
>> 2);
1930 arc_write(struct arc_softc
*sc
, bus_size_t r
, uint32_t v
)
1932 DNPRINTF(ARC_D_RW
, "%s: arc_write 0x%lx 0x%08x\n",
1933 device_xname(sc
->sc_dev
), r
, v
);
1935 bus_space_write_4(sc
->sc_iot
, sc
->sc_ioh
, r
, v
);
1936 bus_space_barrier(sc
->sc_iot
, sc
->sc_ioh
, r
, 4,
1937 BUS_SPACE_BARRIER_WRITE
);
1941 arc_write_region(struct arc_softc
*sc
, bus_size_t r
, void *buf
, size_t len
)
1943 bus_space_write_region_4(sc
->sc_iot
, sc
->sc_ioh
, r
,
1944 (const uint32_t *)buf
, len
>> 2);
1945 bus_space_barrier(sc
->sc_iot
, sc
->sc_ioh
, r
, len
,
1946 BUS_SPACE_BARRIER_WRITE
);
1950 arc_wait_eq(struct arc_softc
*sc
, bus_size_t r
, uint32_t mask
,
1955 DNPRINTF(ARC_D_RW
, "%s: arc_wait_eq 0x%lx 0x%08x 0x%08x\n",
1956 device_xname(sc
->sc_dev
), r
, mask
, target
);
1958 for (i
= 0; i
< 10000; i
++) {
1959 if ((arc_read(sc
, r
) & mask
) == target
)
1968 arc_wait_ne(struct arc_softc
*sc
, bus_size_t r
, uint32_t mask
,
1973 DNPRINTF(ARC_D_RW
, "%s: arc_wait_ne 0x%lx 0x%08x 0x%08x\n",
1974 device_xname(sc
->sc_dev
), r
, mask
, target
);
1976 for (i
= 0; i
< 10000; i
++) {
1977 if ((arc_read(sc
, r
) & mask
) != target
)
1986 arc_msg0(struct arc_softc
*sc
, uint32_t m
)
1989 arc_write(sc
, ARC_REG_INB_MSG0
, m
);
1990 /* wait for the fw to do it */
1991 if (arc_wait_eq(sc
, ARC_REG_INTRSTAT
, ARC_REG_INTRSTAT_MSG0
,
1992 ARC_REG_INTRSTAT_MSG0
) != 0)
1996 arc_write(sc
, ARC_REG_INTRSTAT
, ARC_REG_INTRSTAT_MSG0
);
2002 arc_dmamem_alloc(struct arc_softc
*sc
, size_t size
)
2004 struct arc_dmamem
*adm
;
2007 adm
= kmem_zalloc(sizeof(*adm
), KM_NOSLEEP
);
2011 adm
->adm_size
= size
;
2013 if (bus_dmamap_create(sc
->sc_dmat
, size
, 1, size
, 0,
2014 BUS_DMA_NOWAIT
|BUS_DMA_ALLOCNOW
, &adm
->adm_map
) != 0)
2017 if (bus_dmamem_alloc(sc
->sc_dmat
, size
, PAGE_SIZE
, 0, &adm
->adm_seg
,
2018 1, &nsegs
, BUS_DMA_NOWAIT
) != 0)
2021 if (bus_dmamem_map(sc
->sc_dmat
, &adm
->adm_seg
, nsegs
, size
,
2022 &adm
->adm_kva
, BUS_DMA_NOWAIT
|BUS_DMA_COHERENT
) != 0)
2025 if (bus_dmamap_load(sc
->sc_dmat
, adm
->adm_map
, adm
->adm_kva
, size
,
2026 NULL
, BUS_DMA_NOWAIT
) != 0)
2029 memset(adm
->adm_kva
, 0, size
);
2034 bus_dmamem_unmap(sc
->sc_dmat
, adm
->adm_kva
, size
);
2036 bus_dmamem_free(sc
->sc_dmat
, &adm
->adm_seg
, 1);
2038 bus_dmamap_destroy(sc
->sc_dmat
, adm
->adm_map
);
2040 kmem_free(adm
, sizeof(*adm
));
2046 arc_dmamem_free(struct arc_softc
*sc
, struct arc_dmamem
*adm
)
2048 bus_dmamap_unload(sc
->sc_dmat
, adm
->adm_map
);
2049 bus_dmamem_unmap(sc
->sc_dmat
, adm
->adm_kva
, adm
->adm_size
);
2050 bus_dmamem_free(sc
->sc_dmat
, &adm
->adm_seg
, 1);
2051 bus_dmamap_destroy(sc
->sc_dmat
, adm
->adm_map
);
2052 kmem_free(adm
, sizeof(*adm
));
2056 arc_alloc_ccbs(device_t self
)
2058 struct arc_softc
*sc
= device_private(self
);
2059 struct arc_ccb
*ccb
;
2064 TAILQ_INIT(&sc
->sc_ccb_free
);
2066 ccbslen
= sizeof(struct arc_ccb
) * sc
->sc_req_count
;
2067 sc
->sc_ccbs
= kmem_zalloc(ccbslen
, KM_SLEEP
);
2069 sc
->sc_requests
= arc_dmamem_alloc(sc
,
2070 ARC_MAX_IOCMDLEN
* sc
->sc_req_count
);
2071 if (sc
->sc_requests
== NULL
) {
2072 aprint_error_dev(self
, "unable to allocate ccb dmamem\n");
2075 cmd
= ARC_DMA_KVA(sc
->sc_requests
);
2077 for (i
= 0; i
< sc
->sc_req_count
; i
++) {
2078 ccb
= &sc
->sc_ccbs
[i
];
2080 if (bus_dmamap_create(sc
->sc_dmat
, MAXPHYS
, ARC_SGL_MAXLEN
,
2081 MAXPHYS
, 0, 0, &ccb
->ccb_dmamap
) != 0) {
2082 aprint_error_dev(self
,
2083 "unable to create dmamap for ccb %d\n", i
);
2089 ccb
->ccb_offset
= ARC_MAX_IOCMDLEN
* i
;
2091 ccb
->ccb_cmd
= (struct arc_io_cmd
*)&cmd
[ccb
->ccb_offset
];
2092 ccb
->ccb_cmd_post
= (ARC_DMA_DVA(sc
->sc_requests
) +
2093 ccb
->ccb_offset
) >> ARC_REG_POST_QUEUE_ADDR_SHIFT
;
2095 arc_put_ccb(sc
, ccb
);
2101 while ((ccb
= arc_get_ccb(sc
)) != NULL
)
2102 bus_dmamap_destroy(sc
->sc_dmat
, ccb
->ccb_dmamap
);
2103 arc_dmamem_free(sc
, sc
->sc_requests
);
2106 kmem_free(sc
->sc_ccbs
, ccbslen
);
2112 arc_get_ccb(struct arc_softc
*sc
)
2114 struct arc_ccb
*ccb
;
2116 ccb
= TAILQ_FIRST(&sc
->sc_ccb_free
);
2118 TAILQ_REMOVE(&sc
->sc_ccb_free
, ccb
, ccb_link
);
2124 arc_put_ccb(struct arc_softc
*sc
, struct arc_ccb
*ccb
)
2127 memset(ccb
->ccb_cmd
, 0, ARC_MAX_IOCMDLEN
);
2128 TAILQ_INSERT_TAIL(&sc
->sc_ccb_free
, ccb
, ccb_link
);