rt2x00mac.c 16 KB

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