rx.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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 = (void *) 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. carl9170_update_beacon(ar, true);
  178. break;
  179. default:
  180. break;
  181. }
  182. rcu_read_unlock();
  183. break;
  184. case CARL9170_RSP_TXCOMP:
  185. /* TX status notification */
  186. carl9170_tx_process_status(ar, cmd);
  187. break;
  188. case CARL9170_RSP_BEACON_CONFIG:
  189. /*
  190. * (IBSS) beacon send notification
  191. * bytes: 04 c2 XX YY B4 B3 B2 B1
  192. *
  193. * XX always 80
  194. * YY always 00
  195. * B1-B4 "should" be the number of send out beacons.
  196. */
  197. break;
  198. case CARL9170_RSP_ATIM:
  199. /* End of Atim Window */
  200. break;
  201. case CARL9170_RSP_WATCHDOG:
  202. /* Watchdog Interrupt */
  203. carl9170_restart(ar, CARL9170_RR_WATCHDOG);
  204. break;
  205. case CARL9170_RSP_TEXT:
  206. /* firmware debug */
  207. carl9170_dbg_message(ar, (char *)buf + 4, len - 4);
  208. break;
  209. case CARL9170_RSP_HEXDUMP:
  210. wiphy_dbg(ar->hw->wiphy, "FW: HD %d\n", len - 4);
  211. print_hex_dump_bytes("FW:", DUMP_PREFIX_NONE,
  212. (char *)buf + 4, len - 4);
  213. break;
  214. case CARL9170_RSP_RADAR:
  215. if (!net_ratelimit())
  216. break;
  217. wiphy_info(ar->hw->wiphy, "FW: RADAR! Please report this "
  218. "incident to linux-wireless@vger.kernel.org !\n");
  219. break;
  220. case CARL9170_RSP_GPIO:
  221. #ifdef CONFIG_CARL9170_WPC
  222. if (ar->wps.pbc) {
  223. bool state = !!(cmd->gpio.gpio & cpu_to_le32(
  224. AR9170_GPIO_PORT_WPS_BUTTON_PRESSED));
  225. if (state != ar->wps.pbc_state) {
  226. ar->wps.pbc_state = state;
  227. input_report_key(ar->wps.pbc, KEY_WPS_BUTTON,
  228. state);
  229. input_sync(ar->wps.pbc);
  230. }
  231. }
  232. #endif /* CONFIG_CARL9170_WPC */
  233. break;
  234. case CARL9170_RSP_BOOT:
  235. complete(&ar->fw_boot_wait);
  236. break;
  237. default:
  238. wiphy_err(ar->hw->wiphy, "FW: received unhandled event %x\n",
  239. cmd->hdr.cmd);
  240. print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE, buf, len);
  241. break;
  242. }
  243. }
  244. static int carl9170_rx_mac_status(struct ar9170 *ar,
  245. struct ar9170_rx_head *head, struct ar9170_rx_macstatus *mac,
  246. struct ieee80211_rx_status *status)
  247. {
  248. struct ieee80211_channel *chan;
  249. u8 error, decrypt;
  250. BUILD_BUG_ON(sizeof(struct ar9170_rx_head) != 12);
  251. BUILD_BUG_ON(sizeof(struct ar9170_rx_macstatus) != 4);
  252. error = mac->error;
  253. if (error & AR9170_RX_ERROR_WRONG_RA) {
  254. if (!ar->sniffer_enabled)
  255. return -EINVAL;
  256. }
  257. if (error & AR9170_RX_ERROR_PLCP) {
  258. if (!(ar->filter_state & FIF_PLCPFAIL))
  259. return -EINVAL;
  260. status->flag |= RX_FLAG_FAILED_PLCP_CRC;
  261. }
  262. if (error & AR9170_RX_ERROR_FCS) {
  263. ar->tx_fcs_errors++;
  264. if (!(ar->filter_state & FIF_FCSFAIL))
  265. return -EINVAL;
  266. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  267. }
  268. decrypt = ar9170_get_decrypt_type(mac);
  269. if (!(decrypt & AR9170_RX_ENC_SOFTWARE) &&
  270. decrypt != AR9170_ENC_ALG_NONE) {
  271. if ((decrypt == AR9170_ENC_ALG_TKIP) &&
  272. (error & AR9170_RX_ERROR_MMIC))
  273. status->flag |= RX_FLAG_MMIC_ERROR;
  274. status->flag |= RX_FLAG_DECRYPTED;
  275. }
  276. if (error & AR9170_RX_ERROR_DECRYPT && !ar->sniffer_enabled)
  277. return -ENODATA;
  278. error &= ~(AR9170_RX_ERROR_MMIC |
  279. AR9170_RX_ERROR_FCS |
  280. AR9170_RX_ERROR_WRONG_RA |
  281. AR9170_RX_ERROR_DECRYPT |
  282. AR9170_RX_ERROR_PLCP);
  283. /* drop any other error frames */
  284. if (unlikely(error)) {
  285. /* TODO: update netdevice's RX dropped/errors statistics */
  286. if (net_ratelimit())
  287. wiphy_dbg(ar->hw->wiphy, "received frame with "
  288. "suspicious error code (%#x).\n", error);
  289. return -EINVAL;
  290. }
  291. chan = ar->channel;
  292. if (chan) {
  293. status->band = chan->band;
  294. status->freq = chan->center_freq;
  295. }
  296. switch (mac->status & AR9170_RX_STATUS_MODULATION) {
  297. case AR9170_RX_STATUS_MODULATION_CCK:
  298. if (mac->status & AR9170_RX_STATUS_SHORT_PREAMBLE)
  299. status->flag |= RX_FLAG_SHORTPRE;
  300. switch (head->plcp[0]) {
  301. case AR9170_RX_PHY_RATE_CCK_1M:
  302. status->rate_idx = 0;
  303. break;
  304. case AR9170_RX_PHY_RATE_CCK_2M:
  305. status->rate_idx = 1;
  306. break;
  307. case AR9170_RX_PHY_RATE_CCK_5M:
  308. status->rate_idx = 2;
  309. break;
  310. case AR9170_RX_PHY_RATE_CCK_11M:
  311. status->rate_idx = 3;
  312. break;
  313. default:
  314. if (net_ratelimit()) {
  315. wiphy_err(ar->hw->wiphy, "invalid plcp cck "
  316. "rate (%x).\n", head->plcp[0]);
  317. }
  318. return -EINVAL;
  319. }
  320. break;
  321. case AR9170_RX_STATUS_MODULATION_DUPOFDM:
  322. case AR9170_RX_STATUS_MODULATION_OFDM:
  323. switch (head->plcp[0] & 0xf) {
  324. case AR9170_TXRX_PHY_RATE_OFDM_6M:
  325. status->rate_idx = 0;
  326. break;
  327. case AR9170_TXRX_PHY_RATE_OFDM_9M:
  328. status->rate_idx = 1;
  329. break;
  330. case AR9170_TXRX_PHY_RATE_OFDM_12M:
  331. status->rate_idx = 2;
  332. break;
  333. case AR9170_TXRX_PHY_RATE_OFDM_18M:
  334. status->rate_idx = 3;
  335. break;
  336. case AR9170_TXRX_PHY_RATE_OFDM_24M:
  337. status->rate_idx = 4;
  338. break;
  339. case AR9170_TXRX_PHY_RATE_OFDM_36M:
  340. status->rate_idx = 5;
  341. break;
  342. case AR9170_TXRX_PHY_RATE_OFDM_48M:
  343. status->rate_idx = 6;
  344. break;
  345. case AR9170_TXRX_PHY_RATE_OFDM_54M:
  346. status->rate_idx = 7;
  347. break;
  348. default:
  349. if (net_ratelimit()) {
  350. wiphy_err(ar->hw->wiphy, "invalid plcp ofdm "
  351. "rate (%x).\n", head->plcp[0]);
  352. }
  353. return -EINVAL;
  354. }
  355. if (status->band == IEEE80211_BAND_2GHZ)
  356. status->rate_idx += 4;
  357. break;
  358. case AR9170_RX_STATUS_MODULATION_HT:
  359. if (head->plcp[3] & 0x80)
  360. status->flag |= RX_FLAG_40MHZ;
  361. if (head->plcp[6] & 0x80)
  362. status->flag |= RX_FLAG_SHORT_GI;
  363. status->rate_idx = clamp(0, 75, head->plcp[3] & 0x7f);
  364. status->flag |= RX_FLAG_HT;
  365. break;
  366. default:
  367. BUG();
  368. return -ENOSYS;
  369. }
  370. return 0;
  371. }
  372. static void carl9170_rx_phy_status(struct ar9170 *ar,
  373. struct ar9170_rx_phystatus *phy, struct ieee80211_rx_status *status)
  374. {
  375. int i;
  376. BUILD_BUG_ON(sizeof(struct ar9170_rx_phystatus) != 20);
  377. for (i = 0; i < 3; i++)
  378. if (phy->rssi[i] != 0x80)
  379. status->antenna |= BIT(i);
  380. /* post-process RSSI */
  381. for (i = 0; i < 7; i++)
  382. if (phy->rssi[i] & 0x80)
  383. phy->rssi[i] = ((phy->rssi[i] & 0x7f) + 1) & 0x7f;
  384. /* TODO: we could do something with phy_errors */
  385. status->signal = ar->noise[0] + phy->rssi_combined;
  386. }
  387. static struct sk_buff *carl9170_rx_copy_data(u8 *buf, int len)
  388. {
  389. struct sk_buff *skb;
  390. int reserved = 0;
  391. struct ieee80211_hdr *hdr = (void *) buf;
  392. if (ieee80211_is_data_qos(hdr->frame_control)) {
  393. u8 *qc = ieee80211_get_qos_ctl(hdr);
  394. reserved += NET_IP_ALIGN;
  395. if (*qc & IEEE80211_QOS_CONTROL_A_MSDU_PRESENT)
  396. reserved += NET_IP_ALIGN;
  397. }
  398. if (ieee80211_has_a4(hdr->frame_control))
  399. reserved += NET_IP_ALIGN;
  400. reserved = 32 + (reserved & NET_IP_ALIGN);
  401. skb = dev_alloc_skb(len + reserved);
  402. if (likely(skb)) {
  403. skb_reserve(skb, reserved);
  404. memcpy(skb_put(skb, len), buf, len);
  405. }
  406. return skb;
  407. }
  408. static u8 *carl9170_find_ie(u8 *data, unsigned int len, u8 ie)
  409. {
  410. struct ieee80211_mgmt *mgmt = (void *)data;
  411. u8 *pos, *end;
  412. pos = (u8 *)mgmt->u.beacon.variable;
  413. end = data + len;
  414. while (pos < end) {
  415. if (pos + 2 + pos[1] > end)
  416. return NULL;
  417. if (pos[0] == ie)
  418. return pos;
  419. pos += 2 + pos[1];
  420. }
  421. return NULL;
  422. }
  423. /*
  424. * NOTE:
  425. *
  426. * The firmware is in charge of waking up the device just before
  427. * the AP is expected to transmit the next beacon.
  428. *
  429. * This leaves the driver with the important task of deciding when
  430. * to set the PHY back to bed again.
  431. */
  432. static void carl9170_ps_beacon(struct ar9170 *ar, void *data, unsigned int len)
  433. {
  434. struct ieee80211_hdr *hdr = (void *) data;
  435. struct ieee80211_tim_ie *tim_ie;
  436. u8 *tim;
  437. u8 tim_len;
  438. bool cam;
  439. if (likely(!(ar->hw->conf.flags & IEEE80211_CONF_PS)))
  440. return;
  441. /* check if this really is a beacon */
  442. if (!ieee80211_is_beacon(hdr->frame_control))
  443. return;
  444. /* min. beacon length + FCS_LEN */
  445. if (len <= 40 + FCS_LEN)
  446. return;
  447. /* and only beacons from the associated BSSID, please */
  448. if (compare_ether_addr(hdr->addr3, ar->common.curbssid) ||
  449. !ar->common.curaid)
  450. return;
  451. ar->ps.last_beacon = jiffies;
  452. tim = carl9170_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
  453. if (!tim)
  454. return;
  455. if (tim[1] < sizeof(*tim_ie))
  456. return;
  457. tim_len = tim[1];
  458. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  459. if (!WARN_ON_ONCE(!ar->hw->conf.ps_dtim_period))
  460. ar->ps.dtim_counter = (tim_ie->dtim_count - 1) %
  461. ar->hw->conf.ps_dtim_period;
  462. /* Check whenever the PHY can be turned off again. */
  463. /* 1. What about buffered unicast traffic for our AID? */
  464. cam = ieee80211_check_tim(tim_ie, tim_len, ar->common.curaid);
  465. /* 2. Maybe the AP wants to send multicast/broadcast data? */
  466. cam = !!(tim_ie->bitmap_ctrl & 0x01);
  467. if (!cam) {
  468. /* back to low-power land. */
  469. ar->ps.off_override &= ~PS_OFF_BCN;
  470. carl9170_ps_check(ar);
  471. } else {
  472. /* force CAM */
  473. ar->ps.off_override |= PS_OFF_BCN;
  474. }
  475. }
  476. /*
  477. * If the frame alignment is right (or the kernel has
  478. * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
  479. * is only a single MPDU in the USB frame, then we could
  480. * submit to mac80211 the SKB directly. However, since
  481. * there may be multiple packets in one SKB in stream
  482. * mode, and we need to observe the proper ordering,
  483. * this is non-trivial.
  484. */
  485. static void carl9170_handle_mpdu(struct ar9170 *ar, u8 *buf, int len)
  486. {
  487. struct ar9170_rx_head *head;
  488. struct ar9170_rx_macstatus *mac;
  489. struct ar9170_rx_phystatus *phy = NULL;
  490. struct ieee80211_rx_status status;
  491. struct sk_buff *skb;
  492. int mpdu_len;
  493. if (!IS_STARTED(ar))
  494. return;
  495. if (unlikely(len < sizeof(*mac))) {
  496. ar->rx_dropped++;
  497. return;
  498. }
  499. mpdu_len = len - sizeof(*mac);
  500. mac = (void *)(buf + mpdu_len);
  501. if (unlikely(mac->error & AR9170_RX_ERROR_FATAL)) {
  502. ar->rx_dropped++;
  503. return;
  504. }
  505. switch (mac->status & AR9170_RX_STATUS_MPDU) {
  506. case AR9170_RX_STATUS_MPDU_FIRST:
  507. /* Aggregated MPDUs start with an PLCP header */
  508. if (likely(mpdu_len >= sizeof(struct ar9170_rx_head))) {
  509. head = (void *) buf;
  510. /*
  511. * The PLCP header needs to be cached for the
  512. * following MIDDLE + LAST A-MPDU packets.
  513. *
  514. * So, if you are wondering why all frames seem
  515. * to share a common RX status information,
  516. * then you have the answer right here...
  517. */
  518. memcpy(&ar->rx_plcp, (void *) buf,
  519. sizeof(struct ar9170_rx_head));
  520. mpdu_len -= sizeof(struct ar9170_rx_head);
  521. buf += sizeof(struct ar9170_rx_head);
  522. ar->rx_has_plcp = true;
  523. } else {
  524. if (net_ratelimit()) {
  525. wiphy_err(ar->hw->wiphy, "plcp info "
  526. "is clipped.\n");
  527. }
  528. ar->rx_dropped++;
  529. return;
  530. }
  531. break;
  532. case AR9170_RX_STATUS_MPDU_LAST:
  533. /*
  534. * The last frame of an A-MPDU has an extra tail
  535. * which does contain the phy status of the whole
  536. * aggregate.
  537. */
  538. if (likely(mpdu_len >= sizeof(struct ar9170_rx_phystatus))) {
  539. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  540. phy = (void *)(buf + mpdu_len);
  541. } else {
  542. if (net_ratelimit()) {
  543. wiphy_err(ar->hw->wiphy, "frame tail "
  544. "is clipped.\n");
  545. }
  546. ar->rx_dropped++;
  547. return;
  548. }
  549. case AR9170_RX_STATUS_MPDU_MIDDLE:
  550. /* These are just data + mac status */
  551. if (unlikely(!ar->rx_has_plcp)) {
  552. if (!net_ratelimit())
  553. return;
  554. wiphy_err(ar->hw->wiphy, "rx stream does not start "
  555. "with a first_mpdu frame tag.\n");
  556. ar->rx_dropped++;
  557. return;
  558. }
  559. head = &ar->rx_plcp;
  560. break;
  561. case AR9170_RX_STATUS_MPDU_SINGLE:
  562. /* single mpdu has both: plcp (head) and phy status (tail) */
  563. head = (void *) buf;
  564. mpdu_len -= sizeof(struct ar9170_rx_head);
  565. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  566. buf += sizeof(struct ar9170_rx_head);
  567. phy = (void *)(buf + mpdu_len);
  568. break;
  569. default:
  570. BUG_ON(1);
  571. break;
  572. }
  573. /* FC + DU + RA + FCS */
  574. if (unlikely(mpdu_len < (2 + 2 + 6 + FCS_LEN))) {
  575. ar->rx_dropped++;
  576. return;
  577. }
  578. memset(&status, 0, sizeof(status));
  579. if (unlikely(carl9170_rx_mac_status(ar, head, mac, &status))) {
  580. ar->rx_dropped++;
  581. return;
  582. }
  583. if (phy)
  584. carl9170_rx_phy_status(ar, phy, &status);
  585. carl9170_ps_beacon(ar, buf, mpdu_len);
  586. skb = carl9170_rx_copy_data(buf, mpdu_len);
  587. if (likely(skb)) {
  588. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  589. ieee80211_rx(ar->hw, skb);
  590. } else {
  591. ar->rx_dropped++;
  592. }
  593. }
  594. static void carl9170_rx_untie_cmds(struct ar9170 *ar, const u8 *respbuf,
  595. const unsigned int resplen)
  596. {
  597. struct carl9170_rsp *cmd;
  598. int i = 0;
  599. while (i < resplen) {
  600. cmd = (void *) &respbuf[i];
  601. i += cmd->hdr.len + 4;
  602. if (unlikely(i > resplen))
  603. break;
  604. carl9170_handle_command_response(ar, cmd, cmd->hdr.len + 4);
  605. }
  606. if (unlikely(i != resplen)) {
  607. if (!net_ratelimit())
  608. return;
  609. wiphy_err(ar->hw->wiphy, "malformed firmware trap:\n");
  610. print_hex_dump_bytes("rxcmd:", DUMP_PREFIX_OFFSET,
  611. respbuf, resplen);
  612. }
  613. }
  614. static void __carl9170_rx(struct ar9170 *ar, u8 *buf, unsigned int len)
  615. {
  616. unsigned int i = 0;
  617. /* weird thing, but this is the same in the original driver */
  618. while (len > 2 && i < 12 && buf[0] == 0xff && buf[1] == 0xff) {
  619. i += 2;
  620. len -= 2;
  621. buf += 2;
  622. }
  623. if (unlikely(len < 4))
  624. return;
  625. /* found the 6 * 0xffff marker? */
  626. if (i == 12)
  627. carl9170_rx_untie_cmds(ar, buf, len);
  628. else
  629. carl9170_handle_mpdu(ar, buf, len);
  630. }
  631. static void carl9170_rx_stream(struct ar9170 *ar, void *buf, unsigned int len)
  632. {
  633. unsigned int tlen, wlen = 0, clen = 0;
  634. struct ar9170_stream *rx_stream;
  635. u8 *tbuf;
  636. tbuf = buf;
  637. tlen = len;
  638. while (tlen >= 4) {
  639. rx_stream = (void *) tbuf;
  640. clen = le16_to_cpu(rx_stream->length);
  641. wlen = ALIGN(clen, 4);
  642. /* check if this is stream has a valid tag.*/
  643. if (rx_stream->tag != cpu_to_le16(AR9170_RX_STREAM_TAG)) {
  644. /*
  645. * TODO: handle the highly unlikely event that the
  646. * corrupted stream has the TAG at the right position.
  647. */
  648. /* check if the frame can be repaired. */
  649. if (!ar->rx_failover_missing) {
  650. /* this is not "short read". */
  651. if (net_ratelimit()) {
  652. wiphy_err(ar->hw->wiphy,
  653. "missing tag!\n");
  654. }
  655. __carl9170_rx(ar, tbuf, tlen);
  656. return;
  657. }
  658. if (ar->rx_failover_missing > tlen) {
  659. if (net_ratelimit()) {
  660. wiphy_err(ar->hw->wiphy,
  661. "possible multi "
  662. "stream corruption!\n");
  663. goto err_telluser;
  664. } else {
  665. goto err_silent;
  666. }
  667. }
  668. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  669. ar->rx_failover_missing -= tlen;
  670. if (ar->rx_failover_missing <= 0) {
  671. /*
  672. * nested carl9170_rx_stream call!
  673. *
  674. * termination is guranteed, even when the
  675. * combined frame also have an element with
  676. * a bad tag.
  677. */
  678. ar->rx_failover_missing = 0;
  679. carl9170_rx_stream(ar, ar->rx_failover->data,
  680. ar->rx_failover->len);
  681. skb_reset_tail_pointer(ar->rx_failover);
  682. skb_trim(ar->rx_failover, 0);
  683. }
  684. return;
  685. }
  686. /* check if stream is clipped */
  687. if (wlen > tlen - 4) {
  688. if (ar->rx_failover_missing) {
  689. /* TODO: handle double stream corruption. */
  690. if (net_ratelimit()) {
  691. wiphy_err(ar->hw->wiphy, "double rx "
  692. "stream corruption!\n");
  693. goto err_telluser;
  694. } else {
  695. goto err_silent;
  696. }
  697. }
  698. /*
  699. * save incomplete data set.
  700. * the firmware will resend the missing bits when
  701. * the rx - descriptor comes round again.
  702. */
  703. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  704. ar->rx_failover_missing = clen - tlen;
  705. return;
  706. }
  707. __carl9170_rx(ar, rx_stream->payload, clen);
  708. tbuf += wlen + 4;
  709. tlen -= wlen + 4;
  710. }
  711. if (tlen) {
  712. if (net_ratelimit()) {
  713. wiphy_err(ar->hw->wiphy, "%d bytes of unprocessed "
  714. "data left in rx stream!\n", tlen);
  715. }
  716. goto err_telluser;
  717. }
  718. return;
  719. err_telluser:
  720. wiphy_err(ar->hw->wiphy, "damaged RX stream data [want:%d, "
  721. "data:%d, rx:%d, pending:%d ]\n", clen, wlen, tlen,
  722. ar->rx_failover_missing);
  723. if (ar->rx_failover_missing)
  724. print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET,
  725. ar->rx_failover->data,
  726. ar->rx_failover->len);
  727. print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET,
  728. buf, len);
  729. wiphy_err(ar->hw->wiphy, "please check your hardware and cables, if "
  730. "you see this message frequently.\n");
  731. err_silent:
  732. if (ar->rx_failover_missing) {
  733. skb_reset_tail_pointer(ar->rx_failover);
  734. skb_trim(ar->rx_failover, 0);
  735. ar->rx_failover_missing = 0;
  736. }
  737. }
  738. void carl9170_rx(struct ar9170 *ar, void *buf, unsigned int len)
  739. {
  740. if (ar->fw.rx_stream)
  741. carl9170_rx_stream(ar, buf, len);
  742. else
  743. __carl9170_rx(ar, buf, len);
  744. }