2 * Atheros CARL9170 driver
4 * 802.11 & command trap routines
6 * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
7 * Copyright 2009, 2010, Christian Lamparter <chunkeey@googlemail.com>
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.
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.
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/.
23 * This file incorporates work covered by the following copyright and
25 * Copyright (c) 2007-2008 Atheros Communications, Inc.
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.
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.
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>
49 static void carl9170_dbg_message(struct ar9170 *ar, const char *buf, u32 len)
52 enum carl9170_restart_reasons reason = CARL9170_RR_NO_REASON;
55 if (memcmp(buf, CARL9170_ERR_MAGIC, 3) == 0) {
57 if (ar->fw.err_counter > 3) {
59 reason = CARL9170_RR_TOO_MANY_FIRMWARE_ERRORS;
63 if (memcmp(buf, CARL9170_BUG_MAGIC, 3) == 0) {
66 reason = CARL9170_RR_FATAL_FIRMWARE_ERROR;
70 wiphy_info(ar->hw->wiphy, "FW: %.*s\n", len, buf);
73 carl9170_restart(ar, reason);
76 static void carl9170_handle_ps(struct ar9170 *ar, struct carl9170_rsp *rsp)
81 ps = le32_to_cpu(rsp->psm.state);
83 new_ps = (ps & CARL9170_PSM_COUNTER) != CARL9170_PSM_WAKE;
84 if (ar->ps.state != new_ps) {
86 ar->ps.sleep_ms = jiffies_to_msecs(jiffies -
90 ar->ps.last_action = jiffies;
92 ar->ps.state = new_ps;
96 static int carl9170_check_sequence(struct ar9170 *ar, unsigned int seq)
108 * The sequence is strictly monotonic increasing and it never skips!
110 * Therefore we can safely assume that whenever we received an
111 * unexpected sequence we have lost some valuable data.
113 if (seq != ar->cmd_seq) {
116 count = (seq - ar->cmd_seq) % ar->fw.cmd_bufs;
118 wiphy_err(ar->hw->wiphy, "lost %d command responses/traps! "
119 "w:%d g:%d\n", count, ar->cmd_seq, seq);
121 carl9170_restart(ar, CARL9170_RR_LOST_RSP);
125 ar->cmd_seq = (ar->cmd_seq + 1) % ar->fw.cmd_bufs;
129 static void carl9170_cmd_callback(struct ar9170 *ar, u32 len, void *buffer)
132 * Some commands may have a variable response length
133 * and we cannot predict the correct length in advance.
134 * So we only check if we provided enough space for the data.
136 if (unlikely(ar->readlen != (len - 4))) {
137 dev_warn(&ar->udev->dev, "received invalid command response:"
138 "got %d, instead of %d\n", len - 4, ar->readlen);
139 print_hex_dump_bytes("carl9170 cmd:", DUMP_PREFIX_OFFSET,
140 ar->cmd_buf, (ar->cmd.hdr.len + 4) & 0x3f);
141 print_hex_dump_bytes("carl9170 rsp:", DUMP_PREFIX_OFFSET,
144 * Do not complete. The command times out,
145 * and we get a stack trace from there.
147 carl9170_restart(ar, CARL9170_RR_INVALID_RSP);
150 spin_lock(&ar->cmd_lock);
153 memcpy(ar->readbuf, buffer + 4, len - 4);
157 complete(&ar->cmd_wait);
158 spin_unlock(&ar->cmd_lock);
161 void carl9170_handle_command_response(struct ar9170 *ar, void *buf, u32 len)
163 struct carl9170_rsp *cmd = buf;
164 struct ieee80211_vif *vif;
166 if ((cmd->hdr.cmd & CARL9170_RSP_FLAG) != CARL9170_RSP_FLAG) {
167 if (!(cmd->hdr.cmd & CARL9170_CMD_ASYNC_FLAG))
168 carl9170_cmd_callback(ar, len, buf);
173 if (unlikely(cmd->hdr.len != (len - 4))) {
174 if (net_ratelimit()) {
175 wiphy_err(ar->hw->wiphy, "FW: received over-/under"
176 "sized event %x (%d, but should be %d).\n",
177 cmd->hdr.cmd, cmd->hdr.len, len - 4);
179 print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE,
186 /* hardware event handlers */
187 switch (cmd->hdr.cmd) {
188 case CARL9170_RSP_PRETBTT:
191 vif = carl9170_get_main_vif(ar);
199 case NL80211_IFTYPE_STATION:
200 carl9170_handle_ps(ar, cmd);
203 case NL80211_IFTYPE_AP:
204 case NL80211_IFTYPE_ADHOC:
205 case NL80211_IFTYPE_MESH_POINT:
206 carl9170_update_beacon(ar, true);
217 case CARL9170_RSP_TXCOMP:
218 /* TX status notification */
219 carl9170_tx_process_status(ar, cmd);
222 case CARL9170_RSP_BEACON_CONFIG:
224 * (IBSS) beacon send notification
225 * bytes: 04 c2 XX YY B4 B3 B2 B1
229 * B1-B4 "should" be the number of send out beacons.
233 case CARL9170_RSP_ATIM:
234 /* End of Atim Window */
237 case CARL9170_RSP_WATCHDOG:
238 /* Watchdog Interrupt */
239 carl9170_restart(ar, CARL9170_RR_WATCHDOG);
242 case CARL9170_RSP_TEXT:
244 carl9170_dbg_message(ar, (char *)buf + 4, len - 4);
247 case CARL9170_RSP_HEXDUMP:
248 wiphy_dbg(ar->hw->wiphy, "FW: HD %d\n", len - 4);
249 print_hex_dump_bytes("FW:", DUMP_PREFIX_NONE,
250 (char *)buf + 4, len - 4);
253 case CARL9170_RSP_RADAR:
254 if (!net_ratelimit())
257 wiphy_info(ar->hw->wiphy, "FW: RADAR! Please report this "
258 "incident to linux-wireless@vger.kernel.org !\n");
261 case CARL9170_RSP_GPIO:
262 #ifdef CONFIG_CARL9170_WPC
264 bool state = !!(cmd->gpio.gpio & cpu_to_le32(
265 AR9170_GPIO_PORT_WPS_BUTTON_PRESSED));
267 if (state != ar->wps.pbc_state) {
268 ar->wps.pbc_state = state;
269 input_report_key(ar->wps.pbc, KEY_WPS_BUTTON,
271 input_sync(ar->wps.pbc);
274 #endif /* CONFIG_CARL9170_WPC */
277 case CARL9170_RSP_BOOT:
278 complete(&ar->fw_boot_wait);
282 wiphy_err(ar->hw->wiphy, "FW: received unhandled event %x\n",
284 print_hex_dump_bytes("dump:", DUMP_PREFIX_NONE, buf, len);
289 static int carl9170_rx_mac_status(struct ar9170 *ar,
290 struct ar9170_rx_head *head, struct ar9170_rx_macstatus *mac,
291 struct ieee80211_rx_status *status)
293 struct ieee80211_channel *chan;
296 BUILD_BUG_ON(sizeof(struct ar9170_rx_head) != 12);
297 BUILD_BUG_ON(sizeof(struct ar9170_rx_macstatus) != 4);
301 if (error & AR9170_RX_ERROR_WRONG_RA) {
302 if (!ar->sniffer_enabled)
306 if (error & AR9170_RX_ERROR_PLCP) {
307 if (!(ar->filter_state & FIF_PLCPFAIL))
310 status->flag |= RX_FLAG_FAILED_PLCP_CRC;
313 if (error & AR9170_RX_ERROR_FCS) {
316 if (!(ar->filter_state & FIF_FCSFAIL))
319 status->flag |= RX_FLAG_FAILED_FCS_CRC;
322 decrypt = ar9170_get_decrypt_type(mac);
323 if (!(decrypt & AR9170_RX_ENC_SOFTWARE) &&
324 decrypt != AR9170_ENC_ALG_NONE) {
325 if ((decrypt == AR9170_ENC_ALG_TKIP) &&
326 (error & AR9170_RX_ERROR_MMIC))
327 status->flag |= RX_FLAG_MMIC_ERROR;
329 status->flag |= RX_FLAG_DECRYPTED;
332 if (error & AR9170_RX_ERROR_DECRYPT && !ar->sniffer_enabled)
335 error &= ~(AR9170_RX_ERROR_MMIC |
336 AR9170_RX_ERROR_FCS |
337 AR9170_RX_ERROR_WRONG_RA |
338 AR9170_RX_ERROR_DECRYPT |
339 AR9170_RX_ERROR_PLCP);
341 /* drop any other error frames */
342 if (unlikely(error)) {
343 /* TODO: update netdevice's RX dropped/errors statistics */
346 wiphy_dbg(ar->hw->wiphy, "received frame with "
347 "suspicious error code (%#x).\n", error);
354 status->band = chan->band;
355 status->freq = chan->center_freq;
358 switch (mac->status & AR9170_RX_STATUS_MODULATION) {
359 case AR9170_RX_STATUS_MODULATION_CCK:
360 if (mac->status & AR9170_RX_STATUS_SHORT_PREAMBLE)
361 status->flag |= RX_FLAG_SHORTPRE;
362 switch (head->plcp[0]) {
363 case AR9170_RX_PHY_RATE_CCK_1M:
364 status->rate_idx = 0;
366 case AR9170_RX_PHY_RATE_CCK_2M:
367 status->rate_idx = 1;
369 case AR9170_RX_PHY_RATE_CCK_5M:
370 status->rate_idx = 2;
372 case AR9170_RX_PHY_RATE_CCK_11M:
373 status->rate_idx = 3;
376 if (net_ratelimit()) {
377 wiphy_err(ar->hw->wiphy, "invalid plcp cck "
378 "rate (%x).\n", head->plcp[0]);
385 case AR9170_RX_STATUS_MODULATION_DUPOFDM:
386 case AR9170_RX_STATUS_MODULATION_OFDM:
387 switch (head->plcp[0] & 0xf) {
388 case AR9170_TXRX_PHY_RATE_OFDM_6M:
389 status->rate_idx = 0;
391 case AR9170_TXRX_PHY_RATE_OFDM_9M:
392 status->rate_idx = 1;
394 case AR9170_TXRX_PHY_RATE_OFDM_12M:
395 status->rate_idx = 2;
397 case AR9170_TXRX_PHY_RATE_OFDM_18M:
398 status->rate_idx = 3;
400 case AR9170_TXRX_PHY_RATE_OFDM_24M:
401 status->rate_idx = 4;
403 case AR9170_TXRX_PHY_RATE_OFDM_36M:
404 status->rate_idx = 5;
406 case AR9170_TXRX_PHY_RATE_OFDM_48M:
407 status->rate_idx = 6;
409 case AR9170_TXRX_PHY_RATE_OFDM_54M:
410 status->rate_idx = 7;
413 if (net_ratelimit()) {
414 wiphy_err(ar->hw->wiphy, "invalid plcp ofdm "
415 "rate (%x).\n", head->plcp[0]);
420 if (status->band == IEEE80211_BAND_2GHZ)
421 status->rate_idx += 4;
424 case AR9170_RX_STATUS_MODULATION_HT:
425 if (head->plcp[3] & 0x80)
426 status->flag |= RX_FLAG_40MHZ;
427 if (head->plcp[6] & 0x80)
428 status->flag |= RX_FLAG_SHORT_GI;
430 status->rate_idx = clamp(0, 75, head->plcp[3] & 0x7f);
431 status->flag |= RX_FLAG_HT;
442 static void carl9170_rx_phy_status(struct ar9170 *ar,
443 struct ar9170_rx_phystatus *phy, struct ieee80211_rx_status *status)
447 BUILD_BUG_ON(sizeof(struct ar9170_rx_phystatus) != 20);
449 for (i = 0; i < 3; i++)
450 if (phy->rssi[i] != 0x80)
451 status->antenna |= BIT(i);
453 /* post-process RSSI */
454 for (i = 0; i < 7; i++)
455 if (phy->rssi[i] & 0x80)
456 phy->rssi[i] = ((phy->rssi[i] & 0x7f) + 1) & 0x7f;
458 /* TODO: we could do something with phy_errors */
459 status->signal = ar->noise[0] + phy->rssi_combined;
462 static struct sk_buff *carl9170_rx_copy_data(u8 *buf, int len)
466 struct ieee80211_hdr *hdr = (void *) buf;
468 if (ieee80211_is_data_qos(hdr->frame_control)) {
469 u8 *qc = ieee80211_get_qos_ctl(hdr);
470 reserved += NET_IP_ALIGN;
472 if (*qc & IEEE80211_QOS_CTL_A_MSDU_PRESENT)
473 reserved += NET_IP_ALIGN;
476 if (ieee80211_has_a4(hdr->frame_control))
477 reserved += NET_IP_ALIGN;
479 reserved = 32 + (reserved & NET_IP_ALIGN);
481 skb = dev_alloc_skb(len + reserved);
483 skb_reserve(skb, reserved);
484 memcpy(skb_put(skb, len), buf, len);
490 static u8 *carl9170_find_ie(u8 *data, unsigned int len, u8 ie)
492 struct ieee80211_mgmt *mgmt = (void *)data;
495 pos = (u8 *)mgmt->u.beacon.variable;
498 if (pos + 2 + pos[1] > end)
512 * The firmware is in charge of waking up the device just before
513 * the AP is expected to transmit the next beacon.
515 * This leaves the driver with the important task of deciding when
516 * to set the PHY back to bed again.
518 static void carl9170_ps_beacon(struct ar9170 *ar, void *data, unsigned int len)
520 struct ieee80211_hdr *hdr = data;
521 struct ieee80211_tim_ie *tim_ie;
526 if (likely(!(ar->hw->conf.flags & IEEE80211_CONF_PS)))
529 /* check if this really is a beacon */
530 if (!ieee80211_is_beacon(hdr->frame_control))
533 /* min. beacon length + FCS_LEN */
534 if (len <= 40 + FCS_LEN)
537 /* and only beacons from the associated BSSID, please */
538 if (!ether_addr_equal_64bits(hdr->addr3, ar->common.curbssid) ||
542 ar->ps.last_beacon = jiffies;
544 tim = carl9170_find_ie(data, len - FCS_LEN, WLAN_EID_TIM);
548 if (tim[1] < sizeof(*tim_ie))
552 tim_ie = (struct ieee80211_tim_ie *) &tim[2];
554 if (!WARN_ON_ONCE(!ar->hw->conf.ps_dtim_period))
555 ar->ps.dtim_counter = (tim_ie->dtim_count - 1) %
556 ar->hw->conf.ps_dtim_period;
558 /* Check whenever the PHY can be turned off again. */
560 /* 1. What about buffered unicast traffic for our AID? */
561 cam = ieee80211_check_tim(tim_ie, tim_len, ar->common.curaid);
563 /* 2. Maybe the AP wants to send multicast/broadcast data? */
564 cam |= !!(tim_ie->bitmap_ctrl & 0x01);
567 /* back to low-power land. */
568 ar->ps.off_override &= ~PS_OFF_BCN;
569 carl9170_ps_check(ar);
572 ar->ps.off_override |= PS_OFF_BCN;
576 static void carl9170_ba_check(struct ar9170 *ar, void *data, unsigned int len)
578 struct ieee80211_bar *bar = (void *) data;
579 struct carl9170_bar_list_entry *entry;
582 if (likely(!ieee80211_is_back(bar->frame_control)))
585 if (len <= sizeof(*bar) + FCS_LEN)
588 queue = TID_TO_WME_AC(((le16_to_cpu(bar->control) &
589 IEEE80211_BAR_CTRL_TID_INFO_MASK) >>
590 IEEE80211_BAR_CTRL_TID_INFO_SHIFT) & 7);
593 list_for_each_entry_rcu(entry, &ar->bar_list[queue], list) {
594 struct sk_buff *entry_skb = entry->skb;
595 struct _carl9170_tx_superframe *super = (void *)entry_skb->data;
596 struct ieee80211_bar *entry_bar = (void *)super->frame_data;
598 #define TID_CHECK(a, b) ( \
599 ((a) & cpu_to_le16(IEEE80211_BAR_CTRL_TID_INFO_MASK)) == \
600 ((b) & cpu_to_le16(IEEE80211_BAR_CTRL_TID_INFO_MASK))) \
602 if (bar->start_seq_num == entry_bar->start_seq_num &&
603 TID_CHECK(bar->control, entry_bar->control) &&
604 ether_addr_equal_64bits(bar->ra, entry_bar->ta) &&
605 ether_addr_equal_64bits(bar->ta, entry_bar->ra)) {
606 struct ieee80211_tx_info *tx_info;
608 tx_info = IEEE80211_SKB_CB(entry_skb);
609 tx_info->flags |= IEEE80211_TX_STAT_ACK;
611 spin_lock_bh(&ar->bar_list_lock[queue]);
612 list_del_rcu(&entry->list);
613 spin_unlock_bh(&ar->bar_list_lock[queue]);
614 kfree_rcu(entry, head);
623 static bool carl9170_ampdu_check(struct ar9170 *ar, u8 *buf, u8 ms,
624 struct ieee80211_rx_status *rx_status)
628 if ((ms & AR9170_RX_STATUS_MPDU) == AR9170_RX_STATUS_MPDU_SINGLE) {
630 * This frame is not part of an aMPDU.
631 * Therefore it is not subjected to any
632 * of the following content restrictions.
637 rx_status->flag |= RX_FLAG_AMPDU_DETAILS | RX_FLAG_AMPDU_LAST_KNOWN;
638 rx_status->ampdu_reference = ar->ampdu_ref;
641 * "802.11n - 7.4a.3 A-MPDU contents" describes in which contexts
642 * certain frame types can be part of an aMPDU.
644 * In order to keep the processing cost down, I opted for a
645 * stateless filter solely based on the frame control field.
648 fc = ((struct ieee80211_hdr *)buf)->frame_control;
649 if (ieee80211_is_data_qos(fc) && ieee80211_is_data_present(fc))
652 if (ieee80211_is_ack(fc) || ieee80211_is_back(fc) ||
653 ieee80211_is_back_req(fc))
656 if (ieee80211_is_action(fc))
662 static int carl9170_handle_mpdu(struct ar9170 *ar, u8 *buf, int len,
663 struct ieee80211_rx_status *status)
667 /* (driver) frame trap handler
669 * Because power-saving mode handing has to be implemented by
670 * the driver/firmware. We have to check each incoming beacon
671 * from the associated AP, if there's new data for us (either
672 * broadcast/multicast or unicast) we have to react quickly.
674 * So, if you have you want to add additional frame trap
675 * handlers, this would be the perfect place!
678 carl9170_ps_beacon(ar, buf, len);
680 carl9170_ba_check(ar, buf, len);
682 skb = carl9170_rx_copy_data(buf, len);
686 memcpy(IEEE80211_SKB_RXCB(skb), status, sizeof(*status));
687 ieee80211_rx(ar->hw, skb);
692 * If the frame alignment is right (or the kernel has
693 * CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS), and there
694 * is only a single MPDU in the USB frame, then we could
695 * submit to mac80211 the SKB directly. However, since
696 * there may be multiple packets in one SKB in stream
697 * mode, and we need to observe the proper ordering,
698 * this is non-trivial.
700 static void carl9170_rx_untie_data(struct ar9170 *ar, u8 *buf, int len)
702 struct ar9170_rx_head *head;
703 struct ar9170_rx_macstatus *mac;
704 struct ar9170_rx_phystatus *phy = NULL;
705 struct ieee80211_rx_status status;
712 if (unlikely(len < sizeof(*mac)))
715 memset(&status, 0, sizeof(status));
717 mpdu_len = len - sizeof(*mac);
719 mac = (void *)(buf + mpdu_len);
720 mac_status = mac->status;
721 switch (mac_status & AR9170_RX_STATUS_MPDU) {
722 case AR9170_RX_STATUS_MPDU_FIRST:
724 /* Aggregated MPDUs start with an PLCP header */
725 if (likely(mpdu_len >= sizeof(struct ar9170_rx_head))) {
729 * The PLCP header needs to be cached for the
730 * following MIDDLE + LAST A-MPDU packets.
732 * So, if you are wondering why all frames seem
733 * to share a common RX status information,
734 * then you have the answer right here...
736 memcpy(&ar->rx_plcp, (void *) buf,
737 sizeof(struct ar9170_rx_head));
739 mpdu_len -= sizeof(struct ar9170_rx_head);
740 buf += sizeof(struct ar9170_rx_head);
742 ar->rx_has_plcp = true;
744 if (net_ratelimit()) {
745 wiphy_err(ar->hw->wiphy, "plcp info "
753 case AR9170_RX_STATUS_MPDU_LAST:
754 status.flag |= RX_FLAG_AMPDU_IS_LAST;
757 * The last frame of an A-MPDU has an extra tail
758 * which does contain the phy status of the whole
761 if (likely(mpdu_len >= sizeof(struct ar9170_rx_phystatus))) {
762 mpdu_len -= sizeof(struct ar9170_rx_phystatus);
763 phy = (void *)(buf + mpdu_len);
765 if (net_ratelimit()) {
766 wiphy_err(ar->hw->wiphy, "frame tail "
773 case AR9170_RX_STATUS_MPDU_MIDDLE:
774 /* These are just data + mac status */
775 if (unlikely(!ar->rx_has_plcp)) {
776 if (!net_ratelimit())
779 wiphy_err(ar->hw->wiphy, "rx stream does not start "
780 "with a first_mpdu frame tag.\n");
788 case AR9170_RX_STATUS_MPDU_SINGLE:
789 /* single mpdu has both: plcp (head) and phy status (tail) */
792 mpdu_len -= sizeof(struct ar9170_rx_head);
793 mpdu_len -= sizeof(struct ar9170_rx_phystatus);
795 buf += sizeof(struct ar9170_rx_head);
796 phy = (void *)(buf + mpdu_len);
804 /* FC + DU + RA + FCS */
805 if (unlikely(mpdu_len < (2 + 2 + ETH_ALEN + FCS_LEN)))
808 if (unlikely(carl9170_rx_mac_status(ar, head, mac, &status)))
811 if (!carl9170_ampdu_check(ar, buf, mac_status, &status))
815 carl9170_rx_phy_status(ar, phy, &status);
817 status.flag |= RX_FLAG_NO_SIGNAL_VAL;
819 if (carl9170_handle_mpdu(ar, buf, mpdu_len, &status))
827 static void carl9170_rx_untie_cmds(struct ar9170 *ar, const u8 *respbuf,
828 const unsigned int resplen)
830 struct carl9170_rsp *cmd;
833 while (i < resplen) {
834 cmd = (void *) &respbuf[i];
836 i += cmd->hdr.len + 4;
837 if (unlikely(i > resplen))
840 if (carl9170_check_sequence(ar, cmd->hdr.seq))
843 carl9170_handle_command_response(ar, cmd, cmd->hdr.len + 4);
846 if (unlikely(i != resplen)) {
847 if (!net_ratelimit())
850 wiphy_err(ar->hw->wiphy, "malformed firmware trap:\n");
851 print_hex_dump_bytes("rxcmd:", DUMP_PREFIX_OFFSET,
856 static void __carl9170_rx(struct ar9170 *ar, u8 *buf, unsigned int len)
860 /* weird thing, but this is the same in the original driver */
861 while (len > 2 && i < 12 && buf[0] == 0xff && buf[1] == 0xff) {
867 if (unlikely(len < 4))
870 /* found the 6 * 0xffff marker? */
872 carl9170_rx_untie_cmds(ar, buf, len);
874 carl9170_rx_untie_data(ar, buf, len);
877 static void carl9170_rx_stream(struct ar9170 *ar, void *buf, unsigned int len)
879 unsigned int tlen, wlen = 0, clen = 0;
880 struct ar9170_stream *rx_stream;
887 rx_stream = (void *) tbuf;
888 clen = le16_to_cpu(rx_stream->length);
889 wlen = ALIGN(clen, 4);
891 /* check if this is stream has a valid tag.*/
892 if (rx_stream->tag != cpu_to_le16(AR9170_RX_STREAM_TAG)) {
894 * TODO: handle the highly unlikely event that the
895 * corrupted stream has the TAG at the right position.
898 /* check if the frame can be repaired. */
899 if (!ar->rx_failover_missing) {
901 /* this is not "short read". */
902 if (net_ratelimit()) {
903 wiphy_err(ar->hw->wiphy,
907 __carl9170_rx(ar, tbuf, tlen);
911 if (ar->rx_failover_missing > tlen) {
912 if (net_ratelimit()) {
913 wiphy_err(ar->hw->wiphy,
915 "stream corruption!\n");
922 memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
923 ar->rx_failover_missing -= tlen;
925 if (ar->rx_failover_missing <= 0) {
927 * nested carl9170_rx_stream call!
929 * termination is guaranteed, even when the
930 * combined frame also have an element with
934 ar->rx_failover_missing = 0;
935 carl9170_rx_stream(ar, ar->rx_failover->data,
936 ar->rx_failover->len);
938 skb_reset_tail_pointer(ar->rx_failover);
939 skb_trim(ar->rx_failover, 0);
945 /* check if stream is clipped */
946 if (wlen > tlen - 4) {
947 if (ar->rx_failover_missing) {
948 /* TODO: handle double stream corruption. */
949 if (net_ratelimit()) {
950 wiphy_err(ar->hw->wiphy, "double rx "
951 "stream corruption!\n");
959 * save incomplete data set.
960 * the firmware will resend the missing bits when
961 * the rx - descriptor comes round again.
964 memcpy(skb_put(ar->rx_failover, tlen), tbuf, tlen);
965 ar->rx_failover_missing = clen - tlen;
968 __carl9170_rx(ar, rx_stream->payload, clen);
975 if (net_ratelimit()) {
976 wiphy_err(ar->hw->wiphy, "%d bytes of unprocessed "
977 "data left in rx stream!\n", tlen);
986 wiphy_err(ar->hw->wiphy, "damaged RX stream data [want:%d, "
987 "data:%d, rx:%d, pending:%d ]\n", clen, wlen, tlen,
988 ar->rx_failover_missing);
990 if (ar->rx_failover_missing)
991 print_hex_dump_bytes("rxbuf:", DUMP_PREFIX_OFFSET,
992 ar->rx_failover->data,
993 ar->rx_failover->len);
995 print_hex_dump_bytes("stream:", DUMP_PREFIX_OFFSET,
998 wiphy_err(ar->hw->wiphy, "please check your hardware and cables, if "
999 "you see this message frequently.\n");
1002 if (ar->rx_failover_missing) {
1003 skb_reset_tail_pointer(ar->rx_failover);
1004 skb_trim(ar->rx_failover, 0);
1005 ar->rx_failover_missing = 0;
1009 void carl9170_rx(struct ar9170 *ar, void *buf, unsigned int len)
1011 if (ar->fw.rx_stream)
1012 carl9170_rx_stream(ar, buf, len);
1014 __carl9170_rx(ar, buf, len);