ARM: 7409/1: Do not call flush_cache_user_range with mmap_sem held
[linux/fpc-iii.git] / net / caif / caif_dev.c
blob5ba4366a220d0c1b4047d13e7bd00e3edab2dae3
1 /*
2 * CAIF Interface registration.
3 * Copyright (C) ST-Ericsson AB 2010
4 * Author: Sjur Brendeland/sjur.brandeland@stericsson.com
5 * License terms: GNU General Public License (GPL) version 2
7 * Borrowed heavily from file: pn_dev.c. Thanks to
8 * Remi Denis-Courmont <remi.denis-courmont@nokia.com>
9 * and Sakari Ailus <sakari.ailus@nokia.com>
12 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
14 #include <linux/version.h>
15 #include <linux/kernel.h>
16 #include <linux/if_arp.h>
17 #include <linux/net.h>
18 #include <linux/netdevice.h>
19 #include <linux/mutex.h>
20 #include <net/netns/generic.h>
21 #include <net/net_namespace.h>
22 #include <net/pkt_sched.h>
23 #include <net/caif/caif_device.h>
24 #include <net/caif/caif_layer.h>
25 #include <net/caif/cfpkt.h>
26 #include <net/caif/cfcnfg.h>
28 MODULE_LICENSE("GPL");
30 /* Used for local tracking of the CAIF net devices */
31 struct caif_device_entry {
32 struct cflayer layer;
33 struct list_head list;
34 struct net_device *netdev;
35 int __percpu *pcpu_refcnt;
38 struct caif_device_entry_list {
39 struct list_head list;
40 /* Protects simulanous deletes in list */
41 struct mutex lock;
44 struct caif_net {
45 struct cfcnfg *cfg;
46 struct caif_device_entry_list caifdevs;
49 static int caif_net_id;
51 struct cfcnfg *get_cfcnfg(struct net *net)
53 struct caif_net *caifn;
54 BUG_ON(!net);
55 caifn = net_generic(net, caif_net_id);
56 return caifn->cfg;
58 EXPORT_SYMBOL(get_cfcnfg);
60 static struct caif_device_entry_list *caif_device_list(struct net *net)
62 struct caif_net *caifn;
63 BUG_ON(!net);
64 caifn = net_generic(net, caif_net_id);
65 return &caifn->caifdevs;
68 static void caifd_put(struct caif_device_entry *e)
70 irqsafe_cpu_dec(*e->pcpu_refcnt);
73 static void caifd_hold(struct caif_device_entry *e)
75 irqsafe_cpu_inc(*e->pcpu_refcnt);
78 static int caifd_refcnt_read(struct caif_device_entry *e)
80 int i, refcnt = 0;
81 for_each_possible_cpu(i)
82 refcnt += *per_cpu_ptr(e->pcpu_refcnt, i);
83 return refcnt;
86 /* Allocate new CAIF device. */
87 static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
89 struct caif_device_entry_list *caifdevs;
90 struct caif_device_entry *caifd;
92 caifdevs = caif_device_list(dev_net(dev));
94 caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC);
95 if (!caifd)
96 return NULL;
97 caifd->pcpu_refcnt = alloc_percpu(int);
98 caifd->netdev = dev;
99 dev_hold(dev);
100 return caifd;
103 static struct caif_device_entry *caif_get(struct net_device *dev)
105 struct caif_device_entry_list *caifdevs =
106 caif_device_list(dev_net(dev));
107 struct caif_device_entry *caifd;
109 list_for_each_entry_rcu(caifd, &caifdevs->list, list) {
110 if (caifd->netdev == dev)
111 return caifd;
113 return NULL;
116 static int transmit(struct cflayer *layer, struct cfpkt *pkt)
118 int err;
119 struct caif_device_entry *caifd =
120 container_of(layer, struct caif_device_entry, layer);
121 struct sk_buff *skb;
123 skb = cfpkt_tonative(pkt);
124 skb->dev = caifd->netdev;
126 err = dev_queue_xmit(skb);
127 if (err > 0)
128 err = -EIO;
130 return err;
134 * Stuff received packets into the CAIF stack.
135 * On error, returns non-zero and releases the skb.
137 static int receive(struct sk_buff *skb, struct net_device *dev,
138 struct packet_type *pkttype, struct net_device *orig_dev)
140 struct cfpkt *pkt;
141 struct caif_device_entry *caifd;
142 int err;
144 pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
146 rcu_read_lock();
147 caifd = caif_get(dev);
149 if (!caifd || !caifd->layer.up || !caifd->layer.up->receive ||
150 !netif_oper_up(caifd->netdev)) {
151 rcu_read_unlock();
152 kfree_skb(skb);
153 return NET_RX_DROP;
156 /* Hold reference to netdevice while using CAIF stack */
157 caifd_hold(caifd);
158 rcu_read_unlock();
160 err = caifd->layer.up->receive(caifd->layer.up, pkt);
162 /* For -EILSEQ the packet is not freed so so it now */
163 if (err == -EILSEQ)
164 cfpkt_destroy(pkt);
166 /* Release reference to stack upwards */
167 caifd_put(caifd);
168 return 0;
171 static struct packet_type caif_packet_type __read_mostly = {
172 .type = cpu_to_be16(ETH_P_CAIF),
173 .func = receive,
176 static void dev_flowctrl(struct net_device *dev, int on)
178 struct caif_device_entry *caifd;
180 rcu_read_lock();
182 caifd = caif_get(dev);
183 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
184 rcu_read_unlock();
185 return;
188 caifd_hold(caifd);
189 rcu_read_unlock();
191 caifd->layer.up->ctrlcmd(caifd->layer.up,
192 on ?
193 _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
194 _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
195 caifd->layer.id);
196 caifd_put(caifd);
199 /* notify Caif of device events */
200 static int caif_device_notify(struct notifier_block *me, unsigned long what,
201 void *arg)
203 struct net_device *dev = arg;
204 struct caif_device_entry *caifd = NULL;
205 struct caif_dev_common *caifdev;
206 enum cfcnfg_phy_preference pref;
207 enum cfcnfg_phy_type phy_type;
208 struct cfcnfg *cfg;
209 struct caif_device_entry_list *caifdevs;
211 if (dev->type != ARPHRD_CAIF)
212 return 0;
214 cfg = get_cfcnfg(dev_net(dev));
215 if (cfg == NULL)
216 return 0;
218 caifdevs = caif_device_list(dev_net(dev));
220 switch (what) {
221 case NETDEV_REGISTER:
222 caifd = caif_device_alloc(dev);
223 if (!caifd)
224 return 0;
226 caifdev = netdev_priv(dev);
227 caifdev->flowctrl = dev_flowctrl;
229 caifd->layer.transmit = transmit;
231 if (caifdev->use_frag)
232 phy_type = CFPHYTYPE_FRAG;
233 else
234 phy_type = CFPHYTYPE_CAIF;
236 switch (caifdev->link_select) {
237 case CAIF_LINK_HIGH_BANDW:
238 pref = CFPHYPREF_HIGH_BW;
239 break;
240 case CAIF_LINK_LOW_LATENCY:
241 pref = CFPHYPREF_LOW_LAT;
242 break;
243 default:
244 pref = CFPHYPREF_HIGH_BW;
245 break;
247 strncpy(caifd->layer.name, dev->name,
248 sizeof(caifd->layer.name) - 1);
249 caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
251 mutex_lock(&caifdevs->lock);
252 list_add_rcu(&caifd->list, &caifdevs->list);
254 cfcnfg_add_phy_layer(cfg,
255 phy_type,
256 dev,
257 &caifd->layer,
258 pref,
259 caifdev->use_fcs,
260 caifdev->use_stx);
261 mutex_unlock(&caifdevs->lock);
262 break;
264 case NETDEV_UP:
265 rcu_read_lock();
267 caifd = caif_get(dev);
268 if (caifd == NULL) {
269 rcu_read_unlock();
270 break;
273 cfcnfg_set_phy_state(cfg, &caifd->layer, true);
274 rcu_read_unlock();
276 break;
278 case NETDEV_DOWN:
279 rcu_read_lock();
281 caifd = caif_get(dev);
282 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
283 rcu_read_unlock();
284 return -EINVAL;
287 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
288 caifd_hold(caifd);
289 rcu_read_unlock();
291 caifd->layer.up->ctrlcmd(caifd->layer.up,
292 _CAIF_CTRLCMD_PHYIF_DOWN_IND,
293 caifd->layer.id);
294 caifd_put(caifd);
295 break;
297 case NETDEV_UNREGISTER:
298 mutex_lock(&caifdevs->lock);
300 caifd = caif_get(dev);
301 if (caifd == NULL) {
302 mutex_unlock(&caifdevs->lock);
303 break;
305 list_del_rcu(&caifd->list);
308 * NETDEV_UNREGISTER is called repeatedly until all reference
309 * counts for the net-device are released. If references to
310 * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
311 * the next call to NETDEV_UNREGISTER.
313 * If any packets are in flight down the CAIF Stack,
314 * cfcnfg_del_phy_layer will return nonzero.
315 * If no packets are in flight, the CAIF Stack associated
316 * with the net-device un-registering is freed.
319 if (caifd_refcnt_read(caifd) != 0 ||
320 cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
322 pr_info("Wait for device inuse\n");
323 /* Enrole device if CAIF Stack is still in use */
324 list_add_rcu(&caifd->list, &caifdevs->list);
325 mutex_unlock(&caifdevs->lock);
326 break;
329 synchronize_rcu();
330 dev_put(caifd->netdev);
331 free_percpu(caifd->pcpu_refcnt);
332 kfree(caifd);
334 mutex_unlock(&caifdevs->lock);
335 break;
337 return 0;
340 static struct notifier_block caif_device_notifier = {
341 .notifier_call = caif_device_notify,
342 .priority = 0,
345 /* Per-namespace Caif devices handling */
346 static int caif_init_net(struct net *net)
348 struct caif_net *caifn = net_generic(net, caif_net_id);
350 INIT_LIST_HEAD(&caifn->caifdevs.list);
351 mutex_init(&caifn->caifdevs.lock);
353 caifn->cfg = cfcnfg_create();
354 if (!caifn->cfg) {
355 pr_warn("can't create cfcnfg\n");
356 return -ENOMEM;
359 return 0;
362 static void caif_exit_net(struct net *net)
364 struct caif_device_entry *caifd, *tmp;
365 struct caif_device_entry_list *caifdevs =
366 caif_device_list(net);
367 struct cfcnfg *cfg;
369 rtnl_lock();
370 mutex_lock(&caifdevs->lock);
372 cfg = get_cfcnfg(net);
373 if (cfg == NULL) {
374 mutex_unlock(&caifdevs->lock);
375 return;
378 list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
379 int i = 0;
380 list_del_rcu(&caifd->list);
381 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
383 while (i < 10 &&
384 (caifd_refcnt_read(caifd) != 0 ||
385 cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
387 pr_info("Wait for device inuse\n");
388 msleep(250);
389 i++;
391 synchronize_rcu();
392 dev_put(caifd->netdev);
393 free_percpu(caifd->pcpu_refcnt);
394 kfree(caifd);
396 cfcnfg_remove(cfg);
398 mutex_unlock(&caifdevs->lock);
399 rtnl_unlock();
402 static struct pernet_operations caif_net_ops = {
403 .init = caif_init_net,
404 .exit = caif_exit_net,
405 .id = &caif_net_id,
406 .size = sizeof(struct caif_net),
409 /* Initialize Caif devices list */
410 static int __init caif_device_init(void)
412 int result;
414 result = register_pernet_subsys(&caif_net_ops);
416 if (result)
417 return result;
419 register_netdevice_notifier(&caif_device_notifier);
420 dev_add_pack(&caif_packet_type);
422 return result;
425 static void __exit caif_device_exit(void)
427 unregister_pernet_subsys(&caif_net_ops);
428 unregister_netdevice_notifier(&caif_device_notifier);
429 dev_remove_pack(&caif_packet_type);
432 module_init(caif_device_init);
433 module_exit(caif_device_exit);