sfc: Clean up PHY mode management in loopback self-test
[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(&sk_atm(vcc)->sk_rmem_alloc) <= sk_atm(vcc)->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     gfp_t gfp_flags)
29 {
30         struct sock *sk = sk_atm(vcc);
31         int guess = atm_guess_pdu2truesize(pdu_size);
32
33         atm_force_charge(vcc,guess);
34         if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
35                 struct sk_buff *skb = alloc_skb(pdu_size,gfp_flags);
36
37                 if (skb) {
38                         atomic_add(skb->truesize-guess,
39                                    &sk->sk_rmem_alloc);
40                         return skb;
41                 }
42         }
43         atm_return(vcc,guess);
44         atomic_inc(&vcc->stats->rx_drop);
45         return NULL;
46 }
47
48
49 /*
50  * atm_pcr_goal returns the positive PCR if it should be rounded up, the
51  * negative PCR if it should be rounded down, and zero if the maximum available
52  * bandwidth should be used.
53  *
54  * The rules are as follows (* = maximum, - = absent (0), x = value "x",
55  * (x+ = x or next value above x, x- = x or next value below):
56  *
57  *      min max pcr     result          min max pcr     result
58  *      -   -   -       * (UBR only)    x   -   -       x+
59  *      -   -   *       *               x   -   *       *
60  *      -   -   z       z-              x   -   z       z-
61  *      -   *   -       *               x   *   -       x+
62  *      -   *   *       *               x   *   *       *
63  *      -   *   z       z-              x   *   z       z-
64  *      -   y   -       y-              x   y   -       x+
65  *      -   y   *       y-              x   y   *       y-
66  *      -   y   z       z-              x   y   z       z-
67  *
68  * All non-error cases can be converted with the following simple set of rules:
69  *
70  *   if pcr == z then z-
71  *   else if min == x && pcr == - then x+
72  *     else if max == y then y-
73  *       else *
74  */
75
76
77 int atm_pcr_goal(const struct atm_trafprm *tp)
78 {
79         if (tp->pcr && tp->pcr != ATM_MAX_PCR)
80                 return -tp->pcr;
81         if (tp->min_pcr && !tp->pcr)
82                 return tp->min_pcr;
83         if (tp->max_pcr != ATM_MAX_PCR)
84                 return -tp->max_pcr;
85         return 0;
86 }
87
88
89 void sonet_copy_stats(struct k_sonet_stats *from,struct sonet_stats *to)
90 {
91 #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
92         __SONET_ITEMS
93 #undef __HANDLE_ITEM
94 }
95
96
97 void sonet_subtract_stats(struct k_sonet_stats *from,struct sonet_stats *to)
98 {
99 #define __HANDLE_ITEM(i) atomic_sub(to->i,&from->i)
100         __SONET_ITEMS
101 #undef __HANDLE_ITEM
102 }
103
104
105 EXPORT_SYMBOL(atm_charge);
106 EXPORT_SYMBOL(atm_alloc_charge);
107 EXPORT_SYMBOL(atm_pcr_goal);
108 EXPORT_SYMBOL(sonet_copy_stats);
109 EXPORT_SYMBOL(sonet_subtract_stats);