blob: b757a39f078edb418c6a3690b8759720828f4cf0 [file] [log] [blame]
Ben Hutchings8ceee662008-04-27 12:55:59 +01001/****************************************************************************
2 * Driver for Solarflare Solarstorm network controllers and boards
3 * Copyright 2005-2006 Fen Systems Ltd.
4 * Copyright 2006-2008 Solarflare Communications Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation, incorporated herein by reference.
9 */
10
11#ifndef EFX_BITFIELD_H
12#define EFX_BITFIELD_H
13
14/*
15 * Efx bitfield access
16 *
17 * Efx NICs make extensive use of bitfields up to 128 bits
18 * wide. Since there is no native 128-bit datatype on most systems,
19 * and since 64-bit datatypes are inefficient on 32-bit systems and
20 * vice versa, we wrap accesses in a way that uses the most efficient
21 * datatype.
22 *
23 * The NICs are PCI devices and therefore little-endian. Since most
24 * of the quantities that we deal with are DMAed to/from host memory,
25 * we define our datatypes (efx_oword_t, efx_qword_t and
26 * efx_dword_t) to be little-endian.
27 */
28
29/* Lowest bit numbers and widths */
30#define EFX_DUMMY_FIELD_LBN 0
31#define EFX_DUMMY_FIELD_WIDTH 0
32#define EFX_DWORD_0_LBN 0
33#define EFX_DWORD_0_WIDTH 32
34#define EFX_DWORD_1_LBN 32
35#define EFX_DWORD_1_WIDTH 32
36#define EFX_DWORD_2_LBN 64
37#define EFX_DWORD_2_WIDTH 32
38#define EFX_DWORD_3_LBN 96
39#define EFX_DWORD_3_WIDTH 32
40
41/* Specified attribute (e.g. LBN) of the specified field */
42#define EFX_VAL(field, attribute) field ## _ ## attribute
43/* Low bit number of the specified field */
44#define EFX_LOW_BIT(field) EFX_VAL(field, LBN)
45/* Bit width of the specified field */
46#define EFX_WIDTH(field) EFX_VAL(field, WIDTH)
47/* High bit number of the specified field */
48#define EFX_HIGH_BIT(field) (EFX_LOW_BIT(field) + EFX_WIDTH(field) - 1)
49/* Mask equal in width to the specified field.
50 *
51 * For example, a field with width 5 would have a mask of 0x1f.
52 *
53 * The maximum width mask that can be generated is 64 bits.
54 */
Ben Hutchingsa5150892008-09-01 12:48:55 +010055#define EFX_MASK64(width) \
56 ((width) == 64 ? ~((u64) 0) : \
57 (((((u64) 1) << (width))) - 1))
Ben Hutchings8ceee662008-04-27 12:55:59 +010058
59/* Mask equal in width to the specified field.
60 *
61 * For example, a field with width 5 would have a mask of 0x1f.
62 *
63 * The maximum width mask that can be generated is 32 bits. Use
64 * EFX_MASK64 for higher width fields.
65 */
Ben Hutchingsa5150892008-09-01 12:48:55 +010066#define EFX_MASK32(width) \
67 ((width) == 32 ? ~((u32) 0) : \
68 (((((u32) 1) << (width))) - 1))
Ben Hutchings8ceee662008-04-27 12:55:59 +010069
70/* A doubleword (i.e. 4 byte) datatype - little-endian in HW */
71typedef union efx_dword {
72 __le32 u32[1];
73} efx_dword_t;
74
75/* A quadword (i.e. 8 byte) datatype - little-endian in HW */
76typedef union efx_qword {
77 __le64 u64[1];
78 __le32 u32[2];
79 efx_dword_t dword[2];
80} efx_qword_t;
81
82/* An octword (eight-word, i.e. 16 byte) datatype - little-endian in HW */
83typedef union efx_oword {
84 __le64 u64[2];
85 efx_qword_t qword[2];
86 __le32 u32[4];
87 efx_dword_t dword[4];
88} efx_oword_t;
89
90/* Format string and value expanders for printk */
91#define EFX_DWORD_FMT "%08x"
92#define EFX_QWORD_FMT "%08x:%08x"
93#define EFX_OWORD_FMT "%08x:%08x:%08x:%08x"
94#define EFX_DWORD_VAL(dword) \
95 ((unsigned int) le32_to_cpu((dword).u32[0]))
96#define EFX_QWORD_VAL(qword) \
97 ((unsigned int) le32_to_cpu((qword).u32[1])), \
98 ((unsigned int) le32_to_cpu((qword).u32[0]))
99#define EFX_OWORD_VAL(oword) \
100 ((unsigned int) le32_to_cpu((oword).u32[3])), \
101 ((unsigned int) le32_to_cpu((oword).u32[2])), \
102 ((unsigned int) le32_to_cpu((oword).u32[1])), \
103 ((unsigned int) le32_to_cpu((oword).u32[0]))
104
105/*
106 * Extract bit field portion [low,high) from the native-endian element
107 * which contains bits [min,max).
108 *
109 * For example, suppose "element" represents the high 32 bits of a
110 * 64-bit value, and we wish to extract the bits belonging to the bit
111 * field occupying bits 28-45 of this 64-bit value.
112 *
113 * Then EFX_EXTRACT ( element, 32, 63, 28, 45 ) would give
114 *
115 * ( element ) << 4
116 *
117 * The result will contain the relevant bits filled in in the range
118 * [0,high-low), with garbage in bits [high-low+1,...).
119 */
120#define EFX_EXTRACT_NATIVE(native_element, min, max, low, high) \
121 (((low > max) || (high < min)) ? 0 : \
122 ((low > min) ? \
123 ((native_element) >> (low - min)) : \
124 ((native_element) << (min - low))))
125
126/*
127 * Extract bit field portion [low,high) from the 64-bit little-endian
128 * element which contains bits [min,max)
129 */
130#define EFX_EXTRACT64(element, min, max, low, high) \
131 EFX_EXTRACT_NATIVE(le64_to_cpu(element), min, max, low, high)
132
133/*
134 * Extract bit field portion [low,high) from the 32-bit little-endian
135 * element which contains bits [min,max)
136 */
137#define EFX_EXTRACT32(element, min, max, low, high) \
138 EFX_EXTRACT_NATIVE(le32_to_cpu(element), min, max, low, high)
139
140#define EFX_EXTRACT_OWORD64(oword, low, high) \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100141 ((EFX_EXTRACT64((oword).u64[0], 0, 63, low, high) | \
142 EFX_EXTRACT64((oword).u64[1], 64, 127, low, high)) & \
143 EFX_MASK64(high + 1 - low))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100144
145#define EFX_EXTRACT_QWORD64(qword, low, high) \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100146 (EFX_EXTRACT64((qword).u64[0], 0, 63, low, high) & \
147 EFX_MASK64(high + 1 - low))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100148
149#define EFX_EXTRACT_OWORD32(oword, low, high) \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100150 ((EFX_EXTRACT32((oword).u32[0], 0, 31, low, high) | \
151 EFX_EXTRACT32((oword).u32[1], 32, 63, low, high) | \
152 EFX_EXTRACT32((oword).u32[2], 64, 95, low, high) | \
153 EFX_EXTRACT32((oword).u32[3], 96, 127, low, high)) & \
154 EFX_MASK32(high + 1 - low))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100155
156#define EFX_EXTRACT_QWORD32(qword, low, high) \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100157 ((EFX_EXTRACT32((qword).u32[0], 0, 31, low, high) | \
158 EFX_EXTRACT32((qword).u32[1], 32, 63, low, high)) & \
159 EFX_MASK32(high + 1 - low))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100160
Ben Hutchingsa5150892008-09-01 12:48:55 +0100161#define EFX_EXTRACT_DWORD(dword, low, high) \
162 (EFX_EXTRACT32((dword).u32[0], 0, 31, low, high) & \
163 EFX_MASK32(high + 1 - low))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100164
Ben Hutchingsa5150892008-09-01 12:48:55 +0100165#define EFX_OWORD_FIELD64(oword, field) \
166 EFX_EXTRACT_OWORD64(oword, EFX_LOW_BIT(field), \
167 EFX_HIGH_BIT(field))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100168
Ben Hutchingsa5150892008-09-01 12:48:55 +0100169#define EFX_QWORD_FIELD64(qword, field) \
170 EFX_EXTRACT_QWORD64(qword, EFX_LOW_BIT(field), \
171 EFX_HIGH_BIT(field))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100172
Ben Hutchingsa5150892008-09-01 12:48:55 +0100173#define EFX_OWORD_FIELD32(oword, field) \
174 EFX_EXTRACT_OWORD32(oword, EFX_LOW_BIT(field), \
175 EFX_HIGH_BIT(field))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100176
Ben Hutchingsa5150892008-09-01 12:48:55 +0100177#define EFX_QWORD_FIELD32(qword, field) \
178 EFX_EXTRACT_QWORD32(qword, EFX_LOW_BIT(field), \
179 EFX_HIGH_BIT(field))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100180
Ben Hutchingsa5150892008-09-01 12:48:55 +0100181#define EFX_DWORD_FIELD(dword, field) \
182 EFX_EXTRACT_DWORD(dword, EFX_LOW_BIT(field), \
183 EFX_HIGH_BIT(field))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100184
185#define EFX_OWORD_IS_ZERO64(oword) \
186 (((oword).u64[0] | (oword).u64[1]) == (__force __le64) 0)
187
188#define EFX_QWORD_IS_ZERO64(qword) \
189 (((qword).u64[0]) == (__force __le64) 0)
190
191#define EFX_OWORD_IS_ZERO32(oword) \
192 (((oword).u32[0] | (oword).u32[1] | (oword).u32[2] | (oword).u32[3]) \
193 == (__force __le32) 0)
194
195#define EFX_QWORD_IS_ZERO32(qword) \
196 (((qword).u32[0] | (qword).u32[1]) == (__force __le32) 0)
197
198#define EFX_DWORD_IS_ZERO(dword) \
199 (((dword).u32[0]) == (__force __le32) 0)
200
201#define EFX_OWORD_IS_ALL_ONES64(oword) \
202 (((oword).u64[0] & (oword).u64[1]) == ~((__force __le64) 0))
203
204#define EFX_QWORD_IS_ALL_ONES64(qword) \
205 ((qword).u64[0] == ~((__force __le64) 0))
206
207#define EFX_OWORD_IS_ALL_ONES32(oword) \
208 (((oword).u32[0] & (oword).u32[1] & (oword).u32[2] & (oword).u32[3]) \
209 == ~((__force __le32) 0))
210
211#define EFX_QWORD_IS_ALL_ONES32(qword) \
212 (((qword).u32[0] & (qword).u32[1]) == ~((__force __le32) 0))
213
214#define EFX_DWORD_IS_ALL_ONES(dword) \
215 ((dword).u32[0] == ~((__force __le32) 0))
216
217#if BITS_PER_LONG == 64
218#define EFX_OWORD_FIELD EFX_OWORD_FIELD64
219#define EFX_QWORD_FIELD EFX_QWORD_FIELD64
220#define EFX_OWORD_IS_ZERO EFX_OWORD_IS_ZERO64
221#define EFX_QWORD_IS_ZERO EFX_QWORD_IS_ZERO64
222#define EFX_OWORD_IS_ALL_ONES EFX_OWORD_IS_ALL_ONES64
223#define EFX_QWORD_IS_ALL_ONES EFX_QWORD_IS_ALL_ONES64
224#else
225#define EFX_OWORD_FIELD EFX_OWORD_FIELD32
226#define EFX_QWORD_FIELD EFX_QWORD_FIELD32
227#define EFX_OWORD_IS_ZERO EFX_OWORD_IS_ZERO32
228#define EFX_QWORD_IS_ZERO EFX_QWORD_IS_ZERO32
229#define EFX_OWORD_IS_ALL_ONES EFX_OWORD_IS_ALL_ONES32
230#define EFX_QWORD_IS_ALL_ONES EFX_QWORD_IS_ALL_ONES32
231#endif
232
233/*
234 * Construct bit field portion
235 *
236 * Creates the portion of the bit field [low,high) that lies within
237 * the range [min,max).
238 */
239#define EFX_INSERT_NATIVE64(min, max, low, high, value) \
240 (((low > max) || (high < min)) ? 0 : \
241 ((low > min) ? \
242 (((u64) (value)) << (low - min)) : \
243 (((u64) (value)) >> (min - low))))
244
245#define EFX_INSERT_NATIVE32(min, max, low, high, value) \
246 (((low > max) || (high < min)) ? 0 : \
247 ((low > min) ? \
248 (((u32) (value)) << (low - min)) : \
249 (((u32) (value)) >> (min - low))))
250
251#define EFX_INSERT_NATIVE(min, max, low, high, value) \
252 ((((max - min) >= 32) || ((high - low) >= 32)) ? \
253 EFX_INSERT_NATIVE64(min, max, low, high, value) : \
254 EFX_INSERT_NATIVE32(min, max, low, high, value))
255
256/*
257 * Construct bit field portion
258 *
259 * Creates the portion of the named bit field that lies within the
260 * range [min,max).
261 */
262#define EFX_INSERT_FIELD_NATIVE(min, max, field, value) \
263 EFX_INSERT_NATIVE(min, max, EFX_LOW_BIT(field), \
264 EFX_HIGH_BIT(field), value)
265
266/*
267 * Construct bit field
268 *
269 * Creates the portion of the named bit fields that lie within the
270 * range [min,max).
271 */
272#define EFX_INSERT_FIELDS_NATIVE(min, max, \
273 field1, value1, \
274 field2, value2, \
275 field3, value3, \
276 field4, value4, \
277 field5, value5, \
278 field6, value6, \
279 field7, value7, \
280 field8, value8, \
281 field9, value9, \
282 field10, value10) \
283 (EFX_INSERT_FIELD_NATIVE((min), (max), field1, (value1)) | \
284 EFX_INSERT_FIELD_NATIVE((min), (max), field2, (value2)) | \
285 EFX_INSERT_FIELD_NATIVE((min), (max), field3, (value3)) | \
286 EFX_INSERT_FIELD_NATIVE((min), (max), field4, (value4)) | \
287 EFX_INSERT_FIELD_NATIVE((min), (max), field5, (value5)) | \
288 EFX_INSERT_FIELD_NATIVE((min), (max), field6, (value6)) | \
289 EFX_INSERT_FIELD_NATIVE((min), (max), field7, (value7)) | \
290 EFX_INSERT_FIELD_NATIVE((min), (max), field8, (value8)) | \
291 EFX_INSERT_FIELD_NATIVE((min), (max), field9, (value9)) | \
292 EFX_INSERT_FIELD_NATIVE((min), (max), field10, (value10)))
293
294#define EFX_INSERT_FIELDS64(...) \
295 cpu_to_le64(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))
296
297#define EFX_INSERT_FIELDS32(...) \
298 cpu_to_le32(EFX_INSERT_FIELDS_NATIVE(__VA_ARGS__))
299
300#define EFX_POPULATE_OWORD64(oword, ...) do { \
301 (oword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__); \
302 (oword).u64[1] = EFX_INSERT_FIELDS64(64, 127, __VA_ARGS__); \
303 } while (0)
304
305#define EFX_POPULATE_QWORD64(qword, ...) do { \
306 (qword).u64[0] = EFX_INSERT_FIELDS64(0, 63, __VA_ARGS__); \
307 } while (0)
308
309#define EFX_POPULATE_OWORD32(oword, ...) do { \
310 (oword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__); \
311 (oword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__); \
312 (oword).u32[2] = EFX_INSERT_FIELDS32(64, 95, __VA_ARGS__); \
313 (oword).u32[3] = EFX_INSERT_FIELDS32(96, 127, __VA_ARGS__); \
314 } while (0)
315
316#define EFX_POPULATE_QWORD32(qword, ...) do { \
317 (qword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__); \
318 (qword).u32[1] = EFX_INSERT_FIELDS32(32, 63, __VA_ARGS__); \
319 } while (0)
320
321#define EFX_POPULATE_DWORD(dword, ...) do { \
322 (dword).u32[0] = EFX_INSERT_FIELDS32(0, 31, __VA_ARGS__); \
323 } while (0)
324
325#if BITS_PER_LONG == 64
326#define EFX_POPULATE_OWORD EFX_POPULATE_OWORD64
327#define EFX_POPULATE_QWORD EFX_POPULATE_QWORD64
328#else
329#define EFX_POPULATE_OWORD EFX_POPULATE_OWORD32
330#define EFX_POPULATE_QWORD EFX_POPULATE_QWORD32
331#endif
332
333/* Populate an octword field with various numbers of arguments */
334#define EFX_POPULATE_OWORD_10 EFX_POPULATE_OWORD
335#define EFX_POPULATE_OWORD_9(oword, ...) \
336 EFX_POPULATE_OWORD_10(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
337#define EFX_POPULATE_OWORD_8(oword, ...) \
338 EFX_POPULATE_OWORD_9(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
339#define EFX_POPULATE_OWORD_7(oword, ...) \
340 EFX_POPULATE_OWORD_8(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
341#define EFX_POPULATE_OWORD_6(oword, ...) \
342 EFX_POPULATE_OWORD_7(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
343#define EFX_POPULATE_OWORD_5(oword, ...) \
344 EFX_POPULATE_OWORD_6(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
345#define EFX_POPULATE_OWORD_4(oword, ...) \
346 EFX_POPULATE_OWORD_5(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
347#define EFX_POPULATE_OWORD_3(oword, ...) \
348 EFX_POPULATE_OWORD_4(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
349#define EFX_POPULATE_OWORD_2(oword, ...) \
350 EFX_POPULATE_OWORD_3(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
351#define EFX_POPULATE_OWORD_1(oword, ...) \
352 EFX_POPULATE_OWORD_2(oword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
353#define EFX_ZERO_OWORD(oword) \
354 EFX_POPULATE_OWORD_1(oword, EFX_DUMMY_FIELD, 0)
355#define EFX_SET_OWORD(oword) \
356 EFX_POPULATE_OWORD_4(oword, \
357 EFX_DWORD_0, 0xffffffff, \
358 EFX_DWORD_1, 0xffffffff, \
359 EFX_DWORD_2, 0xffffffff, \
360 EFX_DWORD_3, 0xffffffff)
361
362/* Populate a quadword field with various numbers of arguments */
363#define EFX_POPULATE_QWORD_10 EFX_POPULATE_QWORD
364#define EFX_POPULATE_QWORD_9(qword, ...) \
365 EFX_POPULATE_QWORD_10(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
366#define EFX_POPULATE_QWORD_8(qword, ...) \
367 EFX_POPULATE_QWORD_9(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
368#define EFX_POPULATE_QWORD_7(qword, ...) \
369 EFX_POPULATE_QWORD_8(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
370#define EFX_POPULATE_QWORD_6(qword, ...) \
371 EFX_POPULATE_QWORD_7(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
372#define EFX_POPULATE_QWORD_5(qword, ...) \
373 EFX_POPULATE_QWORD_6(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
374#define EFX_POPULATE_QWORD_4(qword, ...) \
375 EFX_POPULATE_QWORD_5(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
376#define EFX_POPULATE_QWORD_3(qword, ...) \
377 EFX_POPULATE_QWORD_4(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
378#define EFX_POPULATE_QWORD_2(qword, ...) \
379 EFX_POPULATE_QWORD_3(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
380#define EFX_POPULATE_QWORD_1(qword, ...) \
381 EFX_POPULATE_QWORD_2(qword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
382#define EFX_ZERO_QWORD(qword) \
383 EFX_POPULATE_QWORD_1(qword, EFX_DUMMY_FIELD, 0)
384#define EFX_SET_QWORD(qword) \
385 EFX_POPULATE_QWORD_2(qword, \
386 EFX_DWORD_0, 0xffffffff, \
387 EFX_DWORD_1, 0xffffffff)
388
389/* Populate a dword field with various numbers of arguments */
390#define EFX_POPULATE_DWORD_10 EFX_POPULATE_DWORD
391#define EFX_POPULATE_DWORD_9(dword, ...) \
392 EFX_POPULATE_DWORD_10(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
393#define EFX_POPULATE_DWORD_8(dword, ...) \
394 EFX_POPULATE_DWORD_9(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
395#define EFX_POPULATE_DWORD_7(dword, ...) \
396 EFX_POPULATE_DWORD_8(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
397#define EFX_POPULATE_DWORD_6(dword, ...) \
398 EFX_POPULATE_DWORD_7(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
399#define EFX_POPULATE_DWORD_5(dword, ...) \
400 EFX_POPULATE_DWORD_6(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
401#define EFX_POPULATE_DWORD_4(dword, ...) \
402 EFX_POPULATE_DWORD_5(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
403#define EFX_POPULATE_DWORD_3(dword, ...) \
404 EFX_POPULATE_DWORD_4(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
405#define EFX_POPULATE_DWORD_2(dword, ...) \
406 EFX_POPULATE_DWORD_3(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
407#define EFX_POPULATE_DWORD_1(dword, ...) \
408 EFX_POPULATE_DWORD_2(dword, EFX_DUMMY_FIELD, 0, __VA_ARGS__)
409#define EFX_ZERO_DWORD(dword) \
410 EFX_POPULATE_DWORD_1(dword, EFX_DUMMY_FIELD, 0)
411#define EFX_SET_DWORD(dword) \
412 EFX_POPULATE_DWORD_1(dword, EFX_DWORD_0, 0xffffffff)
413
414/*
415 * Modify a named field within an already-populated structure. Used
416 * for read-modify-write operations.
417 *
418 */
419
420#define EFX_INVERT_OWORD(oword) do { \
421 (oword).u64[0] = ~((oword).u64[0]); \
422 (oword).u64[1] = ~((oword).u64[1]); \
423 } while (0)
424
Ben Hutchingsa5150892008-09-01 12:48:55 +0100425#define EFX_INSERT64(min, max, low, high, value) \
426 cpu_to_le64(EFX_INSERT_NATIVE(min, max, low, high, value))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100427
Ben Hutchingsa5150892008-09-01 12:48:55 +0100428#define EFX_INSERT32(min, max, low, high, value) \
429 cpu_to_le32(EFX_INSERT_NATIVE(min, max, low, high, value))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100430
Ben Hutchingsa5150892008-09-01 12:48:55 +0100431#define EFX_INPLACE_MASK64(min, max, low, high) \
432 EFX_INSERT64(min, max, low, high, EFX_MASK64(high + 1 - low))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100433
Ben Hutchingsa5150892008-09-01 12:48:55 +0100434#define EFX_INPLACE_MASK32(min, max, low, high) \
435 EFX_INSERT32(min, max, low, high, EFX_MASK32(high + 1 - low))
Ben Hutchings8ceee662008-04-27 12:55:59 +0100436
Ben Hutchingsa5150892008-09-01 12:48:55 +0100437#define EFX_SET_OWORD64(oword, low, high, value) do { \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100438 (oword).u64[0] = (((oword).u64[0] \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100439 & ~EFX_INPLACE_MASK64(0, 63, low, high)) \
440 | EFX_INSERT64(0, 63, low, high, value)); \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100441 (oword).u64[1] = (((oword).u64[1] \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100442 & ~EFX_INPLACE_MASK64(64, 127, low, high)) \
443 | EFX_INSERT64(64, 127, low, high, value)); \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100444 } while (0)
445
Ben Hutchingsa5150892008-09-01 12:48:55 +0100446#define EFX_SET_QWORD64(qword, low, high, value) do { \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100447 (qword).u64[0] = (((qword).u64[0] \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100448 & ~EFX_INPLACE_MASK64(0, 63, low, high)) \
449 | EFX_INSERT64(0, 63, low, high, value)); \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100450 } while (0)
451
Ben Hutchingsa5150892008-09-01 12:48:55 +0100452#define EFX_SET_OWORD32(oword, low, high, value) do { \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100453 (oword).u32[0] = (((oword).u32[0] \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100454 & ~EFX_INPLACE_MASK32(0, 31, low, high)) \
455 | EFX_INSERT32(0, 31, low, high, value)); \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100456 (oword).u32[1] = (((oword).u32[1] \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100457 & ~EFX_INPLACE_MASK32(32, 63, low, high)) \
458 | EFX_INSERT32(32, 63, low, high, value)); \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100459 (oword).u32[2] = (((oword).u32[2] \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100460 & ~EFX_INPLACE_MASK32(64, 95, low, high)) \
461 | EFX_INSERT32(64, 95, low, high, value)); \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100462 (oword).u32[3] = (((oword).u32[3] \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100463 & ~EFX_INPLACE_MASK32(96, 127, low, high)) \
464 | EFX_INSERT32(96, 127, low, high, value)); \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100465 } while (0)
466
Ben Hutchingsa5150892008-09-01 12:48:55 +0100467#define EFX_SET_QWORD32(qword, low, high, value) do { \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100468 (qword).u32[0] = (((qword).u32[0] \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100469 & ~EFX_INPLACE_MASK32(0, 31, low, high)) \
470 | EFX_INSERT32(0, 31, low, high, value)); \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100471 (qword).u32[1] = (((qword).u32[1] \
Ben Hutchingsa5150892008-09-01 12:48:55 +0100472 & ~EFX_INPLACE_MASK32(32, 63, low, high)) \
473 | EFX_INSERT32(32, 63, low, high, value)); \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100474 } while (0)
475
Ben Hutchingsa5150892008-09-01 12:48:55 +0100476#define EFX_SET_DWORD32(dword, low, high, value) do { \
477 (dword).u32[0] = (((dword).u32[0] \
478 & ~EFX_INPLACE_MASK32(0, 31, low, high)) \
479 | EFX_INSERT32(0, 31, low, high, value)); \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100480 } while (0)
481
Ben Hutchingsa5150892008-09-01 12:48:55 +0100482#define EFX_SET_OWORD_FIELD64(oword, field, value) \
483 EFX_SET_OWORD64(oword, EFX_LOW_BIT(field), \
484 EFX_HIGH_BIT(field), value)
485
486#define EFX_SET_QWORD_FIELD64(qword, field, value) \
487 EFX_SET_QWORD64(qword, EFX_LOW_BIT(field), \
488 EFX_HIGH_BIT(field), value)
489
490#define EFX_SET_OWORD_FIELD32(oword, field, value) \
491 EFX_SET_OWORD32(oword, EFX_LOW_BIT(field), \
492 EFX_HIGH_BIT(field), value)
493
494#define EFX_SET_QWORD_FIELD32(qword, field, value) \
495 EFX_SET_QWORD32(qword, EFX_LOW_BIT(field), \
496 EFX_HIGH_BIT(field), value)
497
498#define EFX_SET_DWORD_FIELD(dword, field, value) \
499 EFX_SET_DWORD32(dword, EFX_LOW_BIT(field), \
500 EFX_HIGH_BIT(field), value)
501
502
503
Ben Hutchings8ceee662008-04-27 12:55:59 +0100504#if BITS_PER_LONG == 64
505#define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD64
506#define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD64
507#else
508#define EFX_SET_OWORD_FIELD EFX_SET_OWORD_FIELD32
509#define EFX_SET_QWORD_FIELD EFX_SET_QWORD_FIELD32
510#endif
511
512#define EFX_SET_OWORD_FIELD_VER(efx, oword, field, value) do { \
Ben Hutchings55668612008-05-16 21:16:10 +0100513 if (falcon_rev(efx) >= FALCON_REV_B0) { \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100514 EFX_SET_OWORD_FIELD((oword), field##_B0, (value)); \
515 } else { \
516 EFX_SET_OWORD_FIELD((oword), field##_A1, (value)); \
517 } \
518} while (0)
519
520#define EFX_QWORD_FIELD_VER(efx, qword, field) \
Ben Hutchings55668612008-05-16 21:16:10 +0100521 (falcon_rev(efx) >= FALCON_REV_B0 ? \
Ben Hutchings8ceee662008-04-27 12:55:59 +0100522 EFX_QWORD_FIELD((qword), field##_B0) : \
523 EFX_QWORD_FIELD((qword), field##_A1))
524
525/* Used to avoid compiler warnings about shift range exceeding width
526 * of the data types when dma_addr_t is only 32 bits wide.
527 */
528#define DMA_ADDR_T_WIDTH (8 * sizeof(dma_addr_t))
529#define EFX_DMA_TYPE_WIDTH(width) \
530 (((width) < DMA_ADDR_T_WIDTH) ? (width) : DMA_ADDR_T_WIDTH)
Ben Hutchings8ceee662008-04-27 12:55:59 +0100531
532#endif /* EFX_BITFIELD_H */