1*2b27bdccSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
290921014SLuciano Coelho /*
390921014SLuciano Coelho * This file is part of wl1251
490921014SLuciano Coelho *
590921014SLuciano Coelho * Copyright (c) 1998-2007 Texas Instruments Incorporated
690921014SLuciano Coelho * Copyright (C) 2008 Nokia Corporation
790921014SLuciano Coelho */
890921014SLuciano Coelho
990921014SLuciano Coelho #include <linux/skbuff.h>
1090921014SLuciano Coelho #include <linux/gfp.h>
1190921014SLuciano Coelho #include <net/mac80211.h>
1290921014SLuciano Coelho
1390921014SLuciano Coelho #include "wl1251.h"
1490921014SLuciano Coelho #include "reg.h"
1590921014SLuciano Coelho #include "io.h"
1690921014SLuciano Coelho #include "rx.h"
1790921014SLuciano Coelho #include "cmd.h"
1890921014SLuciano Coelho #include "acx.h"
1990921014SLuciano Coelho
wl1251_rx_header(struct wl1251 * wl,struct wl1251_rx_descriptor * desc)2090921014SLuciano Coelho static void wl1251_rx_header(struct wl1251 *wl,
2190921014SLuciano Coelho struct wl1251_rx_descriptor *desc)
2290921014SLuciano Coelho {
2390921014SLuciano Coelho u32 rx_packet_ring_addr;
2490921014SLuciano Coelho
2590921014SLuciano Coelho rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr;
2690921014SLuciano Coelho if (wl->rx_current_buffer)
2790921014SLuciano Coelho rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
2890921014SLuciano Coelho
2990921014SLuciano Coelho wl1251_mem_read(wl, rx_packet_ring_addr, desc, sizeof(*desc));
3090921014SLuciano Coelho }
3190921014SLuciano Coelho
wl1251_rx_status(struct wl1251 * wl,struct wl1251_rx_descriptor * desc,struct ieee80211_rx_status * status,u8 beacon)3290921014SLuciano Coelho static void wl1251_rx_status(struct wl1251 *wl,
3390921014SLuciano Coelho struct wl1251_rx_descriptor *desc,
3490921014SLuciano Coelho struct ieee80211_rx_status *status,
3590921014SLuciano Coelho u8 beacon)
3690921014SLuciano Coelho {
3790921014SLuciano Coelho u64 mactime;
3890921014SLuciano Coelho int ret;
3990921014SLuciano Coelho
4090921014SLuciano Coelho memset(status, 0, sizeof(struct ieee80211_rx_status));
4190921014SLuciano Coelho
4257fbcce3SJohannes Berg status->band = NL80211_BAND_2GHZ;
4390921014SLuciano Coelho status->mactime = desc->timestamp;
4490921014SLuciano Coelho
4590921014SLuciano Coelho /*
4690921014SLuciano Coelho * The rx status timestamp is a 32 bits value while the TSF is a
4790921014SLuciano Coelho * 64 bits one.
4890921014SLuciano Coelho * For IBSS merging, TSF is mandatory, so we have to get it
4990921014SLuciano Coelho * somehow, so we ask for ACX_TSF_INFO.
5090921014SLuciano Coelho * That could be moved to the get_tsf() hook, but unfortunately,
5190921014SLuciano Coelho * this one must be atomic, while our SPI routines can sleep.
5290921014SLuciano Coelho */
5390921014SLuciano Coelho if ((wl->bss_type == BSS_TYPE_IBSS) && beacon) {
5490921014SLuciano Coelho ret = wl1251_acx_tsf_info(wl, &mactime);
5590921014SLuciano Coelho if (ret == 0)
5690921014SLuciano Coelho status->mactime = mactime;
5790921014SLuciano Coelho }
5890921014SLuciano Coelho
5990921014SLuciano Coelho status->signal = desc->rssi;
6090921014SLuciano Coelho
6190921014SLuciano Coelho /*
6290921014SLuciano Coelho * FIXME: guessing that snr needs to be divided by two, otherwise
6390921014SLuciano Coelho * the values don't make any sense
6490921014SLuciano Coelho */
6590921014SLuciano Coelho wl->noise = desc->rssi - desc->snr / 2;
6690921014SLuciano Coelho
6790921014SLuciano Coelho status->freq = ieee80211_channel_to_frequency(desc->channel,
6890921014SLuciano Coelho status->band);
6990921014SLuciano Coelho
70f4bda337SThomas Pedersen status->flag |= RX_FLAG_MACTIME_START;
7190921014SLuciano Coelho
724d09b537SDavid Gnedt if (!wl->monitor_present && (desc->flags & RX_DESC_ENCRYPTION_MASK)) {
7390921014SLuciano Coelho status->flag |= RX_FLAG_IV_STRIPPED | RX_FLAG_MMIC_STRIPPED;
7490921014SLuciano Coelho
7590921014SLuciano Coelho if (likely(!(desc->flags & RX_DESC_DECRYPT_FAIL)))
7690921014SLuciano Coelho status->flag |= RX_FLAG_DECRYPTED;
7790921014SLuciano Coelho
7890921014SLuciano Coelho if (unlikely(desc->flags & RX_DESC_MIC_FAIL))
7990921014SLuciano Coelho status->flag |= RX_FLAG_MMIC_ERROR;
8090921014SLuciano Coelho }
8190921014SLuciano Coelho
8290921014SLuciano Coelho if (unlikely(!(desc->flags & RX_DESC_VALID_FCS)))
8390921014SLuciano Coelho status->flag |= RX_FLAG_FAILED_FCS_CRC;
8490921014SLuciano Coelho
8590921014SLuciano Coelho switch (desc->rate) {
8690921014SLuciano Coelho /* skip 1 and 12 Mbps because they have same value 0x0a */
8790921014SLuciano Coelho case RATE_2MBPS:
8890921014SLuciano Coelho status->rate_idx = 1;
8990921014SLuciano Coelho break;
9090921014SLuciano Coelho case RATE_5_5MBPS:
9190921014SLuciano Coelho status->rate_idx = 2;
9290921014SLuciano Coelho break;
9390921014SLuciano Coelho case RATE_11MBPS:
9490921014SLuciano Coelho status->rate_idx = 3;
9590921014SLuciano Coelho break;
9690921014SLuciano Coelho case RATE_6MBPS:
9790921014SLuciano Coelho status->rate_idx = 4;
9890921014SLuciano Coelho break;
9990921014SLuciano Coelho case RATE_9MBPS:
10090921014SLuciano Coelho status->rate_idx = 5;
10190921014SLuciano Coelho break;
10290921014SLuciano Coelho case RATE_18MBPS:
10390921014SLuciano Coelho status->rate_idx = 7;
10490921014SLuciano Coelho break;
10590921014SLuciano Coelho case RATE_24MBPS:
10690921014SLuciano Coelho status->rate_idx = 8;
10790921014SLuciano Coelho break;
10890921014SLuciano Coelho case RATE_36MBPS:
10990921014SLuciano Coelho status->rate_idx = 9;
11090921014SLuciano Coelho break;
11190921014SLuciano Coelho case RATE_48MBPS:
11290921014SLuciano Coelho status->rate_idx = 10;
11390921014SLuciano Coelho break;
11490921014SLuciano Coelho case RATE_54MBPS:
11590921014SLuciano Coelho status->rate_idx = 11;
11690921014SLuciano Coelho break;
11790921014SLuciano Coelho }
11890921014SLuciano Coelho
11990921014SLuciano Coelho /* for 1 and 12 Mbps we have to check the modulation */
12090921014SLuciano Coelho if (desc->rate == RATE_1MBPS) {
12190921014SLuciano Coelho if (!(desc->mod_pre & OFDM_RATE_BIT))
12290921014SLuciano Coelho /* CCK -> RATE_1MBPS */
12390921014SLuciano Coelho status->rate_idx = 0;
12490921014SLuciano Coelho else
12590921014SLuciano Coelho /* OFDM -> RATE_12MBPS */
12690921014SLuciano Coelho status->rate_idx = 6;
12790921014SLuciano Coelho }
12890921014SLuciano Coelho
12990921014SLuciano Coelho if (desc->mod_pre & SHORT_PREAMBLE_BIT)
1307fdd69c5SJohannes Berg status->enc_flags |= RX_ENC_FLAG_SHORTPRE;
13190921014SLuciano Coelho }
13290921014SLuciano Coelho
wl1251_rx_body(struct wl1251 * wl,struct wl1251_rx_descriptor * desc)13390921014SLuciano Coelho static void wl1251_rx_body(struct wl1251 *wl,
13490921014SLuciano Coelho struct wl1251_rx_descriptor *desc)
13590921014SLuciano Coelho {
13690921014SLuciano Coelho struct sk_buff *skb;
13790921014SLuciano Coelho struct ieee80211_rx_status status;
13890921014SLuciano Coelho u8 *rx_buffer, beacon = 0;
13990921014SLuciano Coelho u16 length, *fc;
14090921014SLuciano Coelho u32 curr_id, last_id_inc, rx_packet_ring_addr;
14190921014SLuciano Coelho
14290921014SLuciano Coelho length = WL1251_RX_ALIGN(desc->length - PLCP_HEADER_LENGTH);
14390921014SLuciano Coelho curr_id = (desc->flags & RX_DESC_SEQNUM_MASK) >> RX_DESC_PACKETID_SHIFT;
14490921014SLuciano Coelho last_id_inc = (wl->rx_last_id + 1) % (RX_MAX_PACKET_ID + 1);
14590921014SLuciano Coelho
14690921014SLuciano Coelho if (last_id_inc != curr_id) {
14790921014SLuciano Coelho wl1251_warning("curr ID:%d, last ID inc:%d",
14890921014SLuciano Coelho curr_id, last_id_inc);
14990921014SLuciano Coelho wl->rx_last_id = curr_id;
15090921014SLuciano Coelho } else {
15190921014SLuciano Coelho wl->rx_last_id = last_id_inc;
15290921014SLuciano Coelho }
15390921014SLuciano Coelho
15490921014SLuciano Coelho rx_packet_ring_addr = wl->data_path->rx_packet_ring_addr +
15590921014SLuciano Coelho sizeof(struct wl1251_rx_descriptor) + 20;
15690921014SLuciano Coelho if (wl->rx_current_buffer)
15790921014SLuciano Coelho rx_packet_ring_addr += wl->data_path->rx_packet_ring_chunk_size;
15890921014SLuciano Coelho
15990921014SLuciano Coelho skb = __dev_alloc_skb(length, GFP_KERNEL);
16090921014SLuciano Coelho if (!skb) {
16190921014SLuciano Coelho wl1251_error("Couldn't allocate RX frame");
16290921014SLuciano Coelho return;
16390921014SLuciano Coelho }
16490921014SLuciano Coelho
16590921014SLuciano Coelho rx_buffer = skb_put(skb, length);
16690921014SLuciano Coelho wl1251_mem_read(wl, rx_packet_ring_addr, rx_buffer, length);
16790921014SLuciano Coelho
16890921014SLuciano Coelho /* The actual length doesn't include the target's alignment */
169b6251243SIvaylo Dimitrov skb_trim(skb, desc->length - PLCP_HEADER_LENGTH);
17090921014SLuciano Coelho
17190921014SLuciano Coelho fc = (u16 *)skb->data;
17290921014SLuciano Coelho
17390921014SLuciano Coelho if ((*fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_BEACON)
17490921014SLuciano Coelho beacon = 1;
17590921014SLuciano Coelho
17690921014SLuciano Coelho wl1251_rx_status(wl, desc, &status, beacon);
17790921014SLuciano Coelho
17890921014SLuciano Coelho wl1251_debug(DEBUG_RX, "rx skb 0x%p: %d B %s", skb, skb->len,
17990921014SLuciano Coelho beacon ? "beacon" : "");
18090921014SLuciano Coelho
18190921014SLuciano Coelho memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
18290921014SLuciano Coelho ieee80211_rx_ni(wl->hw, skb);
18390921014SLuciano Coelho }
18490921014SLuciano Coelho
wl1251_rx_ack(struct wl1251 * wl)18590921014SLuciano Coelho static void wl1251_rx_ack(struct wl1251 *wl)
18690921014SLuciano Coelho {
18790921014SLuciano Coelho u32 data, addr;
18890921014SLuciano Coelho
18990921014SLuciano Coelho if (wl->rx_current_buffer) {
19090921014SLuciano Coelho addr = ACX_REG_INTERRUPT_TRIG_H;
19190921014SLuciano Coelho data = INTR_TRIG_RX_PROC1;
19290921014SLuciano Coelho } else {
19390921014SLuciano Coelho addr = ACX_REG_INTERRUPT_TRIG;
19490921014SLuciano Coelho data = INTR_TRIG_RX_PROC0;
19590921014SLuciano Coelho }
19690921014SLuciano Coelho
19790921014SLuciano Coelho wl1251_reg_write32(wl, addr, data);
19890921014SLuciano Coelho
19990921014SLuciano Coelho /* Toggle buffer ring */
20090921014SLuciano Coelho wl->rx_current_buffer = !wl->rx_current_buffer;
20190921014SLuciano Coelho }
20290921014SLuciano Coelho
20390921014SLuciano Coelho
wl1251_rx(struct wl1251 * wl)20490921014SLuciano Coelho void wl1251_rx(struct wl1251 *wl)
20590921014SLuciano Coelho {
20690921014SLuciano Coelho struct wl1251_rx_descriptor *rx_desc;
20790921014SLuciano Coelho
20890921014SLuciano Coelho if (wl->state != WL1251_STATE_ON)
20990921014SLuciano Coelho return;
21090921014SLuciano Coelho
21190921014SLuciano Coelho rx_desc = wl->rx_descriptor;
21290921014SLuciano Coelho
21390921014SLuciano Coelho /* We first read the frame's header */
21490921014SLuciano Coelho wl1251_rx_header(wl, rx_desc);
21590921014SLuciano Coelho
21690921014SLuciano Coelho /* Now we can read the body */
21790921014SLuciano Coelho wl1251_rx_body(wl, rx_desc);
21890921014SLuciano Coelho
21990921014SLuciano Coelho /* Finally, we need to ACK the RX */
22090921014SLuciano Coelho wl1251_rx_ack(wl);
22190921014SLuciano Coelho }
222