xref: /openbsd/sys/dev/pci/arc.c (revision 404b540a)
1 /*	$OpenBSD: arc.c,v 1.79 2009/08/13 11:10:27 dlg Exp $ */
2 
3 /*
4  * Copyright (c) 2006 David Gwynne <dlg@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "bio.h"
20 
21 #include <sys/param.h>
22 #include <sys/systm.h>
23 #include <sys/buf.h>
24 #include <sys/kernel.h>
25 #include <sys/malloc.h>
26 #include <sys/device.h>
27 #include <sys/proc.h>
28 #include <sys/rwlock.h>
29 
30 #include <machine/bus.h>
31 
32 #include <dev/pci/pcireg.h>
33 #include <dev/pci/pcivar.h>
34 #include <dev/pci/pcidevs.h>
35 
36 #include <scsi/scsi_all.h>
37 #include <scsi/scsiconf.h>
38 
39 #include <sys/sensors.h>
40 #if NBIO > 0
41 #include <sys/ioctl.h>
42 #include <dev/biovar.h>
43 #endif
44 
45 #ifdef ARC_DEBUG
46 #define ARC_D_INIT	(1<<0)
47 #define ARC_D_RW	(1<<1)
48 #define ARC_D_DB	(1<<2)
49 
50 int arcdebug = 0;
51 
52 #define DPRINTF(p...)		do { if (arcdebug) printf(p); } while (0)
53 #define DNPRINTF(n, p...)	do { if ((n) & arcdebug) printf(p); } while (0)
54 
55 #else
56 #define DPRINTF(p...)		/* p */
57 #define DNPRINTF(n, p...)	/* n, p */
58 #endif
59 
60 /* Areca boards using the Intel IOP are Revision A (RA) */
61 
62 #define ARC_RA_PCI_BAR			PCI_MAPREG_START
63 
64 #define ARC_RA_INB_MSG0			0x0010
65 #define  ARC_RA_INB_MSG0_NOP			(0x00000000)
66 #define  ARC_RA_INB_MSG0_GET_CONFIG		(0x00000001)
67 #define  ARC_RA_INB_MSG0_SET_CONFIG		(0x00000002)
68 #define  ARC_RA_INB_MSG0_ABORT_CMD		(0x00000003)
69 #define  ARC_RA_INB_MSG0_STOP_BGRB		(0x00000004)
70 #define  ARC_RA_INB_MSG0_FLUSH_CACHE		(0x00000005)
71 #define  ARC_RA_INB_MSG0_START_BGRB		(0x00000006)
72 #define  ARC_RA_INB_MSG0_CHK331PENDING		(0x00000007)
73 #define  ARC_RA_INB_MSG0_SYNC_TIMER		(0x00000008)
74 #define ARC_RA_INB_MSG1			0x0014
75 #define ARC_RA_OUTB_ADDR0		0x0018
76 #define ARC_RA_OUTB_ADDR1		0x001c
77 #define  ARC_RA_OUTB_ADDR1_FIRMWARE_OK		(1<<31)
78 #define ARC_RA_INB_DOORBELL		0x0020
79 #define  ARC_RA_INB_DOORBELL_WRITE_OK		(1<<0)
80 #define  ARC_RA_INB_DOORBELL_READ_OK		(1<<1)
81 #define ARC_RA_OUTB_DOORBELL		0x002c
82 #define  ARC_RA_OUTB_DOORBELL_WRITE_OK		(1<<0)
83 #define  ARC_RA_OUTB_DOORBELL_READ_OK		(1<<1)
84 #define ARC_RA_INTRSTAT			0x0030
85 #define  ARC_RA_INTRSTAT_MSG0			(1<<0)
86 #define  ARC_RA_INTRSTAT_MSG1			(1<<1)
87 #define  ARC_RA_INTRSTAT_DOORBELL		(1<<2)
88 #define  ARC_RA_INTRSTAT_POSTQUEUE		(1<<3)
89 #define  ARC_RA_INTRSTAT_PCI			(1<<4)
90 #define ARC_RA_INTRMASK			0x0034
91 #define  ARC_RA_INTRMASK_MSG0			(1<<0)
92 #define  ARC_RA_INTRMASK_MSG1			(1<<1)
93 #define  ARC_RA_INTRMASK_DOORBELL		(1<<2)
94 #define  ARC_RA_INTRMASK_POSTQUEUE		(1<<3)
95 #define  ARC_RA_INTRMASK_PCI			(1<<4)
96 #define ARC_RA_POST_QUEUE		0x0040
97 #define  ARC_RA_POST_QUEUE_ADDR_SHIFT		5
98 #define  ARC_RA_POST_QUEUE_IAMBIOS		(1<<30)
99 #define  ARC_RA_POST_QUEUE_BIGFRAME		(1<<31)
100 #define ARC_RA_REPLY_QUEUE		0x0044
101 #define  ARC_RA_REPLY_QUEUE_ADDR_SHIFT		5
102 #define  ARC_RA_REPLY_QUEUE_ERR			(1<<28)
103 #define  ARC_RA_REPLY_QUEUE_IAMBIOS		(1<<30)
104 #define ARC_RA_MSGBUF			0x0a00
105 #define  ARC_RA_MSGBUF_LEN			1024
106 #define ARC_RA_IOC_WBUF_LEN		0x0e00
107 #define ARC_RA_IOC_WBUF			0x0e04
108 #define ARC_RA_IOC_RBUF_LEN		0x0f00
109 #define ARC_RA_IOC_RBUF			0x0f04
110 #define  ARC_RA_IOC_RWBUF_MAXLEN	124 /* for both RBUF and WBUF */
111 
112 /* Areca boards using the Marvel IOP are Revision B (RB) */
113 
114 #define ARC_RB_DRV2IOP_DOORBELL		0x00020400
115 #define ARC_RB_DRV2IOP_DOORBELL_MASK	0x00020404
116 #define ARC_RB_IOP2DRV_DOORBELL		0x00020408
117 #define  ARC_RB_IOP2DRV_DOORBELL_FIRMWARE_OK	(1<<31)
118 #define ARC_RB_IOP2DRV_DOORBELL_MASK	0x0002040c
119 
120 struct arc_msg_firmware_info {
121 	u_int32_t		signature;
122 #define ARC_FWINFO_SIGNATURE_GET_CONFIG		(0x87974060)
123 	u_int32_t		request_len;
124 	u_int32_t		queue_len;
125 	u_int32_t		sdram_size;
126 	u_int32_t		sata_ports;
127 	u_int8_t		vendor[40];
128 	u_int8_t		model[8];
129 	u_int8_t		fw_version[16];
130 	u_int8_t		device_map[16];
131 } __packed;
132 
133 struct arc_msg_scsicmd {
134 	u_int8_t		bus;
135 	u_int8_t		target;
136 	u_int8_t		lun;
137 	u_int8_t		function;
138 
139 	u_int8_t		cdb_len;
140 	u_int8_t		sgl_len;
141 	u_int8_t		flags;
142 #define ARC_MSG_SCSICMD_FLAG_SGL_BSIZE_512	(1<<0)
143 #define ARC_MSG_SCSICMD_FLAG_FROM_BIOS		(1<<1)
144 #define ARC_MSG_SCSICMD_FLAG_WRITE		(1<<2)
145 #define ARC_MSG_SCSICMD_FLAG_SIMPLEQ		(0x00)
146 #define ARC_MSG_SCSICMD_FLAG_HEADQ		(0x08)
147 #define ARC_MSG_SCSICMD_FLAG_ORDERQ		(0x10)
148 	u_int8_t		reserved;
149 
150 	u_int32_t		context;
151 	u_int32_t		data_len;
152 
153 #define ARC_MSG_CDBLEN				16
154 	u_int8_t		cdb[ARC_MSG_CDBLEN];
155 
156 	u_int8_t		status;
157 #define ARC_MSG_STATUS_SELTIMEOUT		0xf0
158 #define ARC_MSG_STATUS_ABORTED			0xf1
159 #define ARC_MSG_STATUS_INIT_FAIL		0xf2
160 #define ARC_MSG_SENSELEN			15
161 	u_int8_t		sense_data[ARC_MSG_SENSELEN];
162 
163 	/* followed by an sgl */
164 } __packed;
165 
166 struct arc_sge {
167 	u_int32_t		sg_hdr;
168 #define ARC_SGE_64BIT				(1<<24)
169 	u_int32_t		sg_lo_addr;
170 	u_int32_t		sg_hi_addr;
171 } __packed;
172 
173 #define ARC_MAX_TARGET		16
174 #define ARC_MAX_LUN		8
175 #define ARC_MAX_IOCMDLEN	512
176 #define ARC_BLOCKSIZE		512
177 
178 /* the firmware deals with up to 256 or 512 byte command frames. */
179 /* sizeof(struct arc_msg_scsicmd) + (sizeof(struct arc_sge) * 38) == 508 */
180 #define ARC_SGL_MAXLEN		38
181 /* sizeof(struct arc_msg_scsicmd) + (sizeof(struct arc_sge) * 17) == 252 */
182 #define ARC_SGL_256LEN		17
183 
184 struct arc_io_cmd {
185 	struct arc_msg_scsicmd	cmd;
186 	struct arc_sge		sgl[ARC_SGL_MAXLEN];
187 } __packed;
188 
189 /* definitions of the firmware commands sent via the doorbells */
190 
191 struct arc_fw_hdr {
192 	u_int8_t		byte1;
193 	u_int8_t		byte2;
194 	u_int8_t		byte3;
195 } __packed;
196 
197 /* the fw header must always equal this */
198 struct arc_fw_hdr arc_fw_hdr = { 0x5e, 0x01, 0x61 };
199 
200 struct arc_fw_bufhdr {
201 	struct arc_fw_hdr	hdr;
202 	u_int16_t		len;
203 } __packed;
204 
205 #define ARC_FW_RAIDINFO		0x20	/* opcode + raid# */
206 #define ARC_FW_VOLINFO		0x21	/* opcode + vol# */
207 #define ARC_FW_DISKINFO		0x22	/* opcode + physdisk# */
208 #define ARC_FW_SYSINFO		0x23	/* opcode. reply is fw_sysinfo */
209 #define ARC_FW_MUTE_ALARM	0x30	/* opcode only */
210 #define ARC_FW_SET_ALARM	0x31	/* opcode + 1 byte for setting */
211 #define  ARC_FW_SET_ALARM_DISABLE		0x00
212 #define  ARC_FW_SET_ALARM_ENABLE		0x01
213 #define ARC_FW_NOP		0x38	/* opcode only */
214 
215 #define ARC_FW_CMD_OK		0x41
216 #define ARC_FW_BLINK		0x43
217 #define  ARC_FW_BLINK_ENABLE			0x00
218 #define  ARC_FW_BLINK_DISABLE			0x01
219 #define ARC_FW_CMD_PASS_REQD	0x4d
220 
221 struct arc_fw_comminfo {
222 	u_int8_t		baud_rate;
223 	u_int8_t		data_bits;
224 	u_int8_t		stop_bits;
225 	u_int8_t		parity;
226 	u_int8_t		flow_control;
227 } __packed;
228 
229 struct arc_fw_scsiattr {
230 	u_int8_t		channel;// channel for SCSI target (0/1)
231 	u_int8_t		target;
232 	u_int8_t		lun;
233 	u_int8_t		tagged;
234 	u_int8_t		cache;
235 	u_int8_t		speed;
236 } __packed;
237 
238 struct arc_fw_raidinfo {
239 	u_int8_t		set_name[16];
240 	u_int32_t		capacity;
241 	u_int32_t		capacity2;
242 	u_int32_t		fail_mask;
243 	u_int8_t		device_array[32];
244 	u_int8_t		member_devices;
245 	u_int8_t		new_member_devices;
246 	u_int8_t		raid_state;
247 	u_int8_t		volumes;
248 	u_int8_t		volume_list[16];
249 	u_int8_t		reserved1[3];
250 	u_int8_t		free_segments;
251 	u_int32_t		raw_stripes[8];
252 	u_int8_t		reserved2[12];
253 } __packed;
254 
255 struct arc_fw_volinfo {
256 	u_int8_t		set_name[16];
257 	u_int32_t		capacity;
258 	u_int32_t		capacity2;
259 	u_int32_t		fail_mask;
260 	u_int32_t		stripe_size; /* in blocks */
261 	u_int32_t		new_fail_mask;
262 	u_int32_t		new_stripe_size;
263 	u_int32_t		volume_status;
264 #define ARC_FW_VOL_STATUS_NORMAL	0x00
265 #define ARC_FW_VOL_STATUS_INITTING	(1<<0)
266 #define ARC_FW_VOL_STATUS_FAILED	(1<<1)
267 #define ARC_FW_VOL_STATUS_MIGRATING	(1<<2)
268 #define ARC_FW_VOL_STATUS_REBUILDING	(1<<3)
269 #define ARC_FW_VOL_STATUS_NEED_INIT	(1<<4)
270 #define ARC_FW_VOL_STATUS_NEED_MIGRATE	(1<<5)
271 #define ARC_FW_VOL_STATUS_INIT_FLAG	(1<<6)
272 #define ARC_FW_VOL_STATUS_NEED_REGEN	(1<<7)
273 #define ARC_FW_VOL_STATUS_CHECKING	(1<<8)
274 #define ARC_FW_VOL_STATUS_NEED_CHECK	(1<<9)
275 	u_int32_t		progress;
276 	struct arc_fw_scsiattr	scsi_attr;
277 	u_int8_t		member_disks;
278 	u_int8_t		raid_level;
279 #define ARC_FW_VOL_RAIDLEVEL_0		0x00
280 #define ARC_FW_VOL_RAIDLEVEL_1		0x01
281 #define ARC_FW_VOL_RAIDLEVEL_3		0x02
282 #define ARC_FW_VOL_RAIDLEVEL_5		0x03
283 #define ARC_FW_VOL_RAIDLEVEL_6		0x04
284 #define ARC_FW_VOL_RAIDLEVEL_PASSTHRU	0x05
285 	u_int8_t		new_member_disks;
286 	u_int8_t		new_raid_level;
287 	u_int8_t		raid_set_number;
288 	u_int8_t		reserved[5];
289 } __packed;
290 
291 struct arc_fw_diskinfo {
292 	u_int8_t		model[40];
293 	u_int8_t		serial[20];
294 	u_int8_t		firmware_rev[8];
295 	u_int32_t		capacity;
296 	u_int32_t		capacity2;
297 	u_int8_t		device_state;
298 	u_int8_t		pio_mode;
299 	u_int8_t		current_udma_mode;
300 	u_int8_t		udma_mode;
301 	u_int8_t		drive_select;
302 	u_int8_t		raid_number; // 0xff unowned
303 	struct arc_fw_scsiattr	scsi_attr;
304 	u_int8_t		reserved[44];
305 } __packed;
306 
307 struct arc_fw_sysinfo {
308 	u_int8_t		vendor_name[40];
309 	u_int8_t		serial_number[16];
310 	u_int8_t		firmware_version[16];
311 	u_int8_t		boot_version[16];
312 	u_int8_t		mb_version[16];
313 	u_int8_t		model_name[8];
314 
315 	u_int8_t		local_ip[4];
316 	u_int8_t		current_ip[4];
317 
318 	u_int32_t		time_tick;
319 	u_int32_t		cpu_speed;
320 	u_int32_t		icache;
321 	u_int32_t		dcache;
322 	u_int32_t		scache;
323 	u_int32_t		memory_size;
324 	u_int32_t		memory_speed;
325 	u_int32_t		events;
326 
327 	u_int8_t		gsiMacAddress[6];
328 	u_int8_t		gsiDhcp;
329 
330 	u_int8_t		alarm;
331 	u_int8_t		channel_usage;
332 	u_int8_t		max_ata_mode;
333 	u_int8_t		sdram_ecc;
334 	u_int8_t		rebuild_priority;
335 	struct arc_fw_comminfo	comm_a;
336 	struct arc_fw_comminfo	comm_b;
337 	u_int8_t		ide_channels;
338 	u_int8_t		scsi_host_channels;
339 	u_int8_t		ide_host_channels;
340 	u_int8_t		max_volume_set;
341 	u_int8_t		max_raid_set;
342 	u_int8_t		ether_port;
343 	u_int8_t		raid6_engine;
344 	u_int8_t		reserved[75];
345 } __packed;
346 
347 int			arc_match(struct device *, void *, void *);
348 void			arc_attach(struct device *, struct device *, void *);
349 int			arc_detach(struct device *, int);
350 void			arc_shutdown(void *);
351 int			arc_intr(void *);
352 
353 struct arc_iop;
354 struct arc_ccb;
355 TAILQ_HEAD(arc_ccb_list, arc_ccb);
356 
357 struct arc_softc {
358 	struct device		sc_dev;
359 	const struct arc_iop	*sc_iop;
360 	struct scsi_link	sc_link;
361 
362 	pci_chipset_tag_t	sc_pc;
363 	pcitag_t		sc_tag;
364 
365 	bus_space_tag_t		sc_iot;
366 	bus_space_handle_t	sc_ioh;
367 	bus_size_t		sc_ios;
368 	bus_dma_tag_t		sc_dmat;
369 
370 	void			*sc_ih;
371 
372 	void			*sc_shutdownhook;
373 
374 	int			sc_req_count;
375 
376 	struct arc_dmamem	*sc_requests;
377 	struct arc_ccb		*sc_ccbs;
378 	struct arc_ccb_list	sc_ccb_free;
379 
380 	struct scsibus_softc	*sc_scsibus;
381 
382 	struct rwlock		sc_lock;
383 	volatile int		sc_talking;
384 
385 	struct ksensor		*sc_sensors;
386 	struct ksensordev	sc_sensordev;
387 	int			sc_nsensors;
388 
389 	u_int32_t		sc_ledmask;
390 };
391 #define DEVNAME(_s)		((_s)->sc_dev.dv_xname)
392 
393 struct cfattach arc_ca = {
394 	sizeof(struct arc_softc), arc_match, arc_attach, arc_detach
395 };
396 
397 struct cfdriver arc_cd = {
398 	NULL, "arc", DV_DULL
399 };
400 
401 /* interface for scsi midlayer to talk to */
402 int			arc_scsi_cmd(struct scsi_xfer *);
403 void			arc_minphys(struct buf *, struct scsi_link *);
404 
405 struct scsi_adapter arc_switch = {
406 	arc_scsi_cmd, arc_minphys, NULL, NULL, NULL
407 };
408 
409 struct scsi_device arc_dev = {
410 	NULL, NULL, NULL, NULL
411 };
412 
413 /* code to deal with getting bits in and out of the bus space */
414 u_int32_t		arc_read(struct arc_softc *, bus_size_t);
415 void			arc_read_region(struct arc_softc *, bus_size_t,
416 			    void *, size_t);
417 void			arc_write(struct arc_softc *, bus_size_t, u_int32_t);
418 void			arc_write_region(struct arc_softc *, bus_size_t,
419 			    void *, size_t);
420 int			arc_wait_eq(struct arc_softc *, bus_size_t,
421 			    u_int32_t, u_int32_t);
422 int			arc_wait_ne(struct arc_softc *, bus_size_t,
423 			    u_int32_t, u_int32_t);
424 int			arc_msg0(struct arc_softc *, u_int32_t);
425 
426 #define arc_push(_s, _r)	arc_write((_s), ARC_RA_POST_QUEUE, (_r))
427 #define arc_pop(_s)		arc_read((_s), ARC_RA_REPLY_QUEUE)
428 
429 /* wrap up the bus_dma api */
430 struct arc_dmamem {
431 	bus_dmamap_t		adm_map;
432 	bus_dma_segment_t	adm_seg;
433 	size_t			adm_size;
434 	caddr_t			adm_kva;
435 };
436 #define ARC_DMA_MAP(_adm)	((_adm)->adm_map)
437 #define ARC_DMA_DVA(_adm)	((_adm)->adm_map->dm_segs[0].ds_addr)
438 #define ARC_DMA_KVA(_adm)	((void *)(_adm)->adm_kva)
439 
440 struct arc_dmamem	*arc_dmamem_alloc(struct arc_softc *, size_t);
441 void			arc_dmamem_free(struct arc_softc *,
442 			    struct arc_dmamem *);
443 
444 /* stuff to manage a scsi command */
445 struct arc_ccb {
446 	struct arc_softc	*ccb_sc;
447 	int			ccb_id;
448 
449 	struct scsi_xfer	*ccb_xs;
450 
451 	bus_dmamap_t		ccb_dmamap;
452 	bus_addr_t		ccb_offset;
453 	struct arc_io_cmd	*ccb_cmd;
454 	u_int32_t		ccb_cmd_post;
455 
456 	TAILQ_ENTRY(arc_ccb)	ccb_link;
457 };
458 
459 int			arc_alloc_ccbs(struct arc_softc *);
460 struct arc_ccb		*arc_get_ccb(struct arc_softc *);
461 void			arc_put_ccb(struct arc_softc *, struct arc_ccb *);
462 int			arc_load_xs(struct arc_ccb *);
463 int			arc_complete(struct arc_softc *, struct arc_ccb *,
464 			    int);
465 void			arc_scsi_cmd_done(struct arc_softc *, struct arc_ccb *,
466 			    u_int32_t);
467 
468 /* real stuff for dealing with the hardware */
469 struct arc_iop {
470 	int			(*iop_query_firmware)(struct arc_softc *);
471 };
472 
473 int			arc_map_pci_resources(struct arc_softc *,
474 			    struct pci_attach_args *);
475 void			arc_unmap_pci_resources(struct arc_softc *);
476 int			arc_intel_query_firmware(struct arc_softc *);
477 int			arc_marvell_query_firmware(struct arc_softc *);
478 
479 #if NBIO > 0
480 /* stuff to do messaging via the doorbells */
481 void			arc_lock(struct arc_softc *);
482 void			arc_unlock(struct arc_softc *);
483 void			arc_wait(struct arc_softc *);
484 u_int8_t		arc_msg_cksum(void *, u_int16_t);
485 int			arc_msgbuf(struct arc_softc *, void *, size_t,
486 			    void *, size_t, int);
487 
488 /* bioctl */
489 int			arc_bioctl(struct device *, u_long, caddr_t);
490 int			arc_bio_inq(struct arc_softc *, struct bioc_inq *);
491 int			arc_bio_vol(struct arc_softc *, struct bioc_vol *);
492 int			arc_bio_disk(struct arc_softc *, struct bioc_disk *);
493 int			arc_bio_alarm(struct arc_softc *, struct bioc_alarm *);
494 int			arc_bio_alarm_state(struct arc_softc *,
495 			    struct bioc_alarm *);
496 int			arc_bio_blink(struct arc_softc *, struct bioc_blink *);
497 
498 int			arc_bio_getvol(struct arc_softc *, int,
499 			    struct arc_fw_volinfo *);
500 
501 #ifndef SMALL_KERNEL
502 /* sensors */
503 void			arc_create_sensors(void *, void *);
504 void			arc_refresh_sensors(void *);
505 #endif /* SMALL_KERNEL */
506 #endif
507 
508 static const struct arc_iop arc_intel = {
509 	arc_intel_query_firmware
510 };
511 
512 static const struct arc_iop arc_marvell = {
513 	arc_marvell_query_firmware
514 };
515 
516 struct arc_board {
517 	pcireg_t		ab_vendor;
518 	pcireg_t		ab_product;
519 	const struct arc_iop	*ab_iop;
520 };
521 const struct arc_board	*arc_match_board(struct pci_attach_args *);
522 
523 static const struct arc_board arc_devices[] = {
524 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1110, &arc_intel },
525 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1120, &arc_intel },
526 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1130, &arc_intel },
527 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1160, &arc_intel },
528 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1170, &arc_intel },
529 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1200, &arc_intel },
530 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1200_B, &arc_marvell },
531 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1202, &arc_intel },
532 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1210, &arc_intel },
533 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1220, &arc_intel },
534 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1230, &arc_intel },
535 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1260, &arc_intel },
536 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1270, &arc_intel },
537 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1280, &arc_intel },
538 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1380, &arc_intel },
539 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1381, &arc_intel },
540 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1680, &arc_intel },
541 	{ PCI_VENDOR_ARECA, PCI_PRODUCT_ARECA_ARC1681, &arc_intel }
542 };
543 
544 const struct arc_board *
545 arc_match_board(struct pci_attach_args *pa)
546 {
547 	const struct arc_board		*ab;
548 	int				i;
549 
550 	for (i = 0; i < sizeof(arc_devices) / sizeof(arc_devices[0]); i++) {
551 		ab = &arc_devices[i];
552 
553 		if (PCI_VENDOR(pa->pa_id) == ab->ab_vendor &&
554 		    PCI_PRODUCT(pa->pa_id) == ab->ab_product)
555 			return (ab);
556 	}
557 
558 	return (NULL);
559 }
560 
561 int
562 arc_match(struct device *parent, void *match, void *aux)
563 {
564 	return ((arc_match_board(aux) == NULL) ? 0 : 1);
565 }
566 
567 void
568 arc_attach(struct device *parent, struct device *self, void *aux)
569 {
570 	struct arc_softc		*sc = (struct arc_softc *)self;
571 	struct pci_attach_args		*pa = aux;
572 	struct scsibus_attach_args	saa;
573 	struct device			*child;
574 
575 	sc->sc_talking = 0;
576 	rw_init(&sc->sc_lock, "arcmsg");
577 
578 	sc->sc_iop = arc_match_board(pa)->ab_iop;
579 
580 	if (arc_map_pci_resources(sc, pa) != 0) {
581 		/* error message printed by arc_map_pci_resources */
582 		return;
583 	}
584 
585 	if (sc->sc_iop->iop_query_firmware(sc) != 0) {
586 		/* error message printed by arc_query_firmware */
587 		goto unmap_pci;
588 	}
589 
590 	if (arc_alloc_ccbs(sc) != 0) {
591 		/* error message printed by arc_alloc_ccbs */
592 		goto unmap_pci;
593 	}
594 
595 	sc->sc_shutdownhook = shutdownhook_establish(arc_shutdown, sc);
596 	if (sc->sc_shutdownhook == NULL)
597 		panic("unable to establish arc powerhook");
598 
599 	sc->sc_link.device = &arc_dev;
600 	sc->sc_link.adapter = &arc_switch;
601 	sc->sc_link.adapter_softc = sc;
602 	sc->sc_link.adapter_target = ARC_MAX_TARGET;
603 	sc->sc_link.adapter_buswidth = ARC_MAX_TARGET;
604 	sc->sc_link.openings = sc->sc_req_count / ARC_MAX_TARGET;
605 
606 	bzero(&saa, sizeof(saa));
607 	saa.saa_sc_link = &sc->sc_link;
608 
609 	child = config_found(self, &saa, scsiprint);
610 	sc->sc_scsibus = (struct scsibus_softc *)child;
611 
612 	/* enable interrupts */
613 	arc_write(sc, ARC_RA_INTRMASK,
614 	    ~(ARC_RA_INTRMASK_POSTQUEUE|ARC_RA_INTRSTAT_DOORBELL));
615 
616 #if NBIO > 0
617 	if (bio_register(self, arc_bioctl) != 0)
618 		panic("%s: bioctl registration failed\n", DEVNAME(sc));
619 
620 #ifndef SMALL_KERNEL
621 	/*
622 	 * you need to talk to the firmware to get volume info. our firmware
623 	 * interface relies on being able to sleep, so we need to use a thread
624 	 * to do the work.
625 	 */
626 	if (scsi_task(arc_create_sensors, sc, NULL, 1) != 0)
627 		printf("%s: unable to schedule arc_create_sensors as a "
628 		    "scsi task", DEVNAME(sc));
629 #endif
630 #endif
631 
632 	return;
633 unmap_pci:
634 	arc_unmap_pci_resources(sc);
635 }
636 
637 int
638 arc_detach(struct device *self, int flags)
639 {
640 	struct arc_softc		*sc = (struct arc_softc *)self;
641 
642 	shutdownhook_disestablish(sc->sc_shutdownhook);
643 
644 	if (arc_msg0(sc, ARC_RA_INB_MSG0_STOP_BGRB) != 0)
645 		printf("%s: timeout waiting to stop bg rebuild\n", DEVNAME(sc));
646 
647 	if (arc_msg0(sc, ARC_RA_INB_MSG0_FLUSH_CACHE) != 0)
648 		printf("%s: timeout waiting to flush cache\n", DEVNAME(sc));
649 
650 	return (0);
651 }
652 
653 void
654 arc_shutdown(void *xsc)
655 {
656 	struct arc_softc		*sc = xsc;
657 
658 	if (arc_msg0(sc, ARC_RA_INB_MSG0_STOP_BGRB) != 0)
659 		printf("%s: timeout waiting to stop bg rebuild\n", DEVNAME(sc));
660 
661 	if (arc_msg0(sc, ARC_RA_INB_MSG0_FLUSH_CACHE) != 0)
662 		printf("%s: timeout waiting to flush cache\n", DEVNAME(sc));
663 }
664 
665 int
666 arc_intr(void *arg)
667 {
668 	struct arc_softc		*sc = arg;
669 	struct arc_ccb			*ccb = NULL;
670 	char				*kva = ARC_DMA_KVA(sc->sc_requests);
671 	struct arc_io_cmd		*cmd;
672 	u_int32_t			reg, intrstat;
673 
674 	intrstat = arc_read(sc, ARC_RA_INTRSTAT);
675 	if (intrstat == 0x0)
676 		return (0);
677 	intrstat &= ARC_RA_INTRSTAT_POSTQUEUE | ARC_RA_INTRSTAT_DOORBELL;
678 	arc_write(sc, ARC_RA_INTRSTAT, intrstat);
679 
680 	if (intrstat & ARC_RA_INTRSTAT_DOORBELL) {
681 		if (sc->sc_talking) {
682 			/* if an ioctl is talking, wake it up */
683 			arc_write(sc, ARC_RA_INTRMASK,
684 			    ~ARC_RA_INTRMASK_POSTQUEUE);
685 			wakeup(sc);
686 		} else {
687 			/* otherwise drop it */
688 			reg = arc_read(sc, ARC_RA_OUTB_DOORBELL);
689 			arc_write(sc, ARC_RA_OUTB_DOORBELL, reg);
690 			if (reg & ARC_RA_OUTB_DOORBELL_WRITE_OK)
691 				arc_write(sc, ARC_RA_INB_DOORBELL,
692 				    ARC_RA_INB_DOORBELL_READ_OK);
693 		}
694 	}
695 
696 	while ((reg = arc_pop(sc)) != 0xffffffff) {
697 		cmd = (struct arc_io_cmd *)(kva +
698 		    ((reg << ARC_RA_REPLY_QUEUE_ADDR_SHIFT) -
699 		    (u_int32_t)ARC_DMA_DVA(sc->sc_requests)));
700 		ccb = &sc->sc_ccbs[letoh32(cmd->cmd.context)];
701 
702 		bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
703 		    ccb->ccb_offset, ARC_MAX_IOCMDLEN,
704 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
705 
706 		arc_scsi_cmd_done(sc, ccb, reg);
707 	}
708 
709 	return (1);
710 }
711 
712 int
713 arc_scsi_cmd(struct scsi_xfer *xs)
714 {
715 	struct scsi_link		*link = xs->sc_link;
716 	struct arc_softc		*sc = link->adapter_softc;
717 	struct arc_ccb			*ccb;
718 	struct arc_msg_scsicmd		*cmd;
719 	u_int32_t			reg;
720 	int				rv = SUCCESSFULLY_QUEUED;
721 	int				s;
722 
723 	if (xs->cmdlen > ARC_MSG_CDBLEN) {
724 		bzero(&xs->sense, sizeof(xs->sense));
725 		xs->sense.error_code = SSD_ERRCODE_VALID | 0x70;
726 		xs->sense.flags = SKEY_ILLEGAL_REQUEST;
727 		xs->sense.add_sense_code = 0x20;
728 		xs->error = XS_SENSE;
729 		xs->flags |= ITSDONE;
730 		s = splbio();
731 		scsi_done(xs);
732 		splx(s);
733 		return (COMPLETE);
734 	}
735 
736 	s = splbio();
737 	ccb = arc_get_ccb(sc);
738 	splx(s);
739 	if (ccb == NULL) {
740 		xs->error = XS_DRIVER_STUFFUP;
741 		xs->flags |= ITSDONE;
742 		s = splbio();
743 		scsi_done(xs);
744 		splx(s);
745 		return (COMPLETE);
746 	}
747 
748 	ccb->ccb_xs = xs;
749 
750 	if (arc_load_xs(ccb) != 0) {
751 		xs->error = XS_DRIVER_STUFFUP;
752 		xs->flags |= ITSDONE;
753 		s = splbio();
754 		arc_put_ccb(sc, ccb);
755 		scsi_done(xs);
756 		splx(s);
757 		return (COMPLETE);
758 	}
759 
760 	cmd = &ccb->ccb_cmd->cmd;
761 	reg = ccb->ccb_cmd_post;
762 
763 	/* bus is always 0 */
764 	cmd->target = link->target;
765 	cmd->lun = link->lun;
766 	cmd->function = 1; /* XXX magic number */
767 
768 	cmd->cdb_len = xs->cmdlen;
769 	cmd->sgl_len = ccb->ccb_dmamap->dm_nsegs;
770 	if (xs->flags & SCSI_DATA_OUT)
771 		cmd->flags = ARC_MSG_SCSICMD_FLAG_WRITE;
772 	if (ccb->ccb_dmamap->dm_nsegs > ARC_SGL_256LEN) {
773 		cmd->flags |= ARC_MSG_SCSICMD_FLAG_SGL_BSIZE_512;
774 		reg |= ARC_RA_POST_QUEUE_BIGFRAME;
775 	}
776 
777 	cmd->context = htole32(ccb->ccb_id);
778 	cmd->data_len = htole32(xs->datalen);
779 
780 	bcopy(xs->cmd, cmd->cdb, xs->cmdlen);
781 
782 	/* we've built the command, let's put it on the hw */
783 	bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
784 	    ccb->ccb_offset, ARC_MAX_IOCMDLEN,
785 	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
786 
787 	s = splbio();
788 	arc_push(sc, reg);
789 	if (xs->flags & SCSI_POLL) {
790 		rv = COMPLETE;
791 		if (arc_complete(sc, ccb, xs->timeout) != 0) {
792 			xs->error = XS_DRIVER_STUFFUP;
793 			xs->flags |= ITSDONE;
794 			scsi_done(xs);
795 		}
796 	}
797 	splx(s);
798 
799 	return (rv);
800 }
801 
802 int
803 arc_load_xs(struct arc_ccb *ccb)
804 {
805 	struct arc_softc		*sc = ccb->ccb_sc;
806 	struct scsi_xfer		*xs = ccb->ccb_xs;
807 	bus_dmamap_t			dmap = ccb->ccb_dmamap;
808 	struct arc_sge			*sgl = ccb->ccb_cmd->sgl, *sge;
809 	u_int64_t			addr;
810 	int				i, error;
811 
812 	if (xs->datalen == 0)
813 		return (0);
814 
815 	error = bus_dmamap_load(sc->sc_dmat, dmap,
816 	    xs->data, xs->datalen, NULL,
817 	    (xs->flags & SCSI_NOSLEEP) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK);
818 	if (error != 0) {
819 		printf("%s: error %d loading dmamap\n", DEVNAME(sc), error);
820 		return (1);
821 	}
822 
823 	for (i = 0; i < dmap->dm_nsegs; i++) {
824 		sge = &sgl[i];
825 
826 		sge->sg_hdr = htole32(ARC_SGE_64BIT | dmap->dm_segs[i].ds_len);
827 		addr = dmap->dm_segs[i].ds_addr;
828 		sge->sg_hi_addr = htole32((u_int32_t)(addr >> 32));
829 		sge->sg_lo_addr = htole32((u_int32_t)addr);
830 	}
831 
832 	bus_dmamap_sync(sc->sc_dmat, dmap, 0, dmap->dm_mapsize,
833 	    (xs->flags & SCSI_DATA_IN) ? BUS_DMASYNC_PREREAD :
834 	    BUS_DMASYNC_PREWRITE);
835 
836 	return (0);
837 }
838 
839 void
840 arc_scsi_cmd_done(struct arc_softc *sc, struct arc_ccb *ccb, u_int32_t reg)
841 {
842 	struct scsi_xfer		*xs = ccb->ccb_xs;
843 	struct arc_msg_scsicmd		*cmd;
844 
845 	if (xs->datalen != 0) {
846 		bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap, 0,
847 		    ccb->ccb_dmamap->dm_mapsize, (xs->flags & SCSI_DATA_IN) ?
848 		    BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
849 		bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap);
850 	}
851 
852 	/* timeout_del */
853 	xs->flags |= ITSDONE;
854 
855 	if (reg & ARC_RA_REPLY_QUEUE_ERR) {
856 		cmd = &ccb->ccb_cmd->cmd;
857 
858 		switch (cmd->status) {
859 		case ARC_MSG_STATUS_SELTIMEOUT:
860 		case ARC_MSG_STATUS_ABORTED:
861 		case ARC_MSG_STATUS_INIT_FAIL:
862 			xs->status = SCSI_OK;
863 			xs->error = XS_SELTIMEOUT;
864 			break;
865 
866 		case SCSI_CHECK:
867 			bzero(&xs->sense, sizeof(xs->sense));
868 			bcopy(cmd->sense_data, &xs->sense,
869 			    min(ARC_MSG_SENSELEN, sizeof(xs->sense)));
870 			xs->sense.error_code = SSD_ERRCODE_VALID | 0x70;
871 			xs->status = SCSI_CHECK;
872 			xs->error = XS_SENSE;
873 			xs->resid = 0;
874 			break;
875 
876 		default:
877 			/* unknown device status */
878 			xs->error = XS_BUSY; /* try again later? */
879 			xs->status = SCSI_BUSY;
880 			break;
881 		}
882 	} else {
883 		xs->status = SCSI_OK;
884 		xs->error = XS_NOERROR;
885 		xs->resid = 0;
886 	}
887 
888 	arc_put_ccb(sc, ccb);
889 	scsi_done(xs);
890 }
891 
892 int
893 arc_complete(struct arc_softc *sc, struct arc_ccb *nccb, int timeout)
894 {
895 	struct arc_ccb			*ccb = NULL;
896 	char				*kva = ARC_DMA_KVA(sc->sc_requests);
897 	struct arc_io_cmd		*cmd;
898 	u_int32_t			reg;
899 
900 	do {
901 		reg = arc_pop(sc);
902 		if (reg == 0xffffffff) {
903 			if (timeout-- == 0)
904 				return (1);
905 
906 			delay(1000);
907 			continue;
908 		}
909 
910 		cmd = (struct arc_io_cmd *)(kva +
911 		    ((reg << ARC_RA_REPLY_QUEUE_ADDR_SHIFT) -
912 		    ARC_DMA_DVA(sc->sc_requests)));
913 		ccb = &sc->sc_ccbs[letoh32(cmd->cmd.context)];
914 
915 		bus_dmamap_sync(sc->sc_dmat, ARC_DMA_MAP(sc->sc_requests),
916 		    ccb->ccb_offset, ARC_MAX_IOCMDLEN,
917 		    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
918 
919 		arc_scsi_cmd_done(sc, ccb, reg);
920 	} while (nccb != ccb);
921 
922 	return (0);
923 }
924 
925 void
926 arc_minphys(struct buf *bp, struct scsi_link *sl)
927 {
928 	if (bp->b_bcount > MAXPHYS)
929 		bp->b_bcount = MAXPHYS;
930 	minphys(bp);
931 }
932 
933 int
934 arc_map_pci_resources(struct arc_softc *sc, struct pci_attach_args *pa)
935 {
936 	pcireg_t			memtype;
937 	pci_intr_handle_t		ih;
938 
939 	sc->sc_pc = pa->pa_pc;
940 	sc->sc_tag = pa->pa_tag;
941 	sc->sc_dmat = pa->pa_dmat;
942 
943 	memtype = pci_mapreg_type(sc->sc_pc, sc->sc_tag, ARC_RA_PCI_BAR);
944 	if (pci_mapreg_map(pa, ARC_RA_PCI_BAR, memtype, 0, &sc->sc_iot,
945 	    &sc->sc_ioh, NULL, &sc->sc_ios, 0) != 0) {
946 		printf(": unable to map system interface register\n");
947 		return(1);
948 	}
949 
950 	if (pci_intr_map(pa, &ih) != 0) {
951 		printf(": unable to map interrupt\n");
952 		goto unmap;
953 	}
954 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
955 	    arc_intr, sc, DEVNAME(sc));
956 	if (sc->sc_ih == NULL) {
957 		printf(": unable to map interrupt\n");
958 		goto unmap;
959 	}
960 	printf(": %s\n", pci_intr_string(pa->pa_pc, ih));
961 
962 	return (0);
963 
964 unmap:
965 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
966 	sc->sc_ios = 0;
967 	return (1);
968 }
969 
970 void
971 arc_unmap_pci_resources(struct arc_softc *sc)
972 {
973 	pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
974 	bus_space_unmap(sc->sc_iot, sc->sc_ioh, sc->sc_ios);
975 	sc->sc_ios = 0;
976 }
977 
978 int
979 arc_intel_query_firmware(struct arc_softc *sc)
980 {
981 	struct arc_msg_firmware_info	fwinfo;
982 	char				string[81]; /* sizeof(vendor)*2+1 */
983 
984 	if (arc_wait_eq(sc, ARC_RA_OUTB_ADDR1, ARC_RA_OUTB_ADDR1_FIRMWARE_OK,
985 	    ARC_RA_OUTB_ADDR1_FIRMWARE_OK) != 0) {
986 		printf("%s: timeout waiting for firmware ok\n", DEVNAME(sc));
987 		return (1);
988 	}
989 
990 	if (arc_msg0(sc, ARC_RA_INB_MSG0_GET_CONFIG) != 0) {
991 		printf("%s: timeout waiting for get config\n", DEVNAME(sc));
992 		return (1);
993 	}
994 
995 	arc_read_region(sc, ARC_RA_MSGBUF, &fwinfo, sizeof(fwinfo));
996 
997 	DNPRINTF(ARC_D_INIT, "%s: signature: 0x%08x\n", DEVNAME(sc),
998 	    letoh32(fwinfo.signature));
999 
1000 	if (letoh32(fwinfo.signature) != ARC_FWINFO_SIGNATURE_GET_CONFIG) {
1001 		printf("%s: invalid firmware info from iop\n", DEVNAME(sc));
1002 		return (1);
1003 	}
1004 
1005 	DNPRINTF(ARC_D_INIT, "%s: request_len: %d\n", DEVNAME(sc),
1006 	    letoh32(fwinfo.request_len));
1007 	DNPRINTF(ARC_D_INIT, "%s: queue_len: %d\n", DEVNAME(sc),
1008 	    letoh32(fwinfo.queue_len));
1009 	DNPRINTF(ARC_D_INIT, "%s: sdram_size: %d\n", DEVNAME(sc),
1010 	    letoh32(fwinfo.sdram_size));
1011 	DNPRINTF(ARC_D_INIT, "%s: sata_ports: %d\n", DEVNAME(sc),
1012 	    letoh32(fwinfo.sata_ports), letoh32(fwinfo.sata_ports));
1013 
1014 #ifdef ARC_DEBUG
1015 	scsi_strvis(string, fwinfo.vendor, sizeof(fwinfo.vendor));
1016 	DNPRINTF(ARC_D_INIT, "%s: vendor: \"%s\"\n", DEVNAME(sc), string);
1017 	scsi_strvis(string, fwinfo.model, sizeof(fwinfo.model));
1018 	DNPRINTF(ARC_D_INIT, "%s: model: \"%s\"\n", DEVNAME(sc), string);
1019 #endif /* ARC_DEBUG */
1020 
1021 	scsi_strvis(string, fwinfo.fw_version, sizeof(fwinfo.fw_version));
1022 	DNPRINTF(ARC_D_INIT, "%s: model: \"%s\"\n", DEVNAME(sc), string);
1023 
1024 	if (letoh32(fwinfo.request_len) != ARC_MAX_IOCMDLEN) {
1025 		printf("%s: unexpected request frame size (%d != %d)\n",
1026 		    DEVNAME(sc), letoh32(fwinfo.request_len), ARC_MAX_IOCMDLEN);
1027 		return (1);
1028 	}
1029 
1030 	sc->sc_req_count = letoh32(fwinfo.queue_len);
1031 
1032 	if (arc_msg0(sc, ARC_RA_INB_MSG0_START_BGRB) != 0) {
1033 		printf("%s: timeout waiting to start bg rebuild\n",
1034 		    DEVNAME(sc));
1035 		return (1);
1036 	}
1037 
1038 	printf("%s: %d ports, %dMB SDRAM, firmware %s\n",
1039 	    DEVNAME(sc), letoh32(fwinfo.sata_ports),
1040 	    letoh32(fwinfo.sdram_size), string);
1041 
1042 	return (0);
1043 }
1044 
1045 int
1046 arc_marvell_query_firmware(struct arc_softc *sc)
1047 {
1048 	if (arc_wait_eq(sc, ARC_RB_IOP2DRV_DOORBELL,
1049 	    ARC_RA_OUTB_ADDR1_FIRMWARE_OK,
1050 	    ARC_RA_OUTB_ADDR1_FIRMWARE_OK) != 0) {
1051 		printf("%s: timeout waiting for firmware ok\n", DEVNAME(sc));
1052 		return (1);
1053 	}
1054 
1055 	return (1);
1056 }
1057 
1058 #if NBIO > 0
1059 int
1060 arc_bioctl(struct device *self, u_long cmd, caddr_t addr)
1061 {
1062 	struct arc_softc		*sc = (struct arc_softc *)self;
1063 	int				error = 0;
1064 
1065 	switch (cmd) {
1066 	case BIOCINQ:
1067 		error = arc_bio_inq(sc, (struct bioc_inq *)addr);
1068 		break;
1069 
1070 	case BIOCVOL:
1071 		error = arc_bio_vol(sc, (struct bioc_vol *)addr);
1072 		break;
1073 
1074 	case BIOCDISK:
1075 		error = arc_bio_disk(sc, (struct bioc_disk *)addr);
1076 		break;
1077 
1078 	case BIOCALARM:
1079 		error = arc_bio_alarm(sc, (struct bioc_alarm *)addr);
1080 		break;
1081 
1082 	case BIOCBLINK:
1083 		error = arc_bio_blink(sc, (struct bioc_blink *)addr);
1084 		break;
1085 
1086 	default:
1087 		error = ENOTTY;
1088 		break;
1089 	}
1090 
1091 	return (error);
1092 }
1093 
1094 int
1095 arc_bio_alarm(struct arc_softc *sc, struct bioc_alarm *ba)
1096 {
1097 	u_int8_t			request[2];
1098 	u_int8_t			reply[1];
1099 	size_t				len;
1100 	int				error = 0;
1101 
1102 	switch (ba->ba_opcode) {
1103 	case BIOC_SAENABLE:
1104 	case BIOC_SADISABLE:
1105 		request[0] = ARC_FW_SET_ALARM;
1106 		request[1] = (ba->ba_opcode == BIOC_SAENABLE) ?
1107 		    ARC_FW_SET_ALARM_ENABLE : ARC_FW_SET_ALARM_DISABLE;
1108 		len = sizeof(request);
1109 
1110 		break;
1111 
1112 	case BIOC_SASILENCE:
1113 		request[0] = ARC_FW_MUTE_ALARM;
1114 		len = 1;
1115 
1116 		break;
1117 
1118 	case BIOC_GASTATUS:
1119 		/* system info is too big/ugly to deal with here */
1120 		return (arc_bio_alarm_state(sc, ba));
1121 
1122 	default:
1123 		return (EOPNOTSUPP);
1124 	}
1125 
1126 	arc_lock(sc);
1127 	error = arc_msgbuf(sc, request, len, reply, sizeof(reply), 0);
1128 	arc_unlock(sc);
1129 
1130 	if (error != 0)
1131 		return (error);
1132 
1133 	switch (reply[0]) {
1134 	case ARC_FW_CMD_OK:
1135 		return (0);
1136 	case ARC_FW_CMD_PASS_REQD:
1137 		return (EPERM);
1138 	default:
1139 		return (EIO);
1140 	}
1141 }
1142 
1143 int
1144 arc_bio_alarm_state(struct arc_softc *sc, struct bioc_alarm *ba)
1145 {
1146 	u_int8_t			request = ARC_FW_SYSINFO;
1147 	struct arc_fw_sysinfo		*sysinfo;
1148 	int				error = 0;
1149 
1150 	sysinfo = malloc(sizeof(struct arc_fw_sysinfo), M_TEMP, M_WAITOK);
1151 
1152 	request = ARC_FW_SYSINFO;
1153 
1154 	arc_lock(sc);
1155 	error = arc_msgbuf(sc, &request, sizeof(request),
1156 	    sysinfo, sizeof(struct arc_fw_sysinfo), 0);
1157 	arc_unlock(sc);
1158 
1159 	if (error != 0)
1160 		goto out;
1161 
1162 	ba->ba_status = sysinfo->alarm;
1163 
1164 out:
1165 	free(sysinfo, M_TEMP);
1166 	return (error);
1167 }
1168 
1169 
1170 int
1171 arc_bio_inq(struct arc_softc *sc, struct bioc_inq *bi)
1172 {
1173 	u_int8_t			request[2];
1174 	struct arc_fw_sysinfo		*sysinfo;
1175 	struct arc_fw_volinfo		*volinfo;
1176 	int				maxvols, nvols = 0, i;
1177 	int				error = 0;
1178 
1179 	sysinfo = malloc(sizeof(struct arc_fw_sysinfo), M_TEMP, M_WAITOK);
1180 	volinfo = malloc(sizeof(struct arc_fw_volinfo), M_TEMP, M_WAITOK);
1181 
1182 	arc_lock(sc);
1183 
1184 	request[0] = ARC_FW_SYSINFO;
1185 	error = arc_msgbuf(sc, request, 1, sysinfo,
1186 	    sizeof(struct arc_fw_sysinfo), 0);
1187 	if (error != 0)
1188 		goto out;
1189 
1190 	maxvols = sysinfo->max_volume_set;
1191 
1192 	request[0] = ARC_FW_VOLINFO;
1193 	for (i = 0; i < maxvols; i++) {
1194 		request[1] = i;
1195 		error = arc_msgbuf(sc, request, sizeof(request), volinfo,
1196 		    sizeof(struct arc_fw_volinfo), 0);
1197 		if (error != 0)
1198 			goto out;
1199 
1200 		/*
1201 		 * I can't find an easy way to see if the volume exists or not
1202 		 * except to say that if it has no capacity then it isn't there.
1203 		 * Ignore passthru volumes, bioc_vol doesn't understand them.
1204 		 */
1205 		if ((volinfo->capacity != 0 || volinfo->capacity2 != 0) &&
1206 		    volinfo->raid_level != ARC_FW_VOL_RAIDLEVEL_PASSTHRU)
1207 			nvols++;
1208 	}
1209 
1210 	strlcpy(bi->bi_dev, DEVNAME(sc), sizeof(bi->bi_dev));
1211 	bi->bi_novol = nvols;
1212 out:
1213 	arc_unlock(sc);
1214 	free(volinfo, M_TEMP);
1215 	free(sysinfo, M_TEMP);
1216 	return (error);
1217 }
1218 
1219 int
1220 arc_bio_blink(struct arc_softc *sc, struct bioc_blink *blink)
1221 {
1222 	u_int8_t			 request[5];
1223 	u_int32_t			 mask;
1224 	int				 error = 0;
1225 
1226 	request[0] = ARC_FW_BLINK;
1227 	request[1] = ARC_FW_BLINK_ENABLE;
1228 
1229 	switch (blink->bb_status) {
1230 	case BIOC_SBUNBLINK:
1231 		sc->sc_ledmask &= ~(1 << blink->bb_target);
1232 		break;
1233 	case BIOC_SBBLINK:
1234 		sc->sc_ledmask |= (1 << blink->bb_target);
1235 		break;
1236 	default:
1237 		return (EINVAL);
1238 	}
1239 
1240 	mask = htole32(sc->sc_ledmask);
1241 	bcopy(&mask, &request[2], 3);
1242 
1243 	error = arc_msgbuf(sc, request, sizeof(request), NULL, 0, 0);
1244 	if (error)
1245 		return (EIO);
1246 
1247 	return (0);
1248 }
1249 
1250 int
1251 arc_bio_getvol(struct arc_softc *sc, int vol, struct arc_fw_volinfo *volinfo)
1252 {
1253 	u_int8_t			request[2];
1254 	struct arc_fw_sysinfo		*sysinfo;
1255 	int				error = 0;
1256 	int				maxvols, nvols = 0, i;
1257 
1258 	sysinfo = malloc(sizeof(struct arc_fw_sysinfo), M_TEMP, M_WAITOK);
1259 
1260 	request[0] = ARC_FW_SYSINFO;
1261 	error = arc_msgbuf(sc, request, 1, sysinfo,
1262 	    sizeof(struct arc_fw_sysinfo), 0);
1263 	if (error != 0)
1264 		goto out;
1265 
1266 	maxvols = sysinfo->max_volume_set;
1267 
1268 	request[0] = ARC_FW_VOLINFO;
1269 	for (i = 0; i < maxvols; i++) {
1270 		request[1] = i;
1271 		error = arc_msgbuf(sc, request, sizeof(request), volinfo,
1272 		    sizeof(struct arc_fw_volinfo), 0);
1273 		if (error != 0)
1274 			goto out;
1275 
1276 		if ((volinfo->capacity == 0 && volinfo->capacity2 == 0) ||
1277 		    volinfo->raid_level == ARC_FW_VOL_RAIDLEVEL_PASSTHRU)
1278 			continue;
1279 
1280 		if (nvols == vol)
1281 			break;
1282 
1283 		nvols++;
1284 	}
1285 
1286 	if (nvols != vol ||
1287 	    (volinfo->capacity == 0 && volinfo->capacity2 == 0) ||
1288 	    volinfo->raid_level == ARC_FW_VOL_RAIDLEVEL_PASSTHRU) {
1289 		error = ENODEV;
1290 		goto out;
1291 	}
1292 
1293 out:
1294 	free(sysinfo, M_TEMP);
1295 	return (error);
1296 }
1297 
1298 int
1299 arc_bio_vol(struct arc_softc *sc, struct bioc_vol *bv)
1300 {
1301 	struct arc_fw_volinfo		*volinfo;
1302 	struct scsi_link		*sc_link;
1303 	struct device			*dev;
1304 	u_int64_t			blocks;
1305 	u_int32_t			status;
1306 	int				error = 0;
1307 
1308 	volinfo = malloc(sizeof(struct arc_fw_volinfo), M_TEMP, M_WAITOK);
1309 
1310 	arc_lock(sc);
1311 	error = arc_bio_getvol(sc, bv->bv_volid, volinfo);
1312 	arc_unlock(sc);
1313 
1314 	if (error != 0)
1315 		goto out;
1316 
1317 	bv->bv_percent = -1;
1318 	bv->bv_seconds = 0;
1319 
1320 	status = letoh32(volinfo->volume_status);
1321 	if (status == 0x0) {
1322 		if (letoh32(volinfo->fail_mask) == 0x0)
1323 			bv->bv_status = BIOC_SVONLINE;
1324 		else
1325 			bv->bv_status = BIOC_SVDEGRADED;
1326 	} else if (status & ARC_FW_VOL_STATUS_NEED_REGEN)
1327 		bv->bv_status = BIOC_SVDEGRADED;
1328 	else if (status & ARC_FW_VOL_STATUS_FAILED)
1329 		bv->bv_status = BIOC_SVOFFLINE;
1330 	else if (status & ARC_FW_VOL_STATUS_INITTING) {
1331 		bv->bv_status = BIOC_SVBUILDING;
1332 		bv->bv_percent = letoh32(volinfo->progress) / 10;
1333 	} else if (status & ARC_FW_VOL_STATUS_REBUILDING) {
1334 		bv->bv_status = BIOC_SVREBUILD;
1335 		bv->bv_percent = letoh32(volinfo->progress) / 10;
1336 	}
1337 
1338 	blocks = (u_int64_t)letoh32(volinfo->capacity2) << 32;
1339 	blocks += (u_int64_t)letoh32(volinfo->capacity);
1340 	bv->bv_size = blocks * ARC_BLOCKSIZE; /* XXX */
1341 
1342 	switch (volinfo->raid_level) {
1343 	case ARC_FW_VOL_RAIDLEVEL_0:
1344 		bv->bv_level = 0;
1345 		break;
1346 	case ARC_FW_VOL_RAIDLEVEL_1:
1347 		bv->bv_level = 1;
1348 		break;
1349 	case ARC_FW_VOL_RAIDLEVEL_3:
1350 		bv->bv_level = 3;
1351 		break;
1352 	case ARC_FW_VOL_RAIDLEVEL_5:
1353 		bv->bv_level = 5;
1354 		break;
1355 	case ARC_FW_VOL_RAIDLEVEL_6:
1356 		bv->bv_level = 6;
1357 		break;
1358 	case ARC_FW_VOL_RAIDLEVEL_PASSTHRU:
1359 	default:
1360 		bv->bv_level = -1;
1361 		break;
1362 	}
1363 
1364 	bv->bv_nodisk = volinfo->member_disks;
1365 	sc_link = sc->sc_scsibus->sc_link[volinfo->scsi_attr.target]
1366 	    [volinfo->scsi_attr.lun];
1367 	if (sc_link != NULL) {
1368 		dev = sc_link->device_softc;
1369 		strlcpy(bv->bv_dev, dev->dv_xname, sizeof(bv->bv_dev));
1370 	}
1371 
1372 out:
1373 	free(volinfo, M_TEMP);
1374 	return (error);
1375 }
1376 
1377 int
1378 arc_bio_disk(struct arc_softc *sc, struct bioc_disk *bd)
1379 {
1380 	u_int8_t			request[2];
1381 	struct arc_fw_volinfo		*volinfo;
1382 	struct arc_fw_raidinfo		*raidinfo;
1383 	struct arc_fw_diskinfo		*diskinfo;
1384 	int				error = 0;
1385 	u_int64_t			blocks;
1386 	char				model[81];
1387 	char				serial[41];
1388 	char				rev[17];
1389 
1390 	volinfo = malloc(sizeof(struct arc_fw_volinfo), M_TEMP, M_WAITOK);
1391 	raidinfo = malloc(sizeof(struct arc_fw_raidinfo), M_TEMP, M_WAITOK);
1392 	diskinfo = malloc(sizeof(struct arc_fw_diskinfo), M_TEMP, M_WAITOK);
1393 
1394 	arc_lock(sc);
1395 
1396 	error = arc_bio_getvol(sc, bd->bd_volid, volinfo);
1397 	if (error != 0)
1398 		goto out;
1399 
1400 	request[0] = ARC_FW_RAIDINFO;
1401 	request[1] = volinfo->raid_set_number;
1402 	error = arc_msgbuf(sc, request, sizeof(request), raidinfo,
1403 	    sizeof(struct arc_fw_raidinfo), 0);
1404 	if (error != 0)
1405 		goto out;
1406 
1407 	if (bd->bd_diskid > raidinfo->member_devices) {
1408 		error = ENODEV;
1409 		goto out;
1410 	}
1411 
1412 	if (raidinfo->device_array[bd->bd_diskid] == 0xff) {
1413 		/*
1414 		 * the disk doesn't exist anymore. bio is too dumb to be
1415 		 * able to display that, so put it on another bus
1416 		 */
1417 		bd->bd_channel = 1;
1418 		bd->bd_target = 0;
1419 		bd->bd_lun = 0;
1420 		bd->bd_status = BIOC_SDOFFLINE;
1421 		strlcpy(bd->bd_vendor, "disk missing", sizeof(bd->bd_vendor));
1422 		goto out;
1423 	}
1424 
1425 	request[0] = ARC_FW_DISKINFO;
1426 	request[1] = raidinfo->device_array[bd->bd_diskid];
1427 	error = arc_msgbuf(sc, request, sizeof(request), diskinfo,
1428 	    sizeof(struct arc_fw_diskinfo), 1);
1429 	if (error != 0)
1430 		goto out;
1431 
1432 #if 0
1433 	bd->bd_channel = diskinfo->scsi_attr.channel;
1434 	bd->bd_target = diskinfo->scsi_attr.target;
1435 	bd->bd_lun = diskinfo->scsi_attr.lun;
1436 #endif
1437 	/*
1438 	 * the firwmare doesnt seem to fill scsi_attr in, so fake it with
1439 	 * the diskid.
1440 	 */
1441 	bd->bd_channel = 0;
1442 	bd->bd_target = raidinfo->device_array[bd->bd_diskid];
1443 	bd->bd_lun = 0;
1444 
1445 	bd->bd_status = BIOC_SDONLINE;
1446 	blocks = (u_int64_t)letoh32(diskinfo->capacity2) << 32;
1447 	blocks += (u_int64_t)letoh32(diskinfo->capacity);
1448 	bd->bd_size = blocks * ARC_BLOCKSIZE; /* XXX */
1449 
1450 	scsi_strvis(model, diskinfo->model, sizeof(diskinfo->model));
1451 	scsi_strvis(serial, diskinfo->serial, sizeof(diskinfo->serial));
1452 	scsi_strvis(rev, diskinfo->firmware_rev,
1453 	    sizeof(diskinfo->firmware_rev));
1454 
1455 	snprintf(bd->bd_vendor, sizeof(bd->bd_vendor), "%s %s",
1456 	    model, rev);
1457 	strlcpy(bd->bd_serial, serial, sizeof(bd->bd_serial));
1458 
1459 out:
1460 	arc_unlock(sc);
1461 	free(diskinfo, M_TEMP);
1462 	free(raidinfo, M_TEMP);
1463 	free(volinfo, M_TEMP);
1464 	return (error);
1465 }
1466 
1467 u_int8_t
1468 arc_msg_cksum(void *cmd, u_int16_t len)
1469 {
1470 	u_int8_t			*buf = cmd;
1471 	u_int8_t			cksum;
1472 	int				i;
1473 
1474 	cksum = (u_int8_t)(len >> 8) + (u_int8_t)len;
1475 	for (i = 0; i < len; i++)
1476 		cksum += buf[i];
1477 
1478 	return (cksum);
1479 }
1480 
1481 
1482 int
1483 arc_msgbuf(struct arc_softc *sc, void *wptr, size_t wbuflen, void *rptr,
1484     size_t rbuflen, int sreadok)
1485 {
1486 	u_int8_t			rwbuf[ARC_RA_IOC_RWBUF_MAXLEN];
1487 	u_int8_t			*wbuf, *rbuf;
1488 	int				wlen, wdone = 0, rlen, rdone = 0;
1489 	u_int16_t			rlenhdr = 0;
1490 	struct arc_fw_bufhdr		*bufhdr;
1491 	u_int32_t			reg, rwlen;
1492 	int				error = 0;
1493 #ifdef ARC_DEBUG
1494 	int				i;
1495 #endif
1496 
1497 	DNPRINTF(ARC_D_DB, "%s: arc_msgbuf wbuflen: %d rbuflen: %d\n",
1498 	    DEVNAME(sc), wbuflen, rbuflen);
1499 
1500 	if (arc_read(sc, ARC_RA_OUTB_DOORBELL) != 0)
1501 		return (EBUSY);
1502 
1503 	wlen = sizeof(struct arc_fw_bufhdr) + wbuflen + 1; /* 1 for cksum */
1504 	wbuf = malloc(wlen, M_TEMP, M_WAITOK);
1505 
1506 	rlen = sizeof(struct arc_fw_bufhdr) + rbuflen + 1; /* 1 for cksum */
1507 	rbuf = malloc(rlen, M_TEMP, M_WAITOK);
1508 
1509 	DNPRINTF(ARC_D_DB, "%s: arc_msgbuf wlen: %d rlen: %d\n", DEVNAME(sc),
1510 	    wlen, rlen);
1511 
1512 	bufhdr = (struct arc_fw_bufhdr *)wbuf;
1513 	bufhdr->hdr = arc_fw_hdr;
1514 	bufhdr->len = htole16(wbuflen);
1515 	bcopy(wptr, wbuf + sizeof(struct arc_fw_bufhdr), wbuflen);
1516 	wbuf[wlen - 1] = arc_msg_cksum(wptr, wbuflen);
1517 
1518 	reg = ARC_RA_OUTB_DOORBELL_READ_OK;
1519 
1520 	do {
1521 		if ((reg & ARC_RA_OUTB_DOORBELL_READ_OK) && wdone < wlen) {
1522 			bzero(rwbuf, sizeof(rwbuf));
1523 			rwlen = (wlen - wdone) % sizeof(rwbuf);
1524 			bcopy(&wbuf[wdone], rwbuf, rwlen);
1525 
1526 #ifdef ARC_DEBUG
1527 			if (arcdebug & ARC_D_DB) {
1528 				printf("%s: write %d:", DEVNAME(sc), rwlen);
1529 				for (i = 0; i < rwlen; i++)
1530 					printf(" 0x%02x", rwbuf[i]);
1531 				printf("\n");
1532 			}
1533 #endif
1534 
1535 			/* copy the chunk to the hw */
1536 			arc_write(sc, ARC_RA_IOC_WBUF_LEN, rwlen);
1537 			arc_write_region(sc, ARC_RA_IOC_WBUF, rwbuf,
1538 			    sizeof(rwbuf));
1539 
1540 			/* say we have a buffer for the hw */
1541 			arc_write(sc, ARC_RA_INB_DOORBELL,
1542 			    ARC_RA_INB_DOORBELL_WRITE_OK);
1543 
1544 			wdone += rwlen;
1545 		}
1546 
1547 		if (rptr == NULL)
1548 			goto out;
1549 
1550 		while ((reg = arc_read(sc, ARC_RA_OUTB_DOORBELL)) == 0)
1551 			arc_wait(sc);
1552 		arc_write(sc, ARC_RA_OUTB_DOORBELL, reg);
1553 
1554 		DNPRINTF(ARC_D_DB, "%s: reg: 0x%08x\n", DEVNAME(sc), reg);
1555 
1556 		if ((reg & ARC_RA_OUTB_DOORBELL_WRITE_OK) && rdone < rlen) {
1557 			rwlen = arc_read(sc, ARC_RA_IOC_RBUF_LEN);
1558 			if (rwlen > sizeof(rwbuf)) {
1559 				DNPRINTF(ARC_D_DB, "%s:  rwlen too big\n",
1560 				    DEVNAME(sc));
1561 				error = EIO;
1562 				goto out;
1563 			}
1564 
1565 			arc_read_region(sc, ARC_RA_IOC_RBUF, rwbuf,
1566 			    sizeof(rwbuf));
1567 
1568 			arc_write(sc, ARC_RA_INB_DOORBELL,
1569 			    ARC_RA_INB_DOORBELL_READ_OK);
1570 
1571 #ifdef ARC_DEBUG
1572 			printf("%s:  len: %d+%d=%d/%d\n", DEVNAME(sc),
1573 			    rwlen, rdone, rwlen + rdone, rlen);
1574 			if (arcdebug & ARC_D_DB) {
1575 				printf("%s: read:", DEVNAME(sc));
1576 				for (i = 0; i < rwlen; i++)
1577 					printf(" 0x%02x", rwbuf[i]);
1578 				printf("\n");
1579 			}
1580 #endif
1581 
1582 			if ((rdone + rwlen) > rlen) {
1583 				DNPRINTF(ARC_D_DB, "%s:  rwbuf too big\n",
1584 				    DEVNAME(sc));
1585 				error = EIO;
1586 				goto out;
1587 			}
1588 
1589 			bcopy(rwbuf, &rbuf[rdone], rwlen);
1590 			rdone += rwlen;
1591 
1592 			/*
1593 			 * Allow for short reads, by reading the length
1594 			 * value from the response header and shrinking our
1595 			 * idea of size, if required.
1596 			 * This deals with the growth of diskinfo struct from
1597 			 * 128 to 132 bytes.
1598 			 */
1599 			if (sreadok && rdone >= sizeof(struct arc_fw_bufhdr) &&
1600 			    rlenhdr == 0) {
1601 				bufhdr = (struct arc_fw_bufhdr *)rbuf;
1602 				rlenhdr = letoh16(bufhdr->len);
1603 				if (rlenhdr < rbuflen) {
1604 					rbuflen = rlenhdr;
1605 					rlen = sizeof(struct arc_fw_bufhdr) +
1606 					    rbuflen + 1; /* 1 for cksum */
1607 				}
1608 			}
1609 		}
1610 	} while (rdone != rlen);
1611 
1612 	bufhdr = (struct arc_fw_bufhdr *)rbuf;
1613 	if (memcmp(&bufhdr->hdr, &arc_fw_hdr, sizeof(bufhdr->hdr)) != 0 ||
1614 	    bufhdr->len != htole16(rbuflen)) {
1615 		DNPRINTF(ARC_D_DB, "%s:  rbuf hdr is wrong\n", DEVNAME(sc));
1616 		error = EIO;
1617 		goto out;
1618 	}
1619 
1620 	bcopy(rbuf + sizeof(struct arc_fw_bufhdr), rptr, rbuflen);
1621 
1622 	if (rbuf[rlen - 1] != arc_msg_cksum(rptr, rbuflen)) {
1623 		DNPRINTF(ARC_D_DB, "%s:  invalid cksum\n", DEVNAME(sc));
1624 		error = EIO;
1625 		goto out;
1626 	}
1627 
1628 out:
1629 	free(wbuf, M_TEMP);
1630 	free(rbuf, M_TEMP);
1631 
1632 	return (error);
1633 }
1634 
1635 void
1636 arc_lock(struct arc_softc *sc)
1637 {
1638 	int				s;
1639 
1640 	rw_enter_write(&sc->sc_lock);
1641 	s = splbio();
1642 	arc_write(sc, ARC_RA_INTRMASK, ~ARC_RA_INTRMASK_POSTQUEUE);
1643 	sc->sc_talking = 1;
1644 	splx(s);
1645 }
1646 
1647 void
1648 arc_unlock(struct arc_softc *sc)
1649 {
1650 	int				s;
1651 
1652 	s = splbio();
1653 	sc->sc_talking = 0;
1654 	arc_write(sc, ARC_RA_INTRMASK,
1655 	    ~(ARC_RA_INTRMASK_POSTQUEUE|ARC_RA_INTRMASK_DOORBELL));
1656 	splx(s);
1657 	rw_exit_write(&sc->sc_lock);
1658 }
1659 
1660 void
1661 arc_wait(struct arc_softc *sc)
1662 {
1663 	int				s;
1664 
1665 	s = splbio();
1666 	arc_write(sc, ARC_RA_INTRMASK,
1667 	    ~(ARC_RA_INTRMASK_POSTQUEUE|ARC_RA_INTRMASK_DOORBELL));
1668 	if (tsleep(sc, PWAIT, "arcdb", hz) == EWOULDBLOCK)
1669 		arc_write(sc, ARC_RA_INTRMASK, ~ARC_RA_INTRMASK_POSTQUEUE);
1670 	splx(s);
1671 }
1672 
1673 #ifndef SMALL_KERNEL
1674 void
1675 arc_create_sensors(void *xsc, void *arg)
1676 {
1677 	struct arc_softc	*sc = xsc;
1678 	struct bioc_inq		bi;
1679 	struct bioc_vol		bv;
1680 	int			i;
1681 
1682 	/*
1683 	 * XXX * this is bollocks. the firmware has garbage coming out of it
1684 	 * so we have to wait a bit for it to finish spewing.
1685 	 */
1686 	tsleep(sc, PWAIT, "arcspew", 2 * hz);
1687 
1688 	bzero(&bi, sizeof(bi));
1689 	if (arc_bio_inq(sc, &bi) != 0) {
1690 		printf("%s: unable to query firmware for sensor info\n",
1691 		    DEVNAME(sc));
1692 		return;
1693 	}
1694 	sc->sc_nsensors = bi.bi_novol;
1695 
1696 	sc->sc_sensors = malloc(sizeof(struct ksensor) * sc->sc_nsensors,
1697 	    M_DEVBUF, M_WAITOK | M_ZERO);
1698 
1699 	strlcpy(sc->sc_sensordev.xname, DEVNAME(sc),
1700 	    sizeof(sc->sc_sensordev.xname));
1701 
1702 	for (i = 0; i < sc->sc_nsensors; i++) {
1703 		bzero(&bv, sizeof(bv));
1704 		bv.bv_volid = i;
1705 		if (arc_bio_vol(sc, &bv) != 0)
1706 			goto bad;
1707 
1708 		sc->sc_sensors[i].type = SENSOR_DRIVE;
1709 		sc->sc_sensors[i].status = SENSOR_S_UNKNOWN;
1710 
1711 		strlcpy(sc->sc_sensors[i].desc, bv.bv_dev,
1712 		    sizeof(sc->sc_sensors[i].desc));
1713 
1714 		sensor_attach(&sc->sc_sensordev, &sc->sc_sensors[i]);
1715 	}
1716 
1717 	if (sensor_task_register(sc, arc_refresh_sensors, 120) == NULL)
1718 		goto bad;
1719 
1720 	sensordev_install(&sc->sc_sensordev);
1721 
1722 	return;
1723 
1724 bad:
1725 	free(sc->sc_sensors, M_DEVBUF);
1726 }
1727 
1728 void
1729 arc_refresh_sensors(void *arg)
1730 {
1731 	struct arc_softc	*sc = arg;
1732 	struct bioc_vol		bv;
1733 	int			i;
1734 
1735 	for (i = 0; i < sc->sc_nsensors; i++) {
1736 		bzero(&bv, sizeof(bv));
1737 		bv.bv_volid = i;
1738 		if (arc_bio_vol(sc, &bv)) {
1739 			sc->sc_sensors[i].flags = SENSOR_FINVALID;
1740 			return;
1741 		}
1742 
1743 		switch(bv.bv_status) {
1744 		case BIOC_SVOFFLINE:
1745 			sc->sc_sensors[i].value = SENSOR_DRIVE_FAIL;
1746 			sc->sc_sensors[i].status = SENSOR_S_CRIT;
1747 			break;
1748 
1749 		case BIOC_SVDEGRADED:
1750 			sc->sc_sensors[i].value = SENSOR_DRIVE_PFAIL;
1751 			sc->sc_sensors[i].status = SENSOR_S_WARN;
1752 			break;
1753 
1754 		case BIOC_SVSCRUB:
1755 		case BIOC_SVONLINE:
1756 			sc->sc_sensors[i].value = SENSOR_DRIVE_ONLINE;
1757 			sc->sc_sensors[i].status = SENSOR_S_OK;
1758 			break;
1759 
1760 		case BIOC_SVINVALID:
1761 			/* FALLTRHOUGH */
1762 		default:
1763 			sc->sc_sensors[i].value = 0; /* unknown */
1764 			sc->sc_sensors[i].status = SENSOR_S_UNKNOWN;
1765 		}
1766 
1767 	}
1768 }
1769 #endif /* SMALL_KERNEL */
1770 #endif /* NBIO > 0 */
1771 
1772 u_int32_t
1773 arc_read(struct arc_softc *sc, bus_size_t r)
1774 {
1775 	u_int32_t			v;
1776 
1777 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
1778 	    BUS_SPACE_BARRIER_READ);
1779 	v = bus_space_read_4(sc->sc_iot, sc->sc_ioh, r);
1780 
1781 	DNPRINTF(ARC_D_RW, "%s: arc_read 0x%x 0x%08x\n", DEVNAME(sc), r, v);
1782 
1783 	return (v);
1784 }
1785 
1786 void
1787 arc_read_region(struct arc_softc *sc, bus_size_t r, void *buf, size_t len)
1788 {
1789 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, len,
1790 	    BUS_SPACE_BARRIER_READ);
1791 	bus_space_read_raw_region_4(sc->sc_iot, sc->sc_ioh, r, buf, len);
1792 }
1793 
1794 void
1795 arc_write(struct arc_softc *sc, bus_size_t r, u_int32_t v)
1796 {
1797 	DNPRINTF(ARC_D_RW, "%s: arc_write 0x%x 0x%08x\n", DEVNAME(sc), r, v);
1798 
1799 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, r, v);
1800 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, 4,
1801 	    BUS_SPACE_BARRIER_WRITE);
1802 }
1803 
1804 void
1805 arc_write_region(struct arc_softc *sc, bus_size_t r, void *buf, size_t len)
1806 {
1807 	bus_space_write_raw_region_4(sc->sc_iot, sc->sc_ioh, r, buf, len);
1808 	bus_space_barrier(sc->sc_iot, sc->sc_ioh, r, len,
1809 	    BUS_SPACE_BARRIER_WRITE);
1810 }
1811 
1812 int
1813 arc_wait_eq(struct arc_softc *sc, bus_size_t r, u_int32_t mask,
1814     u_int32_t target)
1815 {
1816 	int				i;
1817 
1818 	DNPRINTF(ARC_D_RW, "%s: arc_wait_eq 0x%x 0x%08x 0x%08x\n",
1819 	    DEVNAME(sc), r, mask, target);
1820 
1821 	for (i = 0; i < 10000; i++) {
1822 		if ((arc_read(sc, r) & mask) == target)
1823 			return (0);
1824 		delay(1000);
1825 	}
1826 
1827 	return (1);
1828 }
1829 
1830 int
1831 arc_wait_ne(struct arc_softc *sc, bus_size_t r, u_int32_t mask,
1832     u_int32_t target)
1833 {
1834 	int				i;
1835 
1836 	DNPRINTF(ARC_D_RW, "%s: arc_wait_ne 0x%x 0x%08x 0x%08x\n",
1837 	    DEVNAME(sc), r, mask, target);
1838 
1839 	for (i = 0; i < 10000; i++) {
1840 		if ((arc_read(sc, r) & mask) != target)
1841 			return (0);
1842 		delay(1000);
1843 	}
1844 
1845 	return (1);
1846 }
1847 
1848 int
1849 arc_msg0(struct arc_softc *sc, u_int32_t m)
1850 {
1851 	/* post message */
1852 	arc_write(sc, ARC_RA_INB_MSG0, m);
1853 	/* wait for the fw to do it */
1854 	if (arc_wait_eq(sc, ARC_RA_INTRSTAT, ARC_RA_INTRSTAT_MSG0,
1855 	    ARC_RA_INTRSTAT_MSG0) != 0)
1856 		return (1);
1857 
1858 	/* ack it */
1859 	arc_write(sc, ARC_RA_INTRSTAT, ARC_RA_INTRSTAT_MSG0);
1860 
1861 	return (0);
1862 }
1863 
1864 struct arc_dmamem *
1865 arc_dmamem_alloc(struct arc_softc *sc, size_t size)
1866 {
1867 	struct arc_dmamem		*adm;
1868 	int				nsegs;
1869 
1870 	adm = malloc(sizeof(*adm), M_DEVBUF, M_NOWAIT | M_ZERO);
1871 	if (adm == NULL)
1872 		return (NULL);
1873 
1874 	adm->adm_size = size;
1875 
1876 	if (bus_dmamap_create(sc->sc_dmat, size, 1, size, 0,
1877 	    BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &adm->adm_map) != 0)
1878 		goto admfree;
1879 
1880 	if (bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &adm->adm_seg,
1881 	    1, &nsegs, BUS_DMA_NOWAIT) != 0)
1882 		goto destroy;
1883 
1884 	if (bus_dmamem_map(sc->sc_dmat, &adm->adm_seg, nsegs, size,
1885 	    &adm->adm_kva, BUS_DMA_NOWAIT) != 0)
1886 		goto free;
1887 
1888 	if (bus_dmamap_load(sc->sc_dmat, adm->adm_map, adm->adm_kva, size,
1889 	    NULL, BUS_DMA_NOWAIT) != 0)
1890 		goto unmap;
1891 
1892 	bzero(adm->adm_kva, size);
1893 
1894 	return (adm);
1895 
1896 unmap:
1897 	bus_dmamem_unmap(sc->sc_dmat, adm->adm_kva, size);
1898 free:
1899 	bus_dmamem_free(sc->sc_dmat, &adm->adm_seg, 1);
1900 destroy:
1901 	bus_dmamap_destroy(sc->sc_dmat, adm->adm_map);
1902 admfree:
1903 	free(adm, M_DEVBUF);
1904 
1905 	return (NULL);
1906 }
1907 
1908 void
1909 arc_dmamem_free(struct arc_softc *sc, struct arc_dmamem *adm)
1910 {
1911 	bus_dmamap_unload(sc->sc_dmat, adm->adm_map);
1912 	bus_dmamem_unmap(sc->sc_dmat, adm->adm_kva, adm->adm_size);
1913 	bus_dmamem_free(sc->sc_dmat, &adm->adm_seg, 1);
1914 	bus_dmamap_destroy(sc->sc_dmat, adm->adm_map);
1915 	free(adm, M_DEVBUF);
1916 }
1917 
1918 int
1919 arc_alloc_ccbs(struct arc_softc *sc)
1920 {
1921 	struct arc_ccb			*ccb;
1922 	u_int8_t			*cmd;
1923 	int				i;
1924 
1925 	TAILQ_INIT(&sc->sc_ccb_free);
1926 
1927 	sc->sc_ccbs = malloc(sizeof(struct arc_ccb) * sc->sc_req_count,
1928 	    M_DEVBUF, M_WAITOK | M_ZERO);
1929 
1930 	sc->sc_requests = arc_dmamem_alloc(sc,
1931 	    ARC_MAX_IOCMDLEN * sc->sc_req_count);
1932 	if (sc->sc_requests == NULL) {
1933 		printf("%s: unable to allocate ccb dmamem\n", DEVNAME(sc));
1934 		goto free_ccbs;
1935 	}
1936 	cmd = ARC_DMA_KVA(sc->sc_requests);
1937 
1938 	for (i = 0; i < sc->sc_req_count; i++) {
1939 		ccb = &sc->sc_ccbs[i];
1940 
1941 		if (bus_dmamap_create(sc->sc_dmat, MAXPHYS, ARC_SGL_MAXLEN,
1942 		    MAXPHYS, 0, 0, &ccb->ccb_dmamap) != 0) {
1943 			printf("%s: unable to create dmamap for ccb %d\n",
1944 			    DEVNAME(sc), i);
1945 			goto free_maps;
1946 		}
1947 
1948 		ccb->ccb_sc = sc;
1949 		ccb->ccb_id = i;
1950 		ccb->ccb_offset = ARC_MAX_IOCMDLEN * i;
1951 
1952 		ccb->ccb_cmd = (struct arc_io_cmd *)&cmd[ccb->ccb_offset];
1953 		ccb->ccb_cmd_post = (ARC_DMA_DVA(sc->sc_requests) +
1954 		    ccb->ccb_offset) >> ARC_RA_POST_QUEUE_ADDR_SHIFT;
1955 
1956 		arc_put_ccb(sc, ccb);
1957 	}
1958 
1959 	return (0);
1960 
1961 free_maps:
1962 	while ((ccb = arc_get_ccb(sc)) != NULL)
1963 	    bus_dmamap_destroy(sc->sc_dmat, ccb->ccb_dmamap);
1964 	arc_dmamem_free(sc, sc->sc_requests);
1965 
1966 free_ccbs:
1967 	free(sc->sc_ccbs, M_DEVBUF);
1968 
1969 	return (1);
1970 }
1971 
1972 struct arc_ccb *
1973 arc_get_ccb(struct arc_softc *sc)
1974 {
1975 	struct arc_ccb			*ccb;
1976 
1977 	ccb = TAILQ_FIRST(&sc->sc_ccb_free);
1978 	if (ccb != NULL)
1979 		TAILQ_REMOVE(&sc->sc_ccb_free, ccb, ccb_link);
1980 
1981 	return (ccb);
1982 }
1983 
1984 void
1985 arc_put_ccb(struct arc_softc *sc, struct arc_ccb *ccb)
1986 {
1987 	ccb->ccb_xs = NULL;
1988 	bzero(ccb->ccb_cmd, ARC_MAX_IOCMDLEN);
1989 	TAILQ_INSERT_TAIL(&sc->sc_ccb_free, ccb, ccb_link);
1990 }
1991