rx.c 24 KB

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