work.c 8.47 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * mac80211 work implementation
 *
 * Copyright 2003-2008, Jouni Malinen <j@w1.fi>
 * Copyright 2004, Instant802 Networks, Inc.
 * Copyright 2005, Devicescape Software, Inc.
 * Copyright 2006-2007	Jiri Benc <jbenc@suse.cz>
 * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
 * Copyright 2009, Johannes Berg <johannes@sipsolutions.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/delay.h>
#include <linux/if_ether.h>
#include <linux/skbuff.h>
#include <linux/if_arp.h>
#include <linux/etherdevice.h>
#include <linux/crc32.h>
22
#include <linux/slab.h>
23 24 25 26 27
#include <net/mac80211.h>
#include <asm/unaligned.h>

#include "ieee80211_i.h"
#include "rate.h"
28
#include "driver-ops.h"
29 30 31 32 33 34 35 36 37 38

enum work_action {
	WORK_ACT_NONE,
	WORK_ACT_TIMEOUT,
};


/* utils */
static inline void ASSERT_WORK_MTX(struct ieee80211_local *local)
{
39
	lockdep_assert_held(&local->mtx);
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
}

/*
 * We can have multiple work items (and connection probing)
 * scheduling this timer, but we need to take care to only
 * reschedule it when it should fire _earlier_ than it was
 * asked for before, or if it's not pending right now. This
 * function ensures that. Note that it then is required to
 * run this function for all timeouts after the first one
 * has happened -- the work that runs from this timer will
 * do that.
 */
static void run_again(struct ieee80211_local *local,
		      unsigned long timeout)
{
	ASSERT_WORK_MTX(local);

	if (!timer_pending(&local->work_timer) ||
	    time_before(timeout, local->work_timer.expires))
		mod_timer(&local->work_timer, timeout);
}

void free_work(struct ieee80211_work *wk)
{
64
	kfree_rcu(wk, rcu_head);
65 66
}

67 68 69 70 71 72 73
static enum work_action __must_check
ieee80211_remain_on_channel_timeout(struct ieee80211_work *wk)
{
	/*
	 * First time we run, do nothing -- the generic code will
	 * have switched to the right channel etc.
	 */
74
	if (!wk->started) {
75
		wk->timeout = jiffies + msecs_to_jiffies(wk->remain.duration);
76

77 78 79
		cfg80211_ready_on_channel(wk->sdata->dev, (unsigned long) wk,
					  wk->chan, wk->chan_type,
					  wk->remain.duration, GFP_KERNEL);
80

81
		return WORK_ACT_NONE;
82 83
	}

84
	return WORK_ACT_TIMEOUT;
85 86
}

87 88 89 90 91 92 93 94 95
static enum work_action __must_check
ieee80211_offchannel_tx(struct ieee80211_work *wk)
{
	if (!wk->started) {
		wk->timeout = jiffies + msecs_to_jiffies(wk->offchan_tx.wait);

		/*
		 * After this, offchan_tx.frame remains but now is no
		 * longer a valid pointer -- we still need it as the
96
		 * cookie for canceling this work/status matching.
97 98 99 100 101 102 103 104 105
		 */
		ieee80211_tx_skb(wk->sdata, wk->offchan_tx.frame);

		return WORK_ACT_NONE;
	}

	return WORK_ACT_TIMEOUT;
}

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
static void ieee80211_work_timer(unsigned long data)
{
	struct ieee80211_local *local = (void *) data;

	if (local->quiescing)
		return;

	ieee80211_queue_work(&local->hw, &local->work_work);
}

