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/kernel.h>
15 #include <linux/if_arp.h>
16 #include <linux/net.h>
17 #include <linux/netdevice.h>
18 #include <linux/mutex.h>
19 #include <net/netns/generic.h>
20 #include <net/net_namespace.h>
21 #include <net/pkt_sched.h>
22 #include <net/caif/caif_device.h>
23 #include <net/caif/caif_layer.h>
24 #include <net/caif/cfpkt.h>
25 #include <net/caif/cfcnfg.h>
27 MODULE_LICENSE("GPL");
29 /* Used for local tracking of the CAIF net devices */
30 struct caif_device_entry
{
32 struct list_head list
;
33 struct net_device
*netdev
;
34 int __percpu
*pcpu_refcnt
;
37 struct caif_device_entry_list
{
38 struct list_head list
;
39 /* Protects simulanous deletes in list */
45 struct caif_device_entry_list caifdevs
;
48 static int caif_net_id
;
50 struct cfcnfg
*get_cfcnfg(struct net
*net
)
52 struct caif_net
*caifn
;
54 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
);
66 return &caifn
->caifdevs
;
69 static void caifd_put(struct caif_device_entry
*e
)
71 irqsafe_cpu_dec(*e
->pcpu_refcnt
);
74 static void caifd_hold(struct caif_device_entry
*e
)
76 irqsafe_cpu_inc(*e
->pcpu_refcnt
);
79 static int caifd_refcnt_read(struct caif_device_entry
*e
)
82 for_each_possible_cpu(i
)
83 refcnt
+= *per_cpu_ptr(e
->pcpu_refcnt
, i
);
87 /* Allocate new CAIF device. */
88 static struct caif_device_entry
*caif_device_alloc(struct net_device
*dev
)
90 struct caif_device_entry_list
*caifdevs
;
91 struct caif_device_entry
*caifd
;
93 caifdevs
= caif_device_list(dev_net(dev
));
96 caifd
= kzalloc(sizeof(*caifd
), GFP_KERNEL
);
99 caifd
->pcpu_refcnt
= alloc_percpu(int);
100 if (!caifd
->pcpu_refcnt
) {
109 static struct caif_device_entry
*caif_get(struct net_device
*dev
)
111 struct caif_device_entry_list
*caifdevs
=
112 caif_device_list(dev_net(dev
));
113 struct caif_device_entry
*caifd
;
115 list_for_each_entry_rcu(caifd
, &caifdevs
->list
, list
) {
116 if (caifd
->netdev
== dev
)
122 static int transmit(struct cflayer
*layer
, struct cfpkt
*pkt
)
125 struct caif_device_entry
*caifd
=
126 container_of(layer
, struct caif_device_entry
, layer
);
129 skb
= cfpkt_tonative(pkt
);
130 skb
->dev
= caifd
->netdev
;
132 err
= dev_queue_xmit(skb
);
140 * Stuff received packets into the CAIF stack.
141 * On error, returns non-zero and releases the skb.
143 static int receive(struct sk_buff
*skb
, struct net_device
*dev
,
144 struct packet_type
*pkttype
, struct net_device
*orig_dev
)
147 struct caif_device_entry
*caifd
;
150 pkt
= cfpkt_fromnative(CAIF_DIR_IN
, skb
);
153 caifd
= caif_get(dev
);
155 if (!caifd
|| !caifd
->layer
.up
|| !caifd
->layer
.up
->receive
||
156 !netif_oper_up(caifd
->netdev
)) {
162 /* Hold reference to netdevice while using CAIF stack */
166 err
= caifd
->layer
.up
->receive(caifd
->layer
.up
, pkt
);
168 /* For -EILSEQ the packet is not freed so so it now */
172 /* Release reference to stack upwards */
177 static struct packet_type caif_packet_type __read_mostly
= {
178 .type
= cpu_to_be16(ETH_P_CAIF
),
182 static void dev_flowctrl(struct net_device
*dev
, int on
)
184 struct caif_device_entry
*caifd
;
188 caifd
= caif_get(dev
);
189 if (!caifd
|| !caifd
->layer
.up
|| !caifd
->layer
.up
->ctrlcmd
) {
197 caifd
->layer
.up
->ctrlcmd(caifd
->layer
.up
,
199 _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND
:
200 _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND
,
205 /* notify Caif of device events */
206 static int caif_device_notify(struct notifier_block
*me
, unsigned long what
,
209 struct net_device
*dev
= arg
;
210 struct caif_device_entry
*caifd
= NULL
;
211 struct caif_dev_common
*caifdev
;
212 enum cfcnfg_phy_preference pref
;
213 enum cfcnfg_phy_type phy_type
;
215 struct caif_device_entry_list
*caifdevs
;
217 if (dev
->type
!= ARPHRD_CAIF
)
220 cfg
= get_cfcnfg(dev_net(dev
));
224 caifdevs
= caif_device_list(dev_net(dev
));
227 case NETDEV_REGISTER
:
228 caifd
= caif_device_alloc(dev
);
232 caifdev
= netdev_priv(dev
);
233 caifdev
->flowctrl
= dev_flowctrl
;
235 caifd
->layer
.transmit
= transmit
;
237 if (caifdev
->use_frag
)
238 phy_type
= CFPHYTYPE_FRAG
;
240 phy_type
= CFPHYTYPE_CAIF
;
242 switch (caifdev
->link_select
) {
243 case CAIF_LINK_HIGH_BANDW
:
244 pref
= CFPHYPREF_HIGH_BW
;
246 case CAIF_LINK_LOW_LATENCY
:
247 pref
= CFPHYPREF_LOW_LAT
;
250 pref
= CFPHYPREF_HIGH_BW
;
253 strncpy(caifd
->layer
.name
, dev
->name
,
254 sizeof(caifd
->layer
.name
) - 1);
255 caifd
->layer
.name
[sizeof(caifd
->layer
.name
) - 1] = 0;
257 mutex_lock(&caifdevs
->lock
);
258 list_add_rcu(&caifd
->list
, &caifdevs
->list
);
260 cfcnfg_add_phy_layer(cfg
,
267 mutex_unlock(&caifdevs
->lock
);
273 caifd
= caif_get(dev
);
279 cfcnfg_set_phy_state(cfg
, &caifd
->layer
, true);
287 caifd
= caif_get(dev
);
288 if (!caifd
|| !caifd
->layer
.up
|| !caifd
->layer
.up
->ctrlcmd
) {
293 cfcnfg_set_phy_state(cfg
, &caifd
->layer
, false);
297 caifd
->layer
.up
->ctrlcmd(caifd
->layer
.up
,
298 _CAIF_CTRLCMD_PHYIF_DOWN_IND
,
303 case NETDEV_UNREGISTER
:
304 mutex_lock(&caifdevs
->lock
);
306 caifd
= caif_get(dev
);
308 mutex_unlock(&caifdevs
->lock
);
311 list_del_rcu(&caifd
->list
);
314 * NETDEV_UNREGISTER is called repeatedly until all reference
315 * counts for the net-device are released. If references to
316 * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
317 * the next call to NETDEV_UNREGISTER.
319 * If any packets are in flight down the CAIF Stack,
320 * cfcnfg_del_phy_layer will return nonzero.
321 * If no packets are in flight, the CAIF Stack associated
322 * with the net-device un-registering is freed.
325 if (caifd_refcnt_read(caifd
) != 0 ||
326 cfcnfg_del_phy_layer(cfg
, &caifd
->layer
) != 0) {
328 pr_info("Wait for device inuse\n");
329 /* Enrole device if CAIF Stack is still in use */
330 list_add_rcu(&caifd
->list
, &caifdevs
->list
);
331 mutex_unlock(&caifdevs
->lock
);
336 dev_put(caifd
->netdev
);
337 free_percpu(caifd
->pcpu_refcnt
);
340 mutex_unlock(&caifdevs
->lock
);
346 static struct notifier_block caif_device_notifier
= {
347 .notifier_call
= caif_device_notify
,
351 /* Per-namespace Caif devices handling */
352 static int caif_init_net(struct net
*net
)
354 struct caif_net
*caifn
= net_generic(net
, caif_net_id
);
356 INIT_LIST_HEAD(&caifn
->caifdevs
.list
);
357 mutex_init(&caifn
->caifdevs
.lock
);
359 caifn
->cfg
= cfcnfg_create();
361 pr_warn("can't create cfcnfg\n");
368 static void caif_exit_net(struct net
*net
)
370 struct caif_device_entry
*caifd
, *tmp
;
371 struct caif_device_entry_list
*caifdevs
=
372 caif_device_list(net
);
376 mutex_lock(&caifdevs
->lock
);
378 cfg
= get_cfcnfg(net
);
380 mutex_unlock(&caifdevs
->lock
);
384 list_for_each_entry_safe(caifd
, tmp
, &caifdevs
->list
, list
) {
386 list_del_rcu(&caifd
->list
);
387 cfcnfg_set_phy_state(cfg
, &caifd
->layer
, false);
390 (caifd_refcnt_read(caifd
) != 0 ||
391 cfcnfg_del_phy_layer(cfg
, &caifd
->layer
) != 0)) {
393 pr_info("Wait for device inuse\n");
398 dev_put(caifd
->netdev
);
399 free_percpu(caifd
->pcpu_refcnt
);
404 mutex_unlock(&caifdevs
->lock
);
408 static struct pernet_operations caif_net_ops
= {
409 .init
= caif_init_net
,
410 .exit
= caif_exit_net
,
412 .size
= sizeof(struct caif_net
),
415 /* Initialize Caif devices list */
416 static int __init
caif_device_init(void)
420 result
= register_pernet_device(&caif_net_ops
);
425 register_netdevice_notifier(&caif_device_notifier
);
426 dev_add_pack(&caif_packet_type
);
431 static void __exit
caif_device_exit(void)
433 unregister_pernet_device(&caif_net_ops
);
434 unregister_netdevice_notifier(&caif_device_notifier
);
435 dev_remove_pack(&caif_packet_type
);
438 module_init(caif_device_init
);
439 module_exit(caif_device_exit
);