summaryrefslogtreecommitdiffstats
path: root/kernel/drivers/md/dm-cache-policy-cleaner.c
blob: 004e463c9423cd7fe81d8f2256b63877b02aebf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * Copyright (C) 2012 Red Hat. All rights reserved.
 *
 * writeback cache policy supporting flushing out dirty cache blocks.
 *
 * This file is released under the GPL.
 */

#include "dm-cache-policy.h"
#include "dm.h"

#include <linux/hash.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>

/*----------------------------------------------------------------*/

#define DM_MSG_PREFIX "cache cleaner"

/* Cache entry struct. */
struct wb_cache_entry {
	struct list_head list;
	struct hlist_node hlist;

	dm_oblock_t oblock;
	dm_cblock_t cblock;
	bool dirty:1;
	bool pending:1;
};

struct hash {
	struct hlist_head *table;
	dm_block_t hash_bits;
	unsigned nr_buckets;
};

struct policy {
	struct dm_cache_policy policy;
	spinlock_t lock;

	struct list_head free;
	struct list_head clean;
	struct list_head clean_pending;
	struct list_head dirty;

	/*
	 * We know exactly how many cblocks will be needed,
	 * so we can allocate them up front.
	 */
	dm_cblock_t cache_size, nr_cblocks_allocated;
	struct wb_cache_entry *cblocks;
	struct hash chash;
};

/*----------------------------------------------------------------------------*/

/*
 * Low-level functions.
 */
static unsigned next_power(unsigned n, unsigned min)
{
	return roundup_pow_of_two(max(n, min));
}

static struct policy *to_policy(struct dm_cache_policy *p)
{
	return container_of(p, struct policy, policy);
}

static struct list_head *list_pop(struct list_head *q)
{
	struct list_head *r = q->next;

	list_del(r);

	return r;
}

/*----------------------------------------------------------------------------*/

/* Allocate/free various resources. */
static int alloc_hash(struct hash *hash, unsigned elts)
{
	hash->nr_buckets = next_power(elts >> 4, 16);
	hash->hash_bits = ffs(hash->nr_buckets) - 1;
	hash->table = vzalloc(sizeof(*hash->table) * hash->nr_buckets);

	return hash->table ? 0 : -ENOMEM;
}

static void free_hash(struct hash *hash)
{
	vfree(hash->table);
}

static int alloc_cache_blocks_with_hash(struct policy *p, dm_cblock_t cache_size)
{
	int r = -ENOMEM;

	p->cblocks = vzalloc(sizeof(*p->cblocks) * from_cblock(cache_size));
	if (p->cblocks) {
		unsigned u = from_cblock(cache_size);

		while (u--)
			list_add(&p->cblocks[u].list, &p->free);

		p->nr_cblocks_allocated = 0;

		/* Cache entries hash. */
		r = alloc_hash(&p->chash, from_cblock(cache_size));
		if (r)
			vfree(p->cblocks);
	}

	return r;
}

static void free_cache_blocks_and_hash(struct policy *p)
{
	free_hash(&p->chash);
	vfree(p->cblocks);
}

static struct wb_cache_entry *alloc_cache_entry(struct policy *p)
{
	struct wb_cache_entry *e;

	BUG_ON(from_cblock(p->nr_cblocks_allocated) >= from_cblock(p->cache_size));

	e = list_entry(list_pop(&p->free), struct wb_cache_entry, list);
	p->nr_cblocks_allocated = to_cblock(from_cblock(p->nr_cblocks_allocated) + 1);

	return e;
}

/*----------------------------------------------------------------------------*/

/* Hash functions (lookup, insert, remove). */
static struct wb_cache_entry *lookup_cache_entry(struct policy *p, dm_oblock_t oblock)
{
	struct hash *hash = &p->chash;
	unsigned h = hash_64(from_oblock(oblock), hash->hash_bits);
	struct wb_cache_entry *cur;
	struct hlist_head *bucket = &hash->table[h];

