2 * Atheros CARL9170 driver
4 * Driver specific definitions
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; see the file COPYING. If not, see
21 * http://www.gnu.org/licenses/.
23 * This file incorporates work covered by the following copyright and
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
27 * Permission to use, copy, modify, and/or distribute this software for any
28 * purpose with or without fee is hereby granted, provided that the above
29 * copyright notice and this permission notice appear in all copies.
31 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
37 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42 #include <linux/kernel.h>
43 #include <linux/firmware.h>
44 #include <linux/completion.h>
45 #include <linux/spinlock.h>
46 #include <net/cfg80211.h>
47 #include <net/mac80211.h>
48 #include <linux/usb.h>
49 #ifdef CONFIG_CARL9170_LEDS
50 #include <linux/leds.h>
51 #endif /* CONFIG_CARL9170_LEDS */
52 #ifdef CONFIG_CARL9170_WPC
53 #include <linux/input.h>
54 #endif /* CONFIG_CARL9170_WPC */
62 #ifdef CONFIG_CARL9170_DEBUGFS
64 #endif /* CONFIG_CARL9170_DEBUGFS */
66 #define CARL9170FW_NAME "carl9170-1.fw"
68 #define PAYLOAD_MAX (CARL9170_MAX_CMD_LEN / 4 - 1)
70 static const u8 ar9170_qmap[__AR9170_NUM_TXQ] = { 3, 2, 1, 0 };
72 enum carl9170_rf_init_mode {
78 #define CARL9170_MAX_RX_BUFFER_SIZE 8192
80 enum carl9170_device_state {
81 CARL9170_UNKNOWN_STATE,
87 #define CARL9170_NUM_TID 16
88 #define WME_BA_BMP_SIZE 64
89 #define CARL9170_TX_USER_RATE_TRIES 3
96 #define TID_TO_WME_AC(_tid) \
97 ((((_tid) == 0) || ((_tid) == 3)) ? WME_AC_BE : \
98 (((_tid) == 1) || ((_tid) == 2)) ? WME_AC_BK : \
99 (((_tid) == 4) || ((_tid) == 5)) ? WME_AC_VI : \
102 #define SEQ_DIFF(_start, _seq) \
103 (((_start) - (_seq)) & 0x0fff)
104 #define SEQ_PREV(_seq) \
105 (((_seq) - 1) & 0x0fff)
106 #define SEQ_NEXT(_seq) \
107 (((_seq) + 1) & 0x0fff)
108 #define BAW_WITHIN(_start, _bawsz, _seqno) \
109 ((((_seqno) - (_start)) & 0xfff) < (_bawsz))
111 enum carl9170_tid_state {
112 CARL9170_TID_STATE_INVALID,
113 CARL9170_TID_STATE_KILLED,
114 CARL9170_TID_STATE_SHUTDOWN,
115 CARL9170_TID_STATE_SUSPEND,
116 CARL9170_TID_STATE_PROGRESS,
117 CARL9170_TID_STATE_IDLE,
118 CARL9170_TID_STATE_XMIT,
121 #define CARL9170_BAW_BITS (2 * WME_BA_BMP_SIZE)
122 #define CARL9170_BAW_SIZE (BITS_TO_LONGS(CARL9170_BAW_BITS))
123 #define CARL9170_BAW_LEN (DIV_ROUND_UP(CARL9170_BAW_BITS, BITS_PER_BYTE))
125 struct carl9170_sta_tid {
126 /* must be the first entry! */
127 struct list_head list;
129 /* temporary list for RCU unlink procedure */
130 struct list_head tmp_list;
132 /* lock for the following data structures */
135 unsigned int counter;
136 enum carl9170_tid_state state;
137 u8 tid; /* TID number ( 0 - 15 ) */
138 u16 max; /* max. AMPDU size */
140 u16 snx; /* awaiting _next_ frame */
141 u16 hsn; /* highest _queued_ sequence */
142 u16 bsn; /* base of the tx/agg bitmap */
143 unsigned long bitmap[CARL9170_BAW_SIZE];
145 /* Preaggregation reorder queue */
146 struct sk_buff_head queue;
149 #define CARL9170_QUEUE_TIMEOUT 256
150 #define CARL9170_BUMP_QUEUE 1000
151 #define CARL9170_TX_TIMEOUT 2500
152 #define CARL9170_JANITOR_DELAY 128
153 #define CARL9170_QUEUE_STUCK_TIMEOUT 5500
154 #define CARL9170_STAT_WORK 30000
156 #define CARL9170_NUM_TX_AGG_MAX 30
159 * Tradeoff between stability/latency and speed.
161 * AR9170_TXQ_DEPTH is devised by dividing the amount of available
162 * tx buffers with the size of a full ethernet frame + overhead.
164 * Naturally: The higher the limit, the faster the device CAN send.
165 * However, even a slight over-commitment at the wrong time and the
166 * hardware is doomed to send all already-queued frames at suboptimal
167 * rates. This in turn leads to an enormous amount of unsuccessful
168 * retries => Latency goes up, whereas the throughput goes down. CRASH!
170 #define CARL9170_NUM_TX_LIMIT_HARD ((AR9170_TXQ_DEPTH * 3) / 2)
171 #define CARL9170_NUM_TX_LIMIT_SOFT (AR9170_TXQ_DEPTH)
173 struct carl9170_tx_queue_stats {
179 struct carl9170_vif {
181 struct ieee80211_vif __rcu *vif;
184 struct carl9170_vif_info {
185 struct list_head list;
188 struct sk_buff *beacon;
192 #define AR9170_NUM_RX_URBS 16
193 #define AR9170_NUM_RX_URBS_MUL 2
194 #define AR9170_NUM_TX_URBS 8
195 #define AR9170_NUM_RX_URBS_POOL (AR9170_NUM_RX_URBS_MUL * AR9170_NUM_RX_URBS)
197 enum carl9170_device_features {
198 CARL9170_WPS_BUTTON = BIT(0),
199 CARL9170_ONE_LED = BIT(1),
202 #ifdef CONFIG_CARL9170_LEDS
205 struct carl9170_led {
207 struct led_classdev l;
209 unsigned int toggled;
213 #endif /* CONFIG_CARL9170_LEDS */
215 enum carl9170_restart_reasons {
216 CARL9170_RR_NO_REASON = 0,
217 CARL9170_RR_FATAL_FIRMWARE_ERROR,
218 CARL9170_RR_TOO_MANY_FIRMWARE_ERRORS,
219 CARL9170_RR_WATCHDOG,
220 CARL9170_RR_STUCK_TX,
221 CARL9170_RR_UNRESPONSIVE_DEVICE,
222 CARL9170_RR_COMMAND_TIMEOUT,
223 CARL9170_RR_TOO_MANY_PHY_ERRORS,
224 CARL9170_RR_LOST_RSP,
225 CARL9170_RR_INVALID_RSP,
226 CARL9170_RR_USER_REQUEST,
231 enum carl9170_erp_modes {
232 CARL9170_ERP_INVALID,
234 CARL9170_ERP_MAC80211,
242 struct ath_common common;
243 struct ieee80211_hw *hw;
245 enum carl9170_device_state state;
246 spinlock_t state_lock;
247 enum carl9170_restart_reasons last_reason;
251 struct usb_device *udev;
252 struct usb_interface *intf;
253 struct usb_anchor rx_anch;
254 struct usb_anchor rx_work;
255 struct usb_anchor rx_pool;
256 struct usb_anchor tx_wait;
257 struct usb_anchor tx_anch;
258 struct usb_anchor tx_cmd;
259 struct usb_anchor tx_err;
260 struct tasklet_struct usb_tasklet;
261 atomic_t tx_cmd_urbs;
262 atomic_t tx_anch_urbs;
263 atomic_t rx_anch_urbs;
264 atomic_t rx_work_urbs;
265 atomic_t rx_pool_urbs;
266 kernel_ulong_t features;
268 /* firmware settings */
269 struct completion fw_load_wait;
270 struct completion fw_boot_wait;
272 const struct carl9170fw_desc_head *desc;
273 const struct firmware *fw;
275 unsigned int address;
276 unsigned int cmd_bufs;
277 unsigned int api_version;
278 unsigned int vif_num;
279 unsigned int err_counter;
280 unsigned int bug_counter;
282 unsigned int beacon_max_len;
287 unsigned int mem_blocks;
288 unsigned int mem_block_size;
289 unsigned int rx_size;
290 unsigned int tx_seq_table;
293 /* interface configuration combinations */
294 struct ieee80211_iface_limit if_comb_limits[1];
295 struct ieee80211_iface_combination if_combs[1];
297 /* reset / stuck frames/queue detection */
298 struct work_struct restart_work;
299 struct work_struct ping_work;
300 unsigned int restart_counter;
301 unsigned long queue_stop_timeout[__AR9170_NUM_TXQ];
302 unsigned long max_queue_stop_timeout[__AR9170_NUM_TXQ];
303 bool needs_full_reset;
304 atomic_t pending_restarts;
306 /* interface mode settings */
307 struct list_head vif_list;
308 unsigned long vif_bitmap;
310 struct carl9170_vif vif_priv[AR9170_MAX_VIRTUAL_MAC];
313 spinlock_t beacon_lock;
314 unsigned int global_pretbtt;
315 unsigned int global_beacon_int;
316 struct carl9170_vif_info __rcu *beacon_iter;
317 unsigned int beacon_enabled;
319 /* cryptographic engine */
321 bool rx_software_decryption;
322 bool disable_offload;
324 /* filter settings */
327 unsigned int filter_state;
328 unsigned int rx_filter_caps;
329 bool sniffer_enabled;
332 enum carl9170_erp_modes erp_mode;
335 struct ieee80211_channel *channel;
336 unsigned int num_channels;
338 unsigned int chan_fail;
339 unsigned int total_chan_fail;
343 u64 active; /* usec */
345 u64 tx_time; /* usec */
349 struct delayed_work stat_work;
350 struct survey_info *survey;
352 /* power calibration data */
361 #ifdef CONFIG_CARL9170_LEDS
363 struct delayed_work led_work;
364 struct carl9170_led leds[AR9170_NUM_LEDS];
365 #endif /* CONFIG_CARL9170_LEDS */
367 /* qos queue settings */
368 spinlock_t tx_stats_lock;
369 struct carl9170_tx_queue_stats tx_stats[__AR9170_NUM_TXQ];
370 struct ieee80211_tx_queue_params edcf[5];
371 struct completion tx_flush;
378 struct completion cmd_wait;
380 __le32 cmd_buf[PAYLOAD_MAX + 1];
381 struct carl9170_cmd cmd;
382 struct carl9170_rsp rsp;
386 unsigned int tx_dropped;
387 unsigned int tx_ack_failures;
388 unsigned int tx_fcs_errors;
389 unsigned int rx_dropped;
392 struct ar9170_eeprom eeprom;
395 struct sk_buff_head tx_pending[__AR9170_NUM_TXQ];
396 struct sk_buff_head tx_status[__AR9170_NUM_TXQ];
397 struct delayed_work tx_janitor;
398 unsigned long tx_janitor_last_run;
402 struct work_struct ampdu_work;
403 spinlock_t tx_ampdu_list_lock;
404 struct carl9170_sta_tid __rcu *tx_ampdu_iter;
405 struct list_head tx_ampdu_list;
406 atomic_t tx_ampdu_upload;
407 atomic_t tx_ampdu_scheduler;
408 atomic_t tx_total_pending;
409 atomic_t tx_total_queued;
410 unsigned int tx_ampdu_list_len;
413 bool tx_ampdu_schedule;
415 /* internal memory management */
417 unsigned long *mem_bitmap;
418 atomic_t mem_free_blocks;
421 /* rxstream mpdu merge */
422 struct ar9170_rx_head rx_plcp;
424 struct sk_buff *rx_failover;
425 int rx_failover_missing;
427 #ifdef CONFIG_CARL9170_WPC
430 struct input_dev *pbc;
434 #endif /* CONFIG_CARL9170_WPC */
436 #ifdef CONFIG_CARL9170_DEBUGFS
437 struct carl9170_debug debug;
438 struct dentry *debug_dir;
439 #endif /* CONFIG_CARL9170_DEBUGFS */
442 struct work_struct ps_work;
444 unsigned int dtim_counter;
445 unsigned long last_beacon;
446 unsigned long last_action;
447 unsigned long last_slept;
448 unsigned int sleep_ms;
449 unsigned int off_override;
454 enum carl9170_ps_off_override_reasons {
459 struct carl9170_ba_stats {
466 struct carl9170_sta_info {
469 atomic_t pending_frames;
470 unsigned int ampdu_max_len;
471 struct carl9170_sta_tid __rcu *agg[CARL9170_NUM_TID];
472 struct carl9170_ba_stats stats[CARL9170_NUM_TID];
475 struct carl9170_tx_info {
476 unsigned long timeout;
481 #define CHK_DEV_STATE(a, s) (((struct ar9170 *)a)->state >= (s))
482 #define IS_INITIALIZED(a) (CHK_DEV_STATE(a, CARL9170_STOPPED))
483 #define IS_ACCEPTING_CMD(a) (CHK_DEV_STATE(a, CARL9170_IDLE))
484 #define IS_STARTED(a) (CHK_DEV_STATE(a, CARL9170_STARTED))
486 static inline void __carl9170_set_state(struct ar9170 *ar,
487 enum carl9170_device_state newstate)
489 ar->state = newstate;
492 static inline void carl9170_set_state(struct ar9170 *ar,
493 enum carl9170_device_state newstate)
497 spin_lock_irqsave(&ar->state_lock, flags);
498 __carl9170_set_state(ar, newstate);
499 spin_unlock_irqrestore(&ar->state_lock, flags);
502 static inline void carl9170_set_state_when(struct ar9170 *ar,
503 enum carl9170_device_state min, enum carl9170_device_state newstate)
507 spin_lock_irqsave(&ar->state_lock, flags);
508 if (CHK_DEV_STATE(ar, min))
509 __carl9170_set_state(ar, newstate);
510 spin_unlock_irqrestore(&ar->state_lock, flags);
513 /* exported interface */
514 void *carl9170_alloc(size_t priv_size);
515 int carl9170_register(struct ar9170 *ar);
516 void carl9170_unregister(struct ar9170 *ar);
517 void carl9170_free(struct ar9170 *ar);
518 void carl9170_restart(struct ar9170 *ar, const enum carl9170_restart_reasons r);
519 void carl9170_ps_check(struct ar9170 *ar);
522 int carl9170_usb_open(struct ar9170 *ar);
523 void carl9170_usb_stop(struct ar9170 *ar);
524 void carl9170_usb_tx(struct ar9170 *ar, struct sk_buff *skb);
525 void carl9170_usb_handle_tx_err(struct ar9170 *ar);
526 int carl9170_exec_cmd(struct ar9170 *ar, const enum carl9170_cmd_oids,
527 u32 plen, void *payload, u32 rlen, void *resp);
528 int __carl9170_exec_cmd(struct ar9170 *ar, struct carl9170_cmd *cmd,
529 const bool free_buf);
530 int carl9170_usb_restart(struct ar9170 *ar);
531 void carl9170_usb_reset(struct ar9170 *ar);
534 int carl9170_init_mac(struct ar9170 *ar);
535 int carl9170_set_qos(struct ar9170 *ar);
536 int carl9170_update_multicast(struct ar9170 *ar, const u64 mc_hast);
537 int carl9170_mod_virtual_mac(struct ar9170 *ar, const unsigned int id,
539 int carl9170_set_operating_mode(struct ar9170 *ar);
540 int carl9170_set_beacon_timers(struct ar9170 *ar);
541 int carl9170_set_dyn_sifs_ack(struct ar9170 *ar);
542 int carl9170_set_rts_cts_rate(struct ar9170 *ar);
543 int carl9170_set_ampdu_settings(struct ar9170 *ar);
544 int carl9170_set_slot_time(struct ar9170 *ar);
545 int carl9170_set_mac_rates(struct ar9170 *ar);
546 int carl9170_set_hwretry_limit(struct ar9170 *ar, const u32 max_retry);
547 int carl9170_upload_key(struct ar9170 *ar, const u8 id, const u8 *mac,
548 const u8 ktype, const u8 keyidx, const u8 *keydata, const int keylen);
549 int carl9170_disable_key(struct ar9170 *ar, const u8 id);
552 void carl9170_rx(struct ar9170 *ar, void *buf, unsigned int len);
553 void carl9170_handle_command_response(struct ar9170 *ar, void *buf, u32 len);
556 void carl9170_op_tx(struct ieee80211_hw *hw, struct sk_buff *skb);
557 void carl9170_tx_janitor(struct work_struct *work);
558 void carl9170_tx_process_status(struct ar9170 *ar,
559 const struct carl9170_rsp *cmd);
560 void carl9170_tx_status(struct ar9170 *ar, struct sk_buff *skb,
562 void carl9170_tx_callback(struct ar9170 *ar, struct sk_buff *skb);
563 void carl9170_tx_drop(struct ar9170 *ar, struct sk_buff *skb);
564 void carl9170_tx_scheduler(struct ar9170 *ar);
565 void carl9170_tx_get_skb(struct sk_buff *skb);
566 int carl9170_tx_put_skb(struct sk_buff *skb);
567 int carl9170_update_beacon(struct ar9170 *ar, const bool submit);
570 #ifdef CONFIG_CARL9170_LEDS
571 int carl9170_led_register(struct ar9170 *ar);
572 void carl9170_led_unregister(struct ar9170 *ar);
573 #endif /* CONFIG_CARL9170_LEDS */
574 int carl9170_led_init(struct ar9170 *ar);
575 int carl9170_led_set_state(struct ar9170 *ar, const u32 led_state);
578 int carl9170_set_channel(struct ar9170 *ar, struct ieee80211_channel *channel,
579 enum nl80211_channel_type bw, enum carl9170_rf_init_mode rfi);
580 int carl9170_get_noisefloor(struct ar9170 *ar);
583 int carl9170_parse_firmware(struct ar9170 *ar);
584 int carl9170_fw_fix_eeprom(struct ar9170 *ar);
586 extern struct ieee80211_rate __carl9170_ratetable[];
587 extern int modparam_noht;
589 static inline struct ar9170 *carl9170_get_priv(struct carl9170_vif *carl_vif)
591 return container_of(carl_vif, struct ar9170,
592 vif_priv[carl_vif->id]);
595 static inline struct ieee80211_hdr *carl9170_get_hdr(struct sk_buff *skb)
597 return (void *)((struct _carl9170_tx_superframe *)
598 skb->data)->frame_data;
601 static inline u16 get_seq_h(struct ieee80211_hdr *hdr)
603 return le16_to_cpu(hdr->seq_ctrl) >> 4;
606 static inline u16 carl9170_get_seq(struct sk_buff *skb)
608 return get_seq_h(carl9170_get_hdr(skb));
611 static inline u16 get_tid_h(struct ieee80211_hdr *hdr)
613 return (ieee80211_get_qos_ctl(hdr))[0] & IEEE80211_QOS_CTL_TID_MASK;
616 static inline u16 carl9170_get_tid(struct sk_buff *skb)
618 return get_tid_h(carl9170_get_hdr(skb));
621 static inline struct ieee80211_vif *
622 carl9170_get_vif(struct carl9170_vif_info *priv)
624 return container_of((void *)priv, struct ieee80211_vif, drv_priv);
627 /* Protected by ar->mutex or RCU */
628 static inline struct ieee80211_vif *carl9170_get_main_vif(struct ar9170 *ar)
630 struct carl9170_vif_info *cvif;
632 list_for_each_entry_rcu(cvif, &ar->vif_list, list) {
634 return carl9170_get_vif(cvif);
640 static inline bool is_main_vif(struct ar9170 *ar, struct ieee80211_vif *vif)
645 ret = (carl9170_get_main_vif(ar) == vif);
650 #endif /* __CARL9170_H */