OpenWrt – Blame information for rev 4
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
4 | office | 1 | /* |
2 | * swconfig.c: Switch configuration API |
||
3 | * |
||
4 | * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name> |
||
5 | * |
||
6 | * This program is free software; you can redistribute it and/or |
||
7 | * modify it under the terms of the GNU General Public License |
||
8 | * as published by the Free Software Foundation; either version 2 |
||
9 | * of the License, or (at your option) any later version. |
||
10 | * |
||
11 | * This program is distributed in the hope that it will be useful, |
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | * GNU General Public License for more details. |
||
15 | */ |
||
16 | |||
17 | #include <linux/types.h> |
||
18 | #include <linux/module.h> |
||
19 | #include <linux/init.h> |
||
20 | #include <linux/list.h> |
||
21 | #include <linux/if.h> |
||
22 | #include <linux/if_ether.h> |
||
23 | #include <linux/capability.h> |
||
24 | #include <linux/skbuff.h> |
||
25 | #include <linux/switch.h> |
||
26 | #include <linux/of.h> |
||
27 | #include <linux/version.h> |
||
28 | #include <uapi/linux/mii.h> |
||
29 | |||
30 | #define SWCONFIG_DEVNAME "switch%d" |
||
31 | |||
32 | #include "swconfig_leds.c" |
||
33 | |||
34 | MODULE_AUTHOR("Felix Fietkau <nbd@nbd.name>"); |
||
35 | MODULE_LICENSE("GPL"); |
||
36 | |||
37 | static int swdev_id; |
||
38 | static struct list_head swdevs; |
||
39 | static DEFINE_MUTEX(swdevs_lock); |
||
40 | struct swconfig_callback; |
||
41 | |||
42 | struct swconfig_callback { |
||
43 | struct sk_buff *msg; |
||
44 | struct genlmsghdr *hdr; |
||
45 | struct genl_info *info; |
||
46 | int cmd; |
||
47 | |||
48 | /* callback for filling in the message data */ |
||
49 | int (*fill)(struct swconfig_callback *cb, void *arg); |
||
50 | |||
51 | /* callback for closing the message before sending it */ |
||
52 | int (*close)(struct swconfig_callback *cb, void *arg); |
||
53 | |||
54 | struct nlattr *nest[4]; |
||
55 | int args[4]; |
||
56 | }; |
||
57 | |||
58 | /* defaults */ |
||
59 | |||
60 | static int |
||
61 | swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, |
||
62 | struct switch_val *val) |
||
63 | { |
||
64 | int ret; |
||
65 | if (val->port_vlan >= dev->vlans) |
||
66 | return -EINVAL; |
||
67 | |||
68 | if (!dev->ops->get_vlan_ports) |
||
69 | return -EOPNOTSUPP; |
||
70 | |||
71 | ret = dev->ops->get_vlan_ports(dev, val); |
||
72 | return ret; |
||
73 | } |
||
74 | |||
75 | static int |
||
76 | swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr, |
||
77 | struct switch_val *val) |
||
78 | { |
||
79 | struct switch_port *ports = val->value.ports; |
||
80 | const struct switch_dev_ops *ops = dev->ops; |
||
81 | int i; |
||
82 | |||
83 | if (val->port_vlan >= dev->vlans) |
||
84 | return -EINVAL; |
||
85 | |||
86 | /* validate ports */ |
||
87 | if (val->len > dev->ports) |
||
88 | return -EINVAL; |
||
89 | |||
90 | if (!ops->set_vlan_ports) |
||
91 | return -EOPNOTSUPP; |
||
92 | |||
93 | for (i = 0; i < val->len; i++) { |
||
94 | if (ports[i].id >= dev->ports) |
||
95 | return -EINVAL; |
||
96 | |||
97 | if (ops->set_port_pvid && |
||
98 | !(ports[i].flags & (1 << SWITCH_PORT_FLAG_TAGGED))) |
||
99 | ops->set_port_pvid(dev, ports[i].id, val->port_vlan); |
||
100 | } |
||
101 | |||
102 | return ops->set_vlan_ports(dev, val); |
||
103 | } |
||
104 | |||
105 | static int |
||
106 | swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr, |
||
107 | struct switch_val *val) |
||
108 | { |
||
109 | if (val->port_vlan >= dev->ports) |
||
110 | return -EINVAL; |
||
111 | |||
112 | if (!dev->ops->set_port_pvid) |
||
113 | return -EOPNOTSUPP; |
||
114 | |||
115 | return dev->ops->set_port_pvid(dev, val->port_vlan, val->value.i); |
||
116 | } |
||
117 | |||
118 | static int |
||
119 | swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr, |
||
120 | struct switch_val *val) |
||
121 | { |
||
122 | if (val->port_vlan >= dev->ports) |
||
123 | return -EINVAL; |
||
124 | |||
125 | if (!dev->ops->get_port_pvid) |
||
126 | return -EOPNOTSUPP; |
||
127 | |||
128 | return dev->ops->get_port_pvid(dev, val->port_vlan, &val->value.i); |
||
129 | } |
||
130 | |||
131 | static int |
||
132 | swconfig_set_link(struct switch_dev *dev, const struct switch_attr *attr, |
||
133 | struct switch_val *val) |
||
134 | { |
||
135 | if (!dev->ops->set_port_link) |
||
136 | return -EOPNOTSUPP; |
||
137 | |||
138 | return dev->ops->set_port_link(dev, val->port_vlan, val->value.link); |
||
139 | } |
||
140 | |||
141 | static int |
||
142 | swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr, |
||
143 | struct switch_val *val) |
||
144 | { |
||
145 | struct switch_port_link *link = val->value.link; |
||
146 | |||
147 | if (val->port_vlan >= dev->ports) |
||
148 | return -EINVAL; |
||
149 | |||
150 | if (!dev->ops->get_port_link) |
||
151 | return -EOPNOTSUPP; |
||
152 | |||
153 | memset(link, 0, sizeof(*link)); |
||
154 | return dev->ops->get_port_link(dev, val->port_vlan, link); |
||
155 | } |
||
156 | |||
157 | static int |
||
158 | swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr, |
||
159 | struct switch_val *val) |
||
160 | { |
||
161 | /* don't complain if not supported by the switch driver */ |
||
162 | if (!dev->ops->apply_config) |
||
163 | return 0; |
||
164 | |||
165 | return dev->ops->apply_config(dev); |
||
166 | } |
||
167 | |||
168 | static int |
||
169 | swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr, |
||
170 | struct switch_val *val) |
||
171 | { |
||
172 | /* don't complain if not supported by the switch driver */ |
||
173 | if (!dev->ops->reset_switch) |
||
174 | return 0; |
||
175 | |||
176 | return dev->ops->reset_switch(dev); |
||
177 | } |
||
178 | |||
179 | enum global_defaults { |
||
180 | GLOBAL_APPLY, |
||
181 | GLOBAL_RESET, |
||
182 | }; |
||
183 | |||
184 | enum vlan_defaults { |
||
185 | VLAN_PORTS, |
||
186 | }; |
||
187 | |||
188 | enum port_defaults { |
||
189 | PORT_PVID, |
||
190 | PORT_LINK, |
||
191 | }; |
||
192 | |||
193 | static struct switch_attr default_global[] = { |
||
194 | [GLOBAL_APPLY] = { |
||
195 | .type = SWITCH_TYPE_NOVAL, |
||
196 | .name = "apply", |
||
197 | .description = "Activate changes in the hardware", |
||
198 | .set = swconfig_apply_config, |
||
199 | }, |
||
200 | [GLOBAL_RESET] = { |
||
201 | .type = SWITCH_TYPE_NOVAL, |
||
202 | .name = "reset", |
||
203 | .description = "Reset the switch", |
||
204 | .set = swconfig_reset_switch, |
||
205 | } |
||
206 | }; |
||
207 | |||
208 | static struct switch_attr default_port[] = { |
||
209 | [PORT_PVID] = { |
||
210 | .type = SWITCH_TYPE_INT, |
||
211 | .name = "pvid", |
||
212 | .description = "Primary VLAN ID", |
||
213 | .set = swconfig_set_pvid, |
||
214 | .get = swconfig_get_pvid, |
||
215 | }, |
||
216 | [PORT_LINK] = { |
||
217 | .type = SWITCH_TYPE_LINK, |
||
218 | .name = "link", |
||
219 | .description = "Get port link information", |
||
220 | .set = swconfig_set_link, |
||
221 | .get = swconfig_get_link, |
||
222 | } |
||
223 | }; |
||
224 | |||
225 | static struct switch_attr default_vlan[] = { |
||
226 | [VLAN_PORTS] = { |
||
227 | .type = SWITCH_TYPE_PORTS, |
||
228 | .name = "ports", |
||
229 | .description = "VLAN port mapping", |
||
230 | .set = swconfig_set_vlan_ports, |
||
231 | .get = swconfig_get_vlan_ports, |
||
232 | }, |
||
233 | }; |
||
234 | |||
235 | static const struct switch_attr * |
||
236 | swconfig_find_attr_by_name(const struct switch_attrlist *alist, |
||
237 | const char *name) |
||
238 | { |
||
239 | int i; |
||
240 | |||
241 | for (i = 0; i < alist->n_attr; i++) |
||
242 | if (strcmp(name, alist->attr[i].name) == 0) |
||
243 | return &alist->attr[i]; |
||
244 | |||
245 | return NULL; |
||
246 | } |
||
247 | |||
248 | static void swconfig_defaults_init(struct switch_dev *dev) |
||
249 | { |
||
250 | const struct switch_dev_ops *ops = dev->ops; |
||
251 | |||
252 | dev->def_global = 0; |
||
253 | dev->def_vlan = 0; |
||
254 | dev->def_port = 0; |
||
255 | |||
256 | if (ops->get_vlan_ports || ops->set_vlan_ports) |
||
257 | set_bit(VLAN_PORTS, &dev->def_vlan); |
||
258 | |||
259 | if (ops->get_port_pvid || ops->set_port_pvid) |
||
260 | set_bit(PORT_PVID, &dev->def_port); |
||
261 | |||
262 | if (ops->get_port_link && |
||
263 | !swconfig_find_attr_by_name(&ops->attr_port, "link")) |
||
264 | set_bit(PORT_LINK, &dev->def_port); |
||
265 | |||
266 | /* always present, can be no-op */ |
||
267 | set_bit(GLOBAL_APPLY, &dev->def_global); |
||
268 | set_bit(GLOBAL_RESET, &dev->def_global); |
||
269 | } |
||
270 | |||
271 | |||
272 | static struct genl_family switch_fam; |
||
273 | |||
274 | static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = { |
||
275 | [SWITCH_ATTR_ID] = { .type = NLA_U32 }, |
||
276 | [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 }, |
||
277 | [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 }, |
||
278 | [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 }, |
||
279 | [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 }, |
||
280 | [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING }, |
||
281 | [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED }, |
||
282 | [SWITCH_ATTR_TYPE] = { .type = NLA_U32 }, |
||
283 | }; |
||
284 | |||
285 | static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = { |
||
286 | [SWITCH_PORT_ID] = { .type = NLA_U32 }, |
||
287 | [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG }, |
||
288 | }; |
||
289 | |||
290 | static struct nla_policy link_policy[SWITCH_LINK_ATTR_MAX] = { |
||
291 | [SWITCH_LINK_FLAG_DUPLEX] = { .type = NLA_FLAG }, |
||
292 | [SWITCH_LINK_FLAG_ANEG] = { .type = NLA_FLAG }, |
||
293 | [SWITCH_LINK_SPEED] = { .type = NLA_U32 }, |
||
294 | }; |
||
295 | |||
296 | static inline void |
||
297 | swconfig_lock(void) |
||
298 | { |
||
299 | mutex_lock(&swdevs_lock); |
||
300 | } |
||
301 | |||
302 | static inline void |
||
303 | swconfig_unlock(void) |
||
304 | { |
||
305 | mutex_unlock(&swdevs_lock); |
||
306 | } |
||
307 | |||
308 | static struct switch_dev * |
||
309 | swconfig_get_dev(struct genl_info *info) |
||
310 | { |
||
311 | struct switch_dev *dev = NULL; |
||
312 | struct switch_dev *p; |
||
313 | int id; |
||
314 | |||
315 | if (!info->attrs[SWITCH_ATTR_ID]) |
||
316 | goto done; |
||
317 | |||
318 | id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]); |
||
319 | swconfig_lock(); |
||
320 | list_for_each_entry(p, &swdevs, dev_list) { |
||
321 | if (id != p->id) |
||
322 | continue; |
||
323 | |||
324 | dev = p; |
||
325 | break; |
||
326 | } |
||
327 | if (dev) |
||
328 | mutex_lock(&dev->sw_mutex); |
||
329 | else |
||
330 | pr_debug("device %d not found\n", id); |
||
331 | swconfig_unlock(); |
||
332 | done: |
||
333 | return dev; |
||
334 | } |
||
335 | |||
336 | static inline void |
||
337 | swconfig_put_dev(struct switch_dev *dev) |
||
338 | { |
||
339 | mutex_unlock(&dev->sw_mutex); |
||
340 | } |
||
341 | |||
342 | static int |
||
343 | swconfig_dump_attr(struct swconfig_callback *cb, void *arg) |
||
344 | { |
||
345 | struct switch_attr *op = arg; |
||
346 | struct genl_info *info = cb->info; |
||
347 | struct sk_buff *msg = cb->msg; |
||
348 | int id = cb->args[0]; |
||
349 | void *hdr; |
||
350 | |||
351 | hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam, |
||
352 | NLM_F_MULTI, SWITCH_CMD_NEW_ATTR); |
||
353 | if (IS_ERR(hdr)) |
||
354 | return -1; |
||
355 | |||
356 | if (nla_put_u32(msg, SWITCH_ATTR_OP_ID, id)) |
||
357 | goto nla_put_failure; |
||
358 | if (nla_put_u32(msg, SWITCH_ATTR_OP_TYPE, op->type)) |
||
359 | goto nla_put_failure; |
||
360 | if (nla_put_string(msg, SWITCH_ATTR_OP_NAME, op->name)) |
||
361 | goto nla_put_failure; |
||
362 | if (op->description) |
||
363 | if (nla_put_string(msg, SWITCH_ATTR_OP_DESCRIPTION, |
||
364 | op->description)) |
||
365 | goto nla_put_failure; |
||
366 | |||
367 | genlmsg_end(msg, hdr); |
||
368 | return msg->len; |
||
369 | nla_put_failure: |
||
370 | genlmsg_cancel(msg, hdr); |
||
371 | return -EMSGSIZE; |
||
372 | } |
||
373 | |||
374 | /* spread multipart messages across multiple message buffers */ |
||
375 | static int |
||
376 | swconfig_send_multipart(struct swconfig_callback *cb, void *arg) |
||
377 | { |
||
378 | struct genl_info *info = cb->info; |
||
379 | int restart = 0; |
||
380 | int err; |
||
381 | |||
382 | do { |
||
383 | if (!cb->msg) { |
||
384 | cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
||
385 | if (cb->msg == NULL) |
||
386 | goto error; |
||
387 | } |
||
388 | |||
389 | if (!(cb->fill(cb, arg) < 0)) |
||
390 | break; |
||
391 | |||
392 | /* fill failed, check if this was already the second attempt */ |
||
393 | if (restart) |
||
394 | goto error; |
||
395 | |||
396 | /* try again in a new message, send the current one */ |
||
397 | restart = 1; |
||
398 | if (cb->close) { |
||
399 | if (cb->close(cb, arg) < 0) |
||
400 | goto error; |
||
401 | } |
||
402 | err = genlmsg_reply(cb->msg, info); |
||
403 | cb->msg = NULL; |
||
404 | if (err < 0) |
||
405 | goto error; |
||
406 | |||
407 | } while (restart); |
||
408 | |||
409 | return 0; |
||
410 | |||
411 | error: |
||
412 | if (cb->msg) |
||
413 | nlmsg_free(cb->msg); |
||
414 | return -1; |
||
415 | } |
||
416 | |||
417 | static int |
||
418 | swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info) |
||
419 | { |
||
420 | struct genlmsghdr *hdr = nlmsg_data(info->nlhdr); |
||
421 | const struct switch_attrlist *alist; |
||
422 | struct switch_dev *dev; |
||
423 | struct swconfig_callback cb; |
||
424 | int err = -EINVAL; |
||
425 | int i; |
||
426 | |||
427 | /* defaults */ |
||
428 | struct switch_attr *def_list; |
||
429 | unsigned long *def_active; |
||
430 | int n_def; |
||
431 | |||
432 | dev = swconfig_get_dev(info); |
||
433 | if (!dev) |
||
434 | return -EINVAL; |
||
435 | |||
436 | switch (hdr->cmd) { |
||
437 | case SWITCH_CMD_LIST_GLOBAL: |
||
438 | alist = &dev->ops->attr_global; |
||
439 | def_list = default_global; |
||
440 | def_active = &dev->def_global; |
||
441 | n_def = ARRAY_SIZE(default_global); |
||
442 | break; |
||
443 | case SWITCH_CMD_LIST_VLAN: |
||
444 | alist = &dev->ops->attr_vlan; |
||
445 | def_list = default_vlan; |
||
446 | def_active = &dev->def_vlan; |
||
447 | n_def = ARRAY_SIZE(default_vlan); |
||
448 | break; |
||
449 | case SWITCH_CMD_LIST_PORT: |
||
450 | alist = &dev->ops->attr_port; |
||
451 | def_list = default_port; |
||
452 | def_active = &dev->def_port; |
||
453 | n_def = ARRAY_SIZE(default_port); |
||
454 | break; |
||
455 | default: |
||
456 | WARN_ON(1); |
||
457 | goto out; |
||
458 | } |
||
459 | |||
460 | memset(&cb, 0, sizeof(cb)); |
||
461 | cb.info = info; |
||
462 | cb.fill = swconfig_dump_attr; |
||
463 | for (i = 0; i < alist->n_attr; i++) { |
||
464 | if (alist->attr[i].disabled) |
||
465 | continue; |
||
466 | cb.args[0] = i; |
||
467 | err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]); |
||
468 | if (err < 0) |
||
469 | goto error; |
||
470 | } |
||
471 | |||
472 | /* defaults */ |
||
473 | for (i = 0; i < n_def; i++) { |
||
474 | if (!test_bit(i, def_active)) |
||
475 | continue; |
||
476 | cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i; |
||
477 | err = swconfig_send_multipart(&cb, (void *) &def_list[i]); |
||
478 | if (err < 0) |
||
479 | goto error; |
||
480 | } |
||
481 | swconfig_put_dev(dev); |
||
482 | |||
483 | if (!cb.msg) |
||
484 | return 0; |
||
485 | |||
486 | return genlmsg_reply(cb.msg, info); |
||
487 | |||
488 | error: |
||
489 | if (cb.msg) |
||
490 | nlmsg_free(cb.msg); |
||
491 | out: |
||
492 | swconfig_put_dev(dev); |
||
493 | return err; |
||
494 | } |
||
495 | |||
496 | static const struct switch_attr * |
||
497 | swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info, |
||
498 | struct switch_val *val) |
||
499 | { |
||
500 | struct genlmsghdr *hdr = nlmsg_data(info->nlhdr); |
||
501 | const struct switch_attrlist *alist; |
||
502 | const struct switch_attr *attr = NULL; |
||
503 | unsigned int attr_id; |
||
504 | |||
505 | /* defaults */ |
||
506 | struct switch_attr *def_list; |
||
507 | unsigned long *def_active; |
||
508 | int n_def; |
||
509 | |||
510 | if (!info->attrs[SWITCH_ATTR_OP_ID]) |
||
511 | goto done; |
||
512 | |||
513 | switch (hdr->cmd) { |
||
514 | case SWITCH_CMD_SET_GLOBAL: |
||
515 | case SWITCH_CMD_GET_GLOBAL: |
||
516 | alist = &dev->ops->attr_global; |
||
517 | def_list = default_global; |
||
518 | def_active = &dev->def_global; |
||
519 | n_def = ARRAY_SIZE(default_global); |
||
520 | break; |
||
521 | case SWITCH_CMD_SET_VLAN: |
||
522 | case SWITCH_CMD_GET_VLAN: |
||
523 | alist = &dev->ops->attr_vlan; |
||
524 | def_list = default_vlan; |
||
525 | def_active = &dev->def_vlan; |
||
526 | n_def = ARRAY_SIZE(default_vlan); |
||
527 | if (!info->attrs[SWITCH_ATTR_OP_VLAN]) |
||
528 | goto done; |
||
529 | val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]); |
||
530 | if (val->port_vlan >= dev->vlans) |
||
531 | goto done; |
||
532 | break; |
||
533 | case SWITCH_CMD_SET_PORT: |
||
534 | case SWITCH_CMD_GET_PORT: |
||
535 | alist = &dev->ops->attr_port; |
||
536 | def_list = default_port; |
||
537 | def_active = &dev->def_port; |
||
538 | n_def = ARRAY_SIZE(default_port); |
||
539 | if (!info->attrs[SWITCH_ATTR_OP_PORT]) |
||
540 | goto done; |
||
541 | val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]); |
||
542 | if (val->port_vlan >= dev->ports) |
||
543 | goto done; |
||
544 | break; |
||
545 | default: |
||
546 | WARN_ON(1); |
||
547 | goto done; |
||
548 | } |
||
549 | |||
550 | if (!alist) |
||
551 | goto done; |
||
552 | |||
553 | attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]); |
||
554 | if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) { |
||
555 | attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET; |
||
556 | if (attr_id >= n_def) |
||
557 | goto done; |
||
558 | if (!test_bit(attr_id, def_active)) |
||
559 | goto done; |
||
560 | attr = &def_list[attr_id]; |
||
561 | } else { |
||
562 | if (attr_id >= alist->n_attr) |
||
563 | goto done; |
||
564 | attr = &alist->attr[attr_id]; |
||
565 | } |
||
566 | |||
567 | if (attr->disabled) |
||
568 | attr = NULL; |
||
569 | |||
570 | done: |
||
571 | if (!attr) |
||
572 | pr_debug("attribute lookup failed\n"); |
||
573 | val->attr = attr; |
||
574 | return attr; |
||
575 | } |
||
576 | |||
577 | static int |
||
578 | swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head, |
||
579 | struct switch_val *val, int max) |
||
580 | { |
||
581 | struct nlattr *nla; |
||
582 | int rem; |
||
583 | |||
584 | val->len = 0; |
||
585 | nla_for_each_nested(nla, head, rem) { |
||
586 | struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1]; |
||
587 | struct switch_port *port; |
||
588 | |||
589 | if (val->len >= max) |
||
590 | return -EINVAL; |
||
591 | |||
592 | port = &val->value.ports[val->len]; |
||
593 | |||
594 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0) |
||
595 | if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla, |
||
596 | port_policy)) |
||
597 | #else |
||
598 | if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla, |
||
599 | port_policy, NULL)) |
||
600 | #endif |
||
601 | return -EINVAL; |
||
602 | |||
603 | if (!tb[SWITCH_PORT_ID]) |
||
604 | return -EINVAL; |
||
605 | |||
606 | port->id = nla_get_u32(tb[SWITCH_PORT_ID]); |
||
607 | if (tb[SWITCH_PORT_FLAG_TAGGED]) |
||
608 | port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED); |
||
609 | val->len++; |
||
610 | } |
||
611 | |||
612 | return 0; |
||
613 | } |
||
614 | |||
615 | static int |
||
616 | swconfig_parse_link(struct sk_buff *msg, struct nlattr *nla, |
||
617 | struct switch_port_link *link) |
||
618 | { |
||
619 | struct nlattr *tb[SWITCH_LINK_ATTR_MAX + 1]; |
||
620 | |||
621 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,12,0) |
||
622 | if (nla_parse_nested(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy)) |
||
623 | #else |
||
624 | if (nla_parse_nested(tb, SWITCH_LINK_ATTR_MAX, nla, link_policy, NULL)) |
||
625 | #endif |
||
626 | return -EINVAL; |
||
627 | |||
628 | link->duplex = !!tb[SWITCH_LINK_FLAG_DUPLEX]; |
||
629 | link->aneg = !!tb[SWITCH_LINK_FLAG_ANEG]; |
||
630 | link->speed = nla_get_u32(tb[SWITCH_LINK_SPEED]); |
||
631 | |||
632 | return 0; |
||
633 | } |
||
634 | |||
635 | static int |
||
636 | swconfig_set_attr(struct sk_buff *skb, struct genl_info *info) |
||
637 | { |
||
638 | const struct switch_attr *attr; |
||
639 | struct switch_dev *dev; |
||
640 | struct switch_val val; |
||
641 | int err = -EINVAL; |
||
642 | |||
643 | if (!capable(CAP_NET_ADMIN)) |
||
644 | return -EPERM; |
||
645 | |||
646 | dev = swconfig_get_dev(info); |
||
647 | if (!dev) |
||
648 | return -EINVAL; |
||
649 | |||
650 | memset(&val, 0, sizeof(val)); |
||
651 | attr = swconfig_lookup_attr(dev, info, &val); |
||
652 | if (!attr || !attr->set) |
||
653 | goto error; |
||
654 | |||
655 | val.attr = attr; |
||
656 | switch (attr->type) { |
||
657 | case SWITCH_TYPE_NOVAL: |
||
658 | break; |
||
659 | case SWITCH_TYPE_INT: |
||
660 | if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT]) |
||
661 | goto error; |
||
662 | val.value.i = |
||
663 | nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]); |
||
664 | break; |
||
665 | case SWITCH_TYPE_STRING: |
||
666 | if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR]) |
||
667 | goto error; |
||
668 | val.value.s = |
||
669 | nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]); |
||
670 | break; |
||
671 | case SWITCH_TYPE_PORTS: |
||
672 | val.value.ports = dev->portbuf; |
||
673 | memset(dev->portbuf, 0, |
||
674 | sizeof(struct switch_port) * dev->ports); |
||
675 | |||
676 | /* TODO: implement multipart? */ |
||
677 | if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) { |
||
678 | err = swconfig_parse_ports(skb, |
||
679 | info->attrs[SWITCH_ATTR_OP_VALUE_PORTS], |
||
680 | &val, dev->ports); |
||
681 | if (err < 0) |
||
682 | goto error; |
||
683 | } else { |
||
684 | val.len = 0; |
||
685 | err = 0; |
||
686 | } |
||
687 | break; |
||
688 | case SWITCH_TYPE_LINK: |
||
689 | val.value.link = &dev->linkbuf; |
||
690 | memset(&dev->linkbuf, 0, sizeof(struct switch_port_link)); |
||
691 | |||
692 | if (info->attrs[SWITCH_ATTR_OP_VALUE_LINK]) { |
||
693 | err = swconfig_parse_link(skb, |
||
694 | info->attrs[SWITCH_ATTR_OP_VALUE_LINK], |
||
695 | val.value.link); |
||
696 | if (err < 0) |
||
697 | goto error; |
||
698 | } else { |
||
699 | val.len = 0; |
||
700 | err = 0; |
||
701 | } |
||
702 | break; |
||
703 | default: |
||
704 | goto error; |
||
705 | } |
||
706 | |||
707 | err = attr->set(dev, attr, &val); |
||
708 | error: |
||
709 | swconfig_put_dev(dev); |
||
710 | return err; |
||
711 | } |
||
712 | |||
713 | static int |
||
714 | swconfig_close_portlist(struct swconfig_callback *cb, void *arg) |
||
715 | { |
||
716 | if (cb->nest[0]) |
||
717 | nla_nest_end(cb->msg, cb->nest[0]); |
||
718 | return 0; |
||
719 | } |
||
720 | |||
721 | static int |
||
722 | swconfig_send_port(struct swconfig_callback *cb, void *arg) |
||
723 | { |
||
724 | const struct switch_port *port = arg; |
||
725 | struct nlattr *p = NULL; |
||
726 | |||
727 | if (!cb->nest[0]) { |
||
728 | cb->nest[0] = nla_nest_start(cb->msg, cb->cmd); |
||
729 | if (!cb->nest[0]) |
||
730 | return -1; |
||
731 | } |
||
732 | |||
733 | p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT); |
||
734 | if (!p) |
||
735 | goto error; |
||
736 | |||
737 | if (nla_put_u32(cb->msg, SWITCH_PORT_ID, port->id)) |
||
738 | goto nla_put_failure; |
||
739 | if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) { |
||
740 | if (nla_put_flag(cb->msg, SWITCH_PORT_FLAG_TAGGED)) |
||
741 | goto nla_put_failure; |
||
742 | } |
||
743 | |||
744 | nla_nest_end(cb->msg, p); |
||
745 | return 0; |
||
746 | |||
747 | nla_put_failure: |
||
748 | nla_nest_cancel(cb->msg, p); |
||
749 | error: |
||
750 | nla_nest_cancel(cb->msg, cb->nest[0]); |
||
751 | return -1; |
||
752 | } |
||
753 | |||
754 | static int |
||
755 | swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr, |
||
756 | const struct switch_val *val) |
||
757 | { |
||
758 | struct swconfig_callback cb; |
||
759 | int err = 0; |
||
760 | int i; |
||
761 | |||
762 | if (!val->value.ports) |
||
763 | return -EINVAL; |
||
764 | |||
765 | memset(&cb, 0, sizeof(cb)); |
||
766 | cb.cmd = attr; |
||
767 | cb.msg = *msg; |
||
768 | cb.info = info; |
||
769 | cb.fill = swconfig_send_port; |
||
770 | cb.close = swconfig_close_portlist; |
||
771 | |||
772 | cb.nest[0] = nla_nest_start(cb.msg, cb.cmd); |
||
773 | for (i = 0; i < val->len; i++) { |
||
774 | err = swconfig_send_multipart(&cb, &val->value.ports[i]); |
||
775 | if (err) |
||
776 | goto done; |
||
777 | } |
||
778 | err = val->len; |
||
779 | swconfig_close_portlist(&cb, NULL); |
||
780 | *msg = cb.msg; |
||
781 | |||
782 | done: |
||
783 | return err; |
||
784 | } |
||
785 | |||
786 | static int |
||
787 | swconfig_send_link(struct sk_buff *msg, struct genl_info *info, int attr, |
||
788 | const struct switch_port_link *link) |
||
789 | { |
||
790 | struct nlattr *p = NULL; |
||
791 | int err = 0; |
||
792 | |||
793 | p = nla_nest_start(msg, attr); |
||
794 | if (link->link) { |
||
795 | if (nla_put_flag(msg, SWITCH_LINK_FLAG_LINK)) |
||
796 | goto nla_put_failure; |
||
797 | } |
||
798 | if (link->duplex) { |
||
799 | if (nla_put_flag(msg, SWITCH_LINK_FLAG_DUPLEX)) |
||
800 | goto nla_put_failure; |
||
801 | } |
||
802 | if (link->aneg) { |
||
803 | if (nla_put_flag(msg, SWITCH_LINK_FLAG_ANEG)) |
||
804 | goto nla_put_failure; |
||
805 | } |
||
806 | if (link->tx_flow) { |
||
807 | if (nla_put_flag(msg, SWITCH_LINK_FLAG_TX_FLOW)) |
||
808 | goto nla_put_failure; |
||
809 | } |
||
810 | if (link->rx_flow) { |
||
811 | if (nla_put_flag(msg, SWITCH_LINK_FLAG_RX_FLOW)) |
||
812 | goto nla_put_failure; |
||
813 | } |
||
814 | if (nla_put_u32(msg, SWITCH_LINK_SPEED, link->speed)) |
||
815 | goto nla_put_failure; |
||
816 | if (link->eee & ADVERTISED_100baseT_Full) { |
||
817 | if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_100BASET)) |
||
818 | goto nla_put_failure; |
||
819 | } |
||
820 | if (link->eee & ADVERTISED_1000baseT_Full) { |
||
821 | if (nla_put_flag(msg, SWITCH_LINK_FLAG_EEE_1000BASET)) |
||
822 | goto nla_put_failure; |
||
823 | } |
||
824 | nla_nest_end(msg, p); |
||
825 | |||
826 | return err; |
||
827 | |||
828 | nla_put_failure: |
||
829 | nla_nest_cancel(msg, p); |
||
830 | return -1; |
||
831 | } |
||
832 | |||
833 | static int |
||
834 | swconfig_get_attr(struct sk_buff *skb, struct genl_info *info) |
||
835 | { |
||
836 | struct genlmsghdr *hdr = nlmsg_data(info->nlhdr); |
||
837 | const struct switch_attr *attr; |
||
838 | struct switch_dev *dev; |
||
839 | struct sk_buff *msg = NULL; |
||
840 | struct switch_val val; |
||
841 | int err = -EINVAL; |
||
842 | int cmd = hdr->cmd; |
||
843 | |||
844 | dev = swconfig_get_dev(info); |
||
845 | if (!dev) |
||
846 | return -EINVAL; |
||
847 | |||
848 | memset(&val, 0, sizeof(val)); |
||
849 | attr = swconfig_lookup_attr(dev, info, &val); |
||
850 | if (!attr || !attr->get) |
||
851 | goto error; |
||
852 | |||
853 | if (attr->type == SWITCH_TYPE_PORTS) { |
||
854 | val.value.ports = dev->portbuf; |
||
855 | memset(dev->portbuf, 0, |
||
856 | sizeof(struct switch_port) * dev->ports); |
||
857 | } else if (attr->type == SWITCH_TYPE_LINK) { |
||
858 | val.value.link = &dev->linkbuf; |
||
859 | memset(&dev->linkbuf, 0, sizeof(struct switch_port_link)); |
||
860 | } |
||
861 | |||
862 | err = attr->get(dev, attr, &val); |
||
863 | if (err) |
||
864 | goto error; |
||
865 | |||
866 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); |
||
867 | if (!msg) |
||
868 | goto error; |
||
869 | |||
870 | hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam, |
||
871 | 0, cmd); |
||
872 | if (IS_ERR(hdr)) |
||
873 | goto nla_put_failure; |
||
874 | |||
875 | switch (attr->type) { |
||
876 | case SWITCH_TYPE_INT: |
||
877 | if (nla_put_u32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i)) |
||
878 | goto nla_put_failure; |
||
879 | break; |
||
880 | case SWITCH_TYPE_STRING: |
||
881 | if (nla_put_string(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s)) |
||
882 | goto nla_put_failure; |
||
883 | break; |
||
884 | case SWITCH_TYPE_PORTS: |
||
885 | err = swconfig_send_ports(&msg, info, |
||
886 | SWITCH_ATTR_OP_VALUE_PORTS, &val); |
||
887 | if (err < 0) |
||
888 | goto nla_put_failure; |
||
889 | break; |
||
890 | case SWITCH_TYPE_LINK: |
||
891 | err = swconfig_send_link(msg, info, |
||
892 | SWITCH_ATTR_OP_VALUE_LINK, val.value.link); |
||
893 | if (err < 0) |
||
894 | goto nla_put_failure; |
||
895 | break; |
||
896 | default: |
||
897 | pr_debug("invalid type in attribute\n"); |
||
898 | err = -EINVAL; |
||
899 | goto nla_put_failure; |
||
900 | } |
||
901 | genlmsg_end(msg, hdr); |
||
902 | err = msg->len; |
||
903 | if (err < 0) |
||
904 | goto nla_put_failure; |
||
905 | |||
906 | swconfig_put_dev(dev); |
||
907 | return genlmsg_reply(msg, info); |
||
908 | |||
909 | nla_put_failure: |
||
910 | if (msg) |
||
911 | nlmsg_free(msg); |
||
912 | error: |
||
913 | swconfig_put_dev(dev); |
||
914 | if (!err) |
||
915 | err = -ENOMEM; |
||
916 | return err; |
||
917 | } |
||
918 | |||
919 | static int |
||
920 | swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags, |
||
921 | const struct switch_dev *dev) |
||
922 | { |
||
923 | struct nlattr *p = NULL, *m = NULL; |
||
924 | void *hdr; |
||
925 | int i; |
||
926 | |||
927 | hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags, |
||
928 | SWITCH_CMD_NEW_ATTR); |
||
929 | if (IS_ERR(hdr)) |
||
930 | return -1; |
||
931 | |||
932 | if (nla_put_u32(msg, SWITCH_ATTR_ID, dev->id)) |
||
933 | goto nla_put_failure; |
||
934 | if (nla_put_string(msg, SWITCH_ATTR_DEV_NAME, dev->devname)) |
||
935 | goto nla_put_failure; |
||
936 | if (nla_put_string(msg, SWITCH_ATTR_ALIAS, dev->alias)) |
||
937 | goto nla_put_failure; |
||
938 | if (nla_put_string(msg, SWITCH_ATTR_NAME, dev->name)) |
||
939 | goto nla_put_failure; |
||
940 | if (nla_put_u32(msg, SWITCH_ATTR_VLANS, dev->vlans)) |
||
941 | goto nla_put_failure; |
||
942 | if (nla_put_u32(msg, SWITCH_ATTR_PORTS, dev->ports)) |
||
943 | goto nla_put_failure; |
||
944 | if (nla_put_u32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port)) |
||
945 | goto nla_put_failure; |
||
946 | |||
947 | m = nla_nest_start(msg, SWITCH_ATTR_PORTMAP); |
||
948 | if (!m) |
||
949 | goto nla_put_failure; |
||
950 | for (i = 0; i < dev->ports; i++) { |
||
951 | p = nla_nest_start(msg, SWITCH_ATTR_PORTS); |
||
952 | if (!p) |
||
953 | continue; |
||
954 | if (dev->portmap[i].s) { |
||
955 | if (nla_put_string(msg, SWITCH_PORTMAP_SEGMENT, |
||
956 | dev->portmap[i].s)) |
||
957 | goto nla_put_failure; |
||
958 | if (nla_put_u32(msg, SWITCH_PORTMAP_VIRT, |
||
959 | dev->portmap[i].virt)) |
||
960 | goto nla_put_failure; |
||
961 | } |
||
962 | nla_nest_end(msg, p); |
||
963 | } |
||
964 | nla_nest_end(msg, m); |
||
965 | genlmsg_end(msg, hdr); |
||
966 | return msg->len; |
||
967 | nla_put_failure: |
||
968 | genlmsg_cancel(msg, hdr); |
||
969 | return -EMSGSIZE; |
||
970 | } |
||
971 | |||
972 | static int swconfig_dump_switches(struct sk_buff *skb, |
||
973 | struct netlink_callback *cb) |
||
974 | { |
||
975 | struct switch_dev *dev; |
||
976 | int start = cb->args[0]; |
||
977 | int idx = 0; |
||
978 | |||
979 | swconfig_lock(); |
||
980 | list_for_each_entry(dev, &swdevs, dev_list) { |
||
981 | if (++idx <= start) |
||
982 | continue; |
||
983 | if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).portid, |
||
984 | cb->nlh->nlmsg_seq, NLM_F_MULTI, |
||
985 | dev) < 0) |
||
986 | break; |
||
987 | } |
||
988 | swconfig_unlock(); |
||
989 | cb->args[0] = idx; |
||
990 | |||
991 | return skb->len; |
||
992 | } |
||
993 | |||
994 | static int |
||
995 | swconfig_done(struct netlink_callback *cb) |
||
996 | { |
||
997 | return 0; |
||
998 | } |
||
999 | |||
1000 | static struct genl_ops swconfig_ops[] = { |
||
1001 | { |
||
1002 | .cmd = SWITCH_CMD_LIST_GLOBAL, |
||
1003 | .doit = swconfig_list_attrs, |
||
1004 | .policy = switch_policy, |
||
1005 | }, |
||
1006 | { |
||
1007 | .cmd = SWITCH_CMD_LIST_VLAN, |
||
1008 | .doit = swconfig_list_attrs, |
||
1009 | .policy = switch_policy, |
||
1010 | }, |
||
1011 | { |
||
1012 | .cmd = SWITCH_CMD_LIST_PORT, |
||
1013 | .doit = swconfig_list_attrs, |
||
1014 | .policy = switch_policy, |
||
1015 | }, |
||
1016 | { |
||
1017 | .cmd = SWITCH_CMD_GET_GLOBAL, |
||
1018 | .doit = swconfig_get_attr, |
||
1019 | .policy = switch_policy, |
||
1020 | }, |
||
1021 | { |
||
1022 | .cmd = SWITCH_CMD_GET_VLAN, |
||
1023 | .doit = swconfig_get_attr, |
||
1024 | .policy = switch_policy, |
||
1025 | }, |
||
1026 | { |
||
1027 | .cmd = SWITCH_CMD_GET_PORT, |
||
1028 | .doit = swconfig_get_attr, |
||
1029 | .policy = switch_policy, |
||
1030 | }, |
||
1031 | { |
||
1032 | .cmd = SWITCH_CMD_SET_GLOBAL, |
||
1033 | .flags = GENL_ADMIN_PERM, |
||
1034 | .doit = swconfig_set_attr, |
||
1035 | .policy = switch_policy, |
||
1036 | }, |
||
1037 | { |
||
1038 | .cmd = SWITCH_CMD_SET_VLAN, |
||
1039 | .flags = GENL_ADMIN_PERM, |
||
1040 | .doit = swconfig_set_attr, |
||
1041 | .policy = switch_policy, |
||
1042 | }, |
||
1043 | { |
||
1044 | .cmd = SWITCH_CMD_SET_PORT, |
||
1045 | .flags = GENL_ADMIN_PERM, |
||
1046 | .doit = swconfig_set_attr, |
||
1047 | .policy = switch_policy, |
||
1048 | }, |
||
1049 | { |
||
1050 | .cmd = SWITCH_CMD_GET_SWITCH, |
||
1051 | .dumpit = swconfig_dump_switches, |
||
1052 | .policy = switch_policy, |
||
1053 | .done = swconfig_done, |
||
1054 | } |
||
1055 | }; |
||
1056 | |||
1057 | static struct genl_family switch_fam = { |
||
1058 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0) |
||
1059 | .id = GENL_ID_GENERATE, |
||
1060 | #endif |
||
1061 | .name = "switch", |
||
1062 | .hdrsize = 0, |
||
1063 | .version = 1, |
||
1064 | .maxattr = SWITCH_ATTR_MAX, |
||
1065 | .module = THIS_MODULE, |
||
1066 | .ops = swconfig_ops, |
||
1067 | .n_ops = ARRAY_SIZE(swconfig_ops), |
||
1068 | }; |
||
1069 | |||
1070 | #ifdef CONFIG_OF |
||
1071 | void |
||
1072 | of_switch_load_portmap(struct switch_dev *dev) |
||
1073 | { |
||
1074 | struct device_node *port; |
||
1075 | |||
1076 | if (!dev->of_node) |
||
1077 | return; |
||
1078 | |||
1079 | for_each_child_of_node(dev->of_node, port) { |
||
1080 | const __be32 *prop; |
||
1081 | const char *segment; |
||
1082 | int size, phys; |
||
1083 | |||
1084 | if (!of_device_is_compatible(port, "swconfig,port")) |
||
1085 | continue; |
||
1086 | |||
1087 | if (of_property_read_string(port, "swconfig,segment", &segment)) |
||
1088 | continue; |
||
1089 | |||
1090 | prop = of_get_property(port, "swconfig,portmap", &size); |
||
1091 | if (!prop) |
||
1092 | continue; |
||
1093 | |||
1094 | if (size != (2 * sizeof(*prop))) { |
||
1095 | pr_err("%s: failed to parse port mapping\n", |
||
1096 | port->name); |
||
1097 | continue; |
||
1098 | } |
||
1099 | |||
1100 | phys = be32_to_cpup(prop++); |
||
1101 | if ((phys < 0) | (phys >= dev->ports)) { |
||
1102 | pr_err("%s: physical port index out of range\n", |
||
1103 | port->name); |
||
1104 | continue; |
||
1105 | } |
||
1106 | |||
1107 | dev->portmap[phys].s = kstrdup(segment, GFP_KERNEL); |
||
1108 | dev->portmap[phys].virt = be32_to_cpup(prop); |
||
1109 | pr_debug("Found port: %s, physical: %d, virtual: %d\n", |
||
1110 | segment, phys, dev->portmap[phys].virt); |
||
1111 | } |
||
1112 | } |
||
1113 | #endif |
||
1114 | |||
1115 | int |
||
1116 | register_switch(struct switch_dev *dev, struct net_device *netdev) |
||
1117 | { |
||
1118 | struct switch_dev *sdev; |
||
1119 | const int max_switches = 8 * sizeof(unsigned long); |
||
1120 | unsigned long in_use = 0; |
||
1121 | int err; |
||
1122 | int i; |
||
1123 | |||
1124 | INIT_LIST_HEAD(&dev->dev_list); |
||
1125 | if (netdev) { |
||
1126 | dev->netdev = netdev; |
||
1127 | if (!dev->alias) |
||
1128 | dev->alias = netdev->name; |
||
1129 | } |
||
1130 | BUG_ON(!dev->alias); |
||
1131 | |||
1132 | /* Make sure swdev_id doesn't overflow */ |
||
1133 | if (swdev_id == INT_MAX) { |
||
1134 | return -ENOMEM; |
||
1135 | } |
||
1136 | |||
1137 | if (dev->ports > 0) { |
||
1138 | dev->portbuf = kzalloc(sizeof(struct switch_port) * |
||
1139 | dev->ports, GFP_KERNEL); |
||
1140 | if (!dev->portbuf) |
||
1141 | return -ENOMEM; |
||
1142 | dev->portmap = kzalloc(sizeof(struct switch_portmap) * |
||
1143 | dev->ports, GFP_KERNEL); |
||
1144 | if (!dev->portmap) { |
||
1145 | kfree(dev->portbuf); |
||
1146 | return -ENOMEM; |
||
1147 | } |
||
1148 | } |
||
1149 | swconfig_defaults_init(dev); |
||
1150 | mutex_init(&dev->sw_mutex); |
||
1151 | swconfig_lock(); |
||
1152 | dev->id = ++swdev_id; |
||
1153 | |||
1154 | list_for_each_entry(sdev, &swdevs, dev_list) { |
||
1155 | if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i)) |
||
1156 | continue; |
||
1157 | if (i < 0 || i > max_switches) |
||
1158 | continue; |
||
1159 | |||
1160 | set_bit(i, &in_use); |
||
1161 | } |
||
1162 | i = find_first_zero_bit(&in_use, max_switches); |
||
1163 | |||
1164 | if (i == max_switches) { |
||
1165 | swconfig_unlock(); |
||
1166 | return -ENFILE; |
||
1167 | } |
||
1168 | |||
1169 | #ifdef CONFIG_OF |
||
1170 | if (dev->ports) |
||
1171 | of_switch_load_portmap(dev); |
||
1172 | #endif |
||
1173 | |||
1174 | /* fill device name */ |
||
1175 | snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i); |
||
1176 | |||
1177 | list_add_tail(&dev->dev_list, &swdevs); |
||
1178 | swconfig_unlock(); |
||
1179 | |||
1180 | err = swconfig_create_led_trigger(dev); |
||
1181 | if (err) |
||
1182 | return err; |
||
1183 | |||
1184 | return 0; |
||
1185 | } |
||
1186 | EXPORT_SYMBOL_GPL(register_switch); |
||
1187 | |||
1188 | void |
||
1189 | unregister_switch(struct switch_dev *dev) |
||
1190 | { |
||
1191 | swconfig_destroy_led_trigger(dev); |
||
1192 | kfree(dev->portbuf); |
||
1193 | mutex_lock(&dev->sw_mutex); |
||
1194 | swconfig_lock(); |
||
1195 | list_del(&dev->dev_list); |
||
1196 | swconfig_unlock(); |
||
1197 | mutex_unlock(&dev->sw_mutex); |
||
1198 | } |
||
1199 | EXPORT_SYMBOL_GPL(unregister_switch); |
||
1200 | |||
1201 | int |
||
1202 | switch_generic_set_link(struct switch_dev *dev, int port, |
||
1203 | struct switch_port_link *link) |
||
1204 | { |
||
1205 | if (WARN_ON(!dev->ops->phy_write16)) |
||
1206 | return -ENOTSUPP; |
||
1207 | |||
1208 | /* Generic implementation */ |
||
1209 | if (link->aneg) { |
||
1210 | dev->ops->phy_write16(dev, port, MII_BMCR, 0x0000); |
||
1211 | dev->ops->phy_write16(dev, port, MII_BMCR, BMCR_ANENABLE | BMCR_ANRESTART); |
||
1212 | } else { |
||
1213 | u16 bmcr = 0; |
||
1214 | |||
1215 | if (link->duplex) |
||
1216 | bmcr |= BMCR_FULLDPLX; |
||
1217 | |||
1218 | switch (link->speed) { |
||
1219 | case SWITCH_PORT_SPEED_10: |
||
1220 | break; |
||
1221 | case SWITCH_PORT_SPEED_100: |
||
1222 | bmcr |= BMCR_SPEED100; |
||
1223 | break; |
||
1224 | case SWITCH_PORT_SPEED_1000: |
||
1225 | bmcr |= BMCR_SPEED1000; |
||
1226 | break; |
||
1227 | default: |
||
1228 | return -ENOTSUPP; |
||
1229 | } |
||
1230 | |||
1231 | dev->ops->phy_write16(dev, port, MII_BMCR, bmcr); |
||
1232 | } |
||
1233 | |||
1234 | return 0; |
||
1235 | } |
||
1236 | |||
1237 | static int __init |
||
1238 | swconfig_init(void) |
||
1239 | { |
||
1240 | INIT_LIST_HEAD(&swdevs); |
||
1241 | |||
1242 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4,10,0) |
||
1243 | return genl_register_family_with_ops(&switch_fam, swconfig_ops); |
||
1244 | #else |
||
1245 | return genl_register_family(&switch_fam); |
||
1246 | #endif |
||
1247 | } |
||
1248 | |||
1249 | static void __exit |
||
1250 | swconfig_exit(void) |
||
1251 | { |
||
1252 | genl_unregister_family(&switch_fam); |
||
1253 | } |
||
1254 | |||
1255 | module_init(swconfig_init); |
||
1256 | module_exit(swconfig_exit); |