	hlist_for_each_entry(cur, bucket, hlist) {
		if (cur->oblock == oblock) {
			/* Move upfront bucket for faster access. */
			hlist_del(&cur->hlist);
			hlist_add_head(&cur->hlist, bucket);
			return cur;
		}
	}

	return NULL;
}

static void insert_cache_hash_entry(struct policy *p, struct wb_cache_entry *e)
{
	unsigned h = hash_64(from_oblock(e->oblock), p->chash.hash_bits);

	hlist_add_head(&e->hlist, &p->chash.table[h]);
}

static void remove_cache_hash_entry(struct wb_cache_entry *e)
{
	hlist_del(&e->hlist);
}

/* Public interface (see dm-cache-policy.h */
static int wb_map(struct dm_cache_policy *pe, dm_oblock_t oblock,
		  bool can_block, bool can_migrate, bool discarded_oblock,
		  struct bio *bio, struct policy_locker *locker,
		  struct policy_result *result)
{
	struct policy *p = to_policy(pe);
	struct wb_cache_entry *e;
	unsigned long flags;

	result->op = POLICY_MISS;

	if (can_block)
		spin_lock_irqsave(&p->lock, flags);

	else if (!spin_trylock_irqsave(&p->lock, flags))
		return -EWOULDBLOCK;

	e = lookup_cache_entry(p, oblock);
	if (e) {
		result->op = POLICY_HIT;
		result->cblock = e->cblock;

	}

	spin_unlock_irqrestore(&p->lock, flags);

	return 0;
}

static int wb_lookup(struct dm_cache_policy *pe, dm_oblock_t oblock, dm_cblock_t *cblock)
{
	int r;
	struct policy *p = to_policy(pe);
	struct wb_cache_entry *e;
	unsigned long flags;

	if (!spin_trylock_irqsave(&p->lock, flags))
		return -EWOULDBLOCK;

	e = lookup_cache_entry(p, oblock);
	if (e) {
		*cblock = e->cblock;
		r = 0;

	} else
		r = -ENOENT;

	spin_unlock_irqrestore(&p->lock, flags);

	return r;
}

static void __set_clear_dirty(struct dm_cache_policy *pe, dm_oblock_t oblock, bool set)
{
	struct policy *p = to_policy(pe);
	struct wb_cache_entry *e;

	e = lookup_cache_entry(p, oblock);
	BUG_ON(!e);

	if (set) {
		if (!e->dirty) {
			e->dirty = true;
			list_move(&e->list, &p->dirty);
		}

	} else {
		if (e->dirty) {
			e->pending = false;
			e->dirty = false;
			list_move(&e->list, &p->clean);
		}
	}
}

static void wb_set_dirty(struct dm_cache_policy *pe, dm_oblock_t oblock)
{
	struct policy *p = to_policy(pe);
	unsigned long flags;

	spin_lock_irqsave(&p->lock, flags);
	__set_clear_dirty(pe, oblock, true);
	spin_unlock_irqrestore(&p->lock, flags);
}

static void wb_clear_dirty(struct dm_cache_policy *pe, dm_oblock_t oblock)
{
	struct policy *p = to_policy(pe);
	unsigned long flags;

	spin_lock_irqsave(&p->lock, flags);
	__set_clear_dirty(pe, oblock, false);
	spin_unlock_irqrestore(&p->lock, flags);
}

static void add_cache_entry(struct policy *p, struct wb_cache_entry *e)
{
	insert_cache_hash_entry(p, e);
	if (e->dirty)
		list_add(&e->list, &p->dirty);
	else
		list_add(&e->list, &p->clean);
}

