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_ATOMIC
);
99 caifd
->pcpu_refcnt
= alloc_percpu(int);
105 static struct caif_device_entry
*caif_get(struct net_device
*dev
)
107 struct caif_device_entry_list
*caifdevs
=
108 caif_device_list(dev_net(dev
));
109 struct caif_device_entry
*caifd
;
111 list_for_each_entry_rcu(caifd
, &caifdevs
->list
, list
) {
112 if (caifd
->netdev
== dev
)
118 static int transmit(struct cflayer
*layer
, struct cfpkt
*pkt
)
121 struct caif_device_entry
*caifd
=
122 container_of(layer
, struct caif_device_entry
, layer
);
125 skb
= cfpkt_tonative(pkt
);
126 skb
->dev
= caifd
->netdev
;
128 err
= dev_queue_xmit(skb
);
136 * Stuff received packets into the CAIF stack.
137 * On error, returns non-zero and releases the skb.
139 static int receive(struct sk_buff
*skb
, struct net_device
*dev
,
140 struct packet_type
*pkttype
, struct net_device
*orig_dev
)
143 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 err
= caifd
->layer
.up
->receive(caifd
->layer
.up
, pkt
);
164 /* For -EILSEQ the packet is not freed so so it now */
168 /* Release reference to stack upwards */
173 static struct packet_type caif_packet_type __read_mostly
= {
174 .type
= cpu_to_be16(ETH_P_CAIF
),
178 static void dev_flowctrl(struct net_device
*dev
, int on
)
180 struct caif_device_entry
*caifd
;
184 caifd
= caif_get(dev
);
185 if (!caifd
|| !caifd
->layer
.up
|| !caifd
->layer
.up
->ctrlcmd
) {
193 caifd
->layer
.up
->ctrlcmd(caifd
->layer
.up
,
195 _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND
:
196 _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND
,
201 /* notify Caif of device events */
202 static int caif_device_notify(struct notifier_block
*me
, unsigned long what
,
205 struct net_device
*dev
= arg
;
206 struct caif_device_entry
*caifd
= NULL
;
207 struct caif_dev_common
*caifdev
;
208 enum cfcnfg_phy_preference pref
;
209 enum cfcnfg_phy_type phy_type
;
211 struct caif_device_entry_list
*caifdevs
=
212 caif_device_list(dev_net(dev
));
214 if (dev
->type
!= ARPHRD_CAIF
)
217 cfg
= get_cfcnfg(dev_net(dev
));
222 case NETDEV_REGISTER
:
223 caifd
= caif_device_alloc(dev
);
227 caifdev
= netdev_priv(dev
);
228 caifdev
->flowctrl
= dev_flowctrl
;
230 caifd
->layer
.transmit
= transmit
;
232 if (caifdev
->use_frag
)
233 phy_type
= CFPHYTYPE_FRAG
;
235 phy_type
= CFPHYTYPE_CAIF
;
237 switch (caifdev
->link_select
) {
238 case CAIF_LINK_HIGH_BANDW
:
239 pref
= CFPHYPREF_HIGH_BW
;
241 case CAIF_LINK_LOW_LATENCY
:
242 pref
= CFPHYPREF_LOW_LAT
;
245 pref
= CFPHYPREF_HIGH_BW
;
248 strncpy(caifd
->layer
.name
, dev
->name
,
249 sizeof(caifd
->layer
.name
) - 1);
250 caifd
->layer
.name
[sizeof(caifd
->layer
.name
) - 1] = 0;
252 mutex_lock(&caifdevs
->lock
);
253 list_add_rcu(&caifd
->list
, &caifdevs
->list
);
255 cfcnfg_add_phy_layer(cfg
,
262 mutex_unlock(&caifdevs
->lock
);
268 caifd
= caif_get(dev
);
274 cfcnfg_set_phy_state(cfg
, &caifd
->layer
, true);
282 caifd
= caif_get(dev
);
283 if (!caifd
|| !caifd
->layer
.up
|| !caifd
->layer
.up
->ctrlcmd
) {
288 cfcnfg_set_phy_state(cfg
, &caifd
->layer
, false);
292 caifd
->layer
.up
->ctrlcmd(caifd
->layer
.up
,
293 _CAIF_CTRLCMD_PHYIF_DOWN_IND
,
298 case NETDEV_UNREGISTER
:
299 mutex_lock(&caifdevs
->lock
);
301 caifd
= caif_get(dev
);
303 mutex_unlock(&caifdevs
->lock
);
306 list_del_rcu(&caifd
->list
);
309 * NETDEV_UNREGISTER is called repeatedly until all reference
310 * counts for the net-device are released. If references to
311 * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
312 * the next call to NETDEV_UNREGISTER.
314 * If any packets are in flight down the CAIF Stack,
315 * cfcnfg_del_phy_layer will return nonzero.
316 * If no packets are in flight, the CAIF Stack associated
317 * with the net-device un-registering is freed.
320 if (caifd_refcnt_read(caifd
) != 0 ||
321 cfcnfg_del_phy_layer(cfg
, &caifd
->layer
) != 0) {
323 pr_info("Wait for device inuse\n");
324 /* Enrole device if CAIF Stack is still in use */
325 list_add_rcu(&caifd
->list
, &caifdevs
->list
);
326 mutex_unlock(&caifdevs
->lock
);
331 dev_put(caifd
->netdev
);
332 free_percpu(caifd
->pcpu_refcnt
);
335 mutex_unlock(&caifdevs
->lock
);
341 static struct notifier_block caif_device_notifier
= {
342 .notifier_call
= caif_device_notify
,
346 /* Per-namespace Caif devices handling */
347 static int caif_init_net(struct net
*net
)
349 struct caif_net
*caifn
= net_generic(net
, caif_net_id
);
351 INIT_LIST_HEAD(&caifn
->caifdevs
.list
);
352 mutex_init(&caifn
->caifdevs
.lock
);
354 caifn
->cfg
= cfcnfg_create();
356 pr_warn("can't create cfcnfg\n");
363 static void caif_exit_net(struct net
*net
)
365 struct caif_device_entry
*caifd
, *tmp
;
366 struct caif_device_entry_list
*caifdevs
=
367 caif_device_list(net
);
371 mutex_lock(&caifdevs
->lock
);
373 cfg
= get_cfcnfg(net
);
375 mutex_unlock(&caifdevs
->lock
);
379 list_for_each_entry_safe(caifd
, tmp
, &caifdevs
->list
, list
) {
381 list_del_rcu(&caifd
->list
);
382 cfcnfg_set_phy_state(cfg
, &caifd
->layer
, false);
385 (caifd_refcnt_read(caifd
) != 0 ||
386 cfcnfg_del_phy_layer(cfg
, &caifd
->layer
) != 0)) {
388 pr_info("Wait for device inuse\n");
393 dev_put(caifd
->netdev
);
394 free_percpu(caifd
->pcpu_refcnt
);
399 mutex_unlock(&caifdevs
->lock
);
403 static struct pernet_operations caif_net_ops
= {
404 .init
= caif_init_net
,
405 .exit
= caif_exit_net
,
407 .size
= sizeof(struct caif_net
),
410 /* Initialize Caif devices list */
411 static int __init
caif_device_init(void)
415 result
= register_pernet_device(&caif_net_ops
);
420 register_netdevice_notifier(&caif_device_notifier
);
421 dev_add_pack(&caif_packet_type
);
426 static void __exit
caif_device_exit(void)
428 unregister_pernet_device(&caif_net_ops
);
429 unregister_netdevice_notifier(&caif_device_notifier
);
430 dev_remove_pack(&caif_packet_type
);
433 module_init(caif_device_init
);
434 module_exit(caif_device_exit
);