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_dev.h>
25 #include <net/caif/caif_layer.h>
26 #include <net/caif/cfpkt.h>
27 #include <net/caif/cfcnfg.h>
29 MODULE_LICENSE("GPL");
31 /* Used for local tracking of the CAIF net devices */
32 struct caif_device_entry {
34 struct list_head list;
35 struct net_device *netdev;
36 int __percpu *pcpu_refcnt;
39 struct caif_device_entry_list {
40 struct list_head list;
41 /* Protects simulanous deletes in list */
46 struct caif_device_entry_list caifdevs;
49 static int caif_net_id;
50 static struct cfcnfg *cfg;
52 static struct caif_device_entry_list *caif_device_list(struct net *net)
54 struct caif_net *caifn;
56 caifn = net_generic(net, caif_net_id);
58 return &caifn->caifdevs;
61 static void caifd_put(struct caif_device_entry *e)
63 irqsafe_cpu_dec(*e->pcpu_refcnt);
66 static void caifd_hold(struct caif_device_entry *e)
68 irqsafe_cpu_inc(*e->pcpu_refcnt);
71 static int caifd_refcnt_read(struct caif_device_entry *e)
74 for_each_possible_cpu(i)
75 refcnt += *per_cpu_ptr(e->pcpu_refcnt, i);
79 /* Allocate new CAIF device. */
80 static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
82 struct caif_device_entry_list *caifdevs;
83 struct caif_device_entry *caifd;
85 caifdevs = caif_device_list(dev_net(dev));
88 caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC);
91 caifd->pcpu_refcnt = alloc_percpu(int);
97 static struct caif_device_entry *caif_get(struct net_device *dev)
99 struct caif_device_entry_list *caifdevs =
100 caif_device_list(dev_net(dev));
101 struct caif_device_entry *caifd;
103 list_for_each_entry_rcu(caifd, &caifdevs->list, list) {
104 if (caifd->netdev == dev)
110 static int transmit(struct cflayer *layer, struct cfpkt *pkt)
112 struct caif_device_entry *caifd =
113 container_of(layer, struct caif_device_entry, layer);
116 skb = cfpkt_tonative(pkt);
117 skb->dev = caifd->netdev;
125 * Stuff received packets into the CAIF stack.
126 * On error, returns non-zero and releases the skb.
128 static int receive(struct sk_buff *skb, struct net_device *dev,
129 struct packet_type *pkttype, struct net_device *orig_dev)
132 struct caif_device_entry *caifd;
134 pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
137 caifd = caif_get(dev);
139 if (!caifd || !caifd->layer.up || !caifd->layer.up->receive ||
140 !netif_oper_up(caifd->netdev)) {
146 /* Hold reference to netdevice while using CAIF stack */
150 caifd->layer.up->receive(caifd->layer.up, pkt);
152 /* Release reference to stack upwards */
157 static struct packet_type caif_packet_type __read_mostly = {
158 .type = cpu_to_be16(ETH_P_CAIF),
162 static void dev_flowctrl(struct net_device *dev, int on)
164 struct caif_device_entry *caifd;
168 caifd = caif_get(dev);
169 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
177 caifd->layer.up->ctrlcmd(caifd->layer.up,
179 _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
180 _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
185 /* notify Caif of device events */
186 static int caif_device_notify(struct notifier_block *me, unsigned long what,
189 struct net_device *dev = arg;
190 struct caif_device_entry *caifd = NULL;
191 struct caif_dev_common *caifdev;
192 enum cfcnfg_phy_preference pref;
193 enum cfcnfg_phy_type phy_type;
194 struct caif_device_entry_list *caifdevs =
195 caif_device_list(dev_net(dev));
197 if (dev->type != ARPHRD_CAIF)
201 case NETDEV_REGISTER:
202 caifd = caif_device_alloc(dev);
206 caifdev = netdev_priv(dev);
207 caifdev->flowctrl = dev_flowctrl;
209 caifd->layer.transmit = transmit;
211 if (caifdev->use_frag)
212 phy_type = CFPHYTYPE_FRAG;
214 phy_type = CFPHYTYPE_CAIF;
216 switch (caifdev->link_select) {
217 case CAIF_LINK_HIGH_BANDW:
218 pref = CFPHYPREF_HIGH_BW;
220 case CAIF_LINK_LOW_LATENCY:
221 pref = CFPHYPREF_LOW_LAT;
224 pref = CFPHYPREF_HIGH_BW;
227 strncpy(caifd->layer.name, dev->name,
228 sizeof(caifd->layer.name) - 1);
229 caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
231 mutex_lock(&caifdevs->lock);
232 list_add_rcu(&caifd->list, &caifdevs->list);
234 cfcnfg_add_phy_layer(cfg,
242 mutex_unlock(&caifdevs->lock);
248 caifd = caif_get(dev);
254 cfcnfg_set_phy_state(cfg, &caifd->layer, true);
262 caifd = caif_get(dev);
263 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
268 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
272 caifd->layer.up->ctrlcmd(caifd->layer.up,
273 _CAIF_CTRLCMD_PHYIF_DOWN_IND,
278 case NETDEV_UNREGISTER:
279 mutex_lock(&caifdevs->lock);
281 caifd = caif_get(dev);
283 mutex_unlock(&caifdevs->lock);
286 list_del_rcu(&caifd->list);
289 * NETDEV_UNREGISTER is called repeatedly until all reference
290 * counts for the net-device are released. If references to
291 * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
292 * the next call to NETDEV_UNREGISTER.
294 * If any packets are in flight down the CAIF Stack,
295 * cfcnfg_del_phy_layer will return nonzero.
296 * If no packets are in flight, the CAIF Stack associated
297 * with the net-device un-registering is freed.
300 if (caifd_refcnt_read(caifd) != 0 ||
301 cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
303 pr_info("Wait for device inuse\n");
304 /* Enrole device if CAIF Stack is still in use */
305 list_add_rcu(&caifd->list, &caifdevs->list);
306 mutex_unlock(&caifdevs->lock);
311 dev_put(caifd->netdev);
312 free_percpu(caifd->pcpu_refcnt);
315 mutex_unlock(&caifdevs->lock);
321 static struct notifier_block caif_device_notifier = {
322 .notifier_call = caif_device_notify,
326 int caif_connect_client(struct caif_connect_request *conn_req,
327 struct cflayer *client_layer, int *ifindex,
328 int *headroom, int *tailroom)
330 struct cfctrl_link_param param;
333 ret = caif_connect_req_to_link_param(cfg, conn_req, ¶m);
336 /* Hook up the adaptation layer. */
337 return cfcnfg_add_adaptation_layer(cfg, ¶m,
338 client_layer, ifindex,
341 EXPORT_SYMBOL(caif_connect_client);
343 int caif_disconnect_client(struct cflayer *adap_layer)
345 return cfcnfg_disconn_adapt_layer(cfg, adap_layer);
347 EXPORT_SYMBOL(caif_disconnect_client);
349 /* Per-namespace Caif devices handling */
350 static int caif_init_net(struct net *net)
352 struct caif_net *caifn = net_generic(net, caif_net_id);
353 INIT_LIST_HEAD(&caifn->caifdevs.list);
354 mutex_init(&caifn->caifdevs.lock);
358 static void caif_exit_net(struct net *net)
360 struct caif_device_entry *caifd, *tmp;
361 struct caif_device_entry_list *caifdevs =
362 caif_device_list(net);
365 mutex_lock(&caifdevs->lock);
367 list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
369 list_del_rcu(&caifd->list);
370 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
373 (caifd_refcnt_read(caifd) != 0 ||
374 cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
376 pr_info("Wait for device inuse\n");
381 dev_put(caifd->netdev);
382 free_percpu(caifd->pcpu_refcnt);
387 mutex_unlock(&caifdevs->lock);
391 static struct pernet_operations caif_net_ops = {
392 .init = caif_init_net,
393 .exit = caif_exit_net,
395 .size = sizeof(struct caif_net),
398 /* Initialize Caif devices list */
399 static int __init caif_device_init(void)
403 cfg = cfcnfg_create();
405 pr_warn("can't create cfcnfg\n");
406 goto err_cfcnfg_create_failed;
408 result = register_pernet_device(&caif_net_ops);
415 dev_add_pack(&caif_packet_type);
416 register_netdevice_notifier(&caif_device_notifier);
419 err_cfcnfg_create_failed:
423 static void __exit caif_device_exit(void)
425 dev_remove_pack(&caif_packet_type);
426 unregister_pernet_device(&caif_net_ops);
427 unregister_netdevice_notifier(&caif_device_notifier);
431 module_init(caif_device_init);
432 module_exit(caif_device_exit);