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
);
58 EXPORT_SYMBOL(get_cfcnfg
);
60 static struct caif_device_entry_list
*caif_device_list(struct net
*net
)
62 struct caif_net
*caifn
;
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
)
81 for_each_possible_cpu(i
)
82 refcnt
+= *per_cpu_ptr(e
->pcpu_refcnt
, i
);
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
);
97 caifd
->pcpu_refcnt
= alloc_percpu(int);
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
)
116 static int transmit(struct cflayer
*layer
, struct cfpkt
*pkt
)
119 struct caif_device_entry
*caifd
=
120 container_of(layer
, struct caif_device_entry
, layer
);
123 skb
= cfpkt_tonative(pkt
);
124 skb
->dev
= caifd
->netdev
;
126 err
= dev_queue_xmit(skb
);
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
)
141 struct caif_device_entry
*caifd
;
144 pkt
= cfpkt_fromnative(CAIF_DIR_IN
, skb
);
147 caifd
= caif_get(dev
);
149 if (!caifd
|| !caifd
->layer
.up
|| !caifd
->layer
.up
->receive
||
150 !netif_oper_up(caifd
->netdev
)) {
156 /* Hold reference to netdevice while using CAIF stack */
160 err
= caifd
->layer
.up
->receive(caifd
->layer
.up
, pkt
);
162 /* For -EILSEQ the packet is not freed so so it now */
166 /* Release reference to stack upwards */
171 static struct packet_type caif_packet_type __read_mostly
= {
172 .type
= cpu_to_be16(ETH_P_CAIF
),
176 static void dev_flowctrl(struct net_device
*dev
, int on
)
178 struct caif_device_entry
*caifd
;
182 caifd
= caif_get(dev
);
183 if (!caifd
|| !caifd
->layer
.up
|| !caifd
->layer
.up
->ctrlcmd
) {
191 caifd
->layer
.up
->ctrlcmd(caifd
->layer
.up
,
193 _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND
:
194 _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND
,
199 /* notify Caif of device events */
200 static int caif_device_notify(struct notifier_block
*me
, unsigned long what
,
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
;
209 struct caif_device_entry_list
*caifdevs
;
211 if (dev
->type
!= ARPHRD_CAIF
)
214 cfg
= get_cfcnfg(dev_net(dev
));
218 caifdevs
= caif_device_list(dev_net(dev
));
221 case NETDEV_REGISTER
:
222 caifd
= caif_device_alloc(dev
);
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
;
234 phy_type
= CFPHYTYPE_CAIF
;
236 switch (caifdev
->link_select
) {
237 case CAIF_LINK_HIGH_BANDW
:
238 pref
= CFPHYPREF_HIGH_BW
;
240 case CAIF_LINK_LOW_LATENCY
:
241 pref
= CFPHYPREF_LOW_LAT
;
244 pref
= CFPHYPREF_HIGH_BW
;
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
,
261 mutex_unlock(&caifdevs
->lock
);
267 caifd
= caif_get(dev
);
273 cfcnfg_set_phy_state(cfg
, &caifd
->layer
, true);
281 caifd
= caif_get(dev
);
282 if (!caifd
|| !caifd
->layer
.up
|| !caifd
->layer
.up
->ctrlcmd
) {
287 cfcnfg_set_phy_state(cfg
, &caifd
->layer
, false);
291 caifd
->layer
.up
->ctrlcmd(caifd
->layer
.up
,
292 _CAIF_CTRLCMD_PHYIF_DOWN_IND
,
297 case NETDEV_UNREGISTER
:
298 mutex_lock(&caifdevs
->lock
);
300 caifd
= caif_get(dev
);
302 mutex_unlock(&caifdevs
->lock
);
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
);
330 dev_put(caifd
->netdev
);
331 free_percpu(caifd
->pcpu_refcnt
);
334 mutex_unlock(&caifdevs
->lock
);
340 static struct notifier_block caif_device_notifier
= {
341 .notifier_call
= caif_device_notify
,
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();
355 pr_warn("can't create cfcnfg\n");
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
);
370 mutex_lock(&caifdevs
->lock
);
372 cfg
= get_cfcnfg(net
);
374 mutex_unlock(&caifdevs
->lock
);
378 list_for_each_entry_safe(caifd
, tmp
, &caifdevs
->list
, list
) {
380 list_del_rcu(&caifd
->list
);
381 cfcnfg_set_phy_state(cfg
, &caifd
->layer
, false);
384 (caifd_refcnt_read(caifd
) != 0 ||
385 cfcnfg_del_phy_layer(cfg
, &caifd
->layer
) != 0)) {
387 pr_info("Wait for device inuse\n");
392 dev_put(caifd
->netdev
);
393 free_percpu(caifd
->pcpu_refcnt
);
398 mutex_unlock(&caifdevs
->lock
);
402 static struct pernet_operations caif_net_ops
= {
403 .init
= caif_init_net
,
404 .exit
= caif_exit_net
,
406 .size
= sizeof(struct caif_net
),
409 /* Initialize Caif devices list */
410 static int __init
caif_device_init(void)
414 result
= register_pernet_subsys(&caif_net_ops
);
419 register_netdevice_notifier(&caif_device_notifier
);
420 dev_add_pack(&caif_packet_type
);
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
);