rt2x00mac.c 15 KB

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