nfsd4: Remove check for a 32-bit cookie in nfsd4_readdir()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / caif / caif_dev.c
blobdbdaa95b80055fc0a2cb9d03d1e5aee9c66f4596
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
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/version.h>
15 #include <linux/kernel.h>
16 #include <linux/if_arp.h>
17 #include <linux/net.h>
18 #include <linux/netdevice.h>
19 #include <linux/mutex.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>
28 MODULE_LICENSE("GPL");
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;
38 struct caif_device_entry_list {
39 struct list_head list;
40 /* Protects simulanous deletes in list */
41 struct mutex lock;
44 struct caif_net {
45 struct cfcnfg *cfg;
46 struct caif_device_entry_list caifdevs;
49 static int caif_net_id;
51 struct cfcnfg *get_cfcnfg(struct net *net)
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;
59 EXPORT_SYMBOL(get_cfcnfg);
61 static struct caif_device_entry_list *caif_device_list(struct net *net)
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;
70 static void caifd_put(struct caif_device_entry *e)
72 irqsafe_cpu_dec(*e->pcpu_refcnt);
75 static void caifd_hold(struct caif_device_entry *e)
77 irqsafe_cpu_inc(*e->pcpu_refcnt);
80 static int caifd_refcnt_read(struct caif_device_entry *e)
82 int i, refcnt = 0;
83 for_each_possible_cpu(i)
84 refcnt += *per_cpu_ptr(e->pcpu_refcnt, i);
85 return refcnt;
88 /* Allocate new CAIF device. */
89 static struct caif_device_entry *caif_device_alloc(struct net_device *dev)
91 struct caif_device_entry_list *caifdevs;
92 struct caif_device_entry *caifd;
94 caifdevs = caif_device_list(dev_net(dev));
95 BUG_ON(!caifdevs);
97 caifd = kzalloc(sizeof(*caifd), GFP_ATOMIC);
98 if (!caifd)
99 return NULL;
100 caifd->pcpu_refcnt = alloc_percpu(int);
101 caifd->netdev = dev;
102 dev_hold(dev);
103 return caifd;
106 static struct caif_device_entry *caif_get(struct net_device *dev)
108 struct caif_device_entry_list *caifdevs =
109 caif_device_list(dev_net(dev));
110 struct caif_device_entry *caifd;
111 BUG_ON(!caifdevs);
112 list_for_each_entry_rcu(caifd, &caifdevs->list, list) {
113 if (caifd->netdev == dev)
114 return caifd;
116 return NULL;
119 static int transmit(struct cflayer *layer, struct cfpkt *pkt)
121 int err;
122 struct caif_device_entry *caifd =
123 container_of(layer, struct caif_device_entry, layer);
124 struct sk_buff *skb;
126 skb = cfpkt_tonative(pkt);
127 skb->dev = caifd->netdev;
129 err = dev_queue_xmit(skb);
130 if (err > 0)
131 err = -EIO;
133 return err;
137 * Stuff received packets into the CAIF stack.
138 * On error, returns non-zero and releases the skb.
140 static int receive(struct sk_buff *skb, struct net_device *dev,
141 struct packet_type *pkttype, struct net_device *orig_dev)
143 struct cfpkt *pkt;
144 struct caif_device_entry *caifd;
145 int err;
147 pkt = cfpkt_fromnative(CAIF_DIR_IN, skb);
149 rcu_read_lock();
150 caifd = caif_get(dev);
152 if (!caifd || !caifd->layer.up || !caifd->layer.up->receive ||
153 !netif_oper_up(caifd->netdev)) {
154 rcu_read_unlock();
155 kfree_skb(skb);
156 return NET_RX_DROP;
159 /* Hold reference to netdevice while using CAIF stack */
160 caifd_hold(caifd);
161 rcu_read_unlock();
163 err = caifd->layer.up->receive(caifd->layer.up, pkt);
165 /* For -EILSEQ the packet is not freed so so it now */
166 if (err == -EILSEQ)
167 cfpkt_destroy(pkt);
169 /* Release reference to stack upwards */
170 caifd_put(caifd);
171 return 0;
174 static struct packet_type caif_packet_type __read_mostly = {
175 .type = cpu_to_be16(ETH_P_CAIF),
176 .func = receive,
179 static void dev_flowctrl(struct net_device *dev, int on)
181 struct caif_device_entry *caifd;
183 rcu_read_lock();
185 caifd = caif_get(dev);
186 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
187 rcu_read_unlock();
188 return;
191 caifd_hold(caifd);
192 rcu_read_unlock();
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);
199 caifd_put(caifd);
202 /* notify Caif of device events */
203 static int caif_device_notify(struct notifier_block *me, unsigned long what,
204 void *arg)
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;
210 enum cfcnfg_phy_type phy_type;
211 struct cfcnfg *cfg;
212 struct caif_device_entry_list *caifdevs;
214 if (dev->type != ARPHRD_CAIF)
215 return 0;
217 cfg = get_cfcnfg(dev_net(dev));
218 if (cfg == NULL)
219 return 0;
221 caifdevs = caif_device_list(dev_net(dev));
223 switch (what) {
224 case NETDEV_REGISTER:
225 caifd = caif_device_alloc(dev);
226 if (!caifd)
227 return 0;
229 caifdev = netdev_priv(dev);
230 caifdev->flowctrl = dev_flowctrl;
232 caifd->layer.transmit = transmit;
234 if (caifdev->use_frag)
235 phy_type = CFPHYTYPE_FRAG;
236 else
237 phy_type = CFPHYTYPE_CAIF;
239 switch (caifdev->link_select) {
240 case CAIF_LINK_HIGH_BANDW:
241 pref = CFPHYPREF_HIGH_BW;
242 break;
243 case CAIF_LINK_LOW_LATENCY:
244 pref = CFPHYPREF_LOW_LAT;
245 break;
246 default:
247 pref = CFPHYPREF_HIGH_BW;
248 break;
250 strncpy(caifd->layer.name, dev->name,
251 sizeof(caifd->layer.name) - 1);
252 caifd->layer.name[sizeof(caifd->layer.name) - 1] = 0;
254 mutex_lock(&caifdevs->lock);
255 list_add_rcu(&caifd->list, &caifdevs->list);
257 cfcnfg_add_phy_layer(cfg,
258 phy_type,
259 dev,
260 &caifd->layer,
261 pref,
262 caifdev->use_fcs,
263 caifdev->use_stx);
264 mutex_unlock(&caifdevs->lock);
265 break;
267 case NETDEV_UP:
268 rcu_read_lock();
270 caifd = caif_get(dev);
271 if (caifd == NULL) {
272 rcu_read_unlock();
273 break;
276 cfcnfg_set_phy_state(cfg, &caifd->layer, true);
277 rcu_read_unlock();
279 break;
281 case NETDEV_DOWN:
282 rcu_read_lock();
284 caifd = caif_get(dev);
285 if (!caifd || !caifd->layer.up || !caifd->layer.up->ctrlcmd) {
286 rcu_read_unlock();
287 return -EINVAL;
290 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
291 caifd_hold(caifd);
292 rcu_read_unlock();
294 caifd->layer.up->ctrlcmd(caifd->layer.up,
295 _CAIF_CTRLCMD_PHYIF_DOWN_IND,
296 caifd->layer.id);
297 caifd_put(caifd);
298 break;
300 case NETDEV_UNREGISTER:
301 mutex_lock(&caifdevs->lock);
303 caifd = caif_get(dev);
304 if (caifd == NULL) {
305 mutex_unlock(&caifdevs->lock);
306 break;
308 list_del_rcu(&caifd->list);
311 * NETDEV_UNREGISTER is called repeatedly until all reference
312 * counts for the net-device are released. If references to
313 * caifd is taken, simply ignore NETDEV_UNREGISTER and wait for
314 * the next call to NETDEV_UNREGISTER.
316 * If any packets are in flight down the CAIF Stack,
317 * cfcnfg_del_phy_layer will return nonzero.
318 * If no packets are in flight, the CAIF Stack associated
319 * with the net-device un-registering is freed.
322 if (caifd_refcnt_read(caifd) != 0 ||
323 cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0) {
325 pr_info("Wait for device inuse\n");
326 /* Enrole device if CAIF Stack is still in use */
327 list_add_rcu(&caifd->list, &caifdevs->list);
328 mutex_unlock(&caifdevs->lock);
329 break;
332 synchronize_rcu();
333 dev_put(caifd->netdev);
334 free_percpu(caifd->pcpu_refcnt);
335 kfree(caifd);
337 mutex_unlock(&caifdevs->lock);
338 break;
340 return 0;
343 static struct notifier_block caif_device_notifier = {
344 .notifier_call = caif_device_notify,
345 .priority = 0,
348 /* Per-namespace Caif devices handling */
349 static int caif_init_net(struct net *net)
351 struct caif_net *caifn = net_generic(net, caif_net_id);
352 BUG_ON(!caifn);
353 INIT_LIST_HEAD(&caifn->caifdevs.list);
354 mutex_init(&caifn->caifdevs.lock);
356 caifn->cfg = cfcnfg_create();
357 if (!caifn->cfg) {
358 pr_warn("can't create cfcnfg\n");
359 return -ENOMEM;
362 return 0;
365 static void caif_exit_net(struct net *net)
367 struct caif_device_entry *caifd, *tmp;
368 struct caif_device_entry_list *caifdevs =
369 caif_device_list(net);
370 struct cfcnfg *cfg;
372 rtnl_lock();
373 mutex_lock(&caifdevs->lock);
375 cfg = get_cfcnfg(net);
376 if (cfg == NULL) {
377 mutex_unlock(&caifdevs->lock);
378 return;
381 list_for_each_entry_safe(caifd, tmp, &caifdevs->list, list) {
382 int i = 0;
383 list_del_rcu(&caifd->list);
384 cfcnfg_set_phy_state(cfg, &caifd->layer, false);
386 while (i < 10 &&
387 (caifd_refcnt_read(caifd) != 0 ||
388 cfcnfg_del_phy_layer(cfg, &caifd->layer) != 0)) {
390 pr_info("Wait for device inuse\n");
391 msleep(250);
392 i++;
394 synchronize_rcu();
395 dev_put(caifd->netdev);
396 free_percpu(caifd->pcpu_refcnt);
397 kfree(caifd);
399 cfcnfg_remove(cfg);
401 mutex_unlock(&caifdevs->lock);
402 rtnl_unlock();
405 static struct pernet_operations caif_net_ops = {
406 .init = caif_init_net,
407 .exit = caif_exit_net,
408 .id = &caif_net_id,
409 .size = sizeof(struct caif_net),
412 /* Initialize Caif devices list */
413 static int __init caif_device_init(void)
415 int result;
417 result = register_pernet_device(&caif_net_ops);
419 if (result)
420 return result;
422 register_netdevice_notifier(&caif_device_notifier);
423 dev_add_pack(&caif_packet_type);
425 return result;
428 static void __exit caif_device_exit(void)
430 unregister_pernet_device(&caif_net_ops);
431 unregister_netdevice_notifier(&caif_device_notifier);
432 dev_remove_pack(&caif_packet_type);
435 module_init(caif_device_init);
436 module_exit(caif_device_exit);