6acec1921e7fc42b21b0a36c7d38003230bb5c13
[linux-3.10.git] / net / caif / caif_dev.c
1 /*
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
12 #define pr_fmt(fmt) KBUILD_MODNAME ":%s(): " fmt, __func__
13
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 <linux/module.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>
27 #include <net/caif/cfserl.h>
28
29 MODULE_LICENSE("GPL");
30
31 /* Used for local tracking of the CAIF net devices */
32 struct caif_device_entry {
33         struct cflayer layer;
34         struct list_head list;
35         struct net_device *netdev;
36         int __percpu *pcpu_refcnt;
37 };
38
39 struct caif_device_entry_list {
40         struct list_head list;
41         /* Protects simulanous deletes in list */
42         struct mutex lock;
43 };
44
45 struct caif_net {
46         struct cfcnfg *cfg;
47         struct caif_device_entry_list caifdevs;
48 };
49
50 static int caif_net_id;
51
52 struct cfcnfg *get_cfcnfg(struct net *net)
53 {
54         struct caif_net *caifn;
55         BUG_ON(!net);
56         caifn = net_generic(net, caif_net_id);
57         if (!caifn)
58                 return NULL;
59         return caifn->cfg;
60 }
61 EXPORT_SYMBOL(get_cfcnfg);
62
63 static struct caif_device_entry_list *caif_device_list(struct net *net)
64 {
65         struct caif_net *caifn;
66         BUG_ON(!net);
67         caifn = net_generic(net, caif_net_id);
68         if (!caifn)
69                 return NULL;
70         return &caifn->caifdevs;
71 }
72
73 static void caifd_put(struct caif_device_entry *e)
74 {
75         irqsafe_cpu_dec(*e->pcpu_refcnt);
76 }
77
78 static void caifd_hold(struct caif_device_entry *e)
79 {
80         irqsafe_cpu_inc(*e->pcpu_refcnt);
81 }
82
83 static int caifd_refcnt_read(struct caif_device_entry *e)
84 {
85         int i, refcnt = 0;
86         for_each_possible_cpu(i)
87                 refcnt += *per_cpu_ptr(e->pcpu_refcnt, i);
88         return refcnt;
89 }
90
91 /* Allocate new CAIF device. */
92 static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
93 {
94         struct caif_device_entry_list *caifdevs;
95         struct caif_device_entry *caifd;
96
97         caifdevs = caif_device_list(dev_net(dev));
98         if (!caifdevs)
99                 return NULL;
100
101         caifd = kzalloc(sizeof(*caifd), GFP_KERNEL);
102         if (!caifd)
103                 return NULL;
104         caifd->pcpu_refcnt = alloc_percpu(int);
105         if (!caifd->pcpu_refcnt) {
106                 kfree(caifd);
107                 return NULL;
108         }
109         caifd->netdev = dev;
110         dev_hold(dev);
111         return caifd;
112 }
113
114 static struct caif_device_entry *caif_get(struct net_device *dev)
115 {
116         struct caif_device_entry_list *caifdevs =
117             caif_device_list(dev_net(dev));
118         struct caif_device_entry *caifd;
119         if (!caifdevs)
120                 return NULL;
121
122         list_for_each_entry_rcu(caifd, &caifdevs->list, list) {
123                 if (caifd->netdev == dev)
124                         return caifd;
125         }
126         return NULL;
127 }
128
129 static int transmit(struct cflayer *layer, struct cfpkt *pkt)
130 {
131         int err;
132         struct caif_device_entry *caifd =
133             container_of(layer, struct caif_device_entry, layer);
134         struct sk_buff *skb;
135
136         skb = cfpkt_tonative(pkt);
137         skb->dev = caifd->netdev;
138         skb_reset_network_header(skb);
139         skb->protocol = htons(ETH_P_CAIF);
140
141         err = dev_queue_xmit(skb);
142         if (err > 0)
143                 err = -EIO;
144
145         return err;
146 }
147
148 /*
149  * Stuff received packets into the CAIF stack.
150  * On error, returns non-zero and releases the skb.
151  */
152 static int receive(struct sk_buff *skb, struct net_device *dev,
153                    struct packet_type *pkttype, struct net_device *orig_dev)
154 {
155         struct cfpkt *pkt;
156         struct caif_device_entry *caifd;
157         int err;
158
159         pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
160
161         rcu_read_lock();
162         caifd = caif_get(dev);
163
164         if (!caifd || !caifd->layer.up || !caifd->layer.up->receive ||
165                         !netif_oper_up(caifd->netdev)) {
166                 rcu_read_unlock();
167                 kfree_skb(skb);
168                 return NET_RX_DROP;
169         }
170
171         /* Hold reference to netdevice while using CAIF stack */
172         caifd_hold(caifd);
173         rcu_read_unlock();
174
175         err = caifd->layer.up->receive(caifd->layer.up, pkt);
176
177         /* For -EILSEQ the packet is not freed so so it now */
178         if (err == -EILSEQ)
179                 cfpkt_destroy(pkt);
180
181         /* Release reference to stack upwards */
182         caifd_put(caifd);
183
184         if (err != 0)
185                 err = NET_RX_DROP;
186         return err;
187 }
188
189 static struct packet_type caif_packet_type __read_mostly = {
190         .type = cpu_to_be16(ETH_P_CAIF),
191         .func = receive,
192 };
193
194 static void dev_flowctrl(struct net_device *dev, int on)
195 {
196         struct caif_device_entry *caifd;
197
198         rcu_read_lock();
199
200         caifd = caif_get(dev);
201         if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
202                 rcu_read_unlock();
203                 return;
204         }
205
206         caifd_hold(caifd);
207         rcu_read_unlock();
208
209         caifd->layer.up->ctrlcmd(caifd->layer.up,
210                                  on ?
211                                  _CAIF_CTRLCMD_PHYIF_FLOW_ON_IND :
212                                  _CAIF_CTRLCMD_PHYIF_FLOW_OFF_IND,
213                                  caifd->layer.id);
214         caifd_put(caifd);
215 }
216
217 void caif_enroll_dev(struct net_device *dev, struct caif_dev_common *caifdev,
218                         struct cflayer *link_support, int head_room,
219                         struct cflayer **layer, int (**rcv_func)(
220                                 struct sk_buff *, struct net_device *,
221                                 struct packet_type *, struct net_device *))
222 {
223         struct caif_device_entry *caifd;
224         enum cfcnfg_phy_preference pref;
225         struct cfcnfg *cfg = get_cfcnfg(dev_net(dev));
226         struct caif_device_entry_list *caifdevs;
227
228         caifdevs = caif_device_list(dev_net(dev));
229         if (!cfg || !caifdevs)
230                 return;
231         caifd = caif_device_alloc(dev);
232         if (!caifd)
233                 return;
234         *layer = &caifd->layer;
235
236         switch (caifdev->link_select) {
237         case CAIF_LINK_HIGH_BANDW:
238                 pref = CFPHYPREF_HIGH_BW;
239                 break;
240         case CAIF_LINK_LOW_LATENCY:
241                 pref = CFPHYPREF_LOW_LAT;
242                 break;
243         default:
244                 pref = CFPHYPREF_HIGH_BW;
245                 break;
246         }
247         mutex_lock(&caifdevs->lock);
248         list_add_rcu(&caifd->list, &caifdevs->list);
249
250         strncpy(caifd->layer.name, dev->name,
251                 sizeof(caifd->layer.name) - 1);
252         caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
253         caifd->layer.transmit = transmit;
254         cfcnfg_add_phy_layer(cfg,
255                                 dev,
256                                 &caifd->layer,
257                                 pref,
258                                 link_support,
259                                 caifdev->use_fcs,
260                                 head_room);
261         mutex_unlock(&caifdevs->lock);
262         if (rcv_func)
263                 *rcv_func = receive;
264 }
265 EXPORT_SYMBOL(caif_enroll_dev);
266
267 /* notify Caif of device events */
268 static int caif_device_notify(struct notifier_block *me, unsigned long what,
269                               void *arg)
270 {
271         struct net_device *dev = arg;
272         struct caif_device_entry *caifd = NULL;
273         struct caif_dev_common *caifdev;
274         struct cfcnfg *cfg;
275         struct cflayer *layer, *link_support;
276         int head_room = 0;
277         struct caif_device_entry_list *caifdevs;
278
279         cfg = get_cfcnfg(dev_net(dev));
280         caifdevs = caif_device_list(dev_net(dev));
281         if (!cfg || !caifdevs)
282                 return 0;
283
284         caifd = caif_get(dev);
285         if (caifd == NULL && dev->type != ARPHRD_CAIF)
286                 return 0;
287
288         switch (what) {
289         case NETDEV_REGISTER:
290                 if (caifd != NULL)
291                         break;
292
293                 caifdev = netdev_priv(dev);
294
295                 link_support = NULL;
296                 if (caifdev->use_frag) {
297                         head_room = 1;
298                         link_support = cfserl_create(dev->ifindex,
299                                                         caifdev->use_stx);
300                         if (!link_support) {
301                                 pr_warn("Out of memory\n");
302                                 break;
303                         }
304                 }
305                 caif_enroll_dev(dev, caifdev, link_support, head_room,
306                                 &layer, NULL);
307                 caifdev->flowctrl = dev_flowctrl;
308                 break;
309
310         case NETDEV_UP:
311                 rcu_read_lock();
312
313                 caifd = caif_get(dev);
314                 if (caifd == NULL) {
315                         rcu_read_unlock();
316                         break;
317                 }
318
319                 cfcnfg_set_phy_state(cfg, &caifd->layer, true);
320                 rcu_read_unlock();
321
322                 break;
323
324         case NETDEV_DOWN:
325                 rcu_read_lock();
326
327                 caifd = caif_get(dev);
328                 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
329                         rcu_read_unlock();
330                         return -EINVAL;
331                 }
332
333                 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
334                 caifd_hold(caifd);
335                 rcu_read_unlock();
336
337                 caifd->layer.up->ctrlcmd(caifd->layer.up,
338                                          _CAIF_CTRLCMD_PHYIF_DOWN_IND,
339                                          caifd->layer.id);
340                 caifd_put(caifd);
341                 break;
342
343         case NETDEV_UNREGISTER:
344                 mutex_lock(&caifdevs->lock);
345
346                 caifd = caif_get(dev);
347                 if (caifd == NULL) {
348                         mutex_unlock(&caifdevs->lock);
349                         break;
350                 }
351                 list_del_rcu(&caifd->list);
352
353                 /*
354                  * NETDEV_UNREGISTER is called repeatedly until all reference
355                  * counts for the net-device are released. If references to
356                  * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
357                  * the next call to NETDEV_UNREGISTER.
358                  *
359                  * If any packets are in flight down the CAIF Stack,
360                  * cfcnfg_del_phy_layer will return nonzero.
361                  * If no packets are in flight, the CAIF Stack associated
362                  * with the net-device un-registering is freed.
363                  */
364
365                 if (caifd_refcnt_read(caifd) != 0 ||
366                         cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
367
368                         pr_info("Wait for device inuse\n");
369                         /* Enrole device if CAIF Stack is still in use */
370                         list_add_rcu(&caifd->list, &caifdevs->list);
371                         mutex_unlock(&caifdevs->lock);
372                         break;
373                 }
374
375                 synchronize_rcu();
376                 dev_put(caifd->netdev);
377                 free_percpu(caifd->pcpu_refcnt);
378                 kfree(caifd);
379
380                 mutex_unlock(&caifdevs->lock);
381                 break;
382         }
383         return 0;
384 }
385
386 static struct notifier_block caif_device_notifier = {
387         .notifier_call = caif_device_notify,
388         .priority = 0,
389 };
390
391 /* Per-namespace Caif devices handling */
392 static int caif_init_net(struct net *net)
393 {
394         struct caif_net *caifn = net_generic(net, caif_net_id);
395         BUG_ON(!caifn);
396         INIT_LIST_HEAD(&caifn->caifdevs.list);
397         mutex_init(&caifn->caifdevs.lock);
398
399         caifn->cfg = cfcnfg_create();
400         if (!caifn->cfg) {
401                 pr_warn("can't create cfcnfg\n");
402                 return -ENOMEM;
403         }
404
405         return 0;
406 }
407
408 static void caif_exit_net(struct net *net)
409 {
410         struct caif_device_entry *caifd, *tmp;
411         struct caif_device_entry_list *caifdevs =
412             caif_device_list(net);
413         struct cfcnfg *cfg =  get_cfcnfg(net);
414
415         if (!cfg || !caifdevs)
416                 return;
417
418         rtnl_lock();
419         mutex_lock(&caifdevs->lock);
420
421         list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
422                 int i = 0;
423                 list_del_rcu(&caifd->list);
424                 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
425
426                 while (i < 10 &&
427                         (caifd_refcnt_read(caifd) != 0 ||
428                         cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
429
430                         pr_info("Wait for device inuse\n");
431                         msleep(250);
432                         i++;
433                 }
434                 synchronize_rcu();
435                 dev_put(caifd->netdev);
436                 free_percpu(caifd->pcpu_refcnt);
437                 kfree(caifd);
438         }
439         cfcnfg_remove(cfg);
440
441         mutex_unlock(&caifdevs->lock);
442         rtnl_unlock();
443 }
444
445 static struct pernet_operations caif_net_ops = {
446         .init = caif_init_net,
447         .exit = caif_exit_net,
448         .id   = &caif_net_id,
449         .size = sizeof(struct caif_net),
450 };
451
452 /* Initialize Caif devices list */
453 static int __init caif_device_init(void)
454 {
455         int result;
456
457         result = register_pernet_device(&caif_net_ops);
458
459         if (result)
460                 return result;
461
462         register_netdevice_notifier(&caif_device_notifier);
463         dev_add_pack(&caif_packet_type);
464
465         return result;
466 }
467
468 static void __exit caif_device_exit(void)
469 {
470         unregister_pernet_device(&caif_net_ops);
471         unregister_netdevice_notifier(&caif_device_notifier);
472         dev_remove_pack(&caif_packet_type);
473 }
474
475 module_init(caif_device_init);
476 module_exit(caif_device_exit);