aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/uwb/wlp/eda.c
blob: 69e020039718aed4711849b0b9fee3c80dfea866 (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
/*
 * WUSB Wire Adapter: WLP interface
 * Ethernet to device address cache
 *
 * Copyright (C) 2005-2006 Intel Corporation
 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 *
 *
 * We need to be able to map ethernet addresses to device addresses
 * and back because there is not explicit relationship between the eth
 * addresses used in the ETH frames and the device addresses (no, it
 * would not have been simpler to force as ETH address the MBOA MAC
 * address...no, not at all :).
 *
 * A device has one MBOA MAC address and one device address. It is possible
 * for a device to have more than one virtual MAC address (although a
 * virtual address can be the same as the MBOA MAC address). The device
 * address is guaranteed to be unique among the devices in the extended
 * beacon group (see ECMA 17.1.1). We thus use the device address as index
 * to this cache. We do allow searching based on virtual address as this
 * is how Ethernet frames will be addressed.
 *
 * We need to support virtual EUI-48. Although, right now the virtual
 * EUI-48 will always be the same as the MAC SAP address. The EDA cache
 * entry thus contains a MAC SAP address as well as the virtual address
 * (used to map the network stack address to a neighbor). When we move
 * to support more than one virtual MAC on a host then this organization
 * will have to change. Perhaps a neighbor has a list of WSSs, each with a
 * tag and virtual EUI-48.
 *
 * On data transmission
 * it is used to determine if the neighbor is connected and what WSS it
 * belongs to. With this we know what tag to add to the WLP frame. Storing
 * the WSS in the EDA cache may be overkill because we only support one
 * WSS. Hopefully we will support more than one WSS at some point.
 * On data reception it is used to determine the WSS based on
 * the tag and address of the transmitting neighbor.
 */

#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/wlp.h>
#include "wlp-internal.h"


/* FIXME: cache is not purged, only on device close */

/* FIXME: does not scale, change to dynamic array */

/*
 * Initialize the EDA cache
 *
 * @returns 0 if ok, < 0 errno code on error
 *
 * Call when the interface is being brought up
 *
 * NOTE: Keep it as a separate function as the implementation will
 *       change and be more complex.
 */
void wlp_eda_init(struct wlp_eda *eda)
{
	INIT_LIST_HEAD(&eda->cache);
	spin_lock_init(&eda->lock);
}

/*
 * Release the EDA cache
 *
 * @returns 0 if ok, < 0 errno code on error
 *
 * Called when the interface is brought down
 */
void wlp_eda_release(struct wlp_eda *eda)
{
	unsigned long flags;
	struct wlp_eda_node *itr, *next;

	spin_lock_irqsave(&eda->lock, flags);
	list_for_each_entry_safe(itr, next, &eda->cache, list_node) {
		list_del(&itr->list_node);
		kfree(itr);
	}
	spin_unlock_irqrestore(&eda->lock, flags);
}

/*
 * Add an address mapping
 *
 * @returns 0 if ok, < 0 errno code on error
 *
 * An address mapping is initially created when the neighbor device is seen
 * for the first time (it is "onair"). At this time the neighbor is not
 * connected or associated with a WSS so we only populate the Ethernet and
 * Device address fields.
 *
 */
int wlp_eda_create_node(struct wlp_eda *eda,
			const unsigned char eth_addr[ETH_ALEN],
			const struct uwb_dev_addr *dev_addr)
{
	int result = 0;
	struct wlp_eda_node *itr;
	unsigned long flags;

	BUG_ON(dev_addr == NULL || eth_addr == NULL);
	spin_lock_irqsave(&eda->lock, flags);
	list_for_each_entry(itr, &eda->cache, list_node) {
		if (!memcmp(&itr->dev_addr, dev_addr, sizeof(itr->dev_addr))) {
			printk(KERN_ERR "EDA cache already contains entry "
			       "for neighbor %02x:%02x\n",
			       dev_addr->data[1], dev_addr->data[0]);
			result = -EEXIST;
			goto out_unlock;
		}
	}
	itr = kzalloc(sizeof(*itr), GFP_ATOMIC);
	if (itr != NULL) {
		memcpy(itr->eth_addr, eth_addr, sizeof(itr->eth_addr));
		itr->dev_addr = *dev_addr;
		list_add(&itr->list_node, &eda->cache);
	} else
		result = -ENOMEM;
out_unlock:
	spin_unlock_irqrestore(&eda->lock, flags);
	return result;
}

/*
 * Remove entry from EDA cache
 *
 * This is done when the device goes off air.
 */
void wlp_eda_rm_node(struct wlp_eda *eda, const struct uwb_dev_addr *dev_addr)
{
	struct wlp_eda_node *itr, *next;
	unsigned long flags;

	spin_lock_irqsave(&eda->lock, flags);
	list_for_each_entry_safe(itr, next, &eda->cache, list_node) {
		if (!memcmp(&itr->dev_addr, dev_addr, sizeof(itr->dev_addr))) {
			list_del(&itr->list_node);
			kfree(itr);
			break;
		}
	}
	spin_unlock_irqrestore(&eda->lock, flags);
}

/*
 * Update an address mapping
 *
 * @returns 0 if ok, < 0 errno code on error
 */
int wlp_eda_update_node(struct wlp_eda *eda,
			const struct uwb_dev_addr *dev_addr,
			struct wlp_wss *wss,
			const unsigned char virt_addr[ETH_ALEN],
			const u8 tag, const enum wlp_wss_connect state)
{
	int result = -ENOENT;
	struct wlp_eda_node *itr;
	unsigned long flags;

	spin_lock_irqsave(&eda->lock, flags);
	list_for_each_entry(itr, &eda->cache, list_node) {
		if (!memcmp(&itr->dev_addr, dev_addr, sizeof(itr->dev_addr))) {
			/* Found it, update it */
			itr->wss = wss;
			memcpy(itr->virt_addr, virt_addr,
			       sizeof(itr->virt_addr));
			itr->tag = tag;
			itr->state = state;
			result = 0;
			goto out_unlock;
		}
	}
	/* Not found */
out_unlock:
	spin_unlock_irqrestore(&eda->lock, flags);
	return result;
}

/*
 * Update only state field of an address mapping
 *
 * @returns 0 if ok, < 0 errno code on error
 */
int wlp_eda_update_node_state(struct wlp_eda *eda,
			      const struct uwb_dev_addr *dev_addr,
			      const enum wlp_wss_connect state)
{
	int result = -ENOENT;
	struct wlp_eda_node *itr;
	unsigned long flags;

	spin_lock_irqsave(&eda->lock, flags);
	list_for_each_entry(itr, &eda->cache, list_node) {
		if (!memcmp(&itr->dev_addr, dev_addr, sizeof(itr->dev_addr))) {
			/* Found it, update it */
			itr->state = state;
			result = 0;
			goto out_unlock;
		}
	}
	/* Not found */
out_unlock:
	spin_unlock_irqrestore(&eda->lock, flags);
	return result;
}

/*
 * Return contents of EDA cache entry
 *
 * @dev_addr: index to EDA cache
 * @eda_entry: pointer to where contents of EDA cache will be copied
 */
int wlp_copy_eda_node(struct wlp_eda *eda, struct uwb_dev_addr *dev_addr,
		      struct wlp_eda_node *eda_entry)
{
	int result = -ENOENT;
	struct wlp_eda_node *itr;
	unsigned long flags;

	spin_lock_irqsave(&eda->lock, flags);
	list_for_each_entry(itr, &eda->cache, list_node) {
		if (!memcmp(&itr->dev_addr, dev_addr, sizeof(itr->dev_addr))) {
			*eda_entry = *itr;
			result = 0;
			goto out_unlock;
		}
	}
	/* Not found */
out_unlock:
	spin_unlock_irqrestore(&eda->lock, flags);
	return result;
}

/*
 * Execute function for every element in the cache
 *
 * @function: function to execute on element of cache (must be atomic)
 * @priv:     private data of function
 * @returns:  result of first function that failed, or last function
 *            executed if no function failed.
 *
 * Stop executing when function returns error for any element in cache.
 *
 * IMPORTANT: We are using a spinlock here: the function executed on each
 * element has to be atomic.
 */
int wlp_eda_for_each(struct wlp_eda *eda, wlp_eda_for_each_f function,
		     void *priv)
{
	int result = 0;
	struct wlp *wlp = container_of(eda, struct wlp, eda);
	struct wlp_eda_node *entry;
	unsigned long flags;

	spin_lock_irqsave(&eda->lock, flags);
	list_for_each_entry(entry, &eda->cache, list_node) {
		result = (*function)(wlp, entry, priv);
		if (result < 0)
			break;
	}
	spin_unlock_irqrestore(&eda->lock, flags);
	return result;
}

/*
 * Execute function for single element in the cache (return dev addr)
 *
 * @virt_addr: index into EDA cache used to determine which element to
 *             execute the function on
 * @dev_addr: device address of element in cache will be returned using
 *            @dev_addr
 * @function: function to execute on element of cache (must be atomic)
 * @priv:     private data of function
 * @returns:  result of function
 *
 * IMPORTANT: We are using a spinlock here: the function executed on the
 * element has to be atomic.
 */
int wlp_eda_for_virtual(struct wlp_eda *eda,
			const unsigned char virt_addr[ETH_ALEN],
			struct uwb_dev_addr *dev_addr,
			wlp_eda_for_each_f function,
			void *priv)
{
	int result = 0;
	struct wlp *wlp = container_of(eda, struct wlp, eda);
	struct wlp_eda_node *itr;
	unsigned long flags;
	int found = 0;

	spin_lock_irqsave(&eda->lock, flags);
	list_for_each_entry(itr, &eda->cache, list_node) {
		if (!memcmp(itr->virt_addr, virt_addr,
			   sizeof(itr->virt_addr))) {
			result = (*function)(wlp, itr, priv);
			*dev_addr = itr->dev_addr;
			found = 1;
			break;
		}
	}
	if (!found)
		result = -ENODEV;
	spin_unlock_irqrestore(&eda->lock, flags);
	return result;
}

static const char *__wlp_wss_connect_state[] = { "WLP_WSS_UNCONNECTED",
					  "WLP_WSS_CONNECTED",
					  "WLP_WSS_CONNECT_FAILED",
};

static const char *wlp_wss_connect_state_str(unsigned id)
{
	if (id >= ARRAY_SIZE(__wlp_wss_connect_state))
		return "unknown WSS connection state";
	return __wlp_wss_connect_state[id];
}

/*
 * View EDA cache from user space
 *
 * A debugging feature to give user visibility into the EDA cache. Also
 * used to display members of WSS to user (called from wlp_wss_members_show())
 */
ssize_t wlp_eda_show(struct wlp *wlp, char *buf)
{
	ssize_t result = 0;
	struct wlp_eda_node *entry;
	unsigned long flags;
	struct wlp_eda *eda = &wlp->eda;
	spin_lock_irqsave(&eda->lock, flags);
	result = scnprintf(buf, PAGE_SIZE, "#eth_addr dev_addr wss_ptr "
			   "tag state virt_addr\n");
	list_for_each_entry(entry, &eda->cache, list_node) {
		result += scnprintf(buf + result, PAGE_SIZE - result,
				    "%pM %02x:%02x %p 0x%02x %s %pM\n",
				    entry->eth_addr,
				    entry->dev_addr.data[1],
				    entry->dev_addr.data[0], entry->wss,
				    entry->tag,
				    wlp_wss_connect_state_str(entry->state),
				    entry->virt_addr);
		if (result >= PAGE_SIZE)
			break;
	}
	spin_unlock_irqrestore(&eda->lock, flags);
	return result;
}
EXPORT_SYMBOL_GPL(wlp_eda_show);

/*
 * Add new EDA cache entry based on user input in sysfs
 *
 * Should only be used for debugging.
 *
 * The WSS is assumed to be the only WSS supported. This needs to be
 * redesigned when we support more than one WSS.
 */
ssize_t wlp_eda_store(struct wlp *wlp, const char *buf, size_t size)
{
	ssize_t result;
	struct wlp_eda *eda = &wlp->eda;
	u8 eth_addr[6];
	struct uwb_dev_addr dev_addr;
	u8 tag;
	unsigned state;

	result = sscanf(buf, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx "
			"%02hhx:%02hhx %02hhx %u\n",
			&eth_addr[0], &eth_addr[1],
			&eth_addr[2], &eth_addr[3],
			&eth_addr[4], &eth_addr[5],
			&dev_addr.data[1], &dev_addr.data[0], &tag, &state);
	switch (result) {
	case 6: /* no dev addr specified -- remove entry NOT IMPLEMENTED */
		/*result = wlp_eda_rm(eda, eth_addr, &dev_addr);*/
		result = -ENOSYS;
		break;
	case 10:
		state = state >= 1 ? 1 : 0;
		result = wlp_eda_create_node(eda, eth_addr, &dev_addr);
		if (result < 0 && result != -EEXIST)
			goto error;
		/* Set virtual addr to be same as MAC */
		result = wlp_eda_update_node(eda, &dev_addr, &wlp->wss,
					     eth_addr, tag, state);
		if (result < 0)
			goto error;
		break;
	default: /* bad format */
		result = -EINVAL;
	}
error:
	return result < 0 ? result : size;
}
EXPORT_SYMBOL_GPL(wlp_eda_store);