90775a5358b81c84345a4e8a311749900a1eabbe
[cascardo/linux.git] / drivers / net / wireless / ath / ath10k / core.h
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, 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
18 #ifndef _CORE_H_
19 #define _CORE_H_
20
21 #include <linux/completion.h>
22 #include <linux/if_ether.h>
23 #include <linux/types.h>
24 #include <linux/pci.h>
25 #include <linux/uuid.h>
26 #include <linux/time.h>
27
28 #include "htt.h"
29 #include "htc.h"
30 #include "hw.h"
31 #include "targaddrs.h"
32 #include "wmi.h"
33 #include "../ath.h"
34 #include "../regd.h"
35 #include "../dfs_pattern_detector.h"
36 #include "spectral.h"
37
38 #define MS(_v, _f) (((_v) & _f##_MASK) >> _f##_LSB)
39 #define SM(_v, _f) (((_v) << _f##_LSB) & _f##_MASK)
40 #define WO(_f)      ((_f##_OFFSET) >> 2)
41
42 #define ATH10K_SCAN_ID 0
43 #define WMI_READY_TIMEOUT (5 * HZ)
44 #define ATH10K_FLUSH_TIMEOUT_HZ (5*HZ)
45 #define ATH10K_NUM_CHANS 38
46
47 /* Antenna noise floor */
48 #define ATH10K_DEFAULT_NOISE_FLOOR -95
49
50 #define ATH10K_MAX_NUM_MGMT_PENDING 128
51
52 /* number of failed packets */
53 #define ATH10K_KICKOUT_THRESHOLD 50
54
55 /*
56  * Use insanely high numbers to make sure that the firmware implementation
57  * won't start, we have the same functionality already in hostapd. Unit
58  * is seconds.
59  */
60 #define ATH10K_KEEPALIVE_MIN_IDLE 3747
61 #define ATH10K_KEEPALIVE_MAX_IDLE 3895
62 #define ATH10K_KEEPALIVE_MAX_UNRESPONSIVE 3900
63
64 struct ath10k;
65
66 enum ath10k_bus {
67         ATH10K_BUS_PCI,
68 };
69
70 static inline const char *ath10k_bus_str(enum ath10k_bus bus)
71 {
72         switch (bus) {
73         case ATH10K_BUS_PCI:
74                 return "pci";
75         }
76
77         return "unknown";
78 }
79
80 struct ath10k_skb_cb {
81         dma_addr_t paddr;
82         u8 vdev_id;
83
84         struct {
85                 u8 tid;
86                 u16 freq;
87                 bool is_offchan;
88                 struct ath10k_htt_txbuf *txbuf;
89                 u32 txbuf_paddr;
90         } __packed htt;
91
92         struct {
93                 bool dtim_zero;
94                 bool deliver_cab;
95         } bcn;
96 } __packed;
97
98 static inline struct ath10k_skb_cb *ATH10K_SKB_CB(struct sk_buff *skb)
99 {
100         BUILD_BUG_ON(sizeof(struct ath10k_skb_cb) >
101                      IEEE80211_TX_INFO_DRIVER_DATA_SIZE);
102         return (struct ath10k_skb_cb *)&IEEE80211_SKB_CB(skb)->driver_data;
103 }
104
105 static inline u32 host_interest_item_address(u32 item_offset)
106 {
107         return QCA988X_HOST_INTEREST_ADDRESS + item_offset;
108 }
109
110 struct ath10k_bmi {
111         bool done_sent;
112 };
113
114 struct ath10k_mem_chunk {
115         void *vaddr;
116         dma_addr_t paddr;
117         u32 len;
118         u32 req_id;
119 };
120
121 struct ath10k_wmi {
122         enum ath10k_htc_ep_id eid;
123         struct completion service_ready;
124         struct completion unified_ready;
125         wait_queue_head_t tx_credits_wq;
126         struct wmi_cmd_map *cmd;
127         struct wmi_vdev_param_map *vdev_param;
128         struct wmi_pdev_param_map *pdev_param;
129
130         u32 num_mem_chunks;
131         struct ath10k_mem_chunk mem_chunks[WMI_MAX_MEM_REQS];
132 };
133
134 struct ath10k_fw_stats_peer {
135         struct list_head list;
136
137         u8 peer_macaddr[ETH_ALEN];
138         u32 peer_rssi;
139         u32 peer_tx_rate;
140         u32 peer_rx_rate; /* 10x only */
141 };
142
143 struct ath10k_fw_stats_pdev {
144         struct list_head list;
145
146         /* PDEV stats */
147         s32 ch_noise_floor;
148         u32 tx_frame_count;
149         u32 rx_frame_count;
150         u32 rx_clear_count;
151         u32 cycle_count;
152         u32 phy_err_count;
153         u32 chan_tx_power;
154         u32 ack_rx_bad;
155         u32 rts_bad;
156         u32 rts_good;
157         u32 fcs_bad;
158         u32 no_beacons;
159         u32 mib_int_count;
160
161         /* PDEV TX stats */
162         s32 comp_queued;
163         s32 comp_delivered;
164         s32 msdu_enqued;
165         s32 mpdu_enqued;
166         s32 wmm_drop;
167         s32 local_enqued;
168         s32 local_freed;
169         s32 hw_queued;
170         s32 hw_reaped;
171         s32 underrun;
172         s32 tx_abort;
173         s32 mpdus_requed;
174         u32 tx_ko;
175         u32 data_rc;
176         u32 self_triggers;
177         u32 sw_retry_failure;
178         u32 illgl_rate_phy_err;
179         u32 pdev_cont_xretry;
180         u32 pdev_tx_timeout;
181         u32 pdev_resets;
182         u32 phy_underrun;
183         u32 txop_ovf;
184
185         /* PDEV RX stats */
186         s32 mid_ppdu_route_change;
187         s32 status_rcvd;
188         s32 r0_frags;
189         s32 r1_frags;
190         s32 r2_frags;
191         s32 r3_frags;
192         s32 htt_msdus;
193         s32 htt_mpdus;
194         s32 loc_msdus;
195         s32 loc_mpdus;
196         s32 oversize_amsdu;
197         s32 phy_errs;
198         s32 phy_err_drop;
199         s32 mpdu_errs;
200 };
201
202 struct ath10k_fw_stats {
203         struct list_head pdevs;
204         struct list_head peers;
205 };
206
207 struct ath10k_dfs_stats {
208         u32 phy_errors;
209         u32 pulses_total;
210         u32 pulses_detected;
211         u32 pulses_discarded;
212         u32 radar_detected;
213 };
214
215 #define ATH10K_MAX_NUM_PEER_IDS (1 << 11) /* htt rx_desc limit */
216
217 struct ath10k_peer {
218         struct list_head list;
219         int vdev_id;
220         u8 addr[ETH_ALEN];
221         DECLARE_BITMAP(peer_ids, ATH10K_MAX_NUM_PEER_IDS);
222
223         /* protected by ar->data_lock */
224         struct ieee80211_key_conf *keys[WMI_MAX_KEY_INDEX + 1];
225 };
226
227 struct ath10k_sta {
228         struct ath10k_vif *arvif;
229
230         /* the following are protected by ar->data_lock */
231         u32 changed; /* IEEE80211_RC_* */
232         u32 bw;
233         u32 nss;
234         u32 smps;
235
236         struct work_struct update_wk;
237 };
238
239 #define ATH10K_VDEV_SETUP_TIMEOUT_HZ (5*HZ)
240
241 struct ath10k_vif {
242         struct list_head list;
243
244         u32 vdev_id;
245         enum wmi_vdev_type vdev_type;
246         enum wmi_vdev_subtype vdev_subtype;
247         u32 beacon_interval;
248         u32 dtim_period;
249         struct sk_buff *beacon;
250         /* protected by data_lock */
251         bool beacon_sent;
252         void *beacon_buf;
253         dma_addr_t beacon_paddr;
254
255         struct ath10k *ar;
256         struct ieee80211_vif *vif;
257
258         bool is_started;
259         bool is_up;
260         bool spectral_enabled;
261         u32 aid;
262         u8 bssid[ETH_ALEN];
263
264         struct work_struct wep_key_work;
265         struct ieee80211_key_conf *wep_keys[WMI_MAX_KEY_INDEX + 1];
266         u8 def_wep_key_idx;
267         u8 def_wep_key_newidx;
268
269         u16 tx_seq_no;
270
271         union {
272                 struct {
273                         u32 uapsd;
274                 } sta;
275                 struct {
276                         /* 127 stations; wmi limit */
277                         u8 tim_bitmap[16];
278                         u8 tim_len;
279                         u32 ssid_len;
280                         u8 ssid[IEEE80211_MAX_SSID_LEN];
281                         bool hidden_ssid;
282                         /* P2P_IE with NoA attribute for P2P_GO case */
283                         u32 noa_len;
284                         u8 *noa_data;
285                 } ap;
286         } u;
287
288         u8 fixed_rate;
289         u8 fixed_nss;
290         u8 force_sgi;
291         bool use_cts_prot;
292         int num_legacy_stations;
293         int txpower;
294 };
295
296 struct ath10k_vif_iter {
297         u32 vdev_id;
298         struct ath10k_vif *arvif;
299 };
300
301 /* used for crash-dump storage, protected by data-lock */
302 struct ath10k_fw_crash_data {
303         bool crashed_since_read;
304
305         uuid_le uuid;
306         struct timespec timestamp;
307         __le32 registers[REG_DUMP_COUNT_QCA988X];
308 };
309
310 struct ath10k_debug {
311         struct dentry *debugfs_phy;
312
313         struct ath10k_fw_stats fw_stats;
314         struct completion fw_stats_complete;
315         bool fw_stats_done;
316         DECLARE_BITMAP(wmi_service_bitmap, WMI_SERVICE_MAX);
317
318         unsigned long htt_stats_mask;
319         struct delayed_work htt_stats_dwork;
320         struct ath10k_dfs_stats dfs_stats;
321         struct ath_dfs_pool_stats dfs_pool_stats;
322
323         /* protected by conf_mutex */
324         u32 fw_dbglog_mask;
325         u32 pktlog_filter;
326
327         u8 htt_max_amsdu;
328         u8 htt_max_ampdu;
329
330         struct ath10k_fw_crash_data *fw_crash_data;
331 };
332
333 enum ath10k_state {
334         ATH10K_STATE_OFF = 0,
335         ATH10K_STATE_ON,
336
337         /* When doing firmware recovery the device is first powered down.
338          * mac80211 is supposed to call in to start() hook later on. It is
339          * however possible that driver unloading and firmware crash overlap.
340          * mac80211 can wait on conf_mutex in stop() while the device is
341          * stopped in ath10k_core_restart() work holding conf_mutex. The state
342          * RESTARTED means that the device is up and mac80211 has started hw
343          * reconfiguration. Once mac80211 is done with the reconfiguration we
344          * set the state to STATE_ON in reconfig_complete(). */
345         ATH10K_STATE_RESTARTING,
346         ATH10K_STATE_RESTARTED,
347
348         /* The device has crashed while restarting hw. This state is like ON
349          * but commands are blocked in HTC and -ECOMM response is given. This
350          * prevents completion timeouts and makes the driver more responsive to
351          * userspace commands. This is also prevents recursive recovery. */
352         ATH10K_STATE_WEDGED,
353
354         /* factory tests */
355         ATH10K_STATE_UTF,
356 };
357
358 enum ath10k_firmware_mode {
359         /* the default mode, standard 802.11 functionality */
360         ATH10K_FIRMWARE_MODE_NORMAL,
361
362         /* factory tests etc */
363         ATH10K_FIRMWARE_MODE_UTF,
364 };
365
366 enum ath10k_fw_features {
367         /* wmi_mgmt_rx_hdr contains extra RSSI information */
368         ATH10K_FW_FEATURE_EXT_WMI_MGMT_RX = 0,
369
370         /* firmware from 10X branch */
371         ATH10K_FW_FEATURE_WMI_10X = 1,
372
373         /* firmware support tx frame management over WMI, otherwise it's HTT */
374         ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX = 2,
375
376         /* Firmware does not support P2P */
377         ATH10K_FW_FEATURE_NO_P2P = 3,
378
379         /* Firmware 10.2 feature bit. The ATH10K_FW_FEATURE_WMI_10X feature bit
380          * is required to be set as well.
381          */
382         ATH10K_FW_FEATURE_WMI_10_2 = 4,
383
384         /* keep last */
385         ATH10K_FW_FEATURE_COUNT,
386 };
387
388 enum ath10k_dev_flags {
389         /* Indicates that ath10k device is during CAC phase of DFS */
390         ATH10K_CAC_RUNNING,
391         ATH10K_FLAG_CORE_REGISTERED,
392
393         /* Device has crashed and needs to restart. This indicates any pending
394          * waiters should immediately cancel instead of waiting for a time out.
395          */
396         ATH10K_FLAG_CRASH_FLUSH,
397 };
398
399 enum ath10k_cal_mode {
400         ATH10K_CAL_MODE_FILE,
401         ATH10K_CAL_MODE_OTP,
402 };
403
404 static inline const char *ath10k_cal_mode_str(enum ath10k_cal_mode mode)
405 {
406         switch (mode) {
407         case ATH10K_CAL_MODE_FILE:
408                 return "file";
409         case ATH10K_CAL_MODE_OTP:
410                 return "otp";
411         }
412
413         return "unknown";
414 }
415
416 enum ath10k_scan_state {
417         ATH10K_SCAN_IDLE,
418         ATH10K_SCAN_STARTING,
419         ATH10K_SCAN_RUNNING,
420         ATH10K_SCAN_ABORTING,
421 };
422
423 static inline const char *ath10k_scan_state_str(enum ath10k_scan_state state)
424 {
425         switch (state) {
426         case ATH10K_SCAN_IDLE:
427                 return "idle";
428         case ATH10K_SCAN_STARTING:
429                 return "starting";
430         case ATH10K_SCAN_RUNNING:
431                 return "running";
432         case ATH10K_SCAN_ABORTING:
433                 return "aborting";
434         }
435
436         return "unknown";
437 }
438
439 struct ath10k {
440         struct ath_common ath_common;
441         struct ieee80211_hw *hw;
442         struct device *dev;
443         u8 mac_addr[ETH_ALEN];
444
445         u32 chip_id;
446         u32 target_version;
447         u8 fw_version_major;
448         u32 fw_version_minor;
449         u16 fw_version_release;
450         u16 fw_version_build;
451         u32 phy_capability;
452         u32 hw_min_tx_power;
453         u32 hw_max_tx_power;
454         u32 ht_cap_info;
455         u32 vht_cap_info;
456         u32 num_rf_chains;
457
458         DECLARE_BITMAP(fw_features, ATH10K_FW_FEATURE_COUNT);
459
460         struct targetdef *targetdef;
461         struct hostdef *hostdef;
462
463         bool p2p;
464
465         struct {
466                 enum ath10k_bus bus;
467                 const struct ath10k_hif_ops *ops;
468         } hif;
469
470         struct completion target_suspend;
471
472         struct ath10k_bmi bmi;
473         struct ath10k_wmi wmi;
474         struct ath10k_htc htc;
475         struct ath10k_htt htt;
476
477         struct ath10k_hw_params {
478                 u32 id;
479                 const char *name;
480                 u32 patch_load_addr;
481
482                 struct ath10k_hw_params_fw {
483                         const char *dir;
484                         const char *fw;
485                         const char *otp;
486                         const char *board;
487                 } fw;
488         } hw_params;
489
490         const struct firmware *board;
491         const void *board_data;
492         size_t board_len;
493
494         const struct firmware *otp;
495         const void *otp_data;
496         size_t otp_len;
497
498         const struct firmware *firmware;
499         const void *firmware_data;
500         size_t firmware_len;
501
502         const struct firmware *cal_file;
503
504         int fw_api;
505         enum ath10k_cal_mode cal_mode;
506
507         struct {
508                 struct completion started;
509                 struct completion completed;
510                 struct completion on_channel;
511                 struct delayed_work timeout;
512                 enum ath10k_scan_state state;
513                 bool is_roc;
514                 int vdev_id;
515                 int roc_freq;
516         } scan;
517
518         struct {
519                 struct ieee80211_supported_band sbands[IEEE80211_NUM_BANDS];
520         } mac;
521
522         /* should never be NULL; needed for regular htt rx */
523         struct ieee80211_channel *rx_channel;
524
525         /* valid during scan; needed for mgmt rx during scan */
526         struct ieee80211_channel *scan_channel;
527
528         /* current operating channel definition */
529         struct cfg80211_chan_def chandef;
530
531         unsigned long long free_vdev_map;
532         bool monitor;
533         int monitor_vdev_id;
534         bool monitor_started;
535         unsigned int filter_flags;
536         unsigned long dev_flags;
537         u32 dfs_block_radar_events;
538
539         /* protected by conf_mutex */
540         bool radar_enabled;
541         int num_started_vdevs;
542
543         /* Protected by conf-mutex */
544         u8 supp_tx_chainmask;
545         u8 supp_rx_chainmask;
546         u8 cfg_tx_chainmask;
547         u8 cfg_rx_chainmask;
548
549         struct wmi_pdev_set_wmm_params_arg wmm_params;
550         struct completion install_key_done;
551
552         struct completion vdev_setup_done;
553
554         struct workqueue_struct *workqueue;
555
556         /* prevents concurrent FW reconfiguration */
557         struct mutex conf_mutex;
558
559         /* protects shared structure data */
560         spinlock_t data_lock;
561
562         struct list_head arvifs;
563         struct list_head peers;
564         wait_queue_head_t peer_mapping_wq;
565
566         /* number of created peers; protected by data_lock */
567         int num_peers;
568
569         struct work_struct offchan_tx_work;
570         struct sk_buff_head offchan_tx_queue;
571         struct completion offchan_tx_completed;
572         struct sk_buff *offchan_tx_skb;
573
574         struct work_struct wmi_mgmt_tx_work;
575         struct sk_buff_head wmi_mgmt_tx_queue;
576
577         enum ath10k_state state;
578
579         struct work_struct register_work;
580         struct work_struct restart_work;
581
582         /* cycle count is reported twice for each visited channel during scan.
583          * access protected by data_lock */
584         u32 survey_last_rx_clear_count;
585         u32 survey_last_cycle_count;
586         struct survey_info survey[ATH10K_NUM_CHANS];
587
588         struct dfs_pattern_detector *dfs_detector;
589
590 #ifdef CONFIG_ATH10K_DEBUGFS
591         struct ath10k_debug debug;
592 #endif
593
594         struct {
595                 /* relay(fs) channel for spectral scan */
596                 struct rchan *rfs_chan_spec_scan;
597
598                 /* spectral_mode and spec_config are protected by conf_mutex */
599                 enum ath10k_spectral_mode mode;
600                 struct ath10k_spec_scan config;
601         } spectral;
602
603         struct {
604                 /* protected by conf_mutex */
605                 const struct firmware *utf;
606                 DECLARE_BITMAP(orig_fw_features, ATH10K_FW_FEATURE_COUNT);
607
608                 /* protected by data_lock */
609                 bool utf_monitor;
610         } testmode;
611
612         struct {
613                 /* protected by data_lock */
614                 u32 fw_crash_counter;
615                 u32 fw_warm_reset_counter;
616                 u32 fw_cold_reset_counter;
617         } stats;
618
619         /* must be last */
620         u8 drv_priv[0] __aligned(sizeof(void *));
621 };
622
623 struct ath10k *ath10k_core_create(size_t priv_size, struct device *dev,
624                                   enum ath10k_bus bus,
625                                   const struct ath10k_hif_ops *hif_ops);
626 void ath10k_core_destroy(struct ath10k *ar);
627
628 int ath10k_core_start(struct ath10k *ar, enum ath10k_firmware_mode mode);
629 int ath10k_wait_for_suspend(struct ath10k *ar, u32 suspend_opt);
630 void ath10k_core_stop(struct ath10k *ar);
631 int ath10k_core_register(struct ath10k *ar, u32 chip_id);
632 void ath10k_core_unregister(struct ath10k *ar);
633
634 #endif /* _CORE_H_ */