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#include <linux/netdevice.h>
55#include <linux/etherdevice.h>
56#include <linux/wlp.h>
57#include "wlp-internal.h"
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74void wlp_eda_init(struct wlp_eda *eda)
75{
76 INIT_LIST_HEAD(&eda->cache);
77 spin_lock_init(&eda->lock);
78}
79
80
81
82
83
84
85
86
87void wlp_eda_release(struct wlp_eda *eda)
88{
89 unsigned long flags;
90 struct wlp_eda_node *itr, *next;
91
92 spin_lock_irqsave(&eda->lock, flags);
93 list_for_each_entry_safe(itr, next, &eda->cache, list_node) {
94 list_del(&itr->list_node);
95 kfree(itr);
96 }
97 spin_unlock_irqrestore(&eda->lock, flags);
98}
99
100
101
102
103
104
105
106
107
108
109
110
111int wlp_eda_create_node(struct wlp_eda *eda,
112 const unsigned char eth_addr[ETH_ALEN],
113 const struct uwb_dev_addr *dev_addr)
114{
115 int result = 0;
116 struct wlp_eda_node *itr;
117 unsigned long flags;
118
119 BUG_ON(dev_addr == NULL || eth_addr == NULL);
120 spin_lock_irqsave(&eda->lock, flags);
121 list_for_each_entry(itr, &eda->cache, list_node) {
122 if (!memcmp(&itr->dev_addr, dev_addr, sizeof(itr->dev_addr))) {
123 printk(KERN_ERR "EDA cache already contains entry "
124 "for neighbor %02x:%02x\n",
125 dev_addr->data[1], dev_addr->data[0]);
126 result = -EEXIST;
127 goto out_unlock;
128 }
129 }
130 itr = kzalloc(sizeof(*itr), GFP_ATOMIC);
131 if (itr != NULL) {
132 memcpy(itr->eth_addr, eth_addr, sizeof(itr->eth_addr));
133 itr->dev_addr = *dev_addr;
134 list_add(&itr->list_node, &eda->cache);
135 } else
136 result = -ENOMEM;
137out_unlock:
138 spin_unlock_irqrestore(&eda->lock, flags);
139 return result;
140}
141
142
143
144
145
146
147void wlp_eda_rm_node(struct wlp_eda *eda, const struct uwb_dev_addr *dev_addr)
148{
149 struct wlp_eda_node *itr, *next;
150 unsigned long flags;
151
152 spin_lock_irqsave(&eda->lock, flags);
153 list_for_each_entry_safe(itr, next, &eda->cache, list_node) {
154 if (!memcmp(&itr->dev_addr, dev_addr, sizeof(itr->dev_addr))) {
155 list_del(&itr->list_node);
156 kfree(itr);
157 break;
158 }
159 }
160 spin_unlock_irqrestore(&eda->lock, flags);
161}
162
163
164
165
166
167
168int wlp_eda_update_node(struct wlp_eda *eda,
169 const struct uwb_dev_addr *dev_addr,
170 struct wlp_wss *wss,
171 const unsigned char virt_addr[ETH_ALEN],
172 const u8 tag, const enum wlp_wss_connect state)
173{
174 int result = -ENOENT;
175 struct wlp_eda_node *itr;
176 unsigned long flags;
177
178 spin_lock_irqsave(&eda->lock, flags);
179 list_for_each_entry(itr, &eda->cache, list_node) {
180 if (!memcmp(&itr->dev_addr, dev_addr, sizeof(itr->dev_addr))) {
181
182 itr->wss = wss;
183 memcpy(itr->virt_addr, virt_addr,
184 sizeof(itr->virt_addr));
185 itr->tag = tag;
186 itr->state = state;
187 result = 0;
188 goto out_unlock;
189 }
190 }
191
192out_unlock:
193 spin_unlock_irqrestore(&eda->lock, flags);
194 return result;
195}
196
197
198
199
200
201
202int wlp_eda_update_node_state(struct wlp_eda *eda,
203 const struct uwb_dev_addr *dev_addr,
204 const enum wlp_wss_connect state)
205{
206 int result = -ENOENT;
207 struct wlp_eda_node *itr;
208 unsigned long flags;
209
210 spin_lock_irqsave(&eda->lock, flags);
211 list_for_each_entry(itr, &eda->cache, list_node) {
212 if (!memcmp(&itr->dev_addr, dev_addr, sizeof(itr->dev_addr))) {
213
214 itr->state = state;
215 result = 0;
216 goto out_unlock;
217 }
218 }
219
220out_unlock:
221 spin_unlock_irqrestore(&eda->lock, flags);
222 return result;
223}
224
225
226
227
228
229
230
231int wlp_copy_eda_node(struct wlp_eda *eda, struct uwb_dev_addr *dev_addr,
232 struct wlp_eda_node *eda_entry)
233{
234 int result = -ENOENT;
235 struct wlp_eda_node *itr;
236 unsigned long flags;
237
238 spin_lock_irqsave(&eda->lock, flags);
239 list_for_each_entry(itr, &eda->cache, list_node) {
240 if (!memcmp(&itr->dev_addr, dev_addr, sizeof(itr->dev_addr))) {
241 *eda_entry = *itr;
242 result = 0;
243 goto out_unlock;
244 }
245 }
246
247out_unlock:
248 spin_unlock_irqrestore(&eda->lock, flags);
249 return result;
250}
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265int wlp_eda_for_each(struct wlp_eda *eda, wlp_eda_for_each_f function,
266 void *priv)
267{
268 int result = 0;
269 struct wlp *wlp = container_of(eda, struct wlp, eda);
270 struct wlp_eda_node *entry;
271 unsigned long flags;
272
273 spin_lock_irqsave(&eda->lock, flags);
274 list_for_each_entry(entry, &eda->cache, list_node) {
275 result = (*function)(wlp, entry, priv);
276 if (result < 0)
277 break;
278 }
279 spin_unlock_irqrestore(&eda->lock, flags);
280 return result;
281}
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297int wlp_eda_for_virtual(struct wlp_eda *eda,
298 const unsigned char virt_addr[ETH_ALEN],
299 struct uwb_dev_addr *dev_addr,
300 wlp_eda_for_each_f function,
301 void *priv)
302{
303 int result = 0;
304 struct wlp *wlp = container_of(eda, struct wlp, eda);
305 struct wlp_eda_node *itr;
306 unsigned long flags;
307 int found = 0;
308
309 spin_lock_irqsave(&eda->lock, flags);
310 list_for_each_entry(itr, &eda->cache, list_node) {
311 if (!memcmp(itr->virt_addr, virt_addr,
312 sizeof(itr->virt_addr))) {
313 result = (*function)(wlp, itr, priv);
314 *dev_addr = itr->dev_addr;
315 found = 1;
316 break;
317 }
318 }
319 if (!found)
320 result = -ENODEV;
321 spin_unlock_irqrestore(&eda->lock, flags);
322 return result;
323}
324
325static const char *__wlp_wss_connect_state[] = { "WLP_WSS_UNCONNECTED",
326 "WLP_WSS_CONNECTED",
327 "WLP_WSS_CONNECT_FAILED",
328};
329
330static const char *wlp_wss_connect_state_str(unsigned id)
331{
332 if (id >= ARRAY_SIZE(__wlp_wss_connect_state))
333 return "unknown WSS connection state";
334 return __wlp_wss_connect_state[id];
335}
336
337
338
339
340
341
342
343ssize_t wlp_eda_show(struct wlp *wlp, char *buf)
344{
345 ssize_t result = 0;
346 struct wlp_eda_node *entry;
347 unsigned long flags;
348 struct wlp_eda *eda = &wlp->eda;
349 spin_lock_irqsave(&eda->lock, flags);
350 result = scnprintf(buf, PAGE_SIZE, "#eth_addr dev_addr wss_ptr "
351 "tag state virt_addr\n");
352 list_for_each_entry(entry, &eda->cache, list_node) {
353 result += scnprintf(buf + result, PAGE_SIZE - result,
354 "%pM %02x:%02x %p 0x%02x %s %pM\n",
355 entry->eth_addr,
356 entry->dev_addr.data[1],
357 entry->dev_addr.data[0], entry->wss,
358 entry->tag,
359 wlp_wss_connect_state_str(entry->state),
360 entry->virt_addr);
361 if (result >= PAGE_SIZE)
362 break;
363 }
364 spin_unlock_irqrestore(&eda->lock, flags);
365 return result;
366}
367EXPORT_SYMBOL_GPL(wlp_eda_show);
368
369
370
371
372
373
374
375
376
377ssize_t wlp_eda_store(struct wlp *wlp, const char *buf, size_t size)
378{
379 ssize_t result;
380 struct wlp_eda *eda = &wlp->eda;
381 u8 eth_addr[6];
382 struct uwb_dev_addr dev_addr;
383 u8 tag;
384 unsigned state;
385
386 result = sscanf(buf, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx "
387 "%02hhx:%02hhx %02hhx %u\n",
388 ð_addr[0], ð_addr[1],
389 ð_addr[2], ð_addr[3],
390 ð_addr[4], ð_addr[5],
391 &dev_addr.data[1], &dev_addr.data[0], &tag, &state);
392 switch (result) {
393 case 6:
394
395 result = -ENOSYS;
396 break;
397 case 10:
398 state = state >= 1 ? 1 : 0;
399 result = wlp_eda_create_node(eda, eth_addr, &dev_addr);
400 if (result < 0 && result != -EEXIST)
401 goto error;
402
403 result = wlp_eda_update_node(eda, &dev_addr, &wlp->wss,
404 eth_addr, tag, state);
405 if (result < 0)
406 goto error;
407 break;
408 default:
409 result = -EINVAL;
410 }
411error:
412 return result < 0 ? result : size;
413}
414EXPORT_SYMBOL_GPL(wlp_eda_store);
415