hash.c (9453B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: hash.c 8 * Purpose: Load balance hash flex bin selection APIs. 9 * This module provides add/get/traverse/delete APIs for the flex hash bin 10 * selection operation. The add API creates a hardware entry which consists of 11 * key part and policy part. The key part is to specify rules to match 12 * specific packets. The policy part selects matched packet's fields and switch 13 * objects for hash bins. User can specify the entry ID with option 14 * BCM_HASH_FLEX_BIN_OPTION_WITH_ID. Higher entry_id has high priority during 15 * hardware lookup in case of multiple entries are matched. If entry_id is not 16 * specified, then a lowest available entry_id is automatically returned. 17 * The hash bin is 16 bit wide. With option BCM_HASH_FLEX_BIN_OPTION_SIZE_8BIT, 18 * user can populate the lower half and upper half of the bin independently. 19 * In this case, the bin ID BCM_HASH_BIN_A_0 indicates lower half of the 20 * BIN_A_0 while BCM_HASH_BIN_A_0_UPPER_HALF indicates the upper half. Otherwise 21 * BCM_HASH_BIN_A_0 indicates the entire 16-bit BIN_A_0. 22 */ 23 24 #include <soc/drv.h> 25 #include <soc/scache.h> 26 27 #include <shared/bsl.h> 28 #include <assert.h> 29 #include <bcm/types.h> 30 #include <bcm/error.h> 31 #include <soc/drv.h> 32 #include <soc/format.h> 33 #include <soc/esw/cancun.h> 34 #include <bcm_int/esw/switch.h> 35 #include <bcm/hash.h> 36 #include <bcm_int/esw_dispatch.h> 37 #include <bcm_int/esw/hash.h> 38 39 bcmi_hash_flex_bin_info_t bcmi_hash_flex_bin_info[BCM_MAX_NUM_UNITS] = { {0} }; 40 41 42 /* 43 * Function: 44 * bcm_esw_hash_init 45 * Purpose: 46 * Initialize the hash flex bin module. 47 * 48 * Parameters: 49 * unit - (IN) Unit ID. 50 */ 51 52 int bcm_esw_hash_init (int unit) 53 { 54 int rv = BCM_E_UNAVAIL; 55 56 #if defined(BCM_TRIDENT3_SUPPORT) 57 if (soc_feature(unit,soc_feature_hash_flex_bin)) { 58 soc_mem_lock(unit, FLEX_RTAG7_HASH_TCAMm); 59 rv = bcmi_td3_hash_flex_bin_init(unit); 60 soc_mem_unlock(unit, FLEX_RTAG7_HASH_TCAMm); 61 } 62 #endif /* defined(BCM_TRIDENT3_SUPPORT) */ 63 64 return rv; 65 } 66 /* 67 * Function: 68 * bcm_esw_hash_flex_bin_add 69 * Purpose: 70 * Specify the flex bin selection rule and write to the TCAM entry. Higher 71 * entry_id has high priority during TCAM lookup in case of multiple rules 72 * are matched. If entry_id is not specified, i.e. option 73 * BCM_HASH_FLEX_BIN_OPTION_WITH_ID is not given, then a lowest available 74 * entry_id is automatically assigned. 75 * 76 * Parameters: 77 * unit - (IN) Unit ID. 78 * cfg - (INOUT) Key part of selection rule and other parameters 79 * num_bins - (IN) Number of bin cmds. 80 * bin_cmd - (IN) Array of bin commands. Policy data part of rule. 81 * num_fields - (IN) Number of flex fields. 82 * flex_field - (IN) Array of flex fields. 83 */ 84 85 int bcm_esw_hash_flex_bin_add (int unit, 86 bcm_hash_flex_bin_config_t *cfg, 87 int num_bins, 88 bcm_hash_flex_bin_cmd_t *bin_cmd, 89 int num_fields, 90 bcm_hash_flex_field_t *flex_field) 91 { 92 int rv = BCM_E_UNAVAIL; 93 94 #if defined(BCM_TRIDENT3_SUPPORT) 95 if (soc_feature(unit,soc_feature_hash_flex_bin)) { 96 soc_mem_lock(unit, FLEX_RTAG7_HASH_TCAMm); 97 rv = bcmi_td3_hash_flex_bin_add (unit, cfg, num_bins, bin_cmd, 98 num_fields, flex_field); 99 soc_mem_unlock(unit, FLEX_RTAG7_HASH_TCAMm); 100 } 101 #endif /* defined(BCM_TRIDENT3_SUPPORT) */ 102 103 return rv; 104 } 105 106 /* 107 * Function: 108 * bcm_esw_hash_flex_bin_delete 109 * Purpose: 110 * Delete the given entry 111 * Parameters: 112 * unit - (IN) Unit ID. 113 * entry_id - (IN) The entry ID 114 */ 115 116 int bcm_esw_hash_flex_bin_delete( int unit, int entry_id) 117 { 118 int rv = BCM_E_UNAVAIL; 119 120 #if defined(BCM_TRIDENT3_SUPPORT) 121 if (soc_feature(unit, soc_feature_hash_flex_bin)) { 122 soc_mem_lock(unit, FLEX_RTAG7_HASH_TCAMm); 123 rv = bcmi_td3_hash_flex_bin_delete(unit,entry_id); 124 soc_mem_unlock(unit, FLEX_RTAG7_HASH_TCAMm); 125 } 126 #endif /* defined(BCM_TRIDENT3_SUPPORT) */ 127 128 return rv; 129 } 130 131 /* 132 * Function: 133 * bcm_esw_hash_flex_bin_delete_all 134 * Purpose: 135 * Delete all entries 136 * Parameters: 137 * unit - (IN) Unit ID. 138 */ 139 140 int bcm_esw_hash_flex_bin_delete_all( int unit) 141 { 142 int rv = BCM_E_UNAVAIL; 143 144 #if defined(BCM_TRIDENT3_SUPPORT) 145 if (soc_feature(unit, soc_feature_hash_flex_bin)) { 146 soc_mem_lock(unit, FLEX_RTAG7_HASH_TCAMm); 147 rv = bcmi_td3_hash_flex_bin_delete_all(unit); 148 soc_mem_unlock(unit, FLEX_RTAG7_HASH_TCAMm); 149 } 150 #endif /* defined(BCM_TRIDENT3_SUPPORT) */ 151 152 return rv; 153 } 154 155 /* 156 * Function: 157 * bcm_esw_hash_flex_bin_get 158 * Purpose: 159 * Get the entry configuration parameters for the given entry_id. 160 * Parameters: 161 * unit - (IN) Unit ID. 162 * cfg - (INOUT) Key part of selection rule and other parameters 163 * num_bins - (IN) Number of bin cmds. 164 * bin_cmd - (IN) Array of bin commands. Policy part of selection rule. 165 * num_fields - (IN) Number of flex fields. 166 * flex_field - (IN) Array of flex fields. 167 */ 168 169 int bcm_esw_hash_flex_bin_get( 170 int unit, 171 bcm_hash_flex_bin_config_t *cfg, 172 int num_bins, 173 bcm_hash_flex_bin_cmd_t *bin_cmd, 174 int num_fields, 175 bcm_hash_flex_field_t *flex_field) 176 { 177 int rv = BCM_E_UNAVAIL; 178 179 #if defined(BCM_TRIDENT3_SUPPORT) 180 if (soc_feature(unit,soc_feature_hash_flex_bin)) { 181 rv = bcmi_td3_hash_flex_bin_get(unit, cfg, num_bins, bin_cmd, 182 num_fields, flex_field); 183 } 184 #endif /* defined(BCM_TRIDENT3_SUPPORT) */ 185 186 return rv; 187 } 188 189 /* 190 * Function: 191 * bcm_esw_hash_flex_bin_traverse 192 * Purpose: 193 * Traverse all added entries and invoke the callback routine on 194 * each entry. 195 * 196 * Parameters: 197 * unit - (IN) Unit ID. 198 * option - (IN) bin_cmd for 8bit data or 16bit. Default 16bit. 199 * BCM_HASH_FLEX_BIN_OPTION_SIZE_8BIT: 8bit 200 * cb - (IN) callback function 201 * user_data - (IN) User context data 202 */ 203 204 int bcm_esw_hash_flex_bin_traverse( 205 int unit, 206 uint32 option, 207 bcm_hash_flex_bin_traverse_cb cb, 208 void *user_data) 209 { 210 int rv = BCM_E_UNAVAIL; 211 212 #if defined(BCM_TRIDENT3_SUPPORT) 213 if (soc_feature(unit,soc_feature_hash_flex_bin)) { 214 rv = bcmi_td3_hash_flex_bin_traverse(unit, option, cb, user_data); 215 } 216 #endif /* defined(BCM_TRIDENT3_SUPPORT) */ 217 218 return rv; 219 } 220 221 /* 222 * Function: 223 * bcm_esw_hash_flex_field_id_get 224 * Description: 225 * Get the field_id from the flex object's field name. In Trident3, 226 * the flex objects are defined in the ceh.yml file. Each object defines a 227 * list of flex fields. For example: 228 * Flex object: Z1_MATCH_ID 229 * Field: ETAG 230 * The ceh.yml file is packaged and loaded in the runtime. 231 * 232 * API call: 233 * uint32 field_id; 234 * bcm_hash_flex_field_id_get(unit, BCM_HASH_FLEX_OBJ_MATCH_ID_ZONE1, 235 * "ETAG", 236 * &field_id); 237 * 238 * Parameters: 239 * Unit (IN) Unit number 240 * object (IN) Flex object id 241 * field_name (IN) Field name 242 * field_id (OUT) field ID 243 * 244 * Return Value: 245 * BCM_E_XXX 246 * Notes: 247 * This API is used for bcm_hash_flex_bin_add API's flex field. 248 */ 249 250 int bcm_esw_hash_flex_field_id_get( 251 int unit, 252 bcm_hash_flex_object_t object, 253 const char *field_name, 254 uint32 *field_id) 255 { 256 int rv = BCM_E_UNAVAIL; 257 258 #if defined(BCM_TRIDENT3_SUPPORT) 259 if (soc_feature(unit,soc_feature_hash_flex_bin)) { 260 rv = bcmi_td3_hash_flex_field_id_get(unit, object, field_name, 261 field_id); 262 } 263 #endif /* defined(BCM_TRIDENT3_SUPPORT) */ 264 265 return rv; 266 } 267 268 /* 269 * Function: 270 * bcm_esw_hash_flex_field_name_get 271 * Description: 272 * Get the flex object ID and field name from the field_id. In Trident3, 273 * the flex objects are defined in the ceh.yml file. Each object defines a 274 * list of flex fields. For example: 275 * Flex object: Z1_MATCH_ID 276 * Field: ETAG 277 * The ceh.yml file is packaged and loaded in the runtime. 278 * 279 * API call: 280 * bcm_hash_flex_object_t object; 281 * char field_name[30]; 282 * bcm_hash_flex_field_name_get(unit, field_id, &object, 30, field_name); 283 * 284 * Parameters: 285 * unit (IN) Unit number 286 * field_id (IN) flex field ID 287 * object (OUT) Flex object id 288 * size (IN) size of the field name buffer 289 * field_name (OUT) Field name buffer 290 * 291 * Return Value: 292 * BCM_E_XXX 293 * Notes: 294 * This API is used for bcm_hash_flex_bin_get API's flex field. 295 */ 296 297 int bcm_esw_hash_flex_field_name_get ( 298 int unit, 299 uint32 field_id, 300 bcm_hash_flex_object_t *object, 301 int size, 302 char *field_name) 303 { 304 int rv = BCM_E_UNAVAIL; 305 306 #if defined(BCM_TRIDENT3_SUPPORT) 307 if (soc_feature(unit,soc_feature_hash_flex_bin)) { 308 int local_size; 309 310 local_size = size; 311 rv = bcmi_td3_hash_flex_field_name_get(unit,field_id, object, 312 &local_size, field_name); 313 } 314 #endif /* defined(BCM_TRIDENT3_SUPPORT) */ 315 316 return rv; 317 } 318 319 #ifdef BCM_WARM_BOOT_SUPPORT 320 int _bcm_esw_hash_sync(int unit) 321 { 322 int rv = BCM_E_UNAVAIL; 323 324 #if defined(BCM_TRIDENT3_SUPPORT) 325 if (soc_feature(unit,soc_feature_hash_flex_bin)) { 326 rv = bcmi_td3_hash_flex_bin_sync(unit); 327 } 328 #endif /* defined(BCM_TRIDENT3_SUPPORT) */ 329 330 return rv; 331 } 332 #endif /* BCM_WARM_BOOT_SUPPORT */