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