rx.c 24 KB

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