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