Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-drm-fsl-dcu.git] / net / core / netpoll.c
1 /*
2  * Common framework for low-level network console, dump, and debugger code
3  *
4  * Sep 8 2003  Matt Mackall <mpm@selenic.com>
5  *
6  * based on the netconsole code from:
7  *
8  * Copyright (C) 2001  Ingo Molnar <mingo@redhat.com>
9  * Copyright (C) 2002  Red Hat, Inc.
10  */
11
12 #include <linux/smp_lock.h>
13 #include <linux/netdevice.h>
14 #include <linux/etherdevice.h>
15 #include <linux/string.h>
16 #include <linux/if_arp.h>
17 #include <linux/inetdevice.h>
18 #include <linux/inet.h>
19 #include <linux/interrupt.h>
20 #include <linux/netpoll.h>
21 #include <linux/sched.h>
22 #include <linux/delay.h>
23 #include <linux/rcupdate.h>
24 #include <linux/workqueue.h>
25 #include <net/tcp.h>
26 #include <net/udp.h>
27 #include <asm/unaligned.h>
28
29 /*
30  * We maintain a small pool of fully-sized skbs, to make sure the
31  * message gets out even in extreme OOM situations.
32  */
33
34 #define MAX_UDP_CHUNK 1460
35 #define MAX_SKBS 32
36 #define MAX_QUEUE_DEPTH (MAX_SKBS / 2)
37
38 static struct sk_buff_head skb_pool;
39
40 static atomic_t trapped;
41
42 #define USEC_PER_POLL   50
43 #define NETPOLL_RX_ENABLED  1
44 #define NETPOLL_RX_DROP     2
45
46 #define MAX_SKB_SIZE \
47                 (MAX_UDP_CHUNK + sizeof(struct udphdr) + \
48                                 sizeof(struct iphdr) + sizeof(struct ethhdr))
49
50 static void zap_completion_queue(void);
51 static void arp_reply(struct sk_buff *skb);
52
53 static void queue_process(struct work_struct *work)
54 {
55         struct netpoll_info *npinfo =
56                 container_of(work, struct netpoll_info, tx_work.work);
57         struct sk_buff *skb;
58
59         while ((skb = skb_dequeue(&npinfo->txq))) {
60                 struct net_device *dev = skb->dev;
61
62                 if (!netif_device_present(dev) || !netif_running(dev)) {
63                         __kfree_skb(skb);
64                         continue;
65                 }
66
67                 netif_tx_lock_bh(dev);
68                 if (netif_queue_stopped(dev) ||
69                     dev->hard_start_xmit(skb, dev) != NETDEV_TX_OK) {
70                         skb_queue_head(&npinfo->txq, skb);
71                         netif_tx_unlock_bh(dev);
72
73                         schedule_delayed_work(&npinfo->tx_work, HZ/10);
74                         return;
75                 }
76         }
77 }
78
79 static __sum16 checksum_udp(struct sk_buff *skb, struct udphdr *uh,
80                             unsigned short ulen, __be32 saddr, __be32 daddr)
81 {
82         __wsum psum;
83
84         if (uh->check == 0 || skb->ip_summed == CHECKSUM_UNNECESSARY)
85                 return 0;
86
87         psum = csum_tcpudp_nofold(saddr, daddr, ulen, IPPROTO_UDP, 0);
88
89         if (skb->ip_summed == CHECKSUM_COMPLETE &&
90             !csum_fold(csum_add(psum, skb->csum)))
91                 return 0;
92
93         skb->csum = psum;
94
95         return __skb_checksum_complete(skb);
96 }
97
98 /*
99  * Check whether delayed processing was scheduled for our NIC. If so,
100  * we attempt to grab the poll lock and use ->poll() to pump the card.
101  * If this fails, either we've recursed in ->poll() or it's already
102  * running on another CPU.
103  *
104  * Note: we don't mask interrupts with this lock because we're using
105  * trylock here and interrupts are already disabled in the softirq
106  * case. Further, we test the poll_owner to avoid recursion on UP
107  * systems where the lock doesn't exist.
108  *
109  * In cases where there is bi-directional communications, reading only
110  * one message at a time can lead to packets being dropped by the
111  * network adapter, forcing superfluous retries and possibly timeouts.
112  * Thus, we set our budget to greater than 1.
113  */
114 static void poll_napi(struct netpoll *np)
115 {
116         struct netpoll_info *npinfo = np->dev->npinfo;
117         int budget = 16;
118
119         if (test_bit(__LINK_STATE_RX_SCHED, &np->dev->state) &&
120             npinfo->poll_owner != smp_processor_id() &&
121             spin_trylock(&npinfo->poll_lock)) {
122                 npinfo->rx_flags |= NETPOLL_RX_DROP;
123                 atomic_inc(&trapped);
124
125                 np->dev->poll(np->dev, &budget);
126
127                 atomic_dec(&trapped);
128                 npinfo->rx_flags &= ~NETPOLL_RX_DROP;
129                 spin_unlock(&npinfo->poll_lock);
130         }
131 }
132
133 static void service_arp_queue(struct netpoll_info *npi)
134 {
135         struct sk_buff *skb;
136
137         if (unlikely(!npi))
138                 return;
139
140         skb = skb_dequeue(&npi->arp_tx);
141
142         while (skb != NULL) {
143                 arp_reply(skb);
144                 skb = skb_dequeue(&npi->arp_tx);
145         }
146 }
147
148 void netpoll_poll(struct netpoll *np)
149 {
150         if (!np->dev || !netif_running(np->dev) || !np->dev->poll_controller)
151                 return;
152
153         /* Process pending work on NIC */
154         np->dev->poll_controller(np->dev);
155         if (np->dev->poll)
156                 poll_napi(np);
157
158         service_arp_queue(np->dev->npinfo);
159
160         zap_completion_queue();
161 }
162
163 static void refill_skbs(void)
164 {
165         struct sk_buff *skb;
166         unsigned long flags;
167
168         spin_lock_irqsave(&skb_pool.lock, flags);
169         while (skb_pool.qlen < MAX_SKBS) {
170                 skb = alloc_skb(MAX_SKB_SIZE, GFP_ATOMIC);
171                 if (!skb)
172                         break;
173
174                 __skb_queue_tail(&skb_pool, skb);
175         }
176         spin_unlock_irqrestore(&skb_pool.lock, flags);
177 }
178
179 static void zap_completion_queue(void)
180 {
181         unsigned long flags;
182         struct softnet_data *sd = &get_cpu_var(softnet_data);
183
184         if (sd->completion_queue) {
185                 struct sk_buff *clist;
186
187                 local_irq_save(flags);
188                 clist = sd->completion_queue;
189                 sd->completion_queue = NULL;
190                 local_irq_restore(flags);
191
192                 while (clist != NULL) {
193                         struct sk_buff *skb = clist;
194                         clist = clist->next;
195                         if (skb->destructor)
196                                 dev_kfree_skb_any(skb); /* put this one back */
197                         else
198                                 __kfree_skb(skb);
199                 }
200         }
201
202         put_cpu_var(softnet_data);
203 }
204
205 static struct sk_buff *find_skb(struct netpoll *np, int len, int reserve)
206 {
207         int count = 0;
208         struct sk_buff *skb;
209
210         zap_completion_queue();
211         refill_skbs();
212 repeat:
213
214         skb = alloc_skb(len, GFP_ATOMIC);
215         if (!skb)
216                 skb = skb_dequeue(&skb_pool);
217
218         if (!skb) {
219                 if (++count < 10) {
220                         netpoll_poll(np);
221                         goto repeat;
222                 }
223                 return NULL;
224         }
225
226         atomic_set(&skb->users, 1);
227         skb_reserve(skb, reserve);
228         return skb;
229 }
230
231 static void netpoll_send_skb(struct netpoll *np, struct sk_buff *skb)
232 {
233         int status = NETDEV_TX_BUSY;
234         unsigned long tries;
235         struct net_device *dev = np->dev;
236         struct netpoll_info *npinfo = np->dev->npinfo;
237
238         if (!npinfo || !netif_running(dev) || !netif_device_present(dev)) {
239                 __kfree_skb(skb);
240                 return;
241         }
242
243         /* don't get messages out of order, and no recursion */
244         if (skb_queue_len(&npinfo->txq) == 0 &&
245             npinfo->poll_owner != smp_processor_id() &&
246             netif_tx_trylock(dev)) {
247                 /* try until next clock tick */
248                 for (tries = jiffies_to_usecs(1)/USEC_PER_POLL; tries > 0; --tries) {
249                         if (!netif_queue_stopped(dev))
250                                 status = dev->hard_start_xmit(skb, dev);
251
252                         if (status == NETDEV_TX_OK)
253                                 break;
254
255                         /* tickle device maybe there is some cleanup */
256                         netpoll_poll(np);
257
258                         udelay(USEC_PER_POLL);
259                 }
260                 netif_tx_unlock(dev);
261         }
262
263         if (status != NETDEV_TX_OK) {
264                 skb_queue_tail(&npinfo->txq, skb);
265                 schedule_delayed_work(&npinfo->tx_work,0);
266         }
267 }
268
269 void netpoll_send_udp(struct netpoll *np, const char *msg, int len)
270 {
271         int total_len, eth_len, ip_len, udp_len;
272         struct sk_buff *skb;
273         struct udphdr *udph;
274         struct iphdr *iph;
275         struct ethhdr *eth;
276
277         udp_len = len + sizeof(*udph);
278         ip_len = eth_len = udp_len + sizeof(*iph);
279         total_len = eth_len + ETH_HLEN + NET_IP_ALIGN;
280
281         skb = find_skb(np, total_len, total_len - len);
282         if (!skb)
283                 return;
284
285         memcpy(skb->data, msg, len);
286         skb->len += len;
287
288         skb->h.uh = udph = (struct udphdr *) skb_push(skb, sizeof(*udph));
289         udph->source = htons(np->local_port);
290         udph->dest = htons(np->remote_port);
291         udph->len = htons(udp_len);
292         udph->check = 0;
293         udph->check = csum_tcpudp_magic(htonl(np->local_ip),
294                                         htonl(np->remote_ip),
295                                         udp_len, IPPROTO_UDP,
296                                         csum_partial((unsigned char *)udph, udp_len, 0));
297         if (udph->check == 0)
298                 udph->check = CSUM_MANGLED_0;
299
300         skb->nh.iph = iph = (struct iphdr *)skb_push(skb, sizeof(*iph));
301
302         /* iph->version = 4; iph->ihl = 5; */
303         put_unaligned(0x45, (unsigned char *)iph);
304         iph->tos      = 0;
305         put_unaligned(htons(ip_len), &(iph->tot_len));
306         iph->id       = 0;
307         iph->frag_off = 0;
308         iph->ttl      = 64;
309         iph->protocol = IPPROTO_UDP;
310         iph->check    = 0;
311         put_unaligned(htonl(np->local_ip), &(iph->saddr));
312         put_unaligned(htonl(np->remote_ip), &(iph->daddr));
313         iph->check    = ip_fast_csum((unsigned char *)iph, iph->ihl);
314
315         eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
316         skb->mac.raw = skb->data;
317         skb->protocol = eth->h_proto = htons(ETH_P_IP);
318         memcpy(eth->h_source, np->local_mac, 6);
319         memcpy(eth->h_dest, np->remote_mac, 6);
320
321         skb->dev = np->dev;
322
323         netpoll_send_skb(np, skb);
324 }
325
326 static void arp_reply(struct sk_buff *skb)
327 {
328         struct netpoll_info *npinfo = skb->dev->npinfo;
329         struct arphdr *arp;
330         unsigned char *arp_ptr;
331         int size, type = ARPOP_REPLY, ptype = ETH_P_ARP;
332         __be32 sip, tip;
333         struct sk_buff *send_skb;
334         struct netpoll *np = NULL;
335
336         if (npinfo->rx_np && npinfo->rx_np->dev == skb->dev)
337                 np = npinfo->rx_np;
338         if (!np)
339                 return;
340
341         /* No arp on this interface */
342         if (skb->dev->flags & IFF_NOARP)
343                 return;
344
345         if (!pskb_may_pull(skb, (sizeof(struct arphdr) +
346                                  (2 * skb->dev->addr_len) +
347                                  (2 * sizeof(u32)))))
348                 return;
349
350         skb->h.raw = skb->nh.raw = skb->data;
351         arp = skb->nh.arph;
352
353         if ((arp->ar_hrd != htons(ARPHRD_ETHER) &&
354              arp->ar_hrd != htons(ARPHRD_IEEE802)) ||
355             arp->ar_pro != htons(ETH_P_IP) ||
356             arp->ar_op != htons(ARPOP_REQUEST))
357                 return;
358
359         arp_ptr = (unsigned char *)(arp+1) + skb->dev->addr_len;
360         memcpy(&sip, arp_ptr, 4);
361         arp_ptr += 4 + skb->dev->addr_len;
362         memcpy(&tip, arp_ptr, 4);
363
364         /* Should we ignore arp? */
365         if (tip != htonl(np->local_ip) || LOOPBACK(tip) || MULTICAST(tip))
366                 return;
367
368         size = sizeof(struct arphdr) + 2 * (skb->dev->addr_len + 4);
369         send_skb = find_skb(np, size + LL_RESERVED_SPACE(np->dev),
370                             LL_RESERVED_SPACE(np->dev));
371
372         if (!send_skb)
373                 return;
374
375         send_skb->nh.raw = send_skb->data;
376         arp = (struct arphdr *) skb_put(send_skb, size);
377         send_skb->dev = skb->dev;
378         send_skb->protocol = htons(ETH_P_ARP);
379
380         /* Fill the device header for the ARP frame */
381
382         if (np->dev->hard_header &&
383             np->dev->hard_header(send_skb, skb->dev, ptype,
384                                  np->remote_mac, np->local_mac,
385                                  send_skb->len) < 0) {
386                 kfree_skb(send_skb);
387                 return;
388         }
389
390         /*
391          * Fill out the arp protocol part.
392          *
393          * we only support ethernet device type,
394          * which (according to RFC 1390) should always equal 1 (Ethernet).
395          */
396
397         arp->ar_hrd = htons(np->dev->type);
398         arp->ar_pro = htons(ETH_P_IP);
399         arp->ar_hln = np->dev->addr_len;
400         arp->ar_pln = 4;
401         arp->ar_op = htons(type);
402
403         arp_ptr=(unsigned char *)(arp + 1);
404         memcpy(arp_ptr, np->dev->dev_addr, np->dev->addr_len);
405         arp_ptr += np->dev->addr_len;
406         memcpy(arp_ptr, &tip, 4);
407         arp_ptr += 4;
408         memcpy(arp_ptr, np->remote_mac, np->dev->addr_len);
409         arp_ptr += np->dev->addr_len;
410         memcpy(arp_ptr, &sip, 4);
411
412         netpoll_send_skb(np, send_skb);
413 }
414
415 int __netpoll_rx(struct sk_buff *skb)
416 {
417         int proto, len, ulen;
418         struct iphdr *iph;
419         struct udphdr *uh;
420         struct netpoll_info *npi = skb->dev->npinfo;
421         struct netpoll *np = npi->rx_np;
422
423         if (!np)
424                 goto out;
425         if (skb->dev->type != ARPHRD_ETHER)
426                 goto out;
427
428         /* check if netpoll clients need ARP */
429         if (skb->protocol == __constant_htons(ETH_P_ARP) &&
430             atomic_read(&trapped)) {
431                 skb_queue_tail(&npi->arp_tx, skb);
432                 return 1;
433         }
434
435         proto = ntohs(eth_hdr(skb)->h_proto);
436         if (proto != ETH_P_IP)
437                 goto out;
438         if (skb->pkt_type == PACKET_OTHERHOST)
439                 goto out;
440         if (skb_shared(skb))
441                 goto out;
442
443         iph = (struct iphdr *)skb->data;
444         if (!pskb_may_pull(skb, sizeof(struct iphdr)))
445                 goto out;
446         if (iph->ihl < 5 || iph->version != 4)
447                 goto out;
448         if (!pskb_may_pull(skb, iph->ihl*4))
449                 goto out;
450         if (ip_fast_csum((u8 *)iph, iph->ihl) != 0)
451                 goto out;
452
453         len = ntohs(iph->tot_len);
454         if (skb->len < len || len < iph->ihl*4)
455                 goto out;
456
457         if (iph->protocol != IPPROTO_UDP)
458                 goto out;
459
460         len -= iph->ihl*4;
461         uh = (struct udphdr *)(((char *)iph) + iph->ihl*4);
462         ulen = ntohs(uh->len);
463
464         if (ulen != len)
465                 goto out;
466         if (checksum_udp(skb, uh, ulen, iph->saddr, iph->daddr))
467                 goto out;
468         if (np->local_ip && np->local_ip != ntohl(iph->daddr))
469                 goto out;
470         if (np->remote_ip && np->remote_ip != ntohl(iph->saddr))
471                 goto out;
472         if (np->local_port && np->local_port != ntohs(uh->dest))
473                 goto out;
474
475         np->rx_hook(np, ntohs(uh->source),
476                     (char *)(uh+1),
477                     ulen - sizeof(struct udphdr));
478
479         kfree_skb(skb);
480         return 1;
481
482 out:
483         if (atomic_read(&trapped)) {
484                 kfree_skb(skb);
485                 return 1;
486         }
487
488         return 0;
489 }
490
491 int netpoll_parse_options(struct netpoll *np, char *opt)
492 {
493         char *cur=opt, *delim;
494
495         if (*cur != '@') {
496                 if ((delim = strchr(cur, '@')) == NULL)
497                         goto parse_failed;
498                 *delim = 0;
499                 np->local_port = simple_strtol(cur, NULL, 10);
500                 cur = delim;
501         }
502         cur++;
503         printk(KERN_INFO "%s: local port %d\n", np->name, np->local_port);
504
505         if (*cur != '/') {
506                 if ((delim = strchr(cur, '/')) == NULL)
507                         goto parse_failed;
508                 *delim = 0;
509                 np->local_ip = ntohl(in_aton(cur));
510                 cur = delim;
511
512                 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
513                        np->name, HIPQUAD(np->local_ip));
514         }
515         cur++;
516
517         if (*cur != ',') {
518                 /* parse out dev name */
519                 if ((delim = strchr(cur, ',')) == NULL)
520                         goto parse_failed;
521                 *delim = 0;
522                 strlcpy(np->dev_name, cur, sizeof(np->dev_name));
523                 cur = delim;
524         }
525         cur++;
526
527         printk(KERN_INFO "%s: interface %s\n", np->name, np->dev_name);
528
529         if (*cur != '@') {
530                 /* dst port */
531                 if ((delim = strchr(cur, '@')) == NULL)
532                         goto parse_failed;
533                 *delim = 0;
534                 np->remote_port = simple_strtol(cur, NULL, 10);
535                 cur = delim;
536         }
537         cur++;
538         printk(KERN_INFO "%s: remote port %d\n", np->name, np->remote_port);
539
540         /* dst ip */
541         if ((delim = strchr(cur, '/')) == NULL)
542                 goto parse_failed;
543         *delim = 0;
544         np->remote_ip = ntohl(in_aton(cur));
545         cur = delim + 1;
546
547         printk(KERN_INFO "%s: remote IP %d.%d.%d.%d\n",
548                np->name, HIPQUAD(np->remote_ip));
549
550         if (*cur != 0) {
551                 /* MAC address */
552                 if ((delim = strchr(cur, ':')) == NULL)
553                         goto parse_failed;
554                 *delim = 0;
555                 np->remote_mac[0] = simple_strtol(cur, NULL, 16);
556                 cur = delim + 1;
557                 if ((delim = strchr(cur, ':')) == NULL)
558                         goto parse_failed;
559                 *delim = 0;
560                 np->remote_mac[1] = simple_strtol(cur, NULL, 16);
561                 cur = delim + 1;
562                 if ((delim = strchr(cur, ':')) == NULL)
563                         goto parse_failed;
564                 *delim = 0;
565                 np->remote_mac[2] = simple_strtol(cur, NULL, 16);
566                 cur = delim + 1;
567                 if ((delim = strchr(cur, ':')) == NULL)
568                         goto parse_failed;
569                 *delim = 0;
570                 np->remote_mac[3] = simple_strtol(cur, NULL, 16);
571                 cur = delim + 1;
572                 if ((delim = strchr(cur, ':')) == NULL)
573                         goto parse_failed;
574                 *delim = 0;
575                 np->remote_mac[4] = simple_strtol(cur, NULL, 16);
576                 cur = delim + 1;
577                 np->remote_mac[5] = simple_strtol(cur, NULL, 16);
578         }
579
580         printk(KERN_INFO "%s: remote ethernet address "
581                "%02x:%02x:%02x:%02x:%02x:%02x\n",
582                np->name,
583                np->remote_mac[0],
584                np->remote_mac[1],
585                np->remote_mac[2],
586                np->remote_mac[3],
587                np->remote_mac[4],
588                np->remote_mac[5]);
589
590         return 0;
591
592  parse_failed:
593         printk(KERN_INFO "%s: couldn't parse config at %s!\n",
594                np->name, cur);
595         return -1;
596 }
597
598 int netpoll_setup(struct netpoll *np)
599 {
600         struct net_device *ndev = NULL;
601         struct in_device *in_dev;
602         struct netpoll_info *npinfo;
603         unsigned long flags;
604         int err;
605
606         if (np->dev_name)
607                 ndev = dev_get_by_name(np->dev_name);
608         if (!ndev) {
609                 printk(KERN_ERR "%s: %s doesn't exist, aborting.\n",
610                        np->name, np->dev_name);
611                 return -ENODEV;
612         }
613
614         np->dev = ndev;
615         if (!ndev->npinfo) {
616                 npinfo = kmalloc(sizeof(*npinfo), GFP_KERNEL);
617                 if (!npinfo) {
618                         err = -ENOMEM;
619                         goto release;
620                 }
621
622                 npinfo->rx_flags = 0;
623                 npinfo->rx_np = NULL;
624                 spin_lock_init(&npinfo->poll_lock);
625                 npinfo->poll_owner = -1;
626
627                 spin_lock_init(&npinfo->rx_lock);
628                 skb_queue_head_init(&npinfo->arp_tx);
629                 skb_queue_head_init(&npinfo->txq);
630                 INIT_DELAYED_WORK(&npinfo->tx_work, queue_process);
631
632                 atomic_set(&npinfo->refcnt, 1);
633         } else {
634                 npinfo = ndev->npinfo;
635                 atomic_inc(&npinfo->refcnt);
636         }
637
638         if (!ndev->poll_controller) {
639                 printk(KERN_ERR "%s: %s doesn't support polling, aborting.\n",
640                        np->name, np->dev_name);
641                 err = -ENOTSUPP;
642                 goto release;
643         }
644
645         if (!netif_running(ndev)) {
646                 unsigned long atmost, atleast;
647
648                 printk(KERN_INFO "%s: device %s not up yet, forcing it\n",
649                        np->name, np->dev_name);
650
651                 rtnl_lock();
652                 err = dev_open(ndev);
653                 rtnl_unlock();
654
655                 if (err) {
656                         printk(KERN_ERR "%s: failed to open %s\n",
657                                np->name, ndev->name);
658                         goto release;
659                 }
660
661                 atleast = jiffies + HZ/10;
662                 atmost = jiffies + 4*HZ;
663                 while (!netif_carrier_ok(ndev)) {
664                         if (time_after(jiffies, atmost)) {
665                                 printk(KERN_NOTICE
666                                        "%s: timeout waiting for carrier\n",
667                                        np->name);
668                                 break;
669                         }
670                         cond_resched();
671                 }
672
673                 /* If carrier appears to come up instantly, we don't
674                  * trust it and pause so that we don't pump all our
675                  * queued console messages into the bitbucket.
676                  */
677
678                 if (time_before(jiffies, atleast)) {
679                         printk(KERN_NOTICE "%s: carrier detect appears"
680                                " untrustworthy, waiting 4 seconds\n",
681                                np->name);
682                         msleep(4000);
683                 }
684         }
685
686         if (is_zero_ether_addr(np->local_mac) && ndev->dev_addr)
687                 memcpy(np->local_mac, ndev->dev_addr, 6);
688
689         if (!np->local_ip) {
690                 rcu_read_lock();
691                 in_dev = __in_dev_get_rcu(ndev);
692
693                 if (!in_dev || !in_dev->ifa_list) {
694                         rcu_read_unlock();
695                         printk(KERN_ERR "%s: no IP address for %s, aborting\n",
696                                np->name, np->dev_name);
697                         err = -EDESTADDRREQ;
698                         goto release;
699                 }
700
701                 np->local_ip = ntohl(in_dev->ifa_list->ifa_local);
702                 rcu_read_unlock();
703                 printk(KERN_INFO "%s: local IP %d.%d.%d.%d\n",
704                        np->name, HIPQUAD(np->local_ip));
705         }
706
707         if (np->rx_hook) {
708                 spin_lock_irqsave(&npinfo->rx_lock, flags);
709                 npinfo->rx_flags |= NETPOLL_RX_ENABLED;
710                 npinfo->rx_np = np;
711                 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
712         }
713
714         /* fill up the skb queue */
715         refill_skbs();
716
717         /* last thing to do is link it to the net device structure */
718         ndev->npinfo = npinfo;
719
720         /* avoid racing with NAPI reading npinfo */
721         synchronize_rcu();
722
723         return 0;
724
725  release:
726         if (!ndev->npinfo)
727                 kfree(npinfo);
728         np->dev = NULL;
729         dev_put(ndev);
730         return err;
731 }
732
733 static int __init netpoll_init(void)
734 {
735         skb_queue_head_init(&skb_pool);
736         return 0;
737 }
738 core_initcall(netpoll_init);
739
740 void netpoll_cleanup(struct netpoll *np)
741 {
742         struct netpoll_info *npinfo;
743         unsigned long flags;
744
745         if (np->dev) {
746                 npinfo = np->dev->npinfo;
747                 if (npinfo) {
748                         if (npinfo->rx_np == np) {
749                                 spin_lock_irqsave(&npinfo->rx_lock, flags);
750                                 npinfo->rx_np = NULL;
751                                 npinfo->rx_flags &= ~NETPOLL_RX_ENABLED;
752                                 spin_unlock_irqrestore(&npinfo->rx_lock, flags);
753                         }
754
755                         np->dev->npinfo = NULL;
756                         if (atomic_dec_and_test(&npinfo->refcnt)) {
757                                 skb_queue_purge(&npinfo->arp_tx);
758                                 skb_queue_purge(&npinfo->txq);
759                                 cancel_rearming_delayed_work(&npinfo->tx_work);
760                                 flush_scheduled_work();
761
762                                 kfree(npinfo);
763                         }
764                 }
765
766                 dev_put(np->dev);
767         }
768
769         np->dev = NULL;
770 }
771
772 int netpoll_trap(void)
773 {
774         return atomic_read(&trapped);
775 }
776
777 void netpoll_set_trap(int trap)
778 {
779         if (trap)
780                 atomic_inc(&trapped);
781         else
782                 atomic_dec(&trapped);
783 }
784
785 EXPORT_SYMBOL(netpoll_set_trap);
786 EXPORT_SYMBOL(netpoll_trap);
787 EXPORT_SYMBOL(netpoll_parse_options);
788 EXPORT_SYMBOL(netpoll_setup);
789 EXPORT_SYMBOL(netpoll_cleanup);
790 EXPORT_SYMBOL(netpoll_send_udp);
791 EXPORT_SYMBOL(netpoll_poll);