USB: remove use of the bus rwsem, as it doesn't really protect anything.
[linux/fpc-iii.git] / net / core / link_watch.c
blobe3c26a9ccad6e097b098bc0783d9076c589199d9
1 /*
2 * Linux network device link state notification
4 * Author:
5 * Stefan Rompf <sux@loplof.de>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
16 #include <linux/if.h>
17 #include <net/sock.h>
18 #include <net/pkt_sched.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/jiffies.h>
21 #include <linux/spinlock.h>
22 #include <linux/list.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <linux/bitops.h>
26 #include <asm/types.h>
29 enum lw_bits {
30 LW_RUNNING = 0,
31 LW_SE_USED
34 static unsigned long linkwatch_flags;
35 static unsigned long linkwatch_nextevent;
37 static void linkwatch_event(struct work_struct *dummy);
38 static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
40 static LIST_HEAD(lweventlist);
41 static DEFINE_SPINLOCK(lweventlist_lock);
43 struct lw_event {
44 struct list_head list;
45 struct net_device *dev;
48 /* Avoid kmalloc() for most systems */
49 static struct lw_event singleevent;
51 static unsigned char default_operstate(const struct net_device *dev)
53 if (!netif_carrier_ok(dev))
54 return (dev->ifindex != dev->iflink ?
55 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
57 if (netif_dormant(dev))
58 return IF_OPER_DORMANT;
60 return IF_OPER_UP;
64 static void rfc2863_policy(struct net_device *dev)
66 unsigned char operstate = default_operstate(dev);
68 if (operstate == dev->operstate)
69 return;
71 write_lock_bh(&dev_base_lock);
73 switch(dev->link_mode) {
74 case IF_LINK_MODE_DORMANT:
75 if (operstate == IF_OPER_UP)
76 operstate = IF_OPER_DORMANT;
77 break;
79 case IF_LINK_MODE_DEFAULT:
80 default:
81 break;
84 dev->operstate = operstate;
86 write_unlock_bh(&dev_base_lock);
90 /* Must be called with the rtnl semaphore held */
91 void linkwatch_run_queue(void)
93 struct list_head head, *n, *next;
95 spin_lock_irq(&lweventlist_lock);
96 list_replace_init(&lweventlist, &head);
97 spin_unlock_irq(&lweventlist_lock);
99 list_for_each_safe(n, next, &head) {
100 struct lw_event *event = list_entry(n, struct lw_event, list);
101 struct net_device *dev = event->dev;
103 if (event == &singleevent) {
104 clear_bit(LW_SE_USED, &linkwatch_flags);
105 } else {
106 kfree(event);
109 /* We are about to handle this device,
110 * so new events can be accepted
112 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
114 rfc2863_policy(dev);
115 if (dev->flags & IFF_UP) {
116 if (netif_carrier_ok(dev)) {
117 WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
118 dev_activate(dev);
119 } else
120 dev_deactivate(dev);
122 netdev_state_change(dev);
125 dev_put(dev);
130 static void linkwatch_event(struct work_struct *dummy)
132 /* Limit the number of linkwatch events to one
133 * per second so that a runaway driver does not
134 * cause a storm of messages on the netlink
135 * socket
137 linkwatch_nextevent = jiffies + HZ;
138 clear_bit(LW_RUNNING, &linkwatch_flags);
140 rtnl_lock();
141 linkwatch_run_queue();
142 rtnl_unlock();
146 void linkwatch_fire_event(struct net_device *dev)
148 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
149 unsigned long flags;
150 struct lw_event *event;
152 if (test_and_set_bit(LW_SE_USED, &linkwatch_flags)) {
153 event = kmalloc(sizeof(struct lw_event), GFP_ATOMIC);
155 if (unlikely(event == NULL)) {
156 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
157 return;
159 } else {
160 event = &singleevent;
163 dev_hold(dev);
164 event->dev = dev;
166 spin_lock_irqsave(&lweventlist_lock, flags);
167 list_add_tail(&event->list, &lweventlist);
168 spin_unlock_irqrestore(&lweventlist_lock, flags);
170 if (!test_and_set_bit(LW_RUNNING, &linkwatch_flags)) {
171 unsigned long delay = linkwatch_nextevent - jiffies;
173 /* If we wrap around we'll delay it by at most HZ. */
174 if (delay > HZ)
175 delay = 0;
176 schedule_delayed_work(&linkwatch_work, delay);
181 EXPORT_SYMBOL(linkwatch_fire_event);