PageRenderTime 1097ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/net/wireless/ath/carl9170/rx.c

https://github.com/Mengqi/linux-2.6
C | 938 lines | 612 code | 174 blank | 152 comment | 138 complexity | a6fd65c1fbe72563254734b9a381edac MD5 | raw file
  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_CTL_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. static bool carl9170_ampdu_check(struct ar9170 *ar, u8 *buf, u8 ms)
  477. {
  478. __le16 fc;
  479. if ((ms & AR9170_RX_STATUS_MPDU) == AR9170_RX_STATUS_MPDU_SINGLE) {
  480. /*
  481. * This frame is not part of an aMPDU.
  482. * Therefore it is not subjected to any
  483. * of the following content restrictions.
  484. */
  485. return true;
  486. }
  487. /*
  488. * "802.11n - 7.4a.3 A-MPDU contents" describes in which contexts
  489. * certain frame types can be part of an aMPDU.
  490. *
  491. * In order to keep the processing cost down, I opted for a
  492. * stateless filter solely based on the frame control field.
  493. */
  494. fc = ((struct ieee80211_hdr *)buf)->frame_control;
  495. if (ieee80211_is_data_qos(fc) && ieee80211_is_data_present(fc))
  496. return true;
  497. if (ieee80211_is_ack(fc) || ieee80211_is_back(fc) ||
  498. ieee80211_is_back_req(fc))
  499. return true;
  500. if (ieee80211_is_action(fc))
  501. return true;
  502. return false;
  503. }
  504. /*
  505. * If the frame alignment is right (or the kernel has
  506. * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
  507. * is only a single MPDU in the USB frame, then we could
  508. * submit to mac80211 the SKB directly. However, since
  509. * there may be multiple packets in one SKB in stream
  510. * mode, and we need to observe the proper ordering,
  511. * this is non-trivial.
  512. */
  513. static void carl9170_handle_mpdu(struct ar9170 *ar, u8 *buf, int len)
  514. {
  515. struct ar9170_rx_head *head;
  516. struct ar9170_rx_macstatus *mac;
  517. struct ar9170_rx_phystatus *phy = NULL;
  518. struct ieee80211_rx_status status;
  519. struct sk_buff *skb;
  520. int mpdu_len;
  521. u8 mac_status;
  522. if (!IS_STARTED(ar))
  523. return;
  524. if (unlikely(len < sizeof(*mac)))
  525. goto drop;
  526. mpdu_len = len - sizeof(*mac);
  527. mac = (void *)(buf + mpdu_len);
  528. mac_status = mac->status;
  529. switch (mac_status & AR9170_RX_STATUS_MPDU) {
  530. case AR9170_RX_STATUS_MPDU_FIRST:
  531. /* Aggregated MPDUs start with an PLCP header */
  532. if (likely(mpdu_len >= sizeof(struct ar9170_rx_head))) {
  533. head = (void *) buf;
  534. /*
  535. * The PLCP header needs to be cached for the
  536. * following MIDDLE + LAST A-MPDU packets.
  537. *
  538. * So, if you are wondering why all frames seem
  539. * to share a common RX status information,
  540. * then you have the answer right here...
  541. */
  542. memcpy(&ar->rx_plcp, (void *) buf,
  543. sizeof(struct ar9170_rx_head));
  544. mpdu_len -= sizeof(struct ar9170_rx_head);
  545. buf += sizeof(struct ar9170_rx_head);
  546. ar->rx_has_plcp = true;
  547. } else {
  548. if (net_ratelimit()) {
  549. wiphy_err(ar->hw->wiphy, "plcp info "
  550. "is clipped.\n");
  551. }
  552. goto drop;
  553. }
  554. break;
  555. case AR9170_RX_STATUS_MPDU_LAST:
  556. /*
  557. * The last frame of an A-MPDU has an extra tail
  558. * which does contain the phy status of the whole
  559. * aggregate.
  560. */
  561. if (likely(mpdu_len >= sizeof(struct ar9170_rx_phystatus))) {
  562. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  563. phy = (void *)(buf + mpdu_len);
  564. } else {
  565. if (net_ratelimit()) {
  566. wiphy_err(ar->hw->wiphy, "frame tail "
  567. "is clipped.\n");
  568. }
  569. goto drop;
  570. }
  571. case AR9170_RX_STATUS_MPDU_MIDDLE:
  572. /* These are just data + mac status */
  573. if (unlikely(!ar->rx_has_plcp)) {
  574. if (!net_ratelimit())
  575. return;
  576. wiphy_err(ar->hw->wiphy, "rx stream does not start "
  577. "with a first_mpdu frame tag.\n");
  578. goto drop;
  579. }
  580. head = &ar->rx_plcp;
  581. break;
  582. case AR9170_RX_STATUS_MPDU_SINGLE:
  583. /* single mpdu has both: plcp (head) and phy status (tail) */
  584. head = (void *) buf;
  585. mpdu_len -= sizeof(struct ar9170_rx_head);
  586. mpdu_len -= sizeof(struct ar9170_rx_phystatus);
  587. buf += sizeof(struct ar9170_rx_head);
  588. phy = (void *)(buf + mpdu_len);
  589. break;
  590. default:
  591. BUG_ON(1);
  592. break;
  593. }
  594. /* FC + DU + RA + FCS */
  595. if (unlikely(mpdu_len < (2 + 2 + ETH_ALEN + FCS_LEN)))
  596. goto drop;
  597. memset(&status, 0, sizeof(status));
  598. if (unlikely(carl9170_rx_mac_status(ar, head, mac, &status)))
  599. goto drop;
  600. if (!carl9170_ampdu_check(ar, buf, mac_status))
  601. goto drop;
  602. if (phy)
  603. carl9170_rx_phy_status(ar, phy, &status);
  604. carl9170_ps_beacon(ar, buf, mpdu_len);
  605. skb = carl9170_rx_copy_data(buf, mpdu_len);
  606. if (!skb)
  607. goto drop;
  608. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  609. ieee80211_rx(ar->hw, skb);
  610. return;
  611. drop:
  612. ar->rx_dropped++;
  613. }
  614. static void carl9170_rx_untie_cmds(struct ar9170 *ar, const u8 *respbuf,
  615. const unsigned int resplen)
  616. {
  617. struct carl9170_rsp *cmd;
  618. int i = 0;
  619. while (i < resplen) {
  620. cmd = (void *) &respbuf[i];
  621. i += cmd->hdr.len + 4;
  622. if (unlikely(i > resplen))
  623. break;
  624. carl9170_handle_command_response(ar, cmd, cmd->hdr.len + 4);
  625. }
  626. if (unlikely(i != resplen)) {
  627. if (!net_ratelimit())
  628. return;
  629. wiphy_err(ar->hw->wiphy, "malformed firmware trap:\n");
  630. print_hex_dump_bytes("rxcmd:", DUMP_PREFIX_OFFSET,
  631. respbuf, resplen);
  632. }
  633. }
  634. static void __carl9170_rx(struct ar9170 *ar, u8 *buf, unsigned int len)
  635. {
  636. unsigned int i = 0;
  637. /* weird thing, but this is the same in the original driver */
  638. while (len > 2 && i < 12 && buf[0] == 0xff && buf[1] == 0xff) {
  639. i += 2;
  640. len -= 2;
  641. buf += 2;
  642. }
  643. if (unlikely(len < 4))
  644. return;
  645. /* found the 6 * 0xffff marker? */
  646. if (i == 12)
  647. carl9170_rx_untie_cmds(ar, buf, len);
  648. else
  649. carl9170_handle_mpdu(ar, buf, len);
  650. }
  651. static void carl9170_rx_stream(struct ar9170 *ar, void *buf, unsigned int len)
  652. {
  653. unsigned int tlen, wlen = 0, clen = 0;
  654. struct ar9170_stream *rx_stream;
  655. u8 *tbuf;
  656. tbuf = buf;
  657. tlen = len;
  658. while (tlen >= 4) {
  659. rx_stream = (void *) tbuf;
  660. clen = le16_to_cpu(rx_stream->length);
  661. wlen = ALIGN(clen, 4);
  662. /* check if this is stream has a valid tag.*/
  663. if (rx_stream->tag != cpu_to_le16(AR9170_RX_STREAM_TAG)) {
  664. /*
  665. * TODO: handle the highly unlikely event that the
  666. * corrupted stream has the TAG at the right position.
  667. */
  668. /* check if the frame can be repaired. */
  669. if (!ar->rx_failover_missing) {
  670. /* this is not "short read". */
  671. if (net_ratelimit()) {
  672. wiphy_err(ar->hw->wiphy,
  673. "missing tag!\n");
  674. }
  675. __carl9170_rx(ar, tbuf, tlen);
  676. return;
  677. }
  678. if (ar->rx_failover_missing > tlen) {
  679. if (net_ratelimit()) {
  680. wiphy_err(ar->hw->wiphy,
  681. "possible multi "
  682. "stream corruption!\n");
  683. goto err_telluser;
  684. } else {
  685. goto err_silent;
  686. }
  687. }
  688. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  689. ar->rx_failover_missing -= tlen;
  690. if (ar->rx_failover_missing <= 0) {
  691. /*
  692. * nested carl9170_rx_stream call!
  693. *
  694. * termination is guaranteed, even when the
  695. * combined frame also have an element with
  696. * a bad tag.
  697. */
  698. ar->rx_failover_missing = 0;
  699. carl9170_rx_stream(ar, ar->rx_failover->data,
  700. ar->rx_failover->len);
  701. skb_reset_tail_pointer(ar->rx_failover);
  702. skb_trim(ar->rx_failover, 0);
  703. }
  704. return;
  705. }
  706. /* check if stream is clipped */
  707. if (wlen > tlen - 4) {
  708. if (ar->rx_failover_missing) {
  709. /* TODO: handle double stream corruption. */
  710. if (net_ratelimit()) {
  711. wiphy_err(ar->hw->wiphy, "double rx "
  712. "stream corruption!\n");
  713. goto err_telluser;
  714. } else {
  715. goto err_silent;
  716. }
  717. }
  718. /*
  719. * save incomplete data set.
  720. * the firmware will resend the missing bits when
  721. * the rx - descriptor comes round again.
  722. */
  723. memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
  724. ar->rx_failover_missing = clen - tlen;
  725. return;
  726. }
  727. __carl9170_rx(ar, rx_stream->payload, clen);
  728. tbuf += wlen + 4;
  729. tlen -= wlen + 4;
  730. }
  731. if (tlen) {
  732. if (net_ratelimit()) {
  733. wiphy_err(ar->hw->wiphy, "%d bytes of unprocessed "
  734. "data left in rx stream!\n", tlen);
  735. }
  736. goto err_telluser;
  737. }
  738. return;
  739. err_telluser:
  740. wiphy_err(ar->hw->wiphy, "damaged RX stream data [want:%d, "
  741. "data:%d, rx:%d, pending:%d ]\n", clen, wlen, tlen,
  742. ar->rx_failover_missing);
  743. if (ar->rx_failover_missing)
  744. print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET,
  745. ar->rx_failover->data,
  746. ar->rx_failover->len);
  747. print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET,
  748. buf, len);
  749. wiphy_err(ar->hw->wiphy, "please check your hardware and cables, if "
  750. "you see this message frequently.\n");
  751. err_silent:
  752. if (ar->rx_failover_missing) {
  753. skb_reset_tail_pointer(ar->rx_failover);
  754. skb_trim(ar->rx_failover, 0);
  755. ar->rx_failover_missing = 0;
  756. }
  757. }
  758. void carl9170_rx(struct ar9170 *ar, void *buf, unsigned int len)
  759. {
  760. if (ar->fw.rx_stream)
  761. carl9170_rx_stream(ar, buf, len);
  762. else
  763. __carl9170_rx(ar, buf, len);
  764. }