rt2x00mac.c 16 KB

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