aboutsummaryrefslogtreecommitdiff
path: root/sys/dev/sfxge/common/efsys.h
blob: 0c1c84cbb4689a71f88f67c6aa6a758a07504b74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
/*-
 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
 *
 * Copyright (c) 2010-2016 Solarflare Communications Inc.
 * All rights reserved.
 *
 * This software was developed in part by Philip Paeps under contract for
 * Solarflare Communications, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of the FreeBSD Project.
 *
 * $FreeBSD$
 */

#ifndef	_SYS_EFSYS_H
#define	_SYS_EFSYS_H

#ifdef	__cplusplus
extern "C" {
#endif

#include <sys/param.h>
#include <sys/bus.h>
#include <sys/endian.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/mutex.h>
#include <sys/rwlock.h>
#include <sys/sdt.h>
#include <sys/systm.h>

#include <machine/bus.h>
#include <machine/endian.h>

#define	EFSYS_HAS_UINT64 1
#if defined(__x86_64__)
#define	EFSYS_USE_UINT64 1
#else
#define	EFSYS_USE_UINT64 0
#endif
#define	EFSYS_HAS_SSE2_M128 0
#if _BYTE_ORDER == _BIG_ENDIAN
#define	EFSYS_IS_BIG_ENDIAN 1
#define	EFSYS_IS_LITTLE_ENDIAN 0
#elif _BYTE_ORDER == _LITTLE_ENDIAN
#define	EFSYS_IS_BIG_ENDIAN 0
#define	EFSYS_IS_LITTLE_ENDIAN 1
#endif
#include "efx_types.h"

/* Common code requires this */
#if __FreeBSD_version < 800068
#define	memmove(d, s, l) bcopy(s, d, l)
#endif

#ifndef B_FALSE
#define	B_FALSE	FALSE
#endif
#ifndef B_TRUE
#define	B_TRUE	TRUE
#endif

#ifndef IS_P2ALIGNED
#define	IS_P2ALIGNED(v, a)	((((uintptr_t)(v)) & ((uintptr_t)(a) - 1)) == 0)
#endif

#ifndef P2ROUNDUP
#define	P2ROUNDUP(x, align)	(-(-(x) & -(align)))
#endif

#ifndef P2ALIGN
#define	P2ALIGN(_x, _a)		((_x) & -(_a))
#endif

#ifndef IS2P
#define	ISP2(x)			(((x) & ((x) - 1)) == 0)
#endif

#if defined(__x86_64__) && __FreeBSD_version >= 1000000

#define	SFXGE_USE_BUS_SPACE_8		1

#if !defined(bus_space_read_stream_8)

#define	bus_space_read_stream_8(t, h, o)				\
	bus_space_read_8((t), (h), (o))

#define	bus_space_write_stream_8(t, h, o, v)				\
	bus_space_write_8((t), (h), (o), (v))

#endif

#endif

#define	ENOTACTIVE EINVAL

/* Memory type to use on FreeBSD */
MALLOC_DECLARE(M_SFXGE);

/* Machine dependend prefetch wrappers */
#if defined(__i386__) || defined(__amd64__)
static __inline void
prefetch_read_many(void *addr)
{

	__asm__(
	    "prefetcht0 (%0)"
	    :
	    : "r" (addr));
}

static __inline void
prefetch_read_once(void *addr)
{

	__asm__(
	    "prefetchnta (%0)"
	    :
	    : "r" (addr));
}
#elif defined(__sparc64__)
static __inline void
prefetch_read_many(void *addr)
{

	__asm__(
	    "prefetch [%0], 0"
	    :
	    : "r" (addr));
}

static __inline void
prefetch_read_once(void *addr)
{

	__asm__(
	    "prefetch [%0], 1"
	    :
	    : "r" (addr));
}
#else
static __inline void
prefetch_read_many(void *addr)
{

}

static __inline void
prefetch_read_once(void *addr)
{

}
#endif

#if defined(__i386__) || defined(__amd64__)
#include <vm/vm.h>
#include <vm/pmap.h>
#endif
static __inline void
sfxge_map_mbuf_fast(bus_dma_tag_t tag, bus_dmamap_t map,
		    struct mbuf *m, bus_dma_segment_t *seg)
{
#if defined(__i386__) || defined(__amd64__)
	seg->ds_addr = pmap_kextract(mtod(m, vm_offset_t));
	seg->ds_len = m->m_len;
#else
	int nsegstmp;

	bus_dmamap_load_mbuf_sg(tag, map, m, seg, &nsegstmp, 0);
#endif
}

/* Code inclusion options */


#define	EFSYS_OPT_NAMES 1

#define	EFSYS_OPT_SIENA 1
#define	EFSYS_OPT_HUNTINGTON 1
#define	EFSYS_OPT_MEDFORD 1
#define	EFSYS_OPT_MEDFORD2 1
#ifdef DEBUG
#define	EFSYS_OPT_CHECK_REG 1
#else
#define	EFSYS_OPT_CHECK_REG 0
#endif

#define	EFSYS_OPT_MCDI 1
#define	EFSYS_OPT_MCDI_LOGGING 0
#define	EFSYS_OPT_MCDI_PROXY_AUTH 0

#define	EFSYS_OPT_MAC_STATS 1

#define	EFSYS_OPT_LOOPBACK 0

#define	EFSYS_OPT_MON_MCDI 0
#define	EFSYS_OPT_MON_STATS 0

#define	EFSYS_OPT_PHY_STATS 1
#define	EFSYS_OPT_BIST 1
#define	EFSYS_OPT_PHY_LED_CONTROL 1
#define	EFSYS_OPT_PHY_FLAGS 0

#define	EFSYS_OPT_VPD 1
#define	EFSYS_OPT_NVRAM 1
#define	EFSYS_OPT_BOOTCFG 0
#define	EFSYS_OPT_IMAGE_LAYOUT 0

#define	EFSYS_OPT_DIAG 0
#define	EFSYS_OPT_RX_SCALE 1
#define	EFSYS_OPT_QSTATS 1
#define	EFSYS_OPT_FILTER 1
#define	EFSYS_OPT_RX_SCATTER 0

#define	EFSYS_OPT_EV_PREFETCH 0

#define	EFSYS_OPT_DECODE_INTR_FATAL 1

#define	EFSYS_OPT_LICENSING 0

#define	EFSYS_OPT_ALLOW_UNCONFIGURED_NIC 0

#define	EFSYS_OPT_RX_PACKED_STREAM 0

#define	EFSYS_OPT_RX_ES_SUPER_BUFFER 0

#define	EFSYS_OPT_TUNNEL 0

#define	EFSYS_OPT_FW_SUBVARIANT_AWARE 0

/* ID */

typedef struct __efsys_identifier_s	efsys_identifier_t;

/* PROBE */

#ifndef DTRACE_PROBE

#define	EFSYS_PROBE(_name)

#define	EFSYS_PROBE1(_name, _type1, _arg1)

#define	EFSYS_PROBE2(_name, _type1, _arg1, _type2, _arg2)

#define	EFSYS_PROBE3(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3)

#define	EFSYS_PROBE4(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4)

#define	EFSYS_PROBE5(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5)

#define	EFSYS_PROBE6(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5,		\
	    _type6, _arg6)