static int wb_load_mapping(struct dm_cache_policy *pe,
			   dm_oblock_t oblock, dm_cblock_t cblock,
			   uint32_t hint, bool hint_valid)
{
	int r;
	struct policy *p = to_policy(pe);
	struct wb_cache_entry *e = alloc_cache_entry(p);

	if (e) {
		e->cblock = cblock;
		e->oblock = oblock;
		e->dirty = false; /* blocks default to clean */
		add_cache_entry(p, e);
		r = 0;

	} else
		r = -ENOMEM;

	return r;
}

static void wb_destroy(struct dm_cache_policy *pe)
{
	struct policy *p = to_policy(pe);

	free_cache_blocks_and_hash(p);
	kfree(p);
}

static struct wb_cache_entry *__wb_force_remove_mapping(struct policy *p, dm_oblock_t oblock)
{
	struct wb_cache_entry *r = lookup_cache_entry(p, oblock);

	BUG_ON(!r);

	remove_cache_hash_entry(r);
	list_del(&r->list);

	return r;
}

static void wb_remove_mapping(struct dm_cache_policy *pe, dm_oblock_t oblock)
{
	struct policy *p = to_policy(pe);
	struct wb_cache_entry *e;
	unsigned long flags;

	spin_lock_irqsave(&p->lock, flags);
	e = __wb_force_remove_mapping(p, oblock);
	list_add_tail(&e->list, &p->free);
	BUG_ON(!from_cblock(p->nr_cblocks_allocated));
	p->nr_cblocks_allocated = to_cblock(from_cblock(p->nr_cblocks_allocated) - 1);
	spin_unlock_irqrestore(&p->lock, flags);
}

static void wb_force_mapping(struct dm_cache_policy *pe,
				dm_oblock_t current_oblock, dm_oblock_t oblock)
{
	struct policy *p = to_policy(pe);
	struct wb_cache_entry *e;
	unsigned long flags;

	spin_lock_irqsave(&p->lock, flags);
	e = __wb_force_remove_mapping(p, current_oblock);
	e->oblock = oblock;
	add_cache_entry(p, e);
	spin_unlock_irqrestore(&p->lock, flags);
}

static struct wb_cache_entry *get_next_dirty_entry(struct policy *p)
{
	struct list_head *l;
	struct wb_cache_entry *r;

	if (list_empty(&p->dirty))
		return NULL;

	l = list_pop(&p->dirty);
	r = container_of(l, struct wb_cache_entry, list);
	list_add(l, &p->clean_pending);

	return r;
}

static int wb_writeback_work(struct dm_cache_policy *pe,
			     dm_oblock_t *oblock,
			     dm_cblock_t *cblock)
{
	int r = -ENOENT;
	struct policy *p = to_policy(pe);
	struct wb_cache_entry *e;
	unsigned long flags;

	spin_lock_irqsave(&p->lock, flags);

	e = get_next_dirty_entry(p);
	if (e) {
		*oblock = e->oblock;
		*cblock = e->cblock;
		r = 0;
	}

	spin_unlock_irqrestore(&p->lock, flags);

	return r;
}

static dm_cblock_t wb_residency(struct dm_cache_policy *pe)
{
	return to_policy(pe)->nr_cblocks_allocated;
}

/* Init the policy plugin interface function pointers. */
static void init_policy_functions(struct policy *p)
{
	p->policy.destroy = wb_destroy;
	p->policy.map = wb_map;
	p->policy.lookup = wb_lookup;
	p->policy.set_dirty = wb_set_dirty;
	p->policy.clear_dirty = wb_clear_dirty;
	p->policy.load_mapping = wb_load_mapping;
	p->policy.walk_mappings = NULL;
	p->policy.remove_mapping = wb_remove_mapping;
	p->policy.writeback_work = wb_writeback_work;
	p->policy.force_mapping = wb_force_mapping;
	p->policy.residency = wb_residency;
	p->policy.tick = NULL;
}

