1
0

0013-net-add-swconfig-support.patch 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859
  1. From fe40f6aba9ba59000ffa681a23ad59e9347346af Mon Sep 17 00:00:00 2001
  2. From: Phil Sutter <phil@nwl.cc>
  3. Date: Tue, 13 May 2014 00:36:13 +0200
  4. Subject: [PATCH] net: add swconfig support
  5. ---
  6. drivers/net/phy/Kconfig | 10 +
  7. drivers/net/phy/Makefile | 1 +
  8. drivers/net/phy/swconfig.c | 1144 +++++++++++++++++++++++++++++++++++++++
  9. drivers/net/phy/swconfig_leds.c | 354 ++++++++++++
  10. include/linux/switch.h | 167 ++++++
  11. include/uapi/linux/Kbuild | 1 +
  12. include/uapi/linux/switch.h | 103 ++++
  13. 7 files changed, 1780 insertions(+)
  14. create mode 100644 drivers/net/phy/swconfig.c
  15. create mode 100644 drivers/net/phy/swconfig_leds.c
  16. create mode 100644 include/linux/switch.h
  17. create mode 100644 include/uapi/linux/switch.h
  18. diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
  19. index 9b5d46c..36a13fc 100644
  20. --- a/drivers/net/phy/Kconfig
  21. +++ b/drivers/net/phy/Kconfig
  22. @@ -12,6 +12,16 @@ menuconfig PHYLIB
  23. if PHYLIB
  24. +config SWCONFIG
  25. + tristate "Switch configuration API"
  26. + ---help---
  27. + Switch configuration API using netlink. This allows
  28. + you to configure the VLAN features of certain switches.
  29. +
  30. +config SWCONFIG_LEDS
  31. + bool "Switch LED trigger support"
  32. + depends on (SWCONFIG && LEDS_TRIGGERS)
  33. +
  34. comment "MII PHY device drivers"
  35. config AT803X_PHY
  36. diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
  37. index 9013dfa..b510bd6 100644
  38. --- a/drivers/net/phy/Makefile
  39. +++ b/drivers/net/phy/Makefile
  40. @@ -3,6 +3,7 @@
  41. libphy-objs := phy.o phy_device.o mdio_bus.o
  42. obj-$(CONFIG_PHYLIB) += libphy.o
  43. +obj-$(CONFIG_SWCONFIG) += swconfig.o
  44. obj-$(CONFIG_MARVELL_PHY) += marvell.o
  45. obj-$(CONFIG_DAVICOM_PHY) += davicom.o
  46. obj-$(CONFIG_CICADA_PHY) += cicada.o
  47. diff --git a/drivers/net/phy/swconfig.c b/drivers/net/phy/swconfig.c
  48. new file mode 100644
  49. index 0000000..c043ee4
  50. --- /dev/null
  51. +++ b/drivers/net/phy/swconfig.c
  52. @@ -0,0 +1,1144 @@
  53. +/*
  54. + * swconfig.c: Switch configuration API
  55. + *
  56. + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  57. + *
  58. + * This program is free software; you can redistribute it and/or
  59. + * modify it under the terms of the GNU General Public License
  60. + * as published by the Free Software Foundation; either version 2
  61. + * of the License, or (at your option) any later version.
  62. + *
  63. + * This program is distributed in the hope that it will be useful,
  64. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  65. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  66. + * GNU General Public License for more details.
  67. + */
  68. +
  69. +#include <linux/types.h>
  70. +#include <linux/module.h>
  71. +#include <linux/init.h>
  72. +#include <linux/list.h>
  73. +#include <linux/if.h>
  74. +#include <linux/if_ether.h>
  75. +#include <linux/capability.h>
  76. +#include <linux/skbuff.h>
  77. +#include <linux/switch.h>
  78. +#include <linux/of.h>
  79. +#include <linux/version.h>
  80. +
  81. +#define SWCONFIG_DEVNAME "switch%d"
  82. +
  83. +#include "swconfig_leds.c"
  84. +
  85. +MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
  86. +MODULE_LICENSE("GPL");
  87. +
  88. +static int swdev_id;
  89. +static struct list_head swdevs;
  90. +static DEFINE_SPINLOCK(swdevs_lock);
  91. +struct swconfig_callback;
  92. +
  93. +struct swconfig_callback {
  94. + struct sk_buff *msg;
  95. + struct genlmsghdr *hdr;
  96. + struct genl_info *info;
  97. + int cmd;
  98. +
  99. + /* callback for filling in the message data */
  100. + int (*fill)(struct swconfig_callback *cb, void *arg);
  101. +
  102. + /* callback for closing the message before sending it */
  103. + int (*close)(struct swconfig_callback *cb, void *arg);
  104. +
  105. + struct nlattr *nest[4];
  106. + int args[4];
  107. +};
  108. +
  109. +/* defaults */
  110. +
  111. +static int
  112. +swconfig_get_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
  113. + struct switch_val *val)
  114. +{
  115. + int ret;
  116. + if (val->port_vlan >= dev->vlans)
  117. + return -EINVAL;
  118. +
  119. + if (!dev->ops->get_vlan_ports)
  120. + return -EOPNOTSUPP;
  121. +
  122. + ret = dev->ops->get_vlan_ports(dev, val);
  123. + return ret;
  124. +}
  125. +
  126. +static int
  127. +swconfig_set_vlan_ports(struct switch_dev *dev, const struct switch_attr *attr,
  128. + struct switch_val *val)
  129. +{
  130. + struct switch_port *ports = val->value.ports;
  131. + const struct switch_dev_ops *ops = dev->ops;
  132. + int i;
  133. +
  134. + if (val->port_vlan >= dev->vlans)
  135. + return -EINVAL;
  136. +
  137. + /* validate ports */
  138. + if (val->len > dev->ports)
  139. + return -EINVAL;
  140. +
  141. + if (!ops->set_vlan_ports)
  142. + return -EOPNOTSUPP;
  143. +
  144. + for (i = 0; i < val->len; i++) {
  145. + if (ports[i].id >= dev->ports)
  146. + return -EINVAL;
  147. +
  148. + if (ops->set_port_pvid &&
  149. + !(ports[i].flags & (1 << SWITCH_PORT_FLAG_TAGGED)))
  150. + ops->set_port_pvid(dev, ports[i].id, val->port_vlan);
  151. + }
  152. +
  153. + return ops->set_vlan_ports(dev, val);
  154. +}
  155. +
  156. +static int
  157. +swconfig_set_pvid(struct switch_dev *dev, const struct switch_attr *attr,
  158. + struct switch_val *val)
  159. +{
  160. + if (val->port_vlan >= dev->ports)
  161. + return -EINVAL;
  162. +
  163. + if (!dev->ops->set_port_pvid)
  164. + return -EOPNOTSUPP;
  165. +
  166. + return dev->ops->set_port_pvid(dev, val->port_vlan, val->value.i);
  167. +}
  168. +
  169. +static int
  170. +swconfig_get_pvid(struct switch_dev *dev, const struct switch_attr *attr,
  171. + struct switch_val *val)
  172. +{
  173. + if (val->port_vlan >= dev->ports)
  174. + return -EINVAL;
  175. +
  176. + if (!dev->ops->get_port_pvid)
  177. + return -EOPNOTSUPP;
  178. +
  179. + return dev->ops->get_port_pvid(dev, val->port_vlan, &val->value.i);
  180. +}
  181. +
  182. +static const char *
  183. +swconfig_speed_str(enum switch_port_speed speed)
  184. +{
  185. + switch (speed) {
  186. + case SWITCH_PORT_SPEED_10:
  187. + return "10baseT";
  188. + case SWITCH_PORT_SPEED_100:
  189. + return "100baseT";
  190. + case SWITCH_PORT_SPEED_1000:
  191. + return "1000baseT";
  192. + default:
  193. + break;
  194. + }
  195. +
  196. + return "unknown";
  197. +}
  198. +
  199. +static int
  200. +swconfig_get_link(struct switch_dev *dev, const struct switch_attr *attr,
  201. + struct switch_val *val)
  202. +{
  203. + struct switch_port_link link;
  204. + int len;
  205. + int ret;
  206. +
  207. + if (val->port_vlan >= dev->ports)
  208. + return -EINVAL;
  209. +
  210. + if (!dev->ops->get_port_link)
  211. + return -EOPNOTSUPP;
  212. +
  213. + memset(&link, 0, sizeof(link));
  214. + ret = dev->ops->get_port_link(dev, val->port_vlan, &link);
  215. + if (ret)
  216. + return ret;
  217. +
  218. + memset(dev->buf, 0, sizeof(dev->buf));
  219. +
  220. + if (link.link)
  221. + len = snprintf(dev->buf, sizeof(dev->buf),
  222. + "port:%d link:up speed:%s %s-duplex %s%s%s",
  223. + val->port_vlan,
  224. + swconfig_speed_str(link.speed),
  225. + link.duplex ? "full" : "half",
  226. + link.tx_flow ? "txflow " : "",
  227. + link.rx_flow ? "rxflow " : "",
  228. + link.aneg ? "auto" : "");
  229. + else
  230. + len = snprintf(dev->buf, sizeof(dev->buf), "port:%d link:down",
  231. + val->port_vlan);
  232. +
  233. + val->value.s = dev->buf;
  234. + val->len = len;
  235. +
  236. + return 0;
  237. +}
  238. +
  239. +static int
  240. +swconfig_apply_config(struct switch_dev *dev, const struct switch_attr *attr,
  241. + struct switch_val *val)
  242. +{
  243. + /* don't complain if not supported by the switch driver */
  244. + if (!dev->ops->apply_config)
  245. + return 0;
  246. +
  247. + return dev->ops->apply_config(dev);
  248. +}
  249. +
  250. +static int
  251. +swconfig_reset_switch(struct switch_dev *dev, const struct switch_attr *attr,
  252. + struct switch_val *val)
  253. +{
  254. + /* don't complain if not supported by the switch driver */
  255. + if (!dev->ops->reset_switch)
  256. + return 0;
  257. +
  258. + return dev->ops->reset_switch(dev);
  259. +}
  260. +
  261. +enum global_defaults {
  262. + GLOBAL_APPLY,
  263. + GLOBAL_RESET,
  264. +};
  265. +
  266. +enum vlan_defaults {
  267. + VLAN_PORTS,
  268. +};
  269. +
  270. +enum port_defaults {
  271. + PORT_PVID,
  272. + PORT_LINK,
  273. +};
  274. +
  275. +static struct switch_attr default_global[] = {
  276. + [GLOBAL_APPLY] = {
  277. + .type = SWITCH_TYPE_NOVAL,
  278. + .name = "apply",
  279. + .description = "Activate changes in the hardware",
  280. + .set = swconfig_apply_config,
  281. + },
  282. + [GLOBAL_RESET] = {
  283. + .type = SWITCH_TYPE_NOVAL,
  284. + .name = "reset",
  285. + .description = "Reset the switch",
  286. + .set = swconfig_reset_switch,
  287. + }
  288. +};
  289. +
  290. +static struct switch_attr default_port[] = {
  291. + [PORT_PVID] = {
  292. + .type = SWITCH_TYPE_INT,
  293. + .name = "pvid",
  294. + .description = "Primary VLAN ID",
  295. + .set = swconfig_set_pvid,
  296. + .get = swconfig_get_pvid,
  297. + },
  298. + [PORT_LINK] = {
  299. + .type = SWITCH_TYPE_STRING,
  300. + .name = "link",
  301. + .description = "Get port link information",
  302. + .set = NULL,
  303. + .get = swconfig_get_link,
  304. + }
  305. +};
  306. +
  307. +static struct switch_attr default_vlan[] = {
  308. + [VLAN_PORTS] = {
  309. + .type = SWITCH_TYPE_PORTS,
  310. + .name = "ports",
  311. + .description = "VLAN port mapping",
  312. + .set = swconfig_set_vlan_ports,
  313. + .get = swconfig_get_vlan_ports,
  314. + },
  315. +};
  316. +
  317. +static const struct switch_attr *
  318. +swconfig_find_attr_by_name(const struct switch_attrlist *alist,
  319. + const char *name)
  320. +{
  321. + int i;
  322. +
  323. + for (i = 0; i < alist->n_attr; i++)
  324. + if (strcmp(name, alist->attr[i].name) == 0)
  325. + return &alist->attr[i];
  326. +
  327. + return NULL;
  328. +}
  329. +
  330. +static void swconfig_defaults_init(struct switch_dev *dev)
  331. +{
  332. + const struct switch_dev_ops *ops = dev->ops;
  333. +
  334. + dev->def_global = 0;
  335. + dev->def_vlan = 0;
  336. + dev->def_port = 0;
  337. +
  338. + if (ops->get_vlan_ports || ops->set_vlan_ports)
  339. + set_bit(VLAN_PORTS, &dev->def_vlan);
  340. +
  341. + if (ops->get_port_pvid || ops->set_port_pvid)
  342. + set_bit(PORT_PVID, &dev->def_port);
  343. +
  344. + if (ops->get_port_link &&
  345. + !swconfig_find_attr_by_name(&ops->attr_port, "link"))
  346. + set_bit(PORT_LINK, &dev->def_port);
  347. +
  348. + /* always present, can be no-op */
  349. + set_bit(GLOBAL_APPLY, &dev->def_global);
  350. + set_bit(GLOBAL_RESET, &dev->def_global);
  351. +}
  352. +
  353. +
  354. +static struct genl_family switch_fam = {
  355. + .id = GENL_ID_GENERATE,
  356. + .name = "switch",
  357. + .hdrsize = 0,
  358. + .version = 1,
  359. + .maxattr = SWITCH_ATTR_MAX,
  360. +};
  361. +
  362. +static const struct nla_policy switch_policy[SWITCH_ATTR_MAX+1] = {
  363. + [SWITCH_ATTR_ID] = { .type = NLA_U32 },
  364. + [SWITCH_ATTR_OP_ID] = { .type = NLA_U32 },
  365. + [SWITCH_ATTR_OP_PORT] = { .type = NLA_U32 },
  366. + [SWITCH_ATTR_OP_VLAN] = { .type = NLA_U32 },
  367. + [SWITCH_ATTR_OP_VALUE_INT] = { .type = NLA_U32 },
  368. + [SWITCH_ATTR_OP_VALUE_STR] = { .type = NLA_NUL_STRING },
  369. + [SWITCH_ATTR_OP_VALUE_PORTS] = { .type = NLA_NESTED },
  370. + [SWITCH_ATTR_TYPE] = { .type = NLA_U32 },
  371. +};
  372. +
  373. +static const struct nla_policy port_policy[SWITCH_PORT_ATTR_MAX+1] = {
  374. + [SWITCH_PORT_ID] = { .type = NLA_U32 },
  375. + [SWITCH_PORT_FLAG_TAGGED] = { .type = NLA_FLAG },
  376. +};
  377. +
  378. +static inline void
  379. +swconfig_lock(void)
  380. +{
  381. + spin_lock(&swdevs_lock);
  382. +}
  383. +
  384. +static inline void
  385. +swconfig_unlock(void)
  386. +{
  387. + spin_unlock(&swdevs_lock);
  388. +}
  389. +
  390. +static struct switch_dev *
  391. +swconfig_get_dev(struct genl_info *info)
  392. +{
  393. + struct switch_dev *dev = NULL;
  394. + struct switch_dev *p;
  395. + int id;
  396. +
  397. + if (!info->attrs[SWITCH_ATTR_ID])
  398. + goto done;
  399. +
  400. + id = nla_get_u32(info->attrs[SWITCH_ATTR_ID]);
  401. + swconfig_lock();
  402. + list_for_each_entry(p, &swdevs, dev_list) {
  403. + if (id != p->id)
  404. + continue;
  405. +
  406. + dev = p;
  407. + break;
  408. + }
  409. + if (dev)
  410. + mutex_lock(&dev->sw_mutex);
  411. + else
  412. + pr_debug("device %d not found\n", id);
  413. + swconfig_unlock();
  414. +done:
  415. + return dev;
  416. +}
  417. +
  418. +static inline void
  419. +swconfig_put_dev(struct switch_dev *dev)
  420. +{
  421. + mutex_unlock(&dev->sw_mutex);
  422. +}
  423. +
  424. +static int
  425. +swconfig_dump_attr(struct swconfig_callback *cb, void *arg)
  426. +{
  427. + struct switch_attr *op = arg;
  428. + struct genl_info *info = cb->info;
  429. + struct sk_buff *msg = cb->msg;
  430. + int id = cb->args[0];
  431. + void *hdr;
  432. +
  433. + hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
  434. + NLM_F_MULTI, SWITCH_CMD_NEW_ATTR);
  435. + if (IS_ERR(hdr))
  436. + return -1;
  437. +
  438. + if (nla_put_u32(msg, SWITCH_ATTR_OP_ID, id))
  439. + goto nla_put_failure;
  440. + if (nla_put_u32(msg, SWITCH_ATTR_OP_TYPE, op->type))
  441. + goto nla_put_failure;
  442. + if (nla_put_string(msg, SWITCH_ATTR_OP_NAME, op->name))
  443. + goto nla_put_failure;
  444. + if (op->description)
  445. + if (nla_put_string(msg, SWITCH_ATTR_OP_DESCRIPTION,
  446. + op->description))
  447. + goto nla_put_failure;
  448. +
  449. + return genlmsg_end(msg, hdr);
  450. +nla_put_failure:
  451. + genlmsg_cancel(msg, hdr);
  452. + return -EMSGSIZE;
  453. +}
  454. +
  455. +/* spread multipart messages across multiple message buffers */
  456. +static int
  457. +swconfig_send_multipart(struct swconfig_callback *cb, void *arg)
  458. +{
  459. + struct genl_info *info = cb->info;
  460. + int restart = 0;
  461. + int err;
  462. +
  463. + do {
  464. + if (!cb->msg) {
  465. + cb->msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  466. + if (cb->msg == NULL)
  467. + goto error;
  468. + }
  469. +
  470. + if (!(cb->fill(cb, arg) < 0))
  471. + break;
  472. +
  473. + /* fill failed, check if this was already the second attempt */
  474. + if (restart)
  475. + goto error;
  476. +
  477. + /* try again in a new message, send the current one */
  478. + restart = 1;
  479. + if (cb->close) {
  480. + if (cb->close(cb, arg) < 0)
  481. + goto error;
  482. + }
  483. + err = genlmsg_reply(cb->msg, info);
  484. + cb->msg = NULL;
  485. + if (err < 0)
  486. + goto error;
  487. +
  488. + } while (restart);
  489. +
  490. + return 0;
  491. +
  492. +error:
  493. + if (cb->msg)
  494. + nlmsg_free(cb->msg);
  495. + return -1;
  496. +}
  497. +
  498. +static int
  499. +swconfig_list_attrs(struct sk_buff *skb, struct genl_info *info)
  500. +{
  501. + struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  502. + const struct switch_attrlist *alist;
  503. + struct switch_dev *dev;
  504. + struct swconfig_callback cb;
  505. + int err = -EINVAL;
  506. + int i;
  507. +
  508. + /* defaults */
  509. + struct switch_attr *def_list;
  510. + unsigned long *def_active;
  511. + int n_def;
  512. +
  513. + dev = swconfig_get_dev(info);
  514. + if (!dev)
  515. + return -EINVAL;
  516. +
  517. + switch (hdr->cmd) {
  518. + case SWITCH_CMD_LIST_GLOBAL:
  519. + alist = &dev->ops->attr_global;
  520. + def_list = default_global;
  521. + def_active = &dev->def_global;
  522. + n_def = ARRAY_SIZE(default_global);
  523. + break;
  524. + case SWITCH_CMD_LIST_VLAN:
  525. + alist = &dev->ops->attr_vlan;
  526. + def_list = default_vlan;
  527. + def_active = &dev->def_vlan;
  528. + n_def = ARRAY_SIZE(default_vlan);
  529. + break;
  530. + case SWITCH_CMD_LIST_PORT:
  531. + alist = &dev->ops->attr_port;
  532. + def_list = default_port;
  533. + def_active = &dev->def_port;
  534. + n_def = ARRAY_SIZE(default_port);
  535. + break;
  536. + default:
  537. + WARN_ON(1);
  538. + goto out;
  539. + }
  540. +
  541. + memset(&cb, 0, sizeof(cb));
  542. + cb.info = info;
  543. + cb.fill = swconfig_dump_attr;
  544. + for (i = 0; i < alist->n_attr; i++) {
  545. + if (alist->attr[i].disabled)
  546. + continue;
  547. + cb.args[0] = i;
  548. + err = swconfig_send_multipart(&cb, (void *) &alist->attr[i]);
  549. + if (err < 0)
  550. + goto error;
  551. + }
  552. +
  553. + /* defaults */
  554. + for (i = 0; i < n_def; i++) {
  555. + if (!test_bit(i, def_active))
  556. + continue;
  557. + cb.args[0] = SWITCH_ATTR_DEFAULTS_OFFSET + i;
  558. + err = swconfig_send_multipart(&cb, (void *) &def_list[i]);
  559. + if (err < 0)
  560. + goto error;
  561. + }
  562. + swconfig_put_dev(dev);
  563. +
  564. + if (!cb.msg)
  565. + return 0;
  566. +
  567. + return genlmsg_reply(cb.msg, info);
  568. +
  569. +error:
  570. + if (cb.msg)
  571. + nlmsg_free(cb.msg);
  572. +out:
  573. + swconfig_put_dev(dev);
  574. + return err;
  575. +}
  576. +
  577. +static const struct switch_attr *
  578. +swconfig_lookup_attr(struct switch_dev *dev, struct genl_info *info,
  579. + struct switch_val *val)
  580. +{
  581. + struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  582. + const struct switch_attrlist *alist;
  583. + const struct switch_attr *attr = NULL;
  584. + int attr_id;
  585. +
  586. + /* defaults */
  587. + struct switch_attr *def_list;
  588. + unsigned long *def_active;
  589. + int n_def;
  590. +
  591. + if (!info->attrs[SWITCH_ATTR_OP_ID])
  592. + goto done;
  593. +
  594. + switch (hdr->cmd) {
  595. + case SWITCH_CMD_SET_GLOBAL:
  596. + case SWITCH_CMD_GET_GLOBAL:
  597. + alist = &dev->ops->attr_global;
  598. + def_list = default_global;
  599. + def_active = &dev->def_global;
  600. + n_def = ARRAY_SIZE(default_global);
  601. + break;
  602. + case SWITCH_CMD_SET_VLAN:
  603. + case SWITCH_CMD_GET_VLAN:
  604. + alist = &dev->ops->attr_vlan;
  605. + def_list = default_vlan;
  606. + def_active = &dev->def_vlan;
  607. + n_def = ARRAY_SIZE(default_vlan);
  608. + if (!info->attrs[SWITCH_ATTR_OP_VLAN])
  609. + goto done;
  610. + val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_VLAN]);
  611. + if (val->port_vlan >= dev->vlans)
  612. + goto done;
  613. + break;
  614. + case SWITCH_CMD_SET_PORT:
  615. + case SWITCH_CMD_GET_PORT:
  616. + alist = &dev->ops->attr_port;
  617. + def_list = default_port;
  618. + def_active = &dev->def_port;
  619. + n_def = ARRAY_SIZE(default_port);
  620. + if (!info->attrs[SWITCH_ATTR_OP_PORT])
  621. + goto done;
  622. + val->port_vlan = nla_get_u32(info->attrs[SWITCH_ATTR_OP_PORT]);
  623. + if (val->port_vlan >= dev->ports)
  624. + goto done;
  625. + break;
  626. + default:
  627. + WARN_ON(1);
  628. + goto done;
  629. + }
  630. +
  631. + if (!alist)
  632. + goto done;
  633. +
  634. + attr_id = nla_get_u32(info->attrs[SWITCH_ATTR_OP_ID]);
  635. + if (attr_id >= SWITCH_ATTR_DEFAULTS_OFFSET) {
  636. + attr_id -= SWITCH_ATTR_DEFAULTS_OFFSET;
  637. + if (attr_id >= n_def)
  638. + goto done;
  639. + if (!test_bit(attr_id, def_active))
  640. + goto done;
  641. + attr = &def_list[attr_id];
  642. + } else {
  643. + if (attr_id >= alist->n_attr)
  644. + goto done;
  645. + attr = &alist->attr[attr_id];
  646. + }
  647. +
  648. + if (attr->disabled)
  649. + attr = NULL;
  650. +
  651. +done:
  652. + if (!attr)
  653. + pr_debug("attribute lookup failed\n");
  654. + val->attr = attr;
  655. + return attr;
  656. +}
  657. +
  658. +static int
  659. +swconfig_parse_ports(struct sk_buff *msg, struct nlattr *head,
  660. + struct switch_val *val, int max)
  661. +{
  662. + struct nlattr *nla;
  663. + int rem;
  664. +
  665. + val->len = 0;
  666. + nla_for_each_nested(nla, head, rem) {
  667. + struct nlattr *tb[SWITCH_PORT_ATTR_MAX+1];
  668. + struct switch_port *port = &val->value.ports[val->len];
  669. +
  670. + if (val->len >= max)
  671. + return -EINVAL;
  672. +
  673. + if (nla_parse_nested(tb, SWITCH_PORT_ATTR_MAX, nla,
  674. + port_policy))
  675. + return -EINVAL;
  676. +
  677. + if (!tb[SWITCH_PORT_ID])
  678. + return -EINVAL;
  679. +
  680. + port->id = nla_get_u32(tb[SWITCH_PORT_ID]);
  681. + if (tb[SWITCH_PORT_FLAG_TAGGED])
  682. + port->flags |= (1 << SWITCH_PORT_FLAG_TAGGED);
  683. + val->len++;
  684. + }
  685. +
  686. + return 0;
  687. +}
  688. +
  689. +static int
  690. +swconfig_set_attr(struct sk_buff *skb, struct genl_info *info)
  691. +{
  692. + const struct switch_attr *attr;
  693. + struct switch_dev *dev;
  694. + struct switch_val val;
  695. + int err = -EINVAL;
  696. +
  697. + dev = swconfig_get_dev(info);
  698. + if (!dev)
  699. + return -EINVAL;
  700. +
  701. + memset(&val, 0, sizeof(val));
  702. + attr = swconfig_lookup_attr(dev, info, &val);
  703. + if (!attr || !attr->set)
  704. + goto error;
  705. +
  706. + val.attr = attr;
  707. + switch (attr->type) {
  708. + case SWITCH_TYPE_NOVAL:
  709. + break;
  710. + case SWITCH_TYPE_INT:
  711. + if (!info->attrs[SWITCH_ATTR_OP_VALUE_INT])
  712. + goto error;
  713. + val.value.i =
  714. + nla_get_u32(info->attrs[SWITCH_ATTR_OP_VALUE_INT]);
  715. + break;
  716. + case SWITCH_TYPE_STRING:
  717. + if (!info->attrs[SWITCH_ATTR_OP_VALUE_STR])
  718. + goto error;
  719. + val.value.s =
  720. + nla_data(info->attrs[SWITCH_ATTR_OP_VALUE_STR]);
  721. + break;
  722. + case SWITCH_TYPE_PORTS:
  723. + val.value.ports = dev->portbuf;
  724. + memset(dev->portbuf, 0,
  725. + sizeof(struct switch_port) * dev->ports);
  726. +
  727. + /* TODO: implement multipart? */
  728. + if (info->attrs[SWITCH_ATTR_OP_VALUE_PORTS]) {
  729. + err = swconfig_parse_ports(skb,
  730. + info->attrs[SWITCH_ATTR_OP_VALUE_PORTS],
  731. + &val, dev->ports);
  732. + if (err < 0)
  733. + goto error;
  734. + } else {
  735. + val.len = 0;
  736. + err = 0;
  737. + }
  738. + break;
  739. + default:
  740. + goto error;
  741. + }
  742. +
  743. + err = attr->set(dev, attr, &val);
  744. +error:
  745. + swconfig_put_dev(dev);
  746. + return err;
  747. +}
  748. +
  749. +static int
  750. +swconfig_close_portlist(struct swconfig_callback *cb, void *arg)
  751. +{
  752. + if (cb->nest[0])
  753. + nla_nest_end(cb->msg, cb->nest[0]);
  754. + return 0;
  755. +}
  756. +
  757. +static int
  758. +swconfig_send_port(struct swconfig_callback *cb, void *arg)
  759. +{
  760. + const struct switch_port *port = arg;
  761. + struct nlattr *p = NULL;
  762. +
  763. + if (!cb->nest[0]) {
  764. + cb->nest[0] = nla_nest_start(cb->msg, cb->cmd);
  765. + if (!cb->nest[0])
  766. + return -1;
  767. + }
  768. +
  769. + p = nla_nest_start(cb->msg, SWITCH_ATTR_PORT);
  770. + if (!p)
  771. + goto error;
  772. +
  773. + if (nla_put_u32(cb->msg, SWITCH_PORT_ID, port->id))
  774. + goto nla_put_failure;
  775. + if (port->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
  776. + if (nla_put_flag(cb->msg, SWITCH_PORT_FLAG_TAGGED))
  777. + goto nla_put_failure;
  778. + }
  779. +
  780. + nla_nest_end(cb->msg, p);
  781. + return 0;
  782. +
  783. +nla_put_failure:
  784. + nla_nest_cancel(cb->msg, p);
  785. +error:
  786. + nla_nest_cancel(cb->msg, cb->nest[0]);
  787. + return -1;
  788. +}
  789. +
  790. +static int
  791. +swconfig_send_ports(struct sk_buff **msg, struct genl_info *info, int attr,
  792. + const struct switch_val *val)
  793. +{
  794. + struct swconfig_callback cb;
  795. + int err = 0;
  796. + int i;
  797. +
  798. + if (!val->value.ports)
  799. + return -EINVAL;
  800. +
  801. + memset(&cb, 0, sizeof(cb));
  802. + cb.cmd = attr;
  803. + cb.msg = *msg;
  804. + cb.info = info;
  805. + cb.fill = swconfig_send_port;
  806. + cb.close = swconfig_close_portlist;
  807. +
  808. + cb.nest[0] = nla_nest_start(cb.msg, cb.cmd);
  809. + for (i = 0; i < val->len; i++) {
  810. + err = swconfig_send_multipart(&cb, &val->value.ports[i]);
  811. + if (err)
  812. + goto done;
  813. + }
  814. + err = val->len;
  815. + swconfig_close_portlist(&cb, NULL);
  816. + *msg = cb.msg;
  817. +
  818. +done:
  819. + return err;
  820. +}
  821. +
  822. +static int
  823. +swconfig_get_attr(struct sk_buff *skb, struct genl_info *info)
  824. +{
  825. + struct genlmsghdr *hdr = nlmsg_data(info->nlhdr);
  826. + const struct switch_attr *attr;
  827. + struct switch_dev *dev;
  828. + struct sk_buff *msg = NULL;
  829. + struct switch_val val;
  830. + int err = -EINVAL;
  831. + int cmd = hdr->cmd;
  832. +
  833. + dev = swconfig_get_dev(info);
  834. + if (!dev)
  835. + return -EINVAL;
  836. +
  837. + memset(&val, 0, sizeof(val));
  838. + attr = swconfig_lookup_attr(dev, info, &val);
  839. + if (!attr || !attr->get)
  840. + goto error;
  841. +
  842. + if (attr->type == SWITCH_TYPE_PORTS) {
  843. + val.value.ports = dev->portbuf;
  844. + memset(dev->portbuf, 0,
  845. + sizeof(struct switch_port) * dev->ports);
  846. + }
  847. +
  848. + err = attr->get(dev, attr, &val);
  849. + if (err)
  850. + goto error;
  851. +
  852. + msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
  853. + if (!msg)
  854. + goto error;
  855. +
  856. + hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq, &switch_fam,
  857. + 0, cmd);
  858. + if (IS_ERR(hdr))
  859. + goto nla_put_failure;
  860. +
  861. + switch (attr->type) {
  862. + case SWITCH_TYPE_INT:
  863. + if (nla_put_u32(msg, SWITCH_ATTR_OP_VALUE_INT, val.value.i))
  864. + goto nla_put_failure;
  865. + break;
  866. + case SWITCH_TYPE_STRING:
  867. + if (nla_put_string(msg, SWITCH_ATTR_OP_VALUE_STR, val.value.s))
  868. + goto nla_put_failure;
  869. + break;
  870. + case SWITCH_TYPE_PORTS:
  871. + err = swconfig_send_ports(&msg, info,
  872. + SWITCH_ATTR_OP_VALUE_PORTS, &val);
  873. + if (err < 0)
  874. + goto nla_put_failure;
  875. + break;
  876. + default:
  877. + pr_debug("invalid type in attribute\n");
  878. + err = -EINVAL;
  879. + goto error;
  880. + }
  881. + err = genlmsg_end(msg, hdr);
  882. + if (err < 0)
  883. + goto nla_put_failure;
  884. +
  885. + swconfig_put_dev(dev);
  886. + return genlmsg_reply(msg, info);
  887. +
  888. +nla_put_failure:
  889. + if (msg)
  890. + nlmsg_free(msg);
  891. +error:
  892. + swconfig_put_dev(dev);
  893. + if (!err)
  894. + err = -ENOMEM;
  895. + return err;
  896. +}
  897. +
  898. +static int
  899. +swconfig_send_switch(struct sk_buff *msg, u32 pid, u32 seq, int flags,
  900. + const struct switch_dev *dev)
  901. +{
  902. + struct nlattr *p = NULL, *m = NULL;
  903. + void *hdr;
  904. + int i;
  905. +
  906. + hdr = genlmsg_put(msg, pid, seq, &switch_fam, flags,
  907. + SWITCH_CMD_NEW_ATTR);
  908. + if (IS_ERR(hdr))
  909. + return -1;
  910. +
  911. + if (nla_put_u32(msg, SWITCH_ATTR_ID, dev->id))
  912. + goto nla_put_failure;
  913. + if (nla_put_string(msg, SWITCH_ATTR_DEV_NAME, dev->devname))
  914. + goto nla_put_failure;
  915. + if (nla_put_string(msg, SWITCH_ATTR_ALIAS, dev->alias))
  916. + goto nla_put_failure;
  917. + if (nla_put_string(msg, SWITCH_ATTR_NAME, dev->name))
  918. + goto nla_put_failure;
  919. + if (nla_put_u32(msg, SWITCH_ATTR_VLANS, dev->vlans))
  920. + goto nla_put_failure;
  921. + if (nla_put_u32(msg, SWITCH_ATTR_PORTS, dev->ports))
  922. + goto nla_put_failure;
  923. + if (nla_put_u32(msg, SWITCH_ATTR_CPU_PORT, dev->cpu_port))
  924. + goto nla_put_failure;
  925. +
  926. + m = nla_nest_start(msg, SWITCH_ATTR_PORTMAP);
  927. + if (!m)
  928. + goto nla_put_failure;
  929. + for (i = 0; i < dev->ports; i++) {
  930. + p = nla_nest_start(msg, SWITCH_ATTR_PORTS);
  931. + if (!p)
  932. + continue;
  933. + if (dev->portmap[i].s) {
  934. + if (nla_put_string(msg, SWITCH_PORTMAP_SEGMENT,
  935. + dev->portmap[i].s))
  936. + goto nla_put_failure;
  937. + if (nla_put_u32(msg, SWITCH_PORTMAP_VIRT,
  938. + dev->portmap[i].virt))
  939. + goto nla_put_failure;
  940. + }
  941. + nla_nest_end(msg, p);
  942. + }
  943. + nla_nest_end(msg, m);
  944. + return genlmsg_end(msg, hdr);
  945. +nla_put_failure:
  946. + genlmsg_cancel(msg, hdr);
  947. + return -EMSGSIZE;
  948. +}
  949. +
  950. +static int swconfig_dump_switches(struct sk_buff *skb,
  951. + struct netlink_callback *cb)
  952. +{
  953. + struct switch_dev *dev;
  954. + int start = cb->args[0];
  955. + int idx = 0;
  956. +
  957. + swconfig_lock();
  958. + list_for_each_entry(dev, &swdevs, dev_list) {
  959. + if (++idx <= start)
  960. + continue;
  961. + if (swconfig_send_switch(skb, NETLINK_CB(cb->skb).portid,
  962. + cb->nlh->nlmsg_seq, NLM_F_MULTI,
  963. + dev) < 0)
  964. + break;
  965. + }
  966. + swconfig_unlock();
  967. + cb->args[0] = idx;
  968. +
  969. + return skb->len;
  970. +}
  971. +
  972. +static int
  973. +swconfig_done(struct netlink_callback *cb)
  974. +{
  975. + return 0;
  976. +}
  977. +
  978. +static struct genl_ops swconfig_ops[] = {
  979. + {
  980. + .cmd = SWITCH_CMD_LIST_GLOBAL,
  981. + .doit = swconfig_list_attrs,
  982. + .policy = switch_policy,
  983. + },
  984. + {
  985. + .cmd = SWITCH_CMD_LIST_VLAN,
  986. + .doit = swconfig_list_attrs,
  987. + .policy = switch_policy,
  988. + },
  989. + {
  990. + .cmd = SWITCH_CMD_LIST_PORT,
  991. + .doit = swconfig_list_attrs,
  992. + .policy = switch_policy,
  993. + },
  994. + {
  995. + .cmd = SWITCH_CMD_GET_GLOBAL,
  996. + .doit = swconfig_get_attr,
  997. + .policy = switch_policy,
  998. + },
  999. + {
  1000. + .cmd = SWITCH_CMD_GET_VLAN,
  1001. + .doit = swconfig_get_attr,
  1002. + .policy = switch_policy,
  1003. + },
  1004. + {
  1005. + .cmd = SWITCH_CMD_GET_PORT,
  1006. + .doit = swconfig_get_attr,
  1007. + .policy = switch_policy,
  1008. + },
  1009. + {
  1010. + .cmd = SWITCH_CMD_SET_GLOBAL,
  1011. + .doit = swconfig_set_attr,
  1012. + .policy = switch_policy,
  1013. + },
  1014. + {
  1015. + .cmd = SWITCH_CMD_SET_VLAN,
  1016. + .doit = swconfig_set_attr,
  1017. + .policy = switch_policy,
  1018. + },
  1019. + {
  1020. + .cmd = SWITCH_CMD_SET_PORT,
  1021. + .doit = swconfig_set_attr,
  1022. + .policy = switch_policy,
  1023. + },
  1024. + {
  1025. + .cmd = SWITCH_CMD_GET_SWITCH,
  1026. + .dumpit = swconfig_dump_switches,
  1027. + .policy = switch_policy,
  1028. + .done = swconfig_done,
  1029. + }
  1030. +};
  1031. +
  1032. +#ifdef CONFIG_OF
  1033. +void
  1034. +of_switch_load_portmap(struct switch_dev *dev)
  1035. +{
  1036. + struct device_node *port;
  1037. +
  1038. + if (!dev->of_node)
  1039. + return;
  1040. +
  1041. + for_each_child_of_node(dev->of_node, port) {
  1042. + const __be32 *prop;
  1043. + const char *segment;
  1044. + int size, phys;
  1045. +
  1046. + if (!of_device_is_compatible(port, "swconfig,port"))
  1047. + continue;
  1048. +
  1049. + if (of_property_read_string(port, "swconfig,segment", &segment))
  1050. + continue;
  1051. +
  1052. + prop = of_get_property(port, "swconfig,portmap", &size);
  1053. + if (!prop)
  1054. + continue;
  1055. +
  1056. + if (size != (2 * sizeof(*prop))) {
  1057. + pr_err("%s: failed to parse port mapping\n",
  1058. + port->name);
  1059. + continue;
  1060. + }
  1061. +
  1062. + phys = be32_to_cpup(prop++);
  1063. + if ((phys < 0) | (phys >= dev->ports)) {
  1064. + pr_err("%s: physical port index out of range\n",
  1065. + port->name);
  1066. + continue;
  1067. + }
  1068. +
  1069. + dev->portmap[phys].s = kstrdup(segment, GFP_KERNEL);
  1070. + dev->portmap[phys].virt = be32_to_cpup(prop);
  1071. + pr_debug("Found port: %s, physical: %d, virtual: %d\n",
  1072. + segment, phys, dev->portmap[phys].virt);
  1073. + }
  1074. +}
  1075. +#endif
  1076. +
  1077. +int
  1078. +register_switch(struct switch_dev *dev, struct net_device *netdev)
  1079. +{
  1080. + struct switch_dev *sdev;
  1081. + const int max_switches = 8 * sizeof(unsigned long);
  1082. + unsigned long in_use = 0;
  1083. + int err;
  1084. + int i;
  1085. +
  1086. + INIT_LIST_HEAD(&dev->dev_list);
  1087. + if (netdev) {
  1088. + dev->netdev = netdev;
  1089. + if (!dev->alias)
  1090. + dev->alias = netdev->name;
  1091. + }
  1092. + BUG_ON(!dev->alias);
  1093. +
  1094. + if (dev->ports > 0) {
  1095. + dev->portbuf = kzalloc(sizeof(struct switch_port) *
  1096. + dev->ports, GFP_KERNEL);
  1097. + if (!dev->portbuf)
  1098. + return -ENOMEM;
  1099. + dev->portmap = kzalloc(sizeof(struct switch_portmap) *
  1100. + dev->ports, GFP_KERNEL);
  1101. + if (!dev->portmap) {
  1102. + kfree(dev->portbuf);
  1103. + return -ENOMEM;
  1104. + }
  1105. + }
  1106. + swconfig_defaults_init(dev);
  1107. + mutex_init(&dev->sw_mutex);
  1108. + swconfig_lock();
  1109. + dev->id = ++swdev_id;
  1110. +
  1111. + list_for_each_entry(sdev, &swdevs, dev_list) {
  1112. + if (!sscanf(sdev->devname, SWCONFIG_DEVNAME, &i))
  1113. + continue;
  1114. + if (i < 0 || i > max_switches)
  1115. + continue;
  1116. +
  1117. + set_bit(i, &in_use);
  1118. + }
  1119. + i = find_first_zero_bit(&in_use, max_switches);
  1120. +
  1121. + if (i == max_switches) {
  1122. + swconfig_unlock();
  1123. + return -ENFILE;
  1124. + }
  1125. +
  1126. +#ifdef CONFIG_OF
  1127. + if (dev->ports)
  1128. + of_switch_load_portmap(dev);
  1129. +#endif
  1130. +
  1131. + /* fill device name */
  1132. + snprintf(dev->devname, IFNAMSIZ, SWCONFIG_DEVNAME, i);
  1133. +
  1134. + list_add(&dev->dev_list, &swdevs);
  1135. + swconfig_unlock();
  1136. +
  1137. + err = swconfig_create_led_trigger(dev);
  1138. + if (err)
  1139. + return err;
  1140. +
  1141. + return 0;
  1142. +}
  1143. +EXPORT_SYMBOL_GPL(register_switch);
  1144. +
  1145. +void
  1146. +unregister_switch(struct switch_dev *dev)
  1147. +{
  1148. + swconfig_destroy_led_trigger(dev);
  1149. + kfree(dev->portbuf);
  1150. + mutex_lock(&dev->sw_mutex);
  1151. + swconfig_lock();
  1152. + list_del(&dev->dev_list);
  1153. + swconfig_unlock();
  1154. + mutex_unlock(&dev->sw_mutex);
  1155. +}
  1156. +EXPORT_SYMBOL_GPL(unregister_switch);
  1157. +
  1158. +
  1159. +static int __init
  1160. +swconfig_init(void)
  1161. +{
  1162. + int i, err;
  1163. +
  1164. + INIT_LIST_HEAD(&swdevs);
  1165. +
  1166. +#if (LINUX_VERSION_CODE < KERNEL_VERSION(3,13,0))
  1167. + err = genl_register_family(&switch_fam);
  1168. + if (err)
  1169. + return err;
  1170. +
  1171. + for (i = 0; i < ARRAY_SIZE(swconfig_ops); i++) {
  1172. + err = genl_register_ops(&switch_fam, &swconfig_ops[i]);
  1173. + if (err)
  1174. + goto unregister;
  1175. + }
  1176. +#else
  1177. + err = genl_register_family_with_ops(&switch_fam, swconfig_ops);
  1178. + if (err)
  1179. + return err;
  1180. +#endif
  1181. + return 0;
  1182. +
  1183. +unregister:
  1184. + genl_unregister_family(&switch_fam);
  1185. + return err;
  1186. +}
  1187. +
  1188. +static void __exit
  1189. +swconfig_exit(void)
  1190. +{
  1191. + genl_unregister_family(&switch_fam);
  1192. +}
  1193. +
  1194. +module_init(swconfig_init);
  1195. +module_exit(swconfig_exit);
  1196. +
  1197. diff --git a/drivers/net/phy/swconfig_leds.c b/drivers/net/phy/swconfig_leds.c
  1198. new file mode 100644
  1199. index 0000000..abd7bed
  1200. --- /dev/null
  1201. +++ b/drivers/net/phy/swconfig_leds.c
  1202. @@ -0,0 +1,354 @@
  1203. +/*
  1204. + * swconfig_led.c: LED trigger support for the switch configuration API
  1205. + *
  1206. + * Copyright (C) 2011 Gabor Juhos <juhosg@openwrt.org>
  1207. + *
  1208. + * This program is free software; you can redistribute it and/or
  1209. + * modify it under the terms of the GNU General Public License
  1210. + * as published by the Free Software Foundation; either version 2
  1211. + * of the License, or (at your option) any later version.
  1212. + *
  1213. + */
  1214. +
  1215. +#ifdef CONFIG_SWCONFIG_LEDS
  1216. +
  1217. +#include <linux/leds.h>
  1218. +#include <linux/ctype.h>
  1219. +#include <linux/device.h>
  1220. +#include <linux/workqueue.h>
  1221. +
  1222. +#define SWCONFIG_LED_TIMER_INTERVAL (HZ / 10)
  1223. +#define SWCONFIG_LED_NUM_PORTS 32
  1224. +
  1225. +struct switch_led_trigger {
  1226. + struct led_trigger trig;
  1227. + struct switch_dev *swdev;
  1228. +
  1229. + struct delayed_work sw_led_work;
  1230. + u32 port_mask;
  1231. + u32 port_link;
  1232. + unsigned long port_traffic[SWCONFIG_LED_NUM_PORTS];
  1233. +};
  1234. +
  1235. +struct swconfig_trig_data {
  1236. + struct led_classdev *led_cdev;
  1237. + struct switch_dev *swdev;
  1238. +
  1239. + rwlock_t lock;
  1240. + u32 port_mask;
  1241. +
  1242. + bool prev_link;
  1243. + unsigned long prev_traffic;
  1244. + enum led_brightness prev_brightness;
  1245. +};
  1246. +
  1247. +static void
  1248. +swconfig_trig_set_brightness(struct swconfig_trig_data *trig_data,
  1249. + enum led_brightness brightness)
  1250. +{
  1251. + led_set_brightness(trig_data->led_cdev, brightness);
  1252. + trig_data->prev_brightness = brightness;
  1253. +}
  1254. +
  1255. +static void
  1256. +swconfig_trig_update_port_mask(struct led_trigger *trigger)
  1257. +{
  1258. + struct list_head *entry;
  1259. + struct switch_led_trigger *sw_trig;
  1260. + u32 port_mask;
  1261. +
  1262. + if (!trigger)
  1263. + return;
  1264. +
  1265. + sw_trig = (void *) trigger;
  1266. +
  1267. + port_mask = 0;
  1268. + read_lock(&trigger->leddev_list_lock);
  1269. + list_for_each(entry, &trigger->led_cdevs) {
  1270. + struct led_classdev *led_cdev;
  1271. + struct swconfig_trig_data *trig_data;
  1272. +
  1273. + led_cdev = list_entry(entry, struct led_classdev, trig_list);
  1274. + trig_data = led_cdev->trigger_data;
  1275. + if (trig_data) {
  1276. + read_lock(&trig_data->lock);
  1277. + port_mask |= trig_data->port_mask;
  1278. + read_unlock(&trig_data->lock);
  1279. + }
  1280. + }
  1281. + read_unlock(&trigger->leddev_list_lock);
  1282. +
  1283. + sw_trig->port_mask = port_mask;
  1284. +
  1285. + if (port_mask)
  1286. + schedule_delayed_work(&sw_trig->sw_led_work,
  1287. + SWCONFIG_LED_TIMER_INTERVAL);
  1288. + else
  1289. + cancel_delayed_work_sync(&sw_trig->sw_led_work);
  1290. +}
  1291. +
  1292. +static ssize_t
  1293. +swconfig_trig_port_mask_store(struct device *dev, struct device_attribute *attr,
  1294. + const char *buf, size_t size)
  1295. +{
  1296. + struct led_classdev *led_cdev = dev_get_drvdata(dev);
  1297. + struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  1298. + unsigned long port_mask;
  1299. + ssize_t ret = -EINVAL;
  1300. + char *after;
  1301. + size_t count;
  1302. +
  1303. + port_mask = simple_strtoul(buf, &after, 16);
  1304. + count = after - buf;
  1305. +
  1306. + if (*after && isspace(*after))
  1307. + count++;
  1308. +
  1309. + if (count == size) {
  1310. + bool changed;
  1311. +
  1312. + write_lock(&trig_data->lock);
  1313. +
  1314. + changed = (trig_data->port_mask != port_mask);
  1315. + if (changed) {
  1316. + trig_data->port_mask = port_mask;
  1317. + if (port_mask == 0)
  1318. + swconfig_trig_set_brightness(trig_data, LED_OFF);
  1319. + }
  1320. +
  1321. + write_unlock(&trig_data->lock);
  1322. +
  1323. + if (changed)
  1324. + swconfig_trig_update_port_mask(led_cdev->trigger);
  1325. +
  1326. + ret = count;
  1327. + }
  1328. +
  1329. + return ret;
  1330. +}
  1331. +
  1332. +static ssize_t
  1333. +swconfig_trig_port_mask_show(struct device *dev, struct device_attribute *attr,
  1334. + char *buf)
  1335. +{
  1336. + struct led_classdev *led_cdev = dev_get_drvdata(dev);
  1337. + struct swconfig_trig_data *trig_data = led_cdev->trigger_data;
  1338. +
  1339. + read_lock(&trig_data->lock);
  1340. + sprintf(buf, "%#x\n", trig_data->port_mask);
  1341. + read_unlock(&trig_data->lock);
  1342. +
  1343. + return strlen(buf) + 1;
  1344. +}
  1345. +
  1346. +static DEVICE_ATTR(port_mask, 0644, swconfig_trig_port_mask_show,
  1347. + swconfig_trig_port_mask_store);
  1348. +
  1349. +static void
  1350. +swconfig_trig_activate(struct led_classdev *led_cdev)
  1351. +{
  1352. + struct switch_led_trigger *sw_trig;
  1353. + struct swconfig_trig_data *trig_data;
  1354. + int err;
  1355. +
  1356. + if (led_cdev->trigger->activate != swconfig_trig_activate)
  1357. + return;
  1358. +
  1359. + trig_data = kzalloc(sizeof(struct swconfig_trig_data), GFP_KERNEL);
  1360. + if (!trig_data)
  1361. + return;
  1362. +
  1363. + sw_trig = (void *) led_cdev->trigger;
  1364. +
  1365. + rwlock_init(&trig_data->lock);
  1366. + trig_data->led_cdev = led_cdev;
  1367. + trig_data->swdev = sw_trig->swdev;
  1368. + led_cdev->trigger_data = trig_data;
  1369. +
  1370. + err = device_create_file(led_cdev->dev, &dev_attr_port_mask);
  1371. + if (err)
  1372. + goto err_free;
  1373. +
  1374. + return;
  1375. +
  1376. +err_free:
  1377. + led_cdev->trigger_data = NULL;
  1378. + kfree(trig_data);
  1379. +}
  1380. +
  1381. +static void
  1382. +swconfig_trig_deactivate(struct led_classdev *led_cdev)
  1383. +{
  1384. + struct swconfig_trig_data *trig_data;
  1385. +
  1386. + swconfig_trig_update_port_mask(led_cdev->trigger);
  1387. +
  1388. + trig_data = (void *) led_cdev->trigger_data;
  1389. + if (trig_data) {
  1390. + device_remove_file(led_cdev->dev, &dev_attr_port_mask);
  1391. + kfree(trig_data);
  1392. + }
  1393. +}
  1394. +
  1395. +static void
  1396. +swconfig_trig_led_event(struct switch_led_trigger *sw_trig,
  1397. + struct led_classdev *led_cdev)
  1398. +{
  1399. + struct swconfig_trig_data *trig_data;
  1400. + u32 port_mask;
  1401. + bool link;
  1402. +
  1403. + trig_data = led_cdev->trigger_data;
  1404. + if (!trig_data)
  1405. + return;
  1406. +
  1407. + read_lock(&trig_data->lock);
  1408. + port_mask = trig_data->port_mask;
  1409. + read_unlock(&trig_data->lock);
  1410. +
  1411. + link = !!(sw_trig->port_link & port_mask);
  1412. + if (!link) {
  1413. + if (link != trig_data->prev_link)
  1414. + swconfig_trig_set_brightness(trig_data, LED_OFF);
  1415. + } else {
  1416. + unsigned long traffic;
  1417. + int i;
  1418. +
  1419. + traffic = 0;
  1420. + for (i = 0; i < SWCONFIG_LED_NUM_PORTS; i++) {
  1421. + if (port_mask & (1 << i))
  1422. + traffic += sw_trig->port_traffic[i];
  1423. + }
  1424. +
  1425. + if (trig_data->prev_brightness != LED_FULL)
  1426. + swconfig_trig_set_brightness(trig_data, LED_FULL);
  1427. + else if (traffic != trig_data->prev_traffic)
  1428. + swconfig_trig_set_brightness(trig_data, LED_OFF);
  1429. +
  1430. + trig_data->prev_traffic = traffic;
  1431. + }
  1432. +
  1433. + trig_data->prev_link = link;
  1434. +}
  1435. +
  1436. +static void
  1437. +swconfig_trig_update_leds(struct switch_led_trigger *sw_trig)
  1438. +{
  1439. + struct list_head *entry;
  1440. + struct led_trigger *trigger;
  1441. +
  1442. + trigger = &sw_trig->trig;
  1443. + read_lock(&trigger->leddev_list_lock);
  1444. + list_for_each(entry, &trigger->led_cdevs) {
  1445. + struct led_classdev *led_cdev;
  1446. +
  1447. + led_cdev = list_entry(entry, struct led_classdev, trig_list);
  1448. + swconfig_trig_led_event(sw_trig, led_cdev);
  1449. + }
  1450. + read_unlock(&trigger->leddev_list_lock);
  1451. +}
  1452. +
  1453. +static void
  1454. +swconfig_led_work_func(struct work_struct *work)
  1455. +{
  1456. + struct switch_led_trigger *sw_trig;
  1457. + struct switch_dev *swdev;
  1458. + u32 port_mask;
  1459. + u32 link;
  1460. + int i;
  1461. +
  1462. + sw_trig = container_of(work, struct switch_led_trigger,
  1463. + sw_led_work.work);
  1464. +
  1465. + port_mask = sw_trig->port_mask;
  1466. + swdev = sw_trig->swdev;
  1467. +
  1468. + link = 0;
  1469. + for (i = 0; i < SWCONFIG_LED_NUM_PORTS; i++) {
  1470. + u32 port_bit;
  1471. +
  1472. + port_bit = BIT(i);
  1473. + if ((port_mask & port_bit) == 0)
  1474. + continue;
  1475. +
  1476. + if (swdev->ops->get_port_link) {
  1477. + struct switch_port_link port_link;
  1478. +
  1479. + memset(&port_link, '\0', sizeof(port_link));
  1480. + swdev->ops->get_port_link(swdev, i, &port_link);
  1481. +
  1482. + if (port_link.link)
  1483. + link |= port_bit;
  1484. + }
  1485. +
  1486. + if (swdev->ops->get_port_stats) {
  1487. + struct switch_port_stats port_stats;
  1488. +
  1489. + memset(&port_stats, '\0', sizeof(port_stats));
  1490. + swdev->ops->get_port_stats(swdev, i, &port_stats);
  1491. + sw_trig->port_traffic[i] = port_stats.tx_bytes +
  1492. + port_stats.rx_bytes;
  1493. + }
  1494. + }
  1495. +
  1496. + sw_trig->port_link = link;
  1497. +
  1498. + swconfig_trig_update_leds(sw_trig);
  1499. +
  1500. + schedule_delayed_work(&sw_trig->sw_led_work,
  1501. + SWCONFIG_LED_TIMER_INTERVAL);
  1502. +}
  1503. +
  1504. +static int
  1505. +swconfig_create_led_trigger(struct switch_dev *swdev)
  1506. +{
  1507. + struct switch_led_trigger *sw_trig;
  1508. + int err;
  1509. +
  1510. + if (!swdev->ops->get_port_link)
  1511. + return 0;
  1512. +
  1513. + sw_trig = kzalloc(sizeof(struct switch_led_trigger), GFP_KERNEL);
  1514. + if (!sw_trig)
  1515. + return -ENOMEM;
  1516. +
  1517. + sw_trig->swdev = swdev;
  1518. + sw_trig->trig.name = swdev->devname;
  1519. + sw_trig->trig.activate = swconfig_trig_activate;
  1520. + sw_trig->trig.deactivate = swconfig_trig_deactivate;
  1521. +
  1522. + INIT_DELAYED_WORK(&sw_trig->sw_led_work, swconfig_led_work_func);
  1523. +
  1524. + err = led_trigger_register(&sw_trig->trig);
  1525. + if (err)
  1526. + goto err_free;
  1527. +
  1528. + swdev->led_trigger = sw_trig;
  1529. +
  1530. + return 0;
  1531. +
  1532. +err_free:
  1533. + kfree(sw_trig);
  1534. + return err;
  1535. +}
  1536. +
  1537. +static void
  1538. +swconfig_destroy_led_trigger(struct switch_dev *swdev)
  1539. +{
  1540. + struct switch_led_trigger *sw_trig;
  1541. +
  1542. + sw_trig = swdev->led_trigger;
  1543. + if (sw_trig) {
  1544. + cancel_delayed_work_sync(&sw_trig->sw_led_work);
  1545. + led_trigger_unregister(&sw_trig->trig);
  1546. + kfree(sw_trig);
  1547. + }
  1548. +}
  1549. +
  1550. +#else /* SWCONFIG_LEDS */
  1551. +static inline int
  1552. +swconfig_create_led_trigger(struct switch_dev *swdev) { return 0; }
  1553. +
  1554. +static inline void
  1555. +swconfig_destroy_led_trigger(struct switch_dev *swdev) { }
  1556. +#endif /* CONFIG_SWCONFIG_LEDS */
  1557. diff --git a/include/linux/switch.h b/include/linux/switch.h
  1558. new file mode 100644
  1559. index 0000000..b53431e
  1560. --- /dev/null
  1561. +++ b/include/linux/switch.h
  1562. @@ -0,0 +1,167 @@
  1563. +/*
  1564. + * switch.h: Switch configuration API
  1565. + *
  1566. + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  1567. + *
  1568. + * This program is free software; you can redistribute it and/or
  1569. + * modify it under the terms of the GNU General Public License
  1570. + * as published by the Free Software Foundation; either version 2
  1571. + * of the License, or (at your option) any later version.
  1572. + *
  1573. + * This program is distributed in the hope that it will be useful,
  1574. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1575. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1576. + * GNU General Public License for more details.
  1577. + */
  1578. +#ifndef _LINUX_SWITCH_H
  1579. +#define _LINUX_SWITCH_H
  1580. +
  1581. +#include <net/genetlink.h>
  1582. +#include <uapi/linux/switch.h>
  1583. +
  1584. +struct switch_dev;
  1585. +struct switch_op;
  1586. +struct switch_val;
  1587. +struct switch_attr;
  1588. +struct switch_attrlist;
  1589. +struct switch_led_trigger;
  1590. +
  1591. +int register_switch(struct switch_dev *dev, struct net_device *netdev);
  1592. +void unregister_switch(struct switch_dev *dev);
  1593. +
  1594. +/**
  1595. + * struct switch_attrlist - attribute list
  1596. + *
  1597. + * @n_attr: number of attributes
  1598. + * @attr: pointer to the attributes array
  1599. + */
  1600. +struct switch_attrlist {
  1601. + int n_attr;
  1602. + const struct switch_attr *attr;
  1603. +};
  1604. +
  1605. +enum switch_port_speed {
  1606. + SWITCH_PORT_SPEED_UNKNOWN = 0,
  1607. + SWITCH_PORT_SPEED_10 = 10,
  1608. + SWITCH_PORT_SPEED_100 = 100,
  1609. + SWITCH_PORT_SPEED_1000 = 1000,
  1610. +};
  1611. +
  1612. +struct switch_port_link {
  1613. + bool link;
  1614. + bool duplex;
  1615. + bool aneg;
  1616. + bool tx_flow;
  1617. + bool rx_flow;
  1618. + enum switch_port_speed speed;
  1619. +};
  1620. +
  1621. +struct switch_port_stats {
  1622. + unsigned long tx_bytes;
  1623. + unsigned long rx_bytes;
  1624. +};
  1625. +
  1626. +/**
  1627. + * struct switch_dev_ops - switch driver operations
  1628. + *
  1629. + * @attr_global: global switch attribute list
  1630. + * @attr_port: port attribute list
  1631. + * @attr_vlan: vlan attribute list
  1632. + *
  1633. + * Callbacks:
  1634. + *
  1635. + * @get_vlan_ports: read the port list of a VLAN
  1636. + * @set_vlan_ports: set the port list of a VLAN
  1637. + *
  1638. + * @get_port_pvid: get the primary VLAN ID of a port
  1639. + * @set_port_pvid: set the primary VLAN ID of a port
  1640. + *
  1641. + * @apply_config: apply all changed settings to the switch
  1642. + * @reset_switch: resetting the switch
  1643. + */
  1644. +struct switch_dev_ops {
  1645. + struct switch_attrlist attr_global, attr_port, attr_vlan;
  1646. +
  1647. + int (*get_vlan_ports)(struct switch_dev *dev, struct switch_val *val);
  1648. + int (*set_vlan_ports)(struct switch_dev *dev, struct switch_val *val);
  1649. +
  1650. + int (*get_port_pvid)(struct switch_dev *dev, int port, int *val);
  1651. + int (*set_port_pvid)(struct switch_dev *dev, int port, int val);
  1652. +
  1653. + int (*apply_config)(struct switch_dev *dev);
  1654. + int (*reset_switch)(struct switch_dev *dev);
  1655. +
  1656. + int (*get_port_link)(struct switch_dev *dev, int port,
  1657. + struct switch_port_link *link);
  1658. + int (*get_port_stats)(struct switch_dev *dev, int port,
  1659. + struct switch_port_stats *stats);
  1660. +};
  1661. +
  1662. +struct switch_dev {
  1663. + struct device_node *of_node;
  1664. + const struct switch_dev_ops *ops;
  1665. + /* will be automatically filled */
  1666. + char devname[IFNAMSIZ];
  1667. +
  1668. + const char *name;
  1669. + /* NB: either alias or netdev must be set */
  1670. + const char *alias;
  1671. + struct net_device *netdev;
  1672. +
  1673. + int ports;
  1674. + int vlans;
  1675. + int cpu_port;
  1676. +
  1677. + /* the following fields are internal for swconfig */
  1678. + int id;
  1679. + struct list_head dev_list;
  1680. + unsigned long def_global, def_port, def_vlan;
  1681. +
  1682. + struct mutex sw_mutex;
  1683. + struct switch_port *portbuf;
  1684. + struct switch_portmap *portmap;
  1685. +
  1686. + char buf[128];
  1687. +
  1688. +#ifdef CONFIG_SWCONFIG_LEDS
  1689. + struct switch_led_trigger *led_trigger;
  1690. +#endif
  1691. +};
  1692. +
  1693. +struct switch_port {
  1694. + u32 id;
  1695. + u32 flags;
  1696. +};
  1697. +
  1698. +struct switch_portmap {
  1699. + u32 virt;
  1700. + const char *s;
  1701. +};
  1702. +
  1703. +struct switch_val {
  1704. + const struct switch_attr *attr;
  1705. + int port_vlan;
  1706. + int len;
  1707. + union {
  1708. + const char *s;
  1709. + u32 i;
  1710. + struct switch_port *ports;
  1711. + } value;
  1712. +};
  1713. +
  1714. +struct switch_attr {
  1715. + int disabled;
  1716. + int type;
  1717. + const char *name;
  1718. + const char *description;
  1719. +
  1720. + int (*set)(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val);
  1721. + int (*get)(struct switch_dev *dev, const struct switch_attr *attr, struct switch_val *val);
  1722. +
  1723. + /* for driver internal use */
  1724. + int id;
  1725. + int ofs;
  1726. + int max;
  1727. +};
  1728. +
  1729. +#endif /* _LINUX_SWITCH_H */
  1730. diff --git a/include/uapi/linux/Kbuild b/include/uapi/linux/Kbuild
  1731. index 3ce25b5..b9565df 100644
  1732. --- a/include/uapi/linux/Kbuild
  1733. +++ b/include/uapi/linux/Kbuild
  1734. @@ -365,6 +365,7 @@ header-y += stddef.h
  1735. header-y += string.h
  1736. header-y += suspend_ioctls.h
  1737. header-y += swab.h
  1738. +header-y += switch.h
  1739. header-y += synclink.h
  1740. header-y += sysctl.h
  1741. header-y += sysinfo.h
  1742. diff --git a/include/uapi/linux/switch.h b/include/uapi/linux/switch.h
  1743. new file mode 100644
  1744. index 0000000..a59b239
  1745. --- /dev/null
  1746. +++ b/include/uapi/linux/switch.h
  1747. @@ -0,0 +1,103 @@
  1748. +/*
  1749. + * switch.h: Switch configuration API
  1750. + *
  1751. + * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
  1752. + *
  1753. + * This program is free software; you can redistribute it and/or
  1754. + * modify it under the terms of the GNU General Public License
  1755. + * as published by the Free Software Foundation; either version 2
  1756. + * of the License, or (at your option) any later version.
  1757. + *
  1758. + * This program is distributed in the hope that it will be useful,
  1759. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1760. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1761. + * GNU General Public License for more details.
  1762. + */
  1763. +
  1764. +#ifndef _UAPI_LINUX_SWITCH_H
  1765. +#define _UAPI_LINUX_SWITCH_H
  1766. +
  1767. +#include <linux/types.h>
  1768. +#include <linux/netdevice.h>
  1769. +#include <linux/netlink.h>
  1770. +#include <linux/genetlink.h>
  1771. +#ifndef __KERNEL__
  1772. +#include <netlink/netlink.h>
  1773. +#include <netlink/genl/genl.h>
  1774. +#include <netlink/genl/ctrl.h>
  1775. +#endif
  1776. +
  1777. +/* main attributes */
  1778. +enum {
  1779. + SWITCH_ATTR_UNSPEC,
  1780. + /* global */
  1781. + SWITCH_ATTR_TYPE,
  1782. + /* device */
  1783. + SWITCH_ATTR_ID,
  1784. + SWITCH_ATTR_DEV_NAME,
  1785. + SWITCH_ATTR_ALIAS,
  1786. + SWITCH_ATTR_NAME,
  1787. + SWITCH_ATTR_VLANS,
  1788. + SWITCH_ATTR_PORTS,
  1789. + SWITCH_ATTR_PORTMAP,
  1790. + SWITCH_ATTR_CPU_PORT,
  1791. + /* attributes */
  1792. + SWITCH_ATTR_OP_ID,
  1793. + SWITCH_ATTR_OP_TYPE,
  1794. + SWITCH_ATTR_OP_NAME,
  1795. + SWITCH_ATTR_OP_PORT,
  1796. + SWITCH_ATTR_OP_VLAN,
  1797. + SWITCH_ATTR_OP_VALUE_INT,
  1798. + SWITCH_ATTR_OP_VALUE_STR,
  1799. + SWITCH_ATTR_OP_VALUE_PORTS,
  1800. + SWITCH_ATTR_OP_DESCRIPTION,
  1801. + /* port lists */
  1802. + SWITCH_ATTR_PORT,
  1803. + SWITCH_ATTR_MAX
  1804. +};
  1805. +
  1806. +enum {
  1807. + /* port map */
  1808. + SWITCH_PORTMAP_PORTS,
  1809. + SWITCH_PORTMAP_SEGMENT,
  1810. + SWITCH_PORTMAP_VIRT,
  1811. + SWITCH_PORTMAP_MAX
  1812. +};
  1813. +
  1814. +/* commands */
  1815. +enum {
  1816. + SWITCH_CMD_UNSPEC,
  1817. + SWITCH_CMD_GET_SWITCH,
  1818. + SWITCH_CMD_NEW_ATTR,
  1819. + SWITCH_CMD_LIST_GLOBAL,
  1820. + SWITCH_CMD_GET_GLOBAL,
  1821. + SWITCH_CMD_SET_GLOBAL,
  1822. + SWITCH_CMD_LIST_PORT,
  1823. + SWITCH_CMD_GET_PORT,
  1824. + SWITCH_CMD_SET_PORT,
  1825. + SWITCH_CMD_LIST_VLAN,
  1826. + SWITCH_CMD_GET_VLAN,
  1827. + SWITCH_CMD_SET_VLAN
  1828. +};
  1829. +
  1830. +/* data types */
  1831. +enum switch_val_type {
  1832. + SWITCH_TYPE_UNSPEC,
  1833. + SWITCH_TYPE_INT,
  1834. + SWITCH_TYPE_STRING,
  1835. + SWITCH_TYPE_PORTS,
  1836. + SWITCH_TYPE_NOVAL,
  1837. +};
  1838. +
  1839. +/* port nested attributes */
  1840. +enum {
  1841. + SWITCH_PORT_UNSPEC,
  1842. + SWITCH_PORT_ID,
  1843. + SWITCH_PORT_FLAG_TAGGED,
  1844. + SWITCH_PORT_ATTR_MAX
  1845. +};
  1846. +
  1847. +#define SWITCH_ATTR_DEFAULTS_OFFSET 0x1000
  1848. +
  1849. +
  1850. +#endif /* _UAPI_LINUX_SWITCH_H */
  1851. --
  1852. 1.8.5.3