swconfig.c 26 KB

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