1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4source "../../../../net/forwarding/devlink_lib.sh"
5source ../mlxsw_lib.sh
6
7mlxsw_only_on_spectrum 1 || exit 1
8
9# Needed for returning to default
10declare -A KVD_DEFAULTS
11
12KVD_CHILDREN="linear hash_single hash_double"
13KVDL_CHILDREN="singles chunks large_chunks"
14
15devlink_sp_resource_minimize()
16{
17	local size
18	local i
19
20	for i in $KVD_CHILDREN; do
21		size=$(devlink_resource_get kvd "$i" | jq '.["size_min"]')
22		devlink_resource_size_set "$size" kvd "$i"
23	done
24
25	for i in $KVDL_CHILDREN; do
26		size=$(devlink_resource_get kvd linear "$i" | \
27		       jq '.["size_min"]')
28		devlink_resource_size_set "$size" kvd linear "$i"
29	done
30}
31
32devlink_sp_size_kvd_to_default()
33{
34	local need_reload=0
35	local i
36
37	for i in $KVD_CHILDREN; do
38		local size=$(echo "${KVD_DEFAULTS[kvd_$i]}" | jq '.["size"]')
39		current_size=$(devlink_resource_size_get kvd "$i")
40
41		if [ "$size" -ne "$current_size" ]; then
42			devlink_resource_size_set "$size" kvd "$i"
43			need_reload=1
44		fi
45	done
46
47	for i in $KVDL_CHILDREN; do
48		local size=$(echo "${KVD_DEFAULTS[kvd_linear_$i]}" | \
49			     jq '.["size"]')
50		current_size=$(devlink_resource_size_get kvd linear "$i")
51
52		if [ "$size" -ne "$current_size" ]; then
53			devlink_resource_size_set "$size" kvd linear "$i"
54			need_reload=1
55		fi
56	done
57
58	if [ "$need_reload" -ne "0" ]; then
59		devlink_reload
60	fi
61}
62
63devlink_sp_read_kvd_defaults()
64{
65	local key
66	local i
67
68	KVD_DEFAULTS[kvd]=$(devlink_resource_get "kvd")
69	for i in $KVD_CHILDREN; do
70		key=kvd_$i
71		KVD_DEFAULTS[$key]=$(devlink_resource_get kvd "$i")
72	done
73
74	for i in $KVDL_CHILDREN; do
75		key=kvd_linear_$i
76		KVD_DEFAULTS[$key]=$(devlink_resource_get kvd linear "$i")
77	done
78}
79
80KVD_PROFILES="default scale ipv4_max"
81
82devlink_sp_resource_kvd_profile_set()
83{
84	local profile=$1
85
86	case "$profile" in
87	scale)
88		devlink_resource_size_set 64000 kvd linear
89		devlink_resource_size_set 15616 kvd linear singles
90		devlink_resource_size_set 32000 kvd linear chunks
91		devlink_resource_size_set 16384 kvd linear large_chunks
92		devlink_resource_size_set 128000 kvd hash_single
93		devlink_resource_size_set 48000 kvd hash_double
94		devlink_reload
95		;;
96	ipv4_max)
97		devlink_resource_size_set 64000 kvd linear
98		devlink_resource_size_set 15616 kvd linear singles
99		devlink_resource_size_set 32000 kvd linear chunks
100		devlink_resource_size_set 16384 kvd linear large_chunks
101		devlink_resource_size_set 144000 kvd hash_single
102		devlink_resource_size_set 32768 kvd hash_double
103		devlink_reload
104		;;
105	default)
106		devlink_resource_size_set 98304 kvd linear
107		devlink_resource_size_set 16384 kvd linear singles
108		devlink_resource_size_set 49152 kvd linear chunks
109		devlink_resource_size_set 32768 kvd linear large_chunks
110		devlink_resource_size_set 87040 kvd hash_single
111		devlink_resource_size_set 60416 kvd hash_double
112		devlink_reload
113		;;
114	*)
115		check_err 1 "Unknown profile $profile"
116	esac
117}
118