static void ieee80211_work_work(struct work_struct *work)
{
	struct ieee80211_local *local =
		container_of(work, struct ieee80211_local, work_work);
	struct ieee80211_work *wk, *tmp;
	LIST_HEAD(free_work);
	enum work_action rma;
123
	bool remain_off_channel = false;
124 125 126 127 128 129

	if (local->scanning)
		return;

	/*
	 * ieee80211_queue_work() should have picked up most cases,
130
	 * here we'll pick the rest.
131 132 133 134
	 */
	if (WARN(local->suspended, "work scheduled while going to suspend\n"))
		return;

135
	mutex_lock(&local->mtx);
136

137 138
	ieee80211_recalc_idle(local);

139
	list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
140 141
		bool started = wk->started;

142
		/* mark work as started if it's on the current off-channel */
143
		if (!started && local->tmp_channel &&
144 145
		    wk->chan == local->tmp_channel &&
		    wk->chan_type == local->tmp_channel_type) {
146
			started = true;
Johannes Berg's avatar
Johannes Berg committed
147
			wk->timeout = jiffies;
148 149
		}

150
		if (!started && !local->tmp_channel) {
151
			ieee80211_offchannel_stop_vifs(local, true);
152

153
			local->tmp_channel = wk->chan;
154 155 156
			local->tmp_channel_type = wk->chan_type;

			ieee80211_hw_config(local, 0);
157

158
			started = true;
159 160 161 162
			wk->timeout = jiffies;
		}

		/* don't try to work with items that aren't started */
163
		if (!started)
164 165
			continue;

166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
		if (time_is_after_jiffies(wk->timeout)) {
			/*
			 * This work item isn't supposed to be worked on
			 * right now, but take care to adjust the timer
			 * properly.
			 */
			run_again(local, wk->timeout);
			continue;
		}

		switch (wk->type) {
		default:
			WARN_ON(1);
			/* nothing */
			rma = WORK_ACT_NONE;
			break;
182 183
		case IEEE80211_WORK_ABORT:
			rma = WORK_ACT_TIMEOUT;
184
			break;
185 186 187
		case IEEE80211_WORK_REMAIN_ON_CHANNEL:
			rma = ieee80211_remain_on_channel_timeout(wk);
			break;
188 189 190
		case IEEE80211_WORK_OFFCHANNEL_TX:
			rma = ieee80211_offchannel_tx(wk);
			break;
191 192
		}

193 194
		wk->started = started;

195 196
		switch (rma) {
		case WORK_ACT_NONE:
197 198
			/* might have changed the timeout */
			run_again(local, wk->timeout);
199 200 201 202 203 204 205 206 207 208 209
			break;
		case WORK_ACT_TIMEOUT:
			list_del_rcu(&wk->list);
			synchronize_rcu();
			list_add(&wk->list, &free_work);
			break;
		default:
			WARN(1, "unexpected: %d", rma);
		}
	}

210 211 212
	list_for_each_entry(wk, &local->work_list, list) {
		if (!wk->started)
			continue;
213 214
		if (wk->chan != local->tmp_channel ||
		    wk->chan_type != local->tmp_channel_type)
215 216 217 218 219 220
			continue;
		remain_off_channel = true;
	}

	if (!remain_off_channel && local->tmp_channel) {
		local->tmp_channel = NULL;
221
		ieee80211_hw_config(local, 0);
222

223
		ieee80211_offchannel_return(local, true);
224

225 226 227 228
		/* give connection some time to breathe */
		run_again(local, jiffies + HZ/2);
	}

229 230
	if (list_empty(&local->work_list) && local->scan_req &&
	    !local->scanning)
231 232 233 234
		ieee80211_queue_delayed_work(&local->hw,
					     &local->scan_work,
					     round_jiffies_relative(0));

235 236
	ieee80211_recalc_idle(local);

237 238
	mutex_unlock(&local->mtx);

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
	list_for_each_entry_safe(wk, tmp, &free_work, list) {
		wk->done(wk, NULL);
		list_del(&wk->list);
		kfree(wk);
	}
}

