| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* net/atm/atm_misc.c - Various functions for use by ATM drivers */ | 
|  | 2 |  | 
|  | 3 | /* Written 1995-2000 by Werner Almesberger, EPFL ICA */ | 
|  | 4 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | #include <linux/module.h> | 
|  | 6 | #include <linux/atm.h> | 
|  | 7 | #include <linux/atmdev.h> | 
|  | 8 | #include <linux/skbuff.h> | 
|  | 9 | #include <linux/sonet.h> | 
|  | 10 | #include <linux/bitops.h> | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 11 | #include <linux/errno.h> | 
| Arun Sharma | 60063497 | 2011-07-26 16:09:06 -0700 | [diff] [blame] | 12 | #include <linux/atomic.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 |  | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 14 | int atm_charge(struct atm_vcc *vcc, int truesize) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | { | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 16 | atm_force_charge(vcc, truesize); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | if (atomic_read(&sk_atm(vcc)->sk_rmem_alloc) <= sk_atm(vcc)->sk_rcvbuf) | 
|  | 18 | return 1; | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 19 | atm_return(vcc, truesize); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | atomic_inc(&vcc->stats->rx_drop); | 
|  | 21 | return 0; | 
|  | 22 | } | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 23 | EXPORT_SYMBOL(atm_charge); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 |  | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 25 | struct sk_buff *atm_alloc_charge(struct atm_vcc *vcc, int pdu_size, | 
|  | 26 | gfp_t gfp_flags) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { | 
|  | 28 | struct sock *sk = sk_atm(vcc); | 
| chas williams - CONTRACTOR | 49f5ed4 | 2011-11-22 12:51:56 +0000 | [diff] [blame] | 29 | int guess = SKB_TRUESIZE(pdu_size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 |  | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 31 | atm_force_charge(vcc, guess); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) { | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 33 | struct sk_buff *skb = alloc_skb(pdu_size, gfp_flags); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 |  | 
|  | 35 | if (skb) { | 
|  | 36 | atomic_add(skb->truesize-guess, | 
|  | 37 | &sk->sk_rmem_alloc); | 
|  | 38 | return skb; | 
|  | 39 | } | 
|  | 40 | } | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 41 | atm_return(vcc, guess); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | atomic_inc(&vcc->stats->rx_drop); | 
|  | 43 | return NULL; | 
|  | 44 | } | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 45 | EXPORT_SYMBOL(atm_alloc_charge); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |  | 
| Mitchell Blank Jr | c219750 | 2005-11-29 16:13:55 -0800 | [diff] [blame] | 75 | int atm_pcr_goal(const struct atm_trafprm *tp) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | { | 
| Mitchell Blank Jr | c219750 | 2005-11-29 16:13:55 -0800 | [diff] [blame] | 77 | if (tp->pcr && tp->pcr != ATM_MAX_PCR) | 
|  | 78 | return -tp->pcr; | 
|  | 79 | if (tp->min_pcr && !tp->pcr) | 
|  | 80 | return tp->min_pcr; | 
|  | 81 | if (tp->max_pcr != ATM_MAX_PCR) | 
|  | 82 | return -tp->max_pcr; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return 0; | 
|  | 84 | } | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 85 | EXPORT_SYMBOL(atm_pcr_goal); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 |  | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 87 | void sonet_copy_stats(struct k_sonet_stats *from, struct sonet_stats *to) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | { | 
|  | 89 | #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i) | 
|  | 90 | __SONET_ITEMS | 
|  | 91 | #undef __HANDLE_ITEM | 
|  | 92 | } | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 93 | EXPORT_SYMBOL(sonet_copy_stats); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 |  | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 95 | void sonet_subtract_stats(struct k_sonet_stats *from, struct sonet_stats *to) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | { | 
| Joe Perches | 3356b4d | 2010-01-26 11:40:02 +0000 | [diff] [blame] | 97 | #define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | __SONET_ITEMS | 
|  | 99 | #undef __HANDLE_ITEM | 
|  | 100 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | EXPORT_SYMBOL(sonet_subtract_stats); |