Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
1da177e4 LT |
31 | */ |
32 | ||
1da177e4 | 33 | #include <linux/module.h> |
174cd4b1 | 34 | #include <linux/sched/signal.h> |
1da177e4 LT |
35 | |
36 | #include <linux/init.h> | |
1da177e4 LT |
37 | #include <linux/seq_file.h> |
38 | ||
7c0f6ba6 | 39 | #include <linux/uaccess.h> |
1da177e4 LT |
40 | |
41 | #include "ipoib.h" | |
42 | ||
43cb76d9 GKH |
43 | static ssize_t show_parent(struct device *d, struct device_attribute *attr, |
44 | char *buf) | |
1da177e4 | 45 | { |
43cb76d9 | 46 | struct net_device *dev = to_net_dev(d); |
c1048aff | 47 | struct ipoib_dev_priv *priv = ipoib_priv(dev); |
1da177e4 LT |
48 | |
49 | return sprintf(buf, "%s\n", priv->parent->name); | |
50 | } | |
43cb76d9 | 51 | static DEVICE_ATTR(parent, S_IRUGO, show_parent, NULL); |
1da177e4 | 52 | |
9f49a5b5 JG |
53 | /* |
54 | * NOTE: If this function fails then the priv->dev will remain valid, however | |
55 | * priv can have been freed and must not be touched by caller in the error | |
56 | * case. | |
57 | * | |
58 | * If (ndev->reg_state == NETREG_UNINITIALIZED) then it is up to the caller to | |
59 | * free the net_device (just as rtnl_newlink does) otherwise the net_device | |
60 | * will be freed when the rtnl is unlocked. | |
61 | */ | |
9baa0b03 OG |
62 | int __ipoib_vlan_add(struct ipoib_dev_priv *ppriv, struct ipoib_dev_priv *priv, |
63 | u16 pkey, int type) | |
1da177e4 | 64 | { |
9f49a5b5 | 65 | struct net_device *ndev = priv->dev; |
1da177e4 LT |
66 | int result; |
67 | ||
9f49a5b5 JG |
68 | ASSERT_RTNL(); |
69 | ||
2c153959 | 70 | priv->parent = ppriv->dev; |
1da177e4 | 71 | priv->pkey = pkey; |
eaeb3984 | 72 | priv->child_type = type; |
1da177e4 | 73 | |
9f49a5b5 JG |
74 | /* We do not need to touch priv if register_netdevice fails */ |
75 | ndev->priv_destructor = ipoib_intf_free; | |
76 | ||
77 | result = register_netdevice(ndev); | |
1da177e4 LT |
78 | if (result) { |
79 | ipoib_warn(priv, "failed to initialize; error %i", result); | |
9f49a5b5 JG |
80 | |
81 | /* | |
82 | * register_netdevice sometimes calls priv_destructor, | |
83 | * sometimes not. Make sure it was done. | |
84 | */ | |
85 | if (ndev->priv_destructor) | |
86 | ndev->priv_destructor(ndev); | |
eaeb3984 | 87 | return result; |
1da177e4 LT |
88 | } |
89 | ||
862096a8 OG |
90 | /* RTNL childs don't need proprietary sysfs entries */ |
91 | if (type == IPOIB_LEGACY_CHILD) { | |
9f49a5b5 | 92 | if (ipoib_cm_add_mode_attr(ndev)) |
862096a8 | 93 | goto sysfs_failed; |
9f49a5b5 | 94 | if (ipoib_add_pkey_attr(ndev)) |
862096a8 | 95 | goto sysfs_failed; |
9f49a5b5 | 96 | if (ipoib_add_umcast_attr(ndev)) |
862096a8 OG |
97 | goto sysfs_failed; |
98 | ||
9f49a5b5 | 99 | if (device_create_file(&ndev->dev, &dev_attr_parent)) |
862096a8 OG |
100 | goto sysfs_failed; |
101 | } | |
1da177e4 LT |
102 | |
103 | list_add_tail(&priv->list, &ppriv->child_intfs); | |
104 | ||
1da177e4 LT |
105 | return 0; |
106 | ||
107 | sysfs_failed: | |
cbbe1efa | 108 | unregister_netdevice(priv->dev); |
eaeb3984 | 109 | return -ENOMEM; |
9baa0b03 OG |
110 | } |
111 | ||
112 | int ipoib_vlan_add(struct net_device *pdev, unsigned short pkey) | |
113 | { | |
114 | struct ipoib_dev_priv *ppriv, *priv; | |
115 | char intf_name[IFNAMSIZ]; | |
9f49a5b5 | 116 | struct net_device *ndev; |
9baa0b03 OG |
117 | struct ipoib_dev_priv *tpriv; |
118 | int result; | |
119 | ||
120 | if (!capable(CAP_NET_ADMIN)) | |
121 | return -EPERM; | |
122 | ||
cd565b4b | 123 | ppriv = ipoib_priv(pdev); |
9baa0b03 | 124 | |
b1b63970 | 125 | snprintf(intf_name, sizeof(intf_name), "%s.%04x", |
9baa0b03 | 126 | ppriv->dev->name, pkey); |
cd565b4b | 127 | |
ee190ab7 | 128 | if (!rtnl_trylock()) |
4542d66b FD |
129 | return restart_syscall(); |
130 | ||
577e07ff JG |
131 | if (pdev->reg_state != NETREG_REGISTERED) { |
132 | rtnl_unlock(); | |
577e07ff JG |
133 | return -EPERM; |
134 | } | |
135 | ||
9c6f42e9 | 136 | if (!down_write_trylock(&ppriv->vlan_rwsem)) { |
69956d83 | 137 | rtnl_unlock(); |
9c6f42e9 | 138 | return restart_syscall(); |
69956d83 | 139 | } |
9baa0b03 | 140 | |
9baa0b03 OG |
141 | /* |
142 | * First ensure this isn't a duplicate. We check the parent device and | |
143 | * then all of the legacy child interfaces to make sure the Pkey | |
144 | * doesn't match. | |
145 | */ | |
146 | if (ppriv->pkey == pkey) { | |
147 | result = -ENOTUNIQ; | |
148 | goto out; | |
149 | } | |
150 | ||
151 | list_for_each_entry(tpriv, &ppriv->child_intfs, list) { | |
152 | if (tpriv->pkey == pkey && | |
153 | tpriv->child_type == IPOIB_LEGACY_CHILD) { | |
154 | result = -ENOTUNIQ; | |
155 | goto out; | |
156 | } | |
157 | } | |
158 | ||
9f49a5b5 JG |
159 | priv = ipoib_intf_alloc(ppriv->ca, ppriv->port, intf_name); |
160 | if (!priv) { | |
161 | result = -ENOMEM; | |
162 | goto out; | |
163 | } | |
164 | ndev = priv->dev; | |
165 | ||
9baa0b03 OG |
166 | result = __ipoib_vlan_add(ppriv, priv, pkey, IPOIB_LEGACY_CHILD); |
167 | ||
9f49a5b5 JG |
168 | if (result && ndev->reg_state == NETREG_UNINITIALIZED) |
169 | free_netdev(ndev); | |
170 | ||
9baa0b03 | 171 | out: |
f47944cc | 172 | up_write(&ppriv->vlan_rwsem); |
89a3987a FD |
173 | rtnl_unlock(); |
174 | ||
1da177e4 LT |
175 | return result; |
176 | } | |
177 | ||
ee190ab7 JG |
178 | struct ipoib_vlan_delete_work { |
179 | struct work_struct work; | |
180 | struct net_device *dev; | |
181 | }; | |
182 | ||
183 | /* | |
184 | * sysfs callbacks of a netdevice cannot obtain the rtnl lock as | |
185 | * unregister_netdev ultimately deletes the sysfs files while holding the rtnl | |
186 | * lock. This deadlocks the system. | |
187 | * | |
188 | * A callback can use rtnl_trylock to avoid the deadlock but it cannot call | |
189 | * unregister_netdev as that internally takes and releases the rtnl_lock. So | |
190 | * instead we find the netdev to unregister and then do the actual unregister | |
191 | * from the global work queue where we can obtain the rtnl_lock safely. | |
192 | */ | |
193 | static void ipoib_vlan_delete_task(struct work_struct *work) | |
194 | { | |
195 | struct ipoib_vlan_delete_work *pwork = | |
196 | container_of(work, struct ipoib_vlan_delete_work, work); | |
197 | struct net_device *dev = pwork->dev; | |
198 | ||
199 | rtnl_lock(); | |
200 | ||
201 | /* Unregistering tasks can race with another task or parent removal */ | |
202 | if (dev->reg_state == NETREG_REGISTERED) { | |
203 | struct ipoib_dev_priv *priv = ipoib_priv(dev); | |
204 | struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent); | |
205 | ||
206 | down_write(&ppriv->vlan_rwsem); | |
207 | list_del(&priv->list); | |
208 | up_write(&ppriv->vlan_rwsem); | |
209 | ||
210 | ipoib_dbg(ppriv, "delete child vlan %s\n", dev->name); | |
211 | unregister_netdevice(dev); | |
212 | } | |
213 | ||
214 | rtnl_unlock(); | |
215 | ||
216 | kfree(pwork); | |
217 | } | |
218 | ||
1da177e4 LT |
219 | int ipoib_vlan_delete(struct net_device *pdev, unsigned short pkey) |
220 | { | |
221 | struct ipoib_dev_priv *ppriv, *priv, *tpriv; | |
ee190ab7 | 222 | int rc; |
1da177e4 LT |
223 | |
224 | if (!capable(CAP_NET_ADMIN)) | |
225 | return -EPERM; | |
226 | ||
ee190ab7 | 227 | if (!rtnl_trylock()) |
69956d83 ES |
228 | return restart_syscall(); |
229 | ||
577e07ff JG |
230 | if (pdev->reg_state != NETREG_REGISTERED) { |
231 | rtnl_unlock(); | |
577e07ff JG |
232 | return -EPERM; |
233 | } | |
234 | ||
ee190ab7 | 235 | ppriv = ipoib_priv(pdev); |
9c6f42e9 | 236 | |
ee190ab7 | 237 | rc = -ENODEV; |
1da177e4 | 238 | list_for_each_entry_safe(priv, tpriv, &ppriv->child_intfs, list) { |
9baa0b03 OG |
239 | if (priv->pkey == pkey && |
240 | priv->child_type == IPOIB_LEGACY_CHILD) { | |
ee190ab7 JG |
241 | struct ipoib_vlan_delete_work *work; |
242 | ||
243 | work = kmalloc(sizeof(*work), GFP_KERNEL); | |
244 | if (!work) { | |
245 | rc = -ENOMEM; | |
246 | goto out; | |
247 | } | |
248 | ||
249 | down_write(&ppriv->vlan_rwsem); | |
250 | list_del_init(&priv->list); | |
251 | up_write(&ppriv->vlan_rwsem); | |
252 | work->dev = priv->dev; | |
253 | INIT_WORK(&work->work, ipoib_vlan_delete_task); | |
254 | queue_work(ipoib_workqueue, &work->work); | |
255 | ||
256 | rc = 0; | |
1da177e4 LT |
257 | break; |
258 | } | |
259 | } | |
1c3098cd | 260 | |
ee190ab7 | 261 | out: |
cbbe1efa | 262 | rtnl_unlock(); |
1da177e4 | 263 | |
ee190ab7 | 264 | return rc; |
1da177e4 | 265 | } |