1 /*
2  * This file is part of wl1271
3  *
4  * Copyright (C) 2008-2009 Nokia Corporation
5  *
6  * Contact: Luciano Coelho <luciano.coelho@nokia.com>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * version 2 as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  *
22  */
23 
24 #include "wl12xx.h"
25 #include "debug.h"
26 #include "reg.h"
27 #include "io.h"
28 #include "event.h"
29 #include "ps.h"
30 #include "scan.h"
31 #include "wl12xx_80211.h"
32 
wl1271_pspoll_work(struct work_struct * work)33 void wl1271_pspoll_work(struct work_struct *work)
34 {
35 	struct ieee80211_vif *vif;
36 	struct wl12xx_vif *wlvif;
37 	struct delayed_work *dwork;
38 	struct wl1271 *wl;
39 	int ret;
40 
41 	dwork = container_of(work, struct delayed_work, work);
42 	wlvif = container_of(dwork, struct wl12xx_vif, pspoll_work);
43 	vif = container_of((void *)wlvif, struct ieee80211_vif, drv_priv);
44 	wl = wlvif->wl;
45 
46 	wl1271_debug(DEBUG_EVENT, "pspoll work");
47 
48 	mutex_lock(&wl->mutex);
49 
50 	if (unlikely(wl->state == WL1271_STATE_OFF))
51 		goto out;
52 
53 	if (!test_and_clear_bit(WLVIF_FLAG_PSPOLL_FAILURE, &wlvif->flags))
54 		goto out;
55 
56 	if (!test_bit(WLVIF_FLAG_STA_ASSOCIATED, &wlvif->flags))
57 		goto out;
58 
59 	/*
60 	 * if we end up here, then we were in powersave when the pspoll
61 	 * delivery failure occurred, and no-one changed state since, so
62 	 * we should go back to powersave.
63 	 */
64 	ret = wl1271_ps_elp_wakeup(wl);
65 	if (ret < 0)
66 		goto out;
67 
68 	wl1271_ps_set_mode(wl, wlvif, STATION_POWER_SAVE_MODE,
69 			   wlvif->basic_rate, true);
70 
71 	wl1271_ps_elp_sleep(wl);
72 out:
73 	mutex_unlock(&wl->mutex);
74 };
75 
wl1271_event_pspoll_delivery_fail(struct wl1271 * wl,struct wl12xx_vif * wlvif)76 static void wl1271_event_pspoll_delivery_fail(struct wl1271 *wl,
77 					      struct wl12xx_vif *wlvif)
78 {
79 	int delay = wl->conf.conn.ps_poll_recovery_period;
80 	int ret;
81 
82 	wlvif->ps_poll_failures++;
83 	if (wlvif->ps_poll_failures == 1)
84 		wl1271_info("AP with dysfunctional ps-poll, "
85 			    "trying to work around it.");
86 
87 	/* force active mode receive data from the AP */
88 	if (test_bit(WLVIF_FLAG_PSM, &wlvif->flags)) {
89 		ret = wl1271_ps_set_mode(wl, wlvif, STATION_ACTIVE_MODE,
90 					 wlvif->basic_rate, true);
91 		if (ret < 0)
92 			return;
93 		set_bit(WLVIF_FLAG_PSPOLL_FAILURE, &wlvif->flags);
94 		ieee80211_queue_delayed_work(wl->hw, &wlvif->pspoll_work,
95 					     msecs_to_jiffies(delay));
96 	}
97 
98 	/*
99 	 * If already in active mode, lets we should be getting data from
100 	 * the AP right away. If we enter PSM too fast after this, and data
101 	 * remains on the AP, we will get another event like this, and we'll
102 	 * go into active once more.
103 	 */
104 }
105 
wl1271_event_ps_report(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct event_mailbox * mbox,bool * beacon_loss)106 static int wl1271_event_ps_report(struct wl1271 *wl,
107 				  struct wl12xx_vif *wlvif,
108 				  struct event_mailbox *mbox,
109 				  bool *beacon_loss)
110 {
111 	int ret = 0;
112 	u32 total_retries = wl->conf.conn.psm_entry_retries;
113 
114 	wl1271_debug(DEBUG_EVENT, "ps_status: 0x%x", mbox->ps_status);
115 
116 	switch (mbox->ps_status) {
117 	case EVENT_ENTER_POWER_SAVE_FAIL:
118 		wl1271_debug(DEBUG_PSM, "PSM entry failed");
119 
120 		if (!test_bit(WLVIF_FLAG_PSM, &wlvif->flags)) {
121 			/* remain in active mode */
122 			wlvif->psm_entry_retry = 0;
123 			break;
124 		}
125 
126 		if (wlvif->psm_entry_retry < total_retries) {
127 			wlvif->psm_entry_retry++;
128 			ret = wl1271_ps_set_mode(wl, wlvif,
129 						 STATION_POWER_SAVE_MODE,
130 						 wlvif->basic_rate, true);
131 		} else {
132 			wl1271_info("No ack to nullfunc from AP.");
133 			wlvif->psm_entry_retry = 0;
134 			*beacon_loss = true;
135 		}
136 		break;
137 	case EVENT_ENTER_POWER_SAVE_SUCCESS:
138 		wlvif->psm_entry_retry = 0;
139 
140 		/*
141 		 * BET has only a minor effect in 5GHz and masks
142 		 * channel switch IEs, so we only enable BET on 2.4GHz
143 		*/
144 		if (wlvif->band == IEEE80211_BAND_2GHZ)
145 			/* enable beacon early termination */
146 			ret = wl1271_acx_bet_enable(wl, wlvif, true);
147 
148 		if (wlvif->ps_compl) {
149 			complete(wlvif->ps_compl);
150 			wlvif->ps_compl = NULL;
151 		}
152 		break;
153 	default:
154 		break;
155 	}
156 
157 	return ret;
158 }
159 
wl1271_event_rssi_trigger(struct wl1271 * wl,struct wl12xx_vif * wlvif,struct event_mailbox * mbox)160 static void wl1271_event_rssi_trigger(struct wl1271 *wl,
161 				      struct wl12xx_vif *wlvif,
162 				      struct event_mailbox *mbox)
163 {
164 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
165 	enum nl80211_cqm_rssi_threshold_event event;
166 	s8 metric = mbox->rssi_snr_trigger_metric[0];
167 
168 	wl1271_debug(DEBUG_EVENT, "RSSI trigger metric: %d", metric);
169 
170 	if (metric <= wlvif->rssi_thold)
171 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
172 	else
173 		event = NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH;
174 
175 	if (event != wlvif->last_rssi_event)
176 		ieee80211_cqm_rssi_notify(vif, event, GFP_KERNEL);
177 	wlvif->last_rssi_event = event;
178 }
179 
wl1271_stop_ba_event(struct wl1271 * wl,struct wl12xx_vif * wlvif)180 static void wl1271_stop_ba_event(struct wl1271 *wl, struct wl12xx_vif *wlvif)
181 {
182 	struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
183 
184 	if (wlvif->bss_type != BSS_TYPE_AP_BSS) {
185 		if (!wlvif->sta.ba_rx_bitmap)
186 			return;
187 		ieee80211_stop_rx_ba_session(vif, wlvif->sta.ba_rx_bitmap,
188 					     vif->bss_conf.bssid);
189 	} else {
190 		u8 hlid;
191 		struct wl1271_link *lnk;
192 		for_each_set_bit(hlid, wlvif->ap.sta_hlid_map,
193 				 WL12XX_MAX_LINKS) {
194 			lnk = &wl->links[hlid];
195 			if (!lnk->ba_bitmap)
196 				continue;
197 
198 			ieee80211_stop_rx_ba_session(vif,
199 						     lnk->ba_bitmap,
200 						     lnk->addr);
201 		}
202 	}
203 }
204 
wl12xx_event_soft_gemini_sense(struct wl1271 * wl,u8 enable)205 static void wl12xx_event_soft_gemini_sense(struct wl1271 *wl,
206 					       u8 enable)
207 {
208 	struct ieee80211_vif *vif;
209 	struct wl12xx_vif *wlvif;
210 
211 	if (enable) {
212 		/* disable dynamic PS when requested by the firmware */
213 		wl12xx_for_each_wlvif_sta(wl, wlvif) {
214 			vif = wl12xx_wlvif_to_vif(wlvif);
215 			ieee80211_disable_dyn_ps(vif);
216 		}
217 		set_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags);
218 	} else {
219 		clear_bit(WL1271_FLAG_SOFT_GEMINI, &wl->flags);
220 		wl12xx_for_each_wlvif_sta(wl, wlvif) {
221 			vif = wl12xx_wlvif_to_vif(wlvif);
222 			ieee80211_enable_dyn_ps(vif);
223 			wl1271_recalc_rx_streaming(wl, wlvif);
224 		}
225 	}
226 
227 }
228 
wl1271_event_mbox_dump(struct event_mailbox * mbox)229 static void wl1271_event_mbox_dump(struct event_mailbox *mbox)
230 {
231 	wl1271_debug(DEBUG_EVENT, "MBOX DUMP:");
232 	wl1271_debug(DEBUG_EVENT, "\tvector: 0x%x", mbox->events_vector);
233 	wl1271_debug(DEBUG_EVENT, "\tmask: 0x%x", mbox->events_mask);
234 }
235 
wl1271_event_process(struct wl1271 * wl,struct event_mailbox * mbox)236 static int wl1271_event_process(struct wl1271 *wl, struct event_mailbox *mbox)
237 {
238 	struct ieee80211_vif *vif;
239 	struct wl12xx_vif *wlvif;
240 	int ret;
241 	u32 vector;
242 	bool beacon_loss = false;
243 	bool disconnect_sta = false;
244 	unsigned long sta_bitmap = 0;
245 
246 	wl1271_event_mbox_dump(mbox);
247 
248 	vector = le32_to_cpu(mbox->events_vector);
249 	vector &= ~(le32_to_cpu(mbox->events_mask));
250 	wl1271_debug(DEBUG_EVENT, "vector: 0x%x", vector);
251 
252 	if (vector & SCAN_COMPLETE_EVENT_ID) {
253 		wl1271_debug(DEBUG_EVENT, "status: 0x%x",
254 			     mbox->scheduled_scan_status);
255 
256 		wl1271_scan_stm(wl, wl->scan_vif);
257 	}
258 
259 	if (vector & PERIODIC_SCAN_REPORT_EVENT_ID) {
260 		wl1271_debug(DEBUG_EVENT, "PERIODIC_SCAN_REPORT_EVENT "
261 			     "(status 0x%0x)", mbox->scheduled_scan_status);
262 
263 		wl1271_scan_sched_scan_results(wl);
264 	}
265 
266 	if (vector & PERIODIC_SCAN_COMPLETE_EVENT_ID) {
267 		wl1271_debug(DEBUG_EVENT, "PERIODIC_SCAN_COMPLETE_EVENT "
268 			     "(status 0x%0x)", mbox->scheduled_scan_status);
269 		if (wl->sched_scanning) {
270 			ieee80211_sched_scan_stopped(wl->hw);
271 			wl->sched_scanning = false;
272 		}
273 	}
274 
275 	if (vector & SOFT_GEMINI_SENSE_EVENT_ID)
276 		wl12xx_event_soft_gemini_sense(wl,
277 					       mbox->soft_gemini_sense_info);
278 
279 	/*
280 	 * The BSS_LOSE_EVENT_ID is only needed while psm (and hence beacon
281 	 * filtering) is enabled. Without PSM, the stack will receive all
282 	 * beacons and can detect beacon loss by itself.
283 	 *
284 	 * As there's possibility that the driver disables PSM before receiving
285 	 * BSS_LOSE_EVENT, beacon loss has to be reported to the stack.
286 	 *
287 	 */
288 	if (vector & BSS_LOSE_EVENT_ID) {
289 		/* TODO: check for multi-role */
290 		wl1271_info("Beacon loss detected.");
291 
292 		/* indicate to the stack, that beacons have been lost */
293 		beacon_loss = true;
294 	}
295 
296 	if (vector & PS_REPORT_EVENT_ID) {
297 		wl1271_debug(DEBUG_EVENT, "PS_REPORT_EVENT");
298 		wl12xx_for_each_wlvif_sta(wl, wlvif) {
299 			ret = wl1271_event_ps_report(wl, wlvif,
300 						     mbox, &beacon_loss);
301 			if (ret < 0)
302 				return ret;
303 		}
304 	}
305 
306 	if (vector & PSPOLL_DELIVERY_FAILURE_EVENT_ID)
307 		wl12xx_for_each_wlvif_sta(wl, wlvif) {
308 			wl1271_event_pspoll_delivery_fail(wl, wlvif);
309 		}
310 
311 	if (vector & RSSI_SNR_TRIGGER_0_EVENT_ID) {
312 		/* TODO: check actual multi-role support */
313 		wl1271_debug(DEBUG_EVENT, "RSSI_SNR_TRIGGER_0_EVENT");
314 		wl12xx_for_each_wlvif_sta(wl, wlvif) {
315 			wl1271_event_rssi_trigger(wl, wlvif, mbox);
316 		}
317 	}
318 
319 	if (vector & BA_SESSION_RX_CONSTRAINT_EVENT_ID) {
320 		u8 role_id = mbox->role_id;
321 		wl1271_debug(DEBUG_EVENT, "BA_SESSION_RX_CONSTRAINT_EVENT_ID. "
322 			     "ba_allowed = 0x%x, role_id=%d",
323 			     mbox->rx_ba_allowed, role_id);
324 
325 		wl12xx_for_each_wlvif(wl, wlvif) {
326 			if (role_id != 0xff && role_id != wlvif->role_id)
327 				continue;
328 
329 			wlvif->ba_allowed = !!mbox->rx_ba_allowed;
330 			if (!wlvif->ba_allowed)
331 				wl1271_stop_ba_event(wl, wlvif);
332 		}
333 	}
334 
335 	if (vector & CHANNEL_SWITCH_COMPLETE_EVENT_ID) {
336 		wl1271_debug(DEBUG_EVENT, "CHANNEL_SWITCH_COMPLETE_EVENT_ID. "
337 					  "status = 0x%x",
338 					  mbox->channel_switch_status);
339 		/*
340 		 * That event uses for two cases:
341 		 * 1) channel switch complete with status=0
342 		 * 2) channel switch failed status=1
343 		 */
344 
345 		/* TODO: configure only the relevant vif */
346 		wl12xx_for_each_wlvif_sta(wl, wlvif) {
347 			struct ieee80211_vif *vif = wl12xx_wlvif_to_vif(wlvif);
348 			bool success;
349 
350 			if (!test_and_clear_bit(WLVIF_FLAG_CS_PROGRESS,
351 						&wl->flags))
352 				continue;
353 
354 			success = mbox->channel_switch_status ? false : true;
355 			ieee80211_chswitch_done(vif, success);
356 		}
357 	}
358 
359 	if ((vector & DUMMY_PACKET_EVENT_ID)) {
360 		wl1271_debug(DEBUG_EVENT, "DUMMY_PACKET_ID_EVENT_ID");
361 		wl1271_tx_dummy_packet(wl);
362 	}
363 
364 	/*
365 	 * "TX retries exceeded" has a different meaning according to mode.
366 	 * In AP mode the offending station is disconnected.
367 	 */
368 	if (vector & MAX_TX_RETRY_EVENT_ID) {
369 		wl1271_debug(DEBUG_EVENT, "MAX_TX_RETRY_EVENT_ID");
370 		sta_bitmap |= le16_to_cpu(mbox->sta_tx_retry_exceeded);
371 		disconnect_sta = true;
372 	}
373 
374 	if (vector & INACTIVE_STA_EVENT_ID) {
375 		wl1271_debug(DEBUG_EVENT, "INACTIVE_STA_EVENT_ID");
376 		sta_bitmap |= le16_to_cpu(mbox->sta_aging_status);
377 		disconnect_sta = true;
378 	}
379 
380 	if (disconnect_sta) {
381 		u32 num_packets = wl->conf.tx.max_tx_retries;
382 		struct ieee80211_sta *sta;
383 		const u8 *addr;
384 		int h;
385 
386 		for_each_set_bit(h, &sta_bitmap, WL12XX_MAX_LINKS) {
387 			bool found = false;
388 			/* find the ap vif connected to this sta */
389 			wl12xx_for_each_wlvif_ap(wl, wlvif) {
390 				if (!test_bit(h, wlvif->ap.sta_hlid_map))
391 					continue;
392 				found = true;
393 				break;
394 			}
395 			if (!found)
396 				continue;
397 
398 			vif = wl12xx_wlvif_to_vif(wlvif);
399 			addr = wl->links[h].addr;
400 
401 			rcu_read_lock();
402 			sta = ieee80211_find_sta(vif, addr);
403 			if (sta) {
404 				wl1271_debug(DEBUG_EVENT, "remove sta %d", h);
405 				ieee80211_report_low_ack(sta, num_packets);
406 			}
407 			rcu_read_unlock();
408 		}
409 	}
410 
411 	if (beacon_loss)
412 		wl12xx_for_each_wlvif_sta(wl, wlvif) {
413 			vif = wl12xx_wlvif_to_vif(wlvif);
414 			ieee80211_connection_loss(vif);
415 		}
416 
417 	return 0;
418 }
419 
wl1271_event_unmask(struct wl1271 * wl)420 int wl1271_event_unmask(struct wl1271 *wl)
421 {
422 	int ret;
423 
424 	ret = wl1271_acx_event_mbox_mask(wl, ~(wl->event_mask));
425 	if (ret < 0)
426 		return ret;
427 
428 	return 0;
429 }
430 
wl1271_event_mbox_config(struct wl1271 * wl)431 void wl1271_event_mbox_config(struct wl1271 *wl)
432 {
433 	wl->mbox_ptr[0] = wl1271_read32(wl, REG_EVENT_MAILBOX_PTR);
434 	wl->mbox_ptr[1] = wl->mbox_ptr[0] + sizeof(struct event_mailbox);
435 
436 	wl1271_debug(DEBUG_EVENT, "MBOX ptrs: 0x%x 0x%x",
437 		     wl->mbox_ptr[0], wl->mbox_ptr[1]);
438 }
439 
wl1271_event_handle(struct wl1271 * wl,u8 mbox_num)440 int wl1271_event_handle(struct wl1271 *wl, u8 mbox_num)
441 {
442 	struct event_mailbox mbox;
443 	int ret;
444 
445 	wl1271_debug(DEBUG_EVENT, "EVENT on mbox %d", mbox_num);
446 
447 	if (mbox_num > 1)
448 		return -EINVAL;
449 
450 	/* first we read the mbox descriptor */
451 	wl1271_read(wl, wl->mbox_ptr[mbox_num], &mbox,
452 		    sizeof(struct event_mailbox), false);
453 
454 	/* process the descriptor */
455 	ret = wl1271_event_process(wl, &mbox);
456 	if (ret < 0)
457 		return ret;
458 
459 	/* then we let the firmware know it can go on...*/
460 	wl1271_write32(wl, ACX_REG_INTERRUPT_TRIG, INTR_TRIG_EVENT_ACK);
461 
462 	return 0;
463 }
464