rt2x00mac.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. /*
  2. Copyright (C) 2004 - 2008 rt2x00 SourceForge Project
  3. <http://rt2x00.serialmonkey.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the
  14. Free Software Foundation, Inc.,
  15. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. /*
  18. Module: rt2x00mac
  19. Abstract: rt2x00 generic mac80211 routines.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include "rt2x00.h"
  24. #include "rt2x00lib.h"
  25. static int rt2x00mac_tx_rts_cts(struct rt2x00_dev *rt2x00dev,
  26. struct data_queue *queue,
  27. struct sk_buff *frag_skb,
  28. struct ieee80211_tx_control *control)
  29. {
  30. struct sk_buff *skb;
  31. int size;
  32. if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  33. size = sizeof(struct ieee80211_cts);
  34. else
  35. size = sizeof(struct ieee80211_rts);
  36. skb = dev_alloc_skb(size + rt2x00dev->hw->extra_tx_headroom);
  37. if (!skb) {
  38. WARNING(rt2x00dev, "Failed to create RTS/CTS frame.\n");
  39. return NETDEV_TX_BUSY;
  40. }
  41. skb_reserve(skb, rt2x00dev->hw->extra_tx_headroom);
  42. skb_put(skb, size);
  43. if (control->flags & IEEE80211_TXCTL_USE_CTS_PROTECT)
  44. ieee80211_ctstoself_get(rt2x00dev->hw, control->vif,
  45. frag_skb->data, frag_skb->len, control,
  46. (struct ieee80211_cts *)(skb->data));
  47. else
  48. ieee80211_rts_get(rt2x00dev->hw, control->vif,
  49. frag_skb->data, frag_skb->len, control,
  50. (struct ieee80211_rts *)(skb->data));
  51. if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, queue, skb, control)) {
  52. WARNING(rt2x00dev, "Failed to send RTS/CTS frame.\n");
  53. return NETDEV_TX_BUSY;
  54. }
  55. return NETDEV_TX_OK;
  56. }
  57. int rt2x00mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
  58. struct ieee80211_tx_control *control)
  59. {
  60. struct rt2x00_dev *rt2x00dev = hw->priv;
  61. struct ieee80211_hdr *ieee80211hdr = (struct ieee80211_hdr *)skb->data;
  62. struct data_queue *queue;
  63. u16 frame_control;
  64. /*
  65. * Mac80211 might be calling this function while we are trying
  66. * to remove the device or perhaps suspending it.
  67. * Note that we can only stop the TX queues inside the TX path
  68. * due to possible race conditions in mac80211.
  69. */
  70. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags)) {
  71. ieee80211_stop_queues(hw);
  72. return NETDEV_TX_OK;
  73. }
  74. /*
  75. * Determine which queue to put packet on.
  76. */
  77. queue = rt2x00queue_get_queue(rt2x00dev, control->queue);
  78. if (unlikely(!queue)) {
  79. ERROR(rt2x00dev,
  80. "Attempt to send packet over invalid queue %d.\n"
  81. "Please file bug report to %s.\n",
  82. control->queue, DRV_PROJECT);
  83. dev_kfree_skb_any(skb);
  84. return NETDEV_TX_OK;
  85. }
  86. /*
  87. * If CTS/RTS is required. and this frame is not CTS or RTS,
  88. * create and queue that frame first. But make sure we have
  89. * at least enough entries available to send this CTS/RTS
  90. * frame as well as the data frame.
  91. */
  92. frame_control = le16_to_cpu(ieee80211hdr->frame_control);
  93. if (!is_rts_frame(frame_control) && !is_cts_frame(frame_control) &&
  94. (control->flags & (IEEE80211_TXCTL_USE_RTS_CTS |
  95. IEEE80211_TXCTL_USE_CTS_PROTECT))) {
  96. if (rt2x00queue_available(queue) <= 1) {
  97. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  98. return NETDEV_TX_BUSY;
  99. }
  100. if (rt2x00mac_tx_rts_cts(rt2x00dev, queue, skb, control)) {
  101. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  102. return NETDEV_TX_BUSY;
  103. }
  104. }
  105. if (rt2x00dev->ops->lib->write_tx_data(rt2x00dev, queue, skb, control)) {
  106. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  107. return NETDEV_TX_BUSY;
  108. }
  109. if (rt2x00queue_full(queue))
  110. ieee80211_stop_queue(rt2x00dev->hw, control->queue);
  111. if (rt2x00dev->ops->lib->kick_tx_queue)
  112. rt2x00dev->ops->lib->kick_tx_queue(rt2x00dev, control->queue);
  113. return NETDEV_TX_OK;
  114. }
  115. EXPORT_SYMBOL_GPL(rt2x00mac_tx);
  116. int rt2x00mac_start(struct ieee80211_hw *hw)
  117. {
  118. struct rt2x00_dev *rt2x00dev = hw->priv;
  119. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  120. return 0;
  121. return rt2x00lib_start(rt2x00dev);
  122. }
  123. EXPORT_SYMBOL_GPL(rt2x00mac_start);
  124. void rt2x00mac_stop(struct ieee80211_hw *hw)
  125. {
  126. struct rt2x00_dev *rt2x00dev = hw->priv;
  127. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  128. return;
  129. rt2x00lib_stop(rt2x00dev);
  130. }
  131. EXPORT_SYMBOL_GPL(rt2x00mac_stop);
  132. int rt2x00mac_add_interface(struct ieee80211_hw *hw,
  133. struct ieee80211_if_init_conf *conf)
  134. {
  135. struct rt2x00_dev *rt2x00dev = hw->priv;
  136. struct rt2x00_intf *intf = vif_to_intf(conf->vif);
  137. struct data_queue *queue =
  138. rt2x00queue_get_queue(rt2x00dev, IEEE80211_TX_QUEUE_BEACON);
  139. struct queue_entry *entry = NULL;
  140. unsigned int i;
  141. /*
  142. * Don't allow interfaces to be added
  143. * the device has disappeared.
  144. */
  145. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
  146. !test_bit(DEVICE_STARTED, &rt2x00dev->flags))
  147. return -ENODEV;
  148. /*
  149. * When we don't support mixed interfaces (a combination
  150. * of sta and ap virtual interfaces) then we can only
  151. * add this interface when the rival interface count is 0.
  152. */
  153. if (!test_bit(DRIVER_SUPPORT_MIXED_INTERFACES, &rt2x00dev->flags) &&
  154. ((conf->type == IEEE80211_IF_TYPE_AP && rt2x00dev->intf_sta_count) ||
  155. (conf->type != IEEE80211_IF_TYPE_AP && rt2x00dev->intf_ap_count)))
  156. return -ENOBUFS;
  157. /*
  158. * Check if we exceeded the maximum amount of supported interfaces.
  159. */
  160. if ((conf->type == IEEE80211_IF_TYPE_AP &&
  161. rt2x00dev->intf_ap_count >= rt2x00dev->ops->max_ap_intf) ||
  162. (conf->type != IEEE80211_IF_TYPE_AP &&
  163. rt2x00dev->intf_sta_count >= rt2x00dev->ops->max_sta_intf))
  164. return -ENOBUFS;
  165. /*
  166. * Loop through all beacon queues to find a free
  167. * entry. Since there are as much beacon entries
  168. * as the maximum interfaces, this search shouldn't
  169. * fail.
  170. */
  171. for (i = 0; i < queue->limit; i++) {
  172. entry = &queue->entries[i];
  173. if (!__test_and_set_bit(ENTRY_BCN_ASSIGNED, &entry->flags))
  174. break;
  175. }
  176. if (unlikely(i == queue->limit))
  177. return -ENOBUFS;
  178. /*
  179. * We are now absolutely sure the interface can be created,
  180. * increase interface count and start initialization.
  181. */
  182. if (conf->type == IEEE80211_IF_TYPE_AP)
  183. rt2x00dev->intf_ap_count++;
  184. else
  185. rt2x00dev->intf_sta_count++;
  186. spin_lock_init(&intf->lock);
  187. intf->beacon = entry;
  188. if (conf->type == IEEE80211_IF_TYPE_AP)
  189. memcpy(&intf->bssid, conf->mac_addr, ETH_ALEN);
  190. memcpy(&intf->mac, conf->mac_addr, ETH_ALEN);
  191. /*
  192. * The MAC adddress must be configured after the device
  193. * has been initialized. Otherwise the device can reset
  194. * the MAC registers.
  195. */
  196. rt2x00lib_config_intf(rt2x00dev, intf, conf->type, intf->mac, NULL);
  197. return 0;
  198. }
  199. EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
  200. void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
  201. struct ieee80211_if_init_conf *conf)
  202. {
  203. struct rt2x00_dev *rt2x00dev = hw->priv;
  204. struct rt2x00_intf *intf = vif_to_intf(conf->vif);
  205. /*
  206. * Don't allow interfaces to be remove while
  207. * either the device has disappeared or when
  208. * no interface is present.
  209. */
  210. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
  211. (conf->type == IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_ap_count) ||
  212. (conf->type != IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_sta_count))
  213. return;
  214. if (conf->type == IEEE80211_IF_TYPE_AP)
  215. rt2x00dev->intf_ap_count--;
  216. else
  217. rt2x00dev->intf_sta_count--;
  218. /*
  219. * Release beacon entry so it is available for
  220. * new interfaces again.
  221. */
  222. __clear_bit(ENTRY_BCN_ASSIGNED, &intf->beacon->flags);
  223. /*
  224. * Make sure the bssid and mac address registers
  225. * are cleared to prevent false ACKing of frames.
  226. */
  227. rt2x00lib_config_intf(rt2x00dev, intf,
  228. IEEE80211_IF_TYPE_INVALID, NULL, NULL);
  229. }
  230. EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
  231. int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  232. {
  233. struct rt2x00_dev *rt2x00dev = hw->priv;
  234. /*
  235. * Mac80211 might be calling this function while we are trying
  236. * to remove the device or perhaps suspending it.
  237. */
  238. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  239. return 0;
  240. /*
  241. * Check if we need to disable the radio,
  242. * if this is not the case, at least the RX must be disabled.
  243. */
  244. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
  245. if (!conf->radio_enabled)
  246. rt2x00lib_disable_radio(rt2x00dev);
  247. else
  248. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  249. }
  250. rt2x00lib_config(rt2x00dev, conf, 0);
  251. /*
  252. * Reenable RX only if the radio should be on.
  253. */
  254. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  255. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  256. else if (conf->radio_enabled)
  257. return rt2x00lib_enable_radio(rt2x00dev);
  258. return 0;
  259. }
  260. EXPORT_SYMBOL_GPL(rt2x00mac_config);
  261. int rt2x00mac_config_interface(struct ieee80211_hw *hw,
  262. struct ieee80211_vif *vif,
  263. struct ieee80211_if_conf *conf)
  264. {
  265. struct rt2x00_dev *rt2x00dev = hw->priv;
  266. struct rt2x00_intf *intf = vif_to_intf(vif);
  267. int status;
  268. /*
  269. * Mac80211 might be calling this function while we are trying
  270. * to remove the device or perhaps suspending it.
  271. */
  272. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  273. return 0;
  274. spin_lock(&intf->lock);
  275. /*
  276. * If the interface does not work in master mode,
  277. * then the bssid value in the interface structure
  278. * should now be set.
  279. */
  280. if (conf->type != IEEE80211_IF_TYPE_AP)
  281. memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
  282. rt2x00lib_config_intf(rt2x00dev, intf, conf->type, NULL, intf->bssid);
  283. spin_unlock(&intf->lock);
  284. /*
  285. * We only need to initialize the beacon when master mode is enabled.
  286. */
  287. if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
  288. return 0;
  289. status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
  290. conf->beacon,
  291. conf->beacon_control);
  292. if (status)
  293. dev_kfree_skb(conf->beacon);
  294. return status;
  295. }
  296. EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
  297. int rt2x00mac_get_stats(struct ieee80211_hw *hw,
  298. struct ieee80211_low_level_stats *stats)
  299. {
  300. struct rt2x00_dev *rt2x00dev = hw->priv;
  301. /*
  302. * The dot11ACKFailureCount, dot11RTSFailureCount and
  303. * dot11RTSSuccessCount are updated in interrupt time.
  304. * dot11FCSErrorCount is updated in the link tuner.
  305. */
  306. memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
  307. return 0;
  308. }
  309. EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
  310. int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
  311. struct ieee80211_tx_queue_stats *stats)
  312. {
  313. struct rt2x00_dev *rt2x00dev = hw->priv;
  314. unsigned int i;
  315. for (i = 0; i < hw->queues; i++) {
  316. stats->data[i].len = rt2x00dev->tx[i].length;
  317. stats->data[i].limit = rt2x00dev->tx[i].limit;
  318. stats->data[i].count = rt2x00dev->tx[i].count;
  319. }
  320. return 0;
  321. }
  322. EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
  323. void rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
  324. struct ieee80211_vif *vif,
  325. struct ieee80211_bss_conf *bss_conf,
  326. u32 changes)
  327. {
  328. struct rt2x00_dev *rt2x00dev = hw->priv;
  329. struct rt2x00_intf *intf = vif_to_intf(vif);
  330. /*
  331. * When the association status has changed we must reset the link
  332. * tuner counter. This is because some drivers determine if they
  333. * should perform link tuning based on the number of seconds
  334. * while associated or not associated.
  335. */
  336. if (changes & BSS_CHANGED_ASSOC) {
  337. rt2x00dev->link.count = 0;
  338. if (bss_conf->assoc)
  339. rt2x00dev->intf_associated++;
  340. else
  341. rt2x00dev->intf_associated--;
  342. }
  343. /*
  344. * When the preamble mode has changed, we should perform additional
  345. * configuration steps. For all other changes we are already done.
  346. */
  347. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  348. rt2x00lib_config_preamble(rt2x00dev, intf,
  349. bss_conf->use_short_preamble);
  350. spin_lock(&intf->lock);
  351. memcpy(&intf->conf, bss_conf, sizeof(*bss_conf));
  352. spin_unlock(&intf->lock);
  353. }
  354. }
  355. EXPORT_SYMBOL_GPL(rt2x00mac_bss_info_changed);
  356. int rt2x00mac_conf_tx(struct ieee80211_hw *hw, int queue_idx,
  357. const struct ieee80211_tx_queue_params *params)
  358. {
  359. struct rt2x00_dev *rt2x00dev = hw->priv;
  360. struct data_queue *queue;
  361. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  362. if (unlikely(!queue))
  363. return -EINVAL;
  364. /*
  365. * The passed variables are stored as real value ((2^n)-1).
  366. * Ralink registers require to know the bit number 'n'.
  367. */
  368. if (params->cw_min > 0)
  369. queue->cw_min = fls(params->cw_min);
  370. else
  371. queue->cw_min = 5; /* cw_min: 2^5 = 32. */
  372. if (params->cw_max > 0)
  373. queue->cw_max = fls(params->cw_max);
  374. else
  375. queue->cw_max = 10; /* cw_min: 2^10 = 1024. */
  376. if (params->aifs >= 0)
  377. queue->aifs = params->aifs;
  378. else
  379. queue->aifs = 2;
  380. INFO(rt2x00dev,
  381. "Configured TX queue %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
  382. queue_idx, queue->cw_min, queue->cw_max, queue->aifs);
  383. return 0;
  384. }
  385. EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);