add the new ath9k driver (loads successfully on an AR9160 card, but still seems to...
[oweals/openwrt.git] / package / ath9k / src / drivers / net / wireless / ath9k / core.h
1 /*
2  * Copyright (c) 2008 Atheros Communications Inc.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 #ifndef CORE_H
18 #define CORE_H
19
20 #include <linux/version.h>
21 #include <linux/autoconf.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/spinlock.h>
25 #include <linux/errno.h>
26 #include <linux/skbuff.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ip.h>
30 #include <linux/tcp.h>
31 #include <linux/in.h>
32 #include <linux/delay.h>
33 #include <linux/wait.h>
34 #include <linux/pci.h>
35 #include <linux/interrupt.h>
36 #include <linux/sched.h>
37 #include <linux/list.h>
38 #include <asm/byteorder.h>
39 #include <linux/scatterlist.h>
40 #include <asm/page.h>
41 #include <net/mac80211.h>
42
43 #include "ath9k.h"
44 #include "rc.h"
45
46 struct ath_node;
47
48 /******************/
49 /* Utility macros */
50 /******************/
51
52 /* An attempt will be made to merge these link list helpers upstream
53  * instead */
54
55 static inline void __list_splice_tail(const struct list_head *list,
56                                  struct list_head *head)
57 {
58         struct list_head *first = list->next;
59         struct list_head *last = list->prev;
60         struct list_head *current_tail = head->prev;
61
62         current_tail->next = first;
63         last->next = head;
64         head->prev = last;
65         first->prev = current_tail;
66 }
67
68 static inline void __list_cut_position(struct list_head *list,
69                 struct list_head *head, struct list_head *entry)
70 {
71         struct list_head *new_first =
72                 (entry->next != head) ? entry->next : head;
73         list->next = head->next;
74         list->next->prev = list;
75         list->prev = entry;
76         entry->next = list;
77         head->next = new_first;
78         new_first->prev = head;
79 }
80
81 /**
82  * list_splice_tail - join two lists, each list being a queue
83  * @list: the new list to add.
84  * @head: the place to add it in the first list.
85  */
86 static inline void list_splice_tail(const struct list_head *list,
87                                 struct list_head *head)
88 {
89         if (!list_empty(list))
90                 __list_splice_tail(list, head);
91 }
92
93 /**
94  * list_splice_tail_init - join two lists, each list being a queue, and
95  *      reinitialise the emptied list.
96  * @list: the new list to add.
97  * @head: the place to add it in the first list.
98  *
99  * The list at @list is reinitialised
100  */
101 static inline void list_splice_tail_init(struct list_head *list,
102                                     struct list_head *head)
103 {
104         if (!list_empty(list)) {
105                 __list_splice_tail(list, head);
106                 INIT_LIST_HEAD(list);
107         }
108 }
109
110 /**
111  * list_cut_position - cut a list into two
112  * @list: a new list to add all removed entries
113  * @head: a list with entries
114  * @entry: an entry within head, could be the head itself
115  *      and if so we won't won't cut the list
116  */
117 static inline void list_cut_position(struct list_head *list,
118                 struct list_head *head, struct list_head *entry)
119 {
120         BUG_ON(list_empty(head));
121         if (list_is_singular(head))
122                 BUG_ON(head->next != entry && head != entry);
123         if (entry == head)
124                 INIT_LIST_HEAD(list);
125         else
126                 __list_cut_position(list, head, entry);
127 }
128
129 /* Macro to expand scalars to 64-bit objects */
130
131 #define ito64(x) (sizeof(x) == 8) ? \
132         (((unsigned long long int)(x)) & (0xff)) : \
133         (sizeof(x) == 16) ? \
134         (((unsigned long long int)(x)) & 0xffff) : \
135         ((sizeof(x) == 32) ?                    \
136          (((unsigned long long int)(x)) & 0xffffffff) : \
137         (unsigned long long int)(x))
138
139 /* increment with wrap-around */
140 #define INCR(_l, _sz)   do { \
141         (_l)++; \
142         (_l) &= ((_sz) - 1); \
143         } while (0)
144
145 /* decrement with wrap-around */
146 #define DECR(_l,  _sz)  do { \
147         (_l)--; \
148         (_l) &= ((_sz) - 1); \
149         } while (0)
150
151 #define A_MAX(a, b) ((a) > (b) ? (a) : (b))
152
153 #define ASSERT(exp) do {                        \
154                 if (unlikely(!(exp))) {         \
155                         BUG();                  \
156                 }                               \
157         } while (0)
158
159 #define KASSERT(exp, msg) do {                  \
160                 if (unlikely(!(exp))) {         \
161                         printk msg;             \
162                         BUG();                  \
163                 }                               \
164         } while (0)
165
166 /* XXX: remove */
167 #define memzero(_buf, _len) memset(_buf, 0, _len)
168
169 #define get_dma_mem_context(var, field) (&((var)->field))
170 #define copy_dma_mem_context(dst, src)  (*dst = *src)
171
172 #define ATH9K_BH_STATUS_INTACT          0
173 #define ATH9K_BH_STATUS_CHANGE          1
174
175 #define ATH_TXQ_SETUP(sc, i)        ((sc)->sc_txqsetup & (1<<i))
176
177 static inline unsigned long get_timestamp(void)
178 {
179         return ((jiffies / HZ) * 1000) + (jiffies % HZ) * (1000 / HZ);
180 }
181
182 /*************/
183 /* Debugging */
184 /*************/
185
186 enum ATH_DEBUG {
187         ATH_DEBUG_XMIT          = 0x00000001,   /* basic xmit operation */
188         ATH_DEBUG_RECV          = 0x00000002,   /* basic recv operation */
189         ATH_DEBUG_BEACON        = 0x00000004,   /* beacon handling */
190         ATH_DEBUG_TX_PROC       = 0x00000008,   /* tx ISR proc */
191         ATH_DEBUG_RX_PROC       = 0x00000010,   /* rx ISR proc */
192         ATH_DEBUG_BEACON_PROC   = 0x00000020,   /* beacon ISR proc */
193         ATH_DEBUG_RATE          = 0x00000040,   /* rate control */
194         ATH_DEBUG_CONFIG        = 0x00000080,   /* configuration */
195         ATH_DEBUG_KEYCACHE      = 0x00000100,   /* key cache management */
196         ATH_DEBUG_NODE          = 0x00000200,   /* node management */
197         ATH_DEBUG_AGGR          = 0x00000400,   /* Aggregation */
198         ATH_DEBUG_CWM           = 0x00000800,   /* Channel Width Management */
199         ATH_DEBUG_FATAL         = 0x00001000,   /* fatal errors */
200         ATH_DEBUG_ANY           = 0xffffffff
201 };
202
203 #define DBG_DEFAULT (ATH_DEBUG_FATAL)
204
205 #define DPRINTF(sc, _m, _fmt, ...) do {                 \
206                 if (sc->sc_debug & (_m))                \
207                         printk(_fmt , ##__VA_ARGS__);   \
208         } while (0)
209
210 /***************************/
211 /* Load-time Configuration */
212 /***************************/
213
214 /* Per-instance load-time (note: NOT run-time) configurations
215  * for Atheros Device */
216 struct ath_config {
217         u_int8_t    chainmask_sel; /* enable automatic tx chainmask selection */
218         u_int32_t   ath_aggr_prot;
219         u_int16_t   txpowlimit;
220         u_int16_t   txpowlimit_override;
221         u_int8_t    cabqReadytime; /* Cabq Readytime % */
222         u_int8_t    swBeaconProcess; /* Process received beacons
223                                         in SW (vs HW) */
224 };
225
226 /***********************/
227 /* Chainmask Selection */
228 /***********************/
229
230 #define ATH_CHAINMASK_SEL_TIMEOUT          6000
231 /* Default - Number of last RSSI values that is used for
232  * chainmask selection */
233 #define ATH_CHAINMASK_SEL_RSSI_CNT         10
234 /* Means use 3x3 chainmask instead of configured chainmask */
235 #define ATH_CHAINMASK_SEL_3X3              7
236 /* Default - Rssi threshold below which we have to switch to 3x3 */
237 #define ATH_CHAINMASK_SEL_UP_RSSI_THRES    20
238 /* Default - Rssi threshold above which we have to switch to
239  * user configured values */
240 #define ATH_CHAINMASK_SEL_DOWN_RSSI_THRES  35
241 /* Struct to store the chainmask select related info */
242 struct ath_chainmask_sel {
243         struct timer_list   timer;
244         int                 cur_tx_mask;        /* user configured or 3x3 */
245         int                 cur_rx_mask;        /* user configured or 3x3 */
246         int                 tx_avgrssi;
247         u8                  switch_allowed:1,   /* timer will set this */
248                             cm_sel_enabled:1;
249 };
250
251 int ath_chainmask_sel_logic(struct ath_softc *sc, struct ath_node *an);
252
253 /*************************/
254 /* Descriptor Management */
255 /*************************/
256
257 /* Number of descriptors per buffer. The only case where we see skbuff
258 chains is due to FF aggregation in the driver. */
259 #define ATH_TXDESC          1
260 /* if there's more fragment for this MSDU */
261 #define ATH_BF_MORE_MPDU    1
262 #define ATH_TXBUF_RESET(_bf) do {                               \
263                 (_bf)->bf_status = 0;                           \
264                 (_bf)->bf_lastbf = NULL;                        \
265                 (_bf)->bf_lastfrm = NULL;                       \
266                 (_bf)->bf_next = NULL;                          \
267                 memzero(&((_bf)->bf_state),                     \
268                             sizeof(struct ath_buf_state));      \
269         } while (0)
270
271 struct ath_buf_state {
272         int bfs_nframes;        /* # frames in aggregate */
273         u_int16_t bfs_al;       /* length of aggregate */
274         u_int16_t bfs_frmlen;   /* length of frame */
275         int bfs_seqno;          /* sequence number */
276         int bfs_tidno;          /* tid of this frame */
277         int bfs_retries;        /* current retries */
278         struct ath_rc_series bfs_rcs[4];        /* rate series */
279         u8 bfs_isdata:1;        /* is a data frame/aggregate */
280         u8 bfs_isaggr:1;        /* is an aggregate */
281         u8 bfs_isampdu:1;       /* is an a-mpdu, aggregate or not */
282         u8 bfs_ht:1;            /* is an HT frame */
283         u8 bfs_isretried:1;     /* is retried */
284         u8 bfs_isxretried:1;    /* is excessive retried */
285         u8 bfs_shpreamble:1;    /* is short preamble */
286         u8 bfs_isbar:1; /* is a BAR */
287         u8 bfs_ispspoll:1;      /* is a PS-Poll */
288         u8 bfs_aggrburst:1;     /* is a aggr burst */
289         u8 bfs_calcairtime:1;   /* requests airtime be calculated
290                                 when set for tx frame */
291         int bfs_rifsburst_elem; /* RIFS burst/bar */
292         int bfs_nrifsubframes;  /* # of elements in burst */
293         enum hal_key_type bfs_keytype;  /* key type use to encrypt this frame */
294 };
295
296 #define bf_nframes              bf_state.bfs_nframes
297 #define bf_al                   bf_state.bfs_al
298 #define bf_frmlen               bf_state.bfs_frmlen
299 #define bf_retries              bf_state.bfs_retries
300 #define bf_seqno                bf_state.bfs_seqno
301 #define bf_tidno                bf_state.bfs_tidno
302 #define bf_rcs                  bf_state.bfs_rcs
303 #define bf_isdata               bf_state.bfs_isdata
304 #define bf_isaggr               bf_state.bfs_isaggr
305 #define bf_isampdu              bf_state.bfs_isampdu
306 #define bf_ht                   bf_state.bfs_ht
307 #define bf_isretried            bf_state.bfs_isretried
308 #define bf_isxretried           bf_state.bfs_isxretried
309 #define bf_shpreamble           bf_state.bfs_shpreamble
310 #define bf_rifsburst_elem       bf_state.bfs_rifsburst_elem
311 #define bf_nrifsubframes        bf_state.bfs_nrifsubframes
312 #define bf_keytype              bf_state.bfs_keytype
313 #define bf_isbar                bf_state.bfs_isbar
314 #define bf_ispspoll             bf_state.bfs_ispspoll
315 #define bf_aggrburst            bf_state.bfs_aggrburst
316 #define bf_calcairtime          bf_state.bfs_calcairtime
317
318 /*
319  * Abstraction of a contiguous buffer to transmit/receive.  There is only
320  * a single hw descriptor encapsulated here.
321  */
322
323 struct ath_buf {
324         struct list_head list;
325         struct list_head *last;
326         struct ath_buf *bf_lastbf;      /* last buf of this unit (a frame or
327                                         an aggregate) */
328         struct ath_buf *bf_lastfrm;     /* last buf of this frame */
329         struct ath_buf *bf_next;        /* next subframe in the aggregate */
330         struct ath_buf *bf_rifslast;    /* last buf for RIFS burst */
331         void *bf_mpdu;                  /* enclosing frame structure */
332         void *bf_node;                  /* pointer to the node */
333         struct ath_desc *bf_desc;       /* virtual addr of desc */
334         dma_addr_t bf_daddr;            /* physical addr of desc */
335         dma_addr_t bf_buf_addr;         /* physical addr of data buffer */
336         u_int32_t bf_status;
337         u_int16_t bf_flags;             /* tx descriptor flags */
338         struct ath_buf_state bf_state;  /* buffer state */
339         dma_addr_t bf_dmacontext;
340 };
341
342 /*
343  * reset the rx buffer.
344  * any new fields added to the athbuf and require
345  * reset need to be added to this macro.
346  * currently bf_status is the only one requires that
347  * requires reset.
348  */
349 #define ATH_RXBUF_RESET(_bf)    ((_bf)->bf_status = 0)
350
351 /* hw processing complete, desc processed by hal */
352 #define ATH_BUFSTATUS_DONE      0x00000001
353 /* hw processing complete, desc hold for hw */
354 #define ATH_BUFSTATUS_STALE     0x00000002
355 /* Rx-only: OS is done with this packet and it's ok to queued it to hw */
356 #define ATH_BUFSTATUS_FREE      0x00000004
357
358 /* DMA state for tx/rx descriptors */
359
360 struct ath_descdma {
361         const char *dd_name;
362         struct ath_desc *dd_desc;       /* descriptors  */
363         dma_addr_t dd_desc_paddr;       /* physical addr of dd_desc  */
364         u_int32_t dd_desc_len;          /* size of dd_desc  */
365         struct ath_buf *dd_bufptr;      /* associated buffers */
366         dma_addr_t dd_dmacontext;
367 };
368
369 /* Abstraction of a received RX MPDU/MMPDU, or a RX fragment */
370
371 struct ath_rx_context {
372         struct ath_buf *ctx_rxbuf;      /* associated ath_buf for rx */
373 };
374 #define ATH_RX_CONTEXT(skb) ((struct ath_rx_context *)skb->cb)
375
376 int ath_descdma_setup(struct ath_softc *sc,
377                       struct ath_descdma *dd,
378                       struct list_head *head,
379                       const char *name,
380                       int nbuf,
381                       int ndesc);
382 int ath_desc_alloc(struct ath_softc *sc);
383 void ath_desc_free(struct ath_softc *sc);
384 void ath_descdma_cleanup(struct ath_softc *sc,
385                          struct ath_descdma *dd,
386                          struct list_head *head);
387 void ath_desc_swap(struct ath_desc *ds);
388
389 /******/
390 /* RX */
391 /******/
392
393 #define ATH_MAX_ANTENNA          3
394 #define ATH_RXBUF                512
395 #define ATH_RX_TIMEOUT           40      /* 40 milliseconds */
396 #define WME_NUM_TID              16
397 #define IEEE80211_BAR_CTL_TID_M  0xF000  /* tid mask */
398 #define IEEE80211_BAR_CTL_TID_S  2       /* tid shift */
399
400 enum ATH_RX_TYPE {
401         ATH_RX_NON_CONSUMED = 0,
402         ATH_RX_CONSUMED
403 };
404
405 /* per frame rx status block */
406 struct ath_recv_status {
407         u_int64_t tsf;          /* mac tsf */
408         int8_t rssi;            /* RSSI (noise floor ajusted) */
409         int8_t rssictl[ATH_MAX_ANTENNA];        /* RSSI (noise floor ajusted) */
410         int8_t rssiextn[ATH_MAX_ANTENNA];       /* RSSI (noise floor ajusted) */
411         int8_t abs_rssi;        /* absolute RSSI */
412         u_int8_t rateieee;      /* data rate received (IEEE rate code) */
413         u_int8_t ratecode;      /* phy rate code */
414         int rateKbps;           /* data rate received (Kbps) */
415         int antenna;            /* rx antenna */
416         int flags;              /* status of associated skb */
417 #define ATH_RX_FCS_ERROR        0x01
418 #define ATH_RX_MIC_ERROR        0x02
419 #define ATH_RX_DECRYPT_ERROR    0x04
420 #define ATH_RX_RSSI_VALID       0x08
421 /* if any of ctl,extn chainrssis are valid */
422 #define ATH_RX_CHAIN_RSSI_VALID 0x10
423 /* if extn chain rssis are valid */
424 #define ATH_RX_RSSI_EXTN_VALID  0x20
425 /* set if 40Mhz, clear if 20Mhz */
426 #define ATH_RX_40MHZ            0x40
427 /* set if short GI, clear if full GI */
428 #define ATH_RX_SHORT_GI         0x80
429 };
430
431 struct ath_rxbuf {
432         struct sk_buff                  *rx_wbuf; /* buffer */
433         unsigned long                   rx_time; /* system time when received */
434         struct ath_recv_status          rx_status; /* cached rx status */
435 };
436
437 /* Per-TID aggregate receiver state for a node */
438 struct ath_arx_tid {
439         struct ath_node     *an;        /* parent ath node */
440         struct ath_rxbuf    *rxbuf;     /* re-ordering buffer */
441         struct timer_list   timer;
442         spinlock_t          tidlock;    /* lock to protect this TID structure */
443         int                 baw_head;   /* seq_next at head */
444         int                 baw_tail;   /* tail of block-ack window */
445         int                 seq_reset;  /* need to reset start sequence */
446         int                 addba_exchangecomplete;
447         u_int16_t           seq_next;   /* next expected sequence */
448         u_int16_t           baw_size;   /* block-ack window size */
449 };
450
451 /* Per-node receiver aggregate state */
452 struct ath_arx {
453         struct ath_arx_tid  tid[WME_NUM_TID];
454 };
455
456 void ath_setrxfilter(struct ath_softc *sc);
457 int ath_startrecv(struct ath_softc *sc);
458 enum hal_bool ath_stoprecv(struct ath_softc *sc);
459 void ath_flushrecv(struct ath_softc *sc);
460 u_int32_t ath_calcrxfilter(struct ath_softc *sc);
461 void ath_rx_node_init(struct ath_softc *sc, struct ath_node *an);
462 void ath_rx_node_free(struct ath_softc *sc, struct ath_node *an);
463 void ath_rx_node_cleanup(struct ath_softc *sc, struct ath_node *an);
464 void ath_handle_rx_intr(struct ath_softc *sc);
465 int ath_rx_init(struct ath_softc *sc, int nbufs);
466 void ath_rx_cleanup(struct ath_softc *sc);
467 int ath_rx_tasklet(struct ath_softc *sc, int flush);
468 int ath_rx_input(struct ath_softc *sc,
469                  struct ath_node *node,
470                  int is_ampdu,
471                  struct sk_buff *skb,
472                  struct ath_recv_status *rx_status,
473                  enum ATH_RX_TYPE *status);
474 int ath__rx_indicate(struct ath_softc *sc,
475                     struct sk_buff *skb,
476                     struct ath_recv_status *status,
477                     u_int16_t keyix);
478 int ath_rx_subframe(struct ath_node *an, struct sk_buff *skb,
479                     struct ath_recv_status *status);
480
481 /******/
482 /* TX */
483 /******/
484
485 #define ATH_FRAG_PER_MSDU       1
486 #define ATH_TXBUF               (512/ATH_FRAG_PER_MSDU)
487 /* max number of transmit attempts (tries) */
488 #define ATH_TXMAXTRY            13
489 /* max number of 11n transmit attempts (tries) */
490 #define ATH_11N_TXMAXTRY        10
491 /* max number of tries for management and control frames */
492 #define ATH_MGT_TXMAXTRY        4
493 #define WME_BA_BMP_SIZE         64
494 #define WME_MAX_BA              WME_BA_BMP_SIZE
495 #define ATH_TID_MAX_BUFS        (2 * WME_MAX_BA)
496 #define TID_TO_WME_AC(_tid)                             \
497         ((((_tid) == 0) || ((_tid) == 3)) ? WME_AC_BE : \
498          (((_tid) == 1) || ((_tid) == 2)) ? WME_AC_BK : \
499          (((_tid) == 4) || ((_tid) == 5)) ? WME_AC_VI : \
500          WME_AC_VO)
501
502
503 /* Wireless Multimedia Extension Defines */
504 #define WME_AC_BE               0 /* best effort */
505 #define WME_AC_BK               1 /* background */
506 #define WME_AC_VI               2 /* video */
507 #define WME_AC_VO               3 /* voice */
508 #define WME_NUM_AC              4
509
510 enum ATH_SM_PWRSAV{
511         ATH_SM_ENABLE,
512         ATH_SM_PWRSAV_STATIC,
513         ATH_SM_PWRSAV_DYNAMIC,
514 };
515
516 /*
517  * Data transmit queue state.  One of these exists for each
518  * hardware transmit queue.  Packets sent to us from above
519  * are assigned to queues based on their priority.  Not all
520  * devices support a complete set of hardware transmit queues.
521  * For those devices the array sc_ac2q will map multiple
522  * priorities to fewer hardware queues (typically all to one
523  * hardware queue).
524  */
525 struct ath_txq {
526         u_int                   axq_qnum;       /* hardware q number */
527         u_int32_t               *axq_link;      /* link ptr in last TX desc */
528         struct list_head        axq_q;          /* transmit queue */
529         spinlock_t              axq_lock;       /* lock on q and link */
530         unsigned long           axq_lockflags;  /* intr state when must cli */
531         u_int                   axq_depth;      /* queue depth */
532         u_int8_t                axq_aggr_depth; /* aggregates queued */
533         u_int32_t               axq_totalqueued;/* total ever queued */
534         u_int                   axq_intrcnt;    /* count to determine
535                                                 if descriptor should generate
536                                                 int on this txq. */
537         bool                    stopped;        /* Is mac80211 queue
538                                                 stopped ? */
539         /* State for patching up CTS when bursting */
540         struct  ath_buf         *axq_linkbuf;   /* virtual addr of last buffer*/
541         struct  ath_desc        *axq_lastdsWithCTS;     /* first desc of the
542                                         last descriptor that contains CTS  */
543         struct  ath_desc        *axq_gatingds; /* final desc of the gating desc
544                                 * that determines whether lastdsWithCTS has
545                                 * been DMA'ed or not */
546         struct list_head        axq_acq;
547 };
548
549 /* per TID aggregate tx state for a destination */
550 struct ath_atx_tid {
551         struct list_head        list;   /* round-robin tid entry */
552         struct list_head        buf_q;    /* pending buffers */
553         struct ath_node         *an;        /* parent node structure */
554         struct ath_atx_ac       *ac;        /* parent access category */
555         struct ath_buf          *tx_buf[ATH_TID_MAX_BUFS];/* active tx frames */
556         u_int16_t               seq_start;  /* starting seq of BA window */
557         u_int16_t               seq_next;   /* next seq to be used */
558         u_int16_t               baw_size;   /* BA window size */
559         int                     tidno;      /* TID number */
560         int                     baw_head;   /* first un-acked tx buffer */
561         int                     baw_tail;   /* next unused tx buffer slot */
562         int                     sched;      /* TID is scheduled */
563         int                     paused;     /* TID is paused */
564         int                     cleanup_inprogress; /* aggr of this TID is
565                                                 being teared down */
566         u_int32_t               addba_exchangecomplete:1; /* ADDBA state */
567         int32_t                 addba_exchangeinprogress;
568         int                     addba_exchangeattempts;
569 };
570
571 /* per access-category aggregate tx state for a destination */
572 struct ath_atx_ac {
573         int                     sched;      /* dest-ac is scheduled */
574         int                     qnum; /* H/W queue number associated
575                                         with this AC */
576         struct list_head        list;   /* round-robin txq entry */
577         struct list_head        tid_q;      /* queue of TIDs with buffers */
578 };
579
580 /* per dest tx state */
581 struct ath_atx {
582         struct ath_atx_tid  tid[WME_NUM_TID];
583         struct ath_atx_ac   ac[WME_NUM_AC];
584 };
585
586 /* per-frame tx control block */
587 struct ath_tx_control {
588         struct ath_node *an;    /* destination to sent to */
589         int if_id;              /* only valid for cab traffic */
590         int qnum;               /* h/w queue number */
591         u_int ht:1;             /* if it can be transmitted using HT */
592         u_int ps:1;             /* if one or more stations are in PS mode */
593         u_int use_minrate:1;    /* if this frame should transmitted using
594                                 minimum rate */
595         enum hal_pkt_type atype;        /* Atheros packet type */
596         enum hal_key_type keytype;      /* key type */
597         u_int flags;            /* HAL flags */
598         u_int16_t seqno;        /* sequence number */
599         u_int16_t tidno;        /* tid number */
600         u_int16_t txpower;      /* transmit power */
601         u_int16_t frmlen;       /* frame length */
602         u_int32_t keyix;        /* key index */
603         int min_rate;           /* minimum rate */
604         int mcast_rate;         /* multicast rate */
605         u_int16_t nextfraglen;  /* next fragment length */
606         /* below is set only by ath_dev */
607         struct ath_softc *dev;  /* device handle */
608         dma_addr_t dmacontext;
609 };
610
611 /* per frame tx status block */
612 struct ath_xmit_status {
613         int retries; /* number of retries to successufully
614                         transmit this frame */
615         int flags; /* status of transmit */
616 #define ATH_TX_ERROR        0x01
617 #define ATH_TX_XRETRY       0x02
618 #define ATH_TX_BAR          0x04
619 };
620
621 struct ath_tx_stat {
622         int rssi;               /* RSSI (noise floor ajusted) */
623         int rssictl[ATH_MAX_ANTENNA];   /* RSSI (noise floor ajusted) */
624         int rssiextn[ATH_MAX_ANTENNA];  /* RSSI (noise floor ajusted) */
625         int rateieee;           /* data rate xmitted (IEEE rate code) */
626         int rateKbps;           /* data rate xmitted (Kbps) */
627         int ratecode;           /* phy rate code */
628         int flags;              /* validity flags */
629 /* if any of ctl,extn chain rssis are valid */
630 #define ATH_TX_CHAIN_RSSI_VALID 0x01
631 /* if extn chain rssis are valid */
632 #define ATH_TX_RSSI_EXTN_VALID  0x02
633         u_int32_t airtime;      /* time on air per final tx rate */
634 };
635
636 struct ath_txq *ath_txq_setup(struct ath_softc *sc, int qtype, int subtype);
637 void ath_tx_cleanupq(struct ath_softc *sc, struct ath_txq *txq);
638 int ath_tx_setup(struct ath_softc *sc, int haltype);
639 void ath_draintxq(struct ath_softc *sc, enum hal_bool retry_tx);
640 void ath_tx_draintxq(struct ath_softc *sc,
641         struct ath_txq *txq, enum hal_bool retry_tx);
642 void ath_tx_node_init(struct ath_softc *sc, struct ath_node *an);
643 void ath_tx_node_cleanup(struct ath_softc *sc,
644         struct ath_node *an, bool bh_flag);
645 void ath_tx_node_free(struct ath_softc *sc, struct ath_node *an);
646 void ath_txq_schedule(struct ath_softc *sc, struct ath_txq *txq);
647 int ath_tx_init(struct ath_softc *sc, int nbufs);
648 int ath_tx_cleanup(struct ath_softc *sc);
649 int ath_tx_get_qnum(struct ath_softc *sc, int qtype, int haltype);
650 int ath_txq_update(struct ath_softc *sc, int qnum, struct hal_txq_info *q);
651 int ath_tx_start(struct ath_softc *sc, struct sk_buff *skb);
652 void ath_tx_tasklet(struct ath_softc *sc);
653 u_int32_t ath_txq_depth(struct ath_softc *sc, int qnum);
654 u_int32_t ath_txq_aggr_depth(struct ath_softc *sc, int qnum);
655 void ath_notify_txq_status(struct ath_softc *sc, u_int16_t queue_depth);
656 void ath_tx_complete(struct ath_softc *sc, struct sk_buff *skb,
657                      struct ath_xmit_status *tx_status, struct ath_node *an);
658
659 /**********************/
660 /* Node / Aggregation */
661 /**********************/
662
663 /* indicates the node is clened up */
664 #define ATH_NODE_CLEAN          0x1
665 /* indicates the node is 80211 power save */
666 #define ATH_NODE_PWRSAVE        0x2
667
668 #define ADDBA_TIMEOUT              200 /* 200 milliseconds */
669 #define ADDBA_EXCHANGE_ATTEMPTS    10
670 #define ATH_AGGR_DELIM_SZ          4   /* delimiter size   */
671 #define ATH_AGGR_MINPLEN           256 /* in bytes, minimum packet length */
672 /* number of delimiters for encryption padding */
673 #define ATH_AGGR_ENCRYPTDELIM      10
674 /* minimum h/w qdepth to be sustained to maximize aggregation */
675 #define ATH_AGGR_MIN_QDEPTH        2
676 #define ATH_AMPDU_SUBFRAME_DEFAULT 32
677 #define IEEE80211_SEQ_SEQ_SHIFT    4
678 #define IEEE80211_SEQ_MAX          4096
679 #define IEEE80211_MIN_AMPDU_BUF    0x8
680
681 /* return whether a bit at index _n in bitmap _bm is set
682  * _sz is the size of the bitmap  */
683 #define ATH_BA_ISSET(_bm, _n)  (((_n) < (WME_BA_BMP_SIZE)) &&           \
684                                 ((_bm)[(_n) >> 5] & (1 << ((_n) & 31))))
685
686 /* return block-ack bitmap index given sequence and starting sequence */
687 #define ATH_BA_INDEX(_st, _seq) (((_seq) - (_st)) & (IEEE80211_SEQ_MAX - 1))
688
689 /* returns delimiter padding required given the packet length */
690 #define ATH_AGGR_GET_NDELIM(_len)                                       \
691         (((((_len) + ATH_AGGR_DELIM_SZ) < ATH_AGGR_MINPLEN) ?           \
692           (ATH_AGGR_MINPLEN - (_len) - ATH_AGGR_DELIM_SZ) : 0) >> 2)
693
694 #define BAW_WITHIN(_start, _bawsz, _seqno) \
695         ((((_seqno) - (_start)) & 4095) < (_bawsz))
696
697 #define ATH_DS_BA_SEQ(_ds)               ((_ds)->ds_us.tx.ts_seqnum)
698 #define ATH_DS_BA_BITMAP(_ds)            (&(_ds)->ds_us.tx.ba_low)
699 #define ATH_DS_TX_BA(_ds)                ((_ds)->ds_us.tx.ts_flags & HAL_TX_BA)
700 #define ATH_AN_2_TID(_an, _tidno)        (&(_an)->an_aggr.tx.tid[(_tidno)])
701
702 enum ATH_AGGR_STATUS {
703         ATH_AGGR_DONE,
704         ATH_AGGR_BAW_CLOSED,
705         ATH_AGGR_LIMITED,
706         ATH_AGGR_SHORTPKT,
707         ATH_AGGR_8K_LIMITED,
708 };
709
710 enum ATH_AGGR_CHECK {
711         AGGR_NOT_REQUIRED,
712         AGGR_REQUIRED,
713         AGGR_CLEANUP_PROGRESS,
714         AGGR_EXCHANGE_PROGRESS,
715         AGGR_EXCHANGE_DONE
716 };
717
718 struct aggr_rifs_param {
719         int param_max_frames;
720         int param_max_len;
721         int param_rl;
722         int param_al;
723         struct ath_rc_series *param_rcs;
724 };
725
726 /* Per-node aggregation state */
727 struct ath_node_aggr {
728         struct ath_atx      tx;         /* node transmit state */
729         struct ath_arx      rx;         /* node receive state */
730 };
731
732 /* driver-specific node state */
733 struct ath_node {
734         struct list_head        list;
735         struct ath_softc        *an_sc;                 /* back pointer */
736         atomic_t                an_refcnt;
737         struct ath_chainmask_sel an_chainmask_sel;
738         struct ath_node_aggr    an_aggr; /* A-MPDU aggregation state */
739         u_int8_t                an_smmode; /* SM Power save mode */
740         u_int8_t                an_flags;
741         u8                      an_addr[ETH_ALEN];
742 };
743
744 void ath_tx_resume_tid(struct ath_softc *sc,
745         struct ath_atx_tid *tid);
746 enum ATH_AGGR_CHECK ath_tx_aggr_check(struct ath_softc *sc,
747         struct ath_node *an, u8 tidno);
748 void ath_tx_aggr_teardown(struct ath_softc *sc,
749         struct ath_node *an, u_int8_t tidno);
750 void ath_rx_aggr_teardown(struct ath_softc *sc,
751         struct ath_node *an, u_int8_t tidno);
752 int ath_rx_aggr_start(struct ath_softc *sc,
753                       const u8 *addr,
754                       u16 tid,
755                       u16 *ssn);
756 int ath_rx_aggr_stop(struct ath_softc *sc,
757                      const u8 *addr,
758                      u16 tid);
759 int ath_tx_aggr_start(struct ath_softc *sc,
760                       const u8 *addr,
761                       u16 tid,
762                       u16 *ssn);
763 int ath_tx_aggr_stop(struct ath_softc *sc,
764                      const u8 *addr,
765                      u16 tid);
766 void ath_newassoc(struct ath_softc *sc,
767         struct ath_node *node, int isnew, int isuapsd);
768 struct ath_node *ath_node_attach(struct ath_softc *sc,
769         u_int8_t addr[ETH_ALEN], int if_id);
770 void ath_node_detach(struct ath_softc *sc, struct ath_node *an, bool bh_flag);
771 struct ath_node *ath_node_get(struct ath_softc *sc, u_int8_t addr[ETH_ALEN]);
772 void ath_node_put(struct ath_softc *sc, struct ath_node *an, bool bh_flag);
773 struct ath_node *ath_node_find(struct ath_softc *sc, u_int8_t *addr);
774
775 /*******************/
776 /* Beacon Handling */
777 /*******************/
778
779 /*
780  * Regardless of the number of beacons we stagger, (i.e. regardless of the
781  * number of BSSIDs) if a given beacon does not go out even after waiting this
782  * number of beacon intervals, the game's up.
783  */
784 #define BSTUCK_THRESH                   (9 * ATH_BCBUF)
785 #define ATH_BCBUF                       4   /* number of beacon buffers */
786 #define ATH_DEFAULT_BINTVAL             100 /* default beacon interval in TU */
787 #define ATH_DEFAULT_BMISS_LIMIT         10
788 #define ATH_BEACON_AIFS_DEFAULT         0  /* Default aifs for ap beacon q */
789 #define ATH_BEACON_CWMIN_DEFAULT        0  /* Default cwmin for ap beacon q */
790 #define ATH_BEACON_CWMAX_DEFAULT        0  /* Default cwmax for ap beacon q */
791 #define IEEE80211_MS_TO_TU(x)           (((x) * 1000) / 1024)
792
793 /* beacon configuration */
794 struct ath_beacon_config {
795         u_int16_t beacon_interval;
796         u_int16_t listen_interval;
797         u_int16_t dtim_period;
798         u_int16_t bmiss_timeout;
799         u_int8_t dtim_count;
800         u_int8_t tim_offset;
801         union {
802                 u_int64_t last_tsf;
803                 u_int8_t last_tstamp[8];
804         } u; /* last received beacon/probe response timestamp of this BSS. */
805 };
806
807 /* offsets in a beacon frame for
808  * quick acess of beacon content by low-level driver */
809 struct ath_beacon_offset {
810         u_int8_t *bo_tim;       /* start of atim/dtim */
811 };
812
813 void ath9k_beacon_tasklet(unsigned long data);
814 void ath_beacon_config(struct ath_softc *sc, int if_id);
815 int ath_beaconq_setup(struct ath_hal *ah);
816 int ath_beacon_alloc(struct ath_softc *sc, int if_id);
817 void ath_bstuck_process(struct ath_softc *sc);
818 void ath_beacon_tasklet(struct ath_softc *sc, int *needmark);
819 void ath_beacon_free(struct ath_softc *sc);
820 void ath_beacon_return(struct ath_softc *sc, struct ath_vap *avp);
821 void ath_beacon_sync(struct ath_softc *sc, int if_id);
822 void ath_update_beacon_info(struct ath_softc *sc, int avgbrssi);
823 void ath_get_beaconconfig(struct ath_softc *sc,
824                           int if_id,
825                           struct ath_beacon_config *conf);
826 struct sk_buff *ath_get_beacon(struct ath_softc *sc,
827                                int if_id,
828                                struct ath_beacon_offset *bo,
829                                struct ath_tx_control *txctl);
830 int ath_update_beacon(struct ath_softc *sc,
831                       int if_id,
832                       struct ath_beacon_offset *bo,
833                       struct sk_buff *skb,
834                       int mcast);
835 /********/
836 /* VAPs */
837 /********/
838
839 #define ATH_IF_HW_OFF           0x0001  /* hardware state needs to turn off */
840 #define ATH_IF_HW_ON            0x0002  /* hardware state needs to turn on */
841 /* STA only: the associated AP is HT capable */
842 #define ATH_IF_HT               0x0004
843 /* AP/IBSS only: current BSS has privacy on */
844 #define ATH_IF_PRIVACY          0x0008
845 #define ATH_IF_BEACON_ENABLE    0x0010  /* AP/IBSS only: enable beacon */
846 #define ATH_IF_BEACON_SYNC      0x0020  /* IBSS only: need to sync beacon */
847
848 /*
849  * Define the scheme that we select MAC address for multiple
850  * BSS on the same radio. The very first VAP will just use the MAC
851  * address from the EEPROM. For the next 3 VAPs, we set the
852  * U/L bit (bit 1) in MAC address, and use the next two bits as the
853  * index of the VAP.
854  */
855
856 #define ATH_SET_VAP_BSSID_MASK(bssid_mask) \
857         ((bssid_mask)[0] &= ~(((ATH_BCBUF-1)<<2)|0x02))
858
859 /* VAP configuration (from protocol layer) */
860 struct ath_vap_config {
861         u_int32_t av_fixed_rateset;
862         u_int32_t av_fixed_retryset;
863 };
864
865 /* driver-specific vap state */
866 struct ath_vap {
867         struct ieee80211_vif            *av_if_data; /* interface(vap)
868                                 instance from 802.11 protocal layer */
869         enum hal_opmode                 av_opmode;  /* VAP operational mode */
870         struct ath_buf                  *av_bcbuf;  /* beacon buffer */
871         struct ath_beacon_offset        av_boff;    /* dynamic update state */
872         struct ath_tx_control           av_btxctl;  /* tx control information
873                                                         for beacon */
874         int                             av_bslot;   /* beacon slot index */
875         struct ath_txq                  av_mcastq;  /* multicast
876                                                 transmit queue */
877         struct ath_vap_config           av_config;  /* vap configuration
878                                         parameters from 802.11 protocol layer*/
879 };
880
881 int ath_vap_attach(struct ath_softc *sc,
882                    int if_id,
883                    struct ieee80211_vif *if_data,
884                    enum hal_opmode opmode,
885                    enum hal_opmode iv_opmode,
886                    int nostabeacons);
887 int ath_vap_detach(struct ath_softc *sc, int if_id);
888 int ath_vap_config(struct ath_softc *sc,
889         int if_id, struct ath_vap_config *if_config);
890 int ath_vap_down(struct ath_softc *sc, int if_id, u_int flags);
891 int ath_vap_listen(struct ath_softc *sc, int if_id);
892 int ath_vap_join(struct ath_softc *sc,
893                  int if_id,
894                  const u_int8_t bssid[ETH_ALEN],
895                  u_int flags);
896 int ath_vap_up(struct ath_softc *sc,
897                int if_id,
898                const u_int8_t bssid[ETH_ALEN],
899                u_int8_t aid,
900                u_int flags);
901
902 /*********************/
903 /* Antenna diversity */
904 /*********************/
905
906 #define ATH_ANT_DIV_MAX_CFG      2
907 #define ATH_ANT_DIV_MIN_IDLE_US  1000000  /* us */
908 #define ATH_ANT_DIV_MIN_SCAN_US  50000    /* us */
909
910 enum ATH_ANT_DIV_STATE{
911         ATH_ANT_DIV_IDLE,
912         ATH_ANT_DIV_SCAN,       /* evaluating antenna */
913 };
914
915 struct ath_antdiv {
916         struct ath_softc *antdiv_sc;
917         u_int8_t antdiv_start;
918         enum ATH_ANT_DIV_STATE antdiv_state;
919         u_int8_t antdiv_num_antcfg;
920         u_int8_t antdiv_curcfg;
921         u_int8_t antdiv_bestcfg;
922         int32_t antdivf_rssitrig;
923         int32_t antdiv_lastbrssi[ATH_ANT_DIV_MAX_CFG];
924         u_int64_t antdiv_lastbtsf[ATH_ANT_DIV_MAX_CFG];
925         u_int64_t antdiv_laststatetsf;
926         u_int8_t antdiv_bssid[ETH_ALEN];
927 };
928
929 void ath_slow_ant_div_init(struct ath_antdiv *antdiv,
930         struct ath_softc *sc, int32_t rssitrig);
931 void ath_slow_ant_div_start(struct ath_antdiv *antdiv,
932                             u_int8_t num_antcfg,
933                             const u_int8_t *bssid);
934 void ath_slow_ant_div_stop(struct ath_antdiv *antdiv);
935 void ath_slow_ant_div(struct ath_antdiv *antdiv,
936                       struct ieee80211_hdr *wh,
937                       struct ath_rx_status *rx_stats);
938 void ath_setdefantenna(void *sc, u_int antenna);
939
940 /********************/
941 /* Main driver core */
942 /********************/
943
944 /*
945  * Default cache line size, in bytes.
946  * Used when PCI device not fully initialized by bootrom/BIOS
947 */
948 #define DEFAULT_CACHELINE       32
949 #define ATH_DEFAULT_NOISE_FLOOR -95
950 #define ATH_REGCLASSIDS_MAX     10
951 #define ATH_CABQ_READY_TIME     80  /* % of beacon interval */
952 #define ATH_PREAMBLE_SHORT      (1<<0)
953 #define ATH_PROTECT_ENABLE      (1<<1)
954 #define ATH_MAX_SW_RETRIES      10
955 /* Num farmes difference in tx to flip default recv */
956 #define ATH_ANTENNA_DIFF        2
957 #define ATH_CHAN_MAX            255
958 #define IEEE80211_WEP_NKID      4       /* number of key ids */
959 #define IEEE80211_RATE_VAL      0x7f
960 /*
961  * The key cache is used for h/w cipher state and also for
962  * tracking station state such as the current tx antenna.
963  * We also setup a mapping table between key cache slot indices
964  * and station state to short-circuit node lookups on rx.
965  * Different parts have different size key caches.  We handle
966  * up to ATH_KEYMAX entries (could dynamically allocate state).
967  */
968 #define ATH_KEYMAX              128        /* max key cache size we handle */
969 #define ATH_KEYBYTES            (ATH_KEYMAX/NBBY) /* storage space in bytes */
970
971 #define RESET_RETRY_TXQ         0x00000001
972 #define ATH_IF_ID_ANY           0xff
973
974 #define ATH_TXPOWER_MAX         100     /* .5 dBm units */
975
976 #define ATH_ISR_NOSCHED         0x0000  /* Do not schedule bottom half */
977 /* Schedule the bottom half for execution */
978 #define ATH_ISR_SCHED           0x0001
979 /* This was not my interrupt, for shared IRQ's */
980 #define ATH_ISR_NOTMINE         0x0002
981
982 #define RSSI_LPF_THRESHOLD         -20
983 #define ATH_RSSI_EP_MULTIPLIER     (1<<7)  /* pow2 to optimize out * and / */
984 #define ATH_RATE_DUMMY_MARKER      0
985 #define ATH_RSSI_LPF_LEN           10
986 #define ATH_RSSI_DUMMY_MARKER      0x127
987
988 #define ATH_EP_MUL(x, mul)         ((x) * (mul))
989 #define ATH_EP_RND(x, mul)                                              \
990         ((((x)%(mul)) >= ((mul)/2)) ? ((x) + ((mul) - 1)) / (mul) : (x)/(mul))
991 #define ATH_RSSI_OUT(x)                                                 \
992         (((x) != ATH_RSSI_DUMMY_MARKER) ?                               \
993          (ATH_EP_RND((x), ATH_RSSI_EP_MULTIPLIER)) : ATH_RSSI_DUMMY_MARKER)
994 #define ATH_RSSI_IN(x)                                  \
995         (ATH_EP_MUL((x), ATH_RSSI_EP_MULTIPLIER))
996 #define ATH_LPF_RSSI(x, y, len)                                         \
997         ((x != ATH_RSSI_DUMMY_MARKER) ? \
998                 (((x) * ((len) - 1) + (y)) / (len)) : (y))
999 #define ATH_RSSI_LPF(x, y) do {                                         \
1000                 if ((y) >= RSSI_LPF_THRESHOLD)                          \
1001                         x = ATH_LPF_RSSI((x), \
1002                                 ATH_RSSI_IN((y)), ATH_RSSI_LPF_LEN); \
1003         } while (0)
1004
1005
1006 enum PROT_MODE {
1007         PROT_M_NONE = 0,
1008         PROT_M_RTSCTS,
1009         PROT_M_CTSONLY
1010 };
1011
1012 enum ieee80211_clist_cmd {
1013         CLIST_UPDATE,
1014         CLIST_DFS_UPDATE,
1015         CLIST_NEW_COUNTRY
1016 };
1017
1018 enum RATE_TYPE {
1019         NORMAL_RATE = 0,
1020         HALF_RATE,
1021         QUARTER_RATE
1022 };
1023
1024 struct ath_ht_info {
1025         enum hal_ht_macmode tx_chan_width;
1026         u_int16_t maxampdu;
1027         u_int8_t mpdudensity;
1028         u_int8_t ext_chan_offset;
1029 };
1030
1031 struct ath_softc {
1032         struct ieee80211_hw *hw; /* mac80211 instance */
1033         struct pci_dev          *pdev;      /* Bus handle */
1034         void __iomem            *mem;       /* address of the device */
1035         struct tasklet_struct   intr_tq;    /* General tasklet */
1036         struct tasklet_struct   bcon_tasklet; /* Beacon tasklet */
1037         struct ath_config       sc_config;  /* per-instance load-time
1038                                                 parameters */
1039         int                     sc_debug;   /* Debug masks */
1040         struct ath_hal          *sc_ah;     /* HAL Instance */
1041         struct ath_rate_softc    *sc_rc;     /* tx rate control support */
1042         u_int32_t               sc_intrstatus; /* HAL_STATUS */
1043         enum hal_opmode         sc_opmode;  /* current operating mode */
1044
1045         /* Properties, Config */
1046         unsigned int
1047                 sc_invalid             : 1, /* being detached */
1048                 sc_mrretry             : 1, /* multi-rate retry support */
1049                 sc_needmib             : 1, /* enable MIB stats intr */
1050                 sc_hasdiversity        : 1, /* rx diversity available */
1051                 sc_diversity           : 1, /* enable rx diversity */
1052                 sc_hasveol             : 1, /* tx VEOL support */
1053                 sc_beacons             : 1, /* beacons running */
1054                 sc_hasbmask            : 1, /* bssid mask support */
1055                 sc_hastsfadd           : 1, /* tsf adjust support */
1056                 sc_scanning            : 1, /* scanning active */
1057                 sc_nostabeacons        : 1, /* no beacons for station */
1058                 sc_hasclrkey           : 1, /* CLR key supported */
1059                 sc_stagbeacons         : 1, /* use staggered beacons */
1060                 sc_txaggr              : 1, /* enable 11n tx aggregation */
1061                 sc_rxaggr              : 1, /* enable 11n rx aggregation */
1062                 sc_hasautosleep        : 1, /* automatic sleep after TIM */
1063                 sc_waitbeacon          : 1, /* waiting for first beacon
1064                                                 after waking up */
1065                 sc_no_tx_3_chains      : 1, /* user, hardware, regulatory
1066                                         or country may disallow transmit on
1067                                         three chains. */
1068                 sc_update_chainmask    : 1, /* change chain mask */
1069                 sc_rx_chainmask_detect : 1, /* enable rx chain mask detection */
1070                 sc_rx_chainmask_start  : 1, /* start rx chain mask detection */
1071                 sc_hashtsupport        : 1, /* supports 11n */
1072                 sc_full_reset          : 1, /* force full reset */
1073                 sc_slowAntDiv          : 1; /* enable slow antenna diversity */
1074         enum wireless_mode      sc_curmode;     /* current phy mode */
1075         u_int16_t               sc_curtxpow;    /* current tx power limit */
1076         u_int16_t               sc_curaid;      /* current association id */
1077         u_int8_t                sc_curbssid[ETH_ALEN];
1078         u_int8_t                sc_myaddr[ETH_ALEN];
1079         enum PROT_MODE          sc_protmode;    /* protection mode */
1080         u_int8_t                sc_mcastantenna;/* Multicast antenna number */
1081         u_int8_t                sc_txantenna;   /* data tx antenna
1082                                                 (fixed or auto) */
1083         u_int8_t                sc_nbcnvaps;    /* # of vaps sending beacons */
1084         u_int16_t               sc_nvaps;       /* # of active virtual ap's */
1085         struct ath_vap          *sc_vaps[ATH_BCBUF]; /* interface id
1086                                                 to avp map */
1087         enum hal_int            sc_imask;       /* interrupt mask copy */
1088         u_int8_t                sc_bssidmask[ETH_ALEN];
1089         u_int8_t                sc_defant;      /* current default antenna */
1090         u_int8_t                sc_rxotherant;  /* rx's on non-default antenna*/
1091         u_int16_t               sc_cachelsz;    /* cache line size */
1092         int                     sc_slotupdate;  /* slot to next advance fsm */
1093         int                     sc_slottime;    /* slot time */
1094         u_int8_t                sc_noreset;
1095         int                     sc_bslot[ATH_BCBUF];/* beacon xmit slots */
1096         struct hal_node_stats   sc_halstats;    /* station-mode rssi stats */
1097         struct list_head        node_list;
1098         struct ath_ht_info      sc_ht_info;
1099         int16_t                 sc_noise_floor; /* signal noise floor in dBm */
1100         enum hal_ht_extprotspacing   sc_ht_extprotspacing;
1101         u_int8_t                sc_tx_chainmask;
1102         u_int8_t                sc_rx_chainmask;
1103         u_int8_t                sc_rxchaindetect_ref;
1104         u_int8_t                sc_rxchaindetect_thresh5GHz;
1105         u_int8_t                sc_rxchaindetect_thresh2GHz;
1106         u_int8_t                sc_rxchaindetect_delta5GHz;
1107         u_int8_t                sc_rxchaindetect_delta2GHz;
1108         u_int32_t               sc_rtsaggrlimit; /* Chipset specific
1109                                                 aggr limit */
1110         u32                     sc_flags;
1111 #ifdef CONFIG_SLOW_ANT_DIV
1112         /* Slow antenna diversity */
1113         struct ath_antdiv       sc_antdiv;
1114 #endif
1115         enum {
1116                 OK,                 /* no change needed */
1117                 UPDATE,             /* update pending */
1118                 COMMIT              /* beacon sent, commit change */
1119         } sc_updateslot;            /* slot time update fsm */
1120
1121         /* Crypto */
1122         u_int                   sc_keymax;      /* size of key cache */
1123         u_int8_t                sc_keymap[ATH_KEYBYTES];/* key use bit map */
1124         u_int8_t                sc_splitmic;    /* split TKIP MIC keys */
1125         int                     sc_keytype;     /* type of the key being used */
1126
1127         /* RX */
1128         struct list_head        sc_rxbuf;       /* receive buffer */
1129         struct ath_descdma      sc_rxdma;       /* RX descriptors */
1130         int                     sc_rxbufsize;   /* rx size based on mtu */
1131         u_int32_t               *sc_rxlink;     /* link ptr in last RX desc */
1132         u_int32_t               sc_rxflush;     /* rx flush in progress */
1133         u_int64_t               sc_lastrx;      /* tsf of last rx'd frame */
1134
1135         /* TX */
1136         struct list_head        sc_txbuf;       /* transmit buffer */
1137         struct ath_txq          sc_txq[HAL_NUM_TX_QUEUES];
1138         struct ath_descdma      sc_txdma;       /* TX descriptors */
1139         u_int                   sc_txqsetup;    /* h/w queues setup */
1140         u_int                   sc_txintrperiod;/* tx interrupt batching */
1141         int                     sc_haltype2q[HAL_WME_AC_VO+1]; /* HAL WME
1142                                                         AC -> h/w qnum */
1143         u_int32_t               sc_ant_tx[8];   /* recent tx frames/antenna */
1144
1145         /* Beacon */
1146         struct hal_txq_info     sc_beacon_qi;   /* adhoc only: beacon
1147                                                 queue parameters */
1148         struct ath_descdma      sc_bdma;        /* beacon descriptors */
1149         struct ath_txq          *sc_cabq;       /* tx q for cab frames */
1150         struct list_head        sc_bbuf;        /* beacon buffers */
1151         u_int                   sc_bhalq;       /* HAL q for outgoing beacons */
1152         u_int                   sc_bmisscount;  /* missed beacon transmits */
1153         u_int32_t               ast_be_xmit; /* beacons transmitted */
1154
1155         /* Rate */
1156         struct ieee80211_rate          rates[IEEE80211_NUM_BANDS][ATH_RATE_MAX];
1157         const struct hal_rate_table    *sc_rates[WIRELESS_MODE_MAX];
1158         const struct hal_rate_table    *sc_currates;   /* current rate table */
1159         u_int8_t                       sc_rixmap[256]; /* IEEE to h/w
1160                                                 rate table ix */
1161         u_int8_t                       sc_minrateix;   /* min h/w rate index */
1162         u_int8_t                       sc_protrix; /* protection rate index */
1163         struct {
1164                 u_int32_t rateKbps;      /* transfer rate in kbs */
1165                 u_int8_t ieeerate;       /* IEEE rate */
1166         } sc_hwmap[256];         /* h/w rate ix mappings */
1167
1168         /* Channel, Band */
1169         struct ieee80211_channel channels[IEEE80211_NUM_BANDS][ATH_CHAN_MAX];
1170         struct ieee80211_supported_band sbands[IEEE80211_NUM_BANDS];
1171         struct hal_channel              sc_curchan; /* current h/w channel */
1172
1173         /* Locks */
1174         spinlock_t              sc_rxflushlock; /* lock of RX flush */
1175         spinlock_t              sc_rxbuflock;   /* rxbuf lock */
1176         spinlock_t              sc_txbuflock;   /* txbuf lock */
1177         spinlock_t              sc_resetlock;
1178         spinlock_t              node_lock;
1179 };
1180
1181 int ath_init(u_int16_t devid, struct ath_softc *sc);
1182 void ath_deinit(struct ath_softc *sc);
1183 int ath_open(struct ath_softc *sc, struct hal_channel *initial_chan);
1184 int ath_suspend(struct ath_softc *sc);
1185 int ath_intr(struct ath_softc *sc);
1186 int ath_reset(struct ath_softc *sc);
1187 void ath_scan_start(struct ath_softc *sc);
1188 void ath_scan_end(struct ath_softc *sc);
1189 int ath_set_channel(struct ath_softc *sc, struct hal_channel *hchan);
1190 void ath_setup_channel_list(struct ath_softc *sc,
1191                             enum ieee80211_clist_cmd cmd,
1192                             const struct hal_channel *chans,
1193                             int nchan,
1194                             const u_int8_t *regclassids,
1195                             u_int nregclass,
1196                             int countrycode);
1197 void ath_setup_rate(struct ath_softc *sc,
1198                     enum wireless_mode wMode,
1199                     enum RATE_TYPE type,
1200                     const struct hal_rate_table *rt);
1201
1202 /*********************/
1203 /* Utility Functions */
1204 /*********************/
1205
1206 void ath_set_macmode(struct ath_softc *sc, enum hal_ht_macmode macmode);
1207 void ath_key_reset(struct ath_softc *sc, u_int16_t keyix, int freeslot);
1208 int ath_keyset(struct ath_softc *sc,
1209                u_int16_t keyix,
1210                struct hal_keyval *hk,
1211                const u_int8_t mac[ETH_ALEN]);
1212 int ath_get_hal_qnum(u16 queue, struct ath_softc *sc);
1213 int ath_get_mac80211_qnum(u_int queue, struct ath_softc *sc);
1214 void ath_setslottime(struct ath_softc *sc);
1215 void ath_update_txpow(struct ath_softc *sc, u_int16_t tpcInDb);
1216 int ath_cabq_update(struct ath_softc *);
1217 void ath_get_currentCountry(struct ath_softc *sc,
1218         struct hal_country_entry *ctry);
1219 u_int64_t ath_extend_tsf(struct ath_softc *sc, u_int32_t rstamp);
1220 void ath_internal_reset(struct ath_softc *sc);
1221 u_int32_t ath_chan2flags(struct ieee80211_channel *chan, struct ath_softc *sc);
1222 dma_addr_t ath_skb_map_single(struct ath_softc *sc,
1223                               struct sk_buff *skb,
1224                               int direction,
1225                               dma_addr_t *pa);
1226 void ath_skb_unmap_single(struct ath_softc *sc,
1227                           struct sk_buff *skb,
1228                           int direction,
1229                           dma_addr_t *pa);
1230 void ath_mcast_merge(struct ath_softc *sc, u_int32_t mfilt[2]);
1231 void ath__update_txpow(struct ath_softc *sc,
1232                        u_int16_t txpowlimit,
1233                        u_int16_t txpowlevel);
1234 enum hal_ht_macmode ath_cwm_macmode(struct ath_softc *sc);
1235
1236 #endif /* CORE_H */