ipoib_vlan.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. * $Id: ipoib_vlan.c 1349 2004-12-16 21:09:43Z roland $
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/seq_file.h>
  38. #include <asm/uaccess.h>
  39. #include "ipoib.h"
  40. static ssize_t show_parent(struct class_device *class_dev, char *buf)
  41. {
  42. struct net_device *dev =
  43. container_of(class_dev, struct net_device, class_dev);
  44. struct ipoib_dev_priv *priv = netdev_priv(dev);
  45. return sprintf(buf, "%s\n", priv->parent->name);
  46. }
  47. static CLASS_DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL);
  48. int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
  49. {
  50. struct ipoib_dev_priv *ppriv, *priv;
  51. char intf_name[IFNAMSIZ];
  52. int result;
  53. if (!capable(CAP_NET_ADMIN))
  54. return -EPERM;
  55. ppriv = netdev_priv(pdev);
  56. down(&ppriv->vlan_mutex);
  57. /*
  58. * First ensure this isn't a duplicate. We check the parent device and
  59. * then all of the child interfaces to make sure the Pkey doesn't match.
  60. */
  61. if (ppriv->pkey == pkey) {
  62. result = -ENOTUNIQ;
  63. goto err;
  64. }
  65. list_for_each_entry(priv, &ppriv->child_intfs, list) {
  66. if (priv->pkey == pkey) {
  67. result = -ENOTUNIQ;
  68. goto err;
  69. }
  70. }
  71. snprintf(intf_name, sizeof intf_name, "%s.%04x",
  72. ppriv->dev->name, pkey);
  73. priv = ipoib_intf_alloc(intf_name);
  74. if (!priv) {
  75. result = -ENOMEM;
  76. goto err;
  77. }
  78. set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
  79. priv->pkey = pkey;
  80. memcpy(priv->dev->dev_addr, ppriv->dev->dev_addr, INFINIBAND_ALEN);
  81. priv->dev->broadcast[8] = pkey >> 8;
  82. priv->dev->broadcast[9] = pkey & 0xff;
  83. result = ipoib_dev_init(priv->dev, ppriv->ca, ppriv->port);
  84. if (result < 0) {
  85. ipoib_warn(ppriv, "failed to initialize subinterface: "
  86. "device %s, port %d",
  87. ppriv->ca->name, ppriv->port);
  88. goto device_init_failed;
  89. }
  90. result = register_netdev(priv->dev);
  91. if (result) {
  92. ipoib_warn(priv, "failed to initialize; error %i", result);
  93. goto register_failed;
  94. }
  95. priv->parent = ppriv->dev;
  96. if (ipoib_create_debug_file(priv->dev))
  97. goto debug_failed;
  98. if (ipoib_add_pkey_attr(priv->dev))
  99. goto sysfs_failed;
  100. if (class_device_create_file(&priv->dev->class_dev,
  101. &class_device_attr_parent))
  102. goto sysfs_failed;
  103. list_add_tail(&priv->list, &ppriv->child_intfs);
  104. up(&ppriv->vlan_mutex);
  105. return 0;
  106. sysfs_failed:
  107. ipoib_delete_debug_file(priv->dev);
  108. debug_failed:
  109. unregister_netdev(priv->dev);
  110. register_failed:
  111. ipoib_dev_cleanup(priv->dev);
  112. device_init_failed:
  113. free_netdev(priv->dev);
  114. err:
  115. up(&ppriv->vlan_mutex);
  116. return result;
  117. }
  118. int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
  119. {
  120. struct ipoib_dev_priv *ppriv, *priv, *tpriv;
  121. int ret = -ENOENT;
  122. if (!capable(CAP_NET_ADMIN))
  123. return -EPERM;
  124. ppriv = netdev_priv(pdev);
  125. down(&ppriv->vlan_mutex);
  126. list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
  127. if (priv->pkey == pkey) {
  128. unregister_netdev(priv->dev);
  129. ipoib_dev_cleanup(priv->dev);
  130. list_del(&priv->list);
  131. kfree(priv);
  132. ret = 0;
  133. break;
  134. }
  135. }
  136. up(&ppriv->vlan_mutex);
  137. return ret;
  138. }