1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) 2004 Topspin Communications. All rights reserved.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * This software is available to you under a choice of one of two
5*4882a593Smuzhiyun * licenses. You may choose to be licensed under the terms of the GNU
6*4882a593Smuzhiyun * General Public License (GPL) Version 2, available from the file
7*4882a593Smuzhiyun * COPYING in the main directory of this source tree, or the
8*4882a593Smuzhiyun * OpenIB.org BSD license below:
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Redistribution and use in source and binary forms, with or
11*4882a593Smuzhiyun * without modification, are permitted provided that the following
12*4882a593Smuzhiyun * conditions are met:
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * - Redistributions of source code must retain the above
15*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
16*4882a593Smuzhiyun * disclaimer.
17*4882a593Smuzhiyun *
18*4882a593Smuzhiyun * - Redistributions in binary form must reproduce the above
19*4882a593Smuzhiyun * copyright notice, this list of conditions and the following
20*4882a593Smuzhiyun * disclaimer in the documentation and/or other materials
21*4882a593Smuzhiyun * provided with the distribution.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24*4882a593Smuzhiyun * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25*4882a593Smuzhiyun * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26*4882a593Smuzhiyun * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27*4882a593Smuzhiyun * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28*4882a593Smuzhiyun * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29*4882a593Smuzhiyun * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30*4882a593Smuzhiyun * SOFTWARE.
31*4882a593Smuzhiyun */
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #include <linux/module.h>
34*4882a593Smuzhiyun #include <linux/sched/signal.h>
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #include <linux/init.h>
37*4882a593Smuzhiyun #include <linux/seq_file.h>
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #include <linux/uaccess.h>
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun #include "ipoib.h"
42*4882a593Smuzhiyun
show_parent(struct device * d,struct device_attribute * attr,char * buf)43*4882a593Smuzhiyun static ssize_t show_parent(struct device *d, struct device_attribute *attr,
44*4882a593Smuzhiyun char *buf)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun struct net_device *dev = to_net_dev(d);
47*4882a593Smuzhiyun struct ipoib_dev_priv *priv = ipoib_priv(dev);
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun return sprintf(buf, "%s\n", priv->parent->name);
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun static DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL);
52*4882a593Smuzhiyun
is_child_unique(struct ipoib_dev_priv * ppriv,struct ipoib_dev_priv * priv)53*4882a593Smuzhiyun static bool is_child_unique(struct ipoib_dev_priv *ppriv,
54*4882a593Smuzhiyun struct ipoib_dev_priv *priv)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun struct ipoib_dev_priv *tpriv;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun ASSERT_RTNL();
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun * Since the legacy sysfs interface uses pkey for deletion it cannot
62*4882a593Smuzhiyun * support more than one interface with the same pkey, it creates
63*4882a593Smuzhiyun * ambiguity. The RTNL interface deletes using the netdev so it does
64*4882a593Smuzhiyun * not have a problem to support duplicated pkeys.
65*4882a593Smuzhiyun */
66*4882a593Smuzhiyun if (priv->child_type != IPOIB_LEGACY_CHILD)
67*4882a593Smuzhiyun return true;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun * First ensure this isn't a duplicate. We check the parent device and
71*4882a593Smuzhiyun * then all of the legacy child interfaces to make sure the Pkey
72*4882a593Smuzhiyun * doesn't match.
73*4882a593Smuzhiyun */
74*4882a593Smuzhiyun if (ppriv->pkey == priv->pkey)
75*4882a593Smuzhiyun return false;
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun list_for_each_entry(tpriv, &ppriv->child_intfs, list) {
78*4882a593Smuzhiyun if (tpriv->pkey == priv->pkey &&
79*4882a593Smuzhiyun tpriv->child_type == IPOIB_LEGACY_CHILD)
80*4882a593Smuzhiyun return false;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun return true;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /*
87*4882a593Smuzhiyun * NOTE: If this function fails then the priv->dev will remain valid, however
88*4882a593Smuzhiyun * priv will have been freed and must not be touched by caller in the error
89*4882a593Smuzhiyun * case.
90*4882a593Smuzhiyun *
91*4882a593Smuzhiyun * If (ndev->reg_state == NETREG_UNINITIALIZED) then it is up to the caller to
92*4882a593Smuzhiyun * free the net_device (just as rtnl_newlink does) otherwise the net_device
93*4882a593Smuzhiyun * will be freed when the rtnl is unlocked.
94*4882a593Smuzhiyun */
__ipoib_vlan_add(struct ipoib_dev_priv * ppriv,struct ipoib_dev_priv * priv,u16 pkey,int type)95*4882a593Smuzhiyun int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv,
96*4882a593Smuzhiyun u16 pkey, int type)
97*4882a593Smuzhiyun {
98*4882a593Smuzhiyun struct net_device *ndev = priv->dev;
99*4882a593Smuzhiyun int result;
100*4882a593Smuzhiyun struct rdma_netdev *rn = netdev_priv(ndev);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun ASSERT_RTNL();
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /*
105*4882a593Smuzhiyun * We do not need to touch priv if register_netdevice fails, so just
106*4882a593Smuzhiyun * always use this flow.
107*4882a593Smuzhiyun */
108*4882a593Smuzhiyun ndev->priv_destructor = ipoib_intf_free;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun /*
111*4882a593Smuzhiyun * Racing with unregister of the parent must be prevented by the
112*4882a593Smuzhiyun * caller.
113*4882a593Smuzhiyun */
114*4882a593Smuzhiyun WARN_ON(ppriv->dev->reg_state != NETREG_REGISTERED);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun if (pkey == 0 || pkey == 0x8000) {
117*4882a593Smuzhiyun result = -EINVAL;
118*4882a593Smuzhiyun goto out_early;
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun rn->mtu = priv->mcast_mtu;
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun priv->parent = ppriv->dev;
124*4882a593Smuzhiyun priv->pkey = pkey;
125*4882a593Smuzhiyun priv->child_type = type;
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun if (!is_child_unique(ppriv, priv)) {
128*4882a593Smuzhiyun result = -ENOTUNIQ;
129*4882a593Smuzhiyun goto out_early;
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun result = register_netdevice(ndev);
133*4882a593Smuzhiyun if (result) {
134*4882a593Smuzhiyun ipoib_warn(priv, "failed to initialize; error %i", result);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun * register_netdevice sometimes calls priv_destructor,
138*4882a593Smuzhiyun * sometimes not. Make sure it was done.
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun goto out_early;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* RTNL childs don't need proprietary sysfs entries */
144*4882a593Smuzhiyun if (type == IPOIB_LEGACY_CHILD) {
145*4882a593Smuzhiyun if (ipoib_cm_add_mode_attr(ndev))
146*4882a593Smuzhiyun goto sysfs_failed;
147*4882a593Smuzhiyun if (ipoib_add_pkey_attr(ndev))
148*4882a593Smuzhiyun goto sysfs_failed;
149*4882a593Smuzhiyun if (ipoib_add_umcast_attr(ndev))
150*4882a593Smuzhiyun goto sysfs_failed;
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun if (device_create_file(&ndev->dev, &dev_attr_parent))
153*4882a593Smuzhiyun goto sysfs_failed;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun return 0;
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun sysfs_failed:
159*4882a593Smuzhiyun unregister_netdevice(priv->dev);
160*4882a593Smuzhiyun return -ENOMEM;
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun out_early:
163*4882a593Smuzhiyun if (ndev->priv_destructor)
164*4882a593Smuzhiyun ndev->priv_destructor(ndev);
165*4882a593Smuzhiyun return result;
166*4882a593Smuzhiyun }
167*4882a593Smuzhiyun
ipoib_vlan_add(struct net_device * pdev,unsigned short pkey)168*4882a593Smuzhiyun int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey)
169*4882a593Smuzhiyun {
170*4882a593Smuzhiyun struct ipoib_dev_priv *ppriv, *priv;
171*4882a593Smuzhiyun char intf_name[IFNAMSIZ];
172*4882a593Smuzhiyun struct net_device *ndev;
173*4882a593Smuzhiyun int result;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun if (!capable(CAP_NET_ADMIN))
176*4882a593Smuzhiyun return -EPERM;
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun if (!rtnl_trylock())
179*4882a593Smuzhiyun return restart_syscall();
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun if (pdev->reg_state != NETREG_REGISTERED) {
182*4882a593Smuzhiyun rtnl_unlock();
183*4882a593Smuzhiyun return -EPERM;
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun ppriv = ipoib_priv(pdev);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun snprintf(intf_name, sizeof(intf_name), "%s.%04x",
189*4882a593Smuzhiyun ppriv->dev->name, pkey);
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun ndev = ipoib_intf_alloc(ppriv->ca, ppriv->port, intf_name);
192*4882a593Smuzhiyun if (IS_ERR(ndev)) {
193*4882a593Smuzhiyun result = PTR_ERR(ndev);
194*4882a593Smuzhiyun goto out;
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun priv = ipoib_priv(ndev);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun ndev->rtnl_link_ops = ipoib_get_link_ops();
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun result = __ipoib_vlan_add(ppriv, priv, pkey, IPOIB_LEGACY_CHILD);
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun if (result && ndev->reg_state == NETREG_UNINITIALIZED)
203*4882a593Smuzhiyun free_netdev(ndev);
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun out:
206*4882a593Smuzhiyun rtnl_unlock();
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun return result;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun struct ipoib_vlan_delete_work {
212*4882a593Smuzhiyun struct work_struct work;
213*4882a593Smuzhiyun struct net_device *dev;
214*4882a593Smuzhiyun };
215*4882a593Smuzhiyun
216*4882a593Smuzhiyun /*
217*4882a593Smuzhiyun * sysfs callbacks of a netdevice cannot obtain the rtnl lock as
218*4882a593Smuzhiyun * unregister_netdev ultimately deletes the sysfs files while holding the rtnl
219*4882a593Smuzhiyun * lock. This deadlocks the system.
220*4882a593Smuzhiyun *
221*4882a593Smuzhiyun * A callback can use rtnl_trylock to avoid the deadlock but it cannot call
222*4882a593Smuzhiyun * unregister_netdev as that internally takes and releases the rtnl_lock. So
223*4882a593Smuzhiyun * instead we find the netdev to unregister and then do the actual unregister
224*4882a593Smuzhiyun * from the global work queue where we can obtain the rtnl_lock safely.
225*4882a593Smuzhiyun */
ipoib_vlan_delete_task(struct work_struct * work)226*4882a593Smuzhiyun static void ipoib_vlan_delete_task(struct work_struct *work)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun struct ipoib_vlan_delete_work *pwork =
229*4882a593Smuzhiyun container_of(work, struct ipoib_vlan_delete_work, work);
230*4882a593Smuzhiyun struct net_device *dev = pwork->dev;
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun rtnl_lock();
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun /* Unregistering tasks can race with another task or parent removal */
235*4882a593Smuzhiyun if (dev->reg_state == NETREG_REGISTERED) {
236*4882a593Smuzhiyun struct ipoib_dev_priv *priv = ipoib_priv(dev);
237*4882a593Smuzhiyun struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun ipoib_dbg(ppriv, "delete child vlan %s\n", dev->name);
240*4882a593Smuzhiyun unregister_netdevice(dev);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun rtnl_unlock();
244*4882a593Smuzhiyun
245*4882a593Smuzhiyun kfree(pwork);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
ipoib_vlan_delete(struct net_device * pdev,unsigned short pkey)248*4882a593Smuzhiyun int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey)
249*4882a593Smuzhiyun {
250*4882a593Smuzhiyun struct ipoib_dev_priv *ppriv, *priv, *tpriv;
251*4882a593Smuzhiyun int rc;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun if (!capable(CAP_NET_ADMIN))
254*4882a593Smuzhiyun return -EPERM;
255*4882a593Smuzhiyun
256*4882a593Smuzhiyun if (!rtnl_trylock())
257*4882a593Smuzhiyun return restart_syscall();
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun if (pdev->reg_state != NETREG_REGISTERED) {
260*4882a593Smuzhiyun rtnl_unlock();
261*4882a593Smuzhiyun return -EPERM;
262*4882a593Smuzhiyun }
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun ppriv = ipoib_priv(pdev);
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun rc = -ENODEV;
267*4882a593Smuzhiyun list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) {
268*4882a593Smuzhiyun if (priv->pkey == pkey &&
269*4882a593Smuzhiyun priv->child_type == IPOIB_LEGACY_CHILD) {
270*4882a593Smuzhiyun struct ipoib_vlan_delete_work *work;
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun work = kmalloc(sizeof(*work), GFP_KERNEL);
273*4882a593Smuzhiyun if (!work) {
274*4882a593Smuzhiyun rc = -ENOMEM;
275*4882a593Smuzhiyun goto out;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun down_write(&ppriv->vlan_rwsem);
279*4882a593Smuzhiyun list_del_init(&priv->list);
280*4882a593Smuzhiyun up_write(&ppriv->vlan_rwsem);
281*4882a593Smuzhiyun work->dev = priv->dev;
282*4882a593Smuzhiyun INIT_WORK(&work->work, ipoib_vlan_delete_task);
283*4882a593Smuzhiyun queue_work(ipoib_workqueue, &work->work);
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun rc = 0;
286*4882a593Smuzhiyun break;
287*4882a593Smuzhiyun }
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun out:
291*4882a593Smuzhiyun rtnl_unlock();
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun return rc;
294*4882a593Smuzhiyun }
295