#define	EFSYS_PROBE7(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5,		\
	    _type6, _arg6, _type7, _arg7)

#else /* DTRACE_PROBE */

#define	EFSYS_PROBE(_name)						\
	DTRACE_PROBE(_name)

#define	EFSYS_PROBE1(_name, _type1, _arg1)				\
	DTRACE_PROBE1(_name, _type1, _arg1)

#define	EFSYS_PROBE2(_name, _type1, _arg1, _type2, _arg2)		\
	DTRACE_PROBE2(_name, _type1, _arg1, _type2, _arg2)

#define	EFSYS_PROBE3(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3)						\
	DTRACE_PROBE3(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3)

#define	EFSYS_PROBE4(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4)				\
	DTRACE_PROBE4(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4)

#ifdef DTRACE_PROBE5
#define	EFSYS_PROBE5(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5)		\
	DTRACE_PROBE5(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5)
#else
#define	EFSYS_PROBE5(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5)		\
	DTRACE_PROBE4(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4)
#endif

#ifdef DTRACE_PROBE6
#define	EFSYS_PROBE6(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5,		\
	    _type6, _arg6)						\
	DTRACE_PROBE6(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5,		\
	    _type6, _arg6)
#else
#define	EFSYS_PROBE6(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5,		\
	    _type6, _arg6)						\
	EFSYS_PROBE5(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5)
#endif

#ifdef DTRACE_PROBE7
#define	EFSYS_PROBE7(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5,		\
	    _type6, _arg6, _type7, _arg7)				\
	DTRACE_PROBE7(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5,		\
	    _type6, _arg6, _type7, _arg7)
#else
#define	EFSYS_PROBE7(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5,		\
	    _type6, _arg6, _type7, _arg7)				\
	EFSYS_PROBE6(_name, _type1, _arg1, _type2, _arg2,		\
	    _type3, _arg3, _type4, _arg4, _type5, _arg5,		\
	    _type6, _arg6)
