xref: /linux/drivers/net/wireless/intel/iwlwifi/mld/agg.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2024-2026 Intel Corporation
4  */
5 #include "agg.h"
6 #include "sta.h"
7 #include "hcmd.h"
8 
9 static void
10 iwl_mld_reorder_release_frames(struct iwl_mld *mld, struct ieee80211_sta *sta,
11 			       struct napi_struct *napi,
12 			       struct iwl_mld_baid_data *baid_data,
13 			       struct iwl_mld_reorder_buffer *reorder_buf,
14 			       u16 nssn)
15 {
16 	struct iwl_mld_reorder_buf_entry *entries =
17 		&baid_data->entries[reorder_buf->queue *
18 				    baid_data->entries_per_queue];
19 	u16 ssn = reorder_buf->head_sn;
20 
21 	while (ieee80211_sn_less(ssn, nssn)) {
22 		int index = ssn % baid_data->buf_size;
23 		struct sk_buff_head *skb_list = &entries[index].frames;
24 		struct sk_buff *skb;
25 
26 		ssn = ieee80211_sn_inc(ssn);
27 
28 		/* Empty the list. Will have more than one frame for A-MSDU.
29 		 * Empty list is valid as well since nssn indicates frames were
30 		 * received.
31 		 */
32 		while ((skb = __skb_dequeue(skb_list))) {
33 			iwl_mld_pass_packet_to_mac80211(mld, napi, skb,
34 							reorder_buf->queue,
35 							sta);
36 			reorder_buf->num_stored--;
37 		}
38 	}
39 	reorder_buf->head_sn = nssn;
40 }
41 
42 static void iwl_mld_release_frames_from_notif(struct iwl_mld *mld,
43 					      struct napi_struct *napi,
44 					      u8 baid, u16 nssn, int queue)
45 {
46 	struct iwl_mld_reorder_buffer *reorder_buf;
47 	struct iwl_mld_baid_data *ba_data;
48 	struct ieee80211_link_sta *link_sta;
49 	u32 sta_id;
50 
51 	IWL_DEBUG_HT(mld, "Frame release notification for BAID %u, NSSN %d\n",
52 		     baid, nssn);
53 
54 	if (WARN_ON_ONCE(baid == IWL_RX_REORDER_DATA_INVALID_BAID ||
55 			 baid >= ARRAY_SIZE(mld->fw_id_to_ba)))
56 		return;
57 
58 	rcu_read_lock();
59 
60 	ba_data = rcu_dereference(mld->fw_id_to_ba[baid]);
61 	if (!ba_data) {
62 		IWL_DEBUG_HT(mld, "BAID %d not found in map\n", baid);
63 		goto out_unlock;
64 	}
65 
66 	/* pick any STA ID to find the pointer */
67 	if (WARN_ON_ONCE(!ba_data->sta_mask))
68 		goto out_unlock;
69 
70 	sta_id = ffs(ba_data->sta_mask) - 1;
71 	link_sta = rcu_dereference(mld->fw_id_to_link_sta[sta_id]);
72 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(link_sta) || !link_sta->sta))
73 		goto out_unlock;
74 
75 	reorder_buf = &ba_data->reorder_buf[queue];
76 
77 	iwl_mld_reorder_release_frames(mld, link_sta->sta, napi, ba_data,
78 				       reorder_buf, nssn);
79 out_unlock:
80 	rcu_read_unlock();
81 }
82 
83 void iwl_mld_handle_frame_release_notif(struct iwl_mld *mld,
84 					struct napi_struct *napi,
85 					struct iwl_rx_packet *pkt, int queue)
86 {
87 	struct iwl_frame_release *release = (void *)pkt->data;
88 	u32 pkt_len = iwl_rx_packet_payload_len(pkt);
89 
90 	if (IWL_FW_CHECK(mld, pkt_len < sizeof(*release),
91 			 "Unexpected frame release notif size %u (expected %zu)\n",
92 			 pkt_len, sizeof(*release)))
93 		return;
94 
95 	iwl_mld_release_frames_from_notif(mld, napi, release->baid,
96 					  le16_to_cpu(release->nssn),
97 					  queue);
98 }
99 
100 void iwl_mld_handle_bar_frame_release_notif(struct iwl_mld *mld,
101 					    struct napi_struct *napi,
102 					    struct iwl_rx_packet *pkt,
103 					    int queue)
104 {
105 	struct iwl_bar_frame_release *release = (void *)pkt->data;
106 	struct iwl_mld_baid_data *baid_data;
107 	unsigned int baid, nssn, sta_id, tid;
108 	u32 pkt_len = iwl_rx_packet_payload_len(pkt);
109 
110 	if (IWL_FW_CHECK(mld, pkt_len < sizeof(*release),
111 			 "Unexpected frame release notif size %u (expected %zu)\n",
112 			 pkt_len, sizeof(*release)))
113 		return;
114 
115 	baid = le32_get_bits(release->ba_info,
116 			     IWL_BAR_FRAME_RELEASE_BAID_MASK);
117 	nssn = le32_get_bits(release->ba_info,
118 			     IWL_BAR_FRAME_RELEASE_NSSN_MASK);
119 	sta_id = le32_get_bits(release->sta_tid,
120 			       IWL_BAR_FRAME_RELEASE_STA_MASK);
121 	tid = le32_get_bits(release->sta_tid,
122 			    IWL_BAR_FRAME_RELEASE_TID_MASK);
123 
124 	if (IWL_FW_CHECK(mld, baid >= ARRAY_SIZE(mld->fw_id_to_ba),
125 			 "BAR release: invalid BAID (%x)\n", baid))
126 		return;
127 
128 	rcu_read_lock();
129 	baid_data = rcu_dereference(mld->fw_id_to_ba[baid]);
130 	if (!baid_data) {
131 		IWL_DEBUG_HT(mld,
132 			     "Got valid BAID %d but not allocated\n",
133 			     baid);
134 		goto out_unlock;
135 	}
136 
137 	if (IWL_FW_CHECK(mld, tid != baid_data->tid ||
138 			 sta_id > mld->fw->ucode_capa.num_stations ||
139 			 !(baid_data->sta_mask & BIT(sta_id)),
140 			 "BAID 0x%x is mapped to sta_mask:0x%x tid:%d, but BAR release received for sta:%d tid:%d\n",
141 			 baid, baid_data->sta_mask, baid_data->tid, sta_id,
142 			 tid))
143 		goto out_unlock;
144 
145 	IWL_DEBUG_DROP(mld, "Received a BAR, expect packet loss: nssn %d\n",
146 		       nssn);
147 
148 	iwl_mld_release_frames_from_notif(mld, napi, baid, nssn, queue);
149 out_unlock:
150 	rcu_read_unlock();
151 }
152 
153 void iwl_mld_del_ba(struct iwl_mld *mld, int queue,
154 		    struct iwl_mld_delba_data *data)
155 {
156 	struct iwl_mld_baid_data *ba_data;
157 	struct iwl_mld_reorder_buffer *reorder_buf;
158 	struct ieee80211_link_sta *link_sta;
159 	u8 baid = data->baid;
160 	u32 sta_id;
161 
162 	if (WARN_ONCE(baid >= IWL_MAX_BAID, "invalid BAID: %x\n", baid))
163 		return;
164 
165 	rcu_read_lock();
166 
167 	ba_data = rcu_dereference(mld->fw_id_to_ba[baid]);
168 	if (WARN_ON_ONCE(!ba_data))
169 		goto out_unlock;
170 
171 	/* pick any STA ID to find the pointer */
172 	if (WARN_ON_ONCE(!ba_data->sta_mask))
173 		goto out_unlock;
174 
175 	sta_id = ffs(ba_data->sta_mask) - 1;
176 	link_sta = rcu_dereference(mld->fw_id_to_link_sta[sta_id]);
177 	if (WARN_ON_ONCE(IS_ERR_OR_NULL(link_sta) || !link_sta->sta))
178 		goto out_unlock;
179 
180 	reorder_buf = &ba_data->reorder_buf[queue];
181 
182 	/* release all frames that are in the reorder buffer to the stack */
183 	iwl_mld_reorder_release_frames(mld, link_sta->sta, NULL,
184 				       ba_data, reorder_buf,
185 				       ieee80211_sn_add(reorder_buf->head_sn,
186 							ba_data->buf_size));
187 out_unlock:
188 	rcu_read_unlock();
189 }
190 
191 /* Returns true if the MPDU was buffered\dropped, false if it should be passed
192  * to upper layer.
193  */
194 enum iwl_mld_reorder_result
195 iwl_mld_reorder(struct iwl_mld *mld, struct napi_struct *napi,
196 		int queue, struct ieee80211_sta *sta,
197 		struct sk_buff *skb, struct iwl_rx_mpdu_desc *desc)
198 {
199 	struct ieee80211_hdr *hdr = (void *)skb_mac_header(skb);
200 	struct iwl_mld_baid_data *baid_data;
201 	struct iwl_mld_reorder_buffer *buffer;
202 	struct iwl_mld_reorder_buf_entry *entries;
203 	struct iwl_mld_link_sta *mld_link_sta;
204 	u32 reorder = le32_to_cpu(desc->reorder_data);
205 	bool amsdu, last_subframe, is_old_sn, is_dup;
206 	u8 tid = ieee80211_get_tid(hdr);
207 	struct iwl_mld_sta *mld_sta;
208 	u8 baid;
209 	u16 nssn, sn;
210 	u32 sta_mask = 0;
211 	int index;
212 	u8 link_id;
213 
214 	baid = u32_get_bits(reorder, IWL_RX_MPDU_REORDER_BAID_MASK);
215 
216 	/* This also covers the case of receiving a Block Ack Request
217 	 * outside a BA session; we'll pass it to mac80211 and that
218 	 * then sends a delBA action frame.
219 	 * This also covers pure monitor mode, in which case we won't
220 	 * have any BA sessions.
221 	 */
222 	if (baid == IWL_RX_REORDER_DATA_INVALID_BAID)
223 		return IWL_MLD_PASS_SKB;
224 
225 	if (IWL_FW_CHECK(mld, baid >= ARRAY_SIZE(mld->fw_id_to_ba),
226 			 "Got out-of-range BAID %u in reorder_data=0x%x\n",
227 			 baid, reorder))
228 		return IWL_MLD_PASS_SKB;
229 
230 	/* no sta yet */
231 	if (IWL_FW_CHECK(mld, !sta,
232 			 "Got valid BAID without a valid station assigned - %d\n",
233 			 baid))
234 		return IWL_MLD_PASS_SKB;
235 
236 	mld_sta = iwl_mld_sta_from_mac80211(sta);
237 
238 	/* not a data packet */
239 	if (!ieee80211_is_data_qos(hdr->frame_control) ||
240 	    is_multicast_ether_addr(hdr->addr1))
241 		return IWL_MLD_PASS_SKB;
242 
243 	if (unlikely(!ieee80211_is_data_present(hdr->frame_control)))
244 		return IWL_MLD_PASS_SKB;
245 
246 	baid_data = rcu_dereference(mld->fw_id_to_ba[baid]);
247 	if (!baid_data) {
248 		IWL_DEBUG_HT(mld,
249 			     "Got valid BAID but no baid allocated, bypass re-ordering (BAID=%d reorder=0x%x)\n",
250 			     baid, reorder);
251 		return IWL_MLD_PASS_SKB;
252 	}
253 
254 	for_each_mld_link_sta(mld_sta, mld_link_sta, link_id)
255 		sta_mask |= BIT(mld_link_sta->fw_id);
256 
257 	/* verify the BAID is correctly mapped to the sta and tid */
258 	if (IWL_FW_CHECK(mld,
259 			 tid != baid_data->tid ||
260 			 !(sta_mask & baid_data->sta_mask),
261 			 "BAID 0x%x is mapped to sta_mask:0x%x tid:%d, but was received for sta_mask:0x%x tid:%d\n",
262 			 baid, baid_data->sta_mask, baid_data->tid,
263 			 sta_mask, tid))
264 		return IWL_MLD_PASS_SKB;
265 
266 	buffer = &baid_data->reorder_buf[queue];
267 	entries = &baid_data->entries[queue * baid_data->entries_per_queue];
268 
269 	is_old_sn = !!(reorder & IWL_RX_MPDU_REORDER_BA_OLD_SN);
270 
271 	if (!buffer->valid && is_old_sn)
272 		return IWL_MLD_PASS_SKB;
273 
274 	buffer->valid = true;
275 
276 	is_dup = !!(desc->status & cpu_to_le32(IWL_RX_MPDU_STATUS_DUPLICATE));
277 
278 	/* drop any duplicated or outdated packets */
279 	if (is_dup || is_old_sn)
280 		return IWL_MLD_DROP_SKB;
281 
282 	sn = u32_get_bits(reorder, IWL_RX_MPDU_REORDER_SN_MASK);
283 	nssn = u32_get_bits(reorder, IWL_RX_MPDU_REORDER_NSSN_MASK);
284 	amsdu = desc->mac_flags2 & IWL_RX_MPDU_MFLG2_AMSDU;
285 	last_subframe = desc->amsdu_info & IWL_RX_MPDU_AMSDU_LAST_SUBFRAME;
286 
287 	/* release immediately if allowed by nssn and no stored frames */
288 	if (!buffer->num_stored && ieee80211_sn_less(sn, nssn)) {
289 		if (!amsdu || last_subframe)
290 			buffer->head_sn = nssn;
291 		return IWL_MLD_PASS_SKB;
292 	}
293 
294 	/* release immediately if there are no stored frames, and the sn is
295 	 * equal to the head.
296 	 * This can happen due to reorder timer, where NSSN is behind head_sn.
297 	 * When we released everything, and we got the next frame in the
298 	 * sequence, according to the NSSN we can't release immediately,
299 	 * while technically there is no hole and we can move forward.
300 	 */
301 	if (!buffer->num_stored && sn == buffer->head_sn) {
302 		if (!amsdu || last_subframe)
303 			buffer->head_sn = ieee80211_sn_inc(buffer->head_sn);
304 		return IWL_MLD_PASS_SKB;
305 	}
306 
307 	/* put in reorder buffer */
308 	index = sn % baid_data->buf_size;
309 	__skb_queue_tail(&entries[index].frames, skb);
310 	buffer->num_stored++;
311 
312 	/* We cannot trust NSSN for AMSDU sub-frames that are not the last. The
313 	 * reason is that NSSN advances on the first sub-frame, and may cause
314 	 * the reorder buffer to advance before all the sub-frames arrive.
315 	 *
316 	 * Example: reorder buffer contains SN 0 & 2, and we receive AMSDU with
317 	 * SN 1. NSSN for first sub frame will be 3 with the result of driver
318 	 * releasing SN 0,1, 2. When sub-frame 1 arrives - reorder buffer is
319 	 * already ahead and it will be dropped.
320 	 * If the last sub-frame is not on this queue - we will get frame
321 	 * release notification with up to date NSSN.
322 	 * If this is the first frame that is stored in the buffer, the head_sn
323 	 * may be outdated. Update it based on the last NSSN to make sure it
324 	 * will be released when the frame release notification arrives.
325 	 */
326 	if (!amsdu || last_subframe)
327 		iwl_mld_reorder_release_frames(mld, sta, napi, baid_data,
328 					       buffer, nssn);
329 	else if (buffer->num_stored == 1)
330 		buffer->head_sn = nssn;
331 
332 	return IWL_MLD_BUFFERED_SKB;
333 }
334 EXPORT_SYMBOL_IF_IWLWIFI_KUNIT(iwl_mld_reorder);
335 
336 static void iwl_mld_rx_agg_session_expired(struct timer_list *t)
337 {
338 	struct iwl_mld_baid_data *data =
339 		timer_container_of(data, t, session_timer);
340 	struct iwl_mld_baid_data __rcu **rcu_ptr = data->rcu_ptr;
341 	struct iwl_mld_baid_data *ba_data;
342 	struct ieee80211_link_sta *link_sta;
343 	struct iwl_mld_sta *mld_sta;
344 	unsigned long timeout;
345 	unsigned int sta_id;
346 
347 	rcu_read_lock();
348 
349 	ba_data = rcu_dereference(*rcu_ptr);
350 	if (WARN_ON(!ba_data))
351 		goto unlock;
352 
353 	if (WARN_ON(!ba_data->timeout))
354 		goto unlock;
355 
356 	timeout = ba_data->last_rx_timestamp +
357 		  TU_TO_JIFFIES(ba_data->timeout * 2);
358 	if (time_is_after_jiffies(timeout)) {
359 		mod_timer(&ba_data->session_timer, timeout);
360 		goto unlock;
361 	}
362 
363 	/* timer expired, pick any STA ID to find the pointer */
364 	if (WARN_ON_ONCE(!ba_data->sta_mask))
365 		goto unlock;
366 
367 	sta_id = ffs(ba_data->sta_mask) - 1;
368 	link_sta = rcu_dereference(ba_data->mld->fw_id_to_link_sta[sta_id]);
369 
370 	/* sta should be valid unless the following happens:
371 	 * The firmware asserts which triggers a reconfig flow, but
372 	 * the reconfig fails before we set the pointer to sta into
373 	 * the fw_id_to_link_sta pointer table. mac80211 can't stop
374 	 * A-MPDU and hence the timer continues to run. Then, the
375 	 * timer expires and sta is NULL.
376 	 */
377 	if (IS_ERR_OR_NULL(link_sta) || WARN_ON(!link_sta->sta))
378 		goto unlock;
379 
380 	mld_sta = iwl_mld_sta_from_mac80211(link_sta->sta);
381 	ieee80211_rx_ba_timer_expired(mld_sta->vif, link_sta->sta->addr,
382 				      ba_data->tid);
383 unlock:
384 	rcu_read_unlock();
385 }
386 
387 static int
388 iwl_mld_stop_ba_in_fw(struct iwl_mld *mld, struct ieee80211_sta *sta, int tid)
389 {
390 	struct iwl_rx_baid_cfg_cmd cmd = {
391 		.action = cpu_to_le32(IWL_RX_BAID_ACTION_REMOVE),
392 		.remove.sta_id_mask =
393 			cpu_to_le32(iwl_mld_fw_sta_id_mask(mld, sta)),
394 		.remove.tid = cpu_to_le32(tid),
395 
396 	};
397 	int ret;
398 
399 	ret = iwl_mld_send_cmd_pdu(mld,
400 				   WIDE_ID(DATA_PATH_GROUP,
401 					   RX_BAID_ALLOCATION_CONFIG_CMD),
402 				   &cmd);
403 	if (ret)
404 		return ret;
405 
406 	IWL_DEBUG_HT(mld, "RX BA Session stopped in fw\n");
407 
408 	return ret;
409 }
410 
411 static int
412 iwl_mld_start_ba_in_fw(struct iwl_mld *mld, struct ieee80211_sta *sta,
413 		       int tid, u16 ssn, u16 buf_size)
414 {
415 	struct iwl_rx_baid_cfg_cmd cmd = {
416 		.action = cpu_to_le32(IWL_RX_BAID_ACTION_ADD),
417 		.alloc.sta_id_mask =
418 			cpu_to_le32(iwl_mld_fw_sta_id_mask(mld, sta)),
419 		.alloc.tid = tid,
420 		.alloc.ssn = cpu_to_le16(ssn),
421 		.alloc.win_size = cpu_to_le16(buf_size),
422 	};
423 	struct iwl_host_cmd hcmd = {
424 		.id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD),
425 		.flags = CMD_WANT_SKB,
426 		.len[0] = sizeof(cmd),
427 		.data[0] = &cmd,
428 	};
429 	struct iwl_rx_baid_cfg_resp *resp;
430 	struct iwl_rx_packet *pkt;
431 	u32 resp_len;
432 	int ret, baid;
433 
434 	BUILD_BUG_ON(sizeof(*resp) != sizeof(baid));
435 
436 	ret = iwl_mld_send_cmd(mld, &hcmd);
437 	if (ret)
438 		return ret;
439 
440 	pkt = hcmd.resp_pkt;
441 
442 	resp_len = iwl_rx_packet_payload_len(pkt);
443 	if (IWL_FW_CHECK(mld, resp_len != sizeof(*resp),
444 			 "BAID_ALLOC_CMD: unexpected response length %d\n",
445 			 resp_len)) {
446 		ret = -EIO;
447 		goto out;
448 	}
449 
450 	IWL_DEBUG_HT(mld, "RX BA Session started in fw\n");
451 
452 	resp = (void *)pkt->data;
453 	baid = le32_to_cpu(resp->baid);
454 
455 	if (IWL_FW_CHECK(mld, baid < 0 || baid >= ARRAY_SIZE(mld->fw_id_to_ba),
456 			 "BAID_ALLOC_CMD: invalid BAID response %d\n", baid)) {
457 		ret = -EINVAL;
458 		goto out;
459 	}
460 
461 	ret = baid;
462 out:
463 	iwl_free_resp(&hcmd);
464 	return ret;
465 }
466 
467 static void iwl_mld_init_reorder_buffer(struct iwl_mld *mld,
468 					struct iwl_mld_baid_data *data,
469 					u16 ssn)
470 {
471 	for (int i = 0; i < mld->trans->info.num_rxqs; i++) {
472 		struct iwl_mld_reorder_buffer *reorder_buf =
473 			&data->reorder_buf[i];
474 		struct iwl_mld_reorder_buf_entry *entries =
475 			&data->entries[i * data->entries_per_queue];
476 
477 		reorder_buf->head_sn = ssn;
478 		reorder_buf->queue = i;
479 
480 		for (int j = 0; j < data->buf_size; j++)
481 			__skb_queue_head_init(&entries[j].frames);
482 	}
483 }
484 
485 static void iwl_mld_free_reorder_buffer(struct iwl_mld *mld,
486 					struct iwl_mld_baid_data *data)
487 {
488 	struct iwl_mld_delba_data delba_data = {
489 		.baid = data->baid,
490 	};
491 
492 	iwl_mld_sync_rx_queues(mld, IWL_MLD_RXQ_NOTIF_DEL_BA,
493 			       &delba_data, sizeof(delba_data));
494 
495 	for (int i = 0; i < mld->trans->info.num_rxqs; i++) {
496 		struct iwl_mld_reorder_buffer *reorder_buf =
497 			&data->reorder_buf[i];
498 		struct iwl_mld_reorder_buf_entry *entries =
499 			&data->entries[i * data->entries_per_queue];
500 
501 		if (likely(!reorder_buf->num_stored))
502 			continue;
503 
504 		/* This shouldn't happen in regular DELBA since the RX queues
505 		 * sync internal DELBA notification should trigger a release
506 		 * of all frames in the reorder buffer.
507 		 */
508 		WARN_ON(1);
509 
510 		for (int j = 0; j < data->buf_size; j++)
511 			__skb_queue_purge(&entries[j].frames);
512 	}
513 }
514 
515 int iwl_mld_ampdu_rx_start(struct iwl_mld *mld, struct ieee80211_sta *sta,
516 			   int tid, u16 ssn, u16 buf_size, u16 timeout)
517 {
518 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
519 	struct iwl_mld_baid_data *baid_data = NULL;
520 	u32 reorder_buf_size = buf_size * sizeof(baid_data->entries[0]);
521 	int ret, baid;
522 	u32 sta_mask;
523 
524 	lockdep_assert_wiphy(mld->wiphy);
525 
526 	if (mld->num_rx_ba_sessions >= IWL_MAX_BAID) {
527 		IWL_DEBUG_HT(mld,
528 			     "Max num of RX BA sessions reached; blocking new session\n");
529 		return -ENOSPC;
530 	}
531 
532 	sta_mask = iwl_mld_fw_sta_id_mask(mld, sta);
533 	if (WARN_ON(!sta_mask))
534 		return -EINVAL;
535 
536 	/* sparse doesn't like the __align() so don't check */
537 #ifndef __CHECKER__
538 	/* The division below will be OK if either the cache line size
539 	 * can be divided by the entry size (ALIGN will round up) or if
540 	 * the entry size can be divided by the cache line size, in which
541 	 * case the ALIGN() will do nothing.
542 	 */
543 	BUILD_BUG_ON(SMP_CACHE_BYTES % sizeof(baid_data->entries[0]) &&
544 		     sizeof(baid_data->entries[0]) % SMP_CACHE_BYTES);
545 #endif
546 
547 	/* Upward align the reorder buffer size to fill an entire cache
548 	 * line for each queue, to avoid sharing cache lines between
549 	 * different queues.
550 	 */
551 	reorder_buf_size = ALIGN(reorder_buf_size, SMP_CACHE_BYTES);
552 
553 	/* Allocate here so if allocation fails we can bail out early
554 	 * before starting the BA session in the firmware
555 	 */
556 	baid_data = kzalloc(sizeof(*baid_data) +
557 			    mld->trans->info.num_rxqs * reorder_buf_size,
558 			    GFP_KERNEL);
559 	if (!baid_data)
560 		return -ENOMEM;
561 
562 	/* This division is why we need the above BUILD_BUG_ON(),
563 	 * if that doesn't hold then this will not be right.
564 	 */
565 	baid_data->entries_per_queue =
566 		reorder_buf_size / sizeof(baid_data->entries[0]);
567 
568 	baid = iwl_mld_start_ba_in_fw(mld, sta, tid, ssn, buf_size);
569 	if (baid < 0) {
570 		ret = baid;
571 		goto out_free;
572 	}
573 
574 	mld->num_rx_ba_sessions++;
575 	mld_sta->tid_to_baid[tid] = baid;
576 
577 	baid_data->baid = baid;
578 	baid_data->mld = mld;
579 	baid_data->tid = tid;
580 	baid_data->buf_size = buf_size;
581 	baid_data->sta_mask = sta_mask;
582 	baid_data->timeout = timeout;
583 	baid_data->last_rx_timestamp = jiffies;
584 	baid_data->rcu_ptr = &mld->fw_id_to_ba[baid];
585 
586 	iwl_mld_init_reorder_buffer(mld, baid_data, ssn);
587 
588 	timer_setup(&baid_data->session_timer, iwl_mld_rx_agg_session_expired,
589 		    0);
590 	if (timeout)
591 		mod_timer(&baid_data->session_timer,
592 			  TU_TO_EXP_TIME(timeout * 2));
593 
594 	IWL_DEBUG_HT(mld, "STA mask=0x%x (tid=%d) is assigned to BAID %d\n",
595 		     baid_data->sta_mask, tid, baid);
596 
597 	/* protect the BA data with RCU to cover a case where our
598 	 * internal RX sync mechanism will timeout (not that it's
599 	 * supposed to happen) and we will free the session data while
600 	 * RX is being processed in parallel
601 	 */
602 	WARN_ON(rcu_access_pointer(mld->fw_id_to_ba[baid]));
603 	rcu_assign_pointer(mld->fw_id_to_ba[baid], baid_data);
604 
605 	return 0;
606 
607 out_free:
608 	kfree(baid_data);
609 	return ret;
610 }
611 
612 int iwl_mld_ampdu_rx_stop(struct iwl_mld *mld, struct ieee80211_sta *sta,
613 			  int tid)
614 {
615 	struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
616 	int baid = mld_sta->tid_to_baid[tid];
617 	struct iwl_mld_baid_data *baid_data;
618 	int ret;
619 
620 	lockdep_assert_wiphy(mld->wiphy);
621 
622 	/* during firmware restart, do not send the command as the firmware no
623 	 * longer recognizes the session. instead, only clear the driver BA
624 	 * session data.
625 	 */
626 	if (!mld->fw_status.in_hw_restart) {
627 		ret = iwl_mld_stop_ba_in_fw(mld, sta, tid);
628 		if (ret)
629 			return ret;
630 	}
631 
632 	if (!WARN_ON(mld->num_rx_ba_sessions == 0))
633 		mld->num_rx_ba_sessions--;
634 
635 	baid_data = wiphy_dereference(mld->wiphy, mld->fw_id_to_ba[baid]);
636 	if (WARN_ON(!baid_data))
637 		return -EINVAL;
638 
639 	if (timer_pending(&baid_data->session_timer))
640 		timer_shutdown_sync(&baid_data->session_timer);
641 
642 	iwl_mld_free_reorder_buffer(mld, baid_data);
643 
644 	RCU_INIT_POINTER(mld->fw_id_to_ba[baid], NULL);
645 	kfree_rcu(baid_data, rcu_head);
646 
647 	IWL_DEBUG_HT(mld, "BAID %d is free\n", baid);
648 
649 	return 0;
650 }
651 
652 int iwl_mld_update_sta_baids(struct iwl_mld *mld,
653 			     u32 old_sta_mask,
654 			     u32 new_sta_mask)
655 {
656 	struct iwl_rx_baid_cfg_cmd cmd = {
657 		.action = cpu_to_le32(IWL_RX_BAID_ACTION_MODIFY),
658 		.modify.old_sta_id_mask = cpu_to_le32(old_sta_mask),
659 		.modify.new_sta_id_mask = cpu_to_le32(new_sta_mask),
660 	};
661 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, RX_BAID_ALLOCATION_CONFIG_CMD);
662 	int baid;
663 
664 	/* mac80211 will remove sessions later, but we ignore all that */
665 	if (mld->fw_status.in_hw_restart)
666 		return 0;
667 
668 	BUILD_BUG_ON(sizeof(struct iwl_rx_baid_cfg_resp) != sizeof(baid));
669 
670 	for (baid = 0; baid < ARRAY_SIZE(mld->fw_id_to_ba); baid++) {
671 		struct iwl_mld_baid_data *data;
672 		int ret;
673 
674 		data = wiphy_dereference(mld->wiphy, mld->fw_id_to_ba[baid]);
675 		if (!data)
676 			continue;
677 
678 		if (!(data->sta_mask & old_sta_mask))
679 			continue;
680 
681 		WARN_ONCE(data->sta_mask != old_sta_mask,
682 			  "BAID data for %d corrupted - expected 0x%x found 0x%x\n",
683 			  baid, old_sta_mask, data->sta_mask);
684 
685 		cmd.modify.tid = cpu_to_le32(data->tid);
686 
687 		ret = iwl_mld_send_cmd_pdu(mld, cmd_id, &cmd);
688 		if (ret)
689 			return ret;
690 		data->sta_mask = new_sta_mask;
691 	}
692 
693 	return 0;
694 }
695