rt2x00mac.c 13 KB

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