blob: 682c0fedf360c3f87177439f3a35e01e18daac18 [file] [log] [blame]
Sjur Braendelandc72dfae2010-03-30 13:56:25 +00001/*
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
6 *
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>
10 */
11
Joe Perchesb31fa5b2010-09-05 21:31:11 +000012#define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
13
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000014#include <linux/version.h>
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000015#include <linux/kernel.h>
16#include <linux/if_arp.h>
17#include <linux/net.h>
18#include <linux/netdevice.h>
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +000019#include <linux/mutex.h>
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000020#include <net/netns/generic.h>
21#include <net/net_namespace.h>
22#include <net/pkt_sched.h>
23#include <net/caif/caif_device.h>
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000024#include <net/caif/caif_layer.h>
25#include <net/caif/cfpkt.h>
26#include <net/caif/cfcnfg.h>
27
28MODULE_LICENSE("GPL");
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000029
30/* Used for local tracking of the CAIF net devices */
31struct caif_device_entry {
32 struct cflayer layer;
33 struct list_head list;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000034 struct net_device *netdev;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +000035 int __percpu *pcpu_refcnt;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000036};
37
38struct caif_device_entry_list {
39 struct list_head list;
40 /* Protects simulanous deletes in list */
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +000041 struct mutex lock;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000042};
43
44struct caif_net {
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +000045 struct cfcnfg *cfg;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000046 struct caif_device_entry_list caifdevs;
47};
48
49static int caif_net_id;
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +000050
51struct cfcnfg *get_cfcnfg(struct net *net)
52{
53 struct caif_net *caifn;
54 BUG_ON(!net);
55 caifn = net_generic(net, caif_net_id);
56 BUG_ON(!caifn);
57 return caifn->cfg;
58}
59EXPORT_SYMBOL(get_cfcnfg);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000060
61static struct caif_device_entry_list *caif_device_list(struct net *net)
62{
63 struct caif_net *caifn;
64 BUG_ON(!net);
65 caifn = net_generic(net, caif_net_id);
66 BUG_ON(!caifn);
67 return &caifn->caifdevs;
68}
69
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +000070static void caifd_put(struct caif_device_entry *e)
71{
72 irqsafe_cpu_dec(*e->pcpu_refcnt);
73}
74
75static void caifd_hold(struct caif_device_entry *e)
76{
77 irqsafe_cpu_inc(*e->pcpu_refcnt);
78}
79
80static int caifd_refcnt_read(struct caif_device_entry *e)
81{
82 int i, refcnt = 0;
83 for_each_possible_cpu(i)
84 refcnt += *per_cpu_ptr(e->pcpu_refcnt, i);
85 return refcnt;
86}
87
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000088/* Allocate new CAIF device. */
89static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
90{
91 struct caif_device_entry_list *caifdevs;
92 struct caif_device_entry *caifd;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +000093
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000094 caifdevs = caif_device_list(dev_net(dev));
95 BUG_ON(!caifdevs);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +000096
Sjur Braendelandc72dfae2010-03-30 13:56:25 +000097 caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC);
98 if (!caifd)
99 return NULL;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000100 caifd->pcpu_refcnt = alloc_percpu(int);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000101 caifd->netdev = dev;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000102 dev_hold(dev);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000103 return caifd;
104}
105
106static struct caif_device_entry *caif_get(struct net_device *dev)
107{
108 struct caif_device_entry_list *caifdevs =
109 caif_device_list(dev_net(dev));
110 struct caif_device_entry *caifd;
111 BUG_ON(!caifdevs);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000112 list_for_each_entry_rcu(caifd, &caifdevs->list, list) {
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000113 if (caifd->netdev == dev)
114 return caifd;
115 }
116 return NULL;
117}
118
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000119static int transmit(struct cflayer *layer, struct cfpkt *pkt)
120{
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000121 int err;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000122 struct caif_device_entry *caifd =
123 container_of(layer, struct caif_device_entry, layer);
Sjur Brændeland4dd820c2011-04-11 10:43:51 +0000124 struct sk_buff *skb;
125
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000126 skb = cfpkt_tonative(pkt);
127 skb->dev = caifd->netdev;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000128
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000129 err = dev_queue_xmit(skb);
130 if (err > 0)
131 err = -EIO;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000132
sjur.brandeland@stericsson.comc85c2952011-05-13 02:44:06 +0000133 return err;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000134}
135
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000136/*
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000137 * Stuff received packets into the CAIF stack.
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000138 * On error, returns non-zero and releases the skb.
139 */
140static int receive(struct sk_buff *skb, struct net_device *dev,
141 struct packet_type *pkttype, struct net_device *orig_dev)
142{
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000143 struct cfpkt *pkt;
144 struct caif_device_entry *caifd;
sjur.brandeland@stericsson.com69c867c2011-05-22 11:18:54 +0000145 int err;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000146
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000147 pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000148
149 rcu_read_lock();
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000150 caifd = caif_get(dev);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000151
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000152 if (!caifd || !caifd->layer.up || !caifd->layer.up->receive ||
153 !netif_oper_up(caifd->netdev)) {
154 rcu_read_unlock();
155 kfree_skb(skb);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000156 return NET_RX_DROP;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000157 }
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000158
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000159 /* Hold reference to netdevice while using CAIF stack */
160 caifd_hold(caifd);
161 rcu_read_unlock();
162
sjur.brandeland@stericsson.com69c867c2011-05-22 11:18:54 +0000163 err = caifd->layer.up->receive(caifd->layer.up, pkt);
164
165 /* For -EILSEQ the packet is not freed so so it now */
166 if (err == -EILSEQ)
167 cfpkt_destroy(pkt);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000168
169 /* Release reference to stack upwards */
170 caifd_put(caifd);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000171 return 0;
172}
173
174static struct packet_type caif_packet_type __read_mostly = {
175 .type = cpu_to_be16(ETH_P_CAIF),
176 .func = receive,
177};
178
179static void dev_flowctrl(struct net_device *dev, int on)
180{
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000181 struct caif_device_entry *caifd;
182
183 rcu_read_lock();
184
185 caifd = caif_get(dev);
186 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
187 rcu_read_unlock();
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000188 return;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000189 }
190
191 caifd_hold(caifd);
192 rcu_read_unlock();
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000193
194 caifd->layer.up->ctrlcmd(caifd->layer.up,
195 on ?
196 _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
197 _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
198 caifd->layer.id);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000199 caifd_put(caifd);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000200}
201
202/* notify Caif of device events */
203static int caif_device_notify(struct notifier_block *me, unsigned long what,
204 void *arg)
205{
206 struct net_device *dev = arg;
207 struct caif_device_entry *caifd = NULL;
208 struct caif_dev_common *caifdev;
209 enum cfcnfg_phy_preference pref;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000210 enum cfcnfg_phy_type phy_type;
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000211 struct cfcnfg *cfg;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000212 struct caif_device_entry_list *caifdevs =
213 caif_device_list(dev_net(dev));
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000214
215 if (dev->type != ARPHRD_CAIF)
216 return 0;
217
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000218 cfg = get_cfcnfg(dev_net(dev));
219 if (cfg == NULL)
220 return 0;
221
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000222 switch (what) {
223 case NETDEV_REGISTER:
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000224 caifd = caif_device_alloc(dev);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000225 if (!caifd)
226 return 0;
227
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000228 caifdev = netdev_priv(dev);
229 caifdev->flowctrl = dev_flowctrl;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000230
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000231 caifd->layer.transmit = transmit;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000232
233 if (caifdev->use_frag)
234 phy_type = CFPHYTYPE_FRAG;
235 else
236 phy_type = CFPHYTYPE_CAIF;
237
238 switch (caifdev->link_select) {
239 case CAIF_LINK_HIGH_BANDW:
Sjur Braendeland2c485202010-04-28 08:54:40 +0000240 pref = CFPHYPREF_HIGH_BW;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000241 break;
242 case CAIF_LINK_LOW_LATENCY:
Sjur Braendeland2c485202010-04-28 08:54:40 +0000243 pref = CFPHYPREF_LOW_LAT;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000244 break;
245 default:
246 pref = CFPHYPREF_HIGH_BW;
247 break;
248 }
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000249 strncpy(caifd->layer.name, dev->name,
250 sizeof(caifd->layer.name) - 1);
251 caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
252
253 mutex_lock(&caifdevs->lock);
254 list_add_rcu(&caifd->list, &caifdevs->list);
255
Stephen Hemminger73d6ac62011-04-11 10:43:50 +0000256 cfcnfg_add_phy_layer(cfg,
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000257 phy_type,
258 dev,
259 &caifd->layer,
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000260 pref,
261 caifdev->use_fcs,
262 caifdev->use_stx);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000263 mutex_unlock(&caifdevs->lock);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000264 break;
265
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000266 case NETDEV_UP:
267 rcu_read_lock();
268
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000269 caifd = caif_get(dev);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000270 if (caifd == NULL) {
271 rcu_read_unlock();
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000272 break;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000273 }
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000274
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000275 cfcnfg_set_phy_state(cfg, &caifd->layer, true);
276 rcu_read_unlock();
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000277
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000278 break;
279
280 case NETDEV_DOWN:
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000281 rcu_read_lock();
282
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000283 caifd = caif_get(dev);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000284 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
285 rcu_read_unlock();
286 return -EINVAL;
287 }
288
289 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
290 caifd_hold(caifd);
291 rcu_read_unlock();
292
293 caifd->layer.up->ctrlcmd(caifd->layer.up,
294 _CAIF_CTRLCMD_PHYIF_DOWN_IND,
295 caifd->layer.id);
296 caifd_put(caifd);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000297 break;
298
299 case NETDEV_UNREGISTER:
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000300 mutex_lock(&caifdevs->lock);
301
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000302 caifd = caif_get(dev);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000303 if (caifd == NULL) {
304 mutex_unlock(&caifdevs->lock);
André Carvalho de Matosf2527ec2010-11-01 11:52:47 +0000305 break;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000306 }
307 list_del_rcu(&caifd->list);
308
309 /*
310 * NETDEV_UNREGISTER is called repeatedly until all reference
311 * counts for the net-device are released. If references to
312 * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
313 * the next call to NETDEV_UNREGISTER.
314 *
315 * If any packets are in flight down the CAIF Stack,
316 * cfcnfg_del_phy_layer will return nonzero.
317 * If no packets are in flight, the CAIF Stack associated
318 * with the net-device un-registering is freed.
319 */
320
321 if (caifd_refcnt_read(caifd) != 0 ||
322 cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
323
324 pr_info("Wait for device inuse\n");
325 /* Enrole device if CAIF Stack is still in use */
326 list_add_rcu(&caifd->list, &caifdevs->list);
327 mutex_unlock(&caifdevs->lock);
328 break;
329 }
330
331 synchronize_rcu();
332 dev_put(caifd->netdev);
333 free_percpu(caifd->pcpu_refcnt);
334 kfree(caifd);
335
336 mutex_unlock(&caifdevs->lock);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000337 break;
338 }
339 return 0;
340}
341
342static struct notifier_block caif_device_notifier = {
343 .notifier_call = caif_device_notify,
344 .priority = 0,
345};
346
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000347/* Per-namespace Caif devices handling */
348static int caif_init_net(struct net *net)
349{
350 struct caif_net *caifn = net_generic(net, caif_net_id);
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000351 BUG_ON(!caifn);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000352 INIT_LIST_HEAD(&caifn->caifdevs.list);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000353 mutex_init(&caifn->caifdevs.lock);
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000354
355 caifn->cfg = cfcnfg_create();
356 if (!caifn->cfg) {
357 pr_warn("can't create cfcnfg\n");
358 return -ENOMEM;
359 }
360
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000361 return 0;
362}
363
364static void caif_exit_net(struct net *net)
365{
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000366 struct caif_device_entry *caifd, *tmp;
367 struct caif_device_entry_list *caifdevs =
368 caif_device_list(net);
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000369 struct cfcnfg *cfg;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000370
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000371 rtnl_lock();
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000372 mutex_lock(&caifdevs->lock);
373
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000374 cfg = get_cfcnfg(net);
375 if (cfg == NULL) {
376 mutex_unlock(&caifdevs->lock);
377 return;
378 }
379
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000380 list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
381 int i = 0;
382 list_del_rcu(&caifd->list);
383 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
384
385 while (i < 10 &&
386 (caifd_refcnt_read(caifd) != 0 ||
387 cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
388
389 pr_info("Wait for device inuse\n");
390 msleep(250);
391 i++;
392 }
393 synchronize_rcu();
394 dev_put(caifd->netdev);
395 free_percpu(caifd->pcpu_refcnt);
396 kfree(caifd);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000397 }
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000398 cfcnfg_remove(cfg);
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000399
400 mutex_unlock(&caifdevs->lock);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000401 rtnl_unlock();
402}
403
404static struct pernet_operations caif_net_ops = {
405 .init = caif_init_net,
406 .exit = caif_exit_net,
407 .id = &caif_net_id,
408 .size = sizeof(struct caif_net),
409};
410
411/* Initialize Caif devices list */
412static int __init caif_device_init(void)
413{
414 int result;
sjur.brandeland@stericsson.combd30ce42011-05-13 02:44:00 +0000415
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000416 result = register_pernet_device(&caif_net_ops);
417
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000418 if (result)
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000419 return result;
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000420
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000421 register_netdevice_notifier(&caif_device_notifier);
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000422 dev_add_pack(&caif_packet_type);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000423
424 return result;
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000425}
426
427static void __exit caif_device_exit(void)
428{
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000429 unregister_pernet_device(&caif_net_ops);
430 unregister_netdevice_notifier(&caif_device_notifier);
sjur.brandeland@stericsson.combee925d2011-05-13 02:44:05 +0000431 dev_remove_pack(&caif_packet_type);
Sjur Braendelandc72dfae2010-03-30 13:56:25 +0000432}
433
434module_init(caif_device_init);
435module_exit(caif_device_exit);