rt2x00mac.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  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. /*
  215. * Some filters depend on the current working mode. We can force
  216. * an update during the next configure_filter() run by mac80211 by
  217. * resetting the current packet_filter state.
  218. */
  219. rt2x00dev->packet_filter = 0;
  220. return 0;
  221. }
  222. EXPORT_SYMBOL_GPL(rt2x00mac_add_interface);
  223. void rt2x00mac_remove_interface(struct ieee80211_hw *hw,
  224. struct ieee80211_if_init_conf *conf)
  225. {
  226. struct rt2x00_dev *rt2x00dev = hw->priv;
  227. struct rt2x00_intf *intf = vif_to_intf(conf->vif);
  228. /*
  229. * Don't allow interfaces to be remove while
  230. * either the device has disappeared or when
  231. * no interface is present.
  232. */
  233. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags) ||
  234. (conf->type == IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_ap_count) ||
  235. (conf->type != IEEE80211_IF_TYPE_AP && !rt2x00dev->intf_sta_count))
  236. return;
  237. if (conf->type == IEEE80211_IF_TYPE_AP)
  238. rt2x00dev->intf_ap_count--;
  239. else
  240. rt2x00dev->intf_sta_count--;
  241. /*
  242. * Release beacon entry so it is available for
  243. * new interfaces again.
  244. */
  245. __clear_bit(ENTRY_BCN_ASSIGNED, &intf->beacon->flags);
  246. /*
  247. * Make sure the bssid and mac address registers
  248. * are cleared to prevent false ACKing of frames.
  249. */
  250. rt2x00lib_config_intf(rt2x00dev, intf,
  251. IEEE80211_IF_TYPE_INVALID, NULL, NULL);
  252. }
  253. EXPORT_SYMBOL_GPL(rt2x00mac_remove_interface);
  254. int rt2x00mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  255. {
  256. struct rt2x00_dev *rt2x00dev = hw->priv;
  257. /*
  258. * Mac80211 might be calling this function while we are trying
  259. * to remove the device or perhaps suspending it.
  260. */
  261. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  262. return 0;
  263. /*
  264. * Check if we need to disable the radio,
  265. * if this is not the case, at least the RX must be disabled.
  266. */
  267. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags)) {
  268. if (!conf->radio_enabled)
  269. rt2x00lib_disable_radio(rt2x00dev);
  270. else
  271. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_OFF);
  272. }
  273. rt2x00lib_config(rt2x00dev, conf, 0);
  274. /*
  275. * Reenable RX only if the radio should be on.
  276. */
  277. if (test_bit(DEVICE_ENABLED_RADIO, &rt2x00dev->flags))
  278. rt2x00lib_toggle_rx(rt2x00dev, STATE_RADIO_RX_ON);
  279. else if (conf->radio_enabled)
  280. return rt2x00lib_enable_radio(rt2x00dev);
  281. return 0;
  282. }
  283. EXPORT_SYMBOL_GPL(rt2x00mac_config);
  284. int rt2x00mac_config_interface(struct ieee80211_hw *hw,
  285. struct ieee80211_vif *vif,
  286. struct ieee80211_if_conf *conf)
  287. {
  288. struct rt2x00_dev *rt2x00dev = hw->priv;
  289. struct rt2x00_intf *intf = vif_to_intf(vif);
  290. int status;
  291. /*
  292. * Mac80211 might be calling this function while we are trying
  293. * to remove the device or perhaps suspending it.
  294. */
  295. if (!test_bit(DEVICE_PRESENT, &rt2x00dev->flags))
  296. return 0;
  297. spin_lock(&intf->lock);
  298. /*
  299. * If the interface does not work in master mode,
  300. * then the bssid value in the interface structure
  301. * should now be set.
  302. */
  303. if (conf->type != IEEE80211_IF_TYPE_AP)
  304. memcpy(&intf->bssid, conf->bssid, ETH_ALEN);
  305. spin_unlock(&intf->lock);
  306. /*
  307. * Call rt2x00_config_intf() outside of the spinlock context since
  308. * the call will sleep for USB drivers. By using the ieee80211_if_conf
  309. * values as arguments we make keep access to rt2x00_intf thread safe
  310. * even without the lock.
  311. */
  312. rt2x00lib_config_intf(rt2x00dev, intf, conf->type, NULL, conf->bssid);
  313. /*
  314. * We only need to initialize the beacon when master mode is enabled.
  315. */
  316. if (conf->type != IEEE80211_IF_TYPE_AP || !conf->beacon)
  317. return 0;
  318. status = rt2x00dev->ops->hw->beacon_update(rt2x00dev->hw,
  319. conf->beacon,
  320. conf->beacon_control);
  321. if (status)
  322. dev_kfree_skb(conf->beacon);
  323. return status;
  324. }
  325. EXPORT_SYMBOL_GPL(rt2x00mac_config_interface);
  326. void rt2x00mac_configure_filter(struct ieee80211_hw *hw,
  327. unsigned int changed_flags,
  328. unsigned int *total_flags,
  329. int mc_count, struct dev_addr_list *mc_list)
  330. {
  331. struct rt2x00_dev *rt2x00dev = hw->priv;
  332. /*
  333. * Mask off any flags we are going to ignore
  334. * from the total_flags field.
  335. */
  336. *total_flags &=
  337. FIF_ALLMULTI |
  338. FIF_FCSFAIL |
  339. FIF_PLCPFAIL |
  340. FIF_CONTROL |
  341. FIF_OTHER_BSS |
  342. FIF_PROMISC_IN_BSS;
  343. /*
  344. * Apply some rules to the filters:
  345. * - Some filters imply different filters to be set.
  346. * - Some things we can't filter out at all.
  347. * - Multicast filter seems to kill broadcast traffic so never use it.
  348. */
  349. *total_flags |= FIF_ALLMULTI;
  350. if (*total_flags & FIF_OTHER_BSS ||
  351. *total_flags & FIF_PROMISC_IN_BSS)
  352. *total_flags |= FIF_PROMISC_IN_BSS | FIF_OTHER_BSS;
  353. /*
  354. * Check if there is any work left for us.
  355. */
  356. if (rt2x00dev->packet_filter == *total_flags)
  357. return;
  358. rt2x00dev->packet_filter = *total_flags;
  359. if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
  360. rt2x00dev->ops->lib->config_filter(rt2x00dev, *total_flags);
  361. else
  362. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->filter_work);
  363. }
  364. EXPORT_SYMBOL_GPL(rt2x00mac_configure_filter);
  365. int rt2x00mac_get_stats(struct ieee80211_hw *hw,
  366. struct ieee80211_low_level_stats *stats)
  367. {
  368. struct rt2x00_dev *rt2x00dev = hw->priv;
  369. /*
  370. * The dot11ACKFailureCount, dot11RTSFailureCount and
  371. * dot11RTSSuccessCount are updated in interrupt time.
  372. * dot11FCSErrorCount is updated in the link tuner.
  373. */
  374. memcpy(stats, &rt2x00dev->low_level_stats, sizeof(*stats));
  375. return 0;
  376. }
  377. EXPORT_SYMBOL_GPL(rt2x00mac_get_stats);
  378. int rt2x00mac_get_tx_stats(struct ieee80211_hw *hw,
  379. struct ieee80211_tx_queue_stats *stats)
  380. {
  381. struct rt2x00_dev *rt2x00dev = hw->priv;
  382. unsigned int i;
  383. for (i = 0; i < hw->queues; i++) {
  384. stats->data[i].len = rt2x00dev->tx[i].length;
  385. stats->data[i].limit = rt2x00dev->tx[i].limit;
  386. stats->data[i].count = rt2x00dev->tx[i].count;
  387. }
  388. return 0;
  389. }
  390. EXPORT_SYMBOL_GPL(rt2x00mac_get_tx_stats);
  391. void rt2x00mac_bss_info_changed(struct ieee80211_hw *hw,
  392. struct ieee80211_vif *vif,
  393. struct ieee80211_bss_conf *bss_conf,
  394. u32 changes)
  395. {
  396. struct rt2x00_dev *rt2x00dev = hw->priv;
  397. struct rt2x00_intf *intf = vif_to_intf(vif);
  398. unsigned int delayed = 0;
  399. /*
  400. * When the association status has changed we must reset the link
  401. * tuner counter. This is because some drivers determine if they
  402. * should perform link tuning based on the number of seconds
  403. * while associated or not associated.
  404. */
  405. if (changes & BSS_CHANGED_ASSOC) {
  406. rt2x00dev->link.count = 0;
  407. if (bss_conf->assoc)
  408. rt2x00dev->intf_associated++;
  409. else
  410. rt2x00dev->intf_associated--;
  411. if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
  412. rt2x00leds_led_assoc(rt2x00dev,
  413. !!rt2x00dev->intf_associated);
  414. else
  415. delayed |= DELAYED_LED_ASSOC;
  416. }
  417. /*
  418. * When the erp information has changed, we should perform
  419. * additional configuration steps. For all other changes we are done.
  420. */
  421. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  422. if (!test_bit(DRIVER_REQUIRE_SCHEDULED, &rt2x00dev->flags))
  423. rt2x00lib_config_erp(rt2x00dev, intf, bss_conf);
  424. else
  425. delayed |= DELAYED_CONFIG_ERP;
  426. }
  427. spin_lock(&intf->lock);
  428. memcpy(&intf->conf, bss_conf, sizeof(*bss_conf));
  429. if (delayed) {
  430. intf->delayed_flags |= delayed;
  431. queue_work(rt2x00dev->hw->workqueue, &rt2x00dev->intf_work);
  432. }
  433. spin_unlock(&intf->lock);
  434. }
  435. EXPORT_SYMBOL_GPL(rt2x00mac_bss_info_changed);
  436. int rt2x00mac_conf_tx(struct ieee80211_hw *hw, int queue_idx,
  437. const struct ieee80211_tx_queue_params *params)
  438. {
  439. struct rt2x00_dev *rt2x00dev = hw->priv;
  440. struct data_queue *queue;
  441. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  442. if (unlikely(!queue))
  443. return -EINVAL;
  444. /*
  445. * The passed variables are stored as real value ((2^n)-1).
  446. * Ralink registers require to know the bit number 'n'.
  447. */
  448. if (params->cw_min > 0)
  449. queue->cw_min = fls(params->cw_min);
  450. else
  451. queue->cw_min = 5; /* cw_min: 2^5 = 32. */
  452. if (params->cw_max > 0)
  453. queue->cw_max = fls(params->cw_max);
  454. else
  455. queue->cw_max = 10; /* cw_min: 2^10 = 1024. */
  456. if (params->aifs >= 0)
  457. queue->aifs = params->aifs;
  458. else
  459. queue->aifs = 2;
  460. INFO(rt2x00dev,
  461. "Configured TX queue %d - CWmin: %d, CWmax: %d, Aifs: %d.\n",
  462. queue_idx, queue->cw_min, queue->cw_max, queue->aifs);
  463. return 0;
  464. }
  465. EXPORT_SYMBOL_GPL(rt2x00mac_conf_tx);