Linux 4.19.133
[linux/fpc-iii.git] / drivers / infiniband / ulp / ipoib / ipoib_vlan.c
blob341753fbda54dc1da2ec06bcc00eeb7895875046
1 /*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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.
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.
33 #include <linux/module.h>
34 #include <linux/sched/signal.h>
36 #include <linux/init.h>
37 #include <linux/seq_file.h>
39 #include <linux/uaccess.h>
41 #include "ipoib.h"
43 static ssize_t show_parent(struct device *d, struct device_attribute *attr,
44 char *buf)
46 struct net_device *dev = to_net_dev(d);
47 struct ipoib_dev_priv *priv = ipoib_priv(dev);
49 return sprintf(buf, "%s\n", priv->parent->name);
51 static DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL);
53 static bool is_child_unique(struct ipoib_dev_priv *ppriv,
54 struct ipoib_dev_priv *priv)
56 struct ipoib_dev_priv *tpriv;
58 ASSERT_RTNL();
61 * Since the legacy sysfs interface uses pkey for deletion it cannot
62 * support more than one interface with the same pkey, it creates
63 * ambiguity. The RTNL interface deletes using the netdev so it does
64 * not have a problem to support duplicated pkeys.
66 if (priv->child_type != IPOIB_LEGACY_CHILD)
67 return true;
70 * First ensure this isn't a duplicate. We check the parent device and
71 * then all of the legacy child interfaces to make sure the Pkey
72 * doesn't match.
74 if (ppriv->pkey == priv->pkey)
75 return false;
77 list_for_each_entry(tpriv, &ppriv->child_intfs, list) {
78 if (tpriv->pkey == priv->pkey &&
79 tpriv->child_type == IPOIB_LEGACY_CHILD)
80 return false;
83 return true;
87 * NOTE: If this function fails then the priv->dev will remain valid, however
88 * priv can have been freed and must not be touched by caller in the error
89 * case.
91 * If (ndev->reg_state == NETREG_UNINITIALIZED) then it is up to the caller to
92 * free the net_device (just as rtnl_newlink does) otherwise the net_device
93 * will be freed when the rtnl is unlocked.
95 int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv,
96 u16 pkey, int type)
98 struct net_device *ndev = priv->dev;
99 int result;
101 ASSERT_RTNL();
104 * Racing with unregister of the parent must be prevented by the
105 * caller.
107 WARN_ON(ppriv->dev->reg_state != NETREG_REGISTERED);
109 if (pkey == 0 || pkey == 0x8000) {
110 result = -EINVAL;
111 goto out_early;
114 priv->parent = ppriv->dev;
115 priv->pkey = pkey;
116 priv->child_type = type;
118 if (!is_child_unique(ppriv, priv)) {
119 result = -ENOTUNIQ;
120 goto out_early;
123 /* We do not need to touch priv if register_netdevice fails */
124 ndev->priv_destructor = ipoib_intf_free;
126 result = register_netdevice(ndev);
127 if (result) {
128 ipoib_warn(priv, "failed to initialize; error %i", result);
131 * register_netdevice sometimes calls priv_destructor,
132 * sometimes not. Make sure it was done.
134 goto out_early;
137 /* RTNL childs don't need proprietary sysfs entries */
138 if (type == IPOIB_LEGACY_CHILD) {
139 if (ipoib_cm_add_mode_attr(ndev))
140 goto sysfs_failed;
141 if (ipoib_add_pkey_attr(ndev))
142 goto sysfs_failed;
143 if (ipoib_add_umcast_attr(ndev))
144 goto sysfs_failed;
146 if (device_create_file(&ndev->dev, &dev_attr_parent))
147 goto sysfs_failed;
150 return 0;
152 sysfs_failed:
153 unregister_netdevice(priv->dev);
154 return -ENOMEM;
156 out_early:
157 if (ndev->priv_destructor)
158 ndev->priv_destructor(ndev);
159 return result;
162 int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
164 struct ipoib_dev_priv *ppriv, *priv;
165 char intf_name[IFNAMSIZ];
166 struct net_device *ndev;
167 int result;
169 if (!capable(CAP_NET_ADMIN))
170 return -EPERM;
172 if (!rtnl_trylock())
173 return restart_syscall();
175 if (pdev->reg_state != NETREG_REGISTERED) {
176 rtnl_unlock();
177 return -EPERM;
180 ppriv = ipoib_priv(pdev);
182 snprintf(intf_name, sizeof(intf_name), "%s.%04x",
183 ppriv->dev->name, pkey);
185 priv = ipoib_intf_alloc(ppriv->ca, ppriv->port, intf_name);
186 if (!priv) {
187 result = -ENOMEM;
188 goto out;
190 ndev = priv->dev;
192 result = __ipoib_vlan_add(ppriv, priv, pkey, IPOIB_LEGACY_CHILD);
194 if (result && ndev->reg_state == NETREG_UNINITIALIZED)
195 free_netdev(ndev);
197 out:
198 rtnl_unlock();
200 return result;
203 struct ipoib_vlan_delete_work {
204 struct work_struct work;
205 struct net_device *dev;
209 * sysfs callbacks of a netdevice cannot obtain the rtnl lock as
210 * unregister_netdev ultimately deletes the sysfs files while holding the rtnl
211 * lock. This deadlocks the system.
213 * A callback can use rtnl_trylock to avoid the deadlock but it cannot call
214 * unregister_netdev as that internally takes and releases the rtnl_lock. So
215 * instead we find the netdev to unregister and then do the actual unregister
216 * from the global work queue where we can obtain the rtnl_lock safely.
218 static void ipoib_vlan_delete_task(struct work_struct *work)
220 struct ipoib_vlan_delete_work *pwork =
221 container_of(work, struct ipoib_vlan_delete_work, work);
222 struct net_device *dev = pwork->dev;
224 rtnl_lock();
226 /* Unregistering tasks can race with another task or parent removal */
227 if (dev->reg_state == NETREG_REGISTERED) {
228 struct ipoib_dev_priv *priv = ipoib_priv(dev);
229 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
231 ipoib_dbg(ppriv, "delete child vlan %s\n", dev->name);
232 unregister_netdevice(dev);
235 rtnl_unlock();
237 kfree(pwork);
240 int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
242 struct ipoib_dev_priv *ppriv, *priv, *tpriv;
243 int rc;
245 if (!capable(CAP_NET_ADMIN))
246 return -EPERM;
248 if (!rtnl_trylock())
249 return restart_syscall();
251 if (pdev->reg_state != NETREG_REGISTERED) {
252 rtnl_unlock();
253 return -EPERM;
256 ppriv = ipoib_priv(pdev);
258 rc = -ENODEV;
259 list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
260 if (priv->pkey == pkey &&
261 priv->child_type == IPOIB_LEGACY_CHILD) {
262 struct ipoib_vlan_delete_work *work;
264 work = kmalloc(sizeof(*work), GFP_KERNEL);
265 if (!work) {
266 rc = -ENOMEM;
267 goto out;
270 down_write(&ppriv->vlan_rwsem);
271 list_del_init(&priv->list);
272 up_write(&ppriv->vlan_rwsem);
273 work->dev = priv->dev;
274 INIT_WORK(&work->work, ipoib_vlan_delete_task);
275 queue_work(ipoib_workqueue, &work->work);
277 rc = 0;
278 break;
282 out:
283 rtnl_unlock();
285 return rc;