13c83654fSVladimir Oltean // SPDX-License-Identifier: (GPL-2.0 OR MIT) 23c83654fSVladimir Oltean /* Microsemi Ocelot Switch driver 33c83654fSVladimir Oltean * Copyright (c) 2019 Microsemi Corporation 43c83654fSVladimir Oltean */ 53c83654fSVladimir Oltean 63c83654fSVladimir Oltean #include <linux/iopoll.h> 73c83654fSVladimir Oltean #include <linux/proc_fs.h> 83c83654fSVladimir Oltean 93c83654fSVladimir Oltean #include <soc/mscc/ocelot_vcap.h> 103c83654fSVladimir Oltean #include "ocelot_police.h" 113c83654fSVladimir Oltean #include "ocelot_vcap.h" 123c83654fSVladimir Oltean 133c83654fSVladimir Oltean #define ENTRY_WIDTH 32 143c83654fSVladimir Oltean 153c83654fSVladimir Oltean enum vcap_sel { 163c83654fSVladimir Oltean VCAP_SEL_ENTRY = 0x1, 173c83654fSVladimir Oltean VCAP_SEL_ACTION = 0x2, 183c83654fSVladimir Oltean VCAP_SEL_COUNTER = 0x4, 193c83654fSVladimir Oltean VCAP_SEL_ALL = 0x7, 203c83654fSVladimir Oltean }; 213c83654fSVladimir Oltean 223c83654fSVladimir Oltean enum vcap_cmd { 233c83654fSVladimir Oltean VCAP_CMD_WRITE = 0, /* Copy from Cache to TCAM */ 243c83654fSVladimir Oltean VCAP_CMD_READ = 1, /* Copy from TCAM to Cache */ 253c83654fSVladimir Oltean VCAP_CMD_MOVE_UP = 2, /* Move <count> up */ 263c83654fSVladimir Oltean VCAP_CMD_MOVE_DOWN = 3, /* Move <count> down */ 273c83654fSVladimir Oltean VCAP_CMD_INITIALIZE = 4, /* Write all (from cache) */ 283c83654fSVladimir Oltean }; 293c83654fSVladimir Oltean 303c83654fSVladimir Oltean #define VCAP_ENTRY_WIDTH 12 /* Max entry width (32bit words) */ 313c83654fSVladimir Oltean #define VCAP_COUNTER_WIDTH 4 /* Max counter width (32bit words) */ 323c83654fSVladimir Oltean 333c83654fSVladimir Oltean struct vcap_data { 343c83654fSVladimir Oltean u32 entry[VCAP_ENTRY_WIDTH]; /* ENTRY_DAT */ 353c83654fSVladimir Oltean u32 mask[VCAP_ENTRY_WIDTH]; /* MASK_DAT */ 363c83654fSVladimir Oltean u32 action[VCAP_ENTRY_WIDTH]; /* ACTION_DAT */ 373c83654fSVladimir Oltean u32 counter[VCAP_COUNTER_WIDTH]; /* CNT_DAT */ 383c83654fSVladimir Oltean u32 tg; /* TG_DAT */ 393c83654fSVladimir Oltean u32 type; /* Action type */ 403c83654fSVladimir Oltean u32 tg_sw; /* Current type-group */ 413c83654fSVladimir Oltean u32 cnt; /* Current counter */ 423c83654fSVladimir Oltean u32 key_offset; /* Current entry offset */ 433c83654fSVladimir Oltean u32 action_offset; /* Current action offset */ 443c83654fSVladimir Oltean u32 counter_offset; /* Current counter offset */ 453c83654fSVladimir Oltean u32 tg_value; /* Current type-group value */ 463c83654fSVladimir Oltean u32 tg_mask; /* Current type-group mask */ 473c83654fSVladimir Oltean }; 483c83654fSVladimir Oltean 49c1c3993eSVladimir Oltean static u32 vcap_read_update_ctrl(struct ocelot *ocelot, 50c1c3993eSVladimir Oltean const struct vcap_props *vcap) 513c83654fSVladimir Oltean { 52c1c3993eSVladimir Oltean return ocelot_target_read(ocelot, vcap->target, VCAP_CORE_UPDATE_CTRL); 533c83654fSVladimir Oltean } 543c83654fSVladimir Oltean 55c1c3993eSVladimir Oltean static void vcap_cmd(struct ocelot *ocelot, const struct vcap_props *vcap, 56c1c3993eSVladimir Oltean u16 ix, int cmd, int sel) 573c83654fSVladimir Oltean { 58c1c3993eSVladimir Oltean u32 value = (VCAP_CORE_UPDATE_CTRL_UPDATE_CMD(cmd) | 59c1c3993eSVladimir Oltean VCAP_CORE_UPDATE_CTRL_UPDATE_ADDR(ix) | 60c1c3993eSVladimir Oltean VCAP_CORE_UPDATE_CTRL_UPDATE_SHOT); 613c83654fSVladimir Oltean 62c1c3993eSVladimir Oltean if ((sel & VCAP_SEL_ENTRY) && ix >= vcap->entry_count) 633c83654fSVladimir Oltean return; 643c83654fSVladimir Oltean 653c83654fSVladimir Oltean if (!(sel & VCAP_SEL_ENTRY)) 66c1c3993eSVladimir Oltean value |= VCAP_CORE_UPDATE_CTRL_UPDATE_ENTRY_DIS; 673c83654fSVladimir Oltean 683c83654fSVladimir Oltean if (!(sel & VCAP_SEL_ACTION)) 69c1c3993eSVladimir Oltean value |= VCAP_CORE_UPDATE_CTRL_UPDATE_ACTION_DIS; 703c83654fSVladimir Oltean 713c83654fSVladimir Oltean if (!(sel & VCAP_SEL_COUNTER)) 72c1c3993eSVladimir Oltean value |= VCAP_CORE_UPDATE_CTRL_UPDATE_CNT_DIS; 733c83654fSVladimir Oltean 74c1c3993eSVladimir Oltean ocelot_target_write(ocelot, vcap->target, value, VCAP_CORE_UPDATE_CTRL); 75c1c3993eSVladimir Oltean 76c1c3993eSVladimir Oltean read_poll_timeout(vcap_read_update_ctrl, value, 77c1c3993eSVladimir Oltean (value & VCAP_CORE_UPDATE_CTRL_UPDATE_SHOT) == 0, 78c1c3993eSVladimir Oltean 10, 100000, false, ocelot, vcap); 793c83654fSVladimir Oltean } 803c83654fSVladimir Oltean 813c83654fSVladimir Oltean /* Convert from 0-based row to VCAP entry row and run command */ 82c1c3993eSVladimir Oltean static void vcap_row_cmd(struct ocelot *ocelot, const struct vcap_props *vcap, 83c1c3993eSVladimir Oltean u32 row, int cmd, int sel) 843c83654fSVladimir Oltean { 85c1c3993eSVladimir Oltean vcap_cmd(ocelot, vcap, vcap->entry_count - row - 1, cmd, sel); 863c83654fSVladimir Oltean } 873c83654fSVladimir Oltean 88c1c3993eSVladimir Oltean static void vcap_entry2cache(struct ocelot *ocelot, 89c1c3993eSVladimir Oltean const struct vcap_props *vcap, 90c1c3993eSVladimir Oltean struct vcap_data *data) 913c83654fSVladimir Oltean { 923c83654fSVladimir Oltean u32 entry_words, i; 933c83654fSVladimir Oltean 94c1c3993eSVladimir Oltean entry_words = DIV_ROUND_UP(vcap->entry_width, ENTRY_WIDTH); 953c83654fSVladimir Oltean 963c83654fSVladimir Oltean for (i = 0; i < entry_words; i++) { 97c1c3993eSVladimir Oltean ocelot_target_write_rix(ocelot, vcap->target, data->entry[i], 98c1c3993eSVladimir Oltean VCAP_CACHE_ENTRY_DAT, i); 99c1c3993eSVladimir Oltean ocelot_target_write_rix(ocelot, vcap->target, ~data->mask[i], 100c1c3993eSVladimir Oltean VCAP_CACHE_MASK_DAT, i); 1013c83654fSVladimir Oltean } 102c1c3993eSVladimir Oltean ocelot_target_write(ocelot, vcap->target, data->tg, VCAP_CACHE_TG_DAT); 1033c83654fSVladimir Oltean } 1043c83654fSVladimir Oltean 105c1c3993eSVladimir Oltean static void vcap_cache2entry(struct ocelot *ocelot, 106c1c3993eSVladimir Oltean const struct vcap_props *vcap, 107c1c3993eSVladimir Oltean struct vcap_data *data) 1083c83654fSVladimir Oltean { 1093c83654fSVladimir Oltean u32 entry_words, i; 1103c83654fSVladimir Oltean 111c1c3993eSVladimir Oltean entry_words = DIV_ROUND_UP(vcap->entry_width, ENTRY_WIDTH); 1123c83654fSVladimir Oltean 1133c83654fSVladimir Oltean for (i = 0; i < entry_words; i++) { 114c1c3993eSVladimir Oltean data->entry[i] = ocelot_target_read_rix(ocelot, vcap->target, 115c1c3993eSVladimir Oltean VCAP_CACHE_ENTRY_DAT, i); 1163c83654fSVladimir Oltean // Invert mask 117c1c3993eSVladimir Oltean data->mask[i] = ~ocelot_target_read_rix(ocelot, vcap->target, 118c1c3993eSVladimir Oltean VCAP_CACHE_MASK_DAT, i); 1193c83654fSVladimir Oltean } 120c1c3993eSVladimir Oltean data->tg = ocelot_target_read(ocelot, vcap->target, VCAP_CACHE_TG_DAT); 1213c83654fSVladimir Oltean } 1223c83654fSVladimir Oltean 123c1c3993eSVladimir Oltean static void vcap_action2cache(struct ocelot *ocelot, 124c1c3993eSVladimir Oltean const struct vcap_props *vcap, 125c1c3993eSVladimir Oltean struct vcap_data *data) 1263c83654fSVladimir Oltean { 1273c83654fSVladimir Oltean u32 action_words, mask; 1283c83654fSVladimir Oltean int i, width; 1293c83654fSVladimir Oltean 1303c83654fSVladimir Oltean /* Encode action type */ 131c1c3993eSVladimir Oltean width = vcap->action_type_width; 1323c83654fSVladimir Oltean if (width) { 1333c83654fSVladimir Oltean mask = GENMASK(width, 0); 1343c83654fSVladimir Oltean data->action[0] = ((data->action[0] & ~mask) | data->type); 1353c83654fSVladimir Oltean } 1363c83654fSVladimir Oltean 137c1c3993eSVladimir Oltean action_words = DIV_ROUND_UP(vcap->action_width, ENTRY_WIDTH); 1383c83654fSVladimir Oltean 1393c83654fSVladimir Oltean for (i = 0; i < action_words; i++) 140c1c3993eSVladimir Oltean ocelot_target_write_rix(ocelot, vcap->target, data->action[i], 141c1c3993eSVladimir Oltean VCAP_CACHE_ACTION_DAT, i); 1423c83654fSVladimir Oltean 143c1c3993eSVladimir Oltean for (i = 0; i < vcap->counter_words; i++) 144c1c3993eSVladimir Oltean ocelot_target_write_rix(ocelot, vcap->target, data->counter[i], 145c1c3993eSVladimir Oltean VCAP_CACHE_CNT_DAT, i); 1463c83654fSVladimir Oltean } 1473c83654fSVladimir Oltean 148c1c3993eSVladimir Oltean static void vcap_cache2action(struct ocelot *ocelot, 149c1c3993eSVladimir Oltean const struct vcap_props *vcap, 150c1c3993eSVladimir Oltean struct vcap_data *data) 1513c83654fSVladimir Oltean { 1523c83654fSVladimir Oltean u32 action_words; 1533c83654fSVladimir Oltean int i, width; 1543c83654fSVladimir Oltean 155c1c3993eSVladimir Oltean action_words = DIV_ROUND_UP(vcap->action_width, ENTRY_WIDTH); 1563c83654fSVladimir Oltean 1573c83654fSVladimir Oltean for (i = 0; i < action_words; i++) 158c1c3993eSVladimir Oltean data->action[i] = ocelot_target_read_rix(ocelot, vcap->target, 159c1c3993eSVladimir Oltean VCAP_CACHE_ACTION_DAT, 1603c83654fSVladimir Oltean i); 1613c83654fSVladimir Oltean 162c1c3993eSVladimir Oltean for (i = 0; i < vcap->counter_words; i++) 163c1c3993eSVladimir Oltean data->counter[i] = ocelot_target_read_rix(ocelot, vcap->target, 164c1c3993eSVladimir Oltean VCAP_CACHE_CNT_DAT, 165c1c3993eSVladimir Oltean i); 1663c83654fSVladimir Oltean 1673c83654fSVladimir Oltean /* Extract action type */ 168c1c3993eSVladimir Oltean width = vcap->action_type_width; 1693c83654fSVladimir Oltean data->type = (width ? (data->action[0] & GENMASK(width, 0)) : 0); 1703c83654fSVladimir Oltean } 1713c83654fSVladimir Oltean 1723c83654fSVladimir Oltean /* Calculate offsets for entry */ 173c1c3993eSVladimir Oltean static void vcap_data_offset_get(const struct vcap_props *vcap, 174c1c3993eSVladimir Oltean struct vcap_data *data, int ix) 1753c83654fSVladimir Oltean { 17664294263SVladimir Oltean int num_subwords_per_entry, num_subwords_per_action; 17764294263SVladimir Oltean int i, col, offset, num_entries_per_row, base; 178c1c3993eSVladimir Oltean u32 width = vcap->tg_width; 1793c83654fSVladimir Oltean 180e6ae7c50SXiaoliang Yang switch (data->tg_sw) { 181e6ae7c50SXiaoliang Yang case VCAP_TG_FULL: 1825963083aSVladimir Oltean num_entries_per_row = 1; 183e6ae7c50SXiaoliang Yang break; 184e6ae7c50SXiaoliang Yang case VCAP_TG_HALF: 1855963083aSVladimir Oltean num_entries_per_row = 2; 186e6ae7c50SXiaoliang Yang break; 187e6ae7c50SXiaoliang Yang case VCAP_TG_QUARTER: 1885963083aSVladimir Oltean num_entries_per_row = 4; 189e6ae7c50SXiaoliang Yang break; 190e6ae7c50SXiaoliang Yang default: 191e6ae7c50SXiaoliang Yang return; 192e6ae7c50SXiaoliang Yang } 193e6ae7c50SXiaoliang Yang 1945963083aSVladimir Oltean col = (ix % num_entries_per_row); 19564294263SVladimir Oltean num_subwords_per_entry = (vcap->sw_count / num_entries_per_row); 19664294263SVladimir Oltean base = (vcap->sw_count - col * num_subwords_per_entry - 19764294263SVladimir Oltean num_subwords_per_entry); 1983c83654fSVladimir Oltean data->tg_value = 0; 1993c83654fSVladimir Oltean data->tg_mask = 0; 20064294263SVladimir Oltean for (i = 0; i < num_subwords_per_entry; i++) { 2013c83654fSVladimir Oltean offset = ((base + i) * width); 2023c83654fSVladimir Oltean data->tg_value |= (data->tg_sw << offset); 2033c83654fSVladimir Oltean data->tg_mask |= GENMASK(offset + width - 1, offset); 2043c83654fSVladimir Oltean } 2053c83654fSVladimir Oltean 2063c83654fSVladimir Oltean /* Calculate key/action/counter offsets */ 2075963083aSVladimir Oltean col = (num_entries_per_row - col - 1); 208c1c3993eSVladimir Oltean data->key_offset = (base * vcap->entry_width) / vcap->sw_count; 20964294263SVladimir Oltean data->counter_offset = (num_subwords_per_entry * col * 21064294263SVladimir Oltean vcap->counter_width); 2113c83654fSVladimir Oltean i = data->type; 212c1c3993eSVladimir Oltean width = vcap->action_table[i].width; 21364294263SVladimir Oltean num_subwords_per_action = vcap->action_table[i].count; 21464294263SVladimir Oltean data->action_offset = ((num_subwords_per_action * col * width) / 21564294263SVladimir Oltean num_entries_per_row); 21664294263SVladimir Oltean data->action_offset += vcap->action_type_width; 2173c83654fSVladimir Oltean } 2183c83654fSVladimir Oltean 2193c83654fSVladimir Oltean static void vcap_data_set(u32 *data, u32 offset, u32 len, u32 value) 2203c83654fSVladimir Oltean { 2213c83654fSVladimir Oltean u32 i, v, m; 2223c83654fSVladimir Oltean 2233c83654fSVladimir Oltean for (i = 0; i < len; i++, offset++) { 2243c83654fSVladimir Oltean v = data[offset / ENTRY_WIDTH]; 2253c83654fSVladimir Oltean m = (1 << (offset % ENTRY_WIDTH)); 2263c83654fSVladimir Oltean if (value & (1 << i)) 2273c83654fSVladimir Oltean v |= m; 2283c83654fSVladimir Oltean else 2293c83654fSVladimir Oltean v &= ~m; 2303c83654fSVladimir Oltean data[offset / ENTRY_WIDTH] = v; 2313c83654fSVladimir Oltean } 2323c83654fSVladimir Oltean } 2333c83654fSVladimir Oltean 2343c83654fSVladimir Oltean static u32 vcap_data_get(u32 *data, u32 offset, u32 len) 2353c83654fSVladimir Oltean { 2363c83654fSVladimir Oltean u32 i, v, m, value = 0; 2373c83654fSVladimir Oltean 2383c83654fSVladimir Oltean for (i = 0; i < len; i++, offset++) { 2393c83654fSVladimir Oltean v = data[offset / ENTRY_WIDTH]; 2403c83654fSVladimir Oltean m = (1 << (offset % ENTRY_WIDTH)); 2413c83654fSVladimir Oltean if (v & m) 2423c83654fSVladimir Oltean value |= (1 << i); 2433c83654fSVladimir Oltean } 2443c83654fSVladimir Oltean return value; 2453c83654fSVladimir Oltean } 2463c83654fSVladimir Oltean 2473c83654fSVladimir Oltean static void vcap_key_field_set(struct vcap_data *data, u32 offset, u32 width, 2483c83654fSVladimir Oltean u32 value, u32 mask) 2493c83654fSVladimir Oltean { 2503c83654fSVladimir Oltean vcap_data_set(data->entry, offset + data->key_offset, width, value); 2513c83654fSVladimir Oltean vcap_data_set(data->mask, offset + data->key_offset, width, mask); 2523c83654fSVladimir Oltean } 2533c83654fSVladimir Oltean 254c1c3993eSVladimir Oltean static void vcap_key_set(const struct vcap_props *vcap, struct vcap_data *data, 255c1c3993eSVladimir Oltean int field, u32 value, u32 mask) 2563c83654fSVladimir Oltean { 257c1c3993eSVladimir Oltean u32 offset = vcap->keys[field].offset; 258c1c3993eSVladimir Oltean u32 length = vcap->keys[field].length; 2593c83654fSVladimir Oltean 2603c83654fSVladimir Oltean vcap_key_field_set(data, offset, length, value, mask); 2613c83654fSVladimir Oltean } 2623c83654fSVladimir Oltean 263c1c3993eSVladimir Oltean static void vcap_key_bytes_set(const struct vcap_props *vcap, 264c1c3993eSVladimir Oltean struct vcap_data *data, int field, 2653c83654fSVladimir Oltean u8 *val, u8 *msk) 2663c83654fSVladimir Oltean { 267c1c3993eSVladimir Oltean u32 offset = vcap->keys[field].offset; 268c1c3993eSVladimir Oltean u32 count = vcap->keys[field].length; 2693c83654fSVladimir Oltean u32 i, j, n = 0, value = 0, mask = 0; 2703c83654fSVladimir Oltean 2713c83654fSVladimir Oltean WARN_ON(count % 8); 2723c83654fSVladimir Oltean 2733c83654fSVladimir Oltean /* Data wider than 32 bits are split up in chunks of maximum 32 bits. 2743c83654fSVladimir Oltean * The 32 LSB of the data are written to the 32 MSB of the TCAM. 2753c83654fSVladimir Oltean */ 2763c83654fSVladimir Oltean offset += count; 2773c83654fSVladimir Oltean count /= 8; 2783c83654fSVladimir Oltean 2793c83654fSVladimir Oltean for (i = 0; i < count; i++) { 2803c83654fSVladimir Oltean j = (count - i - 1); 2813c83654fSVladimir Oltean value += (val[j] << n); 2823c83654fSVladimir Oltean mask += (msk[j] << n); 2833c83654fSVladimir Oltean n += 8; 2843c83654fSVladimir Oltean if (n == ENTRY_WIDTH || (i + 1) == count) { 2853c83654fSVladimir Oltean offset -= n; 2863c83654fSVladimir Oltean vcap_key_field_set(data, offset, n, value, mask); 2873c83654fSVladimir Oltean n = 0; 2883c83654fSVladimir Oltean value = 0; 2893c83654fSVladimir Oltean mask = 0; 2903c83654fSVladimir Oltean } 2913c83654fSVladimir Oltean } 2923c83654fSVladimir Oltean } 2933c83654fSVladimir Oltean 294c1c3993eSVladimir Oltean static void vcap_key_l4_port_set(const struct vcap_props *vcap, 295c1c3993eSVladimir Oltean struct vcap_data *data, int field, 2963c83654fSVladimir Oltean struct ocelot_vcap_udp_tcp *port) 2973c83654fSVladimir Oltean { 298c1c3993eSVladimir Oltean u32 offset = vcap->keys[field].offset; 299c1c3993eSVladimir Oltean u32 length = vcap->keys[field].length; 3003c83654fSVladimir Oltean 3013c83654fSVladimir Oltean WARN_ON(length != 16); 3023c83654fSVladimir Oltean 3033c83654fSVladimir Oltean vcap_key_field_set(data, offset, length, port->value, port->mask); 3043c83654fSVladimir Oltean } 3053c83654fSVladimir Oltean 306c1c3993eSVladimir Oltean static void vcap_key_bit_set(const struct vcap_props *vcap, 307c1c3993eSVladimir Oltean struct vcap_data *data, int field, 3083c83654fSVladimir Oltean enum ocelot_vcap_bit val) 3093c83654fSVladimir Oltean { 3103c83654fSVladimir Oltean u32 value = (val == OCELOT_VCAP_BIT_1 ? 1 : 0); 3113c83654fSVladimir Oltean u32 msk = (val == OCELOT_VCAP_BIT_ANY ? 0 : 1); 312c1c3993eSVladimir Oltean u32 offset = vcap->keys[field].offset; 313c1c3993eSVladimir Oltean u32 length = vcap->keys[field].length; 3143c83654fSVladimir Oltean 3153c83654fSVladimir Oltean WARN_ON(length != 1); 3163c83654fSVladimir Oltean 3173c83654fSVladimir Oltean vcap_key_field_set(data, offset, length, value, msk); 3183c83654fSVladimir Oltean } 3193c83654fSVladimir Oltean 320c1c3993eSVladimir Oltean static void vcap_action_set(const struct vcap_props *vcap, 321c1c3993eSVladimir Oltean struct vcap_data *data, int field, u32 value) 3223c83654fSVladimir Oltean { 323c1c3993eSVladimir Oltean int offset = vcap->actions[field].offset; 324c1c3993eSVladimir Oltean int length = vcap->actions[field].length; 3253c83654fSVladimir Oltean 3263c83654fSVladimir Oltean vcap_data_set(data->action, offset + data->action_offset, length, 3273c83654fSVladimir Oltean value); 3283c83654fSVladimir Oltean } 3293c83654fSVladimir Oltean 3303c83654fSVladimir Oltean static void is2_action_set(struct ocelot *ocelot, struct vcap_data *data, 331aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 3323c83654fSVladimir Oltean { 333c1c3993eSVladimir Oltean const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2]; 334ea9d1f30SVladimir Oltean struct ocelot_vcap_action *a = &filter->action; 335c1c3993eSVladimir Oltean 336ea9d1f30SVladimir Oltean vcap_action_set(vcap, data, VCAP_IS2_ACT_MASK_MODE, a->mask_mode); 337ea9d1f30SVladimir Oltean vcap_action_set(vcap, data, VCAP_IS2_ACT_PORT_MASK, a->port_mask); 338ea9d1f30SVladimir Oltean vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_ENA, a->police_ena); 339ea9d1f30SVladimir Oltean vcap_action_set(vcap, data, VCAP_IS2_ACT_POLICE_IDX, a->pol_ix); 340ea9d1f30SVladimir Oltean vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_QU_NUM, a->cpu_qu_num); 341ea9d1f30SVladimir Oltean vcap_action_set(vcap, data, VCAP_IS2_ACT_CPU_COPY_ENA, a->cpu_copy_ena); 3423c83654fSVladimir Oltean } 3433c83654fSVladimir Oltean 3443c83654fSVladimir Oltean static void is2_entry_set(struct ocelot *ocelot, int ix, 345aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 3463c83654fSVladimir Oltean { 347c1c3993eSVladimir Oltean const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS2]; 348aae4e500SVladimir Oltean struct ocelot_vcap_key_vlan *tag = &filter->vlan; 3493c83654fSVladimir Oltean u32 val, msk, type, type_mask = 0xf, i, count; 3503c83654fSVladimir Oltean struct ocelot_vcap_u64 payload; 3513c83654fSVladimir Oltean struct vcap_data data; 3523c83654fSVladimir Oltean int row = (ix / 2); 3533c83654fSVladimir Oltean 3543c83654fSVladimir Oltean memset(&payload, 0, sizeof(payload)); 3553c83654fSVladimir Oltean memset(&data, 0, sizeof(data)); 3563c83654fSVladimir Oltean 3573c83654fSVladimir Oltean /* Read row */ 358c1c3993eSVladimir Oltean vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL); 359c1c3993eSVladimir Oltean vcap_cache2entry(ocelot, vcap, &data); 360c1c3993eSVladimir Oltean vcap_cache2action(ocelot, vcap, &data); 3613c83654fSVladimir Oltean 3623c83654fSVladimir Oltean data.tg_sw = VCAP_TG_HALF; 363c1c3993eSVladimir Oltean vcap_data_offset_get(vcap, &data, ix); 3643c83654fSVladimir Oltean data.tg = (data.tg & ~data.tg_mask); 365aae4e500SVladimir Oltean if (filter->prio != 0) 3663c83654fSVladimir Oltean data.tg |= data.tg_value; 3673c83654fSVladimir Oltean 3683c83654fSVladimir Oltean data.type = IS2_ACTION_TYPE_NORMAL; 3693c83654fSVladimir Oltean 370226e9cd8SVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_PAG, filter->pag, 0xff); 371226e9cd8SVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST, 372226e9cd8SVladimir Oltean (filter->lookup == 0) ? OCELOT_VCAP_BIT_1 : 373226e9cd8SVladimir Oltean OCELOT_VCAP_BIT_0); 374c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_IGR_PORT_MASK, 0, 375aae4e500SVladimir Oltean ~filter->ingress_port_mask); 376c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST, OCELOT_VCAP_BIT_ANY); 377c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_HOST_MATCH, 3783c83654fSVladimir Oltean OCELOT_VCAP_BIT_ANY); 379c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_MC, filter->dmac_mc); 380c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_BC, filter->dmac_bc); 381c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_VLAN_TAGGED, tag->tagged); 382c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_VID, 3833c83654fSVladimir Oltean tag->vid.value, tag->vid.mask); 384c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_PCP, 3853c83654fSVladimir Oltean tag->pcp.value[0], tag->pcp.mask[0]); 386c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DEI, tag->dei); 3873c83654fSVladimir Oltean 388aae4e500SVladimir Oltean switch (filter->key_type) { 389aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_ETYPE: { 390aae4e500SVladimir Oltean struct ocelot_vcap_key_etype *etype = &filter->key.etype; 3913c83654fSVladimir Oltean 3923c83654fSVladimir Oltean type = IS2_TYPE_ETYPE; 393c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC, 3943c83654fSVladimir Oltean etype->dmac.value, etype->dmac.mask); 395c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC, 3963c83654fSVladimir Oltean etype->smac.value, etype->smac.mask); 397c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_ETYPE, 3983c83654fSVladimir Oltean etype->etype.value, etype->etype.mask); 3993c83654fSVladimir Oltean /* Clear unused bits */ 400c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0, 4013c83654fSVladimir Oltean 0, 0); 402c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1, 4033c83654fSVladimir Oltean 0, 0); 404c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2, 4053c83654fSVladimir Oltean 0, 0); 406c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, 4073c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0, 4083c83654fSVladimir Oltean etype->data.value, etype->data.mask); 4093c83654fSVladimir Oltean break; 4103c83654fSVladimir Oltean } 411aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_LLC: { 412aae4e500SVladimir Oltean struct ocelot_vcap_key_llc *llc = &filter->key.llc; 4133c83654fSVladimir Oltean 4143c83654fSVladimir Oltean type = IS2_TYPE_LLC; 415c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC, 4163c83654fSVladimir Oltean llc->dmac.value, llc->dmac.mask); 417c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC, 4183c83654fSVladimir Oltean llc->smac.value, llc->smac.mask); 4193c83654fSVladimir Oltean for (i = 0; i < 4; i++) { 4203c83654fSVladimir Oltean payload.value[i] = llc->llc.value[i]; 4213c83654fSVladimir Oltean payload.mask[i] = llc->llc.mask[i]; 4223c83654fSVladimir Oltean } 423c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_LLC_L2_LLC, 4243c83654fSVladimir Oltean payload.value, payload.mask); 4253c83654fSVladimir Oltean break; 4263c83654fSVladimir Oltean } 427aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_SNAP: { 428aae4e500SVladimir Oltean struct ocelot_vcap_key_snap *snap = &filter->key.snap; 4293c83654fSVladimir Oltean 4303c83654fSVladimir Oltean type = IS2_TYPE_SNAP; 431c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC, 4323c83654fSVladimir Oltean snap->dmac.value, snap->dmac.mask); 433c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC, 4343c83654fSVladimir Oltean snap->smac.value, snap->smac.mask); 435c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_SNAP_L2_SNAP, 436aae4e500SVladimir Oltean filter->key.snap.snap.value, 437aae4e500SVladimir Oltean filter->key.snap.snap.mask); 4383c83654fSVladimir Oltean break; 4393c83654fSVladimir Oltean } 440aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_ARP: { 441aae4e500SVladimir Oltean struct ocelot_vcap_key_arp *arp = &filter->key.arp; 4423c83654fSVladimir Oltean 4433c83654fSVladimir Oltean type = IS2_TYPE_ARP; 444c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_SMAC, 4453c83654fSVladimir Oltean arp->smac.value, arp->smac.mask); 446c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4473c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK, 4483c83654fSVladimir Oltean arp->ethernet); 449c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4503c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK, 4513c83654fSVladimir Oltean arp->ip); 452c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4533c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_LEN_OK, 4543c83654fSVladimir Oltean arp->length); 455c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4563c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_TARGET_MATCH, 4573c83654fSVladimir Oltean arp->dmac_match); 458c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4593c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_SENDER_MATCH, 4603c83654fSVladimir Oltean arp->smac_match); 461c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4623c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN, 4633c83654fSVladimir Oltean arp->unknown); 4643c83654fSVladimir Oltean 4653c83654fSVladimir Oltean /* OPCODE is inverse, bit 0 is reply flag, bit 1 is RARP flag */ 4663c83654fSVladimir Oltean val = ((arp->req == OCELOT_VCAP_BIT_0 ? 1 : 0) | 4673c83654fSVladimir Oltean (arp->arp == OCELOT_VCAP_BIT_0 ? 2 : 0)); 4683c83654fSVladimir Oltean msk = ((arp->req == OCELOT_VCAP_BIT_ANY ? 0 : 1) | 4693c83654fSVladimir Oltean (arp->arp == OCELOT_VCAP_BIT_ANY ? 0 : 2)); 470c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_OPCODE, 4713c83654fSVladimir Oltean val, msk); 472c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, 4733c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP, 4743c83654fSVladimir Oltean arp->dip.value.addr, arp->dip.mask.addr); 475c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, 4763c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP, 4773c83654fSVladimir Oltean arp->sip.value.addr, arp->sip.mask.addr); 478c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP, 4793c83654fSVladimir Oltean 0, 0); 4803c83654fSVladimir Oltean break; 4813c83654fSVladimir Oltean } 482aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_IPV4: 483aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_IPV6: { 4843c83654fSVladimir Oltean enum ocelot_vcap_bit sip_eq_dip, sport_eq_dport, seq_zero, tcp; 4853c83654fSVladimir Oltean enum ocelot_vcap_bit ttl, fragment, options, tcp_ack, tcp_urg; 4863c83654fSVladimir Oltean enum ocelot_vcap_bit tcp_fin, tcp_syn, tcp_rst, tcp_psh; 487aae4e500SVladimir Oltean struct ocelot_vcap_key_ipv4 *ipv4 = NULL; 488aae4e500SVladimir Oltean struct ocelot_vcap_key_ipv6 *ipv6 = NULL; 4893c83654fSVladimir Oltean struct ocelot_vcap_udp_tcp *sport, *dport; 4903c83654fSVladimir Oltean struct ocelot_vcap_ipv4 sip, dip; 4913c83654fSVladimir Oltean struct ocelot_vcap_u8 proto, ds; 4923c83654fSVladimir Oltean struct ocelot_vcap_u48 *ip_data; 4933c83654fSVladimir Oltean 494aae4e500SVladimir Oltean if (filter->key_type == OCELOT_VCAP_KEY_IPV4) { 495aae4e500SVladimir Oltean ipv4 = &filter->key.ipv4; 4963c83654fSVladimir Oltean ttl = ipv4->ttl; 4973c83654fSVladimir Oltean fragment = ipv4->fragment; 4983c83654fSVladimir Oltean options = ipv4->options; 4993c83654fSVladimir Oltean proto = ipv4->proto; 5003c83654fSVladimir Oltean ds = ipv4->ds; 5013c83654fSVladimir Oltean ip_data = &ipv4->data; 5023c83654fSVladimir Oltean sip = ipv4->sip; 5033c83654fSVladimir Oltean dip = ipv4->dip; 5043c83654fSVladimir Oltean sport = &ipv4->sport; 5053c83654fSVladimir Oltean dport = &ipv4->dport; 5063c83654fSVladimir Oltean tcp_fin = ipv4->tcp_fin; 5073c83654fSVladimir Oltean tcp_syn = ipv4->tcp_syn; 5083c83654fSVladimir Oltean tcp_rst = ipv4->tcp_rst; 5093c83654fSVladimir Oltean tcp_psh = ipv4->tcp_psh; 5103c83654fSVladimir Oltean tcp_ack = ipv4->tcp_ack; 5113c83654fSVladimir Oltean tcp_urg = ipv4->tcp_urg; 5123c83654fSVladimir Oltean sip_eq_dip = ipv4->sip_eq_dip; 5133c83654fSVladimir Oltean sport_eq_dport = ipv4->sport_eq_dport; 5143c83654fSVladimir Oltean seq_zero = ipv4->seq_zero; 5153c83654fSVladimir Oltean } else { 516aae4e500SVladimir Oltean ipv6 = &filter->key.ipv6; 5173c83654fSVladimir Oltean ttl = ipv6->ttl; 5183c83654fSVladimir Oltean fragment = OCELOT_VCAP_BIT_ANY; 5193c83654fSVladimir Oltean options = OCELOT_VCAP_BIT_ANY; 5203c83654fSVladimir Oltean proto = ipv6->proto; 5213c83654fSVladimir Oltean ds = ipv6->ds; 5223c83654fSVladimir Oltean ip_data = &ipv6->data; 5233c83654fSVladimir Oltean for (i = 0; i < 8; i++) { 5243c83654fSVladimir Oltean val = ipv6->sip.value[i + 8]; 5253c83654fSVladimir Oltean msk = ipv6->sip.mask[i + 8]; 5263c83654fSVladimir Oltean if (i < 4) { 5273c83654fSVladimir Oltean dip.value.addr[i] = val; 5283c83654fSVladimir Oltean dip.mask.addr[i] = msk; 5293c83654fSVladimir Oltean } else { 5303c83654fSVladimir Oltean sip.value.addr[i - 4] = val; 5313c83654fSVladimir Oltean sip.mask.addr[i - 4] = msk; 5323c83654fSVladimir Oltean } 5333c83654fSVladimir Oltean } 5343c83654fSVladimir Oltean sport = &ipv6->sport; 5353c83654fSVladimir Oltean dport = &ipv6->dport; 5363c83654fSVladimir Oltean tcp_fin = ipv6->tcp_fin; 5373c83654fSVladimir Oltean tcp_syn = ipv6->tcp_syn; 5383c83654fSVladimir Oltean tcp_rst = ipv6->tcp_rst; 5393c83654fSVladimir Oltean tcp_psh = ipv6->tcp_psh; 5403c83654fSVladimir Oltean tcp_ack = ipv6->tcp_ack; 5413c83654fSVladimir Oltean tcp_urg = ipv6->tcp_urg; 5423c83654fSVladimir Oltean sip_eq_dip = ipv6->sip_eq_dip; 5433c83654fSVladimir Oltean sport_eq_dport = ipv6->sport_eq_dport; 5443c83654fSVladimir Oltean seq_zero = ipv6->seq_zero; 5453c83654fSVladimir Oltean } 5463c83654fSVladimir Oltean 547c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4, 5483c83654fSVladimir Oltean ipv4 ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0); 549c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_FRAGMENT, 5503c83654fSVladimir Oltean fragment); 551c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_L3_FRAG_OFS_GT0, 0, 0); 552c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_OPTIONS, 5533c83654fSVladimir Oltean options); 554c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4_L3_TTL_GT0, 5553c83654fSVladimir Oltean ttl); 556c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_TOS, 5573c83654fSVladimir Oltean ds.value, ds.mask); 558c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_DIP, 5593c83654fSVladimir Oltean dip.value.addr, dip.mask.addr); 560c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_SIP, 5613c83654fSVladimir Oltean sip.value.addr, sip.mask.addr); 562c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DIP_EQ_SIP, 5633c83654fSVladimir Oltean sip_eq_dip); 5643c83654fSVladimir Oltean val = proto.value[0]; 5653c83654fSVladimir Oltean msk = proto.mask[0]; 5663c83654fSVladimir Oltean type = IS2_TYPE_IP_UDP_TCP; 5673c83654fSVladimir Oltean if (msk == 0xff && (val == 6 || val == 17)) { 5683c83654fSVladimir Oltean /* UDP/TCP protocol match */ 5693c83654fSVladimir Oltean tcp = (val == 6 ? 5703c83654fSVladimir Oltean OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0); 571c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_TCP, tcp); 572c1c3993eSVladimir Oltean vcap_key_l4_port_set(vcap, &data, 5733c83654fSVladimir Oltean VCAP_IS2_HK_L4_DPORT, dport); 574c1c3993eSVladimir Oltean vcap_key_l4_port_set(vcap, &data, 5753c83654fSVladimir Oltean VCAP_IS2_HK_L4_SPORT, sport); 576c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_RNG, 0, 0); 577c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 5783c83654fSVladimir Oltean VCAP_IS2_HK_L4_SPORT_EQ_DPORT, 5793c83654fSVladimir Oltean sport_eq_dport); 580c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 5813c83654fSVladimir Oltean VCAP_IS2_HK_L4_SEQUENCE_EQ0, 5823c83654fSVladimir Oltean seq_zero); 583c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_FIN, 5843c83654fSVladimir Oltean tcp_fin); 585c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_SYN, 5863c83654fSVladimir Oltean tcp_syn); 587c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_RST, 5883c83654fSVladimir Oltean tcp_rst); 589c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_PSH, 5903c83654fSVladimir Oltean tcp_psh); 591c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_ACK, 5923c83654fSVladimir Oltean tcp_ack); 593c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_URG, 5943c83654fSVladimir Oltean tcp_urg); 595c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_DOM, 5963c83654fSVladimir Oltean 0, 0); 597c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_VER, 5983c83654fSVladimir Oltean 0, 0); 5993c83654fSVladimir Oltean } else { 6003c83654fSVladimir Oltean if (msk == 0) { 6013c83654fSVladimir Oltean /* Any IP protocol match */ 6023c83654fSVladimir Oltean type_mask = IS2_TYPE_MASK_IP_ANY; 6033c83654fSVladimir Oltean } else { 6043c83654fSVladimir Oltean /* Non-UDP/TCP protocol match */ 6053c83654fSVladimir Oltean type = IS2_TYPE_IP_OTHER; 6063c83654fSVladimir Oltean for (i = 0; i < 6; i++) { 6073c83654fSVladimir Oltean payload.value[i] = ip_data->value[i]; 6083c83654fSVladimir Oltean payload.mask[i] = ip_data->mask[i]; 6093c83654fSVladimir Oltean } 6103c83654fSVladimir Oltean } 611c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, 6123c83654fSVladimir Oltean VCAP_IS2_HK_IP4_L3_PROTO, 6133c83654fSVladimir Oltean proto.value, proto.mask); 614c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, 6153c83654fSVladimir Oltean VCAP_IS2_HK_L3_PAYLOAD, 6163c83654fSVladimir Oltean payload.value, payload.mask); 6173c83654fSVladimir Oltean } 6183c83654fSVladimir Oltean break; 6193c83654fSVladimir Oltean } 620aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_ANY: 6213c83654fSVladimir Oltean default: 6223c83654fSVladimir Oltean type = 0; 6233c83654fSVladimir Oltean type_mask = 0; 624c1c3993eSVladimir Oltean count = vcap->entry_width / 2; 6253c83654fSVladimir Oltean /* Iterate over the non-common part of the key and 6263c83654fSVladimir Oltean * clear entry data 6273c83654fSVladimir Oltean */ 628c1c3993eSVladimir Oltean for (i = vcap->keys[VCAP_IS2_HK_L2_DMAC].offset; 6293c83654fSVladimir Oltean i < count; i += ENTRY_WIDTH) { 6303c83654fSVladimir Oltean vcap_key_field_set(&data, i, min(32u, count - i), 0, 0); 6313c83654fSVladimir Oltean } 6323c83654fSVladimir Oltean break; 6333c83654fSVladimir Oltean } 6343c83654fSVladimir Oltean 635c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_TYPE, type, type_mask); 636aae4e500SVladimir Oltean is2_action_set(ocelot, &data, filter); 6373c83654fSVladimir Oltean vcap_data_set(data.counter, data.counter_offset, 638c1c3993eSVladimir Oltean vcap->counter_width, filter->stats.pkts); 6393c83654fSVladimir Oltean 6403c83654fSVladimir Oltean /* Write row */ 641c1c3993eSVladimir Oltean vcap_entry2cache(ocelot, vcap, &data); 642c1c3993eSVladimir Oltean vcap_action2cache(ocelot, vcap, &data); 643c1c3993eSVladimir Oltean vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL); 6443c83654fSVladimir Oltean } 6453c83654fSVladimir Oltean 64675944fdaSXiaoliang Yang static void is1_action_set(struct ocelot *ocelot, struct vcap_data *data, 64775944fdaSXiaoliang Yang const struct ocelot_vcap_filter *filter) 64875944fdaSXiaoliang Yang { 64975944fdaSXiaoliang Yang const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1]; 65075944fdaSXiaoliang Yang const struct ocelot_vcap_action *a = &filter->action; 65175944fdaSXiaoliang Yang 65275944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_REPLACE_ENA, 65375944fdaSXiaoliang Yang a->vid_replace_ena); 65475944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_ADD_VAL, a->vid); 65575944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT_ENA, 65675944fdaSXiaoliang Yang a->vlan_pop_cnt_ena); 65775944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT, 65875944fdaSXiaoliang Yang a->vlan_pop_cnt); 65975944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_DEI_ENA, a->pcp_dei_ena); 66075944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_VAL, a->pcp); 66175944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_DEI_VAL, a->dei); 66275944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_ENA, a->qos_ena); 66375944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_VAL, a->qos_val); 66475944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_OVERRIDE_MASK, 66575944fdaSXiaoliang Yang a->pag_override_mask); 66675944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_VAL, a->pag_val); 66775944fdaSXiaoliang Yang } 66875944fdaSXiaoliang Yang 66975944fdaSXiaoliang Yang static void is1_entry_set(struct ocelot *ocelot, int ix, 67075944fdaSXiaoliang Yang struct ocelot_vcap_filter *filter) 67175944fdaSXiaoliang Yang { 67275944fdaSXiaoliang Yang const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1]; 67375944fdaSXiaoliang Yang struct ocelot_vcap_key_vlan *tag = &filter->vlan; 67475944fdaSXiaoliang Yang struct ocelot_vcap_u64 payload; 67575944fdaSXiaoliang Yang struct vcap_data data; 67675944fdaSXiaoliang Yang int row = ix / 2; 67775944fdaSXiaoliang Yang u32 type; 67875944fdaSXiaoliang Yang 67975944fdaSXiaoliang Yang memset(&payload, 0, sizeof(payload)); 68075944fdaSXiaoliang Yang memset(&data, 0, sizeof(data)); 68175944fdaSXiaoliang Yang 68275944fdaSXiaoliang Yang /* Read row */ 68375944fdaSXiaoliang Yang vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL); 68475944fdaSXiaoliang Yang vcap_cache2entry(ocelot, vcap, &data); 68575944fdaSXiaoliang Yang vcap_cache2action(ocelot, vcap, &data); 68675944fdaSXiaoliang Yang 68775944fdaSXiaoliang Yang data.tg_sw = VCAP_TG_HALF; 68875944fdaSXiaoliang Yang data.type = IS1_ACTION_TYPE_NORMAL; 68975944fdaSXiaoliang Yang vcap_data_offset_get(vcap, &data, ix); 69075944fdaSXiaoliang Yang data.tg = (data.tg & ~data.tg_mask); 69175944fdaSXiaoliang Yang if (filter->prio != 0) 69275944fdaSXiaoliang Yang data.tg |= data.tg_value; 69375944fdaSXiaoliang Yang 694226e9cd8SVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS1_HK_LOOKUP, filter->lookup, 0x3); 69575944fdaSXiaoliang Yang vcap_key_set(vcap, &data, VCAP_IS1_HK_IGR_PORT_MASK, 0, 69675944fdaSXiaoliang Yang ~filter->ingress_port_mask); 69775944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_MC, filter->dmac_mc); 69875944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_BC, filter->dmac_bc); 69975944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_VLAN_TAGGED, tag->tagged); 70075944fdaSXiaoliang Yang vcap_key_set(vcap, &data, VCAP_IS1_HK_VID, 70175944fdaSXiaoliang Yang tag->vid.value, tag->vid.mask); 70275944fdaSXiaoliang Yang vcap_key_set(vcap, &data, VCAP_IS1_HK_PCP, 70375944fdaSXiaoliang Yang tag->pcp.value[0], tag->pcp.mask[0]); 70475944fdaSXiaoliang Yang type = IS1_TYPE_S1_NORMAL; 70575944fdaSXiaoliang Yang 70675944fdaSXiaoliang Yang switch (filter->key_type) { 70775944fdaSXiaoliang Yang case OCELOT_VCAP_KEY_ETYPE: { 70875944fdaSXiaoliang Yang struct ocelot_vcap_key_etype *etype = &filter->key.etype; 70975944fdaSXiaoliang Yang 71075944fdaSXiaoliang Yang vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L2_SMAC, 71175944fdaSXiaoliang Yang etype->smac.value, etype->smac.mask); 71275944fdaSXiaoliang Yang vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE, 71375944fdaSXiaoliang Yang etype->etype.value, etype->etype.mask); 71475944fdaSXiaoliang Yang break; 71575944fdaSXiaoliang Yang } 71675944fdaSXiaoliang Yang case OCELOT_VCAP_KEY_IPV4: { 71775944fdaSXiaoliang Yang struct ocelot_vcap_key_ipv4 *ipv4 = &filter->key.ipv4; 71875944fdaSXiaoliang Yang struct ocelot_vcap_udp_tcp *sport = &ipv4->sport; 71975944fdaSXiaoliang Yang struct ocelot_vcap_udp_tcp *dport = &ipv4->dport; 72075944fdaSXiaoliang Yang enum ocelot_vcap_bit tcp_udp = OCELOT_VCAP_BIT_0; 72175944fdaSXiaoliang Yang struct ocelot_vcap_u8 proto = ipv4->proto; 72275944fdaSXiaoliang Yang struct ocelot_vcap_ipv4 sip = ipv4->sip; 72375944fdaSXiaoliang Yang u32 val, msk; 72475944fdaSXiaoliang Yang 72575944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP_SNAP, 72675944fdaSXiaoliang Yang OCELOT_VCAP_BIT_1); 72775944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP4, 72875944fdaSXiaoliang Yang OCELOT_VCAP_BIT_1); 72975944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_ETYPE_LEN, 73075944fdaSXiaoliang Yang OCELOT_VCAP_BIT_1); 73175944fdaSXiaoliang Yang vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L3_IP4_SIP, 73275944fdaSXiaoliang Yang sip.value.addr, sip.mask.addr); 73375944fdaSXiaoliang Yang 73475944fdaSXiaoliang Yang val = proto.value[0]; 73575944fdaSXiaoliang Yang msk = proto.mask[0]; 73675944fdaSXiaoliang Yang 73775944fdaSXiaoliang Yang if ((val == NEXTHDR_TCP || val == NEXTHDR_UDP) && msk == 0xff) 73875944fdaSXiaoliang Yang tcp_udp = OCELOT_VCAP_BIT_1; 73975944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP_UDP, tcp_udp); 74075944fdaSXiaoliang Yang 74175944fdaSXiaoliang Yang if (tcp_udp) { 74275944fdaSXiaoliang Yang enum ocelot_vcap_bit tcp = OCELOT_VCAP_BIT_0; 74375944fdaSXiaoliang Yang 74475944fdaSXiaoliang Yang if (val == NEXTHDR_TCP) 74575944fdaSXiaoliang Yang tcp = OCELOT_VCAP_BIT_1; 74675944fdaSXiaoliang Yang 74775944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP, tcp); 74875944fdaSXiaoliang Yang vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_L4_SPORT, 74975944fdaSXiaoliang Yang sport); 75075944fdaSXiaoliang Yang /* Overloaded field */ 75175944fdaSXiaoliang Yang vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_ETYPE, 75275944fdaSXiaoliang Yang dport); 75375944fdaSXiaoliang Yang } else { 75475944fdaSXiaoliang Yang /* IPv4 "other" frame */ 75575944fdaSXiaoliang Yang struct ocelot_vcap_u16 etype = {0}; 75675944fdaSXiaoliang Yang 75775944fdaSXiaoliang Yang /* Overloaded field */ 75875944fdaSXiaoliang Yang etype.value[0] = proto.value[0]; 75975944fdaSXiaoliang Yang etype.mask[0] = proto.mask[0]; 76075944fdaSXiaoliang Yang 76175944fdaSXiaoliang Yang vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE, 76275944fdaSXiaoliang Yang etype.value, etype.mask); 76375944fdaSXiaoliang Yang } 764fdeadd6eSGustavo A. R. Silva break; 76575944fdaSXiaoliang Yang } 76675944fdaSXiaoliang Yang default: 76775944fdaSXiaoliang Yang break; 76875944fdaSXiaoliang Yang } 76975944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TYPE, 77075944fdaSXiaoliang Yang type ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0); 77175944fdaSXiaoliang Yang 77275944fdaSXiaoliang Yang is1_action_set(ocelot, &data, filter); 77375944fdaSXiaoliang Yang vcap_data_set(data.counter, data.counter_offset, 77475944fdaSXiaoliang Yang vcap->counter_width, filter->stats.pkts); 77575944fdaSXiaoliang Yang 77675944fdaSXiaoliang Yang /* Write row */ 77775944fdaSXiaoliang Yang vcap_entry2cache(ocelot, vcap, &data); 77875944fdaSXiaoliang Yang vcap_action2cache(ocelot, vcap, &data); 77975944fdaSXiaoliang Yang vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL); 78075944fdaSXiaoliang Yang } 78175944fdaSXiaoliang Yang 7822f17c050SXiaoliang Yang static void es0_action_set(struct ocelot *ocelot, struct vcap_data *data, 7832f17c050SXiaoliang Yang const struct ocelot_vcap_filter *filter) 7842f17c050SXiaoliang Yang { 7852f17c050SXiaoliang Yang const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0]; 7862f17c050SXiaoliang Yang const struct ocelot_vcap_action *a = &filter->action; 7872f17c050SXiaoliang Yang 7882f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_OUTER_TAG, 7892f17c050SXiaoliang Yang a->push_outer_tag); 7902f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_PUSH_INNER_TAG, 7912f17c050SXiaoliang Yang a->push_inner_tag); 7922f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_TPID_SEL, 7932f17c050SXiaoliang Yang a->tag_a_tpid_sel); 7942f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_VID_SEL, 7952f17c050SXiaoliang Yang a->tag_a_vid_sel); 7962f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_A_PCP_SEL, 7972f17c050SXiaoliang Yang a->tag_a_pcp_sel); 7982f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_A_VAL, a->vid_a_val); 7992f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_A_VAL, a->pcp_a_val); 8002f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_TPID_SEL, 8012f17c050SXiaoliang Yang a->tag_b_tpid_sel); 8022f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_VID_SEL, 8032f17c050SXiaoliang Yang a->tag_b_vid_sel); 8042f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_TAG_B_PCP_SEL, 8052f17c050SXiaoliang Yang a->tag_b_pcp_sel); 8062f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_VID_B_VAL, a->vid_b_val); 8072f17c050SXiaoliang Yang vcap_action_set(vcap, data, VCAP_ES0_ACT_PCP_B_VAL, a->pcp_b_val); 8082f17c050SXiaoliang Yang } 8092f17c050SXiaoliang Yang 8102f17c050SXiaoliang Yang static void es0_entry_set(struct ocelot *ocelot, int ix, 8112f17c050SXiaoliang Yang struct ocelot_vcap_filter *filter) 8122f17c050SXiaoliang Yang { 8132f17c050SXiaoliang Yang const struct vcap_props *vcap = &ocelot->vcap[VCAP_ES0]; 8142f17c050SXiaoliang Yang struct ocelot_vcap_key_vlan *tag = &filter->vlan; 8152f17c050SXiaoliang Yang struct ocelot_vcap_u64 payload; 8162f17c050SXiaoliang Yang struct vcap_data data; 8172f17c050SXiaoliang Yang int row = ix; 8182f17c050SXiaoliang Yang 8192f17c050SXiaoliang Yang memset(&payload, 0, sizeof(payload)); 8202f17c050SXiaoliang Yang memset(&data, 0, sizeof(data)); 8212f17c050SXiaoliang Yang 8222f17c050SXiaoliang Yang /* Read row */ 8232f17c050SXiaoliang Yang vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL); 8242f17c050SXiaoliang Yang vcap_cache2entry(ocelot, vcap, &data); 8252f17c050SXiaoliang Yang vcap_cache2action(ocelot, vcap, &data); 8262f17c050SXiaoliang Yang 8272f17c050SXiaoliang Yang data.tg_sw = VCAP_TG_FULL; 8282f17c050SXiaoliang Yang data.type = ES0_ACTION_TYPE_NORMAL; 8292f17c050SXiaoliang Yang vcap_data_offset_get(vcap, &data, ix); 8302f17c050SXiaoliang Yang data.tg = (data.tg & ~data.tg_mask); 8312f17c050SXiaoliang Yang if (filter->prio != 0) 8322f17c050SXiaoliang Yang data.tg |= data.tg_value; 8332f17c050SXiaoliang Yang 8342f17c050SXiaoliang Yang vcap_key_set(vcap, &data, VCAP_ES0_IGR_PORT, filter->ingress_port.value, 8352f17c050SXiaoliang Yang filter->ingress_port.mask); 8362f17c050SXiaoliang Yang vcap_key_set(vcap, &data, VCAP_ES0_EGR_PORT, filter->egress_port.value, 8372f17c050SXiaoliang Yang filter->egress_port.mask); 8382f17c050SXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_MC, filter->dmac_mc); 8392f17c050SXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_ES0_L2_BC, filter->dmac_bc); 8402f17c050SXiaoliang Yang vcap_key_set(vcap, &data, VCAP_ES0_VID, 8412f17c050SXiaoliang Yang tag->vid.value, tag->vid.mask); 8422f17c050SXiaoliang Yang vcap_key_set(vcap, &data, VCAP_ES0_PCP, 8432f17c050SXiaoliang Yang tag->pcp.value[0], tag->pcp.mask[0]); 8442f17c050SXiaoliang Yang 8452f17c050SXiaoliang Yang es0_action_set(ocelot, &data, filter); 8462f17c050SXiaoliang Yang vcap_data_set(data.counter, data.counter_offset, 8472f17c050SXiaoliang Yang vcap->counter_width, filter->stats.pkts); 8482f17c050SXiaoliang Yang 8492f17c050SXiaoliang Yang /* Write row */ 8502f17c050SXiaoliang Yang vcap_entry2cache(ocelot, vcap, &data); 8512f17c050SXiaoliang Yang vcap_action2cache(ocelot, vcap, &data); 8522f17c050SXiaoliang Yang vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL); 8532f17c050SXiaoliang Yang } 8542f17c050SXiaoliang Yang 8551397a2ebSVladimir Oltean static void vcap_entry_get(struct ocelot *ocelot, int ix, 8561397a2ebSVladimir Oltean struct ocelot_vcap_filter *filter) 8573c83654fSVladimir Oltean { 8581397a2ebSVladimir Oltean const struct vcap_props *vcap = &ocelot->vcap[filter->block_id]; 8593c83654fSVladimir Oltean struct vcap_data data; 860c1c3993eSVladimir Oltean int row, count; 8613c83654fSVladimir Oltean u32 cnt; 8623c83654fSVladimir Oltean 8632f17c050SXiaoliang Yang if (filter->block_id == VCAP_ES0) 8642f17c050SXiaoliang Yang data.tg_sw = VCAP_TG_FULL; 8652f17c050SXiaoliang Yang else 8663c83654fSVladimir Oltean data.tg_sw = VCAP_TG_HALF; 8672f17c050SXiaoliang Yang 868c1c3993eSVladimir Oltean count = (1 << (data.tg_sw - 1)); 869c1c3993eSVladimir Oltean row = (ix / count); 870c1c3993eSVladimir Oltean vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_COUNTER); 871c1c3993eSVladimir Oltean vcap_cache2action(ocelot, vcap, &data); 872c1c3993eSVladimir Oltean vcap_data_offset_get(vcap, &data, ix); 8733c83654fSVladimir Oltean cnt = vcap_data_get(data.counter, data.counter_offset, 874c1c3993eSVladimir Oltean vcap->counter_width); 8753c83654fSVladimir Oltean 876aae4e500SVladimir Oltean filter->stats.pkts = cnt; 8773c83654fSVladimir Oltean } 8783c83654fSVladimir Oltean 8791397a2ebSVladimir Oltean static void vcap_entry_set(struct ocelot *ocelot, int ix, 8801397a2ebSVladimir Oltean struct ocelot_vcap_filter *filter) 8811397a2ebSVladimir Oltean { 88275944fdaSXiaoliang Yang if (filter->block_id == VCAP_IS1) 88375944fdaSXiaoliang Yang return is1_entry_set(ocelot, ix, filter); 8841397a2ebSVladimir Oltean if (filter->block_id == VCAP_IS2) 8851397a2ebSVladimir Oltean return is2_entry_set(ocelot, ix, filter); 8862f17c050SXiaoliang Yang if (filter->block_id == VCAP_ES0) 8872f17c050SXiaoliang Yang return es0_entry_set(ocelot, ix, filter); 8881397a2ebSVladimir Oltean } 8891397a2ebSVladimir Oltean 89077043c37SXiaoliang Yang struct vcap_policer_entry { 89177043c37SXiaoliang Yang struct list_head list; 89277043c37SXiaoliang Yang refcount_t refcount; 89377043c37SXiaoliang Yang u32 pol_ix; 89477043c37SXiaoliang Yang }; 89577043c37SXiaoliang Yang 89677043c37SXiaoliang Yang int ocelot_vcap_policer_add(struct ocelot *ocelot, u32 pol_ix, 897c73b0ad3SVladimir Oltean struct ocelot_policer *pol) 898c73b0ad3SVladimir Oltean { 899c73b0ad3SVladimir Oltean struct qos_policer_conf pp = { 0 }; 90077043c37SXiaoliang Yang struct vcap_policer_entry *tmp; 90177043c37SXiaoliang Yang int ret; 902c73b0ad3SVladimir Oltean 903c73b0ad3SVladimir Oltean if (!pol) 904c73b0ad3SVladimir Oltean return -EINVAL; 905c73b0ad3SVladimir Oltean 906c73b0ad3SVladimir Oltean pp.mode = MSCC_QOS_RATE_MODE_DATA; 907c73b0ad3SVladimir Oltean pp.pir = pol->rate; 908c73b0ad3SVladimir Oltean pp.pbs = pol->burst; 909c73b0ad3SVladimir Oltean 91077043c37SXiaoliang Yang list_for_each_entry(tmp, &ocelot->vcap_pol.pol_list, list) 91177043c37SXiaoliang Yang if (tmp->pol_ix == pol_ix) { 91277043c37SXiaoliang Yang refcount_inc(&tmp->refcount); 91377043c37SXiaoliang Yang return 0; 91477043c37SXiaoliang Yang } 91577043c37SXiaoliang Yang 91677043c37SXiaoliang Yang tmp = kzalloc(sizeof(*tmp), GFP_KERNEL); 91777043c37SXiaoliang Yang if (!tmp) 91877043c37SXiaoliang Yang return -ENOMEM; 91977043c37SXiaoliang Yang 92077043c37SXiaoliang Yang ret = qos_policer_conf_set(ocelot, 0, pol_ix, &pp); 92177043c37SXiaoliang Yang if (ret) { 92277043c37SXiaoliang Yang kfree(tmp); 92377043c37SXiaoliang Yang return ret; 92477043c37SXiaoliang Yang } 92577043c37SXiaoliang Yang 92677043c37SXiaoliang Yang tmp->pol_ix = pol_ix; 92777043c37SXiaoliang Yang refcount_set(&tmp->refcount, 1); 92877043c37SXiaoliang Yang list_add_tail(&tmp->list, &ocelot->vcap_pol.pol_list); 92977043c37SXiaoliang Yang 93077043c37SXiaoliang Yang return 0; 93177043c37SXiaoliang Yang } 93277043c37SXiaoliang Yang EXPORT_SYMBOL(ocelot_vcap_policer_add); 93377043c37SXiaoliang Yang 93477043c37SXiaoliang Yang int ocelot_vcap_policer_del(struct ocelot *ocelot, u32 pol_ix) 93577043c37SXiaoliang Yang { 93677043c37SXiaoliang Yang struct qos_policer_conf pp = {0}; 93777043c37SXiaoliang Yang struct vcap_policer_entry *tmp, *n; 93877043c37SXiaoliang Yang u8 z = 0; 93977043c37SXiaoliang Yang 94077043c37SXiaoliang Yang list_for_each_entry_safe(tmp, n, &ocelot->vcap_pol.pol_list, list) 94177043c37SXiaoliang Yang if (tmp->pol_ix == pol_ix) { 94277043c37SXiaoliang Yang z = refcount_dec_and_test(&tmp->refcount); 94377043c37SXiaoliang Yang if (z) { 94477043c37SXiaoliang Yang list_del(&tmp->list); 94577043c37SXiaoliang Yang kfree(tmp); 94677043c37SXiaoliang Yang } 94777043c37SXiaoliang Yang } 94877043c37SXiaoliang Yang 94977043c37SXiaoliang Yang if (z) { 95077043c37SXiaoliang Yang pp.mode = MSCC_QOS_RATE_MODE_DISABLED; 951c73b0ad3SVladimir Oltean return qos_policer_conf_set(ocelot, 0, pol_ix, &pp); 952c73b0ad3SVladimir Oltean } 953c73b0ad3SVladimir Oltean 95477043c37SXiaoliang Yang return 0; 955c73b0ad3SVladimir Oltean } 95677043c37SXiaoliang Yang EXPORT_SYMBOL(ocelot_vcap_policer_del); 957c73b0ad3SVladimir Oltean 95877043c37SXiaoliang Yang static int ocelot_vcap_filter_add_to_block(struct ocelot *ocelot, 959aae4e500SVladimir Oltean struct ocelot_vcap_block *block, 960aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 9613c83654fSVladimir Oltean { 962aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 9633c83654fSVladimir Oltean struct list_head *pos, *n; 96477043c37SXiaoliang Yang int ret; 9653c83654fSVladimir Oltean 9661397a2ebSVladimir Oltean if (filter->block_id == VCAP_IS2 && filter->action.police_ena) { 96777043c37SXiaoliang Yang ret = ocelot_vcap_policer_add(ocelot, filter->action.pol_ix, 968ea9d1f30SVladimir Oltean &filter->action.pol); 96977043c37SXiaoliang Yang if (ret) 97077043c37SXiaoliang Yang return ret; 9713c83654fSVladimir Oltean } 9723c83654fSVladimir Oltean 9733c83654fSVladimir Oltean block->count++; 9743c83654fSVladimir Oltean 9753c83654fSVladimir Oltean if (list_empty(&block->rules)) { 976aae4e500SVladimir Oltean list_add(&filter->list, &block->rules); 97777043c37SXiaoliang Yang return 0; 9783c83654fSVladimir Oltean } 9793c83654fSVladimir Oltean 9803c83654fSVladimir Oltean list_for_each_safe(pos, n, &block->rules) { 981aae4e500SVladimir Oltean tmp = list_entry(pos, struct ocelot_vcap_filter, list); 982aae4e500SVladimir Oltean if (filter->prio < tmp->prio) 9833c83654fSVladimir Oltean break; 9843c83654fSVladimir Oltean } 985aae4e500SVladimir Oltean list_add(&filter->list, pos->prev); 98677043c37SXiaoliang Yang 98777043c37SXiaoliang Yang return 0; 9883c83654fSVladimir Oltean } 9893c83654fSVladimir Oltean 99050c6cc5bSVladimir Oltean static bool ocelot_vcap_filter_equal(const struct ocelot_vcap_filter *a, 99150c6cc5bSVladimir Oltean const struct ocelot_vcap_filter *b) 99250c6cc5bSVladimir Oltean { 99350c6cc5bSVladimir Oltean return !memcmp(&a->id, &b->id, sizeof(struct ocelot_vcap_id)); 99450c6cc5bSVladimir Oltean } 99550c6cc5bSVladimir Oltean 996aae4e500SVladimir Oltean static int ocelot_vcap_block_get_filter_index(struct ocelot_vcap_block *block, 997aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 9983c83654fSVladimir Oltean { 999aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 1000ed5672d8SXiaoliang Yang int index = 0; 10013c83654fSVladimir Oltean 10023c83654fSVladimir Oltean list_for_each_entry(tmp, &block->rules, list) { 100350c6cc5bSVladimir Oltean if (ocelot_vcap_filter_equal(filter, tmp)) 10043c83654fSVladimir Oltean return index; 1005ed5672d8SXiaoliang Yang index++; 1006ed5672d8SXiaoliang Yang } 1007ed5672d8SXiaoliang Yang 1008ed5672d8SXiaoliang Yang return -ENOENT; 10093c83654fSVladimir Oltean } 10103c83654fSVladimir Oltean 1011aae4e500SVladimir Oltean static struct ocelot_vcap_filter* 1012085f5b91SVladimir Oltean ocelot_vcap_block_find_filter_by_index(struct ocelot_vcap_block *block, 1013aae4e500SVladimir Oltean int index) 10143c83654fSVladimir Oltean { 1015aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 10163c83654fSVladimir Oltean int i = 0; 10173c83654fSVladimir Oltean 10183c83654fSVladimir Oltean list_for_each_entry(tmp, &block->rules, list) { 10193c83654fSVladimir Oltean if (i == index) 10203c83654fSVladimir Oltean return tmp; 10213c83654fSVladimir Oltean ++i; 10223c83654fSVladimir Oltean } 10233c83654fSVladimir Oltean 10243c83654fSVladimir Oltean return NULL; 10253c83654fSVladimir Oltean } 10263c83654fSVladimir Oltean 1027085f5b91SVladimir Oltean struct ocelot_vcap_filter * 1028019d9329SVladimir Oltean ocelot_vcap_block_find_filter_by_id(struct ocelot_vcap_block *block, 1029019d9329SVladimir Oltean unsigned long cookie, bool tc_offload) 1030085f5b91SVladimir Oltean { 1031085f5b91SVladimir Oltean struct ocelot_vcap_filter *filter; 1032085f5b91SVladimir Oltean 1033085f5b91SVladimir Oltean list_for_each_entry(filter, &block->rules, list) 103450c6cc5bSVladimir Oltean if (filter->id.tc_offload == tc_offload && 103550c6cc5bSVladimir Oltean filter->id.cookie == cookie) 1036085f5b91SVladimir Oltean return filter; 1037085f5b91SVladimir Oltean 1038085f5b91SVladimir Oltean return NULL; 1039085f5b91SVladimir Oltean } 1040e21268efSVladimir Oltean EXPORT_SYMBOL(ocelot_vcap_block_find_filter_by_id); 1041085f5b91SVladimir Oltean 10423c83654fSVladimir Oltean /* If @on=false, then SNAP, ARP, IP and OAM frames will not match on keys based 10433c83654fSVladimir Oltean * on destination and source MAC addresses, but only on higher-level protocol 10443c83654fSVladimir Oltean * information. The only frame types to match on keys containing MAC addresses 10453c83654fSVladimir Oltean * in this case are non-SNAP, non-ARP, non-IP and non-OAM frames. 10463c83654fSVladimir Oltean * 10473c83654fSVladimir Oltean * If @on=true, then the above frame types (SNAP, ARP, IP and OAM) will match 10483c83654fSVladimir Oltean * on MAC_ETYPE keys such as destination and source MAC on this ingress port. 10493c83654fSVladimir Oltean * However the setting has the side effect of making these frames not matching 10503c83654fSVladimir Oltean * on any _other_ keys than MAC_ETYPE ones. 10513c83654fSVladimir Oltean */ 10523c83654fSVladimir Oltean static void ocelot_match_all_as_mac_etype(struct ocelot *ocelot, int port, 1053f854e6f6SVladimir Oltean int lookup, bool on) 10543c83654fSVladimir Oltean { 10553c83654fSVladimir Oltean u32 val = 0; 10563c83654fSVladimir Oltean 10573c83654fSVladimir Oltean if (on) 1058f854e6f6SVladimir Oltean val = ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(BIT(lookup)) | 1059f854e6f6SVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(BIT(lookup)) | 1060f854e6f6SVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(BIT(lookup)) | 1061f854e6f6SVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(BIT(lookup)) | 1062f854e6f6SVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(BIT(lookup)); 10633c83654fSVladimir Oltean 10643c83654fSVladimir Oltean ocelot_rmw_gix(ocelot, val, 1065f854e6f6SVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(BIT(lookup)) | 1066f854e6f6SVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(BIT(lookup)) | 1067f854e6f6SVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(BIT(lookup)) | 1068f854e6f6SVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(BIT(lookup)) | 1069f854e6f6SVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(BIT(lookup)), 10703c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG, port); 10713c83654fSVladimir Oltean } 10723c83654fSVladimir Oltean 1073aae4e500SVladimir Oltean static bool 1074aae4e500SVladimir Oltean ocelot_vcap_is_problematic_mac_etype(struct ocelot_vcap_filter *filter) 10753c83654fSVladimir Oltean { 10763c83654fSVladimir Oltean u16 proto, mask; 10773c83654fSVladimir Oltean 1078aae4e500SVladimir Oltean if (filter->key_type != OCELOT_VCAP_KEY_ETYPE) 10793c83654fSVladimir Oltean return false; 10803c83654fSVladimir Oltean 1081aae4e500SVladimir Oltean proto = ntohs(*(__be16 *)filter->key.etype.etype.value); 1082aae4e500SVladimir Oltean mask = ntohs(*(__be16 *)filter->key.etype.etype.mask); 10833c83654fSVladimir Oltean 10843c83654fSVladimir Oltean /* ETH_P_ALL match, so all protocols below are included */ 10853c83654fSVladimir Oltean if (mask == 0) 10863c83654fSVladimir Oltean return true; 10873c83654fSVladimir Oltean if (proto == ETH_P_ARP) 10883c83654fSVladimir Oltean return true; 10893c83654fSVladimir Oltean if (proto == ETH_P_IP) 10903c83654fSVladimir Oltean return true; 10913c83654fSVladimir Oltean if (proto == ETH_P_IPV6) 10923c83654fSVladimir Oltean return true; 10933c83654fSVladimir Oltean 10943c83654fSVladimir Oltean return false; 10953c83654fSVladimir Oltean } 10963c83654fSVladimir Oltean 1097aae4e500SVladimir Oltean static bool 1098aae4e500SVladimir Oltean ocelot_vcap_is_problematic_non_mac_etype(struct ocelot_vcap_filter *filter) 10993c83654fSVladimir Oltean { 1100aae4e500SVladimir Oltean if (filter->key_type == OCELOT_VCAP_KEY_SNAP) 11013c83654fSVladimir Oltean return true; 1102aae4e500SVladimir Oltean if (filter->key_type == OCELOT_VCAP_KEY_ARP) 11033c83654fSVladimir Oltean return true; 1104aae4e500SVladimir Oltean if (filter->key_type == OCELOT_VCAP_KEY_IPV4) 11053c83654fSVladimir Oltean return true; 1106aae4e500SVladimir Oltean if (filter->key_type == OCELOT_VCAP_KEY_IPV6) 11073c83654fSVladimir Oltean return true; 11083c83654fSVladimir Oltean return false; 11093c83654fSVladimir Oltean } 11103c83654fSVladimir Oltean 1111aae4e500SVladimir Oltean static bool 1112aae4e500SVladimir Oltean ocelot_exclusive_mac_etype_filter_rules(struct ocelot *ocelot, 1113aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 11143c83654fSVladimir Oltean { 11151397a2ebSVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[filter->block_id]; 1116aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 11173c83654fSVladimir Oltean unsigned long port; 11183c83654fSVladimir Oltean int i; 11193c83654fSVladimir Oltean 1120f854e6f6SVladimir Oltean /* We only have the S2_IP_TCPUDP_DIS set of knobs for VCAP IS2 */ 1121f854e6f6SVladimir Oltean if (filter->block_id != VCAP_IS2) 1122f854e6f6SVladimir Oltean return true; 1123f854e6f6SVladimir Oltean 1124aae4e500SVladimir Oltean if (ocelot_vcap_is_problematic_mac_etype(filter)) { 11253c83654fSVladimir Oltean /* Search for any non-MAC_ETYPE rules on the port */ 11263c83654fSVladimir Oltean for (i = 0; i < block->count; i++) { 1127085f5b91SVladimir Oltean tmp = ocelot_vcap_block_find_filter_by_index(block, i); 1128aae4e500SVladimir Oltean if (tmp->ingress_port_mask & filter->ingress_port_mask && 1129f854e6f6SVladimir Oltean tmp->lookup == filter->lookup && 1130aae4e500SVladimir Oltean ocelot_vcap_is_problematic_non_mac_etype(tmp)) 11313c83654fSVladimir Oltean return false; 11323c83654fSVladimir Oltean } 11333c83654fSVladimir Oltean 1134aae4e500SVladimir Oltean for_each_set_bit(port, &filter->ingress_port_mask, 11353c83654fSVladimir Oltean ocelot->num_phys_ports) 1136f854e6f6SVladimir Oltean ocelot_match_all_as_mac_etype(ocelot, port, 1137f854e6f6SVladimir Oltean filter->lookup, true); 1138aae4e500SVladimir Oltean } else if (ocelot_vcap_is_problematic_non_mac_etype(filter)) { 11393c83654fSVladimir Oltean /* Search for any MAC_ETYPE rules on the port */ 11403c83654fSVladimir Oltean for (i = 0; i < block->count; i++) { 1141085f5b91SVladimir Oltean tmp = ocelot_vcap_block_find_filter_by_index(block, i); 1142aae4e500SVladimir Oltean if (tmp->ingress_port_mask & filter->ingress_port_mask && 1143f854e6f6SVladimir Oltean tmp->lookup == filter->lookup && 1144aae4e500SVladimir Oltean ocelot_vcap_is_problematic_mac_etype(tmp)) 11453c83654fSVladimir Oltean return false; 11463c83654fSVladimir Oltean } 11473c83654fSVladimir Oltean 1148aae4e500SVladimir Oltean for_each_set_bit(port, &filter->ingress_port_mask, 11493c83654fSVladimir Oltean ocelot->num_phys_ports) 1150f854e6f6SVladimir Oltean ocelot_match_all_as_mac_etype(ocelot, port, 1151f854e6f6SVladimir Oltean filter->lookup, false); 11523c83654fSVladimir Oltean } 11533c83654fSVladimir Oltean 11543c83654fSVladimir Oltean return true; 11553c83654fSVladimir Oltean } 11563c83654fSVladimir Oltean 1157aae4e500SVladimir Oltean int ocelot_vcap_filter_add(struct ocelot *ocelot, 1158aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter, 11593c83654fSVladimir Oltean struct netlink_ext_ack *extack) 11603c83654fSVladimir Oltean { 11611397a2ebSVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[filter->block_id]; 116277043c37SXiaoliang Yang int i, index, ret; 11633c83654fSVladimir Oltean 1164aae4e500SVladimir Oltean if (!ocelot_exclusive_mac_etype_filter_rules(ocelot, filter)) { 11653c83654fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 1166f854e6f6SVladimir Oltean "Cannot mix MAC_ETYPE with non-MAC_ETYPE rules, use the other IS2 lookup"); 11673c83654fSVladimir Oltean return -EBUSY; 11683c83654fSVladimir Oltean } 11693c83654fSVladimir Oltean 1170aae4e500SVladimir Oltean /* Add filter to the linked list */ 117177043c37SXiaoliang Yang ret = ocelot_vcap_filter_add_to_block(ocelot, block, filter); 117277043c37SXiaoliang Yang if (ret) 117377043c37SXiaoliang Yang return ret; 11743c83654fSVladimir Oltean 1175aae4e500SVladimir Oltean /* Get the index of the inserted filter */ 1176aae4e500SVladimir Oltean index = ocelot_vcap_block_get_filter_index(block, filter); 1177ed5672d8SXiaoliang Yang if (index < 0) 1178ed5672d8SXiaoliang Yang return index; 11793c83654fSVladimir Oltean 1180aae4e500SVladimir Oltean /* Move down the rules to make place for the new filter */ 11813c83654fSVladimir Oltean for (i = block->count - 1; i > index; i--) { 1182aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 1183aae4e500SVladimir Oltean 1184085f5b91SVladimir Oltean tmp = ocelot_vcap_block_find_filter_by_index(block, i); 11851397a2ebSVladimir Oltean vcap_entry_set(ocelot, i, tmp); 11863c83654fSVladimir Oltean } 11873c83654fSVladimir Oltean 1188aae4e500SVladimir Oltean /* Now insert the new filter */ 11891397a2ebSVladimir Oltean vcap_entry_set(ocelot, index, filter); 11903c83654fSVladimir Oltean return 0; 11913c83654fSVladimir Oltean } 11920e9bb4e9SVladimir Oltean EXPORT_SYMBOL(ocelot_vcap_filter_add); 11933c83654fSVladimir Oltean 1194aae4e500SVladimir Oltean static void ocelot_vcap_block_remove_filter(struct ocelot *ocelot, 1195aae4e500SVladimir Oltean struct ocelot_vcap_block *block, 1196aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 11973c83654fSVladimir Oltean { 1198*c5a0edaeSVladimir Oltean struct ocelot_vcap_filter *tmp, *n; 11993c83654fSVladimir Oltean 1200*c5a0edaeSVladimir Oltean list_for_each_entry_safe(tmp, n, &block->rules, list) { 120150c6cc5bSVladimir Oltean if (ocelot_vcap_filter_equal(filter, tmp)) { 12021397a2ebSVladimir Oltean if (tmp->block_id == VCAP_IS2 && 12031397a2ebSVladimir Oltean tmp->action.police_ena) 120477043c37SXiaoliang Yang ocelot_vcap_policer_del(ocelot, 1205ea9d1f30SVladimir Oltean tmp->action.pol_ix); 12063c83654fSVladimir Oltean 1207*c5a0edaeSVladimir Oltean list_del(&tmp->list); 12083c83654fSVladimir Oltean kfree(tmp); 12093c83654fSVladimir Oltean } 12103c83654fSVladimir Oltean } 12113c83654fSVladimir Oltean 12123c83654fSVladimir Oltean block->count--; 12133c83654fSVladimir Oltean } 12143c83654fSVladimir Oltean 1215aae4e500SVladimir Oltean int ocelot_vcap_filter_del(struct ocelot *ocelot, 1216aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 12173c83654fSVladimir Oltean { 12181397a2ebSVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[filter->block_id]; 1219aae4e500SVladimir Oltean struct ocelot_vcap_filter del_filter; 12203c83654fSVladimir Oltean int i, index; 12213c83654fSVladimir Oltean 1222aae4e500SVladimir Oltean memset(&del_filter, 0, sizeof(del_filter)); 12233c83654fSVladimir Oltean 1224aae4e500SVladimir Oltean /* Gets index of the filter */ 1225aae4e500SVladimir Oltean index = ocelot_vcap_block_get_filter_index(block, filter); 1226ed5672d8SXiaoliang Yang if (index < 0) 1227ed5672d8SXiaoliang Yang return index; 12283c83654fSVladimir Oltean 1229aae4e500SVladimir Oltean /* Delete filter */ 1230aae4e500SVladimir Oltean ocelot_vcap_block_remove_filter(ocelot, block, filter); 12313c83654fSVladimir Oltean 1232aae4e500SVladimir Oltean /* Move up all the blocks over the deleted filter */ 12333c83654fSVladimir Oltean for (i = index; i < block->count; i++) { 1234aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 1235aae4e500SVladimir Oltean 1236085f5b91SVladimir Oltean tmp = ocelot_vcap_block_find_filter_by_index(block, i); 12371397a2ebSVladimir Oltean vcap_entry_set(ocelot, i, tmp); 12383c83654fSVladimir Oltean } 12393c83654fSVladimir Oltean 1240aae4e500SVladimir Oltean /* Now delete the last filter, because it is duplicated */ 12411397a2ebSVladimir Oltean vcap_entry_set(ocelot, block->count, &del_filter); 12423c83654fSVladimir Oltean 12433c83654fSVladimir Oltean return 0; 12443c83654fSVladimir Oltean } 12450e9bb4e9SVladimir Oltean EXPORT_SYMBOL(ocelot_vcap_filter_del); 12463c83654fSVladimir Oltean 124795706be1SVladimir Oltean int ocelot_vcap_filter_replace(struct ocelot *ocelot, 124895706be1SVladimir Oltean struct ocelot_vcap_filter *filter) 124995706be1SVladimir Oltean { 125095706be1SVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[filter->block_id]; 125195706be1SVladimir Oltean int index; 125295706be1SVladimir Oltean 125395706be1SVladimir Oltean index = ocelot_vcap_block_get_filter_index(block, filter); 125495706be1SVladimir Oltean if (index < 0) 125595706be1SVladimir Oltean return index; 125695706be1SVladimir Oltean 125795706be1SVladimir Oltean vcap_entry_set(ocelot, index, filter); 125895706be1SVladimir Oltean 125995706be1SVladimir Oltean return 0; 126095706be1SVladimir Oltean } 126195706be1SVladimir Oltean EXPORT_SYMBOL(ocelot_vcap_filter_replace); 126295706be1SVladimir Oltean 1263aae4e500SVladimir Oltean int ocelot_vcap_filter_stats_update(struct ocelot *ocelot, 1264aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 12653c83654fSVladimir Oltean { 12661397a2ebSVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[filter->block_id]; 126798642d1aSVladimir Oltean struct ocelot_vcap_filter tmp; 12683c83654fSVladimir Oltean int index; 12693c83654fSVladimir Oltean 1270aae4e500SVladimir Oltean index = ocelot_vcap_block_get_filter_index(block, filter); 1271ed5672d8SXiaoliang Yang if (index < 0) 1272ed5672d8SXiaoliang Yang return index; 1273ed5672d8SXiaoliang Yang 12741397a2ebSVladimir Oltean vcap_entry_get(ocelot, index, filter); 12753c83654fSVladimir Oltean 12763c83654fSVladimir Oltean /* After we get the result we need to clear the counters */ 127798642d1aSVladimir Oltean tmp = *filter; 127898642d1aSVladimir Oltean tmp.stats.pkts = 0; 12791397a2ebSVladimir Oltean vcap_entry_set(ocelot, index, &tmp); 12803c83654fSVladimir Oltean 12813c83654fSVladimir Oltean return 0; 12823c83654fSVladimir Oltean } 12833c83654fSVladimir Oltean 1284c1c3993eSVladimir Oltean static void ocelot_vcap_init_one(struct ocelot *ocelot, 1285c1c3993eSVladimir Oltean const struct vcap_props *vcap) 12863c83654fSVladimir Oltean { 12873c83654fSVladimir Oltean struct vcap_data data; 12883c83654fSVladimir Oltean 12893c83654fSVladimir Oltean memset(&data, 0, sizeof(data)); 12903c83654fSVladimir Oltean 1291c1c3993eSVladimir Oltean vcap_entry2cache(ocelot, vcap, &data); 1292c1c3993eSVladimir Oltean ocelot_target_write(ocelot, vcap->target, vcap->entry_count, 1293c1c3993eSVladimir Oltean VCAP_CORE_MV_CFG); 1294c1c3993eSVladimir Oltean vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, VCAP_SEL_ENTRY); 12953c83654fSVladimir Oltean 1296c1c3993eSVladimir Oltean vcap_action2cache(ocelot, vcap, &data); 1297c1c3993eSVladimir Oltean ocelot_target_write(ocelot, vcap->target, vcap->action_count, 1298c1c3993eSVladimir Oltean VCAP_CORE_MV_CFG); 1299c1c3993eSVladimir Oltean vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, 13003c83654fSVladimir Oltean VCAP_SEL_ACTION | VCAP_SEL_COUNTER); 1301c1c3993eSVladimir Oltean } 1302c1c3993eSVladimir Oltean 130320968054SVladimir Oltean static void ocelot_vcap_detect_constants(struct ocelot *ocelot, 130420968054SVladimir Oltean struct vcap_props *vcap) 130520968054SVladimir Oltean { 130620968054SVladimir Oltean int counter_memory_width; 130720968054SVladimir Oltean int num_default_actions; 130820968054SVladimir Oltean int version; 130920968054SVladimir Oltean 131020968054SVladimir Oltean version = ocelot_target_read(ocelot, vcap->target, 131120968054SVladimir Oltean VCAP_CONST_VCAP_VER); 131220968054SVladimir Oltean /* Only version 0 VCAP supported for now */ 131320968054SVladimir Oltean if (WARN_ON(version != 0)) 131420968054SVladimir Oltean return; 131520968054SVladimir Oltean 131620968054SVladimir Oltean /* Width in bits of type-group field */ 131720968054SVladimir Oltean vcap->tg_width = ocelot_target_read(ocelot, vcap->target, 131820968054SVladimir Oltean VCAP_CONST_ENTRY_TG_WIDTH); 131920968054SVladimir Oltean /* Number of subwords per TCAM row */ 132020968054SVladimir Oltean vcap->sw_count = ocelot_target_read(ocelot, vcap->target, 132120968054SVladimir Oltean VCAP_CONST_ENTRY_SWCNT); 132220968054SVladimir Oltean /* Number of rows in TCAM. There can be this many full keys, or double 132320968054SVladimir Oltean * this number half keys, or 4 times this number quarter keys. 132420968054SVladimir Oltean */ 132520968054SVladimir Oltean vcap->entry_count = ocelot_target_read(ocelot, vcap->target, 132620968054SVladimir Oltean VCAP_CONST_ENTRY_CNT); 132720968054SVladimir Oltean /* Assuming there are 4 subwords per TCAM row, their layout in the 132820968054SVladimir Oltean * actual TCAM (not in the cache) would be: 132920968054SVladimir Oltean * 133020968054SVladimir Oltean * | SW 3 | TG 3 | SW 2 | TG 2 | SW 1 | TG 1 | SW 0 | TG 0 | 133120968054SVladimir Oltean * 133220968054SVladimir Oltean * (where SW=subword and TG=Type-Group). 133320968054SVladimir Oltean * 133420968054SVladimir Oltean * What VCAP_CONST_ENTRY_CNT is giving us is the width of one full TCAM 133520968054SVladimir Oltean * row. But when software accesses the TCAM through the cache 133620968054SVladimir Oltean * registers, the Type-Group values are written through another set of 133720968054SVladimir Oltean * registers VCAP_TG_DAT, and therefore, it appears as though the 4 133820968054SVladimir Oltean * subwords are contiguous in the cache memory. 133920968054SVladimir Oltean * Important mention: regardless of the number of key entries per row 134020968054SVladimir Oltean * (and therefore of key size: 1 full key or 2 half keys or 4 quarter 134120968054SVladimir Oltean * keys), software always has to configure 4 Type-Group values. For 134220968054SVladimir Oltean * example, in the case of 1 full key, the driver needs to set all 4 134320968054SVladimir Oltean * Type-Group to be full key. 134420968054SVladimir Oltean * 134520968054SVladimir Oltean * For this reason, we need to fix up the value that the hardware is 134620968054SVladimir Oltean * giving us. We don't actually care about the width of the entry in 134720968054SVladimir Oltean * the TCAM. What we care about is the width of the entry in the cache 134820968054SVladimir Oltean * registers, which is how we get to interact with it. And since the 134920968054SVladimir Oltean * VCAP_ENTRY_DAT cache registers access only the subwords and not the 135020968054SVladimir Oltean * Type-Groups, this means we need to subtract the width of the 135120968054SVladimir Oltean * Type-Groups when packing and unpacking key entry data in a TCAM row. 135220968054SVladimir Oltean */ 135320968054SVladimir Oltean vcap->entry_width = ocelot_target_read(ocelot, vcap->target, 135420968054SVladimir Oltean VCAP_CONST_ENTRY_WIDTH); 135520968054SVladimir Oltean vcap->entry_width -= vcap->tg_width * vcap->sw_count; 135620968054SVladimir Oltean num_default_actions = ocelot_target_read(ocelot, vcap->target, 135720968054SVladimir Oltean VCAP_CONST_ACTION_DEF_CNT); 135820968054SVladimir Oltean vcap->action_count = vcap->entry_count + num_default_actions; 135920968054SVladimir Oltean vcap->action_width = ocelot_target_read(ocelot, vcap->target, 136020968054SVladimir Oltean VCAP_CONST_ACTION_WIDTH); 136120968054SVladimir Oltean /* The width of the counter memory, this is the complete width of all 136220968054SVladimir Oltean * counter-fields associated with one full-word entry. There is one 136320968054SVladimir Oltean * counter per entry sub-word (see CAP_CORE::ENTRY_SWCNT for number of 136420968054SVladimir Oltean * subwords.) 136520968054SVladimir Oltean */ 136620968054SVladimir Oltean vcap->counter_words = vcap->sw_count; 136720968054SVladimir Oltean counter_memory_width = ocelot_target_read(ocelot, vcap->target, 136820968054SVladimir Oltean VCAP_CONST_CNT_WIDTH); 136920968054SVladimir Oltean vcap->counter_width = counter_memory_width / vcap->counter_words; 137020968054SVladimir Oltean } 137120968054SVladimir Oltean 1372c1c3993eSVladimir Oltean int ocelot_vcap_init(struct ocelot *ocelot) 1373c1c3993eSVladimir Oltean { 137420968054SVladimir Oltean int i; 13753c83654fSVladimir Oltean 13763c83654fSVladimir Oltean /* Create a policer that will drop the frames for the cpu. 13773c83654fSVladimir Oltean * This policer will be used as action in the acl rules to drop 13783c83654fSVladimir Oltean * frames. 13793c83654fSVladimir Oltean */ 13803c83654fSVladimir Oltean ocelot_write_gix(ocelot, 0x299, ANA_POL_MODE_CFG, 13813c83654fSVladimir Oltean OCELOT_POLICER_DISCARD); 13823c83654fSVladimir Oltean ocelot_write_gix(ocelot, 0x1, ANA_POL_PIR_CFG, 13833c83654fSVladimir Oltean OCELOT_POLICER_DISCARD); 13843c83654fSVladimir Oltean ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_PIR_STATE, 13853c83654fSVladimir Oltean OCELOT_POLICER_DISCARD); 13863c83654fSVladimir Oltean ocelot_write_gix(ocelot, 0x0, ANA_POL_CIR_CFG, 13873c83654fSVladimir Oltean OCELOT_POLICER_DISCARD); 13883c83654fSVladimir Oltean ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_CIR_STATE, 13893c83654fSVladimir Oltean OCELOT_POLICER_DISCARD); 13903c83654fSVladimir Oltean 139120968054SVladimir Oltean for (i = 0; i < OCELOT_NUM_VCAP_BLOCKS; i++) { 13921397a2ebSVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[i]; 139320968054SVladimir Oltean struct vcap_props *vcap = &ocelot->vcap[i]; 139420968054SVladimir Oltean 13951397a2ebSVladimir Oltean INIT_LIST_HEAD(&block->rules); 13961397a2ebSVladimir Oltean 139720968054SVladimir Oltean ocelot_vcap_detect_constants(ocelot, vcap); 139820968054SVladimir Oltean ocelot_vcap_init_one(ocelot, vcap); 139920968054SVladimir Oltean } 140020968054SVladimir Oltean 14011397a2ebSVladimir Oltean INIT_LIST_HEAD(&ocelot->dummy_rules); 1402e42bd4edSVladimir Oltean INIT_LIST_HEAD(&ocelot->traps); 140377043c37SXiaoliang Yang INIT_LIST_HEAD(&ocelot->vcap_pol.pol_list); 14043c83654fSVladimir Oltean 14053c83654fSVladimir Oltean return 0; 14063c83654fSVladimir Oltean } 1407