blob: 6252a456b066536cf6701f2b33fe21ef11d484a4 [file] [log] [blame]
Shad Ansari2f7f9be2017-06-07 13:34:53 -07001/*
2<:copyright-BRCM:2016:DUAL/GPL:standard
3
4 Broadcom Proprietary and Confidential.(c) 2016 Broadcom
5 All Rights Reserved
6
7Unless you and Broadcom execute a separate written software license
8agreement governing use of this software, this software is licensed
9to you under the terms of the GNU General Public License version 2
10(the "GPL"), available at http://www.broadcom.com/licenses/GPLv2.php,
11with the following added to such license:
12
13 As a special exception, the copyright holders of this software give
14 you permission to link this software with independent modules, and
15 to copy and distribute the resulting executable under terms of your
16 choice, provided that you also meet, for each linked independent
17 module, the terms and conditions of the license of that module.
18 An independent module is a module which is not derived from this
19 software. The special exception does not apply to any modifications
20 of the software.
21
22Not withstanding the above, under no circumstances may you combine
23this software in any way with any other Broadcom software provided
24under a license other than the GPL, without Broadcom's express prior
25written consent.
26
27:>
28 */
29
30#ifndef _BCMOLT_MH_UTILS_H_
31#define _BCMOLT_MH_UTILS_H_
32
33#define MH_EXIT_IF(cond, error) \
34 do \
35 { \
36 if (cond) \
37 { \
38 if ((error) != BCM_ERR_OK && (error) != BCM_ERR_NO_MORE) \
39 { \
40 BCMOS_TRACE_INFO(#cond); \
41 } \
42 err = (error); \
43 goto cleanup; \
44 } \
45 } while (BCMOS_FALSE)
46
47/* Macro for declaring cfg_get_multi handlers, since the format is perfectly regular */
48#define MH_DECLARE_CFG_GET_MULTI(obj) \
49static bcmos_errno mh_##obj##_cfg_get_multi( \
50 const bcmolt_##obj##_cfg *filter, \
51 bcmolt_filter_flags flags, \
52 bcmolt_msg_set *msg_set) \
53{ \
54 bcmolt_##obj##_cfg cfg; \
55 uint8_t *filter_packed = NULL; \
56 uint8_t *msg_packed = NULL; \
57 bcmos_errno err; \
58 bcmos_bool success; \
59 uint32_t len; \
60 bcmolt_buf buf; \
61 bcmos_bool is_match; \
62 bcmolt_msg dummy_hdr; \
63 bcmolt_##obj##_key_id dummy_prop_id; \
64 \
65 /* assume we didn't finish all objects unless told otherwise */ \
66 msg_set->more = BCMOS_TRUE; \
67 \
68 /* start with the key included in the filter */ \
69 *((bcmolt_##obj##_key *)msg_set->next_key) = filter->key; \
70 \
71 /* if the filter's key contains any wildcards, get the first real key */ \
72 err = mh_##obj##_key_resolve_wildcards(msg_set->next_key); \
73 MH_EXIT_IF(err != BCM_ERR_OK, err); \
74 \
75 /* if the initial key is invalid, exit early */ \
76 MH_EXIT_IF( \
77 !bcmolt_##obj##_key_bounds_check(msg_set->next_key, BCMOLT_PRESENCE_MASK_ALL, &dummy_prop_id), \
78 BCM_ERR_KEY_RANGE); \
79 err = mh_##obj##_key_validate(&dummy_hdr, msg_set->next_key); \
80 MH_EXIT_IF(err != BCM_ERR_OK, err); \
81 \
82 /* pack filter data (including presence mask) for comparisons */ \
83 len = bcmolt_##obj##_cfg_data_get_packed_length(&filter->data, filter->hdr.hdr.presence_mask); \
84 filter_packed = bcmos_calloc(len); \
85 MH_EXIT_IF(len > 0 && filter_packed == NULL, BCM_ERR_NOMEM); \
86 bcmolt_buf_init(&buf, len, filter_packed, BCMOLT_BUF_ENDIAN_FIXED); \
87 success = bcmolt_##obj##_cfg_data_pack(&filter->data, &buf, filter->hdr.hdr.presence_mask); \
88 \
89 while (msg_set->num_instances < msg_set->max_instances) \
90 { \
91 /* initialize the cfg structure with the key and pass it to the MH cfg_get handler */ \
92 BCMOLT_CFG_INIT(&cfg, obj, *((bcmolt_##obj##_key *)msg_set->next_key)); \
93 /* (get all properties that were requested or are being filtered on) */ \
94 cfg.hdr.hdr.presence_mask = msg_set->presence_mask | filter->hdr.hdr.presence_mask; \
95 cfg.hdr.hdr.type = BCMOLT_MSG_TYPE_GET; \
96 cfg.hdr.hdr.dir = BCMOLT_MSG_DIR_RESPONSE; \
97 cfg.hdr.hdr.err = mh_##obj##_cfg_get(&cfg.hdr.hdr, &cfg.key, &cfg.data); \
98 MH_EXIT_IF(cfg.hdr.hdr.err != BCM_ERR_OK, cfg.hdr.hdr.err); \
99 \
100 /* pack the resulting message data */ \
101 len = bcmolt_##obj##_cfg_data_get_packed_length(&cfg.data, filter->hdr.hdr.presence_mask); \
102 msg_packed = bcmos_calloc(len); \
103 MH_EXIT_IF(len > 0 && msg_packed == NULL, BCM_ERR_NOMEM); \
104 bcmolt_buf_init(&buf, len, msg_packed, BCMOLT_BUF_ENDIAN_FIXED); \
105 success = bcmolt_##obj##_cfg_data_pack(&cfg.data, &buf, filter->hdr.hdr.presence_mask); \
106 MH_EXIT_IF(!success, BCM_ERR_INTERNAL); \
107 \
108 /* check the packed message against the filter */ \
109 is_match = (memcmp(filter_packed, msg_packed, len) == 0); \
110 if ((flags & BCMOLT_FILTER_FLAGS_INVERT_SELECTION) != 0) \
111 { \
112 is_match = !is_match; \
113 } \
114 bcmos_free(msg_packed); \
115 msg_packed = NULL; \
116 \
117 if (is_match) \
118 { \
119 /* if there is a match, include this message in the message set */ \
120 msg_set->msg[msg_set->num_instances] = NULL; \
121 err = bcmolt_msg_clone(&msg_set->msg[msg_set->num_instances], &cfg.hdr.hdr); \
122 MH_EXIT_IF(err != BCM_ERR_OK, err); \
123 msg_set->num_instances++; \
124 } \
125 \
126 /* get next key, break if no more keys are found */ \
127 err = mh_##obj##_key_iterate(msg_set->next_key); \
128 MH_EXIT_IF(err != BCM_ERR_OK, err); \
129 } \
130 \
131cleanup: \
132 if (filter_packed != NULL) \
133 { \
134 bcmos_free(filter_packed); \
135 } \
136 if (msg_packed != NULL) \
137 { \
138 bcmos_free(msg_packed); \
139 } \
140 if (err == BCM_ERR_NO_MORE) \
141 { \
142 err = BCM_ERR_OK; \
143 msg_set->more = BCMOS_FALSE; \
144 } \
145 return err; \
146}
147
148#endif /* _BCMOLT_MH_UTILS_H_ */
149