rx.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. /*
  2. * Atheros CARL9170 driver
  3. *
  4. * 802.11 & command trap routines
  5. *
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; see the file COPYING. If not, see
  21. * http://www.gnu.org/licenses/.
  22. *
  23. * This file incorporates work covered by the following copyright and
  24. * permission notice:
  25. * Copyright (c) 2007-2008 Atheros Communications, Inc.
  26. *
  27. * Permission to use, copy, modify, and/or distribute this software for any
  28. * purpose with or without fee is hereby granted, provided that the above
  29. * copyright notice and this permission notice appear in all copies.
  30. *
  31. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  32. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  34. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  35. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  36. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  37. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  38. */
  39. #include <linux/init.h>
  40. #include <linux/slab.h>
  41. #include <linux/module.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/crc32.h>
  44. #include <net/mac80211.h>
  45. #include "carl9170.h"
  46. #include "hw.h"
  47. #include "cmd.h"
  48. static void carl9170_dbg_message(struct ar9170 *ar, const char *buf, u32 len)
  49. {
  50. bool restart = false;
  51. enum carl9170_restart_reasons reason = CARL9170_RR_NO_REASON;
  52. if (len > 3) {
  53. if (memcmp(buf, CARL9170_ERR_MAGIC, 3) == 0) {
  54. ar->fw.err_counter++;
  55. if (ar->fw.err_counter > 3) {
  56. restart = true;
  57. reason = CARL9170_RR_TOO_MANY_FIRMWARE_ERRORS;
  58. }
  59. }
  60. if (memcmp(buf, CARL9170_BUG_MAGIC, 3) == 0) {
  61. ar->fw.bug_counter++;
  62. restart = true;
  63. reason = CARL9170_RR_FATAL_FIRMWARE_ERROR;
  64. }
  65. }
  66. wiphy_info(ar->hw->wiphy, "FW: %.*s\n", len, buf);
  67. if (restart)
  68. carl9170_restart(ar, reason);
  69. }
  70. static void carl9170_handle_ps(struct ar9170 *ar, struct carl9170_rsp *rsp)
  71. {
  72. u32 ps;
  73. bool new_ps;
  74. ps = le32_to_cpu(rsp->psm.state);
  75. new_ps = (ps & CARL9170_PSM_COUNTER) != CARL9170_PSM_WAKE;
  76. if (ar->ps.state != new_ps) {
  77. if (!new_ps) {
  78. ar->ps.sleep_ms = jiffies_to_msecs(jiffies -
  79. ar->ps.last_action);
  80. }
  81. ar->ps.last_action = jiffies;
  82. ar->ps.state = new_ps;
  83. }
  84. }
  85. static int carl9170_check_sequence(struct ar9170 *ar, unsigned int seq)
  86. {
  87. if (ar->cmd_seq < -1)
  88. return 0;
  89. /*
  90. * Initialize Counter
  91. */
  92. if (ar->cmd_seq < 0)
  93. ar->cmd_seq = seq;
  94. /*
  95. * The sequence is strictly monotonic increasing and it never skips!
  96. *
  97. * Therefore we can safely assume that whenever we received an
  98. * unexpected sequence we have lost some valuable data.
  99. */
  100. if (seq != ar->cmd_seq) {
  101. int count;
  102. count = (seq - ar->cmd_seq) % ar->fw.cmd_bufs;
  103. wiphy_err(ar->hw->wiphy, "lost %d command responses/traps! "
  104. "w:%d g:%d\n", count, ar->cmd_seq, seq);
  105. carl9170_restart(ar, CARL9170_RR_LOST_RSP);
  106. return -EIO;
  107. }
  108. ar->cmd_seq = (ar->cmd_seq + 1) % ar->fw.cmd_bufs;
  109. return 0;
  110. }
  111. static void carl9170_cmd_callback(struct ar9170 *ar, u32 len, void *buffer)
  112. {
  113. /*
  114. * Some commands may have a variable response length
  115. * and we cannot predict the correct length in advance.
  116. * So we only check if we provided enough space for the data.
  117. */
  118. if (unlikely(ar->readlen != (len - 4))) {
  119. dev_warn(&ar->udev->dev, "received invalid command response:"
  120. "got %d, instead of %d\n", len - 4, ar->readlen);
  121. print_hex_dump_bytes("carl9170 cmd:", DUMP_PREFIX_OFFSET,
  122. ar->cmd_buf, (ar->cmd.hdr.len + 4) & 0x3f);
  123. print_hex_dump_bytes("carl9170 rsp:", DUMP_PREFIX_OFFSET,
  124. buffer, len);
  125. /*
  126. * Do not complete. The command times out,
  127. * and we get a stack trace from there.
  128. */
  129. carl9170_restart(ar, CARL9170_RR_INVALID_RSP);
  130. }
  131. spin_lock(&ar->cmd_lock);
  132. if (ar->readbuf) {
  133. if (len >= 4)
  134. memcpy(ar->readbuf, buffer + 4, len - 4);
  135. ar->readbuf = NULL;
  136. }
  137. complete(&ar->cmd_wait);
  138. spin_unlock(&ar->cmd_lock);
  139. }
  140. void carl9170_handle_command_response(struct ar9170 *ar, void *buf, u32 len)
  141. {
  142. struct carl9170_rsp *cmd = buf;
  143. struct ieee80211_vif *vif;
  144. if (carl9170_check_sequence(ar, cmd->hdr.seq))
  145. return;
  146. if ((cmd->hdr.cmd & CARL9170_RSP_FLAG) != CARL9170_RSP_FLAG) {
  147. if (!(cmd->hdr.cmd & CARL9170_CMD_ASYNC_FLAG))
  148. carl9170_cmd_callback(ar, len, buf);
  149. return;
  150. }
  151. if (unlikely(cmd->hdr.len != (len - 4))) {
  152. if (net_ratelimit()) {
  153. wiphy_err(ar->hw->wiphy, "FW: received over-/under"
  154. "sized event %x (%d, but should be %d).\n",
  155. cmd->hdr.cmd, cmd->hdr.len, len - 4);
  156. print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE,
  157. buf, len);
  158. }
  159. return;
  160. }
  161. /* hardware event handlers */
  162. switch (cmd->hdr.cmd) {
  163. case CARL9170_RSP_PRETBTT:
  164. /* pre-TBTT event */
  165. rcu_read_lock();
  166. vif = carl9170_get_main_vif(ar);
  167. if (!vif) {
  168. rcu_read_unlock();
  169. break;
  170. }
  171. switch (vif->type) {
  172. case NL80211_IFTYPE_STATION:
  173. carl9170_handle_ps(ar, cmd);
  174. break;
  175. case NL80211_IFTYPE_AP:
  176. case NL80211_IFTYPE_ADHOC:
  177. case NL80211_IFTYPE_MESH_POINT:
  178. carl9170_update_beacon(ar, true);
  179. break;
  180. default:
  181. break;
  182. }
  183. rcu_read_unlock();
  184. break;
  185. case CARL9170_RSP_TXCOMP:
  186. /* TX status notification */
  187. carl9170_tx_process_status(ar, cmd);
  188. break;
  189. case CARL9170_RSP_BEACON_CONFIG:
  190. /*
  191. * (IBSS) beacon send notification
  192. * bytes: 04 c2 XX YY B4 B3 B2 B1
  193. *
  194. * XX always 80
  195. * YY always 00
  196. * B1-B4 "should" be the number of send out beacons.
  197. */
  198. break;
  199. case CARL9170_RSP_ATIM:
  200. /* End of Atim Window */
  201. break;
  202. case CARL9170_RSP_WATCHDOG:
  203. /* Watchdog Interrupt */
  204. carl9170_restart(ar, CARL9170_RR_WATCHDOG);
  205. break;
  206. case CARL9170_RSP_TEXT:
  207. /* firmware debug */
  208. carl9170_dbg_message(ar, (char *)buf + 4, len - 4);
  209. break;
  210. case CARL9170_RSP_HEXDUMP:
  211. wiphy_dbg(ar->hw->wiphy, "FW: HD %d\n", len - 4);
  212. print_hex_dump_bytes("FW:", DUMP_PREFIX_NONE,
  213. (char *)buf + 4, len - 4);
  214. break;
  215. case CARL9170_RSP_RADAR:
  216. if (!net_ratelimit())
  217. break;
  218. wiphy_info(ar->hw->wiphy, "FW: RADAR! Please report this "
  219. "incident to linux-wireless@vger.kernel.org !\n");
  220. break;
  221. case CARL9170_RSP_GPIO:
  222. #ifdef CONFIG_CARL9170_WPC
  223. if (ar->wps.pbc) {
  224. bool state = !!(cmd->gpio.gpio & cpu_to_le32(
  225. AR9170_GPIO_PORT_WPS_BUTTON_PRESSED));
  226. if (state != ar->wps.pbc_state) {
  227. ar->wps.pbc_state = state;
  228. input_report_key(ar->wps.pbc, KEY_WPS_BUTTON,
  229. state);
  230. input_sync(ar->wps.pbc);
  231. }
  232. }
  233. #endif /* CONFIG_CARL9170_WPC */
  234. break;
  235. case CARL9170_RSP_BOOT:
  236. complete(&ar->fw_boot_wait);
  237. break;
  238. default:
  239. wiphy_err(ar->hw->wiphy, "FW: received unhandled event %x\n",
  240. cmd->hdr.cmd);
  241. print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE, buf, len);
  242. break;
  243. }
  244. }
  245. static int carl9170_rx_mac_status(struct ar9170 *ar,
  246. struct ar9170_rx_head *head, struct ar9170_rx_macstatus *mac,
  247. struct ieee80211_rx_status *status)
  248. {
  249. struct ieee80211_channel *chan;
  250. u8 error, decrypt;
  251. BUILD_BUG_ON(sizeof(struct ar9170_rx_head) != 12);
  252. BUILD_BUG_ON(sizeof(struct ar9170_rx_macstatus) != 4);
  253. error = mac->error;
  254. if (error & AR9170_RX_ERROR_WRONG_RA) {
  255. if (!ar->sniffer_enabled)
  256. return -EINVAL;
  257. }
  258. if (error & AR9170_RX_ERROR_PLCP) {
  259. if (!(ar->filter_state & FIF_PLCPFAIL))
  260. return -EINVAL;
  261. status->flag |= RX_FLAG_FAILED_PLCP_CRC;
  262. }
  263. if (error & AR9170_RX_ERROR_FCS) {
  264. ar->tx_fcs_errors++;
  265. if (!(ar->filter_state & FIF_FCSFAIL))
  266. return -EINVAL;
  267. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  268. }
  269. decrypt = ar9170_get_decrypt_type(mac);
  270. if (!(decrypt & AR9170_RX_ENC_SOFTWARE) &&
  271. decrypt != AR9170_ENC_ALG_NONE) {
  272. if ((decrypt == AR9170_ENC_ALG_TKIP) &&
  273. (error & AR9170_RX_ERROR_MMIC))
  274. status->flag |= RX_FLAG_MMIC_ERROR;
  275. status->flag |= RX_FLAG_DECRYPTED;
  276. }
  277. if (error & AR9170_RX_ERROR_DECRYPT && !ar->sniffer_enabled)
  278. return -ENODATA;
  279. error &= ~(AR9170_RX_ERROR_MMIC |
  280. AR9170_RX_ERROR_FCS |
  281. AR9170_RX_ERROR_WRONG_RA |
  282. AR9170_RX_ERROR_DECRYPT |
  283. AR9170_RX_ERROR_PLCP);
  284. /* drop any other error frames */
  285. if (unlikely(error)) {
  286. /* TODO: update netdevice's RX dropped/errors statistics */
  287. if (net_ratelimit())
  288. wiphy_dbg(ar->hw->wiphy, "received frame with "
  289. "suspicious error code (%#x).\n", error);
  290. return -EINVAL;
  291. }
  292. chan = ar->channel;
  293. if (chan) {
  294. status->band = chan->band;
  295. status->freq = chan->center_freq;
  296. }
  297. switch (mac->status & AR9170_RX_STATUS_MODULATION) {
  298. case AR9170_RX_STATUS_MODULATION_CCK:
  299. if (mac->status & AR9170_RX_STATUS_SHORT_PREAMBLE)
  300. status->flag |= RX_FLAG_SHORTPRE;
  301. switch (head->plcp[0]) {
  302. case AR9170_RX_PHY_RATE_CCK_1M:
  303. status->rate_idx = 0;
  304. break;
  305. case AR9170_RX_PHY_RATE_CCK_2M:
  306. status->rate_idx = 1;
  307. break;
  308. case AR9170_RX_PHY_RATE_CCK_5M:
  309. status->rate_idx = 2;
  310. break;
  311. case AR9170_RX_PHY_RATE_CCK_11M:
  312. status->rate_idx = 3;
  313. break;
  314. default:
  315. if (net_ratelimit()) {
  316. wiphy_err(ar->hw->wiphy, "invalid plcp cck "
  317. "rate (%x).\n", head->plcp[0]);
  318. }
  319. return -EINVAL;
  320. }
  321. break;
  322. case AR9170_RX_STATUS_MODULATION_DUPOFDM:
  323. case AR9170_RX_STATUS_MODULATION_OFDM:
  324. switch (head->plcp[0] & 0xf) {
  325. case AR9170_TXRX_PHY_RATE_OFDM_6M:
  326. status->rate_idx = 0;
  327. break;
  328. case AR9170_TXRX_PHY_RATE_OFDM_9M:
  329. status->rate_idx = 1;
  330. break;
  331. case AR9170_TXRX_PHY_RATE_OFDM_12M:
  332. status->rate_idx = 2;
  333. break;
  334. case AR9170_TXRX_PHY_RATE_OFDM_18M:
  335. status->rate_idx = 3;
  336. break;
  337. case AR9170_TXRX_PHY_RATE_OFDM_24M:
  338. status->rate_idx = 4;
  339. break;
  340. case AR9170_TXRX_PHY_RATE_OFDM_36M:
  341. status->rate_idx = 5;
  342. break;
  343. case AR9170_TXRX_PHY_RATE_OFDM_48M:
  344. status->rate_idx = 6;
  345. break;
  346. case AR9170_TXRX_PHY_RATE_OFDM_54M:
  347. status->rate_idx = 7;
  348. break;
  349. default:
  350. if (net_ratelimit()) {
  351. wiphy_err(ar->hw->wiphy, "invalid plcp ofdm "
  352. "rate (%x).\n", head->plcp[0]);
  353. }
  354. return -EINVAL;
  355. }
  356. if (status->band == IEEE80211_BAND_2GHZ)
  357. status->rate_idx += 4;
  358. break;
  359. case AR9170_RX_STATUS_MODULATION_HT:
  360. if (head->plcp[3] & 0x80)
  361. status->flag |= RX_FLAG_40MHZ;
  362. if (head->plcp[6] & 0x80)
  363. status->flag |= RX_FLAG_SHORT_GI;
  364. status->rate_idx = clamp(0, 75, head->plcp[3] & 0x7f);
  365. status->flag |= RX_FLAG_HT;
  366. break;
  367. default:
  368. BUG();
  369. return -ENOSYS;
  370. }
  371. return 0;
  372. }
  373. static void carl9170_rx_phy_status(struct ar9170 *ar,
  374. struct ar9170_rx_phystatus *phy, struct ieee80211_rx_status *status)
  375. {
  376. int i;
  377. BUILD_BUG_ON(sizeof(struct ar9170_rx_phystatus) != 20);
  378. for (i = 0; i < 3; i++)
  379. if (phy->rssi[i] != 0x80)
  380. status->antenna |= BIT(i);
  381. /* post-process RSSI */
  382. for (i = 0; i < 7; i++)
  383. if (phy->rssi[i] & 0x80)
  384. phy->rssi[i] = ((phy->rssi[i] & 0x7f) + 1) & 0x7f;
  385. /* TODO: we could do something with phy_errors */
  386. status->signal = ar->noise[0] + phy->rssi_combined;
  387. }
  388. static struct sk_buff *carl9170_rx_copy_data(u8 *buf, int len)
  389. {
  390. struct sk_buff *skb;
  391. int reserved = 0;
  392. struct ieee80211_hdr *hdr = (void *) buf;
  393. if (ieee80211_is_data_qos(hdr->frame_control)) {
  394. u8 *qc = ieee80211_get_qos_ctl(hdr);
  395. reserved += NET_IP_ALIGN;
  396. if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
  397. reserved += NET_IP_ALIGN;
  398. }
  399. if (ieee80211_has_a4(hdr->frame_control))
  400. reserved += NET_IP_ALIGN;
  401. reserved = 32 + (reserved & NET_IP_ALIGN);
  402. skb = dev_alloc_skb(len + reserved);
  403. if (likely(skb)) {
  404. skb_reserve(skb, reserved);
  405. memcpy(skb_put(skb, len), buf, len);
  406. }
  407. return skb;
  408. }
  409. static u8 *carl9170_find_ie(u8 *data, unsigned int len, u8 ie)
  410. {
  411. struct ieee80211_mgmt *mgmt = (void *)data;
  412. u8 *pos, *end;
  413. pos = (u8 *)mgmt->u.beacon.variable;
  414. end = data + len;
  415. while (pos < end) {
  416. if (pos + 2 + pos[1] > end)
  417. return NULL;
  418. if (pos[0] == ie)
  419. return pos;
  420. pos += 2 + pos[1];
  421. }
  422. return NULL;
  423. }
  424. /*
  425. * NOTE:
  426. *
  427. * The firmware is in charge of waking up the device just before
  428. * the AP is expected to transmit the next beacon.
  429. *
  430. * This leaves the driver with the important task of deciding when
  431. * to set the PHY back to bed again.
  432. */
  433. static void carl9170_ps_beacon(struct ar9170 *ar, void *data, unsigned int len)
  434. {
  435. struct ieee80211_hdr *hdr = data;
  436. struct ieee80211_tim_ie *tim_ie;
  437. u8 *tim;
  438. u8 tim_len;
  439. bool cam;
  440. if (likely(!(ar->hw->conf.flags & IEEE80211_CONF_PS)))
  441. return;
  442. /* check if this really is a beacon */
  443. if (!ieee80211_is_beacon(hdr->frame_control))
  444. return;
  445. /* min. beacon length + FCS_LEN */
  446. if (len <= 40 + FCS_LEN)
  447. return;
  448. /* and only beacons from the associated BSSID, please */
  449. if (!ether_addr_equal(hdr->addr3, ar->common.curbssid) ||
  450. !ar->common.curaid)
  451. return;
  452. ar->ps.last_beacon = jiffies;
  453. tim = carl9170_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
  454. if (!tim)
  455. return;
  456. if (tim[1] < sizeof(*tim_ie))
  457. return;
  458. tim_len = tim[1];
  459. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  460. if (!WARN_ON_ONCE(!ar->hw->conf.ps_dtim_period))
  461. ar->ps.dtim_counter = (tim_ie->dtim_count - 1) %
  462. ar->hw->conf.ps_dtim_period;
  463. /* Check whenever the PHY can be turned off again. */
  464. /* 1. What about buffered unicast traffic for our AID? */
  465. cam = ieee80211_check_tim(tim_ie, tim_len, ar->common.curaid);
  466. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  467. cam |= !!(tim_ie->bitmap_ctrl & 0x01);
  468. if (!cam) {
  469. /* back to low-power land. */
  470. ar->ps.off_override &= ~PS_OFF_BCN;
  471. carl9170_ps_check(ar);
  472. } else {
  473. /* force CAM */
  474. ar->ps.off_override |= PS_OFF_BCN;
  475. }
  476. }
  477. static void carl9170_ba_check(struct ar9170 *ar, void *data, unsigned int len)
  478. {
  479. struct ieee80211_bar *bar = (void *) data;
  480. struct carl9170_bar_list_entry *entry;
  481. unsigned int queue;
  482. if (likely(!ieee80211_is_back(bar->frame_control)))
  483. return;
  484. if (len <= sizeof(*bar) + FCS_LEN)
  485. return;
  486. queue = TID_TO_WME_AC(((le16_to_cpu(bar->control) &
  487. IEEE80211_BAR_CTRL_TID_INFO_MASK) >>
  488. IEEE80211_BAR_CTRL_TID_INFO_SHIFT) & 7);
  489. rcu_read_lock();
  490. list_for_each_entry_rcu(entry, &ar->bar_list[queue], list) {
  491. struct sk_buff *entry_skb = entry->skb;
  492. struct _carl9170_tx_superframe *super = (void *)entry_skb->data;
  493. struct ieee80211_bar *entry_bar = (void *)super->frame_data;
  494. #define TID_CHECK(a, b) ( \
  495. ((a) & cpu_to_le16(IEEE80211_BAR_CTRL_TID_INFO_MASK)) == \
  496. ((b) & cpu_to_le16(IEEE80211_BAR_CTRL_TID_INFO_MASK))) \
  497. if (bar->start_seq_num == entry_bar->start_seq_num &&
  498. TID_CHECK(bar->control, entry_bar->control) &&
  499. compare_ether_addr(bar->ra, entry_bar->ta) == 0 &&
  500. compare_ether_addr(bar->ta, entry_bar->ra) == 0) {
  501. struct ieee80211_tx_info *tx_info;
  502. tx_info = IEEE80211_SKB_CB(entry_skb);
  503. tx_info->flags |= IEEE80211_TX_STAT_ACK;
  504. spin_lock_bh(&ar->bar_list_lock[queue]);
  505. list_del_rcu(&entry->list);
  506. spin_unlock_bh(&ar->bar_list_lock[queue]);
  507. kfree_rcu(entry, head);
  508. break;
  509. }
  510. }
  511. rcu_read_unlock();
  512. #undef TID_CHECK
  513. }
  514. static bool carl9170_ampdu_check(struct ar9170 *ar, u8 *buf, u8 ms,
  515. struct ieee80211_rx_status *rx_status)
  516. {
  517. __le16 fc;
  518. if ((ms & AR9170_RX_STATUS_MPDU) == AR9170_RX_STATUS_MPDU_SINGLE) {
  519. /*
  520. * This frame is not part of an aMPDU.
  521. * Therefore it is not subjected to any
  522. * of the following content restrictions.
  523. */
  524. return true;
  525. }
  526. rx_status->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
  527. rx_status->ampdu_reference = ar->ampdu_ref;
  528. /*
  529. * "802.11n - 7.4a.3 A-MPDU contents" describes in which contexts
  530. * certain frame types can be part of an aMPDU.
  531. *
  532. * In order to keep the processing cost down, I opted for a
  533. * stateless filter solely based on the frame control field.
  534. */
  535. fc = ((struct ieee80211_hdr *)buf)->frame_control;
  536. if (ieee80211_is_data_qos(fc) && ieee80211_is_data_present(fc))
  537. return true;
  538. if (ieee80211_is_ack(fc) || ieee80211_is_back(fc) ||
  539. ieee80211_is_back_req(fc))
  540. return true;
  541. if (ieee80211_is_action(fc))
  542. return true;
  543. return false;
  544. }
  545. /*
  546. * If the frame alignment is right (or the kernel has
  547. * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
  548. * is only a single MPDU in the USB frame, then we could
  549. * submit to mac80211 the SKB directly. However, since
  550. * there may be multiple packets in one SKB in stream
  551. * mode, and we need to observe the proper ordering,
  552. * this is non-trivial.
  553. */
  554. static void carl9170_handle_mpdu(struct ar9170 *ar, u8 *buf, int len)
  555. {
  556. struct ar9170_rx_head *head;
  557. struct ar9170_rx_macstatus *mac;
  558. struct ar9170_rx_phystatus *phy = NULL;
  559. struct ieee80211_rx_status status;
  560. struct sk_buff *skb;
  561. int mpdu_len;
  562. u8 mac_status;
  563. if (!IS_STARTED(ar))
  564. return;
  565. if (unlikely(len < sizeof(*mac)))
  566. goto drop;
  567. memset(&status, 0, sizeof(status));
  568. mpdu_len = len - sizeof(*mac);
  569. mac = (void *)(buf + mpdu_len);
  570. mac_status = mac->status;
  571. switch (mac_status & AR9170_RX_STATUS_MPDU) {
  572. case AR9170_RX_STATUS_MPDU_FIRST:
  573. ar->ampdu_ref++;
  574. /* Aggregated MPDUs start with an PLCP header */
  575. if (likely(mpdu_len >= sizeof(struct ar9170_rx_head))) {
  576. head = (void *) buf;
  577. /*
  578. * The PLCP header needs to be cached for the
  579. * following MIDDLE + LAST A-MPDU packets.
  580. *
  581. * So, if you are wondering why all frames seem
  582. * to share a common RX status information,
  583. * then you have the answer right here...
  584. */
  585. memcpy(&ar->rx_plcp, (void *) buf,
  586. sizeof(struct ar9170_rx_head));
  587. mpdu_len -= sizeof(struct ar9170_rx_head);
  588. buf += sizeof(struct ar9170_rx_head);
  589. ar->rx_has_plcp = true;
  590. } else {
  591. if (net_ratelimit()) {
  592. wiphy_err(ar->hw->wiphy, "plcp info "
  593. "is clipped.\n");
  594. }
  595. goto drop;
  596. }
  597. break;
  598. case AR9170_RX_STATUS_MPDU_LAST:
  599. status.flag |= RX_FLAG_AMPDU_IS_LAST;
  600. /*
  601. * The last frame of an A-MPDU has an extra tail
  602. * which does contain the phy status of the whole
  603. * aggregate.
  604. */
  605. if (likely(mpdu_len >= sizeof(struct ar9170_rx_phystatus))) {
  606. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  607. phy = (void *)(buf + mpdu_len);
  608. } else {
  609. if (net_ratelimit()) {
  610. wiphy_err(ar->hw->wiphy, "frame tail "
  611. "is clipped.\n");
  612. }
  613. goto drop;
  614. }
  615. case AR9170_RX_STATUS_MPDU_MIDDLE:
  616. /* These are just data + mac status */
  617. if (unlikely(!ar->rx_has_plcp)) {
  618. if (!net_ratelimit())
  619. return;
  620. wiphy_err(ar->hw->wiphy, "rx stream does not start "
  621. "with a first_mpdu frame tag.\n");
  622. goto drop;
  623. }
  624. head = &ar->rx_plcp;
  625. break;
  626. case AR9170_RX_STATUS_MPDU_SINGLE:
  627. /* single mpdu has both: plcp (head) and phy status (tail) */
  628. head = (void *) buf;
  629. mpdu_len -= sizeof(struct ar9170_rx_head);
  630. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  631. buf += sizeof(struct ar9170_rx_head);
  632. phy = (void *)(buf + mpdu_len);
  633. break;
  634. default:
  635. BUG_ON(1);
  636. break;
  637. }
  638. /* FC + DU + RA + FCS */
  639. if (unlikely(mpdu_len < (2 + 2 + ETH_ALEN + FCS_LEN)))
  640. goto drop;
  641. if (unlikely(carl9170_rx_mac_status(ar, head, mac, &status)))
  642. goto drop;
  643. if (!carl9170_ampdu_check(ar, buf, mac_status, &status))
  644. goto drop;
  645. if (phy)
  646. carl9170_rx_phy_status(ar, phy, &status);
  647. carl9170_ps_beacon(ar, buf, mpdu_len);
  648. carl9170_ba_check(ar, buf, mpdu_len);
  649. skb = carl9170_rx_copy_data(buf, mpdu_len);
  650. if (!skb)
  651. goto drop;
  652. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  653. ieee80211_rx(ar->hw, skb);
  654. return;
  655. drop:
  656. ar->rx_dropped++;
  657. }
  658. static void carl9170_rx_untie_cmds(struct ar9170 *ar, const u8 *respbuf,
  659. const unsigned int resplen)
  660. {
  661. struct carl9170_rsp *cmd;
  662. int i = 0;
  663. while (i < resplen) {
  664. cmd = (void *) &respbuf[i];
  665. i += cmd->hdr.len + 4;
  666. if (unlikely(i > resplen))
  667. break;
  668. carl9170_handle_command_response(ar, cmd, cmd->hdr.len + 4);
  669. }
  670. if (unlikely(i != resplen)) {
  671. if (!net_ratelimit())
  672. return;
  673. wiphy_err(ar->hw->wiphy, "malformed firmware trap:\n");
  674. print_hex_dump_bytes("rxcmd:", DUMP_PREFIX_OFFSET,
  675. respbuf, resplen);
  676. }
  677. }
  678. static void __carl9170_rx(struct ar9170 *ar, u8 *buf, unsigned int len)
  679. {
  680. unsigned int i = 0;
  681. /* weird thing, but this is the same in the original driver */
  682. while (len > 2 && i < 12 && buf[0] == 0xff && buf[1] == 0xff) {
  683. i += 2;
  684. len -= 2;
  685. buf += 2;
  686. }
  687. if (unlikely(len < 4))
  688. return;
  689. /* found the 6 * 0xffff marker? */
  690. if (i == 12)
  691. carl9170_rx_untie_cmds(ar, buf, len);
  692. else
  693. carl9170_handle_mpdu(ar, buf, len);
  694. }
  695. static void carl9170_rx_stream(struct ar9170 *ar, void *buf, unsigned int len)
  696. {
  697. unsigned int tlen, wlen = 0, clen = 0;
  698. struct ar9170_stream *rx_stream;
  699. u8 *tbuf;
  700. tbuf = buf;
  701. tlen = len;
  702. while (tlen >= 4) {
  703. rx_stream = (void *) tbuf;
  704. clen = le16_to_cpu(rx_stream->length);
  705. wlen = ALIGN(clen, 4);
  706. /* check if this is stream has a valid tag.*/
  707. if (rx_stream->tag != cpu_to_le16(AR9170_RX_STREAM_TAG)) {
  708. /*
  709. * TODO: handle the highly unlikely event that the
  710. * corrupted stream has the TAG at the right position.
  711. */
  712. /* check if the frame can be repaired. */
  713. if (!ar->rx_failover_missing) {
  714. /* this is not "short read". */
  715. if (net_ratelimit()) {
  716. wiphy_err(ar->hw->wiphy,
  717. "missing tag!\n");
  718. }
  719. __carl9170_rx(ar, tbuf, tlen);
  720. return;
  721. }
  722. if (ar->rx_failover_missing > tlen) {
  723. if (net_ratelimit()) {
  724. wiphy_err(ar->hw->wiphy,
  725. "possible multi "
  726. "stream corruption!\n");
  727. goto err_telluser;
  728. } else {
  729. goto err_silent;
  730. }
  731. }
  732. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  733. ar->rx_failover_missing -= tlen;
  734. if (ar->rx_failover_missing <= 0) {
  735. /*
  736. * nested carl9170_rx_stream call!
  737. *
  738. * termination is guaranteed, even when the
  739. * combined frame also have an element with
  740. * a bad tag.
  741. */
  742. ar->rx_failover_missing = 0;
  743. carl9170_rx_stream(ar, ar->rx_failover->data,
  744. ar->rx_failover->len);
  745. skb_reset_tail_pointer(ar->rx_failover);
  746. skb_trim(ar->rx_failover, 0);
  747. }
  748. return;
  749. }
  750. /* check if stream is clipped */
  751. if (wlen > tlen - 4) {
  752. if (ar->rx_failover_missing) {
  753. /* TODO: handle double stream corruption. */
  754. if (net_ratelimit()) {
  755. wiphy_err(ar->hw->wiphy, "double rx "
  756. "stream corruption!\n");
  757. goto err_telluser;
  758. } else {
  759. goto err_silent;
  760. }
  761. }
  762. /*
  763. * save incomplete data set.
  764. * the firmware will resend the missing bits when
  765. * the rx - descriptor comes round again.
  766. */
  767. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  768. ar->rx_failover_missing = clen - tlen;
  769. return;
  770. }
  771. __carl9170_rx(ar, rx_stream->payload, clen);
  772. tbuf += wlen + 4;
  773. tlen -= wlen + 4;
  774. }
  775. if (tlen) {
  776. if (net_ratelimit()) {
  777. wiphy_err(ar->hw->wiphy, "%d bytes of unprocessed "
  778. "data left in rx stream!\n", tlen);
  779. }
  780. goto err_telluser;
  781. }
  782. return;
  783. err_telluser:
  784. wiphy_err(ar->hw->wiphy, "damaged RX stream data [want:%d, "
  785. "data:%d, rx:%d, pending:%d ]\n", clen, wlen, tlen,
  786. ar->rx_failover_missing);
  787. if (ar->rx_failover_missing)
  788. print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET,
  789. ar->rx_failover->data,
  790. ar->rx_failover->len);
  791. print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET,
  792. buf, len);
  793. wiphy_err(ar->hw->wiphy, "please check your hardware and cables, if "
  794. "you see this message frequently.\n");
  795. err_silent:
  796. if (ar->rx_failover_missing) {
  797. skb_reset_tail_pointer(ar->rx_failover);
  798. skb_trim(ar->rx_failover, 0);
  799. ar->rx_failover_missing = 0;
  800. }
  801. }
  802. void carl9170_rx(struct ar9170 *ar, void *buf, unsigned int len)
  803. {
  804. if (ar->fw.rx_stream)
  805. carl9170_rx_stream(ar, buf, len);
  806. else
  807. __carl9170_rx(ar, buf, len);
  808. }