ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / atm / atm_misc.c
1 /* net/atm/atm_misc.c - Various functions for use by ATM drivers */
2
3 /* Written 1995-2000 by Werner Almesberger, EPFL ICA */
4
5
6 #include <linux/module.h>
7 #include <linux/atm.h>
8 #include <linux/atmdev.h>
9 #include <linux/skbuff.h>
10 #include <linux/sonet.h>
11 #include <linux/bitops.h>
12 #include <asm/atomic.h>
13 #include <asm/errno.h>
14
15
16 int atm_charge(struct atm_vcc *vcc,int truesize)
17 {
18         atm_force_charge(vcc,truesize);
19         if (atomic_read(&vcc->sk->sk_rmem_alloc) <= vcc->sk->sk_rcvbuf)
20                 return 1;
21         atm_return(vcc,truesize);
22         atomic_inc(&vcc->stats->rx_drop);
23         return 0;
24 }
25
26
27 struct sk_buff *atm_alloc_charge(struct atm_vcc *vcc,int pdu_size,
28     int gfp_flags)
29 {
30         int guess = atm_guess_pdu2truesize(pdu_size);
31
32         atm_force_charge(vcc,guess);
33         if (atomic_read(&vcc->sk->sk_rmem_alloc) <= vcc->sk->sk_rcvbuf) {
34                 struct sk_buff *skb = alloc_skb(pdu_size,gfp_flags);
35
36                 if (skb) {
37                         atomic_add(skb->truesize-guess,
38                                    &vcc->sk->sk_rmem_alloc);
39                         return skb;
40                 }
41         }
42         atm_return(vcc,guess);
43         atomic_inc(&vcc->stats->rx_drop);
44         return NULL;
45 }
46
47
48 /*
49  * atm_pcr_goal returns the positive PCR if it should be rounded up, the
50  * negative PCR if it should be rounded down, and zero if the maximum available
51  * bandwidth should be used.
52  *
53  * The rules are as follows (* = maximum, - = absent (0), x = value "x",
54  * (x+ = x or next value above x, x- = x or next value below):
55  *
56  *      min max pcr     result          min max pcr     result
57  *      -   -   -       * (UBR only)    x   -   -       x+
58  *      -   -   *       *               x   -   *       *
59  *      -   -   z       z-              x   -   z       z-
60  *      -   *   -       *               x   *   -       x+
61  *      -   *   *       *               x   *   *       *
62  *      -   *   z       z-              x   *   z       z-
63  *      -   y   -       y-              x   y   -       x+
64  *      -   y   *       y-              x   y   *       y-
65  *      -   y   z       z-              x   y   z       z-
66  *
67  * All non-error cases can be converted with the following simple set of rules:
68  *
69  *   if pcr == z then z-
70  *   else if min == x && pcr == - then x+
71  *     else if max == y then y-
72  *       else *
73  */
74
75
76 int atm_pcr_goal(struct atm_trafprm *tp)
77 {
78         if (tp->pcr && tp->pcr != ATM_MAX_PCR) return -tp->pcr;
79         if (tp->min_pcr && !tp->pcr) return tp->min_pcr;
80         if (tp->max_pcr != ATM_MAX_PCR) return -tp->max_pcr;
81         return 0;
82 }
83
84
85 void sonet_copy_stats(struct k_sonet_stats *from,struct sonet_stats *to)
86 {
87 #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
88         __SONET_ITEMS
89 #undef __HANDLE_ITEM
90 }
91
92
93 void sonet_subtract_stats(struct k_sonet_stats *from,struct sonet_stats *to)
94 {
95 #define __HANDLE_ITEM(i) atomic_sub(to->i,&from->i)
96         __SONET_ITEMS
97 #undef __HANDLE_ITEM
98 }
99
100
101 EXPORT_SYMBOL(atm_charge);
102 EXPORT_SYMBOL(atm_alloc_charge);
103 EXPORT_SYMBOL(atm_pcr_goal);
104 EXPORT_SYMBOL(sonet_copy_stats);
105 EXPORT_SYMBOL(sonet_subtract_stats);