void ieee80211_add_work(struct ieee80211_work *wk)
{
	struct ieee80211_local *local;

	if (WARN_ON(!wk->chan))
		return;

	if (WARN_ON(!wk->sdata))
		return;

	if (WARN_ON(!wk->done))
		return;

Johannes Berg's avatar
Johannes Berg committed
259 260 261
	if (WARN_ON(!ieee80211_sdata_running(wk->sdata)))
		return;

262
	wk->started = false;
263 264

	local = wk->sdata->local;
265
	mutex_lock(&local->mtx);
266
	list_add_tail(&wk->list, &local->work_list);
267
	mutex_unlock(&local->mtx);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282

	ieee80211_queue_work(&local->hw, &local->work_work);
}

void ieee80211_work_init(struct ieee80211_local *local)
{
	INIT_LIST_HEAD(&local->work_list);
	setup_timer(&local->work_timer, ieee80211_work_timer,
		    (unsigned long)local);
	INIT_WORK(&local->work_work, ieee80211_work_work);
}

void ieee80211_work_purge(struct ieee80211_sub_if_data *sdata)
{
	struct ieee80211_local *local = sdata->local;
283
	struct ieee80211_work *wk;
284
	bool cleanup = false;
285

286
	mutex_lock(&local->mtx);
287
	list_for_each_entry(wk, &local->work_list, list) {
288 289
		if (wk->sdata != sdata)
			continue;
290
		cleanup = true;
291
		wk->type = IEEE80211_WORK_ABORT;
292 293
		wk->started = true;
		wk->timeout = jiffies;
294
	}
295
	mutex_unlock(&local->mtx);
296 297

	/* run cleanups etc. */
298 299
	if (cleanup)
		ieee80211_work_work(&local->work_work);
300

301
	mutex_lock(&local->mtx);
302 303 304 305 306
	list_for_each_entry(wk, &local->work_list, list) {
		if (wk->sdata != sdata)
			continue;
		WARN_ON(1);
		break;
307
	}
308
	mutex_unlock(&local->mtx);
309 310
}

311 312 313 314 315 316
static enum work_done_result ieee80211_remain_done(struct ieee80211_work *wk,
						   struct sk_buff *skb)
{
	/*
	 * We are done serving the remain-on-channel command.
	 */
317
	cfg80211_remain_on_channel_expired(wk->sdata->dev, (unsigned long) wk,
318 319 320 321 322 323
					   wk->chan, wk->chan_type,
					   GFP_KERNEL);

	return WORK_DONE_DESTROY;
}

324 325 326 327 328 329 330 331 332 333 334 335 336
int ieee80211_wk_remain_on_channel(struct ieee80211_sub_if_data *sdata,
				   struct ieee80211_channel *chan,
				   enum nl80211_channel_type channel_type,
				   unsigned int duration, u64 *cookie)
{
	struct ieee80211_work *wk;

	wk = kzalloc(sizeof(*wk), GFP_KERNEL);
	if (!wk)
		return -ENOMEM;

	wk->type = IEEE80211_WORK_REMAIN_ON_CHANNEL;
	wk->chan = chan;
337
	wk->chan_type = channel_type;
338
	wk->sdata = sdata;
339
	wk->done = ieee80211_remain_done;
340

341
	wk->remain.duration = duration;
342

343
	*cookie = (unsigned long) wk;
344 345 346 347 348 349 350 351 352 353 354 355 356

	ieee80211_add_work(wk);

	return 0;
}

int ieee80211_wk_cancel_remain_on_channel(struct ieee80211_sub_if_data *sdata,
					  u64 cookie)
{
	struct ieee80211_local *local = sdata->local;
	struct ieee80211_work *wk, *tmp;
	bool found = false;

357
	mutex_lock(&local->mtx);
358
	list_for_each_entry_safe(wk, tmp, &local->work_list, list) {
359
		if ((unsigned long) wk == cookie) {
360
			wk->timeout = jiffies;
361 362 363 364
			found = true;
			break;
		}
	}
365
	mutex_unlock(&local->mtx);
366 367 368 369

	if (!found)
		return -ENOENT;

370
	ieee80211_queue_work(&local->hw, &local->work_work);
371 372 373

	return 0;
}