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 370c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_PAG, 0, 0); 371c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_IGR_PORT_MASK, 0, 372aae4e500SVladimir Oltean ~filter->ingress_port_mask); 373c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_FIRST, OCELOT_VCAP_BIT_ANY); 374c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_HOST_MATCH, 3753c83654fSVladimir Oltean OCELOT_VCAP_BIT_ANY); 376c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_MC, filter->dmac_mc); 377c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L2_BC, filter->dmac_bc); 378c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_VLAN_TAGGED, tag->tagged); 379c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_VID, 3803c83654fSVladimir Oltean tag->vid.value, tag->vid.mask); 381c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_PCP, 3823c83654fSVladimir Oltean tag->pcp.value[0], tag->pcp.mask[0]); 383c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DEI, tag->dei); 3843c83654fSVladimir Oltean 385aae4e500SVladimir Oltean switch (filter->key_type) { 386aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_ETYPE: { 387aae4e500SVladimir Oltean struct ocelot_vcap_key_etype *etype = &filter->key.etype; 3883c83654fSVladimir Oltean 3893c83654fSVladimir Oltean type = IS2_TYPE_ETYPE; 390c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC, 3913c83654fSVladimir Oltean etype->dmac.value, etype->dmac.mask); 392c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC, 3933c83654fSVladimir Oltean etype->smac.value, etype->smac.mask); 394c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_ETYPE, 3953c83654fSVladimir Oltean etype->etype.value, etype->etype.mask); 3963c83654fSVladimir Oltean /* Clear unused bits */ 397c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0, 3983c83654fSVladimir Oltean 0, 0); 399c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1, 4003c83654fSVladimir Oltean 0, 0); 401c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2, 4023c83654fSVladimir Oltean 0, 0); 403c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, 4043c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0, 4053c83654fSVladimir Oltean etype->data.value, etype->data.mask); 4063c83654fSVladimir Oltean break; 4073c83654fSVladimir Oltean } 408aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_LLC: { 409aae4e500SVladimir Oltean struct ocelot_vcap_key_llc *llc = &filter->key.llc; 4103c83654fSVladimir Oltean 4113c83654fSVladimir Oltean type = IS2_TYPE_LLC; 412c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC, 4133c83654fSVladimir Oltean llc->dmac.value, llc->dmac.mask); 414c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC, 4153c83654fSVladimir Oltean llc->smac.value, llc->smac.mask); 4163c83654fSVladimir Oltean for (i = 0; i < 4; i++) { 4173c83654fSVladimir Oltean payload.value[i] = llc->llc.value[i]; 4183c83654fSVladimir Oltean payload.mask[i] = llc->llc.mask[i]; 4193c83654fSVladimir Oltean } 420c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_LLC_L2_LLC, 4213c83654fSVladimir Oltean payload.value, payload.mask); 4223c83654fSVladimir Oltean break; 4233c83654fSVladimir Oltean } 424aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_SNAP: { 425aae4e500SVladimir Oltean struct ocelot_vcap_key_snap *snap = &filter->key.snap; 4263c83654fSVladimir Oltean 4273c83654fSVladimir Oltean type = IS2_TYPE_SNAP; 428c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_DMAC, 4293c83654fSVladimir Oltean snap->dmac.value, snap->dmac.mask); 430c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L2_SMAC, 4313c83654fSVladimir Oltean snap->smac.value, snap->smac.mask); 432c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_SNAP_L2_SNAP, 433aae4e500SVladimir Oltean filter->key.snap.snap.value, 434aae4e500SVladimir Oltean filter->key.snap.snap.mask); 4353c83654fSVladimir Oltean break; 4363c83654fSVladimir Oltean } 437aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_ARP: { 438aae4e500SVladimir Oltean struct ocelot_vcap_key_arp *arp = &filter->key.arp; 4393c83654fSVladimir Oltean 4403c83654fSVladimir Oltean type = IS2_TYPE_ARP; 441c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_SMAC, 4423c83654fSVladimir Oltean arp->smac.value, arp->smac.mask); 443c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4443c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK, 4453c83654fSVladimir Oltean arp->ethernet); 446c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4473c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK, 4483c83654fSVladimir Oltean arp->ip); 449c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4503c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_LEN_OK, 4513c83654fSVladimir Oltean arp->length); 452c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4533c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_TARGET_MATCH, 4543c83654fSVladimir Oltean arp->dmac_match); 455c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4563c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_SENDER_MATCH, 4573c83654fSVladimir Oltean arp->smac_match); 458c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 4593c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN, 4603c83654fSVladimir Oltean arp->unknown); 4613c83654fSVladimir Oltean 4623c83654fSVladimir Oltean /* OPCODE is inverse, bit 0 is reply flag, bit 1 is RARP flag */ 4633c83654fSVladimir Oltean val = ((arp->req == OCELOT_VCAP_BIT_0 ? 1 : 0) | 4643c83654fSVladimir Oltean (arp->arp == OCELOT_VCAP_BIT_0 ? 2 : 0)); 4653c83654fSVladimir Oltean msk = ((arp->req == OCELOT_VCAP_BIT_ANY ? 0 : 1) | 4663c83654fSVladimir Oltean (arp->arp == OCELOT_VCAP_BIT_ANY ? 0 : 2)); 467c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_OPCODE, 4683c83654fSVladimir Oltean val, msk); 469c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, 4703c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP, 4713c83654fSVladimir Oltean arp->dip.value.addr, arp->dip.mask.addr); 472c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, 4733c83654fSVladimir Oltean VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP, 4743c83654fSVladimir Oltean arp->sip.value.addr, arp->sip.mask.addr); 475c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP, 4763c83654fSVladimir Oltean 0, 0); 4773c83654fSVladimir Oltean break; 4783c83654fSVladimir Oltean } 479aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_IPV4: 480aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_IPV6: { 4813c83654fSVladimir Oltean enum ocelot_vcap_bit sip_eq_dip, sport_eq_dport, seq_zero, tcp; 4823c83654fSVladimir Oltean enum ocelot_vcap_bit ttl, fragment, options, tcp_ack, tcp_urg; 4833c83654fSVladimir Oltean enum ocelot_vcap_bit tcp_fin, tcp_syn, tcp_rst, tcp_psh; 484aae4e500SVladimir Oltean struct ocelot_vcap_key_ipv4 *ipv4 = NULL; 485aae4e500SVladimir Oltean struct ocelot_vcap_key_ipv6 *ipv6 = NULL; 4863c83654fSVladimir Oltean struct ocelot_vcap_udp_tcp *sport, *dport; 4873c83654fSVladimir Oltean struct ocelot_vcap_ipv4 sip, dip; 4883c83654fSVladimir Oltean struct ocelot_vcap_u8 proto, ds; 4893c83654fSVladimir Oltean struct ocelot_vcap_u48 *ip_data; 4903c83654fSVladimir Oltean 491aae4e500SVladimir Oltean if (filter->key_type == OCELOT_VCAP_KEY_IPV4) { 492aae4e500SVladimir Oltean ipv4 = &filter->key.ipv4; 4933c83654fSVladimir Oltean ttl = ipv4->ttl; 4943c83654fSVladimir Oltean fragment = ipv4->fragment; 4953c83654fSVladimir Oltean options = ipv4->options; 4963c83654fSVladimir Oltean proto = ipv4->proto; 4973c83654fSVladimir Oltean ds = ipv4->ds; 4983c83654fSVladimir Oltean ip_data = &ipv4->data; 4993c83654fSVladimir Oltean sip = ipv4->sip; 5003c83654fSVladimir Oltean dip = ipv4->dip; 5013c83654fSVladimir Oltean sport = &ipv4->sport; 5023c83654fSVladimir Oltean dport = &ipv4->dport; 5033c83654fSVladimir Oltean tcp_fin = ipv4->tcp_fin; 5043c83654fSVladimir Oltean tcp_syn = ipv4->tcp_syn; 5053c83654fSVladimir Oltean tcp_rst = ipv4->tcp_rst; 5063c83654fSVladimir Oltean tcp_psh = ipv4->tcp_psh; 5073c83654fSVladimir Oltean tcp_ack = ipv4->tcp_ack; 5083c83654fSVladimir Oltean tcp_urg = ipv4->tcp_urg; 5093c83654fSVladimir Oltean sip_eq_dip = ipv4->sip_eq_dip; 5103c83654fSVladimir Oltean sport_eq_dport = ipv4->sport_eq_dport; 5113c83654fSVladimir Oltean seq_zero = ipv4->seq_zero; 5123c83654fSVladimir Oltean } else { 513aae4e500SVladimir Oltean ipv6 = &filter->key.ipv6; 5143c83654fSVladimir Oltean ttl = ipv6->ttl; 5153c83654fSVladimir Oltean fragment = OCELOT_VCAP_BIT_ANY; 5163c83654fSVladimir Oltean options = OCELOT_VCAP_BIT_ANY; 5173c83654fSVladimir Oltean proto = ipv6->proto; 5183c83654fSVladimir Oltean ds = ipv6->ds; 5193c83654fSVladimir Oltean ip_data = &ipv6->data; 5203c83654fSVladimir Oltean for (i = 0; i < 8; i++) { 5213c83654fSVladimir Oltean val = ipv6->sip.value[i + 8]; 5223c83654fSVladimir Oltean msk = ipv6->sip.mask[i + 8]; 5233c83654fSVladimir Oltean if (i < 4) { 5243c83654fSVladimir Oltean dip.value.addr[i] = val; 5253c83654fSVladimir Oltean dip.mask.addr[i] = msk; 5263c83654fSVladimir Oltean } else { 5273c83654fSVladimir Oltean sip.value.addr[i - 4] = val; 5283c83654fSVladimir Oltean sip.mask.addr[i - 4] = msk; 5293c83654fSVladimir Oltean } 5303c83654fSVladimir Oltean } 5313c83654fSVladimir Oltean sport = &ipv6->sport; 5323c83654fSVladimir Oltean dport = &ipv6->dport; 5333c83654fSVladimir Oltean tcp_fin = ipv6->tcp_fin; 5343c83654fSVladimir Oltean tcp_syn = ipv6->tcp_syn; 5353c83654fSVladimir Oltean tcp_rst = ipv6->tcp_rst; 5363c83654fSVladimir Oltean tcp_psh = ipv6->tcp_psh; 5373c83654fSVladimir Oltean tcp_ack = ipv6->tcp_ack; 5383c83654fSVladimir Oltean tcp_urg = ipv6->tcp_urg; 5393c83654fSVladimir Oltean sip_eq_dip = ipv6->sip_eq_dip; 5403c83654fSVladimir Oltean sport_eq_dport = ipv6->sport_eq_dport; 5413c83654fSVladimir Oltean seq_zero = ipv6->seq_zero; 5423c83654fSVladimir Oltean } 5433c83654fSVladimir Oltean 544c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4, 5453c83654fSVladimir Oltean ipv4 ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0); 546c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_FRAGMENT, 5473c83654fSVladimir Oltean fragment); 548c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_L3_FRAG_OFS_GT0, 0, 0); 549c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L3_OPTIONS, 5503c83654fSVladimir Oltean options); 551c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_IP4_L3_TTL_GT0, 5523c83654fSVladimir Oltean ttl); 553c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_TOS, 5543c83654fSVladimir Oltean ds.value, ds.mask); 555c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_DIP, 5563c83654fSVladimir Oltean dip.value.addr, dip.mask.addr); 557c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, VCAP_IS2_HK_L3_IP4_SIP, 5583c83654fSVladimir Oltean sip.value.addr, sip.mask.addr); 559c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_DIP_EQ_SIP, 5603c83654fSVladimir Oltean sip_eq_dip); 5613c83654fSVladimir Oltean val = proto.value[0]; 5623c83654fSVladimir Oltean msk = proto.mask[0]; 5633c83654fSVladimir Oltean type = IS2_TYPE_IP_UDP_TCP; 5643c83654fSVladimir Oltean if (msk == 0xff && (val == 6 || val == 17)) { 5653c83654fSVladimir Oltean /* UDP/TCP protocol match */ 5663c83654fSVladimir Oltean tcp = (val == 6 ? 5673c83654fSVladimir Oltean OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0); 568c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_TCP, tcp); 569c1c3993eSVladimir Oltean vcap_key_l4_port_set(vcap, &data, 5703c83654fSVladimir Oltean VCAP_IS2_HK_L4_DPORT, dport); 571c1c3993eSVladimir Oltean vcap_key_l4_port_set(vcap, &data, 5723c83654fSVladimir Oltean VCAP_IS2_HK_L4_SPORT, sport); 573c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_RNG, 0, 0); 574c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 5753c83654fSVladimir Oltean VCAP_IS2_HK_L4_SPORT_EQ_DPORT, 5763c83654fSVladimir Oltean sport_eq_dport); 577c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, 5783c83654fSVladimir Oltean VCAP_IS2_HK_L4_SEQUENCE_EQ0, 5793c83654fSVladimir Oltean seq_zero); 580c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_FIN, 5813c83654fSVladimir Oltean tcp_fin); 582c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_SYN, 5833c83654fSVladimir Oltean tcp_syn); 584c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_RST, 5853c83654fSVladimir Oltean tcp_rst); 586c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_PSH, 5873c83654fSVladimir Oltean tcp_psh); 588c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_ACK, 5893c83654fSVladimir Oltean tcp_ack); 590c1c3993eSVladimir Oltean vcap_key_bit_set(vcap, &data, VCAP_IS2_HK_L4_URG, 5913c83654fSVladimir Oltean tcp_urg); 592c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_DOM, 5933c83654fSVladimir Oltean 0, 0); 594c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_HK_L4_1588_VER, 5953c83654fSVladimir Oltean 0, 0); 5963c83654fSVladimir Oltean } else { 5973c83654fSVladimir Oltean if (msk == 0) { 5983c83654fSVladimir Oltean /* Any IP protocol match */ 5993c83654fSVladimir Oltean type_mask = IS2_TYPE_MASK_IP_ANY; 6003c83654fSVladimir Oltean } else { 6013c83654fSVladimir Oltean /* Non-UDP/TCP protocol match */ 6023c83654fSVladimir Oltean type = IS2_TYPE_IP_OTHER; 6033c83654fSVladimir Oltean for (i = 0; i < 6; i++) { 6043c83654fSVladimir Oltean payload.value[i] = ip_data->value[i]; 6053c83654fSVladimir Oltean payload.mask[i] = ip_data->mask[i]; 6063c83654fSVladimir Oltean } 6073c83654fSVladimir Oltean } 608c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, 6093c83654fSVladimir Oltean VCAP_IS2_HK_IP4_L3_PROTO, 6103c83654fSVladimir Oltean proto.value, proto.mask); 611c1c3993eSVladimir Oltean vcap_key_bytes_set(vcap, &data, 6123c83654fSVladimir Oltean VCAP_IS2_HK_L3_PAYLOAD, 6133c83654fSVladimir Oltean payload.value, payload.mask); 6143c83654fSVladimir Oltean } 6153c83654fSVladimir Oltean break; 6163c83654fSVladimir Oltean } 617aae4e500SVladimir Oltean case OCELOT_VCAP_KEY_ANY: 6183c83654fSVladimir Oltean default: 6193c83654fSVladimir Oltean type = 0; 6203c83654fSVladimir Oltean type_mask = 0; 621c1c3993eSVladimir Oltean count = vcap->entry_width / 2; 6223c83654fSVladimir Oltean /* Iterate over the non-common part of the key and 6233c83654fSVladimir Oltean * clear entry data 6243c83654fSVladimir Oltean */ 625c1c3993eSVladimir Oltean for (i = vcap->keys[VCAP_IS2_HK_L2_DMAC].offset; 6263c83654fSVladimir Oltean i < count; i += ENTRY_WIDTH) { 6273c83654fSVladimir Oltean vcap_key_field_set(&data, i, min(32u, count - i), 0, 0); 6283c83654fSVladimir Oltean } 6293c83654fSVladimir Oltean break; 6303c83654fSVladimir Oltean } 6313c83654fSVladimir Oltean 632c1c3993eSVladimir Oltean vcap_key_set(vcap, &data, VCAP_IS2_TYPE, type, type_mask); 633aae4e500SVladimir Oltean is2_action_set(ocelot, &data, filter); 6343c83654fSVladimir Oltean vcap_data_set(data.counter, data.counter_offset, 635c1c3993eSVladimir Oltean vcap->counter_width, filter->stats.pkts); 6363c83654fSVladimir Oltean 6373c83654fSVladimir Oltean /* Write row */ 638c1c3993eSVladimir Oltean vcap_entry2cache(ocelot, vcap, &data); 639c1c3993eSVladimir Oltean vcap_action2cache(ocelot, vcap, &data); 640c1c3993eSVladimir Oltean vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL); 6413c83654fSVladimir Oltean } 6423c83654fSVladimir Oltean 64375944fdaSXiaoliang Yang static void is1_action_set(struct ocelot *ocelot, struct vcap_data *data, 64475944fdaSXiaoliang Yang const struct ocelot_vcap_filter *filter) 64575944fdaSXiaoliang Yang { 64675944fdaSXiaoliang Yang const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1]; 64775944fdaSXiaoliang Yang const struct ocelot_vcap_action *a = &filter->action; 64875944fdaSXiaoliang Yang 64975944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_REPLACE_ENA, 65075944fdaSXiaoliang Yang a->vid_replace_ena); 65175944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_VID_ADD_VAL, a->vid); 65275944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT_ENA, 65375944fdaSXiaoliang Yang a->vlan_pop_cnt_ena); 65475944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_VLAN_POP_CNT, 65575944fdaSXiaoliang Yang a->vlan_pop_cnt); 65675944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_DEI_ENA, a->pcp_dei_ena); 65775944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_PCP_VAL, a->pcp); 65875944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_DEI_VAL, a->dei); 65975944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_ENA, a->qos_ena); 66075944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_QOS_VAL, a->qos_val); 66175944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_OVERRIDE_MASK, 66275944fdaSXiaoliang Yang a->pag_override_mask); 66375944fdaSXiaoliang Yang vcap_action_set(vcap, data, VCAP_IS1_ACT_PAG_VAL, a->pag_val); 66475944fdaSXiaoliang Yang } 66575944fdaSXiaoliang Yang 66675944fdaSXiaoliang Yang static void is1_entry_set(struct ocelot *ocelot, int ix, 66775944fdaSXiaoliang Yang struct ocelot_vcap_filter *filter) 66875944fdaSXiaoliang Yang { 66975944fdaSXiaoliang Yang const struct vcap_props *vcap = &ocelot->vcap[VCAP_IS1]; 67075944fdaSXiaoliang Yang struct ocelot_vcap_key_vlan *tag = &filter->vlan; 67175944fdaSXiaoliang Yang struct ocelot_vcap_u64 payload; 67275944fdaSXiaoliang Yang struct vcap_data data; 67375944fdaSXiaoliang Yang int row = ix / 2; 67475944fdaSXiaoliang Yang u32 type; 67575944fdaSXiaoliang Yang 67675944fdaSXiaoliang Yang memset(&payload, 0, sizeof(payload)); 67775944fdaSXiaoliang Yang memset(&data, 0, sizeof(data)); 67875944fdaSXiaoliang Yang 67975944fdaSXiaoliang Yang /* Read row */ 68075944fdaSXiaoliang Yang vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_ALL); 68175944fdaSXiaoliang Yang vcap_cache2entry(ocelot, vcap, &data); 68275944fdaSXiaoliang Yang vcap_cache2action(ocelot, vcap, &data); 68375944fdaSXiaoliang Yang 68475944fdaSXiaoliang Yang data.tg_sw = VCAP_TG_HALF; 68575944fdaSXiaoliang Yang data.type = IS1_ACTION_TYPE_NORMAL; 68675944fdaSXiaoliang Yang vcap_data_offset_get(vcap, &data, ix); 68775944fdaSXiaoliang Yang data.tg = (data.tg & ~data.tg_mask); 68875944fdaSXiaoliang Yang if (filter->prio != 0) 68975944fdaSXiaoliang Yang data.tg |= data.tg_value; 69075944fdaSXiaoliang Yang 69175944fdaSXiaoliang Yang vcap_key_set(vcap, &data, VCAP_IS1_HK_IGR_PORT_MASK, 0, 69275944fdaSXiaoliang Yang ~filter->ingress_port_mask); 69375944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_MC, filter->dmac_mc); 69475944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_L2_BC, filter->dmac_bc); 69575944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_VLAN_TAGGED, tag->tagged); 69675944fdaSXiaoliang Yang vcap_key_set(vcap, &data, VCAP_IS1_HK_VID, 69775944fdaSXiaoliang Yang tag->vid.value, tag->vid.mask); 69875944fdaSXiaoliang Yang vcap_key_set(vcap, &data, VCAP_IS1_HK_PCP, 69975944fdaSXiaoliang Yang tag->pcp.value[0], tag->pcp.mask[0]); 70075944fdaSXiaoliang Yang type = IS1_TYPE_S1_NORMAL; 70175944fdaSXiaoliang Yang 70275944fdaSXiaoliang Yang switch (filter->key_type) { 70375944fdaSXiaoliang Yang case OCELOT_VCAP_KEY_ETYPE: { 70475944fdaSXiaoliang Yang struct ocelot_vcap_key_etype *etype = &filter->key.etype; 70575944fdaSXiaoliang Yang 70675944fdaSXiaoliang Yang vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L2_SMAC, 70775944fdaSXiaoliang Yang etype->smac.value, etype->smac.mask); 70875944fdaSXiaoliang Yang vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE, 70975944fdaSXiaoliang Yang etype->etype.value, etype->etype.mask); 71075944fdaSXiaoliang Yang break; 71175944fdaSXiaoliang Yang } 71275944fdaSXiaoliang Yang case OCELOT_VCAP_KEY_IPV4: { 71375944fdaSXiaoliang Yang struct ocelot_vcap_key_ipv4 *ipv4 = &filter->key.ipv4; 71475944fdaSXiaoliang Yang struct ocelot_vcap_udp_tcp *sport = &ipv4->sport; 71575944fdaSXiaoliang Yang struct ocelot_vcap_udp_tcp *dport = &ipv4->dport; 71675944fdaSXiaoliang Yang enum ocelot_vcap_bit tcp_udp = OCELOT_VCAP_BIT_0; 71775944fdaSXiaoliang Yang struct ocelot_vcap_u8 proto = ipv4->proto; 71875944fdaSXiaoliang Yang struct ocelot_vcap_ipv4 sip = ipv4->sip; 71975944fdaSXiaoliang Yang u32 val, msk; 72075944fdaSXiaoliang Yang 72175944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP_SNAP, 72275944fdaSXiaoliang Yang OCELOT_VCAP_BIT_1); 72375944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_IP4, 72475944fdaSXiaoliang Yang OCELOT_VCAP_BIT_1); 72575944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_ETYPE_LEN, 72675944fdaSXiaoliang Yang OCELOT_VCAP_BIT_1); 72775944fdaSXiaoliang Yang vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_L3_IP4_SIP, 72875944fdaSXiaoliang Yang sip.value.addr, sip.mask.addr); 72975944fdaSXiaoliang Yang 73075944fdaSXiaoliang Yang val = proto.value[0]; 73175944fdaSXiaoliang Yang msk = proto.mask[0]; 73275944fdaSXiaoliang Yang 73375944fdaSXiaoliang Yang if ((val == NEXTHDR_TCP || val == NEXTHDR_UDP) && msk == 0xff) 73475944fdaSXiaoliang Yang tcp_udp = OCELOT_VCAP_BIT_1; 73575944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP_UDP, tcp_udp); 73675944fdaSXiaoliang Yang 73775944fdaSXiaoliang Yang if (tcp_udp) { 73875944fdaSXiaoliang Yang enum ocelot_vcap_bit tcp = OCELOT_VCAP_BIT_0; 73975944fdaSXiaoliang Yang 74075944fdaSXiaoliang Yang if (val == NEXTHDR_TCP) 74175944fdaSXiaoliang Yang tcp = OCELOT_VCAP_BIT_1; 74275944fdaSXiaoliang Yang 74375944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TCP, tcp); 74475944fdaSXiaoliang Yang vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_L4_SPORT, 74575944fdaSXiaoliang Yang sport); 74675944fdaSXiaoliang Yang /* Overloaded field */ 74775944fdaSXiaoliang Yang vcap_key_l4_port_set(vcap, &data, VCAP_IS1_HK_ETYPE, 74875944fdaSXiaoliang Yang dport); 74975944fdaSXiaoliang Yang } else { 75075944fdaSXiaoliang Yang /* IPv4 "other" frame */ 75175944fdaSXiaoliang Yang struct ocelot_vcap_u16 etype = {0}; 75275944fdaSXiaoliang Yang 75375944fdaSXiaoliang Yang /* Overloaded field */ 75475944fdaSXiaoliang Yang etype.value[0] = proto.value[0]; 75575944fdaSXiaoliang Yang etype.mask[0] = proto.mask[0]; 75675944fdaSXiaoliang Yang 75775944fdaSXiaoliang Yang vcap_key_bytes_set(vcap, &data, VCAP_IS1_HK_ETYPE, 75875944fdaSXiaoliang Yang etype.value, etype.mask); 75975944fdaSXiaoliang Yang } 76075944fdaSXiaoliang Yang } 76175944fdaSXiaoliang Yang default: 76275944fdaSXiaoliang Yang break; 76375944fdaSXiaoliang Yang } 76475944fdaSXiaoliang Yang vcap_key_bit_set(vcap, &data, VCAP_IS1_HK_TYPE, 76575944fdaSXiaoliang Yang type ? OCELOT_VCAP_BIT_1 : OCELOT_VCAP_BIT_0); 76675944fdaSXiaoliang Yang 76775944fdaSXiaoliang Yang is1_action_set(ocelot, &data, filter); 76875944fdaSXiaoliang Yang vcap_data_set(data.counter, data.counter_offset, 76975944fdaSXiaoliang Yang vcap->counter_width, filter->stats.pkts); 77075944fdaSXiaoliang Yang 77175944fdaSXiaoliang Yang /* Write row */ 77275944fdaSXiaoliang Yang vcap_entry2cache(ocelot, vcap, &data); 77375944fdaSXiaoliang Yang vcap_action2cache(ocelot, vcap, &data); 77475944fdaSXiaoliang Yang vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_WRITE, VCAP_SEL_ALL); 77575944fdaSXiaoliang Yang } 77675944fdaSXiaoliang Yang 7771397a2ebSVladimir Oltean static void vcap_entry_get(struct ocelot *ocelot, int ix, 7781397a2ebSVladimir Oltean struct ocelot_vcap_filter *filter) 7793c83654fSVladimir Oltean { 7801397a2ebSVladimir Oltean const struct vcap_props *vcap = &ocelot->vcap[filter->block_id]; 7813c83654fSVladimir Oltean struct vcap_data data; 782c1c3993eSVladimir Oltean int row, count; 7833c83654fSVladimir Oltean u32 cnt; 7843c83654fSVladimir Oltean 7853c83654fSVladimir Oltean data.tg_sw = VCAP_TG_HALF; 786c1c3993eSVladimir Oltean count = (1 << (data.tg_sw - 1)); 787c1c3993eSVladimir Oltean row = (ix / count); 788c1c3993eSVladimir Oltean vcap_row_cmd(ocelot, vcap, row, VCAP_CMD_READ, VCAP_SEL_COUNTER); 789c1c3993eSVladimir Oltean vcap_cache2action(ocelot, vcap, &data); 790c1c3993eSVladimir Oltean vcap_data_offset_get(vcap, &data, ix); 7913c83654fSVladimir Oltean cnt = vcap_data_get(data.counter, data.counter_offset, 792c1c3993eSVladimir Oltean vcap->counter_width); 7933c83654fSVladimir Oltean 794aae4e500SVladimir Oltean filter->stats.pkts = cnt; 7953c83654fSVladimir Oltean } 7963c83654fSVladimir Oltean 7971397a2ebSVladimir Oltean static void vcap_entry_set(struct ocelot *ocelot, int ix, 7981397a2ebSVladimir Oltean struct ocelot_vcap_filter *filter) 7991397a2ebSVladimir Oltean { 80075944fdaSXiaoliang Yang if (filter->block_id == VCAP_IS1) 80175944fdaSXiaoliang Yang return is1_entry_set(ocelot, ix, filter); 8021397a2ebSVladimir Oltean if (filter->block_id == VCAP_IS2) 8031397a2ebSVladimir Oltean return is2_entry_set(ocelot, ix, filter); 8041397a2ebSVladimir Oltean } 8051397a2ebSVladimir Oltean 806c73b0ad3SVladimir Oltean static int ocelot_vcap_policer_add(struct ocelot *ocelot, u32 pol_ix, 807c73b0ad3SVladimir Oltean struct ocelot_policer *pol) 808c73b0ad3SVladimir Oltean { 809c73b0ad3SVladimir Oltean struct qos_policer_conf pp = { 0 }; 810c73b0ad3SVladimir Oltean 811c73b0ad3SVladimir Oltean if (!pol) 812c73b0ad3SVladimir Oltean return -EINVAL; 813c73b0ad3SVladimir Oltean 814c73b0ad3SVladimir Oltean pp.mode = MSCC_QOS_RATE_MODE_DATA; 815c73b0ad3SVladimir Oltean pp.pir = pol->rate; 816c73b0ad3SVladimir Oltean pp.pbs = pol->burst; 817c73b0ad3SVladimir Oltean 818c73b0ad3SVladimir Oltean return qos_policer_conf_set(ocelot, 0, pol_ix, &pp); 819c73b0ad3SVladimir Oltean } 820c73b0ad3SVladimir Oltean 821c73b0ad3SVladimir Oltean static void ocelot_vcap_policer_del(struct ocelot *ocelot, 822c73b0ad3SVladimir Oltean struct ocelot_vcap_block *block, 823c73b0ad3SVladimir Oltean u32 pol_ix) 824c73b0ad3SVladimir Oltean { 825c73b0ad3SVladimir Oltean struct ocelot_vcap_filter *filter; 826c73b0ad3SVladimir Oltean struct qos_policer_conf pp = {0}; 827c73b0ad3SVladimir Oltean int index = -1; 828c73b0ad3SVladimir Oltean 829c73b0ad3SVladimir Oltean if (pol_ix < block->pol_lpr) 830c73b0ad3SVladimir Oltean return; 831c73b0ad3SVladimir Oltean 832c73b0ad3SVladimir Oltean list_for_each_entry(filter, &block->rules, list) { 833c73b0ad3SVladimir Oltean index++; 8341397a2ebSVladimir Oltean if (filter->block_id == VCAP_IS2 && 8351397a2ebSVladimir Oltean filter->action.police_ena && 836ea9d1f30SVladimir Oltean filter->action.pol_ix < pol_ix) { 837ea9d1f30SVladimir Oltean filter->action.pol_ix += 1; 838ea9d1f30SVladimir Oltean ocelot_vcap_policer_add(ocelot, filter->action.pol_ix, 839ea9d1f30SVladimir Oltean &filter->action.pol); 840c73b0ad3SVladimir Oltean is2_entry_set(ocelot, index, filter); 841c73b0ad3SVladimir Oltean } 842c73b0ad3SVladimir Oltean } 843c73b0ad3SVladimir Oltean 844c73b0ad3SVladimir Oltean pp.mode = MSCC_QOS_RATE_MODE_DISABLED; 845c73b0ad3SVladimir Oltean qos_policer_conf_set(ocelot, 0, pol_ix, &pp); 846c73b0ad3SVladimir Oltean 847c73b0ad3SVladimir Oltean block->pol_lpr++; 848c73b0ad3SVladimir Oltean } 849c73b0ad3SVladimir Oltean 850aae4e500SVladimir Oltean static void ocelot_vcap_filter_add_to_block(struct ocelot *ocelot, 851aae4e500SVladimir Oltean struct ocelot_vcap_block *block, 852aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 8533c83654fSVladimir Oltean { 854aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 8553c83654fSVladimir Oltean struct list_head *pos, *n; 8563c83654fSVladimir Oltean 8571397a2ebSVladimir Oltean if (filter->block_id == VCAP_IS2 && filter->action.police_ena) { 8583c83654fSVladimir Oltean block->pol_lpr--; 859ea9d1f30SVladimir Oltean filter->action.pol_ix = block->pol_lpr; 860ea9d1f30SVladimir Oltean ocelot_vcap_policer_add(ocelot, filter->action.pol_ix, 861ea9d1f30SVladimir Oltean &filter->action.pol); 8623c83654fSVladimir Oltean } 8633c83654fSVladimir Oltean 8643c83654fSVladimir Oltean block->count++; 8653c83654fSVladimir Oltean 8663c83654fSVladimir Oltean if (list_empty(&block->rules)) { 867aae4e500SVladimir Oltean list_add(&filter->list, &block->rules); 8683c83654fSVladimir Oltean return; 8693c83654fSVladimir Oltean } 8703c83654fSVladimir Oltean 8713c83654fSVladimir Oltean list_for_each_safe(pos, n, &block->rules) { 872aae4e500SVladimir Oltean tmp = list_entry(pos, struct ocelot_vcap_filter, list); 873aae4e500SVladimir Oltean if (filter->prio < tmp->prio) 8743c83654fSVladimir Oltean break; 8753c83654fSVladimir Oltean } 876aae4e500SVladimir Oltean list_add(&filter->list, pos->prev); 8773c83654fSVladimir Oltean } 8783c83654fSVladimir Oltean 879aae4e500SVladimir Oltean static int ocelot_vcap_block_get_filter_index(struct ocelot_vcap_block *block, 880aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 8813c83654fSVladimir Oltean { 882aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 883ed5672d8SXiaoliang Yang int index = 0; 8843c83654fSVladimir Oltean 8853c83654fSVladimir Oltean list_for_each_entry(tmp, &block->rules, list) { 886aae4e500SVladimir Oltean if (filter->id == tmp->id) 8873c83654fSVladimir Oltean return index; 888ed5672d8SXiaoliang Yang index++; 889ed5672d8SXiaoliang Yang } 890ed5672d8SXiaoliang Yang 891ed5672d8SXiaoliang Yang return -ENOENT; 8923c83654fSVladimir Oltean } 8933c83654fSVladimir Oltean 894aae4e500SVladimir Oltean static struct ocelot_vcap_filter* 895085f5b91SVladimir Oltean ocelot_vcap_block_find_filter_by_index(struct ocelot_vcap_block *block, 896aae4e500SVladimir Oltean int index) 8973c83654fSVladimir Oltean { 898aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 8993c83654fSVladimir Oltean int i = 0; 9003c83654fSVladimir Oltean 9013c83654fSVladimir Oltean list_for_each_entry(tmp, &block->rules, list) { 9023c83654fSVladimir Oltean if (i == index) 9033c83654fSVladimir Oltean return tmp; 9043c83654fSVladimir Oltean ++i; 9053c83654fSVladimir Oltean } 9063c83654fSVladimir Oltean 9073c83654fSVladimir Oltean return NULL; 9083c83654fSVladimir Oltean } 9093c83654fSVladimir Oltean 910085f5b91SVladimir Oltean struct ocelot_vcap_filter * 911085f5b91SVladimir Oltean ocelot_vcap_block_find_filter_by_id(struct ocelot_vcap_block *block, int id) 912085f5b91SVladimir Oltean { 913085f5b91SVladimir Oltean struct ocelot_vcap_filter *filter; 914085f5b91SVladimir Oltean 915085f5b91SVladimir Oltean list_for_each_entry(filter, &block->rules, list) 916085f5b91SVladimir Oltean if (filter->id == id) 917085f5b91SVladimir Oltean return filter; 918085f5b91SVladimir Oltean 919085f5b91SVladimir Oltean return NULL; 920085f5b91SVladimir Oltean } 921085f5b91SVladimir Oltean 9223c83654fSVladimir Oltean /* If @on=false, then SNAP, ARP, IP and OAM frames will not match on keys based 9233c83654fSVladimir Oltean * on destination and source MAC addresses, but only on higher-level protocol 9243c83654fSVladimir Oltean * information. The only frame types to match on keys containing MAC addresses 9253c83654fSVladimir Oltean * in this case are non-SNAP, non-ARP, non-IP and non-OAM frames. 9263c83654fSVladimir Oltean * 9273c83654fSVladimir Oltean * If @on=true, then the above frame types (SNAP, ARP, IP and OAM) will match 9283c83654fSVladimir Oltean * on MAC_ETYPE keys such as destination and source MAC on this ingress port. 9293c83654fSVladimir Oltean * However the setting has the side effect of making these frames not matching 9303c83654fSVladimir Oltean * on any _other_ keys than MAC_ETYPE ones. 9313c83654fSVladimir Oltean */ 9323c83654fSVladimir Oltean static void ocelot_match_all_as_mac_etype(struct ocelot *ocelot, int port, 9333c83654fSVladimir Oltean bool on) 9343c83654fSVladimir Oltean { 9353c83654fSVladimir Oltean u32 val = 0; 9363c83654fSVladimir Oltean 9373c83654fSVladimir Oltean if (on) 9383c83654fSVladimir Oltean val = ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS(3) | 9393c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS(3) | 9403c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS(3) | 9413c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS(3) | 9423c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS(3); 9433c83654fSVladimir Oltean 9443c83654fSVladimir Oltean ocelot_rmw_gix(ocelot, val, 9453c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_SNAP_DIS_M | 9463c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_ARP_DIS_M | 9473c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_IP_TCPUDP_DIS_M | 9483c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_IP_OTHER_DIS_M | 9493c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG_S2_OAM_DIS_M, 9503c83654fSVladimir Oltean ANA_PORT_VCAP_S2_CFG, port); 9513c83654fSVladimir Oltean } 9523c83654fSVladimir Oltean 953aae4e500SVladimir Oltean static bool 954aae4e500SVladimir Oltean ocelot_vcap_is_problematic_mac_etype(struct ocelot_vcap_filter *filter) 9553c83654fSVladimir Oltean { 9563c83654fSVladimir Oltean u16 proto, mask; 9573c83654fSVladimir Oltean 958aae4e500SVladimir Oltean if (filter->key_type != OCELOT_VCAP_KEY_ETYPE) 9593c83654fSVladimir Oltean return false; 9603c83654fSVladimir Oltean 961aae4e500SVladimir Oltean proto = ntohs(*(__be16 *)filter->key.etype.etype.value); 962aae4e500SVladimir Oltean mask = ntohs(*(__be16 *)filter->key.etype.etype.mask); 9633c83654fSVladimir Oltean 9643c83654fSVladimir Oltean /* ETH_P_ALL match, so all protocols below are included */ 9653c83654fSVladimir Oltean if (mask == 0) 9663c83654fSVladimir Oltean return true; 9673c83654fSVladimir Oltean if (proto == ETH_P_ARP) 9683c83654fSVladimir Oltean return true; 9693c83654fSVladimir Oltean if (proto == ETH_P_IP) 9703c83654fSVladimir Oltean return true; 9713c83654fSVladimir Oltean if (proto == ETH_P_IPV6) 9723c83654fSVladimir Oltean return true; 9733c83654fSVladimir Oltean 9743c83654fSVladimir Oltean return false; 9753c83654fSVladimir Oltean } 9763c83654fSVladimir Oltean 977aae4e500SVladimir Oltean static bool 978aae4e500SVladimir Oltean ocelot_vcap_is_problematic_non_mac_etype(struct ocelot_vcap_filter *filter) 9793c83654fSVladimir Oltean { 980aae4e500SVladimir Oltean if (filter->key_type == OCELOT_VCAP_KEY_SNAP) 9813c83654fSVladimir Oltean return true; 982aae4e500SVladimir Oltean if (filter->key_type == OCELOT_VCAP_KEY_ARP) 9833c83654fSVladimir Oltean return true; 984aae4e500SVladimir Oltean if (filter->key_type == OCELOT_VCAP_KEY_IPV4) 9853c83654fSVladimir Oltean return true; 986aae4e500SVladimir Oltean if (filter->key_type == OCELOT_VCAP_KEY_IPV6) 9873c83654fSVladimir Oltean return true; 9883c83654fSVladimir Oltean return false; 9893c83654fSVladimir Oltean } 9903c83654fSVladimir Oltean 991aae4e500SVladimir Oltean static bool 992aae4e500SVladimir Oltean ocelot_exclusive_mac_etype_filter_rules(struct ocelot *ocelot, 993aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 9943c83654fSVladimir Oltean { 9951397a2ebSVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[filter->block_id]; 996aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 9973c83654fSVladimir Oltean unsigned long port; 9983c83654fSVladimir Oltean int i; 9993c83654fSVladimir Oltean 1000aae4e500SVladimir Oltean if (ocelot_vcap_is_problematic_mac_etype(filter)) { 10013c83654fSVladimir Oltean /* Search for any non-MAC_ETYPE rules on the port */ 10023c83654fSVladimir Oltean for (i = 0; i < block->count; i++) { 1003085f5b91SVladimir Oltean tmp = ocelot_vcap_block_find_filter_by_index(block, i); 1004aae4e500SVladimir Oltean if (tmp->ingress_port_mask & filter->ingress_port_mask && 1005aae4e500SVladimir Oltean ocelot_vcap_is_problematic_non_mac_etype(tmp)) 10063c83654fSVladimir Oltean return false; 10073c83654fSVladimir Oltean } 10083c83654fSVladimir Oltean 1009aae4e500SVladimir Oltean for_each_set_bit(port, &filter->ingress_port_mask, 10103c83654fSVladimir Oltean ocelot->num_phys_ports) 10113c83654fSVladimir Oltean ocelot_match_all_as_mac_etype(ocelot, port, true); 1012aae4e500SVladimir Oltean } else if (ocelot_vcap_is_problematic_non_mac_etype(filter)) { 10133c83654fSVladimir Oltean /* Search for any MAC_ETYPE rules on the port */ 10143c83654fSVladimir Oltean for (i = 0; i < block->count; i++) { 1015085f5b91SVladimir Oltean tmp = ocelot_vcap_block_find_filter_by_index(block, i); 1016aae4e500SVladimir Oltean if (tmp->ingress_port_mask & filter->ingress_port_mask && 1017aae4e500SVladimir Oltean ocelot_vcap_is_problematic_mac_etype(tmp)) 10183c83654fSVladimir Oltean return false; 10193c83654fSVladimir Oltean } 10203c83654fSVladimir Oltean 1021aae4e500SVladimir Oltean for_each_set_bit(port, &filter->ingress_port_mask, 10223c83654fSVladimir Oltean ocelot->num_phys_ports) 10233c83654fSVladimir Oltean ocelot_match_all_as_mac_etype(ocelot, port, false); 10243c83654fSVladimir Oltean } 10253c83654fSVladimir Oltean 10263c83654fSVladimir Oltean return true; 10273c83654fSVladimir Oltean } 10283c83654fSVladimir Oltean 1029aae4e500SVladimir Oltean int ocelot_vcap_filter_add(struct ocelot *ocelot, 1030aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter, 10313c83654fSVladimir Oltean struct netlink_ext_ack *extack) 10323c83654fSVladimir Oltean { 10331397a2ebSVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[filter->block_id]; 10343c83654fSVladimir Oltean int i, index; 10353c83654fSVladimir Oltean 1036aae4e500SVladimir Oltean if (!ocelot_exclusive_mac_etype_filter_rules(ocelot, filter)) { 10373c83654fSVladimir Oltean NL_SET_ERR_MSG_MOD(extack, 10383c83654fSVladimir Oltean "Cannot mix MAC_ETYPE with non-MAC_ETYPE rules"); 10393c83654fSVladimir Oltean return -EBUSY; 10403c83654fSVladimir Oltean } 10413c83654fSVladimir Oltean 1042aae4e500SVladimir Oltean /* Add filter to the linked list */ 1043aae4e500SVladimir Oltean ocelot_vcap_filter_add_to_block(ocelot, block, filter); 10443c83654fSVladimir Oltean 1045aae4e500SVladimir Oltean /* Get the index of the inserted filter */ 1046aae4e500SVladimir Oltean index = ocelot_vcap_block_get_filter_index(block, filter); 1047ed5672d8SXiaoliang Yang if (index < 0) 1048ed5672d8SXiaoliang Yang return index; 10493c83654fSVladimir Oltean 1050aae4e500SVladimir Oltean /* Move down the rules to make place for the new filter */ 10513c83654fSVladimir Oltean for (i = block->count - 1; i > index; i--) { 1052aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 1053aae4e500SVladimir Oltean 1054085f5b91SVladimir Oltean tmp = ocelot_vcap_block_find_filter_by_index(block, i); 10551397a2ebSVladimir Oltean vcap_entry_set(ocelot, i, tmp); 10563c83654fSVladimir Oltean } 10573c83654fSVladimir Oltean 1058aae4e500SVladimir Oltean /* Now insert the new filter */ 10591397a2ebSVladimir Oltean vcap_entry_set(ocelot, index, filter); 10603c83654fSVladimir Oltean return 0; 10613c83654fSVladimir Oltean } 10623c83654fSVladimir Oltean 1063aae4e500SVladimir Oltean static void ocelot_vcap_block_remove_filter(struct ocelot *ocelot, 1064aae4e500SVladimir Oltean struct ocelot_vcap_block *block, 1065aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 10663c83654fSVladimir Oltean { 1067aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 10683c83654fSVladimir Oltean struct list_head *pos, *q; 10693c83654fSVladimir Oltean 10703c83654fSVladimir Oltean list_for_each_safe(pos, q, &block->rules) { 1071aae4e500SVladimir Oltean tmp = list_entry(pos, struct ocelot_vcap_filter, list); 1072aae4e500SVladimir Oltean if (tmp->id == filter->id) { 10731397a2ebSVladimir Oltean if (tmp->block_id == VCAP_IS2 && 10741397a2ebSVladimir Oltean tmp->action.police_ena) 1075c73b0ad3SVladimir Oltean ocelot_vcap_policer_del(ocelot, block, 1076ea9d1f30SVladimir Oltean tmp->action.pol_ix); 10773c83654fSVladimir Oltean 10783c83654fSVladimir Oltean list_del(pos); 10793c83654fSVladimir Oltean kfree(tmp); 10803c83654fSVladimir Oltean } 10813c83654fSVladimir Oltean } 10823c83654fSVladimir Oltean 10833c83654fSVladimir Oltean block->count--; 10843c83654fSVladimir Oltean } 10853c83654fSVladimir Oltean 1086aae4e500SVladimir Oltean int ocelot_vcap_filter_del(struct ocelot *ocelot, 1087aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 10883c83654fSVladimir Oltean { 10891397a2ebSVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[filter->block_id]; 1090aae4e500SVladimir Oltean struct ocelot_vcap_filter del_filter; 10913c83654fSVladimir Oltean int i, index; 10923c83654fSVladimir Oltean 1093aae4e500SVladimir Oltean memset(&del_filter, 0, sizeof(del_filter)); 10943c83654fSVladimir Oltean 1095aae4e500SVladimir Oltean /* Gets index of the filter */ 1096aae4e500SVladimir Oltean index = ocelot_vcap_block_get_filter_index(block, filter); 1097ed5672d8SXiaoliang Yang if (index < 0) 1098ed5672d8SXiaoliang Yang return index; 10993c83654fSVladimir Oltean 1100aae4e500SVladimir Oltean /* Delete filter */ 1101aae4e500SVladimir Oltean ocelot_vcap_block_remove_filter(ocelot, block, filter); 11023c83654fSVladimir Oltean 1103aae4e500SVladimir Oltean /* Move up all the blocks over the deleted filter */ 11043c83654fSVladimir Oltean for (i = index; i < block->count; i++) { 1105aae4e500SVladimir Oltean struct ocelot_vcap_filter *tmp; 1106aae4e500SVladimir Oltean 1107085f5b91SVladimir Oltean tmp = ocelot_vcap_block_find_filter_by_index(block, i); 11081397a2ebSVladimir Oltean vcap_entry_set(ocelot, i, tmp); 11093c83654fSVladimir Oltean } 11103c83654fSVladimir Oltean 1111aae4e500SVladimir Oltean /* Now delete the last filter, because it is duplicated */ 11121397a2ebSVladimir Oltean vcap_entry_set(ocelot, block->count, &del_filter); 11133c83654fSVladimir Oltean 11143c83654fSVladimir Oltean return 0; 11153c83654fSVladimir Oltean } 11163c83654fSVladimir Oltean 1117aae4e500SVladimir Oltean int ocelot_vcap_filter_stats_update(struct ocelot *ocelot, 1118aae4e500SVladimir Oltean struct ocelot_vcap_filter *filter) 11193c83654fSVladimir Oltean { 11201397a2ebSVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[filter->block_id]; 112198642d1aSVladimir Oltean struct ocelot_vcap_filter tmp; 11223c83654fSVladimir Oltean int index; 11233c83654fSVladimir Oltean 1124aae4e500SVladimir Oltean index = ocelot_vcap_block_get_filter_index(block, filter); 1125ed5672d8SXiaoliang Yang if (index < 0) 1126ed5672d8SXiaoliang Yang return index; 1127ed5672d8SXiaoliang Yang 11281397a2ebSVladimir Oltean vcap_entry_get(ocelot, index, filter); 11293c83654fSVladimir Oltean 11303c83654fSVladimir Oltean /* After we get the result we need to clear the counters */ 113198642d1aSVladimir Oltean tmp = *filter; 113298642d1aSVladimir Oltean tmp.stats.pkts = 0; 11331397a2ebSVladimir Oltean vcap_entry_set(ocelot, index, &tmp); 11343c83654fSVladimir Oltean 11353c83654fSVladimir Oltean return 0; 11363c83654fSVladimir Oltean } 11373c83654fSVladimir Oltean 1138c1c3993eSVladimir Oltean static void ocelot_vcap_init_one(struct ocelot *ocelot, 1139c1c3993eSVladimir Oltean const struct vcap_props *vcap) 11403c83654fSVladimir Oltean { 11413c83654fSVladimir Oltean struct vcap_data data; 11423c83654fSVladimir Oltean 11433c83654fSVladimir Oltean memset(&data, 0, sizeof(data)); 11443c83654fSVladimir Oltean 1145c1c3993eSVladimir Oltean vcap_entry2cache(ocelot, vcap, &data); 1146c1c3993eSVladimir Oltean ocelot_target_write(ocelot, vcap->target, vcap->entry_count, 1147c1c3993eSVladimir Oltean VCAP_CORE_MV_CFG); 1148c1c3993eSVladimir Oltean vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, VCAP_SEL_ENTRY); 11493c83654fSVladimir Oltean 1150c1c3993eSVladimir Oltean vcap_action2cache(ocelot, vcap, &data); 1151c1c3993eSVladimir Oltean ocelot_target_write(ocelot, vcap->target, vcap->action_count, 1152c1c3993eSVladimir Oltean VCAP_CORE_MV_CFG); 1153c1c3993eSVladimir Oltean vcap_cmd(ocelot, vcap, 0, VCAP_CMD_INITIALIZE, 11543c83654fSVladimir Oltean VCAP_SEL_ACTION | VCAP_SEL_COUNTER); 1155c1c3993eSVladimir Oltean } 1156c1c3993eSVladimir Oltean 115720968054SVladimir Oltean static void ocelot_vcap_detect_constants(struct ocelot *ocelot, 115820968054SVladimir Oltean struct vcap_props *vcap) 115920968054SVladimir Oltean { 116020968054SVladimir Oltean int counter_memory_width; 116120968054SVladimir Oltean int num_default_actions; 116220968054SVladimir Oltean int version; 116320968054SVladimir Oltean 116420968054SVladimir Oltean version = ocelot_target_read(ocelot, vcap->target, 116520968054SVladimir Oltean VCAP_CONST_VCAP_VER); 116620968054SVladimir Oltean /* Only version 0 VCAP supported for now */ 116720968054SVladimir Oltean if (WARN_ON(version != 0)) 116820968054SVladimir Oltean return; 116920968054SVladimir Oltean 117020968054SVladimir Oltean /* Width in bits of type-group field */ 117120968054SVladimir Oltean vcap->tg_width = ocelot_target_read(ocelot, vcap->target, 117220968054SVladimir Oltean VCAP_CONST_ENTRY_TG_WIDTH); 117320968054SVladimir Oltean /* Number of subwords per TCAM row */ 117420968054SVladimir Oltean vcap->sw_count = ocelot_target_read(ocelot, vcap->target, 117520968054SVladimir Oltean VCAP_CONST_ENTRY_SWCNT); 117620968054SVladimir Oltean /* Number of rows in TCAM. There can be this many full keys, or double 117720968054SVladimir Oltean * this number half keys, or 4 times this number quarter keys. 117820968054SVladimir Oltean */ 117920968054SVladimir Oltean vcap->entry_count = ocelot_target_read(ocelot, vcap->target, 118020968054SVladimir Oltean VCAP_CONST_ENTRY_CNT); 118120968054SVladimir Oltean /* Assuming there are 4 subwords per TCAM row, their layout in the 118220968054SVladimir Oltean * actual TCAM (not in the cache) would be: 118320968054SVladimir Oltean * 118420968054SVladimir Oltean * | SW 3 | TG 3 | SW 2 | TG 2 | SW 1 | TG 1 | SW 0 | TG 0 | 118520968054SVladimir Oltean * 118620968054SVladimir Oltean * (where SW=subword and TG=Type-Group). 118720968054SVladimir Oltean * 118820968054SVladimir Oltean * What VCAP_CONST_ENTRY_CNT is giving us is the width of one full TCAM 118920968054SVladimir Oltean * row. But when software accesses the TCAM through the cache 119020968054SVladimir Oltean * registers, the Type-Group values are written through another set of 119120968054SVladimir Oltean * registers VCAP_TG_DAT, and therefore, it appears as though the 4 119220968054SVladimir Oltean * subwords are contiguous in the cache memory. 119320968054SVladimir Oltean * Important mention: regardless of the number of key entries per row 119420968054SVladimir Oltean * (and therefore of key size: 1 full key or 2 half keys or 4 quarter 119520968054SVladimir Oltean * keys), software always has to configure 4 Type-Group values. For 119620968054SVladimir Oltean * example, in the case of 1 full key, the driver needs to set all 4 119720968054SVladimir Oltean * Type-Group to be full key. 119820968054SVladimir Oltean * 119920968054SVladimir Oltean * For this reason, we need to fix up the value that the hardware is 120020968054SVladimir Oltean * giving us. We don't actually care about the width of the entry in 120120968054SVladimir Oltean * the TCAM. What we care about is the width of the entry in the cache 120220968054SVladimir Oltean * registers, which is how we get to interact with it. And since the 120320968054SVladimir Oltean * VCAP_ENTRY_DAT cache registers access only the subwords and not the 120420968054SVladimir Oltean * Type-Groups, this means we need to subtract the width of the 120520968054SVladimir Oltean * Type-Groups when packing and unpacking key entry data in a TCAM row. 120620968054SVladimir Oltean */ 120720968054SVladimir Oltean vcap->entry_width = ocelot_target_read(ocelot, vcap->target, 120820968054SVladimir Oltean VCAP_CONST_ENTRY_WIDTH); 120920968054SVladimir Oltean vcap->entry_width -= vcap->tg_width * vcap->sw_count; 121020968054SVladimir Oltean num_default_actions = ocelot_target_read(ocelot, vcap->target, 121120968054SVladimir Oltean VCAP_CONST_ACTION_DEF_CNT); 121220968054SVladimir Oltean vcap->action_count = vcap->entry_count + num_default_actions; 121320968054SVladimir Oltean vcap->action_width = ocelot_target_read(ocelot, vcap->target, 121420968054SVladimir Oltean VCAP_CONST_ACTION_WIDTH); 121520968054SVladimir Oltean /* The width of the counter memory, this is the complete width of all 121620968054SVladimir Oltean * counter-fields associated with one full-word entry. There is one 121720968054SVladimir Oltean * counter per entry sub-word (see CAP_CORE::ENTRY_SWCNT for number of 121820968054SVladimir Oltean * subwords.) 121920968054SVladimir Oltean */ 122020968054SVladimir Oltean vcap->counter_words = vcap->sw_count; 122120968054SVladimir Oltean counter_memory_width = ocelot_target_read(ocelot, vcap->target, 122220968054SVladimir Oltean VCAP_CONST_CNT_WIDTH); 122320968054SVladimir Oltean vcap->counter_width = counter_memory_width / vcap->counter_words; 122420968054SVladimir Oltean } 122520968054SVladimir Oltean 1226c1c3993eSVladimir Oltean int ocelot_vcap_init(struct ocelot *ocelot) 1227c1c3993eSVladimir Oltean { 122820968054SVladimir Oltean int i; 12293c83654fSVladimir Oltean 12303c83654fSVladimir Oltean /* Create a policer that will drop the frames for the cpu. 12313c83654fSVladimir Oltean * This policer will be used as action in the acl rules to drop 12323c83654fSVladimir Oltean * frames. 12333c83654fSVladimir Oltean */ 12343c83654fSVladimir Oltean ocelot_write_gix(ocelot, 0x299, ANA_POL_MODE_CFG, 12353c83654fSVladimir Oltean OCELOT_POLICER_DISCARD); 12363c83654fSVladimir Oltean ocelot_write_gix(ocelot, 0x1, ANA_POL_PIR_CFG, 12373c83654fSVladimir Oltean OCELOT_POLICER_DISCARD); 12383c83654fSVladimir Oltean ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_PIR_STATE, 12393c83654fSVladimir Oltean OCELOT_POLICER_DISCARD); 12403c83654fSVladimir Oltean ocelot_write_gix(ocelot, 0x0, ANA_POL_CIR_CFG, 12413c83654fSVladimir Oltean OCELOT_POLICER_DISCARD); 12423c83654fSVladimir Oltean ocelot_write_gix(ocelot, 0x3fffff, ANA_POL_CIR_STATE, 12433c83654fSVladimir Oltean OCELOT_POLICER_DISCARD); 12443c83654fSVladimir Oltean 124520968054SVladimir Oltean for (i = 0; i < OCELOT_NUM_VCAP_BLOCKS; i++) { 12461397a2ebSVladimir Oltean struct ocelot_vcap_block *block = &ocelot->block[i]; 124720968054SVladimir Oltean struct vcap_props *vcap = &ocelot->vcap[i]; 124820968054SVladimir Oltean 12491397a2ebSVladimir Oltean INIT_LIST_HEAD(&block->rules); 12501397a2ebSVladimir Oltean block->pol_lpr = OCELOT_POLICER_DISCARD - 1; 12511397a2ebSVladimir Oltean 125220968054SVladimir Oltean ocelot_vcap_detect_constants(ocelot, vcap); 125320968054SVladimir Oltean ocelot_vcap_init_one(ocelot, vcap); 125420968054SVladimir Oltean } 125520968054SVladimir Oltean 12561397a2ebSVladimir Oltean INIT_LIST_HEAD(&ocelot->dummy_rules); 12573c83654fSVladimir Oltean 12583c83654fSVladimir Oltean return 0; 12593c83654fSVladimir Oltean } 1260