static struct dm_cache_policy *wb_create(dm_cblock_t cache_size,
					 sector_t origin_size,
					 sector_t cache_block_size)
{
	int r;
	struct policy *p = kzalloc(sizeof(*p), GFP_KERNEL);

	if (!p)
		return NULL;

	init_policy_functions(p);
	INIT_LIST_HEAD(&p->free);
	INIT_LIST_HEAD(&p->clean);
	INIT_LIST_HEAD(&p->clean_pending);
	INIT_LIST_HEAD(&p->dirty);

	p->cache_size = cache_size;
	spin_lock_init(&p->lock);

	/* Allocate cache entry structs and add them to free list. */
	r = alloc_cache_blocks_with_hash(p, cache_size);
	if (!r)
		return &p->policy;

	kfree(p);

	return NULL;
}
/*----------------------------------------------------------------------------*/

static struct dm_cache_policy_type wb_policy_type = {
	.name = "cleaner",
	.version = {1, 0, 0},
	.hint_size = 0,
	.owner = THIS_MODULE,
	.create = wb_create
};

static int __init wb_init(void)
{
	int r = dm_cache_policy_register(&wb_policy_type);

	if (r < 0)
		DMERR("register failed %d", r);
	else
		DMINFO("version %u.%u.%u loaded",
		       wb_policy_type.version[0],
		       wb_policy_type.version[1],
		       wb_policy_type.version[2]);

	return r;
}

static void __exit wb_exit(void)
{
	dm_cache_policy_unregister(&wb_policy_type);
}

module_init(wb_init);
module_exit(wb_exit);

