1/*
2 * Copyright (c) 2010-2011 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 HTC_H
18#define HTC_H
19
20#include <linux/module.h>
21#include <linux/usb.h>
22#include <linux/firmware.h>
23#include <linux/skbuff.h>
24#include <linux/netdevice.h>
25#include <linux/leds.h>
26#include <linux/slab.h>
27#include <net/mac80211.h>
28
29#include "common.h"
30#include "htc_hst.h"
31#include "hif_usb.h"
32#include "wmi.h"
33
34#define ATH_STA_SHORT_CALINTERVAL 1000    /* 1 second */
35#define ATH_AP_SHORT_CALINTERVAL  100     /* 100 ms */
36#define ATH_ANI_POLLINTERVAL      100     /* 100 ms */
37#define ATH_LONG_CALINTERVAL      30000   /* 30 seconds */
38#define ATH_RESTART_CALINTERVAL   1200000 /* 20 minutes */
39
40#define ATH_DEFAULT_BMISS_LIMIT 10
41#define IEEE80211_MS_TO_TU(x)   (((x) * 1000) / 1024)
42#define TSF_TO_TU(_h, _l) \
43	((((u32)(_h)) << 22) | (((u32)(_l)) >> 10))
44
45extern struct ieee80211_ops ath9k_htc_ops;
46extern int htc_modparam_nohwcrypt;
47
48enum htc_phymode {
49	HTC_MODE_11NA		= 0,
50	HTC_MODE_11NG		= 1
51};
52
53enum htc_opmode {
54	HTC_M_STA	= 1,
55	HTC_M_IBSS	= 0,
56	HTC_M_AHDEMO	= 3,
57	HTC_M_HOSTAP	= 6,
58	HTC_M_MONITOR	= 8,
59	HTC_M_WDS	= 2
60};
61
62#define ATH9K_HTC_AMPDU  1
63#define ATH9K_HTC_NORMAL 2
64#define ATH9K_HTC_BEACON 3
65#define ATH9K_HTC_MGMT   4
66
67#define ATH9K_HTC_TX_CTSONLY      0x1
68#define ATH9K_HTC_TX_RTSCTS       0x2
69
70struct tx_frame_hdr {
71	u8 data_type;
72	u8 node_idx;
73	u8 vif_idx;
74	u8 tidno;
75	__be32 flags; /* ATH9K_HTC_TX_* */
76	u8 key_type;
77	u8 keyix;
78	u8 cookie;
79	u8 pad;
80} __packed;
81
82struct tx_mgmt_hdr {
83	u8 node_idx;
84	u8 vif_idx;
85	u8 tidno;
86	u8 flags;
87	u8 key_type;
88	u8 keyix;
89	u8 cookie;
90	u8 pad;
91} __packed;
92
93struct tx_beacon_header {
94	u8 vif_index;
95	u8 len_changed;
96	u16 rev;
97} __packed;
98
99#define MAX_TX_AMPDU_SUBFRAMES_9271 17
100#define MAX_TX_AMPDU_SUBFRAMES_7010 22
101
102struct ath9k_htc_cap_target {
103	__be32 ampdu_limit;
104	u8 ampdu_subframes;
105	u8 enable_coex;
106	u8 tx_chainmask;
107	u8 pad;
108} __packed;
109
110struct ath9k_htc_target_vif {
111	u8 index;
112	u8 opmode;
113	u8 myaddr[ETH_ALEN];
114	u8 ath_cap;
115	__be16 rtsthreshold;
116	u8 pad;
117} __packed;
118
119struct ath9k_htc_target_sta {
120	u8 macaddr[ETH_ALEN];
121	u8 bssid[ETH_ALEN];
122	u8 sta_index;
123	u8 vif_index;
124	u8 is_vif_sta;
125	__be16 flags;
126	__be16 htcap;
127	__be16 maxampdu;
128	u8 pad;
129} __packed;
130
131struct ath9k_htc_target_aggr {
132	u8 sta_index;
133	u8 tidno;
134	u8 aggr_enable;
135	u8 padding;
136} __packed;
137
138#define ATH_HTC_RATE_MAX 30
139
140#define WLAN_RC_DS_FLAG  0x01
141#define WLAN_RC_40_FLAG  0x02
142#define WLAN_RC_SGI_FLAG 0x04
143#define WLAN_RC_HT_FLAG  0x08
144
145struct ath9k_htc_rateset {
146	u8 rs_nrates;
147	u8 rs_rates[ATH_HTC_RATE_MAX];
148};
149
150struct ath9k_htc_rate {
151	struct ath9k_htc_rateset legacy_rates;
152	struct ath9k_htc_rateset ht_rates;
153} __packed;
154
155struct ath9k_htc_target_rate {
156	u8 sta_index;
157	u8 isnew;
158	__be32 capflags;
159	struct ath9k_htc_rate rates;
160};
161
162struct ath9k_htc_target_rate_mask {
163	u8 vif_index;
164	u8 band;
165	__be32 mask;
166	u16 pad;
167} __packed;
168
169struct ath9k_htc_target_int_stats {
170	__be32 rx;
171	__be32 rxorn;
172	__be32 rxeol;
173	__be32 txurn;
174	__be32 txto;
175	__be32 cst;
176} __packed;
177
178struct ath9k_htc_target_tx_stats {
179	__be32 xretries;
180	__be32 fifoerr;
181	__be32 filtered;
182	__be32 timer_exp;
183	__be32 shortretries;
184	__be32 longretries;
185	__be32 qnull;
186	__be32 encap_fail;
187	__be32 nobuf;
188} __packed;
189
190struct ath9k_htc_target_rx_stats {
191	__be32 nobuf;
192	__be32 host_send;
193	__be32 host_done;
194} __packed;
195
196#define ATH9K_HTC_MAX_VIF 2
197#define ATH9K_HTC_MAX_BCN_VIF 2
198
199#define INC_VIF(_priv, _type) do {		\
200		switch (_type) {		\
201		case NL80211_IFTYPE_STATION:	\
202			_priv->num_sta_vif++;	\
203			break;			\
204		case NL80211_IFTYPE_ADHOC:	\
205			_priv->num_ibss_vif++;	\
206			break;			\
207		case NL80211_IFTYPE_AP:		\
208			_priv->num_ap_vif++;	\
209			break;			\
210		default:			\
211			break;			\
212		}				\
213	} while (0)
214
215#define DEC_VIF(_priv, _type) do {		\
216		switch (_type) {		\
217		case NL80211_IFTYPE_STATION:	\
218			_priv->num_sta_vif--;	\
219			break;			\
220		case NL80211_IFTYPE_ADHOC:	\
221			_priv->num_ibss_vif--;	\
222			break;			\
223		case NL80211_IFTYPE_AP:		\
224			_priv->num_ap_vif--;	\
225			break;			\
226		default:			\
227			break;			\
228		}				\
229	} while (0)
230
231struct ath9k_htc_vif {
232	u8 index;
233	u16 seq_no;
234	bool beacon_configured;
235	int bslot;
236	__le64 tsfadjust;
237};
238
239struct ath9k_vif_iter_data {
240	const u8 *hw_macaddr;
241	u8 mask[ETH_ALEN];
242};
243
244#define ATH9K_HTC_MAX_STA 8
245#define ATH9K_HTC_MAX_TID 8
246
247enum tid_aggr_state {
248	AGGR_STOP = 0,
249	AGGR_PROGRESS,
250	AGGR_START,
251	AGGR_OPERATIONAL
252};
253
254struct ath9k_htc_sta {
255	u8 index;
256	enum tid_aggr_state tid_state[ATH9K_HTC_MAX_TID];
257};
258
259#define ATH9K_HTC_RXBUF 256
260#define HTC_RX_FRAME_HEADER_SIZE 40
261
262struct ath9k_htc_rxbuf {
263	bool in_process;
264	struct sk_buff *skb;
265	struct ath_htc_rx_status rxstatus;
266	struct list_head list;
267};
268
269struct ath9k_htc_rx {
270	int last_rssi; /* FIXME: per-STA */
271	struct list_head rxbuf;
272	spinlock_t rxbuflock;
273};
274
275#define ATH9K_HTC_TX_CLEANUP_INTERVAL 50 /* ms */
276#define ATH9K_HTC_TX_TIMEOUT_INTERVAL 3000 /* ms */
277#define ATH9K_HTC_TX_RESERVE 10
278#define ATH9K_HTC_TX_TIMEOUT_COUNT 40
279#define ATH9K_HTC_TX_THRESHOLD (MAX_TX_BUF_NUM - ATH9K_HTC_TX_RESERVE)
280
281#define ATH9K_HTC_OP_TX_QUEUES_STOP BIT(0)
282#define ATH9K_HTC_OP_TX_DRAIN       BIT(1)
283
284struct ath9k_htc_tx {
285	u8 flags;
286	int queued_cnt;
287	struct sk_buff_head mgmt_ep_queue;
288	struct sk_buff_head cab_ep_queue;
289	struct sk_buff_head data_be_queue;
290	struct sk_buff_head data_bk_queue;
291	struct sk_buff_head data_vi_queue;
292	struct sk_buff_head data_vo_queue;
293	struct sk_buff_head tx_failed;
294	DECLARE_BITMAP(tx_slot, MAX_TX_BUF_NUM);
295	struct timer_list cleanup_timer;
296	spinlock_t tx_lock;
297};
298
299struct ath9k_htc_tx_ctl {
300	u8 type; /* ATH9K_HTC_* */
301	u8 epid;
302	u8 txok;
303	u8 sta_idx;
304	unsigned long timestamp;
305};
306
307static inline struct ath9k_htc_tx_ctl *HTC_SKB_CB(struct sk_buff *skb)
308{
309	struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
310
311	BUILD_BUG_ON(sizeof(struct ath9k_htc_tx_ctl) >
312		     IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
313	return (struct ath9k_htc_tx_ctl *) &tx_info->driver_data;
314}
315
316#ifdef CONFIG_ATH9K_HTC_DEBUGFS
317
318#define TX_STAT_INC(c) (hif_dev->htc_handle->drv_priv->debug.tx_stats.c++)
319#define RX_STAT_INC(c) (hif_dev->htc_handle->drv_priv->debug.rx_stats.c++)
320#define CAB_STAT_INC   priv->debug.tx_stats.cab_queued++
321
322#define TX_QSTAT_INC(q) (priv->debug.tx_stats.queue_stats[q]++)
323
324void ath9k_htc_err_stat_rx(struct ath9k_htc_priv *priv,
325			   struct ath_htc_rx_status *rxs);
326
327struct ath_tx_stats {
328	u32 buf_queued;
329	u32 buf_completed;
330	u32 skb_queued;
331	u32 skb_success;
332	u32 skb_failed;
333	u32 cab_queued;
334	u32 queue_stats[WME_NUM_AC];
335};
336
337struct ath_rx_stats {
338	u32 skb_allocated;
339	u32 skb_completed;
340	u32 skb_dropped;
341	u32 err_crc;
342	u32 err_decrypt_crc;
343	u32 err_mic;
344	u32 err_pre_delim;
345	u32 err_post_delim;
346	u32 err_decrypt_busy;
347	u32 err_phy;
348	u32 err_phy_stats[ATH9K_PHYERR_MAX];
349};
350
351struct ath9k_debug {
352	struct dentry *debugfs_phy;
353	struct ath_tx_stats tx_stats;
354	struct ath_rx_stats rx_stats;
355};
356
357#else
358
359#define TX_STAT_INC(c) do { } while (0)
360#define RX_STAT_INC(c) do { } while (0)
361#define CAB_STAT_INC   do { } while (0)
362
363#define TX_QSTAT_INC(c) do { } while (0)
364
365static inline void ath9k_htc_err_stat_rx(struct ath9k_htc_priv *priv,
366					 struct ath_htc_rx_status *rxs)
367{
368}
369
370#endif /* CONFIG_ATH9K_HTC_DEBUGFS */
371
372#define ATH_LED_PIN_DEF             1
373#define ATH_LED_PIN_9287            10
374#define ATH_LED_PIN_9271            15
375#define ATH_LED_PIN_7010            12
376
377#define BSTUCK_THRESHOLD 10
378
379/*
380 * Adjust these when the max. no of beaconing interfaces is
381 * increased.
382 */
383#define DEFAULT_SWBA_RESPONSE 40 /* in TUs */
384#define MIN_SWBA_RESPONSE     10 /* in TUs */
385
386struct htc_beacon_config {
387	struct ieee80211_vif *bslot[ATH9K_HTC_MAX_BCN_VIF];
388	u16 beacon_interval;
389	u16 dtim_period;
390	u16 bmiss_timeout;
391	u32 bmiss_cnt;
392};
393
394struct ath_btcoex {
395	u32 bt_priority_cnt;
396	unsigned long bt_priority_time;
397	int bt_stomp_type; /* Types of BT stomping */
398	u32 btcoex_no_stomp;
399	u32 btcoex_period;
400	u32 btscan_no_stomp;
401};
402
403void ath_htc_init_btcoex_work(struct ath9k_htc_priv *priv);
404void ath_htc_resume_btcoex_work(struct ath9k_htc_priv *priv);
405void ath_htc_cancel_btcoex_work(struct ath9k_htc_priv *priv);
406
407#define OP_INVALID		   BIT(0)
408#define OP_SCANNING		   BIT(1)
409#define OP_ENABLE_BEACON           BIT(2)
410#define OP_BT_PRIORITY_DETECTED    BIT(3)
411#define OP_BT_SCAN                 BIT(4)
412#define OP_ANI_RUNNING             BIT(5)
413#define OP_TSF_RESET               BIT(6)
414
415struct ath9k_htc_priv {
416	struct device *dev;
417	struct ieee80211_hw *hw;
418	struct ath_hw *ah;
419	struct htc_target *htc;
420	struct wmi *wmi;
421
422	u16 fw_version_major;
423	u16 fw_version_minor;
424
425	enum htc_endpoint_id wmi_cmd_ep;
426	enum htc_endpoint_id beacon_ep;
427	enum htc_endpoint_id cab_ep;
428	enum htc_endpoint_id uapsd_ep;
429	enum htc_endpoint_id mgmt_ep;
430	enum htc_endpoint_id data_be_ep;
431	enum htc_endpoint_id data_bk_ep;
432	enum htc_endpoint_id data_vi_ep;
433	enum htc_endpoint_id data_vo_ep;
434
435	u8 vif_slot;
436	u8 mon_vif_idx;
437	u8 sta_slot;
438	u8 vif_sta_pos[ATH9K_HTC_MAX_VIF];
439	u8 num_ibss_vif;
440	u8 num_sta_vif;
441	u8 num_sta_assoc_vif;
442	u8 num_ap_vif;
443
444	u16 op_flags;
445	u16 curtxpow;
446	u16 txpowlimit;
447	u16 nvifs;
448	u16 nstations;
449	bool rearm_ani;
450	bool reconfig_beacon;
451	unsigned int rxfilter;
452
453	struct ath9k_hw_cal_data caldata;
454	struct ieee80211_supported_band sbands[IEEE80211_NUM_BANDS];
455
456	spinlock_t beacon_lock;
457	struct htc_beacon_config cur_beacon_conf;
458
459	struct ath9k_htc_rx rx;
460	struct ath9k_htc_tx tx;
461
462	struct tasklet_struct swba_tasklet;
463	struct tasklet_struct rx_tasklet;
464	struct delayed_work ani_work;
465	struct tasklet_struct tx_failed_tasklet;
466	struct work_struct ps_work;
467	struct work_struct fatal_work;
468
469	struct mutex htc_pm_lock;
470	unsigned long ps_usecount;
471	bool ps_enabled;
472	bool ps_idle;
473
474#ifdef CONFIG_MAC80211_LEDS
475	enum led_brightness brightness;
476	bool led_registered;
477	char led_name[32];
478	struct led_classdev led_cdev;
479	struct work_struct led_work;
480#endif
481
482	int beaconq;
483	int cabq;
484	int hwq_map[WME_NUM_AC];
485
486	struct ath_btcoex btcoex;
487	struct delayed_work coex_period_work;
488	struct delayed_work duty_cycle_work;
489#ifdef CONFIG_ATH9K_HTC_DEBUGFS
490	struct ath9k_debug debug;
491#endif
492	struct mutex mutex;
493};
494
495static inline void ath_read_cachesize(struct ath_common *common, int *csz)
496{
497	common->bus_ops->read_cachesize(common, csz);
498}
499
500void ath9k_htc_reset(struct ath9k_htc_priv *priv);
501
502void ath9k_htc_assign_bslot(struct ath9k_htc_priv *priv,
503			    struct ieee80211_vif *vif);
504void ath9k_htc_remove_bslot(struct ath9k_htc_priv *priv,
505			    struct ieee80211_vif *vif);
506void ath9k_htc_set_tsfadjust(struct ath9k_htc_priv *priv,
507			     struct ieee80211_vif *vif);
508void ath9k_htc_beaconq_config(struct ath9k_htc_priv *priv);
509void ath9k_htc_beacon_config(struct ath9k_htc_priv *priv,
510			     struct ieee80211_vif *vif);
511void ath9k_htc_beacon_reconfig(struct ath9k_htc_priv *priv);
512void ath9k_htc_swba(struct ath9k_htc_priv *priv,
513		    struct wmi_event_swba *swba);
514
515void ath9k_htc_rxep(void *priv, struct sk_buff *skb,
516		    enum htc_endpoint_id ep_id);
517void ath9k_htc_txep(void *priv, struct sk_buff *skb, enum htc_endpoint_id ep_id,
518		    bool txok);
519void ath9k_htc_beaconep(void *drv_priv, struct sk_buff *skb,
520			enum htc_endpoint_id ep_id, bool txok);
521
522int ath9k_htc_update_cap_target(struct ath9k_htc_priv *priv,
523				u8 enable_coex);
524void ath9k_htc_ani_work(struct work_struct *work);
525void ath9k_htc_start_ani(struct ath9k_htc_priv *priv);
526void ath9k_htc_stop_ani(struct ath9k_htc_priv *priv);
527
528int ath9k_tx_init(struct ath9k_htc_priv *priv);
529int ath9k_htc_tx_start(struct ath9k_htc_priv *priv,
530		       struct sk_buff *skb, u8 slot, bool is_cab);
531void ath9k_tx_cleanup(struct ath9k_htc_priv *priv);
532bool ath9k_htc_txq_setup(struct ath9k_htc_priv *priv, int subtype);
533int ath9k_htc_cabq_setup(struct ath9k_htc_priv *priv);
534int get_hw_qnum(u16 queue, int *hwq_map);
535int ath_htc_txq_update(struct ath9k_htc_priv *priv, int qnum,
536		       struct ath9k_tx_queue_info *qinfo);
537void ath9k_htc_check_stop_queues(struct ath9k_htc_priv *priv);
538void ath9k_htc_check_wake_queues(struct ath9k_htc_priv *priv);
539int ath9k_htc_tx_get_slot(struct ath9k_htc_priv *priv);
540void ath9k_htc_tx_clear_slot(struct ath9k_htc_priv *priv, int slot);
541void ath9k_htc_tx_drain(struct ath9k_htc_priv *priv);
542void ath9k_htc_txstatus(struct ath9k_htc_priv *priv, void *wmi_event);
543void ath9k_tx_failed_tasklet(unsigned long data);
544void ath9k_htc_tx_cleanup_timer(unsigned long data);
545
546int ath9k_rx_init(struct ath9k_htc_priv *priv);
547void ath9k_rx_cleanup(struct ath9k_htc_priv *priv);
548void ath9k_host_rx_init(struct ath9k_htc_priv *priv);
549void ath9k_rx_tasklet(unsigned long data);
550u32 ath9k_htc_calcrxfilter(struct ath9k_htc_priv *priv);
551
552void ath9k_htc_ps_wakeup(struct ath9k_htc_priv *priv);
553void ath9k_htc_ps_restore(struct ath9k_htc_priv *priv);
554void ath9k_ps_work(struct work_struct *work);
555bool ath9k_htc_setpower(struct ath9k_htc_priv *priv,
556			enum ath9k_power_mode mode);
557
558void ath9k_start_rfkill_poll(struct ath9k_htc_priv *priv);
559void ath9k_htc_rfkill_poll_state(struct ieee80211_hw *hw);
560void ath9k_htc_radio_enable(struct ieee80211_hw *hw);
561void ath9k_htc_radio_disable(struct ieee80211_hw *hw);
562
563#ifdef CONFIG_MAC80211_LEDS
564void ath9k_init_leds(struct ath9k_htc_priv *priv);
565void ath9k_deinit_leds(struct ath9k_htc_priv *priv);
566void ath9k_led_work(struct work_struct *work);
567#else
568static inline void ath9k_init_leds(struct ath9k_htc_priv *priv)
569{
570}
571
572static inline void ath9k_deinit_leds(struct ath9k_htc_priv *priv)
573{
574}
575
576static inline void ath9k_led_work(struct work_struct *work)
577{
578}
579#endif
580
581int ath9k_htc_probe_device(struct htc_target *htc_handle, struct device *dev,
582			   u16 devid, char *product, u32 drv_info);
583void ath9k_htc_disconnect_device(struct htc_target *htc_handle, bool hotunplug);
584#ifdef CONFIG_PM
585void ath9k_htc_suspend(struct htc_target *htc_handle);
586int ath9k_htc_resume(struct htc_target *htc_handle);
587#endif
588#ifdef CONFIG_ATH9K_HTC_DEBUGFS
589int ath9k_htc_init_debug(struct ath_hw *ah);
590#else
591static inline int ath9k_htc_init_debug(struct ath_hw *ah) { return 0; };
592#endif /* CONFIG_ATH9K_HTC_DEBUGFS */
593
594#endif /* HTC_H */
595