rt2x00mac.c 16 KB

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