blob: a30b83c1cb3fc89ca7c2477fa45c0733a5f164c0 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/* net/atm/atm_misc.c - Various functions for use by ATM drivers */
3
4/* Written 1995-2000 by Werner Almesberger, EPFL ICA */
5
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#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>
Joe Perches3356b4d2010-01-26 11:40:02 +000012#include <linux/errno.h>
Arun Sharma600634972011-07-26 16:09:06 -070013#include <linux/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Joe Perches3356b4d2010-01-26 11:40:02 +000015int atm_charge(struct atm_vcc *vcc, int truesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -070016{
Joe Perches3356b4d2010-01-26 11:40:02 +000017 atm_force_charge(vcc, truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 if (atomic_read(&sk_atm(vcc)->sk_rmem_alloc) <= sk_atm(vcc)->sk_rcvbuf)
19 return 1;
Joe Perches3356b4d2010-01-26 11:40:02 +000020 atm_return(vcc, truesize);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 atomic_inc(&vcc->stats->rx_drop);
22 return 0;
23}
Joe Perches3356b4d2010-01-26 11:40:02 +000024EXPORT_SYMBOL(atm_charge);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Joe Perches3356b4d2010-01-26 11:40:02 +000026struct sk_buff *atm_alloc_charge(struct atm_vcc *vcc, int pdu_size,
27 gfp_t gfp_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
29 struct sock *sk = sk_atm(vcc);
chas williams - CONTRACTOR49f5ed42011-11-22 12:51:56 +000030 int guess = SKB_TRUESIZE(pdu_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Joe Perches3356b4d2010-01-26 11:40:02 +000032 atm_force_charge(vcc, guess);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf) {
Joe Perches3356b4d2010-01-26 11:40:02 +000034 struct sk_buff *skb = alloc_skb(pdu_size, gfp_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36 if (skb) {
37 atomic_add(skb->truesize-guess,
38 &sk->sk_rmem_alloc);
39 return skb;
40 }
41 }
Joe Perches3356b4d2010-01-26 11:40:02 +000042 atm_return(vcc, guess);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 atomic_inc(&vcc->stats->rx_drop);
44 return NULL;
45}
Joe Perches3356b4d2010-01-26 11:40:02 +000046EXPORT_SYMBOL(atm_alloc_charge);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
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
Mitchell Blank Jrc2197502005-11-29 16:13:55 -080076int atm_pcr_goal(const struct atm_trafprm *tp)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Mitchell Blank Jrc2197502005-11-29 16:13:55 -080078 if (tp->pcr && tp->pcr != ATM_MAX_PCR)
79 return -tp->pcr;
80 if (tp->min_pcr && !tp->pcr)
81 return tp->min_pcr;
82 if (tp->max_pcr != ATM_MAX_PCR)
83 return -tp->max_pcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 return 0;
85}
Joe Perches3356b4d2010-01-26 11:40:02 +000086EXPORT_SYMBOL(atm_pcr_goal);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Joe Perches3356b4d2010-01-26 11:40:02 +000088void sonet_copy_stats(struct k_sonet_stats *from, struct sonet_stats *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90#define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
91 __SONET_ITEMS
92#undef __HANDLE_ITEM
93}
Joe Perches3356b4d2010-01-26 11:40:02 +000094EXPORT_SYMBOL(sonet_copy_stats);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Joe Perches3356b4d2010-01-26 11:40:02 +000096void sonet_subtract_stats(struct k_sonet_stats *from, struct sonet_stats *to)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Joe Perches3356b4d2010-01-26 11:40:02 +000098#define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 __SONET_ITEMS
100#undef __HANDLE_ITEM
101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102EXPORT_SYMBOL(sonet_subtract_stats);