ah_internal.h revision 221667
1/*
2 * Copyright (c) 2002-2009 Sam Leffler, Errno Consulting
3 * Copyright (c) 2002-2008 Atheros Communications, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 *
17 * $FreeBSD: head/sys/dev/ath/ath_hal/ah_internal.h 221667 2011-05-08 15:55:52Z adrian $
18 */
19#ifndef _ATH_AH_INTERAL_H_
20#define _ATH_AH_INTERAL_H_
21/*
22 * Atheros Device Hardware Access Layer (HAL).
23 *
24 * Internal definitions.
25 */
26#define	AH_NULL	0
27#define	AH_MIN(a,b)	((a)<(b)?(a):(b))
28#define	AH_MAX(a,b)	((a)>(b)?(a):(b))
29
30#include <net80211/_ieee80211.h>
31#include "opt_ah.h"			/* needed for AH_SUPPORT_AR5416 */
32
33#ifndef NBBY
34#define	NBBY	8			/* number of bits/byte */
35#endif
36
37#ifndef roundup
38#define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))  /* to any y */
39#endif
40#ifndef howmany
41#define	howmany(x, y)	(((x)+((y)-1))/(y))
42#endif
43
44#ifndef offsetof
45#define	offsetof(type, field)	((size_t)(&((type *)0)->field))
46#endif
47
48typedef struct {
49	uint16_t	start;		/* first register */
50	uint16_t	end;		/* ending register or zero */
51} HAL_REGRANGE;
52
53typedef struct {
54	uint32_t	addr;		/* regiser address/offset */
55	uint32_t	value;		/* value to write */
56} HAL_REGWRITE;
57
58/*
59 * Transmit power scale factor.
60 *
61 * NB: This is not public because we want to discourage the use of
62 *     scaling; folks should use the tx power limit interface.
63 */
64typedef enum {
65	HAL_TP_SCALE_MAX	= 0,		/* no scaling (default) */
66	HAL_TP_SCALE_50		= 1,		/* 50% of max (-3 dBm) */
67	HAL_TP_SCALE_25		= 2,		/* 25% of max (-6 dBm) */
68	HAL_TP_SCALE_12		= 3,		/* 12% of max (-9 dBm) */
69	HAL_TP_SCALE_MIN	= 4,		/* min, but still on */
70} HAL_TP_SCALE;
71
72typedef enum {
73 	HAL_CAP_RADAR		= 0,		/* Radar capability */
74 	HAL_CAP_AR		= 1,		/* AR capability */
75} HAL_PHYDIAG_CAPS;
76
77/*
78 * Each chip or class of chips registers to offer support.
79 */
80struct ath_hal_chip {
81	const char	*name;
82	const char	*(*probe)(uint16_t vendorid, uint16_t devid);
83	struct ath_hal	*(*attach)(uint16_t devid, HAL_SOFTC,
84			    HAL_BUS_TAG, HAL_BUS_HANDLE, uint16_t *eepromdata,
85			    HAL_STATUS *error);
86};
87#ifndef AH_CHIP
88#define	AH_CHIP(_name, _probe, _attach)				\
89static struct ath_hal_chip _name##_chip = {			\
90	.name		= #_name,				\
91	.probe		= _probe,				\
92	.attach		= _attach				\
93};								\
94OS_DATA_SET(ah_chips, _name##_chip)
95#endif
96
97/*
98 * Each RF backend registers to offer support; this is mostly
99 * used by multi-chip 5212 solutions.  Single-chip solutions
100 * have a fixed idea about which RF to use.
101 */
102struct ath_hal_rf {
103	const char	*name;
104	HAL_BOOL	(*probe)(struct ath_hal *ah);
105	HAL_BOOL	(*attach)(struct ath_hal *ah, HAL_STATUS *ecode);
106};
107#ifndef AH_RF
108#define	AH_RF(_name, _probe, _attach)				\
109static struct ath_hal_rf _name##_rf = {				\
110	.name		= __STRING(_name),			\
111	.probe		= _probe,				\
112	.attach		= _attach				\
113};								\
114OS_DATA_SET(ah_rfs, _name##_rf)
115#endif
116
117struct ath_hal_rf *ath_hal_rfprobe(struct ath_hal *ah, HAL_STATUS *ecode);
118
119/*
120 * Maximum number of internal channels.  Entries are per unique
121 * frequency so this might be need to be increased to handle all
122 * usage cases; typically no more than 32 are really needed but
123 * dynamically allocating the data structures is a bit painful
124 * right now.
125 */
126#ifndef AH_MAXCHAN
127#define	AH_MAXCHAN	96
128#endif
129
130/*
131 * Internal per-channel state.  These are found
132 * using ic_devdata in the ieee80211_channel.
133 */
134typedef struct {
135	uint16_t	channel;	/* h/w frequency, NB: may be mapped */
136	uint8_t		privFlags;
137#define	CHANNEL_IQVALID		0x01	/* IQ calibration valid */
138#define	CHANNEL_ANI_INIT	0x02	/* ANI state initialized */
139#define	CHANNEL_ANI_SETUP	0x04	/* ANI state setup */
140#define	CHANNEL_MIMO_NF_VALID	0x04	/* Mimo NF values are valid */
141	uint8_t		calValid;	/* bitmask of cal types */
142	int8_t		iCoff;
143	int8_t		qCoff;
144	int16_t		rawNoiseFloor;
145	int16_t		noiseFloorAdjust;
146#ifdef	AH_SUPPORT_AR5416
147	int16_t		noiseFloorCtl[AH_MIMO_MAX_CHAINS];
148	int16_t		noiseFloorExt[AH_MIMO_MAX_CHAINS];
149#endif	/* AH_SUPPORT_AR5416 */
150	uint16_t	mainSpur;	/* cached spur value for this channel */
151} HAL_CHANNEL_INTERNAL;
152
153/* channel requires noise floor check */
154#define	CHANNEL_NFCREQUIRED	IEEE80211_CHAN_PRIV0
155
156/* all full-width channels */
157#define	IEEE80211_CHAN_ALLFULL \
158	(IEEE80211_CHAN_ALL - (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER))
159#define	IEEE80211_CHAN_ALLTURBOFULL \
160	(IEEE80211_CHAN_ALLTURBO - \
161	 (IEEE80211_CHAN_HALF | IEEE80211_CHAN_QUARTER))
162
163typedef struct {
164	uint32_t	halChanSpreadSupport 		: 1,
165			halSleepAfterBeaconBroken	: 1,
166			halCompressSupport		: 1,
167			halBurstSupport			: 1,
168			halFastFramesSupport		: 1,
169			halChapTuningSupport		: 1,
170			halTurboGSupport		: 1,
171			halTurboPrimeSupport		: 1,
172			halMicAesCcmSupport		: 1,
173			halMicCkipSupport		: 1,
174			halMicTkipSupport		: 1,
175			halTkipMicTxRxKeySupport	: 1,
176			halCipherAesCcmSupport		: 1,
177			halCipherCkipSupport		: 1,
178			halCipherTkipSupport		: 1,
179			halPSPollBroken			: 1,
180			halVEOLSupport			: 1,
181			halBssIdMaskSupport		: 1,
182			halMcastKeySrchSupport		: 1,
183			halTsfAddSupport		: 1,
184			halChanHalfRate			: 1,
185			halChanQuarterRate		: 1,
186			halHTSupport			: 1,
187			halHTSGI20Support		: 1,
188			halRfSilentSupport		: 1,
189			halHwPhyCounterSupport		: 1,
190			halWowSupport			: 1,
191			halWowMatchPatternExact		: 1,
192			halAutoSleepSupport		: 1,
193			halFastCCSupport		: 1,
194			halBtCoexSupport		: 1;
195	uint32_t	halRxStbcSupport		: 1,
196			halTxStbcSupport		: 1,
197			halGTTSupport			: 1,
198			halCSTSupport			: 1,
199			halRifsRxSupport		: 1,
200			halRifsTxSupport		: 1,
201			hal4AddrAggrSupport		: 1,
202			halExtChanDfsSupport		: 1,
203			halForcePpmSupport		: 1,
204			halEnhancedPmSupport		: 1,
205			halMbssidAggrSupport		: 1,
206			halBssidMatchSupport		: 1,
207			hal4kbSplitTransSupport		: 1,
208			halHasRxSelfLinkedTail		: 1,
209			halSupportsFastClock5GHz	: 1;	/* Hardware supports 5ghz fast clock; check eeprom/channel before using */
210	uint32_t	halWirelessModes;
211	uint16_t	halTotalQueues;
212	uint16_t	halKeyCacheSize;
213	uint16_t	halLow5GhzChan, halHigh5GhzChan;
214	uint16_t	halLow2GhzChan, halHigh2GhzChan;
215	int		halTstampPrecision;
216	int		halRtsAggrLimit;
217	uint8_t		halTxChainMask;
218	uint8_t		halRxChainMask;
219	uint8_t		halNumGpioPins;
220	uint8_t		halNumAntCfg2GHz;
221	uint8_t		halNumAntCfg5GHz;
222	uint32_t	halIntrMask;
223	uint8_t		halTxStreams;
224	uint8_t		halRxStreams;
225} HAL_CAPABILITIES;
226
227struct regDomain;
228
229/*
230 * The ``private area'' follows immediately after the ``public area''
231 * in the data structure returned by ath_hal_attach.  Private data are
232 * used by device-independent code such as the regulatory domain support.
233 * In general, code within the HAL should never depend on data in the
234 * public area.  Instead any public data needed internally should be
235 * shadowed here.
236 *
237 * When declaring a device-specific ath_hal data structure this structure
238 * is assumed to at the front; e.g.
239 *
240 *	struct ath_hal_5212 {
241 *		struct ath_hal_private	ah_priv;
242 *		...
243 *	};
244 *
245 * It might be better to manage the method pointers in this structure
246 * using an indirect pointer to a read-only data structure but this would
247 * disallow class-style method overriding.
248 */
249struct ath_hal_private {
250	struct ath_hal	h;			/* public area */
251
252	/* NB: all methods go first to simplify initialization */
253	HAL_BOOL	(*ah_getChannelEdges)(struct ath_hal*,
254				uint16_t channelFlags,
255				uint16_t *lowChannel, uint16_t *highChannel);
256	u_int		(*ah_getWirelessModes)(struct ath_hal*);
257	HAL_BOOL	(*ah_eepromRead)(struct ath_hal *, u_int off,
258				uint16_t *data);
259	HAL_BOOL	(*ah_eepromWrite)(struct ath_hal *, u_int off,
260				uint16_t data);
261	HAL_BOOL	(*ah_getChipPowerLimits)(struct ath_hal *,
262				struct ieee80211_channel *);
263	int16_t		(*ah_getNfAdjust)(struct ath_hal *,
264				const HAL_CHANNEL_INTERNAL*);
265	void		(*ah_getNoiseFloor)(struct ath_hal *,
266				int16_t nfarray[]);
267
268	void		*ah_eeprom;		/* opaque EEPROM state */
269	uint16_t	ah_eeversion;		/* EEPROM version */
270	void		(*ah_eepromDetach)(struct ath_hal *);
271	HAL_STATUS	(*ah_eepromGet)(struct ath_hal *, int, void *);
272	HAL_BOOL	(*ah_eepromSet)(struct ath_hal *, int, int);
273	uint16_t	(*ah_getSpurChan)(struct ath_hal *, int, HAL_BOOL);
274	HAL_BOOL	(*ah_eepromDiag)(struct ath_hal *, int request,
275			    const void *args, uint32_t argsize,
276			    void **result, uint32_t *resultsize);
277
278	/*
279	 * Device revision information.
280	 */
281	uint16_t	ah_devid;		/* PCI device ID */
282	uint16_t	ah_subvendorid;		/* PCI subvendor ID */
283	uint32_t	ah_macVersion;		/* MAC version id */
284	uint16_t	ah_macRev;		/* MAC revision */
285	uint16_t	ah_phyRev;		/* PHY revision */
286	uint16_t	ah_analog5GhzRev;	/* 2GHz radio revision */
287	uint16_t	ah_analog2GhzRev;	/* 5GHz radio revision */
288	uint8_t		ah_ispcie;		/* PCIE, special treatment */
289
290	HAL_OPMODE	ah_opmode;		/* operating mode from reset */
291	const struct ieee80211_channel *ah_curchan;/* operating channel */
292	HAL_CAPABILITIES ah_caps;		/* device capabilities */
293	uint32_t	ah_diagreg;		/* user-specified AR_DIAG_SW */
294	int16_t		ah_powerLimit;		/* tx power cap */
295	uint16_t	ah_maxPowerLevel;	/* calculated max tx power */
296	u_int		ah_tpScale;		/* tx power scale factor */
297	uint32_t	ah_11nCompat;		/* 11n compat controls */
298
299	/*
300	 * State for regulatory domain handling.
301	 */
302	HAL_REG_DOMAIN	ah_currentRD;		/* EEPROM regulatory domain */
303	HAL_REG_DOMAIN	ah_currentRDext;	/* EEPROM extended regdomain flags */
304	HAL_CHANNEL_INTERNAL ah_channels[AH_MAXCHAN]; /* private chan state */
305	u_int		ah_nchan;		/* valid items in ah_channels */
306	const struct regDomain *ah_rd2GHz;	/* reg state for 2G band */
307	const struct regDomain *ah_rd5GHz;	/* reg state for 5G band */
308
309	uint8_t    	ah_coverageClass;   	/* coverage class */
310	/*
311	 * RF Silent handling; setup according to the EEPROM.
312	 */
313	uint16_t	ah_rfsilent;		/* GPIO pin + polarity */
314	HAL_BOOL	ah_rfkillEnabled;	/* enable/disable RfKill */
315	/*
316	 * Diagnostic support for discriminating HIUERR reports.
317	 */
318	uint32_t	ah_fatalState[6];	/* AR_ISR+shadow regs */
319	int		ah_rxornIsFatal;	/* how to treat HAL_INT_RXORN */
320};
321
322#define	AH_PRIVATE(_ah)	((struct ath_hal_private *)(_ah))
323
324#define	ath_hal_getChannelEdges(_ah, _cf, _lc, _hc) \
325	AH_PRIVATE(_ah)->ah_getChannelEdges(_ah, _cf, _lc, _hc)
326#define	ath_hal_getWirelessModes(_ah) \
327	AH_PRIVATE(_ah)->ah_getWirelessModes(_ah)
328#define	ath_hal_eepromRead(_ah, _off, _data) \
329	AH_PRIVATE(_ah)->ah_eepromRead(_ah, _off, _data)
330#define	ath_hal_eepromWrite(_ah, _off, _data) \
331	AH_PRIVATE(_ah)->ah_eepromWrite(_ah, _off, _data)
332#define	ath_hal_gpioCfgOutput(_ah, _gpio, _type) \
333	(_ah)->ah_gpioCfgOutput(_ah, _gpio, _type)
334#define	ath_hal_gpioCfgInput(_ah, _gpio) \
335	(_ah)->ah_gpioCfgInput(_ah, _gpio)
336#define	ath_hal_gpioGet(_ah, _gpio) \
337	(_ah)->ah_gpioGet(_ah, _gpio)
338#define	ath_hal_gpioSet(_ah, _gpio, _val) \
339	(_ah)->ah_gpioSet(_ah, _gpio, _val)
340#define	ath_hal_gpioSetIntr(_ah, _gpio, _ilevel) \
341	(_ah)->ah_gpioSetIntr(_ah, _gpio, _ilevel)
342#define	ath_hal_getpowerlimits(_ah, _chan) \
343	AH_PRIVATE(_ah)->ah_getChipPowerLimits(_ah, _chan)
344#define ath_hal_getNfAdjust(_ah, _c) \
345	AH_PRIVATE(_ah)->ah_getNfAdjust(_ah, _c)
346#define	ath_hal_getNoiseFloor(_ah, _nfArray) \
347	AH_PRIVATE(_ah)->ah_getNoiseFloor(_ah, _nfArray)
348#define	ath_hal_configPCIE(_ah, _reset) \
349	(_ah)->ah_configPCIE(_ah, _reset)
350#define	ath_hal_disablePCIE(_ah) \
351	(_ah)->ah_disablePCIE(_ah)
352#define	ath_hal_setInterrupts(_ah, _mask) \
353	(_ah)->ah_setInterrupts(_ah, _mask)
354
355#define	ath_hal_eepromDetach(_ah) do {				\
356	if (AH_PRIVATE(_ah)->ah_eepromDetach != AH_NULL)	\
357		AH_PRIVATE(_ah)->ah_eepromDetach(_ah);		\
358} while (0)
359#define	ath_hal_eepromGet(_ah, _param, _val) \
360	AH_PRIVATE(_ah)->ah_eepromGet(_ah, _param, _val)
361#define	ath_hal_eepromSet(_ah, _param, _val) \
362	AH_PRIVATE(_ah)->ah_eepromSet(_ah, _param, _val)
363#define	ath_hal_eepromGetFlag(_ah, _param) \
364	(AH_PRIVATE(_ah)->ah_eepromGet(_ah, _param, AH_NULL) == HAL_OK)
365#define ath_hal_getSpurChan(_ah, _ix, _is2G) \
366	AH_PRIVATE(_ah)->ah_getSpurChan(_ah, _ix, _is2G)
367#define	ath_hal_eepromDiag(_ah, _request, _a, _asize, _r, _rsize) \
368	AH_PRIVATE(_ah)->ah_eepromDiag(_ah, _request, _a, _asize,  _r, _rsize)
369
370#ifndef _NET_IF_IEEE80211_H_
371/*
372 * Stuff that would naturally come from _ieee80211.h
373 */
374#define	IEEE80211_ADDR_LEN		6
375
376#define	IEEE80211_WEP_IVLEN			3	/* 24bit */
377#define	IEEE80211_WEP_KIDLEN			1	/* 1 octet */
378#define	IEEE80211_WEP_CRCLEN			4	/* CRC-32 */
379
380#define	IEEE80211_CRC_LEN			4
381
382#define	IEEE80211_MAX_LEN			(2300 + IEEE80211_CRC_LEN + \
383    (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN + IEEE80211_WEP_CRCLEN))
384#endif /* _NET_IF_IEEE80211_H_ */
385
386#define HAL_TXQ_USE_LOCKOUT_BKOFF_DIS	0x00000001
387
388#define INIT_AIFS		2
389#define INIT_CWMIN		15
390#define INIT_CWMIN_11B		31
391#define INIT_CWMAX		1023
392#define INIT_SH_RETRY		10
393#define INIT_LG_RETRY		10
394#define INIT_SSH_RETRY		32
395#define INIT_SLG_RETRY		32
396
397typedef struct {
398	uint32_t	tqi_ver;		/* HAL TXQ verson */
399	HAL_TX_QUEUE	tqi_type;		/* hw queue type*/
400	HAL_TX_QUEUE_SUBTYPE tqi_subtype;	/* queue subtype, if applicable */
401	HAL_TX_QUEUE_FLAGS tqi_qflags;		/* queue flags */
402	uint32_t	tqi_priority;
403	uint32_t	tqi_aifs;		/* aifs */
404	uint32_t	tqi_cwmin;		/* cwMin */
405	uint32_t	tqi_cwmax;		/* cwMax */
406	uint16_t	tqi_shretry;		/* frame short retry limit */
407	uint16_t	tqi_lgretry;		/* frame long retry limit */
408	uint32_t	tqi_cbrPeriod;
409	uint32_t	tqi_cbrOverflowLimit;
410	uint32_t	tqi_burstTime;
411	uint32_t	tqi_readyTime;
412	uint32_t	tqi_physCompBuf;
413	uint32_t	tqi_intFlags;		/* flags for internal use */
414} HAL_TX_QUEUE_INFO;
415
416extern	HAL_BOOL ath_hal_setTxQProps(struct ath_hal *ah,
417		HAL_TX_QUEUE_INFO *qi, const HAL_TXQ_INFO *qInfo);
418extern	HAL_BOOL ath_hal_getTxQProps(struct ath_hal *ah,
419		HAL_TXQ_INFO *qInfo, const HAL_TX_QUEUE_INFO *qi);
420
421typedef enum {
422	HAL_ANI_PRESENT = 0x1,			/* is ANI support present */
423	HAL_ANI_NOISE_IMMUNITY_LEVEL = 0x2,	/* set level */
424	HAL_ANI_OFDM_WEAK_SIGNAL_DETECTION = 0x4,	/* enable/disable */
425	HAL_ANI_CCK_WEAK_SIGNAL_THR = 0x8,		/* enable/disable */
426	HAL_ANI_FIRSTEP_LEVEL = 0x10,			/* set level */
427	HAL_ANI_SPUR_IMMUNITY_LEVEL = 0x20,		/* set level */
428	HAL_ANI_MODE = 0x40,	/* 0 => manual, 1 => auto (XXX do not change) */
429	HAL_ANI_PHYERR_RESET =0x80,			/* reset phy error stats */
430	HAL_ANI_ALL = 0xff
431} HAL_ANI_CMD;
432
433#define	HAL_SPUR_VAL_MASK		0x3FFF
434#define	HAL_SPUR_CHAN_WIDTH		87
435#define	HAL_BIN_WIDTH_BASE_100HZ	3125
436#define	HAL_BIN_WIDTH_TURBO_100HZ	6250
437#define	HAL_MAX_BINS_ALLOWED		28
438
439#define	IS_CHAN_5GHZ(_c)	((_c)->channel > 4900)
440#define	IS_CHAN_2GHZ(_c)	(!IS_CHAN_5GHZ(_c))
441
442#define	IS_CHAN_IN_PUBLIC_SAFETY_BAND(_c) ((_c) > 4940 && (_c) < 4990)
443
444/*
445 * Deduce if the host cpu has big- or litt-endian byte order.
446 */
447static __inline__ int
448isBigEndian(void)
449{
450	union {
451		int32_t i;
452		char c[4];
453	} u;
454	u.i = 1;
455	return (u.c[0] == 0);
456}
457
458/* unalligned little endian access */
459#define LE_READ_2(p)							\
460	((uint16_t)							\
461	 ((((const uint8_t *)(p))[0]    ) | (((const uint8_t *)(p))[1]<< 8)))
462#define LE_READ_4(p)							\
463	((uint32_t)							\
464	 ((((const uint8_t *)(p))[0]    ) | (((const uint8_t *)(p))[1]<< 8) |\
465	  (((const uint8_t *)(p))[2]<<16) | (((const uint8_t *)(p))[3]<<24)))
466
467/*
468 * Register manipulation macros that expect bit field defines
469 * to follow the convention that an _S suffix is appended for
470 * a shift count, while the field mask has no suffix.
471 */
472#define	SM(_v, _f)	(((_v) << _f##_S) & (_f))
473#define	MS(_v, _f)	(((_v) & (_f)) >> _f##_S)
474#define OS_REG_RMW(_a, _r, _set, _clr)    \
475	OS_REG_WRITE(_a, _r, (OS_REG_READ(_a, _r) & ~(_clr)) | (_set))
476#define	OS_REG_RMW_FIELD(_a, _r, _f, _v) \
477	OS_REG_WRITE(_a, _r, \
478		(OS_REG_READ(_a, _r) &~ (_f)) | (((_v) << _f##_S) & (_f)))
479#define	OS_REG_SET_BIT(_a, _r, _f) \
480	OS_REG_WRITE(_a, _r, OS_REG_READ(_a, _r) | (_f))
481#define	OS_REG_CLR_BIT(_a, _r, _f) \
482	OS_REG_WRITE(_a, _r, OS_REG_READ(_a, _r) &~ (_f))
483
484/* Analog register writes may require a delay between each one (eg Merlin?) */
485#define	OS_A_REG_RMW_FIELD(_a, _r, _f, _v) \
486	do { OS_REG_WRITE(_a, _r, (OS_REG_READ(_a, _r) &~ (_f)) | (((_v) << _f##_S) & (_f))) ; OS_DELAY(100); } while (0)
487
488/* system-configurable parameters */
489extern	int ath_hal_dma_beacon_response_time;	/* in TU's */
490extern	int ath_hal_sw_beacon_response_time;	/* in TU's */
491extern	int ath_hal_additional_swba_backoff;	/* in TU's */
492extern	int ath_hal_ar5416_biasadj;		/* 1 or 0 */
493
494/* wait for the register contents to have the specified value */
495extern	HAL_BOOL ath_hal_wait(struct ath_hal *, u_int reg,
496		uint32_t mask, uint32_t val);
497extern	HAL_BOOL ath_hal_waitfor(struct ath_hal *, u_int reg,
498		uint32_t mask, uint32_t val, uint32_t timeout);
499
500/* return the first n bits in val reversed */
501extern	uint32_t ath_hal_reverseBits(uint32_t val, uint32_t n);
502
503/* printf interfaces */
504extern	void ath_hal_printf(struct ath_hal *, const char*, ...)
505		__printflike(2,3);
506extern	void ath_hal_vprintf(struct ath_hal *, const char*, __va_list)
507		__printflike(2, 0);
508extern	const char* ath_hal_ether_sprintf(const uint8_t *mac);
509
510/* allocate and free memory */
511extern	void *ath_hal_malloc(size_t);
512extern	void ath_hal_free(void *);
513
514/* common debugging interfaces */
515#ifdef AH_DEBUG
516#include "ah_debug.h"
517extern	int ath_hal_debug;
518#define	HALDEBUG(_ah, __m, ...) \
519	do {							\
520		if (ath_hal_debug & (__m)) {			\
521			DO_HALDEBUG((_ah), (__m), __VA_ARGS__);	\
522		}						\
523	} while(0);
524
525extern	void DO_HALDEBUG(struct ath_hal *ah, u_int mask, const char* fmt, ...)
526	__printflike(3,4);
527#else
528#define HALDEBUG(_ah, __m, _fmt, ...)
529#endif /* AH_DEBUG */
530
531/*
532 * Register logging definitions shared with ardecode.
533 */
534#include "ah_decode.h"
535
536/*
537 * Common assertion interface.  Note: it is a bad idea to generate
538 * an assertion failure for any recoverable event.  Instead catch
539 * the violation and, if possible, fix it up or recover from it; either
540 * with an error return value or a diagnostic messages.  System software
541 * does not panic unless the situation is hopeless.
542 */
543#ifdef AH_ASSERT
544extern	void ath_hal_assert_failed(const char* filename,
545		int lineno, const char* msg);
546
547#define	HALASSERT(_x) do {					\
548	if (!(_x)) {						\
549		ath_hal_assert_failed(__FILE__, __LINE__, #_x);	\
550	}							\
551} while (0)
552#else
553#define	HALASSERT(_x)
554#endif /* AH_ASSERT */
555
556/*
557 * Regulatory domain support.
558 */
559
560/*
561 * Return the max allowed antenna gain and apply any regulatory
562 * domain specific changes.
563 */
564u_int	ath_hal_getantennareduction(struct ath_hal *ah,
565	    const struct ieee80211_channel *chan, u_int twiceGain);
566
567/*
568 * Return the test group for the specific channel based on
569 * the current regulatory setup.
570 */
571u_int	ath_hal_getctl(struct ath_hal *, const struct ieee80211_channel *);
572
573/*
574 * Map a public channel definition to the corresponding
575 * internal data structure.  This implicitly specifies
576 * whether or not the specified channel is ok to use
577 * based on the current regulatory domain constraints.
578 */
579#ifndef AH_DEBUG
580static OS_INLINE HAL_CHANNEL_INTERNAL *
581ath_hal_checkchannel(struct ath_hal *ah, const struct ieee80211_channel *c)
582{
583	HAL_CHANNEL_INTERNAL *cc;
584
585	HALASSERT(c->ic_devdata < AH_PRIVATE(ah)->ah_nchan);
586	cc = &AH_PRIVATE(ah)->ah_channels[c->ic_devdata];
587	HALASSERT(c->ic_freq == cc->channel || IEEE80211_IS_CHAN_GSM(c));
588	return cc;
589}
590#else
591/* NB: non-inline version that checks state */
592HAL_CHANNEL_INTERNAL *ath_hal_checkchannel(struct ath_hal *,
593		const struct ieee80211_channel *);
594#endif /* AH_DEBUG */
595
596/*
597 * Return the h/w frequency for a channel.  This may be
598 * different from ic_freq if this is a GSM device that
599 * takes 2.4GHz frequencies and down-converts them.
600 */
601static OS_INLINE uint16_t
602ath_hal_gethwchannel(struct ath_hal *ah, const struct ieee80211_channel *c)
603{
604	return ath_hal_checkchannel(ah, c)->channel;
605}
606
607/*
608 * Convert between microseconds and core system clocks.
609 */
610extern	u_int ath_hal_mac_clks(struct ath_hal *ah, u_int usecs);
611extern	u_int ath_hal_mac_usec(struct ath_hal *ah, u_int clks);
612
613/*
614 * Generic get/set capability support.  Each chip overrides
615 * this routine to support chip-specific capabilities.
616 */
617extern	HAL_STATUS ath_hal_getcapability(struct ath_hal *ah,
618		HAL_CAPABILITY_TYPE type, uint32_t capability,
619		uint32_t *result);
620extern	HAL_BOOL ath_hal_setcapability(struct ath_hal *ah,
621		HAL_CAPABILITY_TYPE type, uint32_t capability,
622		uint32_t setting, HAL_STATUS *status);
623
624/* The diagnostic codes used to be internally defined here -adrian */
625#include "ah_diagcodes.h"
626
627enum {
628    HAL_BB_HANG_DFS		= 0x0001,
629    HAL_BB_HANG_RIFS		= 0x0002,
630    HAL_BB_HANG_RX_CLEAR	= 0x0004,
631    HAL_BB_HANG_UNKNOWN		= 0x0080,
632
633    HAL_MAC_HANG_SIG1		= 0x0100,
634    HAL_MAC_HANG_SIG2		= 0x0200,
635    HAL_MAC_HANG_UNKNOWN	= 0x8000,
636
637    HAL_BB_HANGS = HAL_BB_HANG_DFS
638		 | HAL_BB_HANG_RIFS
639		 | HAL_BB_HANG_RX_CLEAR
640		 | HAL_BB_HANG_UNKNOWN,
641    HAL_MAC_HANGS = HAL_MAC_HANG_SIG1
642		 | HAL_MAC_HANG_SIG2
643		 | HAL_MAC_HANG_UNKNOWN,
644};
645
646/*
647 * Device revision information.
648 */
649typedef struct {
650	uint16_t	ah_devid;		/* PCI device ID */
651	uint16_t	ah_subvendorid;		/* PCI subvendor ID */
652	uint32_t	ah_macVersion;		/* MAC version id */
653	uint16_t	ah_macRev;		/* MAC revision */
654	uint16_t	ah_phyRev;		/* PHY revision */
655	uint16_t	ah_analog5GhzRev;	/* 2GHz radio revision */
656	uint16_t	ah_analog2GhzRev;	/* 5GHz radio revision */
657} HAL_REVS;
658
659/*
660 * Argument payload for HAL_DIAG_SETKEY.
661 */
662typedef struct {
663	HAL_KEYVAL	dk_keyval;
664	uint16_t	dk_keyix;	/* key index */
665	uint8_t		dk_mac[IEEE80211_ADDR_LEN];
666	int		dk_xor;		/* XOR key data */
667} HAL_DIAG_KEYVAL;
668
669/*
670 * Argument payload for HAL_DIAG_EEWRITE.
671 */
672typedef struct {
673	uint16_t	ee_off;		/* eeprom offset */
674	uint16_t	ee_data;	/* write data */
675} HAL_DIAG_EEVAL;
676
677
678typedef struct {
679	u_int offset;		/* reg offset */
680	uint32_t val;		/* reg value  */
681} HAL_DIAG_REGVAL;
682
683/*
684 * 11n compatibility tweaks.
685 */
686#define	HAL_DIAG_11N_SERVICES	0x00000003
687#define	HAL_DIAG_11N_SERVICES_S	0
688#define	HAL_DIAG_11N_TXSTOMP	0x0000000c
689#define	HAL_DIAG_11N_TXSTOMP_S	2
690
691typedef struct {
692	int		maxNoiseImmunityLevel;	/* [0..4] */
693	int		totalSizeDesired[5];
694	int		coarseHigh[5];
695	int		coarseLow[5];
696	int		firpwr[5];
697
698	int		maxSpurImmunityLevel;	/* [0..7] */
699	int		cycPwrThr1[8];
700
701	int		maxFirstepLevel;	/* [0..2] */
702	int		firstep[3];
703
704	uint32_t	ofdmTrigHigh;
705	uint32_t	ofdmTrigLow;
706	int32_t		cckTrigHigh;
707	int32_t		cckTrigLow;
708	int32_t		rssiThrLow;
709	int32_t		rssiThrHigh;
710
711	int		period;			/* update listen period */
712} HAL_ANI_PARAMS;
713
714extern	HAL_BOOL ath_hal_getdiagstate(struct ath_hal *ah, int request,
715			const void *args, uint32_t argsize,
716			void **result, uint32_t *resultsize);
717
718/*
719 * Setup a h/w rate table for use.
720 */
721extern	void ath_hal_setupratetable(struct ath_hal *ah, HAL_RATE_TABLE *rt);
722
723/*
724 * Common routine for implementing getChanNoise api.
725 */
726int16_t	ath_hal_getChanNoise(struct ath_hal *, const struct ieee80211_channel *);
727
728/*
729 * Initialization support.
730 */
731typedef struct {
732	const uint32_t	*data;
733	int		rows, cols;
734} HAL_INI_ARRAY;
735
736#define	HAL_INI_INIT(_ia, _data, _cols) do {			\
737	(_ia)->data = (const uint32_t *)(_data);		\
738	(_ia)->rows = sizeof(_data) / sizeof((_data)[0]);	\
739	(_ia)->cols = (_cols);					\
740} while (0)
741#define	HAL_INI_VAL(_ia, _r, _c) \
742	((_ia)->data[((_r)*(_ia)->cols) + (_c)])
743
744/*
745 * OS_DELAY() does a PIO READ on the PCI bus which allows
746 * other cards' DMA reads to complete in the middle of our reset.
747 */
748#define DMA_YIELD(x) do {		\
749	if ((++(x) % 64) == 0)		\
750		OS_DELAY(1);		\
751} while (0)
752
753#define HAL_INI_WRITE_ARRAY(ah, regArray, col, regWr) do {             	\
754	int r;								\
755	for (r = 0; r < N(regArray); r++) {				\
756		OS_REG_WRITE(ah, (regArray)[r][0], (regArray)[r][col]);	\
757		DMA_YIELD(regWr);					\
758	}								\
759} while (0)
760
761#define HAL_INI_WRITE_BANK(ah, regArray, bankData, regWr) do {		\
762	int r;								\
763	for (r = 0; r < N(regArray); r++) {				\
764		OS_REG_WRITE(ah, (regArray)[r][0], (bankData)[r]);	\
765		DMA_YIELD(regWr);					\
766	}								\
767} while (0)
768
769extern	int ath_hal_ini_write(struct ath_hal *ah, const HAL_INI_ARRAY *ia,
770		int col, int regWr);
771extern	void ath_hal_ini_bank_setup(uint32_t data[], const HAL_INI_ARRAY *ia,
772		int col);
773extern	int ath_hal_ini_bank_write(struct ath_hal *ah, const HAL_INI_ARRAY *ia,
774		const uint32_t data[], int regWr);
775
776#define	CCK_SIFS_TIME		10
777#define	CCK_PREAMBLE_BITS	144
778#define	CCK_PLCP_BITS		48
779
780#define	OFDM_SIFS_TIME		16
781#define	OFDM_PREAMBLE_TIME	20
782#define	OFDM_PLCP_BITS		22
783#define	OFDM_SYMBOL_TIME	4
784
785#define	OFDM_HALF_SIFS_TIME	32
786#define	OFDM_HALF_PREAMBLE_TIME	40
787#define	OFDM_HALF_PLCP_BITS	22
788#define	OFDM_HALF_SYMBOL_TIME	8
789
790#define	OFDM_QUARTER_SIFS_TIME 		64
791#define	OFDM_QUARTER_PREAMBLE_TIME	80
792#define	OFDM_QUARTER_PLCP_BITS		22
793#define	OFDM_QUARTER_SYMBOL_TIME	16
794
795#define	TURBO_SIFS_TIME		8
796#define	TURBO_PREAMBLE_TIME	14
797#define	TURBO_PLCP_BITS		22
798#define	TURBO_SYMBOL_TIME	4
799
800#define	WLAN_CTRL_FRAME_SIZE	(2+2+6+4)	/* ACK+FCS */
801
802/* Generic EEPROM board value functions */
803extern	HAL_BOOL ath_ee_getLowerUpperIndex(uint8_t target, uint8_t *pList,
804	uint16_t listSize, uint16_t *indexL, uint16_t *indexR);
805extern	HAL_BOOL ath_ee_FillVpdTable(uint8_t pwrMin, uint8_t pwrMax,
806	uint8_t *pPwrList, uint8_t *pVpdList, uint16_t numIntercepts,
807	uint8_t *pRetVpdList);
808extern	int16_t ath_ee_interpolate(uint16_t target, uint16_t srcLeft,
809	uint16_t srcRight, int16_t targetLeft, int16_t targetRight);
810
811/* Whether 5ghz fast clock is needed */
812/*
813 * The chipset (Merlin, AR9300/later) should set the capability flag below;
814 * this flag simply says that the hardware can do it, not that the EEPROM
815 * says it can.
816 *
817 * Merlin 2.0/2.1 chips with an EEPROM version > 16 do 5ghz fast clock
818 *   if the relevant eeprom flag is set.
819 * Merlin 2.0/2.1 chips with an EEPROM version <= 16 do 5ghz fast clock
820 *   by default.
821 */
822#define	IS_5GHZ_FAST_CLOCK_EN(_ah, _c) \
823	(IEEE80211_IS_CHAN_5GHZ(_c) && \
824	 AH_PRIVATE((_ah))->ah_caps.halSupportsFastClock5GHz && \
825	ath_hal_eepromGetFlag((_ah), AR_EEP_FSTCLK_5G))
826
827
828#endif /* _ATH_AH_INTERAL_H_ */
829