2 * Copyright (c) 2014-2017 Qualcomm Atheros, Inc.
3 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #define SEQ_MODULO 0x1000
22 #define SEQ_MASK 0xfff
24 static inline int seq_less(u16 sq1, u16 sq2)
26 return ((sq1 - sq2) & SEQ_MASK) > (SEQ_MODULO >> 1);
29 static inline u16 seq_inc(u16 sq)
31 return (sq + 1) & SEQ_MASK;
34 static inline u16 seq_sub(u16 sq1, u16 sq2)
36 return (sq1 - sq2) & SEQ_MASK;
39 static inline int reorder_index(struct wil_tid_ampdu_rx *r, u16 seq)
41 return seq_sub(seq, r->ssn) % r->buf_size;
44 static void wil_release_reorder_frame(struct net_device *ndev,
45 struct wil_tid_ampdu_rx *r,
48 struct sk_buff *skb = r->reorder_buf[index];
53 /* release the frame from the reorder ring buffer */
55 r->reorder_buf[index] = NULL;
56 wil_netif_rx_any(skb, ndev);
59 r->head_seq_num = seq_inc(r->head_seq_num);
62 static void wil_release_reorder_frames(struct net_device *ndev,
63 struct wil_tid_ampdu_rx *r,
68 /* note: this function is never called with
69 * hseq preceding r->head_seq_num, i.e it is always true
70 * !seq_less(hseq, r->head_seq_num)
71 * and thus on loop exit it should be
72 * r->head_seq_num == hseq
74 while (seq_less(r->head_seq_num, hseq) && r->stored_mpdu_num) {
75 index = reorder_index(r, r->head_seq_num);
76 wil_release_reorder_frame(ndev, r, index);
78 r->head_seq_num = hseq;
81 static void wil_reorder_release(struct net_device *ndev,
82 struct wil_tid_ampdu_rx *r)
84 int index = reorder_index(r, r->head_seq_num);
86 while (r->reorder_buf[index]) {
87 wil_release_reorder_frame(ndev, r, index);
88 index = reorder_index(r, r->head_seq_num);
92 /* called in NAPI context */
93 void wil_rx_reorder(struct wil6210_priv *wil, struct sk_buff *skb)
94 __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
96 struct wil6210_vif *vif;
97 struct net_device *ndev;
98 int tid, cid, mid, mcast, retry;
100 struct wil_sta_info *sta;
101 struct wil_tid_ampdu_rx *r;
105 wil->txrx_ops.get_reorder_params(wil, skb, &tid, &cid, &mid, &seq,
107 sta = &wil->sta[cid];
109 wil_dbg_txrx(wil, "MID %d CID %d TID %d Seq 0x%03x mcast %01x\n",
110 mid, cid, tid, seq, mcast);
112 vif = wil->vifs[mid];
113 if (unlikely(!vif)) {
114 wil_dbg_txrx(wil, "invalid VIF, mid %d\n", mid);
118 ndev = vif_to_ndev(vif);
120 spin_lock(&sta->tid_rx_lock);
122 r = sta->tid_rx[tid];
124 wil_netif_rx_any(skb, ndev);
128 if (unlikely(mcast)) {
129 if (retry && seq == r->mcast_last_seq) {
131 wil_dbg_txrx(wil, "Rx drop: dup mcast seq 0x%03x\n",
136 r->mcast_last_seq = seq;
137 wil_netif_rx_any(skb, ndev);
142 hseq = r->head_seq_num;
144 /** Due to the race between WMI events, where BACK establishment
145 * reported, and data Rx, few packets may be pass up before reorder
146 * buffer get allocated. Catch up by pretending SSN is what we
147 * see in the 1-st Rx packet
149 * Another scenario, Rx get delayed and we got packet from before
150 * BACK. Pass it to the stack and wait.
153 r->first_time = false;
154 if (seq != r->head_seq_num) {
155 if (seq_less(seq, r->head_seq_num)) {
157 "Error: frame with early sequence 0x%03x, should be 0x%03x. Waiting...\n",
158 seq, r->head_seq_num);
159 r->first_time = true;
160 wil_netif_rx_any(skb, ndev);
164 "Error: 1-st frame with wrong sequence 0x%03x, should be 0x%03x. Fixing...\n",
165 seq, r->head_seq_num);
166 r->head_seq_num = seq;
171 /* frame with out of date sequence number */
172 if (seq_less(seq, r->head_seq_num)) {
173 r->ssn_last_drop = seq;
175 wil_dbg_txrx(wil, "Rx drop: old seq 0x%03x head 0x%03x\n",
176 seq, r->head_seq_num);
182 * If frame the sequence number exceeds our buffering window
183 * size release some previous frames to make room for this one.
185 if (!seq_less(seq, r->head_seq_num + r->buf_size)) {
186 hseq = seq_inc(seq_sub(seq, r->buf_size));
187 /* release stored frames up to new head to stack */
188 wil_release_reorder_frames(ndev, r, hseq);
191 /* Now the new frame is always in the range of the reordering buffer */
193 index = reorder_index(r, seq);
195 /* check if we already stored this frame */
196 if (r->reorder_buf[index]) {
198 wil_dbg_txrx(wil, "Rx drop: dup seq 0x%03x\n", seq);
204 * If the current MPDU is in the right order and nothing else
205 * is stored we can process it directly, no need to buffer it.
206 * If it is first but there's something stored, we may be able
207 * to release frames after this one.
209 if (seq == r->head_seq_num && r->stored_mpdu_num == 0) {
210 r->head_seq_num = seq_inc(r->head_seq_num);
211 wil_netif_rx_any(skb, ndev);
215 /* put the frame in the reordering buffer */
216 r->reorder_buf[index] = skb;
217 r->stored_mpdu_num++;
218 wil_reorder_release(ndev, r);
221 spin_unlock(&sta->tid_rx_lock);
224 /* process BAR frame, called in NAPI context */
225 void wil_rx_bar(struct wil6210_priv *wil, struct wil6210_vif *vif,
226 u8 cid, u8 tid, u16 seq)
228 struct wil_sta_info *sta = &wil->sta[cid];
229 struct net_device *ndev = vif_to_ndev(vif);
230 struct wil_tid_ampdu_rx *r;
232 spin_lock(&sta->tid_rx_lock);
234 r = sta->tid_rx[tid];
236 wil_err(wil, "BAR for non-existing CID %d TID %d\n", cid, tid);
239 if (seq_less(seq, r->head_seq_num)) {
240 wil_err(wil, "BAR Seq 0x%03x preceding head 0x%03x\n",
241 seq, r->head_seq_num);
244 wil_dbg_txrx(wil, "BAR: CID %d MID %d TID %d Seq 0x%03x head 0x%03x\n",
245 cid, vif->mid, tid, seq, r->head_seq_num);
246 wil_release_reorder_frames(ndev, r, seq);
249 spin_unlock(&sta->tid_rx_lock);
252 struct wil_tid_ampdu_rx *wil_tid_ampdu_rx_alloc(struct wil6210_priv *wil,
255 struct wil_tid_ampdu_rx *r = kzalloc(sizeof(*r), GFP_KERNEL);
261 kcalloc(size, sizeof(struct sk_buff *), GFP_KERNEL);
262 if (!r->reorder_buf) {
263 kfree(r->reorder_buf);
269 r->head_seq_num = ssn;
271 r->stored_mpdu_num = 0;
272 r->first_time = true;
273 r->mcast_last_seq = U16_MAX;
277 void wil_tid_ampdu_rx_free(struct wil6210_priv *wil,
278 struct wil_tid_ampdu_rx *r)
285 /* Do not pass remaining frames to the network stack - it may be
286 * not expecting to get any more Rx. Rx from here may lead to
287 * kernel OOPS since some per-socket accounting info was already
290 for (i = 0; i < r->buf_size; i++)
291 kfree_skb(r->reorder_buf[i]);
293 kfree(r->reorder_buf);
297 /* ADDBA processing */
298 static u16 wil_agg_size(struct wil6210_priv *wil, u16 req_agg_wsize)
300 u16 max_agg_size = min_t(u16, wil->max_agg_wsize, wil->max_ampdu_size /
301 (mtu_max + WIL_MAX_MPDU_OVERHEAD));
306 return min(max_agg_size, req_agg_wsize);
309 /* Block Ack - Rx side (recipient) */
310 int wil_addba_rx_request(struct wil6210_priv *wil, u8 mid,
311 u8 cidxtid, u8 dialog_token, __le16 ba_param_set,
312 __le16 ba_timeout, __le16 ba_seq_ctrl)
313 __acquires(&sta->tid_rx_lock) __releases(&sta->tid_rx_lock)
315 u16 param_set = le16_to_cpu(ba_param_set);
316 u16 agg_timeout = le16_to_cpu(ba_timeout);
317 u16 seq_ctrl = le16_to_cpu(ba_seq_ctrl);
318 struct wil_sta_info *sta;
321 /* bit 0: A-MSDU supported
322 * bit 1: policy (should be 0 for us)
324 * bits 6..15: buffer size
326 u16 req_agg_wsize = WIL_GET_BITS(param_set, 6, 15);
327 bool agg_amsdu = wil->use_enhanced_dma_hw &&
328 wil->use_rx_hw_reordering &&
329 test_bit(WMI_FW_CAPABILITY_AMSDU, wil->fw_capabilities) &&
330 wil->amsdu_en && (param_set & BIT(0));
331 int ba_policy = param_set & BIT(1);
332 u16 status = WLAN_STATUS_SUCCESS;
333 u16 ssn = seq_ctrl >> 4;
334 struct wil_tid_ampdu_rx *r;
338 parse_cidxtid(cidxtid, &cid, &tid);
341 if (cid >= WIL6210_MAX_CID) {
342 wil_err(wil, "BACK: invalid CID %d\n", cid);
347 sta = &wil->sta[cid];
348 if (sta->status != wil_sta_connected) {
349 wil_err(wil, "BACK: CID %d not connected\n", cid);
355 "ADDBA request for CID %d %pM TID %d size %d timeout %d AMSDU%s policy %d token %d SSN 0x%03x\n",
356 cid, sta->addr, tid, req_agg_wsize, agg_timeout,
357 agg_amsdu ? "+" : "-", !!ba_policy, dialog_token, ssn);
361 wil_err(wil, "BACK requested unsupported ba_policy == 1\n");
362 status = WLAN_STATUS_INVALID_QOS_PARAM;
364 if (status == WLAN_STATUS_SUCCESS) {
365 if (req_agg_wsize == 0) {
366 wil_dbg_misc(wil, "Suggest BACK wsize %d\n",
368 agg_wsize = wil->max_agg_wsize;
370 agg_wsize = min_t(u16,
371 wil->max_agg_wsize, req_agg_wsize);
375 rc = wil->txrx_ops.wmi_addba_rx_resp(wil, mid, cid, tid, dialog_token,
376 status, agg_amsdu, agg_wsize,
378 if (rc || (status != WLAN_STATUS_SUCCESS)) {
379 wil_err(wil, "do not apply ba, rc(%d), status(%d)\n", rc,
385 r = wil_tid_ampdu_rx_alloc(wil, agg_wsize, ssn);
386 spin_lock_bh(&sta->tid_rx_lock);
387 wil_tid_ampdu_rx_free(wil, sta->tid_rx[tid]);
388 sta->tid_rx[tid] = r;
389 spin_unlock_bh(&sta->tid_rx_lock);
395 /* BACK - Tx side (originator) */
396 int wil_addba_tx_request(struct wil6210_priv *wil, u8 ringid, u16 wsize)
398 u8 agg_wsize = wil_agg_size(wil, wsize);
400 struct wil_ring_tx_data *txdata = &wil->ring_tx_data[ringid];
403 if (txdata->addba_in_progress) {
404 wil_dbg_misc(wil, "ADDBA for vring[%d] already in progress\n",
408 if (txdata->agg_wsize) {
410 "ADDBA for vring[%d] already done for wsize %d\n",
411 ringid, txdata->agg_wsize);
414 txdata->addba_in_progress = true;
415 rc = wmi_addba(wil, txdata->mid, ringid, agg_wsize, agg_timeout);
417 wil_err(wil, "wmi_addba failed, rc (%d)", rc);
418 txdata->addba_in_progress = false;