rt2x00mac.c 16 KB

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