conduit.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Handling of a conduit device, switching frames via its switch fabric CPU port
  4. *
  5. * Copyright (c) 2017 Savoir-faire Linux Inc.
  6. * Vivien Didelot <vivien.didelot@savoirfairelinux.com>
  7. */
  8. #include <linux/ethtool.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/netlink.h>
  11. #include <net/dsa.h>
  12. #include "conduit.h"
  13. #include "dsa.h"
  14. #include "port.h"
  15. #include "tag.h"
  16. static int dsa_conduit_get_regs_len(struct net_device *dev)
  17. {
  18. struct dsa_port *cpu_dp = dev->dsa_ptr;
  19. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  20. struct dsa_switch *ds = cpu_dp->ds;
  21. int port = cpu_dp->index;
  22. int ret = 0;
  23. int len;
  24. if (ops->get_regs_len) {
  25. len = ops->get_regs_len(dev);
  26. if (len < 0)
  27. return len;
  28. ret += len;
  29. }
  30. ret += sizeof(struct ethtool_drvinfo);
  31. ret += sizeof(struct ethtool_regs);
  32. if (ds->ops->get_regs_len) {
  33. len = ds->ops->get_regs_len(ds, port);
  34. if (len < 0)
  35. return len;
  36. ret += len;
  37. }
  38. return ret;
  39. }
  40. static void dsa_conduit_get_regs(struct net_device *dev,
  41. struct ethtool_regs *regs, void *data)
  42. {
  43. struct dsa_port *cpu_dp = dev->dsa_ptr;
  44. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  45. struct dsa_switch *ds = cpu_dp->ds;
  46. struct ethtool_drvinfo *cpu_info;
  47. struct ethtool_regs *cpu_regs;
  48. int port = cpu_dp->index;
  49. int len;
  50. if (ops->get_regs_len && ops->get_regs) {
  51. len = ops->get_regs_len(dev);
  52. if (len < 0)
  53. return;
  54. regs->len = len;
  55. ops->get_regs(dev, regs, data);
  56. data += regs->len;
  57. }
  58. cpu_info = (struct ethtool_drvinfo *)data;
  59. strscpy(cpu_info->driver, "dsa", sizeof(cpu_info->driver));
  60. data += sizeof(*cpu_info);
  61. cpu_regs = (struct ethtool_regs *)data;
  62. data += sizeof(*cpu_regs);
  63. if (ds->ops->get_regs_len && ds->ops->get_regs) {
  64. len = ds->ops->get_regs_len(ds, port);
  65. if (len < 0)
  66. return;
  67. cpu_regs->len = len;
  68. ds->ops->get_regs(ds, port, cpu_regs, data);
  69. }
  70. }
  71. static void dsa_conduit_get_ethtool_stats(struct net_device *dev,
  72. struct ethtool_stats *stats,
  73. uint64_t *data)
  74. {
  75. struct dsa_port *cpu_dp = dev->dsa_ptr;
  76. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  77. struct dsa_switch *ds = cpu_dp->ds;
  78. int port = cpu_dp->index;
  79. int count = 0;
  80. if (ops->get_sset_count && ops->get_ethtool_stats) {
  81. count = ops->get_sset_count(dev, ETH_SS_STATS);
  82. ops->get_ethtool_stats(dev, stats, data);
  83. }
  84. if (ds->ops->get_ethtool_stats)
  85. ds->ops->get_ethtool_stats(ds, port, data + count);
  86. }
  87. static void dsa_conduit_get_ethtool_phy_stats(struct net_device *dev,
  88. struct ethtool_stats *stats,
  89. uint64_t *data)
  90. {
  91. struct dsa_port *cpu_dp = dev->dsa_ptr;
  92. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  93. struct dsa_switch *ds = cpu_dp->ds;
  94. int port = cpu_dp->index;
  95. int count = 0;
  96. if (dev->phydev && !ops->get_ethtool_phy_stats) {
  97. count = phy_ethtool_get_sset_count(dev->phydev);
  98. if (count >= 0)
  99. phy_ethtool_get_stats(dev->phydev, stats, data);
  100. } else if (ops->get_sset_count && ops->get_ethtool_phy_stats) {
  101. count = ops->get_sset_count(dev, ETH_SS_PHY_STATS);
  102. ops->get_ethtool_phy_stats(dev, stats, data);
  103. }
  104. if (count < 0)
  105. count = 0;
  106. if (ds->ops->get_ethtool_phy_stats)
  107. ds->ops->get_ethtool_phy_stats(ds, port, data + count);
  108. }
  109. static int dsa_conduit_get_sset_count(struct net_device *dev, int sset)
  110. {
  111. struct dsa_port *cpu_dp = dev->dsa_ptr;
  112. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  113. struct dsa_switch *ds = cpu_dp->ds;
  114. int count = 0;
  115. if (sset == ETH_SS_PHY_STATS && dev->phydev &&
  116. !ops->get_ethtool_phy_stats)
  117. count = phy_ethtool_get_sset_count(dev->phydev);
  118. else if (ops->get_sset_count)
  119. count = ops->get_sset_count(dev, sset);
  120. if (count < 0)
  121. count = 0;
  122. if (ds->ops->get_sset_count)
  123. count += ds->ops->get_sset_count(ds, cpu_dp->index, sset);
  124. return count;
  125. }
  126. static void dsa_conduit_get_strings(struct net_device *dev, uint32_t stringset,
  127. uint8_t *data)
  128. {
  129. struct dsa_port *cpu_dp = dev->dsa_ptr;
  130. const struct ethtool_ops *ops = cpu_dp->orig_ethtool_ops;
  131. struct dsa_switch *ds = cpu_dp->ds;
  132. int port = cpu_dp->index;
  133. int len = ETH_GSTRING_LEN;
  134. int mcount = 0, count, i;
  135. uint8_t pfx[4];
  136. uint8_t *ndata;
  137. snprintf(pfx, sizeof(pfx), "p%.2d", port);
  138. /* We do not want to be NULL-terminated, since this is a prefix */
  139. pfx[sizeof(pfx) - 1] = '_';
  140. if (stringset == ETH_SS_PHY_STATS && dev->phydev &&
  141. !ops->get_ethtool_phy_stats) {
  142. mcount = phy_ethtool_get_sset_count(dev->phydev);
  143. if (mcount < 0)
  144. mcount = 0;
  145. else
  146. phy_ethtool_get_strings(dev->phydev, data);
  147. } else if (ops->get_sset_count && ops->get_strings) {
  148. mcount = ops->get_sset_count(dev, stringset);
  149. if (mcount < 0)
  150. mcount = 0;
  151. ops->get_strings(dev, stringset, data);
  152. }
  153. if (ds->ops->get_strings) {
  154. ndata = data + mcount * len;
  155. /* This function copies ETH_GSTRINGS_LEN bytes, we will mangle
  156. * the output after to prepend our CPU port prefix we
  157. * constructed earlier
  158. */
  159. ds->ops->get_strings(ds, port, stringset, ndata);
  160. count = ds->ops->get_sset_count(ds, port, stringset);
  161. if (count < 0)
  162. return;
  163. for (i = 0; i < count; i++) {
  164. memmove(ndata + (i * len + sizeof(pfx)),
  165. ndata + i * len, len - sizeof(pfx));
  166. memcpy(ndata + i * len, pfx, sizeof(pfx));
  167. }
  168. }
  169. }
  170. /* Deny PTP operations on conduit if there is at least one switch in the tree
  171. * that is PTP capable.
  172. */
  173. int __dsa_conduit_hwtstamp_validate(struct net_device *dev,
  174. const struct kernel_hwtstamp_config *config,
  175. struct netlink_ext_ack *extack)
  176. {
  177. struct dsa_port *cpu_dp = dev->dsa_ptr;
  178. struct dsa_switch *ds = cpu_dp->ds;
  179. struct dsa_switch_tree *dst;
  180. struct dsa_port *dp;
  181. dst = ds->dst;
  182. list_for_each_entry(dp, &dst->ports, list) {
  183. if (dsa_port_supports_hwtstamp(dp)) {
  184. NL_SET_ERR_MSG(extack,
  185. "HW timestamping not allowed on DSA conduit when switch supports the operation");
  186. return -EBUSY;
  187. }
  188. }
  189. return 0;
  190. }
  191. static int dsa_conduit_ethtool_setup(struct net_device *dev)
  192. {
  193. struct dsa_port *cpu_dp = dev->dsa_ptr;
  194. struct dsa_switch *ds = cpu_dp->ds;
  195. struct ethtool_ops *ops;
  196. if (netif_is_lag_master(dev))
  197. return 0;
  198. ops = devm_kzalloc(ds->dev, sizeof(*ops), GFP_KERNEL);
  199. if (!ops)
  200. return -ENOMEM;
  201. cpu_dp->orig_ethtool_ops = dev->ethtool_ops;
  202. if (cpu_dp->orig_ethtool_ops)
  203. memcpy(ops, cpu_dp->orig_ethtool_ops, sizeof(*ops));
  204. ops->get_regs_len = dsa_conduit_get_regs_len;
  205. ops->get_regs = dsa_conduit_get_regs;
  206. ops->get_sset_count = dsa_conduit_get_sset_count;
  207. ops->get_ethtool_stats = dsa_conduit_get_ethtool_stats;
  208. ops->get_strings = dsa_conduit_get_strings;
  209. ops->get_ethtool_phy_stats = dsa_conduit_get_ethtool_phy_stats;
  210. dev->ethtool_ops = ops;
  211. return 0;
  212. }
  213. static void dsa_conduit_ethtool_teardown(struct net_device *dev)
  214. {
  215. struct dsa_port *cpu_dp = dev->dsa_ptr;
  216. if (netif_is_lag_master(dev))
  217. return;
  218. dev->ethtool_ops = cpu_dp->orig_ethtool_ops;
  219. cpu_dp->orig_ethtool_ops = NULL;
  220. }
  221. /* Keep the conduit always promiscuous if the tagging protocol requires that
  222. * (garbles MAC DA) or if it doesn't support unicast filtering, case in which
  223. * it would revert to promiscuous mode as soon as we call dev_uc_add() on it
  224. * anyway.
  225. */
  226. static void dsa_conduit_set_promiscuity(struct net_device *dev, int inc)
  227. {
  228. const struct dsa_device_ops *ops = dev->dsa_ptr->tag_ops;
  229. if ((dev->priv_flags & IFF_UNICAST_FLT) && !ops->promisc_on_conduit)
  230. return;
  231. ASSERT_RTNL();
  232. dev_set_promiscuity(dev, inc);
  233. }
  234. static ssize_t tagging_show(struct device *d, struct device_attribute *attr,
  235. char *buf)
  236. {
  237. struct net_device *dev = to_net_dev(d);
  238. struct dsa_port *cpu_dp = dev->dsa_ptr;
  239. return sysfs_emit(buf, "%s\n",
  240. dsa_tag_protocol_to_str(cpu_dp->tag_ops));
  241. }
  242. static ssize_t tagging_store(struct device *d, struct device_attribute *attr,
  243. const char *buf, size_t count)
  244. {
  245. const struct dsa_device_ops *new_tag_ops, *old_tag_ops;
  246. const char *end = strchrnul(buf, '\n'), *name;
  247. struct net_device *dev = to_net_dev(d);
  248. struct dsa_port *cpu_dp = dev->dsa_ptr;
  249. size_t len = end - buf;
  250. int err;
  251. /* Empty string passed */
  252. if (!len)
  253. return -ENOPROTOOPT;
  254. name = kstrndup(buf, len, GFP_KERNEL);
  255. if (!name)
  256. return -ENOMEM;
  257. old_tag_ops = cpu_dp->tag_ops;
  258. new_tag_ops = dsa_tag_driver_get_by_name(name);
  259. kfree(name);
  260. /* Bad tagger name? */
  261. if (IS_ERR(new_tag_ops))
  262. return PTR_ERR(new_tag_ops);
  263. if (new_tag_ops == old_tag_ops)
  264. /* Drop the temporarily held duplicate reference, since
  265. * the DSA switch tree uses this tagger.
  266. */
  267. goto out;
  268. err = dsa_tree_change_tag_proto(cpu_dp->ds->dst, new_tag_ops,
  269. old_tag_ops);
  270. if (err) {
  271. /* On failure the old tagger is restored, so we don't need the
  272. * driver for the new one.
  273. */
  274. dsa_tag_driver_put(new_tag_ops);
  275. return err;
  276. }
  277. /* On success we no longer need the module for the old tagging protocol
  278. */
  279. out:
  280. dsa_tag_driver_put(old_tag_ops);
  281. return count;
  282. }
  283. static DEVICE_ATTR_RW(tagging);
  284. static struct attribute *dsa_user_attrs[] = {
  285. &dev_attr_tagging.attr,
  286. NULL
  287. };
  288. static const struct attribute_group dsa_group = {
  289. .name = "dsa",
  290. .attrs = dsa_user_attrs,
  291. };
  292. static void dsa_conduit_reset_mtu(struct net_device *dev)
  293. {
  294. int err;
  295. err = dev_set_mtu(dev, ETH_DATA_LEN);
  296. if (err)
  297. netdev_dbg(dev,
  298. "Unable to reset MTU to exclude DSA overheads\n");
  299. }
  300. int dsa_conduit_setup(struct net_device *dev, struct dsa_port *cpu_dp)
  301. {
  302. const struct dsa_device_ops *tag_ops = cpu_dp->tag_ops;
  303. struct dsa_switch *ds = cpu_dp->ds;
  304. struct device_link *consumer_link;
  305. int mtu, ret;
  306. mtu = ETH_DATA_LEN + dsa_tag_protocol_overhead(tag_ops);
  307. /* The DSA conduit must use SET_NETDEV_DEV for this to work. */
  308. if (!netif_is_lag_master(dev)) {
  309. consumer_link = device_link_add(ds->dev, dev->dev.parent,
  310. DL_FLAG_AUTOREMOVE_CONSUMER);
  311. if (!consumer_link)
  312. netdev_err(dev,
  313. "Failed to create a device link to DSA switch %s\n",
  314. dev_name(ds->dev));
  315. }
  316. /* The switch driver may not implement ->port_change_mtu(), case in
  317. * which dsa_user_change_mtu() will not update the conduit MTU either,
  318. * so we need to do that here.
  319. */
  320. ret = dev_set_mtu(dev, mtu);
  321. if (ret)
  322. netdev_warn(dev, "error %d setting MTU to %d to include DSA overhead\n",
  323. ret, mtu);
  324. /* If we use a tagging format that doesn't have an ethertype
  325. * field, make sure that all packets from this point on get
  326. * sent to the tag format's receive function.
  327. */
  328. wmb();
  329. dev->dsa_ptr = cpu_dp;
  330. dsa_conduit_set_promiscuity(dev, 1);
  331. ret = dsa_conduit_ethtool_setup(dev);
  332. if (ret)
  333. goto out_err_reset_promisc;
  334. ret = sysfs_create_group(&dev->dev.kobj, &dsa_group);
  335. if (ret)
  336. goto out_err_ethtool_teardown;
  337. return ret;
  338. out_err_ethtool_teardown:
  339. dsa_conduit_ethtool_teardown(dev);
  340. out_err_reset_promisc:
  341. dsa_conduit_set_promiscuity(dev, -1);
  342. return ret;
  343. }
  344. void dsa_conduit_teardown(struct net_device *dev)
  345. {
  346. sysfs_remove_group(&dev->dev.kobj, &dsa_group);
  347. dsa_conduit_ethtool_teardown(dev);
  348. dsa_conduit_reset_mtu(dev);
  349. dsa_conduit_set_promiscuity(dev, -1);
  350. dev->dsa_ptr = NULL;
  351. /* If we used a tagging format that doesn't have an ethertype
  352. * field, make sure that all packets from this point get sent
  353. * without the tag and go through the regular receive path.
  354. */
  355. wmb();
  356. }
  357. int dsa_conduit_lag_setup(struct net_device *lag_dev, struct dsa_port *cpu_dp,
  358. struct netdev_lag_upper_info *uinfo,
  359. struct netlink_ext_ack *extack)
  360. {
  361. bool conduit_setup = false;
  362. int err;
  363. if (!netdev_uses_dsa(lag_dev)) {
  364. err = dsa_conduit_setup(lag_dev, cpu_dp);
  365. if (err)
  366. return err;
  367. conduit_setup = true;
  368. }
  369. err = dsa_port_lag_join(cpu_dp, lag_dev, uinfo, extack);
  370. if (err) {
  371. NL_SET_ERR_MSG_WEAK_MOD(extack, "CPU port failed to join LAG");
  372. goto out_conduit_teardown;
  373. }
  374. return 0;
  375. out_conduit_teardown:
  376. if (conduit_setup)
  377. dsa_conduit_teardown(lag_dev);
  378. return err;
  379. }
  380. /* Tear down a conduit if there isn't any other user port on it,
  381. * optionally also destroying LAG information.
  382. */
  383. void dsa_conduit_lag_teardown(struct net_device *lag_dev,
  384. struct dsa_port *cpu_dp)
  385. {
  386. struct net_device *upper;
  387. struct list_head *iter;
  388. dsa_port_lag_leave(cpu_dp, lag_dev);
  389. netdev_for_each_upper_dev_rcu(lag_dev, upper, iter)
  390. if (dsa_user_dev_check(upper))
  391. return;
  392. dsa_conduit_teardown(lag_dev);
  393. }