rt2x00mac.c 13 KB

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