MODULE_AUTHOR("Heinz Mauelshagen <dm-devel@redhat.com>");
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("cleaner cache policy");
ss="p">, priv->current_net_addr); /* * set BB and packet type at the same time * set Short Slot Time, xIFS, and RSPINF */ if (priv->bb_type == BB_TYPE_11A) priv->short_slot_time = true; else priv->short_slot_time = false; vnt_set_short_slot_time(priv); priv->radio_ctl = priv->eeprom[EEP_OFS_RADIOCTL]; if ((priv->radio_ctl & EEP_RADIOCTL_ENABLE) != 0) { status = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_GPIOCTL1, MESSAGE_REQUEST_MACREG, 1, &tmp); if (status != STATUS_SUCCESS) return false; if ((tmp & GPIO3_DATA) == 0) vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL1, GPIO3_INTMD); else vnt_mac_reg_bits_off(priv, MAC_REG_GPIOCTL1, GPIO3_INTMD); } vnt_mac_set_led(priv, LEDSTS_TMLEN, 0x38); vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW); vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL0, 0x01); vnt_radio_power_on(priv); dev_dbg(&priv->usb->dev, "<----INIbInitAdapter Exit\n"); return true; } static void vnt_free_tx_bufs(struct vnt_private *priv) { struct vnt_usb_send_context *tx_context; int ii; for (ii = 0; ii < priv->num_tx_context; ii++) { tx_context = priv->tx_context[ii]; /* deallocate URBs */ if (tx_context->urb) { usb_kill_urb(tx_context->urb); usb_free_urb(tx_context->urb); } kfree(tx_context); } } static void vnt_free_rx_bufs(struct vnt_private *priv) { struct vnt_rcb *rcb; int ii; for (ii = 0; ii < priv->num_rcb; ii++) { rcb = priv->rcb[ii]; if (!rcb) continue; /* deallocate URBs */ if (rcb->urb) { usb_kill_urb(rcb->urb); usb_free_urb(rcb->urb); } /* deallocate skb */ if (rcb->skb) dev_kfree_skb(rcb->skb); kfree(rcb); } } static void usb_device_reset(struct vnt_private *priv) { int status; status = usb_reset_device(priv->usb); if (status) dev_warn(&priv->usb->dev, "usb_device_reset fail status=%d\n", status); } static void vnt_free_int_bufs(struct vnt_private *priv) { kfree(priv->int_buf.data_buf); } static bool vnt_alloc_bufs(struct vnt_private *priv) { struct vnt_usb_send_context *tx_context; struct vnt_rcb *rcb; int ii; for (ii = 0; ii < priv->num_tx_context; ii++) { tx_context = kmalloc(sizeof(struct vnt_usb_send_context), GFP_KERNEL); if (tx_context == NULL) goto free_tx; priv->tx_context[ii] = tx_context; tx_context->priv = priv; tx_context->pkt_no = ii; /* allocate URBs */ tx_context->urb = usb_alloc_urb(0, GFP_ATOMIC); if (!tx_context->urb) { dev_err(&priv->usb->dev, "alloc tx urb failed\n"); goto free_tx; } tx_context->in_use = false; } for (ii = 0; ii < priv->num_rcb; ii++) { priv->rcb[ii] = kzalloc(sizeof(struct vnt_rcb), GFP_KERNEL); if (!priv->rcb[ii]) { dev_err(&priv->usb->dev, "failed to allocate rcb no %d\n", ii); goto free_rx_tx; } rcb = priv->rcb[ii]; rcb->priv = priv; /* allocate URBs */ rcb->urb = usb_alloc_urb(0, GFP_ATOMIC); if (rcb->urb == NULL) { dev_err(&priv->usb->dev, "Failed to alloc rx urb\n"); goto free_rx_tx; } rcb->skb = dev_alloc_skb(priv->rx_buf_sz); if (rcb->skb == NULL) goto free_rx_tx; rcb->in_use = false; /* submit rx urb */ if (vnt_submit_rx_urb(priv, rcb)) goto free_rx_tx; } priv->interrupt_urb = usb_alloc_urb(0, GFP_ATOMIC); if (priv->interrupt_urb == NULL) { dev_err(&priv->usb->dev, "Failed to alloc int urb\n"); goto free_rx_tx; } priv->int_buf.data_buf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL); if (priv->int_buf.data_buf == NULL) { usb_free_urb(priv->interrupt_urb); goto free_rx_tx; } return true; free_rx_tx: vnt_free_rx_bufs(priv); free_tx: vnt_free_tx_bufs(priv); return false; } static void vnt_tx_80211(struct ieee80211_hw *hw, struct ieee80211_tx_control *control, struct sk_buff *skb) { struct vnt_private *priv = hw->priv; ieee80211_stop_queues(hw); if (vnt_tx_packet(priv, skb)) { ieee80211_free_txskb(hw, skb); ieee80211_wake_queues(hw); } } static int vnt_start(struct ieee80211_hw *hw) { struct vnt_private *priv = hw->priv; priv->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS; if (vnt_alloc_bufs(priv) == false) { dev_dbg(&priv->usb->dev, "vnt_alloc_bufs fail...\n"); return -ENOMEM; } clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags); if (vnt_init_registers(priv) == false) { dev_dbg(&priv->usb->dev, " init register fail\n"); goto free_all; } priv->int_interval = 1; /* bInterval is set to 1 */ vnt_int_start_interrupt(priv); ieee80211_wake_queues(hw); return 0; free_all: vnt_free_rx_bufs(priv); vnt_free_tx_bufs(priv); vnt_free_int_bufs(priv); usb_kill_urb(priv->interrupt_urb); usb_free_urb(priv->interrupt_urb); return -ENOMEM; } static void vnt_stop(struct ieee80211_hw *hw) { struct vnt_private *priv = hw->priv; int i; if (!priv) return; for (i = 0; i < MAX_KEY_TABLE; i++) vnt_mac_disable_keyentry(priv, i); /* clear all keys */ priv->key_entry_inuse = 0; if (!test_bit(DEVICE_FLAGS_UNPLUG, &priv->flags)) vnt_mac_shutdown(priv); ieee80211_stop_queues(hw); set_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags); cancel_delayed_work_sync(&priv->run_command_work); priv->cmd_running = false; vnt_free_tx_bufs(priv); vnt_free_rx_bufs(priv); vnt_free_int_bufs(priv); usb_kill_urb(priv->interrupt_urb); usb_free_urb(priv->interrupt_urb); } static int vnt_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif) { struct vnt_private *priv = hw->priv; priv->vif = vif; switch (vif->type) { case NL80211_IFTYPE_STATION: break; case NL80211_IFTYPE_ADHOC: vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST); vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC); break; case NL80211_IFTYPE_AP: vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST); vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_AP); break; default: return -EOPNOTSUPP; } priv->op_mode = vif->type; vnt_set_bss_mode(priv); /* LED blink on TX */ vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_INTER); return 0; } static void vnt_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif) { struct vnt_private *priv = hw->priv; switch (vif->type) { case NL80211_IFTYPE_STATION: break; case NL80211_IFTYPE_ADHOC: vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX); vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN); vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC); break; case NL80211_IFTYPE_AP: vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX); vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN); vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_AP); break; default: break; } vnt_radio_power_off(priv); priv->op_mode = NL80211_IFTYPE_UNSPECIFIED; /* LED slow blink */ vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW); } static int vnt_config(struct ieee80211_hw *hw, u32 changed) { struct vnt_private *priv = hw->priv; struct ieee80211_conf *conf = &hw->conf; u8 bb_type; if (changed & IEEE80211_CONF_CHANGE_PS) { if (conf->flags & IEEE80211_CONF_PS) vnt_enable_power_saving(priv, conf->listen_interval); else vnt_disable_power_saving(priv); } if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) || (conf->flags & IEEE80211_CONF_OFFCHANNEL)) { vnt_set_channel(priv, conf->chandef.chan->hw_value); if (conf->chandef.chan->band == IEEE80211_BAND_5GHZ) bb_type = BB_TYPE_11A; else bb_type = BB_TYPE_11G; if (priv->bb_type != bb_type) { priv->bb_type = bb_type; vnt_set_bss_mode(priv); } } if (changed & IEEE80211_CONF_CHANGE_POWER) { if (priv->bb_type == BB_TYPE_11B) priv->current_rate = RATE_1M; else priv->current_rate = RATE_54M; vnt_rf_setpower(priv, priv->current_rate, conf->chandef.chan->hw_value); } return 0; } static void vnt_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif, struct ieee80211_bss_conf *conf, u32 changed) { struct vnt_private *priv = hw->priv; priv->current_aid = conf->aid; if (changed & BSS_CHANGED_BSSID && conf->bssid) vnt_mac_set_bssid_addr(priv, (u8 *)conf->bssid); if (changed & BSS_CHANGED_BASIC_RATES) { priv->basic_rates = conf->basic_rates; vnt_update_top_rates(priv); dev_dbg(&priv->usb->dev, "basic rates %x\n", conf->basic_rates); } if (changed & BSS_CHANGED_ERP_PREAMBLE) { if (conf->use_short_preamble) { vnt_mac_enable_barker_preamble_mode(priv); priv->preamble_type = true; } else { vnt_mac_disable_barker_preamble_mode(priv); priv->preamble_type = false; } } if (changed & BSS_CHANGED_ERP_CTS_PROT) { if (conf->use_cts_prot) vnt_mac_enable_protect_mode(priv); else vnt_mac_disable_protect_mode(priv); } if (changed & BSS_CHANGED_ERP_SLOT) { if (conf->use_short_slot) priv->short_slot_time = true; else priv->short_slot_time = false; vnt_set_short_slot_time(priv); vnt_set_vga_gain_offset(priv, priv->bb_vga[0]); vnt_update_pre_ed_threshold(priv, false); } if (changed & BSS_CHANGED_TXPOWER) vnt_rf_setpower(priv, priv->current_rate, conf->chandef.chan->hw_value); if (changed & BSS_CHANGED_BEACON_ENABLED) { dev_dbg(&priv->usb->dev, "Beacon enable %d\n", conf->enable_beacon); if (conf->enable_beacon) { vnt_beacon_enable(priv, vif, conf); vnt_mac_reg_bits_on(priv, MAC_REG_TCR, TCR_AUTOBCNTX); } else { vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX); } } } static u64 vnt_prepare_multicast(struct ieee80211_hw *hw, struct netdev_hw_addr_list *mc_list) { struct vnt_private *priv = hw->priv; struct netdev_hw_addr *ha; u64 mc_filter = 0; u32 bit_nr = 0; netdev_hw_addr_list_for_each(ha, mc_list) { bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26; mc_filter |= 1ULL << (bit_nr & 0x3f); } priv->mc_list_count = mc_list->count; return mc_filter; } static void vnt_configure(struct ieee80211_hw *hw, unsigned int changed_flags, unsigned int *total_flags, u64 multicast) { struct vnt_private *priv = hw->priv; u8 rx_mode = 0; int rc; *total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_PROMISC_IN_BSS | FIF_BCN_PRBRESP_PROMISC; rc = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_RCR, MESSAGE_REQUEST_MACREG, sizeof(u8), &rx_mode); if (!rc) rx_mode = RCR_MULTICAST | RCR_BROADCAST; dev_dbg(&priv->usb->dev, "rx mode in = %x\n", rx_mode); if (changed_flags & FIF_PROMISC_IN_BSS) { /* unconditionally log net taps */ if (*total_flags & FIF_PROMISC_IN_BSS) rx_mode |= RCR_UNICAST; else rx_mode &= ~RCR_UNICAST; } if (changed_flags & FIF_ALLMULTI) { if (*total_flags & FIF_ALLMULTI) { if (priv->mc_list_count > 2) vnt_mac_set_filter(priv, ~0); else vnt_mac_set_filter(priv, multicast); rx_mode |= RCR_MULTICAST | RCR_BROADCAST; } else { rx_mode &= ~(RCR_MULTICAST | RCR_BROADCAST); } } if (changed_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)) { if (*total_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)) rx_mode &= ~RCR_BSSID; else rx_mode |= RCR_BSSID; } vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG, MAC_REG_RCR, rx_mode); dev_dbg(&priv->usb->dev, "rx mode out= %x\n", rx_mode); } static int vnt_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd, struct ieee80211_vif *vif, struct ieee80211_sta *sta, struct ieee80211_key_conf *key) { struct vnt_private *priv = hw->priv; switch (cmd) { case SET_KEY: if (vnt_set_keys(hw, sta, vif, key)) return -EOPNOTSUPP; break; case DISABLE_KEY: if (test_bit(key->hw_key_idx, &priv->key_entry_inuse)) clear_bit(key->hw_key_idx, &priv->key_entry_inuse); default: break; } return 0; } static void vnt_sw_scan_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif, const u8 *addr) { struct vnt_private *priv = hw->priv; vnt_set_bss_mode(priv); /* Set max sensitivity*/ vnt_update_pre_ed_threshold(priv, true); } static void vnt_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif) { struct vnt_private *priv = hw->priv; /* Return sensitivity to channel level*/ vnt_update_pre_ed_threshold(priv, false); } static int vnt_get_stats(struct ieee80211_hw *hw, struct ieee80211_low_level_stats *stats) { struct vnt_private *priv = hw->priv; memcpy(stats, &priv->low_stats, sizeof(*stats)); return 0; } static u64 vnt_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif) { struct vnt_private *priv = hw->priv; return priv->current_tsf; } static void vnt_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif, u64 tsf) { struct vnt_private *priv = hw->priv; vnt_update_next_tbtt(priv, tsf, vif->bss_conf.beacon_int); } static void vnt_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif) { struct vnt_private *priv = hw->priv; vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN); vnt_clear_current_tsf(priv); } static const struct ieee80211_ops vnt_mac_ops = { .tx = vnt_tx_80211, .start = vnt_start, .stop = vnt_stop, .add_interface = vnt_add_interface, .remove_interface = vnt_remove_interface, .config = vnt_config, .bss_info_changed = vnt_bss_info_changed, .prepare_multicast = vnt_prepare_multicast, .configure_filter = vnt_configure, .set_key = vnt_set_key, .sw_scan_start = vnt_sw_scan_start, .sw_scan_complete = vnt_sw_scan_complete, .get_stats = vnt_get_stats, .get_tsf = vnt_get_tsf, .set_tsf = vnt_set_tsf, .reset_tsf = vnt_reset_tsf, }; int vnt_init(struct vnt_private *priv) { if (!(vnt_init_registers(priv))) return -EAGAIN; SET_IEEE80211_PERM_ADDR(priv->hw, priv->permanent_net_addr); vnt_init_bands(priv); if (ieee80211_register_hw(priv->hw)) return -ENODEV; priv->mac_hw = true; vnt_radio_power_off(priv); return 0; } static int vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id) { struct usb_device *udev; struct vnt_private *priv; struct ieee80211_hw *hw; struct wiphy *wiphy; int rc = 0; udev = usb_get_dev(interface_to_usbdev(intf)); dev_notice(&udev->dev, "%s Ver. %s\n", DEVICE_FULL_DRV_NAM, DEVICE_VERSION); dev_notice(&udev->dev, "Copyright (c) 2004 VIA Networking Technologies, Inc.\n"); hw = ieee80211_alloc_hw(sizeof(struct vnt_private), &vnt_mac_ops); if (!hw) { dev_err(&udev->dev, "could not register ieee80211_hw\n"); rc = -ENOMEM; goto err_nomem; } priv = hw->priv; priv->hw = hw; priv->usb = udev; vnt_set_options(priv); spin_lock_init(&priv->lock); mutex_init(&priv->usb_lock); INIT_DELAYED_WORK(&priv->run_command_work, vnt_run_command); usb_set_intfdata(intf, priv); wiphy = priv->hw->wiphy; wiphy->frag_threshold = FRAG_THRESH_DEF; wiphy->rts_threshold = RTS_THRESH_DEF; wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP); priv->hw->flags = IEEE80211_HW_RX_INCLUDES_FCS | IEEE80211_HW_REPORTS_TX_ACK_STATUS | IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_TIMING_BEACON_ONLY; priv->hw->max_signal = 100; SET_IEEE80211_DEV(priv->hw, &intf->dev); usb_device_reset(priv); clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags); vnt_reset_command_timer(priv); vnt_schedule_command(priv, WLAN_CMD_INIT_MAC80211); return 0; err_nomem: usb_put_dev(udev); return rc; } static void vt6656_disconnect(struct usb_interface *intf) { struct vnt_private *priv = usb_get_intfdata(intf); if (!priv) return; if (priv->mac_hw) ieee80211_unregister_hw(priv->hw); usb_set_intfdata(intf, NULL); usb_put_dev(interface_to_usbdev(intf)); set_bit(DEVICE_FLAGS_UNPLUG, &priv->flags); ieee80211_free_hw(priv->hw); } #ifdef CONFIG_PM static int vt6656_suspend(struct usb_interface *intf, pm_message_t message) { return 0; } static int vt6656_resume(struct usb_interface *intf) { return 0; } #endif /* CONFIG_PM */ MODULE_DEVICE_TABLE(usb, vt6656_table); static struct usb_driver vt6656_driver = { .name = DEVICE_NAME, .probe = vt6656_probe, .disconnect = vt6656_disconnect, .id_table = vt6656_table, #ifdef CONFIG_PM .suspend = vt6656_suspend, .resume = vt6656_resume, #endif /* CONFIG_PM */ }; module_usb_driver(vt6656_driver);