#endif

#endif /* DTRACE_PROBE */

/* DMA */

typedef uint64_t		efsys_dma_addr_t;

typedef struct efsys_mem_s {
	bus_dma_tag_t		esm_tag;
	bus_dmamap_t		esm_map;
	caddr_t			esm_base;
	efsys_dma_addr_t	esm_addr;
	size_t			esm_size;
} efsys_mem_t;

#define	EFSYS_MEM_SIZE(_esmp)						\
	((_esmp)->esm_size)

#define	EFSYS_MEM_ADDR(_esmp)						\
	((_esmp)->esm_addr)

#define	EFSYS_MEM_IS_NULL(_esmp)					\
	((_esmp)->esm_base == NULL)


#define	EFSYS_MEM_ZERO(_esmp, _size)					\
	do {								\
		(void) memset((_esmp)->esm_base, 0, (_size));		\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_MEM_READD(_esmp, _offset, _edp)				\
	do {								\
		uint32_t *addr;						\
									\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_dword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		addr = (void *)((_esmp)->esm_base + (_offset));		\
									\
		(_edp)->ed_u32[0] = *addr;				\
									\
		EFSYS_PROBE2(mem_readd, unsigned int, (_offset),	\
		    uint32_t, (_edp)->ed_u32[0]);			\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#if defined(__x86_64__)
#define	EFSYS_MEM_READQ(_esmp, _offset, _eqp)				\
	do {								\
		uint64_t *addr;						\
									\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_qword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		addr = (void *)((_esmp)->esm_base + (_offset));		\
									\
		(_eqp)->eq_u64[0] = *addr;				\
									\
		EFSYS_PROBE3(mem_readq, unsigned int, (_offset),	\
		    uint32_t, (_eqp)->eq_u32[1],			\
		    uint32_t, (_eqp)->eq_u32[0]);			\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#else
#define	EFSYS_MEM_READQ(_esmp, _offset, _eqp)				\
	do {								\
		uint32_t *addr;						\
									\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_qword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		addr = (void *)((_esmp)->esm_base + (_offset));		\
									\
		(_eqp)->eq_u32[0] = *addr++;				\
		(_eqp)->eq_u32[1] = *addr;				\
									\
		EFSYS_PROBE3(mem_readq, unsigned int, (_offset),	\
		    uint32_t, (_eqp)->eq_u32[1],			\
		    uint32_t, (_eqp)->eq_u32[0]);			\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#endif

#if defined(__x86_64__)
#define	EFSYS_MEM_READO(_esmp, _offset, _eop)				\
	do {								\
		uint64_t *addr;						\
									\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_oword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		addr = (void *)((_esmp)->esm_base + (_offset));		\
									\
		(_eop)->eo_u64[0] = *addr++;				\
		(_eop)->eo_u64[1] = *addr;				\
									\
		EFSYS_PROBE5(mem_reado, unsigned int, (_offset),	\
		    uint32_t, (_eop)->eo_u32[3],			\
		    uint32_t, (_eop)->eo_u32[2],			\
		    uint32_t, (_eop)->eo_u32[1],			\
		    uint32_t, (_eop)->eo_u32[0]);			\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#else
#define	EFSYS_MEM_READO(_esmp, _offset, _eop)				\
	do {								\
		uint32_t *addr;						\
									\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_oword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		addr = (void *)((_esmp)->esm_base + (_offset));		\
									\
		(_eop)->eo_u32[0] = *addr++;				\
		(_eop)->eo_u32[1] = *addr++;				\
		(_eop)->eo_u32[2] = *addr++;				\
		(_eop)->eo_u32[3] = *addr;				\
									\
		EFSYS_PROBE5(mem_reado, unsigned int, (_offset),	\
		    uint32_t, (_eop)->eo_u32[3],			\
		    uint32_t, (_eop)->eo_u32[2],			\
		    uint32_t, (_eop)->eo_u32[1],			\
		    uint32_t, (_eop)->eo_u32[0]);			\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#endif

#define	EFSYS_MEM_WRITED(_esmp, _offset, _edp)				\
	do {								\
		uint32_t *addr;						\
									\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_dword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		EFSYS_PROBE2(mem_writed, unsigned int, (_offset),	\
		    uint32_t, (_edp)->ed_u32[0]);			\
									\
		addr = (void *)((_esmp)->esm_base + (_offset));		\
									\
		*addr = (_edp)->ed_u32[0];				\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#if defined(__x86_64__)
#define	EFSYS_MEM_WRITEQ(_esmp, _offset, _eqp)				\
	do {								\
		uint64_t *addr;						\
									\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_qword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		EFSYS_PROBE3(mem_writeq, unsigned int, (_offset),	\
		    uint32_t, (_eqp)->eq_u32[1],			\
		    uint32_t, (_eqp)->eq_u32[0]);			\
									\
		addr = (void *)((_esmp)->esm_base + (_offset));		\
									\
		*addr   = (_eqp)->eq_u64[0];				\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#else
#define	EFSYS_MEM_WRITEQ(_esmp, _offset, _eqp)				\
	do {								\
		uint32_t *addr;						\
									\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_qword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		EFSYS_PROBE3(mem_writeq, unsigned int, (_offset),	\
		    uint32_t, (_eqp)->eq_u32[1],			\
		    uint32_t, (_eqp)->eq_u32[0]);			\
									\
		addr = (void *)((_esmp)->esm_base + (_offset));		\
									\
		*addr++ = (_eqp)->eq_u32[0];				\
		*addr   = (_eqp)->eq_u32[1];				\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#endif

#if defined(__x86_64__)
#define	EFSYS_MEM_WRITEO(_esmp, _offset, _eop)				\
	do {								\
		uint64_t *addr;						\
									\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_oword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		EFSYS_PROBE5(mem_writeo, unsigned int, (_offset),	\
		    uint32_t, (_eop)->eo_u32[3],			\
		    uint32_t, (_eop)->eo_u32[2],			\
		    uint32_t, (_eop)->eo_u32[1],			\
		    uint32_t, (_eop)->eo_u32[0]);			\
									\
		addr = (void *)((_esmp)->esm_base + (_offset));		\
									\
		*addr++ = (_eop)->eo_u64[0];				\
		*addr   = (_eop)->eo_u64[1];				\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#else
#define	EFSYS_MEM_WRITEO(_esmp, _offset, _eop)				\
	do {								\
		uint32_t *addr;						\
									\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_oword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		EFSYS_PROBE5(mem_writeo, unsigned int, (_offset),	\
		    uint32_t, (_eop)->eo_u32[3],			\
		    uint32_t, (_eop)->eo_u32[2],			\
		    uint32_t, (_eop)->eo_u32[1],			\
		    uint32_t, (_eop)->eo_u32[0]);			\
									\
		addr = (void *)((_esmp)->esm_base + (_offset));		\
									\
		*addr++ = (_eop)->eo_u32[0];				\
		*addr++ = (_eop)->eo_u32[1];				\
		*addr++ = (_eop)->eo_u32[2];				\
		*addr   = (_eop)->eo_u32[3];				\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#endif

/* BAR */

#define	SFXGE_LOCK_NAME_MAX	16

typedef struct efsys_bar_s {
	struct mtx		esb_lock;
	char			esb_lock_name[SFXGE_LOCK_NAME_MAX];
	bus_space_tag_t		esb_tag;
	bus_space_handle_t	esb_handle;
	int			esb_rid;
	struct resource		*esb_res;
} efsys_bar_t;

#define	SFXGE_BAR_LOCK_INIT(_esbp, _ifname)				\
	do {								\
		snprintf((_esbp)->esb_lock_name,			\
			 sizeof((_esbp)->esb_lock_name),		\
			 "%s:bar", (_ifname));				\
		mtx_init(&(_esbp)->esb_lock, (_esbp)->esb_lock_name,	\
			 NULL, MTX_DEF);				\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#define	SFXGE_BAR_LOCK_DESTROY(_esbp)					\
	mtx_destroy(&(_esbp)->esb_lock)
#define	SFXGE_BAR_LOCK(_esbp)						\
	mtx_lock(&(_esbp)->esb_lock)
#define	SFXGE_BAR_UNLOCK(_esbp)						\
	mtx_unlock(&(_esbp)->esb_lock)

#define	EFSYS_BAR_READD(_esbp, _offset, _edp, _lock)			\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_dword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_LOCK(_esbp);				\
									\
		(_edp)->ed_u32[0] = bus_space_read_stream_4(		\
		    (_esbp)->esb_tag, (_esbp)->esb_handle,		\
		    (_offset));						\
									\
		EFSYS_PROBE2(bar_readd, unsigned int, (_offset),	\
		    uint32_t, (_edp)->ed_u32[0]);			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_UNLOCK(_esbp);			\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#if defined(SFXGE_USE_BUS_SPACE_8)
#define	EFSYS_BAR_READQ(_esbp, _offset, _eqp)				\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_qword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		SFXGE_BAR_LOCK(_esbp);					\
									\
		(_eqp)->eq_u64[0] = bus_space_read_stream_8(		\
		    (_esbp)->esb_tag, (_esbp)->esb_handle,		\
		    (_offset));						\
									\
		EFSYS_PROBE3(bar_readq, unsigned int, (_offset),	\
		    uint32_t, (_eqp)->eq_u32[1],			\
		    uint32_t, (_eqp)->eq_u32[0]);			\
									\
		SFXGE_BAR_UNLOCK(_esbp);				\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_BAR_READO(_esbp, _offset, _eop, _lock)			\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_oword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_LOCK(_esbp);				\
									\
		(_eop)->eo_u64[0] = bus_space_read_stream_8(		\
		    (_esbp)->esb_tag, (_esbp)->esb_handle,		\
		    (_offset));						\
		(_eop)->eo_u64[1] = bus_space_read_stream_8(		\
		    (_esbp)->esb_tag, (_esbp)->esb_handle,		\
		    (_offset) + 8);					\
									\
		EFSYS_PROBE5(bar_reado, unsigned int, (_offset),	\
		    uint32_t, (_eop)->eo_u32[3],			\
		    uint32_t, (_eop)->eo_u32[2],			\
		    uint32_t, (_eop)->eo_u32[1],			\
		    uint32_t, (_eop)->eo_u32[0]);			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_UNLOCK(_esbp);			\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#else
#define	EFSYS_BAR_READQ(_esbp, _offset, _eqp)				\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_qword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		SFXGE_BAR_LOCK(_esbp);					\
									\
		(_eqp)->eq_u32[0] = bus_space_read_stream_4(		\
		    (_esbp)->esb_tag, (_esbp)->esb_handle,		\
		    (_offset));						\
		(_eqp)->eq_u32[1] = bus_space_read_stream_4(		\
		    (_esbp)->esb_tag, (_esbp)->esb_handle,		\
		    (_offset) + 4);					\
									\
		EFSYS_PROBE3(bar_readq, unsigned int, (_offset),	\
		    uint32_t, (_eqp)->eq_u32[1],			\
		    uint32_t, (_eqp)->eq_u32[0]);			\
									\
		SFXGE_BAR_UNLOCK(_esbp);				\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_BAR_READO(_esbp, _offset, _eop, _lock)			\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_oword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_LOCK(_esbp);				\
									\
		(_eop)->eo_u32[0] = bus_space_read_stream_4(		\
		    (_esbp)->esb_tag, (_esbp)->esb_handle,		\
		    (_offset));						\
		(_eop)->eo_u32[1] = bus_space_read_stream_4(		\
		    (_esbp)->esb_tag, (_esbp)->esb_handle,		\
		    (_offset) + 4);					\
		(_eop)->eo_u32[2] = bus_space_read_stream_4(		\
		    (_esbp)->esb_tag, (_esbp)->esb_handle,		\
		    (_offset) + 8);					\
		(_eop)->eo_u32[3] = bus_space_read_stream_4(		\
		    (_esbp)->esb_tag, (_esbp)->esb_handle,		\
		    (_offset) + 12);					\
									\
		EFSYS_PROBE5(bar_reado, unsigned int, (_offset),	\
		    uint32_t, (_eop)->eo_u32[3],			\
		    uint32_t, (_eop)->eo_u32[2],			\
		    uint32_t, (_eop)->eo_u32[1],			\
		    uint32_t, (_eop)->eo_u32[0]);			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_UNLOCK(_esbp);			\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#endif

#define	EFSYS_BAR_WRITED(_esbp, _offset, _edp, _lock)			\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_dword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_LOCK(_esbp);				\
									\
		EFSYS_PROBE2(bar_writed, unsigned int, (_offset),	\
		    uint32_t, (_edp)->ed_u32[0]);			\
									\
		/*							\
		 * Make sure that previous writes to the dword have	\
		 * been done. It should be cheaper than barrier just	\
		 * after the write below.				\
		 */							\
		bus_space_barrier((_esbp)->esb_tag, (_esbp)->esb_handle,\
		    (_offset), sizeof (efx_dword_t),			\
		    BUS_SPACE_BARRIER_WRITE);				\
		bus_space_write_stream_4((_esbp)->esb_tag,		\
		    (_esbp)->esb_handle,				\
		    (_offset), (_edp)->ed_u32[0]);			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_UNLOCK(_esbp);			\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#if defined(SFXGE_USE_BUS_SPACE_8)
#define	EFSYS_BAR_WRITEQ(_esbp, _offset, _eqp)				\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_qword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		SFXGE_BAR_LOCK(_esbp);					\
									\
		EFSYS_PROBE3(bar_writeq, unsigned int, (_offset),	\
		    uint32_t, (_eqp)->eq_u32[1],			\
		    uint32_t, (_eqp)->eq_u32[0]);			\
									\
		/*							\
		 * Make sure that previous writes to the qword have	\
		 * been done. It should be cheaper than barrier just	\
		 * after the write below.				\
		 */							\
		bus_space_barrier((_esbp)->esb_tag, (_esbp)->esb_handle,\
		    (_offset), sizeof (efx_qword_t),			\
		    BUS_SPACE_BARRIER_WRITE);				\
		bus_space_write_stream_8((_esbp)->esb_tag,		\
		    (_esbp)->esb_handle,				\
		    (_offset), (_eqp)->eq_u64[0]);			\
									\
		SFXGE_BAR_UNLOCK(_esbp);				\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#else
#define	EFSYS_BAR_WRITEQ(_esbp, _offset, _eqp)				\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_qword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		SFXGE_BAR_LOCK(_esbp);					\
									\
		EFSYS_PROBE3(bar_writeq, unsigned int, (_offset),	\
		    uint32_t, (_eqp)->eq_u32[1],			\
		    uint32_t, (_eqp)->eq_u32[0]);			\
									\
		/*							\
		 * Make sure that previous writes to the qword have	\
		 * been done. It should be cheaper than barrier just	\
		 * after the last write below.				\
		 */							\
		bus_space_barrier((_esbp)->esb_tag, (_esbp)->esb_handle,\
		    (_offset), sizeof (efx_qword_t),			\
		    BUS_SPACE_BARRIER_WRITE);				\
		bus_space_write_stream_4((_esbp)->esb_tag,		\
		    (_esbp)->esb_handle,				\
		    (_offset), (_eqp)->eq_u32[0]);			\
		/*							\
		 * It should be guaranteed that the last dword comes	\
		 * the last, so barrier entire qword to be sure that	\
		 * neither above nor below writes are reordered.	\
		 */							\
		bus_space_barrier((_esbp)->esb_tag, (_esbp)->esb_handle,\
		    (_offset), sizeof (efx_qword_t),			\
		    BUS_SPACE_BARRIER_WRITE);				\
		bus_space_write_stream_4((_esbp)->esb_tag,		\
		    (_esbp)->esb_handle,				\
		    (_offset) + 4, (_eqp)->eq_u32[1]);			\
									\
		SFXGE_BAR_UNLOCK(_esbp);				\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#endif

/*
 * Guarantees 64bit aligned 64bit writes to write combined BAR mapping
 * (required by PIO hardware)
 */
#define	EFSYS_BAR_WC_WRITEQ(_esbp, _offset, _eqp)			\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_qword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		(void) (_esbp);						\
									\
		/* FIXME: Perform a 64-bit write */			\
		KASSERT(0, ("not implemented"));			\
									\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#if defined(SFXGE_USE_BUS_SPACE_8)
#define	EFSYS_BAR_WRITEO(_esbp, _offset, _eop, _lock)			\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_oword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_LOCK(_esbp);				\
									\
		EFSYS_PROBE5(bar_writeo, unsigned int, (_offset),	\
		    uint32_t, (_eop)->eo_u32[3],			\
		    uint32_t, (_eop)->eo_u32[2],			\
		    uint32_t, (_eop)->eo_u32[1],			\
		    uint32_t, (_eop)->eo_u32[0]);			\
									\
		/*							\
		 * Make sure that previous writes to the oword have	\
		 * been done. It should be cheaper than barrier just	\
		 * after the last write below.				\
		 */							\
		bus_space_barrier((_esbp)->esb_tag, (_esbp)->esb_handle,\
		    (_offset), sizeof (efx_oword_t),			\
		    BUS_SPACE_BARRIER_WRITE);				\
		bus_space_write_stream_8((_esbp)->esb_tag,		\
		    (_esbp)->esb_handle,				\
		    (_offset), (_eop)->eo_u64[0]);			\
		/*							\
		 * It should be guaranteed that the last qword comes	\
		 * the last, so barrier entire oword to be sure that	\
		 * neither above nor below writes are reordered.	\
		 */							\
		bus_space_barrier((_esbp)->esb_tag, (_esbp)->esb_handle,\
		    (_offset), sizeof (efx_oword_t),			\
		    BUS_SPACE_BARRIER_WRITE);				\
		bus_space_write_stream_8((_esbp)->esb_tag,		\
		    (_esbp)->esb_handle,				\
		    (_offset) + 8, (_eop)->eo_u64[1]);			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_UNLOCK(_esbp);			\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#else
#define	EFSYS_BAR_WRITEO(_esbp, _offset, _eop, _lock)			\
	do {								\
		_NOTE(CONSTANTCONDITION)				\
		KASSERT(IS_P2ALIGNED(_offset, sizeof (efx_oword_t)),	\
		    ("not power of 2 aligned"));			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_LOCK(_esbp);				\
									\
		EFSYS_PROBE5(bar_writeo, unsigned int, (_offset),	\
		    uint32_t, (_eop)->eo_u32[3],			\
		    uint32_t, (_eop)->eo_u32[2],			\
		    uint32_t, (_eop)->eo_u32[1],			\
		    uint32_t, (_eop)->eo_u32[0]);			\
									\
		/*							\
		 * Make sure that previous writes to the oword have	\
		 * been done. It should be cheaper than barrier just	\
		 * after the last write below.				\
		 */							\
		bus_space_barrier((_esbp)->esb_tag, (_esbp)->esb_handle,\
		    (_offset), sizeof (efx_oword_t),			\
		    BUS_SPACE_BARRIER_WRITE);				\
		bus_space_write_stream_4((_esbp)->esb_tag,		\
		    (_esbp)->esb_handle,				\
		    (_offset), (_eop)->eo_u32[0]);			\
		bus_space_write_stream_4((_esbp)->esb_tag,		\
		    (_esbp)->esb_handle,				\
		    (_offset) + 4, (_eop)->eo_u32[1]);			\
		bus_space_write_stream_4((_esbp)->esb_tag,		\
		    (_esbp)->esb_handle,				\
		    (_offset) + 8, (_eop)->eo_u32[2]);			\
		/*							\
		 * It should be guaranteed that the last dword comes	\
		 * the last, so barrier entire oword to be sure that	\
		 * neither above nor below writes are reordered.	\
		 */							\
		bus_space_barrier((_esbp)->esb_tag, (_esbp)->esb_handle,\
		    (_offset), sizeof (efx_oword_t),			\
		    BUS_SPACE_BARRIER_WRITE);				\
		bus_space_write_stream_4((_esbp)->esb_tag,		\
		    (_esbp)->esb_handle,				\
		    (_offset) + 12, (_eop)->eo_u32[3]);			\
									\
		_NOTE(CONSTANTCONDITION)				\
		if (_lock)						\
			SFXGE_BAR_UNLOCK(_esbp);			\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#endif

/* Use the standard octo-word write for doorbell writes */
#define	EFSYS_BAR_DOORBELL_WRITEO(_esbp, _offset, _eop)			\
	do {								\
		EFSYS_BAR_WRITEO((_esbp), (_offset), (_eop), B_FALSE);	\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

/* SPIN */

#define	EFSYS_SPIN(_us)							\
	do {								\
		DELAY(_us);						\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_SLEEP	EFSYS_SPIN

/* BARRIERS */

#define	EFSYS_MEM_READ_BARRIER()	rmb()
#define	EFSYS_PIO_WRITE_BARRIER()

/* DMA SYNC */
#define	EFSYS_DMA_SYNC_FOR_KERNEL(_esmp, _offset, _size)		\
	do {								\
		bus_dmamap_sync((_esmp)->esm_tag,			\
		    (_esmp)->esm_map,					\
		    BUS_DMASYNC_POSTREAD);				\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_DMA_SYNC_FOR_DEVICE(_esmp, _offset, _size)		\
	do {								\
		bus_dmamap_sync((_esmp)->esm_tag,			\
		    (_esmp)->esm_map,					\
		    BUS_DMASYNC_PREWRITE);				\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

/* TIMESTAMP */

typedef	clock_t	efsys_timestamp_t;

#define	EFSYS_TIMESTAMP(_usp)						\
	do {								\
		clock_t now;						\
									\
		now = ticks;						\
		*(_usp) = now * hz / 1000000;				\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

/* KMEM */

#define	EFSYS_KMEM_ALLOC(_esip, _size, _p)				\
	do {								\
		(_esip) = (_esip);					\
		/*							\
		 * The macro is used in non-sleepable contexts, for	\
		 * example, holding a mutex.				\
		 */							\
		(_p) = malloc((_size), M_SFXGE, M_NOWAIT|M_ZERO);	\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_KMEM_FREE(_esip, _size, _p)				\
	do {								\
		(void) (_esip);						\
		(void) (_size);						\
		free((_p), M_SFXGE);					\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

/* LOCK */

typedef struct efsys_lock_s {
	struct mtx	lock;
	char		lock_name[SFXGE_LOCK_NAME_MAX];
} efsys_lock_t;

#define	SFXGE_EFSYS_LOCK_INIT(_eslp, _ifname, _label)			\
	do {								\
		efsys_lock_t *__eslp = (_eslp);				\
									\
		snprintf((__eslp)->lock_name,				\
			 sizeof((__eslp)->lock_name),			\
			 "%s:%s", (_ifname), (_label));			\
		mtx_init(&(__eslp)->lock, (__eslp)->lock_name,		\
			 NULL, MTX_DEF);				\
	} while (B_FALSE)
#define	SFXGE_EFSYS_LOCK_DESTROY(_eslp)					\
	mtx_destroy(&(_eslp)->lock)
#define	SFXGE_EFSYS_LOCK(_eslp)						\
	mtx_lock(&(_eslp)->lock)
#define	SFXGE_EFSYS_UNLOCK(_eslp)					\
	mtx_unlock(&(_eslp)->lock)
#define	SFXGE_EFSYS_LOCK_ASSERT_OWNED(_eslp)				\
	mtx_assert(&(_eslp)->lock, MA_OWNED)

typedef int efsys_lock_state_t;

#define	EFSYS_LOCK_MAGIC	0x000010c4

#define	EFSYS_LOCK(_lockp, _state)					\
	do {								\
		SFXGE_EFSYS_LOCK(_lockp);				\
		(_state) = EFSYS_LOCK_MAGIC;				\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_UNLOCK(_lockp, _state)					\
	do {								\
		if ((_state) != EFSYS_LOCK_MAGIC)			\
			KASSERT(B_FALSE, ("not locked"));		\
		SFXGE_EFSYS_UNLOCK(_lockp);				\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

/* STAT */

typedef uint64_t		efsys_stat_t;

#define	EFSYS_STAT_INCR(_knp, _delta) 					\
	do {								\
		*(_knp) += (_delta);					\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_STAT_DECR(_knp, _delta) 					\
	do {								\
		*(_knp) -= (_delta);					\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_STAT_SET(_knp, _val)					\
	do {								\
		*(_knp) = (_val);					\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_STAT_SET_QWORD(_knp, _valp)				\
	do {								\
		*(_knp) = le64toh((_valp)->eq_u64[0]);			\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_STAT_SET_DWORD(_knp, _valp)				\
	do {								\
		*(_knp) = le32toh((_valp)->ed_u32[0]);			\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_STAT_INCR_QWORD(_knp, _valp)				\
	do {								\
		*(_knp) += le64toh((_valp)->eq_u64[0]);			\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

#define	EFSYS_STAT_SUBR_QWORD(_knp, _valp)				\
	do {								\
		*(_knp) -= le64toh((_valp)->eq_u64[0]);			\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)

/* ERR */

extern void	sfxge_err(efsys_identifier_t *, unsigned int,
		    uint32_t, uint32_t);

#if EFSYS_OPT_DECODE_INTR_FATAL
#define	EFSYS_ERR(_esip, _code, _dword0, _dword1)			\
	do {								\
		sfxge_err((_esip), (_code), (_dword0), (_dword1));	\
	_NOTE(CONSTANTCONDITION)					\
	} while (B_FALSE)
#endif

/* ASSERT */

#define	EFSYS_ASSERT(_exp) do {						\
	if (!(_exp))							\
		panic("%s", #_exp);					\
	} while (0)

#define	EFSYS_ASSERT3(_x, _op, _y, _t) do {				\
	const _t __x = (_t)(_x);					\
	const _t __y = (_t)(_y);					\
	if (!(__x _op __y))						\
		panic("assertion failed at %s:%u", __FILE__, __LINE__);	\
	} while(0)

#define	EFSYS_ASSERT3U(_x, _op, _y)	EFSYS_ASSERT3(_x, _op, _y, uint64_t)
#define	EFSYS_ASSERT3S(_x, _op, _y)	EFSYS_ASSERT3(_x, _op, _y, int64_t)
#define	EFSYS_ASSERT3P(_x, _op, _y)	EFSYS_ASSERT3(_x, _op, _y, uintptr_t)

/* ROTATE */

#define	EFSYS_HAS_ROTL_DWORD 0

#ifdef	__cplusplus
}
#endif

#endif	/* _SYS_EFSYS_H */