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 {
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 */
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;
55 caifn = net_generic(net, caif_net_id);
59 EXPORT_SYMBOL(get_cfcnfg);
61 static struct caif_device_entry_list *caif_device_list(struct net *net)
63 struct caif_net *caifn;
65 caifn = net_generic(net, caif_net_id);
67 return &caifn->caifdevs;
70 static void caifd_put(struct caif_device_entry *e)
72 irqsafe_cpu_dec(*e->pcpu_refcnt);
75 static void caifd_hold(struct caif_device_entry *e)
77 irqsafe_cpu_inc(*e->pcpu_refcnt);
80 static int caifd_refcnt_read(struct caif_device_entry *e)
83 for_each_possible_cpu(i)
84 refcnt += *per_cpu_ptr(e->pcpu_refcnt, i);
88 /* Allocate new CAIF device. */
89 static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
91 struct caif_device_entry_list *caifdevs;
92 struct caif_device_entry *caifd;
94 caifdevs = caif_device_list(dev_net(dev));
97 caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC);
100 caifd->pcpu_refcnt = alloc_percpu(int);
106 static struct caif_device_entry *caif_get(struct net_device *dev)
108 struct caif_device_entry_list *caifdevs =
109 caif_device_list(dev_net(dev));
110 struct caif_device_entry *caifd;
112 list_for_each_entry_rcu(caifd, &caifdevs->list, list) {
113 if (caifd->netdev == dev)
119 static int transmit(struct cflayer *layer, struct cfpkt *pkt)
122 struct caif_device_entry *caifd =
123 container_of(layer, struct caif_device_entry, layer);
126 skb = cfpkt_tonative(pkt);
127 skb->dev = caifd->netdev;
129 err = dev_queue_xmit(skb);
137 * Stuff received packets into the CAIF stack.
138 * On error, returns non-zero and releases the skb.
140 static int receive(struct sk_buff *skb, struct net_device *dev,
141 struct packet_type *pkttype, struct net_device *orig_dev)
144 struct caif_device_entry *caifd;
146 pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
149 caifd = caif_get(dev);
151 if (!caifd || !caifd->layer.up || !caifd->layer.up->receive ||
152 !netif_oper_up(caifd->netdev)) {
158 /* Hold reference to netdevice while using CAIF stack */
162 caifd->layer.up->receive(caifd->layer.up, pkt);
164 /* Release reference to stack upwards */
169 static struct packet_type caif_packet_type __read_mostly = {
170 .type = cpu_to_be16(ETH_P_CAIF),
174 static void dev_flowctrl(struct net_device *dev, int on)
176 struct caif_device_entry *caifd;
180 caifd = caif_get(dev);
181 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
189 caifd->layer.up->ctrlcmd(caifd->layer.up,
191 _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
192 _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
197 /* notify Caif of device events */
198 static int caif_device_notify(struct notifier_block *me, unsigned long what,
201 struct net_device *dev = arg;
202 struct caif_device_entry *caifd = NULL;
203 struct caif_dev_common *caifdev;
204 enum cfcnfg_phy_preference pref;
205 enum cfcnfg_phy_type phy_type;
207 struct caif_device_entry_list *caifdevs =
208 caif_device_list(dev_net(dev));
210 if (dev->type != ARPHRD_CAIF)
213 cfg = get_cfcnfg(dev_net(dev));
218 case NETDEV_REGISTER:
219 caifd = caif_device_alloc(dev);
223 caifdev = netdev_priv(dev);
224 caifdev->flowctrl = dev_flowctrl;
226 caifd->layer.transmit = transmit;
228 if (caifdev->use_frag)
229 phy_type = CFPHYTYPE_FRAG;
231 phy_type = CFPHYTYPE_CAIF;
233 switch (caifdev->link_select) {
234 case CAIF_LINK_HIGH_BANDW:
235 pref = CFPHYPREF_HIGH_BW;
237 case CAIF_LINK_LOW_LATENCY:
238 pref = CFPHYPREF_LOW_LAT;
241 pref = CFPHYPREF_HIGH_BW;
244 strncpy(caifd->layer.name, dev->name,
245 sizeof(caifd->layer.name) - 1);
246 caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
248 mutex_lock(&caifdevs->lock);
249 list_add_rcu(&caifd->list, &caifdevs->list);
251 cfcnfg_add_phy_layer(cfg,
258 mutex_unlock(&caifdevs->lock);
264 caifd = caif_get(dev);
270 cfcnfg_set_phy_state(cfg, &caifd->layer, true);
278 caifd = caif_get(dev);
279 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
284 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
288 caifd->layer.up->ctrlcmd(caifd->layer.up,
289 _CAIF_CTRLCMD_PHYIF_DOWN_IND,
294 case NETDEV_UNREGISTER:
295 mutex_lock(&caifdevs->lock);
297 caifd = caif_get(dev);
299 mutex_unlock(&caifdevs->lock);
302 list_del_rcu(&caifd->list);
305 * NETDEV_UNREGISTER is called repeatedly until all reference
306 * counts for the net-device are released. If references to
307 * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
308 * the next call to NETDEV_UNREGISTER.
310 * If any packets are in flight down the CAIF Stack,
311 * cfcnfg_del_phy_layer will return nonzero.
312 * If no packets are in flight, the CAIF Stack associated
313 * with the net-device un-registering is freed.
316 if (caifd_refcnt_read(caifd) != 0 ||
317 cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
319 pr_info("Wait for device inuse\n");
320 /* Enrole device if CAIF Stack is still in use */
321 list_add_rcu(&caifd->list, &caifdevs->list);
322 mutex_unlock(&caifdevs->lock);
327 dev_put(caifd->netdev);
328 free_percpu(caifd->pcpu_refcnt);
331 mutex_unlock(&caifdevs->lock);
337 static struct notifier_block caif_device_notifier = {
338 .notifier_call = caif_device_notify,
342 /* Per-namespace Caif devices handling */
343 static int caif_init_net(struct net *net)
345 struct caif_net *caifn = net_generic(net, caif_net_id);
347 INIT_LIST_HEAD(&caifn->caifdevs.list);
348 mutex_init(&caifn->caifdevs.lock);
350 caifn->cfg = cfcnfg_create();
352 pr_warn("can't create cfcnfg\n");
359 static void caif_exit_net(struct net *net)
361 struct caif_device_entry *caifd, *tmp;
362 struct caif_device_entry_list *caifdevs =
363 caif_device_list(net);
367 mutex_lock(&caifdevs->lock);
369 cfg = get_cfcnfg(net);
371 mutex_unlock(&caifdevs->lock);
375 list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
377 list_del_rcu(&caifd->list);
378 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
381 (caifd_refcnt_read(caifd) != 0 ||
382 cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
384 pr_info("Wait for device inuse\n");
389 dev_put(caifd->netdev);
390 free_percpu(caifd->pcpu_refcnt);
395 mutex_unlock(&caifdevs->lock);
399 static struct pernet_operations caif_net_ops = {
400 .init = caif_init_net,
401 .exit = caif_exit_net,
403 .size = sizeof(struct caif_net),
406 /* Initialize Caif devices list */
407 static int __init caif_device_init(void)
411 result = register_pernet_device(&caif_net_ops);
416 register_netdevice_notifier(&caif_device_notifier);
417 dev_add_pack(&caif_packet_type);
422 static void __exit caif_device_exit(void)
424 unregister_pernet_device(&caif_net_ops);
425 unregister_netdevice_notifier(&caif_device_notifier);
426 dev_remove_pack(&caif_packet_type);
429 module_init(caif_device_init);
430 module_exit(caif_device_exit);